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> 25400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h> 26400e64dfSOhad Ben-Cohen #include <linux/firmware.h> 27400e64dfSOhad Ben-Cohen #include <linux/string.h> 28400e64dfSOhad Ben-Cohen #include <linux/debugfs.h> 292666ca91SSarangdhar Joshi #include <linux/devcoredump.h> 30c0abe2caSBjorn Andersson #include <linux/rculist.h> 31400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h> 32400e64dfSOhad Ben-Cohen #include <linux/iommu.h> 33b5ab5e24SOhad Ben-Cohen #include <linux/idr.h> 34400e64dfSOhad Ben-Cohen #include <linux/elf.h> 35a2b950acSOhad Ben-Cohen #include <linux/crc32.h> 36086d0872SLoic Pallardy #include <linux/of_reserved_mem.h> 37400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h> 38400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h> 39cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h> 40086d0872SLoic Pallardy #include <linux/platform_device.h> 41400e64dfSOhad Ben-Cohen 42400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h" 438f403350SClement Leger #include "remoteproc_elf_helpers.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 */ 1929ce3bf22SClement Leger void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len) 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) { 198a01f7cd6SSuman Anna ptr = rproc->ops->da_to_va(rproc, da, len); 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 220400e64dfSOhad Ben-Cohen break; 221400e64dfSOhad Ben-Cohen } 222400e64dfSOhad Ben-Cohen 223a01f7cd6SSuman Anna out: 224400e64dfSOhad Ben-Cohen return ptr; 225400e64dfSOhad Ben-Cohen } 2264afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va); 227400e64dfSOhad Ben-Cohen 228b0019ccdSLoic Pallardy /** 229b0019ccdSLoic Pallardy * rproc_find_carveout_by_name() - lookup the carveout region by a name 230b0019ccdSLoic Pallardy * @rproc: handle of a remote processor 2312e7d4c2cSArnaud Pouliquen * @name: carveout name to find (format string) 2322e7d4c2cSArnaud Pouliquen * @...: optional parameters matching @name string 233b0019ccdSLoic Pallardy * 234b0019ccdSLoic Pallardy * Platform driver has the capability to register some pre-allacoted carveout 235b0019ccdSLoic Pallardy * (physically contiguous memory regions) before rproc firmware loading and 236b0019ccdSLoic Pallardy * associated resource table analysis. These regions may be dedicated memory 237b0019ccdSLoic Pallardy * regions internal to the coprocessor or specified DDR region with specific 238b0019ccdSLoic Pallardy * attributes 239b0019ccdSLoic Pallardy * 240b0019ccdSLoic Pallardy * This function is a helper function with which we can go over the 241b0019ccdSLoic Pallardy * allocated carveouts and return associated region characteristics like 242b0019ccdSLoic Pallardy * coprocessor address, length or processor virtual address. 243b0019ccdSLoic Pallardy * 244b0019ccdSLoic Pallardy * Return: a valid pointer on carveout entry on success or NULL on failure. 245b0019ccdSLoic Pallardy */ 2467e05c8deSLee Jones __printf(2, 3) 247b0019ccdSLoic Pallardy struct rproc_mem_entry * 248b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...) 249b0019ccdSLoic Pallardy { 250b0019ccdSLoic Pallardy va_list args; 251b0019ccdSLoic Pallardy char _name[32]; 252b0019ccdSLoic Pallardy struct rproc_mem_entry *carveout, *mem = NULL; 253b0019ccdSLoic Pallardy 254b0019ccdSLoic Pallardy if (!name) 255b0019ccdSLoic Pallardy return NULL; 256b0019ccdSLoic Pallardy 257b0019ccdSLoic Pallardy va_start(args, name); 258b0019ccdSLoic Pallardy vsnprintf(_name, sizeof(_name), name, args); 259b0019ccdSLoic Pallardy va_end(args); 260b0019ccdSLoic Pallardy 261b0019ccdSLoic Pallardy list_for_each_entry(carveout, &rproc->carveouts, node) { 262b0019ccdSLoic Pallardy /* Compare carveout and requested names */ 263b0019ccdSLoic Pallardy if (!strcmp(carveout->name, _name)) { 264b0019ccdSLoic Pallardy mem = carveout; 265b0019ccdSLoic Pallardy break; 266b0019ccdSLoic Pallardy } 267b0019ccdSLoic Pallardy } 268b0019ccdSLoic Pallardy 269b0019ccdSLoic Pallardy return mem; 270b0019ccdSLoic Pallardy } 271b0019ccdSLoic Pallardy 272c874bf59SLoic Pallardy /** 273c874bf59SLoic Pallardy * rproc_check_carveout_da() - Check specified carveout da configuration 274c874bf59SLoic Pallardy * @rproc: handle of a remote processor 275c874bf59SLoic Pallardy * @mem: pointer on carveout to check 276c874bf59SLoic Pallardy * @da: area device address 277c874bf59SLoic Pallardy * @len: associated area size 278c874bf59SLoic Pallardy * 279c874bf59SLoic Pallardy * This function is a helper function to verify requested device area (couple 28028d7d5c6SLoic Pallardy * da, len) is part of specified carveout. 28128d7d5c6SLoic Pallardy * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is 28228d7d5c6SLoic Pallardy * checked. 283c874bf59SLoic Pallardy * 28428d7d5c6SLoic Pallardy * Return: 0 if carveout matches request else error 285c874bf59SLoic Pallardy */ 28628d7d5c6SLoic Pallardy static int rproc_check_carveout_da(struct rproc *rproc, 28728d7d5c6SLoic Pallardy struct rproc_mem_entry *mem, u32 da, u32 len) 288c874bf59SLoic Pallardy { 289c874bf59SLoic Pallardy struct device *dev = &rproc->dev; 29028d7d5c6SLoic Pallardy int delta; 291c874bf59SLoic Pallardy 292c874bf59SLoic Pallardy /* Check requested resource length */ 293c874bf59SLoic Pallardy if (len > mem->len) { 294c874bf59SLoic Pallardy dev_err(dev, "Registered carveout doesn't fit len request\n"); 29528d7d5c6SLoic Pallardy return -EINVAL; 296c874bf59SLoic Pallardy } 297c874bf59SLoic Pallardy 298c874bf59SLoic Pallardy if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) { 29928d7d5c6SLoic Pallardy /* Address doesn't match registered carveout configuration */ 30028d7d5c6SLoic Pallardy return -EINVAL; 301c874bf59SLoic Pallardy } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) { 302c874bf59SLoic Pallardy delta = da - mem->da; 303c874bf59SLoic Pallardy 304c874bf59SLoic Pallardy /* Check requested resource belongs to registered carveout */ 305c874bf59SLoic Pallardy if (delta < 0) { 306c874bf59SLoic Pallardy dev_err(dev, 307c874bf59SLoic Pallardy "Registered carveout doesn't fit da request\n"); 30828d7d5c6SLoic Pallardy return -EINVAL; 309c874bf59SLoic Pallardy } 310c874bf59SLoic Pallardy 311c874bf59SLoic Pallardy if (delta + len > mem->len) { 312c874bf59SLoic Pallardy dev_err(dev, 313c874bf59SLoic Pallardy "Registered carveout doesn't fit len request\n"); 31428d7d5c6SLoic Pallardy return -EINVAL; 315c874bf59SLoic Pallardy } 316c874bf59SLoic Pallardy } 317c874bf59SLoic Pallardy 318c874bf59SLoic Pallardy return 0; 319c874bf59SLoic Pallardy } 320c874bf59SLoic Pallardy 3216db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) 322400e64dfSOhad Ben-Cohen { 3237a186941SOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 324b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 3256db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 326c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 327096ee786SClement Leger int ret, notifyid; 328c6aed238SLoic Pallardy struct rproc_mem_entry *mem; 329096ee786SClement Leger size_t size; 330400e64dfSOhad Ben-Cohen 3316db20ea8SOhad Ben-Cohen /* actual size of vring (in bytes) */ 3326db20ea8SOhad Ben-Cohen size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); 3336db20ea8SOhad Ben-Cohen 334c6aed238SLoic Pallardy rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; 335c6aed238SLoic Pallardy 336c6aed238SLoic Pallardy /* Search for pre-registered carveout */ 337c6aed238SLoic Pallardy mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index, 338c6aed238SLoic Pallardy i); 339c6aed238SLoic Pallardy if (mem) { 340c6aed238SLoic Pallardy if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size)) 341c6aed238SLoic Pallardy return -ENOMEM; 342c6aed238SLoic Pallardy } else { 343c6aed238SLoic Pallardy /* Register carveout in in list */ 34499cf0361SBen Dooks (Codethink) mem = rproc_mem_entry_init(dev, NULL, 0, 34599cf0361SBen Dooks (Codethink) size, rsc->vring[i].da, 346c6aed238SLoic Pallardy rproc_alloc_carveout, 347c6aed238SLoic Pallardy rproc_release_carveout, 348c6aed238SLoic Pallardy "vdev%dvring%d", 349c6aed238SLoic Pallardy rvdev->index, i); 350c6aed238SLoic Pallardy if (!mem) { 351c6aed238SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 352c6aed238SLoic Pallardy return -ENOMEM; 353c6aed238SLoic Pallardy } 354c6aed238SLoic Pallardy 355c6aed238SLoic Pallardy rproc_add_carveout(rproc, mem); 3566db20ea8SOhad Ben-Cohen } 3576db20ea8SOhad Ben-Cohen 3586db20ea8SOhad Ben-Cohen /* 3596db20ea8SOhad Ben-Cohen * Assign an rproc-wide unique index for this vring 3606db20ea8SOhad Ben-Cohen * TODO: assign a notifyid for rvdev updates as well 3616db20ea8SOhad Ben-Cohen * TODO: support predefined notifyids (via resource table) 3626db20ea8SOhad Ben-Cohen */ 36315fc6110STejun Heo ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); 364b39599b7SSuman Anna if (ret < 0) { 36515fc6110STejun Heo dev_err(dev, "idr_alloc failed: %d\n", ret); 3666db20ea8SOhad Ben-Cohen return ret; 3676db20ea8SOhad Ben-Cohen } 36815fc6110STejun Heo notifyid = ret; 3696db20ea8SOhad Ben-Cohen 37048f18f89SBjorn Andersson /* Potentially bump max_notifyid */ 37148f18f89SBjorn Andersson if (notifyid > rproc->max_notifyid) 37248f18f89SBjorn Andersson rproc->max_notifyid = notifyid; 37348f18f89SBjorn Andersson 3746db20ea8SOhad Ben-Cohen rvring->notifyid = notifyid; 3756db20ea8SOhad Ben-Cohen 376c6aed238SLoic Pallardy /* Let the rproc know the notifyid of this vring.*/ 377c0d63157SSjur Brændeland rsc->vring[i].notifyid = notifyid; 3786db20ea8SOhad Ben-Cohen return 0; 3796db20ea8SOhad Ben-Cohen } 3806db20ea8SOhad Ben-Cohen 381400e64dfSOhad Ben-Cohen static int 3826db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) 383400e64dfSOhad Ben-Cohen { 384400e64dfSOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 385b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 386400e64dfSOhad Ben-Cohen struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; 3876db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 388400e64dfSOhad Ben-Cohen 3899d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n", 3907a186941SOhad Ben-Cohen i, vring->da, vring->num, vring->align); 3917a186941SOhad Ben-Cohen 39263140e0eSOhad Ben-Cohen /* verify queue size and vring alignment are sane */ 39363140e0eSOhad Ben-Cohen if (!vring->num || !vring->align) { 39463140e0eSOhad Ben-Cohen dev_err(dev, "invalid qsz (%d) or alignment (%d)\n", 39563140e0eSOhad Ben-Cohen vring->num, vring->align); 396400e64dfSOhad Ben-Cohen return -EINVAL; 397400e64dfSOhad Ben-Cohen } 398400e64dfSOhad Ben-Cohen 3996db20ea8SOhad Ben-Cohen rvring->len = vring->num; 4006db20ea8SOhad Ben-Cohen rvring->align = vring->align; 4016db20ea8SOhad Ben-Cohen rvring->rvdev = rvdev; 402400e64dfSOhad Ben-Cohen 403400e64dfSOhad Ben-Cohen return 0; 404400e64dfSOhad Ben-Cohen } 405400e64dfSOhad Ben-Cohen 4066db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring) 4077a186941SOhad Ben-Cohen { 4086db20ea8SOhad Ben-Cohen struct rproc *rproc = rvring->rvdev->rproc; 40900a0eec5SClement Leger int idx = rvring - rvring->rvdev->vring; 410c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 4117a186941SOhad Ben-Cohen 4127a186941SOhad Ben-Cohen idr_remove(&rproc->notifyids, rvring->notifyid); 413099a3f33SSjur Brændeland 4144d3ebb3bSMathieu Poirier /* 4154d3ebb3bSMathieu Poirier * At this point rproc_stop() has been called and the installed resource 4164d3ebb3bSMathieu Poirier * table in the remote processor memory may no longer be accessible. As 4174d3ebb3bSMathieu Poirier * such and as per rproc_stop(), rproc->table_ptr points to the cached 4184d3ebb3bSMathieu Poirier * resource table (rproc->cached_table). The cached resource table is 4194d3ebb3bSMathieu Poirier * only available when a remote processor has been booted by the 4204d3ebb3bSMathieu Poirier * remoteproc core, otherwise it is NULL. 4214d3ebb3bSMathieu Poirier * 4224d3ebb3bSMathieu Poirier * Based on the above, reset the virtio device section in the cached 4234d3ebb3bSMathieu Poirier * resource table only if there is one to work with. 4244d3ebb3bSMathieu Poirier */ 4254d3ebb3bSMathieu Poirier if (rproc->table_ptr) { 426c0d63157SSjur Brændeland rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; 427c0d63157SSjur Brændeland rsc->vring[idx].da = 0; 428c0d63157SSjur Brændeland rsc->vring[idx].notifyid = -1; 4297a186941SOhad Ben-Cohen } 4304d3ebb3bSMathieu Poirier } 4317a186941SOhad Ben-Cohen 4326f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev) 433f5bcb353SBjorn Andersson { 434f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 435f5bcb353SBjorn Andersson 436f5bcb353SBjorn Andersson return rproc_add_virtio_dev(rvdev, rvdev->id); 437f5bcb353SBjorn Andersson } 438f5bcb353SBjorn Andersson 4396f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed) 440f5bcb353SBjorn Andersson { 441f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 442d4c036feSLoic Pallardy int ret; 443f5bcb353SBjorn Andersson 444d4c036feSLoic Pallardy ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev); 445d4c036feSLoic Pallardy if (ret) 446d4c036feSLoic Pallardy dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret); 447f5bcb353SBjorn Andersson } 448f5bcb353SBjorn Andersson 449400e64dfSOhad Ben-Cohen /** 450086d0872SLoic Pallardy * rproc_rvdev_release() - release the existence of a rvdev 451086d0872SLoic Pallardy * 452086d0872SLoic Pallardy * @dev: the subdevice's dev 453086d0872SLoic Pallardy */ 454086d0872SLoic Pallardy static void rproc_rvdev_release(struct device *dev) 455086d0872SLoic Pallardy { 456086d0872SLoic Pallardy struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev); 457086d0872SLoic Pallardy 458086d0872SLoic Pallardy of_reserved_mem_device_release(dev); 459086d0872SLoic Pallardy 460086d0872SLoic Pallardy kfree(rvdev); 461086d0872SLoic Pallardy } 462086d0872SLoic Pallardy 463086d0872SLoic Pallardy /** 464fd2c15ecSOhad Ben-Cohen * rproc_handle_vdev() - handle a vdev fw resource 465400e64dfSOhad Ben-Cohen * @rproc: the remote processor 466400e64dfSOhad Ben-Cohen * @rsc: the vring resource descriptor 4672e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 468fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 469400e64dfSOhad Ben-Cohen * 4707a186941SOhad Ben-Cohen * This resource entry requests the host to statically register a virtio 4717a186941SOhad Ben-Cohen * device (vdev), and setup everything needed to support it. It contains 4727a186941SOhad Ben-Cohen * everything needed to make it possible: the virtio device id, virtio 4737a186941SOhad Ben-Cohen * device features, vrings information, virtio config space, etc... 474400e64dfSOhad Ben-Cohen * 4757a186941SOhad Ben-Cohen * Before registering the vdev, the vrings are allocated from non-cacheable 4767a186941SOhad Ben-Cohen * physically contiguous memory. Currently we only support two vrings per 4777a186941SOhad Ben-Cohen * remote processor (temporary limitation). We might also want to consider 4787a186941SOhad Ben-Cohen * doing the vring allocation only later when ->find_vqs() is invoked, and 4797a186941SOhad Ben-Cohen * then release them upon ->del_vqs(). 480400e64dfSOhad Ben-Cohen * 4817a186941SOhad Ben-Cohen * Note: @da is currently not really handled correctly: we dynamically 4827a186941SOhad Ben-Cohen * allocate it using the DMA API, ignoring requested hard coded addresses, 4837a186941SOhad Ben-Cohen * and we don't take care of any required IOMMU programming. This is all 4847a186941SOhad Ben-Cohen * going to be taken care of when the generic iommu-based DMA API will be 4857a186941SOhad Ben-Cohen * merged. Meanwhile, statically-addressed iommu-based firmware images should 4867a186941SOhad Ben-Cohen * use RSC_DEVMEM resource entries to map their required @da to the physical 4877a186941SOhad Ben-Cohen * address of their base CMA region (ouch, hacky!). 488400e64dfSOhad Ben-Cohen * 489400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 490400e64dfSOhad Ben-Cohen */ 491fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc, 492a2b950acSOhad Ben-Cohen int offset, int avail) 493400e64dfSOhad Ben-Cohen { 494b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 4957a186941SOhad Ben-Cohen struct rproc_vdev *rvdev; 4967a186941SOhad Ben-Cohen int i, ret; 497086d0872SLoic Pallardy char name[16]; 498fd2c15ecSOhad Ben-Cohen 499fd2c15ecSOhad Ben-Cohen /* make sure resource isn't truncated */ 500c8784657SGustavo A. R. Silva if (struct_size(rsc, vring, rsc->num_of_vrings) + rsc->config_len > 501c8784657SGustavo A. R. Silva avail) { 502b5ab5e24SOhad Ben-Cohen dev_err(dev, "vdev rsc is truncated\n"); 503fd2c15ecSOhad Ben-Cohen return -EINVAL; 504fd2c15ecSOhad Ben-Cohen } 505fd2c15ecSOhad Ben-Cohen 506fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 507fd2c15ecSOhad Ben-Cohen if (rsc->reserved[0] || rsc->reserved[1]) { 508fd2c15ecSOhad Ben-Cohen dev_err(dev, "vdev rsc has non zero reserved bytes\n"); 509fd2c15ecSOhad Ben-Cohen return -EINVAL; 510fd2c15ecSOhad Ben-Cohen } 511fd2c15ecSOhad Ben-Cohen 5129d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n", 513fd2c15ecSOhad Ben-Cohen rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); 514400e64dfSOhad Ben-Cohen 5157a186941SOhad Ben-Cohen /* we currently support only two vrings per rvdev */ 5167a186941SOhad Ben-Cohen if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { 517fd2c15ecSOhad Ben-Cohen dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); 518400e64dfSOhad Ben-Cohen return -EINVAL; 519400e64dfSOhad Ben-Cohen } 520400e64dfSOhad Ben-Cohen 521899585adSAnna, Suman rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL); 5227a186941SOhad Ben-Cohen if (!rvdev) 5237a186941SOhad Ben-Cohen return -ENOMEM; 5247a186941SOhad Ben-Cohen 525aab8d802SBjorn Andersson kref_init(&rvdev->refcount); 526aab8d802SBjorn Andersson 527f5bcb353SBjorn Andersson rvdev->id = rsc->id; 5287a186941SOhad Ben-Cohen rvdev->rproc = rproc; 529c6aed238SLoic Pallardy rvdev->index = rproc->nb_vdev++; 5307a186941SOhad Ben-Cohen 531086d0872SLoic Pallardy /* Initialise vdev subdevice */ 532086d0872SLoic Pallardy snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index); 533c774ad01SSuman Anna rvdev->dev.parent = &rproc->dev; 53472f64cabSClement Leger rvdev->dev.dma_pfn_offset = rproc->dev.parent->dma_pfn_offset; 535086d0872SLoic Pallardy rvdev->dev.release = rproc_rvdev_release; 536086d0872SLoic Pallardy dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name); 537086d0872SLoic Pallardy dev_set_drvdata(&rvdev->dev, rvdev); 538086d0872SLoic Pallardy 539086d0872SLoic Pallardy ret = device_register(&rvdev->dev); 540086d0872SLoic Pallardy if (ret) { 541086d0872SLoic Pallardy put_device(&rvdev->dev); 542086d0872SLoic Pallardy return ret; 543086d0872SLoic Pallardy } 544086d0872SLoic Pallardy /* Make device dma capable by inheriting from parent's capabilities */ 545086d0872SLoic Pallardy set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent)); 546086d0872SLoic Pallardy 547086d0872SLoic Pallardy ret = dma_coerce_mask_and_coherent(&rvdev->dev, 548086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent)); 549086d0872SLoic Pallardy if (ret) { 550086d0872SLoic Pallardy dev_warn(dev, 551086d0872SLoic Pallardy "Failed to set DMA mask %llx. Trying to continue... %x\n", 552086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent), ret); 553086d0872SLoic Pallardy } 554086d0872SLoic Pallardy 5556db20ea8SOhad Ben-Cohen /* parse the vrings */ 556fd2c15ecSOhad Ben-Cohen for (i = 0; i < rsc->num_of_vrings; i++) { 5576db20ea8SOhad Ben-Cohen ret = rproc_parse_vring(rvdev, rsc, i); 5587a186941SOhad Ben-Cohen if (ret) 5596db20ea8SOhad Ben-Cohen goto free_rvdev; 560fd2c15ecSOhad Ben-Cohen } 561fd2c15ecSOhad Ben-Cohen 562a2b950acSOhad Ben-Cohen /* remember the resource offset*/ 563a2b950acSOhad Ben-Cohen rvdev->rsc_offset = offset; 564400e64dfSOhad Ben-Cohen 565a863af5dSBjorn Andersson /* allocate the vring resources */ 566a863af5dSBjorn Andersson for (i = 0; i < rsc->num_of_vrings; i++) { 567a863af5dSBjorn Andersson ret = rproc_alloc_vring(rvdev, i); 568a863af5dSBjorn Andersson if (ret) 569a863af5dSBjorn Andersson goto unwind_vring_allocations; 570a863af5dSBjorn Andersson } 571a863af5dSBjorn Andersson 5727a186941SOhad Ben-Cohen list_add_tail(&rvdev->node, &rproc->rvdevs); 573400e64dfSOhad Ben-Cohen 5746f8b0373SAlex Elder rvdev->subdev.start = rproc_vdev_do_start; 5756f8b0373SAlex Elder rvdev->subdev.stop = rproc_vdev_do_stop; 5764902676fSBjorn Andersson 5774902676fSBjorn Andersson rproc_add_subdev(rproc, &rvdev->subdev); 578400e64dfSOhad Ben-Cohen 579400e64dfSOhad Ben-Cohen return 0; 5807a186941SOhad Ben-Cohen 581a863af5dSBjorn Andersson unwind_vring_allocations: 582a863af5dSBjorn Andersson for (i--; i >= 0; i--) 583a863af5dSBjorn Andersson rproc_free_vring(&rvdev->vring[i]); 5846db20ea8SOhad Ben-Cohen free_rvdev: 585086d0872SLoic Pallardy device_unregister(&rvdev->dev); 5867a186941SOhad Ben-Cohen return ret; 587400e64dfSOhad Ben-Cohen } 588400e64dfSOhad Ben-Cohen 589aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref) 590aab8d802SBjorn Andersson { 591aab8d802SBjorn Andersson struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount); 592a863af5dSBjorn Andersson struct rproc_vring *rvring; 593f5bcb353SBjorn Andersson struct rproc *rproc = rvdev->rproc; 594a863af5dSBjorn Andersson int id; 595a863af5dSBjorn Andersson 596a863af5dSBjorn Andersson for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) { 597a863af5dSBjorn Andersson rvring = &rvdev->vring[id]; 598a863af5dSBjorn Andersson rproc_free_vring(rvring); 599a863af5dSBjorn Andersson } 600aab8d802SBjorn Andersson 601f5bcb353SBjorn Andersson rproc_remove_subdev(rproc, &rvdev->subdev); 602aab8d802SBjorn Andersson list_del(&rvdev->node); 603086d0872SLoic Pallardy device_unregister(&rvdev->dev); 604aab8d802SBjorn Andersson } 605aab8d802SBjorn Andersson 606400e64dfSOhad Ben-Cohen /** 607400e64dfSOhad Ben-Cohen * rproc_handle_trace() - handle a shared trace buffer resource 608400e64dfSOhad Ben-Cohen * @rproc: the remote processor 609400e64dfSOhad Ben-Cohen * @rsc: the trace resource descriptor 6102e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 611fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 612400e64dfSOhad Ben-Cohen * 613400e64dfSOhad Ben-Cohen * In case the remote processor dumps trace logs into memory, 614400e64dfSOhad Ben-Cohen * export it via debugfs. 615400e64dfSOhad Ben-Cohen * 616400e64dfSOhad Ben-Cohen * Currently, the 'da' member of @rsc should contain the device address 617400e64dfSOhad Ben-Cohen * where the remote processor is dumping the traces. Later we could also 618400e64dfSOhad Ben-Cohen * support dynamically allocating this address using the generic 619400e64dfSOhad Ben-Cohen * DMA API (but currently there isn't a use case for that). 620400e64dfSOhad Ben-Cohen * 621400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 622400e64dfSOhad Ben-Cohen */ 623fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc, 624a2b950acSOhad Ben-Cohen int offset, int avail) 625400e64dfSOhad Ben-Cohen { 626a987e6b9SLoic Pallardy struct rproc_debug_trace *trace; 627b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 628400e64dfSOhad Ben-Cohen char name[15]; 629400e64dfSOhad Ben-Cohen 630fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 631b5ab5e24SOhad Ben-Cohen dev_err(dev, "trace rsc is truncated\n"); 632fd2c15ecSOhad Ben-Cohen return -EINVAL; 633fd2c15ecSOhad Ben-Cohen } 634fd2c15ecSOhad Ben-Cohen 635fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 636fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 637fd2c15ecSOhad Ben-Cohen dev_err(dev, "trace rsc has non zero reserved bytes\n"); 638fd2c15ecSOhad Ben-Cohen return -EINVAL; 639fd2c15ecSOhad Ben-Cohen } 640fd2c15ecSOhad Ben-Cohen 641400e64dfSOhad Ben-Cohen trace = kzalloc(sizeof(*trace), GFP_KERNEL); 642172e6ab1SSuman Anna if (!trace) 643400e64dfSOhad Ben-Cohen return -ENOMEM; 644400e64dfSOhad Ben-Cohen 645400e64dfSOhad Ben-Cohen /* set the trace buffer dma properties */ 646a987e6b9SLoic Pallardy trace->trace_mem.len = rsc->len; 647a987e6b9SLoic Pallardy trace->trace_mem.da = rsc->da; 648a987e6b9SLoic Pallardy 649a987e6b9SLoic Pallardy /* set pointer on rproc device */ 650a987e6b9SLoic Pallardy trace->rproc = rproc; 651400e64dfSOhad Ben-Cohen 652400e64dfSOhad Ben-Cohen /* make sure snprintf always null terminates, even if truncating */ 653400e64dfSOhad Ben-Cohen snprintf(name, sizeof(name), "trace%d", rproc->num_traces); 654400e64dfSOhad Ben-Cohen 655400e64dfSOhad Ben-Cohen /* create the debugfs entry */ 656a987e6b9SLoic Pallardy trace->tfile = rproc_create_trace_file(name, rproc, trace); 657a987e6b9SLoic Pallardy if (!trace->tfile) { 658400e64dfSOhad Ben-Cohen kfree(trace); 659400e64dfSOhad Ben-Cohen return -EINVAL; 660400e64dfSOhad Ben-Cohen } 661400e64dfSOhad Ben-Cohen 662400e64dfSOhad Ben-Cohen list_add_tail(&trace->node, &rproc->traces); 663400e64dfSOhad Ben-Cohen 664400e64dfSOhad Ben-Cohen rproc->num_traces++; 665400e64dfSOhad Ben-Cohen 666a987e6b9SLoic Pallardy dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n", 667a987e6b9SLoic Pallardy name, rsc->da, rsc->len); 668400e64dfSOhad Ben-Cohen 669400e64dfSOhad Ben-Cohen return 0; 670400e64dfSOhad Ben-Cohen } 671400e64dfSOhad Ben-Cohen 672400e64dfSOhad Ben-Cohen /** 673400e64dfSOhad Ben-Cohen * rproc_handle_devmem() - handle devmem resource entry 674400e64dfSOhad Ben-Cohen * @rproc: remote processor handle 675400e64dfSOhad Ben-Cohen * @rsc: the devmem resource entry 6762e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 677fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 678400e64dfSOhad Ben-Cohen * 679400e64dfSOhad Ben-Cohen * Remote processors commonly need to access certain on-chip peripherals. 680400e64dfSOhad Ben-Cohen * 681400e64dfSOhad Ben-Cohen * Some of these remote processors access memory via an iommu device, 682400e64dfSOhad Ben-Cohen * and might require us to configure their iommu before they can access 683400e64dfSOhad Ben-Cohen * the on-chip peripherals they need. 684400e64dfSOhad Ben-Cohen * 685400e64dfSOhad Ben-Cohen * This resource entry is a request to map such a peripheral device. 686400e64dfSOhad Ben-Cohen * 687400e64dfSOhad Ben-Cohen * These devmem entries will contain the physical address of the device in 688400e64dfSOhad Ben-Cohen * the 'pa' member. If a specific device address is expected, then 'da' will 689400e64dfSOhad Ben-Cohen * contain it (currently this is the only use case supported). 'len' will 690400e64dfSOhad Ben-Cohen * contain the size of the physical region we need to map. 691400e64dfSOhad Ben-Cohen * 692400e64dfSOhad Ben-Cohen * Currently we just "trust" those devmem entries to contain valid physical 693400e64dfSOhad Ben-Cohen * addresses, but this is going to change: we want the implementations to 694400e64dfSOhad Ben-Cohen * tell us ranges of physical addresses the firmware is allowed to request, 695400e64dfSOhad Ben-Cohen * and not allow firmwares to request access to physical addresses that 696400e64dfSOhad Ben-Cohen * are outside those ranges. 697400e64dfSOhad Ben-Cohen */ 698fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc, 699a2b950acSOhad Ben-Cohen int offset, int avail) 700400e64dfSOhad Ben-Cohen { 701400e64dfSOhad Ben-Cohen struct rproc_mem_entry *mapping; 702b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 703400e64dfSOhad Ben-Cohen int ret; 704400e64dfSOhad Ben-Cohen 705400e64dfSOhad Ben-Cohen /* no point in handling this resource without a valid iommu domain */ 706400e64dfSOhad Ben-Cohen if (!rproc->domain) 707400e64dfSOhad Ben-Cohen return -EINVAL; 708400e64dfSOhad Ben-Cohen 709fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 710b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc is truncated\n"); 711fd2c15ecSOhad Ben-Cohen return -EINVAL; 712fd2c15ecSOhad Ben-Cohen } 713fd2c15ecSOhad Ben-Cohen 714fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 715fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 716b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc has non zero reserved bytes\n"); 717fd2c15ecSOhad Ben-Cohen return -EINVAL; 718fd2c15ecSOhad Ben-Cohen } 719fd2c15ecSOhad Ben-Cohen 720400e64dfSOhad Ben-Cohen mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 721172e6ab1SSuman Anna if (!mapping) 722400e64dfSOhad Ben-Cohen return -ENOMEM; 723400e64dfSOhad Ben-Cohen 724400e64dfSOhad Ben-Cohen ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); 725400e64dfSOhad Ben-Cohen if (ret) { 726b5ab5e24SOhad Ben-Cohen dev_err(dev, "failed to map devmem: %d\n", ret); 727400e64dfSOhad Ben-Cohen goto out; 728400e64dfSOhad Ben-Cohen } 729400e64dfSOhad Ben-Cohen 730400e64dfSOhad Ben-Cohen /* 731400e64dfSOhad Ben-Cohen * We'll need this info later when we'll want to unmap everything 732400e64dfSOhad Ben-Cohen * (e.g. on shutdown). 733400e64dfSOhad Ben-Cohen * 734400e64dfSOhad Ben-Cohen * We can't trust the remote processor not to change the resource 735400e64dfSOhad Ben-Cohen * table, so we must maintain this info independently. 736400e64dfSOhad Ben-Cohen */ 737400e64dfSOhad Ben-Cohen mapping->da = rsc->da; 738400e64dfSOhad Ben-Cohen mapping->len = rsc->len; 739400e64dfSOhad Ben-Cohen list_add_tail(&mapping->node, &rproc->mappings); 740400e64dfSOhad Ben-Cohen 741b5ab5e24SOhad Ben-Cohen dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", 742400e64dfSOhad Ben-Cohen rsc->pa, rsc->da, rsc->len); 743400e64dfSOhad Ben-Cohen 744400e64dfSOhad Ben-Cohen return 0; 745400e64dfSOhad Ben-Cohen 746400e64dfSOhad Ben-Cohen out: 747400e64dfSOhad Ben-Cohen kfree(mapping); 748400e64dfSOhad Ben-Cohen return ret; 749400e64dfSOhad Ben-Cohen } 750400e64dfSOhad Ben-Cohen 751400e64dfSOhad Ben-Cohen /** 752d7c51706SLoic Pallardy * rproc_alloc_carveout() - allocated specified carveout 753d7c51706SLoic Pallardy * @rproc: rproc handle 754d7c51706SLoic Pallardy * @mem: the memory entry to allocate 755d7c51706SLoic Pallardy * 756d7c51706SLoic Pallardy * This function allocate specified memory entry @mem using 757d7c51706SLoic Pallardy * dma_alloc_coherent() as default allocator 758d7c51706SLoic Pallardy */ 759d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc, 760d7c51706SLoic Pallardy struct rproc_mem_entry *mem) 761d7c51706SLoic Pallardy { 762d7c51706SLoic Pallardy struct rproc_mem_entry *mapping = NULL; 763d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 764d7c51706SLoic Pallardy dma_addr_t dma; 765d7c51706SLoic Pallardy void *va; 766d7c51706SLoic Pallardy int ret; 767d7c51706SLoic Pallardy 768d7c51706SLoic Pallardy va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL); 769d7c51706SLoic Pallardy if (!va) { 770d7c51706SLoic Pallardy dev_err(dev->parent, 771096ee786SClement Leger "failed to allocate dma memory: len 0x%zx\n", 772096ee786SClement Leger mem->len); 773d7c51706SLoic Pallardy return -ENOMEM; 774d7c51706SLoic Pallardy } 775d7c51706SLoic Pallardy 776096ee786SClement Leger dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n", 777d7c51706SLoic Pallardy va, &dma, mem->len); 778d7c51706SLoic Pallardy 77960f849a5SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) { 78060f849a5SLoic Pallardy /* 78160f849a5SLoic Pallardy * Check requested da is equal to dma address 78260f849a5SLoic Pallardy * and print a warn message in case of missalignment. 78360f849a5SLoic Pallardy * Don't stop rproc_start sequence as coprocessor may 78460f849a5SLoic Pallardy * build pa to da translation on its side. 78560f849a5SLoic Pallardy */ 78660f849a5SLoic Pallardy if (mem->da != (u32)dma) 78760f849a5SLoic Pallardy dev_warn(dev->parent, 78860f849a5SLoic Pallardy "Allocated carveout doesn't fit device address request\n"); 78960f849a5SLoic Pallardy } 79060f849a5SLoic Pallardy 791d7c51706SLoic Pallardy /* 792d7c51706SLoic Pallardy * Ok, this is non-standard. 793d7c51706SLoic Pallardy * 794d7c51706SLoic Pallardy * Sometimes we can't rely on the generic iommu-based DMA API 795d7c51706SLoic Pallardy * to dynamically allocate the device address and then set the IOMMU 796d7c51706SLoic Pallardy * tables accordingly, because some remote processors might 797d7c51706SLoic Pallardy * _require_ us to use hard coded device addresses that their 798d7c51706SLoic Pallardy * firmware was compiled with. 799d7c51706SLoic Pallardy * 800d7c51706SLoic Pallardy * In this case, we must use the IOMMU API directly and map 801d7c51706SLoic Pallardy * the memory to the device address as expected by the remote 802d7c51706SLoic Pallardy * processor. 803d7c51706SLoic Pallardy * 804d7c51706SLoic Pallardy * Obviously such remote processor devices should not be configured 805d7c51706SLoic Pallardy * to use the iommu-based DMA API: we expect 'dma' to contain the 806d7c51706SLoic Pallardy * physical address in this case. 807d7c51706SLoic Pallardy */ 80860f849a5SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) { 809d7c51706SLoic Pallardy mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 810d7c51706SLoic Pallardy if (!mapping) { 811d7c51706SLoic Pallardy ret = -ENOMEM; 812d7c51706SLoic Pallardy goto dma_free; 813d7c51706SLoic Pallardy } 814d7c51706SLoic Pallardy 815d7c51706SLoic Pallardy ret = iommu_map(rproc->domain, mem->da, dma, mem->len, 816d7c51706SLoic Pallardy mem->flags); 817d7c51706SLoic Pallardy if (ret) { 818d7c51706SLoic Pallardy dev_err(dev, "iommu_map failed: %d\n", ret); 819d7c51706SLoic Pallardy goto free_mapping; 820d7c51706SLoic Pallardy } 821d7c51706SLoic Pallardy 822d7c51706SLoic Pallardy /* 823d7c51706SLoic Pallardy * We'll need this info later when we'll want to unmap 824d7c51706SLoic Pallardy * everything (e.g. on shutdown). 825d7c51706SLoic Pallardy * 826d7c51706SLoic Pallardy * We can't trust the remote processor not to change the 827d7c51706SLoic Pallardy * resource table, so we must maintain this info independently. 828d7c51706SLoic Pallardy */ 829d7c51706SLoic Pallardy mapping->da = mem->da; 830d7c51706SLoic Pallardy mapping->len = mem->len; 831d7c51706SLoic Pallardy list_add_tail(&mapping->node, &rproc->mappings); 832d7c51706SLoic Pallardy 833d7c51706SLoic Pallardy dev_dbg(dev, "carveout mapped 0x%x to %pad\n", 834d7c51706SLoic Pallardy mem->da, &dma); 83560f849a5SLoic Pallardy } 83660f849a5SLoic Pallardy 83760f849a5SLoic Pallardy if (mem->da == FW_RSC_ADDR_ANY) { 838b36de8cfSLoic Pallardy /* Update device address as undefined by requester */ 839b36de8cfSLoic Pallardy if ((u64)dma & HIGH_BITS_MASK) 840b36de8cfSLoic Pallardy dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n"); 841b36de8cfSLoic Pallardy 842d7c51706SLoic Pallardy mem->da = (u32)dma; 843d7c51706SLoic Pallardy } 844d7c51706SLoic Pallardy 84580137b40SLoic Pallardy mem->dma = dma; 846d7c51706SLoic Pallardy mem->va = va; 847d7c51706SLoic Pallardy 848d7c51706SLoic Pallardy return 0; 849d7c51706SLoic Pallardy 850d7c51706SLoic Pallardy free_mapping: 851d7c51706SLoic Pallardy kfree(mapping); 852d7c51706SLoic Pallardy dma_free: 853d7c51706SLoic Pallardy dma_free_coherent(dev->parent, mem->len, va, dma); 854d7c51706SLoic Pallardy return ret; 855d7c51706SLoic Pallardy } 856d7c51706SLoic Pallardy 857d7c51706SLoic Pallardy /** 858f2e74abfSLoic Pallardy * rproc_release_carveout() - release acquired carveout 859f2e74abfSLoic Pallardy * @rproc: rproc handle 860f2e74abfSLoic Pallardy * @mem: the memory entry to release 861f2e74abfSLoic Pallardy * 862f2e74abfSLoic Pallardy * This function releases specified memory entry @mem allocated via 863d7c51706SLoic Pallardy * rproc_alloc_carveout() function by @rproc. 864f2e74abfSLoic Pallardy */ 865f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc, 866f2e74abfSLoic Pallardy struct rproc_mem_entry *mem) 867f2e74abfSLoic Pallardy { 868f2e74abfSLoic Pallardy struct device *dev = &rproc->dev; 869f2e74abfSLoic Pallardy 870f2e74abfSLoic Pallardy /* clean up carveout allocations */ 871f2e74abfSLoic Pallardy dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma); 872f2e74abfSLoic Pallardy return 0; 873f2e74abfSLoic Pallardy } 874f2e74abfSLoic Pallardy 875f2e74abfSLoic Pallardy /** 876400e64dfSOhad Ben-Cohen * rproc_handle_carveout() - handle phys contig memory allocation requests 877400e64dfSOhad Ben-Cohen * @rproc: rproc handle 878400e64dfSOhad Ben-Cohen * @rsc: the resource entry 8792e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 880fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for image validation) 881400e64dfSOhad Ben-Cohen * 882400e64dfSOhad Ben-Cohen * This function will handle firmware requests for allocation of physically 883400e64dfSOhad Ben-Cohen * contiguous memory regions. 884400e64dfSOhad Ben-Cohen * 885400e64dfSOhad Ben-Cohen * These request entries should come first in the firmware's resource table, 886400e64dfSOhad Ben-Cohen * as other firmware entries might request placing other data objects inside 887400e64dfSOhad Ben-Cohen * these memory regions (e.g. data/code segments, trace resource entries, ...). 888400e64dfSOhad Ben-Cohen * 889400e64dfSOhad Ben-Cohen * Allocating memory this way helps utilizing the reserved physical memory 890400e64dfSOhad Ben-Cohen * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries 891400e64dfSOhad Ben-Cohen * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB 892400e64dfSOhad Ben-Cohen * pressure is important; it may have a substantial impact on performance. 893400e64dfSOhad Ben-Cohen */ 894fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc, 895a2b950acSOhad Ben-Cohen struct fw_rsc_carveout *rsc, 896a2b950acSOhad Ben-Cohen int offset, int avail) 897400e64dfSOhad Ben-Cohen { 898d7c51706SLoic Pallardy struct rproc_mem_entry *carveout; 899b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 900400e64dfSOhad Ben-Cohen 901fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 902b5ab5e24SOhad Ben-Cohen dev_err(dev, "carveout rsc is truncated\n"); 903fd2c15ecSOhad Ben-Cohen return -EINVAL; 904fd2c15ecSOhad Ben-Cohen } 905fd2c15ecSOhad Ben-Cohen 906fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 907fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 908fd2c15ecSOhad Ben-Cohen dev_err(dev, "carveout rsc has non zero reserved bytes\n"); 909fd2c15ecSOhad Ben-Cohen return -EINVAL; 910fd2c15ecSOhad Ben-Cohen } 911fd2c15ecSOhad Ben-Cohen 9129d7814a9SAnna, Suman dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n", 91335386166SLee Jones rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); 914fd2c15ecSOhad Ben-Cohen 915ffa5f9c8SLoic Pallardy /* 916ffa5f9c8SLoic Pallardy * Check carveout rsc already part of a registered carveout, 917ffa5f9c8SLoic Pallardy * Search by name, then check the da and length 918ffa5f9c8SLoic Pallardy */ 919ffa5f9c8SLoic Pallardy carveout = rproc_find_carveout_by_name(rproc, rsc->name); 920ffa5f9c8SLoic Pallardy 921ffa5f9c8SLoic Pallardy if (carveout) { 922ffa5f9c8SLoic Pallardy if (carveout->rsc_offset != FW_RSC_ADDR_ANY) { 923ffa5f9c8SLoic Pallardy dev_err(dev, 924ffa5f9c8SLoic Pallardy "Carveout already associated to resource table\n"); 925ffa5f9c8SLoic Pallardy return -ENOMEM; 926ffa5f9c8SLoic Pallardy } 927ffa5f9c8SLoic Pallardy 928ffa5f9c8SLoic Pallardy if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len)) 929ffa5f9c8SLoic Pallardy return -ENOMEM; 930ffa5f9c8SLoic Pallardy 931ffa5f9c8SLoic Pallardy /* Update memory carveout with resource table info */ 932ffa5f9c8SLoic Pallardy carveout->rsc_offset = offset; 933ffa5f9c8SLoic Pallardy carveout->flags = rsc->flags; 934ffa5f9c8SLoic Pallardy 935ffa5f9c8SLoic Pallardy return 0; 936ffa5f9c8SLoic Pallardy } 937ffa5f9c8SLoic Pallardy 938d7c51706SLoic Pallardy /* Register carveout in in list */ 93999cf0361SBen Dooks (Codethink) carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da, 940d7c51706SLoic Pallardy rproc_alloc_carveout, 941d7c51706SLoic Pallardy rproc_release_carveout, rsc->name); 942d7c51706SLoic Pallardy if (!carveout) { 943d7c51706SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 94472029c90SLoic Pallardy return -ENOMEM; 945400e64dfSOhad Ben-Cohen } 946400e64dfSOhad Ben-Cohen 947d7c51706SLoic Pallardy carveout->flags = rsc->flags; 948d7c51706SLoic Pallardy carveout->rsc_offset = offset; 94915c0b025SLoic Pallardy rproc_add_carveout(rproc, carveout); 950400e64dfSOhad Ben-Cohen 951400e64dfSOhad Ben-Cohen return 0; 952400e64dfSOhad Ben-Cohen } 953400e64dfSOhad Ben-Cohen 95472029c90SLoic Pallardy /** 95515c0b025SLoic Pallardy * rproc_add_carveout() - register an allocated carveout region 95615c0b025SLoic Pallardy * @rproc: rproc handle 95715c0b025SLoic Pallardy * @mem: memory entry to register 95815c0b025SLoic Pallardy * 95915c0b025SLoic Pallardy * This function registers specified memory entry in @rproc carveouts list. 96015c0b025SLoic Pallardy * Specified carveout should have been allocated before registering. 96115c0b025SLoic Pallardy */ 96215c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) 96315c0b025SLoic Pallardy { 96415c0b025SLoic Pallardy list_add_tail(&mem->node, &rproc->carveouts); 96515c0b025SLoic Pallardy } 96615c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout); 96715c0b025SLoic Pallardy 96815c0b025SLoic Pallardy /** 96972029c90SLoic Pallardy * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct 97072029c90SLoic Pallardy * @dev: pointer on device struct 97172029c90SLoic Pallardy * @va: virtual address 97272029c90SLoic Pallardy * @dma: dma address 97372029c90SLoic Pallardy * @len: memory carveout length 97472029c90SLoic Pallardy * @da: device address 975a9f6fe0dSLoic Pallardy * @alloc: memory carveout allocation function 976a9f6fe0dSLoic Pallardy * @release: memory carveout release function 97772029c90SLoic Pallardy * @name: carveout name 97872029c90SLoic Pallardy * 97972029c90SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 98072029c90SLoic Pallardy * provided by client. 98172029c90SLoic Pallardy */ 9827e05c8deSLee Jones __printf(8, 9) 98372029c90SLoic Pallardy struct rproc_mem_entry * 98472029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev, 985096ee786SClement Leger void *va, dma_addr_t dma, size_t len, u32 da, 986d7c51706SLoic Pallardy int (*alloc)(struct rproc *, struct rproc_mem_entry *), 98772029c90SLoic Pallardy int (*release)(struct rproc *, struct rproc_mem_entry *), 98872029c90SLoic Pallardy const char *name, ...) 98972029c90SLoic Pallardy { 99072029c90SLoic Pallardy struct rproc_mem_entry *mem; 99172029c90SLoic Pallardy va_list args; 99272029c90SLoic Pallardy 99372029c90SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 99472029c90SLoic Pallardy if (!mem) 99572029c90SLoic Pallardy return mem; 99672029c90SLoic Pallardy 99772029c90SLoic Pallardy mem->va = va; 99872029c90SLoic Pallardy mem->dma = dma; 99972029c90SLoic Pallardy mem->da = da; 100072029c90SLoic Pallardy mem->len = len; 1001d7c51706SLoic Pallardy mem->alloc = alloc; 100272029c90SLoic Pallardy mem->release = release; 1003d7c51706SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 10041429cca1SLoic Pallardy mem->of_resm_idx = -1; 100572029c90SLoic Pallardy 100672029c90SLoic Pallardy va_start(args, name); 100772029c90SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 100872029c90SLoic Pallardy va_end(args); 100972029c90SLoic Pallardy 101072029c90SLoic Pallardy return mem; 101172029c90SLoic Pallardy } 101272029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init); 101372029c90SLoic Pallardy 101472029c90SLoic Pallardy /** 10151429cca1SLoic Pallardy * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct 10161429cca1SLoic Pallardy * from a reserved memory phandle 10171429cca1SLoic Pallardy * @dev: pointer on device struct 10181429cca1SLoic Pallardy * @of_resm_idx: reserved memory phandle index in "memory-region" 10191429cca1SLoic Pallardy * @len: memory carveout length 10201429cca1SLoic Pallardy * @da: device address 10211429cca1SLoic Pallardy * @name: carveout name 10221429cca1SLoic Pallardy * 10231429cca1SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 10241429cca1SLoic Pallardy * provided by client. 10251429cca1SLoic Pallardy */ 10267e05c8deSLee Jones __printf(5, 6) 10271429cca1SLoic Pallardy struct rproc_mem_entry * 1028096ee786SClement Leger rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len, 10291429cca1SLoic Pallardy u32 da, const char *name, ...) 10301429cca1SLoic Pallardy { 10311429cca1SLoic Pallardy struct rproc_mem_entry *mem; 10321429cca1SLoic Pallardy va_list args; 10331429cca1SLoic Pallardy 10341429cca1SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 10351429cca1SLoic Pallardy if (!mem) 10361429cca1SLoic Pallardy return mem; 10371429cca1SLoic Pallardy 10381429cca1SLoic Pallardy mem->da = da; 10391429cca1SLoic Pallardy mem->len = len; 10401429cca1SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 10411429cca1SLoic Pallardy mem->of_resm_idx = of_resm_idx; 10421429cca1SLoic Pallardy 10431429cca1SLoic Pallardy va_start(args, name); 10441429cca1SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 10451429cca1SLoic Pallardy va_end(args); 10461429cca1SLoic Pallardy 10471429cca1SLoic Pallardy return mem; 10481429cca1SLoic Pallardy } 10491429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init); 10501429cca1SLoic Pallardy 10512e7d4c2cSArnaud Pouliquen /* 1052e12bc14bSOhad Ben-Cohen * A lookup table for resource handlers. The indices are defined in 1053e12bc14bSOhad Ben-Cohen * enum fw_resource_type. 1054e12bc14bSOhad Ben-Cohen */ 1055232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { 1056fd2c15ecSOhad Ben-Cohen [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout, 1057fd2c15ecSOhad Ben-Cohen [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem, 1058fd2c15ecSOhad Ben-Cohen [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace, 1059232fcdbbSSjur Brændeland [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev, 1060232fcdbbSSjur Brændeland }; 1061232fcdbbSSjur Brændeland 1062400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */ 1063a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc, 1064232fcdbbSSjur Brændeland rproc_handle_resource_t handlers[RSC_LAST]) 1065400e64dfSOhad Ben-Cohen { 1066b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1067e12bc14bSOhad Ben-Cohen rproc_handle_resource_t handler; 1068fd2c15ecSOhad Ben-Cohen int ret = 0, i; 1069400e64dfSOhad Ben-Cohen 1070d4bb86f2SBjorn Andersson if (!rproc->table_ptr) 1071d4bb86f2SBjorn Andersson return 0; 1072d4bb86f2SBjorn Andersson 1073a2b950acSOhad Ben-Cohen for (i = 0; i < rproc->table_ptr->num; i++) { 1074a2b950acSOhad Ben-Cohen int offset = rproc->table_ptr->offset[i]; 1075a2b950acSOhad Ben-Cohen struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; 1076a4b24c75SBjorn Andersson int avail = rproc->table_sz - offset - sizeof(*hdr); 1077fd2c15ecSOhad Ben-Cohen void *rsc = (void *)hdr + sizeof(*hdr); 1078400e64dfSOhad Ben-Cohen 1079fd2c15ecSOhad Ben-Cohen /* make sure table isn't truncated */ 1080fd2c15ecSOhad Ben-Cohen if (avail < 0) { 1081fd2c15ecSOhad Ben-Cohen dev_err(dev, "rsc table is truncated\n"); 1082fd2c15ecSOhad Ben-Cohen return -EINVAL; 1083fd2c15ecSOhad Ben-Cohen } 1084fd2c15ecSOhad Ben-Cohen 1085fd2c15ecSOhad Ben-Cohen dev_dbg(dev, "rsc: type %d\n", hdr->type); 1086fd2c15ecSOhad Ben-Cohen 1087b1a17513SClement Leger if (hdr->type >= RSC_VENDOR_START && 1088b1a17513SClement Leger hdr->type <= RSC_VENDOR_END) { 1089b1a17513SClement Leger ret = rproc_handle_rsc(rproc, hdr->type, rsc, 1090b1a17513SClement Leger offset + sizeof(*hdr), avail); 1091b1a17513SClement Leger if (ret == RSC_HANDLED) 1092b1a17513SClement Leger continue; 1093b1a17513SClement Leger else if (ret < 0) 1094b1a17513SClement Leger break; 1095b1a17513SClement Leger 1096b1a17513SClement Leger dev_warn(dev, "unsupported vendor resource %d\n", 1097b1a17513SClement Leger hdr->type); 1098b1a17513SClement Leger continue; 1099b1a17513SClement Leger } 1100b1a17513SClement Leger 1101fd2c15ecSOhad Ben-Cohen if (hdr->type >= RSC_LAST) { 1102fd2c15ecSOhad Ben-Cohen dev_warn(dev, "unsupported resource %d\n", hdr->type); 1103e12bc14bSOhad Ben-Cohen continue; 1104400e64dfSOhad Ben-Cohen } 1105400e64dfSOhad Ben-Cohen 1106232fcdbbSSjur Brændeland handler = handlers[hdr->type]; 1107e12bc14bSOhad Ben-Cohen if (!handler) 1108e12bc14bSOhad Ben-Cohen continue; 1109e12bc14bSOhad Ben-Cohen 1110a2b950acSOhad Ben-Cohen ret = handler(rproc, rsc, offset + sizeof(*hdr), avail); 11117a186941SOhad Ben-Cohen if (ret) 1112400e64dfSOhad Ben-Cohen break; 1113400e64dfSOhad Ben-Cohen } 1114400e64dfSOhad Ben-Cohen 1115400e64dfSOhad Ben-Cohen return ret; 1116400e64dfSOhad Ben-Cohen } 1117400e64dfSOhad Ben-Cohen 1118c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc) 1119c455daa4SBjorn Andersson { 1120c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1121c455daa4SBjorn Andersson int ret; 1122c455daa4SBjorn Andersson 1123c455daa4SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1124c455daa4SBjorn Andersson if (subdev->prepare) { 1125c455daa4SBjorn Andersson ret = subdev->prepare(subdev); 1126c455daa4SBjorn Andersson if (ret) 1127c455daa4SBjorn Andersson goto unroll_preparation; 1128c455daa4SBjorn Andersson } 1129c455daa4SBjorn Andersson } 1130c455daa4SBjorn Andersson 1131c455daa4SBjorn Andersson return 0; 1132c455daa4SBjorn Andersson 1133c455daa4SBjorn Andersson unroll_preparation: 1134c455daa4SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1135c455daa4SBjorn Andersson if (subdev->unprepare) 1136c455daa4SBjorn Andersson subdev->unprepare(subdev); 1137c455daa4SBjorn Andersson } 1138c455daa4SBjorn Andersson 1139c455daa4SBjorn Andersson return ret; 1140c455daa4SBjorn Andersson } 1141c455daa4SBjorn Andersson 1142618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc) 11437bdc9650SBjorn Andersson { 11447bdc9650SBjorn Andersson struct rproc_subdev *subdev; 11457bdc9650SBjorn Andersson int ret; 11467bdc9650SBjorn Andersson 11477bdc9650SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1148be37b1e0SBjorn Andersson if (subdev->start) { 1149618fcff3SBjorn Andersson ret = subdev->start(subdev); 11507bdc9650SBjorn Andersson if (ret) 11517bdc9650SBjorn Andersson goto unroll_registration; 11527bdc9650SBjorn Andersson } 1153be37b1e0SBjorn Andersson } 11547bdc9650SBjorn Andersson 11557bdc9650SBjorn Andersson return 0; 11567bdc9650SBjorn Andersson 11577bdc9650SBjorn Andersson unroll_registration: 1158be37b1e0SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1159be37b1e0SBjorn Andersson if (subdev->stop) 1160618fcff3SBjorn Andersson subdev->stop(subdev, true); 1161be37b1e0SBjorn Andersson } 11627bdc9650SBjorn Andersson 11637bdc9650SBjorn Andersson return ret; 11647bdc9650SBjorn Andersson } 11657bdc9650SBjorn Andersson 1166618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed) 11677bdc9650SBjorn Andersson { 11687bdc9650SBjorn Andersson struct rproc_subdev *subdev; 11697bdc9650SBjorn Andersson 1170be37b1e0SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1171be37b1e0SBjorn Andersson if (subdev->stop) 1172618fcff3SBjorn Andersson subdev->stop(subdev, crashed); 11737bdc9650SBjorn Andersson } 1174be37b1e0SBjorn Andersson } 11757bdc9650SBjorn Andersson 1176c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc) 1177c455daa4SBjorn Andersson { 1178c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1179c455daa4SBjorn Andersson 1180c455daa4SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1181c455daa4SBjorn Andersson if (subdev->unprepare) 1182c455daa4SBjorn Andersson subdev->unprepare(subdev); 1183c455daa4SBjorn Andersson } 1184c455daa4SBjorn Andersson } 1185c455daa4SBjorn Andersson 1186400e64dfSOhad Ben-Cohen /** 1187d7c51706SLoic Pallardy * rproc_alloc_registered_carveouts() - allocate all carveouts registered 1188d7c51706SLoic Pallardy * in the list 1189d7c51706SLoic Pallardy * @rproc: the remote processor handle 1190d7c51706SLoic Pallardy * 1191d7c51706SLoic Pallardy * This function parses registered carveout list, performs allocation 1192d7c51706SLoic Pallardy * if alloc() ops registered and updates resource table information 1193d7c51706SLoic Pallardy * if rsc_offset set. 1194d7c51706SLoic Pallardy * 1195d7c51706SLoic Pallardy * Return: 0 on success 1196d7c51706SLoic Pallardy */ 1197d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc) 1198d7c51706SLoic Pallardy { 1199d7c51706SLoic Pallardy struct rproc_mem_entry *entry, *tmp; 1200d7c51706SLoic Pallardy struct fw_rsc_carveout *rsc; 1201d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 1202b36de8cfSLoic Pallardy u64 pa; 1203d7c51706SLoic Pallardy int ret; 1204d7c51706SLoic Pallardy 1205d7c51706SLoic Pallardy list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1206d7c51706SLoic Pallardy if (entry->alloc) { 1207d7c51706SLoic Pallardy ret = entry->alloc(rproc, entry); 1208d7c51706SLoic Pallardy if (ret) { 1209d7c51706SLoic Pallardy dev_err(dev, "Unable to allocate carveout %s: %d\n", 1210d7c51706SLoic Pallardy entry->name, ret); 1211d7c51706SLoic Pallardy return -ENOMEM; 1212d7c51706SLoic Pallardy } 1213d7c51706SLoic Pallardy } 1214d7c51706SLoic Pallardy 1215d7c51706SLoic Pallardy if (entry->rsc_offset != FW_RSC_ADDR_ANY) { 1216d7c51706SLoic Pallardy /* update resource table */ 1217d7c51706SLoic Pallardy rsc = (void *)rproc->table_ptr + entry->rsc_offset; 1218d7c51706SLoic Pallardy 1219d7c51706SLoic Pallardy /* 1220d7c51706SLoic Pallardy * Some remote processors might need to know the pa 1221d7c51706SLoic Pallardy * even though they are behind an IOMMU. E.g., OMAP4's 1222d7c51706SLoic Pallardy * remote M3 processor needs this so it can control 1223d7c51706SLoic Pallardy * on-chip hardware accelerators that are not behind 1224d7c51706SLoic Pallardy * the IOMMU, and therefor must know the pa. 1225d7c51706SLoic Pallardy * 1226d7c51706SLoic Pallardy * Generally we don't want to expose physical addresses 1227d7c51706SLoic Pallardy * if we don't have to (remote processors are generally 1228d7c51706SLoic Pallardy * _not_ trusted), so we might want to do this only for 1229d7c51706SLoic Pallardy * remote processor that _must_ have this (e.g. OMAP4's 1230d7c51706SLoic Pallardy * dual M3 subsystem). 1231d7c51706SLoic Pallardy * 1232d7c51706SLoic Pallardy * Non-IOMMU processors might also want to have this info. 1233d7c51706SLoic Pallardy * In this case, the device address and the physical address 1234d7c51706SLoic Pallardy * are the same. 1235d7c51706SLoic Pallardy */ 1236ffa5f9c8SLoic Pallardy 1237ffa5f9c8SLoic Pallardy /* Use va if defined else dma to generate pa */ 1238d7c51706SLoic Pallardy if (entry->va) 1239b36de8cfSLoic Pallardy pa = (u64)rproc_va_to_pa(entry->va); 1240ffa5f9c8SLoic Pallardy else 1241b36de8cfSLoic Pallardy pa = (u64)entry->dma; 1242ffa5f9c8SLoic Pallardy 1243b36de8cfSLoic Pallardy if (((u64)pa) & HIGH_BITS_MASK) 1244b36de8cfSLoic Pallardy dev_warn(dev, 1245b36de8cfSLoic Pallardy "Physical address cast in 32bit to fit resource table format\n"); 1246b36de8cfSLoic Pallardy 1247b36de8cfSLoic Pallardy rsc->pa = (u32)pa; 1248ffa5f9c8SLoic Pallardy rsc->da = entry->da; 1249ffa5f9c8SLoic Pallardy rsc->len = entry->len; 1250d7c51706SLoic Pallardy } 1251d7c51706SLoic Pallardy } 1252d7c51706SLoic Pallardy 1253d7c51706SLoic Pallardy return 0; 1254d7c51706SLoic Pallardy } 1255d7c51706SLoic Pallardy 1256d7c51706SLoic Pallardy /** 12572666ca91SSarangdhar Joshi * rproc_coredump_cleanup() - clean up dump_segments list 12582666ca91SSarangdhar Joshi * @rproc: the remote processor handle 12592666ca91SSarangdhar Joshi */ 12602666ca91SSarangdhar Joshi static void rproc_coredump_cleanup(struct rproc *rproc) 12612666ca91SSarangdhar Joshi { 12622666ca91SSarangdhar Joshi struct rproc_dump_segment *entry, *tmp; 12632666ca91SSarangdhar Joshi 12642666ca91SSarangdhar Joshi list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) { 12652666ca91SSarangdhar Joshi list_del(&entry->node); 12662666ca91SSarangdhar Joshi kfree(entry); 12672666ca91SSarangdhar Joshi } 12682666ca91SSarangdhar Joshi } 12692666ca91SSarangdhar Joshi 12702666ca91SSarangdhar Joshi /** 1271400e64dfSOhad Ben-Cohen * rproc_resource_cleanup() - clean up and free all acquired resources 1272400e64dfSOhad Ben-Cohen * @rproc: rproc handle 1273400e64dfSOhad Ben-Cohen * 1274400e64dfSOhad Ben-Cohen * This function will free all resources acquired for @rproc, and it 12757a186941SOhad Ben-Cohen * is called whenever @rproc either shuts down or fails to boot. 1276400e64dfSOhad Ben-Cohen */ 1277400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc) 1278400e64dfSOhad Ben-Cohen { 1279400e64dfSOhad Ben-Cohen struct rproc_mem_entry *entry, *tmp; 1280a987e6b9SLoic Pallardy struct rproc_debug_trace *trace, *ttmp; 1281d81fb32fSBjorn Andersson struct rproc_vdev *rvdev, *rvtmp; 1282b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1283400e64dfSOhad Ben-Cohen 1284400e64dfSOhad Ben-Cohen /* clean up debugfs trace entries */ 1285a987e6b9SLoic Pallardy list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) { 1286a987e6b9SLoic Pallardy rproc_remove_trace_file(trace->tfile); 1287400e64dfSOhad Ben-Cohen rproc->num_traces--; 1288a987e6b9SLoic Pallardy list_del(&trace->node); 1289a987e6b9SLoic Pallardy kfree(trace); 1290400e64dfSOhad Ben-Cohen } 1291400e64dfSOhad Ben-Cohen 1292400e64dfSOhad Ben-Cohen /* clean up iommu mapping entries */ 1293400e64dfSOhad Ben-Cohen list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { 1294400e64dfSOhad Ben-Cohen size_t unmapped; 1295400e64dfSOhad Ben-Cohen 1296400e64dfSOhad Ben-Cohen unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); 1297400e64dfSOhad Ben-Cohen if (unmapped != entry->len) { 1298400e64dfSOhad Ben-Cohen /* nothing much to do besides complaining */ 1299096ee786SClement Leger dev_err(dev, "failed to unmap %zx/%zu\n", entry->len, 1300400e64dfSOhad Ben-Cohen unmapped); 1301400e64dfSOhad Ben-Cohen } 1302400e64dfSOhad Ben-Cohen 1303400e64dfSOhad Ben-Cohen list_del(&entry->node); 1304400e64dfSOhad Ben-Cohen kfree(entry); 1305400e64dfSOhad Ben-Cohen } 1306b6356a01SSuman Anna 1307b6356a01SSuman Anna /* clean up carveout allocations */ 1308b6356a01SSuman Anna list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1309f2e74abfSLoic Pallardy if (entry->release) 1310f2e74abfSLoic Pallardy entry->release(rproc, entry); 1311b6356a01SSuman Anna list_del(&entry->node); 1312b6356a01SSuman Anna kfree(entry); 1313b6356a01SSuman Anna } 1314d81fb32fSBjorn Andersson 1315d81fb32fSBjorn Andersson /* clean up remote vdev entries */ 1316f5bcb353SBjorn Andersson list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) 13172b45cef5SBjorn Andersson kref_put(&rvdev->refcount, rproc_vdev_release); 13182666ca91SSarangdhar Joshi 13192666ca91SSarangdhar Joshi rproc_coredump_cleanup(rproc); 13202b45cef5SBjorn Andersson } 1321400e64dfSOhad Ben-Cohen 13221efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw) 13231efa30d0SSarangdhar Joshi { 1324a4b24c75SBjorn Andersson struct resource_table *loaded_table; 13251efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 1326a4b24c75SBjorn Andersson int ret; 13271efa30d0SSarangdhar Joshi 13281efa30d0SSarangdhar Joshi /* load the ELF segments to memory */ 13291efa30d0SSarangdhar Joshi ret = rproc_load_segments(rproc, fw); 13301efa30d0SSarangdhar Joshi if (ret) { 13311efa30d0SSarangdhar Joshi dev_err(dev, "Failed to load program segments: %d\n", ret); 13321efa30d0SSarangdhar Joshi return ret; 13331efa30d0SSarangdhar Joshi } 13341efa30d0SSarangdhar Joshi 13351efa30d0SSarangdhar Joshi /* 13361efa30d0SSarangdhar Joshi * The starting device has been given the rproc->cached_table as the 13371efa30d0SSarangdhar Joshi * resource table. The address of the vring along with the other 13381efa30d0SSarangdhar Joshi * allocated resources (carveouts etc) is stored in cached_table. 13391efa30d0SSarangdhar Joshi * In order to pass this information to the remote device we must copy 13401efa30d0SSarangdhar Joshi * this information to device memory. We also update the table_ptr so 13411efa30d0SSarangdhar Joshi * that any subsequent changes will be applied to the loaded version. 13421efa30d0SSarangdhar Joshi */ 13431efa30d0SSarangdhar Joshi loaded_table = rproc_find_loaded_rsc_table(rproc, fw); 13441efa30d0SSarangdhar Joshi if (loaded_table) { 1345a4b24c75SBjorn Andersson memcpy(loaded_table, rproc->cached_table, rproc->table_sz); 13461efa30d0SSarangdhar Joshi rproc->table_ptr = loaded_table; 13471efa30d0SSarangdhar Joshi } 13481efa30d0SSarangdhar Joshi 1349c455daa4SBjorn Andersson ret = rproc_prepare_subdevices(rproc); 1350c455daa4SBjorn Andersson if (ret) { 1351c455daa4SBjorn Andersson dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1352c455daa4SBjorn Andersson rproc->name, ret); 1353f68d51bdSSuman Anna goto reset_table_ptr; 1354c455daa4SBjorn Andersson } 1355c455daa4SBjorn Andersson 13561efa30d0SSarangdhar Joshi /* power up the remote processor */ 13571efa30d0SSarangdhar Joshi ret = rproc->ops->start(rproc); 13581efa30d0SSarangdhar Joshi if (ret) { 13591efa30d0SSarangdhar Joshi dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); 1360c455daa4SBjorn Andersson goto unprepare_subdevices; 13611efa30d0SSarangdhar Joshi } 13621efa30d0SSarangdhar Joshi 1363618fcff3SBjorn Andersson /* Start any subdevices for the remote processor */ 1364618fcff3SBjorn Andersson ret = rproc_start_subdevices(rproc); 13651efa30d0SSarangdhar Joshi if (ret) { 13661efa30d0SSarangdhar Joshi dev_err(dev, "failed to probe subdevices for %s: %d\n", 13671efa30d0SSarangdhar Joshi rproc->name, ret); 1368c455daa4SBjorn Andersson goto stop_rproc; 13691efa30d0SSarangdhar Joshi } 13701efa30d0SSarangdhar Joshi 13711efa30d0SSarangdhar Joshi rproc->state = RPROC_RUNNING; 13721efa30d0SSarangdhar Joshi 13731efa30d0SSarangdhar Joshi dev_info(dev, "remote processor %s is now up\n", rproc->name); 13741efa30d0SSarangdhar Joshi 13751efa30d0SSarangdhar Joshi return 0; 1376c455daa4SBjorn Andersson 1377c455daa4SBjorn Andersson stop_rproc: 1378c455daa4SBjorn Andersson rproc->ops->stop(rproc); 1379c455daa4SBjorn Andersson unprepare_subdevices: 1380c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1381f68d51bdSSuman Anna reset_table_ptr: 1382f68d51bdSSuman Anna rproc->table_ptr = rproc->cached_table; 1383c455daa4SBjorn Andersson 1384c455daa4SBjorn Andersson return ret; 13851efa30d0SSarangdhar Joshi } 13861efa30d0SSarangdhar Joshi 1387fdf0e00eSMathieu Poirier static int rproc_attach(struct rproc *rproc) 1388d848a481SMathieu Poirier { 1389d848a481SMathieu Poirier struct device *dev = &rproc->dev; 1390d848a481SMathieu Poirier int ret; 1391d848a481SMathieu Poirier 1392d848a481SMathieu Poirier ret = rproc_prepare_subdevices(rproc); 1393d848a481SMathieu Poirier if (ret) { 1394d848a481SMathieu Poirier dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1395d848a481SMathieu Poirier rproc->name, ret); 1396d848a481SMathieu Poirier goto out; 1397d848a481SMathieu Poirier } 1398d848a481SMathieu Poirier 1399d848a481SMathieu Poirier /* Attach to the remote processor */ 1400d848a481SMathieu Poirier ret = rproc_attach_device(rproc); 1401d848a481SMathieu Poirier if (ret) { 1402d848a481SMathieu Poirier dev_err(dev, "can't attach to rproc %s: %d\n", 1403d848a481SMathieu Poirier rproc->name, ret); 1404d848a481SMathieu Poirier goto unprepare_subdevices; 1405d848a481SMathieu Poirier } 1406d848a481SMathieu Poirier 1407d848a481SMathieu Poirier /* Start any subdevices for the remote processor */ 1408d848a481SMathieu Poirier ret = rproc_start_subdevices(rproc); 1409d848a481SMathieu Poirier if (ret) { 1410d848a481SMathieu Poirier dev_err(dev, "failed to probe subdevices for %s: %d\n", 1411d848a481SMathieu Poirier rproc->name, ret); 1412d848a481SMathieu Poirier goto stop_rproc; 1413d848a481SMathieu Poirier } 1414d848a481SMathieu Poirier 1415d848a481SMathieu Poirier rproc->state = RPROC_RUNNING; 1416d848a481SMathieu Poirier 1417d848a481SMathieu Poirier dev_info(dev, "remote processor %s is now attached\n", rproc->name); 1418d848a481SMathieu Poirier 1419d848a481SMathieu Poirier return 0; 1420d848a481SMathieu Poirier 1421d848a481SMathieu Poirier stop_rproc: 1422d848a481SMathieu Poirier rproc->ops->stop(rproc); 1423d848a481SMathieu Poirier unprepare_subdevices: 1424d848a481SMathieu Poirier rproc_unprepare_subdevices(rproc); 1425d848a481SMathieu Poirier out: 1426d848a481SMathieu Poirier return ret; 1427d848a481SMathieu Poirier } 1428d848a481SMathieu Poirier 1429400e64dfSOhad Ben-Cohen /* 1430400e64dfSOhad Ben-Cohen * take a firmware and boot a remote processor with it. 1431400e64dfSOhad Ben-Cohen */ 1432400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) 1433400e64dfSOhad Ben-Cohen { 1434b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1435400e64dfSOhad Ben-Cohen const char *name = rproc->firmware; 143658b64090SBjorn Andersson int ret; 1437400e64dfSOhad Ben-Cohen 1438400e64dfSOhad Ben-Cohen ret = rproc_fw_sanity_check(rproc, fw); 1439400e64dfSOhad Ben-Cohen if (ret) 1440400e64dfSOhad Ben-Cohen return ret; 1441400e64dfSOhad Ben-Cohen 1442e981f6d4SSjur Brændeland dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); 1443400e64dfSOhad Ben-Cohen 1444400e64dfSOhad Ben-Cohen /* 1445400e64dfSOhad Ben-Cohen * if enabling an IOMMU isn't relevant for this rproc, this is 1446400e64dfSOhad Ben-Cohen * just a nop 1447400e64dfSOhad Ben-Cohen */ 1448400e64dfSOhad Ben-Cohen ret = rproc_enable_iommu(rproc); 1449400e64dfSOhad Ben-Cohen if (ret) { 1450400e64dfSOhad Ben-Cohen dev_err(dev, "can't enable iommu: %d\n", ret); 145149cff125SMathieu Poirier return ret; 1452400e64dfSOhad Ben-Cohen } 1453400e64dfSOhad Ben-Cohen 145433467ac3SLoic Pallardy /* Prepare rproc for firmware loading if needed */ 145533467ac3SLoic Pallardy ret = rproc_prepare_device(rproc); 145633467ac3SLoic Pallardy if (ret) { 145733467ac3SLoic Pallardy dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); 145833467ac3SLoic Pallardy goto disable_iommu; 145933467ac3SLoic Pallardy } 146033467ac3SLoic Pallardy 14613e5f9eb5SSjur Brændeland rproc->bootaddr = rproc_get_boot_addr(rproc, fw); 1462400e64dfSOhad Ben-Cohen 1463c1d35c1aSBjorn Andersson /* Load resource table, core dump segment list etc from the firmware */ 1464c1d35c1aSBjorn Andersson ret = rproc_parse_fw(rproc, fw); 146558b64090SBjorn Andersson if (ret) 146633467ac3SLoic Pallardy goto unprepare_rproc; 1467a0c10687SBjorn Andersson 1468b35d7afcSBjorn Andersson /* reset max_notifyid */ 1469b35d7afcSBjorn Andersson rproc->max_notifyid = -1; 1470b35d7afcSBjorn Andersson 1471c6aed238SLoic Pallardy /* reset handled vdev */ 1472c6aed238SLoic Pallardy rproc->nb_vdev = 0; 1473c6aed238SLoic Pallardy 1474400e64dfSOhad Ben-Cohen /* handle fw resources which are required to boot rproc */ 1475a4b24c75SBjorn Andersson ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1476400e64dfSOhad Ben-Cohen if (ret) { 1477400e64dfSOhad Ben-Cohen dev_err(dev, "Failed to process resources: %d\n", ret); 1478229b85a6SBjorn Andersson goto clean_up_resources; 1479400e64dfSOhad Ben-Cohen } 1480400e64dfSOhad Ben-Cohen 1481d7c51706SLoic Pallardy /* Allocate carveout resources associated to rproc */ 1482d7c51706SLoic Pallardy ret = rproc_alloc_registered_carveouts(rproc); 1483d7c51706SLoic Pallardy if (ret) { 1484d7c51706SLoic Pallardy dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1485d7c51706SLoic Pallardy ret); 1486d7c51706SLoic Pallardy goto clean_up_resources; 1487d7c51706SLoic Pallardy } 1488d7c51706SLoic Pallardy 14891efa30d0SSarangdhar Joshi ret = rproc_start(rproc, fw); 14901efa30d0SSarangdhar Joshi if (ret) 1491229b85a6SBjorn Andersson goto clean_up_resources; 1492400e64dfSOhad Ben-Cohen 1493400e64dfSOhad Ben-Cohen return 0; 1494400e64dfSOhad Ben-Cohen 1495229b85a6SBjorn Andersson clean_up_resources: 1496229b85a6SBjorn Andersson rproc_resource_cleanup(rproc); 1497a0c10687SBjorn Andersson kfree(rproc->cached_table); 1498a0c10687SBjorn Andersson rproc->cached_table = NULL; 1499988d204cSBjorn Andersson rproc->table_ptr = NULL; 150033467ac3SLoic Pallardy unprepare_rproc: 150133467ac3SLoic Pallardy /* release HW resources if needed */ 150233467ac3SLoic Pallardy rproc_unprepare_device(rproc); 150358b64090SBjorn Andersson disable_iommu: 1504400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1505400e64dfSOhad Ben-Cohen return ret; 1506400e64dfSOhad Ben-Cohen } 1507400e64dfSOhad Ben-Cohen 1508400e64dfSOhad Ben-Cohen /* 1509fdf0e00eSMathieu Poirier * Attach to remote processor - similar to rproc_fw_boot() but without 1510fdf0e00eSMathieu Poirier * the steps that deal with the firmware image. 1511fdf0e00eSMathieu Poirier */ 15120f9dc562SMathieu Poirier static int rproc_actuate(struct rproc *rproc) 1513fdf0e00eSMathieu Poirier { 1514fdf0e00eSMathieu Poirier struct device *dev = &rproc->dev; 1515fdf0e00eSMathieu Poirier int ret; 1516fdf0e00eSMathieu Poirier 1517fdf0e00eSMathieu Poirier /* 1518fdf0e00eSMathieu Poirier * if enabling an IOMMU isn't relevant for this rproc, this is 1519fdf0e00eSMathieu Poirier * just a nop 1520fdf0e00eSMathieu Poirier */ 1521fdf0e00eSMathieu Poirier ret = rproc_enable_iommu(rproc); 1522fdf0e00eSMathieu Poirier if (ret) { 1523fdf0e00eSMathieu Poirier dev_err(dev, "can't enable iommu: %d\n", ret); 1524fdf0e00eSMathieu Poirier return ret; 1525fdf0e00eSMathieu Poirier } 1526fdf0e00eSMathieu Poirier 1527fdf0e00eSMathieu Poirier /* reset max_notifyid */ 1528fdf0e00eSMathieu Poirier rproc->max_notifyid = -1; 1529fdf0e00eSMathieu Poirier 1530fdf0e00eSMathieu Poirier /* reset handled vdev */ 1531fdf0e00eSMathieu Poirier rproc->nb_vdev = 0; 1532fdf0e00eSMathieu Poirier 1533fdf0e00eSMathieu Poirier /* 1534fdf0e00eSMathieu Poirier * Handle firmware resources required to attach to a remote processor. 1535fdf0e00eSMathieu Poirier * Because we are attaching rather than booting the remote processor, 1536fdf0e00eSMathieu Poirier * we expect the platform driver to properly set rproc->table_ptr. 1537fdf0e00eSMathieu Poirier */ 1538fdf0e00eSMathieu Poirier ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1539fdf0e00eSMathieu Poirier if (ret) { 1540fdf0e00eSMathieu Poirier dev_err(dev, "Failed to process resources: %d\n", ret); 1541fdf0e00eSMathieu Poirier goto disable_iommu; 1542fdf0e00eSMathieu Poirier } 1543fdf0e00eSMathieu Poirier 1544fdf0e00eSMathieu Poirier /* Allocate carveout resources associated to rproc */ 1545fdf0e00eSMathieu Poirier ret = rproc_alloc_registered_carveouts(rproc); 1546fdf0e00eSMathieu Poirier if (ret) { 1547fdf0e00eSMathieu Poirier dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1548fdf0e00eSMathieu Poirier ret); 1549fdf0e00eSMathieu Poirier goto clean_up_resources; 1550fdf0e00eSMathieu Poirier } 1551fdf0e00eSMathieu Poirier 1552fdf0e00eSMathieu Poirier ret = rproc_attach(rproc); 1553fdf0e00eSMathieu Poirier if (ret) 1554fdf0e00eSMathieu Poirier goto clean_up_resources; 1555fdf0e00eSMathieu Poirier 1556fdf0e00eSMathieu Poirier return 0; 1557fdf0e00eSMathieu Poirier 1558fdf0e00eSMathieu Poirier clean_up_resources: 1559fdf0e00eSMathieu Poirier rproc_resource_cleanup(rproc); 1560fdf0e00eSMathieu Poirier disable_iommu: 1561fdf0e00eSMathieu Poirier rproc_disable_iommu(rproc); 1562fdf0e00eSMathieu Poirier return ret; 1563fdf0e00eSMathieu Poirier } 1564fdf0e00eSMathieu Poirier 1565fdf0e00eSMathieu Poirier /* 15665e6533f7SSarangdhar Joshi * take a firmware and boot it up. 1567400e64dfSOhad Ben-Cohen * 1568400e64dfSOhad Ben-Cohen * Note: this function is called asynchronously upon registration of the 1569400e64dfSOhad Ben-Cohen * remote processor (so we must wait until it completes before we try 1570400e64dfSOhad Ben-Cohen * to unregister the device. one other option is just to use kref here, 1571400e64dfSOhad Ben-Cohen * that might be cleaner). 1572400e64dfSOhad Ben-Cohen */ 15735e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context) 1574400e64dfSOhad Ben-Cohen { 1575400e64dfSOhad Ben-Cohen struct rproc *rproc = context; 1576a2b950acSOhad Ben-Cohen 15772bfc311aSBjorn Andersson rproc_boot(rproc); 1578ddf71187SBjorn Andersson 1579400e64dfSOhad Ben-Cohen release_firmware(fw); 1580400e64dfSOhad Ben-Cohen } 1581400e64dfSOhad Ben-Cohen 15825e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc) 158370b85ef8SFernando Guzman Lugo { 158470b85ef8SFernando Guzman Lugo int ret; 158570b85ef8SFernando Guzman Lugo 158670b85ef8SFernando Guzman Lugo /* 1587e3d21939SMathieu Poirier * Since the remote processor is in a detached state, it has already 1588e3d21939SMathieu Poirier * been booted by another entity. As such there is no point in waiting 1589e3d21939SMathieu Poirier * for a firmware image to be loaded, we can simply initiate the process 1590e3d21939SMathieu Poirier * of attaching to it immediately. 1591e3d21939SMathieu Poirier */ 1592e3d21939SMathieu Poirier if (rproc->state == RPROC_DETACHED) 1593e3d21939SMathieu Poirier return rproc_boot(rproc); 1594e3d21939SMathieu Poirier 1595e3d21939SMathieu Poirier /* 159670b85ef8SFernando Guzman Lugo * We're initiating an asynchronous firmware loading, so we can 159770b85ef8SFernando Guzman Lugo * be built-in kernel code, without hanging the boot process. 159870b85ef8SFernando Guzman Lugo */ 159970b85ef8SFernando Guzman Lugo ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, 160070b85ef8SFernando Guzman Lugo rproc->firmware, &rproc->dev, GFP_KERNEL, 16015e6533f7SSarangdhar Joshi rproc, rproc_auto_boot_callback); 16022099c77dSSarangdhar Joshi if (ret < 0) 160370b85ef8SFernando Guzman Lugo dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); 160470b85ef8SFernando Guzman Lugo 160570b85ef8SFernando Guzman Lugo return ret; 160670b85ef8SFernando Guzman Lugo } 160770b85ef8SFernando Guzman Lugo 1608880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed) 16091efa30d0SSarangdhar Joshi { 16101efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 16111efa30d0SSarangdhar Joshi int ret; 16121efa30d0SSarangdhar Joshi 1613618fcff3SBjorn Andersson /* Stop any subdevices for the remote processor */ 1614618fcff3SBjorn Andersson rproc_stop_subdevices(rproc, crashed); 16151efa30d0SSarangdhar Joshi 16160a8b81cbSBjorn Andersson /* the installed resource table is no longer accessible */ 16170a8b81cbSBjorn Andersson rproc->table_ptr = rproc->cached_table; 16180a8b81cbSBjorn Andersson 16191efa30d0SSarangdhar Joshi /* power off the remote processor */ 16201efa30d0SSarangdhar Joshi ret = rproc->ops->stop(rproc); 16211efa30d0SSarangdhar Joshi if (ret) { 16221efa30d0SSarangdhar Joshi dev_err(dev, "can't stop rproc: %d\n", ret); 16231efa30d0SSarangdhar Joshi return ret; 16241efa30d0SSarangdhar Joshi } 16251efa30d0SSarangdhar Joshi 1626c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1627c455daa4SBjorn Andersson 16281efa30d0SSarangdhar Joshi rproc->state = RPROC_OFFLINE; 16291efa30d0SSarangdhar Joshi 1630*4a4dca19SMathieu Poirier /* 1631*4a4dca19SMathieu Poirier * The remote processor has been stopped and is now offline, which means 1632*4a4dca19SMathieu Poirier * that the next time it is brought back online the remoteproc core will 1633*4a4dca19SMathieu Poirier * be responsible to load its firmware. As such it is no longer 1634*4a4dca19SMathieu Poirier * autonomous. 1635*4a4dca19SMathieu Poirier */ 1636*4a4dca19SMathieu Poirier rproc->autonomous = false; 1637*4a4dca19SMathieu Poirier 16381efa30d0SSarangdhar Joshi dev_info(dev, "stopped remote processor %s\n", rproc->name); 16391efa30d0SSarangdhar Joshi 16401efa30d0SSarangdhar Joshi return 0; 16411efa30d0SSarangdhar Joshi } 16421efa30d0SSarangdhar Joshi 164370b85ef8SFernando Guzman Lugo /** 16442666ca91SSarangdhar Joshi * rproc_coredump_add_segment() - add segment of device memory to coredump 16452666ca91SSarangdhar Joshi * @rproc: handle of a remote processor 16462666ca91SSarangdhar Joshi * @da: device address 16472666ca91SSarangdhar Joshi * @size: size of segment 16482666ca91SSarangdhar Joshi * 16492666ca91SSarangdhar Joshi * Add device memory to the list of segments to be included in a coredump for 16502666ca91SSarangdhar Joshi * the remoteproc. 16512666ca91SSarangdhar Joshi * 16522666ca91SSarangdhar Joshi * Return: 0 on success, negative errno on error. 16532666ca91SSarangdhar Joshi */ 16542666ca91SSarangdhar Joshi int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size) 16552666ca91SSarangdhar Joshi { 16562666ca91SSarangdhar Joshi struct rproc_dump_segment *segment; 16572666ca91SSarangdhar Joshi 16582666ca91SSarangdhar Joshi segment = kzalloc(sizeof(*segment), GFP_KERNEL); 16592666ca91SSarangdhar Joshi if (!segment) 16602666ca91SSarangdhar Joshi return -ENOMEM; 16612666ca91SSarangdhar Joshi 16622666ca91SSarangdhar Joshi segment->da = da; 16632666ca91SSarangdhar Joshi segment->size = size; 16642666ca91SSarangdhar Joshi 16652666ca91SSarangdhar Joshi list_add_tail(&segment->node, &rproc->dump_segments); 16662666ca91SSarangdhar Joshi 16672666ca91SSarangdhar Joshi return 0; 16682666ca91SSarangdhar Joshi } 16692666ca91SSarangdhar Joshi EXPORT_SYMBOL(rproc_coredump_add_segment); 16702666ca91SSarangdhar Joshi 16712666ca91SSarangdhar Joshi /** 1672ab8f873bSSibi Sankar * rproc_coredump_add_custom_segment() - add custom coredump segment 1673ab8f873bSSibi Sankar * @rproc: handle of a remote processor 1674ab8f873bSSibi Sankar * @da: device address 1675ab8f873bSSibi Sankar * @size: size of segment 1676ab8f873bSSibi Sankar * @dumpfn: custom dump function called for each segment during coredump 1677ab8f873bSSibi Sankar * @priv: private data 1678ab8f873bSSibi Sankar * 1679ab8f873bSSibi Sankar * Add device memory to the list of segments to be included in the coredump 1680ab8f873bSSibi Sankar * and associate the segment with the given custom dump function and private 1681ab8f873bSSibi Sankar * data. 1682ab8f873bSSibi Sankar * 1683ab8f873bSSibi Sankar * Return: 0 on success, negative errno on error. 1684ab8f873bSSibi Sankar */ 1685ab8f873bSSibi Sankar int rproc_coredump_add_custom_segment(struct rproc *rproc, 1686ab8f873bSSibi Sankar dma_addr_t da, size_t size, 1687ab8f873bSSibi Sankar void (*dumpfn)(struct rproc *rproc, 1688ab8f873bSSibi Sankar struct rproc_dump_segment *segment, 1689ab8f873bSSibi Sankar void *dest), 1690ab8f873bSSibi Sankar void *priv) 1691ab8f873bSSibi Sankar { 1692ab8f873bSSibi Sankar struct rproc_dump_segment *segment; 1693ab8f873bSSibi Sankar 1694ab8f873bSSibi Sankar segment = kzalloc(sizeof(*segment), GFP_KERNEL); 1695ab8f873bSSibi Sankar if (!segment) 1696ab8f873bSSibi Sankar return -ENOMEM; 1697ab8f873bSSibi Sankar 1698ab8f873bSSibi Sankar segment->da = da; 1699ab8f873bSSibi Sankar segment->size = size; 1700ab8f873bSSibi Sankar segment->priv = priv; 1701ab8f873bSSibi Sankar segment->dump = dumpfn; 1702ab8f873bSSibi Sankar 1703ab8f873bSSibi Sankar list_add_tail(&segment->node, &rproc->dump_segments); 1704ab8f873bSSibi Sankar 1705ab8f873bSSibi Sankar return 0; 1706ab8f873bSSibi Sankar } 1707ab8f873bSSibi Sankar EXPORT_SYMBOL(rproc_coredump_add_custom_segment); 1708ab8f873bSSibi Sankar 1709ab8f873bSSibi Sankar /** 1710418fd787SClement Leger * rproc_coredump_set_elf_info() - set coredump elf information 1711418fd787SClement Leger * @rproc: handle of a remote processor 1712418fd787SClement Leger * @class: elf class for coredump elf file 1713418fd787SClement Leger * @machine: elf machine for coredump elf file 1714418fd787SClement Leger * 1715418fd787SClement Leger * Set elf information which will be used for coredump elf file. 1716418fd787SClement Leger * 1717418fd787SClement Leger * Return: 0 on success, negative errno on error. 1718418fd787SClement Leger */ 1719418fd787SClement Leger int rproc_coredump_set_elf_info(struct rproc *rproc, u8 class, u16 machine) 1720418fd787SClement Leger { 1721418fd787SClement Leger if (class != ELFCLASS64 && class != ELFCLASS32) 1722418fd787SClement Leger return -EINVAL; 1723418fd787SClement Leger 1724418fd787SClement Leger rproc->elf_class = class; 1725418fd787SClement Leger rproc->elf_machine = machine; 1726418fd787SClement Leger 1727418fd787SClement Leger return 0; 1728418fd787SClement Leger } 1729418fd787SClement Leger EXPORT_SYMBOL(rproc_coredump_set_elf_info); 1730418fd787SClement Leger 1731418fd787SClement Leger /** 17322666ca91SSarangdhar Joshi * rproc_coredump() - perform coredump 17332666ca91SSarangdhar Joshi * @rproc: rproc handle 17342666ca91SSarangdhar Joshi * 17352666ca91SSarangdhar Joshi * This function will generate an ELF header for the registered segments 17362666ca91SSarangdhar Joshi * and create a devcoredump device associated with rproc. 17372666ca91SSarangdhar Joshi */ 17382666ca91SSarangdhar Joshi static void rproc_coredump(struct rproc *rproc) 17392666ca91SSarangdhar Joshi { 17402666ca91SSarangdhar Joshi struct rproc_dump_segment *segment; 17418f403350SClement Leger void *phdr; 17428f403350SClement Leger void *ehdr; 17432666ca91SSarangdhar Joshi size_t data_size; 17442666ca91SSarangdhar Joshi size_t offset; 17452666ca91SSarangdhar Joshi void *data; 17462666ca91SSarangdhar Joshi void *ptr; 17478f403350SClement Leger u8 class = rproc->elf_class; 17482666ca91SSarangdhar Joshi int phnum = 0; 17492666ca91SSarangdhar Joshi 17502666ca91SSarangdhar Joshi if (list_empty(&rproc->dump_segments)) 17512666ca91SSarangdhar Joshi return; 17522666ca91SSarangdhar Joshi 1753418fd787SClement Leger if (class == ELFCLASSNONE) { 1754418fd787SClement Leger dev_err(&rproc->dev, "Elf class is not set\n"); 1755418fd787SClement Leger return; 1756418fd787SClement Leger } 1757418fd787SClement Leger 17588f403350SClement Leger data_size = elf_size_of_hdr(class); 17592666ca91SSarangdhar Joshi list_for_each_entry(segment, &rproc->dump_segments, node) { 17608f403350SClement Leger data_size += elf_size_of_phdr(class) + segment->size; 17612666ca91SSarangdhar Joshi 17622666ca91SSarangdhar Joshi phnum++; 17632666ca91SSarangdhar Joshi } 17642666ca91SSarangdhar Joshi 17652666ca91SSarangdhar Joshi data = vmalloc(data_size); 17662666ca91SSarangdhar Joshi if (!data) 17672666ca91SSarangdhar Joshi return; 17682666ca91SSarangdhar Joshi 17692666ca91SSarangdhar Joshi ehdr = data; 17702666ca91SSarangdhar Joshi 17718f403350SClement Leger memset(ehdr, 0, elf_size_of_hdr(class)); 17728f403350SClement Leger /* e_ident field is common for both elf32 and elf64 */ 17738f403350SClement Leger elf_hdr_init_ident(ehdr, class); 17742666ca91SSarangdhar Joshi 17758f403350SClement Leger elf_hdr_set_e_type(class, ehdr, ET_CORE); 1776418fd787SClement Leger elf_hdr_set_e_machine(class, ehdr, rproc->elf_machine); 17778f403350SClement Leger elf_hdr_set_e_version(class, ehdr, EV_CURRENT); 17788f403350SClement Leger elf_hdr_set_e_entry(class, ehdr, rproc->bootaddr); 17798f403350SClement Leger elf_hdr_set_e_phoff(class, ehdr, elf_size_of_hdr(class)); 17808f403350SClement Leger elf_hdr_set_e_ehsize(class, ehdr, elf_size_of_hdr(class)); 17818f403350SClement Leger elf_hdr_set_e_phentsize(class, ehdr, elf_size_of_phdr(class)); 17828f403350SClement Leger elf_hdr_set_e_phnum(class, ehdr, phnum); 17838f403350SClement Leger 17848f403350SClement Leger phdr = data + elf_hdr_get_e_phoff(class, ehdr); 17858f403350SClement Leger offset = elf_hdr_get_e_phoff(class, ehdr); 17868f403350SClement Leger offset += elf_size_of_phdr(class) * elf_hdr_get_e_phnum(class, ehdr); 17878f403350SClement Leger 17882666ca91SSarangdhar Joshi list_for_each_entry(segment, &rproc->dump_segments, node) { 17898f403350SClement Leger memset(phdr, 0, elf_size_of_phdr(class)); 17908f403350SClement Leger elf_phdr_set_p_type(class, phdr, PT_LOAD); 17918f403350SClement Leger elf_phdr_set_p_offset(class, phdr, offset); 17928f403350SClement Leger elf_phdr_set_p_vaddr(class, phdr, segment->da); 17938f403350SClement Leger elf_phdr_set_p_paddr(class, phdr, segment->da); 17948f403350SClement Leger elf_phdr_set_p_filesz(class, phdr, segment->size); 17958f403350SClement Leger elf_phdr_set_p_memsz(class, phdr, segment->size); 17968f403350SClement Leger elf_phdr_set_p_flags(class, phdr, PF_R | PF_W | PF_X); 17978f403350SClement Leger elf_phdr_set_p_align(class, phdr, 0); 17982666ca91SSarangdhar Joshi 17993952105dSSibi Sankar if (segment->dump) { 18003952105dSSibi Sankar segment->dump(rproc, segment, data + offset); 18013952105dSSibi Sankar } else { 18022666ca91SSarangdhar Joshi ptr = rproc_da_to_va(rproc, segment->da, segment->size); 18032666ca91SSarangdhar Joshi if (!ptr) { 18042666ca91SSarangdhar Joshi dev_err(&rproc->dev, 18052666ca91SSarangdhar Joshi "invalid coredump segment (%pad, %zu)\n", 18062666ca91SSarangdhar Joshi &segment->da, segment->size); 18072666ca91SSarangdhar Joshi memset(data + offset, 0xff, segment->size); 18082666ca91SSarangdhar Joshi } else { 18092666ca91SSarangdhar Joshi memcpy(data + offset, ptr, segment->size); 18102666ca91SSarangdhar Joshi } 18113952105dSSibi Sankar } 18122666ca91SSarangdhar Joshi 18138f403350SClement Leger offset += elf_phdr_get_p_filesz(class, phdr); 18148f403350SClement Leger phdr += elf_size_of_phdr(class); 18152666ca91SSarangdhar Joshi } 18162666ca91SSarangdhar Joshi 18172666ca91SSarangdhar Joshi dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL); 18182666ca91SSarangdhar Joshi } 18192666ca91SSarangdhar Joshi 18202666ca91SSarangdhar Joshi /** 182170b85ef8SFernando Guzman Lugo * rproc_trigger_recovery() - recover a remoteproc 182270b85ef8SFernando Guzman Lugo * @rproc: the remote processor 182370b85ef8SFernando Guzman Lugo * 182456324d7aSAnna, Suman * The recovery is done by resetting all the virtio devices, that way all the 182570b85ef8SFernando Guzman Lugo * rpmsg drivers will be reseted along with the remote processor making the 182670b85ef8SFernando Guzman Lugo * remoteproc functional again. 182770b85ef8SFernando Guzman Lugo * 182870b85ef8SFernando Guzman Lugo * This function can sleep, so it cannot be called from atomic context. 182970b85ef8SFernando Guzman Lugo */ 183070b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc) 183170b85ef8SFernando Guzman Lugo { 18327e83cab8SSarangdhar Joshi const struct firmware *firmware_p; 18337e83cab8SSarangdhar Joshi struct device *dev = &rproc->dev; 18347e83cab8SSarangdhar Joshi int ret; 18357e83cab8SSarangdhar Joshi 18367e83cab8SSarangdhar Joshi ret = mutex_lock_interruptible(&rproc->lock); 18377e83cab8SSarangdhar Joshi if (ret) 18387e83cab8SSarangdhar Joshi return ret; 18397e83cab8SSarangdhar Joshi 18400b145574SAlex Elder /* State could have changed before we got the mutex */ 18410b145574SAlex Elder if (rproc->state != RPROC_CRASHED) 18420b145574SAlex Elder goto unlock_mutex; 18430b145574SAlex Elder 18440b145574SAlex Elder dev_err(dev, "recovering %s\n", rproc->name); 18450b145574SAlex Elder 1846fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, true); 18477e83cab8SSarangdhar Joshi if (ret) 18487e83cab8SSarangdhar Joshi goto unlock_mutex; 1849ddf71187SBjorn Andersson 18502666ca91SSarangdhar Joshi /* generate coredump */ 18512666ca91SSarangdhar Joshi rproc_coredump(rproc); 18522666ca91SSarangdhar Joshi 18537e83cab8SSarangdhar Joshi /* load firmware */ 18547e83cab8SSarangdhar Joshi ret = request_firmware(&firmware_p, rproc->firmware, dev); 18557e83cab8SSarangdhar Joshi if (ret < 0) { 18567e83cab8SSarangdhar Joshi dev_err(dev, "request_firmware failed: %d\n", ret); 18577e83cab8SSarangdhar Joshi goto unlock_mutex; 18587e83cab8SSarangdhar Joshi } 1859ddf71187SBjorn Andersson 18607e83cab8SSarangdhar Joshi /* boot the remote processor up again */ 18617e83cab8SSarangdhar Joshi ret = rproc_start(rproc, firmware_p); 18627e83cab8SSarangdhar Joshi 18637e83cab8SSarangdhar Joshi release_firmware(firmware_p); 18647e83cab8SSarangdhar Joshi 18657e83cab8SSarangdhar Joshi unlock_mutex: 18667e83cab8SSarangdhar Joshi mutex_unlock(&rproc->lock); 18677e83cab8SSarangdhar Joshi return ret; 186870b85ef8SFernando Guzman Lugo } 186970b85ef8SFernando Guzman Lugo 1870400e64dfSOhad Ben-Cohen /** 18718afd519cSFernando Guzman Lugo * rproc_crash_handler_work() - handle a crash 18722e7d4c2cSArnaud Pouliquen * @work: work treating the crash 18738afd519cSFernando Guzman Lugo * 18748afd519cSFernando Guzman Lugo * This function needs to handle everything related to a crash, like cpu 18758afd519cSFernando Guzman Lugo * registers and stack dump, information to help to debug the fatal error, etc. 18768afd519cSFernando Guzman Lugo */ 18778afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work) 18788afd519cSFernando Guzman Lugo { 18798afd519cSFernando Guzman Lugo struct rproc *rproc = container_of(work, struct rproc, crash_handler); 18808afd519cSFernando Guzman Lugo struct device *dev = &rproc->dev; 18818afd519cSFernando Guzman Lugo 18828afd519cSFernando Guzman Lugo dev_dbg(dev, "enter %s\n", __func__); 18838afd519cSFernando Guzman Lugo 18848afd519cSFernando Guzman Lugo mutex_lock(&rproc->lock); 18858afd519cSFernando Guzman Lugo 18868afd519cSFernando Guzman Lugo if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) { 18878afd519cSFernando Guzman Lugo /* handle only the first crash detected */ 18888afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 18898afd519cSFernando Guzman Lugo return; 18908afd519cSFernando Guzman Lugo } 18918afd519cSFernando Guzman Lugo 18928afd519cSFernando Guzman Lugo rproc->state = RPROC_CRASHED; 18938afd519cSFernando Guzman Lugo dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, 18948afd519cSFernando Guzman Lugo rproc->name); 18958afd519cSFernando Guzman Lugo 18968afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 18978afd519cSFernando Guzman Lugo 18982e37abb8SFernando Guzman Lugo if (!rproc->recovery_disabled) 189970b85ef8SFernando Guzman Lugo rproc_trigger_recovery(rproc); 1900a781e5aaSRishabh Bhatnagar 1901a781e5aaSRishabh Bhatnagar pm_relax(rproc->dev.parent); 19028afd519cSFernando Guzman Lugo } 19038afd519cSFernando Guzman Lugo 19048afd519cSFernando Guzman Lugo /** 19051b0ef906SSuman Anna * rproc_boot() - boot a remote processor 1906400e64dfSOhad Ben-Cohen * @rproc: handle of a remote processor 1907400e64dfSOhad Ben-Cohen * 1908400e64dfSOhad Ben-Cohen * Boot a remote processor (i.e. load its firmware, power it on, ...). 1909400e64dfSOhad Ben-Cohen * 1910400e64dfSOhad Ben-Cohen * If the remote processor is already powered on, this function immediately 1911400e64dfSOhad Ben-Cohen * returns (successfully). 1912400e64dfSOhad Ben-Cohen * 1913400e64dfSOhad Ben-Cohen * Returns 0 on success, and an appropriate error value otherwise. 1914400e64dfSOhad Ben-Cohen */ 19151b0ef906SSuman Anna int rproc_boot(struct rproc *rproc) 1916400e64dfSOhad Ben-Cohen { 1917400e64dfSOhad Ben-Cohen const struct firmware *firmware_p; 1918400e64dfSOhad Ben-Cohen struct device *dev; 1919400e64dfSOhad Ben-Cohen int ret; 1920400e64dfSOhad Ben-Cohen 1921400e64dfSOhad Ben-Cohen if (!rproc) { 1922400e64dfSOhad Ben-Cohen pr_err("invalid rproc handle\n"); 1923400e64dfSOhad Ben-Cohen return -EINVAL; 1924400e64dfSOhad Ben-Cohen } 1925400e64dfSOhad Ben-Cohen 1926b5ab5e24SOhad Ben-Cohen dev = &rproc->dev; 1927400e64dfSOhad Ben-Cohen 1928400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1929400e64dfSOhad Ben-Cohen if (ret) { 1930400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1931400e64dfSOhad Ben-Cohen return ret; 1932400e64dfSOhad Ben-Cohen } 1933400e64dfSOhad Ben-Cohen 19342099c77dSSarangdhar Joshi if (rproc->state == RPROC_DELETED) { 19352099c77dSSarangdhar Joshi ret = -ENODEV; 19362099c77dSSarangdhar Joshi dev_err(dev, "can't boot deleted rproc %s\n", rproc->name); 19372099c77dSSarangdhar Joshi goto unlock_mutex; 19382099c77dSSarangdhar Joshi } 19392099c77dSSarangdhar Joshi 19400f9dc562SMathieu Poirier /* skip the boot or attach process if rproc is already powered up */ 1941400e64dfSOhad Ben-Cohen if (atomic_inc_return(&rproc->power) > 1) { 1942400e64dfSOhad Ben-Cohen ret = 0; 1943400e64dfSOhad Ben-Cohen goto unlock_mutex; 1944400e64dfSOhad Ben-Cohen } 1945400e64dfSOhad Ben-Cohen 19460f9dc562SMathieu Poirier if (rproc->state == RPROC_DETACHED) { 19470f9dc562SMathieu Poirier dev_info(dev, "attaching to %s\n", rproc->name); 19480f9dc562SMathieu Poirier 19490f9dc562SMathieu Poirier ret = rproc_actuate(rproc); 19500f9dc562SMathieu Poirier } else { 1951400e64dfSOhad Ben-Cohen dev_info(dev, "powering up %s\n", rproc->name); 1952400e64dfSOhad Ben-Cohen 1953400e64dfSOhad Ben-Cohen /* load firmware */ 1954400e64dfSOhad Ben-Cohen ret = request_firmware(&firmware_p, rproc->firmware, dev); 1955400e64dfSOhad Ben-Cohen if (ret < 0) { 1956400e64dfSOhad Ben-Cohen dev_err(dev, "request_firmware failed: %d\n", ret); 1957400e64dfSOhad Ben-Cohen goto downref_rproc; 1958400e64dfSOhad Ben-Cohen } 1959400e64dfSOhad Ben-Cohen 1960400e64dfSOhad Ben-Cohen ret = rproc_fw_boot(rproc, firmware_p); 1961400e64dfSOhad Ben-Cohen 1962400e64dfSOhad Ben-Cohen release_firmware(firmware_p); 19630f9dc562SMathieu Poirier } 1964400e64dfSOhad Ben-Cohen 1965400e64dfSOhad Ben-Cohen downref_rproc: 1966fbb6aacbSBjorn Andersson if (ret) 1967400e64dfSOhad Ben-Cohen atomic_dec(&rproc->power); 1968400e64dfSOhad Ben-Cohen unlock_mutex: 1969400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1970400e64dfSOhad Ben-Cohen return ret; 1971400e64dfSOhad Ben-Cohen } 1972400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot); 1973400e64dfSOhad Ben-Cohen 1974400e64dfSOhad Ben-Cohen /** 1975400e64dfSOhad Ben-Cohen * rproc_shutdown() - power off the remote processor 1976400e64dfSOhad Ben-Cohen * @rproc: the remote processor 1977400e64dfSOhad Ben-Cohen * 1978400e64dfSOhad Ben-Cohen * Power off a remote processor (previously booted with rproc_boot()). 1979400e64dfSOhad Ben-Cohen * 1980400e64dfSOhad Ben-Cohen * In case @rproc is still being used by an additional user(s), then 1981400e64dfSOhad Ben-Cohen * this function will just decrement the power refcount and exit, 1982400e64dfSOhad Ben-Cohen * without really powering off the device. 1983400e64dfSOhad Ben-Cohen * 1984400e64dfSOhad Ben-Cohen * Every call to rproc_boot() must (eventually) be accompanied by a call 1985400e64dfSOhad Ben-Cohen * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. 1986400e64dfSOhad Ben-Cohen * 1987400e64dfSOhad Ben-Cohen * Notes: 1988400e64dfSOhad Ben-Cohen * - we're not decrementing the rproc's refcount, only the power refcount. 1989400e64dfSOhad Ben-Cohen * which means that the @rproc handle stays valid even after rproc_shutdown() 1990400e64dfSOhad Ben-Cohen * returns, and users can still use it with a subsequent rproc_boot(), if 1991400e64dfSOhad Ben-Cohen * needed. 1992400e64dfSOhad Ben-Cohen */ 1993400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc) 1994400e64dfSOhad Ben-Cohen { 1995b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1996400e64dfSOhad Ben-Cohen int ret; 1997400e64dfSOhad Ben-Cohen 1998400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1999400e64dfSOhad Ben-Cohen if (ret) { 2000400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 2001400e64dfSOhad Ben-Cohen return; 2002400e64dfSOhad Ben-Cohen } 2003400e64dfSOhad Ben-Cohen 2004400e64dfSOhad Ben-Cohen /* if the remote proc is still needed, bail out */ 2005400e64dfSOhad Ben-Cohen if (!atomic_dec_and_test(&rproc->power)) 2006400e64dfSOhad Ben-Cohen goto out; 2007400e64dfSOhad Ben-Cohen 2008fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, false); 2009400e64dfSOhad Ben-Cohen if (ret) { 2010400e64dfSOhad Ben-Cohen atomic_inc(&rproc->power); 2011400e64dfSOhad Ben-Cohen goto out; 2012400e64dfSOhad Ben-Cohen } 2013400e64dfSOhad Ben-Cohen 2014400e64dfSOhad Ben-Cohen /* clean up all acquired resources */ 2015400e64dfSOhad Ben-Cohen rproc_resource_cleanup(rproc); 2016400e64dfSOhad Ben-Cohen 201733467ac3SLoic Pallardy /* release HW resources if needed */ 201833467ac3SLoic Pallardy rproc_unprepare_device(rproc); 201933467ac3SLoic Pallardy 2020400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 2021400e64dfSOhad Ben-Cohen 2022988d204cSBjorn Andersson /* Free the copy of the resource table */ 2023a0c10687SBjorn Andersson kfree(rproc->cached_table); 2024a0c10687SBjorn Andersson rproc->cached_table = NULL; 2025988d204cSBjorn Andersson rproc->table_ptr = NULL; 2026400e64dfSOhad Ben-Cohen out: 2027400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 2028400e64dfSOhad Ben-Cohen } 2029400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown); 2030400e64dfSOhad Ben-Cohen 2031400e64dfSOhad Ben-Cohen /** 2032fec47d86SDave Gerlach * rproc_get_by_phandle() - find a remote processor by phandle 2033fec47d86SDave Gerlach * @phandle: phandle to the rproc 2034fec47d86SDave Gerlach * 2035fec47d86SDave Gerlach * Finds an rproc handle using the remote processor's phandle, and then 2036fec47d86SDave Gerlach * return a handle to the rproc. 2037fec47d86SDave Gerlach * 2038fec47d86SDave Gerlach * This function increments the remote processor's refcount, so always 2039fec47d86SDave Gerlach * use rproc_put() to decrement it back once rproc isn't needed anymore. 2040fec47d86SDave Gerlach * 2041fec47d86SDave Gerlach * Returns the rproc handle on success, and NULL on failure. 2042fec47d86SDave Gerlach */ 20438de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF 2044fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle) 2045fec47d86SDave Gerlach { 2046fec47d86SDave Gerlach struct rproc *rproc = NULL, *r; 2047fec47d86SDave Gerlach struct device_node *np; 2048fec47d86SDave Gerlach 2049fec47d86SDave Gerlach np = of_find_node_by_phandle(phandle); 2050fec47d86SDave Gerlach if (!np) 2051fec47d86SDave Gerlach return NULL; 2052fec47d86SDave Gerlach 2053c0abe2caSBjorn Andersson rcu_read_lock(); 2054c0abe2caSBjorn Andersson list_for_each_entry_rcu(r, &rproc_list, node) { 2055fec47d86SDave Gerlach if (r->dev.parent && r->dev.parent->of_node == np) { 2056fbb6aacbSBjorn Andersson /* prevent underlying implementation from being removed */ 2057fbb6aacbSBjorn Andersson if (!try_module_get(r->dev.parent->driver->owner)) { 2058fbb6aacbSBjorn Andersson dev_err(&r->dev, "can't get owner\n"); 2059fbb6aacbSBjorn Andersson break; 2060fbb6aacbSBjorn Andersson } 2061fbb6aacbSBjorn Andersson 2062fec47d86SDave Gerlach rproc = r; 2063fec47d86SDave Gerlach get_device(&rproc->dev); 2064fec47d86SDave Gerlach break; 2065fec47d86SDave Gerlach } 2066fec47d86SDave Gerlach } 2067c0abe2caSBjorn Andersson rcu_read_unlock(); 2068fec47d86SDave Gerlach 2069fec47d86SDave Gerlach of_node_put(np); 2070fec47d86SDave Gerlach 2071fec47d86SDave Gerlach return rproc; 2072fec47d86SDave Gerlach } 20738de3dbd0SOhad Ben-Cohen #else 20748de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle) 20758de3dbd0SOhad Ben-Cohen { 20768de3dbd0SOhad Ben-Cohen return NULL; 20778de3dbd0SOhad Ben-Cohen } 20788de3dbd0SOhad Ben-Cohen #endif 2079fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle); 2080fec47d86SDave Gerlach 208188d3a136SMathieu Poirier static int rproc_validate(struct rproc *rproc) 208288d3a136SMathieu Poirier { 208388d3a136SMathieu Poirier switch (rproc->state) { 208488d3a136SMathieu Poirier case RPROC_OFFLINE: 208588d3a136SMathieu Poirier /* 208688d3a136SMathieu Poirier * An offline processor without a start() 208788d3a136SMathieu Poirier * function makes no sense. 208888d3a136SMathieu Poirier */ 208988d3a136SMathieu Poirier if (!rproc->ops->start) 209088d3a136SMathieu Poirier return -EINVAL; 209188d3a136SMathieu Poirier break; 209288d3a136SMathieu Poirier case RPROC_DETACHED: 209388d3a136SMathieu Poirier /* 209488d3a136SMathieu Poirier * A remote processor in a detached state without an 209588d3a136SMathieu Poirier * attach() function makes not sense. 209688d3a136SMathieu Poirier */ 209788d3a136SMathieu Poirier if (!rproc->ops->attach) 209888d3a136SMathieu Poirier return -EINVAL; 209988d3a136SMathieu Poirier /* 210088d3a136SMathieu Poirier * When attaching to a remote processor the device memory 210188d3a136SMathieu Poirier * is already available and as such there is no need to have a 210288d3a136SMathieu Poirier * cached table. 210388d3a136SMathieu Poirier */ 210488d3a136SMathieu Poirier if (rproc->cached_table) 210588d3a136SMathieu Poirier return -EINVAL; 210688d3a136SMathieu Poirier break; 210788d3a136SMathieu Poirier default: 210888d3a136SMathieu Poirier /* 210988d3a136SMathieu Poirier * When adding a remote processor, the state of the device 211088d3a136SMathieu Poirier * can be offline or detached, nothing else. 211188d3a136SMathieu Poirier */ 211288d3a136SMathieu Poirier return -EINVAL; 211388d3a136SMathieu Poirier } 211488d3a136SMathieu Poirier 211588d3a136SMathieu Poirier return 0; 211688d3a136SMathieu Poirier } 211788d3a136SMathieu Poirier 2118fec47d86SDave Gerlach /** 2119160e7c84SOhad Ben-Cohen * rproc_add() - register a remote processor 2120400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle to register 2121400e64dfSOhad Ben-Cohen * 2122400e64dfSOhad Ben-Cohen * Registers @rproc with the remoteproc framework, after it has been 2123400e64dfSOhad Ben-Cohen * allocated with rproc_alloc(). 2124400e64dfSOhad Ben-Cohen * 2125400e64dfSOhad Ben-Cohen * This is called by the platform-specific rproc implementation, whenever 2126400e64dfSOhad Ben-Cohen * a new remote processor device is probed. 2127400e64dfSOhad Ben-Cohen * 2128400e64dfSOhad Ben-Cohen * Returns 0 on success and an appropriate error code otherwise. 2129400e64dfSOhad Ben-Cohen * 2130400e64dfSOhad Ben-Cohen * Note: this function initiates an asynchronous firmware loading 2131400e64dfSOhad Ben-Cohen * context, which will look for virtio devices supported by the rproc's 2132400e64dfSOhad Ben-Cohen * firmware. 2133400e64dfSOhad Ben-Cohen * 2134400e64dfSOhad Ben-Cohen * If found, those virtio devices will be created and added, so as a result 21357a186941SOhad Ben-Cohen * of registering this remote processor, additional virtio drivers might be 2136400e64dfSOhad Ben-Cohen * probed. 2137400e64dfSOhad Ben-Cohen */ 2138160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc) 2139400e64dfSOhad Ben-Cohen { 2140b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 214170b85ef8SFernando Guzman Lugo int ret; 2142400e64dfSOhad Ben-Cohen 2143b5ab5e24SOhad Ben-Cohen ret = device_add(dev); 2144b5ab5e24SOhad Ben-Cohen if (ret < 0) 2145b5ab5e24SOhad Ben-Cohen return ret; 2146400e64dfSOhad Ben-Cohen 214788d3a136SMathieu Poirier ret = rproc_validate(rproc); 214888d3a136SMathieu Poirier if (ret < 0) 214988d3a136SMathieu Poirier return ret; 215088d3a136SMathieu Poirier 2151b5ab5e24SOhad Ben-Cohen dev_info(dev, "%s is available\n", rproc->name); 2152400e64dfSOhad Ben-Cohen 2153400e64dfSOhad Ben-Cohen /* create debugfs entries */ 2154400e64dfSOhad Ben-Cohen rproc_create_debug_dir(rproc); 21557a20c64dSSarangdhar Joshi 2156*4a4dca19SMathieu Poirier /* 2157*4a4dca19SMathieu Poirier * Remind ourselves the remote processor has been attached to rather 2158*4a4dca19SMathieu Poirier * than booted by the remoteproc core. This is important because the 2159*4a4dca19SMathieu Poirier * RPROC_DETACHED state will be lost as soon as the remote processor 2160*4a4dca19SMathieu Poirier * has been attached to. Used in firmware_show() and reset in 2161*4a4dca19SMathieu Poirier * rproc_stop(). 2162*4a4dca19SMathieu Poirier */ 2163*4a4dca19SMathieu Poirier if (rproc->state == RPROC_DETACHED) 2164*4a4dca19SMathieu Poirier rproc->autonomous = true; 2165*4a4dca19SMathieu Poirier 21667a20c64dSSarangdhar Joshi /* if rproc is marked always-on, request it to boot */ 21677a20c64dSSarangdhar Joshi if (rproc->auto_boot) { 21685e6533f7SSarangdhar Joshi ret = rproc_trigger_auto_boot(rproc); 2169d2e12e66SDave Gerlach if (ret < 0) 2170d2e12e66SDave Gerlach return ret; 21717a20c64dSSarangdhar Joshi } 2172400e64dfSOhad Ben-Cohen 2173d2e12e66SDave Gerlach /* expose to rproc_get_by_phandle users */ 2174d2e12e66SDave Gerlach mutex_lock(&rproc_list_mutex); 2175c0abe2caSBjorn Andersson list_add_rcu(&rproc->node, &rproc_list); 2176d2e12e66SDave Gerlach mutex_unlock(&rproc_list_mutex); 2177d2e12e66SDave Gerlach 2178d2e12e66SDave Gerlach return 0; 2179400e64dfSOhad Ben-Cohen } 2180160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add); 2181400e64dfSOhad Ben-Cohen 2182305ac5a7SPaul Cercueil static void devm_rproc_remove(void *rproc) 2183305ac5a7SPaul Cercueil { 2184305ac5a7SPaul Cercueil rproc_del(rproc); 2185305ac5a7SPaul Cercueil } 2186305ac5a7SPaul Cercueil 2187305ac5a7SPaul Cercueil /** 2188305ac5a7SPaul Cercueil * devm_rproc_add() - resource managed rproc_add() 2189305ac5a7SPaul Cercueil * @dev: the underlying device 2190305ac5a7SPaul Cercueil * @rproc: the remote processor handle to register 2191305ac5a7SPaul Cercueil * 2192305ac5a7SPaul Cercueil * This function performs like rproc_add() but the registered rproc device will 2193305ac5a7SPaul Cercueil * automatically be removed on driver detach. 2194305ac5a7SPaul Cercueil * 2195305ac5a7SPaul Cercueil * Returns: 0 on success, negative errno on failure 2196305ac5a7SPaul Cercueil */ 2197305ac5a7SPaul Cercueil int devm_rproc_add(struct device *dev, struct rproc *rproc) 2198305ac5a7SPaul Cercueil { 2199305ac5a7SPaul Cercueil int err; 2200305ac5a7SPaul Cercueil 2201305ac5a7SPaul Cercueil err = rproc_add(rproc); 2202305ac5a7SPaul Cercueil if (err) 2203305ac5a7SPaul Cercueil return err; 2204305ac5a7SPaul Cercueil 2205305ac5a7SPaul Cercueil return devm_add_action_or_reset(dev, devm_rproc_remove, rproc); 2206305ac5a7SPaul Cercueil } 2207305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_add); 2208305ac5a7SPaul Cercueil 2209400e64dfSOhad Ben-Cohen /** 2210b5ab5e24SOhad Ben-Cohen * rproc_type_release() - release a remote processor instance 2211b5ab5e24SOhad Ben-Cohen * @dev: the rproc's device 2212b5ab5e24SOhad Ben-Cohen * 2213b5ab5e24SOhad Ben-Cohen * This function should _never_ be called directly. 2214b5ab5e24SOhad Ben-Cohen * 2215b5ab5e24SOhad Ben-Cohen * It will be called by the driver core when no one holds a valid pointer 2216b5ab5e24SOhad Ben-Cohen * to @dev anymore. 2217b5ab5e24SOhad Ben-Cohen */ 2218b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev) 2219b5ab5e24SOhad Ben-Cohen { 2220b5ab5e24SOhad Ben-Cohen struct rproc *rproc = container_of(dev, struct rproc, dev); 2221b5ab5e24SOhad Ben-Cohen 22227183a2a7SOhad Ben-Cohen dev_info(&rproc->dev, "releasing %s\n", rproc->name); 22237183a2a7SOhad Ben-Cohen 2224b5ab5e24SOhad Ben-Cohen idr_destroy(&rproc->notifyids); 2225b5ab5e24SOhad Ben-Cohen 2226b5ab5e24SOhad Ben-Cohen if (rproc->index >= 0) 2227b5ab5e24SOhad Ben-Cohen ida_simple_remove(&rproc_dev_index, rproc->index); 2228b5ab5e24SOhad Ben-Cohen 22291487dedaSMathieu Poirier kfree_const(rproc->firmware); 2230db655278SSuman Anna kfree_const(rproc->name); 2231fb98e2bdSBjorn Andersson kfree(rproc->ops); 2232b5ab5e24SOhad Ben-Cohen kfree(rproc); 2233b5ab5e24SOhad Ben-Cohen } 2234b5ab5e24SOhad Ben-Cohen 2235c42ca04dSBhumika Goyal static const struct device_type rproc_type = { 2236b5ab5e24SOhad Ben-Cohen .name = "remoteproc", 2237b5ab5e24SOhad Ben-Cohen .release = rproc_type_release, 2238b5ab5e24SOhad Ben-Cohen }; 2239400e64dfSOhad Ben-Cohen 22400c2ae2b1SMathieu Poirier static int rproc_alloc_firmware(struct rproc *rproc, 22410c2ae2b1SMathieu Poirier const char *name, const char *firmware) 22420c2ae2b1SMathieu Poirier { 22431487dedaSMathieu Poirier const char *p; 22440c2ae2b1SMathieu Poirier 22450c2ae2b1SMathieu Poirier /* 22469d5f82c8SMathieu Poirier * Allocate a firmware name if the caller gave us one to work 22479d5f82c8SMathieu Poirier * with. Otherwise construct a new one using a default pattern. 22480c2ae2b1SMathieu Poirier */ 22499d5f82c8SMathieu Poirier if (firmware) 22501487dedaSMathieu Poirier p = kstrdup_const(firmware, GFP_KERNEL); 22519d5f82c8SMathieu Poirier else 22529d5f82c8SMathieu Poirier p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name); 22534df4f8beSMathieu Poirier 22540c2ae2b1SMathieu Poirier if (!p) 22550c2ae2b1SMathieu Poirier return -ENOMEM; 22560c2ae2b1SMathieu Poirier 22570c2ae2b1SMathieu Poirier rproc->firmware = p; 22580c2ae2b1SMathieu Poirier 22590c2ae2b1SMathieu Poirier return 0; 22600c2ae2b1SMathieu Poirier } 22610c2ae2b1SMathieu Poirier 2262bf860aa1SMathieu Poirier static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops) 2263bf860aa1SMathieu Poirier { 2264bf860aa1SMathieu Poirier rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); 2265bf860aa1SMathieu Poirier if (!rproc->ops) 2266bf860aa1SMathieu Poirier return -ENOMEM; 2267bf860aa1SMathieu Poirier 2268bf860aa1SMathieu Poirier if (rproc->ops->load) 2269bf860aa1SMathieu Poirier return 0; 2270bf860aa1SMathieu Poirier 2271bf860aa1SMathieu Poirier /* Default to ELF loader if no load function is specified */ 2272bf860aa1SMathieu Poirier rproc->ops->load = rproc_elf_load_segments; 2273bf860aa1SMathieu Poirier rproc->ops->parse_fw = rproc_elf_load_rsc_table; 2274bf860aa1SMathieu Poirier rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; 2275e29ff72bSClement Leger rproc->ops->sanity_check = rproc_elf_sanity_check; 2276bf860aa1SMathieu Poirier rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; 2277bf860aa1SMathieu Poirier 2278bf860aa1SMathieu Poirier return 0; 2279bf860aa1SMathieu Poirier } 2280bf860aa1SMathieu Poirier 2281400e64dfSOhad Ben-Cohen /** 2282400e64dfSOhad Ben-Cohen * rproc_alloc() - allocate a remote processor handle 2283400e64dfSOhad Ben-Cohen * @dev: the underlying device 2284400e64dfSOhad Ben-Cohen * @name: name of this remote processor 2285400e64dfSOhad Ben-Cohen * @ops: platform-specific handlers (mainly start/stop) 22868b4aec9aSRobert Tivy * @firmware: name of firmware file to load, can be NULL 2287400e64dfSOhad Ben-Cohen * @len: length of private data needed by the rproc driver (in bytes) 2288400e64dfSOhad Ben-Cohen * 2289400e64dfSOhad Ben-Cohen * Allocates a new remote processor handle, but does not register 22908b4aec9aSRobert Tivy * it yet. if @firmware is NULL, a default name is used. 2291400e64dfSOhad Ben-Cohen * 2292400e64dfSOhad Ben-Cohen * This function should be used by rproc implementations during initialization 2293400e64dfSOhad Ben-Cohen * of the remote processor. 2294400e64dfSOhad Ben-Cohen * 2295400e64dfSOhad Ben-Cohen * After creating an rproc handle using this function, and when ready, 2296160e7c84SOhad Ben-Cohen * implementations should then call rproc_add() to complete 2297400e64dfSOhad Ben-Cohen * the registration of the remote processor. 2298400e64dfSOhad Ben-Cohen * 2299400e64dfSOhad Ben-Cohen * On success the new rproc is returned, and on failure, NULL. 2300400e64dfSOhad Ben-Cohen * 2301400e64dfSOhad Ben-Cohen * Note: _never_ directly deallocate @rproc, even if it was not registered 2302433c0e04SBjorn Andersson * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). 2303400e64dfSOhad Ben-Cohen */ 2304400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name, 2305400e64dfSOhad Ben-Cohen const struct rproc_ops *ops, 2306400e64dfSOhad Ben-Cohen const char *firmware, int len) 2307400e64dfSOhad Ben-Cohen { 2308400e64dfSOhad Ben-Cohen struct rproc *rproc; 2309400e64dfSOhad Ben-Cohen 2310400e64dfSOhad Ben-Cohen if (!dev || !name || !ops) 2311400e64dfSOhad Ben-Cohen return NULL; 2312400e64dfSOhad Ben-Cohen 23130f57dc6aSMatt Redfearn rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); 23140c2ae2b1SMathieu Poirier if (!rproc) 23150f57dc6aSMatt Redfearn return NULL; 23160c2ae2b1SMathieu Poirier 2317400e64dfSOhad Ben-Cohen rproc->priv = &rproc[1]; 2318ddf71187SBjorn Andersson rproc->auto_boot = true; 2319418fd787SClement Leger rproc->elf_class = ELFCLASSNONE; 2320418fd787SClement Leger rproc->elf_machine = EM_NONE; 2321400e64dfSOhad Ben-Cohen 2322b5ab5e24SOhad Ben-Cohen device_initialize(&rproc->dev); 2323b5ab5e24SOhad Ben-Cohen rproc->dev.parent = dev; 2324b5ab5e24SOhad Ben-Cohen rproc->dev.type = &rproc_type; 23252aefbef0SMatt Redfearn rproc->dev.class = &rproc_class; 23267c89717fSBjorn Andersson rproc->dev.driver_data = rproc; 23276442df49SAlex Elder idr_init(&rproc->notifyids); 2328b5ab5e24SOhad Ben-Cohen 2329db655278SSuman Anna rproc->name = kstrdup_const(name, GFP_KERNEL); 2330db655278SSuman Anna if (!rproc->name) 2331db655278SSuman Anna goto put_device; 2332db655278SSuman Anna 2333226f5db4SMathieu Poirier if (rproc_alloc_firmware(rproc, name, firmware)) 2334226f5db4SMathieu Poirier goto put_device; 2335226f5db4SMathieu Poirier 2336226f5db4SMathieu Poirier if (rproc_alloc_ops(rproc, ops)) 2337226f5db4SMathieu Poirier goto put_device; 2338226f5db4SMathieu Poirier 2339b5ab5e24SOhad Ben-Cohen /* Assign a unique device index and name */ 2340b5ab5e24SOhad Ben-Cohen rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL); 2341b5ab5e24SOhad Ben-Cohen if (rproc->index < 0) { 2342b5ab5e24SOhad Ben-Cohen dev_err(dev, "ida_simple_get failed: %d\n", rproc->index); 2343226f5db4SMathieu Poirier goto put_device; 2344b5ab5e24SOhad Ben-Cohen } 2345b5ab5e24SOhad Ben-Cohen 2346b5ab5e24SOhad Ben-Cohen dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); 2347b5ab5e24SOhad Ben-Cohen 2348400e64dfSOhad Ben-Cohen atomic_set(&rproc->power, 0); 2349400e64dfSOhad Ben-Cohen 2350400e64dfSOhad Ben-Cohen mutex_init(&rproc->lock); 2351400e64dfSOhad Ben-Cohen 2352400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->carveouts); 2353400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->mappings); 2354400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->traces); 23557a186941SOhad Ben-Cohen INIT_LIST_HEAD(&rproc->rvdevs); 23567bdc9650SBjorn Andersson INIT_LIST_HEAD(&rproc->subdevs); 23572666ca91SSarangdhar Joshi INIT_LIST_HEAD(&rproc->dump_segments); 2358400e64dfSOhad Ben-Cohen 23598afd519cSFernando Guzman Lugo INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); 23608afd519cSFernando Guzman Lugo 2361400e64dfSOhad Ben-Cohen rproc->state = RPROC_OFFLINE; 2362400e64dfSOhad Ben-Cohen 2363400e64dfSOhad Ben-Cohen return rproc; 23640c2ae2b1SMathieu Poirier 2365226f5db4SMathieu Poirier put_device: 2366226f5db4SMathieu Poirier put_device(&rproc->dev); 23670c2ae2b1SMathieu Poirier return NULL; 2368400e64dfSOhad Ben-Cohen } 2369400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc); 2370400e64dfSOhad Ben-Cohen 2371400e64dfSOhad Ben-Cohen /** 2372433c0e04SBjorn Andersson * rproc_free() - unroll rproc_alloc() 2373433c0e04SBjorn Andersson * @rproc: the remote processor handle 2374433c0e04SBjorn Andersson * 2375433c0e04SBjorn Andersson * This function decrements the rproc dev refcount. 2376433c0e04SBjorn Andersson * 2377433c0e04SBjorn Andersson * If no one holds any reference to rproc anymore, then its refcount would 2378433c0e04SBjorn Andersson * now drop to zero, and it would be freed. 2379433c0e04SBjorn Andersson */ 2380433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc) 2381433c0e04SBjorn Andersson { 2382433c0e04SBjorn Andersson put_device(&rproc->dev); 2383433c0e04SBjorn Andersson } 2384433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free); 2385433c0e04SBjorn Andersson 2386433c0e04SBjorn Andersson /** 2387433c0e04SBjorn Andersson * rproc_put() - release rproc reference 2388400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle 2389400e64dfSOhad Ben-Cohen * 2390c6b5a276SOhad Ben-Cohen * This function decrements the rproc dev refcount. 2391400e64dfSOhad Ben-Cohen * 2392c6b5a276SOhad Ben-Cohen * If no one holds any reference to rproc anymore, then its refcount would 2393c6b5a276SOhad Ben-Cohen * now drop to zero, and it would be freed. 2394400e64dfSOhad Ben-Cohen */ 2395160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc) 2396400e64dfSOhad Ben-Cohen { 2397fbb6aacbSBjorn Andersson module_put(rproc->dev.parent->driver->owner); 2398b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 2399400e64dfSOhad Ben-Cohen } 2400160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put); 2401400e64dfSOhad Ben-Cohen 2402400e64dfSOhad Ben-Cohen /** 2403160e7c84SOhad Ben-Cohen * rproc_del() - unregister a remote processor 2404400e64dfSOhad Ben-Cohen * @rproc: rproc handle to unregister 2405400e64dfSOhad Ben-Cohen * 2406400e64dfSOhad Ben-Cohen * This function should be called when the platform specific rproc 2407400e64dfSOhad Ben-Cohen * implementation decides to remove the rproc device. it should 2408160e7c84SOhad Ben-Cohen * _only_ be called if a previous invocation of rproc_add() 2409400e64dfSOhad Ben-Cohen * has completed successfully. 2410400e64dfSOhad Ben-Cohen * 2411160e7c84SOhad Ben-Cohen * After rproc_del() returns, @rproc isn't freed yet, because 2412c6b5a276SOhad Ben-Cohen * of the outstanding reference created by rproc_alloc. To decrement that 2413433c0e04SBjorn Andersson * one last refcount, one still needs to call rproc_free(). 2414400e64dfSOhad Ben-Cohen * 2415400e64dfSOhad Ben-Cohen * Returns 0 on success and -EINVAL if @rproc isn't valid. 2416400e64dfSOhad Ben-Cohen */ 2417160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc) 2418400e64dfSOhad Ben-Cohen { 2419400e64dfSOhad Ben-Cohen if (!rproc) 2420400e64dfSOhad Ben-Cohen return -EINVAL; 2421400e64dfSOhad Ben-Cohen 2422ddf71187SBjorn Andersson /* if rproc is marked always-on, rproc_add() booted it */ 2423ddf71187SBjorn Andersson /* TODO: make sure this works with rproc->power > 1 */ 2424ddf71187SBjorn Andersson if (rproc->auto_boot) 2425ddf71187SBjorn Andersson rproc_shutdown(rproc); 2426ddf71187SBjorn Andersson 24272099c77dSSarangdhar Joshi mutex_lock(&rproc->lock); 24282099c77dSSarangdhar Joshi rproc->state = RPROC_DELETED; 24292099c77dSSarangdhar Joshi mutex_unlock(&rproc->lock); 24302099c77dSSarangdhar Joshi 2431b003d45bSSarangdhar Joshi rproc_delete_debug_dir(rproc); 2432b003d45bSSarangdhar Joshi 2433fec47d86SDave Gerlach /* the rproc is downref'ed as soon as it's removed from the klist */ 2434fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 2435c0abe2caSBjorn Andersson list_del_rcu(&rproc->node); 2436fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 2437fec47d86SDave Gerlach 2438c0abe2caSBjorn Andersson /* Ensure that no readers of rproc_list are still active */ 2439c0abe2caSBjorn Andersson synchronize_rcu(); 2440c0abe2caSBjorn Andersson 2441b5ab5e24SOhad Ben-Cohen device_del(&rproc->dev); 2442400e64dfSOhad Ben-Cohen 2443400e64dfSOhad Ben-Cohen return 0; 2444400e64dfSOhad Ben-Cohen } 2445160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del); 2446400e64dfSOhad Ben-Cohen 2447305ac5a7SPaul Cercueil static void devm_rproc_free(struct device *dev, void *res) 2448305ac5a7SPaul Cercueil { 2449305ac5a7SPaul Cercueil rproc_free(*(struct rproc **)res); 2450305ac5a7SPaul Cercueil } 2451305ac5a7SPaul Cercueil 2452305ac5a7SPaul Cercueil /** 2453305ac5a7SPaul Cercueil * devm_rproc_alloc() - resource managed rproc_alloc() 2454305ac5a7SPaul Cercueil * @dev: the underlying device 2455305ac5a7SPaul Cercueil * @name: name of this remote processor 2456305ac5a7SPaul Cercueil * @ops: platform-specific handlers (mainly start/stop) 2457305ac5a7SPaul Cercueil * @firmware: name of firmware file to load, can be NULL 2458305ac5a7SPaul Cercueil * @len: length of private data needed by the rproc driver (in bytes) 2459305ac5a7SPaul Cercueil * 2460305ac5a7SPaul Cercueil * This function performs like rproc_alloc() but the acquired rproc device will 2461305ac5a7SPaul Cercueil * automatically be released on driver detach. 2462305ac5a7SPaul Cercueil * 2463305ac5a7SPaul Cercueil * Returns: new rproc instance, or NULL on failure 2464305ac5a7SPaul Cercueil */ 2465305ac5a7SPaul Cercueil struct rproc *devm_rproc_alloc(struct device *dev, const char *name, 2466305ac5a7SPaul Cercueil const struct rproc_ops *ops, 2467305ac5a7SPaul Cercueil const char *firmware, int len) 2468305ac5a7SPaul Cercueil { 2469305ac5a7SPaul Cercueil struct rproc **ptr, *rproc; 2470305ac5a7SPaul Cercueil 2471305ac5a7SPaul Cercueil ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL); 2472305ac5a7SPaul Cercueil if (!ptr) 24737dcef398SDan Carpenter return NULL; 2474305ac5a7SPaul Cercueil 2475305ac5a7SPaul Cercueil rproc = rproc_alloc(dev, name, ops, firmware, len); 2476305ac5a7SPaul Cercueil if (rproc) { 2477305ac5a7SPaul Cercueil *ptr = rproc; 2478305ac5a7SPaul Cercueil devres_add(dev, ptr); 2479305ac5a7SPaul Cercueil } else { 2480305ac5a7SPaul Cercueil devres_free(ptr); 2481305ac5a7SPaul Cercueil } 2482305ac5a7SPaul Cercueil 2483305ac5a7SPaul Cercueil return rproc; 2484305ac5a7SPaul Cercueil } 2485305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_alloc); 2486305ac5a7SPaul Cercueil 24878afd519cSFernando Guzman Lugo /** 24887bdc9650SBjorn Andersson * rproc_add_subdev() - add a subdevice to a remoteproc 24897bdc9650SBjorn Andersson * @rproc: rproc handle to add the subdevice to 24907bdc9650SBjorn Andersson * @subdev: subdev handle to register 24914902676fSBjorn Andersson * 24924902676fSBjorn Andersson * Caller is responsible for populating optional subdevice function pointers. 24937bdc9650SBjorn Andersson */ 24944902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 24957bdc9650SBjorn Andersson { 24967bdc9650SBjorn Andersson list_add_tail(&subdev->node, &rproc->subdevs); 24977bdc9650SBjorn Andersson } 24987bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev); 24997bdc9650SBjorn Andersson 25007bdc9650SBjorn Andersson /** 25017bdc9650SBjorn Andersson * rproc_remove_subdev() - remove a subdevice from a remoteproc 25027bdc9650SBjorn Andersson * @rproc: rproc handle to remove the subdevice from 25037bdc9650SBjorn Andersson * @subdev: subdev handle, previously registered with rproc_add_subdev() 25047bdc9650SBjorn Andersson */ 25057bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 25067bdc9650SBjorn Andersson { 25077bdc9650SBjorn Andersson list_del(&subdev->node); 25087bdc9650SBjorn Andersson } 25097bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev); 25107bdc9650SBjorn Andersson 25117bdc9650SBjorn Andersson /** 25127c89717fSBjorn Andersson * rproc_get_by_child() - acquire rproc handle of @dev's ancestor 25137c89717fSBjorn Andersson * @dev: child device to find ancestor of 25147c89717fSBjorn Andersson * 25157c89717fSBjorn Andersson * Returns the ancestor rproc instance, or NULL if not found. 25167c89717fSBjorn Andersson */ 25177c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev) 25187c89717fSBjorn Andersson { 25197c89717fSBjorn Andersson for (dev = dev->parent; dev; dev = dev->parent) { 25207c89717fSBjorn Andersson if (dev->type == &rproc_type) 25217c89717fSBjorn Andersson return dev->driver_data; 25227c89717fSBjorn Andersson } 25237c89717fSBjorn Andersson 25247c89717fSBjorn Andersson return NULL; 25257c89717fSBjorn Andersson } 25267c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child); 25277c89717fSBjorn Andersson 25287c89717fSBjorn Andersson /** 25298afd519cSFernando Guzman Lugo * rproc_report_crash() - rproc crash reporter function 25308afd519cSFernando Guzman Lugo * @rproc: remote processor 25318afd519cSFernando Guzman Lugo * @type: crash type 25328afd519cSFernando Guzman Lugo * 25338afd519cSFernando Guzman Lugo * This function must be called every time a crash is detected by the low-level 25348afd519cSFernando Guzman Lugo * drivers implementing a specific remoteproc. This should not be called from a 25358afd519cSFernando Guzman Lugo * non-remoteproc driver. 25368afd519cSFernando Guzman Lugo * 25378afd519cSFernando Guzman Lugo * This function can be called from atomic/interrupt context. 25388afd519cSFernando Guzman Lugo */ 25398afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) 25408afd519cSFernando Guzman Lugo { 25418afd519cSFernando Guzman Lugo if (!rproc) { 25428afd519cSFernando Guzman Lugo pr_err("NULL rproc pointer\n"); 25438afd519cSFernando Guzman Lugo return; 25448afd519cSFernando Guzman Lugo } 25458afd519cSFernando Guzman Lugo 2546a781e5aaSRishabh Bhatnagar /* Prevent suspend while the remoteproc is being recovered */ 2547a781e5aaSRishabh Bhatnagar pm_stay_awake(rproc->dev.parent); 2548a781e5aaSRishabh Bhatnagar 25498afd519cSFernando Guzman Lugo dev_err(&rproc->dev, "crash detected in %s: type %s\n", 25508afd519cSFernando Guzman Lugo rproc->name, rproc_crash_to_string(type)); 25518afd519cSFernando Guzman Lugo 25528afd519cSFernando Guzman Lugo /* create a new task to handle the error */ 25538afd519cSFernando Guzman Lugo schedule_work(&rproc->crash_handler); 25548afd519cSFernando Guzman Lugo } 25558afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash); 25568afd519cSFernando Guzman Lugo 2557dc5192c4SBjorn Andersson static int rproc_panic_handler(struct notifier_block *nb, unsigned long event, 2558dc5192c4SBjorn Andersson void *ptr) 2559dc5192c4SBjorn Andersson { 2560dc5192c4SBjorn Andersson unsigned int longest = 0; 2561dc5192c4SBjorn Andersson struct rproc *rproc; 2562dc5192c4SBjorn Andersson unsigned int d; 2563dc5192c4SBjorn Andersson 2564dc5192c4SBjorn Andersson rcu_read_lock(); 2565dc5192c4SBjorn Andersson list_for_each_entry_rcu(rproc, &rproc_list, node) { 2566dc5192c4SBjorn Andersson if (!rproc->ops->panic || rproc->state != RPROC_RUNNING) 2567dc5192c4SBjorn Andersson continue; 2568dc5192c4SBjorn Andersson 2569dc5192c4SBjorn Andersson d = rproc->ops->panic(rproc); 2570dc5192c4SBjorn Andersson longest = max(longest, d); 2571dc5192c4SBjorn Andersson } 2572dc5192c4SBjorn Andersson rcu_read_unlock(); 2573dc5192c4SBjorn Andersson 2574dc5192c4SBjorn Andersson /* 2575dc5192c4SBjorn Andersson * Delay for the longest requested duration before returning. This can 2576dc5192c4SBjorn Andersson * be used by the remoteproc drivers to give the remote processor time 2577dc5192c4SBjorn Andersson * to perform any requested operations (such as flush caches), when 2578dc5192c4SBjorn Andersson * it's not possible to signal the Linux side due to the panic. 2579dc5192c4SBjorn Andersson */ 2580dc5192c4SBjorn Andersson mdelay(longest); 2581dc5192c4SBjorn Andersson 2582dc5192c4SBjorn Andersson return NOTIFY_DONE; 2583dc5192c4SBjorn Andersson } 2584dc5192c4SBjorn Andersson 2585dc5192c4SBjorn Andersson static void __init rproc_init_panic(void) 2586dc5192c4SBjorn Andersson { 2587dc5192c4SBjorn Andersson rproc_panic_nb.notifier_call = rproc_panic_handler; 2588dc5192c4SBjorn Andersson atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb); 2589dc5192c4SBjorn Andersson } 2590dc5192c4SBjorn Andersson 2591dc5192c4SBjorn Andersson static void __exit rproc_exit_panic(void) 2592dc5192c4SBjorn Andersson { 2593dc5192c4SBjorn Andersson atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb); 2594dc5192c4SBjorn Andersson } 2595dc5192c4SBjorn Andersson 2596400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void) 2597400e64dfSOhad Ben-Cohen { 25982aefbef0SMatt Redfearn rproc_init_sysfs(); 2599400e64dfSOhad Ben-Cohen rproc_init_debugfs(); 2600dc5192c4SBjorn Andersson rproc_init_panic(); 2601b5ab5e24SOhad Ben-Cohen 2602400e64dfSOhad Ben-Cohen return 0; 2603400e64dfSOhad Ben-Cohen } 2604a8f40111SBrandon Maier subsys_initcall(remoteproc_init); 2605400e64dfSOhad Ben-Cohen 2606400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void) 2607400e64dfSOhad Ben-Cohen { 2608f42f79afSSuman Anna ida_destroy(&rproc_dev_index); 2609f42f79afSSuman Anna 2610dc5192c4SBjorn Andersson rproc_exit_panic(); 2611400e64dfSOhad Ben-Cohen rproc_exit_debugfs(); 26122aefbef0SMatt Redfearn rproc_exit_sysfs(); 2613400e64dfSOhad Ben-Cohen } 2614400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit); 2615400e64dfSOhad Ben-Cohen 2616400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2"); 2617400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework"); 2618