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/debugfs.h>
13 #include <linux/uaccess.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;
234 struct hl_vm_hash_node *hnode;
235 struct hl_userptr *userptr;
236 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
237 enum vm_type_t *vm_type;
242 if (!dev_entry->hdev->mmu_enable)
245 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
247 list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
249 seq_puts(s, "\n\n----------------------------------------------------");
250 seq_puts(s, "\n----------------------------------------------------\n\n");
251 seq_printf(s, "ctx asid: %u\n", ctx->asid);
253 seq_puts(s, "\nmappings:\n\n");
254 seq_puts(s, " virtual address size handle\n");
255 seq_puts(s, "----------------------------------------------------\n");
256 mutex_lock(&ctx->mem_hash_lock);
257 hash_for_each(ctx->mem_hash, i, hnode, node) {
258 vm_type = hnode->ptr;
260 if (*vm_type == VM_TYPE_USERPTR) {
261 userptr = hnode->ptr;
263 " 0x%-14llx %-10u\n",
264 hnode->vaddr, userptr->size);
266 phys_pg_pack = hnode->ptr;
268 " 0x%-14llx %-10llu %-4u\n",
269 hnode->vaddr, phys_pg_pack->total_size,
270 phys_pg_pack->handle);
273 mutex_unlock(&ctx->mem_hash_lock);
276 spin_lock(&vm->idr_lock);
278 if (!idr_is_empty(&vm->phys_pg_pack_handles))
279 seq_puts(s, "\n\nallocations:\n");
281 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
282 if (phys_pg_pack->asid != ctx->asid)
285 seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
286 seq_printf(s, "page size: %u\n\n",
287 phys_pg_pack->page_size);
288 seq_puts(s, " physical address\n");
289 seq_puts(s, "---------------------\n");
290 for (j = 0 ; j < phys_pg_pack->npages ; j++) {
291 seq_printf(s, " 0x%-14llx\n",
292 phys_pg_pack->pages[j]);
295 spin_unlock(&vm->idr_lock);
299 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
307 static int mmu_show(struct seq_file *s, void *data)
309 struct hl_debugfs_entry *entry = s->private;
310 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
311 struct hl_device *hdev = dev_entry->hdev;
313 struct hl_mmu_hop_info hops_info = {0};
314 u64 virt_addr = dev_entry->mmu_addr, phys_addr;
317 if (!hdev->mmu_enable)
320 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
321 ctx = hdev->kernel_ctx;
323 ctx = hdev->compute_ctx;
326 dev_err(hdev->dev, "no ctx available\n");
330 if (hl_mmu_get_tlb_info(ctx, virt_addr, &hops_info)) {
331 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
336 phys_addr = hops_info.hop_info[hops_info.used_hops - 1].hop_pte_val;
338 if (hops_info.scrambled_vaddr &&
339 (dev_entry->mmu_addr != hops_info.scrambled_vaddr))
341 "asid: %u, virt_addr: 0x%llx, scrambled virt_addr: 0x%llx,\nphys_addr: 0x%llx, scrambled_phys_addr: 0x%llx\n",
342 dev_entry->mmu_asid, dev_entry->mmu_addr,
343 hops_info.scrambled_vaddr,
344 hops_info.unscrambled_paddr, phys_addr);
347 "asid: %u, virt_addr: 0x%llx, phys_addr: 0x%llx\n",
348 dev_entry->mmu_asid, dev_entry->mmu_addr, phys_addr);
350 for (i = 0 ; i < hops_info.used_hops ; i++) {
351 seq_printf(s, "hop%d_addr: 0x%llx\n",
352 i, hops_info.hop_info[i].hop_addr);
353 seq_printf(s, "hop%d_pte_addr: 0x%llx\n",
354 i, hops_info.hop_info[i].hop_pte_addr);
355 seq_printf(s, "hop%d_pte: 0x%llx\n",
356 i, hops_info.hop_info[i].hop_pte_val);
362 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
363 size_t count, loff_t *f_pos)
365 struct seq_file *s = file->private_data;
366 struct hl_debugfs_entry *entry = s->private;
367 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
368 struct hl_device *hdev = dev_entry->hdev;
369 char kbuf[MMU_KBUF_SIZE];
373 if (!hdev->mmu_enable)
376 if (count > sizeof(kbuf) - 1)
378 if (copy_from_user(kbuf, buf, count))
382 c = strchr(kbuf, ' ');
387 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
391 if (strncmp(c+1, "0x", 2))
393 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
400 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
405 static int engines_show(struct seq_file *s, void *data)
407 struct hl_debugfs_entry *entry = s->private;
408 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
409 struct hl_device *hdev = dev_entry->hdev;
411 if (atomic_read(&hdev->in_reset)) {
412 dev_warn_ratelimited(hdev->dev,
413 "Can't check device idle during reset\n");
417 hdev->asic_funcs->is_device_idle(hdev, NULL, 0, s);
422 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
424 struct asic_fixed_properties *prop = &hdev->asic_prop;
426 if (!hdev->mmu_enable)
429 if (prop->dram_supports_virtual_memory &&
430 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
433 if (addr >= prop->pmmu.start_addr &&
434 addr < prop->pmmu.end_addr)
437 if (addr >= prop->pmmu_huge.start_addr &&
438 addr < prop->pmmu_huge.end_addr)
444 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr,
447 struct hl_ctx *ctx = hdev->compute_ctx;
451 dev_err(hdev->dev, "no ctx available\n");
455 rc = hl_mmu_va_to_pa(ctx, virt_addr, phys_addr);
457 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
465 static ssize_t hl_data_read32(struct file *f, char __user *buf,
466 size_t count, loff_t *ppos)
468 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
469 struct hl_device *hdev = entry->hdev;
471 u64 addr = entry->addr;
475 if (atomic_read(&hdev->in_reset)) {
476 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
483 if (hl_is_device_va(hdev, addr)) {
484 rc = device_va_to_pa(hdev, addr, &addr);
489 rc = hdev->asic_funcs->debugfs_read32(hdev, addr, &val);
491 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
495 sprintf(tmp_buf, "0x%08x\n", val);
496 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
500 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
501 size_t count, loff_t *ppos)
503 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
504 struct hl_device *hdev = entry->hdev;
505 u64 addr = entry->addr;
509 if (atomic_read(&hdev->in_reset)) {
510 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
514 rc = kstrtouint_from_user(buf, count, 16, &value);
518 if (hl_is_device_va(hdev, addr)) {
519 rc = device_va_to_pa(hdev, addr, &addr);
524 rc = hdev->asic_funcs->debugfs_write32(hdev, addr, value);
526 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
534 static ssize_t hl_data_read64(struct file *f, char __user *buf,
535 size_t count, loff_t *ppos)
537 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
538 struct hl_device *hdev = entry->hdev;
540 u64 addr = entry->addr;
547 if (hl_is_device_va(hdev, addr)) {
548 rc = device_va_to_pa(hdev, addr, &addr);
553 rc = hdev->asic_funcs->debugfs_read64(hdev, addr, &val);
555 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
559 sprintf(tmp_buf, "0x%016llx\n", val);
560 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
564 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
565 size_t count, loff_t *ppos)
567 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
568 struct hl_device *hdev = entry->hdev;
569 u64 addr = entry->addr;
573 rc = kstrtoull_from_user(buf, count, 16, &value);
577 if (hl_is_device_va(hdev, addr)) {
578 rc = device_va_to_pa(hdev, addr, &addr);
583 rc = hdev->asic_funcs->debugfs_write64(hdev, addr, value);
585 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
593 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
594 size_t count, loff_t *ppos)
596 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
597 struct hl_device *hdev = entry->hdev;
604 if (hdev->pdev->current_state == PCI_D0)
606 else if (hdev->pdev->current_state == PCI_D3hot)
612 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
613 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
617 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
618 size_t count, loff_t *ppos)
620 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
621 struct hl_device *hdev = entry->hdev;
625 rc = kstrtouint_from_user(buf, count, 10, &value);
630 pci_set_power_state(hdev->pdev, PCI_D0);
631 pci_restore_state(hdev->pdev);
632 rc = pci_enable_device(hdev->pdev);
633 } else if (value == 2) {
634 pci_save_state(hdev->pdev);
635 pci_disable_device(hdev->pdev);
636 pci_set_power_state(hdev->pdev, PCI_D3hot);
638 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
645 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
646 size_t count, loff_t *ppos)
648 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
649 struct hl_device *hdev = entry->hdev;
657 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
658 entry->i2c_reg, &val);
661 "Failed to read from I2C bus %d, addr %d, reg %d\n",
662 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
666 sprintf(tmp_buf, "0x%02lx\n", val);
667 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
673 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
674 size_t count, loff_t *ppos)
676 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
677 struct hl_device *hdev = entry->hdev;
681 rc = kstrtouint_from_user(buf, count, 16, &value);
685 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
686 entry->i2c_reg, value);
689 "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
690 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
697 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
698 size_t count, loff_t *ppos)
700 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
701 struct hl_device *hdev = entry->hdev;
705 rc = kstrtouint_from_user(buf, count, 10, &value);
709 value = value ? 1 : 0;
711 hl_debugfs_led_set(hdev, 0, value);
716 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
717 size_t count, loff_t *ppos)
719 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
720 struct hl_device *hdev = entry->hdev;
724 rc = kstrtouint_from_user(buf, count, 10, &value);
728 value = value ? 1 : 0;
730 hl_debugfs_led_set(hdev, 1, value);
735 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
736 size_t count, loff_t *ppos)
738 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
739 struct hl_device *hdev = entry->hdev;
743 rc = kstrtouint_from_user(buf, count, 10, &value);
747 value = value ? 1 : 0;
749 hl_debugfs_led_set(hdev, 2, value);
754 static ssize_t hl_device_read(struct file *f, char __user *buf,
755 size_t count, loff_t *ppos)
757 static const char *help =
758 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
759 return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
762 static ssize_t hl_device_write(struct file *f, const char __user *buf,
763 size_t count, loff_t *ppos)
765 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
766 struct hl_device *hdev = entry->hdev;
769 /* don't allow partial writes */
773 simple_write_to_buffer(data, 29, ppos, buf, count);
775 if (strncmp("disable", data, strlen("disable")) == 0) {
776 hdev->disabled = true;
777 } else if (strncmp("enable", data, strlen("enable")) == 0) {
778 hdev->disabled = false;
779 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
780 hdev->asic_funcs->suspend(hdev);
781 } else if (strncmp("resume", data, strlen("resume")) == 0) {
782 hdev->asic_funcs->resume(hdev);
783 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
784 hdev->device_cpu_disabled = true;
787 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
794 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
795 size_t count, loff_t *ppos)
797 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
798 struct hl_device *hdev = entry->hdev;
805 sprintf(tmp_buf, "0x%llx\n", hdev->clock_gating_mask);
806 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
807 strlen(tmp_buf) + 1);
812 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
813 size_t count, loff_t *ppos)
815 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
816 struct hl_device *hdev = entry->hdev;
820 if (atomic_read(&hdev->in_reset)) {
821 dev_warn_ratelimited(hdev->dev,
822 "Can't change clock gating during reset\n");
826 rc = kstrtoull_from_user(buf, count, 16, &value);
830 hdev->clock_gating_mask = value;
831 hdev->asic_funcs->set_clock_gating(hdev);
836 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
837 size_t count, loff_t *ppos)
839 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
840 struct hl_device *hdev = entry->hdev;
847 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
848 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
849 strlen(tmp_buf) + 1);
854 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
855 size_t count, loff_t *ppos)
857 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
858 struct hl_device *hdev = entry->hdev;
862 if (atomic_read(&hdev->in_reset)) {
863 dev_warn_ratelimited(hdev->dev,
864 "Can't change stop on error during reset\n");
868 rc = kstrtouint_from_user(buf, count, 10, &value);
872 hdev->stop_on_err = value ? 1 : 0;
874 hl_device_reset(hdev, false, false);
879 static ssize_t hl_security_violations_read(struct file *f, char __user *buf,
880 size_t count, loff_t *ppos)
882 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
883 struct hl_device *hdev = entry->hdev;
885 hdev->asic_funcs->ack_protection_bits_errors(hdev);
890 static const struct file_operations hl_data32b_fops = {
891 .owner = THIS_MODULE,
892 .read = hl_data_read32,
893 .write = hl_data_write32
896 static const struct file_operations hl_data64b_fops = {
897 .owner = THIS_MODULE,
898 .read = hl_data_read64,
899 .write = hl_data_write64
902 static const struct file_operations hl_i2c_data_fops = {
903 .owner = THIS_MODULE,
904 .read = hl_i2c_data_read,
905 .write = hl_i2c_data_write
908 static const struct file_operations hl_power_fops = {
909 .owner = THIS_MODULE,
910 .read = hl_get_power_state,
911 .write = hl_set_power_state
914 static const struct file_operations hl_led0_fops = {
915 .owner = THIS_MODULE,
916 .write = hl_led0_write
919 static const struct file_operations hl_led1_fops = {
920 .owner = THIS_MODULE,
921 .write = hl_led1_write
924 static const struct file_operations hl_led2_fops = {
925 .owner = THIS_MODULE,
926 .write = hl_led2_write
929 static const struct file_operations hl_device_fops = {
930 .owner = THIS_MODULE,
931 .read = hl_device_read,
932 .write = hl_device_write
935 static const struct file_operations hl_clk_gate_fops = {
936 .owner = THIS_MODULE,
937 .read = hl_clk_gate_read,
938 .write = hl_clk_gate_write
941 static const struct file_operations hl_stop_on_err_fops = {
942 .owner = THIS_MODULE,
943 .read = hl_stop_on_err_read,
944 .write = hl_stop_on_err_write
947 static const struct file_operations hl_security_violations_fops = {
948 .owner = THIS_MODULE,
949 .read = hl_security_violations_read
952 static const struct hl_info_list hl_debugfs_list[] = {
953 {"command_buffers", command_buffers_show, NULL},
954 {"command_submission", command_submission_show, NULL},
955 {"command_submission_jobs", command_submission_jobs_show, NULL},
956 {"userptr", userptr_show, NULL},
957 {"vm", vm_show, NULL},
958 {"mmu", mmu_show, mmu_asid_va_write},
959 {"engines", engines_show, NULL}
962 static int hl_debugfs_open(struct inode *inode, struct file *file)
964 struct hl_debugfs_entry *node = inode->i_private;
966 return single_open(file, node->info_ent->show, node);
969 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
970 size_t count, loff_t *f_pos)
972 struct hl_debugfs_entry *node = file->f_inode->i_private;
974 if (node->info_ent->write)
975 return node->info_ent->write(file, buf, count, f_pos);
981 static const struct file_operations hl_debugfs_fops = {
982 .owner = THIS_MODULE,
983 .open = hl_debugfs_open,
985 .write = hl_debugfs_write,
987 .release = single_release,
990 void hl_debugfs_add_device(struct hl_device *hdev)
992 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
993 int count = ARRAY_SIZE(hl_debugfs_list);
994 struct hl_debugfs_entry *entry;
998 dev_entry->hdev = hdev;
999 dev_entry->entry_arr = kmalloc_array(count,
1000 sizeof(struct hl_debugfs_entry),
1002 if (!dev_entry->entry_arr)
1005 INIT_LIST_HEAD(&dev_entry->file_list);
1006 INIT_LIST_HEAD(&dev_entry->cb_list);
1007 INIT_LIST_HEAD(&dev_entry->cs_list);
1008 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1009 INIT_LIST_HEAD(&dev_entry->userptr_list);
1010 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1011 mutex_init(&dev_entry->file_mutex);
1012 spin_lock_init(&dev_entry->cb_spinlock);
1013 spin_lock_init(&dev_entry->cs_spinlock);
1014 spin_lock_init(&dev_entry->cs_job_spinlock);
1015 spin_lock_init(&dev_entry->userptr_spinlock);
1016 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1018 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1021 debugfs_create_x64("addr",
1026 debugfs_create_file("data32",
1032 debugfs_create_file("data64",
1038 debugfs_create_file("set_power_state",
1044 debugfs_create_u8("i2c_bus",
1047 &dev_entry->i2c_bus);
1049 debugfs_create_u8("i2c_addr",
1052 &dev_entry->i2c_addr);
1054 debugfs_create_u8("i2c_reg",
1057 &dev_entry->i2c_reg);
1059 debugfs_create_file("i2c_data",
1065 debugfs_create_file("led0",
1071 debugfs_create_file("led1",
1077 debugfs_create_file("led2",
1083 debugfs_create_file("device",
1089 debugfs_create_file("clk_gate",
1095 debugfs_create_file("stop_on_err",
1099 &hl_stop_on_err_fops);
1101 debugfs_create_file("dump_security_violations",
1105 &hl_security_violations_fops);
1107 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1109 ent = debugfs_create_file(hl_debugfs_list[i].name,
1115 entry->info_ent = &hl_debugfs_list[i];
1116 entry->dev_entry = dev_entry;
1120 void hl_debugfs_remove_device(struct hl_device *hdev)
1122 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1124 debugfs_remove_recursive(entry->root);
1126 mutex_destroy(&entry->file_mutex);
1127 kfree(entry->entry_arr);
1130 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1132 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1134 mutex_lock(&dev_entry->file_mutex);
1135 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1136 mutex_unlock(&dev_entry->file_mutex);
1139 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1141 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1143 mutex_lock(&dev_entry->file_mutex);
1144 list_del(&hpriv->debugfs_list);
1145 mutex_unlock(&dev_entry->file_mutex);
1148 void hl_debugfs_add_cb(struct hl_cb *cb)
1150 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1152 spin_lock(&dev_entry->cb_spinlock);
1153 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1154 spin_unlock(&dev_entry->cb_spinlock);
1157 void hl_debugfs_remove_cb(struct hl_cb *cb)
1159 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1161 spin_lock(&dev_entry->cb_spinlock);
1162 list_del(&cb->debugfs_list);
1163 spin_unlock(&dev_entry->cb_spinlock);
1166 void hl_debugfs_add_cs(struct hl_cs *cs)
1168 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1170 spin_lock(&dev_entry->cs_spinlock);
1171 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1172 spin_unlock(&dev_entry->cs_spinlock);
1175 void hl_debugfs_remove_cs(struct hl_cs *cs)
1177 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1179 spin_lock(&dev_entry->cs_spinlock);
1180 list_del(&cs->debugfs_list);
1181 spin_unlock(&dev_entry->cs_spinlock);
1184 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1186 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1188 spin_lock(&dev_entry->cs_job_spinlock);
1189 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1190 spin_unlock(&dev_entry->cs_job_spinlock);
1193 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1195 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1197 spin_lock(&dev_entry->cs_job_spinlock);
1198 list_del(&job->debugfs_list);
1199 spin_unlock(&dev_entry->cs_job_spinlock);
1202 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1204 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1206 spin_lock(&dev_entry->userptr_spinlock);
1207 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1208 spin_unlock(&dev_entry->userptr_spinlock);
1211 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1212 struct hl_userptr *userptr)
1214 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1216 spin_lock(&dev_entry->userptr_spinlock);
1217 list_del(&userptr->debugfs_list);
1218 spin_unlock(&dev_entry->userptr_spinlock);
1221 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1223 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1225 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1226 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1227 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1230 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1232 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1234 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1235 list_del(&ctx->debugfs_list);
1236 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1239 void __init hl_debugfs_init(void)
1241 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1244 void hl_debugfs_fini(void)
1246 debugfs_remove_recursive(hl_debug_root);