remoteproc_debugfs.c 10 KB

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