2 * Remote Processor Framework
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
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>
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.
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.
25 #define pr_fmt(fmt) "%s: " fmt, __func__
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>
33 #include "remoteproc_internal.h"
35 /* remoteproc debugfs parent dir */
36 static struct dentry *rproc_dbg;
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.
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.
47 static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf,
48 size_t count, loff_t *ppos)
50 struct rproc_mem_entry *trace = filp->private_data;
51 int len = strnlen(trace->va, trace->len);
53 return simple_read_from_buffer(userbuf, count, ppos, trace->va, len);
56 static const struct file_operations trace_rproc_ops = {
57 .read = rproc_trace_read,
59 .llseek = generic_file_llseek,
63 * A state-to-string lookup table, for exposing a human readable state
64 * via debugfs. Always keep in sync with enum rproc_state
66 static const char * const rproc_state_string[] = {
74 /* expose the state of the remote processor via debugfs */
75 static ssize_t rproc_state_read(struct file *filp, char __user *userbuf,
76 size_t count, loff_t *ppos)
78 struct rproc *rproc = filp->private_data;
83 state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
85 i = snprintf(buf, 30, "%.28s (%d)\n", rproc_state_string[state],
88 return simple_read_from_buffer(userbuf, count, ppos, buf, i);
91 static const struct file_operations rproc_state_ops = {
92 .read = rproc_state_read,
94 .llseek = generic_file_llseek,
97 /* expose the name of the remote processor via debugfs */
98 static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
99 size_t count, loff_t *ppos)
101 struct rproc *rproc = filp->private_data;
102 /* need room for the name, a newline and a terminating null */
106 i = snprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
108 return simple_read_from_buffer(userbuf, count, ppos, buf, i);
111 static const struct file_operations rproc_name_ops = {
112 .read = rproc_name_read,
114 .llseek = generic_file_llseek,
117 /* expose recovery flag via debugfs */
118 static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf,
119 size_t count, loff_t *ppos)
121 struct rproc *rproc = filp->private_data;
122 char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n";
124 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
128 * By writing to the 'recovery' debugfs entry, we control the behavior of the
129 * recovery mechanism dynamically. The default value of this entry is "enabled".
131 * The 'recovery' debugfs entry supports these commands:
133 * enabled: When enabled, the remote processor will be automatically
134 * recovered whenever it crashes. Moreover, if the remote
135 * processor crashes while recovery is disabled, it will
136 * be automatically recovered too as soon as recovery is enabled.
138 * disabled: When disabled, a remote processor will remain in a crashed
139 * state if it crashes. This is useful for debugging purposes;
140 * without it, debugging a crash is substantially harder.
142 * recover: This function will trigger an immediate recovery if the
143 * remote processor is in a crashed state, without changing
144 * or checking the recovery state (enabled/disabled).
145 * This is useful during debugging sessions, when one expects
146 * additional crashes to happen after enabling recovery. In this
147 * case, enabling recovery will make it hard to debug subsequent
148 * crashes, so it's recommended to keep recovery disabled, and
149 * instead use the "recover" command as needed.
152 rproc_recovery_write(struct file *filp, const char __user *user_buf,
153 size_t count, loff_t *ppos)
155 struct rproc *rproc = filp->private_data;
159 if (count > sizeof(buf))
162 ret = copy_from_user(buf, user_buf, count);
166 /* remove end of line */
167 if (buf[count - 1] == '\n')
168 buf[count - 1] = '\0';
170 if (!strncmp(buf, "enabled", count)) {
171 rproc->recovery_disabled = false;
172 /* if rproc has crashed, trigger recovery */
173 if (rproc->state == RPROC_CRASHED)
174 rproc_trigger_recovery(rproc);
175 } else if (!strncmp(buf, "disabled", count)) {
176 rproc->recovery_disabled = true;
177 } else if (!strncmp(buf, "recover", count)) {
178 /* if rproc has crashed, trigger recovery */
179 if (rproc->state == RPROC_CRASHED)
180 rproc_trigger_recovery(rproc);
186 static const struct file_operations rproc_recovery_ops = {
187 .read = rproc_recovery_read,
188 .write = rproc_recovery_write,
190 .llseek = generic_file_llseek,
193 void rproc_remove_trace_file(struct dentry *tfile)
195 debugfs_remove(tfile);
198 struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
199 struct rproc_mem_entry *trace)
201 struct dentry *tfile;
203 tfile = debugfs_create_file(name, 0400, rproc->dbg_dir,
204 trace, &trace_rproc_ops);
206 dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
213 void rproc_delete_debug_dir(struct rproc *rproc)
218 debugfs_remove_recursive(rproc->dbg_dir);
221 void rproc_create_debug_dir(struct rproc *rproc)
223 struct device *dev = &rproc->dev;
228 rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
232 debugfs_create_file("name", 0400, rproc->dbg_dir,
233 rproc, &rproc_name_ops);
234 debugfs_create_file("state", 0400, rproc->dbg_dir,
235 rproc, &rproc_state_ops);
236 debugfs_create_file("recovery", 0400, rproc->dbg_dir,
237 rproc, &rproc_recovery_ops);
240 void __init rproc_init_debugfs(void)
242 if (debugfs_initialized()) {
243 rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
245 pr_err("can't create debugfs dir\n");
249 void __exit rproc_exit_debugfs(void)
252 debugfs_remove(rproc_dbg);