remoteproc_sysfs.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 sysfs_emit(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. mutex_lock(&rproc->lock);
  48. rproc->recovery_disabled = false;
  49. trace_android_vh_rproc_recovery_set(rproc);
  50. mutex_unlock(&rproc->lock);
  51. rproc_trigger_recovery(rproc);
  52. } else if (sysfs_streq(buf, "disabled")) {
  53. mutex_lock(&rproc->lock);
  54. rproc->recovery_disabled = true;
  55. trace_android_vh_rproc_recovery_set(rproc);
  56. mutex_unlock(&rproc->lock);
  57. } else if (sysfs_streq(buf, "recover")) {
  58. /* begin the recovery process without changing the flag */
  59. rproc_trigger_recovery(rproc);
  60. } else {
  61. return -EINVAL;
  62. }
  63. return count;
  64. }
  65. static DEVICE_ATTR_RW(recovery);
  66. /*
  67. * A coredump-configuration-to-string lookup table, for exposing a
  68. * human readable configuration via sysfs. Always keep in sync with
  69. * enum rproc_coredump_mechanism
  70. */
  71. static const char * const rproc_coredump_str[] = {
  72. [RPROC_COREDUMP_DISABLED] = "disabled",
  73. [RPROC_COREDUMP_ENABLED] = "enabled",
  74. [RPROC_COREDUMP_INLINE] = "inline",
  75. };
  76. /* Expose the current coredump configuration via debugfs */
  77. static ssize_t coredump_show(struct device *dev,
  78. struct device_attribute *attr, char *buf)
  79. {
  80. struct rproc *rproc = to_rproc(dev);
  81. return sysfs_emit(buf, "%s\n", rproc_coredump_str[rproc->dump_conf]);
  82. }
  83. /*
  84. * By writing to the 'coredump' sysfs entry, we control the behavior of the
  85. * coredump mechanism dynamically. The default value of this entry is "default".
  86. *
  87. * The 'coredump' sysfs entry supports these commands:
  88. *
  89. * disabled: This is the default coredump mechanism. Recovery will proceed
  90. * without collecting any dump.
  91. *
  92. * default: When the remoteproc crashes the entire coredump will be
  93. * copied to a separate buffer and exposed to userspace.
  94. *
  95. * inline: The coredump will not be copied to a separate buffer and the
  96. * recovery process will have to wait until data is read by
  97. * userspace. But this avoid usage of extra memory.
  98. */
  99. static ssize_t coredump_store(struct device *dev,
  100. struct device_attribute *attr,
  101. const char *buf, size_t count)
  102. {
  103. struct rproc *rproc = to_rproc(dev);
  104. if (rproc->state == RPROC_CRASHED) {
  105. dev_err(&rproc->dev, "can't change coredump configuration\n");
  106. return -EBUSY;
  107. }
  108. if (sysfs_streq(buf, "disabled")) {
  109. rproc->dump_conf = RPROC_COREDUMP_DISABLED;
  110. } else if (sysfs_streq(buf, "enabled")) {
  111. rproc->dump_conf = RPROC_COREDUMP_ENABLED;
  112. } else if (sysfs_streq(buf, "inline")) {
  113. rproc->dump_conf = RPROC_COREDUMP_INLINE;
  114. } else {
  115. dev_err(&rproc->dev, "Invalid coredump configuration\n");
  116. return -EINVAL;
  117. }
  118. return count;
  119. }
  120. static DEVICE_ATTR_RW(coredump);
  121. /* Expose the loaded / running firmware name via sysfs */
  122. static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
  123. char *buf)
  124. {
  125. struct rproc *rproc = to_rproc(dev);
  126. const char *firmware = rproc->firmware;
  127. /*
  128. * If the remote processor has been started by an external
  129. * entity we have no idea of what image it is running. As such
  130. * simply display a generic string rather then rproc->firmware.
  131. */
  132. if (rproc->state == RPROC_ATTACHED)
  133. firmware = "unknown";
  134. return sprintf(buf, "%s\n", firmware);
  135. }
  136. /* Change firmware name via sysfs */
  137. static ssize_t firmware_store(struct device *dev,
  138. struct device_attribute *attr,
  139. const char *buf, size_t count)
  140. {
  141. struct rproc *rproc = to_rproc(dev);
  142. int err;
  143. err = rproc_set_firmware(rproc, buf);
  144. return err ? err : count;
  145. }
  146. static DEVICE_ATTR_RW(firmware);
  147. /*
  148. * A state-to-string lookup table, for exposing a human readable state
  149. * via sysfs. Always keep in sync with enum rproc_state
  150. */
  151. static const char * const rproc_state_string[] = {
  152. [RPROC_OFFLINE] = "offline",
  153. [RPROC_SUSPENDED] = "suspended",
  154. [RPROC_RUNNING] = "running",
  155. [RPROC_CRASHED] = "crashed",
  156. [RPROC_DELETED] = "deleted",
  157. [RPROC_ATTACHED] = "attached",
  158. [RPROC_DETACHED] = "detached",
  159. [RPROC_LAST] = "invalid",
  160. };
  161. /* Expose the state of the remote processor via sysfs */
  162. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  163. char *buf)
  164. {
  165. struct rproc *rproc = to_rproc(dev);
  166. unsigned int state;
  167. state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
  168. return sprintf(buf, "%s\n", rproc_state_string[state]);
  169. }
  170. /* Change remote processor state via sysfs */
  171. static ssize_t state_store(struct device *dev,
  172. struct device_attribute *attr,
  173. const char *buf, size_t count)
  174. {
  175. struct rproc *rproc = to_rproc(dev);
  176. int ret = 0;
  177. if (sysfs_streq(buf, "start")) {
  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. ret = rproc_shutdown(rproc);
  183. } else if (sysfs_streq(buf, "detach")) {
  184. ret = rproc_detach(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 umode_t rproc_is_visible(struct kobject *kobj, struct attribute *attr,
  201. int n)
  202. {
  203. struct device *dev = kobj_to_dev(kobj);
  204. struct rproc *rproc = to_rproc(dev);
  205. umode_t mode = attr->mode;
  206. if (rproc->sysfs_read_only && (attr == &dev_attr_recovery.attr ||
  207. attr == &dev_attr_firmware.attr ||
  208. attr == &dev_attr_state.attr ||
  209. attr == &dev_attr_coredump.attr))
  210. mode = 0444;
  211. return mode;
  212. }
  213. static struct attribute *rproc_attrs[] = {
  214. &dev_attr_coredump.attr,
  215. &dev_attr_recovery.attr,
  216. &dev_attr_firmware.attr,
  217. &dev_attr_state.attr,
  218. &dev_attr_name.attr,
  219. NULL
  220. };
  221. static const struct attribute_group rproc_devgroup = {
  222. .attrs = rproc_attrs,
  223. .is_visible = rproc_is_visible,
  224. };
  225. static const struct attribute_group *rproc_devgroups[] = {
  226. &rproc_devgroup,
  227. NULL
  228. };
  229. struct class rproc_class = {
  230. .name = "remoteproc",
  231. .dev_groups = rproc_devgroups,
  232. };
  233. int __init rproc_init_sysfs(void)
  234. {
  235. /* create remoteproc device class for sysfs */
  236. int err = class_register(&rproc_class);
  237. if (err)
  238. pr_err("remoteproc: unable to register class\n");
  239. return err;
  240. }
  241. void __exit rproc_exit_sysfs(void)
  242. {
  243. class_unregister(&rproc_class);
  244. }