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 20*086d0872SLoic Pallardy #include <linux/dma-mapping.h> 2104a9016eSOhad Ben-Cohen #include <linux/export.h> 22*086d0872SLoic Pallardy #include <linux/of_reserved_mem.h> 2304a9016eSOhad Ben-Cohen #include <linux/remoteproc.h> 2404a9016eSOhad Ben-Cohen #include <linux/virtio.h> 2504a9016eSOhad Ben-Cohen #include <linux/virtio_config.h> 2604a9016eSOhad Ben-Cohen #include <linux/virtio_ids.h> 2704a9016eSOhad Ben-Cohen #include <linux/virtio_ring.h> 2804a9016eSOhad Ben-Cohen #include <linux/err.h> 2904a9016eSOhad Ben-Cohen #include <linux/kref.h> 3004a9016eSOhad Ben-Cohen #include <linux/slab.h> 3104a9016eSOhad Ben-Cohen 3204a9016eSOhad Ben-Cohen #include "remoteproc_internal.h" 3304a9016eSOhad Ben-Cohen 3404a9016eSOhad Ben-Cohen /* kick the remote processor, and let it know which virtqueue to poke at */ 3546f9c2b9SHeinz Graalfs static bool rproc_virtio_notify(struct virtqueue *vq) 3604a9016eSOhad Ben-Cohen { 377a186941SOhad Ben-Cohen struct rproc_vring *rvring = vq->priv; 387a186941SOhad Ben-Cohen struct rproc *rproc = rvring->rvdev->rproc; 397a186941SOhad Ben-Cohen int notifyid = rvring->notifyid; 4004a9016eSOhad Ben-Cohen 41b5ab5e24SOhad Ben-Cohen dev_dbg(&rproc->dev, "kicking vq index: %d\n", notifyid); 4204a9016eSOhad Ben-Cohen 437a186941SOhad Ben-Cohen rproc->ops->kick(rproc, notifyid); 4446f9c2b9SHeinz Graalfs return true; 4504a9016eSOhad Ben-Cohen } 4604a9016eSOhad Ben-Cohen 4704a9016eSOhad Ben-Cohen /** 4804a9016eSOhad Ben-Cohen * rproc_vq_interrupt() - tell remoteproc that a virtqueue is interrupted 4904a9016eSOhad Ben-Cohen * @rproc: handle to the remote processor 507a186941SOhad Ben-Cohen * @notifyid: index of the signalled virtqueue (unique per this @rproc) 5104a9016eSOhad Ben-Cohen * 5204a9016eSOhad Ben-Cohen * This function should be called by the platform-specific rproc driver, 5304a9016eSOhad Ben-Cohen * when the remote processor signals that a specific virtqueue has pending 5404a9016eSOhad Ben-Cohen * messages available. 5504a9016eSOhad Ben-Cohen * 567a186941SOhad Ben-Cohen * Returns IRQ_NONE if no message was found in the @notifyid virtqueue, 5704a9016eSOhad Ben-Cohen * and otherwise returns IRQ_HANDLED. 5804a9016eSOhad Ben-Cohen */ 597a186941SOhad Ben-Cohen irqreturn_t rproc_vq_interrupt(struct rproc *rproc, int notifyid) 6004a9016eSOhad Ben-Cohen { 617a186941SOhad Ben-Cohen struct rproc_vring *rvring; 627a186941SOhad Ben-Cohen 63b5ab5e24SOhad Ben-Cohen dev_dbg(&rproc->dev, "vq index %d is interrupted\n", notifyid); 647a186941SOhad Ben-Cohen 657a186941SOhad Ben-Cohen rvring = idr_find(&rproc->notifyids, notifyid); 667a186941SOhad Ben-Cohen if (!rvring || !rvring->vq) 677a186941SOhad Ben-Cohen return IRQ_NONE; 687a186941SOhad Ben-Cohen 697a186941SOhad Ben-Cohen return vring_interrupt(0, rvring->vq); 7004a9016eSOhad Ben-Cohen } 7104a9016eSOhad Ben-Cohen EXPORT_SYMBOL(rproc_vq_interrupt); 7204a9016eSOhad Ben-Cohen 7304a9016eSOhad Ben-Cohen static struct virtqueue *rp_find_vq(struct virtio_device *vdev, 74f145928dSAnna, Suman unsigned int id, 7504a9016eSOhad Ben-Cohen void (*callback)(struct virtqueue *vq), 76f94682ddSMichael S. Tsirkin const char *name, bool ctx) 7704a9016eSOhad Ben-Cohen { 787a186941SOhad Ben-Cohen struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 7904a9016eSOhad Ben-Cohen struct rproc *rproc = vdev_to_rproc(vdev); 80b5ab5e24SOhad Ben-Cohen struct device *dev = &rproc->dev; 81c6aed238SLoic Pallardy struct rproc_mem_entry *mem; 827a186941SOhad Ben-Cohen struct rproc_vring *rvring; 83c6aed238SLoic Pallardy struct fw_rsc_vdev *rsc; 8404a9016eSOhad Ben-Cohen struct virtqueue *vq; 8504a9016eSOhad Ben-Cohen void *addr; 86a863af5dSBjorn Andersson int len, size; 8704a9016eSOhad Ben-Cohen 887a186941SOhad Ben-Cohen /* we're temporarily limited to two virtqueues per rvdev */ 897a186941SOhad Ben-Cohen if (id >= ARRAY_SIZE(rvdev->vring)) 907a186941SOhad Ben-Cohen return ERR_PTR(-EINVAL); 9104a9016eSOhad Ben-Cohen 926457f126SMichael S. Tsirkin if (!name) 936457f126SMichael S. Tsirkin return NULL; 946457f126SMichael S. Tsirkin 95c6aed238SLoic Pallardy /* Search allocated memory region by name */ 96c6aed238SLoic Pallardy mem = rproc_find_carveout_by_name(rproc, "vdev%dvring%d", rvdev->index, 97c6aed238SLoic Pallardy id); 98c6aed238SLoic Pallardy if (!mem || !mem->va) 99c6aed238SLoic Pallardy return ERR_PTR(-ENOMEM); 100c6aed238SLoic Pallardy 1016db20ea8SOhad Ben-Cohen rvring = &rvdev->vring[id]; 102c6aed238SLoic Pallardy addr = mem->va; 1037a186941SOhad Ben-Cohen len = rvring->len; 10404a9016eSOhad Ben-Cohen 1057a186941SOhad Ben-Cohen /* zero vring */ 1067a186941SOhad Ben-Cohen size = vring_size(len, rvring->align); 1077a186941SOhad Ben-Cohen memset(addr, 0, size); 1087a186941SOhad Ben-Cohen 109276ec993SLoic Pallardy dev_dbg(dev, "vring%d: va %pK qsz %d notifyid %d\n", 1107a186941SOhad Ben-Cohen id, addr, len, rvring->notifyid); 11104a9016eSOhad Ben-Cohen 11204a9016eSOhad Ben-Cohen /* 11304a9016eSOhad Ben-Cohen * Create the new vq, and tell virtio we're not interested in 11404a9016eSOhad Ben-Cohen * the 'weak' smp barriers, since we're talking with a real device. 11504a9016eSOhad Ben-Cohen */ 116f94682ddSMichael S. Tsirkin vq = vring_new_virtqueue(id, len, rvring->align, vdev, false, ctx, 117f94682ddSMichael S. Tsirkin addr, rproc_virtio_notify, callback, name); 11804a9016eSOhad Ben-Cohen if (!vq) { 119b5ab5e24SOhad Ben-Cohen dev_err(dev, "vring_new_virtqueue %s failed\n", name); 1206db20ea8SOhad Ben-Cohen rproc_free_vring(rvring); 1217a186941SOhad Ben-Cohen return ERR_PTR(-ENOMEM); 12204a9016eSOhad Ben-Cohen } 12304a9016eSOhad Ben-Cohen 1247a186941SOhad Ben-Cohen rvring->vq = vq; 1257a186941SOhad Ben-Cohen vq->priv = rvring; 12604a9016eSOhad Ben-Cohen 127c6aed238SLoic Pallardy /* Update vring in resource table */ 128c6aed238SLoic Pallardy rsc = (void *)rproc->table_ptr + rvdev->rsc_offset; 129c6aed238SLoic Pallardy rsc->vring[id].da = mem->da; 130c6aed238SLoic Pallardy 13104a9016eSOhad Ben-Cohen return vq; 13204a9016eSOhad Ben-Cohen } 13304a9016eSOhad Ben-Cohen 134dab55bbaSOhad Ben-Cohen static void __rproc_virtio_del_vqs(struct virtio_device *vdev) 13504a9016eSOhad Ben-Cohen { 13604a9016eSOhad Ben-Cohen struct virtqueue *vq, *n; 1377a186941SOhad Ben-Cohen struct rproc_vring *rvring; 13804a9016eSOhad Ben-Cohen 13904a9016eSOhad Ben-Cohen list_for_each_entry_safe(vq, n, &vdev->vqs, list) { 1407a186941SOhad Ben-Cohen rvring = vq->priv; 1417a186941SOhad Ben-Cohen rvring->vq = NULL; 14204a9016eSOhad Ben-Cohen vring_del_virtqueue(vq); 14304a9016eSOhad Ben-Cohen } 14404a9016eSOhad Ben-Cohen } 14504a9016eSOhad Ben-Cohen 146dab55bbaSOhad Ben-Cohen static void rproc_virtio_del_vqs(struct virtio_device *vdev) 147dab55bbaSOhad Ben-Cohen { 148dab55bbaSOhad Ben-Cohen __rproc_virtio_del_vqs(vdev); 149dab55bbaSOhad Ben-Cohen } 150dab55bbaSOhad Ben-Cohen 151f145928dSAnna, Suman static int rproc_virtio_find_vqs(struct virtio_device *vdev, unsigned int nvqs, 15204a9016eSOhad Ben-Cohen struct virtqueue *vqs[], 15304a9016eSOhad Ben-Cohen vq_callback_t *callbacks[], 154fb5e31d9SChristoph Hellwig const char * const names[], 155f94682ddSMichael S. Tsirkin const bool * ctx, 156fb5e31d9SChristoph Hellwig struct irq_affinity *desc) 15704a9016eSOhad Ben-Cohen { 15804a9016eSOhad Ben-Cohen int i, ret; 15904a9016eSOhad Ben-Cohen 16004a9016eSOhad Ben-Cohen for (i = 0; i < nvqs; ++i) { 161f94682ddSMichael S. Tsirkin vqs[i] = rp_find_vq(vdev, i, callbacks[i], names[i], 162f94682ddSMichael S. Tsirkin ctx ? ctx[i] : false); 16304a9016eSOhad Ben-Cohen if (IS_ERR(vqs[i])) { 16404a9016eSOhad Ben-Cohen ret = PTR_ERR(vqs[i]); 16504a9016eSOhad Ben-Cohen goto error; 16604a9016eSOhad Ben-Cohen } 16704a9016eSOhad Ben-Cohen } 16804a9016eSOhad 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 { 17892b38f85SSjur Brændeland struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 17992b38f85SSjur Brændeland struct fw_rsc_vdev *rsc; 18092b38f85SSjur Brændeland 18192b38f85SSjur Brændeland rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset; 18292b38f85SSjur Brændeland 18392b38f85SSjur 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 { 18892b38f85SSjur Brændeland struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 18992b38f85SSjur Brændeland struct fw_rsc_vdev *rsc; 19092b38f85SSjur Brændeland 19192b38f85SSjur Brændeland rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset; 19292b38f85SSjur Brændeland 19392b38f85SSjur 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 { 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 = 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 */ 209d0254773SMichael S. Tsirkin static u64 rproc_virtio_get_features(struct virtio_device *vdev) 21004a9016eSOhad Ben-Cohen { 2117a186941SOhad Ben-Cohen struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 21292b38f85SSjur Brændeland struct fw_rsc_vdev *rsc; 21304a9016eSOhad Ben-Cohen 21492b38f85SSjur Brændeland rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset; 21592b38f85SSjur Brændeland 21692b38f85SSjur Brændeland return rsc->dfeatures; 21704a9016eSOhad Ben-Cohen } 21804a9016eSOhad Ben-Cohen 2193a814fdfSTiwei Bie static void rproc_transport_features(struct virtio_device *vdev) 2203a814fdfSTiwei Bie { 2213a814fdfSTiwei Bie /* 2223a814fdfSTiwei Bie * Packed ring isn't enabled on remoteproc for now, 2233a814fdfSTiwei Bie * because remoteproc uses vring_new_virtqueue() which 2243a814fdfSTiwei Bie * creates virtio rings on preallocated memory. 2253a814fdfSTiwei Bie */ 2263a814fdfSTiwei Bie __virtio_clear_bit(vdev, VIRTIO_F_RING_PACKED); 2273a814fdfSTiwei Bie } 2283a814fdfSTiwei Bie 2295c609a5eSMichael S. Tsirkin static int rproc_virtio_finalize_features(struct virtio_device *vdev) 23004a9016eSOhad Ben-Cohen { 2317a186941SOhad Ben-Cohen struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 23292b38f85SSjur Brændeland struct fw_rsc_vdev *rsc; 23392b38f85SSjur Brændeland 23492b38f85SSjur Brændeland rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset; 23504a9016eSOhad Ben-Cohen 23604a9016eSOhad Ben-Cohen /* Give virtio_ring a chance to accept features */ 23704a9016eSOhad Ben-Cohen vring_transport_features(vdev); 23804a9016eSOhad Ben-Cohen 2393a814fdfSTiwei Bie /* Give virtio_rproc a chance to accept features. */ 2403a814fdfSTiwei Bie rproc_transport_features(vdev); 2413a814fdfSTiwei Bie 24293d389f8SMichael S. Tsirkin /* Make sure we don't have any features > 32 bits! */ 24393d389f8SMichael S. Tsirkin BUG_ON((u32)vdev->features != vdev->features); 24493d389f8SMichael S. Tsirkin 24504a9016eSOhad Ben-Cohen /* 24604a9016eSOhad Ben-Cohen * Remember the finalized features of our vdev, and provide it 24704a9016eSOhad Ben-Cohen * to the remote processor once it is powered on. 24804a9016eSOhad Ben-Cohen */ 249e16e12beSMichael S. Tsirkin rsc->gfeatures = vdev->features; 2505c609a5eSMichael S. Tsirkin 2515c609a5eSMichael S. Tsirkin return 0; 25292b38f85SSjur Brændeland } 25392b38f85SSjur Brændeland 254f145928dSAnna, Suman static void rproc_virtio_get(struct virtio_device *vdev, unsigned int offset, 255f145928dSAnna, Suman void *buf, unsigned int len) 25692b38f85SSjur Brændeland { 25792b38f85SSjur Brændeland struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 25892b38f85SSjur Brændeland struct fw_rsc_vdev *rsc; 25992b38f85SSjur Brændeland void *cfg; 26092b38f85SSjur Brændeland 26192b38f85SSjur Brændeland rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset; 26292b38f85SSjur Brændeland cfg = &rsc->vring[rsc->num_of_vrings]; 26392b38f85SSjur Brændeland 26492b38f85SSjur Brændeland if (offset + len > rsc->config_len || offset + len < len) { 26592b38f85SSjur Brændeland dev_err(&vdev->dev, "rproc_virtio_get: access out of bounds\n"); 26692b38f85SSjur Brændeland return; 26792b38f85SSjur Brændeland } 26892b38f85SSjur Brændeland 26992b38f85SSjur Brændeland memcpy(buf, cfg + offset, len); 27092b38f85SSjur Brændeland } 27192b38f85SSjur Brændeland 272f145928dSAnna, Suman static void rproc_virtio_set(struct virtio_device *vdev, unsigned int offset, 273f145928dSAnna, Suman const void *buf, unsigned int len) 27492b38f85SSjur Brændeland { 27592b38f85SSjur Brændeland struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 27692b38f85SSjur Brændeland struct fw_rsc_vdev *rsc; 27792b38f85SSjur Brændeland void *cfg; 27892b38f85SSjur Brændeland 27992b38f85SSjur Brændeland rsc = (void *)rvdev->rproc->table_ptr + rvdev->rsc_offset; 28092b38f85SSjur Brændeland cfg = &rsc->vring[rsc->num_of_vrings]; 28192b38f85SSjur Brændeland 28292b38f85SSjur Brændeland if (offset + len > rsc->config_len || offset + len < len) { 28392b38f85SSjur Brændeland dev_err(&vdev->dev, "rproc_virtio_set: access out of bounds\n"); 28492b38f85SSjur Brændeland return; 28592b38f85SSjur Brændeland } 28692b38f85SSjur Brændeland 28792b38f85SSjur Brændeland memcpy(cfg + offset, buf, len); 28804a9016eSOhad Ben-Cohen } 28904a9016eSOhad Ben-Cohen 29093503932SStephen Hemminger static const struct virtio_config_ops rproc_virtio_config_ops = { 29104a9016eSOhad Ben-Cohen .get_features = rproc_virtio_get_features, 29204a9016eSOhad Ben-Cohen .finalize_features = rproc_virtio_finalize_features, 29304a9016eSOhad Ben-Cohen .find_vqs = rproc_virtio_find_vqs, 29404a9016eSOhad Ben-Cohen .del_vqs = rproc_virtio_del_vqs, 29504a9016eSOhad Ben-Cohen .reset = rproc_virtio_reset, 29604a9016eSOhad Ben-Cohen .set_status = rproc_virtio_set_status, 29704a9016eSOhad Ben-Cohen .get_status = rproc_virtio_get_status, 29892b38f85SSjur Brændeland .get = rproc_virtio_get, 29992b38f85SSjur Brændeland .set = rproc_virtio_set, 30004a9016eSOhad Ben-Cohen }; 30104a9016eSOhad Ben-Cohen 30204a9016eSOhad Ben-Cohen /* 30304a9016eSOhad Ben-Cohen * This function is called whenever vdev is released, and is responsible 3047183a2a7SOhad Ben-Cohen * to decrement the remote processor's refcount which was taken when vdev was 30504a9016eSOhad Ben-Cohen * added. 30604a9016eSOhad Ben-Cohen * 30704a9016eSOhad Ben-Cohen * Never call this function directly; it will be called by the driver 30804a9016eSOhad Ben-Cohen * core when needed. 30904a9016eSOhad Ben-Cohen */ 310aab8d802SBjorn Andersson static void rproc_virtio_dev_release(struct device *dev) 31104a9016eSOhad Ben-Cohen { 31204a9016eSOhad Ben-Cohen struct virtio_device *vdev = dev_to_virtio(dev); 3136db20ea8SOhad Ben-Cohen struct rproc_vdev *rvdev = vdev_to_rvdev(vdev); 31404a9016eSOhad Ben-Cohen struct rproc *rproc = vdev_to_rproc(vdev); 31504a9016eSOhad Ben-Cohen 316aab8d802SBjorn Andersson kref_put(&rvdev->refcount, rproc_vdev_release); 3176db20ea8SOhad Ben-Cohen 3187183a2a7SOhad Ben-Cohen put_device(&rproc->dev); 31904a9016eSOhad Ben-Cohen } 32004a9016eSOhad Ben-Cohen 32104a9016eSOhad Ben-Cohen /** 3227a186941SOhad Ben-Cohen * rproc_add_virtio_dev() - register an rproc-induced virtio device 3237a186941SOhad Ben-Cohen * @rvdev: the remote vdev 32404a9016eSOhad Ben-Cohen * 3257a186941SOhad Ben-Cohen * This function registers a virtio device. This vdev's partent is 3267a186941SOhad Ben-Cohen * the rproc device. 32704a9016eSOhad Ben-Cohen * 3287a186941SOhad Ben-Cohen * Returns 0 on success or an appropriate error value otherwise. 32904a9016eSOhad Ben-Cohen */ 3307a186941SOhad Ben-Cohen int rproc_add_virtio_dev(struct rproc_vdev *rvdev, int id) 33104a9016eSOhad Ben-Cohen { 3327a186941SOhad Ben-Cohen struct rproc *rproc = rvdev->rproc; 333*086d0872SLoic Pallardy struct device *dev = &rvdev->dev; 3347a186941SOhad Ben-Cohen struct virtio_device *vdev = &rvdev->vdev; 335*086d0872SLoic Pallardy struct rproc_mem_entry *mem; 33604a9016eSOhad Ben-Cohen int ret; 33704a9016eSOhad Ben-Cohen 338*086d0872SLoic Pallardy /* Try to find dedicated vdev buffer carveout */ 339*086d0872SLoic Pallardy mem = rproc_find_carveout_by_name(rproc, "vdev%dbuffer", rvdev->index); 340*086d0872SLoic Pallardy if (mem) { 341*086d0872SLoic Pallardy phys_addr_t pa; 342*086d0872SLoic Pallardy 343*086d0872SLoic Pallardy if (mem->of_resm_idx != -1) { 344*086d0872SLoic Pallardy struct device_node *np = rproc->dev.parent->of_node; 345*086d0872SLoic Pallardy 346*086d0872SLoic Pallardy /* Associate reserved memory to vdev device */ 347*086d0872SLoic Pallardy ret = of_reserved_mem_device_init_by_idx(dev, np, 348*086d0872SLoic Pallardy mem->of_resm_idx); 349*086d0872SLoic Pallardy if (ret) { 350*086d0872SLoic Pallardy dev_err(dev, "Can't associate reserved memory\n"); 351*086d0872SLoic Pallardy goto out; 352*086d0872SLoic Pallardy } 353*086d0872SLoic Pallardy } else { 354*086d0872SLoic Pallardy if (mem->va) { 355*086d0872SLoic Pallardy dev_warn(dev, "vdev %d buffer already mapped\n", 356*086d0872SLoic Pallardy rvdev->index); 357*086d0872SLoic Pallardy pa = rproc_va_to_pa(mem->va); 358*086d0872SLoic Pallardy } else { 359*086d0872SLoic Pallardy /* Use dma address as carveout no memmapped yet */ 360*086d0872SLoic Pallardy pa = (phys_addr_t)mem->dma; 361*086d0872SLoic Pallardy } 362*086d0872SLoic Pallardy 363*086d0872SLoic Pallardy /* Associate vdev buffer memory pool to vdev subdev */ 364*086d0872SLoic Pallardy ret = dma_declare_coherent_memory(dev, pa, 365*086d0872SLoic Pallardy mem->da, 366*086d0872SLoic Pallardy mem->len, 367*086d0872SLoic Pallardy DMA_MEMORY_EXCLUSIVE); 368*086d0872SLoic Pallardy if (ret < 0) { 369*086d0872SLoic Pallardy dev_err(dev, "Failed to associate buffer\n"); 370*086d0872SLoic Pallardy goto out; 371*086d0872SLoic Pallardy } 372*086d0872SLoic Pallardy } 373*086d0872SLoic Pallardy } 374*086d0872SLoic Pallardy 3757a186941SOhad Ben-Cohen vdev->id.device = id, 3767a186941SOhad Ben-Cohen vdev->config = &rproc_virtio_config_ops, 3777a186941SOhad Ben-Cohen vdev->dev.parent = dev; 378aab8d802SBjorn Andersson vdev->dev.release = rproc_virtio_dev_release; 37904a9016eSOhad Ben-Cohen 38004a9016eSOhad Ben-Cohen /* 38104a9016eSOhad Ben-Cohen * We're indirectly making a non-temporary copy of the rproc pointer 38204a9016eSOhad Ben-Cohen * here, because drivers probed with this vdev will indirectly 38304a9016eSOhad Ben-Cohen * access the wrapping rproc. 38404a9016eSOhad Ben-Cohen * 38504a9016eSOhad Ben-Cohen * Therefore we must increment the rproc refcount here, and decrement 38604a9016eSOhad Ben-Cohen * it _only_ when the vdev is released. 38704a9016eSOhad Ben-Cohen */ 3887183a2a7SOhad Ben-Cohen get_device(&rproc->dev); 38904a9016eSOhad Ben-Cohen 390aab8d802SBjorn Andersson /* Reference the vdev and vring allocations */ 391aab8d802SBjorn Andersson kref_get(&rvdev->refcount); 392aab8d802SBjorn Andersson 3937a186941SOhad Ben-Cohen ret = register_virtio_device(vdev); 39404a9016eSOhad Ben-Cohen if (ret) { 395900a163eSweiping zhang put_device(&vdev->dev); 39604a9016eSOhad Ben-Cohen dev_err(dev, "failed to register vdev: %d\n", ret); 3977a186941SOhad Ben-Cohen goto out; 39804a9016eSOhad Ben-Cohen } 39904a9016eSOhad Ben-Cohen 4007a186941SOhad Ben-Cohen dev_info(dev, "registered %s (type %d)\n", dev_name(&vdev->dev), id); 4017a186941SOhad Ben-Cohen 4027a186941SOhad Ben-Cohen out: 40304a9016eSOhad Ben-Cohen return ret; 40404a9016eSOhad Ben-Cohen } 40504a9016eSOhad Ben-Cohen 40604a9016eSOhad Ben-Cohen /** 4077a186941SOhad Ben-Cohen * rproc_remove_virtio_dev() - remove an rproc-induced virtio device 4087a186941SOhad Ben-Cohen * @rvdev: the remote vdev 40904a9016eSOhad Ben-Cohen * 4107a186941SOhad Ben-Cohen * This function unregisters an existing virtio device. 41104a9016eSOhad Ben-Cohen */ 4127a186941SOhad Ben-Cohen void rproc_remove_virtio_dev(struct rproc_vdev *rvdev) 41304a9016eSOhad Ben-Cohen { 41404a9016eSOhad Ben-Cohen unregister_virtio_device(&rvdev->vdev); 41504a9016eSOhad Ben-Cohen } 416