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 * @num_bufs: total number of buffers for rx and tx
45 * @last_sbuf: index of last tx buffer used
46 * @bufs_dma: dma base addr of the buffers
47 * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
48 * sending a message might require waking up a dozing remote
49 * processor, which involves sleeping, hence the mutex.
50 * @endpoints: idr of local endpoints, allows fast retrieval
51 * @endpoints_lock: lock of the endpoints set
52 * @sendq: wait queue of sending contexts waiting for a tx buffers
53 * @sleepers: number of senders that are waiting for a tx buffer
54 * @ns_ept: the bus's name service endpoint
56 * This structure stores the rpmsg state of a given virtio remote processor
57 * device (there might be several virtio proc devices for each physical
60 struct virtproc_info
{
61 struct virtio_device
*vdev
;
62 struct virtqueue
*rvq
, *svq
;
64 unsigned int num_bufs
;
69 struct mutex endpoints_lock
;
70 wait_queue_head_t sendq
;
72 struct rpmsg_endpoint
*ns_ept
;
76 * struct rpmsg_channel_info - internal channel info representation
77 * @name: name of service
79 * @dst: destination address
81 struct rpmsg_channel_info
{
82 char name
[RPMSG_NAME_SIZE
];
87 #define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev)
88 #define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
91 * We're allocating buffers of 512 bytes each for communications. The
92 * number of buffers will be computed from the number of buffers supported
93 * by the vring, upto a maximum of 512 buffers (256 in each direction).
95 * Each buffer will have 16 bytes for the msg header and 496 bytes for
98 * This will utilize a maximum total space of 256KB for the buffers.
100 * We might also want to add support for user-provided buffers in time.
101 * This will allow bigger buffer size flexibility, and can also be used
102 * to achieve zero-copy messaging.
104 * Note that these numbers are purely a decision of this driver - we
105 * can change this without changing anything in the firmware of the remote
108 #define MAX_RPMSG_NUM_BUFS (512)
109 #define RPMSG_BUF_SIZE (512)
112 * Local addresses are dynamically allocated on-demand.
113 * We do not dynamically assign addresses from the low 1024 range,
114 * in order to reserve that address range for predefined services.
116 #define RPMSG_RESERVED_ADDRESSES (1024)
118 /* Address 53 is reserved for advertising remote services */
119 #define RPMSG_NS_ADDR (53)
121 /* sysfs show configuration fields */
122 #define rpmsg_show_attr(field, path, format_string) \
124 field##_show(struct device *dev, \
125 struct device_attribute *attr, char *buf) \
127 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
129 return sprintf(buf, format_string, rpdev->path); \
132 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
133 rpmsg_show_attr(name
, id
.name
, "%s\n");
134 rpmsg_show_attr(src
, src
, "0x%x\n");
135 rpmsg_show_attr(dst
, dst
, "0x%x\n");
136 rpmsg_show_attr(announce
, announce
? "true" : "false", "%s\n");
139 * Unique (and free running) index for rpmsg devices.
141 * Yeah, we're not recycling those numbers (yet?). will be easy
142 * to change if/when we want to.
144 static unsigned int rpmsg_dev_index
;
146 static ssize_t
modalias_show(struct device
*dev
,
147 struct device_attribute
*attr
, char *buf
)
149 struct rpmsg_channel
*rpdev
= to_rpmsg_channel(dev
);
151 return sprintf(buf
, RPMSG_DEVICE_MODALIAS_FMT
"\n", rpdev
->id
.name
);
154 static struct device_attribute rpmsg_dev_attrs
[] = {
163 /* rpmsg devices and drivers are matched using the service name */
164 static inline int rpmsg_id_match(const struct rpmsg_channel
*rpdev
,
165 const struct rpmsg_device_id
*id
)
167 return strncmp(id
->name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
) == 0;
170 /* match rpmsg channel and rpmsg driver */
171 static int rpmsg_dev_match(struct device
*dev
, struct device_driver
*drv
)
173 struct rpmsg_channel
*rpdev
= to_rpmsg_channel(dev
);
174 struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(drv
);
175 const struct rpmsg_device_id
*ids
= rpdrv
->id_table
;
178 for (i
= 0; ids
[i
].name
[0]; i
++)
179 if (rpmsg_id_match(rpdev
, &ids
[i
]))
185 static int rpmsg_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
187 struct rpmsg_channel
*rpdev
= to_rpmsg_channel(dev
);
189 return add_uevent_var(env
, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT
,
194 * __ept_release() - deallocate an rpmsg endpoint
195 * @kref: the ept's reference count
197 * This function deallocates an ept, and is invoked when its @kref refcount
200 * Never invoke this function directly!
202 static void __ept_release(struct kref
*kref
)
204 struct rpmsg_endpoint
*ept
= container_of(kref
, struct rpmsg_endpoint
,
207 * At this point no one holds a reference to ept anymore,
208 * so we can directly free it
213 /* for more info, see below documentation of rpmsg_create_ept() */
214 static struct rpmsg_endpoint
*__rpmsg_create_ept(struct virtproc_info
*vrp
,
215 struct rpmsg_channel
*rpdev
, rpmsg_rx_cb_t cb
,
216 void *priv
, u32 addr
)
218 int id_min
, id_max
, id
;
219 struct rpmsg_endpoint
*ept
;
220 struct device
*dev
= rpdev
? &rpdev
->dev
: &vrp
->vdev
->dev
;
222 ept
= kzalloc(sizeof(*ept
), GFP_KERNEL
);
224 dev_err(dev
, "failed to kzalloc a new ept\n");
228 kref_init(&ept
->refcount
);
229 mutex_init(&ept
->cb_lock
);
235 /* do we need to allocate a local address ? */
236 if (addr
== RPMSG_ADDR_ANY
) {
237 id_min
= RPMSG_RESERVED_ADDRESSES
;
244 mutex_lock(&vrp
->endpoints_lock
);
246 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
247 id
= idr_alloc(&vrp
->endpoints
, ept
, id_min
, id_max
, GFP_KERNEL
);
249 dev_err(dev
, "idr_alloc failed: %d\n", id
);
254 mutex_unlock(&vrp
->endpoints_lock
);
259 mutex_unlock(&vrp
->endpoints_lock
);
260 kref_put(&ept
->refcount
, __ept_release
);
265 * rpmsg_create_ept() - create a new rpmsg_endpoint
266 * @rpdev: rpmsg channel device
267 * @cb: rx callback handler
268 * @priv: private data for the driver's use
269 * @addr: local rpmsg address to bind with @cb
271 * Every rpmsg address in the system is bound to an rx callback (so when
272 * inbound messages arrive, they are dispatched by the rpmsg bus using the
273 * appropriate callback handler) by means of an rpmsg_endpoint struct.
275 * This function allows drivers to create such an endpoint, and by that,
276 * bind a callback, and possibly some private data too, to an rpmsg address
277 * (either one that is known in advance, or one that will be dynamically
278 * assigned for them).
280 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
281 * is already created for them when they are probed by the rpmsg bus
282 * (using the rx callback provided when they registered to the rpmsg bus).
284 * So things should just work for simple drivers: they already have an
285 * endpoint, their rx callback is bound to their rpmsg address, and when
286 * relevant inbound messages arrive (i.e. messages which their dst address
287 * equals to the src address of their rpmsg channel), the driver's handler
288 * is invoked to process it.
290 * That said, more complicated drivers might do need to allocate
291 * additional rpmsg addresses, and bind them to different rx callbacks.
292 * To accomplish that, those drivers need to call this function.
294 * Drivers should provide their @rpdev channel (so the new endpoint would belong
295 * to the same remote processor their channel belongs to), an rx callback
296 * function, an optional private data (which is provided back when the
297 * rx callback is invoked), and an address they want to bind with the
298 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
299 * dynamically assign them an available rpmsg address (drivers should have
300 * a very good reason why not to always use RPMSG_ADDR_ANY here).
302 * Returns a pointer to the endpoint on success, or NULL on error.
304 struct rpmsg_endpoint
*rpmsg_create_ept(struct rpmsg_channel
*rpdev
,
305 rpmsg_rx_cb_t cb
, void *priv
, u32 addr
)
307 return __rpmsg_create_ept(rpdev
->vrp
, rpdev
, cb
, priv
, addr
);
309 EXPORT_SYMBOL(rpmsg_create_ept
);
312 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
313 * @vrp: virtproc which owns this ept
314 * @ept: endpoing to destroy
316 * An internal function which destroy an ept without assuming it is
317 * bound to an rpmsg channel. This is needed for handling the internal
318 * name service endpoint, which isn't bound to an rpmsg channel.
319 * See also __rpmsg_create_ept().
322 __rpmsg_destroy_ept(struct virtproc_info
*vrp
, struct rpmsg_endpoint
*ept
)
324 /* make sure new inbound messages can't find this ept anymore */
325 mutex_lock(&vrp
->endpoints_lock
);
326 idr_remove(&vrp
->endpoints
, ept
->addr
);
327 mutex_unlock(&vrp
->endpoints_lock
);
329 /* make sure in-flight inbound messages won't invoke cb anymore */
330 mutex_lock(&ept
->cb_lock
);
332 mutex_unlock(&ept
->cb_lock
);
334 kref_put(&ept
->refcount
, __ept_release
);
338 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
339 * @ept: endpoing to destroy
341 * Should be used by drivers to destroy an rpmsg endpoint previously
342 * created with rpmsg_create_ept().
344 void rpmsg_destroy_ept(struct rpmsg_endpoint
*ept
)
346 __rpmsg_destroy_ept(ept
->rpdev
->vrp
, ept
);
348 EXPORT_SYMBOL(rpmsg_destroy_ept
);
351 * when an rpmsg driver is probed with a channel, we seamlessly create
352 * it an endpoint, binding its rx callback to a unique local rpmsg
355 * if we need to, we also announce about this channel to the remote
356 * processor (needed in case the driver is exposing an rpmsg service).
358 static int rpmsg_dev_probe(struct device
*dev
)
360 struct rpmsg_channel
*rpdev
= to_rpmsg_channel(dev
);
361 struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(rpdev
->dev
.driver
);
362 struct virtproc_info
*vrp
= rpdev
->vrp
;
363 struct rpmsg_endpoint
*ept
;
366 ept
= rpmsg_create_ept(rpdev
, rpdrv
->callback
, NULL
, rpdev
->src
);
368 dev_err(dev
, "failed to create endpoint\n");
374 rpdev
->src
= ept
->addr
;
376 err
= rpdrv
->probe(rpdev
);
378 dev_err(dev
, "%s: failed: %d\n", __func__
, err
);
379 rpmsg_destroy_ept(ept
);
383 /* need to tell remote processor's name service about this channel ? */
384 if (rpdev
->announce
&&
385 virtio_has_feature(vrp
->vdev
, VIRTIO_RPMSG_F_NS
)) {
386 struct rpmsg_ns_msg nsm
;
388 strncpy(nsm
.name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
);
389 nsm
.addr
= rpdev
->src
;
390 nsm
.flags
= RPMSG_NS_CREATE
;
392 err
= rpmsg_sendto(rpdev
, &nsm
, sizeof(nsm
), RPMSG_NS_ADDR
);
394 dev_err(dev
, "failed to announce service %d\n", err
);
401 static int rpmsg_dev_remove(struct device
*dev
)
403 struct rpmsg_channel
*rpdev
= to_rpmsg_channel(dev
);
404 struct rpmsg_driver
*rpdrv
= to_rpmsg_driver(rpdev
->dev
.driver
);
405 struct virtproc_info
*vrp
= rpdev
->vrp
;
408 /* tell remote processor's name service we're removing this channel */
409 if (rpdev
->announce
&&
410 virtio_has_feature(vrp
->vdev
, VIRTIO_RPMSG_F_NS
)) {
411 struct rpmsg_ns_msg nsm
;
413 strncpy(nsm
.name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
);
414 nsm
.addr
= rpdev
->src
;
415 nsm
.flags
= RPMSG_NS_DESTROY
;
417 err
= rpmsg_sendto(rpdev
, &nsm
, sizeof(nsm
), RPMSG_NS_ADDR
);
419 dev_err(dev
, "failed to announce service %d\n", err
);
422 rpdrv
->remove(rpdev
);
424 rpmsg_destroy_ept(rpdev
->ept
);
429 static struct bus_type rpmsg_bus
= {
431 .match
= rpmsg_dev_match
,
432 .dev_attrs
= rpmsg_dev_attrs
,
433 .uevent
= rpmsg_uevent
,
434 .probe
= rpmsg_dev_probe
,
435 .remove
= rpmsg_dev_remove
,
439 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
440 * @rpdrv: pointer to a struct rpmsg_driver
441 * @owner: owning module/driver
443 * Returns 0 on success, and an appropriate error value on failure.
445 int __register_rpmsg_driver(struct rpmsg_driver
*rpdrv
, struct module
*owner
)
447 rpdrv
->drv
.bus
= &rpmsg_bus
;
448 rpdrv
->drv
.owner
= owner
;
449 return driver_register(&rpdrv
->drv
);
451 EXPORT_SYMBOL(__register_rpmsg_driver
);
454 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
455 * @rpdrv: pointer to a struct rpmsg_driver
457 * Returns 0 on success, and an appropriate error value on failure.
459 void unregister_rpmsg_driver(struct rpmsg_driver
*rpdrv
)
461 driver_unregister(&rpdrv
->drv
);
463 EXPORT_SYMBOL(unregister_rpmsg_driver
);
465 static void rpmsg_release_device(struct device
*dev
)
467 struct rpmsg_channel
*rpdev
= to_rpmsg_channel(dev
);
473 * match an rpmsg channel with a channel info struct.
474 * this is used to make sure we're not creating rpmsg devices for channels
475 * that already exist.
477 static int rpmsg_channel_match(struct device
*dev
, void *data
)
479 struct rpmsg_channel_info
*chinfo
= data
;
480 struct rpmsg_channel
*rpdev
= to_rpmsg_channel(dev
);
482 if (chinfo
->src
!= RPMSG_ADDR_ANY
&& chinfo
->src
!= rpdev
->src
)
485 if (chinfo
->dst
!= RPMSG_ADDR_ANY
&& chinfo
->dst
!= rpdev
->dst
)
488 if (strncmp(chinfo
->name
, rpdev
->id
.name
, RPMSG_NAME_SIZE
))
491 /* found a match ! */
496 * create an rpmsg channel using its name and address info.
497 * this function will be used to create both static and dynamic
500 static struct rpmsg_channel
*rpmsg_create_channel(struct virtproc_info
*vrp
,
501 struct rpmsg_channel_info
*chinfo
)
503 struct rpmsg_channel
*rpdev
;
504 struct device
*tmp
, *dev
= &vrp
->vdev
->dev
;
507 /* make sure a similar channel doesn't already exist */
508 tmp
= device_find_child(dev
, chinfo
, rpmsg_channel_match
);
510 /* decrement the matched device's refcount back */
512 dev_err(dev
, "channel %s:%x:%x already exist\n",
513 chinfo
->name
, chinfo
->src
, chinfo
->dst
);
517 rpdev
= kzalloc(sizeof(struct rpmsg_channel
), GFP_KERNEL
);
519 pr_err("kzalloc failed\n");
524 rpdev
->src
= chinfo
->src
;
525 rpdev
->dst
= chinfo
->dst
;
528 * rpmsg server channels has predefined local address (for now),
529 * and their existence needs to be announced remotely
531 rpdev
->announce
= rpdev
->src
!= RPMSG_ADDR_ANY
? true : false;
533 strncpy(rpdev
->id
.name
, chinfo
->name
, RPMSG_NAME_SIZE
);
535 /* very simple device indexing plumbing which is enough for now */
536 dev_set_name(&rpdev
->dev
, "rpmsg%d", rpmsg_dev_index
++);
538 rpdev
->dev
.parent
= &vrp
->vdev
->dev
;
539 rpdev
->dev
.bus
= &rpmsg_bus
;
540 rpdev
->dev
.release
= rpmsg_release_device
;
542 ret
= device_register(&rpdev
->dev
);
544 dev_err(dev
, "device_register failed: %d\n", ret
);
545 put_device(&rpdev
->dev
);
553 * find an existing channel using its name + address properties,
556 static int rpmsg_destroy_channel(struct virtproc_info
*vrp
,
557 struct rpmsg_channel_info
*chinfo
)
559 struct virtio_device
*vdev
= vrp
->vdev
;
562 dev
= device_find_child(&vdev
->dev
, chinfo
, rpmsg_channel_match
);
566 device_unregister(dev
);
573 /* super simple buffer "allocator" that is just enough for now */
574 static void *get_a_tx_buf(struct virtproc_info
*vrp
)
579 /* support multiple concurrent senders */
580 mutex_lock(&vrp
->tx_lock
);
583 * either pick the next unused tx buffer
584 * (half of our buffers are used for sending messages)
586 if (vrp
->last_sbuf
< vrp
->num_bufs
/ 2)
587 ret
= vrp
->sbufs
+ RPMSG_BUF_SIZE
* vrp
->last_sbuf
++;
588 /* or recycle a used one */
590 ret
= virtqueue_get_buf(vrp
->svq
, &len
);
592 mutex_unlock(&vrp
->tx_lock
);
598 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
599 * @vrp: virtual remote processor state
601 * This function is called before a sender is blocked, waiting for
602 * a tx buffer to become available.
604 * If we already have blocking senders, this function merely increases
605 * the "sleepers" reference count, and exits.
607 * Otherwise, if this is the first sender to block, we also enable
608 * virtio's tx callbacks, so we'd be immediately notified when a tx
609 * buffer is consumed (we rely on virtio's tx callback in order
610 * to wake up sleeping senders as soon as a tx buffer is used by the
613 static void rpmsg_upref_sleepers(struct virtproc_info
*vrp
)
615 /* support multiple concurrent senders */
616 mutex_lock(&vrp
->tx_lock
);
618 /* are we the first sleeping context waiting for tx buffers ? */
619 if (atomic_inc_return(&vrp
->sleepers
) == 1)
620 /* enable "tx-complete" interrupts before dozing off */
621 virtqueue_enable_cb(vrp
->svq
);
623 mutex_unlock(&vrp
->tx_lock
);
627 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
628 * @vrp: virtual remote processor state
630 * This function is called after a sender, that waited for a tx buffer
631 * to become available, is unblocked.
633 * If we still have blocking senders, this function merely decreases
634 * the "sleepers" reference count, and exits.
636 * Otherwise, if there are no more blocking senders, we also disable
637 * virtio's tx callbacks, to avoid the overhead incurred with handling
638 * those (now redundant) interrupts.
640 static void rpmsg_downref_sleepers(struct virtproc_info
*vrp
)
642 /* support multiple concurrent senders */
643 mutex_lock(&vrp
->tx_lock
);
645 /* are we the last sleeping context waiting for tx buffers ? */
646 if (atomic_dec_and_test(&vrp
->sleepers
))
647 /* disable "tx-complete" interrupts */
648 virtqueue_disable_cb(vrp
->svq
);
650 mutex_unlock(&vrp
->tx_lock
);
654 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
655 * @rpdev: the rpmsg channel
656 * @src: source address
657 * @dst: destination address
658 * @data: payload of message
659 * @len: length of payload
660 * @wait: indicates whether caller should block in case no TX buffers available
662 * This function is the base implementation for all of the rpmsg sending API.
664 * It will send @data of length @len to @dst, and say it's from @src. The
665 * message will be sent to the remote processor which the @rpdev channel
668 * The message is sent using one of the TX buffers that are available for
669 * communication with this remote processor.
671 * If @wait is true, the caller will be blocked until either a TX buffer is
672 * available, or 15 seconds elapses (we don't want callers to
673 * sleep indefinitely due to misbehaving remote processors), and in that
674 * case -ERESTARTSYS is returned. The number '15' itself was picked
675 * arbitrarily; there's little point in asking drivers to provide a timeout
678 * Otherwise, if @wait is false, and there are no TX buffers available,
679 * the function will immediately fail, and -ENOMEM will be returned.
681 * Normally drivers shouldn't use this function directly; instead, drivers
682 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
683 * (see include/linux/rpmsg.h).
685 * Returns 0 on success and an appropriate error value on failure.
687 int rpmsg_send_offchannel_raw(struct rpmsg_channel
*rpdev
, u32 src
, u32 dst
,
688 void *data
, int len
, bool wait
)
690 struct virtproc_info
*vrp
= rpdev
->vrp
;
691 struct device
*dev
= &rpdev
->dev
;
692 struct scatterlist sg
;
693 struct rpmsg_hdr
*msg
;
696 /* bcasting isn't allowed */
697 if (src
== RPMSG_ADDR_ANY
|| dst
== RPMSG_ADDR_ANY
) {
698 dev_err(dev
, "invalid addr (src 0x%x, dst 0x%x)\n", src
, dst
);
703 * We currently use fixed-sized buffers, and therefore the payload
706 * One of the possible improvements here is either to support
707 * user-provided buffers (and then we can also support zero-copy
708 * messaging), or to improve the buffer allocator, to support
709 * variable-length buffer sizes.
711 if (len
> RPMSG_BUF_SIZE
- sizeof(struct rpmsg_hdr
)) {
712 dev_err(dev
, "message is too big (%d)\n", len
);
717 msg
= get_a_tx_buf(vrp
);
721 /* no free buffer ? wait for one (but bail after 15 seconds) */
723 /* enable "tx-complete" interrupts, if not already enabled */
724 rpmsg_upref_sleepers(vrp
);
727 * sleep until a free buffer is available or 15 secs elapse.
728 * the timeout period is not configurable because there's
729 * little point in asking drivers to specify that.
730 * if later this happens to be required, it'd be easy to add.
732 err
= wait_event_interruptible_timeout(vrp
->sendq
,
733 (msg
= get_a_tx_buf(vrp
)),
734 msecs_to_jiffies(15000));
736 /* disable "tx-complete" interrupts if we're the last sleeper */
737 rpmsg_downref_sleepers(vrp
);
741 dev_err(dev
, "timeout waiting for a tx buffer\n");
751 memcpy(msg
->data
, data
, len
);
753 dev_dbg(dev
, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
754 msg
->src
, msg
->dst
, msg
->len
,
755 msg
->flags
, msg
->reserved
);
756 print_hex_dump(KERN_DEBUG
, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE
, 16, 1,
757 msg
, sizeof(*msg
) + msg
->len
, true);
759 sg_init_one(&sg
, msg
, sizeof(*msg
) + len
);
761 mutex_lock(&vrp
->tx_lock
);
763 /* add message to the remote processor's virtqueue */
764 err
= virtqueue_add_outbuf(vrp
->svq
, &sg
, 1, msg
, GFP_KERNEL
);
767 * need to reclaim the buffer here, otherwise it's lost
768 * (memory won't leak, but rpmsg won't use it again for TX).
769 * this will wait for a buffer management overhaul.
771 dev_err(dev
, "virtqueue_add_outbuf failed: %d\n", err
);
775 /* tell the remote processor it has a pending message to read */
776 virtqueue_kick(vrp
->svq
);
778 mutex_unlock(&vrp
->tx_lock
);
781 EXPORT_SYMBOL(rpmsg_send_offchannel_raw
);
783 static int rpmsg_recv_single(struct virtproc_info
*vrp
, struct device
*dev
,
784 struct rpmsg_hdr
*msg
, unsigned int len
)
786 struct rpmsg_endpoint
*ept
;
787 struct scatterlist sg
;
790 dev_dbg(dev
, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
791 msg
->src
, msg
->dst
, msg
->len
,
792 msg
->flags
, msg
->reserved
);
793 print_hex_dump(KERN_DEBUG
, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE
, 16, 1,
794 msg
, sizeof(*msg
) + msg
->len
, true);
797 * We currently use fixed-sized buffers, so trivially sanitize
798 * the reported payload length.
800 if (len
> RPMSG_BUF_SIZE
||
801 msg
->len
> (len
- sizeof(struct rpmsg_hdr
))) {
802 dev_warn(dev
, "inbound msg too big: (%d, %d)\n", len
, msg
->len
);
806 /* use the dst addr to fetch the callback of the appropriate user */
807 mutex_lock(&vrp
->endpoints_lock
);
809 ept
= idr_find(&vrp
->endpoints
, msg
->dst
);
811 /* let's make sure no one deallocates ept while we use it */
813 kref_get(&ept
->refcount
);
815 mutex_unlock(&vrp
->endpoints_lock
);
818 /* make sure ept->cb doesn't go away while we use it */
819 mutex_lock(&ept
->cb_lock
);
822 ept
->cb(ept
->rpdev
, msg
->data
, msg
->len
, ept
->priv
,
825 mutex_unlock(&ept
->cb_lock
);
827 /* farewell, ept, we don't need you anymore */
828 kref_put(&ept
->refcount
, __ept_release
);
830 dev_warn(dev
, "msg received with no recipient\n");
832 /* publish the real size of the buffer */
833 sg_init_one(&sg
, msg
, RPMSG_BUF_SIZE
);
835 /* add the buffer back to the remote processor's virtqueue */
836 err
= virtqueue_add_inbuf(vrp
->rvq
, &sg
, 1, msg
, GFP_KERNEL
);
838 dev_err(dev
, "failed to add a virtqueue buffer: %d\n", err
);
845 /* called when an rx buffer is used, and it's time to digest a message */
846 static void rpmsg_recv_done(struct virtqueue
*rvq
)
848 struct virtproc_info
*vrp
= rvq
->vdev
->priv
;
849 struct device
*dev
= &rvq
->vdev
->dev
;
850 struct rpmsg_hdr
*msg
;
851 unsigned int len
, msgs_received
= 0;
854 msg
= virtqueue_get_buf(rvq
, &len
);
856 dev_err(dev
, "uhm, incoming signal, but no used buffer ?\n");
861 err
= rpmsg_recv_single(vrp
, dev
, msg
, len
);
867 msg
= virtqueue_get_buf(rvq
, &len
);
870 dev_dbg(dev
, "Received %u messages\n", msgs_received
);
872 /* tell the remote processor we added another available rx buffer */
874 virtqueue_kick(vrp
->rvq
);
878 * This is invoked whenever the remote processor completed processing
879 * a TX msg we just sent it, and the buffer is put back to the used ring.
881 * Normally, though, we suppress this "tx complete" interrupt in order to
882 * avoid the incurred overhead.
884 static void rpmsg_xmit_done(struct virtqueue
*svq
)
886 struct virtproc_info
*vrp
= svq
->vdev
->priv
;
888 dev_dbg(&svq
->vdev
->dev
, "%s\n", __func__
);
890 /* wake up potential senders that are waiting for a tx buffer */
891 wake_up_interruptible(&vrp
->sendq
);
894 /* invoked when a name service announcement arrives */
895 static void rpmsg_ns_cb(struct rpmsg_channel
*rpdev
, void *data
, int len
,
898 struct rpmsg_ns_msg
*msg
= data
;
899 struct rpmsg_channel
*newch
;
900 struct rpmsg_channel_info chinfo
;
901 struct virtproc_info
*vrp
= priv
;
902 struct device
*dev
= &vrp
->vdev
->dev
;
905 print_hex_dump(KERN_DEBUG
, "NS announcement: ",
906 DUMP_PREFIX_NONE
, 16, 1,
909 if (len
!= sizeof(*msg
)) {
910 dev_err(dev
, "malformed ns msg (%d)\n", len
);
915 * the name service ept does _not_ belong to a real rpmsg channel,
916 * and is handled by the rpmsg bus itself.
917 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
921 dev_err(dev
, "anomaly: ns ept has an rpdev handle\n");
925 /* don't trust the remote processor for null terminating the name */
926 msg
->name
[RPMSG_NAME_SIZE
- 1] = '\0';
928 dev_info(dev
, "%sing channel %s addr 0x%x\n",
929 msg
->flags
& RPMSG_NS_DESTROY
? "destroy" : "creat",
930 msg
->name
, msg
->addr
);
932 strncpy(chinfo
.name
, msg
->name
, sizeof(chinfo
.name
));
933 chinfo
.src
= RPMSG_ADDR_ANY
;
934 chinfo
.dst
= msg
->addr
;
936 if (msg
->flags
& RPMSG_NS_DESTROY
) {
937 ret
= rpmsg_destroy_channel(vrp
, &chinfo
);
939 dev_err(dev
, "rpmsg_destroy_channel failed: %d\n", ret
);
941 newch
= rpmsg_create_channel(vrp
, &chinfo
);
943 dev_err(dev
, "rpmsg_create_channel failed\n");
947 static int rpmsg_probe(struct virtio_device
*vdev
)
949 vq_callback_t
*vq_cbs
[] = { rpmsg_recv_done
, rpmsg_xmit_done
};
950 static const char * const names
[] = { "input", "output" };
951 struct virtqueue
*vqs
[2];
952 struct virtproc_info
*vrp
;
955 size_t total_buf_space
;
958 vrp
= kzalloc(sizeof(*vrp
), GFP_KERNEL
);
964 idr_init(&vrp
->endpoints
);
965 mutex_init(&vrp
->endpoints_lock
);
966 mutex_init(&vrp
->tx_lock
);
967 init_waitqueue_head(&vrp
->sendq
);
969 /* We expect two virtqueues, rx and tx (and in this order) */
970 err
= vdev
->config
->find_vqs(vdev
, 2, vqs
, vq_cbs
, names
);
977 /* we expect symmetric tx/rx vrings */
978 WARN_ON(virtqueue_get_vring_size(vrp
->rvq
) !=
979 virtqueue_get_vring_size(vrp
->svq
));
981 /* we need less buffers if vrings are small */
982 if (virtqueue_get_vring_size(vrp
->rvq
) < MAX_RPMSG_NUM_BUFS
/ 2)
983 vrp
->num_bufs
= virtqueue_get_vring_size(vrp
->rvq
) * 2;
985 vrp
->num_bufs
= MAX_RPMSG_NUM_BUFS
;
987 total_buf_space
= vrp
->num_bufs
* RPMSG_BUF_SIZE
;
989 /* allocate coherent memory for the buffers */
990 bufs_va
= dma_alloc_coherent(vdev
->dev
.parent
->parent
,
991 total_buf_space
, &vrp
->bufs_dma
,
998 dev_dbg(&vdev
->dev
, "buffers: va %p, dma 0x%llx\n", bufs_va
,
999 (unsigned long long)vrp
->bufs_dma
);
1001 /* half of the buffers is dedicated for RX */
1002 vrp
->rbufs
= bufs_va
;
1004 /* and half is dedicated for TX */
1005 vrp
->sbufs
= bufs_va
+ total_buf_space
/ 2;
1007 /* set up the receive buffers */
1008 for (i
= 0; i
< vrp
->num_bufs
/ 2; i
++) {
1009 struct scatterlist sg
;
1010 void *cpu_addr
= vrp
->rbufs
+ i
* RPMSG_BUF_SIZE
;
1012 sg_init_one(&sg
, cpu_addr
, RPMSG_BUF_SIZE
);
1014 err
= virtqueue_add_inbuf(vrp
->rvq
, &sg
, 1, cpu_addr
,
1016 WARN_ON(err
); /* sanity check; this can't really happen */
1019 /* suppress "tx-complete" interrupts */
1020 virtqueue_disable_cb(vrp
->svq
);
1024 /* if supported by the remote processor, enable the name service */
1025 if (virtio_has_feature(vdev
, VIRTIO_RPMSG_F_NS
)) {
1026 /* a dedicated endpoint handles the name service msgs */
1027 vrp
->ns_ept
= __rpmsg_create_ept(vrp
, NULL
, rpmsg_ns_cb
,
1028 vrp
, RPMSG_NS_ADDR
);
1030 dev_err(&vdev
->dev
, "failed to create the ns ept\n");
1037 * Prepare to kick but don't notify yet - we can't do this before
1040 notify
= virtqueue_kick_prepare(vrp
->rvq
);
1042 /* From this point on, we can notify and get callbacks. */
1043 virtio_device_ready(vdev
);
1045 /* tell the remote processor it can start sending messages */
1047 * this might be concurrent with callbacks, but we are only
1048 * doing notify, not a full kick here, so that's ok.
1051 virtqueue_notify(vrp
->rvq
);
1053 dev_info(&vdev
->dev
, "rpmsg host is online\n");
1058 dma_free_coherent(vdev
->dev
.parent
->parent
, total_buf_space
,
1059 bufs_va
, vrp
->bufs_dma
);
1061 vdev
->config
->del_vqs(vrp
->vdev
);
1067 static int rpmsg_remove_device(struct device
*dev
, void *data
)
1069 device_unregister(dev
);
1074 static void rpmsg_remove(struct virtio_device
*vdev
)
1076 struct virtproc_info
*vrp
= vdev
->priv
;
1077 size_t total_buf_space
= vrp
->num_bufs
* RPMSG_BUF_SIZE
;
1080 vdev
->config
->reset(vdev
);
1082 ret
= device_for_each_child(&vdev
->dev
, NULL
, rpmsg_remove_device
);
1084 dev_warn(&vdev
->dev
, "can't remove rpmsg device: %d\n", ret
);
1087 __rpmsg_destroy_ept(vrp
, vrp
->ns_ept
);
1089 idr_destroy(&vrp
->endpoints
);
1091 vdev
->config
->del_vqs(vrp
->vdev
);
1093 dma_free_coherent(vdev
->dev
.parent
->parent
, total_buf_space
,
1094 vrp
->rbufs
, vrp
->bufs_dma
);
1099 static struct virtio_device_id id_table
[] = {
1100 { VIRTIO_ID_RPMSG
, VIRTIO_DEV_ANY_ID
},
1104 static unsigned int features
[] = {
1108 static struct virtio_driver virtio_ipc_driver
= {
1109 .feature_table
= features
,
1110 .feature_table_size
= ARRAY_SIZE(features
),
1111 .driver
.name
= KBUILD_MODNAME
,
1112 .driver
.owner
= THIS_MODULE
,
1113 .id_table
= id_table
,
1114 .probe
= rpmsg_probe
,
1115 .remove
= rpmsg_remove
,
1118 static int __init
rpmsg_init(void)
1122 ret
= bus_register(&rpmsg_bus
);
1124 pr_err("failed to register rpmsg bus: %d\n", ret
);
1128 ret
= register_virtio_driver(&virtio_ipc_driver
);
1130 pr_err("failed to register virtio driver: %d\n", ret
);
1131 bus_unregister(&rpmsg_bus
);
1136 subsys_initcall(rpmsg_init
);
1138 static void __exit
rpmsg_fini(void)
1140 unregister_virtio_driver(&virtio_ipc_driver
);
1141 bus_unregister(&rpmsg_bus
);
1143 module_exit(rpmsg_fini
);
1145 MODULE_DEVICE_TABLE(virtio
, id_table
);
1146 MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1147 MODULE_LICENSE("GPL v2");