xref: /linux/drivers/remoteproc/remoteproc_core.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
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>
23f39650deSAndy Shevchenko #include <linux/panic_notifier.h>
24400e64dfSOhad Ben-Cohen #include <linux/slab.h>
25400e64dfSOhad Ben-Cohen #include <linux/mutex.h>
26400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h>
27400e64dfSOhad Ben-Cohen #include <linux/firmware.h>
28400e64dfSOhad Ben-Cohen #include <linux/string.h>
29400e64dfSOhad Ben-Cohen #include <linux/debugfs.h>
30c0abe2caSBjorn Andersson #include <linux/rculist.h>
31400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h>
32400e64dfSOhad Ben-Cohen #include <linux/iommu.h>
33b5ab5e24SOhad Ben-Cohen #include <linux/idr.h>
34400e64dfSOhad Ben-Cohen #include <linux/elf.h>
35a2b950acSOhad Ben-Cohen #include <linux/crc32.h>
36*8b46dc5cSMathieu Poirier #include <linux/of_platform.h>
37086d0872SLoic Pallardy #include <linux/of_reserved_mem.h>
38400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h>
39400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h>
40cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h>
41086d0872SLoic Pallardy #include <linux/platform_device.h>
42400e64dfSOhad Ben-Cohen 
43400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h"
44400e64dfSOhad Ben-Cohen 
45b36de8cfSLoic Pallardy #define HIGH_BITS_MASK 0xFFFFFFFF00000000ULL
46b36de8cfSLoic Pallardy 
47fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex);
48fec47d86SDave Gerlach static LIST_HEAD(rproc_list);
49dc5192c4SBjorn Andersson static struct notifier_block rproc_panic_nb;
50fec47d86SDave Gerlach 
51a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
52a2b950acSOhad Ben-Cohen 				 void *, int offset, int avail);
53400e64dfSOhad Ben-Cohen 
54c6aed238SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc,
55c6aed238SLoic Pallardy 				struct rproc_mem_entry *mem);
56c6aed238SLoic Pallardy static int rproc_release_carveout(struct rproc *rproc,
57c6aed238SLoic Pallardy 				  struct rproc_mem_entry *mem);
58c6aed238SLoic Pallardy 
59b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */
60b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index);
61cab8300bSMukesh Ojha static struct workqueue_struct *rproc_recovery_wq;
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 */
rproc_crash_to_string(enum rproc_crash_type type)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  */
rproc_iommu_fault(struct iommu_domain * domain,struct device * dev,unsigned long iova,int flags,void * token)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 
rproc_enable_iommu(struct rproc * rproc)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 
rproc_disable_iommu(struct rproc * rproc)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 
rproc_va_to_pa(void * cpu_addr)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
16995347e73SSuman Anna  * @is_iomem: optional pointer filled in to indicate if @da is iomapped memory
170a01f7cd6SSuman Anna  *
171400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
172400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
173a01f7cd6SSuman Anna  * device addresses (which are hardcoded in the firmware). They may also have
174a01f7cd6SSuman Anna  * dedicated memory regions internal to the processors, and use them either
175a01f7cd6SSuman Anna  * exclusively or alongside carveouts.
176400e64dfSOhad Ben-Cohen  *
177400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
178400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
179400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
180400e64dfSOhad Ben-Cohen  *
181a01f7cd6SSuman Anna  * This function is a helper function with which we can go over the allocated
182a01f7cd6SSuman Anna  * carveouts and translate specific device addresses to kernel virtual addresses
183a01f7cd6SSuman Anna  * so we can access the referenced memory. This function also allows to perform
184a01f7cd6SSuman Anna  * translations on the internal remoteproc memory regions through a platform
185a01f7cd6SSuman Anna  * implementation specific da_to_va ops, if present.
186a01f7cd6SSuman Anna  *
187400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
188400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
189a01f7cd6SSuman Anna  * here the output of the DMA API for the carveouts, which should be more
190a01f7cd6SSuman Anna  * correct.
191f2867434SSuman Anna  *
192f2867434SSuman Anna  * Return: a valid kernel address on success or NULL on failure
193400e64dfSOhad Ben-Cohen  */
rproc_da_to_va(struct rproc * rproc,u64 da,size_t len,bool * is_iomem)19440df0a91SPeng Fan void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
195400e64dfSOhad Ben-Cohen {
196400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
197400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
198400e64dfSOhad Ben-Cohen 
199a01f7cd6SSuman Anna 	if (rproc->ops->da_to_va) {
20040df0a91SPeng Fan 		ptr = rproc->ops->da_to_va(rproc, da, len, is_iomem);
201a01f7cd6SSuman Anna 		if (ptr)
202a01f7cd6SSuman Anna 			goto out;
203a01f7cd6SSuman Anna 	}
204a01f7cd6SSuman Anna 
205400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
206400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
207400e64dfSOhad Ben-Cohen 
20874457c40SLoic Pallardy 		/*  Verify that carveout is allocated */
20974457c40SLoic Pallardy 		if (!carveout->va)
21074457c40SLoic Pallardy 			continue;
21174457c40SLoic Pallardy 
212400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
213400e64dfSOhad Ben-Cohen 		if (offset < 0)
214400e64dfSOhad Ben-Cohen 			continue;
215400e64dfSOhad Ben-Cohen 
216400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
217400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
218400e64dfSOhad Ben-Cohen 			continue;
219400e64dfSOhad Ben-Cohen 
220400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
221400e64dfSOhad Ben-Cohen 
22240df0a91SPeng Fan 		if (is_iomem)
22340df0a91SPeng Fan 			*is_iomem = carveout->is_iomem;
22440df0a91SPeng Fan 
225400e64dfSOhad Ben-Cohen 		break;
226400e64dfSOhad Ben-Cohen 	}
227400e64dfSOhad Ben-Cohen 
228a01f7cd6SSuman Anna out:
229400e64dfSOhad Ben-Cohen 	return ptr;
230400e64dfSOhad Ben-Cohen }
2314afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
232400e64dfSOhad Ben-Cohen 
233b0019ccdSLoic Pallardy /**
234b0019ccdSLoic Pallardy  * rproc_find_carveout_by_name() - lookup the carveout region by a name
235b0019ccdSLoic Pallardy  * @rproc: handle of a remote processor
2362e7d4c2cSArnaud Pouliquen  * @name: carveout name to find (format string)
2372e7d4c2cSArnaud Pouliquen  * @...: optional parameters matching @name string
238b0019ccdSLoic Pallardy  *
239b0019ccdSLoic Pallardy  * Platform driver has the capability to register some pre-allacoted carveout
240b0019ccdSLoic Pallardy  * (physically contiguous memory regions) before rproc firmware loading and
241b0019ccdSLoic Pallardy  * associated resource table analysis. These regions may be dedicated memory
242b0019ccdSLoic Pallardy  * regions internal to the coprocessor or specified DDR region with specific
243b0019ccdSLoic Pallardy  * attributes
244b0019ccdSLoic Pallardy  *
245b0019ccdSLoic Pallardy  * This function is a helper function with which we can go over the
246b0019ccdSLoic Pallardy  * allocated carveouts and return associated region characteristics like
247b0019ccdSLoic Pallardy  * coprocessor address, length or processor virtual address.
248b0019ccdSLoic Pallardy  *
249b0019ccdSLoic Pallardy  * Return: a valid pointer on carveout entry on success or NULL on failure.
250b0019ccdSLoic Pallardy  */
2517e05c8deSLee Jones __printf(2, 3)
252b0019ccdSLoic Pallardy struct rproc_mem_entry *
rproc_find_carveout_by_name(struct rproc * rproc,const char * name,...)253b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
254b0019ccdSLoic Pallardy {
255b0019ccdSLoic Pallardy 	va_list args;
256b0019ccdSLoic Pallardy 	char _name[32];
257b0019ccdSLoic Pallardy 	struct rproc_mem_entry *carveout, *mem = NULL;
258b0019ccdSLoic Pallardy 
259b0019ccdSLoic Pallardy 	if (!name)
260b0019ccdSLoic Pallardy 		return NULL;
261b0019ccdSLoic Pallardy 
262b0019ccdSLoic Pallardy 	va_start(args, name);
263b0019ccdSLoic Pallardy 	vsnprintf(_name, sizeof(_name), name, args);
264b0019ccdSLoic Pallardy 	va_end(args);
265b0019ccdSLoic Pallardy 
266b0019ccdSLoic Pallardy 	list_for_each_entry(carveout, &rproc->carveouts, node) {
267b0019ccdSLoic Pallardy 		/* Compare carveout and requested names */
268b0019ccdSLoic Pallardy 		if (!strcmp(carveout->name, _name)) {
269b0019ccdSLoic Pallardy 			mem = carveout;
270b0019ccdSLoic Pallardy 			break;
271b0019ccdSLoic Pallardy 		}
272b0019ccdSLoic Pallardy 	}
273b0019ccdSLoic Pallardy 
274b0019ccdSLoic Pallardy 	return mem;
275b0019ccdSLoic Pallardy }
276b0019ccdSLoic Pallardy 
277c874bf59SLoic Pallardy /**
278c874bf59SLoic Pallardy  * rproc_check_carveout_da() - Check specified carveout da configuration
279c874bf59SLoic Pallardy  * @rproc: handle of a remote processor
280c874bf59SLoic Pallardy  * @mem: pointer on carveout to check
281c874bf59SLoic Pallardy  * @da: area device address
282c874bf59SLoic Pallardy  * @len: associated area size
283c874bf59SLoic Pallardy  *
284c874bf59SLoic Pallardy  * This function is a helper function to verify requested device area (couple
28528d7d5c6SLoic Pallardy  * da, len) is part of specified carveout.
28628d7d5c6SLoic Pallardy  * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is
28728d7d5c6SLoic Pallardy  * checked.
288c874bf59SLoic Pallardy  *
28928d7d5c6SLoic Pallardy  * Return: 0 if carveout matches request else error
290c874bf59SLoic Pallardy  */
rproc_check_carveout_da(struct rproc * rproc,struct rproc_mem_entry * mem,u32 da,u32 len)29128d7d5c6SLoic Pallardy static int rproc_check_carveout_da(struct rproc *rproc,
29228d7d5c6SLoic Pallardy 				   struct rproc_mem_entry *mem, u32 da, u32 len)
293c874bf59SLoic Pallardy {
294c874bf59SLoic Pallardy 	struct device *dev = &rproc->dev;
29528d7d5c6SLoic Pallardy 	int delta;
296c874bf59SLoic Pallardy 
297c874bf59SLoic Pallardy 	/* Check requested resource length */
298c874bf59SLoic Pallardy 	if (len > mem->len) {
299c874bf59SLoic Pallardy 		dev_err(dev, "Registered carveout doesn't fit len request\n");
30028d7d5c6SLoic Pallardy 		return -EINVAL;
301c874bf59SLoic Pallardy 	}
302c874bf59SLoic Pallardy 
303c874bf59SLoic Pallardy 	if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) {
30428d7d5c6SLoic Pallardy 		/* Address doesn't match registered carveout configuration */
30528d7d5c6SLoic Pallardy 		return -EINVAL;
306c874bf59SLoic Pallardy 	} else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) {
307c874bf59SLoic Pallardy 		delta = da - mem->da;
308c874bf59SLoic Pallardy 
309c874bf59SLoic Pallardy 		/* Check requested resource belongs to registered carveout */
310c874bf59SLoic Pallardy 		if (delta < 0) {
311c874bf59SLoic Pallardy 			dev_err(dev,
312c874bf59SLoic Pallardy 				"Registered carveout doesn't fit da request\n");
31328d7d5c6SLoic Pallardy 			return -EINVAL;
314c874bf59SLoic Pallardy 		}
315c874bf59SLoic Pallardy 
316c874bf59SLoic Pallardy 		if (delta + len > mem->len) {
317c874bf59SLoic Pallardy 			dev_err(dev,
318c874bf59SLoic Pallardy 				"Registered carveout doesn't fit len request\n");
31928d7d5c6SLoic Pallardy 			return -EINVAL;
320c874bf59SLoic Pallardy 		}
321c874bf59SLoic Pallardy 	}
322c874bf59SLoic Pallardy 
323c874bf59SLoic Pallardy 	return 0;
324c874bf59SLoic Pallardy }
325c874bf59SLoic Pallardy 
rproc_alloc_vring(struct rproc_vdev * rvdev,int i)3266db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
327400e64dfSOhad Ben-Cohen {
3287a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
329b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3306db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
331c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
332096ee786SClement Leger 	int ret, notifyid;
333c6aed238SLoic Pallardy 	struct rproc_mem_entry *mem;
334096ee786SClement Leger 	size_t size;
335400e64dfSOhad Ben-Cohen 
3366db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
337c2a052a4SXuan Zhuo 	size = PAGE_ALIGN(vring_size(rvring->num, rvring->align));
3386db20ea8SOhad Ben-Cohen 
339c6aed238SLoic Pallardy 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
340c6aed238SLoic Pallardy 
341c6aed238SLoic Pallardy 	/* Search for pre-registered carveout */
342c6aed238SLoic Pallardy 	mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
343c6aed238SLoic Pallardy 					  i);
344c6aed238SLoic Pallardy 	if (mem) {
345c6aed238SLoic Pallardy 		if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size))
346c6aed238SLoic Pallardy 			return -ENOMEM;
347c6aed238SLoic Pallardy 	} else {
348698ae3d7Swangjianli 		/* Register carveout in list */
34999cf0361SBen Dooks (Codethink) 		mem = rproc_mem_entry_init(dev, NULL, 0,
35099cf0361SBen Dooks (Codethink) 					   size, rsc->vring[i].da,
351c6aed238SLoic Pallardy 					   rproc_alloc_carveout,
352c6aed238SLoic Pallardy 					   rproc_release_carveout,
353c6aed238SLoic Pallardy 					   "vdev%dvring%d",
354c6aed238SLoic Pallardy 					   rvdev->index, i);
355c6aed238SLoic Pallardy 		if (!mem) {
356c6aed238SLoic Pallardy 			dev_err(dev, "Can't allocate memory entry structure\n");
357c6aed238SLoic Pallardy 			return -ENOMEM;
358c6aed238SLoic Pallardy 		}
359c6aed238SLoic Pallardy 
360c6aed238SLoic Pallardy 		rproc_add_carveout(rproc, mem);
3616db20ea8SOhad Ben-Cohen 	}
3626db20ea8SOhad Ben-Cohen 
3636db20ea8SOhad Ben-Cohen 	/*
3646db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
3656db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
3666db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
3676db20ea8SOhad Ben-Cohen 	 */
36815fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
369b39599b7SSuman Anna 	if (ret < 0) {
37015fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
3716db20ea8SOhad Ben-Cohen 		return ret;
3726db20ea8SOhad Ben-Cohen 	}
37315fc6110STejun Heo 	notifyid = ret;
3746db20ea8SOhad Ben-Cohen 
37548f18f89SBjorn Andersson 	/* Potentially bump max_notifyid */
37648f18f89SBjorn Andersson 	if (notifyid > rproc->max_notifyid)
37748f18f89SBjorn Andersson 		rproc->max_notifyid = notifyid;
37848f18f89SBjorn Andersson 
3796db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
3806db20ea8SOhad Ben-Cohen 
381c6aed238SLoic Pallardy 	/* Let the rproc know the notifyid of this vring.*/
382c0d63157SSjur Brændeland 	rsc->vring[i].notifyid = notifyid;
3836db20ea8SOhad Ben-Cohen 	return 0;
3846db20ea8SOhad Ben-Cohen }
3856db20ea8SOhad Ben-Cohen 
3869c31255cSArnaud Pouliquen int
rproc_parse_vring(struct rproc_vdev * rvdev,struct fw_rsc_vdev * rsc,int i)3876db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
388400e64dfSOhad Ben-Cohen {
389400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
390b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
391400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
3926db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
393400e64dfSOhad Ben-Cohen 
3949d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
3957a186941SOhad Ben-Cohen 		i, vring->da, vring->num, vring->align);
3967a186941SOhad Ben-Cohen 
39763140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
39863140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
39963140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
40063140e0eSOhad Ben-Cohen 			vring->num, vring->align);
401400e64dfSOhad Ben-Cohen 		return -EINVAL;
402400e64dfSOhad Ben-Cohen 	}
403400e64dfSOhad Ben-Cohen 
404c2a052a4SXuan Zhuo 	rvring->num = vring->num;
4056db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
4066db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
407400e64dfSOhad Ben-Cohen 
408400e64dfSOhad Ben-Cohen 	return 0;
409400e64dfSOhad Ben-Cohen }
410400e64dfSOhad Ben-Cohen 
rproc_free_vring(struct rproc_vring * rvring)4116db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
4127a186941SOhad Ben-Cohen {
4136db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
41400a0eec5SClement Leger 	int idx = rvring - rvring->rvdev->vring;
415c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
4167a186941SOhad Ben-Cohen 
4177a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
418099a3f33SSjur Brændeland 
4194d3ebb3bSMathieu Poirier 	/*
4204d3ebb3bSMathieu Poirier 	 * At this point rproc_stop() has been called and the installed resource
4214d3ebb3bSMathieu Poirier 	 * table in the remote processor memory may no longer be accessible. As
4224d3ebb3bSMathieu Poirier 	 * such and as per rproc_stop(), rproc->table_ptr points to the cached
4234d3ebb3bSMathieu Poirier 	 * resource table (rproc->cached_table).  The cached resource table is
4244d3ebb3bSMathieu Poirier 	 * only available when a remote processor has been booted by the
4254d3ebb3bSMathieu Poirier 	 * remoteproc core, otherwise it is NULL.
4264d3ebb3bSMathieu Poirier 	 *
4274d3ebb3bSMathieu Poirier 	 * Based on the above, reset the virtio device section in the cached
4284d3ebb3bSMathieu Poirier 	 * resource table only if there is one to work with.
4294d3ebb3bSMathieu Poirier 	 */
4304d3ebb3bSMathieu Poirier 	if (rproc->table_ptr) {
431c0d63157SSjur Brændeland 		rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
432c0d63157SSjur Brændeland 		rsc->vring[idx].da = 0;
433c0d63157SSjur Brændeland 		rsc->vring[idx].notifyid = -1;
4347a186941SOhad Ben-Cohen 	}
4354d3ebb3bSMathieu Poirier }
4367a186941SOhad Ben-Cohen 
rproc_add_rvdev(struct rproc * rproc,struct rproc_vdev * rvdev)4379c31255cSArnaud Pouliquen void rproc_add_rvdev(struct rproc *rproc, struct rproc_vdev *rvdev)
43863badba9SArnaud Pouliquen {
43963badba9SArnaud Pouliquen 	if (rvdev && rproc)
44063badba9SArnaud Pouliquen 		list_add_tail(&rvdev->node, &rproc->rvdevs);
44163badba9SArnaud Pouliquen }
44263badba9SArnaud Pouliquen 
rproc_remove_rvdev(struct rproc_vdev * rvdev)4439c31255cSArnaud Pouliquen void rproc_remove_rvdev(struct rproc_vdev *rvdev)
44463badba9SArnaud Pouliquen {
44563badba9SArnaud Pouliquen 	if (rvdev)
44663badba9SArnaud Pouliquen 		list_del(&rvdev->node);
44763badba9SArnaud Pouliquen }
448086d0872SLoic Pallardy /**
449fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
450400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
4512bf23461SJindong Yue  * @ptr: the vring resource descriptor
4522e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
453fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
454400e64dfSOhad Ben-Cohen  *
4557a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
4567a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
4577a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
4587a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
459400e64dfSOhad Ben-Cohen  *
4607a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
4617a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
4627a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
4637a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
4647a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
465400e64dfSOhad Ben-Cohen  *
4667a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
4677a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
4687a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
4697a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
4707a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
4717a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
4727a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
473400e64dfSOhad Ben-Cohen  *
474f2867434SSuman Anna  * Return: 0 on success, or an appropriate error code otherwise
475400e64dfSOhad Ben-Cohen  */
rproc_handle_vdev(struct rproc * rproc,void * ptr,int offset,int avail)4762bf23461SJindong Yue static int rproc_handle_vdev(struct rproc *rproc, void *ptr,
477a2b950acSOhad Ben-Cohen 			     int offset, int avail)
478400e64dfSOhad Ben-Cohen {
4792bf23461SJindong Yue 	struct fw_rsc_vdev *rsc = ptr;
480b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
4817a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
4827d7f8fe4SDan Carpenter 	size_t rsc_size;
483fd28f879SArnaud Pouliquen 	struct rproc_vdev_data rvdev_data;
4841d7b61c0SArnaud Pouliquen 	struct platform_device *pdev;
485fd2c15ecSOhad Ben-Cohen 
486fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
4877d7f8fe4SDan Carpenter 	rsc_size = struct_size(rsc, vring, rsc->num_of_vrings);
4887d7f8fe4SDan Carpenter 	if (size_add(rsc_size, rsc->config_len) > avail) {
489b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
490fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
491fd2c15ecSOhad Ben-Cohen 	}
492fd2c15ecSOhad Ben-Cohen 
493fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
494fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
495fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
496fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
497fd2c15ecSOhad Ben-Cohen 	}
498fd2c15ecSOhad Ben-Cohen 
4999d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
500fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
501400e64dfSOhad Ben-Cohen 
5027a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
5037a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
504fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
505400e64dfSOhad Ben-Cohen 		return -EINVAL;
506400e64dfSOhad Ben-Cohen 	}
507400e64dfSOhad Ben-Cohen 
508fd28f879SArnaud Pouliquen 	rvdev_data.id = rsc->id;
509fd28f879SArnaud Pouliquen 	rvdev_data.index = rproc->nb_vdev++;
510fd28f879SArnaud Pouliquen 	rvdev_data.rsc_offset = offset;
511fd28f879SArnaud Pouliquen 	rvdev_data.rsc = rsc;
5127a186941SOhad Ben-Cohen 
51365fcf387SShengjiu Wang 	/*
51465fcf387SShengjiu Wang 	 * When there is more than one remote processor, rproc->nb_vdev number is
51565fcf387SShengjiu Wang 	 * same for each separate instances of "rproc". If rvdev_data.index is used
51665fcf387SShengjiu Wang 	 * as device id, then we get duplication in sysfs, so need to use
51765fcf387SShengjiu Wang 	 * PLATFORM_DEVID_AUTO to auto select device id.
51865fcf387SShengjiu Wang 	 */
51965fcf387SShengjiu Wang 	pdev = platform_device_register_data(dev, "rproc-virtio", PLATFORM_DEVID_AUTO, &rvdev_data,
5201d7b61c0SArnaud Pouliquen 					     sizeof(rvdev_data));
5211d7b61c0SArnaud Pouliquen 	if (IS_ERR(pdev)) {
5221d7b61c0SArnaud Pouliquen 		dev_err(dev, "failed to create rproc-virtio device\n");
5231d7b61c0SArnaud Pouliquen 		return PTR_ERR(pdev);
5241d7b61c0SArnaud Pouliquen 	}
525400e64dfSOhad Ben-Cohen 
526400e64dfSOhad Ben-Cohen 	return 0;
527aab8d802SBjorn Andersson }
528aab8d802SBjorn Andersson 
529400e64dfSOhad Ben-Cohen /**
530400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
531400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
5322bf23461SJindong Yue  * @ptr: the trace resource descriptor
5332e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
534fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
535400e64dfSOhad Ben-Cohen  *
536400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
537400e64dfSOhad Ben-Cohen  * export it via debugfs.
538400e64dfSOhad Ben-Cohen  *
539400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
540400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
541400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
542400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
543400e64dfSOhad Ben-Cohen  *
544f2867434SSuman Anna  * Return: 0 on success, or an appropriate error code otherwise
545400e64dfSOhad Ben-Cohen  */
rproc_handle_trace(struct rproc * rproc,void * ptr,int offset,int avail)5462bf23461SJindong Yue static int rproc_handle_trace(struct rproc *rproc, void *ptr,
547a2b950acSOhad Ben-Cohen 			      int offset, int avail)
548400e64dfSOhad Ben-Cohen {
5492bf23461SJindong Yue 	struct fw_rsc_trace *rsc = ptr;
550a987e6b9SLoic Pallardy 	struct rproc_debug_trace *trace;
551b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
552400e64dfSOhad Ben-Cohen 	char name[15];
553400e64dfSOhad Ben-Cohen 
554fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
555b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
556fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
557fd2c15ecSOhad Ben-Cohen 	}
558fd2c15ecSOhad Ben-Cohen 
559fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
560fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
561fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
562fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
563fd2c15ecSOhad Ben-Cohen 	}
564fd2c15ecSOhad Ben-Cohen 
565400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
566172e6ab1SSuman Anna 	if (!trace)
567400e64dfSOhad Ben-Cohen 		return -ENOMEM;
568400e64dfSOhad Ben-Cohen 
569400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
570a987e6b9SLoic Pallardy 	trace->trace_mem.len = rsc->len;
571a987e6b9SLoic Pallardy 	trace->trace_mem.da = rsc->da;
572a987e6b9SLoic Pallardy 
573a987e6b9SLoic Pallardy 	/* set pointer on rproc device */
574a987e6b9SLoic Pallardy 	trace->rproc = rproc;
575400e64dfSOhad Ben-Cohen 
576400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
577400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
578400e64dfSOhad Ben-Cohen 
579400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
580a987e6b9SLoic Pallardy 	trace->tfile = rproc_create_trace_file(name, rproc, trace);
581400e64dfSOhad Ben-Cohen 
582400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
583400e64dfSOhad Ben-Cohen 
584400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
585400e64dfSOhad Ben-Cohen 
586a987e6b9SLoic Pallardy 	dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n",
587a987e6b9SLoic Pallardy 		name, rsc->da, rsc->len);
588400e64dfSOhad Ben-Cohen 
589400e64dfSOhad Ben-Cohen 	return 0;
590400e64dfSOhad Ben-Cohen }
591400e64dfSOhad Ben-Cohen 
592400e64dfSOhad Ben-Cohen /**
593400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
594400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
5952bf23461SJindong Yue  * @ptr: the devmem resource entry
5962e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
597fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
598400e64dfSOhad Ben-Cohen  *
599400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
600400e64dfSOhad Ben-Cohen  *
601400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
602400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
603400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
604400e64dfSOhad Ben-Cohen  *
605400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
606400e64dfSOhad Ben-Cohen  *
607400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
608400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
609400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
610400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
611400e64dfSOhad Ben-Cohen  *
612400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
613400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
614400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
615400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
616400e64dfSOhad Ben-Cohen  * are outside those ranges.
617f2867434SSuman Anna  *
618f2867434SSuman Anna  * Return: 0 on success, or an appropriate error code otherwise
619400e64dfSOhad Ben-Cohen  */
rproc_handle_devmem(struct rproc * rproc,void * ptr,int offset,int avail)6202bf23461SJindong Yue static int rproc_handle_devmem(struct rproc *rproc, void *ptr,
621a2b950acSOhad Ben-Cohen 			       int offset, int avail)
622400e64dfSOhad Ben-Cohen {
6232bf23461SJindong Yue 	struct fw_rsc_devmem *rsc = ptr;
624400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
625b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
626400e64dfSOhad Ben-Cohen 	int ret;
627400e64dfSOhad Ben-Cohen 
628400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
629400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
630400e64dfSOhad Ben-Cohen 		return -EINVAL;
631400e64dfSOhad Ben-Cohen 
632fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
633b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
634fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
635fd2c15ecSOhad Ben-Cohen 	}
636fd2c15ecSOhad Ben-Cohen 
637fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
638fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
639b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
640fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
641fd2c15ecSOhad Ben-Cohen 	}
642fd2c15ecSOhad Ben-Cohen 
643400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
644172e6ab1SSuman Anna 	if (!mapping)
645400e64dfSOhad Ben-Cohen 		return -ENOMEM;
646400e64dfSOhad Ben-Cohen 
6471369459bSJason Gunthorpe 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags,
6481369459bSJason Gunthorpe 			GFP_KERNEL);
649400e64dfSOhad Ben-Cohen 	if (ret) {
650b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
651400e64dfSOhad Ben-Cohen 		goto out;
652400e64dfSOhad Ben-Cohen 	}
653400e64dfSOhad Ben-Cohen 
654400e64dfSOhad Ben-Cohen 	/*
655400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
656400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
657400e64dfSOhad Ben-Cohen 	 *
658400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
659400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
660400e64dfSOhad Ben-Cohen 	 */
661400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
662400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
663400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
664400e64dfSOhad Ben-Cohen 
665b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
666400e64dfSOhad Ben-Cohen 		rsc->pa, rsc->da, rsc->len);
667400e64dfSOhad Ben-Cohen 
668400e64dfSOhad Ben-Cohen 	return 0;
669400e64dfSOhad Ben-Cohen 
670400e64dfSOhad Ben-Cohen out:
671400e64dfSOhad Ben-Cohen 	kfree(mapping);
672400e64dfSOhad Ben-Cohen 	return ret;
673400e64dfSOhad Ben-Cohen }
674400e64dfSOhad Ben-Cohen 
675400e64dfSOhad Ben-Cohen /**
676d7c51706SLoic Pallardy  * rproc_alloc_carveout() - allocated specified carveout
677d7c51706SLoic Pallardy  * @rproc: rproc handle
678d7c51706SLoic Pallardy  * @mem: the memory entry to allocate
679d7c51706SLoic Pallardy  *
680d7c51706SLoic Pallardy  * This function allocate specified memory entry @mem using
681d7c51706SLoic Pallardy  * dma_alloc_coherent() as default allocator
682f2867434SSuman Anna  *
683f2867434SSuman Anna  * Return: 0 on success, or an appropriate error code otherwise
684d7c51706SLoic Pallardy  */
rproc_alloc_carveout(struct rproc * rproc,struct rproc_mem_entry * mem)685d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc,
686d7c51706SLoic Pallardy 				struct rproc_mem_entry *mem)
687d7c51706SLoic Pallardy {
688d7c51706SLoic Pallardy 	struct rproc_mem_entry *mapping = NULL;
689d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
690d7c51706SLoic Pallardy 	dma_addr_t dma;
691d7c51706SLoic Pallardy 	void *va;
692d7c51706SLoic Pallardy 	int ret;
693d7c51706SLoic Pallardy 
694d7c51706SLoic Pallardy 	va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL);
695d7c51706SLoic Pallardy 	if (!va) {
696d7c51706SLoic Pallardy 		dev_err(dev->parent,
697096ee786SClement Leger 			"failed to allocate dma memory: len 0x%zx\n",
698096ee786SClement Leger 			mem->len);
699d7c51706SLoic Pallardy 		return -ENOMEM;
700d7c51706SLoic Pallardy 	}
701d7c51706SLoic Pallardy 
702096ee786SClement Leger 	dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n",
703d7c51706SLoic Pallardy 		va, &dma, mem->len);
704d7c51706SLoic Pallardy 
70560f849a5SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) {
70660f849a5SLoic Pallardy 		/*
70760f849a5SLoic Pallardy 		 * Check requested da is equal to dma address
70860f849a5SLoic Pallardy 		 * and print a warn message in case of missalignment.
70960f849a5SLoic Pallardy 		 * Don't stop rproc_start sequence as coprocessor may
71060f849a5SLoic Pallardy 		 * build pa to da translation on its side.
71160f849a5SLoic Pallardy 		 */
71260f849a5SLoic Pallardy 		if (mem->da != (u32)dma)
71360f849a5SLoic Pallardy 			dev_warn(dev->parent,
71460f849a5SLoic Pallardy 				 "Allocated carveout doesn't fit device address request\n");
71560f849a5SLoic Pallardy 	}
71660f849a5SLoic Pallardy 
717d7c51706SLoic Pallardy 	/*
718d7c51706SLoic Pallardy 	 * Ok, this is non-standard.
719d7c51706SLoic Pallardy 	 *
720d7c51706SLoic Pallardy 	 * Sometimes we can't rely on the generic iommu-based DMA API
721d7c51706SLoic Pallardy 	 * to dynamically allocate the device address and then set the IOMMU
722d7c51706SLoic Pallardy 	 * tables accordingly, because some remote processors might
723d7c51706SLoic Pallardy 	 * _require_ us to use hard coded device addresses that their
724d7c51706SLoic Pallardy 	 * firmware was compiled with.
725d7c51706SLoic Pallardy 	 *
726d7c51706SLoic Pallardy 	 * In this case, we must use the IOMMU API directly and map
727d7c51706SLoic Pallardy 	 * the memory to the device address as expected by the remote
728d7c51706SLoic Pallardy 	 * processor.
729d7c51706SLoic Pallardy 	 *
730d7c51706SLoic Pallardy 	 * Obviously such remote processor devices should not be configured
731d7c51706SLoic Pallardy 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
732d7c51706SLoic Pallardy 	 * physical address in this case.
733d7c51706SLoic Pallardy 	 */
73460f849a5SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) {
735d7c51706SLoic Pallardy 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
736d7c51706SLoic Pallardy 		if (!mapping) {
737d7c51706SLoic Pallardy 			ret = -ENOMEM;
738d7c51706SLoic Pallardy 			goto dma_free;
739d7c51706SLoic Pallardy 		}
740d7c51706SLoic Pallardy 
741d7c51706SLoic Pallardy 		ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
7421369459bSJason Gunthorpe 				mem->flags, GFP_KERNEL);
743d7c51706SLoic Pallardy 		if (ret) {
744d7c51706SLoic Pallardy 			dev_err(dev, "iommu_map failed: %d\n", ret);
745d7c51706SLoic Pallardy 			goto free_mapping;
746d7c51706SLoic Pallardy 		}
747d7c51706SLoic Pallardy 
748d7c51706SLoic Pallardy 		/*
749d7c51706SLoic Pallardy 		 * We'll need this info later when we'll want to unmap
750d7c51706SLoic Pallardy 		 * everything (e.g. on shutdown).
751d7c51706SLoic Pallardy 		 *
752d7c51706SLoic Pallardy 		 * We can't trust the remote processor not to change the
753d7c51706SLoic Pallardy 		 * resource table, so we must maintain this info independently.
754d7c51706SLoic Pallardy 		 */
755d7c51706SLoic Pallardy 		mapping->da = mem->da;
756d7c51706SLoic Pallardy 		mapping->len = mem->len;
757d7c51706SLoic Pallardy 		list_add_tail(&mapping->node, &rproc->mappings);
758d7c51706SLoic Pallardy 
759d7c51706SLoic Pallardy 		dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
760d7c51706SLoic Pallardy 			mem->da, &dma);
76160f849a5SLoic Pallardy 	}
76260f849a5SLoic Pallardy 
76360f849a5SLoic Pallardy 	if (mem->da == FW_RSC_ADDR_ANY) {
764b36de8cfSLoic Pallardy 		/* Update device address as undefined by requester */
765b36de8cfSLoic Pallardy 		if ((u64)dma & HIGH_BITS_MASK)
766b36de8cfSLoic Pallardy 			dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n");
767b36de8cfSLoic Pallardy 
768d7c51706SLoic Pallardy 		mem->da = (u32)dma;
769d7c51706SLoic Pallardy 	}
770d7c51706SLoic Pallardy 
77180137b40SLoic Pallardy 	mem->dma = dma;
772d7c51706SLoic Pallardy 	mem->va = va;
773d7c51706SLoic Pallardy 
774d7c51706SLoic Pallardy 	return 0;
775d7c51706SLoic Pallardy 
776d7c51706SLoic Pallardy free_mapping:
777d7c51706SLoic Pallardy 	kfree(mapping);
778d7c51706SLoic Pallardy dma_free:
779d7c51706SLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, va, dma);
780d7c51706SLoic Pallardy 	return ret;
781d7c51706SLoic Pallardy }
782d7c51706SLoic Pallardy 
783d7c51706SLoic Pallardy /**
784f2e74abfSLoic Pallardy  * rproc_release_carveout() - release acquired carveout
785f2e74abfSLoic Pallardy  * @rproc: rproc handle
786f2e74abfSLoic Pallardy  * @mem: the memory entry to release
787f2e74abfSLoic Pallardy  *
788f2e74abfSLoic Pallardy  * This function releases specified memory entry @mem allocated via
789d7c51706SLoic Pallardy  * rproc_alloc_carveout() function by @rproc.
790f2867434SSuman Anna  *
791f2867434SSuman Anna  * Return: 0 on success, or an appropriate error code otherwise
792f2e74abfSLoic Pallardy  */
rproc_release_carveout(struct rproc * rproc,struct rproc_mem_entry * mem)793f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc,
794f2e74abfSLoic Pallardy 				  struct rproc_mem_entry *mem)
795f2e74abfSLoic Pallardy {
796f2e74abfSLoic Pallardy 	struct device *dev = &rproc->dev;
797f2e74abfSLoic Pallardy 
798f2e74abfSLoic Pallardy 	/* clean up carveout allocations */
799f2e74abfSLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
800f2e74abfSLoic Pallardy 	return 0;
801f2e74abfSLoic Pallardy }
802f2e74abfSLoic Pallardy 
803f2e74abfSLoic Pallardy /**
804400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
805400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
8062bf23461SJindong Yue  * @ptr: the resource entry
8072e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
808fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
809400e64dfSOhad Ben-Cohen  *
810400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
811400e64dfSOhad Ben-Cohen  * contiguous memory regions.
812400e64dfSOhad Ben-Cohen  *
813400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
814400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
815400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
816400e64dfSOhad Ben-Cohen  *
817400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
818400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
819400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
820400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
821f2867434SSuman Anna  *
822f2867434SSuman Anna  * Return: 0 on success, or an appropriate error code otherwise
823400e64dfSOhad Ben-Cohen  */
rproc_handle_carveout(struct rproc * rproc,void * ptr,int offset,int avail)824fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
8252bf23461SJindong Yue 				 void *ptr, int offset, int avail)
826400e64dfSOhad Ben-Cohen {
8272bf23461SJindong Yue 	struct fw_rsc_carveout *rsc = ptr;
828d7c51706SLoic Pallardy 	struct rproc_mem_entry *carveout;
829b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
830400e64dfSOhad Ben-Cohen 
831fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
832b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
833fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
834fd2c15ecSOhad Ben-Cohen 	}
835fd2c15ecSOhad Ben-Cohen 
836fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
837fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
838fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
839fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
840fd2c15ecSOhad Ben-Cohen 	}
841fd2c15ecSOhad Ben-Cohen 
8429d7814a9SAnna, Suman 	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
84335386166SLee Jones 		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
844fd2c15ecSOhad Ben-Cohen 
845ffa5f9c8SLoic Pallardy 	/*
846ffa5f9c8SLoic Pallardy 	 * Check carveout rsc already part of a registered carveout,
847ffa5f9c8SLoic Pallardy 	 * Search by name, then check the da and length
848ffa5f9c8SLoic Pallardy 	 */
849ffa5f9c8SLoic Pallardy 	carveout = rproc_find_carveout_by_name(rproc, rsc->name);
850ffa5f9c8SLoic Pallardy 
851ffa5f9c8SLoic Pallardy 	if (carveout) {
852ffa5f9c8SLoic Pallardy 		if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
853ffa5f9c8SLoic Pallardy 			dev_err(dev,
854ffa5f9c8SLoic Pallardy 				"Carveout already associated to resource table\n");
855ffa5f9c8SLoic Pallardy 			return -ENOMEM;
856ffa5f9c8SLoic Pallardy 		}
857ffa5f9c8SLoic Pallardy 
858ffa5f9c8SLoic Pallardy 		if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len))
859ffa5f9c8SLoic Pallardy 			return -ENOMEM;
860ffa5f9c8SLoic Pallardy 
861ffa5f9c8SLoic Pallardy 		/* Update memory carveout with resource table info */
862ffa5f9c8SLoic Pallardy 		carveout->rsc_offset = offset;
863ffa5f9c8SLoic Pallardy 		carveout->flags = rsc->flags;
864ffa5f9c8SLoic Pallardy 
865ffa5f9c8SLoic Pallardy 		return 0;
866ffa5f9c8SLoic Pallardy 	}
867ffa5f9c8SLoic Pallardy 
868bf24ecc8Swangjianli 	/* Register carveout in list */
86999cf0361SBen Dooks (Codethink) 	carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
870d7c51706SLoic Pallardy 					rproc_alloc_carveout,
871d7c51706SLoic Pallardy 					rproc_release_carveout, rsc->name);
872d7c51706SLoic Pallardy 	if (!carveout) {
873d7c51706SLoic Pallardy 		dev_err(dev, "Can't allocate memory entry structure\n");
87472029c90SLoic Pallardy 		return -ENOMEM;
875400e64dfSOhad Ben-Cohen 	}
876400e64dfSOhad Ben-Cohen 
877d7c51706SLoic Pallardy 	carveout->flags = rsc->flags;
878d7c51706SLoic Pallardy 	carveout->rsc_offset = offset;
87915c0b025SLoic Pallardy 	rproc_add_carveout(rproc, carveout);
880400e64dfSOhad Ben-Cohen 
881400e64dfSOhad Ben-Cohen 	return 0;
882400e64dfSOhad Ben-Cohen }
883400e64dfSOhad Ben-Cohen 
88472029c90SLoic Pallardy /**
88515c0b025SLoic Pallardy  * rproc_add_carveout() - register an allocated carveout region
88615c0b025SLoic Pallardy  * @rproc: rproc handle
88715c0b025SLoic Pallardy  * @mem: memory entry to register
88815c0b025SLoic Pallardy  *
88915c0b025SLoic Pallardy  * This function registers specified memory entry in @rproc carveouts list.
89015c0b025SLoic Pallardy  * Specified carveout should have been allocated before registering.
89115c0b025SLoic Pallardy  */
rproc_add_carveout(struct rproc * rproc,struct rproc_mem_entry * mem)89215c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
89315c0b025SLoic Pallardy {
89415c0b025SLoic Pallardy 	list_add_tail(&mem->node, &rproc->carveouts);
89515c0b025SLoic Pallardy }
89615c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout);
89715c0b025SLoic Pallardy 
89815c0b025SLoic Pallardy /**
89972029c90SLoic Pallardy  * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
90072029c90SLoic Pallardy  * @dev: pointer on device struct
90172029c90SLoic Pallardy  * @va: virtual address
90272029c90SLoic Pallardy  * @dma: dma address
90372029c90SLoic Pallardy  * @len: memory carveout length
90472029c90SLoic Pallardy  * @da: device address
905a9f6fe0dSLoic Pallardy  * @alloc: memory carveout allocation function
906a9f6fe0dSLoic Pallardy  * @release: memory carveout release function
90772029c90SLoic Pallardy  * @name: carveout name
90872029c90SLoic Pallardy  *
90972029c90SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
91072029c90SLoic Pallardy  * provided by client.
911f2867434SSuman Anna  *
912f2867434SSuman Anna  * Return: a valid pointer on success, or NULL on failure
91372029c90SLoic Pallardy  */
9147e05c8deSLee Jones __printf(8, 9)
91572029c90SLoic Pallardy struct rproc_mem_entry *
rproc_mem_entry_init(struct device * dev,void * va,dma_addr_t dma,size_t len,u32 da,int (* alloc)(struct rproc *,struct rproc_mem_entry *),int (* release)(struct rproc *,struct rproc_mem_entry *),const char * name,...)91672029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev,
917096ee786SClement Leger 		     void *va, dma_addr_t dma, size_t len, u32 da,
918d7c51706SLoic Pallardy 		     int (*alloc)(struct rproc *, struct rproc_mem_entry *),
91972029c90SLoic Pallardy 		     int (*release)(struct rproc *, struct rproc_mem_entry *),
92072029c90SLoic Pallardy 		     const char *name, ...)
92172029c90SLoic Pallardy {
92272029c90SLoic Pallardy 	struct rproc_mem_entry *mem;
92372029c90SLoic Pallardy 	va_list args;
92472029c90SLoic Pallardy 
92572029c90SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
92672029c90SLoic Pallardy 	if (!mem)
92772029c90SLoic Pallardy 		return mem;
92872029c90SLoic Pallardy 
92972029c90SLoic Pallardy 	mem->va = va;
93072029c90SLoic Pallardy 	mem->dma = dma;
93172029c90SLoic Pallardy 	mem->da = da;
93272029c90SLoic Pallardy 	mem->len = len;
933d7c51706SLoic Pallardy 	mem->alloc = alloc;
93472029c90SLoic Pallardy 	mem->release = release;
935d7c51706SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
9361429cca1SLoic Pallardy 	mem->of_resm_idx = -1;
93772029c90SLoic Pallardy 
93872029c90SLoic Pallardy 	va_start(args, name);
93972029c90SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
94072029c90SLoic Pallardy 	va_end(args);
94172029c90SLoic Pallardy 
94272029c90SLoic Pallardy 	return mem;
94372029c90SLoic Pallardy }
94472029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init);
94572029c90SLoic Pallardy 
94672029c90SLoic Pallardy /**
9471429cca1SLoic Pallardy  * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
9481429cca1SLoic Pallardy  * from a reserved memory phandle
9491429cca1SLoic Pallardy  * @dev: pointer on device struct
9501429cca1SLoic Pallardy  * @of_resm_idx: reserved memory phandle index in "memory-region"
9511429cca1SLoic Pallardy  * @len: memory carveout length
9521429cca1SLoic Pallardy  * @da: device address
9531429cca1SLoic Pallardy  * @name: carveout name
9541429cca1SLoic Pallardy  *
9551429cca1SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
9561429cca1SLoic Pallardy  * provided by client.
957f2867434SSuman Anna  *
958f2867434SSuman Anna  * Return: a valid pointer on success, or NULL on failure
9591429cca1SLoic Pallardy  */
9607e05c8deSLee Jones __printf(5, 6)
9611429cca1SLoic Pallardy struct rproc_mem_entry *
rproc_of_resm_mem_entry_init(struct device * dev,u32 of_resm_idx,size_t len,u32 da,const char * name,...)962096ee786SClement Leger rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len,
9631429cca1SLoic Pallardy 			     u32 da, const char *name, ...)
9641429cca1SLoic Pallardy {
9651429cca1SLoic Pallardy 	struct rproc_mem_entry *mem;
9661429cca1SLoic Pallardy 	va_list args;
9671429cca1SLoic Pallardy 
9681429cca1SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
9691429cca1SLoic Pallardy 	if (!mem)
9701429cca1SLoic Pallardy 		return mem;
9711429cca1SLoic Pallardy 
9721429cca1SLoic Pallardy 	mem->da = da;
9731429cca1SLoic Pallardy 	mem->len = len;
9741429cca1SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
9751429cca1SLoic Pallardy 	mem->of_resm_idx = of_resm_idx;
9761429cca1SLoic Pallardy 
9771429cca1SLoic Pallardy 	va_start(args, name);
9781429cca1SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
9791429cca1SLoic Pallardy 	va_end(args);
9801429cca1SLoic Pallardy 
9811429cca1SLoic Pallardy 	return mem;
9821429cca1SLoic Pallardy }
9831429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init);
9841429cca1SLoic Pallardy 
985a8aa5ee1SSuman Anna /**
986a8aa5ee1SSuman Anna  * rproc_of_parse_firmware() - parse and return the firmware-name
987a8aa5ee1SSuman Anna  * @dev: pointer on device struct representing a rproc
988a8aa5ee1SSuman Anna  * @index: index to use for the firmware-name retrieval
989a8aa5ee1SSuman Anna  * @fw_name: pointer to a character string, in which the firmware
990a8aa5ee1SSuman Anna  *           name is returned on success and unmodified otherwise.
991a8aa5ee1SSuman Anna  *
992a8aa5ee1SSuman Anna  * This is an OF helper function that parses a device's DT node for
993a8aa5ee1SSuman Anna  * the "firmware-name" property and returns the firmware name pointer
994a8aa5ee1SSuman Anna  * in @fw_name on success.
995a8aa5ee1SSuman Anna  *
996a8aa5ee1SSuman Anna  * Return: 0 on success, or an appropriate failure.
997a8aa5ee1SSuman Anna  */
rproc_of_parse_firmware(struct device * dev,int index,const char ** fw_name)998a8aa5ee1SSuman Anna int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name)
999a8aa5ee1SSuman Anna {
1000a8aa5ee1SSuman Anna 	int ret;
1001a8aa5ee1SSuman Anna 
1002a8aa5ee1SSuman Anna 	ret = of_property_read_string_index(dev->of_node, "firmware-name",
1003a8aa5ee1SSuman Anna 					    index, fw_name);
1004a8aa5ee1SSuman Anna 	return ret ? ret : 0;
1005a8aa5ee1SSuman Anna }
1006a8aa5ee1SSuman Anna EXPORT_SYMBOL(rproc_of_parse_firmware);
1007a8aa5ee1SSuman Anna 
10082e7d4c2cSArnaud Pouliquen /*
1009e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
1010e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
1011e12bc14bSOhad Ben-Cohen  */
1012232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
10132bf23461SJindong Yue 	[RSC_CARVEOUT] = rproc_handle_carveout,
10142bf23461SJindong Yue 	[RSC_DEVMEM] = rproc_handle_devmem,
10152bf23461SJindong Yue 	[RSC_TRACE] = rproc_handle_trace,
10162bf23461SJindong Yue 	[RSC_VDEV] = rproc_handle_vdev,
1017232fcdbbSSjur Brændeland };
1018232fcdbbSSjur Brændeland 
1019400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
rproc_handle_resources(struct rproc * rproc,rproc_handle_resource_t handlers[RSC_LAST])1020a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc,
1021232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
1022400e64dfSOhad Ben-Cohen {
1023b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1024e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
1025fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
1026400e64dfSOhad Ben-Cohen 
1027d4bb86f2SBjorn Andersson 	if (!rproc->table_ptr)
1028d4bb86f2SBjorn Andersson 		return 0;
1029d4bb86f2SBjorn Andersson 
1030a2b950acSOhad Ben-Cohen 	for (i = 0; i < rproc->table_ptr->num; i++) {
1031a2b950acSOhad Ben-Cohen 		int offset = rproc->table_ptr->offset[i];
1032a2b950acSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
1033a4b24c75SBjorn Andersson 		int avail = rproc->table_sz - offset - sizeof(*hdr);
1034fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
1035400e64dfSOhad Ben-Cohen 
1036fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
1037fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
1038fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
1039fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
1040fd2c15ecSOhad Ben-Cohen 		}
1041fd2c15ecSOhad Ben-Cohen 
1042fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
1043fd2c15ecSOhad Ben-Cohen 
1044b1a17513SClement Leger 		if (hdr->type >= RSC_VENDOR_START &&
1045b1a17513SClement Leger 		    hdr->type <= RSC_VENDOR_END) {
1046b1a17513SClement Leger 			ret = rproc_handle_rsc(rproc, hdr->type, rsc,
1047b1a17513SClement Leger 					       offset + sizeof(*hdr), avail);
1048b1a17513SClement Leger 			if (ret == RSC_HANDLED)
1049b1a17513SClement Leger 				continue;
1050b1a17513SClement Leger 			else if (ret < 0)
1051b1a17513SClement Leger 				break;
1052b1a17513SClement Leger 
1053b1a17513SClement Leger 			dev_warn(dev, "unsupported vendor resource %d\n",
1054b1a17513SClement Leger 				 hdr->type);
1055b1a17513SClement Leger 			continue;
1056b1a17513SClement Leger 		}
1057b1a17513SClement Leger 
1058fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
1059fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
1060e12bc14bSOhad Ben-Cohen 			continue;
1061400e64dfSOhad Ben-Cohen 		}
1062400e64dfSOhad Ben-Cohen 
1063232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
1064e12bc14bSOhad Ben-Cohen 		if (!handler)
1065e12bc14bSOhad Ben-Cohen 			continue;
1066e12bc14bSOhad Ben-Cohen 
1067a2b950acSOhad Ben-Cohen 		ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
10687a186941SOhad Ben-Cohen 		if (ret)
1069400e64dfSOhad Ben-Cohen 			break;
1070400e64dfSOhad Ben-Cohen 	}
1071400e64dfSOhad Ben-Cohen 
1072400e64dfSOhad Ben-Cohen 	return ret;
1073400e64dfSOhad Ben-Cohen }
1074400e64dfSOhad Ben-Cohen 
rproc_prepare_subdevices(struct rproc * rproc)1075c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc)
1076c455daa4SBjorn Andersson {
1077c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1078c455daa4SBjorn Andersson 	int ret;
1079c455daa4SBjorn Andersson 
1080c455daa4SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1081c455daa4SBjorn Andersson 		if (subdev->prepare) {
1082c455daa4SBjorn Andersson 			ret = subdev->prepare(subdev);
1083c455daa4SBjorn Andersson 			if (ret)
1084c455daa4SBjorn Andersson 				goto unroll_preparation;
1085c455daa4SBjorn Andersson 		}
1086c455daa4SBjorn Andersson 	}
1087c455daa4SBjorn Andersson 
1088c455daa4SBjorn Andersson 	return 0;
1089c455daa4SBjorn Andersson 
1090c455daa4SBjorn Andersson unroll_preparation:
1091c455daa4SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1092c455daa4SBjorn Andersson 		if (subdev->unprepare)
1093c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1094c455daa4SBjorn Andersson 	}
1095c455daa4SBjorn Andersson 
1096c455daa4SBjorn Andersson 	return ret;
1097c455daa4SBjorn Andersson }
1098c455daa4SBjorn Andersson 
rproc_start_subdevices(struct rproc * rproc)1099618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc)
11007bdc9650SBjorn Andersson {
11017bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
11027bdc9650SBjorn Andersson 	int ret;
11037bdc9650SBjorn Andersson 
11047bdc9650SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1105be37b1e0SBjorn Andersson 		if (subdev->start) {
1106618fcff3SBjorn Andersson 			ret = subdev->start(subdev);
11077bdc9650SBjorn Andersson 			if (ret)
11087bdc9650SBjorn Andersson 				goto unroll_registration;
11097bdc9650SBjorn Andersson 		}
1110be37b1e0SBjorn Andersson 	}
11117bdc9650SBjorn Andersson 
11127bdc9650SBjorn Andersson 	return 0;
11137bdc9650SBjorn Andersson 
11147bdc9650SBjorn Andersson unroll_registration:
1115be37b1e0SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1116be37b1e0SBjorn Andersson 		if (subdev->stop)
1117618fcff3SBjorn Andersson 			subdev->stop(subdev, true);
1118be37b1e0SBjorn Andersson 	}
11197bdc9650SBjorn Andersson 
11207bdc9650SBjorn Andersson 	return ret;
11217bdc9650SBjorn Andersson }
11227bdc9650SBjorn Andersson 
rproc_stop_subdevices(struct rproc * rproc,bool crashed)1123618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
11247bdc9650SBjorn Andersson {
11257bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
11267bdc9650SBjorn Andersson 
1127be37b1e0SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1128be37b1e0SBjorn Andersson 		if (subdev->stop)
1129618fcff3SBjorn Andersson 			subdev->stop(subdev, crashed);
11307bdc9650SBjorn Andersson 	}
1131be37b1e0SBjorn Andersson }
11327bdc9650SBjorn Andersson 
rproc_unprepare_subdevices(struct rproc * rproc)1133c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc)
1134c455daa4SBjorn Andersson {
1135c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1136c455daa4SBjorn Andersson 
1137c455daa4SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1138c455daa4SBjorn Andersson 		if (subdev->unprepare)
1139c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1140c455daa4SBjorn Andersson 	}
1141c455daa4SBjorn Andersson }
1142c455daa4SBjorn Andersson 
1143400e64dfSOhad Ben-Cohen /**
1144d7c51706SLoic Pallardy  * rproc_alloc_registered_carveouts() - allocate all carveouts registered
1145d7c51706SLoic Pallardy  * in the list
1146d7c51706SLoic Pallardy  * @rproc: the remote processor handle
1147d7c51706SLoic Pallardy  *
1148d7c51706SLoic Pallardy  * This function parses registered carveout list, performs allocation
1149d7c51706SLoic Pallardy  * if alloc() ops registered and updates resource table information
1150d7c51706SLoic Pallardy  * if rsc_offset set.
1151d7c51706SLoic Pallardy  *
1152d7c51706SLoic Pallardy  * Return: 0 on success
1153d7c51706SLoic Pallardy  */
rproc_alloc_registered_carveouts(struct rproc * rproc)1154d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc)
1155d7c51706SLoic Pallardy {
1156d7c51706SLoic Pallardy 	struct rproc_mem_entry *entry, *tmp;
1157d7c51706SLoic Pallardy 	struct fw_rsc_carveout *rsc;
1158d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
1159b36de8cfSLoic Pallardy 	u64 pa;
1160d7c51706SLoic Pallardy 	int ret;
1161d7c51706SLoic Pallardy 
1162d7c51706SLoic Pallardy 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1163d7c51706SLoic Pallardy 		if (entry->alloc) {
1164d7c51706SLoic Pallardy 			ret = entry->alloc(rproc, entry);
1165d7c51706SLoic Pallardy 			if (ret) {
1166d7c51706SLoic Pallardy 				dev_err(dev, "Unable to allocate carveout %s: %d\n",
1167d7c51706SLoic Pallardy 					entry->name, ret);
1168d7c51706SLoic Pallardy 				return -ENOMEM;
1169d7c51706SLoic Pallardy 			}
1170d7c51706SLoic Pallardy 		}
1171d7c51706SLoic Pallardy 
1172d7c51706SLoic Pallardy 		if (entry->rsc_offset != FW_RSC_ADDR_ANY) {
1173d7c51706SLoic Pallardy 			/* update resource table */
1174d7c51706SLoic Pallardy 			rsc = (void *)rproc->table_ptr + entry->rsc_offset;
1175d7c51706SLoic Pallardy 
1176d7c51706SLoic Pallardy 			/*
1177d7c51706SLoic Pallardy 			 * Some remote processors might need to know the pa
1178d7c51706SLoic Pallardy 			 * even though they are behind an IOMMU. E.g., OMAP4's
1179d7c51706SLoic Pallardy 			 * remote M3 processor needs this so it can control
1180d7c51706SLoic Pallardy 			 * on-chip hardware accelerators that are not behind
1181d7c51706SLoic Pallardy 			 * the IOMMU, and therefor must know the pa.
1182d7c51706SLoic Pallardy 			 *
1183d7c51706SLoic Pallardy 			 * Generally we don't want to expose physical addresses
1184d7c51706SLoic Pallardy 			 * if we don't have to (remote processors are generally
1185d7c51706SLoic Pallardy 			 * _not_ trusted), so we might want to do this only for
1186d7c51706SLoic Pallardy 			 * remote processor that _must_ have this (e.g. OMAP4's
1187d7c51706SLoic Pallardy 			 * dual M3 subsystem).
1188d7c51706SLoic Pallardy 			 *
1189d7c51706SLoic Pallardy 			 * Non-IOMMU processors might also want to have this info.
1190d7c51706SLoic Pallardy 			 * In this case, the device address and the physical address
1191d7c51706SLoic Pallardy 			 * are the same.
1192d7c51706SLoic Pallardy 			 */
1193ffa5f9c8SLoic Pallardy 
1194ffa5f9c8SLoic Pallardy 			/* Use va if defined else dma to generate pa */
1195d7c51706SLoic Pallardy 			if (entry->va)
1196b36de8cfSLoic Pallardy 				pa = (u64)rproc_va_to_pa(entry->va);
1197ffa5f9c8SLoic Pallardy 			else
1198b36de8cfSLoic Pallardy 				pa = (u64)entry->dma;
1199ffa5f9c8SLoic Pallardy 
1200b36de8cfSLoic Pallardy 			if (((u64)pa) & HIGH_BITS_MASK)
1201b36de8cfSLoic Pallardy 				dev_warn(dev,
1202b36de8cfSLoic Pallardy 					 "Physical address cast in 32bit to fit resource table format\n");
1203b36de8cfSLoic Pallardy 
1204b36de8cfSLoic Pallardy 			rsc->pa = (u32)pa;
1205ffa5f9c8SLoic Pallardy 			rsc->da = entry->da;
1206ffa5f9c8SLoic Pallardy 			rsc->len = entry->len;
1207d7c51706SLoic Pallardy 		}
1208d7c51706SLoic Pallardy 	}
1209d7c51706SLoic Pallardy 
1210d7c51706SLoic Pallardy 	return 0;
1211d7c51706SLoic Pallardy }
1212d7c51706SLoic Pallardy 
12132666ca91SSarangdhar Joshi 
12142666ca91SSarangdhar Joshi /**
1215400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
1216400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
1217400e64dfSOhad Ben-Cohen  *
1218400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
12197a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
1220400e64dfSOhad Ben-Cohen  */
rproc_resource_cleanup(struct rproc * rproc)1221d9473cbfSMathieu Poirier void rproc_resource_cleanup(struct rproc *rproc)
1222400e64dfSOhad Ben-Cohen {
1223400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
1224a987e6b9SLoic Pallardy 	struct rproc_debug_trace *trace, *ttmp;
1225d81fb32fSBjorn Andersson 	struct rproc_vdev *rvdev, *rvtmp;
1226b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1227400e64dfSOhad Ben-Cohen 
1228400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
1229a987e6b9SLoic Pallardy 	list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) {
1230a987e6b9SLoic Pallardy 		rproc_remove_trace_file(trace->tfile);
1231400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
1232a987e6b9SLoic Pallardy 		list_del(&trace->node);
1233a987e6b9SLoic Pallardy 		kfree(trace);
1234400e64dfSOhad Ben-Cohen 	}
1235400e64dfSOhad Ben-Cohen 
1236400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
1237400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
1238400e64dfSOhad Ben-Cohen 		size_t unmapped;
1239400e64dfSOhad Ben-Cohen 
1240400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
1241400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
1242400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
1243096ee786SClement Leger 			dev_err(dev, "failed to unmap %zx/%zu\n", entry->len,
1244400e64dfSOhad Ben-Cohen 				unmapped);
1245400e64dfSOhad Ben-Cohen 		}
1246400e64dfSOhad Ben-Cohen 
1247400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
1248400e64dfSOhad Ben-Cohen 		kfree(entry);
1249400e64dfSOhad Ben-Cohen 	}
1250b6356a01SSuman Anna 
1251b6356a01SSuman Anna 	/* clean up carveout allocations */
1252b6356a01SSuman Anna 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1253f2e74abfSLoic Pallardy 		if (entry->release)
1254f2e74abfSLoic Pallardy 			entry->release(rproc, entry);
1255b6356a01SSuman Anna 		list_del(&entry->node);
1256b6356a01SSuman Anna 		kfree(entry);
1257b6356a01SSuman Anna 	}
1258d81fb32fSBjorn Andersson 
1259d81fb32fSBjorn Andersson 	/* clean up remote vdev entries */
1260f5bcb353SBjorn Andersson 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
12611d7b61c0SArnaud Pouliquen 		platform_device_unregister(rvdev->pdev);
12622666ca91SSarangdhar Joshi 
12632666ca91SSarangdhar Joshi 	rproc_coredump_cleanup(rproc);
12642b45cef5SBjorn Andersson }
1265d9473cbfSMathieu Poirier EXPORT_SYMBOL(rproc_resource_cleanup);
1266400e64dfSOhad Ben-Cohen 
rproc_start(struct rproc * rproc,const struct firmware * fw)12671efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw)
12681efa30d0SSarangdhar Joshi {
1269a4b24c75SBjorn Andersson 	struct resource_table *loaded_table;
12701efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
1271a4b24c75SBjorn Andersson 	int ret;
12721efa30d0SSarangdhar Joshi 
12731efa30d0SSarangdhar Joshi 	/* load the ELF segments to memory */
12741efa30d0SSarangdhar Joshi 	ret = rproc_load_segments(rproc, fw);
12751efa30d0SSarangdhar Joshi 	if (ret) {
12761efa30d0SSarangdhar Joshi 		dev_err(dev, "Failed to load program segments: %d\n", ret);
12771efa30d0SSarangdhar Joshi 		return ret;
12781efa30d0SSarangdhar Joshi 	}
12791efa30d0SSarangdhar Joshi 
12801efa30d0SSarangdhar Joshi 	/*
12811efa30d0SSarangdhar Joshi 	 * The starting device has been given the rproc->cached_table as the
12821efa30d0SSarangdhar Joshi 	 * resource table. The address of the vring along with the other
12831efa30d0SSarangdhar Joshi 	 * allocated resources (carveouts etc) is stored in cached_table.
12841efa30d0SSarangdhar Joshi 	 * In order to pass this information to the remote device we must copy
12851efa30d0SSarangdhar Joshi 	 * this information to device memory. We also update the table_ptr so
12861efa30d0SSarangdhar Joshi 	 * that any subsequent changes will be applied to the loaded version.
12871efa30d0SSarangdhar Joshi 	 */
12881efa30d0SSarangdhar Joshi 	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
12891efa30d0SSarangdhar Joshi 	if (loaded_table) {
1290a4b24c75SBjorn Andersson 		memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
12911efa30d0SSarangdhar Joshi 		rproc->table_ptr = loaded_table;
12921efa30d0SSarangdhar Joshi 	}
12931efa30d0SSarangdhar Joshi 
1294c455daa4SBjorn Andersson 	ret = rproc_prepare_subdevices(rproc);
1295c455daa4SBjorn Andersson 	if (ret) {
1296c455daa4SBjorn Andersson 		dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1297c455daa4SBjorn Andersson 			rproc->name, ret);
1298f68d51bdSSuman Anna 		goto reset_table_ptr;
1299c455daa4SBjorn Andersson 	}
1300c455daa4SBjorn Andersson 
13011efa30d0SSarangdhar Joshi 	/* power up the remote processor */
13021efa30d0SSarangdhar Joshi 	ret = rproc->ops->start(rproc);
13031efa30d0SSarangdhar Joshi 	if (ret) {
13041efa30d0SSarangdhar Joshi 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1305c455daa4SBjorn Andersson 		goto unprepare_subdevices;
13061efa30d0SSarangdhar Joshi 	}
13071efa30d0SSarangdhar Joshi 
1308618fcff3SBjorn Andersson 	/* Start any subdevices for the remote processor */
1309618fcff3SBjorn Andersson 	ret = rproc_start_subdevices(rproc);
13101efa30d0SSarangdhar Joshi 	if (ret) {
13111efa30d0SSarangdhar Joshi 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
13121efa30d0SSarangdhar Joshi 			rproc->name, ret);
1313c455daa4SBjorn Andersson 		goto stop_rproc;
13141efa30d0SSarangdhar Joshi 	}
13151efa30d0SSarangdhar Joshi 
13161efa30d0SSarangdhar Joshi 	rproc->state = RPROC_RUNNING;
13171efa30d0SSarangdhar Joshi 
13181efa30d0SSarangdhar Joshi 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
13191efa30d0SSarangdhar Joshi 
13201efa30d0SSarangdhar Joshi 	return 0;
1321c455daa4SBjorn Andersson 
1322c455daa4SBjorn Andersson stop_rproc:
1323c455daa4SBjorn Andersson 	rproc->ops->stop(rproc);
1324c455daa4SBjorn Andersson unprepare_subdevices:
1325c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1326f68d51bdSSuman Anna reset_table_ptr:
1327f68d51bdSSuman Anna 	rproc->table_ptr = rproc->cached_table;
1328c455daa4SBjorn Andersson 
1329c455daa4SBjorn Andersson 	return ret;
13301efa30d0SSarangdhar Joshi }
13311efa30d0SSarangdhar Joshi 
__rproc_attach(struct rproc * rproc)13326a6c4dc0SMathieu Poirier static int __rproc_attach(struct rproc *rproc)
1333d848a481SMathieu Poirier {
1334d848a481SMathieu Poirier 	struct device *dev = &rproc->dev;
1335d848a481SMathieu Poirier 	int ret;
1336d848a481SMathieu Poirier 
1337d848a481SMathieu Poirier 	ret = rproc_prepare_subdevices(rproc);
1338d848a481SMathieu Poirier 	if (ret) {
1339d848a481SMathieu Poirier 		dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1340d848a481SMathieu Poirier 			rproc->name, ret);
1341d848a481SMathieu Poirier 		goto out;
1342d848a481SMathieu Poirier 	}
1343d848a481SMathieu Poirier 
1344d848a481SMathieu Poirier 	/* Attach to the remote processor */
1345d848a481SMathieu Poirier 	ret = rproc_attach_device(rproc);
1346d848a481SMathieu Poirier 	if (ret) {
1347d848a481SMathieu Poirier 		dev_err(dev, "can't attach to rproc %s: %d\n",
1348d848a481SMathieu Poirier 			rproc->name, ret);
1349d848a481SMathieu Poirier 		goto unprepare_subdevices;
1350d848a481SMathieu Poirier 	}
1351d848a481SMathieu Poirier 
1352d848a481SMathieu Poirier 	/* Start any subdevices for the remote processor */
1353d848a481SMathieu Poirier 	ret = rproc_start_subdevices(rproc);
1354d848a481SMathieu Poirier 	if (ret) {
1355d848a481SMathieu Poirier 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
1356d848a481SMathieu Poirier 			rproc->name, ret);
1357d848a481SMathieu Poirier 		goto stop_rproc;
1358d848a481SMathieu Poirier 	}
1359d848a481SMathieu Poirier 
136076f4c875SMathieu Poirier 	rproc->state = RPROC_ATTACHED;
1361d848a481SMathieu Poirier 
1362d848a481SMathieu Poirier 	dev_info(dev, "remote processor %s is now attached\n", rproc->name);
1363d848a481SMathieu Poirier 
1364d848a481SMathieu Poirier 	return 0;
1365d848a481SMathieu Poirier 
1366d848a481SMathieu Poirier stop_rproc:
1367d848a481SMathieu Poirier 	rproc->ops->stop(rproc);
1368d848a481SMathieu Poirier unprepare_subdevices:
1369d848a481SMathieu Poirier 	rproc_unprepare_subdevices(rproc);
1370d848a481SMathieu Poirier out:
1371d848a481SMathieu Poirier 	return ret;
1372d848a481SMathieu Poirier }
1373d848a481SMathieu Poirier 
1374400e64dfSOhad Ben-Cohen /*
1375400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
1376400e64dfSOhad Ben-Cohen  */
rproc_fw_boot(struct rproc * rproc,const struct firmware * fw)1377400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1378400e64dfSOhad Ben-Cohen {
1379b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1380400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
138158b64090SBjorn Andersson 	int ret;
1382400e64dfSOhad Ben-Cohen 
1383400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
1384400e64dfSOhad Ben-Cohen 	if (ret)
1385400e64dfSOhad Ben-Cohen 		return ret;
1386400e64dfSOhad Ben-Cohen 
1387e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
1388400e64dfSOhad Ben-Cohen 
1389400e64dfSOhad Ben-Cohen 	/*
1390400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1391400e64dfSOhad Ben-Cohen 	 * just a nop
1392400e64dfSOhad Ben-Cohen 	 */
1393400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
1394400e64dfSOhad Ben-Cohen 	if (ret) {
1395400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
139649cff125SMathieu Poirier 		return ret;
1397400e64dfSOhad Ben-Cohen 	}
1398400e64dfSOhad Ben-Cohen 
139933467ac3SLoic Pallardy 	/* Prepare rproc for firmware loading if needed */
140033467ac3SLoic Pallardy 	ret = rproc_prepare_device(rproc);
140133467ac3SLoic Pallardy 	if (ret) {
140233467ac3SLoic Pallardy 		dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
140333467ac3SLoic Pallardy 		goto disable_iommu;
140433467ac3SLoic Pallardy 	}
140533467ac3SLoic Pallardy 
14063e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
1407400e64dfSOhad Ben-Cohen 
1408c1d35c1aSBjorn Andersson 	/* Load resource table, core dump segment list etc from the firmware */
1409c1d35c1aSBjorn Andersson 	ret = rproc_parse_fw(rproc, fw);
141058b64090SBjorn Andersson 	if (ret)
141133467ac3SLoic Pallardy 		goto unprepare_rproc;
1412a0c10687SBjorn Andersson 
1413b35d7afcSBjorn Andersson 	/* reset max_notifyid */
1414b35d7afcSBjorn Andersson 	rproc->max_notifyid = -1;
1415b35d7afcSBjorn Andersson 
1416c6aed238SLoic Pallardy 	/* reset handled vdev */
1417c6aed238SLoic Pallardy 	rproc->nb_vdev = 0;
1418c6aed238SLoic Pallardy 
1419400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
1420a4b24c75SBjorn Andersson 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1421400e64dfSOhad Ben-Cohen 	if (ret) {
1422400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
1423229b85a6SBjorn Andersson 		goto clean_up_resources;
1424400e64dfSOhad Ben-Cohen 	}
1425400e64dfSOhad Ben-Cohen 
1426d7c51706SLoic Pallardy 	/* Allocate carveout resources associated to rproc */
1427d7c51706SLoic Pallardy 	ret = rproc_alloc_registered_carveouts(rproc);
1428d7c51706SLoic Pallardy 	if (ret) {
1429d7c51706SLoic Pallardy 		dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1430d7c51706SLoic Pallardy 			ret);
1431d7c51706SLoic Pallardy 		goto clean_up_resources;
1432d7c51706SLoic Pallardy 	}
1433d7c51706SLoic Pallardy 
14341efa30d0SSarangdhar Joshi 	ret = rproc_start(rproc, fw);
14351efa30d0SSarangdhar Joshi 	if (ret)
1436229b85a6SBjorn Andersson 		goto clean_up_resources;
1437400e64dfSOhad Ben-Cohen 
1438400e64dfSOhad Ben-Cohen 	return 0;
1439400e64dfSOhad Ben-Cohen 
1440229b85a6SBjorn Andersson clean_up_resources:
1441229b85a6SBjorn Andersson 	rproc_resource_cleanup(rproc);
1442a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1443a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1444988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
144533467ac3SLoic Pallardy unprepare_rproc:
144633467ac3SLoic Pallardy 	/* release HW resources if needed */
144733467ac3SLoic Pallardy 	rproc_unprepare_device(rproc);
144858b64090SBjorn Andersson disable_iommu:
1449400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1450400e64dfSOhad Ben-Cohen 	return ret;
1451400e64dfSOhad Ben-Cohen }
1452400e64dfSOhad Ben-Cohen 
rproc_set_rsc_table(struct rproc * rproc)14531a631382SMathieu Poirier static int rproc_set_rsc_table(struct rproc *rproc)
14541a631382SMathieu Poirier {
14551a631382SMathieu Poirier 	struct resource_table *table_ptr;
14561a631382SMathieu Poirier 	struct device *dev = &rproc->dev;
14571a631382SMathieu Poirier 	size_t table_sz;
14581a631382SMathieu Poirier 	int ret;
14591a631382SMathieu Poirier 
14601a631382SMathieu Poirier 	table_ptr = rproc_get_loaded_rsc_table(rproc, &table_sz);
14611a631382SMathieu Poirier 	if (!table_ptr) {
14621a631382SMathieu Poirier 		/* Not having a resource table is acceptable */
14631a631382SMathieu Poirier 		return 0;
14641a631382SMathieu Poirier 	}
14651a631382SMathieu Poirier 
14661a631382SMathieu Poirier 	if (IS_ERR(table_ptr)) {
14671a631382SMathieu Poirier 		ret = PTR_ERR(table_ptr);
14681a631382SMathieu Poirier 		dev_err(dev, "can't load resource table: %d\n", ret);
14691a631382SMathieu Poirier 		return ret;
14701a631382SMathieu Poirier 	}
14711a631382SMathieu Poirier 
14729dc9507fSMathieu Poirier 	/*
14739dc9507fSMathieu Poirier 	 * If it is possible to detach the remote processor, keep an untouched
14749dc9507fSMathieu Poirier 	 * copy of the resource table.  That way we can start fresh again when
14759dc9507fSMathieu Poirier 	 * the remote processor is re-attached, that is:
14769dc9507fSMathieu Poirier 	 *
14779dc9507fSMathieu Poirier 	 *      DETACHED -> ATTACHED -> DETACHED -> ATTACHED
14789dc9507fSMathieu Poirier 	 *
14799dc9507fSMathieu Poirier 	 * Free'd in rproc_reset_rsc_table_on_detach() and
14809dc9507fSMathieu Poirier 	 * rproc_reset_rsc_table_on_stop().
14819dc9507fSMathieu Poirier 	 */
14829dc9507fSMathieu Poirier 	if (rproc->ops->detach) {
14839dc9507fSMathieu Poirier 		rproc->clean_table = kmemdup(table_ptr, table_sz, GFP_KERNEL);
14849dc9507fSMathieu Poirier 		if (!rproc->clean_table)
14859dc9507fSMathieu Poirier 			return -ENOMEM;
14869dc9507fSMathieu Poirier 	} else {
14879dc9507fSMathieu Poirier 		rproc->clean_table = NULL;
14889dc9507fSMathieu Poirier 	}
14899dc9507fSMathieu Poirier 
14901a631382SMathieu Poirier 	rproc->cached_table = NULL;
14911a631382SMathieu Poirier 	rproc->table_ptr = table_ptr;
14921a631382SMathieu Poirier 	rproc->table_sz = table_sz;
14931a631382SMathieu Poirier 
14941a631382SMathieu Poirier 	return 0;
14951a631382SMathieu Poirier }
14961a631382SMathieu Poirier 
rproc_reset_rsc_table_on_detach(struct rproc * rproc)14979dc9507fSMathieu Poirier static int rproc_reset_rsc_table_on_detach(struct rproc *rproc)
14989dc9507fSMathieu Poirier {
14999dc9507fSMathieu Poirier 	struct resource_table *table_ptr;
15009dc9507fSMathieu Poirier 
15019dc9507fSMathieu Poirier 	/* A resource table was never retrieved, nothing to do here */
15029dc9507fSMathieu Poirier 	if (!rproc->table_ptr)
15039dc9507fSMathieu Poirier 		return 0;
15049dc9507fSMathieu Poirier 
15059dc9507fSMathieu Poirier 	/*
15069dc9507fSMathieu Poirier 	 * If we made it to this point a clean_table _must_ have been
15079dc9507fSMathieu Poirier 	 * allocated in rproc_set_rsc_table().  If one isn't present
15089dc9507fSMathieu Poirier 	 * something went really wrong and we must complain.
15099dc9507fSMathieu Poirier 	 */
15109dc9507fSMathieu Poirier 	if (WARN_ON(!rproc->clean_table))
15119dc9507fSMathieu Poirier 		return -EINVAL;
15129dc9507fSMathieu Poirier 
15139dc9507fSMathieu Poirier 	/* Remember where the external entity installed the resource table */
15149dc9507fSMathieu Poirier 	table_ptr = rproc->table_ptr;
15159dc9507fSMathieu Poirier 
15169dc9507fSMathieu Poirier 	/*
15179dc9507fSMathieu Poirier 	 * If we made it here the remote processor was started by another
15189dc9507fSMathieu Poirier 	 * entity and a cache table doesn't exist.  As such make a copy of
15199dc9507fSMathieu Poirier 	 * the resource table currently used by the remote processor and
15209dc9507fSMathieu Poirier 	 * use that for the rest of the shutdown process.  The memory
15219dc9507fSMathieu Poirier 	 * allocated here is free'd in rproc_detach().
15229dc9507fSMathieu Poirier 	 */
15239dc9507fSMathieu Poirier 	rproc->cached_table = kmemdup(rproc->table_ptr,
15249dc9507fSMathieu Poirier 				      rproc->table_sz, GFP_KERNEL);
15259dc9507fSMathieu Poirier 	if (!rproc->cached_table)
15269dc9507fSMathieu Poirier 		return -ENOMEM;
15279dc9507fSMathieu Poirier 
15289dc9507fSMathieu Poirier 	/*
15299dc9507fSMathieu Poirier 	 * Use a copy of the resource table for the remainder of the
15309dc9507fSMathieu Poirier 	 * shutdown process.
15319dc9507fSMathieu Poirier 	 */
15329dc9507fSMathieu Poirier 	rproc->table_ptr = rproc->cached_table;
15339dc9507fSMathieu Poirier 
15349dc9507fSMathieu Poirier 	/*
15359dc9507fSMathieu Poirier 	 * Reset the memory area where the firmware loaded the resource table
15369dc9507fSMathieu Poirier 	 * to its original value.  That way when we re-attach the remote
15379dc9507fSMathieu Poirier 	 * processor the resource table is clean and ready to be used again.
15389dc9507fSMathieu Poirier 	 */
15399dc9507fSMathieu Poirier 	memcpy(table_ptr, rproc->clean_table, rproc->table_sz);
15409dc9507fSMathieu Poirier 
15419dc9507fSMathieu Poirier 	/*
15429dc9507fSMathieu Poirier 	 * The clean resource table is no longer needed.  Allocated in
15439dc9507fSMathieu Poirier 	 * rproc_set_rsc_table().
15449dc9507fSMathieu Poirier 	 */
15459dc9507fSMathieu Poirier 	kfree(rproc->clean_table);
15469dc9507fSMathieu Poirier 
15479dc9507fSMathieu Poirier 	return 0;
15489dc9507fSMathieu Poirier }
15499dc9507fSMathieu Poirier 
rproc_reset_rsc_table_on_stop(struct rproc * rproc)15508088dd4dSMathieu Poirier static int rproc_reset_rsc_table_on_stop(struct rproc *rproc)
15518088dd4dSMathieu Poirier {
15528088dd4dSMathieu Poirier 	/* A resource table was never retrieved, nothing to do here */
15538088dd4dSMathieu Poirier 	if (!rproc->table_ptr)
15548088dd4dSMathieu Poirier 		return 0;
15558088dd4dSMathieu Poirier 
15568088dd4dSMathieu Poirier 	/*
15578088dd4dSMathieu Poirier 	 * If a cache table exists the remote processor was started by
15588088dd4dSMathieu Poirier 	 * the remoteproc core.  That cache table should be used for
15598088dd4dSMathieu Poirier 	 * the rest of the shutdown process.
15608088dd4dSMathieu Poirier 	 */
15618088dd4dSMathieu Poirier 	if (rproc->cached_table)
15628088dd4dSMathieu Poirier 		goto out;
15638088dd4dSMathieu Poirier 
15648088dd4dSMathieu Poirier 	/*
15658088dd4dSMathieu Poirier 	 * If we made it here the remote processor was started by another
15668088dd4dSMathieu Poirier 	 * entity and a cache table doesn't exist.  As such make a copy of
15678088dd4dSMathieu Poirier 	 * the resource table currently used by the remote processor and
15688088dd4dSMathieu Poirier 	 * use that for the rest of the shutdown process.  The memory
15698088dd4dSMathieu Poirier 	 * allocated here is free'd in rproc_shutdown().
15708088dd4dSMathieu Poirier 	 */
15718088dd4dSMathieu Poirier 	rproc->cached_table = kmemdup(rproc->table_ptr,
15728088dd4dSMathieu Poirier 				      rproc->table_sz, GFP_KERNEL);
15738088dd4dSMathieu Poirier 	if (!rproc->cached_table)
15748088dd4dSMathieu Poirier 		return -ENOMEM;
15758088dd4dSMathieu Poirier 
15768088dd4dSMathieu Poirier 	/*
15778088dd4dSMathieu Poirier 	 * Since the remote processor is being switched off the clean table
15788088dd4dSMathieu Poirier 	 * won't be needed.  Allocated in rproc_set_rsc_table().
15798088dd4dSMathieu Poirier 	 */
15808088dd4dSMathieu Poirier 	kfree(rproc->clean_table);
15818088dd4dSMathieu Poirier 
15828088dd4dSMathieu Poirier out:
15838088dd4dSMathieu Poirier 	/*
15848088dd4dSMathieu Poirier 	 * Use a copy of the resource table for the remainder of the
15858088dd4dSMathieu Poirier 	 * shutdown process.
15868088dd4dSMathieu Poirier 	 */
15878088dd4dSMathieu Poirier 	rproc->table_ptr = rproc->cached_table;
15888088dd4dSMathieu Poirier 	return 0;
15898088dd4dSMathieu Poirier }
15908088dd4dSMathieu Poirier 
1591400e64dfSOhad Ben-Cohen /*
1592fdf0e00eSMathieu Poirier  * Attach to remote processor - similar to rproc_fw_boot() but without
1593fdf0e00eSMathieu Poirier  * the steps that deal with the firmware image.
1594fdf0e00eSMathieu Poirier  */
rproc_attach(struct rproc * rproc)15956a6c4dc0SMathieu Poirier static int rproc_attach(struct rproc *rproc)
1596fdf0e00eSMathieu Poirier {
1597fdf0e00eSMathieu Poirier 	struct device *dev = &rproc->dev;
1598fdf0e00eSMathieu Poirier 	int ret;
1599fdf0e00eSMathieu Poirier 
1600fdf0e00eSMathieu Poirier 	/*
1601fdf0e00eSMathieu Poirier 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1602fdf0e00eSMathieu Poirier 	 * just a nop
1603fdf0e00eSMathieu Poirier 	 */
1604fdf0e00eSMathieu Poirier 	ret = rproc_enable_iommu(rproc);
1605fdf0e00eSMathieu Poirier 	if (ret) {
1606fdf0e00eSMathieu Poirier 		dev_err(dev, "can't enable iommu: %d\n", ret);
1607fdf0e00eSMathieu Poirier 		return ret;
1608fdf0e00eSMathieu Poirier 	}
1609fdf0e00eSMathieu Poirier 
16106e20a051SArnaud POULIQUEN 	/* Do anything that is needed to boot the remote processor */
16116e20a051SArnaud POULIQUEN 	ret = rproc_prepare_device(rproc);
16126e20a051SArnaud POULIQUEN 	if (ret) {
16136e20a051SArnaud POULIQUEN 		dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
16146e20a051SArnaud POULIQUEN 		goto disable_iommu;
16156e20a051SArnaud POULIQUEN 	}
16166e20a051SArnaud POULIQUEN 
16171a631382SMathieu Poirier 	ret = rproc_set_rsc_table(rproc);
16181a631382SMathieu Poirier 	if (ret) {
16191a631382SMathieu Poirier 		dev_err(dev, "can't load resource table: %d\n", ret);
16206e20a051SArnaud POULIQUEN 		goto unprepare_device;
16211a631382SMathieu Poirier 	}
16221a631382SMathieu Poirier 
1623fdf0e00eSMathieu Poirier 	/* reset max_notifyid */
1624fdf0e00eSMathieu Poirier 	rproc->max_notifyid = -1;
1625fdf0e00eSMathieu Poirier 
1626fdf0e00eSMathieu Poirier 	/* reset handled vdev */
1627fdf0e00eSMathieu Poirier 	rproc->nb_vdev = 0;
1628fdf0e00eSMathieu Poirier 
1629fdf0e00eSMathieu Poirier 	/*
1630fdf0e00eSMathieu Poirier 	 * Handle firmware resources required to attach to a remote processor.
1631fdf0e00eSMathieu Poirier 	 * Because we are attaching rather than booting the remote processor,
1632fdf0e00eSMathieu Poirier 	 * we expect the platform driver to properly set rproc->table_ptr.
1633fdf0e00eSMathieu Poirier 	 */
1634fdf0e00eSMathieu Poirier 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1635fdf0e00eSMathieu Poirier 	if (ret) {
1636fdf0e00eSMathieu Poirier 		dev_err(dev, "Failed to process resources: %d\n", ret);
16376e20a051SArnaud POULIQUEN 		goto unprepare_device;
1638fdf0e00eSMathieu Poirier 	}
1639fdf0e00eSMathieu Poirier 
1640fdf0e00eSMathieu Poirier 	/* Allocate carveout resources associated to rproc */
1641fdf0e00eSMathieu Poirier 	ret = rproc_alloc_registered_carveouts(rproc);
1642fdf0e00eSMathieu Poirier 	if (ret) {
1643fdf0e00eSMathieu Poirier 		dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1644fdf0e00eSMathieu Poirier 			ret);
1645fdf0e00eSMathieu Poirier 		goto clean_up_resources;
1646fdf0e00eSMathieu Poirier 	}
1647fdf0e00eSMathieu Poirier 
16486a6c4dc0SMathieu Poirier 	ret = __rproc_attach(rproc);
1649fdf0e00eSMathieu Poirier 	if (ret)
1650fdf0e00eSMathieu Poirier 		goto clean_up_resources;
1651fdf0e00eSMathieu Poirier 
1652fdf0e00eSMathieu Poirier 	return 0;
1653fdf0e00eSMathieu Poirier 
1654fdf0e00eSMathieu Poirier clean_up_resources:
1655fdf0e00eSMathieu Poirier 	rproc_resource_cleanup(rproc);
16566e20a051SArnaud POULIQUEN unprepare_device:
16576e20a051SArnaud POULIQUEN 	/* release HW resources if needed */
16586e20a051SArnaud POULIQUEN 	rproc_unprepare_device(rproc);
1659fdf0e00eSMathieu Poirier disable_iommu:
1660fdf0e00eSMathieu Poirier 	rproc_disable_iommu(rproc);
1661fdf0e00eSMathieu Poirier 	return ret;
1662fdf0e00eSMathieu Poirier }
1663fdf0e00eSMathieu Poirier 
1664fdf0e00eSMathieu Poirier /*
16655e6533f7SSarangdhar Joshi  * take a firmware and boot it up.
1666400e64dfSOhad Ben-Cohen  *
1667400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
1668400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
1669400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
1670400e64dfSOhad Ben-Cohen  * that might be cleaner).
1671400e64dfSOhad Ben-Cohen  */
rproc_auto_boot_callback(const struct firmware * fw,void * context)16725e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
1673400e64dfSOhad Ben-Cohen {
1674400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
1675a2b950acSOhad Ben-Cohen 
16762bfc311aSBjorn Andersson 	rproc_boot(rproc);
1677ddf71187SBjorn Andersson 
1678400e64dfSOhad Ben-Cohen 	release_firmware(fw);
1679400e64dfSOhad Ben-Cohen }
1680400e64dfSOhad Ben-Cohen 
rproc_trigger_auto_boot(struct rproc * rproc)16815e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc)
168270b85ef8SFernando Guzman Lugo {
168370b85ef8SFernando Guzman Lugo 	int ret;
168470b85ef8SFernando Guzman Lugo 
168570b85ef8SFernando Guzman Lugo 	/*
1686e3d21939SMathieu Poirier 	 * Since the remote processor is in a detached state, it has already
1687e3d21939SMathieu Poirier 	 * been booted by another entity.  As such there is no point in waiting
1688e3d21939SMathieu Poirier 	 * for a firmware image to be loaded, we can simply initiate the process
1689e3d21939SMathieu Poirier 	 * of attaching to it immediately.
1690e3d21939SMathieu Poirier 	 */
1691e3d21939SMathieu Poirier 	if (rproc->state == RPROC_DETACHED)
1692e3d21939SMathieu Poirier 		return rproc_boot(rproc);
1693e3d21939SMathieu Poirier 
1694e3d21939SMathieu Poirier 	/*
169570b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
169670b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
169770b85ef8SFernando Guzman Lugo 	 */
16980733d839SShawn Guo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_UEVENT,
169970b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
17005e6533f7SSarangdhar Joshi 				      rproc, rproc_auto_boot_callback);
17012099c77dSSarangdhar Joshi 	if (ret < 0)
170270b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
170370b85ef8SFernando Guzman Lugo 
170470b85ef8SFernando Guzman Lugo 	return ret;
170570b85ef8SFernando Guzman Lugo }
170670b85ef8SFernando Guzman Lugo 
rproc_stop(struct rproc * rproc,bool crashed)1707880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed)
17081efa30d0SSarangdhar Joshi {
17091efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
17101efa30d0SSarangdhar Joshi 	int ret;
17111efa30d0SSarangdhar Joshi 
1712d2008a96SMathieu Poirier 	/* No need to continue if a stop() operation has not been provided */
1713d2008a96SMathieu Poirier 	if (!rproc->ops->stop)
1714d2008a96SMathieu Poirier 		return -EINVAL;
1715d2008a96SMathieu Poirier 
1716618fcff3SBjorn Andersson 	/* Stop any subdevices for the remote processor */
1717618fcff3SBjorn Andersson 	rproc_stop_subdevices(rproc, crashed);
17181efa30d0SSarangdhar Joshi 
17190a8b81cbSBjorn Andersson 	/* the installed resource table is no longer accessible */
17208088dd4dSMathieu Poirier 	ret = rproc_reset_rsc_table_on_stop(rproc);
17218088dd4dSMathieu Poirier 	if (ret) {
17228088dd4dSMathieu Poirier 		dev_err(dev, "can't reset resource table: %d\n", ret);
17238088dd4dSMathieu Poirier 		return ret;
17248088dd4dSMathieu Poirier 	}
17258088dd4dSMathieu Poirier 
17260a8b81cbSBjorn Andersson 
17271efa30d0SSarangdhar Joshi 	/* power off the remote processor */
17281efa30d0SSarangdhar Joshi 	ret = rproc->ops->stop(rproc);
17291efa30d0SSarangdhar Joshi 	if (ret) {
17301efa30d0SSarangdhar Joshi 		dev_err(dev, "can't stop rproc: %d\n", ret);
17311efa30d0SSarangdhar Joshi 		return ret;
17321efa30d0SSarangdhar Joshi 	}
17331efa30d0SSarangdhar Joshi 
1734c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1735c455daa4SBjorn Andersson 
17361efa30d0SSarangdhar Joshi 	rproc->state = RPROC_OFFLINE;
17371efa30d0SSarangdhar Joshi 
17381efa30d0SSarangdhar Joshi 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
17391efa30d0SSarangdhar Joshi 
17401efa30d0SSarangdhar Joshi 	return 0;
17411efa30d0SSarangdhar Joshi }
17421efa30d0SSarangdhar Joshi 
17436070203fSMathieu Poirier /*
17446070203fSMathieu Poirier  * __rproc_detach(): Does the opposite of __rproc_attach()
17456070203fSMathieu Poirier  */
__rproc_detach(struct rproc * rproc)1746d3962a39SMathieu Poirier static int __rproc_detach(struct rproc *rproc)
17476070203fSMathieu Poirier {
17486070203fSMathieu Poirier 	struct device *dev = &rproc->dev;
17496070203fSMathieu Poirier 	int ret;
17506070203fSMathieu Poirier 
17516070203fSMathieu Poirier 	/* No need to continue if a detach() operation has not been provided */
17526070203fSMathieu Poirier 	if (!rproc->ops->detach)
17536070203fSMathieu Poirier 		return -EINVAL;
17546070203fSMathieu Poirier 
17556070203fSMathieu Poirier 	/* Stop any subdevices for the remote processor */
17566070203fSMathieu Poirier 	rproc_stop_subdevices(rproc, false);
17576070203fSMathieu Poirier 
17589dc9507fSMathieu Poirier 	/* the installed resource table is no longer accessible */
17599dc9507fSMathieu Poirier 	ret = rproc_reset_rsc_table_on_detach(rproc);
17609dc9507fSMathieu Poirier 	if (ret) {
17619dc9507fSMathieu Poirier 		dev_err(dev, "can't reset resource table: %d\n", ret);
17629dc9507fSMathieu Poirier 		return ret;
17639dc9507fSMathieu Poirier 	}
17649dc9507fSMathieu Poirier 
17656070203fSMathieu Poirier 	/* Tell the remote processor the core isn't available anymore */
17666070203fSMathieu Poirier 	ret = rproc->ops->detach(rproc);
17676070203fSMathieu Poirier 	if (ret) {
17686070203fSMathieu Poirier 		dev_err(dev, "can't detach from rproc: %d\n", ret);
17696070203fSMathieu Poirier 		return ret;
17706070203fSMathieu Poirier 	}
17716070203fSMathieu Poirier 
17726070203fSMathieu Poirier 	rproc_unprepare_subdevices(rproc);
17736070203fSMathieu Poirier 
17746070203fSMathieu Poirier 	rproc->state = RPROC_DETACHED;
17756070203fSMathieu Poirier 
17766070203fSMathieu Poirier 	dev_info(dev, "detached remote processor %s\n", rproc->name);
17776070203fSMathieu Poirier 
17786070203fSMathieu Poirier 	return 0;
17796070203fSMathieu Poirier }
17802666ca91SSarangdhar Joshi 
rproc_attach_recovery(struct rproc * rproc)1781ba194232SPeng Fan static int rproc_attach_recovery(struct rproc *rproc)
1782ba194232SPeng Fan {
1783ba194232SPeng Fan 	int ret;
1784ba194232SPeng Fan 
1785ba194232SPeng Fan 	ret = __rproc_detach(rproc);
1786ba194232SPeng Fan 	if (ret)
1787ba194232SPeng Fan 		return ret;
1788ba194232SPeng Fan 
1789ba194232SPeng Fan 	return __rproc_attach(rproc);
1790ba194232SPeng Fan }
1791ba194232SPeng Fan 
rproc_boot_recovery(struct rproc * rproc)1792ba194232SPeng Fan static int rproc_boot_recovery(struct rproc *rproc)
1793ba194232SPeng Fan {
1794ba194232SPeng Fan 	const struct firmware *firmware_p;
1795ba194232SPeng Fan 	struct device *dev = &rproc->dev;
1796ba194232SPeng Fan 	int ret;
1797ba194232SPeng Fan 
1798ba194232SPeng Fan 	ret = rproc_stop(rproc, true);
1799ba194232SPeng Fan 	if (ret)
1800ba194232SPeng Fan 		return ret;
1801ba194232SPeng Fan 
1802ba194232SPeng Fan 	/* generate coredump */
1803ba194232SPeng Fan 	rproc->ops->coredump(rproc);
1804ba194232SPeng Fan 
1805ba194232SPeng Fan 	/* load firmware */
1806ba194232SPeng Fan 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1807ba194232SPeng Fan 	if (ret < 0) {
1808ba194232SPeng Fan 		dev_err(dev, "request_firmware failed: %d\n", ret);
1809ba194232SPeng Fan 		return ret;
1810ba194232SPeng Fan 	}
1811ba194232SPeng Fan 
1812ba194232SPeng Fan 	/* boot the remote processor up again */
1813ba194232SPeng Fan 	ret = rproc_start(rproc, firmware_p);
1814ba194232SPeng Fan 
1815ba194232SPeng Fan 	release_firmware(firmware_p);
1816ba194232SPeng Fan 
1817ba194232SPeng Fan 	return ret;
1818ba194232SPeng Fan }
1819ba194232SPeng Fan 
18202666ca91SSarangdhar Joshi /**
182170b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
182270b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
182370b85ef8SFernando Guzman Lugo  *
182456324d7aSAnna, Suman  * The recovery is done by resetting all the virtio devices, that way all the
182570b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
182670b85ef8SFernando Guzman Lugo  * remoteproc functional again.
182770b85ef8SFernando Guzman Lugo  *
182870b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
1829f2867434SSuman Anna  *
1830f2867434SSuman Anna  * Return: 0 on success or a negative value upon failure
183170b85ef8SFernando Guzman Lugo  */
rproc_trigger_recovery(struct rproc * rproc)183270b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
183370b85ef8SFernando Guzman Lugo {
18347e83cab8SSarangdhar Joshi 	struct device *dev = &rproc->dev;
18357e83cab8SSarangdhar Joshi 	int ret;
18367e83cab8SSarangdhar Joshi 
18377e83cab8SSarangdhar Joshi 	ret = mutex_lock_interruptible(&rproc->lock);
18387e83cab8SSarangdhar Joshi 	if (ret)
18397e83cab8SSarangdhar Joshi 		return ret;
18407e83cab8SSarangdhar Joshi 
18410b145574SAlex Elder 	/* State could have changed before we got the mutex */
18420b145574SAlex Elder 	if (rproc->state != RPROC_CRASHED)
18430b145574SAlex Elder 		goto unlock_mutex;
18440b145574SAlex Elder 
18450b145574SAlex Elder 	dev_err(dev, "recovering %s\n", rproc->name);
18460b145574SAlex Elder 
1847ba194232SPeng Fan 	if (rproc_has_feature(rproc, RPROC_FEAT_ATTACH_ON_RECOVERY))
1848ba194232SPeng Fan 		ret = rproc_attach_recovery(rproc);
1849ba194232SPeng Fan 	else
1850ba194232SPeng Fan 		ret = rproc_boot_recovery(rproc);
18517e83cab8SSarangdhar Joshi 
18527e83cab8SSarangdhar Joshi unlock_mutex:
18537e83cab8SSarangdhar Joshi 	mutex_unlock(&rproc->lock);
18547e83cab8SSarangdhar Joshi 	return ret;
185570b85ef8SFernando Guzman Lugo }
185670b85ef8SFernando Guzman Lugo 
1857400e64dfSOhad Ben-Cohen /**
18588afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
18592e7d4c2cSArnaud Pouliquen  * @work: work treating the crash
18608afd519cSFernando Guzman Lugo  *
18618afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
18628afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
18638afd519cSFernando Guzman Lugo  */
rproc_crash_handler_work(struct work_struct * work)18648afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
18658afd519cSFernando Guzman Lugo {
18668afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
18678afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
18688afd519cSFernando Guzman Lugo 
18698afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
18708afd519cSFernando Guzman Lugo 
18718afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
18728afd519cSFernando Guzman Lugo 
187311c7f9e3SMaria Yu 	if (rproc->state == RPROC_CRASHED) {
18748afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
18758afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
18768afd519cSFernando Guzman Lugo 		return;
18778afd519cSFernando Guzman Lugo 	}
18788afd519cSFernando Guzman Lugo 
187911c7f9e3SMaria Yu 	if (rproc->state == RPROC_OFFLINE) {
188011c7f9e3SMaria Yu 		/* Don't recover if the remote processor was stopped */
188111c7f9e3SMaria Yu 		mutex_unlock(&rproc->lock);
188211c7f9e3SMaria Yu 		goto out;
188311c7f9e3SMaria Yu 	}
188411c7f9e3SMaria Yu 
18858afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
18868afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
18878afd519cSFernando Guzman Lugo 		rproc->name);
18888afd519cSFernando Guzman Lugo 
18898afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
18908afd519cSFernando Guzman Lugo 
18912e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
189270b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
1893a781e5aaSRishabh Bhatnagar 
189411c7f9e3SMaria Yu out:
1895a781e5aaSRishabh Bhatnagar 	pm_relax(rproc->dev.parent);
18968afd519cSFernando Guzman Lugo }
18978afd519cSFernando Guzman Lugo 
18988afd519cSFernando Guzman Lugo /**
18991b0ef906SSuman Anna  * rproc_boot() - boot a remote processor
1900400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1901400e64dfSOhad Ben-Cohen  *
1902400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1903400e64dfSOhad Ben-Cohen  *
1904400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1905400e64dfSOhad Ben-Cohen  * returns (successfully).
1906400e64dfSOhad Ben-Cohen  *
1907f2867434SSuman Anna  * Return: 0 on success, and an appropriate error value otherwise
1908400e64dfSOhad Ben-Cohen  */
rproc_boot(struct rproc * rproc)19091b0ef906SSuman Anna int rproc_boot(struct rproc *rproc)
1910400e64dfSOhad Ben-Cohen {
1911400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1912400e64dfSOhad Ben-Cohen 	struct device *dev;
1913400e64dfSOhad Ben-Cohen 	int ret;
1914400e64dfSOhad Ben-Cohen 
1915400e64dfSOhad Ben-Cohen 	if (!rproc) {
1916400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1917400e64dfSOhad Ben-Cohen 		return -EINVAL;
1918400e64dfSOhad Ben-Cohen 	}
1919400e64dfSOhad Ben-Cohen 
1920b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1921400e64dfSOhad Ben-Cohen 
1922400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1923400e64dfSOhad Ben-Cohen 	if (ret) {
1924400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1925400e64dfSOhad Ben-Cohen 		return ret;
1926400e64dfSOhad Ben-Cohen 	}
1927400e64dfSOhad Ben-Cohen 
19282099c77dSSarangdhar Joshi 	if (rproc->state == RPROC_DELETED) {
19292099c77dSSarangdhar Joshi 		ret = -ENODEV;
19302099c77dSSarangdhar Joshi 		dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
19312099c77dSSarangdhar Joshi 		goto unlock_mutex;
19322099c77dSSarangdhar Joshi 	}
19332099c77dSSarangdhar Joshi 
19340f9dc562SMathieu Poirier 	/* skip the boot or attach process if rproc is already powered up */
1935400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1936400e64dfSOhad Ben-Cohen 		ret = 0;
1937400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1938400e64dfSOhad Ben-Cohen 	}
1939400e64dfSOhad Ben-Cohen 
19400f9dc562SMathieu Poirier 	if (rproc->state == RPROC_DETACHED) {
19410f9dc562SMathieu Poirier 		dev_info(dev, "attaching to %s\n", rproc->name);
19420f9dc562SMathieu Poirier 
19436a6c4dc0SMathieu Poirier 		ret = rproc_attach(rproc);
19440f9dc562SMathieu Poirier 	} else {
1945400e64dfSOhad Ben-Cohen 		dev_info(dev, "powering up %s\n", rproc->name);
1946400e64dfSOhad Ben-Cohen 
1947400e64dfSOhad Ben-Cohen 		/* load firmware */
1948400e64dfSOhad Ben-Cohen 		ret = request_firmware(&firmware_p, rproc->firmware, dev);
1949400e64dfSOhad Ben-Cohen 		if (ret < 0) {
1950400e64dfSOhad Ben-Cohen 			dev_err(dev, "request_firmware failed: %d\n", ret);
1951400e64dfSOhad Ben-Cohen 			goto downref_rproc;
1952400e64dfSOhad Ben-Cohen 		}
1953400e64dfSOhad Ben-Cohen 
1954400e64dfSOhad Ben-Cohen 		ret = rproc_fw_boot(rproc, firmware_p);
1955400e64dfSOhad Ben-Cohen 
1956400e64dfSOhad Ben-Cohen 		release_firmware(firmware_p);
19570f9dc562SMathieu Poirier 	}
1958400e64dfSOhad Ben-Cohen 
1959400e64dfSOhad Ben-Cohen downref_rproc:
1960fbb6aacbSBjorn Andersson 	if (ret)
1961400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1962400e64dfSOhad Ben-Cohen unlock_mutex:
1963400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1964400e64dfSOhad Ben-Cohen 	return ret;
1965400e64dfSOhad Ben-Cohen }
1966400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1967400e64dfSOhad Ben-Cohen 
1968400e64dfSOhad Ben-Cohen /**
1969400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1970400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1971400e64dfSOhad Ben-Cohen  *
1972400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1973400e64dfSOhad Ben-Cohen  *
1974400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1975400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1976400e64dfSOhad Ben-Cohen  * without really powering off the device.
1977400e64dfSOhad Ben-Cohen  *
1978400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1979400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1980400e64dfSOhad Ben-Cohen  *
1981400e64dfSOhad Ben-Cohen  * Notes:
1982400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1983400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1984400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1985400e64dfSOhad Ben-Cohen  *   needed.
1986c13b780cSSuman Anna  *
1987c13b780cSSuman Anna  * Return: 0 on success, and an appropriate error value otherwise
1988400e64dfSOhad Ben-Cohen  */
rproc_shutdown(struct rproc * rproc)1989c13b780cSSuman Anna int rproc_shutdown(struct rproc *rproc)
1990400e64dfSOhad Ben-Cohen {
1991b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1992c13b780cSSuman Anna 	int ret = 0;
1993400e64dfSOhad Ben-Cohen 
1994400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1995400e64dfSOhad Ben-Cohen 	if (ret) {
1996400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1997c13b780cSSuman Anna 		return ret;
1998400e64dfSOhad Ben-Cohen 	}
1999400e64dfSOhad Ben-Cohen 
20005e6a0e05SShengjiu Wang 	if (rproc->state != RPROC_RUNNING &&
20015e6a0e05SShengjiu Wang 	    rproc->state != RPROC_ATTACHED) {
20025e6a0e05SShengjiu Wang 		ret = -EINVAL;
20035e6a0e05SShengjiu Wang 		goto out;
20045e6a0e05SShengjiu Wang 	}
20055e6a0e05SShengjiu Wang 
2006400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
2007400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
2008400e64dfSOhad Ben-Cohen 		goto out;
2009400e64dfSOhad Ben-Cohen 
2010fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, false);
2011400e64dfSOhad Ben-Cohen 	if (ret) {
2012400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
2013400e64dfSOhad Ben-Cohen 		goto out;
2014400e64dfSOhad Ben-Cohen 	}
2015400e64dfSOhad Ben-Cohen 
2016400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
2017400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
2018400e64dfSOhad Ben-Cohen 
201933467ac3SLoic Pallardy 	/* release HW resources if needed */
202033467ac3SLoic Pallardy 	rproc_unprepare_device(rproc);
202133467ac3SLoic Pallardy 
2022400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
2023400e64dfSOhad Ben-Cohen 
2024988d204cSBjorn Andersson 	/* Free the copy of the resource table */
2025a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
2026a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
2027988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
2028400e64dfSOhad Ben-Cohen out:
2029400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
2030c13b780cSSuman Anna 	return ret;
2031400e64dfSOhad Ben-Cohen }
2032400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
2033400e64dfSOhad Ben-Cohen 
2034400e64dfSOhad Ben-Cohen /**
2035d3962a39SMathieu Poirier  * rproc_detach() - Detach the remote processor from the
2036d3962a39SMathieu Poirier  * remoteproc core
2037d3962a39SMathieu Poirier  *
2038d3962a39SMathieu Poirier  * @rproc: the remote processor
2039d3962a39SMathieu Poirier  *
2040d3962a39SMathieu Poirier  * Detach a remote processor (previously attached to with rproc_attach()).
2041d3962a39SMathieu Poirier  *
2042d3962a39SMathieu Poirier  * In case @rproc is still being used by an additional user(s), then
2043d3962a39SMathieu Poirier  * this function will just decrement the power refcount and exit,
2044d3962a39SMathieu Poirier  * without disconnecting the device.
2045d3962a39SMathieu Poirier  *
2046d3962a39SMathieu Poirier  * Function rproc_detach() calls __rproc_detach() in order to let a remote
2047d3962a39SMathieu Poirier  * processor know that services provided by the application processor are
2048d3962a39SMathieu Poirier  * no longer available.  From there it should be possible to remove the
2049d3962a39SMathieu Poirier  * platform driver and even power cycle the application processor (if the HW
2050d3962a39SMathieu Poirier  * supports it) without needing to switch off the remote processor.
2051f2867434SSuman Anna  *
2052f2867434SSuman Anna  * Return: 0 on success, and an appropriate error value otherwise
2053d3962a39SMathieu Poirier  */
rproc_detach(struct rproc * rproc)2054d3962a39SMathieu Poirier int rproc_detach(struct rproc *rproc)
2055d3962a39SMathieu Poirier {
2056d3962a39SMathieu Poirier 	struct device *dev = &rproc->dev;
2057d3962a39SMathieu Poirier 	int ret;
2058d3962a39SMathieu Poirier 
2059d3962a39SMathieu Poirier 	ret = mutex_lock_interruptible(&rproc->lock);
2060d3962a39SMathieu Poirier 	if (ret) {
2061d3962a39SMathieu Poirier 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
2062d3962a39SMathieu Poirier 		return ret;
2063d3962a39SMathieu Poirier 	}
2064d3962a39SMathieu Poirier 
20655e6a0e05SShengjiu Wang 	if (rproc->state != RPROC_ATTACHED) {
20665e6a0e05SShengjiu Wang 		ret = -EINVAL;
20675e6a0e05SShengjiu Wang 		goto out;
20685e6a0e05SShengjiu Wang 	}
20695e6a0e05SShengjiu Wang 
2070d3962a39SMathieu Poirier 	/* if the remote proc is still needed, bail out */
2071d3962a39SMathieu Poirier 	if (!atomic_dec_and_test(&rproc->power)) {
2072d3962a39SMathieu Poirier 		ret = 0;
2073d3962a39SMathieu Poirier 		goto out;
2074d3962a39SMathieu Poirier 	}
2075d3962a39SMathieu Poirier 
2076d3962a39SMathieu Poirier 	ret = __rproc_detach(rproc);
2077d3962a39SMathieu Poirier 	if (ret) {
2078d3962a39SMathieu Poirier 		atomic_inc(&rproc->power);
2079d3962a39SMathieu Poirier 		goto out;
2080d3962a39SMathieu Poirier 	}
2081d3962a39SMathieu Poirier 
2082d3962a39SMathieu Poirier 	/* clean up all acquired resources */
2083d3962a39SMathieu Poirier 	rproc_resource_cleanup(rproc);
2084d3962a39SMathieu Poirier 
2085d3962a39SMathieu Poirier 	/* release HW resources if needed */
2086d3962a39SMathieu Poirier 	rproc_unprepare_device(rproc);
2087d3962a39SMathieu Poirier 
2088d3962a39SMathieu Poirier 	rproc_disable_iommu(rproc);
2089d3962a39SMathieu Poirier 
20909dc9507fSMathieu Poirier 	/* Free the copy of the resource table */
20919dc9507fSMathieu Poirier 	kfree(rproc->cached_table);
20929dc9507fSMathieu Poirier 	rproc->cached_table = NULL;
2093d3962a39SMathieu Poirier 	rproc->table_ptr = NULL;
2094d3962a39SMathieu Poirier out:
2095d3962a39SMathieu Poirier 	mutex_unlock(&rproc->lock);
2096d3962a39SMathieu Poirier 	return ret;
2097d3962a39SMathieu Poirier }
2098d3962a39SMathieu Poirier EXPORT_SYMBOL(rproc_detach);
2099d3962a39SMathieu Poirier 
2100d3962a39SMathieu Poirier /**
2101fec47d86SDave Gerlach  * rproc_get_by_phandle() - find a remote processor by phandle
2102fec47d86SDave Gerlach  * @phandle: phandle to the rproc
2103fec47d86SDave Gerlach  *
2104fec47d86SDave Gerlach  * Finds an rproc handle using the remote processor's phandle, and then
2105fec47d86SDave Gerlach  * return a handle to the rproc.
2106fec47d86SDave Gerlach  *
2107fec47d86SDave Gerlach  * This function increments the remote processor's refcount, so always
2108fec47d86SDave Gerlach  * use rproc_put() to decrement it back once rproc isn't needed anymore.
2109fec47d86SDave Gerlach  *
2110f2867434SSuman Anna  * Return: rproc handle on success, and NULL on failure
2111fec47d86SDave Gerlach  */
21128de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF
rproc_get_by_phandle(phandle phandle)2113fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle)
2114fec47d86SDave Gerlach {
2115fec47d86SDave Gerlach 	struct rproc *rproc = NULL, *r;
2116*8b46dc5cSMathieu Poirier 	struct device_driver *driver;
2117fec47d86SDave Gerlach 	struct device_node *np;
2118fec47d86SDave Gerlach 
2119fec47d86SDave Gerlach 	np = of_find_node_by_phandle(phandle);
2120fec47d86SDave Gerlach 	if (!np)
2121fec47d86SDave Gerlach 		return NULL;
2122fec47d86SDave Gerlach 
2123c0abe2caSBjorn Andersson 	rcu_read_lock();
2124c0abe2caSBjorn Andersson 	list_for_each_entry_rcu(r, &rproc_list, node) {
2125e63ae3f8Sye xingchen 		if (r->dev.parent && device_match_of_node(r->dev.parent, np)) {
2126fbb6aacbSBjorn Andersson 			/* prevent underlying implementation from being removed */
2127*8b46dc5cSMathieu Poirier 
2128*8b46dc5cSMathieu Poirier 			/*
2129*8b46dc5cSMathieu Poirier 			 * If the remoteproc's parent has a driver, the
2130*8b46dc5cSMathieu Poirier 			 * remoteproc is not part of a cluster and we can use
2131*8b46dc5cSMathieu Poirier 			 * that driver.
2132*8b46dc5cSMathieu Poirier 			 */
2133*8b46dc5cSMathieu Poirier 			driver = r->dev.parent->driver;
2134*8b46dc5cSMathieu Poirier 
2135*8b46dc5cSMathieu Poirier 			/*
2136*8b46dc5cSMathieu Poirier 			 * If the remoteproc's parent does not have a driver,
2137*8b46dc5cSMathieu Poirier 			 * look for the driver associated with the cluster.
2138*8b46dc5cSMathieu Poirier 			 */
2139*8b46dc5cSMathieu Poirier 			if (!driver) {
2140*8b46dc5cSMathieu Poirier 				if (r->dev.parent->parent)
2141*8b46dc5cSMathieu Poirier 					driver = r->dev.parent->parent->driver;
2142*8b46dc5cSMathieu Poirier 				if (!driver)
2143*8b46dc5cSMathieu Poirier 					break;
2144*8b46dc5cSMathieu Poirier 			}
2145*8b46dc5cSMathieu Poirier 
2146*8b46dc5cSMathieu Poirier 			if (!try_module_get(driver->owner)) {
2147fbb6aacbSBjorn Andersson 				dev_err(&r->dev, "can't get owner\n");
2148fbb6aacbSBjorn Andersson 				break;
2149fbb6aacbSBjorn Andersson 			}
2150fbb6aacbSBjorn Andersson 
2151fec47d86SDave Gerlach 			rproc = r;
2152fec47d86SDave Gerlach 			get_device(&rproc->dev);
2153fec47d86SDave Gerlach 			break;
2154fec47d86SDave Gerlach 		}
2155fec47d86SDave Gerlach 	}
2156c0abe2caSBjorn Andersson 	rcu_read_unlock();
2157fec47d86SDave Gerlach 
2158fec47d86SDave Gerlach 	of_node_put(np);
2159fec47d86SDave Gerlach 
2160fec47d86SDave Gerlach 	return rproc;
2161fec47d86SDave Gerlach }
21628de3dbd0SOhad Ben-Cohen #else
rproc_get_by_phandle(phandle phandle)21638de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle)
21648de3dbd0SOhad Ben-Cohen {
21658de3dbd0SOhad Ben-Cohen 	return NULL;
21668de3dbd0SOhad Ben-Cohen }
21678de3dbd0SOhad Ben-Cohen #endif
2168fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle);
2169fec47d86SDave Gerlach 
21704c1ad562SSuman Anna /**
21714c1ad562SSuman Anna  * rproc_set_firmware() - assign a new firmware
21724c1ad562SSuman Anna  * @rproc: rproc handle to which the new firmware is being assigned
21734c1ad562SSuman Anna  * @fw_name: new firmware name to be assigned
21744c1ad562SSuman Anna  *
21754c1ad562SSuman Anna  * This function allows remoteproc drivers or clients to configure a custom
21764c1ad562SSuman Anna  * firmware name that is different from the default name used during remoteproc
21774c1ad562SSuman Anna  * registration. The function does not trigger a remote processor boot,
21784c1ad562SSuman Anna  * only sets the firmware name used for a subsequent boot. This function
21794c1ad562SSuman Anna  * should also be called only when the remote processor is offline.
21804c1ad562SSuman Anna  *
21814c1ad562SSuman Anna  * This allows either the userspace to configure a different name through
21824c1ad562SSuman Anna  * sysfs or a kernel-level remoteproc or a remoteproc client driver to set
21834c1ad562SSuman Anna  * a specific firmware when it is controlling the boot and shutdown of the
21844c1ad562SSuman Anna  * remote processor.
21854c1ad562SSuman Anna  *
21864c1ad562SSuman Anna  * Return: 0 on success or a negative value upon failure
21874c1ad562SSuman Anna  */
rproc_set_firmware(struct rproc * rproc,const char * fw_name)21884c1ad562SSuman Anna int rproc_set_firmware(struct rproc *rproc, const char *fw_name)
21894c1ad562SSuman Anna {
21904c1ad562SSuman Anna 	struct device *dev;
21914c1ad562SSuman Anna 	int ret, len;
21924c1ad562SSuman Anna 	char *p;
21934c1ad562SSuman Anna 
21944c1ad562SSuman Anna 	if (!rproc || !fw_name)
21954c1ad562SSuman Anna 		return -EINVAL;
21964c1ad562SSuman Anna 
21974c1ad562SSuman Anna 	dev = rproc->dev.parent;
21984c1ad562SSuman Anna 
21994c1ad562SSuman Anna 	ret = mutex_lock_interruptible(&rproc->lock);
22004c1ad562SSuman Anna 	if (ret) {
22014c1ad562SSuman Anna 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
22024c1ad562SSuman Anna 		return -EINVAL;
22034c1ad562SSuman Anna 	}
22044c1ad562SSuman Anna 
22054c1ad562SSuman Anna 	if (rproc->state != RPROC_OFFLINE) {
22064c1ad562SSuman Anna 		dev_err(dev, "can't change firmware while running\n");
22074c1ad562SSuman Anna 		ret = -EBUSY;
22084c1ad562SSuman Anna 		goto out;
22094c1ad562SSuman Anna 	}
22104c1ad562SSuman Anna 
22114c1ad562SSuman Anna 	len = strcspn(fw_name, "\n");
22124c1ad562SSuman Anna 	if (!len) {
22134c1ad562SSuman Anna 		dev_err(dev, "can't provide empty string for firmware name\n");
22144c1ad562SSuman Anna 		ret = -EINVAL;
22154c1ad562SSuman Anna 		goto out;
22164c1ad562SSuman Anna 	}
22174c1ad562SSuman Anna 
22184c1ad562SSuman Anna 	p = kstrndup(fw_name, len, GFP_KERNEL);
22194c1ad562SSuman Anna 	if (!p) {
22204c1ad562SSuman Anna 		ret = -ENOMEM;
22214c1ad562SSuman Anna 		goto out;
22224c1ad562SSuman Anna 	}
22234c1ad562SSuman Anna 
222443d3f2c7SDaniele Alessandrelli 	kfree_const(rproc->firmware);
22254c1ad562SSuman Anna 	rproc->firmware = p;
22264c1ad562SSuman Anna 
22274c1ad562SSuman Anna out:
22284c1ad562SSuman Anna 	mutex_unlock(&rproc->lock);
22294c1ad562SSuman Anna 	return ret;
22304c1ad562SSuman Anna }
22314c1ad562SSuman Anna EXPORT_SYMBOL(rproc_set_firmware);
22324c1ad562SSuman Anna 
rproc_validate(struct rproc * rproc)223388d3a136SMathieu Poirier static int rproc_validate(struct rproc *rproc)
223488d3a136SMathieu Poirier {
223588d3a136SMathieu Poirier 	switch (rproc->state) {
223688d3a136SMathieu Poirier 	case RPROC_OFFLINE:
223788d3a136SMathieu Poirier 		/*
223888d3a136SMathieu Poirier 		 * An offline processor without a start()
223988d3a136SMathieu Poirier 		 * function makes no sense.
224088d3a136SMathieu Poirier 		 */
224188d3a136SMathieu Poirier 		if (!rproc->ops->start)
224288d3a136SMathieu Poirier 			return -EINVAL;
224388d3a136SMathieu Poirier 		break;
224488d3a136SMathieu Poirier 	case RPROC_DETACHED:
224588d3a136SMathieu Poirier 		/*
224688d3a136SMathieu Poirier 		 * A remote processor in a detached state without an
224788d3a136SMathieu Poirier 		 * attach() function makes not sense.
224888d3a136SMathieu Poirier 		 */
224988d3a136SMathieu Poirier 		if (!rproc->ops->attach)
225088d3a136SMathieu Poirier 			return -EINVAL;
225188d3a136SMathieu Poirier 		/*
225288d3a136SMathieu Poirier 		 * When attaching to a remote processor the device memory
225388d3a136SMathieu Poirier 		 * is already available and as such there is no need to have a
225488d3a136SMathieu Poirier 		 * cached table.
225588d3a136SMathieu Poirier 		 */
225688d3a136SMathieu Poirier 		if (rproc->cached_table)
225788d3a136SMathieu Poirier 			return -EINVAL;
225888d3a136SMathieu Poirier 		break;
225988d3a136SMathieu Poirier 	default:
226088d3a136SMathieu Poirier 		/*
226188d3a136SMathieu Poirier 		 * When adding a remote processor, the state of the device
226288d3a136SMathieu Poirier 		 * can be offline or detached, nothing else.
226388d3a136SMathieu Poirier 		 */
226488d3a136SMathieu Poirier 		return -EINVAL;
226588d3a136SMathieu Poirier 	}
226688d3a136SMathieu Poirier 
226788d3a136SMathieu Poirier 	return 0;
226888d3a136SMathieu Poirier }
226988d3a136SMathieu Poirier 
2270fec47d86SDave Gerlach /**
2271160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
2272400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
2273400e64dfSOhad Ben-Cohen  *
2274400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
2275400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
2276400e64dfSOhad Ben-Cohen  *
2277400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
2278400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
2279400e64dfSOhad Ben-Cohen  *
2280400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
2281400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
2282400e64dfSOhad Ben-Cohen  * firmware.
2283400e64dfSOhad Ben-Cohen  *
2284400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
22857a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
2286400e64dfSOhad Ben-Cohen  * probed.
2287f2867434SSuman Anna  *
2288f2867434SSuman Anna  * Return: 0 on success and an appropriate error code otherwise
2289400e64dfSOhad Ben-Cohen  */
rproc_add(struct rproc * rproc)2290160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
2291400e64dfSOhad Ben-Cohen {
2292b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
229370b85ef8SFernando Guzman Lugo 	int ret;
2294400e64dfSOhad Ben-Cohen 
229588d3a136SMathieu Poirier 	ret = rproc_validate(rproc);
229688d3a136SMathieu Poirier 	if (ret < 0)
229788d3a136SMathieu Poirier 		return ret;
229888d3a136SMathieu Poirier 
229962b8f9e9SSiddharth Gupta 	/* add char device for this remoteproc */
230062b8f9e9SSiddharth Gupta 	ret = rproc_char_device_add(rproc);
230162b8f9e9SSiddharth Gupta 	if (ret < 0)
230262b8f9e9SSiddharth Gupta 		return ret;
230362b8f9e9SSiddharth Gupta 
2304305ac5a7SPaul Cercueil 	ret = device_add(dev);
23057dbdb8bdSSiddharth Gupta 	if (ret < 0) {
23067dbdb8bdSSiddharth Gupta 		put_device(dev);
23077dbdb8bdSSiddharth Gupta 		goto rproc_remove_cdev;
23087dbdb8bdSSiddharth Gupta 	}
2309305ac5a7SPaul Cercueil 
2310305ac5a7SPaul Cercueil 	dev_info(dev, "%s is available\n", rproc->name);
2311305ac5a7SPaul Cercueil 
2312305ac5a7SPaul Cercueil 	/* create debugfs entries */
2313305ac5a7SPaul Cercueil 	rproc_create_debug_dir(rproc);
2314305ac5a7SPaul Cercueil 
23157a20c64dSSarangdhar Joshi 	/* if rproc is marked always-on, request it to boot */
23167a20c64dSSarangdhar Joshi 	if (rproc->auto_boot) {
23175e6533f7SSarangdhar Joshi 		ret = rproc_trigger_auto_boot(rproc);
2318d2e12e66SDave Gerlach 		if (ret < 0)
23197dbdb8bdSSiddharth Gupta 			goto rproc_remove_dev;
23207a20c64dSSarangdhar Joshi 	}
2321400e64dfSOhad Ben-Cohen 
2322d2e12e66SDave Gerlach 	/* expose to rproc_get_by_phandle users */
2323d2e12e66SDave Gerlach 	mutex_lock(&rproc_list_mutex);
2324c0abe2caSBjorn Andersson 	list_add_rcu(&rproc->node, &rproc_list);
2325d2e12e66SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
2326d2e12e66SDave Gerlach 
2327d2e12e66SDave Gerlach 	return 0;
23287dbdb8bdSSiddharth Gupta 
23297dbdb8bdSSiddharth Gupta rproc_remove_dev:
23307dbdb8bdSSiddharth Gupta 	rproc_delete_debug_dir(rproc);
23317dbdb8bdSSiddharth Gupta 	device_del(dev);
23327dbdb8bdSSiddharth Gupta rproc_remove_cdev:
23337dbdb8bdSSiddharth Gupta 	rproc_char_device_remove(rproc);
23347dbdb8bdSSiddharth Gupta 	return ret;
2335400e64dfSOhad Ben-Cohen }
2336160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
2337400e64dfSOhad Ben-Cohen 
devm_rproc_remove(void * rproc)2338305ac5a7SPaul Cercueil static void devm_rproc_remove(void *rproc)
2339305ac5a7SPaul Cercueil {
2340305ac5a7SPaul Cercueil 	rproc_del(rproc);
2341305ac5a7SPaul Cercueil }
2342305ac5a7SPaul Cercueil 
2343305ac5a7SPaul Cercueil /**
2344305ac5a7SPaul Cercueil  * devm_rproc_add() - resource managed rproc_add()
2345305ac5a7SPaul Cercueil  * @dev: the underlying device
2346305ac5a7SPaul Cercueil  * @rproc: the remote processor handle to register
2347305ac5a7SPaul Cercueil  *
2348305ac5a7SPaul Cercueil  * This function performs like rproc_add() but the registered rproc device will
2349305ac5a7SPaul Cercueil  * automatically be removed on driver detach.
2350305ac5a7SPaul Cercueil  *
2351f2867434SSuman Anna  * Return: 0 on success, negative errno on failure
2352305ac5a7SPaul Cercueil  */
devm_rproc_add(struct device * dev,struct rproc * rproc)2353305ac5a7SPaul Cercueil int devm_rproc_add(struct device *dev, struct rproc *rproc)
2354305ac5a7SPaul Cercueil {
2355305ac5a7SPaul Cercueil 	int err;
2356305ac5a7SPaul Cercueil 
2357305ac5a7SPaul Cercueil 	err = rproc_add(rproc);
2358305ac5a7SPaul Cercueil 	if (err)
2359305ac5a7SPaul Cercueil 		return err;
2360305ac5a7SPaul Cercueil 
2361305ac5a7SPaul Cercueil 	return devm_add_action_or_reset(dev, devm_rproc_remove, rproc);
2362305ac5a7SPaul Cercueil }
2363305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_add);
2364305ac5a7SPaul Cercueil 
2365400e64dfSOhad Ben-Cohen /**
2366b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
2367b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
2368b5ab5e24SOhad Ben-Cohen  *
2369b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
2370b5ab5e24SOhad Ben-Cohen  *
2371b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
2372b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
2373b5ab5e24SOhad Ben-Cohen  */
rproc_type_release(struct device * dev)2374b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
2375b5ab5e24SOhad Ben-Cohen {
2376b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
2377b5ab5e24SOhad Ben-Cohen 
23787183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
23797183a2a7SOhad Ben-Cohen 
2380b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
2381b5ab5e24SOhad Ben-Cohen 
2382b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
238308333b91Skeliu 		ida_free(&rproc_dev_index, rproc->index);
2384b5ab5e24SOhad Ben-Cohen 
23851487dedaSMathieu Poirier 	kfree_const(rproc->firmware);
2386db655278SSuman Anna 	kfree_const(rproc->name);
2387fb98e2bdSBjorn Andersson 	kfree(rproc->ops);
2388b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
2389b5ab5e24SOhad Ben-Cohen }
2390b5ab5e24SOhad Ben-Cohen 
2391c42ca04dSBhumika Goyal static const struct device_type rproc_type = {
2392b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
2393b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
2394b5ab5e24SOhad Ben-Cohen };
2395400e64dfSOhad Ben-Cohen 
rproc_alloc_firmware(struct rproc * rproc,const char * name,const char * firmware)23960c2ae2b1SMathieu Poirier static int rproc_alloc_firmware(struct rproc *rproc,
23970c2ae2b1SMathieu Poirier 				const char *name, const char *firmware)
23980c2ae2b1SMathieu Poirier {
23991487dedaSMathieu Poirier 	const char *p;
24000c2ae2b1SMathieu Poirier 
24010c2ae2b1SMathieu Poirier 	/*
24029d5f82c8SMathieu Poirier 	 * Allocate a firmware name if the caller gave us one to work
24039d5f82c8SMathieu Poirier 	 * with.  Otherwise construct a new one using a default pattern.
24040c2ae2b1SMathieu Poirier 	 */
24059d5f82c8SMathieu Poirier 	if (firmware)
24061487dedaSMathieu Poirier 		p = kstrdup_const(firmware, GFP_KERNEL);
24079d5f82c8SMathieu Poirier 	else
24089d5f82c8SMathieu Poirier 		p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name);
24094df4f8beSMathieu Poirier 
24100c2ae2b1SMathieu Poirier 	if (!p)
24110c2ae2b1SMathieu Poirier 		return -ENOMEM;
24120c2ae2b1SMathieu Poirier 
24130c2ae2b1SMathieu Poirier 	rproc->firmware = p;
24140c2ae2b1SMathieu Poirier 
24150c2ae2b1SMathieu Poirier 	return 0;
24160c2ae2b1SMathieu Poirier }
24170c2ae2b1SMathieu Poirier 
rproc_alloc_ops(struct rproc * rproc,const struct rproc_ops * ops)2418bf860aa1SMathieu Poirier static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops)
2419bf860aa1SMathieu Poirier {
2420bf860aa1SMathieu Poirier 	rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
2421bf860aa1SMathieu Poirier 	if (!rproc->ops)
2422bf860aa1SMathieu Poirier 		return -ENOMEM;
2423bf860aa1SMathieu Poirier 
2424adf60a87SSiddharth Gupta 	/* Default to rproc_coredump if no coredump function is specified */
2425adf60a87SSiddharth Gupta 	if (!rproc->ops->coredump)
2426adf60a87SSiddharth Gupta 		rproc->ops->coredump = rproc_coredump;
2427adf60a87SSiddharth Gupta 
2428bf860aa1SMathieu Poirier 	if (rproc->ops->load)
2429bf860aa1SMathieu Poirier 		return 0;
2430bf860aa1SMathieu Poirier 
2431bf860aa1SMathieu Poirier 	/* Default to ELF loader if no load function is specified */
2432bf860aa1SMathieu Poirier 	rproc->ops->load = rproc_elf_load_segments;
2433bf860aa1SMathieu Poirier 	rproc->ops->parse_fw = rproc_elf_load_rsc_table;
2434bf860aa1SMathieu Poirier 	rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
2435e29ff72bSClement Leger 	rproc->ops->sanity_check = rproc_elf_sanity_check;
2436bf860aa1SMathieu Poirier 	rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
2437bf860aa1SMathieu Poirier 
2438bf860aa1SMathieu Poirier 	return 0;
2439bf860aa1SMathieu Poirier }
2440bf860aa1SMathieu Poirier 
2441400e64dfSOhad Ben-Cohen /**
2442400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
2443400e64dfSOhad Ben-Cohen  * @dev: the underlying device
2444400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
2445400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
24468b4aec9aSRobert Tivy  * @firmware: name of firmware file to load, can be NULL
2447400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
2448400e64dfSOhad Ben-Cohen  *
2449400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
24508b4aec9aSRobert Tivy  * it yet. if @firmware is NULL, a default name is used.
2451400e64dfSOhad Ben-Cohen  *
2452400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
2453400e64dfSOhad Ben-Cohen  * of the remote processor.
2454400e64dfSOhad Ben-Cohen  *
2455400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
2456160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
2457400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
2458400e64dfSOhad Ben-Cohen  *
2459400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
2460433c0e04SBjorn Andersson  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
2461f2867434SSuman Anna  *
2462f2867434SSuman Anna  * Return: new rproc pointer on success, and NULL on failure
2463400e64dfSOhad Ben-Cohen  */
rproc_alloc(struct device * dev,const char * name,const struct rproc_ops * ops,const char * firmware,int len)2464400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
2465400e64dfSOhad Ben-Cohen 			  const struct rproc_ops *ops,
2466400e64dfSOhad Ben-Cohen 			  const char *firmware, int len)
2467400e64dfSOhad Ben-Cohen {
2468400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
2469400e64dfSOhad Ben-Cohen 
2470400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
2471400e64dfSOhad Ben-Cohen 		return NULL;
2472400e64dfSOhad Ben-Cohen 
24730f57dc6aSMatt Redfearn 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
24740c2ae2b1SMathieu Poirier 	if (!rproc)
24750f57dc6aSMatt Redfearn 		return NULL;
24760c2ae2b1SMathieu Poirier 
2477400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
2478ddf71187SBjorn Andersson 	rproc->auto_boot = true;
2479418fd787SClement Leger 	rproc->elf_class = ELFCLASSNONE;
2480418fd787SClement Leger 	rproc->elf_machine = EM_NONE;
2481400e64dfSOhad Ben-Cohen 
2482b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
2483b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
2484b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
24852aefbef0SMatt Redfearn 	rproc->dev.class = &rproc_class;
24867c89717fSBjorn Andersson 	rproc->dev.driver_data = rproc;
24876442df49SAlex Elder 	idr_init(&rproc->notifyids);
2488b5ab5e24SOhad Ben-Cohen 
2489db655278SSuman Anna 	rproc->name = kstrdup_const(name, GFP_KERNEL);
2490db655278SSuman Anna 	if (!rproc->name)
2491db655278SSuman Anna 		goto put_device;
2492db655278SSuman Anna 
2493226f5db4SMathieu Poirier 	if (rproc_alloc_firmware(rproc, name, firmware))
2494226f5db4SMathieu Poirier 		goto put_device;
2495226f5db4SMathieu Poirier 
2496226f5db4SMathieu Poirier 	if (rproc_alloc_ops(rproc, ops))
2497226f5db4SMathieu Poirier 		goto put_device;
2498226f5db4SMathieu Poirier 
2499b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
250008333b91Skeliu 	rproc->index = ida_alloc(&rproc_dev_index, GFP_KERNEL);
2501b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
250208333b91Skeliu 		dev_err(dev, "ida_alloc failed: %d\n", rproc->index);
2503226f5db4SMathieu Poirier 		goto put_device;
2504b5ab5e24SOhad Ben-Cohen 	}
2505b5ab5e24SOhad Ben-Cohen 
2506b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
2507b5ab5e24SOhad Ben-Cohen 
2508400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
2509400e64dfSOhad Ben-Cohen 
2510400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
2511400e64dfSOhad Ben-Cohen 
2512400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
2513400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
2514400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
25157a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
25167bdc9650SBjorn Andersson 	INIT_LIST_HEAD(&rproc->subdevs);
25172666ca91SSarangdhar Joshi 	INIT_LIST_HEAD(&rproc->dump_segments);
2518400e64dfSOhad Ben-Cohen 
25198afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
25208afd519cSFernando Guzman Lugo 
2521400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
2522400e64dfSOhad Ben-Cohen 
2523400e64dfSOhad Ben-Cohen 	return rproc;
25240c2ae2b1SMathieu Poirier 
2525226f5db4SMathieu Poirier put_device:
2526226f5db4SMathieu Poirier 	put_device(&rproc->dev);
25270c2ae2b1SMathieu Poirier 	return NULL;
2528400e64dfSOhad Ben-Cohen }
2529400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
2530400e64dfSOhad Ben-Cohen 
2531400e64dfSOhad Ben-Cohen /**
2532433c0e04SBjorn Andersson  * rproc_free() - unroll rproc_alloc()
2533433c0e04SBjorn Andersson  * @rproc: the remote processor handle
2534433c0e04SBjorn Andersson  *
2535433c0e04SBjorn Andersson  * This function decrements the rproc dev refcount.
2536433c0e04SBjorn Andersson  *
2537433c0e04SBjorn Andersson  * If no one holds any reference to rproc anymore, then its refcount would
2538433c0e04SBjorn Andersson  * now drop to zero, and it would be freed.
2539433c0e04SBjorn Andersson  */
rproc_free(struct rproc * rproc)2540433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc)
2541433c0e04SBjorn Andersson {
2542433c0e04SBjorn Andersson 	put_device(&rproc->dev);
2543433c0e04SBjorn Andersson }
2544433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free);
2545433c0e04SBjorn Andersson 
2546433c0e04SBjorn Andersson /**
2547433c0e04SBjorn Andersson  * rproc_put() - release rproc reference
2548400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
2549400e64dfSOhad Ben-Cohen  *
2550c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
2551400e64dfSOhad Ben-Cohen  *
2552c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
2553c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
2554400e64dfSOhad Ben-Cohen  */
rproc_put(struct rproc * rproc)2555160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
2556400e64dfSOhad Ben-Cohen {
2557*8b46dc5cSMathieu Poirier 	if (rproc->dev.parent->driver)
2558fbb6aacbSBjorn Andersson 		module_put(rproc->dev.parent->driver->owner);
2559*8b46dc5cSMathieu Poirier 	else
2560*8b46dc5cSMathieu Poirier 		module_put(rproc->dev.parent->parent->driver->owner);
2561*8b46dc5cSMathieu Poirier 
2562b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
2563400e64dfSOhad Ben-Cohen }
2564160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
2565400e64dfSOhad Ben-Cohen 
2566400e64dfSOhad Ben-Cohen /**
2567160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
2568400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
2569400e64dfSOhad Ben-Cohen  *
2570400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
2571400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
2572160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
2573400e64dfSOhad Ben-Cohen  * has completed successfully.
2574400e64dfSOhad Ben-Cohen  *
2575160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
2576c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
2577433c0e04SBjorn Andersson  * one last refcount, one still needs to call rproc_free().
2578400e64dfSOhad Ben-Cohen  *
2579f2867434SSuman Anna  * Return: 0 on success and -EINVAL if @rproc isn't valid
2580400e64dfSOhad Ben-Cohen  */
rproc_del(struct rproc * rproc)2581160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
2582400e64dfSOhad Ben-Cohen {
2583400e64dfSOhad Ben-Cohen 	if (!rproc)
2584400e64dfSOhad Ben-Cohen 		return -EINVAL;
2585400e64dfSOhad Ben-Cohen 
2586ddf71187SBjorn Andersson 	/* TODO: make sure this works with rproc->power > 1 */
2587ddf71187SBjorn Andersson 	rproc_shutdown(rproc);
2588ddf71187SBjorn Andersson 
25892099c77dSSarangdhar Joshi 	mutex_lock(&rproc->lock);
25902099c77dSSarangdhar Joshi 	rproc->state = RPROC_DELETED;
25912099c77dSSarangdhar Joshi 	mutex_unlock(&rproc->lock);
25922099c77dSSarangdhar Joshi 
2593b003d45bSSarangdhar Joshi 	rproc_delete_debug_dir(rproc);
2594b003d45bSSarangdhar Joshi 
2595fec47d86SDave Gerlach 	/* the rproc is downref'ed as soon as it's removed from the klist */
2596fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
2597c0abe2caSBjorn Andersson 	list_del_rcu(&rproc->node);
2598fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
2599fec47d86SDave Gerlach 
2600c0abe2caSBjorn Andersson 	/* Ensure that no readers of rproc_list are still active */
2601c0abe2caSBjorn Andersson 	synchronize_rcu();
2602c0abe2caSBjorn Andersson 
2603b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
2604930eec0bSSiddharth Gupta 	rproc_char_device_remove(rproc);
2605400e64dfSOhad Ben-Cohen 
2606400e64dfSOhad Ben-Cohen 	return 0;
2607400e64dfSOhad Ben-Cohen }
2608160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
2609400e64dfSOhad Ben-Cohen 
devm_rproc_free(struct device * dev,void * res)2610305ac5a7SPaul Cercueil static void devm_rproc_free(struct device *dev, void *res)
2611305ac5a7SPaul Cercueil {
2612305ac5a7SPaul Cercueil 	rproc_free(*(struct rproc **)res);
2613305ac5a7SPaul Cercueil }
2614305ac5a7SPaul Cercueil 
2615305ac5a7SPaul Cercueil /**
2616305ac5a7SPaul Cercueil  * devm_rproc_alloc() - resource managed rproc_alloc()
2617305ac5a7SPaul Cercueil  * @dev: the underlying device
2618305ac5a7SPaul Cercueil  * @name: name of this remote processor
2619305ac5a7SPaul Cercueil  * @ops: platform-specific handlers (mainly start/stop)
2620305ac5a7SPaul Cercueil  * @firmware: name of firmware file to load, can be NULL
2621305ac5a7SPaul Cercueil  * @len: length of private data needed by the rproc driver (in bytes)
2622305ac5a7SPaul Cercueil  *
2623305ac5a7SPaul Cercueil  * This function performs like rproc_alloc() but the acquired rproc device will
2624305ac5a7SPaul Cercueil  * automatically be released on driver detach.
2625305ac5a7SPaul Cercueil  *
2626f2867434SSuman Anna  * Return: new rproc instance, or NULL on failure
2627305ac5a7SPaul Cercueil  */
devm_rproc_alloc(struct device * dev,const char * name,const struct rproc_ops * ops,const char * firmware,int len)2628305ac5a7SPaul Cercueil struct rproc *devm_rproc_alloc(struct device *dev, const char *name,
2629305ac5a7SPaul Cercueil 			       const struct rproc_ops *ops,
2630305ac5a7SPaul Cercueil 			       const char *firmware, int len)
2631305ac5a7SPaul Cercueil {
2632305ac5a7SPaul Cercueil 	struct rproc **ptr, *rproc;
2633305ac5a7SPaul Cercueil 
2634305ac5a7SPaul Cercueil 	ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL);
2635305ac5a7SPaul Cercueil 	if (!ptr)
26367dcef398SDan Carpenter 		return NULL;
2637305ac5a7SPaul Cercueil 
2638305ac5a7SPaul Cercueil 	rproc = rproc_alloc(dev, name, ops, firmware, len);
2639305ac5a7SPaul Cercueil 	if (rproc) {
2640305ac5a7SPaul Cercueil 		*ptr = rproc;
2641305ac5a7SPaul Cercueil 		devres_add(dev, ptr);
2642305ac5a7SPaul Cercueil 	} else {
2643305ac5a7SPaul Cercueil 		devres_free(ptr);
2644305ac5a7SPaul Cercueil 	}
2645305ac5a7SPaul Cercueil 
2646305ac5a7SPaul Cercueil 	return rproc;
2647305ac5a7SPaul Cercueil }
2648305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_alloc);
2649305ac5a7SPaul Cercueil 
26508afd519cSFernando Guzman Lugo /**
26517bdc9650SBjorn Andersson  * rproc_add_subdev() - add a subdevice to a remoteproc
26527bdc9650SBjorn Andersson  * @rproc: rproc handle to add the subdevice to
26537bdc9650SBjorn Andersson  * @subdev: subdev handle to register
26544902676fSBjorn Andersson  *
26554902676fSBjorn Andersson  * Caller is responsible for populating optional subdevice function pointers.
26567bdc9650SBjorn Andersson  */
rproc_add_subdev(struct rproc * rproc,struct rproc_subdev * subdev)26574902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
26587bdc9650SBjorn Andersson {
26597bdc9650SBjorn Andersson 	list_add_tail(&subdev->node, &rproc->subdevs);
26607bdc9650SBjorn Andersson }
26617bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev);
26627bdc9650SBjorn Andersson 
26637bdc9650SBjorn Andersson /**
26647bdc9650SBjorn Andersson  * rproc_remove_subdev() - remove a subdevice from a remoteproc
26657bdc9650SBjorn Andersson  * @rproc: rproc handle to remove the subdevice from
26667bdc9650SBjorn Andersson  * @subdev: subdev handle, previously registered with rproc_add_subdev()
26677bdc9650SBjorn Andersson  */
rproc_remove_subdev(struct rproc * rproc,struct rproc_subdev * subdev)26687bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
26697bdc9650SBjorn Andersson {
26707bdc9650SBjorn Andersson 	list_del(&subdev->node);
26717bdc9650SBjorn Andersson }
26727bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev);
26737bdc9650SBjorn Andersson 
26747bdc9650SBjorn Andersson /**
26757c89717fSBjorn Andersson  * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
26767c89717fSBjorn Andersson  * @dev:	child device to find ancestor of
26777c89717fSBjorn Andersson  *
2678f2867434SSuman Anna  * Return: the ancestor rproc instance, or NULL if not found
26797c89717fSBjorn Andersson  */
rproc_get_by_child(struct device * dev)26807c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev)
26817c89717fSBjorn Andersson {
26827c89717fSBjorn Andersson 	for (dev = dev->parent; dev; dev = dev->parent) {
26837c89717fSBjorn Andersson 		if (dev->type == &rproc_type)
26847c89717fSBjorn Andersson 			return dev->driver_data;
26857c89717fSBjorn Andersson 	}
26867c89717fSBjorn Andersson 
26877c89717fSBjorn Andersson 	return NULL;
26887c89717fSBjorn Andersson }
26897c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child);
26907c89717fSBjorn Andersson 
26917c89717fSBjorn Andersson /**
26928afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
26938afd519cSFernando Guzman Lugo  * @rproc: remote processor
26948afd519cSFernando Guzman Lugo  * @type: crash type
26958afd519cSFernando Guzman Lugo  *
26968afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
26978afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
26988afd519cSFernando Guzman Lugo  * non-remoteproc driver.
26998afd519cSFernando Guzman Lugo  *
27008afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
27018afd519cSFernando Guzman Lugo  */
rproc_report_crash(struct rproc * rproc,enum rproc_crash_type type)27028afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
27038afd519cSFernando Guzman Lugo {
27048afd519cSFernando Guzman Lugo 	if (!rproc) {
27058afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
27068afd519cSFernando Guzman Lugo 		return;
27078afd519cSFernando Guzman Lugo 	}
27088afd519cSFernando Guzman Lugo 
2709a781e5aaSRishabh Bhatnagar 	/* Prevent suspend while the remoteproc is being recovered */
2710a781e5aaSRishabh Bhatnagar 	pm_stay_awake(rproc->dev.parent);
2711a781e5aaSRishabh Bhatnagar 
27128afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
27138afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
27148afd519cSFernando Guzman Lugo 
2715cab8300bSMukesh Ojha 	queue_work(rproc_recovery_wq, &rproc->crash_handler);
27168afd519cSFernando Guzman Lugo }
27178afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
27188afd519cSFernando Guzman Lugo 
rproc_panic_handler(struct notifier_block * nb,unsigned long event,void * ptr)2719dc5192c4SBjorn Andersson static int rproc_panic_handler(struct notifier_block *nb, unsigned long event,
2720dc5192c4SBjorn Andersson 			       void *ptr)
2721dc5192c4SBjorn Andersson {
2722dc5192c4SBjorn Andersson 	unsigned int longest = 0;
2723dc5192c4SBjorn Andersson 	struct rproc *rproc;
2724dc5192c4SBjorn Andersson 	unsigned int d;
2725dc5192c4SBjorn Andersson 
2726dc5192c4SBjorn Andersson 	rcu_read_lock();
2727dc5192c4SBjorn Andersson 	list_for_each_entry_rcu(rproc, &rproc_list, node) {
2728800dad00SMathieu Poirier 		if (!rproc->ops->panic)
2729800dad00SMathieu Poirier 			continue;
2730800dad00SMathieu Poirier 
2731800dad00SMathieu Poirier 		if (rproc->state != RPROC_RUNNING &&
2732800dad00SMathieu Poirier 		    rproc->state != RPROC_ATTACHED)
2733dc5192c4SBjorn Andersson 			continue;
2734dc5192c4SBjorn Andersson 
2735dc5192c4SBjorn Andersson 		d = rproc->ops->panic(rproc);
2736dc5192c4SBjorn Andersson 		longest = max(longest, d);
2737dc5192c4SBjorn Andersson 	}
2738dc5192c4SBjorn Andersson 	rcu_read_unlock();
2739dc5192c4SBjorn Andersson 
2740dc5192c4SBjorn Andersson 	/*
2741dc5192c4SBjorn Andersson 	 * Delay for the longest requested duration before returning. This can
2742dc5192c4SBjorn Andersson 	 * be used by the remoteproc drivers to give the remote processor time
2743dc5192c4SBjorn Andersson 	 * to perform any requested operations (such as flush caches), when
2744dc5192c4SBjorn Andersson 	 * it's not possible to signal the Linux side due to the panic.
2745dc5192c4SBjorn Andersson 	 */
2746dc5192c4SBjorn Andersson 	mdelay(longest);
2747dc5192c4SBjorn Andersson 
2748dc5192c4SBjorn Andersson 	return NOTIFY_DONE;
2749dc5192c4SBjorn Andersson }
2750dc5192c4SBjorn Andersson 
rproc_init_panic(void)2751dc5192c4SBjorn Andersson static void __init rproc_init_panic(void)
2752dc5192c4SBjorn Andersson {
2753dc5192c4SBjorn Andersson 	rproc_panic_nb.notifier_call = rproc_panic_handler;
2754dc5192c4SBjorn Andersson 	atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb);
2755dc5192c4SBjorn Andersson }
2756dc5192c4SBjorn Andersson 
rproc_exit_panic(void)2757dc5192c4SBjorn Andersson static void __exit rproc_exit_panic(void)
2758dc5192c4SBjorn Andersson {
2759dc5192c4SBjorn Andersson 	atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb);
2760dc5192c4SBjorn Andersson }
2761dc5192c4SBjorn Andersson 
remoteproc_init(void)2762400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
2763400e64dfSOhad Ben-Cohen {
2764cab8300bSMukesh Ojha 	rproc_recovery_wq = alloc_workqueue("rproc_recovery_wq",
2765cab8300bSMukesh Ojha 						WQ_UNBOUND | WQ_FREEZABLE, 0);
2766cab8300bSMukesh Ojha 	if (!rproc_recovery_wq) {
2767cab8300bSMukesh Ojha 		pr_err("remoteproc: creation of rproc_recovery_wq failed\n");
2768cab8300bSMukesh Ojha 		return -ENOMEM;
2769cab8300bSMukesh Ojha 	}
2770cab8300bSMukesh Ojha 
27712aefbef0SMatt Redfearn 	rproc_init_sysfs();
2772400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
277362b8f9e9SSiddharth Gupta 	rproc_init_cdev();
2774dc5192c4SBjorn Andersson 	rproc_init_panic();
2775b5ab5e24SOhad Ben-Cohen 
2776400e64dfSOhad Ben-Cohen 	return 0;
2777400e64dfSOhad Ben-Cohen }
2778a8f40111SBrandon Maier subsys_initcall(remoteproc_init);
2779400e64dfSOhad Ben-Cohen 
remoteproc_exit(void)2780400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
2781400e64dfSOhad Ben-Cohen {
2782f42f79afSSuman Anna 	ida_destroy(&rproc_dev_index);
2783f42f79afSSuman Anna 
2784cab8300bSMukesh Ojha 	if (!rproc_recovery_wq)
2785cab8300bSMukesh Ojha 		return;
2786cab8300bSMukesh Ojha 
2787dc5192c4SBjorn Andersson 	rproc_exit_panic();
2788400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
27892aefbef0SMatt Redfearn 	rproc_exit_sysfs();
2790cab8300bSMukesh Ojha 	destroy_workqueue(rproc_recovery_wq);
2791400e64dfSOhad Ben-Cohen }
2792400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
2793400e64dfSOhad Ben-Cohen 
2794400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
2795