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;
27 if (hl_device_disabled_or_in_reset(hdev))
30 memset(&pkt, 0, sizeof(pkt));
32 pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_RD <<
33 CPUCP_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),
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 cpucp_packet pkt;
53 if (hl_device_disabled_or_in_reset(hdev))
56 memset(&pkt, 0, sizeof(pkt));
58 pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_WR <<
59 CPUCP_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),
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 cpucp_packet pkt;
79 if (hl_device_disabled_or_in_reset(hdev))
82 memset(&pkt, 0, sizeof(pkt));
84 pkt.ctl = cpu_to_le32(CPUCP_PACKET_LED_SET <<
85 CPUCP_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),
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 " %03llu %d 0x%08x %d %d %d\n",
114 cb->id, cb->ctx->asid, 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_hop5_pte_addr(struct hl_ctx *ctx,
358 struct hl_mmu_properties *mmu_specs,
359 u64 hop_addr, u64 vaddr)
361 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop5_mask,
362 mmu_specs->hop5_shift);
365 static inline u64 get_next_hop_addr(u64 curr_pte)
367 if (curr_pte & PAGE_PRESENT_MASK)
368 return curr_pte & HOP_PHYS_ADDR_MASK;
373 static int mmu_show(struct seq_file *s, void *data)
375 struct hl_debugfs_entry *entry = s->private;
376 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
377 struct hl_device *hdev = dev_entry->hdev;
378 struct asic_fixed_properties *prop = &hdev->asic_prop;
379 struct hl_mmu_properties *mmu_prop;
383 u64 hop0_addr = 0, hop0_pte_addr = 0, hop0_pte = 0,
384 hop1_addr = 0, hop1_pte_addr = 0, hop1_pte = 0,
385 hop2_addr = 0, hop2_pte_addr = 0, hop2_pte = 0,
386 hop3_addr = 0, hop3_pte_addr = 0, hop3_pte = 0,
387 hop4_addr = 0, hop4_pte_addr = 0, hop4_pte = 0,
388 hop5_addr = 0, hop5_pte_addr = 0, hop5_pte = 0,
389 virt_addr = dev_entry->mmu_addr;
391 if (!hdev->mmu_enable)
394 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
395 ctx = hdev->kernel_ctx;
397 ctx = hdev->compute_ctx;
400 dev_err(hdev->dev, "no ctx available\n");
404 is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
405 prop->dmmu.start_addr,
406 prop->dmmu.end_addr);
408 /* shifts and masks are the same in PMMU and HPMMU, use one of them */
409 mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
411 mutex_lock(&ctx->mmu_lock);
413 /* the following lookup is copied from unmap() in mmu.c */
415 hop0_addr = get_hop0_addr(ctx);
416 hop0_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop0_addr, virt_addr);
417 hop0_pte = hdev->asic_funcs->read_pte(hdev, hop0_pte_addr);
418 hop1_addr = get_next_hop_addr(hop0_pte);
420 if (hop1_addr == ULLONG_MAX)
423 hop1_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop1_addr, virt_addr);
424 hop1_pte = hdev->asic_funcs->read_pte(hdev, hop1_pte_addr);
425 hop2_addr = get_next_hop_addr(hop1_pte);
427 if (hop2_addr == ULLONG_MAX)
430 hop2_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop2_addr, virt_addr);
431 hop2_pte = hdev->asic_funcs->read_pte(hdev, hop2_pte_addr);
432 hop3_addr = get_next_hop_addr(hop2_pte);
434 if (hop3_addr == ULLONG_MAX)
437 hop3_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop3_addr, virt_addr);
438 hop3_pte = hdev->asic_funcs->read_pte(hdev, hop3_pte_addr);
440 if (mmu_prop->num_hops == MMU_ARCH_5_HOPS) {
441 if (!(hop3_pte & LAST_MASK)) {
442 hop4_addr = get_next_hop_addr(hop3_pte);
444 if (hop4_addr == ULLONG_MAX)
447 hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop,
448 hop4_addr, virt_addr);
449 hop4_pte = hdev->asic_funcs->read_pte(hdev,
451 if (!(hop4_pte & PAGE_PRESENT_MASK))
454 if (!(hop3_pte & PAGE_PRESENT_MASK))
458 hop4_addr = get_next_hop_addr(hop3_pte);
460 if (hop4_addr == ULLONG_MAX)
463 hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop,
464 hop4_addr, virt_addr);
465 hop4_pte = hdev->asic_funcs->read_pte(hdev,
467 if (!(hop4_pte & LAST_MASK)) {
468 hop5_addr = get_next_hop_addr(hop4_pte);
470 if (hop5_addr == ULLONG_MAX)
473 hop5_pte_addr = get_hop5_pte_addr(ctx, mmu_prop,
474 hop5_addr, virt_addr);
475 hop5_pte = hdev->asic_funcs->read_pte(hdev,
477 if (!(hop5_pte & PAGE_PRESENT_MASK))
480 if (!(hop4_pte & PAGE_PRESENT_MASK))
485 seq_printf(s, "asid: %u, virt_addr: 0x%llx\n",
486 dev_entry->mmu_asid, dev_entry->mmu_addr);
488 seq_printf(s, "hop0_addr: 0x%llx\n", hop0_addr);
489 seq_printf(s, "hop0_pte_addr: 0x%llx\n", hop0_pte_addr);
490 seq_printf(s, "hop0_pte: 0x%llx\n", hop0_pte);
492 seq_printf(s, "hop1_addr: 0x%llx\n", hop1_addr);
493 seq_printf(s, "hop1_pte_addr: 0x%llx\n", hop1_pte_addr);
494 seq_printf(s, "hop1_pte: 0x%llx\n", hop1_pte);
496 seq_printf(s, "hop2_addr: 0x%llx\n", hop2_addr);
497 seq_printf(s, "hop2_pte_addr: 0x%llx\n", hop2_pte_addr);
498 seq_printf(s, "hop2_pte: 0x%llx\n", hop2_pte);
500 seq_printf(s, "hop3_addr: 0x%llx\n", hop3_addr);
501 seq_printf(s, "hop3_pte_addr: 0x%llx\n", hop3_pte_addr);
502 seq_printf(s, "hop3_pte: 0x%llx\n", hop3_pte);
504 if (mmu_prop->num_hops == MMU_ARCH_5_HOPS) {
505 if (!(hop3_pte & LAST_MASK)) {
506 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
507 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
508 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
511 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
512 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
513 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
515 if (!(hop4_pte & LAST_MASK)) {
516 seq_printf(s, "hop5_addr: 0x%llx\n", hop5_addr);
517 seq_printf(s, "hop5_pte_addr: 0x%llx\n", hop5_pte_addr);
518 seq_printf(s, "hop5_pte: 0x%llx\n", hop5_pte);
525 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
528 mutex_unlock(&ctx->mmu_lock);
533 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
534 size_t count, loff_t *f_pos)
536 struct seq_file *s = file->private_data;
537 struct hl_debugfs_entry *entry = s->private;
538 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
539 struct hl_device *hdev = dev_entry->hdev;
540 char kbuf[MMU_KBUF_SIZE];
544 if (!hdev->mmu_enable)
547 if (count > sizeof(kbuf) - 1)
549 if (copy_from_user(kbuf, buf, count))
553 c = strchr(kbuf, ' ');
558 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
562 if (strncmp(c+1, "0x", 2))
564 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
571 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
576 static int engines_show(struct seq_file *s, void *data)
578 struct hl_debugfs_entry *entry = s->private;
579 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
580 struct hl_device *hdev = dev_entry->hdev;
582 if (atomic_read(&hdev->in_reset)) {
583 dev_warn_ratelimited(hdev->dev,
584 "Can't check device idle during reset\n");
588 hdev->asic_funcs->is_device_idle(hdev, NULL, s);
593 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
595 struct asic_fixed_properties *prop = &hdev->asic_prop;
597 if (!hdev->mmu_enable)
600 if (hdev->dram_supports_virtual_memory &&
601 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
604 if (addr >= prop->pmmu.start_addr &&
605 addr < prop->pmmu.end_addr)
608 if (addr >= prop->pmmu_huge.start_addr &&
609 addr < prop->pmmu_huge.end_addr)
615 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr,
618 struct hl_ctx *ctx = hdev->compute_ctx;
619 struct asic_fixed_properties *prop = &hdev->asic_prop;
620 struct hl_mmu_properties *mmu_prop;
621 u64 hop_addr, hop_pte_addr, hop_pte;
622 u64 offset_mask = HOP4_MASK | FLAGS_MASK;
627 dev_err(hdev->dev, "no ctx available\n");
631 is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
632 prop->dmmu.start_addr,
633 prop->dmmu.end_addr);
635 /* shifts and masks are the same in PMMU and HPMMU, use one of them */
636 mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
638 mutex_lock(&ctx->mmu_lock);
641 hop_addr = get_hop0_addr(ctx);
642 hop_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
643 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
646 hop_addr = get_next_hop_addr(hop_pte);
647 if (hop_addr == ULLONG_MAX)
649 hop_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
650 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
653 hop_addr = get_next_hop_addr(hop_pte);
654 if (hop_addr == ULLONG_MAX)
656 hop_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
657 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
660 hop_addr = get_next_hop_addr(hop_pte);
661 if (hop_addr == ULLONG_MAX)
663 hop_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
664 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
666 if (!(hop_pte & LAST_MASK)) {
668 hop_addr = get_next_hop_addr(hop_pte);
669 if (hop_addr == ULLONG_MAX)
671 hop_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop_addr,
673 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
675 offset_mask = FLAGS_MASK;
678 if (!(hop_pte & PAGE_PRESENT_MASK))
681 *phys_addr = (hop_pte & ~offset_mask) | (virt_addr & offset_mask);
686 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
690 mutex_unlock(&ctx->mmu_lock);
694 static ssize_t hl_data_read32(struct file *f, char __user *buf,
695 size_t count, loff_t *ppos)
697 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
698 struct hl_device *hdev = entry->hdev;
700 u64 addr = entry->addr;
704 if (atomic_read(&hdev->in_reset)) {
705 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
712 if (hl_is_device_va(hdev, addr)) {
713 rc = device_va_to_pa(hdev, addr, &addr);
718 rc = hdev->asic_funcs->debugfs_read32(hdev, addr, &val);
720 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
724 sprintf(tmp_buf, "0x%08x\n", val);
725 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
729 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
730 size_t count, loff_t *ppos)
732 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
733 struct hl_device *hdev = entry->hdev;
734 u64 addr = entry->addr;
738 if (atomic_read(&hdev->in_reset)) {
739 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
743 rc = kstrtouint_from_user(buf, count, 16, &value);
747 if (hl_is_device_va(hdev, addr)) {
748 rc = device_va_to_pa(hdev, addr, &addr);
753 rc = hdev->asic_funcs->debugfs_write32(hdev, addr, value);
755 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
763 static ssize_t hl_data_read64(struct file *f, char __user *buf,
764 size_t count, loff_t *ppos)
766 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
767 struct hl_device *hdev = entry->hdev;
769 u64 addr = entry->addr;
776 if (hl_is_device_va(hdev, addr)) {
777 rc = device_va_to_pa(hdev, addr, &addr);
782 rc = hdev->asic_funcs->debugfs_read64(hdev, addr, &val);
784 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
788 sprintf(tmp_buf, "0x%016llx\n", val);
789 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
793 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
794 size_t count, loff_t *ppos)
796 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
797 struct hl_device *hdev = entry->hdev;
798 u64 addr = entry->addr;
802 rc = kstrtoull_from_user(buf, count, 16, &value);
806 if (hl_is_device_va(hdev, addr)) {
807 rc = device_va_to_pa(hdev, addr, &addr);
812 rc = hdev->asic_funcs->debugfs_write64(hdev, addr, value);
814 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
822 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
823 size_t count, loff_t *ppos)
825 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
826 struct hl_device *hdev = entry->hdev;
833 if (hdev->pdev->current_state == PCI_D0)
835 else if (hdev->pdev->current_state == PCI_D3hot)
841 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
842 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
846 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
847 size_t count, loff_t *ppos)
849 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
850 struct hl_device *hdev = entry->hdev;
854 rc = kstrtouint_from_user(buf, count, 10, &value);
859 pci_set_power_state(hdev->pdev, PCI_D0);
860 pci_restore_state(hdev->pdev);
861 rc = pci_enable_device(hdev->pdev);
864 } else if (value == 2) {
865 pci_save_state(hdev->pdev);
866 pci_disable_device(hdev->pdev);
867 pci_set_power_state(hdev->pdev, PCI_D3hot);
869 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
876 static ssize_t hl_i2c_data_read(struct file *f, 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;
888 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
889 entry->i2c_reg, &val);
892 "Failed to read from I2C bus %d, addr %d, reg %d\n",
893 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
897 sprintf(tmp_buf, "0x%02lx\n", val);
898 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
904 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
905 size_t count, loff_t *ppos)
907 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
908 struct hl_device *hdev = entry->hdev;
912 rc = kstrtouint_from_user(buf, count, 16, &value);
916 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
917 entry->i2c_reg, value);
920 "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
921 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
928 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
929 size_t count, loff_t *ppos)
931 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
932 struct hl_device *hdev = entry->hdev;
936 rc = kstrtouint_from_user(buf, count, 10, &value);
940 value = value ? 1 : 0;
942 hl_debugfs_led_set(hdev, 0, value);
947 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
948 size_t count, loff_t *ppos)
950 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
951 struct hl_device *hdev = entry->hdev;
955 rc = kstrtouint_from_user(buf, count, 10, &value);
959 value = value ? 1 : 0;
961 hl_debugfs_led_set(hdev, 1, value);
966 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
967 size_t count, loff_t *ppos)
969 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
970 struct hl_device *hdev = entry->hdev;
974 rc = kstrtouint_from_user(buf, count, 10, &value);
978 value = value ? 1 : 0;
980 hl_debugfs_led_set(hdev, 2, value);
985 static ssize_t hl_device_read(struct file *f, char __user *buf,
986 size_t count, loff_t *ppos)
988 static const char *help =
989 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
990 return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
993 static ssize_t hl_device_write(struct file *f, const char __user *buf,
994 size_t count, loff_t *ppos)
996 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
997 struct hl_device *hdev = entry->hdev;
1000 /* don't allow partial writes */
1004 simple_write_to_buffer(data, 29, ppos, buf, count);
1006 if (strncmp("disable", data, strlen("disable")) == 0) {
1007 hdev->disabled = true;
1008 } else if (strncmp("enable", data, strlen("enable")) == 0) {
1009 hdev->disabled = false;
1010 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
1011 hdev->asic_funcs->suspend(hdev);
1012 } else if (strncmp("resume", data, strlen("resume")) == 0) {
1013 hdev->asic_funcs->resume(hdev);
1014 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
1015 hdev->device_cpu_disabled = true;
1018 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
1025 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
1026 size_t count, loff_t *ppos)
1028 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1029 struct hl_device *hdev = entry->hdev;
1036 sprintf(tmp_buf, "0x%llx\n", hdev->clock_gating_mask);
1037 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
1038 strlen(tmp_buf) + 1);
1043 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1044 size_t count, loff_t *ppos)
1046 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1047 struct hl_device *hdev = entry->hdev;
1051 if (atomic_read(&hdev->in_reset)) {
1052 dev_warn_ratelimited(hdev->dev,
1053 "Can't change clock gating during reset\n");
1057 rc = kstrtoull_from_user(buf, count, 16, &value);
1061 hdev->clock_gating_mask = value;
1062 hdev->asic_funcs->set_clock_gating(hdev);
1067 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1068 size_t count, loff_t *ppos)
1070 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1071 struct hl_device *hdev = entry->hdev;
1078 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1079 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1080 strlen(tmp_buf) + 1);
1085 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1086 size_t count, loff_t *ppos)
1088 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1089 struct hl_device *hdev = entry->hdev;
1093 if (atomic_read(&hdev->in_reset)) {
1094 dev_warn_ratelimited(hdev->dev,
1095 "Can't change stop on error during reset\n");
1099 rc = kstrtouint_from_user(buf, count, 10, &value);
1103 hdev->stop_on_err = value ? 1 : 0;
1105 hl_device_reset(hdev, false, false);
1110 static const struct file_operations hl_data32b_fops = {
1111 .owner = THIS_MODULE,
1112 .read = hl_data_read32,
1113 .write = hl_data_write32
1116 static const struct file_operations hl_data64b_fops = {
1117 .owner = THIS_MODULE,
1118 .read = hl_data_read64,
1119 .write = hl_data_write64
1122 static const struct file_operations hl_i2c_data_fops = {
1123 .owner = THIS_MODULE,
1124 .read = hl_i2c_data_read,
1125 .write = hl_i2c_data_write
1128 static const struct file_operations hl_power_fops = {
1129 .owner = THIS_MODULE,
1130 .read = hl_get_power_state,
1131 .write = hl_set_power_state
1134 static const struct file_operations hl_led0_fops = {
1135 .owner = THIS_MODULE,
1136 .write = hl_led0_write
1139 static const struct file_operations hl_led1_fops = {
1140 .owner = THIS_MODULE,
1141 .write = hl_led1_write
1144 static const struct file_operations hl_led2_fops = {
1145 .owner = THIS_MODULE,
1146 .write = hl_led2_write
1149 static const struct file_operations hl_device_fops = {
1150 .owner = THIS_MODULE,
1151 .read = hl_device_read,
1152 .write = hl_device_write
1155 static const struct file_operations hl_clk_gate_fops = {
1156 .owner = THIS_MODULE,
1157 .read = hl_clk_gate_read,
1158 .write = hl_clk_gate_write
1161 static const struct file_operations hl_stop_on_err_fops = {
1162 .owner = THIS_MODULE,
1163 .read = hl_stop_on_err_read,
1164 .write = hl_stop_on_err_write
1167 static const struct hl_info_list hl_debugfs_list[] = {
1168 {"command_buffers", command_buffers_show, NULL},
1169 {"command_submission", command_submission_show, NULL},
1170 {"command_submission_jobs", command_submission_jobs_show, NULL},
1171 {"userptr", userptr_show, NULL},
1172 {"vm", vm_show, NULL},
1173 {"mmu", mmu_show, mmu_asid_va_write},
1174 {"engines", engines_show, NULL}
1177 static int hl_debugfs_open(struct inode *inode, struct file *file)
1179 struct hl_debugfs_entry *node = inode->i_private;
1181 return single_open(file, node->info_ent->show, node);
1184 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1185 size_t count, loff_t *f_pos)
1187 struct hl_debugfs_entry *node = file->f_inode->i_private;
1189 if (node->info_ent->write)
1190 return node->info_ent->write(file, buf, count, f_pos);
1196 static const struct file_operations hl_debugfs_fops = {
1197 .owner = THIS_MODULE,
1198 .open = hl_debugfs_open,
1200 .write = hl_debugfs_write,
1201 .llseek = seq_lseek,
1202 .release = single_release,
1205 void hl_debugfs_add_device(struct hl_device *hdev)
1207 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1208 int count = ARRAY_SIZE(hl_debugfs_list);
1209 struct hl_debugfs_entry *entry;
1213 dev_entry->hdev = hdev;
1214 dev_entry->entry_arr = kmalloc_array(count,
1215 sizeof(struct hl_debugfs_entry),
1217 if (!dev_entry->entry_arr)
1220 INIT_LIST_HEAD(&dev_entry->file_list);
1221 INIT_LIST_HEAD(&dev_entry->cb_list);
1222 INIT_LIST_HEAD(&dev_entry->cs_list);
1223 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1224 INIT_LIST_HEAD(&dev_entry->userptr_list);
1225 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1226 mutex_init(&dev_entry->file_mutex);
1227 spin_lock_init(&dev_entry->cb_spinlock);
1228 spin_lock_init(&dev_entry->cs_spinlock);
1229 spin_lock_init(&dev_entry->cs_job_spinlock);
1230 spin_lock_init(&dev_entry->userptr_spinlock);
1231 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1233 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1236 debugfs_create_x64("addr",
1241 debugfs_create_file("data32",
1247 debugfs_create_file("data64",
1253 debugfs_create_file("set_power_state",
1259 debugfs_create_u8("i2c_bus",
1262 &dev_entry->i2c_bus);
1264 debugfs_create_u8("i2c_addr",
1267 &dev_entry->i2c_addr);
1269 debugfs_create_u8("i2c_reg",
1272 &dev_entry->i2c_reg);
1274 debugfs_create_file("i2c_data",
1280 debugfs_create_file("led0",
1286 debugfs_create_file("led1",
1292 debugfs_create_file("led2",
1298 debugfs_create_file("device",
1304 debugfs_create_file("clk_gate",
1310 debugfs_create_file("stop_on_err",
1314 &hl_stop_on_err_fops);
1316 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1318 ent = debugfs_create_file(hl_debugfs_list[i].name,
1324 entry->info_ent = &hl_debugfs_list[i];
1325 entry->dev_entry = dev_entry;
1329 void hl_debugfs_remove_device(struct hl_device *hdev)
1331 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1333 debugfs_remove_recursive(entry->root);
1335 mutex_destroy(&entry->file_mutex);
1336 kfree(entry->entry_arr);
1339 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1341 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1343 mutex_lock(&dev_entry->file_mutex);
1344 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1345 mutex_unlock(&dev_entry->file_mutex);
1348 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1350 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1352 mutex_lock(&dev_entry->file_mutex);
1353 list_del(&hpriv->debugfs_list);
1354 mutex_unlock(&dev_entry->file_mutex);
1357 void hl_debugfs_add_cb(struct hl_cb *cb)
1359 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1361 spin_lock(&dev_entry->cb_spinlock);
1362 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1363 spin_unlock(&dev_entry->cb_spinlock);
1366 void hl_debugfs_remove_cb(struct hl_cb *cb)
1368 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1370 spin_lock(&dev_entry->cb_spinlock);
1371 list_del(&cb->debugfs_list);
1372 spin_unlock(&dev_entry->cb_spinlock);
1375 void hl_debugfs_add_cs(struct hl_cs *cs)
1377 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1379 spin_lock(&dev_entry->cs_spinlock);
1380 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1381 spin_unlock(&dev_entry->cs_spinlock);
1384 void hl_debugfs_remove_cs(struct hl_cs *cs)
1386 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1388 spin_lock(&dev_entry->cs_spinlock);
1389 list_del(&cs->debugfs_list);
1390 spin_unlock(&dev_entry->cs_spinlock);
1393 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1395 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1397 spin_lock(&dev_entry->cs_job_spinlock);
1398 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1399 spin_unlock(&dev_entry->cs_job_spinlock);
1402 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1404 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1406 spin_lock(&dev_entry->cs_job_spinlock);
1407 list_del(&job->debugfs_list);
1408 spin_unlock(&dev_entry->cs_job_spinlock);
1411 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1413 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1415 spin_lock(&dev_entry->userptr_spinlock);
1416 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1417 spin_unlock(&dev_entry->userptr_spinlock);
1420 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1421 struct hl_userptr *userptr)
1423 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1425 spin_lock(&dev_entry->userptr_spinlock);
1426 list_del(&userptr->debugfs_list);
1427 spin_unlock(&dev_entry->userptr_spinlock);
1430 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1432 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1434 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1435 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1436 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1439 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1441 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1443 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1444 list_del(&ctx->debugfs_list);
1445 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1448 void __init hl_debugfs_init(void)
1450 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1453 void hl_debugfs_fini(void)
1455 debugfs_remove_recursive(hl_debug_root);