blob: 75506ec2840e2ec4d1e331f209377d874f604386 [file] [log] [blame]
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001/*
2 * Virtio-based remote processor messaging bus
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Copyright (C) 2011 Google, Inc.
6 *
7 * Ohad Ben-Cohen <ohad@wizery.com>
8 * Brian Swetland <swetland@google.com>
9 *
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.
13 *
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.
18 */
19
20#define pr_fmt(fmt) "%s: " fmt, __func__
21
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
37/**
38 * struct virtproc_info - virtual remote processor state
39 * @vdev: the virtio device
40 * @rvq: rx virtqueue
41 * @svq: tx virtqueue
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
54 *
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
57 * remote processor).
58 */
59struct virtproc_info {
60 struct virtio_device *vdev;
61 struct virtqueue *rvq, *svq;
62 void *rbufs, *sbufs;
63 int last_sbuf;
64 dma_addr_t bufs_dma;
65 struct mutex tx_lock;
66 struct idr endpoints;
67 struct mutex endpoints_lock;
68 wait_queue_head_t sendq;
69 atomic_t sleepers;
70 struct rpmsg_endpoint *ns_ept;
71};
72
73/**
74 * struct rpmsg_channel_info - internal channel info representation
75 * @name: name of service
76 * @src: local address
77 * @dst: destination address
78 */
79struct rpmsg_channel_info {
80 char name[RPMSG_NAME_SIZE];
81 u32 src;
82 u32 dst;
83};
84
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)
87
88/*
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.
91 *
92 * Each buffer will have 16 bytes for the msg header and 496 bytes for
93 * the payload.
94 *
95 * This will require a total space of 256KB for the buffers.
96 *
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.
100 *
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
103 * processor.
104 */
105#define RPMSG_NUM_BUFS (512)
106#define RPMSG_BUF_SIZE (512)
107#define RPMSG_TOTAL_BUF_SPACE (RPMSG_NUM_BUFS * RPMSG_BUF_SIZE)
108
109/*
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.
113 */
114#define RPMSG_RESERVED_ADDRESSES (1024)
115
116/* Address 53 is reserved for advertising remote services */
117#define RPMSG_NS_ADDR (53)
118
119/* sysfs show configuration fields */
120#define rpmsg_show_attr(field, path, format_string) \
121static ssize_t \
122field##_show(struct device *dev, \
123 struct device_attribute *attr, char *buf) \
124{ \
125 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
126 \
127 return sprintf(buf, format_string, rpdev->path); \
128}
129
130/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
131rpmsg_show_attr(name, id.name, "%s\n");
132rpmsg_show_attr(src, src, "0x%x\n");
133rpmsg_show_attr(dst, dst, "0x%x\n");
134rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
135
136/*
137 * Unique (and free running) index for rpmsg devices.
138 *
139 * Yeah, we're not recycling those numbers (yet?). will be easy
140 * to change if/when we want to.
141 */
142static unsigned int rpmsg_dev_index;
143
144static ssize_t modalias_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
146{
147 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
148
149 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
150}
151
152static struct device_attribute rpmsg_dev_attrs[] = {
153 __ATTR_RO(name),
154 __ATTR_RO(modalias),
155 __ATTR_RO(dst),
156 __ATTR_RO(src),
157 __ATTR_RO(announce),
158 __ATTR_NULL
159};
160
161/* rpmsg devices and drivers are matched using the service name */
162static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
163 const struct rpmsg_device_id *id)
164{
165 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
166}
167
168/* match rpmsg channel and rpmsg driver */
169static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
170{
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;
174 unsigned int i;
175
176 for (i = 0; ids[i].name[0]; i++)
177 if (rpmsg_id_match(rpdev, &ids[i]))
178 return 1;
179
180 return 0;
181}
182
183static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
184{
185 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
186
187 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
188 rpdev->id.name);
189}
190
191/* for more info, see below documentation of rpmsg_create_ept() */
192static 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)
195{
196 int err, tmpaddr, request;
197 struct rpmsg_endpoint *ept;
198 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
199
200 if (!idr_pre_get(&vrp->endpoints, GFP_KERNEL))
201 return NULL;
202
203 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
204 if (!ept) {
205 dev_err(dev, "failed to kzalloc a new ept\n");
206 return NULL;
207 }
208
209 ept->rpdev = rpdev;
210 ept->cb = cb;
211 ept->priv = priv;
212
213 /* do we need to allocate a local address ? */
214 request = addr == RPMSG_ADDR_ANY ? RPMSG_RESERVED_ADDRESSES : addr;
215
216 mutex_lock(&vrp->endpoints_lock);
217
218 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
219 err = idr_get_new_above(&vrp->endpoints, ept, request, &tmpaddr);
220 if (err) {
221 dev_err(dev, "idr_get_new_above failed: %d\n", err);
222 goto free_ept;
223 }
224
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);
228 goto rem_idr;
229 }
230
231 ept->addr = tmpaddr;
232
233 mutex_unlock(&vrp->endpoints_lock);
234
235 return ept;
236
237rem_idr:
238 idr_remove(&vrp->endpoints, request);
239free_ept:
240 mutex_unlock(&vrp->endpoints_lock);
241 kfree(ept);
242 return NULL;
243}
244
245/**
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
251 *
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.
255 *
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).
260 *
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).
264 *
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.
270 *
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.
274 *
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).
282 *
283 * Returns a pointer to the endpoint on success, or NULL on error.
284 */
285struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
286 rpmsg_rx_cb_t cb, void *priv, u32 addr)
287{
288 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
289}
290EXPORT_SYMBOL(rpmsg_create_ept);
291
292/**
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200293 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
294 * @vrp: virtproc which owns this ept
295 * @ept: endpoing to destroy
296 *
297 * An internal function which destroy an ept without assuming it is
298 * bound to an rpmsg channel. This is needed for handling the internal
299 * name service endpoint, which isn't bound to an rpmsg channel.
300 * See also __rpmsg_create_ept().
301 */
302static void
303__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
304{
305 mutex_lock(&vrp->endpoints_lock);
306 idr_remove(&vrp->endpoints, ept->addr);
307 mutex_unlock(&vrp->endpoints_lock);
308
309 kfree(ept);
310}
311
312/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200313 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
314 * @ept: endpoing to destroy
315 *
316 * Should be used by drivers to destroy an rpmsg endpoint previously
317 * created with rpmsg_create_ept().
318 */
319void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
320{
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200321 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200322}
323EXPORT_SYMBOL(rpmsg_destroy_ept);
324
325/*
326 * when an rpmsg driver is probed with a channel, we seamlessly create
327 * it an endpoint, binding its rx callback to a unique local rpmsg
328 * address.
329 *
330 * if we need to, we also announce about this channel to the remote
331 * processor (needed in case the driver is exposing an rpmsg service).
332 */
333static int rpmsg_dev_probe(struct device *dev)
334{
335 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
336 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
337 struct virtproc_info *vrp = rpdev->vrp;
338 struct rpmsg_endpoint *ept;
339 int err;
340
341 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
342 if (!ept) {
343 dev_err(dev, "failed to create endpoint\n");
344 err = -ENOMEM;
345 goto out;
346 }
347
348 rpdev->ept = ept;
349 rpdev->src = ept->addr;
350
351 err = rpdrv->probe(rpdev);
352 if (err) {
353 dev_err(dev, "%s: failed: %d\n", __func__, err);
354 rpmsg_destroy_ept(ept);
355 goto out;
356 }
357
358 /* need to tell remote processor's name service about this channel ? */
359 if (rpdev->announce &&
360 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
361 struct rpmsg_ns_msg nsm;
362
363 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
364 nsm.addr = rpdev->src;
365 nsm.flags = RPMSG_NS_CREATE;
366
367 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
368 if (err)
369 dev_err(dev, "failed to announce service %d\n", err);
370 }
371
372out:
373 return err;
374}
375
376static int rpmsg_dev_remove(struct device *dev)
377{
378 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
379 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
380 struct virtproc_info *vrp = rpdev->vrp;
381 int err = 0;
382
383 /* tell remote processor's name service we're removing this channel */
384 if (rpdev->announce &&
385 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
386 struct rpmsg_ns_msg nsm;
387
388 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
389 nsm.addr = rpdev->src;
390 nsm.flags = RPMSG_NS_DESTROY;
391
392 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
393 if (err)
394 dev_err(dev, "failed to announce service %d\n", err);
395 }
396
397 rpdrv->remove(rpdev);
398
399 rpmsg_destroy_ept(rpdev->ept);
400
401 return err;
402}
403
404static struct bus_type rpmsg_bus = {
405 .name = "rpmsg",
406 .match = rpmsg_dev_match,
407 .dev_attrs = rpmsg_dev_attrs,
408 .uevent = rpmsg_uevent,
409 .probe = rpmsg_dev_probe,
410 .remove = rpmsg_dev_remove,
411};
412
413/**
414 * register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
415 * @rpdrv: pointer to a struct rpmsg_driver
416 *
417 * Returns 0 on success, and an appropriate error value on failure.
418 */
419int register_rpmsg_driver(struct rpmsg_driver *rpdrv)
420{
421 rpdrv->drv.bus = &rpmsg_bus;
422 return driver_register(&rpdrv->drv);
423}
424EXPORT_SYMBOL(register_rpmsg_driver);
425
426/**
427 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
428 * @rpdrv: pointer to a struct rpmsg_driver
429 *
430 * Returns 0 on success, and an appropriate error value on failure.
431 */
432void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
433{
434 driver_unregister(&rpdrv->drv);
435}
436EXPORT_SYMBOL(unregister_rpmsg_driver);
437
438static void rpmsg_release_device(struct device *dev)
439{
440 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
441
442 kfree(rpdev);
443}
444
445/*
446 * match an rpmsg channel with a channel info struct.
447 * this is used to make sure we're not creating rpmsg devices for channels
448 * that already exist.
449 */
450static int rpmsg_channel_match(struct device *dev, void *data)
451{
452 struct rpmsg_channel_info *chinfo = data;
453 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
454
455 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
456 return 0;
457
458 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
459 return 0;
460
461 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
462 return 0;
463
464 /* found a match ! */
465 return 1;
466}
467
468/*
469 * create an rpmsg channel using its name and address info.
470 * this function will be used to create both static and dynamic
471 * channels.
472 */
473static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
474 struct rpmsg_channel_info *chinfo)
475{
476 struct rpmsg_channel *rpdev;
477 struct device *tmp, *dev = &vrp->vdev->dev;
478 int ret;
479
480 /* make sure a similar channel doesn't already exist */
481 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
482 if (tmp) {
483 /* decrement the matched device's refcount back */
484 put_device(tmp);
485 dev_err(dev, "channel %s:%x:%x already exist\n",
486 chinfo->name, chinfo->src, chinfo->dst);
487 return NULL;
488 }
489
490 rpdev = kzalloc(sizeof(struct rpmsg_channel), GFP_KERNEL);
491 if (!rpdev) {
492 pr_err("kzalloc failed\n");
493 return NULL;
494 }
495
496 rpdev->vrp = vrp;
497 rpdev->src = chinfo->src;
498 rpdev->dst = chinfo->dst;
499
500 /*
501 * rpmsg server channels has predefined local address (for now),
502 * and their existence needs to be announced remotely
503 */
504 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY ? true : false;
505
506 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
507
508 /* very simple device indexing plumbing which is enough for now */
509 dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
510
511 rpdev->dev.parent = &vrp->vdev->dev;
512 rpdev->dev.bus = &rpmsg_bus;
513 rpdev->dev.release = rpmsg_release_device;
514
515 ret = device_register(&rpdev->dev);
516 if (ret) {
517 dev_err(dev, "device_register failed: %d\n", ret);
518 put_device(&rpdev->dev);
519 return NULL;
520 }
521
522 return rpdev;
523}
524
525/*
526 * find an existing channel using its name + address properties,
527 * and destroy it
528 */
529static int rpmsg_destroy_channel(struct virtproc_info *vrp,
530 struct rpmsg_channel_info *chinfo)
531{
532 struct virtio_device *vdev = vrp->vdev;
533 struct device *dev;
534
535 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
536 if (!dev)
537 return -EINVAL;
538
539 device_unregister(dev);
540
541 put_device(dev);
542
543 return 0;
544}
545
546/* super simple buffer "allocator" that is just enough for now */
547static void *get_a_tx_buf(struct virtproc_info *vrp)
548{
549 unsigned int len;
550 void *ret;
551
552 /* support multiple concurrent senders */
553 mutex_lock(&vrp->tx_lock);
554
555 /*
556 * either pick the next unused tx buffer
557 * (half of our buffers are used for sending messages)
558 */
559 if (vrp->last_sbuf < RPMSG_NUM_BUFS / 2)
560 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
561 /* or recycle a used one */
562 else
563 ret = virtqueue_get_buf(vrp->svq, &len);
564
565 mutex_unlock(&vrp->tx_lock);
566
567 return ret;
568}
569
570/**
571 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
572 * @vrp: virtual remote processor state
573 *
574 * This function is called before a sender is blocked, waiting for
575 * a tx buffer to become available.
576 *
577 * If we already have blocking senders, this function merely increases
578 * the "sleepers" reference count, and exits.
579 *
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
584 * remote processor).
585 */
586static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
587{
588 /* support multiple concurrent senders */
589 mutex_lock(&vrp->tx_lock);
590
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);
595
596 mutex_unlock(&vrp->tx_lock);
597}
598
599/**
600 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
601 * @vrp: virtual remote processor state
602 *
603 * This function is called after a sender, that waited for a tx buffer
604 * to become available, is unblocked.
605 *
606 * If we still have blocking senders, this function merely decreases
607 * the "sleepers" reference count, and exits.
608 *
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.
612 */
613static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
614{
615 /* support multiple concurrent senders */
616 mutex_lock(&vrp->tx_lock);
617
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);
622
623 mutex_unlock(&vrp->tx_lock);
624}
625
626/**
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
634 *
635 * This function is the base implementation for all of the rpmsg sending API.
636 *
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
639 * belongs to.
640 *
641 * The message is sent using one of the TX buffers that are available for
642 * communication with this remote processor.
643 *
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
649 * value themselves.
650 *
651 * Otherwise, if @wait is false, and there are no TX buffers available,
652 * the function will immediately fail, and -ENOMEM will be returned.
653 *
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).
657 *
658 * Returns 0 on success and an appropriate error value on failure.
659 */
660int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
661 void *data, int len, bool wait)
662{
663 struct virtproc_info *vrp = rpdev->vrp;
664 struct device *dev = &rpdev->dev;
665 struct scatterlist sg;
666 struct rpmsg_hdr *msg;
667 int err;
668
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);
672 return -EINVAL;
673 }
674
675 /*
676 * We currently use fixed-sized buffers, and therefore the payload
677 * length is limited.
678 *
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.
683 */
684 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
685 dev_err(dev, "message is too big (%d)\n", len);
686 return -EMSGSIZE;
687 }
688
689 /* grab a buffer */
690 msg = get_a_tx_buf(vrp);
691 if (!msg && !wait)
692 return -ENOMEM;
693
694 /* no free buffer ? wait for one (but bail after 15 seconds) */
695 while (!msg) {
696 /* enable "tx-complete" interrupts, if not already enabled */
697 rpmsg_upref_sleepers(vrp);
698
699 /*
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.
704 */
705 err = wait_event_interruptible_timeout(vrp->sendq,
706 (msg = get_a_tx_buf(vrp)),
707 msecs_to_jiffies(15000));
708
709 /* disable "tx-complete" interrupts if we're the last sleeper */
710 rpmsg_downref_sleepers(vrp);
711
712 /* timeout ? */
713 if (!err) {
714 dev_err(dev, "timeout waiting for a tx buffer\n");
715 return -ERESTARTSYS;
716 }
717 }
718
719 msg->len = len;
720 msg->flags = 0;
721 msg->src = src;
722 msg->dst = dst;
723 msg->reserved = 0;
724 memcpy(msg->data, data, len);
725
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,
728 msg->flags, msg->reserved);
729 print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
730 msg, sizeof(*msg) + msg->len, true);
731
732 sg_init_one(&sg, msg, sizeof(*msg) + len);
733
734 mutex_lock(&vrp->tx_lock);
735
736 /* add message to the remote processor's virtqueue */
Axel Linb7195872012-01-12 09:00:27 +0200737 err = virtqueue_add_buf(vrp->svq, &sg, 1, 0, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200738 if (err < 0) {
739 /*
740 * need to reclaim the buffer here, otherwise it's lost
741 * (memory won't leak, but rpmsg won't use it again for TX).
742 * this will wait for a buffer management overhaul.
743 */
Axel Linb7195872012-01-12 09:00:27 +0200744 dev_err(dev, "virtqueue_add_buf failed: %d\n", err);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200745 goto out;
746 }
747
748 /* tell the remote processor it has a pending message to read */
749 virtqueue_kick(vrp->svq);
750
751 err = 0;
752out:
753 mutex_unlock(&vrp->tx_lock);
754 return err;
755}
756EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
757
758/* called when an rx buffer is used, and it's time to digest a message */
759static void rpmsg_recv_done(struct virtqueue *rvq)
760{
761 struct rpmsg_hdr *msg;
762 unsigned int len;
763 struct rpmsg_endpoint *ept;
764 struct scatterlist sg;
765 struct virtproc_info *vrp = rvq->vdev->priv;
766 struct device *dev = &rvq->vdev->dev;
767 int err;
768
769 msg = virtqueue_get_buf(rvq, &len);
770 if (!msg) {
771 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
772 return;
773 }
774
775 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
776 msg->src, msg->dst, msg->len,
777 msg->flags, msg->reserved);
778 print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
779 msg, sizeof(*msg) + msg->len, true);
780
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200781 /*
782 * We currently use fixed-sized buffers, so trivially sanitize
783 * the reported payload length.
784 */
785 if (len > RPMSG_BUF_SIZE ||
786 msg->len > (len - sizeof(struct rpmsg_hdr))) {
787 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
788 return;
789 }
790
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200791 /* use the dst addr to fetch the callback of the appropriate user */
792 mutex_lock(&vrp->endpoints_lock);
793 ept = idr_find(&vrp->endpoints, msg->dst);
794 mutex_unlock(&vrp->endpoints_lock);
795
796 if (ept && ept->cb)
797 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv, msg->src);
798 else
799 dev_warn(dev, "msg received with no recepient\n");
800
Ohad Ben-Cohenf1d9e9c2012-02-28 16:11:28 +0200801 /* publish the real size of the buffer */
802 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200803
804 /* add the buffer back to the remote processor's virtqueue */
Axel Linb7195872012-01-12 09:00:27 +0200805 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200806 if (err < 0) {
807 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
808 return;
809 }
810
811 /* tell the remote processor we added another available rx buffer */
812 virtqueue_kick(vrp->rvq);
813}
814
815/*
816 * This is invoked whenever the remote processor completed processing
817 * a TX msg we just sent it, and the buffer is put back to the used ring.
818 *
819 * Normally, though, we suppress this "tx complete" interrupt in order to
820 * avoid the incurred overhead.
821 */
822static void rpmsg_xmit_done(struct virtqueue *svq)
823{
824 struct virtproc_info *vrp = svq->vdev->priv;
825
826 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
827
828 /* wake up potential senders that are waiting for a tx buffer */
829 wake_up_interruptible(&vrp->sendq);
830}
831
832/* invoked when a name service announcement arrives */
833static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
834 void *priv, u32 src)
835{
836 struct rpmsg_ns_msg *msg = data;
837 struct rpmsg_channel *newch;
838 struct rpmsg_channel_info chinfo;
839 struct virtproc_info *vrp = priv;
840 struct device *dev = &vrp->vdev->dev;
841 int ret;
842
843 print_hex_dump(KERN_DEBUG, "NS announcement: ",
844 DUMP_PREFIX_NONE, 16, 1,
845 data, len, true);
846
847 if (len != sizeof(*msg)) {
848 dev_err(dev, "malformed ns msg (%d)\n", len);
849 return;
850 }
851
852 /*
853 * the name service ept does _not_ belong to a real rpmsg channel,
854 * and is handled by the rpmsg bus itself.
855 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
856 * in somehow.
857 */
858 if (rpdev) {
859 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
860 return;
861 }
862
863 /* don't trust the remote processor for null terminating the name */
864 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
865
866 dev_info(dev, "%sing channel %s addr 0x%x\n",
867 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
868 msg->name, msg->addr);
869
870 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
871 chinfo.src = RPMSG_ADDR_ANY;
872 chinfo.dst = msg->addr;
873
874 if (msg->flags & RPMSG_NS_DESTROY) {
875 ret = rpmsg_destroy_channel(vrp, &chinfo);
876 if (ret)
877 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
878 } else {
879 newch = rpmsg_create_channel(vrp, &chinfo);
880 if (!newch)
881 dev_err(dev, "rpmsg_create_channel failed\n");
882 }
883}
884
885static int rpmsg_probe(struct virtio_device *vdev)
886{
887 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
888 const char *names[] = { "input", "output" };
889 struct virtqueue *vqs[2];
890 struct virtproc_info *vrp;
891 void *bufs_va;
892 int err = 0, i;
893
894 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
895 if (!vrp)
896 return -ENOMEM;
897
898 vrp->vdev = vdev;
899
900 idr_init(&vrp->endpoints);
901 mutex_init(&vrp->endpoints_lock);
902 mutex_init(&vrp->tx_lock);
903 init_waitqueue_head(&vrp->sendq);
904
905 /* We expect two virtqueues, rx and tx (and in this order) */
906 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
907 if (err)
908 goto free_vrp;
909
910 vrp->rvq = vqs[0];
911 vrp->svq = vqs[1];
912
913 /* allocate coherent memory for the buffers */
914 bufs_va = dma_alloc_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
915 &vrp->bufs_dma, GFP_KERNEL);
916 if (!bufs_va)
917 goto vqs_del;
918
Mark Asselstine9d8ae5c2012-03-04 13:33:28 +0200919 dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%llx\n", bufs_va,
920 (unsigned long long)vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200921
922 /* half of the buffers is dedicated for RX */
923 vrp->rbufs = bufs_va;
924
925 /* and half is dedicated for TX */
926 vrp->sbufs = bufs_va + RPMSG_TOTAL_BUF_SPACE / 2;
927
928 /* set up the receive buffers */
929 for (i = 0; i < RPMSG_NUM_BUFS / 2; i++) {
930 struct scatterlist sg;
931 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
932
933 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
934
Axel Linb7195872012-01-12 09:00:27 +0200935 err = virtqueue_add_buf(vrp->rvq, &sg, 0, 1, cpu_addr,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200936 GFP_KERNEL);
937 WARN_ON(err < 0); /* sanity check; this can't really happen */
938 }
939
940 /* suppress "tx-complete" interrupts */
941 virtqueue_disable_cb(vrp->svq);
942
943 vdev->priv = vrp;
944
945 /* if supported by the remote processor, enable the name service */
946 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
947 /* a dedicated endpoint handles the name service msgs */
948 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
949 vrp, RPMSG_NS_ADDR);
950 if (!vrp->ns_ept) {
951 dev_err(&vdev->dev, "failed to create the ns ept\n");
952 err = -ENOMEM;
953 goto free_coherent;
954 }
955 }
956
957 /* tell the remote processor it can start sending messages */
958 virtqueue_kick(vrp->rvq);
959
960 dev_info(&vdev->dev, "rpmsg host is online\n");
961
962 return 0;
963
964free_coherent:
965 dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE, bufs_va,
966 vrp->bufs_dma);
967vqs_del:
968 vdev->config->del_vqs(vrp->vdev);
969free_vrp:
970 kfree(vrp);
971 return err;
972}
973
974static int rpmsg_remove_device(struct device *dev, void *data)
975{
976 device_unregister(dev);
977
978 return 0;
979}
980
981static void __devexit rpmsg_remove(struct virtio_device *vdev)
982{
983 struct virtproc_info *vrp = vdev->priv;
984 int ret;
985
986 vdev->config->reset(vdev);
987
988 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
989 if (ret)
990 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
991
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200992 if (vrp->ns_ept)
993 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
994
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200995 idr_remove_all(&vrp->endpoints);
996 idr_destroy(&vrp->endpoints);
997
998 vdev->config->del_vqs(vrp->vdev);
999
1000 dma_free_coherent(vdev->dev.parent, RPMSG_TOTAL_BUF_SPACE,
1001 vrp->rbufs, vrp->bufs_dma);
1002
1003 kfree(vrp);
1004}
1005
1006static struct virtio_device_id id_table[] = {
1007 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1008 { 0 },
1009};
1010
1011static unsigned int features[] = {
1012 VIRTIO_RPMSG_F_NS,
1013};
1014
1015static struct virtio_driver virtio_ipc_driver = {
1016 .feature_table = features,
1017 .feature_table_size = ARRAY_SIZE(features),
1018 .driver.name = KBUILD_MODNAME,
1019 .driver.owner = THIS_MODULE,
1020 .id_table = id_table,
1021 .probe = rpmsg_probe,
1022 .remove = __devexit_p(rpmsg_remove),
1023};
1024
1025static int __init rpmsg_init(void)
1026{
1027 int ret;
1028
1029 ret = bus_register(&rpmsg_bus);
1030 if (ret) {
1031 pr_err("failed to register rpmsg bus: %d\n", ret);
1032 return ret;
1033 }
1034
1035 ret = register_virtio_driver(&virtio_ipc_driver);
1036 if (ret) {
1037 pr_err("failed to register virtio driver: %d\n", ret);
1038 bus_unregister(&rpmsg_bus);
1039 }
1040
1041 return ret;
1042}
1043module_init(rpmsg_init);
1044
1045static void __exit rpmsg_fini(void)
1046{
1047 unregister_virtio_driver(&virtio_ipc_driver);
1048 bus_unregister(&rpmsg_bus);
1049}
1050module_exit(rpmsg_fini);
1051
1052MODULE_DEVICE_TABLE(virtio, id_table);
1053MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1054MODULE_LICENSE("GPL v2");