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 414c0d63157SSjur Brændeland /* reset resource entry info */ 415c0d63157SSjur Brændeland rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; 416c0d63157SSjur Brændeland rsc->vring[idx].da = 0; 417c0d63157SSjur Brændeland rsc->vring[idx].notifyid = -1; 4187a186941SOhad Ben-Cohen } 4197a186941SOhad Ben-Cohen 4206f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev) 421f5bcb353SBjorn Andersson { 422f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 423f5bcb353SBjorn Andersson 424f5bcb353SBjorn Andersson return rproc_add_virtio_dev(rvdev, rvdev->id); 425f5bcb353SBjorn Andersson } 426f5bcb353SBjorn Andersson 4276f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed) 428f5bcb353SBjorn Andersson { 429f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 430d4c036feSLoic Pallardy int ret; 431f5bcb353SBjorn Andersson 432d4c036feSLoic Pallardy ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev); 433d4c036feSLoic Pallardy if (ret) 434d4c036feSLoic Pallardy dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret); 435f5bcb353SBjorn Andersson } 436f5bcb353SBjorn Andersson 437400e64dfSOhad Ben-Cohen /** 438086d0872SLoic Pallardy * rproc_rvdev_release() - release the existence of a rvdev 439086d0872SLoic Pallardy * 440086d0872SLoic Pallardy * @dev: the subdevice's dev 441086d0872SLoic Pallardy */ 442086d0872SLoic Pallardy static void rproc_rvdev_release(struct device *dev) 443086d0872SLoic Pallardy { 444086d0872SLoic Pallardy struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev); 445086d0872SLoic Pallardy 446086d0872SLoic Pallardy of_reserved_mem_device_release(dev); 447086d0872SLoic Pallardy 448086d0872SLoic Pallardy kfree(rvdev); 449086d0872SLoic Pallardy } 450086d0872SLoic Pallardy 451086d0872SLoic Pallardy /** 452fd2c15ecSOhad Ben-Cohen * rproc_handle_vdev() - handle a vdev fw resource 453400e64dfSOhad Ben-Cohen * @rproc: the remote processor 454400e64dfSOhad Ben-Cohen * @rsc: the vring resource descriptor 4552e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 456fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 457400e64dfSOhad Ben-Cohen * 4587a186941SOhad Ben-Cohen * This resource entry requests the host to statically register a virtio 4597a186941SOhad Ben-Cohen * device (vdev), and setup everything needed to support it. It contains 4607a186941SOhad Ben-Cohen * everything needed to make it possible: the virtio device id, virtio 4617a186941SOhad Ben-Cohen * device features, vrings information, virtio config space, etc... 462400e64dfSOhad Ben-Cohen * 4637a186941SOhad Ben-Cohen * Before registering the vdev, the vrings are allocated from non-cacheable 4647a186941SOhad Ben-Cohen * physically contiguous memory. Currently we only support two vrings per 4657a186941SOhad Ben-Cohen * remote processor (temporary limitation). We might also want to consider 4667a186941SOhad Ben-Cohen * doing the vring allocation only later when ->find_vqs() is invoked, and 4677a186941SOhad Ben-Cohen * then release them upon ->del_vqs(). 468400e64dfSOhad Ben-Cohen * 4697a186941SOhad Ben-Cohen * Note: @da is currently not really handled correctly: we dynamically 4707a186941SOhad Ben-Cohen * allocate it using the DMA API, ignoring requested hard coded addresses, 4717a186941SOhad Ben-Cohen * and we don't take care of any required IOMMU programming. This is all 4727a186941SOhad Ben-Cohen * going to be taken care of when the generic iommu-based DMA API will be 4737a186941SOhad Ben-Cohen * merged. Meanwhile, statically-addressed iommu-based firmware images should 4747a186941SOhad Ben-Cohen * use RSC_DEVMEM resource entries to map their required @da to the physical 4757a186941SOhad Ben-Cohen * address of their base CMA region (ouch, hacky!). 476400e64dfSOhad Ben-Cohen * 477400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 478400e64dfSOhad Ben-Cohen */ 479fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc, 480a2b950acSOhad Ben-Cohen int offset, int avail) 481400e64dfSOhad Ben-Cohen { 482b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 4837a186941SOhad Ben-Cohen struct rproc_vdev *rvdev; 4847a186941SOhad Ben-Cohen int i, ret; 485086d0872SLoic Pallardy char name[16]; 486fd2c15ecSOhad Ben-Cohen 487fd2c15ecSOhad Ben-Cohen /* make sure resource isn't truncated */ 488c8784657SGustavo A. R. Silva if (struct_size(rsc, vring, rsc->num_of_vrings) + rsc->config_len > 489c8784657SGustavo A. R. Silva avail) { 490b5ab5e24SOhad Ben-Cohen dev_err(dev, "vdev rsc is truncated\n"); 491fd2c15ecSOhad Ben-Cohen return -EINVAL; 492fd2c15ecSOhad Ben-Cohen } 493fd2c15ecSOhad Ben-Cohen 494fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 495fd2c15ecSOhad Ben-Cohen if (rsc->reserved[0] || rsc->reserved[1]) { 496fd2c15ecSOhad Ben-Cohen dev_err(dev, "vdev rsc has non zero reserved bytes\n"); 497fd2c15ecSOhad Ben-Cohen return -EINVAL; 498fd2c15ecSOhad Ben-Cohen } 499fd2c15ecSOhad Ben-Cohen 5009d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n", 501fd2c15ecSOhad Ben-Cohen rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); 502400e64dfSOhad Ben-Cohen 5037a186941SOhad Ben-Cohen /* we currently support only two vrings per rvdev */ 5047a186941SOhad Ben-Cohen if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { 505fd2c15ecSOhad Ben-Cohen dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); 506400e64dfSOhad Ben-Cohen return -EINVAL; 507400e64dfSOhad Ben-Cohen } 508400e64dfSOhad Ben-Cohen 509899585adSAnna, Suman rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL); 5107a186941SOhad Ben-Cohen if (!rvdev) 5117a186941SOhad Ben-Cohen return -ENOMEM; 5127a186941SOhad Ben-Cohen 513aab8d802SBjorn Andersson kref_init(&rvdev->refcount); 514aab8d802SBjorn Andersson 515f5bcb353SBjorn Andersson rvdev->id = rsc->id; 5167a186941SOhad Ben-Cohen rvdev->rproc = rproc; 517c6aed238SLoic Pallardy rvdev->index = rproc->nb_vdev++; 5187a186941SOhad Ben-Cohen 519086d0872SLoic Pallardy /* Initialise vdev subdevice */ 520086d0872SLoic Pallardy snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index); 521c774ad01SSuman Anna rvdev->dev.parent = &rproc->dev; 52272f64cabSClement Leger rvdev->dev.dma_pfn_offset = rproc->dev.parent->dma_pfn_offset; 523086d0872SLoic Pallardy rvdev->dev.release = rproc_rvdev_release; 524086d0872SLoic Pallardy dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name); 525086d0872SLoic Pallardy dev_set_drvdata(&rvdev->dev, rvdev); 526086d0872SLoic Pallardy 527086d0872SLoic Pallardy ret = device_register(&rvdev->dev); 528086d0872SLoic Pallardy if (ret) { 529086d0872SLoic Pallardy put_device(&rvdev->dev); 530086d0872SLoic Pallardy return ret; 531086d0872SLoic Pallardy } 532086d0872SLoic Pallardy /* Make device dma capable by inheriting from parent's capabilities */ 533086d0872SLoic Pallardy set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent)); 534086d0872SLoic Pallardy 535086d0872SLoic Pallardy ret = dma_coerce_mask_and_coherent(&rvdev->dev, 536086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent)); 537086d0872SLoic Pallardy if (ret) { 538086d0872SLoic Pallardy dev_warn(dev, 539086d0872SLoic Pallardy "Failed to set DMA mask %llx. Trying to continue... %x\n", 540086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent), ret); 541086d0872SLoic Pallardy } 542086d0872SLoic Pallardy 5436db20ea8SOhad Ben-Cohen /* parse the vrings */ 544fd2c15ecSOhad Ben-Cohen for (i = 0; i < rsc->num_of_vrings; i++) { 5456db20ea8SOhad Ben-Cohen ret = rproc_parse_vring(rvdev, rsc, i); 5467a186941SOhad Ben-Cohen if (ret) 5476db20ea8SOhad Ben-Cohen goto free_rvdev; 548fd2c15ecSOhad Ben-Cohen } 549fd2c15ecSOhad Ben-Cohen 550a2b950acSOhad Ben-Cohen /* remember the resource offset*/ 551a2b950acSOhad Ben-Cohen rvdev->rsc_offset = offset; 552400e64dfSOhad Ben-Cohen 553a863af5dSBjorn Andersson /* allocate the vring resources */ 554a863af5dSBjorn Andersson for (i = 0; i < rsc->num_of_vrings; i++) { 555a863af5dSBjorn Andersson ret = rproc_alloc_vring(rvdev, i); 556a863af5dSBjorn Andersson if (ret) 557a863af5dSBjorn Andersson goto unwind_vring_allocations; 558a863af5dSBjorn Andersson } 559a863af5dSBjorn Andersson 5607a186941SOhad Ben-Cohen list_add_tail(&rvdev->node, &rproc->rvdevs); 561400e64dfSOhad Ben-Cohen 5626f8b0373SAlex Elder rvdev->subdev.start = rproc_vdev_do_start; 5636f8b0373SAlex Elder rvdev->subdev.stop = rproc_vdev_do_stop; 5644902676fSBjorn Andersson 5654902676fSBjorn Andersson rproc_add_subdev(rproc, &rvdev->subdev); 566400e64dfSOhad Ben-Cohen 567400e64dfSOhad Ben-Cohen return 0; 5687a186941SOhad Ben-Cohen 569a863af5dSBjorn Andersson unwind_vring_allocations: 570a863af5dSBjorn Andersson for (i--; i >= 0; i--) 571a863af5dSBjorn Andersson rproc_free_vring(&rvdev->vring[i]); 5726db20ea8SOhad Ben-Cohen free_rvdev: 573086d0872SLoic Pallardy device_unregister(&rvdev->dev); 5747a186941SOhad Ben-Cohen return ret; 575400e64dfSOhad Ben-Cohen } 576400e64dfSOhad Ben-Cohen 577aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref) 578aab8d802SBjorn Andersson { 579aab8d802SBjorn Andersson struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount); 580a863af5dSBjorn Andersson struct rproc_vring *rvring; 581f5bcb353SBjorn Andersson struct rproc *rproc = rvdev->rproc; 582a863af5dSBjorn Andersson int id; 583a863af5dSBjorn Andersson 584a863af5dSBjorn Andersson for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) { 585a863af5dSBjorn Andersson rvring = &rvdev->vring[id]; 586a863af5dSBjorn Andersson rproc_free_vring(rvring); 587a863af5dSBjorn Andersson } 588aab8d802SBjorn Andersson 589f5bcb353SBjorn Andersson rproc_remove_subdev(rproc, &rvdev->subdev); 590aab8d802SBjorn Andersson list_del(&rvdev->node); 591086d0872SLoic Pallardy device_unregister(&rvdev->dev); 592aab8d802SBjorn Andersson } 593aab8d802SBjorn Andersson 594400e64dfSOhad Ben-Cohen /** 595400e64dfSOhad Ben-Cohen * rproc_handle_trace() - handle a shared trace buffer resource 596400e64dfSOhad Ben-Cohen * @rproc: the remote processor 597400e64dfSOhad Ben-Cohen * @rsc: the trace resource descriptor 5982e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 599fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 600400e64dfSOhad Ben-Cohen * 601400e64dfSOhad Ben-Cohen * In case the remote processor dumps trace logs into memory, 602400e64dfSOhad Ben-Cohen * export it via debugfs. 603400e64dfSOhad Ben-Cohen * 604400e64dfSOhad Ben-Cohen * Currently, the 'da' member of @rsc should contain the device address 605400e64dfSOhad Ben-Cohen * where the remote processor is dumping the traces. Later we could also 606400e64dfSOhad Ben-Cohen * support dynamically allocating this address using the generic 607400e64dfSOhad Ben-Cohen * DMA API (but currently there isn't a use case for that). 608400e64dfSOhad Ben-Cohen * 609400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 610400e64dfSOhad Ben-Cohen */ 611fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc, 612a2b950acSOhad Ben-Cohen int offset, int avail) 613400e64dfSOhad Ben-Cohen { 614a987e6b9SLoic Pallardy struct rproc_debug_trace *trace; 615b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 616400e64dfSOhad Ben-Cohen char name[15]; 617400e64dfSOhad Ben-Cohen 618fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 619b5ab5e24SOhad Ben-Cohen dev_err(dev, "trace rsc is truncated\n"); 620fd2c15ecSOhad Ben-Cohen return -EINVAL; 621fd2c15ecSOhad Ben-Cohen } 622fd2c15ecSOhad Ben-Cohen 623fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 624fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 625fd2c15ecSOhad Ben-Cohen dev_err(dev, "trace rsc has non zero reserved bytes\n"); 626fd2c15ecSOhad Ben-Cohen return -EINVAL; 627fd2c15ecSOhad Ben-Cohen } 628fd2c15ecSOhad Ben-Cohen 629400e64dfSOhad Ben-Cohen trace = kzalloc(sizeof(*trace), GFP_KERNEL); 630172e6ab1SSuman Anna if (!trace) 631400e64dfSOhad Ben-Cohen return -ENOMEM; 632400e64dfSOhad Ben-Cohen 633400e64dfSOhad Ben-Cohen /* set the trace buffer dma properties */ 634a987e6b9SLoic Pallardy trace->trace_mem.len = rsc->len; 635a987e6b9SLoic Pallardy trace->trace_mem.da = rsc->da; 636a987e6b9SLoic Pallardy 637a987e6b9SLoic Pallardy /* set pointer on rproc device */ 638a987e6b9SLoic Pallardy trace->rproc = rproc; 639400e64dfSOhad Ben-Cohen 640400e64dfSOhad Ben-Cohen /* make sure snprintf always null terminates, even if truncating */ 641400e64dfSOhad Ben-Cohen snprintf(name, sizeof(name), "trace%d", rproc->num_traces); 642400e64dfSOhad Ben-Cohen 643400e64dfSOhad Ben-Cohen /* create the debugfs entry */ 644a987e6b9SLoic Pallardy trace->tfile = rproc_create_trace_file(name, rproc, trace); 645a987e6b9SLoic Pallardy if (!trace->tfile) { 646400e64dfSOhad Ben-Cohen kfree(trace); 647400e64dfSOhad Ben-Cohen return -EINVAL; 648400e64dfSOhad Ben-Cohen } 649400e64dfSOhad Ben-Cohen 650400e64dfSOhad Ben-Cohen list_add_tail(&trace->node, &rproc->traces); 651400e64dfSOhad Ben-Cohen 652400e64dfSOhad Ben-Cohen rproc->num_traces++; 653400e64dfSOhad Ben-Cohen 654a987e6b9SLoic Pallardy dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n", 655a987e6b9SLoic Pallardy name, rsc->da, rsc->len); 656400e64dfSOhad Ben-Cohen 657400e64dfSOhad Ben-Cohen return 0; 658400e64dfSOhad Ben-Cohen } 659400e64dfSOhad Ben-Cohen 660400e64dfSOhad Ben-Cohen /** 661400e64dfSOhad Ben-Cohen * rproc_handle_devmem() - handle devmem resource entry 662400e64dfSOhad Ben-Cohen * @rproc: remote processor handle 663400e64dfSOhad Ben-Cohen * @rsc: the devmem resource entry 6642e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 665fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 666400e64dfSOhad Ben-Cohen * 667400e64dfSOhad Ben-Cohen * Remote processors commonly need to access certain on-chip peripherals. 668400e64dfSOhad Ben-Cohen * 669400e64dfSOhad Ben-Cohen * Some of these remote processors access memory via an iommu device, 670400e64dfSOhad Ben-Cohen * and might require us to configure their iommu before they can access 671400e64dfSOhad Ben-Cohen * the on-chip peripherals they need. 672400e64dfSOhad Ben-Cohen * 673400e64dfSOhad Ben-Cohen * This resource entry is a request to map such a peripheral device. 674400e64dfSOhad Ben-Cohen * 675400e64dfSOhad Ben-Cohen * These devmem entries will contain the physical address of the device in 676400e64dfSOhad Ben-Cohen * the 'pa' member. If a specific device address is expected, then 'da' will 677400e64dfSOhad Ben-Cohen * contain it (currently this is the only use case supported). 'len' will 678400e64dfSOhad Ben-Cohen * contain the size of the physical region we need to map. 679400e64dfSOhad Ben-Cohen * 680400e64dfSOhad Ben-Cohen * Currently we just "trust" those devmem entries to contain valid physical 681400e64dfSOhad Ben-Cohen * addresses, but this is going to change: we want the implementations to 682400e64dfSOhad Ben-Cohen * tell us ranges of physical addresses the firmware is allowed to request, 683400e64dfSOhad Ben-Cohen * and not allow firmwares to request access to physical addresses that 684400e64dfSOhad Ben-Cohen * are outside those ranges. 685400e64dfSOhad Ben-Cohen */ 686fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc, 687a2b950acSOhad Ben-Cohen int offset, int avail) 688400e64dfSOhad Ben-Cohen { 689400e64dfSOhad Ben-Cohen struct rproc_mem_entry *mapping; 690b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 691400e64dfSOhad Ben-Cohen int ret; 692400e64dfSOhad Ben-Cohen 693400e64dfSOhad Ben-Cohen /* no point in handling this resource without a valid iommu domain */ 694400e64dfSOhad Ben-Cohen if (!rproc->domain) 695400e64dfSOhad Ben-Cohen return -EINVAL; 696400e64dfSOhad Ben-Cohen 697fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 698b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc is truncated\n"); 699fd2c15ecSOhad Ben-Cohen return -EINVAL; 700fd2c15ecSOhad Ben-Cohen } 701fd2c15ecSOhad Ben-Cohen 702fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 703fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 704b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc has non zero reserved bytes\n"); 705fd2c15ecSOhad Ben-Cohen return -EINVAL; 706fd2c15ecSOhad Ben-Cohen } 707fd2c15ecSOhad Ben-Cohen 708400e64dfSOhad Ben-Cohen mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 709172e6ab1SSuman Anna if (!mapping) 710400e64dfSOhad Ben-Cohen return -ENOMEM; 711400e64dfSOhad Ben-Cohen 712400e64dfSOhad Ben-Cohen ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); 713400e64dfSOhad Ben-Cohen if (ret) { 714b5ab5e24SOhad Ben-Cohen dev_err(dev, "failed to map devmem: %d\n", ret); 715400e64dfSOhad Ben-Cohen goto out; 716400e64dfSOhad Ben-Cohen } 717400e64dfSOhad Ben-Cohen 718400e64dfSOhad Ben-Cohen /* 719400e64dfSOhad Ben-Cohen * We'll need this info later when we'll want to unmap everything 720400e64dfSOhad Ben-Cohen * (e.g. on shutdown). 721400e64dfSOhad Ben-Cohen * 722400e64dfSOhad Ben-Cohen * We can't trust the remote processor not to change the resource 723400e64dfSOhad Ben-Cohen * table, so we must maintain this info independently. 724400e64dfSOhad Ben-Cohen */ 725400e64dfSOhad Ben-Cohen mapping->da = rsc->da; 726400e64dfSOhad Ben-Cohen mapping->len = rsc->len; 727400e64dfSOhad Ben-Cohen list_add_tail(&mapping->node, &rproc->mappings); 728400e64dfSOhad Ben-Cohen 729b5ab5e24SOhad Ben-Cohen dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", 730400e64dfSOhad Ben-Cohen rsc->pa, rsc->da, rsc->len); 731400e64dfSOhad Ben-Cohen 732400e64dfSOhad Ben-Cohen return 0; 733400e64dfSOhad Ben-Cohen 734400e64dfSOhad Ben-Cohen out: 735400e64dfSOhad Ben-Cohen kfree(mapping); 736400e64dfSOhad Ben-Cohen return ret; 737400e64dfSOhad Ben-Cohen } 738400e64dfSOhad Ben-Cohen 739400e64dfSOhad Ben-Cohen /** 740d7c51706SLoic Pallardy * rproc_alloc_carveout() - allocated specified carveout 741d7c51706SLoic Pallardy * @rproc: rproc handle 742d7c51706SLoic Pallardy * @mem: the memory entry to allocate 743d7c51706SLoic Pallardy * 744d7c51706SLoic Pallardy * This function allocate specified memory entry @mem using 745d7c51706SLoic Pallardy * dma_alloc_coherent() as default allocator 746d7c51706SLoic Pallardy */ 747d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc, 748d7c51706SLoic Pallardy struct rproc_mem_entry *mem) 749d7c51706SLoic Pallardy { 750d7c51706SLoic Pallardy struct rproc_mem_entry *mapping = NULL; 751d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 752d7c51706SLoic Pallardy dma_addr_t dma; 753d7c51706SLoic Pallardy void *va; 754d7c51706SLoic Pallardy int ret; 755d7c51706SLoic Pallardy 756d7c51706SLoic Pallardy va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL); 757d7c51706SLoic Pallardy if (!va) { 758d7c51706SLoic Pallardy dev_err(dev->parent, 759096ee786SClement Leger "failed to allocate dma memory: len 0x%zx\n", 760096ee786SClement Leger mem->len); 761d7c51706SLoic Pallardy return -ENOMEM; 762d7c51706SLoic Pallardy } 763d7c51706SLoic Pallardy 764096ee786SClement Leger dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n", 765d7c51706SLoic Pallardy va, &dma, mem->len); 766d7c51706SLoic Pallardy 76760f849a5SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) { 76860f849a5SLoic Pallardy /* 76960f849a5SLoic Pallardy * Check requested da is equal to dma address 77060f849a5SLoic Pallardy * and print a warn message in case of missalignment. 77160f849a5SLoic Pallardy * Don't stop rproc_start sequence as coprocessor may 77260f849a5SLoic Pallardy * build pa to da translation on its side. 77360f849a5SLoic Pallardy */ 77460f849a5SLoic Pallardy if (mem->da != (u32)dma) 77560f849a5SLoic Pallardy dev_warn(dev->parent, 77660f849a5SLoic Pallardy "Allocated carveout doesn't fit device address request\n"); 77760f849a5SLoic Pallardy } 77860f849a5SLoic Pallardy 779d7c51706SLoic Pallardy /* 780d7c51706SLoic Pallardy * Ok, this is non-standard. 781d7c51706SLoic Pallardy * 782d7c51706SLoic Pallardy * Sometimes we can't rely on the generic iommu-based DMA API 783d7c51706SLoic Pallardy * to dynamically allocate the device address and then set the IOMMU 784d7c51706SLoic Pallardy * tables accordingly, because some remote processors might 785d7c51706SLoic Pallardy * _require_ us to use hard coded device addresses that their 786d7c51706SLoic Pallardy * firmware was compiled with. 787d7c51706SLoic Pallardy * 788d7c51706SLoic Pallardy * In this case, we must use the IOMMU API directly and map 789d7c51706SLoic Pallardy * the memory to the device address as expected by the remote 790d7c51706SLoic Pallardy * processor. 791d7c51706SLoic Pallardy * 792d7c51706SLoic Pallardy * Obviously such remote processor devices should not be configured 793d7c51706SLoic Pallardy * to use the iommu-based DMA API: we expect 'dma' to contain the 794d7c51706SLoic Pallardy * physical address in this case. 795d7c51706SLoic Pallardy */ 79660f849a5SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) { 797d7c51706SLoic Pallardy mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 798d7c51706SLoic Pallardy if (!mapping) { 799d7c51706SLoic Pallardy ret = -ENOMEM; 800d7c51706SLoic Pallardy goto dma_free; 801d7c51706SLoic Pallardy } 802d7c51706SLoic Pallardy 803d7c51706SLoic Pallardy ret = iommu_map(rproc->domain, mem->da, dma, mem->len, 804d7c51706SLoic Pallardy mem->flags); 805d7c51706SLoic Pallardy if (ret) { 806d7c51706SLoic Pallardy dev_err(dev, "iommu_map failed: %d\n", ret); 807d7c51706SLoic Pallardy goto free_mapping; 808d7c51706SLoic Pallardy } 809d7c51706SLoic Pallardy 810d7c51706SLoic Pallardy /* 811d7c51706SLoic Pallardy * We'll need this info later when we'll want to unmap 812d7c51706SLoic Pallardy * everything (e.g. on shutdown). 813d7c51706SLoic Pallardy * 814d7c51706SLoic Pallardy * We can't trust the remote processor not to change the 815d7c51706SLoic Pallardy * resource table, so we must maintain this info independently. 816d7c51706SLoic Pallardy */ 817d7c51706SLoic Pallardy mapping->da = mem->da; 818d7c51706SLoic Pallardy mapping->len = mem->len; 819d7c51706SLoic Pallardy list_add_tail(&mapping->node, &rproc->mappings); 820d7c51706SLoic Pallardy 821d7c51706SLoic Pallardy dev_dbg(dev, "carveout mapped 0x%x to %pad\n", 822d7c51706SLoic Pallardy mem->da, &dma); 82360f849a5SLoic Pallardy } 82460f849a5SLoic Pallardy 82560f849a5SLoic Pallardy if (mem->da == FW_RSC_ADDR_ANY) { 826b36de8cfSLoic Pallardy /* Update device address as undefined by requester */ 827b36de8cfSLoic Pallardy if ((u64)dma & HIGH_BITS_MASK) 828b36de8cfSLoic Pallardy dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n"); 829b36de8cfSLoic Pallardy 830d7c51706SLoic Pallardy mem->da = (u32)dma; 831d7c51706SLoic Pallardy } 832d7c51706SLoic Pallardy 83380137b40SLoic Pallardy mem->dma = dma; 834d7c51706SLoic Pallardy mem->va = va; 835d7c51706SLoic Pallardy 836d7c51706SLoic Pallardy return 0; 837d7c51706SLoic Pallardy 838d7c51706SLoic Pallardy free_mapping: 839d7c51706SLoic Pallardy kfree(mapping); 840d7c51706SLoic Pallardy dma_free: 841d7c51706SLoic Pallardy dma_free_coherent(dev->parent, mem->len, va, dma); 842d7c51706SLoic Pallardy return ret; 843d7c51706SLoic Pallardy } 844d7c51706SLoic Pallardy 845d7c51706SLoic Pallardy /** 846f2e74abfSLoic Pallardy * rproc_release_carveout() - release acquired carveout 847f2e74abfSLoic Pallardy * @rproc: rproc handle 848f2e74abfSLoic Pallardy * @mem: the memory entry to release 849f2e74abfSLoic Pallardy * 850f2e74abfSLoic Pallardy * This function releases specified memory entry @mem allocated via 851d7c51706SLoic Pallardy * rproc_alloc_carveout() function by @rproc. 852f2e74abfSLoic Pallardy */ 853f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc, 854f2e74abfSLoic Pallardy struct rproc_mem_entry *mem) 855f2e74abfSLoic Pallardy { 856f2e74abfSLoic Pallardy struct device *dev = &rproc->dev; 857f2e74abfSLoic Pallardy 858f2e74abfSLoic Pallardy /* clean up carveout allocations */ 859f2e74abfSLoic Pallardy dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma); 860f2e74abfSLoic Pallardy return 0; 861f2e74abfSLoic Pallardy } 862f2e74abfSLoic Pallardy 863f2e74abfSLoic Pallardy /** 864400e64dfSOhad Ben-Cohen * rproc_handle_carveout() - handle phys contig memory allocation requests 865400e64dfSOhad Ben-Cohen * @rproc: rproc handle 866400e64dfSOhad Ben-Cohen * @rsc: the resource entry 8672e7d4c2cSArnaud Pouliquen * @offset: offset of the resource entry 868fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for image validation) 869400e64dfSOhad Ben-Cohen * 870400e64dfSOhad Ben-Cohen * This function will handle firmware requests for allocation of physically 871400e64dfSOhad Ben-Cohen * contiguous memory regions. 872400e64dfSOhad Ben-Cohen * 873400e64dfSOhad Ben-Cohen * These request entries should come first in the firmware's resource table, 874400e64dfSOhad Ben-Cohen * as other firmware entries might request placing other data objects inside 875400e64dfSOhad Ben-Cohen * these memory regions (e.g. data/code segments, trace resource entries, ...). 876400e64dfSOhad Ben-Cohen * 877400e64dfSOhad Ben-Cohen * Allocating memory this way helps utilizing the reserved physical memory 878400e64dfSOhad Ben-Cohen * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries 879400e64dfSOhad Ben-Cohen * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB 880400e64dfSOhad Ben-Cohen * pressure is important; it may have a substantial impact on performance. 881400e64dfSOhad Ben-Cohen */ 882fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc, 883a2b950acSOhad Ben-Cohen struct fw_rsc_carveout *rsc, 884a2b950acSOhad Ben-Cohen int offset, int avail) 885400e64dfSOhad Ben-Cohen { 886d7c51706SLoic Pallardy struct rproc_mem_entry *carveout; 887b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 888400e64dfSOhad Ben-Cohen 889fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 890b5ab5e24SOhad Ben-Cohen dev_err(dev, "carveout rsc is truncated\n"); 891fd2c15ecSOhad Ben-Cohen return -EINVAL; 892fd2c15ecSOhad Ben-Cohen } 893fd2c15ecSOhad Ben-Cohen 894fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 895fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 896fd2c15ecSOhad Ben-Cohen dev_err(dev, "carveout rsc has non zero reserved bytes\n"); 897fd2c15ecSOhad Ben-Cohen return -EINVAL; 898fd2c15ecSOhad Ben-Cohen } 899fd2c15ecSOhad Ben-Cohen 9009d7814a9SAnna, Suman dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n", 90135386166SLee Jones rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); 902fd2c15ecSOhad Ben-Cohen 903ffa5f9c8SLoic Pallardy /* 904ffa5f9c8SLoic Pallardy * Check carveout rsc already part of a registered carveout, 905ffa5f9c8SLoic Pallardy * Search by name, then check the da and length 906ffa5f9c8SLoic Pallardy */ 907ffa5f9c8SLoic Pallardy carveout = rproc_find_carveout_by_name(rproc, rsc->name); 908ffa5f9c8SLoic Pallardy 909ffa5f9c8SLoic Pallardy if (carveout) { 910ffa5f9c8SLoic Pallardy if (carveout->rsc_offset != FW_RSC_ADDR_ANY) { 911ffa5f9c8SLoic Pallardy dev_err(dev, 912ffa5f9c8SLoic Pallardy "Carveout already associated to resource table\n"); 913ffa5f9c8SLoic Pallardy return -ENOMEM; 914ffa5f9c8SLoic Pallardy } 915ffa5f9c8SLoic Pallardy 916ffa5f9c8SLoic Pallardy if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len)) 917ffa5f9c8SLoic Pallardy return -ENOMEM; 918ffa5f9c8SLoic Pallardy 919ffa5f9c8SLoic Pallardy /* Update memory carveout with resource table info */ 920ffa5f9c8SLoic Pallardy carveout->rsc_offset = offset; 921ffa5f9c8SLoic Pallardy carveout->flags = rsc->flags; 922ffa5f9c8SLoic Pallardy 923ffa5f9c8SLoic Pallardy return 0; 924ffa5f9c8SLoic Pallardy } 925ffa5f9c8SLoic Pallardy 926d7c51706SLoic Pallardy /* Register carveout in in list */ 92799cf0361SBen Dooks (Codethink) carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da, 928d7c51706SLoic Pallardy rproc_alloc_carveout, 929d7c51706SLoic Pallardy rproc_release_carveout, rsc->name); 930d7c51706SLoic Pallardy if (!carveout) { 931d7c51706SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 93272029c90SLoic Pallardy return -ENOMEM; 933400e64dfSOhad Ben-Cohen } 934400e64dfSOhad Ben-Cohen 935d7c51706SLoic Pallardy carveout->flags = rsc->flags; 936d7c51706SLoic Pallardy carveout->rsc_offset = offset; 93715c0b025SLoic Pallardy rproc_add_carveout(rproc, carveout); 938400e64dfSOhad Ben-Cohen 939400e64dfSOhad Ben-Cohen return 0; 940400e64dfSOhad Ben-Cohen } 941400e64dfSOhad Ben-Cohen 94272029c90SLoic Pallardy /** 94315c0b025SLoic Pallardy * rproc_add_carveout() - register an allocated carveout region 94415c0b025SLoic Pallardy * @rproc: rproc handle 94515c0b025SLoic Pallardy * @mem: memory entry to register 94615c0b025SLoic Pallardy * 94715c0b025SLoic Pallardy * This function registers specified memory entry in @rproc carveouts list. 94815c0b025SLoic Pallardy * Specified carveout should have been allocated before registering. 94915c0b025SLoic Pallardy */ 95015c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) 95115c0b025SLoic Pallardy { 95215c0b025SLoic Pallardy list_add_tail(&mem->node, &rproc->carveouts); 95315c0b025SLoic Pallardy } 95415c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout); 95515c0b025SLoic Pallardy 95615c0b025SLoic Pallardy /** 95772029c90SLoic Pallardy * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct 95872029c90SLoic Pallardy * @dev: pointer on device struct 95972029c90SLoic Pallardy * @va: virtual address 96072029c90SLoic Pallardy * @dma: dma address 96172029c90SLoic Pallardy * @len: memory carveout length 96272029c90SLoic Pallardy * @da: device address 963a9f6fe0dSLoic Pallardy * @alloc: memory carveout allocation function 964a9f6fe0dSLoic Pallardy * @release: memory carveout release function 96572029c90SLoic Pallardy * @name: carveout name 96672029c90SLoic Pallardy * 96772029c90SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 96872029c90SLoic Pallardy * provided by client. 96972029c90SLoic Pallardy */ 9707e05c8deSLee Jones __printf(8, 9) 97172029c90SLoic Pallardy struct rproc_mem_entry * 97272029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev, 973096ee786SClement Leger void *va, dma_addr_t dma, size_t len, u32 da, 974d7c51706SLoic Pallardy int (*alloc)(struct rproc *, struct rproc_mem_entry *), 97572029c90SLoic Pallardy int (*release)(struct rproc *, struct rproc_mem_entry *), 97672029c90SLoic Pallardy const char *name, ...) 97772029c90SLoic Pallardy { 97872029c90SLoic Pallardy struct rproc_mem_entry *mem; 97972029c90SLoic Pallardy va_list args; 98072029c90SLoic Pallardy 98172029c90SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 98272029c90SLoic Pallardy if (!mem) 98372029c90SLoic Pallardy return mem; 98472029c90SLoic Pallardy 98572029c90SLoic Pallardy mem->va = va; 98672029c90SLoic Pallardy mem->dma = dma; 98772029c90SLoic Pallardy mem->da = da; 98872029c90SLoic Pallardy mem->len = len; 989d7c51706SLoic Pallardy mem->alloc = alloc; 99072029c90SLoic Pallardy mem->release = release; 991d7c51706SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 9921429cca1SLoic Pallardy mem->of_resm_idx = -1; 99372029c90SLoic Pallardy 99472029c90SLoic Pallardy va_start(args, name); 99572029c90SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 99672029c90SLoic Pallardy va_end(args); 99772029c90SLoic Pallardy 99872029c90SLoic Pallardy return mem; 99972029c90SLoic Pallardy } 100072029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init); 100172029c90SLoic Pallardy 100272029c90SLoic Pallardy /** 10031429cca1SLoic Pallardy * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct 10041429cca1SLoic Pallardy * from a reserved memory phandle 10051429cca1SLoic Pallardy * @dev: pointer on device struct 10061429cca1SLoic Pallardy * @of_resm_idx: reserved memory phandle index in "memory-region" 10071429cca1SLoic Pallardy * @len: memory carveout length 10081429cca1SLoic Pallardy * @da: device address 10091429cca1SLoic Pallardy * @name: carveout name 10101429cca1SLoic Pallardy * 10111429cca1SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 10121429cca1SLoic Pallardy * provided by client. 10131429cca1SLoic Pallardy */ 10147e05c8deSLee Jones __printf(5, 6) 10151429cca1SLoic Pallardy struct rproc_mem_entry * 1016096ee786SClement Leger rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len, 10171429cca1SLoic Pallardy u32 da, const char *name, ...) 10181429cca1SLoic Pallardy { 10191429cca1SLoic Pallardy struct rproc_mem_entry *mem; 10201429cca1SLoic Pallardy va_list args; 10211429cca1SLoic Pallardy 10221429cca1SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 10231429cca1SLoic Pallardy if (!mem) 10241429cca1SLoic Pallardy return mem; 10251429cca1SLoic Pallardy 10261429cca1SLoic Pallardy mem->da = da; 10271429cca1SLoic Pallardy mem->len = len; 10281429cca1SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 10291429cca1SLoic Pallardy mem->of_resm_idx = of_resm_idx; 10301429cca1SLoic Pallardy 10311429cca1SLoic Pallardy va_start(args, name); 10321429cca1SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 10331429cca1SLoic Pallardy va_end(args); 10341429cca1SLoic Pallardy 10351429cca1SLoic Pallardy return mem; 10361429cca1SLoic Pallardy } 10371429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init); 10381429cca1SLoic Pallardy 10392e7d4c2cSArnaud Pouliquen /* 1040e12bc14bSOhad Ben-Cohen * A lookup table for resource handlers. The indices are defined in 1041e12bc14bSOhad Ben-Cohen * enum fw_resource_type. 1042e12bc14bSOhad Ben-Cohen */ 1043232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { 1044fd2c15ecSOhad Ben-Cohen [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout, 1045fd2c15ecSOhad Ben-Cohen [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem, 1046fd2c15ecSOhad Ben-Cohen [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace, 1047232fcdbbSSjur Brændeland [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev, 1048232fcdbbSSjur Brændeland }; 1049232fcdbbSSjur Brændeland 1050400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */ 1051a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc, 1052232fcdbbSSjur Brændeland rproc_handle_resource_t handlers[RSC_LAST]) 1053400e64dfSOhad Ben-Cohen { 1054b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1055e12bc14bSOhad Ben-Cohen rproc_handle_resource_t handler; 1056fd2c15ecSOhad Ben-Cohen int ret = 0, i; 1057400e64dfSOhad Ben-Cohen 1058d4bb86f2SBjorn Andersson if (!rproc->table_ptr) 1059d4bb86f2SBjorn Andersson return 0; 1060d4bb86f2SBjorn Andersson 1061a2b950acSOhad Ben-Cohen for (i = 0; i < rproc->table_ptr->num; i++) { 1062a2b950acSOhad Ben-Cohen int offset = rproc->table_ptr->offset[i]; 1063a2b950acSOhad Ben-Cohen struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; 1064a4b24c75SBjorn Andersson int avail = rproc->table_sz - offset - sizeof(*hdr); 1065fd2c15ecSOhad Ben-Cohen void *rsc = (void *)hdr + sizeof(*hdr); 1066400e64dfSOhad Ben-Cohen 1067fd2c15ecSOhad Ben-Cohen /* make sure table isn't truncated */ 1068fd2c15ecSOhad Ben-Cohen if (avail < 0) { 1069fd2c15ecSOhad Ben-Cohen dev_err(dev, "rsc table is truncated\n"); 1070fd2c15ecSOhad Ben-Cohen return -EINVAL; 1071fd2c15ecSOhad Ben-Cohen } 1072fd2c15ecSOhad Ben-Cohen 1073fd2c15ecSOhad Ben-Cohen dev_dbg(dev, "rsc: type %d\n", hdr->type); 1074fd2c15ecSOhad Ben-Cohen 1075b1a17513SClement Leger if (hdr->type >= RSC_VENDOR_START && 1076b1a17513SClement Leger hdr->type <= RSC_VENDOR_END) { 1077b1a17513SClement Leger ret = rproc_handle_rsc(rproc, hdr->type, rsc, 1078b1a17513SClement Leger offset + sizeof(*hdr), avail); 1079b1a17513SClement Leger if (ret == RSC_HANDLED) 1080b1a17513SClement Leger continue; 1081b1a17513SClement Leger else if (ret < 0) 1082b1a17513SClement Leger break; 1083b1a17513SClement Leger 1084b1a17513SClement Leger dev_warn(dev, "unsupported vendor resource %d\n", 1085b1a17513SClement Leger hdr->type); 1086b1a17513SClement Leger continue; 1087b1a17513SClement Leger } 1088b1a17513SClement Leger 1089fd2c15ecSOhad Ben-Cohen if (hdr->type >= RSC_LAST) { 1090fd2c15ecSOhad Ben-Cohen dev_warn(dev, "unsupported resource %d\n", hdr->type); 1091e12bc14bSOhad Ben-Cohen continue; 1092400e64dfSOhad Ben-Cohen } 1093400e64dfSOhad Ben-Cohen 1094232fcdbbSSjur Brændeland handler = handlers[hdr->type]; 1095e12bc14bSOhad Ben-Cohen if (!handler) 1096e12bc14bSOhad Ben-Cohen continue; 1097e12bc14bSOhad Ben-Cohen 1098a2b950acSOhad Ben-Cohen ret = handler(rproc, rsc, offset + sizeof(*hdr), avail); 10997a186941SOhad Ben-Cohen if (ret) 1100400e64dfSOhad Ben-Cohen break; 1101400e64dfSOhad Ben-Cohen } 1102400e64dfSOhad Ben-Cohen 1103400e64dfSOhad Ben-Cohen return ret; 1104400e64dfSOhad Ben-Cohen } 1105400e64dfSOhad Ben-Cohen 1106c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc) 1107c455daa4SBjorn Andersson { 1108c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1109c455daa4SBjorn Andersson int ret; 1110c455daa4SBjorn Andersson 1111c455daa4SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1112c455daa4SBjorn Andersson if (subdev->prepare) { 1113c455daa4SBjorn Andersson ret = subdev->prepare(subdev); 1114c455daa4SBjorn Andersson if (ret) 1115c455daa4SBjorn Andersson goto unroll_preparation; 1116c455daa4SBjorn Andersson } 1117c455daa4SBjorn Andersson } 1118c455daa4SBjorn Andersson 1119c455daa4SBjorn Andersson return 0; 1120c455daa4SBjorn Andersson 1121c455daa4SBjorn Andersson unroll_preparation: 1122c455daa4SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1123c455daa4SBjorn Andersson if (subdev->unprepare) 1124c455daa4SBjorn Andersson subdev->unprepare(subdev); 1125c455daa4SBjorn Andersson } 1126c455daa4SBjorn Andersson 1127c455daa4SBjorn Andersson return ret; 1128c455daa4SBjorn Andersson } 1129c455daa4SBjorn Andersson 1130618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc) 11317bdc9650SBjorn Andersson { 11327bdc9650SBjorn Andersson struct rproc_subdev *subdev; 11337bdc9650SBjorn Andersson int ret; 11347bdc9650SBjorn Andersson 11357bdc9650SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1136be37b1e0SBjorn Andersson if (subdev->start) { 1137618fcff3SBjorn Andersson ret = subdev->start(subdev); 11387bdc9650SBjorn Andersson if (ret) 11397bdc9650SBjorn Andersson goto unroll_registration; 11407bdc9650SBjorn Andersson } 1141be37b1e0SBjorn Andersson } 11427bdc9650SBjorn Andersson 11437bdc9650SBjorn Andersson return 0; 11447bdc9650SBjorn Andersson 11457bdc9650SBjorn Andersson unroll_registration: 1146be37b1e0SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1147be37b1e0SBjorn Andersson if (subdev->stop) 1148618fcff3SBjorn Andersson subdev->stop(subdev, true); 1149be37b1e0SBjorn Andersson } 11507bdc9650SBjorn Andersson 11517bdc9650SBjorn Andersson return ret; 11527bdc9650SBjorn Andersson } 11537bdc9650SBjorn Andersson 1154618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed) 11557bdc9650SBjorn Andersson { 11567bdc9650SBjorn Andersson struct rproc_subdev *subdev; 11577bdc9650SBjorn Andersson 1158be37b1e0SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1159be37b1e0SBjorn Andersson if (subdev->stop) 1160618fcff3SBjorn Andersson subdev->stop(subdev, crashed); 11617bdc9650SBjorn Andersson } 1162be37b1e0SBjorn Andersson } 11637bdc9650SBjorn Andersson 1164c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc) 1165c455daa4SBjorn Andersson { 1166c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1167c455daa4SBjorn Andersson 1168c455daa4SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1169c455daa4SBjorn Andersson if (subdev->unprepare) 1170c455daa4SBjorn Andersson subdev->unprepare(subdev); 1171c455daa4SBjorn Andersson } 1172c455daa4SBjorn Andersson } 1173c455daa4SBjorn Andersson 1174400e64dfSOhad Ben-Cohen /** 1175d7c51706SLoic Pallardy * rproc_alloc_registered_carveouts() - allocate all carveouts registered 1176d7c51706SLoic Pallardy * in the list 1177d7c51706SLoic Pallardy * @rproc: the remote processor handle 1178d7c51706SLoic Pallardy * 1179d7c51706SLoic Pallardy * This function parses registered carveout list, performs allocation 1180d7c51706SLoic Pallardy * if alloc() ops registered and updates resource table information 1181d7c51706SLoic Pallardy * if rsc_offset set. 1182d7c51706SLoic Pallardy * 1183d7c51706SLoic Pallardy * Return: 0 on success 1184d7c51706SLoic Pallardy */ 1185d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc) 1186d7c51706SLoic Pallardy { 1187d7c51706SLoic Pallardy struct rproc_mem_entry *entry, *tmp; 1188d7c51706SLoic Pallardy struct fw_rsc_carveout *rsc; 1189d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 1190b36de8cfSLoic Pallardy u64 pa; 1191d7c51706SLoic Pallardy int ret; 1192d7c51706SLoic Pallardy 1193d7c51706SLoic Pallardy list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1194d7c51706SLoic Pallardy if (entry->alloc) { 1195d7c51706SLoic Pallardy ret = entry->alloc(rproc, entry); 1196d7c51706SLoic Pallardy if (ret) { 1197d7c51706SLoic Pallardy dev_err(dev, "Unable to allocate carveout %s: %d\n", 1198d7c51706SLoic Pallardy entry->name, ret); 1199d7c51706SLoic Pallardy return -ENOMEM; 1200d7c51706SLoic Pallardy } 1201d7c51706SLoic Pallardy } 1202d7c51706SLoic Pallardy 1203d7c51706SLoic Pallardy if (entry->rsc_offset != FW_RSC_ADDR_ANY) { 1204d7c51706SLoic Pallardy /* update resource table */ 1205d7c51706SLoic Pallardy rsc = (void *)rproc->table_ptr + entry->rsc_offset; 1206d7c51706SLoic Pallardy 1207d7c51706SLoic Pallardy /* 1208d7c51706SLoic Pallardy * Some remote processors might need to know the pa 1209d7c51706SLoic Pallardy * even though they are behind an IOMMU. E.g., OMAP4's 1210d7c51706SLoic Pallardy * remote M3 processor needs this so it can control 1211d7c51706SLoic Pallardy * on-chip hardware accelerators that are not behind 1212d7c51706SLoic Pallardy * the IOMMU, and therefor must know the pa. 1213d7c51706SLoic Pallardy * 1214d7c51706SLoic Pallardy * Generally we don't want to expose physical addresses 1215d7c51706SLoic Pallardy * if we don't have to (remote processors are generally 1216d7c51706SLoic Pallardy * _not_ trusted), so we might want to do this only for 1217d7c51706SLoic Pallardy * remote processor that _must_ have this (e.g. OMAP4's 1218d7c51706SLoic Pallardy * dual M3 subsystem). 1219d7c51706SLoic Pallardy * 1220d7c51706SLoic Pallardy * Non-IOMMU processors might also want to have this info. 1221d7c51706SLoic Pallardy * In this case, the device address and the physical address 1222d7c51706SLoic Pallardy * are the same. 1223d7c51706SLoic Pallardy */ 1224ffa5f9c8SLoic Pallardy 1225ffa5f9c8SLoic Pallardy /* Use va if defined else dma to generate pa */ 1226d7c51706SLoic Pallardy if (entry->va) 1227b36de8cfSLoic Pallardy pa = (u64)rproc_va_to_pa(entry->va); 1228ffa5f9c8SLoic Pallardy else 1229b36de8cfSLoic Pallardy pa = (u64)entry->dma; 1230ffa5f9c8SLoic Pallardy 1231b36de8cfSLoic Pallardy if (((u64)pa) & HIGH_BITS_MASK) 1232b36de8cfSLoic Pallardy dev_warn(dev, 1233b36de8cfSLoic Pallardy "Physical address cast in 32bit to fit resource table format\n"); 1234b36de8cfSLoic Pallardy 1235b36de8cfSLoic Pallardy rsc->pa = (u32)pa; 1236ffa5f9c8SLoic Pallardy rsc->da = entry->da; 1237ffa5f9c8SLoic Pallardy rsc->len = entry->len; 1238d7c51706SLoic Pallardy } 1239d7c51706SLoic Pallardy } 1240d7c51706SLoic Pallardy 1241d7c51706SLoic Pallardy return 0; 1242d7c51706SLoic Pallardy } 1243d7c51706SLoic Pallardy 1244d7c51706SLoic Pallardy /** 12452666ca91SSarangdhar Joshi * rproc_coredump_cleanup() - clean up dump_segments list 12462666ca91SSarangdhar Joshi * @rproc: the remote processor handle 12472666ca91SSarangdhar Joshi */ 12482666ca91SSarangdhar Joshi static void rproc_coredump_cleanup(struct rproc *rproc) 12492666ca91SSarangdhar Joshi { 12502666ca91SSarangdhar Joshi struct rproc_dump_segment *entry, *tmp; 12512666ca91SSarangdhar Joshi 12522666ca91SSarangdhar Joshi list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) { 12532666ca91SSarangdhar Joshi list_del(&entry->node); 12542666ca91SSarangdhar Joshi kfree(entry); 12552666ca91SSarangdhar Joshi } 12562666ca91SSarangdhar Joshi } 12572666ca91SSarangdhar Joshi 12582666ca91SSarangdhar Joshi /** 1259400e64dfSOhad Ben-Cohen * rproc_resource_cleanup() - clean up and free all acquired resources 1260400e64dfSOhad Ben-Cohen * @rproc: rproc handle 1261400e64dfSOhad Ben-Cohen * 1262400e64dfSOhad Ben-Cohen * This function will free all resources acquired for @rproc, and it 12637a186941SOhad Ben-Cohen * is called whenever @rproc either shuts down or fails to boot. 1264400e64dfSOhad Ben-Cohen */ 1265400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc) 1266400e64dfSOhad Ben-Cohen { 1267400e64dfSOhad Ben-Cohen struct rproc_mem_entry *entry, *tmp; 1268a987e6b9SLoic Pallardy struct rproc_debug_trace *trace, *ttmp; 1269d81fb32fSBjorn Andersson struct rproc_vdev *rvdev, *rvtmp; 1270b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1271400e64dfSOhad Ben-Cohen 1272400e64dfSOhad Ben-Cohen /* clean up debugfs trace entries */ 1273a987e6b9SLoic Pallardy list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) { 1274a987e6b9SLoic Pallardy rproc_remove_trace_file(trace->tfile); 1275400e64dfSOhad Ben-Cohen rproc->num_traces--; 1276a987e6b9SLoic Pallardy list_del(&trace->node); 1277a987e6b9SLoic Pallardy kfree(trace); 1278400e64dfSOhad Ben-Cohen } 1279400e64dfSOhad Ben-Cohen 1280400e64dfSOhad Ben-Cohen /* clean up iommu mapping entries */ 1281400e64dfSOhad Ben-Cohen list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { 1282400e64dfSOhad Ben-Cohen size_t unmapped; 1283400e64dfSOhad Ben-Cohen 1284400e64dfSOhad Ben-Cohen unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); 1285400e64dfSOhad Ben-Cohen if (unmapped != entry->len) { 1286400e64dfSOhad Ben-Cohen /* nothing much to do besides complaining */ 1287096ee786SClement Leger dev_err(dev, "failed to unmap %zx/%zu\n", entry->len, 1288400e64dfSOhad Ben-Cohen unmapped); 1289400e64dfSOhad Ben-Cohen } 1290400e64dfSOhad Ben-Cohen 1291400e64dfSOhad Ben-Cohen list_del(&entry->node); 1292400e64dfSOhad Ben-Cohen kfree(entry); 1293400e64dfSOhad Ben-Cohen } 1294b6356a01SSuman Anna 1295b6356a01SSuman Anna /* clean up carveout allocations */ 1296b6356a01SSuman Anna list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1297f2e74abfSLoic Pallardy if (entry->release) 1298f2e74abfSLoic Pallardy entry->release(rproc, entry); 1299b6356a01SSuman Anna list_del(&entry->node); 1300b6356a01SSuman Anna kfree(entry); 1301b6356a01SSuman Anna } 1302d81fb32fSBjorn Andersson 1303d81fb32fSBjorn Andersson /* clean up remote vdev entries */ 1304f5bcb353SBjorn Andersson list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) 13052b45cef5SBjorn Andersson kref_put(&rvdev->refcount, rproc_vdev_release); 13062666ca91SSarangdhar Joshi 13072666ca91SSarangdhar Joshi rproc_coredump_cleanup(rproc); 13082b45cef5SBjorn Andersson } 1309400e64dfSOhad Ben-Cohen 13101efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw) 13111efa30d0SSarangdhar Joshi { 1312a4b24c75SBjorn Andersson struct resource_table *loaded_table; 13131efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 1314a4b24c75SBjorn Andersson int ret; 13151efa30d0SSarangdhar Joshi 13161efa30d0SSarangdhar Joshi /* load the ELF segments to memory */ 13171efa30d0SSarangdhar Joshi ret = rproc_load_segments(rproc, fw); 13181efa30d0SSarangdhar Joshi if (ret) { 13191efa30d0SSarangdhar Joshi dev_err(dev, "Failed to load program segments: %d\n", ret); 13201efa30d0SSarangdhar Joshi return ret; 13211efa30d0SSarangdhar Joshi } 13221efa30d0SSarangdhar Joshi 13231efa30d0SSarangdhar Joshi /* 13241efa30d0SSarangdhar Joshi * The starting device has been given the rproc->cached_table as the 13251efa30d0SSarangdhar Joshi * resource table. The address of the vring along with the other 13261efa30d0SSarangdhar Joshi * allocated resources (carveouts etc) is stored in cached_table. 13271efa30d0SSarangdhar Joshi * In order to pass this information to the remote device we must copy 13281efa30d0SSarangdhar Joshi * this information to device memory. We also update the table_ptr so 13291efa30d0SSarangdhar Joshi * that any subsequent changes will be applied to the loaded version. 13301efa30d0SSarangdhar Joshi */ 13311efa30d0SSarangdhar Joshi loaded_table = rproc_find_loaded_rsc_table(rproc, fw); 13321efa30d0SSarangdhar Joshi if (loaded_table) { 1333a4b24c75SBjorn Andersson memcpy(loaded_table, rproc->cached_table, rproc->table_sz); 13341efa30d0SSarangdhar Joshi rproc->table_ptr = loaded_table; 13351efa30d0SSarangdhar Joshi } 13361efa30d0SSarangdhar Joshi 1337c455daa4SBjorn Andersson ret = rproc_prepare_subdevices(rproc); 1338c455daa4SBjorn Andersson if (ret) { 1339c455daa4SBjorn Andersson dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1340c455daa4SBjorn Andersson rproc->name, ret); 1341f68d51bdSSuman Anna goto reset_table_ptr; 1342c455daa4SBjorn Andersson } 1343c455daa4SBjorn Andersson 13441efa30d0SSarangdhar Joshi /* power up the remote processor */ 13451efa30d0SSarangdhar Joshi ret = rproc->ops->start(rproc); 13461efa30d0SSarangdhar Joshi if (ret) { 13471efa30d0SSarangdhar Joshi dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); 1348c455daa4SBjorn Andersson goto unprepare_subdevices; 13491efa30d0SSarangdhar Joshi } 13501efa30d0SSarangdhar Joshi 1351618fcff3SBjorn Andersson /* Start any subdevices for the remote processor */ 1352618fcff3SBjorn Andersson ret = rproc_start_subdevices(rproc); 13531efa30d0SSarangdhar Joshi if (ret) { 13541efa30d0SSarangdhar Joshi dev_err(dev, "failed to probe subdevices for %s: %d\n", 13551efa30d0SSarangdhar Joshi rproc->name, ret); 1356c455daa4SBjorn Andersson goto stop_rproc; 13571efa30d0SSarangdhar Joshi } 13581efa30d0SSarangdhar Joshi 13591efa30d0SSarangdhar Joshi rproc->state = RPROC_RUNNING; 13601efa30d0SSarangdhar Joshi 13611efa30d0SSarangdhar Joshi dev_info(dev, "remote processor %s is now up\n", rproc->name); 13621efa30d0SSarangdhar Joshi 13631efa30d0SSarangdhar Joshi return 0; 1364c455daa4SBjorn Andersson 1365c455daa4SBjorn Andersson stop_rproc: 1366c455daa4SBjorn Andersson rproc->ops->stop(rproc); 1367c455daa4SBjorn Andersson unprepare_subdevices: 1368c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1369f68d51bdSSuman Anna reset_table_ptr: 1370f68d51bdSSuman Anna rproc->table_ptr = rproc->cached_table; 1371c455daa4SBjorn Andersson 1372c455daa4SBjorn Andersson return ret; 13731efa30d0SSarangdhar Joshi } 13741efa30d0SSarangdhar Joshi 1375fdf0e00eSMathieu Poirier static int rproc_attach(struct rproc *rproc) 1376d848a481SMathieu Poirier { 1377d848a481SMathieu Poirier struct device *dev = &rproc->dev; 1378d848a481SMathieu Poirier int ret; 1379d848a481SMathieu Poirier 1380d848a481SMathieu Poirier ret = rproc_prepare_subdevices(rproc); 1381d848a481SMathieu Poirier if (ret) { 1382d848a481SMathieu Poirier dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1383d848a481SMathieu Poirier rproc->name, ret); 1384d848a481SMathieu Poirier goto out; 1385d848a481SMathieu Poirier } 1386d848a481SMathieu Poirier 1387d848a481SMathieu Poirier /* Attach to the remote processor */ 1388d848a481SMathieu Poirier ret = rproc_attach_device(rproc); 1389d848a481SMathieu Poirier if (ret) { 1390d848a481SMathieu Poirier dev_err(dev, "can't attach to rproc %s: %d\n", 1391d848a481SMathieu Poirier rproc->name, ret); 1392d848a481SMathieu Poirier goto unprepare_subdevices; 1393d848a481SMathieu Poirier } 1394d848a481SMathieu Poirier 1395d848a481SMathieu Poirier /* Start any subdevices for the remote processor */ 1396d848a481SMathieu Poirier ret = rproc_start_subdevices(rproc); 1397d848a481SMathieu Poirier if (ret) { 1398d848a481SMathieu Poirier dev_err(dev, "failed to probe subdevices for %s: %d\n", 1399d848a481SMathieu Poirier rproc->name, ret); 1400d848a481SMathieu Poirier goto stop_rproc; 1401d848a481SMathieu Poirier } 1402d848a481SMathieu Poirier 1403d848a481SMathieu Poirier rproc->state = RPROC_RUNNING; 1404d848a481SMathieu Poirier 1405d848a481SMathieu Poirier dev_info(dev, "remote processor %s is now attached\n", rproc->name); 1406d848a481SMathieu Poirier 1407d848a481SMathieu Poirier return 0; 1408d848a481SMathieu Poirier 1409d848a481SMathieu Poirier stop_rproc: 1410d848a481SMathieu Poirier rproc->ops->stop(rproc); 1411d848a481SMathieu Poirier unprepare_subdevices: 1412d848a481SMathieu Poirier rproc_unprepare_subdevices(rproc); 1413d848a481SMathieu Poirier out: 1414d848a481SMathieu Poirier return ret; 1415d848a481SMathieu Poirier } 1416d848a481SMathieu Poirier 1417400e64dfSOhad Ben-Cohen /* 1418400e64dfSOhad Ben-Cohen * take a firmware and boot a remote processor with it. 1419400e64dfSOhad Ben-Cohen */ 1420400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) 1421400e64dfSOhad Ben-Cohen { 1422b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1423400e64dfSOhad Ben-Cohen const char *name = rproc->firmware; 142458b64090SBjorn Andersson int ret; 1425400e64dfSOhad Ben-Cohen 1426400e64dfSOhad Ben-Cohen ret = rproc_fw_sanity_check(rproc, fw); 1427400e64dfSOhad Ben-Cohen if (ret) 1428400e64dfSOhad Ben-Cohen return ret; 1429400e64dfSOhad Ben-Cohen 1430e981f6d4SSjur Brændeland dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); 1431400e64dfSOhad Ben-Cohen 1432400e64dfSOhad Ben-Cohen /* 1433400e64dfSOhad Ben-Cohen * if enabling an IOMMU isn't relevant for this rproc, this is 1434400e64dfSOhad Ben-Cohen * just a nop 1435400e64dfSOhad Ben-Cohen */ 1436400e64dfSOhad Ben-Cohen ret = rproc_enable_iommu(rproc); 1437400e64dfSOhad Ben-Cohen if (ret) { 1438400e64dfSOhad Ben-Cohen dev_err(dev, "can't enable iommu: %d\n", ret); 143949cff125SMathieu Poirier return ret; 1440400e64dfSOhad Ben-Cohen } 1441400e64dfSOhad Ben-Cohen 144233467ac3SLoic Pallardy /* Prepare rproc for firmware loading if needed */ 144333467ac3SLoic Pallardy ret = rproc_prepare_device(rproc); 144433467ac3SLoic Pallardy if (ret) { 144533467ac3SLoic Pallardy dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret); 144633467ac3SLoic Pallardy goto disable_iommu; 144733467ac3SLoic Pallardy } 144833467ac3SLoic Pallardy 14493e5f9eb5SSjur Brændeland rproc->bootaddr = rproc_get_boot_addr(rproc, fw); 1450400e64dfSOhad Ben-Cohen 1451c1d35c1aSBjorn Andersson /* Load resource table, core dump segment list etc from the firmware */ 1452c1d35c1aSBjorn Andersson ret = rproc_parse_fw(rproc, fw); 145358b64090SBjorn Andersson if (ret) 145433467ac3SLoic Pallardy goto unprepare_rproc; 1455a0c10687SBjorn Andersson 1456b35d7afcSBjorn Andersson /* reset max_notifyid */ 1457b35d7afcSBjorn Andersson rproc->max_notifyid = -1; 1458b35d7afcSBjorn Andersson 1459c6aed238SLoic Pallardy /* reset handled vdev */ 1460c6aed238SLoic Pallardy rproc->nb_vdev = 0; 1461c6aed238SLoic Pallardy 1462400e64dfSOhad Ben-Cohen /* handle fw resources which are required to boot rproc */ 1463a4b24c75SBjorn Andersson ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1464400e64dfSOhad Ben-Cohen if (ret) { 1465400e64dfSOhad Ben-Cohen dev_err(dev, "Failed to process resources: %d\n", ret); 1466229b85a6SBjorn Andersson goto clean_up_resources; 1467400e64dfSOhad Ben-Cohen } 1468400e64dfSOhad Ben-Cohen 1469d7c51706SLoic Pallardy /* Allocate carveout resources associated to rproc */ 1470d7c51706SLoic Pallardy ret = rproc_alloc_registered_carveouts(rproc); 1471d7c51706SLoic Pallardy if (ret) { 1472d7c51706SLoic Pallardy dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1473d7c51706SLoic Pallardy ret); 1474d7c51706SLoic Pallardy goto clean_up_resources; 1475d7c51706SLoic Pallardy } 1476d7c51706SLoic Pallardy 14771efa30d0SSarangdhar Joshi ret = rproc_start(rproc, fw); 14781efa30d0SSarangdhar Joshi if (ret) 1479229b85a6SBjorn Andersson goto clean_up_resources; 1480400e64dfSOhad Ben-Cohen 1481400e64dfSOhad Ben-Cohen return 0; 1482400e64dfSOhad Ben-Cohen 1483229b85a6SBjorn Andersson clean_up_resources: 1484229b85a6SBjorn Andersson rproc_resource_cleanup(rproc); 1485a0c10687SBjorn Andersson kfree(rproc->cached_table); 1486a0c10687SBjorn Andersson rproc->cached_table = NULL; 1487988d204cSBjorn Andersson rproc->table_ptr = NULL; 148833467ac3SLoic Pallardy unprepare_rproc: 148933467ac3SLoic Pallardy /* release HW resources if needed */ 149033467ac3SLoic Pallardy rproc_unprepare_device(rproc); 149158b64090SBjorn Andersson disable_iommu: 1492400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1493400e64dfSOhad Ben-Cohen return ret; 1494400e64dfSOhad Ben-Cohen } 1495400e64dfSOhad Ben-Cohen 1496400e64dfSOhad Ben-Cohen /* 1497fdf0e00eSMathieu Poirier * Attach to remote processor - similar to rproc_fw_boot() but without 1498fdf0e00eSMathieu Poirier * the steps that deal with the firmware image. 1499fdf0e00eSMathieu Poirier */ 1500*0f9dc562SMathieu Poirier static int rproc_actuate(struct rproc *rproc) 1501fdf0e00eSMathieu Poirier { 1502fdf0e00eSMathieu Poirier struct device *dev = &rproc->dev; 1503fdf0e00eSMathieu Poirier int ret; 1504fdf0e00eSMathieu Poirier 1505fdf0e00eSMathieu Poirier /* 1506fdf0e00eSMathieu Poirier * if enabling an IOMMU isn't relevant for this rproc, this is 1507fdf0e00eSMathieu Poirier * just a nop 1508fdf0e00eSMathieu Poirier */ 1509fdf0e00eSMathieu Poirier ret = rproc_enable_iommu(rproc); 1510fdf0e00eSMathieu Poirier if (ret) { 1511fdf0e00eSMathieu Poirier dev_err(dev, "can't enable iommu: %d\n", ret); 1512fdf0e00eSMathieu Poirier return ret; 1513fdf0e00eSMathieu Poirier } 1514fdf0e00eSMathieu Poirier 1515fdf0e00eSMathieu Poirier /* reset max_notifyid */ 1516fdf0e00eSMathieu Poirier rproc->max_notifyid = -1; 1517fdf0e00eSMathieu Poirier 1518fdf0e00eSMathieu Poirier /* reset handled vdev */ 1519fdf0e00eSMathieu Poirier rproc->nb_vdev = 0; 1520fdf0e00eSMathieu Poirier 1521fdf0e00eSMathieu Poirier /* 1522fdf0e00eSMathieu Poirier * Handle firmware resources required to attach to a remote processor. 1523fdf0e00eSMathieu Poirier * Because we are attaching rather than booting the remote processor, 1524fdf0e00eSMathieu Poirier * we expect the platform driver to properly set rproc->table_ptr. 1525fdf0e00eSMathieu Poirier */ 1526fdf0e00eSMathieu Poirier ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1527fdf0e00eSMathieu Poirier if (ret) { 1528fdf0e00eSMathieu Poirier dev_err(dev, "Failed to process resources: %d\n", ret); 1529fdf0e00eSMathieu Poirier goto disable_iommu; 1530fdf0e00eSMathieu Poirier } 1531fdf0e00eSMathieu Poirier 1532fdf0e00eSMathieu Poirier /* Allocate carveout resources associated to rproc */ 1533fdf0e00eSMathieu Poirier ret = rproc_alloc_registered_carveouts(rproc); 1534fdf0e00eSMathieu Poirier if (ret) { 1535fdf0e00eSMathieu Poirier dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1536fdf0e00eSMathieu Poirier ret); 1537fdf0e00eSMathieu Poirier goto clean_up_resources; 1538fdf0e00eSMathieu Poirier } 1539fdf0e00eSMathieu Poirier 1540fdf0e00eSMathieu Poirier ret = rproc_attach(rproc); 1541fdf0e00eSMathieu Poirier if (ret) 1542fdf0e00eSMathieu Poirier goto clean_up_resources; 1543fdf0e00eSMathieu Poirier 1544fdf0e00eSMathieu Poirier return 0; 1545fdf0e00eSMathieu Poirier 1546fdf0e00eSMathieu Poirier clean_up_resources: 1547fdf0e00eSMathieu Poirier rproc_resource_cleanup(rproc); 1548fdf0e00eSMathieu Poirier disable_iommu: 1549fdf0e00eSMathieu Poirier rproc_disable_iommu(rproc); 1550fdf0e00eSMathieu Poirier return ret; 1551fdf0e00eSMathieu Poirier } 1552fdf0e00eSMathieu Poirier 1553fdf0e00eSMathieu Poirier /* 15545e6533f7SSarangdhar Joshi * take a firmware and boot it up. 1555400e64dfSOhad Ben-Cohen * 1556400e64dfSOhad Ben-Cohen * Note: this function is called asynchronously upon registration of the 1557400e64dfSOhad Ben-Cohen * remote processor (so we must wait until it completes before we try 1558400e64dfSOhad Ben-Cohen * to unregister the device. one other option is just to use kref here, 1559400e64dfSOhad Ben-Cohen * that might be cleaner). 1560400e64dfSOhad Ben-Cohen */ 15615e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context) 1562400e64dfSOhad Ben-Cohen { 1563400e64dfSOhad Ben-Cohen struct rproc *rproc = context; 1564a2b950acSOhad Ben-Cohen 15652bfc311aSBjorn Andersson rproc_boot(rproc); 1566ddf71187SBjorn Andersson 1567400e64dfSOhad Ben-Cohen release_firmware(fw); 1568400e64dfSOhad Ben-Cohen } 1569400e64dfSOhad Ben-Cohen 15705e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc) 157170b85ef8SFernando Guzman Lugo { 157270b85ef8SFernando Guzman Lugo int ret; 157370b85ef8SFernando Guzman Lugo 157470b85ef8SFernando Guzman Lugo /* 157570b85ef8SFernando Guzman Lugo * We're initiating an asynchronous firmware loading, so we can 157670b85ef8SFernando Guzman Lugo * be built-in kernel code, without hanging the boot process. 157770b85ef8SFernando Guzman Lugo */ 157870b85ef8SFernando Guzman Lugo ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, 157970b85ef8SFernando Guzman Lugo rproc->firmware, &rproc->dev, GFP_KERNEL, 15805e6533f7SSarangdhar Joshi rproc, rproc_auto_boot_callback); 15812099c77dSSarangdhar Joshi if (ret < 0) 158270b85ef8SFernando Guzman Lugo dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); 158370b85ef8SFernando Guzman Lugo 158470b85ef8SFernando Guzman Lugo return ret; 158570b85ef8SFernando Guzman Lugo } 158670b85ef8SFernando Guzman Lugo 1587880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed) 15881efa30d0SSarangdhar Joshi { 15891efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 15901efa30d0SSarangdhar Joshi int ret; 15911efa30d0SSarangdhar Joshi 1592618fcff3SBjorn Andersson /* Stop any subdevices for the remote processor */ 1593618fcff3SBjorn Andersson rproc_stop_subdevices(rproc, crashed); 15941efa30d0SSarangdhar Joshi 15950a8b81cbSBjorn Andersson /* the installed resource table is no longer accessible */ 15960a8b81cbSBjorn Andersson rproc->table_ptr = rproc->cached_table; 15970a8b81cbSBjorn Andersson 15981efa30d0SSarangdhar Joshi /* power off the remote processor */ 15991efa30d0SSarangdhar Joshi ret = rproc->ops->stop(rproc); 16001efa30d0SSarangdhar Joshi if (ret) { 16011efa30d0SSarangdhar Joshi dev_err(dev, "can't stop rproc: %d\n", ret); 16021efa30d0SSarangdhar Joshi return ret; 16031efa30d0SSarangdhar Joshi } 16041efa30d0SSarangdhar Joshi 1605c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1606c455daa4SBjorn Andersson 16071efa30d0SSarangdhar Joshi rproc->state = RPROC_OFFLINE; 16081efa30d0SSarangdhar Joshi 16091efa30d0SSarangdhar Joshi dev_info(dev, "stopped remote processor %s\n", rproc->name); 16101efa30d0SSarangdhar Joshi 16111efa30d0SSarangdhar Joshi return 0; 16121efa30d0SSarangdhar Joshi } 16131efa30d0SSarangdhar Joshi 161470b85ef8SFernando Guzman Lugo /** 16152666ca91SSarangdhar Joshi * rproc_coredump_add_segment() - add segment of device memory to coredump 16162666ca91SSarangdhar Joshi * @rproc: handle of a remote processor 16172666ca91SSarangdhar Joshi * @da: device address 16182666ca91SSarangdhar Joshi * @size: size of segment 16192666ca91SSarangdhar Joshi * 16202666ca91SSarangdhar Joshi * Add device memory to the list of segments to be included in a coredump for 16212666ca91SSarangdhar Joshi * the remoteproc. 16222666ca91SSarangdhar Joshi * 16232666ca91SSarangdhar Joshi * Return: 0 on success, negative errno on error. 16242666ca91SSarangdhar Joshi */ 16252666ca91SSarangdhar Joshi int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size) 16262666ca91SSarangdhar Joshi { 16272666ca91SSarangdhar Joshi struct rproc_dump_segment *segment; 16282666ca91SSarangdhar Joshi 16292666ca91SSarangdhar Joshi segment = kzalloc(sizeof(*segment), GFP_KERNEL); 16302666ca91SSarangdhar Joshi if (!segment) 16312666ca91SSarangdhar Joshi return -ENOMEM; 16322666ca91SSarangdhar Joshi 16332666ca91SSarangdhar Joshi segment->da = da; 16342666ca91SSarangdhar Joshi segment->size = size; 16352666ca91SSarangdhar Joshi 16362666ca91SSarangdhar Joshi list_add_tail(&segment->node, &rproc->dump_segments); 16372666ca91SSarangdhar Joshi 16382666ca91SSarangdhar Joshi return 0; 16392666ca91SSarangdhar Joshi } 16402666ca91SSarangdhar Joshi EXPORT_SYMBOL(rproc_coredump_add_segment); 16412666ca91SSarangdhar Joshi 16422666ca91SSarangdhar Joshi /** 1643ab8f873bSSibi Sankar * rproc_coredump_add_custom_segment() - add custom coredump segment 1644ab8f873bSSibi Sankar * @rproc: handle of a remote processor 1645ab8f873bSSibi Sankar * @da: device address 1646ab8f873bSSibi Sankar * @size: size of segment 1647ab8f873bSSibi Sankar * @dumpfn: custom dump function called for each segment during coredump 1648ab8f873bSSibi Sankar * @priv: private data 1649ab8f873bSSibi Sankar * 1650ab8f873bSSibi Sankar * Add device memory to the list of segments to be included in the coredump 1651ab8f873bSSibi Sankar * and associate the segment with the given custom dump function and private 1652ab8f873bSSibi Sankar * data. 1653ab8f873bSSibi Sankar * 1654ab8f873bSSibi Sankar * Return: 0 on success, negative errno on error. 1655ab8f873bSSibi Sankar */ 1656ab8f873bSSibi Sankar int rproc_coredump_add_custom_segment(struct rproc *rproc, 1657ab8f873bSSibi Sankar dma_addr_t da, size_t size, 1658ab8f873bSSibi Sankar void (*dumpfn)(struct rproc *rproc, 1659ab8f873bSSibi Sankar struct rproc_dump_segment *segment, 1660ab8f873bSSibi Sankar void *dest), 1661ab8f873bSSibi Sankar void *priv) 1662ab8f873bSSibi Sankar { 1663ab8f873bSSibi Sankar struct rproc_dump_segment *segment; 1664ab8f873bSSibi Sankar 1665ab8f873bSSibi Sankar segment = kzalloc(sizeof(*segment), GFP_KERNEL); 1666ab8f873bSSibi Sankar if (!segment) 1667ab8f873bSSibi Sankar return -ENOMEM; 1668ab8f873bSSibi Sankar 1669ab8f873bSSibi Sankar segment->da = da; 1670ab8f873bSSibi Sankar segment->size = size; 1671ab8f873bSSibi Sankar segment->priv = priv; 1672ab8f873bSSibi Sankar segment->dump = dumpfn; 1673ab8f873bSSibi Sankar 1674ab8f873bSSibi Sankar list_add_tail(&segment->node, &rproc->dump_segments); 1675ab8f873bSSibi Sankar 1676ab8f873bSSibi Sankar return 0; 1677ab8f873bSSibi Sankar } 1678ab8f873bSSibi Sankar EXPORT_SYMBOL(rproc_coredump_add_custom_segment); 1679ab8f873bSSibi Sankar 1680ab8f873bSSibi Sankar /** 1681418fd787SClement Leger * rproc_coredump_set_elf_info() - set coredump elf information 1682418fd787SClement Leger * @rproc: handle of a remote processor 1683418fd787SClement Leger * @class: elf class for coredump elf file 1684418fd787SClement Leger * @machine: elf machine for coredump elf file 1685418fd787SClement Leger * 1686418fd787SClement Leger * Set elf information which will be used for coredump elf file. 1687418fd787SClement Leger * 1688418fd787SClement Leger * Return: 0 on success, negative errno on error. 1689418fd787SClement Leger */ 1690418fd787SClement Leger int rproc_coredump_set_elf_info(struct rproc *rproc, u8 class, u16 machine) 1691418fd787SClement Leger { 1692418fd787SClement Leger if (class != ELFCLASS64 && class != ELFCLASS32) 1693418fd787SClement Leger return -EINVAL; 1694418fd787SClement Leger 1695418fd787SClement Leger rproc->elf_class = class; 1696418fd787SClement Leger rproc->elf_machine = machine; 1697418fd787SClement Leger 1698418fd787SClement Leger return 0; 1699418fd787SClement Leger } 1700418fd787SClement Leger EXPORT_SYMBOL(rproc_coredump_set_elf_info); 1701418fd787SClement Leger 1702418fd787SClement Leger /** 17032666ca91SSarangdhar Joshi * rproc_coredump() - perform coredump 17042666ca91SSarangdhar Joshi * @rproc: rproc handle 17052666ca91SSarangdhar Joshi * 17062666ca91SSarangdhar Joshi * This function will generate an ELF header for the registered segments 17072666ca91SSarangdhar Joshi * and create a devcoredump device associated with rproc. 17082666ca91SSarangdhar Joshi */ 17092666ca91SSarangdhar Joshi static void rproc_coredump(struct rproc *rproc) 17102666ca91SSarangdhar Joshi { 17112666ca91SSarangdhar Joshi struct rproc_dump_segment *segment; 17128f403350SClement Leger void *phdr; 17138f403350SClement Leger void *ehdr; 17142666ca91SSarangdhar Joshi size_t data_size; 17152666ca91SSarangdhar Joshi size_t offset; 17162666ca91SSarangdhar Joshi void *data; 17172666ca91SSarangdhar Joshi void *ptr; 17188f403350SClement Leger u8 class = rproc->elf_class; 17192666ca91SSarangdhar Joshi int phnum = 0; 17202666ca91SSarangdhar Joshi 17212666ca91SSarangdhar Joshi if (list_empty(&rproc->dump_segments)) 17222666ca91SSarangdhar Joshi return; 17232666ca91SSarangdhar Joshi 1724418fd787SClement Leger if (class == ELFCLASSNONE) { 1725418fd787SClement Leger dev_err(&rproc->dev, "Elf class is not set\n"); 1726418fd787SClement Leger return; 1727418fd787SClement Leger } 1728418fd787SClement Leger 17298f403350SClement Leger data_size = elf_size_of_hdr(class); 17302666ca91SSarangdhar Joshi list_for_each_entry(segment, &rproc->dump_segments, node) { 17318f403350SClement Leger data_size += elf_size_of_phdr(class) + segment->size; 17322666ca91SSarangdhar Joshi 17332666ca91SSarangdhar Joshi phnum++; 17342666ca91SSarangdhar Joshi } 17352666ca91SSarangdhar Joshi 17362666ca91SSarangdhar Joshi data = vmalloc(data_size); 17372666ca91SSarangdhar Joshi if (!data) 17382666ca91SSarangdhar Joshi return; 17392666ca91SSarangdhar Joshi 17402666ca91SSarangdhar Joshi ehdr = data; 17412666ca91SSarangdhar Joshi 17428f403350SClement Leger memset(ehdr, 0, elf_size_of_hdr(class)); 17438f403350SClement Leger /* e_ident field is common for both elf32 and elf64 */ 17448f403350SClement Leger elf_hdr_init_ident(ehdr, class); 17452666ca91SSarangdhar Joshi 17468f403350SClement Leger elf_hdr_set_e_type(class, ehdr, ET_CORE); 1747418fd787SClement Leger elf_hdr_set_e_machine(class, ehdr, rproc->elf_machine); 17488f403350SClement Leger elf_hdr_set_e_version(class, ehdr, EV_CURRENT); 17498f403350SClement Leger elf_hdr_set_e_entry(class, ehdr, rproc->bootaddr); 17508f403350SClement Leger elf_hdr_set_e_phoff(class, ehdr, elf_size_of_hdr(class)); 17518f403350SClement Leger elf_hdr_set_e_ehsize(class, ehdr, elf_size_of_hdr(class)); 17528f403350SClement Leger elf_hdr_set_e_phentsize(class, ehdr, elf_size_of_phdr(class)); 17538f403350SClement Leger elf_hdr_set_e_phnum(class, ehdr, phnum); 17548f403350SClement Leger 17558f403350SClement Leger phdr = data + elf_hdr_get_e_phoff(class, ehdr); 17568f403350SClement Leger offset = elf_hdr_get_e_phoff(class, ehdr); 17578f403350SClement Leger offset += elf_size_of_phdr(class) * elf_hdr_get_e_phnum(class, ehdr); 17588f403350SClement Leger 17592666ca91SSarangdhar Joshi list_for_each_entry(segment, &rproc->dump_segments, node) { 17608f403350SClement Leger memset(phdr, 0, elf_size_of_phdr(class)); 17618f403350SClement Leger elf_phdr_set_p_type(class, phdr, PT_LOAD); 17628f403350SClement Leger elf_phdr_set_p_offset(class, phdr, offset); 17638f403350SClement Leger elf_phdr_set_p_vaddr(class, phdr, segment->da); 17648f403350SClement Leger elf_phdr_set_p_paddr(class, phdr, segment->da); 17658f403350SClement Leger elf_phdr_set_p_filesz(class, phdr, segment->size); 17668f403350SClement Leger elf_phdr_set_p_memsz(class, phdr, segment->size); 17678f403350SClement Leger elf_phdr_set_p_flags(class, phdr, PF_R | PF_W | PF_X); 17688f403350SClement Leger elf_phdr_set_p_align(class, phdr, 0); 17692666ca91SSarangdhar Joshi 17703952105dSSibi Sankar if (segment->dump) { 17713952105dSSibi Sankar segment->dump(rproc, segment, data + offset); 17723952105dSSibi Sankar } else { 17732666ca91SSarangdhar Joshi ptr = rproc_da_to_va(rproc, segment->da, segment->size); 17742666ca91SSarangdhar Joshi if (!ptr) { 17752666ca91SSarangdhar Joshi dev_err(&rproc->dev, 17762666ca91SSarangdhar Joshi "invalid coredump segment (%pad, %zu)\n", 17772666ca91SSarangdhar Joshi &segment->da, segment->size); 17782666ca91SSarangdhar Joshi memset(data + offset, 0xff, segment->size); 17792666ca91SSarangdhar Joshi } else { 17802666ca91SSarangdhar Joshi memcpy(data + offset, ptr, segment->size); 17812666ca91SSarangdhar Joshi } 17823952105dSSibi Sankar } 17832666ca91SSarangdhar Joshi 17848f403350SClement Leger offset += elf_phdr_get_p_filesz(class, phdr); 17858f403350SClement Leger phdr += elf_size_of_phdr(class); 17862666ca91SSarangdhar Joshi } 17872666ca91SSarangdhar Joshi 17882666ca91SSarangdhar Joshi dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL); 17892666ca91SSarangdhar Joshi } 17902666ca91SSarangdhar Joshi 17912666ca91SSarangdhar Joshi /** 179270b85ef8SFernando Guzman Lugo * rproc_trigger_recovery() - recover a remoteproc 179370b85ef8SFernando Guzman Lugo * @rproc: the remote processor 179470b85ef8SFernando Guzman Lugo * 179556324d7aSAnna, Suman * The recovery is done by resetting all the virtio devices, that way all the 179670b85ef8SFernando Guzman Lugo * rpmsg drivers will be reseted along with the remote processor making the 179770b85ef8SFernando Guzman Lugo * remoteproc functional again. 179870b85ef8SFernando Guzman Lugo * 179970b85ef8SFernando Guzman Lugo * This function can sleep, so it cannot be called from atomic context. 180070b85ef8SFernando Guzman Lugo */ 180170b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc) 180270b85ef8SFernando Guzman Lugo { 18037e83cab8SSarangdhar Joshi const struct firmware *firmware_p; 18047e83cab8SSarangdhar Joshi struct device *dev = &rproc->dev; 18057e83cab8SSarangdhar Joshi int ret; 18067e83cab8SSarangdhar Joshi 18077e83cab8SSarangdhar Joshi ret = mutex_lock_interruptible(&rproc->lock); 18087e83cab8SSarangdhar Joshi if (ret) 18097e83cab8SSarangdhar Joshi return ret; 18107e83cab8SSarangdhar Joshi 18110b145574SAlex Elder /* State could have changed before we got the mutex */ 18120b145574SAlex Elder if (rproc->state != RPROC_CRASHED) 18130b145574SAlex Elder goto unlock_mutex; 18140b145574SAlex Elder 18150b145574SAlex Elder dev_err(dev, "recovering %s\n", rproc->name); 18160b145574SAlex Elder 1817fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, true); 18187e83cab8SSarangdhar Joshi if (ret) 18197e83cab8SSarangdhar Joshi goto unlock_mutex; 1820ddf71187SBjorn Andersson 18212666ca91SSarangdhar Joshi /* generate coredump */ 18222666ca91SSarangdhar Joshi rproc_coredump(rproc); 18232666ca91SSarangdhar Joshi 18247e83cab8SSarangdhar Joshi /* load firmware */ 18257e83cab8SSarangdhar Joshi ret = request_firmware(&firmware_p, rproc->firmware, dev); 18267e83cab8SSarangdhar Joshi if (ret < 0) { 18277e83cab8SSarangdhar Joshi dev_err(dev, "request_firmware failed: %d\n", ret); 18287e83cab8SSarangdhar Joshi goto unlock_mutex; 18297e83cab8SSarangdhar Joshi } 1830ddf71187SBjorn Andersson 18317e83cab8SSarangdhar Joshi /* boot the remote processor up again */ 18327e83cab8SSarangdhar Joshi ret = rproc_start(rproc, firmware_p); 18337e83cab8SSarangdhar Joshi 18347e83cab8SSarangdhar Joshi release_firmware(firmware_p); 18357e83cab8SSarangdhar Joshi 18367e83cab8SSarangdhar Joshi unlock_mutex: 18377e83cab8SSarangdhar Joshi mutex_unlock(&rproc->lock); 18387e83cab8SSarangdhar Joshi return ret; 183970b85ef8SFernando Guzman Lugo } 184070b85ef8SFernando Guzman Lugo 1841400e64dfSOhad Ben-Cohen /** 18428afd519cSFernando Guzman Lugo * rproc_crash_handler_work() - handle a crash 18432e7d4c2cSArnaud Pouliquen * @work: work treating the crash 18448afd519cSFernando Guzman Lugo * 18458afd519cSFernando Guzman Lugo * This function needs to handle everything related to a crash, like cpu 18468afd519cSFernando Guzman Lugo * registers and stack dump, information to help to debug the fatal error, etc. 18478afd519cSFernando Guzman Lugo */ 18488afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work) 18498afd519cSFernando Guzman Lugo { 18508afd519cSFernando Guzman Lugo struct rproc *rproc = container_of(work, struct rproc, crash_handler); 18518afd519cSFernando Guzman Lugo struct device *dev = &rproc->dev; 18528afd519cSFernando Guzman Lugo 18538afd519cSFernando Guzman Lugo dev_dbg(dev, "enter %s\n", __func__); 18548afd519cSFernando Guzman Lugo 18558afd519cSFernando Guzman Lugo mutex_lock(&rproc->lock); 18568afd519cSFernando Guzman Lugo 18578afd519cSFernando Guzman Lugo if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) { 18588afd519cSFernando Guzman Lugo /* handle only the first crash detected */ 18598afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 18608afd519cSFernando Guzman Lugo return; 18618afd519cSFernando Guzman Lugo } 18628afd519cSFernando Guzman Lugo 18638afd519cSFernando Guzman Lugo rproc->state = RPROC_CRASHED; 18648afd519cSFernando Guzman Lugo dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, 18658afd519cSFernando Guzman Lugo rproc->name); 18668afd519cSFernando Guzman Lugo 18678afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 18688afd519cSFernando Guzman Lugo 18692e37abb8SFernando Guzman Lugo if (!rproc->recovery_disabled) 187070b85ef8SFernando Guzman Lugo rproc_trigger_recovery(rproc); 1871a781e5aaSRishabh Bhatnagar 1872a781e5aaSRishabh Bhatnagar pm_relax(rproc->dev.parent); 18738afd519cSFernando Guzman Lugo } 18748afd519cSFernando Guzman Lugo 18758afd519cSFernando Guzman Lugo /** 18761b0ef906SSuman Anna * rproc_boot() - boot a remote processor 1877400e64dfSOhad Ben-Cohen * @rproc: handle of a remote processor 1878400e64dfSOhad Ben-Cohen * 1879400e64dfSOhad Ben-Cohen * Boot a remote processor (i.e. load its firmware, power it on, ...). 1880400e64dfSOhad Ben-Cohen * 1881400e64dfSOhad Ben-Cohen * If the remote processor is already powered on, this function immediately 1882400e64dfSOhad Ben-Cohen * returns (successfully). 1883400e64dfSOhad Ben-Cohen * 1884400e64dfSOhad Ben-Cohen * Returns 0 on success, and an appropriate error value otherwise. 1885400e64dfSOhad Ben-Cohen */ 18861b0ef906SSuman Anna int rproc_boot(struct rproc *rproc) 1887400e64dfSOhad Ben-Cohen { 1888400e64dfSOhad Ben-Cohen const struct firmware *firmware_p; 1889400e64dfSOhad Ben-Cohen struct device *dev; 1890400e64dfSOhad Ben-Cohen int ret; 1891400e64dfSOhad Ben-Cohen 1892400e64dfSOhad Ben-Cohen if (!rproc) { 1893400e64dfSOhad Ben-Cohen pr_err("invalid rproc handle\n"); 1894400e64dfSOhad Ben-Cohen return -EINVAL; 1895400e64dfSOhad Ben-Cohen } 1896400e64dfSOhad Ben-Cohen 1897b5ab5e24SOhad Ben-Cohen dev = &rproc->dev; 1898400e64dfSOhad Ben-Cohen 1899400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1900400e64dfSOhad Ben-Cohen if (ret) { 1901400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1902400e64dfSOhad Ben-Cohen return ret; 1903400e64dfSOhad Ben-Cohen } 1904400e64dfSOhad Ben-Cohen 19052099c77dSSarangdhar Joshi if (rproc->state == RPROC_DELETED) { 19062099c77dSSarangdhar Joshi ret = -ENODEV; 19072099c77dSSarangdhar Joshi dev_err(dev, "can't boot deleted rproc %s\n", rproc->name); 19082099c77dSSarangdhar Joshi goto unlock_mutex; 19092099c77dSSarangdhar Joshi } 19102099c77dSSarangdhar Joshi 1911*0f9dc562SMathieu Poirier /* skip the boot or attach process if rproc is already powered up */ 1912400e64dfSOhad Ben-Cohen if (atomic_inc_return(&rproc->power) > 1) { 1913400e64dfSOhad Ben-Cohen ret = 0; 1914400e64dfSOhad Ben-Cohen goto unlock_mutex; 1915400e64dfSOhad Ben-Cohen } 1916400e64dfSOhad Ben-Cohen 1917*0f9dc562SMathieu Poirier if (rproc->state == RPROC_DETACHED) { 1918*0f9dc562SMathieu Poirier dev_info(dev, "attaching to %s\n", rproc->name); 1919*0f9dc562SMathieu Poirier 1920*0f9dc562SMathieu Poirier ret = rproc_actuate(rproc); 1921*0f9dc562SMathieu Poirier } else { 1922400e64dfSOhad Ben-Cohen dev_info(dev, "powering up %s\n", rproc->name); 1923400e64dfSOhad Ben-Cohen 1924400e64dfSOhad Ben-Cohen /* load firmware */ 1925400e64dfSOhad Ben-Cohen ret = request_firmware(&firmware_p, rproc->firmware, dev); 1926400e64dfSOhad Ben-Cohen if (ret < 0) { 1927400e64dfSOhad Ben-Cohen dev_err(dev, "request_firmware failed: %d\n", ret); 1928400e64dfSOhad Ben-Cohen goto downref_rproc; 1929400e64dfSOhad Ben-Cohen } 1930400e64dfSOhad Ben-Cohen 1931400e64dfSOhad Ben-Cohen ret = rproc_fw_boot(rproc, firmware_p); 1932400e64dfSOhad Ben-Cohen 1933400e64dfSOhad Ben-Cohen release_firmware(firmware_p); 1934*0f9dc562SMathieu Poirier } 1935400e64dfSOhad Ben-Cohen 1936400e64dfSOhad Ben-Cohen downref_rproc: 1937fbb6aacbSBjorn Andersson if (ret) 1938400e64dfSOhad Ben-Cohen atomic_dec(&rproc->power); 1939400e64dfSOhad Ben-Cohen unlock_mutex: 1940400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1941400e64dfSOhad Ben-Cohen return ret; 1942400e64dfSOhad Ben-Cohen } 1943400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot); 1944400e64dfSOhad Ben-Cohen 1945400e64dfSOhad Ben-Cohen /** 1946400e64dfSOhad Ben-Cohen * rproc_shutdown() - power off the remote processor 1947400e64dfSOhad Ben-Cohen * @rproc: the remote processor 1948400e64dfSOhad Ben-Cohen * 1949400e64dfSOhad Ben-Cohen * Power off a remote processor (previously booted with rproc_boot()). 1950400e64dfSOhad Ben-Cohen * 1951400e64dfSOhad Ben-Cohen * In case @rproc is still being used by an additional user(s), then 1952400e64dfSOhad Ben-Cohen * this function will just decrement the power refcount and exit, 1953400e64dfSOhad Ben-Cohen * without really powering off the device. 1954400e64dfSOhad Ben-Cohen * 1955400e64dfSOhad Ben-Cohen * Every call to rproc_boot() must (eventually) be accompanied by a call 1956400e64dfSOhad Ben-Cohen * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. 1957400e64dfSOhad Ben-Cohen * 1958400e64dfSOhad Ben-Cohen * Notes: 1959400e64dfSOhad Ben-Cohen * - we're not decrementing the rproc's refcount, only the power refcount. 1960400e64dfSOhad Ben-Cohen * which means that the @rproc handle stays valid even after rproc_shutdown() 1961400e64dfSOhad Ben-Cohen * returns, and users can still use it with a subsequent rproc_boot(), if 1962400e64dfSOhad Ben-Cohen * needed. 1963400e64dfSOhad Ben-Cohen */ 1964400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc) 1965400e64dfSOhad Ben-Cohen { 1966b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1967400e64dfSOhad Ben-Cohen int ret; 1968400e64dfSOhad Ben-Cohen 1969400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1970400e64dfSOhad Ben-Cohen if (ret) { 1971400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1972400e64dfSOhad Ben-Cohen return; 1973400e64dfSOhad Ben-Cohen } 1974400e64dfSOhad Ben-Cohen 1975400e64dfSOhad Ben-Cohen /* if the remote proc is still needed, bail out */ 1976400e64dfSOhad Ben-Cohen if (!atomic_dec_and_test(&rproc->power)) 1977400e64dfSOhad Ben-Cohen goto out; 1978400e64dfSOhad Ben-Cohen 1979fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, false); 1980400e64dfSOhad Ben-Cohen if (ret) { 1981400e64dfSOhad Ben-Cohen atomic_inc(&rproc->power); 1982400e64dfSOhad Ben-Cohen goto out; 1983400e64dfSOhad Ben-Cohen } 1984400e64dfSOhad Ben-Cohen 1985400e64dfSOhad Ben-Cohen /* clean up all acquired resources */ 1986400e64dfSOhad Ben-Cohen rproc_resource_cleanup(rproc); 1987400e64dfSOhad Ben-Cohen 198833467ac3SLoic Pallardy /* release HW resources if needed */ 198933467ac3SLoic Pallardy rproc_unprepare_device(rproc); 199033467ac3SLoic Pallardy 1991400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1992400e64dfSOhad Ben-Cohen 1993988d204cSBjorn Andersson /* Free the copy of the resource table */ 1994a0c10687SBjorn Andersson kfree(rproc->cached_table); 1995a0c10687SBjorn Andersson rproc->cached_table = NULL; 1996988d204cSBjorn Andersson rproc->table_ptr = NULL; 1997400e64dfSOhad Ben-Cohen out: 1998400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1999400e64dfSOhad Ben-Cohen } 2000400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown); 2001400e64dfSOhad Ben-Cohen 2002400e64dfSOhad Ben-Cohen /** 2003fec47d86SDave Gerlach * rproc_get_by_phandle() - find a remote processor by phandle 2004fec47d86SDave Gerlach * @phandle: phandle to the rproc 2005fec47d86SDave Gerlach * 2006fec47d86SDave Gerlach * Finds an rproc handle using the remote processor's phandle, and then 2007fec47d86SDave Gerlach * return a handle to the rproc. 2008fec47d86SDave Gerlach * 2009fec47d86SDave Gerlach * This function increments the remote processor's refcount, so always 2010fec47d86SDave Gerlach * use rproc_put() to decrement it back once rproc isn't needed anymore. 2011fec47d86SDave Gerlach * 2012fec47d86SDave Gerlach * Returns the rproc handle on success, and NULL on failure. 2013fec47d86SDave Gerlach */ 20148de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF 2015fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle) 2016fec47d86SDave Gerlach { 2017fec47d86SDave Gerlach struct rproc *rproc = NULL, *r; 2018fec47d86SDave Gerlach struct device_node *np; 2019fec47d86SDave Gerlach 2020fec47d86SDave Gerlach np = of_find_node_by_phandle(phandle); 2021fec47d86SDave Gerlach if (!np) 2022fec47d86SDave Gerlach return NULL; 2023fec47d86SDave Gerlach 2024c0abe2caSBjorn Andersson rcu_read_lock(); 2025c0abe2caSBjorn Andersson list_for_each_entry_rcu(r, &rproc_list, node) { 2026fec47d86SDave Gerlach if (r->dev.parent && r->dev.parent->of_node == np) { 2027fbb6aacbSBjorn Andersson /* prevent underlying implementation from being removed */ 2028fbb6aacbSBjorn Andersson if (!try_module_get(r->dev.parent->driver->owner)) { 2029fbb6aacbSBjorn Andersson dev_err(&r->dev, "can't get owner\n"); 2030fbb6aacbSBjorn Andersson break; 2031fbb6aacbSBjorn Andersson } 2032fbb6aacbSBjorn Andersson 2033fec47d86SDave Gerlach rproc = r; 2034fec47d86SDave Gerlach get_device(&rproc->dev); 2035fec47d86SDave Gerlach break; 2036fec47d86SDave Gerlach } 2037fec47d86SDave Gerlach } 2038c0abe2caSBjorn Andersson rcu_read_unlock(); 2039fec47d86SDave Gerlach 2040fec47d86SDave Gerlach of_node_put(np); 2041fec47d86SDave Gerlach 2042fec47d86SDave Gerlach return rproc; 2043fec47d86SDave Gerlach } 20448de3dbd0SOhad Ben-Cohen #else 20458de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle) 20468de3dbd0SOhad Ben-Cohen { 20478de3dbd0SOhad Ben-Cohen return NULL; 20488de3dbd0SOhad Ben-Cohen } 20498de3dbd0SOhad Ben-Cohen #endif 2050fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle); 2051fec47d86SDave Gerlach 205288d3a136SMathieu Poirier static int rproc_validate(struct rproc *rproc) 205388d3a136SMathieu Poirier { 205488d3a136SMathieu Poirier switch (rproc->state) { 205588d3a136SMathieu Poirier case RPROC_OFFLINE: 205688d3a136SMathieu Poirier /* 205788d3a136SMathieu Poirier * An offline processor without a start() 205888d3a136SMathieu Poirier * function makes no sense. 205988d3a136SMathieu Poirier */ 206088d3a136SMathieu Poirier if (!rproc->ops->start) 206188d3a136SMathieu Poirier return -EINVAL; 206288d3a136SMathieu Poirier break; 206388d3a136SMathieu Poirier case RPROC_DETACHED: 206488d3a136SMathieu Poirier /* 206588d3a136SMathieu Poirier * A remote processor in a detached state without an 206688d3a136SMathieu Poirier * attach() function makes not sense. 206788d3a136SMathieu Poirier */ 206888d3a136SMathieu Poirier if (!rproc->ops->attach) 206988d3a136SMathieu Poirier return -EINVAL; 207088d3a136SMathieu Poirier /* 207188d3a136SMathieu Poirier * When attaching to a remote processor the device memory 207288d3a136SMathieu Poirier * is already available and as such there is no need to have a 207388d3a136SMathieu Poirier * cached table. 207488d3a136SMathieu Poirier */ 207588d3a136SMathieu Poirier if (rproc->cached_table) 207688d3a136SMathieu Poirier return -EINVAL; 207788d3a136SMathieu Poirier break; 207888d3a136SMathieu Poirier default: 207988d3a136SMathieu Poirier /* 208088d3a136SMathieu Poirier * When adding a remote processor, the state of the device 208188d3a136SMathieu Poirier * can be offline or detached, nothing else. 208288d3a136SMathieu Poirier */ 208388d3a136SMathieu Poirier return -EINVAL; 208488d3a136SMathieu Poirier } 208588d3a136SMathieu Poirier 208688d3a136SMathieu Poirier return 0; 208788d3a136SMathieu Poirier } 208888d3a136SMathieu Poirier 2089fec47d86SDave Gerlach /** 2090160e7c84SOhad Ben-Cohen * rproc_add() - register a remote processor 2091400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle to register 2092400e64dfSOhad Ben-Cohen * 2093400e64dfSOhad Ben-Cohen * Registers @rproc with the remoteproc framework, after it has been 2094400e64dfSOhad Ben-Cohen * allocated with rproc_alloc(). 2095400e64dfSOhad Ben-Cohen * 2096400e64dfSOhad Ben-Cohen * This is called by the platform-specific rproc implementation, whenever 2097400e64dfSOhad Ben-Cohen * a new remote processor device is probed. 2098400e64dfSOhad Ben-Cohen * 2099400e64dfSOhad Ben-Cohen * Returns 0 on success and an appropriate error code otherwise. 2100400e64dfSOhad Ben-Cohen * 2101400e64dfSOhad Ben-Cohen * Note: this function initiates an asynchronous firmware loading 2102400e64dfSOhad Ben-Cohen * context, which will look for virtio devices supported by the rproc's 2103400e64dfSOhad Ben-Cohen * firmware. 2104400e64dfSOhad Ben-Cohen * 2105400e64dfSOhad Ben-Cohen * If found, those virtio devices will be created and added, so as a result 21067a186941SOhad Ben-Cohen * of registering this remote processor, additional virtio drivers might be 2107400e64dfSOhad Ben-Cohen * probed. 2108400e64dfSOhad Ben-Cohen */ 2109160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc) 2110400e64dfSOhad Ben-Cohen { 2111b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 211270b85ef8SFernando Guzman Lugo int ret; 2113400e64dfSOhad Ben-Cohen 2114b5ab5e24SOhad Ben-Cohen ret = device_add(dev); 2115b5ab5e24SOhad Ben-Cohen if (ret < 0) 2116b5ab5e24SOhad Ben-Cohen return ret; 2117400e64dfSOhad Ben-Cohen 211888d3a136SMathieu Poirier ret = rproc_validate(rproc); 211988d3a136SMathieu Poirier if (ret < 0) 212088d3a136SMathieu Poirier return ret; 212188d3a136SMathieu Poirier 2122b5ab5e24SOhad Ben-Cohen dev_info(dev, "%s is available\n", rproc->name); 2123400e64dfSOhad Ben-Cohen 2124400e64dfSOhad Ben-Cohen /* create debugfs entries */ 2125400e64dfSOhad Ben-Cohen rproc_create_debug_dir(rproc); 21267a20c64dSSarangdhar Joshi 21277a20c64dSSarangdhar Joshi /* if rproc is marked always-on, request it to boot */ 21287a20c64dSSarangdhar Joshi if (rproc->auto_boot) { 21295e6533f7SSarangdhar Joshi ret = rproc_trigger_auto_boot(rproc); 2130d2e12e66SDave Gerlach if (ret < 0) 2131d2e12e66SDave Gerlach return ret; 21327a20c64dSSarangdhar Joshi } 2133400e64dfSOhad Ben-Cohen 2134d2e12e66SDave Gerlach /* expose to rproc_get_by_phandle users */ 2135d2e12e66SDave Gerlach mutex_lock(&rproc_list_mutex); 2136c0abe2caSBjorn Andersson list_add_rcu(&rproc->node, &rproc_list); 2137d2e12e66SDave Gerlach mutex_unlock(&rproc_list_mutex); 2138d2e12e66SDave Gerlach 2139d2e12e66SDave Gerlach return 0; 2140400e64dfSOhad Ben-Cohen } 2141160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add); 2142400e64dfSOhad Ben-Cohen 2143305ac5a7SPaul Cercueil static void devm_rproc_remove(void *rproc) 2144305ac5a7SPaul Cercueil { 2145305ac5a7SPaul Cercueil rproc_del(rproc); 2146305ac5a7SPaul Cercueil } 2147305ac5a7SPaul Cercueil 2148305ac5a7SPaul Cercueil /** 2149305ac5a7SPaul Cercueil * devm_rproc_add() - resource managed rproc_add() 2150305ac5a7SPaul Cercueil * @dev: the underlying device 2151305ac5a7SPaul Cercueil * @rproc: the remote processor handle to register 2152305ac5a7SPaul Cercueil * 2153305ac5a7SPaul Cercueil * This function performs like rproc_add() but the registered rproc device will 2154305ac5a7SPaul Cercueil * automatically be removed on driver detach. 2155305ac5a7SPaul Cercueil * 2156305ac5a7SPaul Cercueil * Returns: 0 on success, negative errno on failure 2157305ac5a7SPaul Cercueil */ 2158305ac5a7SPaul Cercueil int devm_rproc_add(struct device *dev, struct rproc *rproc) 2159305ac5a7SPaul Cercueil { 2160305ac5a7SPaul Cercueil int err; 2161305ac5a7SPaul Cercueil 2162305ac5a7SPaul Cercueil err = rproc_add(rproc); 2163305ac5a7SPaul Cercueil if (err) 2164305ac5a7SPaul Cercueil return err; 2165305ac5a7SPaul Cercueil 2166305ac5a7SPaul Cercueil return devm_add_action_or_reset(dev, devm_rproc_remove, rproc); 2167305ac5a7SPaul Cercueil } 2168305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_add); 2169305ac5a7SPaul Cercueil 2170400e64dfSOhad Ben-Cohen /** 2171b5ab5e24SOhad Ben-Cohen * rproc_type_release() - release a remote processor instance 2172b5ab5e24SOhad Ben-Cohen * @dev: the rproc's device 2173b5ab5e24SOhad Ben-Cohen * 2174b5ab5e24SOhad Ben-Cohen * This function should _never_ be called directly. 2175b5ab5e24SOhad Ben-Cohen * 2176b5ab5e24SOhad Ben-Cohen * It will be called by the driver core when no one holds a valid pointer 2177b5ab5e24SOhad Ben-Cohen * to @dev anymore. 2178b5ab5e24SOhad Ben-Cohen */ 2179b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev) 2180b5ab5e24SOhad Ben-Cohen { 2181b5ab5e24SOhad Ben-Cohen struct rproc *rproc = container_of(dev, struct rproc, dev); 2182b5ab5e24SOhad Ben-Cohen 21837183a2a7SOhad Ben-Cohen dev_info(&rproc->dev, "releasing %s\n", rproc->name); 21847183a2a7SOhad Ben-Cohen 2185b5ab5e24SOhad Ben-Cohen idr_destroy(&rproc->notifyids); 2186b5ab5e24SOhad Ben-Cohen 2187b5ab5e24SOhad Ben-Cohen if (rproc->index >= 0) 2188b5ab5e24SOhad Ben-Cohen ida_simple_remove(&rproc_dev_index, rproc->index); 2189b5ab5e24SOhad Ben-Cohen 21901487dedaSMathieu Poirier kfree_const(rproc->firmware); 2191db655278SSuman Anna kfree_const(rproc->name); 2192fb98e2bdSBjorn Andersson kfree(rproc->ops); 2193b5ab5e24SOhad Ben-Cohen kfree(rproc); 2194b5ab5e24SOhad Ben-Cohen } 2195b5ab5e24SOhad Ben-Cohen 2196c42ca04dSBhumika Goyal static const struct device_type rproc_type = { 2197b5ab5e24SOhad Ben-Cohen .name = "remoteproc", 2198b5ab5e24SOhad Ben-Cohen .release = rproc_type_release, 2199b5ab5e24SOhad Ben-Cohen }; 2200400e64dfSOhad Ben-Cohen 22010c2ae2b1SMathieu Poirier static int rproc_alloc_firmware(struct rproc *rproc, 22020c2ae2b1SMathieu Poirier const char *name, const char *firmware) 22030c2ae2b1SMathieu Poirier { 22041487dedaSMathieu Poirier const char *p; 22050c2ae2b1SMathieu Poirier 22060c2ae2b1SMathieu Poirier /* 22079d5f82c8SMathieu Poirier * Allocate a firmware name if the caller gave us one to work 22089d5f82c8SMathieu Poirier * with. Otherwise construct a new one using a default pattern. 22090c2ae2b1SMathieu Poirier */ 22109d5f82c8SMathieu Poirier if (firmware) 22111487dedaSMathieu Poirier p = kstrdup_const(firmware, GFP_KERNEL); 22129d5f82c8SMathieu Poirier else 22139d5f82c8SMathieu Poirier p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name); 22144df4f8beSMathieu Poirier 22150c2ae2b1SMathieu Poirier if (!p) 22160c2ae2b1SMathieu Poirier return -ENOMEM; 22170c2ae2b1SMathieu Poirier 22180c2ae2b1SMathieu Poirier rproc->firmware = p; 22190c2ae2b1SMathieu Poirier 22200c2ae2b1SMathieu Poirier return 0; 22210c2ae2b1SMathieu Poirier } 22220c2ae2b1SMathieu Poirier 2223bf860aa1SMathieu Poirier static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops) 2224bf860aa1SMathieu Poirier { 2225bf860aa1SMathieu Poirier rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); 2226bf860aa1SMathieu Poirier if (!rproc->ops) 2227bf860aa1SMathieu Poirier return -ENOMEM; 2228bf860aa1SMathieu Poirier 2229bf860aa1SMathieu Poirier if (rproc->ops->load) 2230bf860aa1SMathieu Poirier return 0; 2231bf860aa1SMathieu Poirier 2232bf860aa1SMathieu Poirier /* Default to ELF loader if no load function is specified */ 2233bf860aa1SMathieu Poirier rproc->ops->load = rproc_elf_load_segments; 2234bf860aa1SMathieu Poirier rproc->ops->parse_fw = rproc_elf_load_rsc_table; 2235bf860aa1SMathieu Poirier rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; 2236e29ff72bSClement Leger rproc->ops->sanity_check = rproc_elf_sanity_check; 2237bf860aa1SMathieu Poirier rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; 2238bf860aa1SMathieu Poirier 2239bf860aa1SMathieu Poirier return 0; 2240bf860aa1SMathieu Poirier } 2241bf860aa1SMathieu Poirier 2242400e64dfSOhad Ben-Cohen /** 2243400e64dfSOhad Ben-Cohen * rproc_alloc() - allocate a remote processor handle 2244400e64dfSOhad Ben-Cohen * @dev: the underlying device 2245400e64dfSOhad Ben-Cohen * @name: name of this remote processor 2246400e64dfSOhad Ben-Cohen * @ops: platform-specific handlers (mainly start/stop) 22478b4aec9aSRobert Tivy * @firmware: name of firmware file to load, can be NULL 2248400e64dfSOhad Ben-Cohen * @len: length of private data needed by the rproc driver (in bytes) 2249400e64dfSOhad Ben-Cohen * 2250400e64dfSOhad Ben-Cohen * Allocates a new remote processor handle, but does not register 22518b4aec9aSRobert Tivy * it yet. if @firmware is NULL, a default name is used. 2252400e64dfSOhad Ben-Cohen * 2253400e64dfSOhad Ben-Cohen * This function should be used by rproc implementations during initialization 2254400e64dfSOhad Ben-Cohen * of the remote processor. 2255400e64dfSOhad Ben-Cohen * 2256400e64dfSOhad Ben-Cohen * After creating an rproc handle using this function, and when ready, 2257160e7c84SOhad Ben-Cohen * implementations should then call rproc_add() to complete 2258400e64dfSOhad Ben-Cohen * the registration of the remote processor. 2259400e64dfSOhad Ben-Cohen * 2260400e64dfSOhad Ben-Cohen * On success the new rproc is returned, and on failure, NULL. 2261400e64dfSOhad Ben-Cohen * 2262400e64dfSOhad Ben-Cohen * Note: _never_ directly deallocate @rproc, even if it was not registered 2263433c0e04SBjorn Andersson * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). 2264400e64dfSOhad Ben-Cohen */ 2265400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name, 2266400e64dfSOhad Ben-Cohen const struct rproc_ops *ops, 2267400e64dfSOhad Ben-Cohen const char *firmware, int len) 2268400e64dfSOhad Ben-Cohen { 2269400e64dfSOhad Ben-Cohen struct rproc *rproc; 2270400e64dfSOhad Ben-Cohen 2271400e64dfSOhad Ben-Cohen if (!dev || !name || !ops) 2272400e64dfSOhad Ben-Cohen return NULL; 2273400e64dfSOhad Ben-Cohen 22740f57dc6aSMatt Redfearn rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); 22750c2ae2b1SMathieu Poirier if (!rproc) 22760f57dc6aSMatt Redfearn return NULL; 22770c2ae2b1SMathieu Poirier 2278400e64dfSOhad Ben-Cohen rproc->priv = &rproc[1]; 2279ddf71187SBjorn Andersson rproc->auto_boot = true; 2280418fd787SClement Leger rproc->elf_class = ELFCLASSNONE; 2281418fd787SClement Leger rproc->elf_machine = EM_NONE; 2282400e64dfSOhad Ben-Cohen 2283b5ab5e24SOhad Ben-Cohen device_initialize(&rproc->dev); 2284b5ab5e24SOhad Ben-Cohen rproc->dev.parent = dev; 2285b5ab5e24SOhad Ben-Cohen rproc->dev.type = &rproc_type; 22862aefbef0SMatt Redfearn rproc->dev.class = &rproc_class; 22877c89717fSBjorn Andersson rproc->dev.driver_data = rproc; 22886442df49SAlex Elder idr_init(&rproc->notifyids); 2289b5ab5e24SOhad Ben-Cohen 2290db655278SSuman Anna rproc->name = kstrdup_const(name, GFP_KERNEL); 2291db655278SSuman Anna if (!rproc->name) 2292db655278SSuman Anna goto put_device; 2293db655278SSuman Anna 2294226f5db4SMathieu Poirier if (rproc_alloc_firmware(rproc, name, firmware)) 2295226f5db4SMathieu Poirier goto put_device; 2296226f5db4SMathieu Poirier 2297226f5db4SMathieu Poirier if (rproc_alloc_ops(rproc, ops)) 2298226f5db4SMathieu Poirier goto put_device; 2299226f5db4SMathieu Poirier 2300b5ab5e24SOhad Ben-Cohen /* Assign a unique device index and name */ 2301b5ab5e24SOhad Ben-Cohen rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL); 2302b5ab5e24SOhad Ben-Cohen if (rproc->index < 0) { 2303b5ab5e24SOhad Ben-Cohen dev_err(dev, "ida_simple_get failed: %d\n", rproc->index); 2304226f5db4SMathieu Poirier goto put_device; 2305b5ab5e24SOhad Ben-Cohen } 2306b5ab5e24SOhad Ben-Cohen 2307b5ab5e24SOhad Ben-Cohen dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); 2308b5ab5e24SOhad Ben-Cohen 2309400e64dfSOhad Ben-Cohen atomic_set(&rproc->power, 0); 2310400e64dfSOhad Ben-Cohen 2311400e64dfSOhad Ben-Cohen mutex_init(&rproc->lock); 2312400e64dfSOhad Ben-Cohen 2313400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->carveouts); 2314400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->mappings); 2315400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->traces); 23167a186941SOhad Ben-Cohen INIT_LIST_HEAD(&rproc->rvdevs); 23177bdc9650SBjorn Andersson INIT_LIST_HEAD(&rproc->subdevs); 23182666ca91SSarangdhar Joshi INIT_LIST_HEAD(&rproc->dump_segments); 2319400e64dfSOhad Ben-Cohen 23208afd519cSFernando Guzman Lugo INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); 23218afd519cSFernando Guzman Lugo 2322400e64dfSOhad Ben-Cohen rproc->state = RPROC_OFFLINE; 2323400e64dfSOhad Ben-Cohen 2324400e64dfSOhad Ben-Cohen return rproc; 23250c2ae2b1SMathieu Poirier 2326226f5db4SMathieu Poirier put_device: 2327226f5db4SMathieu Poirier put_device(&rproc->dev); 23280c2ae2b1SMathieu Poirier return NULL; 2329400e64dfSOhad Ben-Cohen } 2330400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc); 2331400e64dfSOhad Ben-Cohen 2332400e64dfSOhad Ben-Cohen /** 2333433c0e04SBjorn Andersson * rproc_free() - unroll rproc_alloc() 2334433c0e04SBjorn Andersson * @rproc: the remote processor handle 2335433c0e04SBjorn Andersson * 2336433c0e04SBjorn Andersson * This function decrements the rproc dev refcount. 2337433c0e04SBjorn Andersson * 2338433c0e04SBjorn Andersson * If no one holds any reference to rproc anymore, then its refcount would 2339433c0e04SBjorn Andersson * now drop to zero, and it would be freed. 2340433c0e04SBjorn Andersson */ 2341433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc) 2342433c0e04SBjorn Andersson { 2343433c0e04SBjorn Andersson put_device(&rproc->dev); 2344433c0e04SBjorn Andersson } 2345433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free); 2346433c0e04SBjorn Andersson 2347433c0e04SBjorn Andersson /** 2348433c0e04SBjorn Andersson * rproc_put() - release rproc reference 2349400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle 2350400e64dfSOhad Ben-Cohen * 2351c6b5a276SOhad Ben-Cohen * This function decrements the rproc dev refcount. 2352400e64dfSOhad Ben-Cohen * 2353c6b5a276SOhad Ben-Cohen * If no one holds any reference to rproc anymore, then its refcount would 2354c6b5a276SOhad Ben-Cohen * now drop to zero, and it would be freed. 2355400e64dfSOhad Ben-Cohen */ 2356160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc) 2357400e64dfSOhad Ben-Cohen { 2358fbb6aacbSBjorn Andersson module_put(rproc->dev.parent->driver->owner); 2359b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 2360400e64dfSOhad Ben-Cohen } 2361160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put); 2362400e64dfSOhad Ben-Cohen 2363400e64dfSOhad Ben-Cohen /** 2364160e7c84SOhad Ben-Cohen * rproc_del() - unregister a remote processor 2365400e64dfSOhad Ben-Cohen * @rproc: rproc handle to unregister 2366400e64dfSOhad Ben-Cohen * 2367400e64dfSOhad Ben-Cohen * This function should be called when the platform specific rproc 2368400e64dfSOhad Ben-Cohen * implementation decides to remove the rproc device. it should 2369160e7c84SOhad Ben-Cohen * _only_ be called if a previous invocation of rproc_add() 2370400e64dfSOhad Ben-Cohen * has completed successfully. 2371400e64dfSOhad Ben-Cohen * 2372160e7c84SOhad Ben-Cohen * After rproc_del() returns, @rproc isn't freed yet, because 2373c6b5a276SOhad Ben-Cohen * of the outstanding reference created by rproc_alloc. To decrement that 2374433c0e04SBjorn Andersson * one last refcount, one still needs to call rproc_free(). 2375400e64dfSOhad Ben-Cohen * 2376400e64dfSOhad Ben-Cohen * Returns 0 on success and -EINVAL if @rproc isn't valid. 2377400e64dfSOhad Ben-Cohen */ 2378160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc) 2379400e64dfSOhad Ben-Cohen { 2380400e64dfSOhad Ben-Cohen if (!rproc) 2381400e64dfSOhad Ben-Cohen return -EINVAL; 2382400e64dfSOhad Ben-Cohen 2383ddf71187SBjorn Andersson /* if rproc is marked always-on, rproc_add() booted it */ 2384ddf71187SBjorn Andersson /* TODO: make sure this works with rproc->power > 1 */ 2385ddf71187SBjorn Andersson if (rproc->auto_boot) 2386ddf71187SBjorn Andersson rproc_shutdown(rproc); 2387ddf71187SBjorn Andersson 23882099c77dSSarangdhar Joshi mutex_lock(&rproc->lock); 23892099c77dSSarangdhar Joshi rproc->state = RPROC_DELETED; 23902099c77dSSarangdhar Joshi mutex_unlock(&rproc->lock); 23912099c77dSSarangdhar Joshi 2392b003d45bSSarangdhar Joshi rproc_delete_debug_dir(rproc); 2393b003d45bSSarangdhar Joshi 2394fec47d86SDave Gerlach /* the rproc is downref'ed as soon as it's removed from the klist */ 2395fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 2396c0abe2caSBjorn Andersson list_del_rcu(&rproc->node); 2397fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 2398fec47d86SDave Gerlach 2399c0abe2caSBjorn Andersson /* Ensure that no readers of rproc_list are still active */ 2400c0abe2caSBjorn Andersson synchronize_rcu(); 2401c0abe2caSBjorn Andersson 2402b5ab5e24SOhad Ben-Cohen device_del(&rproc->dev); 2403400e64dfSOhad Ben-Cohen 2404400e64dfSOhad Ben-Cohen return 0; 2405400e64dfSOhad Ben-Cohen } 2406160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del); 2407400e64dfSOhad Ben-Cohen 2408305ac5a7SPaul Cercueil static void devm_rproc_free(struct device *dev, void *res) 2409305ac5a7SPaul Cercueil { 2410305ac5a7SPaul Cercueil rproc_free(*(struct rproc **)res); 2411305ac5a7SPaul Cercueil } 2412305ac5a7SPaul Cercueil 2413305ac5a7SPaul Cercueil /** 2414305ac5a7SPaul Cercueil * devm_rproc_alloc() - resource managed rproc_alloc() 2415305ac5a7SPaul Cercueil * @dev: the underlying device 2416305ac5a7SPaul Cercueil * @name: name of this remote processor 2417305ac5a7SPaul Cercueil * @ops: platform-specific handlers (mainly start/stop) 2418305ac5a7SPaul Cercueil * @firmware: name of firmware file to load, can be NULL 2419305ac5a7SPaul Cercueil * @len: length of private data needed by the rproc driver (in bytes) 2420305ac5a7SPaul Cercueil * 2421305ac5a7SPaul Cercueil * This function performs like rproc_alloc() but the acquired rproc device will 2422305ac5a7SPaul Cercueil * automatically be released on driver detach. 2423305ac5a7SPaul Cercueil * 2424305ac5a7SPaul Cercueil * Returns: new rproc instance, or NULL on failure 2425305ac5a7SPaul Cercueil */ 2426305ac5a7SPaul Cercueil struct rproc *devm_rproc_alloc(struct device *dev, const char *name, 2427305ac5a7SPaul Cercueil const struct rproc_ops *ops, 2428305ac5a7SPaul Cercueil const char *firmware, int len) 2429305ac5a7SPaul Cercueil { 2430305ac5a7SPaul Cercueil struct rproc **ptr, *rproc; 2431305ac5a7SPaul Cercueil 2432305ac5a7SPaul Cercueil ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL); 2433305ac5a7SPaul Cercueil if (!ptr) 24347dcef398SDan Carpenter return NULL; 2435305ac5a7SPaul Cercueil 2436305ac5a7SPaul Cercueil rproc = rproc_alloc(dev, name, ops, firmware, len); 2437305ac5a7SPaul Cercueil if (rproc) { 2438305ac5a7SPaul Cercueil *ptr = rproc; 2439305ac5a7SPaul Cercueil devres_add(dev, ptr); 2440305ac5a7SPaul Cercueil } else { 2441305ac5a7SPaul Cercueil devres_free(ptr); 2442305ac5a7SPaul Cercueil } 2443305ac5a7SPaul Cercueil 2444305ac5a7SPaul Cercueil return rproc; 2445305ac5a7SPaul Cercueil } 2446305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_alloc); 2447305ac5a7SPaul Cercueil 24488afd519cSFernando Guzman Lugo /** 24497bdc9650SBjorn Andersson * rproc_add_subdev() - add a subdevice to a remoteproc 24507bdc9650SBjorn Andersson * @rproc: rproc handle to add the subdevice to 24517bdc9650SBjorn Andersson * @subdev: subdev handle to register 24524902676fSBjorn Andersson * 24534902676fSBjorn Andersson * Caller is responsible for populating optional subdevice function pointers. 24547bdc9650SBjorn Andersson */ 24554902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 24567bdc9650SBjorn Andersson { 24577bdc9650SBjorn Andersson list_add_tail(&subdev->node, &rproc->subdevs); 24587bdc9650SBjorn Andersson } 24597bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev); 24607bdc9650SBjorn Andersson 24617bdc9650SBjorn Andersson /** 24627bdc9650SBjorn Andersson * rproc_remove_subdev() - remove a subdevice from a remoteproc 24637bdc9650SBjorn Andersson * @rproc: rproc handle to remove the subdevice from 24647bdc9650SBjorn Andersson * @subdev: subdev handle, previously registered with rproc_add_subdev() 24657bdc9650SBjorn Andersson */ 24667bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 24677bdc9650SBjorn Andersson { 24687bdc9650SBjorn Andersson list_del(&subdev->node); 24697bdc9650SBjorn Andersson } 24707bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev); 24717bdc9650SBjorn Andersson 24727bdc9650SBjorn Andersson /** 24737c89717fSBjorn Andersson * rproc_get_by_child() - acquire rproc handle of @dev's ancestor 24747c89717fSBjorn Andersson * @dev: child device to find ancestor of 24757c89717fSBjorn Andersson * 24767c89717fSBjorn Andersson * Returns the ancestor rproc instance, or NULL if not found. 24777c89717fSBjorn Andersson */ 24787c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev) 24797c89717fSBjorn Andersson { 24807c89717fSBjorn Andersson for (dev = dev->parent; dev; dev = dev->parent) { 24817c89717fSBjorn Andersson if (dev->type == &rproc_type) 24827c89717fSBjorn Andersson return dev->driver_data; 24837c89717fSBjorn Andersson } 24847c89717fSBjorn Andersson 24857c89717fSBjorn Andersson return NULL; 24867c89717fSBjorn Andersson } 24877c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child); 24887c89717fSBjorn Andersson 24897c89717fSBjorn Andersson /** 24908afd519cSFernando Guzman Lugo * rproc_report_crash() - rproc crash reporter function 24918afd519cSFernando Guzman Lugo * @rproc: remote processor 24928afd519cSFernando Guzman Lugo * @type: crash type 24938afd519cSFernando Guzman Lugo * 24948afd519cSFernando Guzman Lugo * This function must be called every time a crash is detected by the low-level 24958afd519cSFernando Guzman Lugo * drivers implementing a specific remoteproc. This should not be called from a 24968afd519cSFernando Guzman Lugo * non-remoteproc driver. 24978afd519cSFernando Guzman Lugo * 24988afd519cSFernando Guzman Lugo * This function can be called from atomic/interrupt context. 24998afd519cSFernando Guzman Lugo */ 25008afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) 25018afd519cSFernando Guzman Lugo { 25028afd519cSFernando Guzman Lugo if (!rproc) { 25038afd519cSFernando Guzman Lugo pr_err("NULL rproc pointer\n"); 25048afd519cSFernando Guzman Lugo return; 25058afd519cSFernando Guzman Lugo } 25068afd519cSFernando Guzman Lugo 2507a781e5aaSRishabh Bhatnagar /* Prevent suspend while the remoteproc is being recovered */ 2508a781e5aaSRishabh Bhatnagar pm_stay_awake(rproc->dev.parent); 2509a781e5aaSRishabh Bhatnagar 25108afd519cSFernando Guzman Lugo dev_err(&rproc->dev, "crash detected in %s: type %s\n", 25118afd519cSFernando Guzman Lugo rproc->name, rproc_crash_to_string(type)); 25128afd519cSFernando Guzman Lugo 25138afd519cSFernando Guzman Lugo /* create a new task to handle the error */ 25148afd519cSFernando Guzman Lugo schedule_work(&rproc->crash_handler); 25158afd519cSFernando Guzman Lugo } 25168afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash); 25178afd519cSFernando Guzman Lugo 2518dc5192c4SBjorn Andersson static int rproc_panic_handler(struct notifier_block *nb, unsigned long event, 2519dc5192c4SBjorn Andersson void *ptr) 2520dc5192c4SBjorn Andersson { 2521dc5192c4SBjorn Andersson unsigned int longest = 0; 2522dc5192c4SBjorn Andersson struct rproc *rproc; 2523dc5192c4SBjorn Andersson unsigned int d; 2524dc5192c4SBjorn Andersson 2525dc5192c4SBjorn Andersson rcu_read_lock(); 2526dc5192c4SBjorn Andersson list_for_each_entry_rcu(rproc, &rproc_list, node) { 2527dc5192c4SBjorn Andersson if (!rproc->ops->panic || rproc->state != RPROC_RUNNING) 2528dc5192c4SBjorn Andersson continue; 2529dc5192c4SBjorn Andersson 2530dc5192c4SBjorn Andersson d = rproc->ops->panic(rproc); 2531dc5192c4SBjorn Andersson longest = max(longest, d); 2532dc5192c4SBjorn Andersson } 2533dc5192c4SBjorn Andersson rcu_read_unlock(); 2534dc5192c4SBjorn Andersson 2535dc5192c4SBjorn Andersson /* 2536dc5192c4SBjorn Andersson * Delay for the longest requested duration before returning. This can 2537dc5192c4SBjorn Andersson * be used by the remoteproc drivers to give the remote processor time 2538dc5192c4SBjorn Andersson * to perform any requested operations (such as flush caches), when 2539dc5192c4SBjorn Andersson * it's not possible to signal the Linux side due to the panic. 2540dc5192c4SBjorn Andersson */ 2541dc5192c4SBjorn Andersson mdelay(longest); 2542dc5192c4SBjorn Andersson 2543dc5192c4SBjorn Andersson return NOTIFY_DONE; 2544dc5192c4SBjorn Andersson } 2545dc5192c4SBjorn Andersson 2546dc5192c4SBjorn Andersson static void __init rproc_init_panic(void) 2547dc5192c4SBjorn Andersson { 2548dc5192c4SBjorn Andersson rproc_panic_nb.notifier_call = rproc_panic_handler; 2549dc5192c4SBjorn Andersson atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb); 2550dc5192c4SBjorn Andersson } 2551dc5192c4SBjorn Andersson 2552dc5192c4SBjorn Andersson static void __exit rproc_exit_panic(void) 2553dc5192c4SBjorn Andersson { 2554dc5192c4SBjorn Andersson atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb); 2555dc5192c4SBjorn Andersson } 2556dc5192c4SBjorn Andersson 2557400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void) 2558400e64dfSOhad Ben-Cohen { 25592aefbef0SMatt Redfearn rproc_init_sysfs(); 2560400e64dfSOhad Ben-Cohen rproc_init_debugfs(); 2561dc5192c4SBjorn Andersson rproc_init_panic(); 2562b5ab5e24SOhad Ben-Cohen 2563400e64dfSOhad Ben-Cohen return 0; 2564400e64dfSOhad Ben-Cohen } 2565a8f40111SBrandon Maier subsys_initcall(remoteproc_init); 2566400e64dfSOhad Ben-Cohen 2567400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void) 2568400e64dfSOhad Ben-Cohen { 2569f42f79afSSuman Anna ida_destroy(&rproc_dev_index); 2570f42f79afSSuman Anna 2571dc5192c4SBjorn Andersson rproc_exit_panic(); 2572400e64dfSOhad Ben-Cohen rproc_exit_debugfs(); 25732aefbef0SMatt Redfearn rproc_exit_sysfs(); 2574400e64dfSOhad Ben-Cohen } 2575400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit); 2576400e64dfSOhad Ben-Cohen 2577400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2"); 2578400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework"); 2579