remoteproc_debugfs.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. void rproc_remove_trace_file(struct dentry *tfile)
  138. {
  139. debugfs_remove(tfile);
  140. }
  141. struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
  142. struct rproc_mem_entry *trace)
  143. {
  144. struct dentry *tfile;
  145. tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace,
  146. &trace_rproc_ops);
  147. if (!tfile) {
  148. dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
  149. return NULL;
  150. }
  151. return tfile;
  152. }
  153. void rproc_delete_debug_dir(struct rproc *rproc)
  154. {
  155. if (!rproc->dbg_dir)
  156. return;
  157. debugfs_remove_recursive(rproc->dbg_dir);
  158. }
  159. void rproc_create_debug_dir(struct rproc *rproc)
  160. {
  161. struct device *dev = &rproc->dev;
  162. if (!rproc_dbg)
  163. return;
  164. rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
  165. if (!rproc->dbg_dir)
  166. return;
  167. debugfs_create_file("name", 0400, rproc->dbg_dir,
  168. rproc, &rproc_name_ops);
  169. debugfs_create_file("recovery", 0400, rproc->dbg_dir,
  170. rproc, &rproc_recovery_ops);
  171. }
  172. void __init rproc_init_debugfs(void)
  173. {
  174. if (debugfs_initialized()) {
  175. rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
  176. if (!rproc_dbg)
  177. pr_err("can't create debugfs dir\n");
  178. }
  179. }
  180. void __exit rproc_exit_debugfs(void)
  181. {
  182. debugfs_remove(rproc_dbg);
  183. }