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