fc084ee5106ec6c0f6c549bb33c42f5532a7e31b
[linux-2.6-microblaze.git] / drivers / misc / habanalabs / common / debugfs.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright 2016-2021 HabanaLabs, Ltd.
5  * All Rights Reserved.
6  */
7
8 #include "habanalabs.h"
9 #include "../include/hw_ip/mmu/mmu_general.h"
10
11 #include <linux/pci.h>
12 #include <linux/uaccess.h>
13 #include <linux/vmalloc.h>
14
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)
18 #define I2C_MAX_TRANSACTION_LEN 8
19
20 static struct dentry *hl_debug_root;
21
22 static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
23                                 u8 i2c_reg, u8 i2c_len, u64 *val)
24 {
25         struct cpucp_packet pkt;
26         int rc;
27
28         if (!hl_device_operational(hdev, NULL))
29                 return -EBUSY;
30
31         if (i2c_len > I2C_MAX_TRANSACTION_LEN) {
32                 dev_err(hdev->dev, "I2C transaction length %u, exceeds maximum of %u\n",
33                                 i2c_len, I2C_MAX_TRANSACTION_LEN);
34                 return -EINVAL;
35         }
36
37         memset(&pkt, 0, sizeof(pkt));
38
39         pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_RD <<
40                                 CPUCP_PKT_CTL_OPCODE_SHIFT);
41         pkt.i2c_bus = i2c_bus;
42         pkt.i2c_addr = i2c_addr;
43         pkt.i2c_reg = i2c_reg;
44         pkt.i2c_len = i2c_len;
45
46         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
47                                                 0, val);
48         if (rc)
49                 dev_err(hdev->dev, "Failed to read from I2C, error %d\n", rc);
50
51         return rc;
52 }
53
54 static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
55                                 u8 i2c_reg, u8 i2c_len, u64 val)
56 {
57         struct cpucp_packet pkt;
58         int rc;
59
60         if (!hl_device_operational(hdev, NULL))
61                 return -EBUSY;
62
63         if (i2c_len > I2C_MAX_TRANSACTION_LEN) {
64                 dev_err(hdev->dev, "I2C transaction length %u, exceeds maximum of %u\n",
65                                 i2c_len, I2C_MAX_TRANSACTION_LEN);
66                 return -EINVAL;
67         }
68
69         memset(&pkt, 0, sizeof(pkt));
70
71         pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_WR <<
72                                 CPUCP_PKT_CTL_OPCODE_SHIFT);
73         pkt.i2c_bus = i2c_bus;
74         pkt.i2c_addr = i2c_addr;
75         pkt.i2c_reg = i2c_reg;
76         pkt.i2c_len = i2c_len;
77         pkt.value = cpu_to_le64(val);
78
79         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
80                                                 0, NULL);
81
82         if (rc)
83                 dev_err(hdev->dev, "Failed to write to I2C, error %d\n", rc);
84
85         return rc;
86 }
87
88 static void hl_debugfs_led_set(struct hl_device *hdev, u8 led, u8 state)
89 {
90         struct cpucp_packet pkt;
91         int rc;
92
93         if (!hl_device_operational(hdev, NULL))
94                 return;
95
96         memset(&pkt, 0, sizeof(pkt));
97
98         pkt.ctl = cpu_to_le32(CPUCP_PACKET_LED_SET <<
99                                 CPUCP_PKT_CTL_OPCODE_SHIFT);
100         pkt.led_index = cpu_to_le32(led);
101         pkt.value = cpu_to_le64(state);
102
103         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
104                                                 0, NULL);
105
106         if (rc)
107                 dev_err(hdev->dev, "Failed to set LED %d, error %d\n", led, rc);
108 }
109
110 static int command_buffers_show(struct seq_file *s, void *data)
111 {
112         struct hl_debugfs_entry *entry = s->private;
113         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
114         struct hl_cb *cb;
115         bool first = true;
116
117         spin_lock(&dev_entry->cb_spinlock);
118
119         list_for_each_entry(cb, &dev_entry->cb_list, debugfs_list) {
120                 if (first) {
121                         first = false;
122                         seq_puts(s, "\n");
123                         seq_puts(s, " CB ID   CTX ID   CB size    CB RefCnt    mmap?   CS counter\n");
124                         seq_puts(s, "---------------------------------------------------------------\n");
125                 }
126                 seq_printf(s,
127                         "   %03llu        %d    0x%08x      %d          %d          %d\n",
128                         cb->id, cb->ctx->asid, cb->size,
129                         kref_read(&cb->refcount),
130                         cb->mmap, atomic_read(&cb->cs_cnt));
131         }
132
133         spin_unlock(&dev_entry->cb_spinlock);
134
135         if (!first)
136                 seq_puts(s, "\n");
137
138         return 0;
139 }
140
141 static int command_submission_show(struct seq_file *s, void *data)
142 {
143         struct hl_debugfs_entry *entry = s->private;
144         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
145         struct hl_cs *cs;
146         bool first = true;
147
148         spin_lock(&dev_entry->cs_spinlock);
149
150         list_for_each_entry(cs, &dev_entry->cs_list, debugfs_list) {
151                 if (first) {
152                         first = false;
153                         seq_puts(s, "\n");
154                         seq_puts(s, " CS ID   CTX ASID   CS RefCnt   Submitted    Completed\n");
155                         seq_puts(s, "------------------------------------------------------\n");
156                 }
157                 seq_printf(s,
158                         "   %llu       %d          %d           %d            %d\n",
159                         cs->sequence, cs->ctx->asid,
160                         kref_read(&cs->refcount),
161                         cs->submitted, cs->completed);
162         }
163
164         spin_unlock(&dev_entry->cs_spinlock);
165
166         if (!first)
167                 seq_puts(s, "\n");
168
169         return 0;
170 }
171
172 static int command_submission_jobs_show(struct seq_file *s, void *data)
173 {
174         struct hl_debugfs_entry *entry = s->private;
175         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
176         struct hl_cs_job *job;
177         bool first = true;
178
179         spin_lock(&dev_entry->cs_job_spinlock);
180
181         list_for_each_entry(job, &dev_entry->cs_job_list, debugfs_list) {
182                 if (first) {
183                         first = false;
184                         seq_puts(s, "\n");
185                         seq_puts(s, " JOB ID   CS ID    CTX ASID   JOB RefCnt   H/W Queue\n");
186                         seq_puts(s, "----------------------------------------------------\n");
187                 }
188                 if (job->cs)
189                         seq_printf(s,
190                                 "   %02d      %llu        %d          %d           %d\n",
191                                 job->id, job->cs->sequence, job->cs->ctx->asid,
192                                 kref_read(&job->refcount), job->hw_queue_id);
193                 else
194                         seq_printf(s,
195                                 "   %02d      0        %d          %d           %d\n",
196                                 job->id, HL_KERNEL_ASID_ID,
197                                 kref_read(&job->refcount), job->hw_queue_id);
198         }
199
200         spin_unlock(&dev_entry->cs_job_spinlock);
201
202         if (!first)
203                 seq_puts(s, "\n");
204
205         return 0;
206 }
207
208 static int userptr_show(struct seq_file *s, void *data)
209 {
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"};
215         bool first = true;
216
217         spin_lock(&dev_entry->userptr_spinlock);
218
219         list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
220                 if (first) {
221                         first = false;
222                         seq_puts(s, "\n");
223                         seq_puts(s, " pid      user virtual address     size             dma dir\n");
224                         seq_puts(s, "----------------------------------------------------------\n");
225                 }
226                 seq_printf(s, " %-7d  0x%-14llx      %-10llu    %-30s\n",
227                                 userptr->pid, userptr->addr, userptr->size,
228                                 dma_dir[userptr->dir]);
229         }
230
231         spin_unlock(&dev_entry->userptr_spinlock);
232
233         if (!first)
234                 seq_puts(s, "\n");
235
236         return 0;
237 }
238
239 static int vm_show(struct seq_file *s, void *data)
240 {
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;
244         struct hl_ctx *ctx;
245         struct hl_vm *vm;
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;
252         bool once = true;
253         u64 j;
254         int i;
255
256         if (!dev_entry->hdev->mmu_enable)
257                 return 0;
258
259         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
260
261         list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
262                 once = false;
263                 seq_puts(s, "\n\n----------------------------------------------------");
264                 seq_puts(s, "\n----------------------------------------------------\n\n");
265                 seq_printf(s, "ctx asid: %u\n", ctx->asid);
266
267                 seq_puts(s, "\nmappings:\n\n");
268                 seq_puts(s, "    virtual address        size          handle\n");
269                 seq_puts(s, "----------------------------------------------------\n");
270                 mutex_lock(&ctx->mem_hash_lock);
271                 hash_for_each(ctx->mem_hash, i, hnode, node) {
272                         vm_type = hnode->ptr;
273
274                         if (*vm_type == VM_TYPE_USERPTR) {
275                                 userptr = hnode->ptr;
276                                 seq_printf(s,
277                                         "    0x%-14llx      %-10llu\n",
278                                         hnode->vaddr, userptr->size);
279                         } else {
280                                 phys_pg_pack = hnode->ptr;
281                                 seq_printf(s,
282                                         "    0x%-14llx      %-10llu       %-4u\n",
283                                         hnode->vaddr, phys_pg_pack->total_size,
284                                         phys_pg_pack->handle);
285                         }
286                 }
287                 mutex_unlock(&ctx->mem_hash_lock);
288
289                 if (ctx->asid != HL_KERNEL_ASID_ID &&
290                     !list_empty(&ctx->hw_block_mem_list)) {
291                         seq_puts(s, "\nhw_block mappings:\n\n");
292                         seq_puts(s, "    virtual address    size    HW block id\n");
293                         seq_puts(s, "-------------------------------------------\n");
294                         mutex_lock(&ctx->hw_block_list_lock);
295                         list_for_each_entry(lnode, &ctx->hw_block_mem_list,
296                                             node) {
297                                 seq_printf(s,
298                                         "    0x%-14lx   %-6u      %-9u\n",
299                                         lnode->vaddr, lnode->size, lnode->id);
300                         }
301                         mutex_unlock(&ctx->hw_block_list_lock);
302                 }
303
304                 vm = &ctx->hdev->vm;
305                 spin_lock(&vm->idr_lock);
306
307                 if (!idr_is_empty(&vm->phys_pg_pack_handles))
308                         seq_puts(s, "\n\nallocations:\n");
309
310                 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
311                         if (phys_pg_pack->asid != ctx->asid)
312                                 continue;
313
314                         seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
315                         seq_printf(s, "page size: %u\n\n",
316                                                 phys_pg_pack->page_size);
317                         seq_puts(s, "   physical address\n");
318                         seq_puts(s, "---------------------\n");
319                         for (j = 0 ; j < phys_pg_pack->npages ; j++) {
320                                 seq_printf(s, "    0x%-14llx\n",
321                                                 phys_pg_pack->pages[j]);
322                         }
323                 }
324                 spin_unlock(&vm->idr_lock);
325
326         }
327
328         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
329
330         ctx = hl_get_compute_ctx(dev_entry->hdev);
331         if (ctx) {
332                 seq_puts(s, "\nVA ranges:\n\n");
333                 for (i = HL_VA_RANGE_TYPE_HOST ; i < HL_VA_RANGE_TYPE_MAX ; ++i) {
334                         va_range = ctx->va_range[i];
335                         seq_printf(s, "   va_range %d\n", i);
336                         seq_puts(s, "---------------------\n");
337                         mutex_lock(&va_range->lock);
338                         list_for_each_entry(va_block, &va_range->list, node) {
339                                 seq_printf(s, "%#16llx - %#16llx (%#llx)\n",
340                                            va_block->start, va_block->end,
341                                            va_block->size);
342                         }
343                         mutex_unlock(&va_range->lock);
344                         seq_puts(s, "\n");
345                 }
346                 hl_ctx_put(ctx);
347         }
348
349         if (!once)
350                 seq_puts(s, "\n");
351
352         return 0;
353 }
354
355 static int userptr_lookup_show(struct seq_file *s, void *data)
356 {
357         struct hl_debugfs_entry *entry = s->private;
358         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
359         struct scatterlist *sg;
360         struct hl_userptr *userptr;
361         bool first = true;
362         u64 total_npages, npages, sg_start, sg_end;
363         dma_addr_t dma_addr;
364         int i;
365
366         spin_lock(&dev_entry->userptr_spinlock);
367
368         list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
369                 if (dev_entry->userptr_lookup >= userptr->addr &&
370                 dev_entry->userptr_lookup < userptr->addr + userptr->size) {
371                         total_npages = 0;
372                         for_each_sg(userptr->sgt->sgl, sg, userptr->sgt->nents,
373                                         i) {
374                                 npages = hl_get_sg_info(sg, &dma_addr);
375                                 sg_start = userptr->addr +
376                                         total_npages * PAGE_SIZE;
377                                 sg_end = userptr->addr +
378                                         (total_npages + npages) * PAGE_SIZE;
379
380                                 if (dev_entry->userptr_lookup >= sg_start &&
381                                     dev_entry->userptr_lookup < sg_end) {
382                                         dma_addr += (dev_entry->userptr_lookup -
383                                                         sg_start);
384                                         if (first) {
385                                                 first = false;
386                                                 seq_puts(s, "\n");
387                                                 seq_puts(s, " user virtual address         dma address       pid        region start     region size\n");
388                                                 seq_puts(s, "---------------------------------------------------------------------------------------\n");
389                                         }
390                                         seq_printf(s, " 0x%-18llx  0x%-16llx  %-8u  0x%-16llx %-12llu\n",
391                                                 dev_entry->userptr_lookup,
392                                                 (u64)dma_addr, userptr->pid,
393                                                 userptr->addr, userptr->size);
394                                 }
395                                 total_npages += npages;
396                         }
397                 }
398         }
399
400         spin_unlock(&dev_entry->userptr_spinlock);
401
402         if (!first)
403                 seq_puts(s, "\n");
404
405         return 0;
406 }
407
408 static ssize_t userptr_lookup_write(struct file *file, const char __user *buf,
409                 size_t count, loff_t *f_pos)
410 {
411         struct seq_file *s = file->private_data;
412         struct hl_debugfs_entry *entry = s->private;
413         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
414         ssize_t rc;
415         u64 value;
416
417         rc = kstrtoull_from_user(buf, count, 16, &value);
418         if (rc)
419                 return rc;
420
421         dev_entry->userptr_lookup = value;
422
423         return count;
424 }
425
426 static int mmu_show(struct seq_file *s, void *data)
427 {
428         struct hl_debugfs_entry *entry = s->private;
429         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
430         struct hl_device *hdev = dev_entry->hdev;
431         struct hl_ctx *ctx;
432         struct hl_mmu_hop_info hops_info = {0};
433         u64 virt_addr = dev_entry->mmu_addr, phys_addr;
434         int i;
435
436         if (!hdev->mmu_enable)
437                 return 0;
438
439         if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
440                 ctx = hdev->kernel_ctx;
441         else
442                 ctx = hl_get_compute_ctx(hdev);
443
444         if (!ctx) {
445                 dev_err(hdev->dev, "no ctx available\n");
446                 return 0;
447         }
448
449         if (hl_mmu_get_tlb_info(ctx, virt_addr, &hops_info)) {
450                 dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
451                                 virt_addr);
452                 return 0;
453         }
454
455         hl_mmu_va_to_pa(ctx, virt_addr, &phys_addr);
456
457         if (hops_info.scrambled_vaddr &&
458                 (dev_entry->mmu_addr != hops_info.scrambled_vaddr))
459                 seq_printf(s,
460                         "asid: %u, virt_addr: 0x%llx, scrambled virt_addr: 0x%llx,\nphys_addr: 0x%llx, scrambled_phys_addr: 0x%llx\n",
461                         dev_entry->mmu_asid, dev_entry->mmu_addr,
462                         hops_info.scrambled_vaddr,
463                         hops_info.unscrambled_paddr, phys_addr);
464         else
465                 seq_printf(s,
466                         "asid: %u, virt_addr: 0x%llx, phys_addr: 0x%llx\n",
467                         dev_entry->mmu_asid, dev_entry->mmu_addr, phys_addr);
468
469         for (i = 0 ; i < hops_info.used_hops ; i++) {
470                 seq_printf(s, "hop%d_addr: 0x%llx\n",
471                                 i, hops_info.hop_info[i].hop_addr);
472                 seq_printf(s, "hop%d_pte_addr: 0x%llx\n",
473                                 i, hops_info.hop_info[i].hop_pte_addr);
474                 seq_printf(s, "hop%d_pte: 0x%llx\n",
475                                 i, hops_info.hop_info[i].hop_pte_val);
476         }
477
478         return 0;
479 }
480
481 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
482                 size_t count, loff_t *f_pos)
483 {
484         struct seq_file *s = file->private_data;
485         struct hl_debugfs_entry *entry = s->private;
486         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
487         struct hl_device *hdev = dev_entry->hdev;
488         char kbuf[MMU_KBUF_SIZE];
489         char *c;
490         ssize_t rc;
491
492         if (!hdev->mmu_enable)
493                 return count;
494
495         if (count > sizeof(kbuf) - 1)
496                 goto err;
497         if (copy_from_user(kbuf, buf, count))
498                 goto err;
499         kbuf[count] = 0;
500
501         c = strchr(kbuf, ' ');
502         if (!c)
503                 goto err;
504         *c = '\0';
505
506         rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
507         if (rc)
508                 goto err;
509
510         if (strncmp(c+1, "0x", 2))
511                 goto err;
512         rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
513         if (rc)
514                 goto err;
515
516         return count;
517
518 err:
519         dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
520
521         return -EINVAL;
522 }
523
524 static int engines_show(struct seq_file *s, void *data)
525 {
526         struct hl_debugfs_entry *entry = s->private;
527         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
528         struct hl_device *hdev = dev_entry->hdev;
529
530         if (hdev->reset_info.in_reset) {
531                 dev_warn_ratelimited(hdev->dev,
532                                 "Can't check device idle during reset\n");
533                 return 0;
534         }
535
536         hdev->asic_funcs->is_device_idle(hdev, NULL, 0, s);
537
538         return 0;
539 }
540
541 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
542 {
543         struct asic_fixed_properties *prop = &hdev->asic_prop;
544
545         if (!hdev->mmu_enable)
546                 goto out;
547
548         if (prop->dram_supports_virtual_memory &&
549                 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
550                 return true;
551
552         if (addr >= prop->pmmu.start_addr &&
553                 addr < prop->pmmu.end_addr)
554                 return true;
555
556         if (addr >= prop->pmmu_huge.start_addr &&
557                 addr < prop->pmmu_huge.end_addr)
558                 return true;
559 out:
560         return false;
561 }
562
563 static bool hl_is_device_internal_memory_va(struct hl_device *hdev, u64 addr,
564                                                 u32 size)
565 {
566         struct asic_fixed_properties *prop = &hdev->asic_prop;
567         u64 dram_start_addr, dram_end_addr;
568
569         if (!hdev->mmu_enable)
570                 return false;
571
572         if (prop->dram_supports_virtual_memory) {
573                 dram_start_addr = prop->dmmu.start_addr;
574                 dram_end_addr = prop->dmmu.end_addr;
575         } else {
576                 dram_start_addr = prop->dram_base_address;
577                 dram_end_addr = prop->dram_end_address;
578         }
579
580         if (hl_mem_area_inside_range(addr, size, dram_start_addr,
581                                         dram_end_addr))
582                 return true;
583
584         if (hl_mem_area_inside_range(addr, size, prop->sram_base_address,
585                                         prop->sram_end_address))
586                 return true;
587
588         return false;
589 }
590
591 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr, u32 size,
592                         u64 *phys_addr)
593 {
594         struct hl_vm_phys_pg_pack *phys_pg_pack;
595         struct hl_ctx *ctx;
596         struct hl_vm_hash_node *hnode;
597         u64 end_address, range_size;
598         struct hl_userptr *userptr;
599         enum vm_type *vm_type;
600         bool valid = false;
601         int i, rc = 0;
602
603         ctx = hl_get_compute_ctx(hdev);
604
605         if (!ctx) {
606                 dev_err(hdev->dev, "no ctx available\n");
607                 return -EINVAL;
608         }
609
610         /* Verify address is mapped */
611         mutex_lock(&ctx->mem_hash_lock);
612         hash_for_each(ctx->mem_hash, i, hnode, node) {
613                 vm_type = hnode->ptr;
614
615                 if (*vm_type == VM_TYPE_USERPTR) {
616                         userptr = hnode->ptr;
617                         range_size = userptr->size;
618                 } else {
619                         phys_pg_pack = hnode->ptr;
620                         range_size = phys_pg_pack->total_size;
621                 }
622
623                 end_address = virt_addr + size;
624                 if ((virt_addr >= hnode->vaddr) &&
625                                 (end_address <= hnode->vaddr + range_size)) {
626                         valid = true;
627                         break;
628                 }
629         }
630         mutex_unlock(&ctx->mem_hash_lock);
631
632         if (!valid) {
633                 dev_err(hdev->dev,
634                         "virt addr 0x%llx is not mapped\n",
635                         virt_addr);
636                 return -EINVAL;
637         }
638
639         rc = hl_mmu_va_to_pa(ctx, virt_addr, phys_addr);
640         if (rc) {
641                 dev_err(hdev->dev,
642                         "virt addr 0x%llx is not mapped to phys addr\n",
643                         virt_addr);
644                 rc = -EINVAL;
645         }
646
647         return rc;
648 }
649
650 static ssize_t hl_data_read32(struct file *f, char __user *buf,
651                                         size_t count, loff_t *ppos)
652 {
653         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
654         struct hl_device *hdev = entry->hdev;
655         u64 addr = entry->addr;
656         bool user_address;
657         char tmp_buf[32];
658         ssize_t rc;
659         u32 val;
660
661         if (hdev->reset_info.in_reset) {
662                 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
663                 return 0;
664         }
665
666         if (*ppos)
667                 return 0;
668
669         user_address = hl_is_device_va(hdev, addr);
670         if (user_address) {
671                 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
672                 if (rc)
673                         return rc;
674         }
675
676         rc = hdev->asic_funcs->debugfs_read32(hdev, addr, user_address, &val);
677         if (rc) {
678                 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
679                 return rc;
680         }
681
682         sprintf(tmp_buf, "0x%08x\n", val);
683         return simple_read_from_buffer(buf, count, ppos, tmp_buf,
684                         strlen(tmp_buf));
685 }
686
687 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
688                                         size_t count, loff_t *ppos)
689 {
690         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
691         struct hl_device *hdev = entry->hdev;
692         u64 addr = entry->addr;
693         bool user_address;
694         u32 value;
695         ssize_t rc;
696
697         if (hdev->reset_info.in_reset) {
698                 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
699                 return 0;
700         }
701
702         rc = kstrtouint_from_user(buf, count, 16, &value);
703         if (rc)
704                 return rc;
705
706         user_address = hl_is_device_va(hdev, addr);
707         if (user_address) {
708                 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
709                 if (rc)
710                         return rc;
711         }
712
713         rc = hdev->asic_funcs->debugfs_write32(hdev, addr, user_address, value);
714         if (rc) {
715                 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
716                         value, addr);
717                 return rc;
718         }
719
720         return count;
721 }
722
723 static ssize_t hl_data_read64(struct file *f, char __user *buf,
724                                         size_t count, loff_t *ppos)
725 {
726         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
727         struct hl_device *hdev = entry->hdev;
728         u64 addr = entry->addr;
729         bool user_address;
730         char tmp_buf[32];
731         ssize_t rc;
732         u64 val;
733
734         if (hdev->reset_info.in_reset) {
735                 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
736                 return 0;
737         }
738
739         if (*ppos)
740                 return 0;
741
742         user_address = hl_is_device_va(hdev, addr);
743         if (user_address) {
744                 rc = device_va_to_pa(hdev, addr, sizeof(val), &addr);
745                 if (rc)
746                         return rc;
747         }
748
749         rc = hdev->asic_funcs->debugfs_read64(hdev, addr, user_address, &val);
750         if (rc) {
751                 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
752                 return rc;
753         }
754
755         sprintf(tmp_buf, "0x%016llx\n", val);
756         return simple_read_from_buffer(buf, count, ppos, tmp_buf,
757                         strlen(tmp_buf));
758 }
759
760 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
761                                         size_t count, loff_t *ppos)
762 {
763         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
764         struct hl_device *hdev = entry->hdev;
765         u64 addr = entry->addr;
766         bool user_address;
767         u64 value;
768         ssize_t rc;
769
770         if (hdev->reset_info.in_reset) {
771                 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
772                 return 0;
773         }
774
775         rc = kstrtoull_from_user(buf, count, 16, &value);
776         if (rc)
777                 return rc;
778
779         user_address = hl_is_device_va(hdev, addr);
780         if (user_address) {
781                 rc = device_va_to_pa(hdev, addr, sizeof(value), &addr);
782                 if (rc)
783                         return rc;
784         }
785
786         rc = hdev->asic_funcs->debugfs_write64(hdev, addr, user_address, value);
787         if (rc) {
788                 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
789                         value, addr);
790                 return rc;
791         }
792
793         return count;
794 }
795
796 static ssize_t hl_dma_size_write(struct file *f, const char __user *buf,
797                                         size_t count, loff_t *ppos)
798 {
799         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
800         struct hl_device *hdev = entry->hdev;
801         u64 addr = entry->addr;
802         ssize_t rc;
803         u32 size;
804
805         if (hdev->reset_info.in_reset) {
806                 dev_warn_ratelimited(hdev->dev, "Can't DMA during reset\n");
807                 return 0;
808         }
809         rc = kstrtouint_from_user(buf, count, 16, &size);
810         if (rc)
811                 return rc;
812
813         if (!size) {
814                 dev_err(hdev->dev, "DMA read failed. size can't be 0\n");
815                 return -EINVAL;
816         }
817
818         if (size > SZ_128M) {
819                 dev_err(hdev->dev,
820                         "DMA read failed. size can't be larger than 128MB\n");
821                 return -EINVAL;
822         }
823
824         if (!hl_is_device_internal_memory_va(hdev, addr, size)) {
825                 dev_err(hdev->dev,
826                         "DMA read failed. Invalid 0x%010llx + 0x%08x\n",
827                         addr, size);
828                 return -EINVAL;
829         }
830
831         /* Free the previous allocation, if there was any */
832         entry->blob_desc.size = 0;
833         vfree(entry->blob_desc.data);
834
835         entry->blob_desc.data = vmalloc(size);
836         if (!entry->blob_desc.data)
837                 return -ENOMEM;
838
839         rc = hdev->asic_funcs->debugfs_read_dma(hdev, addr, size,
840                                                 entry->blob_desc.data);
841         if (rc) {
842                 dev_err(hdev->dev, "Failed to DMA from 0x%010llx\n", addr);
843                 vfree(entry->blob_desc.data);
844                 entry->blob_desc.data = NULL;
845                 return -EIO;
846         }
847
848         entry->blob_desc.size = size;
849
850         return count;
851 }
852
853 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
854                 size_t count, loff_t *ppos)
855 {
856         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
857         struct hl_device *hdev = entry->hdev;
858         char tmp_buf[200];
859         int i;
860
861         if (*ppos)
862                 return 0;
863
864         if (hdev->pdev->current_state == PCI_D0)
865                 i = 1;
866         else if (hdev->pdev->current_state == PCI_D3hot)
867                 i = 2;
868         else
869                 i = 3;
870
871         sprintf(tmp_buf,
872                 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
873         return simple_read_from_buffer(buf, count, ppos, tmp_buf,
874                         strlen(tmp_buf));
875 }
876
877 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
878                                         size_t count, loff_t *ppos)
879 {
880         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
881         struct hl_device *hdev = entry->hdev;
882         u32 value;
883         ssize_t rc;
884
885         rc = kstrtouint_from_user(buf, count, 10, &value);
886         if (rc)
887                 return rc;
888
889         if (value == 1) {
890                 pci_set_power_state(hdev->pdev, PCI_D0);
891                 pci_restore_state(hdev->pdev);
892                 rc = pci_enable_device(hdev->pdev);
893         } else if (value == 2) {
894                 pci_save_state(hdev->pdev);
895                 pci_disable_device(hdev->pdev);
896                 pci_set_power_state(hdev->pdev, PCI_D3hot);
897         } else {
898                 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
899                 return -EINVAL;
900         }
901
902         return count;
903 }
904
905 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
906                                         size_t count, loff_t *ppos)
907 {
908         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
909         struct hl_device *hdev = entry->hdev;
910         char tmp_buf[32];
911         u64 val;
912         ssize_t rc;
913
914         if (*ppos)
915                 return 0;
916
917         rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
918                         entry->i2c_reg, entry->i2c_len, &val);
919         if (rc) {
920                 dev_err(hdev->dev,
921                         "Failed to read from I2C bus %d, addr %d, reg %d, len %d\n",
922                         entry->i2c_bus, entry->i2c_addr, entry->i2c_reg, entry->i2c_len);
923                 return rc;
924         }
925
926         sprintf(tmp_buf, "%#02llx\n", val);
927         rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
928                         strlen(tmp_buf));
929
930         return rc;
931 }
932
933 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
934                                         size_t count, loff_t *ppos)
935 {
936         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
937         struct hl_device *hdev = entry->hdev;
938         u64 value;
939         ssize_t rc;
940
941         rc = kstrtou64_from_user(buf, count, 16, &value);
942         if (rc)
943                 return rc;
944
945         rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
946                         entry->i2c_reg, entry->i2c_len, value);
947         if (rc) {
948                 dev_err(hdev->dev,
949                         "Failed to write %#02llx to I2C bus %d, addr %d, reg %d, len %d\n",
950                         value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg, entry->i2c_len);
951                 return rc;
952         }
953
954         return count;
955 }
956
957 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
958                                         size_t count, loff_t *ppos)
959 {
960         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
961         struct hl_device *hdev = entry->hdev;
962         u32 value;
963         ssize_t rc;
964
965         rc = kstrtouint_from_user(buf, count, 10, &value);
966         if (rc)
967                 return rc;
968
969         value = value ? 1 : 0;
970
971         hl_debugfs_led_set(hdev, 0, value);
972
973         return count;
974 }
975
976 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
977                                         size_t count, loff_t *ppos)
978 {
979         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
980         struct hl_device *hdev = entry->hdev;
981         u32 value;
982         ssize_t rc;
983
984         rc = kstrtouint_from_user(buf, count, 10, &value);
985         if (rc)
986                 return rc;
987
988         value = value ? 1 : 0;
989
990         hl_debugfs_led_set(hdev, 1, value);
991
992         return count;
993 }
994
995 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
996                                         size_t count, loff_t *ppos)
997 {
998         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
999         struct hl_device *hdev = entry->hdev;
1000         u32 value;
1001         ssize_t rc;
1002
1003         rc = kstrtouint_from_user(buf, count, 10, &value);
1004         if (rc)
1005                 return rc;
1006
1007         value = value ? 1 : 0;
1008
1009         hl_debugfs_led_set(hdev, 2, value);
1010
1011         return count;
1012 }
1013
1014 static ssize_t hl_device_read(struct file *f, char __user *buf,
1015                                         size_t count, loff_t *ppos)
1016 {
1017         static const char *help =
1018                 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
1019         return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
1020 }
1021
1022 static ssize_t hl_device_write(struct file *f, const char __user *buf,
1023                                      size_t count, loff_t *ppos)
1024 {
1025         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1026         struct hl_device *hdev = entry->hdev;
1027         char data[30] = {0};
1028
1029         /* don't allow partial writes */
1030         if (*ppos != 0)
1031                 return 0;
1032
1033         simple_write_to_buffer(data, 29, ppos, buf, count);
1034
1035         if (strncmp("disable", data, strlen("disable")) == 0) {
1036                 hdev->disabled = true;
1037         } else if (strncmp("enable", data, strlen("enable")) == 0) {
1038                 hdev->disabled = false;
1039         } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
1040                 hdev->asic_funcs->suspend(hdev);
1041         } else if (strncmp("resume", data, strlen("resume")) == 0) {
1042                 hdev->asic_funcs->resume(hdev);
1043         } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
1044                 hdev->device_cpu_disabled = true;
1045         } else {
1046                 dev_err(hdev->dev,
1047                         "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
1048                 count = -EINVAL;
1049         }
1050
1051         return count;
1052 }
1053
1054 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
1055                                         size_t count, loff_t *ppos)
1056 {
1057         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1058         struct hl_device *hdev = entry->hdev;
1059         char tmp_buf[200];
1060         ssize_t rc;
1061
1062         if (*ppos)
1063                 return 0;
1064
1065         sprintf(tmp_buf, "0x%llx\n", hdev->clock_gating_mask);
1066         rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
1067                         strlen(tmp_buf) + 1);
1068
1069         return rc;
1070 }
1071
1072 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1073                                      size_t count, loff_t *ppos)
1074 {
1075         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1076         struct hl_device *hdev = entry->hdev;
1077         u64 value;
1078         ssize_t rc;
1079
1080         if (hdev->reset_info.in_reset) {
1081                 dev_warn_ratelimited(hdev->dev,
1082                                 "Can't change clock gating during reset\n");
1083                 return 0;
1084         }
1085
1086         rc = kstrtoull_from_user(buf, count, 16, &value);
1087         if (rc)
1088                 return rc;
1089
1090         hdev->clock_gating_mask = value;
1091         hdev->asic_funcs->set_clock_gating(hdev);
1092
1093         return count;
1094 }
1095
1096 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1097                                         size_t count, loff_t *ppos)
1098 {
1099         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1100         struct hl_device *hdev = entry->hdev;
1101         char tmp_buf[200];
1102         ssize_t rc;
1103
1104         if (*ppos)
1105                 return 0;
1106
1107         sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1108         rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1109                         strlen(tmp_buf) + 1);
1110
1111         return rc;
1112 }
1113
1114 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1115                                      size_t count, loff_t *ppos)
1116 {
1117         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1118         struct hl_device *hdev = entry->hdev;
1119         u32 value;
1120         ssize_t rc;
1121
1122         if (hdev->reset_info.in_reset) {
1123                 dev_warn_ratelimited(hdev->dev,
1124                                 "Can't change stop on error during reset\n");
1125                 return 0;
1126         }
1127
1128         rc = kstrtouint_from_user(buf, count, 10, &value);
1129         if (rc)
1130                 return rc;
1131
1132         hdev->stop_on_err = value ? 1 : 0;
1133
1134         hl_device_reset(hdev, 0);
1135
1136         return count;
1137 }
1138
1139 static ssize_t hl_security_violations_read(struct file *f, char __user *buf,
1140                                         size_t count, loff_t *ppos)
1141 {
1142         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1143         struct hl_device *hdev = entry->hdev;
1144
1145         hdev->asic_funcs->ack_protection_bits_errors(hdev);
1146
1147         return 0;
1148 }
1149
1150 static ssize_t hl_state_dump_read(struct file *f, char __user *buf,
1151                                         size_t count, loff_t *ppos)
1152 {
1153         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1154         ssize_t rc;
1155
1156         down_read(&entry->state_dump_sem);
1157         if (!entry->state_dump[entry->state_dump_head])
1158                 rc = 0;
1159         else
1160                 rc = simple_read_from_buffer(
1161                         buf, count, ppos,
1162                         entry->state_dump[entry->state_dump_head],
1163                         strlen(entry->state_dump[entry->state_dump_head]));
1164         up_read(&entry->state_dump_sem);
1165
1166         return rc;
1167 }
1168
1169 static ssize_t hl_state_dump_write(struct file *f, const char __user *buf,
1170                                         size_t count, loff_t *ppos)
1171 {
1172         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1173         struct hl_device *hdev = entry->hdev;
1174         ssize_t rc;
1175         u32 size;
1176         int i;
1177
1178         rc = kstrtouint_from_user(buf, count, 10, &size);
1179         if (rc)
1180                 return rc;
1181
1182         if (size <= 0 || size >= ARRAY_SIZE(entry->state_dump)) {
1183                 dev_err(hdev->dev, "Invalid number of dumps to skip\n");
1184                 return -EINVAL;
1185         }
1186
1187         if (entry->state_dump[entry->state_dump_head]) {
1188                 down_write(&entry->state_dump_sem);
1189                 for (i = 0; i < size; ++i) {
1190                         vfree(entry->state_dump[entry->state_dump_head]);
1191                         entry->state_dump[entry->state_dump_head] = NULL;
1192                         if (entry->state_dump_head > 0)
1193                                 entry->state_dump_head--;
1194                         else
1195                                 entry->state_dump_head =
1196                                         ARRAY_SIZE(entry->state_dump) - 1;
1197                 }
1198                 up_write(&entry->state_dump_sem);
1199         }
1200
1201         return count;
1202 }
1203
1204 static ssize_t hl_timeout_locked_read(struct file *f, char __user *buf,
1205                                         size_t count, loff_t *ppos)
1206 {
1207         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1208         struct hl_device *hdev = entry->hdev;
1209         char tmp_buf[200];
1210         ssize_t rc;
1211
1212         if (*ppos)
1213                 return 0;
1214
1215         sprintf(tmp_buf, "%d\n",
1216                 jiffies_to_msecs(hdev->timeout_jiffies) / 1000);
1217         rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1218                         strlen(tmp_buf) + 1);
1219
1220         return rc;
1221 }
1222
1223 static ssize_t hl_timeout_locked_write(struct file *f, const char __user *buf,
1224                                      size_t count, loff_t *ppos)
1225 {
1226         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1227         struct hl_device *hdev = entry->hdev;
1228         u32 value;
1229         ssize_t rc;
1230
1231         rc = kstrtouint_from_user(buf, count, 10, &value);
1232         if (rc)
1233                 return rc;
1234
1235         if (value)
1236                 hdev->timeout_jiffies = msecs_to_jiffies(value * 1000);
1237         else
1238                 hdev->timeout_jiffies = MAX_SCHEDULE_TIMEOUT;
1239
1240         return count;
1241 }
1242
1243 static const struct file_operations hl_data32b_fops = {
1244         .owner = THIS_MODULE,
1245         .read = hl_data_read32,
1246         .write = hl_data_write32
1247 };
1248
1249 static const struct file_operations hl_data64b_fops = {
1250         .owner = THIS_MODULE,
1251         .read = hl_data_read64,
1252         .write = hl_data_write64
1253 };
1254
1255 static const struct file_operations hl_dma_size_fops = {
1256         .owner = THIS_MODULE,
1257         .write = hl_dma_size_write
1258 };
1259
1260 static const struct file_operations hl_i2c_data_fops = {
1261         .owner = THIS_MODULE,
1262         .read = hl_i2c_data_read,
1263         .write = hl_i2c_data_write
1264 };
1265
1266 static const struct file_operations hl_power_fops = {
1267         .owner = THIS_MODULE,
1268         .read = hl_get_power_state,
1269         .write = hl_set_power_state
1270 };
1271
1272 static const struct file_operations hl_led0_fops = {
1273         .owner = THIS_MODULE,
1274         .write = hl_led0_write
1275 };
1276
1277 static const struct file_operations hl_led1_fops = {
1278         .owner = THIS_MODULE,
1279         .write = hl_led1_write
1280 };
1281
1282 static const struct file_operations hl_led2_fops = {
1283         .owner = THIS_MODULE,
1284         .write = hl_led2_write
1285 };
1286
1287 static const struct file_operations hl_device_fops = {
1288         .owner = THIS_MODULE,
1289         .read = hl_device_read,
1290         .write = hl_device_write
1291 };
1292
1293 static const struct file_operations hl_clk_gate_fops = {
1294         .owner = THIS_MODULE,
1295         .read = hl_clk_gate_read,
1296         .write = hl_clk_gate_write
1297 };
1298
1299 static const struct file_operations hl_stop_on_err_fops = {
1300         .owner = THIS_MODULE,
1301         .read = hl_stop_on_err_read,
1302         .write = hl_stop_on_err_write
1303 };
1304
1305 static const struct file_operations hl_security_violations_fops = {
1306         .owner = THIS_MODULE,
1307         .read = hl_security_violations_read
1308 };
1309
1310 static const struct file_operations hl_state_dump_fops = {
1311         .owner = THIS_MODULE,
1312         .read = hl_state_dump_read,
1313         .write = hl_state_dump_write
1314 };
1315
1316 static const struct file_operations hl_timeout_locked_fops = {
1317         .owner = THIS_MODULE,
1318         .read = hl_timeout_locked_read,
1319         .write = hl_timeout_locked_write
1320 };
1321
1322 static const struct hl_info_list hl_debugfs_list[] = {
1323         {"command_buffers", command_buffers_show, NULL},
1324         {"command_submission", command_submission_show, NULL},
1325         {"command_submission_jobs", command_submission_jobs_show, NULL},
1326         {"userptr", userptr_show, NULL},
1327         {"vm", vm_show, NULL},
1328         {"userptr_lookup", userptr_lookup_show, userptr_lookup_write},
1329         {"mmu", mmu_show, mmu_asid_va_write},
1330         {"engines", engines_show, NULL}
1331 };
1332
1333 static int hl_debugfs_open(struct inode *inode, struct file *file)
1334 {
1335         struct hl_debugfs_entry *node = inode->i_private;
1336
1337         return single_open(file, node->info_ent->show, node);
1338 }
1339
1340 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1341                 size_t count, loff_t *f_pos)
1342 {
1343         struct hl_debugfs_entry *node = file->f_inode->i_private;
1344
1345         if (node->info_ent->write)
1346                 return node->info_ent->write(file, buf, count, f_pos);
1347         else
1348                 return -EINVAL;
1349
1350 }
1351
1352 static const struct file_operations hl_debugfs_fops = {
1353         .owner = THIS_MODULE,
1354         .open = hl_debugfs_open,
1355         .read = seq_read,
1356         .write = hl_debugfs_write,
1357         .llseek = seq_lseek,
1358         .release = single_release,
1359 };
1360
1361 void hl_debugfs_add_device(struct hl_device *hdev)
1362 {
1363         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1364         int count = ARRAY_SIZE(hl_debugfs_list);
1365         struct hl_debugfs_entry *entry;
1366         int i;
1367
1368         dev_entry->hdev = hdev;
1369         dev_entry->entry_arr = kmalloc_array(count,
1370                                         sizeof(struct hl_debugfs_entry),
1371                                         GFP_KERNEL);
1372         if (!dev_entry->entry_arr)
1373                 return;
1374
1375         dev_entry->blob_desc.size = 0;
1376         dev_entry->blob_desc.data = NULL;
1377
1378         INIT_LIST_HEAD(&dev_entry->file_list);
1379         INIT_LIST_HEAD(&dev_entry->cb_list);
1380         INIT_LIST_HEAD(&dev_entry->cs_list);
1381         INIT_LIST_HEAD(&dev_entry->cs_job_list);
1382         INIT_LIST_HEAD(&dev_entry->userptr_list);
1383         INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1384         mutex_init(&dev_entry->file_mutex);
1385         init_rwsem(&dev_entry->state_dump_sem);
1386         spin_lock_init(&dev_entry->cb_spinlock);
1387         spin_lock_init(&dev_entry->cs_spinlock);
1388         spin_lock_init(&dev_entry->cs_job_spinlock);
1389         spin_lock_init(&dev_entry->userptr_spinlock);
1390         spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1391
1392         dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1393                                                 hl_debug_root);
1394
1395         debugfs_create_x64("addr",
1396                                 0644,
1397                                 dev_entry->root,
1398                                 &dev_entry->addr);
1399
1400         debugfs_create_file("data32",
1401                                 0644,
1402                                 dev_entry->root,
1403                                 dev_entry,
1404                                 &hl_data32b_fops);
1405
1406         debugfs_create_file("data64",
1407                                 0644,
1408                                 dev_entry->root,
1409                                 dev_entry,
1410                                 &hl_data64b_fops);
1411
1412         debugfs_create_file("set_power_state",
1413                                 0200,
1414                                 dev_entry->root,
1415                                 dev_entry,
1416                                 &hl_power_fops);
1417
1418         debugfs_create_u8("i2c_bus",
1419                                 0644,
1420                                 dev_entry->root,
1421                                 &dev_entry->i2c_bus);
1422
1423         debugfs_create_u8("i2c_addr",
1424                                 0644,
1425                                 dev_entry->root,
1426                                 &dev_entry->i2c_addr);
1427
1428         debugfs_create_u8("i2c_reg",
1429                                 0644,
1430                                 dev_entry->root,
1431                                 &dev_entry->i2c_reg);
1432
1433         debugfs_create_u8("i2c_len",
1434                                 0644,
1435                                 dev_entry->root,
1436                                 &dev_entry->i2c_len);
1437
1438         debugfs_create_file("i2c_data",
1439                                 0644,
1440                                 dev_entry->root,
1441                                 dev_entry,
1442                                 &hl_i2c_data_fops);
1443
1444         debugfs_create_file("led0",
1445                                 0200,
1446                                 dev_entry->root,
1447                                 dev_entry,
1448                                 &hl_led0_fops);
1449
1450         debugfs_create_file("led1",
1451                                 0200,
1452                                 dev_entry->root,
1453                                 dev_entry,
1454                                 &hl_led1_fops);
1455
1456         debugfs_create_file("led2",
1457                                 0200,
1458                                 dev_entry->root,
1459                                 dev_entry,
1460                                 &hl_led2_fops);
1461
1462         debugfs_create_file("device",
1463                                 0200,
1464                                 dev_entry->root,
1465                                 dev_entry,
1466                                 &hl_device_fops);
1467
1468         debugfs_create_file("clk_gate",
1469                                 0200,
1470                                 dev_entry->root,
1471                                 dev_entry,
1472                                 &hl_clk_gate_fops);
1473
1474         debugfs_create_file("stop_on_err",
1475                                 0644,
1476                                 dev_entry->root,
1477                                 dev_entry,
1478                                 &hl_stop_on_err_fops);
1479
1480         debugfs_create_file("dump_security_violations",
1481                                 0644,
1482                                 dev_entry->root,
1483                                 dev_entry,
1484                                 &hl_security_violations_fops);
1485
1486         debugfs_create_file("dma_size",
1487                                 0200,
1488                                 dev_entry->root,
1489                                 dev_entry,
1490                                 &hl_dma_size_fops);
1491
1492         debugfs_create_blob("data_dma",
1493                                 0400,
1494                                 dev_entry->root,
1495                                 &dev_entry->blob_desc);
1496
1497         debugfs_create_x8("skip_reset_on_timeout",
1498                                 0644,
1499                                 dev_entry->root,
1500                                 &hdev->reset_info.skip_reset_on_timeout);
1501
1502         debugfs_create_file("state_dump",
1503                                 0600,
1504                                 dev_entry->root,
1505                                 dev_entry,
1506                                 &hl_state_dump_fops);
1507
1508         debugfs_create_file("timeout_locked",
1509                                 0644,
1510                                 dev_entry->root,
1511                                 dev_entry,
1512                                 &hl_timeout_locked_fops);
1513
1514         for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1515                 debugfs_create_file(hl_debugfs_list[i].name,
1516                                         0444,
1517                                         dev_entry->root,
1518                                         entry,
1519                                         &hl_debugfs_fops);
1520                 entry->info_ent = &hl_debugfs_list[i];
1521                 entry->dev_entry = dev_entry;
1522         }
1523 }
1524
1525 void hl_debugfs_remove_device(struct hl_device *hdev)
1526 {
1527         struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1528         int i;
1529
1530         debugfs_remove_recursive(entry->root);
1531
1532         mutex_destroy(&entry->file_mutex);
1533
1534         vfree(entry->blob_desc.data);
1535
1536         for (i = 0; i < ARRAY_SIZE(entry->state_dump); ++i)
1537                 vfree(entry->state_dump[i]);
1538
1539         kfree(entry->entry_arr);
1540 }
1541
1542 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1543 {
1544         struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1545
1546         mutex_lock(&dev_entry->file_mutex);
1547         list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1548         mutex_unlock(&dev_entry->file_mutex);
1549 }
1550
1551 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1552 {
1553         struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1554
1555         mutex_lock(&dev_entry->file_mutex);
1556         list_del(&hpriv->debugfs_list);
1557         mutex_unlock(&dev_entry->file_mutex);
1558 }
1559
1560 void hl_debugfs_add_cb(struct hl_cb *cb)
1561 {
1562         struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1563
1564         spin_lock(&dev_entry->cb_spinlock);
1565         list_add(&cb->debugfs_list, &dev_entry->cb_list);
1566         spin_unlock(&dev_entry->cb_spinlock);
1567 }
1568
1569 void hl_debugfs_remove_cb(struct hl_cb *cb)
1570 {
1571         struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1572
1573         spin_lock(&dev_entry->cb_spinlock);
1574         list_del(&cb->debugfs_list);
1575         spin_unlock(&dev_entry->cb_spinlock);
1576 }
1577
1578 void hl_debugfs_add_cs(struct hl_cs *cs)
1579 {
1580         struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1581
1582         spin_lock(&dev_entry->cs_spinlock);
1583         list_add(&cs->debugfs_list, &dev_entry->cs_list);
1584         spin_unlock(&dev_entry->cs_spinlock);
1585 }
1586
1587 void hl_debugfs_remove_cs(struct hl_cs *cs)
1588 {
1589         struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1590
1591         spin_lock(&dev_entry->cs_spinlock);
1592         list_del(&cs->debugfs_list);
1593         spin_unlock(&dev_entry->cs_spinlock);
1594 }
1595
1596 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1597 {
1598         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1599
1600         spin_lock(&dev_entry->cs_job_spinlock);
1601         list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1602         spin_unlock(&dev_entry->cs_job_spinlock);
1603 }
1604
1605 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1606 {
1607         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1608
1609         spin_lock(&dev_entry->cs_job_spinlock);
1610         list_del(&job->debugfs_list);
1611         spin_unlock(&dev_entry->cs_job_spinlock);
1612 }
1613
1614 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1615 {
1616         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1617
1618         spin_lock(&dev_entry->userptr_spinlock);
1619         list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1620         spin_unlock(&dev_entry->userptr_spinlock);
1621 }
1622
1623 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1624                                 struct hl_userptr *userptr)
1625 {
1626         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1627
1628         spin_lock(&dev_entry->userptr_spinlock);
1629         list_del(&userptr->debugfs_list);
1630         spin_unlock(&dev_entry->userptr_spinlock);
1631 }
1632
1633 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1634 {
1635         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1636
1637         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1638         list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1639         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1640 }
1641
1642 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1643 {
1644         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1645
1646         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1647         list_del(&ctx->debugfs_list);
1648         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1649 }
1650
1651 /**
1652  * hl_debugfs_set_state_dump - register state dump making it accessible via
1653  *                             debugfs
1654  * @hdev: pointer to the device structure
1655  * @data: the actual dump data
1656  * @length: the length of the data
1657  */
1658 void hl_debugfs_set_state_dump(struct hl_device *hdev, char *data,
1659                                         unsigned long length)
1660 {
1661         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1662
1663         down_write(&dev_entry->state_dump_sem);
1664
1665         dev_entry->state_dump_head = (dev_entry->state_dump_head + 1) %
1666                                         ARRAY_SIZE(dev_entry->state_dump);
1667         vfree(dev_entry->state_dump[dev_entry->state_dump_head]);
1668         dev_entry->state_dump[dev_entry->state_dump_head] = data;
1669
1670         up_write(&dev_entry->state_dump_sem);
1671 }
1672
1673 void __init hl_debugfs_init(void)
1674 {
1675         hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1676 }
1677
1678 void hl_debugfs_fini(void)
1679 {
1680         debugfs_remove_recursive(hl_debug_root);
1681 }