Lines Matching +full:rpmsg +full:- +full:channel +full:- +full:name

1 // SPDX-License-Identifier: GPL-2.0
3 * Virtio-based remote processor messaging bus
8 * Ohad Ben-Cohen <ohad@wizery.com>
14 #include <linux/dma-mapping.h>
20 #include <linux/rpmsg.h>
21 #include <linux/rpmsg/byteorder.h>
22 #include <linux/rpmsg/ns.h>
34 * struct virtproc_info - virtual remote processor state
52 * This structure stores the rpmsg state of a given virtio remote processor
71 /* The feature bitmap for virtio rpmsg */
72 #define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
75 * struct rpmsg_hdr - common header for all rpmsg messages
83 * Every message sent(/received) on the rpmsg bus begins with this header.
96 * struct virtio_rpmsg_channel - rpmsg channel descriptor
97 * @rpdev: the rpmsg channel device
98 * @vrp: the virtio remote processor device this channel belongs to
100 * This structure stores the channel that links the rpmsg device to the virtio
122 * We might also want to add support for user-provided buffers in time.
124 * to achieve zero-copy messaging.
126 * Note that these numbers are purely a decision of this driver - we
134 * Local addresses are dynamically allocated on-demand.
161 * rpmsg_sg_init - initialize scatterlist according to cpu address location
183 * __ept_release() - deallocate an rpmsg endpoint
210 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
216 kref_init(&ept->refcount);
217 mutex_init(&ept->cb_lock);
219 ept->rpdev = rpdev;
220 ept->cb = cb;
221 ept->priv = priv;
222 ept->ops = &virtio_endpoint_ops;
233 mutex_lock(&vrp->endpoints_lock);
235 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
236 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
241 ept->addr = id;
243 mutex_unlock(&vrp->endpoints_lock);
248 mutex_unlock(&vrp->endpoints_lock);
249 kref_put(&ept->refcount, __ept_release);
257 struct virtproc_info *vrp = vch->vrp;
266 struct virtproc_info *vrp = vch->vrp;
268 return rpmsg_unregister_device(&vrp->vdev->dev, chinfo);
278 return __rpmsg_create_ept(vch->vrp, rpdev, cb, priv, chinfo.src);
282 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
287 * bound to an rpmsg channel. This is needed for handling the internal
288 * name service endpoint, which isn't bound to an rpmsg channel.
295 mutex_lock(&vrp->endpoints_lock);
296 idr_remove(&vrp->endpoints, ept->addr);
297 mutex_unlock(&vrp->endpoints_lock);
299 /* make sure in-flight inbound messages won't invoke cb anymore */
300 mutex_lock(&ept->cb_lock);
301 ept->cb = NULL;
302 mutex_unlock(&ept->cb_lock);
304 kref_put(&ept->refcount, __ept_release);
309 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(ept->rpdev);
311 __rpmsg_destroy_ept(vch->vrp, ept);
317 struct virtproc_info *vrp = vch->vrp;
318 struct device *dev = &rpdev->dev;
321 /* need to tell remote processor's name service about this channel ? */
322 if (rpdev->announce && rpdev->ept &&
323 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
326 strscpy_pad(nsm.name, rpdev->id.name, sizeof(nsm.name));
327 nsm.addr = cpu_to_rpmsg32(rpdev, rpdev->ept->addr);
330 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
341 struct virtproc_info *vrp = vch->vrp;
342 struct device *dev = &rpdev->dev;
345 /* tell remote processor's name service we're removing this channel */
346 if (rpdev->announce && rpdev->ept &&
347 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
350 strscpy_pad(nsm.name, rpdev->id.name, sizeof(nsm.name));
351 nsm.addr = cpu_to_rpmsg32(rpdev, rpdev->ept->addr);
354 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
375 kfree(rpdev->driver_override);
380 * create an rpmsg channel using its name and address info.
389 struct device *tmp, *dev = &vrp->vdev->dev;
392 /* make sure a similar channel doesn't already exist */
397 dev_err(dev, "channel %s:%x:%x already exist\n",
398 chinfo->name, chinfo->src, chinfo->dst);
406 /* Link the channel to our vrp */
407 vch->vrp = vrp;
410 rpdev = &vch->rpdev;
411 rpdev->src = chinfo->src;
412 rpdev->dst = chinfo->dst;
413 rpdev->ops = &virtio_rpmsg_ops;
414 rpdev->little_endian = virtio_is_little_endian(vrp->vdev);
417 * rpmsg server channels has predefined local address (for now),
420 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
422 strscpy(rpdev->id.name, chinfo->name, sizeof(rpdev->id.name));
424 rpdev->dev.parent = &vrp->vdev->dev;
425 rpdev->dev.release = virtio_rpmsg_release_device;
440 mutex_lock(&vrp->tx_lock);
446 if (vrp->last_sbuf < vrp->num_bufs / 2)
447 ret = vrp->sbufs + vrp->buf_size * vrp->last_sbuf++;
450 ret = virtqueue_get_buf(vrp->svq, &len);
452 mutex_unlock(&vrp->tx_lock);
458 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
476 mutex_lock(&vrp->tx_lock);
479 if (atomic_inc_return(&vrp->sleepers) == 1)
480 /* enable "tx-complete" interrupts before dozing off */
481 virtqueue_enable_cb(vrp->svq);
483 mutex_unlock(&vrp->tx_lock);
487 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
503 mutex_lock(&vrp->tx_lock);
506 if (atomic_dec_and_test(&vrp->sleepers))
507 /* disable "tx-complete" interrupts */
508 virtqueue_disable_cb(vrp->svq);
510 mutex_unlock(&vrp->tx_lock);
514 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
515 * @rpdev: the rpmsg channel
522 * This function is the base implementation for all of the rpmsg sending API.
525 * message will be sent to the remote processor which the @rpdev channel
534 * case -ERESTARTSYS is returned. The number '15' itself was picked
539 * the function will immediately fail, and -ENOMEM will be returned.
543 * (see include/linux/rpmsg.h).
552 struct virtproc_info *vrp = vch->vrp;
553 struct device *dev = &rpdev->dev;
561 return -EINVAL;
565 * We currently use fixed-sized buffers, and therefore the payload
569 * user-provided buffers (and then we can also support zero-copy
571 * variable-length buffer sizes.
573 if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) {
575 return -EMSGSIZE;
581 return -ENOMEM;
585 /* enable "tx-complete" interrupts, if not already enabled */
594 err = wait_event_interruptible_timeout(vrp->sendq,
598 /* disable "tx-complete" interrupts if we're the last sleeper */
604 return -ERESTARTSYS;
608 msg->len = cpu_to_rpmsg16(rpdev, len);
609 msg->flags = 0;
610 msg->src = cpu_to_rpmsg32(rpdev, src);
611 msg->dst = cpu_to_rpmsg32(rpdev, dst);
612 msg->reserved = 0;
613 memcpy(msg->data, data, len);
616 src, dst, len, msg->flags, msg->reserved);
624 mutex_lock(&vrp->tx_lock);
627 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
631 * (memory won't leak, but rpmsg won't use it again for TX).
639 virtqueue_kick(vrp->svq);
641 mutex_unlock(&vrp->tx_lock);
647 struct rpmsg_device *rpdev = ept->rpdev;
648 u32 src = ept->addr, dst = rpdev->dst;
656 struct rpmsg_device *rpdev = ept->rpdev;
657 u32 src = ept->addr;
664 struct rpmsg_device *rpdev = ept->rpdev;
665 u32 src = ept->addr, dst = rpdev->dst;
673 struct rpmsg_device *rpdev = ept->rpdev;
674 u32 src = ept->addr;
681 struct rpmsg_device *rpdev = ept->rpdev;
684 return vch->vrp->buf_size - sizeof(struct rpmsg_hdr);
692 bool little_endian = virtio_is_little_endian(vrp->vdev);
693 unsigned int msg_len = __rpmsg16_to_cpu(little_endian, msg->len);
697 __rpmsg32_to_cpu(little_endian, msg->src),
698 __rpmsg32_to_cpu(little_endian, msg->dst), msg_len,
699 __rpmsg16_to_cpu(little_endian, msg->flags),
700 __rpmsg32_to_cpu(little_endian, msg->reserved));
707 * We currently use fixed-sized buffers, so trivially sanitize
710 if (len > vrp->buf_size ||
711 msg_len > (len - sizeof(struct rpmsg_hdr))) {
713 return -EINVAL;
717 mutex_lock(&vrp->endpoints_lock);
719 ept = idr_find(&vrp->endpoints, __rpmsg32_to_cpu(little_endian, msg->dst));
723 kref_get(&ept->refcount);
725 mutex_unlock(&vrp->endpoints_lock);
728 /* make sure ept->cb doesn't go away while we use it */
729 mutex_lock(&ept->cb_lock);
731 if (ept->cb)
732 ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
733 __rpmsg32_to_cpu(little_endian, msg->src));
735 mutex_unlock(&ept->cb_lock);
738 kref_put(&ept->refcount, __ept_release);
743 rpmsg_sg_init(&sg, msg, vrp->buf_size);
746 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
758 struct virtproc_info *vrp = rvq->vdev->priv;
759 struct device *dev = &rvq->vdev->dev;
784 virtqueue_kick(vrp->rvq);
796 struct virtproc_info *vrp = svq->vdev->priv;
798 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
801 wake_up_interruptible(&vrp->sendq);
806 * create endpoint-to-endpoint communication without associated RPMsg channel.
807 * The endpoints are rattached to the ctrldev RPMsg device.
811 struct virtproc_info *vrp = vdev->priv;
818 return ERR_PTR(-ENOMEM);
820 /* Link the channel to the vrp */
821 vch->vrp = vrp;
824 rpdev_ctrl = &vch->rpdev;
825 rpdev_ctrl->ops = &virtio_rpmsg_ops;
827 rpdev_ctrl->dev.parent = &vrp->vdev->dev;
828 rpdev_ctrl->dev.release = virtio_rpmsg_release_device;
829 rpdev_ctrl->little_endian = virtio_is_little_endian(vrp->vdev);
844 device_unregister(&rpdev_ctrl->dev);
864 return -ENOMEM;
866 vrp->vdev = vdev;
868 idr_init(&vrp->endpoints);
869 mutex_init(&vrp->endpoints_lock);
870 mutex_init(&vrp->tx_lock);
871 init_waitqueue_head(&vrp->sendq);
878 vrp->rvq = vqs[0];
879 vrp->svq = vqs[1];
882 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
883 virtqueue_get_vring_size(vrp->svq));
886 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
887 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
889 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
891 vrp->buf_size = MAX_RPMSG_BUF_SIZE;
893 total_buf_space = vrp->num_bufs * vrp->buf_size;
896 bufs_va = dma_alloc_coherent(vdev->dev.parent,
897 total_buf_space, &vrp->bufs_dma,
900 err = -ENOMEM;
904 dev_dbg(&vdev->dev, "buffers: va %pK, dma %pad\n",
905 bufs_va, &vrp->bufs_dma);
908 vrp->rbufs = bufs_va;
911 vrp->sbufs = bufs_va + total_buf_space / 2;
914 for (i = 0; i < vrp->num_bufs / 2; i++) {
916 void *cpu_addr = vrp->rbufs + i * vrp->buf_size;
918 rpmsg_sg_init(&sg, cpu_addr, vrp->buf_size);
920 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
925 /* suppress "tx-complete" interrupts */
926 virtqueue_disable_cb(vrp->svq);
928 vdev->priv = vrp;
936 /* if supported by the remote processor, enable the name service */
940 err = -ENOMEM;
944 /* Link the channel to our vrp */
945 vch->vrp = vrp;
948 rpdev_ns = &vch->rpdev;
949 rpdev_ns->ops = &virtio_rpmsg_ops;
950 rpdev_ns->little_endian = virtio_is_little_endian(vrp->vdev);
952 rpdev_ns->dev.parent = &vrp->vdev->dev;
953 rpdev_ns->dev.release = virtio_rpmsg_release_device;
962 * Prepare to kick but don't notify yet - we can't do this before
965 notify = virtqueue_kick_prepare(vrp->rvq);
976 virtqueue_notify(vrp->rvq);
978 dev_info(&vdev->dev, "rpmsg host is online\n");
985 dma_free_coherent(vdev->dev.parent, total_buf_space,
986 bufs_va, vrp->bufs_dma);
988 vdev->config->del_vqs(vrp->vdev);
1003 struct virtproc_info *vrp = vdev->priv;
1004 size_t total_buf_space = vrp->num_bufs * vrp->buf_size;
1009 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1011 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1013 idr_destroy(&vrp->endpoints);
1015 vdev->config->del_vqs(vrp->vdev);
1017 dma_free_coherent(vdev->dev.parent, total_buf_space,
1018 vrp->rbufs, vrp->bufs_dma);
1035 .driver.name = KBUILD_MODNAME,
1060 MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");