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