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