]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/habanalabs/habanalabs_ioctl.c
Merge tag 'usb-5.2-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh/usb
[linux.git] / drivers / misc / habanalabs / habanalabs_ioctl.c
1 // 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
15 static 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
26 static 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
41 static 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);
68         hw_ip.armcp_cpld_version = __le32_to_cpu(prop->armcp_info.cpld_version);
69         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
78 static 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
92 static int dram_usage_info(struct hl_device *hdev, struct hl_info_args *args)
93 {
94         struct hl_info_dram_usage dram_usage = {0};
95         u32 max_size = args->return_size;
96         void __user *out = (void __user *) (uintptr_t) args->return_pointer;
97         struct asic_fixed_properties *prop = &hdev->asic_prop;
98         u64 dram_kmd_size;
99
100         if ((!max_size) || (!out))
101                 return -EINVAL;
102
103         dram_kmd_size = (prop->dram_user_base_address -
104                                 prop->dram_base_address);
105         dram_usage.dram_free_mem = (prop->dram_size - dram_kmd_size) -
106                                         atomic64_read(&hdev->dram_used_mem);
107         dram_usage.ctx_dram_mem = atomic64_read(&hdev->user_ctx->dram_phys_mem);
108
109         return copy_to_user(out, &dram_usage,
110                 min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
111 }
112
113 static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
114 {
115         struct hl_info_hw_idle hw_idle = {0};
116         u32 max_size = args->return_size;
117         void __user *out = (void __user *) (uintptr_t) args->return_pointer;
118
119         if ((!max_size) || (!out))
120                 return -EINVAL;
121
122         hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev, NULL, 0);
123
124         return copy_to_user(out, &hw_idle,
125                 min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
126 }
127
128 static int debug_coresight(struct hl_device *hdev, struct hl_debug_args *args)
129 {
130         struct hl_debug_params *params;
131         void *input = NULL, *output = NULL;
132         int rc;
133
134         params = kzalloc(sizeof(*params), GFP_KERNEL);
135         if (!params)
136                 return -ENOMEM;
137
138         params->reg_idx = args->reg_idx;
139         params->enable = args->enable;
140         params->op = args->op;
141
142         if (args->input_ptr && args->input_size) {
143                 input = memdup_user((const void __user *) args->input_ptr,
144                                         args->input_size);
145                 if (IS_ERR(input)) {
146                         rc = PTR_ERR(input);
147                         input = NULL;
148                         dev_err(hdev->dev,
149                                 "error %d when copying input debug data\n", rc);
150                         goto out;
151                 }
152
153                 params->input = input;
154         }
155
156         if (args->output_ptr && args->output_size) {
157                 output = kzalloc(args->output_size, GFP_KERNEL);
158                 if (!output) {
159                         rc = -ENOMEM;
160                         goto out;
161                 }
162
163                 params->output = output;
164                 params->output_size = args->output_size;
165         }
166
167         rc = hdev->asic_funcs->debug_coresight(hdev, params);
168         if (rc) {
169                 dev_err(hdev->dev,
170                         "debug coresight operation failed %d\n", rc);
171                 goto out;
172         }
173
174         if (output) {
175                 if (copy_to_user((void __user *) (uintptr_t) args->output_ptr,
176                                         output,
177                                         args->output_size)) {
178                         dev_err(hdev->dev,
179                                 "copy to user failed in debug ioctl\n");
180                         rc = -EFAULT;
181                         goto out;
182                 }
183         }
184
185 out:
186         kfree(params);
187         kfree(output);
188         kfree(input);
189
190         return rc;
191 }
192
193 static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data)
194 {
195         struct hl_info_args *args = data;
196         struct hl_device *hdev = hpriv->hdev;
197         int rc;
198
199         /* We want to return device status even if it disabled or in reset */
200         if (args->op == HL_INFO_DEVICE_STATUS)
201                 return device_status_info(hdev, args);
202
203         if (hl_device_disabled_or_in_reset(hdev)) {
204                 dev_warn_ratelimited(hdev->dev,
205                         "Device is %s. Can't execute INFO IOCTL\n",
206                         atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
207                 return -EBUSY;
208         }
209
210         switch (args->op) {
211         case HL_INFO_HW_IP_INFO:
212                 rc = hw_ip_info(hdev, args);
213                 break;
214
215         case HL_INFO_HW_EVENTS:
216                 rc = hw_events_info(hdev, args);
217                 break;
218
219         case HL_INFO_DRAM_USAGE:
220                 rc = dram_usage_info(hdev, args);
221                 break;
222
223         case HL_INFO_HW_IDLE:
224                 rc = hw_idle(hdev, args);
225                 break;
226
227         default:
228                 dev_err(hdev->dev, "Invalid request %d\n", args->op);
229                 rc = -ENOTTY;
230                 break;
231         }
232
233         return rc;
234 }
235
236 static int hl_debug_ioctl(struct hl_fpriv *hpriv, void *data)
237 {
238         struct hl_debug_args *args = data;
239         struct hl_device *hdev = hpriv->hdev;
240         int rc = 0;
241
242         if (hl_device_disabled_or_in_reset(hdev)) {
243                 dev_warn_ratelimited(hdev->dev,
244                         "Device is %s. Can't execute DEBUG IOCTL\n",
245                         atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
246                 return -EBUSY;
247         }
248
249         switch (args->op) {
250         case HL_DEBUG_OP_ETR:
251         case HL_DEBUG_OP_ETF:
252         case HL_DEBUG_OP_STM:
253         case HL_DEBUG_OP_FUNNEL:
254         case HL_DEBUG_OP_BMON:
255         case HL_DEBUG_OP_SPMU:
256         case HL_DEBUG_OP_TIMESTAMP:
257                 args->input_size =
258                         min(args->input_size, hl_debug_struct_size[args->op]);
259                 rc = debug_coresight(hdev, args);
260                 break;
261         default:
262                 dev_err(hdev->dev, "Invalid request %d\n", args->op);
263                 rc = -ENOTTY;
264                 break;
265         }
266
267         return rc;
268 }
269
270 #define HL_IOCTL_DEF(ioctl, _func) \
271         [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
272
273 static const struct hl_ioctl_desc hl_ioctls[] = {
274         HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl),
275         HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl),
276         HL_IOCTL_DEF(HL_IOCTL_CS, hl_cs_ioctl),
277         HL_IOCTL_DEF(HL_IOCTL_WAIT_CS, hl_cs_wait_ioctl),
278         HL_IOCTL_DEF(HL_IOCTL_MEMORY, hl_mem_ioctl),
279         HL_IOCTL_DEF(HL_IOCTL_DEBUG, hl_debug_ioctl)
280 };
281
282 #define HL_CORE_IOCTL_COUNT     ARRAY_SIZE(hl_ioctls)
283
284 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
285 {
286         struct hl_fpriv *hpriv = filep->private_data;
287         struct hl_device *hdev = hpriv->hdev;
288         hl_ioctl_t *func;
289         const struct hl_ioctl_desc *ioctl = NULL;
290         unsigned int nr = _IOC_NR(cmd);
291         char stack_kdata[128] = {0};
292         char *kdata = NULL;
293         unsigned int usize, asize;
294         int retcode;
295
296         if (hdev->hard_reset_pending) {
297                 dev_crit_ratelimited(hdev->dev,
298                         "Device HARD reset pending! Please close FD\n");
299                 return -ENODEV;
300         }
301
302         if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
303                 u32 hl_size;
304
305                 ioctl = &hl_ioctls[nr];
306
307                 hl_size = _IOC_SIZE(ioctl->cmd);
308                 usize = asize = _IOC_SIZE(cmd);
309                 if (hl_size > asize)
310                         asize = hl_size;
311
312                 cmd = ioctl->cmd;
313         } else {
314                 dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
315                           task_pid_nr(current), nr);
316                 return -ENOTTY;
317         }
318
319         /* Do not trust userspace, use our own definition */
320         func = ioctl->func;
321
322         if (unlikely(!func)) {
323                 dev_dbg(hdev->dev, "no function\n");
324                 retcode = -ENOTTY;
325                 goto out_err;
326         }
327
328         if (cmd & (IOC_IN | IOC_OUT)) {
329                 if (asize <= sizeof(stack_kdata)) {
330                         kdata = stack_kdata;
331                 } else {
332                         kdata = kzalloc(asize, GFP_KERNEL);
333                         if (!kdata) {
334                                 retcode = -ENOMEM;
335                                 goto out_err;
336                         }
337                 }
338         }
339
340         if (cmd & IOC_IN) {
341                 if (copy_from_user(kdata, (void __user *)arg, usize)) {
342                         retcode = -EFAULT;
343                         goto out_err;
344                 }
345         } else if (cmd & IOC_OUT) {
346                 memset(kdata, 0, usize);
347         }
348
349         retcode = func(hpriv, kdata);
350
351         if (cmd & IOC_OUT)
352                 if (copy_to_user((void __user *)arg, kdata, usize))
353                         retcode = -EFAULT;
354
355 out_err:
356         if (retcode)
357                 dev_dbg(hdev->dev,
358                         "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
359                           task_pid_nr(current), cmd, nr);
360
361         if (kdata != stack_kdata)
362                 kfree(kdata);
363
364         return retcode;
365 }