xref: /linux/drivers/remoteproc/remoteproc_core.c (revision bd48498487735a01abc68edb76e3d86393e096e1)
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 
53400e64dfSOhad Ben-Cohen /*
54400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
55400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
56400e64dfSOhad Ben-Cohen  * an IOMMU).
57400e64dfSOhad Ben-Cohen  *
58400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
59400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
60400e64dfSOhad Ben-Cohen  *
61400e64dfSOhad Ben-Cohen  * Currently this is mostly a stub, but it will be later used to trigger
62400e64dfSOhad Ben-Cohen  * the recovery of the remote processor.
63400e64dfSOhad Ben-Cohen  */
64400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
6577ca2332SOhad Ben-Cohen 		unsigned long iova, int flags, void *token)
66400e64dfSOhad Ben-Cohen {
67400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
68400e64dfSOhad Ben-Cohen 
69400e64dfSOhad Ben-Cohen 	/*
70400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
71400e64dfSOhad Ben-Cohen 	 * we just plan to use this as a recovery trigger.
72400e64dfSOhad Ben-Cohen 	 */
73400e64dfSOhad Ben-Cohen 	return -ENOSYS;
74400e64dfSOhad Ben-Cohen }
75400e64dfSOhad Ben-Cohen 
76400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
77400e64dfSOhad Ben-Cohen {
78400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
79b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
80400e64dfSOhad Ben-Cohen 	int ret;
81400e64dfSOhad Ben-Cohen 
82400e64dfSOhad Ben-Cohen 	/*
83400e64dfSOhad Ben-Cohen 	 * We currently use iommu_present() to decide if an IOMMU
84400e64dfSOhad Ben-Cohen 	 * setup is needed.
85400e64dfSOhad Ben-Cohen 	 *
86400e64dfSOhad Ben-Cohen 	 * This works for simple cases, but will easily fail with
87400e64dfSOhad Ben-Cohen 	 * platforms that do have an IOMMU, but not for this specific
88400e64dfSOhad Ben-Cohen 	 * rproc.
89400e64dfSOhad Ben-Cohen 	 *
90400e64dfSOhad Ben-Cohen 	 * This will be easily solved by introducing hw capabilities
91400e64dfSOhad Ben-Cohen 	 * that will be set by the remoteproc driver.
92400e64dfSOhad Ben-Cohen 	 */
93400e64dfSOhad Ben-Cohen 	if (!iommu_present(dev->bus)) {
940798e1daSMark Grosen 		dev_dbg(dev, "iommu not found\n");
950798e1daSMark Grosen 		return 0;
96400e64dfSOhad Ben-Cohen 	}
97400e64dfSOhad Ben-Cohen 
98400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
99400e64dfSOhad Ben-Cohen 	if (!domain) {
100400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
101400e64dfSOhad Ben-Cohen 		return -ENOMEM;
102400e64dfSOhad Ben-Cohen 	}
103400e64dfSOhad Ben-Cohen 
10477ca2332SOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
105400e64dfSOhad Ben-Cohen 
106400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
107400e64dfSOhad Ben-Cohen 	if (ret) {
108400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
109400e64dfSOhad Ben-Cohen 		goto free_domain;
110400e64dfSOhad Ben-Cohen 	}
111400e64dfSOhad Ben-Cohen 
112400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
113400e64dfSOhad Ben-Cohen 
114400e64dfSOhad Ben-Cohen 	return 0;
115400e64dfSOhad Ben-Cohen 
116400e64dfSOhad Ben-Cohen free_domain:
117400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
118400e64dfSOhad Ben-Cohen 	return ret;
119400e64dfSOhad Ben-Cohen }
120400e64dfSOhad Ben-Cohen 
121400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
122400e64dfSOhad Ben-Cohen {
123400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
124b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
125400e64dfSOhad Ben-Cohen 
126400e64dfSOhad Ben-Cohen 	if (!domain)
127400e64dfSOhad Ben-Cohen 		return;
128400e64dfSOhad Ben-Cohen 
129400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
130400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
131400e64dfSOhad Ben-Cohen 
132400e64dfSOhad Ben-Cohen 	return;
133400e64dfSOhad Ben-Cohen }
134400e64dfSOhad Ben-Cohen 
135400e64dfSOhad Ben-Cohen /*
136400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
137400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
138400e64dfSOhad Ben-Cohen  * device addresses (which are hardcoded in the firmware).
139400e64dfSOhad Ben-Cohen  *
140400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
141400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
142400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
143400e64dfSOhad Ben-Cohen  *
144400e64dfSOhad Ben-Cohen  * This function is an internal helper with which we can go over the allocated
145400e64dfSOhad Ben-Cohen  * carveouts and translate specific device address to kernel virtual addresses
146400e64dfSOhad Ben-Cohen  * so we can access the referenced memory.
147400e64dfSOhad Ben-Cohen  *
148400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
149400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
150400e64dfSOhad Ben-Cohen  * here the output of the DMA API, which should be more correct.
151400e64dfSOhad Ben-Cohen  */
152400e64dfSOhad Ben-Cohen static void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
153400e64dfSOhad Ben-Cohen {
154400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
155400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
156400e64dfSOhad Ben-Cohen 
157400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
158400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
159400e64dfSOhad Ben-Cohen 
160400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
161400e64dfSOhad Ben-Cohen 		if (offset < 0)
162400e64dfSOhad Ben-Cohen 			continue;
163400e64dfSOhad Ben-Cohen 
164400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
165400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
166400e64dfSOhad Ben-Cohen 			continue;
167400e64dfSOhad Ben-Cohen 
168400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
169400e64dfSOhad Ben-Cohen 
170400e64dfSOhad Ben-Cohen 		break;
171400e64dfSOhad Ben-Cohen 	}
172400e64dfSOhad Ben-Cohen 
173400e64dfSOhad Ben-Cohen 	return ptr;
174400e64dfSOhad Ben-Cohen }
175400e64dfSOhad Ben-Cohen 
176400e64dfSOhad Ben-Cohen /**
177400e64dfSOhad Ben-Cohen  * rproc_load_segments() - load firmware segments to memory
178400e64dfSOhad Ben-Cohen  * @rproc: remote processor which will be booted using these fw segments
179*bd484984SSjur Brændeland  * @fw: the ELF firmware image
180400e64dfSOhad Ben-Cohen  *
181400e64dfSOhad Ben-Cohen  * This function loads the firmware segments to memory, where the remote
182400e64dfSOhad Ben-Cohen  * processor expects them.
183400e64dfSOhad Ben-Cohen  *
184400e64dfSOhad Ben-Cohen  * Some remote processors will expect their code and data to be placed
185400e64dfSOhad Ben-Cohen  * in specific device addresses, and can't have them dynamically assigned.
186400e64dfSOhad Ben-Cohen  *
187400e64dfSOhad Ben-Cohen  * We currently support only those kind of remote processors, and expect
188400e64dfSOhad Ben-Cohen  * the program header's paddr member to contain those addresses. We then go
189400e64dfSOhad Ben-Cohen  * through the physically contiguous "carveout" memory regions which we
190400e64dfSOhad Ben-Cohen  * allocated (and mapped) earlier on behalf of the remote processor,
191400e64dfSOhad Ben-Cohen  * and "translate" device address to kernel addresses, so we can copy the
192400e64dfSOhad Ben-Cohen  * segments where they are expected.
193400e64dfSOhad Ben-Cohen  *
194400e64dfSOhad Ben-Cohen  * Currently we only support remote processors that required carveout
195400e64dfSOhad Ben-Cohen  * allocations and got them mapped onto their iommus. Some processors
196400e64dfSOhad Ben-Cohen  * might be different: they might not have iommus, and would prefer to
197400e64dfSOhad Ben-Cohen  * directly allocate memory for every segment/resource. This is not yet
198400e64dfSOhad Ben-Cohen  * supported, though.
199400e64dfSOhad Ben-Cohen  */
2009bc91231SOhad Ben-Cohen static int
201*bd484984SSjur Brændeland rproc_load_segments(struct rproc *rproc, const struct firmware *fw)
202400e64dfSOhad Ben-Cohen {
203b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
204400e64dfSOhad Ben-Cohen 	struct elf32_hdr *ehdr;
205400e64dfSOhad Ben-Cohen 	struct elf32_phdr *phdr;
206400e64dfSOhad Ben-Cohen 	int i, ret = 0;
207*bd484984SSjur Brændeland 	const u8 *elf_data = fw->data;
208400e64dfSOhad Ben-Cohen 
209400e64dfSOhad Ben-Cohen 	ehdr = (struct elf32_hdr *)elf_data;
210400e64dfSOhad Ben-Cohen 	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
211400e64dfSOhad Ben-Cohen 
212400e64dfSOhad Ben-Cohen 	/* go through the available ELF segments */
213400e64dfSOhad Ben-Cohen 	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
214400e64dfSOhad Ben-Cohen 		u32 da = phdr->p_paddr;
215400e64dfSOhad Ben-Cohen 		u32 memsz = phdr->p_memsz;
216400e64dfSOhad Ben-Cohen 		u32 filesz = phdr->p_filesz;
2179bc91231SOhad Ben-Cohen 		u32 offset = phdr->p_offset;
218400e64dfSOhad Ben-Cohen 		void *ptr;
219400e64dfSOhad Ben-Cohen 
220400e64dfSOhad Ben-Cohen 		if (phdr->p_type != PT_LOAD)
221400e64dfSOhad Ben-Cohen 			continue;
222400e64dfSOhad Ben-Cohen 
223400e64dfSOhad Ben-Cohen 		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
224400e64dfSOhad Ben-Cohen 					phdr->p_type, da, memsz, filesz);
225400e64dfSOhad Ben-Cohen 
226400e64dfSOhad Ben-Cohen 		if (filesz > memsz) {
227400e64dfSOhad Ben-Cohen 			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
228400e64dfSOhad Ben-Cohen 							filesz, memsz);
229400e64dfSOhad Ben-Cohen 			ret = -EINVAL;
230400e64dfSOhad Ben-Cohen 			break;
231400e64dfSOhad Ben-Cohen 		}
232400e64dfSOhad Ben-Cohen 
233*bd484984SSjur Brændeland 		if (offset + filesz > fw->size) {
2349bc91231SOhad Ben-Cohen 			dev_err(dev, "truncated fw: need 0x%x avail 0x%x\n",
235*bd484984SSjur Brændeland 					offset + filesz, fw->size);
2369bc91231SOhad Ben-Cohen 			ret = -EINVAL;
2379bc91231SOhad Ben-Cohen 			break;
2389bc91231SOhad Ben-Cohen 		}
2399bc91231SOhad Ben-Cohen 
240400e64dfSOhad Ben-Cohen 		/* grab the kernel address for this device address */
241400e64dfSOhad Ben-Cohen 		ptr = rproc_da_to_va(rproc, da, memsz);
242400e64dfSOhad Ben-Cohen 		if (!ptr) {
243400e64dfSOhad Ben-Cohen 			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
244400e64dfSOhad Ben-Cohen 			ret = -EINVAL;
245400e64dfSOhad Ben-Cohen 			break;
246400e64dfSOhad Ben-Cohen 		}
247400e64dfSOhad Ben-Cohen 
248400e64dfSOhad Ben-Cohen 		/* put the segment where the remote processor expects it */
249400e64dfSOhad Ben-Cohen 		if (phdr->p_filesz)
250400e64dfSOhad Ben-Cohen 			memcpy(ptr, elf_data + phdr->p_offset, filesz);
251400e64dfSOhad Ben-Cohen 
252400e64dfSOhad Ben-Cohen 		/*
253400e64dfSOhad Ben-Cohen 		 * Zero out remaining memory for this segment.
254400e64dfSOhad Ben-Cohen 		 *
255400e64dfSOhad Ben-Cohen 		 * This isn't strictly required since dma_alloc_coherent already
256400e64dfSOhad Ben-Cohen 		 * did this for us. albeit harmless, we may consider removing
257400e64dfSOhad Ben-Cohen 		 * this.
258400e64dfSOhad Ben-Cohen 		 */
259400e64dfSOhad Ben-Cohen 		if (memsz > filesz)
260400e64dfSOhad Ben-Cohen 			memset(ptr + filesz, 0, memsz - filesz);
261400e64dfSOhad Ben-Cohen 	}
262400e64dfSOhad Ben-Cohen 
263400e64dfSOhad Ben-Cohen 	return ret;
264400e64dfSOhad Ben-Cohen }
265400e64dfSOhad Ben-Cohen 
2666db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
2676db20ea8SOhad Ben-Cohen {
2686db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
269b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
2706db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
2716db20ea8SOhad Ben-Cohen 	dma_addr_t dma;
2726db20ea8SOhad Ben-Cohen 	void *va;
2736db20ea8SOhad Ben-Cohen 	int ret, size, notifyid;
2746db20ea8SOhad Ben-Cohen 
2756db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
2766db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2776db20ea8SOhad Ben-Cohen 
2786db20ea8SOhad Ben-Cohen 	if (!idr_pre_get(&rproc->notifyids, GFP_KERNEL)) {
2796db20ea8SOhad Ben-Cohen 		dev_err(dev, "idr_pre_get failed\n");
2806db20ea8SOhad Ben-Cohen 		return -ENOMEM;
2816db20ea8SOhad Ben-Cohen 	}
2826db20ea8SOhad Ben-Cohen 
2836db20ea8SOhad Ben-Cohen 	/*
2846db20ea8SOhad Ben-Cohen 	 * Allocate non-cacheable memory for the vring. In the future
2856db20ea8SOhad Ben-Cohen 	 * this call will also configure the IOMMU for us
2866db20ea8SOhad Ben-Cohen 	 * TODO: let the rproc know the da of this vring
2876db20ea8SOhad Ben-Cohen 	 */
288b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
2896db20ea8SOhad Ben-Cohen 	if (!va) {
290b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent failed\n");
2916db20ea8SOhad Ben-Cohen 		return -EINVAL;
2926db20ea8SOhad Ben-Cohen 	}
2936db20ea8SOhad Ben-Cohen 
2946db20ea8SOhad Ben-Cohen 	/*
2956db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
2966db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
2976db20ea8SOhad Ben-Cohen 	 * TODO: let the rproc know the notifyid of this vring
2986db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
2996db20ea8SOhad Ben-Cohen 	 */
3006db20ea8SOhad Ben-Cohen 	ret = idr_get_new(&rproc->notifyids, rvring, &notifyid);
3016db20ea8SOhad Ben-Cohen 	if (ret) {
3026db20ea8SOhad Ben-Cohen 		dev_err(dev, "idr_get_new failed: %d\n", ret);
303b5ab5e24SOhad Ben-Cohen 		dma_free_coherent(dev->parent, size, va, dma);
3046db20ea8SOhad Ben-Cohen 		return ret;
3056db20ea8SOhad Ben-Cohen 	}
3066db20ea8SOhad Ben-Cohen 
3076db20ea8SOhad Ben-Cohen 	dev_dbg(dev, "vring%d: va %p dma %x size %x idr %d\n", i, va,
3086db20ea8SOhad Ben-Cohen 					dma, size, notifyid);
3096db20ea8SOhad Ben-Cohen 
3106db20ea8SOhad Ben-Cohen 	rvring->va = va;
3116db20ea8SOhad Ben-Cohen 	rvring->dma = dma;
3126db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
3136db20ea8SOhad Ben-Cohen 
3146db20ea8SOhad Ben-Cohen 	return 0;
3156db20ea8SOhad Ben-Cohen }
3166db20ea8SOhad Ben-Cohen 
3177a186941SOhad Ben-Cohen static int
3186db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
319400e64dfSOhad Ben-Cohen {
3207a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
321b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3227a186941SOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
3236db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
324400e64dfSOhad Ben-Cohen 
3257a186941SOhad Ben-Cohen 	dev_dbg(dev, "vdev rsc: vring%d: da %x, qsz %d, align %d\n",
3267a186941SOhad Ben-Cohen 				i, vring->da, vring->num, vring->align);
3277a186941SOhad Ben-Cohen 
3287a186941SOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
3297a186941SOhad Ben-Cohen 	if (vring->reserved) {
3307a186941SOhad Ben-Cohen 		dev_err(dev, "vring rsc has non zero reserved bytes\n");
331fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
332fd2c15ecSOhad Ben-Cohen 	}
333fd2c15ecSOhad Ben-Cohen 
33463140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
33563140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
33663140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
33763140e0eSOhad Ben-Cohen 						vring->num, vring->align);
338400e64dfSOhad Ben-Cohen 		return -EINVAL;
339400e64dfSOhad Ben-Cohen 	}
340400e64dfSOhad Ben-Cohen 
3416db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
3426db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
3436db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
344400e64dfSOhad Ben-Cohen 
345400e64dfSOhad Ben-Cohen 	return 0;
346400e64dfSOhad Ben-Cohen }
347400e64dfSOhad Ben-Cohen 
3486db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
3497a186941SOhad Ben-Cohen {
35063140e0eSOhad Ben-Cohen 	int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
3516db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
3527a186941SOhad Ben-Cohen 
353b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
3547a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
3557a186941SOhad Ben-Cohen }
3567a186941SOhad Ben-Cohen 
357400e64dfSOhad Ben-Cohen /**
358fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
359400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
360400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
361fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
362400e64dfSOhad Ben-Cohen  *
3637a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
3647a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
3657a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
3667a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
367400e64dfSOhad Ben-Cohen  *
3687a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
3697a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
3707a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
3717a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
3727a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
373400e64dfSOhad Ben-Cohen  *
3747a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
3757a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
3767a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
3777a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
3787a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
3797a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
3807a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
381400e64dfSOhad Ben-Cohen  *
382400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
383400e64dfSOhad Ben-Cohen  */
384fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
385fd2c15ecSOhad Ben-Cohen 								int avail)
386400e64dfSOhad Ben-Cohen {
387b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3887a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
3897a186941SOhad Ben-Cohen 	int i, ret;
390fd2c15ecSOhad Ben-Cohen 
391fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
392fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
393fd2c15ecSOhad Ben-Cohen 			+ rsc->config_len > avail) {
394b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
395fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
396fd2c15ecSOhad Ben-Cohen 	}
397fd2c15ecSOhad Ben-Cohen 
398fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
399fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
400fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
401fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
402fd2c15ecSOhad Ben-Cohen 	}
403fd2c15ecSOhad Ben-Cohen 
404fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
405fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
406400e64dfSOhad Ben-Cohen 
4077a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
4087a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
409fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
410400e64dfSOhad Ben-Cohen 		return -EINVAL;
411400e64dfSOhad Ben-Cohen 	}
412400e64dfSOhad Ben-Cohen 
4137a186941SOhad Ben-Cohen 	rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
4147a186941SOhad Ben-Cohen 	if (!rvdev)
4157a186941SOhad Ben-Cohen 		return -ENOMEM;
4167a186941SOhad Ben-Cohen 
4177a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
4187a186941SOhad Ben-Cohen 
4196db20ea8SOhad Ben-Cohen 	/* parse the vrings */
420fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
4216db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
4227a186941SOhad Ben-Cohen 		if (ret)
4236db20ea8SOhad Ben-Cohen 			goto free_rvdev;
424fd2c15ecSOhad Ben-Cohen 	}
425fd2c15ecSOhad Ben-Cohen 
4267a186941SOhad Ben-Cohen 	/* remember the device features */
4277a186941SOhad Ben-Cohen 	rvdev->dfeatures = rsc->dfeatures;
428400e64dfSOhad Ben-Cohen 
4297a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
430400e64dfSOhad Ben-Cohen 
4317a186941SOhad Ben-Cohen 	/* it is now safe to add the virtio device */
4327a186941SOhad Ben-Cohen 	ret = rproc_add_virtio_dev(rvdev, rsc->id);
4337a186941SOhad Ben-Cohen 	if (ret)
4346db20ea8SOhad Ben-Cohen 		goto free_rvdev;
435400e64dfSOhad Ben-Cohen 
436400e64dfSOhad Ben-Cohen 	return 0;
4377a186941SOhad Ben-Cohen 
4386db20ea8SOhad Ben-Cohen free_rvdev:
4397a186941SOhad Ben-Cohen 	kfree(rvdev);
4407a186941SOhad Ben-Cohen 	return ret;
441400e64dfSOhad Ben-Cohen }
442400e64dfSOhad Ben-Cohen 
443400e64dfSOhad Ben-Cohen /**
444400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
445400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
446400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
447fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
448400e64dfSOhad Ben-Cohen  *
449400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
450400e64dfSOhad Ben-Cohen  * export it via debugfs.
451400e64dfSOhad Ben-Cohen  *
452400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
453400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
454400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
455400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
456400e64dfSOhad Ben-Cohen  *
457400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
458400e64dfSOhad Ben-Cohen  */
459fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
460fd2c15ecSOhad Ben-Cohen 								int avail)
461400e64dfSOhad Ben-Cohen {
462400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *trace;
463b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
464400e64dfSOhad Ben-Cohen 	void *ptr;
465400e64dfSOhad Ben-Cohen 	char name[15];
466400e64dfSOhad Ben-Cohen 
467fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
468b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
469fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
470fd2c15ecSOhad Ben-Cohen 	}
471fd2c15ecSOhad Ben-Cohen 
472fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
473fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
474fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
475fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
476fd2c15ecSOhad Ben-Cohen 	}
477fd2c15ecSOhad Ben-Cohen 
478400e64dfSOhad Ben-Cohen 	/* what's the kernel address of this resource ? */
479400e64dfSOhad Ben-Cohen 	ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
480400e64dfSOhad Ben-Cohen 	if (!ptr) {
481400e64dfSOhad Ben-Cohen 		dev_err(dev, "erroneous trace resource entry\n");
482400e64dfSOhad Ben-Cohen 		return -EINVAL;
483400e64dfSOhad Ben-Cohen 	}
484400e64dfSOhad Ben-Cohen 
485400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
486400e64dfSOhad Ben-Cohen 	if (!trace) {
487400e64dfSOhad Ben-Cohen 		dev_err(dev, "kzalloc trace failed\n");
488400e64dfSOhad Ben-Cohen 		return -ENOMEM;
489400e64dfSOhad Ben-Cohen 	}
490400e64dfSOhad Ben-Cohen 
491400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
492400e64dfSOhad Ben-Cohen 	trace->len = rsc->len;
493400e64dfSOhad Ben-Cohen 	trace->va = ptr;
494400e64dfSOhad Ben-Cohen 
495400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
496400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
497400e64dfSOhad Ben-Cohen 
498400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
499400e64dfSOhad Ben-Cohen 	trace->priv = rproc_create_trace_file(name, rproc, trace);
500400e64dfSOhad Ben-Cohen 	if (!trace->priv) {
501400e64dfSOhad Ben-Cohen 		trace->va = NULL;
502400e64dfSOhad Ben-Cohen 		kfree(trace);
503400e64dfSOhad Ben-Cohen 		return -EINVAL;
504400e64dfSOhad Ben-Cohen 	}
505400e64dfSOhad Ben-Cohen 
506400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
507400e64dfSOhad Ben-Cohen 
508400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
509400e64dfSOhad Ben-Cohen 
510fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
511400e64dfSOhad Ben-Cohen 						rsc->da, rsc->len);
512400e64dfSOhad Ben-Cohen 
513400e64dfSOhad Ben-Cohen 	return 0;
514400e64dfSOhad Ben-Cohen }
515400e64dfSOhad Ben-Cohen 
516400e64dfSOhad Ben-Cohen /**
517400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
518400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
519400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
520fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
521400e64dfSOhad Ben-Cohen  *
522400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
523400e64dfSOhad Ben-Cohen  *
524400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
525400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
526400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
527400e64dfSOhad Ben-Cohen  *
528400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
529400e64dfSOhad Ben-Cohen  *
530400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
531400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
532400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
533400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
534400e64dfSOhad Ben-Cohen  *
535400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
536400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
537400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
538400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
539400e64dfSOhad Ben-Cohen  * are outside those ranges.
540400e64dfSOhad Ben-Cohen  */
541fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
542fd2c15ecSOhad Ben-Cohen 								int avail)
543400e64dfSOhad Ben-Cohen {
544400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
545b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
546400e64dfSOhad Ben-Cohen 	int ret;
547400e64dfSOhad Ben-Cohen 
548400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
549400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
550400e64dfSOhad Ben-Cohen 		return -EINVAL;
551400e64dfSOhad Ben-Cohen 
552fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
553b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
554fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
555fd2c15ecSOhad Ben-Cohen 	}
556fd2c15ecSOhad Ben-Cohen 
557fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
558fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
559b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
560fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
561fd2c15ecSOhad Ben-Cohen 	}
562fd2c15ecSOhad Ben-Cohen 
563400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
564400e64dfSOhad Ben-Cohen 	if (!mapping) {
565b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "kzalloc mapping failed\n");
566400e64dfSOhad Ben-Cohen 		return -ENOMEM;
567400e64dfSOhad Ben-Cohen 	}
568400e64dfSOhad Ben-Cohen 
569400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
570400e64dfSOhad Ben-Cohen 	if (ret) {
571b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
572400e64dfSOhad Ben-Cohen 		goto out;
573400e64dfSOhad Ben-Cohen 	}
574400e64dfSOhad Ben-Cohen 
575400e64dfSOhad Ben-Cohen 	/*
576400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
577400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
578400e64dfSOhad Ben-Cohen 	 *
579400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
580400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
581400e64dfSOhad Ben-Cohen 	 */
582400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
583400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
584400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
585400e64dfSOhad Ben-Cohen 
586b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
587400e64dfSOhad Ben-Cohen 					rsc->pa, rsc->da, rsc->len);
588400e64dfSOhad Ben-Cohen 
589400e64dfSOhad Ben-Cohen 	return 0;
590400e64dfSOhad Ben-Cohen 
591400e64dfSOhad Ben-Cohen out:
592400e64dfSOhad Ben-Cohen 	kfree(mapping);
593400e64dfSOhad Ben-Cohen 	return ret;
594400e64dfSOhad Ben-Cohen }
595400e64dfSOhad Ben-Cohen 
596400e64dfSOhad Ben-Cohen /**
597400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
598400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
599400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
600fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
601400e64dfSOhad Ben-Cohen  *
602400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
603400e64dfSOhad Ben-Cohen  * contiguous memory regions.
604400e64dfSOhad Ben-Cohen  *
605400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
606400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
607400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
608400e64dfSOhad Ben-Cohen  *
609400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
610400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
611400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
612400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
613400e64dfSOhad Ben-Cohen  */
614fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
615fd2c15ecSOhad Ben-Cohen 				struct fw_rsc_carveout *rsc, int avail)
616400e64dfSOhad Ben-Cohen {
617400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout, *mapping;
618b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
619400e64dfSOhad Ben-Cohen 	dma_addr_t dma;
620400e64dfSOhad Ben-Cohen 	void *va;
621400e64dfSOhad Ben-Cohen 	int ret;
622400e64dfSOhad Ben-Cohen 
623fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
624b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
625fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
626fd2c15ecSOhad Ben-Cohen 	}
627fd2c15ecSOhad Ben-Cohen 
628fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
629fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
630fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
631fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
632fd2c15ecSOhad Ben-Cohen 	}
633fd2c15ecSOhad Ben-Cohen 
634fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
635fd2c15ecSOhad Ben-Cohen 			rsc->da, rsc->pa, rsc->len, rsc->flags);
636fd2c15ecSOhad Ben-Cohen 
637400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
638400e64dfSOhad Ben-Cohen 	if (!mapping) {
639400e64dfSOhad Ben-Cohen 		dev_err(dev, "kzalloc mapping failed\n");
640400e64dfSOhad Ben-Cohen 		return -ENOMEM;
641400e64dfSOhad Ben-Cohen 	}
642400e64dfSOhad Ben-Cohen 
643400e64dfSOhad Ben-Cohen 	carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
644400e64dfSOhad Ben-Cohen 	if (!carveout) {
645400e64dfSOhad Ben-Cohen 		dev_err(dev, "kzalloc carveout failed\n");
646400e64dfSOhad Ben-Cohen 		ret = -ENOMEM;
647400e64dfSOhad Ben-Cohen 		goto free_mapping;
648400e64dfSOhad Ben-Cohen 	}
649400e64dfSOhad Ben-Cohen 
650b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
651400e64dfSOhad Ben-Cohen 	if (!va) {
652b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent err: %d\n", rsc->len);
653400e64dfSOhad Ben-Cohen 		ret = -ENOMEM;
654400e64dfSOhad Ben-Cohen 		goto free_carv;
655400e64dfSOhad Ben-Cohen 	}
656400e64dfSOhad Ben-Cohen 
657400e64dfSOhad Ben-Cohen 	dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len);
658400e64dfSOhad Ben-Cohen 
659400e64dfSOhad Ben-Cohen 	/*
660400e64dfSOhad Ben-Cohen 	 * Ok, this is non-standard.
661400e64dfSOhad Ben-Cohen 	 *
662400e64dfSOhad Ben-Cohen 	 * Sometimes we can't rely on the generic iommu-based DMA API
663400e64dfSOhad Ben-Cohen 	 * to dynamically allocate the device address and then set the IOMMU
664400e64dfSOhad Ben-Cohen 	 * tables accordingly, because some remote processors might
665400e64dfSOhad Ben-Cohen 	 * _require_ us to use hard coded device addresses that their
666400e64dfSOhad Ben-Cohen 	 * firmware was compiled with.
667400e64dfSOhad Ben-Cohen 	 *
668400e64dfSOhad Ben-Cohen 	 * In this case, we must use the IOMMU API directly and map
669400e64dfSOhad Ben-Cohen 	 * the memory to the device address as expected by the remote
670400e64dfSOhad Ben-Cohen 	 * processor.
671400e64dfSOhad Ben-Cohen 	 *
672400e64dfSOhad Ben-Cohen 	 * Obviously such remote processor devices should not be configured
673400e64dfSOhad Ben-Cohen 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
674400e64dfSOhad Ben-Cohen 	 * physical address in this case.
675400e64dfSOhad Ben-Cohen 	 */
676400e64dfSOhad Ben-Cohen 	if (rproc->domain) {
677400e64dfSOhad Ben-Cohen 		ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
678400e64dfSOhad Ben-Cohen 								rsc->flags);
679400e64dfSOhad Ben-Cohen 		if (ret) {
680400e64dfSOhad Ben-Cohen 			dev_err(dev, "iommu_map failed: %d\n", ret);
681400e64dfSOhad Ben-Cohen 			goto dma_free;
682400e64dfSOhad Ben-Cohen 		}
683400e64dfSOhad Ben-Cohen 
684400e64dfSOhad Ben-Cohen 		/*
685400e64dfSOhad Ben-Cohen 		 * We'll need this info later when we'll want to unmap
686400e64dfSOhad Ben-Cohen 		 * everything (e.g. on shutdown).
687400e64dfSOhad Ben-Cohen 		 *
688400e64dfSOhad Ben-Cohen 		 * We can't trust the remote processor not to change the
689400e64dfSOhad Ben-Cohen 		 * resource table, so we must maintain this info independently.
690400e64dfSOhad Ben-Cohen 		 */
691400e64dfSOhad Ben-Cohen 		mapping->da = rsc->da;
692400e64dfSOhad Ben-Cohen 		mapping->len = rsc->len;
693400e64dfSOhad Ben-Cohen 		list_add_tail(&mapping->node, &rproc->mappings);
694400e64dfSOhad Ben-Cohen 
695fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma);
6960e49b72cSOhad Ben-Cohen 	}
697400e64dfSOhad Ben-Cohen 
698400e64dfSOhad Ben-Cohen 	/*
699400e64dfSOhad Ben-Cohen 	 * Some remote processors might need to know the pa
700400e64dfSOhad Ben-Cohen 	 * even though they are behind an IOMMU. E.g., OMAP4's
701400e64dfSOhad Ben-Cohen 	 * remote M3 processor needs this so it can control
702400e64dfSOhad Ben-Cohen 	 * on-chip hardware accelerators that are not behind
703400e64dfSOhad Ben-Cohen 	 * the IOMMU, and therefor must know the pa.
704400e64dfSOhad Ben-Cohen 	 *
705400e64dfSOhad Ben-Cohen 	 * Generally we don't want to expose physical addresses
706400e64dfSOhad Ben-Cohen 	 * if we don't have to (remote processors are generally
707400e64dfSOhad Ben-Cohen 	 * _not_ trusted), so we might want to do this only for
708400e64dfSOhad Ben-Cohen 	 * remote processor that _must_ have this (e.g. OMAP4's
709400e64dfSOhad Ben-Cohen 	 * dual M3 subsystem).
7100e49b72cSOhad Ben-Cohen 	 *
7110e49b72cSOhad Ben-Cohen 	 * Non-IOMMU processors might also want to have this info.
7120e49b72cSOhad Ben-Cohen 	 * In this case, the device address and the physical address
7130e49b72cSOhad Ben-Cohen 	 * are the same.
714400e64dfSOhad Ben-Cohen 	 */
715400e64dfSOhad Ben-Cohen 	rsc->pa = dma;
716400e64dfSOhad Ben-Cohen 
717400e64dfSOhad Ben-Cohen 	carveout->va = va;
718400e64dfSOhad Ben-Cohen 	carveout->len = rsc->len;
719400e64dfSOhad Ben-Cohen 	carveout->dma = dma;
720400e64dfSOhad Ben-Cohen 	carveout->da = rsc->da;
721400e64dfSOhad Ben-Cohen 
722400e64dfSOhad Ben-Cohen 	list_add_tail(&carveout->node, &rproc->carveouts);
723400e64dfSOhad Ben-Cohen 
724400e64dfSOhad Ben-Cohen 	return 0;
725400e64dfSOhad Ben-Cohen 
726400e64dfSOhad Ben-Cohen dma_free:
727b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(dev->parent, rsc->len, va, dma);
728400e64dfSOhad Ben-Cohen free_carv:
729400e64dfSOhad Ben-Cohen 	kfree(carveout);
730400e64dfSOhad Ben-Cohen free_mapping:
731400e64dfSOhad Ben-Cohen 	kfree(mapping);
732400e64dfSOhad Ben-Cohen 	return ret;
733400e64dfSOhad Ben-Cohen }
734400e64dfSOhad Ben-Cohen 
735e12bc14bSOhad Ben-Cohen /*
736e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
737e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
738e12bc14bSOhad Ben-Cohen  */
739e12bc14bSOhad Ben-Cohen static rproc_handle_resource_t rproc_handle_rsc[] = {
740fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
741fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
742fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
7437a186941SOhad Ben-Cohen 	[RSC_VDEV] = NULL, /* VDEVs were handled upon registrarion */
744e12bc14bSOhad Ben-Cohen };
745e12bc14bSOhad Ben-Cohen 
746400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
747400e64dfSOhad Ben-Cohen static int
748fd2c15ecSOhad Ben-Cohen rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
749400e64dfSOhad Ben-Cohen {
750b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
751e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
752fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
753400e64dfSOhad Ben-Cohen 
754fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < table->num; i++) {
755fd2c15ecSOhad Ben-Cohen 		int offset = table->offset[i];
756fd2c15ecSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)table + offset;
757fd2c15ecSOhad Ben-Cohen 		int avail = len - offset - sizeof(*hdr);
758fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
759400e64dfSOhad Ben-Cohen 
760fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
761fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
762fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
763fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
764fd2c15ecSOhad Ben-Cohen 		}
765fd2c15ecSOhad Ben-Cohen 
766fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
767fd2c15ecSOhad Ben-Cohen 
768fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
769fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
770e12bc14bSOhad Ben-Cohen 			continue;
771400e64dfSOhad Ben-Cohen 		}
772400e64dfSOhad Ben-Cohen 
773fd2c15ecSOhad Ben-Cohen 		handler = rproc_handle_rsc[hdr->type];
774e12bc14bSOhad Ben-Cohen 		if (!handler)
775e12bc14bSOhad Ben-Cohen 			continue;
776e12bc14bSOhad Ben-Cohen 
777fd2c15ecSOhad Ben-Cohen 		ret = handler(rproc, rsc, avail);
778400e64dfSOhad Ben-Cohen 		if (ret)
779400e64dfSOhad Ben-Cohen 			break;
780400e64dfSOhad Ben-Cohen 	}
781400e64dfSOhad Ben-Cohen 
782400e64dfSOhad Ben-Cohen 	return ret;
783400e64dfSOhad Ben-Cohen }
784400e64dfSOhad Ben-Cohen 
785400e64dfSOhad Ben-Cohen /* handle firmware resource entries while registering the remote processor */
786400e64dfSOhad Ben-Cohen static int
787fd2c15ecSOhad Ben-Cohen rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
788400e64dfSOhad Ben-Cohen {
789b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
790fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
791400e64dfSOhad Ben-Cohen 
792fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < table->num; i++) {
793fd2c15ecSOhad Ben-Cohen 		int offset = table->offset[i];
794fd2c15ecSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)table + offset;
795fd2c15ecSOhad Ben-Cohen 		int avail = len - offset - sizeof(*hdr);
7967a186941SOhad Ben-Cohen 		struct fw_rsc_vdev *vrsc;
797fd2c15ecSOhad Ben-Cohen 
798fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
799fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
800fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
801fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
802fd2c15ecSOhad Ben-Cohen 		}
803fd2c15ecSOhad Ben-Cohen 
804fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type);
805fd2c15ecSOhad Ben-Cohen 
8067a186941SOhad Ben-Cohen 		if (hdr->type != RSC_VDEV)
8077a186941SOhad Ben-Cohen 			continue;
8087a186941SOhad Ben-Cohen 
8097a186941SOhad Ben-Cohen 		vrsc = (struct fw_rsc_vdev *)hdr->data;
8107a186941SOhad Ben-Cohen 
8117a186941SOhad Ben-Cohen 		ret = rproc_handle_vdev(rproc, vrsc, avail);
8127a186941SOhad Ben-Cohen 		if (ret)
813400e64dfSOhad Ben-Cohen 			break;
814400e64dfSOhad Ben-Cohen 	}
815400e64dfSOhad Ben-Cohen 
816400e64dfSOhad Ben-Cohen 	return ret;
817400e64dfSOhad Ben-Cohen }
818400e64dfSOhad Ben-Cohen 
819400e64dfSOhad Ben-Cohen /**
8201e3e2c7cSOhad Ben-Cohen  * rproc_find_rsc_table() - find the resource table
821400e64dfSOhad Ben-Cohen  * @rproc: the rproc handle
822*bd484984SSjur Brændeland  * @fw: the ELF firmware image
8231e3e2c7cSOhad Ben-Cohen  * @tablesz: place holder for providing back the table size
824400e64dfSOhad Ben-Cohen  *
825400e64dfSOhad Ben-Cohen  * This function finds the resource table inside the remote processor's
8261e3e2c7cSOhad Ben-Cohen  * firmware. It is used both upon the registration of @rproc (in order
8271e3e2c7cSOhad Ben-Cohen  * to look for and register the supported virito devices), and when the
8281e3e2c7cSOhad Ben-Cohen  * @rproc is booted.
829400e64dfSOhad Ben-Cohen  *
8301e3e2c7cSOhad Ben-Cohen  * Returns the pointer to the resource table if it is found, and write its
8311e3e2c7cSOhad Ben-Cohen  * size into @tablesz. If a valid table isn't found, NULL is returned
8321e3e2c7cSOhad Ben-Cohen  * (and @tablesz isn't set).
833400e64dfSOhad Ben-Cohen  */
8341e3e2c7cSOhad Ben-Cohen static struct resource_table *
835*bd484984SSjur Brændeland rproc_find_rsc_table(struct rproc *rproc, const struct firmware *fw,
8361e3e2c7cSOhad Ben-Cohen 							int *tablesz)
837400e64dfSOhad Ben-Cohen {
838400e64dfSOhad Ben-Cohen 	struct elf32_hdr *ehdr;
839400e64dfSOhad Ben-Cohen 	struct elf32_shdr *shdr;
840400e64dfSOhad Ben-Cohen 	const char *name_table;
841b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
8421e3e2c7cSOhad Ben-Cohen 	struct resource_table *table = NULL;
8431e3e2c7cSOhad Ben-Cohen 	int i;
844*bd484984SSjur Brændeland 	const u8 *elf_data = fw->data;
845400e64dfSOhad Ben-Cohen 
846400e64dfSOhad Ben-Cohen 	ehdr = (struct elf32_hdr *)elf_data;
847400e64dfSOhad Ben-Cohen 	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
848400e64dfSOhad Ben-Cohen 	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
849400e64dfSOhad Ben-Cohen 
850400e64dfSOhad Ben-Cohen 	/* look for the resource table and handle it */
851400e64dfSOhad Ben-Cohen 	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
852fd2c15ecSOhad Ben-Cohen 		int size = shdr->sh_size;
853fd2c15ecSOhad Ben-Cohen 		int offset = shdr->sh_offset;
854400e64dfSOhad Ben-Cohen 
855fd2c15ecSOhad Ben-Cohen 		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
856fd2c15ecSOhad Ben-Cohen 			continue;
857fd2c15ecSOhad Ben-Cohen 
858fd2c15ecSOhad Ben-Cohen 		table = (struct resource_table *)(elf_data + offset);
859fd2c15ecSOhad Ben-Cohen 
860fd2c15ecSOhad Ben-Cohen 		/* make sure we have the entire table */
861*bd484984SSjur Brændeland 		if (offset + size > fw->size) {
862fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "resource table truncated\n");
8631e3e2c7cSOhad Ben-Cohen 			return NULL;
864fd2c15ecSOhad Ben-Cohen 		}
865fd2c15ecSOhad Ben-Cohen 
866fd2c15ecSOhad Ben-Cohen 		/* make sure table has at least the header */
867fd2c15ecSOhad Ben-Cohen 		if (sizeof(struct resource_table) > size) {
868fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "header-less resource table\n");
8691e3e2c7cSOhad Ben-Cohen 			return NULL;
870fd2c15ecSOhad Ben-Cohen 		}
871fd2c15ecSOhad Ben-Cohen 
872fd2c15ecSOhad Ben-Cohen 		/* we don't support any version beyond the first */
873fd2c15ecSOhad Ben-Cohen 		if (table->ver != 1) {
874fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "unsupported fw ver: %d\n", table->ver);
8751e3e2c7cSOhad Ben-Cohen 			return NULL;
876fd2c15ecSOhad Ben-Cohen 		}
877fd2c15ecSOhad Ben-Cohen 
878fd2c15ecSOhad Ben-Cohen 		/* make sure reserved bytes are zeroes */
879fd2c15ecSOhad Ben-Cohen 		if (table->reserved[0] || table->reserved[1]) {
880fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "non zero reserved bytes\n");
8811e3e2c7cSOhad Ben-Cohen 			return NULL;
882fd2c15ecSOhad Ben-Cohen 		}
883fd2c15ecSOhad Ben-Cohen 
884fd2c15ecSOhad Ben-Cohen 		/* make sure the offsets array isn't truncated */
885fd2c15ecSOhad Ben-Cohen 		if (table->num * sizeof(table->offset[0]) +
886fd2c15ecSOhad Ben-Cohen 				sizeof(struct resource_table) > size) {
887fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "resource table incomplete\n");
8881e3e2c7cSOhad Ben-Cohen 			return NULL;
8899bc91231SOhad Ben-Cohen 		}
8909bc91231SOhad Ben-Cohen 
8911e3e2c7cSOhad Ben-Cohen 		*tablesz = shdr->sh_size;
892400e64dfSOhad Ben-Cohen 		break;
893400e64dfSOhad Ben-Cohen 	}
894400e64dfSOhad Ben-Cohen 
8951e3e2c7cSOhad Ben-Cohen 	return table;
896400e64dfSOhad Ben-Cohen }
897400e64dfSOhad Ben-Cohen 
898400e64dfSOhad Ben-Cohen /**
899400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
900400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
901400e64dfSOhad Ben-Cohen  *
902400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
9037a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
904400e64dfSOhad Ben-Cohen  */
905400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc)
906400e64dfSOhad Ben-Cohen {
907400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
908b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
909400e64dfSOhad Ben-Cohen 
910400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
911400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
912400e64dfSOhad Ben-Cohen 		rproc_remove_trace_file(entry->priv);
913400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
914400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
915400e64dfSOhad Ben-Cohen 		kfree(entry);
916400e64dfSOhad Ben-Cohen 	}
917400e64dfSOhad Ben-Cohen 
918400e64dfSOhad Ben-Cohen 	/* clean up carveout allocations */
919400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
920b5ab5e24SOhad Ben-Cohen 		dma_free_coherent(dev->parent, entry->len, entry->va, entry->dma);
921400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
922400e64dfSOhad Ben-Cohen 		kfree(entry);
923400e64dfSOhad Ben-Cohen 	}
924400e64dfSOhad Ben-Cohen 
925400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
926400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
927400e64dfSOhad Ben-Cohen 		size_t unmapped;
928400e64dfSOhad Ben-Cohen 
929400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
930400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
931400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
932400e64dfSOhad Ben-Cohen 			dev_err(dev, "failed to unmap %u/%u\n", entry->len,
933400e64dfSOhad Ben-Cohen 								unmapped);
934400e64dfSOhad Ben-Cohen 		}
935400e64dfSOhad Ben-Cohen 
936400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
937400e64dfSOhad Ben-Cohen 		kfree(entry);
938400e64dfSOhad Ben-Cohen 	}
939400e64dfSOhad Ben-Cohen }
940400e64dfSOhad Ben-Cohen 
941400e64dfSOhad Ben-Cohen /* make sure this fw image is sane */
942400e64dfSOhad Ben-Cohen static int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
943400e64dfSOhad Ben-Cohen {
944400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
945b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
946400e64dfSOhad Ben-Cohen 	struct elf32_hdr *ehdr;
94740b78b2cSOhad Ben-Cohen 	char class;
948400e64dfSOhad Ben-Cohen 
949400e64dfSOhad Ben-Cohen 	if (!fw) {
950400e64dfSOhad Ben-Cohen 		dev_err(dev, "failed to load %s\n", name);
951400e64dfSOhad Ben-Cohen 		return -EINVAL;
952400e64dfSOhad Ben-Cohen 	}
953400e64dfSOhad Ben-Cohen 
954400e64dfSOhad Ben-Cohen 	if (fw->size < sizeof(struct elf32_hdr)) {
955400e64dfSOhad Ben-Cohen 		dev_err(dev, "Image is too small\n");
956400e64dfSOhad Ben-Cohen 		return -EINVAL;
957400e64dfSOhad Ben-Cohen 	}
958400e64dfSOhad Ben-Cohen 
959400e64dfSOhad Ben-Cohen 	ehdr = (struct elf32_hdr *)fw->data;
960400e64dfSOhad Ben-Cohen 
96140b78b2cSOhad Ben-Cohen 	/* We only support ELF32 at this point */
96240b78b2cSOhad Ben-Cohen 	class = ehdr->e_ident[EI_CLASS];
96340b78b2cSOhad Ben-Cohen 	if (class != ELFCLASS32) {
96440b78b2cSOhad Ben-Cohen 		dev_err(dev, "Unsupported class: %d\n", class);
96540b78b2cSOhad Ben-Cohen 		return -EINVAL;
96640b78b2cSOhad Ben-Cohen 	}
96740b78b2cSOhad Ben-Cohen 
968cf59d3e9SOhad Ben-Cohen 	/* We assume the firmware has the same endianess as the host */
969cf59d3e9SOhad Ben-Cohen # ifdef __LITTLE_ENDIAN
970cf59d3e9SOhad Ben-Cohen 	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
971cf59d3e9SOhad Ben-Cohen # else /* BIG ENDIAN */
972cf59d3e9SOhad Ben-Cohen 	if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
973cf59d3e9SOhad Ben-Cohen # endif
974cf59d3e9SOhad Ben-Cohen 		dev_err(dev, "Unsupported firmware endianess\n");
975cf59d3e9SOhad Ben-Cohen 		return -EINVAL;
976cf59d3e9SOhad Ben-Cohen 	}
977cf59d3e9SOhad Ben-Cohen 
9789bc91231SOhad Ben-Cohen 	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
9799bc91231SOhad Ben-Cohen 		dev_err(dev, "Image is too small\n");
9809bc91231SOhad Ben-Cohen 		return -EINVAL;
9819bc91231SOhad Ben-Cohen 	}
9829bc91231SOhad Ben-Cohen 
983400e64dfSOhad Ben-Cohen 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
984400e64dfSOhad Ben-Cohen 		dev_err(dev, "Image is corrupted (bad magic)\n");
985400e64dfSOhad Ben-Cohen 		return -EINVAL;
986400e64dfSOhad Ben-Cohen 	}
987400e64dfSOhad Ben-Cohen 
988400e64dfSOhad Ben-Cohen 	if (ehdr->e_phnum == 0) {
989400e64dfSOhad Ben-Cohen 		dev_err(dev, "No loadable segments\n");
990400e64dfSOhad Ben-Cohen 		return -EINVAL;
991400e64dfSOhad Ben-Cohen 	}
992400e64dfSOhad Ben-Cohen 
993400e64dfSOhad Ben-Cohen 	if (ehdr->e_phoff > fw->size) {
994400e64dfSOhad Ben-Cohen 		dev_err(dev, "Firmware size is too small\n");
995400e64dfSOhad Ben-Cohen 		return -EINVAL;
996400e64dfSOhad Ben-Cohen 	}
997400e64dfSOhad Ben-Cohen 
998400e64dfSOhad Ben-Cohen 	return 0;
999400e64dfSOhad Ben-Cohen }
1000400e64dfSOhad Ben-Cohen 
1001400e64dfSOhad Ben-Cohen /*
1002400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
1003400e64dfSOhad Ben-Cohen  */
1004400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1005400e64dfSOhad Ben-Cohen {
1006b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1007400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
1008400e64dfSOhad Ben-Cohen 	struct elf32_hdr *ehdr;
10091e3e2c7cSOhad Ben-Cohen 	struct resource_table *table;
10101e3e2c7cSOhad Ben-Cohen 	int ret, tablesz;
1011400e64dfSOhad Ben-Cohen 
1012400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
1013400e64dfSOhad Ben-Cohen 	if (ret)
1014400e64dfSOhad Ben-Cohen 		return ret;
1015400e64dfSOhad Ben-Cohen 
1016400e64dfSOhad Ben-Cohen 	ehdr = (struct elf32_hdr *)fw->data;
1017400e64dfSOhad Ben-Cohen 
1018400e64dfSOhad Ben-Cohen 	dev_info(dev, "Booting fw image %s, size %d\n", name, fw->size);
1019400e64dfSOhad Ben-Cohen 
1020400e64dfSOhad Ben-Cohen 	/*
1021400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1022400e64dfSOhad Ben-Cohen 	 * just a nop
1023400e64dfSOhad Ben-Cohen 	 */
1024400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
1025400e64dfSOhad Ben-Cohen 	if (ret) {
1026400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
1027400e64dfSOhad Ben-Cohen 		return ret;
1028400e64dfSOhad Ben-Cohen 	}
1029400e64dfSOhad Ben-Cohen 
1030400e64dfSOhad Ben-Cohen 	/*
1031400e64dfSOhad Ben-Cohen 	 * The ELF entry point is the rproc's boot addr (though this is not
1032400e64dfSOhad Ben-Cohen 	 * a configurable property of all remote processors: some will always
1033400e64dfSOhad Ben-Cohen 	 * boot at a specific hardcoded address).
1034400e64dfSOhad Ben-Cohen 	 */
1035400e64dfSOhad Ben-Cohen 	rproc->bootaddr = ehdr->e_entry;
1036400e64dfSOhad Ben-Cohen 
10371e3e2c7cSOhad Ben-Cohen 	/* look for the resource table */
1038*bd484984SSjur Brændeland 	table = rproc_find_rsc_table(rproc, fw, &tablesz);
10391e3e2c7cSOhad Ben-Cohen 	if (!table)
10401e3e2c7cSOhad Ben-Cohen 		goto clean_up;
10411e3e2c7cSOhad Ben-Cohen 
1042400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
10431e3e2c7cSOhad Ben-Cohen 	ret = rproc_handle_boot_rsc(rproc, table, tablesz);
1044400e64dfSOhad Ben-Cohen 	if (ret) {
1045400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
1046400e64dfSOhad Ben-Cohen 		goto clean_up;
1047400e64dfSOhad Ben-Cohen 	}
1048400e64dfSOhad Ben-Cohen 
1049400e64dfSOhad Ben-Cohen 	/* load the ELF segments to memory */
1050*bd484984SSjur Brændeland 	ret = rproc_load_segments(rproc, fw);
1051400e64dfSOhad Ben-Cohen 	if (ret) {
1052400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to load program segments: %d\n", ret);
1053400e64dfSOhad Ben-Cohen 		goto clean_up;
1054400e64dfSOhad Ben-Cohen 	}
1055400e64dfSOhad Ben-Cohen 
1056400e64dfSOhad Ben-Cohen 	/* power up the remote processor */
1057400e64dfSOhad Ben-Cohen 	ret = rproc->ops->start(rproc);
1058400e64dfSOhad Ben-Cohen 	if (ret) {
1059400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1060400e64dfSOhad Ben-Cohen 		goto clean_up;
1061400e64dfSOhad Ben-Cohen 	}
1062400e64dfSOhad Ben-Cohen 
1063400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_RUNNING;
1064400e64dfSOhad Ben-Cohen 
1065400e64dfSOhad Ben-Cohen 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
1066400e64dfSOhad Ben-Cohen 
1067400e64dfSOhad Ben-Cohen 	return 0;
1068400e64dfSOhad Ben-Cohen 
1069400e64dfSOhad Ben-Cohen clean_up:
1070400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1071400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1072400e64dfSOhad Ben-Cohen 	return ret;
1073400e64dfSOhad Ben-Cohen }
1074400e64dfSOhad Ben-Cohen 
1075400e64dfSOhad Ben-Cohen /*
1076400e64dfSOhad Ben-Cohen  * take a firmware and look for virtio devices to register.
1077400e64dfSOhad Ben-Cohen  *
1078400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
1079400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
1080400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
1081400e64dfSOhad Ben-Cohen  * that might be cleaner).
1082400e64dfSOhad Ben-Cohen  */
1083400e64dfSOhad Ben-Cohen static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
1084400e64dfSOhad Ben-Cohen {
1085400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
10861e3e2c7cSOhad Ben-Cohen 	struct resource_table *table;
10871e3e2c7cSOhad Ben-Cohen 	int ret, tablesz;
1088400e64dfSOhad Ben-Cohen 
1089400e64dfSOhad Ben-Cohen 	if (rproc_fw_sanity_check(rproc, fw) < 0)
1090400e64dfSOhad Ben-Cohen 		goto out;
1091400e64dfSOhad Ben-Cohen 
10921e3e2c7cSOhad Ben-Cohen 	/* look for the resource table */
1093*bd484984SSjur Brændeland 	table = rproc_find_rsc_table(rproc, fw,  &tablesz);
10941e3e2c7cSOhad Ben-Cohen 	if (!table)
1095400e64dfSOhad Ben-Cohen 		goto out;
10961e3e2c7cSOhad Ben-Cohen 
10971e3e2c7cSOhad Ben-Cohen 	/* look for virtio devices and register them */
10981e3e2c7cSOhad Ben-Cohen 	ret = rproc_handle_virtio_rsc(rproc, table, tablesz);
10991e3e2c7cSOhad Ben-Cohen 	if (ret)
11001e3e2c7cSOhad Ben-Cohen 		goto out;
1101400e64dfSOhad Ben-Cohen 
1102400e64dfSOhad Ben-Cohen out:
1103400e64dfSOhad Ben-Cohen 	release_firmware(fw);
1104160e7c84SOhad Ben-Cohen 	/* allow rproc_del() contexts, if any, to proceed */
1105400e64dfSOhad Ben-Cohen 	complete_all(&rproc->firmware_loading_complete);
1106400e64dfSOhad Ben-Cohen }
1107400e64dfSOhad Ben-Cohen 
1108400e64dfSOhad Ben-Cohen /**
1109400e64dfSOhad Ben-Cohen  * rproc_boot() - boot a remote processor
1110400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1111400e64dfSOhad Ben-Cohen  *
1112400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1113400e64dfSOhad Ben-Cohen  *
1114400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1115400e64dfSOhad Ben-Cohen  * returns (successfully).
1116400e64dfSOhad Ben-Cohen  *
1117400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1118400e64dfSOhad Ben-Cohen  */
1119400e64dfSOhad Ben-Cohen int rproc_boot(struct rproc *rproc)
1120400e64dfSOhad Ben-Cohen {
1121400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1122400e64dfSOhad Ben-Cohen 	struct device *dev;
1123400e64dfSOhad Ben-Cohen 	int ret;
1124400e64dfSOhad Ben-Cohen 
1125400e64dfSOhad Ben-Cohen 	if (!rproc) {
1126400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1127400e64dfSOhad Ben-Cohen 		return -EINVAL;
1128400e64dfSOhad Ben-Cohen 	}
1129400e64dfSOhad Ben-Cohen 
1130b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1131400e64dfSOhad Ben-Cohen 
1132400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1133400e64dfSOhad Ben-Cohen 	if (ret) {
1134400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1135400e64dfSOhad Ben-Cohen 		return ret;
1136400e64dfSOhad Ben-Cohen 	}
1137400e64dfSOhad Ben-Cohen 
1138400e64dfSOhad Ben-Cohen 	/* loading a firmware is required */
1139400e64dfSOhad Ben-Cohen 	if (!rproc->firmware) {
1140400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: no firmware to load\n", __func__);
1141400e64dfSOhad Ben-Cohen 		ret = -EINVAL;
1142400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1143400e64dfSOhad Ben-Cohen 	}
1144400e64dfSOhad Ben-Cohen 
1145400e64dfSOhad Ben-Cohen 	/* prevent underlying implementation from being removed */
1146b5ab5e24SOhad Ben-Cohen 	if (!try_module_get(dev->parent->driver->owner)) {
1147400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: can't get owner\n", __func__);
1148400e64dfSOhad Ben-Cohen 		ret = -EINVAL;
1149400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1150400e64dfSOhad Ben-Cohen 	}
1151400e64dfSOhad Ben-Cohen 
1152400e64dfSOhad Ben-Cohen 	/* skip the boot process if rproc is already powered up */
1153400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1154400e64dfSOhad Ben-Cohen 		ret = 0;
1155400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1156400e64dfSOhad Ben-Cohen 	}
1157400e64dfSOhad Ben-Cohen 
1158400e64dfSOhad Ben-Cohen 	dev_info(dev, "powering up %s\n", rproc->name);
1159400e64dfSOhad Ben-Cohen 
1160400e64dfSOhad Ben-Cohen 	/* load firmware */
1161400e64dfSOhad Ben-Cohen 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1162400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1163400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware failed: %d\n", ret);
1164400e64dfSOhad Ben-Cohen 		goto downref_rproc;
1165400e64dfSOhad Ben-Cohen 	}
1166400e64dfSOhad Ben-Cohen 
1167400e64dfSOhad Ben-Cohen 	ret = rproc_fw_boot(rproc, firmware_p);
1168400e64dfSOhad Ben-Cohen 
1169400e64dfSOhad Ben-Cohen 	release_firmware(firmware_p);
1170400e64dfSOhad Ben-Cohen 
1171400e64dfSOhad Ben-Cohen downref_rproc:
1172400e64dfSOhad Ben-Cohen 	if (ret) {
1173b5ab5e24SOhad Ben-Cohen 		module_put(dev->parent->driver->owner);
1174400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1175400e64dfSOhad Ben-Cohen 	}
1176400e64dfSOhad Ben-Cohen unlock_mutex:
1177400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1178400e64dfSOhad Ben-Cohen 	return ret;
1179400e64dfSOhad Ben-Cohen }
1180400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1181400e64dfSOhad Ben-Cohen 
1182400e64dfSOhad Ben-Cohen /**
1183400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1184400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1185400e64dfSOhad Ben-Cohen  *
1186400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1187400e64dfSOhad Ben-Cohen  *
1188400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1189400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1190400e64dfSOhad Ben-Cohen  * without really powering off the device.
1191400e64dfSOhad Ben-Cohen  *
1192400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1193400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1194400e64dfSOhad Ben-Cohen  *
1195400e64dfSOhad Ben-Cohen  * Notes:
1196400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1197400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1198400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1199400e64dfSOhad Ben-Cohen  *   needed.
1200400e64dfSOhad Ben-Cohen  */
1201400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1202400e64dfSOhad Ben-Cohen {
1203b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1204400e64dfSOhad Ben-Cohen 	int ret;
1205400e64dfSOhad Ben-Cohen 
1206400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1207400e64dfSOhad Ben-Cohen 	if (ret) {
1208400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1209400e64dfSOhad Ben-Cohen 		return;
1210400e64dfSOhad Ben-Cohen 	}
1211400e64dfSOhad Ben-Cohen 
1212400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1213400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1214400e64dfSOhad Ben-Cohen 		goto out;
1215400e64dfSOhad Ben-Cohen 
1216400e64dfSOhad Ben-Cohen 	/* power off the remote processor */
1217400e64dfSOhad Ben-Cohen 	ret = rproc->ops->stop(rproc);
1218400e64dfSOhad Ben-Cohen 	if (ret) {
1219400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1220400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't stop rproc: %d\n", ret);
1221400e64dfSOhad Ben-Cohen 		goto out;
1222400e64dfSOhad Ben-Cohen 	}
1223400e64dfSOhad Ben-Cohen 
1224400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1225400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1226400e64dfSOhad Ben-Cohen 
1227400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1228400e64dfSOhad Ben-Cohen 
1229400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1230400e64dfSOhad Ben-Cohen 
1231400e64dfSOhad Ben-Cohen 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
1232400e64dfSOhad Ben-Cohen 
1233400e64dfSOhad Ben-Cohen out:
1234400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1235400e64dfSOhad Ben-Cohen 	if (!ret)
1236b5ab5e24SOhad Ben-Cohen 		module_put(dev->parent->driver->owner);
1237400e64dfSOhad Ben-Cohen }
1238400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1239400e64dfSOhad Ben-Cohen 
1240400e64dfSOhad Ben-Cohen /**
1241160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
1242400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
1243400e64dfSOhad Ben-Cohen  *
1244400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
1245400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
1246400e64dfSOhad Ben-Cohen  *
1247400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
1248400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
1249400e64dfSOhad Ben-Cohen  *
1250400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
1251400e64dfSOhad Ben-Cohen  *
1252400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
1253400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
1254400e64dfSOhad Ben-Cohen  * firmware.
1255400e64dfSOhad Ben-Cohen  *
1256400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
12577a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
1258400e64dfSOhad Ben-Cohen  * probed.
1259400e64dfSOhad Ben-Cohen  */
1260160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
1261400e64dfSOhad Ben-Cohen {
1262b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1263400e64dfSOhad Ben-Cohen 	int ret = 0;
1264400e64dfSOhad Ben-Cohen 
1265b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
1266b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
1267b5ab5e24SOhad Ben-Cohen 		return ret;
1268b5ab5e24SOhad Ben-Cohen 
1269b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
1270400e64dfSOhad Ben-Cohen 
1271489d129aSOhad Ben-Cohen 	dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1272489d129aSOhad Ben-Cohen 	dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1273489d129aSOhad Ben-Cohen 
1274400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
1275400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
1276400e64dfSOhad Ben-Cohen 
1277160e7c84SOhad Ben-Cohen 	/* rproc_del() calls must wait until async loader completes */
1278400e64dfSOhad Ben-Cohen 	init_completion(&rproc->firmware_loading_complete);
1279400e64dfSOhad Ben-Cohen 
1280400e64dfSOhad Ben-Cohen 	/*
1281400e64dfSOhad Ben-Cohen 	 * We must retrieve early virtio configuration info from
12827a186941SOhad Ben-Cohen 	 * the firmware (e.g. whether to register a virtio device,
1283400e64dfSOhad Ben-Cohen 	 * what virtio features does it support, ...).
1284400e64dfSOhad Ben-Cohen 	 *
1285400e64dfSOhad Ben-Cohen 	 * We're initiating an asynchronous firmware loading, so we can
1286400e64dfSOhad Ben-Cohen 	 * be built-in kernel code, without hanging the boot process.
1287400e64dfSOhad Ben-Cohen 	 */
1288400e64dfSOhad Ben-Cohen 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
1289400e64dfSOhad Ben-Cohen 					rproc->firmware, dev, GFP_KERNEL,
1290400e64dfSOhad Ben-Cohen 					rproc, rproc_fw_config_virtio);
1291400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1292400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
1293400e64dfSOhad Ben-Cohen 		complete_all(&rproc->firmware_loading_complete);
1294400e64dfSOhad Ben-Cohen 	}
1295400e64dfSOhad Ben-Cohen 
1296400e64dfSOhad Ben-Cohen 	return ret;
1297400e64dfSOhad Ben-Cohen }
1298160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
1299400e64dfSOhad Ben-Cohen 
1300400e64dfSOhad Ben-Cohen /**
1301b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
1302b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
1303b5ab5e24SOhad Ben-Cohen  *
1304b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
1305b5ab5e24SOhad Ben-Cohen  *
1306b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
1307b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
1308b5ab5e24SOhad Ben-Cohen  */
1309b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
1310b5ab5e24SOhad Ben-Cohen {
1311b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
1312b5ab5e24SOhad Ben-Cohen 
13137183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
13147183a2a7SOhad Ben-Cohen 
13157183a2a7SOhad Ben-Cohen 	rproc_delete_debug_dir(rproc);
13167183a2a7SOhad Ben-Cohen 
1317b5ab5e24SOhad Ben-Cohen 	idr_remove_all(&rproc->notifyids);
1318b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
1319b5ab5e24SOhad Ben-Cohen 
1320b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
1321b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
1322b5ab5e24SOhad Ben-Cohen 
1323b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
1324b5ab5e24SOhad Ben-Cohen }
1325b5ab5e24SOhad Ben-Cohen 
1326b5ab5e24SOhad Ben-Cohen static struct device_type rproc_type = {
1327b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
1328b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
1329b5ab5e24SOhad Ben-Cohen };
1330b5ab5e24SOhad Ben-Cohen 
1331b5ab5e24SOhad Ben-Cohen /**
1332400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
1333400e64dfSOhad Ben-Cohen  * @dev: the underlying device
1334400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
1335400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
1336400e64dfSOhad Ben-Cohen  * @firmware: name of firmware file to load
1337400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
1338400e64dfSOhad Ben-Cohen  *
1339400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
1340400e64dfSOhad Ben-Cohen  * it yet.
1341400e64dfSOhad Ben-Cohen  *
1342400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
1343400e64dfSOhad Ben-Cohen  * of the remote processor.
1344400e64dfSOhad Ben-Cohen  *
1345400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
1346160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
1347400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
1348400e64dfSOhad Ben-Cohen  *
1349400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
1350400e64dfSOhad Ben-Cohen  *
1351400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
1352160e7c84SOhad Ben-Cohen  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_put().
1353400e64dfSOhad Ben-Cohen  */
1354400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
1355400e64dfSOhad Ben-Cohen 				const struct rproc_ops *ops,
1356400e64dfSOhad Ben-Cohen 				const char *firmware, int len)
1357400e64dfSOhad Ben-Cohen {
1358400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
1359400e64dfSOhad Ben-Cohen 
1360400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
1361400e64dfSOhad Ben-Cohen 		return NULL;
1362400e64dfSOhad Ben-Cohen 
1363400e64dfSOhad Ben-Cohen 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
1364400e64dfSOhad Ben-Cohen 	if (!rproc) {
1365400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: kzalloc failed\n", __func__);
1366400e64dfSOhad Ben-Cohen 		return NULL;
1367400e64dfSOhad Ben-Cohen 	}
1368400e64dfSOhad Ben-Cohen 
1369400e64dfSOhad Ben-Cohen 	rproc->name = name;
1370400e64dfSOhad Ben-Cohen 	rproc->ops = ops;
1371400e64dfSOhad Ben-Cohen 	rproc->firmware = firmware;
1372400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
1373400e64dfSOhad Ben-Cohen 
1374b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
1375b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
1376b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
1377b5ab5e24SOhad Ben-Cohen 
1378b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
1379b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1380b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
1381b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1382b5ab5e24SOhad Ben-Cohen 		put_device(&rproc->dev);
1383b5ab5e24SOhad Ben-Cohen 		return NULL;
1384b5ab5e24SOhad Ben-Cohen 	}
1385b5ab5e24SOhad Ben-Cohen 
1386b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1387b5ab5e24SOhad Ben-Cohen 
1388400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
1389400e64dfSOhad Ben-Cohen 
1390400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
1391400e64dfSOhad Ben-Cohen 
13927a186941SOhad Ben-Cohen 	idr_init(&rproc->notifyids);
13937a186941SOhad Ben-Cohen 
1394400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
1395400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
1396400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
13977a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
1398400e64dfSOhad Ben-Cohen 
1399400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1400400e64dfSOhad Ben-Cohen 
1401400e64dfSOhad Ben-Cohen 	return rproc;
1402400e64dfSOhad Ben-Cohen }
1403400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
1404400e64dfSOhad Ben-Cohen 
1405400e64dfSOhad Ben-Cohen /**
1406160e7c84SOhad Ben-Cohen  * rproc_put() - unroll rproc_alloc()
1407400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
1408400e64dfSOhad Ben-Cohen  *
1409c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
1410400e64dfSOhad Ben-Cohen  *
1411c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
1412c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
1413400e64dfSOhad Ben-Cohen  */
1414160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
1415400e64dfSOhad Ben-Cohen {
1416b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
1417400e64dfSOhad Ben-Cohen }
1418160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
1419400e64dfSOhad Ben-Cohen 
1420400e64dfSOhad Ben-Cohen /**
1421160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
1422400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
1423400e64dfSOhad Ben-Cohen  *
1424400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
1425400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
1426160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
1427400e64dfSOhad Ben-Cohen  * has completed successfully.
1428400e64dfSOhad Ben-Cohen  *
1429160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
1430c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
1431160e7c84SOhad Ben-Cohen  * one last refcount, one still needs to call rproc_put().
1432400e64dfSOhad Ben-Cohen  *
1433400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
1434400e64dfSOhad Ben-Cohen  */
1435160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
1436400e64dfSOhad Ben-Cohen {
14376db20ea8SOhad Ben-Cohen 	struct rproc_vdev *rvdev, *tmp;
14387a186941SOhad Ben-Cohen 
1439400e64dfSOhad Ben-Cohen 	if (!rproc)
1440400e64dfSOhad Ben-Cohen 		return -EINVAL;
1441400e64dfSOhad Ben-Cohen 
1442400e64dfSOhad Ben-Cohen 	/* if rproc is just being registered, wait */
1443400e64dfSOhad Ben-Cohen 	wait_for_completion(&rproc->firmware_loading_complete);
1444400e64dfSOhad Ben-Cohen 
14457a186941SOhad Ben-Cohen 	/* clean up remote vdev entries */
14466db20ea8SOhad Ben-Cohen 	list_for_each_entry_safe(rvdev, tmp, &rproc->rvdevs, node)
14477a186941SOhad Ben-Cohen 		rproc_remove_virtio_dev(rvdev);
1448400e64dfSOhad Ben-Cohen 
1449b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
1450b5ab5e24SOhad Ben-Cohen 
1451400e64dfSOhad Ben-Cohen 	return 0;
1452400e64dfSOhad Ben-Cohen }
1453160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
1454400e64dfSOhad Ben-Cohen 
1455400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
1456400e64dfSOhad Ben-Cohen {
1457400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
1458b5ab5e24SOhad Ben-Cohen 
1459400e64dfSOhad Ben-Cohen 	return 0;
1460400e64dfSOhad Ben-Cohen }
1461400e64dfSOhad Ben-Cohen module_init(remoteproc_init);
1462400e64dfSOhad Ben-Cohen 
1463400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
1464400e64dfSOhad Ben-Cohen {
1465400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
1466400e64dfSOhad Ben-Cohen }
1467400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
1468400e64dfSOhad Ben-Cohen 
1469400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
1470400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
1471