Commit | Line | Data |
---|---|---|
400e64df OBC |
1 | /* |
2 | * Remote Processor Framework | |
3 | * | |
4 | * Copyright (C) 2011 Texas Instruments, Inc. | |
5 | * Copyright (C) 2011 Google, Inc. | |
6 | * | |
7 | * Ohad Ben-Cohen <ohad@wizery.com> | |
8 | * Brian Swetland <swetland@google.com> | |
9 | * Mark Grosen <mgrosen@ti.com> | |
10 | * Fernando Guzman Lugo <fernando.lugo@ti.com> | |
11 | * Suman Anna <s-anna@ti.com> | |
12 | * Robert Tivy <rtivy@ti.com> | |
13 | * Armando Uribe De Leon <x0095078@ti.com> | |
14 | * | |
15 | * This program is free software; you can redistribute it and/or | |
16 | * modify it under the terms of the GNU General Public License | |
17 | * version 2 as published by the Free Software Foundation. | |
18 | * | |
19 | * This program is distributed in the hope that it will be useful, | |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
22 | * GNU General Public License for more details. | |
23 | */ | |
24 | ||
25 | #define pr_fmt(fmt) "%s: " fmt, __func__ | |
26 | ||
27 | #include <linux/kernel.h> | |
28 | #include <linux/module.h> | |
29 | #include <linux/device.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/mutex.h> | |
32 | #include <linux/dma-mapping.h> | |
33 | #include <linux/firmware.h> | |
34 | #include <linux/string.h> | |
35 | #include <linux/debugfs.h> | |
36 | #include <linux/remoteproc.h> | |
37 | #include <linux/iommu.h> | |
b5ab5e24 | 38 | #include <linux/idr.h> |
400e64df | 39 | #include <linux/elf.h> |
a2b950ac | 40 | #include <linux/crc32.h> |
400e64df OBC |
41 | #include <linux/virtio_ids.h> |
42 | #include <linux/virtio_ring.h> | |
cf59d3e9 | 43 | #include <asm/byteorder.h> |
400e64df OBC |
44 | |
45 | #include "remoteproc_internal.h" | |
46 | ||
400e64df | 47 | typedef int (*rproc_handle_resources_t)(struct rproc *rproc, |
fd2c15ec | 48 | struct resource_table *table, int len); |
a2b950ac OBC |
49 | typedef int (*rproc_handle_resource_t)(struct rproc *rproc, |
50 | void *, int offset, int avail); | |
400e64df | 51 | |
b5ab5e24 OBC |
52 | /* Unique indices for remoteproc devices */ |
53 | static DEFINE_IDA(rproc_dev_index); | |
54 | ||
8afd519c FGL |
55 | static const char * const rproc_crash_names[] = { |
56 | [RPROC_MMUFAULT] = "mmufault", | |
57 | }; | |
58 | ||
59 | /* translate rproc_crash_type to string */ | |
60 | static const char *rproc_crash_to_string(enum rproc_crash_type type) | |
61 | { | |
62 | if (type < ARRAY_SIZE(rproc_crash_names)) | |
63 | return rproc_crash_names[type]; | |
b23f7a09 | 64 | return "unknown"; |
8afd519c FGL |
65 | } |
66 | ||
400e64df OBC |
67 | /* |
68 | * This is the IOMMU fault handler we register with the IOMMU API | |
69 | * (when relevant; not all remote processors access memory through | |
70 | * an IOMMU). | |
71 | * | |
72 | * IOMMU core will invoke this handler whenever the remote processor | |
73 | * will try to access an unmapped device address. | |
400e64df OBC |
74 | */ |
75 | static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, | |
77ca2332 | 76 | unsigned long iova, int flags, void *token) |
400e64df | 77 | { |
8afd519c FGL |
78 | struct rproc *rproc = token; |
79 | ||
400e64df OBC |
80 | dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags); |
81 | ||
8afd519c FGL |
82 | rproc_report_crash(rproc, RPROC_MMUFAULT); |
83 | ||
400e64df OBC |
84 | /* |
85 | * Let the iommu core know we're not really handling this fault; | |
8afd519c | 86 | * we just used it as a recovery trigger. |
400e64df OBC |
87 | */ |
88 | return -ENOSYS; | |
89 | } | |
90 | ||
91 | static int rproc_enable_iommu(struct rproc *rproc) | |
92 | { | |
93 | struct iommu_domain *domain; | |
b5ab5e24 | 94 | struct device *dev = rproc->dev.parent; |
400e64df OBC |
95 | int ret; |
96 | ||
315491e5 SA |
97 | if (!rproc->has_iommu) { |
98 | dev_dbg(dev, "iommu not present\n"); | |
0798e1da | 99 | return 0; |
400e64df OBC |
100 | } |
101 | ||
102 | domain = iommu_domain_alloc(dev->bus); | |
103 | if (!domain) { | |
104 | dev_err(dev, "can't alloc iommu domain\n"); | |
105 | return -ENOMEM; | |
106 | } | |
107 | ||
77ca2332 | 108 | iommu_set_fault_handler(domain, rproc_iommu_fault, rproc); |
400e64df OBC |
109 | |
110 | ret = iommu_attach_device(domain, dev); | |
111 | if (ret) { | |
112 | dev_err(dev, "can't attach iommu device: %d\n", ret); | |
113 | goto free_domain; | |
114 | } | |
115 | ||
116 | rproc->domain = domain; | |
117 | ||
118 | return 0; | |
119 | ||
120 | free_domain: | |
121 | iommu_domain_free(domain); | |
122 | return ret; | |
123 | } | |
124 | ||
125 | static void rproc_disable_iommu(struct rproc *rproc) | |
126 | { | |
127 | struct iommu_domain *domain = rproc->domain; | |
b5ab5e24 | 128 | struct device *dev = rproc->dev.parent; |
400e64df OBC |
129 | |
130 | if (!domain) | |
131 | return; | |
132 | ||
133 | iommu_detach_device(domain, dev); | |
134 | iommu_domain_free(domain); | |
400e64df OBC |
135 | } |
136 | ||
137 | /* | |
138 | * Some remote processors will ask us to allocate them physically contiguous | |
139 | * memory regions (which we call "carveouts"), and map them to specific | |
140 | * device addresses (which are hardcoded in the firmware). | |
141 | * | |
142 | * They may then ask us to copy objects into specific device addresses (e.g. | |
143 | * code/data sections) or expose us certain symbols in other device address | |
144 | * (e.g. their trace buffer). | |
145 | * | |
146 | * This function is an internal helper with which we can go over the allocated | |
147 | * carveouts and translate specific device address to kernel virtual addresses | |
148 | * so we can access the referenced memory. | |
149 | * | |
150 | * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, | |
151 | * but only on kernel direct mapped RAM memory. Instead, we're just using | |
152 | * here the output of the DMA API, which should be more correct. | |
153 | */ | |
72854fb0 | 154 | void *rproc_da_to_va(struct rproc *rproc, u64 da, int len) |
400e64df OBC |
155 | { |
156 | struct rproc_mem_entry *carveout; | |
157 | void *ptr = NULL; | |
158 | ||
159 | list_for_each_entry(carveout, &rproc->carveouts, node) { | |
160 | int offset = da - carveout->da; | |
161 | ||
162 | /* try next carveout if da is too small */ | |
163 | if (offset < 0) | |
164 | continue; | |
165 | ||
166 | /* try next carveout if da is too large */ | |
167 | if (offset + len > carveout->len) | |
168 | continue; | |
169 | ||
170 | ptr = carveout->va + offset; | |
171 | ||
172 | break; | |
173 | } | |
174 | ||
175 | return ptr; | |
176 | } | |
4afc89d6 | 177 | EXPORT_SYMBOL(rproc_da_to_va); |
400e64df | 178 | |
6db20ea8 | 179 | int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) |
400e64df | 180 | { |
7a186941 | 181 | struct rproc *rproc = rvdev->rproc; |
b5ab5e24 | 182 | struct device *dev = &rproc->dev; |
6db20ea8 | 183 | struct rproc_vring *rvring = &rvdev->vring[i]; |
c0d63157 | 184 | struct fw_rsc_vdev *rsc; |
7a186941 OBC |
185 | dma_addr_t dma; |
186 | void *va; | |
187 | int ret, size, notifyid; | |
400e64df | 188 | |
7a186941 | 189 | /* actual size of vring (in bytes) */ |
6db20ea8 | 190 | size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); |
7a186941 | 191 | |
7a186941 OBC |
192 | /* |
193 | * Allocate non-cacheable memory for the vring. In the future | |
194 | * this call will also configure the IOMMU for us | |
195 | */ | |
b5ab5e24 | 196 | va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL); |
7a186941 | 197 | if (!va) { |
b5ab5e24 | 198 | dev_err(dev->parent, "dma_alloc_coherent failed\n"); |
400e64df OBC |
199 | return -EINVAL; |
200 | } | |
201 | ||
6db20ea8 OBC |
202 | /* |
203 | * Assign an rproc-wide unique index for this vring | |
204 | * TODO: assign a notifyid for rvdev updates as well | |
6db20ea8 OBC |
205 | * TODO: support predefined notifyids (via resource table) |
206 | */ | |
15fc6110 | 207 | ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); |
b39599b7 | 208 | if (ret < 0) { |
15fc6110 | 209 | dev_err(dev, "idr_alloc failed: %d\n", ret); |
b5ab5e24 | 210 | dma_free_coherent(dev->parent, size, va, dma); |
7a186941 OBC |
211 | return ret; |
212 | } | |
15fc6110 | 213 | notifyid = ret; |
400e64df | 214 | |
d09f53a7 EG |
215 | dev_dbg(dev, "vring%d: va %p dma %llx size %x idr %d\n", i, va, |
216 | (unsigned long long)dma, size, notifyid); | |
7a186941 | 217 | |
6db20ea8 OBC |
218 | rvring->va = va; |
219 | rvring->dma = dma; | |
220 | rvring->notifyid = notifyid; | |
400e64df | 221 | |
c0d63157 SB |
222 | /* |
223 | * Let the rproc know the notifyid and da of this vring. | |
224 | * Not all platforms use dma_alloc_coherent to automatically | |
225 | * set up the iommu. In this case the device address (da) will | |
226 | * hold the physical address and not the device address. | |
227 | */ | |
228 | rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; | |
229 | rsc->vring[i].da = dma; | |
230 | rsc->vring[i].notifyid = notifyid; | |
400e64df OBC |
231 | return 0; |
232 | } | |
233 | ||
6db20ea8 OBC |
234 | static int |
235 | rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) | |
7a186941 OBC |
236 | { |
237 | struct rproc *rproc = rvdev->rproc; | |
b5ab5e24 | 238 | struct device *dev = &rproc->dev; |
6db20ea8 OBC |
239 | struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; |
240 | struct rproc_vring *rvring = &rvdev->vring[i]; | |
7a186941 | 241 | |
6db20ea8 OBC |
242 | dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n", |
243 | i, vring->da, vring->num, vring->align); | |
7a186941 | 244 | |
6db20ea8 OBC |
245 | /* make sure reserved bytes are zeroes */ |
246 | if (vring->reserved) { | |
247 | dev_err(dev, "vring rsc has non zero reserved bytes\n"); | |
248 | return -EINVAL; | |
249 | } | |
7a186941 | 250 | |
6db20ea8 OBC |
251 | /* verify queue size and vring alignment are sane */ |
252 | if (!vring->num || !vring->align) { | |
253 | dev_err(dev, "invalid qsz (%d) or alignment (%d)\n", | |
254 | vring->num, vring->align); | |
255 | return -EINVAL; | |
7a186941 | 256 | } |
6db20ea8 OBC |
257 | |
258 | rvring->len = vring->num; | |
259 | rvring->align = vring->align; | |
260 | rvring->rvdev = rvdev; | |
261 | ||
262 | return 0; | |
263 | } | |
264 | ||
265 | void rproc_free_vring(struct rproc_vring *rvring) | |
266 | { | |
267 | int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); | |
268 | struct rproc *rproc = rvring->rvdev->rproc; | |
c0d63157 SB |
269 | int idx = rvring->rvdev->vring - rvring; |
270 | struct fw_rsc_vdev *rsc; | |
6db20ea8 | 271 | |
b5ab5e24 | 272 | dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma); |
6db20ea8 | 273 | idr_remove(&rproc->notifyids, rvring->notifyid); |
099a3f33 | 274 | |
c0d63157 SB |
275 | /* reset resource entry info */ |
276 | rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; | |
277 | rsc->vring[idx].da = 0; | |
278 | rsc->vring[idx].notifyid = -1; | |
7a186941 OBC |
279 | } |
280 | ||
400e64df | 281 | /** |
fd2c15ec | 282 | * rproc_handle_vdev() - handle a vdev fw resource |
400e64df OBC |
283 | * @rproc: the remote processor |
284 | * @rsc: the vring resource descriptor | |
fd2c15ec | 285 | * @avail: size of available data (for sanity checking the image) |
400e64df | 286 | * |
7a186941 OBC |
287 | * This resource entry requests the host to statically register a virtio |
288 | * device (vdev), and setup everything needed to support it. It contains | |
289 | * everything needed to make it possible: the virtio device id, virtio | |
290 | * device features, vrings information, virtio config space, etc... | |
291 | * | |
292 | * Before registering the vdev, the vrings are allocated from non-cacheable | |
293 | * physically contiguous memory. Currently we only support two vrings per | |
294 | * remote processor (temporary limitation). We might also want to consider | |
295 | * doing the vring allocation only later when ->find_vqs() is invoked, and | |
296 | * then release them upon ->del_vqs(). | |
297 | * | |
298 | * Note: @da is currently not really handled correctly: we dynamically | |
299 | * allocate it using the DMA API, ignoring requested hard coded addresses, | |
300 | * and we don't take care of any required IOMMU programming. This is all | |
301 | * going to be taken care of when the generic iommu-based DMA API will be | |
302 | * merged. Meanwhile, statically-addressed iommu-based firmware images should | |
303 | * use RSC_DEVMEM resource entries to map their required @da to the physical | |
304 | * address of their base CMA region (ouch, hacky!). | |
400e64df OBC |
305 | * |
306 | * Returns 0 on success, or an appropriate error code otherwise | |
307 | */ | |
fd2c15ec | 308 | static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc, |
a2b950ac | 309 | int offset, int avail) |
400e64df | 310 | { |
b5ab5e24 | 311 | struct device *dev = &rproc->dev; |
7a186941 OBC |
312 | struct rproc_vdev *rvdev; |
313 | int i, ret; | |
400e64df | 314 | |
fd2c15ec OBC |
315 | /* make sure resource isn't truncated */ |
316 | if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring) | |
317 | + rsc->config_len > avail) { | |
b5ab5e24 | 318 | dev_err(dev, "vdev rsc is truncated\n"); |
400e64df OBC |
319 | return -EINVAL; |
320 | } | |
321 | ||
fd2c15ec OBC |
322 | /* make sure reserved bytes are zeroes */ |
323 | if (rsc->reserved[0] || rsc->reserved[1]) { | |
324 | dev_err(dev, "vdev rsc has non zero reserved bytes\n"); | |
400e64df OBC |
325 | return -EINVAL; |
326 | } | |
327 | ||
fd2c15ec OBC |
328 | dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n", |
329 | rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); | |
330 | ||
7a186941 OBC |
331 | /* we currently support only two vrings per rvdev */ |
332 | if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { | |
fd2c15ec | 333 | dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); |
400e64df OBC |
334 | return -EINVAL; |
335 | } | |
336 | ||
7a186941 OBC |
337 | rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL); |
338 | if (!rvdev) | |
339 | return -ENOMEM; | |
400e64df | 340 | |
7a186941 | 341 | rvdev->rproc = rproc; |
400e64df | 342 | |
6db20ea8 | 343 | /* parse the vrings */ |
7a186941 | 344 | for (i = 0; i < rsc->num_of_vrings; i++) { |
6db20ea8 | 345 | ret = rproc_parse_vring(rvdev, rsc, i); |
7a186941 | 346 | if (ret) |
6db20ea8 | 347 | goto free_rvdev; |
7a186941 | 348 | } |
400e64df | 349 | |
a2b950ac OBC |
350 | /* remember the resource offset*/ |
351 | rvdev->rsc_offset = offset; | |
fd2c15ec | 352 | |
7a186941 | 353 | list_add_tail(&rvdev->node, &rproc->rvdevs); |
fd2c15ec | 354 | |
7a186941 OBC |
355 | /* it is now safe to add the virtio device */ |
356 | ret = rproc_add_virtio_dev(rvdev, rsc->id); | |
357 | if (ret) | |
cde42e07 | 358 | goto remove_rvdev; |
400e64df OBC |
359 | |
360 | return 0; | |
7a186941 | 361 | |
cde42e07 SB |
362 | remove_rvdev: |
363 | list_del(&rvdev->node); | |
6db20ea8 | 364 | free_rvdev: |
7a186941 OBC |
365 | kfree(rvdev); |
366 | return ret; | |
400e64df OBC |
367 | } |
368 | ||
369 | /** | |
370 | * rproc_handle_trace() - handle a shared trace buffer resource | |
371 | * @rproc: the remote processor | |
372 | * @rsc: the trace resource descriptor | |
fd2c15ec | 373 | * @avail: size of available data (for sanity checking the image) |
400e64df OBC |
374 | * |
375 | * In case the remote processor dumps trace logs into memory, | |
376 | * export it via debugfs. | |
377 | * | |
378 | * Currently, the 'da' member of @rsc should contain the device address | |
379 | * where the remote processor is dumping the traces. Later we could also | |
380 | * support dynamically allocating this address using the generic | |
381 | * DMA API (but currently there isn't a use case for that). | |
382 | * | |
383 | * Returns 0 on success, or an appropriate error code otherwise | |
384 | */ | |
fd2c15ec | 385 | static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc, |
a2b950ac | 386 | int offset, int avail) |
400e64df OBC |
387 | { |
388 | struct rproc_mem_entry *trace; | |
b5ab5e24 | 389 | struct device *dev = &rproc->dev; |
400e64df OBC |
390 | void *ptr; |
391 | char name[15]; | |
392 | ||
fd2c15ec | 393 | if (sizeof(*rsc) > avail) { |
b5ab5e24 | 394 | dev_err(dev, "trace rsc is truncated\n"); |
fd2c15ec OBC |
395 | return -EINVAL; |
396 | } | |
397 | ||
398 | /* make sure reserved bytes are zeroes */ | |
399 | if (rsc->reserved) { | |
400 | dev_err(dev, "trace rsc has non zero reserved bytes\n"); | |
401 | return -EINVAL; | |
402 | } | |
403 | ||
400e64df OBC |
404 | /* what's the kernel address of this resource ? */ |
405 | ptr = rproc_da_to_va(rproc, rsc->da, rsc->len); | |
406 | if (!ptr) { | |
407 | dev_err(dev, "erroneous trace resource entry\n"); | |
408 | return -EINVAL; | |
409 | } | |
410 | ||
411 | trace = kzalloc(sizeof(*trace), GFP_KERNEL); | |
172e6ab1 | 412 | if (!trace) |
400e64df | 413 | return -ENOMEM; |
400e64df OBC |
414 | |
415 | /* set the trace buffer dma properties */ | |
416 | trace->len = rsc->len; | |
417 | trace->va = ptr; | |
418 | ||
419 | /* make sure snprintf always null terminates, even if truncating */ | |
420 | snprintf(name, sizeof(name), "trace%d", rproc->num_traces); | |
421 | ||
422 | /* create the debugfs entry */ | |
423 | trace->priv = rproc_create_trace_file(name, rproc, trace); | |
424 | if (!trace->priv) { | |
425 | trace->va = NULL; | |
426 | kfree(trace); | |
427 | return -EINVAL; | |
428 | } | |
429 | ||
430 | list_add_tail(&trace->node, &rproc->traces); | |
431 | ||
432 | rproc->num_traces++; | |
433 | ||
fd2c15ec | 434 | dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr, |
400e64df OBC |
435 | rsc->da, rsc->len); |
436 | ||
437 | return 0; | |
438 | } | |
439 | ||
440 | /** | |
441 | * rproc_handle_devmem() - handle devmem resource entry | |
442 | * @rproc: remote processor handle | |
443 | * @rsc: the devmem resource entry | |
fd2c15ec | 444 | * @avail: size of available data (for sanity checking the image) |
400e64df OBC |
445 | * |
446 | * Remote processors commonly need to access certain on-chip peripherals. | |
447 | * | |
448 | * Some of these remote processors access memory via an iommu device, | |
449 | * and might require us to configure their iommu before they can access | |
450 | * the on-chip peripherals they need. | |
451 | * | |
452 | * This resource entry is a request to map such a peripheral device. | |
453 | * | |
454 | * These devmem entries will contain the physical address of the device in | |
455 | * the 'pa' member. If a specific device address is expected, then 'da' will | |
456 | * contain it (currently this is the only use case supported). 'len' will | |
457 | * contain the size of the physical region we need to map. | |
458 | * | |
459 | * Currently we just "trust" those devmem entries to contain valid physical | |
460 | * addresses, but this is going to change: we want the implementations to | |
461 | * tell us ranges of physical addresses the firmware is allowed to request, | |
462 | * and not allow firmwares to request access to physical addresses that | |
463 | * are outside those ranges. | |
464 | */ | |
fd2c15ec | 465 | static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc, |
a2b950ac | 466 | int offset, int avail) |
400e64df OBC |
467 | { |
468 | struct rproc_mem_entry *mapping; | |
b5ab5e24 | 469 | struct device *dev = &rproc->dev; |
400e64df OBC |
470 | int ret; |
471 | ||
472 | /* no point in handling this resource without a valid iommu domain */ | |
473 | if (!rproc->domain) | |
474 | return -EINVAL; | |
475 | ||
fd2c15ec | 476 | if (sizeof(*rsc) > avail) { |
b5ab5e24 | 477 | dev_err(dev, "devmem rsc is truncated\n"); |
fd2c15ec OBC |
478 | return -EINVAL; |
479 | } | |
480 | ||
481 | /* make sure reserved bytes are zeroes */ | |
482 | if (rsc->reserved) { | |
b5ab5e24 | 483 | dev_err(dev, "devmem rsc has non zero reserved bytes\n"); |
fd2c15ec OBC |
484 | return -EINVAL; |
485 | } | |
486 | ||
400e64df | 487 | mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); |
172e6ab1 | 488 | if (!mapping) |
400e64df | 489 | return -ENOMEM; |
400e64df OBC |
490 | |
491 | ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); | |
492 | if (ret) { | |
b5ab5e24 | 493 | dev_err(dev, "failed to map devmem: %d\n", ret); |
400e64df OBC |
494 | goto out; |
495 | } | |
496 | ||
497 | /* | |
498 | * We'll need this info later when we'll want to unmap everything | |
499 | * (e.g. on shutdown). | |
500 | * | |
501 | * We can't trust the remote processor not to change the resource | |
502 | * table, so we must maintain this info independently. | |
503 | */ | |
504 | mapping->da = rsc->da; | |
505 | mapping->len = rsc->len; | |
506 | list_add_tail(&mapping->node, &rproc->mappings); | |
507 | ||
b5ab5e24 | 508 | dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", |
400e64df OBC |
509 | rsc->pa, rsc->da, rsc->len); |
510 | ||
511 | return 0; | |
512 | ||
513 | out: | |
514 | kfree(mapping); | |
515 | return ret; | |
516 | } | |
517 | ||
518 | /** | |
519 | * rproc_handle_carveout() - handle phys contig memory allocation requests | |
520 | * @rproc: rproc handle | |
521 | * @rsc: the resource entry | |
fd2c15ec | 522 | * @avail: size of available data (for image validation) |
400e64df OBC |
523 | * |
524 | * This function will handle firmware requests for allocation of physically | |
525 | * contiguous memory regions. | |
526 | * | |
527 | * These request entries should come first in the firmware's resource table, | |
528 | * as other firmware entries might request placing other data objects inside | |
529 | * these memory regions (e.g. data/code segments, trace resource entries, ...). | |
530 | * | |
531 | * Allocating memory this way helps utilizing the reserved physical memory | |
532 | * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries | |
533 | * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB | |
534 | * pressure is important; it may have a substantial impact on performance. | |
535 | */ | |
fd2c15ec | 536 | static int rproc_handle_carveout(struct rproc *rproc, |
a2b950ac OBC |
537 | struct fw_rsc_carveout *rsc, |
538 | int offset, int avail) | |
539 | ||
400e64df OBC |
540 | { |
541 | struct rproc_mem_entry *carveout, *mapping; | |
b5ab5e24 | 542 | struct device *dev = &rproc->dev; |
400e64df OBC |
543 | dma_addr_t dma; |
544 | void *va; | |
545 | int ret; | |
546 | ||
fd2c15ec | 547 | if (sizeof(*rsc) > avail) { |
b5ab5e24 | 548 | dev_err(dev, "carveout rsc is truncated\n"); |
fd2c15ec OBC |
549 | return -EINVAL; |
550 | } | |
551 | ||
552 | /* make sure reserved bytes are zeroes */ | |
553 | if (rsc->reserved) { | |
554 | dev_err(dev, "carveout rsc has non zero reserved bytes\n"); | |
555 | return -EINVAL; | |
556 | } | |
557 | ||
558 | dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n", | |
559 | rsc->da, rsc->pa, rsc->len, rsc->flags); | |
560 | ||
400e64df | 561 | carveout = kzalloc(sizeof(*carveout), GFP_KERNEL); |
172e6ab1 | 562 | if (!carveout) |
7168d914 | 563 | return -ENOMEM; |
400e64df | 564 | |
b5ab5e24 | 565 | va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL); |
400e64df | 566 | if (!va) { |
b5ab5e24 | 567 | dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len); |
400e64df OBC |
568 | ret = -ENOMEM; |
569 | goto free_carv; | |
570 | } | |
571 | ||
d09f53a7 EG |
572 | dev_dbg(dev, "carveout va %p, dma %llx, len 0x%x\n", va, |
573 | (unsigned long long)dma, rsc->len); | |
400e64df OBC |
574 | |
575 | /* | |
576 | * Ok, this is non-standard. | |
577 | * | |
578 | * Sometimes we can't rely on the generic iommu-based DMA API | |
579 | * to dynamically allocate the device address and then set the IOMMU | |
580 | * tables accordingly, because some remote processors might | |
581 | * _require_ us to use hard coded device addresses that their | |
582 | * firmware was compiled with. | |
583 | * | |
584 | * In this case, we must use the IOMMU API directly and map | |
585 | * the memory to the device address as expected by the remote | |
586 | * processor. | |
587 | * | |
588 | * Obviously such remote processor devices should not be configured | |
589 | * to use the iommu-based DMA API: we expect 'dma' to contain the | |
590 | * physical address in this case. | |
591 | */ | |
592 | if (rproc->domain) { | |
7168d914 DC |
593 | mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); |
594 | if (!mapping) { | |
595 | dev_err(dev, "kzalloc mapping failed\n"); | |
596 | ret = -ENOMEM; | |
597 | goto dma_free; | |
598 | } | |
599 | ||
400e64df OBC |
600 | ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len, |
601 | rsc->flags); | |
602 | if (ret) { | |
603 | dev_err(dev, "iommu_map failed: %d\n", ret); | |
7168d914 | 604 | goto free_mapping; |
400e64df OBC |
605 | } |
606 | ||
607 | /* | |
608 | * We'll need this info later when we'll want to unmap | |
609 | * everything (e.g. on shutdown). | |
610 | * | |
611 | * We can't trust the remote processor not to change the | |
612 | * resource table, so we must maintain this info independently. | |
613 | */ | |
614 | mapping->da = rsc->da; | |
615 | mapping->len = rsc->len; | |
616 | list_add_tail(&mapping->node, &rproc->mappings); | |
617 | ||
d09f53a7 EG |
618 | dev_dbg(dev, "carveout mapped 0x%x to 0x%llx\n", |
619 | rsc->da, (unsigned long long)dma); | |
400e64df OBC |
620 | } |
621 | ||
0e49b72c OBC |
622 | /* |
623 | * Some remote processors might need to know the pa | |
624 | * even though they are behind an IOMMU. E.g., OMAP4's | |
625 | * remote M3 processor needs this so it can control | |
626 | * on-chip hardware accelerators that are not behind | |
627 | * the IOMMU, and therefor must know the pa. | |
628 | * | |
629 | * Generally we don't want to expose physical addresses | |
630 | * if we don't have to (remote processors are generally | |
631 | * _not_ trusted), so we might want to do this only for | |
632 | * remote processor that _must_ have this (e.g. OMAP4's | |
633 | * dual M3 subsystem). | |
634 | * | |
635 | * Non-IOMMU processors might also want to have this info. | |
636 | * In this case, the device address and the physical address | |
637 | * are the same. | |
638 | */ | |
639 | rsc->pa = dma; | |
640 | ||
400e64df OBC |
641 | carveout->va = va; |
642 | carveout->len = rsc->len; | |
643 | carveout->dma = dma; | |
644 | carveout->da = rsc->da; | |
645 | ||
646 | list_add_tail(&carveout->node, &rproc->carveouts); | |
647 | ||
648 | return 0; | |
649 | ||
7168d914 DC |
650 | free_mapping: |
651 | kfree(mapping); | |
400e64df | 652 | dma_free: |
b5ab5e24 | 653 | dma_free_coherent(dev->parent, rsc->len, va, dma); |
400e64df OBC |
654 | free_carv: |
655 | kfree(carveout); | |
400e64df OBC |
656 | return ret; |
657 | } | |
658 | ||
ba7290e0 | 659 | static int rproc_count_vrings(struct rproc *rproc, struct fw_rsc_vdev *rsc, |
a2b950ac | 660 | int offset, int avail) |
ba7290e0 SB |
661 | { |
662 | /* Summarize the number of notification IDs */ | |
663 | rproc->max_notifyid += rsc->num_of_vrings; | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
e12bc14b OBC |
668 | /* |
669 | * A lookup table for resource handlers. The indices are defined in | |
670 | * enum fw_resource_type. | |
671 | */ | |
232fcdbb | 672 | static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { |
fd2c15ec OBC |
673 | [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout, |
674 | [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem, | |
675 | [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace, | |
7a186941 | 676 | [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */ |
e12bc14b OBC |
677 | }; |
678 | ||
232fcdbb SB |
679 | static rproc_handle_resource_t rproc_vdev_handler[RSC_LAST] = { |
680 | [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev, | |
681 | }; | |
682 | ||
ba7290e0 SB |
683 | static rproc_handle_resource_t rproc_count_vrings_handler[RSC_LAST] = { |
684 | [RSC_VDEV] = (rproc_handle_resource_t)rproc_count_vrings, | |
685 | }; | |
686 | ||
400e64df | 687 | /* handle firmware resource entries before booting the remote processor */ |
a2b950ac | 688 | static int rproc_handle_resources(struct rproc *rproc, int len, |
232fcdbb | 689 | rproc_handle_resource_t handlers[RSC_LAST]) |
400e64df | 690 | { |
b5ab5e24 | 691 | struct device *dev = &rproc->dev; |
e12bc14b | 692 | rproc_handle_resource_t handler; |
fd2c15ec OBC |
693 | int ret = 0, i; |
694 | ||
a2b950ac OBC |
695 | for (i = 0; i < rproc->table_ptr->num; i++) { |
696 | int offset = rproc->table_ptr->offset[i]; | |
697 | struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; | |
fd2c15ec OBC |
698 | int avail = len - offset - sizeof(*hdr); |
699 | void *rsc = (void *)hdr + sizeof(*hdr); | |
700 | ||
701 | /* make sure table isn't truncated */ | |
702 | if (avail < 0) { | |
703 | dev_err(dev, "rsc table is truncated\n"); | |
704 | return -EINVAL; | |
705 | } | |
400e64df | 706 | |
fd2c15ec | 707 | dev_dbg(dev, "rsc: type %d\n", hdr->type); |
400e64df | 708 | |
fd2c15ec OBC |
709 | if (hdr->type >= RSC_LAST) { |
710 | dev_warn(dev, "unsupported resource %d\n", hdr->type); | |
e12bc14b | 711 | continue; |
400e64df OBC |
712 | } |
713 | ||
232fcdbb | 714 | handler = handlers[hdr->type]; |
e12bc14b OBC |
715 | if (!handler) |
716 | continue; | |
717 | ||
a2b950ac | 718 | ret = handler(rproc, rsc, offset + sizeof(*hdr), avail); |
7a186941 | 719 | if (ret) |
400e64df | 720 | break; |
fd2c15ec | 721 | } |
400e64df OBC |
722 | |
723 | return ret; | |
724 | } | |
725 | ||
400e64df OBC |
726 | /** |
727 | * rproc_resource_cleanup() - clean up and free all acquired resources | |
728 | * @rproc: rproc handle | |
729 | * | |
730 | * This function will free all resources acquired for @rproc, and it | |
7a186941 | 731 | * is called whenever @rproc either shuts down or fails to boot. |
400e64df OBC |
732 | */ |
733 | static void rproc_resource_cleanup(struct rproc *rproc) | |
734 | { | |
735 | struct rproc_mem_entry *entry, *tmp; | |
b5ab5e24 | 736 | struct device *dev = &rproc->dev; |
400e64df OBC |
737 | |
738 | /* clean up debugfs trace entries */ | |
739 | list_for_each_entry_safe(entry, tmp, &rproc->traces, node) { | |
740 | rproc_remove_trace_file(entry->priv); | |
741 | rproc->num_traces--; | |
742 | list_del(&entry->node); | |
743 | kfree(entry); | |
744 | } | |
745 | ||
400e64df OBC |
746 | /* clean up iommu mapping entries */ |
747 | list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { | |
748 | size_t unmapped; | |
749 | ||
750 | unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); | |
751 | if (unmapped != entry->len) { | |
752 | /* nothing much to do besides complaining */ | |
e981f6d4 | 753 | dev_err(dev, "failed to unmap %u/%zu\n", entry->len, |
400e64df OBC |
754 | unmapped); |
755 | } | |
756 | ||
757 | list_del(&entry->node); | |
758 | kfree(entry); | |
759 | } | |
b6356a01 SA |
760 | |
761 | /* clean up carveout allocations */ | |
762 | list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { | |
172e6ab1 SA |
763 | dma_free_coherent(dev->parent, entry->len, entry->va, |
764 | entry->dma); | |
b6356a01 SA |
765 | list_del(&entry->node); |
766 | kfree(entry); | |
767 | } | |
400e64df OBC |
768 | } |
769 | ||
400e64df OBC |
770 | /* |
771 | * take a firmware and boot a remote processor with it. | |
772 | */ | |
773 | static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) | |
774 | { | |
b5ab5e24 | 775 | struct device *dev = &rproc->dev; |
400e64df | 776 | const char *name = rproc->firmware; |
a2b950ac | 777 | struct resource_table *table, *loaded_table; |
1e3e2c7c | 778 | int ret, tablesz; |
400e64df | 779 | |
a2b950ac OBC |
780 | if (!rproc->table_ptr) |
781 | return -ENOMEM; | |
782 | ||
400e64df OBC |
783 | ret = rproc_fw_sanity_check(rproc, fw); |
784 | if (ret) | |
785 | return ret; | |
786 | ||
e981f6d4 | 787 | dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); |
400e64df OBC |
788 | |
789 | /* | |
790 | * if enabling an IOMMU isn't relevant for this rproc, this is | |
791 | * just a nop | |
792 | */ | |
793 | ret = rproc_enable_iommu(rproc); | |
794 | if (ret) { | |
795 | dev_err(dev, "can't enable iommu: %d\n", ret); | |
796 | return ret; | |
797 | } | |
798 | ||
3e5f9eb5 | 799 | rproc->bootaddr = rproc_get_boot_addr(rproc, fw); |
89970d28 | 800 | ret = -EINVAL; |
400e64df | 801 | |
1e3e2c7c | 802 | /* look for the resource table */ |
bd484984 | 803 | table = rproc_find_rsc_table(rproc, fw, &tablesz); |
172e6ab1 | 804 | if (!table) |
1e3e2c7c OBC |
805 | goto clean_up; |
806 | ||
a2b950ac OBC |
807 | /* Verify that resource table in loaded fw is unchanged */ |
808 | if (rproc->table_csum != crc32(0, table, tablesz)) { | |
809 | dev_err(dev, "resource checksum failed, fw changed?\n"); | |
a2b950ac OBC |
810 | goto clean_up; |
811 | } | |
812 | ||
400e64df | 813 | /* handle fw resources which are required to boot rproc */ |
a2b950ac | 814 | ret = rproc_handle_resources(rproc, tablesz, rproc_loading_handlers); |
400e64df OBC |
815 | if (ret) { |
816 | dev_err(dev, "Failed to process resources: %d\n", ret); | |
817 | goto clean_up; | |
818 | } | |
819 | ||
820 | /* load the ELF segments to memory */ | |
bd484984 | 821 | ret = rproc_load_segments(rproc, fw); |
400e64df OBC |
822 | if (ret) { |
823 | dev_err(dev, "Failed to load program segments: %d\n", ret); | |
824 | goto clean_up; | |
825 | } | |
826 | ||
a2b950ac OBC |
827 | /* |
828 | * The starting device has been given the rproc->cached_table as the | |
829 | * resource table. The address of the vring along with the other | |
830 | * allocated resources (carveouts etc) is stored in cached_table. | |
831 | * In order to pass this information to the remote device we must | |
832 | * copy this information to device memory. | |
833 | */ | |
834 | loaded_table = rproc_find_loaded_rsc_table(rproc, fw); | |
89970d28 WY |
835 | if (!loaded_table) { |
836 | ret = -EINVAL; | |
a2b950ac | 837 | goto clean_up; |
89970d28 | 838 | } |
a2b950ac OBC |
839 | |
840 | memcpy(loaded_table, rproc->cached_table, tablesz); | |
841 | ||
400e64df OBC |
842 | /* power up the remote processor */ |
843 | ret = rproc->ops->start(rproc); | |
844 | if (ret) { | |
845 | dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); | |
846 | goto clean_up; | |
847 | } | |
848 | ||
a2b950ac OBC |
849 | /* |
850 | * Update table_ptr so that all subsequent vring allocations and | |
851 | * virtio fields manipulation update the actual loaded resource table | |
852 | * in device memory. | |
853 | */ | |
854 | rproc->table_ptr = loaded_table; | |
855 | ||
400e64df OBC |
856 | rproc->state = RPROC_RUNNING; |
857 | ||
858 | dev_info(dev, "remote processor %s is now up\n", rproc->name); | |
859 | ||
860 | return 0; | |
861 | ||
862 | clean_up: | |
863 | rproc_resource_cleanup(rproc); | |
864 | rproc_disable_iommu(rproc); | |
865 | return ret; | |
866 | } | |
867 | ||
868 | /* | |
869 | * take a firmware and look for virtio devices to register. | |
870 | * | |
871 | * Note: this function is called asynchronously upon registration of the | |
872 | * remote processor (so we must wait until it completes before we try | |
873 | * to unregister the device. one other option is just to use kref here, | |
874 | * that might be cleaner). | |
875 | */ | |
876 | static void rproc_fw_config_virtio(const struct firmware *fw, void *context) | |
877 | { | |
878 | struct rproc *rproc = context; | |
1e3e2c7c OBC |
879 | struct resource_table *table; |
880 | int ret, tablesz; | |
400e64df OBC |
881 | |
882 | if (rproc_fw_sanity_check(rproc, fw) < 0) | |
883 | goto out; | |
884 | ||
1e3e2c7c | 885 | /* look for the resource table */ |
bd484984 | 886 | table = rproc_find_rsc_table(rproc, fw, &tablesz); |
1e3e2c7c OBC |
887 | if (!table) |
888 | goto out; | |
889 | ||
a2b950ac OBC |
890 | rproc->table_csum = crc32(0, table, tablesz); |
891 | ||
892 | /* | |
893 | * Create a copy of the resource table. When a virtio device starts | |
894 | * and calls vring_new_virtqueue() the address of the allocated vring | |
895 | * will be stored in the cached_table. Before the device is started, | |
896 | * cached_table will be copied into devic memory. | |
897 | */ | |
95cee62c | 898 | rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL); |
a2b950ac OBC |
899 | if (!rproc->cached_table) |
900 | goto out; | |
901 | ||
a2b950ac OBC |
902 | rproc->table_ptr = rproc->cached_table; |
903 | ||
ba7290e0 SB |
904 | /* count the number of notify-ids */ |
905 | rproc->max_notifyid = -1; | |
172e6ab1 SA |
906 | ret = rproc_handle_resources(rproc, tablesz, |
907 | rproc_count_vrings_handler); | |
1e3e2c7c | 908 | if (ret) |
400e64df | 909 | goto out; |
400e64df | 910 | |
a2b950ac OBC |
911 | /* look for virtio devices and register them */ |
912 | ret = rproc_handle_resources(rproc, tablesz, rproc_vdev_handler); | |
913 | ||
400e64df | 914 | out: |
3cc6e787 | 915 | release_firmware(fw); |
160e7c84 | 916 | /* allow rproc_del() contexts, if any, to proceed */ |
400e64df OBC |
917 | complete_all(&rproc->firmware_loading_complete); |
918 | } | |
919 | ||
70b85ef8 FGL |
920 | static int rproc_add_virtio_devices(struct rproc *rproc) |
921 | { | |
922 | int ret; | |
923 | ||
924 | /* rproc_del() calls must wait until async loader completes */ | |
925 | init_completion(&rproc->firmware_loading_complete); | |
926 | ||
927 | /* | |
928 | * We must retrieve early virtio configuration info from | |
929 | * the firmware (e.g. whether to register a virtio device, | |
930 | * what virtio features does it support, ...). | |
931 | * | |
932 | * We're initiating an asynchronous firmware loading, so we can | |
933 | * be built-in kernel code, without hanging the boot process. | |
934 | */ | |
935 | ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, | |
936 | rproc->firmware, &rproc->dev, GFP_KERNEL, | |
937 | rproc, rproc_fw_config_virtio); | |
938 | if (ret < 0) { | |
939 | dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); | |
940 | complete_all(&rproc->firmware_loading_complete); | |
941 | } | |
942 | ||
943 | return ret; | |
944 | } | |
945 | ||
946 | /** | |
947 | * rproc_trigger_recovery() - recover a remoteproc | |
948 | * @rproc: the remote processor | |
949 | * | |
950 | * The recovery is done by reseting all the virtio devices, that way all the | |
951 | * rpmsg drivers will be reseted along with the remote processor making the | |
952 | * remoteproc functional again. | |
953 | * | |
954 | * This function can sleep, so it cannot be called from atomic context. | |
955 | */ | |
956 | int rproc_trigger_recovery(struct rproc *rproc) | |
957 | { | |
958 | struct rproc_vdev *rvdev, *rvtmp; | |
959 | ||
960 | dev_err(&rproc->dev, "recovering %s\n", rproc->name); | |
961 | ||
962 | init_completion(&rproc->crash_comp); | |
963 | ||
964 | /* clean up remote vdev entries */ | |
965 | list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) | |
966 | rproc_remove_virtio_dev(rvdev); | |
967 | ||
968 | /* wait until there is no more rproc users */ | |
969 | wait_for_completion(&rproc->crash_comp); | |
970 | ||
a2b950ac OBC |
971 | /* Free the copy of the resource table */ |
972 | kfree(rproc->cached_table); | |
973 | ||
70b85ef8 FGL |
974 | return rproc_add_virtio_devices(rproc); |
975 | } | |
976 | ||
8afd519c FGL |
977 | /** |
978 | * rproc_crash_handler_work() - handle a crash | |
979 | * | |
980 | * This function needs to handle everything related to a crash, like cpu | |
981 | * registers and stack dump, information to help to debug the fatal error, etc. | |
982 | */ | |
983 | static void rproc_crash_handler_work(struct work_struct *work) | |
984 | { | |
985 | struct rproc *rproc = container_of(work, struct rproc, crash_handler); | |
986 | struct device *dev = &rproc->dev; | |
987 | ||
988 | dev_dbg(dev, "enter %s\n", __func__); | |
989 | ||
990 | mutex_lock(&rproc->lock); | |
991 | ||
992 | if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) { | |
993 | /* handle only the first crash detected */ | |
994 | mutex_unlock(&rproc->lock); | |
995 | return; | |
996 | } | |
997 | ||
998 | rproc->state = RPROC_CRASHED; | |
999 | dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, | |
1000 | rproc->name); | |
1001 | ||
1002 | mutex_unlock(&rproc->lock); | |
1003 | ||
2e37abb8 FGL |
1004 | if (!rproc->recovery_disabled) |
1005 | rproc_trigger_recovery(rproc); | |
8afd519c FGL |
1006 | } |
1007 | ||
400e64df OBC |
1008 | /** |
1009 | * rproc_boot() - boot a remote processor | |
1010 | * @rproc: handle of a remote processor | |
1011 | * | |
1012 | * Boot a remote processor (i.e. load its firmware, power it on, ...). | |
1013 | * | |
1014 | * If the remote processor is already powered on, this function immediately | |
1015 | * returns (successfully). | |
1016 | * | |
1017 | * Returns 0 on success, and an appropriate error value otherwise. | |
1018 | */ | |
1019 | int rproc_boot(struct rproc *rproc) | |
1020 | { | |
1021 | const struct firmware *firmware_p; | |
1022 | struct device *dev; | |
1023 | int ret; | |
1024 | ||
1025 | if (!rproc) { | |
1026 | pr_err("invalid rproc handle\n"); | |
1027 | return -EINVAL; | |
1028 | } | |
1029 | ||
b5ab5e24 | 1030 | dev = &rproc->dev; |
400e64df OBC |
1031 | |
1032 | ret = mutex_lock_interruptible(&rproc->lock); | |
1033 | if (ret) { | |
1034 | dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); | |
1035 | return ret; | |
1036 | } | |
1037 | ||
1038 | /* loading a firmware is required */ | |
1039 | if (!rproc->firmware) { | |
1040 | dev_err(dev, "%s: no firmware to load\n", __func__); | |
1041 | ret = -EINVAL; | |
1042 | goto unlock_mutex; | |
1043 | } | |
1044 | ||
1045 | /* prevent underlying implementation from being removed */ | |
b5ab5e24 | 1046 | if (!try_module_get(dev->parent->driver->owner)) { |
400e64df OBC |
1047 | dev_err(dev, "%s: can't get owner\n", __func__); |
1048 | ret = -EINVAL; | |
1049 | goto unlock_mutex; | |
1050 | } | |
1051 | ||
1052 | /* skip the boot process if rproc is already powered up */ | |
1053 | if (atomic_inc_return(&rproc->power) > 1) { | |
1054 | ret = 0; | |
1055 | goto unlock_mutex; | |
1056 | } | |
1057 | ||
1058 | dev_info(dev, "powering up %s\n", rproc->name); | |
1059 | ||
1060 | /* load firmware */ | |
1061 | ret = request_firmware(&firmware_p, rproc->firmware, dev); | |
1062 | if (ret < 0) { | |
1063 | dev_err(dev, "request_firmware failed: %d\n", ret); | |
1064 | goto downref_rproc; | |
1065 | } | |
1066 | ||
1067 | ret = rproc_fw_boot(rproc, firmware_p); | |
1068 | ||
1069 | release_firmware(firmware_p); | |
1070 | ||
1071 | downref_rproc: | |
1072 | if (ret) { | |
b5ab5e24 | 1073 | module_put(dev->parent->driver->owner); |
400e64df OBC |
1074 | atomic_dec(&rproc->power); |
1075 | } | |
1076 | unlock_mutex: | |
1077 | mutex_unlock(&rproc->lock); | |
1078 | return ret; | |
1079 | } | |
1080 | EXPORT_SYMBOL(rproc_boot); | |
1081 | ||
1082 | /** | |
1083 | * rproc_shutdown() - power off the remote processor | |
1084 | * @rproc: the remote processor | |
1085 | * | |
1086 | * Power off a remote processor (previously booted with rproc_boot()). | |
1087 | * | |
1088 | * In case @rproc is still being used by an additional user(s), then | |
1089 | * this function will just decrement the power refcount and exit, | |
1090 | * without really powering off the device. | |
1091 | * | |
1092 | * Every call to rproc_boot() must (eventually) be accompanied by a call | |
1093 | * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. | |
1094 | * | |
1095 | * Notes: | |
1096 | * - we're not decrementing the rproc's refcount, only the power refcount. | |
1097 | * which means that the @rproc handle stays valid even after rproc_shutdown() | |
1098 | * returns, and users can still use it with a subsequent rproc_boot(), if | |
1099 | * needed. | |
400e64df OBC |
1100 | */ |
1101 | void rproc_shutdown(struct rproc *rproc) | |
1102 | { | |
b5ab5e24 | 1103 | struct device *dev = &rproc->dev; |
400e64df OBC |
1104 | int ret; |
1105 | ||
1106 | ret = mutex_lock_interruptible(&rproc->lock); | |
1107 | if (ret) { | |
1108 | dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); | |
1109 | return; | |
1110 | } | |
1111 | ||
1112 | /* if the remote proc is still needed, bail out */ | |
1113 | if (!atomic_dec_and_test(&rproc->power)) | |
1114 | goto out; | |
1115 | ||
1116 | /* power off the remote processor */ | |
1117 | ret = rproc->ops->stop(rproc); | |
1118 | if (ret) { | |
1119 | atomic_inc(&rproc->power); | |
1120 | dev_err(dev, "can't stop rproc: %d\n", ret); | |
1121 | goto out; | |
1122 | } | |
1123 | ||
1124 | /* clean up all acquired resources */ | |
1125 | rproc_resource_cleanup(rproc); | |
1126 | ||
1127 | rproc_disable_iommu(rproc); | |
1128 | ||
a2b950ac OBC |
1129 | /* Give the next start a clean resource table */ |
1130 | rproc->table_ptr = rproc->cached_table; | |
1131 | ||
70b85ef8 FGL |
1132 | /* if in crash state, unlock crash handler */ |
1133 | if (rproc->state == RPROC_CRASHED) | |
1134 | complete_all(&rproc->crash_comp); | |
1135 | ||
400e64df OBC |
1136 | rproc->state = RPROC_OFFLINE; |
1137 | ||
1138 | dev_info(dev, "stopped remote processor %s\n", rproc->name); | |
1139 | ||
1140 | out: | |
1141 | mutex_unlock(&rproc->lock); | |
1142 | if (!ret) | |
b5ab5e24 | 1143 | module_put(dev->parent->driver->owner); |
400e64df OBC |
1144 | } |
1145 | EXPORT_SYMBOL(rproc_shutdown); | |
1146 | ||
1147 | /** | |
160e7c84 | 1148 | * rproc_add() - register a remote processor |
400e64df OBC |
1149 | * @rproc: the remote processor handle to register |
1150 | * | |
1151 | * Registers @rproc with the remoteproc framework, after it has been | |
1152 | * allocated with rproc_alloc(). | |
1153 | * | |
1154 | * This is called by the platform-specific rproc implementation, whenever | |
1155 | * a new remote processor device is probed. | |
1156 | * | |
1157 | * Returns 0 on success and an appropriate error code otherwise. | |
1158 | * | |
1159 | * Note: this function initiates an asynchronous firmware loading | |
1160 | * context, which will look for virtio devices supported by the rproc's | |
1161 | * firmware. | |
1162 | * | |
1163 | * If found, those virtio devices will be created and added, so as a result | |
7a186941 | 1164 | * of registering this remote processor, additional virtio drivers might be |
400e64df | 1165 | * probed. |
400e64df | 1166 | */ |
160e7c84 | 1167 | int rproc_add(struct rproc *rproc) |
400e64df | 1168 | { |
b5ab5e24 | 1169 | struct device *dev = &rproc->dev; |
70b85ef8 | 1170 | int ret; |
400e64df | 1171 | |
b5ab5e24 OBC |
1172 | ret = device_add(dev); |
1173 | if (ret < 0) | |
1174 | return ret; | |
400e64df | 1175 | |
b5ab5e24 | 1176 | dev_info(dev, "%s is available\n", rproc->name); |
400e64df | 1177 | |
489d129a OBC |
1178 | dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n"); |
1179 | dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n"); | |
1180 | ||
400e64df OBC |
1181 | /* create debugfs entries */ |
1182 | rproc_create_debug_dir(rproc); | |
1183 | ||
70b85ef8 | 1184 | return rproc_add_virtio_devices(rproc); |
400e64df | 1185 | } |
160e7c84 | 1186 | EXPORT_SYMBOL(rproc_add); |
400e64df | 1187 | |
b5ab5e24 OBC |
1188 | /** |
1189 | * rproc_type_release() - release a remote processor instance | |
1190 | * @dev: the rproc's device | |
1191 | * | |
1192 | * This function should _never_ be called directly. | |
1193 | * | |
1194 | * It will be called by the driver core when no one holds a valid pointer | |
1195 | * to @dev anymore. | |
1196 | */ | |
1197 | static void rproc_type_release(struct device *dev) | |
1198 | { | |
1199 | struct rproc *rproc = container_of(dev, struct rproc, dev); | |
1200 | ||
7183a2a7 OBC |
1201 | dev_info(&rproc->dev, "releasing %s\n", rproc->name); |
1202 | ||
1203 | rproc_delete_debug_dir(rproc); | |
1204 | ||
b5ab5e24 OBC |
1205 | idr_destroy(&rproc->notifyids); |
1206 | ||
1207 | if (rproc->index >= 0) | |
1208 | ida_simple_remove(&rproc_dev_index, rproc->index); | |
1209 | ||
1210 | kfree(rproc); | |
1211 | } | |
1212 | ||
1213 | static struct device_type rproc_type = { | |
1214 | .name = "remoteproc", | |
1215 | .release = rproc_type_release, | |
1216 | }; | |
400e64df OBC |
1217 | |
1218 | /** | |
1219 | * rproc_alloc() - allocate a remote processor handle | |
1220 | * @dev: the underlying device | |
1221 | * @name: name of this remote processor | |
1222 | * @ops: platform-specific handlers (mainly start/stop) | |
8b4aec9a | 1223 | * @firmware: name of firmware file to load, can be NULL |
400e64df OBC |
1224 | * @len: length of private data needed by the rproc driver (in bytes) |
1225 | * | |
1226 | * Allocates a new remote processor handle, but does not register | |
8b4aec9a | 1227 | * it yet. if @firmware is NULL, a default name is used. |
400e64df OBC |
1228 | * |
1229 | * This function should be used by rproc implementations during initialization | |
1230 | * of the remote processor. | |
1231 | * | |
1232 | * After creating an rproc handle using this function, and when ready, | |
160e7c84 | 1233 | * implementations should then call rproc_add() to complete |
400e64df OBC |
1234 | * the registration of the remote processor. |
1235 | * | |
1236 | * On success the new rproc is returned, and on failure, NULL. | |
1237 | * | |
1238 | * Note: _never_ directly deallocate @rproc, even if it was not registered | |
160e7c84 | 1239 | * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put(). |
400e64df OBC |
1240 | */ |
1241 | struct rproc *rproc_alloc(struct device *dev, const char *name, | |
1242 | const struct rproc_ops *ops, | |
1243 | const char *firmware, int len) | |
1244 | { | |
1245 | struct rproc *rproc; | |
8b4aec9a RT |
1246 | char *p, *template = "rproc-%s-fw"; |
1247 | int name_len = 0; | |
400e64df OBC |
1248 | |
1249 | if (!dev || !name || !ops) | |
1250 | return NULL; | |
1251 | ||
8b4aec9a RT |
1252 | if (!firmware) |
1253 | /* | |
1254 | * Make room for default firmware name (minus %s plus '\0'). | |
1255 | * If the caller didn't pass in a firmware name then | |
1256 | * construct a default name. We're already glomming 'len' | |
1257 | * bytes onto the end of the struct rproc allocation, so do | |
1258 | * a few more for the default firmware name (but only if | |
1259 | * the caller doesn't pass one). | |
1260 | */ | |
1261 | name_len = strlen(name) + strlen(template) - 2 + 1; | |
1262 | ||
1263 | rproc = kzalloc(sizeof(struct rproc) + len + name_len, GFP_KERNEL); | |
172e6ab1 | 1264 | if (!rproc) |
400e64df | 1265 | return NULL; |
400e64df | 1266 | |
8b4aec9a RT |
1267 | if (!firmware) { |
1268 | p = (char *)rproc + sizeof(struct rproc) + len; | |
1269 | snprintf(p, name_len, template, name); | |
1270 | } else { | |
1271 | p = (char *)firmware; | |
1272 | } | |
1273 | ||
1274 | rproc->firmware = p; | |
400e64df OBC |
1275 | rproc->name = name; |
1276 | rproc->ops = ops; | |
400e64df OBC |
1277 | rproc->priv = &rproc[1]; |
1278 | ||
b5ab5e24 OBC |
1279 | device_initialize(&rproc->dev); |
1280 | rproc->dev.parent = dev; | |
1281 | rproc->dev.type = &rproc_type; | |
1282 | ||
1283 | /* Assign a unique device index and name */ | |
1284 | rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL); | |
1285 | if (rproc->index < 0) { | |
1286 | dev_err(dev, "ida_simple_get failed: %d\n", rproc->index); | |
1287 | put_device(&rproc->dev); | |
1288 | return NULL; | |
1289 | } | |
1290 | ||
1291 | dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); | |
1292 | ||
400e64df OBC |
1293 | atomic_set(&rproc->power, 0); |
1294 | ||
4afc89d6 SB |
1295 | /* Set ELF as the default fw_ops handler */ |
1296 | rproc->fw_ops = &rproc_elf_fw_ops; | |
400e64df OBC |
1297 | |
1298 | mutex_init(&rproc->lock); | |
1299 | ||
7a186941 OBC |
1300 | idr_init(&rproc->notifyids); |
1301 | ||
400e64df OBC |
1302 | INIT_LIST_HEAD(&rproc->carveouts); |
1303 | INIT_LIST_HEAD(&rproc->mappings); | |
1304 | INIT_LIST_HEAD(&rproc->traces); | |
7a186941 | 1305 | INIT_LIST_HEAD(&rproc->rvdevs); |
400e64df | 1306 | |
8afd519c | 1307 | INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); |
70b85ef8 | 1308 | init_completion(&rproc->crash_comp); |
8afd519c | 1309 | |
400e64df OBC |
1310 | rproc->state = RPROC_OFFLINE; |
1311 | ||
1312 | return rproc; | |
1313 | } | |
1314 | EXPORT_SYMBOL(rproc_alloc); | |
1315 | ||
1316 | /** | |
160e7c84 | 1317 | * rproc_put() - unroll rproc_alloc() |
400e64df OBC |
1318 | * @rproc: the remote processor handle |
1319 | * | |
c6b5a276 | 1320 | * This function decrements the rproc dev refcount. |
400e64df | 1321 | * |
c6b5a276 OBC |
1322 | * If no one holds any reference to rproc anymore, then its refcount would |
1323 | * now drop to zero, and it would be freed. | |
400e64df | 1324 | */ |
160e7c84 | 1325 | void rproc_put(struct rproc *rproc) |
400e64df | 1326 | { |
b5ab5e24 | 1327 | put_device(&rproc->dev); |
400e64df | 1328 | } |
160e7c84 | 1329 | EXPORT_SYMBOL(rproc_put); |
400e64df OBC |
1330 | |
1331 | /** | |
160e7c84 | 1332 | * rproc_del() - unregister a remote processor |
400e64df OBC |
1333 | * @rproc: rproc handle to unregister |
1334 | * | |
400e64df OBC |
1335 | * This function should be called when the platform specific rproc |
1336 | * implementation decides to remove the rproc device. it should | |
160e7c84 | 1337 | * _only_ be called if a previous invocation of rproc_add() |
400e64df OBC |
1338 | * has completed successfully. |
1339 | * | |
160e7c84 | 1340 | * After rproc_del() returns, @rproc isn't freed yet, because |
c6b5a276 | 1341 | * of the outstanding reference created by rproc_alloc. To decrement that |
160e7c84 | 1342 | * one last refcount, one still needs to call rproc_put(). |
400e64df OBC |
1343 | * |
1344 | * Returns 0 on success and -EINVAL if @rproc isn't valid. | |
1345 | */ | |
160e7c84 | 1346 | int rproc_del(struct rproc *rproc) |
400e64df | 1347 | { |
6db20ea8 | 1348 | struct rproc_vdev *rvdev, *tmp; |
7a186941 | 1349 | |
400e64df OBC |
1350 | if (!rproc) |
1351 | return -EINVAL; | |
1352 | ||
1353 | /* if rproc is just being registered, wait */ | |
1354 | wait_for_completion(&rproc->firmware_loading_complete); | |
1355 | ||
7a186941 | 1356 | /* clean up remote vdev entries */ |
6db20ea8 | 1357 | list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node) |
7a186941 | 1358 | rproc_remove_virtio_dev(rvdev); |
400e64df | 1359 | |
a2b950ac OBC |
1360 | /* Free the copy of the resource table */ |
1361 | kfree(rproc->cached_table); | |
1362 | ||
b5ab5e24 | 1363 | device_del(&rproc->dev); |
400e64df OBC |
1364 | |
1365 | return 0; | |
1366 | } | |
160e7c84 | 1367 | EXPORT_SYMBOL(rproc_del); |
400e64df | 1368 | |
8afd519c FGL |
1369 | /** |
1370 | * rproc_report_crash() - rproc crash reporter function | |
1371 | * @rproc: remote processor | |
1372 | * @type: crash type | |
1373 | * | |
1374 | * This function must be called every time a crash is detected by the low-level | |
1375 | * drivers implementing a specific remoteproc. This should not be called from a | |
1376 | * non-remoteproc driver. | |
1377 | * | |
1378 | * This function can be called from atomic/interrupt context. | |
1379 | */ | |
1380 | void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) | |
1381 | { | |
1382 | if (!rproc) { | |
1383 | pr_err("NULL rproc pointer\n"); | |
1384 | return; | |
1385 | } | |
1386 | ||
1387 | dev_err(&rproc->dev, "crash detected in %s: type %s\n", | |
1388 | rproc->name, rproc_crash_to_string(type)); | |
1389 | ||
1390 | /* create a new task to handle the error */ | |
1391 | schedule_work(&rproc->crash_handler); | |
1392 | } | |
1393 | EXPORT_SYMBOL(rproc_report_crash); | |
1394 | ||
400e64df OBC |
1395 | static int __init remoteproc_init(void) |
1396 | { | |
1397 | rproc_init_debugfs(); | |
b5ab5e24 | 1398 | |
400e64df OBC |
1399 | return 0; |
1400 | } | |
1401 | module_init(remoteproc_init); | |
1402 | ||
1403 | static void __exit remoteproc_exit(void) | |
1404 | { | |
1405 | rproc_exit_debugfs(); | |
1406 | } | |
1407 | module_exit(remoteproc_exit); | |
1408 | ||
1409 | MODULE_LICENSE("GPL v2"); | |
1410 | MODULE_DESCRIPTION("Generic Remote Processor Framework"); |