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>
38 * struct virtproc_info - virtual remote processor state
39 * @vdev: the virtio device
42 * @rbufs: kernel address of rx buffers
43 * @sbufs: kernel address of tx buffers
44 * @last_sbuf: index of last tx buffer used
45 * @bufs_dma: dma base addr of the buffers
46 * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
47 * sending a message might require waking up a dozing remote
48 * processor, which involves sleeping, hence the mutex.
49 * @endpoints: idr of local endpoints, allows fast retrieval
50 * @endpoints_lock: lock of the endpoints set
51 * @sendq: wait queue of sending contexts waiting for a tx buffers
52 * @sleepers: number of senders that are waiting for a tx buffer
53 * @ns_ept: the bus's name service endpoint
55 * This structure stores the rpmsg state of a given virtio remote processor
56 * device (there might be several virtio proc devices for each physical
59 struct virtproc_info {
60 struct virtio_device *vdev;
61 struct virtqueue *rvq, *svq;
67 struct mutex endpoints_lock;
68 wait_queue_head_t sendq;
70 struct rpmsg_endpoint *ns_ept;
74 * struct rpmsg_channel_info - internal channel info representation
75 * @name: name of service
77 * @dst: destination address
79 struct rpmsg_channel_info {
80 char name[RPMSG_NAME_SIZE];
85 #define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev)
86 #define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
89 * We're allocating 512 buffers of 512 bytes for communications, and then
90 * using the first 256 buffers for RX, and the last 256 buffers for TX.
92 * Each buffer will have 16 bytes for the msg header and 496 bytes for
95 * This will require a total space of 256KB for the buffers.
97 * We might also want to add support for user-provided buffers in time.
98 * This will allow bigger buffer size flexibility, and can also be used
99 * to achieve zero-copy messaging.
101 * Note that these numbers are purely a decision of this driver - we
102 * can change this without changing anything in the firmware of the remote
105 #define RPMSG_NUM_BUFS (512)
106 #define RPMSG_BUF_SIZE (512)
107 #define RPMSG_TOTAL_BUF_SPACE (RPMSG_NUM_BUFS * RPMSG_BUF_SIZE)
110 * Local addresses are dynamically allocated on-demand.
111 * We do not dynamically assign addresses from the low 1024 range,
112 * in order to reserve that address range for predefined services.
114 #define RPMSG_RESERVED_ADDRESSES (1024)
116 /* Address 53 is reserved for advertising remote services */
117 #define RPMSG_NS_ADDR (53)
119 /* sysfs show configuration fields */
120 #define rpmsg_show_attr(field, path, format_string) \
122 field##_show(struct device *dev, \
123 struct device_attribute *attr, char *buf) \
125 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
127 return sprintf(buf, format_string, rpdev->path); \
130 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
131 rpmsg_show_attr(name, id.name, "%s\n");
132 rpmsg_show_attr(src, src, "0x%x\n");
133 rpmsg_show_attr(dst, dst, "0x%x\n");
134 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
137 * Unique (and free running) index for rpmsg devices.
139 * Yeah, we're not recycling those numbers (yet?). will be easy
140 * to change if/when we want to.
142 static unsigned int rpmsg_dev_index;
144 static ssize_t modalias_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
147 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
149 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
152 static struct device_attribute rpmsg_dev_attrs[] = {
161 /* rpmsg devices and drivers are matched using the service name */
162 static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
163 const struct rpmsg_device_id *id)
165 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
168 /* match rpmsg channel and rpmsg driver */
169 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
171 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
172 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
173 const struct rpmsg_device_id *ids = rpdrv->id_table;
176 for (i = 0; ids[i].name[0]; i++)
177 if (rpmsg_id_match(rpdev, &ids[i]))
183 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
185 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
187 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
192 * __ept_release() - deallocate an rpmsg endpoint
193 * @kref: the ept's reference count
195 * This function deallocates an ept, and is invoked when its @kref refcount
198 * Never invoke this function directly!
200 static void __ept_release(struct kref *kref)
202 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
205 * At this point no one holds a reference to ept anymore,
206 * so we can directly free it
211 /* for more info, see below documentation of rpmsg_create_ept() */
212 static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
213 struct rpmsg_channel *rpdev, rpmsg_rx_cb_t cb,
214 void *priv, u32 addr)
216 int id_min, id_max, id;
217 struct rpmsg_endpoint *ept;
218 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
220 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
222 dev_err(dev, "failed to kzalloc a new ept\n");
226 kref_init(&ept->refcount);
227 mutex_init(&ept->cb_lock);
233 /* do we need to allocate a local address ? */
234 if (addr == RPMSG_ADDR_ANY) {
235 id_min = RPMSG_RESERVED_ADDRESSES;
242 mutex_lock(&vrp->endpoints_lock);
244 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
245 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
247 dev_err(dev, "idr_alloc failed: %d\n", id);
252 mutex_unlock(&vrp->endpoints_lock);
257 mutex_unlock(&vrp->endpoints_lock);
258 kref_put(&ept->refcount, __ept_release);
263 * rpmsg_create_ept() - create a new rpmsg_endpoint
264 * @rpdev: rpmsg channel device
265 * @cb: rx callback handler
266 * @priv: private data for the driver's use
267 * @addr: local rpmsg address to bind with @cb
269 * Every rpmsg address in the system is bound to an rx callback (so when
270 * inbound messages arrive, they are dispatched by the rpmsg bus using the
271 * appropriate callback handler) by means of an rpmsg_endpoint struct.
273 * This function allows drivers to create such an endpoint, and by that,
274 * bind a callback, and possibly some private data too, to an rpmsg address
275 * (either one that is known in advance, or one that will be dynamically
276 * assigned for them).
278 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
279 * is already created for them when they are probed by the rpmsg bus
280 * (using the rx callback provided when they registered to the rpmsg bus).
282 * So things should just work for simple drivers: they already have an
283 * endpoint, their rx callback is bound to their rpmsg address, and when
284 * relevant inbound messages arrive (i.e. messages which their dst address
285 * equals to the src address of their rpmsg channel), the driver's handler
286 * is invoked to process it.
288 * That said, more complicated drivers might do need to allocate
289 * additional rpmsg addresses, and bind them to different rx callbacks.
290 * To accomplish that, those drivers need to call this function.
292 * Drivers should provide their @rpdev channel (so the new endpoint would belong
293 * to the same remote processor their channel belongs to), an rx callback
294 * function, an optional private data (which is provided back when the
295 * rx callback is invoked), and an address they want to bind with the
296 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
297 * dynamically assign them an available rpmsg address (drivers should have
298 * a very good reason why not to always use RPMSG_ADDR_ANY here).
300 * Returns a pointer to the endpoint on success, or NULL on error.
302 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
303 rpmsg_rx_cb_t cb, void *priv, u32 addr)
305 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
307 EXPORT_SYMBOL(rpmsg_create_ept);
310 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
311 * @vrp: virtproc which owns this ept
312 * @ept: endpoing to destroy
314 * An internal function which destroy an ept without assuming it is
315 * bound to an rpmsg channel. This is needed for handling the internal
316 * name service endpoint, which isn't bound to an rpmsg channel.
317 * See also __rpmsg_create_ept().
320 __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
322 /* make sure new inbound messages can't find this ept anymore */
323 mutex_lock(&vrp->endpoints_lock);
324 idr_remove(&vrp->endpoints, ept->addr);
325 mutex_unlock(&vrp->endpoints_lock);
327 /* make sure in-flight inbound messages won't invoke cb anymore */
328 mutex_lock(&ept->cb_lock);
330 mutex_unlock(&ept->cb_lock);
332 kref_put(&ept->refcount, __ept_release);
336 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
337 * @ept: endpoing to destroy
339 * Should be used by drivers to destroy an rpmsg endpoint previously
340 * created with rpmsg_create_ept().
342 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
344 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
346 EXPORT_SYMBOL(rpmsg_destroy_ept);
349 * when an rpmsg driver is probed with a channel, we seamlessly create
350 * it an endpoint, binding its rx callback to a unique local rpmsg
353 * if we need to, we also announce about this channel to the remote
354 * processor (needed in case the driver is exposing an rpmsg service).
356 static int rpmsg_dev_probe(struct device *dev)
358 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
359 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
360 struct virtproc_info *vrp = rpdev->vrp;
361 struct rpmsg_endpoint *ept;
364 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
366 dev_err(dev, "failed to create endpoint\n");
372 rpdev->src = ept->addr;
374 err = rpdrv->probe(rpdev);
376 dev_err(dev, "%s: failed: %d\n", __func__, err);
377 rpmsg_destroy_ept(ept);
381 /* need to tell remote processor's name service about this channel ? */
382 if (rpdev->announce &&
383 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
384 struct rpmsg_ns_msg nsm;
386 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
387 nsm.addr = rpdev->src;
388 nsm.flags = RPMSG_NS_CREATE;
390 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
392 dev_err(dev, "failed to announce service %d\n", err);
399 static int rpmsg_dev_remove(struct device *dev)
401 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
402 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
403 struct virtproc_info *vrp = rpdev->vrp;
406 /* tell remote processor's name service we're removing this channel */
407 if (rpdev->announce &&
408 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
409 struct rpmsg_ns_msg nsm;
411 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
412 nsm.addr = rpdev->src;
413 nsm.flags = RPMSG_NS_DESTROY;
415 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
417 dev_err(dev, "failed to announce service %d\n", err);
420 rpdrv->remove(rpdev);
422 rpmsg_destroy_ept(rpdev->ept);
427 static struct bus_type rpmsg_bus = {
429 .match = rpmsg_dev_match,
430 .dev_attrs = rpmsg_dev_attrs,
431 .uevent = rpmsg_uevent,
432 .probe = rpmsg_dev_probe,
433 .remove = rpmsg_dev_remove,
437 * register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
438 * @rpdrv: pointer to a struct rpmsg_driver
440 * Returns 0 on success, and an appropriate error value on failure.
442 int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
444 rpdrv->drv.bus = &rpmsg_bus;
445 return driver_register(&rpdrv->drv);
447 EXPORT_SYMBOL(register_rpmsg_driver);
450 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
451 * @rpdrv: pointer to a struct rpmsg_driver
453 * Returns 0 on success, and an appropriate error value on failure.
455 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
457 driver_unregister(&rpdrv->drv);
459 EXPORT_SYMBOL(unregister_rpmsg_driver);
461 static void rpmsg_release_device(struct device *dev)
463 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
469 * match an rpmsg channel with a channel info struct.
470 * this is used to make sure we're not creating rpmsg devices for channels
471 * that already exist.
473 static int rpmsg_channel_match(struct device *dev, void *data)
475 struct rpmsg_channel_info *chinfo = data;
476 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
478 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
481 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
484 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
487 /* found a match ! */
492 * create an rpmsg channel using its name and address info.
493 * this function will be used to create both static and dynamic
496 static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
497 struct rpmsg_channel_info *chinfo)
499 struct rpmsg_channel *rpdev;
500 struct device *tmp, *dev = &vrp->vdev->dev;
503 /* make sure a similar channel doesn't already exist */
504 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
506 /* decrement the matched device's refcount back */
508 dev_err(dev, "channel %s:%x:%x already exist\n",
509 chinfo->name, chinfo->src, chinfo->dst);
513 rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL);
515 pr_err("kzalloc failed\n");
520 rpdev->src = chinfo->src;
521 rpdev->dst = chinfo->dst;
524 * rpmsg server channels has predefined local address (for now),
525 * and their existence needs to be announced remotely
527 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false;
529 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
531 /* very simple device indexing plumbing which is enough for now */
532 dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
534 rpdev->dev.parent = &vrp->vdev->dev;
535 rpdev->dev.bus = &rpmsg_bus;
536 rpdev->dev.release = rpmsg_release_device;
538 ret = device_register(&rpdev->dev);
540 dev_err(dev, "device_register failed: %d\n", ret);
541 put_device(&rpdev->dev);
549 * find an existing channel using its name + address properties,
552 static int rpmsg_destroy_channel(struct virtproc_info *vrp,
553 struct rpmsg_channel_info *chinfo)
555 struct virtio_device *vdev = vrp->vdev;
558 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
562 device_unregister(dev);
569 /* super simple buffer "allocator" that is just enough for now */
570 static void *get_a_tx_buf(struct virtproc_info *vrp)
575 /* support multiple concurrent senders */
576 mutex_lock(&vrp->tx_lock);
579 * either pick the next unused tx buffer
580 * (half of our buffers are used for sending messages)
582 if (vrp->last_sbuf < RPMSG_NUM_BUFS / 2)
583 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
584 /* or recycle a used one */
586 ret = virtqueue_get_buf(vrp->svq, &len);
588 mutex_unlock(&vrp->tx_lock);
594 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
595 * @vrp: virtual remote processor state
597 * This function is called before a sender is blocked, waiting for
598 * a tx buffer to become available.
600 * If we already have blocking senders, this function merely increases
601 * the "sleepers" reference count, and exits.
603 * Otherwise, if this is the first sender to block, we also enable
604 * virtio's tx callbacks, so we'd be immediately notified when a tx
605 * buffer is consumed (we rely on virtio's tx callback in order
606 * to wake up sleeping senders as soon as a tx buffer is used by the
609 static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
611 /* support multiple concurrent senders */
612 mutex_lock(&vrp->tx_lock);
614 /* are we the first sleeping context waiting for tx buffers ? */
615 if (atomic_inc_return(&vrp->sleepers) == 1)
616 /* enable "tx-complete" interrupts before dozing off */
617 virtqueue_enable_cb(vrp->svq);
619 mutex_unlock(&vrp->tx_lock);
623 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
624 * @vrp: virtual remote processor state
626 * This function is called after a sender, that waited for a tx buffer
627 * to become available, is unblocked.
629 * If we still have blocking senders, this function merely decreases
630 * the "sleepers" reference count, and exits.
632 * Otherwise, if there are no more blocking senders, we also disable
633 * virtio's tx callbacks, to avoid the overhead incurred with handling
634 * those (now redundant) interrupts.
636 static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
638 /* support multiple concurrent senders */
639 mutex_lock(&vrp->tx_lock);
641 /* are we the last sleeping context waiting for tx buffers ? */
642 if (atomic_dec_and_test(&vrp->sleepers))
643 /* disable "tx-complete" interrupts */
644 virtqueue_disable_cb(vrp->svq);
646 mutex_unlock(&vrp->tx_lock);
650 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
651 * @rpdev: the rpmsg channel
652 * @src: source address
653 * @dst: destination address
654 * @data: payload of message
655 * @len: length of payload
656 * @wait: indicates whether caller should block in case no TX buffers available
658 * This function is the base implementation for all of the rpmsg sending API.
660 * It will send @data of length @len to @dst, and say it's from @src. The
661 * message will be sent to the remote processor which the @rpdev channel
664 * The message is sent using one of the TX buffers that are available for
665 * communication with this remote processor.
667 * If @wait is true, the caller will be blocked until either a TX buffer is
668 * available, or 15 seconds elapses (we don't want callers to
669 * sleep indefinitely due to misbehaving remote processors), and in that
670 * case -ERESTARTSYS is returned. The number '15' itself was picked
671 * arbitrarily; there's little point in asking drivers to provide a timeout
674 * Otherwise, if @wait is false, and there are no TX buffers available,
675 * the function will immediately fail, and -ENOMEM will be returned.
677 * Normally drivers shouldn't use this function directly; instead, drivers
678 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
679 * (see include/linux/rpmsg.h).
681 * Returns 0 on success and an appropriate error value on failure.
683 int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
684 void *data, int len, bool wait)
686 struct virtproc_info *vrp = rpdev->vrp;
687 struct device *dev = &rpdev->dev;
688 struct scatterlist sg;
689 struct rpmsg_hdr *msg;
692 /* bcasting isn't allowed */
693 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
694 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
699 * We currently use fixed-sized buffers, and therefore the payload
702 * One of the possible improvements here is either to support
703 * user-provided buffers (and then we can also support zero-copy
704 * messaging), or to improve the buffer allocator, to support
705 * variable-length buffer sizes.
707 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
708 dev_err(dev, "message is too big (%d)\n", len);
713 msg = get_a_tx_buf(vrp);
717 /* no free buffer ? wait for one (but bail after 15 seconds) */
719 /* enable "tx-complete" interrupts, if not already enabled */
720 rpmsg_upref_sleepers(vrp);
723 * sleep until a free buffer is available or 15 secs elapse.
724 * the timeout period is not configurable because there's
725 * little point in asking drivers to specify that.
726 * if later this happens to be required, it'd be easy to add.
728 err = wait_event_interruptible_timeout(vrp->sendq,
729 (msg = get_a_tx_buf(vrp)),
730 msecs_to_jiffies(15000));
732 /* disable "tx-complete" interrupts if we're the last sleeper */
733 rpmsg_downref_sleepers(vrp);
737 dev_err(dev, "timeout waiting for a tx buffer\n");
747 memcpy(msg->data, data, len);
749 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
750 msg->src, msg->dst, msg->len,
751 msg->flags, msg->reserved);
752 print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
753 msg, sizeof(*msg) + msg->len, true);
755 sg_init_one(&sg, msg, sizeof(*msg) + len);
757 mutex_lock(&vrp->tx_lock);
759 /* add message to the remote processor's virtqueue */
760 err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
763 * need to reclaim the buffer here, otherwise it's lost
764 * (memory won't leak, but rpmsg won't use it again for TX).
765 * this will wait for a buffer management overhaul.
767 dev_err(dev, "virtqueue_add_buf failed: %d\n", err);
771 /* tell the remote processor it has a pending message to read */
772 virtqueue_kick(vrp->svq);
774 mutex_unlock(&vrp->tx_lock);
777 EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
779 /* called when an rx buffer is used, and it's time to digest a message */
780 static void rpmsg_recv_done(struct virtqueue *rvq)
782 struct rpmsg_hdr *msg;
784 struct rpmsg_endpoint *ept;
785 struct scatterlist sg;
786 struct virtproc_info *vrp = rvq->vdev->priv;
787 struct device *dev = &rvq->vdev->dev;
790 msg = virtqueue_get_buf(rvq, &len);
792 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
796 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
797 msg->src, msg->dst, msg->len,
798 msg->flags, msg->reserved);
799 print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
800 msg, sizeof(*msg) + msg->len, true);
803 * We currently use fixed-sized buffers, so trivially sanitize
804 * the reported payload length.
806 if (len > RPMSG_BUF_SIZE ||
807 msg->len > (len - sizeof(struct rpmsg_hdr))) {
808 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
812 /* use the dst addr to fetch the callback of the appropriate user */
813 mutex_lock(&vrp->endpoints_lock);
815 ept = idr_find(&vrp->endpoints, msg->dst);
817 /* let's make sure no one deallocates ept while we use it */
819 kref_get(&ept->refcount);
821 mutex_unlock(&vrp->endpoints_lock);
824 /* make sure ept->cb doesn't go away while we use it */
825 mutex_lock(&ept->cb_lock);
828 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
831 mutex_unlock(&ept->cb_lock);
833 /* farewell, ept, we don't need you anymore */
834 kref_put(&ept->refcount, __ept_release);
836 dev_warn(dev, "msg received with no recipient\n");
838 /* publish the real size of the buffer */
839 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
841 /* add the buffer back to the remote processor's virtqueue */
842 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, msg, GFP_KERNEL);
844 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
848 /* 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_channel *rpdev, void *data, int len,
873 struct rpmsg_ns_msg *msg = data;
874 struct rpmsg_channel *newch;
875 struct rpmsg_channel_info chinfo;
876 struct virtproc_info *vrp = priv;
877 struct device *dev = &vrp->vdev->dev;
880 print_hex_dump(KERN_DEBUG, "NS announcement: ",
881 DUMP_PREFIX_NONE, 16, 1,
884 if (len != sizeof(*msg)) {
885 dev_err(dev, "malformed ns msg (%d)\n", len);
890 * the name service ept does _not_ belong to a real rpmsg channel,
891 * and is handled by the rpmsg bus itself.
892 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
896 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
900 /* don't trust the remote processor for null terminating the name */
901 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
903 dev_info(dev, "%sing channel %s addr 0x%x\n",
904 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
905 msg->name, msg->addr);
907 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
908 chinfo.src = RPMSG_ADDR_ANY;
909 chinfo.dst = msg->addr;
911 if (msg->flags & RPMSG_NS_DESTROY) {
912 ret = rpmsg_destroy_channel(vrp, &chinfo);
914 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
916 newch = rpmsg_create_channel(vrp, &chinfo);
918 dev_err(dev, "rpmsg_create_channel failed\n");
922 static int rpmsg_probe(struct virtio_device *vdev)
924 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
925 const char *names[] = { "input", "output" };
926 struct virtqueue *vqs[2];
927 struct virtproc_info *vrp;
931 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
937 idr_init(&vrp->endpoints);
938 mutex_init(&vrp->endpoints_lock);
939 mutex_init(&vrp->tx_lock);
940 init_waitqueue_head(&vrp->sendq);
942 /* We expect two virtqueues, rx and tx (and in this order) */
943 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
950 /* allocate coherent memory for the buffers */
951 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
952 RPMSG_TOTAL_BUF_SPACE,
953 &vrp->bufs_dma, GFP_KERNEL);
959 dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%llx\n", bufs_va,
960 (unsigned long long)vrp->bufs_dma);
962 /* half of the buffers is dedicated for RX */
963 vrp->rbufs = bufs_va;
965 /* and half is dedicated for TX */
966 vrp->sbufs = bufs_va + RPMSG_TOTAL_BUF_SPACE / 2;
968 /* set up the receive buffers */
969 for (i = 0; i < RPMSG_NUM_BUFS / 2; i++) {
970 struct scatterlist sg;
971 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
973 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
975 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr,
977 WARN_ON(err); /* sanity check; this can't really happen */
980 /* suppress "tx-complete" interrupts */
981 virtqueue_disable_cb(vrp->svq);
985 /* if supported by the remote processor, enable the name service */
986 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
987 /* a dedicated endpoint handles the name service msgs */
988 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
991 dev_err(&vdev->dev, "failed to create the ns ept\n");
997 /* tell the remote processor it can start sending messages */
998 virtqueue_kick(vrp->rvq);
1000 dev_info(&vdev->dev, "rpmsg host is online\n");
1005 dma_free_coherent(vdev->dev.parent->parent, RPMSG_TOTAL_BUF_SPACE,
1006 bufs_va, vrp->bufs_dma);
1008 vdev->config->del_vqs(vrp->vdev);
1014 static int rpmsg_remove_device(struct device *dev, void *data)
1016 device_unregister(dev);
1021 static void rpmsg_remove(struct virtio_device *vdev)
1023 struct virtproc_info *vrp = vdev->priv;
1026 vdev->config->reset(vdev);
1028 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1030 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1033 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1035 idr_destroy(&vrp->endpoints);
1037 vdev->config->del_vqs(vrp->vdev);
1039 dma_free_coherent(vdev->dev.parent->parent, RPMSG_TOTAL_BUF_SPACE,
1040 vrp->rbufs, vrp->bufs_dma);
1045 static struct virtio_device_id id_table[] = {
1046 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1050 static unsigned int features[] = {
1054 static struct virtio_driver virtio_ipc_driver = {
1055 .feature_table = features,
1056 .feature_table_size = ARRAY_SIZE(features),
1057 .driver.name = KBUILD_MODNAME,
1058 .driver.owner = THIS_MODULE,
1059 .id_table = id_table,
1060 .probe = rpmsg_probe,
1061 .remove = rpmsg_remove,
1064 static int __init rpmsg_init(void)
1068 ret = bus_register(&rpmsg_bus);
1070 pr_err("failed to register rpmsg bus: %d\n", ret);
1074 ret = register_virtio_driver(&virtio_ipc_driver);
1076 pr_err("failed to register virtio driver: %d\n", ret);
1077 bus_unregister(&rpmsg_bus);
1082 subsys_initcall(rpmsg_init);
1084 static void __exit rpmsg_fini(void)
1086 unregister_virtio_driver(&virtio_ipc_driver);
1087 bus_unregister(&rpmsg_bus);
1089 module_exit(rpmsg_fini);
1091 MODULE_DEVICE_TABLE(virtio, id_table);
1092 MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1093 MODULE_LICENSE("GPL v2");