remoteproc_debugfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. #define pr_fmt(fmt) "%s: " fmt, __func__
  17. #include <linux/kernel.h>
  18. #include <linux/debugfs.h>
  19. #include <linux/remoteproc.h>
  20. #include <linux/device.h>
  21. #include <linux/uaccess.h>
  22. #include "remoteproc_internal.h"
  23. /* remoteproc debugfs parent dir */
  24. static struct dentry *rproc_dbg;
  25. /*
  26. * A coredump-configuration-to-string lookup table, for exposing a
  27. * human readable configuration via debugfs. Always keep in sync with
  28. * enum rproc_coredump_mechanism
  29. */
  30. static const char * const rproc_coredump_str[] = {
  31. [RPROC_COREDUMP_DISABLED] = "disabled",
  32. [RPROC_COREDUMP_ENABLED] = "enabled",
  33. [RPROC_COREDUMP_INLINE] = "inline",
  34. };
  35. /* Expose the current coredump configuration via debugfs */
  36. static ssize_t rproc_coredump_read(struct file *filp, char __user *userbuf,
  37. size_t count, loff_t *ppos)
  38. {
  39. struct rproc *rproc = filp->private_data;
  40. char buf[20];
  41. int len;
  42. len = scnprintf(buf, sizeof(buf), "%s\n",
  43. rproc_coredump_str[rproc->dump_conf]);
  44. return simple_read_from_buffer(userbuf, count, ppos, buf, len);
  45. }
  46. /*
  47. * By writing to the 'coredump' debugfs entry, we control the behavior of the
  48. * coredump mechanism dynamically. The default value of this entry is "disabled".
  49. *
  50. * The 'coredump' debugfs entry supports these commands:
  51. *
  52. * disabled: By default coredump collection is disabled. Recovery will
  53. * proceed without collecting any dump.
  54. *
  55. * enabled: When the remoteproc crashes the entire coredump will be copied
  56. * to a separate buffer and exposed to userspace.
  57. *
  58. * inline: The coredump will not be copied to a separate buffer and the
  59. * recovery process will have to wait until data is read by
  60. * userspace. But this avoid usage of extra memory.
  61. */
  62. static ssize_t rproc_coredump_write(struct file *filp,
  63. const char __user *user_buf, size_t count,
  64. loff_t *ppos)
  65. {
  66. struct rproc *rproc = filp->private_data;
  67. int ret, err = 0;
  68. char buf[20];
  69. if (count < 1 || count > sizeof(buf))
  70. return -EINVAL;
  71. ret = copy_from_user(buf, user_buf, count);
  72. if (ret)
  73. return -EFAULT;
  74. /* remove end of line */
  75. if (buf[count - 1] == '\n')
  76. buf[count - 1] = '\0';
  77. if (rproc->state == RPROC_CRASHED) {
  78. dev_err(&rproc->dev, "can't change coredump configuration\n");
  79. err = -EBUSY;
  80. goto out;
  81. }
  82. if (!strncmp(buf, "disabled", count)) {
  83. rproc->dump_conf = RPROC_COREDUMP_DISABLED;
  84. } else if (!strncmp(buf, "enabled", count)) {
  85. rproc->dump_conf = RPROC_COREDUMP_ENABLED;
  86. } else if (!strncmp(buf, "inline", count)) {
  87. rproc->dump_conf = RPROC_COREDUMP_INLINE;
  88. } else {
  89. dev_err(&rproc->dev, "Invalid coredump configuration\n");
  90. err = -EINVAL;
  91. }
  92. out:
  93. return err ? err : count;
  94. }
  95. static const struct file_operations rproc_coredump_fops = {
  96. .read = rproc_coredump_read,
  97. .write = rproc_coredump_write,
  98. .open = simple_open,
  99. .llseek = generic_file_llseek,
  100. };
  101. /*
  102. * Some remote processors may support dumping trace logs into a shared
  103. * memory buffer. We expose this trace buffer using debugfs, so users
  104. * can easily tell what's going on remotely.
  105. *
  106. * We will most probably improve the rproc tracing facilities later on,
  107. * but this kind of lightweight and simple mechanism is always good to have,
  108. * as it provides very early tracing with little to no dependencies at all.
  109. */
  110. static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf,
  111. size_t count, loff_t *ppos)
  112. {
  113. struct rproc_debug_trace *data = filp->private_data;
  114. struct rproc_mem_entry *trace = &data->trace_mem;
  115. void *va;
  116. char buf[100];
  117. int len;
  118. va = rproc_da_to_va(data->rproc, trace->da, trace->len, NULL);
  119. if (!va) {
  120. len = scnprintf(buf, sizeof(buf), "Trace %s not available\n",
  121. trace->name);
  122. va = buf;
  123. } else {
  124. len = strnlen(va, trace->len);
  125. }
  126. return simple_read_from_buffer(userbuf, count, ppos, va, len);
  127. }
  128. static const struct file_operations trace_rproc_ops = {
  129. .read = rproc_trace_read,
  130. .open = simple_open,
  131. .llseek = generic_file_llseek,
  132. };
  133. /* expose the name of the remote processor via debugfs */
  134. static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
  135. size_t count, loff_t *ppos)
  136. {
  137. struct rproc *rproc = filp->private_data;
  138. /* need room for the name, a newline and a terminating null */
  139. char buf[100];
  140. int i;
  141. i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
  142. return simple_read_from_buffer(userbuf, count, ppos, buf, i);
  143. }
  144. static const struct file_operations rproc_name_ops = {
  145. .read = rproc_name_read,
  146. .open = simple_open,
  147. .llseek = generic_file_llseek,
  148. };
  149. /* expose recovery flag via debugfs */
  150. static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf,
  151. size_t count, loff_t *ppos)
  152. {
  153. struct rproc *rproc = filp->private_data;
  154. char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n";
  155. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  156. }
  157. /*
  158. * By writing to the 'recovery' debugfs entry, we control the behavior of the
  159. * recovery mechanism dynamically. The default value of this entry is "enabled".
  160. *
  161. * The 'recovery' debugfs entry supports these commands:
  162. *
  163. * enabled: When enabled, the remote processor will be automatically
  164. * recovered whenever it crashes. Moreover, if the remote
  165. * processor crashes while recovery is disabled, it will
  166. * be automatically recovered too as soon as recovery is enabled.
  167. *
  168. * disabled: When disabled, a remote processor will remain in a crashed
  169. * state if it crashes. This is useful for debugging purposes;
  170. * without it, debugging a crash is substantially harder.
  171. *
  172. * recover: This function will trigger an immediate recovery if the
  173. * remote processor is in a crashed state, without changing
  174. * or checking the recovery state (enabled/disabled).
  175. * This is useful during debugging sessions, when one expects
  176. * additional crashes to happen after enabling recovery. In this
  177. * case, enabling recovery will make it hard to debug subsequent
  178. * crashes, so it's recommended to keep recovery disabled, and
  179. * instead use the "recover" command as needed.
  180. */
  181. static ssize_t
  182. rproc_recovery_write(struct file *filp, const char __user *user_buf,
  183. size_t count, loff_t *ppos)
  184. {
  185. struct rproc *rproc = filp->private_data;
  186. char buf[10];
  187. int ret;
  188. if (count < 1 || count > sizeof(buf))
  189. return -EINVAL;
  190. ret = copy_from_user(buf, user_buf, count);
  191. if (ret)
  192. return -EFAULT;
  193. /* remove end of line */
  194. if (buf[count - 1] == '\n')
  195. buf[count - 1] = '\0';
  196. if (!strncmp(buf, "enabled", count)) {
  197. /* change the flag and begin the recovery process if needed */
  198. rproc->recovery_disabled = false;
  199. rproc_trigger_recovery(rproc);
  200. } else if (!strncmp(buf, "disabled", count)) {
  201. rproc->recovery_disabled = true;
  202. } else if (!strncmp(buf, "recover", count)) {
  203. /* begin the recovery process without changing the flag */
  204. rproc_trigger_recovery(rproc);
  205. } else {
  206. return -EINVAL;
  207. }
  208. return count;
  209. }
  210. static const struct file_operations rproc_recovery_ops = {
  211. .read = rproc_recovery_read,
  212. .write = rproc_recovery_write,
  213. .open = simple_open,
  214. .llseek = generic_file_llseek,
  215. };
  216. /* expose the crash trigger via debugfs */
  217. static ssize_t
  218. rproc_crash_write(struct file *filp, const char __user *user_buf,
  219. size_t count, loff_t *ppos)
  220. {
  221. struct rproc *rproc = filp->private_data;
  222. unsigned int type;
  223. int ret;
  224. ret = kstrtouint_from_user(user_buf, count, 0, &type);
  225. if (ret < 0)
  226. return ret;
  227. rproc_report_crash(rproc, type);
  228. return count;
  229. }
  230. static const struct file_operations rproc_crash_ops = {
  231. .write = rproc_crash_write,
  232. .open = simple_open,
  233. .llseek = generic_file_llseek,
  234. };
  235. /* Expose resource table content via debugfs */
  236. static int rproc_rsc_table_show(struct seq_file *seq, void *p)
  237. {
  238. static const char * const types[] = {"carveout", "devmem", "trace", "vdev"};
  239. struct rproc *rproc = seq->private;
  240. struct resource_table *table = rproc->table_ptr;
  241. struct fw_rsc_carveout *c;
  242. struct fw_rsc_devmem *d;
  243. struct fw_rsc_trace *t;
  244. struct fw_rsc_vdev *v;
  245. int i, j;
  246. if (!table) {
  247. seq_puts(seq, "No resource table found\n");
  248. return 0;
  249. }
  250. for (i = 0; i < table->num; i++) {
  251. int offset = table->offset[i];
  252. struct fw_rsc_hdr *hdr = (void *)table + offset;
  253. void *rsc = (void *)hdr + sizeof(*hdr);
  254. switch (hdr->type) {
  255. case RSC_CARVEOUT:
  256. c = rsc;
  257. seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
  258. seq_printf(seq, " Device Address 0x%x\n", c->da);
  259. seq_printf(seq, " Physical Address 0x%x\n", c->pa);
  260. seq_printf(seq, " Length 0x%x Bytes\n", c->len);
  261. seq_printf(seq, " Flags 0x%x\n", c->flags);
  262. seq_printf(seq, " Reserved (should be zero) [%d]\n", c->reserved);
  263. seq_printf(seq, " Name %s\n\n", c->name);
  264. break;
  265. case RSC_DEVMEM:
  266. d = rsc;
  267. seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
  268. seq_printf(seq, " Device Address 0x%x\n", d->da);
  269. seq_printf(seq, " Physical Address 0x%x\n", d->pa);
  270. seq_printf(seq, " Length 0x%x Bytes\n", d->len);
  271. seq_printf(seq, " Flags 0x%x\n", d->flags);
  272. seq_printf(seq, " Reserved (should be zero) [%d]\n", d->reserved);
  273. seq_printf(seq, " Name %s\n\n", d->name);
  274. break;
  275. case RSC_TRACE:
  276. t = rsc;
  277. seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
  278. seq_printf(seq, " Device Address 0x%x\n", t->da);
  279. seq_printf(seq, " Length 0x%x Bytes\n", t->len);
  280. seq_printf(seq, " Reserved (should be zero) [%d]\n", t->reserved);
  281. seq_printf(seq, " Name %s\n\n", t->name);
  282. break;
  283. case RSC_VDEV:
  284. v = rsc;
  285. seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]);
  286. seq_printf(seq, " ID %d\n", v->id);
  287. seq_printf(seq, " Notify ID %d\n", v->notifyid);
  288. seq_printf(seq, " Device features 0x%x\n", v->dfeatures);
  289. seq_printf(seq, " Guest features 0x%x\n", v->gfeatures);
  290. seq_printf(seq, " Config length 0x%x\n", v->config_len);
  291. seq_printf(seq, " Status 0x%x\n", v->status);
  292. seq_printf(seq, " Number of vrings %d\n", v->num_of_vrings);
  293. seq_printf(seq, " Reserved (should be zero) [%d][%d]\n\n",
  294. v->reserved[0], v->reserved[1]);
  295. for (j = 0; j < v->num_of_vrings; j++) {
  296. seq_printf(seq, " Vring %d\n", j);
  297. seq_printf(seq, " Device Address 0x%x\n", v->vring[j].da);
  298. seq_printf(seq, " Alignment %d\n", v->vring[j].align);
  299. seq_printf(seq, " Number of buffers %d\n", v->vring[j].num);
  300. seq_printf(seq, " Notify ID %d\n", v->vring[j].notifyid);
  301. seq_printf(seq, " Physical Address 0x%x\n\n",
  302. v->vring[j].pa);
  303. }
  304. break;
  305. default:
  306. seq_printf(seq, "Unknown resource type found: %d [hdr: %pK]\n",
  307. hdr->type, hdr);
  308. break;
  309. }
  310. }
  311. return 0;
  312. }
  313. DEFINE_SHOW_ATTRIBUTE(rproc_rsc_table);
  314. /* Expose carveout content via debugfs */
  315. static int rproc_carveouts_show(struct seq_file *seq, void *p)
  316. {
  317. struct rproc *rproc = seq->private;
  318. struct rproc_mem_entry *carveout;
  319. list_for_each_entry(carveout, &rproc->carveouts, node) {
  320. seq_puts(seq, "Carveout memory entry:\n");
  321. seq_printf(seq, "\tName: %s\n", carveout->name);
  322. seq_printf(seq, "\tVirtual address: %pK\n", carveout->va);
  323. seq_printf(seq, "\tDMA address: %pad\n", &carveout->dma);
  324. seq_printf(seq, "\tDevice address: 0x%x\n", carveout->da);
  325. seq_printf(seq, "\tLength: 0x%zx Bytes\n\n", carveout->len);
  326. }
  327. return 0;
  328. }
  329. DEFINE_SHOW_ATTRIBUTE(rproc_carveouts);
  330. void rproc_remove_trace_file(struct dentry *tfile)
  331. {
  332. debugfs_remove(tfile);
  333. }
  334. struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
  335. struct rproc_debug_trace *trace)
  336. {
  337. struct dentry *tfile;
  338. tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace,
  339. &trace_rproc_ops);
  340. if (!tfile) {
  341. dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
  342. return NULL;
  343. }
  344. return tfile;
  345. }
  346. void rproc_delete_debug_dir(struct rproc *rproc)
  347. {
  348. debugfs_remove_recursive(rproc->dbg_dir);
  349. }
  350. void rproc_create_debug_dir(struct rproc *rproc)
  351. {
  352. struct device *dev = &rproc->dev;
  353. if (!rproc_dbg)
  354. return;
  355. rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
  356. if (!rproc->dbg_dir)
  357. return;
  358. debugfs_create_file("name", 0400, rproc->dbg_dir,
  359. rproc, &rproc_name_ops);
  360. debugfs_create_file("recovery", 0600, rproc->dbg_dir,
  361. rproc, &rproc_recovery_ops);
  362. debugfs_create_file("crash", 0200, rproc->dbg_dir,
  363. rproc, &rproc_crash_ops);
  364. debugfs_create_file("resource_table", 0400, rproc->dbg_dir,
  365. rproc, &rproc_rsc_table_fops);
  366. debugfs_create_file("carveout_memories", 0400, rproc->dbg_dir,
  367. rproc, &rproc_carveouts_fops);
  368. debugfs_create_file("coredump", 0600, rproc->dbg_dir,
  369. rproc, &rproc_coredump_fops);
  370. }
  371. void __init rproc_init_debugfs(void)
  372. {
  373. if (debugfs_initialized()) {
  374. rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
  375. if (!rproc_dbg)
  376. pr_err("can't create debugfs dir\n");
  377. }
  378. }
  379. void __exit rproc_exit_debugfs(void)
  380. {
  381. debugfs_remove(rproc_dbg);
  382. }