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>
36400e64dfSOhad Ben-Cohen #include <linux/remoteproc.h>
37400e64dfSOhad Ben-Cohen #include <linux/iommu.h>
38b5ab5e24SOhad Ben-Cohen #include <linux/idr.h>
39400e64dfSOhad Ben-Cohen #include <linux/elf.h>
40a2b950acSOhad Ben-Cohen #include <linux/crc32.h>
41400e64dfSOhad Ben-Cohen #include <linux/virtio_ids.h>
42400e64dfSOhad Ben-Cohen #include <linux/virtio_ring.h>
43cf59d3e9SOhad Ben-Cohen #include <asm/byteorder.h>
44400e64dfSOhad Ben-Cohen 
45400e64dfSOhad Ben-Cohen #include "remoteproc_internal.h"
46400e64dfSOhad Ben-Cohen 
47fec47d86SDave Gerlach static DEFINE_MUTEX(rproc_list_mutex);
48fec47d86SDave Gerlach static LIST_HEAD(rproc_list);
49fec47d86SDave Gerlach 
50400e64dfSOhad Ben-Cohen typedef int (*rproc_handle_resources_t)(struct rproc *rproc,
51fd2c15ecSOhad Ben-Cohen 				struct resource_table *table, int len);
52a2b950acSOhad Ben-Cohen typedef int (*rproc_handle_resource_t)(struct rproc *rproc,
53a2b950acSOhad Ben-Cohen 				 void *, int offset, int avail);
54400e64dfSOhad Ben-Cohen 
55b5ab5e24SOhad Ben-Cohen /* Unique indices for remoteproc devices */
56b5ab5e24SOhad Ben-Cohen static DEFINE_IDA(rproc_dev_index);
57b5ab5e24SOhad Ben-Cohen 
588afd519cSFernando Guzman Lugo static const char * const rproc_crash_names[] = {
598afd519cSFernando Guzman Lugo 	[RPROC_MMUFAULT]	= "mmufault",
60b3d39032SBjorn Andersson 	[RPROC_WATCHDOG]	= "watchdog",
61b3d39032SBjorn Andersson 	[RPROC_FATAL_ERROR]	= "fatal error",
628afd519cSFernando Guzman Lugo };
638afd519cSFernando Guzman Lugo 
648afd519cSFernando Guzman Lugo /* translate rproc_crash_type to string */
658afd519cSFernando Guzman Lugo static const char *rproc_crash_to_string(enum rproc_crash_type type)
668afd519cSFernando Guzman Lugo {
678afd519cSFernando Guzman Lugo 	if (type < ARRAY_SIZE(rproc_crash_names))
688afd519cSFernando Guzman Lugo 		return rproc_crash_names[type];
69b23f7a09SMasanari Iida 	return "unknown";
708afd519cSFernando Guzman Lugo }
718afd519cSFernando Guzman Lugo 
72400e64dfSOhad Ben-Cohen /*
73400e64dfSOhad Ben-Cohen  * This is the IOMMU fault handler we register with the IOMMU API
74400e64dfSOhad Ben-Cohen  * (when relevant; not all remote processors access memory through
75400e64dfSOhad Ben-Cohen  * an IOMMU).
76400e64dfSOhad Ben-Cohen  *
77400e64dfSOhad Ben-Cohen  * IOMMU core will invoke this handler whenever the remote processor
78400e64dfSOhad Ben-Cohen  * will try to access an unmapped device address.
79400e64dfSOhad Ben-Cohen  */
80400e64dfSOhad Ben-Cohen static int rproc_iommu_fault(struct iommu_domain *domain, struct device *dev,
8177ca2332SOhad Ben-Cohen 			     unsigned long iova, int flags, void *token)
82400e64dfSOhad Ben-Cohen {
838afd519cSFernando Guzman Lugo 	struct rproc *rproc = token;
848afd519cSFernando Guzman Lugo 
85400e64dfSOhad Ben-Cohen 	dev_err(dev, "iommu fault: da 0x%lx flags 0x%x\n", iova, flags);
86400e64dfSOhad Ben-Cohen 
878afd519cSFernando Guzman Lugo 	rproc_report_crash(rproc, RPROC_MMUFAULT);
888afd519cSFernando Guzman Lugo 
89400e64dfSOhad Ben-Cohen 	/*
90400e64dfSOhad Ben-Cohen 	 * Let the iommu core know we're not really handling this fault;
918afd519cSFernando Guzman Lugo 	 * we just used it as a recovery trigger.
92400e64dfSOhad Ben-Cohen 	 */
93400e64dfSOhad Ben-Cohen 	return -ENOSYS;
94400e64dfSOhad Ben-Cohen }
95400e64dfSOhad Ben-Cohen 
96400e64dfSOhad Ben-Cohen static int rproc_enable_iommu(struct rproc *rproc)
97400e64dfSOhad Ben-Cohen {
98400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain;
99b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
100400e64dfSOhad Ben-Cohen 	int ret;
101400e64dfSOhad Ben-Cohen 
102315491e5SSuman Anna 	if (!rproc->has_iommu) {
103315491e5SSuman Anna 		dev_dbg(dev, "iommu not present\n");
1040798e1daSMark Grosen 		return 0;
105400e64dfSOhad Ben-Cohen 	}
106400e64dfSOhad Ben-Cohen 
107400e64dfSOhad Ben-Cohen 	domain = iommu_domain_alloc(dev->bus);
108400e64dfSOhad Ben-Cohen 	if (!domain) {
109400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't alloc iommu domain\n");
110400e64dfSOhad Ben-Cohen 		return -ENOMEM;
111400e64dfSOhad Ben-Cohen 	}
112400e64dfSOhad Ben-Cohen 
11377ca2332SOhad Ben-Cohen 	iommu_set_fault_handler(domain, rproc_iommu_fault, rproc);
114400e64dfSOhad Ben-Cohen 
115400e64dfSOhad Ben-Cohen 	ret = iommu_attach_device(domain, dev);
116400e64dfSOhad Ben-Cohen 	if (ret) {
117400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't attach iommu device: %d\n", ret);
118400e64dfSOhad Ben-Cohen 		goto free_domain;
119400e64dfSOhad Ben-Cohen 	}
120400e64dfSOhad Ben-Cohen 
121400e64dfSOhad Ben-Cohen 	rproc->domain = domain;
122400e64dfSOhad Ben-Cohen 
123400e64dfSOhad Ben-Cohen 	return 0;
124400e64dfSOhad Ben-Cohen 
125400e64dfSOhad Ben-Cohen free_domain:
126400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
127400e64dfSOhad Ben-Cohen 	return ret;
128400e64dfSOhad Ben-Cohen }
129400e64dfSOhad Ben-Cohen 
130400e64dfSOhad Ben-Cohen static void rproc_disable_iommu(struct rproc *rproc)
131400e64dfSOhad Ben-Cohen {
132400e64dfSOhad Ben-Cohen 	struct iommu_domain *domain = rproc->domain;
133b5ab5e24SOhad Ben-Cohen 	struct device *dev = rproc->dev.parent;
134400e64dfSOhad Ben-Cohen 
135400e64dfSOhad Ben-Cohen 	if (!domain)
136400e64dfSOhad Ben-Cohen 		return;
137400e64dfSOhad Ben-Cohen 
138400e64dfSOhad Ben-Cohen 	iommu_detach_device(domain, dev);
139400e64dfSOhad Ben-Cohen 	iommu_domain_free(domain);
140400e64dfSOhad Ben-Cohen }
141400e64dfSOhad Ben-Cohen 
142a01f7cd6SSuman Anna /**
143a01f7cd6SSuman Anna  * rproc_da_to_va() - lookup the kernel virtual address for a remoteproc address
144a01f7cd6SSuman Anna  * @rproc: handle of a remote processor
145a01f7cd6SSuman Anna  * @da: remoteproc device address to translate
146a01f7cd6SSuman Anna  * @len: length of the memory region @da is pointing to
147a01f7cd6SSuman Anna  *
148400e64dfSOhad Ben-Cohen  * Some remote processors will ask us to allocate them physically contiguous
149400e64dfSOhad Ben-Cohen  * memory regions (which we call "carveouts"), and map them to specific
150a01f7cd6SSuman Anna  * device addresses (which are hardcoded in the firmware). They may also have
151a01f7cd6SSuman Anna  * dedicated memory regions internal to the processors, and use them either
152a01f7cd6SSuman Anna  * exclusively or alongside carveouts.
153400e64dfSOhad Ben-Cohen  *
154400e64dfSOhad Ben-Cohen  * They may then ask us to copy objects into specific device addresses (e.g.
155400e64dfSOhad Ben-Cohen  * code/data sections) or expose us certain symbols in other device address
156400e64dfSOhad Ben-Cohen  * (e.g. their trace buffer).
157400e64dfSOhad Ben-Cohen  *
158a01f7cd6SSuman Anna  * This function is a helper function with which we can go over the allocated
159a01f7cd6SSuman Anna  * carveouts and translate specific device addresses to kernel virtual addresses
160a01f7cd6SSuman Anna  * so we can access the referenced memory. This function also allows to perform
161a01f7cd6SSuman Anna  * translations on the internal remoteproc memory regions through a platform
162a01f7cd6SSuman Anna  * implementation specific da_to_va ops, if present.
163a01f7cd6SSuman Anna  *
164a01f7cd6SSuman Anna  * The function returns a valid kernel address on success or NULL on failure.
165400e64dfSOhad Ben-Cohen  *
166400e64dfSOhad Ben-Cohen  * Note: phys_to_virt(iommu_iova_to_phys(rproc->domain, da)) will work too,
167400e64dfSOhad Ben-Cohen  * but only on kernel direct mapped RAM memory. Instead, we're just using
168a01f7cd6SSuman Anna  * here the output of the DMA API for the carveouts, which should be more
169a01f7cd6SSuman Anna  * correct.
170400e64dfSOhad Ben-Cohen  */
17172854fb0SSjur Brændeland void *rproc_da_to_va(struct rproc *rproc, u64 da, int len)
172400e64dfSOhad Ben-Cohen {
173400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout;
174400e64dfSOhad Ben-Cohen 	void *ptr = NULL;
175400e64dfSOhad Ben-Cohen 
176a01f7cd6SSuman Anna 	if (rproc->ops->da_to_va) {
177a01f7cd6SSuman Anna 		ptr = rproc->ops->da_to_va(rproc, da, len);
178a01f7cd6SSuman Anna 		if (ptr)
179a01f7cd6SSuman Anna 			goto out;
180a01f7cd6SSuman Anna 	}
181a01f7cd6SSuman Anna 
182400e64dfSOhad Ben-Cohen 	list_for_each_entry(carveout, &rproc->carveouts, node) {
183400e64dfSOhad Ben-Cohen 		int offset = da - carveout->da;
184400e64dfSOhad Ben-Cohen 
185400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too small */
186400e64dfSOhad Ben-Cohen 		if (offset < 0)
187400e64dfSOhad Ben-Cohen 			continue;
188400e64dfSOhad Ben-Cohen 
189400e64dfSOhad Ben-Cohen 		/* try next carveout if da is too large */
190400e64dfSOhad Ben-Cohen 		if (offset + len > carveout->len)
191400e64dfSOhad Ben-Cohen 			continue;
192400e64dfSOhad Ben-Cohen 
193400e64dfSOhad Ben-Cohen 		ptr = carveout->va + offset;
194400e64dfSOhad Ben-Cohen 
195400e64dfSOhad Ben-Cohen 		break;
196400e64dfSOhad Ben-Cohen 	}
197400e64dfSOhad Ben-Cohen 
198a01f7cd6SSuman Anna out:
199400e64dfSOhad Ben-Cohen 	return ptr;
200400e64dfSOhad Ben-Cohen }
2014afc89d6SSjur Brændeland EXPORT_SYMBOL(rproc_da_to_va);
202400e64dfSOhad Ben-Cohen 
2036db20ea8SOhad Ben-Cohen int rproc_alloc_vring(struct rproc_vdev *rvdev, int i)
204400e64dfSOhad Ben-Cohen {
2057a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
206b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
2076db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
208c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
2097a186941SOhad Ben-Cohen 	dma_addr_t dma;
2107a186941SOhad Ben-Cohen 	void *va;
2117a186941SOhad Ben-Cohen 	int ret, size, notifyid;
212400e64dfSOhad Ben-Cohen 
2136db20ea8SOhad Ben-Cohen 	/* actual size of vring (in bytes) */
2146db20ea8SOhad Ben-Cohen 	size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2156db20ea8SOhad Ben-Cohen 
2166db20ea8SOhad Ben-Cohen 	/*
2176db20ea8SOhad Ben-Cohen 	 * Allocate non-cacheable memory for the vring. In the future
2186db20ea8SOhad Ben-Cohen 	 * this call will also configure the IOMMU for us
2196db20ea8SOhad Ben-Cohen 	 */
220b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, size, &dma, GFP_KERNEL);
2216db20ea8SOhad Ben-Cohen 	if (!va) {
222b5ab5e24SOhad Ben-Cohen 		dev_err(dev->parent, "dma_alloc_coherent failed\n");
2236db20ea8SOhad Ben-Cohen 		return -EINVAL;
2246db20ea8SOhad Ben-Cohen 	}
2256db20ea8SOhad Ben-Cohen 
2266db20ea8SOhad Ben-Cohen 	/*
2276db20ea8SOhad Ben-Cohen 	 * Assign an rproc-wide unique index for this vring
2286db20ea8SOhad Ben-Cohen 	 * TODO: assign a notifyid for rvdev updates as well
2296db20ea8SOhad Ben-Cohen 	 * TODO: support predefined notifyids (via resource table)
2306db20ea8SOhad Ben-Cohen 	 */
23115fc6110STejun Heo 	ret = idr_alloc(&rproc->notifyids, rvring, 0, 0, GFP_KERNEL);
232b39599b7SSuman Anna 	if (ret < 0) {
23315fc6110STejun Heo 		dev_err(dev, "idr_alloc failed: %d\n", ret);
234b5ab5e24SOhad Ben-Cohen 		dma_free_coherent(dev->parent, size, va, dma);
2356db20ea8SOhad Ben-Cohen 		return ret;
2366db20ea8SOhad Ben-Cohen 	}
23715fc6110STejun Heo 	notifyid = ret;
2386db20ea8SOhad Ben-Cohen 
23948f18f89SBjorn Andersson 	/* Potentially bump max_notifyid */
24048f18f89SBjorn Andersson 	if (notifyid > rproc->max_notifyid)
24148f18f89SBjorn Andersson 		rproc->max_notifyid = notifyid;
24248f18f89SBjorn Andersson 
2439d7814a9SAnna, Suman 	dev_dbg(dev, "vring%d: va %p dma %pad size 0x%x idr %d\n",
244b605ed8bSAnna, Suman 		i, va, &dma, size, notifyid);
2456db20ea8SOhad Ben-Cohen 
2466db20ea8SOhad Ben-Cohen 	rvring->va = va;
2476db20ea8SOhad Ben-Cohen 	rvring->dma = dma;
2486db20ea8SOhad Ben-Cohen 	rvring->notifyid = notifyid;
2496db20ea8SOhad Ben-Cohen 
250c0d63157SSjur Brændeland 	/*
251c0d63157SSjur Brændeland 	 * Let the rproc know the notifyid and da of this vring.
252c0d63157SSjur Brændeland 	 * Not all platforms use dma_alloc_coherent to automatically
253c0d63157SSjur Brændeland 	 * set up the iommu. In this case the device address (da) will
254c0d63157SSjur Brændeland 	 * hold the physical address and not the device address.
255c0d63157SSjur Brændeland 	 */
256c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
257c0d63157SSjur Brændeland 	rsc->vring[i].da = dma;
258c0d63157SSjur Brændeland 	rsc->vring[i].notifyid = notifyid;
2596db20ea8SOhad Ben-Cohen 	return 0;
2606db20ea8SOhad Ben-Cohen }
2616db20ea8SOhad Ben-Cohen 
262400e64dfSOhad Ben-Cohen static int
2636db20ea8SOhad Ben-Cohen rproc_parse_vring(struct rproc_vdev *rvdev, struct fw_rsc_vdev *rsc, int i)
264400e64dfSOhad Ben-Cohen {
265400e64dfSOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
266b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
267400e64dfSOhad Ben-Cohen 	struct fw_rsc_vdev_vring *vring = &rsc->vring[i];
2686db20ea8SOhad Ben-Cohen 	struct rproc_vring *rvring = &rvdev->vring[i];
269400e64dfSOhad Ben-Cohen 
2709d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: vring%d: da 0x%x, qsz %d, align %d\n",
2717a186941SOhad Ben-Cohen 		i, vring->da, vring->num, vring->align);
2727a186941SOhad Ben-Cohen 
27363140e0eSOhad Ben-Cohen 	/* verify queue size and vring alignment are sane */
27463140e0eSOhad Ben-Cohen 	if (!vring->num || !vring->align) {
27563140e0eSOhad Ben-Cohen 		dev_err(dev, "invalid qsz (%d) or alignment (%d)\n",
27663140e0eSOhad Ben-Cohen 			vring->num, vring->align);
277400e64dfSOhad Ben-Cohen 		return -EINVAL;
278400e64dfSOhad Ben-Cohen 	}
279400e64dfSOhad Ben-Cohen 
2806db20ea8SOhad Ben-Cohen 	rvring->len = vring->num;
2816db20ea8SOhad Ben-Cohen 	rvring->align = vring->align;
2826db20ea8SOhad Ben-Cohen 	rvring->rvdev = rvdev;
283400e64dfSOhad Ben-Cohen 
284400e64dfSOhad Ben-Cohen 	return 0;
285400e64dfSOhad Ben-Cohen }
286400e64dfSOhad Ben-Cohen 
2876db20ea8SOhad Ben-Cohen void rproc_free_vring(struct rproc_vring *rvring)
2887a186941SOhad Ben-Cohen {
28963140e0eSOhad Ben-Cohen 	int size = PAGE_ALIGN(vring_size(rvring->len, rvring->align));
2906db20ea8SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
291c0d63157SSjur Brændeland 	int idx = rvring->rvdev->vring - rvring;
292c0d63157SSjur Brændeland 	struct fw_rsc_vdev *rsc;
2937a186941SOhad Ben-Cohen 
294b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(rproc->dev.parent, size, rvring->va, rvring->dma);
2957a186941SOhad Ben-Cohen 	idr_remove(&rproc->notifyids, rvring->notifyid);
296099a3f33SSjur Brændeland 
297c0d63157SSjur Brændeland 	/* reset resource entry info */
298c0d63157SSjur Brændeland 	rsc = (void *)rproc->table_ptr + rvring->rvdev->rsc_offset;
299c0d63157SSjur Brændeland 	rsc->vring[idx].da = 0;
300c0d63157SSjur Brændeland 	rsc->vring[idx].notifyid = -1;
3017a186941SOhad Ben-Cohen }
3027a186941SOhad Ben-Cohen 
303f5bcb353SBjorn Andersson static int rproc_vdev_do_probe(struct rproc_subdev *subdev)
304f5bcb353SBjorn Andersson {
305f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
306f5bcb353SBjorn Andersson 
307f5bcb353SBjorn Andersson 	return rproc_add_virtio_dev(rvdev, rvdev->id);
308f5bcb353SBjorn Andersson }
309f5bcb353SBjorn Andersson 
310f5bcb353SBjorn Andersson static void rproc_vdev_do_remove(struct rproc_subdev *subdev)
311f5bcb353SBjorn Andersson {
312f5bcb353SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(subdev, struct rproc_vdev, subdev);
313f5bcb353SBjorn Andersson 
314f5bcb353SBjorn Andersson 	rproc_remove_virtio_dev(rvdev);
315f5bcb353SBjorn Andersson }
316f5bcb353SBjorn Andersson 
317400e64dfSOhad Ben-Cohen /**
318fd2c15ecSOhad Ben-Cohen  * rproc_handle_vdev() - handle a vdev fw resource
319400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
320400e64dfSOhad Ben-Cohen  * @rsc: the vring resource descriptor
321fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
322400e64dfSOhad Ben-Cohen  *
3237a186941SOhad Ben-Cohen  * This resource entry requests the host to statically register a virtio
3247a186941SOhad Ben-Cohen  * device (vdev), and setup everything needed to support it. It contains
3257a186941SOhad Ben-Cohen  * everything needed to make it possible: the virtio device id, virtio
3267a186941SOhad Ben-Cohen  * device features, vrings information, virtio config space, etc...
327400e64dfSOhad Ben-Cohen  *
3287a186941SOhad Ben-Cohen  * Before registering the vdev, the vrings are allocated from non-cacheable
3297a186941SOhad Ben-Cohen  * physically contiguous memory. Currently we only support two vrings per
3307a186941SOhad Ben-Cohen  * remote processor (temporary limitation). We might also want to consider
3317a186941SOhad Ben-Cohen  * doing the vring allocation only later when ->find_vqs() is invoked, and
3327a186941SOhad Ben-Cohen  * then release them upon ->del_vqs().
333400e64dfSOhad Ben-Cohen  *
3347a186941SOhad Ben-Cohen  * Note: @da is currently not really handled correctly: we dynamically
3357a186941SOhad Ben-Cohen  * allocate it using the DMA API, ignoring requested hard coded addresses,
3367a186941SOhad Ben-Cohen  * and we don't take care of any required IOMMU programming. This is all
3377a186941SOhad Ben-Cohen  * going to be taken care of when the generic iommu-based DMA API will be
3387a186941SOhad Ben-Cohen  * merged. Meanwhile, statically-addressed iommu-based firmware images should
3397a186941SOhad Ben-Cohen  * use RSC_DEVMEM resource entries to map their required @da to the physical
3407a186941SOhad Ben-Cohen  * address of their base CMA region (ouch, hacky!).
341400e64dfSOhad Ben-Cohen  *
342400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
343400e64dfSOhad Ben-Cohen  */
344fd2c15ecSOhad Ben-Cohen static int rproc_handle_vdev(struct rproc *rproc, struct fw_rsc_vdev *rsc,
345a2b950acSOhad Ben-Cohen 			     int offset, int avail)
346400e64dfSOhad Ben-Cohen {
347b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3487a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev;
3497a186941SOhad Ben-Cohen 	int i, ret;
350fd2c15ecSOhad Ben-Cohen 
351fd2c15ecSOhad Ben-Cohen 	/* make sure resource isn't truncated */
352fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) + rsc->num_of_vrings * sizeof(struct fw_rsc_vdev_vring)
353fd2c15ecSOhad Ben-Cohen 			+ rsc->config_len > avail) {
354b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vdev rsc is truncated\n");
355fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
356fd2c15ecSOhad Ben-Cohen 	}
357fd2c15ecSOhad Ben-Cohen 
358fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
359fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved[0] || rsc->reserved[1]) {
360fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "vdev rsc has non zero reserved bytes\n");
361fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
362fd2c15ecSOhad Ben-Cohen 	}
363fd2c15ecSOhad Ben-Cohen 
3649d7814a9SAnna, Suman 	dev_dbg(dev, "vdev rsc: id %d, dfeatures 0x%x, cfg len %d, %d vrings\n",
365fd2c15ecSOhad Ben-Cohen 		rsc->id, rsc->dfeatures, rsc->config_len, rsc->num_of_vrings);
366400e64dfSOhad Ben-Cohen 
3677a186941SOhad Ben-Cohen 	/* we currently support only two vrings per rvdev */
3687a186941SOhad Ben-Cohen 	if (rsc->num_of_vrings > ARRAY_SIZE(rvdev->vring)) {
369fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "too many vrings: %d\n", rsc->num_of_vrings);
370400e64dfSOhad Ben-Cohen 		return -EINVAL;
371400e64dfSOhad Ben-Cohen 	}
372400e64dfSOhad Ben-Cohen 
373899585adSAnna, Suman 	rvdev = kzalloc(sizeof(*rvdev), GFP_KERNEL);
3747a186941SOhad Ben-Cohen 	if (!rvdev)
3757a186941SOhad Ben-Cohen 		return -ENOMEM;
3767a186941SOhad Ben-Cohen 
377aab8d802SBjorn Andersson 	kref_init(&rvdev->refcount);
378aab8d802SBjorn Andersson 
379f5bcb353SBjorn Andersson 	rvdev->id = rsc->id;
3807a186941SOhad Ben-Cohen 	rvdev->rproc = rproc;
3817a186941SOhad Ben-Cohen 
3826db20ea8SOhad Ben-Cohen 	/* parse the vrings */
383fd2c15ecSOhad Ben-Cohen 	for (i = 0; i < rsc->num_of_vrings; i++) {
3846db20ea8SOhad Ben-Cohen 		ret = rproc_parse_vring(rvdev, rsc, i);
3857a186941SOhad Ben-Cohen 		if (ret)
3866db20ea8SOhad Ben-Cohen 			goto free_rvdev;
387fd2c15ecSOhad Ben-Cohen 	}
388fd2c15ecSOhad Ben-Cohen 
389a2b950acSOhad Ben-Cohen 	/* remember the resource offset*/
390a2b950acSOhad Ben-Cohen 	rvdev->rsc_offset = offset;
391400e64dfSOhad Ben-Cohen 
392a863af5dSBjorn Andersson 	/* allocate the vring resources */
393a863af5dSBjorn Andersson 	for (i = 0; i < rsc->num_of_vrings; i++) {
394a863af5dSBjorn Andersson 		ret = rproc_alloc_vring(rvdev, i);
395a863af5dSBjorn Andersson 		if (ret)
396a863af5dSBjorn Andersson 			goto unwind_vring_allocations;
397a863af5dSBjorn Andersson 	}
398a863af5dSBjorn Andersson 
3997a186941SOhad Ben-Cohen 	list_add_tail(&rvdev->node, &rproc->rvdevs);
400400e64dfSOhad Ben-Cohen 
401f5bcb353SBjorn Andersson 	rproc_add_subdev(rproc, &rvdev->subdev,
402f5bcb353SBjorn Andersson 			 rproc_vdev_do_probe, rproc_vdev_do_remove);
403400e64dfSOhad Ben-Cohen 
404400e64dfSOhad Ben-Cohen 	return 0;
4057a186941SOhad Ben-Cohen 
406a863af5dSBjorn Andersson unwind_vring_allocations:
407a863af5dSBjorn Andersson 	for (i--; i >= 0; i--)
408a863af5dSBjorn Andersson 		rproc_free_vring(&rvdev->vring[i]);
4096db20ea8SOhad Ben-Cohen free_rvdev:
4107a186941SOhad Ben-Cohen 	kfree(rvdev);
4117a186941SOhad Ben-Cohen 	return ret;
412400e64dfSOhad Ben-Cohen }
413400e64dfSOhad Ben-Cohen 
414aab8d802SBjorn Andersson void rproc_vdev_release(struct kref *ref)
415aab8d802SBjorn Andersson {
416aab8d802SBjorn Andersson 	struct rproc_vdev *rvdev = container_of(ref, struct rproc_vdev, refcount);
417a863af5dSBjorn Andersson 	struct rproc_vring *rvring;
418f5bcb353SBjorn Andersson 	struct rproc *rproc = rvdev->rproc;
419a863af5dSBjorn Andersson 	int id;
420a863af5dSBjorn Andersson 
421a863af5dSBjorn Andersson 	for (id = 0; id < ARRAY_SIZE(rvdev->vring); id++) {
422a863af5dSBjorn Andersson 		rvring = &rvdev->vring[id];
423a863af5dSBjorn Andersson 		if (!rvring->va)
424a863af5dSBjorn Andersson 			continue;
425a863af5dSBjorn Andersson 
426a863af5dSBjorn Andersson 		rproc_free_vring(rvring);
427a863af5dSBjorn Andersson 	}
428aab8d802SBjorn Andersson 
429f5bcb353SBjorn Andersson 	rproc_remove_subdev(rproc, &rvdev->subdev);
430aab8d802SBjorn Andersson 	list_del(&rvdev->node);
431aab8d802SBjorn Andersson 	kfree(rvdev);
432aab8d802SBjorn Andersson }
433aab8d802SBjorn Andersson 
434400e64dfSOhad Ben-Cohen /**
435400e64dfSOhad Ben-Cohen  * rproc_handle_trace() - handle a shared trace buffer resource
436400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
437400e64dfSOhad Ben-Cohen  * @rsc: the trace resource descriptor
438fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
439400e64dfSOhad Ben-Cohen  *
440400e64dfSOhad Ben-Cohen  * In case the remote processor dumps trace logs into memory,
441400e64dfSOhad Ben-Cohen  * export it via debugfs.
442400e64dfSOhad Ben-Cohen  *
443400e64dfSOhad Ben-Cohen  * Currently, the 'da' member of @rsc should contain the device address
444400e64dfSOhad Ben-Cohen  * where the remote processor is dumping the traces. Later we could also
445400e64dfSOhad Ben-Cohen  * support dynamically allocating this address using the generic
446400e64dfSOhad Ben-Cohen  * DMA API (but currently there isn't a use case for that).
447400e64dfSOhad Ben-Cohen  *
448400e64dfSOhad Ben-Cohen  * Returns 0 on success, or an appropriate error code otherwise
449400e64dfSOhad Ben-Cohen  */
450fd2c15ecSOhad Ben-Cohen static int rproc_handle_trace(struct rproc *rproc, struct fw_rsc_trace *rsc,
451a2b950acSOhad Ben-Cohen 			      int offset, int avail)
452400e64dfSOhad Ben-Cohen {
453400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *trace;
454b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
455400e64dfSOhad Ben-Cohen 	void *ptr;
456400e64dfSOhad Ben-Cohen 	char name[15];
457400e64dfSOhad Ben-Cohen 
458fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
459b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "trace rsc is truncated\n");
460fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
461fd2c15ecSOhad Ben-Cohen 	}
462fd2c15ecSOhad Ben-Cohen 
463fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
464fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
465fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "trace rsc has non zero reserved bytes\n");
466fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
467fd2c15ecSOhad Ben-Cohen 	}
468fd2c15ecSOhad Ben-Cohen 
469400e64dfSOhad Ben-Cohen 	/* what's the kernel address of this resource ? */
470400e64dfSOhad Ben-Cohen 	ptr = rproc_da_to_va(rproc, rsc->da, rsc->len);
471400e64dfSOhad Ben-Cohen 	if (!ptr) {
472400e64dfSOhad Ben-Cohen 		dev_err(dev, "erroneous trace resource entry\n");
473400e64dfSOhad Ben-Cohen 		return -EINVAL;
474400e64dfSOhad Ben-Cohen 	}
475400e64dfSOhad Ben-Cohen 
476400e64dfSOhad Ben-Cohen 	trace = kzalloc(sizeof(*trace), GFP_KERNEL);
477172e6ab1SSuman Anna 	if (!trace)
478400e64dfSOhad Ben-Cohen 		return -ENOMEM;
479400e64dfSOhad Ben-Cohen 
480400e64dfSOhad Ben-Cohen 	/* set the trace buffer dma properties */
481400e64dfSOhad Ben-Cohen 	trace->len = rsc->len;
482400e64dfSOhad Ben-Cohen 	trace->va = ptr;
483400e64dfSOhad Ben-Cohen 
484400e64dfSOhad Ben-Cohen 	/* make sure snprintf always null terminates, even if truncating */
485400e64dfSOhad Ben-Cohen 	snprintf(name, sizeof(name), "trace%d", rproc->num_traces);
486400e64dfSOhad Ben-Cohen 
487400e64dfSOhad Ben-Cohen 	/* create the debugfs entry */
488400e64dfSOhad Ben-Cohen 	trace->priv = rproc_create_trace_file(name, rproc, trace);
489400e64dfSOhad Ben-Cohen 	if (!trace->priv) {
490400e64dfSOhad Ben-Cohen 		trace->va = NULL;
491400e64dfSOhad Ben-Cohen 		kfree(trace);
492400e64dfSOhad Ben-Cohen 		return -EINVAL;
493400e64dfSOhad Ben-Cohen 	}
494400e64dfSOhad Ben-Cohen 
495400e64dfSOhad Ben-Cohen 	list_add_tail(&trace->node, &rproc->traces);
496400e64dfSOhad Ben-Cohen 
497400e64dfSOhad Ben-Cohen 	rproc->num_traces++;
498400e64dfSOhad Ben-Cohen 
49935386166SLee Jones 	dev_dbg(dev, "%s added: va %p, da 0x%x, len 0x%x\n",
50035386166SLee Jones 		name, ptr, rsc->da, rsc->len);
501400e64dfSOhad Ben-Cohen 
502400e64dfSOhad Ben-Cohen 	return 0;
503400e64dfSOhad Ben-Cohen }
504400e64dfSOhad Ben-Cohen 
505400e64dfSOhad Ben-Cohen /**
506400e64dfSOhad Ben-Cohen  * rproc_handle_devmem() - handle devmem resource entry
507400e64dfSOhad Ben-Cohen  * @rproc: remote processor handle
508400e64dfSOhad Ben-Cohen  * @rsc: the devmem resource entry
509fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for sanity checking the image)
510400e64dfSOhad Ben-Cohen  *
511400e64dfSOhad Ben-Cohen  * Remote processors commonly need to access certain on-chip peripherals.
512400e64dfSOhad Ben-Cohen  *
513400e64dfSOhad Ben-Cohen  * Some of these remote processors access memory via an iommu device,
514400e64dfSOhad Ben-Cohen  * and might require us to configure their iommu before they can access
515400e64dfSOhad Ben-Cohen  * the on-chip peripherals they need.
516400e64dfSOhad Ben-Cohen  *
517400e64dfSOhad Ben-Cohen  * This resource entry is a request to map such a peripheral device.
518400e64dfSOhad Ben-Cohen  *
519400e64dfSOhad Ben-Cohen  * These devmem entries will contain the physical address of the device in
520400e64dfSOhad Ben-Cohen  * the 'pa' member. If a specific device address is expected, then 'da' will
521400e64dfSOhad Ben-Cohen  * contain it (currently this is the only use case supported). 'len' will
522400e64dfSOhad Ben-Cohen  * contain the size of the physical region we need to map.
523400e64dfSOhad Ben-Cohen  *
524400e64dfSOhad Ben-Cohen  * Currently we just "trust" those devmem entries to contain valid physical
525400e64dfSOhad Ben-Cohen  * addresses, but this is going to change: we want the implementations to
526400e64dfSOhad Ben-Cohen  * tell us ranges of physical addresses the firmware is allowed to request,
527400e64dfSOhad Ben-Cohen  * and not allow firmwares to request access to physical addresses that
528400e64dfSOhad Ben-Cohen  * are outside those ranges.
529400e64dfSOhad Ben-Cohen  */
530fd2c15ecSOhad Ben-Cohen static int rproc_handle_devmem(struct rproc *rproc, struct fw_rsc_devmem *rsc,
531a2b950acSOhad Ben-Cohen 			       int offset, int avail)
532400e64dfSOhad Ben-Cohen {
533400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *mapping;
534b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
535400e64dfSOhad Ben-Cohen 	int ret;
536400e64dfSOhad Ben-Cohen 
537400e64dfSOhad Ben-Cohen 	/* no point in handling this resource without a valid iommu domain */
538400e64dfSOhad Ben-Cohen 	if (!rproc->domain)
539400e64dfSOhad Ben-Cohen 		return -EINVAL;
540400e64dfSOhad Ben-Cohen 
541fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
542b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc is truncated\n");
543fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
544fd2c15ecSOhad Ben-Cohen 	}
545fd2c15ecSOhad Ben-Cohen 
546fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
547fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
548b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "devmem rsc has non zero reserved bytes\n");
549fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
550fd2c15ecSOhad Ben-Cohen 	}
551fd2c15ecSOhad Ben-Cohen 
552400e64dfSOhad Ben-Cohen 	mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
553172e6ab1SSuman Anna 	if (!mapping)
554400e64dfSOhad Ben-Cohen 		return -ENOMEM;
555400e64dfSOhad Ben-Cohen 
556400e64dfSOhad Ben-Cohen 	ret = iommu_map(rproc->domain, rsc->da, rsc->pa, rsc->len, rsc->flags);
557400e64dfSOhad Ben-Cohen 	if (ret) {
558b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "failed to map devmem: %d\n", ret);
559400e64dfSOhad Ben-Cohen 		goto out;
560400e64dfSOhad Ben-Cohen 	}
561400e64dfSOhad Ben-Cohen 
562400e64dfSOhad Ben-Cohen 	/*
563400e64dfSOhad Ben-Cohen 	 * We'll need this info later when we'll want to unmap everything
564400e64dfSOhad Ben-Cohen 	 * (e.g. on shutdown).
565400e64dfSOhad Ben-Cohen 	 *
566400e64dfSOhad Ben-Cohen 	 * We can't trust the remote processor not to change the resource
567400e64dfSOhad Ben-Cohen 	 * table, so we must maintain this info independently.
568400e64dfSOhad Ben-Cohen 	 */
569400e64dfSOhad Ben-Cohen 	mapping->da = rsc->da;
570400e64dfSOhad Ben-Cohen 	mapping->len = rsc->len;
571400e64dfSOhad Ben-Cohen 	list_add_tail(&mapping->node, &rproc->mappings);
572400e64dfSOhad Ben-Cohen 
573b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "mapped devmem pa 0x%x, da 0x%x, len 0x%x\n",
574400e64dfSOhad Ben-Cohen 		rsc->pa, rsc->da, rsc->len);
575400e64dfSOhad Ben-Cohen 
576400e64dfSOhad Ben-Cohen 	return 0;
577400e64dfSOhad Ben-Cohen 
578400e64dfSOhad Ben-Cohen out:
579400e64dfSOhad Ben-Cohen 	kfree(mapping);
580400e64dfSOhad Ben-Cohen 	return ret;
581400e64dfSOhad Ben-Cohen }
582400e64dfSOhad Ben-Cohen 
583400e64dfSOhad Ben-Cohen /**
584400e64dfSOhad Ben-Cohen  * rproc_handle_carveout() - handle phys contig memory allocation requests
585400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
586400e64dfSOhad Ben-Cohen  * @rsc: the resource entry
587fd2c15ecSOhad Ben-Cohen  * @avail: size of available data (for image validation)
588400e64dfSOhad Ben-Cohen  *
589400e64dfSOhad Ben-Cohen  * This function will handle firmware requests for allocation of physically
590400e64dfSOhad Ben-Cohen  * contiguous memory regions.
591400e64dfSOhad Ben-Cohen  *
592400e64dfSOhad Ben-Cohen  * These request entries should come first in the firmware's resource table,
593400e64dfSOhad Ben-Cohen  * as other firmware entries might request placing other data objects inside
594400e64dfSOhad Ben-Cohen  * these memory regions (e.g. data/code segments, trace resource entries, ...).
595400e64dfSOhad Ben-Cohen  *
596400e64dfSOhad Ben-Cohen  * Allocating memory this way helps utilizing the reserved physical memory
597400e64dfSOhad Ben-Cohen  * (e.g. CMA) more efficiently, and also minimizes the number of TLB entries
598400e64dfSOhad Ben-Cohen  * needed to map it (in case @rproc is using an IOMMU). Reducing the TLB
599400e64dfSOhad Ben-Cohen  * pressure is important; it may have a substantial impact on performance.
600400e64dfSOhad Ben-Cohen  */
601fd2c15ecSOhad Ben-Cohen static int rproc_handle_carveout(struct rproc *rproc,
602a2b950acSOhad Ben-Cohen 				 struct fw_rsc_carveout *rsc,
603a2b950acSOhad Ben-Cohen 				 int offset, int avail)
604400e64dfSOhad Ben-Cohen {
605400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *carveout, *mapping;
606b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
607400e64dfSOhad Ben-Cohen 	dma_addr_t dma;
608400e64dfSOhad Ben-Cohen 	void *va;
609400e64dfSOhad Ben-Cohen 	int ret;
610400e64dfSOhad Ben-Cohen 
611fd2c15ecSOhad Ben-Cohen 	if (sizeof(*rsc) > avail) {
612b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "carveout rsc is truncated\n");
613fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
614fd2c15ecSOhad Ben-Cohen 	}
615fd2c15ecSOhad Ben-Cohen 
616fd2c15ecSOhad Ben-Cohen 	/* make sure reserved bytes are zeroes */
617fd2c15ecSOhad Ben-Cohen 	if (rsc->reserved) {
618fd2c15ecSOhad Ben-Cohen 		dev_err(dev, "carveout rsc has non zero reserved bytes\n");
619fd2c15ecSOhad Ben-Cohen 		return -EINVAL;
620fd2c15ecSOhad Ben-Cohen 	}
621fd2c15ecSOhad Ben-Cohen 
6229d7814a9SAnna, Suman 	dev_dbg(dev, "carveout rsc: name: %s, da 0x%x, pa 0x%x, len 0x%x, flags 0x%x\n",
62335386166SLee Jones 		rsc->name, rsc->da, rsc->pa, rsc->len, rsc->flags);
624fd2c15ecSOhad Ben-Cohen 
625400e64dfSOhad Ben-Cohen 	carveout = kzalloc(sizeof(*carveout), GFP_KERNEL);
626172e6ab1SSuman Anna 	if (!carveout)
6277168d914SDan Carpenter 		return -ENOMEM;
628400e64dfSOhad Ben-Cohen 
629b5ab5e24SOhad Ben-Cohen 	va = dma_alloc_coherent(dev->parent, rsc->len, &dma, GFP_KERNEL);
630400e64dfSOhad Ben-Cohen 	if (!va) {
6319c219b23SLee Jones 		dev_err(dev->parent,
6329c219b23SLee Jones 			"failed to allocate dma memory: len 0x%x\n", rsc->len);
633400e64dfSOhad Ben-Cohen 		ret = -ENOMEM;
634400e64dfSOhad Ben-Cohen 		goto free_carv;
635400e64dfSOhad Ben-Cohen 	}
636400e64dfSOhad Ben-Cohen 
637b605ed8bSAnna, Suman 	dev_dbg(dev, "carveout va %p, dma %pad, len 0x%x\n",
638b605ed8bSAnna, Suman 		va, &dma, rsc->len);
639400e64dfSOhad Ben-Cohen 
640400e64dfSOhad Ben-Cohen 	/*
641400e64dfSOhad Ben-Cohen 	 * Ok, this is non-standard.
642400e64dfSOhad Ben-Cohen 	 *
643400e64dfSOhad Ben-Cohen 	 * Sometimes we can't rely on the generic iommu-based DMA API
644400e64dfSOhad Ben-Cohen 	 * to dynamically allocate the device address and then set the IOMMU
645400e64dfSOhad Ben-Cohen 	 * tables accordingly, because some remote processors might
646400e64dfSOhad Ben-Cohen 	 * _require_ us to use hard coded device addresses that their
647400e64dfSOhad Ben-Cohen 	 * firmware was compiled with.
648400e64dfSOhad Ben-Cohen 	 *
649400e64dfSOhad Ben-Cohen 	 * In this case, we must use the IOMMU API directly and map
650400e64dfSOhad Ben-Cohen 	 * the memory to the device address as expected by the remote
651400e64dfSOhad Ben-Cohen 	 * processor.
652400e64dfSOhad Ben-Cohen 	 *
653400e64dfSOhad Ben-Cohen 	 * Obviously such remote processor devices should not be configured
654400e64dfSOhad Ben-Cohen 	 * to use the iommu-based DMA API: we expect 'dma' to contain the
655400e64dfSOhad Ben-Cohen 	 * physical address in this case.
656400e64dfSOhad Ben-Cohen 	 */
657400e64dfSOhad Ben-Cohen 	if (rproc->domain) {
6587168d914SDan Carpenter 		mapping = kzalloc(sizeof(*mapping), GFP_KERNEL);
6597168d914SDan Carpenter 		if (!mapping) {
6607168d914SDan Carpenter 			ret = -ENOMEM;
6617168d914SDan Carpenter 			goto dma_free;
6627168d914SDan Carpenter 		}
6637168d914SDan Carpenter 
664400e64dfSOhad Ben-Cohen 		ret = iommu_map(rproc->domain, rsc->da, dma, rsc->len,
665400e64dfSOhad Ben-Cohen 				rsc->flags);
666400e64dfSOhad Ben-Cohen 		if (ret) {
667400e64dfSOhad Ben-Cohen 			dev_err(dev, "iommu_map failed: %d\n", ret);
6687168d914SDan Carpenter 			goto free_mapping;
669400e64dfSOhad Ben-Cohen 		}
670400e64dfSOhad Ben-Cohen 
671400e64dfSOhad Ben-Cohen 		/*
672400e64dfSOhad Ben-Cohen 		 * We'll need this info later when we'll want to unmap
673400e64dfSOhad Ben-Cohen 		 * everything (e.g. on shutdown).
674400e64dfSOhad Ben-Cohen 		 *
675400e64dfSOhad Ben-Cohen 		 * We can't trust the remote processor not to change the
676400e64dfSOhad Ben-Cohen 		 * resource table, so we must maintain this info independently.
677400e64dfSOhad Ben-Cohen 		 */
678400e64dfSOhad Ben-Cohen 		mapping->da = rsc->da;
679400e64dfSOhad Ben-Cohen 		mapping->len = rsc->len;
680400e64dfSOhad Ben-Cohen 		list_add_tail(&mapping->node, &rproc->mappings);
681400e64dfSOhad Ben-Cohen 
682b605ed8bSAnna, Suman 		dev_dbg(dev, "carveout mapped 0x%x to %pad\n",
683b605ed8bSAnna, Suman 			rsc->da, &dma);
6840e49b72cSOhad Ben-Cohen 	}
685400e64dfSOhad Ben-Cohen 
686400e64dfSOhad Ben-Cohen 	/*
687400e64dfSOhad Ben-Cohen 	 * Some remote processors might need to know the pa
688400e64dfSOhad Ben-Cohen 	 * even though they are behind an IOMMU. E.g., OMAP4's
689400e64dfSOhad Ben-Cohen 	 * remote M3 processor needs this so it can control
690400e64dfSOhad Ben-Cohen 	 * on-chip hardware accelerators that are not behind
691400e64dfSOhad Ben-Cohen 	 * the IOMMU, and therefor must know the pa.
692400e64dfSOhad Ben-Cohen 	 *
693400e64dfSOhad Ben-Cohen 	 * Generally we don't want to expose physical addresses
694400e64dfSOhad Ben-Cohen 	 * if we don't have to (remote processors are generally
695400e64dfSOhad Ben-Cohen 	 * _not_ trusted), so we might want to do this only for
696400e64dfSOhad Ben-Cohen 	 * remote processor that _must_ have this (e.g. OMAP4's
697400e64dfSOhad Ben-Cohen 	 * dual M3 subsystem).
6980e49b72cSOhad Ben-Cohen 	 *
6990e49b72cSOhad Ben-Cohen 	 * Non-IOMMU processors might also want to have this info.
7000e49b72cSOhad Ben-Cohen 	 * In this case, the device address and the physical address
7010e49b72cSOhad Ben-Cohen 	 * are the same.
702400e64dfSOhad Ben-Cohen 	 */
703400e64dfSOhad Ben-Cohen 	rsc->pa = dma;
704400e64dfSOhad Ben-Cohen 
705400e64dfSOhad Ben-Cohen 	carveout->va = va;
706400e64dfSOhad Ben-Cohen 	carveout->len = rsc->len;
707400e64dfSOhad Ben-Cohen 	carveout->dma = dma;
708400e64dfSOhad Ben-Cohen 	carveout->da = rsc->da;
709400e64dfSOhad Ben-Cohen 
710400e64dfSOhad Ben-Cohen 	list_add_tail(&carveout->node, &rproc->carveouts);
711400e64dfSOhad Ben-Cohen 
712400e64dfSOhad Ben-Cohen 	return 0;
713400e64dfSOhad Ben-Cohen 
7147168d914SDan Carpenter free_mapping:
7157168d914SDan Carpenter 	kfree(mapping);
716400e64dfSOhad Ben-Cohen dma_free:
717b5ab5e24SOhad Ben-Cohen 	dma_free_coherent(dev->parent, rsc->len, va, dma);
718400e64dfSOhad Ben-Cohen free_carv:
719400e64dfSOhad Ben-Cohen 	kfree(carveout);
720400e64dfSOhad Ben-Cohen 	return ret;
721400e64dfSOhad Ben-Cohen }
722400e64dfSOhad Ben-Cohen 
723e12bc14bSOhad Ben-Cohen /*
724e12bc14bSOhad Ben-Cohen  * A lookup table for resource handlers. The indices are defined in
725e12bc14bSOhad Ben-Cohen  * enum fw_resource_type.
726e12bc14bSOhad Ben-Cohen  */
727232fcdbbSSjur Brændeland static rproc_handle_resource_t rproc_loading_handlers[RSC_LAST] = {
728fd2c15ecSOhad Ben-Cohen 	[RSC_CARVEOUT] = (rproc_handle_resource_t)rproc_handle_carveout,
729fd2c15ecSOhad Ben-Cohen 	[RSC_DEVMEM] = (rproc_handle_resource_t)rproc_handle_devmem,
730fd2c15ecSOhad Ben-Cohen 	[RSC_TRACE] = (rproc_handle_resource_t)rproc_handle_trace,
731232fcdbbSSjur Brændeland 	[RSC_VDEV] = (rproc_handle_resource_t)rproc_handle_vdev,
732232fcdbbSSjur Brændeland };
733232fcdbbSSjur Brændeland 
734400e64dfSOhad Ben-Cohen /* handle firmware resource entries before booting the remote processor */
735a4b24c75SBjorn Andersson static int rproc_handle_resources(struct rproc *rproc,
736232fcdbbSSjur Brændeland 				  rproc_handle_resource_t handlers[RSC_LAST])
737400e64dfSOhad Ben-Cohen {
738b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
739e12bc14bSOhad Ben-Cohen 	rproc_handle_resource_t handler;
740fd2c15ecSOhad Ben-Cohen 	int ret = 0, i;
741400e64dfSOhad Ben-Cohen 
742d4bb86f2SBjorn Andersson 	if (!rproc->table_ptr)
743d4bb86f2SBjorn Andersson 		return 0;
744d4bb86f2SBjorn Andersson 
745a2b950acSOhad Ben-Cohen 	for (i = 0; i < rproc->table_ptr->num; i++) {
746a2b950acSOhad Ben-Cohen 		int offset = rproc->table_ptr->offset[i];
747a2b950acSOhad Ben-Cohen 		struct fw_rsc_hdr *hdr = (void *)rproc->table_ptr + offset;
748a4b24c75SBjorn Andersson 		int avail = rproc->table_sz - offset - sizeof(*hdr);
749fd2c15ecSOhad Ben-Cohen 		void *rsc = (void *)hdr + sizeof(*hdr);
750400e64dfSOhad Ben-Cohen 
751fd2c15ecSOhad Ben-Cohen 		/* make sure table isn't truncated */
752fd2c15ecSOhad Ben-Cohen 		if (avail < 0) {
753fd2c15ecSOhad Ben-Cohen 			dev_err(dev, "rsc table is truncated\n");
754fd2c15ecSOhad Ben-Cohen 			return -EINVAL;
755fd2c15ecSOhad Ben-Cohen 		}
756fd2c15ecSOhad Ben-Cohen 
757fd2c15ecSOhad Ben-Cohen 		dev_dbg(dev, "rsc: type %d\n", hdr->type);
758fd2c15ecSOhad Ben-Cohen 
759fd2c15ecSOhad Ben-Cohen 		if (hdr->type >= RSC_LAST) {
760fd2c15ecSOhad Ben-Cohen 			dev_warn(dev, "unsupported resource %d\n", hdr->type);
761e12bc14bSOhad Ben-Cohen 			continue;
762400e64dfSOhad Ben-Cohen 		}
763400e64dfSOhad Ben-Cohen 
764232fcdbbSSjur Brændeland 		handler = handlers[hdr->type];
765e12bc14bSOhad Ben-Cohen 		if (!handler)
766e12bc14bSOhad Ben-Cohen 			continue;
767e12bc14bSOhad Ben-Cohen 
768a2b950acSOhad Ben-Cohen 		ret = handler(rproc, rsc, offset + sizeof(*hdr), avail);
7697a186941SOhad Ben-Cohen 		if (ret)
770400e64dfSOhad Ben-Cohen 			break;
771400e64dfSOhad Ben-Cohen 	}
772400e64dfSOhad Ben-Cohen 
773400e64dfSOhad Ben-Cohen 	return ret;
774400e64dfSOhad Ben-Cohen }
775400e64dfSOhad Ben-Cohen 
7767bdc9650SBjorn Andersson static int rproc_probe_subdevices(struct rproc *rproc)
7777bdc9650SBjorn Andersson {
7787bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
7797bdc9650SBjorn Andersson 	int ret;
7807bdc9650SBjorn Andersson 
7817bdc9650SBjorn Andersson 	list_for_each_entry(subdev, &rproc->subdevs, node) {
7827bdc9650SBjorn Andersson 		ret = subdev->probe(subdev);
7837bdc9650SBjorn Andersson 		if (ret)
7847bdc9650SBjorn Andersson 			goto unroll_registration;
7857bdc9650SBjorn Andersson 	}
7867bdc9650SBjorn Andersson 
7877bdc9650SBjorn Andersson 	return 0;
7887bdc9650SBjorn Andersson 
7897bdc9650SBjorn Andersson unroll_registration:
7907bdc9650SBjorn Andersson 	list_for_each_entry_continue_reverse(subdev, &rproc->subdevs, node)
7917bdc9650SBjorn Andersson 		subdev->remove(subdev);
7927bdc9650SBjorn Andersson 
7937bdc9650SBjorn Andersson 	return ret;
7947bdc9650SBjorn Andersson }
7957bdc9650SBjorn Andersson 
7967bdc9650SBjorn Andersson static void rproc_remove_subdevices(struct rproc *rproc)
7977bdc9650SBjorn Andersson {
7987bdc9650SBjorn Andersson 	struct rproc_subdev *subdev;
7997bdc9650SBjorn Andersson 
800f9cbbd25SBjorn Andersson 	list_for_each_entry_reverse(subdev, &rproc->subdevs, node)
8017bdc9650SBjorn Andersson 		subdev->remove(subdev);
8027bdc9650SBjorn Andersson }
8037bdc9650SBjorn Andersson 
804400e64dfSOhad Ben-Cohen /**
805400e64dfSOhad Ben-Cohen  * rproc_resource_cleanup() - clean up and free all acquired resources
806400e64dfSOhad Ben-Cohen  * @rproc: rproc handle
807400e64dfSOhad Ben-Cohen  *
808400e64dfSOhad Ben-Cohen  * This function will free all resources acquired for @rproc, and it
8097a186941SOhad Ben-Cohen  * is called whenever @rproc either shuts down or fails to boot.
810400e64dfSOhad Ben-Cohen  */
811400e64dfSOhad Ben-Cohen static void rproc_resource_cleanup(struct rproc *rproc)
812400e64dfSOhad Ben-Cohen {
813400e64dfSOhad Ben-Cohen 	struct rproc_mem_entry *entry, *tmp;
814d81fb32fSBjorn Andersson 	struct rproc_vdev *rvdev, *rvtmp;
815b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
816400e64dfSOhad Ben-Cohen 
817400e64dfSOhad Ben-Cohen 	/* clean up debugfs trace entries */
818400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->traces, node) {
819400e64dfSOhad Ben-Cohen 		rproc_remove_trace_file(entry->priv);
820400e64dfSOhad Ben-Cohen 		rproc->num_traces--;
821400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
822400e64dfSOhad Ben-Cohen 		kfree(entry);
823400e64dfSOhad Ben-Cohen 	}
824400e64dfSOhad Ben-Cohen 
825400e64dfSOhad Ben-Cohen 	/* clean up iommu mapping entries */
826400e64dfSOhad Ben-Cohen 	list_for_each_entry_safe(entry, tmp, &rproc->mappings, node) {
827400e64dfSOhad Ben-Cohen 		size_t unmapped;
828400e64dfSOhad Ben-Cohen 
829400e64dfSOhad Ben-Cohen 		unmapped = iommu_unmap(rproc->domain, entry->da, entry->len);
830400e64dfSOhad Ben-Cohen 		if (unmapped != entry->len) {
831400e64dfSOhad Ben-Cohen 			/* nothing much to do besides complaining */
832e981f6d4SSjur Brændeland 			dev_err(dev, "failed to unmap %u/%zu\n", entry->len,
833400e64dfSOhad Ben-Cohen 				unmapped);
834400e64dfSOhad Ben-Cohen 		}
835400e64dfSOhad Ben-Cohen 
836400e64dfSOhad Ben-Cohen 		list_del(&entry->node);
837400e64dfSOhad Ben-Cohen 		kfree(entry);
838400e64dfSOhad Ben-Cohen 	}
839b6356a01SSuman Anna 
840b6356a01SSuman Anna 	/* clean up carveout allocations */
841b6356a01SSuman Anna 	list_for_each_entry_safe(entry, tmp, &rproc->carveouts, node) {
842172e6ab1SSuman Anna 		dma_free_coherent(dev->parent, entry->len, entry->va,
843172e6ab1SSuman Anna 				  entry->dma);
844b6356a01SSuman Anna 		list_del(&entry->node);
845b6356a01SSuman Anna 		kfree(entry);
846b6356a01SSuman Anna 	}
847d81fb32fSBjorn Andersson 
848d81fb32fSBjorn Andersson 	/* clean up remote vdev entries */
849f5bcb353SBjorn Andersson 	list_for_each_entry_safe(rvdev, rvtmp, &rproc->rvdevs, node)
8502b45cef5SBjorn Andersson 		kref_put(&rvdev->refcount, rproc_vdev_release);
8512b45cef5SBjorn Andersson }
852400e64dfSOhad Ben-Cohen 
8531efa30d0SSarangdhar Joshi static int rproc_start(struct rproc *rproc, const struct firmware *fw)
8541efa30d0SSarangdhar Joshi {
855a4b24c75SBjorn Andersson 	struct resource_table *loaded_table;
8561efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
857a4b24c75SBjorn Andersson 	int ret;
8581efa30d0SSarangdhar Joshi 
8591efa30d0SSarangdhar Joshi 	/* load the ELF segments to memory */
8601efa30d0SSarangdhar Joshi 	ret = rproc_load_segments(rproc, fw);
8611efa30d0SSarangdhar Joshi 	if (ret) {
8621efa30d0SSarangdhar Joshi 		dev_err(dev, "Failed to load program segments: %d\n", ret);
8631efa30d0SSarangdhar Joshi 		return ret;
8641efa30d0SSarangdhar Joshi 	}
8651efa30d0SSarangdhar Joshi 
8661efa30d0SSarangdhar Joshi 	/*
8671efa30d0SSarangdhar Joshi 	 * The starting device has been given the rproc->cached_table as the
8681efa30d0SSarangdhar Joshi 	 * resource table. The address of the vring along with the other
8691efa30d0SSarangdhar Joshi 	 * allocated resources (carveouts etc) is stored in cached_table.
8701efa30d0SSarangdhar Joshi 	 * In order to pass this information to the remote device we must copy
8711efa30d0SSarangdhar Joshi 	 * this information to device memory. We also update the table_ptr so
8721efa30d0SSarangdhar Joshi 	 * that any subsequent changes will be applied to the loaded version.
8731efa30d0SSarangdhar Joshi 	 */
8741efa30d0SSarangdhar Joshi 	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
8751efa30d0SSarangdhar Joshi 	if (loaded_table) {
876a4b24c75SBjorn Andersson 		memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
8771efa30d0SSarangdhar Joshi 		rproc->table_ptr = loaded_table;
8781efa30d0SSarangdhar Joshi 	}
8791efa30d0SSarangdhar Joshi 
8801efa30d0SSarangdhar Joshi 	/* power up the remote processor */
8811efa30d0SSarangdhar Joshi 	ret = rproc->ops->start(rproc);
8821efa30d0SSarangdhar Joshi 	if (ret) {
8831efa30d0SSarangdhar Joshi 		dev_err(dev, "can't start rproc %s: %d\n", rproc->name, ret);
8841efa30d0SSarangdhar Joshi 		return ret;
8851efa30d0SSarangdhar Joshi 	}
8861efa30d0SSarangdhar Joshi 
8871efa30d0SSarangdhar Joshi 	/* probe any subdevices for the remote processor */
8881efa30d0SSarangdhar Joshi 	ret = rproc_probe_subdevices(rproc);
8891efa30d0SSarangdhar Joshi 	if (ret) {
8901efa30d0SSarangdhar Joshi 		dev_err(dev, "failed to probe subdevices for %s: %d\n",
8911efa30d0SSarangdhar Joshi 			rproc->name, ret);
8921efa30d0SSarangdhar Joshi 		rproc->ops->stop(rproc);
8931efa30d0SSarangdhar Joshi 		return ret;
8941efa30d0SSarangdhar Joshi 	}
8951efa30d0SSarangdhar Joshi 
8961efa30d0SSarangdhar Joshi 	rproc->state = RPROC_RUNNING;
8971efa30d0SSarangdhar Joshi 
8981efa30d0SSarangdhar Joshi 	dev_info(dev, "remote processor %s is now up\n", rproc->name);
8991efa30d0SSarangdhar Joshi 
9001efa30d0SSarangdhar Joshi 	return 0;
9011efa30d0SSarangdhar Joshi }
9021efa30d0SSarangdhar Joshi 
903400e64dfSOhad Ben-Cohen /*
904400e64dfSOhad Ben-Cohen  * take a firmware and boot a remote processor with it.
905400e64dfSOhad Ben-Cohen  */
906400e64dfSOhad Ben-Cohen static int rproc_fw_boot(struct rproc *rproc, const struct firmware *fw)
907400e64dfSOhad Ben-Cohen {
908b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
909400e64dfSOhad Ben-Cohen 	const char *name = rproc->firmware;
9101efa30d0SSarangdhar Joshi 	struct resource_table *table;
9111e3e2c7cSOhad Ben-Cohen 	int ret, tablesz;
912400e64dfSOhad Ben-Cohen 
913400e64dfSOhad Ben-Cohen 	ret = rproc_fw_sanity_check(rproc, fw);
914400e64dfSOhad Ben-Cohen 	if (ret)
915400e64dfSOhad Ben-Cohen 		return ret;
916400e64dfSOhad Ben-Cohen 
917e981f6d4SSjur Brændeland 	dev_info(dev, "Booting fw image %s, size %zd\n", name, fw->size);
918400e64dfSOhad Ben-Cohen 
919400e64dfSOhad Ben-Cohen 	/*
920400e64dfSOhad Ben-Cohen 	 * if enabling an IOMMU isn't relevant for this rproc, this is
921400e64dfSOhad Ben-Cohen 	 * just a nop
922400e64dfSOhad Ben-Cohen 	 */
923400e64dfSOhad Ben-Cohen 	ret = rproc_enable_iommu(rproc);
924400e64dfSOhad Ben-Cohen 	if (ret) {
925400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't enable iommu: %d\n", ret);
926400e64dfSOhad Ben-Cohen 		return ret;
927400e64dfSOhad Ben-Cohen 	}
928400e64dfSOhad Ben-Cohen 
9293e5f9eb5SSjur Brændeland 	rproc->bootaddr = rproc_get_boot_addr(rproc, fw);
93089970d28SWei Yongjun 	ret = -EINVAL;
931400e64dfSOhad Ben-Cohen 
9321e3e2c7cSOhad Ben-Cohen 	/* look for the resource table */
933bd484984SSjur Brændeland 	table = rproc_find_rsc_table(rproc, fw, &tablesz);
934a66a5114SStefan Agner 	if (!table) {
935a66a5114SStefan Agner 		dev_err(dev, "Failed to find resource table\n");
9361e3e2c7cSOhad Ben-Cohen 		goto clean_up;
937a66a5114SStefan Agner 	}
9381e3e2c7cSOhad Ben-Cohen 
939988d204cSBjorn Andersson 	/*
940988d204cSBjorn Andersson 	 * Create a copy of the resource table. When a virtio device starts
941988d204cSBjorn Andersson 	 * and calls vring_new_virtqueue() the address of the allocated vring
942a0c10687SBjorn Andersson 	 * will be stored in the cached_table. Before the device is started,
943a0c10687SBjorn Andersson 	 * cached_table will be copied into device memory.
944988d204cSBjorn Andersson 	 */
945a0c10687SBjorn Andersson 	rproc->cached_table = kmemdup(table, tablesz, GFP_KERNEL);
946a0c10687SBjorn Andersson 	if (!rproc->cached_table)
947a2b950acSOhad Ben-Cohen 		goto clean_up;
948988d204cSBjorn Andersson 
949a0c10687SBjorn Andersson 	rproc->table_ptr = rproc->cached_table;
950a4b24c75SBjorn Andersson 	rproc->table_sz = tablesz;
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);
970400e64dfSOhad Ben-Cohen clean_up:
971a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
972a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
973988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
974988d204cSBjorn Andersson 
975400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
976400e64dfSOhad Ben-Cohen 	return ret;
977400e64dfSOhad Ben-Cohen }
978400e64dfSOhad Ben-Cohen 
979400e64dfSOhad Ben-Cohen /*
9805e6533f7SSarangdhar Joshi  * take a firmware and boot it up.
981400e64dfSOhad Ben-Cohen  *
982400e64dfSOhad Ben-Cohen  * Note: this function is called asynchronously upon registration of the
983400e64dfSOhad Ben-Cohen  * remote processor (so we must wait until it completes before we try
984400e64dfSOhad Ben-Cohen  * to unregister the device. one other option is just to use kref here,
985400e64dfSOhad Ben-Cohen  * that might be cleaner).
986400e64dfSOhad Ben-Cohen  */
9875e6533f7SSarangdhar Joshi static void rproc_auto_boot_callback(const struct firmware *fw, void *context)
988400e64dfSOhad Ben-Cohen {
989400e64dfSOhad Ben-Cohen 	struct rproc *rproc = context;
990a2b950acSOhad Ben-Cohen 
9912bfc311aSBjorn Andersson 	rproc_boot(rproc);
992ddf71187SBjorn Andersson 
993400e64dfSOhad Ben-Cohen 	release_firmware(fw);
994400e64dfSOhad Ben-Cohen }
995400e64dfSOhad Ben-Cohen 
9965e6533f7SSarangdhar Joshi static int rproc_trigger_auto_boot(struct rproc *rproc)
99770b85ef8SFernando Guzman Lugo {
99870b85ef8SFernando Guzman Lugo 	int ret;
99970b85ef8SFernando Guzman Lugo 
100070b85ef8SFernando Guzman Lugo 	/*
100170b85ef8SFernando Guzman Lugo 	 * We're initiating an asynchronous firmware loading, so we can
100270b85ef8SFernando Guzman Lugo 	 * be built-in kernel code, without hanging the boot process.
100370b85ef8SFernando Guzman Lugo 	 */
100470b85ef8SFernando Guzman Lugo 	ret = request_firmware_nowait(THIS_MODULE, FW_ACTION_HOTPLUG,
100570b85ef8SFernando Guzman Lugo 				      rproc->firmware, &rproc->dev, GFP_KERNEL,
10065e6533f7SSarangdhar Joshi 				      rproc, rproc_auto_boot_callback);
10072099c77dSSarangdhar Joshi 	if (ret < 0)
100870b85ef8SFernando Guzman Lugo 		dev_err(&rproc->dev, "request_firmware_nowait err: %d\n", ret);
100970b85ef8SFernando Guzman Lugo 
101070b85ef8SFernando Guzman Lugo 	return ret;
101170b85ef8SFernando Guzman Lugo }
101270b85ef8SFernando Guzman Lugo 
10131efa30d0SSarangdhar Joshi static int rproc_stop(struct rproc *rproc)
10141efa30d0SSarangdhar Joshi {
10151efa30d0SSarangdhar Joshi 	struct device *dev = &rproc->dev;
10161efa30d0SSarangdhar Joshi 	int ret;
10171efa30d0SSarangdhar Joshi 
10181efa30d0SSarangdhar Joshi 	/* remove any subdevices for the remote processor */
10191efa30d0SSarangdhar Joshi 	rproc_remove_subdevices(rproc);
10201efa30d0SSarangdhar Joshi 
10211efa30d0SSarangdhar Joshi 	/* power off the remote processor */
10221efa30d0SSarangdhar Joshi 	ret = rproc->ops->stop(rproc);
10231efa30d0SSarangdhar Joshi 	if (ret) {
10241efa30d0SSarangdhar Joshi 		dev_err(dev, "can't stop rproc: %d\n", ret);
10251efa30d0SSarangdhar Joshi 		return ret;
10261efa30d0SSarangdhar Joshi 	}
10271efa30d0SSarangdhar Joshi 
10281efa30d0SSarangdhar Joshi 	rproc->state = RPROC_OFFLINE;
10291efa30d0SSarangdhar Joshi 
10301efa30d0SSarangdhar Joshi 	dev_info(dev, "stopped remote processor %s\n", rproc->name);
10311efa30d0SSarangdhar Joshi 
10321efa30d0SSarangdhar Joshi 	return 0;
10331efa30d0SSarangdhar Joshi }
10341efa30d0SSarangdhar Joshi 
103570b85ef8SFernando Guzman Lugo /**
103670b85ef8SFernando Guzman Lugo  * rproc_trigger_recovery() - recover a remoteproc
103770b85ef8SFernando Guzman Lugo  * @rproc: the remote processor
103870b85ef8SFernando Guzman Lugo  *
103956324d7aSAnna, Suman  * The recovery is done by resetting all the virtio devices, that way all the
104070b85ef8SFernando Guzman Lugo  * rpmsg drivers will be reseted along with the remote processor making the
104170b85ef8SFernando Guzman Lugo  * remoteproc functional again.
104270b85ef8SFernando Guzman Lugo  *
104370b85ef8SFernando Guzman Lugo  * This function can sleep, so it cannot be called from atomic context.
104470b85ef8SFernando Guzman Lugo  */
104570b85ef8SFernando Guzman Lugo int rproc_trigger_recovery(struct rproc *rproc)
104670b85ef8SFernando Guzman Lugo {
10477e83cab8SSarangdhar Joshi 	const struct firmware *firmware_p;
10487e83cab8SSarangdhar Joshi 	struct device *dev = &rproc->dev;
10497e83cab8SSarangdhar Joshi 	int ret;
10507e83cab8SSarangdhar Joshi 
10517e83cab8SSarangdhar Joshi 	dev_err(dev, "recovering %s\n", rproc->name);
105270b85ef8SFernando Guzman Lugo 
10537e83cab8SSarangdhar Joshi 	ret = mutex_lock_interruptible(&rproc->lock);
10547e83cab8SSarangdhar Joshi 	if (ret)
10557e83cab8SSarangdhar Joshi 		return ret;
10567e83cab8SSarangdhar Joshi 
10577e83cab8SSarangdhar Joshi 	ret = rproc_stop(rproc);
10587e83cab8SSarangdhar Joshi 	if (ret)
10597e83cab8SSarangdhar Joshi 		goto unlock_mutex;
1060ddf71187SBjorn Andersson 
10617e83cab8SSarangdhar Joshi 	/* load firmware */
10627e83cab8SSarangdhar Joshi 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
10637e83cab8SSarangdhar Joshi 	if (ret < 0) {
10647e83cab8SSarangdhar Joshi 		dev_err(dev, "request_firmware failed: %d\n", ret);
10657e83cab8SSarangdhar Joshi 		goto unlock_mutex;
10667e83cab8SSarangdhar Joshi 	}
1067ddf71187SBjorn Andersson 
10687e83cab8SSarangdhar Joshi 	/* boot the remote processor up again */
10697e83cab8SSarangdhar Joshi 	ret = rproc_start(rproc, firmware_p);
10707e83cab8SSarangdhar Joshi 
10717e83cab8SSarangdhar Joshi 	release_firmware(firmware_p);
10727e83cab8SSarangdhar Joshi 
10737e83cab8SSarangdhar Joshi unlock_mutex:
10747e83cab8SSarangdhar Joshi 	mutex_unlock(&rproc->lock);
10757e83cab8SSarangdhar Joshi 	return ret;
107670b85ef8SFernando Guzman Lugo }
107770b85ef8SFernando Guzman Lugo 
1078400e64dfSOhad Ben-Cohen /**
10798afd519cSFernando Guzman Lugo  * rproc_crash_handler_work() - handle a crash
10808afd519cSFernando Guzman Lugo  *
10818afd519cSFernando Guzman Lugo  * This function needs to handle everything related to a crash, like cpu
10828afd519cSFernando Guzman Lugo  * registers and stack dump, information to help to debug the fatal error, etc.
10838afd519cSFernando Guzman Lugo  */
10848afd519cSFernando Guzman Lugo static void rproc_crash_handler_work(struct work_struct *work)
10858afd519cSFernando Guzman Lugo {
10868afd519cSFernando Guzman Lugo 	struct rproc *rproc = container_of(work, struct rproc, crash_handler);
10878afd519cSFernando Guzman Lugo 	struct device *dev = &rproc->dev;
10888afd519cSFernando Guzman Lugo 
10898afd519cSFernando Guzman Lugo 	dev_dbg(dev, "enter %s\n", __func__);
10908afd519cSFernando Guzman Lugo 
10918afd519cSFernando Guzman Lugo 	mutex_lock(&rproc->lock);
10928afd519cSFernando Guzman Lugo 
10938afd519cSFernando Guzman Lugo 	if (rproc->state == RPROC_CRASHED || rproc->state == RPROC_OFFLINE) {
10948afd519cSFernando Guzman Lugo 		/* handle only the first crash detected */
10958afd519cSFernando Guzman Lugo 		mutex_unlock(&rproc->lock);
10968afd519cSFernando Guzman Lugo 		return;
10978afd519cSFernando Guzman Lugo 	}
10988afd519cSFernando Guzman Lugo 
10998afd519cSFernando Guzman Lugo 	rproc->state = RPROC_CRASHED;
11008afd519cSFernando Guzman Lugo 	dev_err(dev, "handling crash #%u in %s\n", ++rproc->crash_cnt,
11018afd519cSFernando Guzman Lugo 		rproc->name);
11028afd519cSFernando Guzman Lugo 
11038afd519cSFernando Guzman Lugo 	mutex_unlock(&rproc->lock);
11048afd519cSFernando Guzman Lugo 
11052e37abb8SFernando Guzman Lugo 	if (!rproc->recovery_disabled)
110670b85ef8SFernando Guzman Lugo 		rproc_trigger_recovery(rproc);
11078afd519cSFernando Guzman Lugo }
11088afd519cSFernando Guzman Lugo 
11098afd519cSFernando Guzman Lugo /**
11101b0ef906SSuman Anna  * rproc_boot() - boot a remote processor
1111400e64dfSOhad Ben-Cohen  * @rproc: handle of a remote processor
1112400e64dfSOhad Ben-Cohen  *
1113400e64dfSOhad Ben-Cohen  * Boot a remote processor (i.e. load its firmware, power it on, ...).
1114400e64dfSOhad Ben-Cohen  *
1115400e64dfSOhad Ben-Cohen  * If the remote processor is already powered on, this function immediately
1116400e64dfSOhad Ben-Cohen  * returns (successfully).
1117400e64dfSOhad Ben-Cohen  *
1118400e64dfSOhad Ben-Cohen  * Returns 0 on success, and an appropriate error value otherwise.
1119400e64dfSOhad Ben-Cohen  */
11201b0ef906SSuman Anna int rproc_boot(struct rproc *rproc)
1121400e64dfSOhad Ben-Cohen {
1122400e64dfSOhad Ben-Cohen 	const struct firmware *firmware_p;
1123400e64dfSOhad Ben-Cohen 	struct device *dev;
1124400e64dfSOhad Ben-Cohen 	int ret;
1125400e64dfSOhad Ben-Cohen 
1126400e64dfSOhad Ben-Cohen 	if (!rproc) {
1127400e64dfSOhad Ben-Cohen 		pr_err("invalid rproc handle\n");
1128400e64dfSOhad Ben-Cohen 		return -EINVAL;
1129400e64dfSOhad Ben-Cohen 	}
1130400e64dfSOhad Ben-Cohen 
1131b5ab5e24SOhad Ben-Cohen 	dev = &rproc->dev;
1132400e64dfSOhad Ben-Cohen 
1133400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1134400e64dfSOhad Ben-Cohen 	if (ret) {
1135400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1136400e64dfSOhad Ben-Cohen 		return ret;
1137400e64dfSOhad Ben-Cohen 	}
1138400e64dfSOhad Ben-Cohen 
11392099c77dSSarangdhar Joshi 	if (rproc->state == RPROC_DELETED) {
11402099c77dSSarangdhar Joshi 		ret = -ENODEV;
11412099c77dSSarangdhar Joshi 		dev_err(dev, "can't boot deleted rproc %s\n", rproc->name);
11422099c77dSSarangdhar Joshi 		goto unlock_mutex;
11432099c77dSSarangdhar Joshi 	}
11442099c77dSSarangdhar Joshi 
1145400e64dfSOhad Ben-Cohen 	/* skip the boot process if rproc is already powered up */
1146400e64dfSOhad Ben-Cohen 	if (atomic_inc_return(&rproc->power) > 1) {
1147400e64dfSOhad Ben-Cohen 		ret = 0;
1148400e64dfSOhad Ben-Cohen 		goto unlock_mutex;
1149400e64dfSOhad Ben-Cohen 	}
1150400e64dfSOhad Ben-Cohen 
1151400e64dfSOhad Ben-Cohen 	dev_info(dev, "powering up %s\n", rproc->name);
1152400e64dfSOhad Ben-Cohen 
1153400e64dfSOhad Ben-Cohen 	/* load firmware */
1154400e64dfSOhad Ben-Cohen 	ret = request_firmware(&firmware_p, rproc->firmware, dev);
1155400e64dfSOhad Ben-Cohen 	if (ret < 0) {
1156400e64dfSOhad Ben-Cohen 		dev_err(dev, "request_firmware failed: %d\n", ret);
1157400e64dfSOhad Ben-Cohen 		goto downref_rproc;
1158400e64dfSOhad Ben-Cohen 	}
1159400e64dfSOhad Ben-Cohen 
1160400e64dfSOhad Ben-Cohen 	ret = rproc_fw_boot(rproc, firmware_p);
1161400e64dfSOhad Ben-Cohen 
1162400e64dfSOhad Ben-Cohen 	release_firmware(firmware_p);
1163400e64dfSOhad Ben-Cohen 
1164400e64dfSOhad Ben-Cohen downref_rproc:
1165fbb6aacbSBjorn Andersson 	if (ret)
1166400e64dfSOhad Ben-Cohen 		atomic_dec(&rproc->power);
1167400e64dfSOhad Ben-Cohen unlock_mutex:
1168400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1169400e64dfSOhad Ben-Cohen 	return ret;
1170400e64dfSOhad Ben-Cohen }
1171400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_boot);
1172400e64dfSOhad Ben-Cohen 
1173400e64dfSOhad Ben-Cohen /**
1174400e64dfSOhad Ben-Cohen  * rproc_shutdown() - power off the remote processor
1175400e64dfSOhad Ben-Cohen  * @rproc: the remote processor
1176400e64dfSOhad Ben-Cohen  *
1177400e64dfSOhad Ben-Cohen  * Power off a remote processor (previously booted with rproc_boot()).
1178400e64dfSOhad Ben-Cohen  *
1179400e64dfSOhad Ben-Cohen  * In case @rproc is still being used by an additional user(s), then
1180400e64dfSOhad Ben-Cohen  * this function will just decrement the power refcount and exit,
1181400e64dfSOhad Ben-Cohen  * without really powering off the device.
1182400e64dfSOhad Ben-Cohen  *
1183400e64dfSOhad Ben-Cohen  * Every call to rproc_boot() must (eventually) be accompanied by a call
1184400e64dfSOhad Ben-Cohen  * to rproc_shutdown(). Calling rproc_shutdown() redundantly is a bug.
1185400e64dfSOhad Ben-Cohen  *
1186400e64dfSOhad Ben-Cohen  * Notes:
1187400e64dfSOhad Ben-Cohen  * - we're not decrementing the rproc's refcount, only the power refcount.
1188400e64dfSOhad Ben-Cohen  *   which means that the @rproc handle stays valid even after rproc_shutdown()
1189400e64dfSOhad Ben-Cohen  *   returns, and users can still use it with a subsequent rproc_boot(), if
1190400e64dfSOhad Ben-Cohen  *   needed.
1191400e64dfSOhad Ben-Cohen  */
1192400e64dfSOhad Ben-Cohen void rproc_shutdown(struct rproc *rproc)
1193400e64dfSOhad Ben-Cohen {
1194b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
1195400e64dfSOhad Ben-Cohen 	int ret;
1196400e64dfSOhad Ben-Cohen 
1197400e64dfSOhad Ben-Cohen 	ret = mutex_lock_interruptible(&rproc->lock);
1198400e64dfSOhad Ben-Cohen 	if (ret) {
1199400e64dfSOhad Ben-Cohen 		dev_err(dev, "can't lock rproc %s: %d\n", rproc->name, ret);
1200400e64dfSOhad Ben-Cohen 		return;
1201400e64dfSOhad Ben-Cohen 	}
1202400e64dfSOhad Ben-Cohen 
1203400e64dfSOhad Ben-Cohen 	/* if the remote proc is still needed, bail out */
1204400e64dfSOhad Ben-Cohen 	if (!atomic_dec_and_test(&rproc->power))
1205400e64dfSOhad Ben-Cohen 		goto out;
1206400e64dfSOhad Ben-Cohen 
12071efa30d0SSarangdhar Joshi 	ret = rproc_stop(rproc);
1208400e64dfSOhad Ben-Cohen 	if (ret) {
1209400e64dfSOhad Ben-Cohen 		atomic_inc(&rproc->power);
1210400e64dfSOhad Ben-Cohen 		goto out;
1211400e64dfSOhad Ben-Cohen 	}
1212400e64dfSOhad Ben-Cohen 
1213400e64dfSOhad Ben-Cohen 	/* clean up all acquired resources */
1214400e64dfSOhad Ben-Cohen 	rproc_resource_cleanup(rproc);
1215400e64dfSOhad Ben-Cohen 
1216400e64dfSOhad Ben-Cohen 	rproc_disable_iommu(rproc);
1217400e64dfSOhad Ben-Cohen 
1218988d204cSBjorn Andersson 	/* Free the copy of the resource table */
1219a0c10687SBjorn Andersson 	kfree(rproc->cached_table);
1220a0c10687SBjorn Andersson 	rproc->cached_table = NULL;
1221988d204cSBjorn Andersson 	rproc->table_ptr = NULL;
1222400e64dfSOhad Ben-Cohen out:
1223400e64dfSOhad Ben-Cohen 	mutex_unlock(&rproc->lock);
1224400e64dfSOhad Ben-Cohen }
1225400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_shutdown);
1226400e64dfSOhad Ben-Cohen 
1227400e64dfSOhad Ben-Cohen /**
1228fec47d86SDave Gerlach  * rproc_get_by_phandle() - find a remote processor by phandle
1229fec47d86SDave Gerlach  * @phandle: phandle to the rproc
1230fec47d86SDave Gerlach  *
1231fec47d86SDave Gerlach  * Finds an rproc handle using the remote processor's phandle, and then
1232fec47d86SDave Gerlach  * return a handle to the rproc.
1233fec47d86SDave Gerlach  *
1234fec47d86SDave Gerlach  * This function increments the remote processor's refcount, so always
1235fec47d86SDave Gerlach  * use rproc_put() to decrement it back once rproc isn't needed anymore.
1236fec47d86SDave Gerlach  *
1237fec47d86SDave Gerlach  * Returns the rproc handle on success, and NULL on failure.
1238fec47d86SDave Gerlach  */
12398de3dbd0SOhad Ben-Cohen #ifdef CONFIG_OF
1240fec47d86SDave Gerlach struct rproc *rproc_get_by_phandle(phandle phandle)
1241fec47d86SDave Gerlach {
1242fec47d86SDave Gerlach 	struct rproc *rproc = NULL, *r;
1243fec47d86SDave Gerlach 	struct device_node *np;
1244fec47d86SDave Gerlach 
1245fec47d86SDave Gerlach 	np = of_find_node_by_phandle(phandle);
1246fec47d86SDave Gerlach 	if (!np)
1247fec47d86SDave Gerlach 		return NULL;
1248fec47d86SDave Gerlach 
1249fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1250fec47d86SDave Gerlach 	list_for_each_entry(r, &rproc_list, node) {
1251fec47d86SDave Gerlach 		if (r->dev.parent && r->dev.parent->of_node == np) {
1252fbb6aacbSBjorn Andersson 			/* prevent underlying implementation from being removed */
1253fbb6aacbSBjorn Andersson 			if (!try_module_get(r->dev.parent->driver->owner)) {
1254fbb6aacbSBjorn Andersson 				dev_err(&r->dev, "can't get owner\n");
1255fbb6aacbSBjorn Andersson 				break;
1256fbb6aacbSBjorn Andersson 			}
1257fbb6aacbSBjorn Andersson 
1258fec47d86SDave Gerlach 			rproc = r;
1259fec47d86SDave Gerlach 			get_device(&rproc->dev);
1260fec47d86SDave Gerlach 			break;
1261fec47d86SDave Gerlach 		}
1262fec47d86SDave Gerlach 	}
1263fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1264fec47d86SDave Gerlach 
1265fec47d86SDave Gerlach 	of_node_put(np);
1266fec47d86SDave Gerlach 
1267fec47d86SDave Gerlach 	return rproc;
1268fec47d86SDave Gerlach }
12698de3dbd0SOhad Ben-Cohen #else
12708de3dbd0SOhad Ben-Cohen struct rproc *rproc_get_by_phandle(phandle phandle)
12718de3dbd0SOhad Ben-Cohen {
12728de3dbd0SOhad Ben-Cohen 	return NULL;
12738de3dbd0SOhad Ben-Cohen }
12748de3dbd0SOhad Ben-Cohen #endif
1275fec47d86SDave Gerlach EXPORT_SYMBOL(rproc_get_by_phandle);
1276fec47d86SDave Gerlach 
1277fec47d86SDave Gerlach /**
1278160e7c84SOhad Ben-Cohen  * rproc_add() - register a remote processor
1279400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle to register
1280400e64dfSOhad Ben-Cohen  *
1281400e64dfSOhad Ben-Cohen  * Registers @rproc with the remoteproc framework, after it has been
1282400e64dfSOhad Ben-Cohen  * allocated with rproc_alloc().
1283400e64dfSOhad Ben-Cohen  *
1284400e64dfSOhad Ben-Cohen  * This is called by the platform-specific rproc implementation, whenever
1285400e64dfSOhad Ben-Cohen  * a new remote processor device is probed.
1286400e64dfSOhad Ben-Cohen  *
1287400e64dfSOhad Ben-Cohen  * Returns 0 on success and an appropriate error code otherwise.
1288400e64dfSOhad Ben-Cohen  *
1289400e64dfSOhad Ben-Cohen  * Note: this function initiates an asynchronous firmware loading
1290400e64dfSOhad Ben-Cohen  * context, which will look for virtio devices supported by the rproc's
1291400e64dfSOhad Ben-Cohen  * firmware.
1292400e64dfSOhad Ben-Cohen  *
1293400e64dfSOhad Ben-Cohen  * If found, those virtio devices will be created and added, so as a result
12947a186941SOhad Ben-Cohen  * of registering this remote processor, additional virtio drivers might be
1295400e64dfSOhad Ben-Cohen  * probed.
1296400e64dfSOhad Ben-Cohen  */
1297160e7c84SOhad Ben-Cohen int rproc_add(struct rproc *rproc)
1298400e64dfSOhad Ben-Cohen {
1299b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
130070b85ef8SFernando Guzman Lugo 	int ret;
1301400e64dfSOhad Ben-Cohen 
1302b5ab5e24SOhad Ben-Cohen 	ret = device_add(dev);
1303b5ab5e24SOhad Ben-Cohen 	if (ret < 0)
1304b5ab5e24SOhad Ben-Cohen 		return ret;
1305400e64dfSOhad Ben-Cohen 
1306b5ab5e24SOhad Ben-Cohen 	dev_info(dev, "%s is available\n", rproc->name);
1307400e64dfSOhad Ben-Cohen 
1308400e64dfSOhad Ben-Cohen 	/* create debugfs entries */
1309400e64dfSOhad Ben-Cohen 	rproc_create_debug_dir(rproc);
13107a20c64dSSarangdhar Joshi 
13117a20c64dSSarangdhar Joshi 	/* if rproc is marked always-on, request it to boot */
13127a20c64dSSarangdhar Joshi 	if (rproc->auto_boot) {
13135e6533f7SSarangdhar Joshi 		ret = rproc_trigger_auto_boot(rproc);
1314d2e12e66SDave Gerlach 		if (ret < 0)
1315d2e12e66SDave Gerlach 			return ret;
13167a20c64dSSarangdhar Joshi 	}
1317400e64dfSOhad Ben-Cohen 
1318d2e12e66SDave Gerlach 	/* expose to rproc_get_by_phandle users */
1319d2e12e66SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1320d2e12e66SDave Gerlach 	list_add(&rproc->node, &rproc_list);
1321d2e12e66SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1322d2e12e66SDave Gerlach 
1323d2e12e66SDave Gerlach 	return 0;
1324400e64dfSOhad Ben-Cohen }
1325160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_add);
1326400e64dfSOhad Ben-Cohen 
1327400e64dfSOhad Ben-Cohen /**
1328b5ab5e24SOhad Ben-Cohen  * rproc_type_release() - release a remote processor instance
1329b5ab5e24SOhad Ben-Cohen  * @dev: the rproc's device
1330b5ab5e24SOhad Ben-Cohen  *
1331b5ab5e24SOhad Ben-Cohen  * This function should _never_ be called directly.
1332b5ab5e24SOhad Ben-Cohen  *
1333b5ab5e24SOhad Ben-Cohen  * It will be called by the driver core when no one holds a valid pointer
1334b5ab5e24SOhad Ben-Cohen  * to @dev anymore.
1335b5ab5e24SOhad Ben-Cohen  */
1336b5ab5e24SOhad Ben-Cohen static void rproc_type_release(struct device *dev)
1337b5ab5e24SOhad Ben-Cohen {
1338b5ab5e24SOhad Ben-Cohen 	struct rproc *rproc = container_of(dev, struct rproc, dev);
1339b5ab5e24SOhad Ben-Cohen 
13407183a2a7SOhad Ben-Cohen 	dev_info(&rproc->dev, "releasing %s\n", rproc->name);
13417183a2a7SOhad Ben-Cohen 
1342b5ab5e24SOhad Ben-Cohen 	idr_destroy(&rproc->notifyids);
1343b5ab5e24SOhad Ben-Cohen 
1344b5ab5e24SOhad Ben-Cohen 	if (rproc->index >= 0)
1345b5ab5e24SOhad Ben-Cohen 		ida_simple_remove(&rproc_dev_index, rproc->index);
1346b5ab5e24SOhad Ben-Cohen 
13470f57dc6aSMatt Redfearn 	kfree(rproc->firmware);
1348fb98e2bdSBjorn Andersson 	kfree(rproc->ops);
1349b5ab5e24SOhad Ben-Cohen 	kfree(rproc);
1350b5ab5e24SOhad Ben-Cohen }
1351b5ab5e24SOhad Ben-Cohen 
1352c42ca04dSBhumika Goyal static const struct device_type rproc_type = {
1353b5ab5e24SOhad Ben-Cohen 	.name		= "remoteproc",
1354b5ab5e24SOhad Ben-Cohen 	.release	= rproc_type_release,
1355b5ab5e24SOhad Ben-Cohen };
1356400e64dfSOhad Ben-Cohen 
1357400e64dfSOhad Ben-Cohen /**
1358400e64dfSOhad Ben-Cohen  * rproc_alloc() - allocate a remote processor handle
1359400e64dfSOhad Ben-Cohen  * @dev: the underlying device
1360400e64dfSOhad Ben-Cohen  * @name: name of this remote processor
1361400e64dfSOhad Ben-Cohen  * @ops: platform-specific handlers (mainly start/stop)
13628b4aec9aSRobert Tivy  * @firmware: name of firmware file to load, can be NULL
1363400e64dfSOhad Ben-Cohen  * @len: length of private data needed by the rproc driver (in bytes)
1364400e64dfSOhad Ben-Cohen  *
1365400e64dfSOhad Ben-Cohen  * Allocates a new remote processor handle, but does not register
13668b4aec9aSRobert Tivy  * it yet. if @firmware is NULL, a default name is used.
1367400e64dfSOhad Ben-Cohen  *
1368400e64dfSOhad Ben-Cohen  * This function should be used by rproc implementations during initialization
1369400e64dfSOhad Ben-Cohen  * of the remote processor.
1370400e64dfSOhad Ben-Cohen  *
1371400e64dfSOhad Ben-Cohen  * After creating an rproc handle using this function, and when ready,
1372160e7c84SOhad Ben-Cohen  * implementations should then call rproc_add() to complete
1373400e64dfSOhad Ben-Cohen  * the registration of the remote processor.
1374400e64dfSOhad Ben-Cohen  *
1375400e64dfSOhad Ben-Cohen  * On success the new rproc is returned, and on failure, NULL.
1376400e64dfSOhad Ben-Cohen  *
1377400e64dfSOhad Ben-Cohen  * Note: _never_ directly deallocate @rproc, even if it was not registered
1378433c0e04SBjorn Andersson  * yet. Instead, when you need to unroll rproc_alloc(), use rproc_free().
1379400e64dfSOhad Ben-Cohen  */
1380400e64dfSOhad Ben-Cohen struct rproc *rproc_alloc(struct device *dev, const char *name,
1381400e64dfSOhad Ben-Cohen 			  const struct rproc_ops *ops,
1382400e64dfSOhad Ben-Cohen 			  const char *firmware, int len)
1383400e64dfSOhad Ben-Cohen {
1384400e64dfSOhad Ben-Cohen 	struct rproc *rproc;
13858b4aec9aSRobert Tivy 	char *p, *template = "rproc-%s-fw";
13860f57dc6aSMatt Redfearn 	int name_len;
1387400e64dfSOhad Ben-Cohen 
1388400e64dfSOhad Ben-Cohen 	if (!dev || !name || !ops)
1389400e64dfSOhad Ben-Cohen 		return NULL;
1390400e64dfSOhad Ben-Cohen 
13910f57dc6aSMatt Redfearn 	if (!firmware) {
13928b4aec9aSRobert Tivy 		/*
13938b4aec9aSRobert Tivy 		 * If the caller didn't pass in a firmware name then
13940f57dc6aSMatt Redfearn 		 * construct a default name.
13958b4aec9aSRobert Tivy 		 */
13968b4aec9aSRobert Tivy 		name_len = strlen(name) + strlen(template) - 2 + 1;
13970f57dc6aSMatt Redfearn 		p = kmalloc(name_len, GFP_KERNEL);
13980f57dc6aSMatt Redfearn 		if (!p)
1399400e64dfSOhad Ben-Cohen 			return NULL;
14008b4aec9aSRobert Tivy 		snprintf(p, name_len, template, name);
14018b4aec9aSRobert Tivy 	} else {
14020f57dc6aSMatt Redfearn 		p = kstrdup(firmware, GFP_KERNEL);
14030f57dc6aSMatt Redfearn 		if (!p)
14040f57dc6aSMatt Redfearn 			return NULL;
14050f57dc6aSMatt Redfearn 	}
14060f57dc6aSMatt Redfearn 
14070f57dc6aSMatt Redfearn 	rproc = kzalloc(sizeof(struct rproc) + len, GFP_KERNEL);
14080f57dc6aSMatt Redfearn 	if (!rproc) {
14090f57dc6aSMatt Redfearn 		kfree(p);
14100f57dc6aSMatt Redfearn 		return NULL;
14118b4aec9aSRobert Tivy 	}
14128b4aec9aSRobert Tivy 
1413fb98e2bdSBjorn Andersson 	rproc->ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL);
1414fb98e2bdSBjorn Andersson 	if (!rproc->ops) {
1415fb98e2bdSBjorn Andersson 		kfree(p);
1416fb98e2bdSBjorn Andersson 		kfree(rproc);
1417fb98e2bdSBjorn Andersson 		return NULL;
1418fb98e2bdSBjorn Andersson 	}
1419fb98e2bdSBjorn Andersson 
14208b4aec9aSRobert Tivy 	rproc->firmware = p;
1421400e64dfSOhad Ben-Cohen 	rproc->name = name;
1422400e64dfSOhad Ben-Cohen 	rproc->priv = &rproc[1];
1423ddf71187SBjorn Andersson 	rproc->auto_boot = true;
1424400e64dfSOhad Ben-Cohen 
1425b5ab5e24SOhad Ben-Cohen 	device_initialize(&rproc->dev);
1426b5ab5e24SOhad Ben-Cohen 	rproc->dev.parent = dev;
1427b5ab5e24SOhad Ben-Cohen 	rproc->dev.type = &rproc_type;
14282aefbef0SMatt Redfearn 	rproc->dev.class = &rproc_class;
14297c89717fSBjorn Andersson 	rproc->dev.driver_data = rproc;
1430b5ab5e24SOhad Ben-Cohen 
1431b5ab5e24SOhad Ben-Cohen 	/* Assign a unique device index and name */
1432b5ab5e24SOhad Ben-Cohen 	rproc->index = ida_simple_get(&rproc_dev_index, 0, 0, GFP_KERNEL);
1433b5ab5e24SOhad Ben-Cohen 	if (rproc->index < 0) {
1434b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "ida_simple_get failed: %d\n", rproc->index);
1435b5ab5e24SOhad Ben-Cohen 		put_device(&rproc->dev);
1436b5ab5e24SOhad Ben-Cohen 		return NULL;
1437b5ab5e24SOhad Ben-Cohen 	}
1438b5ab5e24SOhad Ben-Cohen 
1439b5ab5e24SOhad Ben-Cohen 	dev_set_name(&rproc->dev, "remoteproc%d", rproc->index);
1440b5ab5e24SOhad Ben-Cohen 
1441400e64dfSOhad Ben-Cohen 	atomic_set(&rproc->power, 0);
1442400e64dfSOhad Ben-Cohen 
14430f21f9ccSBjorn Andersson 	/* Default to ELF loader if no load function is specified */
14440f21f9ccSBjorn Andersson 	if (!rproc->ops->load) {
14450f21f9ccSBjorn Andersson 		rproc->ops->load = rproc_elf_load_segments;
14460f21f9ccSBjorn Andersson 		rproc->ops->find_rsc_table = rproc_elf_find_rsc_table;
14470f21f9ccSBjorn Andersson 		rproc->ops->find_loaded_rsc_table = rproc_elf_find_loaded_rsc_table;
14480f21f9ccSBjorn Andersson 		rproc->ops->sanity_check = rproc_elf_sanity_check;
14490f21f9ccSBjorn Andersson 		rproc->ops->get_boot_addr = rproc_elf_get_boot_addr;
14500f21f9ccSBjorn Andersson 	}
1451400e64dfSOhad Ben-Cohen 
1452400e64dfSOhad Ben-Cohen 	mutex_init(&rproc->lock);
1453400e64dfSOhad Ben-Cohen 
14547a186941SOhad Ben-Cohen 	idr_init(&rproc->notifyids);
14557a186941SOhad Ben-Cohen 
1456400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->carveouts);
1457400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->mappings);
1458400e64dfSOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->traces);
14597a186941SOhad Ben-Cohen 	INIT_LIST_HEAD(&rproc->rvdevs);
14607bdc9650SBjorn Andersson 	INIT_LIST_HEAD(&rproc->subdevs);
1461400e64dfSOhad Ben-Cohen 
14628afd519cSFernando Guzman Lugo 	INIT_WORK(&rproc->crash_handler, rproc_crash_handler_work);
14638afd519cSFernando Guzman Lugo 
1464400e64dfSOhad Ben-Cohen 	rproc->state = RPROC_OFFLINE;
1465400e64dfSOhad Ben-Cohen 
1466400e64dfSOhad Ben-Cohen 	return rproc;
1467400e64dfSOhad Ben-Cohen }
1468400e64dfSOhad Ben-Cohen EXPORT_SYMBOL(rproc_alloc);
1469400e64dfSOhad Ben-Cohen 
1470400e64dfSOhad Ben-Cohen /**
1471433c0e04SBjorn Andersson  * rproc_free() - unroll rproc_alloc()
1472433c0e04SBjorn Andersson  * @rproc: the remote processor handle
1473433c0e04SBjorn Andersson  *
1474433c0e04SBjorn Andersson  * This function decrements the rproc dev refcount.
1475433c0e04SBjorn Andersson  *
1476433c0e04SBjorn Andersson  * If no one holds any reference to rproc anymore, then its refcount would
1477433c0e04SBjorn Andersson  * now drop to zero, and it would be freed.
1478433c0e04SBjorn Andersson  */
1479433c0e04SBjorn Andersson void rproc_free(struct rproc *rproc)
1480433c0e04SBjorn Andersson {
1481433c0e04SBjorn Andersson 	put_device(&rproc->dev);
1482433c0e04SBjorn Andersson }
1483433c0e04SBjorn Andersson EXPORT_SYMBOL(rproc_free);
1484433c0e04SBjorn Andersson 
1485433c0e04SBjorn Andersson /**
1486433c0e04SBjorn Andersson  * rproc_put() - release rproc reference
1487400e64dfSOhad Ben-Cohen  * @rproc: the remote processor handle
1488400e64dfSOhad Ben-Cohen  *
1489c6b5a276SOhad Ben-Cohen  * This function decrements the rproc dev refcount.
1490400e64dfSOhad Ben-Cohen  *
1491c6b5a276SOhad Ben-Cohen  * If no one holds any reference to rproc anymore, then its refcount would
1492c6b5a276SOhad Ben-Cohen  * now drop to zero, and it would be freed.
1493400e64dfSOhad Ben-Cohen  */
1494160e7c84SOhad Ben-Cohen void rproc_put(struct rproc *rproc)
1495400e64dfSOhad Ben-Cohen {
1496fbb6aacbSBjorn Andersson 	module_put(rproc->dev.parent->driver->owner);
1497b5ab5e24SOhad Ben-Cohen 	put_device(&rproc->dev);
1498400e64dfSOhad Ben-Cohen }
1499160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_put);
1500400e64dfSOhad Ben-Cohen 
1501400e64dfSOhad Ben-Cohen /**
1502160e7c84SOhad Ben-Cohen  * rproc_del() - unregister a remote processor
1503400e64dfSOhad Ben-Cohen  * @rproc: rproc handle to unregister
1504400e64dfSOhad Ben-Cohen  *
1505400e64dfSOhad Ben-Cohen  * This function should be called when the platform specific rproc
1506400e64dfSOhad Ben-Cohen  * implementation decides to remove the rproc device. it should
1507160e7c84SOhad Ben-Cohen  * _only_ be called if a previous invocation of rproc_add()
1508400e64dfSOhad Ben-Cohen  * has completed successfully.
1509400e64dfSOhad Ben-Cohen  *
1510160e7c84SOhad Ben-Cohen  * After rproc_del() returns, @rproc isn't freed yet, because
1511c6b5a276SOhad Ben-Cohen  * of the outstanding reference created by rproc_alloc. To decrement that
1512433c0e04SBjorn Andersson  * one last refcount, one still needs to call rproc_free().
1513400e64dfSOhad Ben-Cohen  *
1514400e64dfSOhad Ben-Cohen  * Returns 0 on success and -EINVAL if @rproc isn't valid.
1515400e64dfSOhad Ben-Cohen  */
1516160e7c84SOhad Ben-Cohen int rproc_del(struct rproc *rproc)
1517400e64dfSOhad Ben-Cohen {
1518400e64dfSOhad Ben-Cohen 	if (!rproc)
1519400e64dfSOhad Ben-Cohen 		return -EINVAL;
1520400e64dfSOhad Ben-Cohen 
1521ddf71187SBjorn Andersson 	/* if rproc is marked always-on, rproc_add() booted it */
1522ddf71187SBjorn Andersson 	/* TODO: make sure this works with rproc->power > 1 */
1523ddf71187SBjorn Andersson 	if (rproc->auto_boot)
1524ddf71187SBjorn Andersson 		rproc_shutdown(rproc);
1525ddf71187SBjorn Andersson 
15262099c77dSSarangdhar Joshi 	mutex_lock(&rproc->lock);
15272099c77dSSarangdhar Joshi 	rproc->state = RPROC_DELETED;
15282099c77dSSarangdhar Joshi 	mutex_unlock(&rproc->lock);
15292099c77dSSarangdhar Joshi 
1530b003d45bSSarangdhar Joshi 	rproc_delete_debug_dir(rproc);
1531b003d45bSSarangdhar Joshi 
1532fec47d86SDave Gerlach 	/* the rproc is downref'ed as soon as it's removed from the klist */
1533fec47d86SDave Gerlach 	mutex_lock(&rproc_list_mutex);
1534fec47d86SDave Gerlach 	list_del(&rproc->node);
1535fec47d86SDave Gerlach 	mutex_unlock(&rproc_list_mutex);
1536fec47d86SDave Gerlach 
1537b5ab5e24SOhad Ben-Cohen 	device_del(&rproc->dev);
1538400e64dfSOhad Ben-Cohen 
1539400e64dfSOhad Ben-Cohen 	return 0;
1540400e64dfSOhad Ben-Cohen }
1541160e7c84SOhad Ben-Cohen EXPORT_SYMBOL(rproc_del);
1542400e64dfSOhad Ben-Cohen 
15438afd519cSFernando Guzman Lugo /**
15447bdc9650SBjorn Andersson  * rproc_add_subdev() - add a subdevice to a remoteproc
15457bdc9650SBjorn Andersson  * @rproc: rproc handle to add the subdevice to
15467bdc9650SBjorn Andersson  * @subdev: subdev handle to register
15477bdc9650SBjorn Andersson  * @probe: function to call when the rproc boots
15487bdc9650SBjorn Andersson  * @remove: function to call when the rproc shuts down
15497bdc9650SBjorn Andersson  */
15507bdc9650SBjorn Andersson void rproc_add_subdev(struct rproc *rproc,
15517bdc9650SBjorn Andersson 		      struct rproc_subdev *subdev,
15527bdc9650SBjorn Andersson 		      int (*probe)(struct rproc_subdev *subdev),
15537bdc9650SBjorn Andersson 		      void (*remove)(struct rproc_subdev *subdev))
15547bdc9650SBjorn Andersson {
15557bdc9650SBjorn Andersson 	subdev->probe = probe;
15567bdc9650SBjorn Andersson 	subdev->remove = remove;
15577bdc9650SBjorn Andersson 
15587bdc9650SBjorn Andersson 	list_add_tail(&subdev->node, &rproc->subdevs);
15597bdc9650SBjorn Andersson }
15607bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_add_subdev);
15617bdc9650SBjorn Andersson 
15627bdc9650SBjorn Andersson /**
15637bdc9650SBjorn Andersson  * rproc_remove_subdev() - remove a subdevice from a remoteproc
15647bdc9650SBjorn Andersson  * @rproc: rproc handle to remove the subdevice from
15657bdc9650SBjorn Andersson  * @subdev: subdev handle, previously registered with rproc_add_subdev()
15667bdc9650SBjorn Andersson  */
15677bdc9650SBjorn Andersson void rproc_remove_subdev(struct rproc *rproc, struct rproc_subdev *subdev)
15687bdc9650SBjorn Andersson {
15697bdc9650SBjorn Andersson 	list_del(&subdev->node);
15707bdc9650SBjorn Andersson }
15717bdc9650SBjorn Andersson EXPORT_SYMBOL(rproc_remove_subdev);
15727bdc9650SBjorn Andersson 
15737bdc9650SBjorn Andersson /**
15747c89717fSBjorn Andersson  * rproc_get_by_child() - acquire rproc handle of @dev's ancestor
15757c89717fSBjorn Andersson  * @dev:	child device to find ancestor of
15767c89717fSBjorn Andersson  *
15777c89717fSBjorn Andersson  * Returns the ancestor rproc instance, or NULL if not found.
15787c89717fSBjorn Andersson  */
15797c89717fSBjorn Andersson struct rproc *rproc_get_by_child(struct device *dev)
15807c89717fSBjorn Andersson {
15817c89717fSBjorn Andersson 	for (dev = dev->parent; dev; dev = dev->parent) {
15827c89717fSBjorn Andersson 		if (dev->type == &rproc_type)
15837c89717fSBjorn Andersson 			return dev->driver_data;
15847c89717fSBjorn Andersson 	}
15857c89717fSBjorn Andersson 
15867c89717fSBjorn Andersson 	return NULL;
15877c89717fSBjorn Andersson }
15887c89717fSBjorn Andersson EXPORT_SYMBOL(rproc_get_by_child);
15897c89717fSBjorn Andersson 
15907c89717fSBjorn Andersson /**
15918afd519cSFernando Guzman Lugo  * rproc_report_crash() - rproc crash reporter function
15928afd519cSFernando Guzman Lugo  * @rproc: remote processor
15938afd519cSFernando Guzman Lugo  * @type: crash type
15948afd519cSFernando Guzman Lugo  *
15958afd519cSFernando Guzman Lugo  * This function must be called every time a crash is detected by the low-level
15968afd519cSFernando Guzman Lugo  * drivers implementing a specific remoteproc. This should not be called from a
15978afd519cSFernando Guzman Lugo  * non-remoteproc driver.
15988afd519cSFernando Guzman Lugo  *
15998afd519cSFernando Guzman Lugo  * This function can be called from atomic/interrupt context.
16008afd519cSFernando Guzman Lugo  */
16018afd519cSFernando Guzman Lugo void rproc_report_crash(struct rproc *rproc, enum rproc_crash_type type)
16028afd519cSFernando Guzman Lugo {
16038afd519cSFernando Guzman Lugo 	if (!rproc) {
16048afd519cSFernando Guzman Lugo 		pr_err("NULL rproc pointer\n");
16058afd519cSFernando Guzman Lugo 		return;
16068afd519cSFernando Guzman Lugo 	}
16078afd519cSFernando Guzman Lugo 
16088afd519cSFernando Guzman Lugo 	dev_err(&rproc->dev, "crash detected in %s: type %s\n",
16098afd519cSFernando Guzman Lugo 		rproc->name, rproc_crash_to_string(type));
16108afd519cSFernando Guzman Lugo 
16118afd519cSFernando Guzman Lugo 	/* create a new task to handle the error */
16128afd519cSFernando Guzman Lugo 	schedule_work(&rproc->crash_handler);
16138afd519cSFernando Guzman Lugo }
16148afd519cSFernando Guzman Lugo EXPORT_SYMBOL(rproc_report_crash);
16158afd519cSFernando Guzman Lugo 
1616400e64dfSOhad Ben-Cohen static int __init remoteproc_init(void)
1617400e64dfSOhad Ben-Cohen {
16182aefbef0SMatt Redfearn 	rproc_init_sysfs();
1619400e64dfSOhad Ben-Cohen 	rproc_init_debugfs();
1620b5ab5e24SOhad Ben-Cohen 
1621400e64dfSOhad Ben-Cohen 	return 0;
1622400e64dfSOhad Ben-Cohen }
1623400e64dfSOhad Ben-Cohen module_init(remoteproc_init);
1624400e64dfSOhad Ben-Cohen 
1625400e64dfSOhad Ben-Cohen static void __exit remoteproc_exit(void)
1626400e64dfSOhad Ben-Cohen {
1627f42f79afSSuman Anna 	ida_destroy(&rproc_dev_index);
1628f42f79afSSuman Anna 
1629400e64dfSOhad Ben-Cohen 	rproc_exit_debugfs();
16302aefbef0SMatt Redfearn 	rproc_exit_sysfs();
1631400e64dfSOhad Ben-Cohen }
1632400e64dfSOhad Ben-Cohen module_exit(remoteproc_exit);
1633400e64dfSOhad Ben-Cohen 
1634400e64dfSOhad Ben-Cohen MODULE_LICENSE("GPL v2");
1635400e64dfSOhad Ben-Cohen MODULE_DESCRIPTION("Generic Remote Processor Framework");
1636