2 * Virtio-based remote processor messaging bus
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
7 * Ohad Ben-Cohen <ohad@wizery.com>
8 * Brian Swetland <swetland@google.com>
10 * This software is licensed under the terms of the GNU General Public
11 * License version 2, as published by the Free Software Foundation, and
12 * may be copied, distributed, and modified under those terms.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #define pr_fmt(fmt) "%s: " fmt, __func__
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/virtio.h>
25 #include <linux/virtio_ids.h>
26 #include <linux/virtio_config.h>
27 #include <linux/scatterlist.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/slab.h>
30 #include <linux/idr.h>
31 #include <linux/jiffies.h>
32 #include <linux/sched.h>
33 #include <linux/wait.h>
34 #include <linux/rpmsg.h>
35 #include <linux/mutex.h>
36 #include <linux/of_device.h>
39 * struct virtproc_info - virtual remote processor state
40 * @vdev: the virtio device
43 * @rbufs: kernel address of rx buffers
44 * @sbufs: kernel address of tx buffers
45 * @num_bufs: total number of buffers for rx and tx
46 * @last_sbuf: index of last tx buffer used
47 * @bufs_dma: dma base addr of the buffers
48 * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
49 * sending a message might require waking up a dozing remote
50 * processor, which involves sleeping, hence the mutex.
51 * @endpoints: idr of local endpoints, allows fast retrieval
52 * @endpoints_lock: lock of the endpoints set
53 * @sendq: wait queue of sending contexts waiting for a tx buffers
54 * @sleepers: number of senders that are waiting for a tx buffer
55 * @ns_ept: the bus's name service endpoint
57 * This structure stores the rpmsg state of a given virtio remote processor
58 * device (there might be several virtio proc devices for each physical
61 struct virtproc_info {
62 struct virtio_device *vdev;
63 struct virtqueue *rvq, *svq;
65 unsigned int num_bufs;
70 struct mutex endpoints_lock;
71 wait_queue_head_t sendq;
73 struct rpmsg_endpoint *ns_ept;
76 #define to_rpmsg_device(d) container_of(d, struct rpmsg_device, dev)
77 #define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
80 * We're allocating buffers of 512 bytes each for communications. The
81 * number of buffers will be computed from the number of buffers supported
82 * by the vring, upto a maximum of 512 buffers (256 in each direction).
84 * Each buffer will have 16 bytes for the msg header and 496 bytes for
87 * This will utilize a maximum total space of 256KB for the buffers.
89 * We might also want to add support for user-provided buffers in time.
90 * This will allow bigger buffer size flexibility, and can also be used
91 * to achieve zero-copy messaging.
93 * Note that these numbers are purely a decision of this driver - we
94 * can change this without changing anything in the firmware of the remote
97 #define MAX_RPMSG_NUM_BUFS (512)
98 #define RPMSG_BUF_SIZE (512)
101 * Local addresses are dynamically allocated on-demand.
102 * We do not dynamically assign addresses from the low 1024 range,
103 * in order to reserve that address range for predefined services.
105 #define RPMSG_RESERVED_ADDRESSES (1024)
107 /* Address 53 is reserved for advertising remote services */
108 #define RPMSG_NS_ADDR (53)
110 /* sysfs show configuration fields */
111 #define rpmsg_show_attr(field, path, format_string) \
113 field##_show(struct device *dev, \
114 struct device_attribute *attr, char *buf) \
116 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
118 return sprintf(buf, format_string, rpdev->path); \
121 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
122 rpmsg_show_attr(name, id.name, "%s\n");
123 rpmsg_show_attr(src, src, "0x%x\n");
124 rpmsg_show_attr(dst, dst, "0x%x\n");
125 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
127 static ssize_t modalias_show(struct device *dev,
128 struct device_attribute *attr, char *buf)
130 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
132 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
135 static struct device_attribute rpmsg_dev_attrs[] = {
144 /* rpmsg devices and drivers are matched using the service name */
145 static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
146 const struct rpmsg_device_id *id)
148 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
151 /* match rpmsg channel and rpmsg driver */
152 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
154 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
155 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
156 const struct rpmsg_device_id *ids = rpdrv->id_table;
160 for (i = 0; ids[i].name[0]; i++)
161 if (rpmsg_id_match(rpdev, &ids[i]))
164 return of_driver_match_device(dev, drv);
167 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
169 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
171 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
176 * __ept_release() - deallocate an rpmsg endpoint
177 * @kref: the ept's reference count
179 * This function deallocates an ept, and is invoked when its @kref refcount
182 * Never invoke this function directly!
184 static void __ept_release(struct kref *kref)
186 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
189 * At this point no one holds a reference to ept anymore,
190 * so we can directly free it
195 /* for more info, see below documentation of rpmsg_create_ept() */
196 static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
197 struct rpmsg_device *rpdev,
199 void *priv, u32 addr)
201 int id_min, id_max, id;
202 struct rpmsg_endpoint *ept;
203 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
205 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
209 kref_init(&ept->refcount);
210 mutex_init(&ept->cb_lock);
216 /* do we need to allocate a local address ? */
217 if (addr == RPMSG_ADDR_ANY) {
218 id_min = RPMSG_RESERVED_ADDRESSES;
225 mutex_lock(&vrp->endpoints_lock);
227 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
228 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
230 dev_err(dev, "idr_alloc failed: %d\n", id);
235 mutex_unlock(&vrp->endpoints_lock);
240 mutex_unlock(&vrp->endpoints_lock);
241 kref_put(&ept->refcount, __ept_release);
245 static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev,
248 struct rpmsg_channel_info chinfo)
250 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, chinfo.src);
254 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
255 * @vrp: virtproc which owns this ept
256 * @ept: endpoing to destroy
258 * An internal function which destroy an ept without assuming it is
259 * bound to an rpmsg channel. This is needed for handling the internal
260 * name service endpoint, which isn't bound to an rpmsg channel.
261 * See also __rpmsg_create_ept().
264 __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
266 /* make sure new inbound messages can't find this ept anymore */
267 mutex_lock(&vrp->endpoints_lock);
268 idr_remove(&vrp->endpoints, ept->addr);
269 mutex_unlock(&vrp->endpoints_lock);
271 /* make sure in-flight inbound messages won't invoke cb anymore */
272 mutex_lock(&ept->cb_lock);
274 mutex_unlock(&ept->cb_lock);
276 kref_put(&ept->refcount, __ept_release);
280 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
281 * @ept: endpoing to destroy
283 * Should be used by drivers to destroy an rpmsg endpoint previously
284 * created with rpmsg_create_ept().
286 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
288 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
290 EXPORT_SYMBOL(rpmsg_destroy_ept);
293 * when an rpmsg driver is probed with a channel, we seamlessly create
294 * it an endpoint, binding its rx callback to a unique local rpmsg
297 * if we need to, we also announce about this channel to the remote
298 * processor (needed in case the driver is exposing an rpmsg service).
300 static int rpmsg_dev_probe(struct device *dev)
302 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
303 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
304 struct rpmsg_channel_info chinfo = {};
305 struct rpmsg_endpoint *ept;
308 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
309 chinfo.src = rpdev->src;
310 chinfo.dst = RPMSG_ADDR_ANY;
312 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
314 dev_err(dev, "failed to create endpoint\n");
320 rpdev->src = ept->addr;
322 err = rpdrv->probe(rpdev);
324 dev_err(dev, "%s: failed: %d\n", __func__, err);
325 rpmsg_destroy_ept(ept);
329 if (rpdev->ops->announce_create)
330 err = rpdev->ops->announce_create(rpdev);
335 static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
337 struct virtproc_info *vrp = rpdev->vrp;
338 struct device *dev = &rpdev->dev;
341 /* need to tell remote processor's name service about this channel ? */
342 if (rpdev->announce &&
343 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
344 struct rpmsg_ns_msg nsm;
346 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
347 nsm.addr = rpdev->ept->addr;
348 nsm.flags = RPMSG_NS_CREATE;
350 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
352 dev_err(dev, "failed to announce service %d\n", err);
358 static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
360 struct virtproc_info *vrp = rpdev->vrp;
361 struct device *dev = &rpdev->dev;
364 /* tell remote processor's name service we're removing this channel */
365 if (rpdev->announce &&
366 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
367 struct rpmsg_ns_msg nsm;
369 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
370 nsm.addr = rpdev->src;
371 nsm.flags = RPMSG_NS_DESTROY;
373 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
375 dev_err(dev, "failed to announce service %d\n", err);
381 static int rpmsg_dev_remove(struct device *dev)
383 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
384 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
387 if (rpdev->ops->announce_destroy)
388 err = rpdev->ops->announce_destroy(rpdev);
390 rpdrv->remove(rpdev);
392 rpmsg_destroy_ept(rpdev->ept);
397 static struct bus_type rpmsg_bus = {
399 .match = rpmsg_dev_match,
400 .dev_attrs = rpmsg_dev_attrs,
401 .uevent = rpmsg_uevent,
402 .probe = rpmsg_dev_probe,
403 .remove = rpmsg_dev_remove,
407 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
408 * @rpdrv: pointer to a struct rpmsg_driver
409 * @owner: owning module/driver
411 * Returns 0 on success, and an appropriate error value on failure.
413 int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
415 rpdrv->drv.bus = &rpmsg_bus;
416 rpdrv->drv.owner = owner;
417 return driver_register(&rpdrv->drv);
419 EXPORT_SYMBOL(__register_rpmsg_driver);
422 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
423 * @rpdrv: pointer to a struct rpmsg_driver
425 * Returns 0 on success, and an appropriate error value on failure.
427 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
429 driver_unregister(&rpdrv->drv);
431 EXPORT_SYMBOL(unregister_rpmsg_driver);
433 static void rpmsg_release_device(struct device *dev)
435 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
441 * match an rpmsg channel with a channel info struct.
442 * this is used to make sure we're not creating rpmsg devices for channels
443 * that already exist.
445 static int rpmsg_device_match(struct device *dev, void *data)
447 struct rpmsg_channel_info *chinfo = data;
448 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
450 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
453 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
456 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
459 /* found a match ! */
463 static const struct rpmsg_device_ops virtio_rpmsg_ops = {
464 .create_ept = virtio_rpmsg_create_ept,
465 .announce_create = virtio_rpmsg_announce_create,
466 .announce_destroy = virtio_rpmsg_announce_destroy,
470 * create an rpmsg channel using its name and address info.
471 * this function will be used to create both static and dynamic
474 static struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp,
475 struct rpmsg_channel_info *chinfo)
477 struct rpmsg_device *rpdev;
478 struct device *tmp, *dev = &vrp->vdev->dev;
481 /* make sure a similar channel doesn't already exist */
482 tmp = device_find_child(dev, chinfo, rpmsg_device_match);
484 /* decrement the matched device's refcount back */
486 dev_err(dev, "channel %s:%x:%x already exist\n",
487 chinfo->name, chinfo->src, chinfo->dst);
491 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
496 rpdev->src = chinfo->src;
497 rpdev->dst = chinfo->dst;
498 rpdev->ops = &virtio_rpmsg_ops;
501 * rpmsg server channels has predefined local address (for now),
502 * and their existence needs to be announced remotely
504 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
506 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
508 dev_set_name(&rpdev->dev, "%s:%s",
509 dev_name(dev->parent), rpdev->id.name);
511 rpdev->dev.parent = &vrp->vdev->dev;
512 rpdev->dev.bus = &rpmsg_bus;
513 rpdev->dev.release = rpmsg_release_device;
515 ret = device_register(&rpdev->dev);
517 dev_err(dev, "device_register failed: %d\n", ret);
518 put_device(&rpdev->dev);
526 * find an existing channel using its name + address properties,
529 static int rpmsg_destroy_channel(struct virtproc_info *vrp,
530 struct rpmsg_channel_info *chinfo)
532 struct virtio_device *vdev = vrp->vdev;
535 dev = device_find_child(&vdev->dev, chinfo, rpmsg_device_match);
539 device_unregister(dev);
546 /* super simple buffer "allocator" that is just enough for now */
547 static void *get_a_tx_buf(struct virtproc_info *vrp)
552 /* support multiple concurrent senders */
553 mutex_lock(&vrp->tx_lock);
556 * either pick the next unused tx buffer
557 * (half of our buffers are used for sending messages)
559 if (vrp->last_sbuf < vrp->num_bufs / 2)
560 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
561 /* or recycle a used one */
563 ret = virtqueue_get_buf(vrp->svq, &len);
565 mutex_unlock(&vrp->tx_lock);
571 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
572 * @vrp: virtual remote processor state
574 * This function is called before a sender is blocked, waiting for
575 * a tx buffer to become available.
577 * If we already have blocking senders, this function merely increases
578 * the "sleepers" reference count, and exits.
580 * Otherwise, if this is the first sender to block, we also enable
581 * virtio's tx callbacks, so we'd be immediately notified when a tx
582 * buffer is consumed (we rely on virtio's tx callback in order
583 * to wake up sleeping senders as soon as a tx buffer is used by the
586 static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
588 /* support multiple concurrent senders */
589 mutex_lock(&vrp->tx_lock);
591 /* are we the first sleeping context waiting for tx buffers ? */
592 if (atomic_inc_return(&vrp->sleepers) == 1)
593 /* enable "tx-complete" interrupts before dozing off */
594 virtqueue_enable_cb(vrp->svq);
596 mutex_unlock(&vrp->tx_lock);
600 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
601 * @vrp: virtual remote processor state
603 * This function is called after a sender, that waited for a tx buffer
604 * to become available, is unblocked.
606 * If we still have blocking senders, this function merely decreases
607 * the "sleepers" reference count, and exits.
609 * Otherwise, if there are no more blocking senders, we also disable
610 * virtio's tx callbacks, to avoid the overhead incurred with handling
611 * those (now redundant) interrupts.
613 static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
615 /* support multiple concurrent senders */
616 mutex_lock(&vrp->tx_lock);
618 /* are we the last sleeping context waiting for tx buffers ? */
619 if (atomic_dec_and_test(&vrp->sleepers))
620 /* disable "tx-complete" interrupts */
621 virtqueue_disable_cb(vrp->svq);
623 mutex_unlock(&vrp->tx_lock);
627 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
628 * @rpdev: the rpmsg channel
629 * @src: source address
630 * @dst: destination address
631 * @data: payload of message
632 * @len: length of payload
633 * @wait: indicates whether caller should block in case no TX buffers available
635 * This function is the base implementation for all of the rpmsg sending API.
637 * It will send @data of length @len to @dst, and say it's from @src. The
638 * message will be sent to the remote processor which the @rpdev channel
641 * The message is sent using one of the TX buffers that are available for
642 * communication with this remote processor.
644 * If @wait is true, the caller will be blocked until either a TX buffer is
645 * available, or 15 seconds elapses (we don't want callers to
646 * sleep indefinitely due to misbehaving remote processors), and in that
647 * case -ERESTARTSYS is returned. The number '15' itself was picked
648 * arbitrarily; there's little point in asking drivers to provide a timeout
651 * Otherwise, if @wait is false, and there are no TX buffers available,
652 * the function will immediately fail, and -ENOMEM will be returned.
654 * Normally drivers shouldn't use this function directly; instead, drivers
655 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
656 * (see include/linux/rpmsg.h).
658 * Returns 0 on success and an appropriate error value on failure.
660 int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev, u32 src, u32 dst,
661 void *data, int len, bool wait)
663 struct virtproc_info *vrp = rpdev->vrp;
664 struct device *dev = &rpdev->dev;
665 struct scatterlist sg;
666 struct rpmsg_hdr *msg;
669 /* bcasting isn't allowed */
670 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
671 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
676 * We currently use fixed-sized buffers, and therefore the payload
679 * One of the possible improvements here is either to support
680 * user-provided buffers (and then we can also support zero-copy
681 * messaging), or to improve the buffer allocator, to support
682 * variable-length buffer sizes.
684 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
685 dev_err(dev, "message is too big (%d)\n", len);
690 msg = get_a_tx_buf(vrp);
694 /* no free buffer ? wait for one (but bail after 15 seconds) */
696 /* enable "tx-complete" interrupts, if not already enabled */
697 rpmsg_upref_sleepers(vrp);
700 * sleep until a free buffer is available or 15 secs elapse.
701 * the timeout period is not configurable because there's
702 * little point in asking drivers to specify that.
703 * if later this happens to be required, it'd be easy to add.
705 err = wait_event_interruptible_timeout(vrp->sendq,
706 (msg = get_a_tx_buf(vrp)),
707 msecs_to_jiffies(15000));
709 /* disable "tx-complete" interrupts if we're the last sleeper */
710 rpmsg_downref_sleepers(vrp);
714 dev_err(dev, "timeout waiting for a tx buffer\n");
724 memcpy(msg->data, data, len);
726 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
727 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
728 #if defined(CONFIG_DYNAMIC_DEBUG)
729 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
730 msg, sizeof(*msg) + msg->len, true);
733 sg_init_one(&sg, msg, sizeof(*msg) + len);
735 mutex_lock(&vrp->tx_lock);
737 /* add message to the remote processor's virtqueue */
738 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
741 * need to reclaim the buffer here, otherwise it's lost
742 * (memory won't leak, but rpmsg won't use it again for TX).
743 * this will wait for a buffer management overhaul.
745 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
749 /* tell the remote processor it has a pending message to read */
750 virtqueue_kick(vrp->svq);
752 mutex_unlock(&vrp->tx_lock);
755 EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
757 static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
758 struct rpmsg_hdr *msg, unsigned int len)
760 struct rpmsg_endpoint *ept;
761 struct scatterlist sg;
764 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
765 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
766 #if defined(CONFIG_DYNAMIC_DEBUG)
767 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
768 msg, sizeof(*msg) + msg->len, true);
772 * We currently use fixed-sized buffers, so trivially sanitize
773 * the reported payload length.
775 if (len > RPMSG_BUF_SIZE ||
776 msg->len > (len - sizeof(struct rpmsg_hdr))) {
777 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
781 /* use the dst addr to fetch the callback of the appropriate user */
782 mutex_lock(&vrp->endpoints_lock);
784 ept = idr_find(&vrp->endpoints, msg->dst);
786 /* let's make sure no one deallocates ept while we use it */
788 kref_get(&ept->refcount);
790 mutex_unlock(&vrp->endpoints_lock);
793 /* make sure ept->cb doesn't go away while we use it */
794 mutex_lock(&ept->cb_lock);
797 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
800 mutex_unlock(&ept->cb_lock);
802 /* farewell, ept, we don't need you anymore */
803 kref_put(&ept->refcount, __ept_release);
805 dev_warn(dev, "msg received with no recipient\n");
807 /* publish the real size of the buffer */
808 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
810 /* add the buffer back to the remote processor's virtqueue */
811 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
813 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
820 /* called when an rx buffer is used, and it's time to digest a message */
821 static void rpmsg_recv_done(struct virtqueue *rvq)
823 struct virtproc_info *vrp = rvq->vdev->priv;
824 struct device *dev = &rvq->vdev->dev;
825 struct rpmsg_hdr *msg;
826 unsigned int len, msgs_received = 0;
829 msg = virtqueue_get_buf(rvq, &len);
831 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
836 err = rpmsg_recv_single(vrp, dev, msg, len);
842 msg = virtqueue_get_buf(rvq, &len);
845 dev_dbg(dev, "Received %u messages\n", msgs_received);
847 /* tell the remote processor we added another available rx buffer */
849 virtqueue_kick(vrp->rvq);
853 * This is invoked whenever the remote processor completed processing
854 * a TX msg we just sent it, and the buffer is put back to the used ring.
856 * Normally, though, we suppress this "tx complete" interrupt in order to
857 * avoid the incurred overhead.
859 static void rpmsg_xmit_done(struct virtqueue *svq)
861 struct virtproc_info *vrp = svq->vdev->priv;
863 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
865 /* wake up potential senders that are waiting for a tx buffer */
866 wake_up_interruptible(&vrp->sendq);
869 /* invoked when a name service announcement arrives */
870 static void rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
873 struct rpmsg_ns_msg *msg = data;
874 struct rpmsg_device *newch;
875 struct rpmsg_channel_info chinfo;
876 struct virtproc_info *vrp = priv;
877 struct device *dev = &vrp->vdev->dev;
880 #if defined(CONFIG_DYNAMIC_DEBUG)
881 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
885 if (len != sizeof(*msg)) {
886 dev_err(dev, "malformed ns msg (%d)\n", len);
891 * the name service ept does _not_ belong to a real rpmsg channel,
892 * and is handled by the rpmsg bus itself.
893 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
897 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
901 /* don't trust the remote processor for null terminating the name */
902 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
904 dev_info(dev, "%sing channel %s addr 0x%x\n",
905 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
906 msg->name, msg->addr);
908 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
909 chinfo.src = RPMSG_ADDR_ANY;
910 chinfo.dst = msg->addr;
912 if (msg->flags & RPMSG_NS_DESTROY) {
913 ret = rpmsg_destroy_channel(vrp, &chinfo);
915 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
917 newch = rpmsg_create_channel(vrp, &chinfo);
919 dev_err(dev, "rpmsg_create_channel failed\n");
923 static int rpmsg_probe(struct virtio_device *vdev)
925 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
926 static const char * const names[] = { "input", "output" };
927 struct virtqueue *vqs[2];
928 struct virtproc_info *vrp;
931 size_t total_buf_space;
934 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
940 idr_init(&vrp->endpoints);
941 mutex_init(&vrp->endpoints_lock);
942 mutex_init(&vrp->tx_lock);
943 init_waitqueue_head(&vrp->sendq);
945 /* We expect two virtqueues, rx and tx (and in this order) */
946 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
953 /* we expect symmetric tx/rx vrings */
954 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
955 virtqueue_get_vring_size(vrp->svq));
957 /* we need less buffers if vrings are small */
958 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
959 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
961 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
963 total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
965 /* allocate coherent memory for the buffers */
966 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
967 total_buf_space, &vrp->bufs_dma,
974 dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
975 bufs_va, &vrp->bufs_dma);
977 /* half of the buffers is dedicated for RX */
978 vrp->rbufs = bufs_va;
980 /* and half is dedicated for TX */
981 vrp->sbufs = bufs_va + total_buf_space / 2;
983 /* set up the receive buffers */
984 for (i = 0; i < vrp->num_bufs / 2; i++) {
985 struct scatterlist sg;
986 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
988 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
990 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
992 WARN_ON(err); /* sanity check; this can't really happen */
995 /* suppress "tx-complete" interrupts */
996 virtqueue_disable_cb(vrp->svq);
1000 /* if supported by the remote processor, enable the name service */
1001 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
1002 /* a dedicated endpoint handles the name service msgs */
1003 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
1004 vrp, RPMSG_NS_ADDR);
1006 dev_err(&vdev->dev, "failed to create the ns ept\n");
1013 * Prepare to kick but don't notify yet - we can't do this before
1016 notify = virtqueue_kick_prepare(vrp->rvq);
1018 /* From this point on, we can notify and get callbacks. */
1019 virtio_device_ready(vdev);
1021 /* tell the remote processor it can start sending messages */
1023 * this might be concurrent with callbacks, but we are only
1024 * doing notify, not a full kick here, so that's ok.
1027 virtqueue_notify(vrp->rvq);
1029 dev_info(&vdev->dev, "rpmsg host is online\n");
1034 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1035 bufs_va, vrp->bufs_dma);
1037 vdev->config->del_vqs(vrp->vdev);
1043 static int rpmsg_remove_device(struct device *dev, void *data)
1045 device_unregister(dev);
1050 static void rpmsg_remove(struct virtio_device *vdev)
1052 struct virtproc_info *vrp = vdev->priv;
1053 size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
1056 vdev->config->reset(vdev);
1058 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1060 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1063 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1065 idr_destroy(&vrp->endpoints);
1067 vdev->config->del_vqs(vrp->vdev);
1069 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1070 vrp->rbufs, vrp->bufs_dma);
1075 static struct virtio_device_id id_table[] = {
1076 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1080 static unsigned int features[] = {
1084 static struct virtio_driver virtio_ipc_driver = {
1085 .feature_table = features,
1086 .feature_table_size = ARRAY_SIZE(features),
1087 .driver.name = KBUILD_MODNAME,
1088 .driver.owner = THIS_MODULE,
1089 .id_table = id_table,
1090 .probe = rpmsg_probe,
1091 .remove = rpmsg_remove,
1094 static int __init rpmsg_init(void)
1098 ret = bus_register(&rpmsg_bus);
1100 pr_err("failed to register rpmsg bus: %d\n", ret);
1104 ret = register_virtio_driver(&virtio_ipc_driver);
1106 pr_err("failed to register virtio driver: %d\n", ret);
1107 bus_unregister(&rpmsg_bus);
1112 subsys_initcall(rpmsg_init);
1114 static void __exit rpmsg_fini(void)
1116 unregister_virtio_driver(&virtio_ipc_driver);
1117 bus_unregister(&rpmsg_bus);
1119 module_exit(rpmsg_fini);
1121 MODULE_DEVICE_TABLE(virtio, id_table);
1122 MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1123 MODULE_LICENSE("GPL v2");