1 /*
2  * Remote Processor Framework
3  *
4  * Copyright (C) 2011 Texas Instruments, Inc.
5  * Copyright (C) 2011 Google, Inc.
6  *
7  * Ohad Ben-Cohen <ohad@wizery.com>
8  * Mark Grosen <mgrosen@ti.com>
9  * Brian Swetland <swetland@google.com>
10  * Fernando Guzman Lugo <fernando.lugo@ti.com>
11  * Suman Anna <s-anna@ti.com>
12  * Robert Tivy <rtivy@ti.com>
13  * Armando Uribe De Leon <x0095078@ti.com>
14  *
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * version 2 as published by the Free Software Foundation.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  */
24 
25 #define pr_fmt(fmt)    "%s: " fmt, __func__
26 
27 #include <linux/kernel.h>
28 #include <linux/debugfs.h>
29 #include <linux/remoteproc.h>
30 #include <linux/device.h>
31 #include <linux/uaccess.h>
32 
33 #include "remoteproc_internal.h"
34 
35 /* remoteproc debugfs parent dir */
36 static struct dentry *rproc_dbg;
37 
38 /*
39  * Some remote processors may support dumping trace logs into a shared
40  * memory buffer. We expose this trace buffer using debugfs, so users
41  * can easily tell what's going on remotely.
42  *
43  * We will most probably improve the rproc tracing facilities later on,
44  * but this kind of lightweight and simple mechanism is always good to have,
45  * as it provides very early tracing with little to no dependencies at all.
46  */
47 static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf,
48 				size_t count, loff_t *ppos)
49 {
50 	struct rproc_mem_entry *trace = filp->private_data;
51 	int len = strnlen(trace->va, trace->len);
52 
53 	return simple_read_from_buffer(userbuf, count, ppos, trace->va, len);
54 }
55 
56 static const struct file_operations trace_rproc_ops = {
57 	.read = rproc_trace_read,
58 	.open = simple_open,
59 	.llseek	= generic_file_llseek,
60 };
61 
62 /* expose the name of the remote processor via debugfs */
63 static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
64 			       size_t count, loff_t *ppos)
65 {
66 	struct rproc *rproc = filp->private_data;
67 	/* need room for the name, a newline and a terminating null */
68 	char buf[100];
69 	int i;
70 
71 	i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
72 
73 	return simple_read_from_buffer(userbuf, count, ppos, buf, i);
74 }
75 
76 static const struct file_operations rproc_name_ops = {
77 	.read = rproc_name_read,
78 	.open = simple_open,
79 	.llseek	= generic_file_llseek,
80 };
81 
82 /* expose recovery flag via debugfs */
83 static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf,
84 				   size_t count, loff_t *ppos)
85 {
86 	struct rproc *rproc = filp->private_data;
87 	char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n";
88 
89 	return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
90 }
91 
92 /*
93  * By writing to the 'recovery' debugfs entry, we control the behavior of the
94  * recovery mechanism dynamically. The default value of this entry is "enabled".
95  *
96  * The 'recovery' debugfs entry supports these commands:
97  *
98  * enabled:	When enabled, the remote processor will be automatically
99  *		recovered whenever it crashes. Moreover, if the remote
100  *		processor crashes while recovery is disabled, it will
101  *		be automatically recovered too as soon as recovery is enabled.
102  *
103  * disabled:	When disabled, a remote processor will remain in a crashed
104  *		state if it crashes. This is useful for debugging purposes;
105  *		without it, debugging a crash is substantially harder.
106  *
107  * recover:	This function will trigger an immediate recovery if the
108  *		remote processor is in a crashed state, without changing
109  *		or checking the recovery state (enabled/disabled).
110  *		This is useful during debugging sessions, when one expects
111  *		additional crashes to happen after enabling recovery. In this
112  *		case, enabling recovery will make it hard to debug subsequent
113  *		crashes, so it's recommended to keep recovery disabled, and
114  *		instead use the "recover" command as needed.
115  */
116 static ssize_t
117 rproc_recovery_write(struct file *filp, const char __user *user_buf,
118 		     size_t count, loff_t *ppos)
119 {
120 	struct rproc *rproc = filp->private_data;
121 	char buf[10];
122 	int ret;
123 
124 	if (count < 1 || count > sizeof(buf))
125 		return -EINVAL;
126 
127 	ret = copy_from_user(buf, user_buf, count);
128 	if (ret)
129 		return -EFAULT;
130 
131 	/* remove end of line */
132 	if (buf[count - 1] == '\n')
133 		buf[count - 1] = '\0';
134 
135 	if (!strncmp(buf, "enabled", count)) {
136 		rproc->recovery_disabled = false;
137 		/* if rproc has crashed, trigger recovery */
138 		if (rproc->state == RPROC_CRASHED)
139 			rproc_trigger_recovery(rproc);
140 	} else if (!strncmp(buf, "disabled", count)) {
141 		rproc->recovery_disabled = true;
142 	} else if (!strncmp(buf, "recover", count)) {
143 		/* if rproc has crashed, trigger recovery */
144 		if (rproc->state == RPROC_CRASHED)
145 			rproc_trigger_recovery(rproc);
146 	}
147 
148 	return count;
149 }
150 
151 static const struct file_operations rproc_recovery_ops = {
152 	.read = rproc_recovery_read,
153 	.write = rproc_recovery_write,
154 	.open = simple_open,
155 	.llseek = generic_file_llseek,
156 };
157 
158 /* Expose resource table content via debugfs */
159 static int rproc_rsc_table_show(struct seq_file *seq, void *p)
160 {
161 	static const char * const types[] = {"carveout", "devmem", "trace", "vdev"};
162 	struct rproc *rproc = seq->private;
163 	struct resource_table *table = rproc->table_ptr;
164 	struct fw_rsc_carveout *c;
165 	struct fw_rsc_devmem *d;
166 	struct fw_rsc_trace *t;
167 	struct fw_rsc_vdev *v;
168 	int i, j;
169 
170 	if (!table) {
171 		seq_puts(seq, "No resource table found\n");
172 		return 0;
173 	}
174 
175 	for (i = 0; i < table->num; i++) {
176 		int offset = table->offset[i];
177 		struct fw_rsc_hdr *hdr = (void *)table + offset;
178 		void *rsc = (void *)hdr + sizeof(*hdr);
179 
180 		switch (hdr->type) {
181 		case RSC_CARVEOUT:
182 			c = rsc;
183 			seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
184 			seq_printf(seq, "  Device Address 0x%x\n", c->da);
185 			seq_printf(seq, "  Physical Address 0x%x\n", c->pa);
186 			seq_printf(seq, "  Length 0x%x Bytes\n", c->len);
187 			seq_printf(seq, "  Flags 0x%x\n", c->flags);
188 			seq_printf(seq, "  Reserved (should be zero) [%d]\n", c->reserved);
189 			seq_printf(seq, "  Name %s\n\n", c->name);
190 			break;
191 		case RSC_DEVMEM:
192 			d = rsc;
193 			seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
194 			seq_printf(seq, "  Device Address 0x%x\n", d->da);
195 			seq_printf(seq, "  Physical Address 0x%x\n", d->pa);
196 			seq_printf(seq, "  Length 0x%x Bytes\n", d->len);
197 			seq_printf(seq, "  Flags 0x%x\n", d->flags);
198 			seq_printf(seq, "  Reserved (should be zero) [%d]\n", d->reserved);
199 			seq_printf(seq, "  Name %s\n\n", d->name);
200 			break;
201 		case RSC_TRACE:
202 			t = rsc;
203 			seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
204 			seq_printf(seq, "  Device Address 0x%x\n", t->da);
205 			seq_printf(seq, "  Length 0x%x Bytes\n", t->len);
206 			seq_printf(seq, "  Reserved (should be zero) [%d]\n", t->reserved);
207 			seq_printf(seq, "  Name %s\n\n", t->name);
208 			break;
209 		case RSC_VDEV:
210 			v = rsc;
211 			seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
212 
213 			seq_printf(seq, "  ID %d\n", v->id);
214 			seq_printf(seq, "  Notify ID %d\n", v->notifyid);
215 			seq_printf(seq, "  Device features 0x%x\n", v->dfeatures);
216 			seq_printf(seq, "  Guest features 0x%x\n", v->gfeatures);
217 			seq_printf(seq, "  Config length 0x%x\n", v->config_len);
218 			seq_printf(seq, "  Status 0x%x\n", v->status);
219 			seq_printf(seq, "  Number of vrings %d\n", v->num_of_vrings);
220 			seq_printf(seq, "  Reserved (should be zero) [%d][%d]\n\n",
221 				   v->reserved[0], v->reserved[1]);
222 
223 			for (j = 0; j < v->num_of_vrings; j++) {
224 				seq_printf(seq, "  Vring %d\n", j);
225 				seq_printf(seq, "    Device Address 0x%x\n", v->vring[j].da);
226 				seq_printf(seq, "    Alignment %d\n", v->vring[j].align);
227 				seq_printf(seq, "    Number of buffers %d\n", v->vring[j].num);
228 				seq_printf(seq, "    Notify ID %d\n", v->vring[j].notifyid);
229 				seq_printf(seq, "    Physical Address 0x%x\n\n",
230 					   v->vring[j].pa);
231 			}
232 			break;
233 		default:
234 			seq_printf(seq, "Unknown resource type found: %d [hdr: %pK]\n",
235 				   hdr->type, hdr);
236 			break;
237 		}
238 	}
239 
240 	return 0;
241 }
242 
243 static int rproc_rsc_table_open(struct inode *inode, struct file *file)
244 {
245 	return single_open(file, rproc_rsc_table_show, inode->i_private);
246 }
247 
248 static const struct file_operations rproc_rsc_table_ops = {
249 	.open		= rproc_rsc_table_open,
250 	.read		= seq_read,
251 	.llseek		= seq_lseek,
252 	.release	= single_release,
253 };
254 
255 /* Expose carveout content via debugfs */
256 static int rproc_carveouts_show(struct seq_file *seq, void *p)
257 {
258 	struct rproc *rproc = seq->private;
259 	struct rproc_mem_entry *carveout;
260 
261 	list_for_each_entry(carveout, &rproc->carveouts, node) {
262 		seq_puts(seq, "Carveout memory entry:\n");
263 		seq_printf(seq, "\tVirtual address: %pK\n", carveout->va);
264 		seq_printf(seq, "\tDMA address: %pad\n", &carveout->dma);
265 		seq_printf(seq, "\tDevice address: 0x%x\n", carveout->da);
266 		seq_printf(seq, "\tLength: 0x%x Bytes\n\n", carveout->len);
267 	}
268 
269 	return 0;
270 }
271 
272 static int rproc_carveouts_open(struct inode *inode, struct file *file)
273 {
274 	return single_open(file, rproc_carveouts_show, inode->i_private);
275 }
276 
277 static const struct file_operations rproc_carveouts_ops = {
278 	.open		= rproc_carveouts_open,
279 	.read		= seq_read,
280 	.llseek		= seq_lseek,
281 	.release	= single_release,
282 };
283 
284 void rproc_remove_trace_file(struct dentry *tfile)
285 {
286 	debugfs_remove(tfile);
287 }
288 
289 struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
290 				       struct rproc_mem_entry *trace)
291 {
292 	struct dentry *tfile;
293 
294 	tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace,
295 				    &trace_rproc_ops);
296 	if (!tfile) {
297 		dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
298 		return NULL;
299 	}
300 
301 	return tfile;
302 }
303 
304 void rproc_delete_debug_dir(struct rproc *rproc)
305 {
306 	if (!rproc->dbg_dir)
307 		return;
308 
309 	debugfs_remove_recursive(rproc->dbg_dir);
310 }
311 
312 void rproc_create_debug_dir(struct rproc *rproc)
313 {
314 	struct device *dev = &rproc->dev;
315 
316 	if (!rproc_dbg)
317 		return;
318 
319 	rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
320 	if (!rproc->dbg_dir)
321 		return;
322 
323 	debugfs_create_file("name", 0400, rproc->dbg_dir,
324 			    rproc, &rproc_name_ops);
325 	debugfs_create_file("recovery", 0400, rproc->dbg_dir,
326 			    rproc, &rproc_recovery_ops);
327 	debugfs_create_file("resource_table", 0400, rproc->dbg_dir,
328 			    rproc, &rproc_rsc_table_ops);
329 	debugfs_create_file("carveout_memories", 0400, rproc->dbg_dir,
330 			    rproc, &rproc_carveouts_ops);
331 }
332 
333 void __init rproc_init_debugfs(void)
334 {
335 	if (debugfs_initialized()) {
336 		rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
337 		if (!rproc_dbg)
338 			pr_err("can't create debugfs dir\n");
339 	}
340 }
341 
342 void __exit rproc_exit_debugfs(void)
343 {
344 	debugfs_remove(rproc_dbg);
345 }
346