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 /* 17704a9016eSOhad Ben-Cohen * We don't support yet real virtio status semantics. 17804a9016eSOhad Ben-Cohen * 1797a186941SOhad Ben-Cohen * The plan is to provide this via the VDEV resource entry 18004a9016eSOhad Ben-Cohen * which is part of the firmware: this way the remote processor 18104a9016eSOhad Ben-Cohen * will be able to access the status values as set by us. 18204a9016eSOhad Ben-Cohen */ 18304a9016eSOhad Ben-Cohen static u8 rproc_virtio_get_status(struct virtio_device *vdev) 18404a9016eSOhad Ben-Cohen { 18504a9016eSOhad Ben-Cohen return 0; 18604a9016eSOhad Ben-Cohen } 18704a9016eSOhad Ben-Cohen 18804a9016eSOhad Ben-Cohen static void rproc_virtio_set_status(struct virtio_device *vdev, u8 status) 18904a9016eSOhad Ben-Cohen { 1907a186941SOhad Ben-Cohen dev_dbg(&vdev->dev, "status: %d\n", status); 19104a9016eSOhad Ben-Cohen } 19204a9016eSOhad Ben-Cohen 19304a9016eSOhad Ben-Cohen static void rproc_virtio_reset(struct virtio_device *vdev) 19404a9016eSOhad Ben-Cohen { 19504a9016eSOhad Ben-Cohen dev_dbg(&vdev->dev, "reset !\n"); 19604a9016eSOhad Ben-Cohen } 19704a9016eSOhad Ben-Cohen 19804a9016eSOhad Ben-Cohen /* provide the vdev features as retrieved from the firmware */ 19904a9016eSOhad Ben-Cohen static u32 rproc_virtio_get_features(struct virtio_device *vdev) 20004a9016eSOhad Ben-Cohen { 2017a186941SOhad Ben-Cohen struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 20204a9016eSOhad Ben-Cohen 2037a186941SOhad Ben-Cohen return rvdev->dfeatures; 20404a9016eSOhad Ben-Cohen } 20504a9016eSOhad Ben-Cohen 20604a9016eSOhad Ben-Cohen static void rproc_virtio_finalize_features(struct virtio_device *vdev) 20704a9016eSOhad Ben-Cohen { 2087a186941SOhad Ben-Cohen struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 20904a9016eSOhad Ben-Cohen 21004a9016eSOhad Ben-Cohen /* Give virtio_ring a chance to accept features */ 21104a9016eSOhad Ben-Cohen vring_transport_features(vdev); 21204a9016eSOhad Ben-Cohen 21304a9016eSOhad Ben-Cohen /* 21404a9016eSOhad Ben-Cohen * Remember the finalized features of our vdev, and provide it 21504a9016eSOhad Ben-Cohen * to the remote processor once it is powered on. 21604a9016eSOhad Ben-Cohen * 21704a9016eSOhad Ben-Cohen * Similarly to the status field, we don't expose yet the negotiated 21804a9016eSOhad Ben-Cohen * features to the remote processors at this point. This will be 21904a9016eSOhad Ben-Cohen * fixed as part of a small resource table overhaul and then an 22004a9016eSOhad Ben-Cohen * extension of the virtio resource entries. 22104a9016eSOhad Ben-Cohen */ 2227a186941SOhad Ben-Cohen rvdev->gfeatures = vdev->features[0]; 22304a9016eSOhad Ben-Cohen } 22404a9016eSOhad Ben-Cohen 225*93503932SStephen Hemminger static const struct virtio_config_ops rproc_virtio_config_ops = { 22604a9016eSOhad Ben-Cohen .get_features = rproc_virtio_get_features, 22704a9016eSOhad Ben-Cohen .finalize_features = rproc_virtio_finalize_features, 22804a9016eSOhad Ben-Cohen .find_vqs = rproc_virtio_find_vqs, 22904a9016eSOhad Ben-Cohen .del_vqs = rproc_virtio_del_vqs, 23004a9016eSOhad Ben-Cohen .reset = rproc_virtio_reset, 23104a9016eSOhad Ben-Cohen .set_status = rproc_virtio_set_status, 23204a9016eSOhad Ben-Cohen .get_status = rproc_virtio_get_status, 23304a9016eSOhad Ben-Cohen }; 23404a9016eSOhad Ben-Cohen 23504a9016eSOhad Ben-Cohen /* 23604a9016eSOhad Ben-Cohen * This function is called whenever vdev is released, and is responsible 2377183a2a7SOhad Ben-Cohen * to decrement the remote processor's refcount which was taken when vdev was 23804a9016eSOhad Ben-Cohen * added. 23904a9016eSOhad Ben-Cohen * 24004a9016eSOhad Ben-Cohen * Never call this function directly; it will be called by the driver 24104a9016eSOhad Ben-Cohen * core when needed. 24204a9016eSOhad Ben-Cohen */ 24304a9016eSOhad Ben-Cohen static void rproc_vdev_release(struct device *dev) 24404a9016eSOhad Ben-Cohen { 24504a9016eSOhad Ben-Cohen struct virtio_device *vdev = dev_to_virtio(dev); 2466db20ea8SOhad Ben-Cohen struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 24704a9016eSOhad Ben-Cohen struct rproc *rproc = vdev_to_rproc(vdev); 24804a9016eSOhad Ben-Cohen 2496db20ea8SOhad Ben-Cohen list_del(&rvdev->node); 2506db20ea8SOhad Ben-Cohen kfree(rvdev); 2516db20ea8SOhad Ben-Cohen 2527183a2a7SOhad Ben-Cohen put_device(&rproc->dev); 25304a9016eSOhad Ben-Cohen } 25404a9016eSOhad Ben-Cohen 25504a9016eSOhad Ben-Cohen /** 2567a186941SOhad Ben-Cohen * rproc_add_virtio_dev() - register an rproc-induced virtio device 2577a186941SOhad Ben-Cohen * @rvdev: the remote vdev 25804a9016eSOhad Ben-Cohen * 2597a186941SOhad Ben-Cohen * This function registers a virtio device. This vdev's partent is 2607a186941SOhad Ben-Cohen * the rproc device. 26104a9016eSOhad Ben-Cohen * 2627a186941SOhad Ben-Cohen * Returns 0 on success or an appropriate error value otherwise. 26304a9016eSOhad Ben-Cohen */ 2647a186941SOhad Ben-Cohen int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id) 26504a9016eSOhad Ben-Cohen { 2667a186941SOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 267b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 2687a186941SOhad Ben-Cohen struct virtio_device *vdev = &rvdev->vdev; 26904a9016eSOhad Ben-Cohen int ret; 27004a9016eSOhad Ben-Cohen 2717a186941SOhad Ben-Cohen vdev->id.device = id, 2727a186941SOhad Ben-Cohen vdev->config = &rproc_virtio_config_ops, 2737a186941SOhad Ben-Cohen vdev->dev.parent = dev; 2747a186941SOhad Ben-Cohen vdev->dev.release = rproc_vdev_release; 27504a9016eSOhad Ben-Cohen 27604a9016eSOhad Ben-Cohen /* 27704a9016eSOhad Ben-Cohen * We're indirectly making a non-temporary copy of the rproc pointer 27804a9016eSOhad Ben-Cohen * here, because drivers probed with this vdev will indirectly 27904a9016eSOhad Ben-Cohen * access the wrapping rproc. 28004a9016eSOhad Ben-Cohen * 28104a9016eSOhad Ben-Cohen * Therefore we must increment the rproc refcount here, and decrement 28204a9016eSOhad Ben-Cohen * it _only_ when the vdev is released. 28304a9016eSOhad Ben-Cohen */ 2847183a2a7SOhad Ben-Cohen get_device(&rproc->dev); 28504a9016eSOhad Ben-Cohen 2867a186941SOhad Ben-Cohen ret = register_virtio_device(vdev); 28704a9016eSOhad Ben-Cohen if (ret) { 2887183a2a7SOhad Ben-Cohen put_device(&rproc->dev); 28904a9016eSOhad Ben-Cohen dev_err(dev, "failed to register vdev: %d\n", ret); 2907a186941SOhad Ben-Cohen goto out; 29104a9016eSOhad Ben-Cohen } 29204a9016eSOhad Ben-Cohen 2937a186941SOhad Ben-Cohen dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id); 2947a186941SOhad Ben-Cohen 2957a186941SOhad Ben-Cohen out: 29604a9016eSOhad Ben-Cohen return ret; 29704a9016eSOhad Ben-Cohen } 29804a9016eSOhad Ben-Cohen 29904a9016eSOhad Ben-Cohen /** 3007a186941SOhad Ben-Cohen * rproc_remove_virtio_dev() - remove an rproc-induced virtio device 3017a186941SOhad Ben-Cohen * @rvdev: the remote vdev 30204a9016eSOhad Ben-Cohen * 3037a186941SOhad Ben-Cohen * This function unregisters an existing virtio device. 30404a9016eSOhad Ben-Cohen */ 3057a186941SOhad Ben-Cohen void rproc_remove_virtio_dev(struct rproc_vdev *rvdev) 30604a9016eSOhad Ben-Cohen { 30704a9016eSOhad Ben-Cohen unregister_virtio_device(&rvdev->vdev); 30804a9016eSOhad Ben-Cohen } 309