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,
191 /* for more info, see below documentation of rpmsg_create_ept() */
192 static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
193 struct rpmsg_channel *rpdev, rpmsg_rx_cb_t cb,
194 void *priv, u32 addr)
196 int err, tmpaddr, request;
197 struct rpmsg_endpoint *ept;
198 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
200 if (!idr_pre_get(&vrp->endpoints, GFP_KERNEL))
203 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
205 dev_err(dev, "failed to kzalloc a new ept\n");
213 /* do we need to allocate a local address ? */
214 request = addr == RPMSG_ADDR_ANY ? RPMSG_RESERVED_ADDRESSES : addr;
216 mutex_lock(&vrp->endpoints_lock);
218 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
219 err = idr_get_new_above(&vrp->endpoints, ept, request, &tmpaddr);
221 dev_err(dev, "idr_get_new_above failed: %d\n", err);
225 /* make sure the user's address request is fulfilled, if relevant */
226 if (addr != RPMSG_ADDR_ANY && tmpaddr != addr) {
227 dev_err(dev, "address 0x%x already in use\n", addr);
233 mutex_unlock(&vrp->endpoints_lock);
238 idr_remove(&vrp->endpoints, request);
240 mutex_unlock(&vrp->endpoints_lock);
246 * rpmsg_create_ept() - create a new rpmsg_endpoint
247 * @rpdev: rpmsg channel device
248 * @cb: rx callback handler
249 * @priv: private data for the driver's use
250 * @addr: local rpmsg address to bind with @cb
252 * Every rpmsg address in the system is bound to an rx callback (so when
253 * inbound messages arrive, they are dispatched by the rpmsg bus using the
254 * appropriate callback handler) by means of an rpmsg_endpoint struct.
256 * This function allows drivers to create such an endpoint, and by that,
257 * bind a callback, and possibly some private data too, to an rpmsg address
258 * (either one that is known in advance, or one that will be dynamically
259 * assigned for them).
261 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
262 * is already created for them when they are probed by the rpmsg bus
263 * (using the rx callback provided when they registered to the rpmsg bus).
265 * So things should just work for simple drivers: they already have an
266 * endpoint, their rx callback is bound to their rpmsg address, and when
267 * relevant inbound messages arrive (i.e. messages which their dst address
268 * equals to the src address of their rpmsg channel), the driver's handler
269 * is invoked to process it.
271 * That said, more complicated drivers might do need to allocate
272 * additional rpmsg addresses, and bind them to different rx callbacks.
273 * To accomplish that, those drivers need to call this function.
275 * Drivers should provide their @rpdev channel (so the new endpoint would belong
276 * to the same remote processor their channel belongs to), an rx callback
277 * function, an optional private data (which is provided back when the
278 * rx callback is invoked), and an address they want to bind with the
279 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
280 * dynamically assign them an available rpmsg address (drivers should have
281 * a very good reason why not to always use RPMSG_ADDR_ANY here).
283 * Returns a pointer to the endpoint on success, or NULL on error.
285 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
286 rpmsg_rx_cb_t cb, void *priv, u32 addr)
288 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
290 EXPORT_SYMBOL(rpmsg_create_ept);
293 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
294 * @ept: endpoing to destroy
296 * Should be used by drivers to destroy an rpmsg endpoint previously
297 * created with rpmsg_create_ept().
299 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
301 struct virtproc_info *vrp = ept->rpdev->vrp;
303 mutex_lock(&vrp->endpoints_lock);
304 idr_remove(&vrp->endpoints, ept->addr);
305 mutex_unlock(&vrp->endpoints_lock);
309 EXPORT_SYMBOL(rpmsg_destroy_ept);
312 * when an rpmsg driver is probed with a channel, we seamlessly create
313 * it an endpoint, binding its rx callback to a unique local rpmsg
316 * if we need to, we also announce about this channel to the remote
317 * processor (needed in case the driver is exposing an rpmsg service).
319 static int rpmsg_dev_probe(struct device *dev)
321 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
322 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
323 struct virtproc_info *vrp = rpdev->vrp;
324 struct rpmsg_endpoint *ept;
327 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
329 dev_err(dev, "failed to create endpoint\n");
335 rpdev->src = ept->addr;
337 err = rpdrv->probe(rpdev);
339 dev_err(dev, "%s: failed: %d\n", __func__, err);
340 rpmsg_destroy_ept(ept);
344 /* need to tell remote processor's name service about this channel ? */
345 if (rpdev->announce &&
346 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
347 struct rpmsg_ns_msg nsm;
349 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
350 nsm.addr = rpdev->src;
351 nsm.flags = RPMSG_NS_CREATE;
353 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
355 dev_err(dev, "failed to announce service %d\n", err);
362 static int rpmsg_dev_remove(struct device *dev)
364 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
365 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
366 struct virtproc_info *vrp = rpdev->vrp;
369 /* tell remote processor's name service we're removing this channel */
370 if (rpdev->announce &&
371 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
372 struct rpmsg_ns_msg nsm;
374 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
375 nsm.addr = rpdev->src;
376 nsm.flags = RPMSG_NS_DESTROY;
378 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
380 dev_err(dev, "failed to announce service %d\n", err);
383 rpdrv->remove(rpdev);
385 rpmsg_destroy_ept(rpdev->ept);
390 static struct bus_type rpmsg_bus = {
392 .match = rpmsg_dev_match,
393 .dev_attrs = rpmsg_dev_attrs,
394 .uevent = rpmsg_uevent,
395 .probe = rpmsg_dev_probe,
396 .remove = rpmsg_dev_remove,
400 * register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
401 * @rpdrv: pointer to a struct rpmsg_driver
403 * Returns 0 on success, and an appropriate error value on failure.
405 int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
407 rpdrv->drv.bus = &rpmsg_bus;
408 return driver_register(&rpdrv->drv);
410 EXPORT_SYMBOL(register_rpmsg_driver);
413 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
414 * @rpdrv: pointer to a struct rpmsg_driver
416 * Returns 0 on success, and an appropriate error value on failure.
418 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
420 driver_unregister(&rpdrv->drv);
422 EXPORT_SYMBOL(unregister_rpmsg_driver);
424 static void rpmsg_release_device(struct device *dev)
426 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
432 * match an rpmsg channel with a channel info struct.
433 * this is used to make sure we're not creating rpmsg devices for channels
434 * that already exist.
436 static int rpmsg_channel_match(struct device *dev, void *data)
438 struct rpmsg_channel_info *chinfo = data;
439 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
441 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
444 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
447 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
450 /* found a match ! */
455 * create an rpmsg channel using its name and address info.
456 * this function will be used to create both static and dynamic
459 static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
460 struct rpmsg_channel_info *chinfo)
462 struct rpmsg_channel *rpdev;
463 struct device *tmp, *dev = &vrp->vdev->dev;
466 /* make sure a similar channel doesn't already exist */
467 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
469 /* decrement the matched device's refcount back */
471 dev_err(dev, "channel %s:%x:%x already exist\n",
472 chinfo->name, chinfo->src, chinfo->dst);
476 rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL);
478 pr_err("kzalloc failed\n");
483 rpdev->src = chinfo->src;
484 rpdev->dst = chinfo->dst;
487 * rpmsg server channels has predefined local address (for now),
488 * and their existence needs to be announced remotely
490 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false;
492 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
494 /* very simple device indexing plumbing which is enough for now */
495 dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
497 rpdev->dev.parent = &vrp->vdev->dev;
498 rpdev->dev.bus = &rpmsg_bus;
499 rpdev->dev.release = rpmsg_release_device;
501 ret = device_register(&rpdev->dev);
503 dev_err(dev, "device_register failed: %d\n", ret);
504 put_device(&rpdev->dev);
512 * find an existing channel using its name + address properties,
515 static int rpmsg_destroy_channel(struct virtproc_info *vrp,
516 struct rpmsg_channel_info *chinfo)
518 struct virtio_device *vdev = vrp->vdev;
521 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
525 device_unregister(dev);
532 /* super simple buffer "allocator" that is just enough for now */
533 static void *get_a_tx_buf(struct virtproc_info *vrp)
538 /* support multiple concurrent senders */
539 mutex_lock(&vrp->tx_lock);
542 * either pick the next unused tx buffer
543 * (half of our buffers are used for sending messages)
545 if (vrp->last_sbuf < RPMSG_NUM_BUFS / 2)
546 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
547 /* or recycle a used one */
549 ret = virtqueue_get_buf(vrp->svq, &len);
551 mutex_unlock(&vrp->tx_lock);
557 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
558 * @vrp: virtual remote processor state
560 * This function is called before a sender is blocked, waiting for
561 * a tx buffer to become available.
563 * If we already have blocking senders, this function merely increases
564 * the "sleepers" reference count, and exits.
566 * Otherwise, if this is the first sender to block, we also enable
567 * virtio's tx callbacks, so we'd be immediately notified when a tx
568 * buffer is consumed (we rely on virtio's tx callback in order
569 * to wake up sleeping senders as soon as a tx buffer is used by the
572 static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
574 /* support multiple concurrent senders */
575 mutex_lock(&vrp->tx_lock);
577 /* are we the first sleeping context waiting for tx buffers ? */
578 if (atomic_inc_return(&vrp->sleepers) == 1)
579 /* enable "tx-complete" interrupts before dozing off */
580 virtqueue_enable_cb(vrp->svq);
582 mutex_unlock(&vrp->tx_lock);
586 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
587 * @vrp: virtual remote processor state
589 * This function is called after a sender, that waited for a tx buffer
590 * to become available, is unblocked.
592 * If we still have blocking senders, this function merely decreases
593 * the "sleepers" reference count, and exits.
595 * Otherwise, if there are no more blocking senders, we also disable
596 * virtio's tx callbacks, to avoid the overhead incurred with handling
597 * those (now redundant) interrupts.
599 static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
601 /* support multiple concurrent senders */
602 mutex_lock(&vrp->tx_lock);
604 /* are we the last sleeping context waiting for tx buffers ? */
605 if (atomic_dec_and_test(&vrp->sleepers))
606 /* disable "tx-complete" interrupts */
607 virtqueue_disable_cb(vrp->svq);
609 mutex_unlock(&vrp->tx_lock);
613 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
614 * @rpdev: the rpmsg channel
615 * @src: source address
616 * @dst: destination address
617 * @data: payload of message
618 * @len: length of payload
619 * @wait: indicates whether caller should block in case no TX buffers available
621 * This function is the base implementation for all of the rpmsg sending API.
623 * It will send @data of length @len to @dst, and say it's from @src. The
624 * message will be sent to the remote processor which the @rpdev channel
627 * The message is sent using one of the TX buffers that are available for
628 * communication with this remote processor.
630 * If @wait is true, the caller will be blocked until either a TX buffer is
631 * available, or 15 seconds elapses (we don't want callers to
632 * sleep indefinitely due to misbehaving remote processors), and in that
633 * case -ERESTARTSYS is returned. The number '15' itself was picked
634 * arbitrarily; there's little point in asking drivers to provide a timeout
637 * Otherwise, if @wait is false, and there are no TX buffers available,
638 * the function will immediately fail, and -ENOMEM will be returned.
640 * Normally drivers shouldn't use this function directly; instead, drivers
641 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
642 * (see include/linux/rpmsg.h).
644 * Returns 0 on success and an appropriate error value on failure.
646 int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
647 void *data, int len, bool wait)
649 struct virtproc_info *vrp = rpdev->vrp;
650 struct device *dev = &rpdev->dev;
651 struct scatterlist sg;
652 struct rpmsg_hdr *msg;
655 /* bcasting isn't allowed */
656 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
657 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
662 * We currently use fixed-sized buffers, and therefore the payload
665 * One of the possible improvements here is either to support
666 * user-provided buffers (and then we can also support zero-copy
667 * messaging), or to improve the buffer allocator, to support
668 * variable-length buffer sizes.
670 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
671 dev_err(dev, "message is too big (%d)\n", len);
676 msg = get_a_tx_buf(vrp);
680 /* no free buffer ? wait for one (but bail after 15 seconds) */
682 /* enable "tx-complete" interrupts, if not already enabled */
683 rpmsg_upref_sleepers(vrp);
686 * sleep until a free buffer is available or 15 secs elapse.
687 * the timeout period is not configurable because there's
688 * little point in asking drivers to specify that.
689 * if later this happens to be required, it'd be easy to add.
691 err = wait_event_interruptible_timeout(vrp->sendq,
692 (msg = get_a_tx_buf(vrp)),
693 msecs_to_jiffies(15000));
695 /* disable "tx-complete" interrupts if we're the last sleeper */
696 rpmsg_downref_sleepers(vrp);
700 dev_err(dev, "timeout waiting for a tx buffer\n");
710 memcpy(msg->data, data, len);
712 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
713 msg->src, msg->dst, msg->len,
714 msg->flags, msg->reserved);
715 print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
716 msg, sizeof(*msg) + msg->len, true);
718 sg_init_one(&sg, msg, sizeof(*msg) + len);
720 mutex_lock(&vrp->tx_lock);
722 /* add message to the remote processor's virtqueue */
723 err = virtqueue_add_buf_gfp(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
726 * need to reclaim the buffer here, otherwise it's lost
727 * (memory won't leak, but rpmsg won't use it again for TX).
728 * this will wait for a buffer management overhaul.
730 dev_err(dev, "virtqueue_add_buf_gfp failed: %d\n", err);
734 /* tell the remote processor it has a pending message to read */
735 virtqueue_kick(vrp->svq);
739 mutex_unlock(&vrp->tx_lock);
742 EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
744 /* called when an rx buffer is used, and it's time to digest a message */
745 static void rpmsg_recv_done(struct virtqueue *rvq)
747 struct rpmsg_hdr *msg;
749 struct rpmsg_endpoint *ept;
750 struct scatterlist sg;
751 struct virtproc_info *vrp = rvq->vdev->priv;
752 struct device *dev = &rvq->vdev->dev;
755 msg = virtqueue_get_buf(rvq, &len);
757 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
761 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
762 msg->src, msg->dst, msg->len,
763 msg->flags, msg->reserved);
764 print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
765 msg, sizeof(*msg) + msg->len, true);
767 /* use the dst addr to fetch the callback of the appropriate user */
768 mutex_lock(&vrp->endpoints_lock);
769 ept = idr_find(&vrp->endpoints, msg->dst);
770 mutex_unlock(&vrp->endpoints_lock);
773 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv, msg->src);
775 dev_warn(dev, "msg received with no recepient\n");
777 sg_init_one(&sg, msg, sizeof(*msg) + len);
779 /* add the buffer back to the remote processor's virtqueue */
780 err = virtqueue_add_buf_gfp(vrp->rvq, &sg, 0, 1, msg, GFP_KERNEL);
782 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
786 /* tell the remote processor we added another available rx buffer */
787 virtqueue_kick(vrp->rvq);
791 * This is invoked whenever the remote processor completed processing
792 * a TX msg we just sent it, and the buffer is put back to the used ring.
794 * Normally, though, we suppress this "tx complete" interrupt in order to
795 * avoid the incurred overhead.
797 static void rpmsg_xmit_done(struct virtqueue *svq)
799 struct virtproc_info *vrp = svq->vdev->priv;
801 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
803 /* wake up potential senders that are waiting for a tx buffer */
804 wake_up_interruptible(&vrp->sendq);
807 /* invoked when a name service announcement arrives */
808 static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
811 struct rpmsg_ns_msg *msg = data;
812 struct rpmsg_channel *newch;
813 struct rpmsg_channel_info chinfo;
814 struct virtproc_info *vrp = priv;
815 struct device *dev = &vrp->vdev->dev;
818 print_hex_dump(KERN_DEBUG, "NS announcement: ",
819 DUMP_PREFIX_NONE, 16, 1,
822 if (len != sizeof(*msg)) {
823 dev_err(dev, "malformed ns msg (%d)\n", len);
828 * the name service ept does _not_ belong to a real rpmsg channel,
829 * and is handled by the rpmsg bus itself.
830 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
834 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
838 /* don't trust the remote processor for null terminating the name */
839 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
841 dev_info(dev, "%sing channel %s addr 0x%x\n",
842 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
843 msg->name, msg->addr);
845 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
846 chinfo.src = RPMSG_ADDR_ANY;
847 chinfo.dst = msg->addr;
849 if (msg->flags & RPMSG_NS_DESTROY) {
850 ret = rpmsg_destroy_channel(vrp, &chinfo);
852 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
854 newch = rpmsg_create_channel(vrp, &chinfo);
856 dev_err(dev, "rpmsg_create_channel failed\n");
860 static int rpmsg_probe(struct virtio_device *vdev)
862 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
863 const char *names[] = { "input", "output" };
864 struct virtqueue *vqs[2];
865 struct virtproc_info *vrp;
869 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
875 idr_init(&vrp->endpoints);
876 mutex_init(&vrp->endpoints_lock);
877 mutex_init(&vrp->tx_lock);
878 init_waitqueue_head(&vrp->sendq);
880 /* We expect two virtqueues, rx and tx (and in this order) */
881 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
888 /* allocate coherent memory for the buffers */
889 bufs_va = dma_alloc_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
890 &vrp->bufs_dma, GFP_KERNEL);
894 dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%x\n", bufs_va,
897 /* half of the buffers is dedicated for RX */
898 vrp->rbufs = bufs_va;
900 /* and half is dedicated for TX */
901 vrp->sbufs = bufs_va + RPMSG_TOTAL_BUF_SPACE / 2;
903 /* set up the receive buffers */
904 for (i = 0; i < RPMSG_NUM_BUFS / 2; i++) {
905 struct scatterlist sg;
906 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
908 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
910 err = virtqueue_add_buf_gfp(vrp->rvq, &sg, 0, 1, cpu_addr,
912 WARN_ON(err < 0); /* sanity check; this can't really happen */
915 /* suppress "tx-complete" interrupts */
916 virtqueue_disable_cb(vrp->svq);
920 /* if supported by the remote processor, enable the name service */
921 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
922 /* a dedicated endpoint handles the name service msgs */
923 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
926 dev_err(&vdev->dev, "failed to create the ns ept\n");
932 /* tell the remote processor it can start sending messages */
933 virtqueue_kick(vrp->rvq);
935 dev_info(&vdev->dev, "rpmsg host is online\n");
940 dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE, bufs_va,
943 vdev->config->del_vqs(vrp->vdev);
949 static int rpmsg_remove_device(struct device *dev, void *data)
951 device_unregister(dev);
956 static void __devexit rpmsg_remove(struct virtio_device *vdev)
958 struct virtproc_info *vrp = vdev->priv;
961 vdev->config->reset(vdev);
963 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
965 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
967 idr_remove_all(&vrp->endpoints);
968 idr_destroy(&vrp->endpoints);
970 vdev->config->del_vqs(vrp->vdev);
972 dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
973 vrp->rbufs, vrp->bufs_dma);
978 static struct virtio_device_id id_table[] = {
979 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
983 static unsigned int features[] = {
987 static struct virtio_driver virtio_ipc_driver = {
988 .feature_table = features,
989 .feature_table_size = ARRAY_SIZE(features),
990 .driver.name = KBUILD_MODNAME,
991 .driver.owner = THIS_MODULE,
992 .id_table = id_table,
993 .probe = rpmsg_probe,
994 .remove = __devexit_p(rpmsg_remove),
997 static int __init rpmsg_init(void)
1001 ret = bus_register(&rpmsg_bus);
1003 pr_err("failed to register rpmsg bus: %d\n", ret);
1007 ret = register_virtio_driver(&virtio_ipc_driver);
1009 pr_err("failed to register virtio driver: %d\n", ret);
1010 bus_unregister(&rpmsg_bus);
1015 module_init(rpmsg_init);
1017 static void __exit rpmsg_fini(void)
1019 unregister_virtio_driver(&virtio_ipc_driver);
1020 bus_unregister(&rpmsg_bus);
1022 module_exit(rpmsg_fini);
1024 MODULE_DEVICE_TABLE(virtio, id_table);
1025 MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1026 MODULE_LICENSE("GPL v2");