blob: c641c7eb6f7cbc2dbc9ff7a9cf46414c815744aa [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
92static 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
113static 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
Tomer Tayarc811f7b2019-03-07 14:26:02 +0200122 hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev, NULL, 0);
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200123
124 return copy_to_user(out, &hw_idle,
125 min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
126}
127
Omer Shpigelman315bc052019-04-01 22:31:22 +0300128static 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) {
Arnd Bergmannf99bc332019-06-17 14:41:33 +0200143 input = memdup_user(u64_to_user_ptr(args->input_ptr),
Omer Shpigelman315bc052019-04-01 22:31:22 +0300144 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
185out:
186 kfree(params);
187 kfree(output);
188 kfree(input);
189
190 return rc;
191}
192
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200193static 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
Dalit Ben Zooraa957082019-03-24 10:15:44 +0200199 /* 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
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200203 if (hl_device_disabled_or_in_reset(hdev)) {
Oded Gabbay680cb392019-03-05 13:53:22 +0200204 dev_warn_ratelimited(hdev->dev,
Oded Gabbay3f5398c2019-04-06 15:41:35 +0300205 "Device is %s. Can't execute INFO IOCTL\n",
206 atomic_read(&hdev->in_reset) ? "in_reset" : "disabled");
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200207 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
Omer Shpigelman315bc052019-04-01 22:31:22 +0300236static 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:
Oded Gabbay19734972019-05-04 17:36:06 +0300257 if (!hdev->in_debug) {
Oded Gabbay29a7aad2019-06-06 09:28:45 +0300258 dev_err_ratelimited(hdev->dev,
Oded Gabbay19734972019-05-04 17:36:06 +0300259 "Rejecting debug configuration request because device not in debug mode\n");
260 return -EFAULT;
261 }
Omer Shpigelman315bc052019-04-01 22:31:22 +0300262 args->input_size =
263 min(args->input_size, hl_debug_struct_size[args->op]);
264 rc = debug_coresight(hdev, args);
265 break;
Oded Gabbay19734972019-05-04 17:36:06 +0300266 case HL_DEBUG_OP_SET_MODE:
267 rc = hl_device_set_debug_mode(hdev, (bool) args->enable);
268 break;
Omer Shpigelman315bc052019-04-01 22:31:22 +0300269 default:
270 dev_err(hdev->dev, "Invalid request %d\n", args->op);
271 rc = -ENOTTY;
272 break;
273 }
274
275 return rc;
276}
277
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200278#define HL_IOCTL_DEF(ioctl, _func) \
279 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
280
281static const struct hl_ioctl_desc hl_ioctls[] = {
Oded Gabbayd8dd7b02019-02-16 00:39:23 +0200282 HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl),
Oded Gabbayeff6f4a2019-02-16 00:39:21 +0200283 HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl),
284 HL_IOCTL_DEF(HL_IOCTL_CS, hl_cs_ioctl),
Omer Shpigelman0feaf862019-02-16 00:39:22 +0200285 HL_IOCTL_DEF(HL_IOCTL_WAIT_CS, hl_cs_wait_ioctl),
Omer Shpigelman315bc052019-04-01 22:31:22 +0300286 HL_IOCTL_DEF(HL_IOCTL_MEMORY, hl_mem_ioctl),
287 HL_IOCTL_DEF(HL_IOCTL_DEBUG, hl_debug_ioctl)
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200288};
289
290#define HL_CORE_IOCTL_COUNT ARRAY_SIZE(hl_ioctls)
291
292long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
293{
294 struct hl_fpriv *hpriv = filep->private_data;
295 struct hl_device *hdev = hpriv->hdev;
296 hl_ioctl_t *func;
297 const struct hl_ioctl_desc *ioctl = NULL;
298 unsigned int nr = _IOC_NR(cmd);
299 char stack_kdata[128] = {0};
300 char *kdata = NULL;
301 unsigned int usize, asize;
302 int retcode;
303
Oded Gabbayf8c8c7d52019-02-16 00:39:20 +0200304 if (hdev->hard_reset_pending) {
305 dev_crit_ratelimited(hdev->dev,
306 "Device HARD reset pending! Please close FD\n");
307 return -ENODEV;
308 }
309
Oded Gabbaybe5d9262019-02-16 00:39:15 +0200310 if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
311 u32 hl_size;
312
313 ioctl = &hl_ioctls[nr];
314
315 hl_size = _IOC_SIZE(ioctl->cmd);
316 usize = asize = _IOC_SIZE(cmd);
317 if (hl_size > asize)
318 asize = hl_size;
319
320 cmd = ioctl->cmd;
321 } else {
322 dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
323 task_pid_nr(current), nr);
324 return -ENOTTY;
325 }
326
327 /* Do not trust userspace, use our own definition */
328 func = ioctl->func;
329
330 if (unlikely(!func)) {
331 dev_dbg(hdev->dev, "no function\n");
332 retcode = -ENOTTY;
333 goto out_err;
334 }
335
336 if (cmd & (IOC_IN | IOC_OUT)) {
337 if (asize <= sizeof(stack_kdata)) {
338 kdata = stack_kdata;
339 } else {
340 kdata = kzalloc(asize, GFP_KERNEL);
341 if (!kdata) {
342 retcode = -ENOMEM;
343 goto out_err;
344 }
345 }
346 }
347
348 if (cmd & IOC_IN) {
349 if (copy_from_user(kdata, (void __user *)arg, usize)) {
350 retcode = -EFAULT;
351 goto out_err;
352 }
353 } else if (cmd & IOC_OUT) {
354 memset(kdata, 0, usize);
355 }
356
357 retcode = func(hpriv, kdata);
358
359 if (cmd & IOC_OUT)
360 if (copy_to_user((void __user *)arg, kdata, usize))
361 retcode = -EFAULT;
362
363out_err:
364 if (retcode)
365 dev_dbg(hdev->dev,
366 "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
367 task_pid_nr(current), cmd, nr);
368
369 if (kdata != stack_kdata)
370 kfree(kdata);
371
372 return retcode;
373}