11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2400e64dfSOhad Ben-Cohen /* 3400e64dfSOhad Ben-Cohen * Remote Processor Framework 4400e64dfSOhad Ben-Cohen * 5400e64dfSOhad Ben-Cohen * Copyright (C) 2011 Texas Instruments, Inc. 6400e64dfSOhad Ben-Cohen * Copyright (C) 2011 Google, Inc. 7400e64dfSOhad Ben-Cohen * 8400e64dfSOhad Ben-Cohen * Ohad Ben-Cohen <ohad@wizery.com> 9400e64dfSOhad Ben-Cohen * Brian Swetland <swetland@google.com> 10400e64dfSOhad Ben-Cohen * Mark Grosen <mgrosen@ti.com> 11400e64dfSOhad Ben-Cohen * Fernando Guzman Lugo <fernando.lugo@ti.com> 12400e64dfSOhad Ben-Cohen * Suman Anna <s-anna@ti.com> 13400e64dfSOhad Ben-Cohen * Robert Tivy <rtivy@ti.com> 14400e64dfSOhad Ben-Cohen * Armando Uribe De Leon <x0095078@ti.com> 15400e64dfSOhad Ben-Cohen */ 16400e64dfSOhad Ben-Cohen 17400e64dfSOhad Ben-Cohen #define pr_fmt(fmt) "%s: " fmt, __func__ 18400e64dfSOhad Ben-Cohen 19dc5192c4SBjorn Andersson #include <linux/delay.h> 20400e64dfSOhad Ben-Cohen #include <linux/kernel.h> 21400e64dfSOhad Ben-Cohen #include <linux/module.h> 22400e64dfSOhad Ben-Cohen #include <linux/device.h> 23400e64dfSOhad Ben-Cohen #include <linux/slab.h> 24400e64dfSOhad Ben-Cohen #include <linux/mutex.h> 250a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h> 26400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h> 27e0d07278SJim Quinlan #include <linux/dma-direct.h> /* XXX: pokes into bus_dma_range */ 28400e64dfSOhad Ben-Cohen #include <linux/firmware.h> 29400e64dfSOhad Ben-Cohen #include <linux/string.h> 30400e64dfSOhad Ben-Cohen #include <linux/debugfs.h> 31c0abe2caSBjorn Andersson #include <linux/rculist.h> 32400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h> 33400e64dfSOhad Ben-Cohen #include <linux/iommu.h> 34b5ab5e24SOhad Ben-Cohen #include <linux/idr.h> 35400e64dfSOhad Ben-Cohen #include <linux/elf.h> 36a2b950acSOhad Ben-Cohen #include <linux/crc32.h> 37086d0872SLoic Pallardy #include <linux/of_reserved_mem.h> 38400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h> 39400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h> 40cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h> 41086d0872SLoic Pallardy #include <linux/platform_device.h> 42400e64dfSOhad Ben-Cohen 43400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h" 44400e64dfSOhad Ben-Cohen 45b36de8cfSLoic Pallardy #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL 46b36de8cfSLoic Pallardy 47fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex); 48fec47d86SDave Gerlach static LIST_HEAD(rproc_list); 49dc5192c4SBjorn Andersson static struct notifier_block rproc_panic_nb; 50fec47d86SDave Gerlach 51a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc, 52a2b950acSOhad Ben-Cohen void *, int offset, int avail); 53400e64dfSOhad Ben-Cohen 54c6aed238SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc, 55c6aed238SLoic Pallardy struct rproc_mem_entry *mem); 56c6aed238SLoic Pallardy static int rproc_release_carveout(struct rproc *rproc, 57c6aed238SLoic Pallardy struct rproc_mem_entry *mem); 58c6aed238SLoic Pallardy 59b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */ 60b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index); 61b5ab5e24SOhad Ben-Cohen 628afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = { 638afd519cSFernando Guzman Lugo [RPROC_MMUFAULT] = "mmufault", 64b3d39032SBjorn Andersson [RPROC_WATCHDOG] = "watchdog", 65b3d39032SBjorn Andersson [RPROC_FATAL_ERROR] = "fatal error", 668afd519cSFernando Guzman Lugo }; 678afd519cSFernando Guzman Lugo 688afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */ 698afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type) 708afd519cSFernando Guzman Lugo { 718afd519cSFernando Guzman Lugo if (type < ARRAY_SIZE(rproc_crash_names)) 728afd519cSFernando Guzman Lugo return rproc_crash_names[type]; 73b23f7a09SMasanari Iida return "unknown"; 748afd519cSFernando Guzman Lugo } 758afd519cSFernando Guzman Lugo 76400e64dfSOhad Ben-Cohen /* 77400e64dfSOhad Ben-Cohen * This is the IOMMU fault handler we register with the IOMMU API 78400e64dfSOhad Ben-Cohen * (when relevant; not all remote processors access memory through 79400e64dfSOhad Ben-Cohen * an IOMMU). 80400e64dfSOhad Ben-Cohen * 81400e64dfSOhad Ben-Cohen * IOMMU core will invoke this handler whenever the remote processor 82400e64dfSOhad Ben-Cohen * will try to access an unmapped device address. 83400e64dfSOhad Ben-Cohen */ 84400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, 8577ca2332SOhad Ben-Cohen unsigned long iova, int flags, void *token) 86400e64dfSOhad Ben-Cohen { 878afd519cSFernando Guzman Lugo struct rproc *rproc = token; 888afd519cSFernando Guzman Lugo 89400e64dfSOhad Ben-Cohen dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags); 90400e64dfSOhad Ben-Cohen 918afd519cSFernando Guzman Lugo rproc_report_crash(rproc, RPROC_MMUFAULT); 928afd519cSFernando Guzman Lugo 93400e64dfSOhad Ben-Cohen /* 94400e64dfSOhad Ben-Cohen * Let the iommu core know we're not really handling this fault; 958afd519cSFernando Guzman Lugo * we just used it as a recovery trigger. 96400e64dfSOhad Ben-Cohen */ 97400e64dfSOhad Ben-Cohen return -ENOSYS; 98400e64dfSOhad Ben-Cohen } 99400e64dfSOhad Ben-Cohen 100400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc) 101400e64dfSOhad Ben-Cohen { 102400e64dfSOhad Ben-Cohen struct iommu_domain *domain; 103b5ab5e24SOhad Ben-Cohen struct device *dev = rproc->dev.parent; 104400e64dfSOhad Ben-Cohen int ret; 105400e64dfSOhad Ben-Cohen 106315491e5SSuman Anna if (!rproc->has_iommu) { 107315491e5SSuman Anna dev_dbg(dev, "iommu not present\n"); 1080798e1daSMark Grosen return 0; 109400e64dfSOhad Ben-Cohen } 110400e64dfSOhad Ben-Cohen 111400e64dfSOhad Ben-Cohen domain = iommu_domain_alloc(dev->bus); 112400e64dfSOhad Ben-Cohen if (!domain) { 113400e64dfSOhad Ben-Cohen dev_err(dev, "can't alloc iommu domain\n"); 114400e64dfSOhad Ben-Cohen return -ENOMEM; 115400e64dfSOhad Ben-Cohen } 116400e64dfSOhad Ben-Cohen 11777ca2332SOhad Ben-Cohen iommu_set_fault_handler(domain, rproc_iommu_fault, rproc); 118400e64dfSOhad Ben-Cohen 119400e64dfSOhad Ben-Cohen ret = iommu_attach_device(domain, dev); 120400e64dfSOhad Ben-Cohen if (ret) { 121400e64dfSOhad Ben-Cohen dev_err(dev, "can't attach iommu device: %d\n", ret); 122400e64dfSOhad Ben-Cohen goto free_domain; 123400e64dfSOhad Ben-Cohen } 124400e64dfSOhad Ben-Cohen 125400e64dfSOhad Ben-Cohen rproc->domain = domain; 126400e64dfSOhad Ben-Cohen 127400e64dfSOhad Ben-Cohen return 0; 128400e64dfSOhad Ben-Cohen 129400e64dfSOhad Ben-Cohen free_domain: 130400e64dfSOhad Ben-Cohen iommu_domain_free(domain); 131400e64dfSOhad Ben-Cohen return ret; 132400e64dfSOhad Ben-Cohen } 133400e64dfSOhad Ben-Cohen 134400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc) 135400e64dfSOhad Ben-Cohen { 136400e64dfSOhad Ben-Cohen struct iommu_domain *domain = rproc->domain; 137b5ab5e24SOhad Ben-Cohen struct device *dev = rproc->dev.parent; 138400e64dfSOhad Ben-Cohen 139400e64dfSOhad Ben-Cohen if (!domain) 140400e64dfSOhad Ben-Cohen return; 141400e64dfSOhad Ben-Cohen 142400e64dfSOhad Ben-Cohen iommu_detach_device(domain, dev); 143400e64dfSOhad Ben-Cohen iommu_domain_free(domain); 144400e64dfSOhad Ben-Cohen } 145400e64dfSOhad Ben-Cohen 146086d0872SLoic Pallardy phys_addr_t rproc_va_to_pa(void *cpu_addr) 147eb30596eSLoic Pallardy { 148eb30596eSLoic Pallardy /* 149eb30596eSLoic Pallardy * Return physical address according to virtual address location 150eb30596eSLoic Pallardy * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent 151eb30596eSLoic Pallardy * - in kernel: if region allocated in generic dma memory pool 152eb30596eSLoic Pallardy */ 153eb30596eSLoic Pallardy if (is_vmalloc_addr(cpu_addr)) { 154eb30596eSLoic Pallardy return page_to_phys(vmalloc_to_page(cpu_addr)) + 155eb30596eSLoic Pallardy offset_in_page(cpu_addr); 156eb30596eSLoic Pallardy } 157eb30596eSLoic Pallardy 158eb30596eSLoic Pallardy WARN_ON(!virt_addr_valid(cpu_addr)); 159eb30596eSLoic Pallardy return virt_to_phys(cpu_addr); 160eb30596eSLoic Pallardy } 161086d0872SLoic Pallardy EXPORT_SYMBOL(rproc_va_to_pa); 162eb30596eSLoic Pallardy 163a01f7cd6SSuman Anna /** 164a01f7cd6SSuman Anna * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address 165a01f7cd6SSuman Anna * @rproc: handle of a remote processor 166a01f7cd6SSuman Anna * @da: remoteproc device address to translate 167a01f7cd6SSuman Anna * @len: length of the memory region @da is pointing to 168a01f7cd6SSuman Anna * 169400e64dfSOhad Ben-Cohen * Some remote processors will ask us to allocate them physically contiguous 170400e64dfSOhad Ben-Cohen * memory regions (which we call "carveouts"), and map them to specific 171a01f7cd6SSuman Anna * device addresses (which are hardcoded in the firmware). They may also have 172a01f7cd6SSuman Anna * dedicated memory regions internal to the processors, and use them either 173a01f7cd6SSuman Anna * exclusively or alongside carveouts. 174400e64dfSOhad Ben-Cohen * 175400e64dfSOhad Ben-Cohen * They may then ask us to copy objects into specific device addresses (e.g. 176400e64dfSOhad Ben-Cohen * code/data sections) or expose us certain symbols in other device address 177400e64dfSOhad Ben-Cohen * (e.g. their trace buffer). 178400e64dfSOhad Ben-Cohen * 179a01f7cd6SSuman Anna * This function is a helper function with which we can go over the allocated 180a01f7cd6SSuman Anna * carveouts and translate specific device addresses to kernel virtual addresses 181a01f7cd6SSuman Anna * so we can access the referenced memory. This function also allows to perform 182a01f7cd6SSuman Anna * translations on the internal remoteproc memory regions through a platform 183a01f7cd6SSuman Anna * implementation specific da_to_va ops, if present. 184a01f7cd6SSuman Anna * 185a01f7cd6SSuman Anna * The function returns a valid kernel address on success or NULL on failure. 186400e64dfSOhad Ben-Cohen * 187400e64dfSOhad Ben-Cohen * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, 188400e64dfSOhad Ben-Cohen * but only on kernel direct mapped RAM memory. Instead, we're just using 189a01f7cd6SSuman Anna * here the output of the DMA API for the carveouts, which should be more 190a01f7cd6SSuman Anna * correct. 191400e64dfSOhad Ben-Cohen */ 19240df0a91SPeng Fan void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem) 193400e64dfSOhad Ben-Cohen { 194400e64dfSOhad Ben-Cohen struct rproc_mem_entry *carveout; 195400e64dfSOhad Ben-Cohen void *ptr = NULL; 196400e64dfSOhad Ben-Cohen 197a01f7cd6SSuman Anna if (rproc->ops->da_to_va) { 19840df0a91SPeng Fan ptr = rproc->ops->da_to_va(rproc, da, len, is_iomem); 199a01f7cd6SSuman Anna if (ptr) 200a01f7cd6SSuman Anna goto out; 201a01f7cd6SSuman Anna } 202a01f7cd6SSuman Anna 203400e64dfSOhad Ben-Cohen list_for_each_entry(carveout, &rproc->carveouts, node) { 204400e64dfSOhad Ben-Cohen int offset = da - carveout->da; 205400e64dfSOhad Ben-Cohen 20674457c40SLoic Pallardy /* Verify that carveout is allocated */ 20774457c40SLoic Pallardy if (!carveout->va) 20874457c40SLoic Pallardy continue; 20974457c40SLoic Pallardy 210400e64dfSOhad Ben-Cohen /* try next carveout if da is too small */ 211400e64dfSOhad Ben-Cohen if (offset < 0) 212400e64dfSOhad Ben-Cohen continue; 213400e64dfSOhad Ben-Cohen 214400e64dfSOhad Ben-Cohen /* try next carveout if da is too large */ 215400e64dfSOhad Ben-Cohen if (offset + len > carveout->len) 216400e64dfSOhad Ben-Cohen continue; 217400e64dfSOhad Ben-Cohen 218400e64dfSOhad Ben-Cohen ptr = carveout->va + offset; 219400e64dfSOhad Ben-Cohen 22040df0a91SPeng Fan if (is_iomem) 22140df0a91SPeng Fan *is_iomem = carveout->is_iomem; 22240df0a91SPeng Fan 223400e64dfSOhad Ben-Cohen break; 224400e64dfSOhad Ben-Cohen } 225400e64dfSOhad Ben-Cohen 226a01f7cd6SSuman Anna out: 227400e64dfSOhad Ben-Cohen return ptr; 228400e64dfSOhad Ben-Cohen } 2294afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va); 230400e64dfSOhad Ben-Cohen 231b0019ccdSLoic Pallardy /** 232b0019ccdSLoic Pallardy * rproc_find_carveout_by_name() - lookup the carveout region by a name 233b0019ccdSLoic Pallardy * @rproc: handle of a remote processor 2342e7d4c2cSArnaud Pouliquen * @name: carveout name to find (format string) 2352e7d4c2cSArnaud Pouliquen * @...: optional parameters matching @name string 236b0019ccdSLoic Pallardy * 237b0019ccdSLoic Pallardy * Platform driver has the capability to register some pre-allacoted carveout 238b0019ccdSLoic Pallardy * (physically contiguous memory regions) before rproc firmware loading and 239b0019ccdSLoic Pallardy * associated resource table analysis. These regions may be dedicated memory 240b0019ccdSLoic Pallardy * regions internal to the coprocessor or specified DDR region with specific 241b0019ccdSLoic Pallardy * attributes 242b0019ccdSLoic Pallardy * 243b0019ccdSLoic Pallardy * This function is a helper function with which we can go over the 244b0019ccdSLoic Pallardy * allocated carveouts and return associated region characteristics like 245b0019ccdSLoic Pallardy * coprocessor address, length or processor virtual address. 246b0019ccdSLoic Pallardy * 247b0019ccdSLoic Pallardy * Return: a valid pointer on carveout entry on success or NULL on failure. 248b0019ccdSLoic Pallardy */ 2497e05c8deSLee Jones __printf(2, 3) 250b0019ccdSLoic Pallardy struct rproc_mem_entry * 251b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...) 252b0019ccdSLoic Pallardy { 253b0019ccdSLoic Pallardy va_list args; 254b0019ccdSLoic Pallardy char _name[32]; 255b0019ccdSLoic Pallardy struct rproc_mem_entry *carveout, *mem = NULL; 256b0019ccdSLoic Pallardy 257b0019ccdSLoic Pallardy if (!name) 258b0019ccdSLoic Pallardy return NULL; 259b0019ccdSLoic Pallardy 260b0019ccdSLoic Pallardy va_start(args, name); 261b0019ccdSLoic Pallardy vsnprintf(_name, sizeof(_name), name, args); 262b0019ccdSLoic Pallardy va_end(args); 263b0019ccdSLoic Pallardy 264b0019ccdSLoic Pallardy list_for_each_entry(carveout, &rproc->carveouts, node) { 265b0019ccdSLoic Pallardy /* Compare carveout and requested names */ 266b0019ccdSLoic Pallardy if (!strcmp(carveout->name, _name)) { 267b0019ccdSLoic Pallardy mem = carveout; 268b0019ccdSLoic Pallardy break; 269b0019ccdSLoic Pallardy } 270b0019ccdSLoic Pallardy } 271b0019ccdSLoic Pallardy 272b0019ccdSLoic Pallardy return mem; 273b0019ccdSLoic Pallardy } 274b0019ccdSLoic Pallardy 275c874bf59SLoic Pallardy /** 276c874bf59SLoic Pallardy * rproc_check_carveout_da() - Check specified carveout da configuration 277c874bf59SLoic Pallardy * @rproc: handle of a remote processor 278c874bf59SLoic Pallardy * @mem: pointer on carveout to check 279c874bf59SLoic Pallardy * @da: area device address 280c874bf59SLoic Pallardy * @len: associated area size 281c874bf59SLoic Pallardy * 282c874bf59SLoic Pallardy * This function is a helper function to verify requested device area (couple 28328d7d5c6SLoic Pallardy * da, len) is part of specified carveout. 28428d7d5c6SLoic Pallardy * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is 28528d7d5c6SLoic Pallardy * checked. 286c874bf59SLoic Pallardy * 28728d7d5c6SLoic Pallardy * Return: 0 if carveout matches request else error 288c874bf59SLoic Pallardy */ 28928d7d5c6SLoic Pallardy static int rproc_check_carveout_da(struct rproc *rproc, 29028d7d5c6SLoic Pallardy struct rproc_mem_entry *mem, u32 da, u32 len) 291c874bf59SLoic Pallardy { 292c874bf59SLoic Pallardy struct device *dev = &rproc->dev; 29328d7d5c6SLoic Pallardy int delta; 294c874bf59SLoic Pallardy 295c874bf59SLoic Pallardy /* Check requested resource length */ 296c874bf59SLoic Pallardy if (len > mem->len) { 297c874bf59SLoic Pallardy dev_err(dev, "Registered carveout doesn't fit len request\n"); 29828d7d5c6SLoic Pallardy return -EINVAL; 299c874bf59SLoic Pallardy } 300c874bf59SLoic Pallardy 301c874bf59SLoic Pallardy if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) { 30228d7d5c6SLoic Pallardy /* Address doesn't match registered carveout configuration */ 30328d7d5c6SLoic Pallardy return -EINVAL; 304c874bf59SLoic Pallardy } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) { 305c874bf59SLoic Pallardy delta = da - mem->da; 306c874bf59SLoic Pallardy 307c874bf59SLoic Pallardy /* Check requested resource belongs to registered carveout */ 308c874bf59SLoic Pallardy if (delta < 0) { 309c874bf59SLoic Pallardy dev_err(dev, 310c874bf59SLoic Pallardy "Registered carveout doesn't fit da request\n"); 31128d7d5c6SLoic Pallardy return -EINVAL; 312c874bf59SLoic Pallardy } 313c874bf59SLoic Pallardy 314c874bf59SLoic Pallardy if (delta + len > mem->len) { 315c874bf59SLoic Pallardy dev_err(dev, 316c874bf59SLoic Pallardy "Registered carveout doesn't fit len request\n"); 31728d7d5c6SLoic Pallardy return -EINVAL; 318c874bf59SLoic Pallardy } 319c874bf59SLoic Pallardy } 320c874bf59SLoic Pallardy 321c874bf59SLoic Pallardy return 0; 322c874bf59SLoic Pallardy } 323c874bf59SLoic Pallardy 3246db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) 325400e64dfSOhad Ben-Cohen { 3267a186941SOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 327b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 3286db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 329c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 330096ee786SClement Leger int ret, notifyid; 331c6aed238SLoic Pallardy struct rproc_mem_entry *mem; 332096ee786SClement Leger size_t size; 333400e64dfSOhad Ben-Cohen 3346db20ea8SOhad Ben-Cohen /* actual size of vring (in bytes) */ 3356db20ea8SOhad Ben-Cohen size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); 3366db20ea8SOhad Ben-Cohen 337c6aed238SLoic Pallardy rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; 338c6aed238SLoic Pallardy 339c6aed238SLoic Pallardy /* Search for pre-registered carveout */ 340c6aed238SLoic Pallardy mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index, 341c6aed238SLoic Pallardy i); 342c6aed238SLoic Pallardy if (mem) { 343c6aed238SLoic Pallardy if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size)) 344c6aed238SLoic Pallardy return -ENOMEM; 345c6aed238SLoic Pallardy } else { 346c6aed238SLoic Pallardy /* Register carveout in in list */ 34799cf0361SBen Dooks (Codethink) mem = rproc_mem_entry_init(dev, NULL, 0, 34899cf0361SBen Dooks (Codethink) size, rsc->vring[i].da, 349c6aed238SLoic Pallardy rproc_alloc_carveout, 350c6aed238SLoic Pallardy rproc_release_carveout, 351c6aed238SLoic Pallardy "vdev%dvring%d", 352c6aed238SLoic Pallardy rvdev->index, i); 353c6aed238SLoic Pallardy if (!mem) { 354c6aed238SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 355c6aed238SLoic Pallardy return -ENOMEM; 356c6aed238SLoic Pallardy } 357c6aed238SLoic Pallardy 358c6aed238SLoic Pallardy rproc_add_carveout(rproc, mem); 3596db20ea8SOhad Ben-Cohen } 3606db20ea8SOhad Ben-Cohen 3616db20ea8SOhad Ben-Cohen /* 3626db20ea8SOhad Ben-Cohen * Assign an rproc-wide unique index for this vring 3636db20ea8SOhad Ben-Cohen * TODO: assign a notifyid for rvdev updates as well 3646db20ea8SOhad Ben-Cohen * TODO: support predefined notifyids (via resource table) 3656db20ea8SOhad Ben-Cohen */ 36615fc6110STejun Heo ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); 367b39599b7SSuman Anna if (ret < 0) { 36815fc6110STejun Heo dev_err(dev, "idr_alloc failed: %d\n", ret); 3696db20ea8SOhad Ben-Cohen return ret; 3706db20ea8SOhad Ben-Cohen } 37115fc6110STejun Heo notifyid = ret; 3726db20ea8SOhad Ben-Cohen 37348f18f89SBjorn Andersson /* Potentially bump max_notifyid */ 37448f18f89SBjorn Andersson if (notifyid > rproc->max_notifyid) 37548f18f89SBjorn Andersson rproc->max_notifyid = notifyid; 37648f18f89SBjorn Andersson 3776db20ea8SOhad Ben-Cohen rvring->notifyid = notifyid; 3786db20ea8SOhad Ben-Cohen 379c6aed238SLoic Pallardy /* Let the rproc know the notifyid of this vring.*/ 380c0d63157SSjur Brændeland rsc->vring[i].notifyid = notifyid; 3816db20ea8SOhad Ben-Cohen return 0; 3826db20ea8SOhad Ben-Cohen } 3836db20ea8SOhad Ben-Cohen 384400e64dfSOhad Ben-Cohen static int 3856db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) 386400e64dfSOhad Ben-Cohen { 387400e64dfSOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 388b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 389400e64dfSOhad Ben-Cohen struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; 3906db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 391400e64dfSOhad Ben-Cohen 3929d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n", 3937a186941SOhad Ben-Cohen i, vring->da, vring->num, vring->align); 3947a186941SOhad Ben-Cohen 39563140e0eSOhad Ben-Cohen /* verify queue size and vring alignment are sane */ 39663140e0eSOhad Ben-Cohen if (!vring->num || !vring->align) { 39763140e0eSOhad Ben-Cohen dev_err(dev, "invalid qsz (%d) or alignment (%d)\n", 39863140e0eSOhad Ben-Cohen vring->num, vring->align); 399400e64dfSOhad Ben-Cohen return -EINVAL; 400400e64dfSOhad Ben-Cohen } 401400e64dfSOhad Ben-Cohen 4026db20ea8SOhad Ben-Cohen rvring->len = vring->num; 4036db20ea8SOhad Ben-Cohen rvring->align = vring->align; 4046db20ea8SOhad Ben-Cohen rvring->rvdev = rvdev; 405400e64dfSOhad Ben-Cohen 406400e64dfSOhad Ben-Cohen return 0; 407400e64dfSOhad Ben-Cohen } 408400e64dfSOhad Ben-Cohen 4096db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring) 4107a186941SOhad Ben-Cohen { 4116db20ea8SOhad Ben-Cohen struct rproc *rproc = rvring->rvdev->rproc; 41200a0eec5SClement Leger int idx = rvring - rvring->rvdev->vring; 413c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 4147a186941SOhad Ben-Cohen 4157a186941SOhad Ben-Cohen idr_remove(&rproc->notifyids, rvring->notifyid); 416099a3f33SSjur Brændeland 4174d3ebb3bSMathieu Poirier /* 4184d3ebb3bSMathieu Poirier * At this point rproc_stop() has been called and the installed resource 4194d3ebb3bSMathieu Poirier * table in the remote processor memory may no longer be accessible. As 4204d3ebb3bSMathieu Poirier * such and as per rproc_stop(), rproc->table_ptr points to the cached 4214d3ebb3bSMathieu Poirier * resource table (rproc->cached_table). The cached resource table is 4224d3ebb3bSMathieu Poirier * only available when a remote processor has been booted by the 4234d3ebb3bSMathieu Poirier * remoteproc core, otherwise it is NULL. 4244d3ebb3bSMathieu Poirier * 4254d3ebb3bSMathieu Poirier * Based on the above, reset the virtio device section in the cached 4264d3ebb3bSMathieu Poirier * resource table only if there is one to work with. 4274d3ebb3bSMathieu Poirier */ 4284d3ebb3bSMathieu Poirier if (rproc->table_ptr) { 429c0d63157SSjur Brændeland rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; 430c0d63157SSjur Brændeland rsc->vring[idx].da = 0; 431c0d63157SSjur Brændeland rsc->vring[idx].notifyid = -1; 4327a186941SOhad Ben-Cohen } 4334d3ebb3bSMathieu Poirier } 4347a186941SOhad Ben-Cohen 4356f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev) 436f5bcb353SBjorn Andersson { 437f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 438f5bcb353SBjorn Andersson 439f5bcb353SBjorn Andersson return rproc_add_virtio_dev(rvdev, rvdev->id); 440f5bcb353SBjorn Andersson } 441f5bcb353SBjorn Andersson 4426f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed) 443f5bcb353SBjorn Andersson { 444f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 445d4c036feSLoic Pallardy int ret; 446f5bcb353SBjorn Andersson 447d4c036feSLoic Pallardy ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev); 448d4c036feSLoic Pallardy if (ret) 449d4c036feSLoic Pallardy dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret); 450f5bcb353SBjorn Andersson } 451f5bcb353SBjorn Andersson 452400e64dfSOhad Ben-Cohen /** 453086d0872SLoic Pallardy * rproc_rvdev_release() - release the existence of a rvdev 454086d0872SLoic Pallardy * 455086d0872SLoic Pallardy * @dev: the subdevice's dev 456086d0872SLoic Pallardy */ 457086d0872SLoic Pallardy static void rproc_rvdev_release(struct device *dev) 458086d0872SLoic Pallardy { 459086d0872SLoic Pallardy struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev); 460086d0872SLoic Pallardy 461086d0872SLoic Pallardy of_reserved_mem_device_release(dev); 462086d0872SLoic Pallardy 463086d0872SLoic Pallardy kfree(rvdev); 464086d0872SLoic Pallardy } 465086d0872SLoic Pallardy 466e0d07278SJim Quinlan static int copy_dma_range_map(struct device *to, struct device *from) 467e0d07278SJim Quinlan { 468e0d07278SJim Quinlan const struct bus_dma_region *map = from->dma_range_map, *new_map, *r; 469e0d07278SJim Quinlan int num_ranges = 0; 470e0d07278SJim Quinlan 471e0d07278SJim Quinlan if (!map) 472e0d07278SJim Quinlan return 0; 473e0d07278SJim Quinlan 474e0d07278SJim Quinlan for (r = map; r->size; r++) 475e0d07278SJim Quinlan num_ranges++; 476e0d07278SJim Quinlan 477e0d07278SJim Quinlan new_map = kmemdup(map, array_size(num_ranges + 1, sizeof(*map)), 478e0d07278SJim Quinlan GFP_KERNEL); 479e0d07278SJim Quinlan if (!new_map) 480e0d07278SJim Quinlan return -ENOMEM; 481e0d07278SJim Quinlan to->dma_range_map = new_map; 482e0d07278SJim Quinlan return 0; 483e0d07278SJim Quinlan } 484e0d07278SJim Quinlan 485086d0872SLoic Pallardy /** 486fd2c15ecSOhad Ben-Cohen * rproc_handle_vdev() - handle a vdev fw resource 487400e64dfSOhad Ben-Cohen * @rproc: the remote processor 4882bf23461SJindong Yue * @ptr: the vring resource descriptor 4892e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 490fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 491400e64dfSOhad Ben-Cohen * 4927a186941SOhad Ben-Cohen * This resource entry requests the host to statically register a virtio 4937a186941SOhad Ben-Cohen * device (vdev), and setup everything needed to support it. It contains 4947a186941SOhad Ben-Cohen * everything needed to make it possible: the virtio device id, virtio 4957a186941SOhad Ben-Cohen * device features, vrings information, virtio config space, etc... 496400e64dfSOhad Ben-Cohen * 4977a186941SOhad Ben-Cohen * Before registering the vdev, the vrings are allocated from non-cacheable 4987a186941SOhad Ben-Cohen * physically contiguous memory. Currently we only support two vrings per 4997a186941SOhad Ben-Cohen * remote processor (temporary limitation). We might also want to consider 5007a186941SOhad Ben-Cohen * doing the vring allocation only later when ->find_vqs() is invoked, and 5017a186941SOhad Ben-Cohen * then release them upon ->del_vqs(). 502400e64dfSOhad Ben-Cohen * 5037a186941SOhad Ben-Cohen * Note: @da is currently not really handled correctly: we dynamically 5047a186941SOhad Ben-Cohen * allocate it using the DMA API, ignoring requested hard coded addresses, 5057a186941SOhad Ben-Cohen * and we don't take care of any required IOMMU programming. This is all 5067a186941SOhad Ben-Cohen * going to be taken care of when the generic iommu-based DMA API will be 5077a186941SOhad Ben-Cohen * merged. Meanwhile, statically-addressed iommu-based firmware images should 5087a186941SOhad Ben-Cohen * use RSC_DEVMEM resource entries to map their required @da to the physical 5097a186941SOhad Ben-Cohen * address of their base CMA region (ouch, hacky!). 510400e64dfSOhad Ben-Cohen * 511400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 512400e64dfSOhad Ben-Cohen */ 5132bf23461SJindong Yue static int rproc_handle_vdev(struct rproc *rproc, void *ptr, 514a2b950acSOhad Ben-Cohen int offset, int avail) 515400e64dfSOhad Ben-Cohen { 5162bf23461SJindong Yue struct fw_rsc_vdev *rsc = ptr; 517b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 5187a186941SOhad Ben-Cohen struct rproc_vdev *rvdev; 5197a186941SOhad Ben-Cohen int i, ret; 520086d0872SLoic Pallardy char name[16]; 521fd2c15ecSOhad Ben-Cohen 522fd2c15ecSOhad Ben-Cohen /* make sure resource isn't truncated */ 523c8784657SGustavo A. R. Silva if (struct_size(rsc, vring, rsc->num_of_vrings) + rsc->config_len > 524c8784657SGustavo A. R. Silva avail) { 525b5ab5e24SOhad Ben-Cohen dev_err(dev, "vdev rsc is truncated\n"); 526fd2c15ecSOhad Ben-Cohen return -EINVAL; 527fd2c15ecSOhad Ben-Cohen } 528fd2c15ecSOhad Ben-Cohen 529fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 530fd2c15ecSOhad Ben-Cohen if (rsc->reserved[0] || rsc->reserved[1]) { 531fd2c15ecSOhad Ben-Cohen dev_err(dev, "vdev rsc has non zero reserved bytes\n"); 532fd2c15ecSOhad Ben-Cohen return -EINVAL; 533fd2c15ecSOhad Ben-Cohen } 534fd2c15ecSOhad Ben-Cohen 5359d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n", 536fd2c15ecSOhad Ben-Cohen rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); 537400e64dfSOhad Ben-Cohen 5387a186941SOhad Ben-Cohen /* we currently support only two vrings per rvdev */ 5397a186941SOhad Ben-Cohen if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { 540fd2c15ecSOhad Ben-Cohen dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); 541400e64dfSOhad Ben-Cohen return -EINVAL; 542400e64dfSOhad Ben-Cohen } 543400e64dfSOhad Ben-Cohen 544899585adSAnna, Suman rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL); 5457a186941SOhad Ben-Cohen if (!rvdev) 5467a186941SOhad Ben-Cohen return -ENOMEM; 5477a186941SOhad Ben-Cohen 548aab8d802SBjorn Andersson kref_init(&rvdev->refcount); 549aab8d802SBjorn Andersson 550f5bcb353SBjorn Andersson rvdev->id = rsc->id; 5517a186941SOhad Ben-Cohen rvdev->rproc = rproc; 552c6aed238SLoic Pallardy rvdev->index = rproc->nb_vdev++; 5537a186941SOhad Ben-Cohen 554086d0872SLoic Pallardy /* Initialise vdev subdevice */ 555086d0872SLoic Pallardy snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index); 556c774ad01SSuman Anna rvdev->dev.parent = &rproc->dev; 557e0d07278SJim Quinlan ret = copy_dma_range_map(&rvdev->dev, rproc->dev.parent); 558e0d07278SJim Quinlan if (ret) 559e0d07278SJim Quinlan return ret; 560086d0872SLoic Pallardy rvdev->dev.release = rproc_rvdev_release; 561086d0872SLoic Pallardy dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name); 562086d0872SLoic Pallardy dev_set_drvdata(&rvdev->dev, rvdev); 563086d0872SLoic Pallardy 564086d0872SLoic Pallardy ret = device_register(&rvdev->dev); 565086d0872SLoic Pallardy if (ret) { 566086d0872SLoic Pallardy put_device(&rvdev->dev); 567086d0872SLoic Pallardy return ret; 568086d0872SLoic Pallardy } 569086d0872SLoic Pallardy /* Make device dma capable by inheriting from parent's capabilities */ 570086d0872SLoic Pallardy set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent)); 571086d0872SLoic Pallardy 572086d0872SLoic Pallardy ret = dma_coerce_mask_and_coherent(&rvdev->dev, 573086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent)); 574086d0872SLoic Pallardy if (ret) { 575086d0872SLoic Pallardy dev_warn(dev, 576086d0872SLoic Pallardy "Failed to set DMA mask %llx. Trying to continue... %x\n", 577086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent), ret); 578086d0872SLoic Pallardy } 579086d0872SLoic Pallardy 5806db20ea8SOhad Ben-Cohen /* parse the vrings */ 581fd2c15ecSOhad Ben-Cohen for (i = 0; i < rsc->num_of_vrings; i++) { 5826db20ea8SOhad Ben-Cohen ret = rproc_parse_vring(rvdev, rsc, i); 5837a186941SOhad Ben-Cohen if (ret) 5846db20ea8SOhad Ben-Cohen goto free_rvdev; 585fd2c15ecSOhad Ben-Cohen } 586fd2c15ecSOhad Ben-Cohen 587a2b950acSOhad Ben-Cohen /* remember the resource offset*/ 588a2b950acSOhad Ben-Cohen rvdev->rsc_offset = offset; 589400e64dfSOhad Ben-Cohen 590a863af5dSBjorn Andersson /* allocate the vring resources */ 591a863af5dSBjorn Andersson for (i = 0; i < rsc->num_of_vrings; i++) { 592a863af5dSBjorn Andersson ret = rproc_alloc_vring(rvdev, i); 593a863af5dSBjorn Andersson if (ret) 594a863af5dSBjorn Andersson goto unwind_vring_allocations; 595a863af5dSBjorn Andersson } 596a863af5dSBjorn Andersson 5977a186941SOhad Ben-Cohen list_add_tail(&rvdev->node, &rproc->rvdevs); 598400e64dfSOhad Ben-Cohen 5996f8b0373SAlex Elder rvdev->subdev.start = rproc_vdev_do_start; 6006f8b0373SAlex Elder rvdev->subdev.stop = rproc_vdev_do_stop; 6014902676fSBjorn Andersson 6024902676fSBjorn Andersson rproc_add_subdev(rproc, &rvdev->subdev); 603400e64dfSOhad Ben-Cohen 604400e64dfSOhad Ben-Cohen return 0; 6057a186941SOhad Ben-Cohen 606a863af5dSBjorn Andersson unwind_vring_allocations: 607a863af5dSBjorn Andersson for (i--; i >= 0; i--) 608a863af5dSBjorn Andersson rproc_free_vring(&rvdev->vring[i]); 6096db20ea8SOhad Ben-Cohen free_rvdev: 610086d0872SLoic Pallardy device_unregister(&rvdev->dev); 6117a186941SOhad Ben-Cohen return ret; 612400e64dfSOhad Ben-Cohen } 613400e64dfSOhad Ben-Cohen 614aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref) 615aab8d802SBjorn Andersson { 616aab8d802SBjorn Andersson struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount); 617a863af5dSBjorn Andersson struct rproc_vring *rvring; 618f5bcb353SBjorn Andersson struct rproc *rproc = rvdev->rproc; 619a863af5dSBjorn Andersson int id; 620a863af5dSBjorn Andersson 621a863af5dSBjorn Andersson for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) { 622a863af5dSBjorn Andersson rvring = &rvdev->vring[id]; 623a863af5dSBjorn Andersson rproc_free_vring(rvring); 624a863af5dSBjorn Andersson } 625aab8d802SBjorn Andersson 626f5bcb353SBjorn Andersson rproc_remove_subdev(rproc, &rvdev->subdev); 627aab8d802SBjorn Andersson list_del(&rvdev->node); 628086d0872SLoic Pallardy device_unregister(&rvdev->dev); 629aab8d802SBjorn Andersson } 630aab8d802SBjorn Andersson 631400e64dfSOhad Ben-Cohen /** 632400e64dfSOhad Ben-Cohen * rproc_handle_trace() - handle a shared trace buffer resource 633400e64dfSOhad Ben-Cohen * @rproc: the remote processor 6342bf23461SJindong Yue * @ptr: the trace resource descriptor 6352e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 636fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 637400e64dfSOhad Ben-Cohen * 638400e64dfSOhad Ben-Cohen * In case the remote processor dumps trace logs into memory, 639400e64dfSOhad Ben-Cohen * export it via debugfs. 640400e64dfSOhad Ben-Cohen * 641400e64dfSOhad Ben-Cohen * Currently, the 'da' member of @rsc should contain the device address 642400e64dfSOhad Ben-Cohen * where the remote processor is dumping the traces. Later we could also 643400e64dfSOhad Ben-Cohen * support dynamically allocating this address using the generic 644400e64dfSOhad Ben-Cohen * DMA API (but currently there isn't a use case for that). 645400e64dfSOhad Ben-Cohen * 646400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 647400e64dfSOhad Ben-Cohen */ 6482bf23461SJindong Yue static int rproc_handle_trace(struct rproc *rproc, void *ptr, 649a2b950acSOhad Ben-Cohen int offset, int avail) 650400e64dfSOhad Ben-Cohen { 6512bf23461SJindong Yue struct fw_rsc_trace *rsc = ptr; 652a987e6b9SLoic Pallardy struct rproc_debug_trace *trace; 653b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 654400e64dfSOhad Ben-Cohen char name[15]; 655400e64dfSOhad Ben-Cohen 656fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 657b5ab5e24SOhad Ben-Cohen dev_err(dev, "trace rsc is truncated\n"); 658fd2c15ecSOhad Ben-Cohen return -EINVAL; 659fd2c15ecSOhad Ben-Cohen } 660fd2c15ecSOhad Ben-Cohen 661fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 662fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 663fd2c15ecSOhad Ben-Cohen dev_err(dev, "trace rsc has non zero reserved bytes\n"); 664fd2c15ecSOhad Ben-Cohen return -EINVAL; 665fd2c15ecSOhad Ben-Cohen } 666fd2c15ecSOhad Ben-Cohen 667400e64dfSOhad Ben-Cohen trace = kzalloc(sizeof(*trace), GFP_KERNEL); 668172e6ab1SSuman Anna if (!trace) 669400e64dfSOhad Ben-Cohen return -ENOMEM; 670400e64dfSOhad Ben-Cohen 671400e64dfSOhad Ben-Cohen /* set the trace buffer dma properties */ 672a987e6b9SLoic Pallardy trace->trace_mem.len = rsc->len; 673a987e6b9SLoic Pallardy trace->trace_mem.da = rsc->da; 674a987e6b9SLoic Pallardy 675a987e6b9SLoic Pallardy /* set pointer on rproc device */ 676a987e6b9SLoic Pallardy trace->rproc = rproc; 677400e64dfSOhad Ben-Cohen 678400e64dfSOhad Ben-Cohen /* make sure snprintf always null terminates, even if truncating */ 679400e64dfSOhad Ben-Cohen snprintf(name, sizeof(name), "trace%d", rproc->num_traces); 680400e64dfSOhad Ben-Cohen 681400e64dfSOhad Ben-Cohen /* create the debugfs entry */ 682a987e6b9SLoic Pallardy trace->tfile = rproc_create_trace_file(name, rproc, trace); 683a987e6b9SLoic Pallardy if (!trace->tfile) { 684400e64dfSOhad Ben-Cohen kfree(trace); 685400e64dfSOhad Ben-Cohen return -EINVAL; 686400e64dfSOhad Ben-Cohen } 687400e64dfSOhad Ben-Cohen 688400e64dfSOhad Ben-Cohen list_add_tail(&trace->node, &rproc->traces); 689400e64dfSOhad Ben-Cohen 690400e64dfSOhad Ben-Cohen rproc->num_traces++; 691400e64dfSOhad Ben-Cohen 692a987e6b9SLoic Pallardy dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n", 693a987e6b9SLoic Pallardy name, rsc->da, rsc->len); 694400e64dfSOhad Ben-Cohen 695400e64dfSOhad Ben-Cohen return 0; 696400e64dfSOhad Ben-Cohen } 697400e64dfSOhad Ben-Cohen 698400e64dfSOhad Ben-Cohen /** 699400e64dfSOhad Ben-Cohen * rproc_handle_devmem() - handle devmem resource entry 700400e64dfSOhad Ben-Cohen * @rproc: remote processor handle 7012bf23461SJindong Yue * @ptr: the devmem resource entry 7022e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 703fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 704400e64dfSOhad Ben-Cohen * 705400e64dfSOhad Ben-Cohen * Remote processors commonly need to access certain on-chip peripherals. 706400e64dfSOhad Ben-Cohen * 707400e64dfSOhad Ben-Cohen * Some of these remote processors access memory via an iommu device, 708400e64dfSOhad Ben-Cohen * and might require us to configure their iommu before they can access 709400e64dfSOhad Ben-Cohen * the on-chip peripherals they need. 710400e64dfSOhad Ben-Cohen * 711400e64dfSOhad Ben-Cohen * This resource entry is a request to map such a peripheral device. 712400e64dfSOhad Ben-Cohen * 713400e64dfSOhad Ben-Cohen * These devmem entries will contain the physical address of the device in 714400e64dfSOhad Ben-Cohen * the 'pa' member. If a specific device address is expected, then 'da' will 715400e64dfSOhad Ben-Cohen * contain it (currently this is the only use case supported). 'len' will 716400e64dfSOhad Ben-Cohen * contain the size of the physical region we need to map. 717400e64dfSOhad Ben-Cohen * 718400e64dfSOhad Ben-Cohen * Currently we just "trust" those devmem entries to contain valid physical 719400e64dfSOhad Ben-Cohen * addresses, but this is going to change: we want the implementations to 720400e64dfSOhad Ben-Cohen * tell us ranges of physical addresses the firmware is allowed to request, 721400e64dfSOhad Ben-Cohen * and not allow firmwares to request access to physical addresses that 722400e64dfSOhad Ben-Cohen * are outside those ranges. 723400e64dfSOhad Ben-Cohen */ 7242bf23461SJindong Yue static int rproc_handle_devmem(struct rproc *rproc, void *ptr, 725a2b950acSOhad Ben-Cohen int offset, int avail) 726400e64dfSOhad Ben-Cohen { 7272bf23461SJindong Yue struct fw_rsc_devmem *rsc = ptr; 728400e64dfSOhad Ben-Cohen struct rproc_mem_entry *mapping; 729b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 730400e64dfSOhad Ben-Cohen int ret; 731400e64dfSOhad Ben-Cohen 732400e64dfSOhad Ben-Cohen /* no point in handling this resource without a valid iommu domain */ 733400e64dfSOhad Ben-Cohen if (!rproc->domain) 734400e64dfSOhad Ben-Cohen return -EINVAL; 735400e64dfSOhad Ben-Cohen 736fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 737b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc is truncated\n"); 738fd2c15ecSOhad Ben-Cohen return -EINVAL; 739fd2c15ecSOhad Ben-Cohen } 740fd2c15ecSOhad Ben-Cohen 741fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 742fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 743b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc has non zero reserved bytes\n"); 744fd2c15ecSOhad Ben-Cohen return -EINVAL; 745fd2c15ecSOhad Ben-Cohen } 746fd2c15ecSOhad Ben-Cohen 747400e64dfSOhad Ben-Cohen mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 748172e6ab1SSuman Anna if (!mapping) 749400e64dfSOhad Ben-Cohen return -ENOMEM; 750400e64dfSOhad Ben-Cohen 751400e64dfSOhad Ben-Cohen ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); 752400e64dfSOhad Ben-Cohen if (ret) { 753b5ab5e24SOhad Ben-Cohen dev_err(dev, "failed to map devmem: %d\n", ret); 754400e64dfSOhad Ben-Cohen goto out; 755400e64dfSOhad Ben-Cohen } 756400e64dfSOhad Ben-Cohen 757400e64dfSOhad Ben-Cohen /* 758400e64dfSOhad Ben-Cohen * We'll need this info later when we'll want to unmap everything 759400e64dfSOhad Ben-Cohen * (e.g. on shutdown). 760400e64dfSOhad Ben-Cohen * 761400e64dfSOhad Ben-Cohen * We can't trust the remote processor not to change the resource 762400e64dfSOhad Ben-Cohen * table, so we must maintain this info independently. 763400e64dfSOhad Ben-Cohen */ 764400e64dfSOhad Ben-Cohen mapping->da = rsc->da; 765400e64dfSOhad Ben-Cohen mapping->len = rsc->len; 766400e64dfSOhad Ben-Cohen list_add_tail(&mapping->node, &rproc->mappings); 767400e64dfSOhad Ben-Cohen 768b5ab5e24SOhad Ben-Cohen dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", 769400e64dfSOhad Ben-Cohen rsc->pa, rsc->da, rsc->len); 770400e64dfSOhad Ben-Cohen 771400e64dfSOhad Ben-Cohen return 0; 772400e64dfSOhad Ben-Cohen 773400e64dfSOhad Ben-Cohen out: 774400e64dfSOhad Ben-Cohen kfree(mapping); 775400e64dfSOhad Ben-Cohen return ret; 776400e64dfSOhad Ben-Cohen } 777400e64dfSOhad Ben-Cohen 778400e64dfSOhad Ben-Cohen /** 779d7c51706SLoic Pallardy * rproc_alloc_carveout() - allocated specified carveout 780d7c51706SLoic Pallardy * @rproc: rproc handle 781d7c51706SLoic Pallardy * @mem: the memory entry to allocate 782d7c51706SLoic Pallardy * 783d7c51706SLoic Pallardy * This function allocate specified memory entry @mem using 784d7c51706SLoic Pallardy * dma_alloc_coherent() as default allocator 785d7c51706SLoic Pallardy */ 786d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc, 787d7c51706SLoic Pallardy struct rproc_mem_entry *mem) 788d7c51706SLoic Pallardy { 789d7c51706SLoic Pallardy struct rproc_mem_entry *mapping = NULL; 790d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 791d7c51706SLoic Pallardy dma_addr_t dma; 792d7c51706SLoic Pallardy void *va; 793d7c51706SLoic Pallardy int ret; 794d7c51706SLoic Pallardy 795d7c51706SLoic Pallardy va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL); 796d7c51706SLoic Pallardy if (!va) { 797d7c51706SLoic Pallardy dev_err(dev->parent, 798096ee786SClement Leger "failed to allocate dma memory: len 0x%zx\n", 799096ee786SClement Leger mem->len); 800d7c51706SLoic Pallardy return -ENOMEM; 801d7c51706SLoic Pallardy } 802d7c51706SLoic Pallardy 803096ee786SClement Leger dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n", 804d7c51706SLoic Pallardy va, &dma, mem->len); 805d7c51706SLoic Pallardy 80660f849a5SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) { 80760f849a5SLoic Pallardy /* 80860f849a5SLoic Pallardy * Check requested da is equal to dma address 80960f849a5SLoic Pallardy * and print a warn message in case of missalignment. 81060f849a5SLoic Pallardy * Don't stop rproc_start sequence as coprocessor may 81160f849a5SLoic Pallardy * build pa to da translation on its side. 81260f849a5SLoic Pallardy */ 81360f849a5SLoic Pallardy if (mem->da != (u32)dma) 81460f849a5SLoic Pallardy dev_warn(dev->parent, 81560f849a5SLoic Pallardy "Allocated carveout doesn't fit device address request\n"); 81660f849a5SLoic Pallardy } 81760f849a5SLoic Pallardy 818d7c51706SLoic Pallardy /* 819d7c51706SLoic Pallardy * Ok, this is non-standard. 820d7c51706SLoic Pallardy * 821d7c51706SLoic Pallardy * Sometimes we can't rely on the generic iommu-based DMA API 822d7c51706SLoic Pallardy * to dynamically allocate the device address and then set the IOMMU 823d7c51706SLoic Pallardy * tables accordingly, because some remote processors might 824d7c51706SLoic Pallardy * _require_ us to use hard coded device addresses that their 825d7c51706SLoic Pallardy * firmware was compiled with. 826d7c51706SLoic Pallardy * 827d7c51706SLoic Pallardy * In this case, we must use the IOMMU API directly and map 828d7c51706SLoic Pallardy * the memory to the device address as expected by the remote 829d7c51706SLoic Pallardy * processor. 830d7c51706SLoic Pallardy * 831d7c51706SLoic Pallardy * Obviously such remote processor devices should not be configured 832d7c51706SLoic Pallardy * to use the iommu-based DMA API: we expect 'dma' to contain the 833d7c51706SLoic Pallardy * physical address in this case. 834d7c51706SLoic Pallardy */ 83560f849a5SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) { 836d7c51706SLoic Pallardy mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 837d7c51706SLoic Pallardy if (!mapping) { 838d7c51706SLoic Pallardy ret = -ENOMEM; 839d7c51706SLoic Pallardy goto dma_free; 840d7c51706SLoic Pallardy } 841d7c51706SLoic Pallardy 842d7c51706SLoic Pallardy ret = iommu_map(rproc->domain, mem->da, dma, mem->len, 843d7c51706SLoic Pallardy mem->flags); 844d7c51706SLoic Pallardy if (ret) { 845d7c51706SLoic Pallardy dev_err(dev, "iommu_map failed: %d\n", ret); 846d7c51706SLoic Pallardy goto free_mapping; 847d7c51706SLoic Pallardy } 848d7c51706SLoic Pallardy 849d7c51706SLoic Pallardy /* 850d7c51706SLoic Pallardy * We'll need this info later when we'll want to unmap 851d7c51706SLoic Pallardy * everything (e.g. on shutdown). 852d7c51706SLoic Pallardy * 853d7c51706SLoic Pallardy * We can't trust the remote processor not to change the 854d7c51706SLoic Pallardy * resource table, so we must maintain this info independently. 855d7c51706SLoic Pallardy */ 856d7c51706SLoic Pallardy mapping->da = mem->da; 857d7c51706SLoic Pallardy mapping->len = mem->len; 858d7c51706SLoic Pallardy list_add_tail(&mapping->node, &rproc->mappings); 859d7c51706SLoic Pallardy 860d7c51706SLoic Pallardy dev_dbg(dev, "carveout mapped 0x%x to %pad\n", 861d7c51706SLoic Pallardy mem->da, &dma); 86260f849a5SLoic Pallardy } 86360f849a5SLoic Pallardy 86460f849a5SLoic Pallardy if (mem->da == FW_RSC_ADDR_ANY) { 865b36de8cfSLoic Pallardy /* Update device address as undefined by requester */ 866b36de8cfSLoic Pallardy if ((u64)dma & HIGH_BITS_MASK) 867b36de8cfSLoic Pallardy dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n"); 868b36de8cfSLoic Pallardy 869d7c51706SLoic Pallardy mem->da = (u32)dma; 870d7c51706SLoic Pallardy } 871d7c51706SLoic Pallardy 87280137b40SLoic Pallardy mem->dma = dma; 873d7c51706SLoic Pallardy mem->va = va; 874d7c51706SLoic Pallardy 875d7c51706SLoic Pallardy return 0; 876d7c51706SLoic Pallardy 877d7c51706SLoic Pallardy free_mapping: 878d7c51706SLoic Pallardy kfree(mapping); 879d7c51706SLoic Pallardy dma_free: 880d7c51706SLoic Pallardy dma_free_coherent(dev->parent, mem->len, va, dma); 881d7c51706SLoic Pallardy return ret; 882d7c51706SLoic Pallardy } 883d7c51706SLoic Pallardy 884d7c51706SLoic Pallardy /** 885f2e74abfSLoic Pallardy * rproc_release_carveout() - release acquired carveout 886f2e74abfSLoic Pallardy * @rproc: rproc handle 887f2e74abfSLoic Pallardy * @mem: the memory entry to release 888f2e74abfSLoic Pallardy * 889f2e74abfSLoic Pallardy * This function releases specified memory entry @mem allocated via 890d7c51706SLoic Pallardy * rproc_alloc_carveout() function by @rproc. 891f2e74abfSLoic Pallardy */ 892f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc, 893f2e74abfSLoic Pallardy struct rproc_mem_entry *mem) 894f2e74abfSLoic Pallardy { 895f2e74abfSLoic Pallardy struct device *dev = &rproc->dev; 896f2e74abfSLoic Pallardy 897f2e74abfSLoic Pallardy /* clean up carveout allocations */ 898f2e74abfSLoic Pallardy dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma); 899f2e74abfSLoic Pallardy return 0; 900f2e74abfSLoic Pallardy } 901f2e74abfSLoic Pallardy 902f2e74abfSLoic Pallardy /** 903400e64dfSOhad Ben-Cohen * rproc_handle_carveout() - handle phys contig memory allocation requests 904400e64dfSOhad Ben-Cohen * @rproc: rproc handle 9052bf23461SJindong Yue * @ptr: the resource entry 9062e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 907fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for image validation) 908400e64dfSOhad Ben-Cohen * 909400e64dfSOhad Ben-Cohen * This function will handle firmware requests for allocation of physically 910400e64dfSOhad Ben-Cohen * contiguous memory regions. 911400e64dfSOhad Ben-Cohen * 912400e64dfSOhad Ben-Cohen * These request entries should come first in the firmware's resource table, 913400e64dfSOhad Ben-Cohen * as other firmware entries might request placing other data objects inside 914400e64dfSOhad Ben-Cohen * these memory regions (e.g. data/code segments, trace resource entries, ...). 915400e64dfSOhad Ben-Cohen * 916400e64dfSOhad Ben-Cohen * Allocating memory this way helps utilizing the reserved physical memory 917400e64dfSOhad Ben-Cohen * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries 918400e64dfSOhad Ben-Cohen * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB 919400e64dfSOhad Ben-Cohen * pressure is important; it may have a substantial impact on performance. 920400e64dfSOhad Ben-Cohen */ 921fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc, 9222bf23461SJindong Yue void *ptr, int offset, int avail) 923400e64dfSOhad Ben-Cohen { 9242bf23461SJindong Yue struct fw_rsc_carveout *rsc = ptr; 925d7c51706SLoic Pallardy struct rproc_mem_entry *carveout; 926b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 927400e64dfSOhad Ben-Cohen 928fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 929b5ab5e24SOhad Ben-Cohen dev_err(dev, "carveout rsc is truncated\n"); 930fd2c15ecSOhad Ben-Cohen return -EINVAL; 931fd2c15ecSOhad Ben-Cohen } 932fd2c15ecSOhad Ben-Cohen 933fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 934fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 935fd2c15ecSOhad Ben-Cohen dev_err(dev, "carveout rsc has non zero reserved bytes\n"); 936fd2c15ecSOhad Ben-Cohen return -EINVAL; 937fd2c15ecSOhad Ben-Cohen } 938fd2c15ecSOhad Ben-Cohen 9399d7814a9SAnna, Suman dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n", 94035386166SLee Jones rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); 941fd2c15ecSOhad Ben-Cohen 942ffa5f9c8SLoic Pallardy /* 943ffa5f9c8SLoic Pallardy * Check carveout rsc already part of a registered carveout, 944ffa5f9c8SLoic Pallardy * Search by name, then check the da and length 945ffa5f9c8SLoic Pallardy */ 946ffa5f9c8SLoic Pallardy carveout = rproc_find_carveout_by_name(rproc, rsc->name); 947ffa5f9c8SLoic Pallardy 948ffa5f9c8SLoic Pallardy if (carveout) { 949ffa5f9c8SLoic Pallardy if (carveout->rsc_offset != FW_RSC_ADDR_ANY) { 950ffa5f9c8SLoic Pallardy dev_err(dev, 951ffa5f9c8SLoic Pallardy "Carveout already associated to resource table\n"); 952ffa5f9c8SLoic Pallardy return -ENOMEM; 953ffa5f9c8SLoic Pallardy } 954ffa5f9c8SLoic Pallardy 955ffa5f9c8SLoic Pallardy if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len)) 956ffa5f9c8SLoic Pallardy return -ENOMEM; 957ffa5f9c8SLoic Pallardy 958ffa5f9c8SLoic Pallardy /* Update memory carveout with resource table info */ 959ffa5f9c8SLoic Pallardy carveout->rsc_offset = offset; 960ffa5f9c8SLoic Pallardy carveout->flags = rsc->flags; 961ffa5f9c8SLoic Pallardy 962ffa5f9c8SLoic Pallardy return 0; 963ffa5f9c8SLoic Pallardy } 964ffa5f9c8SLoic Pallardy 965d7c51706SLoic Pallardy /* Register carveout in in list */ 96699cf0361SBen Dooks (Codethink) carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da, 967d7c51706SLoic Pallardy rproc_alloc_carveout, 968d7c51706SLoic Pallardy rproc_release_carveout, rsc->name); 969d7c51706SLoic Pallardy if (!carveout) { 970d7c51706SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 97172029c90SLoic Pallardy return -ENOMEM; 972400e64dfSOhad Ben-Cohen } 973400e64dfSOhad Ben-Cohen 974d7c51706SLoic Pallardy carveout->flags = rsc->flags; 975d7c51706SLoic Pallardy carveout->rsc_offset = offset; 97615c0b025SLoic Pallardy rproc_add_carveout(rproc, carveout); 977400e64dfSOhad Ben-Cohen 978400e64dfSOhad Ben-Cohen return 0; 979400e64dfSOhad Ben-Cohen } 980400e64dfSOhad Ben-Cohen 98172029c90SLoic Pallardy /** 98215c0b025SLoic Pallardy * rproc_add_carveout() - register an allocated carveout region 98315c0b025SLoic Pallardy * @rproc: rproc handle 98415c0b025SLoic Pallardy * @mem: memory entry to register 98515c0b025SLoic Pallardy * 98615c0b025SLoic Pallardy * This function registers specified memory entry in @rproc carveouts list. 98715c0b025SLoic Pallardy * Specified carveout should have been allocated before registering. 98815c0b025SLoic Pallardy */ 98915c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) 99015c0b025SLoic Pallardy { 99115c0b025SLoic Pallardy list_add_tail(&mem->node, &rproc->carveouts); 99215c0b025SLoic Pallardy } 99315c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout); 99415c0b025SLoic Pallardy 99515c0b025SLoic Pallardy /** 99672029c90SLoic Pallardy * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct 99772029c90SLoic Pallardy * @dev: pointer on device struct 99872029c90SLoic Pallardy * @va: virtual address 99972029c90SLoic Pallardy * @dma: dma address 100072029c90SLoic Pallardy * @len: memory carveout length 100172029c90SLoic Pallardy * @da: device address 1002a9f6fe0dSLoic Pallardy * @alloc: memory carveout allocation function 1003a9f6fe0dSLoic Pallardy * @release: memory carveout release function 100472029c90SLoic Pallardy * @name: carveout name 100572029c90SLoic Pallardy * 100672029c90SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 100772029c90SLoic Pallardy * provided by client. 100872029c90SLoic Pallardy */ 10097e05c8deSLee Jones __printf(8, 9) 101072029c90SLoic Pallardy struct rproc_mem_entry * 101172029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev, 1012096ee786SClement Leger void *va, dma_addr_t dma, size_t len, u32 da, 1013d7c51706SLoic Pallardy int (*alloc)(struct rproc *, struct rproc_mem_entry *), 101472029c90SLoic Pallardy int (*release)(struct rproc *, struct rproc_mem_entry *), 101572029c90SLoic Pallardy const char *name, ...) 101672029c90SLoic Pallardy { 101772029c90SLoic Pallardy struct rproc_mem_entry *mem; 101872029c90SLoic Pallardy va_list args; 101972029c90SLoic Pallardy 102072029c90SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 102172029c90SLoic Pallardy if (!mem) 102272029c90SLoic Pallardy return mem; 102372029c90SLoic Pallardy 102472029c90SLoic Pallardy mem->va = va; 102572029c90SLoic Pallardy mem->dma = dma; 102672029c90SLoic Pallardy mem->da = da; 102772029c90SLoic Pallardy mem->len = len; 1028d7c51706SLoic Pallardy mem->alloc = alloc; 102972029c90SLoic Pallardy mem->release = release; 1030d7c51706SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 10311429cca1SLoic Pallardy mem->of_resm_idx = -1; 103272029c90SLoic Pallardy 103372029c90SLoic Pallardy va_start(args, name); 103472029c90SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 103572029c90SLoic Pallardy va_end(args); 103672029c90SLoic Pallardy 103772029c90SLoic Pallardy return mem; 103872029c90SLoic Pallardy } 103972029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init); 104072029c90SLoic Pallardy 104172029c90SLoic Pallardy /** 10421429cca1SLoic Pallardy * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct 10431429cca1SLoic Pallardy * from a reserved memory phandle 10441429cca1SLoic Pallardy * @dev: pointer on device struct 10451429cca1SLoic Pallardy * @of_resm_idx: reserved memory phandle index in "memory-region" 10461429cca1SLoic Pallardy * @len: memory carveout length 10471429cca1SLoic Pallardy * @da: device address 10481429cca1SLoic Pallardy * @name: carveout name 10491429cca1SLoic Pallardy * 10501429cca1SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 10511429cca1SLoic Pallardy * provided by client. 10521429cca1SLoic Pallardy */ 10537e05c8deSLee Jones __printf(5, 6) 10541429cca1SLoic Pallardy struct rproc_mem_entry * 1055096ee786SClement Leger rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len, 10561429cca1SLoic Pallardy u32 da, const char *name, ...) 10571429cca1SLoic Pallardy { 10581429cca1SLoic Pallardy struct rproc_mem_entry *mem; 10591429cca1SLoic Pallardy va_list args; 10601429cca1SLoic Pallardy 10611429cca1SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 10621429cca1SLoic Pallardy if (!mem) 10631429cca1SLoic Pallardy return mem; 10641429cca1SLoic Pallardy 10651429cca1SLoic Pallardy mem->da = da; 10661429cca1SLoic Pallardy mem->len = len; 10671429cca1SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 10681429cca1SLoic Pallardy mem->of_resm_idx = of_resm_idx; 10691429cca1SLoic Pallardy 10701429cca1SLoic Pallardy va_start(args, name); 10711429cca1SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 10721429cca1SLoic Pallardy va_end(args); 10731429cca1SLoic Pallardy 10741429cca1SLoic Pallardy return mem; 10751429cca1SLoic Pallardy } 10761429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init); 10771429cca1SLoic Pallardy 1078a8aa5ee1SSuman Anna /** 1079a8aa5ee1SSuman Anna * rproc_of_parse_firmware() - parse and return the firmware-name 1080a8aa5ee1SSuman Anna * @dev: pointer on device struct representing a rproc 1081a8aa5ee1SSuman Anna * @index: index to use for the firmware-name retrieval 1082a8aa5ee1SSuman Anna * @fw_name: pointer to a character string, in which the firmware 1083a8aa5ee1SSuman Anna * name is returned on success and unmodified otherwise. 1084a8aa5ee1SSuman Anna * 1085a8aa5ee1SSuman Anna * This is an OF helper function that parses a device's DT node for 1086a8aa5ee1SSuman Anna * the "firmware-name" property and returns the firmware name pointer 1087a8aa5ee1SSuman Anna * in @fw_name on success. 1088a8aa5ee1SSuman Anna * 1089a8aa5ee1SSuman Anna * Return: 0 on success, or an appropriate failure. 1090a8aa5ee1SSuman Anna */ 1091a8aa5ee1SSuman Anna int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name) 1092a8aa5ee1SSuman Anna { 1093a8aa5ee1SSuman Anna int ret; 1094a8aa5ee1SSuman Anna 1095a8aa5ee1SSuman Anna ret = of_property_read_string_index(dev->of_node, "firmware-name", 1096a8aa5ee1SSuman Anna index, fw_name); 1097a8aa5ee1SSuman Anna return ret ? ret : 0; 1098a8aa5ee1SSuman Anna } 1099a8aa5ee1SSuman Anna EXPORT_SYMBOL(rproc_of_parse_firmware); 1100a8aa5ee1SSuman Anna 11012e7d4c2cSArnaud Pouliquen /* 1102e12bc14bSOhad Ben-Cohen * A lookup table for resource handlers. The indices are defined in 1103e12bc14bSOhad Ben-Cohen * enum fw_resource_type. 1104e12bc14bSOhad Ben-Cohen */ 1105232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { 11062bf23461SJindong Yue [RSC_CARVEOUT] = rproc_handle_carveout, 11072bf23461SJindong Yue [RSC_DEVMEM] = rproc_handle_devmem, 11082bf23461SJindong Yue [RSC_TRACE] = rproc_handle_trace, 11092bf23461SJindong Yue [RSC_VDEV] = rproc_handle_vdev, 1110232fcdbbSSjur Brændeland }; 1111232fcdbbSSjur Brændeland 1112400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */ 1113a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc, 1114232fcdbbSSjur Brændeland rproc_handle_resource_t handlers[RSC_LAST]) 1115400e64dfSOhad Ben-Cohen { 1116b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1117e12bc14bSOhad Ben-Cohen rproc_handle_resource_t handler; 1118fd2c15ecSOhad Ben-Cohen int ret = 0, i; 1119400e64dfSOhad Ben-Cohen 1120d4bb86f2SBjorn Andersson if (!rproc->table_ptr) 1121d4bb86f2SBjorn Andersson return 0; 1122d4bb86f2SBjorn Andersson 1123a2b950acSOhad Ben-Cohen for (i = 0; i < rproc->table_ptr->num; i++) { 1124a2b950acSOhad Ben-Cohen int offset = rproc->table_ptr->offset[i]; 1125a2b950acSOhad Ben-Cohen struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; 1126a4b24c75SBjorn Andersson int avail = rproc->table_sz - offset - sizeof(*hdr); 1127fd2c15ecSOhad Ben-Cohen void *rsc = (void *)hdr + sizeof(*hdr); 1128400e64dfSOhad Ben-Cohen 1129fd2c15ecSOhad Ben-Cohen /* make sure table isn't truncated */ 1130fd2c15ecSOhad Ben-Cohen if (avail < 0) { 1131fd2c15ecSOhad Ben-Cohen dev_err(dev, "rsc table is truncated\n"); 1132fd2c15ecSOhad Ben-Cohen return -EINVAL; 1133fd2c15ecSOhad Ben-Cohen } 1134fd2c15ecSOhad Ben-Cohen 1135fd2c15ecSOhad Ben-Cohen dev_dbg(dev, "rsc: type %d\n", hdr->type); 1136fd2c15ecSOhad Ben-Cohen 1137b1a17513SClement Leger if (hdr->type >= RSC_VENDOR_START && 1138b1a17513SClement Leger hdr->type <= RSC_VENDOR_END) { 1139b1a17513SClement Leger ret = rproc_handle_rsc(rproc, hdr->type, rsc, 1140b1a17513SClement Leger offset + sizeof(*hdr), avail); 1141b1a17513SClement Leger if (ret == RSC_HANDLED) 1142b1a17513SClement Leger continue; 1143b1a17513SClement Leger else if (ret < 0) 1144b1a17513SClement Leger break; 1145b1a17513SClement Leger 1146b1a17513SClement Leger dev_warn(dev, "unsupported vendor resource %d\n", 1147b1a17513SClement Leger hdr->type); 1148b1a17513SClement Leger continue; 1149b1a17513SClement Leger } 1150b1a17513SClement Leger 1151fd2c15ecSOhad Ben-Cohen if (hdr->type >= RSC_LAST) { 1152fd2c15ecSOhad Ben-Cohen dev_warn(dev, "unsupported resource %d\n", hdr->type); 1153e12bc14bSOhad Ben-Cohen continue; 1154400e64dfSOhad Ben-Cohen } 1155400e64dfSOhad Ben-Cohen 1156232fcdbbSSjur Brændeland handler = handlers[hdr->type]; 1157e12bc14bSOhad Ben-Cohen if (!handler) 1158e12bc14bSOhad Ben-Cohen continue; 1159e12bc14bSOhad Ben-Cohen 1160a2b950acSOhad Ben-Cohen ret = handler(rproc, rsc, offset + sizeof(*hdr), avail); 11617a186941SOhad Ben-Cohen if (ret) 1162400e64dfSOhad Ben-Cohen break; 1163400e64dfSOhad Ben-Cohen } 1164400e64dfSOhad Ben-Cohen 1165400e64dfSOhad Ben-Cohen return ret; 1166400e64dfSOhad Ben-Cohen } 1167400e64dfSOhad Ben-Cohen 1168c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc) 1169c455daa4SBjorn Andersson { 1170c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1171c455daa4SBjorn Andersson int ret; 1172c455daa4SBjorn Andersson 1173c455daa4SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1174c455daa4SBjorn Andersson if (subdev->prepare) { 1175c455daa4SBjorn Andersson ret = subdev->prepare(subdev); 1176c455daa4SBjorn Andersson if (ret) 1177c455daa4SBjorn Andersson goto unroll_preparation; 1178c455daa4SBjorn Andersson } 1179c455daa4SBjorn Andersson } 1180c455daa4SBjorn Andersson 1181c455daa4SBjorn Andersson return 0; 1182c455daa4SBjorn Andersson 1183c455daa4SBjorn Andersson unroll_preparation: 1184c455daa4SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1185c455daa4SBjorn Andersson if (subdev->unprepare) 1186c455daa4SBjorn Andersson subdev->unprepare(subdev); 1187c455daa4SBjorn Andersson } 1188c455daa4SBjorn Andersson 1189c455daa4SBjorn Andersson return ret; 1190c455daa4SBjorn Andersson } 1191c455daa4SBjorn Andersson 1192618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc) 11937bdc9650SBjorn Andersson { 11947bdc9650SBjorn Andersson struct rproc_subdev *subdev; 11957bdc9650SBjorn Andersson int ret; 11967bdc9650SBjorn Andersson 11977bdc9650SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1198be37b1e0SBjorn Andersson if (subdev->start) { 1199618fcff3SBjorn Andersson ret = subdev->start(subdev); 12007bdc9650SBjorn Andersson if (ret) 12017bdc9650SBjorn Andersson goto unroll_registration; 12027bdc9650SBjorn Andersson } 1203be37b1e0SBjorn Andersson } 12047bdc9650SBjorn Andersson 12057bdc9650SBjorn Andersson return 0; 12067bdc9650SBjorn Andersson 12077bdc9650SBjorn Andersson unroll_registration: 1208be37b1e0SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1209be37b1e0SBjorn Andersson if (subdev->stop) 1210618fcff3SBjorn Andersson subdev->stop(subdev, true); 1211be37b1e0SBjorn Andersson } 12127bdc9650SBjorn Andersson 12137bdc9650SBjorn Andersson return ret; 12147bdc9650SBjorn Andersson } 12157bdc9650SBjorn Andersson 1216618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed) 12177bdc9650SBjorn Andersson { 12187bdc9650SBjorn Andersson struct rproc_subdev *subdev; 12197bdc9650SBjorn Andersson 1220be37b1e0SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1221be37b1e0SBjorn Andersson if (subdev->stop) 1222618fcff3SBjorn Andersson subdev->stop(subdev, crashed); 12237bdc9650SBjorn Andersson } 1224be37b1e0SBjorn Andersson } 12257bdc9650SBjorn Andersson 1226c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc) 1227c455daa4SBjorn Andersson { 1228c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1229c455daa4SBjorn Andersson 1230c455daa4SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1231c455daa4SBjorn Andersson if (subdev->unprepare) 1232c455daa4SBjorn Andersson subdev->unprepare(subdev); 1233c455daa4SBjorn Andersson } 1234c455daa4SBjorn Andersson } 1235c455daa4SBjorn Andersson 1236400e64dfSOhad Ben-Cohen /** 1237d7c51706SLoic Pallardy * rproc_alloc_registered_carveouts() - allocate all carveouts registered 1238d7c51706SLoic Pallardy * in the list 1239d7c51706SLoic Pallardy * @rproc: the remote processor handle 1240d7c51706SLoic Pallardy * 1241d7c51706SLoic Pallardy * This function parses registered carveout list, performs allocation 1242d7c51706SLoic Pallardy * if alloc() ops registered and updates resource table information 1243d7c51706SLoic Pallardy * if rsc_offset set. 1244d7c51706SLoic Pallardy * 1245d7c51706SLoic Pallardy * Return: 0 on success 1246d7c51706SLoic Pallardy */ 1247d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc) 1248d7c51706SLoic Pallardy { 1249d7c51706SLoic Pallardy struct rproc_mem_entry *entry, *tmp; 1250d7c51706SLoic Pallardy struct fw_rsc_carveout *rsc; 1251d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 1252b36de8cfSLoic Pallardy u64 pa; 1253d7c51706SLoic Pallardy int ret; 1254d7c51706SLoic Pallardy 1255d7c51706SLoic Pallardy list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1256d7c51706SLoic Pallardy if (entry->alloc) { 1257d7c51706SLoic Pallardy ret = entry->alloc(rproc, entry); 1258d7c51706SLoic Pallardy if (ret) { 1259d7c51706SLoic Pallardy dev_err(dev, "Unable to allocate carveout %s: %d\n", 1260d7c51706SLoic Pallardy entry->name, ret); 1261d7c51706SLoic Pallardy return -ENOMEM; 1262d7c51706SLoic Pallardy } 1263d7c51706SLoic Pallardy } 1264d7c51706SLoic Pallardy 1265d7c51706SLoic Pallardy if (entry->rsc_offset != FW_RSC_ADDR_ANY) { 1266d7c51706SLoic Pallardy /* update resource table */ 1267d7c51706SLoic Pallardy rsc = (void *)rproc->table_ptr + entry->rsc_offset; 1268d7c51706SLoic Pallardy 1269d7c51706SLoic Pallardy /* 1270d7c51706SLoic Pallardy * Some remote processors might need to know the pa 1271d7c51706SLoic Pallardy * even though they are behind an IOMMU. E.g., OMAP4's 1272d7c51706SLoic Pallardy * remote M3 processor needs this so it can control 1273d7c51706SLoic Pallardy * on-chip hardware accelerators that are not behind 1274d7c51706SLoic Pallardy * the IOMMU, and therefor must know the pa. 1275d7c51706SLoic Pallardy * 1276d7c51706SLoic Pallardy * Generally we don't want to expose physical addresses 1277d7c51706SLoic Pallardy * if we don't have to (remote processors are generally 1278d7c51706SLoic Pallardy * _not_ trusted), so we might want to do this only for 1279d7c51706SLoic Pallardy * remote processor that _must_ have this (e.g. OMAP4's 1280d7c51706SLoic Pallardy * dual M3 subsystem). 1281d7c51706SLoic Pallardy * 1282d7c51706SLoic Pallardy * Non-IOMMU processors might also want to have this info. 1283d7c51706SLoic Pallardy * In this case, the device address and the physical address 1284d7c51706SLoic Pallardy * are the same. 1285d7c51706SLoic Pallardy */ 1286ffa5f9c8SLoic Pallardy 1287ffa5f9c8SLoic Pallardy /* Use va if defined else dma to generate pa */ 1288d7c51706SLoic Pallardy if (entry->va) 1289b36de8cfSLoic Pallardy pa = (u64)rproc_va_to_pa(entry->va); 1290ffa5f9c8SLoic Pallardy else 1291b36de8cfSLoic Pallardy pa = (u64)entry->dma; 1292ffa5f9c8SLoic Pallardy 1293b36de8cfSLoic Pallardy if (((u64)pa) & HIGH_BITS_MASK) 1294b36de8cfSLoic Pallardy dev_warn(dev, 1295b36de8cfSLoic Pallardy "Physical address cast in 32bit to fit resource table format\n"); 1296b36de8cfSLoic Pallardy 1297b36de8cfSLoic Pallardy rsc->pa = (u32)pa; 1298ffa5f9c8SLoic Pallardy rsc->da = entry->da; 1299ffa5f9c8SLoic Pallardy rsc->len = entry->len; 1300d7c51706SLoic Pallardy } 1301d7c51706SLoic Pallardy } 1302d7c51706SLoic Pallardy 1303d7c51706SLoic Pallardy return 0; 1304d7c51706SLoic Pallardy } 1305d7c51706SLoic Pallardy 13062666ca91SSarangdhar Joshi 13072666ca91SSarangdhar Joshi /** 1308400e64dfSOhad Ben-Cohen * rproc_resource_cleanup() - clean up and free all acquired resources 1309400e64dfSOhad Ben-Cohen * @rproc: rproc handle 1310400e64dfSOhad Ben-Cohen * 1311400e64dfSOhad Ben-Cohen * This function will free all resources acquired for @rproc, and it 13127a186941SOhad Ben-Cohen * is called whenever @rproc either shuts down or fails to boot. 1313400e64dfSOhad Ben-Cohen */ 1314d9473cbfSMathieu Poirier void rproc_resource_cleanup(struct rproc *rproc) 1315400e64dfSOhad Ben-Cohen { 1316400e64dfSOhad Ben-Cohen struct rproc_mem_entry *entry, *tmp; 1317a987e6b9SLoic Pallardy struct rproc_debug_trace *trace, *ttmp; 1318d81fb32fSBjorn Andersson struct rproc_vdev *rvdev, *rvtmp; 1319b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1320400e64dfSOhad Ben-Cohen 1321400e64dfSOhad Ben-Cohen /* clean up debugfs trace entries */ 1322a987e6b9SLoic Pallardy list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) { 1323a987e6b9SLoic Pallardy rproc_remove_trace_file(trace->tfile); 1324400e64dfSOhad Ben-Cohen rproc->num_traces--; 1325a987e6b9SLoic Pallardy list_del(&trace->node); 1326a987e6b9SLoic Pallardy kfree(trace); 1327400e64dfSOhad Ben-Cohen } 1328400e64dfSOhad Ben-Cohen 1329400e64dfSOhad Ben-Cohen /* clean up iommu mapping entries */ 1330400e64dfSOhad Ben-Cohen list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { 1331400e64dfSOhad Ben-Cohen size_t unmapped; 1332400e64dfSOhad Ben-Cohen 1333400e64dfSOhad Ben-Cohen unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); 1334400e64dfSOhad Ben-Cohen if (unmapped != entry->len) { 1335400e64dfSOhad Ben-Cohen /* nothing much to do besides complaining */ 1336096ee786SClement Leger dev_err(dev, "failed to unmap %zx/%zu\n", entry->len, 1337400e64dfSOhad Ben-Cohen unmapped); 1338400e64dfSOhad Ben-Cohen } 1339400e64dfSOhad Ben-Cohen 1340400e64dfSOhad Ben-Cohen list_del(&entry->node); 1341400e64dfSOhad Ben-Cohen kfree(entry); 1342400e64dfSOhad Ben-Cohen } 1343b6356a01SSuman Anna 1344b6356a01SSuman Anna /* clean up carveout allocations */ 1345b6356a01SSuman Anna list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1346f2e74abfSLoic Pallardy if (entry->release) 1347f2e74abfSLoic Pallardy entry->release(rproc, entry); 1348b6356a01SSuman Anna list_del(&entry->node); 1349b6356a01SSuman Anna kfree(entry); 1350b6356a01SSuman Anna } 1351d81fb32fSBjorn Andersson 1352d81fb32fSBjorn Andersson /* clean up remote vdev entries */ 1353f5bcb353SBjorn Andersson list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) 13542b45cef5SBjorn Andersson kref_put(&rvdev->refcount, rproc_vdev_release); 13552666ca91SSarangdhar Joshi 13562666ca91SSarangdhar Joshi rproc_coredump_cleanup(rproc); 13572b45cef5SBjorn Andersson } 1358d9473cbfSMathieu Poirier EXPORT_SYMBOL(rproc_resource_cleanup); 1359400e64dfSOhad Ben-Cohen 13601efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw) 13611efa30d0SSarangdhar Joshi { 1362a4b24c75SBjorn Andersson struct resource_table *loaded_table; 13631efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 1364a4b24c75SBjorn Andersson int ret; 13651efa30d0SSarangdhar Joshi 13661efa30d0SSarangdhar Joshi /* load the ELF segments to memory */ 13671efa30d0SSarangdhar Joshi ret = rproc_load_segments(rproc, fw); 13681efa30d0SSarangdhar Joshi if (ret) { 13691efa30d0SSarangdhar Joshi dev_err(dev, "Failed to load program segments: %d\n", ret); 13701efa30d0SSarangdhar Joshi return ret; 13711efa30d0SSarangdhar Joshi } 13721efa30d0SSarangdhar Joshi 13731efa30d0SSarangdhar Joshi /* 13741efa30d0SSarangdhar Joshi * The starting device has been given the rproc->cached_table as the 13751efa30d0SSarangdhar Joshi * resource table. The address of the vring along with the other 13761efa30d0SSarangdhar Joshi * allocated resources (carveouts etc) is stored in cached_table. 13771efa30d0SSarangdhar Joshi * In order to pass this information to the remote device we must copy 13781efa30d0SSarangdhar Joshi * this information to device memory. We also update the table_ptr so 13791efa30d0SSarangdhar Joshi * that any subsequent changes will be applied to the loaded version. 13801efa30d0SSarangdhar Joshi */ 13811efa30d0SSarangdhar Joshi loaded_table = rproc_find_loaded_rsc_table(rproc, fw); 13821efa30d0SSarangdhar Joshi if (loaded_table) { 1383a4b24c75SBjorn Andersson memcpy(loaded_table, rproc->cached_table, rproc->table_sz); 13841efa30d0SSarangdhar Joshi rproc->table_ptr = loaded_table; 13851efa30d0SSarangdhar Joshi } 13861efa30d0SSarangdhar Joshi 1387c455daa4SBjorn Andersson ret = rproc_prepare_subdevices(rproc); 1388c455daa4SBjorn Andersson if (ret) { 1389c455daa4SBjorn Andersson dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1390c455daa4SBjorn Andersson rproc->name, ret); 1391f68d51bdSSuman Anna goto reset_table_ptr; 1392c455daa4SBjorn Andersson } 1393c455daa4SBjorn Andersson 13941efa30d0SSarangdhar Joshi /* power up the remote processor */ 13951efa30d0SSarangdhar Joshi ret = rproc->ops->start(rproc); 13961efa30d0SSarangdhar Joshi if (ret) { 13971efa30d0SSarangdhar Joshi dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); 1398c455daa4SBjorn Andersson goto unprepare_subdevices; 13991efa30d0SSarangdhar Joshi } 14001efa30d0SSarangdhar Joshi 1401618fcff3SBjorn Andersson /* Start any subdevices for the remote processor */ 1402618fcff3SBjorn Andersson ret = rproc_start_subdevices(rproc); 14031efa30d0SSarangdhar Joshi if (ret) { 14041efa30d0SSarangdhar Joshi dev_err(dev, "failed to probe subdevices for %s: %d\n", 14051efa30d0SSarangdhar Joshi rproc->name, ret); 1406c455daa4SBjorn Andersson goto stop_rproc; 14071efa30d0SSarangdhar Joshi } 14081efa30d0SSarangdhar Joshi 14091efa30d0SSarangdhar Joshi rproc->state = RPROC_RUNNING; 14101efa30d0SSarangdhar Joshi 14111efa30d0SSarangdhar Joshi dev_info(dev, "remote processor %s is now up\n", rproc->name); 14121efa30d0SSarangdhar Joshi 14131efa30d0SSarangdhar Joshi return 0; 1414c455daa4SBjorn Andersson 1415c455daa4SBjorn Andersson stop_rproc: 1416c455daa4SBjorn Andersson rproc->ops->stop(rproc); 1417c455daa4SBjorn Andersson unprepare_subdevices: 1418c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1419f68d51bdSSuman Anna reset_table_ptr: 1420f68d51bdSSuman Anna rproc->table_ptr = rproc->cached_table; 1421c455daa4SBjorn Andersson 1422c455daa4SBjorn Andersson return ret; 14231efa30d0SSarangdhar Joshi } 14241efa30d0SSarangdhar Joshi 14256a6c4dc0SMathieu Poirier static int __rproc_attach(struct rproc *rproc) 1426d848a481SMathieu Poirier { 1427d848a481SMathieu Poirier struct device *dev = &rproc->dev; 1428d848a481SMathieu Poirier int ret; 1429d848a481SMathieu Poirier 1430d848a481SMathieu Poirier ret = rproc_prepare_subdevices(rproc); 1431d848a481SMathieu Poirier if (ret) { 1432d848a481SMathieu Poirier dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1433d848a481SMathieu Poirier rproc->name, ret); 1434d848a481SMathieu Poirier goto out; 1435d848a481SMathieu Poirier } 1436d848a481SMathieu Poirier 1437d848a481SMathieu Poirier /* Attach to the remote processor */ 1438d848a481SMathieu Poirier ret = rproc_attach_device(rproc); 1439d848a481SMathieu Poirier if (ret) { 1440d848a481SMathieu Poirier dev_err(dev, "can't attach to rproc %s: %d\n", 1441d848a481SMathieu Poirier rproc->name, ret); 1442d848a481SMathieu Poirier goto unprepare_subdevices; 1443d848a481SMathieu Poirier } 1444d848a481SMathieu Poirier 1445d848a481SMathieu Poirier /* Start any subdevices for the remote processor */ 1446d848a481SMathieu Poirier ret = rproc_start_subdevices(rproc); 1447d848a481SMathieu Poirier if (ret) { 1448d848a481SMathieu Poirier dev_err(dev, "failed to probe subdevices for %s: %d\n", 1449d848a481SMathieu Poirier rproc->name, ret); 1450d848a481SMathieu Poirier goto stop_rproc; 1451d848a481SMathieu Poirier } 1452d848a481SMathieu Poirier 145376f4c875SMathieu Poirier rproc->state = RPROC_ATTACHED; 1454d848a481SMathieu Poirier 1455d848a481SMathieu Poirier dev_info(dev, "remote processor %s is now attached\n", rproc->name); 1456d848a481SMathieu Poirier 1457d848a481SMathieu Poirier return 0; 1458d848a481SMathieu Poirier 1459d848a481SMathieu Poirier stop_rproc: 1460d848a481SMathieu Poirier rproc->ops->stop(rproc); 1461d848a481SMathieu Poirier unprepare_subdevices: 1462d848a481SMathieu Poirier rproc_unprepare_subdevices(rproc); 1463d848a481SMathieu Poirier out: 1464d848a481SMathieu Poirier return ret; 1465d848a481SMathieu Poirier } 1466d848a481SMathieu Poirier 1467400e64dfSOhad Ben-Cohen /* 1468400e64dfSOhad Ben-Cohen * take a firmware and boot a remote processor with it. 1469400e64dfSOhad Ben-Cohen */ 1470400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) 1471400e64dfSOhad Ben-Cohen { 1472b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1473400e64dfSOhad Ben-Cohen const char *name = rproc->firmware; 147458b64090SBjorn Andersson int ret; 1475400e64dfSOhad Ben-Cohen 1476400e64dfSOhad Ben-Cohen ret = rproc_fw_sanity_check(rproc, fw); 1477400e64dfSOhad Ben-Cohen if (ret) 1478400e64dfSOhad Ben-Cohen return ret; 1479400e64dfSOhad Ben-Cohen 1480e981f6d4SSjur Brændeland dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); 1481400e64dfSOhad Ben-Cohen 1482400e64dfSOhad Ben-Cohen /* 1483400e64dfSOhad Ben-Cohen * if enabling an IOMMU isn't relevant for this rproc, this is 1484400e64dfSOhad Ben-Cohen * just a nop 1485400e64dfSOhad Ben-Cohen */ 1486400e64dfSOhad Ben-Cohen ret = rproc_enable_iommu(rproc); 1487400e64dfSOhad Ben-Cohen if (ret) { 1488400e64dfSOhad Ben-Cohen dev_err(dev, "can't enable iommu: %d\n", ret); 148949cff125SMathieu Poirier return ret; 1490400e64dfSOhad Ben-Cohen } 1491400e64dfSOhad Ben-Cohen 149233467ac3SLoic Pallardy /* Prepare rproc for firmware loading if needed */ 149333467ac3SLoic Pallardy ret = rproc_prepare_device(rproc); 149433467ac3SLoic Pallardy if (ret) { 149533467ac3SLoic Pallardy dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); 149633467ac3SLoic Pallardy goto disable_iommu; 149733467ac3SLoic Pallardy } 149833467ac3SLoic Pallardy 14993e5f9eb5SSjur Brændeland rproc->bootaddr = rproc_get_boot_addr(rproc, fw); 1500400e64dfSOhad Ben-Cohen 1501c1d35c1aSBjorn Andersson /* Load resource table, core dump segment list etc from the firmware */ 1502c1d35c1aSBjorn Andersson ret = rproc_parse_fw(rproc, fw); 150358b64090SBjorn Andersson if (ret) 150433467ac3SLoic Pallardy goto unprepare_rproc; 1505a0c10687SBjorn Andersson 1506b35d7afcSBjorn Andersson /* reset max_notifyid */ 1507b35d7afcSBjorn Andersson rproc->max_notifyid = -1; 1508b35d7afcSBjorn Andersson 1509c6aed238SLoic Pallardy /* reset handled vdev */ 1510c6aed238SLoic Pallardy rproc->nb_vdev = 0; 1511c6aed238SLoic Pallardy 1512400e64dfSOhad Ben-Cohen /* handle fw resources which are required to boot rproc */ 1513a4b24c75SBjorn Andersson ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1514400e64dfSOhad Ben-Cohen if (ret) { 1515400e64dfSOhad Ben-Cohen dev_err(dev, "Failed to process resources: %d\n", ret); 1516229b85a6SBjorn Andersson goto clean_up_resources; 1517400e64dfSOhad Ben-Cohen } 1518400e64dfSOhad Ben-Cohen 1519d7c51706SLoic Pallardy /* Allocate carveout resources associated to rproc */ 1520d7c51706SLoic Pallardy ret = rproc_alloc_registered_carveouts(rproc); 1521d7c51706SLoic Pallardy if (ret) { 1522d7c51706SLoic Pallardy dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1523d7c51706SLoic Pallardy ret); 1524d7c51706SLoic Pallardy goto clean_up_resources; 1525d7c51706SLoic Pallardy } 1526d7c51706SLoic Pallardy 15271efa30d0SSarangdhar Joshi ret = rproc_start(rproc, fw); 15281efa30d0SSarangdhar Joshi if (ret) 1529229b85a6SBjorn Andersson goto clean_up_resources; 1530400e64dfSOhad Ben-Cohen 1531400e64dfSOhad Ben-Cohen return 0; 1532400e64dfSOhad Ben-Cohen 1533229b85a6SBjorn Andersson clean_up_resources: 1534229b85a6SBjorn Andersson rproc_resource_cleanup(rproc); 1535a0c10687SBjorn Andersson kfree(rproc->cached_table); 1536a0c10687SBjorn Andersson rproc->cached_table = NULL; 1537988d204cSBjorn Andersson rproc->table_ptr = NULL; 153833467ac3SLoic Pallardy unprepare_rproc: 153933467ac3SLoic Pallardy /* release HW resources if needed */ 154033467ac3SLoic Pallardy rproc_unprepare_device(rproc); 154158b64090SBjorn Andersson disable_iommu: 1542400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1543400e64dfSOhad Ben-Cohen return ret; 1544400e64dfSOhad Ben-Cohen } 1545400e64dfSOhad Ben-Cohen 1546*1a631382SMathieu Poirier static int rproc_set_rsc_table(struct rproc *rproc) 1547*1a631382SMathieu Poirier { 1548*1a631382SMathieu Poirier struct resource_table *table_ptr; 1549*1a631382SMathieu Poirier struct device *dev = &rproc->dev; 1550*1a631382SMathieu Poirier size_t table_sz; 1551*1a631382SMathieu Poirier int ret; 1552*1a631382SMathieu Poirier 1553*1a631382SMathieu Poirier table_ptr = rproc_get_loaded_rsc_table(rproc, &table_sz); 1554*1a631382SMathieu Poirier if (!table_ptr) { 1555*1a631382SMathieu Poirier /* Not having a resource table is acceptable */ 1556*1a631382SMathieu Poirier return 0; 1557*1a631382SMathieu Poirier } 1558*1a631382SMathieu Poirier 1559*1a631382SMathieu Poirier if (IS_ERR(table_ptr)) { 1560*1a631382SMathieu Poirier ret = PTR_ERR(table_ptr); 1561*1a631382SMathieu Poirier dev_err(dev, "can't load resource table: %d\n", ret); 1562*1a631382SMathieu Poirier return ret; 1563*1a631382SMathieu Poirier } 1564*1a631382SMathieu Poirier 1565*1a631382SMathieu Poirier rproc->cached_table = NULL; 1566*1a631382SMathieu Poirier rproc->table_ptr = table_ptr; 1567*1a631382SMathieu Poirier rproc->table_sz = table_sz; 1568*1a631382SMathieu Poirier 1569*1a631382SMathieu Poirier return 0; 1570*1a631382SMathieu Poirier } 1571*1a631382SMathieu Poirier 1572400e64dfSOhad Ben-Cohen /* 1573fdf0e00eSMathieu Poirier * Attach to remote processor - similar to rproc_fw_boot() but without 1574fdf0e00eSMathieu Poirier * the steps that deal with the firmware image. 1575fdf0e00eSMathieu Poirier */ 15766a6c4dc0SMathieu Poirier static int rproc_attach(struct rproc *rproc) 1577fdf0e00eSMathieu Poirier { 1578fdf0e00eSMathieu Poirier struct device *dev = &rproc->dev; 1579fdf0e00eSMathieu Poirier int ret; 1580fdf0e00eSMathieu Poirier 1581fdf0e00eSMathieu Poirier /* 1582fdf0e00eSMathieu Poirier * if enabling an IOMMU isn't relevant for this rproc, this is 1583fdf0e00eSMathieu Poirier * just a nop 1584fdf0e00eSMathieu Poirier */ 1585fdf0e00eSMathieu Poirier ret = rproc_enable_iommu(rproc); 1586fdf0e00eSMathieu Poirier if (ret) { 1587fdf0e00eSMathieu Poirier dev_err(dev, "can't enable iommu: %d\n", ret); 1588fdf0e00eSMathieu Poirier return ret; 1589fdf0e00eSMathieu Poirier } 1590fdf0e00eSMathieu Poirier 1591*1a631382SMathieu Poirier ret = rproc_set_rsc_table(rproc); 1592*1a631382SMathieu Poirier if (ret) { 1593*1a631382SMathieu Poirier dev_err(dev, "can't load resource table: %d\n", ret); 1594*1a631382SMathieu Poirier goto disable_iommu; 1595*1a631382SMathieu Poirier } 1596*1a631382SMathieu Poirier 1597fdf0e00eSMathieu Poirier /* reset max_notifyid */ 1598fdf0e00eSMathieu Poirier rproc->max_notifyid = -1; 1599fdf0e00eSMathieu Poirier 1600fdf0e00eSMathieu Poirier /* reset handled vdev */ 1601fdf0e00eSMathieu Poirier rproc->nb_vdev = 0; 1602fdf0e00eSMathieu Poirier 1603fdf0e00eSMathieu Poirier /* 1604fdf0e00eSMathieu Poirier * Handle firmware resources required to attach to a remote processor. 1605fdf0e00eSMathieu Poirier * Because we are attaching rather than booting the remote processor, 1606fdf0e00eSMathieu Poirier * we expect the platform driver to properly set rproc->table_ptr. 1607fdf0e00eSMathieu Poirier */ 1608fdf0e00eSMathieu Poirier ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1609fdf0e00eSMathieu Poirier if (ret) { 1610fdf0e00eSMathieu Poirier dev_err(dev, "Failed to process resources: %d\n", ret); 1611fdf0e00eSMathieu Poirier goto disable_iommu; 1612fdf0e00eSMathieu Poirier } 1613fdf0e00eSMathieu Poirier 1614fdf0e00eSMathieu Poirier /* Allocate carveout resources associated to rproc */ 1615fdf0e00eSMathieu Poirier ret = rproc_alloc_registered_carveouts(rproc); 1616fdf0e00eSMathieu Poirier if (ret) { 1617fdf0e00eSMathieu Poirier dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1618fdf0e00eSMathieu Poirier ret); 1619fdf0e00eSMathieu Poirier goto clean_up_resources; 1620fdf0e00eSMathieu Poirier } 1621fdf0e00eSMathieu Poirier 16226a6c4dc0SMathieu Poirier ret = __rproc_attach(rproc); 1623fdf0e00eSMathieu Poirier if (ret) 1624fdf0e00eSMathieu Poirier goto clean_up_resources; 1625fdf0e00eSMathieu Poirier 1626fdf0e00eSMathieu Poirier return 0; 1627fdf0e00eSMathieu Poirier 1628fdf0e00eSMathieu Poirier clean_up_resources: 1629fdf0e00eSMathieu Poirier rproc_resource_cleanup(rproc); 1630fdf0e00eSMathieu Poirier disable_iommu: 1631fdf0e00eSMathieu Poirier rproc_disable_iommu(rproc); 1632fdf0e00eSMathieu Poirier return ret; 1633fdf0e00eSMathieu Poirier } 1634fdf0e00eSMathieu Poirier 1635fdf0e00eSMathieu Poirier /* 16365e6533f7SSarangdhar Joshi * take a firmware and boot it up. 1637400e64dfSOhad Ben-Cohen * 1638400e64dfSOhad Ben-Cohen * Note: this function is called asynchronously upon registration of the 1639400e64dfSOhad Ben-Cohen * remote processor (so we must wait until it completes before we try 1640400e64dfSOhad Ben-Cohen * to unregister the device. one other option is just to use kref here, 1641400e64dfSOhad Ben-Cohen * that might be cleaner). 1642400e64dfSOhad Ben-Cohen */ 16435e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context) 1644400e64dfSOhad Ben-Cohen { 1645400e64dfSOhad Ben-Cohen struct rproc *rproc = context; 1646a2b950acSOhad Ben-Cohen 16472bfc311aSBjorn Andersson rproc_boot(rproc); 1648ddf71187SBjorn Andersson 1649400e64dfSOhad Ben-Cohen release_firmware(fw); 1650400e64dfSOhad Ben-Cohen } 1651400e64dfSOhad Ben-Cohen 16525e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc) 165370b85ef8SFernando Guzman Lugo { 165470b85ef8SFernando Guzman Lugo int ret; 165570b85ef8SFernando Guzman Lugo 165670b85ef8SFernando Guzman Lugo /* 1657e3d21939SMathieu Poirier * Since the remote processor is in a detached state, it has already 1658e3d21939SMathieu Poirier * been booted by another entity. As such there is no point in waiting 1659e3d21939SMathieu Poirier * for a firmware image to be loaded, we can simply initiate the process 1660e3d21939SMathieu Poirier * of attaching to it immediately. 1661e3d21939SMathieu Poirier */ 1662e3d21939SMathieu Poirier if (rproc->state == RPROC_DETACHED) 1663e3d21939SMathieu Poirier return rproc_boot(rproc); 1664e3d21939SMathieu Poirier 1665e3d21939SMathieu Poirier /* 166670b85ef8SFernando Guzman Lugo * We're initiating an asynchronous firmware loading, so we can 166770b85ef8SFernando Guzman Lugo * be built-in kernel code, without hanging the boot process. 166870b85ef8SFernando Guzman Lugo */ 166970b85ef8SFernando Guzman Lugo ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, 167070b85ef8SFernando Guzman Lugo rproc->firmware, &rproc->dev, GFP_KERNEL, 16715e6533f7SSarangdhar Joshi rproc, rproc_auto_boot_callback); 16722099c77dSSarangdhar Joshi if (ret < 0) 167370b85ef8SFernando Guzman Lugo dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); 167470b85ef8SFernando Guzman Lugo 167570b85ef8SFernando Guzman Lugo return ret; 167670b85ef8SFernando Guzman Lugo } 167770b85ef8SFernando Guzman Lugo 1678880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed) 16791efa30d0SSarangdhar Joshi { 16801efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 16811efa30d0SSarangdhar Joshi int ret; 16821efa30d0SSarangdhar Joshi 1683618fcff3SBjorn Andersson /* Stop any subdevices for the remote processor */ 1684618fcff3SBjorn Andersson rproc_stop_subdevices(rproc, crashed); 16851efa30d0SSarangdhar Joshi 16860a8b81cbSBjorn Andersson /* the installed resource table is no longer accessible */ 16870a8b81cbSBjorn Andersson rproc->table_ptr = rproc->cached_table; 16880a8b81cbSBjorn Andersson 16891efa30d0SSarangdhar Joshi /* power off the remote processor */ 16901efa30d0SSarangdhar Joshi ret = rproc->ops->stop(rproc); 16911efa30d0SSarangdhar Joshi if (ret) { 16921efa30d0SSarangdhar Joshi dev_err(dev, "can't stop rproc: %d\n", ret); 16931efa30d0SSarangdhar Joshi return ret; 16941efa30d0SSarangdhar Joshi } 16951efa30d0SSarangdhar Joshi 1696c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1697c455daa4SBjorn Andersson 16981efa30d0SSarangdhar Joshi rproc->state = RPROC_OFFLINE; 16991efa30d0SSarangdhar Joshi 17001efa30d0SSarangdhar Joshi dev_info(dev, "stopped remote processor %s\n", rproc->name); 17011efa30d0SSarangdhar Joshi 17021efa30d0SSarangdhar Joshi return 0; 17031efa30d0SSarangdhar Joshi } 17041efa30d0SSarangdhar Joshi 17052666ca91SSarangdhar Joshi 17062666ca91SSarangdhar Joshi /** 170770b85ef8SFernando Guzman Lugo * rproc_trigger_recovery() - recover a remoteproc 170870b85ef8SFernando Guzman Lugo * @rproc: the remote processor 170970b85ef8SFernando Guzman Lugo * 171056324d7aSAnna, Suman * The recovery is done by resetting all the virtio devices, that way all the 171170b85ef8SFernando Guzman Lugo * rpmsg drivers will be reseted along with the remote processor making the 171270b85ef8SFernando Guzman Lugo * remoteproc functional again. 171370b85ef8SFernando Guzman Lugo * 171470b85ef8SFernando Guzman Lugo * This function can sleep, so it cannot be called from atomic context. 171570b85ef8SFernando Guzman Lugo */ 171670b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc) 171770b85ef8SFernando Guzman Lugo { 17187e83cab8SSarangdhar Joshi const struct firmware *firmware_p; 17197e83cab8SSarangdhar Joshi struct device *dev = &rproc->dev; 17207e83cab8SSarangdhar Joshi int ret; 17217e83cab8SSarangdhar Joshi 17227e83cab8SSarangdhar Joshi ret = mutex_lock_interruptible(&rproc->lock); 17237e83cab8SSarangdhar Joshi if (ret) 17247e83cab8SSarangdhar Joshi return ret; 17257e83cab8SSarangdhar Joshi 17260b145574SAlex Elder /* State could have changed before we got the mutex */ 17270b145574SAlex Elder if (rproc->state != RPROC_CRASHED) 17280b145574SAlex Elder goto unlock_mutex; 17290b145574SAlex Elder 17300b145574SAlex Elder dev_err(dev, "recovering %s\n", rproc->name); 17310b145574SAlex Elder 1732fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, true); 17337e83cab8SSarangdhar Joshi if (ret) 17347e83cab8SSarangdhar Joshi goto unlock_mutex; 1735ddf71187SBjorn Andersson 17362666ca91SSarangdhar Joshi /* generate coredump */ 1737adf60a87SSiddharth Gupta rproc->ops->coredump(rproc); 17382666ca91SSarangdhar Joshi 17397e83cab8SSarangdhar Joshi /* load firmware */ 17407e83cab8SSarangdhar Joshi ret = request_firmware(&firmware_p, rproc->firmware, dev); 17417e83cab8SSarangdhar Joshi if (ret < 0) { 17427e83cab8SSarangdhar Joshi dev_err(dev, "request_firmware failed: %d\n", ret); 17437e83cab8SSarangdhar Joshi goto unlock_mutex; 17447e83cab8SSarangdhar Joshi } 1745ddf71187SBjorn Andersson 17467e83cab8SSarangdhar Joshi /* boot the remote processor up again */ 17477e83cab8SSarangdhar Joshi ret = rproc_start(rproc, firmware_p); 17487e83cab8SSarangdhar Joshi 17497e83cab8SSarangdhar Joshi release_firmware(firmware_p); 17507e83cab8SSarangdhar Joshi 17517e83cab8SSarangdhar Joshi unlock_mutex: 17527e83cab8SSarangdhar Joshi mutex_unlock(&rproc->lock); 17537e83cab8SSarangdhar Joshi return ret; 175470b85ef8SFernando Guzman Lugo } 175570b85ef8SFernando Guzman Lugo 1756400e64dfSOhad Ben-Cohen /** 17578afd519cSFernando Guzman Lugo * rproc_crash_handler_work() - handle a crash 17582e7d4c2cSArnaud Pouliquen * @work: work treating the crash 17598afd519cSFernando Guzman Lugo * 17608afd519cSFernando Guzman Lugo * This function needs to handle everything related to a crash, like cpu 17618afd519cSFernando Guzman Lugo * registers and stack dump, information to help to debug the fatal error, etc. 17628afd519cSFernando Guzman Lugo */ 17638afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work) 17648afd519cSFernando Guzman Lugo { 17658afd519cSFernando Guzman Lugo struct rproc *rproc = container_of(work, struct rproc, crash_handler); 17668afd519cSFernando Guzman Lugo struct device *dev = &rproc->dev; 17678afd519cSFernando Guzman Lugo 17688afd519cSFernando Guzman Lugo dev_dbg(dev, "enter %s\n", __func__); 17698afd519cSFernando Guzman Lugo 17708afd519cSFernando Guzman Lugo mutex_lock(&rproc->lock); 17718afd519cSFernando Guzman Lugo 17728afd519cSFernando Guzman Lugo if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) { 17738afd519cSFernando Guzman Lugo /* handle only the first crash detected */ 17748afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 17758afd519cSFernando Guzman Lugo return; 17768afd519cSFernando Guzman Lugo } 17778afd519cSFernando Guzman Lugo 17788afd519cSFernando Guzman Lugo rproc->state = RPROC_CRASHED; 17798afd519cSFernando Guzman Lugo dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, 17808afd519cSFernando Guzman Lugo rproc->name); 17818afd519cSFernando Guzman Lugo 17828afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 17838afd519cSFernando Guzman Lugo 17842e37abb8SFernando Guzman Lugo if (!rproc->recovery_disabled) 178570b85ef8SFernando Guzman Lugo rproc_trigger_recovery(rproc); 1786a781e5aaSRishabh Bhatnagar 1787a781e5aaSRishabh Bhatnagar pm_relax(rproc->dev.parent); 17888afd519cSFernando Guzman Lugo } 17898afd519cSFernando Guzman Lugo 17908afd519cSFernando Guzman Lugo /** 17911b0ef906SSuman Anna * rproc_boot() - boot a remote processor 1792400e64dfSOhad Ben-Cohen * @rproc: handle of a remote processor 1793400e64dfSOhad Ben-Cohen * 1794400e64dfSOhad Ben-Cohen * Boot a remote processor (i.e. load its firmware, power it on, ...). 1795400e64dfSOhad Ben-Cohen * 1796400e64dfSOhad Ben-Cohen * If the remote processor is already powered on, this function immediately 1797400e64dfSOhad Ben-Cohen * returns (successfully). 1798400e64dfSOhad Ben-Cohen * 1799400e64dfSOhad Ben-Cohen * Returns 0 on success, and an appropriate error value otherwise. 1800400e64dfSOhad Ben-Cohen */ 18011b0ef906SSuman Anna int rproc_boot(struct rproc *rproc) 1802400e64dfSOhad Ben-Cohen { 1803400e64dfSOhad Ben-Cohen const struct firmware *firmware_p; 1804400e64dfSOhad Ben-Cohen struct device *dev; 1805400e64dfSOhad Ben-Cohen int ret; 1806400e64dfSOhad Ben-Cohen 1807400e64dfSOhad Ben-Cohen if (!rproc) { 1808400e64dfSOhad Ben-Cohen pr_err("invalid rproc handle\n"); 1809400e64dfSOhad Ben-Cohen return -EINVAL; 1810400e64dfSOhad Ben-Cohen } 1811400e64dfSOhad Ben-Cohen 1812b5ab5e24SOhad Ben-Cohen dev = &rproc->dev; 1813400e64dfSOhad Ben-Cohen 1814400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1815400e64dfSOhad Ben-Cohen if (ret) { 1816400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1817400e64dfSOhad Ben-Cohen return ret; 1818400e64dfSOhad Ben-Cohen } 1819400e64dfSOhad Ben-Cohen 18202099c77dSSarangdhar Joshi if (rproc->state == RPROC_DELETED) { 18212099c77dSSarangdhar Joshi ret = -ENODEV; 18222099c77dSSarangdhar Joshi dev_err(dev, "can't boot deleted rproc %s\n", rproc->name); 18232099c77dSSarangdhar Joshi goto unlock_mutex; 18242099c77dSSarangdhar Joshi } 18252099c77dSSarangdhar Joshi 18260f9dc562SMathieu Poirier /* skip the boot or attach process if rproc is already powered up */ 1827400e64dfSOhad Ben-Cohen if (atomic_inc_return(&rproc->power) > 1) { 1828400e64dfSOhad Ben-Cohen ret = 0; 1829400e64dfSOhad Ben-Cohen goto unlock_mutex; 1830400e64dfSOhad Ben-Cohen } 1831400e64dfSOhad Ben-Cohen 18320f9dc562SMathieu Poirier if (rproc->state == RPROC_DETACHED) { 18330f9dc562SMathieu Poirier dev_info(dev, "attaching to %s\n", rproc->name); 18340f9dc562SMathieu Poirier 18356a6c4dc0SMathieu Poirier ret = rproc_attach(rproc); 18360f9dc562SMathieu Poirier } else { 1837400e64dfSOhad Ben-Cohen dev_info(dev, "powering up %s\n", rproc->name); 1838400e64dfSOhad Ben-Cohen 1839400e64dfSOhad Ben-Cohen /* load firmware */ 1840400e64dfSOhad Ben-Cohen ret = request_firmware(&firmware_p, rproc->firmware, dev); 1841400e64dfSOhad Ben-Cohen if (ret < 0) { 1842400e64dfSOhad Ben-Cohen dev_err(dev, "request_firmware failed: %d\n", ret); 1843400e64dfSOhad Ben-Cohen goto downref_rproc; 1844400e64dfSOhad Ben-Cohen } 1845400e64dfSOhad Ben-Cohen 1846400e64dfSOhad Ben-Cohen ret = rproc_fw_boot(rproc, firmware_p); 1847400e64dfSOhad Ben-Cohen 1848400e64dfSOhad Ben-Cohen release_firmware(firmware_p); 18490f9dc562SMathieu Poirier } 1850400e64dfSOhad Ben-Cohen 1851400e64dfSOhad Ben-Cohen downref_rproc: 1852fbb6aacbSBjorn Andersson if (ret) 1853400e64dfSOhad Ben-Cohen atomic_dec(&rproc->power); 1854400e64dfSOhad Ben-Cohen unlock_mutex: 1855400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1856400e64dfSOhad Ben-Cohen return ret; 1857400e64dfSOhad Ben-Cohen } 1858400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot); 1859400e64dfSOhad Ben-Cohen 1860400e64dfSOhad Ben-Cohen /** 1861400e64dfSOhad Ben-Cohen * rproc_shutdown() - power off the remote processor 1862400e64dfSOhad Ben-Cohen * @rproc: the remote processor 1863400e64dfSOhad Ben-Cohen * 1864400e64dfSOhad Ben-Cohen * Power off a remote processor (previously booted with rproc_boot()). 1865400e64dfSOhad Ben-Cohen * 1866400e64dfSOhad Ben-Cohen * In case @rproc is still being used by an additional user(s), then 1867400e64dfSOhad Ben-Cohen * this function will just decrement the power refcount and exit, 1868400e64dfSOhad Ben-Cohen * without really powering off the device. 1869400e64dfSOhad Ben-Cohen * 1870400e64dfSOhad Ben-Cohen * Every call to rproc_boot() must (eventually) be accompanied by a call 1871400e64dfSOhad Ben-Cohen * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. 1872400e64dfSOhad Ben-Cohen * 1873400e64dfSOhad Ben-Cohen * Notes: 1874400e64dfSOhad Ben-Cohen * - we're not decrementing the rproc's refcount, only the power refcount. 1875400e64dfSOhad Ben-Cohen * which means that the @rproc handle stays valid even after rproc_shutdown() 1876400e64dfSOhad Ben-Cohen * returns, and users can still use it with a subsequent rproc_boot(), if 1877400e64dfSOhad Ben-Cohen * needed. 1878400e64dfSOhad Ben-Cohen */ 1879400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc) 1880400e64dfSOhad Ben-Cohen { 1881b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1882400e64dfSOhad Ben-Cohen int ret; 1883400e64dfSOhad Ben-Cohen 1884400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1885400e64dfSOhad Ben-Cohen if (ret) { 1886400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1887400e64dfSOhad Ben-Cohen return; 1888400e64dfSOhad Ben-Cohen } 1889400e64dfSOhad Ben-Cohen 1890400e64dfSOhad Ben-Cohen /* if the remote proc is still needed, bail out */ 1891400e64dfSOhad Ben-Cohen if (!atomic_dec_and_test(&rproc->power)) 1892400e64dfSOhad Ben-Cohen goto out; 1893400e64dfSOhad Ben-Cohen 1894fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, false); 1895400e64dfSOhad Ben-Cohen if (ret) { 1896400e64dfSOhad Ben-Cohen atomic_inc(&rproc->power); 1897400e64dfSOhad Ben-Cohen goto out; 1898400e64dfSOhad Ben-Cohen } 1899400e64dfSOhad Ben-Cohen 1900400e64dfSOhad Ben-Cohen /* clean up all acquired resources */ 1901400e64dfSOhad Ben-Cohen rproc_resource_cleanup(rproc); 1902400e64dfSOhad Ben-Cohen 190333467ac3SLoic Pallardy /* release HW resources if needed */ 190433467ac3SLoic Pallardy rproc_unprepare_device(rproc); 190533467ac3SLoic Pallardy 1906400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1907400e64dfSOhad Ben-Cohen 1908988d204cSBjorn Andersson /* Free the copy of the resource table */ 1909a0c10687SBjorn Andersson kfree(rproc->cached_table); 1910a0c10687SBjorn Andersson rproc->cached_table = NULL; 1911988d204cSBjorn Andersson rproc->table_ptr = NULL; 1912400e64dfSOhad Ben-Cohen out: 1913400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1914400e64dfSOhad Ben-Cohen } 1915400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown); 1916400e64dfSOhad Ben-Cohen 1917400e64dfSOhad Ben-Cohen /** 1918fec47d86SDave Gerlach * rproc_get_by_phandle() - find a remote processor by phandle 1919fec47d86SDave Gerlach * @phandle: phandle to the rproc 1920fec47d86SDave Gerlach * 1921fec47d86SDave Gerlach * Finds an rproc handle using the remote processor's phandle, and then 1922fec47d86SDave Gerlach * return a handle to the rproc. 1923fec47d86SDave Gerlach * 1924fec47d86SDave Gerlach * This function increments the remote processor's refcount, so always 1925fec47d86SDave Gerlach * use rproc_put() to decrement it back once rproc isn't needed anymore. 1926fec47d86SDave Gerlach * 1927fec47d86SDave Gerlach * Returns the rproc handle on success, and NULL on failure. 1928fec47d86SDave Gerlach */ 19298de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF 1930fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle) 1931fec47d86SDave Gerlach { 1932fec47d86SDave Gerlach struct rproc *rproc = NULL, *r; 1933fec47d86SDave Gerlach struct device_node *np; 1934fec47d86SDave Gerlach 1935fec47d86SDave Gerlach np = of_find_node_by_phandle(phandle); 1936fec47d86SDave Gerlach if (!np) 1937fec47d86SDave Gerlach return NULL; 1938fec47d86SDave Gerlach 1939c0abe2caSBjorn Andersson rcu_read_lock(); 1940c0abe2caSBjorn Andersson list_for_each_entry_rcu(r, &rproc_list, node) { 1941fec47d86SDave Gerlach if (r->dev.parent && r->dev.parent->of_node == np) { 1942fbb6aacbSBjorn Andersson /* prevent underlying implementation from being removed */ 1943fbb6aacbSBjorn Andersson if (!try_module_get(r->dev.parent->driver->owner)) { 1944fbb6aacbSBjorn Andersson dev_err(&r->dev, "can't get owner\n"); 1945fbb6aacbSBjorn Andersson break; 1946fbb6aacbSBjorn Andersson } 1947fbb6aacbSBjorn Andersson 1948fec47d86SDave Gerlach rproc = r; 1949fec47d86SDave Gerlach get_device(&rproc->dev); 1950fec47d86SDave Gerlach break; 1951fec47d86SDave Gerlach } 1952fec47d86SDave Gerlach } 1953c0abe2caSBjorn Andersson rcu_read_unlock(); 1954fec47d86SDave Gerlach 1955fec47d86SDave Gerlach of_node_put(np); 1956fec47d86SDave Gerlach 1957fec47d86SDave Gerlach return rproc; 1958fec47d86SDave Gerlach } 19598de3dbd0SOhad Ben-Cohen #else 19608de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle) 19618de3dbd0SOhad Ben-Cohen { 19628de3dbd0SOhad Ben-Cohen return NULL; 19638de3dbd0SOhad Ben-Cohen } 19648de3dbd0SOhad Ben-Cohen #endif 1965fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle); 1966fec47d86SDave Gerlach 19674c1ad562SSuman Anna /** 19684c1ad562SSuman Anna * rproc_set_firmware() - assign a new firmware 19694c1ad562SSuman Anna * @rproc: rproc handle to which the new firmware is being assigned 19704c1ad562SSuman Anna * @fw_name: new firmware name to be assigned 19714c1ad562SSuman Anna * 19724c1ad562SSuman Anna * This function allows remoteproc drivers or clients to configure a custom 19734c1ad562SSuman Anna * firmware name that is different from the default name used during remoteproc 19744c1ad562SSuman Anna * registration. The function does not trigger a remote processor boot, 19754c1ad562SSuman Anna * only sets the firmware name used for a subsequent boot. This function 19764c1ad562SSuman Anna * should also be called only when the remote processor is offline. 19774c1ad562SSuman Anna * 19784c1ad562SSuman Anna * This allows either the userspace to configure a different name through 19794c1ad562SSuman Anna * sysfs or a kernel-level remoteproc or a remoteproc client driver to set 19804c1ad562SSuman Anna * a specific firmware when it is controlling the boot and shutdown of the 19814c1ad562SSuman Anna * remote processor. 19824c1ad562SSuman Anna * 19834c1ad562SSuman Anna * Return: 0 on success or a negative value upon failure 19844c1ad562SSuman Anna */ 19854c1ad562SSuman Anna int rproc_set_firmware(struct rproc *rproc, const char *fw_name) 19864c1ad562SSuman Anna { 19874c1ad562SSuman Anna struct device *dev; 19884c1ad562SSuman Anna int ret, len; 19894c1ad562SSuman Anna char *p; 19904c1ad562SSuman Anna 19914c1ad562SSuman Anna if (!rproc || !fw_name) 19924c1ad562SSuman Anna return -EINVAL; 19934c1ad562SSuman Anna 19944c1ad562SSuman Anna dev = rproc->dev.parent; 19954c1ad562SSuman Anna 19964c1ad562SSuman Anna ret = mutex_lock_interruptible(&rproc->lock); 19974c1ad562SSuman Anna if (ret) { 19984c1ad562SSuman Anna dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 19994c1ad562SSuman Anna return -EINVAL; 20004c1ad562SSuman Anna } 20014c1ad562SSuman Anna 20024c1ad562SSuman Anna if (rproc->state != RPROC_OFFLINE) { 20034c1ad562SSuman Anna dev_err(dev, "can't change firmware while running\n"); 20044c1ad562SSuman Anna ret = -EBUSY; 20054c1ad562SSuman Anna goto out; 20064c1ad562SSuman Anna } 20074c1ad562SSuman Anna 20084c1ad562SSuman Anna len = strcspn(fw_name, "\n"); 20094c1ad562SSuman Anna if (!len) { 20104c1ad562SSuman Anna dev_err(dev, "can't provide empty string for firmware name\n"); 20114c1ad562SSuman Anna ret = -EINVAL; 20124c1ad562SSuman Anna goto out; 20134c1ad562SSuman Anna } 20144c1ad562SSuman Anna 20154c1ad562SSuman Anna p = kstrndup(fw_name, len, GFP_KERNEL); 20164c1ad562SSuman Anna if (!p) { 20174c1ad562SSuman Anna ret = -ENOMEM; 20184c1ad562SSuman Anna goto out; 20194c1ad562SSuman Anna } 20204c1ad562SSuman Anna 202143d3f2c7SDaniele Alessandrelli kfree_const(rproc->firmware); 20224c1ad562SSuman Anna rproc->firmware = p; 20234c1ad562SSuman Anna 20244c1ad562SSuman Anna out: 20254c1ad562SSuman Anna mutex_unlock(&rproc->lock); 20264c1ad562SSuman Anna return ret; 20274c1ad562SSuman Anna } 20284c1ad562SSuman Anna EXPORT_SYMBOL(rproc_set_firmware); 20294c1ad562SSuman Anna 203088d3a136SMathieu Poirier static int rproc_validate(struct rproc *rproc) 203188d3a136SMathieu Poirier { 203288d3a136SMathieu Poirier switch (rproc->state) { 203388d3a136SMathieu Poirier case RPROC_OFFLINE: 203488d3a136SMathieu Poirier /* 203588d3a136SMathieu Poirier * An offline processor without a start() 203688d3a136SMathieu Poirier * function makes no sense. 203788d3a136SMathieu Poirier */ 203888d3a136SMathieu Poirier if (!rproc->ops->start) 203988d3a136SMathieu Poirier return -EINVAL; 204088d3a136SMathieu Poirier break; 204188d3a136SMathieu Poirier case RPROC_DETACHED: 204288d3a136SMathieu Poirier /* 204388d3a136SMathieu Poirier * A remote processor in a detached state without an 204488d3a136SMathieu Poirier * attach() function makes not sense. 204588d3a136SMathieu Poirier */ 204688d3a136SMathieu Poirier if (!rproc->ops->attach) 204788d3a136SMathieu Poirier return -EINVAL; 204888d3a136SMathieu Poirier /* 204988d3a136SMathieu Poirier * When attaching to a remote processor the device memory 205088d3a136SMathieu Poirier * is already available and as such there is no need to have a 205188d3a136SMathieu Poirier * cached table. 205288d3a136SMathieu Poirier */ 205388d3a136SMathieu Poirier if (rproc->cached_table) 205488d3a136SMathieu Poirier return -EINVAL; 205588d3a136SMathieu Poirier break; 205688d3a136SMathieu Poirier default: 205788d3a136SMathieu Poirier /* 205888d3a136SMathieu Poirier * When adding a remote processor, the state of the device 205988d3a136SMathieu Poirier * can be offline or detached, nothing else. 206088d3a136SMathieu Poirier */ 206188d3a136SMathieu Poirier return -EINVAL; 206288d3a136SMathieu Poirier } 206388d3a136SMathieu Poirier 206488d3a136SMathieu Poirier return 0; 206588d3a136SMathieu Poirier } 206688d3a136SMathieu Poirier 2067fec47d86SDave Gerlach /** 2068160e7c84SOhad Ben-Cohen * rproc_add() - register a remote processor 2069400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle to register 2070400e64dfSOhad Ben-Cohen * 2071400e64dfSOhad Ben-Cohen * Registers @rproc with the remoteproc framework, after it has been 2072400e64dfSOhad Ben-Cohen * allocated with rproc_alloc(). 2073400e64dfSOhad Ben-Cohen * 2074400e64dfSOhad Ben-Cohen * This is called by the platform-specific rproc implementation, whenever 2075400e64dfSOhad Ben-Cohen * a new remote processor device is probed. 2076400e64dfSOhad Ben-Cohen * 2077400e64dfSOhad Ben-Cohen * Returns 0 on success and an appropriate error code otherwise. 2078400e64dfSOhad Ben-Cohen * 2079400e64dfSOhad Ben-Cohen * Note: this function initiates an asynchronous firmware loading 2080400e64dfSOhad Ben-Cohen * context, which will look for virtio devices supported by the rproc's 2081400e64dfSOhad Ben-Cohen * firmware. 2082400e64dfSOhad Ben-Cohen * 2083400e64dfSOhad Ben-Cohen * If found, those virtio devices will be created and added, so as a result 20847a186941SOhad Ben-Cohen * of registering this remote processor, additional virtio drivers might be 2085400e64dfSOhad Ben-Cohen * probed. 2086400e64dfSOhad Ben-Cohen */ 2087160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc) 2088400e64dfSOhad Ben-Cohen { 2089b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 209070b85ef8SFernando Guzman Lugo int ret; 2091400e64dfSOhad Ben-Cohen 2092b5ab5e24SOhad Ben-Cohen ret = device_add(dev); 2093b5ab5e24SOhad Ben-Cohen if (ret < 0) 2094b5ab5e24SOhad Ben-Cohen return ret; 2095400e64dfSOhad Ben-Cohen 209688d3a136SMathieu Poirier ret = rproc_validate(rproc); 209788d3a136SMathieu Poirier if (ret < 0) 209888d3a136SMathieu Poirier return ret; 209988d3a136SMathieu Poirier 2100b5ab5e24SOhad Ben-Cohen dev_info(dev, "%s is available\n", rproc->name); 2101400e64dfSOhad Ben-Cohen 2102400e64dfSOhad Ben-Cohen /* create debugfs entries */ 2103400e64dfSOhad Ben-Cohen rproc_create_debug_dir(rproc); 21047a20c64dSSarangdhar Joshi 210562b8f9e9SSiddharth Gupta /* add char device for this remoteproc */ 210662b8f9e9SSiddharth Gupta ret = rproc_char_device_add(rproc); 210762b8f9e9SSiddharth Gupta if (ret < 0) 210862b8f9e9SSiddharth Gupta return ret; 210962b8f9e9SSiddharth Gupta 21107a20c64dSSarangdhar Joshi /* if rproc is marked always-on, request it to boot */ 21117a20c64dSSarangdhar Joshi if (rproc->auto_boot) { 21125e6533f7SSarangdhar Joshi ret = rproc_trigger_auto_boot(rproc); 2113d2e12e66SDave Gerlach if (ret < 0) 2114d2e12e66SDave Gerlach return ret; 21157a20c64dSSarangdhar Joshi } 2116400e64dfSOhad Ben-Cohen 2117d2e12e66SDave Gerlach /* expose to rproc_get_by_phandle users */ 2118d2e12e66SDave Gerlach mutex_lock(&rproc_list_mutex); 2119c0abe2caSBjorn Andersson list_add_rcu(&rproc->node, &rproc_list); 2120d2e12e66SDave Gerlach mutex_unlock(&rproc_list_mutex); 2121d2e12e66SDave Gerlach 2122d2e12e66SDave Gerlach return 0; 2123400e64dfSOhad Ben-Cohen } 2124160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add); 2125400e64dfSOhad Ben-Cohen 2126305ac5a7SPaul Cercueil static void devm_rproc_remove(void *rproc) 2127305ac5a7SPaul Cercueil { 2128305ac5a7SPaul Cercueil rproc_del(rproc); 2129305ac5a7SPaul Cercueil } 2130305ac5a7SPaul Cercueil 2131305ac5a7SPaul Cercueil /** 2132305ac5a7SPaul Cercueil * devm_rproc_add() - resource managed rproc_add() 2133305ac5a7SPaul Cercueil * @dev: the underlying device 2134305ac5a7SPaul Cercueil * @rproc: the remote processor handle to register 2135305ac5a7SPaul Cercueil * 2136305ac5a7SPaul Cercueil * This function performs like rproc_add() but the registered rproc device will 2137305ac5a7SPaul Cercueil * automatically be removed on driver detach. 2138305ac5a7SPaul Cercueil * 2139305ac5a7SPaul Cercueil * Returns: 0 on success, negative errno on failure 2140305ac5a7SPaul Cercueil */ 2141305ac5a7SPaul Cercueil int devm_rproc_add(struct device *dev, struct rproc *rproc) 2142305ac5a7SPaul Cercueil { 2143305ac5a7SPaul Cercueil int err; 2144305ac5a7SPaul Cercueil 2145305ac5a7SPaul Cercueil err = rproc_add(rproc); 2146305ac5a7SPaul Cercueil if (err) 2147305ac5a7SPaul Cercueil return err; 2148305ac5a7SPaul Cercueil 2149305ac5a7SPaul Cercueil return devm_add_action_or_reset(dev, devm_rproc_remove, rproc); 2150305ac5a7SPaul Cercueil } 2151305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_add); 2152305ac5a7SPaul Cercueil 2153400e64dfSOhad Ben-Cohen /** 2154b5ab5e24SOhad Ben-Cohen * rproc_type_release() - release a remote processor instance 2155b5ab5e24SOhad Ben-Cohen * @dev: the rproc's device 2156b5ab5e24SOhad Ben-Cohen * 2157b5ab5e24SOhad Ben-Cohen * This function should _never_ be called directly. 2158b5ab5e24SOhad Ben-Cohen * 2159b5ab5e24SOhad Ben-Cohen * It will be called by the driver core when no one holds a valid pointer 2160b5ab5e24SOhad Ben-Cohen * to @dev anymore. 2161b5ab5e24SOhad Ben-Cohen */ 2162b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev) 2163b5ab5e24SOhad Ben-Cohen { 2164b5ab5e24SOhad Ben-Cohen struct rproc *rproc = container_of(dev, struct rproc, dev); 2165b5ab5e24SOhad Ben-Cohen 21667183a2a7SOhad Ben-Cohen dev_info(&rproc->dev, "releasing %s\n", rproc->name); 21677183a2a7SOhad Ben-Cohen 2168b5ab5e24SOhad Ben-Cohen idr_destroy(&rproc->notifyids); 2169b5ab5e24SOhad Ben-Cohen 2170b5ab5e24SOhad Ben-Cohen if (rproc->index >= 0) 2171b5ab5e24SOhad Ben-Cohen ida_simple_remove(&rproc_dev_index, rproc->index); 2172b5ab5e24SOhad Ben-Cohen 21731487dedaSMathieu Poirier kfree_const(rproc->firmware); 2174db655278SSuman Anna kfree_const(rproc->name); 2175fb98e2bdSBjorn Andersson kfree(rproc->ops); 2176b5ab5e24SOhad Ben-Cohen kfree(rproc); 2177b5ab5e24SOhad Ben-Cohen } 2178b5ab5e24SOhad Ben-Cohen 2179c42ca04dSBhumika Goyal static const struct device_type rproc_type = { 2180b5ab5e24SOhad Ben-Cohen .name = "remoteproc", 2181b5ab5e24SOhad Ben-Cohen .release = rproc_type_release, 2182b5ab5e24SOhad Ben-Cohen }; 2183400e64dfSOhad Ben-Cohen 21840c2ae2b1SMathieu Poirier static int rproc_alloc_firmware(struct rproc *rproc, 21850c2ae2b1SMathieu Poirier const char *name, const char *firmware) 21860c2ae2b1SMathieu Poirier { 21871487dedaSMathieu Poirier const char *p; 21880c2ae2b1SMathieu Poirier 21890c2ae2b1SMathieu Poirier /* 21909d5f82c8SMathieu Poirier * Allocate a firmware name if the caller gave us one to work 21919d5f82c8SMathieu Poirier * with. Otherwise construct a new one using a default pattern. 21920c2ae2b1SMathieu Poirier */ 21939d5f82c8SMathieu Poirier if (firmware) 21941487dedaSMathieu Poirier p = kstrdup_const(firmware, GFP_KERNEL); 21959d5f82c8SMathieu Poirier else 21969d5f82c8SMathieu Poirier p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name); 21974df4f8beSMathieu Poirier 21980c2ae2b1SMathieu Poirier if (!p) 21990c2ae2b1SMathieu Poirier return -ENOMEM; 22000c2ae2b1SMathieu Poirier 22010c2ae2b1SMathieu Poirier rproc->firmware = p; 22020c2ae2b1SMathieu Poirier 22030c2ae2b1SMathieu Poirier return 0; 22040c2ae2b1SMathieu Poirier } 22050c2ae2b1SMathieu Poirier 2206bf860aa1SMathieu Poirier static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops) 2207bf860aa1SMathieu Poirier { 2208bf860aa1SMathieu Poirier rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); 2209bf860aa1SMathieu Poirier if (!rproc->ops) 2210bf860aa1SMathieu Poirier return -ENOMEM; 2211bf860aa1SMathieu Poirier 2212adf60a87SSiddharth Gupta /* Default to rproc_coredump if no coredump function is specified */ 2213adf60a87SSiddharth Gupta if (!rproc->ops->coredump) 2214adf60a87SSiddharth Gupta rproc->ops->coredump = rproc_coredump; 2215adf60a87SSiddharth Gupta 2216bf860aa1SMathieu Poirier if (rproc->ops->load) 2217bf860aa1SMathieu Poirier return 0; 2218bf860aa1SMathieu Poirier 2219bf860aa1SMathieu Poirier /* Default to ELF loader if no load function is specified */ 2220bf860aa1SMathieu Poirier rproc->ops->load = rproc_elf_load_segments; 2221bf860aa1SMathieu Poirier rproc->ops->parse_fw = rproc_elf_load_rsc_table; 2222bf860aa1SMathieu Poirier rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; 2223e29ff72bSClement Leger rproc->ops->sanity_check = rproc_elf_sanity_check; 2224bf860aa1SMathieu Poirier rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; 2225bf860aa1SMathieu Poirier 2226bf860aa1SMathieu Poirier return 0; 2227bf860aa1SMathieu Poirier } 2228bf860aa1SMathieu Poirier 2229400e64dfSOhad Ben-Cohen /** 2230400e64dfSOhad Ben-Cohen * rproc_alloc() - allocate a remote processor handle 2231400e64dfSOhad Ben-Cohen * @dev: the underlying device 2232400e64dfSOhad Ben-Cohen * @name: name of this remote processor 2233400e64dfSOhad Ben-Cohen * @ops: platform-specific handlers (mainly start/stop) 22348b4aec9aSRobert Tivy * @firmware: name of firmware file to load, can be NULL 2235400e64dfSOhad Ben-Cohen * @len: length of private data needed by the rproc driver (in bytes) 2236400e64dfSOhad Ben-Cohen * 2237400e64dfSOhad Ben-Cohen * Allocates a new remote processor handle, but does not register 22388b4aec9aSRobert Tivy * it yet. if @firmware is NULL, a default name is used. 2239400e64dfSOhad Ben-Cohen * 2240400e64dfSOhad Ben-Cohen * This function should be used by rproc implementations during initialization 2241400e64dfSOhad Ben-Cohen * of the remote processor. 2242400e64dfSOhad Ben-Cohen * 2243400e64dfSOhad Ben-Cohen * After creating an rproc handle using this function, and when ready, 2244160e7c84SOhad Ben-Cohen * implementations should then call rproc_add() to complete 2245400e64dfSOhad Ben-Cohen * the registration of the remote processor. 2246400e64dfSOhad Ben-Cohen * 2247400e64dfSOhad Ben-Cohen * On success the new rproc is returned, and on failure, NULL. 2248400e64dfSOhad Ben-Cohen * 2249400e64dfSOhad Ben-Cohen * Note: _never_ directly deallocate @rproc, even if it was not registered 2250433c0e04SBjorn Andersson * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). 2251400e64dfSOhad Ben-Cohen */ 2252400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name, 2253400e64dfSOhad Ben-Cohen const struct rproc_ops *ops, 2254400e64dfSOhad Ben-Cohen const char *firmware, int len) 2255400e64dfSOhad Ben-Cohen { 2256400e64dfSOhad Ben-Cohen struct rproc *rproc; 2257400e64dfSOhad Ben-Cohen 2258400e64dfSOhad Ben-Cohen if (!dev || !name || !ops) 2259400e64dfSOhad Ben-Cohen return NULL; 2260400e64dfSOhad Ben-Cohen 22610f57dc6aSMatt Redfearn rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); 22620c2ae2b1SMathieu Poirier if (!rproc) 22630f57dc6aSMatt Redfearn return NULL; 22640c2ae2b1SMathieu Poirier 2265400e64dfSOhad Ben-Cohen rproc->priv = &rproc[1]; 2266ddf71187SBjorn Andersson rproc->auto_boot = true; 2267418fd787SClement Leger rproc->elf_class = ELFCLASSNONE; 2268418fd787SClement Leger rproc->elf_machine = EM_NONE; 2269400e64dfSOhad Ben-Cohen 2270b5ab5e24SOhad Ben-Cohen device_initialize(&rproc->dev); 2271b5ab5e24SOhad Ben-Cohen rproc->dev.parent = dev; 2272b5ab5e24SOhad Ben-Cohen rproc->dev.type = &rproc_type; 22732aefbef0SMatt Redfearn rproc->dev.class = &rproc_class; 22747c89717fSBjorn Andersson rproc->dev.driver_data = rproc; 22756442df49SAlex Elder idr_init(&rproc->notifyids); 2276b5ab5e24SOhad Ben-Cohen 2277db655278SSuman Anna rproc->name = kstrdup_const(name, GFP_KERNEL); 2278db655278SSuman Anna if (!rproc->name) 2279db655278SSuman Anna goto put_device; 2280db655278SSuman Anna 2281226f5db4SMathieu Poirier if (rproc_alloc_firmware(rproc, name, firmware)) 2282226f5db4SMathieu Poirier goto put_device; 2283226f5db4SMathieu Poirier 2284226f5db4SMathieu Poirier if (rproc_alloc_ops(rproc, ops)) 2285226f5db4SMathieu Poirier goto put_device; 2286226f5db4SMathieu Poirier 2287b5ab5e24SOhad Ben-Cohen /* Assign a unique device index and name */ 2288b5ab5e24SOhad Ben-Cohen rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL); 2289b5ab5e24SOhad Ben-Cohen if (rproc->index < 0) { 2290b5ab5e24SOhad Ben-Cohen dev_err(dev, "ida_simple_get failed: %d\n", rproc->index); 2291226f5db4SMathieu Poirier goto put_device; 2292b5ab5e24SOhad Ben-Cohen } 2293b5ab5e24SOhad Ben-Cohen 2294b5ab5e24SOhad Ben-Cohen dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); 2295b5ab5e24SOhad Ben-Cohen 2296400e64dfSOhad Ben-Cohen atomic_set(&rproc->power, 0); 2297400e64dfSOhad Ben-Cohen 2298400e64dfSOhad Ben-Cohen mutex_init(&rproc->lock); 2299400e64dfSOhad Ben-Cohen 2300400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->carveouts); 2301400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->mappings); 2302400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->traces); 23037a186941SOhad Ben-Cohen INIT_LIST_HEAD(&rproc->rvdevs); 23047bdc9650SBjorn Andersson INIT_LIST_HEAD(&rproc->subdevs); 23052666ca91SSarangdhar Joshi INIT_LIST_HEAD(&rproc->dump_segments); 2306400e64dfSOhad Ben-Cohen 23078afd519cSFernando Guzman Lugo INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); 23088afd519cSFernando Guzman Lugo 2309400e64dfSOhad Ben-Cohen rproc->state = RPROC_OFFLINE; 2310400e64dfSOhad Ben-Cohen 2311400e64dfSOhad Ben-Cohen return rproc; 23120c2ae2b1SMathieu Poirier 2313226f5db4SMathieu Poirier put_device: 2314226f5db4SMathieu Poirier put_device(&rproc->dev); 23150c2ae2b1SMathieu Poirier return NULL; 2316400e64dfSOhad Ben-Cohen } 2317400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc); 2318400e64dfSOhad Ben-Cohen 2319400e64dfSOhad Ben-Cohen /** 2320433c0e04SBjorn Andersson * rproc_free() - unroll rproc_alloc() 2321433c0e04SBjorn Andersson * @rproc: the remote processor handle 2322433c0e04SBjorn Andersson * 2323433c0e04SBjorn Andersson * This function decrements the rproc dev refcount. 2324433c0e04SBjorn Andersson * 2325433c0e04SBjorn Andersson * If no one holds any reference to rproc anymore, then its refcount would 2326433c0e04SBjorn Andersson * now drop to zero, and it would be freed. 2327433c0e04SBjorn Andersson */ 2328433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc) 2329433c0e04SBjorn Andersson { 2330433c0e04SBjorn Andersson put_device(&rproc->dev); 2331433c0e04SBjorn Andersson } 2332433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free); 2333433c0e04SBjorn Andersson 2334433c0e04SBjorn Andersson /** 2335433c0e04SBjorn Andersson * rproc_put() - release rproc reference 2336400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle 2337400e64dfSOhad Ben-Cohen * 2338c6b5a276SOhad Ben-Cohen * This function decrements the rproc dev refcount. 2339400e64dfSOhad Ben-Cohen * 2340c6b5a276SOhad Ben-Cohen * If no one holds any reference to rproc anymore, then its refcount would 2341c6b5a276SOhad Ben-Cohen * now drop to zero, and it would be freed. 2342400e64dfSOhad Ben-Cohen */ 2343160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc) 2344400e64dfSOhad Ben-Cohen { 2345fbb6aacbSBjorn Andersson module_put(rproc->dev.parent->driver->owner); 2346b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 2347400e64dfSOhad Ben-Cohen } 2348160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put); 2349400e64dfSOhad Ben-Cohen 2350400e64dfSOhad Ben-Cohen /** 2351160e7c84SOhad Ben-Cohen * rproc_del() - unregister a remote processor 2352400e64dfSOhad Ben-Cohen * @rproc: rproc handle to unregister 2353400e64dfSOhad Ben-Cohen * 2354400e64dfSOhad Ben-Cohen * This function should be called when the platform specific rproc 2355400e64dfSOhad Ben-Cohen * implementation decides to remove the rproc device. it should 2356160e7c84SOhad Ben-Cohen * _only_ be called if a previous invocation of rproc_add() 2357400e64dfSOhad Ben-Cohen * has completed successfully. 2358400e64dfSOhad Ben-Cohen * 2359160e7c84SOhad Ben-Cohen * After rproc_del() returns, @rproc isn't freed yet, because 2360c6b5a276SOhad Ben-Cohen * of the outstanding reference created by rproc_alloc. To decrement that 2361433c0e04SBjorn Andersson * one last refcount, one still needs to call rproc_free(). 2362400e64dfSOhad Ben-Cohen * 2363400e64dfSOhad Ben-Cohen * Returns 0 on success and -EINVAL if @rproc isn't valid. 2364400e64dfSOhad Ben-Cohen */ 2365160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc) 2366400e64dfSOhad Ben-Cohen { 2367400e64dfSOhad Ben-Cohen if (!rproc) 2368400e64dfSOhad Ben-Cohen return -EINVAL; 2369400e64dfSOhad Ben-Cohen 2370ddf71187SBjorn Andersson /* TODO: make sure this works with rproc->power > 1 */ 2371ddf71187SBjorn Andersson rproc_shutdown(rproc); 2372ddf71187SBjorn Andersson 23732099c77dSSarangdhar Joshi mutex_lock(&rproc->lock); 23742099c77dSSarangdhar Joshi rproc->state = RPROC_DELETED; 23752099c77dSSarangdhar Joshi mutex_unlock(&rproc->lock); 23762099c77dSSarangdhar Joshi 2377b003d45bSSarangdhar Joshi rproc_delete_debug_dir(rproc); 237862b8f9e9SSiddharth Gupta rproc_char_device_remove(rproc); 2379b003d45bSSarangdhar Joshi 2380fec47d86SDave Gerlach /* the rproc is downref'ed as soon as it's removed from the klist */ 2381fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 2382c0abe2caSBjorn Andersson list_del_rcu(&rproc->node); 2383fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 2384fec47d86SDave Gerlach 2385c0abe2caSBjorn Andersson /* Ensure that no readers of rproc_list are still active */ 2386c0abe2caSBjorn Andersson synchronize_rcu(); 2387c0abe2caSBjorn Andersson 2388b5ab5e24SOhad Ben-Cohen device_del(&rproc->dev); 2389400e64dfSOhad Ben-Cohen 2390400e64dfSOhad Ben-Cohen return 0; 2391400e64dfSOhad Ben-Cohen } 2392160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del); 2393400e64dfSOhad Ben-Cohen 2394305ac5a7SPaul Cercueil static void devm_rproc_free(struct device *dev, void *res) 2395305ac5a7SPaul Cercueil { 2396305ac5a7SPaul Cercueil rproc_free(*(struct rproc **)res); 2397305ac5a7SPaul Cercueil } 2398305ac5a7SPaul Cercueil 2399305ac5a7SPaul Cercueil /** 2400305ac5a7SPaul Cercueil * devm_rproc_alloc() - resource managed rproc_alloc() 2401305ac5a7SPaul Cercueil * @dev: the underlying device 2402305ac5a7SPaul Cercueil * @name: name of this remote processor 2403305ac5a7SPaul Cercueil * @ops: platform-specific handlers (mainly start/stop) 2404305ac5a7SPaul Cercueil * @firmware: name of firmware file to load, can be NULL 2405305ac5a7SPaul Cercueil * @len: length of private data needed by the rproc driver (in bytes) 2406305ac5a7SPaul Cercueil * 2407305ac5a7SPaul Cercueil * This function performs like rproc_alloc() but the acquired rproc device will 2408305ac5a7SPaul Cercueil * automatically be released on driver detach. 2409305ac5a7SPaul Cercueil * 2410305ac5a7SPaul Cercueil * Returns: new rproc instance, or NULL on failure 2411305ac5a7SPaul Cercueil */ 2412305ac5a7SPaul Cercueil struct rproc *devm_rproc_alloc(struct device *dev, const char *name, 2413305ac5a7SPaul Cercueil const struct rproc_ops *ops, 2414305ac5a7SPaul Cercueil const char *firmware, int len) 2415305ac5a7SPaul Cercueil { 2416305ac5a7SPaul Cercueil struct rproc **ptr, *rproc; 2417305ac5a7SPaul Cercueil 2418305ac5a7SPaul Cercueil ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL); 2419305ac5a7SPaul Cercueil if (!ptr) 24207dcef398SDan Carpenter return NULL; 2421305ac5a7SPaul Cercueil 2422305ac5a7SPaul Cercueil rproc = rproc_alloc(dev, name, ops, firmware, len); 2423305ac5a7SPaul Cercueil if (rproc) { 2424305ac5a7SPaul Cercueil *ptr = rproc; 2425305ac5a7SPaul Cercueil devres_add(dev, ptr); 2426305ac5a7SPaul Cercueil } else { 2427305ac5a7SPaul Cercueil devres_free(ptr); 2428305ac5a7SPaul Cercueil } 2429305ac5a7SPaul Cercueil 2430305ac5a7SPaul Cercueil return rproc; 2431305ac5a7SPaul Cercueil } 2432305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_alloc); 2433305ac5a7SPaul Cercueil 24348afd519cSFernando Guzman Lugo /** 24357bdc9650SBjorn Andersson * rproc_add_subdev() - add a subdevice to a remoteproc 24367bdc9650SBjorn Andersson * @rproc: rproc handle to add the subdevice to 24377bdc9650SBjorn Andersson * @subdev: subdev handle to register 24384902676fSBjorn Andersson * 24394902676fSBjorn Andersson * Caller is responsible for populating optional subdevice function pointers. 24407bdc9650SBjorn Andersson */ 24414902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 24427bdc9650SBjorn Andersson { 24437bdc9650SBjorn Andersson list_add_tail(&subdev->node, &rproc->subdevs); 24447bdc9650SBjorn Andersson } 24457bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev); 24467bdc9650SBjorn Andersson 24477bdc9650SBjorn Andersson /** 24487bdc9650SBjorn Andersson * rproc_remove_subdev() - remove a subdevice from a remoteproc 24497bdc9650SBjorn Andersson * @rproc: rproc handle to remove the subdevice from 24507bdc9650SBjorn Andersson * @subdev: subdev handle, previously registered with rproc_add_subdev() 24517bdc9650SBjorn Andersson */ 24527bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 24537bdc9650SBjorn Andersson { 24547bdc9650SBjorn Andersson list_del(&subdev->node); 24557bdc9650SBjorn Andersson } 24567bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev); 24577bdc9650SBjorn Andersson 24587bdc9650SBjorn Andersson /** 24597c89717fSBjorn Andersson * rproc_get_by_child() - acquire rproc handle of @dev's ancestor 24607c89717fSBjorn Andersson * @dev: child device to find ancestor of 24617c89717fSBjorn Andersson * 24627c89717fSBjorn Andersson * Returns the ancestor rproc instance, or NULL if not found. 24637c89717fSBjorn Andersson */ 24647c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev) 24657c89717fSBjorn Andersson { 24667c89717fSBjorn Andersson for (dev = dev->parent; dev; dev = dev->parent) { 24677c89717fSBjorn Andersson if (dev->type == &rproc_type) 24687c89717fSBjorn Andersson return dev->driver_data; 24697c89717fSBjorn Andersson } 24707c89717fSBjorn Andersson 24717c89717fSBjorn Andersson return NULL; 24727c89717fSBjorn Andersson } 24737c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child); 24747c89717fSBjorn Andersson 24757c89717fSBjorn Andersson /** 24768afd519cSFernando Guzman Lugo * rproc_report_crash() - rproc crash reporter function 24778afd519cSFernando Guzman Lugo * @rproc: remote processor 24788afd519cSFernando Guzman Lugo * @type: crash type 24798afd519cSFernando Guzman Lugo * 24808afd519cSFernando Guzman Lugo * This function must be called every time a crash is detected by the low-level 24818afd519cSFernando Guzman Lugo * drivers implementing a specific remoteproc. This should not be called from a 24828afd519cSFernando Guzman Lugo * non-remoteproc driver. 24838afd519cSFernando Guzman Lugo * 24848afd519cSFernando Guzman Lugo * This function can be called from atomic/interrupt context. 24858afd519cSFernando Guzman Lugo */ 24868afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) 24878afd519cSFernando Guzman Lugo { 24888afd519cSFernando Guzman Lugo if (!rproc) { 24898afd519cSFernando Guzman Lugo pr_err("NULL rproc pointer\n"); 24908afd519cSFernando Guzman Lugo return; 24918afd519cSFernando Guzman Lugo } 24928afd519cSFernando Guzman Lugo 2493a781e5aaSRishabh Bhatnagar /* Prevent suspend while the remoteproc is being recovered */ 2494a781e5aaSRishabh Bhatnagar pm_stay_awake(rproc->dev.parent); 2495a781e5aaSRishabh Bhatnagar 24968afd519cSFernando Guzman Lugo dev_err(&rproc->dev, "crash detected in %s: type %s\n", 24978afd519cSFernando Guzman Lugo rproc->name, rproc_crash_to_string(type)); 24988afd519cSFernando Guzman Lugo 24998afd519cSFernando Guzman Lugo /* create a new task to handle the error */ 25008afd519cSFernando Guzman Lugo schedule_work(&rproc->crash_handler); 25018afd519cSFernando Guzman Lugo } 25028afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash); 25038afd519cSFernando Guzman Lugo 2504dc5192c4SBjorn Andersson static int rproc_panic_handler(struct notifier_block *nb, unsigned long event, 2505dc5192c4SBjorn Andersson void *ptr) 2506dc5192c4SBjorn Andersson { 2507dc5192c4SBjorn Andersson unsigned int longest = 0; 2508dc5192c4SBjorn Andersson struct rproc *rproc; 2509dc5192c4SBjorn Andersson unsigned int d; 2510dc5192c4SBjorn Andersson 2511dc5192c4SBjorn Andersson rcu_read_lock(); 2512dc5192c4SBjorn Andersson list_for_each_entry_rcu(rproc, &rproc_list, node) { 2513dc5192c4SBjorn Andersson if (!rproc->ops->panic || rproc->state != RPROC_RUNNING) 2514dc5192c4SBjorn Andersson continue; 2515dc5192c4SBjorn Andersson 2516dc5192c4SBjorn Andersson d = rproc->ops->panic(rproc); 2517dc5192c4SBjorn Andersson longest = max(longest, d); 2518dc5192c4SBjorn Andersson } 2519dc5192c4SBjorn Andersson rcu_read_unlock(); 2520dc5192c4SBjorn Andersson 2521dc5192c4SBjorn Andersson /* 2522dc5192c4SBjorn Andersson * Delay for the longest requested duration before returning. This can 2523dc5192c4SBjorn Andersson * be used by the remoteproc drivers to give the remote processor time 2524dc5192c4SBjorn Andersson * to perform any requested operations (such as flush caches), when 2525dc5192c4SBjorn Andersson * it's not possible to signal the Linux side due to the panic. 2526dc5192c4SBjorn Andersson */ 2527dc5192c4SBjorn Andersson mdelay(longest); 2528dc5192c4SBjorn Andersson 2529dc5192c4SBjorn Andersson return NOTIFY_DONE; 2530dc5192c4SBjorn Andersson } 2531dc5192c4SBjorn Andersson 2532dc5192c4SBjorn Andersson static void __init rproc_init_panic(void) 2533dc5192c4SBjorn Andersson { 2534dc5192c4SBjorn Andersson rproc_panic_nb.notifier_call = rproc_panic_handler; 2535dc5192c4SBjorn Andersson atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb); 2536dc5192c4SBjorn Andersson } 2537dc5192c4SBjorn Andersson 2538dc5192c4SBjorn Andersson static void __exit rproc_exit_panic(void) 2539dc5192c4SBjorn Andersson { 2540dc5192c4SBjorn Andersson atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb); 2541dc5192c4SBjorn Andersson } 2542dc5192c4SBjorn Andersson 2543400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void) 2544400e64dfSOhad Ben-Cohen { 25452aefbef0SMatt Redfearn rproc_init_sysfs(); 2546400e64dfSOhad Ben-Cohen rproc_init_debugfs(); 254762b8f9e9SSiddharth Gupta rproc_init_cdev(); 2548dc5192c4SBjorn Andersson rproc_init_panic(); 2549b5ab5e24SOhad Ben-Cohen 2550400e64dfSOhad Ben-Cohen return 0; 2551400e64dfSOhad Ben-Cohen } 2552a8f40111SBrandon Maier subsys_initcall(remoteproc_init); 2553400e64dfSOhad Ben-Cohen 2554400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void) 2555400e64dfSOhad Ben-Cohen { 2556f42f79afSSuman Anna ida_destroy(&rproc_dev_index); 2557f42f79afSSuman Anna 2558dc5192c4SBjorn Andersson rproc_exit_panic(); 2559400e64dfSOhad Ben-Cohen rproc_exit_debugfs(); 25602aefbef0SMatt Redfearn rproc_exit_sysfs(); 2561400e64dfSOhad Ben-Cohen } 2562400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit); 2563400e64dfSOhad Ben-Cohen 2564400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2"); 2565400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework"); 2566