2 * Remote Processor Framework
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
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>
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.
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.
25 #define pr_fmt(fmt) "%s: " fmt, __func__
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>
42 #include <asm/byteorder.h>
44 #include "remoteproc_internal.h"
46 static void klist_rproc_get(struct klist_node
*n
);
47 static void klist_rproc_put(struct klist_node
*n
);
50 * klist of the available remote processors.
52 * We need this in order to support name-based lookups (needed by the
53 * rproc_get_by_name()).
55 * That said, we don't use rproc_get_by_name() anymore within the rpmsg
56 * framework. The use cases that do require its existence should be
57 * scrutinized, and hopefully migrated to rproc_boot() using device-based
60 * If/when this materializes, we could drop the klist (and the by_name
63 static DEFINE_KLIST(rprocs
, klist_rproc_get
, klist_rproc_put
);
65 typedef int (*rproc_handle_resources_t
)(struct rproc
*rproc
,
66 struct fw_resource
*rsc
, int len
);
69 * This is the IOMMU fault handler we register with the IOMMU API
70 * (when relevant; not all remote processors access memory through
73 * IOMMU core will invoke this handler whenever the remote processor
74 * will try to access an unmapped device address.
76 * Currently this is mostly a stub, but it will be later used to trigger
77 * the recovery of the remote processor.
79 static int rproc_iommu_fault(struct iommu_domain
*domain
, struct device
*dev
,
80 unsigned long iova
, int flags
)
82 dev_err(dev
, "iommu fault: da 0x%lx flags 0x%x\n", iova
, flags
);
85 * Let the iommu core know we're not really handling this fault;
86 * we just plan to use this as a recovery trigger.
91 static int rproc_enable_iommu(struct rproc
*rproc
)
93 struct iommu_domain
*domain
;
94 struct device
*dev
= rproc
->dev
;
98 * We currently use iommu_present() to decide if an IOMMU
101 * This works for simple cases, but will easily fail with
102 * platforms that do have an IOMMU, but not for this specific
105 * This will be easily solved by introducing hw capabilities
106 * that will be set by the remoteproc driver.
108 if (!iommu_present(dev
->bus
)) {
109 dev_dbg(dev
, "iommu not found\n");
113 domain
= iommu_domain_alloc(dev
->bus
);
115 dev_err(dev
, "can't alloc iommu domain\n");
119 iommu_set_fault_handler(domain
, rproc_iommu_fault
);
121 ret
= iommu_attach_device(domain
, dev
);
123 dev_err(dev
, "can't attach iommu device: %d\n", ret
);
127 rproc
->domain
= domain
;
132 iommu_domain_free(domain
);
136 static void rproc_disable_iommu(struct rproc
*rproc
)
138 struct iommu_domain
*domain
= rproc
->domain
;
139 struct device
*dev
= rproc
->dev
;
144 iommu_detach_device(domain
, dev
);
145 iommu_domain_free(domain
);
151 * Some remote processors will ask us to allocate them physically contiguous
152 * memory regions (which we call "carveouts"), and map them to specific
153 * device addresses (which are hardcoded in the firmware).
155 * They may then ask us to copy objects into specific device addresses (e.g.
156 * code/data sections) or expose us certain symbols in other device address
157 * (e.g. their trace buffer).
159 * This function is an internal helper with which we can go over the allocated
160 * carveouts and translate specific device address to kernel virtual addresses
161 * so we can access the referenced memory.
163 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
164 * but only on kernel direct mapped RAM memory. Instead, we're just using
165 * here the output of the DMA API, which should be more correct.
167 static void *rproc_da_to_va(struct rproc
*rproc
, u64 da
, int len
)
169 struct rproc_mem_entry
*carveout
;
172 list_for_each_entry(carveout
, &rproc
->carveouts
, node
) {
173 int offset
= da
- carveout
->da
;
175 /* try next carveout if da is too small */
179 /* try next carveout if da is too large */
180 if (offset
+ len
> carveout
->len
)
183 ptr
= carveout
->va
+ offset
;
192 * rproc_load_segments() - load firmware segments to memory
193 * @rproc: remote processor which will be booted using these fw segments
194 * @elf_data: the content of the ELF firmware image
195 * @len: firmware size (in bytes)
197 * This function loads the firmware segments to memory, where the remote
198 * processor expects them.
200 * Some remote processors will expect their code and data to be placed
201 * in specific device addresses, and can't have them dynamically assigned.
203 * We currently support only those kind of remote processors, and expect
204 * the program header's paddr member to contain those addresses. We then go
205 * through the physically contiguous "carveout" memory regions which we
206 * allocated (and mapped) earlier on behalf of the remote processor,
207 * and "translate" device address to kernel addresses, so we can copy the
208 * segments where they are expected.
210 * Currently we only support remote processors that required carveout
211 * allocations and got them mapped onto their iommus. Some processors
212 * might be different: they might not have iommus, and would prefer to
213 * directly allocate memory for every segment/resource. This is not yet
217 rproc_load_segments(struct rproc
*rproc
, const u8
*elf_data
, size_t len
)
219 struct device
*dev
= rproc
->dev
;
220 struct elf32_hdr
*ehdr
;
221 struct elf32_phdr
*phdr
;
224 ehdr
= (struct elf32_hdr
*)elf_data
;
225 phdr
= (struct elf32_phdr
*)(elf_data
+ ehdr
->e_phoff
);
227 /* go through the available ELF segments */
228 for (i
= 0; i
< ehdr
->e_phnum
; i
++, phdr
++) {
229 u32 da
= phdr
->p_paddr
;
230 u32 memsz
= phdr
->p_memsz
;
231 u32 filesz
= phdr
->p_filesz
;
232 u32 offset
= phdr
->p_offset
;
235 if (phdr
->p_type
!= PT_LOAD
)
238 dev_dbg(dev
, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
239 phdr
->p_type
, da
, memsz
, filesz
);
241 if (filesz
> memsz
) {
242 dev_err(dev
, "bad phdr filesz 0x%x memsz 0x%x\n",
248 if (offset
+ filesz
> len
) {
249 dev_err(dev
, "truncated fw: need 0x%x avail 0x%x\n",
250 offset
+ filesz
, len
);
255 /* grab the kernel address for this device address */
256 ptr
= rproc_da_to_va(rproc
, da
, memsz
);
258 dev_err(dev
, "bad phdr da 0x%x mem 0x%x\n", da
, memsz
);
263 /* put the segment where the remote processor expects it */
265 memcpy(ptr
, elf_data
+ phdr
->p_offset
, filesz
);
268 * Zero out remaining memory for this segment.
270 * This isn't strictly required since dma_alloc_coherent already
271 * did this for us. albeit harmless, we may consider removing
275 memset(ptr
+ filesz
, 0, memsz
- filesz
);
282 * rproc_handle_virtio_hdr() - handle a virtio header resource
283 * @rproc: the remote processor
284 * @rsc: the resource descriptor
286 * The existence of this virtio hdr resource entry means that the firmware
287 * of this @rproc supports this virtio device.
289 * Currently we support only a single virtio device of type VIRTIO_ID_RPMSG,
290 * but the plan is to remove this limitation and support any number
291 * of virtio devices (and of any type). We'll also add support for dynamically
292 * adding (and removing) virtio devices over the rpmsg bus, but small
293 * firmwares that doesn't want to get involved with rpmsg will be able
294 * to simple use the resource table for this.
296 * At this point this virtio header entry is rather simple: it just
297 * announces the virtio device id and the supported virtio device features.
298 * The plan though is to extend this to include the vring information and
299 * the virtio config space, too (but first, some resource table overhaul
300 * is needed: move from fixed-sized to variable-length TLV entries).
302 * For now, the 'flags' member of the resource entry contains the virtio
303 * device id, the 'da' member contains the device features, and 'pa' is
304 * where we need to store the guest features once negotiation completes.
305 * As usual, the 'id' member of this resource contains the index of this
306 * resource type (i.e. is this the first virtio hdr entry, the 2nd, ...).
308 * Returns 0 on success, or an appropriate error code otherwise
310 static int rproc_handle_virtio_hdr(struct rproc
*rproc
, struct fw_resource
*rsc
)
312 struct rproc_vdev
*rvdev
;
314 /* we only support VIRTIO_ID_RPMSG devices for now */
315 if (rsc
->flags
!= VIRTIO_ID_RPMSG
) {
316 dev_warn(rproc
->dev
, "unsupported vdev: %d\n", rsc
->flags
);
320 /* we only support a single vdev per rproc for now */
321 if (rsc
->id
|| rproc
->rvdev
) {
322 dev_warn(rproc
->dev
, "redundant vdev entry: %s\n", rsc
->name
);
326 rvdev
= kzalloc(sizeof(struct rproc_vdev
), GFP_KERNEL
);
330 /* remember the device features */
331 rvdev
->dfeatures
= rsc
->da
;
333 rproc
->rvdev
= rvdev
;
334 rvdev
->rproc
= rproc
;
340 * rproc_handle_vring() - handle a vring fw resource
341 * @rproc: the remote processor
342 * @rsc: the vring resource descriptor
344 * This resource entry requires allocation of non-cacheable memory
345 * for a virtio vring. Currently we only support two vrings per remote
346 * processor, required for the virtio rpmsg device.
348 * The 'len' member of @rsc should contain the number of buffers this vring
349 * support and 'da' should either contain the device address where
350 * the remote processor is expecting the vring, or indicate that
351 * dynamically allocation of the vring's device address is supported.
353 * Note: 'da' is currently not handled. This will be revised when the generic
354 * iommu-based DMA API will arrive, or a dynanic & non-iommu use case show
355 * up. Meanwhile, statically-addressed iommu-based images should use
356 * RSC_DEVMEM resource entries to map their require 'da' to the physical
357 * address of their base CMA region.
359 * Returns 0 on success, or an appropriate error code otherwise
361 static int rproc_handle_vring(struct rproc
*rproc
, struct fw_resource
*rsc
)
363 struct device
*dev
= rproc
->dev
;
364 struct rproc_vdev
*rvdev
= rproc
->rvdev
;
366 int size
, id
= rsc
->id
;
369 /* no vdev is in place ? */
371 dev_err(dev
, "vring requested without a virtio dev entry\n");
375 /* the firmware must provide the expected queue size */
377 dev_err(dev
, "missing expected queue size\n");
381 /* we currently support two vrings per rproc (for rx and tx) */
382 if (id
>= ARRAY_SIZE(rvdev
->vring
)) {
383 dev_err(dev
, "%s: invalid vring id %d\n", rsc
->name
, id
);
387 /* have we already allocated this vring id ? */
388 if (rvdev
->vring
[id
].len
) {
389 dev_err(dev
, "%s: duplicated id %d\n", rsc
->name
, id
);
393 /* actual size of vring (in bytes) */
394 size
= PAGE_ALIGN(vring_size(rsc
->len
, AMP_VRING_ALIGN
));
397 * Allocate non-cacheable memory for the vring. In the future
398 * this call will also configure the IOMMU for us
400 va
= dma_alloc_coherent(dev
, size
, &dma
, GFP_KERNEL
);
402 dev_err(dev
, "dma_alloc_coherent failed\n");
406 dev_dbg(dev
, "vring%d: va %p dma %x qsz %d ring size %x\n", id
, va
,
407 dma
, rsc
->len
, size
);
409 rvdev
->vring
[id
].len
= rsc
->len
;
410 rvdev
->vring
[id
].va
= va
;
411 rvdev
->vring
[id
].dma
= dma
;
417 * rproc_handle_trace() - handle a shared trace buffer resource
418 * @rproc: the remote processor
419 * @rsc: the trace resource descriptor
421 * In case the remote processor dumps trace logs into memory,
422 * export it via debugfs.
424 * Currently, the 'da' member of @rsc should contain the device address
425 * where the remote processor is dumping the traces. Later we could also
426 * support dynamically allocating this address using the generic
427 * DMA API (but currently there isn't a use case for that).
429 * Returns 0 on success, or an appropriate error code otherwise
431 static int rproc_handle_trace(struct rproc
*rproc
, struct fw_resource
*rsc
)
433 struct rproc_mem_entry
*trace
;
434 struct device
*dev
= rproc
->dev
;
438 /* what's the kernel address of this resource ? */
439 ptr
= rproc_da_to_va(rproc
, rsc
->da
, rsc
->len
);
441 dev_err(dev
, "erroneous trace resource entry\n");
445 trace
= kzalloc(sizeof(*trace
), GFP_KERNEL
);
447 dev_err(dev
, "kzalloc trace failed\n");
451 /* set the trace buffer dma properties */
452 trace
->len
= rsc
->len
;
455 /* make sure snprintf always null terminates, even if truncating */
456 snprintf(name
, sizeof(name
), "trace%d", rproc
->num_traces
);
458 /* create the debugfs entry */
459 trace
->priv
= rproc_create_trace_file(name
, rproc
, trace
);
466 list_add_tail(&trace
->node
, &rproc
->traces
);
470 dev_dbg(dev
, "%s added: va %p, da 0x%llx, len 0x%x\n", name
, ptr
,
477 * rproc_handle_devmem() - handle devmem resource entry
478 * @rproc: remote processor handle
479 * @rsc: the devmem resource entry
481 * Remote processors commonly need to access certain on-chip peripherals.
483 * Some of these remote processors access memory via an iommu device,
484 * and might require us to configure their iommu before they can access
485 * the on-chip peripherals they need.
487 * This resource entry is a request to map such a peripheral device.
489 * These devmem entries will contain the physical address of the device in
490 * the 'pa' member. If a specific device address is expected, then 'da' will
491 * contain it (currently this is the only use case supported). 'len' will
492 * contain the size of the physical region we need to map.
494 * Currently we just "trust" those devmem entries to contain valid physical
495 * addresses, but this is going to change: we want the implementations to
496 * tell us ranges of physical addresses the firmware is allowed to request,
497 * and not allow firmwares to request access to physical addresses that
498 * are outside those ranges.
500 static int rproc_handle_devmem(struct rproc
*rproc
, struct fw_resource
*rsc
)
502 struct rproc_mem_entry
*mapping
;
505 /* no point in handling this resource without a valid iommu domain */
509 mapping
= kzalloc(sizeof(*mapping
), GFP_KERNEL
);
511 dev_err(rproc
->dev
, "kzalloc mapping failed\n");
515 ret
= iommu_map(rproc
->domain
, rsc
->da
, rsc
->pa
, rsc
->len
, rsc
->flags
);
517 dev_err(rproc
->dev
, "failed to map devmem: %d\n", ret
);
522 * We'll need this info later when we'll want to unmap everything
523 * (e.g. on shutdown).
525 * We can't trust the remote processor not to change the resource
526 * table, so we must maintain this info independently.
528 mapping
->da
= rsc
->da
;
529 mapping
->len
= rsc
->len
;
530 list_add_tail(&mapping
->node
, &rproc
->mappings
);
532 dev_dbg(rproc
->dev
, "mapped devmem pa 0x%llx, da 0x%llx, len 0x%x\n",
533 rsc
->pa
, rsc
->da
, rsc
->len
);
543 * rproc_handle_carveout() - handle phys contig memory allocation requests
544 * @rproc: rproc handle
545 * @rsc: the resource entry
547 * This function will handle firmware requests for allocation of physically
548 * contiguous memory regions.
550 * These request entries should come first in the firmware's resource table,
551 * as other firmware entries might request placing other data objects inside
552 * these memory regions (e.g. data/code segments, trace resource entries, ...).
554 * Allocating memory this way helps utilizing the reserved physical memory
555 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
556 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
557 * pressure is important; it may have a substantial impact on performance.
559 static int rproc_handle_carveout(struct rproc
*rproc
, struct fw_resource
*rsc
)
561 struct rproc_mem_entry
*carveout
, *mapping
;
562 struct device
*dev
= rproc
->dev
;
567 mapping
= kzalloc(sizeof(*mapping
), GFP_KERNEL
);
569 dev_err(dev
, "kzalloc mapping failed\n");
573 carveout
= kzalloc(sizeof(*carveout
), GFP_KERNEL
);
575 dev_err(dev
, "kzalloc carveout failed\n");
580 va
= dma_alloc_coherent(dev
, rsc
->len
, &dma
, GFP_KERNEL
);
582 dev_err(dev
, "failed to dma alloc carveout: %d\n", rsc
->len
);
587 dev_dbg(dev
, "carveout va %p, dma %x, len 0x%x\n", va
, dma
, rsc
->len
);
590 * Ok, this is non-standard.
592 * Sometimes we can't rely on the generic iommu-based DMA API
593 * to dynamically allocate the device address and then set the IOMMU
594 * tables accordingly, because some remote processors might
595 * _require_ us to use hard coded device addresses that their
596 * firmware was compiled with.
598 * In this case, we must use the IOMMU API directly and map
599 * the memory to the device address as expected by the remote
602 * Obviously such remote processor devices should not be configured
603 * to use the iommu-based DMA API: we expect 'dma' to contain the
604 * physical address in this case.
607 ret
= iommu_map(rproc
->domain
, rsc
->da
, dma
, rsc
->len
,
610 dev_err(dev
, "iommu_map failed: %d\n", ret
);
615 * We'll need this info later when we'll want to unmap
616 * everything (e.g. on shutdown).
618 * We can't trust the remote processor not to change the
619 * resource table, so we must maintain this info independently.
621 mapping
->da
= rsc
->da
;
622 mapping
->len
= rsc
->len
;
623 list_add_tail(&mapping
->node
, &rproc
->mappings
);
625 dev_dbg(dev
, "carveout mapped 0x%llx to 0x%x\n", rsc
->da
, dma
);
628 * Some remote processors might need to know the pa
629 * even though they are behind an IOMMU. E.g., OMAP4's
630 * remote M3 processor needs this so it can control
631 * on-chip hardware accelerators that are not behind
632 * the IOMMU, and therefor must know the pa.
634 * Generally we don't want to expose physical addresses
635 * if we don't have to (remote processors are generally
636 * _not_ trusted), so we might want to do this only for
637 * remote processor that _must_ have this (e.g. OMAP4's
638 * dual M3 subsystem).
644 carveout
->len
= rsc
->len
;
646 carveout
->da
= rsc
->da
;
648 list_add_tail(&carveout
->node
, &rproc
->carveouts
);
653 dma_free_coherent(dev
, rsc
->len
, va
, dma
);
661 /* handle firmware resource entries before booting the remote processor */
663 rproc_handle_boot_rsc(struct rproc
*rproc
, struct fw_resource
*rsc
, int len
)
665 struct device
*dev
= rproc
->dev
;
668 while (len
>= sizeof(*rsc
)) {
669 dev_dbg(dev
, "rsc: type %d, da 0x%llx, pa 0x%llx, len 0x%x, "
670 "id %d, name %s, flags %x\n", rsc
->type
, rsc
->da
,
671 rsc
->pa
, rsc
->len
, rsc
->id
, rsc
->name
, rsc
->flags
);
675 ret
= rproc_handle_carveout(rproc
, rsc
);
678 ret
= rproc_handle_devmem(rproc
, rsc
);
681 ret
= rproc_handle_trace(rproc
, rsc
);
684 ret
= rproc_handle_vring(rproc
, rsc
);
687 /* this one is handled early upon registration */
690 dev_warn(dev
, "unsupported resource %d\n", rsc
->type
);
704 /* handle firmware resource entries while registering the remote processor */
706 rproc_handle_virtio_rsc(struct rproc
*rproc
, struct fw_resource
*rsc
, int len
)
708 struct device
*dev
= rproc
->dev
;
711 for (; len
>= sizeof(*rsc
); rsc
++, len
-= sizeof(*rsc
))
712 if (rsc
->type
== RSC_VIRTIO_DEV
) {
713 dev_dbg(dev
, "found vdev %d/%s features %llx\n",
714 rsc
->flags
, rsc
->name
, rsc
->da
);
715 ret
= rproc_handle_virtio_hdr(rproc
, rsc
);
723 * rproc_handle_resources() - find and handle the resource table
724 * @rproc: the rproc handle
725 * @elf_data: the content of the ELF firmware image
726 * @len: firmware size (in bytes)
727 * @handler: function that should be used to handle the resource table
729 * This function finds the resource table inside the remote processor's
730 * firmware, and invoke a user-supplied handler with it (we have two
731 * possible handlers: one is invoked upon registration of @rproc,
732 * in order to register the supported virito devices, and the other is
733 * invoked when @rproc is actually booted).
735 * Currently this function fails if a resource table doesn't exist.
736 * This restriction will be removed when we'll start supporting remote
737 * processors that don't need a resource table.
739 static int rproc_handle_resources(struct rproc
*rproc
, const u8
*elf_data
,
740 size_t len
, rproc_handle_resources_t handler
)
743 struct elf32_hdr
*ehdr
;
744 struct elf32_shdr
*shdr
;
745 const char *name_table
;
746 int i
, ret
= -EINVAL
;
748 ehdr
= (struct elf32_hdr
*)elf_data
;
749 shdr
= (struct elf32_shdr
*)(elf_data
+ ehdr
->e_shoff
);
750 name_table
= elf_data
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
752 /* look for the resource table and handle it */
753 for (i
= 0; i
< ehdr
->e_shnum
; i
++, shdr
++) {
754 if (!strcmp(name_table
+ shdr
->sh_name
, ".resource_table")) {
755 struct fw_resource
*table
= (struct fw_resource
*)
756 (elf_data
+ shdr
->sh_offset
);
758 if (shdr
->sh_offset
+ shdr
->sh_size
> len
) {
760 "truncated fw: need 0x%x avail 0x%x\n",
761 shdr
->sh_offset
+ shdr
->sh_size
, len
);
765 ret
= handler(rproc
, table
, shdr
->sh_size
);
775 * rproc_resource_cleanup() - clean up and free all acquired resources
776 * @rproc: rproc handle
778 * This function will free all resources acquired for @rproc, and it
779 * is called when @rproc shuts down, or just failed booting.
781 static void rproc_resource_cleanup(struct rproc
*rproc
)
783 struct rproc_mem_entry
*entry
, *tmp
;
784 struct device
*dev
= rproc
->dev
;
785 struct rproc_vdev
*rvdev
= rproc
->rvdev
;
788 /* clean up debugfs trace entries */
789 list_for_each_entry_safe(entry
, tmp
, &rproc
->traces
, node
) {
790 rproc_remove_trace_file(entry
->priv
);
792 list_del(&entry
->node
);
796 /* free the coherent memory allocated for the vrings */
797 for (i
= 0; rvdev
&& i
< ARRAY_SIZE(rvdev
->vring
); i
++) {
798 int qsz
= rvdev
->vring
[i
].len
;
799 void *va
= rvdev
->vring
[i
].va
;
800 int dma
= rvdev
->vring
[i
].dma
;
802 /* virtqueue size is expressed in number of buffers supported */
804 /* how many bytes does this vring really occupy ? */
805 int size
= PAGE_ALIGN(vring_size(qsz
, AMP_VRING_ALIGN
));
807 dma_free_coherent(rproc
->dev
, size
, va
, dma
);
809 rvdev
->vring
[i
].len
= 0;
813 /* clean up carveout allocations */
814 list_for_each_entry_safe(entry
, tmp
, &rproc
->carveouts
, node
) {
815 dma_free_coherent(dev
, entry
->len
, entry
->va
, entry
->dma
);
816 list_del(&entry
->node
);
820 /* clean up iommu mapping entries */
821 list_for_each_entry_safe(entry
, tmp
, &rproc
->mappings
, node
) {
824 unmapped
= iommu_unmap(rproc
->domain
, entry
->da
, entry
->len
);
825 if (unmapped
!= entry
->len
) {
826 /* nothing much to do besides complaining */
827 dev_err(dev
, "failed to unmap %u/%u\n", entry
->len
,
831 list_del(&entry
->node
);
836 /* make sure this fw image is sane */
837 static int rproc_fw_sanity_check(struct rproc
*rproc
, const struct firmware
*fw
)
839 const char *name
= rproc
->firmware
;
840 struct device
*dev
= rproc
->dev
;
841 struct elf32_hdr
*ehdr
;
844 dev_err(dev
, "failed to load %s\n", name
);
848 if (fw
->size
< sizeof(struct elf32_hdr
)) {
849 dev_err(dev
, "Image is too small\n");
853 ehdr
= (struct elf32_hdr
*)fw
->data
;
855 /* We assume the firmware has the same endianess as the host */
856 # ifdef __LITTLE_ENDIAN
857 if (ehdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
858 # else /* BIG ENDIAN */
859 if (ehdr
->e_ident
[EI_DATA
] != ELFDATA2MSB
) {
861 dev_err(dev
, "Unsupported firmware endianess\n");
865 if (fw
->size
< ehdr
->e_shoff
+ sizeof(struct elf32_shdr
)) {
866 dev_err(dev
, "Image is too small\n");
870 if (memcmp(ehdr
->e_ident
, ELFMAG
, SELFMAG
)) {
871 dev_err(dev
, "Image is corrupted (bad magic)\n");
875 if (ehdr
->e_phnum
== 0) {
876 dev_err(dev
, "No loadable segments\n");
880 if (ehdr
->e_phoff
> fw
->size
) {
881 dev_err(dev
, "Firmware size is too small\n");
889 * take a firmware and boot a remote processor with it.
891 static int rproc_fw_boot(struct rproc
*rproc
, const struct firmware
*fw
)
893 struct device
*dev
= rproc
->dev
;
894 const char *name
= rproc
->firmware
;
895 struct elf32_hdr
*ehdr
;
898 ret
= rproc_fw_sanity_check(rproc
, fw
);
902 ehdr
= (struct elf32_hdr
*)fw
->data
;
904 dev_info(dev
, "Booting fw image %s, size %d\n", name
, fw
->size
);
907 * if enabling an IOMMU isn't relevant for this rproc, this is
910 ret
= rproc_enable_iommu(rproc
);
912 dev_err(dev
, "can't enable iommu: %d\n", ret
);
917 * The ELF entry point is the rproc's boot addr (though this is not
918 * a configurable property of all remote processors: some will always
919 * boot at a specific hardcoded address).
921 rproc
->bootaddr
= ehdr
->e_entry
;
923 /* handle fw resources which are required to boot rproc */
924 ret
= rproc_handle_resources(rproc
, fw
->data
, fw
->size
,
925 rproc_handle_boot_rsc
);
927 dev_err(dev
, "Failed to process resources: %d\n", ret
);
931 /* load the ELF segments to memory */
932 ret
= rproc_load_segments(rproc
, fw
->data
, fw
->size
);
934 dev_err(dev
, "Failed to load program segments: %d\n", ret
);
938 /* power up the remote processor */
939 ret
= rproc
->ops
->start(rproc
);
941 dev_err(dev
, "can't start rproc %s: %d\n", rproc
->name
, ret
);
945 rproc
->state
= RPROC_RUNNING
;
947 dev_info(dev
, "remote processor %s is now up\n", rproc
->name
);
952 rproc_resource_cleanup(rproc
);
953 rproc_disable_iommu(rproc
);
958 * take a firmware and look for virtio devices to register.
960 * Note: this function is called asynchronously upon registration of the
961 * remote processor (so we must wait until it completes before we try
962 * to unregister the device. one other option is just to use kref here,
963 * that might be cleaner).
965 static void rproc_fw_config_virtio(const struct firmware
*fw
, void *context
)
967 struct rproc
*rproc
= context
;
968 struct device
*dev
= rproc
->dev
;
971 if (rproc_fw_sanity_check(rproc
, fw
) < 0)
974 /* does the fw supports any virtio devices ? */
975 ret
= rproc_handle_resources(rproc
, fw
->data
, fw
->size
,
976 rproc_handle_virtio_rsc
);
978 dev_info(dev
, "No fw virtio device was found\n");
982 /* add the virtio device (currently only rpmsg vdevs are supported) */
983 ret
= rproc_add_rpmsg_vdev(rproc
);
989 release_firmware(fw
);
990 /* allow rproc_unregister() contexts, if any, to proceed */
991 complete_all(&rproc
->firmware_loading_complete
);
995 * rproc_boot() - boot a remote processor
996 * @rproc: handle of a remote processor
998 * Boot a remote processor (i.e. load its firmware, power it on, ...).
1000 * If the remote processor is already powered on, this function immediately
1001 * returns (successfully).
1003 * Returns 0 on success, and an appropriate error value otherwise.
1005 int rproc_boot(struct rproc
*rproc
)
1007 const struct firmware
*firmware_p
;
1012 pr_err("invalid rproc handle\n");
1018 ret
= mutex_lock_interruptible(&rproc
->lock
);
1020 dev_err(dev
, "can't lock rproc %s: %d\n", rproc
->name
, ret
);
1024 /* loading a firmware is required */
1025 if (!rproc
->firmware
) {
1026 dev_err(dev
, "%s: no firmware to load\n", __func__
);
1031 /* prevent underlying implementation from being removed */
1032 if (!try_module_get(dev
->driver
->owner
)) {
1033 dev_err(dev
, "%s: can't get owner\n", __func__
);
1038 /* skip the boot process if rproc is already powered up */
1039 if (atomic_inc_return(&rproc
->power
) > 1) {
1044 dev_info(dev
, "powering up %s\n", rproc
->name
);
1047 ret
= request_firmware(&firmware_p
, rproc
->firmware
, dev
);
1049 dev_err(dev
, "request_firmware failed: %d\n", ret
);
1053 ret
= rproc_fw_boot(rproc
, firmware_p
);
1055 release_firmware(firmware_p
);
1059 module_put(dev
->driver
->owner
);
1060 atomic_dec(&rproc
->power
);
1063 mutex_unlock(&rproc
->lock
);
1066 EXPORT_SYMBOL(rproc_boot
);
1069 * rproc_shutdown() - power off the remote processor
1070 * @rproc: the remote processor
1072 * Power off a remote processor (previously booted with rproc_boot()).
1074 * In case @rproc is still being used by an additional user(s), then
1075 * this function will just decrement the power refcount and exit,
1076 * without really powering off the device.
1078 * Every call to rproc_boot() must (eventually) be accompanied by a call
1079 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1082 * - we're not decrementing the rproc's refcount, only the power refcount.
1083 * which means that the @rproc handle stays valid even after rproc_shutdown()
1084 * returns, and users can still use it with a subsequent rproc_boot(), if
1086 * - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
1087 * because rproc_shutdown() _does not_ decrement the refcount of @rproc.
1088 * To decrement the refcount of @rproc, use rproc_put() (but _only_ if
1089 * you acquired @rproc using rproc_get_by_name()).
1091 void rproc_shutdown(struct rproc
*rproc
)
1093 struct device
*dev
= rproc
->dev
;
1096 ret
= mutex_lock_interruptible(&rproc
->lock
);
1098 dev_err(dev
, "can't lock rproc %s: %d\n", rproc
->name
, ret
);
1102 /* if the remote proc is still needed, bail out */
1103 if (!atomic_dec_and_test(&rproc
->power
))
1106 /* power off the remote processor */
1107 ret
= rproc
->ops
->stop(rproc
);
1109 atomic_inc(&rproc
->power
);
1110 dev_err(dev
, "can't stop rproc: %d\n", ret
);
1114 /* clean up all acquired resources */
1115 rproc_resource_cleanup(rproc
);
1117 rproc_disable_iommu(rproc
);
1119 rproc
->state
= RPROC_OFFLINE
;
1121 dev_info(dev
, "stopped remote processor %s\n", rproc
->name
);
1124 mutex_unlock(&rproc
->lock
);
1126 module_put(dev
->driver
->owner
);
1128 EXPORT_SYMBOL(rproc_shutdown
);
1131 * rproc_release() - completely deletes the existence of a remote processor
1132 * @kref: the rproc's kref
1134 * This function should _never_ be called directly.
1136 * The only reasonable location to use it is as an argument when kref_put'ing
1137 * @rproc's refcount.
1139 * This way it will be called when no one holds a valid pointer to this @rproc
1140 * anymore (and obviously after it is removed from the rprocs klist).
1142 * Note: this function is not static because rproc_vdev_release() needs it when
1143 * it decrements @rproc's refcount.
1145 void rproc_release(struct kref
*kref
)
1147 struct rproc
*rproc
= container_of(kref
, struct rproc
, refcount
);
1149 dev_info(rproc
->dev
, "removing %s\n", rproc
->name
);
1151 rproc_delete_debug_dir(rproc
);
1153 /* at this point no one holds a reference to rproc anymore */
1157 /* will be called when an rproc is added to the rprocs klist */
1158 static void klist_rproc_get(struct klist_node
*n
)
1160 struct rproc
*rproc
= container_of(n
, struct rproc
, node
);
1162 kref_get(&rproc
->refcount
);
1165 /* will be called when an rproc is removed from the rprocs klist */
1166 static void klist_rproc_put(struct klist_node
*n
)
1168 struct rproc
*rproc
= container_of(n
, struct rproc
, node
);
1170 kref_put(&rproc
->refcount
, rproc_release
);
1173 static struct rproc
*next_rproc(struct klist_iter
*i
)
1175 struct klist_node
*n
;
1181 return container_of(n
, struct rproc
, node
);
1185 * rproc_get_by_name() - find a remote processor by name and boot it
1186 * @name: name of the remote processor
1188 * Finds an rproc handle using the remote processor's name, and then
1189 * boot it. If it's already powered on, then just immediately return
1192 * Returns the rproc handle on success, and NULL on failure.
1194 * This function increments the remote processor's refcount, so always
1195 * use rproc_put() to decrement it back once rproc isn't needed anymore.
1197 * Note: currently this function (and its counterpart rproc_put()) are not
1198 * used anymore by the rpmsg subsystem. We need to scrutinize the use cases
1199 * that still need them, and see if we can migrate them to use the non
1200 * name-based boot/shutdown interface.
1202 struct rproc
*rproc_get_by_name(const char *name
)
1204 struct rproc
*rproc
;
1205 struct klist_iter i
;
1208 /* find the remote processor, and upref its refcount */
1209 klist_iter_init(&rprocs
, &i
);
1210 while ((rproc
= next_rproc(&i
)) != NULL
)
1211 if (!strcmp(rproc
->name
, name
)) {
1212 kref_get(&rproc
->refcount
);
1215 klist_iter_exit(&i
);
1217 /* can't find this rproc ? */
1219 pr_err("can't find remote processor %s\n", name
);
1223 ret
= rproc_boot(rproc
);
1225 kref_put(&rproc
->refcount
, rproc_release
);
1231 EXPORT_SYMBOL(rproc_get_by_name
);
1234 * rproc_put() - decrement the refcount of a remote processor, and shut it down
1235 * @rproc: the remote processor
1237 * This function tries to shutdown @rproc, and it then decrements its
1240 * After this function returns, @rproc may _not_ be used anymore, and its
1241 * handle should be considered invalid.
1243 * This function should be called _iff_ the @rproc handle was grabbed by
1244 * calling rproc_get_by_name().
1246 void rproc_put(struct rproc
*rproc
)
1248 /* try to power off the remote processor */
1249 rproc_shutdown(rproc
);
1251 /* downref rproc's refcount */
1252 kref_put(&rproc
->refcount
, rproc_release
);
1254 EXPORT_SYMBOL(rproc_put
);
1257 * rproc_register() - register a remote processor
1258 * @rproc: the remote processor handle to register
1260 * Registers @rproc with the remoteproc framework, after it has been
1261 * allocated with rproc_alloc().
1263 * This is called by the platform-specific rproc implementation, whenever
1264 * a new remote processor device is probed.
1266 * Returns 0 on success and an appropriate error code otherwise.
1268 * Note: this function initiates an asynchronous firmware loading
1269 * context, which will look for virtio devices supported by the rproc's
1272 * If found, those virtio devices will be created and added, so as a result
1273 * of registering this remote processor, additional virtio drivers will be
1276 * Currently, though, we only support a single RPMSG virtio vdev per remote
1279 int rproc_register(struct rproc
*rproc
)
1281 struct device
*dev
= rproc
->dev
;
1284 /* expose to rproc_get_by_name users */
1285 klist_add_tail(&rproc
->node
, &rprocs
);
1287 dev_info(rproc
->dev
, "%s is available\n", rproc
->name
);
1289 dev_info(dev
, "Note: remoteproc is still under development and considered experimental.\n");
1290 dev_info(dev
, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1292 /* create debugfs entries */
1293 rproc_create_debug_dir(rproc
);
1295 /* rproc_unregister() calls must wait until async loader completes */
1296 init_completion(&rproc
->firmware_loading_complete
);
1299 * We must retrieve early virtio configuration info from
1300 * the firmware (e.g. whether to register a virtio rpmsg device,
1301 * what virtio features does it support, ...).
1303 * We're initiating an asynchronous firmware loading, so we can
1304 * be built-in kernel code, without hanging the boot process.
1306 ret
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_HOTPLUG
,
1307 rproc
->firmware
, dev
, GFP_KERNEL
,
1308 rproc
, rproc_fw_config_virtio
);
1310 dev_err(dev
, "request_firmware_nowait failed: %d\n", ret
);
1311 complete_all(&rproc
->firmware_loading_complete
);
1312 klist_remove(&rproc
->node
);
1317 EXPORT_SYMBOL(rproc_register
);
1320 * rproc_alloc() - allocate a remote processor handle
1321 * @dev: the underlying device
1322 * @name: name of this remote processor
1323 * @ops: platform-specific handlers (mainly start/stop)
1324 * @firmware: name of firmware file to load
1325 * @len: length of private data needed by the rproc driver (in bytes)
1327 * Allocates a new remote processor handle, but does not register
1330 * This function should be used by rproc implementations during initialization
1331 * of the remote processor.
1333 * After creating an rproc handle using this function, and when ready,
1334 * implementations should then call rproc_register() to complete
1335 * the registration of the remote processor.
1337 * On success the new rproc is returned, and on failure, NULL.
1339 * Note: _never_ directly deallocate @rproc, even if it was not registered
1340 * yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free().
1342 struct rproc
*rproc_alloc(struct device
*dev
, const char *name
,
1343 const struct rproc_ops
*ops
,
1344 const char *firmware
, int len
)
1346 struct rproc
*rproc
;
1348 if (!dev
|| !name
|| !ops
)
1351 rproc
= kzalloc(sizeof(struct rproc
) + len
, GFP_KERNEL
);
1353 dev_err(dev
, "%s: kzalloc failed\n", __func__
);
1360 rproc
->firmware
= firmware
;
1361 rproc
->priv
= &rproc
[1];
1363 atomic_set(&rproc
->power
, 0);
1365 kref_init(&rproc
->refcount
);
1367 mutex_init(&rproc
->lock
);
1369 INIT_LIST_HEAD(&rproc
->carveouts
);
1370 INIT_LIST_HEAD(&rproc
->mappings
);
1371 INIT_LIST_HEAD(&rproc
->traces
);
1373 rproc
->state
= RPROC_OFFLINE
;
1377 EXPORT_SYMBOL(rproc_alloc
);
1380 * rproc_free() - free an rproc handle that was allocated by rproc_alloc
1381 * @rproc: the remote processor handle
1383 * This function should _only_ be used if @rproc was only allocated,
1384 * but not registered yet.
1386 * If @rproc was already successfully registered (by calling rproc_register()),
1387 * then use rproc_unregister() instead.
1389 void rproc_free(struct rproc
*rproc
)
1393 EXPORT_SYMBOL(rproc_free
);
1396 * rproc_unregister() - unregister a remote processor
1397 * @rproc: rproc handle to unregister
1399 * Unregisters a remote processor, and decrements its refcount.
1400 * If its refcount drops to zero, then @rproc will be freed. If not,
1401 * it will be freed later once the last reference is dropped.
1403 * This function should be called when the platform specific rproc
1404 * implementation decides to remove the rproc device. it should
1405 * _only_ be called if a previous invocation of rproc_register()
1406 * has completed successfully.
1408 * After rproc_unregister() returns, @rproc is _not_ valid anymore and
1409 * it shouldn't be used. More specifically, don't call rproc_free()
1410 * or try to directly free @rproc after rproc_unregister() returns;
1411 * none of these are needed, and calling them is a bug.
1413 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1415 int rproc_unregister(struct rproc
*rproc
)
1420 /* if rproc is just being registered, wait */
1421 wait_for_completion(&rproc
->firmware_loading_complete
);
1423 /* was an rpmsg vdev created ? */
1425 rproc_remove_rpmsg_vdev(rproc
);
1427 klist_remove(&rproc
->node
);
1429 kref_put(&rproc
->refcount
, rproc_release
);
1433 EXPORT_SYMBOL(rproc_unregister
);
1435 static int __init
remoteproc_init(void)
1437 rproc_init_debugfs();
1440 module_init(remoteproc_init
);
1442 static void __exit
remoteproc_exit(void)
1444 rproc_exit_debugfs();
1446 module_exit(remoteproc_exit
);
1448 MODULE_LICENSE("GPL v2");
1449 MODULE_DESCRIPTION("Generic Remote Processor Framework");