1400e64dfSOhad Ben-Cohen /* 2400e64dfSOhad Ben-Cohen * Remote Processor Framework 3400e64dfSOhad Ben-Cohen * 4400e64dfSOhad Ben-Cohen * Copyright (C) 2011 Texas Instruments, Inc. 5400e64dfSOhad Ben-Cohen * Copyright (C) 2011 Google, Inc. 6400e64dfSOhad Ben-Cohen * 7400e64dfSOhad Ben-Cohen * Ohad Ben-Cohen <ohad@wizery.com> 8400e64dfSOhad Ben-Cohen * Brian Swetland <swetland@google.com> 9400e64dfSOhad Ben-Cohen * Mark Grosen <mgrosen@ti.com> 10400e64dfSOhad Ben-Cohen * Fernando Guzman Lugo <fernando.lugo@ti.com> 11400e64dfSOhad Ben-Cohen * Suman Anna <s-anna@ti.com> 12400e64dfSOhad Ben-Cohen * Robert Tivy <rtivy@ti.com> 13400e64dfSOhad Ben-Cohen * Armando Uribe De Leon <x0095078@ti.com> 14400e64dfSOhad Ben-Cohen * 15400e64dfSOhad Ben-Cohen * This program is free software; you can redistribute it and/or 16400e64dfSOhad Ben-Cohen * modify it under the terms of the GNU General Public License 17400e64dfSOhad Ben-Cohen * version 2 as published by the Free Software Foundation. 18400e64dfSOhad Ben-Cohen * 19400e64dfSOhad Ben-Cohen * This program is distributed in the hope that it will be useful, 20400e64dfSOhad Ben-Cohen * but WITHOUT ANY WARRANTY; without even the implied warranty of 21400e64dfSOhad Ben-Cohen * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22400e64dfSOhad Ben-Cohen * GNU General Public License for more details. 23400e64dfSOhad Ben-Cohen */ 24400e64dfSOhad Ben-Cohen 25400e64dfSOhad Ben-Cohen #define pr_fmt(fmt) "%s: " fmt, __func__ 26400e64dfSOhad Ben-Cohen 27400e64dfSOhad Ben-Cohen #include <linux/kernel.h> 28400e64dfSOhad Ben-Cohen #include <linux/module.h> 29400e64dfSOhad Ben-Cohen #include <linux/device.h> 30400e64dfSOhad Ben-Cohen #include <linux/slab.h> 31400e64dfSOhad Ben-Cohen #include <linux/mutex.h> 32400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h> 33400e64dfSOhad Ben-Cohen #include <linux/firmware.h> 34400e64dfSOhad Ben-Cohen #include <linux/string.h> 35400e64dfSOhad Ben-Cohen #include <linux/debugfs.h> 362666ca91SSarangdhar Joshi #include <linux/devcoredump.h> 37400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h> 38400e64dfSOhad Ben-Cohen #include <linux/iommu.h> 39b5ab5e24SOhad Ben-Cohen #include <linux/idr.h> 40400e64dfSOhad Ben-Cohen #include <linux/elf.h> 41a2b950acSOhad Ben-Cohen #include <linux/crc32.h> 42*086d0872SLoic Pallardy #include <linux/of_reserved_mem.h> 43400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h> 44400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h> 45cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h> 46*086d0872SLoic Pallardy #include <linux/platform_device.h> 47400e64dfSOhad Ben-Cohen 48400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h" 49400e64dfSOhad Ben-Cohen 50fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex); 51fec47d86SDave Gerlach static LIST_HEAD(rproc_list); 52fec47d86SDave Gerlach 53400e64dfSOhad Ben-Cohen typedef int (*rproc_handle_resources_t)(struct rproc *rproc, 54fd2c15ecSOhad Ben-Cohen struct resource_table *table, int len); 55a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc, 56a2b950acSOhad Ben-Cohen void *, int offset, int avail); 57400e64dfSOhad Ben-Cohen 58c6aed238SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc, 59c6aed238SLoic Pallardy struct rproc_mem_entry *mem); 60c6aed238SLoic Pallardy static int rproc_release_carveout(struct rproc *rproc, 61c6aed238SLoic Pallardy struct rproc_mem_entry *mem); 62c6aed238SLoic Pallardy 63b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */ 64b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index); 65b5ab5e24SOhad Ben-Cohen 668afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = { 678afd519cSFernando Guzman Lugo [RPROC_MMUFAULT] = "mmufault", 68b3d39032SBjorn Andersson [RPROC_WATCHDOG] = "watchdog", 69b3d39032SBjorn Andersson [RPROC_FATAL_ERROR] = "fatal error", 708afd519cSFernando Guzman Lugo }; 718afd519cSFernando Guzman Lugo 728afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */ 738afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type) 748afd519cSFernando Guzman Lugo { 758afd519cSFernando Guzman Lugo if (type < ARRAY_SIZE(rproc_crash_names)) 768afd519cSFernando Guzman Lugo return rproc_crash_names[type]; 77b23f7a09SMasanari Iida return "unknown"; 788afd519cSFernando Guzman Lugo } 798afd519cSFernando Guzman Lugo 80400e64dfSOhad Ben-Cohen /* 81400e64dfSOhad Ben-Cohen * This is the IOMMU fault handler we register with the IOMMU API 82400e64dfSOhad Ben-Cohen * (when relevant; not all remote processors access memory through 83400e64dfSOhad Ben-Cohen * an IOMMU). 84400e64dfSOhad Ben-Cohen * 85400e64dfSOhad Ben-Cohen * IOMMU core will invoke this handler whenever the remote processor 86400e64dfSOhad Ben-Cohen * will try to access an unmapped device address. 87400e64dfSOhad Ben-Cohen */ 88400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev, 8977ca2332SOhad Ben-Cohen unsigned long iova, int flags, void *token) 90400e64dfSOhad Ben-Cohen { 918afd519cSFernando Guzman Lugo struct rproc *rproc = token; 928afd519cSFernando Guzman Lugo 93400e64dfSOhad Ben-Cohen dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags); 94400e64dfSOhad Ben-Cohen 958afd519cSFernando Guzman Lugo rproc_report_crash(rproc, RPROC_MMUFAULT); 968afd519cSFernando Guzman Lugo 97400e64dfSOhad Ben-Cohen /* 98400e64dfSOhad Ben-Cohen * Let the iommu core know we're not really handling this fault; 998afd519cSFernando Guzman Lugo * we just used it as a recovery trigger. 100400e64dfSOhad Ben-Cohen */ 101400e64dfSOhad Ben-Cohen return -ENOSYS; 102400e64dfSOhad Ben-Cohen } 103400e64dfSOhad Ben-Cohen 104400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc) 105400e64dfSOhad Ben-Cohen { 106400e64dfSOhad Ben-Cohen struct iommu_domain *domain; 107b5ab5e24SOhad Ben-Cohen struct device *dev = rproc->dev.parent; 108400e64dfSOhad Ben-Cohen int ret; 109400e64dfSOhad Ben-Cohen 110315491e5SSuman Anna if (!rproc->has_iommu) { 111315491e5SSuman Anna dev_dbg(dev, "iommu not present\n"); 1120798e1daSMark Grosen return 0; 113400e64dfSOhad Ben-Cohen } 114400e64dfSOhad Ben-Cohen 115400e64dfSOhad Ben-Cohen domain = iommu_domain_alloc(dev->bus); 116400e64dfSOhad Ben-Cohen if (!domain) { 117400e64dfSOhad Ben-Cohen dev_err(dev, "can't alloc iommu domain\n"); 118400e64dfSOhad Ben-Cohen return -ENOMEM; 119400e64dfSOhad Ben-Cohen } 120400e64dfSOhad Ben-Cohen 12177ca2332SOhad Ben-Cohen iommu_set_fault_handler(domain, rproc_iommu_fault, rproc); 122400e64dfSOhad Ben-Cohen 123400e64dfSOhad Ben-Cohen ret = iommu_attach_device(domain, dev); 124400e64dfSOhad Ben-Cohen if (ret) { 125400e64dfSOhad Ben-Cohen dev_err(dev, "can't attach iommu device: %d\n", ret); 126400e64dfSOhad Ben-Cohen goto free_domain; 127400e64dfSOhad Ben-Cohen } 128400e64dfSOhad Ben-Cohen 129400e64dfSOhad Ben-Cohen rproc->domain = domain; 130400e64dfSOhad Ben-Cohen 131400e64dfSOhad Ben-Cohen return 0; 132400e64dfSOhad Ben-Cohen 133400e64dfSOhad Ben-Cohen free_domain: 134400e64dfSOhad Ben-Cohen iommu_domain_free(domain); 135400e64dfSOhad Ben-Cohen return ret; 136400e64dfSOhad Ben-Cohen } 137400e64dfSOhad Ben-Cohen 138400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc) 139400e64dfSOhad Ben-Cohen { 140400e64dfSOhad Ben-Cohen struct iommu_domain *domain = rproc->domain; 141b5ab5e24SOhad Ben-Cohen struct device *dev = rproc->dev.parent; 142400e64dfSOhad Ben-Cohen 143400e64dfSOhad Ben-Cohen if (!domain) 144400e64dfSOhad Ben-Cohen return; 145400e64dfSOhad Ben-Cohen 146400e64dfSOhad Ben-Cohen iommu_detach_device(domain, dev); 147400e64dfSOhad Ben-Cohen iommu_domain_free(domain); 148400e64dfSOhad Ben-Cohen } 149400e64dfSOhad Ben-Cohen 150*086d0872SLoic Pallardy phys_addr_t rproc_va_to_pa(void *cpu_addr) 151eb30596eSLoic Pallardy { 152eb30596eSLoic Pallardy /* 153eb30596eSLoic Pallardy * Return physical address according to virtual address location 154eb30596eSLoic Pallardy * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent 155eb30596eSLoic Pallardy * - in kernel: if region allocated in generic dma memory pool 156eb30596eSLoic Pallardy */ 157eb30596eSLoic Pallardy if (is_vmalloc_addr(cpu_addr)) { 158eb30596eSLoic Pallardy return page_to_phys(vmalloc_to_page(cpu_addr)) + 159eb30596eSLoic Pallardy offset_in_page(cpu_addr); 160eb30596eSLoic Pallardy } 161eb30596eSLoic Pallardy 162eb30596eSLoic Pallardy WARN_ON(!virt_addr_valid(cpu_addr)); 163eb30596eSLoic Pallardy return virt_to_phys(cpu_addr); 164eb30596eSLoic Pallardy } 165*086d0872SLoic Pallardy EXPORT_SYMBOL(rproc_va_to_pa); 166eb30596eSLoic Pallardy 167a01f7cd6SSuman Anna /** 168a01f7cd6SSuman Anna * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address 169a01f7cd6SSuman Anna * @rproc: handle of a remote processor 170a01f7cd6SSuman Anna * @da: remoteproc device address to translate 171a01f7cd6SSuman Anna * @len: length of the memory region @da is pointing to 172a01f7cd6SSuman Anna * 173400e64dfSOhad Ben-Cohen * Some remote processors will ask us to allocate them physically contiguous 174400e64dfSOhad Ben-Cohen * memory regions (which we call "carveouts"), and map them to specific 175a01f7cd6SSuman Anna * device addresses (which are hardcoded in the firmware). They may also have 176a01f7cd6SSuman Anna * dedicated memory regions internal to the processors, and use them either 177a01f7cd6SSuman Anna * exclusively or alongside carveouts. 178400e64dfSOhad Ben-Cohen * 179400e64dfSOhad Ben-Cohen * They may then ask us to copy objects into specific device addresses (e.g. 180400e64dfSOhad Ben-Cohen * code/data sections) or expose us certain symbols in other device address 181400e64dfSOhad Ben-Cohen * (e.g. their trace buffer). 182400e64dfSOhad Ben-Cohen * 183a01f7cd6SSuman Anna * This function is a helper function with which we can go over the allocated 184a01f7cd6SSuman Anna * carveouts and translate specific device addresses to kernel virtual addresses 185a01f7cd6SSuman Anna * so we can access the referenced memory. This function also allows to perform 186a01f7cd6SSuman Anna * translations on the internal remoteproc memory regions through a platform 187a01f7cd6SSuman Anna * implementation specific da_to_va ops, if present. 188a01f7cd6SSuman Anna * 189a01f7cd6SSuman Anna * The function returns a valid kernel address on success or NULL on failure. 190400e64dfSOhad Ben-Cohen * 191400e64dfSOhad Ben-Cohen * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too, 192400e64dfSOhad Ben-Cohen * but only on kernel direct mapped RAM memory. Instead, we're just using 193a01f7cd6SSuman Anna * here the output of the DMA API for the carveouts, which should be more 194a01f7cd6SSuman Anna * correct. 195400e64dfSOhad Ben-Cohen */ 19672854fb0SSjur Brændeland void *rproc_da_to_va(struct rproc *rproc, u64 da, int len) 197400e64dfSOhad Ben-Cohen { 198400e64dfSOhad Ben-Cohen struct rproc_mem_entry *carveout; 199400e64dfSOhad Ben-Cohen void *ptr = NULL; 200400e64dfSOhad Ben-Cohen 201a01f7cd6SSuman Anna if (rproc->ops->da_to_va) { 202a01f7cd6SSuman Anna ptr = rproc->ops->da_to_va(rproc, da, len); 203a01f7cd6SSuman Anna if (ptr) 204a01f7cd6SSuman Anna goto out; 205a01f7cd6SSuman Anna } 206a01f7cd6SSuman Anna 207400e64dfSOhad Ben-Cohen list_for_each_entry(carveout, &rproc->carveouts, node) { 208400e64dfSOhad Ben-Cohen int offset = da - carveout->da; 209400e64dfSOhad Ben-Cohen 210400e64dfSOhad Ben-Cohen /* try next carveout if da is too small */ 211400e64dfSOhad Ben-Cohen if (offset < 0) 212400e64dfSOhad Ben-Cohen continue; 213400e64dfSOhad Ben-Cohen 214400e64dfSOhad Ben-Cohen /* try next carveout if da is too large */ 215400e64dfSOhad Ben-Cohen if (offset + len > carveout->len) 216400e64dfSOhad Ben-Cohen continue; 217400e64dfSOhad Ben-Cohen 218400e64dfSOhad Ben-Cohen ptr = carveout->va + offset; 219400e64dfSOhad Ben-Cohen 220400e64dfSOhad Ben-Cohen break; 221400e64dfSOhad Ben-Cohen } 222400e64dfSOhad Ben-Cohen 223a01f7cd6SSuman Anna out: 224400e64dfSOhad Ben-Cohen return ptr; 225400e64dfSOhad Ben-Cohen } 2264afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va); 227400e64dfSOhad Ben-Cohen 228b0019ccdSLoic Pallardy /** 229b0019ccdSLoic Pallardy * rproc_find_carveout_by_name() - lookup the carveout region by a name 230b0019ccdSLoic Pallardy * @rproc: handle of a remote processor 231b0019ccdSLoic Pallardy * @name,..: carveout name to find (standard printf format) 232b0019ccdSLoic Pallardy * 233b0019ccdSLoic Pallardy * Platform driver has the capability to register some pre-allacoted carveout 234b0019ccdSLoic Pallardy * (physically contiguous memory regions) before rproc firmware loading and 235b0019ccdSLoic Pallardy * associated resource table analysis. These regions may be dedicated memory 236b0019ccdSLoic Pallardy * regions internal to the coprocessor or specified DDR region with specific 237b0019ccdSLoic Pallardy * attributes 238b0019ccdSLoic Pallardy * 239b0019ccdSLoic Pallardy * This function is a helper function with which we can go over the 240b0019ccdSLoic Pallardy * allocated carveouts and return associated region characteristics like 241b0019ccdSLoic Pallardy * coprocessor address, length or processor virtual address. 242b0019ccdSLoic Pallardy * 243b0019ccdSLoic Pallardy * Return: a valid pointer on carveout entry on success or NULL on failure. 244b0019ccdSLoic Pallardy */ 245b0019ccdSLoic Pallardy struct rproc_mem_entry * 246b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...) 247b0019ccdSLoic Pallardy { 248b0019ccdSLoic Pallardy va_list args; 249b0019ccdSLoic Pallardy char _name[32]; 250b0019ccdSLoic Pallardy struct rproc_mem_entry *carveout, *mem = NULL; 251b0019ccdSLoic Pallardy 252b0019ccdSLoic Pallardy if (!name) 253b0019ccdSLoic Pallardy return NULL; 254b0019ccdSLoic Pallardy 255b0019ccdSLoic Pallardy va_start(args, name); 256b0019ccdSLoic Pallardy vsnprintf(_name, sizeof(_name), name, args); 257b0019ccdSLoic Pallardy va_end(args); 258b0019ccdSLoic Pallardy 259b0019ccdSLoic Pallardy list_for_each_entry(carveout, &rproc->carveouts, node) { 260b0019ccdSLoic Pallardy /* Compare carveout and requested names */ 261b0019ccdSLoic Pallardy if (!strcmp(carveout->name, _name)) { 262b0019ccdSLoic Pallardy mem = carveout; 263b0019ccdSLoic Pallardy break; 264b0019ccdSLoic Pallardy } 265b0019ccdSLoic Pallardy } 266b0019ccdSLoic Pallardy 267b0019ccdSLoic Pallardy return mem; 268b0019ccdSLoic Pallardy } 269b0019ccdSLoic Pallardy 270c874bf59SLoic Pallardy /** 271c874bf59SLoic Pallardy * rproc_check_carveout_da() - Check specified carveout da configuration 272c874bf59SLoic Pallardy * @rproc: handle of a remote processor 273c874bf59SLoic Pallardy * @mem: pointer on carveout to check 274c874bf59SLoic Pallardy * @da: area device address 275c874bf59SLoic Pallardy * @len: associated area size 276c874bf59SLoic Pallardy * 277c874bf59SLoic Pallardy * This function is a helper function to verify requested device area (couple 278c874bf59SLoic Pallardy * da, len) is part of specified carevout. 279c874bf59SLoic Pallardy * 280c874bf59SLoic Pallardy * Return: 0 if carveout match request else -ENOMEM 281c874bf59SLoic Pallardy */ 282c874bf59SLoic Pallardy int rproc_check_carveout_da(struct rproc *rproc, struct rproc_mem_entry *mem, 283c874bf59SLoic Pallardy u32 da, u32 len) 284c874bf59SLoic Pallardy { 285c874bf59SLoic Pallardy struct device *dev = &rproc->dev; 286c874bf59SLoic Pallardy int delta = 0; 287c874bf59SLoic Pallardy 288c874bf59SLoic Pallardy /* Check requested resource length */ 289c874bf59SLoic Pallardy if (len > mem->len) { 290c874bf59SLoic Pallardy dev_err(dev, "Registered carveout doesn't fit len request\n"); 291c874bf59SLoic Pallardy return -ENOMEM; 292c874bf59SLoic Pallardy } 293c874bf59SLoic Pallardy 294c874bf59SLoic Pallardy if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) { 295c874bf59SLoic Pallardy /* Update existing carveout da */ 296c874bf59SLoic Pallardy mem->da = da; 297c874bf59SLoic Pallardy } else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) { 298c874bf59SLoic Pallardy delta = da - mem->da; 299c874bf59SLoic Pallardy 300c874bf59SLoic Pallardy /* Check requested resource belongs to registered carveout */ 301c874bf59SLoic Pallardy if (delta < 0) { 302c874bf59SLoic Pallardy dev_err(dev, 303c874bf59SLoic Pallardy "Registered carveout doesn't fit da request\n"); 304c874bf59SLoic Pallardy return -ENOMEM; 305c874bf59SLoic Pallardy } 306c874bf59SLoic Pallardy 307c874bf59SLoic Pallardy if (delta + len > mem->len) { 308c874bf59SLoic Pallardy dev_err(dev, 309c874bf59SLoic Pallardy "Registered carveout doesn't fit len request\n"); 310c874bf59SLoic Pallardy return -ENOMEM; 311c874bf59SLoic Pallardy } 312c874bf59SLoic Pallardy } 313c874bf59SLoic Pallardy 314c874bf59SLoic Pallardy return 0; 315c874bf59SLoic Pallardy } 316c874bf59SLoic Pallardy 3176db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i) 318400e64dfSOhad Ben-Cohen { 3197a186941SOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 320b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 3216db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 322c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 3237a186941SOhad Ben-Cohen int ret, size, notifyid; 324c6aed238SLoic Pallardy struct rproc_mem_entry *mem; 325400e64dfSOhad Ben-Cohen 3266db20ea8SOhad Ben-Cohen /* actual size of vring (in bytes) */ 3276db20ea8SOhad Ben-Cohen size = PAGE_ALIGN(vring_size(rvring->len, rvring->align)); 3286db20ea8SOhad Ben-Cohen 329c6aed238SLoic Pallardy rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; 330c6aed238SLoic Pallardy 331c6aed238SLoic Pallardy /* Search for pre-registered carveout */ 332c6aed238SLoic Pallardy mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index, 333c6aed238SLoic Pallardy i); 334c6aed238SLoic Pallardy if (mem) { 335c6aed238SLoic Pallardy if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size)) 336c6aed238SLoic Pallardy return -ENOMEM; 337c6aed238SLoic Pallardy } else { 338c6aed238SLoic Pallardy /* Register carveout in in list */ 339c6aed238SLoic Pallardy mem = rproc_mem_entry_init(dev, 0, 0, size, rsc->vring[i].da, 340c6aed238SLoic Pallardy rproc_alloc_carveout, 341c6aed238SLoic Pallardy rproc_release_carveout, 342c6aed238SLoic Pallardy "vdev%dvring%d", 343c6aed238SLoic Pallardy rvdev->index, i); 344c6aed238SLoic Pallardy if (!mem) { 345c6aed238SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 346c6aed238SLoic Pallardy return -ENOMEM; 347c6aed238SLoic Pallardy } 348c6aed238SLoic Pallardy 349c6aed238SLoic Pallardy rproc_add_carveout(rproc, mem); 3506db20ea8SOhad Ben-Cohen } 3516db20ea8SOhad Ben-Cohen 3526db20ea8SOhad Ben-Cohen /* 3536db20ea8SOhad Ben-Cohen * Assign an rproc-wide unique index for this vring 3546db20ea8SOhad Ben-Cohen * TODO: assign a notifyid for rvdev updates as well 3556db20ea8SOhad Ben-Cohen * TODO: support predefined notifyids (via resource table) 3566db20ea8SOhad Ben-Cohen */ 35715fc6110STejun Heo ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL); 358b39599b7SSuman Anna if (ret < 0) { 35915fc6110STejun Heo dev_err(dev, "idr_alloc failed: %d\n", ret); 3606db20ea8SOhad Ben-Cohen return ret; 3616db20ea8SOhad Ben-Cohen } 36215fc6110STejun Heo notifyid = ret; 3636db20ea8SOhad Ben-Cohen 36448f18f89SBjorn Andersson /* Potentially bump max_notifyid */ 36548f18f89SBjorn Andersson if (notifyid > rproc->max_notifyid) 36648f18f89SBjorn Andersson rproc->max_notifyid = notifyid; 36748f18f89SBjorn Andersson 3686db20ea8SOhad Ben-Cohen rvring->notifyid = notifyid; 3696db20ea8SOhad Ben-Cohen 370c6aed238SLoic Pallardy /* Let the rproc know the notifyid of this vring.*/ 371c0d63157SSjur Brændeland rsc->vring[i].notifyid = notifyid; 3726db20ea8SOhad Ben-Cohen return 0; 3736db20ea8SOhad Ben-Cohen } 3746db20ea8SOhad Ben-Cohen 375400e64dfSOhad Ben-Cohen static int 3766db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i) 377400e64dfSOhad Ben-Cohen { 378400e64dfSOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 379b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 380400e64dfSOhad Ben-Cohen struct fw_rsc_vdev_vring *vring = &rsc->vring[i]; 3816db20ea8SOhad Ben-Cohen struct rproc_vring *rvring = &rvdev->vring[i]; 382400e64dfSOhad Ben-Cohen 3839d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n", 3847a186941SOhad Ben-Cohen i, vring->da, vring->num, vring->align); 3857a186941SOhad Ben-Cohen 38663140e0eSOhad Ben-Cohen /* verify queue size and vring alignment are sane */ 38763140e0eSOhad Ben-Cohen if (!vring->num || !vring->align) { 38863140e0eSOhad Ben-Cohen dev_err(dev, "invalid qsz (%d) or alignment (%d)\n", 38963140e0eSOhad Ben-Cohen vring->num, vring->align); 390400e64dfSOhad Ben-Cohen return -EINVAL; 391400e64dfSOhad Ben-Cohen } 392400e64dfSOhad Ben-Cohen 3936db20ea8SOhad Ben-Cohen rvring->len = vring->num; 3946db20ea8SOhad Ben-Cohen rvring->align = vring->align; 3956db20ea8SOhad Ben-Cohen rvring->rvdev = rvdev; 396400e64dfSOhad Ben-Cohen 397400e64dfSOhad Ben-Cohen return 0; 398400e64dfSOhad Ben-Cohen } 399400e64dfSOhad Ben-Cohen 4006db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring) 4017a186941SOhad Ben-Cohen { 4026db20ea8SOhad Ben-Cohen struct rproc *rproc = rvring->rvdev->rproc; 403c0d63157SSjur Brændeland int idx = rvring->rvdev->vring - rvring; 404c0d63157SSjur Brændeland struct fw_rsc_vdev *rsc; 4057a186941SOhad Ben-Cohen 4067a186941SOhad Ben-Cohen idr_remove(&rproc->notifyids, rvring->notifyid); 407099a3f33SSjur Brændeland 408c0d63157SSjur Brændeland /* reset resource entry info */ 409c0d63157SSjur Brændeland rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset; 410c0d63157SSjur Brændeland rsc->vring[idx].da = 0; 411c0d63157SSjur Brændeland rsc->vring[idx].notifyid = -1; 4127a186941SOhad Ben-Cohen } 4137a186941SOhad Ben-Cohen 4146f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev) 415f5bcb353SBjorn Andersson { 416f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 417f5bcb353SBjorn Andersson 418f5bcb353SBjorn Andersson return rproc_add_virtio_dev(rvdev, rvdev->id); 419f5bcb353SBjorn Andersson } 420f5bcb353SBjorn Andersson 4216f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed) 422f5bcb353SBjorn Andersson { 423f5bcb353SBjorn Andersson struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev); 424f5bcb353SBjorn Andersson 425f5bcb353SBjorn Andersson rproc_remove_virtio_dev(rvdev); 426f5bcb353SBjorn Andersson } 427f5bcb353SBjorn Andersson 428400e64dfSOhad Ben-Cohen /** 429*086d0872SLoic Pallardy * rproc_rvdev_release() - release the existence of a rvdev 430*086d0872SLoic Pallardy * 431*086d0872SLoic Pallardy * @dev: the subdevice's dev 432*086d0872SLoic Pallardy */ 433*086d0872SLoic Pallardy static void rproc_rvdev_release(struct device *dev) 434*086d0872SLoic Pallardy { 435*086d0872SLoic Pallardy struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev); 436*086d0872SLoic Pallardy 437*086d0872SLoic Pallardy of_reserved_mem_device_release(dev); 438*086d0872SLoic Pallardy 439*086d0872SLoic Pallardy kfree(rvdev); 440*086d0872SLoic Pallardy } 441*086d0872SLoic Pallardy 442*086d0872SLoic Pallardy /** 443fd2c15ecSOhad Ben-Cohen * rproc_handle_vdev() - handle a vdev fw resource 444400e64dfSOhad Ben-Cohen * @rproc: the remote processor 445400e64dfSOhad Ben-Cohen * @rsc: the vring resource descriptor 446fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 447400e64dfSOhad Ben-Cohen * 4487a186941SOhad Ben-Cohen * This resource entry requests the host to statically register a virtio 4497a186941SOhad Ben-Cohen * device (vdev), and setup everything needed to support it. It contains 4507a186941SOhad Ben-Cohen * everything needed to make it possible: the virtio device id, virtio 4517a186941SOhad Ben-Cohen * device features, vrings information, virtio config space, etc... 452400e64dfSOhad Ben-Cohen * 4537a186941SOhad Ben-Cohen * Before registering the vdev, the vrings are allocated from non-cacheable 4547a186941SOhad Ben-Cohen * physically contiguous memory. Currently we only support two vrings per 4557a186941SOhad Ben-Cohen * remote processor (temporary limitation). We might also want to consider 4567a186941SOhad Ben-Cohen * doing the vring allocation only later when ->find_vqs() is invoked, and 4577a186941SOhad Ben-Cohen * then release them upon ->del_vqs(). 458400e64dfSOhad Ben-Cohen * 4597a186941SOhad Ben-Cohen * Note: @da is currently not really handled correctly: we dynamically 4607a186941SOhad Ben-Cohen * allocate it using the DMA API, ignoring requested hard coded addresses, 4617a186941SOhad Ben-Cohen * and we don't take care of any required IOMMU programming. This is all 4627a186941SOhad Ben-Cohen * going to be taken care of when the generic iommu-based DMA API will be 4637a186941SOhad Ben-Cohen * merged. Meanwhile, statically-addressed iommu-based firmware images should 4647a186941SOhad Ben-Cohen * use RSC_DEVMEM resource entries to map their required @da to the physical 4657a186941SOhad Ben-Cohen * address of their base CMA region (ouch, hacky!). 466400e64dfSOhad Ben-Cohen * 467400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 468400e64dfSOhad Ben-Cohen */ 469fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc, 470a2b950acSOhad Ben-Cohen int offset, int avail) 471400e64dfSOhad Ben-Cohen { 472b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 4737a186941SOhad Ben-Cohen struct rproc_vdev *rvdev; 4747a186941SOhad Ben-Cohen int i, ret; 475*086d0872SLoic Pallardy char name[16]; 476fd2c15ecSOhad Ben-Cohen 477fd2c15ecSOhad Ben-Cohen /* make sure resource isn't truncated */ 478fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring) 479fd2c15ecSOhad Ben-Cohen + rsc->config_len > avail) { 480b5ab5e24SOhad Ben-Cohen dev_err(dev, "vdev rsc is truncated\n"); 481fd2c15ecSOhad Ben-Cohen return -EINVAL; 482fd2c15ecSOhad Ben-Cohen } 483fd2c15ecSOhad Ben-Cohen 484fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 485fd2c15ecSOhad Ben-Cohen if (rsc->reserved[0] || rsc->reserved[1]) { 486fd2c15ecSOhad Ben-Cohen dev_err(dev, "vdev rsc has non zero reserved bytes\n"); 487fd2c15ecSOhad Ben-Cohen return -EINVAL; 488fd2c15ecSOhad Ben-Cohen } 489fd2c15ecSOhad Ben-Cohen 4909d7814a9SAnna, Suman dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n", 491fd2c15ecSOhad Ben-Cohen rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings); 492400e64dfSOhad Ben-Cohen 4937a186941SOhad Ben-Cohen /* we currently support only two vrings per rvdev */ 4947a186941SOhad Ben-Cohen if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) { 495fd2c15ecSOhad Ben-Cohen dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings); 496400e64dfSOhad Ben-Cohen return -EINVAL; 497400e64dfSOhad Ben-Cohen } 498400e64dfSOhad Ben-Cohen 499899585adSAnna, Suman rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL); 5007a186941SOhad Ben-Cohen if (!rvdev) 5017a186941SOhad Ben-Cohen return -ENOMEM; 5027a186941SOhad Ben-Cohen 503aab8d802SBjorn Andersson kref_init(&rvdev->refcount); 504aab8d802SBjorn Andersson 505f5bcb353SBjorn Andersson rvdev->id = rsc->id; 5067a186941SOhad Ben-Cohen rvdev->rproc = rproc; 507c6aed238SLoic Pallardy rvdev->index = rproc->nb_vdev++; 5087a186941SOhad Ben-Cohen 509*086d0872SLoic Pallardy /* Initialise vdev subdevice */ 510*086d0872SLoic Pallardy snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index); 511*086d0872SLoic Pallardy rvdev->dev.parent = rproc->dev.parent; 512*086d0872SLoic Pallardy rvdev->dev.release = rproc_rvdev_release; 513*086d0872SLoic Pallardy dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name); 514*086d0872SLoic Pallardy dev_set_drvdata(&rvdev->dev, rvdev); 515*086d0872SLoic Pallardy 516*086d0872SLoic Pallardy ret = device_register(&rvdev->dev); 517*086d0872SLoic Pallardy if (ret) { 518*086d0872SLoic Pallardy put_device(&rvdev->dev); 519*086d0872SLoic Pallardy return ret; 520*086d0872SLoic Pallardy } 521*086d0872SLoic Pallardy /* Make device dma capable by inheriting from parent's capabilities */ 522*086d0872SLoic Pallardy set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent)); 523*086d0872SLoic Pallardy 524*086d0872SLoic Pallardy ret = dma_coerce_mask_and_coherent(&rvdev->dev, 525*086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent)); 526*086d0872SLoic Pallardy if (ret) { 527*086d0872SLoic Pallardy dev_warn(dev, 528*086d0872SLoic Pallardy "Failed to set DMA mask %llx. Trying to continue... %x\n", 529*086d0872SLoic Pallardy dma_get_mask(rproc->dev.parent), ret); 530*086d0872SLoic Pallardy } 531*086d0872SLoic Pallardy 5326db20ea8SOhad Ben-Cohen /* parse the vrings */ 533fd2c15ecSOhad Ben-Cohen for (i = 0; i < rsc->num_of_vrings; i++) { 5346db20ea8SOhad Ben-Cohen ret = rproc_parse_vring(rvdev, rsc, i); 5357a186941SOhad Ben-Cohen if (ret) 5366db20ea8SOhad Ben-Cohen goto free_rvdev; 537fd2c15ecSOhad Ben-Cohen } 538fd2c15ecSOhad Ben-Cohen 539a2b950acSOhad Ben-Cohen /* remember the resource offset*/ 540a2b950acSOhad Ben-Cohen rvdev->rsc_offset = offset; 541400e64dfSOhad Ben-Cohen 542a863af5dSBjorn Andersson /* allocate the vring resources */ 543a863af5dSBjorn Andersson for (i = 0; i < rsc->num_of_vrings; i++) { 544a863af5dSBjorn Andersson ret = rproc_alloc_vring(rvdev, i); 545a863af5dSBjorn Andersson if (ret) 546a863af5dSBjorn Andersson goto unwind_vring_allocations; 547a863af5dSBjorn Andersson } 548a863af5dSBjorn Andersson 5497a186941SOhad Ben-Cohen list_add_tail(&rvdev->node, &rproc->rvdevs); 550400e64dfSOhad Ben-Cohen 5516f8b0373SAlex Elder rvdev->subdev.start = rproc_vdev_do_start; 5526f8b0373SAlex Elder rvdev->subdev.stop = rproc_vdev_do_stop; 5534902676fSBjorn Andersson 5544902676fSBjorn Andersson rproc_add_subdev(rproc, &rvdev->subdev); 555400e64dfSOhad Ben-Cohen 556400e64dfSOhad Ben-Cohen return 0; 5577a186941SOhad Ben-Cohen 558a863af5dSBjorn Andersson unwind_vring_allocations: 559a863af5dSBjorn Andersson for (i--; i >= 0; i--) 560a863af5dSBjorn Andersson rproc_free_vring(&rvdev->vring[i]); 5616db20ea8SOhad Ben-Cohen free_rvdev: 562*086d0872SLoic Pallardy device_unregister(&rvdev->dev); 5637a186941SOhad Ben-Cohen return ret; 564400e64dfSOhad Ben-Cohen } 565400e64dfSOhad Ben-Cohen 566aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref) 567aab8d802SBjorn Andersson { 568aab8d802SBjorn Andersson struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount); 569a863af5dSBjorn Andersson struct rproc_vring *rvring; 570f5bcb353SBjorn Andersson struct rproc *rproc = rvdev->rproc; 571a863af5dSBjorn Andersson int id; 572a863af5dSBjorn Andersson 573a863af5dSBjorn Andersson for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) { 574a863af5dSBjorn Andersson rvring = &rvdev->vring[id]; 575a863af5dSBjorn Andersson rproc_free_vring(rvring); 576a863af5dSBjorn Andersson } 577aab8d802SBjorn Andersson 578f5bcb353SBjorn Andersson rproc_remove_subdev(rproc, &rvdev->subdev); 579aab8d802SBjorn Andersson list_del(&rvdev->node); 580*086d0872SLoic Pallardy device_unregister(&rvdev->dev); 581aab8d802SBjorn Andersson } 582aab8d802SBjorn Andersson 583400e64dfSOhad Ben-Cohen /** 584400e64dfSOhad Ben-Cohen * rproc_handle_trace() - handle a shared trace buffer resource 585400e64dfSOhad Ben-Cohen * @rproc: the remote processor 586400e64dfSOhad Ben-Cohen * @rsc: the trace resource descriptor 587fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 588400e64dfSOhad Ben-Cohen * 589400e64dfSOhad Ben-Cohen * In case the remote processor dumps trace logs into memory, 590400e64dfSOhad Ben-Cohen * export it via debugfs. 591400e64dfSOhad Ben-Cohen * 592400e64dfSOhad Ben-Cohen * Currently, the 'da' member of @rsc should contain the device address 593400e64dfSOhad Ben-Cohen * where the remote processor is dumping the traces. Later we could also 594400e64dfSOhad Ben-Cohen * support dynamically allocating this address using the generic 595400e64dfSOhad Ben-Cohen * DMA API (but currently there isn't a use case for that). 596400e64dfSOhad Ben-Cohen * 597400e64dfSOhad Ben-Cohen * Returns 0 on success, or an appropriate error code otherwise 598400e64dfSOhad Ben-Cohen */ 599fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc, 600a2b950acSOhad Ben-Cohen int offset, int avail) 601400e64dfSOhad Ben-Cohen { 602400e64dfSOhad Ben-Cohen struct rproc_mem_entry *trace; 603b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 604400e64dfSOhad Ben-Cohen void *ptr; 605400e64dfSOhad Ben-Cohen char name[15]; 606400e64dfSOhad Ben-Cohen 607fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 608b5ab5e24SOhad Ben-Cohen dev_err(dev, "trace rsc is truncated\n"); 609fd2c15ecSOhad Ben-Cohen return -EINVAL; 610fd2c15ecSOhad Ben-Cohen } 611fd2c15ecSOhad Ben-Cohen 612fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 613fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 614fd2c15ecSOhad Ben-Cohen dev_err(dev, "trace rsc has non zero reserved bytes\n"); 615fd2c15ecSOhad Ben-Cohen return -EINVAL; 616fd2c15ecSOhad Ben-Cohen } 617fd2c15ecSOhad Ben-Cohen 618400e64dfSOhad Ben-Cohen /* what's the kernel address of this resource ? */ 619400e64dfSOhad Ben-Cohen ptr = rproc_da_to_va(rproc, rsc->da, rsc->len); 620400e64dfSOhad Ben-Cohen if (!ptr) { 621400e64dfSOhad Ben-Cohen dev_err(dev, "erroneous trace resource entry\n"); 622400e64dfSOhad Ben-Cohen return -EINVAL; 623400e64dfSOhad Ben-Cohen } 624400e64dfSOhad Ben-Cohen 625400e64dfSOhad Ben-Cohen trace = kzalloc(sizeof(*trace), GFP_KERNEL); 626172e6ab1SSuman Anna if (!trace) 627400e64dfSOhad Ben-Cohen return -ENOMEM; 628400e64dfSOhad Ben-Cohen 629400e64dfSOhad Ben-Cohen /* set the trace buffer dma properties */ 630400e64dfSOhad Ben-Cohen trace->len = rsc->len; 631400e64dfSOhad Ben-Cohen trace->va = ptr; 632400e64dfSOhad Ben-Cohen 633400e64dfSOhad Ben-Cohen /* make sure snprintf always null terminates, even if truncating */ 634400e64dfSOhad Ben-Cohen snprintf(name, sizeof(name), "trace%d", rproc->num_traces); 635400e64dfSOhad Ben-Cohen 636400e64dfSOhad Ben-Cohen /* create the debugfs entry */ 637400e64dfSOhad Ben-Cohen trace->priv = rproc_create_trace_file(name, rproc, trace); 638400e64dfSOhad Ben-Cohen if (!trace->priv) { 639400e64dfSOhad Ben-Cohen trace->va = NULL; 640400e64dfSOhad Ben-Cohen kfree(trace); 641400e64dfSOhad Ben-Cohen return -EINVAL; 642400e64dfSOhad Ben-Cohen } 643400e64dfSOhad Ben-Cohen 644400e64dfSOhad Ben-Cohen list_add_tail(&trace->node, &rproc->traces); 645400e64dfSOhad Ben-Cohen 646400e64dfSOhad Ben-Cohen rproc->num_traces++; 647400e64dfSOhad Ben-Cohen 648276ec993SLoic Pallardy dev_dbg(dev, "%s added: va %pK, da 0x%x, len 0x%x\n", 64935386166SLee Jones name, ptr, rsc->da, rsc->len); 650400e64dfSOhad Ben-Cohen 651400e64dfSOhad Ben-Cohen return 0; 652400e64dfSOhad Ben-Cohen } 653400e64dfSOhad Ben-Cohen 654400e64dfSOhad Ben-Cohen /** 655400e64dfSOhad Ben-Cohen * rproc_handle_devmem() - handle devmem resource entry 656400e64dfSOhad Ben-Cohen * @rproc: remote processor handle 657400e64dfSOhad Ben-Cohen * @rsc: the devmem resource entry 658fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for sanity checking the image) 659400e64dfSOhad Ben-Cohen * 660400e64dfSOhad Ben-Cohen * Remote processors commonly need to access certain on-chip peripherals. 661400e64dfSOhad Ben-Cohen * 662400e64dfSOhad Ben-Cohen * Some of these remote processors access memory via an iommu device, 663400e64dfSOhad Ben-Cohen * and might require us to configure their iommu before they can access 664400e64dfSOhad Ben-Cohen * the on-chip peripherals they need. 665400e64dfSOhad Ben-Cohen * 666400e64dfSOhad Ben-Cohen * This resource entry is a request to map such a peripheral device. 667400e64dfSOhad Ben-Cohen * 668400e64dfSOhad Ben-Cohen * These devmem entries will contain the physical address of the device in 669400e64dfSOhad Ben-Cohen * the 'pa' member. If a specific device address is expected, then 'da' will 670400e64dfSOhad Ben-Cohen * contain it (currently this is the only use case supported). 'len' will 671400e64dfSOhad Ben-Cohen * contain the size of the physical region we need to map. 672400e64dfSOhad Ben-Cohen * 673400e64dfSOhad Ben-Cohen * Currently we just "trust" those devmem entries to contain valid physical 674400e64dfSOhad Ben-Cohen * addresses, but this is going to change: we want the implementations to 675400e64dfSOhad Ben-Cohen * tell us ranges of physical addresses the firmware is allowed to request, 676400e64dfSOhad Ben-Cohen * and not allow firmwares to request access to physical addresses that 677400e64dfSOhad Ben-Cohen * are outside those ranges. 678400e64dfSOhad Ben-Cohen */ 679fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc, 680a2b950acSOhad Ben-Cohen int offset, int avail) 681400e64dfSOhad Ben-Cohen { 682400e64dfSOhad Ben-Cohen struct rproc_mem_entry *mapping; 683b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 684400e64dfSOhad Ben-Cohen int ret; 685400e64dfSOhad Ben-Cohen 686400e64dfSOhad Ben-Cohen /* no point in handling this resource without a valid iommu domain */ 687400e64dfSOhad Ben-Cohen if (!rproc->domain) 688400e64dfSOhad Ben-Cohen return -EINVAL; 689400e64dfSOhad Ben-Cohen 690fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 691b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc is truncated\n"); 692fd2c15ecSOhad Ben-Cohen return -EINVAL; 693fd2c15ecSOhad Ben-Cohen } 694fd2c15ecSOhad Ben-Cohen 695fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 696fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 697b5ab5e24SOhad Ben-Cohen dev_err(dev, "devmem rsc has non zero reserved bytes\n"); 698fd2c15ecSOhad Ben-Cohen return -EINVAL; 699fd2c15ecSOhad Ben-Cohen } 700fd2c15ecSOhad Ben-Cohen 701400e64dfSOhad Ben-Cohen mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 702172e6ab1SSuman Anna if (!mapping) 703400e64dfSOhad Ben-Cohen return -ENOMEM; 704400e64dfSOhad Ben-Cohen 705400e64dfSOhad Ben-Cohen ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags); 706400e64dfSOhad Ben-Cohen if (ret) { 707b5ab5e24SOhad Ben-Cohen dev_err(dev, "failed to map devmem: %d\n", ret); 708400e64dfSOhad Ben-Cohen goto out; 709400e64dfSOhad Ben-Cohen } 710400e64dfSOhad Ben-Cohen 711400e64dfSOhad Ben-Cohen /* 712400e64dfSOhad Ben-Cohen * We'll need this info later when we'll want to unmap everything 713400e64dfSOhad Ben-Cohen * (e.g. on shutdown). 714400e64dfSOhad Ben-Cohen * 715400e64dfSOhad Ben-Cohen * We can't trust the remote processor not to change the resource 716400e64dfSOhad Ben-Cohen * table, so we must maintain this info independently. 717400e64dfSOhad Ben-Cohen */ 718400e64dfSOhad Ben-Cohen mapping->da = rsc->da; 719400e64dfSOhad Ben-Cohen mapping->len = rsc->len; 720400e64dfSOhad Ben-Cohen list_add_tail(&mapping->node, &rproc->mappings); 721400e64dfSOhad Ben-Cohen 722b5ab5e24SOhad Ben-Cohen dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n", 723400e64dfSOhad Ben-Cohen rsc->pa, rsc->da, rsc->len); 724400e64dfSOhad Ben-Cohen 725400e64dfSOhad Ben-Cohen return 0; 726400e64dfSOhad Ben-Cohen 727400e64dfSOhad Ben-Cohen out: 728400e64dfSOhad Ben-Cohen kfree(mapping); 729400e64dfSOhad Ben-Cohen return ret; 730400e64dfSOhad Ben-Cohen } 731400e64dfSOhad Ben-Cohen 732400e64dfSOhad Ben-Cohen /** 733d7c51706SLoic Pallardy * rproc_alloc_carveout() - allocated specified carveout 734d7c51706SLoic Pallardy * @rproc: rproc handle 735d7c51706SLoic Pallardy * @mem: the memory entry to allocate 736d7c51706SLoic Pallardy * 737d7c51706SLoic Pallardy * This function allocate specified memory entry @mem using 738d7c51706SLoic Pallardy * dma_alloc_coherent() as default allocator 739d7c51706SLoic Pallardy */ 740d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc, 741d7c51706SLoic Pallardy struct rproc_mem_entry *mem) 742d7c51706SLoic Pallardy { 743d7c51706SLoic Pallardy struct rproc_mem_entry *mapping = NULL; 744d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 745d7c51706SLoic Pallardy dma_addr_t dma; 746d7c51706SLoic Pallardy void *va; 747d7c51706SLoic Pallardy int ret; 748d7c51706SLoic Pallardy 749d7c51706SLoic Pallardy va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL); 750d7c51706SLoic Pallardy if (!va) { 751d7c51706SLoic Pallardy dev_err(dev->parent, 752d7c51706SLoic Pallardy "failed to allocate dma memory: len 0x%x\n", mem->len); 753d7c51706SLoic Pallardy return -ENOMEM; 754d7c51706SLoic Pallardy } 755d7c51706SLoic Pallardy 756d7c51706SLoic Pallardy dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%x\n", 757d7c51706SLoic Pallardy va, &dma, mem->len); 758d7c51706SLoic Pallardy 759d7c51706SLoic Pallardy /* 760d7c51706SLoic Pallardy * Ok, this is non-standard. 761d7c51706SLoic Pallardy * 762d7c51706SLoic Pallardy * Sometimes we can't rely on the generic iommu-based DMA API 763d7c51706SLoic Pallardy * to dynamically allocate the device address and then set the IOMMU 764d7c51706SLoic Pallardy * tables accordingly, because some remote processors might 765d7c51706SLoic Pallardy * _require_ us to use hard coded device addresses that their 766d7c51706SLoic Pallardy * firmware was compiled with. 767d7c51706SLoic Pallardy * 768d7c51706SLoic Pallardy * In this case, we must use the IOMMU API directly and map 769d7c51706SLoic Pallardy * the memory to the device address as expected by the remote 770d7c51706SLoic Pallardy * processor. 771d7c51706SLoic Pallardy * 772d7c51706SLoic Pallardy * Obviously such remote processor devices should not be configured 773d7c51706SLoic Pallardy * to use the iommu-based DMA API: we expect 'dma' to contain the 774d7c51706SLoic Pallardy * physical address in this case. 775d7c51706SLoic Pallardy */ 776d7c51706SLoic Pallardy 777d7c51706SLoic Pallardy if (mem->da != FW_RSC_ADDR_ANY) { 778d7c51706SLoic Pallardy if (!rproc->domain) { 779d7c51706SLoic Pallardy dev_err(dev->parent, 780d7c51706SLoic Pallardy "Bad carveout rsc configuration\n"); 781d7c51706SLoic Pallardy ret = -ENOMEM; 782d7c51706SLoic Pallardy goto dma_free; 783d7c51706SLoic Pallardy } 784d7c51706SLoic Pallardy 785d7c51706SLoic Pallardy mapping = kzalloc(sizeof(*mapping), GFP_KERNEL); 786d7c51706SLoic Pallardy if (!mapping) { 787d7c51706SLoic Pallardy ret = -ENOMEM; 788d7c51706SLoic Pallardy goto dma_free; 789d7c51706SLoic Pallardy } 790d7c51706SLoic Pallardy 791d7c51706SLoic Pallardy ret = iommu_map(rproc->domain, mem->da, dma, mem->len, 792d7c51706SLoic Pallardy mem->flags); 793d7c51706SLoic Pallardy if (ret) { 794d7c51706SLoic Pallardy dev_err(dev, "iommu_map failed: %d\n", ret); 795d7c51706SLoic Pallardy goto free_mapping; 796d7c51706SLoic Pallardy } 797d7c51706SLoic Pallardy 798d7c51706SLoic Pallardy /* 799d7c51706SLoic Pallardy * We'll need this info later when we'll want to unmap 800d7c51706SLoic Pallardy * everything (e.g. on shutdown). 801d7c51706SLoic Pallardy * 802d7c51706SLoic Pallardy * We can't trust the remote processor not to change the 803d7c51706SLoic Pallardy * resource table, so we must maintain this info independently. 804d7c51706SLoic Pallardy */ 805d7c51706SLoic Pallardy mapping->da = mem->da; 806d7c51706SLoic Pallardy mapping->len = mem->len; 807d7c51706SLoic Pallardy list_add_tail(&mapping->node, &rproc->mappings); 808d7c51706SLoic Pallardy 809d7c51706SLoic Pallardy dev_dbg(dev, "carveout mapped 0x%x to %pad\n", 810d7c51706SLoic Pallardy mem->da, &dma); 811d7c51706SLoic Pallardy } else { 812d7c51706SLoic Pallardy mem->da = (u32)dma; 813d7c51706SLoic Pallardy } 814d7c51706SLoic Pallardy 815d7c51706SLoic Pallardy mem->dma = (u32)dma; 816d7c51706SLoic Pallardy mem->va = va; 817d7c51706SLoic Pallardy 818d7c51706SLoic Pallardy return 0; 819d7c51706SLoic Pallardy 820d7c51706SLoic Pallardy free_mapping: 821d7c51706SLoic Pallardy kfree(mapping); 822d7c51706SLoic Pallardy dma_free: 823d7c51706SLoic Pallardy dma_free_coherent(dev->parent, mem->len, va, dma); 824d7c51706SLoic Pallardy return ret; 825d7c51706SLoic Pallardy } 826d7c51706SLoic Pallardy 827d7c51706SLoic Pallardy /** 828f2e74abfSLoic Pallardy * rproc_release_carveout() - release acquired carveout 829f2e74abfSLoic Pallardy * @rproc: rproc handle 830f2e74abfSLoic Pallardy * @mem: the memory entry to release 831f2e74abfSLoic Pallardy * 832f2e74abfSLoic Pallardy * This function releases specified memory entry @mem allocated via 833d7c51706SLoic Pallardy * rproc_alloc_carveout() function by @rproc. 834f2e74abfSLoic Pallardy */ 835f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc, 836f2e74abfSLoic Pallardy struct rproc_mem_entry *mem) 837f2e74abfSLoic Pallardy { 838f2e74abfSLoic Pallardy struct device *dev = &rproc->dev; 839f2e74abfSLoic Pallardy 840f2e74abfSLoic Pallardy /* clean up carveout allocations */ 841f2e74abfSLoic Pallardy dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma); 842f2e74abfSLoic Pallardy return 0; 843f2e74abfSLoic Pallardy } 844f2e74abfSLoic Pallardy 845f2e74abfSLoic Pallardy /** 846400e64dfSOhad Ben-Cohen * rproc_handle_carveout() - handle phys contig memory allocation requests 847400e64dfSOhad Ben-Cohen * @rproc: rproc handle 848400e64dfSOhad Ben-Cohen * @rsc: the resource entry 849fd2c15ecSOhad Ben-Cohen * @avail: size of available data (for image validation) 850400e64dfSOhad Ben-Cohen * 851400e64dfSOhad Ben-Cohen * This function will handle firmware requests for allocation of physically 852400e64dfSOhad Ben-Cohen * contiguous memory regions. 853400e64dfSOhad Ben-Cohen * 854400e64dfSOhad Ben-Cohen * These request entries should come first in the firmware's resource table, 855400e64dfSOhad Ben-Cohen * as other firmware entries might request placing other data objects inside 856400e64dfSOhad Ben-Cohen * these memory regions (e.g. data/code segments, trace resource entries, ...). 857400e64dfSOhad Ben-Cohen * 858400e64dfSOhad Ben-Cohen * Allocating memory this way helps utilizing the reserved physical memory 859400e64dfSOhad Ben-Cohen * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries 860400e64dfSOhad Ben-Cohen * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB 861400e64dfSOhad Ben-Cohen * pressure is important; it may have a substantial impact on performance. 862400e64dfSOhad Ben-Cohen */ 863fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc, 864a2b950acSOhad Ben-Cohen struct fw_rsc_carveout *rsc, 865a2b950acSOhad Ben-Cohen int offset, int avail) 866400e64dfSOhad Ben-Cohen { 867d7c51706SLoic Pallardy struct rproc_mem_entry *carveout; 868b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 869400e64dfSOhad Ben-Cohen 870fd2c15ecSOhad Ben-Cohen if (sizeof(*rsc) > avail) { 871b5ab5e24SOhad Ben-Cohen dev_err(dev, "carveout rsc is truncated\n"); 872fd2c15ecSOhad Ben-Cohen return -EINVAL; 873fd2c15ecSOhad Ben-Cohen } 874fd2c15ecSOhad Ben-Cohen 875fd2c15ecSOhad Ben-Cohen /* make sure reserved bytes are zeroes */ 876fd2c15ecSOhad Ben-Cohen if (rsc->reserved) { 877fd2c15ecSOhad Ben-Cohen dev_err(dev, "carveout rsc has non zero reserved bytes\n"); 878fd2c15ecSOhad Ben-Cohen return -EINVAL; 879fd2c15ecSOhad Ben-Cohen } 880fd2c15ecSOhad Ben-Cohen 8819d7814a9SAnna, Suman dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n", 88235386166SLee Jones rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags); 883fd2c15ecSOhad Ben-Cohen 884ffa5f9c8SLoic Pallardy /* 885ffa5f9c8SLoic Pallardy * Check carveout rsc already part of a registered carveout, 886ffa5f9c8SLoic Pallardy * Search by name, then check the da and length 887ffa5f9c8SLoic Pallardy */ 888ffa5f9c8SLoic Pallardy carveout = rproc_find_carveout_by_name(rproc, rsc->name); 889ffa5f9c8SLoic Pallardy 890ffa5f9c8SLoic Pallardy if (carveout) { 891ffa5f9c8SLoic Pallardy if (carveout->rsc_offset != FW_RSC_ADDR_ANY) { 892ffa5f9c8SLoic Pallardy dev_err(dev, 893ffa5f9c8SLoic Pallardy "Carveout already associated to resource table\n"); 894ffa5f9c8SLoic Pallardy return -ENOMEM; 895ffa5f9c8SLoic Pallardy } 896ffa5f9c8SLoic Pallardy 897ffa5f9c8SLoic Pallardy if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len)) 898ffa5f9c8SLoic Pallardy return -ENOMEM; 899ffa5f9c8SLoic Pallardy 900ffa5f9c8SLoic Pallardy /* Update memory carveout with resource table info */ 901ffa5f9c8SLoic Pallardy carveout->rsc_offset = offset; 902ffa5f9c8SLoic Pallardy carveout->flags = rsc->flags; 903ffa5f9c8SLoic Pallardy 904ffa5f9c8SLoic Pallardy return 0; 905ffa5f9c8SLoic Pallardy } 906ffa5f9c8SLoic Pallardy 907d7c51706SLoic Pallardy /* Register carveout in in list */ 908d7c51706SLoic Pallardy carveout = rproc_mem_entry_init(dev, 0, 0, rsc->len, rsc->da, 909d7c51706SLoic Pallardy rproc_alloc_carveout, 910d7c51706SLoic Pallardy rproc_release_carveout, rsc->name); 911d7c51706SLoic Pallardy if (!carveout) { 912d7c51706SLoic Pallardy dev_err(dev, "Can't allocate memory entry structure\n"); 91372029c90SLoic Pallardy return -ENOMEM; 914400e64dfSOhad Ben-Cohen } 915400e64dfSOhad Ben-Cohen 916d7c51706SLoic Pallardy carveout->flags = rsc->flags; 917d7c51706SLoic Pallardy carveout->rsc_offset = offset; 91815c0b025SLoic Pallardy rproc_add_carveout(rproc, carveout); 919400e64dfSOhad Ben-Cohen 920400e64dfSOhad Ben-Cohen return 0; 921400e64dfSOhad Ben-Cohen } 922400e64dfSOhad Ben-Cohen 92372029c90SLoic Pallardy /** 92415c0b025SLoic Pallardy * rproc_add_carveout() - register an allocated carveout region 92515c0b025SLoic Pallardy * @rproc: rproc handle 92615c0b025SLoic Pallardy * @mem: memory entry to register 92715c0b025SLoic Pallardy * 92815c0b025SLoic Pallardy * This function registers specified memory entry in @rproc carveouts list. 92915c0b025SLoic Pallardy * Specified carveout should have been allocated before registering. 93015c0b025SLoic Pallardy */ 93115c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem) 93215c0b025SLoic Pallardy { 93315c0b025SLoic Pallardy list_add_tail(&mem->node, &rproc->carveouts); 93415c0b025SLoic Pallardy } 93515c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout); 93615c0b025SLoic Pallardy 93715c0b025SLoic Pallardy /** 93872029c90SLoic Pallardy * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct 93972029c90SLoic Pallardy * @dev: pointer on device struct 94072029c90SLoic Pallardy * @va: virtual address 94172029c90SLoic Pallardy * @dma: dma address 94272029c90SLoic Pallardy * @len: memory carveout length 94372029c90SLoic Pallardy * @da: device address 94472029c90SLoic Pallardy * @release: memory carveout function 94572029c90SLoic Pallardy * @name: carveout name 94672029c90SLoic Pallardy * 94772029c90SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 94872029c90SLoic Pallardy * provided by client. 94972029c90SLoic Pallardy */ 95072029c90SLoic Pallardy struct rproc_mem_entry * 95172029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev, 95272029c90SLoic Pallardy void *va, dma_addr_t dma, int len, u32 da, 953d7c51706SLoic Pallardy int (*alloc)(struct rproc *, struct rproc_mem_entry *), 95472029c90SLoic Pallardy int (*release)(struct rproc *, struct rproc_mem_entry *), 95572029c90SLoic Pallardy const char *name, ...) 95672029c90SLoic Pallardy { 95772029c90SLoic Pallardy struct rproc_mem_entry *mem; 95872029c90SLoic Pallardy va_list args; 95972029c90SLoic Pallardy 96072029c90SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 96172029c90SLoic Pallardy if (!mem) 96272029c90SLoic Pallardy return mem; 96372029c90SLoic Pallardy 96472029c90SLoic Pallardy mem->va = va; 96572029c90SLoic Pallardy mem->dma = dma; 96672029c90SLoic Pallardy mem->da = da; 96772029c90SLoic Pallardy mem->len = len; 968d7c51706SLoic Pallardy mem->alloc = alloc; 96972029c90SLoic Pallardy mem->release = release; 970d7c51706SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 9711429cca1SLoic Pallardy mem->of_resm_idx = -1; 97272029c90SLoic Pallardy 97372029c90SLoic Pallardy va_start(args, name); 97472029c90SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 97572029c90SLoic Pallardy va_end(args); 97672029c90SLoic Pallardy 97772029c90SLoic Pallardy return mem; 97872029c90SLoic Pallardy } 97972029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init); 98072029c90SLoic Pallardy 98172029c90SLoic Pallardy /** 9821429cca1SLoic Pallardy * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct 9831429cca1SLoic Pallardy * from a reserved memory phandle 9841429cca1SLoic Pallardy * @dev: pointer on device struct 9851429cca1SLoic Pallardy * @of_resm_idx: reserved memory phandle index in "memory-region" 9861429cca1SLoic Pallardy * @len: memory carveout length 9871429cca1SLoic Pallardy * @da: device address 9881429cca1SLoic Pallardy * @name: carveout name 9891429cca1SLoic Pallardy * 9901429cca1SLoic Pallardy * This function allocates a rproc_mem_entry struct and fill it with parameters 9911429cca1SLoic Pallardy * provided by client. 9921429cca1SLoic Pallardy */ 9931429cca1SLoic Pallardy struct rproc_mem_entry * 9941429cca1SLoic Pallardy rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, int len, 9951429cca1SLoic Pallardy u32 da, const char *name, ...) 9961429cca1SLoic Pallardy { 9971429cca1SLoic Pallardy struct rproc_mem_entry *mem; 9981429cca1SLoic Pallardy va_list args; 9991429cca1SLoic Pallardy 10001429cca1SLoic Pallardy mem = kzalloc(sizeof(*mem), GFP_KERNEL); 10011429cca1SLoic Pallardy if (!mem) 10021429cca1SLoic Pallardy return mem; 10031429cca1SLoic Pallardy 10041429cca1SLoic Pallardy mem->da = da; 10051429cca1SLoic Pallardy mem->len = len; 10061429cca1SLoic Pallardy mem->rsc_offset = FW_RSC_ADDR_ANY; 10071429cca1SLoic Pallardy mem->of_resm_idx = of_resm_idx; 10081429cca1SLoic Pallardy 10091429cca1SLoic Pallardy va_start(args, name); 10101429cca1SLoic Pallardy vsnprintf(mem->name, sizeof(mem->name), name, args); 10111429cca1SLoic Pallardy va_end(args); 10121429cca1SLoic Pallardy 10131429cca1SLoic Pallardy return mem; 10141429cca1SLoic Pallardy } 10151429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init); 10161429cca1SLoic Pallardy 10171429cca1SLoic Pallardy /** 1018e12bc14bSOhad Ben-Cohen * A lookup table for resource handlers. The indices are defined in 1019e12bc14bSOhad Ben-Cohen * enum fw_resource_type. 1020e12bc14bSOhad Ben-Cohen */ 1021232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = { 1022fd2c15ecSOhad Ben-Cohen [RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout, 1023fd2c15ecSOhad Ben-Cohen [RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem, 1024fd2c15ecSOhad Ben-Cohen [RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace, 1025232fcdbbSSjur Brændeland [RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev, 1026232fcdbbSSjur Brændeland }; 1027232fcdbbSSjur Brændeland 1028400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */ 1029a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc, 1030232fcdbbSSjur Brændeland rproc_handle_resource_t handlers[RSC_LAST]) 1031400e64dfSOhad Ben-Cohen { 1032b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1033e12bc14bSOhad Ben-Cohen rproc_handle_resource_t handler; 1034fd2c15ecSOhad Ben-Cohen int ret = 0, i; 1035400e64dfSOhad Ben-Cohen 1036d4bb86f2SBjorn Andersson if (!rproc->table_ptr) 1037d4bb86f2SBjorn Andersson return 0; 1038d4bb86f2SBjorn Andersson 1039a2b950acSOhad Ben-Cohen for (i = 0; i < rproc->table_ptr->num; i++) { 1040a2b950acSOhad Ben-Cohen int offset = rproc->table_ptr->offset[i]; 1041a2b950acSOhad Ben-Cohen struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset; 1042a4b24c75SBjorn Andersson int avail = rproc->table_sz - offset - sizeof(*hdr); 1043fd2c15ecSOhad Ben-Cohen void *rsc = (void *)hdr + sizeof(*hdr); 1044400e64dfSOhad Ben-Cohen 1045fd2c15ecSOhad Ben-Cohen /* make sure table isn't truncated */ 1046fd2c15ecSOhad Ben-Cohen if (avail < 0) { 1047fd2c15ecSOhad Ben-Cohen dev_err(dev, "rsc table is truncated\n"); 1048fd2c15ecSOhad Ben-Cohen return -EINVAL; 1049fd2c15ecSOhad Ben-Cohen } 1050fd2c15ecSOhad Ben-Cohen 1051fd2c15ecSOhad Ben-Cohen dev_dbg(dev, "rsc: type %d\n", hdr->type); 1052fd2c15ecSOhad Ben-Cohen 1053fd2c15ecSOhad Ben-Cohen if (hdr->type >= RSC_LAST) { 1054fd2c15ecSOhad Ben-Cohen dev_warn(dev, "unsupported resource %d\n", hdr->type); 1055e12bc14bSOhad Ben-Cohen continue; 1056400e64dfSOhad Ben-Cohen } 1057400e64dfSOhad Ben-Cohen 1058232fcdbbSSjur Brændeland handler = handlers[hdr->type]; 1059e12bc14bSOhad Ben-Cohen if (!handler) 1060e12bc14bSOhad Ben-Cohen continue; 1061e12bc14bSOhad Ben-Cohen 1062a2b950acSOhad Ben-Cohen ret = handler(rproc, rsc, offset + sizeof(*hdr), avail); 10637a186941SOhad Ben-Cohen if (ret) 1064400e64dfSOhad Ben-Cohen break; 1065400e64dfSOhad Ben-Cohen } 1066400e64dfSOhad Ben-Cohen 1067400e64dfSOhad Ben-Cohen return ret; 1068400e64dfSOhad Ben-Cohen } 1069400e64dfSOhad Ben-Cohen 1070c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc) 1071c455daa4SBjorn Andersson { 1072c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1073c455daa4SBjorn Andersson int ret; 1074c455daa4SBjorn Andersson 1075c455daa4SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1076c455daa4SBjorn Andersson if (subdev->prepare) { 1077c455daa4SBjorn Andersson ret = subdev->prepare(subdev); 1078c455daa4SBjorn Andersson if (ret) 1079c455daa4SBjorn Andersson goto unroll_preparation; 1080c455daa4SBjorn Andersson } 1081c455daa4SBjorn Andersson } 1082c455daa4SBjorn Andersson 1083c455daa4SBjorn Andersson return 0; 1084c455daa4SBjorn Andersson 1085c455daa4SBjorn Andersson unroll_preparation: 1086c455daa4SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1087c455daa4SBjorn Andersson if (subdev->unprepare) 1088c455daa4SBjorn Andersson subdev->unprepare(subdev); 1089c455daa4SBjorn Andersson } 1090c455daa4SBjorn Andersson 1091c455daa4SBjorn Andersson return ret; 1092c455daa4SBjorn Andersson } 1093c455daa4SBjorn Andersson 1094618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc) 10957bdc9650SBjorn Andersson { 10967bdc9650SBjorn Andersson struct rproc_subdev *subdev; 10977bdc9650SBjorn Andersson int ret; 10987bdc9650SBjorn Andersson 10997bdc9650SBjorn Andersson list_for_each_entry(subdev, &rproc->subdevs, node) { 1100be37b1e0SBjorn Andersson if (subdev->start) { 1101618fcff3SBjorn Andersson ret = subdev->start(subdev); 11027bdc9650SBjorn Andersson if (ret) 11037bdc9650SBjorn Andersson goto unroll_registration; 11047bdc9650SBjorn Andersson } 1105be37b1e0SBjorn Andersson } 11067bdc9650SBjorn Andersson 11077bdc9650SBjorn Andersson return 0; 11087bdc9650SBjorn Andersson 11097bdc9650SBjorn Andersson unroll_registration: 1110be37b1e0SBjorn Andersson list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) { 1111be37b1e0SBjorn Andersson if (subdev->stop) 1112618fcff3SBjorn Andersson subdev->stop(subdev, true); 1113be37b1e0SBjorn Andersson } 11147bdc9650SBjorn Andersson 11157bdc9650SBjorn Andersson return ret; 11167bdc9650SBjorn Andersson } 11177bdc9650SBjorn Andersson 1118618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed) 11197bdc9650SBjorn Andersson { 11207bdc9650SBjorn Andersson struct rproc_subdev *subdev; 11217bdc9650SBjorn Andersson 1122be37b1e0SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1123be37b1e0SBjorn Andersson if (subdev->stop) 1124618fcff3SBjorn Andersson subdev->stop(subdev, crashed); 11257bdc9650SBjorn Andersson } 1126be37b1e0SBjorn Andersson } 11277bdc9650SBjorn Andersson 1128c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc) 1129c455daa4SBjorn Andersson { 1130c455daa4SBjorn Andersson struct rproc_subdev *subdev; 1131c455daa4SBjorn Andersson 1132c455daa4SBjorn Andersson list_for_each_entry_reverse(subdev, &rproc->subdevs, node) { 1133c455daa4SBjorn Andersson if (subdev->unprepare) 1134c455daa4SBjorn Andersson subdev->unprepare(subdev); 1135c455daa4SBjorn Andersson } 1136c455daa4SBjorn Andersson } 1137c455daa4SBjorn Andersson 1138400e64dfSOhad Ben-Cohen /** 1139d7c51706SLoic Pallardy * rproc_alloc_registered_carveouts() - allocate all carveouts registered 1140d7c51706SLoic Pallardy * in the list 1141d7c51706SLoic Pallardy * @rproc: the remote processor handle 1142d7c51706SLoic Pallardy * 1143d7c51706SLoic Pallardy * This function parses registered carveout list, performs allocation 1144d7c51706SLoic Pallardy * if alloc() ops registered and updates resource table information 1145d7c51706SLoic Pallardy * if rsc_offset set. 1146d7c51706SLoic Pallardy * 1147d7c51706SLoic Pallardy * Return: 0 on success 1148d7c51706SLoic Pallardy */ 1149d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc) 1150d7c51706SLoic Pallardy { 1151d7c51706SLoic Pallardy struct rproc_mem_entry *entry, *tmp; 1152d7c51706SLoic Pallardy struct fw_rsc_carveout *rsc; 1153d7c51706SLoic Pallardy struct device *dev = &rproc->dev; 1154d7c51706SLoic Pallardy int ret; 1155d7c51706SLoic Pallardy 1156d7c51706SLoic Pallardy list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1157d7c51706SLoic Pallardy if (entry->alloc) { 1158d7c51706SLoic Pallardy ret = entry->alloc(rproc, entry); 1159d7c51706SLoic Pallardy if (ret) { 1160d7c51706SLoic Pallardy dev_err(dev, "Unable to allocate carveout %s: %d\n", 1161d7c51706SLoic Pallardy entry->name, ret); 1162d7c51706SLoic Pallardy return -ENOMEM; 1163d7c51706SLoic Pallardy } 1164d7c51706SLoic Pallardy } 1165d7c51706SLoic Pallardy 1166d7c51706SLoic Pallardy if (entry->rsc_offset != FW_RSC_ADDR_ANY) { 1167d7c51706SLoic Pallardy /* update resource table */ 1168d7c51706SLoic Pallardy rsc = (void *)rproc->table_ptr + entry->rsc_offset; 1169d7c51706SLoic Pallardy 1170d7c51706SLoic Pallardy /* 1171d7c51706SLoic Pallardy * Some remote processors might need to know the pa 1172d7c51706SLoic Pallardy * even though they are behind an IOMMU. E.g., OMAP4's 1173d7c51706SLoic Pallardy * remote M3 processor needs this so it can control 1174d7c51706SLoic Pallardy * on-chip hardware accelerators that are not behind 1175d7c51706SLoic Pallardy * the IOMMU, and therefor must know the pa. 1176d7c51706SLoic Pallardy * 1177d7c51706SLoic Pallardy * Generally we don't want to expose physical addresses 1178d7c51706SLoic Pallardy * if we don't have to (remote processors are generally 1179d7c51706SLoic Pallardy * _not_ trusted), so we might want to do this only for 1180d7c51706SLoic Pallardy * remote processor that _must_ have this (e.g. OMAP4's 1181d7c51706SLoic Pallardy * dual M3 subsystem). 1182d7c51706SLoic Pallardy * 1183d7c51706SLoic Pallardy * Non-IOMMU processors might also want to have this info. 1184d7c51706SLoic Pallardy * In this case, the device address and the physical address 1185d7c51706SLoic Pallardy * are the same. 1186d7c51706SLoic Pallardy */ 1187ffa5f9c8SLoic Pallardy 1188ffa5f9c8SLoic Pallardy /* Use va if defined else dma to generate pa */ 1189d7c51706SLoic Pallardy if (entry->va) 1190d7c51706SLoic Pallardy rsc->pa = (u32)rproc_va_to_pa(entry->va); 1191ffa5f9c8SLoic Pallardy else 1192ffa5f9c8SLoic Pallardy rsc->pa = (u32)entry->dma; 1193ffa5f9c8SLoic Pallardy 1194ffa5f9c8SLoic Pallardy rsc->da = entry->da; 1195ffa5f9c8SLoic Pallardy rsc->len = entry->len; 1196d7c51706SLoic Pallardy } 1197d7c51706SLoic Pallardy } 1198d7c51706SLoic Pallardy 1199d7c51706SLoic Pallardy return 0; 1200d7c51706SLoic Pallardy } 1201d7c51706SLoic Pallardy 1202d7c51706SLoic Pallardy /** 12032666ca91SSarangdhar Joshi * rproc_coredump_cleanup() - clean up dump_segments list 12042666ca91SSarangdhar Joshi * @rproc: the remote processor handle 12052666ca91SSarangdhar Joshi */ 12062666ca91SSarangdhar Joshi static void rproc_coredump_cleanup(struct rproc *rproc) 12072666ca91SSarangdhar Joshi { 12082666ca91SSarangdhar Joshi struct rproc_dump_segment *entry, *tmp; 12092666ca91SSarangdhar Joshi 12102666ca91SSarangdhar Joshi list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) { 12112666ca91SSarangdhar Joshi list_del(&entry->node); 12122666ca91SSarangdhar Joshi kfree(entry); 12132666ca91SSarangdhar Joshi } 12142666ca91SSarangdhar Joshi } 12152666ca91SSarangdhar Joshi 12162666ca91SSarangdhar Joshi /** 1217400e64dfSOhad Ben-Cohen * rproc_resource_cleanup() - clean up and free all acquired resources 1218400e64dfSOhad Ben-Cohen * @rproc: rproc handle 1219400e64dfSOhad Ben-Cohen * 1220400e64dfSOhad Ben-Cohen * This function will free all resources acquired for @rproc, and it 12217a186941SOhad Ben-Cohen * is called whenever @rproc either shuts down or fails to boot. 1222400e64dfSOhad Ben-Cohen */ 1223400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc) 1224400e64dfSOhad Ben-Cohen { 1225400e64dfSOhad Ben-Cohen struct rproc_mem_entry *entry, *tmp; 1226d81fb32fSBjorn Andersson struct rproc_vdev *rvdev, *rvtmp; 1227b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1228400e64dfSOhad Ben-Cohen 1229400e64dfSOhad Ben-Cohen /* clean up debugfs trace entries */ 1230400e64dfSOhad Ben-Cohen list_for_each_entry_safe(entry, tmp, &rproc->traces, node) { 1231400e64dfSOhad Ben-Cohen rproc_remove_trace_file(entry->priv); 1232400e64dfSOhad Ben-Cohen rproc->num_traces--; 1233400e64dfSOhad Ben-Cohen list_del(&entry->node); 1234400e64dfSOhad Ben-Cohen kfree(entry); 1235400e64dfSOhad Ben-Cohen } 1236400e64dfSOhad Ben-Cohen 1237400e64dfSOhad Ben-Cohen /* clean up iommu mapping entries */ 1238400e64dfSOhad Ben-Cohen list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) { 1239400e64dfSOhad Ben-Cohen size_t unmapped; 1240400e64dfSOhad Ben-Cohen 1241400e64dfSOhad Ben-Cohen unmapped = iommu_unmap(rproc->domain, entry->da, entry->len); 1242400e64dfSOhad Ben-Cohen if (unmapped != entry->len) { 1243400e64dfSOhad Ben-Cohen /* nothing much to do besides complaining */ 1244e981f6d4SSjur Brændeland dev_err(dev, "failed to unmap %u/%zu\n", entry->len, 1245400e64dfSOhad Ben-Cohen unmapped); 1246400e64dfSOhad Ben-Cohen } 1247400e64dfSOhad Ben-Cohen 1248400e64dfSOhad Ben-Cohen list_del(&entry->node); 1249400e64dfSOhad Ben-Cohen kfree(entry); 1250400e64dfSOhad Ben-Cohen } 1251b6356a01SSuman Anna 1252b6356a01SSuman Anna /* clean up carveout allocations */ 1253b6356a01SSuman Anna list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) { 1254f2e74abfSLoic Pallardy if (entry->release) 1255f2e74abfSLoic Pallardy entry->release(rproc, entry); 1256b6356a01SSuman Anna list_del(&entry->node); 1257b6356a01SSuman Anna kfree(entry); 1258b6356a01SSuman Anna } 1259d81fb32fSBjorn Andersson 1260d81fb32fSBjorn Andersson /* clean up remote vdev entries */ 1261f5bcb353SBjorn Andersson list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node) 12622b45cef5SBjorn Andersson kref_put(&rvdev->refcount, rproc_vdev_release); 12632666ca91SSarangdhar Joshi 12642666ca91SSarangdhar Joshi rproc_coredump_cleanup(rproc); 12652b45cef5SBjorn Andersson } 1266400e64dfSOhad Ben-Cohen 12671efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw) 12681efa30d0SSarangdhar Joshi { 1269a4b24c75SBjorn Andersson struct resource_table *loaded_table; 12701efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 1271a4b24c75SBjorn Andersson int ret; 12721efa30d0SSarangdhar Joshi 12731efa30d0SSarangdhar Joshi /* load the ELF segments to memory */ 12741efa30d0SSarangdhar Joshi ret = rproc_load_segments(rproc, fw); 12751efa30d0SSarangdhar Joshi if (ret) { 12761efa30d0SSarangdhar Joshi dev_err(dev, "Failed to load program segments: %d\n", ret); 12771efa30d0SSarangdhar Joshi return ret; 12781efa30d0SSarangdhar Joshi } 12791efa30d0SSarangdhar Joshi 12801efa30d0SSarangdhar Joshi /* 12811efa30d0SSarangdhar Joshi * The starting device has been given the rproc->cached_table as the 12821efa30d0SSarangdhar Joshi * resource table. The address of the vring along with the other 12831efa30d0SSarangdhar Joshi * allocated resources (carveouts etc) is stored in cached_table. 12841efa30d0SSarangdhar Joshi * In order to pass this information to the remote device we must copy 12851efa30d0SSarangdhar Joshi * this information to device memory. We also update the table_ptr so 12861efa30d0SSarangdhar Joshi * that any subsequent changes will be applied to the loaded version. 12871efa30d0SSarangdhar Joshi */ 12881efa30d0SSarangdhar Joshi loaded_table = rproc_find_loaded_rsc_table(rproc, fw); 12891efa30d0SSarangdhar Joshi if (loaded_table) { 1290a4b24c75SBjorn Andersson memcpy(loaded_table, rproc->cached_table, rproc->table_sz); 12911efa30d0SSarangdhar Joshi rproc->table_ptr = loaded_table; 12921efa30d0SSarangdhar Joshi } 12931efa30d0SSarangdhar Joshi 1294c455daa4SBjorn Andersson ret = rproc_prepare_subdevices(rproc); 1295c455daa4SBjorn Andersson if (ret) { 1296c455daa4SBjorn Andersson dev_err(dev, "failed to prepare subdevices for %s: %d\n", 1297c455daa4SBjorn Andersson rproc->name, ret); 1298f68d51bdSSuman Anna goto reset_table_ptr; 1299c455daa4SBjorn Andersson } 1300c455daa4SBjorn Andersson 13011efa30d0SSarangdhar Joshi /* power up the remote processor */ 13021efa30d0SSarangdhar Joshi ret = rproc->ops->start(rproc); 13031efa30d0SSarangdhar Joshi if (ret) { 13041efa30d0SSarangdhar Joshi dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret); 1305c455daa4SBjorn Andersson goto unprepare_subdevices; 13061efa30d0SSarangdhar Joshi } 13071efa30d0SSarangdhar Joshi 1308618fcff3SBjorn Andersson /* Start any subdevices for the remote processor */ 1309618fcff3SBjorn Andersson ret = rproc_start_subdevices(rproc); 13101efa30d0SSarangdhar Joshi if (ret) { 13111efa30d0SSarangdhar Joshi dev_err(dev, "failed to probe subdevices for %s: %d\n", 13121efa30d0SSarangdhar Joshi rproc->name, ret); 1313c455daa4SBjorn Andersson goto stop_rproc; 13141efa30d0SSarangdhar Joshi } 13151efa30d0SSarangdhar Joshi 13161efa30d0SSarangdhar Joshi rproc->state = RPROC_RUNNING; 13171efa30d0SSarangdhar Joshi 13181efa30d0SSarangdhar Joshi dev_info(dev, "remote processor %s is now up\n", rproc->name); 13191efa30d0SSarangdhar Joshi 13201efa30d0SSarangdhar Joshi return 0; 1321c455daa4SBjorn Andersson 1322c455daa4SBjorn Andersson stop_rproc: 1323c455daa4SBjorn Andersson rproc->ops->stop(rproc); 1324c455daa4SBjorn Andersson unprepare_subdevices: 1325c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1326f68d51bdSSuman Anna reset_table_ptr: 1327f68d51bdSSuman Anna rproc->table_ptr = rproc->cached_table; 1328c455daa4SBjorn Andersson 1329c455daa4SBjorn Andersson return ret; 13301efa30d0SSarangdhar Joshi } 13311efa30d0SSarangdhar Joshi 1332400e64dfSOhad Ben-Cohen /* 1333400e64dfSOhad Ben-Cohen * take a firmware and boot a remote processor with it. 1334400e64dfSOhad Ben-Cohen */ 1335400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw) 1336400e64dfSOhad Ben-Cohen { 1337b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1338400e64dfSOhad Ben-Cohen const char *name = rproc->firmware; 133958b64090SBjorn Andersson int ret; 1340400e64dfSOhad Ben-Cohen 1341400e64dfSOhad Ben-Cohen ret = rproc_fw_sanity_check(rproc, fw); 1342400e64dfSOhad Ben-Cohen if (ret) 1343400e64dfSOhad Ben-Cohen return ret; 1344400e64dfSOhad Ben-Cohen 1345e981f6d4SSjur Brændeland dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size); 1346400e64dfSOhad Ben-Cohen 1347400e64dfSOhad Ben-Cohen /* 1348400e64dfSOhad Ben-Cohen * if enabling an IOMMU isn't relevant for this rproc, this is 1349400e64dfSOhad Ben-Cohen * just a nop 1350400e64dfSOhad Ben-Cohen */ 1351400e64dfSOhad Ben-Cohen ret = rproc_enable_iommu(rproc); 1352400e64dfSOhad Ben-Cohen if (ret) { 1353400e64dfSOhad Ben-Cohen dev_err(dev, "can't enable iommu: %d\n", ret); 1354400e64dfSOhad Ben-Cohen return ret; 1355400e64dfSOhad Ben-Cohen } 1356400e64dfSOhad Ben-Cohen 13573e5f9eb5SSjur Brændeland rproc->bootaddr = rproc_get_boot_addr(rproc, fw); 1358400e64dfSOhad Ben-Cohen 1359c1d35c1aSBjorn Andersson /* Load resource table, core dump segment list etc from the firmware */ 1360c1d35c1aSBjorn Andersson ret = rproc_parse_fw(rproc, fw); 136158b64090SBjorn Andersson if (ret) 136258b64090SBjorn Andersson goto disable_iommu; 1363a0c10687SBjorn Andersson 1364b35d7afcSBjorn Andersson /* reset max_notifyid */ 1365b35d7afcSBjorn Andersson rproc->max_notifyid = -1; 1366b35d7afcSBjorn Andersson 1367c6aed238SLoic Pallardy /* reset handled vdev */ 1368c6aed238SLoic Pallardy rproc->nb_vdev = 0; 1369c6aed238SLoic Pallardy 1370400e64dfSOhad Ben-Cohen /* handle fw resources which are required to boot rproc */ 1371a4b24c75SBjorn Andersson ret = rproc_handle_resources(rproc, rproc_loading_handlers); 1372400e64dfSOhad Ben-Cohen if (ret) { 1373400e64dfSOhad Ben-Cohen dev_err(dev, "Failed to process resources: %d\n", ret); 1374229b85a6SBjorn Andersson goto clean_up_resources; 1375400e64dfSOhad Ben-Cohen } 1376400e64dfSOhad Ben-Cohen 1377d7c51706SLoic Pallardy /* Allocate carveout resources associated to rproc */ 1378d7c51706SLoic Pallardy ret = rproc_alloc_registered_carveouts(rproc); 1379d7c51706SLoic Pallardy if (ret) { 1380d7c51706SLoic Pallardy dev_err(dev, "Failed to allocate associated carveouts: %d\n", 1381d7c51706SLoic Pallardy ret); 1382d7c51706SLoic Pallardy goto clean_up_resources; 1383d7c51706SLoic Pallardy } 1384d7c51706SLoic Pallardy 13851efa30d0SSarangdhar Joshi ret = rproc_start(rproc, fw); 13861efa30d0SSarangdhar Joshi if (ret) 1387229b85a6SBjorn Andersson goto clean_up_resources; 1388400e64dfSOhad Ben-Cohen 1389400e64dfSOhad Ben-Cohen return 0; 1390400e64dfSOhad Ben-Cohen 1391229b85a6SBjorn Andersson clean_up_resources: 1392229b85a6SBjorn Andersson rproc_resource_cleanup(rproc); 1393a0c10687SBjorn Andersson kfree(rproc->cached_table); 1394a0c10687SBjorn Andersson rproc->cached_table = NULL; 1395988d204cSBjorn Andersson rproc->table_ptr = NULL; 139658b64090SBjorn Andersson disable_iommu: 1397400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1398400e64dfSOhad Ben-Cohen return ret; 1399400e64dfSOhad Ben-Cohen } 1400400e64dfSOhad Ben-Cohen 1401400e64dfSOhad Ben-Cohen /* 14025e6533f7SSarangdhar Joshi * take a firmware and boot it up. 1403400e64dfSOhad Ben-Cohen * 1404400e64dfSOhad Ben-Cohen * Note: this function is called asynchronously upon registration of the 1405400e64dfSOhad Ben-Cohen * remote processor (so we must wait until it completes before we try 1406400e64dfSOhad Ben-Cohen * to unregister the device. one other option is just to use kref here, 1407400e64dfSOhad Ben-Cohen * that might be cleaner). 1408400e64dfSOhad Ben-Cohen */ 14095e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context) 1410400e64dfSOhad Ben-Cohen { 1411400e64dfSOhad Ben-Cohen struct rproc *rproc = context; 1412a2b950acSOhad Ben-Cohen 14132bfc311aSBjorn Andersson rproc_boot(rproc); 1414ddf71187SBjorn Andersson 1415400e64dfSOhad Ben-Cohen release_firmware(fw); 1416400e64dfSOhad Ben-Cohen } 1417400e64dfSOhad Ben-Cohen 14185e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc) 141970b85ef8SFernando Guzman Lugo { 142070b85ef8SFernando Guzman Lugo int ret; 142170b85ef8SFernando Guzman Lugo 142270b85ef8SFernando Guzman Lugo /* 142370b85ef8SFernando Guzman Lugo * We're initiating an asynchronous firmware loading, so we can 142470b85ef8SFernando Guzman Lugo * be built-in kernel code, without hanging the boot process. 142570b85ef8SFernando Guzman Lugo */ 142670b85ef8SFernando Guzman Lugo ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG, 142770b85ef8SFernando Guzman Lugo rproc->firmware, &rproc->dev, GFP_KERNEL, 14285e6533f7SSarangdhar Joshi rproc, rproc_auto_boot_callback); 14292099c77dSSarangdhar Joshi if (ret < 0) 143070b85ef8SFernando Guzman Lugo dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret); 143170b85ef8SFernando Guzman Lugo 143270b85ef8SFernando Guzman Lugo return ret; 143370b85ef8SFernando Guzman Lugo } 143470b85ef8SFernando Guzman Lugo 1435880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed) 14361efa30d0SSarangdhar Joshi { 14371efa30d0SSarangdhar Joshi struct device *dev = &rproc->dev; 14381efa30d0SSarangdhar Joshi int ret; 14391efa30d0SSarangdhar Joshi 1440618fcff3SBjorn Andersson /* Stop any subdevices for the remote processor */ 1441618fcff3SBjorn Andersson rproc_stop_subdevices(rproc, crashed); 14421efa30d0SSarangdhar Joshi 14430a8b81cbSBjorn Andersson /* the installed resource table is no longer accessible */ 14440a8b81cbSBjorn Andersson rproc->table_ptr = rproc->cached_table; 14450a8b81cbSBjorn Andersson 14461efa30d0SSarangdhar Joshi /* power off the remote processor */ 14471efa30d0SSarangdhar Joshi ret = rproc->ops->stop(rproc); 14481efa30d0SSarangdhar Joshi if (ret) { 14491efa30d0SSarangdhar Joshi dev_err(dev, "can't stop rproc: %d\n", ret); 14501efa30d0SSarangdhar Joshi return ret; 14511efa30d0SSarangdhar Joshi } 14521efa30d0SSarangdhar Joshi 1453c455daa4SBjorn Andersson rproc_unprepare_subdevices(rproc); 1454c455daa4SBjorn Andersson 14551efa30d0SSarangdhar Joshi rproc->state = RPROC_OFFLINE; 14561efa30d0SSarangdhar Joshi 14571efa30d0SSarangdhar Joshi dev_info(dev, "stopped remote processor %s\n", rproc->name); 14581efa30d0SSarangdhar Joshi 14591efa30d0SSarangdhar Joshi return 0; 14601efa30d0SSarangdhar Joshi } 14611efa30d0SSarangdhar Joshi 146270b85ef8SFernando Guzman Lugo /** 14632666ca91SSarangdhar Joshi * rproc_coredump_add_segment() - add segment of device memory to coredump 14642666ca91SSarangdhar Joshi * @rproc: handle of a remote processor 14652666ca91SSarangdhar Joshi * @da: device address 14662666ca91SSarangdhar Joshi * @size: size of segment 14672666ca91SSarangdhar Joshi * 14682666ca91SSarangdhar Joshi * Add device memory to the list of segments to be included in a coredump for 14692666ca91SSarangdhar Joshi * the remoteproc. 14702666ca91SSarangdhar Joshi * 14712666ca91SSarangdhar Joshi * Return: 0 on success, negative errno on error. 14722666ca91SSarangdhar Joshi */ 14732666ca91SSarangdhar Joshi int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size) 14742666ca91SSarangdhar Joshi { 14752666ca91SSarangdhar Joshi struct rproc_dump_segment *segment; 14762666ca91SSarangdhar Joshi 14772666ca91SSarangdhar Joshi segment = kzalloc(sizeof(*segment), GFP_KERNEL); 14782666ca91SSarangdhar Joshi if (!segment) 14792666ca91SSarangdhar Joshi return -ENOMEM; 14802666ca91SSarangdhar Joshi 14812666ca91SSarangdhar Joshi segment->da = da; 14822666ca91SSarangdhar Joshi segment->size = size; 14832666ca91SSarangdhar Joshi 14842666ca91SSarangdhar Joshi list_add_tail(&segment->node, &rproc->dump_segments); 14852666ca91SSarangdhar Joshi 14862666ca91SSarangdhar Joshi return 0; 14872666ca91SSarangdhar Joshi } 14882666ca91SSarangdhar Joshi EXPORT_SYMBOL(rproc_coredump_add_segment); 14892666ca91SSarangdhar Joshi 14902666ca91SSarangdhar Joshi /** 1491ab8f873bSSibi Sankar * rproc_coredump_add_custom_segment() - add custom coredump segment 1492ab8f873bSSibi Sankar * @rproc: handle of a remote processor 1493ab8f873bSSibi Sankar * @da: device address 1494ab8f873bSSibi Sankar * @size: size of segment 1495ab8f873bSSibi Sankar * @dumpfn: custom dump function called for each segment during coredump 1496ab8f873bSSibi Sankar * @priv: private data 1497ab8f873bSSibi Sankar * 1498ab8f873bSSibi Sankar * Add device memory to the list of segments to be included in the coredump 1499ab8f873bSSibi Sankar * and associate the segment with the given custom dump function and private 1500ab8f873bSSibi Sankar * data. 1501ab8f873bSSibi Sankar * 1502ab8f873bSSibi Sankar * Return: 0 on success, negative errno on error. 1503ab8f873bSSibi Sankar */ 1504ab8f873bSSibi Sankar int rproc_coredump_add_custom_segment(struct rproc *rproc, 1505ab8f873bSSibi Sankar dma_addr_t da, size_t size, 1506ab8f873bSSibi Sankar void (*dumpfn)(struct rproc *rproc, 1507ab8f873bSSibi Sankar struct rproc_dump_segment *segment, 1508ab8f873bSSibi Sankar void *dest), 1509ab8f873bSSibi Sankar void *priv) 1510ab8f873bSSibi Sankar { 1511ab8f873bSSibi Sankar struct rproc_dump_segment *segment; 1512ab8f873bSSibi Sankar 1513ab8f873bSSibi Sankar segment = kzalloc(sizeof(*segment), GFP_KERNEL); 1514ab8f873bSSibi Sankar if (!segment) 1515ab8f873bSSibi Sankar return -ENOMEM; 1516ab8f873bSSibi Sankar 1517ab8f873bSSibi Sankar segment->da = da; 1518ab8f873bSSibi Sankar segment->size = size; 1519ab8f873bSSibi Sankar segment->priv = priv; 1520ab8f873bSSibi Sankar segment->dump = dumpfn; 1521ab8f873bSSibi Sankar 1522ab8f873bSSibi Sankar list_add_tail(&segment->node, &rproc->dump_segments); 1523ab8f873bSSibi Sankar 1524ab8f873bSSibi Sankar return 0; 1525ab8f873bSSibi Sankar } 1526ab8f873bSSibi Sankar EXPORT_SYMBOL(rproc_coredump_add_custom_segment); 1527ab8f873bSSibi Sankar 1528ab8f873bSSibi Sankar /** 15292666ca91SSarangdhar Joshi * rproc_coredump() - perform coredump 15302666ca91SSarangdhar Joshi * @rproc: rproc handle 15312666ca91SSarangdhar Joshi * 15322666ca91SSarangdhar Joshi * This function will generate an ELF header for the registered segments 15332666ca91SSarangdhar Joshi * and create a devcoredump device associated with rproc. 15342666ca91SSarangdhar Joshi */ 15352666ca91SSarangdhar Joshi static void rproc_coredump(struct rproc *rproc) 15362666ca91SSarangdhar Joshi { 15372666ca91SSarangdhar Joshi struct rproc_dump_segment *segment; 15382666ca91SSarangdhar Joshi struct elf32_phdr *phdr; 15392666ca91SSarangdhar Joshi struct elf32_hdr *ehdr; 15402666ca91SSarangdhar Joshi size_t data_size; 15412666ca91SSarangdhar Joshi size_t offset; 15422666ca91SSarangdhar Joshi void *data; 15432666ca91SSarangdhar Joshi void *ptr; 15442666ca91SSarangdhar Joshi int phnum = 0; 15452666ca91SSarangdhar Joshi 15462666ca91SSarangdhar Joshi if (list_empty(&rproc->dump_segments)) 15472666ca91SSarangdhar Joshi return; 15482666ca91SSarangdhar Joshi 15492666ca91SSarangdhar Joshi data_size = sizeof(*ehdr); 15502666ca91SSarangdhar Joshi list_for_each_entry(segment, &rproc->dump_segments, node) { 15512666ca91SSarangdhar Joshi data_size += sizeof(*phdr) + segment->size; 15522666ca91SSarangdhar Joshi 15532666ca91SSarangdhar Joshi phnum++; 15542666ca91SSarangdhar Joshi } 15552666ca91SSarangdhar Joshi 15562666ca91SSarangdhar Joshi data = vmalloc(data_size); 15572666ca91SSarangdhar Joshi if (!data) 15582666ca91SSarangdhar Joshi return; 15592666ca91SSarangdhar Joshi 15602666ca91SSarangdhar Joshi ehdr = data; 15612666ca91SSarangdhar Joshi 15622666ca91SSarangdhar Joshi memset(ehdr, 0, sizeof(*ehdr)); 15632666ca91SSarangdhar Joshi memcpy(ehdr->e_ident, ELFMAG, SELFMAG); 15642666ca91SSarangdhar Joshi ehdr->e_ident[EI_CLASS] = ELFCLASS32; 15652666ca91SSarangdhar Joshi ehdr->e_ident[EI_DATA] = ELFDATA2LSB; 15662666ca91SSarangdhar Joshi ehdr->e_ident[EI_VERSION] = EV_CURRENT; 15672666ca91SSarangdhar Joshi ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE; 15682666ca91SSarangdhar Joshi ehdr->e_type = ET_CORE; 15692666ca91SSarangdhar Joshi ehdr->e_machine = EM_NONE; 15702666ca91SSarangdhar Joshi ehdr->e_version = EV_CURRENT; 15712666ca91SSarangdhar Joshi ehdr->e_entry = rproc->bootaddr; 15722666ca91SSarangdhar Joshi ehdr->e_phoff = sizeof(*ehdr); 15732666ca91SSarangdhar Joshi ehdr->e_ehsize = sizeof(*ehdr); 15742666ca91SSarangdhar Joshi ehdr->e_phentsize = sizeof(*phdr); 15752666ca91SSarangdhar Joshi ehdr->e_phnum = phnum; 15762666ca91SSarangdhar Joshi 15772666ca91SSarangdhar Joshi phdr = data + ehdr->e_phoff; 15782666ca91SSarangdhar Joshi offset = ehdr->e_phoff + sizeof(*phdr) * ehdr->e_phnum; 15792666ca91SSarangdhar Joshi list_for_each_entry(segment, &rproc->dump_segments, node) { 15802666ca91SSarangdhar Joshi memset(phdr, 0, sizeof(*phdr)); 15812666ca91SSarangdhar Joshi phdr->p_type = PT_LOAD; 15822666ca91SSarangdhar Joshi phdr->p_offset = offset; 15832666ca91SSarangdhar Joshi phdr->p_vaddr = segment->da; 15842666ca91SSarangdhar Joshi phdr->p_paddr = segment->da; 15852666ca91SSarangdhar Joshi phdr->p_filesz = segment->size; 15862666ca91SSarangdhar Joshi phdr->p_memsz = segment->size; 15872666ca91SSarangdhar Joshi phdr->p_flags = PF_R | PF_W | PF_X; 15882666ca91SSarangdhar Joshi phdr->p_align = 0; 15892666ca91SSarangdhar Joshi 15903952105dSSibi Sankar if (segment->dump) { 15913952105dSSibi Sankar segment->dump(rproc, segment, data + offset); 15923952105dSSibi Sankar } else { 15932666ca91SSarangdhar Joshi ptr = rproc_da_to_va(rproc, segment->da, segment->size); 15942666ca91SSarangdhar Joshi if (!ptr) { 15952666ca91SSarangdhar Joshi dev_err(&rproc->dev, 15962666ca91SSarangdhar Joshi "invalid coredump segment (%pad, %zu)\n", 15972666ca91SSarangdhar Joshi &segment->da, segment->size); 15982666ca91SSarangdhar Joshi memset(data + offset, 0xff, segment->size); 15992666ca91SSarangdhar Joshi } else { 16002666ca91SSarangdhar Joshi memcpy(data + offset, ptr, segment->size); 16012666ca91SSarangdhar Joshi } 16023952105dSSibi Sankar } 16032666ca91SSarangdhar Joshi 16042666ca91SSarangdhar Joshi offset += phdr->p_filesz; 16052666ca91SSarangdhar Joshi phdr++; 16062666ca91SSarangdhar Joshi } 16072666ca91SSarangdhar Joshi 16082666ca91SSarangdhar Joshi dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL); 16092666ca91SSarangdhar Joshi } 16102666ca91SSarangdhar Joshi 16112666ca91SSarangdhar Joshi /** 161270b85ef8SFernando Guzman Lugo * rproc_trigger_recovery() - recover a remoteproc 161370b85ef8SFernando Guzman Lugo * @rproc: the remote processor 161470b85ef8SFernando Guzman Lugo * 161556324d7aSAnna, Suman * The recovery is done by resetting all the virtio devices, that way all the 161670b85ef8SFernando Guzman Lugo * rpmsg drivers will be reseted along with the remote processor making the 161770b85ef8SFernando Guzman Lugo * remoteproc functional again. 161870b85ef8SFernando Guzman Lugo * 161970b85ef8SFernando Guzman Lugo * This function can sleep, so it cannot be called from atomic context. 162070b85ef8SFernando Guzman Lugo */ 162170b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc) 162270b85ef8SFernando Guzman Lugo { 16237e83cab8SSarangdhar Joshi const struct firmware *firmware_p; 16247e83cab8SSarangdhar Joshi struct device *dev = &rproc->dev; 16257e83cab8SSarangdhar Joshi int ret; 16267e83cab8SSarangdhar Joshi 16277e83cab8SSarangdhar Joshi dev_err(dev, "recovering %s\n", rproc->name); 162870b85ef8SFernando Guzman Lugo 16297e83cab8SSarangdhar Joshi ret = mutex_lock_interruptible(&rproc->lock); 16307e83cab8SSarangdhar Joshi if (ret) 16317e83cab8SSarangdhar Joshi return ret; 16327e83cab8SSarangdhar Joshi 1633fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, true); 16347e83cab8SSarangdhar Joshi if (ret) 16357e83cab8SSarangdhar Joshi goto unlock_mutex; 1636ddf71187SBjorn Andersson 16372666ca91SSarangdhar Joshi /* generate coredump */ 16382666ca91SSarangdhar Joshi rproc_coredump(rproc); 16392666ca91SSarangdhar Joshi 16407e83cab8SSarangdhar Joshi /* load firmware */ 16417e83cab8SSarangdhar Joshi ret = request_firmware(&firmware_p, rproc->firmware, dev); 16427e83cab8SSarangdhar Joshi if (ret < 0) { 16437e83cab8SSarangdhar Joshi dev_err(dev, "request_firmware failed: %d\n", ret); 16447e83cab8SSarangdhar Joshi goto unlock_mutex; 16457e83cab8SSarangdhar Joshi } 1646ddf71187SBjorn Andersson 16477e83cab8SSarangdhar Joshi /* boot the remote processor up again */ 16487e83cab8SSarangdhar Joshi ret = rproc_start(rproc, firmware_p); 16497e83cab8SSarangdhar Joshi 16507e83cab8SSarangdhar Joshi release_firmware(firmware_p); 16517e83cab8SSarangdhar Joshi 16527e83cab8SSarangdhar Joshi unlock_mutex: 16537e83cab8SSarangdhar Joshi mutex_unlock(&rproc->lock); 16547e83cab8SSarangdhar Joshi return ret; 165570b85ef8SFernando Guzman Lugo } 165670b85ef8SFernando Guzman Lugo 1657400e64dfSOhad Ben-Cohen /** 16588afd519cSFernando Guzman Lugo * rproc_crash_handler_work() - handle a crash 16598afd519cSFernando Guzman Lugo * 16608afd519cSFernando Guzman Lugo * This function needs to handle everything related to a crash, like cpu 16618afd519cSFernando Guzman Lugo * registers and stack dump, information to help to debug the fatal error, etc. 16628afd519cSFernando Guzman Lugo */ 16638afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work) 16648afd519cSFernando Guzman Lugo { 16658afd519cSFernando Guzman Lugo struct rproc *rproc = container_of(work, struct rproc, crash_handler); 16668afd519cSFernando Guzman Lugo struct device *dev = &rproc->dev; 16678afd519cSFernando Guzman Lugo 16688afd519cSFernando Guzman Lugo dev_dbg(dev, "enter %s\n", __func__); 16698afd519cSFernando Guzman Lugo 16708afd519cSFernando Guzman Lugo mutex_lock(&rproc->lock); 16718afd519cSFernando Guzman Lugo 16728afd519cSFernando Guzman Lugo if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) { 16738afd519cSFernando Guzman Lugo /* handle only the first crash detected */ 16748afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 16758afd519cSFernando Guzman Lugo return; 16768afd519cSFernando Guzman Lugo } 16778afd519cSFernando Guzman Lugo 16788afd519cSFernando Guzman Lugo rproc->state = RPROC_CRASHED; 16798afd519cSFernando Guzman Lugo dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt, 16808afd519cSFernando Guzman Lugo rproc->name); 16818afd519cSFernando Guzman Lugo 16828afd519cSFernando Guzman Lugo mutex_unlock(&rproc->lock); 16838afd519cSFernando Guzman Lugo 16842e37abb8SFernando Guzman Lugo if (!rproc->recovery_disabled) 168570b85ef8SFernando Guzman Lugo rproc_trigger_recovery(rproc); 16868afd519cSFernando Guzman Lugo } 16878afd519cSFernando Guzman Lugo 16888afd519cSFernando Guzman Lugo /** 16891b0ef906SSuman Anna * rproc_boot() - boot a remote processor 1690400e64dfSOhad Ben-Cohen * @rproc: handle of a remote processor 1691400e64dfSOhad Ben-Cohen * 1692400e64dfSOhad Ben-Cohen * Boot a remote processor (i.e. load its firmware, power it on, ...). 1693400e64dfSOhad Ben-Cohen * 1694400e64dfSOhad Ben-Cohen * If the remote processor is already powered on, this function immediately 1695400e64dfSOhad Ben-Cohen * returns (successfully). 1696400e64dfSOhad Ben-Cohen * 1697400e64dfSOhad Ben-Cohen * Returns 0 on success, and an appropriate error value otherwise. 1698400e64dfSOhad Ben-Cohen */ 16991b0ef906SSuman Anna int rproc_boot(struct rproc *rproc) 1700400e64dfSOhad Ben-Cohen { 1701400e64dfSOhad Ben-Cohen const struct firmware *firmware_p; 1702400e64dfSOhad Ben-Cohen struct device *dev; 1703400e64dfSOhad Ben-Cohen int ret; 1704400e64dfSOhad Ben-Cohen 1705400e64dfSOhad Ben-Cohen if (!rproc) { 1706400e64dfSOhad Ben-Cohen pr_err("invalid rproc handle\n"); 1707400e64dfSOhad Ben-Cohen return -EINVAL; 1708400e64dfSOhad Ben-Cohen } 1709400e64dfSOhad Ben-Cohen 1710b5ab5e24SOhad Ben-Cohen dev = &rproc->dev; 1711400e64dfSOhad Ben-Cohen 1712400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1713400e64dfSOhad Ben-Cohen if (ret) { 1714400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1715400e64dfSOhad Ben-Cohen return ret; 1716400e64dfSOhad Ben-Cohen } 1717400e64dfSOhad Ben-Cohen 17182099c77dSSarangdhar Joshi if (rproc->state == RPROC_DELETED) { 17192099c77dSSarangdhar Joshi ret = -ENODEV; 17202099c77dSSarangdhar Joshi dev_err(dev, "can't boot deleted rproc %s\n", rproc->name); 17212099c77dSSarangdhar Joshi goto unlock_mutex; 17222099c77dSSarangdhar Joshi } 17232099c77dSSarangdhar Joshi 1724400e64dfSOhad Ben-Cohen /* skip the boot process if rproc is already powered up */ 1725400e64dfSOhad Ben-Cohen if (atomic_inc_return(&rproc->power) > 1) { 1726400e64dfSOhad Ben-Cohen ret = 0; 1727400e64dfSOhad Ben-Cohen goto unlock_mutex; 1728400e64dfSOhad Ben-Cohen } 1729400e64dfSOhad Ben-Cohen 1730400e64dfSOhad Ben-Cohen dev_info(dev, "powering up %s\n", rproc->name); 1731400e64dfSOhad Ben-Cohen 1732400e64dfSOhad Ben-Cohen /* load firmware */ 1733400e64dfSOhad Ben-Cohen ret = request_firmware(&firmware_p, rproc->firmware, dev); 1734400e64dfSOhad Ben-Cohen if (ret < 0) { 1735400e64dfSOhad Ben-Cohen dev_err(dev, "request_firmware failed: %d\n", ret); 1736400e64dfSOhad Ben-Cohen goto downref_rproc; 1737400e64dfSOhad Ben-Cohen } 1738400e64dfSOhad Ben-Cohen 1739400e64dfSOhad Ben-Cohen ret = rproc_fw_boot(rproc, firmware_p); 1740400e64dfSOhad Ben-Cohen 1741400e64dfSOhad Ben-Cohen release_firmware(firmware_p); 1742400e64dfSOhad Ben-Cohen 1743400e64dfSOhad Ben-Cohen downref_rproc: 1744fbb6aacbSBjorn Andersson if (ret) 1745400e64dfSOhad Ben-Cohen atomic_dec(&rproc->power); 1746400e64dfSOhad Ben-Cohen unlock_mutex: 1747400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1748400e64dfSOhad Ben-Cohen return ret; 1749400e64dfSOhad Ben-Cohen } 1750400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot); 1751400e64dfSOhad Ben-Cohen 1752400e64dfSOhad Ben-Cohen /** 1753400e64dfSOhad Ben-Cohen * rproc_shutdown() - power off the remote processor 1754400e64dfSOhad Ben-Cohen * @rproc: the remote processor 1755400e64dfSOhad Ben-Cohen * 1756400e64dfSOhad Ben-Cohen * Power off a remote processor (previously booted with rproc_boot()). 1757400e64dfSOhad Ben-Cohen * 1758400e64dfSOhad Ben-Cohen * In case @rproc is still being used by an additional user(s), then 1759400e64dfSOhad Ben-Cohen * this function will just decrement the power refcount and exit, 1760400e64dfSOhad Ben-Cohen * without really powering off the device. 1761400e64dfSOhad Ben-Cohen * 1762400e64dfSOhad Ben-Cohen * Every call to rproc_boot() must (eventually) be accompanied by a call 1763400e64dfSOhad Ben-Cohen * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug. 1764400e64dfSOhad Ben-Cohen * 1765400e64dfSOhad Ben-Cohen * Notes: 1766400e64dfSOhad Ben-Cohen * - we're not decrementing the rproc's refcount, only the power refcount. 1767400e64dfSOhad Ben-Cohen * which means that the @rproc handle stays valid even after rproc_shutdown() 1768400e64dfSOhad Ben-Cohen * returns, and users can still use it with a subsequent rproc_boot(), if 1769400e64dfSOhad Ben-Cohen * needed. 1770400e64dfSOhad Ben-Cohen */ 1771400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc) 1772400e64dfSOhad Ben-Cohen { 1773b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 1774400e64dfSOhad Ben-Cohen int ret; 1775400e64dfSOhad Ben-Cohen 1776400e64dfSOhad Ben-Cohen ret = mutex_lock_interruptible(&rproc->lock); 1777400e64dfSOhad Ben-Cohen if (ret) { 1778400e64dfSOhad Ben-Cohen dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret); 1779400e64dfSOhad Ben-Cohen return; 1780400e64dfSOhad Ben-Cohen } 1781400e64dfSOhad Ben-Cohen 1782400e64dfSOhad Ben-Cohen /* if the remote proc is still needed, bail out */ 1783400e64dfSOhad Ben-Cohen if (!atomic_dec_and_test(&rproc->power)) 1784400e64dfSOhad Ben-Cohen goto out; 1785400e64dfSOhad Ben-Cohen 1786fcd58037SArnaud Pouliquen ret = rproc_stop(rproc, false); 1787400e64dfSOhad Ben-Cohen if (ret) { 1788400e64dfSOhad Ben-Cohen atomic_inc(&rproc->power); 1789400e64dfSOhad Ben-Cohen goto out; 1790400e64dfSOhad Ben-Cohen } 1791400e64dfSOhad Ben-Cohen 1792400e64dfSOhad Ben-Cohen /* clean up all acquired resources */ 1793400e64dfSOhad Ben-Cohen rproc_resource_cleanup(rproc); 1794400e64dfSOhad Ben-Cohen 1795400e64dfSOhad Ben-Cohen rproc_disable_iommu(rproc); 1796400e64dfSOhad Ben-Cohen 1797988d204cSBjorn Andersson /* Free the copy of the resource table */ 1798a0c10687SBjorn Andersson kfree(rproc->cached_table); 1799a0c10687SBjorn Andersson rproc->cached_table = NULL; 1800988d204cSBjorn Andersson rproc->table_ptr = NULL; 1801400e64dfSOhad Ben-Cohen out: 1802400e64dfSOhad Ben-Cohen mutex_unlock(&rproc->lock); 1803400e64dfSOhad Ben-Cohen } 1804400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown); 1805400e64dfSOhad Ben-Cohen 1806400e64dfSOhad Ben-Cohen /** 1807fec47d86SDave Gerlach * rproc_get_by_phandle() - find a remote processor by phandle 1808fec47d86SDave Gerlach * @phandle: phandle to the rproc 1809fec47d86SDave Gerlach * 1810fec47d86SDave Gerlach * Finds an rproc handle using the remote processor's phandle, and then 1811fec47d86SDave Gerlach * return a handle to the rproc. 1812fec47d86SDave Gerlach * 1813fec47d86SDave Gerlach * This function increments the remote processor's refcount, so always 1814fec47d86SDave Gerlach * use rproc_put() to decrement it back once rproc isn't needed anymore. 1815fec47d86SDave Gerlach * 1816fec47d86SDave Gerlach * Returns the rproc handle on success, and NULL on failure. 1817fec47d86SDave Gerlach */ 18188de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF 1819fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle) 1820fec47d86SDave Gerlach { 1821fec47d86SDave Gerlach struct rproc *rproc = NULL, *r; 1822fec47d86SDave Gerlach struct device_node *np; 1823fec47d86SDave Gerlach 1824fec47d86SDave Gerlach np = of_find_node_by_phandle(phandle); 1825fec47d86SDave Gerlach if (!np) 1826fec47d86SDave Gerlach return NULL; 1827fec47d86SDave Gerlach 1828fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 1829fec47d86SDave Gerlach list_for_each_entry(r, &rproc_list, node) { 1830fec47d86SDave Gerlach if (r->dev.parent && r->dev.parent->of_node == np) { 1831fbb6aacbSBjorn Andersson /* prevent underlying implementation from being removed */ 1832fbb6aacbSBjorn Andersson if (!try_module_get(r->dev.parent->driver->owner)) { 1833fbb6aacbSBjorn Andersson dev_err(&r->dev, "can't get owner\n"); 1834fbb6aacbSBjorn Andersson break; 1835fbb6aacbSBjorn Andersson } 1836fbb6aacbSBjorn Andersson 1837fec47d86SDave Gerlach rproc = r; 1838fec47d86SDave Gerlach get_device(&rproc->dev); 1839fec47d86SDave Gerlach break; 1840fec47d86SDave Gerlach } 1841fec47d86SDave Gerlach } 1842fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 1843fec47d86SDave Gerlach 1844fec47d86SDave Gerlach of_node_put(np); 1845fec47d86SDave Gerlach 1846fec47d86SDave Gerlach return rproc; 1847fec47d86SDave Gerlach } 18488de3dbd0SOhad Ben-Cohen #else 18498de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle) 18508de3dbd0SOhad Ben-Cohen { 18518de3dbd0SOhad Ben-Cohen return NULL; 18528de3dbd0SOhad Ben-Cohen } 18538de3dbd0SOhad Ben-Cohen #endif 1854fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle); 1855fec47d86SDave Gerlach 1856fec47d86SDave Gerlach /** 1857160e7c84SOhad Ben-Cohen * rproc_add() - register a remote processor 1858400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle to register 1859400e64dfSOhad Ben-Cohen * 1860400e64dfSOhad Ben-Cohen * Registers @rproc with the remoteproc framework, after it has been 1861400e64dfSOhad Ben-Cohen * allocated with rproc_alloc(). 1862400e64dfSOhad Ben-Cohen * 1863400e64dfSOhad Ben-Cohen * This is called by the platform-specific rproc implementation, whenever 1864400e64dfSOhad Ben-Cohen * a new remote processor device is probed. 1865400e64dfSOhad Ben-Cohen * 1866400e64dfSOhad Ben-Cohen * Returns 0 on success and an appropriate error code otherwise. 1867400e64dfSOhad Ben-Cohen * 1868400e64dfSOhad Ben-Cohen * Note: this function initiates an asynchronous firmware loading 1869400e64dfSOhad Ben-Cohen * context, which will look for virtio devices supported by the rproc's 1870400e64dfSOhad Ben-Cohen * firmware. 1871400e64dfSOhad Ben-Cohen * 1872400e64dfSOhad Ben-Cohen * If found, those virtio devices will be created and added, so as a result 18737a186941SOhad Ben-Cohen * of registering this remote processor, additional virtio drivers might be 1874400e64dfSOhad Ben-Cohen * probed. 1875400e64dfSOhad Ben-Cohen */ 1876160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc) 1877400e64dfSOhad Ben-Cohen { 1878b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 187970b85ef8SFernando Guzman Lugo int ret; 1880400e64dfSOhad Ben-Cohen 1881b5ab5e24SOhad Ben-Cohen ret = device_add(dev); 1882b5ab5e24SOhad Ben-Cohen if (ret < 0) 1883b5ab5e24SOhad Ben-Cohen return ret; 1884400e64dfSOhad Ben-Cohen 1885b5ab5e24SOhad Ben-Cohen dev_info(dev, "%s is available\n", rproc->name); 1886400e64dfSOhad Ben-Cohen 1887400e64dfSOhad Ben-Cohen /* create debugfs entries */ 1888400e64dfSOhad Ben-Cohen rproc_create_debug_dir(rproc); 18897a20c64dSSarangdhar Joshi 18907a20c64dSSarangdhar Joshi /* if rproc is marked always-on, request it to boot */ 18917a20c64dSSarangdhar Joshi if (rproc->auto_boot) { 18925e6533f7SSarangdhar Joshi ret = rproc_trigger_auto_boot(rproc); 1893d2e12e66SDave Gerlach if (ret < 0) 1894d2e12e66SDave Gerlach return ret; 18957a20c64dSSarangdhar Joshi } 1896400e64dfSOhad Ben-Cohen 1897d2e12e66SDave Gerlach /* expose to rproc_get_by_phandle users */ 1898d2e12e66SDave Gerlach mutex_lock(&rproc_list_mutex); 1899d2e12e66SDave Gerlach list_add(&rproc->node, &rproc_list); 1900d2e12e66SDave Gerlach mutex_unlock(&rproc_list_mutex); 1901d2e12e66SDave Gerlach 1902d2e12e66SDave Gerlach return 0; 1903400e64dfSOhad Ben-Cohen } 1904160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add); 1905400e64dfSOhad Ben-Cohen 1906400e64dfSOhad Ben-Cohen /** 1907b5ab5e24SOhad Ben-Cohen * rproc_type_release() - release a remote processor instance 1908b5ab5e24SOhad Ben-Cohen * @dev: the rproc's device 1909b5ab5e24SOhad Ben-Cohen * 1910b5ab5e24SOhad Ben-Cohen * This function should _never_ be called directly. 1911b5ab5e24SOhad Ben-Cohen * 1912b5ab5e24SOhad Ben-Cohen * It will be called by the driver core when no one holds a valid pointer 1913b5ab5e24SOhad Ben-Cohen * to @dev anymore. 1914b5ab5e24SOhad Ben-Cohen */ 1915b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev) 1916b5ab5e24SOhad Ben-Cohen { 1917b5ab5e24SOhad Ben-Cohen struct rproc *rproc = container_of(dev, struct rproc, dev); 1918b5ab5e24SOhad Ben-Cohen 19197183a2a7SOhad Ben-Cohen dev_info(&rproc->dev, "releasing %s\n", rproc->name); 19207183a2a7SOhad Ben-Cohen 1921b5ab5e24SOhad Ben-Cohen idr_destroy(&rproc->notifyids); 1922b5ab5e24SOhad Ben-Cohen 1923b5ab5e24SOhad Ben-Cohen if (rproc->index >= 0) 1924b5ab5e24SOhad Ben-Cohen ida_simple_remove(&rproc_dev_index, rproc->index); 1925b5ab5e24SOhad Ben-Cohen 19260f57dc6aSMatt Redfearn kfree(rproc->firmware); 1927fb98e2bdSBjorn Andersson kfree(rproc->ops); 1928b5ab5e24SOhad Ben-Cohen kfree(rproc); 1929b5ab5e24SOhad Ben-Cohen } 1930b5ab5e24SOhad Ben-Cohen 1931c42ca04dSBhumika Goyal static const struct device_type rproc_type = { 1932b5ab5e24SOhad Ben-Cohen .name = "remoteproc", 1933b5ab5e24SOhad Ben-Cohen .release = rproc_type_release, 1934b5ab5e24SOhad Ben-Cohen }; 1935400e64dfSOhad Ben-Cohen 1936400e64dfSOhad Ben-Cohen /** 1937400e64dfSOhad Ben-Cohen * rproc_alloc() - allocate a remote processor handle 1938400e64dfSOhad Ben-Cohen * @dev: the underlying device 1939400e64dfSOhad Ben-Cohen * @name: name of this remote processor 1940400e64dfSOhad Ben-Cohen * @ops: platform-specific handlers (mainly start/stop) 19418b4aec9aSRobert Tivy * @firmware: name of firmware file to load, can be NULL 1942400e64dfSOhad Ben-Cohen * @len: length of private data needed by the rproc driver (in bytes) 1943400e64dfSOhad Ben-Cohen * 1944400e64dfSOhad Ben-Cohen * Allocates a new remote processor handle, but does not register 19458b4aec9aSRobert Tivy * it yet. if @firmware is NULL, a default name is used. 1946400e64dfSOhad Ben-Cohen * 1947400e64dfSOhad Ben-Cohen * This function should be used by rproc implementations during initialization 1948400e64dfSOhad Ben-Cohen * of the remote processor. 1949400e64dfSOhad Ben-Cohen * 1950400e64dfSOhad Ben-Cohen * After creating an rproc handle using this function, and when ready, 1951160e7c84SOhad Ben-Cohen * implementations should then call rproc_add() to complete 1952400e64dfSOhad Ben-Cohen * the registration of the remote processor. 1953400e64dfSOhad Ben-Cohen * 1954400e64dfSOhad Ben-Cohen * On success the new rproc is returned, and on failure, NULL. 1955400e64dfSOhad Ben-Cohen * 1956400e64dfSOhad Ben-Cohen * Note: _never_ directly deallocate @rproc, even if it was not registered 1957433c0e04SBjorn Andersson * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free(). 1958400e64dfSOhad Ben-Cohen */ 1959400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name, 1960400e64dfSOhad Ben-Cohen const struct rproc_ops *ops, 1961400e64dfSOhad Ben-Cohen const char *firmware, int len) 1962400e64dfSOhad Ben-Cohen { 1963400e64dfSOhad Ben-Cohen struct rproc *rproc; 19648b4aec9aSRobert Tivy char *p, *template = "rproc-%s-fw"; 19650f57dc6aSMatt Redfearn int name_len; 1966400e64dfSOhad Ben-Cohen 1967400e64dfSOhad Ben-Cohen if (!dev || !name || !ops) 1968400e64dfSOhad Ben-Cohen return NULL; 1969400e64dfSOhad Ben-Cohen 19700f57dc6aSMatt Redfearn if (!firmware) { 19718b4aec9aSRobert Tivy /* 19728b4aec9aSRobert Tivy * If the caller didn't pass in a firmware name then 19730f57dc6aSMatt Redfearn * construct a default name. 19748b4aec9aSRobert Tivy */ 19758b4aec9aSRobert Tivy name_len = strlen(name) + strlen(template) - 2 + 1; 19760f57dc6aSMatt Redfearn p = kmalloc(name_len, GFP_KERNEL); 19770f57dc6aSMatt Redfearn if (!p) 1978400e64dfSOhad Ben-Cohen return NULL; 19798b4aec9aSRobert Tivy snprintf(p, name_len, template, name); 19808b4aec9aSRobert Tivy } else { 19810f57dc6aSMatt Redfearn p = kstrdup(firmware, GFP_KERNEL); 19820f57dc6aSMatt Redfearn if (!p) 19830f57dc6aSMatt Redfearn return NULL; 19840f57dc6aSMatt Redfearn } 19850f57dc6aSMatt Redfearn 19860f57dc6aSMatt Redfearn rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL); 19870f57dc6aSMatt Redfearn if (!rproc) { 19880f57dc6aSMatt Redfearn kfree(p); 19890f57dc6aSMatt Redfearn return NULL; 19908b4aec9aSRobert Tivy } 19918b4aec9aSRobert Tivy 1992fb98e2bdSBjorn Andersson rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); 1993fb98e2bdSBjorn Andersson if (!rproc->ops) { 1994fb98e2bdSBjorn Andersson kfree(p); 1995fb98e2bdSBjorn Andersson kfree(rproc); 1996fb98e2bdSBjorn Andersson return NULL; 1997fb98e2bdSBjorn Andersson } 1998fb98e2bdSBjorn Andersson 19998b4aec9aSRobert Tivy rproc->firmware = p; 2000400e64dfSOhad Ben-Cohen rproc->name = name; 2001400e64dfSOhad Ben-Cohen rproc->priv = &rproc[1]; 2002ddf71187SBjorn Andersson rproc->auto_boot = true; 2003400e64dfSOhad Ben-Cohen 2004b5ab5e24SOhad Ben-Cohen device_initialize(&rproc->dev); 2005b5ab5e24SOhad Ben-Cohen rproc->dev.parent = dev; 2006b5ab5e24SOhad Ben-Cohen rproc->dev.type = &rproc_type; 20072aefbef0SMatt Redfearn rproc->dev.class = &rproc_class; 20087c89717fSBjorn Andersson rproc->dev.driver_data = rproc; 2009b5ab5e24SOhad Ben-Cohen 2010b5ab5e24SOhad Ben-Cohen /* Assign a unique device index and name */ 2011b5ab5e24SOhad Ben-Cohen rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL); 2012b5ab5e24SOhad Ben-Cohen if (rproc->index < 0) { 2013b5ab5e24SOhad Ben-Cohen dev_err(dev, "ida_simple_get failed: %d\n", rproc->index); 2014b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 2015b5ab5e24SOhad Ben-Cohen return NULL; 2016b5ab5e24SOhad Ben-Cohen } 2017b5ab5e24SOhad Ben-Cohen 2018b5ab5e24SOhad Ben-Cohen dev_set_name(&rproc->dev, "remoteproc%d", rproc->index); 2019b5ab5e24SOhad Ben-Cohen 2020400e64dfSOhad Ben-Cohen atomic_set(&rproc->power, 0); 2021400e64dfSOhad Ben-Cohen 20220f21f9ccSBjorn Andersson /* Default to ELF loader if no load function is specified */ 20230f21f9ccSBjorn Andersson if (!rproc->ops->load) { 20240f21f9ccSBjorn Andersson rproc->ops->load = rproc_elf_load_segments; 2025c1d35c1aSBjorn Andersson rproc->ops->parse_fw = rproc_elf_load_rsc_table; 20260f21f9ccSBjorn Andersson rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table; 20270f21f9ccSBjorn Andersson rproc->ops->sanity_check = rproc_elf_sanity_check; 20280f21f9ccSBjorn Andersson rproc->ops->get_boot_addr = rproc_elf_get_boot_addr; 20290f21f9ccSBjorn Andersson } 2030400e64dfSOhad Ben-Cohen 2031400e64dfSOhad Ben-Cohen mutex_init(&rproc->lock); 2032400e64dfSOhad Ben-Cohen 20337a186941SOhad Ben-Cohen idr_init(&rproc->notifyids); 20347a186941SOhad Ben-Cohen 2035400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->carveouts); 2036400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->mappings); 2037400e64dfSOhad Ben-Cohen INIT_LIST_HEAD(&rproc->traces); 20387a186941SOhad Ben-Cohen INIT_LIST_HEAD(&rproc->rvdevs); 20397bdc9650SBjorn Andersson INIT_LIST_HEAD(&rproc->subdevs); 20402666ca91SSarangdhar Joshi INIT_LIST_HEAD(&rproc->dump_segments); 2041400e64dfSOhad Ben-Cohen 20428afd519cSFernando Guzman Lugo INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work); 20438afd519cSFernando Guzman Lugo 2044400e64dfSOhad Ben-Cohen rproc->state = RPROC_OFFLINE; 2045400e64dfSOhad Ben-Cohen 2046400e64dfSOhad Ben-Cohen return rproc; 2047400e64dfSOhad Ben-Cohen } 2048400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc); 2049400e64dfSOhad Ben-Cohen 2050400e64dfSOhad Ben-Cohen /** 2051433c0e04SBjorn Andersson * rproc_free() - unroll rproc_alloc() 2052433c0e04SBjorn Andersson * @rproc: the remote processor handle 2053433c0e04SBjorn Andersson * 2054433c0e04SBjorn Andersson * This function decrements the rproc dev refcount. 2055433c0e04SBjorn Andersson * 2056433c0e04SBjorn Andersson * If no one holds any reference to rproc anymore, then its refcount would 2057433c0e04SBjorn Andersson * now drop to zero, and it would be freed. 2058433c0e04SBjorn Andersson */ 2059433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc) 2060433c0e04SBjorn Andersson { 2061433c0e04SBjorn Andersson put_device(&rproc->dev); 2062433c0e04SBjorn Andersson } 2063433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free); 2064433c0e04SBjorn Andersson 2065433c0e04SBjorn Andersson /** 2066433c0e04SBjorn Andersson * rproc_put() - release rproc reference 2067400e64dfSOhad Ben-Cohen * @rproc: the remote processor handle 2068400e64dfSOhad Ben-Cohen * 2069c6b5a276SOhad Ben-Cohen * This function decrements the rproc dev refcount. 2070400e64dfSOhad Ben-Cohen * 2071c6b5a276SOhad Ben-Cohen * If no one holds any reference to rproc anymore, then its refcount would 2072c6b5a276SOhad Ben-Cohen * now drop to zero, and it would be freed. 2073400e64dfSOhad Ben-Cohen */ 2074160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc) 2075400e64dfSOhad Ben-Cohen { 2076fbb6aacbSBjorn Andersson module_put(rproc->dev.parent->driver->owner); 2077b5ab5e24SOhad Ben-Cohen put_device(&rproc->dev); 2078400e64dfSOhad Ben-Cohen } 2079160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put); 2080400e64dfSOhad Ben-Cohen 2081400e64dfSOhad Ben-Cohen /** 2082160e7c84SOhad Ben-Cohen * rproc_del() - unregister a remote processor 2083400e64dfSOhad Ben-Cohen * @rproc: rproc handle to unregister 2084400e64dfSOhad Ben-Cohen * 2085400e64dfSOhad Ben-Cohen * This function should be called when the platform specific rproc 2086400e64dfSOhad Ben-Cohen * implementation decides to remove the rproc device. it should 2087160e7c84SOhad Ben-Cohen * _only_ be called if a previous invocation of rproc_add() 2088400e64dfSOhad Ben-Cohen * has completed successfully. 2089400e64dfSOhad Ben-Cohen * 2090160e7c84SOhad Ben-Cohen * After rproc_del() returns, @rproc isn't freed yet, because 2091c6b5a276SOhad Ben-Cohen * of the outstanding reference created by rproc_alloc. To decrement that 2092433c0e04SBjorn Andersson * one last refcount, one still needs to call rproc_free(). 2093400e64dfSOhad Ben-Cohen * 2094400e64dfSOhad Ben-Cohen * Returns 0 on success and -EINVAL if @rproc isn't valid. 2095400e64dfSOhad Ben-Cohen */ 2096160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc) 2097400e64dfSOhad Ben-Cohen { 2098400e64dfSOhad Ben-Cohen if (!rproc) 2099400e64dfSOhad Ben-Cohen return -EINVAL; 2100400e64dfSOhad Ben-Cohen 2101ddf71187SBjorn Andersson /* if rproc is marked always-on, rproc_add() booted it */ 2102ddf71187SBjorn Andersson /* TODO: make sure this works with rproc->power > 1 */ 2103ddf71187SBjorn Andersson if (rproc->auto_boot) 2104ddf71187SBjorn Andersson rproc_shutdown(rproc); 2105ddf71187SBjorn Andersson 21062099c77dSSarangdhar Joshi mutex_lock(&rproc->lock); 21072099c77dSSarangdhar Joshi rproc->state = RPROC_DELETED; 21082099c77dSSarangdhar Joshi mutex_unlock(&rproc->lock); 21092099c77dSSarangdhar Joshi 2110b003d45bSSarangdhar Joshi rproc_delete_debug_dir(rproc); 2111b003d45bSSarangdhar Joshi 2112fec47d86SDave Gerlach /* the rproc is downref'ed as soon as it's removed from the klist */ 2113fec47d86SDave Gerlach mutex_lock(&rproc_list_mutex); 2114fec47d86SDave Gerlach list_del(&rproc->node); 2115fec47d86SDave Gerlach mutex_unlock(&rproc_list_mutex); 2116fec47d86SDave Gerlach 2117b5ab5e24SOhad Ben-Cohen device_del(&rproc->dev); 2118400e64dfSOhad Ben-Cohen 2119400e64dfSOhad Ben-Cohen return 0; 2120400e64dfSOhad Ben-Cohen } 2121160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del); 2122400e64dfSOhad Ben-Cohen 21238afd519cSFernando Guzman Lugo /** 21247bdc9650SBjorn Andersson * rproc_add_subdev() - add a subdevice to a remoteproc 21257bdc9650SBjorn Andersson * @rproc: rproc handle to add the subdevice to 21267bdc9650SBjorn Andersson * @subdev: subdev handle to register 21274902676fSBjorn Andersson * 21284902676fSBjorn Andersson * Caller is responsible for populating optional subdevice function pointers. 21297bdc9650SBjorn Andersson */ 21304902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 21317bdc9650SBjorn Andersson { 21327bdc9650SBjorn Andersson list_add_tail(&subdev->node, &rproc->subdevs); 21337bdc9650SBjorn Andersson } 21347bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev); 21357bdc9650SBjorn Andersson 21367bdc9650SBjorn Andersson /** 21377bdc9650SBjorn Andersson * rproc_remove_subdev() - remove a subdevice from a remoteproc 21387bdc9650SBjorn Andersson * @rproc: rproc handle to remove the subdevice from 21397bdc9650SBjorn Andersson * @subdev: subdev handle, previously registered with rproc_add_subdev() 21407bdc9650SBjorn Andersson */ 21417bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev) 21427bdc9650SBjorn Andersson { 21437bdc9650SBjorn Andersson list_del(&subdev->node); 21447bdc9650SBjorn Andersson } 21457bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev); 21467bdc9650SBjorn Andersson 21477bdc9650SBjorn Andersson /** 21487c89717fSBjorn Andersson * rproc_get_by_child() - acquire rproc handle of @dev's ancestor 21497c89717fSBjorn Andersson * @dev: child device to find ancestor of 21507c89717fSBjorn Andersson * 21517c89717fSBjorn Andersson * Returns the ancestor rproc instance, or NULL if not found. 21527c89717fSBjorn Andersson */ 21537c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev) 21547c89717fSBjorn Andersson { 21557c89717fSBjorn Andersson for (dev = dev->parent; dev; dev = dev->parent) { 21567c89717fSBjorn Andersson if (dev->type == &rproc_type) 21577c89717fSBjorn Andersson return dev->driver_data; 21587c89717fSBjorn Andersson } 21597c89717fSBjorn Andersson 21607c89717fSBjorn Andersson return NULL; 21617c89717fSBjorn Andersson } 21627c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child); 21637c89717fSBjorn Andersson 21647c89717fSBjorn Andersson /** 21658afd519cSFernando Guzman Lugo * rproc_report_crash() - rproc crash reporter function 21668afd519cSFernando Guzman Lugo * @rproc: remote processor 21678afd519cSFernando Guzman Lugo * @type: crash type 21688afd519cSFernando Guzman Lugo * 21698afd519cSFernando Guzman Lugo * This function must be called every time a crash is detected by the low-level 21708afd519cSFernando Guzman Lugo * drivers implementing a specific remoteproc. This should not be called from a 21718afd519cSFernando Guzman Lugo * non-remoteproc driver. 21728afd519cSFernando Guzman Lugo * 21738afd519cSFernando Guzman Lugo * This function can be called from atomic/interrupt context. 21748afd519cSFernando Guzman Lugo */ 21758afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type) 21768afd519cSFernando Guzman Lugo { 21778afd519cSFernando Guzman Lugo if (!rproc) { 21788afd519cSFernando Guzman Lugo pr_err("NULL rproc pointer\n"); 21798afd519cSFernando Guzman Lugo return; 21808afd519cSFernando Guzman Lugo } 21818afd519cSFernando Guzman Lugo 21828afd519cSFernando Guzman Lugo dev_err(&rproc->dev, "crash detected in %s: type %s\n", 21838afd519cSFernando Guzman Lugo rproc->name, rproc_crash_to_string(type)); 21848afd519cSFernando Guzman Lugo 21858afd519cSFernando Guzman Lugo /* create a new task to handle the error */ 21868afd519cSFernando Guzman Lugo schedule_work(&rproc->crash_handler); 21878afd519cSFernando Guzman Lugo } 21888afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash); 21898afd519cSFernando Guzman Lugo 2190400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void) 2191400e64dfSOhad Ben-Cohen { 21922aefbef0SMatt Redfearn rproc_init_sysfs(); 2193400e64dfSOhad Ben-Cohen rproc_init_debugfs(); 2194b5ab5e24SOhad Ben-Cohen 2195400e64dfSOhad Ben-Cohen return 0; 2196400e64dfSOhad Ben-Cohen } 2197400e64dfSOhad Ben-Cohen module_init(remoteproc_init); 2198400e64dfSOhad Ben-Cohen 2199400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void) 2200400e64dfSOhad Ben-Cohen { 2201f42f79afSSuman Anna ida_destroy(&rproc_dev_index); 2202f42f79afSSuman Anna 2203400e64dfSOhad Ben-Cohen rproc_exit_debugfs(); 22042aefbef0SMatt Redfearn rproc_exit_sysfs(); 2205400e64dfSOhad Ben-Cohen } 2206400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit); 2207400e64dfSOhad Ben-Cohen 2208400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2"); 2209400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework"); 2210