1 // SPDX-License-Identifier: GPL-2.0-only
3 * Remote Processor Framework
6 #include <linux/remoteproc.h>
8 #include "remoteproc_internal.h"
10 #define to_rproc(d) container_of(d, struct rproc, dev)
12 /* Expose the loaded / running firmware name via sysfs */
13 static ssize_t firmware_show(struct device *dev, struct device_attribute *attr,
16 struct rproc *rproc = to_rproc(dev);
18 return sprintf(buf, "%s\n", rproc->firmware);
21 /* Change firmware name via sysfs */
22 static ssize_t firmware_store(struct device *dev,
23 struct device_attribute *attr,
24 const char *buf, size_t count)
26 struct rproc *rproc = to_rproc(dev);
30 err = mutex_lock_interruptible(&rproc->lock);
32 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, err);
36 if (rproc->state != RPROC_OFFLINE) {
37 dev_err(dev, "can't change firmware while running\n");
42 len = strcspn(buf, "\n");
44 dev_err(dev, "can't provide a NULL firmware\n");
49 p = kstrndup(buf, len, GFP_KERNEL);
55 kfree(rproc->firmware);
58 mutex_unlock(&rproc->lock);
60 return err ? err : count;
62 static DEVICE_ATTR_RW(firmware);
65 * A state-to-string lookup table, for exposing a human readable state
66 * via sysfs. Always keep in sync with enum rproc_state
68 static const char * const rproc_state_string[] = {
69 [RPROC_OFFLINE] = "offline",
70 [RPROC_SUSPENDED] = "suspended",
71 [RPROC_RUNNING] = "running",
72 [RPROC_CRASHED] = "crashed",
73 [RPROC_DELETED] = "deleted",
74 [RPROC_LAST] = "invalid",
77 /* Expose the state of the remote processor via sysfs */
78 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
81 struct rproc *rproc = to_rproc(dev);
84 state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
85 return sprintf(buf, "%s\n", rproc_state_string[state]);
88 /* Change remote processor state via sysfs */
89 static ssize_t state_store(struct device *dev,
90 struct device_attribute *attr,
91 const char *buf, size_t count)
93 struct rproc *rproc = to_rproc(dev);
96 if (sysfs_streq(buf, "start")) {
97 if (rproc->state == RPROC_RUNNING)
100 ret = rproc_boot(rproc);
102 dev_err(&rproc->dev, "Boot failed: %d\n", ret);
103 } else if (sysfs_streq(buf, "stop")) {
104 if (rproc->state != RPROC_RUNNING)
107 rproc_shutdown(rproc);
109 dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
112 return ret ? ret : count;
114 static DEVICE_ATTR_RW(state);
116 /* Expose the name of the remote processor via sysfs */
117 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
120 struct rproc *rproc = to_rproc(dev);
122 return sprintf(buf, "%s\n", rproc->name);
124 static DEVICE_ATTR_RO(name);
126 static struct attribute *rproc_attrs[] = {
127 &dev_attr_firmware.attr,
128 &dev_attr_state.attr,
133 static const struct attribute_group rproc_devgroup = {
137 static const struct attribute_group *rproc_devgroups[] = {
142 struct class rproc_class = {
143 .name = "remoteproc",
144 .dev_groups = rproc_devgroups,
147 int __init rproc_init_sysfs(void)
149 /* create remoteproc device class for sysfs */
150 int err = class_register(&rproc_class);
153 pr_err("remoteproc: unable to register class\n");
157 void __exit rproc_exit_sysfs(void)
159 class_unregister(&rproc_class);