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/crc32.h>
41 #include <linux/virtio_ids.h>
42 #include <linux/virtio_ring.h>
43 #include <asm/byteorder.h>
45 #include "remoteproc_internal.h"
47 typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
48 struct resource_table *table, int len);
49 typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
50 void *, int offset, int avail);
52 /* Unique indices for remoteproc devices */
53 static DEFINE_IDA(rproc_dev_index);
55 static const char * const rproc_crash_names[] = {
56 [RPROC_MMUFAULT] = "mmufault",
59 /* translate rproc_crash_type to string */
60 static const char *rproc_crash_to_string(enum rproc_crash_type type)
62 if (type < ARRAY_SIZE(rproc_crash_names))
63 return rproc_crash_names[type];
68 * This is the IOMMU fault handler we register with the IOMMU API
69 * (when relevant; not all remote processors access memory through
72 * IOMMU core will invoke this handler whenever the remote processor
73 * will try to access an unmapped device address.
75 static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
76 unsigned long iova, int flags, void *token)
78 struct rproc *rproc = token;
80 dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
82 rproc_report_crash(rproc, RPROC_MMUFAULT);
85 * Let the iommu core know we're not really handling this fault;
86 * we just used it as a recovery trigger.
91 static int rproc_enable_iommu(struct rproc *rproc)
93 struct iommu_domain *domain;
94 struct device *dev = rproc->dev.parent;
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, rproc);
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.parent;
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 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;
190 EXPORT_SYMBOL(rproc_da_to_va);
192 int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
194 struct rproc *rproc = rvdev->rproc;
195 struct device *dev = &rproc->dev;
196 struct rproc_vring *rvring = &rvdev->vring[i];
197 struct fw_rsc_vdev *rsc;
200 int ret, size, notifyid;
202 /* actual size of vring (in bytes) */
203 size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
206 * Allocate non-cacheable memory for the vring. In the future
207 * this call will also configure the IOMMU for us
209 va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
211 dev_err(dev->parent, "dma_alloc_coherent failed\n");
216 * Assign an rproc-wide unique index for this vring
217 * TODO: assign a notifyid for rvdev updates as well
218 * TODO: support predefined notifyids (via resource table)
220 ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
222 dev_err(dev, "idr_alloc failed: %d\n", ret);
223 dma_free_coherent(dev->parent, size, va, dma);
228 dev_dbg(dev, "vring%d: va %p dma %llx size %x idr %d\n", i, va,
229 (unsigned long long)dma, size, notifyid);
233 rvring->notifyid = notifyid;
236 * Let the rproc know the notifyid and da of this vring.
237 * Not all platforms use dma_alloc_coherent to automatically
238 * set up the iommu. In this case the device address (da) will
239 * hold the physical address and not the device address.
241 rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
242 rsc->vring[i].da = dma;
243 rsc->vring[i].notifyid = notifyid;
248 rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
250 struct rproc *rproc = rvdev->rproc;
251 struct device *dev = &rproc->dev;
252 struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
253 struct rproc_vring *rvring = &rvdev->vring[i];
255 dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
256 i, vring->da, vring->num, vring->align);
258 /* make sure reserved bytes are zeroes */
259 if (vring->reserved) {
260 dev_err(dev, "vring rsc has non zero reserved bytes\n");
264 /* verify queue size and vring alignment are sane */
265 if (!vring->num || !vring->align) {
266 dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
267 vring->num, vring->align);
271 rvring->len = vring->num;
272 rvring->align = vring->align;
273 rvring->rvdev = rvdev;
278 void rproc_free_vring(struct rproc_vring *rvring)
280 int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
281 struct rproc *rproc = rvring->rvdev->rproc;
282 int idx = rvring->rvdev->vring - rvring;
283 struct fw_rsc_vdev *rsc;
285 dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
286 idr_remove(&rproc->notifyids, rvring->notifyid);
288 /* reset resource entry info */
289 rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
290 rsc->vring[idx].da = 0;
291 rsc->vring[idx].notifyid = -1;
295 * rproc_handle_vdev() - handle a vdev fw resource
296 * @rproc: the remote processor
297 * @rsc: the vring resource descriptor
298 * @avail: size of available data (for sanity checking the image)
300 * This resource entry requests the host to statically register a virtio
301 * device (vdev), and setup everything needed to support it. It contains
302 * everything needed to make it possible: the virtio device id, virtio
303 * device features, vrings information, virtio config space, etc...
305 * Before registering the vdev, the vrings are allocated from non-cacheable
306 * physically contiguous memory. Currently we only support two vrings per
307 * remote processor (temporary limitation). We might also want to consider
308 * doing the vring allocation only later when ->find_vqs() is invoked, and
309 * then release them upon ->del_vqs().
311 * Note: @da is currently not really handled correctly: we dynamically
312 * allocate it using the DMA API, ignoring requested hard coded addresses,
313 * and we don't take care of any required IOMMU programming. This is all
314 * going to be taken care of when the generic iommu-based DMA API will be
315 * merged. Meanwhile, statically-addressed iommu-based firmware images should
316 * use RSC_DEVMEM resource entries to map their required @da to the physical
317 * address of their base CMA region (ouch, hacky!).
319 * Returns 0 on success, or an appropriate error code otherwise
321 static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
322 int offset, int avail)
324 struct device *dev = &rproc->dev;
325 struct rproc_vdev *rvdev;
328 /* make sure resource isn't truncated */
329 if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
330 + rsc->config_len > avail) {
331 dev_err(dev, "vdev rsc is truncated\n");
335 /* make sure reserved bytes are zeroes */
336 if (rsc->reserved[0] || rsc->reserved[1]) {
337 dev_err(dev, "vdev rsc has non zero reserved bytes\n");
341 dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
342 rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
344 /* we currently support only two vrings per rvdev */
345 if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
346 dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
350 rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
354 rvdev->rproc = rproc;
356 /* parse the vrings */
357 for (i = 0; i < rsc->num_of_vrings; i++) {
358 ret = rproc_parse_vring(rvdev, rsc, i);
363 /* remember the resource offset*/
364 rvdev->rsc_offset = offset;
366 list_add_tail(&rvdev->node, &rproc->rvdevs);
368 /* it is now safe to add the virtio device */
369 ret = rproc_add_virtio_dev(rvdev, rsc->id);
376 list_del(&rvdev->node);
383 * rproc_handle_trace() - handle a shared trace buffer resource
384 * @rproc: the remote processor
385 * @rsc: the trace resource descriptor
386 * @avail: size of available data (for sanity checking the image)
388 * In case the remote processor dumps trace logs into memory,
389 * export it via debugfs.
391 * Currently, the 'da' member of @rsc should contain the device address
392 * where the remote processor is dumping the traces. Later we could also
393 * support dynamically allocating this address using the generic
394 * DMA API (but currently there isn't a use case for that).
396 * Returns 0 on success, or an appropriate error code otherwise
398 static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
399 int offset, int avail)
401 struct rproc_mem_entry *trace;
402 struct device *dev = &rproc->dev;
406 if (sizeof(*rsc) > avail) {
407 dev_err(dev, "trace rsc is truncated\n");
411 /* make sure reserved bytes are zeroes */
413 dev_err(dev, "trace rsc has non zero reserved bytes\n");
417 /* what's the kernel address of this resource ? */
418 ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
420 dev_err(dev, "erroneous trace resource entry\n");
424 trace = kzalloc(sizeof(*trace), GFP_KERNEL);
426 dev_err(dev, "kzalloc trace failed\n");
430 /* set the trace buffer dma properties */
431 trace->len = rsc->len;
434 /* make sure snprintf always null terminates, even if truncating */
435 snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
437 /* create the debugfs entry */
438 trace->priv = rproc_create_trace_file(name, rproc, trace);
445 list_add_tail(&trace->node, &rproc->traces);
449 dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
456 * rproc_handle_devmem() - handle devmem resource entry
457 * @rproc: remote processor handle
458 * @rsc: the devmem resource entry
459 * @avail: size of available data (for sanity checking the image)
461 * Remote processors commonly need to access certain on-chip peripherals.
463 * Some of these remote processors access memory via an iommu device,
464 * and might require us to configure their iommu before they can access
465 * the on-chip peripherals they need.
467 * This resource entry is a request to map such a peripheral device.
469 * These devmem entries will contain the physical address of the device in
470 * the 'pa' member. If a specific device address is expected, then 'da' will
471 * contain it (currently this is the only use case supported). 'len' will
472 * contain the size of the physical region we need to map.
474 * Currently we just "trust" those devmem entries to contain valid physical
475 * addresses, but this is going to change: we want the implementations to
476 * tell us ranges of physical addresses the firmware is allowed to request,
477 * and not allow firmwares to request access to physical addresses that
478 * are outside those ranges.
480 static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
481 int offset, int avail)
483 struct rproc_mem_entry *mapping;
484 struct device *dev = &rproc->dev;
487 /* no point in handling this resource without a valid iommu domain */
491 if (sizeof(*rsc) > avail) {
492 dev_err(dev, "devmem rsc is truncated\n");
496 /* make sure reserved bytes are zeroes */
498 dev_err(dev, "devmem rsc has non zero reserved bytes\n");
502 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
504 dev_err(dev, "kzalloc mapping failed\n");
508 ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
510 dev_err(dev, "failed to map devmem: %d\n", ret);
515 * We'll need this info later when we'll want to unmap everything
516 * (e.g. on shutdown).
518 * We can't trust the remote processor not to change the resource
519 * table, so we must maintain this info independently.
521 mapping->da = rsc->da;
522 mapping->len = rsc->len;
523 list_add_tail(&mapping->node, &rproc->mappings);
525 dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
526 rsc->pa, rsc->da, rsc->len);
536 * rproc_handle_carveout() - handle phys contig memory allocation requests
537 * @rproc: rproc handle
538 * @rsc: the resource entry
539 * @avail: size of available data (for image validation)
541 * This function will handle firmware requests for allocation of physically
542 * contiguous memory regions.
544 * These request entries should come first in the firmware's resource table,
545 * as other firmware entries might request placing other data objects inside
546 * these memory regions (e.g. data/code segments, trace resource entries, ...).
548 * Allocating memory this way helps utilizing the reserved physical memory
549 * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
550 * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
551 * pressure is important; it may have a substantial impact on performance.
553 static int rproc_handle_carveout(struct rproc *rproc,
554 struct fw_rsc_carveout *rsc,
555 int offset, int avail)
558 struct rproc_mem_entry *carveout, *mapping;
559 struct device *dev = &rproc->dev;
564 if (sizeof(*rsc) > avail) {
565 dev_err(dev, "carveout rsc is truncated\n");
569 /* make sure reserved bytes are zeroes */
571 dev_err(dev, "carveout rsc has non zero reserved bytes\n");
575 dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
576 rsc->da, rsc->pa, rsc->len, rsc->flags);
578 carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
580 dev_err(dev, "kzalloc carveout failed\n");
584 va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
586 dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
591 dev_dbg(dev, "carveout va %p, dma %llx, len 0x%x\n", va,
592 (unsigned long long)dma, rsc->len);
595 * Ok, this is non-standard.
597 * Sometimes we can't rely on the generic iommu-based DMA API
598 * to dynamically allocate the device address and then set the IOMMU
599 * tables accordingly, because some remote processors might
600 * _require_ us to use hard coded device addresses that their
601 * firmware was compiled with.
603 * In this case, we must use the IOMMU API directly and map
604 * the memory to the device address as expected by the remote
607 * Obviously such remote processor devices should not be configured
608 * to use the iommu-based DMA API: we expect 'dma' to contain the
609 * physical address in this case.
612 mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
614 dev_err(dev, "kzalloc mapping failed\n");
619 ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
622 dev_err(dev, "iommu_map failed: %d\n", ret);
627 * We'll need this info later when we'll want to unmap
628 * everything (e.g. on shutdown).
630 * We can't trust the remote processor not to change the
631 * resource table, so we must maintain this info independently.
633 mapping->da = rsc->da;
634 mapping->len = rsc->len;
635 list_add_tail(&mapping->node, &rproc->mappings);
637 dev_dbg(dev, "carveout mapped 0x%x to 0x%llx\n",
638 rsc->da, (unsigned long long)dma);
642 * Some remote processors might need to know the pa
643 * even though they are behind an IOMMU. E.g., OMAP4's
644 * remote M3 processor needs this so it can control
645 * on-chip hardware accelerators that are not behind
646 * the IOMMU, and therefor must know the pa.
648 * Generally we don't want to expose physical addresses
649 * if we don't have to (remote processors are generally
650 * _not_ trusted), so we might want to do this only for
651 * remote processor that _must_ have this (e.g. OMAP4's
652 * dual M3 subsystem).
654 * Non-IOMMU processors might also want to have this info.
655 * In this case, the device address and the physical address
661 carveout->len = rsc->len;
663 carveout->da = rsc->da;
665 list_add_tail(&carveout->node, &rproc->carveouts);
672 dma_free_coherent(dev->parent, rsc->len, va, dma);
678 static int rproc_count_vrings(struct rproc *rproc, struct fw_rsc_vdev *rsc,
679 int offset, int avail)
681 /* Summarize the number of notification IDs */
682 rproc->max_notifyid += rsc->num_of_vrings;
688 * A lookup table for resource handlers. The indices are defined in
689 * enum fw_resource_type.
691 static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
692 [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
693 [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
694 [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
695 [RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
698 static rproc_handle_resource_t rproc_vdev_handler[RSC_LAST] = {
699 [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
702 static rproc_handle_resource_t rproc_count_vrings_handler[RSC_LAST] = {
703 [RSC_VDEV] = (rproc_handle_resource_t)rproc_count_vrings,
706 /* handle firmware resource entries before booting the remote processor */
707 static int rproc_handle_resources(struct rproc *rproc, int len,
708 rproc_handle_resource_t handlers[RSC_LAST])
710 struct device *dev = &rproc->dev;
711 rproc_handle_resource_t handler;
714 for (i = 0; i < rproc->table_ptr->num; i++) {
715 int offset = rproc->table_ptr->offset[i];
716 struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
717 int avail = len - offset - sizeof(*hdr);
718 void *rsc = (void *)hdr + sizeof(*hdr);
720 /* make sure table isn't truncated */
722 dev_err(dev, "rsc table is truncated\n");
726 dev_dbg(dev, "rsc: type %d\n", hdr->type);
728 if (hdr->type >= RSC_LAST) {
729 dev_warn(dev, "unsupported resource %d\n", hdr->type);
733 handler = handlers[hdr->type];
737 ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
746 * rproc_resource_cleanup() - clean up and free all acquired resources
747 * @rproc: rproc handle
749 * This function will free all resources acquired for @rproc, and it
750 * is called whenever @rproc either shuts down or fails to boot.
752 static void rproc_resource_cleanup(struct rproc *rproc)
754 struct rproc_mem_entry *entry, *tmp;
755 struct device *dev = &rproc->dev;
757 /* clean up debugfs trace entries */
758 list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
759 rproc_remove_trace_file(entry->priv);
761 list_del(&entry->node);
765 /* clean up carveout allocations */
766 list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
767 dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
768 list_del(&entry->node);
772 /* clean up iommu mapping entries */
773 list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
776 unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
777 if (unmapped != entry->len) {
778 /* nothing much to do besides complaining */
779 dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
783 list_del(&entry->node);
789 * take a firmware and boot a remote processor with it.
791 static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
793 struct device *dev = &rproc->dev;
794 const char *name = rproc->firmware;
795 struct resource_table *table, *loaded_table;
798 if (!rproc->table_ptr)
801 ret = rproc_fw_sanity_check(rproc, fw);
805 dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
808 * if enabling an IOMMU isn't relevant for this rproc, this is
811 ret = rproc_enable_iommu(rproc);
813 dev_err(dev, "can't enable iommu: %d\n", ret);
817 rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
820 /* look for the resource table */
821 table = rproc_find_rsc_table(rproc, fw, &tablesz);
826 /* Verify that resource table in loaded fw is unchanged */
827 if (rproc->table_csum != crc32(0, table, tablesz)) {
828 dev_err(dev, "resource checksum failed, fw changed?\n");
832 /* handle fw resources which are required to boot rproc */
833 ret = rproc_handle_resources(rproc, tablesz, rproc_loading_handlers);
835 dev_err(dev, "Failed to process resources: %d\n", ret);
839 /* load the ELF segments to memory */
840 ret = rproc_load_segments(rproc, fw);
842 dev_err(dev, "Failed to load program segments: %d\n", ret);
847 * The starting device has been given the rproc->cached_table as the
848 * resource table. The address of the vring along with the other
849 * allocated resources (carveouts etc) is stored in cached_table.
850 * In order to pass this information to the remote device we must
851 * copy this information to device memory.
853 loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
859 memcpy(loaded_table, rproc->cached_table, tablesz);
861 /* power up the remote processor */
862 ret = rproc->ops->start(rproc);
864 dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
869 * Update table_ptr so that all subsequent vring allocations and
870 * virtio fields manipulation update the actual loaded resource table
873 rproc->table_ptr = loaded_table;
875 rproc->state = RPROC_RUNNING;
877 dev_info(dev, "remote processor %s is now up\n", rproc->name);
882 rproc_resource_cleanup(rproc);
883 rproc_disable_iommu(rproc);
888 * take a firmware and look for virtio devices to register.
890 * Note: this function is called asynchronously upon registration of the
891 * remote processor (so we must wait until it completes before we try
892 * to unregister the device. one other option is just to use kref here,
893 * that might be cleaner).
895 static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
897 struct rproc *rproc = context;
898 struct resource_table *table;
901 if (rproc_fw_sanity_check(rproc, fw) < 0)
904 /* look for the resource table */
905 table = rproc_find_rsc_table(rproc, fw, &tablesz);
909 rproc->table_csum = crc32(0, table, tablesz);
912 * Create a copy of the resource table. When a virtio device starts
913 * and calls vring_new_virtqueue() the address of the allocated vring
914 * will be stored in the cached_table. Before the device is started,
915 * cached_table will be copied into devic memory.
917 rproc->cached_table = kmalloc(tablesz, GFP_KERNEL);
918 if (!rproc->cached_table)
921 memcpy(rproc->cached_table, table, tablesz);
922 rproc->table_ptr = rproc->cached_table;
924 /* count the number of notify-ids */
925 rproc->max_notifyid = -1;
926 ret = rproc_handle_resources(rproc, tablesz, rproc_count_vrings_handler);
930 /* look for virtio devices and register them */
931 ret = rproc_handle_resources(rproc, tablesz, rproc_vdev_handler);
934 release_firmware(fw);
935 /* allow rproc_del() contexts, if any, to proceed */
936 complete_all(&rproc->firmware_loading_complete);
939 static int rproc_add_virtio_devices(struct rproc *rproc)
943 /* rproc_del() calls must wait until async loader completes */
944 init_completion(&rproc->firmware_loading_complete);
947 * We must retrieve early virtio configuration info from
948 * the firmware (e.g. whether to register a virtio device,
949 * what virtio features does it support, ...).
951 * We're initiating an asynchronous firmware loading, so we can
952 * be built-in kernel code, without hanging the boot process.
954 ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
955 rproc->firmware, &rproc->dev, GFP_KERNEL,
956 rproc, rproc_fw_config_virtio);
958 dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
959 complete_all(&rproc->firmware_loading_complete);
966 * rproc_trigger_recovery() - recover a remoteproc
967 * @rproc: the remote processor
969 * The recovery is done by reseting all the virtio devices, that way all the
970 * rpmsg drivers will be reseted along with the remote processor making the
971 * remoteproc functional again.
973 * This function can sleep, so it cannot be called from atomic context.
975 int rproc_trigger_recovery(struct rproc *rproc)
977 struct rproc_vdev *rvdev, *rvtmp;
979 dev_err(&rproc->dev, "recovering %s\n", rproc->name);
981 init_completion(&rproc->crash_comp);
983 /* clean up remote vdev entries */
984 list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
985 rproc_remove_virtio_dev(rvdev);
987 /* wait until there is no more rproc users */
988 wait_for_completion(&rproc->crash_comp);
990 /* Free the copy of the resource table */
991 kfree(rproc->cached_table);
993 return rproc_add_virtio_devices(rproc);
997 * rproc_crash_handler_work() - handle a crash
999 * This function needs to handle everything related to a crash, like cpu
1000 * registers and stack dump, information to help to debug the fatal error, etc.
1002 static void rproc_crash_handler_work(struct work_struct *work)
1004 struct rproc *rproc = container_of(work, struct rproc, crash_handler);
1005 struct device *dev = &rproc->dev;
1007 dev_dbg(dev, "enter %s\n", __func__);
1009 mutex_lock(&rproc->lock);
1011 if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
1012 /* handle only the first crash detected */
1013 mutex_unlock(&rproc->lock);
1017 rproc->state = RPROC_CRASHED;
1018 dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
1021 mutex_unlock(&rproc->lock);
1023 if (!rproc->recovery_disabled)
1024 rproc_trigger_recovery(rproc);
1028 * rproc_boot() - boot a remote processor
1029 * @rproc: handle of a remote processor
1031 * Boot a remote processor (i.e. load its firmware, power it on, ...).
1033 * If the remote processor is already powered on, this function immediately
1034 * returns (successfully).
1036 * Returns 0 on success, and an appropriate error value otherwise.
1038 int rproc_boot(struct rproc *rproc)
1040 const struct firmware *firmware_p;
1045 pr_err("invalid rproc handle\n");
1051 ret = mutex_lock_interruptible(&rproc->lock);
1053 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1057 /* loading a firmware is required */
1058 if (!rproc->firmware) {
1059 dev_err(dev, "%s: no firmware to load\n", __func__);
1064 /* prevent underlying implementation from being removed */
1065 if (!try_module_get(dev->parent->driver->owner)) {
1066 dev_err(dev, "%s: can't get owner\n", __func__);
1071 /* skip the boot process if rproc is already powered up */
1072 if (atomic_inc_return(&rproc->power) > 1) {
1077 dev_info(dev, "powering up %s\n", rproc->name);
1080 ret = request_firmware(&firmware_p, rproc->firmware, dev);
1082 dev_err(dev, "request_firmware failed: %d\n", ret);
1086 ret = rproc_fw_boot(rproc, firmware_p);
1088 release_firmware(firmware_p);
1092 module_put(dev->parent->driver->owner);
1093 atomic_dec(&rproc->power);
1096 mutex_unlock(&rproc->lock);
1099 EXPORT_SYMBOL(rproc_boot);
1102 * rproc_shutdown() - power off the remote processor
1103 * @rproc: the remote processor
1105 * Power off a remote processor (previously booted with rproc_boot()).
1107 * In case @rproc is still being used by an additional user(s), then
1108 * this function will just decrement the power refcount and exit,
1109 * without really powering off the device.
1111 * Every call to rproc_boot() must (eventually) be accompanied by a call
1112 * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1115 * - we're not decrementing the rproc's refcount, only the power refcount.
1116 * which means that the @rproc handle stays valid even after rproc_shutdown()
1117 * returns, and users can still use it with a subsequent rproc_boot(), if
1120 void rproc_shutdown(struct rproc *rproc)
1122 struct device *dev = &rproc->dev;
1125 ret = mutex_lock_interruptible(&rproc->lock);
1127 dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1131 /* if the remote proc is still needed, bail out */
1132 if (!atomic_dec_and_test(&rproc->power))
1135 /* power off the remote processor */
1136 ret = rproc->ops->stop(rproc);
1138 atomic_inc(&rproc->power);
1139 dev_err(dev, "can't stop rproc: %d\n", ret);
1143 /* clean up all acquired resources */
1144 rproc_resource_cleanup(rproc);
1146 rproc_disable_iommu(rproc);
1148 /* Give the next start a clean resource table */
1149 rproc->table_ptr = rproc->cached_table;
1151 /* if in crash state, unlock crash handler */
1152 if (rproc->state == RPROC_CRASHED)
1153 complete_all(&rproc->crash_comp);
1155 rproc->state = RPROC_OFFLINE;
1157 dev_info(dev, "stopped remote processor %s\n", rproc->name);
1160 mutex_unlock(&rproc->lock);
1162 module_put(dev->parent->driver->owner);
1164 EXPORT_SYMBOL(rproc_shutdown);
1167 * rproc_add() - register a remote processor
1168 * @rproc: the remote processor handle to register
1170 * Registers @rproc with the remoteproc framework, after it has been
1171 * allocated with rproc_alloc().
1173 * This is called by the platform-specific rproc implementation, whenever
1174 * a new remote processor device is probed.
1176 * Returns 0 on success and an appropriate error code otherwise.
1178 * Note: this function initiates an asynchronous firmware loading
1179 * context, which will look for virtio devices supported by the rproc's
1182 * If found, those virtio devices will be created and added, so as a result
1183 * of registering this remote processor, additional virtio drivers might be
1186 int rproc_add(struct rproc *rproc)
1188 struct device *dev = &rproc->dev;
1191 ret = device_add(dev);
1195 dev_info(dev, "%s is available\n", rproc->name);
1197 dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1198 dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1200 /* create debugfs entries */
1201 rproc_create_debug_dir(rproc);
1203 return rproc_add_virtio_devices(rproc);
1205 EXPORT_SYMBOL(rproc_add);
1208 * rproc_type_release() - release a remote processor instance
1209 * @dev: the rproc's device
1211 * This function should _never_ be called directly.
1213 * It will be called by the driver core when no one holds a valid pointer
1216 static void rproc_type_release(struct device *dev)
1218 struct rproc *rproc = container_of(dev, struct rproc, dev);
1220 dev_info(&rproc->dev, "releasing %s\n", rproc->name);
1222 rproc_delete_debug_dir(rproc);
1224 idr_destroy(&rproc->notifyids);
1226 if (rproc->index >= 0)
1227 ida_simple_remove(&rproc_dev_index, rproc->index);
1232 static struct device_type rproc_type = {
1233 .name = "remoteproc",
1234 .release = rproc_type_release,
1238 * rproc_alloc() - allocate a remote processor handle
1239 * @dev: the underlying device
1240 * @name: name of this remote processor
1241 * @ops: platform-specific handlers (mainly start/stop)
1242 * @firmware: name of firmware file to load, can be NULL
1243 * @len: length of private data needed by the rproc driver (in bytes)
1245 * Allocates a new remote processor handle, but does not register
1246 * it yet. if @firmware is NULL, a default name is used.
1248 * This function should be used by rproc implementations during initialization
1249 * of the remote processor.
1251 * After creating an rproc handle using this function, and when ready,
1252 * implementations should then call rproc_add() to complete
1253 * the registration of the remote processor.
1255 * On success the new rproc is returned, and on failure, NULL.
1257 * Note: _never_ directly deallocate @rproc, even if it was not registered
1258 * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
1260 struct rproc *rproc_alloc(struct device *dev, const char *name,
1261 const struct rproc_ops *ops,
1262 const char *firmware, int len)
1264 struct rproc *rproc;
1265 char *p, *template = "rproc-%s-fw";
1268 if (!dev || !name || !ops)
1273 * Make room for default firmware name (minus %s plus '\0').
1274 * If the caller didn't pass in a firmware name then
1275 * construct a default name. We're already glomming 'len'
1276 * bytes onto the end of the struct rproc allocation, so do
1277 * a few more for the default firmware name (but only if
1278 * the caller doesn't pass one).
1280 name_len = strlen(name) + strlen(template) - 2 + 1;
1282 rproc = kzalloc(sizeof(struct rproc) + len + name_len, GFP_KERNEL);
1284 dev_err(dev, "%s: kzalloc failed\n", __func__);
1289 p = (char *)rproc + sizeof(struct rproc) + len;
1290 snprintf(p, name_len, template, name);
1292 p = (char *)firmware;
1295 rproc->firmware = p;
1298 rproc->priv = &rproc[1];
1300 device_initialize(&rproc->dev);
1301 rproc->dev.parent = dev;
1302 rproc->dev.type = &rproc_type;
1304 /* Assign a unique device index and name */
1305 rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1306 if (rproc->index < 0) {
1307 dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1308 put_device(&rproc->dev);
1312 dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1314 atomic_set(&rproc->power, 0);
1316 /* Set ELF as the default fw_ops handler */
1317 rproc->fw_ops = &rproc_elf_fw_ops;
1319 mutex_init(&rproc->lock);
1321 idr_init(&rproc->notifyids);
1323 INIT_LIST_HEAD(&rproc->carveouts);
1324 INIT_LIST_HEAD(&rproc->mappings);
1325 INIT_LIST_HEAD(&rproc->traces);
1326 INIT_LIST_HEAD(&rproc->rvdevs);
1328 INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
1329 init_completion(&rproc->crash_comp);
1331 rproc->state = RPROC_OFFLINE;
1335 EXPORT_SYMBOL(rproc_alloc);
1338 * rproc_put() - unroll rproc_alloc()
1339 * @rproc: the remote processor handle
1341 * This function decrements the rproc dev refcount.
1343 * If no one holds any reference to rproc anymore, then its refcount would
1344 * now drop to zero, and it would be freed.
1346 void rproc_put(struct rproc *rproc)
1348 put_device(&rproc->dev);
1350 EXPORT_SYMBOL(rproc_put);
1353 * rproc_del() - unregister a remote processor
1354 * @rproc: rproc handle to unregister
1356 * This function should be called when the platform specific rproc
1357 * implementation decides to remove the rproc device. it should
1358 * _only_ be called if a previous invocation of rproc_add()
1359 * has completed successfully.
1361 * After rproc_del() returns, @rproc isn't freed yet, because
1362 * of the outstanding reference created by rproc_alloc. To decrement that
1363 * one last refcount, one still needs to call rproc_put().
1365 * Returns 0 on success and -EINVAL if @rproc isn't valid.
1367 int rproc_del(struct rproc *rproc)
1369 struct rproc_vdev *rvdev, *tmp;
1374 /* if rproc is just being registered, wait */
1375 wait_for_completion(&rproc->firmware_loading_complete);
1377 /* clean up remote vdev entries */
1378 list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
1379 rproc_remove_virtio_dev(rvdev);
1381 /* Free the copy of the resource table */
1382 kfree(rproc->cached_table);
1384 device_del(&rproc->dev);
1388 EXPORT_SYMBOL(rproc_del);
1391 * rproc_report_crash() - rproc crash reporter function
1392 * @rproc: remote processor
1395 * This function must be called every time a crash is detected by the low-level
1396 * drivers implementing a specific remoteproc. This should not be called from a
1397 * non-remoteproc driver.
1399 * This function can be called from atomic/interrupt context.
1401 void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
1404 pr_err("NULL rproc pointer\n");
1408 dev_err(&rproc->dev, "crash detected in %s: type %s\n",
1409 rproc->name, rproc_crash_to_string(type));
1411 /* create a new task to handle the error */
1412 schedule_work(&rproc->crash_handler);
1414 EXPORT_SYMBOL(rproc_report_crash);
1416 static int __init remoteproc_init(void)
1418 rproc_init_debugfs();
1422 module_init(remoteproc_init);
1424 static void __exit remoteproc_exit(void)
1426 rproc_exit_debugfs();
1428 module_exit(remoteproc_exit);
1430 MODULE_LICENSE("GPL v2");
1431 MODULE_DESCRIPTION("Generic Remote Processor Framework");