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>
38400e64dfSOhad Ben-Cohen #include <linux/klist.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 static void klist_rproc_get(struct klist_node *n);
47400e64dfSOhad Ben-Cohen static void klist_rproc_put(struct klist_node *n);
48400e64dfSOhad Ben-Cohen 
49400e64dfSOhad Ben-Cohen /*
50400e64dfSOhad Ben-Cohen  * klist of the available remote processors.
51400e64dfSOhad Ben-Cohen  *
52400e64dfSOhad Ben-Cohen  * We need this in order to support name-based lookups (needed by the
53400e64dfSOhad Ben-Cohen  * rproc_get_by_name()).
54400e64dfSOhad Ben-Cohen  *
55400e64dfSOhad Ben-Cohen  * That said, we don't use rproc_get_by_name() anymore within the rpmsg
56400e64dfSOhad Ben-Cohen  * framework. The use cases that do require its existence should be
57400e64dfSOhad Ben-Cohen  * scrutinized, and hopefully migrated to rproc_boot() using device-based
58400e64dfSOhad Ben-Cohen  * binding.
59400e64dfSOhad Ben-Cohen  *
60400e64dfSOhad Ben-Cohen  * If/when this materializes, we could drop the klist (and the by_name
61400e64dfSOhad Ben-Cohen  * API).
62400e64dfSOhad Ben-Cohen  */
63400e64dfSOhad Ben-Cohen static DEFINE_KLIST(rprocs, klist_rproc_get, klist_rproc_put);
64400e64dfSOhad Ben-Cohen 
65400e64dfSOhad Ben-Cohen typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
66fd2c15ecSOhad Ben-Cohen 				struct resource_table *table, int len);
67fd2c15ecSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc, void *, int avail);
68400e64dfSOhad Ben-Cohen 
69400e64dfSOhad Ben-Cohen /*
70400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
71400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
72400e64dfSOhad Ben-Cohen  * an IOMMU).
73400e64dfSOhad Ben-Cohen  *
74400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
75400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
76400e64dfSOhad Ben-Cohen  *
77400e64dfSOhad Ben-Cohen  * Currently this is mostly a stub, but it will be later used to trigger
78400e64dfSOhad Ben-Cohen  * the recovery of the remote processor.
79400e64dfSOhad Ben-Cohen  */
80400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
81400e64dfSOhad Ben-Cohen 		unsigned long iova, int flags)
82400e64dfSOhad Ben-Cohen {
83400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
84400e64dfSOhad Ben-Cohen 
85400e64dfSOhad Ben-Cohen 	/*
86400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
87400e64dfSOhad Ben-Cohen 	 * we just plan to use this as a recovery trigger.
88400e64dfSOhad Ben-Cohen 	 */
89400e64dfSOhad Ben-Cohen 	return -ENOSYS;
90400e64dfSOhad Ben-Cohen }
91400e64dfSOhad Ben-Cohen 
92400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
93400e64dfSOhad Ben-Cohen {
94400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
95400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
96400e64dfSOhad Ben-Cohen 	int ret;
97400e64dfSOhad Ben-Cohen 
98400e64dfSOhad Ben-Cohen 	/*
99400e64dfSOhad Ben-Cohen 	 * We currently use iommu_present() to decide if an IOMMU
100400e64dfSOhad Ben-Cohen 	 * setup is needed.
101400e64dfSOhad Ben-Cohen 	 *
102400e64dfSOhad Ben-Cohen 	 * This works for simple cases, but will easily fail with
103400e64dfSOhad Ben-Cohen 	 * platforms that do have an IOMMU, but not for this specific
104400e64dfSOhad Ben-Cohen 	 * rproc.
105400e64dfSOhad Ben-Cohen 	 *
106400e64dfSOhad Ben-Cohen 	 * This will be easily solved by introducing hw capabilities
107400e64dfSOhad Ben-Cohen 	 * that will be set by the remoteproc driver.
108400e64dfSOhad Ben-Cohen 	 */
109400e64dfSOhad Ben-Cohen 	if (!iommu_present(dev->bus)) {
1100798e1daSMark Grosen 		dev_dbg(dev, "iommu not found\n");
1110798e1daSMark Grosen 		return 0;
112400e64dfSOhad Ben-Cohen 	}
113400e64dfSOhad Ben-Cohen 
114400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
115400e64dfSOhad Ben-Cohen 	if (!domain) {
116400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
117400e64dfSOhad Ben-Cohen 		return -ENOMEM;
118400e64dfSOhad Ben-Cohen 	}
119400e64dfSOhad Ben-Cohen 
120400e64dfSOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault);
121400e64dfSOhad Ben-Cohen 
122400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
123400e64dfSOhad Ben-Cohen 	if (ret) {
124400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
125400e64dfSOhad Ben-Cohen 		goto free_domain;
126400e64dfSOhad Ben-Cohen 	}
127400e64dfSOhad Ben-Cohen 
128400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
129400e64dfSOhad Ben-Cohen 
130400e64dfSOhad Ben-Cohen 	return 0;
131400e64dfSOhad Ben-Cohen 
132400e64dfSOhad Ben-Cohen free_domain:
133400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
134400e64dfSOhad Ben-Cohen 	return ret;
135400e64dfSOhad Ben-Cohen }
136400e64dfSOhad Ben-Cohen 
137400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
138400e64dfSOhad Ben-Cohen {
139400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
140400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
141400e64dfSOhad Ben-Cohen 
142400e64dfSOhad Ben-Cohen 	if (!domain)
143400e64dfSOhad Ben-Cohen 		return;
144400e64dfSOhad Ben-Cohen 
145400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
146400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
147400e64dfSOhad Ben-Cohen 
148400e64dfSOhad Ben-Cohen 	return;
149400e64dfSOhad Ben-Cohen }
150400e64dfSOhad Ben-Cohen 
151400e64dfSOhad Ben-Cohen /*
152400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
153400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
154400e64dfSOhad Ben-Cohen  * device addresses (which are hardcoded in the firmware).
155400e64dfSOhad Ben-Cohen  *
156400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
157400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
158400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
159400e64dfSOhad Ben-Cohen  *
160400e64dfSOhad Ben-Cohen  * This function is an internal helper with which we can go over the allocated
161400e64dfSOhad Ben-Cohen  * carveouts and translate specific device address to kernel virtual addresses
162400e64dfSOhad Ben-Cohen  * so we can access the referenced memory.
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
166400e64dfSOhad Ben-Cohen  * here the output of the DMA API, which should be more correct.
167400e64dfSOhad Ben-Cohen  */
168400e64dfSOhad Ben-Cohen static void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
169400e64dfSOhad Ben-Cohen {
170400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
171400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
172400e64dfSOhad Ben-Cohen 
173400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
174400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
175400e64dfSOhad Ben-Cohen 
176400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
177400e64dfSOhad Ben-Cohen 		if (offset < 0)
178400e64dfSOhad Ben-Cohen 			continue;
179400e64dfSOhad Ben-Cohen 
180400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
181400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
182400e64dfSOhad Ben-Cohen 			continue;
183400e64dfSOhad Ben-Cohen 
184400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
185400e64dfSOhad Ben-Cohen 
186400e64dfSOhad Ben-Cohen 		break;
187400e64dfSOhad Ben-Cohen 	}
188400e64dfSOhad Ben-Cohen 
189400e64dfSOhad Ben-Cohen 	return ptr;
190400e64dfSOhad Ben-Cohen }
191400e64dfSOhad Ben-Cohen 
192400e64dfSOhad Ben-Cohen /**
193400e64dfSOhad Ben-Cohen  * rproc_load_segments() - load firmware segments to memory
194400e64dfSOhad Ben-Cohen  * @rproc: remote processor which will be booted using these fw segments
195400e64dfSOhad Ben-Cohen  * @elf_data: the content of the ELF firmware image
1969bc91231SOhad Ben-Cohen  * @len: firmware size (in bytes)
197400e64dfSOhad Ben-Cohen  *
198400e64dfSOhad Ben-Cohen  * This function loads the firmware segments to memory, where the remote
199400e64dfSOhad Ben-Cohen  * processor expects them.
200400e64dfSOhad Ben-Cohen  *
201400e64dfSOhad Ben-Cohen  * Some remote processors will expect their code and data to be placed
202400e64dfSOhad Ben-Cohen  * in specific device addresses, and can't have them dynamically assigned.
203400e64dfSOhad Ben-Cohen  *
204400e64dfSOhad Ben-Cohen  * We currently support only those kind of remote processors, and expect
205400e64dfSOhad Ben-Cohen  * the program header's paddr member to contain those addresses. We then go
206400e64dfSOhad Ben-Cohen  * through the physically contiguous "carveout" memory regions which we
207400e64dfSOhad Ben-Cohen  * allocated (and mapped) earlier on behalf of the remote processor,
208400e64dfSOhad Ben-Cohen  * and "translate" device address to kernel addresses, so we can copy the
209400e64dfSOhad Ben-Cohen  * segments where they are expected.
210400e64dfSOhad Ben-Cohen  *
211400e64dfSOhad Ben-Cohen  * Currently we only support remote processors that required carveout
212400e64dfSOhad Ben-Cohen  * allocations and got them mapped onto their iommus. Some processors
213400e64dfSOhad Ben-Cohen  * might be different: they might not have iommus, and would prefer to
214400e64dfSOhad Ben-Cohen  * directly allocate memory for every segment/resource. This is not yet
215400e64dfSOhad Ben-Cohen  * supported, though.
216400e64dfSOhad Ben-Cohen  */
2179bc91231SOhad Ben-Cohen static int
2189bc91231SOhad Ben-Cohen rproc_load_segments(struct rproc *rproc, const u8 *elf_data, size_t len)
219400e64dfSOhad Ben-Cohen {
220400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
221400e64dfSOhad Ben-Cohen 	struct elf32_hdr *ehdr;
222400e64dfSOhad Ben-Cohen 	struct elf32_phdr *phdr;
223400e64dfSOhad Ben-Cohen 	int i, ret = 0;
224400e64dfSOhad Ben-Cohen 
225400e64dfSOhad Ben-Cohen 	ehdr = (struct elf32_hdr *)elf_data;
226400e64dfSOhad Ben-Cohen 	phdr = (struct elf32_phdr *)(elf_data + ehdr->e_phoff);
227400e64dfSOhad Ben-Cohen 
228400e64dfSOhad Ben-Cohen 	/* go through the available ELF segments */
229400e64dfSOhad Ben-Cohen 	for (i = 0; i < ehdr->e_phnum; i++, phdr++) {
230400e64dfSOhad Ben-Cohen 		u32 da = phdr->p_paddr;
231400e64dfSOhad Ben-Cohen 		u32 memsz = phdr->p_memsz;
232400e64dfSOhad Ben-Cohen 		u32 filesz = phdr->p_filesz;
2339bc91231SOhad Ben-Cohen 		u32 offset = phdr->p_offset;
234400e64dfSOhad Ben-Cohen 		void *ptr;
235400e64dfSOhad Ben-Cohen 
236400e64dfSOhad Ben-Cohen 		if (phdr->p_type != PT_LOAD)
237400e64dfSOhad Ben-Cohen 			continue;
238400e64dfSOhad Ben-Cohen 
239400e64dfSOhad Ben-Cohen 		dev_dbg(dev, "phdr: type %d da 0x%x memsz 0x%x filesz 0x%x\n",
240400e64dfSOhad Ben-Cohen 					phdr->p_type, da, memsz, filesz);
241400e64dfSOhad Ben-Cohen 
242400e64dfSOhad Ben-Cohen 		if (filesz > memsz) {
243400e64dfSOhad Ben-Cohen 			dev_err(dev, "bad phdr filesz 0x%x memsz 0x%x\n",
244400e64dfSOhad Ben-Cohen 							filesz, memsz);
245400e64dfSOhad Ben-Cohen 			ret = -EINVAL;
246400e64dfSOhad Ben-Cohen 			break;
247400e64dfSOhad Ben-Cohen 		}
248400e64dfSOhad Ben-Cohen 
2499bc91231SOhad Ben-Cohen 		if (offset + filesz > len) {
2509bc91231SOhad Ben-Cohen 			dev_err(dev, "truncated fw: need 0x%x avail 0x%x\n",
2519bc91231SOhad Ben-Cohen 					offset + filesz, len);
2529bc91231SOhad Ben-Cohen 			ret = -EINVAL;
2539bc91231SOhad Ben-Cohen 			break;
2549bc91231SOhad Ben-Cohen 		}
2559bc91231SOhad Ben-Cohen 
256400e64dfSOhad Ben-Cohen 		/* grab the kernel address for this device address */
257400e64dfSOhad Ben-Cohen 		ptr = rproc_da_to_va(rproc, da, memsz);
258400e64dfSOhad Ben-Cohen 		if (!ptr) {
259400e64dfSOhad Ben-Cohen 			dev_err(dev, "bad phdr da 0x%x mem 0x%x\n", da, memsz);
260400e64dfSOhad Ben-Cohen 			ret = -EINVAL;
261400e64dfSOhad Ben-Cohen 			break;
262400e64dfSOhad Ben-Cohen 		}
263400e64dfSOhad Ben-Cohen 
264400e64dfSOhad Ben-Cohen 		/* put the segment where the remote processor expects it */
265400e64dfSOhad Ben-Cohen 		if (phdr->p_filesz)
266400e64dfSOhad Ben-Cohen 			memcpy(ptr, elf_data + phdr->p_offset, filesz);
267400e64dfSOhad Ben-Cohen 
268400e64dfSOhad Ben-Cohen 		/*
269400e64dfSOhad Ben-Cohen 		 * Zero out remaining memory for this segment.
270400e64dfSOhad Ben-Cohen 		 *
271400e64dfSOhad Ben-Cohen 		 * This isn't strictly required since dma_alloc_coherent already
272400e64dfSOhad Ben-Cohen 		 * did this for us. albeit harmless, we may consider removing
273400e64dfSOhad Ben-Cohen 		 * this.
274400e64dfSOhad Ben-Cohen 		 */
275400e64dfSOhad Ben-Cohen 		if (memsz > filesz)
276400e64dfSOhad Ben-Cohen 			memset(ptr + filesz, 0, memsz - filesz);
277400e64dfSOhad Ben-Cohen 	}
278400e64dfSOhad Ben-Cohen 
279400e64dfSOhad Ben-Cohen 	return ret;
280400e64dfSOhad Ben-Cohen }
281400e64dfSOhad Ben-Cohen 
282400e64dfSOhad Ben-Cohen /**
283fd2c15ecSOhad Ben-Cohen  * rproc_handle_early_vdev() - early handle a virtio header resource
284400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
285400e64dfSOhad Ben-Cohen  * @rsc: the resource descriptor
286fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
287400e64dfSOhad Ben-Cohen  *
288400e64dfSOhad Ben-Cohen  * The existence of this virtio hdr resource entry means that the firmware
289400e64dfSOhad Ben-Cohen  * of this @rproc supports this virtio device.
290400e64dfSOhad Ben-Cohen  *
291400e64dfSOhad Ben-Cohen  * Currently we support only a single virtio device of type VIRTIO_ID_RPMSG,
292400e64dfSOhad Ben-Cohen  * but the plan is to remove this limitation and support any number
293400e64dfSOhad Ben-Cohen  * of virtio devices (and of any type). We'll also add support for dynamically
294fd2c15ecSOhad Ben-Cohen  * adding (and removing) virtio devices over the rpmsg bus, but simple
295400e64dfSOhad Ben-Cohen  * firmwares that doesn't want to get involved with rpmsg will be able
296fd2c15ecSOhad Ben-Cohen  * to simply use the resource table for this.
297400e64dfSOhad Ben-Cohen  *
298400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
299400e64dfSOhad Ben-Cohen  */
300fd2c15ecSOhad Ben-Cohen static int rproc_handle_early_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
301fd2c15ecSOhad Ben-Cohen 								int avail)
302400e64dfSOhad Ben-Cohen {
303400e64dfSOhad Ben-Cohen 	struct rproc_vdev *rvdev;
304400e64dfSOhad Ben-Cohen 
305fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
306fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
307fd2c15ecSOhad Ben-Cohen 		dev_err(rproc->dev, "vdev rsc is truncated\n");
308fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
309fd2c15ecSOhad Ben-Cohen 	}
310fd2c15ecSOhad Ben-Cohen 
311400e64dfSOhad Ben-Cohen 	/* we only support VIRTIO_ID_RPMSG devices for now */
312fd2c15ecSOhad Ben-Cohen 	if (rsc->id != VIRTIO_ID_RPMSG) {
313fd2c15ecSOhad Ben-Cohen 		dev_warn(rproc->dev, "unsupported vdev: %d\n", rsc->id);
314400e64dfSOhad Ben-Cohen 		return -EINVAL;
315400e64dfSOhad Ben-Cohen 	}
316400e64dfSOhad Ben-Cohen 
317400e64dfSOhad Ben-Cohen 	/* we only support a single vdev per rproc for now */
318fd2c15ecSOhad Ben-Cohen 	if (rproc->rvdev) {
319fd2c15ecSOhad Ben-Cohen 		dev_warn(rproc->dev, "redundant vdev entry\n");
320400e64dfSOhad Ben-Cohen 		return -EINVAL;
321400e64dfSOhad Ben-Cohen 	}
322400e64dfSOhad Ben-Cohen 
323400e64dfSOhad Ben-Cohen 	rvdev = kzalloc(sizeof(struct rproc_vdev), GFP_KERNEL);
324400e64dfSOhad Ben-Cohen 	if (!rvdev)
325400e64dfSOhad Ben-Cohen 		return -ENOMEM;
326400e64dfSOhad Ben-Cohen 
327400e64dfSOhad Ben-Cohen 	/* remember the device features */
328fd2c15ecSOhad Ben-Cohen 	rvdev->dfeatures = rsc->dfeatures;
329400e64dfSOhad Ben-Cohen 
330400e64dfSOhad Ben-Cohen 	rproc->rvdev = rvdev;
331400e64dfSOhad Ben-Cohen 	rvdev->rproc = rproc;
332400e64dfSOhad Ben-Cohen 
333400e64dfSOhad Ben-Cohen 	return 0;
334400e64dfSOhad Ben-Cohen }
335400e64dfSOhad Ben-Cohen 
336400e64dfSOhad Ben-Cohen /**
337fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
338400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
339400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
340fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
341400e64dfSOhad Ben-Cohen  *
342400e64dfSOhad Ben-Cohen  * This resource entry requires allocation of non-cacheable memory
343400e64dfSOhad Ben-Cohen  * for a virtio vring. Currently we only support two vrings per remote
344400e64dfSOhad Ben-Cohen  * processor, required for the virtio rpmsg device.
345400e64dfSOhad Ben-Cohen  *
346400e64dfSOhad Ben-Cohen  * The 'len' member of @rsc should contain the number of buffers this vring
347400e64dfSOhad Ben-Cohen  * support and 'da' should either contain the device address where
348400e64dfSOhad Ben-Cohen  * the remote processor is expecting the vring, or indicate that
349400e64dfSOhad Ben-Cohen  * dynamically allocation of the vring's device address is supported.
350400e64dfSOhad Ben-Cohen  *
351400e64dfSOhad Ben-Cohen  * Note: 'da' is currently not handled. This will be revised when the generic
352400e64dfSOhad Ben-Cohen  * iommu-based DMA API will arrive, or a dynanic & non-iommu use case show
353400e64dfSOhad Ben-Cohen  * up. Meanwhile, statically-addressed iommu-based images should use
354400e64dfSOhad Ben-Cohen  * RSC_DEVMEM resource entries to map their require 'da' to the physical
355400e64dfSOhad Ben-Cohen  * address of their base CMA region.
356400e64dfSOhad Ben-Cohen  *
357400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
358400e64dfSOhad Ben-Cohen  */
359fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
360fd2c15ecSOhad Ben-Cohen 								int avail)
361400e64dfSOhad Ben-Cohen {
362400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
363400e64dfSOhad Ben-Cohen 	struct rproc_vdev *rvdev = rproc->rvdev;
364fd2c15ecSOhad Ben-Cohen 	int i;
365fd2c15ecSOhad Ben-Cohen 
366fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
367fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
368fd2c15ecSOhad Ben-Cohen 			+ rsc->config_len > avail) {
369fd2c15ecSOhad Ben-Cohen 		dev_err(rproc->dev, "vdev rsc is truncated\n");
370fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
371fd2c15ecSOhad Ben-Cohen 	}
372fd2c15ecSOhad Ben-Cohen 
373fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
374fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
375fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
376fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
377fd2c15ecSOhad Ben-Cohen 	}
378fd2c15ecSOhad Ben-Cohen 
379fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "vdev rsc: id %d, dfeatures %x, cfg len %d, %d vrings\n",
380fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
381400e64dfSOhad Ben-Cohen 
382400e64dfSOhad Ben-Cohen 	/* no vdev is in place ? */
383400e64dfSOhad Ben-Cohen 	if (!rvdev) {
384400e64dfSOhad Ben-Cohen 		dev_err(dev, "vring requested without a virtio dev entry\n");
385400e64dfSOhad Ben-Cohen 		return -EINVAL;
386400e64dfSOhad Ben-Cohen 	}
387400e64dfSOhad Ben-Cohen 
388400e64dfSOhad Ben-Cohen 	/* we currently support two vrings per rproc (for rx and tx) */
389fd2c15ecSOhad Ben-Cohen 	if (rsc->num_of_vrings != ARRAY_SIZE(rvdev->vring)) {
390fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
391400e64dfSOhad Ben-Cohen 		return -EINVAL;
392400e64dfSOhad Ben-Cohen 	}
393400e64dfSOhad Ben-Cohen 
394fd2c15ecSOhad Ben-Cohen 	/* initialize the vrings */
395fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
396fd2c15ecSOhad Ben-Cohen 		struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
397fd2c15ecSOhad Ben-Cohen 		dma_addr_t dma;
398fd2c15ecSOhad Ben-Cohen 		int size;
399fd2c15ecSOhad Ben-Cohen 		void *va;
400fd2c15ecSOhad Ben-Cohen 
401fd2c15ecSOhad Ben-Cohen 		/* make sure reserved bytes are zeroes */
402fd2c15ecSOhad Ben-Cohen 		if (vring->reserved) {
403fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "vring rsc has non zero reserved bytes\n");
404fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
405fd2c15ecSOhad Ben-Cohen 		}
406fd2c15ecSOhad Ben-Cohen 
407fd2c15ecSOhad Ben-Cohen 		/* the firmware must provide the expected queue size */
408fd2c15ecSOhad Ben-Cohen 		if (!vring->num) {
409fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "missing expected queue size\n");
410fd2c15ecSOhad Ben-Cohen 			/* potential cleanups are taken care of later on */
411400e64dfSOhad Ben-Cohen 			return -EINVAL;
412400e64dfSOhad Ben-Cohen 		}
413400e64dfSOhad Ben-Cohen 
414400e64dfSOhad Ben-Cohen 		/* actual size of vring (in bytes) */
415fd2c15ecSOhad Ben-Cohen 		size = PAGE_ALIGN(vring_size(vring->num, AMP_VRING_ALIGN));
416400e64dfSOhad Ben-Cohen 
417400e64dfSOhad Ben-Cohen 		/*
418400e64dfSOhad Ben-Cohen 		 * Allocate non-cacheable memory for the vring. In the future
419400e64dfSOhad Ben-Cohen 		 * this call will also configure the IOMMU for us
420400e64dfSOhad Ben-Cohen 		 */
421400e64dfSOhad Ben-Cohen 		va = dma_alloc_coherent(dev, size, &dma, GFP_KERNEL);
422400e64dfSOhad Ben-Cohen 		if (!va) {
423400e64dfSOhad Ben-Cohen 			dev_err(dev, "dma_alloc_coherent failed\n");
424fd2c15ecSOhad Ben-Cohen 			/* potential cleanups are taken care of later on */
425fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
426400e64dfSOhad Ben-Cohen 		}
427400e64dfSOhad Ben-Cohen 
428fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "vring%d: va %p dma %x qsz %d ring size %x\n", i,
429fd2c15ecSOhad Ben-Cohen 						va, dma, vring->num, size);
430400e64dfSOhad Ben-Cohen 
431fd2c15ecSOhad Ben-Cohen 		rvdev->vring[i].len = vring->num;
432fd2c15ecSOhad Ben-Cohen 		rvdev->vring[i].va = va;
433fd2c15ecSOhad Ben-Cohen 		rvdev->vring[i].dma = dma;
434fd2c15ecSOhad Ben-Cohen 	}
435400e64dfSOhad Ben-Cohen 
436400e64dfSOhad Ben-Cohen 	return 0;
437400e64dfSOhad Ben-Cohen }
438400e64dfSOhad Ben-Cohen 
439400e64dfSOhad Ben-Cohen /**
440400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
441400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
442400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
443fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
444400e64dfSOhad Ben-Cohen  *
445400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
446400e64dfSOhad Ben-Cohen  * export it via debugfs.
447400e64dfSOhad Ben-Cohen  *
448400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
449400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
450400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
451400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
452400e64dfSOhad Ben-Cohen  *
453400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
454400e64dfSOhad Ben-Cohen  */
455fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
456fd2c15ecSOhad Ben-Cohen 								int avail)
457400e64dfSOhad Ben-Cohen {
458400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *trace;
459400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
460400e64dfSOhad Ben-Cohen 	void *ptr;
461400e64dfSOhad Ben-Cohen 	char name[15];
462400e64dfSOhad Ben-Cohen 
463fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
464fd2c15ecSOhad Ben-Cohen 		dev_err(rproc->dev, "trace rsc is truncated\n");
465fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
466fd2c15ecSOhad Ben-Cohen 	}
467fd2c15ecSOhad Ben-Cohen 
468fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
469fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
470fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
471fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
472fd2c15ecSOhad Ben-Cohen 	}
473fd2c15ecSOhad Ben-Cohen 
474400e64dfSOhad Ben-Cohen 	/* what's the kernel address of this resource ? */
475400e64dfSOhad Ben-Cohen 	ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
476400e64dfSOhad Ben-Cohen 	if (!ptr) {
477400e64dfSOhad Ben-Cohen 		dev_err(dev, "erroneous trace resource entry\n");
478400e64dfSOhad Ben-Cohen 		return -EINVAL;
479400e64dfSOhad Ben-Cohen 	}
480400e64dfSOhad Ben-Cohen 
481400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
482400e64dfSOhad Ben-Cohen 	if (!trace) {
483400e64dfSOhad Ben-Cohen 		dev_err(dev, "kzalloc trace failed\n");
484400e64dfSOhad Ben-Cohen 		return -ENOMEM;
485400e64dfSOhad Ben-Cohen 	}
486400e64dfSOhad Ben-Cohen 
487400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
488400e64dfSOhad Ben-Cohen 	trace->len = rsc->len;
489400e64dfSOhad Ben-Cohen 	trace->va = ptr;
490400e64dfSOhad Ben-Cohen 
491400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
492400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
493400e64dfSOhad Ben-Cohen 
494400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
495400e64dfSOhad Ben-Cohen 	trace->priv = rproc_create_trace_file(name, rproc, trace);
496400e64dfSOhad Ben-Cohen 	if (!trace->priv) {
497400e64dfSOhad Ben-Cohen 		trace->va = NULL;
498400e64dfSOhad Ben-Cohen 		kfree(trace);
499400e64dfSOhad Ben-Cohen 		return -EINVAL;
500400e64dfSOhad Ben-Cohen 	}
501400e64dfSOhad Ben-Cohen 
502400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
503400e64dfSOhad Ben-Cohen 
504400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
505400e64dfSOhad Ben-Cohen 
506fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n", name, ptr,
507400e64dfSOhad Ben-Cohen 						rsc->da, rsc->len);
508400e64dfSOhad Ben-Cohen 
509400e64dfSOhad Ben-Cohen 	return 0;
510400e64dfSOhad Ben-Cohen }
511400e64dfSOhad Ben-Cohen 
512400e64dfSOhad Ben-Cohen /**
513400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
514400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
515400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
516fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
517400e64dfSOhad Ben-Cohen  *
518400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
519400e64dfSOhad Ben-Cohen  *
520400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
521400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
522400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
523400e64dfSOhad Ben-Cohen  *
524400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
525400e64dfSOhad Ben-Cohen  *
526400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
527400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
528400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
529400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
530400e64dfSOhad Ben-Cohen  *
531400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
532400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
533400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
534400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
535400e64dfSOhad Ben-Cohen  * are outside those ranges.
536400e64dfSOhad Ben-Cohen  */
537fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
538fd2c15ecSOhad Ben-Cohen 								int avail)
539400e64dfSOhad Ben-Cohen {
540400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
541400e64dfSOhad Ben-Cohen 	int ret;
542400e64dfSOhad Ben-Cohen 
543400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
544400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
545400e64dfSOhad Ben-Cohen 		return -EINVAL;
546400e64dfSOhad Ben-Cohen 
547fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
548fd2c15ecSOhad Ben-Cohen 		dev_err(rproc->dev, "devmem rsc is truncated\n");
549fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
550fd2c15ecSOhad Ben-Cohen 	}
551fd2c15ecSOhad Ben-Cohen 
552fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
553fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
554fd2c15ecSOhad Ben-Cohen 		dev_err(rproc->dev, "devmem rsc has non zero reserved bytes\n");
555fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
556fd2c15ecSOhad Ben-Cohen 	}
557fd2c15ecSOhad Ben-Cohen 
558400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
559400e64dfSOhad Ben-Cohen 	if (!mapping) {
560400e64dfSOhad Ben-Cohen 		dev_err(rproc->dev, "kzalloc mapping failed\n");
561400e64dfSOhad Ben-Cohen 		return -ENOMEM;
562400e64dfSOhad Ben-Cohen 	}
563400e64dfSOhad Ben-Cohen 
564400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
565400e64dfSOhad Ben-Cohen 	if (ret) {
566400e64dfSOhad Ben-Cohen 		dev_err(rproc->dev, "failed to map devmem: %d\n", ret);
567400e64dfSOhad Ben-Cohen 		goto out;
568400e64dfSOhad Ben-Cohen 	}
569400e64dfSOhad Ben-Cohen 
570400e64dfSOhad Ben-Cohen 	/*
571400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
572400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
573400e64dfSOhad Ben-Cohen 	 *
574400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
575400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
576400e64dfSOhad Ben-Cohen 	 */
577400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
578400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
579400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
580400e64dfSOhad Ben-Cohen 
581fd2c15ecSOhad Ben-Cohen 	dev_dbg(rproc->dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
582400e64dfSOhad Ben-Cohen 					rsc->pa, rsc->da, rsc->len);
583400e64dfSOhad Ben-Cohen 
584400e64dfSOhad Ben-Cohen 	return 0;
585400e64dfSOhad Ben-Cohen 
586400e64dfSOhad Ben-Cohen out:
587400e64dfSOhad Ben-Cohen 	kfree(mapping);
588400e64dfSOhad Ben-Cohen 	return ret;
589400e64dfSOhad Ben-Cohen }
590400e64dfSOhad Ben-Cohen 
591400e64dfSOhad Ben-Cohen /**
592400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
593400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
594400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
595fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
596400e64dfSOhad Ben-Cohen  *
597400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
598400e64dfSOhad Ben-Cohen  * contiguous memory regions.
599400e64dfSOhad Ben-Cohen  *
600400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
601400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
602400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
603400e64dfSOhad Ben-Cohen  *
604400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
605400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
606400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
607400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
608400e64dfSOhad Ben-Cohen  */
609fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
610fd2c15ecSOhad Ben-Cohen 				struct fw_rsc_carveout *rsc, int avail)
611400e64dfSOhad Ben-Cohen {
612400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout, *mapping;
613400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
614400e64dfSOhad Ben-Cohen 	dma_addr_t dma;
615400e64dfSOhad Ben-Cohen 	void *va;
616400e64dfSOhad Ben-Cohen 	int ret;
617400e64dfSOhad Ben-Cohen 
618fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
619fd2c15ecSOhad Ben-Cohen 		dev_err(rproc->dev, "carveout rsc is truncated\n");
620fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
621fd2c15ecSOhad Ben-Cohen 	}
622fd2c15ecSOhad Ben-Cohen 
623fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
624fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
625fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
626fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
627fd2c15ecSOhad Ben-Cohen 	}
628fd2c15ecSOhad Ben-Cohen 
629fd2c15ecSOhad Ben-Cohen 	dev_dbg(dev, "carveout rsc: da %x, pa %x, len %x, flags %x\n",
630fd2c15ecSOhad Ben-Cohen 			rsc->da, rsc->pa, rsc->len, rsc->flags);
631fd2c15ecSOhad Ben-Cohen 
632400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
633400e64dfSOhad Ben-Cohen 	if (!mapping) {
634400e64dfSOhad Ben-Cohen 		dev_err(dev, "kzalloc mapping failed\n");
635400e64dfSOhad Ben-Cohen 		return -ENOMEM;
636400e64dfSOhad Ben-Cohen 	}
637400e64dfSOhad Ben-Cohen 
638400e64dfSOhad Ben-Cohen 	carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
639400e64dfSOhad Ben-Cohen 	if (!carveout) {
640400e64dfSOhad Ben-Cohen 		dev_err(dev, "kzalloc carveout failed\n");
641400e64dfSOhad Ben-Cohen 		ret = -ENOMEM;
642400e64dfSOhad Ben-Cohen 		goto free_mapping;
643400e64dfSOhad Ben-Cohen 	}
644400e64dfSOhad Ben-Cohen 
645400e64dfSOhad Ben-Cohen 	va = dma_alloc_coherent(dev, rsc->len, &dma, GFP_KERNEL);
646400e64dfSOhad Ben-Cohen 	if (!va) {
647400e64dfSOhad Ben-Cohen 		dev_err(dev, "failed to dma alloc carveout: %d\n", rsc->len);
648400e64dfSOhad Ben-Cohen 		ret = -ENOMEM;
649400e64dfSOhad Ben-Cohen 		goto free_carv;
650400e64dfSOhad Ben-Cohen 	}
651400e64dfSOhad Ben-Cohen 
652400e64dfSOhad Ben-Cohen 	dev_dbg(dev, "carveout va %p, dma %x, len 0x%x\n", va, dma, rsc->len);
653400e64dfSOhad Ben-Cohen 
654400e64dfSOhad Ben-Cohen 	/*
655400e64dfSOhad Ben-Cohen 	 * Ok, this is non-standard.
656400e64dfSOhad Ben-Cohen 	 *
657400e64dfSOhad Ben-Cohen 	 * Sometimes we can't rely on the generic iommu-based DMA API
658400e64dfSOhad Ben-Cohen 	 * to dynamically allocate the device address and then set the IOMMU
659400e64dfSOhad Ben-Cohen 	 * tables accordingly, because some remote processors might
660400e64dfSOhad Ben-Cohen 	 * _require_ us to use hard coded device addresses that their
661400e64dfSOhad Ben-Cohen 	 * firmware was compiled with.
662400e64dfSOhad Ben-Cohen 	 *
663400e64dfSOhad Ben-Cohen 	 * In this case, we must use the IOMMU API directly and map
664400e64dfSOhad Ben-Cohen 	 * the memory to the device address as expected by the remote
665400e64dfSOhad Ben-Cohen 	 * processor.
666400e64dfSOhad Ben-Cohen 	 *
667400e64dfSOhad Ben-Cohen 	 * Obviously such remote processor devices should not be configured
668400e64dfSOhad Ben-Cohen 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
669400e64dfSOhad Ben-Cohen 	 * physical address in this case.
670400e64dfSOhad Ben-Cohen 	 */
671400e64dfSOhad Ben-Cohen 	if (rproc->domain) {
672400e64dfSOhad Ben-Cohen 		ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
673400e64dfSOhad Ben-Cohen 								rsc->flags);
674400e64dfSOhad Ben-Cohen 		if (ret) {
675400e64dfSOhad Ben-Cohen 			dev_err(dev, "iommu_map failed: %d\n", ret);
676400e64dfSOhad Ben-Cohen 			goto dma_free;
677400e64dfSOhad Ben-Cohen 		}
678400e64dfSOhad Ben-Cohen 
679400e64dfSOhad Ben-Cohen 		/*
680400e64dfSOhad Ben-Cohen 		 * We'll need this info later when we'll want to unmap
681400e64dfSOhad Ben-Cohen 		 * everything (e.g. on shutdown).
682400e64dfSOhad Ben-Cohen 		 *
683400e64dfSOhad Ben-Cohen 		 * We can't trust the remote processor not to change the
684400e64dfSOhad Ben-Cohen 		 * resource table, so we must maintain this info independently.
685400e64dfSOhad Ben-Cohen 		 */
686400e64dfSOhad Ben-Cohen 		mapping->da = rsc->da;
687400e64dfSOhad Ben-Cohen 		mapping->len = rsc->len;
688400e64dfSOhad Ben-Cohen 		list_add_tail(&mapping->node, &rproc->mappings);
689400e64dfSOhad Ben-Cohen 
690fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "carveout mapped 0x%x to 0x%x\n", rsc->da, dma);
691400e64dfSOhad Ben-Cohen 
692400e64dfSOhad Ben-Cohen 		/*
693400e64dfSOhad Ben-Cohen 		 * Some remote processors might need to know the pa
694400e64dfSOhad Ben-Cohen 		 * even though they are behind an IOMMU. E.g., OMAP4's
695400e64dfSOhad Ben-Cohen 		 * remote M3 processor needs this so it can control
696400e64dfSOhad Ben-Cohen 		 * on-chip hardware accelerators that are not behind
697400e64dfSOhad Ben-Cohen 		 * the IOMMU, and therefor must know the pa.
698400e64dfSOhad Ben-Cohen 		 *
699400e64dfSOhad Ben-Cohen 		 * Generally we don't want to expose physical addresses
700400e64dfSOhad Ben-Cohen 		 * if we don't have to (remote processors are generally
701400e64dfSOhad Ben-Cohen 		 * _not_ trusted), so we might want to do this only for
702400e64dfSOhad Ben-Cohen 		 * remote processor that _must_ have this (e.g. OMAP4's
703400e64dfSOhad Ben-Cohen 		 * dual M3 subsystem).
704400e64dfSOhad Ben-Cohen 		 */
705400e64dfSOhad Ben-Cohen 		rsc->pa = dma;
706400e64dfSOhad Ben-Cohen 	}
707400e64dfSOhad Ben-Cohen 
708400e64dfSOhad Ben-Cohen 	carveout->va = va;
709400e64dfSOhad Ben-Cohen 	carveout->len = rsc->len;
710400e64dfSOhad Ben-Cohen 	carveout->dma = dma;
711400e64dfSOhad Ben-Cohen 	carveout->da = rsc->da;
712400e64dfSOhad Ben-Cohen 
713400e64dfSOhad Ben-Cohen 	list_add_tail(&carveout->node, &rproc->carveouts);
714400e64dfSOhad Ben-Cohen 
715400e64dfSOhad Ben-Cohen 	return 0;
716400e64dfSOhad Ben-Cohen 
717400e64dfSOhad Ben-Cohen dma_free:
718400e64dfSOhad Ben-Cohen 	dma_free_coherent(dev, rsc->len, va, dma);
719400e64dfSOhad Ben-Cohen free_carv:
720400e64dfSOhad Ben-Cohen 	kfree(carveout);
721400e64dfSOhad Ben-Cohen free_mapping:
722400e64dfSOhad Ben-Cohen 	kfree(mapping);
723400e64dfSOhad Ben-Cohen 	return ret;
724400e64dfSOhad Ben-Cohen }
725400e64dfSOhad Ben-Cohen 
726e12bc14bSOhad Ben-Cohen /*
727e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
728e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
729e12bc14bSOhad Ben-Cohen  */
730e12bc14bSOhad Ben-Cohen static rproc_handle_resource_t rproc_handle_rsc[] = {
731fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
732fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
733fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
734fd2c15ecSOhad Ben-Cohen 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
735e12bc14bSOhad Ben-Cohen };
736e12bc14bSOhad Ben-Cohen 
737400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
738400e64dfSOhad Ben-Cohen static int
739fd2c15ecSOhad Ben-Cohen rproc_handle_boot_rsc(struct rproc *rproc, struct resource_table *table, int len)
740400e64dfSOhad Ben-Cohen {
741400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
742e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
743fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
744400e64dfSOhad Ben-Cohen 
745fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < table->num; i++) {
746fd2c15ecSOhad Ben-Cohen 		int offset = table->offset[i];
747fd2c15ecSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)table + offset;
748fd2c15ecSOhad Ben-Cohen 		int avail = len - offset - sizeof(*hdr);
749fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
750400e64dfSOhad Ben-Cohen 
751fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
752fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
753fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
754fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
755fd2c15ecSOhad Ben-Cohen 		}
756fd2c15ecSOhad Ben-Cohen 
757fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
758fd2c15ecSOhad Ben-Cohen 
759fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
760fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
761e12bc14bSOhad Ben-Cohen 			continue;
762400e64dfSOhad Ben-Cohen 		}
763400e64dfSOhad Ben-Cohen 
764fd2c15ecSOhad Ben-Cohen 		handler = rproc_handle_rsc[hdr->type];
765e12bc14bSOhad Ben-Cohen 		if (!handler)
766e12bc14bSOhad Ben-Cohen 			continue;
767e12bc14bSOhad Ben-Cohen 
768fd2c15ecSOhad Ben-Cohen 		ret = handler(rproc, rsc, avail);
769400e64dfSOhad Ben-Cohen 		if (ret)
770400e64dfSOhad Ben-Cohen 			break;
771400e64dfSOhad Ben-Cohen 	}
772400e64dfSOhad Ben-Cohen 
773400e64dfSOhad Ben-Cohen 	return ret;
774400e64dfSOhad Ben-Cohen }
775400e64dfSOhad Ben-Cohen 
776400e64dfSOhad Ben-Cohen /* handle firmware resource entries while registering the remote processor */
777400e64dfSOhad Ben-Cohen static int
778fd2c15ecSOhad Ben-Cohen rproc_handle_virtio_rsc(struct rproc *rproc, struct resource_table *table, int len)
779400e64dfSOhad Ben-Cohen {
780400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
781fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
782400e64dfSOhad Ben-Cohen 
783fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < table->num; i++) {
784fd2c15ecSOhad Ben-Cohen 		int offset = table->offset[i];
785fd2c15ecSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)table + offset;
786fd2c15ecSOhad Ben-Cohen 		int avail = len - offset - sizeof(*hdr);
787fd2c15ecSOhad Ben-Cohen 
788fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
789fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
790fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
791fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
792fd2c15ecSOhad Ben-Cohen 		}
793fd2c15ecSOhad Ben-Cohen 
794fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "%s: rsc type %d\n", __func__, hdr->type);
795fd2c15ecSOhad Ben-Cohen 
796fd2c15ecSOhad Ben-Cohen 		if (hdr->type == RSC_VDEV) {
797fd2c15ecSOhad Ben-Cohen 			struct fw_rsc_vdev *vrsc =
798fd2c15ecSOhad Ben-Cohen 					(struct fw_rsc_vdev *)hdr->data;
799fd2c15ecSOhad Ben-Cohen 			ret = rproc_handle_early_vdev(rproc, vrsc, avail);
800400e64dfSOhad Ben-Cohen 			break;
801400e64dfSOhad Ben-Cohen 		}
802fd2c15ecSOhad Ben-Cohen 	}
803400e64dfSOhad Ben-Cohen 
804400e64dfSOhad Ben-Cohen 	return ret;
805400e64dfSOhad Ben-Cohen }
806400e64dfSOhad Ben-Cohen 
807400e64dfSOhad Ben-Cohen /**
808400e64dfSOhad Ben-Cohen  * rproc_handle_resources() - find and handle the resource table
809400e64dfSOhad Ben-Cohen  * @rproc: the rproc handle
810400e64dfSOhad Ben-Cohen  * @elf_data: the content of the ELF firmware image
8119bc91231SOhad Ben-Cohen  * @len: firmware size (in bytes)
812400e64dfSOhad Ben-Cohen  * @handler: function that should be used to handle the resource table
813400e64dfSOhad Ben-Cohen  *
814400e64dfSOhad Ben-Cohen  * This function finds the resource table inside the remote processor's
815400e64dfSOhad Ben-Cohen  * firmware, and invoke a user-supplied handler with it (we have two
816400e64dfSOhad Ben-Cohen  * possible handlers: one is invoked upon registration of @rproc,
817400e64dfSOhad Ben-Cohen  * in order to register the supported virito devices, and the other is
818400e64dfSOhad Ben-Cohen  * invoked when @rproc is actually booted).
819400e64dfSOhad Ben-Cohen  *
820400e64dfSOhad Ben-Cohen  * Currently this function fails if a resource table doesn't exist.
821400e64dfSOhad Ben-Cohen  * This restriction will be removed when we'll start supporting remote
822400e64dfSOhad Ben-Cohen  * processors that don't need a resource table.
823400e64dfSOhad Ben-Cohen  */
824400e64dfSOhad Ben-Cohen static int rproc_handle_resources(struct rproc *rproc, const u8 *elf_data,
8259bc91231SOhad Ben-Cohen 				size_t len, rproc_handle_resources_t handler)
826400e64dfSOhad Ben-Cohen 
827400e64dfSOhad Ben-Cohen {
828400e64dfSOhad Ben-Cohen 	struct elf32_hdr *ehdr;
829400e64dfSOhad Ben-Cohen 	struct elf32_shdr *shdr;
830400e64dfSOhad Ben-Cohen 	const char *name_table;
831fd2c15ecSOhad Ben-Cohen 	struct device *dev = rproc->dev;
832400e64dfSOhad Ben-Cohen 	int i, ret = -EINVAL;
833fd2c15ecSOhad Ben-Cohen 	struct resource_table *table;
834400e64dfSOhad Ben-Cohen 
835400e64dfSOhad Ben-Cohen 	ehdr = (struct elf32_hdr *)elf_data;
836400e64dfSOhad Ben-Cohen 	shdr = (struct elf32_shdr *)(elf_data + ehdr->e_shoff);
837400e64dfSOhad Ben-Cohen 	name_table = elf_data + shdr[ehdr->e_shstrndx].sh_offset;
838400e64dfSOhad Ben-Cohen 
839400e64dfSOhad Ben-Cohen 	/* look for the resource table and handle it */
840400e64dfSOhad Ben-Cohen 	for (i = 0; i < ehdr->e_shnum; i++, shdr++) {
841fd2c15ecSOhad Ben-Cohen 		int size = shdr->sh_size;
842fd2c15ecSOhad Ben-Cohen 		int offset = shdr->sh_offset;
843400e64dfSOhad Ben-Cohen 
844fd2c15ecSOhad Ben-Cohen 		if (strcmp(name_table + shdr->sh_name, ".resource_table"))
845fd2c15ecSOhad Ben-Cohen 			continue;
846fd2c15ecSOhad Ben-Cohen 
847fd2c15ecSOhad Ben-Cohen 		table = (struct resource_table *)(elf_data + offset);
848fd2c15ecSOhad Ben-Cohen 
849fd2c15ecSOhad Ben-Cohen 		/* make sure we have the entire table */
850fd2c15ecSOhad Ben-Cohen 		if (offset + size > len) {
851fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "resource table truncated\n");
852fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
853fd2c15ecSOhad Ben-Cohen 		}
854fd2c15ecSOhad Ben-Cohen 
855fd2c15ecSOhad Ben-Cohen 		/* make sure table has at least the header */
856fd2c15ecSOhad Ben-Cohen 		if (sizeof(struct resource_table) > size) {
857fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "header-less resource table\n");
858fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
859fd2c15ecSOhad Ben-Cohen 		}
860fd2c15ecSOhad Ben-Cohen 
861fd2c15ecSOhad Ben-Cohen 		/* we don't support any version beyond the first */
862fd2c15ecSOhad Ben-Cohen 		if (table->ver != 1) {
863fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "unsupported fw ver: %d\n", table->ver);
864fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
865fd2c15ecSOhad Ben-Cohen 		}
866fd2c15ecSOhad Ben-Cohen 
867fd2c15ecSOhad Ben-Cohen 		/* make sure reserved bytes are zeroes */
868fd2c15ecSOhad Ben-Cohen 		if (table->reserved[0] || table->reserved[1]) {
869fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "non zero reserved bytes\n");
870fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
871fd2c15ecSOhad Ben-Cohen 		}
872fd2c15ecSOhad Ben-Cohen 
873fd2c15ecSOhad Ben-Cohen 		/* make sure the offsets array isn't truncated */
874fd2c15ecSOhad Ben-Cohen 		if (table->num * sizeof(table->offset[0]) +
875fd2c15ecSOhad Ben-Cohen 				sizeof(struct resource_table) > size) {
876fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "resource table incomplete\n");
877fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
8789bc91231SOhad Ben-Cohen 		}
8799bc91231SOhad Ben-Cohen 
880400e64dfSOhad Ben-Cohen 		ret = handler(rproc, table, shdr->sh_size);
881400e64dfSOhad Ben-Cohen 		break;
882400e64dfSOhad Ben-Cohen 	}
883400e64dfSOhad Ben-Cohen 
884400e64dfSOhad Ben-Cohen 	return ret;
885400e64dfSOhad Ben-Cohen }
886400e64dfSOhad Ben-Cohen 
887400e64dfSOhad Ben-Cohen /**
888400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
889400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
890400e64dfSOhad Ben-Cohen  *
891400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
892400e64dfSOhad Ben-Cohen  * is called when @rproc shuts down, or just failed booting.
893400e64dfSOhad Ben-Cohen  */
894400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc)
895400e64dfSOhad Ben-Cohen {
896400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
897400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
898400e64dfSOhad Ben-Cohen 	struct rproc_vdev *rvdev = rproc->rvdev;
899400e64dfSOhad Ben-Cohen 	int i;
900400e64dfSOhad Ben-Cohen 
901400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
902400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
903400e64dfSOhad Ben-Cohen 		rproc_remove_trace_file(entry->priv);
904400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
905400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
906400e64dfSOhad Ben-Cohen 		kfree(entry);
907400e64dfSOhad Ben-Cohen 	}
908400e64dfSOhad Ben-Cohen 
909400e64dfSOhad Ben-Cohen 	/* free the coherent memory allocated for the vrings */
910400e64dfSOhad Ben-Cohen 	for (i = 0; rvdev && i < ARRAY_SIZE(rvdev->vring); i++) {
911400e64dfSOhad Ben-Cohen 		int qsz = rvdev->vring[i].len;
912400e64dfSOhad Ben-Cohen 		void *va = rvdev->vring[i].va;
913400e64dfSOhad Ben-Cohen 		int dma = rvdev->vring[i].dma;
914400e64dfSOhad Ben-Cohen 
915400e64dfSOhad Ben-Cohen 		/* virtqueue size is expressed in number of buffers supported */
916400e64dfSOhad Ben-Cohen 		if (qsz) {
917400e64dfSOhad Ben-Cohen 			/* how many bytes does this vring really occupy ? */
918400e64dfSOhad Ben-Cohen 			int size = PAGE_ALIGN(vring_size(qsz, AMP_VRING_ALIGN));
919400e64dfSOhad Ben-Cohen 
920400e64dfSOhad Ben-Cohen 			dma_free_coherent(rproc->dev, size, va, dma);
921400e64dfSOhad Ben-Cohen 
922400e64dfSOhad Ben-Cohen 			rvdev->vring[i].len = 0;
923400e64dfSOhad Ben-Cohen 		}
924400e64dfSOhad Ben-Cohen 	}
925400e64dfSOhad Ben-Cohen 
926400e64dfSOhad Ben-Cohen 	/* clean up carveout allocations */
927400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
928400e64dfSOhad Ben-Cohen 		dma_free_coherent(dev, entry->len, entry->va, entry->dma);
929400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
930400e64dfSOhad Ben-Cohen 		kfree(entry);
931400e64dfSOhad Ben-Cohen 	}
932400e64dfSOhad Ben-Cohen 
933400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
934400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
935400e64dfSOhad Ben-Cohen 		size_t unmapped;
936400e64dfSOhad Ben-Cohen 
937400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
938400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
939400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
940400e64dfSOhad Ben-Cohen 			dev_err(dev, "failed to unmap %u/%u\n", entry->len,
941400e64dfSOhad Ben-Cohen 								unmapped);
942400e64dfSOhad Ben-Cohen 		}
943400e64dfSOhad Ben-Cohen 
944400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
945400e64dfSOhad Ben-Cohen 		kfree(entry);
946400e64dfSOhad Ben-Cohen 	}
947400e64dfSOhad Ben-Cohen }
948400e64dfSOhad Ben-Cohen 
949400e64dfSOhad Ben-Cohen /* make sure this fw image is sane */
950400e64dfSOhad Ben-Cohen static int rproc_fw_sanity_check(struct rproc *rproc, const struct firmware *fw)
951400e64dfSOhad Ben-Cohen {
952400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
953400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
954400e64dfSOhad Ben-Cohen 	struct elf32_hdr *ehdr;
95540b78b2cSOhad Ben-Cohen 	char class;
956400e64dfSOhad Ben-Cohen 
957400e64dfSOhad Ben-Cohen 	if (!fw) {
958400e64dfSOhad Ben-Cohen 		dev_err(dev, "failed to load %s\n", name);
959400e64dfSOhad Ben-Cohen 		return -EINVAL;
960400e64dfSOhad Ben-Cohen 	}
961400e64dfSOhad Ben-Cohen 
962400e64dfSOhad Ben-Cohen 	if (fw->size < sizeof(struct elf32_hdr)) {
963400e64dfSOhad Ben-Cohen 		dev_err(dev, "Image is too small\n");
964400e64dfSOhad Ben-Cohen 		return -EINVAL;
965400e64dfSOhad Ben-Cohen 	}
966400e64dfSOhad Ben-Cohen 
967400e64dfSOhad Ben-Cohen 	ehdr = (struct elf32_hdr *)fw->data;
968400e64dfSOhad Ben-Cohen 
96940b78b2cSOhad Ben-Cohen 	/* We only support ELF32 at this point */
97040b78b2cSOhad Ben-Cohen 	class = ehdr->e_ident[EI_CLASS];
97140b78b2cSOhad Ben-Cohen 	if (class != ELFCLASS32) {
97240b78b2cSOhad Ben-Cohen 		dev_err(dev, "Unsupported class: %d\n", class);
97340b78b2cSOhad Ben-Cohen 		return -EINVAL;
97440b78b2cSOhad Ben-Cohen 	}
97540b78b2cSOhad Ben-Cohen 
976cf59d3e9SOhad Ben-Cohen 	/* We assume the firmware has the same endianess as the host */
977cf59d3e9SOhad Ben-Cohen # ifdef __LITTLE_ENDIAN
978cf59d3e9SOhad Ben-Cohen 	if (ehdr->e_ident[EI_DATA] != ELFDATA2LSB) {
979cf59d3e9SOhad Ben-Cohen # else /* BIG ENDIAN */
980cf59d3e9SOhad Ben-Cohen 	if (ehdr->e_ident[EI_DATA] != ELFDATA2MSB) {
981cf59d3e9SOhad Ben-Cohen # endif
982cf59d3e9SOhad Ben-Cohen 		dev_err(dev, "Unsupported firmware endianess\n");
983cf59d3e9SOhad Ben-Cohen 		return -EINVAL;
984cf59d3e9SOhad Ben-Cohen 	}
985cf59d3e9SOhad Ben-Cohen 
9869bc91231SOhad Ben-Cohen 	if (fw->size < ehdr->e_shoff + sizeof(struct elf32_shdr)) {
9879bc91231SOhad Ben-Cohen 		dev_err(dev, "Image is too small\n");
9889bc91231SOhad Ben-Cohen 		return -EINVAL;
9899bc91231SOhad Ben-Cohen 	}
9909bc91231SOhad Ben-Cohen 
991400e64dfSOhad Ben-Cohen 	if (memcmp(ehdr->e_ident, ELFMAG, SELFMAG)) {
992400e64dfSOhad Ben-Cohen 		dev_err(dev, "Image is corrupted (bad magic)\n");
993400e64dfSOhad Ben-Cohen 		return -EINVAL;
994400e64dfSOhad Ben-Cohen 	}
995400e64dfSOhad Ben-Cohen 
996400e64dfSOhad Ben-Cohen 	if (ehdr->e_phnum == 0) {
997400e64dfSOhad Ben-Cohen 		dev_err(dev, "No loadable segments\n");
998400e64dfSOhad Ben-Cohen 		return -EINVAL;
999400e64dfSOhad Ben-Cohen 	}
1000400e64dfSOhad Ben-Cohen 
1001400e64dfSOhad Ben-Cohen 	if (ehdr->e_phoff > fw->size) {
1002400e64dfSOhad Ben-Cohen 		dev_err(dev, "Firmware size is too small\n");
1003400e64dfSOhad Ben-Cohen 		return -EINVAL;
1004400e64dfSOhad Ben-Cohen 	}
1005400e64dfSOhad Ben-Cohen 
1006400e64dfSOhad Ben-Cohen 	return 0;
1007400e64dfSOhad Ben-Cohen }
1008400e64dfSOhad Ben-Cohen 
1009400e64dfSOhad Ben-Cohen /*
1010400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
1011400e64dfSOhad Ben-Cohen  */
1012400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1013400e64dfSOhad Ben-Cohen {
1014400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
1015400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
1016400e64dfSOhad Ben-Cohen 	struct elf32_hdr *ehdr;
1017400e64dfSOhad Ben-Cohen 	int ret;
1018400e64dfSOhad Ben-Cohen 
1019400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
1020400e64dfSOhad Ben-Cohen 	if (ret)
1021400e64dfSOhad Ben-Cohen 		return ret;
1022400e64dfSOhad Ben-Cohen 
1023400e64dfSOhad Ben-Cohen 	ehdr = (struct elf32_hdr *)fw->data;
1024400e64dfSOhad Ben-Cohen 
1025400e64dfSOhad Ben-Cohen 	dev_info(dev, "Booting fw image %s, size %d\n", name, fw->size);
1026400e64dfSOhad Ben-Cohen 
1027400e64dfSOhad Ben-Cohen 	/*
1028400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1029400e64dfSOhad Ben-Cohen 	 * just a nop
1030400e64dfSOhad Ben-Cohen 	 */
1031400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
1032400e64dfSOhad Ben-Cohen 	if (ret) {
1033400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
1034400e64dfSOhad Ben-Cohen 		return ret;
1035400e64dfSOhad Ben-Cohen 	}
1036400e64dfSOhad Ben-Cohen 
1037400e64dfSOhad Ben-Cohen 	/*
1038400e64dfSOhad Ben-Cohen 	 * The ELF entry point is the rproc's boot addr (though this is not
1039400e64dfSOhad Ben-Cohen 	 * a configurable property of all remote processors: some will always
1040400e64dfSOhad Ben-Cohen 	 * boot at a specific hardcoded address).
1041400e64dfSOhad Ben-Cohen 	 */
1042400e64dfSOhad Ben-Cohen 	rproc->bootaddr = ehdr->e_entry;
1043400e64dfSOhad Ben-Cohen 
1044400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
10459bc91231SOhad Ben-Cohen 	ret = rproc_handle_resources(rproc, fw->data, fw->size,
10469bc91231SOhad Ben-Cohen 						rproc_handle_boot_rsc);
1047400e64dfSOhad Ben-Cohen 	if (ret) {
1048400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
1049400e64dfSOhad Ben-Cohen 		goto clean_up;
1050400e64dfSOhad Ben-Cohen 	}
1051400e64dfSOhad Ben-Cohen 
1052400e64dfSOhad Ben-Cohen 	/* load the ELF segments to memory */
10539bc91231SOhad Ben-Cohen 	ret = rproc_load_segments(rproc, fw->data, fw->size);
1054400e64dfSOhad Ben-Cohen 	if (ret) {
1055400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to load program segments: %d\n", ret);
1056400e64dfSOhad Ben-Cohen 		goto clean_up;
1057400e64dfSOhad Ben-Cohen 	}
1058400e64dfSOhad Ben-Cohen 
1059400e64dfSOhad Ben-Cohen 	/* power up the remote processor */
1060400e64dfSOhad Ben-Cohen 	ret = rproc->ops->start(rproc);
1061400e64dfSOhad Ben-Cohen 	if (ret) {
1062400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1063400e64dfSOhad Ben-Cohen 		goto clean_up;
1064400e64dfSOhad Ben-Cohen 	}
1065400e64dfSOhad Ben-Cohen 
1066400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_RUNNING;
1067400e64dfSOhad Ben-Cohen 
1068400e64dfSOhad Ben-Cohen 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
1069400e64dfSOhad Ben-Cohen 
1070400e64dfSOhad Ben-Cohen 	return 0;
1071400e64dfSOhad Ben-Cohen 
1072400e64dfSOhad Ben-Cohen clean_up:
1073400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1074400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1075400e64dfSOhad Ben-Cohen 	return ret;
1076400e64dfSOhad Ben-Cohen }
1077400e64dfSOhad Ben-Cohen 
1078400e64dfSOhad Ben-Cohen /*
1079400e64dfSOhad Ben-Cohen  * take a firmware and look for virtio devices to register.
1080400e64dfSOhad Ben-Cohen  *
1081400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
1082400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
1083400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
1084400e64dfSOhad Ben-Cohen  * that might be cleaner).
1085400e64dfSOhad Ben-Cohen  */
1086400e64dfSOhad Ben-Cohen static void rproc_fw_config_virtio(const struct firmware *fw, void *context)
1087400e64dfSOhad Ben-Cohen {
1088400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
1089400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
1090400e64dfSOhad Ben-Cohen 	int ret;
1091400e64dfSOhad Ben-Cohen 
1092400e64dfSOhad Ben-Cohen 	if (rproc_fw_sanity_check(rproc, fw) < 0)
1093400e64dfSOhad Ben-Cohen 		goto out;
1094400e64dfSOhad Ben-Cohen 
1095fd2c15ecSOhad Ben-Cohen 	/* does the fw support any virtio devices ? */
10969bc91231SOhad Ben-Cohen 	ret = rproc_handle_resources(rproc, fw->data, fw->size,
10979bc91231SOhad Ben-Cohen 						rproc_handle_virtio_rsc);
1098400e64dfSOhad Ben-Cohen 	if (ret) {
1099400e64dfSOhad Ben-Cohen 		dev_info(dev, "No fw virtio device was found\n");
1100400e64dfSOhad Ben-Cohen 		goto out;
1101400e64dfSOhad Ben-Cohen 	}
1102400e64dfSOhad Ben-Cohen 
1103400e64dfSOhad Ben-Cohen 	/* add the virtio device (currently only rpmsg vdevs are supported) */
1104400e64dfSOhad Ben-Cohen 	ret = rproc_add_rpmsg_vdev(rproc);
1105400e64dfSOhad Ben-Cohen 	if (ret)
1106400e64dfSOhad Ben-Cohen 		goto out;
1107400e64dfSOhad Ben-Cohen 
1108400e64dfSOhad Ben-Cohen out:
1109400e64dfSOhad Ben-Cohen 	if (fw)
1110400e64dfSOhad Ben-Cohen 		release_firmware(fw);
1111400e64dfSOhad Ben-Cohen 	/* allow rproc_unregister() contexts, if any, to proceed */
1112400e64dfSOhad Ben-Cohen 	complete_all(&rproc->firmware_loading_complete);
1113400e64dfSOhad Ben-Cohen }
1114400e64dfSOhad Ben-Cohen 
1115400e64dfSOhad Ben-Cohen /**
1116400e64dfSOhad Ben-Cohen  * rproc_boot() - boot a remote processor
1117400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1118400e64dfSOhad Ben-Cohen  *
1119400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1120400e64dfSOhad Ben-Cohen  *
1121400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1122400e64dfSOhad Ben-Cohen  * returns (successfully).
1123400e64dfSOhad Ben-Cohen  *
1124400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1125400e64dfSOhad Ben-Cohen  */
1126400e64dfSOhad Ben-Cohen int rproc_boot(struct rproc *rproc)
1127400e64dfSOhad Ben-Cohen {
1128400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1129400e64dfSOhad Ben-Cohen 	struct device *dev;
1130400e64dfSOhad Ben-Cohen 	int ret;
1131400e64dfSOhad Ben-Cohen 
1132400e64dfSOhad Ben-Cohen 	if (!rproc) {
1133400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1134400e64dfSOhad Ben-Cohen 		return -EINVAL;
1135400e64dfSOhad Ben-Cohen 	}
1136400e64dfSOhad Ben-Cohen 
1137400e64dfSOhad Ben-Cohen 	dev = rproc->dev;
1138400e64dfSOhad Ben-Cohen 
1139400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1140400e64dfSOhad Ben-Cohen 	if (ret) {
1141400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1142400e64dfSOhad Ben-Cohen 		return ret;
1143400e64dfSOhad Ben-Cohen 	}
1144400e64dfSOhad Ben-Cohen 
1145400e64dfSOhad Ben-Cohen 	/* loading a firmware is required */
1146400e64dfSOhad Ben-Cohen 	if (!rproc->firmware) {
1147400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: no firmware to load\n", __func__);
1148400e64dfSOhad Ben-Cohen 		ret = -EINVAL;
1149400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1150400e64dfSOhad Ben-Cohen 	}
1151400e64dfSOhad Ben-Cohen 
1152400e64dfSOhad Ben-Cohen 	/* prevent underlying implementation from being removed */
1153400e64dfSOhad Ben-Cohen 	if (!try_module_get(dev->driver->owner)) {
1154400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: can't get owner\n", __func__);
1155400e64dfSOhad Ben-Cohen 		ret = -EINVAL;
1156400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1157400e64dfSOhad Ben-Cohen 	}
1158400e64dfSOhad Ben-Cohen 
1159400e64dfSOhad Ben-Cohen 	/* skip the boot process if rproc is already powered up */
1160400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1161400e64dfSOhad Ben-Cohen 		ret = 0;
1162400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1163400e64dfSOhad Ben-Cohen 	}
1164400e64dfSOhad Ben-Cohen 
1165400e64dfSOhad Ben-Cohen 	dev_info(dev, "powering up %s\n", rproc->name);
1166400e64dfSOhad Ben-Cohen 
1167400e64dfSOhad Ben-Cohen 	/* load firmware */
1168400e64dfSOhad Ben-Cohen 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1169400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1170400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware failed: %d\n", ret);
1171400e64dfSOhad Ben-Cohen 		goto downref_rproc;
1172400e64dfSOhad Ben-Cohen 	}
1173400e64dfSOhad Ben-Cohen 
1174400e64dfSOhad Ben-Cohen 	ret = rproc_fw_boot(rproc, firmware_p);
1175400e64dfSOhad Ben-Cohen 
1176400e64dfSOhad Ben-Cohen 	release_firmware(firmware_p);
1177400e64dfSOhad Ben-Cohen 
1178400e64dfSOhad Ben-Cohen downref_rproc:
1179400e64dfSOhad Ben-Cohen 	if (ret) {
1180400e64dfSOhad Ben-Cohen 		module_put(dev->driver->owner);
1181400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1182400e64dfSOhad Ben-Cohen 	}
1183400e64dfSOhad Ben-Cohen unlock_mutex:
1184400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1185400e64dfSOhad Ben-Cohen 	return ret;
1186400e64dfSOhad Ben-Cohen }
1187400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1188400e64dfSOhad Ben-Cohen 
1189400e64dfSOhad Ben-Cohen /**
1190400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1191400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1192400e64dfSOhad Ben-Cohen  *
1193400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1194400e64dfSOhad Ben-Cohen  *
1195400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1196400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1197400e64dfSOhad Ben-Cohen  * without really powering off the device.
1198400e64dfSOhad Ben-Cohen  *
1199400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1200400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1201400e64dfSOhad Ben-Cohen  *
1202400e64dfSOhad Ben-Cohen  * Notes:
1203400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1204400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1205400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1206400e64dfSOhad Ben-Cohen  *   needed.
1207400e64dfSOhad Ben-Cohen  * - don't call rproc_shutdown() to unroll rproc_get_by_name(), exactly
1208400e64dfSOhad Ben-Cohen  *   because rproc_shutdown() _does not_ decrement the refcount of @rproc.
1209400e64dfSOhad Ben-Cohen  *   To decrement the refcount of @rproc, use rproc_put() (but _only_ if
1210400e64dfSOhad Ben-Cohen  *   you acquired @rproc using rproc_get_by_name()).
1211400e64dfSOhad Ben-Cohen  */
1212400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1213400e64dfSOhad Ben-Cohen {
1214400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
1215400e64dfSOhad Ben-Cohen 	int ret;
1216400e64dfSOhad Ben-Cohen 
1217400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1218400e64dfSOhad Ben-Cohen 	if (ret) {
1219400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1220400e64dfSOhad Ben-Cohen 		return;
1221400e64dfSOhad Ben-Cohen 	}
1222400e64dfSOhad Ben-Cohen 
1223400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1224400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1225400e64dfSOhad Ben-Cohen 		goto out;
1226400e64dfSOhad Ben-Cohen 
1227400e64dfSOhad Ben-Cohen 	/* power off the remote processor */
1228400e64dfSOhad Ben-Cohen 	ret = rproc->ops->stop(rproc);
1229400e64dfSOhad Ben-Cohen 	if (ret) {
1230400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1231400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't stop rproc: %d\n", ret);
1232400e64dfSOhad Ben-Cohen 		goto out;
1233400e64dfSOhad Ben-Cohen 	}
1234400e64dfSOhad Ben-Cohen 
1235400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1236400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1237400e64dfSOhad Ben-Cohen 
1238400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1239400e64dfSOhad Ben-Cohen 
1240400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1241400e64dfSOhad Ben-Cohen 
1242400e64dfSOhad Ben-Cohen 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
1243400e64dfSOhad Ben-Cohen 
1244400e64dfSOhad Ben-Cohen out:
1245400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1246400e64dfSOhad Ben-Cohen 	if (!ret)
1247400e64dfSOhad Ben-Cohen 		module_put(dev->driver->owner);
1248400e64dfSOhad Ben-Cohen }
1249400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1250400e64dfSOhad Ben-Cohen 
1251400e64dfSOhad Ben-Cohen /**
1252400e64dfSOhad Ben-Cohen  * rproc_release() - completely deletes the existence of a remote processor
1253400e64dfSOhad Ben-Cohen  * @kref: the rproc's kref
1254400e64dfSOhad Ben-Cohen  *
1255400e64dfSOhad Ben-Cohen  * This function should _never_ be called directly.
1256400e64dfSOhad Ben-Cohen  *
1257400e64dfSOhad Ben-Cohen  * The only reasonable location to use it is as an argument when kref_put'ing
1258400e64dfSOhad Ben-Cohen  * @rproc's refcount.
1259400e64dfSOhad Ben-Cohen  *
1260400e64dfSOhad Ben-Cohen  * This way it will be called when no one holds a valid pointer to this @rproc
1261400e64dfSOhad Ben-Cohen  * anymore (and obviously after it is removed from the rprocs klist).
1262400e64dfSOhad Ben-Cohen  *
1263400e64dfSOhad Ben-Cohen  * Note: this function is not static because rproc_vdev_release() needs it when
1264400e64dfSOhad Ben-Cohen  * it decrements @rproc's refcount.
1265400e64dfSOhad Ben-Cohen  */
1266400e64dfSOhad Ben-Cohen void rproc_release(struct kref *kref)
1267400e64dfSOhad Ben-Cohen {
1268400e64dfSOhad Ben-Cohen 	struct rproc *rproc = container_of(kref, struct rproc, refcount);
1269400e64dfSOhad Ben-Cohen 
1270400e64dfSOhad Ben-Cohen 	dev_info(rproc->dev, "removing %s\n", rproc->name);
1271400e64dfSOhad Ben-Cohen 
1272400e64dfSOhad Ben-Cohen 	rproc_delete_debug_dir(rproc);
1273400e64dfSOhad Ben-Cohen 
1274400e64dfSOhad Ben-Cohen 	/* at this point no one holds a reference to rproc anymore */
1275400e64dfSOhad Ben-Cohen 	kfree(rproc);
1276400e64dfSOhad Ben-Cohen }
1277400e64dfSOhad Ben-Cohen 
1278400e64dfSOhad Ben-Cohen /* will be called when an rproc is added to the rprocs klist */
1279400e64dfSOhad Ben-Cohen static void klist_rproc_get(struct klist_node *n)
1280400e64dfSOhad Ben-Cohen {
1281400e64dfSOhad Ben-Cohen 	struct rproc *rproc = container_of(n, struct rproc, node);
1282400e64dfSOhad Ben-Cohen 
1283400e64dfSOhad Ben-Cohen 	kref_get(&rproc->refcount);
1284400e64dfSOhad Ben-Cohen }
1285400e64dfSOhad Ben-Cohen 
1286400e64dfSOhad Ben-Cohen /* will be called when an rproc is removed from the rprocs klist */
1287400e64dfSOhad Ben-Cohen static void klist_rproc_put(struct klist_node *n)
1288400e64dfSOhad Ben-Cohen {
1289400e64dfSOhad Ben-Cohen 	struct rproc *rproc = container_of(n, struct rproc, node);
1290400e64dfSOhad Ben-Cohen 
1291400e64dfSOhad Ben-Cohen 	kref_put(&rproc->refcount, rproc_release);
1292400e64dfSOhad Ben-Cohen }
1293400e64dfSOhad Ben-Cohen 
1294400e64dfSOhad Ben-Cohen static struct rproc *next_rproc(struct klist_iter *i)
1295400e64dfSOhad Ben-Cohen {
1296400e64dfSOhad Ben-Cohen 	struct klist_node *n;
1297400e64dfSOhad Ben-Cohen 
1298400e64dfSOhad Ben-Cohen 	n = klist_next(i);
1299400e64dfSOhad Ben-Cohen 	if (!n)
1300400e64dfSOhad Ben-Cohen 		return NULL;
1301400e64dfSOhad Ben-Cohen 
1302400e64dfSOhad Ben-Cohen 	return container_of(n, struct rproc, node);
1303400e64dfSOhad Ben-Cohen }
1304400e64dfSOhad Ben-Cohen 
1305400e64dfSOhad Ben-Cohen /**
1306400e64dfSOhad Ben-Cohen  * rproc_get_by_name() - find a remote processor by name and boot it
1307400e64dfSOhad Ben-Cohen  * @name: name of the remote processor
1308400e64dfSOhad Ben-Cohen  *
1309400e64dfSOhad Ben-Cohen  * Finds an rproc handle using the remote processor's name, and then
1310400e64dfSOhad Ben-Cohen  * boot it. If it's already powered on, then just immediately return
1311400e64dfSOhad Ben-Cohen  * (successfully).
1312400e64dfSOhad Ben-Cohen  *
1313400e64dfSOhad Ben-Cohen  * Returns the rproc handle on success, and NULL on failure.
1314400e64dfSOhad Ben-Cohen  *
1315400e64dfSOhad Ben-Cohen  * This function increments the remote processor's refcount, so always
1316400e64dfSOhad Ben-Cohen  * use rproc_put() to decrement it back once rproc isn't needed anymore.
1317400e64dfSOhad Ben-Cohen  *
1318400e64dfSOhad Ben-Cohen  * Note: currently this function (and its counterpart rproc_put()) are not
1319400e64dfSOhad Ben-Cohen  * used anymore by the rpmsg subsystem. We need to scrutinize the use cases
1320400e64dfSOhad Ben-Cohen  * that still need them, and see if we can migrate them to use the non
1321400e64dfSOhad Ben-Cohen  * name-based boot/shutdown interface.
1322400e64dfSOhad Ben-Cohen  */
1323400e64dfSOhad Ben-Cohen struct rproc *rproc_get_by_name(const char *name)
1324400e64dfSOhad Ben-Cohen {
1325400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
1326400e64dfSOhad Ben-Cohen 	struct klist_iter i;
1327400e64dfSOhad Ben-Cohen 	int ret;
1328400e64dfSOhad Ben-Cohen 
1329400e64dfSOhad Ben-Cohen 	/* find the remote processor, and upref its refcount */
1330400e64dfSOhad Ben-Cohen 	klist_iter_init(&rprocs, &i);
1331400e64dfSOhad Ben-Cohen 	while ((rproc = next_rproc(&i)) != NULL)
1332400e64dfSOhad Ben-Cohen 		if (!strcmp(rproc->name, name)) {
1333400e64dfSOhad Ben-Cohen 			kref_get(&rproc->refcount);
1334400e64dfSOhad Ben-Cohen 			break;
1335400e64dfSOhad Ben-Cohen 		}
1336400e64dfSOhad Ben-Cohen 	klist_iter_exit(&i);
1337400e64dfSOhad Ben-Cohen 
1338400e64dfSOhad Ben-Cohen 	/* can't find this rproc ? */
1339400e64dfSOhad Ben-Cohen 	if (!rproc) {
1340400e64dfSOhad Ben-Cohen 		pr_err("can't find remote processor %s\n", name);
1341400e64dfSOhad Ben-Cohen 		return NULL;
1342400e64dfSOhad Ben-Cohen 	}
1343400e64dfSOhad Ben-Cohen 
1344400e64dfSOhad Ben-Cohen 	ret = rproc_boot(rproc);
1345400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1346400e64dfSOhad Ben-Cohen 		kref_put(&rproc->refcount, rproc_release);
1347400e64dfSOhad Ben-Cohen 		return NULL;
1348400e64dfSOhad Ben-Cohen 	}
1349400e64dfSOhad Ben-Cohen 
1350400e64dfSOhad Ben-Cohen 	return rproc;
1351400e64dfSOhad Ben-Cohen }
1352400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_get_by_name);
1353400e64dfSOhad Ben-Cohen 
1354400e64dfSOhad Ben-Cohen /**
1355400e64dfSOhad Ben-Cohen  * rproc_put() - decrement the refcount of a remote processor, and shut it down
1356400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1357400e64dfSOhad Ben-Cohen  *
1358400e64dfSOhad Ben-Cohen  * This function tries to shutdown @rproc, and it then decrements its
1359400e64dfSOhad Ben-Cohen  * refcount.
1360400e64dfSOhad Ben-Cohen  *
1361400e64dfSOhad Ben-Cohen  * After this function returns, @rproc may _not_ be used anymore, and its
1362400e64dfSOhad Ben-Cohen  * handle should be considered invalid.
1363400e64dfSOhad Ben-Cohen  *
1364400e64dfSOhad Ben-Cohen  * This function should be called _iff_ the @rproc handle was grabbed by
1365400e64dfSOhad Ben-Cohen  * calling rproc_get_by_name().
1366400e64dfSOhad Ben-Cohen  */
1367400e64dfSOhad Ben-Cohen void rproc_put(struct rproc *rproc)
1368400e64dfSOhad Ben-Cohen {
1369400e64dfSOhad Ben-Cohen 	/* try to power off the remote processor */
1370400e64dfSOhad Ben-Cohen 	rproc_shutdown(rproc);
1371400e64dfSOhad Ben-Cohen 
1372400e64dfSOhad Ben-Cohen 	/* downref rproc's refcount */
1373400e64dfSOhad Ben-Cohen 	kref_put(&rproc->refcount, rproc_release);
1374400e64dfSOhad Ben-Cohen }
1375400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
1376400e64dfSOhad Ben-Cohen 
1377400e64dfSOhad Ben-Cohen /**
1378400e64dfSOhad Ben-Cohen  * rproc_register() - register a remote processor
1379400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
1380400e64dfSOhad Ben-Cohen  *
1381400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
1382400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
1383400e64dfSOhad Ben-Cohen  *
1384400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
1385400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
1386400e64dfSOhad Ben-Cohen  *
1387400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
1388400e64dfSOhad Ben-Cohen  *
1389400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
1390400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
1391400e64dfSOhad Ben-Cohen  * firmware.
1392400e64dfSOhad Ben-Cohen  *
1393400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
1394400e64dfSOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers will be
1395400e64dfSOhad Ben-Cohen  * probed.
1396400e64dfSOhad Ben-Cohen  *
1397400e64dfSOhad Ben-Cohen  * Currently, though, we only support a single RPMSG virtio vdev per remote
1398400e64dfSOhad Ben-Cohen  * processor.
1399400e64dfSOhad Ben-Cohen  */
1400400e64dfSOhad Ben-Cohen int rproc_register(struct rproc *rproc)
1401400e64dfSOhad Ben-Cohen {
1402400e64dfSOhad Ben-Cohen 	struct device *dev = rproc->dev;
1403400e64dfSOhad Ben-Cohen 	int ret = 0;
1404400e64dfSOhad Ben-Cohen 
1405400e64dfSOhad Ben-Cohen 	/* expose to rproc_get_by_name users */
1406400e64dfSOhad Ben-Cohen 	klist_add_tail(&rproc->node, &rprocs);
1407400e64dfSOhad Ben-Cohen 
1408400e64dfSOhad Ben-Cohen 	dev_info(rproc->dev, "%s is available\n", rproc->name);
1409400e64dfSOhad Ben-Cohen 
1410489d129aSOhad Ben-Cohen 	dev_info(dev, "Note: remoteproc is still under development and considered experimental.\n");
1411489d129aSOhad Ben-Cohen 	dev_info(dev, "THE BINARY FORMAT IS NOT YET FINALIZED, and backward compatibility isn't yet guaranteed.\n");
1412489d129aSOhad Ben-Cohen 
1413400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
1414400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
1415400e64dfSOhad Ben-Cohen 
1416400e64dfSOhad Ben-Cohen 	/* rproc_unregister() calls must wait until async loader completes */
1417400e64dfSOhad Ben-Cohen 	init_completion(&rproc->firmware_loading_complete);
1418400e64dfSOhad Ben-Cohen 
1419400e64dfSOhad Ben-Cohen 	/*
1420400e64dfSOhad Ben-Cohen 	 * We must retrieve early virtio configuration info from
1421400e64dfSOhad Ben-Cohen 	 * the firmware (e.g. whether to register a virtio rpmsg device,
1422400e64dfSOhad Ben-Cohen 	 * what virtio features does it support, ...).
1423400e64dfSOhad Ben-Cohen 	 *
1424400e64dfSOhad Ben-Cohen 	 * We're initiating an asynchronous firmware loading, so we can
1425400e64dfSOhad Ben-Cohen 	 * be built-in kernel code, without hanging the boot process.
1426400e64dfSOhad Ben-Cohen 	 */
1427400e64dfSOhad Ben-Cohen 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
1428400e64dfSOhad Ben-Cohen 					rproc->firmware, dev, GFP_KERNEL,
1429400e64dfSOhad Ben-Cohen 					rproc, rproc_fw_config_virtio);
1430400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1431400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware_nowait failed: %d\n", ret);
1432400e64dfSOhad Ben-Cohen 		complete_all(&rproc->firmware_loading_complete);
1433400e64dfSOhad Ben-Cohen 		klist_remove(&rproc->node);
1434400e64dfSOhad Ben-Cohen 	}
1435400e64dfSOhad Ben-Cohen 
1436400e64dfSOhad Ben-Cohen 	return ret;
1437400e64dfSOhad Ben-Cohen }
1438400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_register);
1439400e64dfSOhad Ben-Cohen 
1440400e64dfSOhad Ben-Cohen /**
1441400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
1442400e64dfSOhad Ben-Cohen  * @dev: the underlying device
1443400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
1444400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
1445400e64dfSOhad Ben-Cohen  * @firmware: name of firmware file to load
1446400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
1447400e64dfSOhad Ben-Cohen  *
1448400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
1449400e64dfSOhad Ben-Cohen  * it yet.
1450400e64dfSOhad Ben-Cohen  *
1451400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
1452400e64dfSOhad Ben-Cohen  * of the remote processor.
1453400e64dfSOhad Ben-Cohen  *
1454400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
1455400e64dfSOhad Ben-Cohen  * implementations should then call rproc_register() to complete
1456400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
1457400e64dfSOhad Ben-Cohen  *
1458400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
1459400e64dfSOhad Ben-Cohen  *
1460400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
1461400e64dfSOhad Ben-Cohen  * yet. Instead, if you just need to unroll rproc_alloc(), use rproc_free().
1462400e64dfSOhad Ben-Cohen  */
1463400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
1464400e64dfSOhad Ben-Cohen 				const struct rproc_ops *ops,
1465400e64dfSOhad Ben-Cohen 				const char *firmware, int len)
1466400e64dfSOhad Ben-Cohen {
1467400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
1468400e64dfSOhad Ben-Cohen 
1469400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
1470400e64dfSOhad Ben-Cohen 		return NULL;
1471400e64dfSOhad Ben-Cohen 
1472400e64dfSOhad Ben-Cohen 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
1473400e64dfSOhad Ben-Cohen 	if (!rproc) {
1474400e64dfSOhad Ben-Cohen 		dev_err(dev, "%s: kzalloc failed\n", __func__);
1475400e64dfSOhad Ben-Cohen 		return NULL;
1476400e64dfSOhad Ben-Cohen 	}
1477400e64dfSOhad Ben-Cohen 
1478400e64dfSOhad Ben-Cohen 	rproc->dev = dev;
1479400e64dfSOhad Ben-Cohen 	rproc->name = name;
1480400e64dfSOhad Ben-Cohen 	rproc->ops = ops;
1481400e64dfSOhad Ben-Cohen 	rproc->firmware = firmware;
1482400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
1483400e64dfSOhad Ben-Cohen 
1484400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
1485400e64dfSOhad Ben-Cohen 
1486400e64dfSOhad Ben-Cohen 	kref_init(&rproc->refcount);
1487400e64dfSOhad Ben-Cohen 
1488400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
1489400e64dfSOhad Ben-Cohen 
1490400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
1491400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
1492400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
1493400e64dfSOhad Ben-Cohen 
1494400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1495400e64dfSOhad Ben-Cohen 
1496400e64dfSOhad Ben-Cohen 	return rproc;
1497400e64dfSOhad Ben-Cohen }
1498400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
1499400e64dfSOhad Ben-Cohen 
1500400e64dfSOhad Ben-Cohen /**
1501400e64dfSOhad Ben-Cohen  * rproc_free() - free an rproc handle that was allocated by rproc_alloc
1502400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
1503400e64dfSOhad Ben-Cohen  *
1504400e64dfSOhad Ben-Cohen  * This function should _only_ be used if @rproc was only allocated,
1505400e64dfSOhad Ben-Cohen  * but not registered yet.
1506400e64dfSOhad Ben-Cohen  *
1507400e64dfSOhad Ben-Cohen  * If @rproc was already successfully registered (by calling rproc_register()),
1508400e64dfSOhad Ben-Cohen  * then use rproc_unregister() instead.
1509400e64dfSOhad Ben-Cohen  */
1510400e64dfSOhad Ben-Cohen void rproc_free(struct rproc *rproc)
1511400e64dfSOhad Ben-Cohen {
1512400e64dfSOhad Ben-Cohen 	kfree(rproc);
1513400e64dfSOhad Ben-Cohen }
1514400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_free);
1515400e64dfSOhad Ben-Cohen 
1516400e64dfSOhad Ben-Cohen /**
1517400e64dfSOhad Ben-Cohen  * rproc_unregister() - unregister a remote processor
1518400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
1519400e64dfSOhad Ben-Cohen  *
1520400e64dfSOhad Ben-Cohen  * Unregisters a remote processor, and decrements its refcount.
1521400e64dfSOhad Ben-Cohen  * If its refcount drops to zero, then @rproc will be freed. If not,
1522400e64dfSOhad Ben-Cohen  * it will be freed later once the last reference is dropped.
1523400e64dfSOhad Ben-Cohen  *
1524400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
1525400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
1526400e64dfSOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_register()
1527400e64dfSOhad Ben-Cohen  * has completed successfully.
1528400e64dfSOhad Ben-Cohen  *
1529400e64dfSOhad Ben-Cohen  * After rproc_unregister() returns, @rproc is _not_ valid anymore and
1530400e64dfSOhad Ben-Cohen  * it shouldn't be used. More specifically, don't call rproc_free()
1531400e64dfSOhad Ben-Cohen  * or try to directly free @rproc after rproc_unregister() returns;
1532400e64dfSOhad Ben-Cohen  * none of these are needed, and calling them is a bug.
1533400e64dfSOhad Ben-Cohen  *
1534400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
1535400e64dfSOhad Ben-Cohen  */
1536400e64dfSOhad Ben-Cohen int rproc_unregister(struct rproc *rproc)
1537400e64dfSOhad Ben-Cohen {
1538400e64dfSOhad Ben-Cohen 	if (!rproc)
1539400e64dfSOhad Ben-Cohen 		return -EINVAL;
1540400e64dfSOhad Ben-Cohen 
1541400e64dfSOhad Ben-Cohen 	/* if rproc is just being registered, wait */
1542400e64dfSOhad Ben-Cohen 	wait_for_completion(&rproc->firmware_loading_complete);
1543400e64dfSOhad Ben-Cohen 
1544400e64dfSOhad Ben-Cohen 	/* was an rpmsg vdev created ? */
1545400e64dfSOhad Ben-Cohen 	if (rproc->rvdev)
1546400e64dfSOhad Ben-Cohen 		rproc_remove_rpmsg_vdev(rproc);
1547400e64dfSOhad Ben-Cohen 
1548400e64dfSOhad Ben-Cohen 	klist_remove(&rproc->node);
1549400e64dfSOhad Ben-Cohen 
1550400e64dfSOhad Ben-Cohen 	kref_put(&rproc->refcount, rproc_release);
1551400e64dfSOhad Ben-Cohen 
1552400e64dfSOhad Ben-Cohen 	return 0;
1553400e64dfSOhad Ben-Cohen }
1554400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_unregister);
1555400e64dfSOhad Ben-Cohen 
1556400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
1557400e64dfSOhad Ben-Cohen {
1558400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
1559400e64dfSOhad Ben-Cohen 	return 0;
1560400e64dfSOhad Ben-Cohen }
1561400e64dfSOhad Ben-Cohen module_init(remoteproc_init);
1562400e64dfSOhad Ben-Cohen 
1563400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
1564400e64dfSOhad Ben-Cohen {
1565400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
1566400e64dfSOhad Ben-Cohen }
1567400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
1568400e64dfSOhad Ben-Cohen 
1569400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
1570400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
1571