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, " user virtual address size dma dir\n");
213 seq_puts(s, "----------------------------------------------------------\n");
216 " 0x%-14llx %-10u %-30s\n",
217 userptr->addr, userptr->size, 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_t *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 %-10u\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 mmu_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 hl_device *hdev = dev_entry->hdev;
329 struct hl_mmu_hop_info hops_info = {0};
330 u64 virt_addr = dev_entry->mmu_addr, phys_addr;
333 if (!hdev->mmu_enable)
336 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
337 ctx = hdev->kernel_ctx;
339 ctx = hdev->compute_ctx;
342 dev_err(hdev->dev, "no ctx available\n");
346 if (hl_mmu_get_tlb_info(ctx, virt_addr, &hops_info)) {
347 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
352 phys_addr = hops_info.hop_info[hops_info.used_hops - 1].hop_pte_val;
354 if (hops_info.scrambled_vaddr &&
355 (dev_entry->mmu_addr != hops_info.scrambled_vaddr))
357 "asid: %u, virt_addr: 0x%llx, scrambled virt_addr: 0x%llx,\nphys_addr: 0x%llx, scrambled_phys_addr: 0x%llx\n",
358 dev_entry->mmu_asid, dev_entry->mmu_addr,
359 hops_info.scrambled_vaddr,
360 hops_info.unscrambled_paddr, phys_addr);
363 "asid: %u, virt_addr: 0x%llx, phys_addr: 0x%llx\n",
364 dev_entry->mmu_asid, dev_entry->mmu_addr, phys_addr);
366 for (i = 0 ; i < hops_info.used_hops ; i++) {
367 seq_printf(s, "hop%d_addr: 0x%llx\n",
368 i, hops_info.hop_info[i].hop_addr);
369 seq_printf(s, "hop%d_pte_addr: 0x%llx\n",
370 i, hops_info.hop_info[i].hop_pte_addr);
371 seq_printf(s, "hop%d_pte: 0x%llx\n",
372 i, hops_info.hop_info[i].hop_pte_val);
378 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
379 size_t count, loff_t *f_pos)
381 struct seq_file *s = file->private_data;
382 struct hl_debugfs_entry *entry = s->private;
383 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
384 struct hl_device *hdev = dev_entry->hdev;
385 char kbuf[MMU_KBUF_SIZE];
389 if (!hdev->mmu_enable)
392 if (count > sizeof(kbuf) - 1)
394 if (copy_from_user(kbuf, buf, count))
398 c = strchr(kbuf, ' ');
403 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
407 if (strncmp(c+1, "0x", 2))
409 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
416 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
421 static int engines_show(struct seq_file *s, void *data)
423 struct hl_debugfs_entry *entry = s->private;
424 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
425 struct hl_device *hdev = dev_entry->hdev;
427 if (atomic_read(&hdev->in_reset)) {
428 dev_warn_ratelimited(hdev->dev,
429 "Can't check device idle during reset\n");
433 hdev->asic_funcs->is_device_idle(hdev, NULL, 0, s);
438 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
440 struct asic_fixed_properties *prop = &hdev->asic_prop;
442 if (!hdev->mmu_enable)
445 if (prop->dram_supports_virtual_memory &&
446 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
449 if (addr >= prop->pmmu.start_addr &&
450 addr < prop->pmmu.end_addr)
453 if (addr >= prop->pmmu_huge.start_addr &&
454 addr < prop->pmmu_huge.end_addr)
460 static bool hl_is_device_internal_memory_va(struct hl_device *hdev, u64 addr,
463 struct asic_fixed_properties *prop = &hdev->asic_prop;
464 u64 dram_start_addr, dram_end_addr;
466 if (!hdev->mmu_enable)
469 if (prop->dram_supports_virtual_memory) {
470 dram_start_addr = prop->dmmu.start_addr;
471 dram_end_addr = prop->dmmu.end_addr;
473 dram_start_addr = prop->dram_base_address;
474 dram_end_addr = prop->dram_end_address;
477 if (hl_mem_area_inside_range(addr, size, dram_start_addr,
481 if (hl_mem_area_inside_range(addr, size, prop->sram_base_address,
482 prop->sram_end_address))
488 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr, u32 size,
491 struct hl_vm_phys_pg_pack *phys_pg_pack;
492 struct hl_ctx *ctx = hdev->compute_ctx;
493 struct hl_vm_hash_node *hnode;
494 struct hl_userptr *userptr;
495 enum vm_type_t *vm_type;
502 dev_err(hdev->dev, "no ctx available\n");
506 /* Verify address is mapped */
507 mutex_lock(&ctx->mem_hash_lock);
508 hash_for_each(ctx->mem_hash, i, hnode, node) {
509 vm_type = hnode->ptr;
511 if (*vm_type == VM_TYPE_USERPTR) {
512 userptr = hnode->ptr;
513 range_size = userptr->size;
515 phys_pg_pack = hnode->ptr;
516 range_size = phys_pg_pack->total_size;
519 end_address = virt_addr + size;
520 if ((virt_addr >= hnode->vaddr) &&
521 (end_address <= hnode->vaddr + range_size)) {
526 mutex_unlock(&ctx->mem_hash_lock);
530 "virt addr 0x%llx is not mapped\n",
535 rc = hl_mmu_va_to_pa(ctx, virt_addr, phys_addr);
538 "virt addr 0x%llx is not mapped to phys addr\n",
546 static ssize_t hl_data_read32(struct file *f, char __user *buf,
547 size_t count, loff_t *ppos)
549 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
550 struct hl_device *hdev = entry->hdev;
551 u64 addr = entry->addr;
557 if (atomic_read(&hdev->in_reset)) {
558 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
565 user_address = hl_is_device_va(hdev, addr);
567 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
572 rc = hdev->asic_funcs->debugfs_read32(hdev, addr, user_address, &val);
574 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
578 sprintf(tmp_buf, "0x%08x\n", val);
579 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
583 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
584 size_t count, loff_t *ppos)
586 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
587 struct hl_device *hdev = entry->hdev;
588 u64 addr = entry->addr;
593 if (atomic_read(&hdev->in_reset)) {
594 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
598 rc = kstrtouint_from_user(buf, count, 16, &value);
602 user_address = hl_is_device_va(hdev, addr);
604 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
609 rc = hdev->asic_funcs->debugfs_write32(hdev, addr, user_address, value);
611 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
619 static ssize_t hl_data_read64(struct file *f, char __user *buf,
620 size_t count, loff_t *ppos)
622 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
623 struct hl_device *hdev = entry->hdev;
624 u64 addr = entry->addr;
630 if (atomic_read(&hdev->in_reset)) {
631 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
638 user_address = hl_is_device_va(hdev, addr);
640 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
645 rc = hdev->asic_funcs->debugfs_read64(hdev, addr, user_address, &val);
647 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
651 sprintf(tmp_buf, "0x%016llx\n", val);
652 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
656 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
657 size_t count, loff_t *ppos)
659 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
660 struct hl_device *hdev = entry->hdev;
661 u64 addr = entry->addr;
666 if (atomic_read(&hdev->in_reset)) {
667 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
671 rc = kstrtoull_from_user(buf, count, 16, &value);
675 user_address = hl_is_device_va(hdev, addr);
677 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
682 rc = hdev->asic_funcs->debugfs_write64(hdev, addr, user_address, value);
684 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
692 static ssize_t hl_dma_size_write(struct file *f, const char __user *buf,
693 size_t count, loff_t *ppos)
695 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
696 struct hl_device *hdev = entry->hdev;
697 u64 addr = entry->addr;
701 if (atomic_read(&hdev->in_reset)) {
702 dev_warn_ratelimited(hdev->dev, "Can't DMA during reset\n");
705 rc = kstrtouint_from_user(buf, count, 16, &size);
710 dev_err(hdev->dev, "DMA read failed. size can't be 0\n");
714 if (size > SZ_128M) {
716 "DMA read failed. size can't be larger than 128MB\n");
720 if (!hl_is_device_internal_memory_va(hdev, addr, size)) {
722 "DMA read failed. Invalid 0x%010llx + 0x%08x\n",
727 /* Free the previous allocation, if there was any */
728 entry->blob_desc.size = 0;
729 vfree(entry->blob_desc.data);
731 entry->blob_desc.data = vmalloc(size);
732 if (!entry->blob_desc.data)
735 rc = hdev->asic_funcs->debugfs_read_dma(hdev, addr, size,
736 entry->blob_desc.data);
738 dev_err(hdev->dev, "Failed to DMA from 0x%010llx\n", addr);
739 vfree(entry->blob_desc.data);
740 entry->blob_desc.data = NULL;
744 entry->blob_desc.size = size;
749 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
750 size_t count, loff_t *ppos)
752 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
753 struct hl_device *hdev = entry->hdev;
760 if (hdev->pdev->current_state == PCI_D0)
762 else if (hdev->pdev->current_state == PCI_D3hot)
768 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
769 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
773 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
774 size_t count, loff_t *ppos)
776 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
777 struct hl_device *hdev = entry->hdev;
781 rc = kstrtouint_from_user(buf, count, 10, &value);
786 pci_set_power_state(hdev->pdev, PCI_D0);
787 pci_restore_state(hdev->pdev);
788 rc = pci_enable_device(hdev->pdev);
789 } else if (value == 2) {
790 pci_save_state(hdev->pdev);
791 pci_disable_device(hdev->pdev);
792 pci_set_power_state(hdev->pdev, PCI_D3hot);
794 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
801 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
802 size_t count, loff_t *ppos)
804 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
805 struct hl_device *hdev = entry->hdev;
813 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
814 entry->i2c_reg, &val);
817 "Failed to read from I2C bus %d, addr %d, reg %d\n",
818 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
822 sprintf(tmp_buf, "0x%02lx\n", val);
823 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
829 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
830 size_t count, loff_t *ppos)
832 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
833 struct hl_device *hdev = entry->hdev;
837 rc = kstrtouint_from_user(buf, count, 16, &value);
841 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
842 entry->i2c_reg, value);
845 "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
846 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
853 static ssize_t hl_led0_write(struct file *f, const 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;
861 rc = kstrtouint_from_user(buf, count, 10, &value);
865 value = value ? 1 : 0;
867 hl_debugfs_led_set(hdev, 0, value);
872 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
873 size_t count, loff_t *ppos)
875 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
876 struct hl_device *hdev = entry->hdev;
880 rc = kstrtouint_from_user(buf, count, 10, &value);
884 value = value ? 1 : 0;
886 hl_debugfs_led_set(hdev, 1, value);
891 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
892 size_t count, loff_t *ppos)
894 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
895 struct hl_device *hdev = entry->hdev;
899 rc = kstrtouint_from_user(buf, count, 10, &value);
903 value = value ? 1 : 0;
905 hl_debugfs_led_set(hdev, 2, value);
910 static ssize_t hl_device_read(struct file *f, char __user *buf,
911 size_t count, loff_t *ppos)
913 static const char *help =
914 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
915 return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
918 static ssize_t hl_device_write(struct file *f, const char __user *buf,
919 size_t count, loff_t *ppos)
921 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
922 struct hl_device *hdev = entry->hdev;
925 /* don't allow partial writes */
929 simple_write_to_buffer(data, 29, ppos, buf, count);
931 if (strncmp("disable", data, strlen("disable")) == 0) {
932 hdev->disabled = true;
933 } else if (strncmp("enable", data, strlen("enable")) == 0) {
934 hdev->disabled = false;
935 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
936 hdev->asic_funcs->suspend(hdev);
937 } else if (strncmp("resume", data, strlen("resume")) == 0) {
938 hdev->asic_funcs->resume(hdev);
939 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
940 hdev->device_cpu_disabled = true;
943 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
950 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
951 size_t count, loff_t *ppos)
953 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
954 struct hl_device *hdev = entry->hdev;
961 sprintf(tmp_buf, "0x%llx\n", hdev->clock_gating_mask);
962 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
963 strlen(tmp_buf) + 1);
968 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
969 size_t count, loff_t *ppos)
971 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
972 struct hl_device *hdev = entry->hdev;
976 if (atomic_read(&hdev->in_reset)) {
977 dev_warn_ratelimited(hdev->dev,
978 "Can't change clock gating during reset\n");
982 rc = kstrtoull_from_user(buf, count, 16, &value);
986 hdev->clock_gating_mask = value;
987 hdev->asic_funcs->set_clock_gating(hdev);
992 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
993 size_t count, loff_t *ppos)
995 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
996 struct hl_device *hdev = entry->hdev;
1003 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1004 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1005 strlen(tmp_buf) + 1);
1010 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1011 size_t count, loff_t *ppos)
1013 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1014 struct hl_device *hdev = entry->hdev;
1018 if (atomic_read(&hdev->in_reset)) {
1019 dev_warn_ratelimited(hdev->dev,
1020 "Can't change stop on error during reset\n");
1024 rc = kstrtouint_from_user(buf, count, 10, &value);
1028 hdev->stop_on_err = value ? 1 : 0;
1030 hl_device_reset(hdev, 0);
1035 static ssize_t hl_security_violations_read(struct file *f, char __user *buf,
1036 size_t count, loff_t *ppos)
1038 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1039 struct hl_device *hdev = entry->hdev;
1041 hdev->asic_funcs->ack_protection_bits_errors(hdev);
1046 static const struct file_operations hl_data32b_fops = {
1047 .owner = THIS_MODULE,
1048 .read = hl_data_read32,
1049 .write = hl_data_write32
1052 static const struct file_operations hl_data64b_fops = {
1053 .owner = THIS_MODULE,
1054 .read = hl_data_read64,
1055 .write = hl_data_write64
1058 static const struct file_operations hl_dma_size_fops = {
1059 .owner = THIS_MODULE,
1060 .write = hl_dma_size_write
1063 static const struct file_operations hl_i2c_data_fops = {
1064 .owner = THIS_MODULE,
1065 .read = hl_i2c_data_read,
1066 .write = hl_i2c_data_write
1069 static const struct file_operations hl_power_fops = {
1070 .owner = THIS_MODULE,
1071 .read = hl_get_power_state,
1072 .write = hl_set_power_state
1075 static const struct file_operations hl_led0_fops = {
1076 .owner = THIS_MODULE,
1077 .write = hl_led0_write
1080 static const struct file_operations hl_led1_fops = {
1081 .owner = THIS_MODULE,
1082 .write = hl_led1_write
1085 static const struct file_operations hl_led2_fops = {
1086 .owner = THIS_MODULE,
1087 .write = hl_led2_write
1090 static const struct file_operations hl_device_fops = {
1091 .owner = THIS_MODULE,
1092 .read = hl_device_read,
1093 .write = hl_device_write
1096 static const struct file_operations hl_clk_gate_fops = {
1097 .owner = THIS_MODULE,
1098 .read = hl_clk_gate_read,
1099 .write = hl_clk_gate_write
1102 static const struct file_operations hl_stop_on_err_fops = {
1103 .owner = THIS_MODULE,
1104 .read = hl_stop_on_err_read,
1105 .write = hl_stop_on_err_write
1108 static const struct file_operations hl_security_violations_fops = {
1109 .owner = THIS_MODULE,
1110 .read = hl_security_violations_read
1113 static const struct hl_info_list hl_debugfs_list[] = {
1114 {"command_buffers", command_buffers_show, NULL},
1115 {"command_submission", command_submission_show, NULL},
1116 {"command_submission_jobs", command_submission_jobs_show, NULL},
1117 {"userptr", userptr_show, NULL},
1118 {"vm", vm_show, NULL},
1119 {"mmu", mmu_show, mmu_asid_va_write},
1120 {"engines", engines_show, NULL}
1123 static int hl_debugfs_open(struct inode *inode, struct file *file)
1125 struct hl_debugfs_entry *node = inode->i_private;
1127 return single_open(file, node->info_ent->show, node);
1130 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1131 size_t count, loff_t *f_pos)
1133 struct hl_debugfs_entry *node = file->f_inode->i_private;
1135 if (node->info_ent->write)
1136 return node->info_ent->write(file, buf, count, f_pos);
1142 static const struct file_operations hl_debugfs_fops = {
1143 .owner = THIS_MODULE,
1144 .open = hl_debugfs_open,
1146 .write = hl_debugfs_write,
1147 .llseek = seq_lseek,
1148 .release = single_release,
1151 void hl_debugfs_add_device(struct hl_device *hdev)
1153 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1154 int count = ARRAY_SIZE(hl_debugfs_list);
1155 struct hl_debugfs_entry *entry;
1158 dev_entry->hdev = hdev;
1159 dev_entry->entry_arr = kmalloc_array(count,
1160 sizeof(struct hl_debugfs_entry),
1162 if (!dev_entry->entry_arr)
1165 dev_entry->blob_desc.size = 0;
1166 dev_entry->blob_desc.data = NULL;
1168 INIT_LIST_HEAD(&dev_entry->file_list);
1169 INIT_LIST_HEAD(&dev_entry->cb_list);
1170 INIT_LIST_HEAD(&dev_entry->cs_list);
1171 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1172 INIT_LIST_HEAD(&dev_entry->userptr_list);
1173 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1174 mutex_init(&dev_entry->file_mutex);
1175 spin_lock_init(&dev_entry->cb_spinlock);
1176 spin_lock_init(&dev_entry->cs_spinlock);
1177 spin_lock_init(&dev_entry->cs_job_spinlock);
1178 spin_lock_init(&dev_entry->userptr_spinlock);
1179 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1181 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1184 debugfs_create_x64("addr",
1189 debugfs_create_file("data32",
1195 debugfs_create_file("data64",
1201 debugfs_create_file("set_power_state",
1207 debugfs_create_u8("i2c_bus",
1210 &dev_entry->i2c_bus);
1212 debugfs_create_u8("i2c_addr",
1215 &dev_entry->i2c_addr);
1217 debugfs_create_u8("i2c_reg",
1220 &dev_entry->i2c_reg);
1222 debugfs_create_file("i2c_data",
1228 debugfs_create_file("led0",
1234 debugfs_create_file("led1",
1240 debugfs_create_file("led2",
1246 debugfs_create_file("device",
1252 debugfs_create_file("clk_gate",
1258 debugfs_create_file("stop_on_err",
1262 &hl_stop_on_err_fops);
1264 debugfs_create_file("dump_security_violations",
1268 &hl_security_violations_fops);
1270 debugfs_create_file("dma_size",
1276 debugfs_create_blob("data_dma",
1279 &dev_entry->blob_desc);
1281 debugfs_create_x8("skip_reset_on_timeout",
1284 &hdev->skip_reset_on_timeout);
1286 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1287 debugfs_create_file(hl_debugfs_list[i].name,
1292 entry->info_ent = &hl_debugfs_list[i];
1293 entry->dev_entry = dev_entry;
1297 void hl_debugfs_remove_device(struct hl_device *hdev)
1299 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1301 debugfs_remove_recursive(entry->root);
1303 mutex_destroy(&entry->file_mutex);
1305 vfree(entry->blob_desc.data);
1307 kfree(entry->entry_arr);
1310 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1312 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1314 mutex_lock(&dev_entry->file_mutex);
1315 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1316 mutex_unlock(&dev_entry->file_mutex);
1319 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1321 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1323 mutex_lock(&dev_entry->file_mutex);
1324 list_del(&hpriv->debugfs_list);
1325 mutex_unlock(&dev_entry->file_mutex);
1328 void hl_debugfs_add_cb(struct hl_cb *cb)
1330 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1332 spin_lock(&dev_entry->cb_spinlock);
1333 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1334 spin_unlock(&dev_entry->cb_spinlock);
1337 void hl_debugfs_remove_cb(struct hl_cb *cb)
1339 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1341 spin_lock(&dev_entry->cb_spinlock);
1342 list_del(&cb->debugfs_list);
1343 spin_unlock(&dev_entry->cb_spinlock);
1346 void hl_debugfs_add_cs(struct hl_cs *cs)
1348 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1350 spin_lock(&dev_entry->cs_spinlock);
1351 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1352 spin_unlock(&dev_entry->cs_spinlock);
1355 void hl_debugfs_remove_cs(struct hl_cs *cs)
1357 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1359 spin_lock(&dev_entry->cs_spinlock);
1360 list_del(&cs->debugfs_list);
1361 spin_unlock(&dev_entry->cs_spinlock);
1364 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1366 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1368 spin_lock(&dev_entry->cs_job_spinlock);
1369 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1370 spin_unlock(&dev_entry->cs_job_spinlock);
1373 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1375 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1377 spin_lock(&dev_entry->cs_job_spinlock);
1378 list_del(&job->debugfs_list);
1379 spin_unlock(&dev_entry->cs_job_spinlock);
1382 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1384 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1386 spin_lock(&dev_entry->userptr_spinlock);
1387 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1388 spin_unlock(&dev_entry->userptr_spinlock);
1391 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1392 struct hl_userptr *userptr)
1394 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1396 spin_lock(&dev_entry->userptr_spinlock);
1397 list_del(&userptr->debugfs_list);
1398 spin_unlock(&dev_entry->userptr_spinlock);
1401 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1403 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1405 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1406 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1407 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1410 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1412 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1414 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1415 list_del(&ctx->debugfs_list);
1416 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1419 void __init hl_debugfs_init(void)
1421 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1424 void hl_debugfs_fini(void)
1426 debugfs_remove_recursive(hl_debug_root);