remoteproc_debugfs.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /*
  55. * A state-to-string lookup table, for exposing a human readable state
  56. * via debugfs. Always keep in sync with enum rproc_state
  57. */
  58. static const char * const rproc_state_string[] = {
  59. "offline",
  60. "suspended",
  61. "running",
  62. "crashed",
  63. "invalid",
  64. };
  65. /* expose the state of the remote processor via debugfs */
  66. static ssize_t rproc_state_read(struct file *filp, char __user *userbuf,
  67. size_t count, loff_t *ppos)
  68. {
  69. struct rproc *rproc = filp->private_data;
  70. unsigned int state;
  71. char buf[30];
  72. int i;
  73. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  74. i = scnprintf(buf, 30, "%.28s (%d)\n", rproc_state_string[state],
  75. rproc->state);
  76. return simple_read_from_buffer(userbuf, count, ppos, buf, i);
  77. }
  78. static ssize_t rproc_state_write(struct file *filp, const char __user *userbuf,
  79. size_t count, loff_t *ppos)
  80. {
  81. struct rproc *rproc = filp->private_data;
  82. char buf[10];
  83. int ret;
  84. if (count > sizeof(buf) || count <= 0)
  85. return -EINVAL;
  86. ret = copy_from_user(buf, userbuf, count);
  87. if (ret)
  88. return -EFAULT;
  89. if (buf[count - 1] == '\n')
  90. buf[count - 1] = '\0';
  91. if (!strncmp(buf, "start", count)) {
  92. ret = rproc_boot(rproc);
  93. if (ret) {
  94. dev_err(&rproc->dev, "Boot failed: %d\n", ret);
  95. return ret;
  96. }
  97. } else if (!strncmp(buf, "stop", count)) {
  98. rproc_shutdown(rproc);
  99. } else {
  100. dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
  101. return -EINVAL;
  102. }
  103. return count;
  104. }
  105. static const struct file_operations rproc_state_ops = {
  106. .read = rproc_state_read,
  107. .write = rproc_state_write,
  108. .open = simple_open,
  109. .llseek = generic_file_llseek,
  110. };
  111. /* expose the name of the remote processor via debugfs */
  112. static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
  113. size_t count, loff_t *ppos)
  114. {
  115. struct rproc *rproc = filp->private_data;
  116. /* need room for the name, a newline and a terminating null */
  117. char buf[100];
  118. int i;
  119. i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
  120. return simple_read_from_buffer(userbuf, count, ppos, buf, i);
  121. }
  122. static const struct file_operations rproc_name_ops = {
  123. .read = rproc_name_read,
  124. .open = simple_open,
  125. .llseek = generic_file_llseek,
  126. };
  127. /* expose recovery flag via debugfs */
  128. static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf,
  129. size_t count, loff_t *ppos)
  130. {
  131. struct rproc *rproc = filp->private_data;
  132. char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n";
  133. return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
  134. }
  135. /*
  136. * By writing to the 'recovery' debugfs entry, we control the behavior of the
  137. * recovery mechanism dynamically. The default value of this entry is "enabled".
  138. *
  139. * The 'recovery' debugfs entry supports these commands:
  140. *
  141. * enabled: When enabled, the remote processor will be automatically
  142. * recovered whenever it crashes. Moreover, if the remote
  143. * processor crashes while recovery is disabled, it will
  144. * be automatically recovered too as soon as recovery is enabled.
  145. *
  146. * disabled: When disabled, a remote processor will remain in a crashed
  147. * state if it crashes. This is useful for debugging purposes;
  148. * without it, debugging a crash is substantially harder.
  149. *
  150. * recover: This function will trigger an immediate recovery if the
  151. * remote processor is in a crashed state, without changing
  152. * or checking the recovery state (enabled/disabled).
  153. * This is useful during debugging sessions, when one expects
  154. * additional crashes to happen after enabling recovery. In this
  155. * case, enabling recovery will make it hard to debug subsequent
  156. * crashes, so it's recommended to keep recovery disabled, and
  157. * instead use the "recover" command as needed.
  158. */
  159. static ssize_t
  160. rproc_recovery_write(struct file *filp, const char __user *user_buf,
  161. size_t count, loff_t *ppos)
  162. {
  163. struct rproc *rproc = filp->private_data;
  164. char buf[10];
  165. int ret;
  166. if (count < 1 || count > sizeof(buf))
  167. return -EINVAL;
  168. ret = copy_from_user(buf, user_buf, count);
  169. if (ret)
  170. return -EFAULT;
  171. /* remove end of line */
  172. if (buf[count - 1] == '\n')
  173. buf[count - 1] = '\0';
  174. if (!strncmp(buf, "enabled", count)) {
  175. rproc->recovery_disabled = false;
  176. /* if rproc has crashed, trigger recovery */
  177. if (rproc->state == RPROC_CRASHED)
  178. rproc_trigger_recovery(rproc);
  179. } else if (!strncmp(buf, "disabled", count)) {
  180. rproc->recovery_disabled = true;
  181. } else if (!strncmp(buf, "recover", count)) {
  182. /* if rproc has crashed, trigger recovery */
  183. if (rproc->state == RPROC_CRASHED)
  184. rproc_trigger_recovery(rproc);
  185. }
  186. return count;
  187. }
  188. static const struct file_operations rproc_recovery_ops = {
  189. .read = rproc_recovery_read,
  190. .write = rproc_recovery_write,
  191. .open = simple_open,
  192. .llseek = generic_file_llseek,
  193. };
  194. void rproc_remove_trace_file(struct dentry *tfile)
  195. {
  196. debugfs_remove(tfile);
  197. }
  198. struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
  199. struct rproc_mem_entry *trace)
  200. {
  201. struct dentry *tfile;
  202. tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace,
  203. &trace_rproc_ops);
  204. if (!tfile) {
  205. dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
  206. return NULL;
  207. }
  208. return tfile;
  209. }
  210. void rproc_delete_debug_dir(struct rproc *rproc)
  211. {
  212. if (!rproc->dbg_dir)
  213. return;
  214. debugfs_remove_recursive(rproc->dbg_dir);
  215. }
  216. void rproc_create_debug_dir(struct rproc *rproc)
  217. {
  218. struct device *dev = &rproc->dev;
  219. if (!rproc_dbg)
  220. return;
  221. rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
  222. if (!rproc->dbg_dir)
  223. return;
  224. debugfs_create_file("name", 0400, rproc->dbg_dir,
  225. rproc, &rproc_name_ops);
  226. debugfs_create_file("state", 0400, rproc->dbg_dir,
  227. rproc, &rproc_state_ops);
  228. debugfs_create_file("recovery", 0400, rproc->dbg_dir,
  229. rproc, &rproc_recovery_ops);
  230. }
  231. void __init rproc_init_debugfs(void)
  232. {
  233. if (debugfs_initialized()) {
  234. rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
  235. if (!rproc_dbg)
  236. pr_err("can't create debugfs dir\n");
  237. }
  238. }
  239. void __exit rproc_exit_debugfs(void)
  240. {
  241. debugfs_remove(rproc_dbg);
  242. }