1
2
3
4
5
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
19static struct dentry *hl_debug_root;
20
21static 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
50static 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
77static 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
99static 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
130static 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
161static 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
197static 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
228static 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
323static 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
376static 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
394static 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
449static 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
486err:
487 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
488
489 return -EINVAL;
490}
491
492static 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
509static 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;
527out:
528 return false;
529}
530
531static 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
559static 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
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
616static 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
653static 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
689static 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
726static 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
762static 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
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
819static 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
843static 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
871static 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
899static 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
923static 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
942static 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
961static 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
980static 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
988static 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
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
1020static 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
1038static 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
1062static 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
1080static 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
1105static 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
1116static 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
1135static 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
1170static const struct file_operations hl_data32b_fops = {
1171 .owner = THIS_MODULE,
1172 .read = hl_data_read32,
1173 .write = hl_data_write32
1174};
1175
1176static const struct file_operations hl_data64b_fops = {
1177 .owner = THIS_MODULE,
1178 .read = hl_data_read64,
1179 .write = hl_data_write64
1180};
1181
1182static const struct file_operations hl_dma_size_fops = {
1183 .owner = THIS_MODULE,
1184 .write = hl_dma_size_write
1185};
1186
1187static const struct file_operations hl_i2c_data_fops = {
1188 .owner = THIS_MODULE,
1189 .read = hl_i2c_data_read,
1190 .write = hl_i2c_data_write
1191};
1192
1193static const struct file_operations hl_power_fops = {
1194 .owner = THIS_MODULE,
1195 .read = hl_get_power_state,
1196 .write = hl_set_power_state
1197};
1198
1199static const struct file_operations hl_led0_fops = {
1200 .owner = THIS_MODULE,
1201 .write = hl_led0_write
1202};
1203
1204static const struct file_operations hl_led1_fops = {
1205 .owner = THIS_MODULE,
1206 .write = hl_led1_write
1207};
1208
1209static const struct file_operations hl_led2_fops = {
1210 .owner = THIS_MODULE,
1211 .write = hl_led2_write
1212};
1213
1214static const struct file_operations hl_device_fops = {
1215 .owner = THIS_MODULE,
1216 .read = hl_device_read,
1217 .write = hl_device_write
1218};
1219
1220static const struct file_operations hl_clk_gate_fops = {
1221 .owner = THIS_MODULE,
1222 .read = hl_clk_gate_read,
1223 .write = hl_clk_gate_write
1224};
1225
1226static const struct file_operations hl_stop_on_err_fops = {
1227 .owner = THIS_MODULE,
1228 .read = hl_stop_on_err_read,
1229 .write = hl_stop_on_err_write
1230};
1231
1232static const struct file_operations hl_security_violations_fops = {
1233 .owner = THIS_MODULE,
1234 .read = hl_security_violations_read
1235};
1236
1237static const struct file_operations hl_state_dump_fops = {
1238 .owner = THIS_MODULE,
1239 .read = hl_state_dump_read,
1240 .write = hl_state_dump_write
1241};
1242
1243static const struct hl_info_list hl_debugfs_list[] = {
1244 {"command_buffers", command_buffers_show, NULL},
1245 {"command_submission", command_submission_show, NULL},
1246 {"command_submission_jobs", command_submission_jobs_show, NULL},
1247 {"userptr", userptr_show, NULL},
1248 {"vm", vm_show, NULL},
1249 {"userptr_lookup", userptr_lookup_show, userptr_lookup_write},
1250 {"mmu", mmu_show, mmu_asid_va_write},
1251 {"engines", engines_show, NULL}
1252};
1253
1254static int hl_debugfs_open(struct inode *inode, struct file *file)
1255{
1256 struct hl_debugfs_entry *node = inode->i_private;
1257
1258 return single_open(file, node->info_ent->show, node);
1259}
1260
1261static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1262 size_t count, loff_t *f_pos)
1263{
1264 struct hl_debugfs_entry *node = file->f_inode->i_private;
1265
1266 if (node->info_ent->write)
1267 return node->info_ent->write(file, buf, count, f_pos);
1268 else
1269 return -EINVAL;
1270
1271}
1272
1273static const struct file_operations hl_debugfs_fops = {
1274 .owner = THIS_MODULE,
1275 .open = hl_debugfs_open,
1276 .read = seq_read,
1277 .write = hl_debugfs_write,
1278 .llseek = seq_lseek,
1279 .release = single_release,
1280};
1281
1282void hl_debugfs_add_device(struct hl_device *hdev)
1283{
1284 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1285 int count = ARRAY_SIZE(hl_debugfs_list);
1286 struct hl_debugfs_entry *entry;
1287 int i;
1288
1289 dev_entry->hdev = hdev;
1290 dev_entry->entry_arr = kmalloc_array(count,
1291 sizeof(struct hl_debugfs_entry),
1292 GFP_KERNEL);
1293 if (!dev_entry->entry_arr)
1294 return;
1295
1296 dev_entry->blob_desc.size = 0;
1297 dev_entry->blob_desc.data = NULL;
1298
1299 INIT_LIST_HEAD(&dev_entry->file_list);
1300 INIT_LIST_HEAD(&dev_entry->cb_list);
1301 INIT_LIST_HEAD(&dev_entry->cs_list);
1302 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1303 INIT_LIST_HEAD(&dev_entry->userptr_list);
1304 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1305 mutex_init(&dev_entry->file_mutex);
1306 init_rwsem(&dev_entry->state_dump_sem);
1307 spin_lock_init(&dev_entry->cb_spinlock);
1308 spin_lock_init(&dev_entry->cs_spinlock);
1309 spin_lock_init(&dev_entry->cs_job_spinlock);
1310 spin_lock_init(&dev_entry->userptr_spinlock);
1311 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1312
1313 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1314 hl_debug_root);
1315
1316 debugfs_create_x64("addr",
1317 0644,
1318 dev_entry->root,
1319 &dev_entry->addr);
1320
1321 debugfs_create_file("data32",
1322 0644,
1323 dev_entry->root,
1324 dev_entry,
1325 &hl_data32b_fops);
1326
1327 debugfs_create_file("data64",
1328 0644,
1329 dev_entry->root,
1330 dev_entry,
1331 &hl_data64b_fops);
1332
1333 debugfs_create_file("set_power_state",
1334 0200,
1335 dev_entry->root,
1336 dev_entry,
1337 &hl_power_fops);
1338
1339 debugfs_create_u8("i2c_bus",
1340 0644,
1341 dev_entry->root,
1342 &dev_entry->i2c_bus);
1343
1344 debugfs_create_u8("i2c_addr",
1345 0644,
1346 dev_entry->root,
1347 &dev_entry->i2c_addr);
1348
1349 debugfs_create_u8("i2c_reg",
1350 0644,
1351 dev_entry->root,
1352 &dev_entry->i2c_reg);
1353
1354 debugfs_create_file("i2c_data",
1355 0644,
1356 dev_entry->root,
1357 dev_entry,
1358 &hl_i2c_data_fops);
1359
1360 debugfs_create_file("led0",
1361 0200,
1362 dev_entry->root,
1363 dev_entry,
1364 &hl_led0_fops);
1365
1366 debugfs_create_file("led1",
1367 0200,
1368 dev_entry->root,
1369 dev_entry,
1370 &hl_led1_fops);
1371
1372 debugfs_create_file("led2",
1373 0200,
1374 dev_entry->root,
1375 dev_entry,
1376 &hl_led2_fops);
1377
1378 debugfs_create_file("device",
1379 0200,
1380 dev_entry->root,
1381 dev_entry,
1382 &hl_device_fops);
1383
1384 debugfs_create_file("clk_gate",
1385 0200,
1386 dev_entry->root,
1387 dev_entry,
1388 &hl_clk_gate_fops);
1389
1390 debugfs_create_file("stop_on_err",
1391 0644,
1392 dev_entry->root,
1393 dev_entry,
1394 &hl_stop_on_err_fops);
1395
1396 debugfs_create_file("dump_security_violations",
1397 0644,
1398 dev_entry->root,
1399 dev_entry,
1400 &hl_security_violations_fops);
1401
1402 debugfs_create_file("dma_size",
1403 0200,
1404 dev_entry->root,
1405 dev_entry,
1406 &hl_dma_size_fops);
1407
1408 debugfs_create_blob("data_dma",
1409 0400,
1410 dev_entry->root,
1411 &dev_entry->blob_desc);
1412
1413 debugfs_create_x8("skip_reset_on_timeout",
1414 0644,
1415 dev_entry->root,
1416 &hdev->skip_reset_on_timeout);
1417
1418 debugfs_create_file("state_dump",
1419 0600,
1420 dev_entry->root,
1421 dev_entry,
1422 &hl_state_dump_fops);
1423
1424 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1425 debugfs_create_file(hl_debugfs_list[i].name,
1426 0444,
1427 dev_entry->root,
1428 entry,
1429 &hl_debugfs_fops);
1430 entry->info_ent = &hl_debugfs_list[i];
1431 entry->dev_entry = dev_entry;
1432 }
1433}
1434
1435void hl_debugfs_remove_device(struct hl_device *hdev)
1436{
1437 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1438 int i;
1439
1440 debugfs_remove_recursive(entry->root);
1441
1442 mutex_destroy(&entry->file_mutex);
1443
1444 vfree(entry->blob_desc.data);
1445
1446 for (i = 0; i < ARRAY_SIZE(entry->state_dump); ++i)
1447 vfree(entry->state_dump[i]);
1448
1449 kfree(entry->entry_arr);
1450}
1451
1452void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1453{
1454 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1455
1456 mutex_lock(&dev_entry->file_mutex);
1457 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1458 mutex_unlock(&dev_entry->file_mutex);
1459}
1460
1461void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1462{
1463 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1464
1465 mutex_lock(&dev_entry->file_mutex);
1466 list_del(&hpriv->debugfs_list);
1467 mutex_unlock(&dev_entry->file_mutex);
1468}
1469
1470void hl_debugfs_add_cb(struct hl_cb *cb)
1471{
1472 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1473
1474 spin_lock(&dev_entry->cb_spinlock);
1475 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1476 spin_unlock(&dev_entry->cb_spinlock);
1477}
1478
1479void hl_debugfs_remove_cb(struct hl_cb *cb)
1480{
1481 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1482
1483 spin_lock(&dev_entry->cb_spinlock);
1484 list_del(&cb->debugfs_list);
1485 spin_unlock(&dev_entry->cb_spinlock);
1486}
1487
1488void hl_debugfs_add_cs(struct hl_cs *cs)
1489{
1490 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1491
1492 spin_lock(&dev_entry->cs_spinlock);
1493 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1494 spin_unlock(&dev_entry->cs_spinlock);
1495}
1496
1497void hl_debugfs_remove_cs(struct hl_cs *cs)
1498{
1499 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1500
1501 spin_lock(&dev_entry->cs_spinlock);
1502 list_del(&cs->debugfs_list);
1503 spin_unlock(&dev_entry->cs_spinlock);
1504}
1505
1506void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1507{
1508 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1509
1510 spin_lock(&dev_entry->cs_job_spinlock);
1511 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1512 spin_unlock(&dev_entry->cs_job_spinlock);
1513}
1514
1515void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1516{
1517 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1518
1519 spin_lock(&dev_entry->cs_job_spinlock);
1520 list_del(&job->debugfs_list);
1521 spin_unlock(&dev_entry->cs_job_spinlock);
1522}
1523
1524void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1525{
1526 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1527
1528 spin_lock(&dev_entry->userptr_spinlock);
1529 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1530 spin_unlock(&dev_entry->userptr_spinlock);
1531}
1532
1533void hl_debugfs_remove_userptr(struct hl_device *hdev,
1534 struct hl_userptr *userptr)
1535{
1536 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1537
1538 spin_lock(&dev_entry->userptr_spinlock);
1539 list_del(&userptr->debugfs_list);
1540 spin_unlock(&dev_entry->userptr_spinlock);
1541}
1542
1543void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1544{
1545 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1546
1547 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1548 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1549 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1550}
1551
1552void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1553{
1554 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1555
1556 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1557 list_del(&ctx->debugfs_list);
1558 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1559}
1560
1561
1562
1563
1564
1565
1566
1567
1568void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
1569 unsigned long length)
1570{
1571 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1572
1573 down_write(&dev_entry->state_dump_sem);
1574
1575 dev_entry->state_dump_head = (dev_entry->state_dump_head + 1) %
1576 ARRAY_SIZE(dev_entry->state_dump);
1577 vfree(dev_entry->state_dump[dev_entry->state_dump_head]);
1578 dev_entry->state_dump[dev_entry->state_dump_head] = data;
1579
1580 up_write(&dev_entry->state_dump_sem);
1581}
1582
1583void __init hl_debugfs_init(void)
1584{
1585 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1586}
1587
1588void hl_debugfs_fini(void)
1589{
1590 debugfs_remove_recursive(hl_debug_root);
1591}
1592