Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 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 | |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 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 | |
Dalit Ben Zoor | aa95708 | 2019-03-24 10:15:44 +0200 | [diff] [blame] | 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 | |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 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; |
Oded Gabbay | e16ee41 | 2019-11-16 12:29:50 +0200 | [diff] [blame^] | 63 | if (hw_ip.dram_size > PAGE_SIZE) |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 64 | hw_ip.dram_enabled = 1; |
| 65 | hw_ip.num_of_events = prop->num_of_events; |
Oded Gabbay | 91edbf2 | 2019-10-16 11:53:52 +0300 | [diff] [blame] | 66 | |
| 67 | memcpy(hw_ip.armcp_version, prop->armcp_info.armcp_version, |
| 68 | min(VERSION_MAX_LEN, HL_INFO_VERSION_MAX_LEN)); |
| 69 | |
| 70 | memcpy(hw_ip.card_name, prop->armcp_info.card_name, |
| 71 | min(CARD_NAME_MAX_LEN, HL_INFO_CARD_NAME_MAX_LEN)); |
| 72 | |
Oded Gabbay | fe9a52c | 2019-08-08 17:05:45 +0300 | [diff] [blame] | 73 | hw_ip.armcp_cpld_version = le32_to_cpu(prop->armcp_info.cpld_version); |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 74 | hw_ip.psoc_pci_pll_nr = prop->psoc_pci_pll_nr; |
| 75 | hw_ip.psoc_pci_pll_nf = prop->psoc_pci_pll_nf; |
| 76 | hw_ip.psoc_pci_pll_od = prop->psoc_pci_pll_od; |
| 77 | hw_ip.psoc_pci_pll_div_factor = prop->psoc_pci_pll_div_factor; |
| 78 | |
| 79 | return copy_to_user(out, &hw_ip, |
| 80 | min((size_t)size, sizeof(hw_ip))) ? -EFAULT : 0; |
| 81 | } |
| 82 | |
Oded Gabbay | e973076 | 2019-08-28 21:51:52 +0300 | [diff] [blame] | 83 | static int hw_events_info(struct hl_device *hdev, bool aggregate, |
| 84 | struct hl_info_args *args) |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 85 | { |
| 86 | u32 size, max_size = args->return_size; |
| 87 | void __user *out = (void __user *) (uintptr_t) args->return_pointer; |
| 88 | void *arr; |
| 89 | |
| 90 | if ((!max_size) || (!out)) |
| 91 | return -EINVAL; |
| 92 | |
Oded Gabbay | e973076 | 2019-08-28 21:51:52 +0300 | [diff] [blame] | 93 | arr = hdev->asic_funcs->get_events_stat(hdev, aggregate, &size); |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 94 | |
| 95 | return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0; |
| 96 | } |
| 97 | |
Oded Gabbay | 02e921e | 2019-07-30 11:48:02 +0300 | [diff] [blame] | 98 | static int dram_usage_info(struct hl_fpriv *hpriv, struct hl_info_args *args) |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 99 | { |
Oded Gabbay | 02e921e | 2019-07-30 11:48:02 +0300 | [diff] [blame] | 100 | struct hl_device *hdev = hpriv->hdev; |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 101 | struct hl_info_dram_usage dram_usage = {0}; |
| 102 | u32 max_size = args->return_size; |
| 103 | void __user *out = (void __user *) (uintptr_t) args->return_pointer; |
| 104 | struct asic_fixed_properties *prop = &hdev->asic_prop; |
| 105 | u64 dram_kmd_size; |
| 106 | |
| 107 | if ((!max_size) || (!out)) |
| 108 | return -EINVAL; |
| 109 | |
| 110 | dram_kmd_size = (prop->dram_user_base_address - |
| 111 | prop->dram_base_address); |
| 112 | dram_usage.dram_free_mem = (prop->dram_size - dram_kmd_size) - |
| 113 | atomic64_read(&hdev->dram_used_mem); |
Oded Gabbay | 02e921e | 2019-07-30 11:48:02 +0300 | [diff] [blame] | 114 | if (hpriv->ctx) |
| 115 | dram_usage.ctx_dram_mem = |
| 116 | atomic64_read(&hpriv->ctx->dram_phys_mem); |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 117 | |
| 118 | return copy_to_user(out, &dram_usage, |
| 119 | min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0; |
| 120 | } |
| 121 | |
| 122 | static int hw_idle(struct hl_device *hdev, struct hl_info_args *args) |
| 123 | { |
| 124 | struct hl_info_hw_idle hw_idle = {0}; |
| 125 | u32 max_size = args->return_size; |
| 126 | void __user *out = (void __user *) (uintptr_t) args->return_pointer; |
| 127 | |
| 128 | if ((!max_size) || (!out)) |
| 129 | return -EINVAL; |
| 130 | |
Tomer Tayar | e8960ca | 2019-07-01 13:59:45 +0000 | [diff] [blame] | 131 | hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev, |
| 132 | &hw_idle.busy_engines_mask, NULL); |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 133 | |
| 134 | return copy_to_user(out, &hw_idle, |
| 135 | min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0; |
| 136 | } |
| 137 | |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 138 | static int debug_coresight(struct hl_device *hdev, struct hl_debug_args *args) |
| 139 | { |
| 140 | struct hl_debug_params *params; |
| 141 | void *input = NULL, *output = NULL; |
| 142 | int rc; |
| 143 | |
| 144 | params = kzalloc(sizeof(*params), GFP_KERNEL); |
| 145 | if (!params) |
| 146 | return -ENOMEM; |
| 147 | |
| 148 | params->reg_idx = args->reg_idx; |
| 149 | params->enable = args->enable; |
| 150 | params->op = args->op; |
| 151 | |
| 152 | if (args->input_ptr && args->input_size) { |
Omer Shpigelman | 8d17593 | 2019-08-06 07:37:59 +0000 | [diff] [blame] | 153 | input = kzalloc(hl_debug_struct_size[args->op], GFP_KERNEL); |
| 154 | if (!input) { |
| 155 | rc = -ENOMEM; |
| 156 | goto out; |
| 157 | } |
| 158 | |
| 159 | if (copy_from_user(input, u64_to_user_ptr(args->input_ptr), |
| 160 | args->input_size)) { |
| 161 | rc = -EFAULT; |
| 162 | dev_err(hdev->dev, "failed to copy input debug data\n"); |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 163 | goto out; |
| 164 | } |
| 165 | |
| 166 | params->input = input; |
| 167 | } |
| 168 | |
| 169 | if (args->output_ptr && args->output_size) { |
| 170 | output = kzalloc(args->output_size, GFP_KERNEL); |
| 171 | if (!output) { |
| 172 | rc = -ENOMEM; |
| 173 | goto out; |
| 174 | } |
| 175 | |
| 176 | params->output = output; |
| 177 | params->output_size = args->output_size; |
| 178 | } |
| 179 | |
| 180 | rc = hdev->asic_funcs->debug_coresight(hdev, params); |
| 181 | if (rc) { |
| 182 | dev_err(hdev->dev, |
| 183 | "debug coresight operation failed %d\n", rc); |
| 184 | goto out; |
| 185 | } |
| 186 | |
Oded Gabbay | e16ee41 | 2019-11-16 12:29:50 +0200 | [diff] [blame^] | 187 | if (output && copy_to_user((void __user *) (uintptr_t) args->output_ptr, |
| 188 | output, args->output_size)) { |
| 189 | dev_err(hdev->dev, "copy to user failed in debug ioctl\n"); |
| 190 | rc = -EFAULT; |
| 191 | goto out; |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 192 | } |
| 193 | |
Oded Gabbay | e16ee41 | 2019-11-16 12:29:50 +0200 | [diff] [blame^] | 194 | |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 195 | out: |
| 196 | kfree(params); |
| 197 | kfree(output); |
| 198 | kfree(input); |
| 199 | |
| 200 | return rc; |
| 201 | } |
| 202 | |
Oded Gabbay | 75b3cb2 | 2019-08-28 17:32:04 +0300 | [diff] [blame] | 203 | static int device_utilization(struct hl_device *hdev, struct hl_info_args *args) |
| 204 | { |
| 205 | struct hl_info_device_utilization device_util = {0}; |
| 206 | u32 max_size = args->return_size; |
| 207 | void __user *out = (void __user *) (uintptr_t) args->return_pointer; |
| 208 | |
| 209 | if ((!max_size) || (!out)) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | if ((args->period_ms < 100) || (args->period_ms > 1000) || |
| 213 | (args->period_ms % 100)) { |
| 214 | dev_err(hdev->dev, |
| 215 | "period %u must be between 100 - 1000 and must be divisible by 100\n", |
| 216 | args->period_ms); |
| 217 | return -EINVAL; |
| 218 | } |
| 219 | |
| 220 | device_util.utilization = hl_device_utilization(hdev, args->period_ms); |
| 221 | |
| 222 | return copy_to_user(out, &device_util, |
| 223 | min((size_t) max_size, sizeof(device_util))) ? -EFAULT : 0; |
| 224 | } |
| 225 | |
Oded Gabbay | 62c1e12 | 2019-10-10 15:48:59 +0300 | [diff] [blame] | 226 | static int get_clk_rate(struct hl_device *hdev, struct hl_info_args *args) |
| 227 | { |
| 228 | struct hl_info_clk_rate clk_rate = {0}; |
| 229 | u32 max_size = args->return_size; |
| 230 | void __user *out = (void __user *) (uintptr_t) args->return_pointer; |
| 231 | int rc; |
| 232 | |
| 233 | if ((!max_size) || (!out)) |
| 234 | return -EINVAL; |
| 235 | |
| 236 | rc = hdev->asic_funcs->get_clk_rate(hdev, &clk_rate.cur_clk_rate_mhz, |
| 237 | &clk_rate.max_clk_rate_mhz); |
| 238 | if (rc) |
| 239 | return rc; |
| 240 | |
| 241 | return copy_to_user(out, &clk_rate, |
| 242 | min((size_t) max_size, sizeof(clk_rate))) ? -EFAULT : 0; |
| 243 | } |
| 244 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 245 | static int _hl_info_ioctl(struct hl_fpriv *hpriv, void *data, |
| 246 | struct device *dev) |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 247 | { |
| 248 | struct hl_info_args *args = data; |
| 249 | struct hl_device *hdev = hpriv->hdev; |
| 250 | int rc; |
| 251 | |
Tomer Tayar | 129b6a9 | 2019-08-08 12:30:22 +0000 | [diff] [blame] | 252 | /* |
| 253 | * Information is returned for the following opcodes even if the device |
| 254 | * is disabled or in reset. |
| 255 | */ |
| 256 | switch (args->op) { |
| 257 | case HL_INFO_HW_IP_INFO: |
| 258 | return hw_ip_info(hdev, args); |
| 259 | |
| 260 | case HL_INFO_DEVICE_STATUS: |
Dalit Ben Zoor | aa95708 | 2019-03-24 10:15:44 +0200 | [diff] [blame] | 261 | return device_status_info(hdev, args); |
| 262 | |
Tomer Tayar | 129b6a9 | 2019-08-08 12:30:22 +0000 | [diff] [blame] | 263 | default: |
| 264 | break; |
| 265 | } |
| 266 | |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 267 | if (hl_device_disabled_or_in_reset(hdev)) { |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 268 | dev_warn_ratelimited(dev, |
Oded Gabbay | 3f5398c | 2019-04-06 15:41:35 +0300 | [diff] [blame] | 269 | "Device is %s. Can't execute INFO IOCTL\n", |
| 270 | atomic_read(&hdev->in_reset) ? "in_reset" : "disabled"); |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 271 | return -EBUSY; |
| 272 | } |
| 273 | |
| 274 | switch (args->op) { |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 275 | case HL_INFO_HW_EVENTS: |
Oded Gabbay | e973076 | 2019-08-28 21:51:52 +0300 | [diff] [blame] | 276 | rc = hw_events_info(hdev, false, args); |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 277 | break; |
| 278 | |
| 279 | case HL_INFO_DRAM_USAGE: |
Oded Gabbay | 02e921e | 2019-07-30 11:48:02 +0300 | [diff] [blame] | 280 | rc = dram_usage_info(hpriv, args); |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 281 | break; |
| 282 | |
| 283 | case HL_INFO_HW_IDLE: |
| 284 | rc = hw_idle(hdev, args); |
| 285 | break; |
| 286 | |
Oded Gabbay | 75b3cb2 | 2019-08-28 17:32:04 +0300 | [diff] [blame] | 287 | case HL_INFO_DEVICE_UTILIZATION: |
| 288 | rc = device_utilization(hdev, args); |
| 289 | break; |
| 290 | |
Oded Gabbay | e973076 | 2019-08-28 21:51:52 +0300 | [diff] [blame] | 291 | case HL_INFO_HW_EVENTS_AGGREGATE: |
| 292 | rc = hw_events_info(hdev, true, args); |
| 293 | break; |
| 294 | |
Oded Gabbay | 62c1e12 | 2019-10-10 15:48:59 +0300 | [diff] [blame] | 295 | case HL_INFO_CLK_RATE: |
| 296 | rc = get_clk_rate(hdev, args); |
| 297 | break; |
| 298 | |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 299 | default: |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 300 | dev_err(dev, "Invalid request %d\n", args->op); |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 301 | rc = -ENOTTY; |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | return rc; |
| 306 | } |
| 307 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 308 | static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data) |
| 309 | { |
| 310 | return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev); |
| 311 | } |
| 312 | |
| 313 | static int hl_info_ioctl_control(struct hl_fpriv *hpriv, void *data) |
| 314 | { |
| 315 | return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev_ctrl); |
| 316 | } |
| 317 | |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 318 | static int hl_debug_ioctl(struct hl_fpriv *hpriv, void *data) |
| 319 | { |
| 320 | struct hl_debug_args *args = data; |
| 321 | struct hl_device *hdev = hpriv->hdev; |
| 322 | int rc = 0; |
| 323 | |
| 324 | if (hl_device_disabled_or_in_reset(hdev)) { |
| 325 | dev_warn_ratelimited(hdev->dev, |
| 326 | "Device is %s. Can't execute DEBUG IOCTL\n", |
| 327 | atomic_read(&hdev->in_reset) ? "in_reset" : "disabled"); |
| 328 | return -EBUSY; |
| 329 | } |
| 330 | |
| 331 | switch (args->op) { |
| 332 | case HL_DEBUG_OP_ETR: |
| 333 | case HL_DEBUG_OP_ETF: |
| 334 | case HL_DEBUG_OP_STM: |
| 335 | case HL_DEBUG_OP_FUNNEL: |
| 336 | case HL_DEBUG_OP_BMON: |
| 337 | case HL_DEBUG_OP_SPMU: |
| 338 | case HL_DEBUG_OP_TIMESTAMP: |
Oded Gabbay | 1973497 | 2019-05-04 17:36:06 +0300 | [diff] [blame] | 339 | if (!hdev->in_debug) { |
Oded Gabbay | 29a7aad | 2019-06-06 09:28:45 +0300 | [diff] [blame] | 340 | dev_err_ratelimited(hdev->dev, |
Oded Gabbay | 1973497 | 2019-05-04 17:36:06 +0300 | [diff] [blame] | 341 | "Rejecting debug configuration request because device not in debug mode\n"); |
| 342 | return -EFAULT; |
| 343 | } |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 344 | args->input_size = |
| 345 | min(args->input_size, hl_debug_struct_size[args->op]); |
| 346 | rc = debug_coresight(hdev, args); |
| 347 | break; |
Oded Gabbay | 1973497 | 2019-05-04 17:36:06 +0300 | [diff] [blame] | 348 | case HL_DEBUG_OP_SET_MODE: |
| 349 | rc = hl_device_set_debug_mode(hdev, (bool) args->enable); |
| 350 | break; |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 351 | default: |
| 352 | dev_err(hdev->dev, "Invalid request %d\n", args->op); |
| 353 | rc = -ENOTTY; |
| 354 | break; |
| 355 | } |
| 356 | |
| 357 | return rc; |
| 358 | } |
| 359 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 360 | #define HL_IOCTL_DEF(ioctl, _func) \ |
| 361 | [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func} |
| 362 | |
| 363 | static const struct hl_ioctl_desc hl_ioctls[] = { |
Oded Gabbay | d8dd7b0 | 2019-02-16 00:39:23 +0200 | [diff] [blame] | 364 | HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl), |
Oded Gabbay | eff6f4a | 2019-02-16 00:39:21 +0200 | [diff] [blame] | 365 | HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl), |
| 366 | HL_IOCTL_DEF(HL_IOCTL_CS, hl_cs_ioctl), |
Omer Shpigelman | 0feaf86 | 2019-02-16 00:39:22 +0200 | [diff] [blame] | 367 | HL_IOCTL_DEF(HL_IOCTL_WAIT_CS, hl_cs_wait_ioctl), |
Omer Shpigelman | 315bc05 | 2019-04-01 22:31:22 +0300 | [diff] [blame] | 368 | HL_IOCTL_DEF(HL_IOCTL_MEMORY, hl_mem_ioctl), |
| 369 | HL_IOCTL_DEF(HL_IOCTL_DEBUG, hl_debug_ioctl) |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 370 | }; |
| 371 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 372 | static const struct hl_ioctl_desc hl_ioctls_control[] = { |
| 373 | HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl_control) |
| 374 | }; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 375 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 376 | static long _hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg, |
| 377 | const struct hl_ioctl_desc *ioctl, struct device *dev) |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 378 | { |
| 379 | struct hl_fpriv *hpriv = filep->private_data; |
| 380 | struct hl_device *hdev = hpriv->hdev; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 381 | unsigned int nr = _IOC_NR(cmd); |
| 382 | char stack_kdata[128] = {0}; |
| 383 | char *kdata = NULL; |
| 384 | unsigned int usize, asize; |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 385 | hl_ioctl_t *func; |
| 386 | u32 hl_size; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 387 | int retcode; |
| 388 | |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 389 | if (hdev->hard_reset_pending) { |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 390 | dev_crit_ratelimited(hdev->dev_ctrl, |
Oded Gabbay | f8c8c7d5 | 2019-02-16 00:39:20 +0200 | [diff] [blame] | 391 | "Device HARD reset pending! Please close FD\n"); |
| 392 | return -ENODEV; |
| 393 | } |
| 394 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 395 | /* Do not trust userspace, use our own definition */ |
| 396 | func = ioctl->func; |
| 397 | |
| 398 | if (unlikely(!func)) { |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 399 | dev_dbg(dev, "no function\n"); |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 400 | retcode = -ENOTTY; |
| 401 | goto out_err; |
| 402 | } |
| 403 | |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 404 | hl_size = _IOC_SIZE(ioctl->cmd); |
| 405 | usize = asize = _IOC_SIZE(cmd); |
| 406 | if (hl_size > asize) |
| 407 | asize = hl_size; |
| 408 | |
| 409 | cmd = ioctl->cmd; |
| 410 | |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 411 | if (cmd & (IOC_IN | IOC_OUT)) { |
| 412 | if (asize <= sizeof(stack_kdata)) { |
| 413 | kdata = stack_kdata; |
| 414 | } else { |
| 415 | kdata = kzalloc(asize, GFP_KERNEL); |
| 416 | if (!kdata) { |
| 417 | retcode = -ENOMEM; |
| 418 | goto out_err; |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | if (cmd & IOC_IN) { |
| 424 | if (copy_from_user(kdata, (void __user *)arg, usize)) { |
| 425 | retcode = -EFAULT; |
| 426 | goto out_err; |
| 427 | } |
| 428 | } else if (cmd & IOC_OUT) { |
| 429 | memset(kdata, 0, usize); |
| 430 | } |
| 431 | |
| 432 | retcode = func(hpriv, kdata); |
| 433 | |
Oded Gabbay | e16ee41 | 2019-11-16 12:29:50 +0200 | [diff] [blame^] | 434 | if ((cmd & IOC_OUT) && copy_to_user((void __user *)arg, kdata, usize)) |
| 435 | retcode = -EFAULT; |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 436 | |
| 437 | out_err: |
| 438 | if (retcode) |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 439 | dev_dbg(dev, "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n", |
Oded Gabbay | be5d926 | 2019-02-16 00:39:15 +0200 | [diff] [blame] | 440 | task_pid_nr(current), cmd, nr); |
| 441 | |
| 442 | if (kdata != stack_kdata) |
| 443 | kfree(kdata); |
| 444 | |
| 445 | return retcode; |
| 446 | } |
Oded Gabbay | 4d6a775 | 2019-07-30 09:10:50 +0300 | [diff] [blame] | 447 | |
| 448 | long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg) |
| 449 | { |
| 450 | struct hl_fpriv *hpriv = filep->private_data; |
| 451 | struct hl_device *hdev = hpriv->hdev; |
| 452 | const struct hl_ioctl_desc *ioctl = NULL; |
| 453 | unsigned int nr = _IOC_NR(cmd); |
| 454 | |
| 455 | if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) { |
| 456 | ioctl = &hl_ioctls[nr]; |
| 457 | } else { |
| 458 | dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n", |
| 459 | task_pid_nr(current), nr); |
| 460 | return -ENOTTY; |
| 461 | } |
| 462 | |
| 463 | return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev); |
| 464 | } |
| 465 | |
| 466 | long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg) |
| 467 | { |
| 468 | struct hl_fpriv *hpriv = filep->private_data; |
| 469 | struct hl_device *hdev = hpriv->hdev; |
| 470 | const struct hl_ioctl_desc *ioctl = NULL; |
| 471 | unsigned int nr = _IOC_NR(cmd); |
| 472 | |
| 473 | if (nr == _IOC_NR(HL_IOCTL_INFO)) { |
| 474 | ioctl = &hl_ioctls_control[nr]; |
| 475 | } else { |
| 476 | dev_err(hdev->dev_ctrl, "invalid ioctl: pid=%d, nr=0x%02x\n", |
| 477 | task_pid_nr(current), nr); |
| 478 | return -ENOTTY; |
| 479 | } |
| 480 | |
| 481 | return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev_ctrl); |
| 482 | } |