18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Remote Processor Framework
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2011 Texas Instruments, Inc.
68c2ecf20Sopenharmony_ci * Copyright (C) 2011 Google, Inc.
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Ohad Ben-Cohen <ohad@wizery.com>
98c2ecf20Sopenharmony_ci * Mark Grosen <mgrosen@ti.com>
108c2ecf20Sopenharmony_ci * Brian Swetland <swetland@google.com>
118c2ecf20Sopenharmony_ci * Fernando Guzman Lugo <fernando.lugo@ti.com>
128c2ecf20Sopenharmony_ci * Suman Anna <s-anna@ti.com>
138c2ecf20Sopenharmony_ci * Robert Tivy <rtivy@ti.com>
148c2ecf20Sopenharmony_ci * Armando Uribe De Leon <x0095078@ti.com>
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define pr_fmt(fmt)    "%s: " fmt, __func__
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/debugfs.h>
218c2ecf20Sopenharmony_ci#include <linux/remoteproc.h>
228c2ecf20Sopenharmony_ci#include <linux/device.h>
238c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "remoteproc_internal.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* remoteproc debugfs parent dir */
288c2ecf20Sopenharmony_cistatic struct dentry *rproc_dbg;
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci * A coredump-configuration-to-string lookup table, for exposing a
328c2ecf20Sopenharmony_ci * human readable configuration via debugfs. Always keep in sync with
338c2ecf20Sopenharmony_ci * enum rproc_coredump_mechanism
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_cistatic const char * const rproc_coredump_str[] = {
368c2ecf20Sopenharmony_ci	[RPROC_COREDUMP_DISABLED]	= "disabled",
378c2ecf20Sopenharmony_ci	[RPROC_COREDUMP_ENABLED]	= "enabled",
388c2ecf20Sopenharmony_ci	[RPROC_COREDUMP_INLINE]		= "inline",
398c2ecf20Sopenharmony_ci};
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Expose the current coredump configuration via debugfs */
428c2ecf20Sopenharmony_cistatic ssize_t rproc_coredump_read(struct file *filp, char __user *userbuf,
438c2ecf20Sopenharmony_ci				   size_t count, loff_t *ppos)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	struct rproc *rproc = filp->private_data;
468c2ecf20Sopenharmony_ci	char buf[20];
478c2ecf20Sopenharmony_ci	int len;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	len = scnprintf(buf, sizeof(buf), "%s\n",
508c2ecf20Sopenharmony_ci			rproc_coredump_str[rproc->dump_conf]);
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	return simple_read_from_buffer(userbuf, count, ppos, buf, len);
538c2ecf20Sopenharmony_ci}
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci/*
568c2ecf20Sopenharmony_ci * By writing to the 'coredump' debugfs entry, we control the behavior of the
578c2ecf20Sopenharmony_ci * coredump mechanism dynamically. The default value of this entry is "disabled".
588c2ecf20Sopenharmony_ci *
598c2ecf20Sopenharmony_ci * The 'coredump' debugfs entry supports these commands:
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci * disabled:	By default coredump collection is disabled. Recovery will
628c2ecf20Sopenharmony_ci *		proceed without collecting any dump.
638c2ecf20Sopenharmony_ci *
648c2ecf20Sopenharmony_ci * enabled:	When the remoteproc crashes the entire coredump will be copied
658c2ecf20Sopenharmony_ci *		to a separate buffer and exposed to userspace.
668c2ecf20Sopenharmony_ci *
678c2ecf20Sopenharmony_ci * inline:	The coredump will not be copied to a separate buffer and the
688c2ecf20Sopenharmony_ci *		recovery process will have to wait until data is read by
698c2ecf20Sopenharmony_ci *		userspace. But this avoid usage of extra memory.
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_cistatic ssize_t rproc_coredump_write(struct file *filp,
728c2ecf20Sopenharmony_ci				    const char __user *user_buf, size_t count,
738c2ecf20Sopenharmony_ci				    loff_t *ppos)
748c2ecf20Sopenharmony_ci{
758c2ecf20Sopenharmony_ci	struct rproc *rproc = filp->private_data;
768c2ecf20Sopenharmony_ci	int ret, err = 0;
778c2ecf20Sopenharmony_ci	char buf[20];
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	if (count < 1 || count > sizeof(buf))
808c2ecf20Sopenharmony_ci		return -EINVAL;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	ret = copy_from_user(buf, user_buf, count);
838c2ecf20Sopenharmony_ci	if (ret)
848c2ecf20Sopenharmony_ci		return -EFAULT;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	/* remove end of line */
878c2ecf20Sopenharmony_ci	if (buf[count - 1] == '\n')
888c2ecf20Sopenharmony_ci		buf[count - 1] = '\0';
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	if (rproc->state == RPROC_CRASHED) {
918c2ecf20Sopenharmony_ci		dev_err(&rproc->dev, "can't change coredump configuration\n");
928c2ecf20Sopenharmony_ci		err = -EBUSY;
938c2ecf20Sopenharmony_ci		goto out;
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (!strncmp(buf, "disabled", count)) {
978c2ecf20Sopenharmony_ci		rproc->dump_conf = RPROC_COREDUMP_DISABLED;
988c2ecf20Sopenharmony_ci	} else if (!strncmp(buf, "enabled", count)) {
998c2ecf20Sopenharmony_ci		rproc->dump_conf = RPROC_COREDUMP_ENABLED;
1008c2ecf20Sopenharmony_ci	} else if (!strncmp(buf, "inline", count)) {
1018c2ecf20Sopenharmony_ci		rproc->dump_conf = RPROC_COREDUMP_INLINE;
1028c2ecf20Sopenharmony_ci	} else {
1038c2ecf20Sopenharmony_ci		dev_err(&rproc->dev, "Invalid coredump configuration\n");
1048c2ecf20Sopenharmony_ci		err = -EINVAL;
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ciout:
1078c2ecf20Sopenharmony_ci	return err ? err : count;
1088c2ecf20Sopenharmony_ci}
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_cistatic const struct file_operations rproc_coredump_fops = {
1118c2ecf20Sopenharmony_ci	.read = rproc_coredump_read,
1128c2ecf20Sopenharmony_ci	.write = rproc_coredump_write,
1138c2ecf20Sopenharmony_ci	.open = simple_open,
1148c2ecf20Sopenharmony_ci	.llseek = generic_file_llseek,
1158c2ecf20Sopenharmony_ci};
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/*
1188c2ecf20Sopenharmony_ci * Some remote processors may support dumping trace logs into a shared
1198c2ecf20Sopenharmony_ci * memory buffer. We expose this trace buffer using debugfs, so users
1208c2ecf20Sopenharmony_ci * can easily tell what's going on remotely.
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci * We will most probably improve the rproc tracing facilities later on,
1238c2ecf20Sopenharmony_ci * but this kind of lightweight and simple mechanism is always good to have,
1248c2ecf20Sopenharmony_ci * as it provides very early tracing with little to no dependencies at all.
1258c2ecf20Sopenharmony_ci */
1268c2ecf20Sopenharmony_cistatic ssize_t rproc_trace_read(struct file *filp, char __user *userbuf,
1278c2ecf20Sopenharmony_ci				size_t count, loff_t *ppos)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct rproc_debug_trace *data = filp->private_data;
1308c2ecf20Sopenharmony_ci	struct rproc_mem_entry *trace = &data->trace_mem;
1318c2ecf20Sopenharmony_ci	void *va;
1328c2ecf20Sopenharmony_ci	char buf[100];
1338c2ecf20Sopenharmony_ci	int len;
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	va = rproc_da_to_va(data->rproc, trace->da, trace->len);
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	if (!va) {
1388c2ecf20Sopenharmony_ci		len = scnprintf(buf, sizeof(buf), "Trace %s not available\n",
1398c2ecf20Sopenharmony_ci				trace->name);
1408c2ecf20Sopenharmony_ci		va = buf;
1418c2ecf20Sopenharmony_ci	} else {
1428c2ecf20Sopenharmony_ci		len = strnlen(va, trace->len);
1438c2ecf20Sopenharmony_ci	}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	return simple_read_from_buffer(userbuf, count, ppos, va, len);
1468c2ecf20Sopenharmony_ci}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_cistatic const struct file_operations trace_rproc_ops = {
1498c2ecf20Sopenharmony_ci	.read = rproc_trace_read,
1508c2ecf20Sopenharmony_ci	.open = simple_open,
1518c2ecf20Sopenharmony_ci	.llseek	= generic_file_llseek,
1528c2ecf20Sopenharmony_ci};
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci/* expose the name of the remote processor via debugfs */
1558c2ecf20Sopenharmony_cistatic ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
1568c2ecf20Sopenharmony_ci			       size_t count, loff_t *ppos)
1578c2ecf20Sopenharmony_ci{
1588c2ecf20Sopenharmony_ci	struct rproc *rproc = filp->private_data;
1598c2ecf20Sopenharmony_ci	/* need room for the name, a newline and a terminating null */
1608c2ecf20Sopenharmony_ci	char buf[100];
1618c2ecf20Sopenharmony_ci	int i;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return simple_read_from_buffer(userbuf, count, ppos, buf, i);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic const struct file_operations rproc_name_ops = {
1698c2ecf20Sopenharmony_ci	.read = rproc_name_read,
1708c2ecf20Sopenharmony_ci	.open = simple_open,
1718c2ecf20Sopenharmony_ci	.llseek	= generic_file_llseek,
1728c2ecf20Sopenharmony_ci};
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci/* expose recovery flag via debugfs */
1758c2ecf20Sopenharmony_cistatic ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf,
1768c2ecf20Sopenharmony_ci				   size_t count, loff_t *ppos)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct rproc *rproc = filp->private_data;
1798c2ecf20Sopenharmony_ci	char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n";
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
1828c2ecf20Sopenharmony_ci}
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/*
1858c2ecf20Sopenharmony_ci * By writing to the 'recovery' debugfs entry, we control the behavior of the
1868c2ecf20Sopenharmony_ci * recovery mechanism dynamically. The default value of this entry is "enabled".
1878c2ecf20Sopenharmony_ci *
1888c2ecf20Sopenharmony_ci * The 'recovery' debugfs entry supports these commands:
1898c2ecf20Sopenharmony_ci *
1908c2ecf20Sopenharmony_ci * enabled:	When enabled, the remote processor will be automatically
1918c2ecf20Sopenharmony_ci *		recovered whenever it crashes. Moreover, if the remote
1928c2ecf20Sopenharmony_ci *		processor crashes while recovery is disabled, it will
1938c2ecf20Sopenharmony_ci *		be automatically recovered too as soon as recovery is enabled.
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * disabled:	When disabled, a remote processor will remain in a crashed
1968c2ecf20Sopenharmony_ci *		state if it crashes. This is useful for debugging purposes;
1978c2ecf20Sopenharmony_ci *		without it, debugging a crash is substantially harder.
1988c2ecf20Sopenharmony_ci *
1998c2ecf20Sopenharmony_ci * recover:	This function will trigger an immediate recovery if the
2008c2ecf20Sopenharmony_ci *		remote processor is in a crashed state, without changing
2018c2ecf20Sopenharmony_ci *		or checking the recovery state (enabled/disabled).
2028c2ecf20Sopenharmony_ci *		This is useful during debugging sessions, when one expects
2038c2ecf20Sopenharmony_ci *		additional crashes to happen after enabling recovery. In this
2048c2ecf20Sopenharmony_ci *		case, enabling recovery will make it hard to debug subsequent
2058c2ecf20Sopenharmony_ci *		crashes, so it's recommended to keep recovery disabled, and
2068c2ecf20Sopenharmony_ci *		instead use the "recover" command as needed.
2078c2ecf20Sopenharmony_ci */
2088c2ecf20Sopenharmony_cistatic ssize_t
2098c2ecf20Sopenharmony_cirproc_recovery_write(struct file *filp, const char __user *user_buf,
2108c2ecf20Sopenharmony_ci		     size_t count, loff_t *ppos)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	struct rproc *rproc = filp->private_data;
2138c2ecf20Sopenharmony_ci	char buf[10];
2148c2ecf20Sopenharmony_ci	int ret;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	if (count < 1 || count > sizeof(buf))
2178c2ecf20Sopenharmony_ci		return -EINVAL;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	ret = copy_from_user(buf, user_buf, count);
2208c2ecf20Sopenharmony_ci	if (ret)
2218c2ecf20Sopenharmony_ci		return -EFAULT;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* remove end of line */
2248c2ecf20Sopenharmony_ci	if (buf[count - 1] == '\n')
2258c2ecf20Sopenharmony_ci		buf[count - 1] = '\0';
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci	if (!strncmp(buf, "enabled", count)) {
2288c2ecf20Sopenharmony_ci		/* change the flag and begin the recovery process if needed */
2298c2ecf20Sopenharmony_ci		rproc->recovery_disabled = false;
2308c2ecf20Sopenharmony_ci		rproc_trigger_recovery(rproc);
2318c2ecf20Sopenharmony_ci	} else if (!strncmp(buf, "disabled", count)) {
2328c2ecf20Sopenharmony_ci		rproc->recovery_disabled = true;
2338c2ecf20Sopenharmony_ci	} else if (!strncmp(buf, "recover", count)) {
2348c2ecf20Sopenharmony_ci		/* begin the recovery process without changing the flag */
2358c2ecf20Sopenharmony_ci		rproc_trigger_recovery(rproc);
2368c2ecf20Sopenharmony_ci	} else {
2378c2ecf20Sopenharmony_ci		return -EINVAL;
2388c2ecf20Sopenharmony_ci	}
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	return count;
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic const struct file_operations rproc_recovery_ops = {
2448c2ecf20Sopenharmony_ci	.read = rproc_recovery_read,
2458c2ecf20Sopenharmony_ci	.write = rproc_recovery_write,
2468c2ecf20Sopenharmony_ci	.open = simple_open,
2478c2ecf20Sopenharmony_ci	.llseek = generic_file_llseek,
2488c2ecf20Sopenharmony_ci};
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci/* expose the crash trigger via debugfs */
2518c2ecf20Sopenharmony_cistatic ssize_t
2528c2ecf20Sopenharmony_cirproc_crash_write(struct file *filp, const char __user *user_buf,
2538c2ecf20Sopenharmony_ci		  size_t count, loff_t *ppos)
2548c2ecf20Sopenharmony_ci{
2558c2ecf20Sopenharmony_ci	struct rproc *rproc = filp->private_data;
2568c2ecf20Sopenharmony_ci	unsigned int type;
2578c2ecf20Sopenharmony_ci	int ret;
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	ret = kstrtouint_from_user(user_buf, count, 0, &type);
2608c2ecf20Sopenharmony_ci	if (ret < 0)
2618c2ecf20Sopenharmony_ci		return ret;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	rproc_report_crash(rproc, type);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	return count;
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic const struct file_operations rproc_crash_ops = {
2698c2ecf20Sopenharmony_ci	.write = rproc_crash_write,
2708c2ecf20Sopenharmony_ci	.open = simple_open,
2718c2ecf20Sopenharmony_ci	.llseek = generic_file_llseek,
2728c2ecf20Sopenharmony_ci};
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci/* Expose resource table content via debugfs */
2758c2ecf20Sopenharmony_cistatic int rproc_rsc_table_show(struct seq_file *seq, void *p)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	static const char * const types[] = {"carveout", "devmem", "trace", "vdev"};
2788c2ecf20Sopenharmony_ci	struct rproc *rproc = seq->private;
2798c2ecf20Sopenharmony_ci	struct resource_table *table = rproc->table_ptr;
2808c2ecf20Sopenharmony_ci	struct fw_rsc_carveout *c;
2818c2ecf20Sopenharmony_ci	struct fw_rsc_devmem *d;
2828c2ecf20Sopenharmony_ci	struct fw_rsc_trace *t;
2838c2ecf20Sopenharmony_ci	struct fw_rsc_vdev *v;
2848c2ecf20Sopenharmony_ci	int i, j;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	if (!table) {
2878c2ecf20Sopenharmony_ci		seq_puts(seq, "No resource table found\n");
2888c2ecf20Sopenharmony_ci		return 0;
2898c2ecf20Sopenharmony_ci	}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	for (i = 0; i < table->num; i++) {
2928c2ecf20Sopenharmony_ci		int offset = table->offset[i];
2938c2ecf20Sopenharmony_ci		struct fw_rsc_hdr *hdr = (void *)table + offset;
2948c2ecf20Sopenharmony_ci		void *rsc = (void *)hdr + sizeof(*hdr);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci		switch (hdr->type) {
2978c2ecf20Sopenharmony_ci		case RSC_CARVEOUT:
2988c2ecf20Sopenharmony_ci			c = rsc;
2998c2ecf20Sopenharmony_ci			seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
3008c2ecf20Sopenharmony_ci			seq_printf(seq, "  Device Address 0x%x\n", c->da);
3018c2ecf20Sopenharmony_ci			seq_printf(seq, "  Physical Address 0x%x\n", c->pa);
3028c2ecf20Sopenharmony_ci			seq_printf(seq, "  Length 0x%x Bytes\n", c->len);
3038c2ecf20Sopenharmony_ci			seq_printf(seq, "  Flags 0x%x\n", c->flags);
3048c2ecf20Sopenharmony_ci			seq_printf(seq, "  Reserved (should be zero) [%d]\n", c->reserved);
3058c2ecf20Sopenharmony_ci			seq_printf(seq, "  Name %s\n\n", c->name);
3068c2ecf20Sopenharmony_ci			break;
3078c2ecf20Sopenharmony_ci		case RSC_DEVMEM:
3088c2ecf20Sopenharmony_ci			d = rsc;
3098c2ecf20Sopenharmony_ci			seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
3108c2ecf20Sopenharmony_ci			seq_printf(seq, "  Device Address 0x%x\n", d->da);
3118c2ecf20Sopenharmony_ci			seq_printf(seq, "  Physical Address 0x%x\n", d->pa);
3128c2ecf20Sopenharmony_ci			seq_printf(seq, "  Length 0x%x Bytes\n", d->len);
3138c2ecf20Sopenharmony_ci			seq_printf(seq, "  Flags 0x%x\n", d->flags);
3148c2ecf20Sopenharmony_ci			seq_printf(seq, "  Reserved (should be zero) [%d]\n", d->reserved);
3158c2ecf20Sopenharmony_ci			seq_printf(seq, "  Name %s\n\n", d->name);
3168c2ecf20Sopenharmony_ci			break;
3178c2ecf20Sopenharmony_ci		case RSC_TRACE:
3188c2ecf20Sopenharmony_ci			t = rsc;
3198c2ecf20Sopenharmony_ci			seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
3208c2ecf20Sopenharmony_ci			seq_printf(seq, "  Device Address 0x%x\n", t->da);
3218c2ecf20Sopenharmony_ci			seq_printf(seq, "  Length 0x%x Bytes\n", t->len);
3228c2ecf20Sopenharmony_ci			seq_printf(seq, "  Reserved (should be zero) [%d]\n", t->reserved);
3238c2ecf20Sopenharmony_ci			seq_printf(seq, "  Name %s\n\n", t->name);
3248c2ecf20Sopenharmony_ci			break;
3258c2ecf20Sopenharmony_ci		case RSC_VDEV:
3268c2ecf20Sopenharmony_ci			v = rsc;
3278c2ecf20Sopenharmony_ci			seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci			seq_printf(seq, "  ID %d\n", v->id);
3308c2ecf20Sopenharmony_ci			seq_printf(seq, "  Notify ID %d\n", v->notifyid);
3318c2ecf20Sopenharmony_ci			seq_printf(seq, "  Device features 0x%x\n", v->dfeatures);
3328c2ecf20Sopenharmony_ci			seq_printf(seq, "  Guest features 0x%x\n", v->gfeatures);
3338c2ecf20Sopenharmony_ci			seq_printf(seq, "  Config length 0x%x\n", v->config_len);
3348c2ecf20Sopenharmony_ci			seq_printf(seq, "  Status 0x%x\n", v->status);
3358c2ecf20Sopenharmony_ci			seq_printf(seq, "  Number of vrings %d\n", v->num_of_vrings);
3368c2ecf20Sopenharmony_ci			seq_printf(seq, "  Reserved (should be zero) [%d][%d]\n\n",
3378c2ecf20Sopenharmony_ci				   v->reserved[0], v->reserved[1]);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci			for (j = 0; j < v->num_of_vrings; j++) {
3408c2ecf20Sopenharmony_ci				seq_printf(seq, "  Vring %d\n", j);
3418c2ecf20Sopenharmony_ci				seq_printf(seq, "    Device Address 0x%x\n", v->vring[j].da);
3428c2ecf20Sopenharmony_ci				seq_printf(seq, "    Alignment %d\n", v->vring[j].align);
3438c2ecf20Sopenharmony_ci				seq_printf(seq, "    Number of buffers %d\n", v->vring[j].num);
3448c2ecf20Sopenharmony_ci				seq_printf(seq, "    Notify ID %d\n", v->vring[j].notifyid);
3458c2ecf20Sopenharmony_ci				seq_printf(seq, "    Physical Address 0x%x\n\n",
3468c2ecf20Sopenharmony_ci					   v->vring[j].pa);
3478c2ecf20Sopenharmony_ci			}
3488c2ecf20Sopenharmony_ci			break;
3498c2ecf20Sopenharmony_ci		default:
3508c2ecf20Sopenharmony_ci			seq_printf(seq, "Unknown resource type found: %d [hdr: %pK]\n",
3518c2ecf20Sopenharmony_ci				   hdr->type, hdr);
3528c2ecf20Sopenharmony_ci			break;
3538c2ecf20Sopenharmony_ci		}
3548c2ecf20Sopenharmony_ci	}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	return 0;
3578c2ecf20Sopenharmony_ci}
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(rproc_rsc_table);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci/* Expose carveout content via debugfs */
3628c2ecf20Sopenharmony_cistatic int rproc_carveouts_show(struct seq_file *seq, void *p)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	struct rproc *rproc = seq->private;
3658c2ecf20Sopenharmony_ci	struct rproc_mem_entry *carveout;
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	list_for_each_entry(carveout, &rproc->carveouts, node) {
3688c2ecf20Sopenharmony_ci		seq_puts(seq, "Carveout memory entry:\n");
3698c2ecf20Sopenharmony_ci		seq_printf(seq, "\tName: %s\n", carveout->name);
3708c2ecf20Sopenharmony_ci		seq_printf(seq, "\tVirtual address: %pK\n", carveout->va);
3718c2ecf20Sopenharmony_ci		seq_printf(seq, "\tDMA address: %pad\n", &carveout->dma);
3728c2ecf20Sopenharmony_ci		seq_printf(seq, "\tDevice address: 0x%x\n", carveout->da);
3738c2ecf20Sopenharmony_ci		seq_printf(seq, "\tLength: 0x%zx Bytes\n\n", carveout->len);
3748c2ecf20Sopenharmony_ci	}
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return 0;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ciDEFINE_SHOW_ATTRIBUTE(rproc_carveouts);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_civoid rproc_remove_trace_file(struct dentry *tfile)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	debugfs_remove(tfile);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistruct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
3878c2ecf20Sopenharmony_ci				       struct rproc_debug_trace *trace)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	struct dentry *tfile;
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace,
3928c2ecf20Sopenharmony_ci				    &trace_rproc_ops);
3938c2ecf20Sopenharmony_ci	if (!tfile) {
3948c2ecf20Sopenharmony_ci		dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
3958c2ecf20Sopenharmony_ci		return NULL;
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	return tfile;
3998c2ecf20Sopenharmony_ci}
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_civoid rproc_delete_debug_dir(struct rproc *rproc)
4028c2ecf20Sopenharmony_ci{
4038c2ecf20Sopenharmony_ci	debugfs_remove_recursive(rproc->dbg_dir);
4048c2ecf20Sopenharmony_ci}
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_civoid rproc_create_debug_dir(struct rproc *rproc)
4078c2ecf20Sopenharmony_ci{
4088c2ecf20Sopenharmony_ci	struct device *dev = &rproc->dev;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (!rproc_dbg)
4118c2ecf20Sopenharmony_ci		return;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
4148c2ecf20Sopenharmony_ci	if (!rproc->dbg_dir)
4158c2ecf20Sopenharmony_ci		return;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	debugfs_create_file("name", 0400, rproc->dbg_dir,
4188c2ecf20Sopenharmony_ci			    rproc, &rproc_name_ops);
4198c2ecf20Sopenharmony_ci	debugfs_create_file("recovery", 0600, rproc->dbg_dir,
4208c2ecf20Sopenharmony_ci			    rproc, &rproc_recovery_ops);
4218c2ecf20Sopenharmony_ci	debugfs_create_file("crash", 0200, rproc->dbg_dir,
4228c2ecf20Sopenharmony_ci			    rproc, &rproc_crash_ops);
4238c2ecf20Sopenharmony_ci	debugfs_create_file("resource_table", 0400, rproc->dbg_dir,
4248c2ecf20Sopenharmony_ci			    rproc, &rproc_rsc_table_fops);
4258c2ecf20Sopenharmony_ci	debugfs_create_file("carveout_memories", 0400, rproc->dbg_dir,
4268c2ecf20Sopenharmony_ci			    rproc, &rproc_carveouts_fops);
4278c2ecf20Sopenharmony_ci	debugfs_create_file("coredump", 0600, rproc->dbg_dir,
4288c2ecf20Sopenharmony_ci			    rproc, &rproc_coredump_fops);
4298c2ecf20Sopenharmony_ci}
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_civoid __init rproc_init_debugfs(void)
4328c2ecf20Sopenharmony_ci{
4338c2ecf20Sopenharmony_ci	if (debugfs_initialized()) {
4348c2ecf20Sopenharmony_ci		rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
4358c2ecf20Sopenharmony_ci		if (!rproc_dbg)
4368c2ecf20Sopenharmony_ci			pr_err("can't create debugfs dir\n");
4378c2ecf20Sopenharmony_ci	}
4388c2ecf20Sopenharmony_ci}
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_civoid __exit rproc_exit_debugfs(void)
4418c2ecf20Sopenharmony_ci{
4428c2ecf20Sopenharmony_ci	debugfs_remove(rproc_dbg);
4438c2ecf20Sopenharmony_ci}
444