1 // SPDX-License-Identifier: GPL-2.0-only 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> 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> 23 #include <linux/uaccess.h> 24 25 #include "remoteproc_internal.h" 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, 40 size_t count, loff_t *ppos) 41 { 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; 47 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); 59 } 60 61 static const struct file_operations trace_rproc_ops = { 62 .read = rproc_trace_read, 63 .open = simple_open, 64 .llseek = generic_file_llseek, 65 }; 66 67 /* expose the name of the remote processor via debugfs */ 68 static ssize_t rproc_name_read(struct file *filp, char __user *userbuf, 69 size_t count, loff_t *ppos) 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 76 i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name); 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, 83 .open = simple_open, 84 .llseek = generic_file_llseek, 85 }; 86 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 129 if (count < 1 || count > sizeof(buf)) 130 return -EINVAL; 131 132 ret = copy_from_user(buf, user_buf, count); 133 if (ret) 134 return -EFAULT; 135 136 /* remove end of line */ 137 if (buf[count - 1] == '\n') 138 buf[count - 1] = '\0'; 139 140 if (!strncmp(buf, "enabled", count)) { 141 /* change the flag and begin the recovery process if needed */ 142 rproc->recovery_disabled = false; 143 rproc_trigger_recovery(rproc); 144 } else if (!strncmp(buf, "disabled", count)) { 145 rproc->recovery_disabled = true; 146 } else if (!strncmp(buf, "recover", count)) { 147 /* begin the recovery process without changing the flag */ 148 rproc_trigger_recovery(rproc); 149 } else { 150 return -EINVAL; 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 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 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: 263 seq_printf(seq, "Unknown resource type found: %d [hdr: %pK]\n", 264 hdr->type, hdr); 265 break; 266 } 267 } 268 269 return 0; 270 } 271 272 DEFINE_SHOW_ATTRIBUTE(rproc_rsc_table); 273 274 /* Expose carveout content via debugfs */ 275 static int rproc_carveouts_show(struct seq_file *seq, void *p) 276 { 277 struct rproc *rproc = seq->private; 278 struct rproc_mem_entry *carveout; 279 280 list_for_each_entry(carveout, &rproc->carveouts, node) { 281 seq_puts(seq, "Carveout memory entry:\n"); 282 seq_printf(seq, "\tName: %s\n", carveout->name); 283 seq_printf(seq, "\tVirtual address: %pK\n", carveout->va); 284 seq_printf(seq, "\tDMA address: %pad\n", &carveout->dma); 285 seq_printf(seq, "\tDevice address: 0x%x\n", carveout->da); 286 seq_printf(seq, "\tLength: 0x%zx Bytes\n\n", carveout->len); 287 } 288 289 return 0; 290 } 291 292 DEFINE_SHOW_ATTRIBUTE(rproc_carveouts); 293 294 void rproc_remove_trace_file(struct dentry *tfile) 295 { 296 debugfs_remove(tfile); 297 } 298 299 struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc, 300 struct rproc_debug_trace *trace) 301 { 302 struct dentry *tfile; 303 304 tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace, 305 &trace_rproc_ops); 306 if (!tfile) { 307 dev_err(&rproc->dev, "failed to create debugfs trace entry\n"); 308 return NULL; 309 } 310 311 return tfile; 312 } 313 314 void rproc_delete_debug_dir(struct rproc *rproc) 315 { 316 debugfs_remove_recursive(rproc->dbg_dir); 317 } 318 319 void rproc_create_debug_dir(struct rproc *rproc) 320 { 321 struct device *dev = &rproc->dev; 322 323 if (!rproc_dbg) 324 return; 325 326 rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg); 327 if (!rproc->dbg_dir) 328 return; 329 330 debugfs_create_file("name", 0400, rproc->dbg_dir, 331 rproc, &rproc_name_ops); 332 debugfs_create_file("recovery", 0600, rproc->dbg_dir, 333 rproc, &rproc_recovery_ops); 334 debugfs_create_file("crash", 0200, rproc->dbg_dir, 335 rproc, &rproc_crash_ops); 336 debugfs_create_file("resource_table", 0400, rproc->dbg_dir, 337 rproc, &rproc_rsc_table_fops); 338 debugfs_create_file("carveout_memories", 0400, rproc->dbg_dir, 339 rproc, &rproc_carveouts_fops); 340 } 341 342 void __init rproc_init_debugfs(void) 343 { 344 if (debugfs_initialized()) { 345 rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL); 346 if (!rproc_dbg) 347 pr_err("can't create debugfs dir\n"); 348 } 349 } 350 351 void __exit rproc_exit_debugfs(void) 352 { 353 debugfs_remove(rproc_dbg); 354 } 355