1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4 * Copyright 2016-2019 HabanaLabs, Ltd.
5 * All Rights Reserved.
6 */
7
8 #include "habanalabs.h"
9 #include "../include/hw_ip/mmu/mmu_general.h"
10
11 #include <linux/pci.h>
12 #include <linux/uaccess.h>
13 #include <linux/vmalloc.h>
14
15 #define MMU_ADDR_BUF_SIZE 40
16 #define MMU_ASID_BUF_SIZE 10
17 #define MMU_KBUF_SIZE (MMU_ADDR_BUF_SIZE + MMU_ASID_BUF_SIZE)
18
19 static struct dentry *hl_debug_root;
20
hl_debugfs_i2c_read(struct hl_device * hdev,u8 i2c_bus,u8 i2c_addr,u8 i2c_reg,long * val)21 static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
22 u8 i2c_reg, long *val)
23 {
24 struct cpucp_packet pkt;
25 u64 result;
26 int rc;
27
28 if (!hl_device_operational(hdev, NULL))
29 return -EBUSY;
30
31 memset(&pkt, 0, sizeof(pkt));
32
33 pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_RD <<
34 CPUCP_PKT_CTL_OPCODE_SHIFT);
35 pkt.i2c_bus = i2c_bus;
36 pkt.i2c_addr = i2c_addr;
37 pkt.i2c_reg = i2c_reg;
38
39 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
40 0, &result);
41
42 *val = (long) result;
43
44 if (rc)
45 dev_err(hdev->dev, "Failed to read from I2C, error %d\n", rc);
46
47 return rc;
48 }
49
hl_debugfs_i2c_write(struct hl_device * hdev,u8 i2c_bus,u8 i2c_addr,u8 i2c_reg,u32 val)50 static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
51 u8 i2c_reg, u32 val)
52 {
53 struct cpucp_packet pkt;
54 int rc;
55
56 if (!hl_device_operational(hdev, NULL))
57 return -EBUSY;
58
59 memset(&pkt, 0, sizeof(pkt));
60
61 pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_WR <<
62 CPUCP_PKT_CTL_OPCODE_SHIFT);
63 pkt.i2c_bus = i2c_bus;
64 pkt.i2c_addr = i2c_addr;
65 pkt.i2c_reg = i2c_reg;
66 pkt.value = cpu_to_le64(val);
67
68 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
69 0, NULL);
70
71 if (rc)
72 dev_err(hdev->dev, "Failed to write to I2C, error %d\n", rc);
73
74 return rc;
75 }
76
hl_debugfs_led_set(struct hl_device * hdev,u8 led,u8 state)77 static void hl_debugfs_led_set(struct hl_device *hdev, u8 led, u8 state)
78 {
79 struct cpucp_packet pkt;
80 int rc;
81
82 if (!hl_device_operational(hdev, NULL))
83 return;
84
85 memset(&pkt, 0, sizeof(pkt));
86
87 pkt.ctl = cpu_to_le32(CPUCP_PACKET_LED_SET <<
88 CPUCP_PKT_CTL_OPCODE_SHIFT);
89 pkt.led_index = cpu_to_le32(led);
90 pkt.value = cpu_to_le64(state);
91
92 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
93 0, NULL);
94
95 if (rc)
96 dev_err(hdev->dev, "Failed to set LED %d, error %d\n", led, rc);
97 }
98
command_buffers_show(struct seq_file * s,void * data)99 static int command_buffers_show(struct seq_file *s, void *data)
100 {
101 struct hl_debugfs_entry *entry = s->private;
102 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
103 struct hl_cb *cb;
104 bool first = true;
105
106 spin_lock(&dev_entry->cb_spinlock);
107
108 list_for_each_entry(cb, &dev_entry->cb_list, debugfs_list) {
109 if (first) {
110 first = false;
111 seq_puts(s, "\n");
112 seq_puts(s, " CB ID CTX ID CB size CB RefCnt mmap? CS counter\n");
113 seq_puts(s, "---------------------------------------------------------------\n");
114 }
115 seq_printf(s,
116 " %03llu %d 0x%08x %d %d %d\n",
117 cb->id, cb->ctx->asid, cb->size,
118 kref_read(&cb->refcount),
119 cb->mmap, atomic_read(&cb->cs_cnt));
120 }
121
122 spin_unlock(&dev_entry->cb_spinlock);
123
124 if (!first)
125 seq_puts(s, "\n");
126
127 return 0;
128 }
129
command_submission_show(struct seq_file * s,void * data)130 static int command_submission_show(struct seq_file *s, void *data)
131 {
132 struct hl_debugfs_entry *entry = s->private;
133 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
134 struct hl_cs *cs;
135 bool first = true;
136
137 spin_lock(&dev_entry->cs_spinlock);
138
139 list_for_each_entry(cs, &dev_entry->cs_list, debugfs_list) {
140 if (first) {
141 first = false;
142 seq_puts(s, "\n");
143 seq_puts(s, " CS ID CTX ASID CS RefCnt Submitted Completed\n");
144 seq_puts(s, "------------------------------------------------------\n");
145 }
146 seq_printf(s,
147 " %llu %d %d %d %d\n",
148 cs->sequence, cs->ctx->asid,
149 kref_read(&cs->refcount),
150 cs->submitted, cs->completed);
151 }
152
153 spin_unlock(&dev_entry->cs_spinlock);
154
155 if (!first)
156 seq_puts(s, "\n");
157
158 return 0;
159 }
160
command_submission_jobs_show(struct seq_file * s,void * data)161 static int command_submission_jobs_show(struct seq_file *s, void *data)
162 {
163 struct hl_debugfs_entry *entry = s->private;
164 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
165 struct hl_cs_job *job;
166 bool first = true;
167
168 spin_lock(&dev_entry->cs_job_spinlock);
169
170 list_for_each_entry(job, &dev_entry->cs_job_list, debugfs_list) {
171 if (first) {
172 first = false;
173 seq_puts(s, "\n");
174 seq_puts(s, " JOB ID CS ID CTX ASID JOB RefCnt H/W Queue\n");
175 seq_puts(s, "----------------------------------------------------\n");
176 }
177 if (job->cs)
178 seq_printf(s,
179 " %02d %llu %d %d %d\n",
180 job->id, job->cs->sequence, job->cs->ctx->asid,
181 kref_read(&job->refcount), job->hw_queue_id);
182 else
183 seq_printf(s,
184 " %02d 0 %d %d %d\n",
185 job->id, HL_KERNEL_ASID_ID,
186 kref_read(&job->refcount), job->hw_queue_id);
187 }
188
189 spin_unlock(&dev_entry->cs_job_spinlock);
190
191 if (!first)
192 seq_puts(s, "\n");
193
194 return 0;
195 }
196
userptr_show(struct seq_file * s,void * data)197 static int userptr_show(struct seq_file *s, void *data)
198 {
199 struct hl_debugfs_entry *entry = s->private;
200 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
201 struct hl_userptr *userptr;
202 char dma_dir[4][30] = {"DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
203 "DMA_FROM_DEVICE", "DMA_NONE"};
204 bool first = true;
205
206 spin_lock(&dev_entry->userptr_spinlock);
207
208 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
209 if (first) {
210 first = false;
211 seq_puts(s, "\n");
212 seq_puts(s, " pid user virtual address size dma dir\n");
213 seq_puts(s, "----------------------------------------------------------\n");
214 }
215 seq_printf(s, " %-7d 0x%-14llx %-10llu %-30s\n",
216 userptr->pid, userptr->addr, userptr->size,
217 dma_dir[userptr->dir]);
218 }
219
220 spin_unlock(&dev_entry->userptr_spinlock);
221
222 if (!first)
223 seq_puts(s, "\n");
224
225 return 0;
226 }
227
vm_show(struct seq_file * s,void * data)228 static int vm_show(struct seq_file *s, void *data)
229 {
230 struct hl_debugfs_entry *entry = s->private;
231 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
232 struct hl_vm_hw_block_list_node *lnode;
233 struct hl_ctx *ctx;
234 struct hl_vm *vm;
235 struct hl_vm_hash_node *hnode;
236 struct hl_userptr *userptr;
237 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
238 enum vm_type *vm_type;
239 bool once = true;
240 u64 j;
241 int i;
242
243 if (!dev_entry->hdev->mmu_enable)
244 return 0;
245
246 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
247
248 list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
249 once = false;
250 seq_puts(s, "\n\n----------------------------------------------------");
251 seq_puts(s, "\n----------------------------------------------------\n\n");
252 seq_printf(s, "ctx asid: %u\n", ctx->asid);
253
254 seq_puts(s, "\nmappings:\n\n");
255 seq_puts(s, " virtual address size handle\n");
256 seq_puts(s, "----------------------------------------------------\n");
257 mutex_lock(&ctx->mem_hash_lock);
258 hash_for_each(ctx->mem_hash, i, hnode, node) {
259 vm_type = hnode->ptr;
260
261 if (*vm_type == VM_TYPE_USERPTR) {
262 userptr = hnode->ptr;
263 seq_printf(s,
264 " 0x%-14llx %-10llu\n",
265 hnode->vaddr, userptr->size);
266 } else {
267 phys_pg_pack = hnode->ptr;
268 seq_printf(s,
269 " 0x%-14llx %-10llu %-4u\n",
270 hnode->vaddr, phys_pg_pack->total_size,
271 phys_pg_pack->handle);
272 }
273 }
274 mutex_unlock(&ctx->mem_hash_lock);
275
276 if (ctx->asid != HL_KERNEL_ASID_ID &&
277 !list_empty(&ctx->hw_block_mem_list)) {
278 seq_puts(s, "\nhw_block mappings:\n\n");
279 seq_puts(s, " virtual address size HW block id\n");
280 seq_puts(s, "-------------------------------------------\n");
281 mutex_lock(&ctx->hw_block_list_lock);
282 list_for_each_entry(lnode, &ctx->hw_block_mem_list,
283 node) {
284 seq_printf(s,
285 " 0x%-14lx %-6u %-9u\n",
286 lnode->vaddr, lnode->size, lnode->id);
287 }
288 mutex_unlock(&ctx->hw_block_list_lock);
289 }
290
291 vm = &ctx->hdev->vm;
292 spin_lock(&vm->idr_lock);
293
294 if (!idr_is_empty(&vm->phys_pg_pack_handles))
295 seq_puts(s, "\n\nallocations:\n");
296
297 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
298 if (phys_pg_pack->asid != ctx->asid)
299 continue;
300
301 seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
302 seq_printf(s, "page size: %u\n\n",
303 phys_pg_pack->page_size);
304 seq_puts(s, " physical address\n");
305 seq_puts(s, "---------------------\n");
306 for (j = 0 ; j < phys_pg_pack->npages ; j++) {
307 seq_printf(s, " 0x%-14llx\n",
308 phys_pg_pack->pages[j]);
309 }
310 }
311 spin_unlock(&vm->idr_lock);
312
313 }
314
315 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
316
317 if (!once)
318 seq_puts(s, "\n");
319
320 return 0;
321 }
322
userptr_lookup_show(struct seq_file * s,void * data)323 static int userptr_lookup_show(struct seq_file *s, void *data)
324 {
325 struct hl_debugfs_entry *entry = s->private;
326 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
327 struct scatterlist *sg;
328 struct hl_userptr *userptr;
329 bool first = true;
330 u64 total_npages, npages, sg_start, sg_end;
331 dma_addr_t dma_addr;
332 int i;
333
334 spin_lock(&dev_entry->userptr_spinlock);
335
336 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
337 if (dev_entry->userptr_lookup >= userptr->addr &&
338 dev_entry->userptr_lookup < userptr->addr + userptr->size) {
339 total_npages = 0;
340 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents,
341 i) {
342 npages = hl_get_sg_info(sg, &dma_addr);
343 sg_start = userptr->addr +
344 total_npages * PAGE_SIZE;
345 sg_end = userptr->addr +
346 (total_npages + npages) * PAGE_SIZE;
347
348 if (dev_entry->userptr_lookup >= sg_start &&
349 dev_entry->userptr_lookup < sg_end) {
350 dma_addr += (dev_entry->userptr_lookup -
351 sg_start);
352 if (first) {
353 first = false;
354 seq_puts(s, "\n");
355 seq_puts(s, " user virtual address dma address pid region start region size\n");
356 seq_puts(s, "---------------------------------------------------------------------------------------\n");
357 }
358 seq_printf(s, " 0x%-18llx 0x%-16llx %-8u 0x%-16llx %-12llu\n",
359 dev_entry->userptr_lookup,
360 (u64)dma_addr, userptr->pid,
361 userptr->addr, userptr->size);
362 }
363 total_npages += npages;
364 }
365 }
366 }
367
368 spin_unlock(&dev_entry->userptr_spinlock);
369
370 if (!first)
371 seq_puts(s, "\n");
372
373 return 0;
374 }
375
userptr_lookup_write(struct file * file,const char __user * buf,size_t count,loff_t * f_pos)376 static ssize_t userptr_lookup_write(struct file *file, const char __user *buf,
377 size_t count, loff_t *f_pos)
378 {
379 struct seq_file *s = file->private_data;
380 struct hl_debugfs_entry *entry = s->private;
381 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
382 ssize_t rc;
383 u64 value;
384
385 rc = kstrtoull_from_user(buf, count, 16, &value);
386 if (rc)
387 return rc;
388
389 dev_entry->userptr_lookup = value;
390
391 return count;
392 }
393
mmu_show(struct seq_file * s,void * data)394 static int mmu_show(struct seq_file *s, void *data)
395 {
396 struct hl_debugfs_entry *entry = s->private;
397 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
398 struct hl_device *hdev = dev_entry->hdev;
399 struct hl_ctx *ctx;
400 struct hl_mmu_hop_info hops_info = {0};
401 u64 virt_addr = dev_entry->mmu_addr, phys_addr;
402 int i;
403
404 if (!hdev->mmu_enable)
405 return 0;
406
407 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
408 ctx = hdev->kernel_ctx;
409 else
410 ctx = hdev->compute_ctx;
411
412 if (!ctx) {
413 dev_err(hdev->dev, "no ctx available\n");
414 return 0;
415 }
416
417 if (hl_mmu_get_tlb_info(ctx, virt_addr, &hops_info)) {
418 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
419 virt_addr);
420 return 0;
421 }
422
423 hl_mmu_va_to_pa(ctx, virt_addr, &phys_addr);
424
425 if (hops_info.scrambled_vaddr &&
426 (dev_entry->mmu_addr != hops_info.scrambled_vaddr))
427 seq_printf(s,
428 "asid: %u, virt_addr: 0x%llx, scrambled virt_addr: 0x%llx,\nphys_addr: 0x%llx, scrambled_phys_addr: 0x%llx\n",
429 dev_entry->mmu_asid, dev_entry->mmu_addr,
430 hops_info.scrambled_vaddr,
431 hops_info.unscrambled_paddr, phys_addr);
432 else
433 seq_printf(s,
434 "asid: %u, virt_addr: 0x%llx, phys_addr: 0x%llx\n",
435 dev_entry->mmu_asid, dev_entry->mmu_addr, phys_addr);
436
437 for (i = 0 ; i < hops_info.used_hops ; i++) {
438 seq_printf(s, "hop%d_addr: 0x%llx\n",
439 i, hops_info.hop_info[i].hop_addr);
440 seq_printf(s, "hop%d_pte_addr: 0x%llx\n",
441 i, hops_info.hop_info[i].hop_pte_addr);
442 seq_printf(s, "hop%d_pte: 0x%llx\n",
443 i, hops_info.hop_info[i].hop_pte_val);
444 }
445
446 return 0;
447 }
448
mmu_asid_va_write(struct file * file,const char __user * buf,size_t count,loff_t * f_pos)449 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
450 size_t count, loff_t *f_pos)
451 {
452 struct seq_file *s = file->private_data;
453 struct hl_debugfs_entry *entry = s->private;
454 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
455 struct hl_device *hdev = dev_entry->hdev;
456 char kbuf[MMU_KBUF_SIZE];
457 char *c;
458 ssize_t rc;
459
460 if (!hdev->mmu_enable)
461 return count;
462
463 if (count > sizeof(kbuf) - 1)
464 goto err;
465 if (copy_from_user(kbuf, buf, count))
466 goto err;
467 kbuf[count] = 0;
468
469 c = strchr(kbuf, ' ');
470 if (!c)
471 goto err;
472 *c = '\0';
473
474 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
475 if (rc)
476 goto err;
477
478 if (strncmp(c+1, "0x", 2))
479 goto err;
480 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
481 if (rc)
482 goto err;
483
484 return count;
485
486 err:
487 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
488
489 return -EINVAL;
490 }
491
engines_show(struct seq_file * s,void * data)492 static int engines_show(struct seq_file *s, void *data)
493 {
494 struct hl_debugfs_entry *entry = s->private;
495 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
496 struct hl_device *hdev = dev_entry->hdev;
497
498 if (atomic_read(&hdev->in_reset)) {
499 dev_warn_ratelimited(hdev->dev,
500 "Can't check device idle during reset\n");
501 return 0;
502 }
503
504 hdev->asic_funcs->is_device_idle(hdev, NULL, 0, s);
505
506 return 0;
507 }
508
hl_is_device_va(struct hl_device * hdev,u64 addr)509 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
510 {
511 struct asic_fixed_properties *prop = &hdev->asic_prop;
512
513 if (!hdev->mmu_enable)
514 goto out;
515
516 if (prop->dram_supports_virtual_memory &&
517 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
518 return true;
519
520 if (addr >= prop->pmmu.start_addr &&
521 addr < prop->pmmu.end_addr)
522 return true;
523
524 if (addr >= prop->pmmu_huge.start_addr &&
525 addr < prop->pmmu_huge.end_addr)
526 return true;
527 out:
528 return false;
529 }
530
hl_is_device_internal_memory_va(struct hl_device * hdev,u64 addr,u32 size)531 static bool hl_is_device_internal_memory_va(struct hl_device *hdev, u64 addr,
532 u32 size)
533 {
534 struct asic_fixed_properties *prop = &hdev->asic_prop;
535 u64 dram_start_addr, dram_end_addr;
536
537 if (!hdev->mmu_enable)
538 return false;
539
540 if (prop->dram_supports_virtual_memory) {
541 dram_start_addr = prop->dmmu.start_addr;
542 dram_end_addr = prop->dmmu.end_addr;
543 } else {
544 dram_start_addr = prop->dram_base_address;
545 dram_end_addr = prop->dram_end_address;
546 }
547
548 if (hl_mem_area_inside_range(addr, size, dram_start_addr,
549 dram_end_addr))
550 return true;
551
552 if (hl_mem_area_inside_range(addr, size, prop->sram_base_address,
553 prop->sram_end_address))
554 return true;
555
556 return false;
557 }
558
device_va_to_pa(struct hl_device * hdev,u64 virt_addr,u32 size,u64 * phys_addr)559 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr, u32 size,
560 u64 *phys_addr)
561 {
562 struct hl_vm_phys_pg_pack *phys_pg_pack;
563 struct hl_ctx *ctx = hdev->compute_ctx;
564 struct hl_vm_hash_node *hnode;
565 u64 end_address, range_size;
566 struct hl_userptr *userptr;
567 enum vm_type *vm_type;
568 bool valid = false;
569 int i, rc = 0;
570
571 if (!ctx) {
572 dev_err(hdev->dev, "no ctx available\n");
573 return -EINVAL;
574 }
575
576 /* Verify address is mapped */
577 mutex_lock(&ctx->mem_hash_lock);
578 hash_for_each(ctx->mem_hash, i, hnode, node) {
579 vm_type = hnode->ptr;
580
581 if (*vm_type == VM_TYPE_USERPTR) {
582 userptr = hnode->ptr;
583 range_size = userptr->size;
584 } else {
585 phys_pg_pack = hnode->ptr;
586 range_size = phys_pg_pack->total_size;
587 }
588
589 end_address = virt_addr + size;
590 if ((virt_addr >= hnode->vaddr) &&
591 (end_address <= hnode->vaddr + range_size)) {
592 valid = true;
593 break;
594 }
595 }
596 mutex_unlock(&ctx->mem_hash_lock);
597
598 if (!valid) {
599 dev_err(hdev->dev,
600 "virt addr 0x%llx is not mapped\n",
601 virt_addr);
602 return -EINVAL;
603 }
604
605 rc = hl_mmu_va_to_pa(ctx, virt_addr, phys_addr);
606 if (rc) {
607 dev_err(hdev->dev,
608 "virt addr 0x%llx is not mapped to phys addr\n",
609 virt_addr);
610 rc = -EINVAL;
611 }
612
613 return rc;
614 }
615
hl_data_read32(struct file * f,char __user * buf,size_t count,loff_t * ppos)616 static ssize_t hl_data_read32(struct file *f, char __user *buf,
617 size_t count, loff_t *ppos)
618 {
619 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
620 struct hl_device *hdev = entry->hdev;
621 u64 addr = entry->addr;
622 bool user_address;
623 char tmp_buf[32];
624 ssize_t rc;
625 u32 val;
626
627 if (atomic_read(&hdev->in_reset)) {
628 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
629 return 0;
630 }
631
632 if (*ppos)
633 return 0;
634
635 user_address = hl_is_device_va(hdev, addr);
636 if (user_address) {
637 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
638 if (rc)
639 return rc;
640 }
641
642 rc = hdev->asic_funcs->debugfs_read32(hdev, addr, user_address, &val);
643 if (rc) {
644 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
645 return rc;
646 }
647
648 sprintf(tmp_buf, "0x%08x\n", val);
649 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
650 strlen(tmp_buf));
651 }
652
hl_data_write32(struct file * f,const char __user * buf,size_t count,loff_t * ppos)653 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
654 size_t count, loff_t *ppos)
655 {
656 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
657 struct hl_device *hdev = entry->hdev;
658 u64 addr = entry->addr;
659 bool user_address;
660 u32 value;
661 ssize_t rc;
662
663 if (atomic_read(&hdev->in_reset)) {
664 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
665 return 0;
666 }
667
668 rc = kstrtouint_from_user(buf, count, 16, &value);
669 if (rc)
670 return rc;
671
672 user_address = hl_is_device_va(hdev, addr);
673 if (user_address) {
674 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
675 if (rc)
676 return rc;
677 }
678
679 rc = hdev->asic_funcs->debugfs_write32(hdev, addr, user_address, value);
680 if (rc) {
681 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
682 value, addr);
683 return rc;
684 }
685
686 return count;
687 }
688
hl_data_read64(struct file * f,char __user * buf,size_t count,loff_t * ppos)689 static ssize_t hl_data_read64(struct file *f, char __user *buf,
690 size_t count, loff_t *ppos)
691 {
692 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
693 struct hl_device *hdev = entry->hdev;
694 u64 addr = entry->addr;
695 bool user_address;
696 char tmp_buf[32];
697 ssize_t rc;
698 u64 val;
699
700 if (atomic_read(&hdev->in_reset)) {
701 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
702 return 0;
703 }
704
705 if (*ppos)
706 return 0;
707
708 user_address = hl_is_device_va(hdev, addr);
709 if (user_address) {
710 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
711 if (rc)
712 return rc;
713 }
714
715 rc = hdev->asic_funcs->debugfs_read64(hdev, addr, user_address, &val);
716 if (rc) {
717 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
718 return rc;
719 }
720
721 sprintf(tmp_buf, "0x%016llx\n", val);
722 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
723 strlen(tmp_buf));
724 }
725
hl_data_write64(struct file * f,const char __user * buf,size_t count,loff_t * ppos)726 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
727 size_t count, loff_t *ppos)
728 {
729 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
730 struct hl_device *hdev = entry->hdev;
731 u64 addr = entry->addr;
732 bool user_address;
733 u64 value;
734 ssize_t rc;
735
736 if (atomic_read(&hdev->in_reset)) {
737 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
738 return 0;
739 }
740
741 rc = kstrtoull_from_user(buf, count, 16, &value);
742 if (rc)
743 return rc;
744
745 user_address = hl_is_device_va(hdev, addr);
746 if (user_address) {
747 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
748 if (rc)
749 return rc;
750 }
751
752 rc = hdev->asic_funcs->debugfs_write64(hdev, addr, user_address, value);
753 if (rc) {
754 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
755 value, addr);
756 return rc;
757 }
758
759 return count;
760 }
761
hl_dma_size_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)762 static ssize_t hl_dma_size_write(struct file *f, const char __user *buf,
763 size_t count, loff_t *ppos)
764 {
765 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
766 struct hl_device *hdev = entry->hdev;
767 u64 addr = entry->addr;
768 ssize_t rc;
769 u32 size;
770
771 if (atomic_read(&hdev->in_reset)) {
772 dev_warn_ratelimited(hdev->dev, "Can't DMA during reset\n");
773 return 0;
774 }
775 rc = kstrtouint_from_user(buf, count, 16, &size);
776 if (rc)
777 return rc;
778
779 if (!size) {
780 dev_err(hdev->dev, "DMA read failed. size can't be 0\n");
781 return -EINVAL;
782 }
783
784 if (size > SZ_128M) {
785 dev_err(hdev->dev,
786 "DMA read failed. size can't be larger than 128MB\n");
787 return -EINVAL;
788 }
789
790 if (!hl_is_device_internal_memory_va(hdev, addr, size)) {
791 dev_err(hdev->dev,
792 "DMA read failed. Invalid 0x%010llx + 0x%08x\n",
793 addr, size);
794 return -EINVAL;
795 }
796
797 /* Free the previous allocation, if there was any */
798 entry->blob_desc.size = 0;
799 vfree(entry->blob_desc.data);
800
801 entry->blob_desc.data = vmalloc(size);
802 if (!entry->blob_desc.data)
803 return -ENOMEM;
804
805 rc = hdev->asic_funcs->debugfs_read_dma(hdev, addr, size,
806 entry->blob_desc.data);
807 if (rc) {
808 dev_err(hdev->dev, "Failed to DMA from 0x%010llx\n", addr);
809 vfree(entry->blob_desc.data);
810 entry->blob_desc.data = NULL;
811 return -EIO;
812 }
813
814 entry->blob_desc.size = size;
815
816 return count;
817 }
818
hl_get_power_state(struct file * f,char __user * buf,size_t count,loff_t * ppos)819 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
820 size_t count, loff_t *ppos)
821 {
822 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
823 struct hl_device *hdev = entry->hdev;
824 char tmp_buf[200];
825 int i;
826
827 if (*ppos)
828 return 0;
829
830 if (hdev->pdev->current_state == PCI_D0)
831 i = 1;
832 else if (hdev->pdev->current_state == PCI_D3hot)
833 i = 2;
834 else
835 i = 3;
836
837 sprintf(tmp_buf,
838 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
839 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
840 strlen(tmp_buf));
841 }
842
hl_set_power_state(struct file * f,const char __user * buf,size_t count,loff_t * ppos)843 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
844 size_t count, loff_t *ppos)
845 {
846 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
847 struct hl_device *hdev = entry->hdev;
848 u32 value;
849 ssize_t rc;
850
851 rc = kstrtouint_from_user(buf, count, 10, &value);
852 if (rc)
853 return rc;
854
855 if (value == 1) {
856 pci_set_power_state(hdev->pdev, PCI_D0);
857 pci_restore_state(hdev->pdev);
858 rc = pci_enable_device(hdev->pdev);
859 } else if (value == 2) {
860 pci_save_state(hdev->pdev);
861 pci_disable_device(hdev->pdev);
862 pci_set_power_state(hdev->pdev, PCI_D3hot);
863 } else {
864 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
865 return -EINVAL;
866 }
867
868 return count;
869 }
870
hl_i2c_data_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)871 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
872 size_t count, loff_t *ppos)
873 {
874 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
875 struct hl_device *hdev = entry->hdev;
876 char tmp_buf[32];
877 long val;
878 ssize_t rc;
879
880 if (*ppos)
881 return 0;
882
883 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
884 entry->i2c_reg, &val);
885 if (rc) {
886 dev_err(hdev->dev,
887 "Failed to read from I2C bus %d, addr %d, reg %d\n",
888 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
889 return rc;
890 }
891
892 sprintf(tmp_buf, "0x%02lx\n", val);
893 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
894 strlen(tmp_buf));
895
896 return rc;
897 }
898
hl_i2c_data_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)899 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
900 size_t count, loff_t *ppos)
901 {
902 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
903 struct hl_device *hdev = entry->hdev;
904 u32 value;
905 ssize_t rc;
906
907 rc = kstrtouint_from_user(buf, count, 16, &value);
908 if (rc)
909 return rc;
910
911 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
912 entry->i2c_reg, value);
913 if (rc) {
914 dev_err(hdev->dev,
915 "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
916 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
917 return rc;
918 }
919
920 return count;
921 }
922
hl_led0_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)923 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
924 size_t count, loff_t *ppos)
925 {
926 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
927 struct hl_device *hdev = entry->hdev;
928 u32 value;
929 ssize_t rc;
930
931 rc = kstrtouint_from_user(buf, count, 10, &value);
932 if (rc)
933 return rc;
934
935 value = value ? 1 : 0;
936
937 hl_debugfs_led_set(hdev, 0, value);
938
939 return count;
940 }
941
hl_led1_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)942 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
943 size_t count, loff_t *ppos)
944 {
945 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
946 struct hl_device *hdev = entry->hdev;
947 u32 value;
948 ssize_t rc;
949
950 rc = kstrtouint_from_user(buf, count, 10, &value);
951 if (rc)
952 return rc;
953
954 value = value ? 1 : 0;
955
956 hl_debugfs_led_set(hdev, 1, value);
957
958 return count;
959 }
960
hl_led2_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)961 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
962 size_t count, loff_t *ppos)
963 {
964 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
965 struct hl_device *hdev = entry->hdev;
966 u32 value;
967 ssize_t rc;
968
969 rc = kstrtouint_from_user(buf, count, 10, &value);
970 if (rc)
971 return rc;
972
973 value = value ? 1 : 0;
974
975 hl_debugfs_led_set(hdev, 2, value);
976
977 return count;
978 }
979
hl_device_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)980 static ssize_t hl_device_read(struct file *f, char __user *buf,
981 size_t count, loff_t *ppos)
982 {
983 static const char *help =
984 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
985 return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
986 }
987
hl_device_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)988 static ssize_t hl_device_write(struct file *f, const char __user *buf,
989 size_t count, loff_t *ppos)
990 {
991 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
992 struct hl_device *hdev = entry->hdev;
993 char data[30] = {0};
994
995 /* don't allow partial writes */
996 if (*ppos != 0)
997 return 0;
998
999 simple_write_to_buffer(data, 29, ppos, buf, count);
1000
1001 if (strncmp("disable", data, strlen("disable")) == 0) {
1002 hdev->disabled = true;
1003 } else if (strncmp("enable", data, strlen("enable")) == 0) {
1004 hdev->disabled = false;
1005 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
1006 hdev->asic_funcs->suspend(hdev);
1007 } else if (strncmp("resume", data, strlen("resume")) == 0) {
1008 hdev->asic_funcs->resume(hdev);
1009 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
1010 hdev->device_cpu_disabled = true;
1011 } else {
1012 dev_err(hdev->dev,
1013 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
1014 count = -EINVAL;
1015 }
1016
1017 return count;
1018 }
1019
hl_clk_gate_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)1020 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
1021 size_t count, loff_t *ppos)
1022 {
1023 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1024 struct hl_device *hdev = entry->hdev;
1025 char tmp_buf[200];
1026 ssize_t rc;
1027
1028 if (*ppos)
1029 return 0;
1030
1031 sprintf(tmp_buf, "0x%llx\n", hdev->clock_gating_mask);
1032 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
1033 strlen(tmp_buf) + 1);
1034
1035 return rc;
1036 }
1037
hl_clk_gate_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)1038 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1039 size_t count, loff_t *ppos)
1040 {
1041 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1042 struct hl_device *hdev = entry->hdev;
1043 u64 value;
1044 ssize_t rc;
1045
1046 if (atomic_read(&hdev->in_reset)) {
1047 dev_warn_ratelimited(hdev->dev,
1048 "Can't change clock gating during reset\n");
1049 return 0;
1050 }
1051
1052 rc = kstrtoull_from_user(buf, count, 16, &value);
1053 if (rc)
1054 return rc;
1055
1056 hdev->clock_gating_mask = value;
1057 hdev->asic_funcs->set_clock_gating(hdev);
1058
1059 return count;
1060 }
1061
hl_stop_on_err_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)1062 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1063 size_t count, loff_t *ppos)
1064 {
1065 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1066 struct hl_device *hdev = entry->hdev;
1067 char tmp_buf[200];
1068 ssize_t rc;
1069
1070 if (*ppos)
1071 return 0;
1072
1073 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1074 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1075 strlen(tmp_buf) + 1);
1076
1077 return rc;
1078 }
1079
hl_stop_on_err_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)1080 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1081 size_t count, loff_t *ppos)
1082 {
1083 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1084 struct hl_device *hdev = entry->hdev;
1085 u32 value;
1086 ssize_t rc;
1087
1088 if (atomic_read(&hdev->in_reset)) {
1089 dev_warn_ratelimited(hdev->dev,
1090 "Can't change stop on error during reset\n");
1091 return 0;
1092 }
1093
1094 rc = kstrtouint_from_user(buf, count, 10, &value);
1095 if (rc)
1096 return rc;
1097
1098 hdev->stop_on_err = value ? 1 : 0;
1099
1100 hl_device_reset(hdev, 0);
1101
1102 return count;
1103 }
1104
hl_security_violations_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)1105 static ssize_t hl_security_violations_read(struct file *f, char __user *buf,
1106 size_t count, loff_t *ppos)
1107 {
1108 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1109 struct hl_device *hdev = entry->hdev;
1110
1111 hdev->asic_funcs->ack_protection_bits_errors(hdev);
1112
1113 return 0;
1114 }
1115
hl_state_dump_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)1116 static ssize_t hl_state_dump_read(struct file *f, char __user *buf,
1117 size_t count, loff_t *ppos)
1118 {
1119 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1120 ssize_t rc;
1121
1122 down_read(&entry->state_dump_sem);
1123 if (!entry->state_dump[entry->state_dump_head])
1124 rc = 0;
1125 else
1126 rc = simple_read_from_buffer(
1127 buf, count, ppos,
1128 entry->state_dump[entry->state_dump_head],
1129 strlen(entry->state_dump[entry->state_dump_head]));
1130 up_read(&entry->state_dump_sem);
1131
1132 return rc;
1133 }
1134
hl_state_dump_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)1135 static ssize_t hl_state_dump_write(struct file *f, const char __user *buf,
1136 size_t count, loff_t *ppos)
1137 {
1138 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1139 struct hl_device *hdev = entry->hdev;
1140 ssize_t rc;
1141 u32 size;
1142 int i;
1143
1144 rc = kstrtouint_from_user(buf, count, 10, &size);
1145 if (rc)
1146 return rc;
1147
1148 if (size <= 0 || size >= ARRAY_SIZE(entry->state_dump)) {
1149 dev_err(hdev->dev, "Invalid number of dumps to skip\n");
1150 return -EINVAL;
1151 }
1152
1153 if (entry->state_dump[entry->state_dump_head]) {
1154 down_write(&entry->state_dump_sem);
1155 for (i = 0; i < size; ++i) {
1156 vfree(entry->state_dump[entry->state_dump_head]);
1157 entry->state_dump[entry->state_dump_head] = NULL;
1158 if (entry->state_dump_head > 0)
1159 entry->state_dump_head--;
1160 else
1161 entry->state_dump_head =
1162 ARRAY_SIZE(entry->state_dump) - 1;
1163 }
1164 up_write(&entry->state_dump_sem);
1165 }
1166
1167 return count;
1168 }
1169
hl_timeout_locked_read(struct file * f,char __user * buf,size_t count,loff_t * ppos)1170 static ssize_t hl_timeout_locked_read(struct file *f, char __user *buf,
1171 size_t count, loff_t *ppos)
1172 {
1173 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1174 struct hl_device *hdev = entry->hdev;
1175 char tmp_buf[200];
1176 ssize_t rc;
1177
1178 if (*ppos)
1179 return 0;
1180
1181 sprintf(tmp_buf, "%d\n",
1182 jiffies_to_msecs(hdev->timeout_jiffies) / 1000);
1183 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1184 strlen(tmp_buf) + 1);
1185
1186 return rc;
1187 }
1188
hl_timeout_locked_write(struct file * f,const char __user * buf,size_t count,loff_t * ppos)1189 static ssize_t hl_timeout_locked_write(struct file *f, const char __user *buf,
1190 size_t count, loff_t *ppos)
1191 {
1192 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1193 struct hl_device *hdev = entry->hdev;
1194 u32 value;
1195 ssize_t rc;
1196
1197 rc = kstrtouint_from_user(buf, count, 10, &value);
1198 if (rc)
1199 return rc;
1200
1201 if (value)
1202 hdev->timeout_jiffies = msecs_to_jiffies(value * 1000);
1203 else
1204 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
1205
1206 return count;
1207 }
1208
1209 static const struct file_operations hl_data32b_fops = {
1210 .owner = THIS_MODULE,
1211 .read = hl_data_read32,
1212 .write = hl_data_write32
1213 };
1214
1215 static const struct file_operations hl_data64b_fops = {
1216 .owner = THIS_MODULE,
1217 .read = hl_data_read64,
1218 .write = hl_data_write64
1219 };
1220
1221 static const struct file_operations hl_dma_size_fops = {
1222 .owner = THIS_MODULE,
1223 .write = hl_dma_size_write
1224 };
1225
1226 static const struct file_operations hl_i2c_data_fops = {
1227 .owner = THIS_MODULE,
1228 .read = hl_i2c_data_read,
1229 .write = hl_i2c_data_write
1230 };
1231
1232 static const struct file_operations hl_power_fops = {
1233 .owner = THIS_MODULE,
1234 .read = hl_get_power_state,
1235 .write = hl_set_power_state
1236 };
1237
1238 static const struct file_operations hl_led0_fops = {
1239 .owner = THIS_MODULE,
1240 .write = hl_led0_write
1241 };
1242
1243 static const struct file_operations hl_led1_fops = {
1244 .owner = THIS_MODULE,
1245 .write = hl_led1_write
1246 };
1247
1248 static const struct file_operations hl_led2_fops = {
1249 .owner = THIS_MODULE,
1250 .write = hl_led2_write
1251 };
1252
1253 static const struct file_operations hl_device_fops = {
1254 .owner = THIS_MODULE,
1255 .read = hl_device_read,
1256 .write = hl_device_write
1257 };
1258
1259 static const struct file_operations hl_clk_gate_fops = {
1260 .owner = THIS_MODULE,
1261 .read = hl_clk_gate_read,
1262 .write = hl_clk_gate_write
1263 };
1264
1265 static const struct file_operations hl_stop_on_err_fops = {
1266 .owner = THIS_MODULE,
1267 .read = hl_stop_on_err_read,
1268 .write = hl_stop_on_err_write
1269 };
1270
1271 static const struct file_operations hl_security_violations_fops = {
1272 .owner = THIS_MODULE,
1273 .read = hl_security_violations_read
1274 };
1275
1276 static const struct file_operations hl_state_dump_fops = {
1277 .owner = THIS_MODULE,
1278 .read = hl_state_dump_read,
1279 .write = hl_state_dump_write
1280 };
1281
1282 static const struct file_operations hl_timeout_locked_fops = {
1283 .owner = THIS_MODULE,
1284 .read = hl_timeout_locked_read,
1285 .write = hl_timeout_locked_write
1286 };
1287
1288 static const struct hl_info_list hl_debugfs_list[] = {
1289 {"command_buffers", command_buffers_show, NULL},
1290 {"command_submission", command_submission_show, NULL},
1291 {"command_submission_jobs", command_submission_jobs_show, NULL},
1292 {"userptr", userptr_show, NULL},
1293 {"vm", vm_show, NULL},
1294 {"userptr_lookup", userptr_lookup_show, userptr_lookup_write},
1295 {"mmu", mmu_show, mmu_asid_va_write},
1296 {"engines", engines_show, NULL}
1297 };
1298
hl_debugfs_open(struct inode * inode,struct file * file)1299 static int hl_debugfs_open(struct inode *inode, struct file *file)
1300 {
1301 struct hl_debugfs_entry *node = inode->i_private;
1302
1303 return single_open(file, node->info_ent->show, node);
1304 }
1305
hl_debugfs_write(struct file * file,const char __user * buf,size_t count,loff_t * f_pos)1306 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1307 size_t count, loff_t *f_pos)
1308 {
1309 struct hl_debugfs_entry *node = file->f_inode->i_private;
1310
1311 if (node->info_ent->write)
1312 return node->info_ent->write(file, buf, count, f_pos);
1313 else
1314 return -EINVAL;
1315
1316 }
1317
1318 static const struct file_operations hl_debugfs_fops = {
1319 .owner = THIS_MODULE,
1320 .open = hl_debugfs_open,
1321 .read = seq_read,
1322 .write = hl_debugfs_write,
1323 .llseek = seq_lseek,
1324 .release = single_release,
1325 };
1326
hl_debugfs_add_device(struct hl_device * hdev)1327 void hl_debugfs_add_device(struct hl_device *hdev)
1328 {
1329 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1330 int count = ARRAY_SIZE(hl_debugfs_list);
1331 struct hl_debugfs_entry *entry;
1332 int i;
1333
1334 dev_entry->hdev = hdev;
1335 dev_entry->entry_arr = kmalloc_array(count,
1336 sizeof(struct hl_debugfs_entry),
1337 GFP_KERNEL);
1338 if (!dev_entry->entry_arr)
1339 return;
1340
1341 dev_entry->blob_desc.size = 0;
1342 dev_entry->blob_desc.data = NULL;
1343
1344 INIT_LIST_HEAD(&dev_entry->file_list);
1345 INIT_LIST_HEAD(&dev_entry->cb_list);
1346 INIT_LIST_HEAD(&dev_entry->cs_list);
1347 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1348 INIT_LIST_HEAD(&dev_entry->userptr_list);
1349 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1350 mutex_init(&dev_entry->file_mutex);
1351 init_rwsem(&dev_entry->state_dump_sem);
1352 spin_lock_init(&dev_entry->cb_spinlock);
1353 spin_lock_init(&dev_entry->cs_spinlock);
1354 spin_lock_init(&dev_entry->cs_job_spinlock);
1355 spin_lock_init(&dev_entry->userptr_spinlock);
1356 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1357
1358 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1359 hl_debug_root);
1360
1361 debugfs_create_x64("addr",
1362 0644,
1363 dev_entry->root,
1364 &dev_entry->addr);
1365
1366 debugfs_create_file("data32",
1367 0644,
1368 dev_entry->root,
1369 dev_entry,
1370 &hl_data32b_fops);
1371
1372 debugfs_create_file("data64",
1373 0644,
1374 dev_entry->root,
1375 dev_entry,
1376 &hl_data64b_fops);
1377
1378 debugfs_create_file("set_power_state",
1379 0200,
1380 dev_entry->root,
1381 dev_entry,
1382 &hl_power_fops);
1383
1384 debugfs_create_u8("i2c_bus",
1385 0644,
1386 dev_entry->root,
1387 &dev_entry->i2c_bus);
1388
1389 debugfs_create_u8("i2c_addr",
1390 0644,
1391 dev_entry->root,
1392 &dev_entry->i2c_addr);
1393
1394 debugfs_create_u8("i2c_reg",
1395 0644,
1396 dev_entry->root,
1397 &dev_entry->i2c_reg);
1398
1399 debugfs_create_file("i2c_data",
1400 0644,
1401 dev_entry->root,
1402 dev_entry,
1403 &hl_i2c_data_fops);
1404
1405 debugfs_create_file("led0",
1406 0200,
1407 dev_entry->root,
1408 dev_entry,
1409 &hl_led0_fops);
1410
1411 debugfs_create_file("led1",
1412 0200,
1413 dev_entry->root,
1414 dev_entry,
1415 &hl_led1_fops);
1416
1417 debugfs_create_file("led2",
1418 0200,
1419 dev_entry->root,
1420 dev_entry,
1421 &hl_led2_fops);
1422
1423 debugfs_create_file("device",
1424 0200,
1425 dev_entry->root,
1426 dev_entry,
1427 &hl_device_fops);
1428
1429 debugfs_create_file("clk_gate",
1430 0200,
1431 dev_entry->root,
1432 dev_entry,
1433 &hl_clk_gate_fops);
1434
1435 debugfs_create_file("stop_on_err",
1436 0644,
1437 dev_entry->root,
1438 dev_entry,
1439 &hl_stop_on_err_fops);
1440
1441 debugfs_create_file("dump_security_violations",
1442 0644,
1443 dev_entry->root,
1444 dev_entry,
1445 &hl_security_violations_fops);
1446
1447 debugfs_create_file("dma_size",
1448 0200,
1449 dev_entry->root,
1450 dev_entry,
1451 &hl_dma_size_fops);
1452
1453 debugfs_create_blob("data_dma",
1454 0400,
1455 dev_entry->root,
1456 &dev_entry->blob_desc);
1457
1458 debugfs_create_x8("skip_reset_on_timeout",
1459 0644,
1460 dev_entry->root,
1461 &hdev->skip_reset_on_timeout);
1462
1463 debugfs_create_file("state_dump",
1464 0600,
1465 dev_entry->root,
1466 dev_entry,
1467 &hl_state_dump_fops);
1468
1469 debugfs_create_file("timeout_locked",
1470 0644,
1471 dev_entry->root,
1472 dev_entry,
1473 &hl_timeout_locked_fops);
1474
1475 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1476 debugfs_create_file(hl_debugfs_list[i].name,
1477 0444,
1478 dev_entry->root,
1479 entry,
1480 &hl_debugfs_fops);
1481 entry->info_ent = &hl_debugfs_list[i];
1482 entry->dev_entry = dev_entry;
1483 }
1484 }
1485
hl_debugfs_remove_device(struct hl_device * hdev)1486 void hl_debugfs_remove_device(struct hl_device *hdev)
1487 {
1488 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1489 int i;
1490
1491 debugfs_remove_recursive(entry->root);
1492
1493 mutex_destroy(&entry->file_mutex);
1494
1495 vfree(entry->blob_desc.data);
1496
1497 for (i = 0; i < ARRAY_SIZE(entry->state_dump); ++i)
1498 vfree(entry->state_dump[i]);
1499
1500 kfree(entry->entry_arr);
1501 }
1502
hl_debugfs_add_file(struct hl_fpriv * hpriv)1503 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1504 {
1505 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1506
1507 mutex_lock(&dev_entry->file_mutex);
1508 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1509 mutex_unlock(&dev_entry->file_mutex);
1510 }
1511
hl_debugfs_remove_file(struct hl_fpriv * hpriv)1512 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1513 {
1514 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1515
1516 mutex_lock(&dev_entry->file_mutex);
1517 list_del(&hpriv->debugfs_list);
1518 mutex_unlock(&dev_entry->file_mutex);
1519 }
1520
hl_debugfs_add_cb(struct hl_cb * cb)1521 void hl_debugfs_add_cb(struct hl_cb *cb)
1522 {
1523 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1524
1525 spin_lock(&dev_entry->cb_spinlock);
1526 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1527 spin_unlock(&dev_entry->cb_spinlock);
1528 }
1529
hl_debugfs_remove_cb(struct hl_cb * cb)1530 void hl_debugfs_remove_cb(struct hl_cb *cb)
1531 {
1532 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1533
1534 spin_lock(&dev_entry->cb_spinlock);
1535 list_del(&cb->debugfs_list);
1536 spin_unlock(&dev_entry->cb_spinlock);
1537 }
1538
hl_debugfs_add_cs(struct hl_cs * cs)1539 void hl_debugfs_add_cs(struct hl_cs *cs)
1540 {
1541 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1542
1543 spin_lock(&dev_entry->cs_spinlock);
1544 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1545 spin_unlock(&dev_entry->cs_spinlock);
1546 }
1547
hl_debugfs_remove_cs(struct hl_cs * cs)1548 void hl_debugfs_remove_cs(struct hl_cs *cs)
1549 {
1550 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1551
1552 spin_lock(&dev_entry->cs_spinlock);
1553 list_del(&cs->debugfs_list);
1554 spin_unlock(&dev_entry->cs_spinlock);
1555 }
1556
hl_debugfs_add_job(struct hl_device * hdev,struct hl_cs_job * job)1557 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1558 {
1559 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1560
1561 spin_lock(&dev_entry->cs_job_spinlock);
1562 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1563 spin_unlock(&dev_entry->cs_job_spinlock);
1564 }
1565
hl_debugfs_remove_job(struct hl_device * hdev,struct hl_cs_job * job)1566 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1567 {
1568 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1569
1570 spin_lock(&dev_entry->cs_job_spinlock);
1571 list_del(&job->debugfs_list);
1572 spin_unlock(&dev_entry->cs_job_spinlock);
1573 }
1574
hl_debugfs_add_userptr(struct hl_device * hdev,struct hl_userptr * userptr)1575 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1576 {
1577 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1578
1579 spin_lock(&dev_entry->userptr_spinlock);
1580 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1581 spin_unlock(&dev_entry->userptr_spinlock);
1582 }
1583
hl_debugfs_remove_userptr(struct hl_device * hdev,struct hl_userptr * userptr)1584 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1585 struct hl_userptr *userptr)
1586 {
1587 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1588
1589 spin_lock(&dev_entry->userptr_spinlock);
1590 list_del(&userptr->debugfs_list);
1591 spin_unlock(&dev_entry->userptr_spinlock);
1592 }
1593
hl_debugfs_add_ctx_mem_hash(struct hl_device * hdev,struct hl_ctx * ctx)1594 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1595 {
1596 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1597
1598 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1599 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1600 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1601 }
1602
hl_debugfs_remove_ctx_mem_hash(struct hl_device * hdev,struct hl_ctx * ctx)1603 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1604 {
1605 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1606
1607 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1608 list_del(&ctx->debugfs_list);
1609 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1610 }
1611
1612 /**
1613 * hl_debugfs_set_state_dump - register state dump making it accessible via
1614 * debugfs
1615 * @hdev: pointer to the device structure
1616 * @data: the actual dump data
1617 * @length: the length of the data
1618 */
hl_debugfs_set_state_dump(struct hl_device * hdev,char * data,unsigned long length)1619 void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
1620 unsigned long length)
1621 {
1622 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1623
1624 down_write(&dev_entry->state_dump_sem);
1625
1626 dev_entry->state_dump_head = (dev_entry->state_dump_head + 1) %
1627 ARRAY_SIZE(dev_entry->state_dump);
1628 vfree(dev_entry->state_dump[dev_entry->state_dump_head]);
1629 dev_entry->state_dump[dev_entry->state_dump_head] = data;
1630
1631 up_write(&dev_entry->state_dump_sem);
1632 }
1633
hl_debugfs_init(void)1634 void __init hl_debugfs_init(void)
1635 {
1636 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1637 }
1638
hl_debugfs_fini(void)1639 void hl_debugfs_fini(void)
1640 {
1641 debugfs_remove_recursive(hl_debug_root);
1642 }
1643