xref: /linux/drivers/remoteproc/remoteproc_core.c (revision 8de3dbd0895bebe52d069a82feae8e3fb51c1bdf)
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>
40a2b950acSOhad Ben-Cohen #include <linux/crc32.h>
41400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h>
42400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h>
43cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h>
44400e64dfSOhad Ben-Cohen 
45400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h"
46400e64dfSOhad Ben-Cohen 
47fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex);
48fec47d86SDave Gerlach static LIST_HEAD(rproc_list);
49fec47d86SDave Gerlach 
50400e64dfSOhad Ben-Cohen typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
51fd2c15ecSOhad Ben-Cohen 				struct resource_table *table, int len);
52a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
53a2b950acSOhad Ben-Cohen 				 void *, int offset, int avail);
54400e64dfSOhad Ben-Cohen 
55b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */
56b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index);
57b5ab5e24SOhad Ben-Cohen 
588afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = {
598afd519cSFernando Guzman Lugo 	[RPROC_MMUFAULT]	= "mmufault",
608afd519cSFernando Guzman Lugo };
618afd519cSFernando Guzman Lugo 
628afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */
638afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type)
648afd519cSFernando Guzman Lugo {
658afd519cSFernando Guzman Lugo 	if (type < ARRAY_SIZE(rproc_crash_names))
668afd519cSFernando Guzman Lugo 		return rproc_crash_names[type];
67b23f7a09SMasanari Iida 	return "unknown";
688afd519cSFernando Guzman Lugo }
698afd519cSFernando Guzman Lugo 
70400e64dfSOhad Ben-Cohen /*
71400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
72400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
73400e64dfSOhad Ben-Cohen  * an IOMMU).
74400e64dfSOhad Ben-Cohen  *
75400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
76400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
77400e64dfSOhad Ben-Cohen  */
78400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
7977ca2332SOhad Ben-Cohen 		unsigned long iova, int flags, void *token)
80400e64dfSOhad Ben-Cohen {
818afd519cSFernando Guzman Lugo 	struct rproc *rproc = token;
828afd519cSFernando Guzman Lugo 
83400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
84400e64dfSOhad Ben-Cohen 
858afd519cSFernando Guzman Lugo 	rproc_report_crash(rproc, RPROC_MMUFAULT);
868afd519cSFernando Guzman Lugo 
87400e64dfSOhad Ben-Cohen 	/*
88400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
898afd519cSFernando Guzman Lugo 	 * we just used it as a recovery trigger.
90400e64dfSOhad Ben-Cohen 	 */
91400e64dfSOhad Ben-Cohen 	return -ENOSYS;
92400e64dfSOhad Ben-Cohen }
93400e64dfSOhad Ben-Cohen 
94400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
95400e64dfSOhad Ben-Cohen {
96400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
97b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
98400e64dfSOhad Ben-Cohen 	int ret;
99400e64dfSOhad Ben-Cohen 
100315491e5SSuman Anna 	if (!rproc->has_iommu) {
101315491e5SSuman Anna 		dev_dbg(dev, "iommu not present\n");
1020798e1daSMark Grosen 		return 0;
103400e64dfSOhad Ben-Cohen 	}
104400e64dfSOhad Ben-Cohen 
105400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
106400e64dfSOhad Ben-Cohen 	if (!domain) {
107400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
108400e64dfSOhad Ben-Cohen 		return -ENOMEM;
109400e64dfSOhad Ben-Cohen 	}
110400e64dfSOhad Ben-Cohen 
11177ca2332SOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
112400e64dfSOhad Ben-Cohen 
113400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
114400e64dfSOhad Ben-Cohen 	if (ret) {
115400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
116400e64dfSOhad Ben-Cohen 		goto free_domain;
117400e64dfSOhad Ben-Cohen 	}
118400e64dfSOhad Ben-Cohen 
119400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
120400e64dfSOhad Ben-Cohen 
121400e64dfSOhad Ben-Cohen 	return 0;
122400e64dfSOhad Ben-Cohen 
123400e64dfSOhad Ben-Cohen free_domain:
124400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
125400e64dfSOhad Ben-Cohen 	return ret;
126400e64dfSOhad Ben-Cohen }
127400e64dfSOhad Ben-Cohen 
128400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
129400e64dfSOhad Ben-Cohen {
130400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
131b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
132400e64dfSOhad Ben-Cohen 
133400e64dfSOhad Ben-Cohen 	if (!domain)
134400e64dfSOhad Ben-Cohen 		return;
135400e64dfSOhad Ben-Cohen 
136400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
137400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
138400e64dfSOhad Ben-Cohen }
139400e64dfSOhad Ben-Cohen 
140a01f7cd6SSuman Anna /**
141a01f7cd6SSuman Anna  * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
142a01f7cd6SSuman Anna  * @rproc: handle of a remote processor
143a01f7cd6SSuman Anna  * @da: remoteproc device address to translate
144a01f7cd6SSuman Anna  * @len: length of the memory region @da is pointing to
145a01f7cd6SSuman Anna  *
146400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
147400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
148a01f7cd6SSuman Anna  * device addresses (which are hardcoded in the firmware). They may also have
149a01f7cd6SSuman Anna  * dedicated memory regions internal to the processors, and use them either
150a01f7cd6SSuman Anna  * exclusively or alongside carveouts.
151400e64dfSOhad Ben-Cohen  *
152400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
153400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
154400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
155400e64dfSOhad Ben-Cohen  *
156a01f7cd6SSuman Anna  * This function is a helper function with which we can go over the allocated
157a01f7cd6SSuman Anna  * carveouts and translate specific device addresses to kernel virtual addresses
158a01f7cd6SSuman Anna  * so we can access the referenced memory. This function also allows to perform
159a01f7cd6SSuman Anna  * translations on the internal remoteproc memory regions through a platform
160a01f7cd6SSuman Anna  * implementation specific da_to_va ops, if present.
161a01f7cd6SSuman Anna  *
162a01f7cd6SSuman Anna  * The function returns a valid kernel address on success or NULL on failure.
163400e64dfSOhad Ben-Cohen  *
164400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
165400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
166a01f7cd6SSuman Anna  * here the output of the DMA API for the carveouts, which should be more
167a01f7cd6SSuman Anna  * correct.
168400e64dfSOhad Ben-Cohen  */
16972854fb0SSjur Brændeland void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
170400e64dfSOhad Ben-Cohen {
171400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
172400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
173400e64dfSOhad Ben-Cohen 
174a01f7cd6SSuman Anna 	if (rproc->ops->da_to_va) {
175a01f7cd6SSuman Anna 		ptr = rproc->ops->da_to_va(rproc, da, len);
176a01f7cd6SSuman Anna 		if (ptr)
177a01f7cd6SSuman Anna 			goto out;
178a01f7cd6SSuman Anna 	}
179a01f7cd6SSuman Anna 
180400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
181400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
182400e64dfSOhad Ben-Cohen 
183400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
184400e64dfSOhad Ben-Cohen 		if (offset < 0)
185400e64dfSOhad Ben-Cohen 			continue;
186400e64dfSOhad Ben-Cohen 
187400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
188400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
189400e64dfSOhad Ben-Cohen 			continue;
190400e64dfSOhad Ben-Cohen 
191400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
192400e64dfSOhad Ben-Cohen 
193400e64dfSOhad Ben-Cohen 		break;
194400e64dfSOhad Ben-Cohen 	}
195400e64dfSOhad Ben-Cohen 
196a01f7cd6SSuman Anna out:
197400e64dfSOhad Ben-Cohen 	return ptr;
198400e64dfSOhad Ben-Cohen }
1994afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
200400e64dfSOhad Ben-Cohen 
2016db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
202400e64dfSOhad Ben-Cohen {
2037a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
204b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
2056db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
206c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
2077a186941SOhad Ben-Cohen 	dma_addr_t dma;
2087a186941SOhad Ben-Cohen 	void *va;
2097a186941SOhad Ben-Cohen 	int ret, size, notifyid;
210400e64dfSOhad Ben-Cohen 
2116db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
2126db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2136db20ea8SOhad Ben-Cohen 
2146db20ea8SOhad Ben-Cohen 	/*
2156db20ea8SOhad Ben-Cohen 	 * Allocate non-cacheable memory for the vring. In the future
2166db20ea8SOhad Ben-Cohen 	 * this call will also configure the IOMMU for us
2176db20ea8SOhad Ben-Cohen 	 */
218b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
2196db20ea8SOhad Ben-Cohen 	if (!va) {
220b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent failed\n");
2216db20ea8SOhad Ben-Cohen 		return -EINVAL;
2226db20ea8SOhad Ben-Cohen 	}
2236db20ea8SOhad Ben-Cohen 
2246db20ea8SOhad Ben-Cohen 	/*
2256db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
2266db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
2276db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
2286db20ea8SOhad Ben-Cohen 	 */
22915fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
230b39599b7SSuman Anna 	if (ret < 0) {
23115fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
232b5ab5e24SOhad Ben-Cohen 		dma_free_coherent(dev->parent, size, va, dma);
2336db20ea8SOhad Ben-Cohen 		return ret;
2346db20ea8SOhad Ben-Cohen 	}
23515fc6110STejun Heo 	notifyid = ret;
2366db20ea8SOhad Ben-Cohen 
237d09f53a7SEmil Goode 	dev_dbg(dev, "vring%d: va %p dma %llx size %x idr %d\n", i, va,
238d09f53a7SEmil Goode 				(unsigned long long)dma, size, notifyid);
2396db20ea8SOhad Ben-Cohen 
2406db20ea8SOhad Ben-Cohen 	rvring->va = va;
2416db20ea8SOhad Ben-Cohen 	rvring->dma = dma;
2426db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
2436db20ea8SOhad Ben-Cohen 
244c0d63157SSjur Brændeland 	/*
245c0d63157SSjur Brændeland 	 * Let the rproc know the notifyid and da of this vring.
246c0d63157SSjur Brændeland 	 * Not all platforms use dma_alloc_coherent to automatically
247c0d63157SSjur Brændeland 	 * set up the iommu. In this case the device address (da) will
248c0d63157SSjur Brændeland 	 * hold the physical address and not the device address.
249c0d63157SSjur Brændeland 	 */
250c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
251c0d63157SSjur Brændeland 	rsc->vring[i].da = dma;
252c0d63157SSjur Brændeland 	rsc->vring[i].notifyid = notifyid;
2536db20ea8SOhad Ben-Cohen 	return 0;
2546db20ea8SOhad Ben-Cohen }
2556db20ea8SOhad Ben-Cohen 
256400e64dfSOhad Ben-Cohen static int
2576db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
258400e64dfSOhad Ben-Cohen {
259400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
260b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
261400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
2626db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
263400e64dfSOhad Ben-Cohen 
2647a186941SOhad Ben-Cohen 	dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
2657a186941SOhad Ben-Cohen 				i, vring->da, vring->num, vring->align);
2667a186941SOhad Ben-Cohen 
2677a186941SOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
2687a186941SOhad Ben-Cohen 	if (vring->reserved) {
2697a186941SOhad Ben-Cohen 		dev_err(dev, "vring rsc has non zero reserved bytes\n");
270fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
271fd2c15ecSOhad Ben-Cohen 	}
272fd2c15ecSOhad Ben-Cohen 
27363140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
27463140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
27563140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
27663140e0eSOhad Ben-Cohen 						vring->num, vring->align);
277400e64dfSOhad Ben-Cohen 		return -EINVAL;
278400e64dfSOhad Ben-Cohen 	}
279400e64dfSOhad Ben-Cohen 
2806db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
2816db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
2826db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
283400e64dfSOhad Ben-Cohen 
284400e64dfSOhad Ben-Cohen 	return 0;
285400e64dfSOhad Ben-Cohen }
286400e64dfSOhad Ben-Cohen 
2876db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
2887a186941SOhad Ben-Cohen {
28963140e0eSOhad Ben-Cohen 	int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2906db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
291c0d63157SSjur Brændeland 	int idx = rvring->rvdev->vring - rvring;
292c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
2937a186941SOhad Ben-Cohen 
294b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
2957a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
296099a3f33SSjur Brændeland 
297c0d63157SSjur Brændeland 	/* reset resource entry info */
298c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
299c0d63157SSjur Brændeland 	rsc->vring[idx].da = 0;
300c0d63157SSjur Brændeland 	rsc->vring[idx].notifyid = -1;
3017a186941SOhad Ben-Cohen }
3027a186941SOhad Ben-Cohen 
303400e64dfSOhad Ben-Cohen /**
304fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
305400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
306400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
307fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
308400e64dfSOhad Ben-Cohen  *
3097a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
3107a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
3117a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
3127a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
313400e64dfSOhad Ben-Cohen  *
3147a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
3157a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
3167a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
3177a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
3187a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
319400e64dfSOhad Ben-Cohen  *
3207a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
3217a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
3227a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
3237a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
3247a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
3257a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
3267a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
327400e64dfSOhad Ben-Cohen  *
328400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
329400e64dfSOhad Ben-Cohen  */
330fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
331a2b950acSOhad Ben-Cohen 							int offset, int avail)
332400e64dfSOhad Ben-Cohen {
333b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3347a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
3357a186941SOhad Ben-Cohen 	int i, ret;
336fd2c15ecSOhad Ben-Cohen 
337fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
338fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
339fd2c15ecSOhad Ben-Cohen 			+ rsc->config_len > avail) {
340b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
341fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
342fd2c15ecSOhad Ben-Cohen 	}
343fd2c15ecSOhad Ben-Cohen 
344fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
345fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
346fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
347fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
348fd2c15ecSOhad Ben-Cohen 	}
349fd2c15ecSOhad Ben-Cohen 
350fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
351fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
352400e64dfSOhad Ben-Cohen 
3537a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
3547a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
355fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
356400e64dfSOhad Ben-Cohen 		return -EINVAL;
357400e64dfSOhad Ben-Cohen 	}
358400e64dfSOhad Ben-Cohen 
3597a186941SOhad Ben-Cohen 	rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
3607a186941SOhad Ben-Cohen 	if (!rvdev)
3617a186941SOhad Ben-Cohen 		return -ENOMEM;
3627a186941SOhad Ben-Cohen 
3637a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
3647a186941SOhad Ben-Cohen 
3656db20ea8SOhad Ben-Cohen 	/* parse the vrings */
366fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
3676db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
3687a186941SOhad Ben-Cohen 		if (ret)
3696db20ea8SOhad Ben-Cohen 			goto free_rvdev;
370fd2c15ecSOhad Ben-Cohen 	}
371fd2c15ecSOhad Ben-Cohen 
372a2b950acSOhad Ben-Cohen 	/* remember the resource offset*/
373a2b950acSOhad Ben-Cohen 	rvdev->rsc_offset = offset;
374400e64dfSOhad Ben-Cohen 
3757a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
376400e64dfSOhad Ben-Cohen 
3777a186941SOhad Ben-Cohen 	/* it is now safe to add the virtio device */
3787a186941SOhad Ben-Cohen 	ret = rproc_add_virtio_dev(rvdev, rsc->id);
3797a186941SOhad Ben-Cohen 	if (ret)
380cde42e07SSjur Brændeland 		goto remove_rvdev;
381400e64dfSOhad Ben-Cohen 
382400e64dfSOhad Ben-Cohen 	return 0;
3837a186941SOhad Ben-Cohen 
384cde42e07SSjur Brændeland remove_rvdev:
385cde42e07SSjur Brændeland 	list_del(&rvdev->node);
3866db20ea8SOhad Ben-Cohen free_rvdev:
3877a186941SOhad Ben-Cohen 	kfree(rvdev);
3887a186941SOhad Ben-Cohen 	return ret;
389400e64dfSOhad Ben-Cohen }
390400e64dfSOhad Ben-Cohen 
391400e64dfSOhad Ben-Cohen /**
392400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
393400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
394400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
395fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
396400e64dfSOhad Ben-Cohen  *
397400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
398400e64dfSOhad Ben-Cohen  * export it via debugfs.
399400e64dfSOhad Ben-Cohen  *
400400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
401400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
402400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
403400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
404400e64dfSOhad Ben-Cohen  *
405400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
406400e64dfSOhad Ben-Cohen  */
407fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
408a2b950acSOhad Ben-Cohen 							int offset, int avail)
409400e64dfSOhad Ben-Cohen {
410400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *trace;
411b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
412400e64dfSOhad Ben-Cohen 	void *ptr;
413400e64dfSOhad Ben-Cohen 	char name[15];
414400e64dfSOhad Ben-Cohen 
415fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
416b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
417fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
418fd2c15ecSOhad Ben-Cohen 	}
419fd2c15ecSOhad Ben-Cohen 
420fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
421fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
422fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
423fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
424fd2c15ecSOhad Ben-Cohen 	}
425fd2c15ecSOhad Ben-Cohen 
426400e64dfSOhad Ben-Cohen 	/* what's the kernel address of this resource ? */
427400e64dfSOhad Ben-Cohen 	ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
428400e64dfSOhad Ben-Cohen 	if (!ptr) {
429400e64dfSOhad Ben-Cohen 		dev_err(dev, "erroneous trace resource entry\n");
430400e64dfSOhad Ben-Cohen 		return -EINVAL;
431400e64dfSOhad Ben-Cohen 	}
432400e64dfSOhad Ben-Cohen 
433400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
434172e6ab1SSuman Anna 	if (!trace)
435400e64dfSOhad Ben-Cohen 		return -ENOMEM;
436400e64dfSOhad Ben-Cohen 
437400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
438400e64dfSOhad Ben-Cohen 	trace->len = rsc->len;
439400e64dfSOhad Ben-Cohen 	trace->va = ptr;
440400e64dfSOhad Ben-Cohen 
441400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
442400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
443400e64dfSOhad Ben-Cohen 
444400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
445400e64dfSOhad Ben-Cohen 	trace->priv = rproc_create_trace_file(name, rproc, trace);
446400e64dfSOhad Ben-Cohen 	if (!trace->priv) {
447400e64dfSOhad Ben-Cohen 		trace->va = NULL;
448400e64dfSOhad Ben-Cohen 		kfree(trace);
449400e64dfSOhad Ben-Cohen 		return -EINVAL;
450400e64dfSOhad Ben-Cohen 	}
451400e64dfSOhad Ben-Cohen 
452400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
453400e64dfSOhad Ben-Cohen 
454400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
455400e64dfSOhad Ben-Cohen 
456fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
457400e64dfSOhad Ben-Cohen 						rsc->da, rsc->len);
458400e64dfSOhad Ben-Cohen 
459400e64dfSOhad Ben-Cohen 	return 0;
460400e64dfSOhad Ben-Cohen }
461400e64dfSOhad Ben-Cohen 
462400e64dfSOhad Ben-Cohen /**
463400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
464400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
465400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
466fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
467400e64dfSOhad Ben-Cohen  *
468400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
469400e64dfSOhad Ben-Cohen  *
470400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
471400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
472400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
473400e64dfSOhad Ben-Cohen  *
474400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
475400e64dfSOhad Ben-Cohen  *
476400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
477400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
478400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
479400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
480400e64dfSOhad Ben-Cohen  *
481400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
482400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
483400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
484400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
485400e64dfSOhad Ben-Cohen  * are outside those ranges.
486400e64dfSOhad Ben-Cohen  */
487fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
488a2b950acSOhad Ben-Cohen 							int offset, int avail)
489400e64dfSOhad Ben-Cohen {
490400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
491b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
492400e64dfSOhad Ben-Cohen 	int ret;
493400e64dfSOhad Ben-Cohen 
494400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
495400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
496400e64dfSOhad Ben-Cohen 		return -EINVAL;
497400e64dfSOhad Ben-Cohen 
498fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
499b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
500fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
501fd2c15ecSOhad Ben-Cohen 	}
502fd2c15ecSOhad Ben-Cohen 
503fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
504fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
505b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
506fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
507fd2c15ecSOhad Ben-Cohen 	}
508fd2c15ecSOhad Ben-Cohen 
509400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
510172e6ab1SSuman Anna 	if (!mapping)
511400e64dfSOhad Ben-Cohen 		return -ENOMEM;
512400e64dfSOhad Ben-Cohen 
513400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
514400e64dfSOhad Ben-Cohen 	if (ret) {
515b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
516400e64dfSOhad Ben-Cohen 		goto out;
517400e64dfSOhad Ben-Cohen 	}
518400e64dfSOhad Ben-Cohen 
519400e64dfSOhad Ben-Cohen 	/*
520400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
521400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
522400e64dfSOhad Ben-Cohen 	 *
523400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
524400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
525400e64dfSOhad Ben-Cohen 	 */
526400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
527400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
528400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
529400e64dfSOhad Ben-Cohen 
530b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
531400e64dfSOhad Ben-Cohen 					rsc->pa, rsc->da, rsc->len);
532400e64dfSOhad Ben-Cohen 
533400e64dfSOhad Ben-Cohen 	return 0;
534400e64dfSOhad Ben-Cohen 
535400e64dfSOhad Ben-Cohen out:
536400e64dfSOhad Ben-Cohen 	kfree(mapping);
537400e64dfSOhad Ben-Cohen 	return ret;
538400e64dfSOhad Ben-Cohen }
539400e64dfSOhad Ben-Cohen 
540400e64dfSOhad Ben-Cohen /**
541400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
542400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
543400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
544fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
545400e64dfSOhad Ben-Cohen  *
546400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
547400e64dfSOhad Ben-Cohen  * contiguous memory regions.
548400e64dfSOhad Ben-Cohen  *
549400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
550400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
551400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
552400e64dfSOhad Ben-Cohen  *
553400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
554400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
555400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
556400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
557400e64dfSOhad Ben-Cohen  */
558fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
559a2b950acSOhad Ben-Cohen 						struct fw_rsc_carveout *rsc,
560a2b950acSOhad Ben-Cohen 						int offset, int avail)
561a2b950acSOhad Ben-Cohen 
562400e64dfSOhad Ben-Cohen {
563400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout, *mapping;
564b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
565400e64dfSOhad Ben-Cohen 	dma_addr_t dma;
566400e64dfSOhad Ben-Cohen 	void *va;
567400e64dfSOhad Ben-Cohen 	int ret;
568400e64dfSOhad Ben-Cohen 
569fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
570b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
571fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
572fd2c15ecSOhad Ben-Cohen 	}
573fd2c15ecSOhad Ben-Cohen 
574fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
575fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
576fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
577fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
578fd2c15ecSOhad Ben-Cohen 	}
579fd2c15ecSOhad Ben-Cohen 
580fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
581fd2c15ecSOhad Ben-Cohen 			rsc->da, rsc->pa, rsc->len, rsc->flags);
582fd2c15ecSOhad Ben-Cohen 
583400e64dfSOhad Ben-Cohen 	carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
584172e6ab1SSuman Anna 	if (!carveout)
5857168d914SDan Carpenter 		return -ENOMEM;
586400e64dfSOhad Ben-Cohen 
587b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
588400e64dfSOhad Ben-Cohen 	if (!va) {
589b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
590400e64dfSOhad Ben-Cohen 		ret = -ENOMEM;
591400e64dfSOhad Ben-Cohen 		goto free_carv;
592400e64dfSOhad Ben-Cohen 	}
593400e64dfSOhad Ben-Cohen 
594d09f53a7SEmil Goode 	dev_dbg(dev, "carveout va %p, dma %llx, len 0x%x\n", va,
595d09f53a7SEmil Goode 					(unsigned long long)dma, rsc->len);
596400e64dfSOhad Ben-Cohen 
597400e64dfSOhad Ben-Cohen 	/*
598400e64dfSOhad Ben-Cohen 	 * Ok, this is non-standard.
599400e64dfSOhad Ben-Cohen 	 *
600400e64dfSOhad Ben-Cohen 	 * Sometimes we can't rely on the generic iommu-based DMA API
601400e64dfSOhad Ben-Cohen 	 * to dynamically allocate the device address and then set the IOMMU
602400e64dfSOhad Ben-Cohen 	 * tables accordingly, because some remote processors might
603400e64dfSOhad Ben-Cohen 	 * _require_ us to use hard coded device addresses that their
604400e64dfSOhad Ben-Cohen 	 * firmware was compiled with.
605400e64dfSOhad Ben-Cohen 	 *
606400e64dfSOhad Ben-Cohen 	 * In this case, we must use the IOMMU API directly and map
607400e64dfSOhad Ben-Cohen 	 * the memory to the device address as expected by the remote
608400e64dfSOhad Ben-Cohen 	 * processor.
609400e64dfSOhad Ben-Cohen 	 *
610400e64dfSOhad Ben-Cohen 	 * Obviously such remote processor devices should not be configured
611400e64dfSOhad Ben-Cohen 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
612400e64dfSOhad Ben-Cohen 	 * physical address in this case.
613400e64dfSOhad Ben-Cohen 	 */
614400e64dfSOhad Ben-Cohen 	if (rproc->domain) {
6157168d914SDan Carpenter 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
6167168d914SDan Carpenter 		if (!mapping) {
6177168d914SDan Carpenter 			dev_err(dev, "kzalloc mapping failed\n");
6187168d914SDan Carpenter 			ret = -ENOMEM;
6197168d914SDan Carpenter 			goto dma_free;
6207168d914SDan Carpenter 		}
6217168d914SDan Carpenter 
622400e64dfSOhad Ben-Cohen 		ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
623400e64dfSOhad Ben-Cohen 								rsc->flags);
624400e64dfSOhad Ben-Cohen 		if (ret) {
625400e64dfSOhad Ben-Cohen 			dev_err(dev, "iommu_map failed: %d\n", ret);
6267168d914SDan Carpenter 			goto free_mapping;
627400e64dfSOhad Ben-Cohen 		}
628400e64dfSOhad Ben-Cohen 
629400e64dfSOhad Ben-Cohen 		/*
630400e64dfSOhad Ben-Cohen 		 * We'll need this info later when we'll want to unmap
631400e64dfSOhad Ben-Cohen 		 * everything (e.g. on shutdown).
632400e64dfSOhad Ben-Cohen 		 *
633400e64dfSOhad Ben-Cohen 		 * We can't trust the remote processor not to change the
634400e64dfSOhad Ben-Cohen 		 * resource table, so we must maintain this info independently.
635400e64dfSOhad Ben-Cohen 		 */
636400e64dfSOhad Ben-Cohen 		mapping->da = rsc->da;
637400e64dfSOhad Ben-Cohen 		mapping->len = rsc->len;
638400e64dfSOhad Ben-Cohen 		list_add_tail(&mapping->node, &rproc->mappings);
639400e64dfSOhad Ben-Cohen 
640d09f53a7SEmil Goode 		dev_dbg(dev, "carveout mapped 0x%x to 0x%llx\n",
641d09f53a7SEmil Goode 					rsc->da, (unsigned long long)dma);
6420e49b72cSOhad Ben-Cohen 	}
643400e64dfSOhad Ben-Cohen 
644400e64dfSOhad Ben-Cohen 	/*
645400e64dfSOhad Ben-Cohen 	 * Some remote processors might need to know the pa
646400e64dfSOhad Ben-Cohen 	 * even though they are behind an IOMMU. E.g., OMAP4's
647400e64dfSOhad Ben-Cohen 	 * remote M3 processor needs this so it can control
648400e64dfSOhad Ben-Cohen 	 * on-chip hardware accelerators that are not behind
649400e64dfSOhad Ben-Cohen 	 * the IOMMU, and therefor must know the pa.
650400e64dfSOhad Ben-Cohen 	 *
651400e64dfSOhad Ben-Cohen 	 * Generally we don't want to expose physical addresses
652400e64dfSOhad Ben-Cohen 	 * if we don't have to (remote processors are generally
653400e64dfSOhad Ben-Cohen 	 * _not_ trusted), so we might want to do this only for
654400e64dfSOhad Ben-Cohen 	 * remote processor that _must_ have this (e.g. OMAP4's
655400e64dfSOhad Ben-Cohen 	 * dual M3 subsystem).
6560e49b72cSOhad Ben-Cohen 	 *
6570e49b72cSOhad Ben-Cohen 	 * Non-IOMMU processors might also want to have this info.
6580e49b72cSOhad Ben-Cohen 	 * In this case, the device address and the physical address
6590e49b72cSOhad Ben-Cohen 	 * are the same.
660400e64dfSOhad Ben-Cohen 	 */
661400e64dfSOhad Ben-Cohen 	rsc->pa = dma;
662400e64dfSOhad Ben-Cohen 
663400e64dfSOhad Ben-Cohen 	carveout->va = va;
664400e64dfSOhad Ben-Cohen 	carveout->len = rsc->len;
665400e64dfSOhad Ben-Cohen 	carveout->dma = dma;
666400e64dfSOhad Ben-Cohen 	carveout->da = rsc->da;
667400e64dfSOhad Ben-Cohen 
668400e64dfSOhad Ben-Cohen 	list_add_tail(&carveout->node, &rproc->carveouts);
669400e64dfSOhad Ben-Cohen 
670400e64dfSOhad Ben-Cohen 	return 0;
671400e64dfSOhad Ben-Cohen 
6727168d914SDan Carpenter free_mapping:
6737168d914SDan Carpenter 	kfree(mapping);
674400e64dfSOhad Ben-Cohen dma_free:
675b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(dev->parent, rsc->len, va, dma);
676400e64dfSOhad Ben-Cohen free_carv:
677400e64dfSOhad Ben-Cohen 	kfree(carveout);
678400e64dfSOhad Ben-Cohen 	return ret;
679400e64dfSOhad Ben-Cohen }
680400e64dfSOhad Ben-Cohen 
681ba7290e0SSjur Brændeland static int rproc_count_vrings(struct rproc *rproc, struct fw_rsc_vdev *rsc,
682a2b950acSOhad Ben-Cohen 			      int offset, int avail)
683ba7290e0SSjur Brændeland {
684ba7290e0SSjur Brændeland 	/* Summarize the number of notification IDs */
685ba7290e0SSjur Brændeland 	rproc->max_notifyid += rsc->num_of_vrings;
686ba7290e0SSjur Brændeland 
687ba7290e0SSjur Brændeland 	return 0;
688ba7290e0SSjur Brændeland }
689ba7290e0SSjur Brændeland 
690e12bc14bSOhad Ben-Cohen /*
691e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
692e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
693e12bc14bSOhad Ben-Cohen  */
694232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
695fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
696fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
697fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
6987a186941SOhad Ben-Cohen 	[RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
699e12bc14bSOhad Ben-Cohen };
700e12bc14bSOhad Ben-Cohen 
701232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_vdev_handler[RSC_LAST] = {
702232fcdbbSSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
703232fcdbbSSjur Brændeland };
704232fcdbbSSjur Brændeland 
705ba7290e0SSjur Brændeland static rproc_handle_resource_t rproc_count_vrings_handler[RSC_LAST] = {
706ba7290e0SSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_count_vrings,
707ba7290e0SSjur Brændeland };
708ba7290e0SSjur Brændeland 
709400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
710a2b950acSOhad Ben-Cohen static int rproc_handle_resources(struct rproc *rproc, int len,
711232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
712400e64dfSOhad Ben-Cohen {
713b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
714e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
715fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
716400e64dfSOhad Ben-Cohen 
717a2b950acSOhad Ben-Cohen 	for (i = 0; i < rproc->table_ptr->num; i++) {
718a2b950acSOhad Ben-Cohen 		int offset = rproc->table_ptr->offset[i];
719a2b950acSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
720fd2c15ecSOhad Ben-Cohen 		int avail = len - offset - sizeof(*hdr);
721fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
722400e64dfSOhad Ben-Cohen 
723fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
724fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
725fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
726fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
727fd2c15ecSOhad Ben-Cohen 		}
728fd2c15ecSOhad Ben-Cohen 
729fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
730fd2c15ecSOhad Ben-Cohen 
731fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
732fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
733e12bc14bSOhad Ben-Cohen 			continue;
734400e64dfSOhad Ben-Cohen 		}
735400e64dfSOhad Ben-Cohen 
736232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
737e12bc14bSOhad Ben-Cohen 		if (!handler)
738e12bc14bSOhad Ben-Cohen 			continue;
739e12bc14bSOhad Ben-Cohen 
740a2b950acSOhad Ben-Cohen 		ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
7417a186941SOhad Ben-Cohen 		if (ret)
742400e64dfSOhad Ben-Cohen 			break;
743400e64dfSOhad Ben-Cohen 	}
744400e64dfSOhad Ben-Cohen 
745400e64dfSOhad Ben-Cohen 	return ret;
746400e64dfSOhad Ben-Cohen }
747400e64dfSOhad Ben-Cohen 
748400e64dfSOhad Ben-Cohen /**
749400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
750400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
751400e64dfSOhad Ben-Cohen  *
752400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
7537a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
754400e64dfSOhad Ben-Cohen  */
755400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc)
756400e64dfSOhad Ben-Cohen {
757400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
758b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
759400e64dfSOhad Ben-Cohen 
760400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
761400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
762400e64dfSOhad Ben-Cohen 		rproc_remove_trace_file(entry->priv);
763400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
764400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
765400e64dfSOhad Ben-Cohen 		kfree(entry);
766400e64dfSOhad Ben-Cohen 	}
767400e64dfSOhad Ben-Cohen 
768400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
769400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
770400e64dfSOhad Ben-Cohen 		size_t unmapped;
771400e64dfSOhad Ben-Cohen 
772400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
773400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
774400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
775e981f6d4SSjur Brændeland 			dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
776400e64dfSOhad Ben-Cohen 								unmapped);
777400e64dfSOhad Ben-Cohen 		}
778400e64dfSOhad Ben-Cohen 
779400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
780400e64dfSOhad Ben-Cohen 		kfree(entry);
781400e64dfSOhad Ben-Cohen 	}
782b6356a01SSuman Anna 
783b6356a01SSuman Anna 	/* clean up carveout allocations */
784b6356a01SSuman Anna 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
785172e6ab1SSuman Anna 		dma_free_coherent(dev->parent, entry->len, entry->va,
786172e6ab1SSuman Anna 				  entry->dma);
787b6356a01SSuman Anna 		list_del(&entry->node);
788b6356a01SSuman Anna 		kfree(entry);
789b6356a01SSuman Anna 	}
790400e64dfSOhad Ben-Cohen }
791400e64dfSOhad Ben-Cohen 
792400e64dfSOhad Ben-Cohen /*
793400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
794400e64dfSOhad Ben-Cohen  */
795400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
796400e64dfSOhad Ben-Cohen {
797b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
798400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
799a2b950acSOhad Ben-Cohen 	struct resource_table *table, *loaded_table;
8001e3e2c7cSOhad Ben-Cohen 	int ret, tablesz;
801400e64dfSOhad Ben-Cohen 
802a2b950acSOhad Ben-Cohen 	if (!rproc->table_ptr)
803a2b950acSOhad Ben-Cohen 		return -ENOMEM;
804a2b950acSOhad Ben-Cohen 
805400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
806400e64dfSOhad Ben-Cohen 	if (ret)
807400e64dfSOhad Ben-Cohen 		return ret;
808400e64dfSOhad Ben-Cohen 
809e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
810400e64dfSOhad Ben-Cohen 
811400e64dfSOhad Ben-Cohen 	/*
812400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
813400e64dfSOhad Ben-Cohen 	 * just a nop
814400e64dfSOhad Ben-Cohen 	 */
815400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
816400e64dfSOhad Ben-Cohen 	if (ret) {
817400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
818400e64dfSOhad Ben-Cohen 		return ret;
819400e64dfSOhad Ben-Cohen 	}
820400e64dfSOhad Ben-Cohen 
8213e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
82289970d28SWei Yongjun 	ret = -EINVAL;
823400e64dfSOhad Ben-Cohen 
8241e3e2c7cSOhad Ben-Cohen 	/* look for the resource table */
825bd484984SSjur Brændeland 	table = rproc_find_rsc_table(rproc, fw, &tablesz);
826172e6ab1SSuman Anna 	if (!table)
8271e3e2c7cSOhad Ben-Cohen 		goto clean_up;
8281e3e2c7cSOhad Ben-Cohen 
829a2b950acSOhad Ben-Cohen 	/* Verify that resource table in loaded fw is unchanged */
830a2b950acSOhad Ben-Cohen 	if (rproc->table_csum != crc32(0, table, tablesz)) {
831a2b950acSOhad Ben-Cohen 		dev_err(dev, "resource checksum failed, fw changed?\n");
832a2b950acSOhad Ben-Cohen 		goto clean_up;
833a2b950acSOhad Ben-Cohen 	}
834a2b950acSOhad Ben-Cohen 
835400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
836a2b950acSOhad Ben-Cohen 	ret = rproc_handle_resources(rproc, tablesz, rproc_loading_handlers);
837400e64dfSOhad Ben-Cohen 	if (ret) {
838400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
839400e64dfSOhad Ben-Cohen 		goto clean_up;
840400e64dfSOhad Ben-Cohen 	}
841400e64dfSOhad Ben-Cohen 
842400e64dfSOhad Ben-Cohen 	/* load the ELF segments to memory */
843bd484984SSjur Brændeland 	ret = rproc_load_segments(rproc, fw);
844400e64dfSOhad Ben-Cohen 	if (ret) {
845400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to load program segments: %d\n", ret);
846400e64dfSOhad Ben-Cohen 		goto clean_up;
847400e64dfSOhad Ben-Cohen 	}
848400e64dfSOhad Ben-Cohen 
849a2b950acSOhad Ben-Cohen 	/*
850a2b950acSOhad Ben-Cohen 	 * The starting device has been given the rproc->cached_table as the
851a2b950acSOhad Ben-Cohen 	 * resource table. The address of the vring along with the other
852a2b950acSOhad Ben-Cohen 	 * allocated resources (carveouts etc) is stored in cached_table.
853a2b950acSOhad Ben-Cohen 	 * In order to pass this information to the remote device we must
854a2b950acSOhad Ben-Cohen 	 * copy this information to device memory.
855a2b950acSOhad Ben-Cohen 	 */
856a2b950acSOhad Ben-Cohen 	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
85789970d28SWei Yongjun 	if (!loaded_table) {
85889970d28SWei Yongjun 		ret = -EINVAL;
859a2b950acSOhad Ben-Cohen 		goto clean_up;
86089970d28SWei Yongjun 	}
861a2b950acSOhad Ben-Cohen 
862a2b950acSOhad Ben-Cohen 	memcpy(loaded_table, rproc->cached_table, tablesz);
863a2b950acSOhad Ben-Cohen 
864400e64dfSOhad Ben-Cohen 	/* power up the remote processor */
865400e64dfSOhad Ben-Cohen 	ret = rproc->ops->start(rproc);
866400e64dfSOhad Ben-Cohen 	if (ret) {
867400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
868400e64dfSOhad Ben-Cohen 		goto clean_up;
869400e64dfSOhad Ben-Cohen 	}
870400e64dfSOhad Ben-Cohen 
871a2b950acSOhad Ben-Cohen 	/*
872a2b950acSOhad Ben-Cohen 	 * Update table_ptr so that all subsequent vring allocations and
873a2b950acSOhad Ben-Cohen 	 * virtio fields manipulation update the actual loaded resource table
874a2b950acSOhad Ben-Cohen 	 * in device memory.
875a2b950acSOhad Ben-Cohen 	 */
876a2b950acSOhad Ben-Cohen 	rproc->table_ptr = loaded_table;
877a2b950acSOhad Ben-Cohen 
878400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_RUNNING;
879400e64dfSOhad Ben-Cohen 
880400e64dfSOhad Ben-Cohen 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
881400e64dfSOhad Ben-Cohen 
882400e64dfSOhad Ben-Cohen 	return 0;
883400e64dfSOhad Ben-Cohen 
884400e64dfSOhad Ben-Cohen clean_up:
885400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
886400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
887400e64dfSOhad Ben-Cohen 	return ret;
888400e64dfSOhad Ben-Cohen }
889400e64dfSOhad Ben-Cohen 
890400e64dfSOhad Ben-Cohen /*
891400e64dfSOhad Ben-Cohen  * take a firmware and look for virtio devices to register.
892400e64dfSOhad Ben-Cohen  *
893400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
894400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
895400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
896400e64dfSOhad Ben-Cohen  * that might be cleaner).
897400e64dfSOhad Ben-Cohen  */
898400e64dfSOhad Ben-Cohen static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
899400e64dfSOhad Ben-Cohen {
900400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
9011e3e2c7cSOhad Ben-Cohen 	struct resource_table *table;
9021e3e2c7cSOhad Ben-Cohen 	int ret, tablesz;
903400e64dfSOhad Ben-Cohen 
904400e64dfSOhad Ben-Cohen 	if (rproc_fw_sanity_check(rproc, fw) < 0)
905400e64dfSOhad Ben-Cohen 		goto out;
906400e64dfSOhad Ben-Cohen 
9071e3e2c7cSOhad Ben-Cohen 	/* look for the resource table */
908bd484984SSjur Brændeland 	table = rproc_find_rsc_table(rproc, fw,  &tablesz);
9091e3e2c7cSOhad Ben-Cohen 	if (!table)
910400e64dfSOhad Ben-Cohen 		goto out;
9111e3e2c7cSOhad Ben-Cohen 
912a2b950acSOhad Ben-Cohen 	rproc->table_csum = crc32(0, table, tablesz);
913a2b950acSOhad Ben-Cohen 
914a2b950acSOhad Ben-Cohen 	/*
915a2b950acSOhad Ben-Cohen 	 * Create a copy of the resource table. When a virtio device starts
916a2b950acSOhad Ben-Cohen 	 * and calls vring_new_virtqueue() the address of the allocated vring
917a2b950acSOhad Ben-Cohen 	 * will be stored in the cached_table. Before the device is started,
918a2b950acSOhad Ben-Cohen 	 * cached_table will be copied into devic memory.
919a2b950acSOhad Ben-Cohen 	 */
92095cee62cSThomas Meyer 	rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL);
921a2b950acSOhad Ben-Cohen 	if (!rproc->cached_table)
922a2b950acSOhad Ben-Cohen 		goto out;
923a2b950acSOhad Ben-Cohen 
924a2b950acSOhad Ben-Cohen 	rproc->table_ptr = rproc->cached_table;
925a2b950acSOhad Ben-Cohen 
926ba7290e0SSjur Brændeland 	/* count the number of notify-ids */
927ba7290e0SSjur Brændeland 	rproc->max_notifyid = -1;
928172e6ab1SSuman Anna 	ret = rproc_handle_resources(rproc, tablesz,
929172e6ab1SSuman Anna 				     rproc_count_vrings_handler);
9301e3e2c7cSOhad Ben-Cohen 	if (ret)
9311e3e2c7cSOhad Ben-Cohen 		goto out;
932400e64dfSOhad Ben-Cohen 
933a2b950acSOhad Ben-Cohen 	/* look for virtio devices and register them */
934a2b950acSOhad Ben-Cohen 	ret = rproc_handle_resources(rproc, tablesz, rproc_vdev_handler);
935a2b950acSOhad Ben-Cohen 
936400e64dfSOhad Ben-Cohen out:
937400e64dfSOhad Ben-Cohen 	release_firmware(fw);
938160e7c84SOhad Ben-Cohen 	/* allow rproc_del() contexts, if any, to proceed */
939400e64dfSOhad Ben-Cohen 	complete_all(&rproc->firmware_loading_complete);
940400e64dfSOhad Ben-Cohen }
941400e64dfSOhad Ben-Cohen 
94270b85ef8SFernando Guzman Lugo static int rproc_add_virtio_devices(struct rproc *rproc)
94370b85ef8SFernando Guzman Lugo {
94470b85ef8SFernando Guzman Lugo 	int ret;
94570b85ef8SFernando Guzman Lugo 
94670b85ef8SFernando Guzman Lugo 	/* rproc_del() calls must wait until async loader completes */
94770b85ef8SFernando Guzman Lugo 	init_completion(&rproc->firmware_loading_complete);
94870b85ef8SFernando Guzman Lugo 
94970b85ef8SFernando Guzman Lugo 	/*
95070b85ef8SFernando Guzman Lugo 	 * We must retrieve early virtio configuration info from
95170b85ef8SFernando Guzman Lugo 	 * the firmware (e.g. whether to register a virtio device,
95270b85ef8SFernando Guzman Lugo 	 * what virtio features does it support, ...).
95370b85ef8SFernando Guzman Lugo 	 *
95470b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
95570b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
95670b85ef8SFernando Guzman Lugo 	 */
95770b85ef8SFernando Guzman Lugo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
95870b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
95970b85ef8SFernando Guzman Lugo 				      rproc, rproc_fw_config_virtio);
96070b85ef8SFernando Guzman Lugo 	if (ret < 0) {
96170b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
96270b85ef8SFernando Guzman Lugo 		complete_all(&rproc->firmware_loading_complete);
96370b85ef8SFernando Guzman Lugo 	}
96470b85ef8SFernando Guzman Lugo 
96570b85ef8SFernando Guzman Lugo 	return ret;
96670b85ef8SFernando Guzman Lugo }
96770b85ef8SFernando Guzman Lugo 
96870b85ef8SFernando Guzman Lugo /**
96970b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
97070b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
97170b85ef8SFernando Guzman Lugo  *
97270b85ef8SFernando Guzman Lugo  * The recovery is done by reseting all the virtio devices, that way all the
97370b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
97470b85ef8SFernando Guzman Lugo  * remoteproc functional again.
97570b85ef8SFernando Guzman Lugo  *
97670b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
97770b85ef8SFernando Guzman Lugo  */
97870b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
97970b85ef8SFernando Guzman Lugo {
98070b85ef8SFernando Guzman Lugo 	struct rproc_vdev *rvdev, *rvtmp;
98170b85ef8SFernando Guzman Lugo 
98270b85ef8SFernando Guzman Lugo 	dev_err(&rproc->dev, "recovering %s\n", rproc->name);
98370b85ef8SFernando Guzman Lugo 
98470b85ef8SFernando Guzman Lugo 	init_completion(&rproc->crash_comp);
98570b85ef8SFernando Guzman Lugo 
98670b85ef8SFernando Guzman Lugo 	/* clean up remote vdev entries */
98770b85ef8SFernando Guzman Lugo 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
98870b85ef8SFernando Guzman Lugo 		rproc_remove_virtio_dev(rvdev);
98970b85ef8SFernando Guzman Lugo 
99070b85ef8SFernando Guzman Lugo 	/* wait until there is no more rproc users */
99170b85ef8SFernando Guzman Lugo 	wait_for_completion(&rproc->crash_comp);
99270b85ef8SFernando Guzman Lugo 
993a2b950acSOhad Ben-Cohen 	/* Free the copy of the resource table */
994a2b950acSOhad Ben-Cohen 	kfree(rproc->cached_table);
995a2b950acSOhad Ben-Cohen 
99670b85ef8SFernando Guzman Lugo 	return rproc_add_virtio_devices(rproc);
99770b85ef8SFernando Guzman Lugo }
99870b85ef8SFernando Guzman Lugo 
999400e64dfSOhad Ben-Cohen /**
10008afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
10018afd519cSFernando Guzman Lugo  *
10028afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
10038afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
10048afd519cSFernando Guzman Lugo  */
10058afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
10068afd519cSFernando Guzman Lugo {
10078afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
10088afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
10098afd519cSFernando Guzman Lugo 
10108afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
10118afd519cSFernando Guzman Lugo 
10128afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
10138afd519cSFernando Guzman Lugo 
10148afd519cSFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
10158afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
10168afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
10178afd519cSFernando Guzman Lugo 		return;
10188afd519cSFernando Guzman Lugo 	}
10198afd519cSFernando Guzman Lugo 
10208afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
10218afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
10228afd519cSFernando Guzman Lugo 		rproc->name);
10238afd519cSFernando Guzman Lugo 
10248afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
10258afd519cSFernando Guzman Lugo 
10262e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
102770b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
10288afd519cSFernando Guzman Lugo }
10298afd519cSFernando Guzman Lugo 
10308afd519cSFernando Guzman Lugo /**
1031400e64dfSOhad Ben-Cohen  * rproc_boot() - boot a remote processor
1032400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1033400e64dfSOhad Ben-Cohen  *
1034400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1035400e64dfSOhad Ben-Cohen  *
1036400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1037400e64dfSOhad Ben-Cohen  * returns (successfully).
1038400e64dfSOhad Ben-Cohen  *
1039400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1040400e64dfSOhad Ben-Cohen  */
1041400e64dfSOhad Ben-Cohen int rproc_boot(struct rproc *rproc)
1042400e64dfSOhad Ben-Cohen {
1043400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1044400e64dfSOhad Ben-Cohen 	struct device *dev;
1045400e64dfSOhad Ben-Cohen 	int ret;
1046400e64dfSOhad Ben-Cohen 
1047400e64dfSOhad Ben-Cohen 	if (!rproc) {
1048400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1049400e64dfSOhad Ben-Cohen 		return -EINVAL;
1050400e64dfSOhad Ben-Cohen 	}
1051400e64dfSOhad Ben-Cohen 
1052b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1053400e64dfSOhad Ben-Cohen 
1054400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1055400e64dfSOhad Ben-Cohen 	if (ret) {
1056400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1057400e64dfSOhad Ben-Cohen 		return ret;
1058400e64dfSOhad Ben-Cohen 	}
1059400e64dfSOhad Ben-Cohen 
1060400e64dfSOhad Ben-Cohen 	/* loading a firmware is required */
1061400e64dfSOhad Ben-Cohen 	if (!rproc->firmware) {
1062400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: no firmware to load\n", __func__);
1063400e64dfSOhad Ben-Cohen 		ret = -EINVAL;
1064400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1065400e64dfSOhad Ben-Cohen 	}
1066400e64dfSOhad Ben-Cohen 
1067400e64dfSOhad Ben-Cohen 	/* prevent underlying implementation from being removed */
1068b5ab5e24SOhad Ben-Cohen 	if (!try_module_get(dev->parent->driver->owner)) {
1069400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: can't get owner\n", __func__);
1070400e64dfSOhad Ben-Cohen 		ret = -EINVAL;
1071400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1072400e64dfSOhad Ben-Cohen 	}
1073400e64dfSOhad Ben-Cohen 
1074400e64dfSOhad Ben-Cohen 	/* skip the boot process if rproc is already powered up */
1075400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1076400e64dfSOhad Ben-Cohen 		ret = 0;
1077400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1078400e64dfSOhad Ben-Cohen 	}
1079400e64dfSOhad Ben-Cohen 
1080400e64dfSOhad Ben-Cohen 	dev_info(dev, "powering up %s\n", rproc->name);
1081400e64dfSOhad Ben-Cohen 
1082400e64dfSOhad Ben-Cohen 	/* load firmware */
1083400e64dfSOhad Ben-Cohen 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1084400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1085400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware failed: %d\n", ret);
1086400e64dfSOhad Ben-Cohen 		goto downref_rproc;
1087400e64dfSOhad Ben-Cohen 	}
1088400e64dfSOhad Ben-Cohen 
1089400e64dfSOhad Ben-Cohen 	ret = rproc_fw_boot(rproc, firmware_p);
1090400e64dfSOhad Ben-Cohen 
1091400e64dfSOhad Ben-Cohen 	release_firmware(firmware_p);
1092400e64dfSOhad Ben-Cohen 
1093400e64dfSOhad Ben-Cohen downref_rproc:
1094400e64dfSOhad Ben-Cohen 	if (ret) {
1095b5ab5e24SOhad Ben-Cohen 		module_put(dev->parent->driver->owner);
1096400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1097400e64dfSOhad Ben-Cohen 	}
1098400e64dfSOhad Ben-Cohen unlock_mutex:
1099400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1100400e64dfSOhad Ben-Cohen 	return ret;
1101400e64dfSOhad Ben-Cohen }
1102400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1103400e64dfSOhad Ben-Cohen 
1104400e64dfSOhad Ben-Cohen /**
1105400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1106400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1107400e64dfSOhad Ben-Cohen  *
1108400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1109400e64dfSOhad Ben-Cohen  *
1110400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1111400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1112400e64dfSOhad Ben-Cohen  * without really powering off the device.
1113400e64dfSOhad Ben-Cohen  *
1114400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1115400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1116400e64dfSOhad Ben-Cohen  *
1117400e64dfSOhad Ben-Cohen  * Notes:
1118400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1119400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1120400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1121400e64dfSOhad Ben-Cohen  *   needed.
1122400e64dfSOhad Ben-Cohen  */
1123400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1124400e64dfSOhad Ben-Cohen {
1125b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1126400e64dfSOhad Ben-Cohen 	int ret;
1127400e64dfSOhad Ben-Cohen 
1128400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1129400e64dfSOhad Ben-Cohen 	if (ret) {
1130400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1131400e64dfSOhad Ben-Cohen 		return;
1132400e64dfSOhad Ben-Cohen 	}
1133400e64dfSOhad Ben-Cohen 
1134400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1135400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1136400e64dfSOhad Ben-Cohen 		goto out;
1137400e64dfSOhad Ben-Cohen 
1138400e64dfSOhad Ben-Cohen 	/* power off the remote processor */
1139400e64dfSOhad Ben-Cohen 	ret = rproc->ops->stop(rproc);
1140400e64dfSOhad Ben-Cohen 	if (ret) {
1141400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1142400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't stop rproc: %d\n", ret);
1143400e64dfSOhad Ben-Cohen 		goto out;
1144400e64dfSOhad Ben-Cohen 	}
1145400e64dfSOhad Ben-Cohen 
1146400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1147400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1148400e64dfSOhad Ben-Cohen 
1149400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1150400e64dfSOhad Ben-Cohen 
1151a2b950acSOhad Ben-Cohen 	/* Give the next start a clean resource table */
1152a2b950acSOhad Ben-Cohen 	rproc->table_ptr = rproc->cached_table;
1153a2b950acSOhad Ben-Cohen 
115470b85ef8SFernando Guzman Lugo 	/* if in crash state, unlock crash handler */
115570b85ef8SFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED)
115670b85ef8SFernando Guzman Lugo 		complete_all(&rproc->crash_comp);
115770b85ef8SFernando Guzman Lugo 
1158400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1159400e64dfSOhad Ben-Cohen 
1160400e64dfSOhad Ben-Cohen 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
1161400e64dfSOhad Ben-Cohen 
1162400e64dfSOhad Ben-Cohen out:
1163400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1164400e64dfSOhad Ben-Cohen 	if (!ret)
1165b5ab5e24SOhad Ben-Cohen 		module_put(dev->parent->driver->owner);
1166400e64dfSOhad Ben-Cohen }
1167400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1168400e64dfSOhad Ben-Cohen 
1169400e64dfSOhad Ben-Cohen /**
1170fec47d86SDave Gerlach  * rproc_get_by_phandle() - find a remote processor by phandle
1171fec47d86SDave Gerlach  * @phandle: phandle to the rproc
1172fec47d86SDave Gerlach  *
1173fec47d86SDave Gerlach  * Finds an rproc handle using the remote processor's phandle, and then
1174fec47d86SDave Gerlach  * return a handle to the rproc.
1175fec47d86SDave Gerlach  *
1176fec47d86SDave Gerlach  * This function increments the remote processor's refcount, so always
1177fec47d86SDave Gerlach  * use rproc_put() to decrement it back once rproc isn't needed anymore.
1178fec47d86SDave Gerlach  *
1179fec47d86SDave Gerlach  * Returns the rproc handle on success, and NULL on failure.
1180fec47d86SDave Gerlach  */
1181*8de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF
1182fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle)
1183fec47d86SDave Gerlach {
1184fec47d86SDave Gerlach 	struct rproc *rproc = NULL, *r;
1185fec47d86SDave Gerlach 	struct device_node *np;
1186fec47d86SDave Gerlach 
1187fec47d86SDave Gerlach 	np = of_find_node_by_phandle(phandle);
1188fec47d86SDave Gerlach 	if (!np)
1189fec47d86SDave Gerlach 		return NULL;
1190fec47d86SDave Gerlach 
1191fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1192fec47d86SDave Gerlach 	list_for_each_entry(r, &rproc_list, node) {
1193fec47d86SDave Gerlach 		if (r->dev.parent && r->dev.parent->of_node == np) {
1194fec47d86SDave Gerlach 			rproc = r;
1195fec47d86SDave Gerlach 			get_device(&rproc->dev);
1196fec47d86SDave Gerlach 			break;
1197fec47d86SDave Gerlach 		}
1198fec47d86SDave Gerlach 	}
1199fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1200fec47d86SDave Gerlach 
1201fec47d86SDave Gerlach 	of_node_put(np);
1202fec47d86SDave Gerlach 
1203fec47d86SDave Gerlach 	return rproc;
1204fec47d86SDave Gerlach }
1205*8de3dbd0SOhad Ben-Cohen #else
1206*8de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle)
1207*8de3dbd0SOhad Ben-Cohen {
1208*8de3dbd0SOhad Ben-Cohen 	return NULL;
1209*8de3dbd0SOhad Ben-Cohen }
1210*8de3dbd0SOhad Ben-Cohen #endif
1211fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle);
1212fec47d86SDave Gerlach 
1213fec47d86SDave Gerlach /**
1214160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
1215400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
1216400e64dfSOhad Ben-Cohen  *
1217400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
1218400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
1219400e64dfSOhad Ben-Cohen  *
1220400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
1221400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
1222400e64dfSOhad Ben-Cohen  *
1223400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
1224400e64dfSOhad Ben-Cohen  *
1225400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
1226400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
1227400e64dfSOhad Ben-Cohen  * firmware.
1228400e64dfSOhad Ben-Cohen  *
1229400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
12307a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
1231400e64dfSOhad Ben-Cohen  * probed.
1232400e64dfSOhad Ben-Cohen  */
1233160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
1234400e64dfSOhad Ben-Cohen {
1235b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
123670b85ef8SFernando Guzman Lugo 	int ret;
1237400e64dfSOhad Ben-Cohen 
1238b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
1239b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
1240b5ab5e24SOhad Ben-Cohen 		return ret;
1241400e64dfSOhad Ben-Cohen 
1242fec47d86SDave Gerlach 	/* expose to rproc_get_by_phandle users */
1243fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1244fec47d86SDave Gerlach 	list_add(&rproc->node, &rproc_list);
1245fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1246fec47d86SDave Gerlach 
1247b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
1248400e64dfSOhad Ben-Cohen 
1249489d129aSOhad Ben-Cohen 	dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1250489d129aSOhad Ben-Cohen 	dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1251489d129aSOhad Ben-Cohen 
1252400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
1253400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
1254400e64dfSOhad Ben-Cohen 
125570b85ef8SFernando Guzman Lugo 	return rproc_add_virtio_devices(rproc);
1256400e64dfSOhad Ben-Cohen }
1257160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
1258400e64dfSOhad Ben-Cohen 
1259400e64dfSOhad Ben-Cohen /**
1260b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
1261b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
1262b5ab5e24SOhad Ben-Cohen  *
1263b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
1264b5ab5e24SOhad Ben-Cohen  *
1265b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
1266b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
1267b5ab5e24SOhad Ben-Cohen  */
1268b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
1269b5ab5e24SOhad Ben-Cohen {
1270b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
1271b5ab5e24SOhad Ben-Cohen 
12727183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
12737183a2a7SOhad Ben-Cohen 
12747183a2a7SOhad Ben-Cohen 	rproc_delete_debug_dir(rproc);
12757183a2a7SOhad Ben-Cohen 
1276b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
1277b5ab5e24SOhad Ben-Cohen 
1278b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
1279b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
1280b5ab5e24SOhad Ben-Cohen 
1281b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
1282b5ab5e24SOhad Ben-Cohen }
1283b5ab5e24SOhad Ben-Cohen 
1284b5ab5e24SOhad Ben-Cohen static struct device_type rproc_type = {
1285b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
1286b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
1287b5ab5e24SOhad Ben-Cohen };
1288400e64dfSOhad Ben-Cohen 
1289400e64dfSOhad Ben-Cohen /**
1290400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
1291400e64dfSOhad Ben-Cohen  * @dev: the underlying device
1292400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
1293400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
12948b4aec9aSRobert Tivy  * @firmware: name of firmware file to load, can be NULL
1295400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
1296400e64dfSOhad Ben-Cohen  *
1297400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
12988b4aec9aSRobert Tivy  * it yet. if @firmware is NULL, a default name is used.
1299400e64dfSOhad Ben-Cohen  *
1300400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
1301400e64dfSOhad Ben-Cohen  * of the remote processor.
1302400e64dfSOhad Ben-Cohen  *
1303400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
1304160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
1305400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
1306400e64dfSOhad Ben-Cohen  *
1307400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
1308400e64dfSOhad Ben-Cohen  *
1309400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
1310160e7c84SOhad Ben-Cohen  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
1311400e64dfSOhad Ben-Cohen  */
1312400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
1313400e64dfSOhad Ben-Cohen 				const struct rproc_ops *ops,
1314400e64dfSOhad Ben-Cohen 				const char *firmware, int len)
1315400e64dfSOhad Ben-Cohen {
1316400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
13178b4aec9aSRobert Tivy 	char *p, *template = "rproc-%s-fw";
13188b4aec9aSRobert Tivy 	int name_len = 0;
1319400e64dfSOhad Ben-Cohen 
1320400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
1321400e64dfSOhad Ben-Cohen 		return NULL;
1322400e64dfSOhad Ben-Cohen 
13238b4aec9aSRobert Tivy 	if (!firmware)
13248b4aec9aSRobert Tivy 		/*
13258b4aec9aSRobert Tivy 		 * Make room for default firmware name (minus %s plus '\0').
13268b4aec9aSRobert Tivy 		 * If the caller didn't pass in a firmware name then
13278b4aec9aSRobert Tivy 		 * construct a default name.  We're already glomming 'len'
13288b4aec9aSRobert Tivy 		 * bytes onto the end of the struct rproc allocation, so do
13298b4aec9aSRobert Tivy 		 * a few more for the default firmware name (but only if
13308b4aec9aSRobert Tivy 		 * the caller doesn't pass one).
13318b4aec9aSRobert Tivy 		 */
13328b4aec9aSRobert Tivy 		name_len = strlen(name) + strlen(template) - 2 + 1;
13338b4aec9aSRobert Tivy 
13348b4aec9aSRobert Tivy 	rproc = kzalloc(sizeof(struct rproc) + len + name_len, GFP_KERNEL);
1335172e6ab1SSuman Anna 	if (!rproc)
1336400e64dfSOhad Ben-Cohen 		return NULL;
1337400e64dfSOhad Ben-Cohen 
13388b4aec9aSRobert Tivy 	if (!firmware) {
13398b4aec9aSRobert Tivy 		p = (char *)rproc + sizeof(struct rproc) + len;
13408b4aec9aSRobert Tivy 		snprintf(p, name_len, template, name);
13418b4aec9aSRobert Tivy 	} else {
13428b4aec9aSRobert Tivy 		p = (char *)firmware;
13438b4aec9aSRobert Tivy 	}
13448b4aec9aSRobert Tivy 
13458b4aec9aSRobert Tivy 	rproc->firmware = p;
1346400e64dfSOhad Ben-Cohen 	rproc->name = name;
1347400e64dfSOhad Ben-Cohen 	rproc->ops = ops;
1348400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
1349400e64dfSOhad Ben-Cohen 
1350b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
1351b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
1352b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
1353b5ab5e24SOhad Ben-Cohen 
1354b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
1355b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1356b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
1357b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1358b5ab5e24SOhad Ben-Cohen 		put_device(&rproc->dev);
1359b5ab5e24SOhad Ben-Cohen 		return NULL;
1360b5ab5e24SOhad Ben-Cohen 	}
1361b5ab5e24SOhad Ben-Cohen 
1362b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1363b5ab5e24SOhad Ben-Cohen 
1364400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
1365400e64dfSOhad Ben-Cohen 
13664afc89d6SSjur Brændeland 	/* Set ELF as the default fw_ops handler */
13674afc89d6SSjur Brændeland 	rproc->fw_ops = &rproc_elf_fw_ops;
1368400e64dfSOhad Ben-Cohen 
1369400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
1370400e64dfSOhad Ben-Cohen 
13717a186941SOhad Ben-Cohen 	idr_init(&rproc->notifyids);
13727a186941SOhad Ben-Cohen 
1373400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
1374400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
1375400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
13767a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
1377400e64dfSOhad Ben-Cohen 
13788afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
137970b85ef8SFernando Guzman Lugo 	init_completion(&rproc->crash_comp);
13808afd519cSFernando Guzman Lugo 
1381400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1382400e64dfSOhad Ben-Cohen 
1383400e64dfSOhad Ben-Cohen 	return rproc;
1384400e64dfSOhad Ben-Cohen }
1385400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
1386400e64dfSOhad Ben-Cohen 
1387400e64dfSOhad Ben-Cohen /**
1388160e7c84SOhad Ben-Cohen  * rproc_put() - unroll rproc_alloc()
1389400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
1390400e64dfSOhad Ben-Cohen  *
1391c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
1392400e64dfSOhad Ben-Cohen  *
1393c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
1394c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
1395400e64dfSOhad Ben-Cohen  */
1396160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
1397400e64dfSOhad Ben-Cohen {
1398b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
1399400e64dfSOhad Ben-Cohen }
1400160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
1401400e64dfSOhad Ben-Cohen 
1402400e64dfSOhad Ben-Cohen /**
1403160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
1404400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
1405400e64dfSOhad Ben-Cohen  *
1406400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
1407400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
1408160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
1409400e64dfSOhad Ben-Cohen  * has completed successfully.
1410400e64dfSOhad Ben-Cohen  *
1411160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
1412c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
1413160e7c84SOhad Ben-Cohen  * one last refcount, one still needs to call rproc_put().
1414400e64dfSOhad Ben-Cohen  *
1415400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
1416400e64dfSOhad Ben-Cohen  */
1417160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
1418400e64dfSOhad Ben-Cohen {
14196db20ea8SOhad Ben-Cohen 	struct rproc_vdev *rvdev, *tmp;
14207a186941SOhad Ben-Cohen 
1421400e64dfSOhad Ben-Cohen 	if (!rproc)
1422400e64dfSOhad Ben-Cohen 		return -EINVAL;
1423400e64dfSOhad Ben-Cohen 
1424400e64dfSOhad Ben-Cohen 	/* if rproc is just being registered, wait */
1425400e64dfSOhad Ben-Cohen 	wait_for_completion(&rproc->firmware_loading_complete);
1426400e64dfSOhad Ben-Cohen 
14277a186941SOhad Ben-Cohen 	/* clean up remote vdev entries */
14286db20ea8SOhad Ben-Cohen 	list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
14297a186941SOhad Ben-Cohen 		rproc_remove_virtio_dev(rvdev);
1430400e64dfSOhad Ben-Cohen 
1431a2b950acSOhad Ben-Cohen 	/* Free the copy of the resource table */
1432a2b950acSOhad Ben-Cohen 	kfree(rproc->cached_table);
1433a2b950acSOhad Ben-Cohen 
1434fec47d86SDave Gerlach 	/* the rproc is downref'ed as soon as it's removed from the klist */
1435fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1436fec47d86SDave Gerlach 	list_del(&rproc->node);
1437fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1438fec47d86SDave Gerlach 
1439b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
1440400e64dfSOhad Ben-Cohen 
1441400e64dfSOhad Ben-Cohen 	return 0;
1442400e64dfSOhad Ben-Cohen }
1443160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
1444400e64dfSOhad Ben-Cohen 
14458afd519cSFernando Guzman Lugo /**
14468afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
14478afd519cSFernando Guzman Lugo  * @rproc: remote processor
14488afd519cSFernando Guzman Lugo  * @type: crash type
14498afd519cSFernando Guzman Lugo  *
14508afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
14518afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
14528afd519cSFernando Guzman Lugo  * non-remoteproc driver.
14538afd519cSFernando Guzman Lugo  *
14548afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
14558afd519cSFernando Guzman Lugo  */
14568afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
14578afd519cSFernando Guzman Lugo {
14588afd519cSFernando Guzman Lugo 	if (!rproc) {
14598afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
14608afd519cSFernando Guzman Lugo 		return;
14618afd519cSFernando Guzman Lugo 	}
14628afd519cSFernando Guzman Lugo 
14638afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
14648afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
14658afd519cSFernando Guzman Lugo 
14668afd519cSFernando Guzman Lugo 	/* create a new task to handle the error */
14678afd519cSFernando Guzman Lugo 	schedule_work(&rproc->crash_handler);
14688afd519cSFernando Guzman Lugo }
14698afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
14708afd519cSFernando Guzman Lugo 
1471400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
1472400e64dfSOhad Ben-Cohen {
1473400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
1474b5ab5e24SOhad Ben-Cohen 
1475400e64dfSOhad Ben-Cohen 	return 0;
1476400e64dfSOhad Ben-Cohen }
1477400e64dfSOhad Ben-Cohen module_init(remoteproc_init);
1478400e64dfSOhad Ben-Cohen 
1479400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
1480400e64dfSOhad Ben-Cohen {
1481400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
1482400e64dfSOhad Ben-Cohen }
1483400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
1484400e64dfSOhad Ben-Cohen 
1485400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
1486400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
1487