xref: /linux/drivers/remoteproc/remoteproc_core.c (revision ffa5f9c84810ea175226863f66a51bb2d4502f5b)
1400e64dfSOhad Ben-Cohen /*
2400e64dfSOhad Ben-Cohen  * Remote Processor Framework
3400e64dfSOhad Ben-Cohen  *
4400e64dfSOhad Ben-Cohen  * Copyright (C) 2011 Texas Instruments, Inc.
5400e64dfSOhad Ben-Cohen  * Copyright (C) 2011 Google, Inc.
6400e64dfSOhad Ben-Cohen  *
7400e64dfSOhad Ben-Cohen  * Ohad Ben-Cohen <ohad@wizery.com>
8400e64dfSOhad Ben-Cohen  * Brian Swetland <swetland@google.com>
9400e64dfSOhad Ben-Cohen  * Mark Grosen <mgrosen@ti.com>
10400e64dfSOhad Ben-Cohen  * Fernando Guzman Lugo <fernando.lugo@ti.com>
11400e64dfSOhad Ben-Cohen  * Suman Anna <s-anna@ti.com>
12400e64dfSOhad Ben-Cohen  * Robert Tivy <rtivy@ti.com>
13400e64dfSOhad Ben-Cohen  * Armando Uribe De Leon <x0095078@ti.com>
14400e64dfSOhad Ben-Cohen  *
15400e64dfSOhad Ben-Cohen  * This program is free software; you can redistribute it and/or
16400e64dfSOhad Ben-Cohen  * modify it under the terms of the GNU General Public License
17400e64dfSOhad Ben-Cohen  * version 2 as published by the Free Software Foundation.
18400e64dfSOhad Ben-Cohen  *
19400e64dfSOhad Ben-Cohen  * This program is distributed in the hope that it will be useful,
20400e64dfSOhad Ben-Cohen  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21400e64dfSOhad Ben-Cohen  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22400e64dfSOhad Ben-Cohen  * GNU General Public License for more details.
23400e64dfSOhad Ben-Cohen  */
24400e64dfSOhad Ben-Cohen 
25400e64dfSOhad Ben-Cohen #define pr_fmt(fmt)    "%s: " fmt, __func__
26400e64dfSOhad Ben-Cohen 
27400e64dfSOhad Ben-Cohen #include <linux/kernel.h>
28400e64dfSOhad Ben-Cohen #include <linux/module.h>
29400e64dfSOhad Ben-Cohen #include <linux/device.h>
30400e64dfSOhad Ben-Cohen #include <linux/slab.h>
31400e64dfSOhad Ben-Cohen #include <linux/mutex.h>
32400e64dfSOhad Ben-Cohen #include <linux/dma-mapping.h>
33400e64dfSOhad Ben-Cohen #include <linux/firmware.h>
34400e64dfSOhad Ben-Cohen #include <linux/string.h>
35400e64dfSOhad Ben-Cohen #include <linux/debugfs.h>
362666ca91SSarangdhar Joshi #include <linux/devcoredump.h>
37400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h>
38400e64dfSOhad Ben-Cohen #include <linux/iommu.h>
39b5ab5e24SOhad Ben-Cohen #include <linux/idr.h>
40400e64dfSOhad Ben-Cohen #include <linux/elf.h>
41a2b950acSOhad Ben-Cohen #include <linux/crc32.h>
42400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h>
43400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h>
44cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h>
45400e64dfSOhad Ben-Cohen 
46400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h"
47400e64dfSOhad Ben-Cohen 
48fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex);
49fec47d86SDave Gerlach static LIST_HEAD(rproc_list);
50fec47d86SDave Gerlach 
51400e64dfSOhad Ben-Cohen typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
52fd2c15ecSOhad Ben-Cohen 				struct resource_table *table, int len);
53a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
54a2b950acSOhad Ben-Cohen 				 void *, int offset, int avail);
55400e64dfSOhad Ben-Cohen 
56b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */
57b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index);
58b5ab5e24SOhad Ben-Cohen 
598afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = {
608afd519cSFernando Guzman Lugo 	[RPROC_MMUFAULT]	= "mmufault",
61b3d39032SBjorn Andersson 	[RPROC_WATCHDOG]	= "watchdog",
62b3d39032SBjorn Andersson 	[RPROC_FATAL_ERROR]	= "fatal error",
638afd519cSFernando Guzman Lugo };
648afd519cSFernando Guzman Lugo 
658afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */
668afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type)
678afd519cSFernando Guzman Lugo {
688afd519cSFernando Guzman Lugo 	if (type < ARRAY_SIZE(rproc_crash_names))
698afd519cSFernando Guzman Lugo 		return rproc_crash_names[type];
70b23f7a09SMasanari Iida 	return "unknown";
718afd519cSFernando Guzman Lugo }
728afd519cSFernando Guzman Lugo 
73400e64dfSOhad Ben-Cohen /*
74400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
75400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
76400e64dfSOhad Ben-Cohen  * an IOMMU).
77400e64dfSOhad Ben-Cohen  *
78400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
79400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
80400e64dfSOhad Ben-Cohen  */
81400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
8277ca2332SOhad Ben-Cohen 			     unsigned long iova, int flags, void *token)
83400e64dfSOhad Ben-Cohen {
848afd519cSFernando Guzman Lugo 	struct rproc *rproc = token;
858afd519cSFernando Guzman Lugo 
86400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
87400e64dfSOhad Ben-Cohen 
888afd519cSFernando Guzman Lugo 	rproc_report_crash(rproc, RPROC_MMUFAULT);
898afd519cSFernando Guzman Lugo 
90400e64dfSOhad Ben-Cohen 	/*
91400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
928afd519cSFernando Guzman Lugo 	 * we just used it as a recovery trigger.
93400e64dfSOhad Ben-Cohen 	 */
94400e64dfSOhad Ben-Cohen 	return -ENOSYS;
95400e64dfSOhad Ben-Cohen }
96400e64dfSOhad Ben-Cohen 
97400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
98400e64dfSOhad Ben-Cohen {
99400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
100b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
101400e64dfSOhad Ben-Cohen 	int ret;
102400e64dfSOhad Ben-Cohen 
103315491e5SSuman Anna 	if (!rproc->has_iommu) {
104315491e5SSuman Anna 		dev_dbg(dev, "iommu not present\n");
1050798e1daSMark Grosen 		return 0;
106400e64dfSOhad Ben-Cohen 	}
107400e64dfSOhad Ben-Cohen 
108400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
109400e64dfSOhad Ben-Cohen 	if (!domain) {
110400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
111400e64dfSOhad Ben-Cohen 		return -ENOMEM;
112400e64dfSOhad Ben-Cohen 	}
113400e64dfSOhad Ben-Cohen 
11477ca2332SOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
115400e64dfSOhad Ben-Cohen 
116400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
117400e64dfSOhad Ben-Cohen 	if (ret) {
118400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
119400e64dfSOhad Ben-Cohen 		goto free_domain;
120400e64dfSOhad Ben-Cohen 	}
121400e64dfSOhad Ben-Cohen 
122400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
123400e64dfSOhad Ben-Cohen 
124400e64dfSOhad Ben-Cohen 	return 0;
125400e64dfSOhad Ben-Cohen 
126400e64dfSOhad Ben-Cohen free_domain:
127400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
128400e64dfSOhad Ben-Cohen 	return ret;
129400e64dfSOhad Ben-Cohen }
130400e64dfSOhad Ben-Cohen 
131400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
132400e64dfSOhad Ben-Cohen {
133400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
134b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
135400e64dfSOhad Ben-Cohen 
136400e64dfSOhad Ben-Cohen 	if (!domain)
137400e64dfSOhad Ben-Cohen 		return;
138400e64dfSOhad Ben-Cohen 
139400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
140400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
141400e64dfSOhad Ben-Cohen }
142400e64dfSOhad Ben-Cohen 
143eb30596eSLoic Pallardy static phys_addr_t rproc_va_to_pa(void *cpu_addr)
144eb30596eSLoic Pallardy {
145eb30596eSLoic Pallardy 	/*
146eb30596eSLoic Pallardy 	 * Return physical address according to virtual address location
147eb30596eSLoic Pallardy 	 * - in vmalloc: if region ioremapped or defined as dma_alloc_coherent
148eb30596eSLoic Pallardy 	 * - in kernel: if region allocated in generic dma memory pool
149eb30596eSLoic Pallardy 	 */
150eb30596eSLoic Pallardy 	if (is_vmalloc_addr(cpu_addr)) {
151eb30596eSLoic Pallardy 		return page_to_phys(vmalloc_to_page(cpu_addr)) +
152eb30596eSLoic Pallardy 				    offset_in_page(cpu_addr);
153eb30596eSLoic Pallardy 	}
154eb30596eSLoic Pallardy 
155eb30596eSLoic Pallardy 	WARN_ON(!virt_addr_valid(cpu_addr));
156eb30596eSLoic Pallardy 	return virt_to_phys(cpu_addr);
157eb30596eSLoic Pallardy }
158eb30596eSLoic Pallardy 
159a01f7cd6SSuman Anna /**
160a01f7cd6SSuman Anna  * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
161a01f7cd6SSuman Anna  * @rproc: handle of a remote processor
162a01f7cd6SSuman Anna  * @da: remoteproc device address to translate
163a01f7cd6SSuman Anna  * @len: length of the memory region @da is pointing to
164a01f7cd6SSuman Anna  *
165400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
166400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
167a01f7cd6SSuman Anna  * device addresses (which are hardcoded in the firmware). They may also have
168a01f7cd6SSuman Anna  * dedicated memory regions internal to the processors, and use them either
169a01f7cd6SSuman Anna  * exclusively or alongside carveouts.
170400e64dfSOhad Ben-Cohen  *
171400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
172400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
173400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
174400e64dfSOhad Ben-Cohen  *
175a01f7cd6SSuman Anna  * This function is a helper function with which we can go over the allocated
176a01f7cd6SSuman Anna  * carveouts and translate specific device addresses to kernel virtual addresses
177a01f7cd6SSuman Anna  * so we can access the referenced memory. This function also allows to perform
178a01f7cd6SSuman Anna  * translations on the internal remoteproc memory regions through a platform
179a01f7cd6SSuman Anna  * implementation specific da_to_va ops, if present.
180a01f7cd6SSuman Anna  *
181a01f7cd6SSuman Anna  * The function returns a valid kernel address on success or NULL on failure.
182400e64dfSOhad Ben-Cohen  *
183400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
184400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
185a01f7cd6SSuman Anna  * here the output of the DMA API for the carveouts, which should be more
186a01f7cd6SSuman Anna  * correct.
187400e64dfSOhad Ben-Cohen  */
18872854fb0SSjur Brændeland void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
189400e64dfSOhad Ben-Cohen {
190400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
191400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
192400e64dfSOhad Ben-Cohen 
193a01f7cd6SSuman Anna 	if (rproc->ops->da_to_va) {
194a01f7cd6SSuman Anna 		ptr = rproc->ops->da_to_va(rproc, da, len);
195a01f7cd6SSuman Anna 		if (ptr)
196a01f7cd6SSuman Anna 			goto out;
197a01f7cd6SSuman Anna 	}
198a01f7cd6SSuman Anna 
199400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
200400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
201400e64dfSOhad Ben-Cohen 
202400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
203400e64dfSOhad Ben-Cohen 		if (offset < 0)
204400e64dfSOhad Ben-Cohen 			continue;
205400e64dfSOhad Ben-Cohen 
206400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
207400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
208400e64dfSOhad Ben-Cohen 			continue;
209400e64dfSOhad Ben-Cohen 
210400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
211400e64dfSOhad Ben-Cohen 
212400e64dfSOhad Ben-Cohen 		break;
213400e64dfSOhad Ben-Cohen 	}
214400e64dfSOhad Ben-Cohen 
215a01f7cd6SSuman Anna out:
216400e64dfSOhad Ben-Cohen 	return ptr;
217400e64dfSOhad Ben-Cohen }
2184afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
219400e64dfSOhad Ben-Cohen 
220b0019ccdSLoic Pallardy /**
221b0019ccdSLoic Pallardy  * rproc_find_carveout_by_name() - lookup the carveout region by a name
222b0019ccdSLoic Pallardy  * @rproc: handle of a remote processor
223b0019ccdSLoic Pallardy  * @name,..: carveout name to find (standard printf format)
224b0019ccdSLoic Pallardy  *
225b0019ccdSLoic Pallardy  * Platform driver has the capability to register some pre-allacoted carveout
226b0019ccdSLoic Pallardy  * (physically contiguous memory regions) before rproc firmware loading and
227b0019ccdSLoic Pallardy  * associated resource table analysis. These regions may be dedicated memory
228b0019ccdSLoic Pallardy  * regions internal to the coprocessor or specified DDR region with specific
229b0019ccdSLoic Pallardy  * attributes
230b0019ccdSLoic Pallardy  *
231b0019ccdSLoic Pallardy  * This function is a helper function with which we can go over the
232b0019ccdSLoic Pallardy  * allocated carveouts and return associated region characteristics like
233b0019ccdSLoic Pallardy  * coprocessor address, length or processor virtual address.
234b0019ccdSLoic Pallardy  *
235b0019ccdSLoic Pallardy  * Return: a valid pointer on carveout entry on success or NULL on failure.
236b0019ccdSLoic Pallardy  */
237b0019ccdSLoic Pallardy struct rproc_mem_entry *
238b0019ccdSLoic Pallardy rproc_find_carveout_by_name(struct rproc *rproc, const char *name, ...)
239b0019ccdSLoic Pallardy {
240b0019ccdSLoic Pallardy 	va_list args;
241b0019ccdSLoic Pallardy 	char _name[32];
242b0019ccdSLoic Pallardy 	struct rproc_mem_entry *carveout, *mem = NULL;
243b0019ccdSLoic Pallardy 
244b0019ccdSLoic Pallardy 	if (!name)
245b0019ccdSLoic Pallardy 		return NULL;
246b0019ccdSLoic Pallardy 
247b0019ccdSLoic Pallardy 	va_start(args, name);
248b0019ccdSLoic Pallardy 	vsnprintf(_name, sizeof(_name), name, args);
249b0019ccdSLoic Pallardy 	va_end(args);
250b0019ccdSLoic Pallardy 
251b0019ccdSLoic Pallardy 	list_for_each_entry(carveout, &rproc->carveouts, node) {
252b0019ccdSLoic Pallardy 		/* Compare carveout and requested names */
253b0019ccdSLoic Pallardy 		if (!strcmp(carveout->name, _name)) {
254b0019ccdSLoic Pallardy 			mem = carveout;
255b0019ccdSLoic Pallardy 			break;
256b0019ccdSLoic Pallardy 		}
257b0019ccdSLoic Pallardy 	}
258b0019ccdSLoic Pallardy 
259b0019ccdSLoic Pallardy 	return mem;
260b0019ccdSLoic Pallardy }
261b0019ccdSLoic Pallardy 
262c874bf59SLoic Pallardy /**
263c874bf59SLoic Pallardy  * rproc_check_carveout_da() - Check specified carveout da configuration
264c874bf59SLoic Pallardy  * @rproc: handle of a remote processor
265c874bf59SLoic Pallardy  * @mem: pointer on carveout to check
266c874bf59SLoic Pallardy  * @da: area device address
267c874bf59SLoic Pallardy  * @len: associated area size
268c874bf59SLoic Pallardy  *
269c874bf59SLoic Pallardy  * This function is a helper function to verify requested device area (couple
270c874bf59SLoic Pallardy  * da, len) is part of specified carevout.
271c874bf59SLoic Pallardy  *
272c874bf59SLoic Pallardy  * Return: 0 if carveout match request else -ENOMEM
273c874bf59SLoic Pallardy  */
274c874bf59SLoic Pallardy int rproc_check_carveout_da(struct rproc *rproc, struct rproc_mem_entry *mem,
275c874bf59SLoic Pallardy 			    u32 da, u32 len)
276c874bf59SLoic Pallardy {
277c874bf59SLoic Pallardy 	struct device *dev = &rproc->dev;
278c874bf59SLoic Pallardy 	int delta = 0;
279c874bf59SLoic Pallardy 
280c874bf59SLoic Pallardy 	/* Check requested resource length */
281c874bf59SLoic Pallardy 	if (len > mem->len) {
282c874bf59SLoic Pallardy 		dev_err(dev, "Registered carveout doesn't fit len request\n");
283c874bf59SLoic Pallardy 		return -ENOMEM;
284c874bf59SLoic Pallardy 	}
285c874bf59SLoic Pallardy 
286c874bf59SLoic Pallardy 	if (da != FW_RSC_ADDR_ANY && mem->da == FW_RSC_ADDR_ANY) {
287c874bf59SLoic Pallardy 		/* Update existing carveout da */
288c874bf59SLoic Pallardy 		mem->da = da;
289c874bf59SLoic Pallardy 	} else if (da != FW_RSC_ADDR_ANY && mem->da != FW_RSC_ADDR_ANY) {
290c874bf59SLoic Pallardy 		delta = da - mem->da;
291c874bf59SLoic Pallardy 
292c874bf59SLoic Pallardy 		/* Check requested resource belongs to registered carveout */
293c874bf59SLoic Pallardy 		if (delta < 0) {
294c874bf59SLoic Pallardy 			dev_err(dev,
295c874bf59SLoic Pallardy 				"Registered carveout doesn't fit da request\n");
296c874bf59SLoic Pallardy 			return -ENOMEM;
297c874bf59SLoic Pallardy 		}
298c874bf59SLoic Pallardy 
299c874bf59SLoic Pallardy 		if (delta + len > mem->len) {
300c874bf59SLoic Pallardy 			dev_err(dev,
301c874bf59SLoic Pallardy 				"Registered carveout doesn't fit len request\n");
302c874bf59SLoic Pallardy 			return -ENOMEM;
303c874bf59SLoic Pallardy 		}
304c874bf59SLoic Pallardy 	}
305c874bf59SLoic Pallardy 
306c874bf59SLoic Pallardy 	return 0;
307c874bf59SLoic Pallardy }
308c874bf59SLoic Pallardy 
3096db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
310400e64dfSOhad Ben-Cohen {
3117a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
312b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3136db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
314c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
3157a186941SOhad Ben-Cohen 	dma_addr_t dma;
3167a186941SOhad Ben-Cohen 	void *va;
3177a186941SOhad Ben-Cohen 	int ret, size, notifyid;
318400e64dfSOhad Ben-Cohen 
3196db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
3206db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
3216db20ea8SOhad Ben-Cohen 
3226db20ea8SOhad Ben-Cohen 	/*
3236db20ea8SOhad Ben-Cohen 	 * Allocate non-cacheable memory for the vring. In the future
3246db20ea8SOhad Ben-Cohen 	 * this call will also configure the IOMMU for us
3256db20ea8SOhad Ben-Cohen 	 */
326b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
3276db20ea8SOhad Ben-Cohen 	if (!va) {
328b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent failed\n");
3296db20ea8SOhad Ben-Cohen 		return -EINVAL;
3306db20ea8SOhad Ben-Cohen 	}
3316db20ea8SOhad Ben-Cohen 
3326db20ea8SOhad Ben-Cohen 	/*
3336db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
3346db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
3356db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
3366db20ea8SOhad Ben-Cohen 	 */
33715fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
338b39599b7SSuman Anna 	if (ret < 0) {
33915fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
340b5ab5e24SOhad Ben-Cohen 		dma_free_coherent(dev->parent, size, va, dma);
3416db20ea8SOhad Ben-Cohen 		return ret;
3426db20ea8SOhad Ben-Cohen 	}
34315fc6110STejun Heo 	notifyid = ret;
3446db20ea8SOhad Ben-Cohen 
34548f18f89SBjorn Andersson 	/* Potentially bump max_notifyid */
34648f18f89SBjorn Andersson 	if (notifyid > rproc->max_notifyid)
34748f18f89SBjorn Andersson 		rproc->max_notifyid = notifyid;
34848f18f89SBjorn Andersson 
349276ec993SLoic Pallardy 	dev_dbg(dev, "vring%d: va %pK dma %pad size 0x%x idr %d\n",
350b605ed8bSAnna, Suman 		i, va, &dma, size, notifyid);
3516db20ea8SOhad Ben-Cohen 
3526db20ea8SOhad Ben-Cohen 	rvring->va = va;
3536db20ea8SOhad Ben-Cohen 	rvring->dma = dma;
3546db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
3556db20ea8SOhad Ben-Cohen 
356c0d63157SSjur Brændeland 	/*
357c0d63157SSjur Brændeland 	 * Let the rproc know the notifyid and da of this vring.
358c0d63157SSjur Brændeland 	 * Not all platforms use dma_alloc_coherent to automatically
359c0d63157SSjur Brændeland 	 * set up the iommu. In this case the device address (da) will
360c0d63157SSjur Brændeland 	 * hold the physical address and not the device address.
361c0d63157SSjur Brændeland 	 */
362c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
363c0d63157SSjur Brændeland 	rsc->vring[i].da = dma;
364c0d63157SSjur Brændeland 	rsc->vring[i].notifyid = notifyid;
3656db20ea8SOhad Ben-Cohen 	return 0;
3666db20ea8SOhad Ben-Cohen }
3676db20ea8SOhad Ben-Cohen 
368400e64dfSOhad Ben-Cohen static int
3696db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
370400e64dfSOhad Ben-Cohen {
371400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
372b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
373400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
3746db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
375400e64dfSOhad Ben-Cohen 
3769d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
3777a186941SOhad Ben-Cohen 		i, vring->da, vring->num, vring->align);
3787a186941SOhad Ben-Cohen 
37963140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
38063140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
38163140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
38263140e0eSOhad Ben-Cohen 			vring->num, vring->align);
383400e64dfSOhad Ben-Cohen 		return -EINVAL;
384400e64dfSOhad Ben-Cohen 	}
385400e64dfSOhad Ben-Cohen 
3866db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
3876db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
3886db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
389400e64dfSOhad Ben-Cohen 
390400e64dfSOhad Ben-Cohen 	return 0;
391400e64dfSOhad Ben-Cohen }
392400e64dfSOhad Ben-Cohen 
3936db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
3947a186941SOhad Ben-Cohen {
39563140e0eSOhad Ben-Cohen 	int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
3966db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
397c0d63157SSjur Brændeland 	int idx = rvring->rvdev->vring - rvring;
398c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
3997a186941SOhad Ben-Cohen 
400b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
4017a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
402099a3f33SSjur Brændeland 
403c0d63157SSjur Brændeland 	/* reset resource entry info */
404c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
405c0d63157SSjur Brændeland 	rsc->vring[idx].da = 0;
406c0d63157SSjur Brændeland 	rsc->vring[idx].notifyid = -1;
4077a186941SOhad Ben-Cohen }
4087a186941SOhad Ben-Cohen 
4096f8b0373SAlex Elder static int rproc_vdev_do_start(struct rproc_subdev *subdev)
410f5bcb353SBjorn Andersson {
411f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
412f5bcb353SBjorn Andersson 
413f5bcb353SBjorn Andersson 	return rproc_add_virtio_dev(rvdev, rvdev->id);
414f5bcb353SBjorn Andersson }
415f5bcb353SBjorn Andersson 
4166f8b0373SAlex Elder static void rproc_vdev_do_stop(struct rproc_subdev *subdev, bool crashed)
417f5bcb353SBjorn Andersson {
418f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
419f5bcb353SBjorn Andersson 
420f5bcb353SBjorn Andersson 	rproc_remove_virtio_dev(rvdev);
421f5bcb353SBjorn Andersson }
422f5bcb353SBjorn Andersson 
423400e64dfSOhad Ben-Cohen /**
424fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
425400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
426400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
427fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
428400e64dfSOhad Ben-Cohen  *
4297a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
4307a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
4317a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
4327a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
433400e64dfSOhad Ben-Cohen  *
4347a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
4357a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
4367a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
4377a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
4387a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
439400e64dfSOhad Ben-Cohen  *
4407a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
4417a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
4427a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
4437a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
4447a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
4457a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
4467a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
447400e64dfSOhad Ben-Cohen  *
448400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
449400e64dfSOhad Ben-Cohen  */
450fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
451a2b950acSOhad Ben-Cohen 			     int offset, int avail)
452400e64dfSOhad Ben-Cohen {
453b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
4547a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
4557a186941SOhad Ben-Cohen 	int i, ret;
456fd2c15ecSOhad Ben-Cohen 
457fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
458fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
459fd2c15ecSOhad Ben-Cohen 			+ rsc->config_len > avail) {
460b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
461fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
462fd2c15ecSOhad Ben-Cohen 	}
463fd2c15ecSOhad Ben-Cohen 
464fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
465fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
466fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
467fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
468fd2c15ecSOhad Ben-Cohen 	}
469fd2c15ecSOhad Ben-Cohen 
4709d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
471fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
472400e64dfSOhad Ben-Cohen 
4737a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
4747a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
475fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
476400e64dfSOhad Ben-Cohen 		return -EINVAL;
477400e64dfSOhad Ben-Cohen 	}
478400e64dfSOhad Ben-Cohen 
479899585adSAnna, Suman 	rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
4807a186941SOhad Ben-Cohen 	if (!rvdev)
4817a186941SOhad Ben-Cohen 		return -ENOMEM;
4827a186941SOhad Ben-Cohen 
483aab8d802SBjorn Andersson 	kref_init(&rvdev->refcount);
484aab8d802SBjorn Andersson 
485f5bcb353SBjorn Andersson 	rvdev->id = rsc->id;
4867a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
4877a186941SOhad Ben-Cohen 
4886db20ea8SOhad Ben-Cohen 	/* parse the vrings */
489fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
4906db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
4917a186941SOhad Ben-Cohen 		if (ret)
4926db20ea8SOhad Ben-Cohen 			goto free_rvdev;
493fd2c15ecSOhad Ben-Cohen 	}
494fd2c15ecSOhad Ben-Cohen 
495a2b950acSOhad Ben-Cohen 	/* remember the resource offset*/
496a2b950acSOhad Ben-Cohen 	rvdev->rsc_offset = offset;
497400e64dfSOhad Ben-Cohen 
498a863af5dSBjorn Andersson 	/* allocate the vring resources */
499a863af5dSBjorn Andersson 	for (i = 0; i < rsc->num_of_vrings; i++) {
500a863af5dSBjorn Andersson 		ret = rproc_alloc_vring(rvdev, i);
501a863af5dSBjorn Andersson 		if (ret)
502a863af5dSBjorn Andersson 			goto unwind_vring_allocations;
503a863af5dSBjorn Andersson 	}
504a863af5dSBjorn Andersson 
5057a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
506400e64dfSOhad Ben-Cohen 
5076f8b0373SAlex Elder 	rvdev->subdev.start = rproc_vdev_do_start;
5086f8b0373SAlex Elder 	rvdev->subdev.stop = rproc_vdev_do_stop;
5094902676fSBjorn Andersson 
5104902676fSBjorn Andersson 	rproc_add_subdev(rproc, &rvdev->subdev);
511400e64dfSOhad Ben-Cohen 
512400e64dfSOhad Ben-Cohen 	return 0;
5137a186941SOhad Ben-Cohen 
514a863af5dSBjorn Andersson unwind_vring_allocations:
515a863af5dSBjorn Andersson 	for (i--; i >= 0; i--)
516a863af5dSBjorn Andersson 		rproc_free_vring(&rvdev->vring[i]);
5176db20ea8SOhad Ben-Cohen free_rvdev:
5187a186941SOhad Ben-Cohen 	kfree(rvdev);
5197a186941SOhad Ben-Cohen 	return ret;
520400e64dfSOhad Ben-Cohen }
521400e64dfSOhad Ben-Cohen 
522aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref)
523aab8d802SBjorn Andersson {
524aab8d802SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
525a863af5dSBjorn Andersson 	struct rproc_vring *rvring;
526f5bcb353SBjorn Andersson 	struct rproc *rproc = rvdev->rproc;
527a863af5dSBjorn Andersson 	int id;
528a863af5dSBjorn Andersson 
529a863af5dSBjorn Andersson 	for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
530a863af5dSBjorn Andersson 		rvring = &rvdev->vring[id];
531a863af5dSBjorn Andersson 		if (!rvring->va)
532a863af5dSBjorn Andersson 			continue;
533a863af5dSBjorn Andersson 
534a863af5dSBjorn Andersson 		rproc_free_vring(rvring);
535a863af5dSBjorn Andersson 	}
536aab8d802SBjorn Andersson 
537f5bcb353SBjorn Andersson 	rproc_remove_subdev(rproc, &rvdev->subdev);
538aab8d802SBjorn Andersson 	list_del(&rvdev->node);
539aab8d802SBjorn Andersson 	kfree(rvdev);
540aab8d802SBjorn Andersson }
541aab8d802SBjorn Andersson 
542400e64dfSOhad Ben-Cohen /**
543400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
544400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
545400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
546fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
547400e64dfSOhad Ben-Cohen  *
548400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
549400e64dfSOhad Ben-Cohen  * export it via debugfs.
550400e64dfSOhad Ben-Cohen  *
551400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
552400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
553400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
554400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
555400e64dfSOhad Ben-Cohen  *
556400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
557400e64dfSOhad Ben-Cohen  */
558fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
559a2b950acSOhad Ben-Cohen 			      int offset, int avail)
560400e64dfSOhad Ben-Cohen {
561400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *trace;
562b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
563400e64dfSOhad Ben-Cohen 	void *ptr;
564400e64dfSOhad Ben-Cohen 	char name[15];
565400e64dfSOhad Ben-Cohen 
566fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
567b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
568fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
569fd2c15ecSOhad Ben-Cohen 	}
570fd2c15ecSOhad Ben-Cohen 
571fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
572fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
573fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
574fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
575fd2c15ecSOhad Ben-Cohen 	}
576fd2c15ecSOhad Ben-Cohen 
577400e64dfSOhad Ben-Cohen 	/* what's the kernel address of this resource ? */
578400e64dfSOhad Ben-Cohen 	ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
579400e64dfSOhad Ben-Cohen 	if (!ptr) {
580400e64dfSOhad Ben-Cohen 		dev_err(dev, "erroneous trace resource entry\n");
581400e64dfSOhad Ben-Cohen 		return -EINVAL;
582400e64dfSOhad Ben-Cohen 	}
583400e64dfSOhad Ben-Cohen 
584400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
585172e6ab1SSuman Anna 	if (!trace)
586400e64dfSOhad Ben-Cohen 		return -ENOMEM;
587400e64dfSOhad Ben-Cohen 
588400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
589400e64dfSOhad Ben-Cohen 	trace->len = rsc->len;
590400e64dfSOhad Ben-Cohen 	trace->va = ptr;
591400e64dfSOhad Ben-Cohen 
592400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
593400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
594400e64dfSOhad Ben-Cohen 
595400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
596400e64dfSOhad Ben-Cohen 	trace->priv = rproc_create_trace_file(name, rproc, trace);
597400e64dfSOhad Ben-Cohen 	if (!trace->priv) {
598400e64dfSOhad Ben-Cohen 		trace->va = NULL;
599400e64dfSOhad Ben-Cohen 		kfree(trace);
600400e64dfSOhad Ben-Cohen 		return -EINVAL;
601400e64dfSOhad Ben-Cohen 	}
602400e64dfSOhad Ben-Cohen 
603400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
604400e64dfSOhad Ben-Cohen 
605400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
606400e64dfSOhad Ben-Cohen 
607276ec993SLoic Pallardy 	dev_dbg(dev, "%s added: va %pK, da 0x%x, len 0x%x\n",
60835386166SLee Jones 		name, ptr, rsc->da, rsc->len);
609400e64dfSOhad Ben-Cohen 
610400e64dfSOhad Ben-Cohen 	return 0;
611400e64dfSOhad Ben-Cohen }
612400e64dfSOhad Ben-Cohen 
613400e64dfSOhad Ben-Cohen /**
614400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
615400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
616400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
617fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
618400e64dfSOhad Ben-Cohen  *
619400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
620400e64dfSOhad Ben-Cohen  *
621400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
622400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
623400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
624400e64dfSOhad Ben-Cohen  *
625400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
626400e64dfSOhad Ben-Cohen  *
627400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
628400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
629400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
630400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
631400e64dfSOhad Ben-Cohen  *
632400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
633400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
634400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
635400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
636400e64dfSOhad Ben-Cohen  * are outside those ranges.
637400e64dfSOhad Ben-Cohen  */
638fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
639a2b950acSOhad Ben-Cohen 			       int offset, int avail)
640400e64dfSOhad Ben-Cohen {
641400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
642b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
643400e64dfSOhad Ben-Cohen 	int ret;
644400e64dfSOhad Ben-Cohen 
645400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
646400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
647400e64dfSOhad Ben-Cohen 		return -EINVAL;
648400e64dfSOhad Ben-Cohen 
649fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
650b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
651fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
652fd2c15ecSOhad Ben-Cohen 	}
653fd2c15ecSOhad Ben-Cohen 
654fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
655fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
656b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
657fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
658fd2c15ecSOhad Ben-Cohen 	}
659fd2c15ecSOhad Ben-Cohen 
660400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
661172e6ab1SSuman Anna 	if (!mapping)
662400e64dfSOhad Ben-Cohen 		return -ENOMEM;
663400e64dfSOhad Ben-Cohen 
664400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
665400e64dfSOhad Ben-Cohen 	if (ret) {
666b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
667400e64dfSOhad Ben-Cohen 		goto out;
668400e64dfSOhad Ben-Cohen 	}
669400e64dfSOhad Ben-Cohen 
670400e64dfSOhad Ben-Cohen 	/*
671400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
672400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
673400e64dfSOhad Ben-Cohen 	 *
674400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
675400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
676400e64dfSOhad Ben-Cohen 	 */
677400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
678400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
679400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
680400e64dfSOhad Ben-Cohen 
681b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
682400e64dfSOhad Ben-Cohen 		rsc->pa, rsc->da, rsc->len);
683400e64dfSOhad Ben-Cohen 
684400e64dfSOhad Ben-Cohen 	return 0;
685400e64dfSOhad Ben-Cohen 
686400e64dfSOhad Ben-Cohen out:
687400e64dfSOhad Ben-Cohen 	kfree(mapping);
688400e64dfSOhad Ben-Cohen 	return ret;
689400e64dfSOhad Ben-Cohen }
690400e64dfSOhad Ben-Cohen 
691400e64dfSOhad Ben-Cohen /**
692d7c51706SLoic Pallardy  * rproc_alloc_carveout() - allocated specified carveout
693d7c51706SLoic Pallardy  * @rproc: rproc handle
694d7c51706SLoic Pallardy  * @mem: the memory entry to allocate
695d7c51706SLoic Pallardy  *
696d7c51706SLoic Pallardy  * This function allocate specified memory entry @mem using
697d7c51706SLoic Pallardy  * dma_alloc_coherent() as default allocator
698d7c51706SLoic Pallardy  */
699d7c51706SLoic Pallardy static int rproc_alloc_carveout(struct rproc *rproc,
700d7c51706SLoic Pallardy 				struct rproc_mem_entry *mem)
701d7c51706SLoic Pallardy {
702d7c51706SLoic Pallardy 	struct rproc_mem_entry *mapping = NULL;
703d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
704d7c51706SLoic Pallardy 	dma_addr_t dma;
705d7c51706SLoic Pallardy 	void *va;
706d7c51706SLoic Pallardy 	int ret;
707d7c51706SLoic Pallardy 
708d7c51706SLoic Pallardy 	va = dma_alloc_coherent(dev->parent, mem->len, &dma, GFP_KERNEL);
709d7c51706SLoic Pallardy 	if (!va) {
710d7c51706SLoic Pallardy 		dev_err(dev->parent,
711d7c51706SLoic Pallardy 			"failed to allocate dma memory: len 0x%x\n", mem->len);
712d7c51706SLoic Pallardy 		return -ENOMEM;
713d7c51706SLoic Pallardy 	}
714d7c51706SLoic Pallardy 
715d7c51706SLoic Pallardy 	dev_dbg(dev, "carveout va %pK, dma %pad, len 0x%x\n",
716d7c51706SLoic Pallardy 		va, &dma, mem->len);
717d7c51706SLoic Pallardy 
718d7c51706SLoic Pallardy 	/*
719d7c51706SLoic Pallardy 	 * Ok, this is non-standard.
720d7c51706SLoic Pallardy 	 *
721d7c51706SLoic Pallardy 	 * Sometimes we can't rely on the generic iommu-based DMA API
722d7c51706SLoic Pallardy 	 * to dynamically allocate the device address and then set the IOMMU
723d7c51706SLoic Pallardy 	 * tables accordingly, because some remote processors might
724d7c51706SLoic Pallardy 	 * _require_ us to use hard coded device addresses that their
725d7c51706SLoic Pallardy 	 * firmware was compiled with.
726d7c51706SLoic Pallardy 	 *
727d7c51706SLoic Pallardy 	 * In this case, we must use the IOMMU API directly and map
728d7c51706SLoic Pallardy 	 * the memory to the device address as expected by the remote
729d7c51706SLoic Pallardy 	 * processor.
730d7c51706SLoic Pallardy 	 *
731d7c51706SLoic Pallardy 	 * Obviously such remote processor devices should not be configured
732d7c51706SLoic Pallardy 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
733d7c51706SLoic Pallardy 	 * physical address in this case.
734d7c51706SLoic Pallardy 	 */
735d7c51706SLoic Pallardy 
736d7c51706SLoic Pallardy 	if (mem->da != FW_RSC_ADDR_ANY) {
737d7c51706SLoic Pallardy 		if (!rproc->domain) {
738d7c51706SLoic Pallardy 			dev_err(dev->parent,
739d7c51706SLoic Pallardy 				"Bad carveout rsc configuration\n");
740d7c51706SLoic Pallardy 			ret = -ENOMEM;
741d7c51706SLoic Pallardy 			goto dma_free;
742d7c51706SLoic Pallardy 		}
743d7c51706SLoic Pallardy 
744d7c51706SLoic Pallardy 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
745d7c51706SLoic Pallardy 		if (!mapping) {
746d7c51706SLoic Pallardy 			ret = -ENOMEM;
747d7c51706SLoic Pallardy 			goto dma_free;
748d7c51706SLoic Pallardy 		}
749d7c51706SLoic Pallardy 
750d7c51706SLoic Pallardy 		ret = iommu_map(rproc->domain, mem->da, dma, mem->len,
751d7c51706SLoic Pallardy 				mem->flags);
752d7c51706SLoic Pallardy 		if (ret) {
753d7c51706SLoic Pallardy 			dev_err(dev, "iommu_map failed: %d\n", ret);
754d7c51706SLoic Pallardy 			goto free_mapping;
755d7c51706SLoic Pallardy 		}
756d7c51706SLoic Pallardy 
757d7c51706SLoic Pallardy 		/*
758d7c51706SLoic Pallardy 		 * We'll need this info later when we'll want to unmap
759d7c51706SLoic Pallardy 		 * everything (e.g. on shutdown).
760d7c51706SLoic Pallardy 		 *
761d7c51706SLoic Pallardy 		 * We can't trust the remote processor not to change the
762d7c51706SLoic Pallardy 		 * resource table, so we must maintain this info independently.
763d7c51706SLoic Pallardy 		 */
764d7c51706SLoic Pallardy 		mapping->da = mem->da;
765d7c51706SLoic Pallardy 		mapping->len = mem->len;
766d7c51706SLoic Pallardy 		list_add_tail(&mapping->node, &rproc->mappings);
767d7c51706SLoic Pallardy 
768d7c51706SLoic Pallardy 		dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
769d7c51706SLoic Pallardy 			mem->da, &dma);
770d7c51706SLoic Pallardy 	} else {
771d7c51706SLoic Pallardy 		mem->da = (u32)dma;
772d7c51706SLoic Pallardy 	}
773d7c51706SLoic Pallardy 
774d7c51706SLoic Pallardy 	mem->dma = (u32)dma;
775d7c51706SLoic Pallardy 	mem->va = va;
776d7c51706SLoic Pallardy 
777d7c51706SLoic Pallardy 	return 0;
778d7c51706SLoic Pallardy 
779d7c51706SLoic Pallardy free_mapping:
780d7c51706SLoic Pallardy 	kfree(mapping);
781d7c51706SLoic Pallardy dma_free:
782d7c51706SLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, va, dma);
783d7c51706SLoic Pallardy 	return ret;
784d7c51706SLoic Pallardy }
785d7c51706SLoic Pallardy 
786d7c51706SLoic Pallardy /**
787f2e74abfSLoic Pallardy  * rproc_release_carveout() - release acquired carveout
788f2e74abfSLoic Pallardy  * @rproc: rproc handle
789f2e74abfSLoic Pallardy  * @mem: the memory entry to release
790f2e74abfSLoic Pallardy  *
791f2e74abfSLoic Pallardy  * This function releases specified memory entry @mem allocated via
792d7c51706SLoic Pallardy  * rproc_alloc_carveout() function by @rproc.
793f2e74abfSLoic Pallardy  */
794f2e74abfSLoic Pallardy static int rproc_release_carveout(struct rproc *rproc,
795f2e74abfSLoic Pallardy 				  struct rproc_mem_entry *mem)
796f2e74abfSLoic Pallardy {
797f2e74abfSLoic Pallardy 	struct device *dev = &rproc->dev;
798f2e74abfSLoic Pallardy 
799f2e74abfSLoic Pallardy 	/* clean up carveout allocations */
800f2e74abfSLoic Pallardy 	dma_free_coherent(dev->parent, mem->len, mem->va, mem->dma);
801f2e74abfSLoic Pallardy 	return 0;
802f2e74abfSLoic Pallardy }
803f2e74abfSLoic Pallardy 
804f2e74abfSLoic Pallardy /**
805400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
806400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
807400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
808fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
809400e64dfSOhad Ben-Cohen  *
810400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
811400e64dfSOhad Ben-Cohen  * contiguous memory regions.
812400e64dfSOhad Ben-Cohen  *
813400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
814400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
815400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
816400e64dfSOhad Ben-Cohen  *
817400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
818400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
819400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
820400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
821400e64dfSOhad Ben-Cohen  */
822fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
823a2b950acSOhad Ben-Cohen 				 struct fw_rsc_carveout *rsc,
824a2b950acSOhad Ben-Cohen 				 int offset, int avail)
825400e64dfSOhad Ben-Cohen {
826d7c51706SLoic Pallardy 	struct rproc_mem_entry *carveout;
827b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
828400e64dfSOhad Ben-Cohen 
829fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
830b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
831fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
832fd2c15ecSOhad Ben-Cohen 	}
833fd2c15ecSOhad Ben-Cohen 
834fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
835fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
836fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
837fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
838fd2c15ecSOhad Ben-Cohen 	}
839fd2c15ecSOhad Ben-Cohen 
8409d7814a9SAnna, Suman 	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
84135386166SLee Jones 		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
842fd2c15ecSOhad Ben-Cohen 
843*ffa5f9c8SLoic Pallardy 	/*
844*ffa5f9c8SLoic Pallardy 	 * Check carveout rsc already part of a registered carveout,
845*ffa5f9c8SLoic Pallardy 	 * Search by name, then check the da and length
846*ffa5f9c8SLoic Pallardy 	 */
847*ffa5f9c8SLoic Pallardy 	carveout = rproc_find_carveout_by_name(rproc, rsc->name);
848*ffa5f9c8SLoic Pallardy 
849*ffa5f9c8SLoic Pallardy 	if (carveout) {
850*ffa5f9c8SLoic Pallardy 		if (carveout->rsc_offset != FW_RSC_ADDR_ANY) {
851*ffa5f9c8SLoic Pallardy 			dev_err(dev,
852*ffa5f9c8SLoic Pallardy 				"Carveout already associated to resource table\n");
853*ffa5f9c8SLoic Pallardy 			return -ENOMEM;
854*ffa5f9c8SLoic Pallardy 		}
855*ffa5f9c8SLoic Pallardy 
856*ffa5f9c8SLoic Pallardy 		if (rproc_check_carveout_da(rproc, carveout, rsc->da, rsc->len))
857*ffa5f9c8SLoic Pallardy 			return -ENOMEM;
858*ffa5f9c8SLoic Pallardy 
859*ffa5f9c8SLoic Pallardy 		/* Update memory carveout with resource table info */
860*ffa5f9c8SLoic Pallardy 		carveout->rsc_offset = offset;
861*ffa5f9c8SLoic Pallardy 		carveout->flags = rsc->flags;
862*ffa5f9c8SLoic Pallardy 
863*ffa5f9c8SLoic Pallardy 		return 0;
864*ffa5f9c8SLoic Pallardy 	}
865*ffa5f9c8SLoic Pallardy 
866d7c51706SLoic Pallardy 	/* Register carveout in in list */
867d7c51706SLoic Pallardy 	carveout = rproc_mem_entry_init(dev, 0, 0, rsc->len, rsc->da,
868d7c51706SLoic Pallardy 					rproc_alloc_carveout,
869d7c51706SLoic Pallardy 					rproc_release_carveout, rsc->name);
870d7c51706SLoic Pallardy 	if (!carveout) {
871d7c51706SLoic Pallardy 		dev_err(dev, "Can't allocate memory entry structure\n");
87272029c90SLoic Pallardy 		return -ENOMEM;
873400e64dfSOhad Ben-Cohen 	}
874400e64dfSOhad Ben-Cohen 
875d7c51706SLoic Pallardy 	carveout->flags = rsc->flags;
876d7c51706SLoic Pallardy 	carveout->rsc_offset = offset;
87715c0b025SLoic Pallardy 	rproc_add_carveout(rproc, carveout);
878400e64dfSOhad Ben-Cohen 
879400e64dfSOhad Ben-Cohen 	return 0;
880400e64dfSOhad Ben-Cohen }
881400e64dfSOhad Ben-Cohen 
88272029c90SLoic Pallardy /**
88315c0b025SLoic Pallardy  * rproc_add_carveout() - register an allocated carveout region
88415c0b025SLoic Pallardy  * @rproc: rproc handle
88515c0b025SLoic Pallardy  * @mem: memory entry to register
88615c0b025SLoic Pallardy  *
88715c0b025SLoic Pallardy  * This function registers specified memory entry in @rproc carveouts list.
88815c0b025SLoic Pallardy  * Specified carveout should have been allocated before registering.
88915c0b025SLoic Pallardy  */
89015c0b025SLoic Pallardy void rproc_add_carveout(struct rproc *rproc, struct rproc_mem_entry *mem)
89115c0b025SLoic Pallardy {
89215c0b025SLoic Pallardy 	list_add_tail(&mem->node, &rproc->carveouts);
89315c0b025SLoic Pallardy }
89415c0b025SLoic Pallardy EXPORT_SYMBOL(rproc_add_carveout);
89515c0b025SLoic Pallardy 
89615c0b025SLoic Pallardy /**
89772029c90SLoic Pallardy  * rproc_mem_entry_init() - allocate and initialize rproc_mem_entry struct
89872029c90SLoic Pallardy  * @dev: pointer on device struct
89972029c90SLoic Pallardy  * @va: virtual address
90072029c90SLoic Pallardy  * @dma: dma address
90172029c90SLoic Pallardy  * @len: memory carveout length
90272029c90SLoic Pallardy  * @da: device address
90372029c90SLoic Pallardy  * @release: memory carveout function
90472029c90SLoic Pallardy  * @name: carveout name
90572029c90SLoic Pallardy  *
90672029c90SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
90772029c90SLoic Pallardy  * provided by client.
90872029c90SLoic Pallardy  */
90972029c90SLoic Pallardy struct rproc_mem_entry *
91072029c90SLoic Pallardy rproc_mem_entry_init(struct device *dev,
91172029c90SLoic Pallardy 		     void *va, dma_addr_t dma, int len, u32 da,
912d7c51706SLoic Pallardy 		     int (*alloc)(struct rproc *, struct rproc_mem_entry *),
91372029c90SLoic Pallardy 		     int (*release)(struct rproc *, struct rproc_mem_entry *),
91472029c90SLoic Pallardy 		     const char *name, ...)
91572029c90SLoic Pallardy {
91672029c90SLoic Pallardy 	struct rproc_mem_entry *mem;
91772029c90SLoic Pallardy 	va_list args;
91872029c90SLoic Pallardy 
91972029c90SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
92072029c90SLoic Pallardy 	if (!mem)
92172029c90SLoic Pallardy 		return mem;
92272029c90SLoic Pallardy 
92372029c90SLoic Pallardy 	mem->va = va;
92472029c90SLoic Pallardy 	mem->dma = dma;
92572029c90SLoic Pallardy 	mem->da = da;
92672029c90SLoic Pallardy 	mem->len = len;
927d7c51706SLoic Pallardy 	mem->alloc = alloc;
92872029c90SLoic Pallardy 	mem->release = release;
929d7c51706SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
9301429cca1SLoic Pallardy 	mem->of_resm_idx = -1;
93172029c90SLoic Pallardy 
93272029c90SLoic Pallardy 	va_start(args, name);
93372029c90SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
93472029c90SLoic Pallardy 	va_end(args);
93572029c90SLoic Pallardy 
93672029c90SLoic Pallardy 	return mem;
93772029c90SLoic Pallardy }
93872029c90SLoic Pallardy EXPORT_SYMBOL(rproc_mem_entry_init);
93972029c90SLoic Pallardy 
94072029c90SLoic Pallardy /**
9411429cca1SLoic Pallardy  * rproc_of_resm_mem_entry_init() - allocate and initialize rproc_mem_entry struct
9421429cca1SLoic Pallardy  * from a reserved memory phandle
9431429cca1SLoic Pallardy  * @dev: pointer on device struct
9441429cca1SLoic Pallardy  * @of_resm_idx: reserved memory phandle index in "memory-region"
9451429cca1SLoic Pallardy  * @len: memory carveout length
9461429cca1SLoic Pallardy  * @da: device address
9471429cca1SLoic Pallardy  * @name: carveout name
9481429cca1SLoic Pallardy  *
9491429cca1SLoic Pallardy  * This function allocates a rproc_mem_entry struct and fill it with parameters
9501429cca1SLoic Pallardy  * provided by client.
9511429cca1SLoic Pallardy  */
9521429cca1SLoic Pallardy struct rproc_mem_entry *
9531429cca1SLoic Pallardy rproc_of_resm_mem_entry_init(struct device *dev, u32 of_resm_idx, int len,
9541429cca1SLoic Pallardy 			     u32 da, const char *name, ...)
9551429cca1SLoic Pallardy {
9561429cca1SLoic Pallardy 	struct rproc_mem_entry *mem;
9571429cca1SLoic Pallardy 	va_list args;
9581429cca1SLoic Pallardy 
9591429cca1SLoic Pallardy 	mem = kzalloc(sizeof(*mem), GFP_KERNEL);
9601429cca1SLoic Pallardy 	if (!mem)
9611429cca1SLoic Pallardy 		return mem;
9621429cca1SLoic Pallardy 
9631429cca1SLoic Pallardy 	mem->da = da;
9641429cca1SLoic Pallardy 	mem->len = len;
9651429cca1SLoic Pallardy 	mem->rsc_offset = FW_RSC_ADDR_ANY;
9661429cca1SLoic Pallardy 	mem->of_resm_idx = of_resm_idx;
9671429cca1SLoic Pallardy 
9681429cca1SLoic Pallardy 	va_start(args, name);
9691429cca1SLoic Pallardy 	vsnprintf(mem->name, sizeof(mem->name), name, args);
9701429cca1SLoic Pallardy 	va_end(args);
9711429cca1SLoic Pallardy 
9721429cca1SLoic Pallardy 	return mem;
9731429cca1SLoic Pallardy }
9741429cca1SLoic Pallardy EXPORT_SYMBOL(rproc_of_resm_mem_entry_init);
9751429cca1SLoic Pallardy 
9761429cca1SLoic Pallardy /**
977e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
978e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
979e12bc14bSOhad Ben-Cohen  */
980232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
981fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
982fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
983fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
984232fcdbbSSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
985232fcdbbSSjur Brændeland };
986232fcdbbSSjur Brændeland 
987400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
988a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc,
989232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
990400e64dfSOhad Ben-Cohen {
991b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
992e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
993fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
994400e64dfSOhad Ben-Cohen 
995d4bb86f2SBjorn Andersson 	if (!rproc->table_ptr)
996d4bb86f2SBjorn Andersson 		return 0;
997d4bb86f2SBjorn Andersson 
998a2b950acSOhad Ben-Cohen 	for (i = 0; i < rproc->table_ptr->num; i++) {
999a2b950acSOhad Ben-Cohen 		int offset = rproc->table_ptr->offset[i];
1000a2b950acSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
1001a4b24c75SBjorn Andersson 		int avail = rproc->table_sz - offset - sizeof(*hdr);
1002fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
1003400e64dfSOhad Ben-Cohen 
1004fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
1005fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
1006fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
1007fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
1008fd2c15ecSOhad Ben-Cohen 		}
1009fd2c15ecSOhad Ben-Cohen 
1010fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
1011fd2c15ecSOhad Ben-Cohen 
1012fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
1013fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
1014e12bc14bSOhad Ben-Cohen 			continue;
1015400e64dfSOhad Ben-Cohen 		}
1016400e64dfSOhad Ben-Cohen 
1017232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
1018e12bc14bSOhad Ben-Cohen 		if (!handler)
1019e12bc14bSOhad Ben-Cohen 			continue;
1020e12bc14bSOhad Ben-Cohen 
1021a2b950acSOhad Ben-Cohen 		ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
10227a186941SOhad Ben-Cohen 		if (ret)
1023400e64dfSOhad Ben-Cohen 			break;
1024400e64dfSOhad Ben-Cohen 	}
1025400e64dfSOhad Ben-Cohen 
1026400e64dfSOhad Ben-Cohen 	return ret;
1027400e64dfSOhad Ben-Cohen }
1028400e64dfSOhad Ben-Cohen 
1029c455daa4SBjorn Andersson static int rproc_prepare_subdevices(struct rproc *rproc)
1030c455daa4SBjorn Andersson {
1031c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1032c455daa4SBjorn Andersson 	int ret;
1033c455daa4SBjorn Andersson 
1034c455daa4SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1035c455daa4SBjorn Andersson 		if (subdev->prepare) {
1036c455daa4SBjorn Andersson 			ret = subdev->prepare(subdev);
1037c455daa4SBjorn Andersson 			if (ret)
1038c455daa4SBjorn Andersson 				goto unroll_preparation;
1039c455daa4SBjorn Andersson 		}
1040c455daa4SBjorn Andersson 	}
1041c455daa4SBjorn Andersson 
1042c455daa4SBjorn Andersson 	return 0;
1043c455daa4SBjorn Andersson 
1044c455daa4SBjorn Andersson unroll_preparation:
1045c455daa4SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1046c455daa4SBjorn Andersson 		if (subdev->unprepare)
1047c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1048c455daa4SBjorn Andersson 	}
1049c455daa4SBjorn Andersson 
1050c455daa4SBjorn Andersson 	return ret;
1051c455daa4SBjorn Andersson }
1052c455daa4SBjorn Andersson 
1053618fcff3SBjorn Andersson static int rproc_start_subdevices(struct rproc *rproc)
10547bdc9650SBjorn Andersson {
10557bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
10567bdc9650SBjorn Andersson 	int ret;
10577bdc9650SBjorn Andersson 
10587bdc9650SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
1059be37b1e0SBjorn Andersson 		if (subdev->start) {
1060618fcff3SBjorn Andersson 			ret = subdev->start(subdev);
10617bdc9650SBjorn Andersson 			if (ret)
10627bdc9650SBjorn Andersson 				goto unroll_registration;
10637bdc9650SBjorn Andersson 		}
1064be37b1e0SBjorn Andersson 	}
10657bdc9650SBjorn Andersson 
10667bdc9650SBjorn Andersson 	return 0;
10677bdc9650SBjorn Andersson 
10687bdc9650SBjorn Andersson unroll_registration:
1069be37b1e0SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node) {
1070be37b1e0SBjorn Andersson 		if (subdev->stop)
1071618fcff3SBjorn Andersson 			subdev->stop(subdev, true);
1072be37b1e0SBjorn Andersson 	}
10737bdc9650SBjorn Andersson 
10747bdc9650SBjorn Andersson 	return ret;
10757bdc9650SBjorn Andersson }
10767bdc9650SBjorn Andersson 
1077618fcff3SBjorn Andersson static void rproc_stop_subdevices(struct rproc *rproc, bool crashed)
10787bdc9650SBjorn Andersson {
10797bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
10807bdc9650SBjorn Andersson 
1081be37b1e0SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1082be37b1e0SBjorn Andersson 		if (subdev->stop)
1083618fcff3SBjorn Andersson 			subdev->stop(subdev, crashed);
10847bdc9650SBjorn Andersson 	}
1085be37b1e0SBjorn Andersson }
10867bdc9650SBjorn Andersson 
1087c455daa4SBjorn Andersson static void rproc_unprepare_subdevices(struct rproc *rproc)
1088c455daa4SBjorn Andersson {
1089c455daa4SBjorn Andersson 	struct rproc_subdev *subdev;
1090c455daa4SBjorn Andersson 
1091c455daa4SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node) {
1092c455daa4SBjorn Andersson 		if (subdev->unprepare)
1093c455daa4SBjorn Andersson 			subdev->unprepare(subdev);
1094c455daa4SBjorn Andersson 	}
1095c455daa4SBjorn Andersson }
1096c455daa4SBjorn Andersson 
1097400e64dfSOhad Ben-Cohen /**
1098d7c51706SLoic Pallardy  * rproc_alloc_registered_carveouts() - allocate all carveouts registered
1099d7c51706SLoic Pallardy  * in the list
1100d7c51706SLoic Pallardy  * @rproc: the remote processor handle
1101d7c51706SLoic Pallardy  *
1102d7c51706SLoic Pallardy  * This function parses registered carveout list, performs allocation
1103d7c51706SLoic Pallardy  * if alloc() ops registered and updates resource table information
1104d7c51706SLoic Pallardy  * if rsc_offset set.
1105d7c51706SLoic Pallardy  *
1106d7c51706SLoic Pallardy  * Return: 0 on success
1107d7c51706SLoic Pallardy  */
1108d7c51706SLoic Pallardy static int rproc_alloc_registered_carveouts(struct rproc *rproc)
1109d7c51706SLoic Pallardy {
1110d7c51706SLoic Pallardy 	struct rproc_mem_entry *entry, *tmp;
1111d7c51706SLoic Pallardy 	struct fw_rsc_carveout *rsc;
1112d7c51706SLoic Pallardy 	struct device *dev = &rproc->dev;
1113d7c51706SLoic Pallardy 	int ret;
1114d7c51706SLoic Pallardy 
1115d7c51706SLoic Pallardy 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1116d7c51706SLoic Pallardy 		if (entry->alloc) {
1117d7c51706SLoic Pallardy 			ret = entry->alloc(rproc, entry);
1118d7c51706SLoic Pallardy 			if (ret) {
1119d7c51706SLoic Pallardy 				dev_err(dev, "Unable to allocate carveout %s: %d\n",
1120d7c51706SLoic Pallardy 					entry->name, ret);
1121d7c51706SLoic Pallardy 				return -ENOMEM;
1122d7c51706SLoic Pallardy 			}
1123d7c51706SLoic Pallardy 		}
1124d7c51706SLoic Pallardy 
1125d7c51706SLoic Pallardy 		if (entry->rsc_offset != FW_RSC_ADDR_ANY) {
1126d7c51706SLoic Pallardy 			/* update resource table */
1127d7c51706SLoic Pallardy 			rsc = (void *)rproc->table_ptr + entry->rsc_offset;
1128d7c51706SLoic Pallardy 
1129d7c51706SLoic Pallardy 			/*
1130d7c51706SLoic Pallardy 			 * Some remote processors might need to know the pa
1131d7c51706SLoic Pallardy 			 * even though they are behind an IOMMU. E.g., OMAP4's
1132d7c51706SLoic Pallardy 			 * remote M3 processor needs this so it can control
1133d7c51706SLoic Pallardy 			 * on-chip hardware accelerators that are not behind
1134d7c51706SLoic Pallardy 			 * the IOMMU, and therefor must know the pa.
1135d7c51706SLoic Pallardy 			 *
1136d7c51706SLoic Pallardy 			 * Generally we don't want to expose physical addresses
1137d7c51706SLoic Pallardy 			 * if we don't have to (remote processors are generally
1138d7c51706SLoic Pallardy 			 * _not_ trusted), so we might want to do this only for
1139d7c51706SLoic Pallardy 			 * remote processor that _must_ have this (e.g. OMAP4's
1140d7c51706SLoic Pallardy 			 * dual M3 subsystem).
1141d7c51706SLoic Pallardy 			 *
1142d7c51706SLoic Pallardy 			 * Non-IOMMU processors might also want to have this info.
1143d7c51706SLoic Pallardy 			 * In this case, the device address and the physical address
1144d7c51706SLoic Pallardy 			 * are the same.
1145d7c51706SLoic Pallardy 			 */
1146*ffa5f9c8SLoic Pallardy 
1147*ffa5f9c8SLoic Pallardy 			/* Use va if defined else dma to generate pa */
1148d7c51706SLoic Pallardy 			if (entry->va)
1149d7c51706SLoic Pallardy 				rsc->pa = (u32)rproc_va_to_pa(entry->va);
1150*ffa5f9c8SLoic Pallardy 			else
1151*ffa5f9c8SLoic Pallardy 				rsc->pa = (u32)entry->dma;
1152*ffa5f9c8SLoic Pallardy 
1153*ffa5f9c8SLoic Pallardy 			rsc->da = entry->da;
1154*ffa5f9c8SLoic Pallardy 			rsc->len = entry->len;
1155d7c51706SLoic Pallardy 		}
1156d7c51706SLoic Pallardy 	}
1157d7c51706SLoic Pallardy 
1158d7c51706SLoic Pallardy 	return 0;
1159d7c51706SLoic Pallardy }
1160d7c51706SLoic Pallardy 
1161d7c51706SLoic Pallardy /**
11622666ca91SSarangdhar Joshi  * rproc_coredump_cleanup() - clean up dump_segments list
11632666ca91SSarangdhar Joshi  * @rproc: the remote processor handle
11642666ca91SSarangdhar Joshi  */
11652666ca91SSarangdhar Joshi static void rproc_coredump_cleanup(struct rproc *rproc)
11662666ca91SSarangdhar Joshi {
11672666ca91SSarangdhar Joshi 	struct rproc_dump_segment *entry, *tmp;
11682666ca91SSarangdhar Joshi 
11692666ca91SSarangdhar Joshi 	list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) {
11702666ca91SSarangdhar Joshi 		list_del(&entry->node);
11712666ca91SSarangdhar Joshi 		kfree(entry);
11722666ca91SSarangdhar Joshi 	}
11732666ca91SSarangdhar Joshi }
11742666ca91SSarangdhar Joshi 
11752666ca91SSarangdhar Joshi /**
1176400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
1177400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
1178400e64dfSOhad Ben-Cohen  *
1179400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
11807a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
1181400e64dfSOhad Ben-Cohen  */
1182400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc)
1183400e64dfSOhad Ben-Cohen {
1184400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
1185d81fb32fSBjorn Andersson 	struct rproc_vdev *rvdev, *rvtmp;
1186b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1187400e64dfSOhad Ben-Cohen 
1188400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
1189400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
1190400e64dfSOhad Ben-Cohen 		rproc_remove_trace_file(entry->priv);
1191400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
1192400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
1193400e64dfSOhad Ben-Cohen 		kfree(entry);
1194400e64dfSOhad Ben-Cohen 	}
1195400e64dfSOhad Ben-Cohen 
1196400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
1197400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
1198400e64dfSOhad Ben-Cohen 		size_t unmapped;
1199400e64dfSOhad Ben-Cohen 
1200400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
1201400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
1202400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
1203e981f6d4SSjur Brændeland 			dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
1204400e64dfSOhad Ben-Cohen 				unmapped);
1205400e64dfSOhad Ben-Cohen 		}
1206400e64dfSOhad Ben-Cohen 
1207400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
1208400e64dfSOhad Ben-Cohen 		kfree(entry);
1209400e64dfSOhad Ben-Cohen 	}
1210b6356a01SSuman Anna 
1211b6356a01SSuman Anna 	/* clean up carveout allocations */
1212b6356a01SSuman Anna 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
1213f2e74abfSLoic Pallardy 		if (entry->release)
1214f2e74abfSLoic Pallardy 			entry->release(rproc, entry);
1215b6356a01SSuman Anna 		list_del(&entry->node);
1216b6356a01SSuman Anna 		kfree(entry);
1217b6356a01SSuman Anna 	}
1218d81fb32fSBjorn Andersson 
1219d81fb32fSBjorn Andersson 	/* clean up remote vdev entries */
1220f5bcb353SBjorn Andersson 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
12212b45cef5SBjorn Andersson 		kref_put(&rvdev->refcount, rproc_vdev_release);
12222666ca91SSarangdhar Joshi 
12232666ca91SSarangdhar Joshi 	rproc_coredump_cleanup(rproc);
12242b45cef5SBjorn Andersson }
1225400e64dfSOhad Ben-Cohen 
12261efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw)
12271efa30d0SSarangdhar Joshi {
1228a4b24c75SBjorn Andersson 	struct resource_table *loaded_table;
12291efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
1230a4b24c75SBjorn Andersson 	int ret;
12311efa30d0SSarangdhar Joshi 
12321efa30d0SSarangdhar Joshi 	/* load the ELF segments to memory */
12331efa30d0SSarangdhar Joshi 	ret = rproc_load_segments(rproc, fw);
12341efa30d0SSarangdhar Joshi 	if (ret) {
12351efa30d0SSarangdhar Joshi 		dev_err(dev, "Failed to load program segments: %d\n", ret);
12361efa30d0SSarangdhar Joshi 		return ret;
12371efa30d0SSarangdhar Joshi 	}
12381efa30d0SSarangdhar Joshi 
12391efa30d0SSarangdhar Joshi 	/*
12401efa30d0SSarangdhar Joshi 	 * The starting device has been given the rproc->cached_table as the
12411efa30d0SSarangdhar Joshi 	 * resource table. The address of the vring along with the other
12421efa30d0SSarangdhar Joshi 	 * allocated resources (carveouts etc) is stored in cached_table.
12431efa30d0SSarangdhar Joshi 	 * In order to pass this information to the remote device we must copy
12441efa30d0SSarangdhar Joshi 	 * this information to device memory. We also update the table_ptr so
12451efa30d0SSarangdhar Joshi 	 * that any subsequent changes will be applied to the loaded version.
12461efa30d0SSarangdhar Joshi 	 */
12471efa30d0SSarangdhar Joshi 	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
12481efa30d0SSarangdhar Joshi 	if (loaded_table) {
1249a4b24c75SBjorn Andersson 		memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
12501efa30d0SSarangdhar Joshi 		rproc->table_ptr = loaded_table;
12511efa30d0SSarangdhar Joshi 	}
12521efa30d0SSarangdhar Joshi 
1253c455daa4SBjorn Andersson 	ret = rproc_prepare_subdevices(rproc);
1254c455daa4SBjorn Andersson 	if (ret) {
1255c455daa4SBjorn Andersson 		dev_err(dev, "failed to prepare subdevices for %s: %d\n",
1256c455daa4SBjorn Andersson 			rproc->name, ret);
1257f68d51bdSSuman Anna 		goto reset_table_ptr;
1258c455daa4SBjorn Andersson 	}
1259c455daa4SBjorn Andersson 
12601efa30d0SSarangdhar Joshi 	/* power up the remote processor */
12611efa30d0SSarangdhar Joshi 	ret = rproc->ops->start(rproc);
12621efa30d0SSarangdhar Joshi 	if (ret) {
12631efa30d0SSarangdhar Joshi 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
1264c455daa4SBjorn Andersson 		goto unprepare_subdevices;
12651efa30d0SSarangdhar Joshi 	}
12661efa30d0SSarangdhar Joshi 
1267618fcff3SBjorn Andersson 	/* Start any subdevices for the remote processor */
1268618fcff3SBjorn Andersson 	ret = rproc_start_subdevices(rproc);
12691efa30d0SSarangdhar Joshi 	if (ret) {
12701efa30d0SSarangdhar Joshi 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
12711efa30d0SSarangdhar Joshi 			rproc->name, ret);
1272c455daa4SBjorn Andersson 		goto stop_rproc;
12731efa30d0SSarangdhar Joshi 	}
12741efa30d0SSarangdhar Joshi 
12751efa30d0SSarangdhar Joshi 	rproc->state = RPROC_RUNNING;
12761efa30d0SSarangdhar Joshi 
12771efa30d0SSarangdhar Joshi 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
12781efa30d0SSarangdhar Joshi 
12791efa30d0SSarangdhar Joshi 	return 0;
1280c455daa4SBjorn Andersson 
1281c455daa4SBjorn Andersson stop_rproc:
1282c455daa4SBjorn Andersson 	rproc->ops->stop(rproc);
1283c455daa4SBjorn Andersson unprepare_subdevices:
1284c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1285f68d51bdSSuman Anna reset_table_ptr:
1286f68d51bdSSuman Anna 	rproc->table_ptr = rproc->cached_table;
1287c455daa4SBjorn Andersson 
1288c455daa4SBjorn Andersson 	return ret;
12891efa30d0SSarangdhar Joshi }
12901efa30d0SSarangdhar Joshi 
1291400e64dfSOhad Ben-Cohen /*
1292400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
1293400e64dfSOhad Ben-Cohen  */
1294400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
1295400e64dfSOhad Ben-Cohen {
1296b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1297400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
129858b64090SBjorn Andersson 	int ret;
1299400e64dfSOhad Ben-Cohen 
1300400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
1301400e64dfSOhad Ben-Cohen 	if (ret)
1302400e64dfSOhad Ben-Cohen 		return ret;
1303400e64dfSOhad Ben-Cohen 
1304e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
1305400e64dfSOhad Ben-Cohen 
1306400e64dfSOhad Ben-Cohen 	/*
1307400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
1308400e64dfSOhad Ben-Cohen 	 * just a nop
1309400e64dfSOhad Ben-Cohen 	 */
1310400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
1311400e64dfSOhad Ben-Cohen 	if (ret) {
1312400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
1313400e64dfSOhad Ben-Cohen 		return ret;
1314400e64dfSOhad Ben-Cohen 	}
1315400e64dfSOhad Ben-Cohen 
13163e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
1317400e64dfSOhad Ben-Cohen 
1318c1d35c1aSBjorn Andersson 	/* Load resource table, core dump segment list etc from the firmware */
1319c1d35c1aSBjorn Andersson 	ret = rproc_parse_fw(rproc, fw);
132058b64090SBjorn Andersson 	if (ret)
132158b64090SBjorn Andersson 		goto disable_iommu;
1322a0c10687SBjorn Andersson 
1323b35d7afcSBjorn Andersson 	/* reset max_notifyid */
1324b35d7afcSBjorn Andersson 	rproc->max_notifyid = -1;
1325b35d7afcSBjorn Andersson 
1326400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
1327a4b24c75SBjorn Andersson 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
1328400e64dfSOhad Ben-Cohen 	if (ret) {
1329400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
1330229b85a6SBjorn Andersson 		goto clean_up_resources;
1331400e64dfSOhad Ben-Cohen 	}
1332400e64dfSOhad Ben-Cohen 
1333d7c51706SLoic Pallardy 	/* Allocate carveout resources associated to rproc */
1334d7c51706SLoic Pallardy 	ret = rproc_alloc_registered_carveouts(rproc);
1335d7c51706SLoic Pallardy 	if (ret) {
1336d7c51706SLoic Pallardy 		dev_err(dev, "Failed to allocate associated carveouts: %d\n",
1337d7c51706SLoic Pallardy 			ret);
1338d7c51706SLoic Pallardy 		goto clean_up_resources;
1339d7c51706SLoic Pallardy 	}
1340d7c51706SLoic Pallardy 
13411efa30d0SSarangdhar Joshi 	ret = rproc_start(rproc, fw);
13421efa30d0SSarangdhar Joshi 	if (ret)
1343229b85a6SBjorn Andersson 		goto clean_up_resources;
1344400e64dfSOhad Ben-Cohen 
1345400e64dfSOhad Ben-Cohen 	return 0;
1346400e64dfSOhad Ben-Cohen 
1347229b85a6SBjorn Andersson clean_up_resources:
1348229b85a6SBjorn Andersson 	rproc_resource_cleanup(rproc);
1349a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1350a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1351988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
135258b64090SBjorn Andersson disable_iommu:
1353400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1354400e64dfSOhad Ben-Cohen 	return ret;
1355400e64dfSOhad Ben-Cohen }
1356400e64dfSOhad Ben-Cohen 
1357400e64dfSOhad Ben-Cohen /*
13585e6533f7SSarangdhar Joshi  * take a firmware and boot it up.
1359400e64dfSOhad Ben-Cohen  *
1360400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
1361400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
1362400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
1363400e64dfSOhad Ben-Cohen  * that might be cleaner).
1364400e64dfSOhad Ben-Cohen  */
13655e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
1366400e64dfSOhad Ben-Cohen {
1367400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
1368a2b950acSOhad Ben-Cohen 
13692bfc311aSBjorn Andersson 	rproc_boot(rproc);
1370ddf71187SBjorn Andersson 
1371400e64dfSOhad Ben-Cohen 	release_firmware(fw);
1372400e64dfSOhad Ben-Cohen }
1373400e64dfSOhad Ben-Cohen 
13745e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc)
137570b85ef8SFernando Guzman Lugo {
137670b85ef8SFernando Guzman Lugo 	int ret;
137770b85ef8SFernando Guzman Lugo 
137870b85ef8SFernando Guzman Lugo 	/*
137970b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
138070b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
138170b85ef8SFernando Guzman Lugo 	 */
138270b85ef8SFernando Guzman Lugo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
138370b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
13845e6533f7SSarangdhar Joshi 				      rproc, rproc_auto_boot_callback);
13852099c77dSSarangdhar Joshi 	if (ret < 0)
138670b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
138770b85ef8SFernando Guzman Lugo 
138870b85ef8SFernando Guzman Lugo 	return ret;
138970b85ef8SFernando Guzman Lugo }
139070b85ef8SFernando Guzman Lugo 
1391880f5b38SBjorn Andersson static int rproc_stop(struct rproc *rproc, bool crashed)
13921efa30d0SSarangdhar Joshi {
13931efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
13941efa30d0SSarangdhar Joshi 	int ret;
13951efa30d0SSarangdhar Joshi 
1396618fcff3SBjorn Andersson 	/* Stop any subdevices for the remote processor */
1397618fcff3SBjorn Andersson 	rproc_stop_subdevices(rproc, crashed);
13981efa30d0SSarangdhar Joshi 
13990a8b81cbSBjorn Andersson 	/* the installed resource table is no longer accessible */
14000a8b81cbSBjorn Andersson 	rproc->table_ptr = rproc->cached_table;
14010a8b81cbSBjorn Andersson 
14021efa30d0SSarangdhar Joshi 	/* power off the remote processor */
14031efa30d0SSarangdhar Joshi 	ret = rproc->ops->stop(rproc);
14041efa30d0SSarangdhar Joshi 	if (ret) {
14051efa30d0SSarangdhar Joshi 		dev_err(dev, "can't stop rproc: %d\n", ret);
14061efa30d0SSarangdhar Joshi 		return ret;
14071efa30d0SSarangdhar Joshi 	}
14081efa30d0SSarangdhar Joshi 
1409c455daa4SBjorn Andersson 	rproc_unprepare_subdevices(rproc);
1410c455daa4SBjorn Andersson 
14111efa30d0SSarangdhar Joshi 	rproc->state = RPROC_OFFLINE;
14121efa30d0SSarangdhar Joshi 
14131efa30d0SSarangdhar Joshi 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
14141efa30d0SSarangdhar Joshi 
14151efa30d0SSarangdhar Joshi 	return 0;
14161efa30d0SSarangdhar Joshi }
14171efa30d0SSarangdhar Joshi 
141870b85ef8SFernando Guzman Lugo /**
14192666ca91SSarangdhar Joshi  * rproc_coredump_add_segment() - add segment of device memory to coredump
14202666ca91SSarangdhar Joshi  * @rproc:	handle of a remote processor
14212666ca91SSarangdhar Joshi  * @da:		device address
14222666ca91SSarangdhar Joshi  * @size:	size of segment
14232666ca91SSarangdhar Joshi  *
14242666ca91SSarangdhar Joshi  * Add device memory to the list of segments to be included in a coredump for
14252666ca91SSarangdhar Joshi  * the remoteproc.
14262666ca91SSarangdhar Joshi  *
14272666ca91SSarangdhar Joshi  * Return: 0 on success, negative errno on error.
14282666ca91SSarangdhar Joshi  */
14292666ca91SSarangdhar Joshi int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size)
14302666ca91SSarangdhar Joshi {
14312666ca91SSarangdhar Joshi 	struct rproc_dump_segment *segment;
14322666ca91SSarangdhar Joshi 
14332666ca91SSarangdhar Joshi 	segment = kzalloc(sizeof(*segment), GFP_KERNEL);
14342666ca91SSarangdhar Joshi 	if (!segment)
14352666ca91SSarangdhar Joshi 		return -ENOMEM;
14362666ca91SSarangdhar Joshi 
14372666ca91SSarangdhar Joshi 	segment->da = da;
14382666ca91SSarangdhar Joshi 	segment->size = size;
14392666ca91SSarangdhar Joshi 
14402666ca91SSarangdhar Joshi 	list_add_tail(&segment->node, &rproc->dump_segments);
14412666ca91SSarangdhar Joshi 
14422666ca91SSarangdhar Joshi 	return 0;
14432666ca91SSarangdhar Joshi }
14442666ca91SSarangdhar Joshi EXPORT_SYMBOL(rproc_coredump_add_segment);
14452666ca91SSarangdhar Joshi 
14462666ca91SSarangdhar Joshi /**
14472666ca91SSarangdhar Joshi  * rproc_coredump() - perform coredump
14482666ca91SSarangdhar Joshi  * @rproc:	rproc handle
14492666ca91SSarangdhar Joshi  *
14502666ca91SSarangdhar Joshi  * This function will generate an ELF header for the registered segments
14512666ca91SSarangdhar Joshi  * and create a devcoredump device associated with rproc.
14522666ca91SSarangdhar Joshi  */
14532666ca91SSarangdhar Joshi static void rproc_coredump(struct rproc *rproc)
14542666ca91SSarangdhar Joshi {
14552666ca91SSarangdhar Joshi 	struct rproc_dump_segment *segment;
14562666ca91SSarangdhar Joshi 	struct elf32_phdr *phdr;
14572666ca91SSarangdhar Joshi 	struct elf32_hdr *ehdr;
14582666ca91SSarangdhar Joshi 	size_t data_size;
14592666ca91SSarangdhar Joshi 	size_t offset;
14602666ca91SSarangdhar Joshi 	void *data;
14612666ca91SSarangdhar Joshi 	void *ptr;
14622666ca91SSarangdhar Joshi 	int phnum = 0;
14632666ca91SSarangdhar Joshi 
14642666ca91SSarangdhar Joshi 	if (list_empty(&rproc->dump_segments))
14652666ca91SSarangdhar Joshi 		return;
14662666ca91SSarangdhar Joshi 
14672666ca91SSarangdhar Joshi 	data_size = sizeof(*ehdr);
14682666ca91SSarangdhar Joshi 	list_for_each_entry(segment, &rproc->dump_segments, node) {
14692666ca91SSarangdhar Joshi 		data_size += sizeof(*phdr) + segment->size;
14702666ca91SSarangdhar Joshi 
14712666ca91SSarangdhar Joshi 		phnum++;
14722666ca91SSarangdhar Joshi 	}
14732666ca91SSarangdhar Joshi 
14742666ca91SSarangdhar Joshi 	data = vmalloc(data_size);
14752666ca91SSarangdhar Joshi 	if (!data)
14762666ca91SSarangdhar Joshi 		return;
14772666ca91SSarangdhar Joshi 
14782666ca91SSarangdhar Joshi 	ehdr = data;
14792666ca91SSarangdhar Joshi 
14802666ca91SSarangdhar Joshi 	memset(ehdr, 0, sizeof(*ehdr));
14812666ca91SSarangdhar Joshi 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
14822666ca91SSarangdhar Joshi 	ehdr->e_ident[EI_CLASS] = ELFCLASS32;
14832666ca91SSarangdhar Joshi 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
14842666ca91SSarangdhar Joshi 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
14852666ca91SSarangdhar Joshi 	ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
14862666ca91SSarangdhar Joshi 	ehdr->e_type = ET_CORE;
14872666ca91SSarangdhar Joshi 	ehdr->e_machine = EM_NONE;
14882666ca91SSarangdhar Joshi 	ehdr->e_version = EV_CURRENT;
14892666ca91SSarangdhar Joshi 	ehdr->e_entry = rproc->bootaddr;
14902666ca91SSarangdhar Joshi 	ehdr->e_phoff = sizeof(*ehdr);
14912666ca91SSarangdhar Joshi 	ehdr->e_ehsize = sizeof(*ehdr);
14922666ca91SSarangdhar Joshi 	ehdr->e_phentsize = sizeof(*phdr);
14932666ca91SSarangdhar Joshi 	ehdr->e_phnum = phnum;
14942666ca91SSarangdhar Joshi 
14952666ca91SSarangdhar Joshi 	phdr = data + ehdr->e_phoff;
14962666ca91SSarangdhar Joshi 	offset = ehdr->e_phoff + sizeof(*phdr) * ehdr->e_phnum;
14972666ca91SSarangdhar Joshi 	list_for_each_entry(segment, &rproc->dump_segments, node) {
14982666ca91SSarangdhar Joshi 		memset(phdr, 0, sizeof(*phdr));
14992666ca91SSarangdhar Joshi 		phdr->p_type = PT_LOAD;
15002666ca91SSarangdhar Joshi 		phdr->p_offset = offset;
15012666ca91SSarangdhar Joshi 		phdr->p_vaddr = segment->da;
15022666ca91SSarangdhar Joshi 		phdr->p_paddr = segment->da;
15032666ca91SSarangdhar Joshi 		phdr->p_filesz = segment->size;
15042666ca91SSarangdhar Joshi 		phdr->p_memsz = segment->size;
15052666ca91SSarangdhar Joshi 		phdr->p_flags = PF_R | PF_W | PF_X;
15062666ca91SSarangdhar Joshi 		phdr->p_align = 0;
15072666ca91SSarangdhar Joshi 
15082666ca91SSarangdhar Joshi 		ptr = rproc_da_to_va(rproc, segment->da, segment->size);
15092666ca91SSarangdhar Joshi 		if (!ptr) {
15102666ca91SSarangdhar Joshi 			dev_err(&rproc->dev,
15112666ca91SSarangdhar Joshi 				"invalid coredump segment (%pad, %zu)\n",
15122666ca91SSarangdhar Joshi 				&segment->da, segment->size);
15132666ca91SSarangdhar Joshi 			memset(data + offset, 0xff, segment->size);
15142666ca91SSarangdhar Joshi 		} else {
15152666ca91SSarangdhar Joshi 			memcpy(data + offset, ptr, segment->size);
15162666ca91SSarangdhar Joshi 		}
15172666ca91SSarangdhar Joshi 
15182666ca91SSarangdhar Joshi 		offset += phdr->p_filesz;
15192666ca91SSarangdhar Joshi 		phdr++;
15202666ca91SSarangdhar Joshi 	}
15212666ca91SSarangdhar Joshi 
15222666ca91SSarangdhar Joshi 	dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL);
15232666ca91SSarangdhar Joshi }
15242666ca91SSarangdhar Joshi 
15252666ca91SSarangdhar Joshi /**
152670b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
152770b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
152870b85ef8SFernando Guzman Lugo  *
152956324d7aSAnna, Suman  * The recovery is done by resetting all the virtio devices, that way all the
153070b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
153170b85ef8SFernando Guzman Lugo  * remoteproc functional again.
153270b85ef8SFernando Guzman Lugo  *
153370b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
153470b85ef8SFernando Guzman Lugo  */
153570b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
153670b85ef8SFernando Guzman Lugo {
15377e83cab8SSarangdhar Joshi 	const struct firmware *firmware_p;
15387e83cab8SSarangdhar Joshi 	struct device *dev = &rproc->dev;
15397e83cab8SSarangdhar Joshi 	int ret;
15407e83cab8SSarangdhar Joshi 
15417e83cab8SSarangdhar Joshi 	dev_err(dev, "recovering %s\n", rproc->name);
154270b85ef8SFernando Guzman Lugo 
15437e83cab8SSarangdhar Joshi 	ret = mutex_lock_interruptible(&rproc->lock);
15447e83cab8SSarangdhar Joshi 	if (ret)
15457e83cab8SSarangdhar Joshi 		return ret;
15467e83cab8SSarangdhar Joshi 
1547fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, true);
15487e83cab8SSarangdhar Joshi 	if (ret)
15497e83cab8SSarangdhar Joshi 		goto unlock_mutex;
1550ddf71187SBjorn Andersson 
15512666ca91SSarangdhar Joshi 	/* generate coredump */
15522666ca91SSarangdhar Joshi 	rproc_coredump(rproc);
15532666ca91SSarangdhar Joshi 
15547e83cab8SSarangdhar Joshi 	/* load firmware */
15557e83cab8SSarangdhar Joshi 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
15567e83cab8SSarangdhar Joshi 	if (ret < 0) {
15577e83cab8SSarangdhar Joshi 		dev_err(dev, "request_firmware failed: %d\n", ret);
15587e83cab8SSarangdhar Joshi 		goto unlock_mutex;
15597e83cab8SSarangdhar Joshi 	}
1560ddf71187SBjorn Andersson 
15617e83cab8SSarangdhar Joshi 	/* boot the remote processor up again */
15627e83cab8SSarangdhar Joshi 	ret = rproc_start(rproc, firmware_p);
15637e83cab8SSarangdhar Joshi 
15647e83cab8SSarangdhar Joshi 	release_firmware(firmware_p);
15657e83cab8SSarangdhar Joshi 
15667e83cab8SSarangdhar Joshi unlock_mutex:
15677e83cab8SSarangdhar Joshi 	mutex_unlock(&rproc->lock);
15687e83cab8SSarangdhar Joshi 	return ret;
156970b85ef8SFernando Guzman Lugo }
157070b85ef8SFernando Guzman Lugo 
1571400e64dfSOhad Ben-Cohen /**
15728afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
15738afd519cSFernando Guzman Lugo  *
15748afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
15758afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
15768afd519cSFernando Guzman Lugo  */
15778afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
15788afd519cSFernando Guzman Lugo {
15798afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
15808afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
15818afd519cSFernando Guzman Lugo 
15828afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
15838afd519cSFernando Guzman Lugo 
15848afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
15858afd519cSFernando Guzman Lugo 
15868afd519cSFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
15878afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
15888afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
15898afd519cSFernando Guzman Lugo 		return;
15908afd519cSFernando Guzman Lugo 	}
15918afd519cSFernando Guzman Lugo 
15928afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
15938afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
15948afd519cSFernando Guzman Lugo 		rproc->name);
15958afd519cSFernando Guzman Lugo 
15968afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
15978afd519cSFernando Guzman Lugo 
15982e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
159970b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
16008afd519cSFernando Guzman Lugo }
16018afd519cSFernando Guzman Lugo 
16028afd519cSFernando Guzman Lugo /**
16031b0ef906SSuman Anna  * rproc_boot() - boot a remote processor
1604400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1605400e64dfSOhad Ben-Cohen  *
1606400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1607400e64dfSOhad Ben-Cohen  *
1608400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1609400e64dfSOhad Ben-Cohen  * returns (successfully).
1610400e64dfSOhad Ben-Cohen  *
1611400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1612400e64dfSOhad Ben-Cohen  */
16131b0ef906SSuman Anna int rproc_boot(struct rproc *rproc)
1614400e64dfSOhad Ben-Cohen {
1615400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1616400e64dfSOhad Ben-Cohen 	struct device *dev;
1617400e64dfSOhad Ben-Cohen 	int ret;
1618400e64dfSOhad Ben-Cohen 
1619400e64dfSOhad Ben-Cohen 	if (!rproc) {
1620400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1621400e64dfSOhad Ben-Cohen 		return -EINVAL;
1622400e64dfSOhad Ben-Cohen 	}
1623400e64dfSOhad Ben-Cohen 
1624b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1625400e64dfSOhad Ben-Cohen 
1626400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1627400e64dfSOhad Ben-Cohen 	if (ret) {
1628400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1629400e64dfSOhad Ben-Cohen 		return ret;
1630400e64dfSOhad Ben-Cohen 	}
1631400e64dfSOhad Ben-Cohen 
16322099c77dSSarangdhar Joshi 	if (rproc->state == RPROC_DELETED) {
16332099c77dSSarangdhar Joshi 		ret = -ENODEV;
16342099c77dSSarangdhar Joshi 		dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
16352099c77dSSarangdhar Joshi 		goto unlock_mutex;
16362099c77dSSarangdhar Joshi 	}
16372099c77dSSarangdhar Joshi 
1638400e64dfSOhad Ben-Cohen 	/* skip the boot process if rproc is already powered up */
1639400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1640400e64dfSOhad Ben-Cohen 		ret = 0;
1641400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1642400e64dfSOhad Ben-Cohen 	}
1643400e64dfSOhad Ben-Cohen 
1644400e64dfSOhad Ben-Cohen 	dev_info(dev, "powering up %s\n", rproc->name);
1645400e64dfSOhad Ben-Cohen 
1646400e64dfSOhad Ben-Cohen 	/* load firmware */
1647400e64dfSOhad Ben-Cohen 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1648400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1649400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware failed: %d\n", ret);
1650400e64dfSOhad Ben-Cohen 		goto downref_rproc;
1651400e64dfSOhad Ben-Cohen 	}
1652400e64dfSOhad Ben-Cohen 
1653400e64dfSOhad Ben-Cohen 	ret = rproc_fw_boot(rproc, firmware_p);
1654400e64dfSOhad Ben-Cohen 
1655400e64dfSOhad Ben-Cohen 	release_firmware(firmware_p);
1656400e64dfSOhad Ben-Cohen 
1657400e64dfSOhad Ben-Cohen downref_rproc:
1658fbb6aacbSBjorn Andersson 	if (ret)
1659400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1660400e64dfSOhad Ben-Cohen unlock_mutex:
1661400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1662400e64dfSOhad Ben-Cohen 	return ret;
1663400e64dfSOhad Ben-Cohen }
1664400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1665400e64dfSOhad Ben-Cohen 
1666400e64dfSOhad Ben-Cohen /**
1667400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1668400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1669400e64dfSOhad Ben-Cohen  *
1670400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1671400e64dfSOhad Ben-Cohen  *
1672400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1673400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1674400e64dfSOhad Ben-Cohen  * without really powering off the device.
1675400e64dfSOhad Ben-Cohen  *
1676400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1677400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1678400e64dfSOhad Ben-Cohen  *
1679400e64dfSOhad Ben-Cohen  * Notes:
1680400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1681400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1682400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1683400e64dfSOhad Ben-Cohen  *   needed.
1684400e64dfSOhad Ben-Cohen  */
1685400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1686400e64dfSOhad Ben-Cohen {
1687b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1688400e64dfSOhad Ben-Cohen 	int ret;
1689400e64dfSOhad Ben-Cohen 
1690400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1691400e64dfSOhad Ben-Cohen 	if (ret) {
1692400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1693400e64dfSOhad Ben-Cohen 		return;
1694400e64dfSOhad Ben-Cohen 	}
1695400e64dfSOhad Ben-Cohen 
1696400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1697400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1698400e64dfSOhad Ben-Cohen 		goto out;
1699400e64dfSOhad Ben-Cohen 
1700fcd58037SArnaud Pouliquen 	ret = rproc_stop(rproc, false);
1701400e64dfSOhad Ben-Cohen 	if (ret) {
1702400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1703400e64dfSOhad Ben-Cohen 		goto out;
1704400e64dfSOhad Ben-Cohen 	}
1705400e64dfSOhad Ben-Cohen 
1706400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1707400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1708400e64dfSOhad Ben-Cohen 
1709400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1710400e64dfSOhad Ben-Cohen 
1711988d204cSBjorn Andersson 	/* Free the copy of the resource table */
1712a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1713a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1714988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
1715400e64dfSOhad Ben-Cohen out:
1716400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1717400e64dfSOhad Ben-Cohen }
1718400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1719400e64dfSOhad Ben-Cohen 
1720400e64dfSOhad Ben-Cohen /**
1721fec47d86SDave Gerlach  * rproc_get_by_phandle() - find a remote processor by phandle
1722fec47d86SDave Gerlach  * @phandle: phandle to the rproc
1723fec47d86SDave Gerlach  *
1724fec47d86SDave Gerlach  * Finds an rproc handle using the remote processor's phandle, and then
1725fec47d86SDave Gerlach  * return a handle to the rproc.
1726fec47d86SDave Gerlach  *
1727fec47d86SDave Gerlach  * This function increments the remote processor's refcount, so always
1728fec47d86SDave Gerlach  * use rproc_put() to decrement it back once rproc isn't needed anymore.
1729fec47d86SDave Gerlach  *
1730fec47d86SDave Gerlach  * Returns the rproc handle on success, and NULL on failure.
1731fec47d86SDave Gerlach  */
17328de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF
1733fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle)
1734fec47d86SDave Gerlach {
1735fec47d86SDave Gerlach 	struct rproc *rproc = NULL, *r;
1736fec47d86SDave Gerlach 	struct device_node *np;
1737fec47d86SDave Gerlach 
1738fec47d86SDave Gerlach 	np = of_find_node_by_phandle(phandle);
1739fec47d86SDave Gerlach 	if (!np)
1740fec47d86SDave Gerlach 		return NULL;
1741fec47d86SDave Gerlach 
1742fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1743fec47d86SDave Gerlach 	list_for_each_entry(r, &rproc_list, node) {
1744fec47d86SDave Gerlach 		if (r->dev.parent && r->dev.parent->of_node == np) {
1745fbb6aacbSBjorn Andersson 			/* prevent underlying implementation from being removed */
1746fbb6aacbSBjorn Andersson 			if (!try_module_get(r->dev.parent->driver->owner)) {
1747fbb6aacbSBjorn Andersson 				dev_err(&r->dev, "can't get owner\n");
1748fbb6aacbSBjorn Andersson 				break;
1749fbb6aacbSBjorn Andersson 			}
1750fbb6aacbSBjorn Andersson 
1751fec47d86SDave Gerlach 			rproc = r;
1752fec47d86SDave Gerlach 			get_device(&rproc->dev);
1753fec47d86SDave Gerlach 			break;
1754fec47d86SDave Gerlach 		}
1755fec47d86SDave Gerlach 	}
1756fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1757fec47d86SDave Gerlach 
1758fec47d86SDave Gerlach 	of_node_put(np);
1759fec47d86SDave Gerlach 
1760fec47d86SDave Gerlach 	return rproc;
1761fec47d86SDave Gerlach }
17628de3dbd0SOhad Ben-Cohen #else
17638de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle)
17648de3dbd0SOhad Ben-Cohen {
17658de3dbd0SOhad Ben-Cohen 	return NULL;
17668de3dbd0SOhad Ben-Cohen }
17678de3dbd0SOhad Ben-Cohen #endif
1768fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle);
1769fec47d86SDave Gerlach 
1770fec47d86SDave Gerlach /**
1771160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
1772400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
1773400e64dfSOhad Ben-Cohen  *
1774400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
1775400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
1776400e64dfSOhad Ben-Cohen  *
1777400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
1778400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
1779400e64dfSOhad Ben-Cohen  *
1780400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
1781400e64dfSOhad Ben-Cohen  *
1782400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
1783400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
1784400e64dfSOhad Ben-Cohen  * firmware.
1785400e64dfSOhad Ben-Cohen  *
1786400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
17877a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
1788400e64dfSOhad Ben-Cohen  * probed.
1789400e64dfSOhad Ben-Cohen  */
1790160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
1791400e64dfSOhad Ben-Cohen {
1792b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
179370b85ef8SFernando Guzman Lugo 	int ret;
1794400e64dfSOhad Ben-Cohen 
1795b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
1796b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
1797b5ab5e24SOhad Ben-Cohen 		return ret;
1798400e64dfSOhad Ben-Cohen 
1799b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
1800400e64dfSOhad Ben-Cohen 
1801400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
1802400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
18037a20c64dSSarangdhar Joshi 
18047a20c64dSSarangdhar Joshi 	/* if rproc is marked always-on, request it to boot */
18057a20c64dSSarangdhar Joshi 	if (rproc->auto_boot) {
18065e6533f7SSarangdhar Joshi 		ret = rproc_trigger_auto_boot(rproc);
1807d2e12e66SDave Gerlach 		if (ret < 0)
1808d2e12e66SDave Gerlach 			return ret;
18097a20c64dSSarangdhar Joshi 	}
1810400e64dfSOhad Ben-Cohen 
1811d2e12e66SDave Gerlach 	/* expose to rproc_get_by_phandle users */
1812d2e12e66SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1813d2e12e66SDave Gerlach 	list_add(&rproc->node, &rproc_list);
1814d2e12e66SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1815d2e12e66SDave Gerlach 
1816d2e12e66SDave Gerlach 	return 0;
1817400e64dfSOhad Ben-Cohen }
1818160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
1819400e64dfSOhad Ben-Cohen 
1820400e64dfSOhad Ben-Cohen /**
1821b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
1822b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
1823b5ab5e24SOhad Ben-Cohen  *
1824b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
1825b5ab5e24SOhad Ben-Cohen  *
1826b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
1827b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
1828b5ab5e24SOhad Ben-Cohen  */
1829b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
1830b5ab5e24SOhad Ben-Cohen {
1831b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
1832b5ab5e24SOhad Ben-Cohen 
18337183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
18347183a2a7SOhad Ben-Cohen 
1835b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
1836b5ab5e24SOhad Ben-Cohen 
1837b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
1838b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
1839b5ab5e24SOhad Ben-Cohen 
18400f57dc6aSMatt Redfearn 	kfree(rproc->firmware);
1841fb98e2bdSBjorn Andersson 	kfree(rproc->ops);
1842b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
1843b5ab5e24SOhad Ben-Cohen }
1844b5ab5e24SOhad Ben-Cohen 
1845c42ca04dSBhumika Goyal static const struct device_type rproc_type = {
1846b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
1847b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
1848b5ab5e24SOhad Ben-Cohen };
1849400e64dfSOhad Ben-Cohen 
1850400e64dfSOhad Ben-Cohen /**
1851400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
1852400e64dfSOhad Ben-Cohen  * @dev: the underlying device
1853400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
1854400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
18558b4aec9aSRobert Tivy  * @firmware: name of firmware file to load, can be NULL
1856400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
1857400e64dfSOhad Ben-Cohen  *
1858400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
18598b4aec9aSRobert Tivy  * it yet. if @firmware is NULL, a default name is used.
1860400e64dfSOhad Ben-Cohen  *
1861400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
1862400e64dfSOhad Ben-Cohen  * of the remote processor.
1863400e64dfSOhad Ben-Cohen  *
1864400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
1865160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
1866400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
1867400e64dfSOhad Ben-Cohen  *
1868400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
1869400e64dfSOhad Ben-Cohen  *
1870400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
1871433c0e04SBjorn Andersson  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
1872400e64dfSOhad Ben-Cohen  */
1873400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
1874400e64dfSOhad Ben-Cohen 			  const struct rproc_ops *ops,
1875400e64dfSOhad Ben-Cohen 			  const char *firmware, int len)
1876400e64dfSOhad Ben-Cohen {
1877400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
18788b4aec9aSRobert Tivy 	char *p, *template = "rproc-%s-fw";
18790f57dc6aSMatt Redfearn 	int name_len;
1880400e64dfSOhad Ben-Cohen 
1881400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
1882400e64dfSOhad Ben-Cohen 		return NULL;
1883400e64dfSOhad Ben-Cohen 
18840f57dc6aSMatt Redfearn 	if (!firmware) {
18858b4aec9aSRobert Tivy 		/*
18868b4aec9aSRobert Tivy 		 * If the caller didn't pass in a firmware name then
18870f57dc6aSMatt Redfearn 		 * construct a default name.
18888b4aec9aSRobert Tivy 		 */
18898b4aec9aSRobert Tivy 		name_len = strlen(name) + strlen(template) - 2 + 1;
18900f57dc6aSMatt Redfearn 		p = kmalloc(name_len, GFP_KERNEL);
18910f57dc6aSMatt Redfearn 		if (!p)
1892400e64dfSOhad Ben-Cohen 			return NULL;
18938b4aec9aSRobert Tivy 		snprintf(p, name_len, template, name);
18948b4aec9aSRobert Tivy 	} else {
18950f57dc6aSMatt Redfearn 		p = kstrdup(firmware, GFP_KERNEL);
18960f57dc6aSMatt Redfearn 		if (!p)
18970f57dc6aSMatt Redfearn 			return NULL;
18980f57dc6aSMatt Redfearn 	}
18990f57dc6aSMatt Redfearn 
19000f57dc6aSMatt Redfearn 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
19010f57dc6aSMatt Redfearn 	if (!rproc) {
19020f57dc6aSMatt Redfearn 		kfree(p);
19030f57dc6aSMatt Redfearn 		return NULL;
19048b4aec9aSRobert Tivy 	}
19058b4aec9aSRobert Tivy 
1906fb98e2bdSBjorn Andersson 	rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
1907fb98e2bdSBjorn Andersson 	if (!rproc->ops) {
1908fb98e2bdSBjorn Andersson 		kfree(p);
1909fb98e2bdSBjorn Andersson 		kfree(rproc);
1910fb98e2bdSBjorn Andersson 		return NULL;
1911fb98e2bdSBjorn Andersson 	}
1912fb98e2bdSBjorn Andersson 
19138b4aec9aSRobert Tivy 	rproc->firmware = p;
1914400e64dfSOhad Ben-Cohen 	rproc->name = name;
1915400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
1916ddf71187SBjorn Andersson 	rproc->auto_boot = true;
1917400e64dfSOhad Ben-Cohen 
1918b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
1919b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
1920b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
19212aefbef0SMatt Redfearn 	rproc->dev.class = &rproc_class;
19227c89717fSBjorn Andersson 	rproc->dev.driver_data = rproc;
1923b5ab5e24SOhad Ben-Cohen 
1924b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
1925b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1926b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
1927b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1928b5ab5e24SOhad Ben-Cohen 		put_device(&rproc->dev);
1929b5ab5e24SOhad Ben-Cohen 		return NULL;
1930b5ab5e24SOhad Ben-Cohen 	}
1931b5ab5e24SOhad Ben-Cohen 
1932b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1933b5ab5e24SOhad Ben-Cohen 
1934400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
1935400e64dfSOhad Ben-Cohen 
19360f21f9ccSBjorn Andersson 	/* Default to ELF loader if no load function is specified */
19370f21f9ccSBjorn Andersson 	if (!rproc->ops->load) {
19380f21f9ccSBjorn Andersson 		rproc->ops->load = rproc_elf_load_segments;
1939c1d35c1aSBjorn Andersson 		rproc->ops->parse_fw = rproc_elf_load_rsc_table;
19400f21f9ccSBjorn Andersson 		rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
19410f21f9ccSBjorn Andersson 		rproc->ops->sanity_check = rproc_elf_sanity_check;
19420f21f9ccSBjorn Andersson 		rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
19430f21f9ccSBjorn Andersson 	}
1944400e64dfSOhad Ben-Cohen 
1945400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
1946400e64dfSOhad Ben-Cohen 
19477a186941SOhad Ben-Cohen 	idr_init(&rproc->notifyids);
19487a186941SOhad Ben-Cohen 
1949400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
1950400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
1951400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
19527a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
19537bdc9650SBjorn Andersson 	INIT_LIST_HEAD(&rproc->subdevs);
19542666ca91SSarangdhar Joshi 	INIT_LIST_HEAD(&rproc->dump_segments);
1955400e64dfSOhad Ben-Cohen 
19568afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
19578afd519cSFernando Guzman Lugo 
1958400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1959400e64dfSOhad Ben-Cohen 
1960400e64dfSOhad Ben-Cohen 	return rproc;
1961400e64dfSOhad Ben-Cohen }
1962400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
1963400e64dfSOhad Ben-Cohen 
1964400e64dfSOhad Ben-Cohen /**
1965433c0e04SBjorn Andersson  * rproc_free() - unroll rproc_alloc()
1966433c0e04SBjorn Andersson  * @rproc: the remote processor handle
1967433c0e04SBjorn Andersson  *
1968433c0e04SBjorn Andersson  * This function decrements the rproc dev refcount.
1969433c0e04SBjorn Andersson  *
1970433c0e04SBjorn Andersson  * If no one holds any reference to rproc anymore, then its refcount would
1971433c0e04SBjorn Andersson  * now drop to zero, and it would be freed.
1972433c0e04SBjorn Andersson  */
1973433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc)
1974433c0e04SBjorn Andersson {
1975433c0e04SBjorn Andersson 	put_device(&rproc->dev);
1976433c0e04SBjorn Andersson }
1977433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free);
1978433c0e04SBjorn Andersson 
1979433c0e04SBjorn Andersson /**
1980433c0e04SBjorn Andersson  * rproc_put() - release rproc reference
1981400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
1982400e64dfSOhad Ben-Cohen  *
1983c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
1984400e64dfSOhad Ben-Cohen  *
1985c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
1986c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
1987400e64dfSOhad Ben-Cohen  */
1988160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
1989400e64dfSOhad Ben-Cohen {
1990fbb6aacbSBjorn Andersson 	module_put(rproc->dev.parent->driver->owner);
1991b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
1992400e64dfSOhad Ben-Cohen }
1993160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
1994400e64dfSOhad Ben-Cohen 
1995400e64dfSOhad Ben-Cohen /**
1996160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
1997400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
1998400e64dfSOhad Ben-Cohen  *
1999400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
2000400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
2001160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
2002400e64dfSOhad Ben-Cohen  * has completed successfully.
2003400e64dfSOhad Ben-Cohen  *
2004160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
2005c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
2006433c0e04SBjorn Andersson  * one last refcount, one still needs to call rproc_free().
2007400e64dfSOhad Ben-Cohen  *
2008400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
2009400e64dfSOhad Ben-Cohen  */
2010160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
2011400e64dfSOhad Ben-Cohen {
2012400e64dfSOhad Ben-Cohen 	if (!rproc)
2013400e64dfSOhad Ben-Cohen 		return -EINVAL;
2014400e64dfSOhad Ben-Cohen 
2015ddf71187SBjorn Andersson 	/* if rproc is marked always-on, rproc_add() booted it */
2016ddf71187SBjorn Andersson 	/* TODO: make sure this works with rproc->power > 1 */
2017ddf71187SBjorn Andersson 	if (rproc->auto_boot)
2018ddf71187SBjorn Andersson 		rproc_shutdown(rproc);
2019ddf71187SBjorn Andersson 
20202099c77dSSarangdhar Joshi 	mutex_lock(&rproc->lock);
20212099c77dSSarangdhar Joshi 	rproc->state = RPROC_DELETED;
20222099c77dSSarangdhar Joshi 	mutex_unlock(&rproc->lock);
20232099c77dSSarangdhar Joshi 
2024b003d45bSSarangdhar Joshi 	rproc_delete_debug_dir(rproc);
2025b003d45bSSarangdhar Joshi 
2026fec47d86SDave Gerlach 	/* the rproc is downref'ed as soon as it's removed from the klist */
2027fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
2028fec47d86SDave Gerlach 	list_del(&rproc->node);
2029fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
2030fec47d86SDave Gerlach 
2031b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
2032400e64dfSOhad Ben-Cohen 
2033400e64dfSOhad Ben-Cohen 	return 0;
2034400e64dfSOhad Ben-Cohen }
2035160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
2036400e64dfSOhad Ben-Cohen 
20378afd519cSFernando Guzman Lugo /**
20387bdc9650SBjorn Andersson  * rproc_add_subdev() - add a subdevice to a remoteproc
20397bdc9650SBjorn Andersson  * @rproc: rproc handle to add the subdevice to
20407bdc9650SBjorn Andersson  * @subdev: subdev handle to register
20414902676fSBjorn Andersson  *
20424902676fSBjorn Andersson  * Caller is responsible for populating optional subdevice function pointers.
20437bdc9650SBjorn Andersson  */
20444902676fSBjorn Andersson void rproc_add_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
20457bdc9650SBjorn Andersson {
20467bdc9650SBjorn Andersson 	list_add_tail(&subdev->node, &rproc->subdevs);
20477bdc9650SBjorn Andersson }
20487bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev);
20497bdc9650SBjorn Andersson 
20507bdc9650SBjorn Andersson /**
20517bdc9650SBjorn Andersson  * rproc_remove_subdev() - remove a subdevice from a remoteproc
20527bdc9650SBjorn Andersson  * @rproc: rproc handle to remove the subdevice from
20537bdc9650SBjorn Andersson  * @subdev: subdev handle, previously registered with rproc_add_subdev()
20547bdc9650SBjorn Andersson  */
20557bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
20567bdc9650SBjorn Andersson {
20577bdc9650SBjorn Andersson 	list_del(&subdev->node);
20587bdc9650SBjorn Andersson }
20597bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev);
20607bdc9650SBjorn Andersson 
20617bdc9650SBjorn Andersson /**
20627c89717fSBjorn Andersson  * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
20637c89717fSBjorn Andersson  * @dev:	child device to find ancestor of
20647c89717fSBjorn Andersson  *
20657c89717fSBjorn Andersson  * Returns the ancestor rproc instance, or NULL if not found.
20667c89717fSBjorn Andersson  */
20677c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev)
20687c89717fSBjorn Andersson {
20697c89717fSBjorn Andersson 	for (dev = dev->parent; dev; dev = dev->parent) {
20707c89717fSBjorn Andersson 		if (dev->type == &rproc_type)
20717c89717fSBjorn Andersson 			return dev->driver_data;
20727c89717fSBjorn Andersson 	}
20737c89717fSBjorn Andersson 
20747c89717fSBjorn Andersson 	return NULL;
20757c89717fSBjorn Andersson }
20767c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child);
20777c89717fSBjorn Andersson 
20787c89717fSBjorn Andersson /**
20798afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
20808afd519cSFernando Guzman Lugo  * @rproc: remote processor
20818afd519cSFernando Guzman Lugo  * @type: crash type
20828afd519cSFernando Guzman Lugo  *
20838afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
20848afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
20858afd519cSFernando Guzman Lugo  * non-remoteproc driver.
20868afd519cSFernando Guzman Lugo  *
20878afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
20888afd519cSFernando Guzman Lugo  */
20898afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
20908afd519cSFernando Guzman Lugo {
20918afd519cSFernando Guzman Lugo 	if (!rproc) {
20928afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
20938afd519cSFernando Guzman Lugo 		return;
20948afd519cSFernando Guzman Lugo 	}
20958afd519cSFernando Guzman Lugo 
20968afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
20978afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
20988afd519cSFernando Guzman Lugo 
20998afd519cSFernando Guzman Lugo 	/* create a new task to handle the error */
21008afd519cSFernando Guzman Lugo 	schedule_work(&rproc->crash_handler);
21018afd519cSFernando Guzman Lugo }
21028afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
21038afd519cSFernando Guzman Lugo 
2104400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
2105400e64dfSOhad Ben-Cohen {
21062aefbef0SMatt Redfearn 	rproc_init_sysfs();
2107400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
2108b5ab5e24SOhad Ben-Cohen 
2109400e64dfSOhad Ben-Cohen 	return 0;
2110400e64dfSOhad Ben-Cohen }
2111400e64dfSOhad Ben-Cohen module_init(remoteproc_init);
2112400e64dfSOhad Ben-Cohen 
2113400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
2114400e64dfSOhad Ben-Cohen {
2115f42f79afSSuman Anna 	ida_destroy(&rproc_dev_index);
2116f42f79afSSuman Anna 
2117400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
21182aefbef0SMatt Redfearn 	rproc_exit_sysfs();
2119400e64dfSOhad Ben-Cohen }
2120400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
2121400e64dfSOhad Ben-Cohen 
2122400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
2123400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
2124