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/idr.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 typedef int (*rproc_handle_resources_t
)(struct rproc
*rproc
,
47 struct resource_table
*table
, int len
);
48 typedef int (*rproc_handle_resource_t
)(struct rproc
*rproc
, void *, int avail
);
50 /* Unique indices for remoteproc devices */
51 static DEFINE_IDA(rproc_dev_index
);
54 * This is the IOMMU fault handler we register with the IOMMU API
55 * (when relevant; not all remote processors access memory through
58 * IOMMU core will invoke this handler whenever the remote processor
59 * will try to access an unmapped device address.
61 * Currently this is mostly a stub, but it will be later used to trigger
62 * the recovery of the remote processor.
64 static int rproc_iommu_fault(struct iommu_domain
*domain
, struct device
*dev
,
65 unsigned long iova
, int flags
, void *token
)
67 dev_err(dev
, "iommu fault: da 0x%lx flags 0x%x\n", iova
, flags
);
70 * Let the iommu core know we're not really handling this fault;
71 * we just plan to use this as a recovery trigger.
76 static int rproc_enable_iommu(struct rproc
*rproc
)
78 struct iommu_domain
*domain
;
79 struct device
*dev
= rproc
->dev
.parent
;
83 * We currently use iommu_present() to decide if an IOMMU
86 * This works for simple cases, but will easily fail with
87 * platforms that do have an IOMMU, but not for this specific
90 * This will be easily solved by introducing hw capabilities
91 * that will be set by the remoteproc driver.
93 if (!iommu_present(dev
->bus
)) {
94 dev_dbg(dev
, "iommu not found\n");
98 domain
= iommu_domain_alloc(dev
->bus
);
100 dev_err(dev
, "can't alloc iommu domain\n");
104 iommu_set_fault_handler(domain
, rproc_iommu_fault
, rproc
);
106 ret
= iommu_attach_device(domain
, dev
);
108 dev_err(dev
, "can't attach iommu device: %d\n", ret
);
112 rproc
->domain
= domain
;
117 iommu_domain_free(domain
);
121 static void rproc_disable_iommu(struct rproc
*rproc
)
123 struct iommu_domain
*domain
= rproc
->domain
;
124 struct device
*dev
= rproc
->dev
.parent
;
129 iommu_detach_device(domain
, dev
);
130 iommu_domain_free(domain
);
136 * Some remote processors will ask us to allocate them physically contiguous
137 * memory regions (which we call "carveouts"), and map them to specific
138 * device addresses (which are hardcoded in the firmware).
140 * They may then ask us to copy objects into specific device addresses (e.g.
141 * code/data sections) or expose us certain symbols in other device address
142 * (e.g. their trace buffer).
144 * This function is an internal helper with which we can go over the allocated
145 * carveouts and translate specific device address to kernel virtual addresses
146 * so we can access the referenced memory.
148 * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
149 * but only on kernel direct mapped RAM memory. Instead, we're just using
150 * here the output of the DMA API, which should be more correct.
152 static void *rproc_da_to_va(struct rproc
*rproc
, u64 da
, int len
)
154 struct rproc_mem_entry
*carveout
;
157 list_for_each_entry(carveout
, &rproc
->carveouts
, node
) {
158 int offset
= da
- carveout
->da
;
160 /* try next carveout if da is too small */
164 /* try next carveout if da is too large */
165 if (offset
+ len
> carveout
->len
)
168 ptr
= carveout
->va
+ offset
;
177 * rproc_load_segments() - load firmware segments to memory
178 * @rproc: remote processor which will be booted using these fw segments
179 * @fw: the ELF firmware image
181 * This function loads the firmware segments to memory, where the remote
182 * processor expects them.
184 * Some remote processors will expect their code and data to be placed
185 * in specific device addresses, and can't have them dynamically assigned.
187 * We currently support only those kind of remote processors, and expect
188 * the program header's paddr member to contain those addresses. We then go
189 * through the physically contiguous "carveout" memory regions which we
190 * allocated (and mapped) earlier on behalf of the remote processor,
191 * and "translate" device address to kernel addresses, so we can copy the
192 * segments where they are expected.
194 * Currently we only support remote processors that required carveout
195 * allocations and got them mapped onto their iommus. Some processors
196 * might be different: they might not have iommus, and would prefer to
197 * directly allocate memory for every segment/resource. This is not yet
201 rproc_load_segments(struct rproc
*rproc
, const struct firmware
*fw
)
203 struct device
*dev
= &rproc
->dev
;
204 struct elf32_hdr
*ehdr
;
205 struct elf32_phdr
*phdr
;
207 const u8
*elf_data
= fw
->data
;
209 ehdr
= (struct elf32_hdr
*)elf_data
;
210 phdr
= (struct elf32_phdr
*)(elf_data
+ ehdr
->e_phoff
);
212 /* go through the available ELF segments */
213 for (i
= 0; i
< ehdr
->e_phnum
; i
++, phdr
++) {
214 u32 da
= phdr
->p_paddr
;
215 u32 memsz
= phdr
->p_memsz
;
216 u32 filesz
= phdr
->p_filesz
;
217 u32 offset
= phdr
->p_offset
;
220 if (phdr
->p_type
!= PT_LOAD
)
223 dev_dbg(dev
, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
224 phdr
->p_type
, da
, memsz
, filesz
);
226 if (filesz
> memsz
) {
227 dev_err(dev
, "bad phdr filesz 0x%x memsz 0x%x\n",
233 if (offset
+ filesz
> fw
->size
) {
234 dev_err(dev
, "truncated fw: need 0x%x avail 0x%x\n",
235 offset
+ filesz
, fw
->size
);
240 /* grab the kernel address for this device address */
241 ptr
= rproc_da_to_va(rproc
, da
, memsz
);
243 dev_err(dev
, "bad phdr da 0x%x mem 0x%x\n", da
, memsz
);
248 /* put the segment where the remote processor expects it */
250 memcpy(ptr
, elf_data
+ phdr
->p_offset
, filesz
);
253 * Zero out remaining memory for this segment.
255 * This isn't strictly required since dma_alloc_coherent already
256 * did this for us. albeit harmless, we may consider removing
260 memset(ptr
+ filesz
, 0, memsz
- filesz
);
266 int rproc_alloc_vring(struct rproc_vdev
*rvdev
, int i
)
268 struct rproc
*rproc
= rvdev
->rproc
;
269 struct device
*dev
= &rproc
->dev
;
270 struct rproc_vring
*rvring
= &rvdev
->vring
[i
];
273 int ret
, size
, notifyid
;
275 /* actual size of vring (in bytes) */
276 size
= PAGE_ALIGN(vring_size(rvring
->len
, rvring
->align
));
278 if (!idr_pre_get(&rproc
->notifyids
, GFP_KERNEL
)) {
279 dev_err(dev
, "idr_pre_get failed\n");
284 * Allocate non-cacheable memory for the vring. In the future
285 * this call will also configure the IOMMU for us
286 * TODO: let the rproc know the da of this vring
288 va
= dma_alloc_coherent(dev
->parent
, size
, &dma
, GFP_KERNEL
);
290 dev_err(dev
->parent
, "dma_alloc_coherent failed\n");
295 * Assign an rproc-wide unique index for this vring
296 * TODO: assign a notifyid for rvdev updates as well
297 * TODO: let the rproc know the notifyid of this vring
298 * TODO: support predefined notifyids (via resource table)
300 ret
= idr_get_new(&rproc
->notifyids
, rvring
, ¬ifyid
);
302 dev_err(dev
, "idr_get_new failed: %d\n", ret
);
303 dma_free_coherent(dev
->parent
, size
, va
, dma
);
307 dev_dbg(dev
, "vring%d: va %p dma %x size %x idr %d\n", i
, va
,
308 dma
, size
, notifyid
);
312 rvring
->notifyid
= notifyid
;
318 rproc_parse_vring(struct rproc_vdev
*rvdev
, struct fw_rsc_vdev
*rsc
, int i
)
320 struct rproc
*rproc
= rvdev
->rproc
;
321 struct device
*dev
= &rproc
->dev
;
322 struct fw_rsc_vdev_vring
*vring
= &rsc
->vring
[i
];
323 struct rproc_vring
*rvring
= &rvdev
->vring
[i
];
325 dev_dbg(dev
, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
326 i
, vring
->da
, vring
->num
, vring
->align
);
328 /* make sure reserved bytes are zeroes */
329 if (vring
->reserved
) {
330 dev_err(dev
, "vring rsc has non zero reserved bytes\n");
334 /* verify queue size and vring alignment are sane */
335 if (!vring
->num
|| !vring
->align
) {
336 dev_err(dev
, "invalid qsz (%d) or alignment (%d)\n",
337 vring
->num
, vring
->align
);
341 rvring
->len
= vring
->num
;
342 rvring
->align
= vring
->align
;
343 rvring
->rvdev
= rvdev
;
348 void rproc_free_vring(struct rproc_vring
*rvring
)
350 int size
= PAGE_ALIGN(vring_size(rvring
->len
, rvring
->align
));
351 struct rproc
*rproc
= rvring
->rvdev
->rproc
;
353 dma_free_coherent(rproc
->dev
.parent
, size
, rvring
->va
, rvring
->dma
);
354 idr_remove(&rproc
->notifyids
, rvring
->notifyid
);
358 * rproc_handle_vdev() - handle a vdev fw resource
359 * @rproc: the remote processor
360 * @rsc: the vring resource descriptor
361 * @avail: size of available data (for sanity checking the image)
363 * This resource entry requests the host to statically register a virtio
364 * device (vdev), and setup everything needed to support it. It contains
365 * everything needed to make it possible: the virtio device id, virtio
366 * device features, vrings information, virtio config space, etc...
368 * Before registering the vdev, the vrings are allocated from non-cacheable
369 * physically contiguous memory. Currently we only support two vrings per
370 * remote processor (temporary limitation). We might also want to consider
371 * doing the vring allocation only later when ->find_vqs() is invoked, and
372 * then release them upon ->del_vqs().
374 * Note: @da is currently not really handled correctly: we dynamically
375 * allocate it using the DMA API, ignoring requested hard coded addresses,
376 * and we don't take care of any required IOMMU programming. This is all
377 * going to be taken care of when the generic iommu-based DMA API will be
378 * merged. Meanwhile, statically-addressed iommu-based firmware images should
379 * use RSC_DEVMEM resource entries to map their required @da to the physical
380 * address of their base CMA region (ouch, hacky!).
382 * Returns 0 on success, or an appropriate error code otherwise
384 static int rproc_handle_vdev(struct rproc
*rproc
, struct fw_rsc_vdev
*rsc
,
387 struct device
*dev
= &rproc
->dev
;
388 struct rproc_vdev
*rvdev
;
391 /* make sure resource isn't truncated */
392 if (sizeof(*rsc
) + rsc
->num_of_vrings
* sizeof(struct fw_rsc_vdev_vring
)
393 + rsc
->config_len
> avail
) {
394 dev_err(dev
, "vdev rsc is truncated\n");
398 /* make sure reserved bytes are zeroes */
399 if (rsc
->reserved
[0] || rsc
->reserved
[1]) {
400 dev_err(dev
, "vdev rsc has non zero reserved bytes\n");
404 dev_dbg(dev
, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
405 rsc
->id
, rsc
->dfeatures
, rsc
->config_len
, rsc
->num_of_vrings
);
407 /* we currently support only two vrings per rvdev */
408 if (rsc
->num_of_vrings
> ARRAY_SIZE(rvdev
->vring
)) {
409 dev_err(dev
, "too many vrings: %d\n", rsc
->num_of_vrings
);
413 rvdev
= kzalloc(sizeof(struct rproc_vdev
), GFP_KERNEL
);
417 rvdev
->rproc
= rproc
;
419 /* parse the vrings */
420 for (i
= 0; i
< rsc
->num_of_vrings
; i
++) {
421 ret
= rproc_parse_vring(rvdev
, rsc
, i
);
426 /* remember the device features */
427 rvdev
->dfeatures
= rsc
->dfeatures
;
429 list_add_tail(&rvdev
->node
, &rproc
->rvdevs
);
431 /* it is now safe to add the virtio device */
432 ret
= rproc_add_virtio_dev(rvdev
, rsc
->id
);
444 * rproc_handle_trace() - handle a shared trace buffer resource
445 * @rproc: the remote processor
446 * @rsc: the trace resource descriptor
447 * @avail: size of available data (for sanity checking the image)
449 * In case the remote processor dumps trace logs into memory,
450 * export it via debugfs.
452 * Currently, the 'da' member of @rsc should contain the device address
453 * where the remote processor is dumping the traces. Later we could also
454 * support dynamically allocating this address using the generic
455 * DMA API (but currently there isn't a use case for that).
457 * Returns 0 on success, or an appropriate error code otherwise
459 static int rproc_handle_trace(struct rproc
*rproc
, struct fw_rsc_trace
*rsc
,
462 struct rproc_mem_entry
*trace
;
463 struct device
*dev
= &rproc
->dev
;
467 if (sizeof(*rsc
) > avail
) {
468 dev_err(dev
, "trace rsc is truncated\n");
472 /* make sure reserved bytes are zeroes */
474 dev_err(dev
, "trace rsc has non zero reserved bytes\n");
478 /* what's the kernel address of this resource ? */
479 ptr
= rproc_da_to_va(rproc
, rsc
->da
, rsc
->len
);
481 dev_err(dev
, "erroneous trace resource entry\n");
485 trace
= kzalloc(sizeof(*trace
), GFP_KERNEL
);
487 dev_err(dev
, "kzalloc trace failed\n");
491 /* set the trace buffer dma properties */
492 trace
->len
= rsc
->len
;
495 /* make sure snprintf always null terminates, even if truncating */
496 snprintf(name
, sizeof(name
), "trace%d", rproc
->num_traces
);
498 /* create the debugfs entry */
499 trace
->priv
= rproc_create_trace_file(name
, rproc
, trace
);
506 list_add_tail(&trace
->node
, &rproc
->traces
);
510 dev_dbg(dev
, "%s added: va %p, da 0x%x, len 0x%x\n", name
, ptr
,
517 * rproc_handle_devmem() - handle devmem resource entry
518 * @rproc: remote processor handle
519 * @rsc: the devmem resource entry
520 * @avail: size of available data (for sanity checking the image)
522 * Remote processors commonly need to access certain on-chip peripherals.
524 * Some of these remote processors access memory via an iommu device,
525 * and might require us to configure their iommu before they can access
526 * the on-chip peripherals they need.
528 * This resource entry is a request to map such a peripheral device.
530 * These devmem entries will contain the physical address of the device in
531 * the 'pa' member. If a specific device address is expected, then 'da' will
532 * contain it (currently this is the only use case supported). 'len' will
533 * contain the size of the physical region we need to map.
535 * Currently we just "trust" those devmem entries to contain valid physical
536 * addresses, but this is going to change: we want the implementations to
537 * tell us ranges of physical addresses the firmware is allowed to request,
538 * and not allow firmwares to request access to physical addresses that
539 * are outside those ranges.
541 static int rproc_handle_devmem(struct rproc
*rproc
, struct fw_rsc_devmem
*rsc
,
544 struct rproc_mem_entry
*mapping
;
545 struct device
*dev
= &rproc
->dev
;
548 /* no point in handling this resource without a valid iommu domain */
552 if (sizeof(*rsc
) > avail
) {
553 dev_err(dev
, "devmem rsc is truncated\n");
557 /* make sure reserved bytes are zeroes */
559 dev_err(dev
, "devmem rsc has non zero reserved bytes\n");
563 mapping
= kzalloc(sizeof(*mapping
), GFP_KERNEL
);
565 dev_err(dev
, "kzalloc mapping failed\n");
569 ret
= iommu_map(rproc
->domain
, rsc
->da
, rsc
->pa
, rsc
->len
, rsc
->flags
);
571 dev_err(dev
, "failed to map devmem: %d\n", ret
);
576 * We'll need this info later when we'll want to unmap everything
577 * (e.g. on shutdown).
579 * We can't trust the remote processor not to change the resource
580 * table, so we must maintain this info independently.
582 mapping
->da
= rsc
->da
;
583 mapping
->len
= rsc
->len
;
584 list_add_tail(&mapping
->node
, &rproc
->mappings
);
586 dev_dbg(dev
, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
587 rsc
->pa
, rsc
->da
, rsc
->len
);
597 * rproc_handle_carveout() - handle phys contig memory allocation requests
598 * @rproc: rproc handle
599 * @rsc: the resource entry
600 * @avail: size of available data (for image validation)
602 * This function will handle firmware requests for allocation of physically
603 * contiguous memory regions.
605 * These request entries should come first in the firmware's resource table,
606 * as other firmware entries might request placing other data objects inside
607 * these memory regions (e.g. data/code segments, trace resource entries, ...).
609 * Allocating memory this way helps utilizing the reserved physical memory
610 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
611 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
612 * pressure is important; it may have a substantial impact on performance.
614 static int rproc_handle_carveout(struct rproc
*rproc
,
615 struct fw_rsc_carveout
*rsc
, int avail
)
617 struct rproc_mem_entry
*carveout
, *mapping
;
618 struct device
*dev
= &rproc
->dev
;
623 if (sizeof(*rsc
) > avail
) {
624 dev_err(dev
, "carveout rsc is truncated\n");
628 /* make sure reserved bytes are zeroes */
630 dev_err(dev
, "carveout rsc has non zero reserved bytes\n");
634 dev_dbg(dev
, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
635 rsc
->da
, rsc
->pa
, rsc
->len
, rsc
->flags
);
637 mapping
= kzalloc(sizeof(*mapping
), GFP_KERNEL
);
639 dev_err(dev
, "kzalloc mapping failed\n");
643 carveout
= kzalloc(sizeof(*carveout
), GFP_KERNEL
);
645 dev_err(dev
, "kzalloc carveout failed\n");
650 va
= dma_alloc_coherent(dev
->parent
, rsc
->len
, &dma
, GFP_KERNEL
);
652 dev_err(dev
->parent
, "dma_alloc_coherent err: %d\n", rsc
->len
);
657 dev_dbg(dev
, "carveout va %p, dma %x, len 0x%x\n", va
, dma
, rsc
->len
);
660 * Ok, this is non-standard.
662 * Sometimes we can't rely on the generic iommu-based DMA API
663 * to dynamically allocate the device address and then set the IOMMU
664 * tables accordingly, because some remote processors might
665 * _require_ us to use hard coded device addresses that their
666 * firmware was compiled with.
668 * In this case, we must use the IOMMU API directly and map
669 * the memory to the device address as expected by the remote
672 * Obviously such remote processor devices should not be configured
673 * to use the iommu-based DMA API: we expect 'dma' to contain the
674 * physical address in this case.
677 ret
= iommu_map(rproc
->domain
, rsc
->da
, dma
, rsc
->len
,
680 dev_err(dev
, "iommu_map failed: %d\n", ret
);
685 * We'll need this info later when we'll want to unmap
686 * everything (e.g. on shutdown).
688 * We can't trust the remote processor not to change the
689 * resource table, so we must maintain this info independently.
691 mapping
->da
= rsc
->da
;
692 mapping
->len
= rsc
->len
;
693 list_add_tail(&mapping
->node
, &rproc
->mappings
);
695 dev_dbg(dev
, "carveout mapped 0x%x to 0x%x\n", rsc
->da
, dma
);
699 * Some remote processors might need to know the pa
700 * even though they are behind an IOMMU. E.g., OMAP4's
701 * remote M3 processor needs this so it can control
702 * on-chip hardware accelerators that are not behind
703 * the IOMMU, and therefor must know the pa.
705 * Generally we don't want to expose physical addresses
706 * if we don't have to (remote processors are generally
707 * _not_ trusted), so we might want to do this only for
708 * remote processor that _must_ have this (e.g. OMAP4's
709 * dual M3 subsystem).
711 * Non-IOMMU processors might also want to have this info.
712 * In this case, the device address and the physical address
718 carveout
->len
= rsc
->len
;
720 carveout
->da
= rsc
->da
;
722 list_add_tail(&carveout
->node
, &rproc
->carveouts
);
727 dma_free_coherent(dev
->parent
, rsc
->len
, va
, dma
);
736 * A lookup table for resource handlers. The indices are defined in
737 * enum fw_resource_type.
739 static rproc_handle_resource_t rproc_handle_rsc
[] = {
740 [RSC_CARVEOUT
] = (rproc_handle_resource_t
)rproc_handle_carveout
,
741 [RSC_DEVMEM
] = (rproc_handle_resource_t
)rproc_handle_devmem
,
742 [RSC_TRACE
] = (rproc_handle_resource_t
)rproc_handle_trace
,
743 [RSC_VDEV
] = NULL
, /* VDEVs were handled upon registrarion */
746 /* handle firmware resource entries before booting the remote processor */
748 rproc_handle_boot_rsc(struct rproc
*rproc
, struct resource_table
*table
, int len
)
750 struct device
*dev
= &rproc
->dev
;
751 rproc_handle_resource_t handler
;
754 for (i
= 0; i
< table
->num
; i
++) {
755 int offset
= table
->offset
[i
];
756 struct fw_rsc_hdr
*hdr
= (void *)table
+ offset
;
757 int avail
= len
- offset
- sizeof(*hdr
);
758 void *rsc
= (void *)hdr
+ sizeof(*hdr
);
760 /* make sure table isn't truncated */
762 dev_err(dev
, "rsc table is truncated\n");
766 dev_dbg(dev
, "rsc: type %d\n", hdr
->type
);
768 if (hdr
->type
>= RSC_LAST
) {
769 dev_warn(dev
, "unsupported resource %d\n", hdr
->type
);
773 handler
= rproc_handle_rsc
[hdr
->type
];
777 ret
= handler(rproc
, rsc
, avail
);
785 /* handle firmware resource entries while registering the remote processor */
787 rproc_handle_virtio_rsc(struct rproc
*rproc
, struct resource_table
*table
, int len
)
789 struct device
*dev
= &rproc
->dev
;
792 for (i
= 0; i
< table
->num
; i
++) {
793 int offset
= table
->offset
[i
];
794 struct fw_rsc_hdr
*hdr
= (void *)table
+ offset
;
795 int avail
= len
- offset
- sizeof(*hdr
);
796 struct fw_rsc_vdev
*vrsc
;
798 /* make sure table isn't truncated */
800 dev_err(dev
, "rsc table is truncated\n");
804 dev_dbg(dev
, "%s: rsc type %d\n", __func__
, hdr
->type
);
806 if (hdr
->type
!= RSC_VDEV
)
809 vrsc
= (struct fw_rsc_vdev
*)hdr
->data
;
811 ret
= rproc_handle_vdev(rproc
, vrsc
, avail
);
820 * rproc_find_rsc_table() - find the resource table
821 * @rproc: the rproc handle
822 * @fw: the ELF firmware image
823 * @tablesz: place holder for providing back the table size
825 * This function finds the resource table inside the remote processor's
826 * firmware. It is used both upon the registration of @rproc (in order
827 * to look for and register the supported virito devices), and when the
830 * Returns the pointer to the resource table if it is found, and write its
831 * size into @tablesz. If a valid table isn't found, NULL is returned
832 * (and @tablesz isn't set).
834 static struct resource_table
*
835 rproc_find_rsc_table(struct rproc
*rproc
, const struct firmware
*fw
,
838 struct elf32_hdr
*ehdr
;
839 struct elf32_shdr
*shdr
;
840 const char *name_table
;
841 struct device
*dev
= &rproc
->dev
;
842 struct resource_table
*table
= NULL
;
844 const u8
*elf_data
= fw
->data
;
846 ehdr
= (struct elf32_hdr
*)elf_data
;
847 shdr
= (struct elf32_shdr
*)(elf_data
+ ehdr
->e_shoff
);
848 name_table
= elf_data
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
850 /* look for the resource table and handle it */
851 for (i
= 0; i
< ehdr
->e_shnum
; i
++, shdr
++) {
852 int size
= shdr
->sh_size
;
853 int offset
= shdr
->sh_offset
;
855 if (strcmp(name_table
+ shdr
->sh_name
, ".resource_table"))
858 table
= (struct resource_table
*)(elf_data
+ offset
);
860 /* make sure we have the entire table */
861 if (offset
+ size
> fw
->size
) {
862 dev_err(dev
, "resource table truncated\n");
866 /* make sure table has at least the header */
867 if (sizeof(struct resource_table
) > size
) {
868 dev_err(dev
, "header-less resource table\n");
872 /* we don't support any version beyond the first */
873 if (table
->ver
!= 1) {
874 dev_err(dev
, "unsupported fw ver: %d\n", table
->ver
);
878 /* make sure reserved bytes are zeroes */
879 if (table
->reserved
[0] || table
->reserved
[1]) {
880 dev_err(dev
, "non zero reserved bytes\n");
884 /* make sure the offsets array isn't truncated */
885 if (table
->num
* sizeof(table
->offset
[0]) +
886 sizeof(struct resource_table
) > size
) {
887 dev_err(dev
, "resource table incomplete\n");
891 *tablesz
= shdr
->sh_size
;
899 * rproc_resource_cleanup() - clean up and free all acquired resources
900 * @rproc: rproc handle
902 * This function will free all resources acquired for @rproc, and it
903 * is called whenever @rproc either shuts down or fails to boot.
905 static void rproc_resource_cleanup(struct rproc
*rproc
)
907 struct rproc_mem_entry
*entry
, *tmp
;
908 struct device
*dev
= &rproc
->dev
;
910 /* clean up debugfs trace entries */
911 list_for_each_entry_safe(entry
, tmp
, &rproc
->traces
, node
) {
912 rproc_remove_trace_file(entry
->priv
);
914 list_del(&entry
->node
);
918 /* clean up carveout allocations */
919 list_for_each_entry_safe(entry
, tmp
, &rproc
->carveouts
, node
) {
920 dma_free_coherent(dev
->parent
, entry
->len
, entry
->va
, entry
->dma
);
921 list_del(&entry
->node
);
925 /* clean up iommu mapping entries */
926 list_for_each_entry_safe(entry
, tmp
, &rproc
->mappings
, node
) {
929 unmapped
= iommu_unmap(rproc
->domain
, entry
->da
, entry
->len
);
930 if (unmapped
!= entry
->len
) {
931 /* nothing much to do besides complaining */
932 dev_err(dev
, "failed to unmap %u/%u\n", entry
->len
,
936 list_del(&entry
->node
);
941 /* make sure this fw image is sane */
942 static int rproc_fw_sanity_check(struct rproc
*rproc
, const struct firmware
*fw
)
944 const char *name
= rproc
->firmware
;
945 struct device
*dev
= &rproc
->dev
;
946 struct elf32_hdr
*ehdr
;
950 dev_err(dev
, "failed to load %s\n", name
);
954 if (fw
->size
< sizeof(struct elf32_hdr
)) {
955 dev_err(dev
, "Image is too small\n");
959 ehdr
= (struct elf32_hdr
*)fw
->data
;
961 /* We only support ELF32 at this point */
962 class = ehdr
->e_ident
[EI_CLASS
];
963 if (class != ELFCLASS32
) {
964 dev_err(dev
, "Unsupported class: %d\n", class);
968 /* We assume the firmware has the same endianess as the host */
969 # ifdef __LITTLE_ENDIAN
970 if (ehdr
->e_ident
[EI_DATA
] != ELFDATA2LSB
) {
971 # else /* BIG ENDIAN */
972 if (ehdr
->e_ident
[EI_DATA
] != ELFDATA2MSB
) {
974 dev_err(dev
, "Unsupported firmware endianess\n");
978 if (fw
->size
< ehdr
->e_shoff
+ sizeof(struct elf32_shdr
)) {
979 dev_err(dev
, "Image is too small\n");
983 if (memcmp(ehdr
->e_ident
, ELFMAG
, SELFMAG
)) {
984 dev_err(dev
, "Image is corrupted (bad magic)\n");
988 if (ehdr
->e_phnum
== 0) {
989 dev_err(dev
, "No loadable segments\n");
993 if (ehdr
->e_phoff
> fw
->size
) {
994 dev_err(dev
, "Firmware size is too small\n");
1002 * take a firmware and boot a remote processor with it.
1004 static int rproc_fw_boot(struct rproc
*rproc
, const struct firmware
*fw
)
1006 struct device
*dev
= &rproc
->dev
;
1007 const char *name
= rproc
->firmware
;
1008 struct elf32_hdr
*ehdr
;
1009 struct resource_table
*table
;
1012 ret
= rproc_fw_sanity_check(rproc
, fw
);
1016 ehdr
= (struct elf32_hdr
*)fw
->data
;
1018 dev_info(dev
, "Booting fw image %s, size %d\n", name
, fw
->size
);
1021 * if enabling an IOMMU isn't relevant for this rproc, this is
1024 ret
= rproc_enable_iommu(rproc
);
1026 dev_err(dev
, "can't enable iommu: %d\n", ret
);
1031 * The ELF entry point is the rproc's boot addr (though this is not
1032 * a configurable property of all remote processors: some will always
1033 * boot at a specific hardcoded address).
1035 rproc
->bootaddr
= ehdr
->e_entry
;
1037 /* look for the resource table */
1038 table
= rproc_find_rsc_table(rproc
, fw
, &tablesz
);
1042 /* handle fw resources which are required to boot rproc */
1043 ret
= rproc_handle_boot_rsc(rproc
, table
, tablesz
);
1045 dev_err(dev
, "Failed to process resources: %d\n", ret
);
1049 /* load the ELF segments to memory */
1050 ret
= rproc_load_segments(rproc
, fw
);
1052 dev_err(dev
, "Failed to load program segments: %d\n", ret
);
1056 /* power up the remote processor */
1057 ret
= rproc
->ops
->start(rproc
);
1059 dev_err(dev
, "can't start rproc %s: %d\n", rproc
->name
, ret
);
1063 rproc
->state
= RPROC_RUNNING
;
1065 dev_info(dev
, "remote processor %s is now up\n", rproc
->name
);
1070 rproc_resource_cleanup(rproc
);
1071 rproc_disable_iommu(rproc
);
1076 * take a firmware and look for virtio devices to register.
1078 * Note: this function is called asynchronously upon registration of the
1079 * remote processor (so we must wait until it completes before we try
1080 * to unregister the device. one other option is just to use kref here,
1081 * that might be cleaner).
1083 static void rproc_fw_config_virtio(const struct firmware
*fw
, void *context
)
1085 struct rproc
*rproc
= context
;
1086 struct resource_table
*table
;
1089 if (rproc_fw_sanity_check(rproc
, fw
) < 0)
1092 /* look for the resource table */
1093 table
= rproc_find_rsc_table(rproc
, fw
, &tablesz
);
1097 /* look for virtio devices and register them */
1098 ret
= rproc_handle_virtio_rsc(rproc
, table
, tablesz
);
1103 release_firmware(fw
);
1104 /* allow rproc_del() contexts, if any, to proceed */
1105 complete_all(&rproc
->firmware_loading_complete
);
1109 * rproc_boot() - boot a remote processor
1110 * @rproc: handle of a remote processor
1112 * Boot a remote processor (i.e. load its firmware, power it on, ...).
1114 * If the remote processor is already powered on, this function immediately
1115 * returns (successfully).
1117 * Returns 0 on success, and an appropriate error value otherwise.
1119 int rproc_boot(struct rproc
*rproc
)
1121 const struct firmware
*firmware_p
;
1126 pr_err("invalid rproc handle\n");
1132 ret
= mutex_lock_interruptible(&rproc
->lock
);
1134 dev_err(dev
, "can't lock rproc %s: %d\n", rproc
->name
, ret
);
1138 /* loading a firmware is required */
1139 if (!rproc
->firmware
) {
1140 dev_err(dev
, "%s: no firmware to load\n", __func__
);
1145 /* prevent underlying implementation from being removed */
1146 if (!try_module_get(dev
->parent
->driver
->owner
)) {
1147 dev_err(dev
, "%s: can't get owner\n", __func__
);
1152 /* skip the boot process if rproc is already powered up */
1153 if (atomic_inc_return(&rproc
->power
) > 1) {
1158 dev_info(dev
, "powering up %s\n", rproc
->name
);
1161 ret
= request_firmware(&firmware_p
, rproc
->firmware
, dev
);
1163 dev_err(dev
, "request_firmware failed: %d\n", ret
);
1167 ret
= rproc_fw_boot(rproc
, firmware_p
);
1169 release_firmware(firmware_p
);
1173 module_put(dev
->parent
->driver
->owner
);
1174 atomic_dec(&rproc
->power
);
1177 mutex_unlock(&rproc
->lock
);
1180 EXPORT_SYMBOL(rproc_boot
);
1183 * rproc_shutdown() - power off the remote processor
1184 * @rproc: the remote processor
1186 * Power off a remote processor (previously booted with rproc_boot()).
1188 * In case @rproc is still being used by an additional user(s), then
1189 * this function will just decrement the power refcount and exit,
1190 * without really powering off the device.
1192 * Every call to rproc_boot() must (eventually) be accompanied by a call
1193 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1196 * - we're not decrementing the rproc's refcount, only the power refcount.
1197 * which means that the @rproc handle stays valid even after rproc_shutdown()
1198 * returns, and users can still use it with a subsequent rproc_boot(), if
1201 void rproc_shutdown(struct rproc
*rproc
)
1203 struct device
*dev
= &rproc
->dev
;
1206 ret
= mutex_lock_interruptible(&rproc
->lock
);
1208 dev_err(dev
, "can't lock rproc %s: %d\n", rproc
->name
, ret
);
1212 /* if the remote proc is still needed, bail out */
1213 if (!atomic_dec_and_test(&rproc
->power
))
1216 /* power off the remote processor */
1217 ret
= rproc
->ops
->stop(rproc
);
1219 atomic_inc(&rproc
->power
);
1220 dev_err(dev
, "can't stop rproc: %d\n", ret
);
1224 /* clean up all acquired resources */
1225 rproc_resource_cleanup(rproc
);
1227 rproc_disable_iommu(rproc
);
1229 rproc
->state
= RPROC_OFFLINE
;
1231 dev_info(dev
, "stopped remote processor %s\n", rproc
->name
);
1234 mutex_unlock(&rproc
->lock
);
1236 module_put(dev
->parent
->driver
->owner
);
1238 EXPORT_SYMBOL(rproc_shutdown
);
1241 * rproc_add() - register a remote processor
1242 * @rproc: the remote processor handle to register
1244 * Registers @rproc with the remoteproc framework, after it has been
1245 * allocated with rproc_alloc().
1247 * This is called by the platform-specific rproc implementation, whenever
1248 * a new remote processor device is probed.
1250 * Returns 0 on success and an appropriate error code otherwise.
1252 * Note: this function initiates an asynchronous firmware loading
1253 * context, which will look for virtio devices supported by the rproc's
1256 * If found, those virtio devices will be created and added, so as a result
1257 * of registering this remote processor, additional virtio drivers might be
1260 int rproc_add(struct rproc
*rproc
)
1262 struct device
*dev
= &rproc
->dev
;
1265 ret
= device_add(dev
);
1269 dev_info(dev
, "%s is available\n", rproc
->name
);
1271 dev_info(dev
, "Note: remoteproc is still under development and considered experimental.\n");
1272 dev_info(dev
, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1274 /* create debugfs entries */
1275 rproc_create_debug_dir(rproc
);
1277 /* rproc_del() calls must wait until async loader completes */
1278 init_completion(&rproc
->firmware_loading_complete
);
1281 * We must retrieve early virtio configuration info from
1282 * the firmware (e.g. whether to register a virtio device,
1283 * what virtio features does it support, ...).
1285 * We're initiating an asynchronous firmware loading, so we can
1286 * be built-in kernel code, without hanging the boot process.
1288 ret
= request_firmware_nowait(THIS_MODULE
, FW_ACTION_HOTPLUG
,
1289 rproc
->firmware
, dev
, GFP_KERNEL
,
1290 rproc
, rproc_fw_config_virtio
);
1292 dev_err(dev
, "request_firmware_nowait failed: %d\n", ret
);
1293 complete_all(&rproc
->firmware_loading_complete
);
1298 EXPORT_SYMBOL(rproc_add
);
1301 * rproc_type_release() - release a remote processor instance
1302 * @dev: the rproc's device
1304 * This function should _never_ be called directly.
1306 * It will be called by the driver core when no one holds a valid pointer
1309 static void rproc_type_release(struct device
*dev
)
1311 struct rproc
*rproc
= container_of(dev
, struct rproc
, dev
);
1313 dev_info(&rproc
->dev
, "releasing %s\n", rproc
->name
);
1315 rproc_delete_debug_dir(rproc
);
1317 idr_remove_all(&rproc
->notifyids
);
1318 idr_destroy(&rproc
->notifyids
);
1320 if (rproc
->index
>= 0)
1321 ida_simple_remove(&rproc_dev_index
, rproc
->index
);
1326 static struct device_type rproc_type
= {
1327 .name
= "remoteproc",
1328 .release
= rproc_type_release
,
1332 * rproc_alloc() - allocate a remote processor handle
1333 * @dev: the underlying device
1334 * @name: name of this remote processor
1335 * @ops: platform-specific handlers (mainly start/stop)
1336 * @firmware: name of firmware file to load
1337 * @len: length of private data needed by the rproc driver (in bytes)
1339 * Allocates a new remote processor handle, but does not register
1342 * This function should be used by rproc implementations during initialization
1343 * of the remote processor.
1345 * After creating an rproc handle using this function, and when ready,
1346 * implementations should then call rproc_add() to complete
1347 * the registration of the remote processor.
1349 * On success the new rproc is returned, and on failure, NULL.
1351 * Note: _never_ directly deallocate @rproc, even if it was not registered
1352 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
1354 struct rproc
*rproc_alloc(struct device
*dev
, const char *name
,
1355 const struct rproc_ops
*ops
,
1356 const char *firmware
, int len
)
1358 struct rproc
*rproc
;
1360 if (!dev
|| !name
|| !ops
)
1363 rproc
= kzalloc(sizeof(struct rproc
) + len
, GFP_KERNEL
);
1365 dev_err(dev
, "%s: kzalloc failed\n", __func__
);
1371 rproc
->firmware
= firmware
;
1372 rproc
->priv
= &rproc
[1];
1374 device_initialize(&rproc
->dev
);
1375 rproc
->dev
.parent
= dev
;
1376 rproc
->dev
.type
= &rproc_type
;
1378 /* Assign a unique device index and name */
1379 rproc
->index
= ida_simple_get(&rproc_dev_index
, 0, 0, GFP_KERNEL
);
1380 if (rproc
->index
< 0) {
1381 dev_err(dev
, "ida_simple_get failed: %d\n", rproc
->index
);
1382 put_device(&rproc
->dev
);
1386 dev_set_name(&rproc
->dev
, "remoteproc%d", rproc
->index
);
1388 atomic_set(&rproc
->power
, 0);
1390 mutex_init(&rproc
->lock
);
1392 idr_init(&rproc
->notifyids
);
1394 INIT_LIST_HEAD(&rproc
->carveouts
);
1395 INIT_LIST_HEAD(&rproc
->mappings
);
1396 INIT_LIST_HEAD(&rproc
->traces
);
1397 INIT_LIST_HEAD(&rproc
->rvdevs
);
1399 rproc
->state
= RPROC_OFFLINE
;
1403 EXPORT_SYMBOL(rproc_alloc
);
1406 * rproc_put() - unroll rproc_alloc()
1407 * @rproc: the remote processor handle
1409 * This function decrements the rproc dev refcount.
1411 * If no one holds any reference to rproc anymore, then its refcount would
1412 * now drop to zero, and it would be freed.
1414 void rproc_put(struct rproc
*rproc
)
1416 put_device(&rproc
->dev
);
1418 EXPORT_SYMBOL(rproc_put
);
1421 * rproc_del() - unregister a remote processor
1422 * @rproc: rproc handle to unregister
1424 * This function should be called when the platform specific rproc
1425 * implementation decides to remove the rproc device. it should
1426 * _only_ be called if a previous invocation of rproc_add()
1427 * has completed successfully.
1429 * After rproc_del() returns, @rproc isn't freed yet, because
1430 * of the outstanding reference created by rproc_alloc. To decrement that
1431 * one last refcount, one still needs to call rproc_put().
1433 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1435 int rproc_del(struct rproc
*rproc
)
1437 struct rproc_vdev
*rvdev
, *tmp
;
1442 /* if rproc is just being registered, wait */
1443 wait_for_completion(&rproc
->firmware_loading_complete
);
1445 /* clean up remote vdev entries */
1446 list_for_each_entry_safe(rvdev
, tmp
, &rproc
->rvdevs
, node
)
1447 rproc_remove_virtio_dev(rvdev
);
1449 device_del(&rproc
->dev
);
1453 EXPORT_SYMBOL(rproc_del
);
1455 static int __init
remoteproc_init(void)
1457 rproc_init_debugfs();
1461 module_init(remoteproc_init
);
1463 static void __exit
remoteproc_exit(void)
1465 rproc_exit_debugfs();
1467 module_exit(remoteproc_exit
);
1469 MODULE_LICENSE("GPL v2");
1470 MODULE_DESCRIPTION("Generic Remote Processor Framework");