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> | |
38 | #include <linux/klist.h> | |
39 | #include <linux/elf.h> | |
40 | #include <linux/virtio_ids.h> | |
41 | #include <linux/virtio_ring.h> | |
cf59d3e9 | 42 | #include <asm/byteorder.h> |
400e64df OBC |
43 | |
44 | #include "remoteproc_internal.h" | |
45 | ||
46 | static void klist_rproc_get(struct klist_node *n); | |
47 | static void klist_rproc_put(struct klist_node *n); | |
48 | ||
49 | /* | |
50 | * klist of the available remote processors. | |
51 | * | |
52 | * We need this in order to support name-based lookups (needed by the | |
53 | * rproc_get_by_name()). | |
54 | * | |
7a186941 OBC |
55 | * That said, we don't use rproc_get_by_name() at this point. |
56 | * The use cases that do require its existence should be | |
400e64df OBC |
57 | * scrutinized, and hopefully migrated to rproc_boot() using device-based |
58 | * binding. | |
59 | * | |
60 | * If/when this materializes, we could drop the klist (and the by_name | |
61 | * API). | |
62 | */ | |
63 | static DEFINE_KLIST(rprocs, klist_rproc_get, klist_rproc_put); | |
64 | ||
65 | typedef int (*rproc_handle_resources_t)(struct rproc *rproc, | |
fd2c15ec OBC |
66 | struct resource_table *table, int len); |
67 | typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail); | |
400e64df OBC |
68 | |
69 | /* | |
70 | * This is the IOMMU fault handler we register with the IOMMU API | |
71 | * (when relevant; not all remote processors access memory through | |
72 | * an IOMMU). | |
73 | * | |
74 | * IOMMU core will invoke this handler whenever the remote processor | |
75 | * will try to access an unmapped device address. | |
76 | * | |
77 | * Currently this is mostly a stub, but it will be later used to trigger | |
78 | * the recovery of the remote processor. | |
79 | */ | |
80 | static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, | |
81 | unsigned long iova, int flags) | |
82 | { | |
83 | dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags); | |
84 | ||
85 | /* | |
86 | * Let the iommu core know we're not really handling this fault; | |
87 | * we just plan to use this as a recovery trigger. | |
88 | */ | |
89 | return -ENOSYS; | |
90 | } | |
91 | ||
92 | static int rproc_enable_iommu(struct rproc *rproc) | |
93 | { | |
94 | struct iommu_domain *domain; | |
95 | struct device *dev = rproc->dev; | |
96 | int ret; | |
97 | ||
98 | /* | |
99 | * We currently use iommu_present() to decide if an IOMMU | |
100 | * setup is needed. | |
101 | * | |
102 | * This works for simple cases, but will easily fail with | |
103 | * platforms that do have an IOMMU, but not for this specific | |
104 | * rproc. | |
105 | * | |
106 | * This will be easily solved by introducing hw capabilities | |
107 | * that will be set by the remoteproc driver. | |
108 | */ | |
109 | if (!iommu_present(dev->bus)) { | |
0798e1da MG |
110 | dev_dbg(dev, "iommu not found\n"); |
111 | return 0; | |
400e64df OBC |
112 | } |
113 | ||
114 | domain = iommu_domain_alloc(dev->bus); | |
115 | if (!domain) { | |
116 | dev_err(dev, "can't alloc iommu domain\n"); | |
117 | return -ENOMEM; | |
118 | } | |
119 | ||
120 | iommu_set_fault_handler(domain, rproc_iommu_fault); | |
121 | ||
122 | ret = iommu_attach_device(domain, dev); | |
123 | if (ret) { | |
124 | dev_err(dev, "can't attach iommu device: %d\n", ret); | |
125 | goto free_domain; | |
126 | } | |
127 | ||
128 | rproc->domain = domain; | |
129 | ||
130 | return 0; | |
131 | ||
132 | free_domain: | |
133 | iommu_domain_free(domain); | |
134 | return ret; | |
135 | } | |
136 | ||
137 | static void rproc_disable_iommu(struct rproc *rproc) | |
138 | { | |
139 | struct iommu_domain *domain = rproc->domain; | |
140 | struct device *dev = rproc->dev; | |
141 | ||
142 | if (!domain) | |
143 | return; | |
144 | ||
145 | iommu_detach_device(domain, dev); | |
146 | iommu_domain_free(domain); | |
147 | ||
148 | return; | |
149 | } | |
150 | ||
151 | /* | |
152 | * Some remote processors will ask us to allocate them physically contiguous | |
153 | * memory regions (which we call "carveouts"), and map them to specific | |
154 | * device addresses (which are hardcoded in the firmware). | |
155 | * | |
156 | * They may then ask us to copy objects into specific device addresses (e.g. | |
157 | * code/data sections) or expose us certain symbols in other device address | |
158 | * (e.g. their trace buffer). | |
159 | * | |
160 | * This function is an internal helper with which we can go over the allocated | |
161 | * carveouts and translate specific device address to kernel virtual addresses | |
162 | * so we can access the referenced memory. | |
163 | * | |
164 | * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, | |
165 | * but only on kernel direct mapped RAM memory. Instead, we're just using | |
166 | * here the output of the DMA API, which should be more correct. | |
167 | */ | |
168 | static void *rproc_da_to_va(struct rproc *rproc, u64 da, int len) | |
169 | { | |
170 | struct rproc_mem_entry *carveout; | |
171 | void *ptr = NULL; | |
172 | ||
173 | list_for_each_entry(carveout, &rproc->carveouts, node) { | |
174 | int offset = da - carveout->da; | |
175 | ||
176 | /* try next carveout if da is too small */ | |
177 | if (offset < 0) | |
178 | continue; | |
179 | ||
180 | /* try next carveout if da is too large */ | |
181 | if (offset + len > carveout->len) | |
182 | continue; | |
183 | ||
184 | ptr = carveout->va + offset; | |
185 | ||
186 | break; | |
187 | } | |
188 | ||
189 | return ptr; | |
190 | } | |
191 | ||
192 | /** | |
193 | * rproc_load_segments() - load firmware segments to memory | |
194 | * @rproc: remote processor which will be booted using these fw segments | |
195 | * @elf_data: the content of the ELF firmware image | |
9bc91231 | 196 | * @len: firmware size (in bytes) |
400e64df OBC |
197 | * |
198 | * This function loads the firmware segments to memory, where the remote | |
199 | * processor expects them. | |
200 | * | |
201 | * Some remote processors will expect their code and data to be placed | |
202 | * in specific device addresses, and can't have them dynamically assigned. | |
203 | * | |
204 | * We currently support only those kind of remote processors, and expect | |
205 | * the program header's paddr member to contain those addresses. We then go | |
206 | * through the physically contiguous "carveout" memory regions which we | |
207 | * allocated (and mapped) earlier on behalf of the remote processor, | |
208 | * and "translate" device address to kernel addresses, so we can copy the | |
209 | * segments where they are expected. | |
210 | * | |
211 | * Currently we only support remote processors that required carveout | |
212 | * allocations and got them mapped onto their iommus. Some processors | |
213 | * might be different: they might not have iommus, and would prefer to | |
214 | * directly allocate memory for every segment/resource. This is not yet | |
215 | * supported, though. | |
216 | */ | |
9bc91231 OBC |
217 | static int |
218 | rproc_load_segments(struct rproc *rproc, const u8 *elf_data, size_t len) | |
400e64df OBC |
219 | { |
220 | struct device *dev = rproc->dev; | |
221 | struct elf32_hdr *ehdr; | |
222 | struct elf32_phdr *phdr; | |
223 | int i, ret = 0; | |
224 | ||
225 | ehdr = (struct elf32_hdr *)elf_data; | |
226 | phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff); | |
227 | ||
228 | /* go through the available ELF segments */ | |
229 | for (i = 0; i < ehdr->e_phnum; i++, phdr++) { | |
230 | u32 da = phdr->p_paddr; | |
231 | u32 memsz = phdr->p_memsz; | |
232 | u32 filesz = phdr->p_filesz; | |
9bc91231 | 233 | u32 offset = phdr->p_offset; |
400e64df OBC |
234 | void *ptr; |
235 | ||
236 | if (phdr->p_type != PT_LOAD) | |
237 | continue; | |
238 | ||
239 | dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n", | |
240 | phdr->p_type, da, memsz, filesz); | |
241 | ||
242 | if (filesz > memsz) { | |
243 | dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n", | |
244 | filesz, memsz); | |
245 | ret = -EINVAL; | |
246 | break; | |
247 | } | |
248 | ||
9bc91231 OBC |
249 | if (offset + filesz > len) { |
250 | dev_err(dev, "truncated fw: need 0x%x avail 0x%x\n", | |
251 | offset + filesz, len); | |
252 | ret = -EINVAL; | |
253 | break; | |
254 | } | |
255 | ||
400e64df OBC |
256 | /* grab the kernel address for this device address */ |
257 | ptr = rproc_da_to_va(rproc, da, memsz); | |
258 | if (!ptr) { | |
259 | dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz); | |
260 | ret = -EINVAL; | |
261 | break; | |
262 | } | |
263 | ||
264 | /* put the segment where the remote processor expects it */ | |
265 | if (phdr->p_filesz) | |
266 | memcpy(ptr, elf_data + phdr->p_offset, filesz); | |
267 | ||
268 | /* | |
269 | * Zero out remaining memory for this segment. | |
270 | * | |
271 | * This isn't strictly required since dma_alloc_coherent already | |
272 | * did this for us. albeit harmless, we may consider removing | |
273 | * this. | |
274 | */ | |
275 | if (memsz > filesz) | |
276 | memset(ptr + filesz, 0, memsz - filesz); | |
277 | } | |
278 | ||
279 | return ret; | |
280 | } | |
281 | ||
7a186941 OBC |
282 | static int |
283 | __rproc_handle_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) | |
400e64df | 284 | { |
7a186941 OBC |
285 | struct rproc *rproc = rvdev->rproc; |
286 | struct device *dev = rproc->dev; | |
287 | struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; | |
288 | dma_addr_t dma; | |
289 | void *va; | |
290 | int ret, size, notifyid; | |
400e64df | 291 | |
7a186941 OBC |
292 | dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n", |
293 | i, vring->da, vring->num, vring->align); | |
294 | ||
295 | /* make sure reserved bytes are zeroes */ | |
296 | if (vring->reserved) { | |
297 | dev_err(dev, "vring rsc has non zero reserved bytes\n"); | |
fd2c15ec OBC |
298 | return -EINVAL; |
299 | } | |
300 | ||
7a186941 OBC |
301 | /* the firmware must provide the expected queue size */ |
302 | if (!vring->num) { | |
303 | dev_err(dev, "invalid qsz (%d)\n", vring->num); | |
400e64df OBC |
304 | return -EINVAL; |
305 | } | |
306 | ||
7a186941 OBC |
307 | /* actual size of vring (in bytes) */ |
308 | size = PAGE_ALIGN(vring_size(vring->num, AMP_VRING_ALIGN)); | |
309 | ||
310 | if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) { | |
311 | dev_err(dev, "idr_pre_get failed\n"); | |
312 | return -ENOMEM; | |
313 | } | |
314 | ||
315 | /* | |
316 | * Allocate non-cacheable memory for the vring. In the future | |
317 | * this call will also configure the IOMMU for us | |
318 | */ | |
319 | va = dma_alloc_coherent(dev, size, &dma, GFP_KERNEL); | |
320 | if (!va) { | |
321 | dev_err(dev, "dma_alloc_coherent failed\n"); | |
400e64df OBC |
322 | return -EINVAL; |
323 | } | |
324 | ||
7a186941 OBC |
325 | /* assign an rproc-wide unique index for this vring */ |
326 | /* TODO: assign a notifyid for rvdev updates as well */ | |
327 | ret = idr_get_new(&rproc->notifyids, &rvdev->vring[i], ¬ifyid); | |
328 | if (ret) { | |
329 | dev_err(dev, "idr_get_new failed: %d\n", ret); | |
330 | dma_free_coherent(dev, size, va, dma); | |
331 | return ret; | |
332 | } | |
400e64df | 333 | |
7a186941 OBC |
334 | /* let the rproc know the da and notifyid of this vring */ |
335 | /* TODO: expose this to remote processor */ | |
336 | vring->da = dma; | |
337 | vring->notifyid = notifyid; | |
400e64df | 338 | |
7a186941 OBC |
339 | dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va, |
340 | dma, size, notifyid); | |
341 | ||
342 | rvdev->vring[i].len = vring->num; | |
343 | rvdev->vring[i].va = va; | |
344 | rvdev->vring[i].dma = dma; | |
345 | rvdev->vring[i].notifyid = notifyid; | |
346 | rvdev->vring[i].rvdev = rvdev; | |
400e64df OBC |
347 | |
348 | return 0; | |
349 | } | |
350 | ||
7a186941 OBC |
351 | static void __rproc_free_vrings(struct rproc_vdev *rvdev, int i) |
352 | { | |
353 | struct rproc *rproc = rvdev->rproc; | |
354 | ||
355 | for (i--; i > 0; i--) { | |
356 | struct rproc_vring *rvring = &rvdev->vring[i]; | |
357 | int size = PAGE_ALIGN(vring_size(rvring->len, AMP_VRING_ALIGN)); | |
358 | ||
359 | dma_free_coherent(rproc->dev, size, rvring->va, rvring->dma); | |
360 | idr_remove(&rproc->notifyids, rvring->notifyid); | |
361 | } | |
362 | } | |
363 | ||
400e64df | 364 | /** |
fd2c15ec | 365 | * rproc_handle_vdev() - handle a vdev fw resource |
400e64df OBC |
366 | * @rproc: the remote processor |
367 | * @rsc: the vring resource descriptor | |
fd2c15ec | 368 | * @avail: size of available data (for sanity checking the image) |
400e64df | 369 | * |
7a186941 OBC |
370 | * This resource entry requests the host to statically register a virtio |
371 | * device (vdev), and setup everything needed to support it. It contains | |
372 | * everything needed to make it possible: the virtio device id, virtio | |
373 | * device features, vrings information, virtio config space, etc... | |
374 | * | |
375 | * Before registering the vdev, the vrings are allocated from non-cacheable | |
376 | * physically contiguous memory. Currently we only support two vrings per | |
377 | * remote processor (temporary limitation). We might also want to consider | |
378 | * doing the vring allocation only later when ->find_vqs() is invoked, and | |
379 | * then release them upon ->del_vqs(). | |
380 | * | |
381 | * Note: @da is currently not really handled correctly: we dynamically | |
382 | * allocate it using the DMA API, ignoring requested hard coded addresses, | |
383 | * and we don't take care of any required IOMMU programming. This is all | |
384 | * going to be taken care of when the generic iommu-based DMA API will be | |
385 | * merged. Meanwhile, statically-addressed iommu-based firmware images should | |
386 | * use RSC_DEVMEM resource entries to map their required @da to the physical | |
387 | * address of their base CMA region (ouch, hacky!). | |
400e64df OBC |
388 | * |
389 | * Returns 0 on success, or an appropriate error code otherwise | |
390 | */ | |
fd2c15ec OBC |
391 | static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc, |
392 | int avail) | |
400e64df OBC |
393 | { |
394 | struct device *dev = rproc->dev; | |
7a186941 OBC |
395 | struct rproc_vdev *rvdev; |
396 | int i, ret; | |
400e64df | 397 | |
fd2c15ec OBC |
398 | /* make sure resource isn't truncated */ |
399 | if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring) | |
400 | + rsc->config_len > avail) { | |
401 | dev_err(rproc->dev, "vdev rsc is truncated\n"); | |
400e64df OBC |
402 | return -EINVAL; |
403 | } | |
404 | ||
fd2c15ec OBC |
405 | /* make sure reserved bytes are zeroes */ |
406 | if (rsc->reserved[0] || rsc->reserved[1]) { | |
407 | dev_err(dev, "vdev rsc has non zero reserved bytes\n"); | |
400e64df OBC |
408 | return -EINVAL; |
409 | } | |
410 | ||
fd2c15ec OBC |
411 | dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n", |
412 | rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); | |
413 | ||
7a186941 OBC |
414 | /* we currently support only two vrings per rvdev */ |
415 | if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { | |
fd2c15ec | 416 | dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); |
400e64df OBC |
417 | return -EINVAL; |
418 | } | |
419 | ||
7a186941 OBC |
420 | rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL); |
421 | if (!rvdev) | |
422 | return -ENOMEM; | |
400e64df | 423 | |
7a186941 | 424 | rvdev->rproc = rproc; |
400e64df | 425 | |
7a186941 OBC |
426 | /* allocate the vrings */ |
427 | for (i = 0; i < rsc->num_of_vrings; i++) { | |
428 | ret = __rproc_handle_vring(rvdev, rsc, i); | |
429 | if (ret) | |
430 | goto free_vrings; | |
431 | } | |
400e64df | 432 | |
7a186941 OBC |
433 | /* remember the device features */ |
434 | rvdev->dfeatures = rsc->dfeatures; | |
fd2c15ec | 435 | |
7a186941 | 436 | list_add_tail(&rvdev->node, &rproc->rvdevs); |
fd2c15ec | 437 | |
7a186941 OBC |
438 | /* it is now safe to add the virtio device */ |
439 | ret = rproc_add_virtio_dev(rvdev, rsc->id); | |
440 | if (ret) | |
441 | goto free_vrings; | |
400e64df OBC |
442 | |
443 | return 0; | |
7a186941 OBC |
444 | |
445 | free_vrings: | |
446 | __rproc_free_vrings(rvdev, i); | |
447 | kfree(rvdev); | |
448 | return ret; | |
400e64df OBC |
449 | } |
450 | ||
451 | /** | |
452 | * rproc_handle_trace() - handle a shared trace buffer resource | |
453 | * @rproc: the remote processor | |
454 | * @rsc: the trace resource descriptor | |
fd2c15ec | 455 | * @avail: size of available data (for sanity checking the image) |
400e64df OBC |
456 | * |
457 | * In case the remote processor dumps trace logs into memory, | |
458 | * export it via debugfs. | |
459 | * | |
460 | * Currently, the 'da' member of @rsc should contain the device address | |
461 | * where the remote processor is dumping the traces. Later we could also | |
462 | * support dynamically allocating this address using the generic | |
463 | * DMA API (but currently there isn't a use case for that). | |
464 | * | |
465 | * Returns 0 on success, or an appropriate error code otherwise | |
466 | */ | |
fd2c15ec OBC |
467 | static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc, |
468 | int avail) | |
400e64df OBC |
469 | { |
470 | struct rproc_mem_entry *trace; | |
471 | struct device *dev = rproc->dev; | |
472 | void *ptr; | |
473 | char name[15]; | |
474 | ||
fd2c15ec OBC |
475 | if (sizeof(*rsc) > avail) { |
476 | dev_err(rproc->dev, "trace rsc is truncated\n"); | |
477 | return -EINVAL; | |
478 | } | |
479 | ||
480 | /* make sure reserved bytes are zeroes */ | |
481 | if (rsc->reserved) { | |
482 | dev_err(dev, "trace rsc has non zero reserved bytes\n"); | |
483 | return -EINVAL; | |
484 | } | |
485 | ||
400e64df OBC |
486 | /* what's the kernel address of this resource ? */ |
487 | ptr = rproc_da_to_va(rproc, rsc->da, rsc->len); | |
488 | if (!ptr) { | |
489 | dev_err(dev, "erroneous trace resource entry\n"); | |
490 | return -EINVAL; | |
491 | } | |
492 | ||
493 | trace = kzalloc(sizeof(*trace), GFP_KERNEL); | |
494 | if (!trace) { | |
495 | dev_err(dev, "kzalloc trace failed\n"); | |
496 | return -ENOMEM; | |
497 | } | |
498 | ||
499 | /* set the trace buffer dma properties */ | |
500 | trace->len = rsc->len; | |
501 | trace->va = ptr; | |
502 | ||
503 | /* make sure snprintf always null terminates, even if truncating */ | |
504 | snprintf(name, sizeof(name), "trace%d", rproc->num_traces); | |
505 | ||
506 | /* create the debugfs entry */ | |
507 | trace->priv = rproc_create_trace_file(name, rproc, trace); | |
508 | if (!trace->priv) { | |
509 | trace->va = NULL; | |
510 | kfree(trace); | |
511 | return -EINVAL; | |
512 | } | |
513 | ||
514 | list_add_tail(&trace->node, &rproc->traces); | |
515 | ||
516 | rproc->num_traces++; | |
517 | ||
fd2c15ec | 518 | dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr, |
400e64df OBC |
519 | rsc->da, rsc->len); |
520 | ||
521 | return 0; | |
522 | } | |
523 | ||
524 | /** | |
525 | * rproc_handle_devmem() - handle devmem resource entry | |
526 | * @rproc: remote processor handle | |
527 | * @rsc: the devmem resource entry | |
fd2c15ec | 528 | * @avail: size of available data (for sanity checking the image) |
400e64df OBC |
529 | * |
530 | * Remote processors commonly need to access certain on-chip peripherals. | |
531 | * | |
532 | * Some of these remote processors access memory via an iommu device, | |
533 | * and might require us to configure their iommu before they can access | |
534 | * the on-chip peripherals they need. | |
535 | * | |
536 | * This resource entry is a request to map such a peripheral device. | |
537 | * | |
538 | * These devmem entries will contain the physical address of the device in | |
539 | * the 'pa' member. If a specific device address is expected, then 'da' will | |
540 | * contain it (currently this is the only use case supported). 'len' will | |
541 | * contain the size of the physical region we need to map. | |
542 | * | |
543 | * Currently we just "trust" those devmem entries to contain valid physical | |
544 | * addresses, but this is going to change: we want the implementations to | |
545 | * tell us ranges of physical addresses the firmware is allowed to request, | |
546 | * and not allow firmwares to request access to physical addresses that | |
547 | * are outside those ranges. | |
548 | */ | |
fd2c15ec OBC |
549 | static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc, |
550 | int avail) | |
400e64df OBC |
551 | { |
552 | struct rproc_mem_entry *mapping; | |
553 | int ret; | |
554 | ||
555 | /* no point in handling this resource without a valid iommu domain */ | |
556 | if (!rproc->domain) | |
557 | return -EINVAL; | |
558 | ||
fd2c15ec OBC |
559 | if (sizeof(*rsc) > avail) { |
560 | dev_err(rproc->dev, "devmem rsc is truncated\n"); | |
561 | return -EINVAL; | |
562 | } | |
563 | ||
564 | /* make sure reserved bytes are zeroes */ | |
565 | if (rsc->reserved) { | |
566 | dev_err(rproc->dev, "devmem rsc has non zero reserved bytes\n"); | |
567 | return -EINVAL; | |
568 | } | |
569 | ||
400e64df OBC |
570 | mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); |
571 | if (!mapping) { | |
572 | dev_err(rproc->dev, "kzalloc mapping failed\n"); | |
573 | return -ENOMEM; | |
574 | } | |
575 | ||
576 | ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); | |
577 | if (ret) { | |
578 | dev_err(rproc->dev, "failed to map devmem: %d\n", ret); | |
579 | goto out; | |
580 | } | |
581 | ||
582 | /* | |
583 | * We'll need this info later when we'll want to unmap everything | |
584 | * (e.g. on shutdown). | |
585 | * | |
586 | * We can't trust the remote processor not to change the resource | |
587 | * table, so we must maintain this info independently. | |
588 | */ | |
589 | mapping->da = rsc->da; | |
590 | mapping->len = rsc->len; | |
591 | list_add_tail(&mapping->node, &rproc->mappings); | |
592 | ||
fd2c15ec | 593 | dev_dbg(rproc->dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", |
400e64df OBC |
594 | rsc->pa, rsc->da, rsc->len); |
595 | ||
596 | return 0; | |
597 | ||
598 | out: | |
599 | kfree(mapping); | |
600 | return ret; | |
601 | } | |
602 | ||
603 | /** | |
604 | * rproc_handle_carveout() - handle phys contig memory allocation requests | |
605 | * @rproc: rproc handle | |
606 | * @rsc: the resource entry | |
fd2c15ec | 607 | * @avail: size of available data (for image validation) |
400e64df OBC |
608 | * |
609 | * This function will handle firmware requests for allocation of physically | |
610 | * contiguous memory regions. | |
611 | * | |
612 | * These request entries should come first in the firmware's resource table, | |
613 | * as other firmware entries might request placing other data objects inside | |
614 | * these memory regions (e.g. data/code segments, trace resource entries, ...). | |
615 | * | |
616 | * Allocating memory this way helps utilizing the reserved physical memory | |
617 | * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries | |
618 | * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB | |
619 | * pressure is important; it may have a substantial impact on performance. | |
620 | */ | |
fd2c15ec OBC |
621 | static int rproc_handle_carveout(struct rproc *rproc, |
622 | struct fw_rsc_carveout *rsc, int avail) | |
400e64df OBC |
623 | { |
624 | struct rproc_mem_entry *carveout, *mapping; | |
625 | struct device *dev = rproc->dev; | |
626 | dma_addr_t dma; | |
627 | void *va; | |
628 | int ret; | |
629 | ||
fd2c15ec OBC |
630 | if (sizeof(*rsc) > avail) { |
631 | dev_err(rproc->dev, "carveout rsc is truncated\n"); | |
632 | return -EINVAL; | |
633 | } | |
634 | ||
635 | /* make sure reserved bytes are zeroes */ | |
636 | if (rsc->reserved) { | |
637 | dev_err(dev, "carveout rsc has non zero reserved bytes\n"); | |
638 | return -EINVAL; | |
639 | } | |
640 | ||
641 | dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n", | |
642 | rsc->da, rsc->pa, rsc->len, rsc->flags); | |
643 | ||
400e64df OBC |
644 | mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); |
645 | if (!mapping) { | |
646 | dev_err(dev, "kzalloc mapping failed\n"); | |
647 | return -ENOMEM; | |
648 | } | |
649 | ||
650 | carveout = kzalloc(sizeof(*carveout), GFP_KERNEL); | |
651 | if (!carveout) { | |
652 | dev_err(dev, "kzalloc carveout failed\n"); | |
653 | ret = -ENOMEM; | |
654 | goto free_mapping; | |
655 | } | |
656 | ||
657 | va = dma_alloc_coherent(dev, rsc->len, &dma, GFP_KERNEL); | |
658 | if (!va) { | |
659 | dev_err(dev, "failed to dma alloc carveout: %d\n", rsc->len); | |
660 | ret = -ENOMEM; | |
661 | goto free_carv; | |
662 | } | |
663 | ||
664 | dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len); | |
665 | ||
666 | /* | |
667 | * Ok, this is non-standard. | |
668 | * | |
669 | * Sometimes we can't rely on the generic iommu-based DMA API | |
670 | * to dynamically allocate the device address and then set the IOMMU | |
671 | * tables accordingly, because some remote processors might | |
672 | * _require_ us to use hard coded device addresses that their | |
673 | * firmware was compiled with. | |
674 | * | |
675 | * In this case, we must use the IOMMU API directly and map | |
676 | * the memory to the device address as expected by the remote | |
677 | * processor. | |
678 | * | |
679 | * Obviously such remote processor devices should not be configured | |
680 | * to use the iommu-based DMA API: we expect 'dma' to contain the | |
681 | * physical address in this case. | |
682 | */ | |
683 | if (rproc->domain) { | |
684 | ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len, | |
685 | rsc->flags); | |
686 | if (ret) { | |
687 | dev_err(dev, "iommu_map failed: %d\n", ret); | |
688 | goto dma_free; | |
689 | } | |
690 | ||
691 | /* | |
692 | * We'll need this info later when we'll want to unmap | |
693 | * everything (e.g. on shutdown). | |
694 | * | |
695 | * We can't trust the remote processor not to change the | |
696 | * resource table, so we must maintain this info independently. | |
697 | */ | |
698 | mapping->da = rsc->da; | |
699 | mapping->len = rsc->len; | |
700 | list_add_tail(&mapping->node, &rproc->mappings); | |
701 | ||
fd2c15ec | 702 | dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma); |
400e64df OBC |
703 | |
704 | /* | |
705 | * Some remote processors might need to know the pa | |
706 | * even though they are behind an IOMMU. E.g., OMAP4's | |
707 | * remote M3 processor needs this so it can control | |
708 | * on-chip hardware accelerators that are not behind | |
709 | * the IOMMU, and therefor must know the pa. | |
710 | * | |
711 | * Generally we don't want to expose physical addresses | |
712 | * if we don't have to (remote processors are generally | |
713 | * _not_ trusted), so we might want to do this only for | |
714 | * remote processor that _must_ have this (e.g. OMAP4's | |
715 | * dual M3 subsystem). | |
716 | */ | |
717 | rsc->pa = dma; | |
718 | } | |
719 | ||
720 | carveout->va = va; | |
721 | carveout->len = rsc->len; | |
722 | carveout->dma = dma; | |
723 | carveout->da = rsc->da; | |
724 | ||
725 | list_add_tail(&carveout->node, &rproc->carveouts); | |
726 | ||
727 | return 0; | |
728 | ||
729 | dma_free: | |
730 | dma_free_coherent(dev, rsc->len, va, dma); | |
731 | free_carv: | |
732 | kfree(carveout); | |
733 | free_mapping: | |
734 | kfree(mapping); | |
735 | return ret; | |
736 | } | |
737 | ||
e12bc14b OBC |
738 | /* |
739 | * A lookup table for resource handlers. The indices are defined in | |
740 | * enum fw_resource_type. | |
741 | */ | |
742 | static rproc_handle_resource_t rproc_handle_rsc[] = { | |
fd2c15ec OBC |
743 | [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout, |
744 | [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem, | |
745 | [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace, | |
7a186941 | 746 | [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */ |
e12bc14b OBC |
747 | }; |
748 | ||
400e64df OBC |
749 | /* handle firmware resource entries before booting the remote processor */ |
750 | static int | |
fd2c15ec | 751 | rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len) |
400e64df OBC |
752 | { |
753 | struct device *dev = rproc->dev; | |
e12bc14b | 754 | rproc_handle_resource_t handler; |
fd2c15ec OBC |
755 | int ret = 0, i; |
756 | ||
757 | for (i = 0; i < table->num; i++) { | |
758 | int offset = table->offset[i]; | |
759 | struct fw_rsc_hdr *hdr = (void *)table + offset; | |
760 | int avail = len - offset - sizeof(*hdr); | |
761 | void *rsc = (void *)hdr + sizeof(*hdr); | |
762 | ||
763 | /* make sure table isn't truncated */ | |
764 | if (avail < 0) { | |
765 | dev_err(dev, "rsc table is truncated\n"); | |
766 | return -EINVAL; | |
767 | } | |
400e64df | 768 | |
fd2c15ec | 769 | dev_dbg(dev, "rsc: type %d\n", hdr->type); |
400e64df | 770 | |
fd2c15ec OBC |
771 | if (hdr->type >= RSC_LAST) { |
772 | dev_warn(dev, "unsupported resource %d\n", hdr->type); | |
e12bc14b | 773 | continue; |
400e64df OBC |
774 | } |
775 | ||
fd2c15ec | 776 | handler = rproc_handle_rsc[hdr->type]; |
e12bc14b OBC |
777 | if (!handler) |
778 | continue; | |
779 | ||
fd2c15ec | 780 | ret = handler(rproc, rsc, avail); |
400e64df OBC |
781 | if (ret) |
782 | break; | |
400e64df OBC |
783 | } |
784 | ||
785 | return ret; | |
786 | } | |
787 | ||
788 | /* handle firmware resource entries while registering the remote processor */ | |
789 | static int | |
fd2c15ec | 790 | rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len) |
400e64df OBC |
791 | { |
792 | struct device *dev = rproc->dev; | |
fd2c15ec OBC |
793 | int ret = 0, i; |
794 | ||
795 | for (i = 0; i < table->num; i++) { | |
796 | int offset = table->offset[i]; | |
797 | struct fw_rsc_hdr *hdr = (void *)table + offset; | |
798 | int avail = len - offset - sizeof(*hdr); | |
7a186941 | 799 | struct fw_rsc_vdev *vrsc; |
400e64df | 800 | |
fd2c15ec OBC |
801 | /* make sure table isn't truncated */ |
802 | if (avail < 0) { | |
803 | dev_err(dev, "rsc table is truncated\n"); | |
804 | return -EINVAL; | |
805 | } | |
806 | ||
807 | dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type); | |
808 | ||
7a186941 OBC |
809 | if (hdr->type != RSC_VDEV) |
810 | continue; | |
811 | ||
812 | vrsc = (struct fw_rsc_vdev *)hdr->data; | |
813 | ||
814 | ret = rproc_handle_vdev(rproc, vrsc, avail); | |
815 | if (ret) | |
400e64df | 816 | break; |
fd2c15ec | 817 | } |
400e64df OBC |
818 | |
819 | return ret; | |
820 | } | |
821 | ||
822 | /** | |
823 | * rproc_handle_resources() - find and handle the resource table | |
824 | * @rproc: the rproc handle | |
825 | * @elf_data: the content of the ELF firmware image | |
9bc91231 | 826 | * @len: firmware size (in bytes) |
400e64df OBC |
827 | * @handler: function that should be used to handle the resource table |
828 | * | |
829 | * This function finds the resource table inside the remote processor's | |
830 | * firmware, and invoke a user-supplied handler with it (we have two | |
831 | * possible handlers: one is invoked upon registration of @rproc, | |
832 | * in order to register the supported virito devices, and the other is | |
833 | * invoked when @rproc is actually booted). | |
834 | * | |
835 | * Currently this function fails if a resource table doesn't exist. | |
836 | * This restriction will be removed when we'll start supporting remote | |
837 | * processors that don't need a resource table. | |
838 | */ | |
839 | static int rproc_handle_resources(struct rproc *rproc, const u8 *elf_data, | |
9bc91231 | 840 | size_t len, rproc_handle_resources_t handler) |
400e64df OBC |
841 | |
842 | { | |
843 | struct elf32_hdr *ehdr; | |
844 | struct elf32_shdr *shdr; | |
845 | const char *name_table; | |
fd2c15ec | 846 | struct device *dev = rproc->dev; |
400e64df | 847 | int i, ret = -EINVAL; |
fd2c15ec | 848 | struct resource_table *table; |
400e64df OBC |
849 | |
850 | ehdr = (struct elf32_hdr *)elf_data; | |
851 | shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff); | |
852 | name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset; | |
853 | ||
854 | /* look for the resource table and handle it */ | |
855 | for (i = 0; i < ehdr->e_shnum; i++, shdr++) { | |
fd2c15ec OBC |
856 | int size = shdr->sh_size; |
857 | int offset = shdr->sh_offset; | |
400e64df | 858 | |
fd2c15ec OBC |
859 | if (strcmp(name_table + shdr->sh_name, ".resource_table")) |
860 | continue; | |
9bc91231 | 861 | |
fd2c15ec | 862 | table = (struct resource_table *)(elf_data + offset); |
400e64df | 863 | |
fd2c15ec OBC |
864 | /* make sure we have the entire table */ |
865 | if (offset + size > len) { | |
866 | dev_err(dev, "resource table truncated\n"); | |
867 | return -EINVAL; | |
868 | } | |
869 | ||
870 | /* make sure table has at least the header */ | |
871 | if (sizeof(struct resource_table) > size) { | |
872 | dev_err(dev, "header-less resource table\n"); | |
873 | return -EINVAL; | |
400e64df | 874 | } |
fd2c15ec OBC |
875 | |
876 | /* we don't support any version beyond the first */ | |
877 | if (table->ver != 1) { | |
878 | dev_err(dev, "unsupported fw ver: %d\n", table->ver); | |
879 | return -EINVAL; | |
880 | } | |
881 | ||
882 | /* make sure reserved bytes are zeroes */ | |
883 | if (table->reserved[0] || table->reserved[1]) { | |
884 | dev_err(dev, "non zero reserved bytes\n"); | |
885 | return -EINVAL; | |
886 | } | |
887 | ||
888 | /* make sure the offsets array isn't truncated */ | |
889 | if (table->num * sizeof(table->offset[0]) + | |
890 | sizeof(struct resource_table) > size) { | |
891 | dev_err(dev, "resource table incomplete\n"); | |
892 | return -EINVAL; | |
893 | } | |
894 | ||
895 | ret = handler(rproc, table, shdr->sh_size); | |
896 | break; | |
400e64df OBC |
897 | } |
898 | ||
899 | return ret; | |
900 | } | |
901 | ||
902 | /** | |
903 | * rproc_resource_cleanup() - clean up and free all acquired resources | |
904 | * @rproc: rproc handle | |
905 | * | |
906 | * This function will free all resources acquired for @rproc, and it | |
7a186941 | 907 | * is called whenever @rproc either shuts down or fails to boot. |
400e64df OBC |
908 | */ |
909 | static void rproc_resource_cleanup(struct rproc *rproc) | |
910 | { | |
911 | struct rproc_mem_entry *entry, *tmp; | |
912 | struct device *dev = rproc->dev; | |
400e64df OBC |
913 | |
914 | /* clean up debugfs trace entries */ | |
915 | list_for_each_entry_safe(entry, tmp, &rproc->traces, node) { | |
916 | rproc_remove_trace_file(entry->priv); | |
917 | rproc->num_traces--; | |
918 | list_del(&entry->node); | |
919 | kfree(entry); | |
920 | } | |
921 | ||
400e64df OBC |
922 | /* clean up carveout allocations */ |
923 | list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { | |
924 | dma_free_coherent(dev, entry->len, entry->va, entry->dma); | |
925 | list_del(&entry->node); | |
926 | kfree(entry); | |
927 | } | |
928 | ||
929 | /* clean up iommu mapping entries */ | |
930 | list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { | |
931 | size_t unmapped; | |
932 | ||
933 | unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); | |
934 | if (unmapped != entry->len) { | |
935 | /* nothing much to do besides complaining */ | |
936 | dev_err(dev, "failed to unmap %u/%u\n", entry->len, | |
937 | unmapped); | |
938 | } | |
939 | ||
940 | list_del(&entry->node); | |
941 | kfree(entry); | |
942 | } | |
943 | } | |
944 | ||
945 | /* make sure this fw image is sane */ | |
946 | static int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw) | |
947 | { | |
948 | const char *name = rproc->firmware; | |
949 | struct device *dev = rproc->dev; | |
950 | struct elf32_hdr *ehdr; | |
40b78b2c | 951 | char class; |
400e64df OBC |
952 | |
953 | if (!fw) { | |
954 | dev_err(dev, "failed to load %s\n", name); | |
955 | return -EINVAL; | |
956 | } | |
957 | ||
958 | if (fw->size < sizeof(struct elf32_hdr)) { | |
959 | dev_err(dev, "Image is too small\n"); | |
960 | return -EINVAL; | |
961 | } | |
962 | ||
963 | ehdr = (struct elf32_hdr *)fw->data; | |
964 | ||
40b78b2c OBC |
965 | /* We only support ELF32 at this point */ |
966 | class = ehdr->e_ident[EI_CLASS]; | |
967 | if (class != ELFCLASS32) { | |
968 | dev_err(dev, "Unsupported class: %d\n", class); | |
969 | return -EINVAL; | |
970 | } | |
971 | ||
cf59d3e9 OBC |
972 | /* We assume the firmware has the same endianess as the host */ |
973 | # ifdef __LITTLE_ENDIAN | |
974 | if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) { | |
975 | # else /* BIG ENDIAN */ | |
976 | if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) { | |
977 | # endif | |
978 | dev_err(dev, "Unsupported firmware endianess\n"); | |
979 | return -EINVAL; | |
980 | } | |
981 | ||
9bc91231 OBC |
982 | if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) { |
983 | dev_err(dev, "Image is too small\n"); | |
984 | return -EINVAL; | |
985 | } | |
986 | ||
400e64df OBC |
987 | if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) { |
988 | dev_err(dev, "Image is corrupted (bad magic)\n"); | |
989 | return -EINVAL; | |
990 | } | |
991 | ||
992 | if (ehdr->e_phnum == 0) { | |
993 | dev_err(dev, "No loadable segments\n"); | |
994 | return -EINVAL; | |
995 | } | |
996 | ||
997 | if (ehdr->e_phoff > fw->size) { | |
998 | dev_err(dev, "Firmware size is too small\n"); | |
999 | return -EINVAL; | |
1000 | } | |
1001 | ||
1002 | return 0; | |
1003 | } | |
1004 | ||
1005 | /* | |
1006 | * take a firmware and boot a remote processor with it. | |
1007 | */ | |
1008 | static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) | |
1009 | { | |
1010 | struct device *dev = rproc->dev; | |
1011 | const char *name = rproc->firmware; | |
1012 | struct elf32_hdr *ehdr; | |
1013 | int ret; | |
1014 | ||
1015 | ret = rproc_fw_sanity_check(rproc, fw); | |
1016 | if (ret) | |
1017 | return ret; | |
1018 | ||
1019 | ehdr = (struct elf32_hdr *)fw->data; | |
1020 | ||
1021 | dev_info(dev, "Booting fw image %s, size %d\n", name, fw->size); | |
1022 | ||
1023 | /* | |
1024 | * if enabling an IOMMU isn't relevant for this rproc, this is | |
1025 | * just a nop | |
1026 | */ | |
1027 | ret = rproc_enable_iommu(rproc); | |
1028 | if (ret) { | |
1029 | dev_err(dev, "can't enable iommu: %d\n", ret); | |
1030 | return ret; | |
1031 | } | |
1032 | ||
1033 | /* | |
1034 | * The ELF entry point is the rproc's boot addr (though this is not | |
1035 | * a configurable property of all remote processors: some will always | |
1036 | * boot at a specific hardcoded address). | |
1037 | */ | |
1038 | rproc->bootaddr = ehdr->e_entry; | |
1039 | ||
1040 | /* handle fw resources which are required to boot rproc */ | |
9bc91231 OBC |
1041 | ret = rproc_handle_resources(rproc, fw->data, fw->size, |
1042 | rproc_handle_boot_rsc); | |
400e64df OBC |
1043 | if (ret) { |
1044 | dev_err(dev, "Failed to process resources: %d\n", ret); | |
1045 | goto clean_up; | |
1046 | } | |
1047 | ||
1048 | /* load the ELF segments to memory */ | |
9bc91231 | 1049 | ret = rproc_load_segments(rproc, fw->data, fw->size); |
400e64df OBC |
1050 | if (ret) { |
1051 | dev_err(dev, "Failed to load program segments: %d\n", ret); | |
1052 | goto clean_up; | |
1053 | } | |
1054 | ||
1055 | /* power up the remote processor */ | |
1056 | ret = rproc->ops->start(rproc); | |
1057 | if (ret) { | |
1058 | dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); | |
1059 | goto clean_up; | |
1060 | } | |
1061 | ||
1062 | rproc->state = RPROC_RUNNING; | |
1063 | ||
1064 | dev_info(dev, "remote processor %s is now up\n", rproc->name); | |
1065 | ||
1066 | return 0; | |
1067 | ||
1068 | clean_up: | |
1069 | rproc_resource_cleanup(rproc); | |
1070 | rproc_disable_iommu(rproc); | |
1071 | return ret; | |
1072 | } | |
1073 | ||
1074 | /* | |
1075 | * take a firmware and look for virtio devices to register. | |
1076 | * | |
1077 | * Note: this function is called asynchronously upon registration of the | |
1078 | * remote processor (so we must wait until it completes before we try | |
1079 | * to unregister the device. one other option is just to use kref here, | |
1080 | * that might be cleaner). | |
1081 | */ | |
1082 | static void rproc_fw_config_virtio(const struct firmware *fw, void *context) | |
1083 | { | |
1084 | struct rproc *rproc = context; | |
1085 | struct device *dev = rproc->dev; | |
1086 | int ret; | |
1087 | ||
1088 | if (rproc_fw_sanity_check(rproc, fw) < 0) | |
1089 | goto out; | |
1090 | ||
fd2c15ec | 1091 | /* does the fw support any virtio devices ? */ |
9bc91231 OBC |
1092 | ret = rproc_handle_resources(rproc, fw->data, fw->size, |
1093 | rproc_handle_virtio_rsc); | |
400e64df OBC |
1094 | if (ret) { |
1095 | dev_info(dev, "No fw virtio device was found\n"); | |
1096 | goto out; | |
1097 | } | |
1098 | ||
400e64df OBC |
1099 | out: |
1100 | if (fw) | |
1101 | release_firmware(fw); | |
1102 | /* allow rproc_unregister() contexts, if any, to proceed */ | |
1103 | complete_all(&rproc->firmware_loading_complete); | |
1104 | } | |
1105 | ||
1106 | /** | |
1107 | * rproc_boot() - boot a remote processor | |
1108 | * @rproc: handle of a remote processor | |
1109 | * | |
1110 | * Boot a remote processor (i.e. load its firmware, power it on, ...). | |
1111 | * | |
1112 | * If the remote processor is already powered on, this function immediately | |
1113 | * returns (successfully). | |
1114 | * | |
1115 | * Returns 0 on success, and an appropriate error value otherwise. | |
1116 | */ | |
1117 | int rproc_boot(struct rproc *rproc) | |
1118 | { | |
1119 | const struct firmware *firmware_p; | |
1120 | struct device *dev; | |
1121 | int ret; | |
1122 | ||
1123 | if (!rproc) { | |
1124 | pr_err("invalid rproc handle\n"); | |
1125 | return -EINVAL; | |
1126 | } | |
1127 | ||
1128 | dev = rproc->dev; | |
1129 | ||
1130 | ret = mutex_lock_interruptible(&rproc->lock); | |
1131 | if (ret) { | |
1132 | dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); | |
1133 | return ret; | |
1134 | } | |
1135 | ||
1136 | /* loading a firmware is required */ | |
1137 | if (!rproc->firmware) { | |
1138 | dev_err(dev, "%s: no firmware to load\n", __func__); | |
1139 | ret = -EINVAL; | |
1140 | goto unlock_mutex; | |
1141 | } | |
1142 | ||
1143 | /* prevent underlying implementation from being removed */ | |
1144 | if (!try_module_get(dev->driver->owner)) { | |
1145 | dev_err(dev, "%s: can't get owner\n", __func__); | |
1146 | ret = -EINVAL; | |
1147 | goto unlock_mutex; | |
1148 | } | |
1149 | ||
1150 | /* skip the boot process if rproc is already powered up */ | |
1151 | if (atomic_inc_return(&rproc->power) > 1) { | |
1152 | ret = 0; | |
1153 | goto unlock_mutex; | |
1154 | } | |
1155 | ||
1156 | dev_info(dev, "powering up %s\n", rproc->name); | |
1157 | ||
1158 | /* load firmware */ | |
1159 | ret = request_firmware(&firmware_p, rproc->firmware, dev); | |
1160 | if (ret < 0) { | |
1161 | dev_err(dev, "request_firmware failed: %d\n", ret); | |
1162 | goto downref_rproc; | |
1163 | } | |
1164 | ||
1165 | ret = rproc_fw_boot(rproc, firmware_p); | |
1166 | ||
1167 | release_firmware(firmware_p); | |
1168 | ||
1169 | downref_rproc: | |
1170 | if (ret) { | |
1171 | module_put(dev->driver->owner); | |
1172 | atomic_dec(&rproc->power); | |
1173 | } | |
1174 | unlock_mutex: | |
1175 | mutex_unlock(&rproc->lock); | |
1176 | return ret; | |
1177 | } | |
1178 | EXPORT_SYMBOL(rproc_boot); | |
1179 | ||
1180 | /** | |
1181 | * rproc_shutdown() - power off the remote processor | |
1182 | * @rproc: the remote processor | |
1183 | * | |
1184 | * Power off a remote processor (previously booted with rproc_boot()). | |
1185 | * | |
1186 | * In case @rproc is still being used by an additional user(s), then | |
1187 | * this function will just decrement the power refcount and exit, | |
1188 | * without really powering off the device. | |
1189 | * | |
1190 | * Every call to rproc_boot() must (eventually) be accompanied by a call | |
1191 | * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. | |
1192 | * | |
1193 | * Notes: | |
1194 | * - we're not decrementing the rproc's refcount, only the power refcount. | |
1195 | * which means that the @rproc handle stays valid even after rproc_shutdown() | |
1196 | * returns, and users can still use it with a subsequent rproc_boot(), if | |
1197 | * needed. | |
1198 | * - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly | |
1199 | * because rproc_shutdown() _does not_ decrement the refcount of @rproc. | |
1200 | * To decrement the refcount of @rproc, use rproc_put() (but _only_ if | |
1201 | * you acquired @rproc using rproc_get_by_name()). | |
1202 | */ | |
1203 | void rproc_shutdown(struct rproc *rproc) | |
1204 | { | |
1205 | struct device *dev = rproc->dev; | |
1206 | int ret; | |
1207 | ||
1208 | ret = mutex_lock_interruptible(&rproc->lock); | |
1209 | if (ret) { | |
1210 | dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); | |
1211 | return; | |
1212 | } | |
1213 | ||
1214 | /* if the remote proc is still needed, bail out */ | |
1215 | if (!atomic_dec_and_test(&rproc->power)) | |
1216 | goto out; | |
1217 | ||
1218 | /* power off the remote processor */ | |
1219 | ret = rproc->ops->stop(rproc); | |
1220 | if (ret) { | |
1221 | atomic_inc(&rproc->power); | |
1222 | dev_err(dev, "can't stop rproc: %d\n", ret); | |
1223 | goto out; | |
1224 | } | |
1225 | ||
1226 | /* clean up all acquired resources */ | |
1227 | rproc_resource_cleanup(rproc); | |
1228 | ||
1229 | rproc_disable_iommu(rproc); | |
1230 | ||
1231 | rproc->state = RPROC_OFFLINE; | |
1232 | ||
1233 | dev_info(dev, "stopped remote processor %s\n", rproc->name); | |
1234 | ||
1235 | out: | |
1236 | mutex_unlock(&rproc->lock); | |
1237 | if (!ret) | |
1238 | module_put(dev->driver->owner); | |
1239 | } | |
1240 | EXPORT_SYMBOL(rproc_shutdown); | |
1241 | ||
1242 | /** | |
1243 | * rproc_release() - completely deletes the existence of a remote processor | |
1244 | * @kref: the rproc's kref | |
1245 | * | |
1246 | * This function should _never_ be called directly. | |
1247 | * | |
1248 | * The only reasonable location to use it is as an argument when kref_put'ing | |
1249 | * @rproc's refcount. | |
1250 | * | |
1251 | * This way it will be called when no one holds a valid pointer to this @rproc | |
1252 | * anymore (and obviously after it is removed from the rprocs klist). | |
1253 | * | |
1254 | * Note: this function is not static because rproc_vdev_release() needs it when | |
1255 | * it decrements @rproc's refcount. | |
1256 | */ | |
1257 | void rproc_release(struct kref *kref) | |
1258 | { | |
1259 | struct rproc *rproc = container_of(kref, struct rproc, refcount); | |
7a186941 | 1260 | struct rproc_vdev *rvdev, *rvtmp; |
400e64df OBC |
1261 | |
1262 | dev_info(rproc->dev, "removing %s\n", rproc->name); | |
1263 | ||
1264 | rproc_delete_debug_dir(rproc); | |
1265 | ||
7a186941 OBC |
1266 | /* clean up remote vdev entries */ |
1267 | list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) { | |
1268 | __rproc_free_vrings(rvdev, RVDEV_NUM_VRINGS); | |
1269 | list_del(&rvdev->node); | |
1270 | } | |
1271 | ||
1272 | /* | |
1273 | * At this point no one holds a reference to rproc anymore, | |
1274 | * so we can directly unroll rproc_alloc() | |
1275 | */ | |
1276 | rproc_free(rproc); | |
400e64df OBC |
1277 | } |
1278 | ||
1279 | /* will be called when an rproc is added to the rprocs klist */ | |
1280 | static void klist_rproc_get(struct klist_node *n) | |
1281 | { | |
1282 | struct rproc *rproc = container_of(n, struct rproc, node); | |
1283 | ||
1284 | kref_get(&rproc->refcount); | |
1285 | } | |
1286 | ||
1287 | /* will be called when an rproc is removed from the rprocs klist */ | |
1288 | static void klist_rproc_put(struct klist_node *n) | |
1289 | { | |
1290 | struct rproc *rproc = container_of(n, struct rproc, node); | |
1291 | ||
1292 | kref_put(&rproc->refcount, rproc_release); | |
1293 | } | |
1294 | ||
1295 | static struct rproc *next_rproc(struct klist_iter *i) | |
1296 | { | |
1297 | struct klist_node *n; | |
1298 | ||
1299 | n = klist_next(i); | |
1300 | if (!n) | |
1301 | return NULL; | |
1302 | ||
1303 | return container_of(n, struct rproc, node); | |
1304 | } | |
1305 | ||
1306 | /** | |
1307 | * rproc_get_by_name() - find a remote processor by name and boot it | |
1308 | * @name: name of the remote processor | |
1309 | * | |
1310 | * Finds an rproc handle using the remote processor's name, and then | |
1311 | * boot it. If it's already powered on, then just immediately return | |
1312 | * (successfully). | |
1313 | * | |
1314 | * Returns the rproc handle on success, and NULL on failure. | |
1315 | * | |
1316 | * This function increments the remote processor's refcount, so always | |
1317 | * use rproc_put() to decrement it back once rproc isn't needed anymore. | |
1318 | * | |
1319 | * Note: currently this function (and its counterpart rproc_put()) are not | |
7a186941 | 1320 | * being used. We need to scrutinize the use cases |
400e64df OBC |
1321 | * that still need them, and see if we can migrate them to use the non |
1322 | * name-based boot/shutdown interface. | |
1323 | */ | |
1324 | struct rproc *rproc_get_by_name(const char *name) | |
1325 | { | |
1326 | struct rproc *rproc; | |
1327 | struct klist_iter i; | |
1328 | int ret; | |
1329 | ||
1330 | /* find the remote processor, and upref its refcount */ | |
1331 | klist_iter_init(&rprocs, &i); | |
1332 | while ((rproc = next_rproc(&i)) != NULL) | |
1333 | if (!strcmp(rproc->name, name)) { | |
1334 | kref_get(&rproc->refcount); | |
1335 | break; | |
1336 | } | |
1337 | klist_iter_exit(&i); | |
1338 | ||
1339 | /* can't find this rproc ? */ | |
1340 | if (!rproc) { | |
1341 | pr_err("can't find remote processor %s\n", name); | |
1342 | return NULL; | |
1343 | } | |
1344 | ||
1345 | ret = rproc_boot(rproc); | |
1346 | if (ret < 0) { | |
1347 | kref_put(&rproc->refcount, rproc_release); | |
1348 | return NULL; | |
1349 | } | |
1350 | ||
1351 | return rproc; | |
1352 | } | |
1353 | EXPORT_SYMBOL(rproc_get_by_name); | |
1354 | ||
1355 | /** | |
1356 | * rproc_put() - decrement the refcount of a remote processor, and shut it down | |
1357 | * @rproc: the remote processor | |
1358 | * | |
1359 | * This function tries to shutdown @rproc, and it then decrements its | |
1360 | * refcount. | |
1361 | * | |
1362 | * After this function returns, @rproc may _not_ be used anymore, and its | |
1363 | * handle should be considered invalid. | |
1364 | * | |
1365 | * This function should be called _iff_ the @rproc handle was grabbed by | |
1366 | * calling rproc_get_by_name(). | |
1367 | */ | |
1368 | void rproc_put(struct rproc *rproc) | |
1369 | { | |
1370 | /* try to power off the remote processor */ | |
1371 | rproc_shutdown(rproc); | |
1372 | ||
1373 | /* downref rproc's refcount */ | |
1374 | kref_put(&rproc->refcount, rproc_release); | |
1375 | } | |
1376 | EXPORT_SYMBOL(rproc_put); | |
1377 | ||
1378 | /** | |
1379 | * rproc_register() - register a remote processor | |
1380 | * @rproc: the remote processor handle to register | |
1381 | * | |
1382 | * Registers @rproc with the remoteproc framework, after it has been | |
1383 | * allocated with rproc_alloc(). | |
1384 | * | |
1385 | * This is called by the platform-specific rproc implementation, whenever | |
1386 | * a new remote processor device is probed. | |
1387 | * | |
1388 | * Returns 0 on success and an appropriate error code otherwise. | |
1389 | * | |
1390 | * Note: this function initiates an asynchronous firmware loading | |
1391 | * context, which will look for virtio devices supported by the rproc's | |
1392 | * firmware. | |
1393 | * | |
1394 | * If found, those virtio devices will be created and added, so as a result | |
7a186941 | 1395 | * of registering this remote processor, additional virtio drivers might be |
400e64df | 1396 | * probed. |
400e64df OBC |
1397 | */ |
1398 | int rproc_register(struct rproc *rproc) | |
1399 | { | |
1400 | struct device *dev = rproc->dev; | |
1401 | int ret = 0; | |
1402 | ||
1403 | /* expose to rproc_get_by_name users */ | |
1404 | klist_add_tail(&rproc->node, &rprocs); | |
1405 | ||
1406 | dev_info(rproc->dev, "%s is available\n", rproc->name); | |
1407 | ||
489d129a OBC |
1408 | dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n"); |
1409 | dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n"); | |
1410 | ||
400e64df OBC |
1411 | /* create debugfs entries */ |
1412 | rproc_create_debug_dir(rproc); | |
1413 | ||
1414 | /* rproc_unregister() calls must wait until async loader completes */ | |
1415 | init_completion(&rproc->firmware_loading_complete); | |
1416 | ||
1417 | /* | |
1418 | * We must retrieve early virtio configuration info from | |
7a186941 | 1419 | * the firmware (e.g. whether to register a virtio device, |
400e64df OBC |
1420 | * what virtio features does it support, ...). |
1421 | * | |
1422 | * We're initiating an asynchronous firmware loading, so we can | |
1423 | * be built-in kernel code, without hanging the boot process. | |
1424 | */ | |
1425 | ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, | |
1426 | rproc->firmware, dev, GFP_KERNEL, | |
1427 | rproc, rproc_fw_config_virtio); | |
1428 | if (ret < 0) { | |
1429 | dev_err(dev, "request_firmware_nowait failed: %d\n", ret); | |
1430 | complete_all(&rproc->firmware_loading_complete); | |
1431 | klist_remove(&rproc->node); | |
1432 | } | |
1433 | ||
1434 | return ret; | |
1435 | } | |
1436 | EXPORT_SYMBOL(rproc_register); | |
1437 | ||
1438 | /** | |
1439 | * rproc_alloc() - allocate a remote processor handle | |
1440 | * @dev: the underlying device | |
1441 | * @name: name of this remote processor | |
1442 | * @ops: platform-specific handlers (mainly start/stop) | |
1443 | * @firmware: name of firmware file to load | |
1444 | * @len: length of private data needed by the rproc driver (in bytes) | |
1445 | * | |
1446 | * Allocates a new remote processor handle, but does not register | |
1447 | * it yet. | |
1448 | * | |
1449 | * This function should be used by rproc implementations during initialization | |
1450 | * of the remote processor. | |
1451 | * | |
1452 | * After creating an rproc handle using this function, and when ready, | |
1453 | * implementations should then call rproc_register() to complete | |
1454 | * the registration of the remote processor. | |
1455 | * | |
1456 | * On success the new rproc is returned, and on failure, NULL. | |
1457 | * | |
1458 | * Note: _never_ directly deallocate @rproc, even if it was not registered | |
1459 | * yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free(). | |
1460 | */ | |
1461 | struct rproc *rproc_alloc(struct device *dev, const char *name, | |
1462 | const struct rproc_ops *ops, | |
1463 | const char *firmware, int len) | |
1464 | { | |
1465 | struct rproc *rproc; | |
1466 | ||
1467 | if (!dev || !name || !ops) | |
1468 | return NULL; | |
1469 | ||
1470 | rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); | |
1471 | if (!rproc) { | |
1472 | dev_err(dev, "%s: kzalloc failed\n", __func__); | |
1473 | return NULL; | |
1474 | } | |
1475 | ||
1476 | rproc->dev = dev; | |
1477 | rproc->name = name; | |
1478 | rproc->ops = ops; | |
1479 | rproc->firmware = firmware; | |
1480 | rproc->priv = &rproc[1]; | |
1481 | ||
1482 | atomic_set(&rproc->power, 0); | |
1483 | ||
1484 | kref_init(&rproc->refcount); | |
1485 | ||
1486 | mutex_init(&rproc->lock); | |
1487 | ||
7a186941 OBC |
1488 | idr_init(&rproc->notifyids); |
1489 | ||
400e64df OBC |
1490 | INIT_LIST_HEAD(&rproc->carveouts); |
1491 | INIT_LIST_HEAD(&rproc->mappings); | |
1492 | INIT_LIST_HEAD(&rproc->traces); | |
7a186941 | 1493 | INIT_LIST_HEAD(&rproc->rvdevs); |
400e64df OBC |
1494 | |
1495 | rproc->state = RPROC_OFFLINE; | |
1496 | ||
1497 | return rproc; | |
1498 | } | |
1499 | EXPORT_SYMBOL(rproc_alloc); | |
1500 | ||
1501 | /** | |
1502 | * rproc_free() - free an rproc handle that was allocated by rproc_alloc | |
1503 | * @rproc: the remote processor handle | |
1504 | * | |
1505 | * This function should _only_ be used if @rproc was only allocated, | |
1506 | * but not registered yet. | |
1507 | * | |
1508 | * If @rproc was already successfully registered (by calling rproc_register()), | |
1509 | * then use rproc_unregister() instead. | |
1510 | */ | |
1511 | void rproc_free(struct rproc *rproc) | |
1512 | { | |
7a186941 OBC |
1513 | idr_remove_all(&rproc->notifyids); |
1514 | idr_destroy(&rproc->notifyids); | |
1515 | ||
400e64df OBC |
1516 | kfree(rproc); |
1517 | } | |
1518 | EXPORT_SYMBOL(rproc_free); | |
1519 | ||
1520 | /** | |
1521 | * rproc_unregister() - unregister a remote processor | |
1522 | * @rproc: rproc handle to unregister | |
1523 | * | |
1524 | * Unregisters a remote processor, and decrements its refcount. | |
1525 | * If its refcount drops to zero, then @rproc will be freed. If not, | |
1526 | * it will be freed later once the last reference is dropped. | |
1527 | * | |
1528 | * This function should be called when the platform specific rproc | |
1529 | * implementation decides to remove the rproc device. it should | |
1530 | * _only_ be called if a previous invocation of rproc_register() | |
1531 | * has completed successfully. | |
1532 | * | |
1533 | * After rproc_unregister() returns, @rproc is _not_ valid anymore and | |
1534 | * it shouldn't be used. More specifically, don't call rproc_free() | |
1535 | * or try to directly free @rproc after rproc_unregister() returns; | |
1536 | * none of these are needed, and calling them is a bug. | |
1537 | * | |
1538 | * Returns 0 on success and -EINVAL if @rproc isn't valid. | |
1539 | */ | |
1540 | int rproc_unregister(struct rproc *rproc) | |
1541 | { | |
7a186941 OBC |
1542 | struct rproc_vdev *rvdev; |
1543 | ||
400e64df OBC |
1544 | if (!rproc) |
1545 | return -EINVAL; | |
1546 | ||
1547 | /* if rproc is just being registered, wait */ | |
1548 | wait_for_completion(&rproc->firmware_loading_complete); | |
1549 | ||
7a186941 OBC |
1550 | /* clean up remote vdev entries */ |
1551 | list_for_each_entry(rvdev, &rproc->rvdevs, node) | |
1552 | rproc_remove_virtio_dev(rvdev); | |
400e64df | 1553 | |
7a186941 OBC |
1554 | /* the rproc is downref'ed as soon as it's removed from the klist */ |
1555 | klist_del(&rproc->node); | |
400e64df | 1556 | |
7a186941 | 1557 | /* the rproc will only be released after its refcount drops to zero */ |
400e64df OBC |
1558 | kref_put(&rproc->refcount, rproc_release); |
1559 | ||
1560 | return 0; | |
1561 | } | |
1562 | EXPORT_SYMBOL(rproc_unregister); | |
1563 | ||
1564 | static int __init remoteproc_init(void) | |
1565 | { | |
1566 | rproc_init_debugfs(); | |
1567 | return 0; | |
1568 | } | |
1569 | module_init(remoteproc_init); | |
1570 | ||
1571 | static void __exit remoteproc_exit(void) | |
1572 | { | |
1573 | rproc_exit_debugfs(); | |
1574 | } | |
1575 | module_exit(remoteproc_exit); | |
1576 | ||
1577 | MODULE_LICENSE("GPL v2"); | |
1578 | MODULE_DESCRIPTION("Generic Remote Processor Framework"); |