xref: /OK3568_Linux_fs/kernel/drivers/remoteproc/remoteproc_sysfs.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 sprintf(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 sprintf(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 	 * Here we rely on the autonomous flag because a remote processor
146 	 * may have been attached to and currently in a running state.
147 	 */
148 	if (rproc->autonomous)
149 		firmware = "unknown";
150 
151 	return sprintf(buf, "%s\n", firmware);
152 }
153 
154 /* Change firmware name via sysfs */
firmware_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)155 static ssize_t firmware_store(struct device *dev,
156 			      struct device_attribute *attr,
157 			      const char *buf, size_t count)
158 {
159 	struct rproc *rproc = to_rproc(dev);
160 	int err;
161 
162 	err = rproc_set_firmware(rproc, buf);
163 
164 	return err ? err : count;
165 }
166 static DEVICE_ATTR_RW(firmware);
167 
168 /*
169  * A state-to-string lookup table, for exposing a human readable state
170  * via sysfs. Always keep in sync with enum rproc_state
171  */
172 static const char * const rproc_state_string[] = {
173 	[RPROC_OFFLINE]		= "offline",
174 	[RPROC_SUSPENDED]	= "suspended",
175 	[RPROC_RUNNING]		= "running",
176 	[RPROC_CRASHED]		= "crashed",
177 	[RPROC_DELETED]		= "deleted",
178 	[RPROC_DETACHED]	= "detached",
179 	[RPROC_LAST]		= "invalid",
180 };
181 
182 /* Expose the state of the remote processor via sysfs */
state_show(struct device * dev,struct device_attribute * attr,char * buf)183 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
184 			  char *buf)
185 {
186 	struct rproc *rproc = to_rproc(dev);
187 	unsigned int state;
188 
189 	state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
190 	return sprintf(buf, "%s\n", rproc_state_string[state]);
191 }
192 
193 /* Change remote processor state via sysfs */
state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)194 static ssize_t state_store(struct device *dev,
195 			      struct device_attribute *attr,
196 			      const char *buf, size_t count)
197 {
198 	struct rproc *rproc = to_rproc(dev);
199 	int ret = 0;
200 
201 	if (sysfs_streq(buf, "start")) {
202 		if (rproc->state == RPROC_RUNNING)
203 			return -EBUSY;
204 
205 		ret = rproc_boot(rproc);
206 		if (ret)
207 			dev_err(&rproc->dev, "Boot failed: %d\n", ret);
208 	} else if (sysfs_streq(buf, "stop")) {
209 		if (rproc->state != RPROC_RUNNING)
210 			return -EINVAL;
211 
212 		rproc_shutdown(rproc);
213 	} else {
214 		dev_err(&rproc->dev, "Unrecognised option: %s\n", buf);
215 		ret = -EINVAL;
216 	}
217 	return ret ? ret : count;
218 }
219 static DEVICE_ATTR_RW(state);
220 
221 /* Expose the name of the remote processor via sysfs */
name_show(struct device * dev,struct device_attribute * attr,char * buf)222 static ssize_t name_show(struct device *dev, struct device_attribute *attr,
223 			 char *buf)
224 {
225 	struct rproc *rproc = to_rproc(dev);
226 
227 	return sprintf(buf, "%s\n", rproc->name);
228 }
229 static DEVICE_ATTR_RO(name);
230 
231 static struct attribute *rproc_attrs[] = {
232 	&dev_attr_coredump.attr,
233 	&dev_attr_recovery.attr,
234 	&dev_attr_firmware.attr,
235 	&dev_attr_state.attr,
236 	&dev_attr_name.attr,
237 	NULL
238 };
239 
240 static const struct attribute_group rproc_devgroup = {
241 	.attrs = rproc_attrs
242 };
243 
244 static const struct attribute_group *rproc_devgroups[] = {
245 	&rproc_devgroup,
246 	NULL
247 };
248 
249 struct class rproc_class = {
250 	.name		= "remoteproc",
251 	.dev_groups	= rproc_devgroups,
252 };
253 
rproc_init_sysfs(void)254 int __init rproc_init_sysfs(void)
255 {
256 	/* create remoteproc device class for sysfs */
257 	int err = class_register(&rproc_class);
258 
259 	if (err)
260 		pr_err("remoteproc: unable to register class\n");
261 	return err;
262 }
263 
rproc_exit_sysfs(void)264 void __exit rproc_exit_sysfs(void)
265 {
266 	class_unregister(&rproc_class);
267 }
268