xref: /linux/drivers/remoteproc/remoteproc_virtio.c (revision 92b38f851470f8d8ea7ed638d546f83b5268bc12)
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 
876457f126SMichael S. Tsirkin 	if (!name)
886457f126SMichael S. Tsirkin 		return NULL;
896457f126SMichael 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 
123dab55bbaSOhad Ben-Cohen static void __rproc_virtio_del_vqs(struct virtio_device *vdev)
12404a9016eSOhad Ben-Cohen {
12504a9016eSOhad Ben-Cohen 	struct virtqueue *vq, *n;
1267a186941SOhad Ben-Cohen 	struct rproc_vring *rvring;
12704a9016eSOhad Ben-Cohen 
12804a9016eSOhad Ben-Cohen 	list_for_each_entry_safe(vq, n, &vdev->vqs, list) {
1297a186941SOhad Ben-Cohen 		rvring = vq->priv;
1307a186941SOhad Ben-Cohen 		rvring->vq = NULL;
13104a9016eSOhad Ben-Cohen 		vring_del_virtqueue(vq);
1326db20ea8SOhad Ben-Cohen 		rproc_free_vring(rvring);
13304a9016eSOhad Ben-Cohen 	}
13404a9016eSOhad Ben-Cohen }
13504a9016eSOhad Ben-Cohen 
136dab55bbaSOhad Ben-Cohen static void rproc_virtio_del_vqs(struct virtio_device *vdev)
137dab55bbaSOhad Ben-Cohen {
138dab55bbaSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
139dab55bbaSOhad Ben-Cohen 
140dab55bbaSOhad Ben-Cohen 	/* power down the remote processor before deleting vqs */
141dab55bbaSOhad Ben-Cohen 	rproc_shutdown(rproc);
142dab55bbaSOhad Ben-Cohen 
143dab55bbaSOhad Ben-Cohen 	__rproc_virtio_del_vqs(vdev);
144dab55bbaSOhad Ben-Cohen }
145dab55bbaSOhad Ben-Cohen 
14604a9016eSOhad Ben-Cohen static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned nvqs,
14704a9016eSOhad Ben-Cohen 		       struct virtqueue *vqs[],
14804a9016eSOhad Ben-Cohen 		       vq_callback_t *callbacks[],
14904a9016eSOhad Ben-Cohen 		       const char *names[])
15004a9016eSOhad Ben-Cohen {
15104a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
15204a9016eSOhad Ben-Cohen 	int i, ret;
15304a9016eSOhad Ben-Cohen 
15404a9016eSOhad Ben-Cohen 	for (i = 0; i < nvqs; ++i) {
15504a9016eSOhad Ben-Cohen 		vqs[i] = rp_find_vq(vdev, i, callbacks[i], names[i]);
15604a9016eSOhad Ben-Cohen 		if (IS_ERR(vqs[i])) {
15704a9016eSOhad Ben-Cohen 			ret = PTR_ERR(vqs[i]);
15804a9016eSOhad Ben-Cohen 			goto error;
15904a9016eSOhad Ben-Cohen 		}
16004a9016eSOhad Ben-Cohen 	}
16104a9016eSOhad Ben-Cohen 
16241a6ee09SOhad Ben-Cohen 	/* now that the vqs are all set, boot the remote processor */
16341a6ee09SOhad Ben-Cohen 	ret = rproc_boot(rproc);
16441a6ee09SOhad Ben-Cohen 	if (ret) {
165b5ab5e24SOhad Ben-Cohen 		dev_err(&rproc->dev, "rproc_boot() failed %d\n", ret);
16641a6ee09SOhad Ben-Cohen 		goto error;
16741a6ee09SOhad Ben-Cohen 	}
16841a6ee09SOhad Ben-Cohen 
16904a9016eSOhad Ben-Cohen 	return 0;
17004a9016eSOhad Ben-Cohen 
17104a9016eSOhad Ben-Cohen error:
172dab55bbaSOhad Ben-Cohen 	__rproc_virtio_del_vqs(vdev);
17304a9016eSOhad Ben-Cohen 	return ret;
17404a9016eSOhad Ben-Cohen }
17504a9016eSOhad Ben-Cohen 
17604a9016eSOhad Ben-Cohen static u8 rproc_virtio_get_status(struct virtio_device *vdev)
17704a9016eSOhad Ben-Cohen {
178*92b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
179*92b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
180*92b38f85SSjur Brændeland 
181*92b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
182*92b38f85SSjur Brændeland 
183*92b38f85SSjur Brændeland 	return rsc->status;
18404a9016eSOhad Ben-Cohen }
18504a9016eSOhad Ben-Cohen 
18604a9016eSOhad Ben-Cohen static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status)
18704a9016eSOhad Ben-Cohen {
188*92b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
189*92b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
190*92b38f85SSjur Brændeland 
191*92b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
192*92b38f85SSjur Brændeland 
193*92b38f85SSjur Brændeland 	rsc->status = status;
1947a186941SOhad Ben-Cohen 	dev_dbg(&vdev->dev, "status: %d\n", status);
19504a9016eSOhad Ben-Cohen }
19604a9016eSOhad Ben-Cohen 
19704a9016eSOhad Ben-Cohen static void rproc_virtio_reset(struct virtio_device *vdev)
19804a9016eSOhad Ben-Cohen {
199*92b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
200*92b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
201*92b38f85SSjur Brændeland 
202*92b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
203*92b38f85SSjur Brændeland 
204*92b38f85SSjur Brændeland 	rsc->status = 0;
20504a9016eSOhad Ben-Cohen 	dev_dbg(&vdev->dev, "reset !\n");
20604a9016eSOhad Ben-Cohen }
20704a9016eSOhad Ben-Cohen 
20804a9016eSOhad Ben-Cohen /* provide the vdev features as retrieved from the firmware */
20904a9016eSOhad Ben-Cohen static u32 rproc_virtio_get_features(struct virtio_device *vdev)
21004a9016eSOhad Ben-Cohen {
2117a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
212*92b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
21304a9016eSOhad Ben-Cohen 
214*92b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
215*92b38f85SSjur Brændeland 
216*92b38f85SSjur Brændeland 	return rsc->dfeatures;
21704a9016eSOhad Ben-Cohen }
21804a9016eSOhad Ben-Cohen 
21904a9016eSOhad Ben-Cohen static void rproc_virtio_finalize_features(struct virtio_device *vdev)
22004a9016eSOhad Ben-Cohen {
2217a186941SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
222*92b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
223*92b38f85SSjur Brændeland 
224*92b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
22504a9016eSOhad Ben-Cohen 
22604a9016eSOhad Ben-Cohen 	/* Give virtio_ring a chance to accept features */
22704a9016eSOhad Ben-Cohen 	vring_transport_features(vdev);
22804a9016eSOhad Ben-Cohen 
22904a9016eSOhad Ben-Cohen 	/*
23004a9016eSOhad Ben-Cohen 	 * Remember the finalized features of our vdev, and provide it
23104a9016eSOhad Ben-Cohen 	 * to the remote processor once it is powered on.
23204a9016eSOhad Ben-Cohen 	 */
233*92b38f85SSjur Brændeland 	rsc->gfeatures = vdev->features[0];
234*92b38f85SSjur Brændeland }
235*92b38f85SSjur Brændeland 
236*92b38f85SSjur Brændeland static void rproc_virtio_get(struct virtio_device *vdev, unsigned offset,
237*92b38f85SSjur Brændeland 							void *buf, unsigned len)
238*92b38f85SSjur Brændeland {
239*92b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
240*92b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
241*92b38f85SSjur Brændeland 	void *cfg;
242*92b38f85SSjur Brændeland 
243*92b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
244*92b38f85SSjur Brændeland 	cfg = &rsc->vring[rsc->num_of_vrings];
245*92b38f85SSjur Brændeland 
246*92b38f85SSjur Brændeland 	if (offset + len > rsc->config_len || offset + len < len) {
247*92b38f85SSjur Brændeland 		dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n");
248*92b38f85SSjur Brændeland 		return;
249*92b38f85SSjur Brændeland 	}
250*92b38f85SSjur Brændeland 
251*92b38f85SSjur Brændeland 	memcpy(buf, cfg + offset, len);
252*92b38f85SSjur Brændeland }
253*92b38f85SSjur Brændeland 
254*92b38f85SSjur Brændeland static void rproc_virtio_set(struct virtio_device *vdev, unsigned offset,
255*92b38f85SSjur Brændeland 		      const void *buf, unsigned len)
256*92b38f85SSjur Brændeland {
257*92b38f85SSjur Brændeland 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
258*92b38f85SSjur Brændeland 	struct fw_rsc_vdev *rsc;
259*92b38f85SSjur Brændeland 	void *cfg;
260*92b38f85SSjur Brændeland 
261*92b38f85SSjur Brændeland 	rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset;
262*92b38f85SSjur Brændeland 	cfg = &rsc->vring[rsc->num_of_vrings];
263*92b38f85SSjur Brændeland 
264*92b38f85SSjur Brændeland 	if (offset + len > rsc->config_len || offset + len < len) {
265*92b38f85SSjur Brændeland 		dev_err(&vdev->dev, "rproc_virtio_set: access out of bounds\n");
266*92b38f85SSjur Brændeland 		return;
267*92b38f85SSjur Brændeland 	}
268*92b38f85SSjur Brændeland 
269*92b38f85SSjur Brændeland 	memcpy(cfg + offset, buf, len);
27004a9016eSOhad Ben-Cohen }
27104a9016eSOhad Ben-Cohen 
27293503932SStephen Hemminger static const struct virtio_config_ops rproc_virtio_config_ops = {
27304a9016eSOhad Ben-Cohen 	.get_features	= rproc_virtio_get_features,
27404a9016eSOhad Ben-Cohen 	.finalize_features = rproc_virtio_finalize_features,
27504a9016eSOhad Ben-Cohen 	.find_vqs	= rproc_virtio_find_vqs,
27604a9016eSOhad Ben-Cohen 	.del_vqs	= rproc_virtio_del_vqs,
27704a9016eSOhad Ben-Cohen 	.reset		= rproc_virtio_reset,
27804a9016eSOhad Ben-Cohen 	.set_status	= rproc_virtio_set_status,
27904a9016eSOhad Ben-Cohen 	.get_status	= rproc_virtio_get_status,
280*92b38f85SSjur Brændeland 	.get		= rproc_virtio_get,
281*92b38f85SSjur Brændeland 	.set		= rproc_virtio_set,
28204a9016eSOhad Ben-Cohen };
28304a9016eSOhad Ben-Cohen 
28404a9016eSOhad Ben-Cohen /*
28504a9016eSOhad Ben-Cohen  * This function is called whenever vdev is released, and is responsible
2867183a2a7SOhad Ben-Cohen  * to decrement the remote processor's refcount which was taken when vdev was
28704a9016eSOhad Ben-Cohen  * added.
28804a9016eSOhad Ben-Cohen  *
28904a9016eSOhad Ben-Cohen  * Never call this function directly; it will be called by the driver
29004a9016eSOhad Ben-Cohen  * core when needed.
29104a9016eSOhad Ben-Cohen  */
29204a9016eSOhad Ben-Cohen static void rproc_vdev_release(struct device *dev)
29304a9016eSOhad Ben-Cohen {
29404a9016eSOhad Ben-Cohen 	struct virtio_device *vdev = dev_to_virtio(dev);
2956db20ea8SOhad Ben-Cohen 	struct rproc_vdev *rvdev = vdev_to_rvdev(vdev);
29604a9016eSOhad Ben-Cohen 	struct rproc *rproc = vdev_to_rproc(vdev);
29704a9016eSOhad Ben-Cohen 
2986db20ea8SOhad Ben-Cohen 	list_del(&rvdev->node);
2996db20ea8SOhad Ben-Cohen 	kfree(rvdev);
3006db20ea8SOhad Ben-Cohen 
3017183a2a7SOhad Ben-Cohen 	put_device(&rproc->dev);
30204a9016eSOhad Ben-Cohen }
30304a9016eSOhad Ben-Cohen 
30404a9016eSOhad Ben-Cohen /**
3057a186941SOhad Ben-Cohen  * rproc_add_virtio_dev() - register an rproc-induced virtio device
3067a186941SOhad Ben-Cohen  * @rvdev: the remote vdev
30704a9016eSOhad Ben-Cohen  *
3087a186941SOhad Ben-Cohen  * This function registers a virtio device. This vdev's partent is
3097a186941SOhad Ben-Cohen  * the rproc device.
31004a9016eSOhad Ben-Cohen  *
3117a186941SOhad Ben-Cohen  * Returns 0 on success or an appropriate error value otherwise.
31204a9016eSOhad Ben-Cohen  */
3137a186941SOhad Ben-Cohen int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id)
31404a9016eSOhad Ben-Cohen {
3157a186941SOhad Ben-Cohen 	struct rproc *rproc = rvdev->rproc;
316b5ab5e24SOhad Ben-Cohen 	struct device *dev = &rproc->dev;
3177a186941SOhad Ben-Cohen 	struct virtio_device *vdev = &rvdev->vdev;
31804a9016eSOhad Ben-Cohen 	int ret;
31904a9016eSOhad Ben-Cohen 
3207a186941SOhad Ben-Cohen 	vdev->id.device	= id,
3217a186941SOhad Ben-Cohen 	vdev->config = &rproc_virtio_config_ops,
3227a186941SOhad Ben-Cohen 	vdev->dev.parent = dev;
3237a186941SOhad Ben-Cohen 	vdev->dev.release = rproc_vdev_release;
32404a9016eSOhad Ben-Cohen 
32504a9016eSOhad Ben-Cohen 	/*
32604a9016eSOhad Ben-Cohen 	 * We're indirectly making a non-temporary copy of the rproc pointer
32704a9016eSOhad Ben-Cohen 	 * here, because drivers probed with this vdev will indirectly
32804a9016eSOhad Ben-Cohen 	 * access the wrapping rproc.
32904a9016eSOhad Ben-Cohen 	 *
33004a9016eSOhad Ben-Cohen 	 * Therefore we must increment the rproc refcount here, and decrement
33104a9016eSOhad Ben-Cohen 	 * it _only_ when the vdev is released.
33204a9016eSOhad Ben-Cohen 	 */
3337183a2a7SOhad Ben-Cohen 	get_device(&rproc->dev);
33404a9016eSOhad Ben-Cohen 
3357a186941SOhad Ben-Cohen 	ret = register_virtio_device(vdev);
33604a9016eSOhad Ben-Cohen 	if (ret) {
3377183a2a7SOhad Ben-Cohen 		put_device(&rproc->dev);
33804a9016eSOhad Ben-Cohen 		dev_err(dev, "failed to register vdev: %d\n", ret);
3397a186941SOhad Ben-Cohen 		goto out;
34004a9016eSOhad Ben-Cohen 	}
34104a9016eSOhad Ben-Cohen 
3427a186941SOhad Ben-Cohen 	dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id);
3437a186941SOhad Ben-Cohen 
3447a186941SOhad Ben-Cohen out:
34504a9016eSOhad Ben-Cohen 	return ret;
34604a9016eSOhad Ben-Cohen }
34704a9016eSOhad Ben-Cohen 
34804a9016eSOhad Ben-Cohen /**
3497a186941SOhad Ben-Cohen  * rproc_remove_virtio_dev() - remove an rproc-induced virtio device
3507a186941SOhad Ben-Cohen  * @rvdev: the remote vdev
35104a9016eSOhad Ben-Cohen  *
3527a186941SOhad Ben-Cohen  * This function unregisters an existing virtio device.
35304a9016eSOhad Ben-Cohen  */
3547a186941SOhad Ben-Cohen void rproc_remove_virtio_dev(struct rproc_vdev *rvdev)
35504a9016eSOhad Ben-Cohen {
35604a9016eSOhad Ben-Cohen 	unregister_virtio_device(&rvdev->vdev);
35704a9016eSOhad Ben-Cohen }
358