xref: /linux/drivers/remoteproc/remoteproc_core.c (revision 2666ca9197e3d352f43b02d7dfb7c6dd72e7c614)
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>
36*2666ca91SSarangdhar 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 
143a01f7cd6SSuman Anna /**
144a01f7cd6SSuman Anna  * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
145a01f7cd6SSuman Anna  * @rproc: handle of a remote processor
146a01f7cd6SSuman Anna  * @da: remoteproc device address to translate
147a01f7cd6SSuman Anna  * @len: length of the memory region @da is pointing to
148a01f7cd6SSuman Anna  *
149400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
150400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
151a01f7cd6SSuman Anna  * device addresses (which are hardcoded in the firmware). They may also have
152a01f7cd6SSuman Anna  * dedicated memory regions internal to the processors, and use them either
153a01f7cd6SSuman Anna  * exclusively or alongside carveouts.
154400e64dfSOhad Ben-Cohen  *
155400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
156400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
157400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
158400e64dfSOhad Ben-Cohen  *
159a01f7cd6SSuman Anna  * This function is a helper function with which we can go over the allocated
160a01f7cd6SSuman Anna  * carveouts and translate specific device addresses to kernel virtual addresses
161a01f7cd6SSuman Anna  * so we can access the referenced memory. This function also allows to perform
162a01f7cd6SSuman Anna  * translations on the internal remoteproc memory regions through a platform
163a01f7cd6SSuman Anna  * implementation specific da_to_va ops, if present.
164a01f7cd6SSuman Anna  *
165a01f7cd6SSuman Anna  * The function returns a valid kernel address on success or NULL on failure.
166400e64dfSOhad Ben-Cohen  *
167400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
168400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
169a01f7cd6SSuman Anna  * here the output of the DMA API for the carveouts, which should be more
170a01f7cd6SSuman Anna  * correct.
171400e64dfSOhad Ben-Cohen  */
17272854fb0SSjur Brændeland void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
173400e64dfSOhad Ben-Cohen {
174400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
175400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
176400e64dfSOhad Ben-Cohen 
177a01f7cd6SSuman Anna 	if (rproc->ops->da_to_va) {
178a01f7cd6SSuman Anna 		ptr = rproc->ops->da_to_va(rproc, da, len);
179a01f7cd6SSuman Anna 		if (ptr)
180a01f7cd6SSuman Anna 			goto out;
181a01f7cd6SSuman Anna 	}
182a01f7cd6SSuman Anna 
183400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
184400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
185400e64dfSOhad Ben-Cohen 
186400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
187400e64dfSOhad Ben-Cohen 		if (offset < 0)
188400e64dfSOhad Ben-Cohen 			continue;
189400e64dfSOhad Ben-Cohen 
190400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
191400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
192400e64dfSOhad Ben-Cohen 			continue;
193400e64dfSOhad Ben-Cohen 
194400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
195400e64dfSOhad Ben-Cohen 
196400e64dfSOhad Ben-Cohen 		break;
197400e64dfSOhad Ben-Cohen 	}
198400e64dfSOhad Ben-Cohen 
199a01f7cd6SSuman Anna out:
200400e64dfSOhad Ben-Cohen 	return ptr;
201400e64dfSOhad Ben-Cohen }
2024afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
203400e64dfSOhad Ben-Cohen 
2046db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
205400e64dfSOhad Ben-Cohen {
2067a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
207b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
2086db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
209c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
2107a186941SOhad Ben-Cohen 	dma_addr_t dma;
2117a186941SOhad Ben-Cohen 	void *va;
2127a186941SOhad Ben-Cohen 	int ret, size, notifyid;
213400e64dfSOhad Ben-Cohen 
2146db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
2156db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2166db20ea8SOhad Ben-Cohen 
2176db20ea8SOhad Ben-Cohen 	/*
2186db20ea8SOhad Ben-Cohen 	 * Allocate non-cacheable memory for the vring. In the future
2196db20ea8SOhad Ben-Cohen 	 * this call will also configure the IOMMU for us
2206db20ea8SOhad Ben-Cohen 	 */
221b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
2226db20ea8SOhad Ben-Cohen 	if (!va) {
223b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent failed\n");
2246db20ea8SOhad Ben-Cohen 		return -EINVAL;
2256db20ea8SOhad Ben-Cohen 	}
2266db20ea8SOhad Ben-Cohen 
2276db20ea8SOhad Ben-Cohen 	/*
2286db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
2296db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
2306db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
2316db20ea8SOhad Ben-Cohen 	 */
23215fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
233b39599b7SSuman Anna 	if (ret < 0) {
23415fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
235b5ab5e24SOhad Ben-Cohen 		dma_free_coherent(dev->parent, size, va, dma);
2366db20ea8SOhad Ben-Cohen 		return ret;
2376db20ea8SOhad Ben-Cohen 	}
23815fc6110STejun Heo 	notifyid = ret;
2396db20ea8SOhad Ben-Cohen 
24048f18f89SBjorn Andersson 	/* Potentially bump max_notifyid */
24148f18f89SBjorn Andersson 	if (notifyid > rproc->max_notifyid)
24248f18f89SBjorn Andersson 		rproc->max_notifyid = notifyid;
24348f18f89SBjorn Andersson 
2449d7814a9SAnna, Suman 	dev_dbg(dev, "vring%d: va %p dma %pad size 0x%x idr %d\n",
245b605ed8bSAnna, Suman 		i, va, &dma, size, notifyid);
2466db20ea8SOhad Ben-Cohen 
2476db20ea8SOhad Ben-Cohen 	rvring->va = va;
2486db20ea8SOhad Ben-Cohen 	rvring->dma = dma;
2496db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
2506db20ea8SOhad Ben-Cohen 
251c0d63157SSjur Brændeland 	/*
252c0d63157SSjur Brændeland 	 * Let the rproc know the notifyid and da of this vring.
253c0d63157SSjur Brændeland 	 * Not all platforms use dma_alloc_coherent to automatically
254c0d63157SSjur Brændeland 	 * set up the iommu. In this case the device address (da) will
255c0d63157SSjur Brændeland 	 * hold the physical address and not the device address.
256c0d63157SSjur Brændeland 	 */
257c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
258c0d63157SSjur Brændeland 	rsc->vring[i].da = dma;
259c0d63157SSjur Brændeland 	rsc->vring[i].notifyid = notifyid;
2606db20ea8SOhad Ben-Cohen 	return 0;
2616db20ea8SOhad Ben-Cohen }
2626db20ea8SOhad Ben-Cohen 
263400e64dfSOhad Ben-Cohen static int
2646db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
265400e64dfSOhad Ben-Cohen {
266400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
267b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
268400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
2696db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
270400e64dfSOhad Ben-Cohen 
2719d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
2727a186941SOhad Ben-Cohen 		i, vring->da, vring->num, vring->align);
2737a186941SOhad Ben-Cohen 
27463140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
27563140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
27663140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
27763140e0eSOhad Ben-Cohen 			vring->num, vring->align);
278400e64dfSOhad Ben-Cohen 		return -EINVAL;
279400e64dfSOhad Ben-Cohen 	}
280400e64dfSOhad Ben-Cohen 
2816db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
2826db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
2836db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
284400e64dfSOhad Ben-Cohen 
285400e64dfSOhad Ben-Cohen 	return 0;
286400e64dfSOhad Ben-Cohen }
287400e64dfSOhad Ben-Cohen 
2886db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
2897a186941SOhad Ben-Cohen {
29063140e0eSOhad Ben-Cohen 	int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2916db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
292c0d63157SSjur Brændeland 	int idx = rvring->rvdev->vring - rvring;
293c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
2947a186941SOhad Ben-Cohen 
295b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
2967a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
297099a3f33SSjur Brændeland 
298c0d63157SSjur Brændeland 	/* reset resource entry info */
299c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
300c0d63157SSjur Brændeland 	rsc->vring[idx].da = 0;
301c0d63157SSjur Brændeland 	rsc->vring[idx].notifyid = -1;
3027a186941SOhad Ben-Cohen }
3037a186941SOhad Ben-Cohen 
304f5bcb353SBjorn Andersson static int rproc_vdev_do_probe(struct rproc_subdev *subdev)
305f5bcb353SBjorn Andersson {
306f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
307f5bcb353SBjorn Andersson 
308f5bcb353SBjorn Andersson 	return rproc_add_virtio_dev(rvdev, rvdev->id);
309f5bcb353SBjorn Andersson }
310f5bcb353SBjorn Andersson 
311f5bcb353SBjorn Andersson static void rproc_vdev_do_remove(struct rproc_subdev *subdev)
312f5bcb353SBjorn Andersson {
313f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
314f5bcb353SBjorn Andersson 
315f5bcb353SBjorn Andersson 	rproc_remove_virtio_dev(rvdev);
316f5bcb353SBjorn Andersson }
317f5bcb353SBjorn Andersson 
318400e64dfSOhad Ben-Cohen /**
319fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
320400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
321400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
322fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
323400e64dfSOhad Ben-Cohen  *
3247a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
3257a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
3267a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
3277a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
328400e64dfSOhad Ben-Cohen  *
3297a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
3307a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
3317a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
3327a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
3337a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
334400e64dfSOhad Ben-Cohen  *
3357a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
3367a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
3377a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
3387a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
3397a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
3407a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
3417a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
342400e64dfSOhad Ben-Cohen  *
343400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
344400e64dfSOhad Ben-Cohen  */
345fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
346a2b950acSOhad Ben-Cohen 			     int offset, int avail)
347400e64dfSOhad Ben-Cohen {
348b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3497a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
3507a186941SOhad Ben-Cohen 	int i, ret;
351fd2c15ecSOhad Ben-Cohen 
352fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
353fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
354fd2c15ecSOhad Ben-Cohen 			+ rsc->config_len > avail) {
355b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
356fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
357fd2c15ecSOhad Ben-Cohen 	}
358fd2c15ecSOhad Ben-Cohen 
359fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
360fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
361fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
362fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
363fd2c15ecSOhad Ben-Cohen 	}
364fd2c15ecSOhad Ben-Cohen 
3659d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
366fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
367400e64dfSOhad Ben-Cohen 
3687a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
3697a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
370fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
371400e64dfSOhad Ben-Cohen 		return -EINVAL;
372400e64dfSOhad Ben-Cohen 	}
373400e64dfSOhad Ben-Cohen 
374899585adSAnna, Suman 	rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
3757a186941SOhad Ben-Cohen 	if (!rvdev)
3767a186941SOhad Ben-Cohen 		return -ENOMEM;
3777a186941SOhad Ben-Cohen 
378aab8d802SBjorn Andersson 	kref_init(&rvdev->refcount);
379aab8d802SBjorn Andersson 
380f5bcb353SBjorn Andersson 	rvdev->id = rsc->id;
3817a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
3827a186941SOhad Ben-Cohen 
3836db20ea8SOhad Ben-Cohen 	/* parse the vrings */
384fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
3856db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
3867a186941SOhad Ben-Cohen 		if (ret)
3876db20ea8SOhad Ben-Cohen 			goto free_rvdev;
388fd2c15ecSOhad Ben-Cohen 	}
389fd2c15ecSOhad Ben-Cohen 
390a2b950acSOhad Ben-Cohen 	/* remember the resource offset*/
391a2b950acSOhad Ben-Cohen 	rvdev->rsc_offset = offset;
392400e64dfSOhad Ben-Cohen 
393a863af5dSBjorn Andersson 	/* allocate the vring resources */
394a863af5dSBjorn Andersson 	for (i = 0; i < rsc->num_of_vrings; i++) {
395a863af5dSBjorn Andersson 		ret = rproc_alloc_vring(rvdev, i);
396a863af5dSBjorn Andersson 		if (ret)
397a863af5dSBjorn Andersson 			goto unwind_vring_allocations;
398a863af5dSBjorn Andersson 	}
399a863af5dSBjorn Andersson 
4007a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
401400e64dfSOhad Ben-Cohen 
402f5bcb353SBjorn Andersson 	rproc_add_subdev(rproc, &rvdev->subdev,
403f5bcb353SBjorn Andersson 			 rproc_vdev_do_probe, rproc_vdev_do_remove);
404400e64dfSOhad Ben-Cohen 
405400e64dfSOhad Ben-Cohen 	return 0;
4067a186941SOhad Ben-Cohen 
407a863af5dSBjorn Andersson unwind_vring_allocations:
408a863af5dSBjorn Andersson 	for (i--; i >= 0; i--)
409a863af5dSBjorn Andersson 		rproc_free_vring(&rvdev->vring[i]);
4106db20ea8SOhad Ben-Cohen free_rvdev:
4117a186941SOhad Ben-Cohen 	kfree(rvdev);
4127a186941SOhad Ben-Cohen 	return ret;
413400e64dfSOhad Ben-Cohen }
414400e64dfSOhad Ben-Cohen 
415aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref)
416aab8d802SBjorn Andersson {
417aab8d802SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
418a863af5dSBjorn Andersson 	struct rproc_vring *rvring;
419f5bcb353SBjorn Andersson 	struct rproc *rproc = rvdev->rproc;
420a863af5dSBjorn Andersson 	int id;
421a863af5dSBjorn Andersson 
422a863af5dSBjorn Andersson 	for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
423a863af5dSBjorn Andersson 		rvring = &rvdev->vring[id];
424a863af5dSBjorn Andersson 		if (!rvring->va)
425a863af5dSBjorn Andersson 			continue;
426a863af5dSBjorn Andersson 
427a863af5dSBjorn Andersson 		rproc_free_vring(rvring);
428a863af5dSBjorn Andersson 	}
429aab8d802SBjorn Andersson 
430f5bcb353SBjorn Andersson 	rproc_remove_subdev(rproc, &rvdev->subdev);
431aab8d802SBjorn Andersson 	list_del(&rvdev->node);
432aab8d802SBjorn Andersson 	kfree(rvdev);
433aab8d802SBjorn Andersson }
434aab8d802SBjorn Andersson 
435400e64dfSOhad Ben-Cohen /**
436400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
437400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
438400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
439fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
440400e64dfSOhad Ben-Cohen  *
441400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
442400e64dfSOhad Ben-Cohen  * export it via debugfs.
443400e64dfSOhad Ben-Cohen  *
444400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
445400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
446400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
447400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
448400e64dfSOhad Ben-Cohen  *
449400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
450400e64dfSOhad Ben-Cohen  */
451fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
452a2b950acSOhad Ben-Cohen 			      int offset, int avail)
453400e64dfSOhad Ben-Cohen {
454400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *trace;
455b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
456400e64dfSOhad Ben-Cohen 	void *ptr;
457400e64dfSOhad Ben-Cohen 	char name[15];
458400e64dfSOhad Ben-Cohen 
459fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
460b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace 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) {
466fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
467fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
468fd2c15ecSOhad Ben-Cohen 	}
469fd2c15ecSOhad Ben-Cohen 
470400e64dfSOhad Ben-Cohen 	/* what's the kernel address of this resource ? */
471400e64dfSOhad Ben-Cohen 	ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
472400e64dfSOhad Ben-Cohen 	if (!ptr) {
473400e64dfSOhad Ben-Cohen 		dev_err(dev, "erroneous trace resource entry\n");
474400e64dfSOhad Ben-Cohen 		return -EINVAL;
475400e64dfSOhad Ben-Cohen 	}
476400e64dfSOhad Ben-Cohen 
477400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
478172e6ab1SSuman Anna 	if (!trace)
479400e64dfSOhad Ben-Cohen 		return -ENOMEM;
480400e64dfSOhad Ben-Cohen 
481400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
482400e64dfSOhad Ben-Cohen 	trace->len = rsc->len;
483400e64dfSOhad Ben-Cohen 	trace->va = ptr;
484400e64dfSOhad Ben-Cohen 
485400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
486400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
487400e64dfSOhad Ben-Cohen 
488400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
489400e64dfSOhad Ben-Cohen 	trace->priv = rproc_create_trace_file(name, rproc, trace);
490400e64dfSOhad Ben-Cohen 	if (!trace->priv) {
491400e64dfSOhad Ben-Cohen 		trace->va = NULL;
492400e64dfSOhad Ben-Cohen 		kfree(trace);
493400e64dfSOhad Ben-Cohen 		return -EINVAL;
494400e64dfSOhad Ben-Cohen 	}
495400e64dfSOhad Ben-Cohen 
496400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
497400e64dfSOhad Ben-Cohen 
498400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
499400e64dfSOhad Ben-Cohen 
50035386166SLee Jones 	dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n",
50135386166SLee Jones 		name, ptr, rsc->da, rsc->len);
502400e64dfSOhad Ben-Cohen 
503400e64dfSOhad Ben-Cohen 	return 0;
504400e64dfSOhad Ben-Cohen }
505400e64dfSOhad Ben-Cohen 
506400e64dfSOhad Ben-Cohen /**
507400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
508400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
509400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
510fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
511400e64dfSOhad Ben-Cohen  *
512400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
513400e64dfSOhad Ben-Cohen  *
514400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
515400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
516400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
517400e64dfSOhad Ben-Cohen  *
518400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
519400e64dfSOhad Ben-Cohen  *
520400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
521400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
522400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
523400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
524400e64dfSOhad Ben-Cohen  *
525400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
526400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
527400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
528400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
529400e64dfSOhad Ben-Cohen  * are outside those ranges.
530400e64dfSOhad Ben-Cohen  */
531fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
532a2b950acSOhad Ben-Cohen 			       int offset, int avail)
533400e64dfSOhad Ben-Cohen {
534400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
535b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
536400e64dfSOhad Ben-Cohen 	int ret;
537400e64dfSOhad Ben-Cohen 
538400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
539400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
540400e64dfSOhad Ben-Cohen 		return -EINVAL;
541400e64dfSOhad Ben-Cohen 
542fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
543b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
544fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
545fd2c15ecSOhad Ben-Cohen 	}
546fd2c15ecSOhad Ben-Cohen 
547fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
548fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
549b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
550fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
551fd2c15ecSOhad Ben-Cohen 	}
552fd2c15ecSOhad Ben-Cohen 
553400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
554172e6ab1SSuman Anna 	if (!mapping)
555400e64dfSOhad Ben-Cohen 		return -ENOMEM;
556400e64dfSOhad Ben-Cohen 
557400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
558400e64dfSOhad Ben-Cohen 	if (ret) {
559b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
560400e64dfSOhad Ben-Cohen 		goto out;
561400e64dfSOhad Ben-Cohen 	}
562400e64dfSOhad Ben-Cohen 
563400e64dfSOhad Ben-Cohen 	/*
564400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
565400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
566400e64dfSOhad Ben-Cohen 	 *
567400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
568400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
569400e64dfSOhad Ben-Cohen 	 */
570400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
571400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
572400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
573400e64dfSOhad Ben-Cohen 
574b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
575400e64dfSOhad Ben-Cohen 		rsc->pa, rsc->da, rsc->len);
576400e64dfSOhad Ben-Cohen 
577400e64dfSOhad Ben-Cohen 	return 0;
578400e64dfSOhad Ben-Cohen 
579400e64dfSOhad Ben-Cohen out:
580400e64dfSOhad Ben-Cohen 	kfree(mapping);
581400e64dfSOhad Ben-Cohen 	return ret;
582400e64dfSOhad Ben-Cohen }
583400e64dfSOhad Ben-Cohen 
584400e64dfSOhad Ben-Cohen /**
585400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
586400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
587400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
588fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
589400e64dfSOhad Ben-Cohen  *
590400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
591400e64dfSOhad Ben-Cohen  * contiguous memory regions.
592400e64dfSOhad Ben-Cohen  *
593400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
594400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
595400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
596400e64dfSOhad Ben-Cohen  *
597400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
598400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
599400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
600400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
601400e64dfSOhad Ben-Cohen  */
602fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
603a2b950acSOhad Ben-Cohen 				 struct fw_rsc_carveout *rsc,
604a2b950acSOhad Ben-Cohen 				 int offset, int avail)
605400e64dfSOhad Ben-Cohen {
606400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout, *mapping;
607b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
608400e64dfSOhad Ben-Cohen 	dma_addr_t dma;
609400e64dfSOhad Ben-Cohen 	void *va;
610400e64dfSOhad Ben-Cohen 	int ret;
611400e64dfSOhad Ben-Cohen 
612fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
613b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
614fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
615fd2c15ecSOhad Ben-Cohen 	}
616fd2c15ecSOhad Ben-Cohen 
617fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
618fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
619fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
620fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
621fd2c15ecSOhad Ben-Cohen 	}
622fd2c15ecSOhad Ben-Cohen 
6239d7814a9SAnna, Suman 	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
62435386166SLee Jones 		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
625fd2c15ecSOhad Ben-Cohen 
626400e64dfSOhad Ben-Cohen 	carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
627172e6ab1SSuman Anna 	if (!carveout)
6287168d914SDan Carpenter 		return -ENOMEM;
629400e64dfSOhad Ben-Cohen 
630b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
631400e64dfSOhad Ben-Cohen 	if (!va) {
6329c219b23SLee Jones 		dev_err(dev->parent,
6339c219b23SLee Jones 			"failed to allocate dma memory: len 0x%x\n", rsc->len);
634400e64dfSOhad Ben-Cohen 		ret = -ENOMEM;
635400e64dfSOhad Ben-Cohen 		goto free_carv;
636400e64dfSOhad Ben-Cohen 	}
637400e64dfSOhad Ben-Cohen 
638b605ed8bSAnna, Suman 	dev_dbg(dev, "carveout va %p, dma %pad, len 0x%x\n",
639b605ed8bSAnna, Suman 		va, &dma, rsc->len);
640400e64dfSOhad Ben-Cohen 
641400e64dfSOhad Ben-Cohen 	/*
642400e64dfSOhad Ben-Cohen 	 * Ok, this is non-standard.
643400e64dfSOhad Ben-Cohen 	 *
644400e64dfSOhad Ben-Cohen 	 * Sometimes we can't rely on the generic iommu-based DMA API
645400e64dfSOhad Ben-Cohen 	 * to dynamically allocate the device address and then set the IOMMU
646400e64dfSOhad Ben-Cohen 	 * tables accordingly, because some remote processors might
647400e64dfSOhad Ben-Cohen 	 * _require_ us to use hard coded device addresses that their
648400e64dfSOhad Ben-Cohen 	 * firmware was compiled with.
649400e64dfSOhad Ben-Cohen 	 *
650400e64dfSOhad Ben-Cohen 	 * In this case, we must use the IOMMU API directly and map
651400e64dfSOhad Ben-Cohen 	 * the memory to the device address as expected by the remote
652400e64dfSOhad Ben-Cohen 	 * processor.
653400e64dfSOhad Ben-Cohen 	 *
654400e64dfSOhad Ben-Cohen 	 * Obviously such remote processor devices should not be configured
655400e64dfSOhad Ben-Cohen 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
656400e64dfSOhad Ben-Cohen 	 * physical address in this case.
657400e64dfSOhad Ben-Cohen 	 */
658400e64dfSOhad Ben-Cohen 	if (rproc->domain) {
6597168d914SDan Carpenter 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
6607168d914SDan Carpenter 		if (!mapping) {
6617168d914SDan Carpenter 			ret = -ENOMEM;
6627168d914SDan Carpenter 			goto dma_free;
6637168d914SDan Carpenter 		}
6647168d914SDan Carpenter 
665400e64dfSOhad Ben-Cohen 		ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
666400e64dfSOhad Ben-Cohen 				rsc->flags);
667400e64dfSOhad Ben-Cohen 		if (ret) {
668400e64dfSOhad Ben-Cohen 			dev_err(dev, "iommu_map failed: %d\n", ret);
6697168d914SDan Carpenter 			goto free_mapping;
670400e64dfSOhad Ben-Cohen 		}
671400e64dfSOhad Ben-Cohen 
672400e64dfSOhad Ben-Cohen 		/*
673400e64dfSOhad Ben-Cohen 		 * We'll need this info later when we'll want to unmap
674400e64dfSOhad Ben-Cohen 		 * everything (e.g. on shutdown).
675400e64dfSOhad Ben-Cohen 		 *
676400e64dfSOhad Ben-Cohen 		 * We can't trust the remote processor not to change the
677400e64dfSOhad Ben-Cohen 		 * resource table, so we must maintain this info independently.
678400e64dfSOhad Ben-Cohen 		 */
679400e64dfSOhad Ben-Cohen 		mapping->da = rsc->da;
680400e64dfSOhad Ben-Cohen 		mapping->len = rsc->len;
681400e64dfSOhad Ben-Cohen 		list_add_tail(&mapping->node, &rproc->mappings);
682400e64dfSOhad Ben-Cohen 
683b605ed8bSAnna, Suman 		dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
684b605ed8bSAnna, Suman 			rsc->da, &dma);
6850e49b72cSOhad Ben-Cohen 	}
686400e64dfSOhad Ben-Cohen 
687400e64dfSOhad Ben-Cohen 	/*
688400e64dfSOhad Ben-Cohen 	 * Some remote processors might need to know the pa
689400e64dfSOhad Ben-Cohen 	 * even though they are behind an IOMMU. E.g., OMAP4's
690400e64dfSOhad Ben-Cohen 	 * remote M3 processor needs this so it can control
691400e64dfSOhad Ben-Cohen 	 * on-chip hardware accelerators that are not behind
692400e64dfSOhad Ben-Cohen 	 * the IOMMU, and therefor must know the pa.
693400e64dfSOhad Ben-Cohen 	 *
694400e64dfSOhad Ben-Cohen 	 * Generally we don't want to expose physical addresses
695400e64dfSOhad Ben-Cohen 	 * if we don't have to (remote processors are generally
696400e64dfSOhad Ben-Cohen 	 * _not_ trusted), so we might want to do this only for
697400e64dfSOhad Ben-Cohen 	 * remote processor that _must_ have this (e.g. OMAP4's
698400e64dfSOhad Ben-Cohen 	 * dual M3 subsystem).
6990e49b72cSOhad Ben-Cohen 	 *
7000e49b72cSOhad Ben-Cohen 	 * Non-IOMMU processors might also want to have this info.
7010e49b72cSOhad Ben-Cohen 	 * In this case, the device address and the physical address
7020e49b72cSOhad Ben-Cohen 	 * are the same.
703400e64dfSOhad Ben-Cohen 	 */
704400e64dfSOhad Ben-Cohen 	rsc->pa = dma;
705400e64dfSOhad Ben-Cohen 
706400e64dfSOhad Ben-Cohen 	carveout->va = va;
707400e64dfSOhad Ben-Cohen 	carveout->len = rsc->len;
708400e64dfSOhad Ben-Cohen 	carveout->dma = dma;
709400e64dfSOhad Ben-Cohen 	carveout->da = rsc->da;
710400e64dfSOhad Ben-Cohen 
711400e64dfSOhad Ben-Cohen 	list_add_tail(&carveout->node, &rproc->carveouts);
712400e64dfSOhad Ben-Cohen 
713400e64dfSOhad Ben-Cohen 	return 0;
714400e64dfSOhad Ben-Cohen 
7157168d914SDan Carpenter free_mapping:
7167168d914SDan Carpenter 	kfree(mapping);
717400e64dfSOhad Ben-Cohen dma_free:
718b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(dev->parent, rsc->len, va, dma);
719400e64dfSOhad Ben-Cohen free_carv:
720400e64dfSOhad Ben-Cohen 	kfree(carveout);
721400e64dfSOhad Ben-Cohen 	return ret;
722400e64dfSOhad Ben-Cohen }
723400e64dfSOhad Ben-Cohen 
724e12bc14bSOhad Ben-Cohen /*
725e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
726e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
727e12bc14bSOhad Ben-Cohen  */
728232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
729fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
730fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
731fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
732232fcdbbSSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
733232fcdbbSSjur Brændeland };
734232fcdbbSSjur Brændeland 
735400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
736a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc,
737232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
738400e64dfSOhad Ben-Cohen {
739b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
740e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
741fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
742400e64dfSOhad Ben-Cohen 
743d4bb86f2SBjorn Andersson 	if (!rproc->table_ptr)
744d4bb86f2SBjorn Andersson 		return 0;
745d4bb86f2SBjorn Andersson 
746a2b950acSOhad Ben-Cohen 	for (i = 0; i < rproc->table_ptr->num; i++) {
747a2b950acSOhad Ben-Cohen 		int offset = rproc->table_ptr->offset[i];
748a2b950acSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
749a4b24c75SBjorn Andersson 		int avail = rproc->table_sz - offset - sizeof(*hdr);
750fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
751400e64dfSOhad Ben-Cohen 
752fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
753fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
754fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
755fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
756fd2c15ecSOhad Ben-Cohen 		}
757fd2c15ecSOhad Ben-Cohen 
758fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
759fd2c15ecSOhad Ben-Cohen 
760fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
761fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
762e12bc14bSOhad Ben-Cohen 			continue;
763400e64dfSOhad Ben-Cohen 		}
764400e64dfSOhad Ben-Cohen 
765232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
766e12bc14bSOhad Ben-Cohen 		if (!handler)
767e12bc14bSOhad Ben-Cohen 			continue;
768e12bc14bSOhad Ben-Cohen 
769a2b950acSOhad Ben-Cohen 		ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
7707a186941SOhad Ben-Cohen 		if (ret)
771400e64dfSOhad Ben-Cohen 			break;
772400e64dfSOhad Ben-Cohen 	}
773400e64dfSOhad Ben-Cohen 
774400e64dfSOhad Ben-Cohen 	return ret;
775400e64dfSOhad Ben-Cohen }
776400e64dfSOhad Ben-Cohen 
7777bdc9650SBjorn Andersson static int rproc_probe_subdevices(struct rproc *rproc)
7787bdc9650SBjorn Andersson {
7797bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
7807bdc9650SBjorn Andersson 	int ret;
7817bdc9650SBjorn Andersson 
7827bdc9650SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
7837bdc9650SBjorn Andersson 		ret = subdev->probe(subdev);
7847bdc9650SBjorn Andersson 		if (ret)
7857bdc9650SBjorn Andersson 			goto unroll_registration;
7867bdc9650SBjorn Andersson 	}
7877bdc9650SBjorn Andersson 
7887bdc9650SBjorn Andersson 	return 0;
7897bdc9650SBjorn Andersson 
7907bdc9650SBjorn Andersson unroll_registration:
7917bdc9650SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node)
7927bdc9650SBjorn Andersson 		subdev->remove(subdev);
7937bdc9650SBjorn Andersson 
7947bdc9650SBjorn Andersson 	return ret;
7957bdc9650SBjorn Andersson }
7967bdc9650SBjorn Andersson 
7977bdc9650SBjorn Andersson static void rproc_remove_subdevices(struct rproc *rproc)
7987bdc9650SBjorn Andersson {
7997bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
8007bdc9650SBjorn Andersson 
801f9cbbd25SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node)
8027bdc9650SBjorn Andersson 		subdev->remove(subdev);
8037bdc9650SBjorn Andersson }
8047bdc9650SBjorn Andersson 
805400e64dfSOhad Ben-Cohen /**
806*2666ca91SSarangdhar Joshi  * rproc_coredump_cleanup() - clean up dump_segments list
807*2666ca91SSarangdhar Joshi  * @rproc: the remote processor handle
808*2666ca91SSarangdhar Joshi  */
809*2666ca91SSarangdhar Joshi static void rproc_coredump_cleanup(struct rproc *rproc)
810*2666ca91SSarangdhar Joshi {
811*2666ca91SSarangdhar Joshi 	struct rproc_dump_segment *entry, *tmp;
812*2666ca91SSarangdhar Joshi 
813*2666ca91SSarangdhar Joshi 	list_for_each_entry_safe(entry, tmp, &rproc->dump_segments, node) {
814*2666ca91SSarangdhar Joshi 		list_del(&entry->node);
815*2666ca91SSarangdhar Joshi 		kfree(entry);
816*2666ca91SSarangdhar Joshi 	}
817*2666ca91SSarangdhar Joshi }
818*2666ca91SSarangdhar Joshi 
819*2666ca91SSarangdhar Joshi /**
820400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
821400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
822400e64dfSOhad Ben-Cohen  *
823400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
8247a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
825400e64dfSOhad Ben-Cohen  */
826400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc)
827400e64dfSOhad Ben-Cohen {
828400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
829d81fb32fSBjorn Andersson 	struct rproc_vdev *rvdev, *rvtmp;
830b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
831400e64dfSOhad Ben-Cohen 
832400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
833400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
834400e64dfSOhad Ben-Cohen 		rproc_remove_trace_file(entry->priv);
835400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
836400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
837400e64dfSOhad Ben-Cohen 		kfree(entry);
838400e64dfSOhad Ben-Cohen 	}
839400e64dfSOhad Ben-Cohen 
840400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
841400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
842400e64dfSOhad Ben-Cohen 		size_t unmapped;
843400e64dfSOhad Ben-Cohen 
844400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
845400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
846400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
847e981f6d4SSjur Brændeland 			dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
848400e64dfSOhad Ben-Cohen 				unmapped);
849400e64dfSOhad Ben-Cohen 		}
850400e64dfSOhad Ben-Cohen 
851400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
852400e64dfSOhad Ben-Cohen 		kfree(entry);
853400e64dfSOhad Ben-Cohen 	}
854b6356a01SSuman Anna 
855b6356a01SSuman Anna 	/* clean up carveout allocations */
856b6356a01SSuman Anna 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
857172e6ab1SSuman Anna 		dma_free_coherent(dev->parent, entry->len, entry->va,
858172e6ab1SSuman Anna 				  entry->dma);
859b6356a01SSuman Anna 		list_del(&entry->node);
860b6356a01SSuman Anna 		kfree(entry);
861b6356a01SSuman Anna 	}
862d81fb32fSBjorn Andersson 
863d81fb32fSBjorn Andersson 	/* clean up remote vdev entries */
864f5bcb353SBjorn Andersson 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
8652b45cef5SBjorn Andersson 		kref_put(&rvdev->refcount, rproc_vdev_release);
866*2666ca91SSarangdhar Joshi 
867*2666ca91SSarangdhar Joshi 	rproc_coredump_cleanup(rproc);
8682b45cef5SBjorn Andersson }
869400e64dfSOhad Ben-Cohen 
8701efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw)
8711efa30d0SSarangdhar Joshi {
872a4b24c75SBjorn Andersson 	struct resource_table *loaded_table;
8731efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
874a4b24c75SBjorn Andersson 	int ret;
8751efa30d0SSarangdhar Joshi 
8761efa30d0SSarangdhar Joshi 	/* load the ELF segments to memory */
8771efa30d0SSarangdhar Joshi 	ret = rproc_load_segments(rproc, fw);
8781efa30d0SSarangdhar Joshi 	if (ret) {
8791efa30d0SSarangdhar Joshi 		dev_err(dev, "Failed to load program segments: %d\n", ret);
8801efa30d0SSarangdhar Joshi 		return ret;
8811efa30d0SSarangdhar Joshi 	}
8821efa30d0SSarangdhar Joshi 
8831efa30d0SSarangdhar Joshi 	/*
8841efa30d0SSarangdhar Joshi 	 * The starting device has been given the rproc->cached_table as the
8851efa30d0SSarangdhar Joshi 	 * resource table. The address of the vring along with the other
8861efa30d0SSarangdhar Joshi 	 * allocated resources (carveouts etc) is stored in cached_table.
8871efa30d0SSarangdhar Joshi 	 * In order to pass this information to the remote device we must copy
8881efa30d0SSarangdhar Joshi 	 * this information to device memory. We also update the table_ptr so
8891efa30d0SSarangdhar Joshi 	 * that any subsequent changes will be applied to the loaded version.
8901efa30d0SSarangdhar Joshi 	 */
8911efa30d0SSarangdhar Joshi 	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
8921efa30d0SSarangdhar Joshi 	if (loaded_table) {
893a4b24c75SBjorn Andersson 		memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
8941efa30d0SSarangdhar Joshi 		rproc->table_ptr = loaded_table;
8951efa30d0SSarangdhar Joshi 	}
8961efa30d0SSarangdhar Joshi 
8971efa30d0SSarangdhar Joshi 	/* power up the remote processor */
8981efa30d0SSarangdhar Joshi 	ret = rproc->ops->start(rproc);
8991efa30d0SSarangdhar Joshi 	if (ret) {
9001efa30d0SSarangdhar Joshi 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
9011efa30d0SSarangdhar Joshi 		return ret;
9021efa30d0SSarangdhar Joshi 	}
9031efa30d0SSarangdhar Joshi 
9041efa30d0SSarangdhar Joshi 	/* probe any subdevices for the remote processor */
9051efa30d0SSarangdhar Joshi 	ret = rproc_probe_subdevices(rproc);
9061efa30d0SSarangdhar Joshi 	if (ret) {
9071efa30d0SSarangdhar Joshi 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
9081efa30d0SSarangdhar Joshi 			rproc->name, ret);
9091efa30d0SSarangdhar Joshi 		rproc->ops->stop(rproc);
9101efa30d0SSarangdhar Joshi 		return ret;
9111efa30d0SSarangdhar Joshi 	}
9121efa30d0SSarangdhar Joshi 
9131efa30d0SSarangdhar Joshi 	rproc->state = RPROC_RUNNING;
9141efa30d0SSarangdhar Joshi 
9151efa30d0SSarangdhar Joshi 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
9161efa30d0SSarangdhar Joshi 
9171efa30d0SSarangdhar Joshi 	return 0;
9181efa30d0SSarangdhar Joshi }
9191efa30d0SSarangdhar Joshi 
920400e64dfSOhad Ben-Cohen /*
921400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
922400e64dfSOhad Ben-Cohen  */
923400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
924400e64dfSOhad Ben-Cohen {
925b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
926400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
92758b64090SBjorn Andersson 	int ret;
928400e64dfSOhad Ben-Cohen 
929400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
930400e64dfSOhad Ben-Cohen 	if (ret)
931400e64dfSOhad Ben-Cohen 		return ret;
932400e64dfSOhad Ben-Cohen 
933e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
934400e64dfSOhad Ben-Cohen 
935400e64dfSOhad Ben-Cohen 	/*
936400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
937400e64dfSOhad Ben-Cohen 	 * just a nop
938400e64dfSOhad Ben-Cohen 	 */
939400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
940400e64dfSOhad Ben-Cohen 	if (ret) {
941400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
942400e64dfSOhad Ben-Cohen 		return ret;
943400e64dfSOhad Ben-Cohen 	}
944400e64dfSOhad Ben-Cohen 
9453e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
946400e64dfSOhad Ben-Cohen 
94758b64090SBjorn Andersson 	/* load resource table */
94858b64090SBjorn Andersson 	ret = rproc_load_rsc_table(rproc, fw);
94958b64090SBjorn Andersson 	if (ret)
95058b64090SBjorn Andersson 		goto disable_iommu;
951a0c10687SBjorn Andersson 
952b35d7afcSBjorn Andersson 	/* reset max_notifyid */
953b35d7afcSBjorn Andersson 	rproc->max_notifyid = -1;
954b35d7afcSBjorn Andersson 
955400e64dfSOhad Ben-Cohen 	/* handle fw resources which are required to boot rproc */
956a4b24c75SBjorn Andersson 	ret = rproc_handle_resources(rproc, rproc_loading_handlers);
957400e64dfSOhad Ben-Cohen 	if (ret) {
958400e64dfSOhad Ben-Cohen 		dev_err(dev, "Failed to process resources: %d\n", ret);
959229b85a6SBjorn Andersson 		goto clean_up_resources;
960400e64dfSOhad Ben-Cohen 	}
961400e64dfSOhad Ben-Cohen 
9621efa30d0SSarangdhar Joshi 	ret = rproc_start(rproc, fw);
9631efa30d0SSarangdhar Joshi 	if (ret)
964229b85a6SBjorn Andersson 		goto clean_up_resources;
965400e64dfSOhad Ben-Cohen 
966400e64dfSOhad Ben-Cohen 	return 0;
967400e64dfSOhad Ben-Cohen 
968229b85a6SBjorn Andersson clean_up_resources:
969229b85a6SBjorn Andersson 	rproc_resource_cleanup(rproc);
970a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
971a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
972988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
97358b64090SBjorn Andersson disable_iommu:
974400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
975400e64dfSOhad Ben-Cohen 	return ret;
976400e64dfSOhad Ben-Cohen }
977400e64dfSOhad Ben-Cohen 
978400e64dfSOhad Ben-Cohen /*
9795e6533f7SSarangdhar Joshi  * take a firmware and boot it up.
980400e64dfSOhad Ben-Cohen  *
981400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
982400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
983400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
984400e64dfSOhad Ben-Cohen  * that might be cleaner).
985400e64dfSOhad Ben-Cohen  */
9865e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
987400e64dfSOhad Ben-Cohen {
988400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
989a2b950acSOhad Ben-Cohen 
9902bfc311aSBjorn Andersson 	rproc_boot(rproc);
991ddf71187SBjorn Andersson 
992400e64dfSOhad Ben-Cohen 	release_firmware(fw);
993400e64dfSOhad Ben-Cohen }
994400e64dfSOhad Ben-Cohen 
9955e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc)
99670b85ef8SFernando Guzman Lugo {
99770b85ef8SFernando Guzman Lugo 	int ret;
99870b85ef8SFernando Guzman Lugo 
99970b85ef8SFernando Guzman Lugo 	/*
100070b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
100170b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
100270b85ef8SFernando Guzman Lugo 	 */
100370b85ef8SFernando Guzman Lugo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
100470b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
10055e6533f7SSarangdhar Joshi 				      rproc, rproc_auto_boot_callback);
10062099c77dSSarangdhar Joshi 	if (ret < 0)
100770b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
100870b85ef8SFernando Guzman Lugo 
100970b85ef8SFernando Guzman Lugo 	return ret;
101070b85ef8SFernando Guzman Lugo }
101170b85ef8SFernando Guzman Lugo 
10121efa30d0SSarangdhar Joshi static int rproc_stop(struct rproc *rproc)
10131efa30d0SSarangdhar Joshi {
10141efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
10151efa30d0SSarangdhar Joshi 	int ret;
10161efa30d0SSarangdhar Joshi 
10171efa30d0SSarangdhar Joshi 	/* remove any subdevices for the remote processor */
10181efa30d0SSarangdhar Joshi 	rproc_remove_subdevices(rproc);
10191efa30d0SSarangdhar Joshi 
10200a8b81cbSBjorn Andersson 	/* the installed resource table is no longer accessible */
10210a8b81cbSBjorn Andersson 	rproc->table_ptr = rproc->cached_table;
10220a8b81cbSBjorn Andersson 
10231efa30d0SSarangdhar Joshi 	/* power off the remote processor */
10241efa30d0SSarangdhar Joshi 	ret = rproc->ops->stop(rproc);
10251efa30d0SSarangdhar Joshi 	if (ret) {
10261efa30d0SSarangdhar Joshi 		dev_err(dev, "can't stop rproc: %d\n", ret);
10271efa30d0SSarangdhar Joshi 		return ret;
10281efa30d0SSarangdhar Joshi 	}
10291efa30d0SSarangdhar Joshi 
10301efa30d0SSarangdhar Joshi 	rproc->state = RPROC_OFFLINE;
10311efa30d0SSarangdhar Joshi 
10321efa30d0SSarangdhar Joshi 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
10331efa30d0SSarangdhar Joshi 
10341efa30d0SSarangdhar Joshi 	return 0;
10351efa30d0SSarangdhar Joshi }
10361efa30d0SSarangdhar Joshi 
103770b85ef8SFernando Guzman Lugo /**
1038*2666ca91SSarangdhar Joshi  * rproc_coredump_add_segment() - add segment of device memory to coredump
1039*2666ca91SSarangdhar Joshi  * @rproc:	handle of a remote processor
1040*2666ca91SSarangdhar Joshi  * @da:		device address
1041*2666ca91SSarangdhar Joshi  * @size:	size of segment
1042*2666ca91SSarangdhar Joshi  *
1043*2666ca91SSarangdhar Joshi  * Add device memory to the list of segments to be included in a coredump for
1044*2666ca91SSarangdhar Joshi  * the remoteproc.
1045*2666ca91SSarangdhar Joshi  *
1046*2666ca91SSarangdhar Joshi  * Return: 0 on success, negative errno on error.
1047*2666ca91SSarangdhar Joshi  */
1048*2666ca91SSarangdhar Joshi int rproc_coredump_add_segment(struct rproc *rproc, dma_addr_t da, size_t size)
1049*2666ca91SSarangdhar Joshi {
1050*2666ca91SSarangdhar Joshi 	struct rproc_dump_segment *segment;
1051*2666ca91SSarangdhar Joshi 
1052*2666ca91SSarangdhar Joshi 	segment = kzalloc(sizeof(*segment), GFP_KERNEL);
1053*2666ca91SSarangdhar Joshi 	if (!segment)
1054*2666ca91SSarangdhar Joshi 		return -ENOMEM;
1055*2666ca91SSarangdhar Joshi 
1056*2666ca91SSarangdhar Joshi 	segment->da = da;
1057*2666ca91SSarangdhar Joshi 	segment->size = size;
1058*2666ca91SSarangdhar Joshi 
1059*2666ca91SSarangdhar Joshi 	list_add_tail(&segment->node, &rproc->dump_segments);
1060*2666ca91SSarangdhar Joshi 
1061*2666ca91SSarangdhar Joshi 	return 0;
1062*2666ca91SSarangdhar Joshi }
1063*2666ca91SSarangdhar Joshi EXPORT_SYMBOL(rproc_coredump_add_segment);
1064*2666ca91SSarangdhar Joshi 
1065*2666ca91SSarangdhar Joshi /**
1066*2666ca91SSarangdhar Joshi  * rproc_coredump() - perform coredump
1067*2666ca91SSarangdhar Joshi  * @rproc:	rproc handle
1068*2666ca91SSarangdhar Joshi  *
1069*2666ca91SSarangdhar Joshi  * This function will generate an ELF header for the registered segments
1070*2666ca91SSarangdhar Joshi  * and create a devcoredump device associated with rproc.
1071*2666ca91SSarangdhar Joshi  */
1072*2666ca91SSarangdhar Joshi static void rproc_coredump(struct rproc *rproc)
1073*2666ca91SSarangdhar Joshi {
1074*2666ca91SSarangdhar Joshi 	struct rproc_dump_segment *segment;
1075*2666ca91SSarangdhar Joshi 	struct elf32_phdr *phdr;
1076*2666ca91SSarangdhar Joshi 	struct elf32_hdr *ehdr;
1077*2666ca91SSarangdhar Joshi 	size_t data_size;
1078*2666ca91SSarangdhar Joshi 	size_t offset;
1079*2666ca91SSarangdhar Joshi 	void *data;
1080*2666ca91SSarangdhar Joshi 	void *ptr;
1081*2666ca91SSarangdhar Joshi 	int phnum = 0;
1082*2666ca91SSarangdhar Joshi 
1083*2666ca91SSarangdhar Joshi 	if (list_empty(&rproc->dump_segments))
1084*2666ca91SSarangdhar Joshi 		return;
1085*2666ca91SSarangdhar Joshi 
1086*2666ca91SSarangdhar Joshi 	data_size = sizeof(*ehdr);
1087*2666ca91SSarangdhar Joshi 	list_for_each_entry(segment, &rproc->dump_segments, node) {
1088*2666ca91SSarangdhar Joshi 		data_size += sizeof(*phdr) + segment->size;
1089*2666ca91SSarangdhar Joshi 
1090*2666ca91SSarangdhar Joshi 		phnum++;
1091*2666ca91SSarangdhar Joshi 	}
1092*2666ca91SSarangdhar Joshi 
1093*2666ca91SSarangdhar Joshi 	data = vmalloc(data_size);
1094*2666ca91SSarangdhar Joshi 	if (!data)
1095*2666ca91SSarangdhar Joshi 		return;
1096*2666ca91SSarangdhar Joshi 
1097*2666ca91SSarangdhar Joshi 	ehdr = data;
1098*2666ca91SSarangdhar Joshi 
1099*2666ca91SSarangdhar Joshi 	memset(ehdr, 0, sizeof(*ehdr));
1100*2666ca91SSarangdhar Joshi 	memcpy(ehdr->e_ident, ELFMAG, SELFMAG);
1101*2666ca91SSarangdhar Joshi 	ehdr->e_ident[EI_CLASS] = ELFCLASS32;
1102*2666ca91SSarangdhar Joshi 	ehdr->e_ident[EI_DATA] = ELFDATA2LSB;
1103*2666ca91SSarangdhar Joshi 	ehdr->e_ident[EI_VERSION] = EV_CURRENT;
1104*2666ca91SSarangdhar Joshi 	ehdr->e_ident[EI_OSABI] = ELFOSABI_NONE;
1105*2666ca91SSarangdhar Joshi 	ehdr->e_type = ET_CORE;
1106*2666ca91SSarangdhar Joshi 	ehdr->e_machine = EM_NONE;
1107*2666ca91SSarangdhar Joshi 	ehdr->e_version = EV_CURRENT;
1108*2666ca91SSarangdhar Joshi 	ehdr->e_entry = rproc->bootaddr;
1109*2666ca91SSarangdhar Joshi 	ehdr->e_phoff = sizeof(*ehdr);
1110*2666ca91SSarangdhar Joshi 	ehdr->e_ehsize = sizeof(*ehdr);
1111*2666ca91SSarangdhar Joshi 	ehdr->e_phentsize = sizeof(*phdr);
1112*2666ca91SSarangdhar Joshi 	ehdr->e_phnum = phnum;
1113*2666ca91SSarangdhar Joshi 
1114*2666ca91SSarangdhar Joshi 	phdr = data + ehdr->e_phoff;
1115*2666ca91SSarangdhar Joshi 	offset = ehdr->e_phoff + sizeof(*phdr) * ehdr->e_phnum;
1116*2666ca91SSarangdhar Joshi 	list_for_each_entry(segment, &rproc->dump_segments, node) {
1117*2666ca91SSarangdhar Joshi 		memset(phdr, 0, sizeof(*phdr));
1118*2666ca91SSarangdhar Joshi 		phdr->p_type = PT_LOAD;
1119*2666ca91SSarangdhar Joshi 		phdr->p_offset = offset;
1120*2666ca91SSarangdhar Joshi 		phdr->p_vaddr = segment->da;
1121*2666ca91SSarangdhar Joshi 		phdr->p_paddr = segment->da;
1122*2666ca91SSarangdhar Joshi 		phdr->p_filesz = segment->size;
1123*2666ca91SSarangdhar Joshi 		phdr->p_memsz = segment->size;
1124*2666ca91SSarangdhar Joshi 		phdr->p_flags = PF_R | PF_W | PF_X;
1125*2666ca91SSarangdhar Joshi 		phdr->p_align = 0;
1126*2666ca91SSarangdhar Joshi 
1127*2666ca91SSarangdhar Joshi 		ptr = rproc_da_to_va(rproc, segment->da, segment->size);
1128*2666ca91SSarangdhar Joshi 		if (!ptr) {
1129*2666ca91SSarangdhar Joshi 			dev_err(&rproc->dev,
1130*2666ca91SSarangdhar Joshi 				"invalid coredump segment (%pad, %zu)\n",
1131*2666ca91SSarangdhar Joshi 				&segment->da, segment->size);
1132*2666ca91SSarangdhar Joshi 			memset(data + offset, 0xff, segment->size);
1133*2666ca91SSarangdhar Joshi 		} else {
1134*2666ca91SSarangdhar Joshi 			memcpy(data + offset, ptr, segment->size);
1135*2666ca91SSarangdhar Joshi 		}
1136*2666ca91SSarangdhar Joshi 
1137*2666ca91SSarangdhar Joshi 		offset += phdr->p_filesz;
1138*2666ca91SSarangdhar Joshi 		phdr++;
1139*2666ca91SSarangdhar Joshi 	}
1140*2666ca91SSarangdhar Joshi 
1141*2666ca91SSarangdhar Joshi 	dev_coredumpv(&rproc->dev, data, data_size, GFP_KERNEL);
1142*2666ca91SSarangdhar Joshi }
1143*2666ca91SSarangdhar Joshi 
1144*2666ca91SSarangdhar Joshi /**
114570b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
114670b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
114770b85ef8SFernando Guzman Lugo  *
114856324d7aSAnna, Suman  * The recovery is done by resetting all the virtio devices, that way all the
114970b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
115070b85ef8SFernando Guzman Lugo  * remoteproc functional again.
115170b85ef8SFernando Guzman Lugo  *
115270b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
115370b85ef8SFernando Guzman Lugo  */
115470b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
115570b85ef8SFernando Guzman Lugo {
11567e83cab8SSarangdhar Joshi 	const struct firmware *firmware_p;
11577e83cab8SSarangdhar Joshi 	struct device *dev = &rproc->dev;
11587e83cab8SSarangdhar Joshi 	int ret;
11597e83cab8SSarangdhar Joshi 
11607e83cab8SSarangdhar Joshi 	dev_err(dev, "recovering %s\n", rproc->name);
116170b85ef8SFernando Guzman Lugo 
11627e83cab8SSarangdhar Joshi 	ret = mutex_lock_interruptible(&rproc->lock);
11637e83cab8SSarangdhar Joshi 	if (ret)
11647e83cab8SSarangdhar Joshi 		return ret;
11657e83cab8SSarangdhar Joshi 
11667e83cab8SSarangdhar Joshi 	ret = rproc_stop(rproc);
11677e83cab8SSarangdhar Joshi 	if (ret)
11687e83cab8SSarangdhar Joshi 		goto unlock_mutex;
1169ddf71187SBjorn Andersson 
1170*2666ca91SSarangdhar Joshi 	/* generate coredump */
1171*2666ca91SSarangdhar Joshi 	rproc_coredump(rproc);
1172*2666ca91SSarangdhar Joshi 
11737e83cab8SSarangdhar Joshi 	/* load firmware */
11747e83cab8SSarangdhar Joshi 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
11757e83cab8SSarangdhar Joshi 	if (ret < 0) {
11767e83cab8SSarangdhar Joshi 		dev_err(dev, "request_firmware failed: %d\n", ret);
11777e83cab8SSarangdhar Joshi 		goto unlock_mutex;
11787e83cab8SSarangdhar Joshi 	}
1179ddf71187SBjorn Andersson 
11807e83cab8SSarangdhar Joshi 	/* boot the remote processor up again */
11817e83cab8SSarangdhar Joshi 	ret = rproc_start(rproc, firmware_p);
11827e83cab8SSarangdhar Joshi 
11837e83cab8SSarangdhar Joshi 	release_firmware(firmware_p);
11847e83cab8SSarangdhar Joshi 
11857e83cab8SSarangdhar Joshi unlock_mutex:
11867e83cab8SSarangdhar Joshi 	mutex_unlock(&rproc->lock);
11877e83cab8SSarangdhar Joshi 	return ret;
118870b85ef8SFernando Guzman Lugo }
118970b85ef8SFernando Guzman Lugo 
1190400e64dfSOhad Ben-Cohen /**
11918afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
11928afd519cSFernando Guzman Lugo  *
11938afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
11948afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
11958afd519cSFernando Guzman Lugo  */
11968afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
11978afd519cSFernando Guzman Lugo {
11988afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
11998afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
12008afd519cSFernando Guzman Lugo 
12018afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
12028afd519cSFernando Guzman Lugo 
12038afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
12048afd519cSFernando Guzman Lugo 
12058afd519cSFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
12068afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
12078afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
12088afd519cSFernando Guzman Lugo 		return;
12098afd519cSFernando Guzman Lugo 	}
12108afd519cSFernando Guzman Lugo 
12118afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
12128afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
12138afd519cSFernando Guzman Lugo 		rproc->name);
12148afd519cSFernando Guzman Lugo 
12158afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
12168afd519cSFernando Guzman Lugo 
12172e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
121870b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
12198afd519cSFernando Guzman Lugo }
12208afd519cSFernando Guzman Lugo 
12218afd519cSFernando Guzman Lugo /**
12221b0ef906SSuman Anna  * rproc_boot() - boot a remote processor
1223400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1224400e64dfSOhad Ben-Cohen  *
1225400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1226400e64dfSOhad Ben-Cohen  *
1227400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1228400e64dfSOhad Ben-Cohen  * returns (successfully).
1229400e64dfSOhad Ben-Cohen  *
1230400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1231400e64dfSOhad Ben-Cohen  */
12321b0ef906SSuman Anna int rproc_boot(struct rproc *rproc)
1233400e64dfSOhad Ben-Cohen {
1234400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1235400e64dfSOhad Ben-Cohen 	struct device *dev;
1236400e64dfSOhad Ben-Cohen 	int ret;
1237400e64dfSOhad Ben-Cohen 
1238400e64dfSOhad Ben-Cohen 	if (!rproc) {
1239400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1240400e64dfSOhad Ben-Cohen 		return -EINVAL;
1241400e64dfSOhad Ben-Cohen 	}
1242400e64dfSOhad Ben-Cohen 
1243b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1244400e64dfSOhad Ben-Cohen 
1245400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1246400e64dfSOhad Ben-Cohen 	if (ret) {
1247400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1248400e64dfSOhad Ben-Cohen 		return ret;
1249400e64dfSOhad Ben-Cohen 	}
1250400e64dfSOhad Ben-Cohen 
12512099c77dSSarangdhar Joshi 	if (rproc->state == RPROC_DELETED) {
12522099c77dSSarangdhar Joshi 		ret = -ENODEV;
12532099c77dSSarangdhar Joshi 		dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
12542099c77dSSarangdhar Joshi 		goto unlock_mutex;
12552099c77dSSarangdhar Joshi 	}
12562099c77dSSarangdhar Joshi 
1257400e64dfSOhad Ben-Cohen 	/* skip the boot process if rproc is already powered up */
1258400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1259400e64dfSOhad Ben-Cohen 		ret = 0;
1260400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1261400e64dfSOhad Ben-Cohen 	}
1262400e64dfSOhad Ben-Cohen 
1263400e64dfSOhad Ben-Cohen 	dev_info(dev, "powering up %s\n", rproc->name);
1264400e64dfSOhad Ben-Cohen 
1265400e64dfSOhad Ben-Cohen 	/* load firmware */
1266400e64dfSOhad Ben-Cohen 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1267400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1268400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware failed: %d\n", ret);
1269400e64dfSOhad Ben-Cohen 		goto downref_rproc;
1270400e64dfSOhad Ben-Cohen 	}
1271400e64dfSOhad Ben-Cohen 
1272400e64dfSOhad Ben-Cohen 	ret = rproc_fw_boot(rproc, firmware_p);
1273400e64dfSOhad Ben-Cohen 
1274400e64dfSOhad Ben-Cohen 	release_firmware(firmware_p);
1275400e64dfSOhad Ben-Cohen 
1276400e64dfSOhad Ben-Cohen downref_rproc:
1277fbb6aacbSBjorn Andersson 	if (ret)
1278400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1279400e64dfSOhad Ben-Cohen unlock_mutex:
1280400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1281400e64dfSOhad Ben-Cohen 	return ret;
1282400e64dfSOhad Ben-Cohen }
1283400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1284400e64dfSOhad Ben-Cohen 
1285400e64dfSOhad Ben-Cohen /**
1286400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1287400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1288400e64dfSOhad Ben-Cohen  *
1289400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1290400e64dfSOhad Ben-Cohen  *
1291400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1292400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1293400e64dfSOhad Ben-Cohen  * without really powering off the device.
1294400e64dfSOhad Ben-Cohen  *
1295400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1296400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1297400e64dfSOhad Ben-Cohen  *
1298400e64dfSOhad Ben-Cohen  * Notes:
1299400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1300400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1301400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1302400e64dfSOhad Ben-Cohen  *   needed.
1303400e64dfSOhad Ben-Cohen  */
1304400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1305400e64dfSOhad Ben-Cohen {
1306b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1307400e64dfSOhad Ben-Cohen 	int ret;
1308400e64dfSOhad Ben-Cohen 
1309400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1310400e64dfSOhad Ben-Cohen 	if (ret) {
1311400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1312400e64dfSOhad Ben-Cohen 		return;
1313400e64dfSOhad Ben-Cohen 	}
1314400e64dfSOhad Ben-Cohen 
1315400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1316400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1317400e64dfSOhad Ben-Cohen 		goto out;
1318400e64dfSOhad Ben-Cohen 
13191efa30d0SSarangdhar Joshi 	ret = rproc_stop(rproc);
1320400e64dfSOhad Ben-Cohen 	if (ret) {
1321400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1322400e64dfSOhad Ben-Cohen 		goto out;
1323400e64dfSOhad Ben-Cohen 	}
1324400e64dfSOhad Ben-Cohen 
1325400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1326400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1327400e64dfSOhad Ben-Cohen 
1328400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1329400e64dfSOhad Ben-Cohen 
1330988d204cSBjorn Andersson 	/* Free the copy of the resource table */
1331a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1332a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1333988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
1334400e64dfSOhad Ben-Cohen out:
1335400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1336400e64dfSOhad Ben-Cohen }
1337400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1338400e64dfSOhad Ben-Cohen 
1339400e64dfSOhad Ben-Cohen /**
1340fec47d86SDave Gerlach  * rproc_get_by_phandle() - find a remote processor by phandle
1341fec47d86SDave Gerlach  * @phandle: phandle to the rproc
1342fec47d86SDave Gerlach  *
1343fec47d86SDave Gerlach  * Finds an rproc handle using the remote processor's phandle, and then
1344fec47d86SDave Gerlach  * return a handle to the rproc.
1345fec47d86SDave Gerlach  *
1346fec47d86SDave Gerlach  * This function increments the remote processor's refcount, so always
1347fec47d86SDave Gerlach  * use rproc_put() to decrement it back once rproc isn't needed anymore.
1348fec47d86SDave Gerlach  *
1349fec47d86SDave Gerlach  * Returns the rproc handle on success, and NULL on failure.
1350fec47d86SDave Gerlach  */
13518de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF
1352fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle)
1353fec47d86SDave Gerlach {
1354fec47d86SDave Gerlach 	struct rproc *rproc = NULL, *r;
1355fec47d86SDave Gerlach 	struct device_node *np;
1356fec47d86SDave Gerlach 
1357fec47d86SDave Gerlach 	np = of_find_node_by_phandle(phandle);
1358fec47d86SDave Gerlach 	if (!np)
1359fec47d86SDave Gerlach 		return NULL;
1360fec47d86SDave Gerlach 
1361fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1362fec47d86SDave Gerlach 	list_for_each_entry(r, &rproc_list, node) {
1363fec47d86SDave Gerlach 		if (r->dev.parent && r->dev.parent->of_node == np) {
1364fbb6aacbSBjorn Andersson 			/* prevent underlying implementation from being removed */
1365fbb6aacbSBjorn Andersson 			if (!try_module_get(r->dev.parent->driver->owner)) {
1366fbb6aacbSBjorn Andersson 				dev_err(&r->dev, "can't get owner\n");
1367fbb6aacbSBjorn Andersson 				break;
1368fbb6aacbSBjorn Andersson 			}
1369fbb6aacbSBjorn Andersson 
1370fec47d86SDave Gerlach 			rproc = r;
1371fec47d86SDave Gerlach 			get_device(&rproc->dev);
1372fec47d86SDave Gerlach 			break;
1373fec47d86SDave Gerlach 		}
1374fec47d86SDave Gerlach 	}
1375fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1376fec47d86SDave Gerlach 
1377fec47d86SDave Gerlach 	of_node_put(np);
1378fec47d86SDave Gerlach 
1379fec47d86SDave Gerlach 	return rproc;
1380fec47d86SDave Gerlach }
13818de3dbd0SOhad Ben-Cohen #else
13828de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle)
13838de3dbd0SOhad Ben-Cohen {
13848de3dbd0SOhad Ben-Cohen 	return NULL;
13858de3dbd0SOhad Ben-Cohen }
13868de3dbd0SOhad Ben-Cohen #endif
1387fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle);
1388fec47d86SDave Gerlach 
1389fec47d86SDave Gerlach /**
1390160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
1391400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
1392400e64dfSOhad Ben-Cohen  *
1393400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
1394400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
1395400e64dfSOhad Ben-Cohen  *
1396400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
1397400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
1398400e64dfSOhad Ben-Cohen  *
1399400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
1400400e64dfSOhad Ben-Cohen  *
1401400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
1402400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
1403400e64dfSOhad Ben-Cohen  * firmware.
1404400e64dfSOhad Ben-Cohen  *
1405400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
14067a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
1407400e64dfSOhad Ben-Cohen  * probed.
1408400e64dfSOhad Ben-Cohen  */
1409160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
1410400e64dfSOhad Ben-Cohen {
1411b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
141270b85ef8SFernando Guzman Lugo 	int ret;
1413400e64dfSOhad Ben-Cohen 
1414b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
1415b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
1416b5ab5e24SOhad Ben-Cohen 		return ret;
1417400e64dfSOhad Ben-Cohen 
1418b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
1419400e64dfSOhad Ben-Cohen 
1420400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
1421400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
14227a20c64dSSarangdhar Joshi 
14237a20c64dSSarangdhar Joshi 	/* if rproc is marked always-on, request it to boot */
14247a20c64dSSarangdhar Joshi 	if (rproc->auto_boot) {
14255e6533f7SSarangdhar Joshi 		ret = rproc_trigger_auto_boot(rproc);
1426d2e12e66SDave Gerlach 		if (ret < 0)
1427d2e12e66SDave Gerlach 			return ret;
14287a20c64dSSarangdhar Joshi 	}
1429400e64dfSOhad Ben-Cohen 
1430d2e12e66SDave Gerlach 	/* expose to rproc_get_by_phandle users */
1431d2e12e66SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1432d2e12e66SDave Gerlach 	list_add(&rproc->node, &rproc_list);
1433d2e12e66SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1434d2e12e66SDave Gerlach 
1435d2e12e66SDave Gerlach 	return 0;
1436400e64dfSOhad Ben-Cohen }
1437160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
1438400e64dfSOhad Ben-Cohen 
1439400e64dfSOhad Ben-Cohen /**
1440b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
1441b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
1442b5ab5e24SOhad Ben-Cohen  *
1443b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
1444b5ab5e24SOhad Ben-Cohen  *
1445b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
1446b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
1447b5ab5e24SOhad Ben-Cohen  */
1448b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
1449b5ab5e24SOhad Ben-Cohen {
1450b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
1451b5ab5e24SOhad Ben-Cohen 
14527183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
14537183a2a7SOhad Ben-Cohen 
1454b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
1455b5ab5e24SOhad Ben-Cohen 
1456b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
1457b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
1458b5ab5e24SOhad Ben-Cohen 
14590f57dc6aSMatt Redfearn 	kfree(rproc->firmware);
1460fb98e2bdSBjorn Andersson 	kfree(rproc->ops);
1461b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
1462b5ab5e24SOhad Ben-Cohen }
1463b5ab5e24SOhad Ben-Cohen 
1464c42ca04dSBhumika Goyal static const struct device_type rproc_type = {
1465b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
1466b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
1467b5ab5e24SOhad Ben-Cohen };
1468400e64dfSOhad Ben-Cohen 
1469400e64dfSOhad Ben-Cohen /**
1470400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
1471400e64dfSOhad Ben-Cohen  * @dev: the underlying device
1472400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
1473400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
14748b4aec9aSRobert Tivy  * @firmware: name of firmware file to load, can be NULL
1475400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
1476400e64dfSOhad Ben-Cohen  *
1477400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
14788b4aec9aSRobert Tivy  * it yet. if @firmware is NULL, a default name is used.
1479400e64dfSOhad Ben-Cohen  *
1480400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
1481400e64dfSOhad Ben-Cohen  * of the remote processor.
1482400e64dfSOhad Ben-Cohen  *
1483400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
1484160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
1485400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
1486400e64dfSOhad Ben-Cohen  *
1487400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
1488400e64dfSOhad Ben-Cohen  *
1489400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
1490433c0e04SBjorn Andersson  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
1491400e64dfSOhad Ben-Cohen  */
1492400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
1493400e64dfSOhad Ben-Cohen 			  const struct rproc_ops *ops,
1494400e64dfSOhad Ben-Cohen 			  const char *firmware, int len)
1495400e64dfSOhad Ben-Cohen {
1496400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
14978b4aec9aSRobert Tivy 	char *p, *template = "rproc-%s-fw";
14980f57dc6aSMatt Redfearn 	int name_len;
1499400e64dfSOhad Ben-Cohen 
1500400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
1501400e64dfSOhad Ben-Cohen 		return NULL;
1502400e64dfSOhad Ben-Cohen 
15030f57dc6aSMatt Redfearn 	if (!firmware) {
15048b4aec9aSRobert Tivy 		/*
15058b4aec9aSRobert Tivy 		 * If the caller didn't pass in a firmware name then
15060f57dc6aSMatt Redfearn 		 * construct a default name.
15078b4aec9aSRobert Tivy 		 */
15088b4aec9aSRobert Tivy 		name_len = strlen(name) + strlen(template) - 2 + 1;
15090f57dc6aSMatt Redfearn 		p = kmalloc(name_len, GFP_KERNEL);
15100f57dc6aSMatt Redfearn 		if (!p)
1511400e64dfSOhad Ben-Cohen 			return NULL;
15128b4aec9aSRobert Tivy 		snprintf(p, name_len, template, name);
15138b4aec9aSRobert Tivy 	} else {
15140f57dc6aSMatt Redfearn 		p = kstrdup(firmware, GFP_KERNEL);
15150f57dc6aSMatt Redfearn 		if (!p)
15160f57dc6aSMatt Redfearn 			return NULL;
15170f57dc6aSMatt Redfearn 	}
15180f57dc6aSMatt Redfearn 
15190f57dc6aSMatt Redfearn 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
15200f57dc6aSMatt Redfearn 	if (!rproc) {
15210f57dc6aSMatt Redfearn 		kfree(p);
15220f57dc6aSMatt Redfearn 		return NULL;
15238b4aec9aSRobert Tivy 	}
15248b4aec9aSRobert Tivy 
1525fb98e2bdSBjorn Andersson 	rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
1526fb98e2bdSBjorn Andersson 	if (!rproc->ops) {
1527fb98e2bdSBjorn Andersson 		kfree(p);
1528fb98e2bdSBjorn Andersson 		kfree(rproc);
1529fb98e2bdSBjorn Andersson 		return NULL;
1530fb98e2bdSBjorn Andersson 	}
1531fb98e2bdSBjorn Andersson 
15328b4aec9aSRobert Tivy 	rproc->firmware = p;
1533400e64dfSOhad Ben-Cohen 	rproc->name = name;
1534400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
1535ddf71187SBjorn Andersson 	rproc->auto_boot = true;
1536400e64dfSOhad Ben-Cohen 
1537b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
1538b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
1539b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
15402aefbef0SMatt Redfearn 	rproc->dev.class = &rproc_class;
15417c89717fSBjorn Andersson 	rproc->dev.driver_data = rproc;
1542b5ab5e24SOhad Ben-Cohen 
1543b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
1544b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1545b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
1546b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1547b5ab5e24SOhad Ben-Cohen 		put_device(&rproc->dev);
1548b5ab5e24SOhad Ben-Cohen 		return NULL;
1549b5ab5e24SOhad Ben-Cohen 	}
1550b5ab5e24SOhad Ben-Cohen 
1551b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1552b5ab5e24SOhad Ben-Cohen 
1553400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
1554400e64dfSOhad Ben-Cohen 
15550f21f9ccSBjorn Andersson 	/* Default to ELF loader if no load function is specified */
15560f21f9ccSBjorn Andersson 	if (!rproc->ops->load) {
15570f21f9ccSBjorn Andersson 		rproc->ops->load = rproc_elf_load_segments;
155858b64090SBjorn Andersson 		rproc->ops->load_rsc_table = rproc_elf_load_rsc_table;
15590f21f9ccSBjorn Andersson 		rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
15600f21f9ccSBjorn Andersson 		rproc->ops->sanity_check = rproc_elf_sanity_check;
15610f21f9ccSBjorn Andersson 		rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
15620f21f9ccSBjorn Andersson 	}
1563400e64dfSOhad Ben-Cohen 
1564400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
1565400e64dfSOhad Ben-Cohen 
15667a186941SOhad Ben-Cohen 	idr_init(&rproc->notifyids);
15677a186941SOhad Ben-Cohen 
1568400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
1569400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
1570400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
15717a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
15727bdc9650SBjorn Andersson 	INIT_LIST_HEAD(&rproc->subdevs);
1573*2666ca91SSarangdhar Joshi 	INIT_LIST_HEAD(&rproc->dump_segments);
1574400e64dfSOhad Ben-Cohen 
15758afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
15768afd519cSFernando Guzman Lugo 
1577400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1578400e64dfSOhad Ben-Cohen 
1579400e64dfSOhad Ben-Cohen 	return rproc;
1580400e64dfSOhad Ben-Cohen }
1581400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
1582400e64dfSOhad Ben-Cohen 
1583400e64dfSOhad Ben-Cohen /**
1584433c0e04SBjorn Andersson  * rproc_free() - unroll rproc_alloc()
1585433c0e04SBjorn Andersson  * @rproc: the remote processor handle
1586433c0e04SBjorn Andersson  *
1587433c0e04SBjorn Andersson  * This function decrements the rproc dev refcount.
1588433c0e04SBjorn Andersson  *
1589433c0e04SBjorn Andersson  * If no one holds any reference to rproc anymore, then its refcount would
1590433c0e04SBjorn Andersson  * now drop to zero, and it would be freed.
1591433c0e04SBjorn Andersson  */
1592433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc)
1593433c0e04SBjorn Andersson {
1594433c0e04SBjorn Andersson 	put_device(&rproc->dev);
1595433c0e04SBjorn Andersson }
1596433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free);
1597433c0e04SBjorn Andersson 
1598433c0e04SBjorn Andersson /**
1599433c0e04SBjorn Andersson  * rproc_put() - release rproc reference
1600400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
1601400e64dfSOhad Ben-Cohen  *
1602c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
1603400e64dfSOhad Ben-Cohen  *
1604c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
1605c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
1606400e64dfSOhad Ben-Cohen  */
1607160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
1608400e64dfSOhad Ben-Cohen {
1609fbb6aacbSBjorn Andersson 	module_put(rproc->dev.parent->driver->owner);
1610b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
1611400e64dfSOhad Ben-Cohen }
1612160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
1613400e64dfSOhad Ben-Cohen 
1614400e64dfSOhad Ben-Cohen /**
1615160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
1616400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
1617400e64dfSOhad Ben-Cohen  *
1618400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
1619400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
1620160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
1621400e64dfSOhad Ben-Cohen  * has completed successfully.
1622400e64dfSOhad Ben-Cohen  *
1623160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
1624c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
1625433c0e04SBjorn Andersson  * one last refcount, one still needs to call rproc_free().
1626400e64dfSOhad Ben-Cohen  *
1627400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
1628400e64dfSOhad Ben-Cohen  */
1629160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
1630400e64dfSOhad Ben-Cohen {
1631400e64dfSOhad Ben-Cohen 	if (!rproc)
1632400e64dfSOhad Ben-Cohen 		return -EINVAL;
1633400e64dfSOhad Ben-Cohen 
1634ddf71187SBjorn Andersson 	/* if rproc is marked always-on, rproc_add() booted it */
1635ddf71187SBjorn Andersson 	/* TODO: make sure this works with rproc->power > 1 */
1636ddf71187SBjorn Andersson 	if (rproc->auto_boot)
1637ddf71187SBjorn Andersson 		rproc_shutdown(rproc);
1638ddf71187SBjorn Andersson 
16392099c77dSSarangdhar Joshi 	mutex_lock(&rproc->lock);
16402099c77dSSarangdhar Joshi 	rproc->state = RPROC_DELETED;
16412099c77dSSarangdhar Joshi 	mutex_unlock(&rproc->lock);
16422099c77dSSarangdhar Joshi 
1643b003d45bSSarangdhar Joshi 	rproc_delete_debug_dir(rproc);
1644b003d45bSSarangdhar Joshi 
1645fec47d86SDave Gerlach 	/* the rproc is downref'ed as soon as it's removed from the klist */
1646fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1647fec47d86SDave Gerlach 	list_del(&rproc->node);
1648fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1649fec47d86SDave Gerlach 
1650b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
1651400e64dfSOhad Ben-Cohen 
1652400e64dfSOhad Ben-Cohen 	return 0;
1653400e64dfSOhad Ben-Cohen }
1654160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
1655400e64dfSOhad Ben-Cohen 
16568afd519cSFernando Guzman Lugo /**
16577bdc9650SBjorn Andersson  * rproc_add_subdev() - add a subdevice to a remoteproc
16587bdc9650SBjorn Andersson  * @rproc: rproc handle to add the subdevice to
16597bdc9650SBjorn Andersson  * @subdev: subdev handle to register
16607bdc9650SBjorn Andersson  * @probe: function to call when the rproc boots
16617bdc9650SBjorn Andersson  * @remove: function to call when the rproc shuts down
16627bdc9650SBjorn Andersson  */
16637bdc9650SBjorn Andersson void rproc_add_subdev(struct rproc *rproc,
16647bdc9650SBjorn Andersson 		      struct rproc_subdev *subdev,
16657bdc9650SBjorn Andersson 		      int (*probe)(struct rproc_subdev *subdev),
16667bdc9650SBjorn Andersson 		      void (*remove)(struct rproc_subdev *subdev))
16677bdc9650SBjorn Andersson {
16687bdc9650SBjorn Andersson 	subdev->probe = probe;
16697bdc9650SBjorn Andersson 	subdev->remove = remove;
16707bdc9650SBjorn Andersson 
16717bdc9650SBjorn Andersson 	list_add_tail(&subdev->node, &rproc->subdevs);
16727bdc9650SBjorn Andersson }
16737bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev);
16747bdc9650SBjorn Andersson 
16757bdc9650SBjorn Andersson /**
16767bdc9650SBjorn Andersson  * rproc_remove_subdev() - remove a subdevice from a remoteproc
16777bdc9650SBjorn Andersson  * @rproc: rproc handle to remove the subdevice from
16787bdc9650SBjorn Andersson  * @subdev: subdev handle, previously registered with rproc_add_subdev()
16797bdc9650SBjorn Andersson  */
16807bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
16817bdc9650SBjorn Andersson {
16827bdc9650SBjorn Andersson 	list_del(&subdev->node);
16837bdc9650SBjorn Andersson }
16847bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev);
16857bdc9650SBjorn Andersson 
16867bdc9650SBjorn Andersson /**
16877c89717fSBjorn Andersson  * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
16887c89717fSBjorn Andersson  * @dev:	child device to find ancestor of
16897c89717fSBjorn Andersson  *
16907c89717fSBjorn Andersson  * Returns the ancestor rproc instance, or NULL if not found.
16917c89717fSBjorn Andersson  */
16927c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev)
16937c89717fSBjorn Andersson {
16947c89717fSBjorn Andersson 	for (dev = dev->parent; dev; dev = dev->parent) {
16957c89717fSBjorn Andersson 		if (dev->type == &rproc_type)
16967c89717fSBjorn Andersson 			return dev->driver_data;
16977c89717fSBjorn Andersson 	}
16987c89717fSBjorn Andersson 
16997c89717fSBjorn Andersson 	return NULL;
17007c89717fSBjorn Andersson }
17017c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child);
17027c89717fSBjorn Andersson 
17037c89717fSBjorn Andersson /**
17048afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
17058afd519cSFernando Guzman Lugo  * @rproc: remote processor
17068afd519cSFernando Guzman Lugo  * @type: crash type
17078afd519cSFernando Guzman Lugo  *
17088afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
17098afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
17108afd519cSFernando Guzman Lugo  * non-remoteproc driver.
17118afd519cSFernando Guzman Lugo  *
17128afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
17138afd519cSFernando Guzman Lugo  */
17148afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
17158afd519cSFernando Guzman Lugo {
17168afd519cSFernando Guzman Lugo 	if (!rproc) {
17178afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
17188afd519cSFernando Guzman Lugo 		return;
17198afd519cSFernando Guzman Lugo 	}
17208afd519cSFernando Guzman Lugo 
17218afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
17228afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
17238afd519cSFernando Guzman Lugo 
17248afd519cSFernando Guzman Lugo 	/* create a new task to handle the error */
17258afd519cSFernando Guzman Lugo 	schedule_work(&rproc->crash_handler);
17268afd519cSFernando Guzman Lugo }
17278afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
17288afd519cSFernando Guzman Lugo 
1729400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
1730400e64dfSOhad Ben-Cohen {
17312aefbef0SMatt Redfearn 	rproc_init_sysfs();
1732400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
1733b5ab5e24SOhad Ben-Cohen 
1734400e64dfSOhad Ben-Cohen 	return 0;
1735400e64dfSOhad Ben-Cohen }
1736400e64dfSOhad Ben-Cohen module_init(remoteproc_init);
1737400e64dfSOhad Ben-Cohen 
1738400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
1739400e64dfSOhad Ben-Cohen {
1740f42f79afSSuman Anna 	ida_destroy(&rproc_dev_index);
1741f42f79afSSuman Anna 
1742400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
17432aefbef0SMatt Redfearn 	rproc_exit_sysfs();
1744400e64dfSOhad Ben-Cohen }
1745400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
1746400e64dfSOhad Ben-Cohen 
1747400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
1748400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
1749