1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Copyright 2016-2022 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
8 #define pr_fmt(fmt) "habanalabs: " fmt
9
10 #include <uapi/misc/habanalabs.h>
11 #include "habanalabs.h"
12
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/uaccess.h>
16 #include <linux/slab.h>
17 #include <linux/vmalloc.h>
18
19 static u32 hl_debug_struct_size[HL_DEBUG_OP_TIMESTAMP + 1] = {
20 [HL_DEBUG_OP_ETR] = sizeof(struct hl_debug_params_etr),
21 [HL_DEBUG_OP_ETF] = sizeof(struct hl_debug_params_etf),
22 [HL_DEBUG_OP_STM] = sizeof(struct hl_debug_params_stm),
23 [HL_DEBUG_OP_FUNNEL] = 0,
24 [HL_DEBUG_OP_BMON] = sizeof(struct hl_debug_params_bmon),
25 [HL_DEBUG_OP_SPMU] = sizeof(struct hl_debug_params_spmu),
26 [HL_DEBUG_OP_TIMESTAMP] = 0
27
28 };
29
device_status_info(struct hl_device * hdev,struct hl_info_args * args)30 static int device_status_info(struct hl_device *hdev, struct hl_info_args *args)
31 {
32 struct hl_info_device_status dev_stat = {0};
33 u32 size = args->return_size;
34 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
35
36 if ((!size) || (!out))
37 return -EINVAL;
38
39 dev_stat.status = hl_device_status(hdev);
40
41 return copy_to_user(out, &dev_stat,
42 min((size_t)size, sizeof(dev_stat))) ? -EFAULT : 0;
43 }
44
hw_ip_info(struct hl_device * hdev,struct hl_info_args * args)45 static int hw_ip_info(struct hl_device *hdev, struct hl_info_args *args)
46 {
47 struct hl_info_hw_ip_info hw_ip = {0};
48 u32 size = args->return_size;
49 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
50 struct asic_fixed_properties *prop = &hdev->asic_prop;
51 u64 sram_kmd_size, dram_kmd_size, dram_available_size;
52
53 if ((!size) || (!out))
54 return -EINVAL;
55
56 sram_kmd_size = (prop->sram_user_base_address -
57 prop->sram_base_address);
58 dram_kmd_size = (prop->dram_user_base_address -
59 prop->dram_base_address);
60
61 hw_ip.device_id = hdev->asic_funcs->get_pci_id(hdev);
62 hw_ip.sram_base_address = prop->sram_user_base_address;
63 hw_ip.dram_base_address =
64 hdev->mmu_enable && prop->dram_supports_virtual_memory ?
65 prop->dmmu.start_addr : prop->dram_user_base_address;
66 hw_ip.tpc_enabled_mask = prop->tpc_enabled_mask & 0xFF;
67 hw_ip.tpc_enabled_mask_ext = prop->tpc_enabled_mask;
68
69 hw_ip.sram_size = prop->sram_size - sram_kmd_size;
70
71 dram_available_size = prop->dram_size - dram_kmd_size;
72
73 if (hdev->mmu_enable == MMU_EN_ALL)
74 hw_ip.dram_size = DIV_ROUND_DOWN_ULL(dram_available_size,
75 prop->dram_page_size) * prop->dram_page_size;
76 else
77 hw_ip.dram_size = dram_available_size;
78
79 if (hw_ip.dram_size > PAGE_SIZE)
80 hw_ip.dram_enabled = 1;
81
82 hw_ip.dram_page_size = prop->dram_page_size;
83 hw_ip.device_mem_alloc_default_page_size = prop->device_mem_alloc_default_page_size;
84 hw_ip.num_of_events = prop->num_of_events;
85
86 memcpy(hw_ip.cpucp_version, prop->cpucp_info.cpucp_version,
87 min(VERSION_MAX_LEN, HL_INFO_VERSION_MAX_LEN));
88
89 memcpy(hw_ip.card_name, prop->cpucp_info.card_name,
90 min(CARD_NAME_MAX_LEN, HL_INFO_CARD_NAME_MAX_LEN));
91
92 hw_ip.cpld_version = le32_to_cpu(prop->cpucp_info.cpld_version);
93 hw_ip.module_id = le32_to_cpu(prop->cpucp_info.card_location);
94
95 hw_ip.psoc_pci_pll_nr = prop->psoc_pci_pll_nr;
96 hw_ip.psoc_pci_pll_nf = prop->psoc_pci_pll_nf;
97 hw_ip.psoc_pci_pll_od = prop->psoc_pci_pll_od;
98 hw_ip.psoc_pci_pll_div_factor = prop->psoc_pci_pll_div_factor;
99
100 hw_ip.decoder_enabled_mask = prop->decoder_enabled_mask;
101 hw_ip.mme_master_slave_mode = prop->mme_master_slave_mode;
102 hw_ip.first_available_interrupt_id = prop->first_available_user_interrupt;
103 hw_ip.number_of_user_interrupts = prop->user_interrupt_count;
104
105 hw_ip.edma_enabled_mask = prop->edma_enabled_mask;
106 hw_ip.server_type = prop->server_type;
107 hw_ip.security_enabled = prop->fw_security_enabled;
108
109 return copy_to_user(out, &hw_ip,
110 min((size_t) size, sizeof(hw_ip))) ? -EFAULT : 0;
111 }
112
hw_events_info(struct hl_device * hdev,bool aggregate,struct hl_info_args * args)113 static int hw_events_info(struct hl_device *hdev, bool aggregate,
114 struct hl_info_args *args)
115 {
116 u32 size, max_size = args->return_size;
117 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
118 void *arr;
119
120 if ((!max_size) || (!out))
121 return -EINVAL;
122
123 arr = hdev->asic_funcs->get_events_stat(hdev, aggregate, &size);
124
125 return copy_to_user(out, arr, min(max_size, size)) ? -EFAULT : 0;
126 }
127
events_info(struct hl_fpriv * hpriv,struct hl_info_args * args)128 static int events_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
129 {
130 u32 max_size = args->return_size;
131 u64 events_mask;
132 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
133
134 if ((max_size < sizeof(u64)) || (!out))
135 return -EINVAL;
136
137 mutex_lock(&hpriv->notifier_event.lock);
138 events_mask = hpriv->notifier_event.events_mask;
139 hpriv->notifier_event.events_mask = 0;
140 mutex_unlock(&hpriv->notifier_event.lock);
141
142 return copy_to_user(out, &events_mask, sizeof(u64)) ? -EFAULT : 0;
143 }
144
dram_usage_info(struct hl_fpriv * hpriv,struct hl_info_args * args)145 static int dram_usage_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
146 {
147 struct hl_device *hdev = hpriv->hdev;
148 struct hl_info_dram_usage dram_usage = {0};
149 u32 max_size = args->return_size;
150 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
151 struct asic_fixed_properties *prop = &hdev->asic_prop;
152 u64 dram_kmd_size;
153
154 if ((!max_size) || (!out))
155 return -EINVAL;
156
157 dram_kmd_size = (prop->dram_user_base_address -
158 prop->dram_base_address);
159 dram_usage.dram_free_mem = (prop->dram_size - dram_kmd_size) -
160 atomic64_read(&hdev->dram_used_mem);
161 if (hpriv->ctx)
162 dram_usage.ctx_dram_mem =
163 atomic64_read(&hpriv->ctx->dram_phys_mem);
164
165 return copy_to_user(out, &dram_usage,
166 min((size_t) max_size, sizeof(dram_usage))) ? -EFAULT : 0;
167 }
168
hw_idle(struct hl_device * hdev,struct hl_info_args * args)169 static int hw_idle(struct hl_device *hdev, struct hl_info_args *args)
170 {
171 struct hl_info_hw_idle hw_idle = {0};
172 u32 max_size = args->return_size;
173 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
174
175 if ((!max_size) || (!out))
176 return -EINVAL;
177
178 hw_idle.is_idle = hdev->asic_funcs->is_device_idle(hdev,
179 hw_idle.busy_engines_mask_ext,
180 HL_BUSY_ENGINES_MASK_EXT_SIZE, NULL);
181 hw_idle.busy_engines_mask =
182 lower_32_bits(hw_idle.busy_engines_mask_ext[0]);
183
184 return copy_to_user(out, &hw_idle,
185 min((size_t) max_size, sizeof(hw_idle))) ? -EFAULT : 0;
186 }
187
debug_coresight(struct hl_device * hdev,struct hl_ctx * ctx,struct hl_debug_args * args)188 static int debug_coresight(struct hl_device *hdev, struct hl_ctx *ctx, struct hl_debug_args *args)
189 {
190 struct hl_debug_params *params;
191 void *input = NULL, *output = NULL;
192 int rc;
193
194 params = kzalloc(sizeof(*params), GFP_KERNEL);
195 if (!params)
196 return -ENOMEM;
197
198 params->reg_idx = args->reg_idx;
199 params->enable = args->enable;
200 params->op = args->op;
201
202 if (args->input_ptr && args->input_size) {
203 input = kzalloc(hl_debug_struct_size[args->op], GFP_KERNEL);
204 if (!input) {
205 rc = -ENOMEM;
206 goto out;
207 }
208
209 if (copy_from_user(input, u64_to_user_ptr(args->input_ptr),
210 args->input_size)) {
211 rc = -EFAULT;
212 dev_err(hdev->dev, "failed to copy input debug data\n");
213 goto out;
214 }
215
216 params->input = input;
217 }
218
219 if (args->output_ptr && args->output_size) {
220 output = kzalloc(args->output_size, GFP_KERNEL);
221 if (!output) {
222 rc = -ENOMEM;
223 goto out;
224 }
225
226 params->output = output;
227 params->output_size = args->output_size;
228 }
229
230 rc = hdev->asic_funcs->debug_coresight(hdev, ctx, params);
231 if (rc) {
232 dev_err(hdev->dev,
233 "debug coresight operation failed %d\n", rc);
234 goto out;
235 }
236
237 if (output && copy_to_user((void __user *) (uintptr_t) args->output_ptr,
238 output, args->output_size)) {
239 dev_err(hdev->dev, "copy to user failed in debug ioctl\n");
240 rc = -EFAULT;
241 goto out;
242 }
243
244
245 out:
246 kfree(params);
247 kfree(output);
248 kfree(input);
249
250 return rc;
251 }
252
device_utilization(struct hl_device * hdev,struct hl_info_args * args)253 static int device_utilization(struct hl_device *hdev, struct hl_info_args *args)
254 {
255 struct hl_info_device_utilization device_util = {0};
256 u32 max_size = args->return_size;
257 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
258 int rc;
259
260 if ((!max_size) || (!out))
261 return -EINVAL;
262
263 rc = hl_device_utilization(hdev, &device_util.utilization);
264 if (rc)
265 return -EINVAL;
266
267 return copy_to_user(out, &device_util,
268 min((size_t) max_size, sizeof(device_util))) ? -EFAULT : 0;
269 }
270
get_clk_rate(struct hl_device * hdev,struct hl_info_args * args)271 static int get_clk_rate(struct hl_device *hdev, struct hl_info_args *args)
272 {
273 struct hl_info_clk_rate clk_rate = {0};
274 u32 max_size = args->return_size;
275 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
276 int rc;
277
278 if ((!max_size) || (!out))
279 return -EINVAL;
280
281 rc = hl_fw_get_clk_rate(hdev, &clk_rate.cur_clk_rate_mhz, &clk_rate.max_clk_rate_mhz);
282 if (rc)
283 return rc;
284
285 return copy_to_user(out, &clk_rate, min_t(size_t, max_size, sizeof(clk_rate)))
286 ? -EFAULT : 0;
287 }
288
get_reset_count(struct hl_device * hdev,struct hl_info_args * args)289 static int get_reset_count(struct hl_device *hdev, struct hl_info_args *args)
290 {
291 struct hl_info_reset_count reset_count = {0};
292 u32 max_size = args->return_size;
293 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
294
295 if ((!max_size) || (!out))
296 return -EINVAL;
297
298 reset_count.hard_reset_cnt = hdev->reset_info.hard_reset_cnt;
299 reset_count.soft_reset_cnt = hdev->reset_info.compute_reset_cnt;
300
301 return copy_to_user(out, &reset_count,
302 min((size_t) max_size, sizeof(reset_count))) ? -EFAULT : 0;
303 }
304
time_sync_info(struct hl_device * hdev,struct hl_info_args * args)305 static int time_sync_info(struct hl_device *hdev, struct hl_info_args *args)
306 {
307 struct hl_info_time_sync time_sync = {0};
308 u32 max_size = args->return_size;
309 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
310
311 if ((!max_size) || (!out))
312 return -EINVAL;
313
314 time_sync.device_time = hdev->asic_funcs->get_device_time(hdev);
315 time_sync.host_time = ktime_get_raw_ns();
316
317 return copy_to_user(out, &time_sync,
318 min((size_t) max_size, sizeof(time_sync))) ? -EFAULT : 0;
319 }
320
pci_counters_info(struct hl_fpriv * hpriv,struct hl_info_args * args)321 static int pci_counters_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
322 {
323 struct hl_device *hdev = hpriv->hdev;
324 struct hl_info_pci_counters pci_counters = {0};
325 u32 max_size = args->return_size;
326 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
327 int rc;
328
329 if ((!max_size) || (!out))
330 return -EINVAL;
331
332 rc = hl_fw_cpucp_pci_counters_get(hdev, &pci_counters);
333 if (rc)
334 return rc;
335
336 return copy_to_user(out, &pci_counters,
337 min((size_t) max_size, sizeof(pci_counters))) ? -EFAULT : 0;
338 }
339
clk_throttle_info(struct hl_fpriv * hpriv,struct hl_info_args * args)340 static int clk_throttle_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
341 {
342 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
343 struct hl_device *hdev = hpriv->hdev;
344 struct hl_info_clk_throttle clk_throttle = {0};
345 ktime_t end_time, zero_time = ktime_set(0, 0);
346 u32 max_size = args->return_size;
347 int i;
348
349 if ((!max_size) || (!out))
350 return -EINVAL;
351
352 mutex_lock(&hdev->clk_throttling.lock);
353
354 clk_throttle.clk_throttling_reason = hdev->clk_throttling.current_reason;
355
356 for (i = 0 ; i < HL_CLK_THROTTLE_TYPE_MAX ; i++) {
357 if (!(hdev->clk_throttling.aggregated_reason & BIT(i)))
358 continue;
359
360 clk_throttle.clk_throttling_timestamp_us[i] =
361 ktime_to_us(hdev->clk_throttling.timestamp[i].start);
362
363 if (ktime_compare(hdev->clk_throttling.timestamp[i].end, zero_time))
364 end_time = hdev->clk_throttling.timestamp[i].end;
365 else
366 end_time = ktime_get();
367
368 clk_throttle.clk_throttling_duration_ns[i] =
369 ktime_to_ns(ktime_sub(end_time,
370 hdev->clk_throttling.timestamp[i].start));
371
372 }
373 mutex_unlock(&hdev->clk_throttling.lock);
374
375 return copy_to_user(out, &clk_throttle,
376 min((size_t) max_size, sizeof(clk_throttle))) ? -EFAULT : 0;
377 }
378
cs_counters_info(struct hl_fpriv * hpriv,struct hl_info_args * args)379 static int cs_counters_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
380 {
381 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
382 struct hl_info_cs_counters cs_counters = {0};
383 struct hl_device *hdev = hpriv->hdev;
384 struct hl_cs_counters_atomic *cntr;
385 u32 max_size = args->return_size;
386
387 cntr = &hdev->aggregated_cs_counters;
388
389 if ((!max_size) || (!out))
390 return -EINVAL;
391
392 cs_counters.total_out_of_mem_drop_cnt =
393 atomic64_read(&cntr->out_of_mem_drop_cnt);
394 cs_counters.total_parsing_drop_cnt =
395 atomic64_read(&cntr->parsing_drop_cnt);
396 cs_counters.total_queue_full_drop_cnt =
397 atomic64_read(&cntr->queue_full_drop_cnt);
398 cs_counters.total_device_in_reset_drop_cnt =
399 atomic64_read(&cntr->device_in_reset_drop_cnt);
400 cs_counters.total_max_cs_in_flight_drop_cnt =
401 atomic64_read(&cntr->max_cs_in_flight_drop_cnt);
402 cs_counters.total_validation_drop_cnt =
403 atomic64_read(&cntr->validation_drop_cnt);
404
405 if (hpriv->ctx) {
406 cs_counters.ctx_out_of_mem_drop_cnt =
407 atomic64_read(
408 &hpriv->ctx->cs_counters.out_of_mem_drop_cnt);
409 cs_counters.ctx_parsing_drop_cnt =
410 atomic64_read(
411 &hpriv->ctx->cs_counters.parsing_drop_cnt);
412 cs_counters.ctx_queue_full_drop_cnt =
413 atomic64_read(
414 &hpriv->ctx->cs_counters.queue_full_drop_cnt);
415 cs_counters.ctx_device_in_reset_drop_cnt =
416 atomic64_read(
417 &hpriv->ctx->cs_counters.device_in_reset_drop_cnt);
418 cs_counters.ctx_max_cs_in_flight_drop_cnt =
419 atomic64_read(
420 &hpriv->ctx->cs_counters.max_cs_in_flight_drop_cnt);
421 cs_counters.ctx_validation_drop_cnt =
422 atomic64_read(
423 &hpriv->ctx->cs_counters.validation_drop_cnt);
424 }
425
426 return copy_to_user(out, &cs_counters,
427 min((size_t) max_size, sizeof(cs_counters))) ? -EFAULT : 0;
428 }
429
sync_manager_info(struct hl_fpriv * hpriv,struct hl_info_args * args)430 static int sync_manager_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
431 {
432 struct hl_device *hdev = hpriv->hdev;
433 struct asic_fixed_properties *prop = &hdev->asic_prop;
434 struct hl_info_sync_manager sm_info = {0};
435 u32 max_size = args->return_size;
436 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
437
438 if ((!max_size) || (!out))
439 return -EINVAL;
440
441 if (args->dcore_id >= HL_MAX_DCORES)
442 return -EINVAL;
443
444 sm_info.first_available_sync_object =
445 prop->first_available_user_sob[args->dcore_id];
446 sm_info.first_available_monitor =
447 prop->first_available_user_mon[args->dcore_id];
448 sm_info.first_available_cq =
449 prop->first_available_cq[args->dcore_id];
450
451 return copy_to_user(out, &sm_info, min_t(size_t, (size_t) max_size,
452 sizeof(sm_info))) ? -EFAULT : 0;
453 }
454
total_energy_consumption_info(struct hl_fpriv * hpriv,struct hl_info_args * args)455 static int total_energy_consumption_info(struct hl_fpriv *hpriv,
456 struct hl_info_args *args)
457 {
458 struct hl_device *hdev = hpriv->hdev;
459 struct hl_info_energy total_energy = {0};
460 u32 max_size = args->return_size;
461 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
462 int rc;
463
464 if ((!max_size) || (!out))
465 return -EINVAL;
466
467 rc = hl_fw_cpucp_total_energy_get(hdev,
468 &total_energy.total_energy_consumption);
469 if (rc)
470 return rc;
471
472 return copy_to_user(out, &total_energy,
473 min((size_t) max_size, sizeof(total_energy))) ? -EFAULT : 0;
474 }
475
pll_frequency_info(struct hl_fpriv * hpriv,struct hl_info_args * args)476 static int pll_frequency_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
477 {
478 struct hl_device *hdev = hpriv->hdev;
479 struct hl_pll_frequency_info freq_info = { {0} };
480 u32 max_size = args->return_size;
481 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
482 int rc;
483
484 if ((!max_size) || (!out))
485 return -EINVAL;
486
487 rc = hl_fw_cpucp_pll_info_get(hdev, args->pll_index, freq_info.output);
488 if (rc)
489 return rc;
490
491 return copy_to_user(out, &freq_info,
492 min((size_t) max_size, sizeof(freq_info))) ? -EFAULT : 0;
493 }
494
power_info(struct hl_fpriv * hpriv,struct hl_info_args * args)495 static int power_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
496 {
497 struct hl_device *hdev = hpriv->hdev;
498 u32 max_size = args->return_size;
499 struct hl_power_info power_info = {0};
500 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
501 int rc;
502
503 if ((!max_size) || (!out))
504 return -EINVAL;
505
506 rc = hl_fw_cpucp_power_get(hdev, &power_info.power);
507 if (rc)
508 return rc;
509
510 return copy_to_user(out, &power_info,
511 min((size_t) max_size, sizeof(power_info))) ? -EFAULT : 0;
512 }
513
open_stats_info(struct hl_fpriv * hpriv,struct hl_info_args * args)514 static int open_stats_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
515 {
516 struct hl_device *hdev = hpriv->hdev;
517 u32 max_size = args->return_size;
518 struct hl_open_stats_info open_stats_info = {0};
519 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
520
521 if ((!max_size) || (!out))
522 return -EINVAL;
523
524 open_stats_info.last_open_period_ms = jiffies64_to_msecs(
525 hdev->last_open_session_duration_jif);
526 open_stats_info.open_counter = hdev->open_counter;
527 open_stats_info.is_compute_ctx_active = hdev->is_compute_ctx_active;
528 open_stats_info.compute_ctx_in_release = hdev->compute_ctx_in_release;
529
530 return copy_to_user(out, &open_stats_info,
531 min((size_t) max_size, sizeof(open_stats_info))) ? -EFAULT : 0;
532 }
533
dram_pending_rows_info(struct hl_fpriv * hpriv,struct hl_info_args * args)534 static int dram_pending_rows_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
535 {
536 struct hl_device *hdev = hpriv->hdev;
537 u32 max_size = args->return_size;
538 u32 pend_rows_num = 0;
539 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
540 int rc;
541
542 if ((!max_size) || (!out))
543 return -EINVAL;
544
545 rc = hl_fw_dram_pending_row_get(hdev, &pend_rows_num);
546 if (rc)
547 return rc;
548
549 return copy_to_user(out, &pend_rows_num,
550 min_t(size_t, max_size, sizeof(pend_rows_num))) ? -EFAULT : 0;
551 }
552
dram_replaced_rows_info(struct hl_fpriv * hpriv,struct hl_info_args * args)553 static int dram_replaced_rows_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
554 {
555 struct hl_device *hdev = hpriv->hdev;
556 u32 max_size = args->return_size;
557 struct cpucp_hbm_row_info info = {0};
558 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
559 int rc;
560
561 if ((!max_size) || (!out))
562 return -EINVAL;
563
564 rc = hl_fw_dram_replaced_row_get(hdev, &info);
565 if (rc)
566 return rc;
567
568 return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
569 }
570
last_err_open_dev_info(struct hl_fpriv * hpriv,struct hl_info_args * args)571 static int last_err_open_dev_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
572 {
573 struct hl_info_last_err_open_dev_time info = {0};
574 struct hl_device *hdev = hpriv->hdev;
575 u32 max_size = args->return_size;
576 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
577
578 if ((!max_size) || (!out))
579 return -EINVAL;
580
581 info.timestamp = ktime_to_ns(hdev->last_successful_open_ktime);
582
583 return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
584 }
585
cs_timeout_info(struct hl_fpriv * hpriv,struct hl_info_args * args)586 static int cs_timeout_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
587 {
588 struct hl_info_cs_timeout_event info = {0};
589 struct hl_device *hdev = hpriv->hdev;
590 u32 max_size = args->return_size;
591 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
592
593 if ((!max_size) || (!out))
594 return -EINVAL;
595
596 info.seq = hdev->captured_err_info.cs_timeout.seq;
597 info.timestamp = ktime_to_ns(hdev->captured_err_info.cs_timeout.timestamp);
598
599 return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
600 }
601
razwi_info(struct hl_fpriv * hpriv,struct hl_info_args * args)602 static int razwi_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
603 {
604 struct hl_device *hdev = hpriv->hdev;
605 u32 max_size = args->return_size;
606 struct hl_info_razwi_event info = {0};
607 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
608
609 if ((!max_size) || (!out))
610 return -EINVAL;
611
612 info.timestamp = ktime_to_ns(hdev->captured_err_info.razwi.timestamp);
613 info.addr = hdev->captured_err_info.razwi.addr;
614 info.engine_id_1 = hdev->captured_err_info.razwi.engine_id_1;
615 info.engine_id_2 = hdev->captured_err_info.razwi.engine_id_2;
616 info.no_engine_id = hdev->captured_err_info.razwi.non_engine_initiator;
617 info.error_type = hdev->captured_err_info.razwi.type;
618
619 return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
620 }
621
undefined_opcode_info(struct hl_fpriv * hpriv,struct hl_info_args * args)622 static int undefined_opcode_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
623 {
624 struct hl_device *hdev = hpriv->hdev;
625 u32 max_size = args->return_size;
626 struct hl_info_undefined_opcode_event info = {0};
627 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
628
629 if ((!max_size) || (!out))
630 return -EINVAL;
631
632 info.timestamp = ktime_to_ns(hdev->captured_err_info.undef_opcode.timestamp);
633 info.engine_id = hdev->captured_err_info.undef_opcode.engine_id;
634 info.cq_addr = hdev->captured_err_info.undef_opcode.cq_addr;
635 info.cq_size = hdev->captured_err_info.undef_opcode.cq_size;
636 info.stream_id = hdev->captured_err_info.undef_opcode.stream_id;
637 info.cb_addr_streams_len = hdev->captured_err_info.undef_opcode.cb_addr_streams_len;
638 memcpy(info.cb_addr_streams, hdev->captured_err_info.undef_opcode.cb_addr_streams,
639 sizeof(info.cb_addr_streams));
640
641 return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
642 }
643
dev_mem_alloc_page_sizes_info(struct hl_fpriv * hpriv,struct hl_info_args * args)644 static int dev_mem_alloc_page_sizes_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
645 {
646 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
647 struct hl_info_dev_memalloc_page_sizes info = {0};
648 struct hl_device *hdev = hpriv->hdev;
649 u32 max_size = args->return_size;
650
651 if ((!max_size) || (!out))
652 return -EINVAL;
653
654 /*
655 * Future ASICs that will support multiple DRAM page sizes will support only "powers of 2"
656 * pages (unlike some of the ASICs before supporting multiple page sizes).
657 * For this reason for all ASICs that not support multiple page size the function will
658 * return an empty bitmask indicating that multiple page sizes is not supported.
659 */
660 info.page_order_bitmask = hdev->asic_prop.dmmu.supported_pages_mask;
661
662 return copy_to_user(out, &info, min_t(size_t, max_size, sizeof(info))) ? -EFAULT : 0;
663 }
664
sec_attest_info(struct hl_fpriv * hpriv,struct hl_info_args * args)665 static int sec_attest_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
666 {
667 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
668 struct cpucp_sec_attest_info *sec_attest_info;
669 struct hl_info_sec_attest *info;
670 u32 max_size = args->return_size;
671 int rc;
672
673 if ((!max_size) || (!out))
674 return -EINVAL;
675
676 sec_attest_info = kmalloc(sizeof(*sec_attest_info), GFP_KERNEL);
677 if (!sec_attest_info)
678 return -ENOMEM;
679
680 info = kmalloc(sizeof(*info), GFP_KERNEL);
681 if (!info) {
682 rc = -ENOMEM;
683 goto free_sec_attest_info;
684 }
685
686 rc = hl_fw_get_sec_attest_info(hpriv->hdev, sec_attest_info, args->sec_attest_nonce);
687 if (rc)
688 goto free_info;
689
690 info->nonce = le32_to_cpu(sec_attest_info->nonce);
691 info->pcr_quote_len = le16_to_cpu(sec_attest_info->pcr_quote_len);
692 info->pub_data_len = le16_to_cpu(sec_attest_info->pub_data_len);
693 info->certificate_len = le16_to_cpu(sec_attest_info->certificate_len);
694 info->pcr_num_reg = sec_attest_info->pcr_num_reg;
695 info->pcr_reg_len = sec_attest_info->pcr_reg_len;
696 info->quote_sig_len = sec_attest_info->quote_sig_len;
697 memcpy(&info->pcr_data, &sec_attest_info->pcr_data, sizeof(info->pcr_data));
698 memcpy(&info->pcr_quote, &sec_attest_info->pcr_quote, sizeof(info->pcr_quote));
699 memcpy(&info->public_data, &sec_attest_info->public_data, sizeof(info->public_data));
700 memcpy(&info->certificate, &sec_attest_info->certificate, sizeof(info->certificate));
701 memcpy(&info->quote_sig, &sec_attest_info->quote_sig, sizeof(info->quote_sig));
702
703 rc = copy_to_user(out, info,
704 min_t(size_t, max_size, sizeof(*info))) ? -EFAULT : 0;
705
706 free_info:
707 kfree(info);
708 free_sec_attest_info:
709 kfree(sec_attest_info);
710
711 return rc;
712 }
713
eventfd_register(struct hl_fpriv * hpriv,struct hl_info_args * args)714 static int eventfd_register(struct hl_fpriv *hpriv, struct hl_info_args *args)
715 {
716 int rc;
717
718 /* check if there is already a registered on that process */
719 mutex_lock(&hpriv->notifier_event.lock);
720 if (hpriv->notifier_event.eventfd) {
721 mutex_unlock(&hpriv->notifier_event.lock);
722 return -EINVAL;
723 }
724
725 hpriv->notifier_event.eventfd = eventfd_ctx_fdget(args->eventfd);
726 if (IS_ERR(hpriv->notifier_event.eventfd)) {
727 rc = PTR_ERR(hpriv->notifier_event.eventfd);
728 hpriv->notifier_event.eventfd = NULL;
729 mutex_unlock(&hpriv->notifier_event.lock);
730 return rc;
731 }
732
733 mutex_unlock(&hpriv->notifier_event.lock);
734 return 0;
735 }
736
eventfd_unregister(struct hl_fpriv * hpriv,struct hl_info_args * args)737 static int eventfd_unregister(struct hl_fpriv *hpriv, struct hl_info_args *args)
738 {
739 mutex_lock(&hpriv->notifier_event.lock);
740 if (!hpriv->notifier_event.eventfd) {
741 mutex_unlock(&hpriv->notifier_event.lock);
742 return -EINVAL;
743 }
744
745 eventfd_ctx_put(hpriv->notifier_event.eventfd);
746 hpriv->notifier_event.eventfd = NULL;
747 mutex_unlock(&hpriv->notifier_event.lock);
748 return 0;
749 }
750
engine_status_info(struct hl_fpriv * hpriv,struct hl_info_args * args)751 static int engine_status_info(struct hl_fpriv *hpriv, struct hl_info_args *args)
752 {
753 void __user *out = (void __user *) (uintptr_t) args->return_pointer;
754 u32 status_buf_size = args->return_size;
755 struct hl_device *hdev = hpriv->hdev;
756 struct engines_data eng_data;
757 int rc;
758
759 if ((status_buf_size < SZ_1K) || (status_buf_size > HL_ENGINES_DATA_MAX_SIZE) || (!out))
760 return -EINVAL;
761
762 eng_data.actual_size = 0;
763 eng_data.allocated_buf_size = status_buf_size;
764 eng_data.buf = vmalloc(status_buf_size);
765 if (!eng_data.buf)
766 return -ENOMEM;
767
768 hdev->asic_funcs->is_device_idle(hdev, NULL, 0, &eng_data);
769
770 if (eng_data.actual_size > eng_data.allocated_buf_size) {
771 dev_err(hdev->dev,
772 "Engines data size (%d Bytes) is bigger than allocated size (%u Bytes)\n",
773 eng_data.actual_size, status_buf_size);
774 vfree(eng_data.buf);
775 return -ENOMEM;
776 }
777
778 args->user_buffer_actual_size = eng_data.actual_size;
779 rc = copy_to_user(out, eng_data.buf, min_t(size_t, status_buf_size, eng_data.actual_size)) ?
780 -EFAULT : 0;
781
782 vfree(eng_data.buf);
783
784 return rc;
785 }
786
_hl_info_ioctl(struct hl_fpriv * hpriv,void * data,struct device * dev)787 static int _hl_info_ioctl(struct hl_fpriv *hpriv, void *data,
788 struct device *dev)
789 {
790 enum hl_device_status status;
791 struct hl_info_args *args = data;
792 struct hl_device *hdev = hpriv->hdev;
793
794 int rc;
795
796 /*
797 * Information is returned for the following opcodes even if the device
798 * is disabled or in reset.
799 */
800 switch (args->op) {
801 case HL_INFO_HW_IP_INFO:
802 return hw_ip_info(hdev, args);
803
804 case HL_INFO_DEVICE_STATUS:
805 return device_status_info(hdev, args);
806
807 case HL_INFO_RESET_COUNT:
808 return get_reset_count(hdev, args);
809
810 case HL_INFO_HW_EVENTS:
811 return hw_events_info(hdev, false, args);
812
813 case HL_INFO_HW_EVENTS_AGGREGATE:
814 return hw_events_info(hdev, true, args);
815
816 case HL_INFO_CS_COUNTERS:
817 return cs_counters_info(hpriv, args);
818
819 case HL_INFO_CLK_THROTTLE_REASON:
820 return clk_throttle_info(hpriv, args);
821
822 case HL_INFO_SYNC_MANAGER:
823 return sync_manager_info(hpriv, args);
824
825 case HL_INFO_OPEN_STATS:
826 return open_stats_info(hpriv, args);
827
828 case HL_INFO_LAST_ERR_OPEN_DEV_TIME:
829 return last_err_open_dev_info(hpriv, args);
830
831 case HL_INFO_CS_TIMEOUT_EVENT:
832 return cs_timeout_info(hpriv, args);
833
834 case HL_INFO_RAZWI_EVENT:
835 return razwi_info(hpriv, args);
836
837 case HL_INFO_UNDEFINED_OPCODE_EVENT:
838 return undefined_opcode_info(hpriv, args);
839
840 case HL_INFO_DEV_MEM_ALLOC_PAGE_SIZES:
841 return dev_mem_alloc_page_sizes_info(hpriv, args);
842
843 case HL_INFO_GET_EVENTS:
844 return events_info(hpriv, args);
845
846 default:
847 break;
848 }
849
850 if (!hl_device_operational(hdev, &status)) {
851 dev_warn_ratelimited(dev,
852 "Device is %s. Can't execute INFO IOCTL\n",
853 hdev->status[status]);
854 return -EBUSY;
855 }
856
857 switch (args->op) {
858 case HL_INFO_DRAM_USAGE:
859 rc = dram_usage_info(hpriv, args);
860 break;
861
862 case HL_INFO_HW_IDLE:
863 rc = hw_idle(hdev, args);
864 break;
865
866 case HL_INFO_DEVICE_UTILIZATION:
867 rc = device_utilization(hdev, args);
868 break;
869
870 case HL_INFO_CLK_RATE:
871 rc = get_clk_rate(hdev, args);
872 break;
873
874 case HL_INFO_TIME_SYNC:
875 return time_sync_info(hdev, args);
876
877 case HL_INFO_PCI_COUNTERS:
878 return pci_counters_info(hpriv, args);
879
880 case HL_INFO_TOTAL_ENERGY:
881 return total_energy_consumption_info(hpriv, args);
882
883 case HL_INFO_PLL_FREQUENCY:
884 return pll_frequency_info(hpriv, args);
885
886 case HL_INFO_POWER:
887 return power_info(hpriv, args);
888
889
890 case HL_INFO_DRAM_REPLACED_ROWS:
891 return dram_replaced_rows_info(hpriv, args);
892
893 case HL_INFO_DRAM_PENDING_ROWS:
894 return dram_pending_rows_info(hpriv, args);
895
896 case HL_INFO_SECURED_ATTESTATION:
897 return sec_attest_info(hpriv, args);
898
899 case HL_INFO_REGISTER_EVENTFD:
900 return eventfd_register(hpriv, args);
901
902 case HL_INFO_UNREGISTER_EVENTFD:
903 return eventfd_unregister(hpriv, args);
904
905 case HL_INFO_ENGINE_STATUS:
906 return engine_status_info(hpriv, args);
907
908 default:
909 dev_err(dev, "Invalid request %d\n", args->op);
910 rc = -EINVAL;
911 break;
912 }
913
914 return rc;
915 }
916
hl_info_ioctl(struct hl_fpriv * hpriv,void * data)917 static int hl_info_ioctl(struct hl_fpriv *hpriv, void *data)
918 {
919 return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev);
920 }
921
hl_info_ioctl_control(struct hl_fpriv * hpriv,void * data)922 static int hl_info_ioctl_control(struct hl_fpriv *hpriv, void *data)
923 {
924 return _hl_info_ioctl(hpriv, data, hpriv->hdev->dev_ctrl);
925 }
926
hl_debug_ioctl(struct hl_fpriv * hpriv,void * data)927 static int hl_debug_ioctl(struct hl_fpriv *hpriv, void *data)
928 {
929 struct hl_debug_args *args = data;
930 struct hl_device *hdev = hpriv->hdev;
931 enum hl_device_status status;
932
933 int rc = 0;
934
935 if (!hl_device_operational(hdev, &status)) {
936 dev_warn_ratelimited(hdev->dev,
937 "Device is %s. Can't execute DEBUG IOCTL\n",
938 hdev->status[status]);
939 return -EBUSY;
940 }
941
942 switch (args->op) {
943 case HL_DEBUG_OP_ETR:
944 case HL_DEBUG_OP_ETF:
945 case HL_DEBUG_OP_STM:
946 case HL_DEBUG_OP_FUNNEL:
947 case HL_DEBUG_OP_BMON:
948 case HL_DEBUG_OP_SPMU:
949 case HL_DEBUG_OP_TIMESTAMP:
950 if (!hdev->in_debug) {
951 dev_err_ratelimited(hdev->dev,
952 "Rejecting debug configuration request because device not in debug mode\n");
953 return -EFAULT;
954 }
955 args->input_size = min(args->input_size, hl_debug_struct_size[args->op]);
956 rc = debug_coresight(hdev, hpriv->ctx, args);
957 break;
958
959 case HL_DEBUG_OP_SET_MODE:
960 rc = hl_device_set_debug_mode(hdev, hpriv->ctx, (bool) args->enable);
961 break;
962
963 default:
964 dev_err(hdev->dev, "Invalid request %d\n", args->op);
965 rc = -EINVAL;
966 break;
967 }
968
969 return rc;
970 }
971
972 #define HL_IOCTL_DEF(ioctl, _func) \
973 [_IOC_NR(ioctl)] = {.cmd = ioctl, .func = _func}
974
975 static const struct hl_ioctl_desc hl_ioctls[] = {
976 HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl),
977 HL_IOCTL_DEF(HL_IOCTL_CB, hl_cb_ioctl),
978 HL_IOCTL_DEF(HL_IOCTL_CS, hl_cs_ioctl),
979 HL_IOCTL_DEF(HL_IOCTL_WAIT_CS, hl_wait_ioctl),
980 HL_IOCTL_DEF(HL_IOCTL_MEMORY, hl_mem_ioctl),
981 HL_IOCTL_DEF(HL_IOCTL_DEBUG, hl_debug_ioctl)
982 };
983
984 static const struct hl_ioctl_desc hl_ioctls_control[] = {
985 HL_IOCTL_DEF(HL_IOCTL_INFO, hl_info_ioctl_control)
986 };
987
_hl_ioctl(struct file * filep,unsigned int cmd,unsigned long arg,const struct hl_ioctl_desc * ioctl,struct device * dev)988 static long _hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg,
989 const struct hl_ioctl_desc *ioctl, struct device *dev)
990 {
991 struct hl_fpriv *hpriv = filep->private_data;
992 unsigned int nr = _IOC_NR(cmd);
993 char stack_kdata[128] = {0};
994 char *kdata = NULL;
995 unsigned int usize, asize;
996 hl_ioctl_t *func;
997 u32 hl_size;
998 int retcode;
999
1000 /* Do not trust userspace, use our own definition */
1001 func = ioctl->func;
1002
1003 if (unlikely(!func)) {
1004 dev_dbg(dev, "no function\n");
1005 retcode = -ENOTTY;
1006 goto out_err;
1007 }
1008
1009 hl_size = _IOC_SIZE(ioctl->cmd);
1010 usize = asize = _IOC_SIZE(cmd);
1011 if (hl_size > asize)
1012 asize = hl_size;
1013
1014 cmd = ioctl->cmd;
1015
1016 if (cmd & (IOC_IN | IOC_OUT)) {
1017 if (asize <= sizeof(stack_kdata)) {
1018 kdata = stack_kdata;
1019 } else {
1020 kdata = kzalloc(asize, GFP_KERNEL);
1021 if (!kdata) {
1022 retcode = -ENOMEM;
1023 goto out_err;
1024 }
1025 }
1026 }
1027
1028 if (cmd & IOC_IN) {
1029 if (copy_from_user(kdata, (void __user *)arg, usize)) {
1030 retcode = -EFAULT;
1031 goto out_err;
1032 }
1033 } else if (cmd & IOC_OUT) {
1034 memset(kdata, 0, usize);
1035 }
1036
1037 retcode = func(hpriv, kdata);
1038
1039 if ((cmd & IOC_OUT) && copy_to_user((void __user *)arg, kdata, usize))
1040 retcode = -EFAULT;
1041
1042 out_err:
1043 if (retcode)
1044 dev_dbg(dev, "error in ioctl: pid=%d, cmd=0x%02x, nr=0x%02x\n",
1045 task_pid_nr(current), cmd, nr);
1046
1047 if (kdata != stack_kdata)
1048 kfree(kdata);
1049
1050 return retcode;
1051 }
1052
hl_ioctl(struct file * filep,unsigned int cmd,unsigned long arg)1053 long hl_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1054 {
1055 struct hl_fpriv *hpriv = filep->private_data;
1056 struct hl_device *hdev = hpriv->hdev;
1057 const struct hl_ioctl_desc *ioctl = NULL;
1058 unsigned int nr = _IOC_NR(cmd);
1059
1060 if (!hdev) {
1061 pr_err_ratelimited("Sending ioctl after device was removed! Please close FD\n");
1062 return -ENODEV;
1063 }
1064
1065 if ((nr >= HL_COMMAND_START) && (nr < HL_COMMAND_END)) {
1066 ioctl = &hl_ioctls[nr];
1067 } else {
1068 dev_err(hdev->dev, "invalid ioctl: pid=%d, nr=0x%02x\n",
1069 task_pid_nr(current), nr);
1070 return -ENOTTY;
1071 }
1072
1073 return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev);
1074 }
1075
hl_ioctl_control(struct file * filep,unsigned int cmd,unsigned long arg)1076 long hl_ioctl_control(struct file *filep, unsigned int cmd, unsigned long arg)
1077 {
1078 struct hl_fpriv *hpriv = filep->private_data;
1079 struct hl_device *hdev = hpriv->hdev;
1080 const struct hl_ioctl_desc *ioctl = NULL;
1081 unsigned int nr = _IOC_NR(cmd);
1082
1083 if (!hdev) {
1084 pr_err_ratelimited("Sending ioctl after device was removed! Please close FD\n");
1085 return -ENODEV;
1086 }
1087
1088 if (nr == _IOC_NR(HL_IOCTL_INFO)) {
1089 ioctl = &hl_ioctls_control[nr];
1090 } else {
1091 dev_err(hdev->dev_ctrl, "invalid ioctl: pid=%d, nr=0x%02x\n",
1092 task_pid_nr(current), nr);
1093 return -ENOTTY;
1094 }
1095
1096 return _hl_ioctl(filep, cmd, arg, ioctl, hdev->dev_ctrl);
1097 }
1098