blob: 7ca823f6aa638f6280683222ae497d6f5d44b694 [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/*
Rishabh Bhatnagar3afdc592020-07-16 15:20:35 -070031 * A coredump-configuration-to-string lookup table, for exposing a
32 * human readable configuration via debugfs. Always keep in sync with
33 * enum rproc_coredump_mechanism
34 */
35static const char * const rproc_coredump_str[] = {
36 [RPROC_COREDUMP_DEFAULT] = "default",
37 [RPROC_COREDUMP_INLINE] = "inline",
38 [RPROC_COREDUMP_DISABLED] = "disabled",
39};
40
41/* Expose the current coredump configuration via debugfs */
42static ssize_t rproc_coredump_read(struct file *filp, char __user *userbuf,
43 size_t count, loff_t *ppos)
44{
45 struct rproc *rproc = filp->private_data;
46 char buf[20];
47 int len;
48
49 len = scnprintf(buf, sizeof(buf), "%s\n",
50 rproc_coredump_str[rproc->dump_conf]);
51
52 return simple_read_from_buffer(userbuf, count, ppos, buf, len);
53}
54
55/*
56 * By writing to the 'coredump' debugfs entry, we control the behavior of the
57 * coredump mechanism dynamically. The default value of this entry is "default".
58 *
59 * The 'coredump' debugfs entry supports these commands:
60 *
61 * default: This is the default coredump mechanism. When the remoteproc
62 * crashes the entire coredump will be copied to a separate buffer
63 * and exposed to userspace.
64 *
65 * inline: The coredump will not be copied to a separate buffer and the
66 * recovery process will have to wait until data is read by
67 * userspace. But this avoid usage of extra memory.
68 *
69 * disabled: This will disable coredump. Recovery will proceed without
70 * collecting any dump.
71 */
72static ssize_t rproc_coredump_write(struct file *filp,
73 const char __user *user_buf, size_t count,
74 loff_t *ppos)
75{
76 struct rproc *rproc = filp->private_data;
77 int ret, err = 0;
78 char buf[20];
79
80 if (count > sizeof(buf))
81 return -EINVAL;
82
83 ret = copy_from_user(buf, user_buf, count);
84 if (ret)
85 return -EFAULT;
86
87 /* remove end of line */
88 if (buf[count - 1] == '\n')
89 buf[count - 1] = '\0';
90
91 if (rproc->state == RPROC_CRASHED) {
92 dev_err(&rproc->dev, "can't change coredump configuration\n");
93 err = -EBUSY;
94 goto out;
95 }
96
Sibi Sankar18946222020-09-16 20:21:00 +053097 if (!strncmp(buf, "disabled", count)) {
Rishabh Bhatnagar3afdc592020-07-16 15:20:35 -070098 rproc->dump_conf = RPROC_COREDUMP_DISABLED;
99 } else if (!strncmp(buf, "inline", count)) {
100 rproc->dump_conf = RPROC_COREDUMP_INLINE;
101 } else if (!strncmp(buf, "default", count)) {
102 rproc->dump_conf = RPROC_COREDUMP_DEFAULT;
103 } else {
104 dev_err(&rproc->dev, "Invalid coredump configuration\n");
105 err = -EINVAL;
106 }
107out:
108 return err ? err : count;
109}
110
111static const struct file_operations rproc_coredump_fops = {
112 .read = rproc_coredump_read,
113 .write = rproc_coredump_write,
114 .open = simple_open,
115 .llseek = generic_file_llseek,
116};
117
118/*
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200119 * Some remote processors may support dumping trace logs into a shared
120 * memory buffer. We expose this trace buffer using debugfs, so users
121 * can easily tell what's going on remotely.
122 *
123 * We will most probably improve the rproc tracing facilities later on,
124 * but this kind of lightweight and simple mechanism is always good to have,
125 * as it provides very early tracing with little to no dependencies at all.
126 */
127static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf,
Anna, Suman730f84c2016-08-12 18:42:20 -0500128 size_t count, loff_t *ppos)
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200129{
Loic Pallardya987e6b92019-01-10 14:49:10 +0100130 struct rproc_debug_trace *data = filp->private_data;
131 struct rproc_mem_entry *trace = &data->trace_mem;
132 void *va;
133 char buf[100];
134 int len;
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200135
Loic Pallardya987e6b92019-01-10 14:49:10 +0100136 va = rproc_da_to_va(data->rproc, trace->da, trace->len);
137
138 if (!va) {
139 len = scnprintf(buf, sizeof(buf), "Trace %s not available\n",
140 trace->name);
141 va = buf;
142 } else {
143 len = strnlen(va, trace->len);
144 }
145
146 return simple_read_from_buffer(userbuf, count, ppos, va, len);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200147}
148
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200149static const struct file_operations trace_rproc_ops = {
150 .read = rproc_trace_read,
Stephen Boyd234e3402012-04-05 14:25:11 -0700151 .open = simple_open,
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200152 .llseek = generic_file_llseek,
153};
154
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200155/* expose the name of the remote processor via debugfs */
156static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
Anna, Suman730f84c2016-08-12 18:42:20 -0500157 size_t count, loff_t *ppos)
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200158{
159 struct rproc *rproc = filp->private_data;
160 /* need room for the name, a newline and a terminating null */
161 char buf[100];
162 int i;
163
Dan Carpenterae768d52012-09-25 10:02:51 +0300164 i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200165
166 return simple_read_from_buffer(userbuf, count, ppos, buf, i);
167}
168
169static const struct file_operations rproc_name_ops = {
170 .read = rproc_name_read,
Stephen Boyd234e3402012-04-05 14:25:11 -0700171 .open = simple_open,
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200172 .llseek = generic_file_llseek,
173};
174
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300175/* expose recovery flag via debugfs */
176static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf,
177 size_t count, loff_t *ppos)
178{
179 struct rproc *rproc = filp->private_data;
180 char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n";
181
182 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
183}
184
185/*
186 * By writing to the 'recovery' debugfs entry, we control the behavior of the
187 * recovery mechanism dynamically. The default value of this entry is "enabled".
188 *
189 * The 'recovery' debugfs entry supports these commands:
190 *
191 * enabled: When enabled, the remote processor will be automatically
192 * recovered whenever it crashes. Moreover, if the remote
193 * processor crashes while recovery is disabled, it will
194 * be automatically recovered too as soon as recovery is enabled.
195 *
196 * disabled: When disabled, a remote processor will remain in a crashed
197 * state if it crashes. This is useful for debugging purposes;
198 * without it, debugging a crash is substantially harder.
199 *
200 * recover: This function will trigger an immediate recovery if the
201 * remote processor is in a crashed state, without changing
202 * or checking the recovery state (enabled/disabled).
203 * This is useful during debugging sessions, when one expects
204 * additional crashes to happen after enabling recovery. In this
205 * case, enabling recovery will make it hard to debug subsequent
206 * crashes, so it's recommended to keep recovery disabled, and
207 * instead use the "recover" command as needed.
208 */
209static ssize_t
210rproc_recovery_write(struct file *filp, const char __user *user_buf,
211 size_t count, loff_t *ppos)
212{
213 struct rproc *rproc = filp->private_data;
214 char buf[10];
215 int ret;
216
Arnd Bergmann92792e42015-11-20 18:26:07 +0100217 if (count < 1 || count > sizeof(buf))
Lee Jones47fff9f2016-01-12 12:46:15 +0000218 return -EINVAL;
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300219
220 ret = copy_from_user(buf, user_buf, count);
221 if (ret)
Dan Carpenterbec109a2012-09-25 10:05:33 +0300222 return -EFAULT;
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300223
224 /* remove end of line */
225 if (buf[count - 1] == '\n')
226 buf[count - 1] = '\0';
227
228 if (!strncmp(buf, "enabled", count)) {
Alex Eldere138cce2020-02-28 12:33:57 -0600229 /* change the flag and begin the recovery process if needed */
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300230 rproc->recovery_disabled = false;
Alex Eldere138cce2020-02-28 12:33:57 -0600231 rproc_trigger_recovery(rproc);
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300232 } else if (!strncmp(buf, "disabled", count)) {
233 rproc->recovery_disabled = true;
234 } else if (!strncmp(buf, "recover", count)) {
Alex Eldere138cce2020-02-28 12:33:57 -0600235 /* begin the recovery process without changing the flag */
236 rproc_trigger_recovery(rproc);
Alex Elder1f2f65c2020-02-28 12:33:59 -0600237 } else {
238 return -EINVAL;
Fernando Guzman Lugo2e37abb2012-09-18 12:26:35 +0300239 }
240
241 return count;
242}
243
244static const struct file_operations rproc_recovery_ops = {
245 .read = rproc_recovery_read,
246 .write = rproc_recovery_write,
247 .open = simple_open,
248 .llseek = generic_file_llseek,
249};
250
Xiang Xiao60042a22018-11-07 23:26:01 +0800251/* expose the crash trigger via debugfs */
252static ssize_t
253rproc_crash_write(struct file *filp, const char __user *user_buf,
254 size_t count, loff_t *ppos)
255{
256 struct rproc *rproc = filp->private_data;
257 unsigned int type;
258 int ret;
259
260 ret = kstrtouint_from_user(user_buf, count, 0, &type);
261 if (ret < 0)
262 return ret;
263
264 rproc_report_crash(rproc, type);
265
266 return count;
267}
268
269static const struct file_operations rproc_crash_ops = {
270 .write = rproc_crash_write,
271 .open = simple_open,
272 .llseek = generic_file_llseek,
273};
274
Loic Pallardybdd8edb2017-11-06 18:09:55 +0100275/* Expose resource table content via debugfs */
276static int rproc_rsc_table_show(struct seq_file *seq, void *p)
277{
278 static const char * const types[] = {"carveout", "devmem", "trace", "vdev"};
279 struct rproc *rproc = seq->private;
280 struct resource_table *table = rproc->table_ptr;
281 struct fw_rsc_carveout *c;
282 struct fw_rsc_devmem *d;
283 struct fw_rsc_trace *t;
284 struct fw_rsc_vdev *v;
285 int i, j;
286
287 if (!table) {
288 seq_puts(seq, "No resource table found\n");
289 return 0;
290 }
291
292 for (i = 0; i < table->num; i++) {
293 int offset = table->offset[i];
294 struct fw_rsc_hdr *hdr = (void *)table + offset;
295 void *rsc = (void *)hdr + sizeof(*hdr);
296
297 switch (hdr->type) {
298 case RSC_CARVEOUT:
299 c = rsc;
300 seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
301 seq_printf(seq, " Device Address 0x%x\n", c->da);
302 seq_printf(seq, " Physical Address 0x%x\n", c->pa);
303 seq_printf(seq, " Length 0x%x Bytes\n", c->len);
304 seq_printf(seq, " Flags 0x%x\n", c->flags);
305 seq_printf(seq, " Reserved (should be zero) [%d]\n", c->reserved);
306 seq_printf(seq, " Name %s\n\n", c->name);
307 break;
308 case RSC_DEVMEM:
309 d = rsc;
310 seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
311 seq_printf(seq, " Device Address 0x%x\n", d->da);
312 seq_printf(seq, " Physical Address 0x%x\n", d->pa);
313 seq_printf(seq, " Length 0x%x Bytes\n", d->len);
314 seq_printf(seq, " Flags 0x%x\n", d->flags);
315 seq_printf(seq, " Reserved (should be zero) [%d]\n", d->reserved);
316 seq_printf(seq, " Name %s\n\n", d->name);
317 break;
318 case RSC_TRACE:
319 t = rsc;
320 seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
321 seq_printf(seq, " Device Address 0x%x\n", t->da);
322 seq_printf(seq, " Length 0x%x Bytes\n", t->len);
323 seq_printf(seq, " Reserved (should be zero) [%d]\n", t->reserved);
324 seq_printf(seq, " Name %s\n\n", t->name);
325 break;
326 case RSC_VDEV:
327 v = rsc;
328 seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
329
330 seq_printf(seq, " ID %d\n", v->id);
331 seq_printf(seq, " Notify ID %d\n", v->notifyid);
332 seq_printf(seq, " Device features 0x%x\n", v->dfeatures);
333 seq_printf(seq, " Guest features 0x%x\n", v->gfeatures);
334 seq_printf(seq, " Config length 0x%x\n", v->config_len);
335 seq_printf(seq, " Status 0x%x\n", v->status);
336 seq_printf(seq, " Number of vrings %d\n", v->num_of_vrings);
337 seq_printf(seq, " Reserved (should be zero) [%d][%d]\n\n",
338 v->reserved[0], v->reserved[1]);
339
340 for (j = 0; j < v->num_of_vrings; j++) {
341 seq_printf(seq, " Vring %d\n", j);
342 seq_printf(seq, " Device Address 0x%x\n", v->vring[j].da);
343 seq_printf(seq, " Alignment %d\n", v->vring[j].align);
344 seq_printf(seq, " Number of buffers %d\n", v->vring[j].num);
345 seq_printf(seq, " Notify ID %d\n", v->vring[j].notifyid);
346 seq_printf(seq, " Physical Address 0x%x\n\n",
347 v->vring[j].pa);
348 }
349 break;
350 default:
Loic Pallardy276ec992018-07-06 14:38:27 +0200351 seq_printf(seq, "Unknown resource type found: %d [hdr: %pK]\n",
Loic Pallardybdd8edb2017-11-06 18:09:55 +0100352 hdr->type, hdr);
353 break;
354 }
355 }
356
357 return 0;
358}
359
Yangtao Lic78bc072018-12-01 10:58:38 -0500360DEFINE_SHOW_ATTRIBUTE(rproc_rsc_table);
Loic Pallardybdd8edb2017-11-06 18:09:55 +0100361
Loic Pallardyb8918832017-11-06 18:09:56 +0100362/* Expose carveout content via debugfs */
363static int rproc_carveouts_show(struct seq_file *seq, void *p)
364{
365 struct rproc *rproc = seq->private;
366 struct rproc_mem_entry *carveout;
367
368 list_for_each_entry(carveout, &rproc->carveouts, node) {
369 seq_puts(seq, "Carveout memory entry:\n");
Loic Pallardy32652302018-07-27 15:14:39 +0200370 seq_printf(seq, "\tName: %s\n", carveout->name);
Loic Pallardy276ec992018-07-06 14:38:27 +0200371 seq_printf(seq, "\tVirtual address: %pK\n", carveout->va);
Loic Pallardyb8918832017-11-06 18:09:56 +0100372 seq_printf(seq, "\tDMA address: %pad\n", &carveout->dma);
373 seq_printf(seq, "\tDevice address: 0x%x\n", carveout->da);
Clement Leger096ee782020-03-02 10:38:56 +0100374 seq_printf(seq, "\tLength: 0x%zx Bytes\n\n", carveout->len);
Loic Pallardyb8918832017-11-06 18:09:56 +0100375 }
376
377 return 0;
378}
379
Yangtao Lic78bc072018-12-01 10:58:38 -0500380DEFINE_SHOW_ATTRIBUTE(rproc_carveouts);
Loic Pallardyb8918832017-11-06 18:09:56 +0100381
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200382void rproc_remove_trace_file(struct dentry *tfile)
383{
384 debugfs_remove(tfile);
385}
386
387struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
Loic Pallardya987e6b92019-01-10 14:49:10 +0100388 struct rproc_debug_trace *trace)
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200389{
390 struct dentry *tfile;
391
Anna, Suman730f84c2016-08-12 18:42:20 -0500392 tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace,
393 &trace_rproc_ops);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200394 if (!tfile) {
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300395 dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200396 return NULL;
397 }
398
399 return tfile;
400}
401
402void rproc_delete_debug_dir(struct rproc *rproc)
403{
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200404 debugfs_remove_recursive(rproc->dbg_dir);
405}
406
407void rproc_create_debug_dir(struct rproc *rproc)
408{
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300409 struct device *dev = &rproc->dev;
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200410
411 if (!rproc_dbg)
412 return;
413
414 rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
415 if (!rproc->dbg_dir)
416 return;
417
418 debugfs_create_file("name", 0400, rproc->dbg_dir,
Anna, Suman730f84c2016-08-12 18:42:20 -0500419 rproc, &rproc_name_ops);
Alex Eldere138cce2020-02-28 12:33:57 -0600420 debugfs_create_file("recovery", 0600, rproc->dbg_dir,
Anna, Suman730f84c2016-08-12 18:42:20 -0500421 rproc, &rproc_recovery_ops);
Xiang Xiao60042a22018-11-07 23:26:01 +0800422 debugfs_create_file("crash", 0200, rproc->dbg_dir,
423 rproc, &rproc_crash_ops);
Loic Pallardybdd8edb2017-11-06 18:09:55 +0100424 debugfs_create_file("resource_table", 0400, rproc->dbg_dir,
Yangtao Lic78bc072018-12-01 10:58:38 -0500425 rproc, &rproc_rsc_table_fops);
Loic Pallardyb8918832017-11-06 18:09:56 +0100426 debugfs_create_file("carveout_memories", 0400, rproc->dbg_dir,
Yangtao Lic78bc072018-12-01 10:58:38 -0500427 rproc, &rproc_carveouts_fops);
Rishabh Bhatnagar3afdc592020-07-16 15:20:35 -0700428 debugfs_create_file("coredump", 0600, rproc->dbg_dir,
429 rproc, &rproc_coredump_fops);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200430}
431
432void __init rproc_init_debugfs(void)
433{
434 if (debugfs_initialized()) {
435 rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
436 if (!rproc_dbg)
437 pr_err("can't create debugfs dir\n");
438 }
439}
440
441void __exit rproc_exit_debugfs(void)
442{
Suman Anna57971152013-06-30 11:33:05 +0300443 debugfs_remove(rproc_dbg);
Ohad Ben-Cohen6391a702011-10-20 17:24:15 +0200444}