]>
Commit | Line | Data |
---|---|---|
1802d0be | 1 | // SPDX-License-Identifier: GPL-2.0-only |
6391a706 OBC |
2 | /* |
3 | * Remote Processor Framework | |
4 | * | |
5 | * Copyright (C) 2011 Texas Instruments, Inc. | |
6 | * Copyright (C) 2011 Google, Inc. | |
7 | * | |
8 | * Ohad Ben-Cohen <ohad@wizery.com> | |
9 | * Mark Grosen <mgrosen@ti.com> | |
10 | * Brian Swetland <swetland@google.com> | |
11 | * Fernando Guzman Lugo <fernando.lugo@ti.com> | |
12 | * Suman Anna <s-anna@ti.com> | |
13 | * Robert Tivy <rtivy@ti.com> | |
14 | * Armando Uribe De Leon <x0095078@ti.com> | |
6391a706 OBC |
15 | */ |
16 | ||
17 | #define pr_fmt(fmt) "%s: " fmt, __func__ | |
18 | ||
19 | #include <linux/kernel.h> | |
20 | #include <linux/debugfs.h> | |
21 | #include <linux/remoteproc.h> | |
22 | #include <linux/device.h> | |
2e37abb8 FGL |
23 | #include <linux/uaccess.h> |
24 | ||
25 | #include "remoteproc_internal.h" | |
6391a706 OBC |
26 | |
27 | /* remoteproc debugfs parent dir */ | |
28 | static struct dentry *rproc_dbg; | |
29 | ||
30 | /* | |
31 | * Some remote processors may support dumping trace logs into a shared | |
32 | * memory buffer. We expose this trace buffer using debugfs, so users | |
33 | * can easily tell what's going on remotely. | |
34 | * | |
35 | * We will most probably improve the rproc tracing facilities later on, | |
36 | * but this kind of lightweight and simple mechanism is always good to have, | |
37 | * as it provides very early tracing with little to no dependencies at all. | |
38 | */ | |
39 | static ssize_t rproc_trace_read(struct file *filp, char __user *userbuf, | |
730f84ce | 40 | size_t count, loff_t *ppos) |
6391a706 | 41 | { |
a987e6b9 LP |
42 | struct rproc_debug_trace *data = filp->private_data; |
43 | struct rproc_mem_entry *trace = &data->trace_mem; | |
44 | void *va; | |
45 | char buf[100]; | |
46 | int len; | |
47 | ||
48 | va = rproc_da_to_va(data->rproc, trace->da, trace->len); | |
49 | ||
50 | if (!va) { | |
51 | len = scnprintf(buf, sizeof(buf), "Trace %s not available\n", | |
52 | trace->name); | |
53 | va = buf; | |
54 | } else { | |
55 | len = strnlen(va, trace->len); | |
56 | } | |
6391a706 | 57 | |
a987e6b9 | 58 | return simple_read_from_buffer(userbuf, count, ppos, va, len); |
6391a706 OBC |
59 | } |
60 | ||
6391a706 OBC |
61 | static const struct file_operations trace_rproc_ops = { |
62 | .read = rproc_trace_read, | |
234e3405 | 63 | .open = simple_open, |
6391a706 OBC |
64 | .llseek = generic_file_llseek, |
65 | }; | |
66 | ||
6391a706 OBC |
67 | /* expose the name of the remote processor via debugfs */ |
68 | static ssize_t rproc_name_read(struct file *filp, char __user *userbuf, | |
730f84ce | 69 | size_t count, loff_t *ppos) |
6391a706 OBC |
70 | { |
71 | struct rproc *rproc = filp->private_data; | |
72 | /* need room for the name, a newline and a terminating null */ | |
73 | char buf[100]; | |
74 | int i; | |
75 | ||
ae768d5f | 76 | i = scnprintf(buf, sizeof(buf), "%.98s\n", rproc->name); |
6391a706 OBC |
77 | |
78 | return simple_read_from_buffer(userbuf, count, ppos, buf, i); | |
79 | } | |
80 | ||
81 | static const struct file_operations rproc_name_ops = { | |
82 | .read = rproc_name_read, | |
234e3405 | 83 | .open = simple_open, |
6391a706 OBC |
84 | .llseek = generic_file_llseek, |
85 | }; | |
86 | ||
2e37abb8 FGL |
87 | /* expose recovery flag via debugfs */ |
88 | static ssize_t rproc_recovery_read(struct file *filp, char __user *userbuf, | |
89 | size_t count, loff_t *ppos) | |
90 | { | |
91 | struct rproc *rproc = filp->private_data; | |
92 | char *buf = rproc->recovery_disabled ? "disabled\n" : "enabled\n"; | |
93 | ||
94 | return simple_read_from_buffer(userbuf, count, ppos, buf, strlen(buf)); | |
95 | } | |
96 | ||
97 | /* | |
98 | * By writing to the 'recovery' debugfs entry, we control the behavior of the | |
99 | * recovery mechanism dynamically. The default value of this entry is "enabled". | |
100 | * | |
101 | * The 'recovery' debugfs entry supports these commands: | |
102 | * | |
103 | * enabled: When enabled, the remote processor will be automatically | |
104 | * recovered whenever it crashes. Moreover, if the remote | |
105 | * processor crashes while recovery is disabled, it will | |
106 | * be automatically recovered too as soon as recovery is enabled. | |
107 | * | |
108 | * disabled: When disabled, a remote processor will remain in a crashed | |
109 | * state if it crashes. This is useful for debugging purposes; | |
110 | * without it, debugging a crash is substantially harder. | |
111 | * | |
112 | * recover: This function will trigger an immediate recovery if the | |
113 | * remote processor is in a crashed state, without changing | |
114 | * or checking the recovery state (enabled/disabled). | |
115 | * This is useful during debugging sessions, when one expects | |
116 | * additional crashes to happen after enabling recovery. In this | |
117 | * case, enabling recovery will make it hard to debug subsequent | |
118 | * crashes, so it's recommended to keep recovery disabled, and | |
119 | * instead use the "recover" command as needed. | |
120 | */ | |
121 | static ssize_t | |
122 | rproc_recovery_write(struct file *filp, const char __user *user_buf, | |
123 | size_t count, loff_t *ppos) | |
124 | { | |
125 | struct rproc *rproc = filp->private_data; | |
126 | char buf[10]; | |
127 | int ret; | |
128 | ||
92792e48 | 129 | if (count < 1 || count > sizeof(buf)) |
47fff9fd | 130 | return -EINVAL; |
2e37abb8 FGL |
131 | |
132 | ret = copy_from_user(buf, user_buf, count); | |
133 | if (ret) | |
bec109a4 | 134 | return -EFAULT; |
2e37abb8 FGL |
135 | |
136 | /* remove end of line */ | |
137 | if (buf[count - 1] == '\n') | |
138 | buf[count - 1] = '\0'; | |
139 | ||
140 | if (!strncmp(buf, "enabled", count)) { | |
e138cce3 | 141 | /* change the flag and begin the recovery process if needed */ |
2e37abb8 | 142 | rproc->recovery_disabled = false; |
e138cce3 | 143 | rproc_trigger_recovery(rproc); |
2e37abb8 FGL |
144 | } else if (!strncmp(buf, "disabled", count)) { |
145 | rproc->recovery_disabled = true; | |
146 | } else if (!strncmp(buf, "recover", count)) { | |
e138cce3 AE |
147 | /* begin the recovery process without changing the flag */ |
148 | rproc_trigger_recovery(rproc); | |
1f2f65c4 AE |
149 | } else { |
150 | return -EINVAL; | |
2e37abb8 FGL |
151 | } |
152 | ||
153 | return count; | |
154 | } | |
155 | ||
156 | static const struct file_operations rproc_recovery_ops = { | |
157 | .read = rproc_recovery_read, | |
158 | .write = rproc_recovery_write, | |
159 | .open = simple_open, | |
160 | .llseek = generic_file_llseek, | |
161 | }; | |
162 | ||
60042a22 XX |
163 | /* expose the crash trigger via debugfs */ |
164 | static ssize_t | |
165 | rproc_crash_write(struct file *filp, const char __user *user_buf, | |
166 | size_t count, loff_t *ppos) | |
167 | { | |
168 | struct rproc *rproc = filp->private_data; | |
169 | unsigned int type; | |
170 | int ret; | |
171 | ||
172 | ret = kstrtouint_from_user(user_buf, count, 0, &type); | |
173 | if (ret < 0) | |
174 | return ret; | |
175 | ||
176 | rproc_report_crash(rproc, type); | |
177 | ||
178 | return count; | |
179 | } | |
180 | ||
181 | static const struct file_operations rproc_crash_ops = { | |
182 | .write = rproc_crash_write, | |
183 | .open = simple_open, | |
184 | .llseek = generic_file_llseek, | |
185 | }; | |
186 | ||
bdd8edb9 LP |
187 | /* Expose resource table content via debugfs */ |
188 | static int rproc_rsc_table_show(struct seq_file *seq, void *p) | |
189 | { | |
190 | static const char * const types[] = {"carveout", "devmem", "trace", "vdev"}; | |
191 | struct rproc *rproc = seq->private; | |
192 | struct resource_table *table = rproc->table_ptr; | |
193 | struct fw_rsc_carveout *c; | |
194 | struct fw_rsc_devmem *d; | |
195 | struct fw_rsc_trace *t; | |
196 | struct fw_rsc_vdev *v; | |
197 | int i, j; | |
198 | ||
199 | if (!table) { | |
200 | seq_puts(seq, "No resource table found\n"); | |
201 | return 0; | |
202 | } | |
203 | ||
204 | for (i = 0; i < table->num; i++) { | |
205 | int offset = table->offset[i]; | |
206 | struct fw_rsc_hdr *hdr = (void *)table + offset; | |
207 | void *rsc = (void *)hdr + sizeof(*hdr); | |
208 | ||
209 | switch (hdr->type) { | |
210 | case RSC_CARVEOUT: | |
211 | c = rsc; | |
212 | seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]); | |
213 | seq_printf(seq, " Device Address 0x%x\n", c->da); | |
214 | seq_printf(seq, " Physical Address 0x%x\n", c->pa); | |
215 | seq_printf(seq, " Length 0x%x Bytes\n", c->len); | |
216 | seq_printf(seq, " Flags 0x%x\n", c->flags); | |
217 | seq_printf(seq, " Reserved (should be zero) [%d]\n", c->reserved); | |
218 | seq_printf(seq, " Name %s\n\n", c->name); | |
219 | break; | |
220 | case RSC_DEVMEM: | |
221 | d = rsc; | |
222 | seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]); | |
223 | seq_printf(seq, " Device Address 0x%x\n", d->da); | |
224 | seq_printf(seq, " Physical Address 0x%x\n", d->pa); | |
225 | seq_printf(seq, " Length 0x%x Bytes\n", d->len); | |
226 | seq_printf(seq, " Flags 0x%x\n", d->flags); | |
227 | seq_printf(seq, " Reserved (should be zero) [%d]\n", d->reserved); | |
228 | seq_printf(seq, " Name %s\n\n", d->name); | |
229 | break; | |
230 | case RSC_TRACE: | |
231 | t = rsc; | |
232 | seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]); | |
233 | seq_printf(seq, " Device Address 0x%x\n", t->da); | |
234 | seq_printf(seq, " Length 0x%x Bytes\n", t->len); | |
235 | seq_printf(seq, " Reserved (should be zero) [%d]\n", t->reserved); | |
236 | seq_printf(seq, " Name %s\n\n", t->name); | |
237 | break; | |
238 | case RSC_VDEV: | |
239 | v = rsc; | |
240 | seq_printf(seq, "Entry %d is of type %s\n", i, types[hdr->type]); | |
241 | ||
242 | seq_printf(seq, " ID %d\n", v->id); | |
243 | seq_printf(seq, " Notify ID %d\n", v->notifyid); | |
244 | seq_printf(seq, " Device features 0x%x\n", v->dfeatures); | |
245 | seq_printf(seq, " Guest features 0x%x\n", v->gfeatures); | |
246 | seq_printf(seq, " Config length 0x%x\n", v->config_len); | |
247 | seq_printf(seq, " Status 0x%x\n", v->status); | |
248 | seq_printf(seq, " Number of vrings %d\n", v->num_of_vrings); | |
249 | seq_printf(seq, " Reserved (should be zero) [%d][%d]\n\n", | |
250 | v->reserved[0], v->reserved[1]); | |
251 | ||
252 | for (j = 0; j < v->num_of_vrings; j++) { | |
253 | seq_printf(seq, " Vring %d\n", j); | |
254 | seq_printf(seq, " Device Address 0x%x\n", v->vring[j].da); | |
255 | seq_printf(seq, " Alignment %d\n", v->vring[j].align); | |
256 | seq_printf(seq, " Number of buffers %d\n", v->vring[j].num); | |
257 | seq_printf(seq, " Notify ID %d\n", v->vring[j].notifyid); | |
258 | seq_printf(seq, " Physical Address 0x%x\n\n", | |
259 | v->vring[j].pa); | |
260 | } | |
261 | break; | |
262 | default: | |
276ec993 | 263 | seq_printf(seq, "Unknown resource type found: %d [hdr: %pK]\n", |
bdd8edb9 LP |
264 | hdr->type, hdr); |
265 | break; | |
266 | } | |
267 | } | |
268 | ||
269 | return 0; | |
270 | } | |
271 | ||
c78bc072 | 272 | DEFINE_SHOW_ATTRIBUTE(rproc_rsc_table); |
bdd8edb9 | 273 | |
b8918839 LP |
274 | /* Expose carveout content via debugfs */ |
275 | static int rproc_carveouts_show(struct seq_file *seq, void *p) | |
276 | { | |
277 | struct rproc *rproc = seq->private; | |
278 | struct rproc_mem_entry *carveout; | |
279 | ||
280 | list_for_each_entry(carveout, &rproc->carveouts, node) { | |
281 | seq_puts(seq, "Carveout memory entry:\n"); | |
3265230c | 282 | seq_printf(seq, "\tName: %s\n", carveout->name); |
276ec993 | 283 | seq_printf(seq, "\tVirtual address: %pK\n", carveout->va); |
b8918839 LP |
284 | seq_printf(seq, "\tDMA address: %pad\n", &carveout->dma); |
285 | seq_printf(seq, "\tDevice address: 0x%x\n", carveout->da); | |
096ee786 | 286 | seq_printf(seq, "\tLength: 0x%zx Bytes\n\n", carveout->len); |
b8918839 LP |
287 | } |
288 | ||
289 | return 0; | |
290 | } | |
291 | ||
c78bc072 | 292 | DEFINE_SHOW_ATTRIBUTE(rproc_carveouts); |
b8918839 | 293 | |
6391a706 OBC |
294 | void rproc_remove_trace_file(struct dentry *tfile) |
295 | { | |
296 | debugfs_remove(tfile); | |
297 | } | |
298 | ||
299 | struct dentry *rproc_create_trace_file(const char *name, struct rproc *rproc, | |
a987e6b9 | 300 | struct rproc_debug_trace *trace) |
6391a706 OBC |
301 | { |
302 | struct dentry *tfile; | |
303 | ||
730f84ce AS |
304 | tfile = debugfs_create_file(name, 0400, rproc->dbg_dir, trace, |
305 | &trace_rproc_ops); | |
6391a706 | 306 | if (!tfile) { |
b5ab5e24 | 307 | dev_err(&rproc->dev, "failed to create debugfs trace entry\n"); |
6391a706 OBC |
308 | return NULL; |
309 | } | |
310 | ||
311 | return tfile; | |
312 | } | |
313 | ||
314 | void rproc_delete_debug_dir(struct rproc *rproc) | |
315 | { | |
6391a706 OBC |
316 | debugfs_remove_recursive(rproc->dbg_dir); |
317 | } | |
318 | ||
319 | void rproc_create_debug_dir(struct rproc *rproc) | |
320 | { | |
b5ab5e24 | 321 | struct device *dev = &rproc->dev; |
6391a706 OBC |
322 | |
323 | if (!rproc_dbg) | |
324 | return; | |
325 | ||
326 | rproc->dbg_dir = debugfs_create_dir(dev_name(dev), rproc_dbg); | |
327 | if (!rproc->dbg_dir) | |
328 | return; | |
329 | ||
330 | debugfs_create_file("name", 0400, rproc->dbg_dir, | |
730f84ce | 331 | rproc, &rproc_name_ops); |
e138cce3 | 332 | debugfs_create_file("recovery", 0600, rproc->dbg_dir, |
730f84ce | 333 | rproc, &rproc_recovery_ops); |
60042a22 XX |
334 | debugfs_create_file("crash", 0200, rproc->dbg_dir, |
335 | rproc, &rproc_crash_ops); | |
bdd8edb9 | 336 | debugfs_create_file("resource_table", 0400, rproc->dbg_dir, |
c78bc072 | 337 | rproc, &rproc_rsc_table_fops); |
b8918839 | 338 | debugfs_create_file("carveout_memories", 0400, rproc->dbg_dir, |
c78bc072 | 339 | rproc, &rproc_carveouts_fops); |
6391a706 OBC |
340 | } |
341 | ||
342 | void __init rproc_init_debugfs(void) | |
343 | { | |
344 | if (debugfs_initialized()) { | |
345 | rproc_dbg = debugfs_create_dir(KBUILD_MODNAME, NULL); | |
346 | if (!rproc_dbg) | |
347 | pr_err("can't create debugfs dir\n"); | |
348 | } | |
349 | } | |
350 | ||
351 | void __exit rproc_exit_debugfs(void) | |
352 | { | |
57971159 | 353 | debugfs_remove(rproc_dbg); |
6391a706 | 354 | } |