1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#define pr_fmt(fmt) "%s: " fmt, __func__
26
27#include <linux/kernel.h>
28#include <linux/debugfs.h>
29#include <linux/remoteproc.h>
30#include <linux/device.h>
31#include <linux/uaccess.h>
32
33#include "remoteproc_internal.h"
34
35
36static struct dentry *rproc_dbg;
37
38
39
40
41
42
43
44
45
46
47static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf,
48 size_t count, loff_t *ppos)
49{
50 struct rproc_mem_entry *trace = filp->private_data;
51 int len = strnlen(trace->va, trace->len);
52
53 return simple_read_from_buffer(userbuf, count, ppos, trace->va, len);
54}
55
56static const struct file_operations trace_rproc_ops = {
57 .read = rproc_trace_read,
58 .open = simple_open,
59 .llseek = generic_file_llseek,
60};
61
62
63
64
65
66static const char * const rproc_state_string[] = {
67 "offline",
68 "suspended",
69 "running",
70 "crashed",
71 "invalid",
72};
73
74
75static ssize_t rproc_state_read(struct file *filp, char __user *userbuf,
76 size_t count, loff_t *ppos)
77{
78 struct rproc *rproc = filp->private_data;
79 unsigned int state;
80 char buf[30];
81 int i;
82
83 state = rproc->state > RPROC_LAST ? RPROC_LAST : rproc->state;
84
85 i = scnprintf(buf, 30, "%.28s (%d)\n", rproc_state_string[state],
86 rproc->state);
87
88 return simple_read_from_buffer(userbuf, count, ppos, buf, i);
89}
90
91static const struct file_operations rproc_state_ops = {
92 .read = rproc_state_read,
93 .open = simple_open,
94 .llseek = generic_file_llseek,
95};
96
97
98static ssize_t rproc_name_read(struct file *filp, char __user *userbuf,
99 size_t count, loff_t *ppos)
100{
101 struct rproc *rproc = filp->private_data;
102
103 char buf[100];
104 int i;
105
106 i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name);
107
108 return simple_read_from_buffer(userbuf, count, ppos, buf, i);
109}
110
111static const struct file_operations rproc_name_ops = {
112 .read = rproc_name_read,
113 .open = simple_open,
114 .llseek = generic_file_llseek,
115};
116
117
118static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf,
119 size_t count, loff_t *ppos)
120{
121 struct rproc *rproc = filp->private_data;
122 char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n";
123
124 return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf));
125}
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151static ssize_t
152rproc_recovery_write(struct file *filp, const char __user *user_buf,
153 size_t count, loff_t *ppos)
154{
155 struct rproc *rproc = filp->private_data;
156 char buf[10];
157 int ret;
158
159 if (count > sizeof(buf))
160 return count;
161
162 ret = copy_from_user(buf, user_buf, count);
163 if (ret)
164 return -EFAULT;
165
166
167 if (buf[count - 1] == '\n')
168 buf[count - 1] = '\0';
169
170 if (!strncmp(buf, "enabled", count)) {
171 rproc->recovery_disabled = false;
172
173 if (rproc->state == RPROC_CRASHED)
174 rproc_trigger_recovery(rproc);
175 } else if (!strncmp(buf, "disabled", count)) {
176 rproc->recovery_disabled = true;
177 } else if (!strncmp(buf, "recover", count)) {
178
179 if (rproc->state == RPROC_CRASHED)
180 rproc_trigger_recovery(rproc);
181 }
182
183 return count;
184}
185
186static const struct file_operations rproc_recovery_ops = {
187 .read = rproc_recovery_read,
188 .write = rproc_recovery_write,
189 .open = simple_open,
190 .llseek = generic_file_llseek,
191};
192
193void rproc_remove_trace_file(struct dentry *tfile)
194{
195 debugfs_remove(tfile);
196}
197
198struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc,
199 struct rproc_mem_entry *trace)
200{
201 struct dentry *tfile;
202
203 tfile = debugfs_create_file(name, 0400, rproc->dbg_dir,
204 trace, &trace_rproc_ops);
205 if (!tfile) {
206 dev_err(&rproc->dev, "failed to create debugfs trace entry\n");
207 return NULL;
208 }
209
210 return tfile;
211}
212
213void rproc_delete_debug_dir(struct rproc *rproc)
214{
215 if (!rproc->dbg_dir)
216 return;
217
218 debugfs_remove_recursive(rproc->dbg_dir);
219}
220
221void rproc_create_debug_dir(struct rproc *rproc)
222{
223 struct device *dev = &rproc->dev;
224
225 if (!rproc_dbg)
226 return;
227
228 rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg);
229 if (!rproc->dbg_dir)
230 return;
231
232 debugfs_create_file("name", 0400, rproc->dbg_dir,
233 rproc, &rproc_name_ops);
234 debugfs_create_file("state", 0400, rproc->dbg_dir,
235 rproc, &rproc_state_ops);
236 debugfs_create_file("recovery", 0400, rproc->dbg_dir,
237 rproc, &rproc_recovery_ops);
238}
239
240void __init rproc_init_debugfs(void)
241{
242 if (debugfs_initialized()) {
243 rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL);
244 if (!rproc_dbg)
245 pr_err("can't create debugfs dir\n");
246 }
247}
248
249void __exit rproc_exit_debugfs(void)
250{
251 debugfs_remove(rproc_dbg);
252}
253