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;
238 if (!dev_entry->hdev->mmu_enable)
241 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
243 list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
245 seq_puts(s, "\n\n----------------------------------------------------");
246 seq_puts(s, "\n----------------------------------------------------\n\n");
247 seq_printf(s, "ctx asid: %u\n", ctx->asid);
249 seq_puts(s, "\nmappings:\n\n");
250 seq_puts(s, " virtual address size handle\n");
251 seq_puts(s, "----------------------------------------------------\n");
252 mutex_lock(&ctx->mem_hash_lock);
253 hash_for_each(ctx->mem_hash, i, hnode, node) {
254 vm_type = hnode->ptr;
256 if (*vm_type == VM_TYPE_USERPTR) {
257 userptr = hnode->ptr;
259 " 0x%-14llx %-10u\n",
260 hnode->vaddr, userptr->size);
262 phys_pg_pack = hnode->ptr;
264 " 0x%-14llx %-10llu %-4u\n",
265 hnode->vaddr, phys_pg_pack->total_size,
266 phys_pg_pack->handle);
269 mutex_unlock(&ctx->mem_hash_lock);
272 spin_lock(&vm->idr_lock);
274 if (!idr_is_empty(&vm->phys_pg_pack_handles))
275 seq_puts(s, "\n\nallocations:\n");
277 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
278 if (phys_pg_pack->asid != ctx->asid)
281 seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
282 seq_printf(s, "page size: %u\n\n",
283 phys_pg_pack->page_size);
284 seq_puts(s, " physical address\n");
285 seq_puts(s, "---------------------\n");
286 for (j = 0 ; j < phys_pg_pack->npages ; j++) {
287 seq_printf(s, " 0x%-14llx\n",
288 phys_pg_pack->pages[j]);
291 spin_unlock(&vm->idr_lock);
295 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
303 /* these inline functions are copied from mmu.c */
304 static inline u64 get_hop0_addr(struct hl_ctx *ctx)
306 return ctx->hdev->asic_prop.mmu_pgt_addr +
307 (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
310 static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
313 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
314 ((virt_addr & HOP0_MASK) >> HOP0_SHIFT);
317 static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
320 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
321 ((virt_addr & HOP1_MASK) >> HOP1_SHIFT);
324 static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
327 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
328 ((virt_addr & HOP2_MASK) >> HOP2_SHIFT);
331 static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
334 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
335 ((virt_addr & HOP3_MASK) >> HOP3_SHIFT);
338 static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
341 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
342 ((virt_addr & HOP4_MASK) >> HOP4_SHIFT);
345 static inline u64 get_next_hop_addr(u64 curr_pte)
347 if (curr_pte & PAGE_PRESENT_MASK)
348 return curr_pte & PHYS_ADDR_MASK;
353 static int mmu_show(struct seq_file *s, void *data)
355 struct hl_debugfs_entry *entry = s->private;
356 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
357 struct hl_device *hdev = dev_entry->hdev;
358 struct hl_ctx *ctx = hdev->user_ctx;
360 u64 hop0_addr = 0, hop0_pte_addr = 0, hop0_pte = 0,
361 hop1_addr = 0, hop1_pte_addr = 0, hop1_pte = 0,
362 hop2_addr = 0, hop2_pte_addr = 0, hop2_pte = 0,
363 hop3_addr = 0, hop3_pte_addr = 0, hop3_pte = 0,
364 hop4_addr = 0, hop4_pte_addr = 0, hop4_pte = 0,
365 virt_addr = dev_entry->mmu_addr;
367 if (!hdev->mmu_enable)
371 dev_err(hdev->dev, "no ctx available\n");
375 mutex_lock(&ctx->mmu_lock);
377 /* the following lookup is copied from unmap() in mmu.c */
379 hop0_addr = get_hop0_addr(ctx);
380 hop0_pte_addr = get_hop0_pte_addr(ctx, hop0_addr, virt_addr);
381 hop0_pte = hdev->asic_funcs->read_pte(hdev, hop0_pte_addr);
382 hop1_addr = get_next_hop_addr(hop0_pte);
384 if (hop1_addr == ULLONG_MAX)
387 hop1_pte_addr = get_hop1_pte_addr(ctx, hop1_addr, virt_addr);
388 hop1_pte = hdev->asic_funcs->read_pte(hdev, hop1_pte_addr);
389 hop2_addr = get_next_hop_addr(hop1_pte);
391 if (hop2_addr == ULLONG_MAX)
394 hop2_pte_addr = get_hop2_pte_addr(ctx, hop2_addr, virt_addr);
395 hop2_pte = hdev->asic_funcs->read_pte(hdev, hop2_pte_addr);
396 hop3_addr = get_next_hop_addr(hop2_pte);
398 if (hop3_addr == ULLONG_MAX)
401 hop3_pte_addr = get_hop3_pte_addr(ctx, hop3_addr, virt_addr);
402 hop3_pte = hdev->asic_funcs->read_pte(hdev, hop3_pte_addr);
404 if (!(hop3_pte & LAST_MASK)) {
405 hop4_addr = get_next_hop_addr(hop3_pte);
407 if (hop4_addr == ULLONG_MAX)
410 hop4_pte_addr = get_hop4_pte_addr(ctx, hop4_addr, virt_addr);
411 hop4_pte = hdev->asic_funcs->read_pte(hdev, hop4_pte_addr);
412 if (!(hop4_pte & PAGE_PRESENT_MASK))
415 if (!(hop3_pte & PAGE_PRESENT_MASK))
419 seq_printf(s, "asid: %u, virt_addr: 0x%llx\n",
420 dev_entry->mmu_asid, dev_entry->mmu_addr);
422 seq_printf(s, "hop0_addr: 0x%llx\n", hop0_addr);
423 seq_printf(s, "hop0_pte_addr: 0x%llx\n", hop0_pte_addr);
424 seq_printf(s, "hop0_pte: 0x%llx\n", hop0_pte);
426 seq_printf(s, "hop1_addr: 0x%llx\n", hop1_addr);
427 seq_printf(s, "hop1_pte_addr: 0x%llx\n", hop1_pte_addr);
428 seq_printf(s, "hop1_pte: 0x%llx\n", hop1_pte);
430 seq_printf(s, "hop2_addr: 0x%llx\n", hop2_addr);
431 seq_printf(s, "hop2_pte_addr: 0x%llx\n", hop2_pte_addr);
432 seq_printf(s, "hop2_pte: 0x%llx\n", hop2_pte);
434 seq_printf(s, "hop3_addr: 0x%llx\n", hop3_addr);
435 seq_printf(s, "hop3_pte_addr: 0x%llx\n", hop3_pte_addr);
436 seq_printf(s, "hop3_pte: 0x%llx\n", hop3_pte);
438 if (!(hop3_pte & LAST_MASK)) {
439 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
440 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
441 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
447 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
450 mutex_unlock(&ctx->mmu_lock);
455 static ssize_t mmu_write(struct file *file, const char __user *buf,
456 size_t count, loff_t *f_pos)
458 struct seq_file *s = file->private_data;
459 struct hl_debugfs_entry *entry = s->private;
460 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
461 struct hl_device *hdev = dev_entry->hdev;
462 char kbuf[MMU_KBUF_SIZE], asid_kbuf[MMU_ASID_BUF_SIZE],
463 addr_kbuf[MMU_ADDR_BUF_SIZE];
467 if (!hdev->mmu_enable)
470 memset(kbuf, 0, sizeof(kbuf));
471 memset(asid_kbuf, 0, sizeof(asid_kbuf));
472 memset(addr_kbuf, 0, sizeof(addr_kbuf));
474 if (copy_from_user(kbuf, buf, count))
477 kbuf[MMU_KBUF_SIZE - 1] = 0;
479 c = strchr(kbuf, ' ');
483 memcpy(asid_kbuf, kbuf, c - kbuf);
485 rc = kstrtouint(asid_kbuf, 10, &dev_entry->mmu_asid);
489 c = strstr(kbuf, " 0x");
494 memcpy(addr_kbuf, c, (kbuf + count) - c);
496 rc = kstrtoull(addr_kbuf, 16, &dev_entry->mmu_addr);
503 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
508 static ssize_t hl_data_read32(struct file *f, char __user *buf,
509 size_t count, loff_t *ppos)
511 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
512 struct hl_device *hdev = entry->hdev;
520 rc = hdev->asic_funcs->debugfs_read32(hdev, entry->addr, &val);
522 dev_err(hdev->dev, "Failed to read from 0x%010llx\n",
527 sprintf(tmp_buf, "0x%08x\n", val);
528 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
529 strlen(tmp_buf) + 1);
534 static ssize_t hl_data_write32(struct file *f, const 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;
542 rc = kstrtouint_from_user(buf, count, 16, &value);
546 rc = hdev->asic_funcs->debugfs_write32(hdev, entry->addr, value);
548 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
556 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
557 size_t count, loff_t *ppos)
559 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
560 struct hl_device *hdev = entry->hdev;
568 if (hdev->pdev->current_state == PCI_D0)
570 else if (hdev->pdev->current_state == PCI_D3hot)
576 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
577 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
578 strlen(tmp_buf) + 1);
583 static ssize_t hl_set_power_state(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;
591 rc = kstrtouint_from_user(buf, count, 10, &value);
596 pci_set_power_state(hdev->pdev, PCI_D0);
597 pci_restore_state(hdev->pdev);
598 rc = pci_enable_device(hdev->pdev);
599 } else if (value == 2) {
600 pci_save_state(hdev->pdev);
601 pci_disable_device(hdev->pdev);
602 pci_set_power_state(hdev->pdev, PCI_D3hot);
604 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
611 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
612 size_t count, loff_t *ppos)
614 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
615 struct hl_device *hdev = entry->hdev;
623 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
624 entry->i2c_reg, &val);
627 "Failed to read from I2C bus %d, addr %d, reg %d\n",
628 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
632 sprintf(tmp_buf, "0x%02x\n", val);
633 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
634 strlen(tmp_buf) + 1);
639 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
640 size_t count, loff_t *ppos)
642 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
643 struct hl_device *hdev = entry->hdev;
647 rc = kstrtouint_from_user(buf, count, 16, &value);
651 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
652 entry->i2c_reg, value);
655 "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
656 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
663 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
664 size_t count, loff_t *ppos)
666 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
667 struct hl_device *hdev = entry->hdev;
671 rc = kstrtouint_from_user(buf, count, 10, &value);
675 value = value ? 1 : 0;
677 hl_debugfs_led_set(hdev, 0, value);
682 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
683 size_t count, loff_t *ppos)
685 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
686 struct hl_device *hdev = entry->hdev;
690 rc = kstrtouint_from_user(buf, count, 10, &value);
694 value = value ? 1 : 0;
696 hl_debugfs_led_set(hdev, 1, value);
701 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
702 size_t count, loff_t *ppos)
704 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
705 struct hl_device *hdev = entry->hdev;
709 rc = kstrtouint_from_user(buf, count, 10, &value);
713 value = value ? 1 : 0;
715 hl_debugfs_led_set(hdev, 2, value);
720 static ssize_t hl_device_read(struct file *f, char __user *buf,
721 size_t count, loff_t *ppos)
730 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
731 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
732 strlen(tmp_buf) + 1);
737 static ssize_t hl_device_write(struct file *f, const char __user *buf,
738 size_t count, loff_t *ppos)
740 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
741 struct hl_device *hdev = entry->hdev;
744 /* don't allow partial writes */
748 simple_write_to_buffer(data, 29, ppos, buf, count);
750 if (strncmp("disable", data, strlen("disable")) == 0) {
751 hdev->disabled = true;
752 } else if (strncmp("enable", data, strlen("enable")) == 0) {
753 hdev->disabled = false;
754 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
755 hdev->asic_funcs->suspend(hdev);
756 } else if (strncmp("resume", data, strlen("resume")) == 0) {
757 hdev->asic_funcs->resume(hdev);
758 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
759 hdev->device_cpu_disabled = true;
762 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
769 static const struct file_operations hl_data32b_fops = {
770 .owner = THIS_MODULE,
771 .read = hl_data_read32,
772 .write = hl_data_write32
775 static const struct file_operations hl_i2c_data_fops = {
776 .owner = THIS_MODULE,
777 .read = hl_i2c_data_read,
778 .write = hl_i2c_data_write
781 static const struct file_operations hl_power_fops = {
782 .owner = THIS_MODULE,
783 .read = hl_get_power_state,
784 .write = hl_set_power_state
787 static const struct file_operations hl_led0_fops = {
788 .owner = THIS_MODULE,
789 .write = hl_led0_write
792 static const struct file_operations hl_led1_fops = {
793 .owner = THIS_MODULE,
794 .write = hl_led1_write
797 static const struct file_operations hl_led2_fops = {
798 .owner = THIS_MODULE,
799 .write = hl_led2_write
802 static const struct file_operations hl_device_fops = {
803 .owner = THIS_MODULE,
804 .read = hl_device_read,
805 .write = hl_device_write
808 static const struct hl_info_list hl_debugfs_list[] = {
809 {"command_buffers", command_buffers_show, NULL},
810 {"command_submission", command_submission_show, NULL},
811 {"command_submission_jobs", command_submission_jobs_show, NULL},
812 {"userptr", userptr_show, NULL},
813 {"vm", vm_show, NULL},
814 {"mmu", mmu_show, mmu_write},
817 static int hl_debugfs_open(struct inode *inode, struct file *file)
819 struct hl_debugfs_entry *node = inode->i_private;
821 return single_open(file, node->info_ent->show, node);
824 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
825 size_t count, loff_t *f_pos)
827 struct hl_debugfs_entry *node = file->f_inode->i_private;
829 if (node->info_ent->write)
830 return node->info_ent->write(file, buf, count, f_pos);
836 static const struct file_operations hl_debugfs_fops = {
837 .owner = THIS_MODULE,
838 .open = hl_debugfs_open,
840 .write = hl_debugfs_write,
842 .release = single_release,
845 void hl_debugfs_add_device(struct hl_device *hdev)
847 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
848 int count = ARRAY_SIZE(hl_debugfs_list);
849 struct hl_debugfs_entry *entry;
853 dev_entry->hdev = hdev;
854 dev_entry->entry_arr = kmalloc_array(count,
855 sizeof(struct hl_debugfs_entry),
857 if (!dev_entry->entry_arr)
860 INIT_LIST_HEAD(&dev_entry->file_list);
861 INIT_LIST_HEAD(&dev_entry->cb_list);
862 INIT_LIST_HEAD(&dev_entry->cs_list);
863 INIT_LIST_HEAD(&dev_entry->cs_job_list);
864 INIT_LIST_HEAD(&dev_entry->userptr_list);
865 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
866 mutex_init(&dev_entry->file_mutex);
867 spin_lock_init(&dev_entry->cb_spinlock);
868 spin_lock_init(&dev_entry->cs_spinlock);
869 spin_lock_init(&dev_entry->cs_job_spinlock);
870 spin_lock_init(&dev_entry->userptr_spinlock);
871 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
873 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
876 debugfs_create_x64("addr",
881 debugfs_create_file("data32",
887 debugfs_create_file("set_power_state",
893 debugfs_create_u8("i2c_bus",
896 &dev_entry->i2c_bus);
898 debugfs_create_u8("i2c_addr",
901 &dev_entry->i2c_addr);
903 debugfs_create_u8("i2c_reg",
906 &dev_entry->i2c_reg);
908 debugfs_create_file("i2c_data",
914 debugfs_create_file("led0",
920 debugfs_create_file("led1",
926 debugfs_create_file("led2",
932 debugfs_create_file("device",
938 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
940 ent = debugfs_create_file(hl_debugfs_list[i].name,
946 entry->info_ent = &hl_debugfs_list[i];
947 entry->dev_entry = dev_entry;
951 void hl_debugfs_remove_device(struct hl_device *hdev)
953 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
955 debugfs_remove_recursive(entry->root);
957 mutex_destroy(&entry->file_mutex);
958 kfree(entry->entry_arr);
961 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
963 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
965 mutex_lock(&dev_entry->file_mutex);
966 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
967 mutex_unlock(&dev_entry->file_mutex);
970 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
972 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
974 mutex_lock(&dev_entry->file_mutex);
975 list_del(&hpriv->debugfs_list);
976 mutex_unlock(&dev_entry->file_mutex);
979 void hl_debugfs_add_cb(struct hl_cb *cb)
981 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
983 spin_lock(&dev_entry->cb_spinlock);
984 list_add(&cb->debugfs_list, &dev_entry->cb_list);
985 spin_unlock(&dev_entry->cb_spinlock);
988 void hl_debugfs_remove_cb(struct hl_cb *cb)
990 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
992 spin_lock(&dev_entry->cb_spinlock);
993 list_del(&cb->debugfs_list);
994 spin_unlock(&dev_entry->cb_spinlock);
997 void hl_debugfs_add_cs(struct hl_cs *cs)
999 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1001 spin_lock(&dev_entry->cs_spinlock);
1002 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1003 spin_unlock(&dev_entry->cs_spinlock);
1006 void hl_debugfs_remove_cs(struct hl_cs *cs)
1008 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1010 spin_lock(&dev_entry->cs_spinlock);
1011 list_del(&cs->debugfs_list);
1012 spin_unlock(&dev_entry->cs_spinlock);
1015 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1017 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1019 spin_lock(&dev_entry->cs_job_spinlock);
1020 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1021 spin_unlock(&dev_entry->cs_job_spinlock);
1024 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1026 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1028 spin_lock(&dev_entry->cs_job_spinlock);
1029 list_del(&job->debugfs_list);
1030 spin_unlock(&dev_entry->cs_job_spinlock);
1033 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1035 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1037 spin_lock(&dev_entry->userptr_spinlock);
1038 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1039 spin_unlock(&dev_entry->userptr_spinlock);
1042 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1043 struct hl_userptr *userptr)
1045 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1047 spin_lock(&dev_entry->userptr_spinlock);
1048 list_del(&userptr->debugfs_list);
1049 spin_unlock(&dev_entry->userptr_spinlock);
1052 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1054 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1056 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1057 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1058 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1061 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1063 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1065 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1066 list_del(&ctx->debugfs_list);
1067 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1070 void __init hl_debugfs_init(void)
1072 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1075 void hl_debugfs_fini(void)
1077 debugfs_remove_recursive(hl_debug_root);