1 // SPDX-License-Identifier: GPL-2.0-only
3 * Remote Processor Framework
6 #include <linux/remoteproc.h>
7 #include <linux/slab.h>
9 #include "remoteproc_internal.h"
11 #define to_rproc(d) container_of(d, struct rproc, dev)
13 /* Expose the loaded / running firmware name via sysfs */
14 static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
17 struct rproc *rproc = to_rproc(dev);
19 return sprintf(buf, "%s\n", rproc->firmware);
22 /* Change firmware name via sysfs */
23 static ssize_t firmware_store(struct device *dev,
24 struct device_attribute *attr,
25 const char *buf, size_t count)
27 struct rproc *rproc = to_rproc(dev);
31 err = mutex_lock_interruptible(&rproc->lock);
33 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, err);
37 if (rproc->state != RPROC_OFFLINE) {
38 dev_err(dev, "can't change firmware while running\n");
43 len = strcspn(buf, "\n");
45 dev_err(dev, "can't provide a NULL firmware\n");
50 p = kstrndup(buf, len, GFP_KERNEL);
56 kfree(rproc->firmware);
59 mutex_unlock(&rproc->lock);
61 return err ? err : count;
63 static DEVICE_ATTR_RW(firmware);
66 * A state-to-string lookup table, for exposing a human readable state
67 * via sysfs. Always keep in sync with enum rproc_state
69 static const char * const rproc_state_string[] = {
70 [RPROC_OFFLINE] = "offline",
71 [RPROC_SUSPENDED] = "suspended",
72 [RPROC_RUNNING] = "running",
73 [RPROC_CRASHED] = "crashed",
74 [RPROC_DELETED] = "deleted",
75 [RPROC_LAST] = "invalid",
78 /* Expose the state of the remote processor via sysfs */
79 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
82 struct rproc *rproc = to_rproc(dev);
85 state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
86 return sprintf(buf, "%s\n", rproc_state_string[state]);
89 /* Change remote processor state via sysfs */
90 static ssize_t state_store(struct device *dev,
91 struct device_attribute *attr,
92 const char *buf, size_t count)
94 struct rproc *rproc = to_rproc(dev);
97 if (sysfs_streq(buf, "start")) {
98 if (rproc->state == RPROC_RUNNING)
101 ret = rproc_boot(rproc);
103 dev_err(&rproc->dev, "Boot failed: %d\n", ret);
104 } else if (sysfs_streq(buf, "stop")) {
105 if (rproc->state != RPROC_RUNNING)
108 rproc_shutdown(rproc);
110 dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
113 return ret ? ret : count;
115 static DEVICE_ATTR_RW(state);
117 /* Expose the name of the remote processor via sysfs */
118 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
121 struct rproc *rproc = to_rproc(dev);
123 return sprintf(buf, "%s\n", rproc->name);
125 static DEVICE_ATTR_RO(name);
127 static struct attribute *rproc_attrs[] = {
128 &dev_attr_firmware.attr,
129 &dev_attr_state.attr,
134 static const struct attribute_group rproc_devgroup = {
138 static const struct attribute_group *rproc_devgroups[] = {
143 struct class rproc_class = {
144 .name = "remoteproc",
145 .dev_groups = rproc_devgroups,
148 int __init rproc_init_sysfs(void)
150 /* create remoteproc device class for sysfs */
151 int err = class_register(&rproc_class);
154 pr_err("remoteproc: unable to register class\n");
158 void __exit rproc_exit_sysfs(void)
160 class_unregister(&rproc_class);