xref: /linux/drivers/remoteproc/remoteproc_virtio.c (revision b5ab5e24e960b9f780a4cc96815cfd4b0d412720)
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 
39*b5ab5e24SOhad 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 
60*b5ab5e24SOhad 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);
77*b5ab5e24SOhad 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 
876db20ea8SOhad Ben-Cohen 	ret = rproc_alloc_vring(rvdev, id);
886db20ea8SOhad Ben-Cohen 	if (ret)
896db20ea8SOhad Ben-Cohen 		return ERR_PTR(ret);
9004a9016eSOhad Ben-Cohen 
916db20ea8SOhad Ben-Cohen 	rvring = &rvdev->vring[id];
927a186941SOhad Ben-Cohen 	addr = rvring->va;
937a186941SOhad Ben-Cohen 	len = rvring->len;
9404a9016eSOhad Ben-Cohen 
957a186941SOhad Ben-Cohen 	/* zero vring */
967a186941SOhad Ben-Cohen 	size = vring_size(len, rvring->align);
977a186941SOhad Ben-Cohen 	memset(addr, 0, size);
987a186941SOhad Ben-Cohen 
99*b5ab5e24SOhad Ben-Cohen 	dev_dbg(dev, "vring%d: va %p qsz %d notifyid %d\n",
1007a186941SOhad Ben-Cohen 					id, addr, len, rvring->notifyid);
10104a9016eSOhad Ben-Cohen 
10204a9016eSOhad Ben-Cohen 	/*
10304a9016eSOhad Ben-Cohen 	 * Create the new vq, and tell virtio we're not interested in
10404a9016eSOhad Ben-Cohen 	 * the 'weak' smp barriers, since we're talking with a real device.
10504a9016eSOhad Ben-Cohen 	 */
10663140e0eSOhad Ben-Cohen 	vq = vring_new_virtqueue(len, rvring->align, vdev, false, addr,
10704a9016eSOhad Ben-Cohen 					rproc_virtio_notify, callback, name);
10804a9016eSOhad Ben-Cohen 	if (!vq) {
109*b5ab5e24SOhad Ben-Cohen 		dev_err(dev, "vring_new_virtqueue %s failed\n", name);
1106db20ea8SOhad Ben-Cohen 		rproc_free_vring(rvring);
1117a186941SOhad Ben-Cohen 		return ERR_PTR(-ENOMEM);
11204a9016eSOhad Ben-Cohen 	}
11304a9016eSOhad Ben-Cohen 
1147a186941SOhad Ben-Cohen 	rvring->vq = vq;
1157a186941SOhad Ben-Cohen 	vq->priv = rvring;
11604a9016eSOhad Ben-Cohen 
11704a9016eSOhad Ben-Cohen 	return vq;
11804a9016eSOhad Ben-Cohen }
11904a9016eSOhad Ben-Cohen 
12004a9016eSOhad Ben-Cohen static void rproc_virtio_del_vqs(struct virtio_device *vdev)
12104a9016eSOhad Ben-Cohen {
12204a9016eSOhad Ben-Cohen 	struct virtqueue *vq, *n;
12304a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
1247a186941SOhad Ben-Cohen 	struct rproc_vring *rvring;
12504a9016eSOhad Ben-Cohen 
12641a6ee09SOhad Ben-Cohen 	/* power down the remote processor before deleting vqs */
12741a6ee09SOhad Ben-Cohen 	rproc_shutdown(rproc);
12841a6ee09SOhad Ben-Cohen 
12904a9016eSOhad Ben-Cohen 	list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
1307a186941SOhad Ben-Cohen 		rvring = vq->priv;
1317a186941SOhad Ben-Cohen 		rvring->vq = NULL;
13204a9016eSOhad Ben-Cohen 		vring_del_virtqueue(vq);
1336db20ea8SOhad Ben-Cohen 		rproc_free_vring(rvring);
13404a9016eSOhad Ben-Cohen 	}
13504a9016eSOhad Ben-Cohen }
13604a9016eSOhad Ben-Cohen 
13704a9016eSOhad Ben-Cohen static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
13804a9016eSOhad Ben-Cohen 		       struct virtqueue *vqs[],
13904a9016eSOhad Ben-Cohen 		       vq_callback_t *callbacks[],
14004a9016eSOhad Ben-Cohen 		       const char *names[])
14104a9016eSOhad Ben-Cohen {
14204a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
14304a9016eSOhad Ben-Cohen 	int i, ret;
14404a9016eSOhad Ben-Cohen 
14504a9016eSOhad Ben-Cohen 	for (i = 0; i < nvqs; ++i) {
14604a9016eSOhad Ben-Cohen 		vqs[i] = rp_find_vq(vdev, i, callbacks[i], names[i]);
14704a9016eSOhad Ben-Cohen 		if (IS_ERR(vqs[i])) {
14804a9016eSOhad Ben-Cohen 			ret = PTR_ERR(vqs[i]);
14904a9016eSOhad Ben-Cohen 			goto error;
15004a9016eSOhad Ben-Cohen 		}
15104a9016eSOhad Ben-Cohen 	}
15204a9016eSOhad Ben-Cohen 
15341a6ee09SOhad Ben-Cohen 	/* now that the vqs are all set, boot the remote processor */
15441a6ee09SOhad Ben-Cohen 	ret = rproc_boot(rproc);
15541a6ee09SOhad Ben-Cohen 	if (ret) {
156*b5ab5e24SOhad Ben-Cohen 		dev_err(&rproc->dev, "rproc_boot() failed %d\n", ret);
15741a6ee09SOhad Ben-Cohen 		goto error;
15841a6ee09SOhad Ben-Cohen 	}
15941a6ee09SOhad Ben-Cohen 
16004a9016eSOhad Ben-Cohen 	return 0;
16104a9016eSOhad Ben-Cohen 
16204a9016eSOhad Ben-Cohen error:
16304a9016eSOhad Ben-Cohen 	rproc_virtio_del_vqs(vdev);
16404a9016eSOhad Ben-Cohen 	return ret;
16504a9016eSOhad Ben-Cohen }
16604a9016eSOhad Ben-Cohen 
16704a9016eSOhad Ben-Cohen /*
16804a9016eSOhad Ben-Cohen  * We don't support yet real virtio status semantics.
16904a9016eSOhad Ben-Cohen  *
1707a186941SOhad Ben-Cohen  * The plan is to provide this via the VDEV resource entry
17104a9016eSOhad Ben-Cohen  * which is part of the firmware: this way the remote processor
17204a9016eSOhad Ben-Cohen  * will be able to access the status values as set by us.
17304a9016eSOhad Ben-Cohen  */
17404a9016eSOhad Ben-Cohen static u8 rproc_virtio_get_status(struct virtio_device *vdev)
17504a9016eSOhad Ben-Cohen {
17604a9016eSOhad Ben-Cohen 	return 0;
17704a9016eSOhad Ben-Cohen }
17804a9016eSOhad Ben-Cohen 
17904a9016eSOhad Ben-Cohen static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
18004a9016eSOhad Ben-Cohen {
1817a186941SOhad Ben-Cohen 	dev_dbg(&vdev->dev, "status: %d\n", status);
18204a9016eSOhad Ben-Cohen }
18304a9016eSOhad Ben-Cohen 
18404a9016eSOhad Ben-Cohen static void rproc_virtio_reset(struct virtio_device *vdev)
18504a9016eSOhad Ben-Cohen {
18604a9016eSOhad Ben-Cohen 	dev_dbg(&vdev->dev, "reset !\n");
18704a9016eSOhad Ben-Cohen }
18804a9016eSOhad Ben-Cohen 
18904a9016eSOhad Ben-Cohen /* provide the vdev features as retrieved from the firmware */
19004a9016eSOhad Ben-Cohen static u32 rproc_virtio_get_features(struct virtio_device *vdev)
19104a9016eSOhad Ben-Cohen {
1927a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
19304a9016eSOhad Ben-Cohen 
1947a186941SOhad Ben-Cohen 	return rvdev->dfeatures;
19504a9016eSOhad Ben-Cohen }
19604a9016eSOhad Ben-Cohen 
19704a9016eSOhad Ben-Cohen static void rproc_virtio_finalize_features(struct virtio_device *vdev)
19804a9016eSOhad Ben-Cohen {
1997a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
20004a9016eSOhad Ben-Cohen 
20104a9016eSOhad Ben-Cohen 	/* Give virtio_ring a chance to accept features */
20204a9016eSOhad Ben-Cohen 	vring_transport_features(vdev);
20304a9016eSOhad Ben-Cohen 
20404a9016eSOhad Ben-Cohen 	/*
20504a9016eSOhad Ben-Cohen 	 * Remember the finalized features of our vdev, and provide it
20604a9016eSOhad Ben-Cohen 	 * to the remote processor once it is powered on.
20704a9016eSOhad Ben-Cohen 	 *
20804a9016eSOhad Ben-Cohen 	 * Similarly to the status field, we don't expose yet the negotiated
20904a9016eSOhad Ben-Cohen 	 * features to the remote processors at this point. This will be
21004a9016eSOhad Ben-Cohen 	 * fixed as part of a small resource table overhaul and then an
21104a9016eSOhad Ben-Cohen 	 * extension of the virtio resource entries.
21204a9016eSOhad Ben-Cohen 	 */
2137a186941SOhad Ben-Cohen 	rvdev->gfeatures = vdev->features[0];
21404a9016eSOhad Ben-Cohen }
21504a9016eSOhad Ben-Cohen 
21604a9016eSOhad Ben-Cohen static struct virtio_config_ops rproc_virtio_config_ops = {
21704a9016eSOhad Ben-Cohen 	.get_features	= rproc_virtio_get_features,
21804a9016eSOhad Ben-Cohen 	.finalize_features = rproc_virtio_finalize_features,
21904a9016eSOhad Ben-Cohen 	.find_vqs	= rproc_virtio_find_vqs,
22004a9016eSOhad Ben-Cohen 	.del_vqs	= rproc_virtio_del_vqs,
22104a9016eSOhad Ben-Cohen 	.reset		= rproc_virtio_reset,
22204a9016eSOhad Ben-Cohen 	.set_status	= rproc_virtio_set_status,
22304a9016eSOhad Ben-Cohen 	.get_status	= rproc_virtio_get_status,
22404a9016eSOhad Ben-Cohen };
22504a9016eSOhad Ben-Cohen 
22604a9016eSOhad Ben-Cohen /*
22704a9016eSOhad Ben-Cohen  * This function is called whenever vdev is released, and is responsible
22804a9016eSOhad Ben-Cohen  * to decrement the remote processor's refcount taken when vdev was
22904a9016eSOhad Ben-Cohen  * added.
23004a9016eSOhad Ben-Cohen  *
23104a9016eSOhad Ben-Cohen  * Never call this function directly; it will be called by the driver
23204a9016eSOhad Ben-Cohen  * core when needed.
23304a9016eSOhad Ben-Cohen  */
23404a9016eSOhad Ben-Cohen static void rproc_vdev_release(struct device *dev)
23504a9016eSOhad Ben-Cohen {
23604a9016eSOhad Ben-Cohen 	struct virtio_device *vdev = dev_to_virtio(dev);
2376db20ea8SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
23804a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
23904a9016eSOhad Ben-Cohen 
2406db20ea8SOhad Ben-Cohen 	list_del(&rvdev->node);
2416db20ea8SOhad Ben-Cohen 	kfree(rvdev);
2426db20ea8SOhad Ben-Cohen 
24304a9016eSOhad Ben-Cohen 	kref_put(&rproc->refcount, rproc_release);
24404a9016eSOhad Ben-Cohen }
24504a9016eSOhad Ben-Cohen 
24604a9016eSOhad Ben-Cohen /**
2477a186941SOhad Ben-Cohen  * rproc_add_virtio_dev() - register an rproc-induced virtio device
2487a186941SOhad Ben-Cohen  * @rvdev: the remote vdev
24904a9016eSOhad Ben-Cohen  *
2507a186941SOhad Ben-Cohen  * This function registers a virtio device. This vdev's partent is
2517a186941SOhad Ben-Cohen  * the rproc device.
25204a9016eSOhad Ben-Cohen  *
2537a186941SOhad Ben-Cohen  * Returns 0 on success or an appropriate error value otherwise.
25404a9016eSOhad Ben-Cohen  */
2557a186941SOhad Ben-Cohen int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
25604a9016eSOhad Ben-Cohen {
2577a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
258*b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
2597a186941SOhad Ben-Cohen 	struct virtio_device *vdev = &rvdev->vdev;
26004a9016eSOhad Ben-Cohen 	int ret;
26104a9016eSOhad Ben-Cohen 
2627a186941SOhad Ben-Cohen 	vdev->id.device	= id,
2637a186941SOhad Ben-Cohen 	vdev->config = &rproc_virtio_config_ops,
2647a186941SOhad Ben-Cohen 	vdev->dev.parent = dev;
2657a186941SOhad Ben-Cohen 	vdev->dev.release = rproc_vdev_release;
26604a9016eSOhad Ben-Cohen 
26704a9016eSOhad Ben-Cohen 	/*
26804a9016eSOhad Ben-Cohen 	 * We're indirectly making a non-temporary copy of the rproc pointer
26904a9016eSOhad Ben-Cohen 	 * here, because drivers probed with this vdev will indirectly
27004a9016eSOhad Ben-Cohen 	 * access the wrapping rproc.
27104a9016eSOhad Ben-Cohen 	 *
27204a9016eSOhad Ben-Cohen 	 * Therefore we must increment the rproc refcount here, and decrement
27304a9016eSOhad Ben-Cohen 	 * it _only_ when the vdev is released.
27404a9016eSOhad Ben-Cohen 	 */
27504a9016eSOhad Ben-Cohen 	kref_get(&rproc->refcount);
27604a9016eSOhad Ben-Cohen 
2777a186941SOhad Ben-Cohen 	ret = register_virtio_device(vdev);
27804a9016eSOhad Ben-Cohen 	if (ret) {
27904a9016eSOhad Ben-Cohen 		kref_put(&rproc->refcount, rproc_release);
28004a9016eSOhad Ben-Cohen 		dev_err(dev, "failed to register vdev: %d\n", ret);
2817a186941SOhad Ben-Cohen 		goto out;
28204a9016eSOhad Ben-Cohen 	}
28304a9016eSOhad Ben-Cohen 
2847a186941SOhad Ben-Cohen 	dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
2857a186941SOhad Ben-Cohen 
2867a186941SOhad Ben-Cohen out:
28704a9016eSOhad Ben-Cohen 	return ret;
28804a9016eSOhad Ben-Cohen }
28904a9016eSOhad Ben-Cohen 
29004a9016eSOhad Ben-Cohen /**
2917a186941SOhad Ben-Cohen  * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
2927a186941SOhad Ben-Cohen  * @rvdev: the remote vdev
29304a9016eSOhad Ben-Cohen  *
2947a186941SOhad Ben-Cohen  * This function unregisters an existing virtio device.
29504a9016eSOhad Ben-Cohen  */
2967a186941SOhad Ben-Cohen void rproc_remove_virtio_dev(struct rproc_vdev *rvdev)
29704a9016eSOhad Ben-Cohen {
29804a9016eSOhad Ben-Cohen 	unregister_virtio_device(&rvdev->vdev);
29904a9016eSOhad Ben-Cohen }
300