xref: /linux/drivers/remoteproc/remoteproc_core.c (revision 43d3f2c715cefcfb89b10675728e9bf0d8bb98e3)
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>
23400e64dfSOhad Ben-Cohen #include <linux/slab.h>
24400e64dfSOhad Ben-Cohen #include <linux/mutex.h>
250a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
26400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h>
27e0d07278SJim Quinlan #include <linux/dma-direct.h> /* XXX: pokes into bus_dma_range */
28400e64dfSOhad Ben-Cohen #include <linux/firmware.h>
29400e64dfSOhad Ben-Cohen #include <linux/string.h>
30400e64dfSOhad Ben-Cohen #include <linux/debugfs.h>
31c0abe2caSBjorn Andersson #include <linux/rculist.h>
32400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h>
33400e64dfSOhad Ben-Cohen #include <linux/iommu.h>
34b5ab5e24SOhad Ben-Cohen #include <linux/idr.h>
35400e64dfSOhad Ben-Cohen #include <linux/elf.h>
36a2b950acSOhad Ben-Cohen #include <linux/crc32.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);
61b5ab5e24SOhad Ben-Cohen 
628afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = {
638afd519cSFernando Guzman Lugo 	[RPROC_MMUFAULT]	= "mmufault",
64b3d39032SBjorn Andersson 	[RPROC_WATCHDOG]	= "watchdog",
65b3d39032SBjorn Andersson 	[RPROC_FATAL_ERROR]	= "fatal error",
668afd519cSFernando Guzman Lugo };
678afd519cSFernando Guzman Lugo 
688afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */
698afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type)
708afd519cSFernando Guzman Lugo {
718afd519cSFernando Guzman Lugo 	if (type < ARRAY_SIZE(rproc_crash_names))
728afd519cSFernando Guzman Lugo 		return rproc_crash_names[type];
73b23f7a09SMasanari Iida 	return "unknown";
748afd519cSFernando Guzman Lugo }
758afd519cSFernando Guzman Lugo 
76400e64dfSOhad Ben-Cohen /*
77400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
78400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
79400e64dfSOhad Ben-Cohen  * an IOMMU).
80400e64dfSOhad Ben-Cohen  *
81400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
82400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
83400e64dfSOhad Ben-Cohen  */
84400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
8577ca2332SOhad Ben-Cohen 			     unsigned long iova, int flags, void *token)
86400e64dfSOhad Ben-Cohen {
878afd519cSFernando Guzman Lugo 	struct rproc *rproc = token;
888afd519cSFernando Guzman Lugo 
89400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
90400e64dfSOhad Ben-Cohen 
918afd519cSFernando Guzman Lugo 	rproc_report_crash(rproc, RPROC_MMUFAULT);
928afd519cSFernando Guzman Lugo 
93400e64dfSOhad Ben-Cohen 	/*
94400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
958afd519cSFernando Guzman Lugo 	 * we just used it as a recovery trigger.
96400e64dfSOhad Ben-Cohen 	 */
97400e64dfSOhad Ben-Cohen 	return -ENOSYS;
98400e64dfSOhad Ben-Cohen }
99400e64dfSOhad Ben-Cohen 
100400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
101400e64dfSOhad Ben-Cohen {
102400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
103b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
104400e64dfSOhad Ben-Cohen 	int ret;
105400e64dfSOhad Ben-Cohen 
106315491e5SSuman Anna 	if (!rproc->has_iommu) {
107315491e5SSuman Anna 		dev_dbg(dev, "iommu not present\n");
1080798e1daSMark Grosen 		return 0;
109400e64dfSOhad Ben-Cohen 	}
110400e64dfSOhad Ben-Cohen 
111400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
112400e64dfSOhad Ben-Cohen 	if (!domain) {
113400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
114400e64dfSOhad Ben-Cohen 		return -ENOMEM;
115400e64dfSOhad Ben-Cohen 	}
116400e64dfSOhad Ben-Cohen 
11777ca2332SOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
118400e64dfSOhad Ben-Cohen 
119400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
120400e64dfSOhad Ben-Cohen 	if (ret) {
121400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
122400e64dfSOhad Ben-Cohen 		goto free_domain;
123400e64dfSOhad Ben-Cohen 	}
124400e64dfSOhad Ben-Cohen 
125400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
126400e64dfSOhad Ben-Cohen 
127400e64dfSOhad Ben-Cohen 	return 0;
128400e64dfSOhad Ben-Cohen 
129400e64dfSOhad Ben-Cohen free_domain:
130400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
131400e64dfSOhad Ben-Cohen 	return ret;
132400e64dfSOhad Ben-Cohen }
133400e64dfSOhad Ben-Cohen 
134400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
135400e64dfSOhad Ben-Cohen {
136400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
137b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
138400e64dfSOhad Ben-Cohen 
139400e64dfSOhad Ben-Cohen 	if (!domain)
140400e64dfSOhad Ben-Cohen 		return;
141400e64dfSOhad Ben-Cohen 
142400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
143400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
144400e64dfSOhad Ben-Cohen }
145400e64dfSOhad Ben-Cohen 
146086d0872SLoic Pallardy phys_addr_t rproc_va_to_pa(void *cpu_addr)
147eb30596eSLoic Pallardy {
148eb30596eSLoic Pallardy 	/*
149eb30596eSLoic Pallardy 	 * Return physical address according to virtual address location
150eb30596eSLoic Pallardy 	 * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent
151eb30596eSLoic Pallardy 	 * - in kernel: if region allocated in generic dma memory pool
152eb30596eSLoic Pallardy 	 */
153eb30596eSLoic Pallardy 	if (is_vmalloc_addr(cpu_addr)) {
154eb30596eSLoic Pallardy 		return page_to_phys(vmalloc_to_page(cpu_addr)) +
155eb30596eSLoic Pallardy 				    offset_in_page(cpu_addr);
156eb30596eSLoic Pallardy 	}
157eb30596eSLoic Pallardy 
158eb30596eSLoic Pallardy 	WARN_ON(!virt_addr_valid(cpu_addr));
159eb30596eSLoic Pallardy 	return virt_to_phys(cpu_addr);
160eb30596eSLoic Pallardy }
161086d0872SLoic Pallardy EXPORT_SYMBOL(rproc_va_to_pa);
162eb30596eSLoic Pallardy 
163a01f7cd6SSuman Anna /**
164a01f7cd6SSuman Anna  * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
165a01f7cd6SSuman Anna  * @rproc: handle of a remote processor
166a01f7cd6SSuman Anna  * @da: remoteproc device address to translate
167a01f7cd6SSuman Anna  * @len: length of the memory region @da is pointing to
168a01f7cd6SSuman Anna  *
169400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
170400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
171a01f7cd6SSuman Anna  * device addresses (which are hardcoded in the firmware). They may also have
172a01f7cd6SSuman Anna  * dedicated memory regions internal to the processors, and use them either
173a01f7cd6SSuman Anna  * exclusively or alongside carveouts.
174400e64dfSOhad Ben-Cohen  *
175400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
176400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
177400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
178400e64dfSOhad Ben-Cohen  *
179a01f7cd6SSuman Anna  * This function is a helper function with which we can go over the allocated
180a01f7cd6SSuman Anna  * carveouts and translate specific device addresses to kernel virtual addresses
181a01f7cd6SSuman Anna  * so we can access the referenced memory. This function also allows to perform
182a01f7cd6SSuman Anna  * translations on the internal remoteproc memory regions through a platform
183a01f7cd6SSuman Anna  * implementation specific da_to_va ops, if present.
184a01f7cd6SSuman Anna  *
185a01f7cd6SSuman Anna  * The function returns a valid kernel address on success or NULL on failure.
186400e64dfSOhad Ben-Cohen  *
187400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
188400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
189a01f7cd6SSuman Anna  * here the output of the DMA API for the carveouts, which should be more
190a01f7cd6SSuman Anna  * correct.
191400e64dfSOhad Ben-Cohen  */
1929ce3bf22SClement Leger void *rproc_da_to_va(struct rproc *rproc, u64 da, size_t len)
193400e64dfSOhad Ben-Cohen {
194400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
195400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
196400e64dfSOhad Ben-Cohen 
197a01f7cd6SSuman Anna 	if (rproc->ops->da_to_va) {
198a01f7cd6SSuman Anna 		ptr = rproc->ops->da_to_va(rproc, da, len);
199a01f7cd6SSuman Anna 		if (ptr)
200a01f7cd6SSuman Anna 			goto out;
201a01f7cd6SSuman Anna 	}
202a01f7cd6SSuman Anna 
203400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
204400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
205400e64dfSOhad Ben-Cohen 
20674457c40SLoic Pallardy 		/*  Verify that carveout is allocated */
20774457c40SLoic Pallardy 		if (!carveout->va)
20874457c40SLoic Pallardy 			continue;
20974457c40SLoic Pallardy 
210400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
211400e64dfSOhad Ben-Cohen 		if (offset < 0)
212400e64dfSOhad Ben-Cohen 			continue;
213400e64dfSOhad Ben-Cohen 
214400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
215400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
216400e64dfSOhad Ben-Cohen 			continue;
217400e64dfSOhad Ben-Cohen 
218400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
219400e64dfSOhad Ben-Cohen 
220400e64dfSOhad Ben-Cohen 		break;
221400e64dfSOhad Ben-Cohen 	}
222400e64dfSOhad Ben-Cohen 
223a01f7cd6SSuman Anna out:
224400e64dfSOhad Ben-Cohen 	return ptr;
225400e64dfSOhad Ben-Cohen }
2264afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
227400e64dfSOhad Ben-Cohen 
228b0019ccdSLoic Pallardy /**
229b0019ccdSLoic Pallardy  * rproc_find_carveout_by_name() - lookup the carveout region by a name
230b0019ccdSLoic Pallardy  * @rproc: handle of a remote processor
2312e7d4c2cSArnaud Pouliquen  * @name: carveout name to find (format string)
2322e7d4c2cSArnaud Pouliquen  * @...: optional parameters matching @name string
233b0019ccdSLoic Pallardy  *
234b0019ccdSLoic Pallardy  * Platform driver has the capability to register some pre-allacoted carveout
235b0019ccdSLoic Pallardy  * (physically contiguous memory regions) before rproc firmware loading and
236b0019ccdSLoic Pallardy  * associated resource table analysis. These regions may be dedicated memory
237b0019ccdSLoic Pallardy  * regions internal to the coprocessor or specified DDR region with specific
238b0019ccdSLoic Pallardy  * attributes
239b0019ccdSLoic Pallardy  *
240b0019ccdSLoic Pallardy  * This function is a helper function with which we can go over the
241b0019ccdSLoic Pallardy  * allocated carveouts and return associated region characteristics like
242b0019ccdSLoic Pallardy  * coprocessor address, length or processor virtual address.
243b0019ccdSLoic Pallardy  *
244b0019ccdSLoic Pallardy  * Return: a valid pointer on carveout entry on success or NULL on failure.
245b0019ccdSLoic Pallardy  */
2467e05c8deSLee Jones __printf(2, 3)
247b0019ccdSLoic Pallardy struct rproc_mem_entry *
248b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
249b0019ccdSLoic Pallardy {
250b0019ccdSLoic Pallardy 	va_list args;
251b0019ccdSLoic Pallardy 	char _name[32];
252b0019ccdSLoic Pallardy 	struct rproc_mem_entry *carveout, *mem = NULL;
253b0019ccdSLoic Pallardy 
254b0019ccdSLoic Pallardy 	if (!name)
255b0019ccdSLoic Pallardy 		return NULL;
256b0019ccdSLoic Pallardy 
257b0019ccdSLoic Pallardy 	va_start(args, name);
258b0019ccdSLoic Pallardy 	vsnprintf(_name, sizeof(_name), name, args);
259b0019ccdSLoic Pallardy 	va_end(args);
260b0019ccdSLoic Pallardy 
261b0019ccdSLoic Pallardy 	list_for_each_entry(carveout, &rproc->carveouts, node) {
262b0019ccdSLoic Pallardy 		/* Compare carveout and requested names */
263b0019ccdSLoic Pallardy 		if (!strcmp(carveout->name, _name)) {
264b0019ccdSLoic Pallardy 			mem = carveout;
265b0019ccdSLoic Pallardy 			break;
266b0019ccdSLoic Pallardy 		}
267b0019ccdSLoic Pallardy 	}
268b0019ccdSLoic Pallardy 
269b0019ccdSLoic Pallardy 	return mem;
270b0019ccdSLoic Pallardy }
271b0019ccdSLoic Pallardy 
272c874bf59SLoic Pallardy /**
273c874bf59SLoic Pallardy  * rproc_check_carveout_da() - Check specified carveout da configuration
274c874bf59SLoic Pallardy  * @rproc: handle of a remote processor
275c874bf59SLoic Pallardy  * @mem: pointer on carveout to check
276c874bf59SLoic Pallardy  * @da: area device address
277c874bf59SLoic Pallardy  * @len: associated area size
278c874bf59SLoic Pallardy  *
279c874bf59SLoic Pallardy  * This function is a helper function to verify requested device area (couple
28028d7d5c6SLoic Pallardy  * da, len) is part of specified carveout.
28128d7d5c6SLoic Pallardy  * If da is not set (defined as FW_RSC_ADDR_ANY), only requested length is
28228d7d5c6SLoic Pallardy  * checked.
283c874bf59SLoic Pallardy  *
28428d7d5c6SLoic Pallardy  * Return: 0 if carveout matches request else error
285c874bf59SLoic Pallardy  */
28628d7d5c6SLoic Pallardy static int rproc_check_carveout_da(struct rproc *rproc,
28728d7d5c6SLoic Pallardy 				   struct rproc_mem_entry *mem, u32 da, u32 len)
288c874bf59SLoic Pallardy {
289c874bf59SLoic Pallardy 	struct device *dev = &rproc->dev;
29028d7d5c6SLoic Pallardy 	int delta;
291c874bf59SLoic Pallardy 
292c874bf59SLoic Pallardy 	/* Check requested resource length */
293c874bf59SLoic Pallardy 	if (len > mem->len) {
294c874bf59SLoic Pallardy 		dev_err(dev, "Registered carveout doesn't fit len request\n");
29528d7d5c6SLoic Pallardy 		return -EINVAL;
296c874bf59SLoic Pallardy 	}
297c874bf59SLoic Pallardy 
298c874bf59SLoic Pallardy 	if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) {
29928d7d5c6SLoic Pallardy 		/* Address doesn't match registered carveout configuration */
30028d7d5c6SLoic Pallardy 		return -EINVAL;
301c874bf59SLoic Pallardy 	} else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) {
302c874bf59SLoic Pallardy 		delta = da - mem->da;
303c874bf59SLoic Pallardy 
304c874bf59SLoic Pallardy 		/* Check requested resource belongs to registered carveout */
305c874bf59SLoic Pallardy 		if (delta < 0) {
306c874bf59SLoic Pallardy 			dev_err(dev,
307c874bf59SLoic Pallardy 				"Registered carveout doesn't fit da request\n");
30828d7d5c6SLoic Pallardy 			return -EINVAL;
309c874bf59SLoic Pallardy 		}
310c874bf59SLoic Pallardy 
311c874bf59SLoic Pallardy 		if (delta + len > mem->len) {
312c874bf59SLoic Pallardy 			dev_err(dev,
313c874bf59SLoic Pallardy 				"Registered carveout doesn't fit len request\n");
31428d7d5c6SLoic Pallardy 			return -EINVAL;
315c874bf59SLoic Pallardy 		}
316c874bf59SLoic Pallardy 	}
317c874bf59SLoic Pallardy 
318c874bf59SLoic Pallardy 	return 0;
319c874bf59SLoic Pallardy }
320c874bf59SLoic Pallardy 
3216db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
322400e64dfSOhad Ben-Cohen {
3237a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
324b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3256db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
326c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
327096ee786SClement Leger 	int ret, notifyid;
328c6aed238SLoic Pallardy 	struct rproc_mem_entry *mem;
329096ee786SClement Leger 	size_t size;
330400e64dfSOhad Ben-Cohen 
3316db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
3326db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
3336db20ea8SOhad Ben-Cohen 
334c6aed238SLoic Pallardy 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
335c6aed238SLoic Pallardy 
336c6aed238SLoic Pallardy 	/* Search for pre-registered carveout */
337c6aed238SLoic Pallardy 	mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
338c6aed238SLoic Pallardy 					  i);
339c6aed238SLoic Pallardy 	if (mem) {
340c6aed238SLoic Pallardy 		if (rproc_check_carveout_da(rproc, mem, rsc->vring[i].da, size))
341c6aed238SLoic Pallardy 			return -ENOMEM;
342c6aed238SLoic Pallardy 	} else {
343c6aed238SLoic Pallardy 		/* Register carveout in in list */
34499cf0361SBen Dooks (Codethink) 		mem = rproc_mem_entry_init(dev, NULL, 0,
34599cf0361SBen Dooks (Codethink) 					   size, rsc->vring[i].da,
346c6aed238SLoic Pallardy 					   rproc_alloc_carveout,
347c6aed238SLoic Pallardy 					   rproc_release_carveout,
348c6aed238SLoic Pallardy 					   "vdev%dvring%d",
349c6aed238SLoic Pallardy 					   rvdev->index, i);
350c6aed238SLoic Pallardy 		if (!mem) {
351c6aed238SLoic Pallardy 			dev_err(dev, "Can't allocate memory entry structure\n");
352c6aed238SLoic Pallardy 			return -ENOMEM;
353c6aed238SLoic Pallardy 		}
354c6aed238SLoic Pallardy 
355c6aed238SLoic Pallardy 		rproc_add_carveout(rproc, mem);
3566db20ea8SOhad Ben-Cohen 	}
3576db20ea8SOhad Ben-Cohen 
3586db20ea8SOhad Ben-Cohen 	/*
3596db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
3606db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
3616db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
3626db20ea8SOhad Ben-Cohen 	 */
36315fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
364b39599b7SSuman Anna 	if (ret < 0) {
36515fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
3666db20ea8SOhad Ben-Cohen 		return ret;
3676db20ea8SOhad Ben-Cohen 	}
36815fc6110STejun Heo 	notifyid = ret;
3696db20ea8SOhad Ben-Cohen 
37048f18f89SBjorn Andersson 	/* Potentially bump max_notifyid */
37148f18f89SBjorn Andersson 	if (notifyid > rproc->max_notifyid)
37248f18f89SBjorn Andersson 		rproc->max_notifyid = notifyid;
37348f18f89SBjorn Andersson 
3746db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
3756db20ea8SOhad Ben-Cohen 
376c6aed238SLoic Pallardy 	/* Let the rproc know the notifyid of this vring.*/
377c0d63157SSjur Brændeland 	rsc->vring[i].notifyid = notifyid;
3786db20ea8SOhad Ben-Cohen 	return 0;
3796db20ea8SOhad Ben-Cohen }
3806db20ea8SOhad Ben-Cohen 
381400e64dfSOhad Ben-Cohen static int
3826db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
383400e64dfSOhad Ben-Cohen {
384400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
385b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
386400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
3876db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
388400e64dfSOhad Ben-Cohen 
3899d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
3907a186941SOhad Ben-Cohen 		i, vring->da, vring->num, vring->align);
3917a186941SOhad Ben-Cohen 
39263140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
39363140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
39463140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
39563140e0eSOhad Ben-Cohen 			vring->num, vring->align);
396400e64dfSOhad Ben-Cohen 		return -EINVAL;
397400e64dfSOhad Ben-Cohen 	}
398400e64dfSOhad Ben-Cohen 
3996db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
4006db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
4016db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
402400e64dfSOhad Ben-Cohen 
403400e64dfSOhad Ben-Cohen 	return 0;
404400e64dfSOhad Ben-Cohen }
405400e64dfSOhad Ben-Cohen 
4066db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
4077a186941SOhad Ben-Cohen {
4086db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
40900a0eec5SClement Leger 	int idx = rvring - rvring->rvdev->vring;
410c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
4117a186941SOhad Ben-Cohen 
4127a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
413099a3f33SSjur Brændeland 
4144d3ebb3bSMathieu Poirier 	/*
4154d3ebb3bSMathieu Poirier 	 * At this point rproc_stop() has been called and the installed resource
4164d3ebb3bSMathieu Poirier 	 * table in the remote processor memory may no longer be accessible. As
4174d3ebb3bSMathieu Poirier 	 * such and as per rproc_stop(), rproc->table_ptr points to the cached
4184d3ebb3bSMathieu Poirier 	 * resource table (rproc->cached_table).  The cached resource table is
4194d3ebb3bSMathieu Poirier 	 * only available when a remote processor has been booted by the
4204d3ebb3bSMathieu Poirier 	 * remoteproc core, otherwise it is NULL.
4214d3ebb3bSMathieu Poirier 	 *
4224d3ebb3bSMathieu Poirier 	 * Based on the above, reset the virtio device section in the cached
4234d3ebb3bSMathieu Poirier 	 * resource table only if there is one to work with.
4244d3ebb3bSMathieu Poirier 	 */
4254d3ebb3bSMathieu Poirier 	if (rproc->table_ptr) {
426c0d63157SSjur Brændeland 		rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
427c0d63157SSjur Brændeland 		rsc->vring[idx].da = 0;
428c0d63157SSjur Brændeland 		rsc->vring[idx].notifyid = -1;
4297a186941SOhad Ben-Cohen 	}
4304d3ebb3bSMathieu Poirier }
4317a186941SOhad Ben-Cohen 
4326f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev)
433f5bcb353SBjorn Andersson {
434f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
435f5bcb353SBjorn Andersson 
436f5bcb353SBjorn Andersson 	return rproc_add_virtio_dev(rvdev, rvdev->id);
437f5bcb353SBjorn Andersson }
438f5bcb353SBjorn Andersson 
4396f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
440f5bcb353SBjorn Andersson {
441f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
442d4c036feSLoic Pallardy 	int ret;
443f5bcb353SBjorn Andersson 
444d4c036feSLoic Pallardy 	ret = device_for_each_child(&rvdev->dev, NULL, rproc_remove_virtio_dev);
445d4c036feSLoic Pallardy 	if (ret)
446d4c036feSLoic Pallardy 		dev_warn(&rvdev->dev, "can't remove vdev child device: %d\n", ret);
447f5bcb353SBjorn Andersson }
448f5bcb353SBjorn Andersson 
449400e64dfSOhad Ben-Cohen /**
450086d0872SLoic Pallardy  * rproc_rvdev_release() - release the existence of a rvdev
451086d0872SLoic Pallardy  *
452086d0872SLoic Pallardy  * @dev: the subdevice's dev
453086d0872SLoic Pallardy  */
454086d0872SLoic Pallardy static void rproc_rvdev_release(struct device *dev)
455086d0872SLoic Pallardy {
456086d0872SLoic Pallardy 	struct rproc_vdev *rvdev = container_of(dev, struct rproc_vdev, dev);
457086d0872SLoic Pallardy 
458086d0872SLoic Pallardy 	of_reserved_mem_device_release(dev);
459086d0872SLoic Pallardy 
460086d0872SLoic Pallardy 	kfree(rvdev);
461086d0872SLoic Pallardy }
462086d0872SLoic Pallardy 
463e0d07278SJim Quinlan static int copy_dma_range_map(struct device *to, struct device *from)
464e0d07278SJim Quinlan {
465e0d07278SJim Quinlan 	const struct bus_dma_region *map = from->dma_range_map, *new_map, *r;
466e0d07278SJim Quinlan 	int num_ranges = 0;
467e0d07278SJim Quinlan 
468e0d07278SJim Quinlan 	if (!map)
469e0d07278SJim Quinlan 		return 0;
470e0d07278SJim Quinlan 
471e0d07278SJim Quinlan 	for (r = map; r->size; r++)
472e0d07278SJim Quinlan 		num_ranges++;
473e0d07278SJim Quinlan 
474e0d07278SJim Quinlan 	new_map = kmemdup(map, array_size(num_ranges + 1, sizeof(*map)),
475e0d07278SJim Quinlan 			  GFP_KERNEL);
476e0d07278SJim Quinlan 	if (!new_map)
477e0d07278SJim Quinlan 		return -ENOMEM;
478e0d07278SJim Quinlan 	to->dma_range_map = new_map;
479e0d07278SJim Quinlan 	return 0;
480e0d07278SJim Quinlan }
481e0d07278SJim Quinlan 
482086d0872SLoic Pallardy /**
483fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
484400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
485400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
4862e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
487fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
488400e64dfSOhad Ben-Cohen  *
4897a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
4907a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
4917a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
4927a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
493400e64dfSOhad Ben-Cohen  *
4947a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
4957a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
4967a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
4977a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
4987a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
499400e64dfSOhad Ben-Cohen  *
5007a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
5017a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
5027a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
5037a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
5047a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
5057a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
5067a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
507400e64dfSOhad Ben-Cohen  *
508400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
509400e64dfSOhad Ben-Cohen  */
510fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
511a2b950acSOhad Ben-Cohen 			     int offset, int avail)
512400e64dfSOhad Ben-Cohen {
513b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
5147a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
5157a186941SOhad Ben-Cohen 	int i, ret;
516086d0872SLoic Pallardy 	char name[16];
517fd2c15ecSOhad Ben-Cohen 
518fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
519c8784657SGustavo A. R. Silva 	if (struct_size(rsc, vring, rsc->num_of_vrings) + rsc->config_len >
520c8784657SGustavo A. R. Silva 			avail) {
521b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
522fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
523fd2c15ecSOhad Ben-Cohen 	}
524fd2c15ecSOhad Ben-Cohen 
525fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
526fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
527fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
528fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
529fd2c15ecSOhad Ben-Cohen 	}
530fd2c15ecSOhad Ben-Cohen 
5319d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
532fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
533400e64dfSOhad Ben-Cohen 
5347a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
5357a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
536fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
537400e64dfSOhad Ben-Cohen 		return -EINVAL;
538400e64dfSOhad Ben-Cohen 	}
539400e64dfSOhad Ben-Cohen 
540899585adSAnna, Suman 	rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
5417a186941SOhad Ben-Cohen 	if (!rvdev)
5427a186941SOhad Ben-Cohen 		return -ENOMEM;
5437a186941SOhad Ben-Cohen 
544aab8d802SBjorn Andersson 	kref_init(&rvdev->refcount);
545aab8d802SBjorn Andersson 
546f5bcb353SBjorn Andersson 	rvdev->id = rsc->id;
5477a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
548c6aed238SLoic Pallardy 	rvdev->index = rproc->nb_vdev++;
5497a186941SOhad Ben-Cohen 
550086d0872SLoic Pallardy 	/* Initialise vdev subdevice */
551086d0872SLoic Pallardy 	snprintf(name, sizeof(name), "vdev%dbuffer", rvdev->index);
552c774ad01SSuman Anna 	rvdev->dev.parent = &rproc->dev;
553e0d07278SJim Quinlan 	ret = copy_dma_range_map(&rvdev->dev, rproc->dev.parent);
554e0d07278SJim Quinlan 	if (ret)
555e0d07278SJim Quinlan 		return ret;
556086d0872SLoic Pallardy 	rvdev->dev.release = rproc_rvdev_release;
557086d0872SLoic Pallardy 	dev_set_name(&rvdev->dev, "%s#%s", dev_name(rvdev->dev.parent), name);
558086d0872SLoic Pallardy 	dev_set_drvdata(&rvdev->dev, rvdev);
559086d0872SLoic Pallardy 
560086d0872SLoic Pallardy 	ret = device_register(&rvdev->dev);
561086d0872SLoic Pallardy 	if (ret) {
562086d0872SLoic Pallardy 		put_device(&rvdev->dev);
563086d0872SLoic Pallardy 		return ret;
564086d0872SLoic Pallardy 	}
565086d0872SLoic Pallardy 	/* Make device dma capable by inheriting from parent's capabilities */
566086d0872SLoic Pallardy 	set_dma_ops(&rvdev->dev, get_dma_ops(rproc->dev.parent));
567086d0872SLoic Pallardy 
568086d0872SLoic Pallardy 	ret = dma_coerce_mask_and_coherent(&rvdev->dev,
569086d0872SLoic Pallardy 					   dma_get_mask(rproc->dev.parent));
570086d0872SLoic Pallardy 	if (ret) {
571086d0872SLoic Pallardy 		dev_warn(dev,
572086d0872SLoic Pallardy 			 "Failed to set DMA mask %llx. Trying to continue... %x\n",
573086d0872SLoic Pallardy 			 dma_get_mask(rproc->dev.parent), ret);
574086d0872SLoic Pallardy 	}
575086d0872SLoic Pallardy 
5766db20ea8SOhad Ben-Cohen 	/* parse the vrings */
577fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
5786db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
5797a186941SOhad Ben-Cohen 		if (ret)
5806db20ea8SOhad Ben-Cohen 			goto free_rvdev;
581fd2c15ecSOhad Ben-Cohen 	}
582fd2c15ecSOhad Ben-Cohen 
583a2b950acSOhad Ben-Cohen 	/* remember the resource offset*/
584a2b950acSOhad Ben-Cohen 	rvdev->rsc_offset = offset;
585400e64dfSOhad Ben-Cohen 
586a863af5dSBjorn Andersson 	/* allocate the vring resources */
587a863af5dSBjorn Andersson 	for (i = 0; i < rsc->num_of_vrings; i++) {
588a863af5dSBjorn Andersson 		ret = rproc_alloc_vring(rvdev, i);
589a863af5dSBjorn Andersson 		if (ret)
590a863af5dSBjorn Andersson 			goto unwind_vring_allocations;
591a863af5dSBjorn Andersson 	}
592a863af5dSBjorn Andersson 
5937a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
594400e64dfSOhad Ben-Cohen 
5956f8b0373SAlex Elder 	rvdev->subdev.start = rproc_vdev_do_start;
5966f8b0373SAlex Elder 	rvdev->subdev.stop = rproc_vdev_do_stop;
5974902676fSBjorn Andersson 
5984902676fSBjorn Andersson 	rproc_add_subdev(rproc, &rvdev->subdev);
599400e64dfSOhad Ben-Cohen 
600400e64dfSOhad Ben-Cohen 	return 0;
6017a186941SOhad Ben-Cohen 
602a863af5dSBjorn Andersson unwind_vring_allocations:
603a863af5dSBjorn Andersson 	for (i--; i >= 0; i--)
604a863af5dSBjorn Andersson 		rproc_free_vring(&rvdev->vring[i]);
6056db20ea8SOhad Ben-Cohen free_rvdev:
606086d0872SLoic Pallardy 	device_unregister(&rvdev->dev);
6077a186941SOhad Ben-Cohen 	return ret;
608400e64dfSOhad Ben-Cohen }
609400e64dfSOhad Ben-Cohen 
610aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref)
611aab8d802SBjorn Andersson {
612aab8d802SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
613a863af5dSBjorn Andersson 	struct rproc_vring *rvring;
614f5bcb353SBjorn Andersson 	struct rproc *rproc = rvdev->rproc;
615a863af5dSBjorn Andersson 	int id;
616a863af5dSBjorn Andersson 
617a863af5dSBjorn Andersson 	for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
618a863af5dSBjorn Andersson 		rvring = &rvdev->vring[id];
619a863af5dSBjorn Andersson 		rproc_free_vring(rvring);
620a863af5dSBjorn Andersson 	}
621aab8d802SBjorn Andersson 
622f5bcb353SBjorn Andersson 	rproc_remove_subdev(rproc, &rvdev->subdev);
623aab8d802SBjorn Andersson 	list_del(&rvdev->node);
624086d0872SLoic Pallardy 	device_unregister(&rvdev->dev);
625aab8d802SBjorn Andersson }
626aab8d802SBjorn Andersson 
627400e64dfSOhad Ben-Cohen /**
628400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
629400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
630400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
6312e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
632fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
633400e64dfSOhad Ben-Cohen  *
634400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
635400e64dfSOhad Ben-Cohen  * export it via debugfs.
636400e64dfSOhad Ben-Cohen  *
637400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
638400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
639400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
640400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
641400e64dfSOhad Ben-Cohen  *
642400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
643400e64dfSOhad Ben-Cohen  */
644fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
645a2b950acSOhad Ben-Cohen 			      int offset, int avail)
646400e64dfSOhad Ben-Cohen {
647a987e6b9SLoic Pallardy 	struct rproc_debug_trace *trace;
648b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
649400e64dfSOhad Ben-Cohen 	char name[15];
650400e64dfSOhad Ben-Cohen 
651fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
652b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
653fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
654fd2c15ecSOhad Ben-Cohen 	}
655fd2c15ecSOhad Ben-Cohen 
656fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
657fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
658fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
659fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
660fd2c15ecSOhad Ben-Cohen 	}
661fd2c15ecSOhad Ben-Cohen 
662400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
663172e6ab1SSuman Anna 	if (!trace)
664400e64dfSOhad Ben-Cohen 		return -ENOMEM;
665400e64dfSOhad Ben-Cohen 
666400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
667a987e6b9SLoic Pallardy 	trace->trace_mem.len = rsc->len;
668a987e6b9SLoic Pallardy 	trace->trace_mem.da = rsc->da;
669a987e6b9SLoic Pallardy 
670a987e6b9SLoic Pallardy 	/* set pointer on rproc device */
671a987e6b9SLoic Pallardy 	trace->rproc = rproc;
672400e64dfSOhad Ben-Cohen 
673400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
674400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
675400e64dfSOhad Ben-Cohen 
676400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
677a987e6b9SLoic Pallardy 	trace->tfile = rproc_create_trace_file(name, rproc, trace);
678a987e6b9SLoic Pallardy 	if (!trace->tfile) {
679400e64dfSOhad Ben-Cohen 		kfree(trace);
680400e64dfSOhad Ben-Cohen 		return -EINVAL;
681400e64dfSOhad Ben-Cohen 	}
682400e64dfSOhad Ben-Cohen 
683400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
684400e64dfSOhad Ben-Cohen 
685400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
686400e64dfSOhad Ben-Cohen 
687a987e6b9SLoic Pallardy 	dev_dbg(dev, "%s added: da 0x%x, len 0x%x\n",
688a987e6b9SLoic Pallardy 		name, rsc->da, rsc->len);
689400e64dfSOhad Ben-Cohen 
690400e64dfSOhad Ben-Cohen 	return 0;
691400e64dfSOhad Ben-Cohen }
692400e64dfSOhad Ben-Cohen 
693400e64dfSOhad Ben-Cohen /**
694400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
695400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
696400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
6972e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
698fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
699400e64dfSOhad Ben-Cohen  *
700400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
701400e64dfSOhad Ben-Cohen  *
702400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
703400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
704400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
705400e64dfSOhad Ben-Cohen  *
706400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
707400e64dfSOhad Ben-Cohen  *
708400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
709400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
710400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
711400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
712400e64dfSOhad Ben-Cohen  *
713400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
714400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
715400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
716400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
717400e64dfSOhad Ben-Cohen  * are outside those ranges.
718400e64dfSOhad Ben-Cohen  */
719fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
720a2b950acSOhad Ben-Cohen 			       int offset, int avail)
721400e64dfSOhad Ben-Cohen {
722400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
723b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
724400e64dfSOhad Ben-Cohen 	int ret;
725400e64dfSOhad Ben-Cohen 
726400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
727400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
728400e64dfSOhad Ben-Cohen 		return -EINVAL;
729400e64dfSOhad Ben-Cohen 
730fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
731b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
732fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
733fd2c15ecSOhad Ben-Cohen 	}
734fd2c15ecSOhad Ben-Cohen 
735fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
736fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
737b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
738fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
739fd2c15ecSOhad Ben-Cohen 	}
740fd2c15ecSOhad Ben-Cohen 
741400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
742172e6ab1SSuman Anna 	if (!mapping)
743400e64dfSOhad Ben-Cohen 		return -ENOMEM;
744400e64dfSOhad Ben-Cohen 
745400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
746400e64dfSOhad Ben-Cohen 	if (ret) {
747b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
748400e64dfSOhad Ben-Cohen 		goto out;
749400e64dfSOhad Ben-Cohen 	}
750400e64dfSOhad Ben-Cohen 
751400e64dfSOhad Ben-Cohen 	/*
752400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
753400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
754400e64dfSOhad Ben-Cohen 	 *
755400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
756400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
757400e64dfSOhad Ben-Cohen 	 */
758400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
759400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
760400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
761400e64dfSOhad Ben-Cohen 
762b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
763400e64dfSOhad Ben-Cohen 		rsc->pa, rsc->da, rsc->len);
764400e64dfSOhad Ben-Cohen 
765400e64dfSOhad Ben-Cohen 	return 0;
766400e64dfSOhad Ben-Cohen 
767400e64dfSOhad Ben-Cohen out:
768400e64dfSOhad Ben-Cohen 	kfree(mapping);
769400e64dfSOhad Ben-Cohen 	return ret;
770400e64dfSOhad Ben-Cohen }
771400e64dfSOhad Ben-Cohen 
772400e64dfSOhad Ben-Cohen /**
773d7c51706SLoic Pallardy  * rproc_alloc_carveout() - allocated specified carveout
774d7c51706SLoic Pallardy  * @rproc: rproc handle
775d7c51706SLoic Pallardy  * @mem: the memory entry to allocate
776d7c51706SLoic Pallardy  *
777d7c51706SLoic Pallardy  * This function allocate specified memory entry @mem using
778d7c51706SLoic Pallardy  * dma_alloc_coherent() as default allocator
779d7c51706SLoic Pallardy  */
780d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc,
781d7c51706SLoic Pallardy 				struct rproc_mem_entry *mem)
782d7c51706SLoic Pallardy {
783d7c51706SLoic Pallardy 	struct rproc_mem_entry *mapping = NULL;
784d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
785d7c51706SLoic Pallardy 	dma_addr_t dma;
786d7c51706SLoic Pallardy 	void *va;
787d7c51706SLoic Pallardy 	int ret;
788d7c51706SLoic Pallardy 
789d7c51706SLoic Pallardy 	va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL);
790d7c51706SLoic Pallardy 	if (!va) {
791d7c51706SLoic Pallardy 		dev_err(dev->parent,
792096ee786SClement Leger 			"failed to allocate dma memory: len 0x%zx\n",
793096ee786SClement Leger 			mem->len);
794d7c51706SLoic Pallardy 		return -ENOMEM;
795d7c51706SLoic Pallardy 	}
796d7c51706SLoic Pallardy 
797096ee786SClement Leger 	dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%zx\n",
798d7c51706SLoic Pallardy 		va, &dma, mem->len);
799d7c51706SLoic Pallardy 
80060f849a5SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY && !rproc->domain) {
80160f849a5SLoic Pallardy 		/*
80260f849a5SLoic Pallardy 		 * Check requested da is equal to dma address
80360f849a5SLoic Pallardy 		 * and print a warn message in case of missalignment.
80460f849a5SLoic Pallardy 		 * Don't stop rproc_start sequence as coprocessor may
80560f849a5SLoic Pallardy 		 * build pa to da translation on its side.
80660f849a5SLoic Pallardy 		 */
80760f849a5SLoic Pallardy 		if (mem->da != (u32)dma)
80860f849a5SLoic Pallardy 			dev_warn(dev->parent,
80960f849a5SLoic Pallardy 				 "Allocated carveout doesn't fit device address request\n");
81060f849a5SLoic Pallardy 	}
81160f849a5SLoic Pallardy 
812d7c51706SLoic Pallardy 	/*
813d7c51706SLoic Pallardy 	 * Ok, this is non-standard.
814d7c51706SLoic Pallardy 	 *
815d7c51706SLoic Pallardy 	 * Sometimes we can't rely on the generic iommu-based DMA API
816d7c51706SLoic Pallardy 	 * to dynamically allocate the device address and then set the IOMMU
817d7c51706SLoic Pallardy 	 * tables accordingly, because some remote processors might
818d7c51706SLoic Pallardy 	 * _require_ us to use hard coded device addresses that their
819d7c51706SLoic Pallardy 	 * firmware was compiled with.
820d7c51706SLoic Pallardy 	 *
821d7c51706SLoic Pallardy 	 * In this case, we must use the IOMMU API directly and map
822d7c51706SLoic Pallardy 	 * the memory to the device address as expected by the remote
823d7c51706SLoic Pallardy 	 * processor.
824d7c51706SLoic Pallardy 	 *
825d7c51706SLoic Pallardy 	 * Obviously such remote processor devices should not be configured
826d7c51706SLoic Pallardy 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
827d7c51706SLoic Pallardy 	 * physical address in this case.
828d7c51706SLoic Pallardy 	 */
82960f849a5SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY && rproc->domain) {
830d7c51706SLoic Pallardy 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
831d7c51706SLoic Pallardy 		if (!mapping) {
832d7c51706SLoic Pallardy 			ret = -ENOMEM;
833d7c51706SLoic Pallardy 			goto dma_free;
834d7c51706SLoic Pallardy 		}
835d7c51706SLoic Pallardy 
836d7c51706SLoic Pallardy 		ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
837d7c51706SLoic Pallardy 				mem->flags);
838d7c51706SLoic Pallardy 		if (ret) {
839d7c51706SLoic Pallardy 			dev_err(dev, "iommu_map failed: %d\n", ret);
840d7c51706SLoic Pallardy 			goto free_mapping;
841d7c51706SLoic Pallardy 		}
842d7c51706SLoic Pallardy 
843d7c51706SLoic Pallardy 		/*
844d7c51706SLoic Pallardy 		 * We'll need this info later when we'll want to unmap
845d7c51706SLoic Pallardy 		 * everything (e.g. on shutdown).
846d7c51706SLoic Pallardy 		 *
847d7c51706SLoic Pallardy 		 * We can't trust the remote processor not to change the
848d7c51706SLoic Pallardy 		 * resource table, so we must maintain this info independently.
849d7c51706SLoic Pallardy 		 */
850d7c51706SLoic Pallardy 		mapping->da = mem->da;
851d7c51706SLoic Pallardy 		mapping->len = mem->len;
852d7c51706SLoic Pallardy 		list_add_tail(&mapping->node, &rproc->mappings);
853d7c51706SLoic Pallardy 
854d7c51706SLoic Pallardy 		dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
855d7c51706SLoic Pallardy 			mem->da, &dma);
85660f849a5SLoic Pallardy 	}
85760f849a5SLoic Pallardy 
85860f849a5SLoic Pallardy 	if (mem->da == FW_RSC_ADDR_ANY) {
859b36de8cfSLoic Pallardy 		/* Update device address as undefined by requester */
860b36de8cfSLoic Pallardy 		if ((u64)dma & HIGH_BITS_MASK)
861b36de8cfSLoic Pallardy 			dev_warn(dev, "DMA address cast in 32bit to fit resource table format\n");
862b36de8cfSLoic Pallardy 
863d7c51706SLoic Pallardy 		mem->da = (u32)dma;
864d7c51706SLoic Pallardy 	}
865d7c51706SLoic Pallardy 
86680137b40SLoic Pallardy 	mem->dma = dma;
867d7c51706SLoic Pallardy 	mem->va = va;
868d7c51706SLoic Pallardy 
869d7c51706SLoic Pallardy 	return 0;
870d7c51706SLoic Pallardy 
871d7c51706SLoic Pallardy free_mapping:
872d7c51706SLoic Pallardy 	kfree(mapping);
873d7c51706SLoic Pallardy dma_free:
874d7c51706SLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, va, dma);
875d7c51706SLoic Pallardy 	return ret;
876d7c51706SLoic Pallardy }
877d7c51706SLoic Pallardy 
878d7c51706SLoic Pallardy /**
879f2e74abfSLoic Pallardy  * rproc_release_carveout() - release acquired carveout
880f2e74abfSLoic Pallardy  * @rproc: rproc handle
881f2e74abfSLoic Pallardy  * @mem: the memory entry to release
882f2e74abfSLoic Pallardy  *
883f2e74abfSLoic Pallardy  * This function releases specified memory entry @mem allocated via
884d7c51706SLoic Pallardy  * rproc_alloc_carveout() function by @rproc.
885f2e74abfSLoic Pallardy  */
886f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc,
887f2e74abfSLoic Pallardy 				  struct rproc_mem_entry *mem)
888f2e74abfSLoic Pallardy {
889f2e74abfSLoic Pallardy 	struct device *dev = &rproc->dev;
890f2e74abfSLoic Pallardy 
891f2e74abfSLoic Pallardy 	/* clean up carveout allocations */
892f2e74abfSLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
893f2e74abfSLoic Pallardy 	return 0;
894f2e74abfSLoic Pallardy }
895f2e74abfSLoic Pallardy 
896f2e74abfSLoic Pallardy /**
897400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
898400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
899400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
9002e7d4c2cSArnaud Pouliquen  * @offset: offset of the resource entry
901fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
902400e64dfSOhad Ben-Cohen  *
903400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
904400e64dfSOhad Ben-Cohen  * contiguous memory regions.
905400e64dfSOhad Ben-Cohen  *
906400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
907400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
908400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
909400e64dfSOhad Ben-Cohen  *
910400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
911400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
912400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
913400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
914400e64dfSOhad Ben-Cohen  */
915fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
916a2b950acSOhad Ben-Cohen 				 struct fw_rsc_carveout *rsc,
917a2b950acSOhad Ben-Cohen 				 int offset, int avail)
918400e64dfSOhad Ben-Cohen {
919d7c51706SLoic Pallardy 	struct rproc_mem_entry *carveout;
920b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
921400e64dfSOhad Ben-Cohen 
922fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
923b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
924fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
925fd2c15ecSOhad Ben-Cohen 	}
926fd2c15ecSOhad Ben-Cohen 
927fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
928fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
929fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
930fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
931fd2c15ecSOhad Ben-Cohen 	}
932fd2c15ecSOhad Ben-Cohen 
9339d7814a9SAnna, Suman 	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
93435386166SLee Jones 		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
935fd2c15ecSOhad Ben-Cohen 
936ffa5f9c8SLoic Pallardy 	/*
937ffa5f9c8SLoic Pallardy 	 * Check carveout rsc already part of a registered carveout,
938ffa5f9c8SLoic Pallardy 	 * Search by name, then check the da and length
939ffa5f9c8SLoic Pallardy 	 */
940ffa5f9c8SLoic Pallardy 	carveout = rproc_find_carveout_by_name(rproc, rsc->name);
941ffa5f9c8SLoic Pallardy 
942ffa5f9c8SLoic Pallardy 	if (carveout) {
943ffa5f9c8SLoic Pallardy 		if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
944ffa5f9c8SLoic Pallardy 			dev_err(dev,
945ffa5f9c8SLoic Pallardy 				"Carveout already associated to resource table\n");
946ffa5f9c8SLoic Pallardy 			return -ENOMEM;
947ffa5f9c8SLoic Pallardy 		}
948ffa5f9c8SLoic Pallardy 
949ffa5f9c8SLoic Pallardy 		if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len))
950ffa5f9c8SLoic Pallardy 			return -ENOMEM;
951ffa5f9c8SLoic Pallardy 
952ffa5f9c8SLoic Pallardy 		/* Update memory carveout with resource table info */
953ffa5f9c8SLoic Pallardy 		carveout->rsc_offset = offset;
954ffa5f9c8SLoic Pallardy 		carveout->flags = rsc->flags;
955ffa5f9c8SLoic Pallardy 
956ffa5f9c8SLoic Pallardy 		return 0;
957ffa5f9c8SLoic Pallardy 	}
958ffa5f9c8SLoic Pallardy 
959d7c51706SLoic Pallardy 	/* Register carveout in in list */
96099cf0361SBen Dooks (Codethink) 	carveout = rproc_mem_entry_init(dev, NULL, 0, rsc->len, rsc->da,
961d7c51706SLoic Pallardy 					rproc_alloc_carveout,
962d7c51706SLoic Pallardy 					rproc_release_carveout, rsc->name);
963d7c51706SLoic Pallardy 	if (!carveout) {
964d7c51706SLoic Pallardy 		dev_err(dev, "Can't allocate memory entry structure\n");
96572029c90SLoic Pallardy 		return -ENOMEM;
966400e64dfSOhad Ben-Cohen 	}
967400e64dfSOhad Ben-Cohen 
968d7c51706SLoic Pallardy 	carveout->flags = rsc->flags;
969d7c51706SLoic Pallardy 	carveout->rsc_offset = offset;
97015c0b025SLoic Pallardy 	rproc_add_carveout(rproc, carveout);
971400e64dfSOhad Ben-Cohen 
972400e64dfSOhad Ben-Cohen 	return 0;
973400e64dfSOhad Ben-Cohen }
974400e64dfSOhad Ben-Cohen 
97572029c90SLoic Pallardy /**
97615c0b025SLoic Pallardy  * rproc_add_carveout() - register an allocated carveout region
97715c0b025SLoic Pallardy  * @rproc: rproc handle
97815c0b025SLoic Pallardy  * @mem: memory entry to register
97915c0b025SLoic Pallardy  *
98015c0b025SLoic Pallardy  * This function registers specified memory entry in @rproc carveouts list.
98115c0b025SLoic Pallardy  * Specified carveout should have been allocated before registering.
98215c0b025SLoic Pallardy  */
98315c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
98415c0b025SLoic Pallardy {
98515c0b025SLoic Pallardy 	list_add_tail(&mem->node, &rproc->carveouts);
98615c0b025SLoic Pallardy }
98715c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout);
98815c0b025SLoic Pallardy 
98915c0b025SLoic Pallardy /**
99072029c90SLoic Pallardy  * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
99172029c90SLoic Pallardy  * @dev: pointer on device struct
99272029c90SLoic Pallardy  * @va: virtual address
99372029c90SLoic Pallardy  * @dma: dma address
99472029c90SLoic Pallardy  * @len: memory carveout length
99572029c90SLoic Pallardy  * @da: device address
996a9f6fe0dSLoic Pallardy  * @alloc: memory carveout allocation function
997a9f6fe0dSLoic Pallardy  * @release: memory carveout release function
99872029c90SLoic Pallardy  * @name: carveout name
99972029c90SLoic Pallardy  *
100072029c90SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
100172029c90SLoic Pallardy  * provided by client.
100272029c90SLoic Pallardy  */
10037e05c8deSLee Jones __printf(8, 9)
100472029c90SLoic Pallardy struct rproc_mem_entry *
100572029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev,
1006096ee786SClement Leger 		     void *va, dma_addr_t dma, size_t len, u32 da,
1007d7c51706SLoic Pallardy 		     int (*alloc)(struct rproc *, struct rproc_mem_entry *),
100872029c90SLoic Pallardy 		     int (*release)(struct rproc *, struct rproc_mem_entry *),
100972029c90SLoic Pallardy 		     const char *name, ...)
101072029c90SLoic Pallardy {
101172029c90SLoic Pallardy 	struct rproc_mem_entry *mem;
101272029c90SLoic Pallardy 	va_list args;
101372029c90SLoic Pallardy 
101472029c90SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
101572029c90SLoic Pallardy 	if (!mem)
101672029c90SLoic Pallardy 		return mem;
101772029c90SLoic Pallardy 
101872029c90SLoic Pallardy 	mem->va = va;
101972029c90SLoic Pallardy 	mem->dma = dma;
102072029c90SLoic Pallardy 	mem->da = da;
102172029c90SLoic Pallardy 	mem->len = len;
1022d7c51706SLoic Pallardy 	mem->alloc = alloc;
102372029c90SLoic Pallardy 	mem->release = release;
1024d7c51706SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
10251429cca1SLoic Pallardy 	mem->of_resm_idx = -1;
102672029c90SLoic Pallardy 
102772029c90SLoic Pallardy 	va_start(args, name);
102872029c90SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
102972029c90SLoic Pallardy 	va_end(args);
103072029c90SLoic Pallardy 
103172029c90SLoic Pallardy 	return mem;
103272029c90SLoic Pallardy }
103372029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init);
103472029c90SLoic Pallardy 
103572029c90SLoic Pallardy /**
10361429cca1SLoic Pallardy  * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
10371429cca1SLoic Pallardy  * from a reserved memory phandle
10381429cca1SLoic Pallardy  * @dev: pointer on device struct
10391429cca1SLoic Pallardy  * @of_resm_idx: reserved memory phandle index in "memory-region"
10401429cca1SLoic Pallardy  * @len: memory carveout length
10411429cca1SLoic Pallardy  * @da: device address
10421429cca1SLoic Pallardy  * @name: carveout name
10431429cca1SLoic Pallardy  *
10441429cca1SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
10451429cca1SLoic Pallardy  * provided by client.
10461429cca1SLoic Pallardy  */
10477e05c8deSLee Jones __printf(5, 6)
10481429cca1SLoic Pallardy struct rproc_mem_entry *
1049096ee786SClement Leger rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, size_t len,
10501429cca1SLoic Pallardy 			     u32 da, const char *name, ...)
10511429cca1SLoic Pallardy {
10521429cca1SLoic Pallardy 	struct rproc_mem_entry *mem;
10531429cca1SLoic Pallardy 	va_list args;
10541429cca1SLoic Pallardy 
10551429cca1SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
10561429cca1SLoic Pallardy 	if (!mem)
10571429cca1SLoic Pallardy 		return mem;
10581429cca1SLoic Pallardy 
10591429cca1SLoic Pallardy 	mem->da = da;
10601429cca1SLoic Pallardy 	mem->len = len;
10611429cca1SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
10621429cca1SLoic Pallardy 	mem->of_resm_idx = of_resm_idx;
10631429cca1SLoic Pallardy 
10641429cca1SLoic Pallardy 	va_start(args, name);
10651429cca1SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
10661429cca1SLoic Pallardy 	va_end(args);
10671429cca1SLoic Pallardy 
10681429cca1SLoic Pallardy 	return mem;
10691429cca1SLoic Pallardy }
10701429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init);
10711429cca1SLoic Pallardy 
1072a8aa5ee1SSuman Anna /**
1073a8aa5ee1SSuman Anna  * rproc_of_parse_firmware() - parse and return the firmware-name
1074a8aa5ee1SSuman Anna  * @dev: pointer on device struct representing a rproc
1075a8aa5ee1SSuman Anna  * @index: index to use for the firmware-name retrieval
1076a8aa5ee1SSuman Anna  * @fw_name: pointer to a character string, in which the firmware
1077a8aa5ee1SSuman Anna  *           name is returned on success and unmodified otherwise.
1078a8aa5ee1SSuman Anna  *
1079a8aa5ee1SSuman Anna  * This is an OF helper function that parses a device's DT node for
1080a8aa5ee1SSuman Anna  * the "firmware-name" property and returns the firmware name pointer
1081a8aa5ee1SSuman Anna  * in @fw_name on success.
1082a8aa5ee1SSuman Anna  *
1083a8aa5ee1SSuman Anna  * Return: 0 on success, or an appropriate failure.
1084a8aa5ee1SSuman Anna  */
1085a8aa5ee1SSuman Anna int rproc_of_parse_firmware(struct device *dev, int index, const char **fw_name)
1086a8aa5ee1SSuman Anna {
1087a8aa5ee1SSuman Anna 	int ret;
1088a8aa5ee1SSuman Anna 
1089a8aa5ee1SSuman Anna 	ret = of_property_read_string_index(dev->of_node, "firmware-name",
1090a8aa5ee1SSuman Anna 					    index, fw_name);
1091a8aa5ee1SSuman Anna 	return ret ? ret : 0;
1092a8aa5ee1SSuman Anna }
1093a8aa5ee1SSuman Anna EXPORT_SYMBOL(rproc_of_parse_firmware);
1094a8aa5ee1SSuman Anna 
10952e7d4c2cSArnaud Pouliquen /*
1096e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
1097e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
1098e12bc14bSOhad Ben-Cohen  */
1099232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
1100fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
1101fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
1102fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
1103232fcdbbSSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
1104232fcdbbSSjur Brændeland };
1105232fcdbbSSjur Brændeland 
1106400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
1107a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc,
1108232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
1109400e64dfSOhad Ben-Cohen {
1110b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1111e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
1112fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
1113400e64dfSOhad Ben-Cohen 
1114d4bb86f2SBjorn Andersson 	if (!rproc->table_ptr)
1115d4bb86f2SBjorn Andersson 		return 0;
1116d4bb86f2SBjorn Andersson 
1117a2b950acSOhad Ben-Cohen 	for (i = 0; i < rproc->table_ptr->num; i++) {
1118a2b950acSOhad Ben-Cohen 		int offset = rproc->table_ptr->offset[i];
1119a2b950acSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
1120a4b24c75SBjorn Andersson 		int avail = rproc->table_sz - offset - sizeof(*hdr);
1121fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
1122400e64dfSOhad Ben-Cohen 
1123fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
1124fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
1125fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
1126fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
1127fd2c15ecSOhad Ben-Cohen 		}
1128fd2c15ecSOhad Ben-Cohen 
1129fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
1130fd2c15ecSOhad Ben-Cohen 
1131b1a17513SClement Leger 		if (hdr->type >= RSC_VENDOR_START &&
1132b1a17513SClement Leger 		    hdr->type <= RSC_VENDOR_END) {
1133b1a17513SClement Leger 			ret = rproc_handle_rsc(rproc, hdr->type, rsc,
1134b1a17513SClement Leger 					       offset + sizeof(*hdr), avail);
1135b1a17513SClement Leger 			if (ret == RSC_HANDLED)
1136b1a17513SClement Leger 				continue;
1137b1a17513SClement Leger 			else if (ret < 0)
1138b1a17513SClement Leger 				break;
1139b1a17513SClement Leger 
1140b1a17513SClement Leger 			dev_warn(dev, "unsupported vendor resource %d\n",
1141b1a17513SClement Leger 				 hdr->type);
1142b1a17513SClement Leger 			continue;
1143b1a17513SClement Leger 		}
1144b1a17513SClement Leger 
1145fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
1146fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
1147e12bc14bSOhad Ben-Cohen 			continue;
1148400e64dfSOhad Ben-Cohen 		}
1149400e64dfSOhad Ben-Cohen 
1150232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
1151e12bc14bSOhad Ben-Cohen 		if (!handler)
1152e12bc14bSOhad Ben-Cohen 			continue;
1153e12bc14bSOhad Ben-Cohen 
1154a2b950acSOhad Ben-Cohen 		ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
11557a186941SOhad Ben-Cohen 		if (ret)
1156400e64dfSOhad Ben-Cohen 			break;
1157400e64dfSOhad Ben-Cohen 	}
1158400e64dfSOhad Ben-Cohen 
1159400e64dfSOhad Ben-Cohen 	return ret;
1160400e64dfSOhad Ben-Cohen }
1161400e64dfSOhad Ben-Cohen 
1162c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc)
1163c455daa4SBjorn Andersson {
1164c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1165c455daa4SBjorn Andersson 	int ret;
1166c455daa4SBjorn Andersson 
1167c455daa4SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1168c455daa4SBjorn Andersson 		if (subdev->prepare) {
1169c455daa4SBjorn Andersson 			ret = subdev->prepare(subdev);
1170c455daa4SBjorn Andersson 			if (ret)
1171c455daa4SBjorn Andersson 				goto unroll_preparation;
1172c455daa4SBjorn Andersson 		}
1173c455daa4SBjorn Andersson 	}
1174c455daa4SBjorn Andersson 
1175c455daa4SBjorn Andersson 	return 0;
1176c455daa4SBjorn Andersson 
1177c455daa4SBjorn Andersson unroll_preparation:
1178c455daa4SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1179c455daa4SBjorn Andersson 		if (subdev->unprepare)
1180c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1181c455daa4SBjorn Andersson 	}
1182c455daa4SBjorn Andersson 
1183c455daa4SBjorn Andersson 	return ret;
1184c455daa4SBjorn Andersson }
1185c455daa4SBjorn Andersson 
1186618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc)
11877bdc9650SBjorn Andersson {
11887bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
11897bdc9650SBjorn Andersson 	int ret;
11907bdc9650SBjorn Andersson 
11917bdc9650SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1192be37b1e0SBjorn Andersson 		if (subdev->start) {
1193618fcff3SBjorn Andersson 			ret = subdev->start(subdev);
11947bdc9650SBjorn Andersson 			if (ret)
11957bdc9650SBjorn Andersson 				goto unroll_registration;
11967bdc9650SBjorn Andersson 		}
1197be37b1e0SBjorn Andersson 	}
11987bdc9650SBjorn Andersson 
11997bdc9650SBjorn Andersson 	return 0;
12007bdc9650SBjorn Andersson 
12017bdc9650SBjorn Andersson unroll_registration:
1202be37b1e0SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1203be37b1e0SBjorn Andersson 		if (subdev->stop)
1204618fcff3SBjorn Andersson 			subdev->stop(subdev, true);
1205be37b1e0SBjorn Andersson 	}
12067bdc9650SBjorn Andersson 
12077bdc9650SBjorn Andersson 	return ret;
12087bdc9650SBjorn Andersson }
12097bdc9650SBjorn Andersson 
1210618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
12117bdc9650SBjorn Andersson {
12127bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
12137bdc9650SBjorn Andersson 
1214be37b1e0SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1215be37b1e0SBjorn Andersson 		if (subdev->stop)
1216618fcff3SBjorn Andersson 			subdev->stop(subdev, crashed);
12177bdc9650SBjorn Andersson 	}
1218be37b1e0SBjorn Andersson }
12197bdc9650SBjorn Andersson 
1220c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc)
1221c455daa4SBjorn Andersson {
1222c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1223c455daa4SBjorn Andersson 
1224c455daa4SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1225c455daa4SBjorn Andersson 		if (subdev->unprepare)
1226c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1227c455daa4SBjorn Andersson 	}
1228c455daa4SBjorn Andersson }
1229c455daa4SBjorn Andersson 
1230400e64dfSOhad Ben-Cohen /**
1231d7c51706SLoic Pallardy  * rproc_alloc_registered_carveouts() - allocate all carveouts registered
1232d7c51706SLoic Pallardy  * in the list
1233d7c51706SLoic Pallardy  * @rproc: the remote processor handle
1234d7c51706SLoic Pallardy  *
1235d7c51706SLoic Pallardy  * This function parses registered carveout list, performs allocation
1236d7c51706SLoic Pallardy  * if alloc() ops registered and updates resource table information
1237d7c51706SLoic Pallardy  * if rsc_offset set.
1238d7c51706SLoic Pallardy  *
1239d7c51706SLoic Pallardy  * Return: 0 on success
1240d7c51706SLoic Pallardy  */
1241d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc)
1242d7c51706SLoic Pallardy {
1243d7c51706SLoic Pallardy 	struct rproc_mem_entry *entry, *tmp;
1244d7c51706SLoic Pallardy 	struct fw_rsc_carveout *rsc;
1245d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
1246b36de8cfSLoic Pallardy 	u64 pa;
1247d7c51706SLoic Pallardy 	int ret;
1248d7c51706SLoic Pallardy 
1249d7c51706SLoic Pallardy 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1250d7c51706SLoic Pallardy 		if (entry->alloc) {
1251d7c51706SLoic Pallardy 			ret = entry->alloc(rproc, entry);
1252d7c51706SLoic Pallardy 			if (ret) {
1253d7c51706SLoic Pallardy 				dev_err(dev, "Unable to allocate carveout %s: %d\n",
1254d7c51706SLoic Pallardy 					entry->name, ret);
1255d7c51706SLoic Pallardy 				return -ENOMEM;
1256d7c51706SLoic Pallardy 			}
1257d7c51706SLoic Pallardy 		}
1258d7c51706SLoic Pallardy 
1259d7c51706SLoic Pallardy 		if (entry->rsc_offset != FW_RSC_ADDR_ANY) {
1260d7c51706SLoic Pallardy 			/* update resource table */
1261d7c51706SLoic Pallardy 			rsc = (void *)rproc->table_ptr + entry->rsc_offset;
1262d7c51706SLoic Pallardy 
1263d7c51706SLoic Pallardy 			/*
1264d7c51706SLoic Pallardy 			 * Some remote processors might need to know the pa
1265d7c51706SLoic Pallardy 			 * even though they are behind an IOMMU. E.g., OMAP4's
1266d7c51706SLoic Pallardy 			 * remote M3 processor needs this so it can control
1267d7c51706SLoic Pallardy 			 * on-chip hardware accelerators that are not behind
1268d7c51706SLoic Pallardy 			 * the IOMMU, and therefor must know the pa.
1269d7c51706SLoic Pallardy 			 *
1270d7c51706SLoic Pallardy 			 * Generally we don't want to expose physical addresses
1271d7c51706SLoic Pallardy 			 * if we don't have to (remote processors are generally
1272d7c51706SLoic Pallardy 			 * _not_ trusted), so we might want to do this only for
1273d7c51706SLoic Pallardy 			 * remote processor that _must_ have this (e.g. OMAP4's
1274d7c51706SLoic Pallardy 			 * dual M3 subsystem).
1275d7c51706SLoic Pallardy 			 *
1276d7c51706SLoic Pallardy 			 * Non-IOMMU processors might also want to have this info.
1277d7c51706SLoic Pallardy 			 * In this case, the device address and the physical address
1278d7c51706SLoic Pallardy 			 * are the same.
1279d7c51706SLoic Pallardy 			 */
1280ffa5f9c8SLoic Pallardy 
1281ffa5f9c8SLoic Pallardy 			/* Use va if defined else dma to generate pa */
1282d7c51706SLoic Pallardy 			if (entry->va)
1283b36de8cfSLoic Pallardy 				pa = (u64)rproc_va_to_pa(entry->va);
1284ffa5f9c8SLoic Pallardy 			else
1285b36de8cfSLoic Pallardy 				pa = (u64)entry->dma;
1286ffa5f9c8SLoic Pallardy 
1287b36de8cfSLoic Pallardy 			if (((u64)pa) & HIGH_BITS_MASK)
1288b36de8cfSLoic Pallardy 				dev_warn(dev,
1289b36de8cfSLoic Pallardy 					 "Physical address cast in 32bit to fit resource table format\n");
1290b36de8cfSLoic Pallardy 
1291b36de8cfSLoic Pallardy 			rsc->pa = (u32)pa;
1292ffa5f9c8SLoic Pallardy 			rsc->da = entry->da;
1293ffa5f9c8SLoic Pallardy 			rsc->len = entry->len;
1294d7c51706SLoic Pallardy 		}
1295d7c51706SLoic Pallardy 	}
1296d7c51706SLoic Pallardy 
1297d7c51706SLoic Pallardy 	return 0;
1298d7c51706SLoic Pallardy }
1299d7c51706SLoic Pallardy 
13002666ca91SSarangdhar Joshi 
13012666ca91SSarangdhar Joshi /**
1302400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
1303400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
1304400e64dfSOhad Ben-Cohen  *
1305400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
13067a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
1307400e64dfSOhad Ben-Cohen  */
1308d9473cbfSMathieu Poirier void rproc_resource_cleanup(struct rproc *rproc)
1309400e64dfSOhad Ben-Cohen {
1310400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
1311a987e6b9SLoic Pallardy 	struct rproc_debug_trace *trace, *ttmp;
1312d81fb32fSBjorn Andersson 	struct rproc_vdev *rvdev, *rvtmp;
1313b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1314400e64dfSOhad Ben-Cohen 
1315400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
1316a987e6b9SLoic Pallardy 	list_for_each_entry_safe(trace, ttmp, &rproc->traces, node) {
1317a987e6b9SLoic Pallardy 		rproc_remove_trace_file(trace->tfile);
1318400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
1319a987e6b9SLoic Pallardy 		list_del(&trace->node);
1320a987e6b9SLoic Pallardy 		kfree(trace);
1321400e64dfSOhad Ben-Cohen 	}
1322400e64dfSOhad Ben-Cohen 
1323400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
1324400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
1325400e64dfSOhad Ben-Cohen 		size_t unmapped;
1326400e64dfSOhad Ben-Cohen 
1327400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
1328400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
1329400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
1330096ee786SClement Leger 			dev_err(dev, "failed to unmap %zx/%zu\n", entry->len,
1331400e64dfSOhad Ben-Cohen 				unmapped);
1332400e64dfSOhad Ben-Cohen 		}
1333400e64dfSOhad Ben-Cohen 
1334400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
1335400e64dfSOhad Ben-Cohen 		kfree(entry);
1336400e64dfSOhad Ben-Cohen 	}
1337b6356a01SSuman Anna 
1338b6356a01SSuman Anna 	/* clean up carveout allocations */
1339b6356a01SSuman Anna 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1340f2e74abfSLoic Pallardy 		if (entry->release)
1341f2e74abfSLoic Pallardy 			entry->release(rproc, entry);
1342b6356a01SSuman Anna 		list_del(&entry->node);
1343b6356a01SSuman Anna 		kfree(entry);
1344b6356a01SSuman Anna 	}
1345d81fb32fSBjorn Andersson 
1346d81fb32fSBjorn Andersson 	/* clean up remote vdev entries */
1347f5bcb353SBjorn Andersson 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
13482b45cef5SBjorn Andersson 		kref_put(&rvdev->refcount, rproc_vdev_release);
13492666ca91SSarangdhar Joshi 
13502666ca91SSarangdhar Joshi 	rproc_coredump_cleanup(rproc);
13512b45cef5SBjorn Andersson }
1352d9473cbfSMathieu Poirier EXPORT_SYMBOL(rproc_resource_cleanup);
1353400e64dfSOhad Ben-Cohen 
13541efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw)
13551efa30d0SSarangdhar Joshi {
1356a4b24c75SBjorn Andersson 	struct resource_table *loaded_table;
13571efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
1358a4b24c75SBjorn Andersson 	int ret;
13591efa30d0SSarangdhar Joshi 
13601efa30d0SSarangdhar Joshi 	/* load the ELF segments to memory */
13611efa30d0SSarangdhar Joshi 	ret = rproc_load_segments(rproc, fw);
13621efa30d0SSarangdhar Joshi 	if (ret) {
13631efa30d0SSarangdhar Joshi 		dev_err(dev, "Failed to load program segments: %d\n", ret);
13641efa30d0SSarangdhar Joshi 		return ret;
13651efa30d0SSarangdhar Joshi 	}
13661efa30d0SSarangdhar Joshi 
13671efa30d0SSarangdhar Joshi 	/*
13681efa30d0SSarangdhar Joshi 	 * The starting device has been given the rproc->cached_table as the
13691efa30d0SSarangdhar Joshi 	 * resource table. The address of the vring along with the other
13701efa30d0SSarangdhar Joshi 	 * allocated resources (carveouts etc) is stored in cached_table.
13711efa30d0SSarangdhar Joshi 	 * In order to pass this information to the remote device we must copy
13721efa30d0SSarangdhar Joshi 	 * this information to device memory. We also update the table_ptr so
13731efa30d0SSarangdhar Joshi 	 * that any subsequent changes will be applied to the loaded version.
13741efa30d0SSarangdhar Joshi 	 */
13751efa30d0SSarangdhar Joshi 	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
13761efa30d0SSarangdhar Joshi 	if (loaded_table) {
1377a4b24c75SBjorn Andersson 		memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
13781efa30d0SSarangdhar Joshi 		rproc->table_ptr = loaded_table;
13791efa30d0SSarangdhar Joshi 	}
13801efa30d0SSarangdhar Joshi 
1381c455daa4SBjorn Andersson 	ret = rproc_prepare_subdevices(rproc);
1382c455daa4SBjorn Andersson 	if (ret) {
1383c455daa4SBjorn Andersson 		dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1384c455daa4SBjorn Andersson 			rproc->name, ret);
1385f68d51bdSSuman Anna 		goto reset_table_ptr;
1386c455daa4SBjorn Andersson 	}
1387c455daa4SBjorn Andersson 
13881efa30d0SSarangdhar Joshi 	/* power up the remote processor */
13891efa30d0SSarangdhar Joshi 	ret = rproc->ops->start(rproc);
13901efa30d0SSarangdhar Joshi 	if (ret) {
13911efa30d0SSarangdhar Joshi 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1392c455daa4SBjorn Andersson 		goto unprepare_subdevices;
13931efa30d0SSarangdhar Joshi 	}
13941efa30d0SSarangdhar Joshi 
1395618fcff3SBjorn Andersson 	/* Start any subdevices for the remote processor */
1396618fcff3SBjorn Andersson 	ret = rproc_start_subdevices(rproc);
13971efa30d0SSarangdhar Joshi 	if (ret) {
13981efa30d0SSarangdhar Joshi 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
13991efa30d0SSarangdhar Joshi 			rproc->name, ret);
1400c455daa4SBjorn Andersson 		goto stop_rproc;
14011efa30d0SSarangdhar Joshi 	}
14021efa30d0SSarangdhar Joshi 
14031efa30d0SSarangdhar Joshi 	rproc->state = RPROC_RUNNING;
14041efa30d0SSarangdhar Joshi 
14051efa30d0SSarangdhar Joshi 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
14061efa30d0SSarangdhar Joshi 
14071efa30d0SSarangdhar Joshi 	return 0;
1408c455daa4SBjorn Andersson 
1409c455daa4SBjorn Andersson stop_rproc:
1410c455daa4SBjorn Andersson 	rproc->ops->stop(rproc);
1411c455daa4SBjorn Andersson unprepare_subdevices:
1412c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1413f68d51bdSSuman Anna reset_table_ptr:
1414f68d51bdSSuman Anna 	rproc->table_ptr = rproc->cached_table;
1415c455daa4SBjorn Andersson 
1416c455daa4SBjorn Andersson 	return ret;
14171efa30d0SSarangdhar Joshi }
14181efa30d0SSarangdhar Joshi 
1419fdf0e00eSMathieu Poirier static int rproc_attach(struct rproc *rproc)
1420d848a481SMathieu Poirier {
1421d848a481SMathieu Poirier 	struct device *dev = &rproc->dev;
1422d848a481SMathieu Poirier 	int ret;
1423d848a481SMathieu Poirier 
1424d848a481SMathieu Poirier 	ret = rproc_prepare_subdevices(rproc);
1425d848a481SMathieu Poirier 	if (ret) {
1426d848a481SMathieu Poirier 		dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1427d848a481SMathieu Poirier 			rproc->name, ret);
1428d848a481SMathieu Poirier 		goto out;
1429d848a481SMathieu Poirier 	}
1430d848a481SMathieu Poirier 
1431d848a481SMathieu Poirier 	/* Attach to the remote processor */
1432d848a481SMathieu Poirier 	ret = rproc_attach_device(rproc);
1433d848a481SMathieu Poirier 	if (ret) {
1434d848a481SMathieu Poirier 		dev_err(dev, "can't attach to rproc %s: %d\n",
1435d848a481SMathieu Poirier 			rproc->name, ret);
1436d848a481SMathieu Poirier 		goto unprepare_subdevices;
1437d848a481SMathieu Poirier 	}
1438d848a481SMathieu Poirier 
1439d848a481SMathieu Poirier 	/* Start any subdevices for the remote processor */
1440d848a481SMathieu Poirier 	ret = rproc_start_subdevices(rproc);
1441d848a481SMathieu Poirier 	if (ret) {
1442d848a481SMathieu Poirier 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
1443d848a481SMathieu Poirier 			rproc->name, ret);
1444d848a481SMathieu Poirier 		goto stop_rproc;
1445d848a481SMathieu Poirier 	}
1446d848a481SMathieu Poirier 
1447d848a481SMathieu Poirier 	rproc->state = RPROC_RUNNING;
1448d848a481SMathieu Poirier 
1449d848a481SMathieu Poirier 	dev_info(dev, "remote processor %s is now attached\n", rproc->name);
1450d848a481SMathieu Poirier 
1451d848a481SMathieu Poirier 	return 0;
1452d848a481SMathieu Poirier 
1453d848a481SMathieu Poirier stop_rproc:
1454d848a481SMathieu Poirier 	rproc->ops->stop(rproc);
1455d848a481SMathieu Poirier unprepare_subdevices:
1456d848a481SMathieu Poirier 	rproc_unprepare_subdevices(rproc);
1457d848a481SMathieu Poirier out:
1458d848a481SMathieu Poirier 	return ret;
1459d848a481SMathieu Poirier }
1460d848a481SMathieu Poirier 
1461400e64dfSOhad Ben-Cohen /*
1462400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
1463400e64dfSOhad Ben-Cohen  */
1464400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1465400e64dfSOhad Ben-Cohen {
1466b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1467400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
146858b64090SBjorn Andersson 	int ret;
1469400e64dfSOhad Ben-Cohen 
1470400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
1471400e64dfSOhad Ben-Cohen 	if (ret)
1472400e64dfSOhad Ben-Cohen 		return ret;
1473400e64dfSOhad Ben-Cohen 
1474e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
1475400e64dfSOhad Ben-Cohen 
1476400e64dfSOhad Ben-Cohen 	/*
1477400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1478400e64dfSOhad Ben-Cohen 	 * just a nop
1479400e64dfSOhad Ben-Cohen 	 */
1480400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
1481400e64dfSOhad Ben-Cohen 	if (ret) {
1482400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
148349cff125SMathieu Poirier 		return ret;
1484400e64dfSOhad Ben-Cohen 	}
1485400e64dfSOhad Ben-Cohen 
148633467ac3SLoic Pallardy 	/* Prepare rproc for firmware loading if needed */
148733467ac3SLoic Pallardy 	ret = rproc_prepare_device(rproc);
148833467ac3SLoic Pallardy 	if (ret) {
148933467ac3SLoic Pallardy 		dev_err(dev, "can't prepare rproc %s: %d\n", rproc->name, ret);
149033467ac3SLoic Pallardy 		goto disable_iommu;
149133467ac3SLoic Pallardy 	}
149233467ac3SLoic Pallardy 
14933e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
1494400e64dfSOhad Ben-Cohen 
1495c1d35c1aSBjorn Andersson 	/* Load resource table, core dump segment list etc from the firmware */
1496c1d35c1aSBjorn Andersson 	ret = rproc_parse_fw(rproc, fw);
149758b64090SBjorn Andersson 	if (ret)
149833467ac3SLoic Pallardy 		goto unprepare_rproc;
1499a0c10687SBjorn Andersson 
1500b35d7afcSBjorn Andersson 	/* reset max_notifyid */
1501b35d7afcSBjorn Andersson 	rproc->max_notifyid = -1;
1502b35d7afcSBjorn Andersson 
1503c6aed238SLoic Pallardy 	/* reset handled vdev */
1504c6aed238SLoic Pallardy 	rproc->nb_vdev = 0;
1505c6aed238SLoic Pallardy 
1506400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
1507a4b24c75SBjorn Andersson 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1508400e64dfSOhad Ben-Cohen 	if (ret) {
1509400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
1510229b85a6SBjorn Andersson 		goto clean_up_resources;
1511400e64dfSOhad Ben-Cohen 	}
1512400e64dfSOhad Ben-Cohen 
1513d7c51706SLoic Pallardy 	/* Allocate carveout resources associated to rproc */
1514d7c51706SLoic Pallardy 	ret = rproc_alloc_registered_carveouts(rproc);
1515d7c51706SLoic Pallardy 	if (ret) {
1516d7c51706SLoic Pallardy 		dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1517d7c51706SLoic Pallardy 			ret);
1518d7c51706SLoic Pallardy 		goto clean_up_resources;
1519d7c51706SLoic Pallardy 	}
1520d7c51706SLoic Pallardy 
15211efa30d0SSarangdhar Joshi 	ret = rproc_start(rproc, fw);
15221efa30d0SSarangdhar Joshi 	if (ret)
1523229b85a6SBjorn Andersson 		goto clean_up_resources;
1524400e64dfSOhad Ben-Cohen 
1525400e64dfSOhad Ben-Cohen 	return 0;
1526400e64dfSOhad Ben-Cohen 
1527229b85a6SBjorn Andersson clean_up_resources:
1528229b85a6SBjorn Andersson 	rproc_resource_cleanup(rproc);
1529a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1530a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1531988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
153233467ac3SLoic Pallardy unprepare_rproc:
153333467ac3SLoic Pallardy 	/* release HW resources if needed */
153433467ac3SLoic Pallardy 	rproc_unprepare_device(rproc);
153558b64090SBjorn Andersson disable_iommu:
1536400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1537400e64dfSOhad Ben-Cohen 	return ret;
1538400e64dfSOhad Ben-Cohen }
1539400e64dfSOhad Ben-Cohen 
1540400e64dfSOhad Ben-Cohen /*
1541fdf0e00eSMathieu Poirier  * Attach to remote processor - similar to rproc_fw_boot() but without
1542fdf0e00eSMathieu Poirier  * the steps that deal with the firmware image.
1543fdf0e00eSMathieu Poirier  */
15440f9dc562SMathieu Poirier static int rproc_actuate(struct rproc *rproc)
1545fdf0e00eSMathieu Poirier {
1546fdf0e00eSMathieu Poirier 	struct device *dev = &rproc->dev;
1547fdf0e00eSMathieu Poirier 	int ret;
1548fdf0e00eSMathieu Poirier 
1549fdf0e00eSMathieu Poirier 	/*
1550fdf0e00eSMathieu Poirier 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1551fdf0e00eSMathieu Poirier 	 * just a nop
1552fdf0e00eSMathieu Poirier 	 */
1553fdf0e00eSMathieu Poirier 	ret = rproc_enable_iommu(rproc);
1554fdf0e00eSMathieu Poirier 	if (ret) {
1555fdf0e00eSMathieu Poirier 		dev_err(dev, "can't enable iommu: %d\n", ret);
1556fdf0e00eSMathieu Poirier 		return ret;
1557fdf0e00eSMathieu Poirier 	}
1558fdf0e00eSMathieu Poirier 
1559fdf0e00eSMathieu Poirier 	/* reset max_notifyid */
1560fdf0e00eSMathieu Poirier 	rproc->max_notifyid = -1;
1561fdf0e00eSMathieu Poirier 
1562fdf0e00eSMathieu Poirier 	/* reset handled vdev */
1563fdf0e00eSMathieu Poirier 	rproc->nb_vdev = 0;
1564fdf0e00eSMathieu Poirier 
1565fdf0e00eSMathieu Poirier 	/*
1566fdf0e00eSMathieu Poirier 	 * Handle firmware resources required to attach to a remote processor.
1567fdf0e00eSMathieu Poirier 	 * Because we are attaching rather than booting the remote processor,
1568fdf0e00eSMathieu Poirier 	 * we expect the platform driver to properly set rproc->table_ptr.
1569fdf0e00eSMathieu Poirier 	 */
1570fdf0e00eSMathieu Poirier 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1571fdf0e00eSMathieu Poirier 	if (ret) {
1572fdf0e00eSMathieu Poirier 		dev_err(dev, "Failed to process resources: %d\n", ret);
1573fdf0e00eSMathieu Poirier 		goto disable_iommu;
1574fdf0e00eSMathieu Poirier 	}
1575fdf0e00eSMathieu Poirier 
1576fdf0e00eSMathieu Poirier 	/* Allocate carveout resources associated to rproc */
1577fdf0e00eSMathieu Poirier 	ret = rproc_alloc_registered_carveouts(rproc);
1578fdf0e00eSMathieu Poirier 	if (ret) {
1579fdf0e00eSMathieu Poirier 		dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1580fdf0e00eSMathieu Poirier 			ret);
1581fdf0e00eSMathieu Poirier 		goto clean_up_resources;
1582fdf0e00eSMathieu Poirier 	}
1583fdf0e00eSMathieu Poirier 
1584fdf0e00eSMathieu Poirier 	ret = rproc_attach(rproc);
1585fdf0e00eSMathieu Poirier 	if (ret)
1586fdf0e00eSMathieu Poirier 		goto clean_up_resources;
1587fdf0e00eSMathieu Poirier 
1588fdf0e00eSMathieu Poirier 	return 0;
1589fdf0e00eSMathieu Poirier 
1590fdf0e00eSMathieu Poirier clean_up_resources:
1591fdf0e00eSMathieu Poirier 	rproc_resource_cleanup(rproc);
1592fdf0e00eSMathieu Poirier disable_iommu:
1593fdf0e00eSMathieu Poirier 	rproc_disable_iommu(rproc);
1594fdf0e00eSMathieu Poirier 	return ret;
1595fdf0e00eSMathieu Poirier }
1596fdf0e00eSMathieu Poirier 
1597fdf0e00eSMathieu Poirier /*
15985e6533f7SSarangdhar Joshi  * take a firmware and boot it up.
1599400e64dfSOhad Ben-Cohen  *
1600400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
1601400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
1602400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
1603400e64dfSOhad Ben-Cohen  * that might be cleaner).
1604400e64dfSOhad Ben-Cohen  */
16055e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
1606400e64dfSOhad Ben-Cohen {
1607400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
1608a2b950acSOhad Ben-Cohen 
16092bfc311aSBjorn Andersson 	rproc_boot(rproc);
1610ddf71187SBjorn Andersson 
1611400e64dfSOhad Ben-Cohen 	release_firmware(fw);
1612400e64dfSOhad Ben-Cohen }
1613400e64dfSOhad Ben-Cohen 
16145e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc)
161570b85ef8SFernando Guzman Lugo {
161670b85ef8SFernando Guzman Lugo 	int ret;
161770b85ef8SFernando Guzman Lugo 
161870b85ef8SFernando Guzman Lugo 	/*
1619e3d21939SMathieu Poirier 	 * Since the remote processor is in a detached state, it has already
1620e3d21939SMathieu Poirier 	 * been booted by another entity.  As such there is no point in waiting
1621e3d21939SMathieu Poirier 	 * for a firmware image to be loaded, we can simply initiate the process
1622e3d21939SMathieu Poirier 	 * of attaching to it immediately.
1623e3d21939SMathieu Poirier 	 */
1624e3d21939SMathieu Poirier 	if (rproc->state == RPROC_DETACHED)
1625e3d21939SMathieu Poirier 		return rproc_boot(rproc);
1626e3d21939SMathieu Poirier 
1627e3d21939SMathieu Poirier 	/*
162870b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
162970b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
163070b85ef8SFernando Guzman Lugo 	 */
163170b85ef8SFernando Guzman Lugo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
163270b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
16335e6533f7SSarangdhar Joshi 				      rproc, rproc_auto_boot_callback);
16342099c77dSSarangdhar Joshi 	if (ret < 0)
163570b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
163670b85ef8SFernando Guzman Lugo 
163770b85ef8SFernando Guzman Lugo 	return ret;
163870b85ef8SFernando Guzman Lugo }
163970b85ef8SFernando Guzman Lugo 
1640880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed)
16411efa30d0SSarangdhar Joshi {
16421efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
16431efa30d0SSarangdhar Joshi 	int ret;
16441efa30d0SSarangdhar Joshi 
1645618fcff3SBjorn Andersson 	/* Stop any subdevices for the remote processor */
1646618fcff3SBjorn Andersson 	rproc_stop_subdevices(rproc, crashed);
16471efa30d0SSarangdhar Joshi 
16480a8b81cbSBjorn Andersson 	/* the installed resource table is no longer accessible */
16490a8b81cbSBjorn Andersson 	rproc->table_ptr = rproc->cached_table;
16500a8b81cbSBjorn Andersson 
16511efa30d0SSarangdhar Joshi 	/* power off the remote processor */
16521efa30d0SSarangdhar Joshi 	ret = rproc->ops->stop(rproc);
16531efa30d0SSarangdhar Joshi 	if (ret) {
16541efa30d0SSarangdhar Joshi 		dev_err(dev, "can't stop rproc: %d\n", ret);
16551efa30d0SSarangdhar Joshi 		return ret;
16561efa30d0SSarangdhar Joshi 	}
16571efa30d0SSarangdhar Joshi 
1658c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1659c455daa4SBjorn Andersson 
16601efa30d0SSarangdhar Joshi 	rproc->state = RPROC_OFFLINE;
16611efa30d0SSarangdhar Joshi 
16624a4dca19SMathieu Poirier 	/*
16634a4dca19SMathieu Poirier 	 * The remote processor has been stopped and is now offline, which means
16644a4dca19SMathieu Poirier 	 * that the next time it is brought back online the remoteproc core will
16654a4dca19SMathieu Poirier 	 * be responsible to load its firmware.  As such it is no longer
16664a4dca19SMathieu Poirier 	 * autonomous.
16674a4dca19SMathieu Poirier 	 */
16684a4dca19SMathieu Poirier 	rproc->autonomous = false;
16694a4dca19SMathieu Poirier 
16701efa30d0SSarangdhar Joshi 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
16711efa30d0SSarangdhar Joshi 
16721efa30d0SSarangdhar Joshi 	return 0;
16731efa30d0SSarangdhar Joshi }
16741efa30d0SSarangdhar Joshi 
16752666ca91SSarangdhar Joshi 
16762666ca91SSarangdhar Joshi /**
167770b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
167870b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
167970b85ef8SFernando Guzman Lugo  *
168056324d7aSAnna, Suman  * The recovery is done by resetting all the virtio devices, that way all the
168170b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
168270b85ef8SFernando Guzman Lugo  * remoteproc functional again.
168370b85ef8SFernando Guzman Lugo  *
168470b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
168570b85ef8SFernando Guzman Lugo  */
168670b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
168770b85ef8SFernando Guzman Lugo {
16887e83cab8SSarangdhar Joshi 	const struct firmware *firmware_p;
16897e83cab8SSarangdhar Joshi 	struct device *dev = &rproc->dev;
16907e83cab8SSarangdhar Joshi 	int ret;
16917e83cab8SSarangdhar Joshi 
16927e83cab8SSarangdhar Joshi 	ret = mutex_lock_interruptible(&rproc->lock);
16937e83cab8SSarangdhar Joshi 	if (ret)
16947e83cab8SSarangdhar Joshi 		return ret;
16957e83cab8SSarangdhar Joshi 
16960b145574SAlex Elder 	/* State could have changed before we got the mutex */
16970b145574SAlex Elder 	if (rproc->state != RPROC_CRASHED)
16980b145574SAlex Elder 		goto unlock_mutex;
16990b145574SAlex Elder 
17000b145574SAlex Elder 	dev_err(dev, "recovering %s\n", rproc->name);
17010b145574SAlex Elder 
1702fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, true);
17037e83cab8SSarangdhar Joshi 	if (ret)
17047e83cab8SSarangdhar Joshi 		goto unlock_mutex;
1705ddf71187SBjorn Andersson 
17062666ca91SSarangdhar Joshi 	/* generate coredump */
1707adf60a87SSiddharth Gupta 	rproc->ops->coredump(rproc);
17082666ca91SSarangdhar Joshi 
17097e83cab8SSarangdhar Joshi 	/* load firmware */
17107e83cab8SSarangdhar Joshi 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
17117e83cab8SSarangdhar Joshi 	if (ret < 0) {
17127e83cab8SSarangdhar Joshi 		dev_err(dev, "request_firmware failed: %d\n", ret);
17137e83cab8SSarangdhar Joshi 		goto unlock_mutex;
17147e83cab8SSarangdhar Joshi 	}
1715ddf71187SBjorn Andersson 
17167e83cab8SSarangdhar Joshi 	/* boot the remote processor up again */
17177e83cab8SSarangdhar Joshi 	ret = rproc_start(rproc, firmware_p);
17187e83cab8SSarangdhar Joshi 
17197e83cab8SSarangdhar Joshi 	release_firmware(firmware_p);
17207e83cab8SSarangdhar Joshi 
17217e83cab8SSarangdhar Joshi unlock_mutex:
17227e83cab8SSarangdhar Joshi 	mutex_unlock(&rproc->lock);
17237e83cab8SSarangdhar Joshi 	return ret;
172470b85ef8SFernando Guzman Lugo }
172570b85ef8SFernando Guzman Lugo 
1726400e64dfSOhad Ben-Cohen /**
17278afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
17282e7d4c2cSArnaud Pouliquen  * @work: work treating the crash
17298afd519cSFernando Guzman Lugo  *
17308afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
17318afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
17328afd519cSFernando Guzman Lugo  */
17338afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
17348afd519cSFernando Guzman Lugo {
17358afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
17368afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
17378afd519cSFernando Guzman Lugo 
17388afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
17398afd519cSFernando Guzman Lugo 
17408afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
17418afd519cSFernando Guzman Lugo 
17428afd519cSFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
17438afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
17448afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
17458afd519cSFernando Guzman Lugo 		return;
17468afd519cSFernando Guzman Lugo 	}
17478afd519cSFernando Guzman Lugo 
17488afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
17498afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
17508afd519cSFernando Guzman Lugo 		rproc->name);
17518afd519cSFernando Guzman Lugo 
17528afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
17538afd519cSFernando Guzman Lugo 
17542e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
175570b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
1756a781e5aaSRishabh Bhatnagar 
1757a781e5aaSRishabh Bhatnagar 	pm_relax(rproc->dev.parent);
17588afd519cSFernando Guzman Lugo }
17598afd519cSFernando Guzman Lugo 
17608afd519cSFernando Guzman Lugo /**
17611b0ef906SSuman Anna  * rproc_boot() - boot a remote processor
1762400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1763400e64dfSOhad Ben-Cohen  *
1764400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1765400e64dfSOhad Ben-Cohen  *
1766400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1767400e64dfSOhad Ben-Cohen  * returns (successfully).
1768400e64dfSOhad Ben-Cohen  *
1769400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1770400e64dfSOhad Ben-Cohen  */
17711b0ef906SSuman Anna int rproc_boot(struct rproc *rproc)
1772400e64dfSOhad Ben-Cohen {
1773400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1774400e64dfSOhad Ben-Cohen 	struct device *dev;
1775400e64dfSOhad Ben-Cohen 	int ret;
1776400e64dfSOhad Ben-Cohen 
1777400e64dfSOhad Ben-Cohen 	if (!rproc) {
1778400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1779400e64dfSOhad Ben-Cohen 		return -EINVAL;
1780400e64dfSOhad Ben-Cohen 	}
1781400e64dfSOhad Ben-Cohen 
1782b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1783400e64dfSOhad Ben-Cohen 
1784400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1785400e64dfSOhad Ben-Cohen 	if (ret) {
1786400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1787400e64dfSOhad Ben-Cohen 		return ret;
1788400e64dfSOhad Ben-Cohen 	}
1789400e64dfSOhad Ben-Cohen 
17902099c77dSSarangdhar Joshi 	if (rproc->state == RPROC_DELETED) {
17912099c77dSSarangdhar Joshi 		ret = -ENODEV;
17922099c77dSSarangdhar Joshi 		dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
17932099c77dSSarangdhar Joshi 		goto unlock_mutex;
17942099c77dSSarangdhar Joshi 	}
17952099c77dSSarangdhar Joshi 
17960f9dc562SMathieu Poirier 	/* skip the boot or attach process if rproc is already powered up */
1797400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1798400e64dfSOhad Ben-Cohen 		ret = 0;
1799400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1800400e64dfSOhad Ben-Cohen 	}
1801400e64dfSOhad Ben-Cohen 
18020f9dc562SMathieu Poirier 	if (rproc->state == RPROC_DETACHED) {
18030f9dc562SMathieu Poirier 		dev_info(dev, "attaching to %s\n", rproc->name);
18040f9dc562SMathieu Poirier 
18050f9dc562SMathieu Poirier 		ret = rproc_actuate(rproc);
18060f9dc562SMathieu Poirier 	} else {
1807400e64dfSOhad Ben-Cohen 		dev_info(dev, "powering up %s\n", rproc->name);
1808400e64dfSOhad Ben-Cohen 
1809400e64dfSOhad Ben-Cohen 		/* load firmware */
1810400e64dfSOhad Ben-Cohen 		ret = request_firmware(&firmware_p, rproc->firmware, dev);
1811400e64dfSOhad Ben-Cohen 		if (ret < 0) {
1812400e64dfSOhad Ben-Cohen 			dev_err(dev, "request_firmware failed: %d\n", ret);
1813400e64dfSOhad Ben-Cohen 			goto downref_rproc;
1814400e64dfSOhad Ben-Cohen 		}
1815400e64dfSOhad Ben-Cohen 
1816400e64dfSOhad Ben-Cohen 		ret = rproc_fw_boot(rproc, firmware_p);
1817400e64dfSOhad Ben-Cohen 
1818400e64dfSOhad Ben-Cohen 		release_firmware(firmware_p);
18190f9dc562SMathieu Poirier 	}
1820400e64dfSOhad Ben-Cohen 
1821400e64dfSOhad Ben-Cohen downref_rproc:
1822fbb6aacbSBjorn Andersson 	if (ret)
1823400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1824400e64dfSOhad Ben-Cohen unlock_mutex:
1825400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1826400e64dfSOhad Ben-Cohen 	return ret;
1827400e64dfSOhad Ben-Cohen }
1828400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1829400e64dfSOhad Ben-Cohen 
1830400e64dfSOhad Ben-Cohen /**
1831400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1832400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1833400e64dfSOhad Ben-Cohen  *
1834400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1835400e64dfSOhad Ben-Cohen  *
1836400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1837400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1838400e64dfSOhad Ben-Cohen  * without really powering off the device.
1839400e64dfSOhad Ben-Cohen  *
1840400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1841400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1842400e64dfSOhad Ben-Cohen  *
1843400e64dfSOhad Ben-Cohen  * Notes:
1844400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1845400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1846400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1847400e64dfSOhad Ben-Cohen  *   needed.
1848400e64dfSOhad Ben-Cohen  */
1849400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1850400e64dfSOhad Ben-Cohen {
1851b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1852400e64dfSOhad Ben-Cohen 	int ret;
1853400e64dfSOhad Ben-Cohen 
1854400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1855400e64dfSOhad Ben-Cohen 	if (ret) {
1856400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1857400e64dfSOhad Ben-Cohen 		return;
1858400e64dfSOhad Ben-Cohen 	}
1859400e64dfSOhad Ben-Cohen 
1860400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1861400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1862400e64dfSOhad Ben-Cohen 		goto out;
1863400e64dfSOhad Ben-Cohen 
1864fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, false);
1865400e64dfSOhad Ben-Cohen 	if (ret) {
1866400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1867400e64dfSOhad Ben-Cohen 		goto out;
1868400e64dfSOhad Ben-Cohen 	}
1869400e64dfSOhad Ben-Cohen 
1870400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1871400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1872400e64dfSOhad Ben-Cohen 
187333467ac3SLoic Pallardy 	/* release HW resources if needed */
187433467ac3SLoic Pallardy 	rproc_unprepare_device(rproc);
187533467ac3SLoic Pallardy 
1876400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1877400e64dfSOhad Ben-Cohen 
1878988d204cSBjorn Andersson 	/* Free the copy of the resource table */
1879a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1880a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1881988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
1882400e64dfSOhad Ben-Cohen out:
1883400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1884400e64dfSOhad Ben-Cohen }
1885400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1886400e64dfSOhad Ben-Cohen 
1887400e64dfSOhad Ben-Cohen /**
1888fec47d86SDave Gerlach  * rproc_get_by_phandle() - find a remote processor by phandle
1889fec47d86SDave Gerlach  * @phandle: phandle to the rproc
1890fec47d86SDave Gerlach  *
1891fec47d86SDave Gerlach  * Finds an rproc handle using the remote processor's phandle, and then
1892fec47d86SDave Gerlach  * return a handle to the rproc.
1893fec47d86SDave Gerlach  *
1894fec47d86SDave Gerlach  * This function increments the remote processor's refcount, so always
1895fec47d86SDave Gerlach  * use rproc_put() to decrement it back once rproc isn't needed anymore.
1896fec47d86SDave Gerlach  *
1897fec47d86SDave Gerlach  * Returns the rproc handle on success, and NULL on failure.
1898fec47d86SDave Gerlach  */
18998de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF
1900fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle)
1901fec47d86SDave Gerlach {
1902fec47d86SDave Gerlach 	struct rproc *rproc = NULL, *r;
1903fec47d86SDave Gerlach 	struct device_node *np;
1904fec47d86SDave Gerlach 
1905fec47d86SDave Gerlach 	np = of_find_node_by_phandle(phandle);
1906fec47d86SDave Gerlach 	if (!np)
1907fec47d86SDave Gerlach 		return NULL;
1908fec47d86SDave Gerlach 
1909c0abe2caSBjorn Andersson 	rcu_read_lock();
1910c0abe2caSBjorn Andersson 	list_for_each_entry_rcu(r, &rproc_list, node) {
1911fec47d86SDave Gerlach 		if (r->dev.parent && r->dev.parent->of_node == np) {
1912fbb6aacbSBjorn Andersson 			/* prevent underlying implementation from being removed */
1913fbb6aacbSBjorn Andersson 			if (!try_module_get(r->dev.parent->driver->owner)) {
1914fbb6aacbSBjorn Andersson 				dev_err(&r->dev, "can't get owner\n");
1915fbb6aacbSBjorn Andersson 				break;
1916fbb6aacbSBjorn Andersson 			}
1917fbb6aacbSBjorn Andersson 
1918fec47d86SDave Gerlach 			rproc = r;
1919fec47d86SDave Gerlach 			get_device(&rproc->dev);
1920fec47d86SDave Gerlach 			break;
1921fec47d86SDave Gerlach 		}
1922fec47d86SDave Gerlach 	}
1923c0abe2caSBjorn Andersson 	rcu_read_unlock();
1924fec47d86SDave Gerlach 
1925fec47d86SDave Gerlach 	of_node_put(np);
1926fec47d86SDave Gerlach 
1927fec47d86SDave Gerlach 	return rproc;
1928fec47d86SDave Gerlach }
19298de3dbd0SOhad Ben-Cohen #else
19308de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle)
19318de3dbd0SOhad Ben-Cohen {
19328de3dbd0SOhad Ben-Cohen 	return NULL;
19338de3dbd0SOhad Ben-Cohen }
19348de3dbd0SOhad Ben-Cohen #endif
1935fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle);
1936fec47d86SDave Gerlach 
19374c1ad562SSuman Anna /**
19384c1ad562SSuman Anna  * rproc_set_firmware() - assign a new firmware
19394c1ad562SSuman Anna  * @rproc: rproc handle to which the new firmware is being assigned
19404c1ad562SSuman Anna  * @fw_name: new firmware name to be assigned
19414c1ad562SSuman Anna  *
19424c1ad562SSuman Anna  * This function allows remoteproc drivers or clients to configure a custom
19434c1ad562SSuman Anna  * firmware name that is different from the default name used during remoteproc
19444c1ad562SSuman Anna  * registration. The function does not trigger a remote processor boot,
19454c1ad562SSuman Anna  * only sets the firmware name used for a subsequent boot. This function
19464c1ad562SSuman Anna  * should also be called only when the remote processor is offline.
19474c1ad562SSuman Anna  *
19484c1ad562SSuman Anna  * This allows either the userspace to configure a different name through
19494c1ad562SSuman Anna  * sysfs or a kernel-level remoteproc or a remoteproc client driver to set
19504c1ad562SSuman Anna  * a specific firmware when it is controlling the boot and shutdown of the
19514c1ad562SSuman Anna  * remote processor.
19524c1ad562SSuman Anna  *
19534c1ad562SSuman Anna  * Return: 0 on success or a negative value upon failure
19544c1ad562SSuman Anna  */
19554c1ad562SSuman Anna int rproc_set_firmware(struct rproc *rproc, const char *fw_name)
19564c1ad562SSuman Anna {
19574c1ad562SSuman Anna 	struct device *dev;
19584c1ad562SSuman Anna 	int ret, len;
19594c1ad562SSuman Anna 	char *p;
19604c1ad562SSuman Anna 
19614c1ad562SSuman Anna 	if (!rproc || !fw_name)
19624c1ad562SSuman Anna 		return -EINVAL;
19634c1ad562SSuman Anna 
19644c1ad562SSuman Anna 	dev = rproc->dev.parent;
19654c1ad562SSuman Anna 
19664c1ad562SSuman Anna 	ret = mutex_lock_interruptible(&rproc->lock);
19674c1ad562SSuman Anna 	if (ret) {
19684c1ad562SSuman Anna 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
19694c1ad562SSuman Anna 		return -EINVAL;
19704c1ad562SSuman Anna 	}
19714c1ad562SSuman Anna 
19724c1ad562SSuman Anna 	if (rproc->state != RPROC_OFFLINE) {
19734c1ad562SSuman Anna 		dev_err(dev, "can't change firmware while running\n");
19744c1ad562SSuman Anna 		ret = -EBUSY;
19754c1ad562SSuman Anna 		goto out;
19764c1ad562SSuman Anna 	}
19774c1ad562SSuman Anna 
19784c1ad562SSuman Anna 	len = strcspn(fw_name, "\n");
19794c1ad562SSuman Anna 	if (!len) {
19804c1ad562SSuman Anna 		dev_err(dev, "can't provide empty string for firmware name\n");
19814c1ad562SSuman Anna 		ret = -EINVAL;
19824c1ad562SSuman Anna 		goto out;
19834c1ad562SSuman Anna 	}
19844c1ad562SSuman Anna 
19854c1ad562SSuman Anna 	p = kstrndup(fw_name, len, GFP_KERNEL);
19864c1ad562SSuman Anna 	if (!p) {
19874c1ad562SSuman Anna 		ret = -ENOMEM;
19884c1ad562SSuman Anna 		goto out;
19894c1ad562SSuman Anna 	}
19904c1ad562SSuman Anna 
1991*43d3f2c7SDaniele Alessandrelli 	kfree_const(rproc->firmware);
19924c1ad562SSuman Anna 	rproc->firmware = p;
19934c1ad562SSuman Anna 
19944c1ad562SSuman Anna out:
19954c1ad562SSuman Anna 	mutex_unlock(&rproc->lock);
19964c1ad562SSuman Anna 	return ret;
19974c1ad562SSuman Anna }
19984c1ad562SSuman Anna EXPORT_SYMBOL(rproc_set_firmware);
19994c1ad562SSuman Anna 
200088d3a136SMathieu Poirier static int rproc_validate(struct rproc *rproc)
200188d3a136SMathieu Poirier {
200288d3a136SMathieu Poirier 	switch (rproc->state) {
200388d3a136SMathieu Poirier 	case RPROC_OFFLINE:
200488d3a136SMathieu Poirier 		/*
200588d3a136SMathieu Poirier 		 * An offline processor without a start()
200688d3a136SMathieu Poirier 		 * function makes no sense.
200788d3a136SMathieu Poirier 		 */
200888d3a136SMathieu Poirier 		if (!rproc->ops->start)
200988d3a136SMathieu Poirier 			return -EINVAL;
201088d3a136SMathieu Poirier 		break;
201188d3a136SMathieu Poirier 	case RPROC_DETACHED:
201288d3a136SMathieu Poirier 		/*
201388d3a136SMathieu Poirier 		 * A remote processor in a detached state without an
201488d3a136SMathieu Poirier 		 * attach() function makes not sense.
201588d3a136SMathieu Poirier 		 */
201688d3a136SMathieu Poirier 		if (!rproc->ops->attach)
201788d3a136SMathieu Poirier 			return -EINVAL;
201888d3a136SMathieu Poirier 		/*
201988d3a136SMathieu Poirier 		 * When attaching to a remote processor the device memory
202088d3a136SMathieu Poirier 		 * is already available and as such there is no need to have a
202188d3a136SMathieu Poirier 		 * cached table.
202288d3a136SMathieu Poirier 		 */
202388d3a136SMathieu Poirier 		if (rproc->cached_table)
202488d3a136SMathieu Poirier 			return -EINVAL;
202588d3a136SMathieu Poirier 		break;
202688d3a136SMathieu Poirier 	default:
202788d3a136SMathieu Poirier 		/*
202888d3a136SMathieu Poirier 		 * When adding a remote processor, the state of the device
202988d3a136SMathieu Poirier 		 * can be offline or detached, nothing else.
203088d3a136SMathieu Poirier 		 */
203188d3a136SMathieu Poirier 		return -EINVAL;
203288d3a136SMathieu Poirier 	}
203388d3a136SMathieu Poirier 
203488d3a136SMathieu Poirier 	return 0;
203588d3a136SMathieu Poirier }
203688d3a136SMathieu Poirier 
2037fec47d86SDave Gerlach /**
2038160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
2039400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
2040400e64dfSOhad Ben-Cohen  *
2041400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
2042400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
2043400e64dfSOhad Ben-Cohen  *
2044400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
2045400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
2046400e64dfSOhad Ben-Cohen  *
2047400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
2048400e64dfSOhad Ben-Cohen  *
2049400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
2050400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
2051400e64dfSOhad Ben-Cohen  * firmware.
2052400e64dfSOhad Ben-Cohen  *
2053400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
20547a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
2055400e64dfSOhad Ben-Cohen  * probed.
2056400e64dfSOhad Ben-Cohen  */
2057160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
2058400e64dfSOhad Ben-Cohen {
2059b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
206070b85ef8SFernando Guzman Lugo 	int ret;
2061400e64dfSOhad Ben-Cohen 
2062b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
2063b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
2064b5ab5e24SOhad Ben-Cohen 		return ret;
2065400e64dfSOhad Ben-Cohen 
206688d3a136SMathieu Poirier 	ret = rproc_validate(rproc);
206788d3a136SMathieu Poirier 	if (ret < 0)
206888d3a136SMathieu Poirier 		return ret;
206988d3a136SMathieu Poirier 
2070b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
2071400e64dfSOhad Ben-Cohen 
2072400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
2073400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
20747a20c64dSSarangdhar Joshi 
207562b8f9e9SSiddharth Gupta 	/* add char device for this remoteproc */
207662b8f9e9SSiddharth Gupta 	ret = rproc_char_device_add(rproc);
207762b8f9e9SSiddharth Gupta 	if (ret < 0)
207862b8f9e9SSiddharth Gupta 		return ret;
207962b8f9e9SSiddharth Gupta 
20804a4dca19SMathieu Poirier 	/*
20814a4dca19SMathieu Poirier 	 * Remind ourselves the remote processor has been attached to rather
20824a4dca19SMathieu Poirier 	 * than booted by the remoteproc core.  This is important because the
20834a4dca19SMathieu Poirier 	 * RPROC_DETACHED state will be lost as soon as the remote processor
20844a4dca19SMathieu Poirier 	 * has been attached to.  Used in firmware_show() and reset in
20854a4dca19SMathieu Poirier 	 * rproc_stop().
20864a4dca19SMathieu Poirier 	 */
20874a4dca19SMathieu Poirier 	if (rproc->state == RPROC_DETACHED)
20884a4dca19SMathieu Poirier 		rproc->autonomous = true;
20894a4dca19SMathieu Poirier 
20907a20c64dSSarangdhar Joshi 	/* if rproc is marked always-on, request it to boot */
20917a20c64dSSarangdhar Joshi 	if (rproc->auto_boot) {
20925e6533f7SSarangdhar Joshi 		ret = rproc_trigger_auto_boot(rproc);
2093d2e12e66SDave Gerlach 		if (ret < 0)
2094d2e12e66SDave Gerlach 			return ret;
20957a20c64dSSarangdhar Joshi 	}
2096400e64dfSOhad Ben-Cohen 
2097d2e12e66SDave Gerlach 	/* expose to rproc_get_by_phandle users */
2098d2e12e66SDave Gerlach 	mutex_lock(&rproc_list_mutex);
2099c0abe2caSBjorn Andersson 	list_add_rcu(&rproc->node, &rproc_list);
2100d2e12e66SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
2101d2e12e66SDave Gerlach 
2102d2e12e66SDave Gerlach 	return 0;
2103400e64dfSOhad Ben-Cohen }
2104160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
2105400e64dfSOhad Ben-Cohen 
2106305ac5a7SPaul Cercueil static void devm_rproc_remove(void *rproc)
2107305ac5a7SPaul Cercueil {
2108305ac5a7SPaul Cercueil 	rproc_del(rproc);
2109305ac5a7SPaul Cercueil }
2110305ac5a7SPaul Cercueil 
2111305ac5a7SPaul Cercueil /**
2112305ac5a7SPaul Cercueil  * devm_rproc_add() - resource managed rproc_add()
2113305ac5a7SPaul Cercueil  * @dev: the underlying device
2114305ac5a7SPaul Cercueil  * @rproc: the remote processor handle to register
2115305ac5a7SPaul Cercueil  *
2116305ac5a7SPaul Cercueil  * This function performs like rproc_add() but the registered rproc device will
2117305ac5a7SPaul Cercueil  * automatically be removed on driver detach.
2118305ac5a7SPaul Cercueil  *
2119305ac5a7SPaul Cercueil  * Returns: 0 on success, negative errno on failure
2120305ac5a7SPaul Cercueil  */
2121305ac5a7SPaul Cercueil int devm_rproc_add(struct device *dev, struct rproc *rproc)
2122305ac5a7SPaul Cercueil {
2123305ac5a7SPaul Cercueil 	int err;
2124305ac5a7SPaul Cercueil 
2125305ac5a7SPaul Cercueil 	err = rproc_add(rproc);
2126305ac5a7SPaul Cercueil 	if (err)
2127305ac5a7SPaul Cercueil 		return err;
2128305ac5a7SPaul Cercueil 
2129305ac5a7SPaul Cercueil 	return devm_add_action_or_reset(dev, devm_rproc_remove, rproc);
2130305ac5a7SPaul Cercueil }
2131305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_add);
2132305ac5a7SPaul Cercueil 
2133400e64dfSOhad Ben-Cohen /**
2134b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
2135b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
2136b5ab5e24SOhad Ben-Cohen  *
2137b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
2138b5ab5e24SOhad Ben-Cohen  *
2139b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
2140b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
2141b5ab5e24SOhad Ben-Cohen  */
2142b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
2143b5ab5e24SOhad Ben-Cohen {
2144b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
2145b5ab5e24SOhad Ben-Cohen 
21467183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
21477183a2a7SOhad Ben-Cohen 
2148b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
2149b5ab5e24SOhad Ben-Cohen 
2150b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
2151b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
2152b5ab5e24SOhad Ben-Cohen 
21531487dedaSMathieu Poirier 	kfree_const(rproc->firmware);
2154db655278SSuman Anna 	kfree_const(rproc->name);
2155fb98e2bdSBjorn Andersson 	kfree(rproc->ops);
2156b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
2157b5ab5e24SOhad Ben-Cohen }
2158b5ab5e24SOhad Ben-Cohen 
2159c42ca04dSBhumika Goyal static const struct device_type rproc_type = {
2160b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
2161b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
2162b5ab5e24SOhad Ben-Cohen };
2163400e64dfSOhad Ben-Cohen 
21640c2ae2b1SMathieu Poirier static int rproc_alloc_firmware(struct rproc *rproc,
21650c2ae2b1SMathieu Poirier 				const char *name, const char *firmware)
21660c2ae2b1SMathieu Poirier {
21671487dedaSMathieu Poirier 	const char *p;
21680c2ae2b1SMathieu Poirier 
21690c2ae2b1SMathieu Poirier 	/*
21709d5f82c8SMathieu Poirier 	 * Allocate a firmware name if the caller gave us one to work
21719d5f82c8SMathieu Poirier 	 * with.  Otherwise construct a new one using a default pattern.
21720c2ae2b1SMathieu Poirier 	 */
21739d5f82c8SMathieu Poirier 	if (firmware)
21741487dedaSMathieu Poirier 		p = kstrdup_const(firmware, GFP_KERNEL);
21759d5f82c8SMathieu Poirier 	else
21769d5f82c8SMathieu Poirier 		p = kasprintf(GFP_KERNEL, "rproc-%s-fw", name);
21774df4f8beSMathieu Poirier 
21780c2ae2b1SMathieu Poirier 	if (!p)
21790c2ae2b1SMathieu Poirier 		return -ENOMEM;
21800c2ae2b1SMathieu Poirier 
21810c2ae2b1SMathieu Poirier 	rproc->firmware = p;
21820c2ae2b1SMathieu Poirier 
21830c2ae2b1SMathieu Poirier 	return 0;
21840c2ae2b1SMathieu Poirier }
21850c2ae2b1SMathieu Poirier 
2186bf860aa1SMathieu Poirier static int rproc_alloc_ops(struct rproc *rproc, const struct rproc_ops *ops)
2187bf860aa1SMathieu Poirier {
2188bf860aa1SMathieu Poirier 	rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
2189bf860aa1SMathieu Poirier 	if (!rproc->ops)
2190bf860aa1SMathieu Poirier 		return -ENOMEM;
2191bf860aa1SMathieu Poirier 
2192adf60a87SSiddharth Gupta 	/* Default to rproc_coredump if no coredump function is specified */
2193adf60a87SSiddharth Gupta 	if (!rproc->ops->coredump)
2194adf60a87SSiddharth Gupta 		rproc->ops->coredump = rproc_coredump;
2195adf60a87SSiddharth Gupta 
2196bf860aa1SMathieu Poirier 	if (rproc->ops->load)
2197bf860aa1SMathieu Poirier 		return 0;
2198bf860aa1SMathieu Poirier 
2199bf860aa1SMathieu Poirier 	/* Default to ELF loader if no load function is specified */
2200bf860aa1SMathieu Poirier 	rproc->ops->load = rproc_elf_load_segments;
2201bf860aa1SMathieu Poirier 	rproc->ops->parse_fw = rproc_elf_load_rsc_table;
2202bf860aa1SMathieu Poirier 	rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
2203e29ff72bSClement Leger 	rproc->ops->sanity_check = rproc_elf_sanity_check;
2204bf860aa1SMathieu Poirier 	rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
2205bf860aa1SMathieu Poirier 
2206bf860aa1SMathieu Poirier 	return 0;
2207bf860aa1SMathieu Poirier }
2208bf860aa1SMathieu Poirier 
2209400e64dfSOhad Ben-Cohen /**
2210400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
2211400e64dfSOhad Ben-Cohen  * @dev: the underlying device
2212400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
2213400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
22148b4aec9aSRobert Tivy  * @firmware: name of firmware file to load, can be NULL
2215400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
2216400e64dfSOhad Ben-Cohen  *
2217400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
22188b4aec9aSRobert Tivy  * it yet. if @firmware is NULL, a default name is used.
2219400e64dfSOhad Ben-Cohen  *
2220400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
2221400e64dfSOhad Ben-Cohen  * of the remote processor.
2222400e64dfSOhad Ben-Cohen  *
2223400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
2224160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
2225400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
2226400e64dfSOhad Ben-Cohen  *
2227400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
2228400e64dfSOhad Ben-Cohen  *
2229400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
2230433c0e04SBjorn Andersson  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
2231400e64dfSOhad Ben-Cohen  */
2232400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
2233400e64dfSOhad Ben-Cohen 			  const struct rproc_ops *ops,
2234400e64dfSOhad Ben-Cohen 			  const char *firmware, int len)
2235400e64dfSOhad Ben-Cohen {
2236400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
2237400e64dfSOhad Ben-Cohen 
2238400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
2239400e64dfSOhad Ben-Cohen 		return NULL;
2240400e64dfSOhad Ben-Cohen 
22410f57dc6aSMatt Redfearn 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
22420c2ae2b1SMathieu Poirier 	if (!rproc)
22430f57dc6aSMatt Redfearn 		return NULL;
22440c2ae2b1SMathieu Poirier 
2245400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
2246ddf71187SBjorn Andersson 	rproc->auto_boot = true;
2247418fd787SClement Leger 	rproc->elf_class = ELFCLASSNONE;
2248418fd787SClement Leger 	rproc->elf_machine = EM_NONE;
2249400e64dfSOhad Ben-Cohen 
2250b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
2251b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
2252b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
22532aefbef0SMatt Redfearn 	rproc->dev.class = &rproc_class;
22547c89717fSBjorn Andersson 	rproc->dev.driver_data = rproc;
22556442df49SAlex Elder 	idr_init(&rproc->notifyids);
2256b5ab5e24SOhad Ben-Cohen 
2257db655278SSuman Anna 	rproc->name = kstrdup_const(name, GFP_KERNEL);
2258db655278SSuman Anna 	if (!rproc->name)
2259db655278SSuman Anna 		goto put_device;
2260db655278SSuman Anna 
2261226f5db4SMathieu Poirier 	if (rproc_alloc_firmware(rproc, name, firmware))
2262226f5db4SMathieu Poirier 		goto put_device;
2263226f5db4SMathieu Poirier 
2264226f5db4SMathieu Poirier 	if (rproc_alloc_ops(rproc, ops))
2265226f5db4SMathieu Poirier 		goto put_device;
2266226f5db4SMathieu Poirier 
2267b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
2268b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
2269b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
2270b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
2271226f5db4SMathieu Poirier 		goto put_device;
2272b5ab5e24SOhad Ben-Cohen 	}
2273b5ab5e24SOhad Ben-Cohen 
2274b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
2275b5ab5e24SOhad Ben-Cohen 
2276400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
2277400e64dfSOhad Ben-Cohen 
2278400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
2279400e64dfSOhad Ben-Cohen 
2280400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
2281400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
2282400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
22837a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
22847bdc9650SBjorn Andersson 	INIT_LIST_HEAD(&rproc->subdevs);
22852666ca91SSarangdhar Joshi 	INIT_LIST_HEAD(&rproc->dump_segments);
2286400e64dfSOhad Ben-Cohen 
22878afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
22888afd519cSFernando Guzman Lugo 
2289400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
2290400e64dfSOhad Ben-Cohen 
2291400e64dfSOhad Ben-Cohen 	return rproc;
22920c2ae2b1SMathieu Poirier 
2293226f5db4SMathieu Poirier put_device:
2294226f5db4SMathieu Poirier 	put_device(&rproc->dev);
22950c2ae2b1SMathieu Poirier 	return NULL;
2296400e64dfSOhad Ben-Cohen }
2297400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
2298400e64dfSOhad Ben-Cohen 
2299400e64dfSOhad Ben-Cohen /**
2300433c0e04SBjorn Andersson  * rproc_free() - unroll rproc_alloc()
2301433c0e04SBjorn Andersson  * @rproc: the remote processor handle
2302433c0e04SBjorn Andersson  *
2303433c0e04SBjorn Andersson  * This function decrements the rproc dev refcount.
2304433c0e04SBjorn Andersson  *
2305433c0e04SBjorn Andersson  * If no one holds any reference to rproc anymore, then its refcount would
2306433c0e04SBjorn Andersson  * now drop to zero, and it would be freed.
2307433c0e04SBjorn Andersson  */
2308433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc)
2309433c0e04SBjorn Andersson {
2310433c0e04SBjorn Andersson 	put_device(&rproc->dev);
2311433c0e04SBjorn Andersson }
2312433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free);
2313433c0e04SBjorn Andersson 
2314433c0e04SBjorn Andersson /**
2315433c0e04SBjorn Andersson  * rproc_put() - release rproc reference
2316400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
2317400e64dfSOhad Ben-Cohen  *
2318c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
2319400e64dfSOhad Ben-Cohen  *
2320c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
2321c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
2322400e64dfSOhad Ben-Cohen  */
2323160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
2324400e64dfSOhad Ben-Cohen {
2325fbb6aacbSBjorn Andersson 	module_put(rproc->dev.parent->driver->owner);
2326b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
2327400e64dfSOhad Ben-Cohen }
2328160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
2329400e64dfSOhad Ben-Cohen 
2330400e64dfSOhad Ben-Cohen /**
2331160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
2332400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
2333400e64dfSOhad Ben-Cohen  *
2334400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
2335400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
2336160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
2337400e64dfSOhad Ben-Cohen  * has completed successfully.
2338400e64dfSOhad Ben-Cohen  *
2339160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
2340c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
2341433c0e04SBjorn Andersson  * one last refcount, one still needs to call rproc_free().
2342400e64dfSOhad Ben-Cohen  *
2343400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
2344400e64dfSOhad Ben-Cohen  */
2345160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
2346400e64dfSOhad Ben-Cohen {
2347400e64dfSOhad Ben-Cohen 	if (!rproc)
2348400e64dfSOhad Ben-Cohen 		return -EINVAL;
2349400e64dfSOhad Ben-Cohen 
2350ddf71187SBjorn Andersson 	/* if rproc is marked always-on, rproc_add() booted it */
2351ddf71187SBjorn Andersson 	/* TODO: make sure this works with rproc->power > 1 */
2352ddf71187SBjorn Andersson 	if (rproc->auto_boot)
2353ddf71187SBjorn Andersson 		rproc_shutdown(rproc);
2354ddf71187SBjorn Andersson 
23552099c77dSSarangdhar Joshi 	mutex_lock(&rproc->lock);
23562099c77dSSarangdhar Joshi 	rproc->state = RPROC_DELETED;
23572099c77dSSarangdhar Joshi 	mutex_unlock(&rproc->lock);
23582099c77dSSarangdhar Joshi 
2359b003d45bSSarangdhar Joshi 	rproc_delete_debug_dir(rproc);
236062b8f9e9SSiddharth Gupta 	rproc_char_device_remove(rproc);
2361b003d45bSSarangdhar Joshi 
2362fec47d86SDave Gerlach 	/* the rproc is downref'ed as soon as it's removed from the klist */
2363fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
2364c0abe2caSBjorn Andersson 	list_del_rcu(&rproc->node);
2365fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
2366fec47d86SDave Gerlach 
2367c0abe2caSBjorn Andersson 	/* Ensure that no readers of rproc_list are still active */
2368c0abe2caSBjorn Andersson 	synchronize_rcu();
2369c0abe2caSBjorn Andersson 
2370b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
2371400e64dfSOhad Ben-Cohen 
2372400e64dfSOhad Ben-Cohen 	return 0;
2373400e64dfSOhad Ben-Cohen }
2374160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
2375400e64dfSOhad Ben-Cohen 
2376305ac5a7SPaul Cercueil static void devm_rproc_free(struct device *dev, void *res)
2377305ac5a7SPaul Cercueil {
2378305ac5a7SPaul Cercueil 	rproc_free(*(struct rproc **)res);
2379305ac5a7SPaul Cercueil }
2380305ac5a7SPaul Cercueil 
2381305ac5a7SPaul Cercueil /**
2382305ac5a7SPaul Cercueil  * devm_rproc_alloc() - resource managed rproc_alloc()
2383305ac5a7SPaul Cercueil  * @dev: the underlying device
2384305ac5a7SPaul Cercueil  * @name: name of this remote processor
2385305ac5a7SPaul Cercueil  * @ops: platform-specific handlers (mainly start/stop)
2386305ac5a7SPaul Cercueil  * @firmware: name of firmware file to load, can be NULL
2387305ac5a7SPaul Cercueil  * @len: length of private data needed by the rproc driver (in bytes)
2388305ac5a7SPaul Cercueil  *
2389305ac5a7SPaul Cercueil  * This function performs like rproc_alloc() but the acquired rproc device will
2390305ac5a7SPaul Cercueil  * automatically be released on driver detach.
2391305ac5a7SPaul Cercueil  *
2392305ac5a7SPaul Cercueil  * Returns: new rproc instance, or NULL on failure
2393305ac5a7SPaul Cercueil  */
2394305ac5a7SPaul Cercueil struct rproc *devm_rproc_alloc(struct device *dev, const char *name,
2395305ac5a7SPaul Cercueil 			       const struct rproc_ops *ops,
2396305ac5a7SPaul Cercueil 			       const char *firmware, int len)
2397305ac5a7SPaul Cercueil {
2398305ac5a7SPaul Cercueil 	struct rproc **ptr, *rproc;
2399305ac5a7SPaul Cercueil 
2400305ac5a7SPaul Cercueil 	ptr = devres_alloc(devm_rproc_free, sizeof(*ptr), GFP_KERNEL);
2401305ac5a7SPaul Cercueil 	if (!ptr)
24027dcef398SDan Carpenter 		return NULL;
2403305ac5a7SPaul Cercueil 
2404305ac5a7SPaul Cercueil 	rproc = rproc_alloc(dev, name, ops, firmware, len);
2405305ac5a7SPaul Cercueil 	if (rproc) {
2406305ac5a7SPaul Cercueil 		*ptr = rproc;
2407305ac5a7SPaul Cercueil 		devres_add(dev, ptr);
2408305ac5a7SPaul Cercueil 	} else {
2409305ac5a7SPaul Cercueil 		devres_free(ptr);
2410305ac5a7SPaul Cercueil 	}
2411305ac5a7SPaul Cercueil 
2412305ac5a7SPaul Cercueil 	return rproc;
2413305ac5a7SPaul Cercueil }
2414305ac5a7SPaul Cercueil EXPORT_SYMBOL(devm_rproc_alloc);
2415305ac5a7SPaul Cercueil 
24168afd519cSFernando Guzman Lugo /**
24177bdc9650SBjorn Andersson  * rproc_add_subdev() - add a subdevice to a remoteproc
24187bdc9650SBjorn Andersson  * @rproc: rproc handle to add the subdevice to
24197bdc9650SBjorn Andersson  * @subdev: subdev handle to register
24204902676fSBjorn Andersson  *
24214902676fSBjorn Andersson  * Caller is responsible for populating optional subdevice function pointers.
24227bdc9650SBjorn Andersson  */
24234902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
24247bdc9650SBjorn Andersson {
24257bdc9650SBjorn Andersson 	list_add_tail(&subdev->node, &rproc->subdevs);
24267bdc9650SBjorn Andersson }
24277bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev);
24287bdc9650SBjorn Andersson 
24297bdc9650SBjorn Andersson /**
24307bdc9650SBjorn Andersson  * rproc_remove_subdev() - remove a subdevice from a remoteproc
24317bdc9650SBjorn Andersson  * @rproc: rproc handle to remove the subdevice from
24327bdc9650SBjorn Andersson  * @subdev: subdev handle, previously registered with rproc_add_subdev()
24337bdc9650SBjorn Andersson  */
24347bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
24357bdc9650SBjorn Andersson {
24367bdc9650SBjorn Andersson 	list_del(&subdev->node);
24377bdc9650SBjorn Andersson }
24387bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev);
24397bdc9650SBjorn Andersson 
24407bdc9650SBjorn Andersson /**
24417c89717fSBjorn Andersson  * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
24427c89717fSBjorn Andersson  * @dev:	child device to find ancestor of
24437c89717fSBjorn Andersson  *
24447c89717fSBjorn Andersson  * Returns the ancestor rproc instance, or NULL if not found.
24457c89717fSBjorn Andersson  */
24467c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev)
24477c89717fSBjorn Andersson {
24487c89717fSBjorn Andersson 	for (dev = dev->parent; dev; dev = dev->parent) {
24497c89717fSBjorn Andersson 		if (dev->type == &rproc_type)
24507c89717fSBjorn Andersson 			return dev->driver_data;
24517c89717fSBjorn Andersson 	}
24527c89717fSBjorn Andersson 
24537c89717fSBjorn Andersson 	return NULL;
24547c89717fSBjorn Andersson }
24557c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child);
24567c89717fSBjorn Andersson 
24577c89717fSBjorn Andersson /**
24588afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
24598afd519cSFernando Guzman Lugo  * @rproc: remote processor
24608afd519cSFernando Guzman Lugo  * @type: crash type
24618afd519cSFernando Guzman Lugo  *
24628afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
24638afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
24648afd519cSFernando Guzman Lugo  * non-remoteproc driver.
24658afd519cSFernando Guzman Lugo  *
24668afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
24678afd519cSFernando Guzman Lugo  */
24688afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
24698afd519cSFernando Guzman Lugo {
24708afd519cSFernando Guzman Lugo 	if (!rproc) {
24718afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
24728afd519cSFernando Guzman Lugo 		return;
24738afd519cSFernando Guzman Lugo 	}
24748afd519cSFernando Guzman Lugo 
2475a781e5aaSRishabh Bhatnagar 	/* Prevent suspend while the remoteproc is being recovered */
2476a781e5aaSRishabh Bhatnagar 	pm_stay_awake(rproc->dev.parent);
2477a781e5aaSRishabh Bhatnagar 
24788afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
24798afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
24808afd519cSFernando Guzman Lugo 
24818afd519cSFernando Guzman Lugo 	/* create a new task to handle the error */
24828afd519cSFernando Guzman Lugo 	schedule_work(&rproc->crash_handler);
24838afd519cSFernando Guzman Lugo }
24848afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
24858afd519cSFernando Guzman Lugo 
2486dc5192c4SBjorn Andersson static int rproc_panic_handler(struct notifier_block *nb, unsigned long event,
2487dc5192c4SBjorn Andersson 			       void *ptr)
2488dc5192c4SBjorn Andersson {
2489dc5192c4SBjorn Andersson 	unsigned int longest = 0;
2490dc5192c4SBjorn Andersson 	struct rproc *rproc;
2491dc5192c4SBjorn Andersson 	unsigned int d;
2492dc5192c4SBjorn Andersson 
2493dc5192c4SBjorn Andersson 	rcu_read_lock();
2494dc5192c4SBjorn Andersson 	list_for_each_entry_rcu(rproc, &rproc_list, node) {
2495dc5192c4SBjorn Andersson 		if (!rproc->ops->panic || rproc->state != RPROC_RUNNING)
2496dc5192c4SBjorn Andersson 			continue;
2497dc5192c4SBjorn Andersson 
2498dc5192c4SBjorn Andersson 		d = rproc->ops->panic(rproc);
2499dc5192c4SBjorn Andersson 		longest = max(longest, d);
2500dc5192c4SBjorn Andersson 	}
2501dc5192c4SBjorn Andersson 	rcu_read_unlock();
2502dc5192c4SBjorn Andersson 
2503dc5192c4SBjorn Andersson 	/*
2504dc5192c4SBjorn Andersson 	 * Delay for the longest requested duration before returning. This can
2505dc5192c4SBjorn Andersson 	 * be used by the remoteproc drivers to give the remote processor time
2506dc5192c4SBjorn Andersson 	 * to perform any requested operations (such as flush caches), when
2507dc5192c4SBjorn Andersson 	 * it's not possible to signal the Linux side due to the panic.
2508dc5192c4SBjorn Andersson 	 */
2509dc5192c4SBjorn Andersson 	mdelay(longest);
2510dc5192c4SBjorn Andersson 
2511dc5192c4SBjorn Andersson 	return NOTIFY_DONE;
2512dc5192c4SBjorn Andersson }
2513dc5192c4SBjorn Andersson 
2514dc5192c4SBjorn Andersson static void __init rproc_init_panic(void)
2515dc5192c4SBjorn Andersson {
2516dc5192c4SBjorn Andersson 	rproc_panic_nb.notifier_call = rproc_panic_handler;
2517dc5192c4SBjorn Andersson 	atomic_notifier_chain_register(&panic_notifier_list, &rproc_panic_nb);
2518dc5192c4SBjorn Andersson }
2519dc5192c4SBjorn Andersson 
2520dc5192c4SBjorn Andersson static void __exit rproc_exit_panic(void)
2521dc5192c4SBjorn Andersson {
2522dc5192c4SBjorn Andersson 	atomic_notifier_chain_unregister(&panic_notifier_list, &rproc_panic_nb);
2523dc5192c4SBjorn Andersson }
2524dc5192c4SBjorn Andersson 
2525400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
2526400e64dfSOhad Ben-Cohen {
25272aefbef0SMatt Redfearn 	rproc_init_sysfs();
2528400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
252962b8f9e9SSiddharth Gupta 	rproc_init_cdev();
2530dc5192c4SBjorn Andersson 	rproc_init_panic();
2531b5ab5e24SOhad Ben-Cohen 
2532400e64dfSOhad Ben-Cohen 	return 0;
2533400e64dfSOhad Ben-Cohen }
2534a8f40111SBrandon Maier subsys_initcall(remoteproc_init);
2535400e64dfSOhad Ben-Cohen 
2536400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
2537400e64dfSOhad Ben-Cohen {
2538f42f79afSSuman Anna 	ida_destroy(&rproc_dev_index);
2539f42f79afSSuman Anna 
2540dc5192c4SBjorn Andersson 	rproc_exit_panic();
2541400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
25422aefbef0SMatt Redfearn 	rproc_exit_sysfs();
2543400e64dfSOhad Ben-Cohen }
2544400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
2545400e64dfSOhad Ben-Cohen 
2546400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
2547400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
2548