remoteproc_sysfs.c 7.8 KB

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