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);
18 const char *firmware = rproc->firmware;
21 * If the remote processor has been started by an external
22 * entity we have no idea of what image it is running. As such
23 * simply display a generic string rather then rproc->firmware.
25 * Here we rely on the autonomous flag because a remote processor
26 * may have been attached to and currently in a running state.
28 if (rproc->autonomous)
31 return sprintf(buf, "%s\n", firmware);
34 /* Change firmware name via sysfs */
35 static ssize_t firmware_store(struct device *dev,
36 struct device_attribute *attr,
37 const char *buf, size_t count)
39 struct rproc *rproc = to_rproc(dev);
43 err = mutex_lock_interruptible(&rproc->lock);
45 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, err);
49 if (rproc->state != RPROC_OFFLINE) {
50 dev_err(dev, "can't change firmware while running\n");
55 len = strcspn(buf, "\n");
57 dev_err(dev, "can't provide a NULL firmware\n");
62 p = kstrndup(buf, len, GFP_KERNEL);
68 kfree(rproc->firmware);
71 mutex_unlock(&rproc->lock);
73 return err ? err : count;
75 static DEVICE_ATTR_RW(firmware);
78 * A state-to-string lookup table, for exposing a human readable state
79 * via sysfs. Always keep in sync with enum rproc_state
81 static const char * const rproc_state_string[] = {
82 [RPROC_OFFLINE] = "offline",
83 [RPROC_SUSPENDED] = "suspended",
84 [RPROC_RUNNING] = "running",
85 [RPROC_CRASHED] = "crashed",
86 [RPROC_DELETED] = "deleted",
87 [RPROC_DETACHED] = "detached",
88 [RPROC_LAST] = "invalid",
91 /* Expose the state of the remote processor via sysfs */
92 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
95 struct rproc *rproc = to_rproc(dev);
98 state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
99 return sprintf(buf, "%s\n", rproc_state_string[state]);
102 /* Change remote processor state via sysfs */
103 static ssize_t state_store(struct device *dev,
104 struct device_attribute *attr,
105 const char *buf, size_t count)
107 struct rproc *rproc = to_rproc(dev);
110 if (sysfs_streq(buf, "start")) {
111 if (rproc->state == RPROC_RUNNING)
114 ret = rproc_boot(rproc);
116 dev_err(&rproc->dev, "Boot failed: %d\n", ret);
117 } else if (sysfs_streq(buf, "stop")) {
118 if (rproc->state != RPROC_RUNNING)
121 rproc_shutdown(rproc);
123 dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
126 return ret ? ret : count;
128 static DEVICE_ATTR_RW(state);
130 /* Expose the name of the remote processor via sysfs */
131 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
134 struct rproc *rproc = to_rproc(dev);
136 return sprintf(buf, "%s\n", rproc->name);
138 static DEVICE_ATTR_RO(name);
140 static struct attribute *rproc_attrs[] = {
141 &dev_attr_firmware.attr,
142 &dev_attr_state.attr,
147 static const struct attribute_group rproc_devgroup = {
151 static const struct attribute_group *rproc_devgroups[] = {
156 struct class rproc_class = {
157 .name = "remoteproc",
158 .dev_groups = rproc_devgroups,
161 int __init rproc_init_sysfs(void)
163 /* create remoteproc device class for sysfs */
164 int err = class_register(&rproc_class);
167 pr_err("remoteproc: unable to register class\n");
171 void __exit rproc_exit_sysfs(void)
173 class_unregister(&rproc_class);