remoteproc_debugfs.c 7.1 KB

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