xref: /linux/drivers/remoteproc/remoteproc_virtio.c (revision 6457f126c888b3481fdae6f702e616cd0c79646e)
104a9016eSOhad Ben-Cohen /*
204a9016eSOhad Ben-Cohen  * Remote processor messaging transport (OMAP platform-specific bits)
304a9016eSOhad Ben-Cohen  *
404a9016eSOhad Ben-Cohen  * Copyright (C) 2011 Texas Instruments, Inc.
504a9016eSOhad Ben-Cohen  * Copyright (C) 2011 Google, Inc.
604a9016eSOhad Ben-Cohen  *
704a9016eSOhad Ben-Cohen  * Ohad Ben-Cohen <ohad@wizery.com>
804a9016eSOhad Ben-Cohen  * Brian Swetland <swetland@google.com>
904a9016eSOhad Ben-Cohen  *
1004a9016eSOhad Ben-Cohen  * This software is licensed under the terms of the GNU General Public
1104a9016eSOhad Ben-Cohen  * License version 2, as published by the Free Software Foundation, and
1204a9016eSOhad Ben-Cohen  * may be copied, distributed, and modified under those terms.
1304a9016eSOhad Ben-Cohen  *
1404a9016eSOhad Ben-Cohen  * This program is distributed in the hope that it will be useful,
1504a9016eSOhad Ben-Cohen  * but WITHOUT ANY WARRANTY; without even the implied warranty of
1604a9016eSOhad Ben-Cohen  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1704a9016eSOhad Ben-Cohen  * GNU General Public License for more details.
1804a9016eSOhad Ben-Cohen  */
1904a9016eSOhad Ben-Cohen 
2004a9016eSOhad Ben-Cohen #include <linux/export.h>
2104a9016eSOhad Ben-Cohen #include <linux/remoteproc.h>
2204a9016eSOhad Ben-Cohen #include <linux/virtio.h>
2304a9016eSOhad Ben-Cohen #include <linux/virtio_config.h>
2404a9016eSOhad Ben-Cohen #include <linux/virtio_ids.h>
2504a9016eSOhad Ben-Cohen #include <linux/virtio_ring.h>
2604a9016eSOhad Ben-Cohen #include <linux/err.h>
2704a9016eSOhad Ben-Cohen #include <linux/kref.h>
2804a9016eSOhad Ben-Cohen #include <linux/slab.h>
2904a9016eSOhad Ben-Cohen 
3004a9016eSOhad Ben-Cohen #include "remoteproc_internal.h"
3104a9016eSOhad Ben-Cohen 
3204a9016eSOhad Ben-Cohen /* kick the remote processor, and let it know which virtqueue to poke at */
3304a9016eSOhad Ben-Cohen static void rproc_virtio_notify(struct virtqueue *vq)
3404a9016eSOhad Ben-Cohen {
357a186941SOhad Ben-Cohen 	struct rproc_vring *rvring = vq->priv;
367a186941SOhad Ben-Cohen 	struct rproc *rproc = rvring->rvdev->rproc;
377a186941SOhad Ben-Cohen 	int notifyid = rvring->notifyid;
3804a9016eSOhad Ben-Cohen 
39b5ab5e24SOhad Ben-Cohen 	dev_dbg(&rproc->dev, "kicking vq index: %d\n", notifyid);
4004a9016eSOhad Ben-Cohen 
417a186941SOhad Ben-Cohen 	rproc->ops->kick(rproc, notifyid);
4204a9016eSOhad Ben-Cohen }
4304a9016eSOhad Ben-Cohen 
4404a9016eSOhad Ben-Cohen /**
4504a9016eSOhad Ben-Cohen  * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted
4604a9016eSOhad Ben-Cohen  * @rproc: handle to the remote processor
477a186941SOhad Ben-Cohen  * @notifyid: index of the signalled virtqueue (unique per this @rproc)
4804a9016eSOhad Ben-Cohen  *
4904a9016eSOhad Ben-Cohen  * This function should be called by the platform-specific rproc driver,
5004a9016eSOhad Ben-Cohen  * when the remote processor signals that a specific virtqueue has pending
5104a9016eSOhad Ben-Cohen  * messages available.
5204a9016eSOhad Ben-Cohen  *
537a186941SOhad Ben-Cohen  * Returns IRQ_NONE if no message was found in the @notifyid virtqueue,
5404a9016eSOhad Ben-Cohen  * and otherwise returns IRQ_HANDLED.
5504a9016eSOhad Ben-Cohen  */
567a186941SOhad Ben-Cohen irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid)
5704a9016eSOhad Ben-Cohen {
587a186941SOhad Ben-Cohen 	struct rproc_vring *rvring;
597a186941SOhad Ben-Cohen 
60b5ab5e24SOhad Ben-Cohen 	dev_dbg(&rproc->dev, "vq index %d is interrupted\n", notifyid);
617a186941SOhad Ben-Cohen 
627a186941SOhad Ben-Cohen 	rvring = idr_find(&rproc->notifyids, notifyid);
637a186941SOhad Ben-Cohen 	if (!rvring || !rvring->vq)
647a186941SOhad Ben-Cohen 		return IRQ_NONE;
657a186941SOhad Ben-Cohen 
667a186941SOhad Ben-Cohen 	return vring_interrupt(0, rvring->vq);
6704a9016eSOhad Ben-Cohen }
6804a9016eSOhad Ben-Cohen EXPORT_SYMBOL(rproc_vq_interrupt);
6904a9016eSOhad Ben-Cohen 
7004a9016eSOhad Ben-Cohen static struct virtqueue *rp_find_vq(struct virtio_device *vdev,
7104a9016eSOhad Ben-Cohen 				    unsigned id,
7204a9016eSOhad Ben-Cohen 				    void (*callback)(struct virtqueue *vq),
7304a9016eSOhad Ben-Cohen 				    const char *name)
7404a9016eSOhad Ben-Cohen {
757a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
7604a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
77b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
787a186941SOhad Ben-Cohen 	struct rproc_vring *rvring;
7904a9016eSOhad Ben-Cohen 	struct virtqueue *vq;
8004a9016eSOhad Ben-Cohen 	void *addr;
816db20ea8SOhad Ben-Cohen 	int len, size, ret;
8204a9016eSOhad Ben-Cohen 
837a186941SOhad Ben-Cohen 	/* we're temporarily limited to two virtqueues per rvdev */
847a186941SOhad Ben-Cohen 	if (id >= ARRAY_SIZE(rvdev->vring))
857a186941SOhad Ben-Cohen 		return ERR_PTR(-EINVAL);
8604a9016eSOhad Ben-Cohen 
87*6457f126SMichael S. Tsirkin 	if (!name)
88*6457f126SMichael S. Tsirkin 		return NULL;
89*6457f126SMichael S. Tsirkin 
906db20ea8SOhad Ben-Cohen 	ret = rproc_alloc_vring(rvdev, id);
916db20ea8SOhad Ben-Cohen 	if (ret)
926db20ea8SOhad Ben-Cohen 		return ERR_PTR(ret);
9304a9016eSOhad Ben-Cohen 
946db20ea8SOhad Ben-Cohen 	rvring = &rvdev->vring[id];
957a186941SOhad Ben-Cohen 	addr = rvring->va;
967a186941SOhad Ben-Cohen 	len = rvring->len;
9704a9016eSOhad Ben-Cohen 
987a186941SOhad Ben-Cohen 	/* zero vring */
997a186941SOhad Ben-Cohen 	size = vring_size(len, rvring->align);
1007a186941SOhad Ben-Cohen 	memset(addr, 0, size);
1017a186941SOhad Ben-Cohen 
102b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "vring%d: va %p qsz %d notifyid %d\n",
1037a186941SOhad Ben-Cohen 					id, addr, len, rvring->notifyid);
10404a9016eSOhad Ben-Cohen 
10504a9016eSOhad Ben-Cohen 	/*
10604a9016eSOhad Ben-Cohen 	 * Create the new vq, and tell virtio we're not interested in
10704a9016eSOhad Ben-Cohen 	 * the 'weak' smp barriers, since we're talking with a real device.
10804a9016eSOhad Ben-Cohen 	 */
10917bb6d40SJason Wang 	vq = vring_new_virtqueue(id, len, rvring->align, vdev, false, addr,
11004a9016eSOhad Ben-Cohen 					rproc_virtio_notify, callback, name);
11104a9016eSOhad Ben-Cohen 	if (!vq) {
112b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vring_new_virtqueue %s failed\n", name);
1136db20ea8SOhad Ben-Cohen 		rproc_free_vring(rvring);
1147a186941SOhad Ben-Cohen 		return ERR_PTR(-ENOMEM);
11504a9016eSOhad Ben-Cohen 	}
11604a9016eSOhad Ben-Cohen 
1177a186941SOhad Ben-Cohen 	rvring->vq = vq;
1187a186941SOhad Ben-Cohen 	vq->priv = rvring;
11904a9016eSOhad Ben-Cohen 
12004a9016eSOhad Ben-Cohen 	return vq;
12104a9016eSOhad Ben-Cohen }
12204a9016eSOhad Ben-Cohen 
12304a9016eSOhad Ben-Cohen static void rproc_virtio_del_vqs(struct virtio_device *vdev)
12404a9016eSOhad Ben-Cohen {
12504a9016eSOhad Ben-Cohen 	struct virtqueue *vq, *n;
12604a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
1277a186941SOhad Ben-Cohen 	struct rproc_vring *rvring;
12804a9016eSOhad Ben-Cohen 
12941a6ee09SOhad Ben-Cohen 	/* power down the remote processor before deleting vqs */
13041a6ee09SOhad Ben-Cohen 	rproc_shutdown(rproc);
13141a6ee09SOhad Ben-Cohen 
13204a9016eSOhad Ben-Cohen 	list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
1337a186941SOhad Ben-Cohen 		rvring = vq->priv;
1347a186941SOhad Ben-Cohen 		rvring->vq = NULL;
13504a9016eSOhad Ben-Cohen 		vring_del_virtqueue(vq);
1366db20ea8SOhad Ben-Cohen 		rproc_free_vring(rvring);
13704a9016eSOhad Ben-Cohen 	}
13804a9016eSOhad Ben-Cohen }
13904a9016eSOhad Ben-Cohen 
14004a9016eSOhad Ben-Cohen static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
14104a9016eSOhad Ben-Cohen 		       struct virtqueue *vqs[],
14204a9016eSOhad Ben-Cohen 		       vq_callback_t *callbacks[],
14304a9016eSOhad Ben-Cohen 		       const char *names[])
14404a9016eSOhad Ben-Cohen {
14504a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
14604a9016eSOhad Ben-Cohen 	int i, ret;
14704a9016eSOhad Ben-Cohen 
14804a9016eSOhad Ben-Cohen 	for (i = 0; i < nvqs; ++i) {
14904a9016eSOhad Ben-Cohen 		vqs[i] = rp_find_vq(vdev, i, callbacks[i], names[i]);
15004a9016eSOhad Ben-Cohen 		if (IS_ERR(vqs[i])) {
15104a9016eSOhad Ben-Cohen 			ret = PTR_ERR(vqs[i]);
15204a9016eSOhad Ben-Cohen 			goto error;
15304a9016eSOhad Ben-Cohen 		}
15404a9016eSOhad Ben-Cohen 	}
15504a9016eSOhad Ben-Cohen 
15641a6ee09SOhad Ben-Cohen 	/* now that the vqs are all set, boot the remote processor */
15741a6ee09SOhad Ben-Cohen 	ret = rproc_boot(rproc);
15841a6ee09SOhad Ben-Cohen 	if (ret) {
159b5ab5e24SOhad Ben-Cohen 		dev_err(&rproc->dev, "rproc_boot() failed %d\n", ret);
16041a6ee09SOhad Ben-Cohen 		goto error;
16141a6ee09SOhad Ben-Cohen 	}
16241a6ee09SOhad Ben-Cohen 
16304a9016eSOhad Ben-Cohen 	return 0;
16404a9016eSOhad Ben-Cohen 
16504a9016eSOhad Ben-Cohen error:
16604a9016eSOhad Ben-Cohen 	rproc_virtio_del_vqs(vdev);
16704a9016eSOhad Ben-Cohen 	return ret;
16804a9016eSOhad Ben-Cohen }
16904a9016eSOhad Ben-Cohen 
17004a9016eSOhad Ben-Cohen /*
17104a9016eSOhad Ben-Cohen  * We don't support yet real virtio status semantics.
17204a9016eSOhad Ben-Cohen  *
1737a186941SOhad Ben-Cohen  * The plan is to provide this via the VDEV resource entry
17404a9016eSOhad Ben-Cohen  * which is part of the firmware: this way the remote processor
17504a9016eSOhad Ben-Cohen  * will be able to access the status values as set by us.
17604a9016eSOhad Ben-Cohen  */
17704a9016eSOhad Ben-Cohen static u8 rproc_virtio_get_status(struct virtio_device *vdev)
17804a9016eSOhad Ben-Cohen {
17904a9016eSOhad Ben-Cohen 	return 0;
18004a9016eSOhad Ben-Cohen }
18104a9016eSOhad Ben-Cohen 
18204a9016eSOhad Ben-Cohen static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
18304a9016eSOhad Ben-Cohen {
1847a186941SOhad Ben-Cohen 	dev_dbg(&vdev->dev, "status: %d\n", status);
18504a9016eSOhad Ben-Cohen }
18604a9016eSOhad Ben-Cohen 
18704a9016eSOhad Ben-Cohen static void rproc_virtio_reset(struct virtio_device *vdev)
18804a9016eSOhad Ben-Cohen {
18904a9016eSOhad Ben-Cohen 	dev_dbg(&vdev->dev, "reset !\n");
19004a9016eSOhad Ben-Cohen }
19104a9016eSOhad Ben-Cohen 
19204a9016eSOhad Ben-Cohen /* provide the vdev features as retrieved from the firmware */
19304a9016eSOhad Ben-Cohen static u32 rproc_virtio_get_features(struct virtio_device *vdev)
19404a9016eSOhad Ben-Cohen {
1957a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
19604a9016eSOhad Ben-Cohen 
1977a186941SOhad Ben-Cohen 	return rvdev->dfeatures;
19804a9016eSOhad Ben-Cohen }
19904a9016eSOhad Ben-Cohen 
20004a9016eSOhad Ben-Cohen static void rproc_virtio_finalize_features(struct virtio_device *vdev)
20104a9016eSOhad Ben-Cohen {
2027a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
20304a9016eSOhad Ben-Cohen 
20404a9016eSOhad Ben-Cohen 	/* Give virtio_ring a chance to accept features */
20504a9016eSOhad Ben-Cohen 	vring_transport_features(vdev);
20604a9016eSOhad Ben-Cohen 
20704a9016eSOhad Ben-Cohen 	/*
20804a9016eSOhad Ben-Cohen 	 * Remember the finalized features of our vdev, and provide it
20904a9016eSOhad Ben-Cohen 	 * to the remote processor once it is powered on.
21004a9016eSOhad Ben-Cohen 	 *
21104a9016eSOhad Ben-Cohen 	 * Similarly to the status field, we don't expose yet the negotiated
21204a9016eSOhad Ben-Cohen 	 * features to the remote processors at this point. This will be
21304a9016eSOhad Ben-Cohen 	 * fixed as part of a small resource table overhaul and then an
21404a9016eSOhad Ben-Cohen 	 * extension of the virtio resource entries.
21504a9016eSOhad Ben-Cohen 	 */
2167a186941SOhad Ben-Cohen 	rvdev->gfeatures = vdev->features[0];
21704a9016eSOhad Ben-Cohen }
21804a9016eSOhad Ben-Cohen 
21904a9016eSOhad Ben-Cohen static struct virtio_config_ops rproc_virtio_config_ops = {
22004a9016eSOhad Ben-Cohen 	.get_features	= rproc_virtio_get_features,
22104a9016eSOhad Ben-Cohen 	.finalize_features = rproc_virtio_finalize_features,
22204a9016eSOhad Ben-Cohen 	.find_vqs	= rproc_virtio_find_vqs,
22304a9016eSOhad Ben-Cohen 	.del_vqs	= rproc_virtio_del_vqs,
22404a9016eSOhad Ben-Cohen 	.reset		= rproc_virtio_reset,
22504a9016eSOhad Ben-Cohen 	.set_status	= rproc_virtio_set_status,
22604a9016eSOhad Ben-Cohen 	.get_status	= rproc_virtio_get_status,
22704a9016eSOhad Ben-Cohen };
22804a9016eSOhad Ben-Cohen 
22904a9016eSOhad Ben-Cohen /*
23004a9016eSOhad Ben-Cohen  * This function is called whenever vdev is released, and is responsible
2317183a2a7SOhad Ben-Cohen  * to decrement the remote processor's refcount which was taken when vdev was
23204a9016eSOhad Ben-Cohen  * added.
23304a9016eSOhad Ben-Cohen  *
23404a9016eSOhad Ben-Cohen  * Never call this function directly; it will be called by the driver
23504a9016eSOhad Ben-Cohen  * core when needed.
23604a9016eSOhad Ben-Cohen  */
23704a9016eSOhad Ben-Cohen static void rproc_vdev_release(struct device *dev)
23804a9016eSOhad Ben-Cohen {
23904a9016eSOhad Ben-Cohen 	struct virtio_device *vdev = dev_to_virtio(dev);
2406db20ea8SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
24104a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
24204a9016eSOhad Ben-Cohen 
2436db20ea8SOhad Ben-Cohen 	list_del(&rvdev->node);
2446db20ea8SOhad Ben-Cohen 	kfree(rvdev);
2456db20ea8SOhad Ben-Cohen 
2467183a2a7SOhad Ben-Cohen 	put_device(&rproc->dev);
24704a9016eSOhad Ben-Cohen }
24804a9016eSOhad Ben-Cohen 
24904a9016eSOhad Ben-Cohen /**
2507a186941SOhad Ben-Cohen  * rproc_add_virtio_dev() - register an rproc-induced virtio device
2517a186941SOhad Ben-Cohen  * @rvdev: the remote vdev
25204a9016eSOhad Ben-Cohen  *
2537a186941SOhad Ben-Cohen  * This function registers a virtio device. This vdev's partent is
2547a186941SOhad Ben-Cohen  * the rproc device.
25504a9016eSOhad Ben-Cohen  *
2567a186941SOhad Ben-Cohen  * Returns 0 on success or an appropriate error value otherwise.
25704a9016eSOhad Ben-Cohen  */
2587a186941SOhad Ben-Cohen int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
25904a9016eSOhad Ben-Cohen {
2607a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
261b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
2627a186941SOhad Ben-Cohen 	struct virtio_device *vdev = &rvdev->vdev;
26304a9016eSOhad Ben-Cohen 	int ret;
26404a9016eSOhad Ben-Cohen 
2657a186941SOhad Ben-Cohen 	vdev->id.device	= id,
2667a186941SOhad Ben-Cohen 	vdev->config = &rproc_virtio_config_ops,
2677a186941SOhad Ben-Cohen 	vdev->dev.parent = dev;
2687a186941SOhad Ben-Cohen 	vdev->dev.release = rproc_vdev_release;
26904a9016eSOhad Ben-Cohen 
27004a9016eSOhad Ben-Cohen 	/*
27104a9016eSOhad Ben-Cohen 	 * We're indirectly making a non-temporary copy of the rproc pointer
27204a9016eSOhad Ben-Cohen 	 * here, because drivers probed with this vdev will indirectly
27304a9016eSOhad Ben-Cohen 	 * access the wrapping rproc.
27404a9016eSOhad Ben-Cohen 	 *
27504a9016eSOhad Ben-Cohen 	 * Therefore we must increment the rproc refcount here, and decrement
27604a9016eSOhad Ben-Cohen 	 * it _only_ when the vdev is released.
27704a9016eSOhad Ben-Cohen 	 */
2787183a2a7SOhad Ben-Cohen 	get_device(&rproc->dev);
27904a9016eSOhad Ben-Cohen 
2807a186941SOhad Ben-Cohen 	ret = register_virtio_device(vdev);
28104a9016eSOhad Ben-Cohen 	if (ret) {
2827183a2a7SOhad Ben-Cohen 		put_device(&rproc->dev);
28304a9016eSOhad Ben-Cohen 		dev_err(dev, "failed to register vdev: %d\n", ret);
2847a186941SOhad Ben-Cohen 		goto out;
28504a9016eSOhad Ben-Cohen 	}
28604a9016eSOhad Ben-Cohen 
2877a186941SOhad Ben-Cohen 	dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
2887a186941SOhad Ben-Cohen 
2897a186941SOhad Ben-Cohen out:
29004a9016eSOhad Ben-Cohen 	return ret;
29104a9016eSOhad Ben-Cohen }
29204a9016eSOhad Ben-Cohen 
29304a9016eSOhad Ben-Cohen /**
2947a186941SOhad Ben-Cohen  * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
2957a186941SOhad Ben-Cohen  * @rvdev: the remote vdev
29604a9016eSOhad Ben-Cohen  *
2977a186941SOhad Ben-Cohen  * This function unregisters an existing virtio device.
29804a9016eSOhad Ben-Cohen  */
2997a186941SOhad Ben-Cohen void rproc_remove_virtio_dev(struct rproc_vdev *rvdev)
30004a9016eSOhad Ben-Cohen {
30104a9016eSOhad Ben-Cohen 	unregister_virtio_device(&rvdev->vdev);
30204a9016eSOhad Ben-Cohen }
303