xref: /linux/drivers/remoteproc/remoteproc_core.c (revision dc5192c449368eed3385f4405670aa3ed21d6270)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2400e64dfSOhad Ben-Cohen /*
3400e64dfSOhad Ben-Cohen  * Remote Processor Framework
4400e64dfSOhad Ben-Cohen  *
5400e64dfSOhad Ben-Cohen  * Copyright (C) 2011 Texas Instruments, Inc.
6400e64dfSOhad Ben-Cohen  * Copyright (C) 2011 Google, Inc.
7400e64dfSOhad Ben-Cohen  *
8400e64dfSOhad Ben-Cohen  * Ohad Ben-Cohen <ohad@wizery.com>
9400e64dfSOhad Ben-Cohen  * Brian Swetland <swetland@google.com>
10400e64dfSOhad Ben-Cohen  * Mark Grosen <mgrosen@ti.com>
11400e64dfSOhad Ben-Cohen  * Fernando Guzman Lugo <fernando.lugo@ti.com>
12400e64dfSOhad Ben-Cohen  * Suman Anna <s-anna@ti.com>
13400e64dfSOhad Ben-Cohen  * Robert Tivy <rtivy@ti.com>
14400e64dfSOhad Ben-Cohen  * Armando Uribe De Leon <x0095078@ti.com>
15400e64dfSOhad Ben-Cohen  */
16400e64dfSOhad Ben-Cohen 
17400e64dfSOhad Ben-Cohen #define pr_fmt(fmt)    "%s: " fmt, __func__
18400e64dfSOhad Ben-Cohen 
19*dc5192c4SBjorn Andersson #include <linux/delay.h>
20400e64dfSOhad Ben-Cohen #include <linux/kernel.h>
21400e64dfSOhad Ben-Cohen #include <linux/module.h>
22400e64dfSOhad Ben-Cohen #include <linux/device.h>
23400e64dfSOhad Ben-Cohen #include <linux/slab.h>
24400e64dfSOhad Ben-Cohen #include <linux/mutex.h>
25400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h>
26400e64dfSOhad Ben-Cohen #include <linux/firmware.h>
27400e64dfSOhad Ben-Cohen #include <linux/string.h>
28400e64dfSOhad Ben-Cohen #include <linux/debugfs.h>
292666ca91SSarangdhar Joshi #include <linux/devcoredump.h>
30c0abe2caSBjorn Andersson #include <linux/rculist.h>
31400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h>
32400e64dfSOhad Ben-Cohen #include <linux/iommu.h>
33b5ab5e24SOhad Ben-Cohen #include <linux/idr.h>
34400e64dfSOhad Ben-Cohen #include <linux/elf.h>
35a2b950acSOhad Ben-Cohen #include <linux/crc32.h>
36086d0872SLoic Pallardy #include <linux/of_reserved_mem.h>
37400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h>
38400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h>
39cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h>
40086d0872SLoic Pallardy #include <linux/platform_device.h>
41400e64dfSOhad Ben-Cohen 
42400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h"
438f403350SClement Leger #include "remoteproc_elf_helpers.h"
44400e64dfSOhad Ben-Cohen 
45b36de8cfSLoic Pallardy #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL
46b36de8cfSLoic Pallardy 
47fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex);
48fec47d86SDave Gerlach static LIST_HEAD(rproc_list);
49*dc5192c4SBjorn Andersson static struct notifier_block rproc_panic_nb;
50fec47d86SDave Gerlach 
51a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
52a2b950acSOhad Ben-Cohen 				 void *, int offset, int avail);
53400e64dfSOhad Ben-Cohen 
54c6aed238SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc,
55c6aed238SLoic Pallardy 				struct rproc_mem_entry *mem);
56c6aed238SLoic Pallardy static int rproc_release_carveout(struct rproc *rproc,
57c6aed238SLoic Pallardy 				  struct rproc_mem_entry *mem);
58c6aed238SLoic Pallardy 
59b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */
60b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index);
61b5ab5e24SOhad Ben-Cohen 
628afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = {
638afd519cSFernando Guzman Lugo 	[RPROC_MMUFAULT]	= "mmufault",
64b3d39032SBjorn Andersson 	[RPROC_WATCHDOG]	= "watchdog",
65b3d39032SBjorn Andersson 	[RPROC_FATAL_ERROR]	= "fatal error",
668afd519cSFernando Guzman Lugo };
678afd519cSFernando Guzman Lugo 
688afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */
698afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type)
708afd519cSFernando Guzman Lugo {
718afd519cSFernando Guzman Lugo 	if (type < ARRAY_SIZE(rproc_crash_names))
728afd519cSFernando Guzman Lugo 		return rproc_crash_names[type];
73b23f7a09SMasanari Iida 	return "unknown";
748afd519cSFernando Guzman Lugo }
758afd519cSFernando Guzman Lugo 
76400e64dfSOhad Ben-Cohen /*
77400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
78400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
79400e64dfSOhad Ben-Cohen  * an IOMMU).
80400e64dfSOhad Ben-Cohen  *
81400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
82400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
83400e64dfSOhad Ben-Cohen  */
84400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
8577ca2332SOhad Ben-Cohen 			     unsigned long iova, int flags, void *token)
86400e64dfSOhad Ben-Cohen {
878afd519cSFernando Guzman Lugo 	struct rproc *rproc = token;
888afd519cSFernando Guzman Lugo 
89400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
90400e64dfSOhad Ben-Cohen 
918afd519cSFernando Guzman Lugo 	rproc_report_crash(rproc, RPROC_MMUFAULT);
928afd519cSFernando Guzman Lugo 
93400e64dfSOhad Ben-Cohen 	/*
94400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
958afd519cSFernando Guzman Lugo 	 * we just used it as a recovery trigger.
96400e64dfSOhad Ben-Cohen 	 */
97400e64dfSOhad Ben-Cohen 	return -ENOSYS;
98400e64dfSOhad Ben-Cohen }
99400e64dfSOhad Ben-Cohen 
100400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
101400e64dfSOhad Ben-Cohen {
102400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
103b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
104400e64dfSOhad Ben-Cohen 	int ret;
105400e64dfSOhad Ben-Cohen 
106315491e5SSuman Anna 	if (!rproc->has_iommu) {
107315491e5SSuman Anna 		dev_dbg(dev, "iommu not present\n");
1080798e1daSMark Grosen 		return 0;
109400e64dfSOhad Ben-Cohen 	}
110400e64dfSOhad Ben-Cohen 
111400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
112400e64dfSOhad Ben-Cohen 	if (!domain) {
113400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
114400e64dfSOhad Ben-Cohen 		return -ENOMEM;
115400e64dfSOhad Ben-Cohen 	}
116400e64dfSOhad Ben-Cohen 
11777ca2332SOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
118400e64dfSOhad Ben-Cohen 
119400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
120400e64dfSOhad Ben-Cohen 	if (ret) {
121400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
122400e64dfSOhad Ben-Cohen 		goto free_domain;
123400e64dfSOhad Ben-Cohen 	}
124400e64dfSOhad Ben-Cohen 
125400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
126400e64dfSOhad Ben-Cohen 
127400e64dfSOhad Ben-Cohen 	return 0;
128400e64dfSOhad Ben-Cohen 
129400e64dfSOhad Ben-Cohen free_domain:
130400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
131400e64dfSOhad Ben-Cohen 	return ret;
132400e64dfSOhad Ben-Cohen }
133400e64dfSOhad Ben-Cohen 
134400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
135400e64dfSOhad Ben-Cohen {
136400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
137b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
138400e64dfSOhad Ben-Cohen 
139400e64dfSOhad Ben-Cohen 	if (!domain)
140400e64dfSOhad Ben-Cohen 		return;
141400e64dfSOhad Ben-Cohen 
142400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
143400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
144400e64dfSOhad Ben-Cohen }
145400e64dfSOhad Ben-Cohen 
146086d0872SLoic Pallardy phys_addr_t rproc_va_to_pa(void *cpu_addr)
147eb30596eSLoic Pallardy {
148eb30596eSLoic Pallardy 	/*
149eb30596eSLoic Pallardy 	 * Return physical address according to virtual address location
150eb30596eSLoic Pallardy 	 * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent
151eb30596eSLoic Pallardy 	 * - in kernel: if region allocated in generic dma memory pool
152eb30596eSLoic Pallardy 	 */
153eb30596eSLoic Pallardy 	if (is_vmalloc_addr(cpu_addr)) {
154eb30596eSLoic Pallardy 		return page_to_phys(vmalloc_to_page(cpu_addr)) +
155eb30596eSLoic Pallardy 				    offset_in_page(cpu_addr);
156eb30596eSLoic Pallardy 	}
157eb30596eSLoic Pallardy 
158eb30596eSLoic Pallardy 	WARN_ON(!virt_addr_valid(cpu_addr));
159eb30596eSLoic Pallardy 	return virt_to_phys(cpu_addr);
160eb30596eSLoic Pallardy }
161086d0872SLoic Pallardy EXPORT_SYMBOL(rproc_va_to_pa);
162eb30596eSLoic Pallardy 
163a01f7cd6SSuman Anna /**
164a01f7cd6SSuman Anna  * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
165a01f7cd6SSuman Anna  * @rproc: handle of a remote processor
166a01f7cd6SSuman Anna  * @da: remoteproc device address to translate
167a01f7cd6SSuman Anna  * @len: length of the memory region @da is pointing to
168a01f7cd6SSuman Anna  *
169400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
170400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
171a01f7cd6SSuman Anna  * device addresses (which are hardcoded in the firmware). They may also have
172a01f7cd6SSuman Anna  * dedicated memory regions internal to the processors, and use them either
173a01f7cd6SSuman Anna  * exclusively or alongside carveouts.
174400e64dfSOhad Ben-Cohen  *
175400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
176400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
177400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
178400e64dfSOhad Ben-Cohen  *
179a01f7cd6SSuman Anna  * This function is a helper function with which we can go over the allocated
180a01f7cd6SSuman Anna  * carveouts and translate specific device addresses to kernel virtual addresses
181a01f7cd6SSuman Anna  * so we can access the referenced memory. This function also allows to perform
182a01f7cd6SSuman Anna  * translations on the internal remoteproc memory regions through a platform
183a01f7cd6SSuman Anna  * implementation specific da_to_va ops, if present.
184a01f7cd6SSuman Anna  *
185a01f7cd6SSuman Anna  * The function returns a valid kernel address on success or NULL on failure.
186400e64dfSOhad Ben-Cohen  *
187400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
188400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
189a01f7cd6SSuman Anna  * here the output of the DMA API for the carveouts, which should be more
190a01f7cd6SSuman Anna  * correct.
191400e64dfSOhad Ben-Cohen  */
1929ce3bf22SClement Leger void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len)
193400e64dfSOhad Ben-Cohen {
194400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
195400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
196400e64dfSOhad Ben-Cohen 
197a01f7cd6SSuman Anna 	if (rproc->ops->da_to_va) {
198a01f7cd6SSuman Anna 		ptr = rproc->ops->da_to_va(rproc, da, len);
199a01f7cd6SSuman Anna 		if (ptr)
200a01f7cd6SSuman Anna 			goto out;
201a01f7cd6SSuman Anna 	}
202a01f7cd6SSuman Anna 
203400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
204400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
205400e64dfSOhad Ben-Cohen 
20674457c40SLoic Pallardy 		/*  Verify that carveout is allocated */
20774457c40SLoic Pallardy 		if (!carveout->va)
20874457c40SLoic Pallardy 			continue;
20974457c40SLoic Pallardy 
210400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
211400e64dfSOhad Ben-Cohen 		if (offset < 0)
212400e64dfSOhad Ben-Cohen 			continue;
213400e64dfSOhad Ben-Cohen 
214400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
215400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
216400e64dfSOhad Ben-Cohen 			continue;
217400e64dfSOhad Ben-Cohen 
218400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
219400e64dfSOhad Ben-Cohen 
220400e64dfSOhad Ben-Cohen 		break;
221400e64dfSOhad Ben-Cohen 	}
222400e64dfSOhad Ben-Cohen 
223a01f7cd6SSuman Anna out:
224400e64dfSOhad Ben-Cohen 	return ptr;
225400e64dfSOhad Ben-Cohen }
2264afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
227400e64dfSOhad Ben-Cohen 
228b0019ccdSLoic Pallardy /**
229b0019ccdSLoic Pallardy  * rproc_find_carveout_by_name() - lookup the carveout region by a name
230b0019ccdSLoic Pallardy  * @rproc: handle of a remote processor
2312e7d4c2cSArnaud Pouliquen  * @name: carveout name to find (format string)
2322e7d4c2cSArnaud Pouliquen  * @...: optional parameters matching @name string
233b0019ccdSLoic Pallardy  *
234b0019ccdSLoic Pallardy  * Platform driver has the capability to register some pre-allacoted carveout
235b0019ccdSLoic Pallardy  * (physically contiguous memory regions) before rproc firmware loading and
236b0019ccdSLoic Pallardy  * associated resource table analysis. These regions may be dedicated memory
237b0019ccdSLoic Pallardy  * regions internal to the coprocessor or specified DDR region with specific
238b0019ccdSLoic Pallardy  * attributes
239b0019ccdSLoic Pallardy  *
240b0019ccdSLoic Pallardy  * This function is a helper function with which we can go over the
241b0019ccdSLoic Pallardy  * allocated carveouts and return associated region characteristics like
242b0019ccdSLoic Pallardy  * coprocessor address, length or processor virtual address.
243b0019ccdSLoic Pallardy  *
244b0019ccdSLoic Pallardy  * Return: a valid pointer on carveout entry on success or NULL on failure.
245b0019ccdSLoic Pallardy  */
246b0019ccdSLoic Pallardy struct rproc_mem_entry *
247b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
248b0019ccdSLoic Pallardy {
249b0019ccdSLoic Pallardy 	va_list args;
250b0019ccdSLoic Pallardy 	char _name[32];
251b0019ccdSLoic Pallardy 	struct rproc_mem_entry *carveout, *mem = NULL;
252b0019ccdSLoic Pallardy 
253b0019ccdSLoic Pallardy 	if (!name)
254b0019ccdSLoic Pallardy 		return NULL;
255b0019ccdSLoic Pallardy 
256b0019ccdSLoic Pallardy 	va_start(args, name);
257b0019ccdSLoic Pallardy 	vsnprintf(_name, sizeof(_name), name, args);
258b0019ccdSLoic Pallardy 	va_end(args);
259b0019ccdSLoic Pallardy 
260b0019ccdSLoic Pallardy 	list_for_each_entry(carveout, &rproc->carveouts, node) {
261b0019ccdSLoic Pallardy 		/* Compare carveout and requested names */
262b0019ccdSLoic Pallardy 		if (!strcmp(carveout->name, _name)) {
263b0019ccdSLoic Pallardy 			mem = carveout;
264b0019ccdSLoic Pallardy 			break;
265b0019ccdSLoic Pallardy 		}
266b0019ccdSLoic Pallardy 	}
267b0019ccdSLoic Pallardy 
268b0019ccdSLoic Pallardy 	return mem;
269b0019ccdSLoic Pallardy }
270b0019ccdSLoic Pallardy 
271c874bf59SLoic Pallardy /**
272c874bf59SLoic Pallardy  * rproc_check_carveout_da() - Check specified carveout da configuration
273c874bf59SLoic Pallardy  * @rproc: handle of a remote processor
274c874bf59SLoic Pallardy  * @mem: pointer on carveout to check
275c874bf59SLoic Pallardy  * @da: area device address
276c874bf59SLoic Pallardy  * @len: associated area size
277c874bf59SLoic Pallardy  *
278c874bf59SLoic Pallardy  * This function is a helper function to verify requested device area (couple
27928d7d5c6SLoic Pallardy  * da, len) is part of specified carveout.
28028d7d5c6SLoic Pallardy  * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is
28128d7d5c6SLoic Pallardy  * checked.
282c874bf59SLoic Pallardy  *
28328d7d5c6SLoic Pallardy  * Return: 0 if carveout matches request else error
284c874bf59SLoic Pallardy  */
28528d7d5c6SLoic Pallardy static int rproc_check_carveout_da(struct rproc *rproc,
28628d7d5c6SLoic Pallardy 				   struct rproc_mem_entry *mem, u32 da, u32 len)
287c874bf59SLoic Pallardy {
288c874bf59SLoic Pallardy 	struct device *dev = &rproc->dev;
28928d7d5c6SLoic Pallardy 	int delta;
290c874bf59SLoic Pallardy 
291c874bf59SLoic Pallardy 	/* Check requested resource length */
292c874bf59SLoic Pallardy 	if (len > mem->len) {
293c874bf59SLoic Pallardy 		dev_err(dev, "Registered carveout doesn't fit len request\n");
29428d7d5c6SLoic Pallardy 		return -EINVAL;
295c874bf59SLoic Pallardy 	}
296c874bf59SLoic Pallardy 
297c874bf59SLoic Pallardy 	if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) {
29828d7d5c6SLoic Pallardy 		/* Address doesn't match registered carveout configuration */
29928d7d5c6SLoic Pallardy 		return -EINVAL;
300c874bf59SLoic Pallardy 	} else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) {
301c874bf59SLoic Pallardy 		delta = da - mem->da;
302c874bf59SLoic Pallardy 
303c874bf59SLoic Pallardy 		/* Check requested resource belongs to registered carveout */
304c874bf59SLoic Pallardy 		if (delta < 0) {
305c874bf59SLoic Pallardy 			dev_err(dev,
306c874bf59SLoic Pallardy 				"Registered carveout doesn't fit da request\n");
30728d7d5c6SLoic Pallardy 			return -EINVAL;
308c874bf59SLoic Pallardy 		}
309c874bf59SLoic Pallardy 
310c874bf59SLoic Pallardy 		if (delta + len > mem->len) {
311c874bf59SLoic Pallardy 			dev_err(dev,
312c874bf59SLoic Pallardy 				"Registered carveout doesn't fit len request\n");
31328d7d5c6SLoic Pallardy 			return -EINVAL;
314c874bf59SLoic Pallardy 		}
315c874bf59SLoic Pallardy 	}
316c874bf59SLoic Pallardy 
317c874bf59SLoic Pallardy 	return 0;
318c874bf59SLoic Pallardy }
319c874bf59SLoic Pallardy 
3206db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
321400e64dfSOhad Ben-Cohen {
3227a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
323b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3246db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
325c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
326096ee786SClement Leger 	int ret, notifyid;
327c6aed238SLoic Pallardy 	struct rproc_mem_entry *mem;
328096ee786SClement Leger 	size_t size;
329400e64dfSOhad Ben-Cohen 
3306db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
3316db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
3326db20ea8SOhad Ben-Cohen 
333c6aed238SLoic Pallardy 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
334c6aed238SLoic Pallardy 
335c6aed238SLoic Pallardy 	/* Search for pre-registered carveout */
336c6aed238SLoic Pallardy 	mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
337c6aed238SLoic Pallardy 					  i);
338c6aed238SLoic Pallardy 	if (mem) {
339c6aed238SLoic Pallardy 		if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size))
340c6aed238SLoic Pallardy 			return -ENOMEM;
341c6aed238SLoic Pallardy 	} else {
342c6aed238SLoic Pallardy 		/* Register carveout in in list */
34399cf0361SBen Dooks (Codethink) 		mem = rproc_mem_entry_init(dev, NULL, 0,
34499cf0361SBen Dooks (Codethink) 					   size, rsc->vring[i].da,
345c6aed238SLoic Pallardy 					   rproc_alloc_carveout,
346c6aed238SLoic Pallardy 					   rproc_release_carveout,
347c6aed238SLoic Pallardy 					   "vdev%dvring%d",
348c6aed238SLoic Pallardy 					   rvdev->index, i);
349c6aed238SLoic Pallardy 		if (!mem) {
350c6aed238SLoic Pallardy 			dev_err(dev, "Can't allocate memory entry structure\n");
351c6aed238SLoic Pallardy 			return -ENOMEM;
352c6aed238SLoic Pallardy 		}
353c6aed238SLoic Pallardy 
354c6aed238SLoic Pallardy 		rproc_add_carveout(rproc, mem);
3556db20ea8SOhad Ben-Cohen 	}
3566db20ea8SOhad Ben-Cohen 
3576db20ea8SOhad Ben-Cohen 	/*
3586db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
3596db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
3606db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
3616db20ea8SOhad Ben-Cohen 	 */
36215fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
363b39599b7SSuman Anna 	if (ret < 0) {
36415fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
3656db20ea8SOhad Ben-Cohen 		return ret;
3666db20ea8SOhad Ben-Cohen 	}
36715fc6110STejun Heo 	notifyid = ret;
3686db20ea8SOhad Ben-Cohen 
36948f18f89SBjorn Andersson 	/* Potentially bump max_notifyid */
37048f18f89SBjorn Andersson 	if (notifyid > rproc->max_notifyid)
37148f18f89SBjorn Andersson 		rproc->max_notifyid = notifyid;
37248f18f89SBjorn Andersson 
3736db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
3746db20ea8SOhad Ben-Cohen 
375c6aed238SLoic Pallardy 	/* Let the rproc know the notifyid of this vring.*/
376c0d63157SSjur Brændeland 	rsc->vring[i].notifyid = notifyid;
3776db20ea8SOhad Ben-Cohen 	return 0;
3786db20ea8SOhad Ben-Cohen }
3796db20ea8SOhad Ben-Cohen 
380400e64dfSOhad Ben-Cohen static int
3816db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
382400e64dfSOhad Ben-Cohen {
383400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
384b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
385400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
3866db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
387400e64dfSOhad Ben-Cohen 
3889d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
3897a186941SOhad Ben-Cohen 		i, vring->da, vring->num, vring->align);
3907a186941SOhad Ben-Cohen 
39163140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
39263140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
39363140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
39463140e0eSOhad Ben-Cohen 			vring->num, vring->align);
395400e64dfSOhad Ben-Cohen 		return -EINVAL;
396400e64dfSOhad Ben-Cohen 	}
397400e64dfSOhad Ben-Cohen 
3986db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
3996db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
4006db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
401400e64dfSOhad Ben-Cohen 
402400e64dfSOhad Ben-Cohen 	return 0;
403400e64dfSOhad Ben-Cohen }
404400e64dfSOhad Ben-Cohen 
4056db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
4067a186941SOhad Ben-Cohen {
4076db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
40800a0eec5SClement Leger 	int idx = rvring - rvring->rvdev->vring;
409c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
4107a186941SOhad Ben-Cohen 
4117a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
412099a3f33SSjur Brændeland 
413c0d63157SSjur Brændeland 	/* reset resource entry info */
414c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
415c0d63157SSjur Brændeland 	rsc->vring[idx].da = 0;
416c0d63157SSjur Brændeland 	rsc->vring[idx].notifyid = -1;
4177a186941SOhad Ben-Cohen }
4187a186941SOhad Ben-Cohen 
4196f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev)
420f5bcb353SBjorn Andersson {
421f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
422f5bcb353SBjorn Andersson 
423f5bcb353SBjorn Andersson 	return rproc_add_virtio_dev(rvdev, rvdev->id);
424f5bcb353SBjorn Andersson }
425f5bcb353SBjorn Andersson 
4266f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
427f5bcb353SBjorn Andersson {
428f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
429d4c036feSLoic Pallardy 	int ret;
430f5bcb353SBjorn Andersson 
431d4c036feSLoic Pallardy 	ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev);
432d4c036feSLoic Pallardy 	if (ret)
433d4c036feSLoic Pallardy 		dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret);
434f5bcb353SBjorn Andersson }
435f5bcb353SBjorn Andersson 
436400e64dfSOhad Ben-Cohen /**
437086d0872SLoic Pallardy  * rproc_rvdev_release() - release the existence of a rvdev
438086d0872SLoic Pallardy  *
439086d0872SLoic Pallardy  * @dev: the subdevice's dev
440086d0872SLoic Pallardy  */
441086d0872SLoic Pallardy static void rproc_rvdev_release(struct device *dev)
442086d0872SLoic Pallardy {
443086d0872SLoic Pallardy 	struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev);
444086d0872SLoic Pallardy 
445086d0872SLoic Pallardy 	of_reserved_mem_device_release(dev);
446086d0872SLoic Pallardy 
447086d0872SLoic Pallardy 	kfree(rvdev);
448086d0872SLoic Pallardy }
449086d0872SLoic Pallardy 
450086d0872SLoic Pallardy /**
451fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
452400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
453400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
4542e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
455fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
456400e64dfSOhad Ben-Cohen  *
4577a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
4587a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
4597a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
4607a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
461400e64dfSOhad Ben-Cohen  *
4627a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
4637a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
4647a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
4657a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
4667a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
467400e64dfSOhad Ben-Cohen  *
4687a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
4697a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
4707a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
4717a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
4727a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
4737a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
4747a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
475400e64dfSOhad Ben-Cohen  *
476400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
477400e64dfSOhad Ben-Cohen  */
478fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
479a2b950acSOhad Ben-Cohen 			     int offset, int avail)
480400e64dfSOhad Ben-Cohen {
481b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
4827a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
4837a186941SOhad Ben-Cohen 	int i, ret;
484086d0872SLoic Pallardy 	char name[16];
485fd2c15ecSOhad Ben-Cohen 
486fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
487c8784657SGustavo A. R. Silva 	if (struct_size(rsc, vring, rsc->num_of_vrings) + rsc->config_len >
488c8784657SGustavo A. R. Silva 			avail) {
489b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
490fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
491fd2c15ecSOhad Ben-Cohen 	}
492fd2c15ecSOhad Ben-Cohen 
493fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
494fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
495fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
496fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
497fd2c15ecSOhad Ben-Cohen 	}
498fd2c15ecSOhad Ben-Cohen 
4999d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
500fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
501400e64dfSOhad Ben-Cohen 
5027a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
5037a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
504fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
505400e64dfSOhad Ben-Cohen 		return -EINVAL;
506400e64dfSOhad Ben-Cohen 	}
507400e64dfSOhad Ben-Cohen 
508899585adSAnna, Suman 	rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
5097a186941SOhad Ben-Cohen 	if (!rvdev)
5107a186941SOhad Ben-Cohen 		return -ENOMEM;
5117a186941SOhad Ben-Cohen 
512aab8d802SBjorn Andersson 	kref_init(&rvdev->refcount);
513aab8d802SBjorn Andersson 
514f5bcb353SBjorn Andersson 	rvdev->id = rsc->id;
5157a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
516c6aed238SLoic Pallardy 	rvdev->index = rproc->nb_vdev++;
5177a186941SOhad Ben-Cohen 
518086d0872SLoic Pallardy 	/* Initialise vdev subdevice */
519086d0872SLoic Pallardy 	snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index);
520086d0872SLoic Pallardy 	rvdev->dev.parent = rproc->dev.parent;
52172f64cabSClement Leger 	rvdev->dev.dma_pfn_offset = rproc->dev.parent->dma_pfn_offset;
522086d0872SLoic Pallardy 	rvdev->dev.release = rproc_rvdev_release;
523086d0872SLoic Pallardy 	dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name);
524086d0872SLoic Pallardy 	dev_set_drvdata(&rvdev->dev, rvdev);
525086d0872SLoic Pallardy 
526086d0872SLoic Pallardy 	ret = device_register(&rvdev->dev);
527086d0872SLoic Pallardy 	if (ret) {
528086d0872SLoic Pallardy 		put_device(&rvdev->dev);
529086d0872SLoic Pallardy 		return ret;
530086d0872SLoic Pallardy 	}
531086d0872SLoic Pallardy 	/* Make device dma capable by inheriting from parent's capabilities */
532086d0872SLoic Pallardy 	set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent));
533086d0872SLoic Pallardy 
534086d0872SLoic Pallardy 	ret = dma_coerce_mask_and_coherent(&rvdev->dev,
535086d0872SLoic Pallardy 					   dma_get_mask(rproc->dev.parent));
536086d0872SLoic Pallardy 	if (ret) {
537086d0872SLoic Pallardy 		dev_warn(dev,
538086d0872SLoic Pallardy 			 "Failed to set DMA mask %llx. Trying to continue... %x\n",
539086d0872SLoic Pallardy 			 dma_get_mask(rproc->dev.parent), ret);
540086d0872SLoic Pallardy 	}
541086d0872SLoic Pallardy 
5426db20ea8SOhad Ben-Cohen 	/* parse the vrings */
543fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
5446db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
5457a186941SOhad Ben-Cohen 		if (ret)
5466db20ea8SOhad Ben-Cohen 			goto free_rvdev;
547fd2c15ecSOhad Ben-Cohen 	}
548fd2c15ecSOhad Ben-Cohen 
549a2b950acSOhad Ben-Cohen 	/* remember the resource offset*/
550a2b950acSOhad Ben-Cohen 	rvdev->rsc_offset = offset;
551400e64dfSOhad Ben-Cohen 
552a863af5dSBjorn Andersson 	/* allocate the vring resources */
553a863af5dSBjorn Andersson 	for (i = 0; i < rsc->num_of_vrings; i++) {
554a863af5dSBjorn Andersson 		ret = rproc_alloc_vring(rvdev, i);
555a863af5dSBjorn Andersson 		if (ret)
556a863af5dSBjorn Andersson 			goto unwind_vring_allocations;
557a863af5dSBjorn Andersson 	}
558a863af5dSBjorn Andersson 
5597a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
560400e64dfSOhad Ben-Cohen 
5616f8b0373SAlex Elder 	rvdev->subdev.start = rproc_vdev_do_start;
5626f8b0373SAlex Elder 	rvdev->subdev.stop = rproc_vdev_do_stop;
5634902676fSBjorn Andersson 
5644902676fSBjorn Andersson 	rproc_add_subdev(rproc, &rvdev->subdev);
565400e64dfSOhad Ben-Cohen 
566400e64dfSOhad Ben-Cohen 	return 0;
5677a186941SOhad Ben-Cohen 
568a863af5dSBjorn Andersson unwind_vring_allocations:
569a863af5dSBjorn Andersson 	for (i--; i >= 0; i--)
570a863af5dSBjorn Andersson 		rproc_free_vring(&rvdev->vring[i]);
5716db20ea8SOhad Ben-Cohen free_rvdev:
572086d0872SLoic Pallardy 	device_unregister(&rvdev->dev);
5737a186941SOhad Ben-Cohen 	return ret;
574400e64dfSOhad Ben-Cohen }
575400e64dfSOhad Ben-Cohen 
576aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref)
577aab8d802SBjorn Andersson {
578aab8d802SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
579a863af5dSBjorn Andersson 	struct rproc_vring *rvring;
580f5bcb353SBjorn Andersson 	struct rproc *rproc = rvdev->rproc;
581a863af5dSBjorn Andersson 	int id;
582a863af5dSBjorn Andersson 
583a863af5dSBjorn Andersson 	for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
584a863af5dSBjorn Andersson 		rvring = &rvdev->vring[id];
585a863af5dSBjorn Andersson 		rproc_free_vring(rvring);
586a863af5dSBjorn Andersson 	}
587aab8d802SBjorn Andersson 
588f5bcb353SBjorn Andersson 	rproc_remove_subdev(rproc, &rvdev->subdev);
589aab8d802SBjorn Andersson 	list_del(&rvdev->node);
590086d0872SLoic Pallardy 	device_unregister(&rvdev->dev);
591aab8d802SBjorn Andersson }
592aab8d802SBjorn Andersson 
593400e64dfSOhad Ben-Cohen /**
594400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
595400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
596400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
5972e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
598fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
599400e64dfSOhad Ben-Cohen  *
600400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
601400e64dfSOhad Ben-Cohen  * export it via debugfs.
602400e64dfSOhad Ben-Cohen  *
603400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
604400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
605400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
606400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
607400e64dfSOhad Ben-Cohen  *
608400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
609400e64dfSOhad Ben-Cohen  */
610fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
611a2b950acSOhad Ben-Cohen 			      int offset, int avail)
612400e64dfSOhad Ben-Cohen {
613a987e6b9SLoic Pallardy 	struct rproc_debug_trace *trace;
614b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
615400e64dfSOhad Ben-Cohen 	char name[15];
616400e64dfSOhad Ben-Cohen 
617fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
618b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
619fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
620fd2c15ecSOhad Ben-Cohen 	}
621fd2c15ecSOhad Ben-Cohen 
622fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
623fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
624fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
625fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
626fd2c15ecSOhad Ben-Cohen 	}
627fd2c15ecSOhad Ben-Cohen 
628400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
629172e6ab1SSuman Anna 	if (!trace)
630400e64dfSOhad Ben-Cohen 		return -ENOMEM;
631400e64dfSOhad Ben-Cohen 
632400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
633a987e6b9SLoic Pallardy 	trace->trace_mem.len = rsc->len;
634a987e6b9SLoic Pallardy 	trace->trace_mem.da = rsc->da;
635a987e6b9SLoic Pallardy 
636a987e6b9SLoic Pallardy 	/* set pointer on rproc device */
637a987e6b9SLoic Pallardy 	trace->rproc = rproc;
638400e64dfSOhad Ben-Cohen 
639400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
640400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
641400e64dfSOhad Ben-Cohen 
642400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
643a987e6b9SLoic Pallardy 	trace->tfile = rproc_create_trace_file(name, rproc, trace);
644a987e6b9SLoic Pallardy 	if (!trace->tfile) {
645400e64dfSOhad Ben-Cohen 		kfree(trace);
646400e64dfSOhad Ben-Cohen 		return -EINVAL;
647400e64dfSOhad Ben-Cohen 	}
648400e64dfSOhad Ben-Cohen 
649400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
650400e64dfSOhad Ben-Cohen 
651400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
652400e64dfSOhad Ben-Cohen 
653a987e6b9SLoic Pallardy 	dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n",
654a987e6b9SLoic Pallardy 		name, rsc->da, rsc->len);
655400e64dfSOhad Ben-Cohen 
656400e64dfSOhad Ben-Cohen 	return 0;
657400e64dfSOhad Ben-Cohen }
658400e64dfSOhad Ben-Cohen 
659400e64dfSOhad Ben-Cohen /**
660400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
661400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
662400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
6632e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
664fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
665400e64dfSOhad Ben-Cohen  *
666400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
667400e64dfSOhad Ben-Cohen  *
668400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
669400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
670400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
671400e64dfSOhad Ben-Cohen  *
672400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
673400e64dfSOhad Ben-Cohen  *
674400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
675400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
676400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
677400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
678400e64dfSOhad Ben-Cohen  *
679400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
680400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
681400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
682400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
683400e64dfSOhad Ben-Cohen  * are outside those ranges.
684400e64dfSOhad Ben-Cohen  */
685fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
686a2b950acSOhad Ben-Cohen 			       int offset, int avail)
687400e64dfSOhad Ben-Cohen {
688400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
689b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
690400e64dfSOhad Ben-Cohen 	int ret;
691400e64dfSOhad Ben-Cohen 
692400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
693400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
694400e64dfSOhad Ben-Cohen 		return -EINVAL;
695400e64dfSOhad Ben-Cohen 
696fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
697b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
698fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
699fd2c15ecSOhad Ben-Cohen 	}
700fd2c15ecSOhad Ben-Cohen 
701fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
702fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
703b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
704fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
705fd2c15ecSOhad Ben-Cohen 	}
706fd2c15ecSOhad Ben-Cohen 
707400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
708172e6ab1SSuman Anna 	if (!mapping)
709400e64dfSOhad Ben-Cohen 		return -ENOMEM;
710400e64dfSOhad Ben-Cohen 
711400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
712400e64dfSOhad Ben-Cohen 	if (ret) {
713b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
714400e64dfSOhad Ben-Cohen 		goto out;
715400e64dfSOhad Ben-Cohen 	}
716400e64dfSOhad Ben-Cohen 
717400e64dfSOhad Ben-Cohen 	/*
718400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
719400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
720400e64dfSOhad Ben-Cohen 	 *
721400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
722400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
723400e64dfSOhad Ben-Cohen 	 */
724400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
725400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
726400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
727400e64dfSOhad Ben-Cohen 
728b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
729400e64dfSOhad Ben-Cohen 		rsc->pa, rsc->da, rsc->len);
730400e64dfSOhad Ben-Cohen 
731400e64dfSOhad Ben-Cohen 	return 0;
732400e64dfSOhad Ben-Cohen 
733400e64dfSOhad Ben-Cohen out:
734400e64dfSOhad Ben-Cohen 	kfree(mapping);
735400e64dfSOhad Ben-Cohen 	return ret;
736400e64dfSOhad Ben-Cohen }
737400e64dfSOhad Ben-Cohen 
738400e64dfSOhad Ben-Cohen /**
739d7c51706SLoic Pallardy  * rproc_alloc_carveout() - allocated specified carveout
740d7c51706SLoic Pallardy  * @rproc: rproc handle
741d7c51706SLoic Pallardy  * @mem: the memory entry to allocate
742d7c51706SLoic Pallardy  *
743d7c51706SLoic Pallardy  * This function allocate specified memory entry @mem using
744d7c51706SLoic Pallardy  * dma_alloc_coherent() as default allocator
745d7c51706SLoic Pallardy  */
746d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc,
747d7c51706SLoic Pallardy 				struct rproc_mem_entry *mem)
748d7c51706SLoic Pallardy {
749d7c51706SLoic Pallardy 	struct rproc_mem_entry *mapping = NULL;
750d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
751d7c51706SLoic Pallardy 	dma_addr_t dma;
752d7c51706SLoic Pallardy 	void *va;
753d7c51706SLoic Pallardy 	int ret;
754d7c51706SLoic Pallardy 
755d7c51706SLoic Pallardy 	va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL);
756d7c51706SLoic Pallardy 	if (!va) {
757d7c51706SLoic Pallardy 		dev_err(dev->parent,
758096ee786SClement Leger 			"failed to allocate dma memory: len 0x%zx\n",
759096ee786SClement Leger 			mem->len);
760d7c51706SLoic Pallardy 		return -ENOMEM;
761d7c51706SLoic Pallardy 	}
762d7c51706SLoic Pallardy 
763096ee786SClement Leger 	dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n",
764d7c51706SLoic Pallardy 		va, &dma, mem->len);
765d7c51706SLoic Pallardy 
76660f849a5SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) {
76760f849a5SLoic Pallardy 		/*
76860f849a5SLoic Pallardy 		 * Check requested da is equal to dma address
76960f849a5SLoic Pallardy 		 * and print a warn message in case of missalignment.
77060f849a5SLoic Pallardy 		 * Don't stop rproc_start sequence as coprocessor may
77160f849a5SLoic Pallardy 		 * build pa to da translation on its side.
77260f849a5SLoic Pallardy 		 */
77360f849a5SLoic Pallardy 		if (mem->da != (u32)dma)
77460f849a5SLoic Pallardy 			dev_warn(dev->parent,
77560f849a5SLoic Pallardy 				 "Allocated carveout doesn't fit device address request\n");
77660f849a5SLoic Pallardy 	}
77760f849a5SLoic Pallardy 
778d7c51706SLoic Pallardy 	/*
779d7c51706SLoic Pallardy 	 * Ok, this is non-standard.
780d7c51706SLoic Pallardy 	 *
781d7c51706SLoic Pallardy 	 * Sometimes we can't rely on the generic iommu-based DMA API
782d7c51706SLoic Pallardy 	 * to dynamically allocate the device address and then set the IOMMU
783d7c51706SLoic Pallardy 	 * tables accordingly, because some remote processors might
784d7c51706SLoic Pallardy 	 * _require_ us to use hard coded device addresses that their
785d7c51706SLoic Pallardy 	 * firmware was compiled with.
786d7c51706SLoic Pallardy 	 *
787d7c51706SLoic Pallardy 	 * In this case, we must use the IOMMU API directly and map
788d7c51706SLoic Pallardy 	 * the memory to the device address as expected by the remote
789d7c51706SLoic Pallardy 	 * processor.
790d7c51706SLoic Pallardy 	 *
791d7c51706SLoic Pallardy 	 * Obviously such remote processor devices should not be configured
792d7c51706SLoic Pallardy 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
793d7c51706SLoic Pallardy 	 * physical address in this case.
794d7c51706SLoic Pallardy 	 */
79560f849a5SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) {
796d7c51706SLoic Pallardy 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
797d7c51706SLoic Pallardy 		if (!mapping) {
798d7c51706SLoic Pallardy 			ret = -ENOMEM;
799d7c51706SLoic Pallardy 			goto dma_free;
800d7c51706SLoic Pallardy 		}
801d7c51706SLoic Pallardy 
802d7c51706SLoic Pallardy 		ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
803d7c51706SLoic Pallardy 				mem->flags);
804d7c51706SLoic Pallardy 		if (ret) {
805d7c51706SLoic Pallardy 			dev_err(dev, "iommu_map failed: %d\n", ret);
806d7c51706SLoic Pallardy 			goto free_mapping;
807d7c51706SLoic Pallardy 		}
808d7c51706SLoic Pallardy 
809d7c51706SLoic Pallardy 		/*
810d7c51706SLoic Pallardy 		 * We'll need this info later when we'll want to unmap
811d7c51706SLoic Pallardy 		 * everything (e.g. on shutdown).
812d7c51706SLoic Pallardy 		 *
813d7c51706SLoic Pallardy 		 * We can't trust the remote processor not to change the
814d7c51706SLoic Pallardy 		 * resource table, so we must maintain this info independently.
815d7c51706SLoic Pallardy 		 */
816d7c51706SLoic Pallardy 		mapping->da = mem->da;
817d7c51706SLoic Pallardy 		mapping->len = mem->len;
818d7c51706SLoic Pallardy 		list_add_tail(&mapping->node, &rproc->mappings);
819d7c51706SLoic Pallardy 
820d7c51706SLoic Pallardy 		dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
821d7c51706SLoic Pallardy 			mem->da, &dma);
82260f849a5SLoic Pallardy 	}
82360f849a5SLoic Pallardy 
82460f849a5SLoic Pallardy 	if (mem->da == FW_RSC_ADDR_ANY) {
825b36de8cfSLoic Pallardy 		/* Update device address as undefined by requester */
826b36de8cfSLoic Pallardy 		if ((u64)dma & HIGH_BITS_MASK)
827b36de8cfSLoic Pallardy 			dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n");
828b36de8cfSLoic Pallardy 
829d7c51706SLoic Pallardy 		mem->da = (u32)dma;
830d7c51706SLoic Pallardy 	}
831d7c51706SLoic Pallardy 
83280137b40SLoic Pallardy 	mem->dma = dma;
833d7c51706SLoic Pallardy 	mem->va = va;
834d7c51706SLoic Pallardy 
835d7c51706SLoic Pallardy 	return 0;
836d7c51706SLoic Pallardy 
837d7c51706SLoic Pallardy free_mapping:
838d7c51706SLoic Pallardy 	kfree(mapping);
839d7c51706SLoic Pallardy dma_free:
840d7c51706SLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, va, dma);
841d7c51706SLoic Pallardy 	return ret;
842d7c51706SLoic Pallardy }
843d7c51706SLoic Pallardy 
844d7c51706SLoic Pallardy /**
845f2e74abfSLoic Pallardy  * rproc_release_carveout() - release acquired carveout
846f2e74abfSLoic Pallardy  * @rproc: rproc handle
847f2e74abfSLoic Pallardy  * @mem: the memory entry to release
848f2e74abfSLoic Pallardy  *
849f2e74abfSLoic Pallardy  * This function releases specified memory entry @mem allocated via
850d7c51706SLoic Pallardy  * rproc_alloc_carveout() function by @rproc.
851f2e74abfSLoic Pallardy  */
852f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc,
853f2e74abfSLoic Pallardy 				  struct rproc_mem_entry *mem)
854f2e74abfSLoic Pallardy {
855f2e74abfSLoic Pallardy 	struct device *dev = &rproc->dev;
856f2e74abfSLoic Pallardy 
857f2e74abfSLoic Pallardy 	/* clean up carveout allocations */
858f2e74abfSLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
859f2e74abfSLoic Pallardy 	return 0;
860f2e74abfSLoic Pallardy }
861f2e74abfSLoic Pallardy 
862f2e74abfSLoic Pallardy /**
863400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
864400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
865400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
8662e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
867fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
868400e64dfSOhad Ben-Cohen  *
869400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
870400e64dfSOhad Ben-Cohen  * contiguous memory regions.
871400e64dfSOhad Ben-Cohen  *
872400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
873400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
874400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
875400e64dfSOhad Ben-Cohen  *
876400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
877400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
878400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
879400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
880400e64dfSOhad Ben-Cohen  */
881fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
882a2b950acSOhad Ben-Cohen 				 struct fw_rsc_carveout *rsc,
883a2b950acSOhad Ben-Cohen 				 int offset, int avail)
884400e64dfSOhad Ben-Cohen {
885d7c51706SLoic Pallardy 	struct rproc_mem_entry *carveout;
886b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
887400e64dfSOhad Ben-Cohen 
888fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
889b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
890fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
891fd2c15ecSOhad Ben-Cohen 	}
892fd2c15ecSOhad Ben-Cohen 
893fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
894fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
895fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
896fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
897fd2c15ecSOhad Ben-Cohen 	}
898fd2c15ecSOhad Ben-Cohen 
8999d7814a9SAnna, Suman 	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
90035386166SLee Jones 		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
901fd2c15ecSOhad Ben-Cohen 
902ffa5f9c8SLoic Pallardy 	/*
903ffa5f9c8SLoic Pallardy 	 * Check carveout rsc already part of a registered carveout,
904ffa5f9c8SLoic Pallardy 	 * Search by name, then check the da and length
905ffa5f9c8SLoic Pallardy 	 */
906ffa5f9c8SLoic Pallardy 	carveout = rproc_find_carveout_by_name(rproc, rsc->name);
907ffa5f9c8SLoic Pallardy 
908ffa5f9c8SLoic Pallardy 	if (carveout) {
909ffa5f9c8SLoic Pallardy 		if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
910ffa5f9c8SLoic Pallardy 			dev_err(dev,
911ffa5f9c8SLoic Pallardy 				"Carveout already associated to resource table\n");
912ffa5f9c8SLoic Pallardy 			return -ENOMEM;
913ffa5f9c8SLoic Pallardy 		}
914ffa5f9c8SLoic Pallardy 
915ffa5f9c8SLoic Pallardy 		if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len))
916ffa5f9c8SLoic Pallardy 			return -ENOMEM;
917ffa5f9c8SLoic Pallardy 
918ffa5f9c8SLoic Pallardy 		/* Update memory carveout with resource table info */
919ffa5f9c8SLoic Pallardy 		carveout->rsc_offset = offset;
920ffa5f9c8SLoic Pallardy 		carveout->flags = rsc->flags;
921ffa5f9c8SLoic Pallardy 
922ffa5f9c8SLoic Pallardy 		return 0;
923ffa5f9c8SLoic Pallardy 	}
924ffa5f9c8SLoic Pallardy 
925d7c51706SLoic Pallardy 	/* Register carveout in in list */
92699cf0361SBen Dooks (Codethink) 	carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
927d7c51706SLoic Pallardy 					rproc_alloc_carveout,
928d7c51706SLoic Pallardy 					rproc_release_carveout, rsc->name);
929d7c51706SLoic Pallardy 	if (!carveout) {
930d7c51706SLoic Pallardy 		dev_err(dev, "Can't allocate memory entry structure\n");
93172029c90SLoic Pallardy 		return -ENOMEM;
932400e64dfSOhad Ben-Cohen 	}
933400e64dfSOhad Ben-Cohen 
934d7c51706SLoic Pallardy 	carveout->flags = rsc->flags;
935d7c51706SLoic Pallardy 	carveout->rsc_offset = offset;
93615c0b025SLoic Pallardy 	rproc_add_carveout(rproc, carveout);
937400e64dfSOhad Ben-Cohen 
938400e64dfSOhad Ben-Cohen 	return 0;
939400e64dfSOhad Ben-Cohen }
940400e64dfSOhad Ben-Cohen 
94172029c90SLoic Pallardy /**
94215c0b025SLoic Pallardy  * rproc_add_carveout() - register an allocated carveout region
94315c0b025SLoic Pallardy  * @rproc: rproc handle
94415c0b025SLoic Pallardy  * @mem: memory entry to register
94515c0b025SLoic Pallardy  *
94615c0b025SLoic Pallardy  * This function registers specified memory entry in @rproc carveouts list.
94715c0b025SLoic Pallardy  * Specified carveout should have been allocated before registering.
94815c0b025SLoic Pallardy  */
94915c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
95015c0b025SLoic Pallardy {
95115c0b025SLoic Pallardy 	list_add_tail(&mem->node, &rproc->carveouts);
95215c0b025SLoic Pallardy }
95315c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout);
95415c0b025SLoic Pallardy 
95515c0b025SLoic Pallardy /**
95672029c90SLoic Pallardy  * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
95772029c90SLoic Pallardy  * @dev: pointer on device struct
95872029c90SLoic Pallardy  * @va: virtual address
95972029c90SLoic Pallardy  * @dma: dma address
96072029c90SLoic Pallardy  * @len: memory carveout length
96172029c90SLoic Pallardy  * @da: device address
962a9f6fe0dSLoic Pallardy  * @alloc: memory carveout allocation function
963a9f6fe0dSLoic Pallardy  * @release: memory carveout release function
96472029c90SLoic Pallardy  * @name: carveout name
96572029c90SLoic Pallardy  *
96672029c90SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
96772029c90SLoic Pallardy  * provided by client.
96872029c90SLoic Pallardy  */
96972029c90SLoic Pallardy struct rproc_mem_entry *
97072029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev,
971096ee786SClement Leger 		     void *va, dma_addr_t dma, size_t len, u32 da,
972d7c51706SLoic Pallardy 		     int (*alloc)(struct rproc *, struct rproc_mem_entry *),
97372029c90SLoic Pallardy 		     int (*release)(struct rproc *, struct rproc_mem_entry *),
97472029c90SLoic Pallardy 		     const char *name, ...)
97572029c90SLoic Pallardy {
97672029c90SLoic Pallardy 	struct rproc_mem_entry *mem;
97772029c90SLoic Pallardy 	va_list args;
97872029c90SLoic Pallardy 
97972029c90SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
98072029c90SLoic Pallardy 	if (!mem)
98172029c90SLoic Pallardy 		return mem;
98272029c90SLoic Pallardy 
98372029c90SLoic Pallardy 	mem->va = va;
98472029c90SLoic Pallardy 	mem->dma = dma;
98572029c90SLoic Pallardy 	mem->da = da;
98672029c90SLoic Pallardy 	mem->len = len;
987d7c51706SLoic Pallardy 	mem->alloc = alloc;
98872029c90SLoic Pallardy 	mem->release = release;
989d7c51706SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
9901429cca1SLoic Pallardy 	mem->of_resm_idx = -1;
99172029c90SLoic Pallardy 
99272029c90SLoic Pallardy 	va_start(args, name);
99372029c90SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
99472029c90SLoic Pallardy 	va_end(args);
99572029c90SLoic Pallardy 
99672029c90SLoic Pallardy 	return mem;
99772029c90SLoic Pallardy }
99872029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init);
99972029c90SLoic Pallardy 
100072029c90SLoic Pallardy /**
10011429cca1SLoic Pallardy  * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
10021429cca1SLoic Pallardy  * from a reserved memory phandle
10031429cca1SLoic Pallardy  * @dev: pointer on device struct
10041429cca1SLoic Pallardy  * @of_resm_idx: reserved memory phandle index in "memory-region"
10051429cca1SLoic Pallardy  * @len: memory carveout length
10061429cca1SLoic Pallardy  * @da: device address
10071429cca1SLoic Pallardy  * @name: carveout name
10081429cca1SLoic Pallardy  *
10091429cca1SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
10101429cca1SLoic Pallardy  * provided by client.
10111429cca1SLoic Pallardy  */
10121429cca1SLoic Pallardy struct rproc_mem_entry *
1013096ee786SClement Leger rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len,
10141429cca1SLoic Pallardy 			     u32 da, const char *name, ...)
10151429cca1SLoic Pallardy {
10161429cca1SLoic Pallardy 	struct rproc_mem_entry *mem;
10171429cca1SLoic Pallardy 	va_list args;
10181429cca1SLoic Pallardy 
10191429cca1SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
10201429cca1SLoic Pallardy 	if (!mem)
10211429cca1SLoic Pallardy 		return mem;
10221429cca1SLoic Pallardy 
10231429cca1SLoic Pallardy 	mem->da = da;
10241429cca1SLoic Pallardy 	mem->len = len;
10251429cca1SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
10261429cca1SLoic Pallardy 	mem->of_resm_idx = of_resm_idx;
10271429cca1SLoic Pallardy 
10281429cca1SLoic Pallardy 	va_start(args, name);
10291429cca1SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
10301429cca1SLoic Pallardy 	va_end(args);
10311429cca1SLoic Pallardy 
10321429cca1SLoic Pallardy 	return mem;
10331429cca1SLoic Pallardy }
10341429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init);
10351429cca1SLoic Pallardy 
10362e7d4c2cSArnaud Pouliquen /*
1037e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
1038e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
1039e12bc14bSOhad Ben-Cohen  */
1040232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
1041fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
1042fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
1043fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
1044232fcdbbSSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
1045232fcdbbSSjur Brændeland };
1046232fcdbbSSjur Brændeland 
1047400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
1048a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc,
1049232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
1050400e64dfSOhad Ben-Cohen {
1051b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1052e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
1053fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
1054400e64dfSOhad Ben-Cohen 
1055d4bb86f2SBjorn Andersson 	if (!rproc->table_ptr)
1056d4bb86f2SBjorn Andersson 		return 0;
1057d4bb86f2SBjorn Andersson 
1058a2b950acSOhad Ben-Cohen 	for (i = 0; i < rproc->table_ptr->num; i++) {
1059a2b950acSOhad Ben-Cohen 		int offset = rproc->table_ptr->offset[i];
1060a2b950acSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
1061a4b24c75SBjorn Andersson 		int avail = rproc->table_sz - offset - sizeof(*hdr);
1062fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
1063400e64dfSOhad Ben-Cohen 
1064fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
1065fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
1066fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
1067fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
1068fd2c15ecSOhad Ben-Cohen 		}
1069fd2c15ecSOhad Ben-Cohen 
1070fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
1071fd2c15ecSOhad Ben-Cohen 
1072b1a17513SClement Leger 		if (hdr->type >= RSC_VENDOR_START &&
1073b1a17513SClement Leger 		    hdr->type <= RSC_VENDOR_END) {
1074b1a17513SClement Leger 			ret = rproc_handle_rsc(rproc, hdr->type, rsc,
1075b1a17513SClement Leger 					       offset + sizeof(*hdr), avail);
1076b1a17513SClement Leger 			if (ret == RSC_HANDLED)
1077b1a17513SClement Leger 				continue;
1078b1a17513SClement Leger 			else if (ret < 0)
1079b1a17513SClement Leger 				break;
1080b1a17513SClement Leger 
1081b1a17513SClement Leger 			dev_warn(dev, "unsupported vendor resource %d\n",
1082b1a17513SClement Leger 				 hdr->type);
1083b1a17513SClement Leger 			continue;
1084b1a17513SClement Leger 		}
1085b1a17513SClement Leger 
1086fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
1087fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
1088e12bc14bSOhad Ben-Cohen 			continue;
1089400e64dfSOhad Ben-Cohen 		}
1090400e64dfSOhad Ben-Cohen 
1091232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
1092e12bc14bSOhad Ben-Cohen 		if (!handler)
1093e12bc14bSOhad Ben-Cohen 			continue;
1094e12bc14bSOhad Ben-Cohen 
1095a2b950acSOhad Ben-Cohen 		ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
10967a186941SOhad Ben-Cohen 		if (ret)
1097400e64dfSOhad Ben-Cohen 			break;
1098400e64dfSOhad Ben-Cohen 	}
1099400e64dfSOhad Ben-Cohen 
1100400e64dfSOhad Ben-Cohen 	return ret;
1101400e64dfSOhad Ben-Cohen }
1102400e64dfSOhad Ben-Cohen 
1103c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc)
1104c455daa4SBjorn Andersson {
1105c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1106c455daa4SBjorn Andersson 	int ret;
1107c455daa4SBjorn Andersson 
1108c455daa4SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1109c455daa4SBjorn Andersson 		if (subdev->prepare) {
1110c455daa4SBjorn Andersson 			ret = subdev->prepare(subdev);
1111c455daa4SBjorn Andersson 			if (ret)
1112c455daa4SBjorn Andersson 				goto unroll_preparation;
1113c455daa4SBjorn Andersson 		}
1114c455daa4SBjorn Andersson 	}
1115c455daa4SBjorn Andersson 
1116c455daa4SBjorn Andersson 	return 0;
1117c455daa4SBjorn Andersson 
1118c455daa4SBjorn Andersson unroll_preparation:
1119c455daa4SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1120c455daa4SBjorn Andersson 		if (subdev->unprepare)
1121c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1122c455daa4SBjorn Andersson 	}
1123c455daa4SBjorn Andersson 
1124c455daa4SBjorn Andersson 	return ret;
1125c455daa4SBjorn Andersson }
1126c455daa4SBjorn Andersson 
1127618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc)
11287bdc9650SBjorn Andersson {
11297bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
11307bdc9650SBjorn Andersson 	int ret;
11317bdc9650SBjorn Andersson 
11327bdc9650SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1133be37b1e0SBjorn Andersson 		if (subdev->start) {
1134618fcff3SBjorn Andersson 			ret = subdev->start(subdev);
11357bdc9650SBjorn Andersson 			if (ret)
11367bdc9650SBjorn Andersson 				goto unroll_registration;
11377bdc9650SBjorn Andersson 		}
1138be37b1e0SBjorn Andersson 	}
11397bdc9650SBjorn Andersson 
11407bdc9650SBjorn Andersson 	return 0;
11417bdc9650SBjorn Andersson 
11427bdc9650SBjorn Andersson unroll_registration:
1143be37b1e0SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1144be37b1e0SBjorn Andersson 		if (subdev->stop)
1145618fcff3SBjorn Andersson 			subdev->stop(subdev, true);
1146be37b1e0SBjorn Andersson 	}
11477bdc9650SBjorn Andersson 
11487bdc9650SBjorn Andersson 	return ret;
11497bdc9650SBjorn Andersson }
11507bdc9650SBjorn Andersson 
1151618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
11527bdc9650SBjorn Andersson {
11537bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
11547bdc9650SBjorn Andersson 
1155be37b1e0SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1156be37b1e0SBjorn Andersson 		if (subdev->stop)
1157618fcff3SBjorn Andersson 			subdev->stop(subdev, crashed);
11587bdc9650SBjorn Andersson 	}
1159be37b1e0SBjorn Andersson }
11607bdc9650SBjorn Andersson 
1161c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc)
1162c455daa4SBjorn Andersson {
1163c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1164c455daa4SBjorn Andersson 
1165c455daa4SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1166c455daa4SBjorn Andersson 		if (subdev->unprepare)
1167c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1168c455daa4SBjorn Andersson 	}
1169c455daa4SBjorn Andersson }
1170c455daa4SBjorn Andersson 
1171400e64dfSOhad Ben-Cohen /**
1172d7c51706SLoic Pallardy  * rproc_alloc_registered_carveouts() - allocate all carveouts registered
1173d7c51706SLoic Pallardy  * in the list
1174d7c51706SLoic Pallardy  * @rproc: the remote processor handle
1175d7c51706SLoic Pallardy  *
1176d7c51706SLoic Pallardy  * This function parses registered carveout list, performs allocation
1177d7c51706SLoic Pallardy  * if alloc() ops registered and updates resource table information
1178d7c51706SLoic Pallardy  * if rsc_offset set.
1179d7c51706SLoic Pallardy  *
1180d7c51706SLoic Pallardy  * Return: 0 on success
1181d7c51706SLoic Pallardy  */
1182d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc)
1183d7c51706SLoic Pallardy {
1184d7c51706SLoic Pallardy 	struct rproc_mem_entry *entry, *tmp;
1185d7c51706SLoic Pallardy 	struct fw_rsc_carveout *rsc;
1186d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
1187b36de8cfSLoic Pallardy 	u64 pa;
1188d7c51706SLoic Pallardy 	int ret;
1189d7c51706SLoic Pallardy 
1190d7c51706SLoic Pallardy 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1191d7c51706SLoic Pallardy 		if (entry->alloc) {
1192d7c51706SLoic Pallardy 			ret = entry->alloc(rproc, entry);
1193d7c51706SLoic Pallardy 			if (ret) {
1194d7c51706SLoic Pallardy 				dev_err(dev, "Unable to allocate carveout %s: %d\n",
1195d7c51706SLoic Pallardy 					entry->name, ret);
1196d7c51706SLoic Pallardy 				return -ENOMEM;
1197d7c51706SLoic Pallardy 			}
1198d7c51706SLoic Pallardy 		}
1199d7c51706SLoic Pallardy 
1200d7c51706SLoic Pallardy 		if (entry->rsc_offset != FW_RSC_ADDR_ANY) {
1201d7c51706SLoic Pallardy 			/* update resource table */
1202d7c51706SLoic Pallardy 			rsc = (void *)rproc->table_ptr + entry->rsc_offset;
1203d7c51706SLoic Pallardy 
1204d7c51706SLoic Pallardy 			/*
1205d7c51706SLoic Pallardy 			 * Some remote processors might need to know the pa
1206d7c51706SLoic Pallardy 			 * even though they are behind an IOMMU. E.g., OMAP4's
1207d7c51706SLoic Pallardy 			 * remote M3 processor needs this so it can control
1208d7c51706SLoic Pallardy 			 * on-chip hardware accelerators that are not behind
1209d7c51706SLoic Pallardy 			 * the IOMMU, and therefor must know the pa.
1210d7c51706SLoic Pallardy 			 *
1211d7c51706SLoic Pallardy 			 * Generally we don't want to expose physical addresses
1212d7c51706SLoic Pallardy 			 * if we don't have to (remote processors are generally
1213d7c51706SLoic Pallardy 			 * _not_ trusted), so we might want to do this only for
1214d7c51706SLoic Pallardy 			 * remote processor that _must_ have this (e.g. OMAP4's
1215d7c51706SLoic Pallardy 			 * dual M3 subsystem).
1216d7c51706SLoic Pallardy 			 *
1217d7c51706SLoic Pallardy 			 * Non-IOMMU processors might also want to have this info.
1218d7c51706SLoic Pallardy 			 * In this case, the device address and the physical address
1219d7c51706SLoic Pallardy 			 * are the same.
1220d7c51706SLoic Pallardy 			 */
1221ffa5f9c8SLoic Pallardy 
1222ffa5f9c8SLoic Pallardy 			/* Use va if defined else dma to generate pa */
1223d7c51706SLoic Pallardy 			if (entry->va)
1224b36de8cfSLoic Pallardy 				pa = (u64)rproc_va_to_pa(entry->va);
1225ffa5f9c8SLoic Pallardy 			else
1226b36de8cfSLoic Pallardy 				pa = (u64)entry->dma;
1227ffa5f9c8SLoic Pallardy 
1228b36de8cfSLoic Pallardy 			if (((u64)pa) & HIGH_BITS_MASK)
1229b36de8cfSLoic Pallardy 				dev_warn(dev,
1230b36de8cfSLoic Pallardy 					 "Physical address cast in 32bit to fit resource table format\n");
1231b36de8cfSLoic Pallardy 
1232b36de8cfSLoic Pallardy 			rsc->pa = (u32)pa;
1233ffa5f9c8SLoic Pallardy 			rsc->da = entry->da;
1234ffa5f9c8SLoic Pallardy 			rsc->len = entry->len;
1235d7c51706SLoic Pallardy 		}
1236d7c51706SLoic Pallardy 	}
1237d7c51706SLoic Pallardy 
1238d7c51706SLoic Pallardy 	return 0;
1239d7c51706SLoic Pallardy }
1240d7c51706SLoic Pallardy 
1241d7c51706SLoic Pallardy /**
12422666ca91SSarangdhar Joshi  * rproc_coredump_cleanup() - clean up dump_segments list
12432666ca91SSarangdhar Joshi  * @rproc: the remote processor handle
12442666ca91SSarangdhar Joshi  */
12452666ca91SSarangdhar Joshi static void rproc_coredump_cleanup(struct rproc *rproc)
12462666ca91SSarangdhar Joshi {
12472666ca91SSarangdhar Joshi 	struct rproc_dump_segment *entry, *tmp;
12482666ca91SSarangdhar Joshi 
12492666ca91SSarangdhar Joshi 	list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) {
12502666ca91SSarangdhar Joshi 		list_del(&entry->node);
12512666ca91SSarangdhar Joshi 		kfree(entry);
12522666ca91SSarangdhar Joshi 	}
12532666ca91SSarangdhar Joshi }
12542666ca91SSarangdhar Joshi 
12552666ca91SSarangdhar Joshi /**
1256400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
1257400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
1258400e64dfSOhad Ben-Cohen  *
1259400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
12607a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
1261400e64dfSOhad Ben-Cohen  */
1262400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc)
1263400e64dfSOhad Ben-Cohen {
1264400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
1265a987e6b9SLoic Pallardy 	struct rproc_debug_trace *trace, *ttmp;
1266d81fb32fSBjorn Andersson 	struct rproc_vdev *rvdev, *rvtmp;
1267b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1268400e64dfSOhad Ben-Cohen 
1269400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
1270a987e6b9SLoic Pallardy 	list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) {
1271a987e6b9SLoic Pallardy 		rproc_remove_trace_file(trace->tfile);
1272400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
1273a987e6b9SLoic Pallardy 		list_del(&trace->node);
1274a987e6b9SLoic Pallardy 		kfree(trace);
1275400e64dfSOhad Ben-Cohen 	}
1276400e64dfSOhad Ben-Cohen 
1277400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
1278400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
1279400e64dfSOhad Ben-Cohen 		size_t unmapped;
1280400e64dfSOhad Ben-Cohen 
1281400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
1282400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
1283400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
1284096ee786SClement Leger 			dev_err(dev, "failed to unmap %zx/%zu\n", entry->len,
1285400e64dfSOhad Ben-Cohen 				unmapped);
1286400e64dfSOhad Ben-Cohen 		}
1287400e64dfSOhad Ben-Cohen 
1288400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
1289400e64dfSOhad Ben-Cohen 		kfree(entry);
1290400e64dfSOhad Ben-Cohen 	}
1291b6356a01SSuman Anna 
1292b6356a01SSuman Anna 	/* clean up carveout allocations */
1293b6356a01SSuman Anna 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1294f2e74abfSLoic Pallardy 		if (entry->release)
1295f2e74abfSLoic Pallardy 			entry->release(rproc, entry);
1296b6356a01SSuman Anna 		list_del(&entry->node);
1297b6356a01SSuman Anna 		kfree(entry);
1298b6356a01SSuman Anna 	}
1299d81fb32fSBjorn Andersson 
1300d81fb32fSBjorn Andersson 	/* clean up remote vdev entries */
1301f5bcb353SBjorn Andersson 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
13022b45cef5SBjorn Andersson 		kref_put(&rvdev->refcount, rproc_vdev_release);
13032666ca91SSarangdhar Joshi 
13042666ca91SSarangdhar Joshi 	rproc_coredump_cleanup(rproc);
13052b45cef5SBjorn Andersson }
1306400e64dfSOhad Ben-Cohen 
13071efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw)
13081efa30d0SSarangdhar Joshi {
1309a4b24c75SBjorn Andersson 	struct resource_table *loaded_table;
13101efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
1311a4b24c75SBjorn Andersson 	int ret;
13121efa30d0SSarangdhar Joshi 
13131efa30d0SSarangdhar Joshi 	/* load the ELF segments to memory */
13141efa30d0SSarangdhar Joshi 	ret = rproc_load_segments(rproc, fw);
13151efa30d0SSarangdhar Joshi 	if (ret) {
13161efa30d0SSarangdhar Joshi 		dev_err(dev, "Failed to load program segments: %d\n", ret);
13171efa30d0SSarangdhar Joshi 		return ret;
13181efa30d0SSarangdhar Joshi 	}
13191efa30d0SSarangdhar Joshi 
13201efa30d0SSarangdhar Joshi 	/*
13211efa30d0SSarangdhar Joshi 	 * The starting device has been given the rproc->cached_table as the
13221efa30d0SSarangdhar Joshi 	 * resource table. The address of the vring along with the other
13231efa30d0SSarangdhar Joshi 	 * allocated resources (carveouts etc) is stored in cached_table.
13241efa30d0SSarangdhar Joshi 	 * In order to pass this information to the remote device we must copy
13251efa30d0SSarangdhar Joshi 	 * this information to device memory. We also update the table_ptr so
13261efa30d0SSarangdhar Joshi 	 * that any subsequent changes will be applied to the loaded version.
13271efa30d0SSarangdhar Joshi 	 */
13281efa30d0SSarangdhar Joshi 	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
13291efa30d0SSarangdhar Joshi 	if (loaded_table) {
1330a4b24c75SBjorn Andersson 		memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
13311efa30d0SSarangdhar Joshi 		rproc->table_ptr = loaded_table;
13321efa30d0SSarangdhar Joshi 	}
13331efa30d0SSarangdhar Joshi 
1334c455daa4SBjorn Andersson 	ret = rproc_prepare_subdevices(rproc);
1335c455daa4SBjorn Andersson 	if (ret) {
1336c455daa4SBjorn Andersson 		dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1337c455daa4SBjorn Andersson 			rproc->name, ret);
1338f68d51bdSSuman Anna 		goto reset_table_ptr;
1339c455daa4SBjorn Andersson 	}
1340c455daa4SBjorn Andersson 
13411efa30d0SSarangdhar Joshi 	/* power up the remote processor */
13421efa30d0SSarangdhar Joshi 	ret = rproc->ops->start(rproc);
13431efa30d0SSarangdhar Joshi 	if (ret) {
13441efa30d0SSarangdhar Joshi 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1345c455daa4SBjorn Andersson 		goto unprepare_subdevices;
13461efa30d0SSarangdhar Joshi 	}
13471efa30d0SSarangdhar Joshi 
1348618fcff3SBjorn Andersson 	/* Start any subdevices for the remote processor */
1349618fcff3SBjorn Andersson 	ret = rproc_start_subdevices(rproc);
13501efa30d0SSarangdhar Joshi 	if (ret) {
13511efa30d0SSarangdhar Joshi 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
13521efa30d0SSarangdhar Joshi 			rproc->name, ret);
1353c455daa4SBjorn Andersson 		goto stop_rproc;
13541efa30d0SSarangdhar Joshi 	}
13551efa30d0SSarangdhar Joshi 
13561efa30d0SSarangdhar Joshi 	rproc->state = RPROC_RUNNING;
13571efa30d0SSarangdhar Joshi 
13581efa30d0SSarangdhar Joshi 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
13591efa30d0SSarangdhar Joshi 
13601efa30d0SSarangdhar Joshi 	return 0;
1361c455daa4SBjorn Andersson 
1362c455daa4SBjorn Andersson stop_rproc:
1363c455daa4SBjorn Andersson 	rproc->ops->stop(rproc);
1364c455daa4SBjorn Andersson unprepare_subdevices:
1365c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1366f68d51bdSSuman Anna reset_table_ptr:
1367f68d51bdSSuman Anna 	rproc->table_ptr = rproc->cached_table;
1368c455daa4SBjorn Andersson 
1369c455daa4SBjorn Andersson 	return ret;
13701efa30d0SSarangdhar Joshi }
13711efa30d0SSarangdhar Joshi 
1372400e64dfSOhad Ben-Cohen /*
1373400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
1374400e64dfSOhad Ben-Cohen  */
1375400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1376400e64dfSOhad Ben-Cohen {
1377b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1378400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
137958b64090SBjorn Andersson 	int ret;
1380400e64dfSOhad Ben-Cohen 
1381400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
1382400e64dfSOhad Ben-Cohen 	if (ret)
1383400e64dfSOhad Ben-Cohen 		return ret;
1384400e64dfSOhad Ben-Cohen 
1385e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
1386400e64dfSOhad Ben-Cohen 
1387400e64dfSOhad Ben-Cohen 	/*
1388400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1389400e64dfSOhad Ben-Cohen 	 * just a nop
1390400e64dfSOhad Ben-Cohen 	 */
1391400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
1392400e64dfSOhad Ben-Cohen 	if (ret) {
1393400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
1394400e64dfSOhad Ben-Cohen 		return ret;
1395400e64dfSOhad Ben-Cohen 	}
1396400e64dfSOhad Ben-Cohen 
13973e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
1398400e64dfSOhad Ben-Cohen 
1399c1d35c1aSBjorn Andersson 	/* Load resource table, core dump segment list etc from the firmware */
1400c1d35c1aSBjorn Andersson 	ret = rproc_parse_fw(rproc, fw);
140158b64090SBjorn Andersson 	if (ret)
140258b64090SBjorn Andersson 		goto disable_iommu;
1403a0c10687SBjorn Andersson 
1404b35d7afcSBjorn Andersson 	/* reset max_notifyid */
1405b35d7afcSBjorn Andersson 	rproc->max_notifyid = -1;
1406b35d7afcSBjorn Andersson 
1407c6aed238SLoic Pallardy 	/* reset handled vdev */
1408c6aed238SLoic Pallardy 	rproc->nb_vdev = 0;
1409c6aed238SLoic Pallardy 
1410400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
1411a4b24c75SBjorn Andersson 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1412400e64dfSOhad Ben-Cohen 	if (ret) {
1413400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
1414229b85a6SBjorn Andersson 		goto clean_up_resources;
1415400e64dfSOhad Ben-Cohen 	}
1416400e64dfSOhad Ben-Cohen 
1417d7c51706SLoic Pallardy 	/* Allocate carveout resources associated to rproc */
1418d7c51706SLoic Pallardy 	ret = rproc_alloc_registered_carveouts(rproc);
1419d7c51706SLoic Pallardy 	if (ret) {
1420d7c51706SLoic Pallardy 		dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1421d7c51706SLoic Pallardy 			ret);
1422d7c51706SLoic Pallardy 		goto clean_up_resources;
1423d7c51706SLoic Pallardy 	}
1424d7c51706SLoic Pallardy 
14251efa30d0SSarangdhar Joshi 	ret = rproc_start(rproc, fw);
14261efa30d0SSarangdhar Joshi 	if (ret)
1427229b85a6SBjorn Andersson 		goto clean_up_resources;
1428400e64dfSOhad Ben-Cohen 
1429400e64dfSOhad Ben-Cohen 	return 0;
1430400e64dfSOhad Ben-Cohen 
1431229b85a6SBjorn Andersson clean_up_resources:
1432229b85a6SBjorn Andersson 	rproc_resource_cleanup(rproc);
1433a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1434a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1435988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
143658b64090SBjorn Andersson disable_iommu:
1437400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1438400e64dfSOhad Ben-Cohen 	return ret;
1439400e64dfSOhad Ben-Cohen }
1440400e64dfSOhad Ben-Cohen 
1441400e64dfSOhad Ben-Cohen /*
14425e6533f7SSarangdhar Joshi  * take a firmware and boot it up.
1443400e64dfSOhad Ben-Cohen  *
1444400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
1445400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
1446400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
1447400e64dfSOhad Ben-Cohen  * that might be cleaner).
1448400e64dfSOhad Ben-Cohen  */
14495e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
1450400e64dfSOhad Ben-Cohen {
1451400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
1452a2b950acSOhad Ben-Cohen 
14532bfc311aSBjorn Andersson 	rproc_boot(rproc);
1454ddf71187SBjorn Andersson 
1455400e64dfSOhad Ben-Cohen 	release_firmware(fw);
1456400e64dfSOhad Ben-Cohen }
1457400e64dfSOhad Ben-Cohen 
14585e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc)
145970b85ef8SFernando Guzman Lugo {
146070b85ef8SFernando Guzman Lugo 	int ret;
146170b85ef8SFernando Guzman Lugo 
146270b85ef8SFernando Guzman Lugo 	/*
146370b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
146470b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
146570b85ef8SFernando Guzman Lugo 	 */
146670b85ef8SFernando Guzman Lugo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
146770b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
14685e6533f7SSarangdhar Joshi 				      rproc, rproc_auto_boot_callback);
14692099c77dSSarangdhar Joshi 	if (ret < 0)
147070b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
147170b85ef8SFernando Guzman Lugo 
147270b85ef8SFernando Guzman Lugo 	return ret;
147370b85ef8SFernando Guzman Lugo }
147470b85ef8SFernando Guzman Lugo 
1475880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed)
14761efa30d0SSarangdhar Joshi {
14771efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
14781efa30d0SSarangdhar Joshi 	int ret;
14791efa30d0SSarangdhar Joshi 
1480618fcff3SBjorn Andersson 	/* Stop any subdevices for the remote processor */
1481618fcff3SBjorn Andersson 	rproc_stop_subdevices(rproc, crashed);
14821efa30d0SSarangdhar Joshi 
14830a8b81cbSBjorn Andersson 	/* the installed resource table is no longer accessible */
14840a8b81cbSBjorn Andersson 	rproc->table_ptr = rproc->cached_table;
14850a8b81cbSBjorn Andersson 
14861efa30d0SSarangdhar Joshi 	/* power off the remote processor */
14871efa30d0SSarangdhar Joshi 	ret = rproc->ops->stop(rproc);
14881efa30d0SSarangdhar Joshi 	if (ret) {
14891efa30d0SSarangdhar Joshi 		dev_err(dev, "can't stop rproc: %d\n", ret);
14901efa30d0SSarangdhar Joshi 		return ret;
14911efa30d0SSarangdhar Joshi 	}
14921efa30d0SSarangdhar Joshi 
1493c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1494c455daa4SBjorn Andersson 
14951efa30d0SSarangdhar Joshi 	rproc->state = RPROC_OFFLINE;
14961efa30d0SSarangdhar Joshi 
14971efa30d0SSarangdhar Joshi 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
14981efa30d0SSarangdhar Joshi 
14991efa30d0SSarangdhar Joshi 	return 0;
15001efa30d0SSarangdhar Joshi }
15011efa30d0SSarangdhar Joshi 
150270b85ef8SFernando Guzman Lugo /**
15032666ca91SSarangdhar Joshi  * rproc_coredump_add_segment() - add segment of device memory to coredump
15042666ca91SSarangdhar Joshi  * @rproc:	handle of a remote processor
15052666ca91SSarangdhar Joshi  * @da:		device address
15062666ca91SSarangdhar Joshi  * @size:	size of segment
15072666ca91SSarangdhar Joshi  *
15082666ca91SSarangdhar Joshi  * Add device memory to the list of segments to be included in a coredump for
15092666ca91SSarangdhar Joshi  * the remoteproc.
15102666ca91SSarangdhar Joshi  *
15112666ca91SSarangdhar Joshi  * Return: 0 on success, negative errno on error.
15122666ca91SSarangdhar Joshi  */
15132666ca91SSarangdhar Joshi int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size)
15142666ca91SSarangdhar Joshi {
15152666ca91SSarangdhar Joshi 	struct rproc_dump_segment *segment;
15162666ca91SSarangdhar Joshi 
15172666ca91SSarangdhar Joshi 	segment = kzalloc(sizeof(*segment), GFP_KERNEL);
15182666ca91SSarangdhar Joshi 	if (!segment)
15192666ca91SSarangdhar Joshi 		return -ENOMEM;
15202666ca91SSarangdhar Joshi 
15212666ca91SSarangdhar Joshi 	segment->da = da;
15222666ca91SSarangdhar Joshi 	segment->size = size;
15232666ca91SSarangdhar Joshi 
15242666ca91SSarangdhar Joshi 	list_add_tail(&segment->node, &rproc->dump_segments);
15252666ca91SSarangdhar Joshi 
15262666ca91SSarangdhar Joshi 	return 0;
15272666ca91SSarangdhar Joshi }
15282666ca91SSarangdhar Joshi EXPORT_SYMBOL(rproc_coredump_add_segment);
15292666ca91SSarangdhar Joshi 
15302666ca91SSarangdhar Joshi /**
1531ab8f873bSSibi Sankar  * rproc_coredump_add_custom_segment() - add custom coredump segment
1532ab8f873bSSibi Sankar  * @rproc:	handle of a remote processor
1533ab8f873bSSibi Sankar  * @da:		device address
1534ab8f873bSSibi Sankar  * @size:	size of segment
1535ab8f873bSSibi Sankar  * @dumpfn:	custom dump function called for each segment during coredump
1536ab8f873bSSibi Sankar  * @priv:	private data
1537ab8f873bSSibi Sankar  *
1538ab8f873bSSibi Sankar  * Add device memory to the list of segments to be included in the coredump
1539ab8f873bSSibi Sankar  * and associate the segment with the given custom dump function and private
1540ab8f873bSSibi Sankar  * data.
1541ab8f873bSSibi Sankar  *
1542ab8f873bSSibi Sankar  * Return: 0 on success, negative errno on error.
1543ab8f873bSSibi Sankar  */
1544ab8f873bSSibi Sankar int rproc_coredump_add_custom_segment(struct rproc *rproc,
1545ab8f873bSSibi Sankar 				      dma_addr_t da, size_t size,
1546ab8f873bSSibi Sankar 				      void (*dumpfn)(struct rproc *rproc,
1547ab8f873bSSibi Sankar 						     struct rproc_dump_segment *segment,
1548ab8f873bSSibi Sankar 						     void *dest),
1549ab8f873bSSibi Sankar 				      void *priv)
1550ab8f873bSSibi Sankar {
1551ab8f873bSSibi Sankar 	struct rproc_dump_segment *segment;
1552ab8f873bSSibi Sankar 
1553ab8f873bSSibi Sankar 	segment = kzalloc(sizeof(*segment), GFP_KERNEL);
1554ab8f873bSSibi Sankar 	if (!segment)
1555ab8f873bSSibi Sankar 		return -ENOMEM;
1556ab8f873bSSibi Sankar 
1557ab8f873bSSibi Sankar 	segment->da = da;
1558ab8f873bSSibi Sankar 	segment->size = size;
1559ab8f873bSSibi Sankar 	segment->priv = priv;
1560ab8f873bSSibi Sankar 	segment->dump = dumpfn;
1561ab8f873bSSibi Sankar 
1562ab8f873bSSibi Sankar 	list_add_tail(&segment->node, &rproc->dump_segments);
1563ab8f873bSSibi Sankar 
1564ab8f873bSSibi Sankar 	return 0;
1565ab8f873bSSibi Sankar }
1566ab8f873bSSibi Sankar EXPORT_SYMBOL(rproc_coredump_add_custom_segment);
1567ab8f873bSSibi Sankar 
1568ab8f873bSSibi Sankar /**
15692666ca91SSarangdhar Joshi  * rproc_coredump() - perform coredump
15702666ca91SSarangdhar Joshi  * @rproc:	rproc handle
15712666ca91SSarangdhar Joshi  *
15722666ca91SSarangdhar Joshi  * This function will generate an ELF header for the registered segments
15732666ca91SSarangdhar Joshi  * and create a devcoredump device associated with rproc.
15742666ca91SSarangdhar Joshi  */
15752666ca91SSarangdhar Joshi static void rproc_coredump(struct rproc *rproc)
15762666ca91SSarangdhar Joshi {
15772666ca91SSarangdhar Joshi 	struct rproc_dump_segment *segment;
15788f403350SClement Leger 	void *phdr;
15798f403350SClement Leger 	void *ehdr;
15802666ca91SSarangdhar Joshi 	size_t data_size;
15812666ca91SSarangdhar Joshi 	size_t offset;
15822666ca91SSarangdhar Joshi 	void *data;
15832666ca91SSarangdhar Joshi 	void *ptr;
15848f403350SClement Leger 	u8 class = rproc->elf_class;
15852666ca91SSarangdhar Joshi 	int phnum = 0;
15862666ca91SSarangdhar Joshi 
15872666ca91SSarangdhar Joshi 	if (list_empty(&rproc->dump_segments))
15882666ca91SSarangdhar Joshi 		return;
15892666ca91SSarangdhar Joshi 
15908f403350SClement Leger 	data_size = elf_size_of_hdr(class);
15912666ca91SSarangdhar Joshi 	list_for_each_entry(segment, &rproc->dump_segments, node) {
15928f403350SClement Leger 		data_size += elf_size_of_phdr(class) + segment->size;
15932666ca91SSarangdhar Joshi 
15942666ca91SSarangdhar Joshi 		phnum++;
15952666ca91SSarangdhar Joshi 	}
15962666ca91SSarangdhar Joshi 
15972666ca91SSarangdhar Joshi 	data = vmalloc(data_size);
15982666ca91SSarangdhar Joshi 	if (!data)
15992666ca91SSarangdhar Joshi 		return;
16002666ca91SSarangdhar Joshi 
16012666ca91SSarangdhar Joshi 	ehdr = data;
16022666ca91SSarangdhar Joshi 
16038f403350SClement Leger 	memset(ehdr, 0, elf_size_of_hdr(class));
16048f403350SClement Leger 	/* e_ident field is common for both elf32 and elf64 */
16058f403350SClement Leger 	elf_hdr_init_ident(ehdr, class);
16062666ca91SSarangdhar Joshi 
16078f403350SClement Leger 	elf_hdr_set_e_type(class, ehdr, ET_CORE);
16088f403350SClement Leger 	elf_hdr_set_e_machine(class, ehdr, EM_NONE);
16098f403350SClement Leger 	elf_hdr_set_e_version(class, ehdr, EV_CURRENT);
16108f403350SClement Leger 	elf_hdr_set_e_entry(class, ehdr, rproc->bootaddr);
16118f403350SClement Leger 	elf_hdr_set_e_phoff(class, ehdr, elf_size_of_hdr(class));
16128f403350SClement Leger 	elf_hdr_set_e_ehsize(class, ehdr, elf_size_of_hdr(class));
16138f403350SClement Leger 	elf_hdr_set_e_phentsize(class, ehdr, elf_size_of_phdr(class));
16148f403350SClement Leger 	elf_hdr_set_e_phnum(class, ehdr, phnum);
16158f403350SClement Leger 
16168f403350SClement Leger 	phdr = data + elf_hdr_get_e_phoff(class, ehdr);
16178f403350SClement Leger 	offset = elf_hdr_get_e_phoff(class, ehdr);
16188f403350SClement Leger 	offset += elf_size_of_phdr(class) * elf_hdr_get_e_phnum(class, ehdr);
16198f403350SClement Leger 
16202666ca91SSarangdhar Joshi 	list_for_each_entry(segment, &rproc->dump_segments, node) {
16218f403350SClement Leger 		memset(phdr, 0, elf_size_of_phdr(class));
16228f403350SClement Leger 		elf_phdr_set_p_type(class, phdr, PT_LOAD);
16238f403350SClement Leger 		elf_phdr_set_p_offset(class, phdr, offset);
16248f403350SClement Leger 		elf_phdr_set_p_vaddr(class, phdr, segment->da);
16258f403350SClement Leger 		elf_phdr_set_p_paddr(class, phdr, segment->da);
16268f403350SClement Leger 		elf_phdr_set_p_filesz(class, phdr, segment->size);
16278f403350SClement Leger 		elf_phdr_set_p_memsz(class, phdr, segment->size);
16288f403350SClement Leger 		elf_phdr_set_p_flags(class, phdr, PF_R | PF_W | PF_X);
16298f403350SClement Leger 		elf_phdr_set_p_align(class, phdr, 0);
16302666ca91SSarangdhar Joshi 
16313952105dSSibi Sankar 		if (segment->dump) {
16323952105dSSibi Sankar 			segment->dump(rproc, segment, data + offset);
16333952105dSSibi Sankar 		} else {
16342666ca91SSarangdhar Joshi 			ptr = rproc_da_to_va(rproc, segment->da, segment->size);
16352666ca91SSarangdhar Joshi 			if (!ptr) {
16362666ca91SSarangdhar Joshi 				dev_err(&rproc->dev,
16372666ca91SSarangdhar Joshi 					"invalid coredump segment (%pad, %zu)\n",
16382666ca91SSarangdhar Joshi 					&segment->da, segment->size);
16392666ca91SSarangdhar Joshi 				memset(data + offset, 0xff, segment->size);
16402666ca91SSarangdhar Joshi 			} else {
16412666ca91SSarangdhar Joshi 				memcpy(data + offset, ptr, segment->size);
16422666ca91SSarangdhar Joshi 			}
16433952105dSSibi Sankar 		}
16442666ca91SSarangdhar Joshi 
16458f403350SClement Leger 		offset += elf_phdr_get_p_filesz(class, phdr);
16468f403350SClement Leger 		phdr += elf_size_of_phdr(class);
16472666ca91SSarangdhar Joshi 	}
16482666ca91SSarangdhar Joshi 
16492666ca91SSarangdhar Joshi 	dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL);
16502666ca91SSarangdhar Joshi }
16512666ca91SSarangdhar Joshi 
16522666ca91SSarangdhar Joshi /**
165370b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
165470b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
165570b85ef8SFernando Guzman Lugo  *
165656324d7aSAnna, Suman  * The recovery is done by resetting all the virtio devices, that way all the
165770b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
165870b85ef8SFernando Guzman Lugo  * remoteproc functional again.
165970b85ef8SFernando Guzman Lugo  *
166070b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
166170b85ef8SFernando Guzman Lugo  */
166270b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
166370b85ef8SFernando Guzman Lugo {
16647e83cab8SSarangdhar Joshi 	const struct firmware *firmware_p;
16657e83cab8SSarangdhar Joshi 	struct device *dev = &rproc->dev;
16667e83cab8SSarangdhar Joshi 	int ret;
16677e83cab8SSarangdhar Joshi 
16687e83cab8SSarangdhar Joshi 	ret = mutex_lock_interruptible(&rproc->lock);
16697e83cab8SSarangdhar Joshi 	if (ret)
16707e83cab8SSarangdhar Joshi 		return ret;
16717e83cab8SSarangdhar Joshi 
16720b145574SAlex Elder 	/* State could have changed before we got the mutex */
16730b145574SAlex Elder 	if (rproc->state != RPROC_CRASHED)
16740b145574SAlex Elder 		goto unlock_mutex;
16750b145574SAlex Elder 
16760b145574SAlex Elder 	dev_err(dev, "recovering %s\n", rproc->name);
16770b145574SAlex Elder 
1678fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, true);
16797e83cab8SSarangdhar Joshi 	if (ret)
16807e83cab8SSarangdhar Joshi 		goto unlock_mutex;
1681ddf71187SBjorn Andersson 
16822666ca91SSarangdhar Joshi 	/* generate coredump */
16832666ca91SSarangdhar Joshi 	rproc_coredump(rproc);
16842666ca91SSarangdhar Joshi 
16857e83cab8SSarangdhar Joshi 	/* load firmware */
16867e83cab8SSarangdhar Joshi 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
16877e83cab8SSarangdhar Joshi 	if (ret < 0) {
16887e83cab8SSarangdhar Joshi 		dev_err(dev, "request_firmware failed: %d\n", ret);
16897e83cab8SSarangdhar Joshi 		goto unlock_mutex;
16907e83cab8SSarangdhar Joshi 	}
1691ddf71187SBjorn Andersson 
16927e83cab8SSarangdhar Joshi 	/* boot the remote processor up again */
16937e83cab8SSarangdhar Joshi 	ret = rproc_start(rproc, firmware_p);
16947e83cab8SSarangdhar Joshi 
16957e83cab8SSarangdhar Joshi 	release_firmware(firmware_p);
16967e83cab8SSarangdhar Joshi 
16977e83cab8SSarangdhar Joshi unlock_mutex:
16987e83cab8SSarangdhar Joshi 	mutex_unlock(&rproc->lock);
16997e83cab8SSarangdhar Joshi 	return ret;
170070b85ef8SFernando Guzman Lugo }
170170b85ef8SFernando Guzman Lugo 
1702400e64dfSOhad Ben-Cohen /**
17038afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
17042e7d4c2cSArnaud Pouliquen  * @work: work treating the crash
17058afd519cSFernando Guzman Lugo  *
17068afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
17078afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
17088afd519cSFernando Guzman Lugo  */
17098afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
17108afd519cSFernando Guzman Lugo {
17118afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
17128afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
17138afd519cSFernando Guzman Lugo 
17148afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
17158afd519cSFernando Guzman Lugo 
17168afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
17178afd519cSFernando Guzman Lugo 
17188afd519cSFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
17198afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
17208afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
17218afd519cSFernando Guzman Lugo 		return;
17228afd519cSFernando Guzman Lugo 	}
17238afd519cSFernando Guzman Lugo 
17248afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
17258afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
17268afd519cSFernando Guzman Lugo 		rproc->name);
17278afd519cSFernando Guzman Lugo 
17288afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
17298afd519cSFernando Guzman Lugo 
17302e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
173170b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
17328afd519cSFernando Guzman Lugo }
17338afd519cSFernando Guzman Lugo 
17348afd519cSFernando Guzman Lugo /**
17351b0ef906SSuman Anna  * rproc_boot() - boot a remote processor
1736400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1737400e64dfSOhad Ben-Cohen  *
1738400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1739400e64dfSOhad Ben-Cohen  *
1740400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1741400e64dfSOhad Ben-Cohen  * returns (successfully).
1742400e64dfSOhad Ben-Cohen  *
1743400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1744400e64dfSOhad Ben-Cohen  */
17451b0ef906SSuman Anna int rproc_boot(struct rproc *rproc)
1746400e64dfSOhad Ben-Cohen {
1747400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1748400e64dfSOhad Ben-Cohen 	struct device *dev;
1749400e64dfSOhad Ben-Cohen 	int ret;
1750400e64dfSOhad Ben-Cohen 
1751400e64dfSOhad Ben-Cohen 	if (!rproc) {
1752400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1753400e64dfSOhad Ben-Cohen 		return -EINVAL;
1754400e64dfSOhad Ben-Cohen 	}
1755400e64dfSOhad Ben-Cohen 
1756b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1757400e64dfSOhad Ben-Cohen 
1758400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1759400e64dfSOhad Ben-Cohen 	if (ret) {
1760400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1761400e64dfSOhad Ben-Cohen 		return ret;
1762400e64dfSOhad Ben-Cohen 	}
1763400e64dfSOhad Ben-Cohen 
17642099c77dSSarangdhar Joshi 	if (rproc->state == RPROC_DELETED) {
17652099c77dSSarangdhar Joshi 		ret = -ENODEV;
17662099c77dSSarangdhar Joshi 		dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
17672099c77dSSarangdhar Joshi 		goto unlock_mutex;
17682099c77dSSarangdhar Joshi 	}
17692099c77dSSarangdhar Joshi 
1770400e64dfSOhad Ben-Cohen 	/* skip the boot process if rproc is already powered up */
1771400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1772400e64dfSOhad Ben-Cohen 		ret = 0;
1773400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1774400e64dfSOhad Ben-Cohen 	}
1775400e64dfSOhad Ben-Cohen 
1776400e64dfSOhad Ben-Cohen 	dev_info(dev, "powering up %s\n", rproc->name);
1777400e64dfSOhad Ben-Cohen 
1778400e64dfSOhad Ben-Cohen 	/* load firmware */
1779400e64dfSOhad Ben-Cohen 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1780400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1781400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware failed: %d\n", ret);
1782400e64dfSOhad Ben-Cohen 		goto downref_rproc;
1783400e64dfSOhad Ben-Cohen 	}
1784400e64dfSOhad Ben-Cohen 
1785400e64dfSOhad Ben-Cohen 	ret = rproc_fw_boot(rproc, firmware_p);
1786400e64dfSOhad Ben-Cohen 
1787400e64dfSOhad Ben-Cohen 	release_firmware(firmware_p);
1788400e64dfSOhad Ben-Cohen 
1789400e64dfSOhad Ben-Cohen downref_rproc:
1790fbb6aacbSBjorn Andersson 	if (ret)
1791400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1792400e64dfSOhad Ben-Cohen unlock_mutex:
1793400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1794400e64dfSOhad Ben-Cohen 	return ret;
1795400e64dfSOhad Ben-Cohen }
1796400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1797400e64dfSOhad Ben-Cohen 
1798400e64dfSOhad Ben-Cohen /**
1799400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1800400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1801400e64dfSOhad Ben-Cohen  *
1802400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1803400e64dfSOhad Ben-Cohen  *
1804400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1805400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1806400e64dfSOhad Ben-Cohen  * without really powering off the device.
1807400e64dfSOhad Ben-Cohen  *
1808400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1809400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1810400e64dfSOhad Ben-Cohen  *
1811400e64dfSOhad Ben-Cohen  * Notes:
1812400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1813400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1814400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1815400e64dfSOhad Ben-Cohen  *   needed.
1816400e64dfSOhad Ben-Cohen  */
1817400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1818400e64dfSOhad Ben-Cohen {
1819b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1820400e64dfSOhad Ben-Cohen 	int ret;
1821400e64dfSOhad Ben-Cohen 
1822400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1823400e64dfSOhad Ben-Cohen 	if (ret) {
1824400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1825400e64dfSOhad Ben-Cohen 		return;
1826400e64dfSOhad Ben-Cohen 	}
1827400e64dfSOhad Ben-Cohen 
1828400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1829400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1830400e64dfSOhad Ben-Cohen 		goto out;
1831400e64dfSOhad Ben-Cohen 
1832fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, false);
1833400e64dfSOhad Ben-Cohen 	if (ret) {
1834400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1835400e64dfSOhad Ben-Cohen 		goto out;
1836400e64dfSOhad Ben-Cohen 	}
1837400e64dfSOhad Ben-Cohen 
1838400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1839400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1840400e64dfSOhad Ben-Cohen 
1841400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1842400e64dfSOhad Ben-Cohen 
1843988d204cSBjorn Andersson 	/* Free the copy of the resource table */
1844a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1845a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1846988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
1847400e64dfSOhad Ben-Cohen out:
1848400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1849400e64dfSOhad Ben-Cohen }
1850400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1851400e64dfSOhad Ben-Cohen 
1852400e64dfSOhad Ben-Cohen /**
1853fec47d86SDave Gerlach  * rproc_get_by_phandle() - find a remote processor by phandle
1854fec47d86SDave Gerlach  * @phandle: phandle to the rproc
1855fec47d86SDave Gerlach  *
1856fec47d86SDave Gerlach  * Finds an rproc handle using the remote processor's phandle, and then
1857fec47d86SDave Gerlach  * return a handle to the rproc.
1858fec47d86SDave Gerlach  *
1859fec47d86SDave Gerlach  * This function increments the remote processor's refcount, so always
1860fec47d86SDave Gerlach  * use rproc_put() to decrement it back once rproc isn't needed anymore.
1861fec47d86SDave Gerlach  *
1862fec47d86SDave Gerlach  * Returns the rproc handle on success, and NULL on failure.
1863fec47d86SDave Gerlach  */
18648de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF
1865fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle)
1866fec47d86SDave Gerlach {
1867fec47d86SDave Gerlach 	struct rproc *rproc = NULL, *r;
1868fec47d86SDave Gerlach 	struct device_node *np;
1869fec47d86SDave Gerlach 
1870fec47d86SDave Gerlach 	np = of_find_node_by_phandle(phandle);
1871fec47d86SDave Gerlach 	if (!np)
1872fec47d86SDave Gerlach 		return NULL;
1873fec47d86SDave Gerlach 
1874c0abe2caSBjorn Andersson 	rcu_read_lock();
1875c0abe2caSBjorn Andersson 	list_for_each_entry_rcu(r, &rproc_list, node) {
1876fec47d86SDave Gerlach 		if (r->dev.parent && r->dev.parent->of_node == np) {
1877fbb6aacbSBjorn Andersson 			/* prevent underlying implementation from being removed */
1878fbb6aacbSBjorn Andersson 			if (!try_module_get(r->dev.parent->driver->owner)) {
1879fbb6aacbSBjorn Andersson 				dev_err(&r->dev, "can't get owner\n");
1880fbb6aacbSBjorn Andersson 				break;
1881fbb6aacbSBjorn Andersson 			}
1882fbb6aacbSBjorn Andersson 
1883fec47d86SDave Gerlach 			rproc = r;
1884fec47d86SDave Gerlach 			get_device(&rproc->dev);
1885fec47d86SDave Gerlach 			break;
1886fec47d86SDave Gerlach 		}
1887fec47d86SDave Gerlach 	}
1888c0abe2caSBjorn Andersson 	rcu_read_unlock();
1889fec47d86SDave Gerlach 
1890fec47d86SDave Gerlach 	of_node_put(np);
1891fec47d86SDave Gerlach 
1892fec47d86SDave Gerlach 	return rproc;
1893fec47d86SDave Gerlach }
18948de3dbd0SOhad Ben-Cohen #else
18958de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle)
18968de3dbd0SOhad Ben-Cohen {
18978de3dbd0SOhad Ben-Cohen 	return NULL;
18988de3dbd0SOhad Ben-Cohen }
18998de3dbd0SOhad Ben-Cohen #endif
1900fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle);
1901fec47d86SDave Gerlach 
1902fec47d86SDave Gerlach /**
1903160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
1904400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
1905400e64dfSOhad Ben-Cohen  *
1906400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
1907400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
1908400e64dfSOhad Ben-Cohen  *
1909400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
1910400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
1911400e64dfSOhad Ben-Cohen  *
1912400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
1913400e64dfSOhad Ben-Cohen  *
1914400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
1915400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
1916400e64dfSOhad Ben-Cohen  * firmware.
1917400e64dfSOhad Ben-Cohen  *
1918400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
19197a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
1920400e64dfSOhad Ben-Cohen  * probed.
1921400e64dfSOhad Ben-Cohen  */
1922160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
1923400e64dfSOhad Ben-Cohen {
1924b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
192570b85ef8SFernando Guzman Lugo 	int ret;
1926400e64dfSOhad Ben-Cohen 
1927b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
1928b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
1929b5ab5e24SOhad Ben-Cohen 		return ret;
1930400e64dfSOhad Ben-Cohen 
1931b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
1932400e64dfSOhad Ben-Cohen 
1933400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
1934400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
19357a20c64dSSarangdhar Joshi 
19367a20c64dSSarangdhar Joshi 	/* if rproc is marked always-on, request it to boot */
19377a20c64dSSarangdhar Joshi 	if (rproc->auto_boot) {
19385e6533f7SSarangdhar Joshi 		ret = rproc_trigger_auto_boot(rproc);
1939d2e12e66SDave Gerlach 		if (ret < 0)
1940d2e12e66SDave Gerlach 			return ret;
19417a20c64dSSarangdhar Joshi 	}
1942400e64dfSOhad Ben-Cohen 
1943d2e12e66SDave Gerlach 	/* expose to rproc_get_by_phandle users */
1944d2e12e66SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1945c0abe2caSBjorn Andersson 	list_add_rcu(&rproc->node, &rproc_list);
1946d2e12e66SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1947d2e12e66SDave Gerlach 
1948d2e12e66SDave Gerlach 	return 0;
1949400e64dfSOhad Ben-Cohen }
1950160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
1951400e64dfSOhad Ben-Cohen 
1952400e64dfSOhad Ben-Cohen /**
1953b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
1954b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
1955b5ab5e24SOhad Ben-Cohen  *
1956b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
1957b5ab5e24SOhad Ben-Cohen  *
1958b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
1959b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
1960b5ab5e24SOhad Ben-Cohen  */
1961b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
1962b5ab5e24SOhad Ben-Cohen {
1963b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
1964b5ab5e24SOhad Ben-Cohen 
19657183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
19667183a2a7SOhad Ben-Cohen 
1967b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
1968b5ab5e24SOhad Ben-Cohen 
1969b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
1970b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
1971b5ab5e24SOhad Ben-Cohen 
19720f57dc6aSMatt Redfearn 	kfree(rproc->firmware);
1973fb98e2bdSBjorn Andersson 	kfree(rproc->ops);
1974b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
1975b5ab5e24SOhad Ben-Cohen }
1976b5ab5e24SOhad Ben-Cohen 
1977c42ca04dSBhumika Goyal static const struct device_type rproc_type = {
1978b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
1979b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
1980b5ab5e24SOhad Ben-Cohen };
1981400e64dfSOhad Ben-Cohen 
1982400e64dfSOhad Ben-Cohen /**
1983400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
1984400e64dfSOhad Ben-Cohen  * @dev: the underlying device
1985400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
1986400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
19878b4aec9aSRobert Tivy  * @firmware: name of firmware file to load, can be NULL
1988400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
1989400e64dfSOhad Ben-Cohen  *
1990400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
19918b4aec9aSRobert Tivy  * it yet. if @firmware is NULL, a default name is used.
1992400e64dfSOhad Ben-Cohen  *
1993400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
1994400e64dfSOhad Ben-Cohen  * of the remote processor.
1995400e64dfSOhad Ben-Cohen  *
1996400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
1997160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
1998400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
1999400e64dfSOhad Ben-Cohen  *
2000400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
2001400e64dfSOhad Ben-Cohen  *
2002400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
2003433c0e04SBjorn Andersson  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
2004400e64dfSOhad Ben-Cohen  */
2005400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
2006400e64dfSOhad Ben-Cohen 			  const struct rproc_ops *ops,
2007400e64dfSOhad Ben-Cohen 			  const char *firmware, int len)
2008400e64dfSOhad Ben-Cohen {
2009400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
20108b4aec9aSRobert Tivy 	char *p, *template = "rproc-%s-fw";
20110f57dc6aSMatt Redfearn 	int name_len;
2012400e64dfSOhad Ben-Cohen 
2013400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
2014400e64dfSOhad Ben-Cohen 		return NULL;
2015400e64dfSOhad Ben-Cohen 
20160f57dc6aSMatt Redfearn 	if (!firmware) {
20178b4aec9aSRobert Tivy 		/*
20188b4aec9aSRobert Tivy 		 * If the caller didn't pass in a firmware name then
20190f57dc6aSMatt Redfearn 		 * construct a default name.
20208b4aec9aSRobert Tivy 		 */
20218b4aec9aSRobert Tivy 		name_len = strlen(name) + strlen(template) - 2 + 1;
20220f57dc6aSMatt Redfearn 		p = kmalloc(name_len, GFP_KERNEL);
20230f57dc6aSMatt Redfearn 		if (!p)
2024400e64dfSOhad Ben-Cohen 			return NULL;
20258b4aec9aSRobert Tivy 		snprintf(p, name_len, template, name);
20268b4aec9aSRobert Tivy 	} else {
20270f57dc6aSMatt Redfearn 		p = kstrdup(firmware, GFP_KERNEL);
20280f57dc6aSMatt Redfearn 		if (!p)
20290f57dc6aSMatt Redfearn 			return NULL;
20300f57dc6aSMatt Redfearn 	}
20310f57dc6aSMatt Redfearn 
20320f57dc6aSMatt Redfearn 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
20330f57dc6aSMatt Redfearn 	if (!rproc) {
20340f57dc6aSMatt Redfearn 		kfree(p);
20350f57dc6aSMatt Redfearn 		return NULL;
20368b4aec9aSRobert Tivy 	}
20378b4aec9aSRobert Tivy 
2038fb98e2bdSBjorn Andersson 	rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
2039fb98e2bdSBjorn Andersson 	if (!rproc->ops) {
2040fb98e2bdSBjorn Andersson 		kfree(p);
2041fb98e2bdSBjorn Andersson 		kfree(rproc);
2042fb98e2bdSBjorn Andersson 		return NULL;
2043fb98e2bdSBjorn Andersson 	}
2044fb98e2bdSBjorn Andersson 
20458b4aec9aSRobert Tivy 	rproc->firmware = p;
2046400e64dfSOhad Ben-Cohen 	rproc->name = name;
2047400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
2048ddf71187SBjorn Andersson 	rproc->auto_boot = true;
20498f403350SClement Leger 	rproc->elf_class = ELFCLASS32;
2050400e64dfSOhad Ben-Cohen 
2051b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
2052b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
2053b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
20542aefbef0SMatt Redfearn 	rproc->dev.class = &rproc_class;
20557c89717fSBjorn Andersson 	rproc->dev.driver_data = rproc;
2056b5ab5e24SOhad Ben-Cohen 
2057b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
2058b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
2059b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
2060b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
2061b5ab5e24SOhad Ben-Cohen 		put_device(&rproc->dev);
2062b5ab5e24SOhad Ben-Cohen 		return NULL;
2063b5ab5e24SOhad Ben-Cohen 	}
2064b5ab5e24SOhad Ben-Cohen 
2065b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
2066b5ab5e24SOhad Ben-Cohen 
2067400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
2068400e64dfSOhad Ben-Cohen 
20690f21f9ccSBjorn Andersson 	/* Default to ELF loader if no load function is specified */
20700f21f9ccSBjorn Andersson 	if (!rproc->ops->load) {
20710f21f9ccSBjorn Andersson 		rproc->ops->load = rproc_elf_load_segments;
2072c1d35c1aSBjorn Andersson 		rproc->ops->parse_fw = rproc_elf_load_rsc_table;
20730f21f9ccSBjorn Andersson 		rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
207412677467SClement Leger 		if (!rproc->ops->sanity_check)
2075826c3390SClement Leger 			rproc->ops->sanity_check = rproc_elf32_sanity_check;
20760f21f9ccSBjorn Andersson 		rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
20770f21f9ccSBjorn Andersson 	}
2078400e64dfSOhad Ben-Cohen 
2079400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
2080400e64dfSOhad Ben-Cohen 
20817a186941SOhad Ben-Cohen 	idr_init(&rproc->notifyids);
20827a186941SOhad Ben-Cohen 
2083400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
2084400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
2085400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
20867a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
20877bdc9650SBjorn Andersson 	INIT_LIST_HEAD(&rproc->subdevs);
20882666ca91SSarangdhar Joshi 	INIT_LIST_HEAD(&rproc->dump_segments);
2089400e64dfSOhad Ben-Cohen 
20908afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
20918afd519cSFernando Guzman Lugo 
2092400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
2093400e64dfSOhad Ben-Cohen 
2094400e64dfSOhad Ben-Cohen 	return rproc;
2095400e64dfSOhad Ben-Cohen }
2096400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
2097400e64dfSOhad Ben-Cohen 
2098400e64dfSOhad Ben-Cohen /**
2099433c0e04SBjorn Andersson  * rproc_free() - unroll rproc_alloc()
2100433c0e04SBjorn Andersson  * @rproc: the remote processor handle
2101433c0e04SBjorn Andersson  *
2102433c0e04SBjorn Andersson  * This function decrements the rproc dev refcount.
2103433c0e04SBjorn Andersson  *
2104433c0e04SBjorn Andersson  * If no one holds any reference to rproc anymore, then its refcount would
2105433c0e04SBjorn Andersson  * now drop to zero, and it would be freed.
2106433c0e04SBjorn Andersson  */
2107433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc)
2108433c0e04SBjorn Andersson {
2109433c0e04SBjorn Andersson 	put_device(&rproc->dev);
2110433c0e04SBjorn Andersson }
2111433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free);
2112433c0e04SBjorn Andersson 
2113433c0e04SBjorn Andersson /**
2114433c0e04SBjorn Andersson  * rproc_put() - release rproc reference
2115400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
2116400e64dfSOhad Ben-Cohen  *
2117c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
2118400e64dfSOhad Ben-Cohen  *
2119c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
2120c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
2121400e64dfSOhad Ben-Cohen  */
2122160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
2123400e64dfSOhad Ben-Cohen {
2124fbb6aacbSBjorn Andersson 	module_put(rproc->dev.parent->driver->owner);
2125b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
2126400e64dfSOhad Ben-Cohen }
2127160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
2128400e64dfSOhad Ben-Cohen 
2129400e64dfSOhad Ben-Cohen /**
2130160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
2131400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
2132400e64dfSOhad Ben-Cohen  *
2133400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
2134400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
2135160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
2136400e64dfSOhad Ben-Cohen  * has completed successfully.
2137400e64dfSOhad Ben-Cohen  *
2138160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
2139c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
2140433c0e04SBjorn Andersson  * one last refcount, one still needs to call rproc_free().
2141400e64dfSOhad Ben-Cohen  *
2142400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
2143400e64dfSOhad Ben-Cohen  */
2144160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
2145400e64dfSOhad Ben-Cohen {
2146400e64dfSOhad Ben-Cohen 	if (!rproc)
2147400e64dfSOhad Ben-Cohen 		return -EINVAL;
2148400e64dfSOhad Ben-Cohen 
2149ddf71187SBjorn Andersson 	/* if rproc is marked always-on, rproc_add() booted it */
2150ddf71187SBjorn Andersson 	/* TODO: make sure this works with rproc->power > 1 */
2151ddf71187SBjorn Andersson 	if (rproc->auto_boot)
2152ddf71187SBjorn Andersson 		rproc_shutdown(rproc);
2153ddf71187SBjorn Andersson 
21542099c77dSSarangdhar Joshi 	mutex_lock(&rproc->lock);
21552099c77dSSarangdhar Joshi 	rproc->state = RPROC_DELETED;
21562099c77dSSarangdhar Joshi 	mutex_unlock(&rproc->lock);
21572099c77dSSarangdhar Joshi 
2158b003d45bSSarangdhar Joshi 	rproc_delete_debug_dir(rproc);
2159b003d45bSSarangdhar Joshi 
2160fec47d86SDave Gerlach 	/* the rproc is downref'ed as soon as it's removed from the klist */
2161fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
2162c0abe2caSBjorn Andersson 	list_del_rcu(&rproc->node);
2163fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
2164fec47d86SDave Gerlach 
2165c0abe2caSBjorn Andersson 	/* Ensure that no readers of rproc_list are still active */
2166c0abe2caSBjorn Andersson 	synchronize_rcu();
2167c0abe2caSBjorn Andersson 
2168b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
2169400e64dfSOhad Ben-Cohen 
2170400e64dfSOhad Ben-Cohen 	return 0;
2171400e64dfSOhad Ben-Cohen }
2172160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
2173400e64dfSOhad Ben-Cohen 
21748afd519cSFernando Guzman Lugo /**
21757bdc9650SBjorn Andersson  * rproc_add_subdev() - add a subdevice to a remoteproc
21767bdc9650SBjorn Andersson  * @rproc: rproc handle to add the subdevice to
21777bdc9650SBjorn Andersson  * @subdev: subdev handle to register
21784902676fSBjorn Andersson  *
21794902676fSBjorn Andersson  * Caller is responsible for populating optional subdevice function pointers.
21807bdc9650SBjorn Andersson  */
21814902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
21827bdc9650SBjorn Andersson {
21837bdc9650SBjorn Andersson 	list_add_tail(&subdev->node, &rproc->subdevs);
21847bdc9650SBjorn Andersson }
21857bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev);
21867bdc9650SBjorn Andersson 
21877bdc9650SBjorn Andersson /**
21887bdc9650SBjorn Andersson  * rproc_remove_subdev() - remove a subdevice from a remoteproc
21897bdc9650SBjorn Andersson  * @rproc: rproc handle to remove the subdevice from
21907bdc9650SBjorn Andersson  * @subdev: subdev handle, previously registered with rproc_add_subdev()
21917bdc9650SBjorn Andersson  */
21927bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
21937bdc9650SBjorn Andersson {
21947bdc9650SBjorn Andersson 	list_del(&subdev->node);
21957bdc9650SBjorn Andersson }
21967bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev);
21977bdc9650SBjorn Andersson 
21987bdc9650SBjorn Andersson /**
21997c89717fSBjorn Andersson  * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
22007c89717fSBjorn Andersson  * @dev:	child device to find ancestor of
22017c89717fSBjorn Andersson  *
22027c89717fSBjorn Andersson  * Returns the ancestor rproc instance, or NULL if not found.
22037c89717fSBjorn Andersson  */
22047c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev)
22057c89717fSBjorn Andersson {
22067c89717fSBjorn Andersson 	for (dev = dev->parent; dev; dev = dev->parent) {
22077c89717fSBjorn Andersson 		if (dev->type == &rproc_type)
22087c89717fSBjorn Andersson 			return dev->driver_data;
22097c89717fSBjorn Andersson 	}
22107c89717fSBjorn Andersson 
22117c89717fSBjorn Andersson 	return NULL;
22127c89717fSBjorn Andersson }
22137c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child);
22147c89717fSBjorn Andersson 
22157c89717fSBjorn Andersson /**
22168afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
22178afd519cSFernando Guzman Lugo  * @rproc: remote processor
22188afd519cSFernando Guzman Lugo  * @type: crash type
22198afd519cSFernando Guzman Lugo  *
22208afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
22218afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
22228afd519cSFernando Guzman Lugo  * non-remoteproc driver.
22238afd519cSFernando Guzman Lugo  *
22248afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
22258afd519cSFernando Guzman Lugo  */
22268afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
22278afd519cSFernando Guzman Lugo {
22288afd519cSFernando Guzman Lugo 	if (!rproc) {
22298afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
22308afd519cSFernando Guzman Lugo 		return;
22318afd519cSFernando Guzman Lugo 	}
22328afd519cSFernando Guzman Lugo 
22338afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
22348afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
22358afd519cSFernando Guzman Lugo 
22368afd519cSFernando Guzman Lugo 	/* create a new task to handle the error */
22378afd519cSFernando Guzman Lugo 	schedule_work(&rproc->crash_handler);
22388afd519cSFernando Guzman Lugo }
22398afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
22408afd519cSFernando Guzman Lugo 
2241*dc5192c4SBjorn Andersson static int rproc_panic_handler(struct notifier_block *nb, unsigned long event,
2242*dc5192c4SBjorn Andersson 			       void *ptr)
2243*dc5192c4SBjorn Andersson {
2244*dc5192c4SBjorn Andersson 	unsigned int longest = 0;
2245*dc5192c4SBjorn Andersson 	struct rproc *rproc;
2246*dc5192c4SBjorn Andersson 	unsigned int d;
2247*dc5192c4SBjorn Andersson 
2248*dc5192c4SBjorn Andersson 	rcu_read_lock();
2249*dc5192c4SBjorn Andersson 	list_for_each_entry_rcu(rproc, &rproc_list, node) {
2250*dc5192c4SBjorn Andersson 		if (!rproc->ops->panic || rproc->state != RPROC_RUNNING)
2251*dc5192c4SBjorn Andersson 			continue;
2252*dc5192c4SBjorn Andersson 
2253*dc5192c4SBjorn Andersson 		d = rproc->ops->panic(rproc);
2254*dc5192c4SBjorn Andersson 		longest = max(longest, d);
2255*dc5192c4SBjorn Andersson 	}
2256*dc5192c4SBjorn Andersson 	rcu_read_unlock();
2257*dc5192c4SBjorn Andersson 
2258*dc5192c4SBjorn Andersson 	/*
2259*dc5192c4SBjorn Andersson 	 * Delay for the longest requested duration before returning. This can
2260*dc5192c4SBjorn Andersson 	 * be used by the remoteproc drivers to give the remote processor time
2261*dc5192c4SBjorn Andersson 	 * to perform any requested operations (such as flush caches), when
2262*dc5192c4SBjorn Andersson 	 * it's not possible to signal the Linux side due to the panic.
2263*dc5192c4SBjorn Andersson 	 */
2264*dc5192c4SBjorn Andersson 	mdelay(longest);
2265*dc5192c4SBjorn Andersson 
2266*dc5192c4SBjorn Andersson 	return NOTIFY_DONE;
2267*dc5192c4SBjorn Andersson }
2268*dc5192c4SBjorn Andersson 
2269*dc5192c4SBjorn Andersson static void __init rproc_init_panic(void)
2270*dc5192c4SBjorn Andersson {
2271*dc5192c4SBjorn Andersson 	rproc_panic_nb.notifier_call = rproc_panic_handler;
2272*dc5192c4SBjorn Andersson 	atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb);
2273*dc5192c4SBjorn Andersson }
2274*dc5192c4SBjorn Andersson 
2275*dc5192c4SBjorn Andersson static void __exit rproc_exit_panic(void)
2276*dc5192c4SBjorn Andersson {
2277*dc5192c4SBjorn Andersson 	atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb);
2278*dc5192c4SBjorn Andersson }
2279*dc5192c4SBjorn Andersson 
2280400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
2281400e64dfSOhad Ben-Cohen {
22822aefbef0SMatt Redfearn 	rproc_init_sysfs();
2283400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
2284*dc5192c4SBjorn Andersson 	rproc_init_panic();
2285b5ab5e24SOhad Ben-Cohen 
2286400e64dfSOhad Ben-Cohen 	return 0;
2287400e64dfSOhad Ben-Cohen }
2288a8f40111SBrandon Maier subsys_initcall(remoteproc_init);
2289400e64dfSOhad Ben-Cohen 
2290400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
2291400e64dfSOhad Ben-Cohen {
2292f42f79afSSuman Anna 	ida_destroy(&rproc_dev_index);
2293f42f79afSSuman Anna 
2294*dc5192c4SBjorn Andersson 	rproc_exit_panic();
2295400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
22962aefbef0SMatt Redfearn 	rproc_exit_sysfs();
2297400e64dfSOhad Ben-Cohen }
2298400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
2299400e64dfSOhad Ben-Cohen 
2300400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
2301400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
2302