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 19400e64dfSOhad Ben-Cohen #include <linux/kernel.h> 20400e64dfSOhad Ben-Cohen #include <linux/module.h> 21400e64dfSOhad Ben-Cohen #include <linux/device.h> 22400e64dfSOhad Ben-Cohen #include <linux/slab.h> 23400e64dfSOhad Ben-Cohen #include <linux/mutex.h> 24400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h> 25400e64dfSOhad Ben-Cohen #include <linux/firmware.h> 26400e64dfSOhad Ben-Cohen #include <linux/string.h> 27400e64dfSOhad Ben-Cohen #include <linux/debugfs.h> 282666ca91SSarangdhar Joshi #include <linux/devcoredump.h> 29400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h> 30400e64dfSOhad Ben-Cohen #include <linux/iommu.h> 31b5ab5e24SOhad Ben-Cohen #include <linux/idr.h> 32400e64dfSOhad Ben-Cohen #include <linux/elf.h> 33a2b950acSOhad Ben-Cohen #include <linux/crc32.h> 34086d0872SLoic Pallardy #include <linux/of_reserved_mem.h> 35400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h> 36400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h> 37cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h> 38086d0872SLoic Pallardy #include <linux/platform_device.h> 39400e64dfSOhad Ben-Cohen 40400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h" 41400e64dfSOhad Ben-Cohen 42b36de8cfSLoic Pallardy #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL 43b36de8cfSLoic Pallardy 44fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex); 45fec47d86SDave Gerlach static LIST_HEAD(rproc_list); 46fec47d86SDave Gerlach 47a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc, 48a2b950acSOhad Ben-Cohen void *, int offset, int avail); 49400e64dfSOhad Ben-Cohen 50c6aed238SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc, 51c6aed238SLoic Pallardy struct rproc_mem_entry *mem); 52c6aed238SLoic Pallardy static int rproc_release_carveout(struct rproc *rproc, 53c6aed238SLoic Pallardy struct rproc_mem_entry *mem); 54c6aed238SLoic Pallardy 55b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */ 56b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index); 57b5ab5e24SOhad Ben-Cohen 588afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = { 598afd519cSFernando Guzman Lugo [RPROC_MMUFAULT] = "mmufault", 60b3d39032SBjorn Andersson [RPROC_WATCHDOG] = "watchdog", 61b3d39032SBjorn Andersson [RPROC_FATAL_ERROR] = "fatal error", 628afd519cSFernando Guzman Lugo }; 638afd519cSFernando Guzman Lugo 648afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */ 658afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type) 668afd519cSFernando Guzman Lugo { 678afd519cSFernando Guzman Lugo if (type < ARRAY_SIZE(rproc_crash_names)) 688afd519cSFernando Guzman Lugo return rproc_crash_names[type]; 69b23f7a09SMasanari Iida return "unknown"; 708afd519cSFernando Guzman Lugo } 718afd519cSFernando Guzman Lugo 72400e64dfSOhad Ben-Cohen /* 73400e64dfSOhad Ben-Cohen * This is the IOMMU fault handler we register with the IOMMU API 74400e64dfSOhad Ben-Cohen * (when relevant; not all remote processors access memory through 75400e64dfSOhad Ben-Cohen * an IOMMU). 76400e64dfSOhad Ben-Cohen * 77400e64dfSOhad Ben-Cohen * IOMMU core will invoke this handler whenever the remote processor 78400e64dfSOhad Ben-Cohen * will try to access an unmapped device address. 79400e64dfSOhad Ben-Cohen */ 80400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, 8177ca2332SOhad Ben-Cohen unsigned long iova, int flags, void *token) 82400e64dfSOhad Ben-Cohen { 838afd519cSFernando Guzman Lugo struct rproc *rproc = token; 848afd519cSFernando Guzman Lugo 85400e64dfSOhad Ben-Cohen dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags); 86400e64dfSOhad Ben-Cohen 878afd519cSFernando Guzman Lugo rproc_report_crash(rproc, RPROC_MMUFAULT); 888afd519cSFernando Guzman Lugo 89400e64dfSOhad Ben-Cohen /* 90400e64dfSOhad Ben-Cohen * Let the iommu core know we're not really handling this fault; 918afd519cSFernando Guzman Lugo * we just used it as a recovery trigger. 92400e64dfSOhad Ben-Cohen */ 93400e64dfSOhad Ben-Cohen return -ENOSYS; 94400e64dfSOhad Ben-Cohen } 95400e64dfSOhad Ben-Cohen 96400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc) 97400e64dfSOhad Ben-Cohen { 98400e64dfSOhad Ben-Cohen struct iommu_domain *domain; 99b5ab5e24SOhad Ben-Cohen struct device *dev = rproc->dev.parent; 100400e64dfSOhad Ben-Cohen int ret; 101400e64dfSOhad Ben-Cohen 102315491e5SSuman Anna if (!rproc->has_iommu) { 103315491e5SSuman Anna dev_dbg(dev, "iommu not present\n"); 1040798e1daSMark Grosen return 0; 105400e64dfSOhad Ben-Cohen } 106400e64dfSOhad Ben-Cohen 107400e64dfSOhad Ben-Cohen domain = iommu_domain_alloc(dev->bus); 108400e64dfSOhad Ben-Cohen if (!domain) { 109400e64dfSOhad Ben-Cohen dev_err(dev, "can't alloc iommu domain\n"); 110400e64dfSOhad Ben-Cohen return -ENOMEM; 111400e64dfSOhad Ben-Cohen } 112400e64dfSOhad Ben-Cohen 11377ca2332SOhad Ben-Cohen iommu_set_fault_handler(domain, rproc_iommu_fault, rproc); 114400e64dfSOhad Ben-Cohen 115400e64dfSOhad Ben-Cohen ret = iommu_attach_device(domain, dev); 116400e64dfSOhad Ben-Cohen if (ret) { 117400e64dfSOhad Ben-Cohen dev_err(dev, "can't attach iommu device: %d\n", ret); 118400e64dfSOhad Ben-Cohen goto free_domain; 119400e64dfSOhad Ben-Cohen } 120400e64dfSOhad Ben-Cohen 121400e64dfSOhad Ben-Cohen rproc->domain = domain; 122400e64dfSOhad Ben-Cohen 123400e64dfSOhad Ben-Cohen return 0; 124400e64dfSOhad Ben-Cohen 125400e64dfSOhad Ben-Cohen free_domain: 126400e64dfSOhad Ben-Cohen iommu_domain_free(domain); 127400e64dfSOhad Ben-Cohen return ret; 128400e64dfSOhad Ben-Cohen } 129400e64dfSOhad Ben-Cohen 130400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc) 131400e64dfSOhad Ben-Cohen { 132400e64dfSOhad Ben-Cohen struct iommu_domain *domain = rproc->domain; 133b5ab5e24SOhad Ben-Cohen struct device *dev = rproc->dev.parent; 134400e64dfSOhad Ben-Cohen 135400e64dfSOhad Ben-Cohen if (!domain) 136400e64dfSOhad Ben-Cohen return; 137400e64dfSOhad Ben-Cohen 138400e64dfSOhad Ben-Cohen iommu_detach_device(domain, dev); 139400e64dfSOhad Ben-Cohen iommu_domain_free(domain); 140400e64dfSOhad Ben-Cohen } 141400e64dfSOhad Ben-Cohen 142086d0872SLoic Pallardy phys_addr_t rproc_va_to_pa(void *cpu_addr) 143eb30596eSLoic Pallardy { 144eb30596eSLoic Pallardy /* 145eb30596eSLoic Pallardy * Return physical address according to virtual address location 146eb30596eSLoic Pallardy * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent 147eb30596eSLoic Pallardy * - in kernel: if region allocated in generic dma memory pool 148eb30596eSLoic Pallardy */ 149eb30596eSLoic Pallardy if (is_vmalloc_addr(cpu_addr)) { 150eb30596eSLoic Pallardy return page_to_phys(vmalloc_to_page(cpu_addr)) + 151eb30596eSLoic Pallardy offset_in_page(cpu_addr); 152eb30596eSLoic Pallardy } 153eb30596eSLoic Pallardy 154eb30596eSLoic Pallardy WARN_ON(!virt_addr_valid(cpu_addr)); 155eb30596eSLoic Pallardy return virt_to_phys(cpu_addr); 156eb30596eSLoic Pallardy } 157086d0872SLoic Pallardy EXPORT_SYMBOL(rproc_va_to_pa); 158eb30596eSLoic Pallardy 159a01f7cd6SSuman Anna /** 160a01f7cd6SSuman Anna * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address 161a01f7cd6SSuman Anna * @rproc: handle of a remote processor 162a01f7cd6SSuman Anna * @da: remoteproc device address to translate 163a01f7cd6SSuman Anna * @len: length of the memory region @da is pointing to 164a01f7cd6SSuman Anna * 165400e64dfSOhad Ben-Cohen * Some remote processors will ask us to allocate them physically contiguous 166400e64dfSOhad Ben-Cohen * memory regions (which we call "carveouts"), and map them to specific 167a01f7cd6SSuman Anna * device addresses (which are hardcoded in the firmware). They may also have 168a01f7cd6SSuman Anna * dedicated memory regions internal to the processors, and use them either 169a01f7cd6SSuman Anna * exclusively or alongside carveouts. 170400e64dfSOhad Ben-Cohen * 171400e64dfSOhad Ben-Cohen * They may then ask us to copy objects into specific device addresses (e.g. 172400e64dfSOhad Ben-Cohen * code/data sections) or expose us certain symbols in other device address 173400e64dfSOhad Ben-Cohen * (e.g. their trace buffer). 174400e64dfSOhad Ben-Cohen * 175a01f7cd6SSuman Anna * This function is a helper function with which we can go over the allocated 176a01f7cd6SSuman Anna * carveouts and translate specific device addresses to kernel virtual addresses 177a01f7cd6SSuman Anna * so we can access the referenced memory. This function also allows to perform 178a01f7cd6SSuman Anna * translations on the internal remoteproc memory regions through a platform 179a01f7cd6SSuman Anna * implementation specific da_to_va ops, if present. 180a01f7cd6SSuman Anna * 181a01f7cd6SSuman Anna * The function returns a valid kernel address on success or NULL on failure. 182400e64dfSOhad Ben-Cohen * 183400e64dfSOhad Ben-Cohen * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, 184400e64dfSOhad Ben-Cohen * but only on kernel direct mapped RAM memory. Instead, we're just using 185a01f7cd6SSuman Anna * here the output of the DMA API for the carveouts, which should be more 186a01f7cd6SSuman Anna * correct. 187400e64dfSOhad Ben-Cohen */ 18872854fb0SSjur Brændeland void *rproc_da_to_va(struct rproc *rproc, u64 da, int len) 189400e64dfSOhad Ben-Cohen { 190400e64dfSOhad Ben-Cohen struct rproc_mem_entry *carveout; 191400e64dfSOhad Ben-Cohen void *ptr = NULL; 192400e64dfSOhad Ben-Cohen 193a01f7cd6SSuman Anna if (rproc->ops->da_to_va) { 194a01f7cd6SSuman Anna ptr = rproc->ops->da_to_va(rproc, da, len); 195a01f7cd6SSuman Anna if (ptr) 196a01f7cd6SSuman Anna goto out; 197a01f7cd6SSuman Anna } 198a01f7cd6SSuman Anna 199400e64dfSOhad Ben-Cohen list_for_each_entry(carveout, &rproc->carveouts, node) { 200400e64dfSOhad Ben-Cohen int offset = da - carveout->da; 201400e64dfSOhad Ben-Cohen 20274457c40SLoic Pallardy /* Verify that carveout is allocated */ 20374457c40SLoic Pallardy if (!carveout->va) 20474457c40SLoic Pallardy continue; 20574457c40SLoic Pallardy 206400e64dfSOhad Ben-Cohen /* try next carveout if da is too small */ 207400e64dfSOhad Ben-Cohen if (offset < 0) 208400e64dfSOhad Ben-Cohen continue; 209400e64dfSOhad Ben-Cohen 210400e64dfSOhad Ben-Cohen /* try next carveout if da is too large */ 211400e64dfSOhad Ben-Cohen if (offset + len > carveout->len) 212400e64dfSOhad Ben-Cohen continue; 213400e64dfSOhad Ben-Cohen 214400e64dfSOhad Ben-Cohen ptr = carveout->va + offset; 215400e64dfSOhad Ben-Cohen 216400e64dfSOhad Ben-Cohen break; 217400e64dfSOhad Ben-Cohen } 218400e64dfSOhad Ben-Cohen 219a01f7cd6SSuman Anna out: 220400e64dfSOhad Ben-Cohen return ptr; 221400e64dfSOhad Ben-Cohen } 2224afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va); 223400e64dfSOhad Ben-Cohen 224b0019ccdSLoic Pallardy /** 225b0019ccdSLoic Pallardy * rproc_find_carveout_by_name() - lookup the carveout region by a name 226b0019ccdSLoic Pallardy * @rproc: handle of a remote processor 227b0019ccdSLoic Pallardy * @name,..: carveout name to find (standard printf format) 228b0019ccdSLoic Pallardy * 229b0019ccdSLoic Pallardy * Platform driver has the capability to register some pre-allacoted carveout 230b0019ccdSLoic Pallardy * (physically contiguous memory regions) before rproc firmware loading and 231b0019ccdSLoic Pallardy * associated resource table analysis. These regions may be dedicated memory 232b0019ccdSLoic Pallardy * regions internal to the coprocessor or specified DDR region with specific 233b0019ccdSLoic Pallardy * attributes 234b0019ccdSLoic Pallardy * 235b0019ccdSLoic Pallardy * This function is a helper function with which we can go over the 236b0019ccdSLoic Pallardy * allocated carveouts and return associated region characteristics like 237b0019ccdSLoic Pallardy * coprocessor address, length or processor virtual address. 238b0019ccdSLoic Pallardy * 239b0019ccdSLoic Pallardy * Return: a valid pointer on carveout entry on success or NULL on failure. 240b0019ccdSLoic Pallardy */ 241b0019ccdSLoic Pallardy struct rproc_mem_entry * 242b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...) 243b0019ccdSLoic Pallardy { 244b0019ccdSLoic Pallardy va_list args; 245b0019ccdSLoic Pallardy char _name[32]; 246b0019ccdSLoic Pallardy struct rproc_mem_entry *carveout, *mem = NULL; 247b0019ccdSLoic Pallardy 248b0019ccdSLoic Pallardy if (!name) 249b0019ccdSLoic Pallardy return NULL; 250b0019ccdSLoic Pallardy 251b0019ccdSLoic Pallardy va_start(args, name); 252b0019ccdSLoic Pallardy vsnprintf(_name, sizeof(_name), name, args); 253b0019ccdSLoic Pallardy va_end(args); 254b0019ccdSLoic Pallardy 255b0019ccdSLoic Pallardy list_for_each_entry(carveout, &rproc->carveouts, node) { 256b0019ccdSLoic Pallardy /* Compare carveout and requested names */ 257b0019ccdSLoic Pallardy if (!strcmp(carveout->name, _name)) { 258b0019ccdSLoic Pallardy mem = carveout; 259b0019ccdSLoic Pallardy break; 260b0019ccdSLoic Pallardy } 261b0019ccdSLoic Pallardy } 262b0019ccdSLoic Pallardy 263b0019ccdSLoic Pallardy return mem; 264b0019ccdSLoic Pallardy } 265b0019ccdSLoic Pallardy 266c874bf59SLoic Pallardy /** 267c874bf59SLoic Pallardy * rproc_check_carveout_da() - Check specified carveout da configuration 268c874bf59SLoic Pallardy * @rproc: handle of a remote processor 269c874bf59SLoic Pallardy * @mem: pointer on carveout to check 270c874bf59SLoic Pallardy * @da: area device address 271c874bf59SLoic Pallardy * @len: associated area size 272c874bf59SLoic Pallardy * 273c874bf59SLoic Pallardy * This function is a helper function to verify requested device area (couple 27428d7d5c6SLoic Pallardy * da, len) is part of specified carveout. 27528d7d5c6SLoic Pallardy * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is 27628d7d5c6SLoic Pallardy * checked. 277c874bf59SLoic Pallardy * 27828d7d5c6SLoic Pallardy * Return: 0 if carveout matches request else error 279c874bf59SLoic Pallardy */ 28028d7d5c6SLoic Pallardy static int rproc_check_carveout_da(struct rproc *rproc, 28128d7d5c6SLoic Pallardy struct rproc_mem_entry *mem, u32 da, u32 len) 282c874bf59SLoic Pallardy { 283c874bf59SLoic Pallardy struct device *dev = &rproc->dev; 28428d7d5c6SLoic Pallardy int delta; 285c874bf59SLoic Pallardy 286c874bf59SLoic Pallardy /* Check requested resource length */ 287c874bf59SLoic Pallardy if (len > mem->len) { 288c874bf59SLoic Pallardy dev_err(dev, "Registered carveout doesn't fit len request\n"); 28928d7d5c6SLoic Pallardy return -EINVAL; 290c874bf59SLoic Pallardy } 291c874bf59SLoic Pallardy 292c874bf59SLoic Pallardy if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) { 29328d7d5c6SLoic Pallardy /* Address doesn't match registered carveout configuration */ 29428d7d5c6SLoic Pallardy return -EINVAL; 295c874bf59SLoic Pallardy } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) { 296c874bf59SLoic Pallardy delta = da - mem->da; 297c874bf59SLoic Pallardy 298c874bf59SLoic Pallardy /* Check requested resource belongs to registered carveout */ 299c874bf59SLoic Pallardy if (delta < 0) { 300c874bf59SLoic Pallardy dev_err(dev, 301c874bf59SLoic Pallardy "Registered carveout doesn't fit da request\n"); 30228d7d5c6SLoic Pallardy return -EINVAL; 303c874bf59SLoic Pallardy } 304c874bf59SLoic Pallardy 305c874bf59SLoic Pallardy if (delta + len > mem->len) { 306c874bf59SLoic Pallardy dev_err(dev, 307c874bf59SLoic Pallardy "Registered carveout doesn't fit len request\n"); 30828d7d5c6SLoic Pallardy return -EINVAL; 309c874bf59SLoic Pallardy } 310c874bf59SLoic Pallardy } 311c874bf59SLoic Pallardy 312c874bf59SLoic Pallardy return 0; 313c874bf59SLoic Pallardy } 314c874bf59SLoic Pallardy 3156db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) 316400e64dfSOhad Ben-Cohen { 3177a186941SOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 318b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 3196db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 320c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 3217a186941SOhad Ben-Cohen int ret, size, notifyid; 322c6aed238SLoic Pallardy struct rproc_mem_entry *mem; 323400e64dfSOhad Ben-Cohen 3246db20ea8SOhad Ben-Cohen /* actual size of vring (in bytes) */ 3256db20ea8SOhad Ben-Cohen size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); 3266db20ea8SOhad Ben-Cohen 327c6aed238SLoic Pallardy rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; 328c6aed238SLoic Pallardy 329c6aed238SLoic Pallardy /* Search for pre-registered carveout */ 330c6aed238SLoic Pallardy mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index, 331c6aed238SLoic Pallardy i); 332c6aed238SLoic Pallardy if (mem) { 333c6aed238SLoic Pallardy if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size)) 334c6aed238SLoic Pallardy return -ENOMEM; 335c6aed238SLoic Pallardy } else { 336c6aed238SLoic Pallardy /* Register carveout in in list */ 33799cf0361SBen Dooks (Codethink) mem = rproc_mem_entry_init(dev, NULL, 0, 33899cf0361SBen Dooks (Codethink) size, rsc->vring[i].da, 339c6aed238SLoic Pallardy rproc_alloc_carveout, 340c6aed238SLoic Pallardy rproc_release_carveout, 341c6aed238SLoic Pallardy "vdev%dvring%d", 342c6aed238SLoic Pallardy rvdev->index, i); 343c6aed238SLoic Pallardy if (!mem) { 344c6aed238SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 345c6aed238SLoic Pallardy return -ENOMEM; 346c6aed238SLoic Pallardy } 347c6aed238SLoic Pallardy 348c6aed238SLoic Pallardy rproc_add_carveout(rproc, mem); 3496db20ea8SOhad Ben-Cohen } 3506db20ea8SOhad Ben-Cohen 3516db20ea8SOhad Ben-Cohen /* 3526db20ea8SOhad Ben-Cohen * Assign an rproc-wide unique index for this vring 3536db20ea8SOhad Ben-Cohen * TODO: assign a notifyid for rvdev updates as well 3546db20ea8SOhad Ben-Cohen * TODO: support predefined notifyids (via resource table) 3556db20ea8SOhad Ben-Cohen */ 35615fc6110STejun Heo ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); 357b39599b7SSuman Anna if (ret < 0) { 35815fc6110STejun Heo dev_err(dev, "idr_alloc failed: %d\n", ret); 3596db20ea8SOhad Ben-Cohen return ret; 3606db20ea8SOhad Ben-Cohen } 36115fc6110STejun Heo notifyid = ret; 3626db20ea8SOhad Ben-Cohen 36348f18f89SBjorn Andersson /* Potentially bump max_notifyid */ 36448f18f89SBjorn Andersson if (notifyid > rproc->max_notifyid) 36548f18f89SBjorn Andersson rproc->max_notifyid = notifyid; 36648f18f89SBjorn Andersson 3676db20ea8SOhad Ben-Cohen rvring->notifyid = notifyid; 3686db20ea8SOhad Ben-Cohen 369c6aed238SLoic Pallardy /* Let the rproc know the notifyid of this vring.*/ 370c0d63157SSjur Brændeland rsc->vring[i].notifyid = notifyid; 3716db20ea8SOhad Ben-Cohen return 0; 3726db20ea8SOhad Ben-Cohen } 3736db20ea8SOhad Ben-Cohen 374400e64dfSOhad Ben-Cohen static int 3756db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) 376400e64dfSOhad Ben-Cohen { 377400e64dfSOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 378b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 379400e64dfSOhad Ben-Cohen struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; 3806db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 381400e64dfSOhad Ben-Cohen 3829d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n", 3837a186941SOhad Ben-Cohen i, vring->da, vring->num, vring->align); 3847a186941SOhad Ben-Cohen 38563140e0eSOhad Ben-Cohen /* verify queue size and vring alignment are sane */ 38663140e0eSOhad Ben-Cohen if (!vring->num || !vring->align) { 38763140e0eSOhad Ben-Cohen dev_err(dev, "invalid qsz (%d) or alignment (%d)\n", 38863140e0eSOhad Ben-Cohen vring->num, vring->align); 389400e64dfSOhad Ben-Cohen return -EINVAL; 390400e64dfSOhad Ben-Cohen } 391400e64dfSOhad Ben-Cohen 3926db20ea8SOhad Ben-Cohen rvring->len = vring->num; 3936db20ea8SOhad Ben-Cohen rvring->align = vring->align; 3946db20ea8SOhad Ben-Cohen rvring->rvdev = rvdev; 395400e64dfSOhad Ben-Cohen 396400e64dfSOhad Ben-Cohen return 0; 397400e64dfSOhad Ben-Cohen } 398400e64dfSOhad Ben-Cohen 3996db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring) 4007a186941SOhad Ben-Cohen { 4016db20ea8SOhad Ben-Cohen struct rproc *rproc = rvring->rvdev->rproc; 402*00a0eec5SClement Leger int idx = rvring - rvring->rvdev->vring; 403c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 4047a186941SOhad Ben-Cohen 4057a186941SOhad Ben-Cohen idr_remove(&rproc->notifyids, rvring->notifyid); 406099a3f33SSjur Brændeland 407c0d63157SSjur Brændeland /* reset resource entry info */ 408c0d63157SSjur Brændeland rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; 409c0d63157SSjur Brændeland rsc->vring[idx].da = 0; 410c0d63157SSjur Brændeland rsc->vring[idx].notifyid = -1; 4117a186941SOhad Ben-Cohen } 4127a186941SOhad Ben-Cohen 4136f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev) 414f5bcb353SBjorn Andersson { 415f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 416f5bcb353SBjorn Andersson 417f5bcb353SBjorn Andersson return rproc_add_virtio_dev(rvdev, rvdev->id); 418f5bcb353SBjorn Andersson } 419f5bcb353SBjorn Andersson 4206f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed) 421f5bcb353SBjorn Andersson { 422f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 423d4c036feSLoic Pallardy int ret; 424f5bcb353SBjorn Andersson 425d4c036feSLoic Pallardy ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev); 426d4c036feSLoic Pallardy if (ret) 427d4c036feSLoic Pallardy dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret); 428f5bcb353SBjorn Andersson } 429f5bcb353SBjorn Andersson 430400e64dfSOhad Ben-Cohen /** 431086d0872SLoic Pallardy * rproc_rvdev_release() - release the existence of a rvdev 432086d0872SLoic Pallardy * 433086d0872SLoic Pallardy * @dev: the subdevice's dev 434086d0872SLoic Pallardy */ 435086d0872SLoic Pallardy static void rproc_rvdev_release(struct device *dev) 436086d0872SLoic Pallardy { 437086d0872SLoic Pallardy struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev); 438086d0872SLoic Pallardy 439086d0872SLoic Pallardy of_reserved_mem_device_release(dev); 440086d0872SLoic Pallardy 441086d0872SLoic Pallardy kfree(rvdev); 442086d0872SLoic Pallardy } 443086d0872SLoic Pallardy 444086d0872SLoic Pallardy /** 445fd2c15ecSOhad Ben-Cohen * rproc_handle_vdev() - handle a vdev fw resource 446400e64dfSOhad Ben-Cohen * @rproc: the remote processor 447400e64dfSOhad Ben-Cohen * @rsc: the vring resource descriptor 448fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 449400e64dfSOhad Ben-Cohen * 4507a186941SOhad Ben-Cohen * This resource entry requests the host to statically register a virtio 4517a186941SOhad Ben-Cohen * device (vdev), and setup everything needed to support it. It contains 4527a186941SOhad Ben-Cohen * everything needed to make it possible: the virtio device id, virtio 4537a186941SOhad Ben-Cohen * device features, vrings information, virtio config space, etc... 454400e64dfSOhad Ben-Cohen * 4557a186941SOhad Ben-Cohen * Before registering the vdev, the vrings are allocated from non-cacheable 4567a186941SOhad Ben-Cohen * physically contiguous memory. Currently we only support two vrings per 4577a186941SOhad Ben-Cohen * remote processor (temporary limitation). We might also want to consider 4587a186941SOhad Ben-Cohen * doing the vring allocation only later when ->find_vqs() is invoked, and 4597a186941SOhad Ben-Cohen * then release them upon ->del_vqs(). 460400e64dfSOhad Ben-Cohen * 4617a186941SOhad Ben-Cohen * Note: @da is currently not really handled correctly: we dynamically 4627a186941SOhad Ben-Cohen * allocate it using the DMA API, ignoring requested hard coded addresses, 4637a186941SOhad Ben-Cohen * and we don't take care of any required IOMMU programming. This is all 4647a186941SOhad Ben-Cohen * going to be taken care of when the generic iommu-based DMA API will be 4657a186941SOhad Ben-Cohen * merged. Meanwhile, statically-addressed iommu-based firmware images should 4667a186941SOhad Ben-Cohen * use RSC_DEVMEM resource entries to map their required @da to the physical 4677a186941SOhad Ben-Cohen * address of their base CMA region (ouch, hacky!). 468400e64dfSOhad Ben-Cohen * 469400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 470400e64dfSOhad Ben-Cohen */ 471fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc, 472a2b950acSOhad Ben-Cohen int offset, int avail) 473400e64dfSOhad Ben-Cohen { 474b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 4757a186941SOhad Ben-Cohen struct rproc_vdev *rvdev; 4767a186941SOhad Ben-Cohen int i, ret; 477086d0872SLoic Pallardy char name[16]; 478fd2c15ecSOhad Ben-Cohen 479fd2c15ecSOhad Ben-Cohen /* make sure resource isn't truncated */ 480fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring) 481fd2c15ecSOhad Ben-Cohen + rsc->config_len > avail) { 482b5ab5e24SOhad Ben-Cohen dev_err(dev, "vdev rsc is truncated\n"); 483fd2c15ecSOhad Ben-Cohen return -EINVAL; 484fd2c15ecSOhad Ben-Cohen } 485fd2c15ecSOhad Ben-Cohen 486fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 487fd2c15ecSOhad Ben-Cohen if (rsc->reserved[0] || rsc->reserved[1]) { 488fd2c15ecSOhad Ben-Cohen dev_err(dev, "vdev rsc has non zero reserved bytes\n"); 489fd2c15ecSOhad Ben-Cohen return -EINVAL; 490fd2c15ecSOhad Ben-Cohen } 491fd2c15ecSOhad Ben-Cohen 4929d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n", 493fd2c15ecSOhad Ben-Cohen rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); 494400e64dfSOhad Ben-Cohen 4957a186941SOhad Ben-Cohen /* we currently support only two vrings per rvdev */ 4967a186941SOhad Ben-Cohen if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { 497fd2c15ecSOhad Ben-Cohen dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); 498400e64dfSOhad Ben-Cohen return -EINVAL; 499400e64dfSOhad Ben-Cohen } 500400e64dfSOhad Ben-Cohen 501899585adSAnna, Suman rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL); 5027a186941SOhad Ben-Cohen if (!rvdev) 5037a186941SOhad Ben-Cohen return -ENOMEM; 5047a186941SOhad Ben-Cohen 505aab8d802SBjorn Andersson kref_init(&rvdev->refcount); 506aab8d802SBjorn Andersson 507f5bcb353SBjorn Andersson rvdev->id = rsc->id; 5087a186941SOhad Ben-Cohen rvdev->rproc = rproc; 509c6aed238SLoic Pallardy rvdev->index = rproc->nb_vdev++; 5107a186941SOhad Ben-Cohen 511086d0872SLoic Pallardy /* Initialise vdev subdevice */ 512086d0872SLoic Pallardy snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index); 513086d0872SLoic Pallardy rvdev->dev.parent = rproc->dev.parent; 51472f64cabSClement Leger rvdev->dev.dma_pfn_offset = rproc->dev.parent->dma_pfn_offset; 515086d0872SLoic Pallardy rvdev->dev.release = rproc_rvdev_release; 516086d0872SLoic Pallardy dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name); 517086d0872SLoic Pallardy dev_set_drvdata(&rvdev->dev, rvdev); 518086d0872SLoic Pallardy 519086d0872SLoic Pallardy ret = device_register(&rvdev->dev); 520086d0872SLoic Pallardy if (ret) { 521086d0872SLoic Pallardy put_device(&rvdev->dev); 522086d0872SLoic Pallardy return ret; 523086d0872SLoic Pallardy } 524086d0872SLoic Pallardy /* Make device dma capable by inheriting from parent's capabilities */ 525086d0872SLoic Pallardy set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent)); 526086d0872SLoic Pallardy 527086d0872SLoic Pallardy ret = dma_coerce_mask_and_coherent(&rvdev->dev, 528086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent)); 529086d0872SLoic Pallardy if (ret) { 530086d0872SLoic Pallardy dev_warn(dev, 531086d0872SLoic Pallardy "Failed to set DMA mask %llx. Trying to continue... %x\n", 532086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent), ret); 533086d0872SLoic Pallardy } 534086d0872SLoic Pallardy 5356db20ea8SOhad Ben-Cohen /* parse the vrings */ 536fd2c15ecSOhad Ben-Cohen for (i = 0; i < rsc->num_of_vrings; i++) { 5376db20ea8SOhad Ben-Cohen ret = rproc_parse_vring(rvdev, rsc, i); 5387a186941SOhad Ben-Cohen if (ret) 5396db20ea8SOhad Ben-Cohen goto free_rvdev; 540fd2c15ecSOhad Ben-Cohen } 541fd2c15ecSOhad Ben-Cohen 542a2b950acSOhad Ben-Cohen /* remember the resource offset*/ 543a2b950acSOhad Ben-Cohen rvdev->rsc_offset = offset; 544400e64dfSOhad Ben-Cohen 545a863af5dSBjorn Andersson /* allocate the vring resources */ 546a863af5dSBjorn Andersson for (i = 0; i < rsc->num_of_vrings; i++) { 547a863af5dSBjorn Andersson ret = rproc_alloc_vring(rvdev, i); 548a863af5dSBjorn Andersson if (ret) 549a863af5dSBjorn Andersson goto unwind_vring_allocations; 550a863af5dSBjorn Andersson } 551a863af5dSBjorn Andersson 5527a186941SOhad Ben-Cohen list_add_tail(&rvdev->node, &rproc->rvdevs); 553400e64dfSOhad Ben-Cohen 5546f8b0373SAlex Elder rvdev->subdev.start = rproc_vdev_do_start; 5556f8b0373SAlex Elder rvdev->subdev.stop = rproc_vdev_do_stop; 5564902676fSBjorn Andersson 5574902676fSBjorn Andersson rproc_add_subdev(rproc, &rvdev->subdev); 558400e64dfSOhad Ben-Cohen 559400e64dfSOhad Ben-Cohen return 0; 5607a186941SOhad Ben-Cohen 561a863af5dSBjorn Andersson unwind_vring_allocations: 562a863af5dSBjorn Andersson for (i--; i >= 0; i--) 563a863af5dSBjorn Andersson rproc_free_vring(&rvdev->vring[i]); 5646db20ea8SOhad Ben-Cohen free_rvdev: 565086d0872SLoic Pallardy device_unregister(&rvdev->dev); 5667a186941SOhad Ben-Cohen return ret; 567400e64dfSOhad Ben-Cohen } 568400e64dfSOhad Ben-Cohen 569aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref) 570aab8d802SBjorn Andersson { 571aab8d802SBjorn Andersson struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount); 572a863af5dSBjorn Andersson struct rproc_vring *rvring; 573f5bcb353SBjorn Andersson struct rproc *rproc = rvdev->rproc; 574a863af5dSBjorn Andersson int id; 575a863af5dSBjorn Andersson 576a863af5dSBjorn Andersson for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) { 577a863af5dSBjorn Andersson rvring = &rvdev->vring[id]; 578a863af5dSBjorn Andersson rproc_free_vring(rvring); 579a863af5dSBjorn Andersson } 580aab8d802SBjorn Andersson 581f5bcb353SBjorn Andersson rproc_remove_subdev(rproc, &rvdev->subdev); 582aab8d802SBjorn Andersson list_del(&rvdev->node); 583086d0872SLoic Pallardy device_unregister(&rvdev->dev); 584aab8d802SBjorn Andersson } 585aab8d802SBjorn Andersson 586400e64dfSOhad Ben-Cohen /** 587400e64dfSOhad Ben-Cohen * rproc_handle_trace() - handle a shared trace buffer resource 588400e64dfSOhad Ben-Cohen * @rproc: the remote processor 589400e64dfSOhad Ben-Cohen * @rsc: the trace resource descriptor 590fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 591400e64dfSOhad Ben-Cohen * 592400e64dfSOhad Ben-Cohen * In case the remote processor dumps trace logs into memory, 593400e64dfSOhad Ben-Cohen * export it via debugfs. 594400e64dfSOhad Ben-Cohen * 595400e64dfSOhad Ben-Cohen * Currently, the 'da' member of @rsc should contain the device address 596400e64dfSOhad Ben-Cohen * where the remote processor is dumping the traces. Later we could also 597400e64dfSOhad Ben-Cohen * support dynamically allocating this address using the generic 598400e64dfSOhad Ben-Cohen * DMA API (but currently there isn't a use case for that). 599400e64dfSOhad Ben-Cohen * 600400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 601400e64dfSOhad Ben-Cohen */ 602fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc, 603a2b950acSOhad Ben-Cohen int offset, int avail) 604400e64dfSOhad Ben-Cohen { 605a987e6b9SLoic Pallardy struct rproc_debug_trace *trace; 606b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 607400e64dfSOhad Ben-Cohen char name[15]; 608400e64dfSOhad Ben-Cohen 609fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 610b5ab5e24SOhad Ben-Cohen dev_err(dev, "trace rsc is truncated\n"); 611fd2c15ecSOhad Ben-Cohen return -EINVAL; 612fd2c15ecSOhad Ben-Cohen } 613fd2c15ecSOhad Ben-Cohen 614fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 615fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 616fd2c15ecSOhad Ben-Cohen dev_err(dev, "trace rsc has non zero reserved bytes\n"); 617fd2c15ecSOhad Ben-Cohen return -EINVAL; 618fd2c15ecSOhad Ben-Cohen } 619fd2c15ecSOhad Ben-Cohen 620400e64dfSOhad Ben-Cohen trace = kzalloc(sizeof(*trace), GFP_KERNEL); 621172e6ab1SSuman Anna if (!trace) 622400e64dfSOhad Ben-Cohen return -ENOMEM; 623400e64dfSOhad Ben-Cohen 624400e64dfSOhad Ben-Cohen /* set the trace buffer dma properties */ 625a987e6b9SLoic Pallardy trace->trace_mem.len = rsc->len; 626a987e6b9SLoic Pallardy trace->trace_mem.da = rsc->da; 627a987e6b9SLoic Pallardy 628a987e6b9SLoic Pallardy /* set pointer on rproc device */ 629a987e6b9SLoic Pallardy trace->rproc = rproc; 630400e64dfSOhad Ben-Cohen 631400e64dfSOhad Ben-Cohen /* make sure snprintf always null terminates, even if truncating */ 632400e64dfSOhad Ben-Cohen snprintf(name, sizeof(name), "trace%d", rproc->num_traces); 633400e64dfSOhad Ben-Cohen 634400e64dfSOhad Ben-Cohen /* create the debugfs entry */ 635a987e6b9SLoic Pallardy trace->tfile = rproc_create_trace_file(name, rproc, trace); 636a987e6b9SLoic Pallardy if (!trace->tfile) { 637400e64dfSOhad Ben-Cohen kfree(trace); 638400e64dfSOhad Ben-Cohen return -EINVAL; 639400e64dfSOhad Ben-Cohen } 640400e64dfSOhad Ben-Cohen 641400e64dfSOhad Ben-Cohen list_add_tail(&trace->node, &rproc->traces); 642400e64dfSOhad Ben-Cohen 643400e64dfSOhad Ben-Cohen rproc->num_traces++; 644400e64dfSOhad Ben-Cohen 645a987e6b9SLoic Pallardy dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n", 646a987e6b9SLoic Pallardy name, rsc->da, rsc->len); 647400e64dfSOhad Ben-Cohen 648400e64dfSOhad Ben-Cohen return 0; 649400e64dfSOhad Ben-Cohen } 650400e64dfSOhad Ben-Cohen 651400e64dfSOhad Ben-Cohen /** 652400e64dfSOhad Ben-Cohen * rproc_handle_devmem() - handle devmem resource entry 653400e64dfSOhad Ben-Cohen * @rproc: remote processor handle 654400e64dfSOhad Ben-Cohen * @rsc: the devmem resource entry 655fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 656400e64dfSOhad Ben-Cohen * 657400e64dfSOhad Ben-Cohen * Remote processors commonly need to access certain on-chip peripherals. 658400e64dfSOhad Ben-Cohen * 659400e64dfSOhad Ben-Cohen * Some of these remote processors access memory via an iommu device, 660400e64dfSOhad Ben-Cohen * and might require us to configure their iommu before they can access 661400e64dfSOhad Ben-Cohen * the on-chip peripherals they need. 662400e64dfSOhad Ben-Cohen * 663400e64dfSOhad Ben-Cohen * This resource entry is a request to map such a peripheral device. 664400e64dfSOhad Ben-Cohen * 665400e64dfSOhad Ben-Cohen * These devmem entries will contain the physical address of the device in 666400e64dfSOhad Ben-Cohen * the 'pa' member. If a specific device address is expected, then 'da' will 667400e64dfSOhad Ben-Cohen * contain it (currently this is the only use case supported). 'len' will 668400e64dfSOhad Ben-Cohen * contain the size of the physical region we need to map. 669400e64dfSOhad Ben-Cohen * 670400e64dfSOhad Ben-Cohen * Currently we just "trust" those devmem entries to contain valid physical 671400e64dfSOhad Ben-Cohen * addresses, but this is going to change: we want the implementations to 672400e64dfSOhad Ben-Cohen * tell us ranges of physical addresses the firmware is allowed to request, 673400e64dfSOhad Ben-Cohen * and not allow firmwares to request access to physical addresses that 674400e64dfSOhad Ben-Cohen * are outside those ranges. 675400e64dfSOhad Ben-Cohen */ 676fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc, 677a2b950acSOhad Ben-Cohen int offset, int avail) 678400e64dfSOhad Ben-Cohen { 679400e64dfSOhad Ben-Cohen struct rproc_mem_entry *mapping; 680b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 681400e64dfSOhad Ben-Cohen int ret; 682400e64dfSOhad Ben-Cohen 683400e64dfSOhad Ben-Cohen /* no point in handling this resource without a valid iommu domain */ 684400e64dfSOhad Ben-Cohen if (!rproc->domain) 685400e64dfSOhad Ben-Cohen return -EINVAL; 686400e64dfSOhad Ben-Cohen 687fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 688b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc is truncated\n"); 689fd2c15ecSOhad Ben-Cohen return -EINVAL; 690fd2c15ecSOhad Ben-Cohen } 691fd2c15ecSOhad Ben-Cohen 692fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 693fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 694b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc has non zero reserved bytes\n"); 695fd2c15ecSOhad Ben-Cohen return -EINVAL; 696fd2c15ecSOhad Ben-Cohen } 697fd2c15ecSOhad Ben-Cohen 698400e64dfSOhad Ben-Cohen mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 699172e6ab1SSuman Anna if (!mapping) 700400e64dfSOhad Ben-Cohen return -ENOMEM; 701400e64dfSOhad Ben-Cohen 702400e64dfSOhad Ben-Cohen ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); 703400e64dfSOhad Ben-Cohen if (ret) { 704b5ab5e24SOhad Ben-Cohen dev_err(dev, "failed to map devmem: %d\n", ret); 705400e64dfSOhad Ben-Cohen goto out; 706400e64dfSOhad Ben-Cohen } 707400e64dfSOhad Ben-Cohen 708400e64dfSOhad Ben-Cohen /* 709400e64dfSOhad Ben-Cohen * We'll need this info later when we'll want to unmap everything 710400e64dfSOhad Ben-Cohen * (e.g. on shutdown). 711400e64dfSOhad Ben-Cohen * 712400e64dfSOhad Ben-Cohen * We can't trust the remote processor not to change the resource 713400e64dfSOhad Ben-Cohen * table, so we must maintain this info independently. 714400e64dfSOhad Ben-Cohen */ 715400e64dfSOhad Ben-Cohen mapping->da = rsc->da; 716400e64dfSOhad Ben-Cohen mapping->len = rsc->len; 717400e64dfSOhad Ben-Cohen list_add_tail(&mapping->node, &rproc->mappings); 718400e64dfSOhad Ben-Cohen 719b5ab5e24SOhad Ben-Cohen dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", 720400e64dfSOhad Ben-Cohen rsc->pa, rsc->da, rsc->len); 721400e64dfSOhad Ben-Cohen 722400e64dfSOhad Ben-Cohen return 0; 723400e64dfSOhad Ben-Cohen 724400e64dfSOhad Ben-Cohen out: 725400e64dfSOhad Ben-Cohen kfree(mapping); 726400e64dfSOhad Ben-Cohen return ret; 727400e64dfSOhad Ben-Cohen } 728400e64dfSOhad Ben-Cohen 729400e64dfSOhad Ben-Cohen /** 730d7c51706SLoic Pallardy * rproc_alloc_carveout() - allocated specified carveout 731d7c51706SLoic Pallardy * @rproc: rproc handle 732d7c51706SLoic Pallardy * @mem: the memory entry to allocate 733d7c51706SLoic Pallardy * 734d7c51706SLoic Pallardy * This function allocate specified memory entry @mem using 735d7c51706SLoic Pallardy * dma_alloc_coherent() as default allocator 736d7c51706SLoic Pallardy */ 737d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc, 738d7c51706SLoic Pallardy struct rproc_mem_entry *mem) 739d7c51706SLoic Pallardy { 740d7c51706SLoic Pallardy struct rproc_mem_entry *mapping = NULL; 741d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 742d7c51706SLoic Pallardy dma_addr_t dma; 743d7c51706SLoic Pallardy void *va; 744d7c51706SLoic Pallardy int ret; 745d7c51706SLoic Pallardy 746d7c51706SLoic Pallardy va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL); 747d7c51706SLoic Pallardy if (!va) { 748d7c51706SLoic Pallardy dev_err(dev->parent, 749d7c51706SLoic Pallardy "failed to allocate dma memory: len 0x%x\n", mem->len); 750d7c51706SLoic Pallardy return -ENOMEM; 751d7c51706SLoic Pallardy } 752d7c51706SLoic Pallardy 753d7c51706SLoic Pallardy dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%x\n", 754d7c51706SLoic Pallardy va, &dma, mem->len); 755d7c51706SLoic Pallardy 75660f849a5SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) { 75760f849a5SLoic Pallardy /* 75860f849a5SLoic Pallardy * Check requested da is equal to dma address 75960f849a5SLoic Pallardy * and print a warn message in case of missalignment. 76060f849a5SLoic Pallardy * Don't stop rproc_start sequence as coprocessor may 76160f849a5SLoic Pallardy * build pa to da translation on its side. 76260f849a5SLoic Pallardy */ 76360f849a5SLoic Pallardy if (mem->da != (u32)dma) 76460f849a5SLoic Pallardy dev_warn(dev->parent, 76560f849a5SLoic Pallardy "Allocated carveout doesn't fit device address request\n"); 76660f849a5SLoic Pallardy } 76760f849a5SLoic Pallardy 768d7c51706SLoic Pallardy /* 769d7c51706SLoic Pallardy * Ok, this is non-standard. 770d7c51706SLoic Pallardy * 771d7c51706SLoic Pallardy * Sometimes we can't rely on the generic iommu-based DMA API 772d7c51706SLoic Pallardy * to dynamically allocate the device address and then set the IOMMU 773d7c51706SLoic Pallardy * tables accordingly, because some remote processors might 774d7c51706SLoic Pallardy * _require_ us to use hard coded device addresses that their 775d7c51706SLoic Pallardy * firmware was compiled with. 776d7c51706SLoic Pallardy * 777d7c51706SLoic Pallardy * In this case, we must use the IOMMU API directly and map 778d7c51706SLoic Pallardy * the memory to the device address as expected by the remote 779d7c51706SLoic Pallardy * processor. 780d7c51706SLoic Pallardy * 781d7c51706SLoic Pallardy * Obviously such remote processor devices should not be configured 782d7c51706SLoic Pallardy * to use the iommu-based DMA API: we expect 'dma' to contain the 783d7c51706SLoic Pallardy * physical address in this case. 784d7c51706SLoic Pallardy */ 78560f849a5SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) { 786d7c51706SLoic Pallardy mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 787d7c51706SLoic Pallardy if (!mapping) { 788d7c51706SLoic Pallardy ret = -ENOMEM; 789d7c51706SLoic Pallardy goto dma_free; 790d7c51706SLoic Pallardy } 791d7c51706SLoic Pallardy 792d7c51706SLoic Pallardy ret = iommu_map(rproc->domain, mem->da, dma, mem->len, 793d7c51706SLoic Pallardy mem->flags); 794d7c51706SLoic Pallardy if (ret) { 795d7c51706SLoic Pallardy dev_err(dev, "iommu_map failed: %d\n", ret); 796d7c51706SLoic Pallardy goto free_mapping; 797d7c51706SLoic Pallardy } 798d7c51706SLoic Pallardy 799d7c51706SLoic Pallardy /* 800d7c51706SLoic Pallardy * We'll need this info later when we'll want to unmap 801d7c51706SLoic Pallardy * everything (e.g. on shutdown). 802d7c51706SLoic Pallardy * 803d7c51706SLoic Pallardy * We can't trust the remote processor not to change the 804d7c51706SLoic Pallardy * resource table, so we must maintain this info independently. 805d7c51706SLoic Pallardy */ 806d7c51706SLoic Pallardy mapping->da = mem->da; 807d7c51706SLoic Pallardy mapping->len = mem->len; 808d7c51706SLoic Pallardy list_add_tail(&mapping->node, &rproc->mappings); 809d7c51706SLoic Pallardy 810d7c51706SLoic Pallardy dev_dbg(dev, "carveout mapped 0x%x to %pad\n", 811d7c51706SLoic Pallardy mem->da, &dma); 81260f849a5SLoic Pallardy } 81360f849a5SLoic Pallardy 81460f849a5SLoic Pallardy if (mem->da == FW_RSC_ADDR_ANY) { 815b36de8cfSLoic Pallardy /* Update device address as undefined by requester */ 816b36de8cfSLoic Pallardy if ((u64)dma & HIGH_BITS_MASK) 817b36de8cfSLoic Pallardy dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n"); 818b36de8cfSLoic Pallardy 819d7c51706SLoic Pallardy mem->da = (u32)dma; 820d7c51706SLoic Pallardy } 821d7c51706SLoic Pallardy 82280137b40SLoic Pallardy mem->dma = dma; 823d7c51706SLoic Pallardy mem->va = va; 824d7c51706SLoic Pallardy 825d7c51706SLoic Pallardy return 0; 826d7c51706SLoic Pallardy 827d7c51706SLoic Pallardy free_mapping: 828d7c51706SLoic Pallardy kfree(mapping); 829d7c51706SLoic Pallardy dma_free: 830d7c51706SLoic Pallardy dma_free_coherent(dev->parent, mem->len, va, dma); 831d7c51706SLoic Pallardy return ret; 832d7c51706SLoic Pallardy } 833d7c51706SLoic Pallardy 834d7c51706SLoic Pallardy /** 835f2e74abfSLoic Pallardy * rproc_release_carveout() - release acquired carveout 836f2e74abfSLoic Pallardy * @rproc: rproc handle 837f2e74abfSLoic Pallardy * @mem: the memory entry to release 838f2e74abfSLoic Pallardy * 839f2e74abfSLoic Pallardy * This function releases specified memory entry @mem allocated via 840d7c51706SLoic Pallardy * rproc_alloc_carveout() function by @rproc. 841f2e74abfSLoic Pallardy */ 842f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc, 843f2e74abfSLoic Pallardy struct rproc_mem_entry *mem) 844f2e74abfSLoic Pallardy { 845f2e74abfSLoic Pallardy struct device *dev = &rproc->dev; 846f2e74abfSLoic Pallardy 847f2e74abfSLoic Pallardy /* clean up carveout allocations */ 848f2e74abfSLoic Pallardy dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma); 849f2e74abfSLoic Pallardy return 0; 850f2e74abfSLoic Pallardy } 851f2e74abfSLoic Pallardy 852f2e74abfSLoic Pallardy /** 853400e64dfSOhad Ben-Cohen * rproc_handle_carveout() - handle phys contig memory allocation requests 854400e64dfSOhad Ben-Cohen * @rproc: rproc handle 855400e64dfSOhad Ben-Cohen * @rsc: the resource entry 856fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for image validation) 857400e64dfSOhad Ben-Cohen * 858400e64dfSOhad Ben-Cohen * This function will handle firmware requests for allocation of physically 859400e64dfSOhad Ben-Cohen * contiguous memory regions. 860400e64dfSOhad Ben-Cohen * 861400e64dfSOhad Ben-Cohen * These request entries should come first in the firmware's resource table, 862400e64dfSOhad Ben-Cohen * as other firmware entries might request placing other data objects inside 863400e64dfSOhad Ben-Cohen * these memory regions (e.g. data/code segments, trace resource entries, ...). 864400e64dfSOhad Ben-Cohen * 865400e64dfSOhad Ben-Cohen * Allocating memory this way helps utilizing the reserved physical memory 866400e64dfSOhad Ben-Cohen * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries 867400e64dfSOhad Ben-Cohen * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB 868400e64dfSOhad Ben-Cohen * pressure is important; it may have a substantial impact on performance. 869400e64dfSOhad Ben-Cohen */ 870fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc, 871a2b950acSOhad Ben-Cohen struct fw_rsc_carveout *rsc, 872a2b950acSOhad Ben-Cohen int offset, int avail) 873400e64dfSOhad Ben-Cohen { 874d7c51706SLoic Pallardy struct rproc_mem_entry *carveout; 875b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 876400e64dfSOhad Ben-Cohen 877fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 878b5ab5e24SOhad Ben-Cohen dev_err(dev, "carveout rsc is truncated\n"); 879fd2c15ecSOhad Ben-Cohen return -EINVAL; 880fd2c15ecSOhad Ben-Cohen } 881fd2c15ecSOhad Ben-Cohen 882fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 883fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 884fd2c15ecSOhad Ben-Cohen dev_err(dev, "carveout rsc has non zero reserved bytes\n"); 885fd2c15ecSOhad Ben-Cohen return -EINVAL; 886fd2c15ecSOhad Ben-Cohen } 887fd2c15ecSOhad Ben-Cohen 8889d7814a9SAnna, Suman dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n", 88935386166SLee Jones rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); 890fd2c15ecSOhad Ben-Cohen 891ffa5f9c8SLoic Pallardy /* 892ffa5f9c8SLoic Pallardy * Check carveout rsc already part of a registered carveout, 893ffa5f9c8SLoic Pallardy * Search by name, then check the da and length 894ffa5f9c8SLoic Pallardy */ 895ffa5f9c8SLoic Pallardy carveout = rproc_find_carveout_by_name(rproc, rsc->name); 896ffa5f9c8SLoic Pallardy 897ffa5f9c8SLoic Pallardy if (carveout) { 898ffa5f9c8SLoic Pallardy if (carveout->rsc_offset != FW_RSC_ADDR_ANY) { 899ffa5f9c8SLoic Pallardy dev_err(dev, 900ffa5f9c8SLoic Pallardy "Carveout already associated to resource table\n"); 901ffa5f9c8SLoic Pallardy return -ENOMEM; 902ffa5f9c8SLoic Pallardy } 903ffa5f9c8SLoic Pallardy 904ffa5f9c8SLoic Pallardy if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len)) 905ffa5f9c8SLoic Pallardy return -ENOMEM; 906ffa5f9c8SLoic Pallardy 907ffa5f9c8SLoic Pallardy /* Update memory carveout with resource table info */ 908ffa5f9c8SLoic Pallardy carveout->rsc_offset = offset; 909ffa5f9c8SLoic Pallardy carveout->flags = rsc->flags; 910ffa5f9c8SLoic Pallardy 911ffa5f9c8SLoic Pallardy return 0; 912ffa5f9c8SLoic Pallardy } 913ffa5f9c8SLoic Pallardy 914d7c51706SLoic Pallardy /* Register carveout in in list */ 91599cf0361SBen Dooks (Codethink) carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da, 916d7c51706SLoic Pallardy rproc_alloc_carveout, 917d7c51706SLoic Pallardy rproc_release_carveout, rsc->name); 918d7c51706SLoic Pallardy if (!carveout) { 919d7c51706SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 92072029c90SLoic Pallardy return -ENOMEM; 921400e64dfSOhad Ben-Cohen } 922400e64dfSOhad Ben-Cohen 923d7c51706SLoic Pallardy carveout->flags = rsc->flags; 924d7c51706SLoic Pallardy carveout->rsc_offset = offset; 92515c0b025SLoic Pallardy rproc_add_carveout(rproc, carveout); 926400e64dfSOhad Ben-Cohen 927400e64dfSOhad Ben-Cohen return 0; 928400e64dfSOhad Ben-Cohen } 929400e64dfSOhad Ben-Cohen 93072029c90SLoic Pallardy /** 93115c0b025SLoic Pallardy * rproc_add_carveout() - register an allocated carveout region 93215c0b025SLoic Pallardy * @rproc: rproc handle 93315c0b025SLoic Pallardy * @mem: memory entry to register 93415c0b025SLoic Pallardy * 93515c0b025SLoic Pallardy * This function registers specified memory entry in @rproc carveouts list. 93615c0b025SLoic Pallardy * Specified carveout should have been allocated before registering. 93715c0b025SLoic Pallardy */ 93815c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) 93915c0b025SLoic Pallardy { 94015c0b025SLoic Pallardy list_add_tail(&mem->node, &rproc->carveouts); 94115c0b025SLoic Pallardy } 94215c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout); 94315c0b025SLoic Pallardy 94415c0b025SLoic Pallardy /** 94572029c90SLoic Pallardy * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct 94672029c90SLoic Pallardy * @dev: pointer on device struct 94772029c90SLoic Pallardy * @va: virtual address 94872029c90SLoic Pallardy * @dma: dma address 94972029c90SLoic Pallardy * @len: memory carveout length 95072029c90SLoic Pallardy * @da: device address 951a9f6fe0dSLoic Pallardy * @alloc: memory carveout allocation function 952a9f6fe0dSLoic Pallardy * @release: memory carveout release function 95372029c90SLoic Pallardy * @name: carveout name 95472029c90SLoic Pallardy * 95572029c90SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 95672029c90SLoic Pallardy * provided by client. 95772029c90SLoic Pallardy */ 95872029c90SLoic Pallardy struct rproc_mem_entry * 95972029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev, 96072029c90SLoic Pallardy void *va, dma_addr_t dma, int len, u32 da, 961d7c51706SLoic Pallardy int (*alloc)(struct rproc *, struct rproc_mem_entry *), 96272029c90SLoic Pallardy int (*release)(struct rproc *, struct rproc_mem_entry *), 96372029c90SLoic Pallardy const char *name, ...) 96472029c90SLoic Pallardy { 96572029c90SLoic Pallardy struct rproc_mem_entry *mem; 96672029c90SLoic Pallardy va_list args; 96772029c90SLoic Pallardy 96872029c90SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 96972029c90SLoic Pallardy if (!mem) 97072029c90SLoic Pallardy return mem; 97172029c90SLoic Pallardy 97272029c90SLoic Pallardy mem->va = va; 97372029c90SLoic Pallardy mem->dma = dma; 97472029c90SLoic Pallardy mem->da = da; 97572029c90SLoic Pallardy mem->len = len; 976d7c51706SLoic Pallardy mem->alloc = alloc; 97772029c90SLoic Pallardy mem->release = release; 978d7c51706SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 9791429cca1SLoic Pallardy mem->of_resm_idx = -1; 98072029c90SLoic Pallardy 98172029c90SLoic Pallardy va_start(args, name); 98272029c90SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 98372029c90SLoic Pallardy va_end(args); 98472029c90SLoic Pallardy 98572029c90SLoic Pallardy return mem; 98672029c90SLoic Pallardy } 98772029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init); 98872029c90SLoic Pallardy 98972029c90SLoic Pallardy /** 9901429cca1SLoic Pallardy * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct 9911429cca1SLoic Pallardy * from a reserved memory phandle 9921429cca1SLoic Pallardy * @dev: pointer on device struct 9931429cca1SLoic Pallardy * @of_resm_idx: reserved memory phandle index in "memory-region" 9941429cca1SLoic Pallardy * @len: memory carveout length 9951429cca1SLoic Pallardy * @da: device address 9961429cca1SLoic Pallardy * @name: carveout name 9971429cca1SLoic Pallardy * 9981429cca1SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 9991429cca1SLoic Pallardy * provided by client. 10001429cca1SLoic Pallardy */ 10011429cca1SLoic Pallardy struct rproc_mem_entry * 10021429cca1SLoic Pallardy rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, int len, 10031429cca1SLoic Pallardy u32 da, const char *name, ...) 10041429cca1SLoic Pallardy { 10051429cca1SLoic Pallardy struct rproc_mem_entry *mem; 10061429cca1SLoic Pallardy va_list args; 10071429cca1SLoic Pallardy 10081429cca1SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 10091429cca1SLoic Pallardy if (!mem) 10101429cca1SLoic Pallardy return mem; 10111429cca1SLoic Pallardy 10121429cca1SLoic Pallardy mem->da = da; 10131429cca1SLoic Pallardy mem->len = len; 10141429cca1SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 10151429cca1SLoic Pallardy mem->of_resm_idx = of_resm_idx; 10161429cca1SLoic Pallardy 10171429cca1SLoic Pallardy va_start(args, name); 10181429cca1SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 10191429cca1SLoic Pallardy va_end(args); 10201429cca1SLoic Pallardy 10211429cca1SLoic Pallardy return mem; 10221429cca1SLoic Pallardy } 10231429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init); 10241429cca1SLoic Pallardy 10251429cca1SLoic Pallardy /** 1026e12bc14bSOhad Ben-Cohen * A lookup table for resource handlers. The indices are defined in 1027e12bc14bSOhad Ben-Cohen * enum fw_resource_type. 1028e12bc14bSOhad Ben-Cohen */ 1029232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { 1030fd2c15ecSOhad Ben-Cohen [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout, 1031fd2c15ecSOhad Ben-Cohen [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem, 1032fd2c15ecSOhad Ben-Cohen [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace, 1033232fcdbbSSjur Brændeland [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev, 1034232fcdbbSSjur Brændeland }; 1035232fcdbbSSjur Brændeland 1036400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */ 1037a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc, 1038232fcdbbSSjur Brændeland rproc_handle_resource_t handlers[RSC_LAST]) 1039400e64dfSOhad Ben-Cohen { 1040b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1041e12bc14bSOhad Ben-Cohen rproc_handle_resource_t handler; 1042fd2c15ecSOhad Ben-Cohen int ret = 0, i; 1043400e64dfSOhad Ben-Cohen 1044d4bb86f2SBjorn Andersson if (!rproc->table_ptr) 1045d4bb86f2SBjorn Andersson return 0; 1046d4bb86f2SBjorn Andersson 1047a2b950acSOhad Ben-Cohen for (i = 0; i < rproc->table_ptr->num; i++) { 1048a2b950acSOhad Ben-Cohen int offset = rproc->table_ptr->offset[i]; 1049a2b950acSOhad Ben-Cohen struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; 1050a4b24c75SBjorn Andersson int avail = rproc->table_sz - offset - sizeof(*hdr); 1051fd2c15ecSOhad Ben-Cohen void *rsc = (void *)hdr + sizeof(*hdr); 1052400e64dfSOhad Ben-Cohen 1053fd2c15ecSOhad Ben-Cohen /* make sure table isn't truncated */ 1054fd2c15ecSOhad Ben-Cohen if (avail < 0) { 1055fd2c15ecSOhad Ben-Cohen dev_err(dev, "rsc table is truncated\n"); 1056fd2c15ecSOhad Ben-Cohen return -EINVAL; 1057fd2c15ecSOhad Ben-Cohen } 1058fd2c15ecSOhad Ben-Cohen 1059fd2c15ecSOhad Ben-Cohen dev_dbg(dev, "rsc: type %d\n", hdr->type); 1060fd2c15ecSOhad Ben-Cohen 1061b1a17513SClement Leger if (hdr->type >= RSC_VENDOR_START && 1062b1a17513SClement Leger hdr->type <= RSC_VENDOR_END) { 1063b1a17513SClement Leger ret = rproc_handle_rsc(rproc, hdr->type, rsc, 1064b1a17513SClement Leger offset + sizeof(*hdr), avail); 1065b1a17513SClement Leger if (ret == RSC_HANDLED) 1066b1a17513SClement Leger continue; 1067b1a17513SClement Leger else if (ret < 0) 1068b1a17513SClement Leger break; 1069b1a17513SClement Leger 1070b1a17513SClement Leger dev_warn(dev, "unsupported vendor resource %d\n", 1071b1a17513SClement Leger hdr->type); 1072b1a17513SClement Leger continue; 1073b1a17513SClement Leger } 1074b1a17513SClement Leger 1075fd2c15ecSOhad Ben-Cohen if (hdr->type >= RSC_LAST) { 1076fd2c15ecSOhad Ben-Cohen dev_warn(dev, "unsupported resource %d\n", hdr->type); 1077e12bc14bSOhad Ben-Cohen continue; 1078400e64dfSOhad Ben-Cohen } 1079400e64dfSOhad Ben-Cohen 1080232fcdbbSSjur Brændeland handler = handlers[hdr->type]; 1081e12bc14bSOhad Ben-Cohen if (!handler) 1082e12bc14bSOhad Ben-Cohen continue; 1083e12bc14bSOhad Ben-Cohen 1084a2b950acSOhad Ben-Cohen ret = handler(rproc, rsc, offset + sizeof(*hdr), avail); 10857a186941SOhad Ben-Cohen if (ret) 1086400e64dfSOhad Ben-Cohen break; 1087400e64dfSOhad Ben-Cohen } 1088400e64dfSOhad Ben-Cohen 1089400e64dfSOhad Ben-Cohen return ret; 1090400e64dfSOhad Ben-Cohen } 1091400e64dfSOhad Ben-Cohen 1092c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc) 1093c455daa4SBjorn Andersson { 1094c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1095c455daa4SBjorn Andersson int ret; 1096c455daa4SBjorn Andersson 1097c455daa4SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1098c455daa4SBjorn Andersson if (subdev->prepare) { 1099c455daa4SBjorn Andersson ret = subdev->prepare(subdev); 1100c455daa4SBjorn Andersson if (ret) 1101c455daa4SBjorn Andersson goto unroll_preparation; 1102c455daa4SBjorn Andersson } 1103c455daa4SBjorn Andersson } 1104c455daa4SBjorn Andersson 1105c455daa4SBjorn Andersson return 0; 1106c455daa4SBjorn Andersson 1107c455daa4SBjorn Andersson unroll_preparation: 1108c455daa4SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1109c455daa4SBjorn Andersson if (subdev->unprepare) 1110c455daa4SBjorn Andersson subdev->unprepare(subdev); 1111c455daa4SBjorn Andersson } 1112c455daa4SBjorn Andersson 1113c455daa4SBjorn Andersson return ret; 1114c455daa4SBjorn Andersson } 1115c455daa4SBjorn Andersson 1116618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc) 11177bdc9650SBjorn Andersson { 11187bdc9650SBjorn Andersson struct rproc_subdev *subdev; 11197bdc9650SBjorn Andersson int ret; 11207bdc9650SBjorn Andersson 11217bdc9650SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1122be37b1e0SBjorn Andersson if (subdev->start) { 1123618fcff3SBjorn Andersson ret = subdev->start(subdev); 11247bdc9650SBjorn Andersson if (ret) 11257bdc9650SBjorn Andersson goto unroll_registration; 11267bdc9650SBjorn Andersson } 1127be37b1e0SBjorn Andersson } 11287bdc9650SBjorn Andersson 11297bdc9650SBjorn Andersson return 0; 11307bdc9650SBjorn Andersson 11317bdc9650SBjorn Andersson unroll_registration: 1132be37b1e0SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1133be37b1e0SBjorn Andersson if (subdev->stop) 1134618fcff3SBjorn Andersson subdev->stop(subdev, true); 1135be37b1e0SBjorn Andersson } 11367bdc9650SBjorn Andersson 11377bdc9650SBjorn Andersson return ret; 11387bdc9650SBjorn Andersson } 11397bdc9650SBjorn Andersson 1140618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed) 11417bdc9650SBjorn Andersson { 11427bdc9650SBjorn Andersson struct rproc_subdev *subdev; 11437bdc9650SBjorn Andersson 1144be37b1e0SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1145be37b1e0SBjorn Andersson if (subdev->stop) 1146618fcff3SBjorn Andersson subdev->stop(subdev, crashed); 11477bdc9650SBjorn Andersson } 1148be37b1e0SBjorn Andersson } 11497bdc9650SBjorn Andersson 1150c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc) 1151c455daa4SBjorn Andersson { 1152c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1153c455daa4SBjorn Andersson 1154c455daa4SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1155c455daa4SBjorn Andersson if (subdev->unprepare) 1156c455daa4SBjorn Andersson subdev->unprepare(subdev); 1157c455daa4SBjorn Andersson } 1158c455daa4SBjorn Andersson } 1159c455daa4SBjorn Andersson 1160400e64dfSOhad Ben-Cohen /** 1161d7c51706SLoic Pallardy * rproc_alloc_registered_carveouts() - allocate all carveouts registered 1162d7c51706SLoic Pallardy * in the list 1163d7c51706SLoic Pallardy * @rproc: the remote processor handle 1164d7c51706SLoic Pallardy * 1165d7c51706SLoic Pallardy * This function parses registered carveout list, performs allocation 1166d7c51706SLoic Pallardy * if alloc() ops registered and updates resource table information 1167d7c51706SLoic Pallardy * if rsc_offset set. 1168d7c51706SLoic Pallardy * 1169d7c51706SLoic Pallardy * Return: 0 on success 1170d7c51706SLoic Pallardy */ 1171d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc) 1172d7c51706SLoic Pallardy { 1173d7c51706SLoic Pallardy struct rproc_mem_entry *entry, *tmp; 1174d7c51706SLoic Pallardy struct fw_rsc_carveout *rsc; 1175d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 1176b36de8cfSLoic Pallardy u64 pa; 1177d7c51706SLoic Pallardy int ret; 1178d7c51706SLoic Pallardy 1179d7c51706SLoic Pallardy list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1180d7c51706SLoic Pallardy if (entry->alloc) { 1181d7c51706SLoic Pallardy ret = entry->alloc(rproc, entry); 1182d7c51706SLoic Pallardy if (ret) { 1183d7c51706SLoic Pallardy dev_err(dev, "Unable to allocate carveout %s: %d\n", 1184d7c51706SLoic Pallardy entry->name, ret); 1185d7c51706SLoic Pallardy return -ENOMEM; 1186d7c51706SLoic Pallardy } 1187d7c51706SLoic Pallardy } 1188d7c51706SLoic Pallardy 1189d7c51706SLoic Pallardy if (entry->rsc_offset != FW_RSC_ADDR_ANY) { 1190d7c51706SLoic Pallardy /* update resource table */ 1191d7c51706SLoic Pallardy rsc = (void *)rproc->table_ptr + entry->rsc_offset; 1192d7c51706SLoic Pallardy 1193d7c51706SLoic Pallardy /* 1194d7c51706SLoic Pallardy * Some remote processors might need to know the pa 1195d7c51706SLoic Pallardy * even though they are behind an IOMMU. E.g., OMAP4's 1196d7c51706SLoic Pallardy * remote M3 processor needs this so it can control 1197d7c51706SLoic Pallardy * on-chip hardware accelerators that are not behind 1198d7c51706SLoic Pallardy * the IOMMU, and therefor must know the pa. 1199d7c51706SLoic Pallardy * 1200d7c51706SLoic Pallardy * Generally we don't want to expose physical addresses 1201d7c51706SLoic Pallardy * if we don't have to (remote processors are generally 1202d7c51706SLoic Pallardy * _not_ trusted), so we might want to do this only for 1203d7c51706SLoic Pallardy * remote processor that _must_ have this (e.g. OMAP4's 1204d7c51706SLoic Pallardy * dual M3 subsystem). 1205d7c51706SLoic Pallardy * 1206d7c51706SLoic Pallardy * Non-IOMMU processors might also want to have this info. 1207d7c51706SLoic Pallardy * In this case, the device address and the physical address 1208d7c51706SLoic Pallardy * are the same. 1209d7c51706SLoic Pallardy */ 1210ffa5f9c8SLoic Pallardy 1211ffa5f9c8SLoic Pallardy /* Use va if defined else dma to generate pa */ 1212d7c51706SLoic Pallardy if (entry->va) 1213b36de8cfSLoic Pallardy pa = (u64)rproc_va_to_pa(entry->va); 1214ffa5f9c8SLoic Pallardy else 1215b36de8cfSLoic Pallardy pa = (u64)entry->dma; 1216ffa5f9c8SLoic Pallardy 1217b36de8cfSLoic Pallardy if (((u64)pa) & HIGH_BITS_MASK) 1218b36de8cfSLoic Pallardy dev_warn(dev, 1219b36de8cfSLoic Pallardy "Physical address cast in 32bit to fit resource table format\n"); 1220b36de8cfSLoic Pallardy 1221b36de8cfSLoic Pallardy rsc->pa = (u32)pa; 1222ffa5f9c8SLoic Pallardy rsc->da = entry->da; 1223ffa5f9c8SLoic Pallardy rsc->len = entry->len; 1224d7c51706SLoic Pallardy } 1225d7c51706SLoic Pallardy } 1226d7c51706SLoic Pallardy 1227d7c51706SLoic Pallardy return 0; 1228d7c51706SLoic Pallardy } 1229d7c51706SLoic Pallardy 1230d7c51706SLoic Pallardy /** 12312666ca91SSarangdhar Joshi * rproc_coredump_cleanup() - clean up dump_segments list 12322666ca91SSarangdhar Joshi * @rproc: the remote processor handle 12332666ca91SSarangdhar Joshi */ 12342666ca91SSarangdhar Joshi static void rproc_coredump_cleanup(struct rproc *rproc) 12352666ca91SSarangdhar Joshi { 12362666ca91SSarangdhar Joshi struct rproc_dump_segment *entry, *tmp; 12372666ca91SSarangdhar Joshi 12382666ca91SSarangdhar Joshi list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) { 12392666ca91SSarangdhar Joshi list_del(&entry->node); 12402666ca91SSarangdhar Joshi kfree(entry); 12412666ca91SSarangdhar Joshi } 12422666ca91SSarangdhar Joshi } 12432666ca91SSarangdhar Joshi 12442666ca91SSarangdhar Joshi /** 1245400e64dfSOhad Ben-Cohen * rproc_resource_cleanup() - clean up and free all acquired resources 1246400e64dfSOhad Ben-Cohen * @rproc: rproc handle 1247400e64dfSOhad Ben-Cohen * 1248400e64dfSOhad Ben-Cohen * This function will free all resources acquired for @rproc, and it 12497a186941SOhad Ben-Cohen * is called whenever @rproc either shuts down or fails to boot. 1250400e64dfSOhad Ben-Cohen */ 1251400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc) 1252400e64dfSOhad Ben-Cohen { 1253400e64dfSOhad Ben-Cohen struct rproc_mem_entry *entry, *tmp; 1254a987e6b9SLoic Pallardy struct rproc_debug_trace *trace, *ttmp; 1255d81fb32fSBjorn Andersson struct rproc_vdev *rvdev, *rvtmp; 1256b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1257400e64dfSOhad Ben-Cohen 1258400e64dfSOhad Ben-Cohen /* clean up debugfs trace entries */ 1259a987e6b9SLoic Pallardy list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) { 1260a987e6b9SLoic Pallardy rproc_remove_trace_file(trace->tfile); 1261400e64dfSOhad Ben-Cohen rproc->num_traces--; 1262a987e6b9SLoic Pallardy list_del(&trace->node); 1263a987e6b9SLoic Pallardy kfree(trace); 1264400e64dfSOhad Ben-Cohen } 1265400e64dfSOhad Ben-Cohen 1266400e64dfSOhad Ben-Cohen /* clean up iommu mapping entries */ 1267400e64dfSOhad Ben-Cohen list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { 1268400e64dfSOhad Ben-Cohen size_t unmapped; 1269400e64dfSOhad Ben-Cohen 1270400e64dfSOhad Ben-Cohen unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); 1271400e64dfSOhad Ben-Cohen if (unmapped != entry->len) { 1272400e64dfSOhad Ben-Cohen /* nothing much to do besides complaining */ 1273e981f6d4SSjur Brændeland dev_err(dev, "failed to unmap %u/%zu\n", entry->len, 1274400e64dfSOhad Ben-Cohen unmapped); 1275400e64dfSOhad Ben-Cohen } 1276400e64dfSOhad Ben-Cohen 1277400e64dfSOhad Ben-Cohen list_del(&entry->node); 1278400e64dfSOhad Ben-Cohen kfree(entry); 1279400e64dfSOhad Ben-Cohen } 1280b6356a01SSuman Anna 1281b6356a01SSuman Anna /* clean up carveout allocations */ 1282b6356a01SSuman Anna list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1283f2e74abfSLoic Pallardy if (entry->release) 1284f2e74abfSLoic Pallardy entry->release(rproc, entry); 1285b6356a01SSuman Anna list_del(&entry->node); 1286b6356a01SSuman Anna kfree(entry); 1287b6356a01SSuman Anna } 1288d81fb32fSBjorn Andersson 1289d81fb32fSBjorn Andersson /* clean up remote vdev entries */ 1290f5bcb353SBjorn Andersson list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) 12912b45cef5SBjorn Andersson kref_put(&rvdev->refcount, rproc_vdev_release); 12922666ca91SSarangdhar Joshi 12932666ca91SSarangdhar Joshi rproc_coredump_cleanup(rproc); 12942b45cef5SBjorn Andersson } 1295400e64dfSOhad Ben-Cohen 12961efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw) 12971efa30d0SSarangdhar Joshi { 1298a4b24c75SBjorn Andersson struct resource_table *loaded_table; 12991efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 1300a4b24c75SBjorn Andersson int ret; 13011efa30d0SSarangdhar Joshi 13021efa30d0SSarangdhar Joshi /* load the ELF segments to memory */ 13031efa30d0SSarangdhar Joshi ret = rproc_load_segments(rproc, fw); 13041efa30d0SSarangdhar Joshi if (ret) { 13051efa30d0SSarangdhar Joshi dev_err(dev, "Failed to load program segments: %d\n", ret); 13061efa30d0SSarangdhar Joshi return ret; 13071efa30d0SSarangdhar Joshi } 13081efa30d0SSarangdhar Joshi 13091efa30d0SSarangdhar Joshi /* 13101efa30d0SSarangdhar Joshi * The starting device has been given the rproc->cached_table as the 13111efa30d0SSarangdhar Joshi * resource table. The address of the vring along with the other 13121efa30d0SSarangdhar Joshi * allocated resources (carveouts etc) is stored in cached_table. 13131efa30d0SSarangdhar Joshi * In order to pass this information to the remote device we must copy 13141efa30d0SSarangdhar Joshi * this information to device memory. We also update the table_ptr so 13151efa30d0SSarangdhar Joshi * that any subsequent changes will be applied to the loaded version. 13161efa30d0SSarangdhar Joshi */ 13171efa30d0SSarangdhar Joshi loaded_table = rproc_find_loaded_rsc_table(rproc, fw); 13181efa30d0SSarangdhar Joshi if (loaded_table) { 1319a4b24c75SBjorn Andersson memcpy(loaded_table, rproc->cached_table, rproc->table_sz); 13201efa30d0SSarangdhar Joshi rproc->table_ptr = loaded_table; 13211efa30d0SSarangdhar Joshi } 13221efa30d0SSarangdhar Joshi 1323c455daa4SBjorn Andersson ret = rproc_prepare_subdevices(rproc); 1324c455daa4SBjorn Andersson if (ret) { 1325c455daa4SBjorn Andersson dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1326c455daa4SBjorn Andersson rproc->name, ret); 1327f68d51bdSSuman Anna goto reset_table_ptr; 1328c455daa4SBjorn Andersson } 1329c455daa4SBjorn Andersson 13301efa30d0SSarangdhar Joshi /* power up the remote processor */ 13311efa30d0SSarangdhar Joshi ret = rproc->ops->start(rproc); 13321efa30d0SSarangdhar Joshi if (ret) { 13331efa30d0SSarangdhar Joshi dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); 1334c455daa4SBjorn Andersson goto unprepare_subdevices; 13351efa30d0SSarangdhar Joshi } 13361efa30d0SSarangdhar Joshi 1337618fcff3SBjorn Andersson /* Start any subdevices for the remote processor */ 1338618fcff3SBjorn Andersson ret = rproc_start_subdevices(rproc); 13391efa30d0SSarangdhar Joshi if (ret) { 13401efa30d0SSarangdhar Joshi dev_err(dev, "failed to probe subdevices for %s: %d\n", 13411efa30d0SSarangdhar Joshi rproc->name, ret); 1342c455daa4SBjorn Andersson goto stop_rproc; 13431efa30d0SSarangdhar Joshi } 13441efa30d0SSarangdhar Joshi 13451efa30d0SSarangdhar Joshi rproc->state = RPROC_RUNNING; 13461efa30d0SSarangdhar Joshi 13471efa30d0SSarangdhar Joshi dev_info(dev, "remote processor %s is now up\n", rproc->name); 13481efa30d0SSarangdhar Joshi 13491efa30d0SSarangdhar Joshi return 0; 1350c455daa4SBjorn Andersson 1351c455daa4SBjorn Andersson stop_rproc: 1352c455daa4SBjorn Andersson rproc->ops->stop(rproc); 1353c455daa4SBjorn Andersson unprepare_subdevices: 1354c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1355f68d51bdSSuman Anna reset_table_ptr: 1356f68d51bdSSuman Anna rproc->table_ptr = rproc->cached_table; 1357c455daa4SBjorn Andersson 1358c455daa4SBjorn Andersson return ret; 13591efa30d0SSarangdhar Joshi } 13601efa30d0SSarangdhar Joshi 1361400e64dfSOhad Ben-Cohen /* 1362400e64dfSOhad Ben-Cohen * take a firmware and boot a remote processor with it. 1363400e64dfSOhad Ben-Cohen */ 1364400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) 1365400e64dfSOhad Ben-Cohen { 1366b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1367400e64dfSOhad Ben-Cohen const char *name = rproc->firmware; 136858b64090SBjorn Andersson int ret; 1369400e64dfSOhad Ben-Cohen 1370400e64dfSOhad Ben-Cohen ret = rproc_fw_sanity_check(rproc, fw); 1371400e64dfSOhad Ben-Cohen if (ret) 1372400e64dfSOhad Ben-Cohen return ret; 1373400e64dfSOhad Ben-Cohen 1374e981f6d4SSjur Brændeland dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); 1375400e64dfSOhad Ben-Cohen 1376400e64dfSOhad Ben-Cohen /* 1377400e64dfSOhad Ben-Cohen * if enabling an IOMMU isn't relevant for this rproc, this is 1378400e64dfSOhad Ben-Cohen * just a nop 1379400e64dfSOhad Ben-Cohen */ 1380400e64dfSOhad Ben-Cohen ret = rproc_enable_iommu(rproc); 1381400e64dfSOhad Ben-Cohen if (ret) { 1382400e64dfSOhad Ben-Cohen dev_err(dev, "can't enable iommu: %d\n", ret); 1383400e64dfSOhad Ben-Cohen return ret; 1384400e64dfSOhad Ben-Cohen } 1385400e64dfSOhad Ben-Cohen 13863e5f9eb5SSjur Brændeland rproc->bootaddr = rproc_get_boot_addr(rproc, fw); 1387400e64dfSOhad Ben-Cohen 1388c1d35c1aSBjorn Andersson /* Load resource table, core dump segment list etc from the firmware */ 1389c1d35c1aSBjorn Andersson ret = rproc_parse_fw(rproc, fw); 139058b64090SBjorn Andersson if (ret) 139158b64090SBjorn Andersson goto disable_iommu; 1392a0c10687SBjorn Andersson 1393b35d7afcSBjorn Andersson /* reset max_notifyid */ 1394b35d7afcSBjorn Andersson rproc->max_notifyid = -1; 1395b35d7afcSBjorn Andersson 1396c6aed238SLoic Pallardy /* reset handled vdev */ 1397c6aed238SLoic Pallardy rproc->nb_vdev = 0; 1398c6aed238SLoic Pallardy 1399400e64dfSOhad Ben-Cohen /* handle fw resources which are required to boot rproc */ 1400a4b24c75SBjorn Andersson ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1401400e64dfSOhad Ben-Cohen if (ret) { 1402400e64dfSOhad Ben-Cohen dev_err(dev, "Failed to process resources: %d\n", ret); 1403229b85a6SBjorn Andersson goto clean_up_resources; 1404400e64dfSOhad Ben-Cohen } 1405400e64dfSOhad Ben-Cohen 1406d7c51706SLoic Pallardy /* Allocate carveout resources associated to rproc */ 1407d7c51706SLoic Pallardy ret = rproc_alloc_registered_carveouts(rproc); 1408d7c51706SLoic Pallardy if (ret) { 1409d7c51706SLoic Pallardy dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1410d7c51706SLoic Pallardy ret); 1411d7c51706SLoic Pallardy goto clean_up_resources; 1412d7c51706SLoic Pallardy } 1413d7c51706SLoic Pallardy 14141efa30d0SSarangdhar Joshi ret = rproc_start(rproc, fw); 14151efa30d0SSarangdhar Joshi if (ret) 1416229b85a6SBjorn Andersson goto clean_up_resources; 1417400e64dfSOhad Ben-Cohen 1418400e64dfSOhad Ben-Cohen return 0; 1419400e64dfSOhad Ben-Cohen 1420229b85a6SBjorn Andersson clean_up_resources: 1421229b85a6SBjorn Andersson rproc_resource_cleanup(rproc); 1422a0c10687SBjorn Andersson kfree(rproc->cached_table); 1423a0c10687SBjorn Andersson rproc->cached_table = NULL; 1424988d204cSBjorn Andersson rproc->table_ptr = NULL; 142558b64090SBjorn Andersson disable_iommu: 1426400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1427400e64dfSOhad Ben-Cohen return ret; 1428400e64dfSOhad Ben-Cohen } 1429400e64dfSOhad Ben-Cohen 1430400e64dfSOhad Ben-Cohen /* 14315e6533f7SSarangdhar Joshi * take a firmware and boot it up. 1432400e64dfSOhad Ben-Cohen * 1433400e64dfSOhad Ben-Cohen * Note: this function is called asynchronously upon registration of the 1434400e64dfSOhad Ben-Cohen * remote processor (so we must wait until it completes before we try 1435400e64dfSOhad Ben-Cohen * to unregister the device. one other option is just to use kref here, 1436400e64dfSOhad Ben-Cohen * that might be cleaner). 1437400e64dfSOhad Ben-Cohen */ 14385e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context) 1439400e64dfSOhad Ben-Cohen { 1440400e64dfSOhad Ben-Cohen struct rproc *rproc = context; 1441a2b950acSOhad Ben-Cohen 14422bfc311aSBjorn Andersson rproc_boot(rproc); 1443ddf71187SBjorn Andersson 1444400e64dfSOhad Ben-Cohen release_firmware(fw); 1445400e64dfSOhad Ben-Cohen } 1446400e64dfSOhad Ben-Cohen 14475e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc) 144870b85ef8SFernando Guzman Lugo { 144970b85ef8SFernando Guzman Lugo int ret; 145070b85ef8SFernando Guzman Lugo 145170b85ef8SFernando Guzman Lugo /* 145270b85ef8SFernando Guzman Lugo * We're initiating an asynchronous firmware loading, so we can 145370b85ef8SFernando Guzman Lugo * be built-in kernel code, without hanging the boot process. 145470b85ef8SFernando Guzman Lugo */ 145570b85ef8SFernando Guzman Lugo ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, 145670b85ef8SFernando Guzman Lugo rproc->firmware, &rproc->dev, GFP_KERNEL, 14575e6533f7SSarangdhar Joshi rproc, rproc_auto_boot_callback); 14582099c77dSSarangdhar Joshi if (ret < 0) 145970b85ef8SFernando Guzman Lugo dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); 146070b85ef8SFernando Guzman Lugo 146170b85ef8SFernando Guzman Lugo return ret; 146270b85ef8SFernando Guzman Lugo } 146370b85ef8SFernando Guzman Lugo 1464880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed) 14651efa30d0SSarangdhar Joshi { 14661efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 14671efa30d0SSarangdhar Joshi int ret; 14681efa30d0SSarangdhar Joshi 1469618fcff3SBjorn Andersson /* Stop any subdevices for the remote processor */ 1470618fcff3SBjorn Andersson rproc_stop_subdevices(rproc, crashed); 14711efa30d0SSarangdhar Joshi 14720a8b81cbSBjorn Andersson /* the installed resource table is no longer accessible */ 14730a8b81cbSBjorn Andersson rproc->table_ptr = rproc->cached_table; 14740a8b81cbSBjorn Andersson 14751efa30d0SSarangdhar Joshi /* power off the remote processor */ 14761efa30d0SSarangdhar Joshi ret = rproc->ops->stop(rproc); 14771efa30d0SSarangdhar Joshi if (ret) { 14781efa30d0SSarangdhar Joshi dev_err(dev, "can't stop rproc: %d\n", ret); 14791efa30d0SSarangdhar Joshi return ret; 14801efa30d0SSarangdhar Joshi } 14811efa30d0SSarangdhar Joshi 1482c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1483c455daa4SBjorn Andersson 14841efa30d0SSarangdhar Joshi rproc->state = RPROC_OFFLINE; 14851efa30d0SSarangdhar Joshi 14861efa30d0SSarangdhar Joshi dev_info(dev, "stopped remote processor %s\n", rproc->name); 14871efa30d0SSarangdhar Joshi 14881efa30d0SSarangdhar Joshi return 0; 14891efa30d0SSarangdhar Joshi } 14901efa30d0SSarangdhar Joshi 149170b85ef8SFernando Guzman Lugo /** 14922666ca91SSarangdhar Joshi * rproc_coredump_add_segment() - add segment of device memory to coredump 14932666ca91SSarangdhar Joshi * @rproc: handle of a remote processor 14942666ca91SSarangdhar Joshi * @da: device address 14952666ca91SSarangdhar Joshi * @size: size of segment 14962666ca91SSarangdhar Joshi * 14972666ca91SSarangdhar Joshi * Add device memory to the list of segments to be included in a coredump for 14982666ca91SSarangdhar Joshi * the remoteproc. 14992666ca91SSarangdhar Joshi * 15002666ca91SSarangdhar Joshi * Return: 0 on success, negative errno on error. 15012666ca91SSarangdhar Joshi */ 15022666ca91SSarangdhar Joshi int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size) 15032666ca91SSarangdhar Joshi { 15042666ca91SSarangdhar Joshi struct rproc_dump_segment *segment; 15052666ca91SSarangdhar Joshi 15062666ca91SSarangdhar Joshi segment = kzalloc(sizeof(*segment), GFP_KERNEL); 15072666ca91SSarangdhar Joshi if (!segment) 15082666ca91SSarangdhar Joshi return -ENOMEM; 15092666ca91SSarangdhar Joshi 15102666ca91SSarangdhar Joshi segment->da = da; 15112666ca91SSarangdhar Joshi segment->size = size; 15122666ca91SSarangdhar Joshi 15132666ca91SSarangdhar Joshi list_add_tail(&segment->node, &rproc->dump_segments); 15142666ca91SSarangdhar Joshi 15152666ca91SSarangdhar Joshi return 0; 15162666ca91SSarangdhar Joshi } 15172666ca91SSarangdhar Joshi EXPORT_SYMBOL(rproc_coredump_add_segment); 15182666ca91SSarangdhar Joshi 15192666ca91SSarangdhar Joshi /** 1520ab8f873bSSibi Sankar * rproc_coredump_add_custom_segment() - add custom coredump segment 1521ab8f873bSSibi Sankar * @rproc: handle of a remote processor 1522ab8f873bSSibi Sankar * @da: device address 1523ab8f873bSSibi Sankar * @size: size of segment 1524ab8f873bSSibi Sankar * @dumpfn: custom dump function called for each segment during coredump 1525ab8f873bSSibi Sankar * @priv: private data 1526ab8f873bSSibi Sankar * 1527ab8f873bSSibi Sankar * Add device memory to the list of segments to be included in the coredump 1528ab8f873bSSibi Sankar * and associate the segment with the given custom dump function and private 1529ab8f873bSSibi Sankar * data. 1530ab8f873bSSibi Sankar * 1531ab8f873bSSibi Sankar * Return: 0 on success, negative errno on error. 1532ab8f873bSSibi Sankar */ 1533ab8f873bSSibi Sankar int rproc_coredump_add_custom_segment(struct rproc *rproc, 1534ab8f873bSSibi Sankar dma_addr_t da, size_t size, 1535ab8f873bSSibi Sankar void (*dumpfn)(struct rproc *rproc, 1536ab8f873bSSibi Sankar struct rproc_dump_segment *segment, 1537ab8f873bSSibi Sankar void *dest), 1538ab8f873bSSibi Sankar void *priv) 1539ab8f873bSSibi Sankar { 1540ab8f873bSSibi Sankar struct rproc_dump_segment *segment; 1541ab8f873bSSibi Sankar 1542ab8f873bSSibi Sankar segment = kzalloc(sizeof(*segment), GFP_KERNEL); 1543ab8f873bSSibi Sankar if (!segment) 1544ab8f873bSSibi Sankar return -ENOMEM; 1545ab8f873bSSibi Sankar 1546ab8f873bSSibi Sankar segment->da = da; 1547ab8f873bSSibi Sankar segment->size = size; 1548ab8f873bSSibi Sankar segment->priv = priv; 1549ab8f873bSSibi Sankar segment->dump = dumpfn; 1550ab8f873bSSibi Sankar 1551ab8f873bSSibi Sankar list_add_tail(&segment->node, &rproc->dump_segments); 1552ab8f873bSSibi Sankar 1553ab8f873bSSibi Sankar return 0; 1554ab8f873bSSibi Sankar } 1555ab8f873bSSibi Sankar EXPORT_SYMBOL(rproc_coredump_add_custom_segment); 1556ab8f873bSSibi Sankar 1557ab8f873bSSibi Sankar /** 15582666ca91SSarangdhar Joshi * rproc_coredump() - perform coredump 15592666ca91SSarangdhar Joshi * @rproc: rproc handle 15602666ca91SSarangdhar Joshi * 15612666ca91SSarangdhar Joshi * This function will generate an ELF header for the registered segments 15622666ca91SSarangdhar Joshi * and create a devcoredump device associated with rproc. 15632666ca91SSarangdhar Joshi */ 15642666ca91SSarangdhar Joshi static void rproc_coredump(struct rproc *rproc) 15652666ca91SSarangdhar Joshi { 15662666ca91SSarangdhar Joshi struct rproc_dump_segment *segment; 15672666ca91SSarangdhar Joshi struct elf32_phdr *phdr; 15682666ca91SSarangdhar Joshi struct elf32_hdr *ehdr; 15692666ca91SSarangdhar Joshi size_t data_size; 15702666ca91SSarangdhar Joshi size_t offset; 15712666ca91SSarangdhar Joshi void *data; 15722666ca91SSarangdhar Joshi void *ptr; 15732666ca91SSarangdhar Joshi int phnum = 0; 15742666ca91SSarangdhar Joshi 15752666ca91SSarangdhar Joshi if (list_empty(&rproc->dump_segments)) 15762666ca91SSarangdhar Joshi return; 15772666ca91SSarangdhar Joshi 15782666ca91SSarangdhar Joshi data_size = sizeof(*ehdr); 15792666ca91SSarangdhar Joshi list_for_each_entry(segment, &rproc->dump_segments, node) { 15802666ca91SSarangdhar Joshi data_size += sizeof(*phdr) + segment->size; 15812666ca91SSarangdhar Joshi 15822666ca91SSarangdhar Joshi phnum++; 15832666ca91SSarangdhar Joshi } 15842666ca91SSarangdhar Joshi 15852666ca91SSarangdhar Joshi data = vmalloc(data_size); 15862666ca91SSarangdhar Joshi if (!data) 15872666ca91SSarangdhar Joshi return; 15882666ca91SSarangdhar Joshi 15892666ca91SSarangdhar Joshi ehdr = data; 15902666ca91SSarangdhar Joshi 15912666ca91SSarangdhar Joshi memset(ehdr, 0, sizeof(*ehdr)); 15922666ca91SSarangdhar Joshi memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 15932666ca91SSarangdhar Joshi ehdr->e_ident[EI_CLASS] = ELFCLASS32; 15942666ca91SSarangdhar Joshi ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 15952666ca91SSarangdhar Joshi ehdr->e_ident[EI_VERSION] = EV_CURRENT; 15962666ca91SSarangdhar Joshi ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE; 15972666ca91SSarangdhar Joshi ehdr->e_type = ET_CORE; 15982666ca91SSarangdhar Joshi ehdr->e_machine = EM_NONE; 15992666ca91SSarangdhar Joshi ehdr->e_version = EV_CURRENT; 16002666ca91SSarangdhar Joshi ehdr->e_entry = rproc->bootaddr; 16012666ca91SSarangdhar Joshi ehdr->e_phoff = sizeof(*ehdr); 16022666ca91SSarangdhar Joshi ehdr->e_ehsize = sizeof(*ehdr); 16032666ca91SSarangdhar Joshi ehdr->e_phentsize = sizeof(*phdr); 16042666ca91SSarangdhar Joshi ehdr->e_phnum = phnum; 16052666ca91SSarangdhar Joshi 16062666ca91SSarangdhar Joshi phdr = data + ehdr->e_phoff; 16072666ca91SSarangdhar Joshi offset = ehdr->e_phoff + sizeof(*phdr) * ehdr->e_phnum; 16082666ca91SSarangdhar Joshi list_for_each_entry(segment, &rproc->dump_segments, node) { 16092666ca91SSarangdhar Joshi memset(phdr, 0, sizeof(*phdr)); 16102666ca91SSarangdhar Joshi phdr->p_type = PT_LOAD; 16112666ca91SSarangdhar Joshi phdr->p_offset = offset; 16122666ca91SSarangdhar Joshi phdr->p_vaddr = segment->da; 16132666ca91SSarangdhar Joshi phdr->p_paddr = segment->da; 16142666ca91SSarangdhar Joshi phdr->p_filesz = segment->size; 16152666ca91SSarangdhar Joshi phdr->p_memsz = segment->size; 16162666ca91SSarangdhar Joshi phdr->p_flags = PF_R | PF_W | PF_X; 16172666ca91SSarangdhar Joshi phdr->p_align = 0; 16182666ca91SSarangdhar Joshi 16193952105dSSibi Sankar if (segment->dump) { 16203952105dSSibi Sankar segment->dump(rproc, segment, data + offset); 16213952105dSSibi Sankar } else { 16222666ca91SSarangdhar Joshi ptr = rproc_da_to_va(rproc, segment->da, segment->size); 16232666ca91SSarangdhar Joshi if (!ptr) { 16242666ca91SSarangdhar Joshi dev_err(&rproc->dev, 16252666ca91SSarangdhar Joshi "invalid coredump segment (%pad, %zu)\n", 16262666ca91SSarangdhar Joshi &segment->da, segment->size); 16272666ca91SSarangdhar Joshi memset(data + offset, 0xff, segment->size); 16282666ca91SSarangdhar Joshi } else { 16292666ca91SSarangdhar Joshi memcpy(data + offset, ptr, segment->size); 16302666ca91SSarangdhar Joshi } 16313952105dSSibi Sankar } 16322666ca91SSarangdhar Joshi 16332666ca91SSarangdhar Joshi offset += phdr->p_filesz; 16342666ca91SSarangdhar Joshi phdr++; 16352666ca91SSarangdhar Joshi } 16362666ca91SSarangdhar Joshi 16372666ca91SSarangdhar Joshi dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL); 16382666ca91SSarangdhar Joshi } 16392666ca91SSarangdhar Joshi 16402666ca91SSarangdhar Joshi /** 164170b85ef8SFernando Guzman Lugo * rproc_trigger_recovery() - recover a remoteproc 164270b85ef8SFernando Guzman Lugo * @rproc: the remote processor 164370b85ef8SFernando Guzman Lugo * 164456324d7aSAnna, Suman * The recovery is done by resetting all the virtio devices, that way all the 164570b85ef8SFernando Guzman Lugo * rpmsg drivers will be reseted along with the remote processor making the 164670b85ef8SFernando Guzman Lugo * remoteproc functional again. 164770b85ef8SFernando Guzman Lugo * 164870b85ef8SFernando Guzman Lugo * This function can sleep, so it cannot be called from atomic context. 164970b85ef8SFernando Guzman Lugo */ 165070b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc) 165170b85ef8SFernando Guzman Lugo { 16527e83cab8SSarangdhar Joshi const struct firmware *firmware_p; 16537e83cab8SSarangdhar Joshi struct device *dev = &rproc->dev; 16547e83cab8SSarangdhar Joshi int ret; 16557e83cab8SSarangdhar Joshi 16567e83cab8SSarangdhar Joshi dev_err(dev, "recovering %s\n", rproc->name); 165770b85ef8SFernando Guzman Lugo 16587e83cab8SSarangdhar Joshi ret = mutex_lock_interruptible(&rproc->lock); 16597e83cab8SSarangdhar Joshi if (ret) 16607e83cab8SSarangdhar Joshi return ret; 16617e83cab8SSarangdhar Joshi 1662fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, true); 16637e83cab8SSarangdhar Joshi if (ret) 16647e83cab8SSarangdhar Joshi goto unlock_mutex; 1665ddf71187SBjorn Andersson 16662666ca91SSarangdhar Joshi /* generate coredump */ 16672666ca91SSarangdhar Joshi rproc_coredump(rproc); 16682666ca91SSarangdhar Joshi 16697e83cab8SSarangdhar Joshi /* load firmware */ 16707e83cab8SSarangdhar Joshi ret = request_firmware(&firmware_p, rproc->firmware, dev); 16717e83cab8SSarangdhar Joshi if (ret < 0) { 16727e83cab8SSarangdhar Joshi dev_err(dev, "request_firmware failed: %d\n", ret); 16737e83cab8SSarangdhar Joshi goto unlock_mutex; 16747e83cab8SSarangdhar Joshi } 1675ddf71187SBjorn Andersson 16767e83cab8SSarangdhar Joshi /* boot the remote processor up again */ 16777e83cab8SSarangdhar Joshi ret = rproc_start(rproc, firmware_p); 16787e83cab8SSarangdhar Joshi 16797e83cab8SSarangdhar Joshi release_firmware(firmware_p); 16807e83cab8SSarangdhar Joshi 16817e83cab8SSarangdhar Joshi unlock_mutex: 16827e83cab8SSarangdhar Joshi mutex_unlock(&rproc->lock); 16837e83cab8SSarangdhar Joshi return ret; 168470b85ef8SFernando Guzman Lugo } 168570b85ef8SFernando Guzman Lugo 1686400e64dfSOhad Ben-Cohen /** 16878afd519cSFernando Guzman Lugo * rproc_crash_handler_work() - handle a crash 16888afd519cSFernando Guzman Lugo * 16898afd519cSFernando Guzman Lugo * This function needs to handle everything related to a crash, like cpu 16908afd519cSFernando Guzman Lugo * registers and stack dump, information to help to debug the fatal error, etc. 16918afd519cSFernando Guzman Lugo */ 16928afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work) 16938afd519cSFernando Guzman Lugo { 16948afd519cSFernando Guzman Lugo struct rproc *rproc = container_of(work, struct rproc, crash_handler); 16958afd519cSFernando Guzman Lugo struct device *dev = &rproc->dev; 16968afd519cSFernando Guzman Lugo 16978afd519cSFernando Guzman Lugo dev_dbg(dev, "enter %s\n", __func__); 16988afd519cSFernando Guzman Lugo 16998afd519cSFernando Guzman Lugo mutex_lock(&rproc->lock); 17008afd519cSFernando Guzman Lugo 17018afd519cSFernando Guzman Lugo if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) { 17028afd519cSFernando Guzman Lugo /* handle only the first crash detected */ 17038afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 17048afd519cSFernando Guzman Lugo return; 17058afd519cSFernando Guzman Lugo } 17068afd519cSFernando Guzman Lugo 17078afd519cSFernando Guzman Lugo rproc->state = RPROC_CRASHED; 17088afd519cSFernando Guzman Lugo dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, 17098afd519cSFernando Guzman Lugo rproc->name); 17108afd519cSFernando Guzman Lugo 17118afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 17128afd519cSFernando Guzman Lugo 17132e37abb8SFernando Guzman Lugo if (!rproc->recovery_disabled) 171470b85ef8SFernando Guzman Lugo rproc_trigger_recovery(rproc); 17158afd519cSFernando Guzman Lugo } 17168afd519cSFernando Guzman Lugo 17178afd519cSFernando Guzman Lugo /** 17181b0ef906SSuman Anna * rproc_boot() - boot a remote processor 1719400e64dfSOhad Ben-Cohen * @rproc: handle of a remote processor 1720400e64dfSOhad Ben-Cohen * 1721400e64dfSOhad Ben-Cohen * Boot a remote processor (i.e. load its firmware, power it on, ...). 1722400e64dfSOhad Ben-Cohen * 1723400e64dfSOhad Ben-Cohen * If the remote processor is already powered on, this function immediately 1724400e64dfSOhad Ben-Cohen * returns (successfully). 1725400e64dfSOhad Ben-Cohen * 1726400e64dfSOhad Ben-Cohen * Returns 0 on success, and an appropriate error value otherwise. 1727400e64dfSOhad Ben-Cohen */ 17281b0ef906SSuman Anna int rproc_boot(struct rproc *rproc) 1729400e64dfSOhad Ben-Cohen { 1730400e64dfSOhad Ben-Cohen const struct firmware *firmware_p; 1731400e64dfSOhad Ben-Cohen struct device *dev; 1732400e64dfSOhad Ben-Cohen int ret; 1733400e64dfSOhad Ben-Cohen 1734400e64dfSOhad Ben-Cohen if (!rproc) { 1735400e64dfSOhad Ben-Cohen pr_err("invalid rproc handle\n"); 1736400e64dfSOhad Ben-Cohen return -EINVAL; 1737400e64dfSOhad Ben-Cohen } 1738400e64dfSOhad Ben-Cohen 1739b5ab5e24SOhad Ben-Cohen dev = &rproc->dev; 1740400e64dfSOhad Ben-Cohen 1741400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1742400e64dfSOhad Ben-Cohen if (ret) { 1743400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1744400e64dfSOhad Ben-Cohen return ret; 1745400e64dfSOhad Ben-Cohen } 1746400e64dfSOhad Ben-Cohen 17472099c77dSSarangdhar Joshi if (rproc->state == RPROC_DELETED) { 17482099c77dSSarangdhar Joshi ret = -ENODEV; 17492099c77dSSarangdhar Joshi dev_err(dev, "can't boot deleted rproc %s\n", rproc->name); 17502099c77dSSarangdhar Joshi goto unlock_mutex; 17512099c77dSSarangdhar Joshi } 17522099c77dSSarangdhar Joshi 1753400e64dfSOhad Ben-Cohen /* skip the boot process if rproc is already powered up */ 1754400e64dfSOhad Ben-Cohen if (atomic_inc_return(&rproc->power) > 1) { 1755400e64dfSOhad Ben-Cohen ret = 0; 1756400e64dfSOhad Ben-Cohen goto unlock_mutex; 1757400e64dfSOhad Ben-Cohen } 1758400e64dfSOhad Ben-Cohen 1759400e64dfSOhad Ben-Cohen dev_info(dev, "powering up %s\n", rproc->name); 1760400e64dfSOhad Ben-Cohen 1761400e64dfSOhad Ben-Cohen /* load firmware */ 1762400e64dfSOhad Ben-Cohen ret = request_firmware(&firmware_p, rproc->firmware, dev); 1763400e64dfSOhad Ben-Cohen if (ret < 0) { 1764400e64dfSOhad Ben-Cohen dev_err(dev, "request_firmware failed: %d\n", ret); 1765400e64dfSOhad Ben-Cohen goto downref_rproc; 1766400e64dfSOhad Ben-Cohen } 1767400e64dfSOhad Ben-Cohen 1768400e64dfSOhad Ben-Cohen ret = rproc_fw_boot(rproc, firmware_p); 1769400e64dfSOhad Ben-Cohen 1770400e64dfSOhad Ben-Cohen release_firmware(firmware_p); 1771400e64dfSOhad Ben-Cohen 1772400e64dfSOhad Ben-Cohen downref_rproc: 1773fbb6aacbSBjorn Andersson if (ret) 1774400e64dfSOhad Ben-Cohen atomic_dec(&rproc->power); 1775400e64dfSOhad Ben-Cohen unlock_mutex: 1776400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1777400e64dfSOhad Ben-Cohen return ret; 1778400e64dfSOhad Ben-Cohen } 1779400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot); 1780400e64dfSOhad Ben-Cohen 1781400e64dfSOhad Ben-Cohen /** 1782400e64dfSOhad Ben-Cohen * rproc_shutdown() - power off the remote processor 1783400e64dfSOhad Ben-Cohen * @rproc: the remote processor 1784400e64dfSOhad Ben-Cohen * 1785400e64dfSOhad Ben-Cohen * Power off a remote processor (previously booted with rproc_boot()). 1786400e64dfSOhad Ben-Cohen * 1787400e64dfSOhad Ben-Cohen * In case @rproc is still being used by an additional user(s), then 1788400e64dfSOhad Ben-Cohen * this function will just decrement the power refcount and exit, 1789400e64dfSOhad Ben-Cohen * without really powering off the device. 1790400e64dfSOhad Ben-Cohen * 1791400e64dfSOhad Ben-Cohen * Every call to rproc_boot() must (eventually) be accompanied by a call 1792400e64dfSOhad Ben-Cohen * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. 1793400e64dfSOhad Ben-Cohen * 1794400e64dfSOhad Ben-Cohen * Notes: 1795400e64dfSOhad Ben-Cohen * - we're not decrementing the rproc's refcount, only the power refcount. 1796400e64dfSOhad Ben-Cohen * which means that the @rproc handle stays valid even after rproc_shutdown() 1797400e64dfSOhad Ben-Cohen * returns, and users can still use it with a subsequent rproc_boot(), if 1798400e64dfSOhad Ben-Cohen * needed. 1799400e64dfSOhad Ben-Cohen */ 1800400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc) 1801400e64dfSOhad Ben-Cohen { 1802b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1803400e64dfSOhad Ben-Cohen int ret; 1804400e64dfSOhad Ben-Cohen 1805400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1806400e64dfSOhad Ben-Cohen if (ret) { 1807400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1808400e64dfSOhad Ben-Cohen return; 1809400e64dfSOhad Ben-Cohen } 1810400e64dfSOhad Ben-Cohen 1811400e64dfSOhad Ben-Cohen /* if the remote proc is still needed, bail out */ 1812400e64dfSOhad Ben-Cohen if (!atomic_dec_and_test(&rproc->power)) 1813400e64dfSOhad Ben-Cohen goto out; 1814400e64dfSOhad Ben-Cohen 1815fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, false); 1816400e64dfSOhad Ben-Cohen if (ret) { 1817400e64dfSOhad Ben-Cohen atomic_inc(&rproc->power); 1818400e64dfSOhad Ben-Cohen goto out; 1819400e64dfSOhad Ben-Cohen } 1820400e64dfSOhad Ben-Cohen 1821400e64dfSOhad Ben-Cohen /* clean up all acquired resources */ 1822400e64dfSOhad Ben-Cohen rproc_resource_cleanup(rproc); 1823400e64dfSOhad Ben-Cohen 1824400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1825400e64dfSOhad Ben-Cohen 1826988d204cSBjorn Andersson /* Free the copy of the resource table */ 1827a0c10687SBjorn Andersson kfree(rproc->cached_table); 1828a0c10687SBjorn Andersson rproc->cached_table = NULL; 1829988d204cSBjorn Andersson rproc->table_ptr = NULL; 1830400e64dfSOhad Ben-Cohen out: 1831400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1832400e64dfSOhad Ben-Cohen } 1833400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown); 1834400e64dfSOhad Ben-Cohen 1835400e64dfSOhad Ben-Cohen /** 1836fec47d86SDave Gerlach * rproc_get_by_phandle() - find a remote processor by phandle 1837fec47d86SDave Gerlach * @phandle: phandle to the rproc 1838fec47d86SDave Gerlach * 1839fec47d86SDave Gerlach * Finds an rproc handle using the remote processor's phandle, and then 1840fec47d86SDave Gerlach * return a handle to the rproc. 1841fec47d86SDave Gerlach * 1842fec47d86SDave Gerlach * This function increments the remote processor's refcount, so always 1843fec47d86SDave Gerlach * use rproc_put() to decrement it back once rproc isn't needed anymore. 1844fec47d86SDave Gerlach * 1845fec47d86SDave Gerlach * Returns the rproc handle on success, and NULL on failure. 1846fec47d86SDave Gerlach */ 18478de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF 1848fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle) 1849fec47d86SDave Gerlach { 1850fec47d86SDave Gerlach struct rproc *rproc = NULL, *r; 1851fec47d86SDave Gerlach struct device_node *np; 1852fec47d86SDave Gerlach 1853fec47d86SDave Gerlach np = of_find_node_by_phandle(phandle); 1854fec47d86SDave Gerlach if (!np) 1855fec47d86SDave Gerlach return NULL; 1856fec47d86SDave Gerlach 1857fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 1858fec47d86SDave Gerlach list_for_each_entry(r, &rproc_list, node) { 1859fec47d86SDave Gerlach if (r->dev.parent && r->dev.parent->of_node == np) { 1860fbb6aacbSBjorn Andersson /* prevent underlying implementation from being removed */ 1861fbb6aacbSBjorn Andersson if (!try_module_get(r->dev.parent->driver->owner)) { 1862fbb6aacbSBjorn Andersson dev_err(&r->dev, "can't get owner\n"); 1863fbb6aacbSBjorn Andersson break; 1864fbb6aacbSBjorn Andersson } 1865fbb6aacbSBjorn Andersson 1866fec47d86SDave Gerlach rproc = r; 1867fec47d86SDave Gerlach get_device(&rproc->dev); 1868fec47d86SDave Gerlach break; 1869fec47d86SDave Gerlach } 1870fec47d86SDave Gerlach } 1871fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 1872fec47d86SDave Gerlach 1873fec47d86SDave Gerlach of_node_put(np); 1874fec47d86SDave Gerlach 1875fec47d86SDave Gerlach return rproc; 1876fec47d86SDave Gerlach } 18778de3dbd0SOhad Ben-Cohen #else 18788de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle) 18798de3dbd0SOhad Ben-Cohen { 18808de3dbd0SOhad Ben-Cohen return NULL; 18818de3dbd0SOhad Ben-Cohen } 18828de3dbd0SOhad Ben-Cohen #endif 1883fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle); 1884fec47d86SDave Gerlach 1885fec47d86SDave Gerlach /** 1886160e7c84SOhad Ben-Cohen * rproc_add() - register a remote processor 1887400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle to register 1888400e64dfSOhad Ben-Cohen * 1889400e64dfSOhad Ben-Cohen * Registers @rproc with the remoteproc framework, after it has been 1890400e64dfSOhad Ben-Cohen * allocated with rproc_alloc(). 1891400e64dfSOhad Ben-Cohen * 1892400e64dfSOhad Ben-Cohen * This is called by the platform-specific rproc implementation, whenever 1893400e64dfSOhad Ben-Cohen * a new remote processor device is probed. 1894400e64dfSOhad Ben-Cohen * 1895400e64dfSOhad Ben-Cohen * Returns 0 on success and an appropriate error code otherwise. 1896400e64dfSOhad Ben-Cohen * 1897400e64dfSOhad Ben-Cohen * Note: this function initiates an asynchronous firmware loading 1898400e64dfSOhad Ben-Cohen * context, which will look for virtio devices supported by the rproc's 1899400e64dfSOhad Ben-Cohen * firmware. 1900400e64dfSOhad Ben-Cohen * 1901400e64dfSOhad Ben-Cohen * If found, those virtio devices will be created and added, so as a result 19027a186941SOhad Ben-Cohen * of registering this remote processor, additional virtio drivers might be 1903400e64dfSOhad Ben-Cohen * probed. 1904400e64dfSOhad Ben-Cohen */ 1905160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc) 1906400e64dfSOhad Ben-Cohen { 1907b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 190870b85ef8SFernando Guzman Lugo int ret; 1909400e64dfSOhad Ben-Cohen 1910b5ab5e24SOhad Ben-Cohen ret = device_add(dev); 1911b5ab5e24SOhad Ben-Cohen if (ret < 0) 1912b5ab5e24SOhad Ben-Cohen return ret; 1913400e64dfSOhad Ben-Cohen 1914b5ab5e24SOhad Ben-Cohen dev_info(dev, "%s is available\n", rproc->name); 1915400e64dfSOhad Ben-Cohen 1916400e64dfSOhad Ben-Cohen /* create debugfs entries */ 1917400e64dfSOhad Ben-Cohen rproc_create_debug_dir(rproc); 19187a20c64dSSarangdhar Joshi 19197a20c64dSSarangdhar Joshi /* if rproc is marked always-on, request it to boot */ 19207a20c64dSSarangdhar Joshi if (rproc->auto_boot) { 19215e6533f7SSarangdhar Joshi ret = rproc_trigger_auto_boot(rproc); 1922d2e12e66SDave Gerlach if (ret < 0) 1923d2e12e66SDave Gerlach return ret; 19247a20c64dSSarangdhar Joshi } 1925400e64dfSOhad Ben-Cohen 1926d2e12e66SDave Gerlach /* expose to rproc_get_by_phandle users */ 1927d2e12e66SDave Gerlach mutex_lock(&rproc_list_mutex); 1928d2e12e66SDave Gerlach list_add(&rproc->node, &rproc_list); 1929d2e12e66SDave Gerlach mutex_unlock(&rproc_list_mutex); 1930d2e12e66SDave Gerlach 1931d2e12e66SDave Gerlach return 0; 1932400e64dfSOhad Ben-Cohen } 1933160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add); 1934400e64dfSOhad Ben-Cohen 1935400e64dfSOhad Ben-Cohen /** 1936b5ab5e24SOhad Ben-Cohen * rproc_type_release() - release a remote processor instance 1937b5ab5e24SOhad Ben-Cohen * @dev: the rproc's device 1938b5ab5e24SOhad Ben-Cohen * 1939b5ab5e24SOhad Ben-Cohen * This function should _never_ be called directly. 1940b5ab5e24SOhad Ben-Cohen * 1941b5ab5e24SOhad Ben-Cohen * It will be called by the driver core when no one holds a valid pointer 1942b5ab5e24SOhad Ben-Cohen * to @dev anymore. 1943b5ab5e24SOhad Ben-Cohen */ 1944b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev) 1945b5ab5e24SOhad Ben-Cohen { 1946b5ab5e24SOhad Ben-Cohen struct rproc *rproc = container_of(dev, struct rproc, dev); 1947b5ab5e24SOhad Ben-Cohen 19487183a2a7SOhad Ben-Cohen dev_info(&rproc->dev, "releasing %s\n", rproc->name); 19497183a2a7SOhad Ben-Cohen 1950b5ab5e24SOhad Ben-Cohen idr_destroy(&rproc->notifyids); 1951b5ab5e24SOhad Ben-Cohen 1952b5ab5e24SOhad Ben-Cohen if (rproc->index >= 0) 1953b5ab5e24SOhad Ben-Cohen ida_simple_remove(&rproc_dev_index, rproc->index); 1954b5ab5e24SOhad Ben-Cohen 19550f57dc6aSMatt Redfearn kfree(rproc->firmware); 1956fb98e2bdSBjorn Andersson kfree(rproc->ops); 1957b5ab5e24SOhad Ben-Cohen kfree(rproc); 1958b5ab5e24SOhad Ben-Cohen } 1959b5ab5e24SOhad Ben-Cohen 1960c42ca04dSBhumika Goyal static const struct device_type rproc_type = { 1961b5ab5e24SOhad Ben-Cohen .name = "remoteproc", 1962b5ab5e24SOhad Ben-Cohen .release = rproc_type_release, 1963b5ab5e24SOhad Ben-Cohen }; 1964400e64dfSOhad Ben-Cohen 1965400e64dfSOhad Ben-Cohen /** 1966400e64dfSOhad Ben-Cohen * rproc_alloc() - allocate a remote processor handle 1967400e64dfSOhad Ben-Cohen * @dev: the underlying device 1968400e64dfSOhad Ben-Cohen * @name: name of this remote processor 1969400e64dfSOhad Ben-Cohen * @ops: platform-specific handlers (mainly start/stop) 19708b4aec9aSRobert Tivy * @firmware: name of firmware file to load, can be NULL 1971400e64dfSOhad Ben-Cohen * @len: length of private data needed by the rproc driver (in bytes) 1972400e64dfSOhad Ben-Cohen * 1973400e64dfSOhad Ben-Cohen * Allocates a new remote processor handle, but does not register 19748b4aec9aSRobert Tivy * it yet. if @firmware is NULL, a default name is used. 1975400e64dfSOhad Ben-Cohen * 1976400e64dfSOhad Ben-Cohen * This function should be used by rproc implementations during initialization 1977400e64dfSOhad Ben-Cohen * of the remote processor. 1978400e64dfSOhad Ben-Cohen * 1979400e64dfSOhad Ben-Cohen * After creating an rproc handle using this function, and when ready, 1980160e7c84SOhad Ben-Cohen * implementations should then call rproc_add() to complete 1981400e64dfSOhad Ben-Cohen * the registration of the remote processor. 1982400e64dfSOhad Ben-Cohen * 1983400e64dfSOhad Ben-Cohen * On success the new rproc is returned, and on failure, NULL. 1984400e64dfSOhad Ben-Cohen * 1985400e64dfSOhad Ben-Cohen * Note: _never_ directly deallocate @rproc, even if it was not registered 1986433c0e04SBjorn Andersson * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). 1987400e64dfSOhad Ben-Cohen */ 1988400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name, 1989400e64dfSOhad Ben-Cohen const struct rproc_ops *ops, 1990400e64dfSOhad Ben-Cohen const char *firmware, int len) 1991400e64dfSOhad Ben-Cohen { 1992400e64dfSOhad Ben-Cohen struct rproc *rproc; 19938b4aec9aSRobert Tivy char *p, *template = "rproc-%s-fw"; 19940f57dc6aSMatt Redfearn int name_len; 1995400e64dfSOhad Ben-Cohen 1996400e64dfSOhad Ben-Cohen if (!dev || !name || !ops) 1997400e64dfSOhad Ben-Cohen return NULL; 1998400e64dfSOhad Ben-Cohen 19990f57dc6aSMatt Redfearn if (!firmware) { 20008b4aec9aSRobert Tivy /* 20018b4aec9aSRobert Tivy * If the caller didn't pass in a firmware name then 20020f57dc6aSMatt Redfearn * construct a default name. 20038b4aec9aSRobert Tivy */ 20048b4aec9aSRobert Tivy name_len = strlen(name) + strlen(template) - 2 + 1; 20050f57dc6aSMatt Redfearn p = kmalloc(name_len, GFP_KERNEL); 20060f57dc6aSMatt Redfearn if (!p) 2007400e64dfSOhad Ben-Cohen return NULL; 20088b4aec9aSRobert Tivy snprintf(p, name_len, template, name); 20098b4aec9aSRobert Tivy } else { 20100f57dc6aSMatt Redfearn p = kstrdup(firmware, GFP_KERNEL); 20110f57dc6aSMatt Redfearn if (!p) 20120f57dc6aSMatt Redfearn return NULL; 20130f57dc6aSMatt Redfearn } 20140f57dc6aSMatt Redfearn 20150f57dc6aSMatt Redfearn rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); 20160f57dc6aSMatt Redfearn if (!rproc) { 20170f57dc6aSMatt Redfearn kfree(p); 20180f57dc6aSMatt Redfearn return NULL; 20198b4aec9aSRobert Tivy } 20208b4aec9aSRobert Tivy 2021fb98e2bdSBjorn Andersson rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); 2022fb98e2bdSBjorn Andersson if (!rproc->ops) { 2023fb98e2bdSBjorn Andersson kfree(p); 2024fb98e2bdSBjorn Andersson kfree(rproc); 2025fb98e2bdSBjorn Andersson return NULL; 2026fb98e2bdSBjorn Andersson } 2027fb98e2bdSBjorn Andersson 20288b4aec9aSRobert Tivy rproc->firmware = p; 2029400e64dfSOhad Ben-Cohen rproc->name = name; 2030400e64dfSOhad Ben-Cohen rproc->priv = &rproc[1]; 2031ddf71187SBjorn Andersson rproc->auto_boot = true; 2032400e64dfSOhad Ben-Cohen 2033b5ab5e24SOhad Ben-Cohen device_initialize(&rproc->dev); 2034b5ab5e24SOhad Ben-Cohen rproc->dev.parent = dev; 2035b5ab5e24SOhad Ben-Cohen rproc->dev.type = &rproc_type; 20362aefbef0SMatt Redfearn rproc->dev.class = &rproc_class; 20377c89717fSBjorn Andersson rproc->dev.driver_data = rproc; 2038b5ab5e24SOhad Ben-Cohen 2039b5ab5e24SOhad Ben-Cohen /* Assign a unique device index and name */ 2040b5ab5e24SOhad Ben-Cohen rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL); 2041b5ab5e24SOhad Ben-Cohen if (rproc->index < 0) { 2042b5ab5e24SOhad Ben-Cohen dev_err(dev, "ida_simple_get failed: %d\n", rproc->index); 2043b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 2044b5ab5e24SOhad Ben-Cohen return NULL; 2045b5ab5e24SOhad Ben-Cohen } 2046b5ab5e24SOhad Ben-Cohen 2047b5ab5e24SOhad Ben-Cohen dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); 2048b5ab5e24SOhad Ben-Cohen 2049400e64dfSOhad Ben-Cohen atomic_set(&rproc->power, 0); 2050400e64dfSOhad Ben-Cohen 20510f21f9ccSBjorn Andersson /* Default to ELF loader if no load function is specified */ 20520f21f9ccSBjorn Andersson if (!rproc->ops->load) { 20530f21f9ccSBjorn Andersson rproc->ops->load = rproc_elf_load_segments; 2054c1d35c1aSBjorn Andersson rproc->ops->parse_fw = rproc_elf_load_rsc_table; 20550f21f9ccSBjorn Andersson rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; 20560f21f9ccSBjorn Andersson rproc->ops->sanity_check = rproc_elf_sanity_check; 20570f21f9ccSBjorn Andersson rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; 20580f21f9ccSBjorn Andersson } 2059400e64dfSOhad Ben-Cohen 2060400e64dfSOhad Ben-Cohen mutex_init(&rproc->lock); 2061400e64dfSOhad Ben-Cohen 20627a186941SOhad Ben-Cohen idr_init(&rproc->notifyids); 20637a186941SOhad Ben-Cohen 2064400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->carveouts); 2065400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->mappings); 2066400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->traces); 20677a186941SOhad Ben-Cohen INIT_LIST_HEAD(&rproc->rvdevs); 20687bdc9650SBjorn Andersson INIT_LIST_HEAD(&rproc->subdevs); 20692666ca91SSarangdhar Joshi INIT_LIST_HEAD(&rproc->dump_segments); 2070400e64dfSOhad Ben-Cohen 20718afd519cSFernando Guzman Lugo INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); 20728afd519cSFernando Guzman Lugo 2073400e64dfSOhad Ben-Cohen rproc->state = RPROC_OFFLINE; 2074400e64dfSOhad Ben-Cohen 2075400e64dfSOhad Ben-Cohen return rproc; 2076400e64dfSOhad Ben-Cohen } 2077400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc); 2078400e64dfSOhad Ben-Cohen 2079400e64dfSOhad Ben-Cohen /** 2080433c0e04SBjorn Andersson * rproc_free() - unroll rproc_alloc() 2081433c0e04SBjorn Andersson * @rproc: the remote processor handle 2082433c0e04SBjorn Andersson * 2083433c0e04SBjorn Andersson * This function decrements the rproc dev refcount. 2084433c0e04SBjorn Andersson * 2085433c0e04SBjorn Andersson * If no one holds any reference to rproc anymore, then its refcount would 2086433c0e04SBjorn Andersson * now drop to zero, and it would be freed. 2087433c0e04SBjorn Andersson */ 2088433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc) 2089433c0e04SBjorn Andersson { 2090433c0e04SBjorn Andersson put_device(&rproc->dev); 2091433c0e04SBjorn Andersson } 2092433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free); 2093433c0e04SBjorn Andersson 2094433c0e04SBjorn Andersson /** 2095433c0e04SBjorn Andersson * rproc_put() - release rproc reference 2096400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle 2097400e64dfSOhad Ben-Cohen * 2098c6b5a276SOhad Ben-Cohen * This function decrements the rproc dev refcount. 2099400e64dfSOhad Ben-Cohen * 2100c6b5a276SOhad Ben-Cohen * If no one holds any reference to rproc anymore, then its refcount would 2101c6b5a276SOhad Ben-Cohen * now drop to zero, and it would be freed. 2102400e64dfSOhad Ben-Cohen */ 2103160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc) 2104400e64dfSOhad Ben-Cohen { 2105fbb6aacbSBjorn Andersson module_put(rproc->dev.parent->driver->owner); 2106b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 2107400e64dfSOhad Ben-Cohen } 2108160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put); 2109400e64dfSOhad Ben-Cohen 2110400e64dfSOhad Ben-Cohen /** 2111160e7c84SOhad Ben-Cohen * rproc_del() - unregister a remote processor 2112400e64dfSOhad Ben-Cohen * @rproc: rproc handle to unregister 2113400e64dfSOhad Ben-Cohen * 2114400e64dfSOhad Ben-Cohen * This function should be called when the platform specific rproc 2115400e64dfSOhad Ben-Cohen * implementation decides to remove the rproc device. it should 2116160e7c84SOhad Ben-Cohen * _only_ be called if a previous invocation of rproc_add() 2117400e64dfSOhad Ben-Cohen * has completed successfully. 2118400e64dfSOhad Ben-Cohen * 2119160e7c84SOhad Ben-Cohen * After rproc_del() returns, @rproc isn't freed yet, because 2120c6b5a276SOhad Ben-Cohen * of the outstanding reference created by rproc_alloc. To decrement that 2121433c0e04SBjorn Andersson * one last refcount, one still needs to call rproc_free(). 2122400e64dfSOhad Ben-Cohen * 2123400e64dfSOhad Ben-Cohen * Returns 0 on success and -EINVAL if @rproc isn't valid. 2124400e64dfSOhad Ben-Cohen */ 2125160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc) 2126400e64dfSOhad Ben-Cohen { 2127400e64dfSOhad Ben-Cohen if (!rproc) 2128400e64dfSOhad Ben-Cohen return -EINVAL; 2129400e64dfSOhad Ben-Cohen 2130ddf71187SBjorn Andersson /* if rproc is marked always-on, rproc_add() booted it */ 2131ddf71187SBjorn Andersson /* TODO: make sure this works with rproc->power > 1 */ 2132ddf71187SBjorn Andersson if (rproc->auto_boot) 2133ddf71187SBjorn Andersson rproc_shutdown(rproc); 2134ddf71187SBjorn Andersson 21352099c77dSSarangdhar Joshi mutex_lock(&rproc->lock); 21362099c77dSSarangdhar Joshi rproc->state = RPROC_DELETED; 21372099c77dSSarangdhar Joshi mutex_unlock(&rproc->lock); 21382099c77dSSarangdhar Joshi 2139b003d45bSSarangdhar Joshi rproc_delete_debug_dir(rproc); 2140b003d45bSSarangdhar Joshi 2141fec47d86SDave Gerlach /* the rproc is downref'ed as soon as it's removed from the klist */ 2142fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 2143fec47d86SDave Gerlach list_del(&rproc->node); 2144fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 2145fec47d86SDave Gerlach 2146b5ab5e24SOhad Ben-Cohen device_del(&rproc->dev); 2147400e64dfSOhad Ben-Cohen 2148400e64dfSOhad Ben-Cohen return 0; 2149400e64dfSOhad Ben-Cohen } 2150160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del); 2151400e64dfSOhad Ben-Cohen 21528afd519cSFernando Guzman Lugo /** 21537bdc9650SBjorn Andersson * rproc_add_subdev() - add a subdevice to a remoteproc 21547bdc9650SBjorn Andersson * @rproc: rproc handle to add the subdevice to 21557bdc9650SBjorn Andersson * @subdev: subdev handle to register 21564902676fSBjorn Andersson * 21574902676fSBjorn Andersson * Caller is responsible for populating optional subdevice function pointers. 21587bdc9650SBjorn Andersson */ 21594902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 21607bdc9650SBjorn Andersson { 21617bdc9650SBjorn Andersson list_add_tail(&subdev->node, &rproc->subdevs); 21627bdc9650SBjorn Andersson } 21637bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev); 21647bdc9650SBjorn Andersson 21657bdc9650SBjorn Andersson /** 21667bdc9650SBjorn Andersson * rproc_remove_subdev() - remove a subdevice from a remoteproc 21677bdc9650SBjorn Andersson * @rproc: rproc handle to remove the subdevice from 21687bdc9650SBjorn Andersson * @subdev: subdev handle, previously registered with rproc_add_subdev() 21697bdc9650SBjorn Andersson */ 21707bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 21717bdc9650SBjorn Andersson { 21727bdc9650SBjorn Andersson list_del(&subdev->node); 21737bdc9650SBjorn Andersson } 21747bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev); 21757bdc9650SBjorn Andersson 21767bdc9650SBjorn Andersson /** 21777c89717fSBjorn Andersson * rproc_get_by_child() - acquire rproc handle of @dev's ancestor 21787c89717fSBjorn Andersson * @dev: child device to find ancestor of 21797c89717fSBjorn Andersson * 21807c89717fSBjorn Andersson * Returns the ancestor rproc instance, or NULL if not found. 21817c89717fSBjorn Andersson */ 21827c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev) 21837c89717fSBjorn Andersson { 21847c89717fSBjorn Andersson for (dev = dev->parent; dev; dev = dev->parent) { 21857c89717fSBjorn Andersson if (dev->type == &rproc_type) 21867c89717fSBjorn Andersson return dev->driver_data; 21877c89717fSBjorn Andersson } 21887c89717fSBjorn Andersson 21897c89717fSBjorn Andersson return NULL; 21907c89717fSBjorn Andersson } 21917c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child); 21927c89717fSBjorn Andersson 21937c89717fSBjorn Andersson /** 21948afd519cSFernando Guzman Lugo * rproc_report_crash() - rproc crash reporter function 21958afd519cSFernando Guzman Lugo * @rproc: remote processor 21968afd519cSFernando Guzman Lugo * @type: crash type 21978afd519cSFernando Guzman Lugo * 21988afd519cSFernando Guzman Lugo * This function must be called every time a crash is detected by the low-level 21998afd519cSFernando Guzman Lugo * drivers implementing a specific remoteproc. This should not be called from a 22008afd519cSFernando Guzman Lugo * non-remoteproc driver. 22018afd519cSFernando Guzman Lugo * 22028afd519cSFernando Guzman Lugo * This function can be called from atomic/interrupt context. 22038afd519cSFernando Guzman Lugo */ 22048afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) 22058afd519cSFernando Guzman Lugo { 22068afd519cSFernando Guzman Lugo if (!rproc) { 22078afd519cSFernando Guzman Lugo pr_err("NULL rproc pointer\n"); 22088afd519cSFernando Guzman Lugo return; 22098afd519cSFernando Guzman Lugo } 22108afd519cSFernando Guzman Lugo 22118afd519cSFernando Guzman Lugo dev_err(&rproc->dev, "crash detected in %s: type %s\n", 22128afd519cSFernando Guzman Lugo rproc->name, rproc_crash_to_string(type)); 22138afd519cSFernando Guzman Lugo 22148afd519cSFernando Guzman Lugo /* create a new task to handle the error */ 22158afd519cSFernando Guzman Lugo schedule_work(&rproc->crash_handler); 22168afd519cSFernando Guzman Lugo } 22178afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash); 22188afd519cSFernando Guzman Lugo 2219400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void) 2220400e64dfSOhad Ben-Cohen { 22212aefbef0SMatt Redfearn rproc_init_sysfs(); 2222400e64dfSOhad Ben-Cohen rproc_init_debugfs(); 2223b5ab5e24SOhad Ben-Cohen 2224400e64dfSOhad Ben-Cohen return 0; 2225400e64dfSOhad Ben-Cohen } 2226400e64dfSOhad Ben-Cohen module_init(remoteproc_init); 2227400e64dfSOhad Ben-Cohen 2228400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void) 2229400e64dfSOhad Ben-Cohen { 2230f42f79afSSuman Anna ida_destroy(&rproc_dev_index); 2231f42f79afSSuman Anna 2232400e64dfSOhad Ben-Cohen rproc_exit_debugfs(); 22332aefbef0SMatt Redfearn rproc_exit_sysfs(); 2234400e64dfSOhad Ben-Cohen } 2235400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit); 2236400e64dfSOhad Ben-Cohen 2237400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2"); 2238400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework"); 2239