1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include <uapi/misc/habanalabs.h>
9 #include "habanalabs.h"
12 #include <linux/uaccess.h>
13 #include <linux/slab.h>
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
26 static int device_status_info(struct hl_device *hdev, struct hl_info_args *args)
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;
32 if ((!size) || (!out))
35 dev_stat.status = hl_device_status(hdev);
37 return copy_to_user(out, &dev_stat,
38 min((size_t)size, sizeof(dev_stat))) ? -EFAULT : 0;
41 static int hw_ip_info(struct hl_device *hdev, struct hl_info_args *args)
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;
49 if ((!size) || (!out))
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);
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;
74 return copy_to_user(out, &hw_ip,
75 min((size_t)size, sizeof(hw_ip))) ? -EFAULT : 0;
78 static int hw_events_info(struct hl_device *hdev, struct hl_info_args *args)
80 u32 size, max_size = args->return_size;
81 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
84 if ((!max_size) || (!out))
87 arr = hdev->asic_funcs->get_events_stat(hdev, &size);
89 return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0;
92 static int dram_usage_info(struct hl_device *hdev, struct hl_info_args *args)
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;
100 if ((!max_size) || (!out))
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);
109 return copy_to_user(out, &dram_usage,
110 min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
113 static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
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;
119 if ((!max_size) || (!out))
122 hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev, NULL, 0);
124 return copy_to_user(out, &hw_idle,
125 min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
128 static int debug_coresight(struct hl_device *hdev, struct hl_debug_args *args)
130 struct hl_debug_params *params;
131 void *input = NULL, *output = NULL;
134 params = kzalloc(sizeof(*params), GFP_KERNEL);
138 params->reg_idx = args->reg_idx;
139 params->enable = args->enable;
140 params->op = args->op;
142 if (args->input_ptr && args->input_size) {
143 input = memdup_user((const void __user *) args->input_ptr,
149 "error %d when copying input debug data\n", rc);
153 params->input = input;
156 if (args->output_ptr && args->output_size) {
157 output = kzalloc(args->output_size, GFP_KERNEL);
163 params->output = output;
164 params->output_size = args->output_size;
167 rc = hdev->asic_funcs->debug_coresight(hdev, params);
170 "debug coresight operation failed %d\n", rc);
175 if (copy_to_user((void __user *) (uintptr_t) args->output_ptr,
177 args->output_size)) {
179 "copy to user failed in debug ioctl\n");
193 static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data)
195 struct hl_info_args *args = data;
196 struct hl_device *hdev = hpriv->hdev;
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);
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");
211 case HL_INFO_HW_IP_INFO:
212 rc = hw_ip_info(hdev, args);
215 case HL_INFO_HW_EVENTS:
216 rc = hw_events_info(hdev, args);
219 case HL_INFO_DRAM_USAGE:
220 rc = dram_usage_info(hdev, args);
223 case HL_INFO_HW_IDLE:
224 rc = hw_idle(hdev, args);
228 dev_err(hdev->dev, "Invalid request %d\n", args->op);
236 static int hl_debug_ioctl(struct hl_fpriv *hpriv, void *data)
238 struct hl_debug_args *args = data;
239 struct hl_device *hdev = hpriv->hdev;
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");
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:
258 min(args->input_size, hl_debug_struct_size[args->op]);
259 rc = debug_coresight(hdev, args);
262 dev_err(hdev->dev, "Invalid request %d\n", args->op);
270 #define HL_IOCTL_DEF(ioctl, _func) \
271 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
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)
282 #define HL_CORE_IOCTL_COUNT ARRAY_SIZE(hl_ioctls)
284 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
286 struct hl_fpriv *hpriv = filep->private_data;
287 struct hl_device *hdev = hpriv->hdev;
289 const struct hl_ioctl_desc *ioctl = NULL;
290 unsigned int nr = _IOC_NR(cmd);
291 char stack_kdata[128] = {0};
293 unsigned int usize, asize;
296 if (hdev->hard_reset_pending) {
297 dev_crit_ratelimited(hdev->dev,
298 "Device HARD reset pending! Please close FD\n");
302 if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
305 ioctl = &hl_ioctls[nr];
307 hl_size = _IOC_SIZE(ioctl->cmd);
308 usize = asize = _IOC_SIZE(cmd);
314 dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
315 task_pid_nr(current), nr);
319 /* Do not trust userspace, use our own definition */
322 if (unlikely(!func)) {
323 dev_dbg(hdev->dev, "no function\n");
328 if (cmd & (IOC_IN | IOC_OUT)) {
329 if (asize <= sizeof(stack_kdata)) {
332 kdata = kzalloc(asize, GFP_KERNEL);
341 if (copy_from_user(kdata, (void __user *)arg, usize)) {
345 } else if (cmd & IOC_OUT) {
346 memset(kdata, 0, usize);
349 retcode = func(hpriv, kdata);
352 if (copy_to_user((void __user *)arg, kdata, usize))
358 "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
359 task_pid_nr(current), cmd, nr);
361 if (kdata != stack_kdata)