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 int hw_ip_info(struct hl_device *hdev, struct hl_info_args *args)
17 struct hl_info_hw_ip_info hw_ip = {0};
18 u32 size = args->return_size;
19 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
20 struct asic_fixed_properties *prop = &hdev->asic_prop;
21 u64 sram_kmd_size, dram_kmd_size;
23 if ((!size) || (!out))
26 sram_kmd_size = (prop->sram_user_base_address -
27 prop->sram_base_address);
28 dram_kmd_size = (prop->dram_user_base_address -
29 prop->dram_base_address);
31 hw_ip.device_id = hdev->asic_funcs->get_pci_id(hdev);
32 hw_ip.sram_base_address = prop->sram_user_base_address;
33 hw_ip.dram_base_address = prop->dram_user_base_address;
34 hw_ip.tpc_enabled_mask = prop->tpc_enabled_mask;
35 hw_ip.sram_size = prop->sram_size - sram_kmd_size;
36 hw_ip.dram_size = prop->dram_size - dram_kmd_size;
37 if (hw_ip.dram_size > 0)
38 hw_ip.dram_enabled = 1;
39 hw_ip.num_of_events = prop->num_of_events;
40 memcpy(hw_ip.armcp_version,
41 prop->armcp_info.armcp_version, VERSION_MAX_LEN);
42 hw_ip.armcp_cpld_version = __le32_to_cpu(prop->armcp_info.cpld_version);
43 hw_ip.psoc_pci_pll_nr = prop->psoc_pci_pll_nr;
44 hw_ip.psoc_pci_pll_nf = prop->psoc_pci_pll_nf;
45 hw_ip.psoc_pci_pll_od = prop->psoc_pci_pll_od;
46 hw_ip.psoc_pci_pll_div_factor = prop->psoc_pci_pll_div_factor;
48 return copy_to_user(out, &hw_ip,
49 min((size_t)size, sizeof(hw_ip))) ? -EFAULT : 0;
52 static int hw_events_info(struct hl_device *hdev, struct hl_info_args *args)
54 u32 size, max_size = args->return_size;
55 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
58 if ((!max_size) || (!out))
61 arr = hdev->asic_funcs->get_events_stat(hdev, &size);
63 return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0;
66 static int dram_usage_info(struct hl_device *hdev, struct hl_info_args *args)
68 struct hl_info_dram_usage dram_usage = {0};
69 u32 max_size = args->return_size;
70 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
71 struct asic_fixed_properties *prop = &hdev->asic_prop;
74 if ((!max_size) || (!out))
77 dram_kmd_size = (prop->dram_user_base_address -
78 prop->dram_base_address);
79 dram_usage.dram_free_mem = (prop->dram_size - dram_kmd_size) -
80 atomic64_read(&hdev->dram_used_mem);
81 dram_usage.ctx_dram_mem = atomic64_read(&hdev->user_ctx->dram_phys_mem);
83 return copy_to_user(out, &dram_usage,
84 min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
87 static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
89 struct hl_info_hw_idle hw_idle = {0};
90 u32 max_size = args->return_size;
91 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
93 if ((!max_size) || (!out))
96 hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev);
98 return copy_to_user(out, &hw_idle,
99 min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
102 static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data)
104 struct hl_info_args *args = data;
105 struct hl_device *hdev = hpriv->hdev;
108 if (hl_device_disabled_or_in_reset(hdev)) {
110 "Device is disabled or in reset. Can't execute INFO IOCTL\n");
115 case HL_INFO_HW_IP_INFO:
116 rc = hw_ip_info(hdev, args);
119 case HL_INFO_HW_EVENTS:
120 rc = hw_events_info(hdev, args);
123 case HL_INFO_DRAM_USAGE:
124 rc = dram_usage_info(hdev, args);
127 case HL_INFO_HW_IDLE:
128 rc = hw_idle(hdev, args);
132 dev_err(hdev->dev, "Invalid request %d\n", args->op);
140 #define HL_IOCTL_DEF(ioctl, _func) \
141 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
143 static const struct hl_ioctl_desc hl_ioctls[] = {
144 HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl),
145 HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl),
146 HL_IOCTL_DEF(HL_IOCTL_CS, hl_cs_ioctl),
147 HL_IOCTL_DEF(HL_IOCTL_WAIT_CS, hl_cs_wait_ioctl),
148 HL_IOCTL_DEF(HL_IOCTL_MEMORY, hl_mem_ioctl)
151 #define HL_CORE_IOCTL_COUNT ARRAY_SIZE(hl_ioctls)
153 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
155 struct hl_fpriv *hpriv = filep->private_data;
156 struct hl_device *hdev = hpriv->hdev;
158 const struct hl_ioctl_desc *ioctl = NULL;
159 unsigned int nr = _IOC_NR(cmd);
160 char stack_kdata[128] = {0};
162 unsigned int usize, asize;
165 if (hdev->hard_reset_pending) {
166 dev_crit_ratelimited(hdev->dev,
167 "Device HARD reset pending! Please close FD\n");
171 if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
174 ioctl = &hl_ioctls[nr];
176 hl_size = _IOC_SIZE(ioctl->cmd);
177 usize = asize = _IOC_SIZE(cmd);
183 dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
184 task_pid_nr(current), nr);
188 /* Do not trust userspace, use our own definition */
191 if (unlikely(!func)) {
192 dev_dbg(hdev->dev, "no function\n");
197 if (cmd & (IOC_IN | IOC_OUT)) {
198 if (asize <= sizeof(stack_kdata)) {
201 kdata = kzalloc(asize, GFP_KERNEL);
210 if (copy_from_user(kdata, (void __user *)arg, usize)) {
214 } else if (cmd & IOC_OUT) {
215 memset(kdata, 0, usize);
218 retcode = func(hpriv, kdata);
221 if (copy_to_user((void __user *)arg, kdata, usize))
227 "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
228 task_pid_nr(current), cmd, nr);
230 if (kdata != stack_kdata)