blob: 04fd6bd22a7206bc1dcf583f7d0ce875a859cdc2 [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
Suman Annab1b98912014-09-16 13:33:07 -050044 * @num_bufs: total number of buffers for rx and tx
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020045 * @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
55 *
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
58 * remote processor).
59 */
60struct virtproc_info {
61 struct virtio_device *vdev;
62 struct virtqueue *rvq, *svq;
63 void *rbufs, *sbufs;
Suman Annab1b98912014-09-16 13:33:07 -050064 unsigned int num_bufs;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020065 int last_sbuf;
66 dma_addr_t bufs_dma;
67 struct mutex tx_lock;
68 struct idr endpoints;
69 struct mutex endpoints_lock;
70 wait_queue_head_t sendq;
71 atomic_t sleepers;
72 struct rpmsg_endpoint *ns_ept;
73};
74
75/**
76 * struct rpmsg_channel_info - internal channel info representation
77 * @name: name of service
78 * @src: local address
79 * @dst: destination address
80 */
81struct rpmsg_channel_info {
82 char name[RPMSG_NAME_SIZE];
83 u32 src;
84 u32 dst;
85};
86
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)
89
90/*
Suman Annab1b98912014-09-16 13:33:07 -050091 * 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).
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020094 *
95 * Each buffer will have 16 bytes for the msg header and 496 bytes for
96 * the payload.
97 *
Suman Annab1b98912014-09-16 13:33:07 -050098 * This will utilize a maximum total space of 256KB for the buffers.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020099 *
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.
103 *
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
106 * processor.
107 */
Suman Annab1b98912014-09-16 13:33:07 -0500108#define MAX_RPMSG_NUM_BUFS (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200109#define RPMSG_BUF_SIZE (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200110
111/*
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.
115 */
116#define RPMSG_RESERVED_ADDRESSES (1024)
117
118/* Address 53 is reserved for advertising remote services */
119#define RPMSG_NS_ADDR (53)
120
121/* sysfs show configuration fields */
122#define rpmsg_show_attr(field, path, format_string) \
123static ssize_t \
124field##_show(struct device *dev, \
125 struct device_attribute *attr, char *buf) \
126{ \
127 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
128 \
129 return sprintf(buf, format_string, rpdev->path); \
130}
131
132/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
133rpmsg_show_attr(name, id.name, "%s\n");
134rpmsg_show_attr(src, src, "0x%x\n");
135rpmsg_show_attr(dst, dst, "0x%x\n");
136rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
137
138/*
139 * Unique (and free running) index for rpmsg devices.
140 *
141 * Yeah, we're not recycling those numbers (yet?). will be easy
142 * to change if/when we want to.
143 */
144static unsigned int rpmsg_dev_index;
145
146static ssize_t modalias_show(struct device *dev,
147 struct device_attribute *attr, char *buf)
148{
149 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
150
151 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
152}
153
154static struct device_attribute rpmsg_dev_attrs[] = {
155 __ATTR_RO(name),
156 __ATTR_RO(modalias),
157 __ATTR_RO(dst),
158 __ATTR_RO(src),
159 __ATTR_RO(announce),
160 __ATTR_NULL
161};
162
163/* rpmsg devices and drivers are matched using the service name */
164static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
165 const struct rpmsg_device_id *id)
166{
167 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
168}
169
170/* match rpmsg channel and rpmsg driver */
171static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
172{
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;
176 unsigned int i;
177
178 for (i = 0; ids[i].name[0]; i++)
179 if (rpmsg_id_match(rpdev, &ids[i]))
180 return 1;
181
182 return 0;
183}
184
185static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
186{
187 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
188
189 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
190 rpdev->id.name);
191}
192
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300193/**
194 * __ept_release() - deallocate an rpmsg endpoint
195 * @kref: the ept's reference count
196 *
197 * This function deallocates an ept, and is invoked when its @kref refcount
198 * drops to zero.
199 *
200 * Never invoke this function directly!
201 */
202static void __ept_release(struct kref *kref)
203{
204 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
205 refcount);
206 /*
207 * At this point no one holds a reference to ept anymore,
208 * so we can directly free it
209 */
210 kfree(ept);
211}
212
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200213/* for more info, see below documentation of rpmsg_create_ept() */
214static 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)
217{
Tejun Heod0ffce72013-02-27 17:04:40 -0800218 int id_min, id_max, id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200219 struct rpmsg_endpoint *ept;
220 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
221
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200222 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500223 if (!ept)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200224 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200225
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300226 kref_init(&ept->refcount);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300227 mutex_init(&ept->cb_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300228
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200229 ept->rpdev = rpdev;
230 ept->cb = cb;
231 ept->priv = priv;
232
233 /* do we need to allocate a local address ? */
Tejun Heod0ffce72013-02-27 17:04:40 -0800234 if (addr == RPMSG_ADDR_ANY) {
235 id_min = RPMSG_RESERVED_ADDRESSES;
236 id_max = 0;
237 } else {
238 id_min = addr;
239 id_max = addr + 1;
240 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200241
242 mutex_lock(&vrp->endpoints_lock);
243
244 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
Tejun Heod0ffce72013-02-27 17:04:40 -0800245 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
246 if (id < 0) {
247 dev_err(dev, "idr_alloc failed: %d\n", id);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200248 goto free_ept;
249 }
Tejun Heod0ffce72013-02-27 17:04:40 -0800250 ept->addr = id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200251
252 mutex_unlock(&vrp->endpoints_lock);
253
254 return ept;
255
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200256free_ept:
257 mutex_unlock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300258 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200259 return NULL;
260}
261
262/**
263 * rpmsg_create_ept() - create a new rpmsg_endpoint
264 * @rpdev: rpmsg channel device
265 * @cb: rx callback handler
266 * @priv: private data for the driver's use
267 * @addr: local rpmsg address to bind with @cb
268 *
269 * Every rpmsg address in the system is bound to an rx callback (so when
270 * inbound messages arrive, they are dispatched by the rpmsg bus using the
271 * appropriate callback handler) by means of an rpmsg_endpoint struct.
272 *
273 * This function allows drivers to create such an endpoint, and by that,
274 * bind a callback, and possibly some private data too, to an rpmsg address
275 * (either one that is known in advance, or one that will be dynamically
276 * assigned for them).
277 *
278 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
279 * is already created for them when they are probed by the rpmsg bus
280 * (using the rx callback provided when they registered to the rpmsg bus).
281 *
282 * So things should just work for simple drivers: they already have an
283 * endpoint, their rx callback is bound to their rpmsg address, and when
284 * relevant inbound messages arrive (i.e. messages which their dst address
285 * equals to the src address of their rpmsg channel), the driver's handler
286 * is invoked to process it.
287 *
288 * That said, more complicated drivers might do need to allocate
289 * additional rpmsg addresses, and bind them to different rx callbacks.
290 * To accomplish that, those drivers need to call this function.
291 *
292 * Drivers should provide their @rpdev channel (so the new endpoint would belong
293 * to the same remote processor their channel belongs to), an rx callback
294 * function, an optional private data (which is provided back when the
295 * rx callback is invoked), and an address they want to bind with the
296 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
297 * dynamically assign them an available rpmsg address (drivers should have
298 * a very good reason why not to always use RPMSG_ADDR_ANY here).
299 *
300 * Returns a pointer to the endpoint on success, or NULL on error.
301 */
302struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
303 rpmsg_rx_cb_t cb, void *priv, u32 addr)
304{
305 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
306}
307EXPORT_SYMBOL(rpmsg_create_ept);
308
309/**
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200310 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
311 * @vrp: virtproc which owns this ept
312 * @ept: endpoing to destroy
313 *
314 * An internal function which destroy an ept without assuming it is
315 * bound to an rpmsg channel. This is needed for handling the internal
316 * name service endpoint, which isn't bound to an rpmsg channel.
317 * See also __rpmsg_create_ept().
318 */
319static void
320__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
321{
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300322 /* make sure new inbound messages can't find this ept anymore */
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200323 mutex_lock(&vrp->endpoints_lock);
324 idr_remove(&vrp->endpoints, ept->addr);
325 mutex_unlock(&vrp->endpoints_lock);
326
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300327 /* make sure in-flight inbound messages won't invoke cb anymore */
328 mutex_lock(&ept->cb_lock);
329 ept->cb = NULL;
330 mutex_unlock(&ept->cb_lock);
331
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300332 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200333}
334
335/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200336 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
337 * @ept: endpoing to destroy
338 *
339 * Should be used by drivers to destroy an rpmsg endpoint previously
340 * created with rpmsg_create_ept().
341 */
342void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
343{
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200344 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200345}
346EXPORT_SYMBOL(rpmsg_destroy_ept);
347
348/*
349 * when an rpmsg driver is probed with a channel, we seamlessly create
350 * it an endpoint, binding its rx callback to a unique local rpmsg
351 * address.
352 *
353 * if we need to, we also announce about this channel to the remote
354 * processor (needed in case the driver is exposing an rpmsg service).
355 */
356static int rpmsg_dev_probe(struct device *dev)
357{
358 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
359 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
360 struct virtproc_info *vrp = rpdev->vrp;
361 struct rpmsg_endpoint *ept;
362 int err;
363
364 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
365 if (!ept) {
366 dev_err(dev, "failed to create endpoint\n");
367 err = -ENOMEM;
368 goto out;
369 }
370
371 rpdev->ept = ept;
372 rpdev->src = ept->addr;
373
374 err = rpdrv->probe(rpdev);
375 if (err) {
376 dev_err(dev, "%s: failed: %d\n", __func__, err);
377 rpmsg_destroy_ept(ept);
378 goto out;
379 }
380
381 /* need to tell remote processor's name service about this channel ? */
382 if (rpdev->announce &&
383 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
384 struct rpmsg_ns_msg nsm;
385
386 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
387 nsm.addr = rpdev->src;
388 nsm.flags = RPMSG_NS_CREATE;
389
390 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
391 if (err)
392 dev_err(dev, "failed to announce service %d\n", err);
393 }
394
395out:
396 return err;
397}
398
399static int rpmsg_dev_remove(struct device *dev)
400{
401 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
402 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
403 struct virtproc_info *vrp = rpdev->vrp;
404 int err = 0;
405
406 /* tell remote processor's name service we're removing this channel */
407 if (rpdev->announce &&
408 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
409 struct rpmsg_ns_msg nsm;
410
411 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
412 nsm.addr = rpdev->src;
413 nsm.flags = RPMSG_NS_DESTROY;
414
415 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
416 if (err)
417 dev_err(dev, "failed to announce service %d\n", err);
418 }
419
420 rpdrv->remove(rpdev);
421
422 rpmsg_destroy_ept(rpdev->ept);
423
424 return err;
425}
426
427static struct bus_type rpmsg_bus = {
428 .name = "rpmsg",
429 .match = rpmsg_dev_match,
430 .dev_attrs = rpmsg_dev_attrs,
431 .uevent = rpmsg_uevent,
432 .probe = rpmsg_dev_probe,
433 .remove = rpmsg_dev_remove,
434};
435
436/**
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500437 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200438 * @rpdrv: pointer to a struct rpmsg_driver
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500439 * @owner: owning module/driver
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200440 *
441 * Returns 0 on success, and an appropriate error value on failure.
442 */
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500443int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200444{
445 rpdrv->drv.bus = &rpmsg_bus;
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500446 rpdrv->drv.owner = owner;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200447 return driver_register(&rpdrv->drv);
448}
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500449EXPORT_SYMBOL(__register_rpmsg_driver);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200450
451/**
452 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
453 * @rpdrv: pointer to a struct rpmsg_driver
454 *
455 * Returns 0 on success, and an appropriate error value on failure.
456 */
457void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
458{
459 driver_unregister(&rpdrv->drv);
460}
461EXPORT_SYMBOL(unregister_rpmsg_driver);
462
463static void rpmsg_release_device(struct device *dev)
464{
465 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
466
467 kfree(rpdev);
468}
469
470/*
471 * match an rpmsg channel with a channel info struct.
472 * this is used to make sure we're not creating rpmsg devices for channels
473 * that already exist.
474 */
475static int rpmsg_channel_match(struct device *dev, void *data)
476{
477 struct rpmsg_channel_info *chinfo = data;
478 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
479
480 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
481 return 0;
482
483 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
484 return 0;
485
486 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
487 return 0;
488
489 /* found a match ! */
490 return 1;
491}
492
493/*
494 * create an rpmsg channel using its name and address info.
495 * this function will be used to create both static and dynamic
496 * channels.
497 */
498static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
499 struct rpmsg_channel_info *chinfo)
500{
501 struct rpmsg_channel *rpdev;
502 struct device *tmp, *dev = &vrp->vdev->dev;
503 int ret;
504
505 /* make sure a similar channel doesn't already exist */
506 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
507 if (tmp) {
508 /* decrement the matched device's refcount back */
509 put_device(tmp);
510 dev_err(dev, "channel %s:%x:%x already exist\n",
511 chinfo->name, chinfo->src, chinfo->dst);
512 return NULL;
513 }
514
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500515 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
516 if (!rpdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200517 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200518
519 rpdev->vrp = vrp;
520 rpdev->src = chinfo->src;
521 rpdev->dst = chinfo->dst;
522
523 /*
524 * rpmsg server channels has predefined local address (for now),
525 * and their existence needs to be announced remotely
526 */
Andrew F. Davisc8ced112016-07-01 09:24:58 -0500527 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200528
529 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
530
531 /* very simple device indexing plumbing which is enough for now */
532 dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
533
534 rpdev->dev.parent = &vrp->vdev->dev;
535 rpdev->dev.bus = &rpmsg_bus;
536 rpdev->dev.release = rpmsg_release_device;
537
538 ret = device_register(&rpdev->dev);
539 if (ret) {
540 dev_err(dev, "device_register failed: %d\n", ret);
541 put_device(&rpdev->dev);
542 return NULL;
543 }
544
545 return rpdev;
546}
547
548/*
549 * find an existing channel using its name + address properties,
550 * and destroy it
551 */
552static int rpmsg_destroy_channel(struct virtproc_info *vrp,
553 struct rpmsg_channel_info *chinfo)
554{
555 struct virtio_device *vdev = vrp->vdev;
556 struct device *dev;
557
558 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
559 if (!dev)
560 return -EINVAL;
561
562 device_unregister(dev);
563
564 put_device(dev);
565
566 return 0;
567}
568
569/* super simple buffer "allocator" that is just enough for now */
570static void *get_a_tx_buf(struct virtproc_info *vrp)
571{
572 unsigned int len;
573 void *ret;
574
575 /* support multiple concurrent senders */
576 mutex_lock(&vrp->tx_lock);
577
578 /*
579 * either pick the next unused tx buffer
580 * (half of our buffers are used for sending messages)
581 */
Suman Annab1b98912014-09-16 13:33:07 -0500582 if (vrp->last_sbuf < vrp->num_bufs / 2)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200583 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
584 /* or recycle a used one */
585 else
586 ret = virtqueue_get_buf(vrp->svq, &len);
587
588 mutex_unlock(&vrp->tx_lock);
589
590 return ret;
591}
592
593/**
594 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
595 * @vrp: virtual remote processor state
596 *
597 * This function is called before a sender is blocked, waiting for
598 * a tx buffer to become available.
599 *
600 * If we already have blocking senders, this function merely increases
601 * the "sleepers" reference count, and exits.
602 *
603 * Otherwise, if this is the first sender to block, we also enable
604 * virtio's tx callbacks, so we'd be immediately notified when a tx
605 * buffer is consumed (we rely on virtio's tx callback in order
606 * to wake up sleeping senders as soon as a tx buffer is used by the
607 * remote processor).
608 */
609static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
610{
611 /* support multiple concurrent senders */
612 mutex_lock(&vrp->tx_lock);
613
614 /* are we the first sleeping context waiting for tx buffers ? */
615 if (atomic_inc_return(&vrp->sleepers) == 1)
616 /* enable "tx-complete" interrupts before dozing off */
617 virtqueue_enable_cb(vrp->svq);
618
619 mutex_unlock(&vrp->tx_lock);
620}
621
622/**
623 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
624 * @vrp: virtual remote processor state
625 *
626 * This function is called after a sender, that waited for a tx buffer
627 * to become available, is unblocked.
628 *
629 * If we still have blocking senders, this function merely decreases
630 * the "sleepers" reference count, and exits.
631 *
632 * Otherwise, if there are no more blocking senders, we also disable
633 * virtio's tx callbacks, to avoid the overhead incurred with handling
634 * those (now redundant) interrupts.
635 */
636static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
637{
638 /* support multiple concurrent senders */
639 mutex_lock(&vrp->tx_lock);
640
641 /* are we the last sleeping context waiting for tx buffers ? */
642 if (atomic_dec_and_test(&vrp->sleepers))
643 /* disable "tx-complete" interrupts */
644 virtqueue_disable_cb(vrp->svq);
645
646 mutex_unlock(&vrp->tx_lock);
647}
648
649/**
650 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
651 * @rpdev: the rpmsg channel
652 * @src: source address
653 * @dst: destination address
654 * @data: payload of message
655 * @len: length of payload
656 * @wait: indicates whether caller should block in case no TX buffers available
657 *
658 * This function is the base implementation for all of the rpmsg sending API.
659 *
660 * It will send @data of length @len to @dst, and say it's from @src. The
661 * message will be sent to the remote processor which the @rpdev channel
662 * belongs to.
663 *
664 * The message is sent using one of the TX buffers that are available for
665 * communication with this remote processor.
666 *
667 * If @wait is true, the caller will be blocked until either a TX buffer is
668 * available, or 15 seconds elapses (we don't want callers to
669 * sleep indefinitely due to misbehaving remote processors), and in that
670 * case -ERESTARTSYS is returned. The number '15' itself was picked
671 * arbitrarily; there's little point in asking drivers to provide a timeout
672 * value themselves.
673 *
674 * Otherwise, if @wait is false, and there are no TX buffers available,
675 * the function will immediately fail, and -ENOMEM will be returned.
676 *
677 * Normally drivers shouldn't use this function directly; instead, drivers
678 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
679 * (see include/linux/rpmsg.h).
680 *
681 * Returns 0 on success and an appropriate error value on failure.
682 */
683int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
684 void *data, int len, bool wait)
685{
686 struct virtproc_info *vrp = rpdev->vrp;
687 struct device *dev = &rpdev->dev;
688 struct scatterlist sg;
689 struct rpmsg_hdr *msg;
690 int err;
691
692 /* bcasting isn't allowed */
693 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
694 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
695 return -EINVAL;
696 }
697
698 /*
699 * We currently use fixed-sized buffers, and therefore the payload
700 * length is limited.
701 *
702 * One of the possible improvements here is either to support
703 * user-provided buffers (and then we can also support zero-copy
704 * messaging), or to improve the buffer allocator, to support
705 * variable-length buffer sizes.
706 */
707 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
708 dev_err(dev, "message is too big (%d)\n", len);
709 return -EMSGSIZE;
710 }
711
712 /* grab a buffer */
713 msg = get_a_tx_buf(vrp);
714 if (!msg && !wait)
715 return -ENOMEM;
716
717 /* no free buffer ? wait for one (but bail after 15 seconds) */
718 while (!msg) {
719 /* enable "tx-complete" interrupts, if not already enabled */
720 rpmsg_upref_sleepers(vrp);
721
722 /*
723 * sleep until a free buffer is available or 15 secs elapse.
724 * the timeout period is not configurable because there's
725 * little point in asking drivers to specify that.
726 * if later this happens to be required, it'd be easy to add.
727 */
728 err = wait_event_interruptible_timeout(vrp->sendq,
729 (msg = get_a_tx_buf(vrp)),
730 msecs_to_jiffies(15000));
731
732 /* disable "tx-complete" interrupts if we're the last sleeper */
733 rpmsg_downref_sleepers(vrp);
734
735 /* timeout ? */
736 if (!err) {
737 dev_err(dev, "timeout waiting for a tx buffer\n");
738 return -ERESTARTSYS;
739 }
740 }
741
742 msg->len = len;
743 msg->flags = 0;
744 msg->src = src;
745 msg->dst = dst;
746 msg->reserved = 0;
747 memcpy(msg->data, data, len);
748
749 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
750 msg->src, msg->dst, msg->len,
751 msg->flags, msg->reserved);
752 print_hex_dump(KERN_DEBUG, "rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
753 msg, sizeof(*msg) + msg->len, true);
754
755 sg_init_one(&sg, msg, sizeof(*msg) + len);
756
757 mutex_lock(&vrp->tx_lock);
758
759 /* add message to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030760 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030761 if (err) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200762 /*
763 * need to reclaim the buffer here, otherwise it's lost
764 * (memory won't leak, but rpmsg won't use it again for TX).
765 * this will wait for a buffer management overhaul.
766 */
Rusty Russellcee51d62013-03-20 15:44:29 +1030767 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200768 goto out;
769 }
770
771 /* tell the remote processor it has a pending message to read */
772 virtqueue_kick(vrp->svq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200773out:
774 mutex_unlock(&vrp->tx_lock);
775 return err;
776}
777EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
778
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300779static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
780 struct rpmsg_hdr *msg, unsigned int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200781{
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200782 struct rpmsg_endpoint *ept;
783 struct scatterlist sg;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200784 int err;
785
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200786 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
787 msg->src, msg->dst, msg->len,
788 msg->flags, msg->reserved);
789 print_hex_dump(KERN_DEBUG, "rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
790 msg, sizeof(*msg) + msg->len, true);
791
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200792 /*
793 * We currently use fixed-sized buffers, so trivially sanitize
794 * the reported payload length.
795 */
796 if (len > RPMSG_BUF_SIZE ||
797 msg->len > (len - sizeof(struct rpmsg_hdr))) {
798 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300799 return -EINVAL;
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200800 }
801
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200802 /* use the dst addr to fetch the callback of the appropriate user */
803 mutex_lock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300804
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200805 ept = idr_find(&vrp->endpoints, msg->dst);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300806
807 /* let's make sure no one deallocates ept while we use it */
808 if (ept)
809 kref_get(&ept->refcount);
810
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200811 mutex_unlock(&vrp->endpoints_lock);
812
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300813 if (ept) {
814 /* make sure ept->cb doesn't go away while we use it */
815 mutex_lock(&ept->cb_lock);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200816
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300817 if (ept->cb)
818 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
819 msg->src);
820
821 mutex_unlock(&ept->cb_lock);
822
823 /* farewell, ept, we don't need you anymore */
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300824 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300825 } else
Masanari Iida8a168ca2012-12-29 02:00:09 +0900826 dev_warn(dev, "msg received with no recipient\n");
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300827
Ohad Ben-Cohenf1d9e9c2012-02-28 16:11:28 +0200828 /* publish the real size of the buffer */
829 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200830
831 /* add the buffer back to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030832 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200833 if (err < 0) {
834 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300835 return err;
836 }
837
838 return 0;
839}
840
841/* called when an rx buffer is used, and it's time to digest a message */
842static void rpmsg_recv_done(struct virtqueue *rvq)
843{
844 struct virtproc_info *vrp = rvq->vdev->priv;
845 struct device *dev = &rvq->vdev->dev;
846 struct rpmsg_hdr *msg;
847 unsigned int len, msgs_received = 0;
848 int err;
849
850 msg = virtqueue_get_buf(rvq, &len);
851 if (!msg) {
852 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200853 return;
854 }
855
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300856 while (msg) {
857 err = rpmsg_recv_single(vrp, dev, msg, len);
858 if (err)
859 break;
860
861 msgs_received++;
862
863 msg = virtqueue_get_buf(rvq, &len);
Lee Jones6c49fbe2016-07-20 10:29:35 +0100864 }
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300865
866 dev_dbg(dev, "Received %u messages\n", msgs_received);
867
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200868 /* tell the remote processor we added another available rx buffer */
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300869 if (msgs_received)
870 virtqueue_kick(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200871}
872
873/*
874 * This is invoked whenever the remote processor completed processing
875 * a TX msg we just sent it, and the buffer is put back to the used ring.
876 *
877 * Normally, though, we suppress this "tx complete" interrupt in order to
878 * avoid the incurred overhead.
879 */
880static void rpmsg_xmit_done(struct virtqueue *svq)
881{
882 struct virtproc_info *vrp = svq->vdev->priv;
883
884 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
885
886 /* wake up potential senders that are waiting for a tx buffer */
887 wake_up_interruptible(&vrp->sendq);
888}
889
890/* invoked when a name service announcement arrives */
891static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
892 void *priv, u32 src)
893{
894 struct rpmsg_ns_msg *msg = data;
895 struct rpmsg_channel *newch;
896 struct rpmsg_channel_info chinfo;
897 struct virtproc_info *vrp = priv;
898 struct device *dev = &vrp->vdev->dev;
899 int ret;
900
901 print_hex_dump(KERN_DEBUG, "NS announcement: ",
902 DUMP_PREFIX_NONE, 16, 1,
903 data, len, true);
904
905 if (len != sizeof(*msg)) {
906 dev_err(dev, "malformed ns msg (%d)\n", len);
907 return;
908 }
909
910 /*
911 * the name service ept does _not_ belong to a real rpmsg channel,
912 * and is handled by the rpmsg bus itself.
913 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
914 * in somehow.
915 */
916 if (rpdev) {
917 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
918 return;
919 }
920
921 /* don't trust the remote processor for null terminating the name */
922 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
923
924 dev_info(dev, "%sing channel %s addr 0x%x\n",
925 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
926 msg->name, msg->addr);
927
928 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
929 chinfo.src = RPMSG_ADDR_ANY;
930 chinfo.dst = msg->addr;
931
932 if (msg->flags & RPMSG_NS_DESTROY) {
933 ret = rpmsg_destroy_channel(vrp, &chinfo);
934 if (ret)
935 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
936 } else {
937 newch = rpmsg_create_channel(vrp, &chinfo);
938 if (!newch)
939 dev_err(dev, "rpmsg_create_channel failed\n");
940 }
941}
942
943static int rpmsg_probe(struct virtio_device *vdev)
944{
945 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800946 static const char * const names[] = { "input", "output" };
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200947 struct virtqueue *vqs[2];
948 struct virtproc_info *vrp;
949 void *bufs_va;
950 int err = 0, i;
Suman Annab1b98912014-09-16 13:33:07 -0500951 size_t total_buf_space;
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030952 bool notify;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200953
954 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
955 if (!vrp)
956 return -ENOMEM;
957
958 vrp->vdev = vdev;
959
960 idr_init(&vrp->endpoints);
961 mutex_init(&vrp->endpoints_lock);
962 mutex_init(&vrp->tx_lock);
963 init_waitqueue_head(&vrp->sendq);
964
965 /* We expect two virtqueues, rx and tx (and in this order) */
966 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
967 if (err)
968 goto free_vrp;
969
970 vrp->rvq = vqs[0];
971 vrp->svq = vqs[1];
972
Suman Annab1b98912014-09-16 13:33:07 -0500973 /* we expect symmetric tx/rx vrings */
974 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
975 virtqueue_get_vring_size(vrp->svq));
976
977 /* we need less buffers if vrings are small */
978 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
979 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
980 else
981 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
982
983 total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
984
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200985 /* allocate coherent memory for the buffers */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300986 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
Suman Annab1b98912014-09-16 13:33:07 -0500987 total_buf_space, &vrp->bufs_dma,
988 GFP_KERNEL);
Wei Yongjun3119b482013-04-29 16:17:09 -0700989 if (!bufs_va) {
990 err = -ENOMEM;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200991 goto vqs_del;
Wei Yongjun3119b482013-04-29 16:17:09 -0700992 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200993
Mark Asselstine9d8ae5c2012-03-04 13:33:28 +0200994 dev_dbg(&vdev->dev, "buffers: va %p, dma 0x%llx\n", bufs_va,
995 (unsigned long long)vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200996
997 /* half of the buffers is dedicated for RX */
998 vrp->rbufs = bufs_va;
999
1000 /* and half is dedicated for TX */
Suman Annab1b98912014-09-16 13:33:07 -05001001 vrp->sbufs = bufs_va + total_buf_space / 2;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001002
1003 /* set up the receive buffers */
Suman Annab1b98912014-09-16 13:33:07 -05001004 for (i = 0; i < vrp->num_bufs / 2; i++) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001005 struct scatterlist sg;
1006 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
1007
1008 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
1009
Rusty Russellcee51d62013-03-20 15:44:29 +10301010 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001011 GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +10301012 WARN_ON(err); /* sanity check; this can't really happen */
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001013 }
1014
1015 /* suppress "tx-complete" interrupts */
1016 virtqueue_disable_cb(vrp->svq);
1017
1018 vdev->priv = vrp;
1019
1020 /* if supported by the remote processor, enable the name service */
1021 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
1022 /* a dedicated endpoint handles the name service msgs */
1023 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
1024 vrp, RPMSG_NS_ADDR);
1025 if (!vrp->ns_ept) {
1026 dev_err(&vdev->dev, "failed to create the ns ept\n");
1027 err = -ENOMEM;
1028 goto free_coherent;
1029 }
1030 }
1031
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301032 /*
1033 * Prepare to kick but don't notify yet - we can't do this before
1034 * device is ready.
1035 */
1036 notify = virtqueue_kick_prepare(vrp->rvq);
1037
1038 /* From this point on, we can notify and get callbacks. */
1039 virtio_device_ready(vdev);
1040
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001041 /* tell the remote processor it can start sending messages */
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301042 /*
1043 * this might be concurrent with callbacks, but we are only
1044 * doing notify, not a full kick here, so that's ok.
1045 */
1046 if (notify)
1047 virtqueue_notify(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001048
1049 dev_info(&vdev->dev, "rpmsg host is online\n");
1050
1051 return 0;
1052
1053free_coherent:
Suman Annab1b98912014-09-16 13:33:07 -05001054 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1055 bufs_va, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001056vqs_del:
1057 vdev->config->del_vqs(vrp->vdev);
1058free_vrp:
1059 kfree(vrp);
1060 return err;
1061}
1062
1063static int rpmsg_remove_device(struct device *dev, void *data)
1064{
1065 device_unregister(dev);
1066
1067 return 0;
1068}
1069
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001070static void rpmsg_remove(struct virtio_device *vdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001071{
1072 struct virtproc_info *vrp = vdev->priv;
Suman Annab1b98912014-09-16 13:33:07 -05001073 size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001074 int ret;
1075
1076 vdev->config->reset(vdev);
1077
1078 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1079 if (ret)
1080 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1081
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +02001082 if (vrp->ns_ept)
1083 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1084
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001085 idr_destroy(&vrp->endpoints);
1086
1087 vdev->config->del_vqs(vrp->vdev);
1088
Suman Annab1b98912014-09-16 13:33:07 -05001089 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1090 vrp->rbufs, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001091
1092 kfree(vrp);
1093}
1094
1095static struct virtio_device_id id_table[] = {
1096 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1097 { 0 },
1098};
1099
1100static unsigned int features[] = {
1101 VIRTIO_RPMSG_F_NS,
1102};
1103
1104static struct virtio_driver virtio_ipc_driver = {
1105 .feature_table = features,
1106 .feature_table_size = ARRAY_SIZE(features),
1107 .driver.name = KBUILD_MODNAME,
1108 .driver.owner = THIS_MODULE,
1109 .id_table = id_table,
1110 .probe = rpmsg_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001111 .remove = rpmsg_remove,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001112};
1113
1114static int __init rpmsg_init(void)
1115{
1116 int ret;
1117
1118 ret = bus_register(&rpmsg_bus);
1119 if (ret) {
1120 pr_err("failed to register rpmsg bus: %d\n", ret);
1121 return ret;
1122 }
1123
1124 ret = register_virtio_driver(&virtio_ipc_driver);
1125 if (ret) {
1126 pr_err("failed to register virtio driver: %d\n", ret);
1127 bus_unregister(&rpmsg_bus);
1128 }
1129
1130 return ret;
1131}
Federico Fuga96342522012-07-16 10:36:51 +03001132subsys_initcall(rpmsg_init);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001133
1134static void __exit rpmsg_fini(void)
1135{
1136 unregister_virtio_driver(&virtio_ipc_driver);
1137 bus_unregister(&rpmsg_bus);
1138}
1139module_exit(rpmsg_fini);
1140
1141MODULE_DEVICE_TABLE(virtio, id_table);
1142MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1143MODULE_LICENSE("GPL v2");