• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
4  */
5 #include <linux/debugfs.h>
6 #include <linux/dma-mapping.h>
7 #include <linux/slab.h>
8 #include <linux/uaccess.h>
9 
10 #include <soc/tegra/bpmp.h>
11 #include <soc/tegra/bpmp-abi.h>
12 
13 static DEFINE_MUTEX(bpmp_debug_lock);
14 
15 struct seqbuf {
16 	char *buf;
17 	size_t pos;
18 	size_t size;
19 };
20 
seqbuf_init(struct seqbuf * seqbuf,void * buf,size_t size)21 static void seqbuf_init(struct seqbuf *seqbuf, void *buf, size_t size)
22 {
23 	seqbuf->buf = buf;
24 	seqbuf->size = size;
25 	seqbuf->pos = 0;
26 }
27 
seqbuf_avail(struct seqbuf * seqbuf)28 static size_t seqbuf_avail(struct seqbuf *seqbuf)
29 {
30 	return seqbuf->pos < seqbuf->size ? seqbuf->size - seqbuf->pos : 0;
31 }
32 
seqbuf_status(struct seqbuf * seqbuf)33 static size_t seqbuf_status(struct seqbuf *seqbuf)
34 {
35 	return seqbuf->pos <= seqbuf->size ? 0 : -EOVERFLOW;
36 }
37 
seqbuf_eof(struct seqbuf * seqbuf)38 static int seqbuf_eof(struct seqbuf *seqbuf)
39 {
40 	return seqbuf->pos >= seqbuf->size;
41 }
42 
seqbuf_read(struct seqbuf * seqbuf,void * buf,size_t nbyte)43 static int seqbuf_read(struct seqbuf *seqbuf, void *buf, size_t nbyte)
44 {
45 	nbyte = min(nbyte, seqbuf_avail(seqbuf));
46 	memcpy(buf, seqbuf->buf + seqbuf->pos, nbyte);
47 	seqbuf->pos += nbyte;
48 	return seqbuf_status(seqbuf);
49 }
50 
seqbuf_read_u32(struct seqbuf * seqbuf,uint32_t * v)51 static int seqbuf_read_u32(struct seqbuf *seqbuf, uint32_t *v)
52 {
53 	int err;
54 
55 	err = seqbuf_read(seqbuf, v, 4);
56 	*v = le32_to_cpu(*v);
57 	return err;
58 }
59 
seqbuf_read_str(struct seqbuf * seqbuf,const char ** str)60 static int seqbuf_read_str(struct seqbuf *seqbuf, const char **str)
61 {
62 	*str = seqbuf->buf + seqbuf->pos;
63 	seqbuf->pos += strnlen(*str, seqbuf_avail(seqbuf));
64 	seqbuf->pos++;
65 	return seqbuf_status(seqbuf);
66 }
67 
seqbuf_seek(struct seqbuf * seqbuf,ssize_t offset)68 static void seqbuf_seek(struct seqbuf *seqbuf, ssize_t offset)
69 {
70 	seqbuf->pos += offset;
71 }
72 
73 /* map filename in Linux debugfs to corresponding entry in BPMP */
get_filename(struct tegra_bpmp * bpmp,const struct file * file,char * buf,int size)74 static const char *get_filename(struct tegra_bpmp *bpmp,
75 				const struct file *file, char *buf, int size)
76 {
77 	const char *root_path, *filename = NULL;
78 	char *root_path_buf;
79 	size_t root_len;
80 	size_t root_path_buf_len = 512;
81 
82 	root_path_buf = kzalloc(root_path_buf_len, GFP_KERNEL);
83 	if (!root_path_buf)
84 		goto out;
85 
86 	root_path = dentry_path(bpmp->debugfs_mirror, root_path_buf,
87 				root_path_buf_len);
88 	if (IS_ERR(root_path))
89 		goto out;
90 
91 	root_len = strlen(root_path);
92 
93 	filename = dentry_path(file->f_path.dentry, buf, size);
94 	if (IS_ERR(filename)) {
95 		filename = NULL;
96 		goto out;
97 	}
98 
99 	if (strlen(filename) < root_len || strncmp(filename, root_path, root_len)) {
100 		filename = NULL;
101 		goto out;
102 	}
103 
104 	filename += root_len;
105 
106 out:
107 	kfree(root_path_buf);
108 	return filename;
109 }
110 
mrq_debug_open(struct tegra_bpmp * bpmp,const char * name,uint32_t * fd,uint32_t * len,bool write)111 static int mrq_debug_open(struct tegra_bpmp *bpmp, const char *name,
112 			  uint32_t *fd, uint32_t *len, bool write)
113 {
114 	struct mrq_debug_request req = {
115 		.cmd = cpu_to_le32(write ? CMD_DEBUG_OPEN_WO : CMD_DEBUG_OPEN_RO),
116 	};
117 	struct mrq_debug_response resp;
118 	struct tegra_bpmp_message msg = {
119 		.mrq = MRQ_DEBUG,
120 		.tx = {
121 			.data = &req,
122 			.size = sizeof(req),
123 		},
124 		.rx = {
125 			.data = &resp,
126 			.size = sizeof(resp),
127 		},
128 	};
129 	ssize_t sz_name;
130 	int err = 0;
131 
132 	sz_name = strscpy(req.fop.name, name, sizeof(req.fop.name));
133 	if (sz_name < 0) {
134 		pr_err("File name too large: %s\n", name);
135 		return -EINVAL;
136 	}
137 
138 	err = tegra_bpmp_transfer(bpmp, &msg);
139 	if (err < 0)
140 		return err;
141 	else if (msg.rx.ret < 0)
142 		return -EINVAL;
143 
144 	*len = resp.fop.datalen;
145 	*fd = resp.fop.fd;
146 
147 	return 0;
148 }
149 
mrq_debug_close(struct tegra_bpmp * bpmp,uint32_t fd)150 static int mrq_debug_close(struct tegra_bpmp *bpmp, uint32_t fd)
151 {
152 	struct mrq_debug_request req = {
153 		.cmd = cpu_to_le32(CMD_DEBUG_CLOSE),
154 		.frd = {
155 			.fd = fd,
156 		},
157 	};
158 	struct mrq_debug_response resp;
159 	struct tegra_bpmp_message msg = {
160 		.mrq = MRQ_DEBUG,
161 		.tx = {
162 			.data = &req,
163 			.size = sizeof(req),
164 		},
165 		.rx = {
166 			.data = &resp,
167 			.size = sizeof(resp),
168 		},
169 	};
170 	int err = 0;
171 
172 	err = tegra_bpmp_transfer(bpmp, &msg);
173 	if (err < 0)
174 		return err;
175 	else if (msg.rx.ret < 0)
176 		return -EINVAL;
177 
178 	return 0;
179 }
180 
mrq_debug_read(struct tegra_bpmp * bpmp,const char * name,char * data,size_t sz_data,uint32_t * nbytes)181 static int mrq_debug_read(struct tegra_bpmp *bpmp, const char *name,
182 			  char *data, size_t sz_data, uint32_t *nbytes)
183 {
184 	struct mrq_debug_request req = {
185 		.cmd = cpu_to_le32(CMD_DEBUG_READ),
186 	};
187 	struct mrq_debug_response resp;
188 	struct tegra_bpmp_message msg = {
189 		.mrq = MRQ_DEBUG,
190 		.tx = {
191 			.data = &req,
192 			.size = sizeof(req),
193 		},
194 		.rx = {
195 			.data = &resp,
196 			.size = sizeof(resp),
197 		},
198 	};
199 	uint32_t fd = 0, len = 0;
200 	int remaining, err;
201 
202 	mutex_lock(&bpmp_debug_lock);
203 	err = mrq_debug_open(bpmp, name, &fd, &len, 0);
204 	if (err)
205 		goto out;
206 
207 	if (len > sz_data) {
208 		err = -EFBIG;
209 		goto close;
210 	}
211 
212 	req.frd.fd = fd;
213 	remaining = len;
214 
215 	while (remaining > 0) {
216 		err = tegra_bpmp_transfer(bpmp, &msg);
217 		if (err < 0) {
218 			goto close;
219 		} else if (msg.rx.ret < 0) {
220 			err = -EINVAL;
221 			goto close;
222 		}
223 
224 		if (resp.frd.readlen > remaining) {
225 			pr_err("%s: read data length invalid\n", __func__);
226 			err = -EINVAL;
227 			goto close;
228 		}
229 
230 		memcpy(data, resp.frd.data, resp.frd.readlen);
231 		data += resp.frd.readlen;
232 		remaining -= resp.frd.readlen;
233 	}
234 
235 	*nbytes = len;
236 
237 close:
238 	err = mrq_debug_close(bpmp, fd);
239 out:
240 	mutex_unlock(&bpmp_debug_lock);
241 	return err;
242 }
243 
mrq_debug_write(struct tegra_bpmp * bpmp,const char * name,uint8_t * data,size_t sz_data)244 static int mrq_debug_write(struct tegra_bpmp *bpmp, const char *name,
245 			   uint8_t *data, size_t sz_data)
246 {
247 	struct mrq_debug_request req = {
248 		.cmd = cpu_to_le32(CMD_DEBUG_WRITE)
249 	};
250 	struct mrq_debug_response resp;
251 	struct tegra_bpmp_message msg = {
252 		.mrq = MRQ_DEBUG,
253 		.tx = {
254 			.data = &req,
255 			.size = sizeof(req),
256 		},
257 		.rx = {
258 			.data = &resp,
259 			.size = sizeof(resp),
260 		},
261 	};
262 	uint32_t fd = 0, len = 0;
263 	size_t remaining;
264 	int err;
265 
266 	mutex_lock(&bpmp_debug_lock);
267 	err = mrq_debug_open(bpmp, name, &fd, &len, 1);
268 	if (err)
269 		goto out;
270 
271 	if (sz_data > len) {
272 		err = -EINVAL;
273 		goto close;
274 	}
275 
276 	req.fwr.fd = fd;
277 	remaining = sz_data;
278 
279 	while (remaining > 0) {
280 		len = min(remaining, sizeof(req.fwr.data));
281 		memcpy(req.fwr.data, data, len);
282 		req.fwr.datalen = len;
283 
284 		err = tegra_bpmp_transfer(bpmp, &msg);
285 		if (err < 0) {
286 			goto close;
287 		} else if (msg.rx.ret < 0) {
288 			err = -EINVAL;
289 			goto close;
290 		}
291 
292 		data += req.fwr.datalen;
293 		remaining -= req.fwr.datalen;
294 	}
295 
296 close:
297 	err = mrq_debug_close(bpmp, fd);
298 out:
299 	mutex_unlock(&bpmp_debug_lock);
300 	return err;
301 }
302 
bpmp_debug_show(struct seq_file * m,void * p)303 static int bpmp_debug_show(struct seq_file *m, void *p)
304 {
305 	struct file *file = m->private;
306 	struct inode *inode = file_inode(file);
307 	struct tegra_bpmp *bpmp = inode->i_private;
308 	char *databuf = NULL;
309 	char fnamebuf[256];
310 	const char *filename;
311 	uint32_t nbytes = 0;
312 	size_t len;
313 	int err;
314 
315 	len = seq_get_buf(m, &databuf);
316 	if (!databuf)
317 		return -ENOMEM;
318 
319 	filename = get_filename(bpmp, file, fnamebuf, sizeof(fnamebuf));
320 	if (!filename)
321 		return -ENOENT;
322 
323 	err = mrq_debug_read(bpmp, filename, databuf, len, &nbytes);
324 	if (!err)
325 		seq_commit(m, nbytes);
326 
327 	return err;
328 }
329 
bpmp_debug_store(struct file * file,const char __user * buf,size_t count,loff_t * f_pos)330 static ssize_t bpmp_debug_store(struct file *file, const char __user *buf,
331 		size_t count, loff_t *f_pos)
332 {
333 	struct inode *inode = file_inode(file);
334 	struct tegra_bpmp *bpmp = inode->i_private;
335 	char *databuf = NULL;
336 	char fnamebuf[256];
337 	const char *filename;
338 	ssize_t err;
339 
340 	filename = get_filename(bpmp, file, fnamebuf, sizeof(fnamebuf));
341 	if (!filename)
342 		return -ENOENT;
343 
344 	databuf = kmalloc(count, GFP_KERNEL);
345 	if (!databuf)
346 		return -ENOMEM;
347 
348 	if (copy_from_user(databuf, buf, count)) {
349 		err = -EFAULT;
350 		goto free_ret;
351 	}
352 
353 	err = mrq_debug_write(bpmp, filename, databuf, count);
354 
355 free_ret:
356 	kfree(databuf);
357 
358 	return err ?: count;
359 }
360 
bpmp_debug_open(struct inode * inode,struct file * file)361 static int bpmp_debug_open(struct inode *inode, struct file *file)
362 {
363 	return single_open_size(file, bpmp_debug_show, file, SZ_256K);
364 }
365 
366 static const struct file_operations bpmp_debug_fops = {
367 	.open		= bpmp_debug_open,
368 	.read		= seq_read,
369 	.llseek		= seq_lseek,
370 	.write		= bpmp_debug_store,
371 	.release	= single_release,
372 };
373 
bpmp_populate_debugfs_inband(struct tegra_bpmp * bpmp,struct dentry * parent,char * ppath)374 static int bpmp_populate_debugfs_inband(struct tegra_bpmp *bpmp,
375 					struct dentry *parent,
376 					char *ppath)
377 {
378 	const size_t pathlen = SZ_256;
379 	const size_t bufsize = SZ_16K;
380 	uint32_t dsize, attrs = 0;
381 	struct dentry *dentry;
382 	struct seqbuf seqbuf;
383 	char *buf, *pathbuf;
384 	const char *name;
385 	int err = 0;
386 
387 	if (!bpmp || !parent || !ppath)
388 		return -EINVAL;
389 
390 	buf = kmalloc(bufsize, GFP_KERNEL);
391 	if (!buf)
392 		return -ENOMEM;
393 
394 	pathbuf = kzalloc(pathlen, GFP_KERNEL);
395 	if (!pathbuf) {
396 		kfree(buf);
397 		return -ENOMEM;
398 	}
399 
400 	err = mrq_debug_read(bpmp, ppath, buf, bufsize, &dsize);
401 	if (err)
402 		goto out;
403 
404 	seqbuf_init(&seqbuf, buf, dsize);
405 
406 	while (!seqbuf_eof(&seqbuf)) {
407 		err = seqbuf_read_u32(&seqbuf, &attrs);
408 		if (err)
409 			goto out;
410 
411 		err = seqbuf_read_str(&seqbuf, &name);
412 		if (err < 0)
413 			goto out;
414 
415 		if (attrs & DEBUGFS_S_ISDIR) {
416 			size_t len;
417 
418 			dentry = debugfs_create_dir(name, parent);
419 			if (IS_ERR(dentry)) {
420 				err = PTR_ERR(dentry);
421 				goto out;
422 			}
423 
424 			len = snprintf(pathbuf, pathlen, "%s%s/", ppath, name);
425 			if (len >= pathlen) {
426 				err = -EINVAL;
427 				goto out;
428 			}
429 
430 			err = bpmp_populate_debugfs_inband(bpmp, dentry,
431 							   pathbuf);
432 			if (err < 0)
433 				goto out;
434 		} else {
435 			umode_t mode;
436 
437 			mode = attrs & DEBUGFS_S_IRUSR ? 0400 : 0;
438 			mode |= attrs & DEBUGFS_S_IWUSR ? 0200 : 0;
439 			dentry = debugfs_create_file(name, mode, parent, bpmp,
440 						     &bpmp_debug_fops);
441 			if (IS_ERR(dentry)) {
442 				err = -ENOMEM;
443 				goto out;
444 			}
445 		}
446 	}
447 
448 out:
449 	kfree(pathbuf);
450 	kfree(buf);
451 
452 	return err;
453 }
454 
mrq_debugfs_read(struct tegra_bpmp * bpmp,dma_addr_t name,size_t sz_name,dma_addr_t data,size_t sz_data,size_t * nbytes)455 static int mrq_debugfs_read(struct tegra_bpmp *bpmp,
456 			    dma_addr_t name, size_t sz_name,
457 			    dma_addr_t data, size_t sz_data,
458 			    size_t *nbytes)
459 {
460 	struct mrq_debugfs_request req = {
461 		.cmd = cpu_to_le32(CMD_DEBUGFS_READ),
462 		.fop = {
463 			.fnameaddr = cpu_to_le32((uint32_t)name),
464 			.fnamelen = cpu_to_le32((uint32_t)sz_name),
465 			.dataaddr = cpu_to_le32((uint32_t)data),
466 			.datalen = cpu_to_le32((uint32_t)sz_data),
467 		},
468 	};
469 	struct mrq_debugfs_response resp;
470 	struct tegra_bpmp_message msg = {
471 		.mrq = MRQ_DEBUGFS,
472 		.tx = {
473 			.data = &req,
474 			.size = sizeof(req),
475 		},
476 		.rx = {
477 			.data = &resp,
478 			.size = sizeof(resp),
479 		},
480 	};
481 	int err;
482 
483 	err = tegra_bpmp_transfer(bpmp, &msg);
484 	if (err < 0)
485 		return err;
486 	else if (msg.rx.ret < 0)
487 		return -EINVAL;
488 
489 	*nbytes = (size_t)resp.fop.nbytes;
490 
491 	return 0;
492 }
493 
mrq_debugfs_write(struct tegra_bpmp * bpmp,dma_addr_t name,size_t sz_name,dma_addr_t data,size_t sz_data)494 static int mrq_debugfs_write(struct tegra_bpmp *bpmp,
495 			     dma_addr_t name, size_t sz_name,
496 			     dma_addr_t data, size_t sz_data)
497 {
498 	const struct mrq_debugfs_request req = {
499 		.cmd = cpu_to_le32(CMD_DEBUGFS_WRITE),
500 		.fop = {
501 			.fnameaddr = cpu_to_le32((uint32_t)name),
502 			.fnamelen = cpu_to_le32((uint32_t)sz_name),
503 			.dataaddr = cpu_to_le32((uint32_t)data),
504 			.datalen = cpu_to_le32((uint32_t)sz_data),
505 		},
506 	};
507 	struct tegra_bpmp_message msg = {
508 		.mrq = MRQ_DEBUGFS,
509 		.tx = {
510 			.data = &req,
511 			.size = sizeof(req),
512 		},
513 	};
514 
515 	return tegra_bpmp_transfer(bpmp, &msg);
516 }
517 
mrq_debugfs_dumpdir(struct tegra_bpmp * bpmp,dma_addr_t addr,size_t size,size_t * nbytes)518 static int mrq_debugfs_dumpdir(struct tegra_bpmp *bpmp, dma_addr_t addr,
519 			       size_t size, size_t *nbytes)
520 {
521 	const struct mrq_debugfs_request req = {
522 		.cmd = cpu_to_le32(CMD_DEBUGFS_DUMPDIR),
523 		.dumpdir = {
524 			.dataaddr = cpu_to_le32((uint32_t)addr),
525 			.datalen = cpu_to_le32((uint32_t)size),
526 		},
527 	};
528 	struct mrq_debugfs_response resp;
529 	struct tegra_bpmp_message msg = {
530 		.mrq = MRQ_DEBUGFS,
531 		.tx = {
532 			.data = &req,
533 			.size = sizeof(req),
534 		},
535 		.rx = {
536 			.data = &resp,
537 			.size = sizeof(resp),
538 		},
539 	};
540 	int err;
541 
542 	err = tegra_bpmp_transfer(bpmp, &msg);
543 	if (err < 0)
544 		return err;
545 	else if (msg.rx.ret < 0)
546 		return -EINVAL;
547 
548 	*nbytes = (size_t)resp.dumpdir.nbytes;
549 
550 	return 0;
551 }
552 
debugfs_show(struct seq_file * m,void * p)553 static int debugfs_show(struct seq_file *m, void *p)
554 {
555 	struct file *file = m->private;
556 	struct inode *inode = file_inode(file);
557 	struct tegra_bpmp *bpmp = inode->i_private;
558 	const size_t datasize = m->size;
559 	const size_t namesize = SZ_256;
560 	void *datavirt, *namevirt;
561 	dma_addr_t dataphys, namephys;
562 	char buf[256];
563 	const char *filename;
564 	size_t len, nbytes;
565 	int err;
566 
567 	filename = get_filename(bpmp, file, buf, sizeof(buf));
568 	if (!filename)
569 		return -ENOENT;
570 
571 	namevirt = dma_alloc_coherent(bpmp->dev, namesize, &namephys,
572 				      GFP_KERNEL | GFP_DMA32);
573 	if (!namevirt)
574 		return -ENOMEM;
575 
576 	datavirt = dma_alloc_coherent(bpmp->dev, datasize, &dataphys,
577 				      GFP_KERNEL | GFP_DMA32);
578 	if (!datavirt) {
579 		err = -ENOMEM;
580 		goto free_namebuf;
581 	}
582 
583 	len = strlen(filename);
584 	strncpy(namevirt, filename, namesize);
585 
586 	err = mrq_debugfs_read(bpmp, namephys, len, dataphys, datasize,
587 			       &nbytes);
588 
589 	if (!err)
590 		seq_write(m, datavirt, nbytes);
591 
592 	dma_free_coherent(bpmp->dev, datasize, datavirt, dataphys);
593 free_namebuf:
594 	dma_free_coherent(bpmp->dev, namesize, namevirt, namephys);
595 
596 	return err;
597 }
598 
debugfs_open(struct inode * inode,struct file * file)599 static int debugfs_open(struct inode *inode, struct file *file)
600 {
601 	return single_open_size(file, debugfs_show, file, SZ_128K);
602 }
603 
debugfs_store(struct file * file,const char __user * buf,size_t count,loff_t * f_pos)604 static ssize_t debugfs_store(struct file *file, const char __user *buf,
605 		size_t count, loff_t *f_pos)
606 {
607 	struct inode *inode = file_inode(file);
608 	struct tegra_bpmp *bpmp = inode->i_private;
609 	const size_t datasize = count;
610 	const size_t namesize = SZ_256;
611 	void *datavirt, *namevirt;
612 	dma_addr_t dataphys, namephys;
613 	char fnamebuf[256];
614 	const char *filename;
615 	size_t len;
616 	int err;
617 
618 	filename = get_filename(bpmp, file, fnamebuf, sizeof(fnamebuf));
619 	if (!filename)
620 		return -ENOENT;
621 
622 	namevirt = dma_alloc_coherent(bpmp->dev, namesize, &namephys,
623 				      GFP_KERNEL | GFP_DMA32);
624 	if (!namevirt)
625 		return -ENOMEM;
626 
627 	datavirt = dma_alloc_coherent(bpmp->dev, datasize, &dataphys,
628 				      GFP_KERNEL | GFP_DMA32);
629 	if (!datavirt) {
630 		err = -ENOMEM;
631 		goto free_namebuf;
632 	}
633 
634 	len = strlen(filename);
635 	strncpy(namevirt, filename, namesize);
636 
637 	if (copy_from_user(datavirt, buf, count)) {
638 		err = -EFAULT;
639 		goto free_databuf;
640 	}
641 
642 	err = mrq_debugfs_write(bpmp, namephys, len, dataphys,
643 				count);
644 
645 free_databuf:
646 	dma_free_coherent(bpmp->dev, datasize, datavirt, dataphys);
647 free_namebuf:
648 	dma_free_coherent(bpmp->dev, namesize, namevirt, namephys);
649 
650 	return err ?: count;
651 }
652 
653 static const struct file_operations debugfs_fops = {
654 	.open		= debugfs_open,
655 	.read		= seq_read,
656 	.llseek		= seq_lseek,
657 	.write		= debugfs_store,
658 	.release	= single_release,
659 };
660 
bpmp_populate_dir(struct tegra_bpmp * bpmp,struct seqbuf * seqbuf,struct dentry * parent,uint32_t depth)661 static int bpmp_populate_dir(struct tegra_bpmp *bpmp, struct seqbuf *seqbuf,
662 			     struct dentry *parent, uint32_t depth)
663 {
664 	int err;
665 	uint32_t d, t;
666 	const char *name;
667 	struct dentry *dentry;
668 
669 	while (!seqbuf_eof(seqbuf)) {
670 		err = seqbuf_read_u32(seqbuf, &d);
671 		if (err < 0)
672 			return err;
673 
674 		if (d < depth) {
675 			seqbuf_seek(seqbuf, -4);
676 			/* go up a level */
677 			return 0;
678 		} else if (d != depth) {
679 			/* malformed data received from BPMP */
680 			return -EIO;
681 		}
682 
683 		err = seqbuf_read_u32(seqbuf, &t);
684 		if (err < 0)
685 			return err;
686 		err = seqbuf_read_str(seqbuf, &name);
687 		if (err < 0)
688 			return err;
689 
690 		if (t & DEBUGFS_S_ISDIR) {
691 			dentry = debugfs_create_dir(name, parent);
692 			if (IS_ERR(dentry))
693 				return -ENOMEM;
694 			err = bpmp_populate_dir(bpmp, seqbuf, dentry, depth+1);
695 			if (err < 0)
696 				return err;
697 		} else {
698 			umode_t mode;
699 
700 			mode = t & DEBUGFS_S_IRUSR ? S_IRUSR : 0;
701 			mode |= t & DEBUGFS_S_IWUSR ? S_IWUSR : 0;
702 			dentry = debugfs_create_file(name, mode,
703 						     parent, bpmp,
704 						     &debugfs_fops);
705 			if (IS_ERR(dentry))
706 				return -ENOMEM;
707 		}
708 	}
709 
710 	return 0;
711 }
712 
bpmp_populate_debugfs_shmem(struct tegra_bpmp * bpmp)713 static int bpmp_populate_debugfs_shmem(struct tegra_bpmp *bpmp)
714 {
715 	struct seqbuf seqbuf;
716 	const size_t sz = SZ_512K;
717 	dma_addr_t phys;
718 	size_t nbytes;
719 	void *virt;
720 	int err;
721 
722 	virt = dma_alloc_coherent(bpmp->dev, sz, &phys,
723 				  GFP_KERNEL | GFP_DMA32);
724 	if (!virt)
725 		return -ENOMEM;
726 
727 	err = mrq_debugfs_dumpdir(bpmp, phys, sz, &nbytes);
728 	if (err < 0) {
729 		goto free;
730 	} else if (nbytes > sz) {
731 		err = -EINVAL;
732 		goto free;
733 	}
734 
735 	seqbuf_init(&seqbuf, virt, nbytes);
736 	err = bpmp_populate_dir(bpmp, &seqbuf, bpmp->debugfs_mirror, 0);
737 free:
738 	dma_free_coherent(bpmp->dev, sz, virt, phys);
739 
740 	return err;
741 }
742 
tegra_bpmp_init_debugfs(struct tegra_bpmp * bpmp)743 int tegra_bpmp_init_debugfs(struct tegra_bpmp *bpmp)
744 {
745 	struct dentry *root;
746 	bool inband;
747 	int err;
748 
749 	inband = tegra_bpmp_mrq_is_supported(bpmp, MRQ_DEBUG);
750 
751 	if (!inband && !tegra_bpmp_mrq_is_supported(bpmp, MRQ_DEBUGFS))
752 		return 0;
753 
754 	root = debugfs_create_dir("bpmp", NULL);
755 	if (IS_ERR(root))
756 		return -ENOMEM;
757 
758 	bpmp->debugfs_mirror = debugfs_create_dir("debug", root);
759 	if (IS_ERR(bpmp->debugfs_mirror)) {
760 		err = -ENOMEM;
761 		goto out;
762 	}
763 
764 	if (inband)
765 		err = bpmp_populate_debugfs_inband(bpmp, bpmp->debugfs_mirror,
766 						   "/");
767 	else
768 		err = bpmp_populate_debugfs_shmem(bpmp);
769 
770 out:
771 	if (err < 0)
772 		debugfs_remove_recursive(root);
773 
774 	return err;
775 }
776