1400e64dfSOhad Ben-Cohen /* 2400e64dfSOhad Ben-Cohen * Remote Processor Framework 3400e64dfSOhad Ben-Cohen * 4400e64dfSOhad Ben-Cohen * Copyright (C) 2011 Texas Instruments, Inc. 5400e64dfSOhad Ben-Cohen * Copyright (C) 2011 Google, Inc. 6400e64dfSOhad Ben-Cohen * 7400e64dfSOhad Ben-Cohen * Ohad Ben-Cohen <ohad@wizery.com> 8400e64dfSOhad Ben-Cohen * Brian Swetland <swetland@google.com> 9400e64dfSOhad Ben-Cohen * Mark Grosen <mgrosen@ti.com> 10400e64dfSOhad Ben-Cohen * Fernando Guzman Lugo <fernando.lugo@ti.com> 11400e64dfSOhad Ben-Cohen * Suman Anna <s-anna@ti.com> 12400e64dfSOhad Ben-Cohen * Robert Tivy <rtivy@ti.com> 13400e64dfSOhad Ben-Cohen * Armando Uribe De Leon <x0095078@ti.com> 14400e64dfSOhad Ben-Cohen * 15400e64dfSOhad Ben-Cohen * This program is free software; you can redistribute it and/or 16400e64dfSOhad Ben-Cohen * modify it under the terms of the GNU General Public License 17400e64dfSOhad Ben-Cohen * version 2 as published by the Free Software Foundation. 18400e64dfSOhad Ben-Cohen * 19400e64dfSOhad Ben-Cohen * This program is distributed in the hope that it will be useful, 20400e64dfSOhad Ben-Cohen * but WITHOUT ANY WARRANTY; without even the implied warranty of 21400e64dfSOhad Ben-Cohen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22400e64dfSOhad Ben-Cohen * GNU General Public License for more details. 23400e64dfSOhad Ben-Cohen */ 24400e64dfSOhad Ben-Cohen 25400e64dfSOhad Ben-Cohen #define pr_fmt(fmt) "%s: " fmt, __func__ 26400e64dfSOhad Ben-Cohen 27400e64dfSOhad Ben-Cohen #include <linux/kernel.h> 28400e64dfSOhad Ben-Cohen #include <linux/module.h> 29400e64dfSOhad Ben-Cohen #include <linux/device.h> 30400e64dfSOhad Ben-Cohen #include <linux/slab.h> 31400e64dfSOhad Ben-Cohen #include <linux/mutex.h> 32400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h> 33400e64dfSOhad Ben-Cohen #include <linux/firmware.h> 34400e64dfSOhad Ben-Cohen #include <linux/string.h> 35400e64dfSOhad Ben-Cohen #include <linux/debugfs.h> 36400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h> 37400e64dfSOhad Ben-Cohen #include <linux/iommu.h> 38b5ab5e24SOhad Ben-Cohen #include <linux/idr.h> 39400e64dfSOhad Ben-Cohen #include <linux/elf.h> 40a2b950acSOhad Ben-Cohen #include <linux/crc32.h> 41400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h> 42400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h> 43cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h> 44400e64dfSOhad Ben-Cohen 45400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h" 46400e64dfSOhad Ben-Cohen 47fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex); 48fec47d86SDave Gerlach static LIST_HEAD(rproc_list); 49fec47d86SDave Gerlach 50400e64dfSOhad Ben-Cohen typedef int (*rproc_handle_resources_t)(struct rproc *rproc, 51fd2c15ecSOhad Ben-Cohen struct resource_table *table, int len); 52a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc, 53a2b950acSOhad Ben-Cohen void *, int offset, int avail); 54400e64dfSOhad Ben-Cohen 55b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */ 56b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index); 57b5ab5e24SOhad Ben-Cohen 588afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = { 598afd519cSFernando Guzman Lugo [RPROC_MMUFAULT] = "mmufault", 60b3d39032SBjorn Andersson [RPROC_WATCHDOG] = "watchdog", 61b3d39032SBjorn Andersson [RPROC_FATAL_ERROR] = "fatal error", 628afd519cSFernando Guzman Lugo }; 638afd519cSFernando Guzman Lugo 648afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */ 658afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type) 668afd519cSFernando Guzman Lugo { 678afd519cSFernando Guzman Lugo if (type < ARRAY_SIZE(rproc_crash_names)) 688afd519cSFernando Guzman Lugo return rproc_crash_names[type]; 69b23f7a09SMasanari Iida return "unknown"; 708afd519cSFernando Guzman Lugo } 718afd519cSFernando Guzman Lugo 72400e64dfSOhad Ben-Cohen /* 73400e64dfSOhad Ben-Cohen * This is the IOMMU fault handler we register with the IOMMU API 74400e64dfSOhad Ben-Cohen * (when relevant; not all remote processors access memory through 75400e64dfSOhad Ben-Cohen * an IOMMU). 76400e64dfSOhad Ben-Cohen * 77400e64dfSOhad Ben-Cohen * IOMMU core will invoke this handler whenever the remote processor 78400e64dfSOhad Ben-Cohen * will try to access an unmapped device address. 79400e64dfSOhad Ben-Cohen */ 80400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, 8177ca2332SOhad Ben-Cohen unsigned long iova, int flags, void *token) 82400e64dfSOhad Ben-Cohen { 838afd519cSFernando Guzman Lugo struct rproc *rproc = token; 848afd519cSFernando Guzman Lugo 85400e64dfSOhad Ben-Cohen dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags); 86400e64dfSOhad Ben-Cohen 878afd519cSFernando Guzman Lugo rproc_report_crash(rproc, RPROC_MMUFAULT); 888afd519cSFernando Guzman Lugo 89400e64dfSOhad Ben-Cohen /* 90400e64dfSOhad Ben-Cohen * Let the iommu core know we're not really handling this fault; 918afd519cSFernando Guzman Lugo * we just used it as a recovery trigger. 92400e64dfSOhad Ben-Cohen */ 93400e64dfSOhad Ben-Cohen return -ENOSYS; 94400e64dfSOhad Ben-Cohen } 95400e64dfSOhad Ben-Cohen 96400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc) 97400e64dfSOhad Ben-Cohen { 98400e64dfSOhad Ben-Cohen struct iommu_domain *domain; 99b5ab5e24SOhad Ben-Cohen struct device *dev = rproc->dev.parent; 100400e64dfSOhad Ben-Cohen int ret; 101400e64dfSOhad Ben-Cohen 102315491e5SSuman Anna if (!rproc->has_iommu) { 103315491e5SSuman Anna dev_dbg(dev, "iommu not present\n"); 1040798e1daSMark Grosen return 0; 105400e64dfSOhad Ben-Cohen } 106400e64dfSOhad Ben-Cohen 107400e64dfSOhad Ben-Cohen domain = iommu_domain_alloc(dev->bus); 108400e64dfSOhad Ben-Cohen if (!domain) { 109400e64dfSOhad Ben-Cohen dev_err(dev, "can't alloc iommu domain\n"); 110400e64dfSOhad Ben-Cohen return -ENOMEM; 111400e64dfSOhad Ben-Cohen } 112400e64dfSOhad Ben-Cohen 11377ca2332SOhad Ben-Cohen iommu_set_fault_handler(domain, rproc_iommu_fault, rproc); 114400e64dfSOhad Ben-Cohen 115400e64dfSOhad Ben-Cohen ret = iommu_attach_device(domain, dev); 116400e64dfSOhad Ben-Cohen if (ret) { 117400e64dfSOhad Ben-Cohen dev_err(dev, "can't attach iommu device: %d\n", ret); 118400e64dfSOhad Ben-Cohen goto free_domain; 119400e64dfSOhad Ben-Cohen } 120400e64dfSOhad Ben-Cohen 121400e64dfSOhad Ben-Cohen rproc->domain = domain; 122400e64dfSOhad Ben-Cohen 123400e64dfSOhad Ben-Cohen return 0; 124400e64dfSOhad Ben-Cohen 125400e64dfSOhad Ben-Cohen free_domain: 126400e64dfSOhad Ben-Cohen iommu_domain_free(domain); 127400e64dfSOhad Ben-Cohen return ret; 128400e64dfSOhad Ben-Cohen } 129400e64dfSOhad Ben-Cohen 130400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc) 131400e64dfSOhad Ben-Cohen { 132400e64dfSOhad Ben-Cohen struct iommu_domain *domain = rproc->domain; 133b5ab5e24SOhad Ben-Cohen struct device *dev = rproc->dev.parent; 134400e64dfSOhad Ben-Cohen 135400e64dfSOhad Ben-Cohen if (!domain) 136400e64dfSOhad Ben-Cohen return; 137400e64dfSOhad Ben-Cohen 138400e64dfSOhad Ben-Cohen iommu_detach_device(domain, dev); 139400e64dfSOhad Ben-Cohen iommu_domain_free(domain); 140400e64dfSOhad Ben-Cohen } 141400e64dfSOhad Ben-Cohen 142a01f7cd6SSuman Anna /** 143a01f7cd6SSuman Anna * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address 144a01f7cd6SSuman Anna * @rproc: handle of a remote processor 145a01f7cd6SSuman Anna * @da: remoteproc device address to translate 146a01f7cd6SSuman Anna * @len: length of the memory region @da is pointing to 147a01f7cd6SSuman Anna * 148400e64dfSOhad Ben-Cohen * Some remote processors will ask us to allocate them physically contiguous 149400e64dfSOhad Ben-Cohen * memory regions (which we call "carveouts"), and map them to specific 150a01f7cd6SSuman Anna * device addresses (which are hardcoded in the firmware). They may also have 151a01f7cd6SSuman Anna * dedicated memory regions internal to the processors, and use them either 152a01f7cd6SSuman Anna * exclusively or alongside carveouts. 153400e64dfSOhad Ben-Cohen * 154400e64dfSOhad Ben-Cohen * They may then ask us to copy objects into specific device addresses (e.g. 155400e64dfSOhad Ben-Cohen * code/data sections) or expose us certain symbols in other device address 156400e64dfSOhad Ben-Cohen * (e.g. their trace buffer). 157400e64dfSOhad Ben-Cohen * 158a01f7cd6SSuman Anna * This function is a helper function with which we can go over the allocated 159a01f7cd6SSuman Anna * carveouts and translate specific device addresses to kernel virtual addresses 160a01f7cd6SSuman Anna * so we can access the referenced memory. This function also allows to perform 161a01f7cd6SSuman Anna * translations on the internal remoteproc memory regions through a platform 162a01f7cd6SSuman Anna * implementation specific da_to_va ops, if present. 163a01f7cd6SSuman Anna * 164a01f7cd6SSuman Anna * The function returns a valid kernel address on success or NULL on failure. 165400e64dfSOhad Ben-Cohen * 166400e64dfSOhad Ben-Cohen * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, 167400e64dfSOhad Ben-Cohen * but only on kernel direct mapped RAM memory. Instead, we're just using 168a01f7cd6SSuman Anna * here the output of the DMA API for the carveouts, which should be more 169a01f7cd6SSuman Anna * correct. 170400e64dfSOhad Ben-Cohen */ 17172854fb0SSjur Brændeland void *rproc_da_to_va(struct rproc *rproc, u64 da, int len) 172400e64dfSOhad Ben-Cohen { 173400e64dfSOhad Ben-Cohen struct rproc_mem_entry *carveout; 174400e64dfSOhad Ben-Cohen void *ptr = NULL; 175400e64dfSOhad Ben-Cohen 176a01f7cd6SSuman Anna if (rproc->ops->da_to_va) { 177a01f7cd6SSuman Anna ptr = rproc->ops->da_to_va(rproc, da, len); 178a01f7cd6SSuman Anna if (ptr) 179a01f7cd6SSuman Anna goto out; 180a01f7cd6SSuman Anna } 181a01f7cd6SSuman Anna 182400e64dfSOhad Ben-Cohen list_for_each_entry(carveout, &rproc->carveouts, node) { 183400e64dfSOhad Ben-Cohen int offset = da - carveout->da; 184400e64dfSOhad Ben-Cohen 185400e64dfSOhad Ben-Cohen /* try next carveout if da is too small */ 186400e64dfSOhad Ben-Cohen if (offset < 0) 187400e64dfSOhad Ben-Cohen continue; 188400e64dfSOhad Ben-Cohen 189400e64dfSOhad Ben-Cohen /* try next carveout if da is too large */ 190400e64dfSOhad Ben-Cohen if (offset + len > carveout->len) 191400e64dfSOhad Ben-Cohen continue; 192400e64dfSOhad Ben-Cohen 193400e64dfSOhad Ben-Cohen ptr = carveout->va + offset; 194400e64dfSOhad Ben-Cohen 195400e64dfSOhad Ben-Cohen break; 196400e64dfSOhad Ben-Cohen } 197400e64dfSOhad Ben-Cohen 198a01f7cd6SSuman Anna out: 199400e64dfSOhad Ben-Cohen return ptr; 200400e64dfSOhad Ben-Cohen } 2014afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va); 202400e64dfSOhad Ben-Cohen 2036db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) 204400e64dfSOhad Ben-Cohen { 2057a186941SOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 206b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 2076db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 208c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 2097a186941SOhad Ben-Cohen dma_addr_t dma; 2107a186941SOhad Ben-Cohen void *va; 2117a186941SOhad Ben-Cohen int ret, size, notifyid; 212400e64dfSOhad Ben-Cohen 2136db20ea8SOhad Ben-Cohen /* actual size of vring (in bytes) */ 2146db20ea8SOhad Ben-Cohen size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); 2156db20ea8SOhad Ben-Cohen 2166db20ea8SOhad Ben-Cohen /* 2176db20ea8SOhad Ben-Cohen * Allocate non-cacheable memory for the vring. In the future 2186db20ea8SOhad Ben-Cohen * this call will also configure the IOMMU for us 2196db20ea8SOhad Ben-Cohen */ 220b5ab5e24SOhad Ben-Cohen va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL); 2216db20ea8SOhad Ben-Cohen if (!va) { 222b5ab5e24SOhad Ben-Cohen dev_err(dev->parent, "dma_alloc_coherent failed\n"); 2236db20ea8SOhad Ben-Cohen return -EINVAL; 2246db20ea8SOhad Ben-Cohen } 2256db20ea8SOhad Ben-Cohen 2266db20ea8SOhad Ben-Cohen /* 2276db20ea8SOhad Ben-Cohen * Assign an rproc-wide unique index for this vring 2286db20ea8SOhad Ben-Cohen * TODO: assign a notifyid for rvdev updates as well 2296db20ea8SOhad Ben-Cohen * TODO: support predefined notifyids (via resource table) 2306db20ea8SOhad Ben-Cohen */ 23115fc6110STejun Heo ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); 232b39599b7SSuman Anna if (ret < 0) { 23315fc6110STejun Heo dev_err(dev, "idr_alloc failed: %d\n", ret); 234b5ab5e24SOhad Ben-Cohen dma_free_coherent(dev->parent, size, va, dma); 2356db20ea8SOhad Ben-Cohen return ret; 2366db20ea8SOhad Ben-Cohen } 23715fc6110STejun Heo notifyid = ret; 2386db20ea8SOhad Ben-Cohen 2399d7814a9SAnna, Suman dev_dbg(dev, "vring%d: va %p dma %pad size 0x%x idr %d\n", 240b605ed8bSAnna, Suman i, va, &dma, size, notifyid); 2416db20ea8SOhad Ben-Cohen 2426db20ea8SOhad Ben-Cohen rvring->va = va; 2436db20ea8SOhad Ben-Cohen rvring->dma = dma; 2446db20ea8SOhad Ben-Cohen rvring->notifyid = notifyid; 2456db20ea8SOhad Ben-Cohen 246c0d63157SSjur Brændeland /* 247c0d63157SSjur Brændeland * Let the rproc know the notifyid and da of this vring. 248c0d63157SSjur Brændeland * Not all platforms use dma_alloc_coherent to automatically 249c0d63157SSjur Brændeland * set up the iommu. In this case the device address (da) will 250c0d63157SSjur Brændeland * hold the physical address and not the device address. 251c0d63157SSjur Brændeland */ 252c0d63157SSjur Brændeland rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; 253c0d63157SSjur Brændeland rsc->vring[i].da = dma; 254c0d63157SSjur Brændeland rsc->vring[i].notifyid = notifyid; 2556db20ea8SOhad Ben-Cohen return 0; 2566db20ea8SOhad Ben-Cohen } 2576db20ea8SOhad Ben-Cohen 258400e64dfSOhad Ben-Cohen static int 2596db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) 260400e64dfSOhad Ben-Cohen { 261400e64dfSOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 262b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 263400e64dfSOhad Ben-Cohen struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; 2646db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 265400e64dfSOhad Ben-Cohen 2669d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n", 2677a186941SOhad Ben-Cohen i, vring->da, vring->num, vring->align); 2687a186941SOhad Ben-Cohen 26963140e0eSOhad Ben-Cohen /* verify queue size and vring alignment are sane */ 27063140e0eSOhad Ben-Cohen if (!vring->num || !vring->align) { 27163140e0eSOhad Ben-Cohen dev_err(dev, "invalid qsz (%d) or alignment (%d)\n", 27263140e0eSOhad Ben-Cohen vring->num, vring->align); 273400e64dfSOhad Ben-Cohen return -EINVAL; 274400e64dfSOhad Ben-Cohen } 275400e64dfSOhad Ben-Cohen 2766db20ea8SOhad Ben-Cohen rvring->len = vring->num; 2776db20ea8SOhad Ben-Cohen rvring->align = vring->align; 2786db20ea8SOhad Ben-Cohen rvring->rvdev = rvdev; 279400e64dfSOhad Ben-Cohen 280400e64dfSOhad Ben-Cohen return 0; 281400e64dfSOhad Ben-Cohen } 282400e64dfSOhad Ben-Cohen 2836db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring) 2847a186941SOhad Ben-Cohen { 28563140e0eSOhad Ben-Cohen int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); 2866db20ea8SOhad Ben-Cohen struct rproc *rproc = rvring->rvdev->rproc; 287c0d63157SSjur Brændeland int idx = rvring->rvdev->vring - rvring; 288c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 2897a186941SOhad Ben-Cohen 290b5ab5e24SOhad Ben-Cohen dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma); 2917a186941SOhad Ben-Cohen idr_remove(&rproc->notifyids, rvring->notifyid); 292099a3f33SSjur Brændeland 293c0d63157SSjur Brændeland /* reset resource entry info */ 294c0d63157SSjur Brændeland rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; 295c0d63157SSjur Brændeland rsc->vring[idx].da = 0; 296c0d63157SSjur Brændeland rsc->vring[idx].notifyid = -1; 2977a186941SOhad Ben-Cohen } 2987a186941SOhad Ben-Cohen 299400e64dfSOhad Ben-Cohen /** 300fd2c15ecSOhad Ben-Cohen * rproc_handle_vdev() - handle a vdev fw resource 301400e64dfSOhad Ben-Cohen * @rproc: the remote processor 302400e64dfSOhad Ben-Cohen * @rsc: the vring resource descriptor 303fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 304400e64dfSOhad Ben-Cohen * 3057a186941SOhad Ben-Cohen * This resource entry requests the host to statically register a virtio 3067a186941SOhad Ben-Cohen * device (vdev), and setup everything needed to support it. It contains 3077a186941SOhad Ben-Cohen * everything needed to make it possible: the virtio device id, virtio 3087a186941SOhad Ben-Cohen * device features, vrings information, virtio config space, etc... 309400e64dfSOhad Ben-Cohen * 3107a186941SOhad Ben-Cohen * Before registering the vdev, the vrings are allocated from non-cacheable 3117a186941SOhad Ben-Cohen * physically contiguous memory. Currently we only support two vrings per 3127a186941SOhad Ben-Cohen * remote processor (temporary limitation). We might also want to consider 3137a186941SOhad Ben-Cohen * doing the vring allocation only later when ->find_vqs() is invoked, and 3147a186941SOhad Ben-Cohen * then release them upon ->del_vqs(). 315400e64dfSOhad Ben-Cohen * 3167a186941SOhad Ben-Cohen * Note: @da is currently not really handled correctly: we dynamically 3177a186941SOhad Ben-Cohen * allocate it using the DMA API, ignoring requested hard coded addresses, 3187a186941SOhad Ben-Cohen * and we don't take care of any required IOMMU programming. This is all 3197a186941SOhad Ben-Cohen * going to be taken care of when the generic iommu-based DMA API will be 3207a186941SOhad Ben-Cohen * merged. Meanwhile, statically-addressed iommu-based firmware images should 3217a186941SOhad Ben-Cohen * use RSC_DEVMEM resource entries to map their required @da to the physical 3227a186941SOhad Ben-Cohen * address of their base CMA region (ouch, hacky!). 323400e64dfSOhad Ben-Cohen * 324400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 325400e64dfSOhad Ben-Cohen */ 326fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc, 327a2b950acSOhad Ben-Cohen int offset, int avail) 328400e64dfSOhad Ben-Cohen { 329b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 3307a186941SOhad Ben-Cohen struct rproc_vdev *rvdev; 3317a186941SOhad Ben-Cohen int i, ret; 332fd2c15ecSOhad Ben-Cohen 333fd2c15ecSOhad Ben-Cohen /* make sure resource isn't truncated */ 334fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring) 335fd2c15ecSOhad Ben-Cohen + rsc->config_len > avail) { 336b5ab5e24SOhad Ben-Cohen dev_err(dev, "vdev rsc is truncated\n"); 337fd2c15ecSOhad Ben-Cohen return -EINVAL; 338fd2c15ecSOhad Ben-Cohen } 339fd2c15ecSOhad Ben-Cohen 340fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 341fd2c15ecSOhad Ben-Cohen if (rsc->reserved[0] || rsc->reserved[1]) { 342fd2c15ecSOhad Ben-Cohen dev_err(dev, "vdev rsc has non zero reserved bytes\n"); 343fd2c15ecSOhad Ben-Cohen return -EINVAL; 344fd2c15ecSOhad Ben-Cohen } 345fd2c15ecSOhad Ben-Cohen 3469d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n", 347fd2c15ecSOhad Ben-Cohen rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); 348400e64dfSOhad Ben-Cohen 3497a186941SOhad Ben-Cohen /* we currently support only two vrings per rvdev */ 3507a186941SOhad Ben-Cohen if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { 351fd2c15ecSOhad Ben-Cohen dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); 352400e64dfSOhad Ben-Cohen return -EINVAL; 353400e64dfSOhad Ben-Cohen } 354400e64dfSOhad Ben-Cohen 355899585adSAnna, Suman rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL); 3567a186941SOhad Ben-Cohen if (!rvdev) 3577a186941SOhad Ben-Cohen return -ENOMEM; 3587a186941SOhad Ben-Cohen 3597a186941SOhad Ben-Cohen rvdev->rproc = rproc; 3607a186941SOhad Ben-Cohen 3616db20ea8SOhad Ben-Cohen /* parse the vrings */ 362fd2c15ecSOhad Ben-Cohen for (i = 0; i < rsc->num_of_vrings; i++) { 3636db20ea8SOhad Ben-Cohen ret = rproc_parse_vring(rvdev, rsc, i); 3647a186941SOhad Ben-Cohen if (ret) 3656db20ea8SOhad Ben-Cohen goto free_rvdev; 366fd2c15ecSOhad Ben-Cohen } 367fd2c15ecSOhad Ben-Cohen 368a2b950acSOhad Ben-Cohen /* remember the resource offset*/ 369a2b950acSOhad Ben-Cohen rvdev->rsc_offset = offset; 370400e64dfSOhad Ben-Cohen 3717a186941SOhad Ben-Cohen list_add_tail(&rvdev->node, &rproc->rvdevs); 372400e64dfSOhad Ben-Cohen 3737a186941SOhad Ben-Cohen /* it is now safe to add the virtio device */ 3747a186941SOhad Ben-Cohen ret = rproc_add_virtio_dev(rvdev, rsc->id); 3757a186941SOhad Ben-Cohen if (ret) 376cde42e07SSjur Brændeland goto remove_rvdev; 377400e64dfSOhad Ben-Cohen 378400e64dfSOhad Ben-Cohen return 0; 3797a186941SOhad Ben-Cohen 380cde42e07SSjur Brændeland remove_rvdev: 381cde42e07SSjur Brændeland list_del(&rvdev->node); 3826db20ea8SOhad Ben-Cohen free_rvdev: 3837a186941SOhad Ben-Cohen kfree(rvdev); 3847a186941SOhad Ben-Cohen return ret; 385400e64dfSOhad Ben-Cohen } 386400e64dfSOhad Ben-Cohen 387400e64dfSOhad Ben-Cohen /** 388400e64dfSOhad Ben-Cohen * rproc_handle_trace() - handle a shared trace buffer resource 389400e64dfSOhad Ben-Cohen * @rproc: the remote processor 390400e64dfSOhad Ben-Cohen * @rsc: the trace resource descriptor 391fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 392400e64dfSOhad Ben-Cohen * 393400e64dfSOhad Ben-Cohen * In case the remote processor dumps trace logs into memory, 394400e64dfSOhad Ben-Cohen * export it via debugfs. 395400e64dfSOhad Ben-Cohen * 396400e64dfSOhad Ben-Cohen * Currently, the 'da' member of @rsc should contain the device address 397400e64dfSOhad Ben-Cohen * where the remote processor is dumping the traces. Later we could also 398400e64dfSOhad Ben-Cohen * support dynamically allocating this address using the generic 399400e64dfSOhad Ben-Cohen * DMA API (but currently there isn't a use case for that). 400400e64dfSOhad Ben-Cohen * 401400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 402400e64dfSOhad Ben-Cohen */ 403fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc, 404a2b950acSOhad Ben-Cohen int offset, int avail) 405400e64dfSOhad Ben-Cohen { 406400e64dfSOhad Ben-Cohen struct rproc_mem_entry *trace; 407b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 408400e64dfSOhad Ben-Cohen void *ptr; 409400e64dfSOhad Ben-Cohen char name[15]; 410400e64dfSOhad Ben-Cohen 411fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 412b5ab5e24SOhad Ben-Cohen dev_err(dev, "trace rsc is truncated\n"); 413fd2c15ecSOhad Ben-Cohen return -EINVAL; 414fd2c15ecSOhad Ben-Cohen } 415fd2c15ecSOhad Ben-Cohen 416fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 417fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 418fd2c15ecSOhad Ben-Cohen dev_err(dev, "trace rsc has non zero reserved bytes\n"); 419fd2c15ecSOhad Ben-Cohen return -EINVAL; 420fd2c15ecSOhad Ben-Cohen } 421fd2c15ecSOhad Ben-Cohen 422400e64dfSOhad Ben-Cohen /* what's the kernel address of this resource ? */ 423400e64dfSOhad Ben-Cohen ptr = rproc_da_to_va(rproc, rsc->da, rsc->len); 424400e64dfSOhad Ben-Cohen if (!ptr) { 425400e64dfSOhad Ben-Cohen dev_err(dev, "erroneous trace resource entry\n"); 426400e64dfSOhad Ben-Cohen return -EINVAL; 427400e64dfSOhad Ben-Cohen } 428400e64dfSOhad Ben-Cohen 429400e64dfSOhad Ben-Cohen trace = kzalloc(sizeof(*trace), GFP_KERNEL); 430172e6ab1SSuman Anna if (!trace) 431400e64dfSOhad Ben-Cohen return -ENOMEM; 432400e64dfSOhad Ben-Cohen 433400e64dfSOhad Ben-Cohen /* set the trace buffer dma properties */ 434400e64dfSOhad Ben-Cohen trace->len = rsc->len; 435400e64dfSOhad Ben-Cohen trace->va = ptr; 436400e64dfSOhad Ben-Cohen 437400e64dfSOhad Ben-Cohen /* make sure snprintf always null terminates, even if truncating */ 438400e64dfSOhad Ben-Cohen snprintf(name, sizeof(name), "trace%d", rproc->num_traces); 439400e64dfSOhad Ben-Cohen 440400e64dfSOhad Ben-Cohen /* create the debugfs entry */ 441400e64dfSOhad Ben-Cohen trace->priv = rproc_create_trace_file(name, rproc, trace); 442400e64dfSOhad Ben-Cohen if (!trace->priv) { 443400e64dfSOhad Ben-Cohen trace->va = NULL; 444400e64dfSOhad Ben-Cohen kfree(trace); 445400e64dfSOhad Ben-Cohen return -EINVAL; 446400e64dfSOhad Ben-Cohen } 447400e64dfSOhad Ben-Cohen 448400e64dfSOhad Ben-Cohen list_add_tail(&trace->node, &rproc->traces); 449400e64dfSOhad Ben-Cohen 450400e64dfSOhad Ben-Cohen rproc->num_traces++; 451400e64dfSOhad Ben-Cohen 45235386166SLee Jones dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", 45335386166SLee Jones name, ptr, rsc->da, rsc->len); 454400e64dfSOhad Ben-Cohen 455400e64dfSOhad Ben-Cohen return 0; 456400e64dfSOhad Ben-Cohen } 457400e64dfSOhad Ben-Cohen 458400e64dfSOhad Ben-Cohen /** 459400e64dfSOhad Ben-Cohen * rproc_handle_devmem() - handle devmem resource entry 460400e64dfSOhad Ben-Cohen * @rproc: remote processor handle 461400e64dfSOhad Ben-Cohen * @rsc: the devmem resource entry 462fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 463400e64dfSOhad Ben-Cohen * 464400e64dfSOhad Ben-Cohen * Remote processors commonly need to access certain on-chip peripherals. 465400e64dfSOhad Ben-Cohen * 466400e64dfSOhad Ben-Cohen * Some of these remote processors access memory via an iommu device, 467400e64dfSOhad Ben-Cohen * and might require us to configure their iommu before they can access 468400e64dfSOhad Ben-Cohen * the on-chip peripherals they need. 469400e64dfSOhad Ben-Cohen * 470400e64dfSOhad Ben-Cohen * This resource entry is a request to map such a peripheral device. 471400e64dfSOhad Ben-Cohen * 472400e64dfSOhad Ben-Cohen * These devmem entries will contain the physical address of the device in 473400e64dfSOhad Ben-Cohen * the 'pa' member. If a specific device address is expected, then 'da' will 474400e64dfSOhad Ben-Cohen * contain it (currently this is the only use case supported). 'len' will 475400e64dfSOhad Ben-Cohen * contain the size of the physical region we need to map. 476400e64dfSOhad Ben-Cohen * 477400e64dfSOhad Ben-Cohen * Currently we just "trust" those devmem entries to contain valid physical 478400e64dfSOhad Ben-Cohen * addresses, but this is going to change: we want the implementations to 479400e64dfSOhad Ben-Cohen * tell us ranges of physical addresses the firmware is allowed to request, 480400e64dfSOhad Ben-Cohen * and not allow firmwares to request access to physical addresses that 481400e64dfSOhad Ben-Cohen * are outside those ranges. 482400e64dfSOhad Ben-Cohen */ 483fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc, 484a2b950acSOhad Ben-Cohen int offset, int avail) 485400e64dfSOhad Ben-Cohen { 486400e64dfSOhad Ben-Cohen struct rproc_mem_entry *mapping; 487b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 488400e64dfSOhad Ben-Cohen int ret; 489400e64dfSOhad Ben-Cohen 490400e64dfSOhad Ben-Cohen /* no point in handling this resource without a valid iommu domain */ 491400e64dfSOhad Ben-Cohen if (!rproc->domain) 492400e64dfSOhad Ben-Cohen return -EINVAL; 493400e64dfSOhad Ben-Cohen 494fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 495b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc is truncated\n"); 496fd2c15ecSOhad Ben-Cohen return -EINVAL; 497fd2c15ecSOhad Ben-Cohen } 498fd2c15ecSOhad Ben-Cohen 499fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 500fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 501b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc has non zero reserved bytes\n"); 502fd2c15ecSOhad Ben-Cohen return -EINVAL; 503fd2c15ecSOhad Ben-Cohen } 504fd2c15ecSOhad Ben-Cohen 505400e64dfSOhad Ben-Cohen mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 506172e6ab1SSuman Anna if (!mapping) 507400e64dfSOhad Ben-Cohen return -ENOMEM; 508400e64dfSOhad Ben-Cohen 509400e64dfSOhad Ben-Cohen ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); 510400e64dfSOhad Ben-Cohen if (ret) { 511b5ab5e24SOhad Ben-Cohen dev_err(dev, "failed to map devmem: %d\n", ret); 512400e64dfSOhad Ben-Cohen goto out; 513400e64dfSOhad Ben-Cohen } 514400e64dfSOhad Ben-Cohen 515400e64dfSOhad Ben-Cohen /* 516400e64dfSOhad Ben-Cohen * We'll need this info later when we'll want to unmap everything 517400e64dfSOhad Ben-Cohen * (e.g. on shutdown). 518400e64dfSOhad Ben-Cohen * 519400e64dfSOhad Ben-Cohen * We can't trust the remote processor not to change the resource 520400e64dfSOhad Ben-Cohen * table, so we must maintain this info independently. 521400e64dfSOhad Ben-Cohen */ 522400e64dfSOhad Ben-Cohen mapping->da = rsc->da; 523400e64dfSOhad Ben-Cohen mapping->len = rsc->len; 524400e64dfSOhad Ben-Cohen list_add_tail(&mapping->node, &rproc->mappings); 525400e64dfSOhad Ben-Cohen 526b5ab5e24SOhad Ben-Cohen dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", 527400e64dfSOhad Ben-Cohen rsc->pa, rsc->da, rsc->len); 528400e64dfSOhad Ben-Cohen 529400e64dfSOhad Ben-Cohen return 0; 530400e64dfSOhad Ben-Cohen 531400e64dfSOhad Ben-Cohen out: 532400e64dfSOhad Ben-Cohen kfree(mapping); 533400e64dfSOhad Ben-Cohen return ret; 534400e64dfSOhad Ben-Cohen } 535400e64dfSOhad Ben-Cohen 536400e64dfSOhad Ben-Cohen /** 537400e64dfSOhad Ben-Cohen * rproc_handle_carveout() - handle phys contig memory allocation requests 538400e64dfSOhad Ben-Cohen * @rproc: rproc handle 539400e64dfSOhad Ben-Cohen * @rsc: the resource entry 540fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for image validation) 541400e64dfSOhad Ben-Cohen * 542400e64dfSOhad Ben-Cohen * This function will handle firmware requests for allocation of physically 543400e64dfSOhad Ben-Cohen * contiguous memory regions. 544400e64dfSOhad Ben-Cohen * 545400e64dfSOhad Ben-Cohen * These request entries should come first in the firmware's resource table, 546400e64dfSOhad Ben-Cohen * as other firmware entries might request placing other data objects inside 547400e64dfSOhad Ben-Cohen * these memory regions (e.g. data/code segments, trace resource entries, ...). 548400e64dfSOhad Ben-Cohen * 549400e64dfSOhad Ben-Cohen * Allocating memory this way helps utilizing the reserved physical memory 550400e64dfSOhad Ben-Cohen * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries 551400e64dfSOhad Ben-Cohen * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB 552400e64dfSOhad Ben-Cohen * pressure is important; it may have a substantial impact on performance. 553400e64dfSOhad Ben-Cohen */ 554fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc, 555a2b950acSOhad Ben-Cohen struct fw_rsc_carveout *rsc, 556a2b950acSOhad Ben-Cohen int offset, int avail) 557400e64dfSOhad Ben-Cohen { 558400e64dfSOhad Ben-Cohen struct rproc_mem_entry *carveout, *mapping; 559b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 560400e64dfSOhad Ben-Cohen dma_addr_t dma; 561400e64dfSOhad Ben-Cohen void *va; 562400e64dfSOhad Ben-Cohen int ret; 563400e64dfSOhad Ben-Cohen 564fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 565b5ab5e24SOhad Ben-Cohen dev_err(dev, "carveout rsc is truncated\n"); 566fd2c15ecSOhad Ben-Cohen return -EINVAL; 567fd2c15ecSOhad Ben-Cohen } 568fd2c15ecSOhad Ben-Cohen 569fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 570fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 571fd2c15ecSOhad Ben-Cohen dev_err(dev, "carveout rsc has non zero reserved bytes\n"); 572fd2c15ecSOhad Ben-Cohen return -EINVAL; 573fd2c15ecSOhad Ben-Cohen } 574fd2c15ecSOhad Ben-Cohen 5759d7814a9SAnna, Suman dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n", 57635386166SLee Jones rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); 577fd2c15ecSOhad Ben-Cohen 578400e64dfSOhad Ben-Cohen carveout = kzalloc(sizeof(*carveout), GFP_KERNEL); 579172e6ab1SSuman Anna if (!carveout) 5807168d914SDan Carpenter return -ENOMEM; 581400e64dfSOhad Ben-Cohen 582b5ab5e24SOhad Ben-Cohen va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL); 583400e64dfSOhad Ben-Cohen if (!va) { 5849c219b23SLee Jones dev_err(dev->parent, 5859c219b23SLee Jones "failed to allocate dma memory: len 0x%x\n", rsc->len); 586400e64dfSOhad Ben-Cohen ret = -ENOMEM; 587400e64dfSOhad Ben-Cohen goto free_carv; 588400e64dfSOhad Ben-Cohen } 589400e64dfSOhad Ben-Cohen 590b605ed8bSAnna, Suman dev_dbg(dev, "carveout va %p, dma %pad, len 0x%x\n", 591b605ed8bSAnna, Suman va, &dma, rsc->len); 592400e64dfSOhad Ben-Cohen 593400e64dfSOhad Ben-Cohen /* 594400e64dfSOhad Ben-Cohen * Ok, this is non-standard. 595400e64dfSOhad Ben-Cohen * 596400e64dfSOhad Ben-Cohen * Sometimes we can't rely on the generic iommu-based DMA API 597400e64dfSOhad Ben-Cohen * to dynamically allocate the device address and then set the IOMMU 598400e64dfSOhad Ben-Cohen * tables accordingly, because some remote processors might 599400e64dfSOhad Ben-Cohen * _require_ us to use hard coded device addresses that their 600400e64dfSOhad Ben-Cohen * firmware was compiled with. 601400e64dfSOhad Ben-Cohen * 602400e64dfSOhad Ben-Cohen * In this case, we must use the IOMMU API directly and map 603400e64dfSOhad Ben-Cohen * the memory to the device address as expected by the remote 604400e64dfSOhad Ben-Cohen * processor. 605400e64dfSOhad Ben-Cohen * 606400e64dfSOhad Ben-Cohen * Obviously such remote processor devices should not be configured 607400e64dfSOhad Ben-Cohen * to use the iommu-based DMA API: we expect 'dma' to contain the 608400e64dfSOhad Ben-Cohen * physical address in this case. 609400e64dfSOhad Ben-Cohen */ 610400e64dfSOhad Ben-Cohen if (rproc->domain) { 6117168d914SDan Carpenter mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 6127168d914SDan Carpenter if (!mapping) { 6137168d914SDan Carpenter ret = -ENOMEM; 6147168d914SDan Carpenter goto dma_free; 6157168d914SDan Carpenter } 6167168d914SDan Carpenter 617400e64dfSOhad Ben-Cohen ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len, 618400e64dfSOhad Ben-Cohen rsc->flags); 619400e64dfSOhad Ben-Cohen if (ret) { 620400e64dfSOhad Ben-Cohen dev_err(dev, "iommu_map failed: %d\n", ret); 6217168d914SDan Carpenter goto free_mapping; 622400e64dfSOhad Ben-Cohen } 623400e64dfSOhad Ben-Cohen 624400e64dfSOhad Ben-Cohen /* 625400e64dfSOhad Ben-Cohen * We'll need this info later when we'll want to unmap 626400e64dfSOhad Ben-Cohen * everything (e.g. on shutdown). 627400e64dfSOhad Ben-Cohen * 628400e64dfSOhad Ben-Cohen * We can't trust the remote processor not to change the 629400e64dfSOhad Ben-Cohen * resource table, so we must maintain this info independently. 630400e64dfSOhad Ben-Cohen */ 631400e64dfSOhad Ben-Cohen mapping->da = rsc->da; 632400e64dfSOhad Ben-Cohen mapping->len = rsc->len; 633400e64dfSOhad Ben-Cohen list_add_tail(&mapping->node, &rproc->mappings); 634400e64dfSOhad Ben-Cohen 635b605ed8bSAnna, Suman dev_dbg(dev, "carveout mapped 0x%x to %pad\n", 636b605ed8bSAnna, Suman rsc->da, &dma); 6370e49b72cSOhad Ben-Cohen } 638400e64dfSOhad Ben-Cohen 639400e64dfSOhad Ben-Cohen /* 640400e64dfSOhad Ben-Cohen * Some remote processors might need to know the pa 641400e64dfSOhad Ben-Cohen * even though they are behind an IOMMU. E.g., OMAP4's 642400e64dfSOhad Ben-Cohen * remote M3 processor needs this so it can control 643400e64dfSOhad Ben-Cohen * on-chip hardware accelerators that are not behind 644400e64dfSOhad Ben-Cohen * the IOMMU, and therefor must know the pa. 645400e64dfSOhad Ben-Cohen * 646400e64dfSOhad Ben-Cohen * Generally we don't want to expose physical addresses 647400e64dfSOhad Ben-Cohen * if we don't have to (remote processors are generally 648400e64dfSOhad Ben-Cohen * _not_ trusted), so we might want to do this only for 649400e64dfSOhad Ben-Cohen * remote processor that _must_ have this (e.g. OMAP4's 650400e64dfSOhad Ben-Cohen * dual M3 subsystem). 6510e49b72cSOhad Ben-Cohen * 6520e49b72cSOhad Ben-Cohen * Non-IOMMU processors might also want to have this info. 6530e49b72cSOhad Ben-Cohen * In this case, the device address and the physical address 6540e49b72cSOhad Ben-Cohen * are the same. 655400e64dfSOhad Ben-Cohen */ 656400e64dfSOhad Ben-Cohen rsc->pa = dma; 657400e64dfSOhad Ben-Cohen 658400e64dfSOhad Ben-Cohen carveout->va = va; 659400e64dfSOhad Ben-Cohen carveout->len = rsc->len; 660400e64dfSOhad Ben-Cohen carveout->dma = dma; 661400e64dfSOhad Ben-Cohen carveout->da = rsc->da; 662400e64dfSOhad Ben-Cohen 663400e64dfSOhad Ben-Cohen list_add_tail(&carveout->node, &rproc->carveouts); 664400e64dfSOhad Ben-Cohen 665400e64dfSOhad Ben-Cohen return 0; 666400e64dfSOhad Ben-Cohen 6677168d914SDan Carpenter free_mapping: 6687168d914SDan Carpenter kfree(mapping); 669400e64dfSOhad Ben-Cohen dma_free: 670b5ab5e24SOhad Ben-Cohen dma_free_coherent(dev->parent, rsc->len, va, dma); 671400e64dfSOhad Ben-Cohen free_carv: 672400e64dfSOhad Ben-Cohen kfree(carveout); 673400e64dfSOhad Ben-Cohen return ret; 674400e64dfSOhad Ben-Cohen } 675400e64dfSOhad Ben-Cohen 676ba7290e0SSjur Brændeland static int rproc_count_vrings(struct rproc *rproc, struct fw_rsc_vdev *rsc, 677a2b950acSOhad Ben-Cohen int offset, int avail) 678ba7290e0SSjur Brændeland { 679ba7290e0SSjur Brændeland /* Summarize the number of notification IDs */ 680ba7290e0SSjur Brændeland rproc->max_notifyid += rsc->num_of_vrings; 681ba7290e0SSjur Brændeland 682ba7290e0SSjur Brændeland return 0; 683ba7290e0SSjur Brændeland } 684ba7290e0SSjur Brændeland 685e12bc14bSOhad Ben-Cohen /* 686e12bc14bSOhad Ben-Cohen * A lookup table for resource handlers. The indices are defined in 687e12bc14bSOhad Ben-Cohen * enum fw_resource_type. 688e12bc14bSOhad Ben-Cohen */ 689232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { 690fd2c15ecSOhad Ben-Cohen [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout, 691fd2c15ecSOhad Ben-Cohen [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem, 692fd2c15ecSOhad Ben-Cohen [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace, 693b35d7afcSBjorn Andersson [RSC_VDEV] = (rproc_handle_resource_t)rproc_count_vrings, 694e12bc14bSOhad Ben-Cohen }; 695e12bc14bSOhad Ben-Cohen 696232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_vdev_handler[RSC_LAST] = { 697232fcdbbSSjur Brændeland [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev, 698232fcdbbSSjur Brændeland }; 699232fcdbbSSjur Brændeland 700400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */ 701a2b950acSOhad Ben-Cohen static int rproc_handle_resources(struct rproc *rproc, int len, 702232fcdbbSSjur Brændeland rproc_handle_resource_t handlers[RSC_LAST]) 703400e64dfSOhad Ben-Cohen { 704b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 705e12bc14bSOhad Ben-Cohen rproc_handle_resource_t handler; 706fd2c15ecSOhad Ben-Cohen int ret = 0, i; 707400e64dfSOhad Ben-Cohen 708a2b950acSOhad Ben-Cohen for (i = 0; i < rproc->table_ptr->num; i++) { 709a2b950acSOhad Ben-Cohen int offset = rproc->table_ptr->offset[i]; 710a2b950acSOhad Ben-Cohen struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; 711fd2c15ecSOhad Ben-Cohen int avail = len - offset - sizeof(*hdr); 712fd2c15ecSOhad Ben-Cohen void *rsc = (void *)hdr + sizeof(*hdr); 713400e64dfSOhad Ben-Cohen 714fd2c15ecSOhad Ben-Cohen /* make sure table isn't truncated */ 715fd2c15ecSOhad Ben-Cohen if (avail < 0) { 716fd2c15ecSOhad Ben-Cohen dev_err(dev, "rsc table is truncated\n"); 717fd2c15ecSOhad Ben-Cohen return -EINVAL; 718fd2c15ecSOhad Ben-Cohen } 719fd2c15ecSOhad Ben-Cohen 720fd2c15ecSOhad Ben-Cohen dev_dbg(dev, "rsc: type %d\n", hdr->type); 721fd2c15ecSOhad Ben-Cohen 722fd2c15ecSOhad Ben-Cohen if (hdr->type >= RSC_LAST) { 723fd2c15ecSOhad Ben-Cohen dev_warn(dev, "unsupported resource %d\n", hdr->type); 724e12bc14bSOhad Ben-Cohen continue; 725400e64dfSOhad Ben-Cohen } 726400e64dfSOhad Ben-Cohen 727232fcdbbSSjur Brændeland handler = handlers[hdr->type]; 728e12bc14bSOhad Ben-Cohen if (!handler) 729e12bc14bSOhad Ben-Cohen continue; 730e12bc14bSOhad Ben-Cohen 731a2b950acSOhad Ben-Cohen ret = handler(rproc, rsc, offset + sizeof(*hdr), avail); 7327a186941SOhad Ben-Cohen if (ret) 733400e64dfSOhad Ben-Cohen break; 734400e64dfSOhad Ben-Cohen } 735400e64dfSOhad Ben-Cohen 736400e64dfSOhad Ben-Cohen return ret; 737400e64dfSOhad Ben-Cohen } 738400e64dfSOhad Ben-Cohen 7397bdc9650SBjorn Andersson static int rproc_probe_subdevices(struct rproc *rproc) 7407bdc9650SBjorn Andersson { 7417bdc9650SBjorn Andersson struct rproc_subdev *subdev; 7427bdc9650SBjorn Andersson int ret; 7437bdc9650SBjorn Andersson 7447bdc9650SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 7457bdc9650SBjorn Andersson ret = subdev->probe(subdev); 7467bdc9650SBjorn Andersson if (ret) 7477bdc9650SBjorn Andersson goto unroll_registration; 7487bdc9650SBjorn Andersson } 7497bdc9650SBjorn Andersson 7507bdc9650SBjorn Andersson return 0; 7517bdc9650SBjorn Andersson 7527bdc9650SBjorn Andersson unroll_registration: 7537bdc9650SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) 7547bdc9650SBjorn Andersson subdev->remove(subdev); 7557bdc9650SBjorn Andersson 7567bdc9650SBjorn Andersson return ret; 7577bdc9650SBjorn Andersson } 7587bdc9650SBjorn Andersson 7597bdc9650SBjorn Andersson static void rproc_remove_subdevices(struct rproc *rproc) 7607bdc9650SBjorn Andersson { 7617bdc9650SBjorn Andersson struct rproc_subdev *subdev; 7627bdc9650SBjorn Andersson 7637bdc9650SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) 7647bdc9650SBjorn Andersson subdev->remove(subdev); 7657bdc9650SBjorn Andersson } 7667bdc9650SBjorn Andersson 767400e64dfSOhad Ben-Cohen /** 768400e64dfSOhad Ben-Cohen * rproc_resource_cleanup() - clean up and free all acquired resources 769400e64dfSOhad Ben-Cohen * @rproc: rproc handle 770400e64dfSOhad Ben-Cohen * 771400e64dfSOhad Ben-Cohen * This function will free all resources acquired for @rproc, and it 7727a186941SOhad Ben-Cohen * is called whenever @rproc either shuts down or fails to boot. 773400e64dfSOhad Ben-Cohen */ 774400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc) 775400e64dfSOhad Ben-Cohen { 776400e64dfSOhad Ben-Cohen struct rproc_mem_entry *entry, *tmp; 777d81fb32fSBjorn Andersson struct rproc_vdev *rvdev, *rvtmp; 778b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 779400e64dfSOhad Ben-Cohen 780400e64dfSOhad Ben-Cohen /* clean up debugfs trace entries */ 781400e64dfSOhad Ben-Cohen list_for_each_entry_safe(entry, tmp, &rproc->traces, node) { 782400e64dfSOhad Ben-Cohen rproc_remove_trace_file(entry->priv); 783400e64dfSOhad Ben-Cohen rproc->num_traces--; 784400e64dfSOhad Ben-Cohen list_del(&entry->node); 785400e64dfSOhad Ben-Cohen kfree(entry); 786400e64dfSOhad Ben-Cohen } 787400e64dfSOhad Ben-Cohen 788400e64dfSOhad Ben-Cohen /* clean up iommu mapping entries */ 789400e64dfSOhad Ben-Cohen list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { 790400e64dfSOhad Ben-Cohen size_t unmapped; 791400e64dfSOhad Ben-Cohen 792400e64dfSOhad Ben-Cohen unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); 793400e64dfSOhad Ben-Cohen if (unmapped != entry->len) { 794400e64dfSOhad Ben-Cohen /* nothing much to do besides complaining */ 795e981f6d4SSjur Brændeland dev_err(dev, "failed to unmap %u/%zu\n", entry->len, 796400e64dfSOhad Ben-Cohen unmapped); 797400e64dfSOhad Ben-Cohen } 798400e64dfSOhad Ben-Cohen 799400e64dfSOhad Ben-Cohen list_del(&entry->node); 800400e64dfSOhad Ben-Cohen kfree(entry); 801400e64dfSOhad Ben-Cohen } 802b6356a01SSuman Anna 803b6356a01SSuman Anna /* clean up carveout allocations */ 804b6356a01SSuman Anna list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 805172e6ab1SSuman Anna dma_free_coherent(dev->parent, entry->len, entry->va, 806172e6ab1SSuman Anna entry->dma); 807b6356a01SSuman Anna list_del(&entry->node); 808b6356a01SSuman Anna kfree(entry); 809b6356a01SSuman Anna } 810d81fb32fSBjorn Andersson 811d81fb32fSBjorn Andersson /* clean up remote vdev entries */ 812d81fb32fSBjorn Andersson list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) 813d81fb32fSBjorn Andersson rproc_remove_virtio_dev(rvdev); 814400e64dfSOhad Ben-Cohen } 815400e64dfSOhad Ben-Cohen 816400e64dfSOhad Ben-Cohen /* 817400e64dfSOhad Ben-Cohen * take a firmware and boot a remote processor with it. 818400e64dfSOhad Ben-Cohen */ 819400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) 820400e64dfSOhad Ben-Cohen { 821b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 822400e64dfSOhad Ben-Cohen const char *name = rproc->firmware; 823a2b950acSOhad Ben-Cohen struct resource_table *table, *loaded_table; 8241e3e2c7cSOhad Ben-Cohen int ret, tablesz; 825400e64dfSOhad Ben-Cohen 826400e64dfSOhad Ben-Cohen ret = rproc_fw_sanity_check(rproc, fw); 827400e64dfSOhad Ben-Cohen if (ret) 828400e64dfSOhad Ben-Cohen return ret; 829400e64dfSOhad Ben-Cohen 830e981f6d4SSjur Brændeland dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); 831400e64dfSOhad Ben-Cohen 832400e64dfSOhad Ben-Cohen /* 833400e64dfSOhad Ben-Cohen * if enabling an IOMMU isn't relevant for this rproc, this is 834400e64dfSOhad Ben-Cohen * just a nop 835400e64dfSOhad Ben-Cohen */ 836400e64dfSOhad Ben-Cohen ret = rproc_enable_iommu(rproc); 837400e64dfSOhad Ben-Cohen if (ret) { 838400e64dfSOhad Ben-Cohen dev_err(dev, "can't enable iommu: %d\n", ret); 839400e64dfSOhad Ben-Cohen return ret; 840400e64dfSOhad Ben-Cohen } 841400e64dfSOhad Ben-Cohen 8423e5f9eb5SSjur Brændeland rproc->bootaddr = rproc_get_boot_addr(rproc, fw); 84389970d28SWei Yongjun ret = -EINVAL; 844400e64dfSOhad Ben-Cohen 8451e3e2c7cSOhad Ben-Cohen /* look for the resource table */ 846bd484984SSjur Brændeland table = rproc_find_rsc_table(rproc, fw, &tablesz); 847a66a5114SStefan Agner if (!table) { 848a66a5114SStefan Agner dev_err(dev, "Failed to find resource table\n"); 8491e3e2c7cSOhad Ben-Cohen goto clean_up; 850a66a5114SStefan Agner } 8511e3e2c7cSOhad Ben-Cohen 852988d204cSBjorn Andersson /* 853988d204cSBjorn Andersson * Create a copy of the resource table. When a virtio device starts 854988d204cSBjorn Andersson * and calls vring_new_virtqueue() the address of the allocated vring 855988d204cSBjorn Andersson * will be stored in the cached_table. Before the device is started, 856988d204cSBjorn Andersson * cached_table will be copied into device memory. 857988d204cSBjorn Andersson */ 858988d204cSBjorn Andersson rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL); 859988d204cSBjorn Andersson if (!rproc->cached_table) 860a2b950acSOhad Ben-Cohen goto clean_up; 861988d204cSBjorn Andersson 862988d204cSBjorn Andersson rproc->table_ptr = rproc->cached_table; 863a2b950acSOhad Ben-Cohen 864b35d7afcSBjorn Andersson /* reset max_notifyid */ 865b35d7afcSBjorn Andersson rproc->max_notifyid = -1; 866b35d7afcSBjorn Andersson 867d81fb32fSBjorn Andersson /* look for virtio devices and register them */ 868d81fb32fSBjorn Andersson ret = rproc_handle_resources(rproc, tablesz, rproc_vdev_handler); 869d81fb32fSBjorn Andersson if (ret) { 870d81fb32fSBjorn Andersson dev_err(dev, "Failed to handle vdev resources: %d\n", ret); 871d81fb32fSBjorn Andersson goto clean_up; 872d81fb32fSBjorn Andersson } 873d81fb32fSBjorn Andersson 874400e64dfSOhad Ben-Cohen /* handle fw resources which are required to boot rproc */ 875a2b950acSOhad Ben-Cohen ret = rproc_handle_resources(rproc, tablesz, rproc_loading_handlers); 876400e64dfSOhad Ben-Cohen if (ret) { 877400e64dfSOhad Ben-Cohen dev_err(dev, "Failed to process resources: %d\n", ret); 878229b85a6SBjorn Andersson goto clean_up_resources; 879400e64dfSOhad Ben-Cohen } 880400e64dfSOhad Ben-Cohen 881400e64dfSOhad Ben-Cohen /* load the ELF segments to memory */ 882bd484984SSjur Brændeland ret = rproc_load_segments(rproc, fw); 883400e64dfSOhad Ben-Cohen if (ret) { 884400e64dfSOhad Ben-Cohen dev_err(dev, "Failed to load program segments: %d\n", ret); 885229b85a6SBjorn Andersson goto clean_up_resources; 886400e64dfSOhad Ben-Cohen } 887400e64dfSOhad Ben-Cohen 888a2b950acSOhad Ben-Cohen /* 889a2b950acSOhad Ben-Cohen * The starting device has been given the rproc->cached_table as the 890a2b950acSOhad Ben-Cohen * resource table. The address of the vring along with the other 891a2b950acSOhad Ben-Cohen * allocated resources (carveouts etc) is stored in cached_table. 89213c4245bSBjorn Andersson * In order to pass this information to the remote device we must copy 89313c4245bSBjorn Andersson * this information to device memory. We also update the table_ptr so 89413c4245bSBjorn Andersson * that any subsequent changes will be applied to the loaded version. 895a2b950acSOhad Ben-Cohen */ 896a2b950acSOhad Ben-Cohen loaded_table = rproc_find_loaded_rsc_table(rproc, fw); 89713c4245bSBjorn Andersson if (loaded_table) { 898a2b950acSOhad Ben-Cohen memcpy(loaded_table, rproc->cached_table, tablesz); 89913c4245bSBjorn Andersson rproc->table_ptr = loaded_table; 90013c4245bSBjorn Andersson } 901a2b950acSOhad Ben-Cohen 902400e64dfSOhad Ben-Cohen /* power up the remote processor */ 903400e64dfSOhad Ben-Cohen ret = rproc->ops->start(rproc); 904400e64dfSOhad Ben-Cohen if (ret) { 905400e64dfSOhad Ben-Cohen dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); 906229b85a6SBjorn Andersson goto clean_up_resources; 907400e64dfSOhad Ben-Cohen } 908400e64dfSOhad Ben-Cohen 9097bdc9650SBjorn Andersson /* probe any subdevices for the remote processor */ 9107bdc9650SBjorn Andersson ret = rproc_probe_subdevices(rproc); 9117bdc9650SBjorn Andersson if (ret) { 9127bdc9650SBjorn Andersson dev_err(dev, "failed to probe subdevices for %s: %d\n", 9137bdc9650SBjorn Andersson rproc->name, ret); 9147bdc9650SBjorn Andersson goto stop_rproc; 9157bdc9650SBjorn Andersson } 9167bdc9650SBjorn Andersson 917400e64dfSOhad Ben-Cohen rproc->state = RPROC_RUNNING; 918400e64dfSOhad Ben-Cohen 919400e64dfSOhad Ben-Cohen dev_info(dev, "remote processor %s is now up\n", rproc->name); 920400e64dfSOhad Ben-Cohen 921400e64dfSOhad Ben-Cohen return 0; 922400e64dfSOhad Ben-Cohen 9237bdc9650SBjorn Andersson stop_rproc: 9247bdc9650SBjorn Andersson rproc->ops->stop(rproc); 925229b85a6SBjorn Andersson clean_up_resources: 926229b85a6SBjorn Andersson rproc_resource_cleanup(rproc); 927400e64dfSOhad Ben-Cohen clean_up: 928988d204cSBjorn Andersson kfree(rproc->cached_table); 929988d204cSBjorn Andersson rproc->cached_table = NULL; 930988d204cSBjorn Andersson rproc->table_ptr = NULL; 931988d204cSBjorn Andersson 932400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 933400e64dfSOhad Ben-Cohen return ret; 934400e64dfSOhad Ben-Cohen } 935400e64dfSOhad Ben-Cohen 936400e64dfSOhad Ben-Cohen /* 937400e64dfSOhad Ben-Cohen * take a firmware and look for virtio devices to register. 938400e64dfSOhad Ben-Cohen * 939400e64dfSOhad Ben-Cohen * Note: this function is called asynchronously upon registration of the 940400e64dfSOhad Ben-Cohen * remote processor (so we must wait until it completes before we try 941400e64dfSOhad Ben-Cohen * to unregister the device. one other option is just to use kref here, 942400e64dfSOhad Ben-Cohen * that might be cleaner). 943400e64dfSOhad Ben-Cohen */ 944400e64dfSOhad Ben-Cohen static void rproc_fw_config_virtio(const struct firmware *fw, void *context) 945400e64dfSOhad Ben-Cohen { 946400e64dfSOhad Ben-Cohen struct rproc *rproc = context; 947a2b950acSOhad Ben-Cohen 948ddf71187SBjorn Andersson /* if rproc is marked always-on, request it to boot */ 949ddf71187SBjorn Andersson if (rproc->auto_boot) 950ddf71187SBjorn Andersson rproc_boot_nowait(rproc); 951ddf71187SBjorn Andersson 952400e64dfSOhad Ben-Cohen release_firmware(fw); 953160e7c84SOhad Ben-Cohen /* allow rproc_del() contexts, if any, to proceed */ 954400e64dfSOhad Ben-Cohen complete_all(&rproc->firmware_loading_complete); 955400e64dfSOhad Ben-Cohen } 956400e64dfSOhad Ben-Cohen 95770b85ef8SFernando Guzman Lugo static int rproc_add_virtio_devices(struct rproc *rproc) 95870b85ef8SFernando Guzman Lugo { 95970b85ef8SFernando Guzman Lugo int ret; 96070b85ef8SFernando Guzman Lugo 96170b85ef8SFernando Guzman Lugo /* rproc_del() calls must wait until async loader completes */ 96270b85ef8SFernando Guzman Lugo init_completion(&rproc->firmware_loading_complete); 96370b85ef8SFernando Guzman Lugo 96470b85ef8SFernando Guzman Lugo /* 96570b85ef8SFernando Guzman Lugo * We must retrieve early virtio configuration info from 96670b85ef8SFernando Guzman Lugo * the firmware (e.g. whether to register a virtio device, 96770b85ef8SFernando Guzman Lugo * what virtio features does it support, ...). 96870b85ef8SFernando Guzman Lugo * 96970b85ef8SFernando Guzman Lugo * We're initiating an asynchronous firmware loading, so we can 97070b85ef8SFernando Guzman Lugo * be built-in kernel code, without hanging the boot process. 97170b85ef8SFernando Guzman Lugo */ 97270b85ef8SFernando Guzman Lugo ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, 97370b85ef8SFernando Guzman Lugo rproc->firmware, &rproc->dev, GFP_KERNEL, 97470b85ef8SFernando Guzman Lugo rproc, rproc_fw_config_virtio); 97570b85ef8SFernando Guzman Lugo if (ret < 0) { 97670b85ef8SFernando Guzman Lugo dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); 97770b85ef8SFernando Guzman Lugo complete_all(&rproc->firmware_loading_complete); 97870b85ef8SFernando Guzman Lugo } 97970b85ef8SFernando Guzman Lugo 98070b85ef8SFernando Guzman Lugo return ret; 98170b85ef8SFernando Guzman Lugo } 98270b85ef8SFernando Guzman Lugo 98370b85ef8SFernando Guzman Lugo /** 98470b85ef8SFernando Guzman Lugo * rproc_trigger_recovery() - recover a remoteproc 98570b85ef8SFernando Guzman Lugo * @rproc: the remote processor 98670b85ef8SFernando Guzman Lugo * 98756324d7aSAnna, Suman * The recovery is done by resetting all the virtio devices, that way all the 98870b85ef8SFernando Guzman Lugo * rpmsg drivers will be reseted along with the remote processor making the 98970b85ef8SFernando Guzman Lugo * remoteproc functional again. 99070b85ef8SFernando Guzman Lugo * 99170b85ef8SFernando Guzman Lugo * This function can sleep, so it cannot be called from atomic context. 99270b85ef8SFernando Guzman Lugo */ 99370b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc) 99470b85ef8SFernando Guzman Lugo { 99570b85ef8SFernando Guzman Lugo dev_err(&rproc->dev, "recovering %s\n", rproc->name); 99670b85ef8SFernando Guzman Lugo 99770b85ef8SFernando Guzman Lugo init_completion(&rproc->crash_comp); 99870b85ef8SFernando Guzman Lugo 999ddf71187SBjorn Andersson /* shut down the remote */ 1000ddf71187SBjorn Andersson /* TODO: make sure this works with rproc->power > 1 */ 1001ddf71187SBjorn Andersson rproc_shutdown(rproc); 1002ddf71187SBjorn Andersson 100370b85ef8SFernando Guzman Lugo /* wait until there is no more rproc users */ 100470b85ef8SFernando Guzman Lugo wait_for_completion(&rproc->crash_comp); 100570b85ef8SFernando Guzman Lugo 1006ddf71187SBjorn Andersson /* 1007d81fb32fSBjorn Andersson * boot the remote processor up again 1008ddf71187SBjorn Andersson */ 1009ddf71187SBjorn Andersson rproc_boot(rproc); 1010ddf71187SBjorn Andersson 1011ddf71187SBjorn Andersson return 0; 101270b85ef8SFernando Guzman Lugo } 101370b85ef8SFernando Guzman Lugo 1014400e64dfSOhad Ben-Cohen /** 10158afd519cSFernando Guzman Lugo * rproc_crash_handler_work() - handle a crash 10168afd519cSFernando Guzman Lugo * 10178afd519cSFernando Guzman Lugo * This function needs to handle everything related to a crash, like cpu 10188afd519cSFernando Guzman Lugo * registers and stack dump, information to help to debug the fatal error, etc. 10198afd519cSFernando Guzman Lugo */ 10208afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work) 10218afd519cSFernando Guzman Lugo { 10228afd519cSFernando Guzman Lugo struct rproc *rproc = container_of(work, struct rproc, crash_handler); 10238afd519cSFernando Guzman Lugo struct device *dev = &rproc->dev; 10248afd519cSFernando Guzman Lugo 10258afd519cSFernando Guzman Lugo dev_dbg(dev, "enter %s\n", __func__); 10268afd519cSFernando Guzman Lugo 10278afd519cSFernando Guzman Lugo mutex_lock(&rproc->lock); 10288afd519cSFernando Guzman Lugo 10298afd519cSFernando Guzman Lugo if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) { 10308afd519cSFernando Guzman Lugo /* handle only the first crash detected */ 10318afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 10328afd519cSFernando Guzman Lugo return; 10338afd519cSFernando Guzman Lugo } 10348afd519cSFernando Guzman Lugo 10358afd519cSFernando Guzman Lugo rproc->state = RPROC_CRASHED; 10368afd519cSFernando Guzman Lugo dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, 10378afd519cSFernando Guzman Lugo rproc->name); 10388afd519cSFernando Guzman Lugo 10398afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 10408afd519cSFernando Guzman Lugo 10412e37abb8SFernando Guzman Lugo if (!rproc->recovery_disabled) 104270b85ef8SFernando Guzman Lugo rproc_trigger_recovery(rproc); 10438afd519cSFernando Guzman Lugo } 10448afd519cSFernando Guzman Lugo 10458afd519cSFernando Guzman Lugo /** 10463d87fa1dSLee Jones * __rproc_boot() - boot a remote processor 1047400e64dfSOhad Ben-Cohen * @rproc: handle of a remote processor 10483d87fa1dSLee Jones * @wait: wait for rproc registration completion 1049400e64dfSOhad Ben-Cohen * 1050400e64dfSOhad Ben-Cohen * Boot a remote processor (i.e. load its firmware, power it on, ...). 1051400e64dfSOhad Ben-Cohen * 1052400e64dfSOhad Ben-Cohen * If the remote processor is already powered on, this function immediately 1053400e64dfSOhad Ben-Cohen * returns (successfully). 1054400e64dfSOhad Ben-Cohen * 1055400e64dfSOhad Ben-Cohen * Returns 0 on success, and an appropriate error value otherwise. 1056400e64dfSOhad Ben-Cohen */ 10573d87fa1dSLee Jones static int __rproc_boot(struct rproc *rproc, bool wait) 1058400e64dfSOhad Ben-Cohen { 1059400e64dfSOhad Ben-Cohen const struct firmware *firmware_p; 1060400e64dfSOhad Ben-Cohen struct device *dev; 1061400e64dfSOhad Ben-Cohen int ret; 1062400e64dfSOhad Ben-Cohen 1063400e64dfSOhad Ben-Cohen if (!rproc) { 1064400e64dfSOhad Ben-Cohen pr_err("invalid rproc handle\n"); 1065400e64dfSOhad Ben-Cohen return -EINVAL; 1066400e64dfSOhad Ben-Cohen } 1067400e64dfSOhad Ben-Cohen 1068b5ab5e24SOhad Ben-Cohen dev = &rproc->dev; 1069400e64dfSOhad Ben-Cohen 1070400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1071400e64dfSOhad Ben-Cohen if (ret) { 1072400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1073400e64dfSOhad Ben-Cohen return ret; 1074400e64dfSOhad Ben-Cohen } 1075400e64dfSOhad Ben-Cohen 1076400e64dfSOhad Ben-Cohen /* skip the boot process if rproc is already powered up */ 1077400e64dfSOhad Ben-Cohen if (atomic_inc_return(&rproc->power) > 1) { 1078400e64dfSOhad Ben-Cohen ret = 0; 1079400e64dfSOhad Ben-Cohen goto unlock_mutex; 1080400e64dfSOhad Ben-Cohen } 1081400e64dfSOhad Ben-Cohen 1082400e64dfSOhad Ben-Cohen dev_info(dev, "powering up %s\n", rproc->name); 1083400e64dfSOhad Ben-Cohen 1084400e64dfSOhad Ben-Cohen /* load firmware */ 1085400e64dfSOhad Ben-Cohen ret = request_firmware(&firmware_p, rproc->firmware, dev); 1086400e64dfSOhad Ben-Cohen if (ret < 0) { 1087400e64dfSOhad Ben-Cohen dev_err(dev, "request_firmware failed: %d\n", ret); 1088400e64dfSOhad Ben-Cohen goto downref_rproc; 1089400e64dfSOhad Ben-Cohen } 1090400e64dfSOhad Ben-Cohen 10913d87fa1dSLee Jones /* if rproc virtio is not yet configured, wait */ 10923d87fa1dSLee Jones if (wait) 10933d87fa1dSLee Jones wait_for_completion(&rproc->firmware_loading_complete); 10943d87fa1dSLee Jones 1095400e64dfSOhad Ben-Cohen ret = rproc_fw_boot(rproc, firmware_p); 1096400e64dfSOhad Ben-Cohen 1097400e64dfSOhad Ben-Cohen release_firmware(firmware_p); 1098400e64dfSOhad Ben-Cohen 1099400e64dfSOhad Ben-Cohen downref_rproc: 1100fbb6aacbSBjorn Andersson if (ret) 1101400e64dfSOhad Ben-Cohen atomic_dec(&rproc->power); 1102400e64dfSOhad Ben-Cohen unlock_mutex: 1103400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1104400e64dfSOhad Ben-Cohen return ret; 1105400e64dfSOhad Ben-Cohen } 11063d87fa1dSLee Jones 11073d87fa1dSLee Jones /** 11083d87fa1dSLee Jones * rproc_boot() - boot a remote processor 11093d87fa1dSLee Jones * @rproc: handle of a remote processor 11103d87fa1dSLee Jones */ 11113d87fa1dSLee Jones int rproc_boot(struct rproc *rproc) 11123d87fa1dSLee Jones { 11133d87fa1dSLee Jones return __rproc_boot(rproc, true); 11143d87fa1dSLee Jones } 1115400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot); 1116400e64dfSOhad Ben-Cohen 1117400e64dfSOhad Ben-Cohen /** 11183d87fa1dSLee Jones * rproc_boot_nowait() - boot a remote processor 11193d87fa1dSLee Jones * @rproc: handle of a remote processor 11203d87fa1dSLee Jones * 11213d87fa1dSLee Jones * Same as rproc_boot() but don't wait for rproc registration completion 11223d87fa1dSLee Jones */ 11233d87fa1dSLee Jones int rproc_boot_nowait(struct rproc *rproc) 11243d87fa1dSLee Jones { 11253d87fa1dSLee Jones return __rproc_boot(rproc, false); 11263d87fa1dSLee Jones } 11273d87fa1dSLee Jones 11283d87fa1dSLee Jones /** 1129400e64dfSOhad Ben-Cohen * rproc_shutdown() - power off the remote processor 1130400e64dfSOhad Ben-Cohen * @rproc: the remote processor 1131400e64dfSOhad Ben-Cohen * 1132400e64dfSOhad Ben-Cohen * Power off a remote processor (previously booted with rproc_boot()). 1133400e64dfSOhad Ben-Cohen * 1134400e64dfSOhad Ben-Cohen * In case @rproc is still being used by an additional user(s), then 1135400e64dfSOhad Ben-Cohen * this function will just decrement the power refcount and exit, 1136400e64dfSOhad Ben-Cohen * without really powering off the device. 1137400e64dfSOhad Ben-Cohen * 1138400e64dfSOhad Ben-Cohen * Every call to rproc_boot() must (eventually) be accompanied by a call 1139400e64dfSOhad Ben-Cohen * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. 1140400e64dfSOhad Ben-Cohen * 1141400e64dfSOhad Ben-Cohen * Notes: 1142400e64dfSOhad Ben-Cohen * - we're not decrementing the rproc's refcount, only the power refcount. 1143400e64dfSOhad Ben-Cohen * which means that the @rproc handle stays valid even after rproc_shutdown() 1144400e64dfSOhad Ben-Cohen * returns, and users can still use it with a subsequent rproc_boot(), if 1145400e64dfSOhad Ben-Cohen * needed. 1146400e64dfSOhad Ben-Cohen */ 1147400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc) 1148400e64dfSOhad Ben-Cohen { 1149b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1150400e64dfSOhad Ben-Cohen int ret; 1151400e64dfSOhad Ben-Cohen 1152400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1153400e64dfSOhad Ben-Cohen if (ret) { 1154400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1155400e64dfSOhad Ben-Cohen return; 1156400e64dfSOhad Ben-Cohen } 1157400e64dfSOhad Ben-Cohen 1158400e64dfSOhad Ben-Cohen /* if the remote proc is still needed, bail out */ 1159400e64dfSOhad Ben-Cohen if (!atomic_dec_and_test(&rproc->power)) 1160400e64dfSOhad Ben-Cohen goto out; 1161400e64dfSOhad Ben-Cohen 11627bdc9650SBjorn Andersson /* remove any subdevices for the remote processor */ 11637bdc9650SBjorn Andersson rproc_remove_subdevices(rproc); 11647bdc9650SBjorn Andersson 1165400e64dfSOhad Ben-Cohen /* power off the remote processor */ 1166400e64dfSOhad Ben-Cohen ret = rproc->ops->stop(rproc); 1167400e64dfSOhad Ben-Cohen if (ret) { 1168400e64dfSOhad Ben-Cohen atomic_inc(&rproc->power); 1169400e64dfSOhad Ben-Cohen dev_err(dev, "can't stop rproc: %d\n", ret); 1170400e64dfSOhad Ben-Cohen goto out; 1171400e64dfSOhad Ben-Cohen } 1172400e64dfSOhad Ben-Cohen 1173400e64dfSOhad Ben-Cohen /* clean up all acquired resources */ 1174400e64dfSOhad Ben-Cohen rproc_resource_cleanup(rproc); 1175400e64dfSOhad Ben-Cohen 1176400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1177400e64dfSOhad Ben-Cohen 1178988d204cSBjorn Andersson /* Free the copy of the resource table */ 1179988d204cSBjorn Andersson kfree(rproc->cached_table); 1180988d204cSBjorn Andersson rproc->cached_table = NULL; 1181988d204cSBjorn Andersson rproc->table_ptr = NULL; 1182a2b950acSOhad Ben-Cohen 118370b85ef8SFernando Guzman Lugo /* if in crash state, unlock crash handler */ 118470b85ef8SFernando Guzman Lugo if (rproc->state == RPROC_CRASHED) 118570b85ef8SFernando Guzman Lugo complete_all(&rproc->crash_comp); 118670b85ef8SFernando Guzman Lugo 1187400e64dfSOhad Ben-Cohen rproc->state = RPROC_OFFLINE; 1188400e64dfSOhad Ben-Cohen 1189400e64dfSOhad Ben-Cohen dev_info(dev, "stopped remote processor %s\n", rproc->name); 1190400e64dfSOhad Ben-Cohen 1191400e64dfSOhad Ben-Cohen out: 1192400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1193400e64dfSOhad Ben-Cohen } 1194400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown); 1195400e64dfSOhad Ben-Cohen 1196400e64dfSOhad Ben-Cohen /** 1197fec47d86SDave Gerlach * rproc_get_by_phandle() - find a remote processor by phandle 1198fec47d86SDave Gerlach * @phandle: phandle to the rproc 1199fec47d86SDave Gerlach * 1200fec47d86SDave Gerlach * Finds an rproc handle using the remote processor's phandle, and then 1201fec47d86SDave Gerlach * return a handle to the rproc. 1202fec47d86SDave Gerlach * 1203fec47d86SDave Gerlach * This function increments the remote processor's refcount, so always 1204fec47d86SDave Gerlach * use rproc_put() to decrement it back once rproc isn't needed anymore. 1205fec47d86SDave Gerlach * 1206fec47d86SDave Gerlach * Returns the rproc handle on success, and NULL on failure. 1207fec47d86SDave Gerlach */ 12088de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF 1209fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle) 1210fec47d86SDave Gerlach { 1211fec47d86SDave Gerlach struct rproc *rproc = NULL, *r; 1212fec47d86SDave Gerlach struct device_node *np; 1213fec47d86SDave Gerlach 1214fec47d86SDave Gerlach np = of_find_node_by_phandle(phandle); 1215fec47d86SDave Gerlach if (!np) 1216fec47d86SDave Gerlach return NULL; 1217fec47d86SDave Gerlach 1218fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 1219fec47d86SDave Gerlach list_for_each_entry(r, &rproc_list, node) { 1220fec47d86SDave Gerlach if (r->dev.parent && r->dev.parent->of_node == np) { 1221fbb6aacbSBjorn Andersson /* prevent underlying implementation from being removed */ 1222fbb6aacbSBjorn Andersson if (!try_module_get(r->dev.parent->driver->owner)) { 1223fbb6aacbSBjorn Andersson dev_err(&r->dev, "can't get owner\n"); 1224fbb6aacbSBjorn Andersson break; 1225fbb6aacbSBjorn Andersson } 1226fbb6aacbSBjorn Andersson 1227fec47d86SDave Gerlach rproc = r; 1228fec47d86SDave Gerlach get_device(&rproc->dev); 1229fec47d86SDave Gerlach break; 1230fec47d86SDave Gerlach } 1231fec47d86SDave Gerlach } 1232fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 1233fec47d86SDave Gerlach 1234fec47d86SDave Gerlach of_node_put(np); 1235fec47d86SDave Gerlach 1236fec47d86SDave Gerlach return rproc; 1237fec47d86SDave Gerlach } 12388de3dbd0SOhad Ben-Cohen #else 12398de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle) 12408de3dbd0SOhad Ben-Cohen { 12418de3dbd0SOhad Ben-Cohen return NULL; 12428de3dbd0SOhad Ben-Cohen } 12438de3dbd0SOhad Ben-Cohen #endif 1244fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle); 1245fec47d86SDave Gerlach 1246fec47d86SDave Gerlach /** 1247160e7c84SOhad Ben-Cohen * rproc_add() - register a remote processor 1248400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle to register 1249400e64dfSOhad Ben-Cohen * 1250400e64dfSOhad Ben-Cohen * Registers @rproc with the remoteproc framework, after it has been 1251400e64dfSOhad Ben-Cohen * allocated with rproc_alloc(). 1252400e64dfSOhad Ben-Cohen * 1253400e64dfSOhad Ben-Cohen * This is called by the platform-specific rproc implementation, whenever 1254400e64dfSOhad Ben-Cohen * a new remote processor device is probed. 1255400e64dfSOhad Ben-Cohen * 1256400e64dfSOhad Ben-Cohen * Returns 0 on success and an appropriate error code otherwise. 1257400e64dfSOhad Ben-Cohen * 1258400e64dfSOhad Ben-Cohen * Note: this function initiates an asynchronous firmware loading 1259400e64dfSOhad Ben-Cohen * context, which will look for virtio devices supported by the rproc's 1260400e64dfSOhad Ben-Cohen * firmware. 1261400e64dfSOhad Ben-Cohen * 1262400e64dfSOhad Ben-Cohen * If found, those virtio devices will be created and added, so as a result 12637a186941SOhad Ben-Cohen * of registering this remote processor, additional virtio drivers might be 1264400e64dfSOhad Ben-Cohen * probed. 1265400e64dfSOhad Ben-Cohen */ 1266160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc) 1267400e64dfSOhad Ben-Cohen { 1268b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 126970b85ef8SFernando Guzman Lugo int ret; 1270400e64dfSOhad Ben-Cohen 1271b5ab5e24SOhad Ben-Cohen ret = device_add(dev); 1272b5ab5e24SOhad Ben-Cohen if (ret < 0) 1273b5ab5e24SOhad Ben-Cohen return ret; 1274400e64dfSOhad Ben-Cohen 1275b5ab5e24SOhad Ben-Cohen dev_info(dev, "%s is available\n", rproc->name); 1276400e64dfSOhad Ben-Cohen 1277489d129aSOhad Ben-Cohen dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n"); 1278489d129aSOhad Ben-Cohen dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n"); 1279489d129aSOhad Ben-Cohen 1280400e64dfSOhad Ben-Cohen /* create debugfs entries */ 1281400e64dfSOhad Ben-Cohen rproc_create_debug_dir(rproc); 1282d2e12e66SDave Gerlach ret = rproc_add_virtio_devices(rproc); 1283d2e12e66SDave Gerlach if (ret < 0) 1284d2e12e66SDave Gerlach return ret; 1285400e64dfSOhad Ben-Cohen 1286d2e12e66SDave Gerlach /* expose to rproc_get_by_phandle users */ 1287d2e12e66SDave Gerlach mutex_lock(&rproc_list_mutex); 1288d2e12e66SDave Gerlach list_add(&rproc->node, &rproc_list); 1289d2e12e66SDave Gerlach mutex_unlock(&rproc_list_mutex); 1290d2e12e66SDave Gerlach 1291d2e12e66SDave Gerlach return 0; 1292400e64dfSOhad Ben-Cohen } 1293160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add); 1294400e64dfSOhad Ben-Cohen 1295400e64dfSOhad Ben-Cohen /** 1296b5ab5e24SOhad Ben-Cohen * rproc_type_release() - release a remote processor instance 1297b5ab5e24SOhad Ben-Cohen * @dev: the rproc's device 1298b5ab5e24SOhad Ben-Cohen * 1299b5ab5e24SOhad Ben-Cohen * This function should _never_ be called directly. 1300b5ab5e24SOhad Ben-Cohen * 1301b5ab5e24SOhad Ben-Cohen * It will be called by the driver core when no one holds a valid pointer 1302b5ab5e24SOhad Ben-Cohen * to @dev anymore. 1303b5ab5e24SOhad Ben-Cohen */ 1304b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev) 1305b5ab5e24SOhad Ben-Cohen { 1306b5ab5e24SOhad Ben-Cohen struct rproc *rproc = container_of(dev, struct rproc, dev); 1307b5ab5e24SOhad Ben-Cohen 13087183a2a7SOhad Ben-Cohen dev_info(&rproc->dev, "releasing %s\n", rproc->name); 13097183a2a7SOhad Ben-Cohen 13107183a2a7SOhad Ben-Cohen rproc_delete_debug_dir(rproc); 13117183a2a7SOhad Ben-Cohen 1312b5ab5e24SOhad Ben-Cohen idr_destroy(&rproc->notifyids); 1313b5ab5e24SOhad Ben-Cohen 1314b5ab5e24SOhad Ben-Cohen if (rproc->index >= 0) 1315b5ab5e24SOhad Ben-Cohen ida_simple_remove(&rproc_dev_index, rproc->index); 1316b5ab5e24SOhad Ben-Cohen 13170f57dc6aSMatt Redfearn kfree(rproc->firmware); 1318b5ab5e24SOhad Ben-Cohen kfree(rproc); 1319b5ab5e24SOhad Ben-Cohen } 1320b5ab5e24SOhad Ben-Cohen 1321b5ab5e24SOhad Ben-Cohen static struct device_type rproc_type = { 1322b5ab5e24SOhad Ben-Cohen .name = "remoteproc", 1323b5ab5e24SOhad Ben-Cohen .release = rproc_type_release, 1324b5ab5e24SOhad Ben-Cohen }; 1325400e64dfSOhad Ben-Cohen 1326400e64dfSOhad Ben-Cohen /** 1327400e64dfSOhad Ben-Cohen * rproc_alloc() - allocate a remote processor handle 1328400e64dfSOhad Ben-Cohen * @dev: the underlying device 1329400e64dfSOhad Ben-Cohen * @name: name of this remote processor 1330400e64dfSOhad Ben-Cohen * @ops: platform-specific handlers (mainly start/stop) 13318b4aec9aSRobert Tivy * @firmware: name of firmware file to load, can be NULL 1332400e64dfSOhad Ben-Cohen * @len: length of private data needed by the rproc driver (in bytes) 1333400e64dfSOhad Ben-Cohen * 1334400e64dfSOhad Ben-Cohen * Allocates a new remote processor handle, but does not register 13358b4aec9aSRobert Tivy * it yet. if @firmware is NULL, a default name is used. 1336400e64dfSOhad Ben-Cohen * 1337400e64dfSOhad Ben-Cohen * This function should be used by rproc implementations during initialization 1338400e64dfSOhad Ben-Cohen * of the remote processor. 1339400e64dfSOhad Ben-Cohen * 1340400e64dfSOhad Ben-Cohen * After creating an rproc handle using this function, and when ready, 1341160e7c84SOhad Ben-Cohen * implementations should then call rproc_add() to complete 1342400e64dfSOhad Ben-Cohen * the registration of the remote processor. 1343400e64dfSOhad Ben-Cohen * 1344400e64dfSOhad Ben-Cohen * On success the new rproc is returned, and on failure, NULL. 1345400e64dfSOhad Ben-Cohen * 1346400e64dfSOhad Ben-Cohen * Note: _never_ directly deallocate @rproc, even if it was not registered 1347433c0e04SBjorn Andersson * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). 1348400e64dfSOhad Ben-Cohen */ 1349400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name, 1350400e64dfSOhad Ben-Cohen const struct rproc_ops *ops, 1351400e64dfSOhad Ben-Cohen const char *firmware, int len) 1352400e64dfSOhad Ben-Cohen { 1353400e64dfSOhad Ben-Cohen struct rproc *rproc; 13548b4aec9aSRobert Tivy char *p, *template = "rproc-%s-fw"; 13550f57dc6aSMatt Redfearn int name_len; 1356400e64dfSOhad Ben-Cohen 1357400e64dfSOhad Ben-Cohen if (!dev || !name || !ops) 1358400e64dfSOhad Ben-Cohen return NULL; 1359400e64dfSOhad Ben-Cohen 13600f57dc6aSMatt Redfearn if (!firmware) { 13618b4aec9aSRobert Tivy /* 13628b4aec9aSRobert Tivy * If the caller didn't pass in a firmware name then 13630f57dc6aSMatt Redfearn * construct a default name. 13648b4aec9aSRobert Tivy */ 13658b4aec9aSRobert Tivy name_len = strlen(name) + strlen(template) - 2 + 1; 13660f57dc6aSMatt Redfearn p = kmalloc(name_len, GFP_KERNEL); 13670f57dc6aSMatt Redfearn if (!p) 1368400e64dfSOhad Ben-Cohen return NULL; 13698b4aec9aSRobert Tivy snprintf(p, name_len, template, name); 13708b4aec9aSRobert Tivy } else { 13710f57dc6aSMatt Redfearn p = kstrdup(firmware, GFP_KERNEL); 13720f57dc6aSMatt Redfearn if (!p) 13730f57dc6aSMatt Redfearn return NULL; 13740f57dc6aSMatt Redfearn } 13750f57dc6aSMatt Redfearn 13760f57dc6aSMatt Redfearn rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); 13770f57dc6aSMatt Redfearn if (!rproc) { 13780f57dc6aSMatt Redfearn kfree(p); 13790f57dc6aSMatt Redfearn return NULL; 13808b4aec9aSRobert Tivy } 13818b4aec9aSRobert Tivy 13828b4aec9aSRobert Tivy rproc->firmware = p; 1383400e64dfSOhad Ben-Cohen rproc->name = name; 1384400e64dfSOhad Ben-Cohen rproc->ops = ops; 1385400e64dfSOhad Ben-Cohen rproc->priv = &rproc[1]; 1386ddf71187SBjorn Andersson rproc->auto_boot = true; 1387400e64dfSOhad Ben-Cohen 1388b5ab5e24SOhad Ben-Cohen device_initialize(&rproc->dev); 1389b5ab5e24SOhad Ben-Cohen rproc->dev.parent = dev; 1390b5ab5e24SOhad Ben-Cohen rproc->dev.type = &rproc_type; 13912aefbef0SMatt Redfearn rproc->dev.class = &rproc_class; 1392b5ab5e24SOhad Ben-Cohen 1393b5ab5e24SOhad Ben-Cohen /* Assign a unique device index and name */ 1394b5ab5e24SOhad Ben-Cohen rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL); 1395b5ab5e24SOhad Ben-Cohen if (rproc->index < 0) { 1396b5ab5e24SOhad Ben-Cohen dev_err(dev, "ida_simple_get failed: %d\n", rproc->index); 1397b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 1398b5ab5e24SOhad Ben-Cohen return NULL; 1399b5ab5e24SOhad Ben-Cohen } 1400b5ab5e24SOhad Ben-Cohen 1401b5ab5e24SOhad Ben-Cohen dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); 1402b5ab5e24SOhad Ben-Cohen 1403400e64dfSOhad Ben-Cohen atomic_set(&rproc->power, 0); 1404400e64dfSOhad Ben-Cohen 14054afc89d6SSjur Brændeland /* Set ELF as the default fw_ops handler */ 14064afc89d6SSjur Brændeland rproc->fw_ops = &rproc_elf_fw_ops; 1407400e64dfSOhad Ben-Cohen 1408400e64dfSOhad Ben-Cohen mutex_init(&rproc->lock); 1409400e64dfSOhad Ben-Cohen 14107a186941SOhad Ben-Cohen idr_init(&rproc->notifyids); 14117a186941SOhad Ben-Cohen 1412400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->carveouts); 1413400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->mappings); 1414400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->traces); 14157a186941SOhad Ben-Cohen INIT_LIST_HEAD(&rproc->rvdevs); 14167bdc9650SBjorn Andersson INIT_LIST_HEAD(&rproc->subdevs); 1417400e64dfSOhad Ben-Cohen 14188afd519cSFernando Guzman Lugo INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); 141970b85ef8SFernando Guzman Lugo init_completion(&rproc->crash_comp); 14208afd519cSFernando Guzman Lugo 1421400e64dfSOhad Ben-Cohen rproc->state = RPROC_OFFLINE; 1422400e64dfSOhad Ben-Cohen 1423400e64dfSOhad Ben-Cohen return rproc; 1424400e64dfSOhad Ben-Cohen } 1425400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc); 1426400e64dfSOhad Ben-Cohen 1427400e64dfSOhad Ben-Cohen /** 1428433c0e04SBjorn Andersson * rproc_free() - unroll rproc_alloc() 1429433c0e04SBjorn Andersson * @rproc: the remote processor handle 1430433c0e04SBjorn Andersson * 1431433c0e04SBjorn Andersson * This function decrements the rproc dev refcount. 1432433c0e04SBjorn Andersson * 1433433c0e04SBjorn Andersson * If no one holds any reference to rproc anymore, then its refcount would 1434433c0e04SBjorn Andersson * now drop to zero, and it would be freed. 1435433c0e04SBjorn Andersson */ 1436433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc) 1437433c0e04SBjorn Andersson { 1438433c0e04SBjorn Andersson put_device(&rproc->dev); 1439433c0e04SBjorn Andersson } 1440433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free); 1441433c0e04SBjorn Andersson 1442433c0e04SBjorn Andersson /** 1443433c0e04SBjorn Andersson * rproc_put() - release rproc reference 1444400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle 1445400e64dfSOhad Ben-Cohen * 1446c6b5a276SOhad Ben-Cohen * This function decrements the rproc dev refcount. 1447400e64dfSOhad Ben-Cohen * 1448c6b5a276SOhad Ben-Cohen * If no one holds any reference to rproc anymore, then its refcount would 1449c6b5a276SOhad Ben-Cohen * now drop to zero, and it would be freed. 1450400e64dfSOhad Ben-Cohen */ 1451160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc) 1452400e64dfSOhad Ben-Cohen { 1453fbb6aacbSBjorn Andersson module_put(rproc->dev.parent->driver->owner); 1454b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 1455400e64dfSOhad Ben-Cohen } 1456160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put); 1457400e64dfSOhad Ben-Cohen 1458400e64dfSOhad Ben-Cohen /** 1459160e7c84SOhad Ben-Cohen * rproc_del() - unregister a remote processor 1460400e64dfSOhad Ben-Cohen * @rproc: rproc handle to unregister 1461400e64dfSOhad Ben-Cohen * 1462400e64dfSOhad Ben-Cohen * This function should be called when the platform specific rproc 1463400e64dfSOhad Ben-Cohen * implementation decides to remove the rproc device. it should 1464160e7c84SOhad Ben-Cohen * _only_ be called if a previous invocation of rproc_add() 1465400e64dfSOhad Ben-Cohen * has completed successfully. 1466400e64dfSOhad Ben-Cohen * 1467160e7c84SOhad Ben-Cohen * After rproc_del() returns, @rproc isn't freed yet, because 1468c6b5a276SOhad Ben-Cohen * of the outstanding reference created by rproc_alloc. To decrement that 1469433c0e04SBjorn Andersson * one last refcount, one still needs to call rproc_free(). 1470400e64dfSOhad Ben-Cohen * 1471400e64dfSOhad Ben-Cohen * Returns 0 on success and -EINVAL if @rproc isn't valid. 1472400e64dfSOhad Ben-Cohen */ 1473160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc) 1474400e64dfSOhad Ben-Cohen { 14756db20ea8SOhad Ben-Cohen struct rproc_vdev *rvdev, *tmp; 14767a186941SOhad Ben-Cohen 1477400e64dfSOhad Ben-Cohen if (!rproc) 1478400e64dfSOhad Ben-Cohen return -EINVAL; 1479400e64dfSOhad Ben-Cohen 1480400e64dfSOhad Ben-Cohen /* if rproc is just being registered, wait */ 1481400e64dfSOhad Ben-Cohen wait_for_completion(&rproc->firmware_loading_complete); 1482400e64dfSOhad Ben-Cohen 1483ddf71187SBjorn Andersson /* if rproc is marked always-on, rproc_add() booted it */ 1484ddf71187SBjorn Andersson /* TODO: make sure this works with rproc->power > 1 */ 1485ddf71187SBjorn Andersson if (rproc->auto_boot) 1486ddf71187SBjorn Andersson rproc_shutdown(rproc); 1487ddf71187SBjorn Andersson 14887a186941SOhad Ben-Cohen /* clean up remote vdev entries */ 14896db20ea8SOhad Ben-Cohen list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node) 14907a186941SOhad Ben-Cohen rproc_remove_virtio_dev(rvdev); 1491400e64dfSOhad Ben-Cohen 1492fec47d86SDave Gerlach /* the rproc is downref'ed as soon as it's removed from the klist */ 1493fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 1494fec47d86SDave Gerlach list_del(&rproc->node); 1495fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 1496fec47d86SDave Gerlach 1497b5ab5e24SOhad Ben-Cohen device_del(&rproc->dev); 1498400e64dfSOhad Ben-Cohen 1499400e64dfSOhad Ben-Cohen return 0; 1500400e64dfSOhad Ben-Cohen } 1501160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del); 1502400e64dfSOhad Ben-Cohen 15038afd519cSFernando Guzman Lugo /** 15047bdc9650SBjorn Andersson * rproc_add_subdev() - add a subdevice to a remoteproc 15057bdc9650SBjorn Andersson * @rproc: rproc handle to add the subdevice to 15067bdc9650SBjorn Andersson * @subdev: subdev handle to register 15077bdc9650SBjorn Andersson * @probe: function to call when the rproc boots 15087bdc9650SBjorn Andersson * @remove: function to call when the rproc shuts down 15097bdc9650SBjorn Andersson */ 15107bdc9650SBjorn Andersson void rproc_add_subdev(struct rproc *rproc, 15117bdc9650SBjorn Andersson struct rproc_subdev *subdev, 15127bdc9650SBjorn Andersson int (*probe)(struct rproc_subdev *subdev), 15137bdc9650SBjorn Andersson void (*remove)(struct rproc_subdev *subdev)) 15147bdc9650SBjorn Andersson { 15157bdc9650SBjorn Andersson subdev->probe = probe; 15167bdc9650SBjorn Andersson subdev->remove = remove; 15177bdc9650SBjorn Andersson 15187bdc9650SBjorn Andersson list_add_tail(&subdev->node, &rproc->subdevs); 15197bdc9650SBjorn Andersson } 15207bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev); 15217bdc9650SBjorn Andersson 15227bdc9650SBjorn Andersson /** 15237bdc9650SBjorn Andersson * rproc_remove_subdev() - remove a subdevice from a remoteproc 15247bdc9650SBjorn Andersson * @rproc: rproc handle to remove the subdevice from 15257bdc9650SBjorn Andersson * @subdev: subdev handle, previously registered with rproc_add_subdev() 15267bdc9650SBjorn Andersson */ 15277bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 15287bdc9650SBjorn Andersson { 15297bdc9650SBjorn Andersson list_del(&subdev->node); 15307bdc9650SBjorn Andersson } 15317bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev); 15327bdc9650SBjorn Andersson 15337bdc9650SBjorn Andersson /** 15348afd519cSFernando Guzman Lugo * rproc_report_crash() - rproc crash reporter function 15358afd519cSFernando Guzman Lugo * @rproc: remote processor 15368afd519cSFernando Guzman Lugo * @type: crash type 15378afd519cSFernando Guzman Lugo * 15388afd519cSFernando Guzman Lugo * This function must be called every time a crash is detected by the low-level 15398afd519cSFernando Guzman Lugo * drivers implementing a specific remoteproc. This should not be called from a 15408afd519cSFernando Guzman Lugo * non-remoteproc driver. 15418afd519cSFernando Guzman Lugo * 15428afd519cSFernando Guzman Lugo * This function can be called from atomic/interrupt context. 15438afd519cSFernando Guzman Lugo */ 15448afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) 15458afd519cSFernando Guzman Lugo { 15468afd519cSFernando Guzman Lugo if (!rproc) { 15478afd519cSFernando Guzman Lugo pr_err("NULL rproc pointer\n"); 15488afd519cSFernando Guzman Lugo return; 15498afd519cSFernando Guzman Lugo } 15508afd519cSFernando Guzman Lugo 15518afd519cSFernando Guzman Lugo dev_err(&rproc->dev, "crash detected in %s: type %s\n", 15528afd519cSFernando Guzman Lugo rproc->name, rproc_crash_to_string(type)); 15538afd519cSFernando Guzman Lugo 15548afd519cSFernando Guzman Lugo /* create a new task to handle the error */ 15558afd519cSFernando Guzman Lugo schedule_work(&rproc->crash_handler); 15568afd519cSFernando Guzman Lugo } 15578afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash); 15588afd519cSFernando Guzman Lugo 1559400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void) 1560400e64dfSOhad Ben-Cohen { 15612aefbef0SMatt Redfearn rproc_init_sysfs(); 1562400e64dfSOhad Ben-Cohen rproc_init_debugfs(); 1563b5ab5e24SOhad Ben-Cohen 1564400e64dfSOhad Ben-Cohen return 0; 1565400e64dfSOhad Ben-Cohen } 1566400e64dfSOhad Ben-Cohen module_init(remoteproc_init); 1567400e64dfSOhad Ben-Cohen 1568400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void) 1569400e64dfSOhad Ben-Cohen { 1570f42f79afSSuman Anna ida_destroy(&rproc_dev_index); 1571f42f79afSSuman Anna 1572400e64dfSOhad Ben-Cohen rproc_exit_debugfs(); 15732aefbef0SMatt Redfearn rproc_exit_sysfs(); 1574400e64dfSOhad Ben-Cohen } 1575400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit); 1576400e64dfSOhad Ben-Cohen 1577400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2"); 1578400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework"); 1579