1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2021 HabanaLabs, Ltd.
8 #include "habanalabs.h"
9 #include "../include/hw_ip/mmu/mmu_general.h"
11 #include <linux/pci.h>
12 #include <linux/uaccess.h>
13 #include <linux/vmalloc.h>
14 #include <linux/iommu.h>
16 #define MMU_ADDR_BUF_SIZE 40
17 #define MMU_ASID_BUF_SIZE 10
18 #define MMU_KBUF_SIZE (MMU_ADDR_BUF_SIZE + MMU_ASID_BUF_SIZE)
19 #define I2C_MAX_TRANSACTION_LEN 8
21 static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
22 u8 i2c_reg, u8 i2c_len, u64 *val)
24 struct cpucp_packet pkt;
27 if (!hl_device_operational(hdev, NULL))
30 if (i2c_len > I2C_MAX_TRANSACTION_LEN) {
31 dev_err(hdev->dev, "I2C transaction length %u, exceeds maximum of %u\n",
32 i2c_len, I2C_MAX_TRANSACTION_LEN);
36 memset(&pkt, 0, sizeof(pkt));
38 pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_RD <<
39 CPUCP_PKT_CTL_OPCODE_SHIFT);
40 pkt.i2c_bus = i2c_bus;
41 pkt.i2c_addr = i2c_addr;
42 pkt.i2c_reg = i2c_reg;
43 pkt.i2c_len = i2c_len;
45 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
48 dev_err(hdev->dev, "Failed to read from I2C, error %d\n", rc);
53 static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
54 u8 i2c_reg, u8 i2c_len, u64 val)
56 struct cpucp_packet pkt;
59 if (!hl_device_operational(hdev, NULL))
62 if (i2c_len > I2C_MAX_TRANSACTION_LEN) {
63 dev_err(hdev->dev, "I2C transaction length %u, exceeds maximum of %u\n",
64 i2c_len, I2C_MAX_TRANSACTION_LEN);
68 memset(&pkt, 0, sizeof(pkt));
70 pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_WR <<
71 CPUCP_PKT_CTL_OPCODE_SHIFT);
72 pkt.i2c_bus = i2c_bus;
73 pkt.i2c_addr = i2c_addr;
74 pkt.i2c_reg = i2c_reg;
75 pkt.i2c_len = i2c_len;
76 pkt.value = cpu_to_le64(val);
78 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
82 dev_err(hdev->dev, "Failed to write to I2C, error %d\n", rc);
87 static void hl_debugfs_led_set(struct hl_device *hdev, u8 led, u8 state)
89 struct cpucp_packet pkt;
92 if (!hl_device_operational(hdev, NULL))
95 memset(&pkt, 0, sizeof(pkt));
97 pkt.ctl = cpu_to_le32(CPUCP_PACKET_LED_SET <<
98 CPUCP_PKT_CTL_OPCODE_SHIFT);
99 pkt.led_index = cpu_to_le32(led);
100 pkt.value = cpu_to_le64(state);
102 rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
106 dev_err(hdev->dev, "Failed to set LED %d, error %d\n", led, rc);
109 static int command_buffers_show(struct seq_file *s, void *data)
111 struct hl_debugfs_entry *entry = s->private;
112 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
116 spin_lock(&dev_entry->cb_spinlock);
118 list_for_each_entry(cb, &dev_entry->cb_list, debugfs_list) {
122 seq_puts(s, " CB ID CTX ID CB size CB RefCnt mmap? CS counter\n");
123 seq_puts(s, "---------------------------------------------------------------\n");
126 " %03llu %d 0x%08x %d %d %d\n",
127 cb->buf->handle, cb->ctx->asid, cb->size,
128 kref_read(&cb->buf->refcount),
129 atomic_read(&cb->buf->mmap), atomic_read(&cb->cs_cnt));
132 spin_unlock(&dev_entry->cb_spinlock);
140 static int command_submission_show(struct seq_file *s, void *data)
142 struct hl_debugfs_entry *entry = s->private;
143 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
147 spin_lock(&dev_entry->cs_spinlock);
149 list_for_each_entry(cs, &dev_entry->cs_list, debugfs_list) {
153 seq_puts(s, " CS ID CS TYPE CTX ASID CS RefCnt Submitted Completed\n");
154 seq_puts(s, "----------------------------------------------------------------\n");
157 " %llu %d %d %d %d %d\n",
158 cs->sequence, cs->type, cs->ctx->asid,
159 kref_read(&cs->refcount),
160 cs->submitted, cs->completed);
163 spin_unlock(&dev_entry->cs_spinlock);
171 static int command_submission_jobs_show(struct seq_file *s, void *data)
173 struct hl_debugfs_entry *entry = s->private;
174 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
175 struct hl_cs_job *job;
178 spin_lock(&dev_entry->cs_job_spinlock);
180 list_for_each_entry(job, &dev_entry->cs_job_list, debugfs_list) {
184 seq_puts(s, " JOB ID CS ID CS TYPE CTX ASID JOB RefCnt H/W Queue\n");
185 seq_puts(s, "---------------------------------------------------------------\n");
189 " %02d %llu %d %d %d %d\n",
190 job->id, job->cs->sequence, job->cs->type,
191 job->cs->ctx->asid, kref_read(&job->refcount),
195 " %02d 0 0 %d %d %d\n",
196 job->id, HL_KERNEL_ASID_ID,
197 kref_read(&job->refcount), job->hw_queue_id);
200 spin_unlock(&dev_entry->cs_job_spinlock);
208 static int userptr_show(struct seq_file *s, void *data)
210 struct hl_debugfs_entry *entry = s->private;
211 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
212 struct hl_userptr *userptr;
213 char dma_dir[4][30] = {"DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
214 "DMA_FROM_DEVICE", "DMA_NONE"};
217 spin_lock(&dev_entry->userptr_spinlock);
219 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
223 seq_puts(s, " pid user virtual address size dma dir\n");
224 seq_puts(s, "----------------------------------------------------------\n");
226 seq_printf(s, " %-7d 0x%-14llx %-10llu %-30s\n",
227 userptr->pid, userptr->addr, userptr->size,
228 dma_dir[userptr->dir]);
231 spin_unlock(&dev_entry->userptr_spinlock);
239 static int vm_show(struct seq_file *s, void *data)
241 struct hl_debugfs_entry *entry = s->private;
242 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
243 struct hl_vm_hw_block_list_node *lnode;
246 struct hl_vm_hash_node *hnode;
247 struct hl_userptr *userptr;
248 struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
249 struct hl_va_range *va_range;
250 struct hl_vm_va_block *va_block;
251 enum vm_type *vm_type;
256 mutex_lock(&dev_entry->ctx_mem_hash_mutex);
258 list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
260 seq_puts(s, "\n\n----------------------------------------------------");
261 seq_puts(s, "\n----------------------------------------------------\n\n");
262 seq_printf(s, "ctx asid: %u\n", ctx->asid);
264 seq_puts(s, "\nmappings:\n\n");
265 seq_puts(s, " virtual address size handle\n");
266 seq_puts(s, "----------------------------------------------------\n");
267 mutex_lock(&ctx->mem_hash_lock);
268 hash_for_each(ctx->mem_hash, i, hnode, node) {
269 vm_type = hnode->ptr;
271 if (*vm_type == VM_TYPE_USERPTR) {
272 userptr = hnode->ptr;
274 " 0x%-14llx %-10llu\n",
275 hnode->vaddr, userptr->size);
277 phys_pg_pack = hnode->ptr;
279 " 0x%-14llx %-10llu %-4u\n",
280 hnode->vaddr, phys_pg_pack->total_size,
281 phys_pg_pack->handle);
284 mutex_unlock(&ctx->mem_hash_lock);
286 if (ctx->asid != HL_KERNEL_ASID_ID &&
287 !list_empty(&ctx->hw_block_mem_list)) {
288 seq_puts(s, "\nhw_block mappings:\n\n");
290 " virtual address block size mapped size HW block id\n");
292 "---------------------------------------------------------------\n");
293 mutex_lock(&ctx->hw_block_list_lock);
294 list_for_each_entry(lnode, &ctx->hw_block_mem_list, node) {
296 " 0x%-14lx %-6u %-6u %-9u\n",
297 lnode->vaddr, lnode->block_size, lnode->mapped_size,
300 mutex_unlock(&ctx->hw_block_list_lock);
304 spin_lock(&vm->idr_lock);
306 if (!idr_is_empty(&vm->phys_pg_pack_handles))
307 seq_puts(s, "\n\nallocations:\n");
309 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
310 if (phys_pg_pack->asid != ctx->asid)
313 seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
314 seq_printf(s, "page size: %u\n\n",
315 phys_pg_pack->page_size);
316 seq_puts(s, " physical address\n");
317 seq_puts(s, "---------------------\n");
318 for (j = 0 ; j < phys_pg_pack->npages ; j++) {
319 seq_printf(s, " 0x%-14llx\n",
320 phys_pg_pack->pages[j]);
323 spin_unlock(&vm->idr_lock);
327 mutex_unlock(&dev_entry->ctx_mem_hash_mutex);
329 ctx = hl_get_compute_ctx(dev_entry->hdev);
331 seq_puts(s, "\nVA ranges:\n\n");
332 for (i = HL_VA_RANGE_TYPE_HOST ; i < HL_VA_RANGE_TYPE_MAX ; ++i) {
333 va_range = ctx->va_range[i];
334 seq_printf(s, " va_range %d\n", i);
335 seq_puts(s, "---------------------\n");
336 mutex_lock(&va_range->lock);
337 list_for_each_entry(va_block, &va_range->list, node) {
338 seq_printf(s, "%#16llx - %#16llx (%#llx)\n",
339 va_block->start, va_block->end,
342 mutex_unlock(&va_range->lock);
354 static int userptr_lookup_show(struct seq_file *s, void *data)
356 struct hl_debugfs_entry *entry = s->private;
357 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
358 struct scatterlist *sg;
359 struct hl_userptr *userptr;
361 u64 total_npages, npages, sg_start, sg_end;
365 spin_lock(&dev_entry->userptr_spinlock);
367 list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
368 if (dev_entry->userptr_lookup >= userptr->addr &&
369 dev_entry->userptr_lookup < userptr->addr + userptr->size) {
371 for_each_sgtable_dma_sg(userptr->sgt, sg, i) {
372 npages = hl_get_sg_info(sg, &dma_addr);
373 sg_start = userptr->addr +
374 total_npages * PAGE_SIZE;
375 sg_end = userptr->addr +
376 (total_npages + npages) * PAGE_SIZE;
378 if (dev_entry->userptr_lookup >= sg_start &&
379 dev_entry->userptr_lookup < sg_end) {
380 dma_addr += (dev_entry->userptr_lookup -
385 seq_puts(s, " user virtual address dma address pid region start region size\n");
386 seq_puts(s, "---------------------------------------------------------------------------------------\n");
388 seq_printf(s, " 0x%-18llx 0x%-16llx %-8u 0x%-16llx %-12llu\n",
389 dev_entry->userptr_lookup,
390 (u64)dma_addr, userptr->pid,
391 userptr->addr, userptr->size);
393 total_npages += npages;
398 spin_unlock(&dev_entry->userptr_spinlock);
406 static ssize_t userptr_lookup_write(struct file *file, const char __user *buf,
407 size_t count, loff_t *f_pos)
409 struct seq_file *s = file->private_data;
410 struct hl_debugfs_entry *entry = s->private;
411 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
415 rc = kstrtoull_from_user(buf, count, 16, &value);
419 dev_entry->userptr_lookup = value;
424 static int mmu_show(struct seq_file *s, void *data)
426 struct hl_debugfs_entry *entry = s->private;
427 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
428 struct hl_device *hdev = dev_entry->hdev;
430 struct hl_mmu_hop_info hops_info = {0};
431 u64 virt_addr = dev_entry->mmu_addr, phys_addr;
434 if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
435 ctx = hdev->kernel_ctx;
437 ctx = hl_get_compute_ctx(hdev);
440 dev_err(hdev->dev, "no ctx available\n");
444 if (hl_mmu_get_tlb_info(ctx, virt_addr, &hops_info)) {
445 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
450 hl_mmu_va_to_pa(ctx, virt_addr, &phys_addr);
452 if (hops_info.scrambled_vaddr &&
453 (dev_entry->mmu_addr != hops_info.scrambled_vaddr))
455 "asid: %u, virt_addr: 0x%llx, scrambled virt_addr: 0x%llx,\nphys_addr: 0x%llx, scrambled_phys_addr: 0x%llx\n",
456 dev_entry->mmu_asid, dev_entry->mmu_addr,
457 hops_info.scrambled_vaddr,
458 hops_info.unscrambled_paddr, phys_addr);
461 "asid: %u, virt_addr: 0x%llx, phys_addr: 0x%llx\n",
462 dev_entry->mmu_asid, dev_entry->mmu_addr, phys_addr);
464 for (i = 0 ; i < hops_info.used_hops ; i++) {
465 seq_printf(s, "hop%d_addr: 0x%llx\n",
466 i, hops_info.hop_info[i].hop_addr);
467 seq_printf(s, "hop%d_pte_addr: 0x%llx\n",
468 i, hops_info.hop_info[i].hop_pte_addr);
469 seq_printf(s, "hop%d_pte: 0x%llx\n",
470 i, hops_info.hop_info[i].hop_pte_val);
474 if (dev_entry->mmu_asid != HL_KERNEL_ASID_ID)
480 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
481 size_t count, loff_t *f_pos)
483 struct seq_file *s = file->private_data;
484 struct hl_debugfs_entry *entry = s->private;
485 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
486 struct hl_device *hdev = dev_entry->hdev;
487 char kbuf[MMU_KBUF_SIZE];
491 if (count > sizeof(kbuf) - 1)
493 if (copy_from_user(kbuf, buf, count))
497 c = strchr(kbuf, ' ');
502 rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
506 if (strncmp(c+1, "0x", 2))
508 rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
515 dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
520 static int mmu_ack_error(struct seq_file *s, void *data)
522 struct hl_debugfs_entry *entry = s->private;
523 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
524 struct hl_device *hdev = dev_entry->hdev;
527 if (!dev_entry->mmu_cap_mask) {
528 dev_err(hdev->dev, "mmu_cap_mask is not set\n");
532 rc = hdev->asic_funcs->ack_mmu_errors(hdev, dev_entry->mmu_cap_mask);
541 static ssize_t mmu_ack_error_value_write(struct file *file,
542 const char __user *buf,
543 size_t count, loff_t *f_pos)
545 struct seq_file *s = file->private_data;
546 struct hl_debugfs_entry *entry = s->private;
547 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
548 struct hl_device *hdev = dev_entry->hdev;
549 char kbuf[MMU_KBUF_SIZE];
552 if (count > sizeof(kbuf) - 1)
555 if (copy_from_user(kbuf, buf, count))
560 if (strncmp(kbuf, "0x", 2))
563 rc = kstrtoull(kbuf, 16, &dev_entry->mmu_cap_mask);
569 dev_err(hdev->dev, "usage: echo <0xmmu_cap_mask > > mmu_error\n");
574 static int engines_show(struct seq_file *s, void *data)
576 struct hl_debugfs_entry *entry = s->private;
577 struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
578 struct hl_device *hdev = dev_entry->hdev;
579 struct engines_data eng_data;
581 if (hdev->reset_info.in_reset) {
582 dev_warn_ratelimited(hdev->dev,
583 "Can't check device idle during reset\n");
587 eng_data.actual_size = 0;
588 eng_data.allocated_buf_size = HL_ENGINES_DATA_MAX_SIZE;
589 eng_data.buf = vmalloc(eng_data.allocated_buf_size);
593 hdev->asic_funcs->is_device_idle(hdev, NULL, 0, &eng_data);
595 if (eng_data.actual_size > eng_data.allocated_buf_size) {
597 "Engines data size (%d Bytes) is bigger than allocated size (%u Bytes)\n",
598 eng_data.actual_size, eng_data.allocated_buf_size);
603 seq_write(s, eng_data.buf, eng_data.actual_size);
610 static ssize_t hl_memory_scrub(struct file *f, const char __user *buf,
611 size_t count, loff_t *ppos)
613 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
614 struct hl_device *hdev = entry->hdev;
615 u64 val = hdev->memory_scrub_val;
618 if (!hl_device_operational(hdev, NULL)) {
619 dev_warn_ratelimited(hdev->dev, "Can't scrub memory, device is not operational\n");
623 mutex_lock(&hdev->fpriv_list_lock);
624 if (hdev->is_compute_ctx_active) {
625 mutex_unlock(&hdev->fpriv_list_lock);
626 dev_err(hdev->dev, "can't scrub dram, context exist\n");
629 hdev->is_in_dram_scrub = true;
630 mutex_unlock(&hdev->fpriv_list_lock);
632 rc = hdev->asic_funcs->scrub_device_dram(hdev, val);
634 mutex_lock(&hdev->fpriv_list_lock);
635 hdev->is_in_dram_scrub = false;
636 mutex_unlock(&hdev->fpriv_list_lock);
643 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
645 struct asic_fixed_properties *prop = &hdev->asic_prop;
647 if (prop->dram_supports_virtual_memory &&
648 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
651 if (addr >= prop->pmmu.start_addr &&
652 addr < prop->pmmu.end_addr)
655 if (addr >= prop->pmmu_huge.start_addr &&
656 addr < prop->pmmu_huge.end_addr)
662 static bool hl_is_device_internal_memory_va(struct hl_device *hdev, u64 addr,
665 struct asic_fixed_properties *prop = &hdev->asic_prop;
666 u64 dram_start_addr, dram_end_addr;
668 if (prop->dram_supports_virtual_memory) {
669 dram_start_addr = prop->dmmu.start_addr;
670 dram_end_addr = prop->dmmu.end_addr;
672 dram_start_addr = prop->dram_base_address;
673 dram_end_addr = prop->dram_end_address;
676 if (hl_mem_area_inside_range(addr, size, dram_start_addr,
680 if (hl_mem_area_inside_range(addr, size, prop->sram_base_address,
681 prop->sram_end_address))
687 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr, u32 size,
690 struct hl_vm_phys_pg_pack *phys_pg_pack;
692 struct hl_vm_hash_node *hnode;
693 u64 end_address, range_size;
694 struct hl_userptr *userptr;
695 enum vm_type *vm_type;
699 ctx = hl_get_compute_ctx(hdev);
702 dev_err(hdev->dev, "no ctx available\n");
706 /* Verify address is mapped */
707 mutex_lock(&ctx->mem_hash_lock);
708 hash_for_each(ctx->mem_hash, i, hnode, node) {
709 vm_type = hnode->ptr;
711 if (*vm_type == VM_TYPE_USERPTR) {
712 userptr = hnode->ptr;
713 range_size = userptr->size;
715 phys_pg_pack = hnode->ptr;
716 range_size = phys_pg_pack->total_size;
719 end_address = virt_addr + size;
720 if ((virt_addr >= hnode->vaddr) &&
721 (end_address <= hnode->vaddr + range_size)) {
726 mutex_unlock(&ctx->mem_hash_lock);
730 "virt addr 0x%llx is not mapped\n",
736 rc = hl_mmu_va_to_pa(ctx, virt_addr, phys_addr);
739 "virt addr 0x%llx is not mapped to phys addr\n",
750 static int hl_access_dev_mem_by_region(struct hl_device *hdev, u64 addr,
751 u64 *val, enum debugfs_access_type acc_type, bool *found)
753 size_t acc_size = (acc_type == DEBUGFS_READ64 || acc_type == DEBUGFS_WRITE64) ?
754 sizeof(u64) : sizeof(u32);
755 struct pci_mem_region *mem_reg;
758 for (i = 0; i < PCI_REGION_NUMBER; i++) {
759 mem_reg = &hdev->pci_mem_region[i];
762 if (addr >= mem_reg->region_base &&
763 addr <= mem_reg->region_base + mem_reg->region_size - acc_size) {
765 return hdev->asic_funcs->access_dev_mem(hdev, i, addr, val, acc_type);
771 static void hl_access_host_mem(struct hl_device *hdev, u64 addr, u64 *val,
772 enum debugfs_access_type acc_type)
774 struct asic_fixed_properties *prop = &hdev->asic_prop;
775 u64 offset = prop->device_dma_offset_for_host_access;
779 *val = *(u32 *) phys_to_virt(addr - offset);
781 case DEBUGFS_WRITE32:
782 *(u32 *) phys_to_virt(addr - offset) = *val;
785 *val = *(u64 *) phys_to_virt(addr - offset);
787 case DEBUGFS_WRITE64:
788 *(u64 *) phys_to_virt(addr - offset) = *val;
791 dev_err(hdev->dev, "hostmem access-type %d id not supported\n", acc_type);
796 static int hl_access_mem(struct hl_device *hdev, u64 addr, u64 *val,
797 enum debugfs_access_type acc_type)
799 size_t acc_size = (acc_type == DEBUGFS_READ64 || acc_type == DEBUGFS_WRITE64) ?
800 sizeof(u64) : sizeof(u32);
801 u64 host_start = hdev->asic_prop.host_base_address;
802 u64 host_end = hdev->asic_prop.host_end_address;
803 bool user_address, found = false;
806 user_address = hl_is_device_va(hdev, addr);
808 rc = device_va_to_pa(hdev, addr, acc_size, &addr);
813 rc = hl_access_dev_mem_by_region(hdev, addr, val, acc_type, &found);
816 "Failed reading addr %#llx from dev mem (%d)\n",
824 if (!user_address || device_iommu_mapped(&hdev->pdev->dev)) {
829 if (addr >= host_start && addr <= host_end - acc_size) {
830 hl_access_host_mem(hdev, addr, val, acc_type);
838 dev_err(hdev->dev, "invalid addr %#llx\n", addr);
842 static ssize_t hl_data_read32(struct file *f, char __user *buf,
843 size_t count, loff_t *ppos)
845 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
846 struct hl_device *hdev = entry->hdev;
847 u64 value64, addr = entry->addr;
852 if (hdev->reset_info.in_reset) {
853 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
860 rc = hl_access_mem(hdev, addr, &value64, DEBUGFS_READ32);
864 val = value64; /* downcast back to 32 */
866 sprintf(tmp_buf, "0x%08x\n", val);
867 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
871 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
872 size_t count, loff_t *ppos)
874 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
875 struct hl_device *hdev = entry->hdev;
876 u64 value64, addr = entry->addr;
880 if (hdev->reset_info.in_reset) {
881 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
885 rc = kstrtouint_from_user(buf, count, 16, &value);
890 rc = hl_access_mem(hdev, addr, &value64, DEBUGFS_WRITE32);
897 static ssize_t hl_data_read64(struct file *f, char __user *buf,
898 size_t count, loff_t *ppos)
900 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
901 struct hl_device *hdev = entry->hdev;
902 u64 addr = entry->addr;
907 if (hdev->reset_info.in_reset) {
908 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
915 rc = hl_access_mem(hdev, addr, &val, DEBUGFS_READ64);
919 sprintf(tmp_buf, "0x%016llx\n", val);
920 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
924 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
925 size_t count, loff_t *ppos)
927 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
928 struct hl_device *hdev = entry->hdev;
929 u64 addr = entry->addr;
933 if (hdev->reset_info.in_reset) {
934 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
938 rc = kstrtoull_from_user(buf, count, 16, &value);
942 rc = hl_access_mem(hdev, addr, &value, DEBUGFS_WRITE64);
949 static ssize_t hl_dma_size_write(struct file *f, const char __user *buf,
950 size_t count, loff_t *ppos)
952 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
953 struct hl_device *hdev = entry->hdev;
954 u64 addr = entry->addr;
958 if (hdev->reset_info.in_reset) {
959 dev_warn_ratelimited(hdev->dev, "Can't DMA during reset\n");
962 rc = kstrtouint_from_user(buf, count, 16, &size);
967 dev_err(hdev->dev, "DMA read failed. size can't be 0\n");
971 if (size > SZ_128M) {
973 "DMA read failed. size can't be larger than 128MB\n");
977 if (!hl_is_device_internal_memory_va(hdev, addr, size)) {
979 "DMA read failed. Invalid 0x%010llx + 0x%08x\n",
984 /* Free the previous allocation, if there was any */
985 entry->data_dma_blob_desc.size = 0;
986 vfree(entry->data_dma_blob_desc.data);
988 entry->data_dma_blob_desc.data = vmalloc(size);
989 if (!entry->data_dma_blob_desc.data)
992 rc = hdev->asic_funcs->debugfs_read_dma(hdev, addr, size,
993 entry->data_dma_blob_desc.data);
995 dev_err(hdev->dev, "Failed to DMA from 0x%010llx\n", addr);
996 vfree(entry->data_dma_blob_desc.data);
997 entry->data_dma_blob_desc.data = NULL;
1001 entry->data_dma_blob_desc.size = size;
1006 static ssize_t hl_monitor_dump_trigger(struct file *f, const char __user *buf,
1007 size_t count, loff_t *ppos)
1009 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1010 struct hl_device *hdev = entry->hdev;
1014 if (hdev->reset_info.in_reset) {
1015 dev_warn_ratelimited(hdev->dev, "Can't dump monitors during reset\n");
1018 rc = kstrtouint_from_user(buf, count, 10, &trig);
1023 dev_err(hdev->dev, "Must write 1 to trigger monitor dump\n");
1027 size = sizeof(struct cpucp_monitor_dump);
1029 /* Free the previous allocation, if there was any */
1030 entry->mon_dump_blob_desc.size = 0;
1031 vfree(entry->mon_dump_blob_desc.data);
1033 entry->mon_dump_blob_desc.data = vmalloc(size);
1034 if (!entry->mon_dump_blob_desc.data)
1037 rc = hdev->asic_funcs->get_monitor_dump(hdev, entry->mon_dump_blob_desc.data);
1039 dev_err(hdev->dev, "Failed to dump monitors\n");
1040 vfree(entry->mon_dump_blob_desc.data);
1041 entry->mon_dump_blob_desc.data = NULL;
1045 entry->mon_dump_blob_desc.size = size;
1050 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
1051 size_t count, loff_t *ppos)
1053 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1054 struct hl_device *hdev = entry->hdev;
1061 if (hdev->pdev->current_state == PCI_D0)
1063 else if (hdev->pdev->current_state == PCI_D3hot)
1069 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
1070 return simple_read_from_buffer(buf, count, ppos, tmp_buf,
1074 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
1075 size_t count, loff_t *ppos)
1077 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1078 struct hl_device *hdev = entry->hdev;
1082 rc = kstrtouint_from_user(buf, count, 10, &value);
1087 pci_set_power_state(hdev->pdev, PCI_D0);
1088 pci_restore_state(hdev->pdev);
1089 rc = pci_enable_device(hdev->pdev);
1092 } else if (value == 2) {
1093 pci_save_state(hdev->pdev);
1094 pci_disable_device(hdev->pdev);
1095 pci_set_power_state(hdev->pdev, PCI_D3hot);
1097 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
1104 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
1105 size_t count, loff_t *ppos)
1107 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1108 struct hl_device *hdev = entry->hdev;
1116 rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
1117 entry->i2c_reg, entry->i2c_len, &val);
1120 "Failed to read from I2C bus %d, addr %d, reg %d, len %d\n",
1121 entry->i2c_bus, entry->i2c_addr, entry->i2c_reg, entry->i2c_len);
1125 sprintf(tmp_buf, "%#02llx\n", val);
1126 rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
1132 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
1133 size_t count, loff_t *ppos)
1135 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1136 struct hl_device *hdev = entry->hdev;
1140 rc = kstrtou64_from_user(buf, count, 16, &value);
1144 rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
1145 entry->i2c_reg, entry->i2c_len, value);
1148 "Failed to write %#02llx to I2C bus %d, addr %d, reg %d, len %d\n",
1149 value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg, entry->i2c_len);
1156 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
1157 size_t count, loff_t *ppos)
1159 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1160 struct hl_device *hdev = entry->hdev;
1164 rc = kstrtouint_from_user(buf, count, 10, &value);
1168 value = value ? 1 : 0;
1170 hl_debugfs_led_set(hdev, 0, value);
1175 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
1176 size_t count, loff_t *ppos)
1178 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1179 struct hl_device *hdev = entry->hdev;
1183 rc = kstrtouint_from_user(buf, count, 10, &value);
1187 value = value ? 1 : 0;
1189 hl_debugfs_led_set(hdev, 1, value);
1194 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
1195 size_t count, loff_t *ppos)
1197 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1198 struct hl_device *hdev = entry->hdev;
1202 rc = kstrtouint_from_user(buf, count, 10, &value);
1206 value = value ? 1 : 0;
1208 hl_debugfs_led_set(hdev, 2, value);
1213 static ssize_t hl_device_read(struct file *f, char __user *buf,
1214 size_t count, loff_t *ppos)
1216 static const char *help =
1217 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
1218 return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
1221 static ssize_t hl_device_write(struct file *f, const char __user *buf,
1222 size_t count, loff_t *ppos)
1224 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1225 struct hl_device *hdev = entry->hdev;
1226 char data[30] = {0};
1228 /* don't allow partial writes */
1232 simple_write_to_buffer(data, 29, ppos, buf, count);
1234 if (strncmp("disable", data, strlen("disable")) == 0) {
1235 hdev->disabled = true;
1236 } else if (strncmp("enable", data, strlen("enable")) == 0) {
1237 hdev->disabled = false;
1238 } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
1239 hdev->asic_funcs->suspend(hdev);
1240 } else if (strncmp("resume", data, strlen("resume")) == 0) {
1241 hdev->asic_funcs->resume(hdev);
1242 } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
1243 hdev->device_cpu_disabled = true;
1246 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
1253 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
1254 size_t count, loff_t *ppos)
1259 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1260 size_t count, loff_t *ppos)
1265 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1266 size_t count, loff_t *ppos)
1268 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1269 struct hl_device *hdev = entry->hdev;
1273 if (!hdev->asic_prop.configurable_stop_on_err)
1279 sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1280 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1281 strlen(tmp_buf) + 1);
1286 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1287 size_t count, loff_t *ppos)
1289 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1290 struct hl_device *hdev = entry->hdev;
1294 if (!hdev->asic_prop.configurable_stop_on_err)
1297 if (hdev->reset_info.in_reset) {
1298 dev_warn_ratelimited(hdev->dev,
1299 "Can't change stop on error during reset\n");
1303 rc = kstrtouint_from_user(buf, count, 10, &value);
1307 hdev->stop_on_err = value ? 1 : 0;
1309 hl_device_reset(hdev, 0);
1314 static ssize_t hl_security_violations_read(struct file *f, char __user *buf,
1315 size_t count, loff_t *ppos)
1317 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1318 struct hl_device *hdev = entry->hdev;
1320 hdev->asic_funcs->ack_protection_bits_errors(hdev);
1325 static ssize_t hl_state_dump_read(struct file *f, char __user *buf,
1326 size_t count, loff_t *ppos)
1328 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1331 down_read(&entry->state_dump_sem);
1332 if (!entry->state_dump[entry->state_dump_head])
1335 rc = simple_read_from_buffer(
1337 entry->state_dump[entry->state_dump_head],
1338 strlen(entry->state_dump[entry->state_dump_head]));
1339 up_read(&entry->state_dump_sem);
1344 static ssize_t hl_state_dump_write(struct file *f, const char __user *buf,
1345 size_t count, loff_t *ppos)
1347 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1348 struct hl_device *hdev = entry->hdev;
1353 rc = kstrtouint_from_user(buf, count, 10, &size);
1357 if (size <= 0 || size >= ARRAY_SIZE(entry->state_dump)) {
1358 dev_err(hdev->dev, "Invalid number of dumps to skip\n");
1362 if (entry->state_dump[entry->state_dump_head]) {
1363 down_write(&entry->state_dump_sem);
1364 for (i = 0; i < size; ++i) {
1365 vfree(entry->state_dump[entry->state_dump_head]);
1366 entry->state_dump[entry->state_dump_head] = NULL;
1367 if (entry->state_dump_head > 0)
1368 entry->state_dump_head--;
1370 entry->state_dump_head =
1371 ARRAY_SIZE(entry->state_dump) - 1;
1373 up_write(&entry->state_dump_sem);
1379 static ssize_t hl_timeout_locked_read(struct file *f, char __user *buf,
1380 size_t count, loff_t *ppos)
1382 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1383 struct hl_device *hdev = entry->hdev;
1390 sprintf(tmp_buf, "%d\n",
1391 jiffies_to_msecs(hdev->timeout_jiffies) / 1000);
1392 rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1393 strlen(tmp_buf) + 1);
1398 static ssize_t hl_timeout_locked_write(struct file *f, const char __user *buf,
1399 size_t count, loff_t *ppos)
1401 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1402 struct hl_device *hdev = entry->hdev;
1406 rc = kstrtouint_from_user(buf, count, 10, &value);
1411 hdev->timeout_jiffies = msecs_to_jiffies(value * 1000);
1413 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
1418 static ssize_t hl_check_razwi_happened(struct file *f, char __user *buf,
1419 size_t count, loff_t *ppos)
1421 struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1422 struct hl_device *hdev = entry->hdev;
1424 hdev->asic_funcs->check_if_razwi_happened(hdev);
1429 static const struct file_operations hl_mem_scrub_fops = {
1430 .owner = THIS_MODULE,
1431 .write = hl_memory_scrub,
1434 static const struct file_operations hl_data32b_fops = {
1435 .owner = THIS_MODULE,
1436 .read = hl_data_read32,
1437 .write = hl_data_write32
1440 static const struct file_operations hl_data64b_fops = {
1441 .owner = THIS_MODULE,
1442 .read = hl_data_read64,
1443 .write = hl_data_write64
1446 static const struct file_operations hl_dma_size_fops = {
1447 .owner = THIS_MODULE,
1448 .write = hl_dma_size_write
1451 static const struct file_operations hl_monitor_dump_fops = {
1452 .owner = THIS_MODULE,
1453 .write = hl_monitor_dump_trigger
1456 static const struct file_operations hl_i2c_data_fops = {
1457 .owner = THIS_MODULE,
1458 .read = hl_i2c_data_read,
1459 .write = hl_i2c_data_write
1462 static const struct file_operations hl_power_fops = {
1463 .owner = THIS_MODULE,
1464 .read = hl_get_power_state,
1465 .write = hl_set_power_state
1468 static const struct file_operations hl_led0_fops = {
1469 .owner = THIS_MODULE,
1470 .write = hl_led0_write
1473 static const struct file_operations hl_led1_fops = {
1474 .owner = THIS_MODULE,
1475 .write = hl_led1_write
1478 static const struct file_operations hl_led2_fops = {
1479 .owner = THIS_MODULE,
1480 .write = hl_led2_write
1483 static const struct file_operations hl_device_fops = {
1484 .owner = THIS_MODULE,
1485 .read = hl_device_read,
1486 .write = hl_device_write
1489 static const struct file_operations hl_clk_gate_fops = {
1490 .owner = THIS_MODULE,
1491 .read = hl_clk_gate_read,
1492 .write = hl_clk_gate_write
1495 static const struct file_operations hl_stop_on_err_fops = {
1496 .owner = THIS_MODULE,
1497 .read = hl_stop_on_err_read,
1498 .write = hl_stop_on_err_write
1501 static const struct file_operations hl_security_violations_fops = {
1502 .owner = THIS_MODULE,
1503 .read = hl_security_violations_read
1506 static const struct file_operations hl_state_dump_fops = {
1507 .owner = THIS_MODULE,
1508 .read = hl_state_dump_read,
1509 .write = hl_state_dump_write
1512 static const struct file_operations hl_timeout_locked_fops = {
1513 .owner = THIS_MODULE,
1514 .read = hl_timeout_locked_read,
1515 .write = hl_timeout_locked_write
1518 static const struct file_operations hl_razwi_check_fops = {
1519 .owner = THIS_MODULE,
1520 .read = hl_check_razwi_happened
1523 static const struct hl_info_list hl_debugfs_list[] = {
1524 {"command_buffers", command_buffers_show, NULL},
1525 {"command_submission", command_submission_show, NULL},
1526 {"command_submission_jobs", command_submission_jobs_show, NULL},
1527 {"userptr", userptr_show, NULL},
1528 {"vm", vm_show, NULL},
1529 {"userptr_lookup", userptr_lookup_show, userptr_lookup_write},
1530 {"mmu", mmu_show, mmu_asid_va_write},
1531 {"mmu_error", mmu_ack_error, mmu_ack_error_value_write},
1532 {"engines", engines_show, NULL},
1535 static int hl_debugfs_open(struct inode *inode, struct file *file)
1537 struct hl_debugfs_entry *node = inode->i_private;
1539 return single_open(file, node->info_ent->show, node);
1542 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1543 size_t count, loff_t *f_pos)
1545 struct hl_debugfs_entry *node = file->f_inode->i_private;
1547 if (node->info_ent->write)
1548 return node->info_ent->write(file, buf, count, f_pos);
1554 static const struct file_operations hl_debugfs_fops = {
1555 .owner = THIS_MODULE,
1556 .open = hl_debugfs_open,
1558 .write = hl_debugfs_write,
1559 .llseek = seq_lseek,
1560 .release = single_release,
1563 static void add_secured_nodes(struct hl_dbg_device_entry *dev_entry, struct dentry *root)
1565 debugfs_create_u8("i2c_bus",
1568 &dev_entry->i2c_bus);
1570 debugfs_create_u8("i2c_addr",
1573 &dev_entry->i2c_addr);
1575 debugfs_create_u8("i2c_reg",
1578 &dev_entry->i2c_reg);
1580 debugfs_create_u8("i2c_len",
1583 &dev_entry->i2c_len);
1585 debugfs_create_file("i2c_data",
1591 debugfs_create_file("led0",
1597 debugfs_create_file("led1",
1603 debugfs_create_file("led2",
1610 static void add_files_to_device(struct hl_device *hdev, struct hl_dbg_device_entry *dev_entry,
1611 struct dentry *root)
1613 int count = ARRAY_SIZE(hl_debugfs_list);
1614 struct hl_debugfs_entry *entry;
1617 debugfs_create_x64("memory_scrub_val",
1620 &hdev->memory_scrub_val);
1622 debugfs_create_file("memory_scrub",
1626 &hl_mem_scrub_fops);
1628 debugfs_create_x64("addr",
1633 debugfs_create_file("data32",
1639 debugfs_create_file("data64",
1645 debugfs_create_file("set_power_state",
1651 debugfs_create_file("device",
1657 debugfs_create_file("clk_gate",
1663 debugfs_create_file("stop_on_err",
1667 &hl_stop_on_err_fops);
1669 debugfs_create_file("dump_security_violations",
1673 &hl_security_violations_fops);
1675 debugfs_create_file("dump_razwi_events",
1679 &hl_razwi_check_fops);
1681 debugfs_create_file("dma_size",
1687 debugfs_create_blob("data_dma",
1690 &dev_entry->data_dma_blob_desc);
1692 debugfs_create_file("monitor_dump_trig",
1696 &hl_monitor_dump_fops);
1698 debugfs_create_blob("monitor_dump",
1701 &dev_entry->mon_dump_blob_desc);
1703 debugfs_create_x8("skip_reset_on_timeout",
1706 &hdev->reset_info.skip_reset_on_timeout);
1708 debugfs_create_file("state_dump",
1712 &hl_state_dump_fops);
1714 debugfs_create_file("timeout_locked",
1718 &hl_timeout_locked_fops);
1720 debugfs_create_u32("device_release_watchdog_timeout",
1723 &hdev->device_release_watchdog_timeout_sec);
1725 for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1726 debugfs_create_file(hl_debugfs_list[i].name,
1731 entry->info_ent = &hl_debugfs_list[i];
1732 entry->dev_entry = dev_entry;
1736 int hl_debugfs_device_init(struct hl_device *hdev)
1738 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1739 int count = ARRAY_SIZE(hl_debugfs_list);
1741 dev_entry->hdev = hdev;
1742 dev_entry->entry_arr = kmalloc_array(count, sizeof(struct hl_debugfs_entry), GFP_KERNEL);
1743 if (!dev_entry->entry_arr)
1746 dev_entry->data_dma_blob_desc.size = 0;
1747 dev_entry->data_dma_blob_desc.data = NULL;
1748 dev_entry->mon_dump_blob_desc.size = 0;
1749 dev_entry->mon_dump_blob_desc.data = NULL;
1751 INIT_LIST_HEAD(&dev_entry->file_list);
1752 INIT_LIST_HEAD(&dev_entry->cb_list);
1753 INIT_LIST_HEAD(&dev_entry->cs_list);
1754 INIT_LIST_HEAD(&dev_entry->cs_job_list);
1755 INIT_LIST_HEAD(&dev_entry->userptr_list);
1756 INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1757 mutex_init(&dev_entry->file_mutex);
1758 init_rwsem(&dev_entry->state_dump_sem);
1759 spin_lock_init(&dev_entry->cb_spinlock);
1760 spin_lock_init(&dev_entry->cs_spinlock);
1761 spin_lock_init(&dev_entry->cs_job_spinlock);
1762 spin_lock_init(&dev_entry->userptr_spinlock);
1763 mutex_init(&dev_entry->ctx_mem_hash_mutex);
1768 void hl_debugfs_device_fini(struct hl_device *hdev)
1770 struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1773 mutex_destroy(&entry->ctx_mem_hash_mutex);
1774 mutex_destroy(&entry->file_mutex);
1776 vfree(entry->data_dma_blob_desc.data);
1777 vfree(entry->mon_dump_blob_desc.data);
1779 for (i = 0; i < ARRAY_SIZE(entry->state_dump); ++i)
1780 vfree(entry->state_dump[i]);
1782 kfree(entry->entry_arr);
1785 void hl_debugfs_add_device(struct hl_device *hdev)
1787 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1789 dev_entry->root = hdev->drm.accel->debugfs_root;
1791 add_files_to_device(hdev, dev_entry, dev_entry->root);
1793 if (!hdev->asic_prop.fw_security_enabled)
1794 add_secured_nodes(dev_entry, dev_entry->root);
1797 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1799 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1801 mutex_lock(&dev_entry->file_mutex);
1802 list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1803 mutex_unlock(&dev_entry->file_mutex);
1806 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1808 struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1810 mutex_lock(&dev_entry->file_mutex);
1811 list_del(&hpriv->debugfs_list);
1812 mutex_unlock(&dev_entry->file_mutex);
1815 void hl_debugfs_add_cb(struct hl_cb *cb)
1817 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1819 spin_lock(&dev_entry->cb_spinlock);
1820 list_add(&cb->debugfs_list, &dev_entry->cb_list);
1821 spin_unlock(&dev_entry->cb_spinlock);
1824 void hl_debugfs_remove_cb(struct hl_cb *cb)
1826 struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1828 spin_lock(&dev_entry->cb_spinlock);
1829 list_del(&cb->debugfs_list);
1830 spin_unlock(&dev_entry->cb_spinlock);
1833 void hl_debugfs_add_cs(struct hl_cs *cs)
1835 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1837 spin_lock(&dev_entry->cs_spinlock);
1838 list_add(&cs->debugfs_list, &dev_entry->cs_list);
1839 spin_unlock(&dev_entry->cs_spinlock);
1842 void hl_debugfs_remove_cs(struct hl_cs *cs)
1844 struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1846 spin_lock(&dev_entry->cs_spinlock);
1847 list_del(&cs->debugfs_list);
1848 spin_unlock(&dev_entry->cs_spinlock);
1851 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1853 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1855 spin_lock(&dev_entry->cs_job_spinlock);
1856 list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1857 spin_unlock(&dev_entry->cs_job_spinlock);
1860 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1862 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1864 spin_lock(&dev_entry->cs_job_spinlock);
1865 list_del(&job->debugfs_list);
1866 spin_unlock(&dev_entry->cs_job_spinlock);
1869 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1871 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1873 spin_lock(&dev_entry->userptr_spinlock);
1874 list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1875 spin_unlock(&dev_entry->userptr_spinlock);
1878 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1879 struct hl_userptr *userptr)
1881 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1883 spin_lock(&dev_entry->userptr_spinlock);
1884 list_del(&userptr->debugfs_list);
1885 spin_unlock(&dev_entry->userptr_spinlock);
1888 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1890 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1892 mutex_lock(&dev_entry->ctx_mem_hash_mutex);
1893 list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1894 mutex_unlock(&dev_entry->ctx_mem_hash_mutex);
1897 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1899 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1901 mutex_lock(&dev_entry->ctx_mem_hash_mutex);
1902 list_del(&ctx->debugfs_list);
1903 mutex_unlock(&dev_entry->ctx_mem_hash_mutex);
1907 * hl_debugfs_set_state_dump - register state dump making it accessible via
1909 * @hdev: pointer to the device structure
1910 * @data: the actual dump data
1911 * @length: the length of the data
1913 void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
1914 unsigned long length)
1916 struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1918 down_write(&dev_entry->state_dump_sem);
1920 dev_entry->state_dump_head = (dev_entry->state_dump_head + 1) %
1921 ARRAY_SIZE(dev_entry->state_dump);
1922 vfree(dev_entry->state_dump[dev_entry->state_dump_head]);
1923 dev_entry->state_dump[dev_entry->state_dump_head] = data;
1925 up_write(&dev_entry->state_dump_sem);