]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/habanalabs/debugfs.c
habanalabs: fix debugfs code
[linux.git] / drivers / misc / habanalabs / 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, u32 *val)
23 {
24         struct armcp_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(ARMCP_PACKET_I2C_RD <<
33                                 ARMCP_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                                         HL_DEVICE_TIMEOUT_USEC, (long *) 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 armcp_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(ARMCP_PACKET_I2C_WR <<
59                                 ARMCP_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                                         HL_DEVICE_TIMEOUT_USEC, 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 armcp_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(ARMCP_PACKET_LED_SET <<
85                                 ARMCP_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                                                 HL_DEVICE_TIMEOUT_USEC, 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                         "   %03d        %d    0x%08x      %d          %d          %d\n",
114                         cb->id, cb->ctx_id, 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   H/W Queue\n");
172                         seq_puts(s, "---------------------------------------\n");
173                 }
174                 if (job->cs)
175                         seq_printf(s,
176                                 "    %02d       %llu         %d         %d\n",
177                                 job->id, job->cs->sequence, job->cs->ctx->asid,
178                                 job->hw_queue_id);
179                 else
180                         seq_printf(s,
181                                 "    %02d       0         %d         %d\n",
182                                 job->id, HL_KERNEL_ASID_ID, job->hw_queue_id);
183         }
184
185         spin_unlock(&dev_entry->cs_job_spinlock);
186
187         if (!first)
188                 seq_puts(s, "\n");
189
190         return 0;
191 }
192
193 static int userptr_show(struct seq_file *s, void *data)
194 {
195         struct hl_debugfs_entry *entry = s->private;
196         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
197         struct hl_userptr *userptr;
198         char dma_dir[4][30] = {"DMA_BIDIRECTIONAL", "DMA_TO_DEVICE",
199                                 "DMA_FROM_DEVICE", "DMA_NONE"};
200         bool first = true;
201
202         spin_lock(&dev_entry->userptr_spinlock);
203
204         list_for_each_entry(userptr, &dev_entry->userptr_list, debugfs_list) {
205                 if (first) {
206                         first = false;
207                         seq_puts(s, "\n");
208                         seq_puts(s, " user virtual address     size             dma dir\n");
209                         seq_puts(s, "----------------------------------------------------------\n");
210                 }
211                 seq_printf(s,
212                         "    0x%-14llx      %-10u    %-30s\n",
213                         userptr->addr, userptr->size, dma_dir[userptr->dir]);
214         }
215
216         spin_unlock(&dev_entry->userptr_spinlock);
217
218         if (!first)
219                 seq_puts(s, "\n");
220
221         return 0;
222 }
223
224 static int vm_show(struct seq_file *s, void *data)
225 {
226         struct hl_debugfs_entry *entry = s->private;
227         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
228         struct hl_ctx *ctx;
229         struct hl_vm *vm;
230         struct hl_vm_hash_node *hnode;
231         struct hl_userptr *userptr;
232         struct hl_vm_phys_pg_pack *phys_pg_pack = NULL;
233         enum vm_type_t *vm_type;
234         bool once = true;
235         u64 j;
236         int i;
237
238         if (!dev_entry->hdev->mmu_enable)
239                 return 0;
240
241         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
242
243         list_for_each_entry(ctx, &dev_entry->ctx_mem_hash_list, debugfs_list) {
244                 once = false;
245                 seq_puts(s, "\n\n----------------------------------------------------");
246                 seq_puts(s, "\n----------------------------------------------------\n\n");
247                 seq_printf(s, "ctx asid: %u\n", ctx->asid);
248
249                 seq_puts(s, "\nmappings:\n\n");
250                 seq_puts(s, "    virtual address        size          handle\n");
251                 seq_puts(s, "----------------------------------------------------\n");
252                 mutex_lock(&ctx->mem_hash_lock);
253                 hash_for_each(ctx->mem_hash, i, hnode, node) {
254                         vm_type = hnode->ptr;
255
256                         if (*vm_type == VM_TYPE_USERPTR) {
257                                 userptr = hnode->ptr;
258                                 seq_printf(s,
259                                         "    0x%-14llx      %-10u\n",
260                                         hnode->vaddr, userptr->size);
261                         } else {
262                                 phys_pg_pack = hnode->ptr;
263                                 seq_printf(s,
264                                         "    0x%-14llx      %-10llu       %-4u\n",
265                                         hnode->vaddr, phys_pg_pack->total_size,
266                                         phys_pg_pack->handle);
267                         }
268                 }
269                 mutex_unlock(&ctx->mem_hash_lock);
270
271                 vm = &ctx->hdev->vm;
272                 spin_lock(&vm->idr_lock);
273
274                 if (!idr_is_empty(&vm->phys_pg_pack_handles))
275                         seq_puts(s, "\n\nallocations:\n");
276
277                 idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_pack, i) {
278                         if (phys_pg_pack->asid != ctx->asid)
279                                 continue;
280
281                         seq_printf(s, "\nhandle: %u\n", phys_pg_pack->handle);
282                         seq_printf(s, "page size: %u\n\n",
283                                                 phys_pg_pack->page_size);
284                         seq_puts(s, "   physical address\n");
285                         seq_puts(s, "---------------------\n");
286                         for (j = 0 ; j < phys_pg_pack->npages ; j++) {
287                                 seq_printf(s, "    0x%-14llx\n",
288                                                 phys_pg_pack->pages[j]);
289                         }
290                 }
291                 spin_unlock(&vm->idr_lock);
292
293         }
294
295         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
296
297         if (!once)
298                 seq_puts(s, "\n");
299
300         return 0;
301 }
302
303 /* these inline functions are copied from mmu.c */
304 static inline u64 get_hop0_addr(struct hl_ctx *ctx)
305 {
306         return ctx->hdev->asic_prop.mmu_pgt_addr +
307                         (ctx->asid * ctx->hdev->asic_prop.mmu_hop_table_size);
308 }
309
310 static inline u64 get_hop0_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
311                 u64 virt_addr)
312 {
313         return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
314                         ((virt_addr & HOP0_MASK) >> HOP0_SHIFT);
315 }
316
317 static inline u64 get_hop1_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
318                 u64 virt_addr)
319 {
320         return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
321                         ((virt_addr & HOP1_MASK) >> HOP1_SHIFT);
322 }
323
324 static inline u64 get_hop2_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
325                 u64 virt_addr)
326 {
327         return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
328                         ((virt_addr & HOP2_MASK) >> HOP2_SHIFT);
329 }
330
331 static inline u64 get_hop3_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
332                 u64 virt_addr)
333 {
334         return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
335                         ((virt_addr & HOP3_MASK) >> HOP3_SHIFT);
336 }
337
338 static inline u64 get_hop4_pte_addr(struct hl_ctx *ctx, u64 hop_addr,
339                 u64 virt_addr)
340 {
341         return hop_addr + ctx->hdev->asic_prop.mmu_pte_size *
342                         ((virt_addr & HOP4_MASK) >> HOP4_SHIFT);
343 }
344
345 static inline u64 get_next_hop_addr(u64 curr_pte)
346 {
347         if (curr_pte & PAGE_PRESENT_MASK)
348                 return curr_pte & PHYS_ADDR_MASK;
349         else
350                 return ULLONG_MAX;
351 }
352
353 static int mmu_show(struct seq_file *s, void *data)
354 {
355         struct hl_debugfs_entry *entry = s->private;
356         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
357         struct hl_device *hdev = dev_entry->hdev;
358         struct hl_ctx *ctx = hdev->user_ctx;
359
360         u64 hop0_addr = 0, hop0_pte_addr = 0, hop0_pte = 0,
361                 hop1_addr = 0, hop1_pte_addr = 0, hop1_pte = 0,
362                 hop2_addr = 0, hop2_pte_addr = 0, hop2_pte = 0,
363                 hop3_addr = 0, hop3_pte_addr = 0, hop3_pte = 0,
364                 hop4_addr = 0, hop4_pte_addr = 0, hop4_pte = 0,
365                 virt_addr = dev_entry->mmu_addr;
366
367         if (!hdev->mmu_enable)
368                 return 0;
369
370         if (!ctx) {
371                 dev_err(hdev->dev, "no ctx available\n");
372                 return 0;
373         }
374
375         mutex_lock(&ctx->mmu_lock);
376
377         /* the following lookup is copied from unmap() in mmu.c */
378
379         hop0_addr = get_hop0_addr(ctx);
380         hop0_pte_addr = get_hop0_pte_addr(ctx, hop0_addr, virt_addr);
381         hop0_pte = hdev->asic_funcs->read_pte(hdev, hop0_pte_addr);
382         hop1_addr = get_next_hop_addr(hop0_pte);
383
384         if (hop1_addr == ULLONG_MAX)
385                 goto not_mapped;
386
387         hop1_pte_addr = get_hop1_pte_addr(ctx, hop1_addr, virt_addr);
388         hop1_pte = hdev->asic_funcs->read_pte(hdev, hop1_pte_addr);
389         hop2_addr = get_next_hop_addr(hop1_pte);
390
391         if (hop2_addr == ULLONG_MAX)
392                 goto not_mapped;
393
394         hop2_pte_addr = get_hop2_pte_addr(ctx, hop2_addr, virt_addr);
395         hop2_pte = hdev->asic_funcs->read_pte(hdev, hop2_pte_addr);
396         hop3_addr = get_next_hop_addr(hop2_pte);
397
398         if (hop3_addr == ULLONG_MAX)
399                 goto not_mapped;
400
401         hop3_pte_addr = get_hop3_pte_addr(ctx, hop3_addr, virt_addr);
402         hop3_pte = hdev->asic_funcs->read_pte(hdev, hop3_pte_addr);
403
404         if (!(hop3_pte & LAST_MASK)) {
405                 hop4_addr = get_next_hop_addr(hop3_pte);
406
407                 if (hop4_addr == ULLONG_MAX)
408                         goto not_mapped;
409
410                 hop4_pte_addr = get_hop4_pte_addr(ctx, hop4_addr, virt_addr);
411                 hop4_pte = hdev->asic_funcs->read_pte(hdev, hop4_pte_addr);
412                 if (!(hop4_pte & PAGE_PRESENT_MASK))
413                         goto not_mapped;
414         } else {
415                 if (!(hop3_pte & PAGE_PRESENT_MASK))
416                         goto not_mapped;
417         }
418
419         seq_printf(s, "asid: %u, virt_addr: 0x%llx\n",
420                         dev_entry->mmu_asid, dev_entry->mmu_addr);
421
422         seq_printf(s, "hop0_addr: 0x%llx\n", hop0_addr);
423         seq_printf(s, "hop0_pte_addr: 0x%llx\n", hop0_pte_addr);
424         seq_printf(s, "hop0_pte: 0x%llx\n", hop0_pte);
425
426         seq_printf(s, "hop1_addr: 0x%llx\n", hop1_addr);
427         seq_printf(s, "hop1_pte_addr: 0x%llx\n", hop1_pte_addr);
428         seq_printf(s, "hop1_pte: 0x%llx\n", hop1_pte);
429
430         seq_printf(s, "hop2_addr: 0x%llx\n", hop2_addr);
431         seq_printf(s, "hop2_pte_addr: 0x%llx\n", hop2_pte_addr);
432         seq_printf(s, "hop2_pte: 0x%llx\n", hop2_pte);
433
434         seq_printf(s, "hop3_addr: 0x%llx\n", hop3_addr);
435         seq_printf(s, "hop3_pte_addr: 0x%llx\n", hop3_pte_addr);
436         seq_printf(s, "hop3_pte: 0x%llx\n", hop3_pte);
437
438         if (!(hop3_pte & LAST_MASK)) {
439                 seq_printf(s, "hop4_addr: 0x%llx\n", hop4_addr);
440                 seq_printf(s, "hop4_pte_addr: 0x%llx\n", hop4_pte_addr);
441                 seq_printf(s, "hop4_pte: 0x%llx\n", hop4_pte);
442         }
443
444         goto out;
445
446 not_mapped:
447         dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
448                         virt_addr);
449 out:
450         mutex_unlock(&ctx->mmu_lock);
451
452         return 0;
453 }
454
455 static ssize_t mmu_write(struct file *file, const char __user *buf,
456                 size_t count, loff_t *f_pos)
457 {
458         struct seq_file *s = file->private_data;
459         struct hl_debugfs_entry *entry = s->private;
460         struct hl_dbg_device_entry *dev_entry = entry->dev_entry;
461         struct hl_device *hdev = dev_entry->hdev;
462         char kbuf[MMU_KBUF_SIZE];
463         char *c;
464         ssize_t rc;
465
466         if (!hdev->mmu_enable)
467                 return count;
468
469         if (count > sizeof(kbuf) - 1)
470                 goto err;
471         if (copy_from_user(kbuf, buf, count))
472                 goto err;
473         kbuf[count] = 0;
474
475         c = strchr(kbuf, ' ');
476         if (!c)
477                 goto err;
478         *c = '\0';
479
480         rc = kstrtouint(kbuf, 10, &dev_entry->mmu_asid);
481         if (rc)
482                 goto err;
483
484         if (strncmp(c+1, "0x", 2))
485                 goto err;
486         rc = kstrtoull(c+3, 16, &dev_entry->mmu_addr);
487         if (rc)
488                 goto err;
489
490         return count;
491
492 err:
493         dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
494
495         return -EINVAL;
496 }
497
498 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr,
499                                 u64 *phys_addr)
500 {
501         struct hl_ctx *ctx = hdev->user_ctx;
502         u64 hop_addr, hop_pte_addr, hop_pte;
503         int rc = 0;
504
505         if (!ctx) {
506                 dev_err(hdev->dev, "no ctx available\n");
507                 return -EINVAL;
508         }
509
510         mutex_lock(&ctx->mmu_lock);
511
512         /* hop 0 */
513         hop_addr = get_hop0_addr(ctx);
514         hop_pte_addr = get_hop0_pte_addr(ctx, hop_addr, virt_addr);
515         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
516
517         /* hop 1 */
518         hop_addr = get_next_hop_addr(hop_pte);
519         if (hop_addr == ULLONG_MAX)
520                 goto not_mapped;
521         hop_pte_addr = get_hop1_pte_addr(ctx, hop_addr, virt_addr);
522         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
523
524         /* hop 2 */
525         hop_addr = get_next_hop_addr(hop_pte);
526         if (hop_addr == ULLONG_MAX)
527                 goto not_mapped;
528         hop_pte_addr = get_hop2_pte_addr(ctx, hop_addr, virt_addr);
529         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
530
531         /* hop 3 */
532         hop_addr = get_next_hop_addr(hop_pte);
533         if (hop_addr == ULLONG_MAX)
534                 goto not_mapped;
535         hop_pte_addr = get_hop3_pte_addr(ctx, hop_addr, virt_addr);
536         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
537
538         if (!(hop_pte & LAST_MASK)) {
539                 /* hop 4 */
540                 hop_addr = get_next_hop_addr(hop_pte);
541                 if (hop_addr == ULLONG_MAX)
542                         goto not_mapped;
543                 hop_pte_addr = get_hop4_pte_addr(ctx, hop_addr, virt_addr);
544                 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
545         }
546
547         if (!(hop_pte & PAGE_PRESENT_MASK))
548                 goto not_mapped;
549
550         *phys_addr = (hop_pte & PTE_PHYS_ADDR_MASK) | (virt_addr & OFFSET_MASK);
551
552         goto out;
553
554 not_mapped:
555         dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
556                         virt_addr);
557         rc = -EINVAL;
558 out:
559         mutex_unlock(&ctx->mmu_lock);
560         return rc;
561 }
562
563 static ssize_t hl_data_read32(struct file *f, char __user *buf,
564                                         size_t count, loff_t *ppos)
565 {
566         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
567         struct hl_device *hdev = entry->hdev;
568         struct asic_fixed_properties *prop = &hdev->asic_prop;
569         char tmp_buf[32];
570         u64 addr = entry->addr;
571         u32 val;
572         ssize_t rc;
573
574         if (*ppos)
575                 return 0;
576
577         if (addr >= prop->va_space_dram_start_address &&
578                         addr < prop->va_space_dram_end_address &&
579                         hdev->mmu_enable &&
580                         hdev->dram_supports_virtual_memory) {
581                 rc = device_va_to_pa(hdev, entry->addr, &addr);
582                 if (rc)
583                         return rc;
584         }
585
586         rc = hdev->asic_funcs->debugfs_read32(hdev, addr, &val);
587         if (rc) {
588                 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
589                 return rc;
590         }
591
592         sprintf(tmp_buf, "0x%08x\n", val);
593         return simple_read_from_buffer(buf, count, ppos, tmp_buf,
594                         strlen(tmp_buf));
595 }
596
597 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
598                                         size_t count, loff_t *ppos)
599 {
600         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
601         struct hl_device *hdev = entry->hdev;
602         struct asic_fixed_properties *prop = &hdev->asic_prop;
603         u64 addr = entry->addr;
604         u32 value;
605         ssize_t rc;
606
607         rc = kstrtouint_from_user(buf, count, 16, &value);
608         if (rc)
609                 return rc;
610
611         if (addr >= prop->va_space_dram_start_address &&
612                         addr < prop->va_space_dram_end_address &&
613                         hdev->mmu_enable &&
614                         hdev->dram_supports_virtual_memory) {
615                 rc = device_va_to_pa(hdev, entry->addr, &addr);
616                 if (rc)
617                         return rc;
618         }
619
620         rc = hdev->asic_funcs->debugfs_write32(hdev, addr, value);
621         if (rc) {
622                 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
623                         value, addr);
624                 return rc;
625         }
626
627         return count;
628 }
629
630 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
631                 size_t count, loff_t *ppos)
632 {
633         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
634         struct hl_device *hdev = entry->hdev;
635         char tmp_buf[200];
636         int i;
637
638         if (*ppos)
639                 return 0;
640
641         if (hdev->pdev->current_state == PCI_D0)
642                 i = 1;
643         else if (hdev->pdev->current_state == PCI_D3hot)
644                 i = 2;
645         else
646                 i = 3;
647
648         sprintf(tmp_buf,
649                 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
650         return simple_read_from_buffer(buf, count, ppos, tmp_buf,
651                         strlen(tmp_buf));
652 }
653
654 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
655                                         size_t count, loff_t *ppos)
656 {
657         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
658         struct hl_device *hdev = entry->hdev;
659         u32 value;
660         ssize_t rc;
661
662         rc = kstrtouint_from_user(buf, count, 10, &value);
663         if (rc)
664                 return rc;
665
666         if (value == 1) {
667                 pci_set_power_state(hdev->pdev, PCI_D0);
668                 pci_restore_state(hdev->pdev);
669                 rc = pci_enable_device(hdev->pdev);
670         } else if (value == 2) {
671                 pci_save_state(hdev->pdev);
672                 pci_disable_device(hdev->pdev);
673                 pci_set_power_state(hdev->pdev, PCI_D3hot);
674         } else {
675                 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
676                 return -EINVAL;
677         }
678
679         return count;
680 }
681
682 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
683                                         size_t count, loff_t *ppos)
684 {
685         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
686         struct hl_device *hdev = entry->hdev;
687         char tmp_buf[32];
688         u32 val;
689         ssize_t rc;
690
691         if (*ppos)
692                 return 0;
693
694         rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
695                         entry->i2c_reg, &val);
696         if (rc) {
697                 dev_err(hdev->dev,
698                         "Failed to read from I2C bus %d, addr %d, reg %d\n",
699                         entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
700                 return rc;
701         }
702
703         sprintf(tmp_buf, "0x%02x\n", val);
704         rc = simple_read_from_buffer(buf, count, ppos, tmp_buf,
705                         strlen(tmp_buf));
706
707         return rc;
708 }
709
710 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
711                                         size_t count, loff_t *ppos)
712 {
713         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
714         struct hl_device *hdev = entry->hdev;
715         u32 value;
716         ssize_t rc;
717
718         rc = kstrtouint_from_user(buf, count, 16, &value);
719         if (rc)
720                 return rc;
721
722         rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
723                         entry->i2c_reg, value);
724         if (rc) {
725                 dev_err(hdev->dev,
726                         "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
727                         value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
728                 return rc;
729         }
730
731         return count;
732 }
733
734 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
735                                         size_t count, loff_t *ppos)
736 {
737         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
738         struct hl_device *hdev = entry->hdev;
739         u32 value;
740         ssize_t rc;
741
742         rc = kstrtouint_from_user(buf, count, 10, &value);
743         if (rc)
744                 return rc;
745
746         value = value ? 1 : 0;
747
748         hl_debugfs_led_set(hdev, 0, value);
749
750         return count;
751 }
752
753 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
754                                         size_t count, loff_t *ppos)
755 {
756         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
757         struct hl_device *hdev = entry->hdev;
758         u32 value;
759         ssize_t rc;
760
761         rc = kstrtouint_from_user(buf, count, 10, &value);
762         if (rc)
763                 return rc;
764
765         value = value ? 1 : 0;
766
767         hl_debugfs_led_set(hdev, 1, value);
768
769         return count;
770 }
771
772 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
773                                         size_t count, loff_t *ppos)
774 {
775         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
776         struct hl_device *hdev = entry->hdev;
777         u32 value;
778         ssize_t rc;
779
780         rc = kstrtouint_from_user(buf, count, 10, &value);
781         if (rc)
782                 return rc;
783
784         value = value ? 1 : 0;
785
786         hl_debugfs_led_set(hdev, 2, value);
787
788         return count;
789 }
790
791 static ssize_t hl_device_read(struct file *f, char __user *buf,
792                                         size_t count, loff_t *ppos)
793 {
794         static const char *help =
795                 "Valid values: disable, enable, suspend, resume, cpu_timeout\n";
796         return simple_read_from_buffer(buf, count, ppos, help, strlen(help));
797 }
798
799 static ssize_t hl_device_write(struct file *f, const char __user *buf,
800                                      size_t count, loff_t *ppos)
801 {
802         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
803         struct hl_device *hdev = entry->hdev;
804         char data[30] = {0};
805
806         /* don't allow partial writes */
807         if (*ppos != 0)
808                 return 0;
809
810         simple_write_to_buffer(data, 29, ppos, buf, count);
811
812         if (strncmp("disable", data, strlen("disable")) == 0) {
813                 hdev->disabled = true;
814         } else if (strncmp("enable", data, strlen("enable")) == 0) {
815                 hdev->disabled = false;
816         } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
817                 hdev->asic_funcs->suspend(hdev);
818         } else if (strncmp("resume", data, strlen("resume")) == 0) {
819                 hdev->asic_funcs->resume(hdev);
820         } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
821                 hdev->device_cpu_disabled = true;
822         } else {
823                 dev_err(hdev->dev,
824                         "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
825                 count = -EINVAL;
826         }
827
828         return count;
829 }
830
831 static const struct file_operations hl_data32b_fops = {
832         .owner = THIS_MODULE,
833         .read = hl_data_read32,
834         .write = hl_data_write32
835 };
836
837 static const struct file_operations hl_i2c_data_fops = {
838         .owner = THIS_MODULE,
839         .read = hl_i2c_data_read,
840         .write = hl_i2c_data_write
841 };
842
843 static const struct file_operations hl_power_fops = {
844         .owner = THIS_MODULE,
845         .read = hl_get_power_state,
846         .write = hl_set_power_state
847 };
848
849 static const struct file_operations hl_led0_fops = {
850         .owner = THIS_MODULE,
851         .write = hl_led0_write
852 };
853
854 static const struct file_operations hl_led1_fops = {
855         .owner = THIS_MODULE,
856         .write = hl_led1_write
857 };
858
859 static const struct file_operations hl_led2_fops = {
860         .owner = THIS_MODULE,
861         .write = hl_led2_write
862 };
863
864 static const struct file_operations hl_device_fops = {
865         .owner = THIS_MODULE,
866         .read = hl_device_read,
867         .write = hl_device_write
868 };
869
870 static const struct hl_info_list hl_debugfs_list[] = {
871         {"command_buffers", command_buffers_show, NULL},
872         {"command_submission", command_submission_show, NULL},
873         {"command_submission_jobs", command_submission_jobs_show, NULL},
874         {"userptr", userptr_show, NULL},
875         {"vm", vm_show, NULL},
876         {"mmu", mmu_show, mmu_write},
877 };
878
879 static int hl_debugfs_open(struct inode *inode, struct file *file)
880 {
881         struct hl_debugfs_entry *node = inode->i_private;
882
883         return single_open(file, node->info_ent->show, node);
884 }
885
886 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
887                 size_t count, loff_t *f_pos)
888 {
889         struct hl_debugfs_entry *node = file->f_inode->i_private;
890
891         if (node->info_ent->write)
892                 return node->info_ent->write(file, buf, count, f_pos);
893         else
894                 return -EINVAL;
895
896 }
897
898 static const struct file_operations hl_debugfs_fops = {
899         .owner = THIS_MODULE,
900         .open = hl_debugfs_open,
901         .read = seq_read,
902         .write = hl_debugfs_write,
903         .llseek = seq_lseek,
904         .release = single_release,
905 };
906
907 void hl_debugfs_add_device(struct hl_device *hdev)
908 {
909         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
910         int count = ARRAY_SIZE(hl_debugfs_list);
911         struct hl_debugfs_entry *entry;
912         struct dentry *ent;
913         int i;
914
915         dev_entry->hdev = hdev;
916         dev_entry->entry_arr = kmalloc_array(count,
917                                         sizeof(struct hl_debugfs_entry),
918                                         GFP_KERNEL);
919         if (!dev_entry->entry_arr)
920                 return;
921
922         INIT_LIST_HEAD(&dev_entry->file_list);
923         INIT_LIST_HEAD(&dev_entry->cb_list);
924         INIT_LIST_HEAD(&dev_entry->cs_list);
925         INIT_LIST_HEAD(&dev_entry->cs_job_list);
926         INIT_LIST_HEAD(&dev_entry->userptr_list);
927         INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
928         mutex_init(&dev_entry->file_mutex);
929         spin_lock_init(&dev_entry->cb_spinlock);
930         spin_lock_init(&dev_entry->cs_spinlock);
931         spin_lock_init(&dev_entry->cs_job_spinlock);
932         spin_lock_init(&dev_entry->userptr_spinlock);
933         spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
934
935         dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
936                                                 hl_debug_root);
937
938         debugfs_create_x64("addr",
939                                 0644,
940                                 dev_entry->root,
941                                 &dev_entry->addr);
942
943         debugfs_create_file("data32",
944                                 0644,
945                                 dev_entry->root,
946                                 dev_entry,
947                                 &hl_data32b_fops);
948
949         debugfs_create_file("set_power_state",
950                                 0200,
951                                 dev_entry->root,
952                                 dev_entry,
953                                 &hl_power_fops);
954
955         debugfs_create_u8("i2c_bus",
956                                 0644,
957                                 dev_entry->root,
958                                 &dev_entry->i2c_bus);
959
960         debugfs_create_u8("i2c_addr",
961                                 0644,
962                                 dev_entry->root,
963                                 &dev_entry->i2c_addr);
964
965         debugfs_create_u8("i2c_reg",
966                                 0644,
967                                 dev_entry->root,
968                                 &dev_entry->i2c_reg);
969
970         debugfs_create_file("i2c_data",
971                                 0644,
972                                 dev_entry->root,
973                                 dev_entry,
974                                 &hl_i2c_data_fops);
975
976         debugfs_create_file("led0",
977                                 0200,
978                                 dev_entry->root,
979                                 dev_entry,
980                                 &hl_led0_fops);
981
982         debugfs_create_file("led1",
983                                 0200,
984                                 dev_entry->root,
985                                 dev_entry,
986                                 &hl_led1_fops);
987
988         debugfs_create_file("led2",
989                                 0200,
990                                 dev_entry->root,
991                                 dev_entry,
992                                 &hl_led2_fops);
993
994         debugfs_create_file("device",
995                                 0200,
996                                 dev_entry->root,
997                                 dev_entry,
998                                 &hl_device_fops);
999
1000         for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1001
1002                 ent = debugfs_create_file(hl_debugfs_list[i].name,
1003                                         0444,
1004                                         dev_entry->root,
1005                                         entry,
1006                                         &hl_debugfs_fops);
1007                 entry->dent = ent;
1008                 entry->info_ent = &hl_debugfs_list[i];
1009                 entry->dev_entry = dev_entry;
1010         }
1011 }
1012
1013 void hl_debugfs_remove_device(struct hl_device *hdev)
1014 {
1015         struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1016
1017         debugfs_remove_recursive(entry->root);
1018
1019         mutex_destroy(&entry->file_mutex);
1020         kfree(entry->entry_arr);
1021 }
1022
1023 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1024 {
1025         struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1026
1027         mutex_lock(&dev_entry->file_mutex);
1028         list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1029         mutex_unlock(&dev_entry->file_mutex);
1030 }
1031
1032 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1033 {
1034         struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1035
1036         mutex_lock(&dev_entry->file_mutex);
1037         list_del(&hpriv->debugfs_list);
1038         mutex_unlock(&dev_entry->file_mutex);
1039 }
1040
1041 void hl_debugfs_add_cb(struct hl_cb *cb)
1042 {
1043         struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1044
1045         spin_lock(&dev_entry->cb_spinlock);
1046         list_add(&cb->debugfs_list, &dev_entry->cb_list);
1047         spin_unlock(&dev_entry->cb_spinlock);
1048 }
1049
1050 void hl_debugfs_remove_cb(struct hl_cb *cb)
1051 {
1052         struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1053
1054         spin_lock(&dev_entry->cb_spinlock);
1055         list_del(&cb->debugfs_list);
1056         spin_unlock(&dev_entry->cb_spinlock);
1057 }
1058
1059 void hl_debugfs_add_cs(struct hl_cs *cs)
1060 {
1061         struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1062
1063         spin_lock(&dev_entry->cs_spinlock);
1064         list_add(&cs->debugfs_list, &dev_entry->cs_list);
1065         spin_unlock(&dev_entry->cs_spinlock);
1066 }
1067
1068 void hl_debugfs_remove_cs(struct hl_cs *cs)
1069 {
1070         struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1071
1072         spin_lock(&dev_entry->cs_spinlock);
1073         list_del(&cs->debugfs_list);
1074         spin_unlock(&dev_entry->cs_spinlock);
1075 }
1076
1077 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1078 {
1079         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1080
1081         spin_lock(&dev_entry->cs_job_spinlock);
1082         list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1083         spin_unlock(&dev_entry->cs_job_spinlock);
1084 }
1085
1086 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1087 {
1088         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1089
1090         spin_lock(&dev_entry->cs_job_spinlock);
1091         list_del(&job->debugfs_list);
1092         spin_unlock(&dev_entry->cs_job_spinlock);
1093 }
1094
1095 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1096 {
1097         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1098
1099         spin_lock(&dev_entry->userptr_spinlock);
1100         list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1101         spin_unlock(&dev_entry->userptr_spinlock);
1102 }
1103
1104 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1105                                 struct hl_userptr *userptr)
1106 {
1107         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1108
1109         spin_lock(&dev_entry->userptr_spinlock);
1110         list_del(&userptr->debugfs_list);
1111         spin_unlock(&dev_entry->userptr_spinlock);
1112 }
1113
1114 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1115 {
1116         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1117
1118         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1119         list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1120         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1121 }
1122
1123 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1124 {
1125         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1126
1127         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1128         list_del(&ctx->debugfs_list);
1129         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1130 }
1131
1132 void __init hl_debugfs_init(void)
1133 {
1134         hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1135 }
1136
1137 void hl_debugfs_fini(void)
1138 {
1139         debugfs_remove_recursive(hl_debug_root);
1140 }