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 JOB RefCnt H/W Queue\n");
172 seq_puts(s, "----------------------------------------------------\n");
176 " %02d %llu %d %d %d\n",
177 job->id, job->cs->sequence, job->cs->ctx->asid,
178 kref_read(&job->refcount), job->hw_queue_id);
181 " %02d 0 %d %d %d\n",
182 job->id, HL_KERNEL_ASID_ID,
183 kref_read(&job->refcount), job->hw_queue_id);
186 spin_unlock(&dev_entry->cs_job_spinlock);
194 static int userptr_show(struct seq_file *s, void *data)
196 struct hl_debugfs_entry *entry = s->private;
197 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
198 struct hl_userptr *userptr;
199 char dma_dir[4][30] = {"DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
200 "DMA_FROM_DEVICE", "DMA_NONE"};
203 spin_lock(&dev_entry->userptr_spinlock);
205 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
209 seq_puts(s, " user virtual address size dma dir\n");
210 seq_puts(s, "----------------------------------------------------------\n");
213 " 0x%-14llx %-10u %-30s\n",
214 userptr->addr, userptr->size, dma_dir[userptr->dir]);
217 spin_unlock(&dev_entry->userptr_spinlock);
225 static int vm_show(struct seq_file *s, void *data)
227 struct hl_debugfs_entry *entry = s->private;
228 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
231 struct hl_vm_hash_node *hnode;
232 struct hl_userptr *userptr;
233 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
234 enum vm_type_t *vm_type;
239 if (!dev_entry->hdev->mmu_enable)
242 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
244 list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
246 seq_puts(s, "\n\n----------------------------------------------------");
247 seq_puts(s, "\n----------------------------------------------------\n\n");
248 seq_printf(s, "ctx asid: %u\n", ctx->asid);
250 seq_puts(s, "\nmappings:\n\n");
251 seq_puts(s, " virtual address size handle\n");
252 seq_puts(s, "----------------------------------------------------\n");
253 mutex_lock(&ctx->mem_hash_lock);
254 hash_for_each(ctx->mem_hash, i, hnode, node) {
255 vm_type = hnode->ptr;
257 if (*vm_type == VM_TYPE_USERPTR) {
258 userptr = hnode->ptr;
260 " 0x%-14llx %-10u\n",
261 hnode->vaddr, userptr->size);
263 phys_pg_pack = hnode->ptr;
265 " 0x%-14llx %-10llu %-4u\n",
266 hnode->vaddr, phys_pg_pack->total_size,
267 phys_pg_pack->handle);
270 mutex_unlock(&ctx->mem_hash_lock);
273 spin_lock(&vm->idr_lock);
275 if (!idr_is_empty(&vm->phys_pg_pack_handles))
276 seq_puts(s, "\n\nallocations:\n");
278 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
279 if (phys_pg_pack->asid != ctx->asid)
282 seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
283 seq_printf(s, "page size: %u\n\n",
284 phys_pg_pack->page_size);
285 seq_puts(s, " physical address\n");
286 seq_puts(s, "---------------------\n");
287 for (j = 0 ; j < phys_pg_pack->npages ; j++) {
288 seq_printf(s, " 0x%-14llx\n",
289 phys_pg_pack->pages[j]);
292 spin_unlock(&vm->idr_lock);
296 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
304 /* these inline functions are copied from mmu.c */
305 static inline u64 get_hop0_addr(struct hl_ctx *ctx)
307 return ctx->hdev->asic_prop.mmu_pgt_addr +
308 (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
311 static inline u64 get_hopN_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
312 u64 virt_addr, u64 mask, u64 shift)
314 return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
315 ((virt_addr & mask) >> shift);
318 static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx,
319 struct hl_mmu_properties *mmu_specs,
320 u64 hop_addr, u64 vaddr)
322 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop0_mask,
323 mmu_specs->hop0_shift);
326 static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx,
327 struct hl_mmu_properties *mmu_specs,
328 u64 hop_addr, u64 vaddr)
330 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop1_mask,
331 mmu_specs->hop1_shift);
334 static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx,
335 struct hl_mmu_properties *mmu_specs,
336 u64 hop_addr, u64 vaddr)
338 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop2_mask,
339 mmu_specs->hop2_shift);
342 static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx,
343 struct hl_mmu_properties *mmu_specs,
344 u64 hop_addr, u64 vaddr)
346 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop3_mask,
347 mmu_specs->hop3_shift);
350 static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx,
351 struct hl_mmu_properties *mmu_specs,
352 u64 hop_addr, u64 vaddr)
354 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop4_mask,
355 mmu_specs->hop4_shift);
358 static inline u64 get_hop5_pte_addr(struct hl_ctx *ctx,
359 struct hl_mmu_properties *mmu_specs,
360 u64 hop_addr, u64 vaddr)
362 return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop5_mask,
363 mmu_specs->hop5_shift);
366 static inline u64 get_next_hop_addr(u64 curr_pte)
368 if (curr_pte & PAGE_PRESENT_MASK)
369 return curr_pte & HOP_PHYS_ADDR_MASK;
374 static int mmu_show(struct seq_file *s, void *data)
376 struct hl_debugfs_entry *entry = s->private;
377 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
378 struct hl_device *hdev = dev_entry->hdev;
379 struct asic_fixed_properties *prop = &hdev->asic_prop;
380 struct hl_mmu_properties *mmu_prop;
384 u64 hop0_addr = 0, hop0_pte_addr = 0, hop0_pte = 0,
385 hop1_addr = 0, hop1_pte_addr = 0, hop1_pte = 0,
386 hop2_addr = 0, hop2_pte_addr = 0, hop2_pte = 0,
387 hop3_addr = 0, hop3_pte_addr = 0, hop3_pte = 0,
388 hop4_addr = 0, hop4_pte_addr = 0, hop4_pte = 0,
389 hop5_addr = 0, hop5_pte_addr = 0, hop5_pte = 0,
390 virt_addr = dev_entry->mmu_addr;
392 if (!hdev->mmu_enable)
395 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
396 ctx = hdev->kernel_ctx;
398 ctx = hdev->compute_ctx;
401 dev_err(hdev->dev, "no ctx available\n");
405 is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
406 prop->dmmu.start_addr,
407 prop->dmmu.end_addr);
409 /* shifts and masks are the same in PMMU and HPMMU, use one of them */
410 mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
412 mutex_lock(&ctx->mmu_lock);
414 /* the following lookup is copied from unmap() in mmu.c */
416 hop0_addr = get_hop0_addr(ctx);
417 hop0_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop0_addr, virt_addr);
418 hop0_pte = hdev->asic_funcs->read_pte(hdev, hop0_pte_addr);
419 hop1_addr = get_next_hop_addr(hop0_pte);
421 if (hop1_addr == ULLONG_MAX)
424 hop1_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop1_addr, virt_addr);
425 hop1_pte = hdev->asic_funcs->read_pte(hdev, hop1_pte_addr);
426 hop2_addr = get_next_hop_addr(hop1_pte);
428 if (hop2_addr == ULLONG_MAX)
431 hop2_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop2_addr, virt_addr);
432 hop2_pte = hdev->asic_funcs->read_pte(hdev, hop2_pte_addr);
433 hop3_addr = get_next_hop_addr(hop2_pte);
435 if (hop3_addr == ULLONG_MAX)
438 hop3_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop3_addr, virt_addr);
439 hop3_pte = hdev->asic_funcs->read_pte(hdev, hop3_pte_addr);
441 if (mmu_prop->num_hops == MMU_ARCH_5_HOPS) {
442 if (!(hop3_pte & LAST_MASK)) {
443 hop4_addr = get_next_hop_addr(hop3_pte);
445 if (hop4_addr == ULLONG_MAX)
448 hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop,
449 hop4_addr, virt_addr);
450 hop4_pte = hdev->asic_funcs->read_pte(hdev,
452 if (!(hop4_pte & PAGE_PRESENT_MASK))
455 if (!(hop3_pte & PAGE_PRESENT_MASK))
459 hop4_addr = get_next_hop_addr(hop3_pte);
461 if (hop4_addr == ULLONG_MAX)
464 hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop,
465 hop4_addr, virt_addr);
466 hop4_pte = hdev->asic_funcs->read_pte(hdev,
468 if (!(hop4_pte & LAST_MASK)) {
469 hop5_addr = get_next_hop_addr(hop4_pte);
471 if (hop5_addr == ULLONG_MAX)
474 hop5_pte_addr = get_hop5_pte_addr(ctx, mmu_prop,
475 hop5_addr, virt_addr);
476 hop5_pte = hdev->asic_funcs->read_pte(hdev,
478 if (!(hop5_pte & PAGE_PRESENT_MASK))
481 if (!(hop4_pte & PAGE_PRESENT_MASK))
486 seq_printf(s, "asid: %u, virt_addr: 0x%llx\n",
487 dev_entry->mmu_asid, dev_entry->mmu_addr);
489 seq_printf(s, "hop0_addr: 0x%llx\n", hop0_addr);
490 seq_printf(s, "hop0_pte_addr: 0x%llx\n", hop0_pte_addr);
491 seq_printf(s, "hop0_pte: 0x%llx\n", hop0_pte);
493 seq_printf(s, "hop1_addr: 0x%llx\n", hop1_addr);
494 seq_printf(s, "hop1_pte_addr: 0x%llx\n", hop1_pte_addr);
495 seq_printf(s, "hop1_pte: 0x%llx\n", hop1_pte);
497 seq_printf(s, "hop2_addr: 0x%llx\n", hop2_addr);
498 seq_printf(s, "hop2_pte_addr: 0x%llx\n", hop2_pte_addr);
499 seq_printf(s, "hop2_pte: 0x%llx\n", hop2_pte);
501 seq_printf(s, "hop3_addr: 0x%llx\n", hop3_addr);
502 seq_printf(s, "hop3_pte_addr: 0x%llx\n", hop3_pte_addr);
503 seq_printf(s, "hop3_pte: 0x%llx\n", hop3_pte);
505 if (mmu_prop->num_hops == MMU_ARCH_5_HOPS) {
506 if (!(hop3_pte & LAST_MASK)) {
507 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
508 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
509 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
512 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
513 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
514 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
516 if (!(hop4_pte & LAST_MASK)) {
517 seq_printf(s, "hop5_addr: 0x%llx\n", hop5_addr);
518 seq_printf(s, "hop5_pte_addr: 0x%llx\n", hop5_pte_addr);
519 seq_printf(s, "hop5_pte: 0x%llx\n", hop5_pte);
526 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
529 mutex_unlock(&ctx->mmu_lock);
534 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
535 size_t count, loff_t *f_pos)
537 struct seq_file *s = file->private_data;
538 struct hl_debugfs_entry *entry = s->private;
539 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
540 struct hl_device *hdev = dev_entry->hdev;
541 char kbuf[MMU_KBUF_SIZE];
545 if (!hdev->mmu_enable)
548 if (count > sizeof(kbuf) - 1)
550 if (copy_from_user(kbuf, buf, count))
554 c = strchr(kbuf, ' ');
559 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
563 if (strncmp(c+1, "0x", 2))
565 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
572 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
577 static int engines_show(struct seq_file *s, void *data)
579 struct hl_debugfs_entry *entry = s->private;
580 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
581 struct hl_device *hdev = dev_entry->hdev;
583 if (atomic_read(&hdev->in_reset)) {
584 dev_warn_ratelimited(hdev->dev,
585 "Can't check device idle during reset\n");
589 hdev->asic_funcs->is_device_idle(hdev, NULL, s);
594 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
596 struct asic_fixed_properties *prop = &hdev->asic_prop;
598 if (!hdev->mmu_enable)
601 if (hdev->dram_supports_virtual_memory &&
602 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
605 if (addr >= prop->pmmu.start_addr &&
606 addr < prop->pmmu.end_addr)
609 if (addr >= prop->pmmu_huge.start_addr &&
610 addr < prop->pmmu_huge.end_addr)
616 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr,
619 struct hl_ctx *ctx = hdev->compute_ctx;
620 struct asic_fixed_properties *prop = &hdev->asic_prop;
621 struct hl_mmu_properties *mmu_prop;
622 u64 hop_addr, hop_pte_addr, hop_pte;
623 u64 offset_mask = HOP4_MASK | FLAGS_MASK;
628 dev_err(hdev->dev, "no ctx available\n");
632 is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
633 prop->dmmu.start_addr,
634 prop->dmmu.end_addr);
636 /* shifts and masks are the same in PMMU and HPMMU, use one of them */
637 mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
639 mutex_lock(&ctx->mmu_lock);
642 hop_addr = get_hop0_addr(ctx);
643 hop_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
644 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
647 hop_addr = get_next_hop_addr(hop_pte);
648 if (hop_addr == ULLONG_MAX)
650 hop_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
651 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
654 hop_addr = get_next_hop_addr(hop_pte);
655 if (hop_addr == ULLONG_MAX)
657 hop_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
658 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
661 hop_addr = get_next_hop_addr(hop_pte);
662 if (hop_addr == ULLONG_MAX)
664 hop_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
665 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
667 if (!(hop_pte & LAST_MASK)) {
669 hop_addr = get_next_hop_addr(hop_pte);
670 if (hop_addr == ULLONG_MAX)
672 hop_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop_addr,
674 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
676 offset_mask = FLAGS_MASK;
679 if (!(hop_pte & PAGE_PRESENT_MASK))
682 *phys_addr = (hop_pte & ~offset_mask) | (virt_addr & offset_mask);
687 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
691 mutex_unlock(&ctx->mmu_lock);
695 static ssize_t hl_data_read32(struct file *f, char __user *buf,
696 size_t count, loff_t *ppos)
698 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
699 struct hl_device *hdev = entry->hdev;
701 u64 addr = entry->addr;
705 if (atomic_read(&hdev->in_reset)) {
706 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
713 if (hl_is_device_va(hdev, addr)) {
714 rc = device_va_to_pa(hdev, addr, &addr);
719 rc = hdev->asic_funcs->debugfs_read32(hdev, addr, &val);
721 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
725 sprintf(tmp_buf, "0x%08x\n", val);
726 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
730 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
731 size_t count, loff_t *ppos)
733 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
734 struct hl_device *hdev = entry->hdev;
735 u64 addr = entry->addr;
739 if (atomic_read(&hdev->in_reset)) {
740 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
744 rc = kstrtouint_from_user(buf, count, 16, &value);
748 if (hl_is_device_va(hdev, addr)) {
749 rc = device_va_to_pa(hdev, addr, &addr);
754 rc = hdev->asic_funcs->debugfs_write32(hdev, addr, value);
756 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
764 static ssize_t hl_data_read64(struct file *f, char __user *buf,
765 size_t count, loff_t *ppos)
767 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
768 struct hl_device *hdev = entry->hdev;
770 u64 addr = entry->addr;
777 if (hl_is_device_va(hdev, addr)) {
778 rc = device_va_to_pa(hdev, addr, &addr);
783 rc = hdev->asic_funcs->debugfs_read64(hdev, addr, &val);
785 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
789 sprintf(tmp_buf, "0x%016llx\n", val);
790 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
794 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
795 size_t count, loff_t *ppos)
797 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
798 struct hl_device *hdev = entry->hdev;
799 u64 addr = entry->addr;
803 rc = kstrtoull_from_user(buf, count, 16, &value);
807 if (hl_is_device_va(hdev, addr)) {
808 rc = device_va_to_pa(hdev, addr, &addr);
813 rc = hdev->asic_funcs->debugfs_write64(hdev, addr, value);
815 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
823 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
824 size_t count, loff_t *ppos)
826 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
827 struct hl_device *hdev = entry->hdev;
834 if (hdev->pdev->current_state == PCI_D0)
836 else if (hdev->pdev->current_state == PCI_D3hot)
842 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
843 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
847 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
848 size_t count, loff_t *ppos)
850 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
851 struct hl_device *hdev = entry->hdev;
855 rc = kstrtouint_from_user(buf, count, 10, &value);
860 pci_set_power_state(hdev->pdev, PCI_D0);
861 pci_restore_state(hdev->pdev);
862 rc = pci_enable_device(hdev->pdev);
863 } else if (value == 2) {
864 pci_save_state(hdev->pdev);
865 pci_disable_device(hdev->pdev);
866 pci_set_power_state(hdev->pdev, PCI_D3hot);
868 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
875 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
876 size_t count, loff_t *ppos)
878 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
879 struct hl_device *hdev = entry->hdev;
887 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
888 entry->i2c_reg, &val);
891 "Failed to read from I2C bus %d, addr %d, reg %d\n",
892 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
896 sprintf(tmp_buf, "0x%02lx\n", val);
897 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
903 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
904 size_t count, loff_t *ppos)
906 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
907 struct hl_device *hdev = entry->hdev;
911 rc = kstrtouint_from_user(buf, count, 16, &value);
915 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
916 entry->i2c_reg, value);
919 "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
920 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
927 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
928 size_t count, loff_t *ppos)
930 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
931 struct hl_device *hdev = entry->hdev;
935 rc = kstrtouint_from_user(buf, count, 10, &value);
939 value = value ? 1 : 0;
941 hl_debugfs_led_set(hdev, 0, value);
946 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
947 size_t count, loff_t *ppos)
949 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
950 struct hl_device *hdev = entry->hdev;
954 rc = kstrtouint_from_user(buf, count, 10, &value);
958 value = value ? 1 : 0;
960 hl_debugfs_led_set(hdev, 1, value);
965 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
966 size_t count, loff_t *ppos)
968 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
969 struct hl_device *hdev = entry->hdev;
973 rc = kstrtouint_from_user(buf, count, 10, &value);
977 value = value ? 1 : 0;
979 hl_debugfs_led_set(hdev, 2, value);
984 static ssize_t hl_device_read(struct file *f, char __user *buf,
985 size_t count, loff_t *ppos)
987 static const char *help =
988 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
989 return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
992 static ssize_t hl_device_write(struct file *f, const char __user *buf,
993 size_t count, loff_t *ppos)
995 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
996 struct hl_device *hdev = entry->hdev;
999 /* don't allow partial writes */
1003 simple_write_to_buffer(data, 29, ppos, buf, count);
1005 if (strncmp("disable", data, strlen("disable")) == 0) {
1006 hdev->disabled = true;
1007 } else if (strncmp("enable", data, strlen("enable")) == 0) {
1008 hdev->disabled = false;
1009 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
1010 hdev->asic_funcs->suspend(hdev);
1011 } else if (strncmp("resume", data, strlen("resume")) == 0) {
1012 hdev->asic_funcs->resume(hdev);
1013 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
1014 hdev->device_cpu_disabled = true;
1017 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
1024 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
1025 size_t count, loff_t *ppos)
1027 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1028 struct hl_device *hdev = entry->hdev;
1035 sprintf(tmp_buf, "0x%llx\n", hdev->clock_gating_mask);
1036 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
1037 strlen(tmp_buf) + 1);
1042 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1043 size_t count, loff_t *ppos)
1045 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1046 struct hl_device *hdev = entry->hdev;
1050 if (atomic_read(&hdev->in_reset)) {
1051 dev_warn_ratelimited(hdev->dev,
1052 "Can't change clock gating during reset\n");
1056 rc = kstrtoull_from_user(buf, count, 16, &value);
1060 hdev->clock_gating_mask = value;
1061 hdev->asic_funcs->set_clock_gating(hdev);
1066 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1067 size_t count, loff_t *ppos)
1069 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1070 struct hl_device *hdev = entry->hdev;
1077 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1078 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1079 strlen(tmp_buf) + 1);
1084 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1085 size_t count, loff_t *ppos)
1087 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1088 struct hl_device *hdev = entry->hdev;
1092 if (atomic_read(&hdev->in_reset)) {
1093 dev_warn_ratelimited(hdev->dev,
1094 "Can't change stop on error during reset\n");
1098 rc = kstrtouint_from_user(buf, count, 10, &value);
1102 hdev->stop_on_err = value ? 1 : 0;
1104 hl_device_reset(hdev, false, false);
1109 static const struct file_operations hl_data32b_fops = {
1110 .owner = THIS_MODULE,
1111 .read = hl_data_read32,
1112 .write = hl_data_write32
1115 static const struct file_operations hl_data64b_fops = {
1116 .owner = THIS_MODULE,
1117 .read = hl_data_read64,
1118 .write = hl_data_write64
1121 static const struct file_operations hl_i2c_data_fops = {
1122 .owner = THIS_MODULE,
1123 .read = hl_i2c_data_read,
1124 .write = hl_i2c_data_write
1127 static const struct file_operations hl_power_fops = {
1128 .owner = THIS_MODULE,
1129 .read = hl_get_power_state,
1130 .write = hl_set_power_state
1133 static const struct file_operations hl_led0_fops = {
1134 .owner = THIS_MODULE,
1135 .write = hl_led0_write
1138 static const struct file_operations hl_led1_fops = {
1139 .owner = THIS_MODULE,
1140 .write = hl_led1_write
1143 static const struct file_operations hl_led2_fops = {
1144 .owner = THIS_MODULE,
1145 .write = hl_led2_write
1148 static const struct file_operations hl_device_fops = {
1149 .owner = THIS_MODULE,
1150 .read = hl_device_read,
1151 .write = hl_device_write
1154 static const struct file_operations hl_clk_gate_fops = {
1155 .owner = THIS_MODULE,
1156 .read = hl_clk_gate_read,
1157 .write = hl_clk_gate_write
1160 static const struct file_operations hl_stop_on_err_fops = {
1161 .owner = THIS_MODULE,
1162 .read = hl_stop_on_err_read,
1163 .write = hl_stop_on_err_write
1166 static const struct hl_info_list hl_debugfs_list[] = {
1167 {"command_buffers", command_buffers_show, NULL},
1168 {"command_submission", command_submission_show, NULL},
1169 {"command_submission_jobs", command_submission_jobs_show, NULL},
1170 {"userptr", userptr_show, NULL},
1171 {"vm", vm_show, NULL},
1172 {"mmu", mmu_show, mmu_asid_va_write},
1173 {"engines", engines_show, NULL}
1176 static int hl_debugfs_open(struct inode *inode, struct file *file)
1178 struct hl_debugfs_entry *node = inode->i_private;
1180 return single_open(file, node->info_ent->show, node);
1183 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1184 size_t count, loff_t *f_pos)
1186 struct hl_debugfs_entry *node = file->f_inode->i_private;
1188 if (node->info_ent->write)
1189 return node->info_ent->write(file, buf, count, f_pos);
1195 static const struct file_operations hl_debugfs_fops = {
1196 .owner = THIS_MODULE,
1197 .open = hl_debugfs_open,
1199 .write = hl_debugfs_write,
1200 .llseek = seq_lseek,
1201 .release = single_release,
1204 void hl_debugfs_add_device(struct hl_device *hdev)
1206 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1207 int count = ARRAY_SIZE(hl_debugfs_list);
1208 struct hl_debugfs_entry *entry;
1212 dev_entry->hdev = hdev;
1213 dev_entry->entry_arr = kmalloc_array(count,
1214 sizeof(struct hl_debugfs_entry),
1216 if (!dev_entry->entry_arr)
1219 INIT_LIST_HEAD(&dev_entry->file_list);
1220 INIT_LIST_HEAD(&dev_entry->cb_list);
1221 INIT_LIST_HEAD(&dev_entry->cs_list);
1222 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1223 INIT_LIST_HEAD(&dev_entry->userptr_list);
1224 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1225 mutex_init(&dev_entry->file_mutex);
1226 spin_lock_init(&dev_entry->cb_spinlock);
1227 spin_lock_init(&dev_entry->cs_spinlock);
1228 spin_lock_init(&dev_entry->cs_job_spinlock);
1229 spin_lock_init(&dev_entry->userptr_spinlock);
1230 spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1232 dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1235 debugfs_create_x64("addr",
1240 debugfs_create_file("data32",
1246 debugfs_create_file("data64",
1252 debugfs_create_file("set_power_state",
1258 debugfs_create_u8("i2c_bus",
1261 &dev_entry->i2c_bus);
1263 debugfs_create_u8("i2c_addr",
1266 &dev_entry->i2c_addr);
1268 debugfs_create_u8("i2c_reg",
1271 &dev_entry->i2c_reg);
1273 debugfs_create_file("i2c_data",
1279 debugfs_create_file("led0",
1285 debugfs_create_file("led1",
1291 debugfs_create_file("led2",
1297 debugfs_create_file("device",
1303 debugfs_create_file("clk_gate",
1309 debugfs_create_file("stop_on_err",
1313 &hl_stop_on_err_fops);
1315 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1317 ent = debugfs_create_file(hl_debugfs_list[i].name,
1323 entry->info_ent = &hl_debugfs_list[i];
1324 entry->dev_entry = dev_entry;
1328 void hl_debugfs_remove_device(struct hl_device *hdev)
1330 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1332 debugfs_remove_recursive(entry->root);
1334 mutex_destroy(&entry->file_mutex);
1335 kfree(entry->entry_arr);
1338 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1340 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1342 mutex_lock(&dev_entry->file_mutex);
1343 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1344 mutex_unlock(&dev_entry->file_mutex);
1347 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1349 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1351 mutex_lock(&dev_entry->file_mutex);
1352 list_del(&hpriv->debugfs_list);
1353 mutex_unlock(&dev_entry->file_mutex);
1356 void hl_debugfs_add_cb(struct hl_cb *cb)
1358 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1360 spin_lock(&dev_entry->cb_spinlock);
1361 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1362 spin_unlock(&dev_entry->cb_spinlock);
1365 void hl_debugfs_remove_cb(struct hl_cb *cb)
1367 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1369 spin_lock(&dev_entry->cb_spinlock);
1370 list_del(&cb->debugfs_list);
1371 spin_unlock(&dev_entry->cb_spinlock);
1374 void hl_debugfs_add_cs(struct hl_cs *cs)
1376 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1378 spin_lock(&dev_entry->cs_spinlock);
1379 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1380 spin_unlock(&dev_entry->cs_spinlock);
1383 void hl_debugfs_remove_cs(struct hl_cs *cs)
1385 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1387 spin_lock(&dev_entry->cs_spinlock);
1388 list_del(&cs->debugfs_list);
1389 spin_unlock(&dev_entry->cs_spinlock);
1392 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1394 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1396 spin_lock(&dev_entry->cs_job_spinlock);
1397 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1398 spin_unlock(&dev_entry->cs_job_spinlock);
1401 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1403 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1405 spin_lock(&dev_entry->cs_job_spinlock);
1406 list_del(&job->debugfs_list);
1407 spin_unlock(&dev_entry->cs_job_spinlock);
1410 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1412 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1414 spin_lock(&dev_entry->userptr_spinlock);
1415 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1416 spin_unlock(&dev_entry->userptr_spinlock);
1419 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1420 struct hl_userptr *userptr)
1422 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1424 spin_lock(&dev_entry->userptr_spinlock);
1425 list_del(&userptr->debugfs_list);
1426 spin_unlock(&dev_entry->userptr_spinlock);
1429 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1431 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1433 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1434 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1435 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1438 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1440 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1442 spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1443 list_del(&ctx->debugfs_list);
1444 spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1447 void __init hl_debugfs_init(void)
1449 hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1452 void hl_debugfs_fini(void)
1454 debugfs_remove_recursive(hl_debug_root);