1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
9 #include "../include/hw_ip/mmu/mmu_general.h"
11 #include <linux/pci.h>
12 #include <linux/uaccess.h>
13 #include <linux/vmalloc.h>
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)
19 static struct dentry *hl_debug_root;
21 static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
22 u8 i2c_reg, long *val)
24 struct cpucp_packet pkt;
28 if (!hl_device_operational(hdev, NULL))
31 memset(&pkt, 0, sizeof(pkt));
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;
39 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
45 dev_err(hdev->dev, "Failed to read from I2C, error %d\n", rc);
50 static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
53 struct cpucp_packet pkt;
56 if (!hl_device_operational(hdev, NULL))
59 memset(&pkt, 0, sizeof(pkt));
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);
68 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
72 dev_err(hdev->dev, "Failed to write to I2C, error %d\n", rc);
77 static void hl_debugfs_led_set(struct hl_device *hdev, u8 led, u8 state)
79 struct cpucp_packet pkt;
82 if (!hl_device_operational(hdev, NULL))
85 memset(&pkt, 0, sizeof(pkt));
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);
92 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
96 dev_err(hdev->dev, "Failed to set LED %d, error %d\n", led, rc);
99 static int command_buffers_show(struct seq_file *s, void *data)
101 struct hl_debugfs_entry *entry = s->private;
102 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
106 spin_lock(&dev_entry->cb_spinlock);
108 list_for_each_entry(cb, &dev_entry->cb_list, debugfs_list) {
112 seq_puts(s, " CB ID CTX ID CB size CB RefCnt mmap? CS counter\n");
113 seq_puts(s, "---------------------------------------------------------------\n");
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));
122 spin_unlock(&dev_entry->cb_spinlock);
130 static int command_submission_show(struct seq_file *s, void *data)
132 struct hl_debugfs_entry *entry = s->private;
133 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
137 spin_lock(&dev_entry->cs_spinlock);
139 list_for_each_entry(cs, &dev_entry->cs_list, debugfs_list) {
143 seq_puts(s, " CS ID CTX ASID CS RefCnt Submitted Completed\n");
144 seq_puts(s, "------------------------------------------------------\n");
147 " %llu %d %d %d %d\n",
148 cs->sequence, cs->ctx->asid,
149 kref_read(&cs->refcount),
150 cs->submitted, cs->completed);
153 spin_unlock(&dev_entry->cs_spinlock);
161 static int command_submission_jobs_show(struct seq_file *s, void *data)
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;
168 spin_lock(&dev_entry->cs_job_spinlock);
170 list_for_each_entry(job, &dev_entry->cs_job_list, debugfs_list) {
174 seq_puts(s, " JOB ID CS ID CTX ASID JOB RefCnt H/W Queue\n");
175 seq_puts(s, "----------------------------------------------------\n");
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);
184 " %02d 0 %d %d %d\n",
185 job->id, HL_KERNEL_ASID_ID,
186 kref_read(&job->refcount), job->hw_queue_id);
189 spin_unlock(&dev_entry->cs_job_spinlock);
197 static int userptr_show(struct seq_file *s, void *data)
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"};
206 spin_lock(&dev_entry->userptr_spinlock);
208 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
212 seq_puts(s, " pid user virtual address size dma dir\n");
213 seq_puts(s, "----------------------------------------------------------\n");
215 seq_printf(s, " %-7d 0x%-14llx %-10llu %-30s\n",
216 userptr->pid, userptr->addr, userptr->size,
217 dma_dir[userptr->dir]);
220 spin_unlock(&dev_entry->userptr_spinlock);
228 static int vm_show(struct seq_file *s, void *data)
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;
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;
243 if (!dev_entry->hdev->mmu_enable)
246 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
248 list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
250 seq_puts(s, "\n\n----------------------------------------------------");
251 seq_puts(s, "\n----------------------------------------------------\n\n");
252 seq_printf(s, "ctx asid: %u\n", ctx->asid);
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;
261 if (*vm_type == VM_TYPE_USERPTR) {
262 userptr = hnode->ptr;
264 " 0x%-14llx %-10llu\n",
265 hnode->vaddr, userptr->size);
267 phys_pg_pack = hnode->ptr;
269 " 0x%-14llx %-10llu %-4u\n",
270 hnode->vaddr, phys_pg_pack->total_size,
271 phys_pg_pack->handle);
274 mutex_unlock(&ctx->mem_hash_lock);
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,
285 " 0x%-14lx %-6u %-9u\n",
286 lnode->vaddr, lnode->size, lnode->id);
288 mutex_unlock(&ctx->hw_block_list_lock);
292 spin_lock(&vm->idr_lock);
294 if (!idr_is_empty(&vm->phys_pg_pack_handles))
295 seq_puts(s, "\n\nallocations:\n");
297 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
298 if (phys_pg_pack->asid != ctx->asid)
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]);
311 spin_unlock(&vm->idr_lock);
315 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
323 static int userptr_lookup_show(struct seq_file *s, void *data)
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;
330 u64 total_npages, npages, sg_start, sg_end;
334 spin_lock(&dev_entry->userptr_spinlock);
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) {
340 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents,
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;
348 if (dev_entry->userptr_lookup >= sg_start &&
349 dev_entry->userptr_lookup < sg_end) {
350 dma_addr += (dev_entry->userptr_lookup -
355 seq_puts(s, " user virtual address dma address pid region start region size\n");
356 seq_puts(s, "---------------------------------------------------------------------------------------\n");
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);
363 total_npages += npages;
368 spin_unlock(&dev_entry->userptr_spinlock);
376 static ssize_t userptr_lookup_write(struct file *file, const char __user *buf,
377 size_t count, loff_t *f_pos)
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;
385 rc = kstrtoull_from_user(buf, count, 16, &value);
389 dev_entry->userptr_lookup = value;
394 static int mmu_show(struct seq_file *s, void *data)
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;
400 struct hl_mmu_hop_info hops_info = {0};
401 u64 virt_addr = dev_entry->mmu_addr, phys_addr;
404 if (!hdev->mmu_enable)
407 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
408 ctx = hdev->kernel_ctx;
410 ctx = hdev->compute_ctx;
413 dev_err(hdev->dev, "no ctx available\n");
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",
423 hl_mmu_va_to_pa(ctx, virt_addr, &phys_addr);
425 if (hops_info.scrambled_vaddr &&
426 (dev_entry->mmu_addr != hops_info.scrambled_vaddr))
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);
434 "asid: %u, virt_addr: 0x%llx, phys_addr: 0x%llx\n",
435 dev_entry->mmu_asid, dev_entry->mmu_addr, phys_addr);
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);
449 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
450 size_t count, loff_t *f_pos)
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];
460 if (!hdev->mmu_enable)
463 if (count > sizeof(kbuf) - 1)
465 if (copy_from_user(kbuf, buf, count))
469 c = strchr(kbuf, ' ');
474 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
478 if (strncmp(c+1, "0x", 2))
480 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
487 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
492 static int engines_show(struct seq_file *s, void *data)
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;
498 if (atomic_read(&hdev->in_reset)) {
499 dev_warn_ratelimited(hdev->dev,
500 "Can't check device idle during reset\n");
504 hdev->asic_funcs->is_device_idle(hdev, NULL, 0, s);
509 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
511 struct asic_fixed_properties *prop = &hdev->asic_prop;
513 if (!hdev->mmu_enable)
516 if (prop->dram_supports_virtual_memory &&
517 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
520 if (addr >= prop->pmmu.start_addr &&
521 addr < prop->pmmu.end_addr)
524 if (addr >= prop->pmmu_huge.start_addr &&
525 addr < prop->pmmu_huge.end_addr)
531 static bool hl_is_device_internal_memory_va(struct hl_device *hdev, u64 addr,
534 struct asic_fixed_properties *prop = &hdev->asic_prop;
535 u64 dram_start_addr, dram_end_addr;
537 if (!hdev->mmu_enable)
540 if (prop->dram_supports_virtual_memory) {
541 dram_start_addr = prop->dmmu.start_addr;
542 dram_end_addr = prop->dmmu.end_addr;
544 dram_start_addr = prop->dram_base_address;
545 dram_end_addr = prop->dram_end_address;
548 if (hl_mem_area_inside_range(addr, size, dram_start_addr,
552 if (hl_mem_area_inside_range(addr, size, prop->sram_base_address,
553 prop->sram_end_address))
559 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr, u32 size,
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;
572 dev_err(hdev->dev, "no ctx available\n");
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;
581 if (*vm_type == VM_TYPE_USERPTR) {
582 userptr = hnode->ptr;
583 range_size = userptr->size;
585 phys_pg_pack = hnode->ptr;
586 range_size = phys_pg_pack->total_size;
589 end_address = virt_addr + size;
590 if ((virt_addr >= hnode->vaddr) &&
591 (end_address <= hnode->vaddr + range_size)) {
596 mutex_unlock(&ctx->mem_hash_lock);
600 "virt addr 0x%llx is not mapped\n",
605 rc = hl_mmu_va_to_pa(ctx, virt_addr, phys_addr);
608 "virt addr 0x%llx is not mapped to phys addr\n",
616 static ssize_t hl_data_read32(struct file *f, char __user *buf,
617 size_t count, loff_t *ppos)
619 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
620 struct hl_device *hdev = entry->hdev;
621 u64 addr = entry->addr;
627 if (atomic_read(&hdev->in_reset)) {
628 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
635 user_address = hl_is_device_va(hdev, addr);
637 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
642 rc = hdev->asic_funcs->debugfs_read32(hdev, addr, user_address, &val);
644 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
648 sprintf(tmp_buf, "0x%08x\n", val);
649 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
653 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
654 size_t count, loff_t *ppos)
656 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
657 struct hl_device *hdev = entry->hdev;
658 u64 addr = entry->addr;
663 if (atomic_read(&hdev->in_reset)) {
664 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
668 rc = kstrtouint_from_user(buf, count, 16, &value);
672 user_address = hl_is_device_va(hdev, addr);
674 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
679 rc = hdev->asic_funcs->debugfs_write32(hdev, addr, user_address, value);
681 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
689 static ssize_t hl_data_read64(struct file *f, char __user *buf,
690 size_t count, loff_t *ppos)
692 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
693 struct hl_device *hdev = entry->hdev;
694 u64 addr = entry->addr;
700 if (atomic_read(&hdev->in_reset)) {
701 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
708 user_address = hl_is_device_va(hdev, addr);
710 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
715 rc = hdev->asic_funcs->debugfs_read64(hdev, addr, user_address, &val);
717 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
721 sprintf(tmp_buf, "0x%016llx\n", val);
722 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
726 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
727 size_t count, loff_t *ppos)
729 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
730 struct hl_device *hdev = entry->hdev;
731 u64 addr = entry->addr;
736 if (atomic_read(&hdev->in_reset)) {
737 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
741 rc = kstrtoull_from_user(buf, count, 16, &value);
745 user_address = hl_is_device_va(hdev, addr);
747 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
752 rc = hdev->asic_funcs->debugfs_write64(hdev, addr, user_address, value);
754 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
762 static ssize_t hl_dma_size_write(struct file *f, const char __user *buf,
763 size_t count, loff_t *ppos)
765 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
766 struct hl_device *hdev = entry->hdev;
767 u64 addr = entry->addr;
771 if (atomic_read(&hdev->in_reset)) {
772 dev_warn_ratelimited(hdev->dev, "Can't DMA during reset\n");
775 rc = kstrtouint_from_user(buf, count, 16, &size);
780 dev_err(hdev->dev, "DMA read failed. size can't be 0\n");
784 if (size > SZ_128M) {
786 "DMA read failed. size can't be larger than 128MB\n");
790 if (!hl_is_device_internal_memory_va(hdev, addr, size)) {
792 "DMA read failed. Invalid 0x%010llx + 0x%08x\n",
797 /* Free the previous allocation, if there was any */
798 entry->blob_desc.size = 0;
799 vfree(entry->blob_desc.data);
801 entry->blob_desc.data = vmalloc(size);
802 if (!entry->blob_desc.data)
805 rc = hdev->asic_funcs->debugfs_read_dma(hdev, addr, size,
806 entry->blob_desc.data);
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;
814 entry->blob_desc.size = size;
819 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
820 size_t count, loff_t *ppos)
822 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
823 struct hl_device *hdev = entry->hdev;
830 if (hdev->pdev->current_state == PCI_D0)
832 else if (hdev->pdev->current_state == PCI_D3hot)
838 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
839 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
843 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
844 size_t count, loff_t *ppos)
846 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
847 struct hl_device *hdev = entry->hdev;
851 rc = kstrtouint_from_user(buf, count, 10, &value);
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);
864 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
871 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
872 size_t count, loff_t *ppos)
874 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
875 struct hl_device *hdev = entry->hdev;
883 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
884 entry->i2c_reg, &val);
887 "Failed to read from I2C bus %d, addr %d, reg %d\n",
888 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
892 sprintf(tmp_buf, "0x%02lx\n", val);
893 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
899 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
900 size_t count, loff_t *ppos)
902 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
903 struct hl_device *hdev = entry->hdev;
907 rc = kstrtouint_from_user(buf, count, 16, &value);
911 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
912 entry->i2c_reg, value);
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);
923 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
924 size_t count, loff_t *ppos)
926 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
927 struct hl_device *hdev = entry->hdev;
931 rc = kstrtouint_from_user(buf, count, 10, &value);
935 value = value ? 1 : 0;
937 hl_debugfs_led_set(hdev, 0, value);
942 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
943 size_t count, loff_t *ppos)
945 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
946 struct hl_device *hdev = entry->hdev;
950 rc = kstrtouint_from_user(buf, count, 10, &value);
954 value = value ? 1 : 0;
956 hl_debugfs_led_set(hdev, 1, value);
961 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
962 size_t count, loff_t *ppos)
964 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
965 struct hl_device *hdev = entry->hdev;
969 rc = kstrtouint_from_user(buf, count, 10, &value);
973 value = value ? 1 : 0;
975 hl_debugfs_led_set(hdev, 2, value);
980 static ssize_t hl_device_read(struct file *f, char __user *buf,
981 size_t count, loff_t *ppos)
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));
988 static ssize_t hl_device_write(struct file *f, const char __user *buf,
989 size_t count, loff_t *ppos)
991 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
992 struct hl_device *hdev = entry->hdev;
995 /* don't allow partial writes */
999 simple_write_to_buffer(data, 29, ppos, buf, count);
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;
1013 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
1020 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
1021 size_t count, loff_t *ppos)
1023 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1024 struct hl_device *hdev = entry->hdev;
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);
1038 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1039 size_t count, loff_t *ppos)
1041 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1042 struct hl_device *hdev = entry->hdev;
1046 if (atomic_read(&hdev->in_reset)) {
1047 dev_warn_ratelimited(hdev->dev,
1048 "Can't change clock gating during reset\n");
1052 rc = kstrtoull_from_user(buf, count, 16, &value);
1056 hdev->clock_gating_mask = value;
1057 hdev->asic_funcs->set_clock_gating(hdev);
1062 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1063 size_t count, loff_t *ppos)
1065 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1066 struct hl_device *hdev = entry->hdev;
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);
1080 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1081 size_t count, loff_t *ppos)
1083 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1084 struct hl_device *hdev = entry->hdev;
1088 if (atomic_read(&hdev->in_reset)) {
1089 dev_warn_ratelimited(hdev->dev,
1090 "Can't change stop on error during reset\n");
1094 rc = kstrtouint_from_user(buf, count, 10, &value);
1098 hdev->stop_on_err = value ? 1 : 0;
1100 hl_device_reset(hdev, 0);
1105 static ssize_t hl_security_violations_read(struct file *f, char __user *buf,
1106 size_t count, loff_t *ppos)
1108 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1109 struct hl_device *hdev = entry->hdev;
1111 hdev->asic_funcs->ack_protection_bits_errors(hdev);
1116 static ssize_t hl_state_dump_read(struct file *f, char __user *buf,
1117 size_t count, loff_t *ppos)
1119 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1122 down_read(&entry->state_dump_sem);
1123 if (!entry->state_dump[entry->state_dump_head])
1126 rc = simple_read_from_buffer(
1128 entry->state_dump[entry->state_dump_head],
1129 strlen(entry->state_dump[entry->state_dump_head]));
1130 up_read(&entry->state_dump_sem);
1135 static ssize_t hl_state_dump_write(struct file *f, const char __user *buf,
1136 size_t count, loff_t *ppos)
1138 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1139 struct hl_device *hdev = entry->hdev;
1144 rc = kstrtouint_from_user(buf, count, 10, &size);
1148 if (size <= 0 || size >= ARRAY_SIZE(entry->state_dump)) {
1149 dev_err(hdev->dev, "Invalid number of dumps to skip\n");
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--;
1161 entry->state_dump_head =
1162 ARRAY_SIZE(entry->state_dump) - 1;
1164 up_write(&entry->state_dump_sem);
1170 static const struct file_operations hl_data32b_fops = {
1171 .owner = THIS_MODULE,
1172 .read = hl_data_read32,
1173 .write = hl_data_write32
1176 static const struct file_operations hl_data64b_fops = {
1177 .owner = THIS_MODULE,
1178 .read = hl_data_read64,
1179 .write = hl_data_write64
1182 static const struct file_operations hl_dma_size_fops = {
1183 .owner = THIS_MODULE,
1184 .write = hl_dma_size_write
1187 static const struct file_operations hl_i2c_data_fops = {
1188 .owner = THIS_MODULE,
1189 .read = hl_i2c_data_read,
1190 .write = hl_i2c_data_write
1193 static const struct file_operations hl_power_fops = {
1194 .owner = THIS_MODULE,
1195 .read = hl_get_power_state,
1196 .write = hl_set_power_state
1199 static const struct file_operations hl_led0_fops = {
1200 .owner = THIS_MODULE,
1201 .write = hl_led0_write
1204 static const struct file_operations hl_led1_fops = {
1205 .owner = THIS_MODULE,
1206 .write = hl_led1_write
1209 static const struct file_operations hl_led2_fops = {
1210 .owner = THIS_MODULE,
1211 .write = hl_led2_write
1214 static const struct file_operations hl_device_fops = {
1215 .owner = THIS_MODULE,
1216 .read = hl_device_read,
1217 .write = hl_device_write
1220 static const struct file_operations hl_clk_gate_fops = {
1221 .owner = THIS_MODULE,
1222 .read = hl_clk_gate_read,
1223 .write = hl_clk_gate_write
1226 static 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
1232 static const struct file_operations hl_security_violations_fops = {
1233 .owner = THIS_MODULE,
1234 .read = hl_security_violations_read
1237 static const struct file_operations hl_state_dump_fops = {
1238 .owner = THIS_MODULE,
1239 .read = hl_state_dump_read,
1240 .write = hl_state_dump_write
1243 static 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}
1254 static int hl_debugfs_open(struct inode *inode, struct file *file)
1256 struct hl_debugfs_entry *node = inode->i_private;
1258 return single_open(file, node->info_ent->show, node);
1261 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1262 size_t count, loff_t *f_pos)
1264 struct hl_debugfs_entry *node = file->f_inode->i_private;
1266 if (node->info_ent->write)
1267 return node->info_ent->write(file, buf, count, f_pos);
1273 static const struct file_operations hl_debugfs_fops = {
1274 .owner = THIS_MODULE,
1275 .open = hl_debugfs_open,
1277 .write = hl_debugfs_write,
1278 .llseek = seq_lseek,
1279 .release = single_release,
1282 void hl_debugfs_add_device(struct hl_device *hdev)
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;
1289 dev_entry->hdev = hdev;
1290 dev_entry->entry_arr = kmalloc_array(count,
1291 sizeof(struct hl_debugfs_entry),
1293 if (!dev_entry->entry_arr)
1296 dev_entry->blob_desc.size = 0;
1297 dev_entry->blob_desc.data = NULL;
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);
1313 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1316 debugfs_create_x64("addr",
1321 debugfs_create_file("data32",
1327 debugfs_create_file("data64",
1333 debugfs_create_file("set_power_state",
1339 debugfs_create_u8("i2c_bus",
1342 &dev_entry->i2c_bus);
1344 debugfs_create_u8("i2c_addr",
1347 &dev_entry->i2c_addr);
1349 debugfs_create_u8("i2c_reg",
1352 &dev_entry->i2c_reg);
1354 debugfs_create_file("i2c_data",
1360 debugfs_create_file("led0",
1366 debugfs_create_file("led1",
1372 debugfs_create_file("led2",
1378 debugfs_create_file("device",
1384 debugfs_create_file("clk_gate",
1390 debugfs_create_file("stop_on_err",
1394 &hl_stop_on_err_fops);
1396 debugfs_create_file("dump_security_violations",
1400 &hl_security_violations_fops);
1402 debugfs_create_file("dma_size",
1408 debugfs_create_blob("data_dma",
1411 &dev_entry->blob_desc);
1413 debugfs_create_x8("skip_reset_on_timeout",
1416 &hdev->skip_reset_on_timeout);
1418 debugfs_create_file("state_dump",
1422 &hl_state_dump_fops);
1424 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1425 debugfs_create_file(hl_debugfs_list[i].name,
1430 entry->info_ent = &hl_debugfs_list[i];
1431 entry->dev_entry = dev_entry;
1435 void hl_debugfs_remove_device(struct hl_device *hdev)
1437 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1440 debugfs_remove_recursive(entry->root);
1442 mutex_destroy(&entry->file_mutex);
1444 vfree(entry->blob_desc.data);
1446 for (i = 0; i < ARRAY_SIZE(entry->state_dump); ++i)
1447 vfree(entry->state_dump[i]);
1449 kfree(entry->entry_arr);
1452 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1454 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1456 mutex_lock(&dev_entry->file_mutex);
1457 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1458 mutex_unlock(&dev_entry->file_mutex);
1461 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1463 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1465 mutex_lock(&dev_entry->file_mutex);
1466 list_del(&hpriv->debugfs_list);
1467 mutex_unlock(&dev_entry->file_mutex);
1470 void hl_debugfs_add_cb(struct hl_cb *cb)
1472 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1474 spin_lock(&dev_entry->cb_spinlock);
1475 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1476 spin_unlock(&dev_entry->cb_spinlock);
1479 void hl_debugfs_remove_cb(struct hl_cb *cb)
1481 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1483 spin_lock(&dev_entry->cb_spinlock);
1484 list_del(&cb->debugfs_list);
1485 spin_unlock(&dev_entry->cb_spinlock);
1488 void hl_debugfs_add_cs(struct hl_cs *cs)
1490 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1492 spin_lock(&dev_entry->cs_spinlock);
1493 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1494 spin_unlock(&dev_entry->cs_spinlock);
1497 void hl_debugfs_remove_cs(struct hl_cs *cs)
1499 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1501 spin_lock(&dev_entry->cs_spinlock);
1502 list_del(&cs->debugfs_list);
1503 spin_unlock(&dev_entry->cs_spinlock);
1506 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1508 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
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);
1515 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1517 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1519 spin_lock(&dev_entry->cs_job_spinlock);
1520 list_del(&job->debugfs_list);
1521 spin_unlock(&dev_entry->cs_job_spinlock);
1524 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1526 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1528 spin_lock(&dev_entry->userptr_spinlock);
1529 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1530 spin_unlock(&dev_entry->userptr_spinlock);
1533 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1534 struct hl_userptr *userptr)
1536 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1538 spin_lock(&dev_entry->userptr_spinlock);
1539 list_del(&userptr->debugfs_list);
1540 spin_unlock(&dev_entry->userptr_spinlock);
1543 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1545 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
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);
1552 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1554 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1556 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1557 list_del(&ctx->debugfs_list);
1558 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1562 * hl_debugfs_set_state_dump - register state dump making it accessible via
1564 * @hdev: pointer to the device structure
1565 * @data: the actual dump data
1566 * @length: the length of the data
1568 void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
1569 unsigned long length)
1571 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1573 down_write(&dev_entry->state_dump_sem);
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;
1580 up_write(&dev_entry->state_dump_sem);
1583 void __init hl_debugfs_init(void)
1585 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1588 void hl_debugfs_fini(void)
1590 debugfs_remove_recursive(hl_debug_root);