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