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_hopN_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
311 u64 virt_addr, u64 mask, u64 shift)
313 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
314 ((virt_addr & mask) >> shift);
317 static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx,
318 struct hl_mmu_properties *mmu_specs,
319 u64 hop_addr, u64 vaddr)
321 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop0_mask,
322 mmu_specs->hop0_shift);
325 static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx,
326 struct hl_mmu_properties *mmu_specs,
327 u64 hop_addr, u64 vaddr)
329 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop1_mask,
330 mmu_specs->hop1_shift);
333 static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx,
334 struct hl_mmu_properties *mmu_specs,
335 u64 hop_addr, u64 vaddr)
337 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop2_mask,
338 mmu_specs->hop2_shift);
341 static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx,
342 struct hl_mmu_properties *mmu_specs,
343 u64 hop_addr, u64 vaddr)
345 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop3_mask,
346 mmu_specs->hop3_shift);
349 static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx,
350 struct hl_mmu_properties *mmu_specs,
351 u64 hop_addr, u64 vaddr)
353 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop4_mask,
354 mmu_specs->hop4_shift);
357 static inline u64 get_next_hop_addr(u64 curr_pte)
359 if (curr_pte & PAGE_PRESENT_MASK)
360 return curr_pte & HOP_PHYS_ADDR_MASK;
365 static int mmu_show(struct seq_file *s, void *data)
367 struct hl_debugfs_entry *entry = s->private;
368 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
369 struct hl_device *hdev = dev_entry->hdev;
370 struct asic_fixed_properties *prop = &hdev->asic_prop;
371 struct hl_mmu_properties *mmu_prop;
375 u64 hop0_addr = 0, hop0_pte_addr = 0, hop0_pte = 0,
376 hop1_addr = 0, hop1_pte_addr = 0, hop1_pte = 0,
377 hop2_addr = 0, hop2_pte_addr = 0, hop2_pte = 0,
378 hop3_addr = 0, hop3_pte_addr = 0, hop3_pte = 0,
379 hop4_addr = 0, hop4_pte_addr = 0, hop4_pte = 0,
380 virt_addr = dev_entry->mmu_addr;
382 if (!hdev->mmu_enable)
385 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
386 ctx = hdev->kernel_ctx;
388 ctx = hdev->compute_ctx;
391 dev_err(hdev->dev, "no ctx available\n");
395 is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
396 prop->dmmu.start_addr,
397 prop->dmmu.end_addr);
399 /* shifts and masks are the same in PMMU and HPMMU, use one of them */
400 mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
402 mutex_lock(&ctx->mmu_lock);
404 /* the following lookup is copied from unmap() in mmu.c */
406 hop0_addr = get_hop0_addr(ctx);
407 hop0_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop0_addr, virt_addr);
408 hop0_pte = hdev->asic_funcs->read_pte(hdev, hop0_pte_addr);
409 hop1_addr = get_next_hop_addr(hop0_pte);
411 if (hop1_addr == ULLONG_MAX)
414 hop1_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop1_addr, virt_addr);
415 hop1_pte = hdev->asic_funcs->read_pte(hdev, hop1_pte_addr);
416 hop2_addr = get_next_hop_addr(hop1_pte);
418 if (hop2_addr == ULLONG_MAX)
421 hop2_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop2_addr, virt_addr);
422 hop2_pte = hdev->asic_funcs->read_pte(hdev, hop2_pte_addr);
423 hop3_addr = get_next_hop_addr(hop2_pte);
425 if (hop3_addr == ULLONG_MAX)
428 hop3_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop3_addr, virt_addr);
429 hop3_pte = hdev->asic_funcs->read_pte(hdev, hop3_pte_addr);
431 if (!(hop3_pte & LAST_MASK)) {
432 hop4_addr = get_next_hop_addr(hop3_pte);
434 if (hop4_addr == ULLONG_MAX)
437 hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop4_addr,
439 hop4_pte = hdev->asic_funcs->read_pte(hdev, hop4_pte_addr);
440 if (!(hop4_pte & PAGE_PRESENT_MASK))
443 if (!(hop3_pte & PAGE_PRESENT_MASK))
447 seq_printf(s, "asid: %u, virt_addr: 0x%llx\n",
448 dev_entry->mmu_asid, dev_entry->mmu_addr);
450 seq_printf(s, "hop0_addr: 0x%llx\n", hop0_addr);
451 seq_printf(s, "hop0_pte_addr: 0x%llx\n", hop0_pte_addr);
452 seq_printf(s, "hop0_pte: 0x%llx\n", hop0_pte);
454 seq_printf(s, "hop1_addr: 0x%llx\n", hop1_addr);
455 seq_printf(s, "hop1_pte_addr: 0x%llx\n", hop1_pte_addr);
456 seq_printf(s, "hop1_pte: 0x%llx\n", hop1_pte);
458 seq_printf(s, "hop2_addr: 0x%llx\n", hop2_addr);
459 seq_printf(s, "hop2_pte_addr: 0x%llx\n", hop2_pte_addr);
460 seq_printf(s, "hop2_pte: 0x%llx\n", hop2_pte);
462 seq_printf(s, "hop3_addr: 0x%llx\n", hop3_addr);
463 seq_printf(s, "hop3_pte_addr: 0x%llx\n", hop3_pte_addr);
464 seq_printf(s, "hop3_pte: 0x%llx\n", hop3_pte);
466 if (!(hop3_pte & LAST_MASK)) {
467 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
468 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
469 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
475 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
478 mutex_unlock(&ctx->mmu_lock);
483 static ssize_t mmu_write(struct file *file, const char __user *buf,
484 size_t count, loff_t *f_pos)
486 struct seq_file *s = file->private_data;
487 struct hl_debugfs_entry *entry = s->private;
488 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
489 struct hl_device *hdev = dev_entry->hdev;
490 char kbuf[MMU_KBUF_SIZE];
494 if (!hdev->mmu_enable)
497 if (count > sizeof(kbuf) - 1)
499 if (copy_from_user(kbuf, buf, count))
503 c = strchr(kbuf, ' ');
508 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
512 if (strncmp(c+1, "0x", 2))
514 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
521 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
526 static int engines_show(struct seq_file *s, void *data)
528 struct hl_debugfs_entry *entry = s->private;
529 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
530 struct hl_device *hdev = dev_entry->hdev;
532 if (atomic_read(&hdev->in_reset)) {
533 dev_warn_ratelimited(hdev->dev,
534 "Can't check device idle during reset\n");
538 hdev->asic_funcs->is_device_idle(hdev, NULL, s);
543 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
545 struct asic_fixed_properties *prop = &hdev->asic_prop;
547 if (!hdev->mmu_enable)
550 if (hdev->dram_supports_virtual_memory &&
551 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
554 if (addr >= prop->pmmu.start_addr &&
555 addr < prop->pmmu.end_addr)
558 if (addr >= prop->pmmu_huge.start_addr &&
559 addr < prop->pmmu_huge.end_addr)
565 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr,
568 struct hl_ctx *ctx = hdev->compute_ctx;
569 struct asic_fixed_properties *prop = &hdev->asic_prop;
570 struct hl_mmu_properties *mmu_prop;
571 u64 hop_addr, hop_pte_addr, hop_pte;
572 u64 offset_mask = HOP4_MASK | FLAGS_MASK;
577 dev_err(hdev->dev, "no ctx available\n");
581 is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
582 prop->dmmu.start_addr,
583 prop->dmmu.end_addr);
585 /* shifts and masks are the same in PMMU and HPMMU, use one of them */
586 mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
588 mutex_lock(&ctx->mmu_lock);
591 hop_addr = get_hop0_addr(ctx);
592 hop_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
593 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
596 hop_addr = get_next_hop_addr(hop_pte);
597 if (hop_addr == ULLONG_MAX)
599 hop_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
600 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
603 hop_addr = get_next_hop_addr(hop_pte);
604 if (hop_addr == ULLONG_MAX)
606 hop_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
607 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
610 hop_addr = get_next_hop_addr(hop_pte);
611 if (hop_addr == ULLONG_MAX)
613 hop_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
614 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
616 if (!(hop_pte & LAST_MASK)) {
618 hop_addr = get_next_hop_addr(hop_pte);
619 if (hop_addr == ULLONG_MAX)
621 hop_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop_addr,
623 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
625 offset_mask = FLAGS_MASK;
628 if (!(hop_pte & PAGE_PRESENT_MASK))
631 *phys_addr = (hop_pte & ~offset_mask) | (virt_addr & offset_mask);
636 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
640 mutex_unlock(&ctx->mmu_lock);
644 static ssize_t hl_data_read32(struct file *f, char __user *buf,
645 size_t count, loff_t *ppos)
647 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
648 struct hl_device *hdev = entry->hdev;
650 u64 addr = entry->addr;
654 if (atomic_read(&hdev->in_reset)) {
655 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
662 if (hl_is_device_va(hdev, addr)) {
663 rc = device_va_to_pa(hdev, addr, &addr);
668 rc = hdev->asic_funcs->debugfs_read32(hdev, addr, &val);
670 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
674 sprintf(tmp_buf, "0x%08x\n", val);
675 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
679 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
680 size_t count, loff_t *ppos)
682 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
683 struct hl_device *hdev = entry->hdev;
684 u64 addr = entry->addr;
688 if (atomic_read(&hdev->in_reset)) {
689 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
693 rc = kstrtouint_from_user(buf, count, 16, &value);
697 if (hl_is_device_va(hdev, addr)) {
698 rc = device_va_to_pa(hdev, addr, &addr);
703 rc = hdev->asic_funcs->debugfs_write32(hdev, addr, value);
705 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
713 static ssize_t hl_data_read64(struct file *f, char __user *buf,
714 size_t count, loff_t *ppos)
716 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
717 struct hl_device *hdev = entry->hdev;
719 u64 addr = entry->addr;
726 if (hl_is_device_va(hdev, addr)) {
727 rc = device_va_to_pa(hdev, addr, &addr);
732 rc = hdev->asic_funcs->debugfs_read64(hdev, addr, &val);
734 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
738 sprintf(tmp_buf, "0x%016llx\n", val);
739 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
743 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
744 size_t count, loff_t *ppos)
746 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
747 struct hl_device *hdev = entry->hdev;
748 u64 addr = entry->addr;
752 rc = kstrtoull_from_user(buf, count, 16, &value);
756 if (hl_is_device_va(hdev, addr)) {
757 rc = device_va_to_pa(hdev, addr, &addr);
762 rc = hdev->asic_funcs->debugfs_write64(hdev, addr, value);
764 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
772 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
773 size_t count, loff_t *ppos)
775 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
776 struct hl_device *hdev = entry->hdev;
783 if (hdev->pdev->current_state == PCI_D0)
785 else if (hdev->pdev->current_state == PCI_D3hot)
791 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
792 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
796 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
797 size_t count, loff_t *ppos)
799 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
800 struct hl_device *hdev = entry->hdev;
804 rc = kstrtouint_from_user(buf, count, 10, &value);
809 pci_set_power_state(hdev->pdev, PCI_D0);
810 pci_restore_state(hdev->pdev);
811 rc = pci_enable_device(hdev->pdev);
812 } else if (value == 2) {
813 pci_save_state(hdev->pdev);
814 pci_disable_device(hdev->pdev);
815 pci_set_power_state(hdev->pdev, PCI_D3hot);
817 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
824 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
825 size_t count, loff_t *ppos)
827 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
828 struct hl_device *hdev = entry->hdev;
836 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
837 entry->i2c_reg, &val);
840 "Failed to read from I2C bus %d, addr %d, reg %d\n",
841 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
845 sprintf(tmp_buf, "0x%02x\n", val);
846 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
852 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
853 size_t count, loff_t *ppos)
855 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
856 struct hl_device *hdev = entry->hdev;
860 rc = kstrtouint_from_user(buf, count, 16, &value);
864 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
865 entry->i2c_reg, value);
868 "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
869 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
876 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
877 size_t count, loff_t *ppos)
879 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
880 struct hl_device *hdev = entry->hdev;
884 rc = kstrtouint_from_user(buf, count, 10, &value);
888 value = value ? 1 : 0;
890 hl_debugfs_led_set(hdev, 0, value);
895 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
896 size_t count, loff_t *ppos)
898 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
899 struct hl_device *hdev = entry->hdev;
903 rc = kstrtouint_from_user(buf, count, 10, &value);
907 value = value ? 1 : 0;
909 hl_debugfs_led_set(hdev, 1, value);
914 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
915 size_t count, loff_t *ppos)
917 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
918 struct hl_device *hdev = entry->hdev;
922 rc = kstrtouint_from_user(buf, count, 10, &value);
926 value = value ? 1 : 0;
928 hl_debugfs_led_set(hdev, 2, value);
933 static ssize_t hl_device_read(struct file *f, char __user *buf,
934 size_t count, loff_t *ppos)
936 static const char *help =
937 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
938 return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
941 static ssize_t hl_device_write(struct file *f, const char __user *buf,
942 size_t count, loff_t *ppos)
944 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
945 struct hl_device *hdev = entry->hdev;
948 /* don't allow partial writes */
952 simple_write_to_buffer(data, 29, ppos, buf, count);
954 if (strncmp("disable", data, strlen("disable")) == 0) {
955 hdev->disabled = true;
956 } else if (strncmp("enable", data, strlen("enable")) == 0) {
957 hdev->disabled = false;
958 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
959 hdev->asic_funcs->suspend(hdev);
960 } else if (strncmp("resume", data, strlen("resume")) == 0) {
961 hdev->asic_funcs->resume(hdev);
962 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
963 hdev->device_cpu_disabled = true;
966 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
973 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
974 size_t count, loff_t *ppos)
976 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
977 struct hl_device *hdev = entry->hdev;
984 sprintf(tmp_buf, "%d\n", hdev->clock_gating);
985 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
986 strlen(tmp_buf) + 1);
991 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
992 size_t count, loff_t *ppos)
994 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
995 struct hl_device *hdev = entry->hdev;
999 if (atomic_read(&hdev->in_reset)) {
1000 dev_warn_ratelimited(hdev->dev,
1001 "Can't change clock gating during reset\n");
1005 rc = kstrtouint_from_user(buf, count, 10, &value);
1010 hdev->clock_gating = 1;
1011 if (hdev->asic_funcs->enable_clock_gating)
1012 hdev->asic_funcs->enable_clock_gating(hdev);
1014 if (hdev->asic_funcs->disable_clock_gating)
1015 hdev->asic_funcs->disable_clock_gating(hdev);
1016 hdev->clock_gating = 0;
1022 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1023 size_t count, loff_t *ppos)
1025 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1026 struct hl_device *hdev = entry->hdev;
1033 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1034 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1035 strlen(tmp_buf) + 1);
1040 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1041 size_t count, loff_t *ppos)
1043 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1044 struct hl_device *hdev = entry->hdev;
1048 if (atomic_read(&hdev->in_reset)) {
1049 dev_warn_ratelimited(hdev->dev,
1050 "Can't change stop on error during reset\n");
1054 rc = kstrtouint_from_user(buf, count, 10, &value);
1058 hdev->stop_on_err = value ? 1 : 0;
1060 hl_device_reset(hdev, false, false);
1065 static const struct file_operations hl_data32b_fops = {
1066 .owner = THIS_MODULE,
1067 .read = hl_data_read32,
1068 .write = hl_data_write32
1071 static const struct file_operations hl_data64b_fops = {
1072 .owner = THIS_MODULE,
1073 .read = hl_data_read64,
1074 .write = hl_data_write64
1077 static const struct file_operations hl_i2c_data_fops = {
1078 .owner = THIS_MODULE,
1079 .read = hl_i2c_data_read,
1080 .write = hl_i2c_data_write
1083 static const struct file_operations hl_power_fops = {
1084 .owner = THIS_MODULE,
1085 .read = hl_get_power_state,
1086 .write = hl_set_power_state
1089 static const struct file_operations hl_led0_fops = {
1090 .owner = THIS_MODULE,
1091 .write = hl_led0_write
1094 static const struct file_operations hl_led1_fops = {
1095 .owner = THIS_MODULE,
1096 .write = hl_led1_write
1099 static const struct file_operations hl_led2_fops = {
1100 .owner = THIS_MODULE,
1101 .write = hl_led2_write
1104 static const struct file_operations hl_device_fops = {
1105 .owner = THIS_MODULE,
1106 .read = hl_device_read,
1107 .write = hl_device_write
1110 static const struct file_operations hl_clk_gate_fops = {
1111 .owner = THIS_MODULE,
1112 .read = hl_clk_gate_read,
1113 .write = hl_clk_gate_write
1116 static const struct file_operations hl_stop_on_err_fops = {
1117 .owner = THIS_MODULE,
1118 .read = hl_stop_on_err_read,
1119 .write = hl_stop_on_err_write
1122 static const struct hl_info_list hl_debugfs_list[] = {
1123 {"command_buffers", command_buffers_show, NULL},
1124 {"command_submission", command_submission_show, NULL},
1125 {"command_submission_jobs", command_submission_jobs_show, NULL},
1126 {"userptr", userptr_show, NULL},
1127 {"vm", vm_show, NULL},
1128 {"mmu", mmu_show, mmu_write},
1129 {"engines", engines_show, NULL}
1132 static int hl_debugfs_open(struct inode *inode, struct file *file)
1134 struct hl_debugfs_entry *node = inode->i_private;
1136 return single_open(file, node->info_ent->show, node);
1139 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1140 size_t count, loff_t *f_pos)
1142 struct hl_debugfs_entry *node = file->f_inode->i_private;
1144 if (node->info_ent->write)
1145 return node->info_ent->write(file, buf, count, f_pos);
1151 static const struct file_operations hl_debugfs_fops = {
1152 .owner = THIS_MODULE,
1153 .open = hl_debugfs_open,
1155 .write = hl_debugfs_write,
1156 .llseek = seq_lseek,
1157 .release = single_release,
1160 void hl_debugfs_add_device(struct hl_device *hdev)
1162 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1163 int count = ARRAY_SIZE(hl_debugfs_list);
1164 struct hl_debugfs_entry *entry;
1168 dev_entry->hdev = hdev;
1169 dev_entry->entry_arr = kmalloc_array(count,
1170 sizeof(struct hl_debugfs_entry),
1172 if (!dev_entry->entry_arr)
1175 INIT_LIST_HEAD(&dev_entry->file_list);
1176 INIT_LIST_HEAD(&dev_entry->cb_list);
1177 INIT_LIST_HEAD(&dev_entry->cs_list);
1178 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1179 INIT_LIST_HEAD(&dev_entry->userptr_list);
1180 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1181 mutex_init(&dev_entry->file_mutex);
1182 spin_lock_init(&dev_entry->cb_spinlock);
1183 spin_lock_init(&dev_entry->cs_spinlock);
1184 spin_lock_init(&dev_entry->cs_job_spinlock);
1185 spin_lock_init(&dev_entry->userptr_spinlock);
1186 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1188 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1191 debugfs_create_x64("addr",
1196 debugfs_create_file("data32",
1202 debugfs_create_file("data64",
1208 debugfs_create_file("set_power_state",
1214 debugfs_create_u8("i2c_bus",
1217 &dev_entry->i2c_bus);
1219 debugfs_create_u8("i2c_addr",
1222 &dev_entry->i2c_addr);
1224 debugfs_create_u8("i2c_reg",
1227 &dev_entry->i2c_reg);
1229 debugfs_create_file("i2c_data",
1235 debugfs_create_file("led0",
1241 debugfs_create_file("led1",
1247 debugfs_create_file("led2",
1253 debugfs_create_file("device",
1259 debugfs_create_file("clk_gate",
1265 debugfs_create_file("stop_on_err",
1269 &hl_stop_on_err_fops);
1271 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1273 ent = debugfs_create_file(hl_debugfs_list[i].name,
1279 entry->info_ent = &hl_debugfs_list[i];
1280 entry->dev_entry = dev_entry;
1284 void hl_debugfs_remove_device(struct hl_device *hdev)
1286 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1288 debugfs_remove_recursive(entry->root);
1290 mutex_destroy(&entry->file_mutex);
1291 kfree(entry->entry_arr);
1294 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1296 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1298 mutex_lock(&dev_entry->file_mutex);
1299 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1300 mutex_unlock(&dev_entry->file_mutex);
1303 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1305 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1307 mutex_lock(&dev_entry->file_mutex);
1308 list_del(&hpriv->debugfs_list);
1309 mutex_unlock(&dev_entry->file_mutex);
1312 void hl_debugfs_add_cb(struct hl_cb *cb)
1314 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1316 spin_lock(&dev_entry->cb_spinlock);
1317 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1318 spin_unlock(&dev_entry->cb_spinlock);
1321 void hl_debugfs_remove_cb(struct hl_cb *cb)
1323 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1325 spin_lock(&dev_entry->cb_spinlock);
1326 list_del(&cb->debugfs_list);
1327 spin_unlock(&dev_entry->cb_spinlock);
1330 void hl_debugfs_add_cs(struct hl_cs *cs)
1332 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1334 spin_lock(&dev_entry->cs_spinlock);
1335 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1336 spin_unlock(&dev_entry->cs_spinlock);
1339 void hl_debugfs_remove_cs(struct hl_cs *cs)
1341 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1343 spin_lock(&dev_entry->cs_spinlock);
1344 list_del(&cs->debugfs_list);
1345 spin_unlock(&dev_entry->cs_spinlock);
1348 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1350 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1352 spin_lock(&dev_entry->cs_job_spinlock);
1353 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1354 spin_unlock(&dev_entry->cs_job_spinlock);
1357 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1359 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1361 spin_lock(&dev_entry->cs_job_spinlock);
1362 list_del(&job->debugfs_list);
1363 spin_unlock(&dev_entry->cs_job_spinlock);
1366 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1368 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1370 spin_lock(&dev_entry->userptr_spinlock);
1371 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1372 spin_unlock(&dev_entry->userptr_spinlock);
1375 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1376 struct hl_userptr *userptr)
1378 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1380 spin_lock(&dev_entry->userptr_spinlock);
1381 list_del(&userptr->debugfs_list);
1382 spin_unlock(&dev_entry->userptr_spinlock);
1385 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1387 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1389 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1390 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1391 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1394 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1396 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1398 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1399 list_del(&ctx->debugfs_list);
1400 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1403 void __init hl_debugfs_init(void)
1405 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1408 void hl_debugfs_fini(void)
1410 debugfs_remove_recursive(hl_debug_root);