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