blob: 6351fbf10ddc8f608b967b7491b71f455dc77a6e [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;
63 if (hw_ip.dram_size > 0)
64 hw_ip.dram_enabled = 1;
65 hw_ip.num_of_events = prop->num_of_events;
66 memcpy(hw_ip.armcp_version,
67 prop->armcp_info.armcp_version, VERSION_MAX_LEN);
Oded Gabbay8c844872019-02-28 10:46:24 +020068 hw_ip.armcp_cpld_version = __le32_to_cpu(prop->armcp_info.cpld_version);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020069 hw_ip.psoc_pci_pll_nr = prop->psoc_pci_pll_nr;
70 hw_ip.psoc_pci_pll_nf = prop->psoc_pci_pll_nf;
71 hw_ip.psoc_pci_pll_od = prop->psoc_pci_pll_od;
72 hw_ip.psoc_pci_pll_div_factor = prop->psoc_pci_pll_div_factor;
73
74 return copy_to_user(out, &hw_ip,
75 min((size_t)size, sizeof(hw_ip))) ? -EFAULT : 0;
76}
77
78static int hw_events_info(struct hl_device *hdev, struct hl_info_args *args)
79{
80 u32 size, max_size = args->return_size;
81 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
82 void *arr;
83
84 if ((!max_size) || (!out))
85 return -EINVAL;
86
87 arr = hdev->asic_funcs->get_events_stat(hdev, &size);
88
89 return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0;
90}
91
Oded Gabbay02e921e2019-07-30 11:48:02 +030092static int dram_usage_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020093{
Oded Gabbay02e921e2019-07-30 11:48:02 +030094 struct hl_device *hdev = hpriv->hdev;
Oded Gabbayd8dd7b02019-02-16 00:39:23 +020095 struct hl_info_dram_usage dram_usage = {0};
96 u32 max_size = args->return_size;
97 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
98 struct asic_fixed_properties *prop = &hdev->asic_prop;
99 u64 dram_kmd_size;
100
101 if ((!max_size) || (!out))
102 return -EINVAL;
103
104 dram_kmd_size = (prop->dram_user_base_address -
105 prop->dram_base_address);
106 dram_usage.dram_free_mem = (prop->dram_size - dram_kmd_size) -
107 atomic64_read(&hdev->dram_used_mem);
Oded Gabbay02e921e2019-07-30 11:48:02 +0300108 if (hpriv->ctx)
109 dram_usage.ctx_dram_mem =
110 atomic64_read(&hpriv->ctx->dram_phys_mem);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200111
112 return copy_to_user(out, &dram_usage,
113 min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
114}
115
116static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
117{
118 struct hl_info_hw_idle hw_idle = {0};
119 u32 max_size = args->return_size;
120 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
121
122 if ((!max_size) || (!out))
123 return -EINVAL;
124
Tomer Tayare8960ca2019-07-01 13:59:45 +0000125 hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev,
126 &hw_idle.busy_engines_mask, NULL);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200127
128 return copy_to_user(out, &hw_idle,
129 min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
130}
131
Omer Shpigelman315bc052019-04-01 22:31:22 +0300132static int debug_coresight(struct hl_device *hdev, struct hl_debug_args *args)
133{
134 struct hl_debug_params *params;
135 void *input = NULL, *output = NULL;
136 int rc;
137
138 params = kzalloc(sizeof(*params), GFP_KERNEL);
139 if (!params)
140 return -ENOMEM;
141
142 params->reg_idx = args->reg_idx;
143 params->enable = args->enable;
144 params->op = args->op;
145
146 if (args->input_ptr && args->input_size) {
Omer Shpigelman8d175932019-08-06 07:37:59 +0000147 input = kzalloc(hl_debug_struct_size[args->op], GFP_KERNEL);
148 if (!input) {
149 rc = -ENOMEM;
150 goto out;
151 }
152
153 if (copy_from_user(input, u64_to_user_ptr(args->input_ptr),
154 args->input_size)) {
155 rc = -EFAULT;
156 dev_err(hdev->dev, "failed to copy input debug data\n");
Omer Shpigelman315bc052019-04-01 22:31:22 +0300157 goto out;
158 }
159
160 params->input = input;
161 }
162
163 if (args->output_ptr && args->output_size) {
164 output = kzalloc(args->output_size, GFP_KERNEL);
165 if (!output) {
166 rc = -ENOMEM;
167 goto out;
168 }
169
170 params->output = output;
171 params->output_size = args->output_size;
172 }
173
174 rc = hdev->asic_funcs->debug_coresight(hdev, params);
175 if (rc) {
176 dev_err(hdev->dev,
177 "debug coresight operation failed %d\n", rc);
178 goto out;
179 }
180
181 if (output) {
182 if (copy_to_user((void __user *) (uintptr_t) args->output_ptr,
183 output,
184 args->output_size)) {
185 dev_err(hdev->dev,
186 "copy to user failed in debug ioctl\n");
187 rc = -EFAULT;
188 goto out;
189 }
190 }
191
192out:
193 kfree(params);
194 kfree(output);
195 kfree(input);
196
197 return rc;
198}
199
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300200static int _hl_info_ioctl(struct hl_fpriv *hpriv, void *data,
201 struct device *dev)
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200202{
203 struct hl_info_args *args = data;
204 struct hl_device *hdev = hpriv->hdev;
205 int rc;
206
Dalit Ben Zooraa957082019-03-24 10:15:44 +0200207 /* We want to return device status even if it disabled or in reset */
208 if (args->op == HL_INFO_DEVICE_STATUS)
209 return device_status_info(hdev, args);
210
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200211 if (hl_device_disabled_or_in_reset(hdev)) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300212 dev_warn_ratelimited(dev,
Oded Gabbay3f5398c2019-04-06 15:41:35 +0300213 "Device is %s. Can't execute INFO IOCTL\n",
214 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200215 return -EBUSY;
216 }
217
218 switch (args->op) {
219 case HL_INFO_HW_IP_INFO:
220 rc = hw_ip_info(hdev, args);
221 break;
222
223 case HL_INFO_HW_EVENTS:
224 rc = hw_events_info(hdev, args);
225 break;
226
227 case HL_INFO_DRAM_USAGE:
Oded Gabbay02e921e2019-07-30 11:48:02 +0300228 rc = dram_usage_info(hpriv, args);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200229 break;
230
231 case HL_INFO_HW_IDLE:
232 rc = hw_idle(hdev, args);
233 break;
234
235 default:
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300236 dev_err(dev, "Invalid request %d\n", args->op);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200237 rc = -ENOTTY;
238 break;
239 }
240
241 return rc;
242}
243
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300244static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data)
245{
246 return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev);
247}
248
249static int hl_info_ioctl_control(struct hl_fpriv *hpriv, void *data)
250{
251 return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev_ctrl);
252}
253
Omer Shpigelman315bc052019-04-01 22:31:22 +0300254static int hl_debug_ioctl(struct hl_fpriv *hpriv, void *data)
255{
256 struct hl_debug_args *args = data;
257 struct hl_device *hdev = hpriv->hdev;
258 int rc = 0;
259
260 if (hl_device_disabled_or_in_reset(hdev)) {
261 dev_warn_ratelimited(hdev->dev,
262 "Device is %s. Can't execute DEBUG IOCTL\n",
263 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
264 return -EBUSY;
265 }
266
267 switch (args->op) {
268 case HL_DEBUG_OP_ETR:
269 case HL_DEBUG_OP_ETF:
270 case HL_DEBUG_OP_STM:
271 case HL_DEBUG_OP_FUNNEL:
272 case HL_DEBUG_OP_BMON:
273 case HL_DEBUG_OP_SPMU:
274 case HL_DEBUG_OP_TIMESTAMP:
Oded Gabbay19734972019-05-04 17:36:06 +0300275 if (!hdev->in_debug) {
Oded Gabbay29a7aad2019-06-06 09:28:45 +0300276 dev_err_ratelimited(hdev->dev,
Oded Gabbay19734972019-05-04 17:36:06 +0300277 "Rejecting debug configuration request because device not in debug mode\n");
278 return -EFAULT;
279 }
Omer Shpigelman315bc052019-04-01 22:31:22 +0300280 args->input_size =
281 min(args->input_size, hl_debug_struct_size[args->op]);
282 rc = debug_coresight(hdev, args);
283 break;
Oded Gabbay19734972019-05-04 17:36:06 +0300284 case HL_DEBUG_OP_SET_MODE:
285 rc = hl_device_set_debug_mode(hdev, (bool) args->enable);
286 break;
Omer Shpigelman315bc052019-04-01 22:31:22 +0300287 default:
288 dev_err(hdev->dev, "Invalid request %d\n", args->op);
289 rc = -ENOTTY;
290 break;
291 }
292
293 return rc;
294}
295
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200296#define HL_IOCTL_DEF(ioctl, _func) \
297 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
298
299static const struct hl_ioctl_desc hl_ioctls[] = {
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200300 HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl),
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200301 HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl),
302 HL_IOCTL_DEF(HL_IOCTL_CS, hl_cs_ioctl),
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200303 HL_IOCTL_DEF(HL_IOCTL_WAIT_CS, hl_cs_wait_ioctl),
Omer Shpigelman315bc052019-04-01 22:31:22 +0300304 HL_IOCTL_DEF(HL_IOCTL_MEMORY, hl_mem_ioctl),
305 HL_IOCTL_DEF(HL_IOCTL_DEBUG, hl_debug_ioctl)
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200306};
307
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300308static const struct hl_ioctl_desc hl_ioctls_control[] = {
309 HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl_control)
310};
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200311
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300312static long _hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg,
313 const struct hl_ioctl_desc *ioctl, struct device *dev)
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200314{
315 struct hl_fpriv *hpriv = filep->private_data;
316 struct hl_device *hdev = hpriv->hdev;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200317 unsigned int nr = _IOC_NR(cmd);
318 char stack_kdata[128] = {0};
319 char *kdata = NULL;
320 unsigned int usize, asize;
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300321 hl_ioctl_t *func;
322 u32 hl_size;
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200323 int retcode;
324
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200325 if (hdev->hard_reset_pending) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300326 dev_crit_ratelimited(hdev->dev_ctrl,
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200327 "Device HARD reset pending! Please close FD\n");
328 return -ENODEV;
329 }
330
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200331 /* Do not trust userspace, use our own definition */
332 func = ioctl->func;
333
334 if (unlikely(!func)) {
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300335 dev_dbg(dev, "no function\n");
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200336 retcode = -ENOTTY;
337 goto out_err;
338 }
339
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300340 hl_size = _IOC_SIZE(ioctl->cmd);
341 usize = asize = _IOC_SIZE(cmd);
342 if (hl_size > asize)
343 asize = hl_size;
344
345 cmd = ioctl->cmd;
346
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200347 if (cmd & (IOC_IN | IOC_OUT)) {
348 if (asize <= sizeof(stack_kdata)) {
349 kdata = stack_kdata;
350 } else {
351 kdata = kzalloc(asize, GFP_KERNEL);
352 if (!kdata) {
353 retcode = -ENOMEM;
354 goto out_err;
355 }
356 }
357 }
358
359 if (cmd & IOC_IN) {
360 if (copy_from_user(kdata, (void __user *)arg, usize)) {
361 retcode = -EFAULT;
362 goto out_err;
363 }
364 } else if (cmd & IOC_OUT) {
365 memset(kdata, 0, usize);
366 }
367
368 retcode = func(hpriv, kdata);
369
370 if (cmd & IOC_OUT)
371 if (copy_to_user((void __user *)arg, kdata, usize))
372 retcode = -EFAULT;
373
374out_err:
375 if (retcode)
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300376 dev_dbg(dev, "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200377 task_pid_nr(current), cmd, nr);
378
379 if (kdata != stack_kdata)
380 kfree(kdata);
381
382 return retcode;
383}
Oded Gabbay4d6a7752019-07-30 09:10:50 +0300384
385long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
386{
387 struct hl_fpriv *hpriv = filep->private_data;
388 struct hl_device *hdev = hpriv->hdev;
389 const struct hl_ioctl_desc *ioctl = NULL;
390 unsigned int nr = _IOC_NR(cmd);
391
392 if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
393 ioctl = &hl_ioctls[nr];
394 } else {
395 dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
396 task_pid_nr(current), nr);
397 return -ENOTTY;
398 }
399
400 return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev);
401}
402
403long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg)
404{
405 struct hl_fpriv *hpriv = filep->private_data;
406 struct hl_device *hdev = hpriv->hdev;
407 const struct hl_ioctl_desc *ioctl = NULL;
408 unsigned int nr = _IOC_NR(cmd);
409
410 if (nr == _IOC_NR(HL_IOCTL_INFO)) {
411 ioctl = &hl_ioctls_control[nr];
412 } else {
413 dev_err(hdev->dev_ctrl, "invalid ioctl: pid=%d, nr=0x%02x\n",
414 task_pid_nr(current), nr);
415 return -ENOTTY;
416 }
417
418 return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev_ctrl);
419}