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)
1057 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1058 struct hl_device *hdev = entry->hdev;
1065 sprintf(tmp_buf, "0x%llx\n", hdev->clock_gating_mask);
1066 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
1067 strlen(tmp_buf) + 1);
1072 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1073 size_t count, loff_t *ppos)
1075 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1076 struct hl_device *hdev = entry->hdev;
1080 if (hdev->reset_info.in_reset) {
1081 dev_warn_ratelimited(hdev->dev,
1082 "Can't change clock gating during reset\n");
1086 rc = kstrtoull_from_user(buf, count, 16, &value);
1090 hdev->clock_gating_mask = value;
1091 hdev->asic_funcs->set_clock_gating(hdev);
1096 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1097 size_t count, loff_t *ppos)
1099 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1100 struct hl_device *hdev = entry->hdev;
1107 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1108 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1109 strlen(tmp_buf) + 1);
1114 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1115 size_t count, loff_t *ppos)
1117 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1118 struct hl_device *hdev = entry->hdev;
1122 if (hdev->reset_info.in_reset) {
1123 dev_warn_ratelimited(hdev->dev,
1124 "Can't change stop on error during reset\n");
1128 rc = kstrtouint_from_user(buf, count, 10, &value);
1132 hdev->stop_on_err = value ? 1 : 0;
1134 hl_device_reset(hdev, 0);
1139 static ssize_t hl_security_violations_read(struct file *f, 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;
1145 hdev->asic_funcs->ack_protection_bits_errors(hdev);
1150 static ssize_t hl_state_dump_read(struct file *f, char __user *buf,
1151 size_t count, loff_t *ppos)
1153 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1156 down_read(&entry->state_dump_sem);
1157 if (!entry->state_dump[entry->state_dump_head])
1160 rc = simple_read_from_buffer(
1162 entry->state_dump[entry->state_dump_head],
1163 strlen(entry->state_dump[entry->state_dump_head]));
1164 up_read(&entry->state_dump_sem);
1169 static ssize_t hl_state_dump_write(struct file *f, const char __user *buf,
1170 size_t count, loff_t *ppos)
1172 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1173 struct hl_device *hdev = entry->hdev;
1178 rc = kstrtouint_from_user(buf, count, 10, &size);
1182 if (size <= 0 || size >= ARRAY_SIZE(entry->state_dump)) {
1183 dev_err(hdev->dev, "Invalid number of dumps to skip\n");
1187 if (entry->state_dump[entry->state_dump_head]) {
1188 down_write(&entry->state_dump_sem);
1189 for (i = 0; i < size; ++i) {
1190 vfree(entry->state_dump[entry->state_dump_head]);
1191 entry->state_dump[entry->state_dump_head] = NULL;
1192 if (entry->state_dump_head > 0)
1193 entry->state_dump_head--;
1195 entry->state_dump_head =
1196 ARRAY_SIZE(entry->state_dump) - 1;
1198 up_write(&entry->state_dump_sem);
1204 static ssize_t hl_timeout_locked_read(struct file *f, char __user *buf,
1205 size_t count, loff_t *ppos)
1207 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1208 struct hl_device *hdev = entry->hdev;
1215 sprintf(tmp_buf, "%d\n",
1216 jiffies_to_msecs(hdev->timeout_jiffies) / 1000);
1217 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1218 strlen(tmp_buf) + 1);
1223 static ssize_t hl_timeout_locked_write(struct file *f, const char __user *buf,
1224 size_t count, loff_t *ppos)
1226 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1227 struct hl_device *hdev = entry->hdev;
1231 rc = kstrtouint_from_user(buf, count, 10, &value);
1236 hdev->timeout_jiffies = msecs_to_jiffies(value * 1000);
1238 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
1243 static const struct file_operations hl_data32b_fops = {
1244 .owner = THIS_MODULE,
1245 .read = hl_data_read32,
1246 .write = hl_data_write32
1249 static const struct file_operations hl_data64b_fops = {
1250 .owner = THIS_MODULE,
1251 .read = hl_data_read64,
1252 .write = hl_data_write64
1255 static const struct file_operations hl_dma_size_fops = {
1256 .owner = THIS_MODULE,
1257 .write = hl_dma_size_write
1260 static const struct file_operations hl_i2c_data_fops = {
1261 .owner = THIS_MODULE,
1262 .read = hl_i2c_data_read,
1263 .write = hl_i2c_data_write
1266 static const struct file_operations hl_power_fops = {
1267 .owner = THIS_MODULE,
1268 .read = hl_get_power_state,
1269 .write = hl_set_power_state
1272 static const struct file_operations hl_led0_fops = {
1273 .owner = THIS_MODULE,
1274 .write = hl_led0_write
1277 static const struct file_operations hl_led1_fops = {
1278 .owner = THIS_MODULE,
1279 .write = hl_led1_write
1282 static const struct file_operations hl_led2_fops = {
1283 .owner = THIS_MODULE,
1284 .write = hl_led2_write
1287 static const struct file_operations hl_device_fops = {
1288 .owner = THIS_MODULE,
1289 .read = hl_device_read,
1290 .write = hl_device_write
1293 static const struct file_operations hl_clk_gate_fops = {
1294 .owner = THIS_MODULE,
1295 .read = hl_clk_gate_read,
1296 .write = hl_clk_gate_write
1299 static const struct file_operations hl_stop_on_err_fops = {
1300 .owner = THIS_MODULE,
1301 .read = hl_stop_on_err_read,
1302 .write = hl_stop_on_err_write
1305 static const struct file_operations hl_security_violations_fops = {
1306 .owner = THIS_MODULE,
1307 .read = hl_security_violations_read
1310 static const struct file_operations hl_state_dump_fops = {
1311 .owner = THIS_MODULE,
1312 .read = hl_state_dump_read,
1313 .write = hl_state_dump_write
1316 static const struct file_operations hl_timeout_locked_fops = {
1317 .owner = THIS_MODULE,
1318 .read = hl_timeout_locked_read,
1319 .write = hl_timeout_locked_write
1322 static const struct hl_info_list hl_debugfs_list[] = {
1323 {"command_buffers", command_buffers_show, NULL},
1324 {"command_submission", command_submission_show, NULL},
1325 {"command_submission_jobs", command_submission_jobs_show, NULL},
1326 {"userptr", userptr_show, NULL},
1327 {"vm", vm_show, NULL},
1328 {"userptr_lookup", userptr_lookup_show, userptr_lookup_write},
1329 {"mmu", mmu_show, mmu_asid_va_write},
1330 {"engines", engines_show, NULL}
1333 static int hl_debugfs_open(struct inode *inode, struct file *file)
1335 struct hl_debugfs_entry *node = inode->i_private;
1337 return single_open(file, node->info_ent->show, node);
1340 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1341 size_t count, loff_t *f_pos)
1343 struct hl_debugfs_entry *node = file->f_inode->i_private;
1345 if (node->info_ent->write)
1346 return node->info_ent->write(file, buf, count, f_pos);
1352 static const struct file_operations hl_debugfs_fops = {
1353 .owner = THIS_MODULE,
1354 .open = hl_debugfs_open,
1356 .write = hl_debugfs_write,
1357 .llseek = seq_lseek,
1358 .release = single_release,
1361 void hl_debugfs_add_device(struct hl_device *hdev)
1363 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1364 int count = ARRAY_SIZE(hl_debugfs_list);
1365 struct hl_debugfs_entry *entry;
1368 dev_entry->hdev = hdev;
1369 dev_entry->entry_arr = kmalloc_array(count,
1370 sizeof(struct hl_debugfs_entry),
1372 if (!dev_entry->entry_arr)
1375 dev_entry->blob_desc.size = 0;
1376 dev_entry->blob_desc.data = NULL;
1378 INIT_LIST_HEAD(&dev_entry->file_list);
1379 INIT_LIST_HEAD(&dev_entry->cb_list);
1380 INIT_LIST_HEAD(&dev_entry->cs_list);
1381 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1382 INIT_LIST_HEAD(&dev_entry->userptr_list);
1383 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1384 mutex_init(&dev_entry->file_mutex);
1385 init_rwsem(&dev_entry->state_dump_sem);
1386 spin_lock_init(&dev_entry->cb_spinlock);
1387 spin_lock_init(&dev_entry->cs_spinlock);
1388 spin_lock_init(&dev_entry->cs_job_spinlock);
1389 spin_lock_init(&dev_entry->userptr_spinlock);
1390 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1392 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1395 debugfs_create_x64("addr",
1400 debugfs_create_file("data32",
1406 debugfs_create_file("data64",
1412 debugfs_create_file("set_power_state",
1418 debugfs_create_u8("i2c_bus",
1421 &dev_entry->i2c_bus);
1423 debugfs_create_u8("i2c_addr",
1426 &dev_entry->i2c_addr);
1428 debugfs_create_u8("i2c_reg",
1431 &dev_entry->i2c_reg);
1433 debugfs_create_u8("i2c_len",
1436 &dev_entry->i2c_len);
1438 debugfs_create_file("i2c_data",
1444 debugfs_create_file("led0",
1450 debugfs_create_file("led1",
1456 debugfs_create_file("led2",
1462 debugfs_create_file("device",
1468 debugfs_create_file("clk_gate",
1474 debugfs_create_file("stop_on_err",
1478 &hl_stop_on_err_fops);
1480 debugfs_create_file("dump_security_violations",
1484 &hl_security_violations_fops);
1486 debugfs_create_file("dma_size",
1492 debugfs_create_blob("data_dma",
1495 &dev_entry->blob_desc);
1497 debugfs_create_x8("skip_reset_on_timeout",
1500 &hdev->reset_info.skip_reset_on_timeout);
1502 debugfs_create_file("state_dump",
1506 &hl_state_dump_fops);
1508 debugfs_create_file("timeout_locked",
1512 &hl_timeout_locked_fops);
1514 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1515 debugfs_create_file(hl_debugfs_list[i].name,
1520 entry->info_ent = &hl_debugfs_list[i];
1521 entry->dev_entry = dev_entry;
1525 void hl_debugfs_remove_device(struct hl_device *hdev)
1527 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1530 debugfs_remove_recursive(entry->root);
1532 mutex_destroy(&entry->file_mutex);
1534 vfree(entry->blob_desc.data);
1536 for (i = 0; i < ARRAY_SIZE(entry->state_dump); ++i)
1537 vfree(entry->state_dump[i]);
1539 kfree(entry->entry_arr);
1542 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1544 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1546 mutex_lock(&dev_entry->file_mutex);
1547 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1548 mutex_unlock(&dev_entry->file_mutex);
1551 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1553 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1555 mutex_lock(&dev_entry->file_mutex);
1556 list_del(&hpriv->debugfs_list);
1557 mutex_unlock(&dev_entry->file_mutex);
1560 void hl_debugfs_add_cb(struct hl_cb *cb)
1562 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1564 spin_lock(&dev_entry->cb_spinlock);
1565 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1566 spin_unlock(&dev_entry->cb_spinlock);
1569 void hl_debugfs_remove_cb(struct hl_cb *cb)
1571 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1573 spin_lock(&dev_entry->cb_spinlock);
1574 list_del(&cb->debugfs_list);
1575 spin_unlock(&dev_entry->cb_spinlock);
1578 void hl_debugfs_add_cs(struct hl_cs *cs)
1580 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1582 spin_lock(&dev_entry->cs_spinlock);
1583 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1584 spin_unlock(&dev_entry->cs_spinlock);
1587 void hl_debugfs_remove_cs(struct hl_cs *cs)
1589 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1591 spin_lock(&dev_entry->cs_spinlock);
1592 list_del(&cs->debugfs_list);
1593 spin_unlock(&dev_entry->cs_spinlock);
1596 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1598 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1600 spin_lock(&dev_entry->cs_job_spinlock);
1601 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1602 spin_unlock(&dev_entry->cs_job_spinlock);
1605 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1607 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1609 spin_lock(&dev_entry->cs_job_spinlock);
1610 list_del(&job->debugfs_list);
1611 spin_unlock(&dev_entry->cs_job_spinlock);
1614 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1616 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1618 spin_lock(&dev_entry->userptr_spinlock);
1619 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1620 spin_unlock(&dev_entry->userptr_spinlock);
1623 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1624 struct hl_userptr *userptr)
1626 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1628 spin_lock(&dev_entry->userptr_spinlock);
1629 list_del(&userptr->debugfs_list);
1630 spin_unlock(&dev_entry->userptr_spinlock);
1633 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1635 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1637 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1638 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1639 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1642 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1644 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1646 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1647 list_del(&ctx->debugfs_list);
1648 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1652 * hl_debugfs_set_state_dump - register state dump making it accessible via
1654 * @hdev: pointer to the device structure
1655 * @data: the actual dump data
1656 * @length: the length of the data
1658 void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
1659 unsigned long length)
1661 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1663 down_write(&dev_entry->state_dump_sem);
1665 dev_entry->state_dump_head = (dev_entry->state_dump_head + 1) %
1666 ARRAY_SIZE(dev_entry->state_dump);
1667 vfree(dev_entry->state_dump[dev_entry->state_dump_head]);
1668 dev_entry->state_dump[dev_entry->state_dump_head] = data;
1670 up_write(&dev_entry->state_dump_sem);
1673 void __init hl_debugfs_init(void)
1675 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1678 void hl_debugfs_fini(void)
1680 debugfs_remove_recursive(hl_debug_root);