habanalabs: fix little-endian<->cpu conversion warnings
[linux-2.6-microblaze.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 int hw_ip_info(struct hl_device *hdev, struct hl_info_args *args)
16 {
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;
22
23         if ((!size) || (!out))
24                 return -EINVAL;
25
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);
30
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;
47
48         return copy_to_user(out, &hw_ip,
49                 min((size_t)size, sizeof(hw_ip))) ? -EFAULT : 0;
50 }
51
52 static int hw_events_info(struct hl_device *hdev, struct hl_info_args *args)
53 {
54         u32 size, max_size = args->return_size;
55         void __user *out = (void __user *) (uintptr_t) args->return_pointer;
56         void *arr;
57
58         if ((!max_size) || (!out))
59                 return -EINVAL;
60
61         arr = hdev->asic_funcs->get_events_stat(hdev, &size);
62
63         return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0;
64 }
65
66 static int dram_usage_info(struct hl_device *hdev, struct hl_info_args *args)
67 {
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;
72         u64 dram_kmd_size;
73
74         if ((!max_size) || (!out))
75                 return -EINVAL;
76
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);
82
83         return copy_to_user(out, &dram_usage,
84                 min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
85 }
86
87 static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
88 {
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;
92
93         if ((!max_size) || (!out))
94                 return -EINVAL;
95
96         hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev);
97
98         return copy_to_user(out, &hw_idle,
99                 min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
100 }
101
102 static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data)
103 {
104         struct hl_info_args *args = data;
105         struct hl_device *hdev = hpriv->hdev;
106         int rc;
107
108         if (hl_device_disabled_or_in_reset(hdev)) {
109                 dev_err(hdev->dev,
110                         "Device is disabled or in reset. Can't execute INFO IOCTL\n");
111                 return -EBUSY;
112         }
113
114         switch (args->op) {
115         case HL_INFO_HW_IP_INFO:
116                 rc = hw_ip_info(hdev, args);
117                 break;
118
119         case HL_INFO_HW_EVENTS:
120                 rc = hw_events_info(hdev, args);
121                 break;
122
123         case HL_INFO_DRAM_USAGE:
124                 rc = dram_usage_info(hdev, args);
125                 break;
126
127         case HL_INFO_HW_IDLE:
128                 rc = hw_idle(hdev, args);
129                 break;
130
131         default:
132                 dev_err(hdev->dev, "Invalid request %d\n", args->op);
133                 rc = -ENOTTY;
134                 break;
135         }
136
137         return rc;
138 }
139
140 #define HL_IOCTL_DEF(ioctl, _func) \
141         [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
142
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)
149 };
150
151 #define HL_CORE_IOCTL_COUNT     ARRAY_SIZE(hl_ioctls)
152
153 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
154 {
155         struct hl_fpriv *hpriv = filep->private_data;
156         struct hl_device *hdev = hpriv->hdev;
157         hl_ioctl_t *func;
158         const struct hl_ioctl_desc *ioctl = NULL;
159         unsigned int nr = _IOC_NR(cmd);
160         char stack_kdata[128] = {0};
161         char *kdata = NULL;
162         unsigned int usize, asize;
163         int retcode;
164
165         if (hdev->hard_reset_pending) {
166                 dev_crit_ratelimited(hdev->dev,
167                         "Device HARD reset pending! Please close FD\n");
168                 return -ENODEV;
169         }
170
171         if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
172                 u32 hl_size;
173
174                 ioctl = &hl_ioctls[nr];
175
176                 hl_size = _IOC_SIZE(ioctl->cmd);
177                 usize = asize = _IOC_SIZE(cmd);
178                 if (hl_size > asize)
179                         asize = hl_size;
180
181                 cmd = ioctl->cmd;
182         } else {
183                 dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
184                           task_pid_nr(current), nr);
185                 return -ENOTTY;
186         }
187
188         /* Do not trust userspace, use our own definition */
189         func = ioctl->func;
190
191         if (unlikely(!func)) {
192                 dev_dbg(hdev->dev, "no function\n");
193                 retcode = -ENOTTY;
194                 goto out_err;
195         }
196
197         if (cmd & (IOC_IN | IOC_OUT)) {
198                 if (asize <= sizeof(stack_kdata)) {
199                         kdata = stack_kdata;
200                 } else {
201                         kdata = kzalloc(asize, GFP_KERNEL);
202                         if (!kdata) {
203                                 retcode = -ENOMEM;
204                                 goto out_err;
205                         }
206                 }
207         }
208
209         if (cmd & IOC_IN) {
210                 if (copy_from_user(kdata, (void __user *)arg, usize)) {
211                         retcode = -EFAULT;
212                         goto out_err;
213                 }
214         } else if (cmd & IOC_OUT) {
215                 memset(kdata, 0, usize);
216         }
217
218         retcode = func(hpriv, kdata);
219
220         if (cmd & IOC_OUT)
221                 if (copy_to_user((void __user *)arg, kdata, usize))
222                         retcode = -EFAULT;
223
224 out_err:
225         if (retcode)
226                 dev_dbg(hdev->dev,
227                         "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
228                           task_pid_nr(current), cmd, nr);
229
230         if (kdata != stack_kdata)
231                 kfree(kdata);
232
233         return retcode;
234 }