1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2021 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)
18 #define I2C_MAX_TRANSACTION_LEN 8
20 static struct dentry *hl_debug_root;
22 static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
23 u8 i2c_reg, u8 i2c_len, u64 *val)
25 struct cpucp_packet pkt;
28 if (!hl_device_operational(hdev, NULL))
31 if (i2c_len > I2C_MAX_TRANSACTION_LEN) {
32 dev_err(hdev->dev, "I2C transaction length %u, exceeds maximum of %u\n",
33 i2c_len, I2C_MAX_TRANSACTION_LEN);
37 memset(&pkt, 0, sizeof(pkt));
39 pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_RD <<
40 CPUCP_PKT_CTL_OPCODE_SHIFT);
41 pkt.i2c_bus = i2c_bus;
42 pkt.i2c_addr = i2c_addr;
43 pkt.i2c_reg = i2c_reg;
44 pkt.i2c_len = i2c_len;
46 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
49 dev_err(hdev->dev, "Failed to read from I2C, error %d\n", rc);
54 static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
55 u8 i2c_reg, u8 i2c_len, u64 val)
57 struct cpucp_packet pkt;
60 if (!hl_device_operational(hdev, NULL))
63 if (i2c_len > I2C_MAX_TRANSACTION_LEN) {
64 dev_err(hdev->dev, "I2C transaction length %u, exceeds maximum of %u\n",
65 i2c_len, I2C_MAX_TRANSACTION_LEN);
69 memset(&pkt, 0, sizeof(pkt));
71 pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_WR <<
72 CPUCP_PKT_CTL_OPCODE_SHIFT);
73 pkt.i2c_bus = i2c_bus;
74 pkt.i2c_addr = i2c_addr;
75 pkt.i2c_reg = i2c_reg;
76 pkt.i2c_len = i2c_len;
77 pkt.value = cpu_to_le64(val);
79 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
83 dev_err(hdev->dev, "Failed to write to I2C, error %d\n", rc);
88 static void hl_debugfs_led_set(struct hl_device *hdev, u8 led, u8 state)
90 struct cpucp_packet pkt;
93 if (!hl_device_operational(hdev, NULL))
96 memset(&pkt, 0, sizeof(pkt));
98 pkt.ctl = cpu_to_le32(CPUCP_PACKET_LED_SET <<
99 CPUCP_PKT_CTL_OPCODE_SHIFT);
100 pkt.led_index = cpu_to_le32(led);
101 pkt.value = cpu_to_le64(state);
103 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
107 dev_err(hdev->dev, "Failed to set LED %d, error %d\n", led, rc);
110 static int command_buffers_show(struct seq_file *s, void *data)
112 struct hl_debugfs_entry *entry = s->private;
113 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
117 spin_lock(&dev_entry->cb_spinlock);
119 list_for_each_entry(cb, &dev_entry->cb_list, debugfs_list) {
123 seq_puts(s, " CB ID CTX ID CB size CB RefCnt mmap? CS counter\n");
124 seq_puts(s, "---------------------------------------------------------------\n");
127 " %03llu %d 0x%08x %d %d %d\n",
128 cb->id, cb->ctx->asid, cb->size,
129 kref_read(&cb->refcount),
130 cb->mmap, atomic_read(&cb->cs_cnt));
133 spin_unlock(&dev_entry->cb_spinlock);
141 static int command_submission_show(struct seq_file *s, void *data)
143 struct hl_debugfs_entry *entry = s->private;
144 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
148 spin_lock(&dev_entry->cs_spinlock);
150 list_for_each_entry(cs, &dev_entry->cs_list, debugfs_list) {
154 seq_puts(s, " CS ID CTX ASID CS RefCnt Submitted Completed\n");
155 seq_puts(s, "------------------------------------------------------\n");
158 " %llu %d %d %d %d\n",
159 cs->sequence, cs->ctx->asid,
160 kref_read(&cs->refcount),
161 cs->submitted, cs->completed);
164 spin_unlock(&dev_entry->cs_spinlock);
172 static int command_submission_jobs_show(struct seq_file *s, void *data)
174 struct hl_debugfs_entry *entry = s->private;
175 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
176 struct hl_cs_job *job;
179 spin_lock(&dev_entry->cs_job_spinlock);
181 list_for_each_entry(job, &dev_entry->cs_job_list, debugfs_list) {
185 seq_puts(s, " JOB ID CS ID CTX ASID JOB RefCnt H/W Queue\n");
186 seq_puts(s, "----------------------------------------------------\n");
190 " %02d %llu %d %d %d\n",
191 job->id, job->cs->sequence, job->cs->ctx->asid,
192 kref_read(&job->refcount), job->hw_queue_id);
195 " %02d 0 %d %d %d\n",
196 job->id, HL_KERNEL_ASID_ID,
197 kref_read(&job->refcount), job->hw_queue_id);
200 spin_unlock(&dev_entry->cs_job_spinlock);
208 static int userptr_show(struct seq_file *s, void *data)
210 struct hl_debugfs_entry *entry = s->private;
211 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
212 struct hl_userptr *userptr;
213 char dma_dir[4][30] = {"DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
214 "DMA_FROM_DEVICE", "DMA_NONE"};
217 spin_lock(&dev_entry->userptr_spinlock);
219 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
223 seq_puts(s, " pid user virtual address size dma dir\n");
224 seq_puts(s, "----------------------------------------------------------\n");
226 seq_printf(s, " %-7d 0x%-14llx %-10llu %-30s\n",
227 userptr->pid, userptr->addr, userptr->size,
228 dma_dir[userptr->dir]);
231 spin_unlock(&dev_entry->userptr_spinlock);
239 static int vm_show(struct seq_file *s, void *data)
241 struct hl_debugfs_entry *entry = s->private;
242 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
243 struct hl_vm_hw_block_list_node *lnode;
246 struct hl_vm_hash_node *hnode;
247 struct hl_userptr *userptr;
248 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
249 struct hl_va_range *va_range;
250 struct hl_vm_va_block *va_block;
251 enum vm_type *vm_type;
256 if (!dev_entry->hdev->mmu_enable)
259 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
261 list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
263 seq_puts(s, "\n\n----------------------------------------------------");
264 seq_puts(s, "\n----------------------------------------------------\n\n");
265 seq_printf(s, "ctx asid: %u\n", ctx->asid);
267 seq_puts(s, "\nmappings:\n\n");
268 seq_puts(s, " virtual address size handle\n");
269 seq_puts(s, "----------------------------------------------------\n");
270 mutex_lock(&ctx->mem_hash_lock);
271 hash_for_each(ctx->mem_hash, i, hnode, node) {
272 vm_type = hnode->ptr;
274 if (*vm_type == VM_TYPE_USERPTR) {
275 userptr = hnode->ptr;
277 " 0x%-14llx %-10llu\n",
278 hnode->vaddr, userptr->size);
280 phys_pg_pack = hnode->ptr;
282 " 0x%-14llx %-10llu %-4u\n",
283 hnode->vaddr, phys_pg_pack->total_size,
284 phys_pg_pack->handle);
287 mutex_unlock(&ctx->mem_hash_lock);
289 if (ctx->asid != HL_KERNEL_ASID_ID &&
290 !list_empty(&ctx->hw_block_mem_list)) {
291 seq_puts(s, "\nhw_block mappings:\n\n");
292 seq_puts(s, " virtual address size HW block id\n");
293 seq_puts(s, "-------------------------------------------\n");
294 mutex_lock(&ctx->hw_block_list_lock);
295 list_for_each_entry(lnode, &ctx->hw_block_mem_list,
298 " 0x%-14lx %-6u %-9u\n",
299 lnode->vaddr, lnode->size, lnode->id);
301 mutex_unlock(&ctx->hw_block_list_lock);
305 spin_lock(&vm->idr_lock);
307 if (!idr_is_empty(&vm->phys_pg_pack_handles))
308 seq_puts(s, "\n\nallocations:\n");
310 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
311 if (phys_pg_pack->asid != ctx->asid)
314 seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
315 seq_printf(s, "page size: %u\n\n",
316 phys_pg_pack->page_size);
317 seq_puts(s, " physical address\n");
318 seq_puts(s, "---------------------\n");
319 for (j = 0 ; j < phys_pg_pack->npages ; j++) {
320 seq_printf(s, " 0x%-14llx\n",
321 phys_pg_pack->pages[j]);
324 spin_unlock(&vm->idr_lock);
328 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
330 ctx = hl_get_compute_ctx(dev_entry->hdev);
332 seq_puts(s, "\nVA ranges:\n\n");
333 for (i = HL_VA_RANGE_TYPE_HOST ; i < HL_VA_RANGE_TYPE_MAX ; ++i) {
334 va_range = ctx->va_range[i];
335 seq_printf(s, " va_range %d\n", i);
336 seq_puts(s, "---------------------\n");
337 mutex_lock(&va_range->lock);
338 list_for_each_entry(va_block, &va_range->list, node) {
339 seq_printf(s, "%#16llx - %#16llx (%#llx)\n",
340 va_block->start, va_block->end,
343 mutex_unlock(&va_range->lock);
355 static int userptr_lookup_show(struct seq_file *s, void *data)
357 struct hl_debugfs_entry *entry = s->private;
358 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
359 struct scatterlist *sg;
360 struct hl_userptr *userptr;
362 u64 total_npages, npages, sg_start, sg_end;
366 spin_lock(&dev_entry->userptr_spinlock);
368 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
369 if (dev_entry->userptr_lookup >= userptr->addr &&
370 dev_entry->userptr_lookup < userptr->addr + userptr->size) {
372 for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents,
374 npages = hl_get_sg_info(sg, &dma_addr);
375 sg_start = userptr->addr +
376 total_npages * PAGE_SIZE;
377 sg_end = userptr->addr +
378 (total_npages + npages) * PAGE_SIZE;
380 if (dev_entry->userptr_lookup >= sg_start &&
381 dev_entry->userptr_lookup < sg_end) {
382 dma_addr += (dev_entry->userptr_lookup -
387 seq_puts(s, " user virtual address dma address pid region start region size\n");
388 seq_puts(s, "---------------------------------------------------------------------------------------\n");
390 seq_printf(s, " 0x%-18llx 0x%-16llx %-8u 0x%-16llx %-12llu\n",
391 dev_entry->userptr_lookup,
392 (u64)dma_addr, userptr->pid,
393 userptr->addr, userptr->size);
395 total_npages += npages;
400 spin_unlock(&dev_entry->userptr_spinlock);
408 static ssize_t userptr_lookup_write(struct file *file, const char __user *buf,
409 size_t count, loff_t *f_pos)
411 struct seq_file *s = file->private_data;
412 struct hl_debugfs_entry *entry = s->private;
413 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
417 rc = kstrtoull_from_user(buf, count, 16, &value);
421 dev_entry->userptr_lookup = value;
426 static int mmu_show(struct seq_file *s, void *data)
428 struct hl_debugfs_entry *entry = s->private;
429 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
430 struct hl_device *hdev = dev_entry->hdev;
432 struct hl_mmu_hop_info hops_info = {0};
433 u64 virt_addr = dev_entry->mmu_addr, phys_addr;
436 if (!hdev->mmu_enable)
439 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
440 ctx = hdev->kernel_ctx;
442 ctx = hl_get_compute_ctx(hdev);
445 dev_err(hdev->dev, "no ctx available\n");
449 if (hl_mmu_get_tlb_info(ctx, virt_addr, &hops_info)) {
450 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
455 hl_mmu_va_to_pa(ctx, virt_addr, &phys_addr);
457 if (hops_info.scrambled_vaddr &&
458 (dev_entry->mmu_addr != hops_info.scrambled_vaddr))
460 "asid: %u, virt_addr: 0x%llx, scrambled virt_addr: 0x%llx,\nphys_addr: 0x%llx, scrambled_phys_addr: 0x%llx\n",
461 dev_entry->mmu_asid, dev_entry->mmu_addr,
462 hops_info.scrambled_vaddr,
463 hops_info.unscrambled_paddr, phys_addr);
466 "asid: %u, virt_addr: 0x%llx, phys_addr: 0x%llx\n",
467 dev_entry->mmu_asid, dev_entry->mmu_addr, phys_addr);
469 for (i = 0 ; i < hops_info.used_hops ; i++) {
470 seq_printf(s, "hop%d_addr: 0x%llx\n",
471 i, hops_info.hop_info[i].hop_addr);
472 seq_printf(s, "hop%d_pte_addr: 0x%llx\n",
473 i, hops_info.hop_info[i].hop_pte_addr);
474 seq_printf(s, "hop%d_pte: 0x%llx\n",
475 i, hops_info.hop_info[i].hop_pte_val);
481 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
482 size_t count, loff_t *f_pos)
484 struct seq_file *s = file->private_data;
485 struct hl_debugfs_entry *entry = s->private;
486 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
487 struct hl_device *hdev = dev_entry->hdev;
488 char kbuf[MMU_KBUF_SIZE];
492 if (!hdev->mmu_enable)
495 if (count > sizeof(kbuf) - 1)
497 if (copy_from_user(kbuf, buf, count))
501 c = strchr(kbuf, ' ');
506 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
510 if (strncmp(c+1, "0x", 2))
512 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
519 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
524 static int engines_show(struct seq_file *s, void *data)
526 struct hl_debugfs_entry *entry = s->private;
527 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
528 struct hl_device *hdev = dev_entry->hdev;
530 if (hdev->reset_info.in_reset) {
531 dev_warn_ratelimited(hdev->dev,
532 "Can't check device idle during reset\n");
536 hdev->asic_funcs->is_device_idle(hdev, NULL, 0, s);
541 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
543 struct asic_fixed_properties *prop = &hdev->asic_prop;
545 if (!hdev->mmu_enable)
548 if (prop->dram_supports_virtual_memory &&
549 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
552 if (addr >= prop->pmmu.start_addr &&
553 addr < prop->pmmu.end_addr)
556 if (addr >= prop->pmmu_huge.start_addr &&
557 addr < prop->pmmu_huge.end_addr)
563 static bool hl_is_device_internal_memory_va(struct hl_device *hdev, u64 addr,
566 struct asic_fixed_properties *prop = &hdev->asic_prop;
567 u64 dram_start_addr, dram_end_addr;
569 if (!hdev->mmu_enable)
572 if (prop->dram_supports_virtual_memory) {
573 dram_start_addr = prop->dmmu.start_addr;
574 dram_end_addr = prop->dmmu.end_addr;
576 dram_start_addr = prop->dram_base_address;
577 dram_end_addr = prop->dram_end_address;
580 if (hl_mem_area_inside_range(addr, size, dram_start_addr,
584 if (hl_mem_area_inside_range(addr, size, prop->sram_base_address,
585 prop->sram_end_address))
591 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr, u32 size,
594 struct hl_vm_phys_pg_pack *phys_pg_pack;
596 struct hl_vm_hash_node *hnode;
597 u64 end_address, range_size;
598 struct hl_userptr *userptr;
599 enum vm_type *vm_type;
603 ctx = hl_get_compute_ctx(hdev);
606 dev_err(hdev->dev, "no ctx available\n");
610 /* Verify address is mapped */
611 mutex_lock(&ctx->mem_hash_lock);
612 hash_for_each(ctx->mem_hash, i, hnode, node) {
613 vm_type = hnode->ptr;
615 if (*vm_type == VM_TYPE_USERPTR) {
616 userptr = hnode->ptr;
617 range_size = userptr->size;
619 phys_pg_pack = hnode->ptr;
620 range_size = phys_pg_pack->total_size;
623 end_address = virt_addr + size;
624 if ((virt_addr >= hnode->vaddr) &&
625 (end_address <= hnode->vaddr + range_size)) {
630 mutex_unlock(&ctx->mem_hash_lock);
634 "virt addr 0x%llx is not mapped\n",
639 rc = hl_mmu_va_to_pa(ctx, virt_addr, phys_addr);
642 "virt addr 0x%llx is not mapped to phys addr\n",
650 static ssize_t hl_data_read32(struct file *f, char __user *buf,
651 size_t count, loff_t *ppos)
653 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
654 struct hl_device *hdev = entry->hdev;
655 u64 addr = entry->addr;
661 if (hdev->reset_info.in_reset) {
662 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
669 user_address = hl_is_device_va(hdev, addr);
671 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
676 rc = hdev->asic_funcs->debugfs_read32(hdev, addr, user_address, &val);
678 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
682 sprintf(tmp_buf, "0x%08x\n", val);
683 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
687 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
688 size_t count, loff_t *ppos)
690 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
691 struct hl_device *hdev = entry->hdev;
692 u64 addr = entry->addr;
697 if (hdev->reset_info.in_reset) {
698 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
702 rc = kstrtouint_from_user(buf, count, 16, &value);
706 user_address = hl_is_device_va(hdev, addr);
708 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
713 rc = hdev->asic_funcs->debugfs_write32(hdev, addr, user_address, value);
715 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
723 static ssize_t hl_data_read64(struct file *f, char __user *buf,
724 size_t count, loff_t *ppos)
726 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
727 struct hl_device *hdev = entry->hdev;
728 u64 addr = entry->addr;
734 if (hdev->reset_info.in_reset) {
735 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
742 user_address = hl_is_device_va(hdev, addr);
744 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
749 rc = hdev->asic_funcs->debugfs_read64(hdev, addr, user_address, &val);
751 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
755 sprintf(tmp_buf, "0x%016llx\n", val);
756 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
760 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
761 size_t count, loff_t *ppos)
763 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
764 struct hl_device *hdev = entry->hdev;
765 u64 addr = entry->addr;
770 if (hdev->reset_info.in_reset) {
771 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
775 rc = kstrtoull_from_user(buf, count, 16, &value);
779 user_address = hl_is_device_va(hdev, addr);
781 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
786 rc = hdev->asic_funcs->debugfs_write64(hdev, addr, user_address, value);
788 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
796 static ssize_t hl_dma_size_write(struct file *f, const char __user *buf,
797 size_t count, loff_t *ppos)
799 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
800 struct hl_device *hdev = entry->hdev;
801 u64 addr = entry->addr;
805 if (hdev->reset_info.in_reset) {
806 dev_warn_ratelimited(hdev->dev, "Can't DMA during reset\n");
809 rc = kstrtouint_from_user(buf, count, 16, &size);
814 dev_err(hdev->dev, "DMA read failed. size can't be 0\n");
818 if (size > SZ_128M) {
820 "DMA read failed. size can't be larger than 128MB\n");
824 if (!hl_is_device_internal_memory_va(hdev, addr, size)) {
826 "DMA read failed. Invalid 0x%010llx + 0x%08x\n",
831 /* Free the previous allocation, if there was any */
832 entry->blob_desc.size = 0;
833 vfree(entry->blob_desc.data);
835 entry->blob_desc.data = vmalloc(size);
836 if (!entry->blob_desc.data)
839 rc = hdev->asic_funcs->debugfs_read_dma(hdev, addr, size,
840 entry->blob_desc.data);
842 dev_err(hdev->dev, "Failed to DMA from 0x%010llx\n", addr);
843 vfree(entry->blob_desc.data);
844 entry->blob_desc.data = NULL;
848 entry->blob_desc.size = size;
853 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
854 size_t count, loff_t *ppos)
856 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
857 struct hl_device *hdev = entry->hdev;
864 if (hdev->pdev->current_state == PCI_D0)
866 else if (hdev->pdev->current_state == PCI_D3hot)
872 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
873 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
877 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
878 size_t count, loff_t *ppos)
880 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
881 struct hl_device *hdev = entry->hdev;
885 rc = kstrtouint_from_user(buf, count, 10, &value);
890 pci_set_power_state(hdev->pdev, PCI_D0);
891 pci_restore_state(hdev->pdev);
892 rc = pci_enable_device(hdev->pdev);
893 } else if (value == 2) {
894 pci_save_state(hdev->pdev);
895 pci_disable_device(hdev->pdev);
896 pci_set_power_state(hdev->pdev, PCI_D3hot);
898 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
905 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
906 size_t count, loff_t *ppos)
908 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
909 struct hl_device *hdev = entry->hdev;
917 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
918 entry->i2c_reg, entry->i2c_len, &val);
921 "Failed to read from I2C bus %d, addr %d, reg %d, len %d\n",
922 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg, entry->i2c_len);
926 sprintf(tmp_buf, "%#02llx\n", val);
927 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
933 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
934 size_t count, loff_t *ppos)
936 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
937 struct hl_device *hdev = entry->hdev;
941 rc = kstrtou64_from_user(buf, count, 16, &value);
945 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
946 entry->i2c_reg, entry->i2c_len, value);
949 "Failed to write %#02llx to I2C bus %d, addr %d, reg %d, len %d\n",
950 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg, entry->i2c_len);
957 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
958 size_t count, loff_t *ppos)
960 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
961 struct hl_device *hdev = entry->hdev;
965 rc = kstrtouint_from_user(buf, count, 10, &value);
969 value = value ? 1 : 0;
971 hl_debugfs_led_set(hdev, 0, value);
976 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
977 size_t count, loff_t *ppos)
979 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
980 struct hl_device *hdev = entry->hdev;
984 rc = kstrtouint_from_user(buf, count, 10, &value);
988 value = value ? 1 : 0;
990 hl_debugfs_led_set(hdev, 1, value);
995 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
996 size_t count, loff_t *ppos)
998 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
999 struct hl_device *hdev = entry->hdev;
1003 rc = kstrtouint_from_user(buf, count, 10, &value);
1007 value = value ? 1 : 0;
1009 hl_debugfs_led_set(hdev, 2, value);
1014 static ssize_t hl_device_read(struct file *f, char __user *buf,
1015 size_t count, loff_t *ppos)
1017 static const char *help =
1018 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
1019 return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
1022 static ssize_t hl_device_write(struct file *f, const char __user *buf,
1023 size_t count, loff_t *ppos)
1025 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1026 struct hl_device *hdev = entry->hdev;
1027 char data[30] = {0};
1029 /* don't allow partial writes */
1033 simple_write_to_buffer(data, 29, ppos, buf, count);
1035 if (strncmp("disable", data, strlen("disable")) == 0) {
1036 hdev->disabled = true;
1037 } else if (strncmp("enable", data, strlen("enable")) == 0) {
1038 hdev->disabled = false;
1039 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
1040 hdev->asic_funcs->suspend(hdev);
1041 } else if (strncmp("resume", data, strlen("resume")) == 0) {
1042 hdev->asic_funcs->resume(hdev);
1043 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
1044 hdev->device_cpu_disabled = true;
1047 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
1054 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
1055 size_t count, loff_t *ppos)
1060 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1061 size_t count, loff_t *ppos)
1066 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1067 size_t count, loff_t *ppos)
1069 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1070 struct hl_device *hdev = entry->hdev;
1077 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1078 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1079 strlen(tmp_buf) + 1);
1084 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1085 size_t count, loff_t *ppos)
1087 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1088 struct hl_device *hdev = entry->hdev;
1092 if (hdev->reset_info.in_reset) {
1093 dev_warn_ratelimited(hdev->dev,
1094 "Can't change stop on error during reset\n");
1098 rc = kstrtouint_from_user(buf, count, 10, &value);
1102 hdev->stop_on_err = value ? 1 : 0;
1104 hl_device_reset(hdev, 0);
1109 static ssize_t hl_security_violations_read(struct file *f, char __user *buf,
1110 size_t count, loff_t *ppos)
1112 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1113 struct hl_device *hdev = entry->hdev;
1115 hdev->asic_funcs->ack_protection_bits_errors(hdev);
1120 static ssize_t hl_state_dump_read(struct file *f, char __user *buf,
1121 size_t count, loff_t *ppos)
1123 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1126 down_read(&entry->state_dump_sem);
1127 if (!entry->state_dump[entry->state_dump_head])
1130 rc = simple_read_from_buffer(
1132 entry->state_dump[entry->state_dump_head],
1133 strlen(entry->state_dump[entry->state_dump_head]));
1134 up_read(&entry->state_dump_sem);
1139 static ssize_t hl_state_dump_write(struct file *f, const char __user *buf,
1140 size_t count, loff_t *ppos)
1142 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1143 struct hl_device *hdev = entry->hdev;
1148 rc = kstrtouint_from_user(buf, count, 10, &size);
1152 if (size <= 0 || size >= ARRAY_SIZE(entry->state_dump)) {
1153 dev_err(hdev->dev, "Invalid number of dumps to skip\n");
1157 if (entry->state_dump[entry->state_dump_head]) {
1158 down_write(&entry->state_dump_sem);
1159 for (i = 0; i < size; ++i) {
1160 vfree(entry->state_dump[entry->state_dump_head]);
1161 entry->state_dump[entry->state_dump_head] = NULL;
1162 if (entry->state_dump_head > 0)
1163 entry->state_dump_head--;
1165 entry->state_dump_head =
1166 ARRAY_SIZE(entry->state_dump) - 1;
1168 up_write(&entry->state_dump_sem);
1174 static ssize_t hl_timeout_locked_read(struct file *f, char __user *buf,
1175 size_t count, loff_t *ppos)
1177 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1178 struct hl_device *hdev = entry->hdev;
1185 sprintf(tmp_buf, "%d\n",
1186 jiffies_to_msecs(hdev->timeout_jiffies) / 1000);
1187 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1188 strlen(tmp_buf) + 1);
1193 static ssize_t hl_timeout_locked_write(struct file *f, const char __user *buf,
1194 size_t count, loff_t *ppos)
1196 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1197 struct hl_device *hdev = entry->hdev;
1201 rc = kstrtouint_from_user(buf, count, 10, &value);
1206 hdev->timeout_jiffies = msecs_to_jiffies(value * 1000);
1208 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
1213 static const struct file_operations hl_data32b_fops = {
1214 .owner = THIS_MODULE,
1215 .read = hl_data_read32,
1216 .write = hl_data_write32
1219 static const struct file_operations hl_data64b_fops = {
1220 .owner = THIS_MODULE,
1221 .read = hl_data_read64,
1222 .write = hl_data_write64
1225 static const struct file_operations hl_dma_size_fops = {
1226 .owner = THIS_MODULE,
1227 .write = hl_dma_size_write
1230 static const struct file_operations hl_i2c_data_fops = {
1231 .owner = THIS_MODULE,
1232 .read = hl_i2c_data_read,
1233 .write = hl_i2c_data_write
1236 static const struct file_operations hl_power_fops = {
1237 .owner = THIS_MODULE,
1238 .read = hl_get_power_state,
1239 .write = hl_set_power_state
1242 static const struct file_operations hl_led0_fops = {
1243 .owner = THIS_MODULE,
1244 .write = hl_led0_write
1247 static const struct file_operations hl_led1_fops = {
1248 .owner = THIS_MODULE,
1249 .write = hl_led1_write
1252 static const struct file_operations hl_led2_fops = {
1253 .owner = THIS_MODULE,
1254 .write = hl_led2_write
1257 static const struct file_operations hl_device_fops = {
1258 .owner = THIS_MODULE,
1259 .read = hl_device_read,
1260 .write = hl_device_write
1263 static const struct file_operations hl_clk_gate_fops = {
1264 .owner = THIS_MODULE,
1265 .read = hl_clk_gate_read,
1266 .write = hl_clk_gate_write
1269 static const struct file_operations hl_stop_on_err_fops = {
1270 .owner = THIS_MODULE,
1271 .read = hl_stop_on_err_read,
1272 .write = hl_stop_on_err_write
1275 static const struct file_operations hl_security_violations_fops = {
1276 .owner = THIS_MODULE,
1277 .read = hl_security_violations_read
1280 static const struct file_operations hl_state_dump_fops = {
1281 .owner = THIS_MODULE,
1282 .read = hl_state_dump_read,
1283 .write = hl_state_dump_write
1286 static const struct file_operations hl_timeout_locked_fops = {
1287 .owner = THIS_MODULE,
1288 .read = hl_timeout_locked_read,
1289 .write = hl_timeout_locked_write
1292 static const struct hl_info_list hl_debugfs_list[] = {
1293 {"command_buffers", command_buffers_show, NULL},
1294 {"command_submission", command_submission_show, NULL},
1295 {"command_submission_jobs", command_submission_jobs_show, NULL},
1296 {"userptr", userptr_show, NULL},
1297 {"vm", vm_show, NULL},
1298 {"userptr_lookup", userptr_lookup_show, userptr_lookup_write},
1299 {"mmu", mmu_show, mmu_asid_va_write},
1300 {"engines", engines_show, NULL}
1303 static int hl_debugfs_open(struct inode *inode, struct file *file)
1305 struct hl_debugfs_entry *node = inode->i_private;
1307 return single_open(file, node->info_ent->show, node);
1310 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1311 size_t count, loff_t *f_pos)
1313 struct hl_debugfs_entry *node = file->f_inode->i_private;
1315 if (node->info_ent->write)
1316 return node->info_ent->write(file, buf, count, f_pos);
1322 static const struct file_operations hl_debugfs_fops = {
1323 .owner = THIS_MODULE,
1324 .open = hl_debugfs_open,
1326 .write = hl_debugfs_write,
1327 .llseek = seq_lseek,
1328 .release = single_release,
1331 void hl_debugfs_add_device(struct hl_device *hdev)
1333 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1334 int count = ARRAY_SIZE(hl_debugfs_list);
1335 struct hl_debugfs_entry *entry;
1338 dev_entry->hdev = hdev;
1339 dev_entry->entry_arr = kmalloc_array(count,
1340 sizeof(struct hl_debugfs_entry),
1342 if (!dev_entry->entry_arr)
1345 dev_entry->blob_desc.size = 0;
1346 dev_entry->blob_desc.data = NULL;
1348 INIT_LIST_HEAD(&dev_entry->file_list);
1349 INIT_LIST_HEAD(&dev_entry->cb_list);
1350 INIT_LIST_HEAD(&dev_entry->cs_list);
1351 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1352 INIT_LIST_HEAD(&dev_entry->userptr_list);
1353 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1354 mutex_init(&dev_entry->file_mutex);
1355 init_rwsem(&dev_entry->state_dump_sem);
1356 spin_lock_init(&dev_entry->cb_spinlock);
1357 spin_lock_init(&dev_entry->cs_spinlock);
1358 spin_lock_init(&dev_entry->cs_job_spinlock);
1359 spin_lock_init(&dev_entry->userptr_spinlock);
1360 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1362 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1365 debugfs_create_x64("addr",
1370 debugfs_create_file("data32",
1376 debugfs_create_file("data64",
1382 debugfs_create_file("set_power_state",
1388 debugfs_create_u8("i2c_bus",
1391 &dev_entry->i2c_bus);
1393 debugfs_create_u8("i2c_addr",
1396 &dev_entry->i2c_addr);
1398 debugfs_create_u8("i2c_reg",
1401 &dev_entry->i2c_reg);
1403 debugfs_create_u8("i2c_len",
1406 &dev_entry->i2c_len);
1408 debugfs_create_file("i2c_data",
1414 debugfs_create_file("led0",
1420 debugfs_create_file("led1",
1426 debugfs_create_file("led2",
1432 debugfs_create_file("device",
1438 debugfs_create_file("clk_gate",
1444 debugfs_create_file("stop_on_err",
1448 &hl_stop_on_err_fops);
1450 debugfs_create_file("dump_security_violations",
1454 &hl_security_violations_fops);
1456 debugfs_create_file("dma_size",
1462 debugfs_create_blob("data_dma",
1465 &dev_entry->blob_desc);
1467 debugfs_create_x8("skip_reset_on_timeout",
1470 &hdev->reset_info.skip_reset_on_timeout);
1472 debugfs_create_file("state_dump",
1476 &hl_state_dump_fops);
1478 debugfs_create_file("timeout_locked",
1482 &hl_timeout_locked_fops);
1484 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1485 debugfs_create_file(hl_debugfs_list[i].name,
1490 entry->info_ent = &hl_debugfs_list[i];
1491 entry->dev_entry = dev_entry;
1495 void hl_debugfs_remove_device(struct hl_device *hdev)
1497 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1500 debugfs_remove_recursive(entry->root);
1502 mutex_destroy(&entry->file_mutex);
1504 vfree(entry->blob_desc.data);
1506 for (i = 0; i < ARRAY_SIZE(entry->state_dump); ++i)
1507 vfree(entry->state_dump[i]);
1509 kfree(entry->entry_arr);
1512 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1514 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1516 mutex_lock(&dev_entry->file_mutex);
1517 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1518 mutex_unlock(&dev_entry->file_mutex);
1521 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1523 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1525 mutex_lock(&dev_entry->file_mutex);
1526 list_del(&hpriv->debugfs_list);
1527 mutex_unlock(&dev_entry->file_mutex);
1530 void hl_debugfs_add_cb(struct hl_cb *cb)
1532 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1534 spin_lock(&dev_entry->cb_spinlock);
1535 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1536 spin_unlock(&dev_entry->cb_spinlock);
1539 void hl_debugfs_remove_cb(struct hl_cb *cb)
1541 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1543 spin_lock(&dev_entry->cb_spinlock);
1544 list_del(&cb->debugfs_list);
1545 spin_unlock(&dev_entry->cb_spinlock);
1548 void hl_debugfs_add_cs(struct hl_cs *cs)
1550 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1552 spin_lock(&dev_entry->cs_spinlock);
1553 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1554 spin_unlock(&dev_entry->cs_spinlock);
1557 void hl_debugfs_remove_cs(struct hl_cs *cs)
1559 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1561 spin_lock(&dev_entry->cs_spinlock);
1562 list_del(&cs->debugfs_list);
1563 spin_unlock(&dev_entry->cs_spinlock);
1566 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1568 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1570 spin_lock(&dev_entry->cs_job_spinlock);
1571 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1572 spin_unlock(&dev_entry->cs_job_spinlock);
1575 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1577 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1579 spin_lock(&dev_entry->cs_job_spinlock);
1580 list_del(&job->debugfs_list);
1581 spin_unlock(&dev_entry->cs_job_spinlock);
1584 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1586 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1588 spin_lock(&dev_entry->userptr_spinlock);
1589 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1590 spin_unlock(&dev_entry->userptr_spinlock);
1593 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1594 struct hl_userptr *userptr)
1596 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1598 spin_lock(&dev_entry->userptr_spinlock);
1599 list_del(&userptr->debugfs_list);
1600 spin_unlock(&dev_entry->userptr_spinlock);
1603 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1605 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1607 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1608 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1609 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1612 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1614 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1616 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1617 list_del(&ctx->debugfs_list);
1618 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1622 * hl_debugfs_set_state_dump - register state dump making it accessible via
1624 * @hdev: pointer to the device structure
1625 * @data: the actual dump data
1626 * @length: the length of the data
1628 void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
1629 unsigned long length)
1631 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1633 down_write(&dev_entry->state_dump_sem);
1635 dev_entry->state_dump_head = (dev_entry->state_dump_head + 1) %
1636 ARRAY_SIZE(dev_entry->state_dump);
1637 vfree(dev_entry->state_dump[dev_entry->state_dump_head]);
1638 dev_entry->state_dump[dev_entry->state_dump_head] = data;
1640 up_write(&dev_entry->state_dump_sem);
1643 void __init hl_debugfs_init(void)
1645 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1648 void hl_debugfs_fini(void)
1650 debugfs_remove_recursive(hl_debug_root);