remoteproc_debugfs.c 11 KB

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