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,
24 struct armcp_packet pkt;
27 if (hl_device_disabled_or_in_reset(hdev))
30 memset(&pkt, 0, sizeof(pkt));
32 pkt.ctl = __cpu_to_le32(ARMCP_PACKET_I2C_RD <<
33 ARMCP_PKT_CTL_OPCODE_SHIFT);
34 pkt.i2c_bus = i2c_bus;
35 pkt.i2c_addr = i2c_addr;
36 pkt.i2c_reg = i2c_reg;
38 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
39 HL_DEVICE_TIMEOUT_USEC, (long *) val);
42 dev_err(hdev->dev, "Failed to read from I2C, error %d\n", rc);
47 static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
50 struct armcp_packet pkt;
53 if (hl_device_disabled_or_in_reset(hdev))
56 memset(&pkt, 0, sizeof(pkt));
58 pkt.ctl = __cpu_to_le32(ARMCP_PACKET_I2C_WR <<
59 ARMCP_PKT_CTL_OPCODE_SHIFT);
60 pkt.i2c_bus = i2c_bus;
61 pkt.i2c_addr = i2c_addr;
62 pkt.i2c_reg = i2c_reg;
63 pkt.value = __cpu_to_le64(val);
65 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
66 HL_DEVICE_TIMEOUT_USEC, NULL);
69 dev_err(hdev->dev, "Failed to write to I2C, error %d\n", rc);
74 static void hl_debugfs_led_set(struct hl_device *hdev, u8 led, u8 state)
76 struct armcp_packet pkt;
79 if (hl_device_disabled_or_in_reset(hdev))
82 memset(&pkt, 0, sizeof(pkt));
84 pkt.ctl = __cpu_to_le32(ARMCP_PACKET_LED_SET <<
85 ARMCP_PKT_CTL_OPCODE_SHIFT);
86 pkt.led_index = __cpu_to_le32(led);
87 pkt.value = __cpu_to_le64(state);
89 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
90 HL_DEVICE_TIMEOUT_USEC, NULL);
93 dev_err(hdev->dev, "Failed to set LED %d, error %d\n", led, rc);
96 static int command_buffers_show(struct seq_file *s, void *data)
98 struct hl_debugfs_entry *entry = s->private;
99 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
103 spin_lock(&dev_entry->cb_spinlock);
105 list_for_each_entry(cb, &dev_entry->cb_list, debugfs_list) {
109 seq_puts(s, " CB ID CTX ID CB size CB RefCnt mmap? CS counter\n");
110 seq_puts(s, "---------------------------------------------------------------\n");
113 " %03d %d 0x%08x %d %d %d\n",
114 cb->id, cb->ctx_id, cb->size,
115 kref_read(&cb->refcount),
116 cb->mmap, cb->cs_cnt);
119 spin_unlock(&dev_entry->cb_spinlock);
127 static int command_submission_show(struct seq_file *s, void *data)
129 struct hl_debugfs_entry *entry = s->private;
130 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
134 spin_lock(&dev_entry->cs_spinlock);
136 list_for_each_entry(cs, &dev_entry->cs_list, debugfs_list) {
140 seq_puts(s, " CS ID CTX ASID CS RefCnt Submitted Completed\n");
141 seq_puts(s, "------------------------------------------------------\n");
144 " %llu %d %d %d %d\n",
145 cs->sequence, cs->ctx->asid,
146 kref_read(&cs->refcount),
147 cs->submitted, cs->completed);
150 spin_unlock(&dev_entry->cs_spinlock);
158 static int command_submission_jobs_show(struct seq_file *s, void *data)
160 struct hl_debugfs_entry *entry = s->private;
161 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
162 struct hl_cs_job *job;
165 spin_lock(&dev_entry->cs_job_spinlock);
167 list_for_each_entry(job, &dev_entry->cs_job_list, debugfs_list) {
171 seq_puts(s, " JOB ID CS ID CTX ASID H/W Queue\n");
172 seq_puts(s, "---------------------------------------\n");
176 " %02d %llu %d %d\n",
177 job->id, job->cs->sequence, job->cs->ctx->asid,
182 job->id, HL_KERNEL_ASID_ID, job->hw_queue_id);
185 spin_unlock(&dev_entry->cs_job_spinlock);
193 static int userptr_show(struct seq_file *s, void *data)
195 struct hl_debugfs_entry *entry = s->private;
196 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
197 struct hl_userptr *userptr;
198 char dma_dir[4][30] = {"DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
199 "DMA_FROM_DEVICE", "DMA_NONE"};
202 spin_lock(&dev_entry->userptr_spinlock);
204 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
208 seq_puts(s, " user virtual address size dma dir\n");
209 seq_puts(s, "----------------------------------------------------------\n");
212 " 0x%-14llx %-10u %-30s\n",
213 userptr->addr, userptr->size, dma_dir[userptr->dir]);
216 spin_unlock(&dev_entry->userptr_spinlock);
224 static int vm_show(struct seq_file *s, void *data)
226 struct hl_debugfs_entry *entry = s->private;
227 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
230 struct hl_vm_hash_node *hnode;
231 struct hl_userptr *userptr;
232 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
233 enum vm_type_t *vm_type;
237 if (!dev_entry->hdev->mmu_enable)
240 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
242 list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
244 seq_puts(s, "\n\n----------------------------------------------------");
245 seq_puts(s, "\n----------------------------------------------------\n\n");
246 seq_printf(s, "ctx asid: %u\n", ctx->asid);
248 seq_puts(s, "\nmappings:\n\n");
249 seq_puts(s, " virtual address size handle\n");
250 seq_puts(s, "----------------------------------------------------\n");
251 mutex_lock(&ctx->mem_hash_lock);
252 hash_for_each(ctx->mem_hash, i, hnode, node) {
253 vm_type = hnode->ptr;
255 if (*vm_type == VM_TYPE_USERPTR) {
256 userptr = hnode->ptr;
258 " 0x%-14llx %-10u\n",
259 hnode->vaddr, userptr->size);
261 phys_pg_pack = hnode->ptr;
263 " 0x%-14llx %-10u %-4u\n",
264 hnode->vaddr, phys_pg_pack->total_size,
265 phys_pg_pack->handle);
268 mutex_unlock(&ctx->mem_hash_lock);
271 spin_lock(&vm->idr_lock);
273 if (!idr_is_empty(&vm->phys_pg_pack_handles))
274 seq_puts(s, "\n\nallocations:\n");
276 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
277 if (phys_pg_pack->asid != ctx->asid)
280 seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
281 seq_printf(s, "page size: %u\n\n",
282 phys_pg_pack->page_size);
283 seq_puts(s, " physical address\n");
284 seq_puts(s, "---------------------\n");
285 for (i = 0 ; i < phys_pg_pack->npages ; i++) {
286 seq_printf(s, " 0x%-14llx\n",
287 phys_pg_pack->pages[i]);
290 spin_unlock(&vm->idr_lock);
294 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
302 /* these inline functions are copied from mmu.c */
303 static inline u64 get_hop0_addr(struct hl_ctx *ctx)
305 return ctx->hdev->asic_prop.mmu_pgt_addr +
306 (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
309 static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
312 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
313 ((virt_addr & HOP0_MASK) >> HOP0_SHIFT);
316 static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
319 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
320 ((virt_addr & HOP1_MASK) >> HOP1_SHIFT);
323 static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
326 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
327 ((virt_addr & HOP2_MASK) >> HOP2_SHIFT);
330 static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
333 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
334 ((virt_addr & HOP3_MASK) >> HOP3_SHIFT);
337 static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
340 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
341 ((virt_addr & HOP4_MASK) >> HOP4_SHIFT);
344 static inline u64 get_next_hop_addr(u64 curr_pte)
346 if (curr_pte & PAGE_PRESENT_MASK)
347 return curr_pte & PHYS_ADDR_MASK;
352 static int mmu_show(struct seq_file *s, void *data)
354 struct hl_debugfs_entry *entry = s->private;
355 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
356 struct hl_device *hdev = dev_entry->hdev;
357 struct hl_ctx *ctx = hdev->user_ctx;
359 u64 hop0_addr = 0, hop0_pte_addr = 0, hop0_pte = 0,
360 hop1_addr = 0, hop1_pte_addr = 0, hop1_pte = 0,
361 hop2_addr = 0, hop2_pte_addr = 0, hop2_pte = 0,
362 hop3_addr = 0, hop3_pte_addr = 0, hop3_pte = 0,
363 hop4_addr = 0, hop4_pte_addr = 0, hop4_pte = 0,
364 virt_addr = dev_entry->mmu_addr;
366 if (!hdev->mmu_enable)
370 dev_err(hdev->dev, "no ctx available\n");
374 mutex_lock(&ctx->mmu_lock);
376 /* the following lookup is copied from unmap() in mmu.c */
378 hop0_addr = get_hop0_addr(ctx);
379 hop0_pte_addr = get_hop0_pte_addr(ctx, hop0_addr, virt_addr);
380 hop0_pte = hdev->asic_funcs->read_pte(hdev, hop0_pte_addr);
381 hop1_addr = get_next_hop_addr(hop0_pte);
383 if (hop1_addr == ULLONG_MAX)
386 hop1_pte_addr = get_hop1_pte_addr(ctx, hop1_addr, virt_addr);
387 hop1_pte = hdev->asic_funcs->read_pte(hdev, hop1_pte_addr);
388 hop2_addr = get_next_hop_addr(hop1_pte);
390 if (hop2_addr == ULLONG_MAX)
393 hop2_pte_addr = get_hop2_pte_addr(ctx, hop2_addr, virt_addr);
394 hop2_pte = hdev->asic_funcs->read_pte(hdev, hop2_pte_addr);
395 hop3_addr = get_next_hop_addr(hop2_pte);
397 if (hop3_addr == ULLONG_MAX)
400 hop3_pte_addr = get_hop3_pte_addr(ctx, hop3_addr, virt_addr);
401 hop3_pte = hdev->asic_funcs->read_pte(hdev, hop3_pte_addr);
403 if (!(hop3_pte & LAST_MASK)) {
404 hop4_addr = get_next_hop_addr(hop3_pte);
406 if (hop4_addr == ULLONG_MAX)
409 hop4_pte_addr = get_hop4_pte_addr(ctx, hop4_addr, virt_addr);
410 hop4_pte = hdev->asic_funcs->read_pte(hdev, hop4_pte_addr);
411 if (!(hop4_pte & PAGE_PRESENT_MASK))
414 if (!(hop3_pte & PAGE_PRESENT_MASK))
418 seq_printf(s, "asid: %u, virt_addr: 0x%llx\n",
419 dev_entry->mmu_asid, dev_entry->mmu_addr);
421 seq_printf(s, "hop0_addr: 0x%llx\n", hop0_addr);
422 seq_printf(s, "hop0_pte_addr: 0x%llx\n", hop0_pte_addr);
423 seq_printf(s, "hop0_pte: 0x%llx\n", hop0_pte);
425 seq_printf(s, "hop1_addr: 0x%llx\n", hop1_addr);
426 seq_printf(s, "hop1_pte_addr: 0x%llx\n", hop1_pte_addr);
427 seq_printf(s, "hop1_pte: 0x%llx\n", hop1_pte);
429 seq_printf(s, "hop2_addr: 0x%llx\n", hop2_addr);
430 seq_printf(s, "hop2_pte_addr: 0x%llx\n", hop2_pte_addr);
431 seq_printf(s, "hop2_pte: 0x%llx\n", hop2_pte);
433 seq_printf(s, "hop3_addr: 0x%llx\n", hop3_addr);
434 seq_printf(s, "hop3_pte_addr: 0x%llx\n", hop3_pte_addr);
435 seq_printf(s, "hop3_pte: 0x%llx\n", hop3_pte);
437 if (!(hop3_pte & LAST_MASK)) {
438 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
439 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
440 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
446 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
449 mutex_unlock(&ctx->mmu_lock);
454 static ssize_t mmu_write(struct file *file, const char __user *buf,
455 size_t count, loff_t *f_pos)
457 struct seq_file *s = file->private_data;
458 struct hl_debugfs_entry *entry = s->private;
459 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
460 struct hl_device *hdev = dev_entry->hdev;
461 char kbuf[MMU_KBUF_SIZE], asid_kbuf[MMU_ASID_BUF_SIZE],
462 addr_kbuf[MMU_ADDR_BUF_SIZE];
466 if (!hdev->mmu_enable)
469 memset(kbuf, 0, sizeof(kbuf));
470 memset(asid_kbuf, 0, sizeof(asid_kbuf));
471 memset(addr_kbuf, 0, sizeof(addr_kbuf));
473 if (copy_from_user(kbuf, buf, count))
476 kbuf[MMU_KBUF_SIZE - 1] = 0;
478 c = strchr(kbuf, ' ');
482 memcpy(asid_kbuf, kbuf, c - kbuf);
484 rc = kstrtouint(asid_kbuf, 10, &dev_entry->mmu_asid);
488 c = strstr(kbuf, " 0x");
493 memcpy(addr_kbuf, c, (kbuf + count) - c);
495 rc = kstrtoull(addr_kbuf, 16, &dev_entry->mmu_addr);
502 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
507 static ssize_t hl_data_read32(struct file *f, char __user *buf,
508 size_t count, loff_t *ppos)
510 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
511 struct hl_device *hdev = entry->hdev;
519 rc = hdev->asic_funcs->debugfs_read32(hdev, entry->addr, &val);
521 dev_err(hdev->dev, "Failed to read from 0x%010llx\n",
526 sprintf(tmp_buf, "0x%08x\n", val);
527 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
528 strlen(tmp_buf) + 1);
533 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
534 size_t count, loff_t *ppos)
536 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
537 struct hl_device *hdev = entry->hdev;
541 rc = kstrtouint_from_user(buf, count, 16, &value);
545 rc = hdev->asic_funcs->debugfs_write32(hdev, entry->addr, value);
547 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
555 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
556 size_t count, loff_t *ppos)
558 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
559 struct hl_device *hdev = entry->hdev;
567 if (hdev->pdev->current_state == PCI_D0)
569 else if (hdev->pdev->current_state == PCI_D3hot)
575 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
576 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
577 strlen(tmp_buf) + 1);
582 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
583 size_t count, loff_t *ppos)
585 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
586 struct hl_device *hdev = entry->hdev;
590 rc = kstrtouint_from_user(buf, count, 10, &value);
595 pci_set_power_state(hdev->pdev, PCI_D0);
596 pci_restore_state(hdev->pdev);
597 rc = pci_enable_device(hdev->pdev);
598 } else if (value == 2) {
599 pci_save_state(hdev->pdev);
600 pci_disable_device(hdev->pdev);
601 pci_set_power_state(hdev->pdev, PCI_D3hot);
603 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
610 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
611 size_t count, loff_t *ppos)
613 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
614 struct hl_device *hdev = entry->hdev;
622 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
623 entry->i2c_reg, &val);
626 "Failed to read from I2C bus %d, addr %d, reg %d\n",
627 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
631 sprintf(tmp_buf, "0x%02x\n", val);
632 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
633 strlen(tmp_buf) + 1);
638 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
639 size_t count, loff_t *ppos)
641 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
642 struct hl_device *hdev = entry->hdev;
646 rc = kstrtouint_from_user(buf, count, 16, &value);
650 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
651 entry->i2c_reg, value);
654 "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
655 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
662 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
663 size_t count, loff_t *ppos)
665 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
666 struct hl_device *hdev = entry->hdev;
670 rc = kstrtouint_from_user(buf, count, 10, &value);
674 value = value ? 1 : 0;
676 hl_debugfs_led_set(hdev, 0, value);
681 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
682 size_t count, loff_t *ppos)
684 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
685 struct hl_device *hdev = entry->hdev;
689 rc = kstrtouint_from_user(buf, count, 10, &value);
693 value = value ? 1 : 0;
695 hl_debugfs_led_set(hdev, 1, value);
700 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
701 size_t count, loff_t *ppos)
703 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
704 struct hl_device *hdev = entry->hdev;
708 rc = kstrtouint_from_user(buf, count, 10, &value);
712 value = value ? 1 : 0;
714 hl_debugfs_led_set(hdev, 2, value);
719 static ssize_t hl_device_read(struct file *f, char __user *buf,
720 size_t count, loff_t *ppos)
729 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
730 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
731 strlen(tmp_buf) + 1);
736 static ssize_t hl_device_write(struct file *f, const char __user *buf,
737 size_t count, loff_t *ppos)
739 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
740 struct hl_device *hdev = entry->hdev;
743 /* don't allow partial writes */
747 simple_write_to_buffer(data, 29, ppos, buf, count);
749 if (strncmp("disable", data, strlen("disable")) == 0) {
750 hdev->disabled = true;
751 } else if (strncmp("enable", data, strlen("enable")) == 0) {
752 hdev->disabled = false;
753 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
754 hdev->asic_funcs->suspend(hdev);
755 } else if (strncmp("resume", data, strlen("resume")) == 0) {
756 hdev->asic_funcs->resume(hdev);
757 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
758 hdev->device_cpu_disabled = true;
761 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
768 static const struct file_operations hl_data32b_fops = {
769 .owner = THIS_MODULE,
770 .read = hl_data_read32,
771 .write = hl_data_write32
774 static const struct file_operations hl_i2c_data_fops = {
775 .owner = THIS_MODULE,
776 .read = hl_i2c_data_read,
777 .write = hl_i2c_data_write
780 static const struct file_operations hl_power_fops = {
781 .owner = THIS_MODULE,
782 .read = hl_get_power_state,
783 .write = hl_set_power_state
786 static const struct file_operations hl_led0_fops = {
787 .owner = THIS_MODULE,
788 .write = hl_led0_write
791 static const struct file_operations hl_led1_fops = {
792 .owner = THIS_MODULE,
793 .write = hl_led1_write
796 static const struct file_operations hl_led2_fops = {
797 .owner = THIS_MODULE,
798 .write = hl_led2_write
801 static const struct file_operations hl_device_fops = {
802 .owner = THIS_MODULE,
803 .read = hl_device_read,
804 .write = hl_device_write
807 static const struct hl_info_list hl_debugfs_list[] = {
808 {"command_buffers", command_buffers_show, NULL},
809 {"command_submission", command_submission_show, NULL},
810 {"command_submission_jobs", command_submission_jobs_show, NULL},
811 {"userptr", userptr_show, NULL},
812 {"vm", vm_show, NULL},
813 {"mmu", mmu_show, mmu_write},
816 static int hl_debugfs_open(struct inode *inode, struct file *file)
818 struct hl_debugfs_entry *node = inode->i_private;
820 return single_open(file, node->info_ent->show, node);
823 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
824 size_t count, loff_t *f_pos)
826 struct hl_debugfs_entry *node = file->f_inode->i_private;
828 if (node->info_ent->write)
829 return node->info_ent->write(file, buf, count, f_pos);
835 static const struct file_operations hl_debugfs_fops = {
836 .owner = THIS_MODULE,
837 .open = hl_debugfs_open,
839 .write = hl_debugfs_write,
841 .release = single_release,
844 void hl_debugfs_add_device(struct hl_device *hdev)
846 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
847 int count = ARRAY_SIZE(hl_debugfs_list);
848 struct hl_debugfs_entry *entry;
852 dev_entry->hdev = hdev;
853 dev_entry->entry_arr = kmalloc_array(count,
854 sizeof(struct hl_debugfs_entry),
856 if (!dev_entry->entry_arr)
859 INIT_LIST_HEAD(&dev_entry->file_list);
860 INIT_LIST_HEAD(&dev_entry->cb_list);
861 INIT_LIST_HEAD(&dev_entry->cs_list);
862 INIT_LIST_HEAD(&dev_entry->cs_job_list);
863 INIT_LIST_HEAD(&dev_entry->userptr_list);
864 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
865 mutex_init(&dev_entry->file_mutex);
866 spin_lock_init(&dev_entry->cb_spinlock);
867 spin_lock_init(&dev_entry->cs_spinlock);
868 spin_lock_init(&dev_entry->cs_job_spinlock);
869 spin_lock_init(&dev_entry->userptr_spinlock);
870 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
872 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
875 debugfs_create_x64("addr",
880 debugfs_create_file("data32",
886 debugfs_create_file("set_power_state",
892 debugfs_create_u8("i2c_bus",
895 &dev_entry->i2c_bus);
897 debugfs_create_u8("i2c_addr",
900 &dev_entry->i2c_addr);
902 debugfs_create_u8("i2c_reg",
905 &dev_entry->i2c_reg);
907 debugfs_create_file("i2c_data",
913 debugfs_create_file("led0",
919 debugfs_create_file("led1",
925 debugfs_create_file("led2",
931 debugfs_create_file("device",
937 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
939 ent = debugfs_create_file(hl_debugfs_list[i].name,
945 entry->info_ent = &hl_debugfs_list[i];
946 entry->dev_entry = dev_entry;
950 void hl_debugfs_remove_device(struct hl_device *hdev)
952 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
954 debugfs_remove_recursive(entry->root);
956 mutex_destroy(&entry->file_mutex);
957 kfree(entry->entry_arr);
960 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
962 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
964 mutex_lock(&dev_entry->file_mutex);
965 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
966 mutex_unlock(&dev_entry->file_mutex);
969 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
971 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
973 mutex_lock(&dev_entry->file_mutex);
974 list_del(&hpriv->debugfs_list);
975 mutex_unlock(&dev_entry->file_mutex);
978 void hl_debugfs_add_cb(struct hl_cb *cb)
980 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
982 spin_lock(&dev_entry->cb_spinlock);
983 list_add(&cb->debugfs_list, &dev_entry->cb_list);
984 spin_unlock(&dev_entry->cb_spinlock);
987 void hl_debugfs_remove_cb(struct hl_cb *cb)
989 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
991 spin_lock(&dev_entry->cb_spinlock);
992 list_del(&cb->debugfs_list);
993 spin_unlock(&dev_entry->cb_spinlock);
996 void hl_debugfs_add_cs(struct hl_cs *cs)
998 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1000 spin_lock(&dev_entry->cs_spinlock);
1001 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1002 spin_unlock(&dev_entry->cs_spinlock);
1005 void hl_debugfs_remove_cs(struct hl_cs *cs)
1007 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1009 spin_lock(&dev_entry->cs_spinlock);
1010 list_del(&cs->debugfs_list);
1011 spin_unlock(&dev_entry->cs_spinlock);
1014 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1016 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1018 spin_lock(&dev_entry->cs_job_spinlock);
1019 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1020 spin_unlock(&dev_entry->cs_job_spinlock);
1023 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1025 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1027 spin_lock(&dev_entry->cs_job_spinlock);
1028 list_del(&job->debugfs_list);
1029 spin_unlock(&dev_entry->cs_job_spinlock);
1032 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1034 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1036 spin_lock(&dev_entry->userptr_spinlock);
1037 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1038 spin_unlock(&dev_entry->userptr_spinlock);
1041 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1042 struct hl_userptr *userptr)
1044 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1046 spin_lock(&dev_entry->userptr_spinlock);
1047 list_del(&userptr->debugfs_list);
1048 spin_unlock(&dev_entry->userptr_spinlock);
1051 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1053 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1055 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1056 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1057 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1060 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1062 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1064 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1065 list_del(&ctx->debugfs_list);
1066 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1069 void __init hl_debugfs_init(void)
1071 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1074 void hl_debugfs_fini(void)
1076 debugfs_remove_recursive(hl_debug_root);