]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/misc/habanalabs/debugfs.c
Merge tag 's390-5.2-2' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[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], asid_kbuf[MMU_ASID_BUF_SIZE],
463                 addr_kbuf[MMU_ADDR_BUF_SIZE];
464         char *c;
465         ssize_t rc;
466
467         if (!hdev->mmu_enable)
468                 return count;
469
470         memset(kbuf, 0, sizeof(kbuf));
471         memset(asid_kbuf, 0, sizeof(asid_kbuf));
472         memset(addr_kbuf, 0, sizeof(addr_kbuf));
473
474         if (copy_from_user(kbuf, buf, count))
475                 goto err;
476
477         kbuf[MMU_KBUF_SIZE - 1] = 0;
478
479         c = strchr(kbuf, ' ');
480         if (!c)
481                 goto err;
482
483         memcpy(asid_kbuf, kbuf, c - kbuf);
484
485         rc = kstrtouint(asid_kbuf, 10, &dev_entry->mmu_asid);
486         if (rc)
487                 goto err;
488
489         c = strstr(kbuf, " 0x");
490         if (!c)
491                 goto err;
492
493         c += 3;
494         memcpy(addr_kbuf, c, (kbuf + count) - c);
495
496         rc = kstrtoull(addr_kbuf, 16, &dev_entry->mmu_addr);
497         if (rc)
498                 goto err;
499
500         return count;
501
502 err:
503         dev_err(hdev->dev, "usage: echo <asid> <0xaddr> > mmu\n");
504
505         return -EINVAL;
506 }
507
508 static int device_va_to_pa(struct hl_device *hdev, u64 virt_addr,
509                                 u64 *phys_addr)
510 {
511         struct hl_ctx *ctx = hdev->user_ctx;
512         u64 hop_addr, hop_pte_addr, hop_pte;
513         int rc = 0;
514
515         if (!ctx) {
516                 dev_err(hdev->dev, "no ctx available\n");
517                 return -EINVAL;
518         }
519
520         mutex_lock(&ctx->mmu_lock);
521
522         /* hop 0 */
523         hop_addr = get_hop0_addr(ctx);
524         hop_pte_addr = get_hop0_pte_addr(ctx, hop_addr, virt_addr);
525         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
526
527         /* hop 1 */
528         hop_addr = get_next_hop_addr(hop_pte);
529         if (hop_addr == ULLONG_MAX)
530                 goto not_mapped;
531         hop_pte_addr = get_hop1_pte_addr(ctx, hop_addr, virt_addr);
532         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
533
534         /* hop 2 */
535         hop_addr = get_next_hop_addr(hop_pte);
536         if (hop_addr == ULLONG_MAX)
537                 goto not_mapped;
538         hop_pte_addr = get_hop2_pte_addr(ctx, hop_addr, virt_addr);
539         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
540
541         /* hop 3 */
542         hop_addr = get_next_hop_addr(hop_pte);
543         if (hop_addr == ULLONG_MAX)
544                 goto not_mapped;
545         hop_pte_addr = get_hop3_pte_addr(ctx, hop_addr, virt_addr);
546         hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
547
548         if (!(hop_pte & LAST_MASK)) {
549                 /* hop 4 */
550                 hop_addr = get_next_hop_addr(hop_pte);
551                 if (hop_addr == ULLONG_MAX)
552                         goto not_mapped;
553                 hop_pte_addr = get_hop4_pte_addr(ctx, hop_addr, virt_addr);
554                 hop_pte = hdev->asic_funcs->read_pte(hdev, hop_pte_addr);
555         }
556
557         if (!(hop_pte & PAGE_PRESENT_MASK))
558                 goto not_mapped;
559
560         *phys_addr = (hop_pte & PTE_PHYS_ADDR_MASK) | (virt_addr & OFFSET_MASK);
561
562         goto out;
563
564 not_mapped:
565         dev_err(hdev->dev, "virt addr 0x%llx is not mapped to phys addr\n",
566                         virt_addr);
567         rc = -EINVAL;
568 out:
569         mutex_unlock(&ctx->mmu_lock);
570         return rc;
571 }
572
573 static ssize_t hl_data_read32(struct file *f, char __user *buf,
574                                         size_t count, loff_t *ppos)
575 {
576         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
577         struct hl_device *hdev = entry->hdev;
578         struct asic_fixed_properties *prop = &hdev->asic_prop;
579         char tmp_buf[32];
580         u64 addr = entry->addr;
581         u32 val;
582         ssize_t rc;
583
584         if (*ppos)
585                 return 0;
586
587         if (addr >= prop->va_space_dram_start_address &&
588                         addr < prop->va_space_dram_end_address &&
589                         hdev->mmu_enable &&
590                         hdev->dram_supports_virtual_memory) {
591                 rc = device_va_to_pa(hdev, entry->addr, &addr);
592                 if (rc)
593                         return rc;
594         }
595
596         rc = hdev->asic_funcs->debugfs_read32(hdev, addr, &val);
597         if (rc) {
598                 dev_err(hdev->dev, "Failed to read from 0x%010llx\n", addr);
599                 return rc;
600         }
601
602         sprintf(tmp_buf, "0x%08x\n", val);
603         rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
604                         strlen(tmp_buf) + 1);
605
606         return rc;
607 }
608
609 static ssize_t hl_data_write32(struct file *f, const char __user *buf,
610                                         size_t count, loff_t *ppos)
611 {
612         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
613         struct hl_device *hdev = entry->hdev;
614         struct asic_fixed_properties *prop = &hdev->asic_prop;
615         u64 addr = entry->addr;
616         u32 value;
617         ssize_t rc;
618
619         rc = kstrtouint_from_user(buf, count, 16, &value);
620         if (rc)
621                 return rc;
622
623         if (addr >= prop->va_space_dram_start_address &&
624                         addr < prop->va_space_dram_end_address &&
625                         hdev->mmu_enable &&
626                         hdev->dram_supports_virtual_memory) {
627                 rc = device_va_to_pa(hdev, entry->addr, &addr);
628                 if (rc)
629                         return rc;
630         }
631
632         rc = hdev->asic_funcs->debugfs_write32(hdev, addr, value);
633         if (rc) {
634                 dev_err(hdev->dev, "Failed to write 0x%08x to 0x%010llx\n",
635                         value, addr);
636                 return rc;
637         }
638
639         return count;
640 }
641
642 static ssize_t hl_get_power_state(struct file *f, char __user *buf,
643                 size_t count, loff_t *ppos)
644 {
645         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
646         struct hl_device *hdev = entry->hdev;
647         char tmp_buf[200];
648         ssize_t rc;
649         int i;
650
651         if (*ppos)
652                 return 0;
653
654         if (hdev->pdev->current_state == PCI_D0)
655                 i = 1;
656         else if (hdev->pdev->current_state == PCI_D3hot)
657                 i = 2;
658         else
659                 i = 3;
660
661         sprintf(tmp_buf,
662                 "current power state: %d\n1 - D0\n2 - D3hot\n3 - Unknown\n", i);
663         rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
664                         strlen(tmp_buf) + 1);
665
666         return rc;
667 }
668
669 static ssize_t hl_set_power_state(struct file *f, const char __user *buf,
670                                         size_t count, loff_t *ppos)
671 {
672         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
673         struct hl_device *hdev = entry->hdev;
674         u32 value;
675         ssize_t rc;
676
677         rc = kstrtouint_from_user(buf, count, 10, &value);
678         if (rc)
679                 return rc;
680
681         if (value == 1) {
682                 pci_set_power_state(hdev->pdev, PCI_D0);
683                 pci_restore_state(hdev->pdev);
684                 rc = pci_enable_device(hdev->pdev);
685         } else if (value == 2) {
686                 pci_save_state(hdev->pdev);
687                 pci_disable_device(hdev->pdev);
688                 pci_set_power_state(hdev->pdev, PCI_D3hot);
689         } else {
690                 dev_dbg(hdev->dev, "invalid power state value %u\n", value);
691                 return -EINVAL;
692         }
693
694         return count;
695 }
696
697 static ssize_t hl_i2c_data_read(struct file *f, char __user *buf,
698                                         size_t count, loff_t *ppos)
699 {
700         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
701         struct hl_device *hdev = entry->hdev;
702         char tmp_buf[32];
703         u32 val;
704         ssize_t rc;
705
706         if (*ppos)
707                 return 0;
708
709         rc = hl_debugfs_i2c_read(hdev, entry->i2c_bus, entry->i2c_addr,
710                         entry->i2c_reg, &val);
711         if (rc) {
712                 dev_err(hdev->dev,
713                         "Failed to read from I2C bus %d, addr %d, reg %d\n",
714                         entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
715                 return rc;
716         }
717
718         sprintf(tmp_buf, "0x%02x\n", val);
719         rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
720                         strlen(tmp_buf) + 1);
721
722         return rc;
723 }
724
725 static ssize_t hl_i2c_data_write(struct file *f, const char __user *buf,
726                                         size_t count, loff_t *ppos)
727 {
728         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
729         struct hl_device *hdev = entry->hdev;
730         u32 value;
731         ssize_t rc;
732
733         rc = kstrtouint_from_user(buf, count, 16, &value);
734         if (rc)
735                 return rc;
736
737         rc = hl_debugfs_i2c_write(hdev, entry->i2c_bus, entry->i2c_addr,
738                         entry->i2c_reg, value);
739         if (rc) {
740                 dev_err(hdev->dev,
741                         "Failed to write 0x%02x to I2C bus %d, addr %d, reg %d\n",
742                         value, entry->i2c_bus, entry->i2c_addr, entry->i2c_reg);
743                 return rc;
744         }
745
746         return count;
747 }
748
749 static ssize_t hl_led0_write(struct file *f, const char __user *buf,
750                                         size_t count, loff_t *ppos)
751 {
752         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
753         struct hl_device *hdev = entry->hdev;
754         u32 value;
755         ssize_t rc;
756
757         rc = kstrtouint_from_user(buf, count, 10, &value);
758         if (rc)
759                 return rc;
760
761         value = value ? 1 : 0;
762
763         hl_debugfs_led_set(hdev, 0, value);
764
765         return count;
766 }
767
768 static ssize_t hl_led1_write(struct file *f, const char __user *buf,
769                                         size_t count, loff_t *ppos)
770 {
771         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
772         struct hl_device *hdev = entry->hdev;
773         u32 value;
774         ssize_t rc;
775
776         rc = kstrtouint_from_user(buf, count, 10, &value);
777         if (rc)
778                 return rc;
779
780         value = value ? 1 : 0;
781
782         hl_debugfs_led_set(hdev, 1, value);
783
784         return count;
785 }
786
787 static ssize_t hl_led2_write(struct file *f, const char __user *buf,
788                                         size_t count, loff_t *ppos)
789 {
790         struct hl_dbg_device_entry *entry = file_inode(f)->i_private;
791         struct hl_device *hdev = entry->hdev;
792         u32 value;
793         ssize_t rc;
794
795         rc = kstrtouint_from_user(buf, count, 10, &value);
796         if (rc)
797                 return rc;
798
799         value = value ? 1 : 0;
800
801         hl_debugfs_led_set(hdev, 2, value);
802
803         return count;
804 }
805
806 static ssize_t hl_device_read(struct file *f, char __user *buf,
807                                         size_t count, loff_t *ppos)
808 {
809         char tmp_buf[200];
810         ssize_t rc;
811
812         if (*ppos)
813                 return 0;
814
815         sprintf(tmp_buf,
816                 "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
817         rc = simple_read_from_buffer(buf, strlen(tmp_buf) + 1, ppos, tmp_buf,
818                         strlen(tmp_buf) + 1);
819
820         return rc;
821 }
822
823 static ssize_t hl_device_write(struct file *f, const 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 data[30];
829
830         /* don't allow partial writes */
831         if (*ppos != 0)
832                 return 0;
833
834         simple_write_to_buffer(data, 29, ppos, buf, count);
835
836         if (strncmp("disable", data, strlen("disable")) == 0) {
837                 hdev->disabled = true;
838         } else if (strncmp("enable", data, strlen("enable")) == 0) {
839                 hdev->disabled = false;
840         } else if (strncmp("suspend", data, strlen("suspend")) == 0) {
841                 hdev->asic_funcs->suspend(hdev);
842         } else if (strncmp("resume", data, strlen("resume")) == 0) {
843                 hdev->asic_funcs->resume(hdev);
844         } else if (strncmp("cpu_timeout", data, strlen("cpu_timeout")) == 0) {
845                 hdev->device_cpu_disabled = true;
846         } else {
847                 dev_err(hdev->dev,
848                         "Valid values: disable, enable, suspend, resume, cpu_timeout\n");
849                 count = -EINVAL;
850         }
851
852         return count;
853 }
854
855 static const struct file_operations hl_data32b_fops = {
856         .owner = THIS_MODULE,
857         .read = hl_data_read32,
858         .write = hl_data_write32
859 };
860
861 static const struct file_operations hl_i2c_data_fops = {
862         .owner = THIS_MODULE,
863         .read = hl_i2c_data_read,
864         .write = hl_i2c_data_write
865 };
866
867 static const struct file_operations hl_power_fops = {
868         .owner = THIS_MODULE,
869         .read = hl_get_power_state,
870         .write = hl_set_power_state
871 };
872
873 static const struct file_operations hl_led0_fops = {
874         .owner = THIS_MODULE,
875         .write = hl_led0_write
876 };
877
878 static const struct file_operations hl_led1_fops = {
879         .owner = THIS_MODULE,
880         .write = hl_led1_write
881 };
882
883 static const struct file_operations hl_led2_fops = {
884         .owner = THIS_MODULE,
885         .write = hl_led2_write
886 };
887
888 static const struct file_operations hl_device_fops = {
889         .owner = THIS_MODULE,
890         .read = hl_device_read,
891         .write = hl_device_write
892 };
893
894 static const struct hl_info_list hl_debugfs_list[] = {
895         {"command_buffers", command_buffers_show, NULL},
896         {"command_submission", command_submission_show, NULL},
897         {"command_submission_jobs", command_submission_jobs_show, NULL},
898         {"userptr", userptr_show, NULL},
899         {"vm", vm_show, NULL},
900         {"mmu", mmu_show, mmu_write},
901 };
902
903 static int hl_debugfs_open(struct inode *inode, struct file *file)
904 {
905         struct hl_debugfs_entry *node = inode->i_private;
906
907         return single_open(file, node->info_ent->show, node);
908 }
909
910 static ssize_t hl_debugfs_write(struct file *file, const char __user *buf,
911                 size_t count, loff_t *f_pos)
912 {
913         struct hl_debugfs_entry *node = file->f_inode->i_private;
914
915         if (node->info_ent->write)
916                 return node->info_ent->write(file, buf, count, f_pos);
917         else
918                 return -EINVAL;
919
920 }
921
922 static const struct file_operations hl_debugfs_fops = {
923         .owner = THIS_MODULE,
924         .open = hl_debugfs_open,
925         .read = seq_read,
926         .write = hl_debugfs_write,
927         .llseek = seq_lseek,
928         .release = single_release,
929 };
930
931 void hl_debugfs_add_device(struct hl_device *hdev)
932 {
933         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
934         int count = ARRAY_SIZE(hl_debugfs_list);
935         struct hl_debugfs_entry *entry;
936         struct dentry *ent;
937         int i;
938
939         dev_entry->hdev = hdev;
940         dev_entry->entry_arr = kmalloc_array(count,
941                                         sizeof(struct hl_debugfs_entry),
942                                         GFP_KERNEL);
943         if (!dev_entry->entry_arr)
944                 return;
945
946         INIT_LIST_HEAD(&dev_entry->file_list);
947         INIT_LIST_HEAD(&dev_entry->cb_list);
948         INIT_LIST_HEAD(&dev_entry->cs_list);
949         INIT_LIST_HEAD(&dev_entry->cs_job_list);
950         INIT_LIST_HEAD(&dev_entry->userptr_list);
951         INIT_LIST_HEAD(&dev_entry->ctx_mem_hash_list);
952         mutex_init(&dev_entry->file_mutex);
953         spin_lock_init(&dev_entry->cb_spinlock);
954         spin_lock_init(&dev_entry->cs_spinlock);
955         spin_lock_init(&dev_entry->cs_job_spinlock);
956         spin_lock_init(&dev_entry->userptr_spinlock);
957         spin_lock_init(&dev_entry->ctx_mem_hash_spinlock);
958
959         dev_entry->root = debugfs_create_dir(dev_name(hdev->dev),
960                                                 hl_debug_root);
961
962         debugfs_create_x64("addr",
963                                 0644,
964                                 dev_entry->root,
965                                 &dev_entry->addr);
966
967         debugfs_create_file("data32",
968                                 0644,
969                                 dev_entry->root,
970                                 dev_entry,
971                                 &hl_data32b_fops);
972
973         debugfs_create_file("set_power_state",
974                                 0200,
975                                 dev_entry->root,
976                                 dev_entry,
977                                 &hl_power_fops);
978
979         debugfs_create_u8("i2c_bus",
980                                 0644,
981                                 dev_entry->root,
982                                 &dev_entry->i2c_bus);
983
984         debugfs_create_u8("i2c_addr",
985                                 0644,
986                                 dev_entry->root,
987                                 &dev_entry->i2c_addr);
988
989         debugfs_create_u8("i2c_reg",
990                                 0644,
991                                 dev_entry->root,
992                                 &dev_entry->i2c_reg);
993
994         debugfs_create_file("i2c_data",
995                                 0644,
996                                 dev_entry->root,
997                                 dev_entry,
998                                 &hl_i2c_data_fops);
999
1000         debugfs_create_file("led0",
1001                                 0200,
1002                                 dev_entry->root,
1003                                 dev_entry,
1004                                 &hl_led0_fops);
1005
1006         debugfs_create_file("led1",
1007                                 0200,
1008                                 dev_entry->root,
1009                                 dev_entry,
1010                                 &hl_led1_fops);
1011
1012         debugfs_create_file("led2",
1013                                 0200,
1014                                 dev_entry->root,
1015                                 dev_entry,
1016                                 &hl_led2_fops);
1017
1018         debugfs_create_file("device",
1019                                 0200,
1020                                 dev_entry->root,
1021                                 dev_entry,
1022                                 &hl_device_fops);
1023
1024         for (i = 0, entry = dev_entry->entry_arr ; i < count ; i++, entry++) {
1025
1026                 ent = debugfs_create_file(hl_debugfs_list[i].name,
1027                                         0444,
1028                                         dev_entry->root,
1029                                         entry,
1030                                         &hl_debugfs_fops);
1031                 entry->dent = ent;
1032                 entry->info_ent = &hl_debugfs_list[i];
1033                 entry->dev_entry = dev_entry;
1034         }
1035 }
1036
1037 void hl_debugfs_remove_device(struct hl_device *hdev)
1038 {
1039         struct hl_dbg_device_entry *entry = &hdev->hl_debugfs;
1040
1041         debugfs_remove_recursive(entry->root);
1042
1043         mutex_destroy(&entry->file_mutex);
1044         kfree(entry->entry_arr);
1045 }
1046
1047 void hl_debugfs_add_file(struct hl_fpriv *hpriv)
1048 {
1049         struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1050
1051         mutex_lock(&dev_entry->file_mutex);
1052         list_add(&hpriv->debugfs_list, &dev_entry->file_list);
1053         mutex_unlock(&dev_entry->file_mutex);
1054 }
1055
1056 void hl_debugfs_remove_file(struct hl_fpriv *hpriv)
1057 {
1058         struct hl_dbg_device_entry *dev_entry = &hpriv->hdev->hl_debugfs;
1059
1060         mutex_lock(&dev_entry->file_mutex);
1061         list_del(&hpriv->debugfs_list);
1062         mutex_unlock(&dev_entry->file_mutex);
1063 }
1064
1065 void hl_debugfs_add_cb(struct hl_cb *cb)
1066 {
1067         struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1068
1069         spin_lock(&dev_entry->cb_spinlock);
1070         list_add(&cb->debugfs_list, &dev_entry->cb_list);
1071         spin_unlock(&dev_entry->cb_spinlock);
1072 }
1073
1074 void hl_debugfs_remove_cb(struct hl_cb *cb)
1075 {
1076         struct hl_dbg_device_entry *dev_entry = &cb->hdev->hl_debugfs;
1077
1078         spin_lock(&dev_entry->cb_spinlock);
1079         list_del(&cb->debugfs_list);
1080         spin_unlock(&dev_entry->cb_spinlock);
1081 }
1082
1083 void hl_debugfs_add_cs(struct hl_cs *cs)
1084 {
1085         struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1086
1087         spin_lock(&dev_entry->cs_spinlock);
1088         list_add(&cs->debugfs_list, &dev_entry->cs_list);
1089         spin_unlock(&dev_entry->cs_spinlock);
1090 }
1091
1092 void hl_debugfs_remove_cs(struct hl_cs *cs)
1093 {
1094         struct hl_dbg_device_entry *dev_entry = &cs->ctx->hdev->hl_debugfs;
1095
1096         spin_lock(&dev_entry->cs_spinlock);
1097         list_del(&cs->debugfs_list);
1098         spin_unlock(&dev_entry->cs_spinlock);
1099 }
1100
1101 void hl_debugfs_add_job(struct hl_device *hdev, struct hl_cs_job *job)
1102 {
1103         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1104
1105         spin_lock(&dev_entry->cs_job_spinlock);
1106         list_add(&job->debugfs_list, &dev_entry->cs_job_list);
1107         spin_unlock(&dev_entry->cs_job_spinlock);
1108 }
1109
1110 void hl_debugfs_remove_job(struct hl_device *hdev, struct hl_cs_job *job)
1111 {
1112         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1113
1114         spin_lock(&dev_entry->cs_job_spinlock);
1115         list_del(&job->debugfs_list);
1116         spin_unlock(&dev_entry->cs_job_spinlock);
1117 }
1118
1119 void hl_debugfs_add_userptr(struct hl_device *hdev, struct hl_userptr *userptr)
1120 {
1121         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1122
1123         spin_lock(&dev_entry->userptr_spinlock);
1124         list_add(&userptr->debugfs_list, &dev_entry->userptr_list);
1125         spin_unlock(&dev_entry->userptr_spinlock);
1126 }
1127
1128 void hl_debugfs_remove_userptr(struct hl_device *hdev,
1129                                 struct hl_userptr *userptr)
1130 {
1131         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1132
1133         spin_lock(&dev_entry->userptr_spinlock);
1134         list_del(&userptr->debugfs_list);
1135         spin_unlock(&dev_entry->userptr_spinlock);
1136 }
1137
1138 void hl_debugfs_add_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1139 {
1140         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1141
1142         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1143         list_add(&ctx->debugfs_list, &dev_entry->ctx_mem_hash_list);
1144         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1145 }
1146
1147 void hl_debugfs_remove_ctx_mem_hash(struct hl_device *hdev, struct hl_ctx *ctx)
1148 {
1149         struct hl_dbg_device_entry *dev_entry = &hdev->hl_debugfs;
1150
1151         spin_lock(&dev_entry->ctx_mem_hash_spinlock);
1152         list_del(&ctx->debugfs_list);
1153         spin_unlock(&dev_entry->ctx_mem_hash_spinlock);
1154 }
1155
1156 void __init hl_debugfs_init(void)
1157 {
1158         hl_debug_root = debugfs_create_dir("habanalabs", NULL);
1159 }
1160
1161 void hl_debugfs_fini(void)
1162 {
1163         debugfs_remove_recursive(hl_debug_root);
1164 }