xref: /linux/drivers/remoteproc/remoteproc_core.c (revision ba7290e01663787fcfc2bedaff6232359d4ff248)
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>
36400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h>
37400e64dfSOhad Ben-Cohen #include <linux/iommu.h>
38b5ab5e24SOhad Ben-Cohen #include <linux/idr.h>
39400e64dfSOhad Ben-Cohen #include <linux/elf.h>
40400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h>
41400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h>
42cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h>
43400e64dfSOhad Ben-Cohen 
44400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h"
45400e64dfSOhad Ben-Cohen 
46400e64dfSOhad Ben-Cohen typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
47fd2c15ecSOhad Ben-Cohen 				struct resource_table *table, int len);
48fd2c15ecSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
49400e64dfSOhad Ben-Cohen 
50b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */
51b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index);
52b5ab5e24SOhad Ben-Cohen 
538afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = {
548afd519cSFernando Guzman Lugo 	[RPROC_MMUFAULT]	= "mmufault",
558afd519cSFernando Guzman Lugo };
568afd519cSFernando Guzman Lugo 
578afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */
588afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type)
598afd519cSFernando Guzman Lugo {
608afd519cSFernando Guzman Lugo 	if (type < ARRAY_SIZE(rproc_crash_names))
618afd519cSFernando Guzman Lugo 		return rproc_crash_names[type];
628afd519cSFernando Guzman Lugo 	return "unkown";
638afd519cSFernando Guzman Lugo }
648afd519cSFernando Guzman Lugo 
65400e64dfSOhad Ben-Cohen /*
66400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
67400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
68400e64dfSOhad Ben-Cohen  * an IOMMU).
69400e64dfSOhad Ben-Cohen  *
70400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
71400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
72400e64dfSOhad Ben-Cohen  */
73400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
7477ca2332SOhad Ben-Cohen 		unsigned long iova, int flags, void *token)
75400e64dfSOhad Ben-Cohen {
768afd519cSFernando Guzman Lugo 	struct rproc *rproc = token;
778afd519cSFernando Guzman Lugo 
78400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
79400e64dfSOhad Ben-Cohen 
808afd519cSFernando Guzman Lugo 	rproc_report_crash(rproc, RPROC_MMUFAULT);
818afd519cSFernando Guzman Lugo 
82400e64dfSOhad Ben-Cohen 	/*
83400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
848afd519cSFernando Guzman Lugo 	 * we just used it as a recovery trigger.
85400e64dfSOhad Ben-Cohen 	 */
86400e64dfSOhad Ben-Cohen 	return -ENOSYS;
87400e64dfSOhad Ben-Cohen }
88400e64dfSOhad Ben-Cohen 
89400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
90400e64dfSOhad Ben-Cohen {
91400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
92b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
93400e64dfSOhad Ben-Cohen 	int ret;
94400e64dfSOhad Ben-Cohen 
95400e64dfSOhad Ben-Cohen 	/*
96400e64dfSOhad Ben-Cohen 	 * We currently use iommu_present() to decide if an IOMMU
97400e64dfSOhad Ben-Cohen 	 * setup is needed.
98400e64dfSOhad Ben-Cohen 	 *
99400e64dfSOhad Ben-Cohen 	 * This works for simple cases, but will easily fail with
100400e64dfSOhad Ben-Cohen 	 * platforms that do have an IOMMU, but not for this specific
101400e64dfSOhad Ben-Cohen 	 * rproc.
102400e64dfSOhad Ben-Cohen 	 *
103400e64dfSOhad Ben-Cohen 	 * This will be easily solved by introducing hw capabilities
104400e64dfSOhad Ben-Cohen 	 * that will be set by the remoteproc driver.
105400e64dfSOhad Ben-Cohen 	 */
106400e64dfSOhad Ben-Cohen 	if (!iommu_present(dev->bus)) {
1070798e1daSMark Grosen 		dev_dbg(dev, "iommu not found\n");
1080798e1daSMark Grosen 		return 0;
109400e64dfSOhad Ben-Cohen 	}
110400e64dfSOhad Ben-Cohen 
111400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
112400e64dfSOhad Ben-Cohen 	if (!domain) {
113400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
114400e64dfSOhad Ben-Cohen 		return -ENOMEM;
115400e64dfSOhad Ben-Cohen 	}
116400e64dfSOhad Ben-Cohen 
11777ca2332SOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
118400e64dfSOhad Ben-Cohen 
119400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
120400e64dfSOhad Ben-Cohen 	if (ret) {
121400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
122400e64dfSOhad Ben-Cohen 		goto free_domain;
123400e64dfSOhad Ben-Cohen 	}
124400e64dfSOhad Ben-Cohen 
125400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
126400e64dfSOhad Ben-Cohen 
127400e64dfSOhad Ben-Cohen 	return 0;
128400e64dfSOhad Ben-Cohen 
129400e64dfSOhad Ben-Cohen free_domain:
130400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
131400e64dfSOhad Ben-Cohen 	return ret;
132400e64dfSOhad Ben-Cohen }
133400e64dfSOhad Ben-Cohen 
134400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
135400e64dfSOhad Ben-Cohen {
136400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
137b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
138400e64dfSOhad Ben-Cohen 
139400e64dfSOhad Ben-Cohen 	if (!domain)
140400e64dfSOhad Ben-Cohen 		return;
141400e64dfSOhad Ben-Cohen 
142400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
143400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
144400e64dfSOhad Ben-Cohen 
145400e64dfSOhad Ben-Cohen 	return;
146400e64dfSOhad Ben-Cohen }
147400e64dfSOhad Ben-Cohen 
148400e64dfSOhad Ben-Cohen /*
149400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
150400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
151400e64dfSOhad Ben-Cohen  * device addresses (which are hardcoded in the firmware).
152400e64dfSOhad Ben-Cohen  *
153400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
154400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
155400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
156400e64dfSOhad Ben-Cohen  *
157400e64dfSOhad Ben-Cohen  * This function is an internal helper with which we can go over the allocated
158400e64dfSOhad Ben-Cohen  * carveouts and translate specific device address to kernel virtual addresses
159400e64dfSOhad Ben-Cohen  * so we can access the referenced memory.
160400e64dfSOhad Ben-Cohen  *
161400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
162400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
163400e64dfSOhad Ben-Cohen  * here the output of the DMA API, which should be more correct.
164400e64dfSOhad Ben-Cohen  */
16572854fb0SSjur Brændeland void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
166400e64dfSOhad Ben-Cohen {
167400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
168400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
169400e64dfSOhad Ben-Cohen 
170400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
171400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
172400e64dfSOhad Ben-Cohen 
173400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
174400e64dfSOhad Ben-Cohen 		if (offset < 0)
175400e64dfSOhad Ben-Cohen 			continue;
176400e64dfSOhad Ben-Cohen 
177400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
178400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
179400e64dfSOhad Ben-Cohen 			continue;
180400e64dfSOhad Ben-Cohen 
181400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
182400e64dfSOhad Ben-Cohen 
183400e64dfSOhad Ben-Cohen 		break;
184400e64dfSOhad Ben-Cohen 	}
185400e64dfSOhad Ben-Cohen 
186400e64dfSOhad Ben-Cohen 	return ptr;
187400e64dfSOhad Ben-Cohen }
1884afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
189400e64dfSOhad Ben-Cohen 
1906db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
191400e64dfSOhad Ben-Cohen {
1927a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
193b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1946db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
1957a186941SOhad Ben-Cohen 	dma_addr_t dma;
1967a186941SOhad Ben-Cohen 	void *va;
1977a186941SOhad Ben-Cohen 	int ret, size, notifyid;
198400e64dfSOhad Ben-Cohen 
1996db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
2006db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2016db20ea8SOhad Ben-Cohen 
2026db20ea8SOhad Ben-Cohen 	/*
2036db20ea8SOhad Ben-Cohen 	 * Allocate non-cacheable memory for the vring. In the future
2046db20ea8SOhad Ben-Cohen 	 * this call will also configure the IOMMU for us
2056db20ea8SOhad Ben-Cohen 	 * TODO: let the rproc know the da of this vring
2066db20ea8SOhad Ben-Cohen 	 */
207b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
2086db20ea8SOhad Ben-Cohen 	if (!va) {
209b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent failed\n");
2106db20ea8SOhad Ben-Cohen 		return -EINVAL;
2116db20ea8SOhad Ben-Cohen 	}
2126db20ea8SOhad Ben-Cohen 
2136db20ea8SOhad Ben-Cohen 	/*
2146db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
2156db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
2166db20ea8SOhad Ben-Cohen 	 * TODO: let the rproc know the notifyid of this vring
2176db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
2186db20ea8SOhad Ben-Cohen 	 */
21915fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
2206db20ea8SOhad Ben-Cohen 	if (ret) {
22115fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
222b5ab5e24SOhad Ben-Cohen 		dma_free_coherent(dev->parent, size, va, dma);
2236db20ea8SOhad Ben-Cohen 		return ret;
2246db20ea8SOhad Ben-Cohen 	}
22515fc6110STejun Heo 	notifyid = ret;
2266db20ea8SOhad Ben-Cohen 
227d09f53a7SEmil Goode 	dev_dbg(dev, "vring%d: va %p dma %llx size %x idr %d\n", i, va,
228d09f53a7SEmil Goode 				(unsigned long long)dma, size, notifyid);
2296db20ea8SOhad Ben-Cohen 
2306db20ea8SOhad Ben-Cohen 	rvring->va = va;
2316db20ea8SOhad Ben-Cohen 	rvring->dma = dma;
2326db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
2336db20ea8SOhad Ben-Cohen 
2346db20ea8SOhad Ben-Cohen 	return 0;
2356db20ea8SOhad Ben-Cohen }
2366db20ea8SOhad Ben-Cohen 
237400e64dfSOhad Ben-Cohen static int
2386db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
239400e64dfSOhad Ben-Cohen {
240400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
241b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
242400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
2436db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
244400e64dfSOhad Ben-Cohen 
2457a186941SOhad Ben-Cohen 	dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
2467a186941SOhad Ben-Cohen 				i, vring->da, vring->num, vring->align);
2477a186941SOhad Ben-Cohen 
2487a186941SOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
2497a186941SOhad Ben-Cohen 	if (vring->reserved) {
2507a186941SOhad Ben-Cohen 		dev_err(dev, "vring rsc has non zero reserved bytes\n");
251fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
252fd2c15ecSOhad Ben-Cohen 	}
253fd2c15ecSOhad Ben-Cohen 
25463140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
25563140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
25663140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
25763140e0eSOhad Ben-Cohen 						vring->num, vring->align);
258400e64dfSOhad Ben-Cohen 		return -EINVAL;
259400e64dfSOhad Ben-Cohen 	}
260400e64dfSOhad Ben-Cohen 
2616db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
2626db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
2636db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
264400e64dfSOhad Ben-Cohen 
265400e64dfSOhad Ben-Cohen 	return 0;
266400e64dfSOhad Ben-Cohen }
267400e64dfSOhad Ben-Cohen 
2686db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
2697a186941SOhad Ben-Cohen {
27063140e0eSOhad Ben-Cohen 	int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2716db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
2727a186941SOhad Ben-Cohen 
273b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
2747a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
2757a186941SOhad Ben-Cohen }
2767a186941SOhad Ben-Cohen 
277400e64dfSOhad Ben-Cohen /**
278fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
279400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
280400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
281fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
282400e64dfSOhad Ben-Cohen  *
2837a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
2847a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
2857a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
2867a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
287400e64dfSOhad Ben-Cohen  *
2887a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
2897a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
2907a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
2917a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
2927a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
293400e64dfSOhad Ben-Cohen  *
2947a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
2957a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
2967a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
2977a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
2987a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
2997a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
3007a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
301400e64dfSOhad Ben-Cohen  *
302400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
303400e64dfSOhad Ben-Cohen  */
304fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
305fd2c15ecSOhad Ben-Cohen 								int avail)
306400e64dfSOhad Ben-Cohen {
307b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3087a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
3097a186941SOhad Ben-Cohen 	int i, ret;
310fd2c15ecSOhad Ben-Cohen 
311fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
312fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
313fd2c15ecSOhad Ben-Cohen 			+ rsc->config_len > avail) {
314b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
315fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
316fd2c15ecSOhad Ben-Cohen 	}
317fd2c15ecSOhad Ben-Cohen 
318fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
319fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
320fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
321fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
322fd2c15ecSOhad Ben-Cohen 	}
323fd2c15ecSOhad Ben-Cohen 
324fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
325fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
326400e64dfSOhad Ben-Cohen 
3277a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
3287a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
329fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
330400e64dfSOhad Ben-Cohen 		return -EINVAL;
331400e64dfSOhad Ben-Cohen 	}
332400e64dfSOhad Ben-Cohen 
3337a186941SOhad Ben-Cohen 	rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
3347a186941SOhad Ben-Cohen 	if (!rvdev)
3357a186941SOhad Ben-Cohen 		return -ENOMEM;
3367a186941SOhad Ben-Cohen 
3377a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
3387a186941SOhad Ben-Cohen 
3396db20ea8SOhad Ben-Cohen 	/* parse the vrings */
340fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
3416db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
3427a186941SOhad Ben-Cohen 		if (ret)
3436db20ea8SOhad Ben-Cohen 			goto free_rvdev;
344fd2c15ecSOhad Ben-Cohen 	}
345fd2c15ecSOhad Ben-Cohen 
3467a186941SOhad Ben-Cohen 	/* remember the device features */
3477a186941SOhad Ben-Cohen 	rvdev->dfeatures = rsc->dfeatures;
348400e64dfSOhad Ben-Cohen 
3497a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
350400e64dfSOhad Ben-Cohen 
3517a186941SOhad Ben-Cohen 	/* it is now safe to add the virtio device */
3527a186941SOhad Ben-Cohen 	ret = rproc_add_virtio_dev(rvdev, rsc->id);
3537a186941SOhad Ben-Cohen 	if (ret)
3546db20ea8SOhad Ben-Cohen 		goto free_rvdev;
355400e64dfSOhad Ben-Cohen 
356400e64dfSOhad Ben-Cohen 	return 0;
3577a186941SOhad Ben-Cohen 
3586db20ea8SOhad Ben-Cohen free_rvdev:
3597a186941SOhad Ben-Cohen 	kfree(rvdev);
3607a186941SOhad Ben-Cohen 	return ret;
361400e64dfSOhad Ben-Cohen }
362400e64dfSOhad Ben-Cohen 
363400e64dfSOhad Ben-Cohen /**
364400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
365400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
366400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
367fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
368400e64dfSOhad Ben-Cohen  *
369400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
370400e64dfSOhad Ben-Cohen  * export it via debugfs.
371400e64dfSOhad Ben-Cohen  *
372400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
373400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
374400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
375400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
376400e64dfSOhad Ben-Cohen  *
377400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
378400e64dfSOhad Ben-Cohen  */
379fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
380fd2c15ecSOhad Ben-Cohen 								int avail)
381400e64dfSOhad Ben-Cohen {
382400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *trace;
383b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
384400e64dfSOhad Ben-Cohen 	void *ptr;
385400e64dfSOhad Ben-Cohen 	char name[15];
386400e64dfSOhad Ben-Cohen 
387fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
388b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
389fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
390fd2c15ecSOhad Ben-Cohen 	}
391fd2c15ecSOhad Ben-Cohen 
392fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
393fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
394fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
395fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
396fd2c15ecSOhad Ben-Cohen 	}
397fd2c15ecSOhad Ben-Cohen 
398400e64dfSOhad Ben-Cohen 	/* what's the kernel address of this resource ? */
399400e64dfSOhad Ben-Cohen 	ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
400400e64dfSOhad Ben-Cohen 	if (!ptr) {
401400e64dfSOhad Ben-Cohen 		dev_err(dev, "erroneous trace resource entry\n");
402400e64dfSOhad Ben-Cohen 		return -EINVAL;
403400e64dfSOhad Ben-Cohen 	}
404400e64dfSOhad Ben-Cohen 
405400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
406400e64dfSOhad Ben-Cohen 	if (!trace) {
407400e64dfSOhad Ben-Cohen 		dev_err(dev, "kzalloc trace failed\n");
408400e64dfSOhad Ben-Cohen 		return -ENOMEM;
409400e64dfSOhad Ben-Cohen 	}
410400e64dfSOhad Ben-Cohen 
411400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
412400e64dfSOhad Ben-Cohen 	trace->len = rsc->len;
413400e64dfSOhad Ben-Cohen 	trace->va = ptr;
414400e64dfSOhad Ben-Cohen 
415400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
416400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
417400e64dfSOhad Ben-Cohen 
418400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
419400e64dfSOhad Ben-Cohen 	trace->priv = rproc_create_trace_file(name, rproc, trace);
420400e64dfSOhad Ben-Cohen 	if (!trace->priv) {
421400e64dfSOhad Ben-Cohen 		trace->va = NULL;
422400e64dfSOhad Ben-Cohen 		kfree(trace);
423400e64dfSOhad Ben-Cohen 		return -EINVAL;
424400e64dfSOhad Ben-Cohen 	}
425400e64dfSOhad Ben-Cohen 
426400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
427400e64dfSOhad Ben-Cohen 
428400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
429400e64dfSOhad Ben-Cohen 
430fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
431400e64dfSOhad Ben-Cohen 						rsc->da, rsc->len);
432400e64dfSOhad Ben-Cohen 
433400e64dfSOhad Ben-Cohen 	return 0;
434400e64dfSOhad Ben-Cohen }
435400e64dfSOhad Ben-Cohen 
436400e64dfSOhad Ben-Cohen /**
437400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
438400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
439400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
440fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
441400e64dfSOhad Ben-Cohen  *
442400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
443400e64dfSOhad Ben-Cohen  *
444400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
445400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
446400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
447400e64dfSOhad Ben-Cohen  *
448400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
449400e64dfSOhad Ben-Cohen  *
450400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
451400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
452400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
453400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
454400e64dfSOhad Ben-Cohen  *
455400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
456400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
457400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
458400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
459400e64dfSOhad Ben-Cohen  * are outside those ranges.
460400e64dfSOhad Ben-Cohen  */
461fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
462fd2c15ecSOhad Ben-Cohen 								int avail)
463400e64dfSOhad Ben-Cohen {
464400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
465b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
466400e64dfSOhad Ben-Cohen 	int ret;
467400e64dfSOhad Ben-Cohen 
468400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
469400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
470400e64dfSOhad Ben-Cohen 		return -EINVAL;
471400e64dfSOhad Ben-Cohen 
472fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
473b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
474fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
475fd2c15ecSOhad Ben-Cohen 	}
476fd2c15ecSOhad Ben-Cohen 
477fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
478fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
479b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
480fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
481fd2c15ecSOhad Ben-Cohen 	}
482fd2c15ecSOhad Ben-Cohen 
483400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
484400e64dfSOhad Ben-Cohen 	if (!mapping) {
485b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "kzalloc mapping failed\n");
486400e64dfSOhad Ben-Cohen 		return -ENOMEM;
487400e64dfSOhad Ben-Cohen 	}
488400e64dfSOhad Ben-Cohen 
489400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
490400e64dfSOhad Ben-Cohen 	if (ret) {
491b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
492400e64dfSOhad Ben-Cohen 		goto out;
493400e64dfSOhad Ben-Cohen 	}
494400e64dfSOhad Ben-Cohen 
495400e64dfSOhad Ben-Cohen 	/*
496400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
497400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
498400e64dfSOhad Ben-Cohen 	 *
499400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
500400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
501400e64dfSOhad Ben-Cohen 	 */
502400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
503400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
504400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
505400e64dfSOhad Ben-Cohen 
506b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
507400e64dfSOhad Ben-Cohen 					rsc->pa, rsc->da, rsc->len);
508400e64dfSOhad Ben-Cohen 
509400e64dfSOhad Ben-Cohen 	return 0;
510400e64dfSOhad Ben-Cohen 
511400e64dfSOhad Ben-Cohen out:
512400e64dfSOhad Ben-Cohen 	kfree(mapping);
513400e64dfSOhad Ben-Cohen 	return ret;
514400e64dfSOhad Ben-Cohen }
515400e64dfSOhad Ben-Cohen 
516400e64dfSOhad Ben-Cohen /**
517400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
518400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
519400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
520fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
521400e64dfSOhad Ben-Cohen  *
522400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
523400e64dfSOhad Ben-Cohen  * contiguous memory regions.
524400e64dfSOhad Ben-Cohen  *
525400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
526400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
527400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
528400e64dfSOhad Ben-Cohen  *
529400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
530400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
531400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
532400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
533400e64dfSOhad Ben-Cohen  */
534fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
535fd2c15ecSOhad Ben-Cohen 				struct fw_rsc_carveout *rsc, int avail)
536400e64dfSOhad Ben-Cohen {
537400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout, *mapping;
538b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
539400e64dfSOhad Ben-Cohen 	dma_addr_t dma;
540400e64dfSOhad Ben-Cohen 	void *va;
541400e64dfSOhad Ben-Cohen 	int ret;
542400e64dfSOhad Ben-Cohen 
543fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
544b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
545fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
546fd2c15ecSOhad Ben-Cohen 	}
547fd2c15ecSOhad Ben-Cohen 
548fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
549fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
550fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
551fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
552fd2c15ecSOhad Ben-Cohen 	}
553fd2c15ecSOhad Ben-Cohen 
554fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
555fd2c15ecSOhad Ben-Cohen 			rsc->da, rsc->pa, rsc->len, rsc->flags);
556fd2c15ecSOhad Ben-Cohen 
557400e64dfSOhad Ben-Cohen 	carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
558400e64dfSOhad Ben-Cohen 	if (!carveout) {
559400e64dfSOhad Ben-Cohen 		dev_err(dev, "kzalloc carveout failed\n");
5607168d914SDan Carpenter 		return -ENOMEM;
561400e64dfSOhad Ben-Cohen 	}
562400e64dfSOhad Ben-Cohen 
563b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
564400e64dfSOhad Ben-Cohen 	if (!va) {
565b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
566400e64dfSOhad Ben-Cohen 		ret = -ENOMEM;
567400e64dfSOhad Ben-Cohen 		goto free_carv;
568400e64dfSOhad Ben-Cohen 	}
569400e64dfSOhad Ben-Cohen 
570d09f53a7SEmil Goode 	dev_dbg(dev, "carveout va %p, dma %llx, len 0x%x\n", va,
571d09f53a7SEmil Goode 					(unsigned long long)dma, rsc->len);
572400e64dfSOhad Ben-Cohen 
573400e64dfSOhad Ben-Cohen 	/*
574400e64dfSOhad Ben-Cohen 	 * Ok, this is non-standard.
575400e64dfSOhad Ben-Cohen 	 *
576400e64dfSOhad Ben-Cohen 	 * Sometimes we can't rely on the generic iommu-based DMA API
577400e64dfSOhad Ben-Cohen 	 * to dynamically allocate the device address and then set the IOMMU
578400e64dfSOhad Ben-Cohen 	 * tables accordingly, because some remote processors might
579400e64dfSOhad Ben-Cohen 	 * _require_ us to use hard coded device addresses that their
580400e64dfSOhad Ben-Cohen 	 * firmware was compiled with.
581400e64dfSOhad Ben-Cohen 	 *
582400e64dfSOhad Ben-Cohen 	 * In this case, we must use the IOMMU API directly and map
583400e64dfSOhad Ben-Cohen 	 * the memory to the device address as expected by the remote
584400e64dfSOhad Ben-Cohen 	 * processor.
585400e64dfSOhad Ben-Cohen 	 *
586400e64dfSOhad Ben-Cohen 	 * Obviously such remote processor devices should not be configured
587400e64dfSOhad Ben-Cohen 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
588400e64dfSOhad Ben-Cohen 	 * physical address in this case.
589400e64dfSOhad Ben-Cohen 	 */
590400e64dfSOhad Ben-Cohen 	if (rproc->domain) {
5917168d914SDan Carpenter 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
5927168d914SDan Carpenter 		if (!mapping) {
5937168d914SDan Carpenter 			dev_err(dev, "kzalloc mapping failed\n");
5947168d914SDan Carpenter 			ret = -ENOMEM;
5957168d914SDan Carpenter 			goto dma_free;
5967168d914SDan Carpenter 		}
5977168d914SDan Carpenter 
598400e64dfSOhad Ben-Cohen 		ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
599400e64dfSOhad Ben-Cohen 								rsc->flags);
600400e64dfSOhad Ben-Cohen 		if (ret) {
601400e64dfSOhad Ben-Cohen 			dev_err(dev, "iommu_map failed: %d\n", ret);
6027168d914SDan Carpenter 			goto free_mapping;
603400e64dfSOhad Ben-Cohen 		}
604400e64dfSOhad Ben-Cohen 
605400e64dfSOhad Ben-Cohen 		/*
606400e64dfSOhad Ben-Cohen 		 * We'll need this info later when we'll want to unmap
607400e64dfSOhad Ben-Cohen 		 * everything (e.g. on shutdown).
608400e64dfSOhad Ben-Cohen 		 *
609400e64dfSOhad Ben-Cohen 		 * We can't trust the remote processor not to change the
610400e64dfSOhad Ben-Cohen 		 * resource table, so we must maintain this info independently.
611400e64dfSOhad Ben-Cohen 		 */
612400e64dfSOhad Ben-Cohen 		mapping->da = rsc->da;
613400e64dfSOhad Ben-Cohen 		mapping->len = rsc->len;
614400e64dfSOhad Ben-Cohen 		list_add_tail(&mapping->node, &rproc->mappings);
615400e64dfSOhad Ben-Cohen 
616d09f53a7SEmil Goode 		dev_dbg(dev, "carveout mapped 0x%x to 0x%llx\n",
617d09f53a7SEmil Goode 					rsc->da, (unsigned long long)dma);
6180e49b72cSOhad Ben-Cohen 	}
619400e64dfSOhad Ben-Cohen 
620400e64dfSOhad Ben-Cohen 	/*
621400e64dfSOhad Ben-Cohen 	 * Some remote processors might need to know the pa
622400e64dfSOhad Ben-Cohen 	 * even though they are behind an IOMMU. E.g., OMAP4's
623400e64dfSOhad Ben-Cohen 	 * remote M3 processor needs this so it can control
624400e64dfSOhad Ben-Cohen 	 * on-chip hardware accelerators that are not behind
625400e64dfSOhad Ben-Cohen 	 * the IOMMU, and therefor must know the pa.
626400e64dfSOhad Ben-Cohen 	 *
627400e64dfSOhad Ben-Cohen 	 * Generally we don't want to expose physical addresses
628400e64dfSOhad Ben-Cohen 	 * if we don't have to (remote processors are generally
629400e64dfSOhad Ben-Cohen 	 * _not_ trusted), so we might want to do this only for
630400e64dfSOhad Ben-Cohen 	 * remote processor that _must_ have this (e.g. OMAP4's
631400e64dfSOhad Ben-Cohen 	 * dual M3 subsystem).
6320e49b72cSOhad Ben-Cohen 	 *
6330e49b72cSOhad Ben-Cohen 	 * Non-IOMMU processors might also want to have this info.
6340e49b72cSOhad Ben-Cohen 	 * In this case, the device address and the physical address
6350e49b72cSOhad Ben-Cohen 	 * are the same.
636400e64dfSOhad Ben-Cohen 	 */
637400e64dfSOhad Ben-Cohen 	rsc->pa = dma;
638400e64dfSOhad Ben-Cohen 
639400e64dfSOhad Ben-Cohen 	carveout->va = va;
640400e64dfSOhad Ben-Cohen 	carveout->len = rsc->len;
641400e64dfSOhad Ben-Cohen 	carveout->dma = dma;
642400e64dfSOhad Ben-Cohen 	carveout->da = rsc->da;
643400e64dfSOhad Ben-Cohen 
644400e64dfSOhad Ben-Cohen 	list_add_tail(&carveout->node, &rproc->carveouts);
645400e64dfSOhad Ben-Cohen 
646400e64dfSOhad Ben-Cohen 	return 0;
647400e64dfSOhad Ben-Cohen 
6487168d914SDan Carpenter free_mapping:
6497168d914SDan Carpenter 	kfree(mapping);
650400e64dfSOhad Ben-Cohen dma_free:
651b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(dev->parent, rsc->len, va, dma);
652400e64dfSOhad Ben-Cohen free_carv:
653400e64dfSOhad Ben-Cohen 	kfree(carveout);
654400e64dfSOhad Ben-Cohen 	return ret;
655400e64dfSOhad Ben-Cohen }
656400e64dfSOhad Ben-Cohen 
657*ba7290e0SSjur Brændeland static int rproc_count_vrings(struct rproc *rproc, struct fw_rsc_vdev *rsc,
658*ba7290e0SSjur Brændeland 				 int avail)
659*ba7290e0SSjur Brændeland {
660*ba7290e0SSjur Brændeland 	/* Summarize the number of notification IDs */
661*ba7290e0SSjur Brændeland 	rproc->max_notifyid += rsc->num_of_vrings;
662*ba7290e0SSjur Brændeland 
663*ba7290e0SSjur Brændeland 	return 0;
664*ba7290e0SSjur Brændeland }
665*ba7290e0SSjur Brændeland 
666e12bc14bSOhad Ben-Cohen /*
667e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
668e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
669e12bc14bSOhad Ben-Cohen  */
670232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
671fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
672fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
673fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
6747a186941SOhad Ben-Cohen 	[RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
675e12bc14bSOhad Ben-Cohen };
676e12bc14bSOhad Ben-Cohen 
677232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_vdev_handler[RSC_LAST] = {
678232fcdbbSSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
679232fcdbbSSjur Brændeland };
680232fcdbbSSjur Brændeland 
681*ba7290e0SSjur Brændeland static rproc_handle_resource_t rproc_count_vrings_handler[RSC_LAST] = {
682*ba7290e0SSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_count_vrings,
683*ba7290e0SSjur Brændeland };
684*ba7290e0SSjur Brændeland 
685400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
686232fcdbbSSjur Brændeland static int rproc_handle_resources(struct rproc *rproc,
687232fcdbbSSjur Brændeland 				  struct resource_table *table, int len,
688232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
689400e64dfSOhad Ben-Cohen {
690b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
691e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
692fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
693400e64dfSOhad Ben-Cohen 
694fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < table->num; i++) {
695fd2c15ecSOhad Ben-Cohen 		int offset = table->offset[i];
696fd2c15ecSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)table + offset;
697fd2c15ecSOhad Ben-Cohen 		int avail = len - offset - sizeof(*hdr);
698fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
699400e64dfSOhad Ben-Cohen 
700fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
701fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
702fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
703fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
704fd2c15ecSOhad Ben-Cohen 		}
705fd2c15ecSOhad Ben-Cohen 
706fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
707fd2c15ecSOhad Ben-Cohen 
708fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
709fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
710e12bc14bSOhad Ben-Cohen 			continue;
711400e64dfSOhad Ben-Cohen 		}
712400e64dfSOhad Ben-Cohen 
713232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
714e12bc14bSOhad Ben-Cohen 		if (!handler)
715e12bc14bSOhad Ben-Cohen 			continue;
716e12bc14bSOhad Ben-Cohen 
717fd2c15ecSOhad Ben-Cohen 		ret = handler(rproc, rsc, avail);
718400e64dfSOhad Ben-Cohen 		if (ret)
719400e64dfSOhad Ben-Cohen 			break;
720400e64dfSOhad Ben-Cohen 	}
721400e64dfSOhad Ben-Cohen 
722400e64dfSOhad Ben-Cohen 	return ret;
723400e64dfSOhad Ben-Cohen }
724400e64dfSOhad Ben-Cohen 
725400e64dfSOhad Ben-Cohen /**
726400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
727400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
728400e64dfSOhad Ben-Cohen  *
729400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
7307a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
731400e64dfSOhad Ben-Cohen  */
732400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc)
733400e64dfSOhad Ben-Cohen {
734400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
735b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
736400e64dfSOhad Ben-Cohen 
737400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
738400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
739400e64dfSOhad Ben-Cohen 		rproc_remove_trace_file(entry->priv);
740400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
741400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
742400e64dfSOhad Ben-Cohen 		kfree(entry);
743400e64dfSOhad Ben-Cohen 	}
744400e64dfSOhad Ben-Cohen 
745400e64dfSOhad Ben-Cohen 	/* clean up carveout allocations */
746400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
747b5ab5e24SOhad Ben-Cohen 		dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
748400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
749400e64dfSOhad Ben-Cohen 		kfree(entry);
750400e64dfSOhad Ben-Cohen 	}
751400e64dfSOhad Ben-Cohen 
752400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
753400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
754400e64dfSOhad Ben-Cohen 		size_t unmapped;
755400e64dfSOhad Ben-Cohen 
756400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
757400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
758400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
759e981f6d4SSjur Brændeland 			dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
760400e64dfSOhad Ben-Cohen 								unmapped);
761400e64dfSOhad Ben-Cohen 		}
762400e64dfSOhad Ben-Cohen 
763400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
764400e64dfSOhad Ben-Cohen 		kfree(entry);
765400e64dfSOhad Ben-Cohen 	}
766400e64dfSOhad Ben-Cohen }
767400e64dfSOhad Ben-Cohen 
768400e64dfSOhad Ben-Cohen /*
769400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
770400e64dfSOhad Ben-Cohen  */
771400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
772400e64dfSOhad Ben-Cohen {
773b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
774400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
7751e3e2c7cSOhad Ben-Cohen 	struct resource_table *table;
7761e3e2c7cSOhad Ben-Cohen 	int ret, tablesz;
777400e64dfSOhad Ben-Cohen 
778400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
779400e64dfSOhad Ben-Cohen 	if (ret)
780400e64dfSOhad Ben-Cohen 		return ret;
781400e64dfSOhad Ben-Cohen 
782e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
783400e64dfSOhad Ben-Cohen 
784400e64dfSOhad Ben-Cohen 	/*
785400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
786400e64dfSOhad Ben-Cohen 	 * just a nop
787400e64dfSOhad Ben-Cohen 	 */
788400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
789400e64dfSOhad Ben-Cohen 	if (ret) {
790400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
791400e64dfSOhad Ben-Cohen 		return ret;
792400e64dfSOhad Ben-Cohen 	}
793400e64dfSOhad Ben-Cohen 
7943e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
795400e64dfSOhad Ben-Cohen 
7961e3e2c7cSOhad Ben-Cohen 	/* look for the resource table */
797bd484984SSjur Brændeland 	table = rproc_find_rsc_table(rproc, fw, &tablesz);
79830338cf0SSjur Brændeland 	if (!table) {
79930338cf0SSjur Brændeland 		ret = -EINVAL;
8001e3e2c7cSOhad Ben-Cohen 		goto clean_up;
80130338cf0SSjur Brændeland 	}
8021e3e2c7cSOhad Ben-Cohen 
803400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
804232fcdbbSSjur Brændeland 	ret = rproc_handle_resources(rproc, table, tablesz,
805232fcdbbSSjur Brændeland 				     rproc_loading_handlers);
806400e64dfSOhad Ben-Cohen 	if (ret) {
807400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
808400e64dfSOhad Ben-Cohen 		goto clean_up;
809400e64dfSOhad Ben-Cohen 	}
810400e64dfSOhad Ben-Cohen 
811400e64dfSOhad Ben-Cohen 	/* load the ELF segments to memory */
812bd484984SSjur Brændeland 	ret = rproc_load_segments(rproc, fw);
813400e64dfSOhad Ben-Cohen 	if (ret) {
814400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to load program segments: %d\n", ret);
815400e64dfSOhad Ben-Cohen 		goto clean_up;
816400e64dfSOhad Ben-Cohen 	}
817400e64dfSOhad Ben-Cohen 
818400e64dfSOhad Ben-Cohen 	/* power up the remote processor */
819400e64dfSOhad Ben-Cohen 	ret = rproc->ops->start(rproc);
820400e64dfSOhad Ben-Cohen 	if (ret) {
821400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
822400e64dfSOhad Ben-Cohen 		goto clean_up;
823400e64dfSOhad Ben-Cohen 	}
824400e64dfSOhad Ben-Cohen 
825400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_RUNNING;
826400e64dfSOhad Ben-Cohen 
827400e64dfSOhad Ben-Cohen 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
828400e64dfSOhad Ben-Cohen 
829400e64dfSOhad Ben-Cohen 	return 0;
830400e64dfSOhad Ben-Cohen 
831400e64dfSOhad Ben-Cohen clean_up:
832400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
833400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
834400e64dfSOhad Ben-Cohen 	return ret;
835400e64dfSOhad Ben-Cohen }
836400e64dfSOhad Ben-Cohen 
837400e64dfSOhad Ben-Cohen /*
838400e64dfSOhad Ben-Cohen  * take a firmware and look for virtio devices to register.
839400e64dfSOhad Ben-Cohen  *
840400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
841400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
842400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
843400e64dfSOhad Ben-Cohen  * that might be cleaner).
844400e64dfSOhad Ben-Cohen  */
845400e64dfSOhad Ben-Cohen static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
846400e64dfSOhad Ben-Cohen {
847400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
8481e3e2c7cSOhad Ben-Cohen 	struct resource_table *table;
8491e3e2c7cSOhad Ben-Cohen 	int ret, tablesz;
850400e64dfSOhad Ben-Cohen 
851400e64dfSOhad Ben-Cohen 	if (rproc_fw_sanity_check(rproc, fw) < 0)
852400e64dfSOhad Ben-Cohen 		goto out;
853400e64dfSOhad Ben-Cohen 
8541e3e2c7cSOhad Ben-Cohen 	/* look for the resource table */
855bd484984SSjur Brændeland 	table = rproc_find_rsc_table(rproc, fw,  &tablesz);
8561e3e2c7cSOhad Ben-Cohen 	if (!table)
857400e64dfSOhad Ben-Cohen 		goto out;
8581e3e2c7cSOhad Ben-Cohen 
859*ba7290e0SSjur Brændeland 	/* count the number of notify-ids */
860*ba7290e0SSjur Brændeland 	rproc->max_notifyid = -1;
861*ba7290e0SSjur Brændeland 	ret = rproc_handle_resources(rproc, table, tablesz,
862*ba7290e0SSjur Brændeland 				    rproc_count_vrings_handler);
863*ba7290e0SSjur Brændeland 
8641e3e2c7cSOhad Ben-Cohen 	/* look for virtio devices and register them */
865232fcdbbSSjur Brændeland 	ret = rproc_handle_resources(rproc, table, tablesz, rproc_vdev_handler);
8661e3e2c7cSOhad Ben-Cohen 	if (ret)
8671e3e2c7cSOhad Ben-Cohen 		goto out;
868400e64dfSOhad Ben-Cohen 
869400e64dfSOhad Ben-Cohen out:
870400e64dfSOhad Ben-Cohen 	release_firmware(fw);
871160e7c84SOhad Ben-Cohen 	/* allow rproc_del() contexts, if any, to proceed */
872400e64dfSOhad Ben-Cohen 	complete_all(&rproc->firmware_loading_complete);
873400e64dfSOhad Ben-Cohen }
874400e64dfSOhad Ben-Cohen 
87570b85ef8SFernando Guzman Lugo static int rproc_add_virtio_devices(struct rproc *rproc)
87670b85ef8SFernando Guzman Lugo {
87770b85ef8SFernando Guzman Lugo 	int ret;
87870b85ef8SFernando Guzman Lugo 
87970b85ef8SFernando Guzman Lugo 	/* rproc_del() calls must wait until async loader completes */
88070b85ef8SFernando Guzman Lugo 	init_completion(&rproc->firmware_loading_complete);
88170b85ef8SFernando Guzman Lugo 
88270b85ef8SFernando Guzman Lugo 	/*
88370b85ef8SFernando Guzman Lugo 	 * We must retrieve early virtio configuration info from
88470b85ef8SFernando Guzman Lugo 	 * the firmware (e.g. whether to register a virtio device,
88570b85ef8SFernando Guzman Lugo 	 * what virtio features does it support, ...).
88670b85ef8SFernando Guzman Lugo 	 *
88770b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
88870b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
88970b85ef8SFernando Guzman Lugo 	 */
89070b85ef8SFernando Guzman Lugo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
89170b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
89270b85ef8SFernando Guzman Lugo 				      rproc, rproc_fw_config_virtio);
89370b85ef8SFernando Guzman Lugo 	if (ret < 0) {
89470b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
89570b85ef8SFernando Guzman Lugo 		complete_all(&rproc->firmware_loading_complete);
89670b85ef8SFernando Guzman Lugo 	}
89770b85ef8SFernando Guzman Lugo 
89870b85ef8SFernando Guzman Lugo 	return ret;
89970b85ef8SFernando Guzman Lugo }
90070b85ef8SFernando Guzman Lugo 
90170b85ef8SFernando Guzman Lugo /**
90270b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
90370b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
90470b85ef8SFernando Guzman Lugo  *
90570b85ef8SFernando Guzman Lugo  * The recovery is done by reseting all the virtio devices, that way all the
90670b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
90770b85ef8SFernando Guzman Lugo  * remoteproc functional again.
90870b85ef8SFernando Guzman Lugo  *
90970b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
91070b85ef8SFernando Guzman Lugo  */
91170b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
91270b85ef8SFernando Guzman Lugo {
91370b85ef8SFernando Guzman Lugo 	struct rproc_vdev *rvdev, *rvtmp;
91470b85ef8SFernando Guzman Lugo 
91570b85ef8SFernando Guzman Lugo 	dev_err(&rproc->dev, "recovering %s\n", rproc->name);
91670b85ef8SFernando Guzman Lugo 
91770b85ef8SFernando Guzman Lugo 	init_completion(&rproc->crash_comp);
91870b85ef8SFernando Guzman Lugo 
91970b85ef8SFernando Guzman Lugo 	/* clean up remote vdev entries */
92070b85ef8SFernando Guzman Lugo 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
92170b85ef8SFernando Guzman Lugo 		rproc_remove_virtio_dev(rvdev);
92270b85ef8SFernando Guzman Lugo 
92370b85ef8SFernando Guzman Lugo 	/* wait until there is no more rproc users */
92470b85ef8SFernando Guzman Lugo 	wait_for_completion(&rproc->crash_comp);
92570b85ef8SFernando Guzman Lugo 
92670b85ef8SFernando Guzman Lugo 	return rproc_add_virtio_devices(rproc);
92770b85ef8SFernando Guzman Lugo }
92870b85ef8SFernando Guzman Lugo 
929400e64dfSOhad Ben-Cohen /**
9308afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
9318afd519cSFernando Guzman Lugo  *
9328afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
9338afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
9348afd519cSFernando Guzman Lugo  */
9358afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
9368afd519cSFernando Guzman Lugo {
9378afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
9388afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
9398afd519cSFernando Guzman Lugo 
9408afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
9418afd519cSFernando Guzman Lugo 
9428afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
9438afd519cSFernando Guzman Lugo 
9448afd519cSFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
9458afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
9468afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
9478afd519cSFernando Guzman Lugo 		return;
9488afd519cSFernando Guzman Lugo 	}
9498afd519cSFernando Guzman Lugo 
9508afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
9518afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
9528afd519cSFernando Guzman Lugo 		rproc->name);
9538afd519cSFernando Guzman Lugo 
9548afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
9558afd519cSFernando Guzman Lugo 
9562e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
95770b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
9588afd519cSFernando Guzman Lugo }
9598afd519cSFernando Guzman Lugo 
9608afd519cSFernando Guzman Lugo /**
961400e64dfSOhad Ben-Cohen  * rproc_boot() - boot a remote processor
962400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
963400e64dfSOhad Ben-Cohen  *
964400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
965400e64dfSOhad Ben-Cohen  *
966400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
967400e64dfSOhad Ben-Cohen  * returns (successfully).
968400e64dfSOhad Ben-Cohen  *
969400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
970400e64dfSOhad Ben-Cohen  */
971400e64dfSOhad Ben-Cohen int rproc_boot(struct rproc *rproc)
972400e64dfSOhad Ben-Cohen {
973400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
974400e64dfSOhad Ben-Cohen 	struct device *dev;
975400e64dfSOhad Ben-Cohen 	int ret;
976400e64dfSOhad Ben-Cohen 
977400e64dfSOhad Ben-Cohen 	if (!rproc) {
978400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
979400e64dfSOhad Ben-Cohen 		return -EINVAL;
980400e64dfSOhad Ben-Cohen 	}
981400e64dfSOhad Ben-Cohen 
982b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
983400e64dfSOhad Ben-Cohen 
984400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
985400e64dfSOhad Ben-Cohen 	if (ret) {
986400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
987400e64dfSOhad Ben-Cohen 		return ret;
988400e64dfSOhad Ben-Cohen 	}
989400e64dfSOhad Ben-Cohen 
990400e64dfSOhad Ben-Cohen 	/* loading a firmware is required */
991400e64dfSOhad Ben-Cohen 	if (!rproc->firmware) {
992400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: no firmware to load\n", __func__);
993400e64dfSOhad Ben-Cohen 		ret = -EINVAL;
994400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
995400e64dfSOhad Ben-Cohen 	}
996400e64dfSOhad Ben-Cohen 
997400e64dfSOhad Ben-Cohen 	/* prevent underlying implementation from being removed */
998b5ab5e24SOhad Ben-Cohen 	if (!try_module_get(dev->parent->driver->owner)) {
999400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: can't get owner\n", __func__);
1000400e64dfSOhad Ben-Cohen 		ret = -EINVAL;
1001400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1002400e64dfSOhad Ben-Cohen 	}
1003400e64dfSOhad Ben-Cohen 
1004400e64dfSOhad Ben-Cohen 	/* skip the boot process if rproc is already powered up */
1005400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1006400e64dfSOhad Ben-Cohen 		ret = 0;
1007400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1008400e64dfSOhad Ben-Cohen 	}
1009400e64dfSOhad Ben-Cohen 
1010400e64dfSOhad Ben-Cohen 	dev_info(dev, "powering up %s\n", rproc->name);
1011400e64dfSOhad Ben-Cohen 
1012400e64dfSOhad Ben-Cohen 	/* load firmware */
1013400e64dfSOhad Ben-Cohen 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1014400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1015400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware failed: %d\n", ret);
1016400e64dfSOhad Ben-Cohen 		goto downref_rproc;
1017400e64dfSOhad Ben-Cohen 	}
1018400e64dfSOhad Ben-Cohen 
1019400e64dfSOhad Ben-Cohen 	ret = rproc_fw_boot(rproc, firmware_p);
1020400e64dfSOhad Ben-Cohen 
1021400e64dfSOhad Ben-Cohen 	release_firmware(firmware_p);
1022400e64dfSOhad Ben-Cohen 
1023400e64dfSOhad Ben-Cohen downref_rproc:
1024400e64dfSOhad Ben-Cohen 	if (ret) {
1025b5ab5e24SOhad Ben-Cohen 		module_put(dev->parent->driver->owner);
1026400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1027400e64dfSOhad Ben-Cohen 	}
1028400e64dfSOhad Ben-Cohen unlock_mutex:
1029400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1030400e64dfSOhad Ben-Cohen 	return ret;
1031400e64dfSOhad Ben-Cohen }
1032400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1033400e64dfSOhad Ben-Cohen 
1034400e64dfSOhad Ben-Cohen /**
1035400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1036400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1037400e64dfSOhad Ben-Cohen  *
1038400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1039400e64dfSOhad Ben-Cohen  *
1040400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1041400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1042400e64dfSOhad Ben-Cohen  * without really powering off the device.
1043400e64dfSOhad Ben-Cohen  *
1044400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1045400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1046400e64dfSOhad Ben-Cohen  *
1047400e64dfSOhad Ben-Cohen  * Notes:
1048400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1049400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1050400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1051400e64dfSOhad Ben-Cohen  *   needed.
1052400e64dfSOhad Ben-Cohen  */
1053400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1054400e64dfSOhad Ben-Cohen {
1055b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1056400e64dfSOhad Ben-Cohen 	int ret;
1057400e64dfSOhad Ben-Cohen 
1058400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1059400e64dfSOhad Ben-Cohen 	if (ret) {
1060400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1061400e64dfSOhad Ben-Cohen 		return;
1062400e64dfSOhad Ben-Cohen 	}
1063400e64dfSOhad Ben-Cohen 
1064400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1065400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1066400e64dfSOhad Ben-Cohen 		goto out;
1067400e64dfSOhad Ben-Cohen 
1068400e64dfSOhad Ben-Cohen 	/* power off the remote processor */
1069400e64dfSOhad Ben-Cohen 	ret = rproc->ops->stop(rproc);
1070400e64dfSOhad Ben-Cohen 	if (ret) {
1071400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1072400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't stop rproc: %d\n", ret);
1073400e64dfSOhad Ben-Cohen 		goto out;
1074400e64dfSOhad Ben-Cohen 	}
1075400e64dfSOhad Ben-Cohen 
1076400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1077400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1078400e64dfSOhad Ben-Cohen 
1079400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1080400e64dfSOhad Ben-Cohen 
108170b85ef8SFernando Guzman Lugo 	/* if in crash state, unlock crash handler */
108270b85ef8SFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED)
108370b85ef8SFernando Guzman Lugo 		complete_all(&rproc->crash_comp);
108470b85ef8SFernando Guzman Lugo 
1085400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1086400e64dfSOhad Ben-Cohen 
1087400e64dfSOhad Ben-Cohen 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
1088400e64dfSOhad Ben-Cohen 
1089400e64dfSOhad Ben-Cohen out:
1090400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1091400e64dfSOhad Ben-Cohen 	if (!ret)
1092b5ab5e24SOhad Ben-Cohen 		module_put(dev->parent->driver->owner);
1093400e64dfSOhad Ben-Cohen }
1094400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1095400e64dfSOhad Ben-Cohen 
1096400e64dfSOhad Ben-Cohen /**
1097160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
1098400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
1099400e64dfSOhad Ben-Cohen  *
1100400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
1101400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
1102400e64dfSOhad Ben-Cohen  *
1103400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
1104400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
1105400e64dfSOhad Ben-Cohen  *
1106400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
1107400e64dfSOhad Ben-Cohen  *
1108400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
1109400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
1110400e64dfSOhad Ben-Cohen  * firmware.
1111400e64dfSOhad Ben-Cohen  *
1112400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
11137a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
1114400e64dfSOhad Ben-Cohen  * probed.
1115400e64dfSOhad Ben-Cohen  */
1116160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
1117400e64dfSOhad Ben-Cohen {
1118b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
111970b85ef8SFernando Guzman Lugo 	int ret;
1120400e64dfSOhad Ben-Cohen 
1121b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
1122b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
1123b5ab5e24SOhad Ben-Cohen 		return ret;
1124400e64dfSOhad Ben-Cohen 
1125b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
1126400e64dfSOhad Ben-Cohen 
1127489d129aSOhad Ben-Cohen 	dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1128489d129aSOhad Ben-Cohen 	dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1129489d129aSOhad Ben-Cohen 
1130400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
1131400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
1132400e64dfSOhad Ben-Cohen 
113370b85ef8SFernando Guzman Lugo 	return rproc_add_virtio_devices(rproc);
1134400e64dfSOhad Ben-Cohen }
1135160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
1136400e64dfSOhad Ben-Cohen 
1137400e64dfSOhad Ben-Cohen /**
1138b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
1139b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
1140b5ab5e24SOhad Ben-Cohen  *
1141b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
1142b5ab5e24SOhad Ben-Cohen  *
1143b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
1144b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
1145b5ab5e24SOhad Ben-Cohen  */
1146b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
1147b5ab5e24SOhad Ben-Cohen {
1148b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
1149b5ab5e24SOhad Ben-Cohen 
11507183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
11517183a2a7SOhad Ben-Cohen 
11527183a2a7SOhad Ben-Cohen 	rproc_delete_debug_dir(rproc);
11537183a2a7SOhad Ben-Cohen 
1154b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
1155b5ab5e24SOhad Ben-Cohen 
1156b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
1157b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
1158b5ab5e24SOhad Ben-Cohen 
1159b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
1160b5ab5e24SOhad Ben-Cohen }
1161b5ab5e24SOhad Ben-Cohen 
1162b5ab5e24SOhad Ben-Cohen static struct device_type rproc_type = {
1163b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
1164b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
1165b5ab5e24SOhad Ben-Cohen };
1166400e64dfSOhad Ben-Cohen 
1167400e64dfSOhad Ben-Cohen /**
1168400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
1169400e64dfSOhad Ben-Cohen  * @dev: the underlying device
1170400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
1171400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
1172400e64dfSOhad Ben-Cohen  * @firmware: name of firmware file to load
1173400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
1174400e64dfSOhad Ben-Cohen  *
1175400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
1176400e64dfSOhad Ben-Cohen  * it yet.
1177400e64dfSOhad Ben-Cohen  *
1178400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
1179400e64dfSOhad Ben-Cohen  * of the remote processor.
1180400e64dfSOhad Ben-Cohen  *
1181400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
1182160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
1183400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
1184400e64dfSOhad Ben-Cohen  *
1185400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
1186400e64dfSOhad Ben-Cohen  *
1187400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
1188160e7c84SOhad Ben-Cohen  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
1189400e64dfSOhad Ben-Cohen  */
1190400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
1191400e64dfSOhad Ben-Cohen 				const struct rproc_ops *ops,
1192400e64dfSOhad Ben-Cohen 				const char *firmware, int len)
1193400e64dfSOhad Ben-Cohen {
1194400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
1195400e64dfSOhad Ben-Cohen 
1196400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
1197400e64dfSOhad Ben-Cohen 		return NULL;
1198400e64dfSOhad Ben-Cohen 
1199400e64dfSOhad Ben-Cohen 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
1200400e64dfSOhad Ben-Cohen 	if (!rproc) {
1201400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: kzalloc failed\n", __func__);
1202400e64dfSOhad Ben-Cohen 		return NULL;
1203400e64dfSOhad Ben-Cohen 	}
1204400e64dfSOhad Ben-Cohen 
1205400e64dfSOhad Ben-Cohen 	rproc->name = name;
1206400e64dfSOhad Ben-Cohen 	rproc->ops = ops;
1207400e64dfSOhad Ben-Cohen 	rproc->firmware = firmware;
1208400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
1209400e64dfSOhad Ben-Cohen 
1210b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
1211b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
1212b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
1213b5ab5e24SOhad Ben-Cohen 
1214b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
1215b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1216b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
1217b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1218b5ab5e24SOhad Ben-Cohen 		put_device(&rproc->dev);
1219b5ab5e24SOhad Ben-Cohen 		return NULL;
1220b5ab5e24SOhad Ben-Cohen 	}
1221b5ab5e24SOhad Ben-Cohen 
1222b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1223b5ab5e24SOhad Ben-Cohen 
1224400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
1225400e64dfSOhad Ben-Cohen 
12264afc89d6SSjur Brændeland 	/* Set ELF as the default fw_ops handler */
12274afc89d6SSjur Brændeland 	rproc->fw_ops = &rproc_elf_fw_ops;
1228400e64dfSOhad Ben-Cohen 
1229400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
1230400e64dfSOhad Ben-Cohen 
12317a186941SOhad Ben-Cohen 	idr_init(&rproc->notifyids);
12327a186941SOhad Ben-Cohen 
1233400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
1234400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
1235400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
12367a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
1237400e64dfSOhad Ben-Cohen 
12388afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
123970b85ef8SFernando Guzman Lugo 	init_completion(&rproc->crash_comp);
12408afd519cSFernando Guzman Lugo 
1241400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1242400e64dfSOhad Ben-Cohen 
1243400e64dfSOhad Ben-Cohen 	return rproc;
1244400e64dfSOhad Ben-Cohen }
1245400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
1246400e64dfSOhad Ben-Cohen 
1247400e64dfSOhad Ben-Cohen /**
1248160e7c84SOhad Ben-Cohen  * rproc_put() - unroll rproc_alloc()
1249400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
1250400e64dfSOhad Ben-Cohen  *
1251c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
1252400e64dfSOhad Ben-Cohen  *
1253c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
1254c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
1255400e64dfSOhad Ben-Cohen  */
1256160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
1257400e64dfSOhad Ben-Cohen {
1258b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
1259400e64dfSOhad Ben-Cohen }
1260160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
1261400e64dfSOhad Ben-Cohen 
1262400e64dfSOhad Ben-Cohen /**
1263160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
1264400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
1265400e64dfSOhad Ben-Cohen  *
1266400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
1267400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
1268160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
1269400e64dfSOhad Ben-Cohen  * has completed successfully.
1270400e64dfSOhad Ben-Cohen  *
1271160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
1272c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
1273160e7c84SOhad Ben-Cohen  * one last refcount, one still needs to call rproc_put().
1274400e64dfSOhad Ben-Cohen  *
1275400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
1276400e64dfSOhad Ben-Cohen  */
1277160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
1278400e64dfSOhad Ben-Cohen {
12796db20ea8SOhad Ben-Cohen 	struct rproc_vdev *rvdev, *tmp;
12807a186941SOhad Ben-Cohen 
1281400e64dfSOhad Ben-Cohen 	if (!rproc)
1282400e64dfSOhad Ben-Cohen 		return -EINVAL;
1283400e64dfSOhad Ben-Cohen 
1284400e64dfSOhad Ben-Cohen 	/* if rproc is just being registered, wait */
1285400e64dfSOhad Ben-Cohen 	wait_for_completion(&rproc->firmware_loading_complete);
1286400e64dfSOhad Ben-Cohen 
12877a186941SOhad Ben-Cohen 	/* clean up remote vdev entries */
12886db20ea8SOhad Ben-Cohen 	list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
12897a186941SOhad Ben-Cohen 		rproc_remove_virtio_dev(rvdev);
1290400e64dfSOhad Ben-Cohen 
1291b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
1292400e64dfSOhad Ben-Cohen 
1293400e64dfSOhad Ben-Cohen 	return 0;
1294400e64dfSOhad Ben-Cohen }
1295160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
1296400e64dfSOhad Ben-Cohen 
12978afd519cSFernando Guzman Lugo /**
12988afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
12998afd519cSFernando Guzman Lugo  * @rproc: remote processor
13008afd519cSFernando Guzman Lugo  * @type: crash type
13018afd519cSFernando Guzman Lugo  *
13028afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
13038afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
13048afd519cSFernando Guzman Lugo  * non-remoteproc driver.
13058afd519cSFernando Guzman Lugo  *
13068afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
13078afd519cSFernando Guzman Lugo  */
13088afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
13098afd519cSFernando Guzman Lugo {
13108afd519cSFernando Guzman Lugo 	if (!rproc) {
13118afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
13128afd519cSFernando Guzman Lugo 		return;
13138afd519cSFernando Guzman Lugo 	}
13148afd519cSFernando Guzman Lugo 
13158afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
13168afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
13178afd519cSFernando Guzman Lugo 
13188afd519cSFernando Guzman Lugo 	/* create a new task to handle the error */
13198afd519cSFernando Guzman Lugo 	schedule_work(&rproc->crash_handler);
13208afd519cSFernando Guzman Lugo }
13218afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
13228afd519cSFernando Guzman Lugo 
1323400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
1324400e64dfSOhad Ben-Cohen {
1325400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
1326b5ab5e24SOhad Ben-Cohen 
1327400e64dfSOhad Ben-Cohen 	return 0;
1328400e64dfSOhad Ben-Cohen }
1329400e64dfSOhad Ben-Cohen module_init(remoteproc_init);
1330400e64dfSOhad Ben-Cohen 
1331400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
1332400e64dfSOhad Ben-Cohen {
1333400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
1334400e64dfSOhad Ben-Cohen }
1335400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
1336400e64dfSOhad Ben-Cohen 
1337400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
1338400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
1339