Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 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> |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 31 | #include <linux/uaccess.h> |
| 32 | |
| 33 | #include "remoteproc_internal.h" |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 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, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 48 | size_t count, loff_t *ppos) |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 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 | |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 56 | static const struct file_operations trace_rproc_ops = { |
| 57 | .read = rproc_trace_read, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 58 | .open = simple_open, |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 59 | .llseek = generic_file_llseek, |
| 60 | }; |
| 61 | |
| 62 | /* |
| 63 | * A state-to-string lookup table, for exposing a human readable state |
| 64 | * via debugfs. Always keep in sync with enum rproc_state |
| 65 | */ |
| 66 | static const char * const rproc_state_string[] = { |
| 67 | "offline", |
| 68 | "suspended", |
| 69 | "running", |
| 70 | "crashed", |
| 71 | "invalid", |
| 72 | }; |
| 73 | |
| 74 | /* expose the state of the remote processor via debugfs */ |
| 75 | static ssize_t rproc_state_read(struct file *filp, char __user *userbuf, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 76 | size_t count, loff_t *ppos) |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 77 | { |
| 78 | struct rproc *rproc = filp->private_data; |
| 79 | unsigned int state; |
| 80 | char buf[30]; |
| 81 | int i; |
| 82 | |
| 83 | state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state; |
| 84 | |
Dan Carpenter | ae768d5 | 2012-09-25 10:02:51 +0300 | [diff] [blame] | 85 | i = scnprintf(buf, 30, "%.28s (%d)\n", rproc_state_string[state], |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 86 | rproc->state); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 87 | |
| 88 | return simple_read_from_buffer(userbuf, count, ppos, buf, i); |
| 89 | } |
| 90 | |
Lee Jones | 69e5047 | 2016-01-12 12:46:17 +0000 | [diff] [blame] | 91 | static ssize_t rproc_state_write(struct file *filp, const char __user *userbuf, |
| 92 | size_t count, loff_t *ppos) |
| 93 | { |
| 94 | struct rproc *rproc = filp->private_data; |
| 95 | char buf[10]; |
| 96 | int ret; |
| 97 | |
| 98 | if (count > sizeof(buf) || count <= 0) |
| 99 | return -EINVAL; |
| 100 | |
| 101 | ret = copy_from_user(buf, userbuf, count); |
| 102 | if (ret) |
| 103 | return -EFAULT; |
| 104 | |
| 105 | if (buf[count - 1] == '\n') |
| 106 | buf[count - 1] = '\0'; |
| 107 | |
| 108 | if (!strncmp(buf, "start", count)) { |
| 109 | ret = rproc_boot(rproc); |
| 110 | if (ret) { |
| 111 | dev_err(&rproc->dev, "Boot failed: %d\n", ret); |
| 112 | return ret; |
| 113 | } |
| 114 | } else if (!strncmp(buf, "stop", count)) { |
| 115 | rproc_shutdown(rproc); |
| 116 | } else { |
| 117 | dev_err(&rproc->dev, "Unrecognised option: %s\n", buf); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | return count; |
| 122 | } |
| 123 | |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 124 | static const struct file_operations rproc_state_ops = { |
| 125 | .read = rproc_state_read, |
Lee Jones | 69e5047 | 2016-01-12 12:46:17 +0000 | [diff] [blame] | 126 | .write = rproc_state_write, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 127 | .open = simple_open, |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 128 | .llseek = generic_file_llseek, |
| 129 | }; |
| 130 | |
| 131 | /* expose the name of the remote processor via debugfs */ |
| 132 | static ssize_t rproc_name_read(struct file *filp, char __user *userbuf, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 133 | size_t count, loff_t *ppos) |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 134 | { |
| 135 | struct rproc *rproc = filp->private_data; |
| 136 | /* need room for the name, a newline and a terminating null */ |
| 137 | char buf[100]; |
| 138 | int i; |
| 139 | |
Dan Carpenter | ae768d5 | 2012-09-25 10:02:51 +0300 | [diff] [blame] | 140 | i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 141 | |
| 142 | return simple_read_from_buffer(userbuf, count, ppos, buf, i); |
| 143 | } |
| 144 | |
| 145 | static const struct file_operations rproc_name_ops = { |
| 146 | .read = rproc_name_read, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 147 | .open = simple_open, |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 148 | .llseek = generic_file_llseek, |
| 149 | }; |
| 150 | |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 151 | /* expose recovery flag via debugfs */ |
| 152 | static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf, |
| 153 | size_t count, loff_t *ppos) |
| 154 | { |
| 155 | struct rproc *rproc = filp->private_data; |
| 156 | char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n"; |
| 157 | |
| 158 | return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf)); |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * By writing to the 'recovery' debugfs entry, we control the behavior of the |
| 163 | * recovery mechanism dynamically. The default value of this entry is "enabled". |
| 164 | * |
| 165 | * The 'recovery' debugfs entry supports these commands: |
| 166 | * |
| 167 | * enabled: When enabled, the remote processor will be automatically |
| 168 | * recovered whenever it crashes. Moreover, if the remote |
| 169 | * processor crashes while recovery is disabled, it will |
| 170 | * be automatically recovered too as soon as recovery is enabled. |
| 171 | * |
| 172 | * disabled: When disabled, a remote processor will remain in a crashed |
| 173 | * state if it crashes. This is useful for debugging purposes; |
| 174 | * without it, debugging a crash is substantially harder. |
| 175 | * |
| 176 | * recover: This function will trigger an immediate recovery if the |
| 177 | * remote processor is in a crashed state, without changing |
| 178 | * or checking the recovery state (enabled/disabled). |
| 179 | * This is useful during debugging sessions, when one expects |
| 180 | * additional crashes to happen after enabling recovery. In this |
| 181 | * case, enabling recovery will make it hard to debug subsequent |
| 182 | * crashes, so it's recommended to keep recovery disabled, and |
| 183 | * instead use the "recover" command as needed. |
| 184 | */ |
| 185 | static ssize_t |
| 186 | rproc_recovery_write(struct file *filp, const char __user *user_buf, |
| 187 | size_t count, loff_t *ppos) |
| 188 | { |
| 189 | struct rproc *rproc = filp->private_data; |
| 190 | char buf[10]; |
| 191 | int ret; |
| 192 | |
Arnd Bergmann | 92792e4 | 2015-11-20 18:26:07 +0100 | [diff] [blame] | 193 | if (count < 1 || count > sizeof(buf)) |
Lee Jones | 47fff9f | 2016-01-12 12:46:15 +0000 | [diff] [blame] | 194 | return -EINVAL; |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 195 | |
| 196 | ret = copy_from_user(buf, user_buf, count); |
| 197 | if (ret) |
Dan Carpenter | bec109a | 2012-09-25 10:05:33 +0300 | [diff] [blame] | 198 | return -EFAULT; |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 199 | |
| 200 | /* remove end of line */ |
| 201 | if (buf[count - 1] == '\n') |
| 202 | buf[count - 1] = '\0'; |
| 203 | |
| 204 | if (!strncmp(buf, "enabled", count)) { |
| 205 | rproc->recovery_disabled = false; |
| 206 | /* if rproc has crashed, trigger recovery */ |
| 207 | if (rproc->state == RPROC_CRASHED) |
| 208 | rproc_trigger_recovery(rproc); |
| 209 | } else if (!strncmp(buf, "disabled", count)) { |
| 210 | rproc->recovery_disabled = true; |
| 211 | } else if (!strncmp(buf, "recover", count)) { |
| 212 | /* if rproc has crashed, trigger recovery */ |
| 213 | if (rproc->state == RPROC_CRASHED) |
| 214 | rproc_trigger_recovery(rproc); |
| 215 | } |
| 216 | |
| 217 | return count; |
| 218 | } |
| 219 | |
| 220 | static const struct file_operations rproc_recovery_ops = { |
| 221 | .read = rproc_recovery_read, |
| 222 | .write = rproc_recovery_write, |
| 223 | .open = simple_open, |
| 224 | .llseek = generic_file_llseek, |
| 225 | }; |
| 226 | |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 227 | void rproc_remove_trace_file(struct dentry *tfile) |
| 228 | { |
| 229 | debugfs_remove(tfile); |
| 230 | } |
| 231 | |
| 232 | struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 233 | struct rproc_mem_entry *trace) |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 234 | { |
| 235 | struct dentry *tfile; |
| 236 | |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 237 | tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace, |
| 238 | &trace_rproc_ops); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 239 | if (!tfile) { |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 240 | dev_err(&rproc->dev, "failed to create debugfs trace entry\n"); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | return tfile; |
| 245 | } |
| 246 | |
| 247 | void rproc_delete_debug_dir(struct rproc *rproc) |
| 248 | { |
| 249 | if (!rproc->dbg_dir) |
| 250 | return; |
| 251 | |
| 252 | debugfs_remove_recursive(rproc->dbg_dir); |
| 253 | } |
| 254 | |
| 255 | void rproc_create_debug_dir(struct rproc *rproc) |
| 256 | { |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 257 | struct device *dev = &rproc->dev; |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 258 | |
| 259 | if (!rproc_dbg) |
| 260 | return; |
| 261 | |
| 262 | rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg); |
| 263 | if (!rproc->dbg_dir) |
| 264 | return; |
| 265 | |
| 266 | debugfs_create_file("name", 0400, rproc->dbg_dir, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 267 | rproc, &rproc_name_ops); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 268 | debugfs_create_file("state", 0400, rproc->dbg_dir, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 269 | rproc, &rproc_state_ops); |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 270 | debugfs_create_file("recovery", 0400, rproc->dbg_dir, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 271 | rproc, &rproc_recovery_ops); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | void __init rproc_init_debugfs(void) |
| 275 | { |
| 276 | if (debugfs_initialized()) { |
| 277 | rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL); |
| 278 | if (!rproc_dbg) |
| 279 | pr_err("can't create debugfs dir\n"); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | void __exit rproc_exit_debugfs(void) |
| 284 | { |
Suman Anna | 5797115 | 2013-06-30 11:33:05 +0300 | [diff] [blame] | 285 | debugfs_remove(rproc_dbg); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 286 | } |