Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 2 | /* |
| 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-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 15 | */ |
| 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 Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 23 | #include <linux/uaccess.h> |
| 24 | |
| 25 | #include "remoteproc_internal.h" |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 26 | |
| 27 | /* remoteproc debugfs parent dir */ |
| 28 | static 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 | */ |
| 39 | static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 40 | size_t count, loff_t *ppos) |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 41 | { |
Loic Pallardy | a987e6b9 | 2019-01-10 14:49:10 +0100 | [diff] [blame] | 42 | 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-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 47 | |
Loic Pallardy | a987e6b9 | 2019-01-10 14:49:10 +0100 | [diff] [blame] | 48 | 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-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 61 | static const struct file_operations trace_rproc_ops = { |
| 62 | .read = rproc_trace_read, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 63 | .open = simple_open, |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 64 | .llseek = generic_file_llseek, |
| 65 | }; |
| 66 | |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 67 | /* expose the name of the remote processor via debugfs */ |
| 68 | static ssize_t rproc_name_read(struct file *filp, char __user *userbuf, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 69 | size_t count, loff_t *ppos) |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 70 | { |
| 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 Carpenter | ae768d5 | 2012-09-25 10:02:51 +0300 | [diff] [blame] | 76 | i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 77 | |
| 78 | return simple_read_from_buffer(userbuf, count, ppos, buf, i); |
| 79 | } |
| 80 | |
| 81 | static const struct file_operations rproc_name_ops = { |
| 82 | .read = rproc_name_read, |
Stephen Boyd | 234e340 | 2012-04-05 14:25:11 -0700 | [diff] [blame] | 83 | .open = simple_open, |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 84 | .llseek = generic_file_llseek, |
| 85 | }; |
| 86 | |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 87 | /* expose recovery flag via debugfs */ |
| 88 | static 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 | */ |
| 121 | static ssize_t |
| 122 | rproc_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 Bergmann | 92792e4 | 2015-11-20 18:26:07 +0100 | [diff] [blame] | 129 | if (count < 1 || count > sizeof(buf)) |
Lee Jones | 47fff9f | 2016-01-12 12:46:15 +0000 | [diff] [blame] | 130 | return -EINVAL; |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 131 | |
| 132 | ret = copy_from_user(buf, user_buf, count); |
| 133 | if (ret) |
Dan Carpenter | bec109a | 2012-09-25 10:05:33 +0300 | [diff] [blame] | 134 | return -EFAULT; |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 135 | |
| 136 | /* remove end of line */ |
| 137 | if (buf[count - 1] == '\n') |
| 138 | buf[count - 1] = '\0'; |
| 139 | |
| 140 | if (!strncmp(buf, "enabled", count)) { |
Alex Elder | e138cce | 2020-02-28 12:33:57 -0600 | [diff] [blame] | 141 | /* change the flag and begin the recovery process if needed */ |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 142 | rproc->recovery_disabled = false; |
Alex Elder | e138cce | 2020-02-28 12:33:57 -0600 | [diff] [blame] | 143 | rproc_trigger_recovery(rproc); |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 144 | } else if (!strncmp(buf, "disabled", count)) { |
| 145 | rproc->recovery_disabled = true; |
| 146 | } else if (!strncmp(buf, "recover", count)) { |
Alex Elder | e138cce | 2020-02-28 12:33:57 -0600 | [diff] [blame] | 147 | /* begin the recovery process without changing the flag */ |
| 148 | rproc_trigger_recovery(rproc); |
Alex Elder | 1f2f65c | 2020-02-28 12:33:59 -0600 | [diff] [blame] | 149 | } else { |
| 150 | return -EINVAL; |
Fernando Guzman Lugo | 2e37abb | 2012-09-18 12:26:35 +0300 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | return count; |
| 154 | } |
| 155 | |
| 156 | static 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 Xiao | 60042a2 | 2018-11-07 23:26:01 +0800 | [diff] [blame] | 163 | /* expose the crash trigger via debugfs */ |
| 164 | static ssize_t |
| 165 | rproc_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 | |
| 181 | static const struct file_operations rproc_crash_ops = { |
| 182 | .write = rproc_crash_write, |
| 183 | .open = simple_open, |
| 184 | .llseek = generic_file_llseek, |
| 185 | }; |
| 186 | |
Loic Pallardy | bdd8edb | 2017-11-06 18:09:55 +0100 | [diff] [blame] | 187 | /* Expose resource table content via debugfs */ |
| 188 | static 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 Pallardy | 276ec99 | 2018-07-06 14:38:27 +0200 | [diff] [blame] | 263 | seq_printf(seq, "Unknown resource type found: %d [hdr: %pK]\n", |
Loic Pallardy | bdd8edb | 2017-11-06 18:09:55 +0100 | [diff] [blame] | 264 | hdr->type, hdr); |
| 265 | break; |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static 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 | |
| 277 | static 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 Pallardy | b891883 | 2017-11-06 18:09:56 +0100 | [diff] [blame] | 284 | /* Expose carveout content via debugfs */ |
| 285 | static 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 Pallardy | 3265230 | 2018-07-27 15:14:39 +0200 | [diff] [blame] | 292 | seq_printf(seq, "\tName: %s\n", carveout->name); |
Loic Pallardy | 276ec99 | 2018-07-06 14:38:27 +0200 | [diff] [blame] | 293 | seq_printf(seq, "\tVirtual address: %pK\n", carveout->va); |
Loic Pallardy | b891883 | 2017-11-06 18:09:56 +0100 | [diff] [blame] | 294 | seq_printf(seq, "\tDMA address: %pad\n", &carveout->dma); |
| 295 | seq_printf(seq, "\tDevice address: 0x%x\n", carveout->da); |
Clement Leger | 096ee78 | 2020-03-02 10:38:56 +0100 | [diff] [blame] | 296 | seq_printf(seq, "\tLength: 0x%zx Bytes\n\n", carveout->len); |
Loic Pallardy | b891883 | 2017-11-06 18:09:56 +0100 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | static int rproc_carveouts_open(struct inode *inode, struct file *file) |
| 303 | { |
| 304 | return single_open(file, rproc_carveouts_show, inode->i_private); |
| 305 | } |
| 306 | |
| 307 | static 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-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 314 | void rproc_remove_trace_file(struct dentry *tfile) |
| 315 | { |
| 316 | debugfs_remove(tfile); |
| 317 | } |
| 318 | |
| 319 | struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc, |
Loic Pallardy | a987e6b9 | 2019-01-10 14:49:10 +0100 | [diff] [blame] | 320 | struct rproc_debug_trace *trace) |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 321 | { |
| 322 | struct dentry *tfile; |
| 323 | |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 324 | tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace, |
| 325 | &trace_rproc_ops); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 326 | if (!tfile) { |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 327 | dev_err(&rproc->dev, "failed to create debugfs trace entry\n"); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 328 | return NULL; |
| 329 | } |
| 330 | |
| 331 | return tfile; |
| 332 | } |
| 333 | |
| 334 | void rproc_delete_debug_dir(struct rproc *rproc) |
| 335 | { |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 336 | debugfs_remove_recursive(rproc->dbg_dir); |
| 337 | } |
| 338 | |
| 339 | void rproc_create_debug_dir(struct rproc *rproc) |
| 340 | { |
Ohad Ben-Cohen | b5ab5e2 | 2012-05-30 22:01:25 +0300 | [diff] [blame] | 341 | struct device *dev = &rproc->dev; |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 342 | |
| 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, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 351 | rproc, &rproc_name_ops); |
Alex Elder | e138cce | 2020-02-28 12:33:57 -0600 | [diff] [blame] | 352 | debugfs_create_file("recovery", 0600, rproc->dbg_dir, |
Anna, Suman | 730f84c | 2016-08-12 18:42:20 -0500 | [diff] [blame] | 353 | rproc, &rproc_recovery_ops); |
Xiang Xiao | 60042a2 | 2018-11-07 23:26:01 +0800 | [diff] [blame] | 354 | debugfs_create_file("crash", 0200, rproc->dbg_dir, |
| 355 | rproc, &rproc_crash_ops); |
Loic Pallardy | bdd8edb | 2017-11-06 18:09:55 +0100 | [diff] [blame] | 356 | debugfs_create_file("resource_table", 0400, rproc->dbg_dir, |
| 357 | rproc, &rproc_rsc_table_ops); |
Loic Pallardy | b891883 | 2017-11-06 18:09:56 +0100 | [diff] [blame] | 358 | debugfs_create_file("carveout_memories", 0400, rproc->dbg_dir, |
| 359 | rproc, &rproc_carveouts_ops); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | void __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 | |
| 371 | void __exit rproc_exit_debugfs(void) |
| 372 | { |
Suman Anna | 5797115 | 2013-06-30 11:33:05 +0300 | [diff] [blame] | 373 | debugfs_remove(rproc_dbg); |
Ohad Ben-Cohen | 6391a70 | 2011-10-20 17:24:15 +0200 | [diff] [blame] | 374 | } |