remoteproc_sysfs.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Remote Processor Framework
  4. */
  5. #include <linux/remoteproc.h>
  6. #include <linux/slab.h>
  7. #include <trace/hooks/remoteproc.h>
  8. #include "remoteproc_internal.h"
  9. #define to_rproc(d) container_of(d, struct rproc, dev)
  10. static ssize_t recovery_show(struct device *dev,
  11. struct device_attribute *attr, char *buf)
  12. {
  13. struct rproc *rproc = to_rproc(dev);
  14. return sprintf(buf, "%s", rproc->recovery_disabled ? "disabled\n" : "enabled\n");
  15. }
  16. /*
  17. * By writing to the 'recovery' sysfs entry, we control the behavior of the
  18. * recovery mechanism dynamically. The default value of this entry is "enabled".
  19. *
  20. * The 'recovery' sysfs entry supports these commands:
  21. *
  22. * enabled: When enabled, the remote processor will be automatically
  23. * recovered whenever it crashes. Moreover, if the remote
  24. * processor crashes while recovery is disabled, it will
  25. * be automatically recovered too as soon as recovery is enabled.
  26. *
  27. * disabled: When disabled, a remote processor will remain in a crashed
  28. * state if it crashes. This is useful for debugging purposes;
  29. * without it, debugging a crash is substantially harder.
  30. *
  31. * recover: This function will trigger an immediate recovery if the
  32. * remote processor is in a crashed state, without changing
  33. * or checking the recovery state (enabled/disabled).
  34. * This is useful during debugging sessions, when one expects
  35. * additional crashes to happen after enabling recovery. In this
  36. * case, enabling recovery will make it hard to debug subsequent
  37. * crashes, so it's recommended to keep recovery disabled, and
  38. * instead use the "recover" command as needed.
  39. */
  40. static ssize_t recovery_store(struct device *dev,
  41. struct device_attribute *attr,
  42. const char *buf, size_t count)
  43. {
  44. struct rproc *rproc = to_rproc(dev);
  45. if (sysfs_streq(buf, "enabled")) {
  46. /* change the flag and begin the recovery process if needed */
  47. rproc->recovery_disabled = false;
  48. trace_android_vh_rproc_recovery_set(rproc);
  49. rproc_trigger_recovery(rproc);
  50. } else if (sysfs_streq(buf, "disabled")) {
  51. rproc->recovery_disabled = true;
  52. trace_android_vh_rproc_recovery_set(rproc);
  53. } else if (sysfs_streq(buf, "recover")) {
  54. /* begin the recovery process without changing the flag */
  55. rproc_trigger_recovery(rproc);
  56. } else {
  57. return -EINVAL;
  58. }
  59. return count;
  60. }
  61. static DEVICE_ATTR_RW(recovery);
  62. /*
  63. * A coredump-configuration-to-string lookup table, for exposing a
  64. * human readable configuration via sysfs. Always keep in sync with
  65. * enum rproc_coredump_mechanism
  66. */
  67. static const char * const rproc_coredump_str[] = {
  68. [RPROC_COREDUMP_DISABLED] = "disabled",
  69. [RPROC_COREDUMP_ENABLED] = "enabled",
  70. [RPROC_COREDUMP_INLINE] = "inline",
  71. };
  72. /* Expose the current coredump configuration via debugfs */
  73. static ssize_t coredump_show(struct device *dev,
  74. struct device_attribute *attr, char *buf)
  75. {
  76. struct rproc *rproc = to_rproc(dev);
  77. return sprintf(buf, "%s\n", rproc_coredump_str[rproc->dump_conf]);
  78. }
  79. /*
  80. * By writing to the 'coredump' sysfs entry, we control the behavior of the
  81. * coredump mechanism dynamically. The default value of this entry is "default".
  82. *
  83. * The 'coredump' sysfs entry supports these commands:
  84. *
  85. * disabled: This is the default coredump mechanism. Recovery will proceed
  86. * without collecting any dump.
  87. *
  88. * default: When the remoteproc crashes the entire coredump will be
  89. * copied to a separate buffer and exposed to userspace.
  90. *
  91. * inline: The coredump will not be copied to a separate buffer and the
  92. * recovery process will have to wait until data is read by
  93. * userspace. But this avoid usage of extra memory.
  94. */
  95. static ssize_t coredump_store(struct device *dev,
  96. struct device_attribute *attr,
  97. const char *buf, size_t count)
  98. {
  99. struct rproc *rproc = to_rproc(dev);
  100. if (rproc->state == RPROC_CRASHED) {
  101. dev_err(&rproc->dev, "can't change coredump configuration\n");
  102. return -EBUSY;
  103. }
  104. if (sysfs_streq(buf, "disabled")) {
  105. rproc->dump_conf = RPROC_COREDUMP_DISABLED;
  106. } else if (sysfs_streq(buf, "enabled")) {
  107. rproc->dump_conf = RPROC_COREDUMP_ENABLED;
  108. } else if (sysfs_streq(buf, "inline")) {
  109. rproc->dump_conf = RPROC_COREDUMP_INLINE;
  110. } else {
  111. dev_err(&rproc->dev, "Invalid coredump configuration\n");
  112. return -EINVAL;
  113. }
  114. return count;
  115. }
  116. static DEVICE_ATTR_RW(coredump);
  117. /* Expose the loaded / running firmware name via sysfs */
  118. static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
  119. char *buf)
  120. {
  121. struct rproc *rproc = to_rproc(dev);
  122. const char *firmware = rproc->firmware;
  123. /*
  124. * If the remote processor has been started by an external
  125. * entity we have no idea of what image it is running. As such
  126. * simply display a generic string rather then rproc->firmware.
  127. *
  128. * Here we rely on the autonomous flag because a remote processor
  129. * may have been attached to and currently in a running state.
  130. */
  131. if (rproc->autonomous)
  132. firmware = "unknown";
  133. return sprintf(buf, "%s\n", firmware);
  134. }
  135. /* Change firmware name via sysfs */
  136. static ssize_t firmware_store(struct device *dev,
  137. struct device_attribute *attr,
  138. const char *buf, size_t count)
  139. {
  140. struct rproc *rproc = to_rproc(dev);
  141. int err;
  142. err = rproc_set_firmware(rproc, buf);
  143. return err ? err : count;
  144. }
  145. static DEVICE_ATTR_RW(firmware);
  146. /*
  147. * A state-to-string lookup table, for exposing a human readable state
  148. * via sysfs. Always keep in sync with enum rproc_state
  149. */
  150. static const char * const rproc_state_string[] = {
  151. [RPROC_OFFLINE] = "offline",
  152. [RPROC_SUSPENDED] = "suspended",
  153. [RPROC_RUNNING] = "running",
  154. [RPROC_CRASHED] = "crashed",
  155. [RPROC_DELETED] = "deleted",
  156. [RPROC_DETACHED] = "detached",
  157. [RPROC_LAST] = "invalid",
  158. };
  159. /* Expose the state of the remote processor via sysfs */
  160. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  161. char *buf)
  162. {
  163. struct rproc *rproc = to_rproc(dev);
  164. unsigned int state;
  165. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  166. return sprintf(buf, "%s\n", rproc_state_string[state]);
  167. }
  168. /* Change remote processor state via sysfs */
  169. static ssize_t state_store(struct device *dev,
  170. struct device_attribute *attr,
  171. const char *buf, size_t count)
  172. {
  173. struct rproc *rproc = to_rproc(dev);
  174. int ret = 0;
  175. if (sysfs_streq(buf, "start")) {
  176. if (rproc->state == RPROC_RUNNING)
  177. return -EBUSY;
  178. ret = rproc_boot(rproc);
  179. if (ret)
  180. dev_err(&rproc->dev, "Boot failed: %d\n", ret);
  181. } else if (sysfs_streq(buf, "stop")) {
  182. if (rproc->state != RPROC_RUNNING)
  183. return -EINVAL;
  184. rproc_shutdown(rproc);
  185. } else {
  186. dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
  187. ret = -EINVAL;
  188. }
  189. return ret ? ret : count;
  190. }
  191. static DEVICE_ATTR_RW(state);
  192. /* Expose the name of the remote processor via sysfs */
  193. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  194. char *buf)
  195. {
  196. struct rproc *rproc = to_rproc(dev);
  197. return sprintf(buf, "%s\n", rproc->name);
  198. }
  199. static DEVICE_ATTR_RO(name);
  200. static struct attribute *rproc_attrs[] = {
  201. &dev_attr_coredump.attr,
  202. &dev_attr_recovery.attr,
  203. &dev_attr_firmware.attr,
  204. &dev_attr_state.attr,
  205. &dev_attr_name.attr,
  206. NULL
  207. };
  208. static const struct attribute_group rproc_devgroup = {
  209. .attrs = rproc_attrs
  210. };
  211. static const struct attribute_group *rproc_devgroups[] = {
  212. &rproc_devgroup,
  213. NULL
  214. };
  215. struct class rproc_class = {
  216. .name = "remoteproc",
  217. .dev_groups = rproc_devgroups,
  218. };
  219. int __init rproc_init_sysfs(void)
  220. {
  221. /* create remoteproc device class for sysfs */
  222. int err = class_register(&rproc_class);
  223. if (err)
  224. pr_err("remoteproc: unable to register class\n");
  225. return err;
  226. }
  227. void __exit rproc_exit_sysfs(void)
  228. {
  229. class_unregister(&rproc_class);
  230. }