habanalabs: Separate CS job completion from its deallocation
[linux-2.6-microblaze.git] / drivers / misc / habanalabs / common / debugfs.c
1 // SPDX-License-Identifier: GPL-2.0
2
3 /*
4  * Copyright 2016-2019 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/debugfs.h>
13 #include <linux/uaccess.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
19 static struct dentry *hl_debug_root;
20
21 static int hl_debugfs_i2c_read(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
22                                 u8 i2c_reg, long *val)
23 {
24         struct cpucp_packet pkt;
25         int rc;
26
27         if (hl_device_disabled_or_in_reset(hdev))
28                 return -EBUSY;
29
30         memset(&pkt, 0, sizeof(pkt));
31
32         pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_RD <<
33                                 CPUCP_PKT_CTL_OPCODE_SHIFT);
34         pkt.i2c_bus = i2c_bus;
35         pkt.i2c_addr = i2c_addr;
36         pkt.i2c_reg = i2c_reg;
37
38         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
39                                                 0, val);
40
41         if (rc)
42                 dev_err(hdev->dev, "Failed to read from I2C, error %d\n", rc);
43
44         return rc;
45 }
46
47 static int hl_debugfs_i2c_write(struct hl_device *hdev, u8 i2c_bus, u8 i2c_addr,
48                                 u8 i2c_reg, u32 val)
49 {
50         struct cpucp_packet pkt;
51         int rc;
52
53         if (hl_device_disabled_or_in_reset(hdev))
54                 return -EBUSY;
55
56         memset(&pkt, 0, sizeof(pkt));
57
58         pkt.ctl = cpu_to_le32(CPUCP_PACKET_I2C_WR <<
59                                 CPUCP_PKT_CTL_OPCODE_SHIFT);
60         pkt.i2c_bus = i2c_bus;
61         pkt.i2c_addr = i2c_addr;
62         pkt.i2c_reg = i2c_reg;
63         pkt.value = cpu_to_le64(val);
64
65         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
66                                                 0, NULL);
67
68         if (rc)
69                 dev_err(hdev->dev, "Failed to write to I2C, error %d\n", rc);
70
71         return rc;
72 }
73
74 static void hl_debugfs_led_set(struct hl_device *hdev, u8 led, u8 state)
75 {
76         struct cpucp_packet pkt;
77         int rc;
78
79         if (hl_device_disabled_or_in_reset(hdev))
80                 return;
81
82         memset(&pkt, 0, sizeof(pkt));
83
84         pkt.ctl = cpu_to_le32(CPUCP_PACKET_LED_SET <<
85                                 CPUCP_PKT_CTL_OPCODE_SHIFT);
86         pkt.led_index = cpu_to_le32(led);
87         pkt.value = cpu_to_le64(state);
88
89         rc = hdev->asic_funcs->send_cpu_message(hdev, (u32 *) &pkt, sizeof(pkt),
90                                                 0, NULL);
91
92         if (rc)
93                 dev_err(hdev->dev, "Failed to set LED %d, error %d\n", led, rc);
94 }
95
96 static int command_buffers_show(struct seq_file *s, void *data)
97 {
98         struct hl_debugfs_entry *entry = s->private;
99         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
100         struct hl_cb *cb;
101         bool first = true;
102
103         spin_lock(&dev_entry->cb_spinlock);
104
105         list_for_each_entry(cb, &dev_entry->cb_list, debugfs_list) {
106                 if (first) {
107                         first = false;
108                         seq_puts(s, "\n");
109                         seq_puts(s, " CB ID   CTX ID   CB size    CB RefCnt    mmap?   CS counter\n");
110                         seq_puts(s, "---------------------------------------------------------------\n");
111                 }
112                 seq_printf(s,
113                         "   %03llu        %d    0x%08x      %d          %d          %d\n",
114                         cb->id, cb->ctx->asid, cb->size,
115                         kref_read(&cb->refcount),
116                         cb->mmap, cb->cs_cnt);
117         }
118
119         spin_unlock(&dev_entry->cb_spinlock);
120
121         if (!first)
122                 seq_puts(s, "\n");
123
124         return 0;
125 }
126
127 static int command_submission_show(struct seq_file *s, void *data)
128 {
129         struct hl_debugfs_entry *entry = s->private;
130         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
131         struct hl_cs *cs;
132         bool first = true;
133
134         spin_lock(&dev_entry->cs_spinlock);
135
136         list_for_each_entry(cs, &dev_entry->cs_list, debugfs_list) {
137                 if (first) {
138                         first = false;
139                         seq_puts(s, "\n");
140                         seq_puts(s, " CS ID   CTX ASID   CS RefCnt   Submitted    Completed\n");
141                         seq_puts(s, "------------------------------------------------------\n");
142                 }
143                 seq_printf(s,
144                         "   %llu       %d          %d           %d            %d\n",
145                         cs->sequence, cs->ctx->asid,
146                         kref_read(&cs->refcount),
147                         cs->submitted, cs->completed);
148         }
149
150         spin_unlock(&dev_entry->cs_spinlock);
151
152         if (!first)
153                 seq_puts(s, "\n");
154
155         return 0;
156 }
157
158 static int command_submission_jobs_show(struct seq_file *s, void *data)
159 {
160         struct hl_debugfs_entry *entry = s->private;
161         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
162         struct hl_cs_job *job;
163         bool first = true;
164
165         spin_lock(&dev_entry->cs_job_spinlock);
166
167         list_for_each_entry(job, &dev_entry->cs_job_list, debugfs_list) {
168                 if (first) {
169                         first = false;
170                         seq_puts(s, "\n");
171                         seq_puts(s, " JOB ID   CS ID    CTX ASID   JOB RefCnt   H/W Queue\n");
172                         seq_puts(s, "----------------------------------------------------\n");
173                 }
174                 if (job->cs)
175                         seq_printf(s,
176                                 "   %02d      %llu        %d          %d           %d\n",
177                                 job->id, job->cs->sequence, job->cs->ctx->asid,
178                                 kref_read(&job->refcount), job->hw_queue_id);
179                 else
180                         seq_printf(s,
181                                 "   %02d      0        %d          %d           %d\n",
182                                 job->id, HL_KERNEL_ASID_ID,
183                                 kref_read(&job->refcount), job->hw_queue_id);
184         }
185
186         spin_unlock(&dev_entry->cs_job_spinlock);
187
188         if (!first)
189                 seq_puts(s, "\n");
190
191         return 0;
192 }
193
194 static int userptr_show(struct seq_file *s, void *data)
195 {
196         struct hl_debugfs_entry *entry = s->private;
197         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
198         struct hl_userptr *userptr;
199         char dma_dir[4][30] = {"DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
200                                 "DMA_FROM_DEVICE", "DMA_NONE"};
201         bool first = true;
202
203         spin_lock(&dev_entry->userptr_spinlock);
204
205         list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
206                 if (first) {
207                         first = false;
208                         seq_puts(s, "\n");
209                         seq_puts(s, " user virtual address     size             dma dir\n");
210                         seq_puts(s, "----------------------------------------------------------\n");
211                 }
212                 seq_printf(s,
213                         "    0x%-14llx      %-10u    %-30s\n",
214                         userptr->addr, userptr->size, dma_dir[userptr->dir]);
215         }
216
217         spin_unlock(&dev_entry->userptr_spinlock);
218
219         if (!first)
220                 seq_puts(s, "\n");
221
222         return 0;
223 }
224
225 static int vm_show(struct seq_file *s, void *data)
226 {
227         struct hl_debugfs_entry *entry = s->private;
228         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
229         struct hl_ctx *ctx;
230         struct hl_vm *vm;
231         struct hl_vm_hash_node *hnode;
232         struct hl_userptr *userptr;
233         struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
234         enum vm_type_t *vm_type;
235         bool once = true;
236         u64 j;
237         int i;
238
239         if (!dev_entry->hdev->mmu_enable)
240                 return 0;
241
242         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
243
244         list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
245                 once = false;
246                 seq_puts(s, "\n\n----------------------------------------------------");
247                 seq_puts(s, "\n----------------------------------------------------\n\n");
248                 seq_printf(s, "ctx asid: %u\n", ctx->asid);
249
250                 seq_puts(s, "\nmappings:\n\n");
251                 seq_puts(s, "    virtual address        size          handle\n");
252                 seq_puts(s, "----------------------------------------------------\n");
253                 mutex_lock(&ctx->mem_hash_lock);
254                 hash_for_each(ctx->mem_hash, i, hnode, node) {
255                         vm_type = hnode->ptr;
256
257                         if (*vm_type == VM_TYPE_USERPTR) {
258                                 userptr = hnode->ptr;
259                                 seq_printf(s,
260                                         "    0x%-14llx      %-10u\n",
261                                         hnode->vaddr, userptr->size);
262                         } else {
263                                 phys_pg_pack = hnode->ptr;
264                                 seq_printf(s,
265                                         "    0x%-14llx      %-10llu       %-4u\n",
266                                         hnode->vaddr, phys_pg_pack->total_size,
267                                         phys_pg_pack->handle);
268                         }
269                 }
270                 mutex_unlock(&ctx->mem_hash_lock);
271
272                 vm = &ctx->hdev->vm;
273                 spin_lock(&vm->idr_lock);
274
275                 if (!idr_is_empty(&vm->phys_pg_pack_handles))
276                         seq_puts(s, "\n\nallocations:\n");
277
278                 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
279                         if (phys_pg_pack->asid != ctx->asid)
280                                 continue;
281
282                         seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
283                         seq_printf(s, "page size: %u\n\n",
284                                                 phys_pg_pack->page_size);
285                         seq_puts(s, "   physical address\n");
286                         seq_puts(s, "---------------------\n");
287                         for (j = 0 ; j < phys_pg_pack->npages ; j++) {
288                                 seq_printf(s, "    0x%-14llx\n",
289                                                 phys_pg_pack->pages[j]);
290                         }
291                 }
292                 spin_unlock(&vm->idr_lock);
293
294         }
295
296         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
297
298         if (!once)
299                 seq_puts(s, "\n");
300
301         return 0;
302 }
303
304 /* these inline functions are copied from mmu.c */
305 static inline u64 get_hop0_addr(struct hl_ctx *ctx)
306 {
307         return ctx->hdev->asic_prop.mmu_pgt_addr +
308                         (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
309 }
310
311 static inline u64 get_hopN_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
312                                         u64 virt_addr, u64 mask, u64 shift)
313 {
314         return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
315                         ((virt_addr & mask) >> shift);
316 }
317
318 static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx,
319                                         struct hl_mmu_properties *mmu_specs,
320                                         u64 hop_addr, u64 vaddr)
321 {
322         return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop0_mask,
323                                         mmu_specs->hop0_shift);
324 }
325
326 static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx,
327                                         struct hl_mmu_properties *mmu_specs,
328                                         u64 hop_addr, u64 vaddr)
329 {
330         return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop1_mask,
331                                         mmu_specs->hop1_shift);
332 }
333
334 static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx,
335                                         struct hl_mmu_properties *mmu_specs,
336                                         u64 hop_addr, u64 vaddr)
337 {
338         return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop2_mask,
339                                         mmu_specs->hop2_shift);
340 }
341
342 static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx,
343                                         struct hl_mmu_properties *mmu_specs,
344                                         u64 hop_addr, u64 vaddr)
345 {
346         return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop3_mask,
347                                         mmu_specs->hop3_shift);
348 }
349
350 static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx,
351                                         struct hl_mmu_properties *mmu_specs,
352                                         u64 hop_addr, u64 vaddr)
353 {
354         return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop4_mask,
355                                         mmu_specs->hop4_shift);
356 }
357
358 static inline u64 get_hop5_pte_addr(struct hl_ctx *ctx,
359                                         struct hl_mmu_properties *mmu_specs,
360                                         u64 hop_addr, u64 vaddr)
361 {
362         return get_hopN_pte_addr(ctx, hop_addr, vaddr, mmu_specs->hop5_mask,
363                                         mmu_specs->hop5_shift);
364 }
365
366 static inline u64 get_next_hop_addr(u64 curr_pte)
367 {
368         if (curr_pte & PAGE_PRESENT_MASK)
369                 return curr_pte & HOP_PHYS_ADDR_MASK;
370         else
371                 return ULLONG_MAX;
372 }
373
374 static int mmu_show(struct seq_file *s, void *data)
375 {
376         struct hl_debugfs_entry *entry = s->private;
377         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
378         struct hl_device *hdev = dev_entry->hdev;
379         struct asic_fixed_properties *prop = &hdev->asic_prop;
380         struct hl_mmu_properties *mmu_prop;
381         struct hl_ctx *ctx;
382         bool is_dram_addr;
383
384         u64 hop0_addr = 0, hop0_pte_addr = 0, hop0_pte = 0,
385                 hop1_addr = 0, hop1_pte_addr = 0, hop1_pte = 0,
386                 hop2_addr = 0, hop2_pte_addr = 0, hop2_pte = 0,
387                 hop3_addr = 0, hop3_pte_addr = 0, hop3_pte = 0,
388                 hop4_addr = 0, hop4_pte_addr = 0, hop4_pte = 0,
389                 hop5_addr = 0, hop5_pte_addr = 0, hop5_pte = 0,
390                 virt_addr = dev_entry->mmu_addr;
391
392         if (!hdev->mmu_enable)
393                 return 0;
394
395         if (dev_entry->mmu_asid == HL_KERNEL_ASID_ID)
396                 ctx = hdev->kernel_ctx;
397         else
398                 ctx = hdev->compute_ctx;
399
400         if (!ctx) {
401                 dev_err(hdev->dev, "no ctx available\n");
402                 return 0;
403         }
404
405         is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
406                                                 prop->dmmu.start_addr,
407                                                 prop->dmmu.end_addr);
408
409         /* shifts and masks are the same in PMMU and HPMMU, use one of them */
410         mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
411
412         mutex_lock(&ctx->mmu_lock);
413
414         /* the following lookup is copied from unmap() in mmu.c */
415
416         hop0_addr = get_hop0_addr(ctx);
417         hop0_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop0_addr, virt_addr);
418         hop0_pte = hdev->asic_funcs->read_pte(hdev, hop0_pte_addr);
419         hop1_addr = get_next_hop_addr(hop0_pte);
420
421         if (hop1_addr == ULLONG_MAX)
422                 goto not_mapped;
423
424         hop1_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop1_addr, virt_addr);
425         hop1_pte = hdev->asic_funcs->read_pte(hdev, hop1_pte_addr);
426         hop2_addr = get_next_hop_addr(hop1_pte);
427
428         if (hop2_addr == ULLONG_MAX)
429                 goto not_mapped;
430
431         hop2_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop2_addr, virt_addr);
432         hop2_pte = hdev->asic_funcs->read_pte(hdev, hop2_pte_addr);
433         hop3_addr = get_next_hop_addr(hop2_pte);
434
435         if (hop3_addr == ULLONG_MAX)
436                 goto not_mapped;
437
438         hop3_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop3_addr, virt_addr);
439         hop3_pte = hdev->asic_funcs->read_pte(hdev, hop3_pte_addr);
440
441         if (mmu_prop->num_hops == MMU_ARCH_5_HOPS) {
442                 if (!(hop3_pte & LAST_MASK)) {
443                         hop4_addr = get_next_hop_addr(hop3_pte);
444
445                         if (hop4_addr == ULLONG_MAX)
446                                 goto not_mapped;
447
448                         hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop,
449                                                         hop4_addr, virt_addr);
450                         hop4_pte = hdev->asic_funcs->read_pte(hdev,
451                                                                 hop4_pte_addr);
452                         if (!(hop4_pte & PAGE_PRESENT_MASK))
453                                 goto not_mapped;
454                 } else {
455                         if (!(hop3_pte & PAGE_PRESENT_MASK))
456                                 goto not_mapped;
457                 }
458         } else {
459                 hop4_addr = get_next_hop_addr(hop3_pte);
460
461                 if (hop4_addr == ULLONG_MAX)
462                         goto not_mapped;
463
464                 hop4_pte_addr = get_hop4_pte_addr(ctx, mmu_prop,
465                                                 hop4_addr, virt_addr);
466                 hop4_pte = hdev->asic_funcs->read_pte(hdev,
467                                                         hop4_pte_addr);
468                 if (!(hop4_pte & LAST_MASK)) {
469                         hop5_addr = get_next_hop_addr(hop4_pte);
470
471                         if (hop5_addr == ULLONG_MAX)
472                                 goto not_mapped;
473
474                         hop5_pte_addr = get_hop5_pte_addr(ctx, mmu_prop,
475                                                         hop5_addr, virt_addr);
476                         hop5_pte = hdev->asic_funcs->read_pte(hdev,
477                                                                 hop5_pte_addr);
478                         if (!(hop5_pte & PAGE_PRESENT_MASK))
479                                 goto not_mapped;
480                 } else {
481                         if (!(hop4_pte & PAGE_PRESENT_MASK))
482                                 goto not_mapped;
483                 }
484         }
485
486         seq_printf(s, "asid: %u, virt_addr: 0x%llx\n",
487                         dev_entry->mmu_asid, dev_entry->mmu_addr);
488
489         seq_printf(s, "hop0_addr: 0x%llx\n", hop0_addr);
490         seq_printf(s, "hop0_pte_addr: 0x%llx\n", hop0_pte_addr);
491         seq_printf(s, "hop0_pte: 0x%llx\n", hop0_pte);
492
493         seq_printf(s, "hop1_addr: 0x%llx\n", hop1_addr);
494         seq_printf(s, "hop1_pte_addr: 0x%llx\n", hop1_pte_addr);
495         seq_printf(s, "hop1_pte: 0x%llx\n", hop1_pte);
496
497         seq_printf(s, "hop2_addr: 0x%llx\n", hop2_addr);
498         seq_printf(s, "hop2_pte_addr: 0x%llx\n", hop2_pte_addr);
499         seq_printf(s, "hop2_pte: 0x%llx\n", hop2_pte);
500
501         seq_printf(s, "hop3_addr: 0x%llx\n", hop3_addr);
502         seq_printf(s, "hop3_pte_addr: 0x%llx\n", hop3_pte_addr);
503         seq_printf(s, "hop3_pte: 0x%llx\n", hop3_pte);
504
505         if (mmu_prop->num_hops == MMU_ARCH_5_HOPS) {
506                 if (!(hop3_pte & LAST_MASK)) {
507                         seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
508                         seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
509                         seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
510                 }
511         } else {
512                 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
513                 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
514                 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
515
516                 if (!(hop4_pte & LAST_MASK)) {
517                         seq_printf(s, "hop5_addr: 0x%llx\n", hop5_addr);
518                         seq_printf(s, "hop5_pte_addr: 0x%llx\n", hop5_pte_addr);
519                         seq_printf(s, "hop5_pte: 0x%llx\n", hop5_pte);
520                 }
521         }
522
523         goto out;
524
525 not_mapped:
526         dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
527                         virt_addr);
528 out:
529         mutex_unlock(&ctx->mmu_lock);
530
531         return 0;
532 }
533
534 static ssize_t mmu_asid_va_write(struct file *file, const char __user *buf,
535                 size_t count, loff_t *f_pos)
536 {
537         struct seq_file *s = file->private_data;
538         struct hl_debugfs_entry *entry = s->private;
539         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
540         struct hl_device *hdev = dev_entry->hdev;
541         char kbuf[MMU_KBUF_SIZE];
542         char *c;
543         ssize_t rc;
544
545         if (!hdev->mmu_enable)
546                 return count;
547
548         if (count > sizeof(kbuf) - 1)
549                 goto err;
550         if (copy_from_user(kbuf, buf, count))
551                 goto err;
552         kbuf[count] = 0;
553
554         c = strchr(kbuf, ' ');
555         if (!c)
556                 goto err;
557         *c = '\0';
558
559         rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
560         if (rc)
561                 goto err;
562
563         if (strncmp(c+1, "0x", 2))
564                 goto err;
565         rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
566         if (rc)
567                 goto err;
568
569         return count;
570
571 err:
572         dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
573
574         return -EINVAL;
575 }
576
577 static int engines_show(struct seq_file *s, void *data)
578 {
579         struct hl_debugfs_entry *entry = s->private;
580         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
581         struct hl_device *hdev = dev_entry->hdev;
582
583         if (atomic_read(&hdev->in_reset)) {
584                 dev_warn_ratelimited(hdev->dev,
585                                 "Can't check device idle during reset\n");
586                 return 0;
587         }
588
589         hdev->asic_funcs->is_device_idle(hdev, NULL, s);
590
591         return 0;
592 }
593
594 static bool hl_is_device_va(struct hl_device *hdev, u64 addr)
595 {
596         struct asic_fixed_properties *prop = &hdev->asic_prop;
597
598         if (!hdev->mmu_enable)
599                 goto out;
600
601         if (hdev->dram_supports_virtual_memory &&
602                 (addr >= prop->dmmu.start_addr && addr < prop->dmmu.end_addr))
603                 return true;
604
605         if (addr >= prop->pmmu.start_addr &&
606                 addr < prop->pmmu.end_addr)
607                 return true;
608
609         if (addr >= prop->pmmu_huge.start_addr &&
610                 addr < prop->pmmu_huge.end_addr)
611                 return true;
612 out:
613         return false;
614 }
615
616 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr,
617                                 u64 *phys_addr)
618 {
619         struct hl_ctx *ctx = hdev->compute_ctx;
620         struct asic_fixed_properties *prop = &hdev->asic_prop;
621         struct hl_mmu_properties *mmu_prop;
622         u64 hop_addr, hop_pte_addr, hop_pte;
623         u64 offset_mask = HOP4_MASK | FLAGS_MASK;
624         int rc = 0;
625         bool is_dram_addr;
626
627         if (!ctx) {
628                 dev_err(hdev->dev, "no ctx available\n");
629                 return -EINVAL;
630         }
631
632         is_dram_addr = hl_mem_area_inside_range(virt_addr, prop->dmmu.page_size,
633                                                 prop->dmmu.start_addr,
634                                                 prop->dmmu.end_addr);
635
636         /* shifts and masks are the same in PMMU and HPMMU, use one of them */
637         mmu_prop = is_dram_addr ? &prop->dmmu : &prop->pmmu;
638
639         mutex_lock(&ctx->mmu_lock);
640
641         /* hop 0 */
642         hop_addr = get_hop0_addr(ctx);
643         hop_pte_addr = get_hop0_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
644         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
645
646         /* hop 1 */
647         hop_addr = get_next_hop_addr(hop_pte);
648         if (hop_addr == ULLONG_MAX)
649                 goto not_mapped;
650         hop_pte_addr = get_hop1_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
651         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
652
653         /* hop 2 */
654         hop_addr = get_next_hop_addr(hop_pte);
655         if (hop_addr == ULLONG_MAX)
656                 goto not_mapped;
657         hop_pte_addr = get_hop2_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
658         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
659
660         /* hop 3 */
661         hop_addr = get_next_hop_addr(hop_pte);
662         if (hop_addr == ULLONG_MAX)
663                 goto not_mapped;
664         hop_pte_addr = get_hop3_pte_addr(ctx, mmu_prop, hop_addr, virt_addr);
665         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
666
667         if (!(hop_pte & LAST_MASK)) {
668                 /* hop 4 */
669                 hop_addr = get_next_hop_addr(hop_pte);
670                 if (hop_addr == ULLONG_MAX)
671                         goto not_mapped;
672                 hop_pte_addr = get_hop4_pte_addr(ctx, mmu_prop, hop_addr,
673                                                         virt_addr);
674                 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
675
676                 offset_mask = FLAGS_MASK;
677         }
678
679         if (!(hop_pte & PAGE_PRESENT_MASK))
680                 goto not_mapped;
681
682         *phys_addr = (hop_pte & ~offset_mask) | (virt_addr & offset_mask);
683
684         goto out;
685
686 not_mapped:
687         dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
688                         virt_addr);
689         rc = -EINVAL;
690 out:
691         mutex_unlock(&ctx->mmu_lock);
692         return rc;
693 }
694
695 static ssize_t hl_data_read32(struct file *f, char __user *buf,
696                                         size_t count, loff_t *ppos)
697 {
698         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
699         struct hl_device *hdev = entry->hdev;
700         char tmp_buf[32];
701         u64 addr = entry->addr;
702         u32 val;
703         ssize_t rc;
704
705         if (atomic_read(&hdev->in_reset)) {
706                 dev_warn_ratelimited(hdev->dev, "Can't read during reset\n");
707                 return 0;
708         }
709
710         if (*ppos)
711                 return 0;
712
713         if (hl_is_device_va(hdev, addr)) {
714                 rc = device_va_to_pa(hdev, addr, &addr);
715                 if (rc)
716                         return rc;
717         }
718
719         rc = hdev->asic_funcs->debugfs_read32(hdev, addr, &val);
720         if (rc) {
721                 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
722                 return rc;
723         }
724
725         sprintf(tmp_buf, "0x%08x\n", val);
726         return simple_read_from_buffer(buf, count, ppos, tmp_buf,
727                         strlen(tmp_buf));
728 }
729
730 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
731                                         size_t count, loff_t *ppos)
732 {
733         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
734         struct hl_device *hdev = entry->hdev;
735         u64 addr = entry->addr;
736         u32 value;
737         ssize_t rc;
738
739         if (atomic_read(&hdev->in_reset)) {
740                 dev_warn_ratelimited(hdev->dev, "Can't write during reset\n");
741                 return 0;
742         }
743
744         rc = kstrtouint_from_user(buf, count, 16, &value);
745         if (rc)
746                 return rc;
747
748         if (hl_is_device_va(hdev, addr)) {
749                 rc = device_va_to_pa(hdev, addr, &addr);
750                 if (rc)
751                         return rc;
752         }
753
754         rc = hdev->asic_funcs->debugfs_write32(hdev, addr, value);
755         if (rc) {
756                 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
757                         value, addr);
758                 return rc;
759         }
760
761         return count;
762 }
763
764 static ssize_t hl_data_read64(struct file *f, char __user *buf,
765                                         size_t count, loff_t *ppos)
766 {
767         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
768         struct hl_device *hdev = entry->hdev;
769         char tmp_buf[32];
770         u64 addr = entry->addr;
771         u64 val;
772         ssize_t rc;
773
774         if (*ppos)
775                 return 0;
776
777         if (hl_is_device_va(hdev, addr)) {
778                 rc = device_va_to_pa(hdev, addr, &addr);
779                 if (rc)
780                         return rc;
781         }
782
783         rc = hdev->asic_funcs->debugfs_read64(hdev, addr, &val);
784         if (rc) {
785                 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
786                 return rc;
787         }
788
789         sprintf(tmp_buf, "0x%016llx\n", val);
790         return simple_read_from_buffer(buf, count, ppos, tmp_buf,
791                         strlen(tmp_buf));
792 }
793
794 static ssize_t hl_data_write64(struct file *f, const char __user *buf,
795                                         size_t count, loff_t *ppos)
796 {
797         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
798         struct hl_device *hdev = entry->hdev;
799         u64 addr = entry->addr;
800         u64 value;
801         ssize_t rc;
802
803         rc = kstrtoull_from_user(buf, count, 16, &value);
804         if (rc)
805                 return rc;
806
807         if (hl_is_device_va(hdev, addr)) {
808                 rc = device_va_to_pa(hdev, addr, &addr);
809                 if (rc)
810                         return rc;
811         }
812
813         rc = hdev->asic_funcs->debugfs_write64(hdev, addr, value);
814         if (rc) {
815                 dev_err(hdev->dev, "Failed to write 0x%016llx to 0x%010llx\n",
816                         value, addr);
817                 return rc;
818         }
819
820         return count;
821 }
822
823 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
824                 size_t count, loff_t *ppos)
825 {
826         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
827         struct hl_device *hdev = entry->hdev;
828         char tmp_buf[200];
829         int i;
830
831         if (*ppos)
832                 return 0;
833
834         if (hdev->pdev->current_state == PCI_D0)
835                 i = 1;
836         else if (hdev->pdev->current_state == PCI_D3hot)
837                 i = 2;
838         else
839                 i = 3;
840
841         sprintf(tmp_buf,
842                 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
843         return simple_read_from_buffer(buf, count, ppos, tmp_buf,
844                         strlen(tmp_buf));
845 }
846
847 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
848                                         size_t count, loff_t *ppos)
849 {
850         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
851         struct hl_device *hdev = entry->hdev;
852         u32 value;
853         ssize_t rc;
854
855         rc = kstrtouint_from_user(buf, count, 10, &value);
856         if (rc)
857                 return rc;
858
859         if (value == 1) {
860                 pci_set_power_state(hdev->pdev, PCI_D0);
861                 pci_restore_state(hdev->pdev);
862                 rc = pci_enable_device(hdev->pdev);
863         } else if (value == 2) {
864                 pci_save_state(hdev->pdev);
865                 pci_disable_device(hdev->pdev);
866                 pci_set_power_state(hdev->pdev, PCI_D3hot);
867         } else {
868                 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
869                 return -EINVAL;
870         }
871
872         return count;
873 }
874
875 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
876                                         size_t count, loff_t *ppos)
877 {
878         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
879         struct hl_device *hdev = entry->hdev;
880         char tmp_buf[32];
881         long val;
882         ssize_t rc;
883
884         if (*ppos)
885                 return 0;
886
887         rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
888                         entry->i2c_reg, &val);
889         if (rc) {
890                 dev_err(hdev->dev,
891                         "Failed to read from I2C bus %d, addr %d, reg %d\n",
892                         entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
893                 return rc;
894         }
895
896         sprintf(tmp_buf, "0x%02lx\n", val);
897         rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
898                         strlen(tmp_buf));
899
900         return rc;
901 }
902
903 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
904                                         size_t count, loff_t *ppos)
905 {
906         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
907         struct hl_device *hdev = entry->hdev;
908         u32 value;
909         ssize_t rc;
910
911         rc = kstrtouint_from_user(buf, count, 16, &value);
912         if (rc)
913                 return rc;
914
915         rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
916                         entry->i2c_reg, value);
917         if (rc) {
918                 dev_err(hdev->dev,
919                         "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
920                         value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
921                 return rc;
922         }
923
924         return count;
925 }
926
927 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
928                                         size_t count, loff_t *ppos)
929 {
930         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
931         struct hl_device *hdev = entry->hdev;
932         u32 value;
933         ssize_t rc;
934
935         rc = kstrtouint_from_user(buf, count, 10, &value);
936         if (rc)
937                 return rc;
938
939         value = value ? 1 : 0;
940
941         hl_debugfs_led_set(hdev, 0, value);
942
943         return count;
944 }
945
946 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
947                                         size_t count, loff_t *ppos)
948 {
949         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
950         struct hl_device *hdev = entry->hdev;
951         u32 value;
952         ssize_t rc;
953
954         rc = kstrtouint_from_user(buf, count, 10, &value);
955         if (rc)
956                 return rc;
957
958         value = value ? 1 : 0;
959
960         hl_debugfs_led_set(hdev, 1, value);
961
962         return count;
963 }
964
965 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
966                                         size_t count, loff_t *ppos)
967 {
968         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
969         struct hl_device *hdev = entry->hdev;
970         u32 value;
971         ssize_t rc;
972
973         rc = kstrtouint_from_user(buf, count, 10, &value);
974         if (rc)
975                 return rc;
976
977         value = value ? 1 : 0;
978
979         hl_debugfs_led_set(hdev, 2, value);
980
981         return count;
982 }
983
984 static ssize_t hl_device_read(struct file *f, char __user *buf,
985                                         size_t count, loff_t *ppos)
986 {
987         static const char *help =
988                 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
989         return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
990 }
991
992 static ssize_t hl_device_write(struct file *f, const char __user *buf,
993                                      size_t count, loff_t *ppos)
994 {
995         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
996         struct hl_device *hdev = entry->hdev;
997         char data[30] = {0};
998
999         /* don't allow partial writes */
1000         if (*ppos != 0)
1001                 return 0;
1002
1003         simple_write_to_buffer(data, 29, ppos, buf, count);
1004
1005         if (strncmp("disable", data, strlen("disable")) == 0) {
1006                 hdev->disabled = true;
1007         } else if (strncmp("enable", data, strlen("enable")) == 0) {
1008                 hdev->disabled = false;
1009         } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
1010                 hdev->asic_funcs->suspend(hdev);
1011         } else if (strncmp("resume", data, strlen("resume")) == 0) {
1012                 hdev->asic_funcs->resume(hdev);
1013         } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
1014                 hdev->device_cpu_disabled = true;
1015         } else {
1016                 dev_err(hdev->dev,
1017                         "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
1018                 count = -EINVAL;
1019         }
1020
1021         return count;
1022 }
1023
1024 static ssize_t hl_clk_gate_read(struct file *f, char __user *buf,
1025                                         size_t count, loff_t *ppos)
1026 {
1027         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1028         struct hl_device *hdev = entry->hdev;
1029         char tmp_buf[200];
1030         ssize_t rc;
1031
1032         if (*ppos)
1033                 return 0;
1034
1035         sprintf(tmp_buf, "0x%llx\n", hdev->clock_gating_mask);
1036         rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
1037                         strlen(tmp_buf) + 1);
1038
1039         return rc;
1040 }
1041
1042 static ssize_t hl_clk_gate_write(struct file *f, const char __user *buf,
1043                                      size_t count, loff_t *ppos)
1044 {
1045         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1046         struct hl_device *hdev = entry->hdev;
1047         u64 value;
1048         ssize_t rc;
1049
1050         if (atomic_read(&hdev->in_reset)) {
1051                 dev_warn_ratelimited(hdev->dev,
1052                                 "Can't change clock gating during reset\n");
1053                 return 0;
1054         }
1055
1056         rc = kstrtoull_from_user(buf, count, 16, &value);
1057         if (rc)
1058                 return rc;
1059
1060         hdev->clock_gating_mask = value;
1061         hdev->asic_funcs->set_clock_gating(hdev);
1062
1063         return count;
1064 }
1065
1066 static ssize_t hl_stop_on_err_read(struct file *f, char __user *buf,
1067                                         size_t count, loff_t *ppos)
1068 {
1069         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1070         struct hl_device *hdev = entry->hdev;
1071         char tmp_buf[200];
1072         ssize_t rc;
1073
1074         if (*ppos)
1075                 return 0;
1076
1077         sprintf(tmp_buf, "%d\n", hdev->stop_on_err);
1078         rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
1079                         strlen(tmp_buf) + 1);
1080
1081         return rc;
1082 }
1083
1084 static ssize_t hl_stop_on_err_write(struct file *f, const char __user *buf,
1085                                      size_t count, loff_t *ppos)
1086 {
1087         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
1088         struct hl_device *hdev = entry->hdev;
1089         u32 value;
1090         ssize_t rc;
1091
1092         if (atomic_read(&hdev->in_reset)) {
1093                 dev_warn_ratelimited(hdev->dev,
1094                                 "Can't change stop on error during reset\n");
1095                 return 0;
1096         }
1097
1098         rc = kstrtouint_from_user(buf, count, 10, &value);
1099         if (rc)
1100                 return rc;
1101
1102         hdev->stop_on_err = value ? 1 : 0;
1103
1104         hl_device_reset(hdev, false, false);
1105
1106         return count;
1107 }
1108
1109 static const struct file_operations hl_data32b_fops = {
1110         .owner = THIS_MODULE,
1111         .read = hl_data_read32,
1112         .write = hl_data_write32
1113 };
1114
1115 static const struct file_operations hl_data64b_fops = {
1116         .owner = THIS_MODULE,
1117         .read = hl_data_read64,
1118         .write = hl_data_write64
1119 };
1120
1121 static const struct file_operations hl_i2c_data_fops = {
1122         .owner = THIS_MODULE,
1123         .read = hl_i2c_data_read,
1124         .write = hl_i2c_data_write
1125 };
1126
1127 static const struct file_operations hl_power_fops = {
1128         .owner = THIS_MODULE,
1129         .read = hl_get_power_state,
1130         .write = hl_set_power_state
1131 };
1132
1133 static const struct file_operations hl_led0_fops = {
1134         .owner = THIS_MODULE,
1135         .write = hl_led0_write
1136 };
1137
1138 static const struct file_operations hl_led1_fops = {
1139         .owner = THIS_MODULE,
1140         .write = hl_led1_write
1141 };
1142
1143 static const struct file_operations hl_led2_fops = {
1144         .owner = THIS_MODULE,
1145         .write = hl_led2_write
1146 };
1147
1148 static const struct file_operations hl_device_fops = {
1149         .owner = THIS_MODULE,
1150         .read = hl_device_read,
1151         .write = hl_device_write
1152 };
1153
1154 static const struct file_operations hl_clk_gate_fops = {
1155         .owner = THIS_MODULE,
1156         .read = hl_clk_gate_read,
1157         .write = hl_clk_gate_write
1158 };
1159
1160 static const struct file_operations hl_stop_on_err_fops = {
1161         .owner = THIS_MODULE,
1162         .read = hl_stop_on_err_read,
1163         .write = hl_stop_on_err_write
1164 };
1165
1166 static const struct hl_info_list hl_debugfs_list[] = {
1167         {"command_buffers", command_buffers_show, NULL},
1168         {"command_submission", command_submission_show, NULL},
1169         {"command_submission_jobs", command_submission_jobs_show, NULL},
1170         {"userptr", userptr_show, NULL},
1171         {"vm", vm_show, NULL},
1172         {"mmu", mmu_show, mmu_asid_va_write},
1173         {"engines", engines_show, NULL}
1174 };
1175
1176 static int hl_debugfs_open(struct inode *inode, struct file *file)
1177 {
1178         struct hl_debugfs_entry *node = inode->i_private;
1179
1180         return single_open(file, node->info_ent->show, node);
1181 }
1182
1183 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
1184                 size_t count, loff_t *f_pos)
1185 {
1186         struct hl_debugfs_entry *node = file->f_inode->i_private;
1187
1188         if (node->info_ent->write)
1189                 return node->info_ent->write(file, buf, count, f_pos);
1190         else
1191                 return -EINVAL;
1192
1193 }
1194
1195 static const struct file_operations hl_debugfs_fops = {
1196         .owner = THIS_MODULE,
1197         .open = hl_debugfs_open,
1198         .read = seq_read,
1199         .write = hl_debugfs_write,
1200         .llseek = seq_lseek,
1201         .release = single_release,
1202 };
1203
1204 void hl_debugfs_add_device(struct hl_device *hdev)
1205 {
1206         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1207         int count = ARRAY_SIZE(hl_debugfs_list);
1208         struct hl_debugfs_entry *entry;
1209         struct dentry *ent;
1210         int i;
1211
1212         dev_entry->hdev = hdev;
1213         dev_entry->entry_arr = kmalloc_array(count,
1214                                         sizeof(struct hl_debugfs_entry),
1215                                         GFP_KERNEL);
1216         if (!dev_entry->entry_arr)
1217                 return;
1218
1219         INIT_LIST_HEAD(&dev_entry->file_list);
1220         INIT_LIST_HEAD(&dev_entry->cb_list);
1221         INIT_LIST_HEAD(&dev_entry->cs_list);
1222         INIT_LIST_HEAD(&dev_entry->cs_job_list);
1223         INIT_LIST_HEAD(&dev_entry->userptr_list);
1224         INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
1225         mutex_init(&dev_entry->file_mutex);
1226         spin_lock_init(&dev_entry->cb_spinlock);
1227         spin_lock_init(&dev_entry->cs_spinlock);
1228         spin_lock_init(&dev_entry->cs_job_spinlock);
1229         spin_lock_init(&dev_entry->userptr_spinlock);
1230         spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
1231
1232         dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
1233                                                 hl_debug_root);
1234
1235         debugfs_create_x64("addr",
1236                                 0644,
1237                                 dev_entry->root,
1238                                 &dev_entry->addr);
1239
1240         debugfs_create_file("data32",
1241                                 0644,
1242                                 dev_entry->root,
1243                                 dev_entry,
1244                                 &hl_data32b_fops);
1245
1246         debugfs_create_file("data64",
1247                                 0644,
1248                                 dev_entry->root,
1249                                 dev_entry,
1250                                 &hl_data64b_fops);
1251
1252         debugfs_create_file("set_power_state",
1253                                 0200,
1254                                 dev_entry->root,
1255                                 dev_entry,
1256                                 &hl_power_fops);
1257
1258         debugfs_create_u8("i2c_bus",
1259                                 0644,
1260                                 dev_entry->root,
1261                                 &dev_entry->i2c_bus);
1262
1263         debugfs_create_u8("i2c_addr",
1264                                 0644,
1265                                 dev_entry->root,
1266                                 &dev_entry->i2c_addr);
1267
1268         debugfs_create_u8("i2c_reg",
1269                                 0644,
1270                                 dev_entry->root,
1271                                 &dev_entry->i2c_reg);
1272
1273         debugfs_create_file("i2c_data",
1274                                 0644,
1275                                 dev_entry->root,
1276                                 dev_entry,
1277                                 &hl_i2c_data_fops);
1278
1279         debugfs_create_file("led0",
1280                                 0200,
1281                                 dev_entry->root,
1282                                 dev_entry,
1283                                 &hl_led0_fops);
1284
1285         debugfs_create_file("led1",
1286                                 0200,
1287                                 dev_entry->root,
1288                                 dev_entry,
1289                                 &hl_led1_fops);
1290
1291         debugfs_create_file("led2",
1292                                 0200,
1293                                 dev_entry->root,
1294                                 dev_entry,
1295                                 &hl_led2_fops);
1296
1297         debugfs_create_file("device",
1298                                 0200,
1299                                 dev_entry->root,
1300                                 dev_entry,
1301                                 &hl_device_fops);
1302
1303         debugfs_create_file("clk_gate",
1304                                 0200,
1305                                 dev_entry->root,
1306                                 dev_entry,
1307                                 &hl_clk_gate_fops);
1308
1309         debugfs_create_file("stop_on_err",
1310                                 0644,
1311                                 dev_entry->root,
1312                                 dev_entry,
1313                                 &hl_stop_on_err_fops);
1314
1315         for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1316
1317                 ent = debugfs_create_file(hl_debugfs_list[i].name,
1318                                         0444,
1319                                         dev_entry->root,
1320                                         entry,
1321                                         &hl_debugfs_fops);
1322                 entry->dent = ent;
1323                 entry->info_ent = &hl_debugfs_list[i];
1324                 entry->dev_entry = dev_entry;
1325         }
1326 }
1327
1328 void hl_debugfs_remove_device(struct hl_device *hdev)
1329 {
1330         struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1331
1332         debugfs_remove_recursive(entry->root);
1333
1334         mutex_destroy(&entry->file_mutex);
1335         kfree(entry->entry_arr);
1336 }
1337
1338 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1339 {
1340         struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1341
1342         mutex_lock(&dev_entry->file_mutex);
1343         list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1344         mutex_unlock(&dev_entry->file_mutex);
1345 }
1346
1347 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1348 {
1349         struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1350
1351         mutex_lock(&dev_entry->file_mutex);
1352         list_del(&hpriv->debugfs_list);
1353         mutex_unlock(&dev_entry->file_mutex);
1354 }
1355
1356 void hl_debugfs_add_cb(struct hl_cb *cb)
1357 {
1358         struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1359
1360         spin_lock(&dev_entry->cb_spinlock);
1361         list_add(&cb->debugfs_list, &dev_entry->cb_list);
1362         spin_unlock(&dev_entry->cb_spinlock);
1363 }
1364
1365 void hl_debugfs_remove_cb(struct hl_cb *cb)
1366 {
1367         struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1368
1369         spin_lock(&dev_entry->cb_spinlock);
1370         list_del(&cb->debugfs_list);
1371         spin_unlock(&dev_entry->cb_spinlock);
1372 }
1373
1374 void hl_debugfs_add_cs(struct hl_cs *cs)
1375 {
1376         struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1377
1378         spin_lock(&dev_entry->cs_spinlock);
1379         list_add(&cs->debugfs_list, &dev_entry->cs_list);
1380         spin_unlock(&dev_entry->cs_spinlock);
1381 }
1382
1383 void hl_debugfs_remove_cs(struct hl_cs *cs)
1384 {
1385         struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1386
1387         spin_lock(&dev_entry->cs_spinlock);
1388         list_del(&cs->debugfs_list);
1389         spin_unlock(&dev_entry->cs_spinlock);
1390 }
1391
1392 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1393 {
1394         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1395
1396         spin_lock(&dev_entry->cs_job_spinlock);
1397         list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1398         spin_unlock(&dev_entry->cs_job_spinlock);
1399 }
1400
1401 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1402 {
1403         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1404
1405         spin_lock(&dev_entry->cs_job_spinlock);
1406         list_del(&job->debugfs_list);
1407         spin_unlock(&dev_entry->cs_job_spinlock);
1408 }
1409
1410 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1411 {
1412         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1413
1414         spin_lock(&dev_entry->userptr_spinlock);
1415         list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1416         spin_unlock(&dev_entry->userptr_spinlock);
1417 }
1418
1419 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1420                                 struct hl_userptr *userptr)
1421 {
1422         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1423
1424         spin_lock(&dev_entry->userptr_spinlock);
1425         list_del(&userptr->debugfs_list);
1426         spin_unlock(&dev_entry->userptr_spinlock);
1427 }
1428
1429 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1430 {
1431         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1432
1433         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1434         list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1435         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1436 }
1437
1438 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1439 {
1440         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1441
1442         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1443         list_del(&ctx->debugfs_list);
1444         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1445 }
1446
1447 void __init hl_debugfs_init(void)
1448 {
1449         hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1450 }
1451
1452 void hl_debugfs_fini(void)
1453 {
1454         debugfs_remove_recursive(hl_debug_root);
1455 }