blob: d734cadb16e3ee17a9a1ec172cd35be4b053e813 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +02002/*
3 * Remote Processor Framework
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
7 *
8 * Ohad Ben-Cohen <ohad@wizery.com>
9 * Mark Grosen <mgrosen@ti.com>
10 * Brian Swetland <swetland@google.com>
11 * Fernando Guzman Lugo <fernando.lugo@ti.com>
12 * Suman Anna <s-anna@ti.com>
13 * Robert Tivy <rtivy@ti.com>
14 * Armando Uribe De Leon <x0095078@ti.com>
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020015 */
16
17#define pr_fmt(fmt) "%s: " fmt, __func__
18
19#include <linux/kernel.h>
20#include <linux/debugfs.h>
21#include <linux/remoteproc.h>
22#include <linux/device.h>
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +030023#include <linux/uaccess.h>
24
25#include "remoteproc_internal.h"
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020026
27/* remoteproc debugfs parent dir */
28static struct dentry *rproc_dbg;
29
30/*
31 * Some remote processors may support dumping trace logs into a shared
32 * memory buffer. We expose this trace buffer using debugfs, so users
33 * can easily tell what's going on remotely.
34 *
35 * We will most probably improve the rproc tracing facilities later on,
36 * but this kind of lightweight and simple mechanism is always good to have,
37 * as it provides very early tracing with little to no dependencies at all.
38 */
39static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf,
Anna, Suman730f84c2016-08-12 18:42:20 -050040 size_t count, loff_t *ppos)
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020041{
Loic Pallardya987e6b92019-01-10 14:49:10 +010042 struct rproc_debug_trace *data = filp->private_data;
43 struct rproc_mem_entry *trace = &data->trace_mem;
44 void *va;
45 char buf[100];
46 int len;
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020047
Loic Pallardya987e6b92019-01-10 14:49:10 +010048 va = rproc_da_to_va(data->rproc, trace->da, trace->len);
49
50 if (!va) {
51 len = scnprintf(buf, sizeof(buf), "Trace %s not available\n",
52 trace->name);
53 va = buf;
54 } else {
55 len = strnlen(va, trace->len);
56 }
57
58 return simple_read_from_buffer(userbuf, count, ppos, va, len);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020059}
60
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020061static const struct file_operations trace_rproc_ops = {
62 .read = rproc_trace_read,
Stephen Boyd234e3402012-04-05 14:25:11 -070063 .open = simple_open,
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020064 .llseek = generic_file_llseek,
65};
66
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020067/* expose the name of the remote processor via debugfs */
68static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
Anna, Suman730f84c2016-08-12 18:42:20 -050069 size_t count, loff_t *ppos)
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020070{
71 struct rproc *rproc = filp->private_data;
72 /* need room for the name, a newline and a terminating null */
73 char buf[100];
74 int i;
75
Dan Carpenterae768d52012-09-25 10:02:51 +030076 i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020077
78 return simple_read_from_buffer(userbuf, count, ppos, buf, i);
79}
80
81static const struct file_operations rproc_name_ops = {
82 .read = rproc_name_read,
Stephen Boyd234e3402012-04-05 14:25:11 -070083 .open = simple_open,
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +020084 .llseek = generic_file_llseek,
85};
86
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +030087/* expose recovery flag via debugfs */
88static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf,
89 size_t count, loff_t *ppos)
90{
91 struct rproc *rproc = filp->private_data;
92 char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n";
93
94 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
95}
96
97/*
98 * By writing to the 'recovery' debugfs entry, we control the behavior of the
99 * recovery mechanism dynamically. The default value of this entry is "enabled".
100 *
101 * The 'recovery' debugfs entry supports these commands:
102 *
103 * enabled: When enabled, the remote processor will be automatically
104 * recovered whenever it crashes. Moreover, if the remote
105 * processor crashes while recovery is disabled, it will
106 * be automatically recovered too as soon as recovery is enabled.
107 *
108 * disabled: When disabled, a remote processor will remain in a crashed
109 * state if it crashes. This is useful for debugging purposes;
110 * without it, debugging a crash is substantially harder.
111 *
112 * recover: This function will trigger an immediate recovery if the
113 * remote processor is in a crashed state, without changing
114 * or checking the recovery state (enabled/disabled).
115 * This is useful during debugging sessions, when one expects
116 * additional crashes to happen after enabling recovery. In this
117 * case, enabling recovery will make it hard to debug subsequent
118 * crashes, so it's recommended to keep recovery disabled, and
119 * instead use the "recover" command as needed.
120 */
121static ssize_t
122rproc_recovery_write(struct file *filp, const char __user *user_buf,
123 size_t count, loff_t *ppos)
124{
125 struct rproc *rproc = filp->private_data;
126 char buf[10];
127 int ret;
128
Arnd Bergmann92792e42015-11-20 18:26:07 +0100129 if (count < 1 || count > sizeof(buf))
Lee Jones47fff9f2016-01-12 12:46:15 +0000130 return -EINVAL;
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300131
132 ret = copy_from_user(buf, user_buf, count);
133 if (ret)
Dan Carpenterbec109a2012-09-25 10:05:33 +0300134 return -EFAULT;
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300135
136 /* remove end of line */
137 if (buf[count - 1] == '\n')
138 buf[count - 1] = '\0';
139
140 if (!strncmp(buf, "enabled", count)) {
Alex Eldere138cce2020-02-28 12:33:57 -0600141 /* change the flag and begin the recovery process if needed */
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300142 rproc->recovery_disabled = false;
Alex Eldere138cce2020-02-28 12:33:57 -0600143 rproc_trigger_recovery(rproc);
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300144 } else if (!strncmp(buf, "disabled", count)) {
145 rproc->recovery_disabled = true;
146 } else if (!strncmp(buf, "recover", count)) {
Alex Eldere138cce2020-02-28 12:33:57 -0600147 /* begin the recovery process without changing the flag */
148 rproc_trigger_recovery(rproc);
Alex Elder1f2f65c2020-02-28 12:33:59 -0600149 } else {
150 return -EINVAL;
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300151 }
152
153 return count;
154}
155
156static const struct file_operations rproc_recovery_ops = {
157 .read = rproc_recovery_read,
158 .write = rproc_recovery_write,
159 .open = simple_open,
160 .llseek = generic_file_llseek,
161};
162
Xiang Xiao60042a22018-11-07 23:26:01 +0800163/* expose the crash trigger via debugfs */
164static ssize_t
165rproc_crash_write(struct file *filp, const char __user *user_buf,
166 size_t count, loff_t *ppos)
167{
168 struct rproc *rproc = filp->private_data;
169 unsigned int type;
170 int ret;
171
172 ret = kstrtouint_from_user(user_buf, count, 0, &type);
173 if (ret < 0)
174 return ret;
175
176 rproc_report_crash(rproc, type);
177
178 return count;
179}
180
181static const struct file_operations rproc_crash_ops = {
182 .write = rproc_crash_write,
183 .open = simple_open,
184 .llseek = generic_file_llseek,
185};
186
Loic Pallardybdd8edb2017-11-06 18:09:55 +0100187/* Expose resource table content via debugfs */
188static int rproc_rsc_table_show(struct seq_file *seq, void *p)
189{
190 static const char * const types[] = {"carveout", "devmem", "trace", "vdev"};
191 struct rproc *rproc = seq->private;
192 struct resource_table *table = rproc->table_ptr;
193 struct fw_rsc_carveout *c;
194 struct fw_rsc_devmem *d;
195 struct fw_rsc_trace *t;
196 struct fw_rsc_vdev *v;
197 int i, j;
198
199 if (!table) {
200 seq_puts(seq, "No resource table found\n");
201 return 0;
202 }
203
204 for (i = 0; i < table->num; i++) {
205 int offset = table->offset[i];
206 struct fw_rsc_hdr *hdr = (void *)table + offset;
207 void *rsc = (void *)hdr + sizeof(*hdr);
208
209 switch (hdr->type) {
210 case RSC_CARVEOUT:
211 c = rsc;
212 seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
213 seq_printf(seq, " Device Address 0x%x\n", c->da);
214 seq_printf(seq, " Physical Address 0x%x\n", c->pa);
215 seq_printf(seq, " Length 0x%x Bytes\n", c->len);
216 seq_printf(seq, " Flags 0x%x\n", c->flags);
217 seq_printf(seq, " Reserved (should be zero) [%d]\n", c->reserved);
218 seq_printf(seq, " Name %s\n\n", c->name);
219 break;
220 case RSC_DEVMEM:
221 d = rsc;
222 seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
223 seq_printf(seq, " Device Address 0x%x\n", d->da);
224 seq_printf(seq, " Physical Address 0x%x\n", d->pa);
225 seq_printf(seq, " Length 0x%x Bytes\n", d->len);
226 seq_printf(seq, " Flags 0x%x\n", d->flags);
227 seq_printf(seq, " Reserved (should be zero) [%d]\n", d->reserved);
228 seq_printf(seq, " Name %s\n\n", d->name);
229 break;
230 case RSC_TRACE:
231 t = rsc;
232 seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
233 seq_printf(seq, " Device Address 0x%x\n", t->da);
234 seq_printf(seq, " Length 0x%x Bytes\n", t->len);
235 seq_printf(seq, " Reserved (should be zero) [%d]\n", t->reserved);
236 seq_printf(seq, " Name %s\n\n", t->name);
237 break;
238 case RSC_VDEV:
239 v = rsc;
240 seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
241
242 seq_printf(seq, " ID %d\n", v->id);
243 seq_printf(seq, " Notify ID %d\n", v->notifyid);
244 seq_printf(seq, " Device features 0x%x\n", v->dfeatures);
245 seq_printf(seq, " Guest features 0x%x\n", v->gfeatures);
246 seq_printf(seq, " Config length 0x%x\n", v->config_len);
247 seq_printf(seq, " Status 0x%x\n", v->status);
248 seq_printf(seq, " Number of vrings %d\n", v->num_of_vrings);
249 seq_printf(seq, " Reserved (should be zero) [%d][%d]\n\n",
250 v->reserved[0], v->reserved[1]);
251
252 for (j = 0; j < v->num_of_vrings; j++) {
253 seq_printf(seq, " Vring %d\n", j);
254 seq_printf(seq, " Device Address 0x%x\n", v->vring[j].da);
255 seq_printf(seq, " Alignment %d\n", v->vring[j].align);
256 seq_printf(seq, " Number of buffers %d\n", v->vring[j].num);
257 seq_printf(seq, " Notify ID %d\n", v->vring[j].notifyid);
258 seq_printf(seq, " Physical Address 0x%x\n\n",
259 v->vring[j].pa);
260 }
261 break;
262 default:
Loic Pallardy276ec992018-07-06 14:38:27 +0200263 seq_printf(seq, "Unknown resource type found: %d [hdr: %pK]\n",
Loic Pallardybdd8edb2017-11-06 18:09:55 +0100264 hdr->type, hdr);
265 break;
266 }
267 }
268
269 return 0;
270}
271
272static int rproc_rsc_table_open(struct inode *inode, struct file *file)
273{
274 return single_open(file, rproc_rsc_table_show, inode->i_private);
275}
276
277static const struct file_operations rproc_rsc_table_ops = {
278 .open = rproc_rsc_table_open,
279 .read = seq_read,
280 .llseek = seq_lseek,
281 .release = single_release,
282};
283
Loic Pallardyb8918832017-11-06 18:09:56 +0100284/* Expose carveout content via debugfs */
285static int rproc_carveouts_show(struct seq_file *seq, void *p)
286{
287 struct rproc *rproc = seq->private;
288 struct rproc_mem_entry *carveout;
289
290 list_for_each_entry(carveout, &rproc->carveouts, node) {
291 seq_puts(seq, "Carveout memory entry:\n");
Loic Pallardy32652302018-07-27 15:14:39 +0200292 seq_printf(seq, "\tName: %s\n", carveout->name);
Loic Pallardy276ec992018-07-06 14:38:27 +0200293 seq_printf(seq, "\tVirtual address: %pK\n", carveout->va);
Loic Pallardyb8918832017-11-06 18:09:56 +0100294 seq_printf(seq, "\tDMA address: %pad\n", &carveout->dma);
295 seq_printf(seq, "\tDevice address: 0x%x\n", carveout->da);
Clement Leger096ee782020-03-02 10:38:56 +0100296 seq_printf(seq, "\tLength: 0x%zx Bytes\n\n", carveout->len);
Loic Pallardyb8918832017-11-06 18:09:56 +0100297 }
298
299 return 0;
300}
301
302static int rproc_carveouts_open(struct inode *inode, struct file *file)
303{
304 return single_open(file, rproc_carveouts_show, inode->i_private);
305}
306
307static const struct file_operations rproc_carveouts_ops = {
308 .open = rproc_carveouts_open,
309 .read = seq_read,
310 .llseek = seq_lseek,
311 .release = single_release,
312};
313
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200314void rproc_remove_trace_file(struct dentry *tfile)
315{
316 debugfs_remove(tfile);
317}
318
319struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
Loic Pallardya987e6b92019-01-10 14:49:10 +0100320 struct rproc_debug_trace *trace)
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200321{
322 struct dentry *tfile;
323
Anna, Suman730f84c2016-08-12 18:42:20 -0500324 tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace,
325 &trace_rproc_ops);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200326 if (!tfile) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300327 dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200328 return NULL;
329 }
330
331 return tfile;
332}
333
334void rproc_delete_debug_dir(struct rproc *rproc)
335{
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200336 debugfs_remove_recursive(rproc->dbg_dir);
337}
338
339void rproc_create_debug_dir(struct rproc *rproc)
340{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300341 struct device *dev = &rproc->dev;
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200342
343 if (!rproc_dbg)
344 return;
345
346 rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
347 if (!rproc->dbg_dir)
348 return;
349
350 debugfs_create_file("name", 0400, rproc->dbg_dir,
Anna, Suman730f84c2016-08-12 18:42:20 -0500351 rproc, &rproc_name_ops);
Alex Eldere138cce2020-02-28 12:33:57 -0600352 debugfs_create_file("recovery", 0600, rproc->dbg_dir,
Anna, Suman730f84c2016-08-12 18:42:20 -0500353 rproc, &rproc_recovery_ops);
Xiang Xiao60042a22018-11-07 23:26:01 +0800354 debugfs_create_file("crash", 0200, rproc->dbg_dir,
355 rproc, &rproc_crash_ops);
Loic Pallardybdd8edb2017-11-06 18:09:55 +0100356 debugfs_create_file("resource_table", 0400, rproc->dbg_dir,
357 rproc, &rproc_rsc_table_ops);
Loic Pallardyb8918832017-11-06 18:09:56 +0100358 debugfs_create_file("carveout_memories", 0400, rproc->dbg_dir,
359 rproc, &rproc_carveouts_ops);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200360}
361
362void __init rproc_init_debugfs(void)
363{
364 if (debugfs_initialized()) {
365 rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
366 if (!rproc_dbg)
367 pr_err("can't create debugfs dir\n");
368 }
369}
370
371void __exit rproc_exit_debugfs(void)
372{
Suman Anna57971152013-06-30 11:33:05 +0300373 debugfs_remove(rproc_dbg);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200374}