xref: /linux/drivers/remoteproc/remoteproc_virtio.c (revision da802961832f9852886304290135457519815497)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
204a9016eSOhad Ben-Cohen /*
304a9016eSOhad Ben-Cohen  * Remote processor messaging transport (OMAP platform-specific bits)
404a9016eSOhad Ben-Cohen  *
504a9016eSOhad Ben-Cohen  * Copyright (C) 2011 Texas Instruments, Inc.
604a9016eSOhad Ben-Cohen  * Copyright (C) 2011 Google, Inc.
704a9016eSOhad Ben-Cohen  *
804a9016eSOhad Ben-Cohen  * Ohad Ben-Cohen <ohad@wizery.com>
904a9016eSOhad Ben-Cohen  * Brian Swetland <swetland@google.com>
1004a9016eSOhad Ben-Cohen  */
1104a9016eSOhad Ben-Cohen 
120a0f0d8bSChristoph Hellwig #include <linux/dma-map-ops.h>
1304a9016eSOhad Ben-Cohen #include <linux/export.h>
14086d0872SLoic Pallardy #include <linux/of_reserved_mem.h>
1504a9016eSOhad Ben-Cohen #include <linux/remoteproc.h>
1604a9016eSOhad Ben-Cohen #include <linux/virtio.h>
1704a9016eSOhad Ben-Cohen #include <linux/virtio_config.h>
1804a9016eSOhad Ben-Cohen #include <linux/virtio_ids.h>
1904a9016eSOhad Ben-Cohen #include <linux/virtio_ring.h>
2004a9016eSOhad Ben-Cohen #include <linux/err.h>
2104a9016eSOhad Ben-Cohen #include <linux/kref.h>
2204a9016eSOhad Ben-Cohen #include <linux/slab.h>
2304a9016eSOhad Ben-Cohen 
2404a9016eSOhad Ben-Cohen #include "remoteproc_internal.h"
2504a9016eSOhad Ben-Cohen 
2699555489SArnaud Pouliquen static struct rproc_vdev *vdev_to_rvdev(struct virtio_device *vdev)
2799555489SArnaud Pouliquen {
2899555489SArnaud Pouliquen 	return container_of(vdev->dev.parent, struct rproc_vdev, dev);
2999555489SArnaud Pouliquen }
3099555489SArnaud Pouliquen 
3199555489SArnaud Pouliquen static  struct rproc *vdev_to_rproc(struct virtio_device *vdev)
3299555489SArnaud Pouliquen {
3399555489SArnaud Pouliquen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
3499555489SArnaud Pouliquen 
3599555489SArnaud Pouliquen 	return rvdev->rproc;
3699555489SArnaud Pouliquen }
3799555489SArnaud Pouliquen 
3804a9016eSOhad Ben-Cohen /* kick the remote processor, and let it know which virtqueue to poke at */
3946f9c2b9SHeinz Graalfs static bool rproc_virtio_notify(struct virtqueue *vq)
4004a9016eSOhad Ben-Cohen {
417a186941SOhad Ben-Cohen 	struct rproc_vring *rvring = vq->priv;
427a186941SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
437a186941SOhad Ben-Cohen 	int notifyid = rvring->notifyid;
4404a9016eSOhad Ben-Cohen 
45b5ab5e24SOhad Ben-Cohen 	dev_dbg(&rproc->dev, "kicking vq index: %d\n", notifyid);
4604a9016eSOhad Ben-Cohen 
477a186941SOhad Ben-Cohen 	rproc->ops->kick(rproc, notifyid);
4846f9c2b9SHeinz Graalfs 	return true;
4904a9016eSOhad Ben-Cohen }
5004a9016eSOhad Ben-Cohen 
5104a9016eSOhad Ben-Cohen /**
5204a9016eSOhad Ben-Cohen  * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted
5304a9016eSOhad Ben-Cohen  * @rproc: handle to the remote processor
547a186941SOhad Ben-Cohen  * @notifyid: index of the signalled virtqueue (unique per this @rproc)
5504a9016eSOhad Ben-Cohen  *
5604a9016eSOhad Ben-Cohen  * This function should be called by the platform-specific rproc driver,
5704a9016eSOhad Ben-Cohen  * when the remote processor signals that a specific virtqueue has pending
5804a9016eSOhad Ben-Cohen  * messages available.
5904a9016eSOhad Ben-Cohen  *
60f2867434SSuman Anna  * Return: IRQ_NONE if no message was found in the @notifyid virtqueue,
6104a9016eSOhad Ben-Cohen  * and otherwise returns IRQ_HANDLED.
6204a9016eSOhad Ben-Cohen  */
637a186941SOhad Ben-Cohen irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid)
6404a9016eSOhad Ben-Cohen {
657a186941SOhad Ben-Cohen 	struct rproc_vring *rvring;
667a186941SOhad Ben-Cohen 
67b5ab5e24SOhad Ben-Cohen 	dev_dbg(&rproc->dev, "vq index %d is interrupted\n", notifyid);
687a186941SOhad Ben-Cohen 
697a186941SOhad Ben-Cohen 	rvring = idr_find(&rproc->notifyids, notifyid);
707a186941SOhad Ben-Cohen 	if (!rvring || !rvring->vq)
717a186941SOhad Ben-Cohen 		return IRQ_NONE;
727a186941SOhad Ben-Cohen 
737a186941SOhad Ben-Cohen 	return vring_interrupt(0, rvring->vq);
7404a9016eSOhad Ben-Cohen }
7504a9016eSOhad Ben-Cohen EXPORT_SYMBOL(rproc_vq_interrupt);
7604a9016eSOhad Ben-Cohen 
7704a9016eSOhad Ben-Cohen static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
78f145928dSAnna, Suman 				    unsigned int id,
7904a9016eSOhad Ben-Cohen 				    void (*callback)(struct virtqueue *vq),
80f94682ddSMichael S. Tsirkin 				    const char *name, bool ctx)
8104a9016eSOhad Ben-Cohen {
827a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
8304a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
84b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
85c6aed238SLoic Pallardy 	struct rproc_mem_entry *mem;
867a186941SOhad Ben-Cohen 	struct rproc_vring *rvring;
87c6aed238SLoic Pallardy 	struct fw_rsc_vdev *rsc;
8804a9016eSOhad Ben-Cohen 	struct virtqueue *vq;
8904a9016eSOhad Ben-Cohen 	void *addr;
90c2a052a4SXuan Zhuo 	int num, size;
9104a9016eSOhad Ben-Cohen 
927a186941SOhad Ben-Cohen 	/* we're temporarily limited to two virtqueues per rvdev */
937a186941SOhad Ben-Cohen 	if (id >= ARRAY_SIZE(rvdev->vring))
947a186941SOhad Ben-Cohen 		return ERR_PTR(-EINVAL);
9504a9016eSOhad Ben-Cohen 
966457f126SMichael S. Tsirkin 	if (!name)
976457f126SMichael S. Tsirkin 		return NULL;
986457f126SMichael S. Tsirkin 
99c6aed238SLoic Pallardy 	/* Search allocated memory region by name */
100c6aed238SLoic Pallardy 	mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index,
101c6aed238SLoic Pallardy 					  id);
102c6aed238SLoic Pallardy 	if (!mem || !mem->va)
103c6aed238SLoic Pallardy 		return ERR_PTR(-ENOMEM);
104c6aed238SLoic Pallardy 
1056db20ea8SOhad Ben-Cohen 	rvring = &rvdev->vring[id];
106c6aed238SLoic Pallardy 	addr = mem->va;
107c2a052a4SXuan Zhuo 	num = rvring->num;
10804a9016eSOhad Ben-Cohen 
1097a186941SOhad Ben-Cohen 	/* zero vring */
110c2a052a4SXuan Zhuo 	size = vring_size(num, rvring->align);
1117a186941SOhad Ben-Cohen 	memset(addr, 0, size);
1127a186941SOhad Ben-Cohen 
113276ec993SLoic Pallardy 	dev_dbg(dev, "vring%d: va %pK qsz %d notifyid %d\n",
114c2a052a4SXuan Zhuo 		id, addr, num, rvring->notifyid);
11504a9016eSOhad Ben-Cohen 
11604a9016eSOhad Ben-Cohen 	/*
11704a9016eSOhad Ben-Cohen 	 * Create the new vq, and tell virtio we're not interested in
11804a9016eSOhad Ben-Cohen 	 * the 'weak' smp barriers, since we're talking with a real device.
11904a9016eSOhad Ben-Cohen 	 */
120c2a052a4SXuan Zhuo 	vq = vring_new_virtqueue(id, num, rvring->align, vdev, false, ctx,
121f94682ddSMichael S. Tsirkin 				 addr, rproc_virtio_notify, callback, name);
12204a9016eSOhad Ben-Cohen 	if (!vq) {
123b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vring_new_virtqueue %s failed\n", name);
1246db20ea8SOhad Ben-Cohen 		rproc_free_vring(rvring);
1257a186941SOhad Ben-Cohen 		return ERR_PTR(-ENOMEM);
12604a9016eSOhad Ben-Cohen 	}
12704a9016eSOhad Ben-Cohen 
128*da802961SXuan Zhuo 	vq->num_max = num;
129*da802961SXuan Zhuo 
1307a186941SOhad Ben-Cohen 	rvring->vq = vq;
1317a186941SOhad Ben-Cohen 	vq->priv = rvring;
13204a9016eSOhad Ben-Cohen 
133c6aed238SLoic Pallardy 	/* Update vring in resource table */
134c6aed238SLoic Pallardy 	rsc = (void *)rproc->table_ptr + rvdev->rsc_offset;
135c6aed238SLoic Pallardy 	rsc->vring[id].da = mem->da;
136c6aed238SLoic Pallardy 
13704a9016eSOhad Ben-Cohen 	return vq;
13804a9016eSOhad Ben-Cohen }
13904a9016eSOhad Ben-Cohen 
140dab55bbaSOhad Ben-Cohen static void __rproc_virtio_del_vqs(struct virtio_device *vdev)
14104a9016eSOhad Ben-Cohen {
14204a9016eSOhad Ben-Cohen 	struct virtqueue *vq, *n;
1437a186941SOhad Ben-Cohen 	struct rproc_vring *rvring;
14404a9016eSOhad Ben-Cohen 
14504a9016eSOhad Ben-Cohen 	list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
1467a186941SOhad Ben-Cohen 		rvring = vq->priv;
1477a186941SOhad Ben-Cohen 		rvring->vq = NULL;
14804a9016eSOhad Ben-Cohen 		vring_del_virtqueue(vq);
14904a9016eSOhad Ben-Cohen 	}
15004a9016eSOhad Ben-Cohen }
15104a9016eSOhad Ben-Cohen 
152dab55bbaSOhad Ben-Cohen static void rproc_virtio_del_vqs(struct virtio_device *vdev)
153dab55bbaSOhad Ben-Cohen {
154dab55bbaSOhad Ben-Cohen 	__rproc_virtio_del_vqs(vdev);
155dab55bbaSOhad Ben-Cohen }
156dab55bbaSOhad Ben-Cohen 
157f145928dSAnna, Suman static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs,
15804a9016eSOhad Ben-Cohen 				 struct virtqueue *vqs[],
15904a9016eSOhad Ben-Cohen 				 vq_callback_t *callbacks[],
160fb5e31d9SChristoph Hellwig 				 const char * const names[],
161f94682ddSMichael S. Tsirkin 				 const bool * ctx,
162fb5e31d9SChristoph Hellwig 				 struct irq_affinity *desc)
16304a9016eSOhad Ben-Cohen {
164a229989dSWei Wang 	int i, ret, queue_idx = 0;
16504a9016eSOhad Ben-Cohen 
16604a9016eSOhad Ben-Cohen 	for (i = 0; i < nvqs; ++i) {
167a229989dSWei Wang 		if (!names[i]) {
168a229989dSWei Wang 			vqs[i] = NULL;
169a229989dSWei Wang 			continue;
170a229989dSWei Wang 		}
171a229989dSWei Wang 
172a229989dSWei Wang 		vqs[i] = rp_find_vq(vdev, queue_idx++, callbacks[i], names[i],
173f94682ddSMichael S. Tsirkin 				    ctx ? ctx[i] : false);
17404a9016eSOhad Ben-Cohen 		if (IS_ERR(vqs[i])) {
17504a9016eSOhad Ben-Cohen 			ret = PTR_ERR(vqs[i]);
17604a9016eSOhad Ben-Cohen 			goto error;
17704a9016eSOhad Ben-Cohen 		}
17804a9016eSOhad Ben-Cohen 	}
17904a9016eSOhad Ben-Cohen 
18004a9016eSOhad Ben-Cohen 	return 0;
18104a9016eSOhad Ben-Cohen 
18204a9016eSOhad Ben-Cohen error:
183dab55bbaSOhad Ben-Cohen 	__rproc_virtio_del_vqs(vdev);
18404a9016eSOhad Ben-Cohen 	return ret;
18504a9016eSOhad Ben-Cohen }
18604a9016eSOhad Ben-Cohen 
18704a9016eSOhad Ben-Cohen static u8 rproc_virtio_get_status(struct virtio_device *vdev)
18804a9016eSOhad Ben-Cohen {
18992b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
19092b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
19192b38f85SSjur Brændeland 
19292b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
19392b38f85SSjur Brændeland 
19492b38f85SSjur Brændeland 	return rsc->status;
19504a9016eSOhad Ben-Cohen }
19604a9016eSOhad Ben-Cohen 
19704a9016eSOhad Ben-Cohen static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
19804a9016eSOhad Ben-Cohen {
19992b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
20092b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
20192b38f85SSjur Brændeland 
20292b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
20392b38f85SSjur Brændeland 
20492b38f85SSjur Brændeland 	rsc->status = status;
2057a186941SOhad Ben-Cohen 	dev_dbg(&vdev->dev, "status: %d\n", status);
20604a9016eSOhad Ben-Cohen }
20704a9016eSOhad Ben-Cohen 
20804a9016eSOhad Ben-Cohen static void rproc_virtio_reset(struct virtio_device *vdev)
20904a9016eSOhad Ben-Cohen {
21092b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
21192b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
21292b38f85SSjur Brændeland 
21392b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
21492b38f85SSjur Brændeland 
21592b38f85SSjur Brændeland 	rsc->status = 0;
21604a9016eSOhad Ben-Cohen 	dev_dbg(&vdev->dev, "reset !\n");
21704a9016eSOhad Ben-Cohen }
21804a9016eSOhad Ben-Cohen 
21904a9016eSOhad Ben-Cohen /* provide the vdev features as retrieved from the firmware */
220d0254773SMichael S. Tsirkin static u64 rproc_virtio_get_features(struct virtio_device *vdev)
22104a9016eSOhad Ben-Cohen {
2227a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
22392b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
22404a9016eSOhad Ben-Cohen 
22592b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
22692b38f85SSjur Brændeland 
22792b38f85SSjur Brændeland 	return rsc->dfeatures;
22804a9016eSOhad Ben-Cohen }
22904a9016eSOhad Ben-Cohen 
2303a814fdfSTiwei Bie static void rproc_transport_features(struct virtio_device *vdev)
2313a814fdfSTiwei Bie {
2323a814fdfSTiwei Bie 	/*
2333a814fdfSTiwei Bie 	 * Packed ring isn't enabled on remoteproc for now,
2343a814fdfSTiwei Bie 	 * because remoteproc uses vring_new_virtqueue() which
2353a814fdfSTiwei Bie 	 * creates virtio rings on preallocated memory.
2363a814fdfSTiwei Bie 	 */
2373a814fdfSTiwei Bie 	__virtio_clear_bit(vdev, VIRTIO_F_RING_PACKED);
2383a814fdfSTiwei Bie }
2393a814fdfSTiwei Bie 
2405c609a5eSMichael S. Tsirkin static int rproc_virtio_finalize_features(struct virtio_device *vdev)
24104a9016eSOhad Ben-Cohen {
2427a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
24392b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
24492b38f85SSjur Brændeland 
24592b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
24604a9016eSOhad Ben-Cohen 
24704a9016eSOhad Ben-Cohen 	/* Give virtio_ring a chance to accept features */
24804a9016eSOhad Ben-Cohen 	vring_transport_features(vdev);
24904a9016eSOhad Ben-Cohen 
2503a814fdfSTiwei Bie 	/* Give virtio_rproc a chance to accept features. */
2513a814fdfSTiwei Bie 	rproc_transport_features(vdev);
2523a814fdfSTiwei Bie 
25393d389f8SMichael S. Tsirkin 	/* Make sure we don't have any features > 32 bits! */
25493d389f8SMichael S. Tsirkin 	BUG_ON((u32)vdev->features != vdev->features);
25593d389f8SMichael S. Tsirkin 
25604a9016eSOhad Ben-Cohen 	/*
25704a9016eSOhad Ben-Cohen 	 * Remember the finalized features of our vdev, and provide it
25804a9016eSOhad Ben-Cohen 	 * to the remote processor once it is powered on.
25904a9016eSOhad Ben-Cohen 	 */
260e16e12beSMichael S. Tsirkin 	rsc->gfeatures = vdev->features;
2615c609a5eSMichael S. Tsirkin 
2625c609a5eSMichael S. Tsirkin 	return 0;
26392b38f85SSjur Brændeland }
26492b38f85SSjur Brændeland 
265f145928dSAnna, Suman static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset,
266f145928dSAnna, Suman 			     void *buf, unsigned int len)
26792b38f85SSjur Brændeland {
26892b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
26992b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
27092b38f85SSjur Brændeland 	void *cfg;
27192b38f85SSjur Brændeland 
27292b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
27392b38f85SSjur Brændeland 	cfg = &rsc->vring[rsc->num_of_vrings];
27492b38f85SSjur Brændeland 
27592b38f85SSjur Brændeland 	if (offset + len > rsc->config_len || offset + len < len) {
27692b38f85SSjur Brændeland 		dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
27792b38f85SSjur Brændeland 		return;
27892b38f85SSjur Brændeland 	}
27992b38f85SSjur Brændeland 
28092b38f85SSjur Brændeland 	memcpy(buf, cfg + offset, len);
28192b38f85SSjur Brændeland }
28292b38f85SSjur Brændeland 
283f145928dSAnna, Suman static void rproc_virtio_set(struct virtio_device *vdev, unsigned int offset,
284f145928dSAnna, Suman 			     const void *buf, unsigned int len)
28592b38f85SSjur Brændeland {
28692b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
28792b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
28892b38f85SSjur Brændeland 	void *cfg;
28992b38f85SSjur Brændeland 
29092b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
29192b38f85SSjur Brændeland 	cfg = &rsc->vring[rsc->num_of_vrings];
29292b38f85SSjur Brændeland 
29392b38f85SSjur Brændeland 	if (offset + len > rsc->config_len || offset + len < len) {
29492b38f85SSjur Brændeland 		dev_err(&vdev->dev, "rproc_virtio_set: access out of bounds\n");
29592b38f85SSjur Brændeland 		return;
29692b38f85SSjur Brændeland 	}
29792b38f85SSjur Brændeland 
29892b38f85SSjur Brændeland 	memcpy(cfg + offset, buf, len);
29904a9016eSOhad Ben-Cohen }
30004a9016eSOhad Ben-Cohen 
30193503932SStephen Hemminger static const struct virtio_config_ops rproc_virtio_config_ops = {
30204a9016eSOhad Ben-Cohen 	.get_features	= rproc_virtio_get_features,
30304a9016eSOhad Ben-Cohen 	.finalize_features = rproc_virtio_finalize_features,
30404a9016eSOhad Ben-Cohen 	.find_vqs	= rproc_virtio_find_vqs,
30504a9016eSOhad Ben-Cohen 	.del_vqs	= rproc_virtio_del_vqs,
30604a9016eSOhad Ben-Cohen 	.reset		= rproc_virtio_reset,
30704a9016eSOhad Ben-Cohen 	.set_status	= rproc_virtio_set_status,
30804a9016eSOhad Ben-Cohen 	.get_status	= rproc_virtio_get_status,
30992b38f85SSjur Brændeland 	.get		= rproc_virtio_get,
31092b38f85SSjur Brændeland 	.set		= rproc_virtio_set,
31104a9016eSOhad Ben-Cohen };
31204a9016eSOhad Ben-Cohen 
31304a9016eSOhad Ben-Cohen /*
31404a9016eSOhad Ben-Cohen  * This function is called whenever vdev is released, and is responsible
3157183a2a7SOhad Ben-Cohen  * to decrement the remote processor's refcount which was taken when vdev was
31604a9016eSOhad Ben-Cohen  * added.
31704a9016eSOhad Ben-Cohen  *
31804a9016eSOhad Ben-Cohen  * Never call this function directly; it will be called by the driver
31904a9016eSOhad Ben-Cohen  * core when needed.
32004a9016eSOhad Ben-Cohen  */
321aab8d802SBjorn Andersson static void rproc_virtio_dev_release(struct device *dev)
32204a9016eSOhad Ben-Cohen {
32304a9016eSOhad Ben-Cohen 	struct virtio_device *vdev = dev_to_virtio(dev);
3246db20ea8SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
32504a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
32604a9016eSOhad Ben-Cohen 
327d4c036feSLoic Pallardy 	kfree(vdev);
328d4c036feSLoic Pallardy 
329aab8d802SBjorn Andersson 	kref_put(&rvdev->refcount, rproc_vdev_release);
3306db20ea8SOhad Ben-Cohen 
3317183a2a7SOhad Ben-Cohen 	put_device(&rproc->dev);
33204a9016eSOhad Ben-Cohen }
33304a9016eSOhad Ben-Cohen 
33404a9016eSOhad Ben-Cohen /**
3357a186941SOhad Ben-Cohen  * rproc_add_virtio_dev() - register an rproc-induced virtio device
3367a186941SOhad Ben-Cohen  * @rvdev: the remote vdev
3372e7d4c2cSArnaud Pouliquen  * @id: the device type identification (used to match it with a driver).
33804a9016eSOhad Ben-Cohen  *
3397a186941SOhad Ben-Cohen  * This function registers a virtio device. This vdev's partent is
3407a186941SOhad Ben-Cohen  * the rproc device.
34104a9016eSOhad Ben-Cohen  *
342f2867434SSuman Anna  * Return: 0 on success or an appropriate error value otherwise
34304a9016eSOhad Ben-Cohen  */
3447a186941SOhad Ben-Cohen int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
34504a9016eSOhad Ben-Cohen {
3467a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
347086d0872SLoic Pallardy 	struct device *dev = &rvdev->dev;
348d4c036feSLoic Pallardy 	struct virtio_device *vdev;
349086d0872SLoic Pallardy 	struct rproc_mem_entry *mem;
35004a9016eSOhad Ben-Cohen 	int ret;
35104a9016eSOhad Ben-Cohen 
352791c13b7SNikita Shubin 	if (rproc->ops->kick == NULL) {
353791c13b7SNikita Shubin 		ret = -EINVAL;
3542fb75ceaSChristophe JAILLET 		dev_err(dev, ".kick method not defined for %s\n", rproc->name);
355791c13b7SNikita Shubin 		goto out;
356791c13b7SNikita Shubin 	}
357791c13b7SNikita Shubin 
358086d0872SLoic Pallardy 	/* Try to find dedicated vdev buffer carveout */
359086d0872SLoic Pallardy 	mem = rproc_find_carveout_by_name(rproc, "vdev%dbuffer", rvdev->index);
360086d0872SLoic Pallardy 	if (mem) {
361086d0872SLoic Pallardy 		phys_addr_t pa;
362086d0872SLoic Pallardy 
363086d0872SLoic Pallardy 		if (mem->of_resm_idx != -1) {
364086d0872SLoic Pallardy 			struct device_node *np = rproc->dev.parent->of_node;
365086d0872SLoic Pallardy 
366086d0872SLoic Pallardy 			/* Associate reserved memory to vdev device */
367086d0872SLoic Pallardy 			ret = of_reserved_mem_device_init_by_idx(dev, np,
368086d0872SLoic Pallardy 								 mem->of_resm_idx);
369086d0872SLoic Pallardy 			if (ret) {
370086d0872SLoic Pallardy 				dev_err(dev, "Can't associate reserved memory\n");
371086d0872SLoic Pallardy 				goto out;
372086d0872SLoic Pallardy 			}
373086d0872SLoic Pallardy 		} else {
374086d0872SLoic Pallardy 			if (mem->va) {
375086d0872SLoic Pallardy 				dev_warn(dev, "vdev %d buffer already mapped\n",
376086d0872SLoic Pallardy 					 rvdev->index);
377086d0872SLoic Pallardy 				pa = rproc_va_to_pa(mem->va);
378086d0872SLoic Pallardy 			} else {
379086d0872SLoic Pallardy 				/* Use dma address as carveout no memmapped yet */
380086d0872SLoic Pallardy 				pa = (phys_addr_t)mem->dma;
381086d0872SLoic Pallardy 			}
382086d0872SLoic Pallardy 
383086d0872SLoic Pallardy 			/* Associate vdev buffer memory pool to vdev subdev */
384086d0872SLoic Pallardy 			ret = dma_declare_coherent_memory(dev, pa,
385086d0872SLoic Pallardy 							   mem->da,
386d664ce75SStephen Rothwell 							   mem->len);
387086d0872SLoic Pallardy 			if (ret < 0) {
388086d0872SLoic Pallardy 				dev_err(dev, "Failed to associate buffer\n");
389086d0872SLoic Pallardy 				goto out;
390086d0872SLoic Pallardy 			}
391086d0872SLoic Pallardy 		}
392db9178a4STero Kristo 	} else {
393db9178a4STero Kristo 		struct device_node *np = rproc->dev.parent->of_node;
394db9178a4STero Kristo 
395db9178a4STero Kristo 		/*
396db9178a4STero Kristo 		 * If we don't have dedicated buffer, just attempt to re-assign
397db9178a4STero Kristo 		 * the reserved memory from our parent. A default memory-region
398db9178a4STero Kristo 		 * at index 0 from the parent's memory-regions is assigned for
399db9178a4STero Kristo 		 * the rvdev dev to allocate from. Failure is non-critical and
400db9178a4STero Kristo 		 * the allocations will fall back to global pools, so don't
401db9178a4STero Kristo 		 * check return value either.
402db9178a4STero Kristo 		 */
403db9178a4STero Kristo 		of_reserved_mem_device_init_by_idx(dev, np, 0);
404086d0872SLoic Pallardy 	}
405086d0872SLoic Pallardy 
406d4c036feSLoic Pallardy 	/* Allocate virtio device */
407d4c036feSLoic Pallardy 	vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
408d4c036feSLoic Pallardy 	if (!vdev) {
409d4c036feSLoic Pallardy 		ret = -ENOMEM;
410d4c036feSLoic Pallardy 		goto out;
411d4c036feSLoic Pallardy 	}
4127a186941SOhad Ben-Cohen 	vdev->id.device	= id,
4137a186941SOhad Ben-Cohen 	vdev->config = &rproc_virtio_config_ops,
4147a186941SOhad Ben-Cohen 	vdev->dev.parent = dev;
415aab8d802SBjorn Andersson 	vdev->dev.release = rproc_virtio_dev_release;
41604a9016eSOhad Ben-Cohen 
41704a9016eSOhad Ben-Cohen 	/*
41804a9016eSOhad Ben-Cohen 	 * We're indirectly making a non-temporary copy of the rproc pointer
41904a9016eSOhad Ben-Cohen 	 * here, because drivers probed with this vdev will indirectly
42004a9016eSOhad Ben-Cohen 	 * access the wrapping rproc.
42104a9016eSOhad Ben-Cohen 	 *
42204a9016eSOhad Ben-Cohen 	 * Therefore we must increment the rproc refcount here, and decrement
42304a9016eSOhad Ben-Cohen 	 * it _only_ when the vdev is released.
42404a9016eSOhad Ben-Cohen 	 */
4257183a2a7SOhad Ben-Cohen 	get_device(&rproc->dev);
42604a9016eSOhad Ben-Cohen 
427aab8d802SBjorn Andersson 	/* Reference the vdev and vring allocations */
428aab8d802SBjorn Andersson 	kref_get(&rvdev->refcount);
429aab8d802SBjorn Andersson 
4307a186941SOhad Ben-Cohen 	ret = register_virtio_device(vdev);
43104a9016eSOhad Ben-Cohen 	if (ret) {
432900a163eSweiping zhang 		put_device(&vdev->dev);
43304a9016eSOhad Ben-Cohen 		dev_err(dev, "failed to register vdev: %d\n", ret);
4347a186941SOhad Ben-Cohen 		goto out;
43504a9016eSOhad Ben-Cohen 	}
43604a9016eSOhad Ben-Cohen 
4377a186941SOhad Ben-Cohen 	dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
4387a186941SOhad Ben-Cohen 
4397a186941SOhad Ben-Cohen out:
44004a9016eSOhad Ben-Cohen 	return ret;
44104a9016eSOhad Ben-Cohen }
44204a9016eSOhad Ben-Cohen 
44304a9016eSOhad Ben-Cohen /**
4447a186941SOhad Ben-Cohen  * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
445d4c036feSLoic Pallardy  * @dev: the virtio device
446d4c036feSLoic Pallardy  * @data: must be null
44704a9016eSOhad Ben-Cohen  *
4487a186941SOhad Ben-Cohen  * This function unregisters an existing virtio device.
449f2867434SSuman Anna  *
450f2867434SSuman Anna  * Return: 0
45104a9016eSOhad Ben-Cohen  */
452d4c036feSLoic Pallardy int rproc_remove_virtio_dev(struct device *dev, void *data)
45304a9016eSOhad Ben-Cohen {
454d4c036feSLoic Pallardy 	struct virtio_device *vdev = dev_to_virtio(dev);
455d4c036feSLoic Pallardy 
456d4c036feSLoic Pallardy 	unregister_virtio_device(vdev);
457d4c036feSLoic Pallardy 	return 0;
45804a9016eSOhad Ben-Cohen }
459