xref: /linux/drivers/remoteproc/remoteproc_core.c (revision f39650de687e35766572ac89dbcd16a5911e2f0a)
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 
19dc5192c4SBjorn 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>
23*f39650deSAndy Shevchenko #include <linux/panic_notifier.h>
24400e64dfSOhad Ben-Cohen #include <linux/slab.h>
25400e64dfSOhad Ben-Cohen #include <linux/mutex.h>
260a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
27400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h>
28e0d07278SJim Quinlan #include <linux/dma-direct.h> /* XXX: pokes into bus_dma_range */
29400e64dfSOhad Ben-Cohen #include <linux/firmware.h>
30400e64dfSOhad Ben-Cohen #include <linux/string.h>
31400e64dfSOhad Ben-Cohen #include <linux/debugfs.h>
32c0abe2caSBjorn Andersson #include <linux/rculist.h>
33400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h>
34400e64dfSOhad Ben-Cohen #include <linux/iommu.h>
35b5ab5e24SOhad Ben-Cohen #include <linux/idr.h>
36400e64dfSOhad Ben-Cohen #include <linux/elf.h>
37a2b950acSOhad Ben-Cohen #include <linux/crc32.h>
38086d0872SLoic Pallardy #include <linux/of_reserved_mem.h>
39400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h>
40400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h>
41cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h>
42086d0872SLoic Pallardy #include <linux/platform_device.h>
43400e64dfSOhad Ben-Cohen 
44400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h"
45400e64dfSOhad Ben-Cohen 
46b36de8cfSLoic Pallardy #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL
47b36de8cfSLoic Pallardy 
48fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex);
49fec47d86SDave Gerlach static LIST_HEAD(rproc_list);
50dc5192c4SBjorn Andersson static struct notifier_block rproc_panic_nb;
51fec47d86SDave Gerlach 
52a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
53a2b950acSOhad Ben-Cohen 				 void *, int offset, int avail);
54400e64dfSOhad Ben-Cohen 
55c6aed238SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc,
56c6aed238SLoic Pallardy 				struct rproc_mem_entry *mem);
57c6aed238SLoic Pallardy static int rproc_release_carveout(struct rproc *rproc,
58c6aed238SLoic Pallardy 				  struct rproc_mem_entry *mem);
59c6aed238SLoic Pallardy 
60b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */
61b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index);
62b5ab5e24SOhad Ben-Cohen 
638afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = {
648afd519cSFernando Guzman Lugo 	[RPROC_MMUFAULT]	= "mmufault",
65b3d39032SBjorn Andersson 	[RPROC_WATCHDOG]	= "watchdog",
66b3d39032SBjorn Andersson 	[RPROC_FATAL_ERROR]	= "fatal error",
678afd519cSFernando Guzman Lugo };
688afd519cSFernando Guzman Lugo 
698afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */
708afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type)
718afd519cSFernando Guzman Lugo {
728afd519cSFernando Guzman Lugo 	if (type < ARRAY_SIZE(rproc_crash_names))
738afd519cSFernando Guzman Lugo 		return rproc_crash_names[type];
74b23f7a09SMasanari Iida 	return "unknown";
758afd519cSFernando Guzman Lugo }
768afd519cSFernando Guzman Lugo 
77400e64dfSOhad Ben-Cohen /*
78400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
79400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
80400e64dfSOhad Ben-Cohen  * an IOMMU).
81400e64dfSOhad Ben-Cohen  *
82400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
83400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
84400e64dfSOhad Ben-Cohen  */
85400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
8677ca2332SOhad Ben-Cohen 			     unsigned long iova, int flags, void *token)
87400e64dfSOhad Ben-Cohen {
888afd519cSFernando Guzman Lugo 	struct rproc *rproc = token;
898afd519cSFernando Guzman Lugo 
90400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
91400e64dfSOhad Ben-Cohen 
928afd519cSFernando Guzman Lugo 	rproc_report_crash(rproc, RPROC_MMUFAULT);
938afd519cSFernando Guzman Lugo 
94400e64dfSOhad Ben-Cohen 	/*
95400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
968afd519cSFernando Guzman Lugo 	 * we just used it as a recovery trigger.
97400e64dfSOhad Ben-Cohen 	 */
98400e64dfSOhad Ben-Cohen 	return -ENOSYS;
99400e64dfSOhad Ben-Cohen }
100400e64dfSOhad Ben-Cohen 
101400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
102400e64dfSOhad Ben-Cohen {
103400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
104b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
105400e64dfSOhad Ben-Cohen 	int ret;
106400e64dfSOhad Ben-Cohen 
107315491e5SSuman Anna 	if (!rproc->has_iommu) {
108315491e5SSuman Anna 		dev_dbg(dev, "iommu not present\n");
1090798e1daSMark Grosen 		return 0;
110400e64dfSOhad Ben-Cohen 	}
111400e64dfSOhad Ben-Cohen 
112400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
113400e64dfSOhad Ben-Cohen 	if (!domain) {
114400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
115400e64dfSOhad Ben-Cohen 		return -ENOMEM;
116400e64dfSOhad Ben-Cohen 	}
117400e64dfSOhad Ben-Cohen 
11877ca2332SOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
119400e64dfSOhad Ben-Cohen 
120400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
121400e64dfSOhad Ben-Cohen 	if (ret) {
122400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
123400e64dfSOhad Ben-Cohen 		goto free_domain;
124400e64dfSOhad Ben-Cohen 	}
125400e64dfSOhad Ben-Cohen 
126400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
127400e64dfSOhad Ben-Cohen 
128400e64dfSOhad Ben-Cohen 	return 0;
129400e64dfSOhad Ben-Cohen 
130400e64dfSOhad Ben-Cohen free_domain:
131400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
132400e64dfSOhad Ben-Cohen 	return ret;
133400e64dfSOhad Ben-Cohen }
134400e64dfSOhad Ben-Cohen 
135400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
136400e64dfSOhad Ben-Cohen {
137400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
138b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
139400e64dfSOhad Ben-Cohen 
140400e64dfSOhad Ben-Cohen 	if (!domain)
141400e64dfSOhad Ben-Cohen 		return;
142400e64dfSOhad Ben-Cohen 
143400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
144400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
145400e64dfSOhad Ben-Cohen }
146400e64dfSOhad Ben-Cohen 
147086d0872SLoic Pallardy phys_addr_t rproc_va_to_pa(void *cpu_addr)
148eb30596eSLoic Pallardy {
149eb30596eSLoic Pallardy 	/*
150eb30596eSLoic Pallardy 	 * Return physical address according to virtual address location
151eb30596eSLoic Pallardy 	 * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent
152eb30596eSLoic Pallardy 	 * - in kernel: if region allocated in generic dma memory pool
153eb30596eSLoic Pallardy 	 */
154eb30596eSLoic Pallardy 	if (is_vmalloc_addr(cpu_addr)) {
155eb30596eSLoic Pallardy 		return page_to_phys(vmalloc_to_page(cpu_addr)) +
156eb30596eSLoic Pallardy 				    offset_in_page(cpu_addr);
157eb30596eSLoic Pallardy 	}
158eb30596eSLoic Pallardy 
159eb30596eSLoic Pallardy 	WARN_ON(!virt_addr_valid(cpu_addr));
160eb30596eSLoic Pallardy 	return virt_to_phys(cpu_addr);
161eb30596eSLoic Pallardy }
162086d0872SLoic Pallardy EXPORT_SYMBOL(rproc_va_to_pa);
163eb30596eSLoic Pallardy 
164a01f7cd6SSuman Anna /**
165a01f7cd6SSuman Anna  * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
166a01f7cd6SSuman Anna  * @rproc: handle of a remote processor
167a01f7cd6SSuman Anna  * @da: remoteproc device address to translate
168a01f7cd6SSuman Anna  * @len: length of the memory region @da is pointing to
169a01f7cd6SSuman Anna  *
170400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
171400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
172a01f7cd6SSuman Anna  * device addresses (which are hardcoded in the firmware). They may also have
173a01f7cd6SSuman Anna  * dedicated memory regions internal to the processors, and use them either
174a01f7cd6SSuman Anna  * exclusively or alongside carveouts.
175400e64dfSOhad Ben-Cohen  *
176400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
177400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
178400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
179400e64dfSOhad Ben-Cohen  *
180a01f7cd6SSuman Anna  * This function is a helper function with which we can go over the allocated
181a01f7cd6SSuman Anna  * carveouts and translate specific device addresses to kernel virtual addresses
182a01f7cd6SSuman Anna  * so we can access the referenced memory. This function also allows to perform
183a01f7cd6SSuman Anna  * translations on the internal remoteproc memory regions through a platform
184a01f7cd6SSuman Anna  * implementation specific da_to_va ops, if present.
185a01f7cd6SSuman Anna  *
186a01f7cd6SSuman Anna  * The function returns a valid kernel address on success or NULL on failure.
187400e64dfSOhad Ben-Cohen  *
188400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
189400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
190a01f7cd6SSuman Anna  * here the output of the DMA API for the carveouts, which should be more
191a01f7cd6SSuman Anna  * correct.
192400e64dfSOhad Ben-Cohen  */
19340df0a91SPeng Fan void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
194400e64dfSOhad Ben-Cohen {
195400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
196400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
197400e64dfSOhad Ben-Cohen 
198a01f7cd6SSuman Anna 	if (rproc->ops->da_to_va) {
19940df0a91SPeng Fan 		ptr = rproc->ops->da_to_va(rproc, da, len, is_iomem);
200a01f7cd6SSuman Anna 		if (ptr)
201a01f7cd6SSuman Anna 			goto out;
202a01f7cd6SSuman Anna 	}
203a01f7cd6SSuman Anna 
204400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
205400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
206400e64dfSOhad Ben-Cohen 
20774457c40SLoic Pallardy 		/*  Verify that carveout is allocated */
20874457c40SLoic Pallardy 		if (!carveout->va)
20974457c40SLoic Pallardy 			continue;
21074457c40SLoic Pallardy 
211400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
212400e64dfSOhad Ben-Cohen 		if (offset < 0)
213400e64dfSOhad Ben-Cohen 			continue;
214400e64dfSOhad Ben-Cohen 
215400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
216400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
217400e64dfSOhad Ben-Cohen 			continue;
218400e64dfSOhad Ben-Cohen 
219400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
220400e64dfSOhad Ben-Cohen 
22140df0a91SPeng Fan 		if (is_iomem)
22240df0a91SPeng Fan 			*is_iomem = carveout->is_iomem;
22340df0a91SPeng Fan 
224400e64dfSOhad Ben-Cohen 		break;
225400e64dfSOhad Ben-Cohen 	}
226400e64dfSOhad Ben-Cohen 
227a01f7cd6SSuman Anna out:
228400e64dfSOhad Ben-Cohen 	return ptr;
229400e64dfSOhad Ben-Cohen }
2304afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
231400e64dfSOhad Ben-Cohen 
232b0019ccdSLoic Pallardy /**
233b0019ccdSLoic Pallardy  * rproc_find_carveout_by_name() - lookup the carveout region by a name
234b0019ccdSLoic Pallardy  * @rproc: handle of a remote processor
2352e7d4c2cSArnaud Pouliquen  * @name: carveout name to find (format string)
2362e7d4c2cSArnaud Pouliquen  * @...: optional parameters matching @name string
237b0019ccdSLoic Pallardy  *
238b0019ccdSLoic Pallardy  * Platform driver has the capability to register some pre-allacoted carveout
239b0019ccdSLoic Pallardy  * (physically contiguous memory regions) before rproc firmware loading and
240b0019ccdSLoic Pallardy  * associated resource table analysis. These regions may be dedicated memory
241b0019ccdSLoic Pallardy  * regions internal to the coprocessor or specified DDR region with specific
242b0019ccdSLoic Pallardy  * attributes
243b0019ccdSLoic Pallardy  *
244b0019ccdSLoic Pallardy  * This function is a helper function with which we can go over the
245b0019ccdSLoic Pallardy  * allocated carveouts and return associated region characteristics like
246b0019ccdSLoic Pallardy  * coprocessor address, length or processor virtual address.
247b0019ccdSLoic Pallardy  *
248b0019ccdSLoic Pallardy  * Return: a valid pointer on carveout entry on success or NULL on failure.
249b0019ccdSLoic Pallardy  */
2507e05c8deSLee Jones __printf(2, 3)
251b0019ccdSLoic Pallardy struct rproc_mem_entry *
252b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
253b0019ccdSLoic Pallardy {
254b0019ccdSLoic Pallardy 	va_list args;
255b0019ccdSLoic Pallardy 	char _name[32];
256b0019ccdSLoic Pallardy 	struct rproc_mem_entry *carveout, *mem = NULL;
257b0019ccdSLoic Pallardy 
258b0019ccdSLoic Pallardy 	if (!name)
259b0019ccdSLoic Pallardy 		return NULL;
260b0019ccdSLoic Pallardy 
261b0019ccdSLoic Pallardy 	va_start(args, name);
262b0019ccdSLoic Pallardy 	vsnprintf(_name, sizeof(_name), name, args);
263b0019ccdSLoic Pallardy 	va_end(args);
264b0019ccdSLoic Pallardy 
265b0019ccdSLoic Pallardy 	list_for_each_entry(carveout, &rproc->carveouts, node) {
266b0019ccdSLoic Pallardy 		/* Compare carveout and requested names */
267b0019ccdSLoic Pallardy 		if (!strcmp(carveout->name, _name)) {
268b0019ccdSLoic Pallardy 			mem = carveout;
269b0019ccdSLoic Pallardy 			break;
270b0019ccdSLoic Pallardy 		}
271b0019ccdSLoic Pallardy 	}
272b0019ccdSLoic Pallardy 
273b0019ccdSLoic Pallardy 	return mem;
274b0019ccdSLoic Pallardy }
275b0019ccdSLoic Pallardy 
276c874bf59SLoic Pallardy /**
277c874bf59SLoic Pallardy  * rproc_check_carveout_da() - Check specified carveout da configuration
278c874bf59SLoic Pallardy  * @rproc: handle of a remote processor
279c874bf59SLoic Pallardy  * @mem: pointer on carveout to check
280c874bf59SLoic Pallardy  * @da: area device address
281c874bf59SLoic Pallardy  * @len: associated area size
282c874bf59SLoic Pallardy  *
283c874bf59SLoic Pallardy  * This function is a helper function to verify requested device area (couple
28428d7d5c6SLoic Pallardy  * da, len) is part of specified carveout.
28528d7d5c6SLoic Pallardy  * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is
28628d7d5c6SLoic Pallardy  * checked.
287c874bf59SLoic Pallardy  *
28828d7d5c6SLoic Pallardy  * Return: 0 if carveout matches request else error
289c874bf59SLoic Pallardy  */
29028d7d5c6SLoic Pallardy static int rproc_check_carveout_da(struct rproc *rproc,
29128d7d5c6SLoic Pallardy 				   struct rproc_mem_entry *mem, u32 da, u32 len)
292c874bf59SLoic Pallardy {
293c874bf59SLoic Pallardy 	struct device *dev = &rproc->dev;
29428d7d5c6SLoic Pallardy 	int delta;
295c874bf59SLoic Pallardy 
296c874bf59SLoic Pallardy 	/* Check requested resource length */
297c874bf59SLoic Pallardy 	if (len > mem->len) {
298c874bf59SLoic Pallardy 		dev_err(dev, "Registered carveout doesn't fit len request\n");
29928d7d5c6SLoic Pallardy 		return -EINVAL;
300c874bf59SLoic Pallardy 	}
301c874bf59SLoic Pallardy 
302c874bf59SLoic Pallardy 	if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) {
30328d7d5c6SLoic Pallardy 		/* Address doesn't match registered carveout configuration */
30428d7d5c6SLoic Pallardy 		return -EINVAL;
305c874bf59SLoic Pallardy 	} else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) {
306c874bf59SLoic Pallardy 		delta = da - mem->da;
307c874bf59SLoic Pallardy 
308c874bf59SLoic Pallardy 		/* Check requested resource belongs to registered carveout */
309c874bf59SLoic Pallardy 		if (delta < 0) {
310c874bf59SLoic Pallardy 			dev_err(dev,
311c874bf59SLoic Pallardy 				"Registered carveout doesn't fit da request\n");
31228d7d5c6SLoic Pallardy 			return -EINVAL;
313c874bf59SLoic Pallardy 		}
314c874bf59SLoic Pallardy 
315c874bf59SLoic Pallardy 		if (delta + len > mem->len) {
316c874bf59SLoic Pallardy 			dev_err(dev,
317c874bf59SLoic Pallardy 				"Registered carveout doesn't fit len request\n");
31828d7d5c6SLoic Pallardy 			return -EINVAL;
319c874bf59SLoic Pallardy 		}
320c874bf59SLoic Pallardy 	}
321c874bf59SLoic Pallardy 
322c874bf59SLoic Pallardy 	return 0;
323c874bf59SLoic Pallardy }
324c874bf59SLoic Pallardy 
3256db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
326400e64dfSOhad Ben-Cohen {
3277a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
328b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3296db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
330c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
331096ee786SClement Leger 	int ret, notifyid;
332c6aed238SLoic Pallardy 	struct rproc_mem_entry *mem;
333096ee786SClement Leger 	size_t size;
334400e64dfSOhad Ben-Cohen 
3356db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
3366db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
3376db20ea8SOhad Ben-Cohen 
338c6aed238SLoic Pallardy 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
339c6aed238SLoic Pallardy 
340c6aed238SLoic Pallardy 	/* Search for pre-registered carveout */
341c6aed238SLoic Pallardy 	mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
342c6aed238SLoic Pallardy 					  i);
343c6aed238SLoic Pallardy 	if (mem) {
344c6aed238SLoic Pallardy 		if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size))
345c6aed238SLoic Pallardy 			return -ENOMEM;
346c6aed238SLoic Pallardy 	} else {
347c6aed238SLoic Pallardy 		/* Register carveout in in list */
34899cf0361SBen Dooks (Codethink) 		mem = rproc_mem_entry_init(dev, NULL, 0,
34999cf0361SBen Dooks (Codethink) 					   size, rsc->vring[i].da,
350c6aed238SLoic Pallardy 					   rproc_alloc_carveout,
351c6aed238SLoic Pallardy 					   rproc_release_carveout,
352c6aed238SLoic Pallardy 					   "vdev%dvring%d",
353c6aed238SLoic Pallardy 					   rvdev->index, i);
354c6aed238SLoic Pallardy 		if (!mem) {
355c6aed238SLoic Pallardy 			dev_err(dev, "Can't allocate memory entry structure\n");
356c6aed238SLoic Pallardy 			return -ENOMEM;
357c6aed238SLoic Pallardy 		}
358c6aed238SLoic Pallardy 
359c6aed238SLoic Pallardy 		rproc_add_carveout(rproc, mem);
3606db20ea8SOhad Ben-Cohen 	}
3616db20ea8SOhad Ben-Cohen 
3626db20ea8SOhad Ben-Cohen 	/*
3636db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
3646db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
3656db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
3666db20ea8SOhad Ben-Cohen 	 */
36715fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
368b39599b7SSuman Anna 	if (ret < 0) {
36915fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
3706db20ea8SOhad Ben-Cohen 		return ret;
3716db20ea8SOhad Ben-Cohen 	}
37215fc6110STejun Heo 	notifyid = ret;
3736db20ea8SOhad Ben-Cohen 
37448f18f89SBjorn Andersson 	/* Potentially bump max_notifyid */
37548f18f89SBjorn Andersson 	if (notifyid > rproc->max_notifyid)
37648f18f89SBjorn Andersson 		rproc->max_notifyid = notifyid;
37748f18f89SBjorn Andersson 
3786db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
3796db20ea8SOhad Ben-Cohen 
380c6aed238SLoic Pallardy 	/* Let the rproc know the notifyid of this vring.*/
381c0d63157SSjur Brændeland 	rsc->vring[i].notifyid = notifyid;
3826db20ea8SOhad Ben-Cohen 	return 0;
3836db20ea8SOhad Ben-Cohen }
3846db20ea8SOhad Ben-Cohen 
385400e64dfSOhad Ben-Cohen static int
3866db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
387400e64dfSOhad Ben-Cohen {
388400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
389b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
390400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
3916db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
392400e64dfSOhad Ben-Cohen 
3939d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
3947a186941SOhad Ben-Cohen 		i, vring->da, vring->num, vring->align);
3957a186941SOhad Ben-Cohen 
39663140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
39763140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
39863140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
39963140e0eSOhad Ben-Cohen 			vring->num, vring->align);
400400e64dfSOhad Ben-Cohen 		return -EINVAL;
401400e64dfSOhad Ben-Cohen 	}
402400e64dfSOhad Ben-Cohen 
4036db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
4046db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
4056db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
406400e64dfSOhad Ben-Cohen 
407400e64dfSOhad Ben-Cohen 	return 0;
408400e64dfSOhad Ben-Cohen }
409400e64dfSOhad Ben-Cohen 
4106db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
4117a186941SOhad Ben-Cohen {
4126db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
41300a0eec5SClement Leger 	int idx = rvring - rvring->rvdev->vring;
414c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
4157a186941SOhad Ben-Cohen 
4167a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
417099a3f33SSjur Brændeland 
4184d3ebb3bSMathieu Poirier 	/*
4194d3ebb3bSMathieu Poirier 	 * At this point rproc_stop() has been called and the installed resource
4204d3ebb3bSMathieu Poirier 	 * table in the remote processor memory may no longer be accessible. As
4214d3ebb3bSMathieu Poirier 	 * such and as per rproc_stop(), rproc->table_ptr points to the cached
4224d3ebb3bSMathieu Poirier 	 * resource table (rproc->cached_table).  The cached resource table is
4234d3ebb3bSMathieu Poirier 	 * only available when a remote processor has been booted by the
4244d3ebb3bSMathieu Poirier 	 * remoteproc core, otherwise it is NULL.
4254d3ebb3bSMathieu Poirier 	 *
4264d3ebb3bSMathieu Poirier 	 * Based on the above, reset the virtio device section in the cached
4274d3ebb3bSMathieu Poirier 	 * resource table only if there is one to work with.
4284d3ebb3bSMathieu Poirier 	 */
4294d3ebb3bSMathieu Poirier 	if (rproc->table_ptr) {
430c0d63157SSjur Brændeland 		rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
431c0d63157SSjur Brændeland 		rsc->vring[idx].da = 0;
432c0d63157SSjur Brændeland 		rsc->vring[idx].notifyid = -1;
4337a186941SOhad Ben-Cohen 	}
4344d3ebb3bSMathieu Poirier }
4357a186941SOhad Ben-Cohen 
4366f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev)
437f5bcb353SBjorn Andersson {
438f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
439f5bcb353SBjorn Andersson 
440f5bcb353SBjorn Andersson 	return rproc_add_virtio_dev(rvdev, rvdev->id);
441f5bcb353SBjorn Andersson }
442f5bcb353SBjorn Andersson 
4436f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
444f5bcb353SBjorn Andersson {
445f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
446d4c036feSLoic Pallardy 	int ret;
447f5bcb353SBjorn Andersson 
448d4c036feSLoic Pallardy 	ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev);
449d4c036feSLoic Pallardy 	if (ret)
450d4c036feSLoic Pallardy 		dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret);
451f5bcb353SBjorn Andersson }
452f5bcb353SBjorn Andersson 
453400e64dfSOhad Ben-Cohen /**
454086d0872SLoic Pallardy  * rproc_rvdev_release() - release the existence of a rvdev
455086d0872SLoic Pallardy  *
456086d0872SLoic Pallardy  * @dev: the subdevice's dev
457086d0872SLoic Pallardy  */
458086d0872SLoic Pallardy static void rproc_rvdev_release(struct device *dev)
459086d0872SLoic Pallardy {
460086d0872SLoic Pallardy 	struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev);
461086d0872SLoic Pallardy 
462086d0872SLoic Pallardy 	of_reserved_mem_device_release(dev);
463086d0872SLoic Pallardy 
464086d0872SLoic Pallardy 	kfree(rvdev);
465086d0872SLoic Pallardy }
466086d0872SLoic Pallardy 
467e0d07278SJim Quinlan static int copy_dma_range_map(struct device *to, struct device *from)
468e0d07278SJim Quinlan {
469e0d07278SJim Quinlan 	const struct bus_dma_region *map = from->dma_range_map, *new_map, *r;
470e0d07278SJim Quinlan 	int num_ranges = 0;
471e0d07278SJim Quinlan 
472e0d07278SJim Quinlan 	if (!map)
473e0d07278SJim Quinlan 		return 0;
474e0d07278SJim Quinlan 
475e0d07278SJim Quinlan 	for (r = map; r->size; r++)
476e0d07278SJim Quinlan 		num_ranges++;
477e0d07278SJim Quinlan 
478e0d07278SJim Quinlan 	new_map = kmemdup(map, array_size(num_ranges + 1, sizeof(*map)),
479e0d07278SJim Quinlan 			  GFP_KERNEL);
480e0d07278SJim Quinlan 	if (!new_map)
481e0d07278SJim Quinlan 		return -ENOMEM;
482e0d07278SJim Quinlan 	to->dma_range_map = new_map;
483e0d07278SJim Quinlan 	return 0;
484e0d07278SJim Quinlan }
485e0d07278SJim Quinlan 
486086d0872SLoic Pallardy /**
487fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
488400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
4892bf23461SJindong Yue  * @ptr: the vring resource descriptor
4902e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
491fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
492400e64dfSOhad Ben-Cohen  *
4937a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
4947a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
4957a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
4967a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
497400e64dfSOhad Ben-Cohen  *
4987a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
4997a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
5007a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
5017a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
5027a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
503400e64dfSOhad Ben-Cohen  *
5047a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
5057a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
5067a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
5077a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
5087a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
5097a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
5107a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
511400e64dfSOhad Ben-Cohen  *
512400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
513400e64dfSOhad Ben-Cohen  */
5142bf23461SJindong Yue static int rproc_handle_vdev(struct rproc *rproc, void *ptr,
515a2b950acSOhad Ben-Cohen 			     int offset, int avail)
516400e64dfSOhad Ben-Cohen {
5172bf23461SJindong Yue 	struct fw_rsc_vdev *rsc = ptr;
518b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
5197a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
5207a186941SOhad Ben-Cohen 	int i, ret;
521086d0872SLoic Pallardy 	char name[16];
522fd2c15ecSOhad Ben-Cohen 
523fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
524c8784657SGustavo A. R. Silva 	if (struct_size(rsc, vring, rsc->num_of_vrings) + rsc->config_len >
525c8784657SGustavo A. R. Silva 			avail) {
526b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
527fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
528fd2c15ecSOhad Ben-Cohen 	}
529fd2c15ecSOhad Ben-Cohen 
530fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
531fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
532fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
533fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
534fd2c15ecSOhad Ben-Cohen 	}
535fd2c15ecSOhad Ben-Cohen 
5369d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
537fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
538400e64dfSOhad Ben-Cohen 
5397a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
5407a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
541fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
542400e64dfSOhad Ben-Cohen 		return -EINVAL;
543400e64dfSOhad Ben-Cohen 	}
544400e64dfSOhad Ben-Cohen 
545899585adSAnna, Suman 	rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
5467a186941SOhad Ben-Cohen 	if (!rvdev)
5477a186941SOhad Ben-Cohen 		return -ENOMEM;
5487a186941SOhad Ben-Cohen 
549aab8d802SBjorn Andersson 	kref_init(&rvdev->refcount);
550aab8d802SBjorn Andersson 
551f5bcb353SBjorn Andersson 	rvdev->id = rsc->id;
5527a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
553c6aed238SLoic Pallardy 	rvdev->index = rproc->nb_vdev++;
5547a186941SOhad Ben-Cohen 
555086d0872SLoic Pallardy 	/* Initialise vdev subdevice */
556086d0872SLoic Pallardy 	snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index);
557c774ad01SSuman Anna 	rvdev->dev.parent = &rproc->dev;
558e0d07278SJim Quinlan 	ret = copy_dma_range_map(&rvdev->dev, rproc->dev.parent);
559e0d07278SJim Quinlan 	if (ret)
560e0d07278SJim Quinlan 		return ret;
561086d0872SLoic Pallardy 	rvdev->dev.release = rproc_rvdev_release;
562086d0872SLoic Pallardy 	dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name);
563086d0872SLoic Pallardy 	dev_set_drvdata(&rvdev->dev, rvdev);
564086d0872SLoic Pallardy 
565086d0872SLoic Pallardy 	ret = device_register(&rvdev->dev);
566086d0872SLoic Pallardy 	if (ret) {
567086d0872SLoic Pallardy 		put_device(&rvdev->dev);
568086d0872SLoic Pallardy 		return ret;
569086d0872SLoic Pallardy 	}
570086d0872SLoic Pallardy 	/* Make device dma capable by inheriting from parent's capabilities */
571086d0872SLoic Pallardy 	set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent));
572086d0872SLoic Pallardy 
573086d0872SLoic Pallardy 	ret = dma_coerce_mask_and_coherent(&rvdev->dev,
574086d0872SLoic Pallardy 					   dma_get_mask(rproc->dev.parent));
575086d0872SLoic Pallardy 	if (ret) {
576086d0872SLoic Pallardy 		dev_warn(dev,
577086d0872SLoic Pallardy 			 "Failed to set DMA mask %llx. Trying to continue... %x\n",
578086d0872SLoic Pallardy 			 dma_get_mask(rproc->dev.parent), ret);
579086d0872SLoic Pallardy 	}
580086d0872SLoic Pallardy 
5816db20ea8SOhad Ben-Cohen 	/* parse the vrings */
582fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
5836db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
5847a186941SOhad Ben-Cohen 		if (ret)
5856db20ea8SOhad Ben-Cohen 			goto free_rvdev;
586fd2c15ecSOhad Ben-Cohen 	}
587fd2c15ecSOhad Ben-Cohen 
588a2b950acSOhad Ben-Cohen 	/* remember the resource offset*/
589a2b950acSOhad Ben-Cohen 	rvdev->rsc_offset = offset;
590400e64dfSOhad Ben-Cohen 
591a863af5dSBjorn Andersson 	/* allocate the vring resources */
592a863af5dSBjorn Andersson 	for (i = 0; i < rsc->num_of_vrings; i++) {
593a863af5dSBjorn Andersson 		ret = rproc_alloc_vring(rvdev, i);
594a863af5dSBjorn Andersson 		if (ret)
595a863af5dSBjorn Andersson 			goto unwind_vring_allocations;
596a863af5dSBjorn Andersson 	}
597a863af5dSBjorn Andersson 
5987a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
599400e64dfSOhad Ben-Cohen 
6006f8b0373SAlex Elder 	rvdev->subdev.start = rproc_vdev_do_start;
6016f8b0373SAlex Elder 	rvdev->subdev.stop = rproc_vdev_do_stop;
6024902676fSBjorn Andersson 
6034902676fSBjorn Andersson 	rproc_add_subdev(rproc, &rvdev->subdev);
604400e64dfSOhad Ben-Cohen 
605400e64dfSOhad Ben-Cohen 	return 0;
6067a186941SOhad Ben-Cohen 
607a863af5dSBjorn Andersson unwind_vring_allocations:
608a863af5dSBjorn Andersson 	for (i--; i >= 0; i--)
609a863af5dSBjorn Andersson 		rproc_free_vring(&rvdev->vring[i]);
6106db20ea8SOhad Ben-Cohen free_rvdev:
611086d0872SLoic Pallardy 	device_unregister(&rvdev->dev);
6127a186941SOhad Ben-Cohen 	return ret;
613400e64dfSOhad Ben-Cohen }
614400e64dfSOhad Ben-Cohen 
615aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref)
616aab8d802SBjorn Andersson {
617aab8d802SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
618a863af5dSBjorn Andersson 	struct rproc_vring *rvring;
619f5bcb353SBjorn Andersson 	struct rproc *rproc = rvdev->rproc;
620a863af5dSBjorn Andersson 	int id;
621a863af5dSBjorn Andersson 
622a863af5dSBjorn Andersson 	for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
623a863af5dSBjorn Andersson 		rvring = &rvdev->vring[id];
624a863af5dSBjorn Andersson 		rproc_free_vring(rvring);
625a863af5dSBjorn Andersson 	}
626aab8d802SBjorn Andersson 
627f5bcb353SBjorn Andersson 	rproc_remove_subdev(rproc, &rvdev->subdev);
628aab8d802SBjorn Andersson 	list_del(&rvdev->node);
629086d0872SLoic Pallardy 	device_unregister(&rvdev->dev);
630aab8d802SBjorn Andersson }
631aab8d802SBjorn Andersson 
632400e64dfSOhad Ben-Cohen /**
633400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
634400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
6352bf23461SJindong Yue  * @ptr: the trace resource descriptor
6362e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
637fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
638400e64dfSOhad Ben-Cohen  *
639400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
640400e64dfSOhad Ben-Cohen  * export it via debugfs.
641400e64dfSOhad Ben-Cohen  *
642400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
643400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
644400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
645400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
646400e64dfSOhad Ben-Cohen  *
647400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
648400e64dfSOhad Ben-Cohen  */
6492bf23461SJindong Yue static int rproc_handle_trace(struct rproc *rproc, void *ptr,
650a2b950acSOhad Ben-Cohen 			      int offset, int avail)
651400e64dfSOhad Ben-Cohen {
6522bf23461SJindong Yue 	struct fw_rsc_trace *rsc = ptr;
653a987e6b9SLoic Pallardy 	struct rproc_debug_trace *trace;
654b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
655400e64dfSOhad Ben-Cohen 	char name[15];
656400e64dfSOhad Ben-Cohen 
657fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
658b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
659fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
660fd2c15ecSOhad Ben-Cohen 	}
661fd2c15ecSOhad Ben-Cohen 
662fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
663fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
664fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
665fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
666fd2c15ecSOhad Ben-Cohen 	}
667fd2c15ecSOhad Ben-Cohen 
668400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
669172e6ab1SSuman Anna 	if (!trace)
670400e64dfSOhad Ben-Cohen 		return -ENOMEM;
671400e64dfSOhad Ben-Cohen 
672400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
673a987e6b9SLoic Pallardy 	trace->trace_mem.len = rsc->len;
674a987e6b9SLoic Pallardy 	trace->trace_mem.da = rsc->da;
675a987e6b9SLoic Pallardy 
676a987e6b9SLoic Pallardy 	/* set pointer on rproc device */
677a987e6b9SLoic Pallardy 	trace->rproc = rproc;
678400e64dfSOhad Ben-Cohen 
679400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
680400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
681400e64dfSOhad Ben-Cohen 
682400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
683a987e6b9SLoic Pallardy 	trace->tfile = rproc_create_trace_file(name, rproc, trace);
684a987e6b9SLoic Pallardy 	if (!trace->tfile) {
685400e64dfSOhad Ben-Cohen 		kfree(trace);
686400e64dfSOhad Ben-Cohen 		return -EINVAL;
687400e64dfSOhad Ben-Cohen 	}
688400e64dfSOhad Ben-Cohen 
689400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
690400e64dfSOhad Ben-Cohen 
691400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
692400e64dfSOhad Ben-Cohen 
693a987e6b9SLoic Pallardy 	dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n",
694a987e6b9SLoic Pallardy 		name, rsc->da, rsc->len);
695400e64dfSOhad Ben-Cohen 
696400e64dfSOhad Ben-Cohen 	return 0;
697400e64dfSOhad Ben-Cohen }
698400e64dfSOhad Ben-Cohen 
699400e64dfSOhad Ben-Cohen /**
700400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
701400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
7022bf23461SJindong Yue  * @ptr: the devmem resource entry
7032e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
704fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
705400e64dfSOhad Ben-Cohen  *
706400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
707400e64dfSOhad Ben-Cohen  *
708400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
709400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
710400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
711400e64dfSOhad Ben-Cohen  *
712400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
713400e64dfSOhad Ben-Cohen  *
714400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
715400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
716400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
717400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
718400e64dfSOhad Ben-Cohen  *
719400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
720400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
721400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
722400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
723400e64dfSOhad Ben-Cohen  * are outside those ranges.
724400e64dfSOhad Ben-Cohen  */
7252bf23461SJindong Yue static int rproc_handle_devmem(struct rproc *rproc, void *ptr,
726a2b950acSOhad Ben-Cohen 			       int offset, int avail)
727400e64dfSOhad Ben-Cohen {
7282bf23461SJindong Yue 	struct fw_rsc_devmem *rsc = ptr;
729400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
730b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
731400e64dfSOhad Ben-Cohen 	int ret;
732400e64dfSOhad Ben-Cohen 
733400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
734400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
735400e64dfSOhad Ben-Cohen 		return -EINVAL;
736400e64dfSOhad Ben-Cohen 
737fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
738b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
739fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
740fd2c15ecSOhad Ben-Cohen 	}
741fd2c15ecSOhad Ben-Cohen 
742fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
743fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
744b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
745fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
746fd2c15ecSOhad Ben-Cohen 	}
747fd2c15ecSOhad Ben-Cohen 
748400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
749172e6ab1SSuman Anna 	if (!mapping)
750400e64dfSOhad Ben-Cohen 		return -ENOMEM;
751400e64dfSOhad Ben-Cohen 
752400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
753400e64dfSOhad Ben-Cohen 	if (ret) {
754b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
755400e64dfSOhad Ben-Cohen 		goto out;
756400e64dfSOhad Ben-Cohen 	}
757400e64dfSOhad Ben-Cohen 
758400e64dfSOhad Ben-Cohen 	/*
759400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
760400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
761400e64dfSOhad Ben-Cohen 	 *
762400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
763400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
764400e64dfSOhad Ben-Cohen 	 */
765400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
766400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
767400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
768400e64dfSOhad Ben-Cohen 
769b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
770400e64dfSOhad Ben-Cohen 		rsc->pa, rsc->da, rsc->len);
771400e64dfSOhad Ben-Cohen 
772400e64dfSOhad Ben-Cohen 	return 0;
773400e64dfSOhad Ben-Cohen 
774400e64dfSOhad Ben-Cohen out:
775400e64dfSOhad Ben-Cohen 	kfree(mapping);
776400e64dfSOhad Ben-Cohen 	return ret;
777400e64dfSOhad Ben-Cohen }
778400e64dfSOhad Ben-Cohen 
779400e64dfSOhad Ben-Cohen /**
780d7c51706SLoic Pallardy  * rproc_alloc_carveout() - allocated specified carveout
781d7c51706SLoic Pallardy  * @rproc: rproc handle
782d7c51706SLoic Pallardy  * @mem: the memory entry to allocate
783d7c51706SLoic Pallardy  *
784d7c51706SLoic Pallardy  * This function allocate specified memory entry @mem using
785d7c51706SLoic Pallardy  * dma_alloc_coherent() as default allocator
786d7c51706SLoic Pallardy  */
787d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc,
788d7c51706SLoic Pallardy 				struct rproc_mem_entry *mem)
789d7c51706SLoic Pallardy {
790d7c51706SLoic Pallardy 	struct rproc_mem_entry *mapping = NULL;
791d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
792d7c51706SLoic Pallardy 	dma_addr_t dma;
793d7c51706SLoic Pallardy 	void *va;
794d7c51706SLoic Pallardy 	int ret;
795d7c51706SLoic Pallardy 
796d7c51706SLoic Pallardy 	va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL);
797d7c51706SLoic Pallardy 	if (!va) {
798d7c51706SLoic Pallardy 		dev_err(dev->parent,
799096ee786SClement Leger 			"failed to allocate dma memory: len 0x%zx\n",
800096ee786SClement Leger 			mem->len);
801d7c51706SLoic Pallardy 		return -ENOMEM;
802d7c51706SLoic Pallardy 	}
803d7c51706SLoic Pallardy 
804096ee786SClement Leger 	dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n",
805d7c51706SLoic Pallardy 		va, &dma, mem->len);
806d7c51706SLoic Pallardy 
80760f849a5SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) {
80860f849a5SLoic Pallardy 		/*
80960f849a5SLoic Pallardy 		 * Check requested da is equal to dma address
81060f849a5SLoic Pallardy 		 * and print a warn message in case of missalignment.
81160f849a5SLoic Pallardy 		 * Don't stop rproc_start sequence as coprocessor may
81260f849a5SLoic Pallardy 		 * build pa to da translation on its side.
81360f849a5SLoic Pallardy 		 */
81460f849a5SLoic Pallardy 		if (mem->da != (u32)dma)
81560f849a5SLoic Pallardy 			dev_warn(dev->parent,
81660f849a5SLoic Pallardy 				 "Allocated carveout doesn't fit device address request\n");
81760f849a5SLoic Pallardy 	}
81860f849a5SLoic Pallardy 
819d7c51706SLoic Pallardy 	/*
820d7c51706SLoic Pallardy 	 * Ok, this is non-standard.
821d7c51706SLoic Pallardy 	 *
822d7c51706SLoic Pallardy 	 * Sometimes we can't rely on the generic iommu-based DMA API
823d7c51706SLoic Pallardy 	 * to dynamically allocate the device address and then set the IOMMU
824d7c51706SLoic Pallardy 	 * tables accordingly, because some remote processors might
825d7c51706SLoic Pallardy 	 * _require_ us to use hard coded device addresses that their
826d7c51706SLoic Pallardy 	 * firmware was compiled with.
827d7c51706SLoic Pallardy 	 *
828d7c51706SLoic Pallardy 	 * In this case, we must use the IOMMU API directly and map
829d7c51706SLoic Pallardy 	 * the memory to the device address as expected by the remote
830d7c51706SLoic Pallardy 	 * processor.
831d7c51706SLoic Pallardy 	 *
832d7c51706SLoic Pallardy 	 * Obviously such remote processor devices should not be configured
833d7c51706SLoic Pallardy 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
834d7c51706SLoic Pallardy 	 * physical address in this case.
835d7c51706SLoic Pallardy 	 */
83660f849a5SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) {
837d7c51706SLoic Pallardy 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
838d7c51706SLoic Pallardy 		if (!mapping) {
839d7c51706SLoic Pallardy 			ret = -ENOMEM;
840d7c51706SLoic Pallardy 			goto dma_free;
841d7c51706SLoic Pallardy 		}
842d7c51706SLoic Pallardy 
843d7c51706SLoic Pallardy 		ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
844d7c51706SLoic Pallardy 				mem->flags);
845d7c51706SLoic Pallardy 		if (ret) {
846d7c51706SLoic Pallardy 			dev_err(dev, "iommu_map failed: %d\n", ret);
847d7c51706SLoic Pallardy 			goto free_mapping;
848d7c51706SLoic Pallardy 		}
849d7c51706SLoic Pallardy 
850d7c51706SLoic Pallardy 		/*
851d7c51706SLoic Pallardy 		 * We'll need this info later when we'll want to unmap
852d7c51706SLoic Pallardy 		 * everything (e.g. on shutdown).
853d7c51706SLoic Pallardy 		 *
854d7c51706SLoic Pallardy 		 * We can't trust the remote processor not to change the
855d7c51706SLoic Pallardy 		 * resource table, so we must maintain this info independently.
856d7c51706SLoic Pallardy 		 */
857d7c51706SLoic Pallardy 		mapping->da = mem->da;
858d7c51706SLoic Pallardy 		mapping->len = mem->len;
859d7c51706SLoic Pallardy 		list_add_tail(&mapping->node, &rproc->mappings);
860d7c51706SLoic Pallardy 
861d7c51706SLoic Pallardy 		dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
862d7c51706SLoic Pallardy 			mem->da, &dma);
86360f849a5SLoic Pallardy 	}
86460f849a5SLoic Pallardy 
86560f849a5SLoic Pallardy 	if (mem->da == FW_RSC_ADDR_ANY) {
866b36de8cfSLoic Pallardy 		/* Update device address as undefined by requester */
867b36de8cfSLoic Pallardy 		if ((u64)dma & HIGH_BITS_MASK)
868b36de8cfSLoic Pallardy 			dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n");
869b36de8cfSLoic Pallardy 
870d7c51706SLoic Pallardy 		mem->da = (u32)dma;
871d7c51706SLoic Pallardy 	}
872d7c51706SLoic Pallardy 
87380137b40SLoic Pallardy 	mem->dma = dma;
874d7c51706SLoic Pallardy 	mem->va = va;
875d7c51706SLoic Pallardy 
876d7c51706SLoic Pallardy 	return 0;
877d7c51706SLoic Pallardy 
878d7c51706SLoic Pallardy free_mapping:
879d7c51706SLoic Pallardy 	kfree(mapping);
880d7c51706SLoic Pallardy dma_free:
881d7c51706SLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, va, dma);
882d7c51706SLoic Pallardy 	return ret;
883d7c51706SLoic Pallardy }
884d7c51706SLoic Pallardy 
885d7c51706SLoic Pallardy /**
886f2e74abfSLoic Pallardy  * rproc_release_carveout() - release acquired carveout
887f2e74abfSLoic Pallardy  * @rproc: rproc handle
888f2e74abfSLoic Pallardy  * @mem: the memory entry to release
889f2e74abfSLoic Pallardy  *
890f2e74abfSLoic Pallardy  * This function releases specified memory entry @mem allocated via
891d7c51706SLoic Pallardy  * rproc_alloc_carveout() function by @rproc.
892f2e74abfSLoic Pallardy  */
893f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc,
894f2e74abfSLoic Pallardy 				  struct rproc_mem_entry *mem)
895f2e74abfSLoic Pallardy {
896f2e74abfSLoic Pallardy 	struct device *dev = &rproc->dev;
897f2e74abfSLoic Pallardy 
898f2e74abfSLoic Pallardy 	/* clean up carveout allocations */
899f2e74abfSLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
900f2e74abfSLoic Pallardy 	return 0;
901f2e74abfSLoic Pallardy }
902f2e74abfSLoic Pallardy 
903f2e74abfSLoic Pallardy /**
904400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
905400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
9062bf23461SJindong Yue  * @ptr: the resource entry
9072e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
908fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
909400e64dfSOhad Ben-Cohen  *
910400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
911400e64dfSOhad Ben-Cohen  * contiguous memory regions.
912400e64dfSOhad Ben-Cohen  *
913400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
914400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
915400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
916400e64dfSOhad Ben-Cohen  *
917400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
918400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
919400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
920400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
921400e64dfSOhad Ben-Cohen  */
922fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
9232bf23461SJindong Yue 				 void *ptr, int offset, int avail)
924400e64dfSOhad Ben-Cohen {
9252bf23461SJindong Yue 	struct fw_rsc_carveout *rsc = ptr;
926d7c51706SLoic Pallardy 	struct rproc_mem_entry *carveout;
927b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
928400e64dfSOhad Ben-Cohen 
929fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
930b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
931fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
932fd2c15ecSOhad Ben-Cohen 	}
933fd2c15ecSOhad Ben-Cohen 
934fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
935fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
936fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
937fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
938fd2c15ecSOhad Ben-Cohen 	}
939fd2c15ecSOhad Ben-Cohen 
9409d7814a9SAnna, Suman 	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
94135386166SLee Jones 		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
942fd2c15ecSOhad Ben-Cohen 
943ffa5f9c8SLoic Pallardy 	/*
944ffa5f9c8SLoic Pallardy 	 * Check carveout rsc already part of a registered carveout,
945ffa5f9c8SLoic Pallardy 	 * Search by name, then check the da and length
946ffa5f9c8SLoic Pallardy 	 */
947ffa5f9c8SLoic Pallardy 	carveout = rproc_find_carveout_by_name(rproc, rsc->name);
948ffa5f9c8SLoic Pallardy 
949ffa5f9c8SLoic Pallardy 	if (carveout) {
950ffa5f9c8SLoic Pallardy 		if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
951ffa5f9c8SLoic Pallardy 			dev_err(dev,
952ffa5f9c8SLoic Pallardy 				"Carveout already associated to resource table\n");
953ffa5f9c8SLoic Pallardy 			return -ENOMEM;
954ffa5f9c8SLoic Pallardy 		}
955ffa5f9c8SLoic Pallardy 
956ffa5f9c8SLoic Pallardy 		if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len))
957ffa5f9c8SLoic Pallardy 			return -ENOMEM;
958ffa5f9c8SLoic Pallardy 
959ffa5f9c8SLoic Pallardy 		/* Update memory carveout with resource table info */
960ffa5f9c8SLoic Pallardy 		carveout->rsc_offset = offset;
961ffa5f9c8SLoic Pallardy 		carveout->flags = rsc->flags;
962ffa5f9c8SLoic Pallardy 
963ffa5f9c8SLoic Pallardy 		return 0;
964ffa5f9c8SLoic Pallardy 	}
965ffa5f9c8SLoic Pallardy 
966d7c51706SLoic Pallardy 	/* Register carveout in in list */
96799cf0361SBen Dooks (Codethink) 	carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
968d7c51706SLoic Pallardy 					rproc_alloc_carveout,
969d7c51706SLoic Pallardy 					rproc_release_carveout, rsc->name);
970d7c51706SLoic Pallardy 	if (!carveout) {
971d7c51706SLoic Pallardy 		dev_err(dev, "Can't allocate memory entry structure\n");
97272029c90SLoic Pallardy 		return -ENOMEM;
973400e64dfSOhad Ben-Cohen 	}
974400e64dfSOhad Ben-Cohen 
975d7c51706SLoic Pallardy 	carveout->flags = rsc->flags;
976d7c51706SLoic Pallardy 	carveout->rsc_offset = offset;
97715c0b025SLoic Pallardy 	rproc_add_carveout(rproc, carveout);
978400e64dfSOhad Ben-Cohen 
979400e64dfSOhad Ben-Cohen 	return 0;
980400e64dfSOhad Ben-Cohen }
981400e64dfSOhad Ben-Cohen 
98272029c90SLoic Pallardy /**
98315c0b025SLoic Pallardy  * rproc_add_carveout() - register an allocated carveout region
98415c0b025SLoic Pallardy  * @rproc: rproc handle
98515c0b025SLoic Pallardy  * @mem: memory entry to register
98615c0b025SLoic Pallardy  *
98715c0b025SLoic Pallardy  * This function registers specified memory entry in @rproc carveouts list.
98815c0b025SLoic Pallardy  * Specified carveout should have been allocated before registering.
98915c0b025SLoic Pallardy  */
99015c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
99115c0b025SLoic Pallardy {
99215c0b025SLoic Pallardy 	list_add_tail(&mem->node, &rproc->carveouts);
99315c0b025SLoic Pallardy }
99415c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout);
99515c0b025SLoic Pallardy 
99615c0b025SLoic Pallardy /**
99772029c90SLoic Pallardy  * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
99872029c90SLoic Pallardy  * @dev: pointer on device struct
99972029c90SLoic Pallardy  * @va: virtual address
100072029c90SLoic Pallardy  * @dma: dma address
100172029c90SLoic Pallardy  * @len: memory carveout length
100272029c90SLoic Pallardy  * @da: device address
1003a9f6fe0dSLoic Pallardy  * @alloc: memory carveout allocation function
1004a9f6fe0dSLoic Pallardy  * @release: memory carveout release function
100572029c90SLoic Pallardy  * @name: carveout name
100672029c90SLoic Pallardy  *
100772029c90SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
100872029c90SLoic Pallardy  * provided by client.
100972029c90SLoic Pallardy  */
10107e05c8deSLee Jones __printf(8, 9)
101172029c90SLoic Pallardy struct rproc_mem_entry *
101272029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev,
1013096ee786SClement Leger 		     void *va, dma_addr_t dma, size_t len, u32 da,
1014d7c51706SLoic Pallardy 		     int (*alloc)(struct rproc *, struct rproc_mem_entry *),
101572029c90SLoic Pallardy 		     int (*release)(struct rproc *, struct rproc_mem_entry *),
101672029c90SLoic Pallardy 		     const char *name, ...)
101772029c90SLoic Pallardy {
101872029c90SLoic Pallardy 	struct rproc_mem_entry *mem;
101972029c90SLoic Pallardy 	va_list args;
102072029c90SLoic Pallardy 
102172029c90SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
102272029c90SLoic Pallardy 	if (!mem)
102372029c90SLoic Pallardy 		return mem;
102472029c90SLoic Pallardy 
102572029c90SLoic Pallardy 	mem->va = va;
102672029c90SLoic Pallardy 	mem->dma = dma;
102772029c90SLoic Pallardy 	mem->da = da;
102872029c90SLoic Pallardy 	mem->len = len;
1029d7c51706SLoic Pallardy 	mem->alloc = alloc;
103072029c90SLoic Pallardy 	mem->release = release;
1031d7c51706SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
10321429cca1SLoic Pallardy 	mem->of_resm_idx = -1;
103372029c90SLoic Pallardy 
103472029c90SLoic Pallardy 	va_start(args, name);
103572029c90SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
103672029c90SLoic Pallardy 	va_end(args);
103772029c90SLoic Pallardy 
103872029c90SLoic Pallardy 	return mem;
103972029c90SLoic Pallardy }
104072029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init);
104172029c90SLoic Pallardy 
104272029c90SLoic Pallardy /**
10431429cca1SLoic Pallardy  * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
10441429cca1SLoic Pallardy  * from a reserved memory phandle
10451429cca1SLoic Pallardy  * @dev: pointer on device struct
10461429cca1SLoic Pallardy  * @of_resm_idx: reserved memory phandle index in "memory-region"
10471429cca1SLoic Pallardy  * @len: memory carveout length
10481429cca1SLoic Pallardy  * @da: device address
10491429cca1SLoic Pallardy  * @name: carveout name
10501429cca1SLoic Pallardy  *
10511429cca1SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
10521429cca1SLoic Pallardy  * provided by client.
10531429cca1SLoic Pallardy  */
10547e05c8deSLee Jones __printf(5, 6)
10551429cca1SLoic Pallardy struct rproc_mem_entry *
1056096ee786SClement Leger rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len,
10571429cca1SLoic Pallardy 			     u32 da, const char *name, ...)
10581429cca1SLoic Pallardy {
10591429cca1SLoic Pallardy 	struct rproc_mem_entry *mem;
10601429cca1SLoic Pallardy 	va_list args;
10611429cca1SLoic Pallardy 
10621429cca1SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
10631429cca1SLoic Pallardy 	if (!mem)
10641429cca1SLoic Pallardy 		return mem;
10651429cca1SLoic Pallardy 
10661429cca1SLoic Pallardy 	mem->da = da;
10671429cca1SLoic Pallardy 	mem->len = len;
10681429cca1SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
10691429cca1SLoic Pallardy 	mem->of_resm_idx = of_resm_idx;
10701429cca1SLoic Pallardy 
10711429cca1SLoic Pallardy 	va_start(args, name);
10721429cca1SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
10731429cca1SLoic Pallardy 	va_end(args);
10741429cca1SLoic Pallardy 
10751429cca1SLoic Pallardy 	return mem;
10761429cca1SLoic Pallardy }
10771429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init);
10781429cca1SLoic Pallardy 
1079a8aa5ee1SSuman Anna /**
1080a8aa5ee1SSuman Anna  * rproc_of_parse_firmware() - parse and return the firmware-name
1081a8aa5ee1SSuman Anna  * @dev: pointer on device struct representing a rproc
1082a8aa5ee1SSuman Anna  * @index: index to use for the firmware-name retrieval
1083a8aa5ee1SSuman Anna  * @fw_name: pointer to a character string, in which the firmware
1084a8aa5ee1SSuman Anna  *           name is returned on success and unmodified otherwise.
1085a8aa5ee1SSuman Anna  *
1086a8aa5ee1SSuman Anna  * This is an OF helper function that parses a device's DT node for
1087a8aa5ee1SSuman Anna  * the "firmware-name" property and returns the firmware name pointer
1088a8aa5ee1SSuman Anna  * in @fw_name on success.
1089a8aa5ee1SSuman Anna  *
1090a8aa5ee1SSuman Anna  * Return: 0 on success, or an appropriate failure.
1091a8aa5ee1SSuman Anna  */
1092a8aa5ee1SSuman Anna int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name)
1093a8aa5ee1SSuman Anna {
1094a8aa5ee1SSuman Anna 	int ret;
1095a8aa5ee1SSuman Anna 
1096a8aa5ee1SSuman Anna 	ret = of_property_read_string_index(dev->of_node, "firmware-name",
1097a8aa5ee1SSuman Anna 					    index, fw_name);
1098a8aa5ee1SSuman Anna 	return ret ? ret : 0;
1099a8aa5ee1SSuman Anna }
1100a8aa5ee1SSuman Anna EXPORT_SYMBOL(rproc_of_parse_firmware);
1101a8aa5ee1SSuman Anna 
11022e7d4c2cSArnaud Pouliquen /*
1103e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
1104e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
1105e12bc14bSOhad Ben-Cohen  */
1106232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
11072bf23461SJindong Yue 	[RSC_CARVEOUT] = rproc_handle_carveout,
11082bf23461SJindong Yue 	[RSC_DEVMEM] = rproc_handle_devmem,
11092bf23461SJindong Yue 	[RSC_TRACE] = rproc_handle_trace,
11102bf23461SJindong Yue 	[RSC_VDEV] = rproc_handle_vdev,
1111232fcdbbSSjur Brændeland };
1112232fcdbbSSjur Brændeland 
1113400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
1114a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc,
1115232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
1116400e64dfSOhad Ben-Cohen {
1117b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1118e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
1119fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
1120400e64dfSOhad Ben-Cohen 
1121d4bb86f2SBjorn Andersson 	if (!rproc->table_ptr)
1122d4bb86f2SBjorn Andersson 		return 0;
1123d4bb86f2SBjorn Andersson 
1124a2b950acSOhad Ben-Cohen 	for (i = 0; i < rproc->table_ptr->num; i++) {
1125a2b950acSOhad Ben-Cohen 		int offset = rproc->table_ptr->offset[i];
1126a2b950acSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
1127a4b24c75SBjorn Andersson 		int avail = rproc->table_sz - offset - sizeof(*hdr);
1128fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
1129400e64dfSOhad Ben-Cohen 
1130fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
1131fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
1132fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
1133fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
1134fd2c15ecSOhad Ben-Cohen 		}
1135fd2c15ecSOhad Ben-Cohen 
1136fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
1137fd2c15ecSOhad Ben-Cohen 
1138b1a17513SClement Leger 		if (hdr->type >= RSC_VENDOR_START &&
1139b1a17513SClement Leger 		    hdr->type <= RSC_VENDOR_END) {
1140b1a17513SClement Leger 			ret = rproc_handle_rsc(rproc, hdr->type, rsc,
1141b1a17513SClement Leger 					       offset + sizeof(*hdr), avail);
1142b1a17513SClement Leger 			if (ret == RSC_HANDLED)
1143b1a17513SClement Leger 				continue;
1144b1a17513SClement Leger 			else if (ret < 0)
1145b1a17513SClement Leger 				break;
1146b1a17513SClement Leger 
1147b1a17513SClement Leger 			dev_warn(dev, "unsupported vendor resource %d\n",
1148b1a17513SClement Leger 				 hdr->type);
1149b1a17513SClement Leger 			continue;
1150b1a17513SClement Leger 		}
1151b1a17513SClement Leger 
1152fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
1153fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
1154e12bc14bSOhad Ben-Cohen 			continue;
1155400e64dfSOhad Ben-Cohen 		}
1156400e64dfSOhad Ben-Cohen 
1157232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
1158e12bc14bSOhad Ben-Cohen 		if (!handler)
1159e12bc14bSOhad Ben-Cohen 			continue;
1160e12bc14bSOhad Ben-Cohen 
1161a2b950acSOhad Ben-Cohen 		ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
11627a186941SOhad Ben-Cohen 		if (ret)
1163400e64dfSOhad Ben-Cohen 			break;
1164400e64dfSOhad Ben-Cohen 	}
1165400e64dfSOhad Ben-Cohen 
1166400e64dfSOhad Ben-Cohen 	return ret;
1167400e64dfSOhad Ben-Cohen }
1168400e64dfSOhad Ben-Cohen 
1169c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc)
1170c455daa4SBjorn Andersson {
1171c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1172c455daa4SBjorn Andersson 	int ret;
1173c455daa4SBjorn Andersson 
1174c455daa4SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1175c455daa4SBjorn Andersson 		if (subdev->prepare) {
1176c455daa4SBjorn Andersson 			ret = subdev->prepare(subdev);
1177c455daa4SBjorn Andersson 			if (ret)
1178c455daa4SBjorn Andersson 				goto unroll_preparation;
1179c455daa4SBjorn Andersson 		}
1180c455daa4SBjorn Andersson 	}
1181c455daa4SBjorn Andersson 
1182c455daa4SBjorn Andersson 	return 0;
1183c455daa4SBjorn Andersson 
1184c455daa4SBjorn Andersson unroll_preparation:
1185c455daa4SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1186c455daa4SBjorn Andersson 		if (subdev->unprepare)
1187c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1188c455daa4SBjorn Andersson 	}
1189c455daa4SBjorn Andersson 
1190c455daa4SBjorn Andersson 	return ret;
1191c455daa4SBjorn Andersson }
1192c455daa4SBjorn Andersson 
1193618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc)
11947bdc9650SBjorn Andersson {
11957bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
11967bdc9650SBjorn Andersson 	int ret;
11977bdc9650SBjorn Andersson 
11987bdc9650SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1199be37b1e0SBjorn Andersson 		if (subdev->start) {
1200618fcff3SBjorn Andersson 			ret = subdev->start(subdev);
12017bdc9650SBjorn Andersson 			if (ret)
12027bdc9650SBjorn Andersson 				goto unroll_registration;
12037bdc9650SBjorn Andersson 		}
1204be37b1e0SBjorn Andersson 	}
12057bdc9650SBjorn Andersson 
12067bdc9650SBjorn Andersson 	return 0;
12077bdc9650SBjorn Andersson 
12087bdc9650SBjorn Andersson unroll_registration:
1209be37b1e0SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1210be37b1e0SBjorn Andersson 		if (subdev->stop)
1211618fcff3SBjorn Andersson 			subdev->stop(subdev, true);
1212be37b1e0SBjorn Andersson 	}
12137bdc9650SBjorn Andersson 
12147bdc9650SBjorn Andersson 	return ret;
12157bdc9650SBjorn Andersson }
12167bdc9650SBjorn Andersson 
1217618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
12187bdc9650SBjorn Andersson {
12197bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
12207bdc9650SBjorn Andersson 
1221be37b1e0SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1222be37b1e0SBjorn Andersson 		if (subdev->stop)
1223618fcff3SBjorn Andersson 			subdev->stop(subdev, crashed);
12247bdc9650SBjorn Andersson 	}
1225be37b1e0SBjorn Andersson }
12267bdc9650SBjorn Andersson 
1227c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc)
1228c455daa4SBjorn Andersson {
1229c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1230c455daa4SBjorn Andersson 
1231c455daa4SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1232c455daa4SBjorn Andersson 		if (subdev->unprepare)
1233c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1234c455daa4SBjorn Andersson 	}
1235c455daa4SBjorn Andersson }
1236c455daa4SBjorn Andersson 
1237400e64dfSOhad Ben-Cohen /**
1238d7c51706SLoic Pallardy  * rproc_alloc_registered_carveouts() - allocate all carveouts registered
1239d7c51706SLoic Pallardy  * in the list
1240d7c51706SLoic Pallardy  * @rproc: the remote processor handle
1241d7c51706SLoic Pallardy  *
1242d7c51706SLoic Pallardy  * This function parses registered carveout list, performs allocation
1243d7c51706SLoic Pallardy  * if alloc() ops registered and updates resource table information
1244d7c51706SLoic Pallardy  * if rsc_offset set.
1245d7c51706SLoic Pallardy  *
1246d7c51706SLoic Pallardy  * Return: 0 on success
1247d7c51706SLoic Pallardy  */
1248d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc)
1249d7c51706SLoic Pallardy {
1250d7c51706SLoic Pallardy 	struct rproc_mem_entry *entry, *tmp;
1251d7c51706SLoic Pallardy 	struct fw_rsc_carveout *rsc;
1252d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
1253b36de8cfSLoic Pallardy 	u64 pa;
1254d7c51706SLoic Pallardy 	int ret;
1255d7c51706SLoic Pallardy 
1256d7c51706SLoic Pallardy 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1257d7c51706SLoic Pallardy 		if (entry->alloc) {
1258d7c51706SLoic Pallardy 			ret = entry->alloc(rproc, entry);
1259d7c51706SLoic Pallardy 			if (ret) {
1260d7c51706SLoic Pallardy 				dev_err(dev, "Unable to allocate carveout %s: %d\n",
1261d7c51706SLoic Pallardy 					entry->name, ret);
1262d7c51706SLoic Pallardy 				return -ENOMEM;
1263d7c51706SLoic Pallardy 			}
1264d7c51706SLoic Pallardy 		}
1265d7c51706SLoic Pallardy 
1266d7c51706SLoic Pallardy 		if (entry->rsc_offset != FW_RSC_ADDR_ANY) {
1267d7c51706SLoic Pallardy 			/* update resource table */
1268d7c51706SLoic Pallardy 			rsc = (void *)rproc->table_ptr + entry->rsc_offset;
1269d7c51706SLoic Pallardy 
1270d7c51706SLoic Pallardy 			/*
1271d7c51706SLoic Pallardy 			 * Some remote processors might need to know the pa
1272d7c51706SLoic Pallardy 			 * even though they are behind an IOMMU. E.g., OMAP4's
1273d7c51706SLoic Pallardy 			 * remote M3 processor needs this so it can control
1274d7c51706SLoic Pallardy 			 * on-chip hardware accelerators that are not behind
1275d7c51706SLoic Pallardy 			 * the IOMMU, and therefor must know the pa.
1276d7c51706SLoic Pallardy 			 *
1277d7c51706SLoic Pallardy 			 * Generally we don't want to expose physical addresses
1278d7c51706SLoic Pallardy 			 * if we don't have to (remote processors are generally
1279d7c51706SLoic Pallardy 			 * _not_ trusted), so we might want to do this only for
1280d7c51706SLoic Pallardy 			 * remote processor that _must_ have this (e.g. OMAP4's
1281d7c51706SLoic Pallardy 			 * dual M3 subsystem).
1282d7c51706SLoic Pallardy 			 *
1283d7c51706SLoic Pallardy 			 * Non-IOMMU processors might also want to have this info.
1284d7c51706SLoic Pallardy 			 * In this case, the device address and the physical address
1285d7c51706SLoic Pallardy 			 * are the same.
1286d7c51706SLoic Pallardy 			 */
1287ffa5f9c8SLoic Pallardy 
1288ffa5f9c8SLoic Pallardy 			/* Use va if defined else dma to generate pa */
1289d7c51706SLoic Pallardy 			if (entry->va)
1290b36de8cfSLoic Pallardy 				pa = (u64)rproc_va_to_pa(entry->va);
1291ffa5f9c8SLoic Pallardy 			else
1292b36de8cfSLoic Pallardy 				pa = (u64)entry->dma;
1293ffa5f9c8SLoic Pallardy 
1294b36de8cfSLoic Pallardy 			if (((u64)pa) & HIGH_BITS_MASK)
1295b36de8cfSLoic Pallardy 				dev_warn(dev,
1296b36de8cfSLoic Pallardy 					 "Physical address cast in 32bit to fit resource table format\n");
1297b36de8cfSLoic Pallardy 
1298b36de8cfSLoic Pallardy 			rsc->pa = (u32)pa;
1299ffa5f9c8SLoic Pallardy 			rsc->da = entry->da;
1300ffa5f9c8SLoic Pallardy 			rsc->len = entry->len;
1301d7c51706SLoic Pallardy 		}
1302d7c51706SLoic Pallardy 	}
1303d7c51706SLoic Pallardy 
1304d7c51706SLoic Pallardy 	return 0;
1305d7c51706SLoic Pallardy }
1306d7c51706SLoic Pallardy 
13072666ca91SSarangdhar Joshi 
13082666ca91SSarangdhar Joshi /**
1309400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
1310400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
1311400e64dfSOhad Ben-Cohen  *
1312400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
13137a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
1314400e64dfSOhad Ben-Cohen  */
1315d9473cbfSMathieu Poirier void rproc_resource_cleanup(struct rproc *rproc)
1316400e64dfSOhad Ben-Cohen {
1317400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
1318a987e6b9SLoic Pallardy 	struct rproc_debug_trace *trace, *ttmp;
1319d81fb32fSBjorn Andersson 	struct rproc_vdev *rvdev, *rvtmp;
1320b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1321400e64dfSOhad Ben-Cohen 
1322400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
1323a987e6b9SLoic Pallardy 	list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) {
1324a987e6b9SLoic Pallardy 		rproc_remove_trace_file(trace->tfile);
1325400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
1326a987e6b9SLoic Pallardy 		list_del(&trace->node);
1327a987e6b9SLoic Pallardy 		kfree(trace);
1328400e64dfSOhad Ben-Cohen 	}
1329400e64dfSOhad Ben-Cohen 
1330400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
1331400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
1332400e64dfSOhad Ben-Cohen 		size_t unmapped;
1333400e64dfSOhad Ben-Cohen 
1334400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
1335400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
1336400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
1337096ee786SClement Leger 			dev_err(dev, "failed to unmap %zx/%zu\n", entry->len,
1338400e64dfSOhad Ben-Cohen 				unmapped);
1339400e64dfSOhad Ben-Cohen 		}
1340400e64dfSOhad Ben-Cohen 
1341400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
1342400e64dfSOhad Ben-Cohen 		kfree(entry);
1343400e64dfSOhad Ben-Cohen 	}
1344b6356a01SSuman Anna 
1345b6356a01SSuman Anna 	/* clean up carveout allocations */
1346b6356a01SSuman Anna 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1347f2e74abfSLoic Pallardy 		if (entry->release)
1348f2e74abfSLoic Pallardy 			entry->release(rproc, entry);
1349b6356a01SSuman Anna 		list_del(&entry->node);
1350b6356a01SSuman Anna 		kfree(entry);
1351b6356a01SSuman Anna 	}
1352d81fb32fSBjorn Andersson 
1353d81fb32fSBjorn Andersson 	/* clean up remote vdev entries */
1354f5bcb353SBjorn Andersson 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
13552b45cef5SBjorn Andersson 		kref_put(&rvdev->refcount, rproc_vdev_release);
13562666ca91SSarangdhar Joshi 
13572666ca91SSarangdhar Joshi 	rproc_coredump_cleanup(rproc);
13582b45cef5SBjorn Andersson }
1359d9473cbfSMathieu Poirier EXPORT_SYMBOL(rproc_resource_cleanup);
1360400e64dfSOhad Ben-Cohen 
13611efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw)
13621efa30d0SSarangdhar Joshi {
1363a4b24c75SBjorn Andersson 	struct resource_table *loaded_table;
13641efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
1365a4b24c75SBjorn Andersson 	int ret;
13661efa30d0SSarangdhar Joshi 
13671efa30d0SSarangdhar Joshi 	/* load the ELF segments to memory */
13681efa30d0SSarangdhar Joshi 	ret = rproc_load_segments(rproc, fw);
13691efa30d0SSarangdhar Joshi 	if (ret) {
13701efa30d0SSarangdhar Joshi 		dev_err(dev, "Failed to load program segments: %d\n", ret);
13711efa30d0SSarangdhar Joshi 		return ret;
13721efa30d0SSarangdhar Joshi 	}
13731efa30d0SSarangdhar Joshi 
13741efa30d0SSarangdhar Joshi 	/*
13751efa30d0SSarangdhar Joshi 	 * The starting device has been given the rproc->cached_table as the
13761efa30d0SSarangdhar Joshi 	 * resource table. The address of the vring along with the other
13771efa30d0SSarangdhar Joshi 	 * allocated resources (carveouts etc) is stored in cached_table.
13781efa30d0SSarangdhar Joshi 	 * In order to pass this information to the remote device we must copy
13791efa30d0SSarangdhar Joshi 	 * this information to device memory. We also update the table_ptr so
13801efa30d0SSarangdhar Joshi 	 * that any subsequent changes will be applied to the loaded version.
13811efa30d0SSarangdhar Joshi 	 */
13821efa30d0SSarangdhar Joshi 	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
13831efa30d0SSarangdhar Joshi 	if (loaded_table) {
1384a4b24c75SBjorn Andersson 		memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
13851efa30d0SSarangdhar Joshi 		rproc->table_ptr = loaded_table;
13861efa30d0SSarangdhar Joshi 	}
13871efa30d0SSarangdhar Joshi 
1388c455daa4SBjorn Andersson 	ret = rproc_prepare_subdevices(rproc);
1389c455daa4SBjorn Andersson 	if (ret) {
1390c455daa4SBjorn Andersson 		dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1391c455daa4SBjorn Andersson 			rproc->name, ret);
1392f68d51bdSSuman Anna 		goto reset_table_ptr;
1393c455daa4SBjorn Andersson 	}
1394c455daa4SBjorn Andersson 
13951efa30d0SSarangdhar Joshi 	/* power up the remote processor */
13961efa30d0SSarangdhar Joshi 	ret = rproc->ops->start(rproc);
13971efa30d0SSarangdhar Joshi 	if (ret) {
13981efa30d0SSarangdhar Joshi 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1399c455daa4SBjorn Andersson 		goto unprepare_subdevices;
14001efa30d0SSarangdhar Joshi 	}
14011efa30d0SSarangdhar Joshi 
1402618fcff3SBjorn Andersson 	/* Start any subdevices for the remote processor */
1403618fcff3SBjorn Andersson 	ret = rproc_start_subdevices(rproc);
14041efa30d0SSarangdhar Joshi 	if (ret) {
14051efa30d0SSarangdhar Joshi 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
14061efa30d0SSarangdhar Joshi 			rproc->name, ret);
1407c455daa4SBjorn Andersson 		goto stop_rproc;
14081efa30d0SSarangdhar Joshi 	}
14091efa30d0SSarangdhar Joshi 
14101efa30d0SSarangdhar Joshi 	rproc->state = RPROC_RUNNING;
14111efa30d0SSarangdhar Joshi 
14121efa30d0SSarangdhar Joshi 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
14131efa30d0SSarangdhar Joshi 
14141efa30d0SSarangdhar Joshi 	return 0;
1415c455daa4SBjorn Andersson 
1416c455daa4SBjorn Andersson stop_rproc:
1417c455daa4SBjorn Andersson 	rproc->ops->stop(rproc);
1418c455daa4SBjorn Andersson unprepare_subdevices:
1419c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1420f68d51bdSSuman Anna reset_table_ptr:
1421f68d51bdSSuman Anna 	rproc->table_ptr = rproc->cached_table;
1422c455daa4SBjorn Andersson 
1423c455daa4SBjorn Andersson 	return ret;
14241efa30d0SSarangdhar Joshi }
14251efa30d0SSarangdhar Joshi 
14266a6c4dc0SMathieu Poirier static int __rproc_attach(struct rproc *rproc)
1427d848a481SMathieu Poirier {
1428d848a481SMathieu Poirier 	struct device *dev = &rproc->dev;
1429d848a481SMathieu Poirier 	int ret;
1430d848a481SMathieu Poirier 
1431d848a481SMathieu Poirier 	ret = rproc_prepare_subdevices(rproc);
1432d848a481SMathieu Poirier 	if (ret) {
1433d848a481SMathieu Poirier 		dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1434d848a481SMathieu Poirier 			rproc->name, ret);
1435d848a481SMathieu Poirier 		goto out;
1436d848a481SMathieu Poirier 	}
1437d848a481SMathieu Poirier 
1438d848a481SMathieu Poirier 	/* Attach to the remote processor */
1439d848a481SMathieu Poirier 	ret = rproc_attach_device(rproc);
1440d848a481SMathieu Poirier 	if (ret) {
1441d848a481SMathieu Poirier 		dev_err(dev, "can't attach to rproc %s: %d\n",
1442d848a481SMathieu Poirier 			rproc->name, ret);
1443d848a481SMathieu Poirier 		goto unprepare_subdevices;
1444d848a481SMathieu Poirier 	}
1445d848a481SMathieu Poirier 
1446d848a481SMathieu Poirier 	/* Start any subdevices for the remote processor */
1447d848a481SMathieu Poirier 	ret = rproc_start_subdevices(rproc);
1448d848a481SMathieu Poirier 	if (ret) {
1449d848a481SMathieu Poirier 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
1450d848a481SMathieu Poirier 			rproc->name, ret);
1451d848a481SMathieu Poirier 		goto stop_rproc;
1452d848a481SMathieu Poirier 	}
1453d848a481SMathieu Poirier 
145476f4c875SMathieu Poirier 	rproc->state = RPROC_ATTACHED;
1455d848a481SMathieu Poirier 
1456d848a481SMathieu Poirier 	dev_info(dev, "remote processor %s is now attached\n", rproc->name);
1457d848a481SMathieu Poirier 
1458d848a481SMathieu Poirier 	return 0;
1459d848a481SMathieu Poirier 
1460d848a481SMathieu Poirier stop_rproc:
1461d848a481SMathieu Poirier 	rproc->ops->stop(rproc);
1462d848a481SMathieu Poirier unprepare_subdevices:
1463d848a481SMathieu Poirier 	rproc_unprepare_subdevices(rproc);
1464d848a481SMathieu Poirier out:
1465d848a481SMathieu Poirier 	return ret;
1466d848a481SMathieu Poirier }
1467d848a481SMathieu Poirier 
1468400e64dfSOhad Ben-Cohen /*
1469400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
1470400e64dfSOhad Ben-Cohen  */
1471400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1472400e64dfSOhad Ben-Cohen {
1473b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1474400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
147558b64090SBjorn Andersson 	int ret;
1476400e64dfSOhad Ben-Cohen 
1477400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
1478400e64dfSOhad Ben-Cohen 	if (ret)
1479400e64dfSOhad Ben-Cohen 		return ret;
1480400e64dfSOhad Ben-Cohen 
1481e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
1482400e64dfSOhad Ben-Cohen 
1483400e64dfSOhad Ben-Cohen 	/*
1484400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1485400e64dfSOhad Ben-Cohen 	 * just a nop
1486400e64dfSOhad Ben-Cohen 	 */
1487400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
1488400e64dfSOhad Ben-Cohen 	if (ret) {
1489400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
149049cff125SMathieu Poirier 		return ret;
1491400e64dfSOhad Ben-Cohen 	}
1492400e64dfSOhad Ben-Cohen 
149333467ac3SLoic Pallardy 	/* Prepare rproc for firmware loading if needed */
149433467ac3SLoic Pallardy 	ret = rproc_prepare_device(rproc);
149533467ac3SLoic Pallardy 	if (ret) {
149633467ac3SLoic Pallardy 		dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
149733467ac3SLoic Pallardy 		goto disable_iommu;
149833467ac3SLoic Pallardy 	}
149933467ac3SLoic Pallardy 
15003e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
1501400e64dfSOhad Ben-Cohen 
1502c1d35c1aSBjorn Andersson 	/* Load resource table, core dump segment list etc from the firmware */
1503c1d35c1aSBjorn Andersson 	ret = rproc_parse_fw(rproc, fw);
150458b64090SBjorn Andersson 	if (ret)
150533467ac3SLoic Pallardy 		goto unprepare_rproc;
1506a0c10687SBjorn Andersson 
1507b35d7afcSBjorn Andersson 	/* reset max_notifyid */
1508b35d7afcSBjorn Andersson 	rproc->max_notifyid = -1;
1509b35d7afcSBjorn Andersson 
1510c6aed238SLoic Pallardy 	/* reset handled vdev */
1511c6aed238SLoic Pallardy 	rproc->nb_vdev = 0;
1512c6aed238SLoic Pallardy 
1513400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
1514a4b24c75SBjorn Andersson 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1515400e64dfSOhad Ben-Cohen 	if (ret) {
1516400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
1517229b85a6SBjorn Andersson 		goto clean_up_resources;
1518400e64dfSOhad Ben-Cohen 	}
1519400e64dfSOhad Ben-Cohen 
1520d7c51706SLoic Pallardy 	/* Allocate carveout resources associated to rproc */
1521d7c51706SLoic Pallardy 	ret = rproc_alloc_registered_carveouts(rproc);
1522d7c51706SLoic Pallardy 	if (ret) {
1523d7c51706SLoic Pallardy 		dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1524d7c51706SLoic Pallardy 			ret);
1525d7c51706SLoic Pallardy 		goto clean_up_resources;
1526d7c51706SLoic Pallardy 	}
1527d7c51706SLoic Pallardy 
15281efa30d0SSarangdhar Joshi 	ret = rproc_start(rproc, fw);
15291efa30d0SSarangdhar Joshi 	if (ret)
1530229b85a6SBjorn Andersson 		goto clean_up_resources;
1531400e64dfSOhad Ben-Cohen 
1532400e64dfSOhad Ben-Cohen 	return 0;
1533400e64dfSOhad Ben-Cohen 
1534229b85a6SBjorn Andersson clean_up_resources:
1535229b85a6SBjorn Andersson 	rproc_resource_cleanup(rproc);
1536a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1537a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1538988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
153933467ac3SLoic Pallardy unprepare_rproc:
154033467ac3SLoic Pallardy 	/* release HW resources if needed */
154133467ac3SLoic Pallardy 	rproc_unprepare_device(rproc);
154258b64090SBjorn Andersson disable_iommu:
1543400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1544400e64dfSOhad Ben-Cohen 	return ret;
1545400e64dfSOhad Ben-Cohen }
1546400e64dfSOhad Ben-Cohen 
15471a631382SMathieu Poirier static int rproc_set_rsc_table(struct rproc *rproc)
15481a631382SMathieu Poirier {
15491a631382SMathieu Poirier 	struct resource_table *table_ptr;
15501a631382SMathieu Poirier 	struct device *dev = &rproc->dev;
15511a631382SMathieu Poirier 	size_t table_sz;
15521a631382SMathieu Poirier 	int ret;
15531a631382SMathieu Poirier 
15541a631382SMathieu Poirier 	table_ptr = rproc_get_loaded_rsc_table(rproc, &table_sz);
15551a631382SMathieu Poirier 	if (!table_ptr) {
15561a631382SMathieu Poirier 		/* Not having a resource table is acceptable */
15571a631382SMathieu Poirier 		return 0;
15581a631382SMathieu Poirier 	}
15591a631382SMathieu Poirier 
15601a631382SMathieu Poirier 	if (IS_ERR(table_ptr)) {
15611a631382SMathieu Poirier 		ret = PTR_ERR(table_ptr);
15621a631382SMathieu Poirier 		dev_err(dev, "can't load resource table: %d\n", ret);
15631a631382SMathieu Poirier 		return ret;
15641a631382SMathieu Poirier 	}
15651a631382SMathieu Poirier 
15669dc9507fSMathieu Poirier 	/*
15679dc9507fSMathieu Poirier 	 * If it is possible to detach the remote processor, keep an untouched
15689dc9507fSMathieu Poirier 	 * copy of the resource table.  That way we can start fresh again when
15699dc9507fSMathieu Poirier 	 * the remote processor is re-attached, that is:
15709dc9507fSMathieu Poirier 	 *
15719dc9507fSMathieu Poirier 	 *      DETACHED -> ATTACHED -> DETACHED -> ATTACHED
15729dc9507fSMathieu Poirier 	 *
15739dc9507fSMathieu Poirier 	 * Free'd in rproc_reset_rsc_table_on_detach() and
15749dc9507fSMathieu Poirier 	 * rproc_reset_rsc_table_on_stop().
15759dc9507fSMathieu Poirier 	 */
15769dc9507fSMathieu Poirier 	if (rproc->ops->detach) {
15779dc9507fSMathieu Poirier 		rproc->clean_table = kmemdup(table_ptr, table_sz, GFP_KERNEL);
15789dc9507fSMathieu Poirier 		if (!rproc->clean_table)
15799dc9507fSMathieu Poirier 			return -ENOMEM;
15809dc9507fSMathieu Poirier 	} else {
15819dc9507fSMathieu Poirier 		rproc->clean_table = NULL;
15829dc9507fSMathieu Poirier 	}
15839dc9507fSMathieu Poirier 
15841a631382SMathieu Poirier 	rproc->cached_table = NULL;
15851a631382SMathieu Poirier 	rproc->table_ptr = table_ptr;
15861a631382SMathieu Poirier 	rproc->table_sz = table_sz;
15871a631382SMathieu Poirier 
15881a631382SMathieu Poirier 	return 0;
15891a631382SMathieu Poirier }
15901a631382SMathieu Poirier 
15919dc9507fSMathieu Poirier static int rproc_reset_rsc_table_on_detach(struct rproc *rproc)
15929dc9507fSMathieu Poirier {
15939dc9507fSMathieu Poirier 	struct resource_table *table_ptr;
15949dc9507fSMathieu Poirier 
15959dc9507fSMathieu Poirier 	/* A resource table was never retrieved, nothing to do here */
15969dc9507fSMathieu Poirier 	if (!rproc->table_ptr)
15979dc9507fSMathieu Poirier 		return 0;
15989dc9507fSMathieu Poirier 
15999dc9507fSMathieu Poirier 	/*
16009dc9507fSMathieu Poirier 	 * If we made it to this point a clean_table _must_ have been
16019dc9507fSMathieu Poirier 	 * allocated in rproc_set_rsc_table().  If one isn't present
16029dc9507fSMathieu Poirier 	 * something went really wrong and we must complain.
16039dc9507fSMathieu Poirier 	 */
16049dc9507fSMathieu Poirier 	if (WARN_ON(!rproc->clean_table))
16059dc9507fSMathieu Poirier 		return -EINVAL;
16069dc9507fSMathieu Poirier 
16079dc9507fSMathieu Poirier 	/* Remember where the external entity installed the resource table */
16089dc9507fSMathieu Poirier 	table_ptr = rproc->table_ptr;
16099dc9507fSMathieu Poirier 
16109dc9507fSMathieu Poirier 	/*
16119dc9507fSMathieu Poirier 	 * If we made it here the remote processor was started by another
16129dc9507fSMathieu Poirier 	 * entity and a cache table doesn't exist.  As such make a copy of
16139dc9507fSMathieu Poirier 	 * the resource table currently used by the remote processor and
16149dc9507fSMathieu Poirier 	 * use that for the rest of the shutdown process.  The memory
16159dc9507fSMathieu Poirier 	 * allocated here is free'd in rproc_detach().
16169dc9507fSMathieu Poirier 	 */
16179dc9507fSMathieu Poirier 	rproc->cached_table = kmemdup(rproc->table_ptr,
16189dc9507fSMathieu Poirier 				      rproc->table_sz, GFP_KERNEL);
16199dc9507fSMathieu Poirier 	if (!rproc->cached_table)
16209dc9507fSMathieu Poirier 		return -ENOMEM;
16219dc9507fSMathieu Poirier 
16229dc9507fSMathieu Poirier 	/*
16239dc9507fSMathieu Poirier 	 * Use a copy of the resource table for the remainder of the
16249dc9507fSMathieu Poirier 	 * shutdown process.
16259dc9507fSMathieu Poirier 	 */
16269dc9507fSMathieu Poirier 	rproc->table_ptr = rproc->cached_table;
16279dc9507fSMathieu Poirier 
16289dc9507fSMathieu Poirier 	/*
16299dc9507fSMathieu Poirier 	 * Reset the memory area where the firmware loaded the resource table
16309dc9507fSMathieu Poirier 	 * to its original value.  That way when we re-attach the remote
16319dc9507fSMathieu Poirier 	 * processor the resource table is clean and ready to be used again.
16329dc9507fSMathieu Poirier 	 */
16339dc9507fSMathieu Poirier 	memcpy(table_ptr, rproc->clean_table, rproc->table_sz);
16349dc9507fSMathieu Poirier 
16359dc9507fSMathieu Poirier 	/*
16369dc9507fSMathieu Poirier 	 * The clean resource table is no longer needed.  Allocated in
16379dc9507fSMathieu Poirier 	 * rproc_set_rsc_table().
16389dc9507fSMathieu Poirier 	 */
16399dc9507fSMathieu Poirier 	kfree(rproc->clean_table);
16409dc9507fSMathieu Poirier 
16419dc9507fSMathieu Poirier 	return 0;
16429dc9507fSMathieu Poirier }
16439dc9507fSMathieu Poirier 
16448088dd4dSMathieu Poirier static int rproc_reset_rsc_table_on_stop(struct rproc *rproc)
16458088dd4dSMathieu Poirier {
16468088dd4dSMathieu Poirier 	/* A resource table was never retrieved, nothing to do here */
16478088dd4dSMathieu Poirier 	if (!rproc->table_ptr)
16488088dd4dSMathieu Poirier 		return 0;
16498088dd4dSMathieu Poirier 
16508088dd4dSMathieu Poirier 	/*
16518088dd4dSMathieu Poirier 	 * If a cache table exists the remote processor was started by
16528088dd4dSMathieu Poirier 	 * the remoteproc core.  That cache table should be used for
16538088dd4dSMathieu Poirier 	 * the rest of the shutdown process.
16548088dd4dSMathieu Poirier 	 */
16558088dd4dSMathieu Poirier 	if (rproc->cached_table)
16568088dd4dSMathieu Poirier 		goto out;
16578088dd4dSMathieu Poirier 
16588088dd4dSMathieu Poirier 	/*
16598088dd4dSMathieu Poirier 	 * If we made it here the remote processor was started by another
16608088dd4dSMathieu Poirier 	 * entity and a cache table doesn't exist.  As such make a copy of
16618088dd4dSMathieu Poirier 	 * the resource table currently used by the remote processor and
16628088dd4dSMathieu Poirier 	 * use that for the rest of the shutdown process.  The memory
16638088dd4dSMathieu Poirier 	 * allocated here is free'd in rproc_shutdown().
16648088dd4dSMathieu Poirier 	 */
16658088dd4dSMathieu Poirier 	rproc->cached_table = kmemdup(rproc->table_ptr,
16668088dd4dSMathieu Poirier 				      rproc->table_sz, GFP_KERNEL);
16678088dd4dSMathieu Poirier 	if (!rproc->cached_table)
16688088dd4dSMathieu Poirier 		return -ENOMEM;
16698088dd4dSMathieu Poirier 
16708088dd4dSMathieu Poirier 	/*
16718088dd4dSMathieu Poirier 	 * Since the remote processor is being switched off the clean table
16728088dd4dSMathieu Poirier 	 * won't be needed.  Allocated in rproc_set_rsc_table().
16738088dd4dSMathieu Poirier 	 */
16748088dd4dSMathieu Poirier 	kfree(rproc->clean_table);
16758088dd4dSMathieu Poirier 
16768088dd4dSMathieu Poirier out:
16778088dd4dSMathieu Poirier 	/*
16788088dd4dSMathieu Poirier 	 * Use a copy of the resource table for the remainder of the
16798088dd4dSMathieu Poirier 	 * shutdown process.
16808088dd4dSMathieu Poirier 	 */
16818088dd4dSMathieu Poirier 	rproc->table_ptr = rproc->cached_table;
16828088dd4dSMathieu Poirier 	return 0;
16838088dd4dSMathieu Poirier }
16848088dd4dSMathieu Poirier 
1685400e64dfSOhad Ben-Cohen /*
1686fdf0e00eSMathieu Poirier  * Attach to remote processor - similar to rproc_fw_boot() but without
1687fdf0e00eSMathieu Poirier  * the steps that deal with the firmware image.
1688fdf0e00eSMathieu Poirier  */
16896a6c4dc0SMathieu Poirier static int rproc_attach(struct rproc *rproc)
1690fdf0e00eSMathieu Poirier {
1691fdf0e00eSMathieu Poirier 	struct device *dev = &rproc->dev;
1692fdf0e00eSMathieu Poirier 	int ret;
1693fdf0e00eSMathieu Poirier 
1694fdf0e00eSMathieu Poirier 	/*
1695fdf0e00eSMathieu Poirier 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1696fdf0e00eSMathieu Poirier 	 * just a nop
1697fdf0e00eSMathieu Poirier 	 */
1698fdf0e00eSMathieu Poirier 	ret = rproc_enable_iommu(rproc);
1699fdf0e00eSMathieu Poirier 	if (ret) {
1700fdf0e00eSMathieu Poirier 		dev_err(dev, "can't enable iommu: %d\n", ret);
1701fdf0e00eSMathieu Poirier 		return ret;
1702fdf0e00eSMathieu Poirier 	}
1703fdf0e00eSMathieu Poirier 
17046e20a051SArnaud POULIQUEN 	/* Do anything that is needed to boot the remote processor */
17056e20a051SArnaud POULIQUEN 	ret = rproc_prepare_device(rproc);
17066e20a051SArnaud POULIQUEN 	if (ret) {
17076e20a051SArnaud POULIQUEN 		dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
17086e20a051SArnaud POULIQUEN 		goto disable_iommu;
17096e20a051SArnaud POULIQUEN 	}
17106e20a051SArnaud POULIQUEN 
17111a631382SMathieu Poirier 	ret = rproc_set_rsc_table(rproc);
17121a631382SMathieu Poirier 	if (ret) {
17131a631382SMathieu Poirier 		dev_err(dev, "can't load resource table: %d\n", ret);
17146e20a051SArnaud POULIQUEN 		goto unprepare_device;
17151a631382SMathieu Poirier 	}
17161a631382SMathieu Poirier 
1717fdf0e00eSMathieu Poirier 	/* reset max_notifyid */
1718fdf0e00eSMathieu Poirier 	rproc->max_notifyid = -1;
1719fdf0e00eSMathieu Poirier 
1720fdf0e00eSMathieu Poirier 	/* reset handled vdev */
1721fdf0e00eSMathieu Poirier 	rproc->nb_vdev = 0;
1722fdf0e00eSMathieu Poirier 
1723fdf0e00eSMathieu Poirier 	/*
1724fdf0e00eSMathieu Poirier 	 * Handle firmware resources required to attach to a remote processor.
1725fdf0e00eSMathieu Poirier 	 * Because we are attaching rather than booting the remote processor,
1726fdf0e00eSMathieu Poirier 	 * we expect the platform driver to properly set rproc->table_ptr.
1727fdf0e00eSMathieu Poirier 	 */
1728fdf0e00eSMathieu Poirier 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1729fdf0e00eSMathieu Poirier 	if (ret) {
1730fdf0e00eSMathieu Poirier 		dev_err(dev, "Failed to process resources: %d\n", ret);
17316e20a051SArnaud POULIQUEN 		goto unprepare_device;
1732fdf0e00eSMathieu Poirier 	}
1733fdf0e00eSMathieu Poirier 
1734fdf0e00eSMathieu Poirier 	/* Allocate carveout resources associated to rproc */
1735fdf0e00eSMathieu Poirier 	ret = rproc_alloc_registered_carveouts(rproc);
1736fdf0e00eSMathieu Poirier 	if (ret) {
1737fdf0e00eSMathieu Poirier 		dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1738fdf0e00eSMathieu Poirier 			ret);
1739fdf0e00eSMathieu Poirier 		goto clean_up_resources;
1740fdf0e00eSMathieu Poirier 	}
1741fdf0e00eSMathieu Poirier 
17426a6c4dc0SMathieu Poirier 	ret = __rproc_attach(rproc);
1743fdf0e00eSMathieu Poirier 	if (ret)
1744fdf0e00eSMathieu Poirier 		goto clean_up_resources;
1745fdf0e00eSMathieu Poirier 
1746fdf0e00eSMathieu Poirier 	return 0;
1747fdf0e00eSMathieu Poirier 
1748fdf0e00eSMathieu Poirier clean_up_resources:
1749fdf0e00eSMathieu Poirier 	rproc_resource_cleanup(rproc);
17506e20a051SArnaud POULIQUEN unprepare_device:
17516e20a051SArnaud POULIQUEN 	/* release HW resources if needed */
17526e20a051SArnaud POULIQUEN 	rproc_unprepare_device(rproc);
1753fdf0e00eSMathieu Poirier disable_iommu:
1754fdf0e00eSMathieu Poirier 	rproc_disable_iommu(rproc);
1755fdf0e00eSMathieu Poirier 	return ret;
1756fdf0e00eSMathieu Poirier }
1757fdf0e00eSMathieu Poirier 
1758fdf0e00eSMathieu Poirier /*
17595e6533f7SSarangdhar Joshi  * take a firmware and boot it up.
1760400e64dfSOhad Ben-Cohen  *
1761400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
1762400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
1763400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
1764400e64dfSOhad Ben-Cohen  * that might be cleaner).
1765400e64dfSOhad Ben-Cohen  */
17665e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
1767400e64dfSOhad Ben-Cohen {
1768400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
1769a2b950acSOhad Ben-Cohen 
17702bfc311aSBjorn Andersson 	rproc_boot(rproc);
1771ddf71187SBjorn Andersson 
1772400e64dfSOhad Ben-Cohen 	release_firmware(fw);
1773400e64dfSOhad Ben-Cohen }
1774400e64dfSOhad Ben-Cohen 
17755e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc)
177670b85ef8SFernando Guzman Lugo {
177770b85ef8SFernando Guzman Lugo 	int ret;
177870b85ef8SFernando Guzman Lugo 
177970b85ef8SFernando Guzman Lugo 	/*
1780e3d21939SMathieu Poirier 	 * Since the remote processor is in a detached state, it has already
1781e3d21939SMathieu Poirier 	 * been booted by another entity.  As such there is no point in waiting
1782e3d21939SMathieu Poirier 	 * for a firmware image to be loaded, we can simply initiate the process
1783e3d21939SMathieu Poirier 	 * of attaching to it immediately.
1784e3d21939SMathieu Poirier 	 */
1785e3d21939SMathieu Poirier 	if (rproc->state == RPROC_DETACHED)
1786e3d21939SMathieu Poirier 		return rproc_boot(rproc);
1787e3d21939SMathieu Poirier 
1788e3d21939SMathieu Poirier 	/*
178970b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
179070b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
179170b85ef8SFernando Guzman Lugo 	 */
179270b85ef8SFernando Guzman Lugo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
179370b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
17945e6533f7SSarangdhar Joshi 				      rproc, rproc_auto_boot_callback);
17952099c77dSSarangdhar Joshi 	if (ret < 0)
179670b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
179770b85ef8SFernando Guzman Lugo 
179870b85ef8SFernando Guzman Lugo 	return ret;
179970b85ef8SFernando Guzman Lugo }
180070b85ef8SFernando Guzman Lugo 
1801880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed)
18021efa30d0SSarangdhar Joshi {
18031efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
18041efa30d0SSarangdhar Joshi 	int ret;
18051efa30d0SSarangdhar Joshi 
1806d2008a96SMathieu Poirier 	/* No need to continue if a stop() operation has not been provided */
1807d2008a96SMathieu Poirier 	if (!rproc->ops->stop)
1808d2008a96SMathieu Poirier 		return -EINVAL;
1809d2008a96SMathieu Poirier 
1810618fcff3SBjorn Andersson 	/* Stop any subdevices for the remote processor */
1811618fcff3SBjorn Andersson 	rproc_stop_subdevices(rproc, crashed);
18121efa30d0SSarangdhar Joshi 
18130a8b81cbSBjorn Andersson 	/* the installed resource table is no longer accessible */
18148088dd4dSMathieu Poirier 	ret = rproc_reset_rsc_table_on_stop(rproc);
18158088dd4dSMathieu Poirier 	if (ret) {
18168088dd4dSMathieu Poirier 		dev_err(dev, "can't reset resource table: %d\n", ret);
18178088dd4dSMathieu Poirier 		return ret;
18188088dd4dSMathieu Poirier 	}
18198088dd4dSMathieu Poirier 
18200a8b81cbSBjorn Andersson 
18211efa30d0SSarangdhar Joshi 	/* power off the remote processor */
18221efa30d0SSarangdhar Joshi 	ret = rproc->ops->stop(rproc);
18231efa30d0SSarangdhar Joshi 	if (ret) {
18241efa30d0SSarangdhar Joshi 		dev_err(dev, "can't stop rproc: %d\n", ret);
18251efa30d0SSarangdhar Joshi 		return ret;
18261efa30d0SSarangdhar Joshi 	}
18271efa30d0SSarangdhar Joshi 
1828c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1829c455daa4SBjorn Andersson 
18301efa30d0SSarangdhar Joshi 	rproc->state = RPROC_OFFLINE;
18311efa30d0SSarangdhar Joshi 
18321efa30d0SSarangdhar Joshi 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
18331efa30d0SSarangdhar Joshi 
18341efa30d0SSarangdhar Joshi 	return 0;
18351efa30d0SSarangdhar Joshi }
18361efa30d0SSarangdhar Joshi 
18376070203fSMathieu Poirier /*
18386070203fSMathieu Poirier  * __rproc_detach(): Does the opposite of __rproc_attach()
18396070203fSMathieu Poirier  */
1840d3962a39SMathieu Poirier static int __rproc_detach(struct rproc *rproc)
18416070203fSMathieu Poirier {
18426070203fSMathieu Poirier 	struct device *dev = &rproc->dev;
18436070203fSMathieu Poirier 	int ret;
18446070203fSMathieu Poirier 
18456070203fSMathieu Poirier 	/* No need to continue if a detach() operation has not been provided */
18466070203fSMathieu Poirier 	if (!rproc->ops->detach)
18476070203fSMathieu Poirier 		return -EINVAL;
18486070203fSMathieu Poirier 
18496070203fSMathieu Poirier 	/* Stop any subdevices for the remote processor */
18506070203fSMathieu Poirier 	rproc_stop_subdevices(rproc, false);
18516070203fSMathieu Poirier 
18529dc9507fSMathieu Poirier 	/* the installed resource table is no longer accessible */
18539dc9507fSMathieu Poirier 	ret = rproc_reset_rsc_table_on_detach(rproc);
18549dc9507fSMathieu Poirier 	if (ret) {
18559dc9507fSMathieu Poirier 		dev_err(dev, "can't reset resource table: %d\n", ret);
18569dc9507fSMathieu Poirier 		return ret;
18579dc9507fSMathieu Poirier 	}
18589dc9507fSMathieu Poirier 
18596070203fSMathieu Poirier 	/* Tell the remote processor the core isn't available anymore */
18606070203fSMathieu Poirier 	ret = rproc->ops->detach(rproc);
18616070203fSMathieu Poirier 	if (ret) {
18626070203fSMathieu Poirier 		dev_err(dev, "can't detach from rproc: %d\n", ret);
18636070203fSMathieu Poirier 		return ret;
18646070203fSMathieu Poirier 	}
18656070203fSMathieu Poirier 
18666070203fSMathieu Poirier 	rproc_unprepare_subdevices(rproc);
18676070203fSMathieu Poirier 
18686070203fSMathieu Poirier 	rproc->state = RPROC_DETACHED;
18696070203fSMathieu Poirier 
18706070203fSMathieu Poirier 	dev_info(dev, "detached remote processor %s\n", rproc->name);
18716070203fSMathieu Poirier 
18726070203fSMathieu Poirier 	return 0;
18736070203fSMathieu Poirier }
18742666ca91SSarangdhar Joshi 
18752666ca91SSarangdhar Joshi /**
187670b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
187770b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
187870b85ef8SFernando Guzman Lugo  *
187956324d7aSAnna, Suman  * The recovery is done by resetting all the virtio devices, that way all the
188070b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
188170b85ef8SFernando Guzman Lugo  * remoteproc functional again.
188270b85ef8SFernando Guzman Lugo  *
188370b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
188470b85ef8SFernando Guzman Lugo  */
188570b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
188670b85ef8SFernando Guzman Lugo {
18877e83cab8SSarangdhar Joshi 	const struct firmware *firmware_p;
18887e83cab8SSarangdhar Joshi 	struct device *dev = &rproc->dev;
18897e83cab8SSarangdhar Joshi 	int ret;
18907e83cab8SSarangdhar Joshi 
18917e83cab8SSarangdhar Joshi 	ret = mutex_lock_interruptible(&rproc->lock);
18927e83cab8SSarangdhar Joshi 	if (ret)
18937e83cab8SSarangdhar Joshi 		return ret;
18947e83cab8SSarangdhar Joshi 
18950b145574SAlex Elder 	/* State could have changed before we got the mutex */
18960b145574SAlex Elder 	if (rproc->state != RPROC_CRASHED)
18970b145574SAlex Elder 		goto unlock_mutex;
18980b145574SAlex Elder 
18990b145574SAlex Elder 	dev_err(dev, "recovering %s\n", rproc->name);
19000b145574SAlex Elder 
1901fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, true);
19027e83cab8SSarangdhar Joshi 	if (ret)
19037e83cab8SSarangdhar Joshi 		goto unlock_mutex;
1904ddf71187SBjorn Andersson 
19052666ca91SSarangdhar Joshi 	/* generate coredump */
1906adf60a87SSiddharth Gupta 	rproc->ops->coredump(rproc);
19072666ca91SSarangdhar Joshi 
19087e83cab8SSarangdhar Joshi 	/* load firmware */
19097e83cab8SSarangdhar Joshi 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
19107e83cab8SSarangdhar Joshi 	if (ret < 0) {
19117e83cab8SSarangdhar Joshi 		dev_err(dev, "request_firmware failed: %d\n", ret);
19127e83cab8SSarangdhar Joshi 		goto unlock_mutex;
19137e83cab8SSarangdhar Joshi 	}
1914ddf71187SBjorn Andersson 
19157e83cab8SSarangdhar Joshi 	/* boot the remote processor up again */
19167e83cab8SSarangdhar Joshi 	ret = rproc_start(rproc, firmware_p);
19177e83cab8SSarangdhar Joshi 
19187e83cab8SSarangdhar Joshi 	release_firmware(firmware_p);
19197e83cab8SSarangdhar Joshi 
19207e83cab8SSarangdhar Joshi unlock_mutex:
19217e83cab8SSarangdhar Joshi 	mutex_unlock(&rproc->lock);
19227e83cab8SSarangdhar Joshi 	return ret;
192370b85ef8SFernando Guzman Lugo }
192470b85ef8SFernando Guzman Lugo 
1925400e64dfSOhad Ben-Cohen /**
19268afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
19272e7d4c2cSArnaud Pouliquen  * @work: work treating the crash
19288afd519cSFernando Guzman Lugo  *
19298afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
19308afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
19318afd519cSFernando Guzman Lugo  */
19328afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
19338afd519cSFernando Guzman Lugo {
19348afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
19358afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
19368afd519cSFernando Guzman Lugo 
19378afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
19388afd519cSFernando Guzman Lugo 
19398afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
19408afd519cSFernando Guzman Lugo 
19418afd519cSFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
19428afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
19438afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
19448afd519cSFernando Guzman Lugo 		return;
19458afd519cSFernando Guzman Lugo 	}
19468afd519cSFernando Guzman Lugo 
19478afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
19488afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
19498afd519cSFernando Guzman Lugo 		rproc->name);
19508afd519cSFernando Guzman Lugo 
19518afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
19528afd519cSFernando Guzman Lugo 
19532e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
195470b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
1955a781e5aaSRishabh Bhatnagar 
1956a781e5aaSRishabh Bhatnagar 	pm_relax(rproc->dev.parent);
19578afd519cSFernando Guzman Lugo }
19588afd519cSFernando Guzman Lugo 
19598afd519cSFernando Guzman Lugo /**
19601b0ef906SSuman Anna  * rproc_boot() - boot a remote processor
1961400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1962400e64dfSOhad Ben-Cohen  *
1963400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1964400e64dfSOhad Ben-Cohen  *
1965400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1966400e64dfSOhad Ben-Cohen  * returns (successfully).
1967400e64dfSOhad Ben-Cohen  *
1968400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1969400e64dfSOhad Ben-Cohen  */
19701b0ef906SSuman Anna int rproc_boot(struct rproc *rproc)
1971400e64dfSOhad Ben-Cohen {
1972400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1973400e64dfSOhad Ben-Cohen 	struct device *dev;
1974400e64dfSOhad Ben-Cohen 	int ret;
1975400e64dfSOhad Ben-Cohen 
1976400e64dfSOhad Ben-Cohen 	if (!rproc) {
1977400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1978400e64dfSOhad Ben-Cohen 		return -EINVAL;
1979400e64dfSOhad Ben-Cohen 	}
1980400e64dfSOhad Ben-Cohen 
1981b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1982400e64dfSOhad Ben-Cohen 
1983400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1984400e64dfSOhad Ben-Cohen 	if (ret) {
1985400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1986400e64dfSOhad Ben-Cohen 		return ret;
1987400e64dfSOhad Ben-Cohen 	}
1988400e64dfSOhad Ben-Cohen 
19892099c77dSSarangdhar Joshi 	if (rproc->state == RPROC_DELETED) {
19902099c77dSSarangdhar Joshi 		ret = -ENODEV;
19912099c77dSSarangdhar Joshi 		dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
19922099c77dSSarangdhar Joshi 		goto unlock_mutex;
19932099c77dSSarangdhar Joshi 	}
19942099c77dSSarangdhar Joshi 
19950f9dc562SMathieu Poirier 	/* skip the boot or attach process if rproc is already powered up */
1996400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1997400e64dfSOhad Ben-Cohen 		ret = 0;
1998400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1999400e64dfSOhad Ben-Cohen 	}
2000400e64dfSOhad Ben-Cohen 
20010f9dc562SMathieu Poirier 	if (rproc->state == RPROC_DETACHED) {
20020f9dc562SMathieu Poirier 		dev_info(dev, "attaching to %s\n", rproc->name);
20030f9dc562SMathieu Poirier 
20046a6c4dc0SMathieu Poirier 		ret = rproc_attach(rproc);
20050f9dc562SMathieu Poirier 	} else {
2006400e64dfSOhad Ben-Cohen 		dev_info(dev, "powering up %s\n", rproc->name);
2007400e64dfSOhad Ben-Cohen 
2008400e64dfSOhad Ben-Cohen 		/* load firmware */
2009400e64dfSOhad Ben-Cohen 		ret = request_firmware(&firmware_p, rproc->firmware, dev);
2010400e64dfSOhad Ben-Cohen 		if (ret < 0) {
2011400e64dfSOhad Ben-Cohen 			dev_err(dev, "request_firmware failed: %d\n", ret);
2012400e64dfSOhad Ben-Cohen 			goto downref_rproc;
2013400e64dfSOhad Ben-Cohen 		}
2014400e64dfSOhad Ben-Cohen 
2015400e64dfSOhad Ben-Cohen 		ret = rproc_fw_boot(rproc, firmware_p);
2016400e64dfSOhad Ben-Cohen 
2017400e64dfSOhad Ben-Cohen 		release_firmware(firmware_p);
20180f9dc562SMathieu Poirier 	}
2019400e64dfSOhad Ben-Cohen 
2020400e64dfSOhad Ben-Cohen downref_rproc:
2021fbb6aacbSBjorn Andersson 	if (ret)
2022400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
2023400e64dfSOhad Ben-Cohen unlock_mutex:
2024400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
2025400e64dfSOhad Ben-Cohen 	return ret;
2026400e64dfSOhad Ben-Cohen }
2027400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
2028400e64dfSOhad Ben-Cohen 
2029400e64dfSOhad Ben-Cohen /**
2030400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
2031400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
2032400e64dfSOhad Ben-Cohen  *
2033400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
2034400e64dfSOhad Ben-Cohen  *
2035400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
2036400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
2037400e64dfSOhad Ben-Cohen  * without really powering off the device.
2038400e64dfSOhad Ben-Cohen  *
2039400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
2040400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
2041400e64dfSOhad Ben-Cohen  *
2042400e64dfSOhad Ben-Cohen  * Notes:
2043400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
2044400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
2045400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
2046400e64dfSOhad Ben-Cohen  *   needed.
2047400e64dfSOhad Ben-Cohen  */
2048400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
2049400e64dfSOhad Ben-Cohen {
2050b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
2051400e64dfSOhad Ben-Cohen 	int ret;
2052400e64dfSOhad Ben-Cohen 
2053400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
2054400e64dfSOhad Ben-Cohen 	if (ret) {
2055400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
2056400e64dfSOhad Ben-Cohen 		return;
2057400e64dfSOhad Ben-Cohen 	}
2058400e64dfSOhad Ben-Cohen 
2059400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
2060400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
2061400e64dfSOhad Ben-Cohen 		goto out;
2062400e64dfSOhad Ben-Cohen 
2063fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, false);
2064400e64dfSOhad Ben-Cohen 	if (ret) {
2065400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
2066400e64dfSOhad Ben-Cohen 		goto out;
2067400e64dfSOhad Ben-Cohen 	}
2068400e64dfSOhad Ben-Cohen 
2069400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
2070400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
2071400e64dfSOhad Ben-Cohen 
207233467ac3SLoic Pallardy 	/* release HW resources if needed */
207333467ac3SLoic Pallardy 	rproc_unprepare_device(rproc);
207433467ac3SLoic Pallardy 
2075400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
2076400e64dfSOhad Ben-Cohen 
2077988d204cSBjorn Andersson 	/* Free the copy of the resource table */
2078a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
2079a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
2080988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
2081400e64dfSOhad Ben-Cohen out:
2082400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
2083400e64dfSOhad Ben-Cohen }
2084400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
2085400e64dfSOhad Ben-Cohen 
2086400e64dfSOhad Ben-Cohen /**
2087d3962a39SMathieu Poirier  * rproc_detach() - Detach the remote processor from the
2088d3962a39SMathieu Poirier  * remoteproc core
2089d3962a39SMathieu Poirier  *
2090d3962a39SMathieu Poirier  * @rproc: the remote processor
2091d3962a39SMathieu Poirier  *
2092d3962a39SMathieu Poirier  * Detach a remote processor (previously attached to with rproc_attach()).
2093d3962a39SMathieu Poirier  *
2094d3962a39SMathieu Poirier  * In case @rproc is still being used by an additional user(s), then
2095d3962a39SMathieu Poirier  * this function will just decrement the power refcount and exit,
2096d3962a39SMathieu Poirier  * without disconnecting the device.
2097d3962a39SMathieu Poirier  *
2098d3962a39SMathieu Poirier  * Function rproc_detach() calls __rproc_detach() in order to let a remote
2099d3962a39SMathieu Poirier  * processor know that services provided by the application processor are
2100d3962a39SMathieu Poirier  * no longer available.  From there it should be possible to remove the
2101d3962a39SMathieu Poirier  * platform driver and even power cycle the application processor (if the HW
2102d3962a39SMathieu Poirier  * supports it) without needing to switch off the remote processor.
2103d3962a39SMathieu Poirier  */
2104d3962a39SMathieu Poirier int rproc_detach(struct rproc *rproc)
2105d3962a39SMathieu Poirier {
2106d3962a39SMathieu Poirier 	struct device *dev = &rproc->dev;
2107d3962a39SMathieu Poirier 	int ret;
2108d3962a39SMathieu Poirier 
2109d3962a39SMathieu Poirier 	ret = mutex_lock_interruptible(&rproc->lock);
2110d3962a39SMathieu Poirier 	if (ret) {
2111d3962a39SMathieu Poirier 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
2112d3962a39SMathieu Poirier 		return ret;
2113d3962a39SMathieu Poirier 	}
2114d3962a39SMathieu Poirier 
2115d3962a39SMathieu Poirier 	/* if the remote proc is still needed, bail out */
2116d3962a39SMathieu Poirier 	if (!atomic_dec_and_test(&rproc->power)) {
2117d3962a39SMathieu Poirier 		ret = 0;
2118d3962a39SMathieu Poirier 		goto out;
2119d3962a39SMathieu Poirier 	}
2120d3962a39SMathieu Poirier 
2121d3962a39SMathieu Poirier 	ret = __rproc_detach(rproc);
2122d3962a39SMathieu Poirier 	if (ret) {
2123d3962a39SMathieu Poirier 		atomic_inc(&rproc->power);
2124d3962a39SMathieu Poirier 		goto out;
2125d3962a39SMathieu Poirier 	}
2126d3962a39SMathieu Poirier 
2127d3962a39SMathieu Poirier 	/* clean up all acquired resources */
2128d3962a39SMathieu Poirier 	rproc_resource_cleanup(rproc);
2129d3962a39SMathieu Poirier 
2130d3962a39SMathieu Poirier 	/* release HW resources if needed */
2131d3962a39SMathieu Poirier 	rproc_unprepare_device(rproc);
2132d3962a39SMathieu Poirier 
2133d3962a39SMathieu Poirier 	rproc_disable_iommu(rproc);
2134d3962a39SMathieu Poirier 
21359dc9507fSMathieu Poirier 	/* Free the copy of the resource table */
21369dc9507fSMathieu Poirier 	kfree(rproc->cached_table);
21379dc9507fSMathieu Poirier 	rproc->cached_table = NULL;
2138d3962a39SMathieu Poirier 	rproc->table_ptr = NULL;
2139d3962a39SMathieu Poirier out:
2140d3962a39SMathieu Poirier 	mutex_unlock(&rproc->lock);
2141d3962a39SMathieu Poirier 	return ret;
2142d3962a39SMathieu Poirier }
2143d3962a39SMathieu Poirier EXPORT_SYMBOL(rproc_detach);
2144d3962a39SMathieu Poirier 
2145d3962a39SMathieu Poirier /**
2146fec47d86SDave Gerlach  * rproc_get_by_phandle() - find a remote processor by phandle
2147fec47d86SDave Gerlach  * @phandle: phandle to the rproc
2148fec47d86SDave Gerlach  *
2149fec47d86SDave Gerlach  * Finds an rproc handle using the remote processor's phandle, and then
2150fec47d86SDave Gerlach  * return a handle to the rproc.
2151fec47d86SDave Gerlach  *
2152fec47d86SDave Gerlach  * This function increments the remote processor's refcount, so always
2153fec47d86SDave Gerlach  * use rproc_put() to decrement it back once rproc isn't needed anymore.
2154fec47d86SDave Gerlach  *
2155fec47d86SDave Gerlach  * Returns the rproc handle on success, and NULL on failure.
2156fec47d86SDave Gerlach  */
21578de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF
2158fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle)
2159fec47d86SDave Gerlach {
2160fec47d86SDave Gerlach 	struct rproc *rproc = NULL, *r;
2161fec47d86SDave Gerlach 	struct device_node *np;
2162fec47d86SDave Gerlach 
2163fec47d86SDave Gerlach 	np = of_find_node_by_phandle(phandle);
2164fec47d86SDave Gerlach 	if (!np)
2165fec47d86SDave Gerlach 		return NULL;
2166fec47d86SDave Gerlach 
2167c0abe2caSBjorn Andersson 	rcu_read_lock();
2168c0abe2caSBjorn Andersson 	list_for_each_entry_rcu(r, &rproc_list, node) {
2169fec47d86SDave Gerlach 		if (r->dev.parent && r->dev.parent->of_node == np) {
2170fbb6aacbSBjorn Andersson 			/* prevent underlying implementation from being removed */
2171fbb6aacbSBjorn Andersson 			if (!try_module_get(r->dev.parent->driver->owner)) {
2172fbb6aacbSBjorn Andersson 				dev_err(&r->dev, "can't get owner\n");
2173fbb6aacbSBjorn Andersson 				break;
2174fbb6aacbSBjorn Andersson 			}
2175fbb6aacbSBjorn Andersson 
2176fec47d86SDave Gerlach 			rproc = r;
2177fec47d86SDave Gerlach 			get_device(&rproc->dev);
2178fec47d86SDave Gerlach 			break;
2179fec47d86SDave Gerlach 		}
2180fec47d86SDave Gerlach 	}
2181c0abe2caSBjorn Andersson 	rcu_read_unlock();
2182fec47d86SDave Gerlach 
2183fec47d86SDave Gerlach 	of_node_put(np);
2184fec47d86SDave Gerlach 
2185fec47d86SDave Gerlach 	return rproc;
2186fec47d86SDave Gerlach }
21878de3dbd0SOhad Ben-Cohen #else
21888de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle)
21898de3dbd0SOhad Ben-Cohen {
21908de3dbd0SOhad Ben-Cohen 	return NULL;
21918de3dbd0SOhad Ben-Cohen }
21928de3dbd0SOhad Ben-Cohen #endif
2193fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle);
2194fec47d86SDave Gerlach 
21954c1ad562SSuman Anna /**
21964c1ad562SSuman Anna  * rproc_set_firmware() - assign a new firmware
21974c1ad562SSuman Anna  * @rproc: rproc handle to which the new firmware is being assigned
21984c1ad562SSuman Anna  * @fw_name: new firmware name to be assigned
21994c1ad562SSuman Anna  *
22004c1ad562SSuman Anna  * This function allows remoteproc drivers or clients to configure a custom
22014c1ad562SSuman Anna  * firmware name that is different from the default name used during remoteproc
22024c1ad562SSuman Anna  * registration. The function does not trigger a remote processor boot,
22034c1ad562SSuman Anna  * only sets the firmware name used for a subsequent boot. This function
22044c1ad562SSuman Anna  * should also be called only when the remote processor is offline.
22054c1ad562SSuman Anna  *
22064c1ad562SSuman Anna  * This allows either the userspace to configure a different name through
22074c1ad562SSuman Anna  * sysfs or a kernel-level remoteproc or a remoteproc client driver to set
22084c1ad562SSuman Anna  * a specific firmware when it is controlling the boot and shutdown of the
22094c1ad562SSuman Anna  * remote processor.
22104c1ad562SSuman Anna  *
22114c1ad562SSuman Anna  * Return: 0 on success or a negative value upon failure
22124c1ad562SSuman Anna  */
22134c1ad562SSuman Anna int rproc_set_firmware(struct rproc *rproc, const char *fw_name)
22144c1ad562SSuman Anna {
22154c1ad562SSuman Anna 	struct device *dev;
22164c1ad562SSuman Anna 	int ret, len;
22174c1ad562SSuman Anna 	char *p;
22184c1ad562SSuman Anna 
22194c1ad562SSuman Anna 	if (!rproc || !fw_name)
22204c1ad562SSuman Anna 		return -EINVAL;
22214c1ad562SSuman Anna 
22224c1ad562SSuman Anna 	dev = rproc->dev.parent;
22234c1ad562SSuman Anna 
22244c1ad562SSuman Anna 	ret = mutex_lock_interruptible(&rproc->lock);
22254c1ad562SSuman Anna 	if (ret) {
22264c1ad562SSuman Anna 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
22274c1ad562SSuman Anna 		return -EINVAL;
22284c1ad562SSuman Anna 	}
22294c1ad562SSuman Anna 
22304c1ad562SSuman Anna 	if (rproc->state != RPROC_OFFLINE) {
22314c1ad562SSuman Anna 		dev_err(dev, "can't change firmware while running\n");
22324c1ad562SSuman Anna 		ret = -EBUSY;
22334c1ad562SSuman Anna 		goto out;
22344c1ad562SSuman Anna 	}
22354c1ad562SSuman Anna 
22364c1ad562SSuman Anna 	len = strcspn(fw_name, "\n");
22374c1ad562SSuman Anna 	if (!len) {
22384c1ad562SSuman Anna 		dev_err(dev, "can't provide empty string for firmware name\n");
22394c1ad562SSuman Anna 		ret = -EINVAL;
22404c1ad562SSuman Anna 		goto out;
22414c1ad562SSuman Anna 	}
22424c1ad562SSuman Anna 
22434c1ad562SSuman Anna 	p = kstrndup(fw_name, len, GFP_KERNEL);
22444c1ad562SSuman Anna 	if (!p) {
22454c1ad562SSuman Anna 		ret = -ENOMEM;
22464c1ad562SSuman Anna 		goto out;
22474c1ad562SSuman Anna 	}
22484c1ad562SSuman Anna 
224943d3f2c7SDaniele Alessandrelli 	kfree_const(rproc->firmware);
22504c1ad562SSuman Anna 	rproc->firmware = p;
22514c1ad562SSuman Anna 
22524c1ad562SSuman Anna out:
22534c1ad562SSuman Anna 	mutex_unlock(&rproc->lock);
22544c1ad562SSuman Anna 	return ret;
22554c1ad562SSuman Anna }
22564c1ad562SSuman Anna EXPORT_SYMBOL(rproc_set_firmware);
22574c1ad562SSuman Anna 
225888d3a136SMathieu Poirier static int rproc_validate(struct rproc *rproc)
225988d3a136SMathieu Poirier {
226088d3a136SMathieu Poirier 	switch (rproc->state) {
226188d3a136SMathieu Poirier 	case RPROC_OFFLINE:
226288d3a136SMathieu Poirier 		/*
226388d3a136SMathieu Poirier 		 * An offline processor without a start()
226488d3a136SMathieu Poirier 		 * function makes no sense.
226588d3a136SMathieu Poirier 		 */
226688d3a136SMathieu Poirier 		if (!rproc->ops->start)
226788d3a136SMathieu Poirier 			return -EINVAL;
226888d3a136SMathieu Poirier 		break;
226988d3a136SMathieu Poirier 	case RPROC_DETACHED:
227088d3a136SMathieu Poirier 		/*
227188d3a136SMathieu Poirier 		 * A remote processor in a detached state without an
227288d3a136SMathieu Poirier 		 * attach() function makes not sense.
227388d3a136SMathieu Poirier 		 */
227488d3a136SMathieu Poirier 		if (!rproc->ops->attach)
227588d3a136SMathieu Poirier 			return -EINVAL;
227688d3a136SMathieu Poirier 		/*
227788d3a136SMathieu Poirier 		 * When attaching to a remote processor the device memory
227888d3a136SMathieu Poirier 		 * is already available and as such there is no need to have a
227988d3a136SMathieu Poirier 		 * cached table.
228088d3a136SMathieu Poirier 		 */
228188d3a136SMathieu Poirier 		if (rproc->cached_table)
228288d3a136SMathieu Poirier 			return -EINVAL;
228388d3a136SMathieu Poirier 		break;
228488d3a136SMathieu Poirier 	default:
228588d3a136SMathieu Poirier 		/*
228688d3a136SMathieu Poirier 		 * When adding a remote processor, the state of the device
228788d3a136SMathieu Poirier 		 * can be offline or detached, nothing else.
228888d3a136SMathieu Poirier 		 */
228988d3a136SMathieu Poirier 		return -EINVAL;
229088d3a136SMathieu Poirier 	}
229188d3a136SMathieu Poirier 
229288d3a136SMathieu Poirier 	return 0;
229388d3a136SMathieu Poirier }
229488d3a136SMathieu Poirier 
2295fec47d86SDave Gerlach /**
2296160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
2297400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
2298400e64dfSOhad Ben-Cohen  *
2299400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
2300400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
2301400e64dfSOhad Ben-Cohen  *
2302400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
2303400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
2304400e64dfSOhad Ben-Cohen  *
2305400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
2306400e64dfSOhad Ben-Cohen  *
2307400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
2308400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
2309400e64dfSOhad Ben-Cohen  * firmware.
2310400e64dfSOhad Ben-Cohen  *
2311400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
23127a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
2313400e64dfSOhad Ben-Cohen  * probed.
2314400e64dfSOhad Ben-Cohen  */
2315160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
2316400e64dfSOhad Ben-Cohen {
2317b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
231870b85ef8SFernando Guzman Lugo 	int ret;
2319400e64dfSOhad Ben-Cohen 
2320b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
2321b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
2322b5ab5e24SOhad Ben-Cohen 		return ret;
2323400e64dfSOhad Ben-Cohen 
232488d3a136SMathieu Poirier 	ret = rproc_validate(rproc);
232588d3a136SMathieu Poirier 	if (ret < 0)
232688d3a136SMathieu Poirier 		return ret;
232788d3a136SMathieu Poirier 
2328b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
2329400e64dfSOhad Ben-Cohen 
2330400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
2331400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
23327a20c64dSSarangdhar Joshi 
233362b8f9e9SSiddharth Gupta 	/* add char device for this remoteproc */
233462b8f9e9SSiddharth Gupta 	ret = rproc_char_device_add(rproc);
233562b8f9e9SSiddharth Gupta 	if (ret < 0)
233662b8f9e9SSiddharth Gupta 		return ret;
233762b8f9e9SSiddharth Gupta 
23387a20c64dSSarangdhar Joshi 	/* if rproc is marked always-on, request it to boot */
23397a20c64dSSarangdhar Joshi 	if (rproc->auto_boot) {
23405e6533f7SSarangdhar Joshi 		ret = rproc_trigger_auto_boot(rproc);
2341d2e12e66SDave Gerlach 		if (ret < 0)
2342d2e12e66SDave Gerlach 			return ret;
23437a20c64dSSarangdhar Joshi 	}
2344400e64dfSOhad Ben-Cohen 
2345d2e12e66SDave Gerlach 	/* expose to rproc_get_by_phandle users */
2346d2e12e66SDave Gerlach 	mutex_lock(&rproc_list_mutex);
2347c0abe2caSBjorn Andersson 	list_add_rcu(&rproc->node, &rproc_list);
2348d2e12e66SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
2349d2e12e66SDave Gerlach 
2350d2e12e66SDave Gerlach 	return 0;
2351400e64dfSOhad Ben-Cohen }
2352160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
2353400e64dfSOhad Ben-Cohen 
2354305ac5a7SPaul Cercueil static void devm_rproc_remove(void *rproc)
2355305ac5a7SPaul Cercueil {
2356305ac5a7SPaul Cercueil 	rproc_del(rproc);
2357305ac5a7SPaul Cercueil }
2358305ac5a7SPaul Cercueil 
2359305ac5a7SPaul Cercueil /**
2360305ac5a7SPaul Cercueil  * devm_rproc_add() - resource managed rproc_add()
2361305ac5a7SPaul Cercueil  * @dev: the underlying device
2362305ac5a7SPaul Cercueil  * @rproc: the remote processor handle to register
2363305ac5a7SPaul Cercueil  *
2364305ac5a7SPaul Cercueil  * This function performs like rproc_add() but the registered rproc device will
2365305ac5a7SPaul Cercueil  * automatically be removed on driver detach.
2366305ac5a7SPaul Cercueil  *
2367305ac5a7SPaul Cercueil  * Returns: 0 on success, negative errno on failure
2368305ac5a7SPaul Cercueil  */
2369305ac5a7SPaul Cercueil int devm_rproc_add(struct device *dev, struct rproc *rproc)
2370305ac5a7SPaul Cercueil {
2371305ac5a7SPaul Cercueil 	int err;
2372305ac5a7SPaul Cercueil 
2373305ac5a7SPaul Cercueil 	err = rproc_add(rproc);
2374305ac5a7SPaul Cercueil 	if (err)
2375305ac5a7SPaul Cercueil 		return err;
2376305ac5a7SPaul Cercueil 
2377305ac5a7SPaul Cercueil 	return devm_add_action_or_reset(dev, devm_rproc_remove, rproc);
2378305ac5a7SPaul Cercueil }
2379305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_add);
2380305ac5a7SPaul Cercueil 
2381400e64dfSOhad Ben-Cohen /**
2382b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
2383b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
2384b5ab5e24SOhad Ben-Cohen  *
2385b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
2386b5ab5e24SOhad Ben-Cohen  *
2387b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
2388b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
2389b5ab5e24SOhad Ben-Cohen  */
2390b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
2391b5ab5e24SOhad Ben-Cohen {
2392b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
2393b5ab5e24SOhad Ben-Cohen 
23947183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
23957183a2a7SOhad Ben-Cohen 
2396b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
2397b5ab5e24SOhad Ben-Cohen 
2398b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
2399b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
2400b5ab5e24SOhad Ben-Cohen 
24011487dedaSMathieu Poirier 	kfree_const(rproc->firmware);
2402db655278SSuman Anna 	kfree_const(rproc->name);
2403fb98e2bdSBjorn Andersson 	kfree(rproc->ops);
2404b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
2405b5ab5e24SOhad Ben-Cohen }
2406b5ab5e24SOhad Ben-Cohen 
2407c42ca04dSBhumika Goyal static const struct device_type rproc_type = {
2408b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
2409b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
2410b5ab5e24SOhad Ben-Cohen };
2411400e64dfSOhad Ben-Cohen 
24120c2ae2b1SMathieu Poirier static int rproc_alloc_firmware(struct rproc *rproc,
24130c2ae2b1SMathieu Poirier 				const char *name, const char *firmware)
24140c2ae2b1SMathieu Poirier {
24151487dedaSMathieu Poirier 	const char *p;
24160c2ae2b1SMathieu Poirier 
24170c2ae2b1SMathieu Poirier 	/*
24189d5f82c8SMathieu Poirier 	 * Allocate a firmware name if the caller gave us one to work
24199d5f82c8SMathieu Poirier 	 * with.  Otherwise construct a new one using a default pattern.
24200c2ae2b1SMathieu Poirier 	 */
24219d5f82c8SMathieu Poirier 	if (firmware)
24221487dedaSMathieu Poirier 		p = kstrdup_const(firmware, GFP_KERNEL);
24239d5f82c8SMathieu Poirier 	else
24249d5f82c8SMathieu Poirier 		p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name);
24254df4f8beSMathieu Poirier 
24260c2ae2b1SMathieu Poirier 	if (!p)
24270c2ae2b1SMathieu Poirier 		return -ENOMEM;
24280c2ae2b1SMathieu Poirier 
24290c2ae2b1SMathieu Poirier 	rproc->firmware = p;
24300c2ae2b1SMathieu Poirier 
24310c2ae2b1SMathieu Poirier 	return 0;
24320c2ae2b1SMathieu Poirier }
24330c2ae2b1SMathieu Poirier 
2434bf860aa1SMathieu Poirier static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops)
2435bf860aa1SMathieu Poirier {
2436bf860aa1SMathieu Poirier 	rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
2437bf860aa1SMathieu Poirier 	if (!rproc->ops)
2438bf860aa1SMathieu Poirier 		return -ENOMEM;
2439bf860aa1SMathieu Poirier 
2440adf60a87SSiddharth Gupta 	/* Default to rproc_coredump if no coredump function is specified */
2441adf60a87SSiddharth Gupta 	if (!rproc->ops->coredump)
2442adf60a87SSiddharth Gupta 		rproc->ops->coredump = rproc_coredump;
2443adf60a87SSiddharth Gupta 
2444bf860aa1SMathieu Poirier 	if (rproc->ops->load)
2445bf860aa1SMathieu Poirier 		return 0;
2446bf860aa1SMathieu Poirier 
2447bf860aa1SMathieu Poirier 	/* Default to ELF loader if no load function is specified */
2448bf860aa1SMathieu Poirier 	rproc->ops->load = rproc_elf_load_segments;
2449bf860aa1SMathieu Poirier 	rproc->ops->parse_fw = rproc_elf_load_rsc_table;
2450bf860aa1SMathieu Poirier 	rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
2451e29ff72bSClement Leger 	rproc->ops->sanity_check = rproc_elf_sanity_check;
2452bf860aa1SMathieu Poirier 	rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
2453bf860aa1SMathieu Poirier 
2454bf860aa1SMathieu Poirier 	return 0;
2455bf860aa1SMathieu Poirier }
2456bf860aa1SMathieu Poirier 
2457400e64dfSOhad Ben-Cohen /**
2458400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
2459400e64dfSOhad Ben-Cohen  * @dev: the underlying device
2460400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
2461400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
24628b4aec9aSRobert Tivy  * @firmware: name of firmware file to load, can be NULL
2463400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
2464400e64dfSOhad Ben-Cohen  *
2465400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
24668b4aec9aSRobert Tivy  * it yet. if @firmware is NULL, a default name is used.
2467400e64dfSOhad Ben-Cohen  *
2468400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
2469400e64dfSOhad Ben-Cohen  * of the remote processor.
2470400e64dfSOhad Ben-Cohen  *
2471400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
2472160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
2473400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
2474400e64dfSOhad Ben-Cohen  *
2475400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
2476400e64dfSOhad Ben-Cohen  *
2477400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
2478433c0e04SBjorn Andersson  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
2479400e64dfSOhad Ben-Cohen  */
2480400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
2481400e64dfSOhad Ben-Cohen 			  const struct rproc_ops *ops,
2482400e64dfSOhad Ben-Cohen 			  const char *firmware, int len)
2483400e64dfSOhad Ben-Cohen {
2484400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
2485400e64dfSOhad Ben-Cohen 
2486400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
2487400e64dfSOhad Ben-Cohen 		return NULL;
2488400e64dfSOhad Ben-Cohen 
24890f57dc6aSMatt Redfearn 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
24900c2ae2b1SMathieu Poirier 	if (!rproc)
24910f57dc6aSMatt Redfearn 		return NULL;
24920c2ae2b1SMathieu Poirier 
2493400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
2494ddf71187SBjorn Andersson 	rproc->auto_boot = true;
2495418fd787SClement Leger 	rproc->elf_class = ELFCLASSNONE;
2496418fd787SClement Leger 	rproc->elf_machine = EM_NONE;
2497400e64dfSOhad Ben-Cohen 
2498b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
2499b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
2500b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
25012aefbef0SMatt Redfearn 	rproc->dev.class = &rproc_class;
25027c89717fSBjorn Andersson 	rproc->dev.driver_data = rproc;
25036442df49SAlex Elder 	idr_init(&rproc->notifyids);
2504b5ab5e24SOhad Ben-Cohen 
2505db655278SSuman Anna 	rproc->name = kstrdup_const(name, GFP_KERNEL);
2506db655278SSuman Anna 	if (!rproc->name)
2507db655278SSuman Anna 		goto put_device;
2508db655278SSuman Anna 
2509226f5db4SMathieu Poirier 	if (rproc_alloc_firmware(rproc, name, firmware))
2510226f5db4SMathieu Poirier 		goto put_device;
2511226f5db4SMathieu Poirier 
2512226f5db4SMathieu Poirier 	if (rproc_alloc_ops(rproc, ops))
2513226f5db4SMathieu Poirier 		goto put_device;
2514226f5db4SMathieu Poirier 
2515b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
2516b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
2517b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
2518b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
2519226f5db4SMathieu Poirier 		goto put_device;
2520b5ab5e24SOhad Ben-Cohen 	}
2521b5ab5e24SOhad Ben-Cohen 
2522b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
2523b5ab5e24SOhad Ben-Cohen 
2524400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
2525400e64dfSOhad Ben-Cohen 
2526400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
2527400e64dfSOhad Ben-Cohen 
2528400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
2529400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
2530400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
25317a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
25327bdc9650SBjorn Andersson 	INIT_LIST_HEAD(&rproc->subdevs);
25332666ca91SSarangdhar Joshi 	INIT_LIST_HEAD(&rproc->dump_segments);
2534400e64dfSOhad Ben-Cohen 
25358afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
25368afd519cSFernando Guzman Lugo 
2537400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
2538400e64dfSOhad Ben-Cohen 
2539400e64dfSOhad Ben-Cohen 	return rproc;
25400c2ae2b1SMathieu Poirier 
2541226f5db4SMathieu Poirier put_device:
2542226f5db4SMathieu Poirier 	put_device(&rproc->dev);
25430c2ae2b1SMathieu Poirier 	return NULL;
2544400e64dfSOhad Ben-Cohen }
2545400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
2546400e64dfSOhad Ben-Cohen 
2547400e64dfSOhad Ben-Cohen /**
2548433c0e04SBjorn Andersson  * rproc_free() - unroll rproc_alloc()
2549433c0e04SBjorn Andersson  * @rproc: the remote processor handle
2550433c0e04SBjorn Andersson  *
2551433c0e04SBjorn Andersson  * This function decrements the rproc dev refcount.
2552433c0e04SBjorn Andersson  *
2553433c0e04SBjorn Andersson  * If no one holds any reference to rproc anymore, then its refcount would
2554433c0e04SBjorn Andersson  * now drop to zero, and it would be freed.
2555433c0e04SBjorn Andersson  */
2556433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc)
2557433c0e04SBjorn Andersson {
2558433c0e04SBjorn Andersson 	put_device(&rproc->dev);
2559433c0e04SBjorn Andersson }
2560433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free);
2561433c0e04SBjorn Andersson 
2562433c0e04SBjorn Andersson /**
2563433c0e04SBjorn Andersson  * rproc_put() - release rproc reference
2564400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
2565400e64dfSOhad Ben-Cohen  *
2566c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
2567400e64dfSOhad Ben-Cohen  *
2568c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
2569c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
2570400e64dfSOhad Ben-Cohen  */
2571160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
2572400e64dfSOhad Ben-Cohen {
2573fbb6aacbSBjorn Andersson 	module_put(rproc->dev.parent->driver->owner);
2574b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
2575400e64dfSOhad Ben-Cohen }
2576160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
2577400e64dfSOhad Ben-Cohen 
2578400e64dfSOhad Ben-Cohen /**
2579160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
2580400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
2581400e64dfSOhad Ben-Cohen  *
2582400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
2583400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
2584160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
2585400e64dfSOhad Ben-Cohen  * has completed successfully.
2586400e64dfSOhad Ben-Cohen  *
2587160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
2588c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
2589433c0e04SBjorn Andersson  * one last refcount, one still needs to call rproc_free().
2590400e64dfSOhad Ben-Cohen  *
2591400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
2592400e64dfSOhad Ben-Cohen  */
2593160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
2594400e64dfSOhad Ben-Cohen {
2595400e64dfSOhad Ben-Cohen 	if (!rproc)
2596400e64dfSOhad Ben-Cohen 		return -EINVAL;
2597400e64dfSOhad Ben-Cohen 
2598ddf71187SBjorn Andersson 	/* TODO: make sure this works with rproc->power > 1 */
2599ddf71187SBjorn Andersson 	rproc_shutdown(rproc);
2600ddf71187SBjorn Andersson 
26012099c77dSSarangdhar Joshi 	mutex_lock(&rproc->lock);
26022099c77dSSarangdhar Joshi 	rproc->state = RPROC_DELETED;
26032099c77dSSarangdhar Joshi 	mutex_unlock(&rproc->lock);
26042099c77dSSarangdhar Joshi 
2605b003d45bSSarangdhar Joshi 	rproc_delete_debug_dir(rproc);
260662b8f9e9SSiddharth Gupta 	rproc_char_device_remove(rproc);
2607b003d45bSSarangdhar Joshi 
2608fec47d86SDave Gerlach 	/* the rproc is downref'ed as soon as it's removed from the klist */
2609fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
2610c0abe2caSBjorn Andersson 	list_del_rcu(&rproc->node);
2611fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
2612fec47d86SDave Gerlach 
2613c0abe2caSBjorn Andersson 	/* Ensure that no readers of rproc_list are still active */
2614c0abe2caSBjorn Andersson 	synchronize_rcu();
2615c0abe2caSBjorn Andersson 
2616b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
2617400e64dfSOhad Ben-Cohen 
2618400e64dfSOhad Ben-Cohen 	return 0;
2619400e64dfSOhad Ben-Cohen }
2620160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
2621400e64dfSOhad Ben-Cohen 
2622305ac5a7SPaul Cercueil static void devm_rproc_free(struct device *dev, void *res)
2623305ac5a7SPaul Cercueil {
2624305ac5a7SPaul Cercueil 	rproc_free(*(struct rproc **)res);
2625305ac5a7SPaul Cercueil }
2626305ac5a7SPaul Cercueil 
2627305ac5a7SPaul Cercueil /**
2628305ac5a7SPaul Cercueil  * devm_rproc_alloc() - resource managed rproc_alloc()
2629305ac5a7SPaul Cercueil  * @dev: the underlying device
2630305ac5a7SPaul Cercueil  * @name: name of this remote processor
2631305ac5a7SPaul Cercueil  * @ops: platform-specific handlers (mainly start/stop)
2632305ac5a7SPaul Cercueil  * @firmware: name of firmware file to load, can be NULL
2633305ac5a7SPaul Cercueil  * @len: length of private data needed by the rproc driver (in bytes)
2634305ac5a7SPaul Cercueil  *
2635305ac5a7SPaul Cercueil  * This function performs like rproc_alloc() but the acquired rproc device will
2636305ac5a7SPaul Cercueil  * automatically be released on driver detach.
2637305ac5a7SPaul Cercueil  *
2638305ac5a7SPaul Cercueil  * Returns: new rproc instance, or NULL on failure
2639305ac5a7SPaul Cercueil  */
2640305ac5a7SPaul Cercueil struct rproc *devm_rproc_alloc(struct device *dev, const char *name,
2641305ac5a7SPaul Cercueil 			       const struct rproc_ops *ops,
2642305ac5a7SPaul Cercueil 			       const char *firmware, int len)
2643305ac5a7SPaul Cercueil {
2644305ac5a7SPaul Cercueil 	struct rproc **ptr, *rproc;
2645305ac5a7SPaul Cercueil 
2646305ac5a7SPaul Cercueil 	ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL);
2647305ac5a7SPaul Cercueil 	if (!ptr)
26487dcef398SDan Carpenter 		return NULL;
2649305ac5a7SPaul Cercueil 
2650305ac5a7SPaul Cercueil 	rproc = rproc_alloc(dev, name, ops, firmware, len);
2651305ac5a7SPaul Cercueil 	if (rproc) {
2652305ac5a7SPaul Cercueil 		*ptr = rproc;
2653305ac5a7SPaul Cercueil 		devres_add(dev, ptr);
2654305ac5a7SPaul Cercueil 	} else {
2655305ac5a7SPaul Cercueil 		devres_free(ptr);
2656305ac5a7SPaul Cercueil 	}
2657305ac5a7SPaul Cercueil 
2658305ac5a7SPaul Cercueil 	return rproc;
2659305ac5a7SPaul Cercueil }
2660305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_alloc);
2661305ac5a7SPaul Cercueil 
26628afd519cSFernando Guzman Lugo /**
26637bdc9650SBjorn Andersson  * rproc_add_subdev() - add a subdevice to a remoteproc
26647bdc9650SBjorn Andersson  * @rproc: rproc handle to add the subdevice to
26657bdc9650SBjorn Andersson  * @subdev: subdev handle to register
26664902676fSBjorn Andersson  *
26674902676fSBjorn Andersson  * Caller is responsible for populating optional subdevice function pointers.
26687bdc9650SBjorn Andersson  */
26694902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
26707bdc9650SBjorn Andersson {
26717bdc9650SBjorn Andersson 	list_add_tail(&subdev->node, &rproc->subdevs);
26727bdc9650SBjorn Andersson }
26737bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev);
26747bdc9650SBjorn Andersson 
26757bdc9650SBjorn Andersson /**
26767bdc9650SBjorn Andersson  * rproc_remove_subdev() - remove a subdevice from a remoteproc
26777bdc9650SBjorn Andersson  * @rproc: rproc handle to remove the subdevice from
26787bdc9650SBjorn Andersson  * @subdev: subdev handle, previously registered with rproc_add_subdev()
26797bdc9650SBjorn Andersson  */
26807bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
26817bdc9650SBjorn Andersson {
26827bdc9650SBjorn Andersson 	list_del(&subdev->node);
26837bdc9650SBjorn Andersson }
26847bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev);
26857bdc9650SBjorn Andersson 
26867bdc9650SBjorn Andersson /**
26877c89717fSBjorn Andersson  * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
26887c89717fSBjorn Andersson  * @dev:	child device to find ancestor of
26897c89717fSBjorn Andersson  *
26907c89717fSBjorn Andersson  * Returns the ancestor rproc instance, or NULL if not found.
26917c89717fSBjorn Andersson  */
26927c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev)
26937c89717fSBjorn Andersson {
26947c89717fSBjorn Andersson 	for (dev = dev->parent; dev; dev = dev->parent) {
26957c89717fSBjorn Andersson 		if (dev->type == &rproc_type)
26967c89717fSBjorn Andersson 			return dev->driver_data;
26977c89717fSBjorn Andersson 	}
26987c89717fSBjorn Andersson 
26997c89717fSBjorn Andersson 	return NULL;
27007c89717fSBjorn Andersson }
27017c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child);
27027c89717fSBjorn Andersson 
27037c89717fSBjorn Andersson /**
27048afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
27058afd519cSFernando Guzman Lugo  * @rproc: remote processor
27068afd519cSFernando Guzman Lugo  * @type: crash type
27078afd519cSFernando Guzman Lugo  *
27088afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
27098afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
27108afd519cSFernando Guzman Lugo  * non-remoteproc driver.
27118afd519cSFernando Guzman Lugo  *
27128afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
27138afd519cSFernando Guzman Lugo  */
27148afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
27158afd519cSFernando Guzman Lugo {
27168afd519cSFernando Guzman Lugo 	if (!rproc) {
27178afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
27188afd519cSFernando Guzman Lugo 		return;
27198afd519cSFernando Guzman Lugo 	}
27208afd519cSFernando Guzman Lugo 
2721a781e5aaSRishabh Bhatnagar 	/* Prevent suspend while the remoteproc is being recovered */
2722a781e5aaSRishabh Bhatnagar 	pm_stay_awake(rproc->dev.parent);
2723a781e5aaSRishabh Bhatnagar 
27248afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
27258afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
27268afd519cSFernando Guzman Lugo 
27278afd519cSFernando Guzman Lugo 	/* create a new task to handle the error */
27288afd519cSFernando Guzman Lugo 	schedule_work(&rproc->crash_handler);
27298afd519cSFernando Guzman Lugo }
27308afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
27318afd519cSFernando Guzman Lugo 
2732dc5192c4SBjorn Andersson static int rproc_panic_handler(struct notifier_block *nb, unsigned long event,
2733dc5192c4SBjorn Andersson 			       void *ptr)
2734dc5192c4SBjorn Andersson {
2735dc5192c4SBjorn Andersson 	unsigned int longest = 0;
2736dc5192c4SBjorn Andersson 	struct rproc *rproc;
2737dc5192c4SBjorn Andersson 	unsigned int d;
2738dc5192c4SBjorn Andersson 
2739dc5192c4SBjorn Andersson 	rcu_read_lock();
2740dc5192c4SBjorn Andersson 	list_for_each_entry_rcu(rproc, &rproc_list, node) {
2741800dad00SMathieu Poirier 		if (!rproc->ops->panic)
2742800dad00SMathieu Poirier 			continue;
2743800dad00SMathieu Poirier 
2744800dad00SMathieu Poirier 		if (rproc->state != RPROC_RUNNING &&
2745800dad00SMathieu Poirier 		    rproc->state != RPROC_ATTACHED)
2746dc5192c4SBjorn Andersson 			continue;
2747dc5192c4SBjorn Andersson 
2748dc5192c4SBjorn Andersson 		d = rproc->ops->panic(rproc);
2749dc5192c4SBjorn Andersson 		longest = max(longest, d);
2750dc5192c4SBjorn Andersson 	}
2751dc5192c4SBjorn Andersson 	rcu_read_unlock();
2752dc5192c4SBjorn Andersson 
2753dc5192c4SBjorn Andersson 	/*
2754dc5192c4SBjorn Andersson 	 * Delay for the longest requested duration before returning. This can
2755dc5192c4SBjorn Andersson 	 * be used by the remoteproc drivers to give the remote processor time
2756dc5192c4SBjorn Andersson 	 * to perform any requested operations (such as flush caches), when
2757dc5192c4SBjorn Andersson 	 * it's not possible to signal the Linux side due to the panic.
2758dc5192c4SBjorn Andersson 	 */
2759dc5192c4SBjorn Andersson 	mdelay(longest);
2760dc5192c4SBjorn Andersson 
2761dc5192c4SBjorn Andersson 	return NOTIFY_DONE;
2762dc5192c4SBjorn Andersson }
2763dc5192c4SBjorn Andersson 
2764dc5192c4SBjorn Andersson static void __init rproc_init_panic(void)
2765dc5192c4SBjorn Andersson {
2766dc5192c4SBjorn Andersson 	rproc_panic_nb.notifier_call = rproc_panic_handler;
2767dc5192c4SBjorn Andersson 	atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb);
2768dc5192c4SBjorn Andersson }
2769dc5192c4SBjorn Andersson 
2770dc5192c4SBjorn Andersson static void __exit rproc_exit_panic(void)
2771dc5192c4SBjorn Andersson {
2772dc5192c4SBjorn Andersson 	atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb);
2773dc5192c4SBjorn Andersson }
2774dc5192c4SBjorn Andersson 
2775400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
2776400e64dfSOhad Ben-Cohen {
27772aefbef0SMatt Redfearn 	rproc_init_sysfs();
2778400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
277962b8f9e9SSiddharth Gupta 	rproc_init_cdev();
2780dc5192c4SBjorn Andersson 	rproc_init_panic();
2781b5ab5e24SOhad Ben-Cohen 
2782400e64dfSOhad Ben-Cohen 	return 0;
2783400e64dfSOhad Ben-Cohen }
2784a8f40111SBrandon Maier subsys_initcall(remoteproc_init);
2785400e64dfSOhad Ben-Cohen 
2786400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
2787400e64dfSOhad Ben-Cohen {
2788f42f79afSSuman Anna 	ida_destroy(&rproc_dev_index);
2789f42f79afSSuman Anna 
2790dc5192c4SBjorn Andersson 	rproc_exit_panic();
2791400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
27922aefbef0SMatt Redfearn 	rproc_exit_sysfs();
2793400e64dfSOhad Ben-Cohen }
2794400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
2795400e64dfSOhad Ben-Cohen 
2796400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
2797400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
2798