blob: 5d9c269d99db0712df2f0ee963ad76b4e53442ae [file] [log] [blame]
Oded Gabbaybe5d9262019-02-16 00:39:15 +02001// SPDX-License-Identifier: GPL-2.0
2
3/*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
8#include <uapi/misc/habanalabs.h>
9#include "habanalabs.h"
10
11#include <linux/fs.h>
12#include <linux/uaccess.h>
13#include <linux/slab.h>
14
Omer Shpigelman315bc052019-04-01 22:31:22 +030015static u32 hl_debug_struct_size[HL_DEBUG_OP_TIMESTAMP + 1] = {
16 [HL_DEBUG_OP_ETR] = sizeof(struct hl_debug_params_etr),
17 [HL_DEBUG_OP_ETF] = sizeof(struct hl_debug_params_etf),
18 [HL_DEBUG_OP_STM] = sizeof(struct hl_debug_params_stm),
19 [HL_DEBUG_OP_FUNNEL] = 0,
20 [HL_DEBUG_OP_BMON] = sizeof(struct hl_debug_params_bmon),
21 [HL_DEBUG_OP_SPMU] = sizeof(struct hl_debug_params_spmu),
22 [HL_DEBUG_OP_TIMESTAMP] = 0
23
24};
25
Dalit Ben Zooraa957082019-03-24 10:15:44 +020026static int device_status_info(struct hl_device *hdev, struct hl_info_args *args)
27{
28 struct hl_info_device_status dev_stat = {0};
29 u32 size = args->return_size;
30 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
31
32 if ((!size) || (!out))
33 return -EINVAL;
34
35 dev_stat.status = hl_device_status(hdev);
36
37 return copy_to_user(out, &dev_stat,
38 min((size_t)size, sizeof(dev_stat))) ? -EFAULT : 0;
39}
40
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020041static int hw_ip_info(struct hl_device *hdev, struct hl_info_args *args)
42{
43 struct hl_info_hw_ip_info hw_ip = {0};
44 u32 size = args->return_size;
45 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
46 struct asic_fixed_properties *prop = &hdev->asic_prop;
47 u64 sram_kmd_size, dram_kmd_size;
48
49 if ((!size) || (!out))
50 return -EINVAL;
51
52 sram_kmd_size = (prop->sram_user_base_address -
53 prop->sram_base_address);
54 dram_kmd_size = (prop->dram_user_base_address -
55 prop->dram_base_address);
56
57 hw_ip.device_id = hdev->asic_funcs->get_pci_id(hdev);
58 hw_ip.sram_base_address = prop->sram_user_base_address;
59 hw_ip.dram_base_address = prop->dram_user_base_address;
60 hw_ip.tpc_enabled_mask = prop->tpc_enabled_mask;
61 hw_ip.sram_size = prop->sram_size - sram_kmd_size;
62 hw_ip.dram_size = prop->dram_size - dram_kmd_size;
Oded Gabbaye16ee412019-11-16 12:29:50 +020063 if (hw_ip.dram_size > PAGE_SIZE)
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020064 hw_ip.dram_enabled = 1;
65 hw_ip.num_of_events = prop->num_of_events;
Oded Gabbay91edbf22019-10-16 11:53:52 +030066
67 memcpy(hw_ip.armcp_version, prop->armcp_info.armcp_version,
68 min(VERSION_MAX_LEN, HL_INFO_VERSION_MAX_LEN));
69
70 memcpy(hw_ip.card_name, prop->armcp_info.card_name,
71 min(CARD_NAME_MAX_LEN, HL_INFO_CARD_NAME_MAX_LEN));
72
Oded Gabbayfe9a52c2019-08-08 17:05:45 +030073 hw_ip.armcp_cpld_version = le32_to_cpu(prop->armcp_info.cpld_version);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020074 hw_ip.psoc_pci_pll_nr = prop->psoc_pci_pll_nr;
75 hw_ip.psoc_pci_pll_nf = prop->psoc_pci_pll_nf;
76 hw_ip.psoc_pci_pll_od = prop->psoc_pci_pll_od;
77 hw_ip.psoc_pci_pll_div_factor = prop->psoc_pci_pll_div_factor;
78
79 return copy_to_user(out, &hw_ip,
80 min((size_t)size, sizeof(hw_ip))) ? -EFAULT : 0;
81}
82
Oded Gabbaye9730762019-08-28 21:51:52 +030083static int hw_events_info(struct hl_device *hdev, bool aggregate,
84 struct hl_info_args *args)
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020085{
86 u32 size, max_size = args->return_size;
87 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
88 void *arr;
89
90 if ((!max_size) || (!out))
91 return -EINVAL;
92
Oded Gabbaye9730762019-08-28 21:51:52 +030093 arr = hdev->asic_funcs->get_events_stat(hdev, aggregate, &size);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020094
95 return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0;
96}
97
Oded Gabbay02e921e2019-07-30 11:48:02 +030098static int dram_usage_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020099{
Oded Gabbay02e921e2019-07-30 11:48:02 +0300100 struct hl_device *hdev = hpriv->hdev;
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200101 struct hl_info_dram_usage dram_usage = {0};
102 u32 max_size = args->return_size;
103 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
104 struct asic_fixed_properties *prop = &hdev->asic_prop;
105 u64 dram_kmd_size;
106
107 if ((!max_size) || (!out))
108 return -EINVAL;
109
110 dram_kmd_size = (prop->dram_user_base_address -
111 prop->dram_base_address);
112 dram_usage.dram_free_mem = (prop->dram_size - dram_kmd_size) -
113 atomic64_read(&hdev->dram_used_mem);
Oded Gabbay02e921e2019-07-30 11:48:02 +0300114 if (hpriv->ctx)
115 dram_usage.ctx_dram_mem =
116 atomic64_read(&hpriv->ctx->dram_phys_mem);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200117
118 return copy_to_user(out, &dram_usage,
119 min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
120}
121
122static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
123{
124 struct hl_info_hw_idle hw_idle = {0};
125 u32 max_size = args->return_size;
126 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
127
128 if ((!max_size) || (!out))
129 return -EINVAL;
130
Tomer Tayare8960ca2019-07-01 13:59:45 +0000131 hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev,
132 &hw_idle.busy_engines_mask, NULL);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200133
134 return copy_to_user(out, &hw_idle,
135 min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
136}
137
Omer Shpigelman315bc052019-04-01 22:31:22 +0300138static int debug_coresight(struct hl_device *hdev, struct hl_debug_args *args)
139{
140 struct hl_debug_params *params;
141 void *input = NULL, *output = NULL;
142 int rc;
143
144 params = kzalloc(sizeof(*params), GFP_KERNEL);
145 if (!params)
146 return -ENOMEM;
147
148 params->reg_idx = args->reg_idx;
149 params->enable = args->enable;
150 params->op = args->op;
151
152 if (args->input_ptr && args->input_size) {
Omer Shpigelman8d175932019-08-06 07:37:59 +0000153 input = kzalloc(hl_debug_struct_size[args->op], GFP_KERNEL);
154 if (!input) {
155 rc = -ENOMEM;
156 goto out;
157 }
158
159 if (copy_from_user(input, u64_to_user_ptr(args->input_ptr),
160 args->input_size)) {
161 rc = -EFAULT;
162 dev_err(hdev->dev, "failed to copy input debug data\n");
Omer Shpigelman315bc052019-04-01 22:31:22 +0300163 goto out;
164 }
165
166 params->input = input;
167 }
168
169 if (args->output_ptr && args->output_size) {
170 output = kzalloc(args->output_size, GFP_KERNEL);
171 if (!output) {
172 rc = -ENOMEM;
173 goto out;
174 }
175
176 params->output = output;
177 params->output_size = args->output_size;
178 }
179
180 rc = hdev->asic_funcs->debug_coresight(hdev, params);
181 if (rc) {
182 dev_err(hdev->dev,
183 "debug coresight operation failed %d\n", rc);
184 goto out;
185 }
186
Oded Gabbaye16ee412019-11-16 12:29:50 +0200187 if (output && copy_to_user((void __user *) (uintptr_t) args->output_ptr,
188 output, args->output_size)) {
189 dev_err(hdev->dev, "copy to user failed in debug ioctl\n");
190 rc = -EFAULT;
191 goto out;
Omer Shpigelman315bc052019-04-01 22:31:22 +0300192 }
193
Oded Gabbaye16ee412019-11-16 12:29:50 +0200194
Omer Shpigelman315bc052019-04-01 22:31:22 +0300195out:
196 kfree(params);
197 kfree(output);
198 kfree(input);
199
200 return rc;
201}
202
Oded Gabbay75b3cb22019-08-28 17:32:04 +0300203static int device_utilization(struct hl_device *hdev, struct hl_info_args *args)
204{
205 struct hl_info_device_utilization device_util = {0};
206 u32 max_size = args->return_size;
207 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
208
209 if ((!max_size) || (!out))
210 return -EINVAL;
211
212 if ((args->period_ms < 100) || (args->period_ms > 1000) ||
213 (args->period_ms % 100)) {
214 dev_err(hdev->dev,
215 "period %u must be between 100 - 1000 and must be divisible by 100\n",
216 args->period_ms);
217 return -EINVAL;
218 }
219
220 device_util.utilization = hl_device_utilization(hdev, args->period_ms);
221
222 return copy_to_user(out, &device_util,
223 min((size_t) max_size, sizeof(device_util))) ? -EFAULT : 0;
224}
225
Oded Gabbay62c1e122019-10-10 15:48:59 +0300226static int get_clk_rate(struct hl_device *hdev, struct hl_info_args *args)
227{
228 struct hl_info_clk_rate clk_rate = {0};
229 u32 max_size = args->return_size;
230 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
231 int rc;
232
233 if ((!max_size) || (!out))
234 return -EINVAL;
235
236 rc = hdev->asic_funcs->get_clk_rate(hdev, &clk_rate.cur_clk_rate_mhz,
237 &clk_rate.max_clk_rate_mhz);
238 if (rc)
239 return rc;
240
241 return copy_to_user(out, &clk_rate,
242 min((size_t) max_size, sizeof(clk_rate))) ? -EFAULT : 0;
243}
244
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300245static int _hl_info_ioctl(struct hl_fpriv *hpriv, void *data,
246 struct device *dev)
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200247{
248 struct hl_info_args *args = data;
249 struct hl_device *hdev = hpriv->hdev;
250 int rc;
251
Tomer Tayar129b6a92019-08-08 12:30:22 +0000252 /*
253 * Information is returned for the following opcodes even if the device
254 * is disabled or in reset.
255 */
256 switch (args->op) {
257 case HL_INFO_HW_IP_INFO:
258 return hw_ip_info(hdev, args);
259
260 case HL_INFO_DEVICE_STATUS:
Dalit Ben Zooraa957082019-03-24 10:15:44 +0200261 return device_status_info(hdev, args);
262
Tomer Tayar129b6a92019-08-08 12:30:22 +0000263 default:
264 break;
265 }
266
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200267 if (hl_device_disabled_or_in_reset(hdev)) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300268 dev_warn_ratelimited(dev,
Oded Gabbay3f5398c2019-04-06 15:41:35 +0300269 "Device is %s. Can't execute INFO IOCTL\n",
270 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200271 return -EBUSY;
272 }
273
274 switch (args->op) {
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200275 case HL_INFO_HW_EVENTS:
Oded Gabbaye9730762019-08-28 21:51:52 +0300276 rc = hw_events_info(hdev, false, args);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200277 break;
278
279 case HL_INFO_DRAM_USAGE:
Oded Gabbay02e921e2019-07-30 11:48:02 +0300280 rc = dram_usage_info(hpriv, args);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200281 break;
282
283 case HL_INFO_HW_IDLE:
284 rc = hw_idle(hdev, args);
285 break;
286
Oded Gabbay75b3cb22019-08-28 17:32:04 +0300287 case HL_INFO_DEVICE_UTILIZATION:
288 rc = device_utilization(hdev, args);
289 break;
290
Oded Gabbaye9730762019-08-28 21:51:52 +0300291 case HL_INFO_HW_EVENTS_AGGREGATE:
292 rc = hw_events_info(hdev, true, args);
293 break;
294
Oded Gabbay62c1e122019-10-10 15:48:59 +0300295 case HL_INFO_CLK_RATE:
296 rc = get_clk_rate(hdev, args);
297 break;
298
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200299 default:
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300300 dev_err(dev, "Invalid request %d\n", args->op);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200301 rc = -ENOTTY;
302 break;
303 }
304
305 return rc;
306}
307
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300308static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data)
309{
310 return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev);
311}
312
313static int hl_info_ioctl_control(struct hl_fpriv *hpriv, void *data)
314{
315 return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev_ctrl);
316}
317
Omer Shpigelman315bc052019-04-01 22:31:22 +0300318static int hl_debug_ioctl(struct hl_fpriv *hpriv, void *data)
319{
320 struct hl_debug_args *args = data;
321 struct hl_device *hdev = hpriv->hdev;
322 int rc = 0;
323
324 if (hl_device_disabled_or_in_reset(hdev)) {
325 dev_warn_ratelimited(hdev->dev,
326 "Device is %s. Can't execute DEBUG IOCTL\n",
327 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
328 return -EBUSY;
329 }
330
331 switch (args->op) {
332 case HL_DEBUG_OP_ETR:
333 case HL_DEBUG_OP_ETF:
334 case HL_DEBUG_OP_STM:
335 case HL_DEBUG_OP_FUNNEL:
336 case HL_DEBUG_OP_BMON:
337 case HL_DEBUG_OP_SPMU:
338 case HL_DEBUG_OP_TIMESTAMP:
Oded Gabbay19734972019-05-04 17:36:06 +0300339 if (!hdev->in_debug) {
Oded Gabbay29a7aad2019-06-06 09:28:45 +0300340 dev_err_ratelimited(hdev->dev,
Oded Gabbay19734972019-05-04 17:36:06 +0300341 "Rejecting debug configuration request because device not in debug mode\n");
342 return -EFAULT;
343 }
Omer Shpigelman315bc052019-04-01 22:31:22 +0300344 args->input_size =
345 min(args->input_size, hl_debug_struct_size[args->op]);
346 rc = debug_coresight(hdev, args);
347 break;
Oded Gabbay19734972019-05-04 17:36:06 +0300348 case HL_DEBUG_OP_SET_MODE:
349 rc = hl_device_set_debug_mode(hdev, (bool) args->enable);
350 break;
Omer Shpigelman315bc052019-04-01 22:31:22 +0300351 default:
352 dev_err(hdev->dev, "Invalid request %d\n", args->op);
353 rc = -ENOTTY;
354 break;
355 }
356
357 return rc;
358}
359
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200360#define HL_IOCTL_DEF(ioctl, _func) \
361 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
362
363static const struct hl_ioctl_desc hl_ioctls[] = {
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200364 HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl),
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200365 HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl),
366 HL_IOCTL_DEF(HL_IOCTL_CS, hl_cs_ioctl),
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200367 HL_IOCTL_DEF(HL_IOCTL_WAIT_CS, hl_cs_wait_ioctl),
Omer Shpigelman315bc052019-04-01 22:31:22 +0300368 HL_IOCTL_DEF(HL_IOCTL_MEMORY, hl_mem_ioctl),
369 HL_IOCTL_DEF(HL_IOCTL_DEBUG, hl_debug_ioctl)
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200370};
371
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300372static const struct hl_ioctl_desc hl_ioctls_control[] = {
373 HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl_control)
374};
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200375
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300376static long _hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg,
377 const struct hl_ioctl_desc *ioctl, struct device *dev)
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200378{
379 struct hl_fpriv *hpriv = filep->private_data;
380 struct hl_device *hdev = hpriv->hdev;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200381 unsigned int nr = _IOC_NR(cmd);
382 char stack_kdata[128] = {0};
383 char *kdata = NULL;
384 unsigned int usize, asize;
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300385 hl_ioctl_t *func;
386 u32 hl_size;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200387 int retcode;
388
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200389 if (hdev->hard_reset_pending) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300390 dev_crit_ratelimited(hdev->dev_ctrl,
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200391 "Device HARD reset pending! Please close FD\n");
392 return -ENODEV;
393 }
394
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200395 /* Do not trust userspace, use our own definition */
396 func = ioctl->func;
397
398 if (unlikely(!func)) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300399 dev_dbg(dev, "no function\n");
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200400 retcode = -ENOTTY;
401 goto out_err;
402 }
403
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300404 hl_size = _IOC_SIZE(ioctl->cmd);
405 usize = asize = _IOC_SIZE(cmd);
406 if (hl_size > asize)
407 asize = hl_size;
408
409 cmd = ioctl->cmd;
410
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200411 if (cmd & (IOC_IN | IOC_OUT)) {
412 if (asize <= sizeof(stack_kdata)) {
413 kdata = stack_kdata;
414 } else {
415 kdata = kzalloc(asize, GFP_KERNEL);
416 if (!kdata) {
417 retcode = -ENOMEM;
418 goto out_err;
419 }
420 }
421 }
422
423 if (cmd & IOC_IN) {
424 if (copy_from_user(kdata, (void __user *)arg, usize)) {
425 retcode = -EFAULT;
426 goto out_err;
427 }
428 } else if (cmd & IOC_OUT) {
429 memset(kdata, 0, usize);
430 }
431
432 retcode = func(hpriv, kdata);
433
Oded Gabbaye16ee412019-11-16 12:29:50 +0200434 if ((cmd & IOC_OUT) && copy_to_user((void __user *)arg, kdata, usize))
435 retcode = -EFAULT;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200436
437out_err:
438 if (retcode)
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300439 dev_dbg(dev, "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200440 task_pid_nr(current), cmd, nr);
441
442 if (kdata != stack_kdata)
443 kfree(kdata);
444
445 return retcode;
446}
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300447
448long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
449{
450 struct hl_fpriv *hpriv = filep->private_data;
451 struct hl_device *hdev = hpriv->hdev;
452 const struct hl_ioctl_desc *ioctl = NULL;
453 unsigned int nr = _IOC_NR(cmd);
454
455 if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
456 ioctl = &hl_ioctls[nr];
457 } else {
458 dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
459 task_pid_nr(current), nr);
460 return -ENOTTY;
461 }
462
463 return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev);
464}
465
466long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg)
467{
468 struct hl_fpriv *hpriv = filep->private_data;
469 struct hl_device *hdev = hpriv->hdev;
470 const struct hl_ioctl_desc *ioctl = NULL;
471 unsigned int nr = _IOC_NR(cmd);
472
473 if (nr == _IOC_NR(HL_IOCTL_INFO)) {
474 ioctl = &hl_ioctls_control[nr];
475 } else {
476 dev_err(hdev->dev_ctrl, "invalid ioctl: pid=%d, nr=0x%02x\n",
477 task_pid_nr(current), nr);
478 return -ENOTTY;
479 }
480
481 return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev_ctrl);
482}