blob: 495fa0a282d3619972d210080e48e8aeaac3ddc9 [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>
Bjorn Anderssona16644c2016-09-01 15:27:53 -070036#include <linux/of_device.h>
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020037
38/**
39 * struct virtproc_info - virtual remote processor state
40 * @vdev: the virtio device
41 * @rvq: rx virtqueue
42 * @svq: tx virtqueue
43 * @rbufs: kernel address of rx buffers
44 * @sbufs: kernel address of tx buffers
Suman Annab1b98912014-09-16 13:33:07 -050045 * @num_bufs: total number of buffers for rx and tx
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020046 * @last_sbuf: index of last tx buffer used
47 * @bufs_dma: dma base addr of the buffers
48 * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
49 * sending a message might require waking up a dozing remote
50 * processor, which involves sleeping, hence the mutex.
51 * @endpoints: idr of local endpoints, allows fast retrieval
52 * @endpoints_lock: lock of the endpoints set
53 * @sendq: wait queue of sending contexts waiting for a tx buffers
54 * @sleepers: number of senders that are waiting for a tx buffer
55 * @ns_ept: the bus's name service endpoint
56 *
57 * This structure stores the rpmsg state of a given virtio remote processor
58 * device (there might be several virtio proc devices for each physical
59 * remote processor).
60 */
61struct virtproc_info {
62 struct virtio_device *vdev;
63 struct virtqueue *rvq, *svq;
64 void *rbufs, *sbufs;
Suman Annab1b98912014-09-16 13:33:07 -050065 unsigned int num_bufs;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020066 int last_sbuf;
67 dma_addr_t bufs_dma;
68 struct mutex tx_lock;
69 struct idr endpoints;
70 struct mutex endpoints_lock;
71 wait_queue_head_t sendq;
72 atomic_t sleepers;
73 struct rpmsg_endpoint *ns_ept;
74};
75
76/**
77 * struct rpmsg_channel_info - internal channel info representation
78 * @name: name of service
79 * @src: local address
80 * @dst: destination address
81 */
82struct rpmsg_channel_info {
83 char name[RPMSG_NAME_SIZE];
84 u32 src;
85 u32 dst;
86};
87
88#define to_rpmsg_channel(d) container_of(d, struct rpmsg_channel, dev)
89#define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
90
91/*
Suman Annab1b98912014-09-16 13:33:07 -050092 * We're allocating buffers of 512 bytes each for communications. The
93 * number of buffers will be computed from the number of buffers supported
94 * by the vring, upto a maximum of 512 buffers (256 in each direction).
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020095 *
96 * Each buffer will have 16 bytes for the msg header and 496 bytes for
97 * the payload.
98 *
Suman Annab1b98912014-09-16 13:33:07 -050099 * This will utilize a maximum total space of 256KB for the buffers.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200100 *
101 * We might also want to add support for user-provided buffers in time.
102 * This will allow bigger buffer size flexibility, and can also be used
103 * to achieve zero-copy messaging.
104 *
105 * Note that these numbers are purely a decision of this driver - we
106 * can change this without changing anything in the firmware of the remote
107 * processor.
108 */
Suman Annab1b98912014-09-16 13:33:07 -0500109#define MAX_RPMSG_NUM_BUFS (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200110#define RPMSG_BUF_SIZE (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200111
112/*
113 * Local addresses are dynamically allocated on-demand.
114 * We do not dynamically assign addresses from the low 1024 range,
115 * in order to reserve that address range for predefined services.
116 */
117#define RPMSG_RESERVED_ADDRESSES (1024)
118
119/* Address 53 is reserved for advertising remote services */
120#define RPMSG_NS_ADDR (53)
121
122/* sysfs show configuration fields */
123#define rpmsg_show_attr(field, path, format_string) \
124static ssize_t \
125field##_show(struct device *dev, \
126 struct device_attribute *attr, char *buf) \
127{ \
128 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev); \
129 \
130 return sprintf(buf, format_string, rpdev->path); \
131}
132
133/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
134rpmsg_show_attr(name, id.name, "%s\n");
135rpmsg_show_attr(src, src, "0x%x\n");
136rpmsg_show_attr(dst, dst, "0x%x\n");
137rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
138
139/*
140 * Unique (and free running) index for rpmsg devices.
141 *
142 * Yeah, we're not recycling those numbers (yet?). will be easy
143 * to change if/when we want to.
144 */
145static unsigned int rpmsg_dev_index;
146
147static ssize_t modalias_show(struct device *dev,
148 struct device_attribute *attr, char *buf)
149{
150 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
151
152 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
153}
154
155static struct device_attribute rpmsg_dev_attrs[] = {
156 __ATTR_RO(name),
157 __ATTR_RO(modalias),
158 __ATTR_RO(dst),
159 __ATTR_RO(src),
160 __ATTR_RO(announce),
161 __ATTR_NULL
162};
163
164/* rpmsg devices and drivers are matched using the service name */
165static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500166 const struct rpmsg_device_id *id)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200167{
168 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
169}
170
171/* match rpmsg channel and rpmsg driver */
172static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
173{
174 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
175 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
176 const struct rpmsg_device_id *ids = rpdrv->id_table;
177 unsigned int i;
178
Bjorn Anderssona16644c2016-09-01 15:27:53 -0700179 if (ids)
180 for (i = 0; ids[i].name[0]; i++)
181 if (rpmsg_id_match(rpdev, &ids[i]))
182 return 1;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200183
Bjorn Anderssona16644c2016-09-01 15:27:53 -0700184 return of_driver_match_device(dev, drv);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200185}
186
187static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
188{
189 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
190
191 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
192 rpdev->id.name);
193}
194
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300195/**
196 * __ept_release() - deallocate an rpmsg endpoint
197 * @kref: the ept's reference count
198 *
199 * This function deallocates an ept, and is invoked when its @kref refcount
200 * drops to zero.
201 *
202 * Never invoke this function directly!
203 */
204static void __ept_release(struct kref *kref)
205{
206 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
207 refcount);
208 /*
209 * At this point no one holds a reference to ept anymore,
210 * so we can directly free it
211 */
212 kfree(ept);
213}
214
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200215/* for more info, see below documentation of rpmsg_create_ept() */
216static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
Anna, Suman09636792016-08-12 18:42:26 -0500217 struct rpmsg_channel *rpdev,
218 rpmsg_rx_cb_t cb,
219 void *priv, u32 addr)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200220{
Tejun Heod0ffce72013-02-27 17:04:40 -0800221 int id_min, id_max, id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200222 struct rpmsg_endpoint *ept;
223 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
224
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200225 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500226 if (!ept)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200227 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200228
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300229 kref_init(&ept->refcount);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300230 mutex_init(&ept->cb_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300231
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200232 ept->rpdev = rpdev;
233 ept->cb = cb;
234 ept->priv = priv;
235
236 /* do we need to allocate a local address ? */
Tejun Heod0ffce72013-02-27 17:04:40 -0800237 if (addr == RPMSG_ADDR_ANY) {
238 id_min = RPMSG_RESERVED_ADDRESSES;
239 id_max = 0;
240 } else {
241 id_min = addr;
242 id_max = addr + 1;
243 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200244
245 mutex_lock(&vrp->endpoints_lock);
246
247 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
Tejun Heod0ffce72013-02-27 17:04:40 -0800248 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
249 if (id < 0) {
250 dev_err(dev, "idr_alloc failed: %d\n", id);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200251 goto free_ept;
252 }
Tejun Heod0ffce72013-02-27 17:04:40 -0800253 ept->addr = id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200254
255 mutex_unlock(&vrp->endpoints_lock);
256
257 return ept;
258
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200259free_ept:
260 mutex_unlock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300261 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200262 return NULL;
263}
264
265/**
266 * rpmsg_create_ept() - create a new rpmsg_endpoint
267 * @rpdev: rpmsg channel device
268 * @cb: rx callback handler
269 * @priv: private data for the driver's use
270 * @addr: local rpmsg address to bind with @cb
271 *
272 * Every rpmsg address in the system is bound to an rx callback (so when
273 * inbound messages arrive, they are dispatched by the rpmsg bus using the
274 * appropriate callback handler) by means of an rpmsg_endpoint struct.
275 *
276 * This function allows drivers to create such an endpoint, and by that,
277 * bind a callback, and possibly some private data too, to an rpmsg address
278 * (either one that is known in advance, or one that will be dynamically
279 * assigned for them).
280 *
281 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
282 * is already created for them when they are probed by the rpmsg bus
283 * (using the rx callback provided when they registered to the rpmsg bus).
284 *
285 * So things should just work for simple drivers: they already have an
286 * endpoint, their rx callback is bound to their rpmsg address, and when
287 * relevant inbound messages arrive (i.e. messages which their dst address
288 * equals to the src address of their rpmsg channel), the driver's handler
289 * is invoked to process it.
290 *
291 * That said, more complicated drivers might do need to allocate
292 * additional rpmsg addresses, and bind them to different rx callbacks.
293 * To accomplish that, those drivers need to call this function.
294 *
295 * Drivers should provide their @rpdev channel (so the new endpoint would belong
296 * to the same remote processor their channel belongs to), an rx callback
297 * function, an optional private data (which is provided back when the
298 * rx callback is invoked), and an address they want to bind with the
299 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
300 * dynamically assign them an available rpmsg address (drivers should have
301 * a very good reason why not to always use RPMSG_ADDR_ANY here).
302 *
303 * Returns a pointer to the endpoint on success, or NULL on error.
304 */
305struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500306 rpmsg_rx_cb_t cb, void *priv, u32 addr)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200307{
308 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
309}
310EXPORT_SYMBOL(rpmsg_create_ept);
311
312/**
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200313 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
314 * @vrp: virtproc which owns this ept
315 * @ept: endpoing to destroy
316 *
317 * An internal function which destroy an ept without assuming it is
318 * bound to an rpmsg channel. This is needed for handling the internal
319 * name service endpoint, which isn't bound to an rpmsg channel.
320 * See also __rpmsg_create_ept().
321 */
322static void
323__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
324{
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300325 /* make sure new inbound messages can't find this ept anymore */
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200326 mutex_lock(&vrp->endpoints_lock);
327 idr_remove(&vrp->endpoints, ept->addr);
328 mutex_unlock(&vrp->endpoints_lock);
329
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300330 /* make sure in-flight inbound messages won't invoke cb anymore */
331 mutex_lock(&ept->cb_lock);
332 ept->cb = NULL;
333 mutex_unlock(&ept->cb_lock);
334
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300335 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200336}
337
338/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200339 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
340 * @ept: endpoing to destroy
341 *
342 * Should be used by drivers to destroy an rpmsg endpoint previously
343 * created with rpmsg_create_ept().
344 */
345void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
346{
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200347 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200348}
349EXPORT_SYMBOL(rpmsg_destroy_ept);
350
351/*
352 * when an rpmsg driver is probed with a channel, we seamlessly create
353 * it an endpoint, binding its rx callback to a unique local rpmsg
354 * address.
355 *
356 * if we need to, we also announce about this channel to the remote
357 * processor (needed in case the driver is exposing an rpmsg service).
358 */
359static int rpmsg_dev_probe(struct device *dev)
360{
361 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
362 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
363 struct virtproc_info *vrp = rpdev->vrp;
364 struct rpmsg_endpoint *ept;
365 int err;
366
367 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
368 if (!ept) {
369 dev_err(dev, "failed to create endpoint\n");
370 err = -ENOMEM;
371 goto out;
372 }
373
374 rpdev->ept = ept;
375 rpdev->src = ept->addr;
376
377 err = rpdrv->probe(rpdev);
378 if (err) {
379 dev_err(dev, "%s: failed: %d\n", __func__, err);
380 rpmsg_destroy_ept(ept);
381 goto out;
382 }
383
384 /* need to tell remote processor's name service about this channel ? */
385 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500386 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200387 struct rpmsg_ns_msg nsm;
388
389 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
390 nsm.addr = rpdev->src;
391 nsm.flags = RPMSG_NS_CREATE;
392
393 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
394 if (err)
395 dev_err(dev, "failed to announce service %d\n", err);
396 }
397
398out:
399 return err;
400}
401
402static int rpmsg_dev_remove(struct device *dev)
403{
404 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
405 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
406 struct virtproc_info *vrp = rpdev->vrp;
407 int err = 0;
408
409 /* tell remote processor's name service we're removing this channel */
410 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500411 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200412 struct rpmsg_ns_msg nsm;
413
414 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
415 nsm.addr = rpdev->src;
416 nsm.flags = RPMSG_NS_DESTROY;
417
418 err = rpmsg_sendto(rpdev, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
419 if (err)
420 dev_err(dev, "failed to announce service %d\n", err);
421 }
422
423 rpdrv->remove(rpdev);
424
425 rpmsg_destroy_ept(rpdev->ept);
426
427 return err;
428}
429
430static struct bus_type rpmsg_bus = {
431 .name = "rpmsg",
432 .match = rpmsg_dev_match,
433 .dev_attrs = rpmsg_dev_attrs,
434 .uevent = rpmsg_uevent,
435 .probe = rpmsg_dev_probe,
436 .remove = rpmsg_dev_remove,
437};
438
439/**
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500440 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200441 * @rpdrv: pointer to a struct rpmsg_driver
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500442 * @owner: owning module/driver
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200443 *
444 * Returns 0 on success, and an appropriate error value on failure.
445 */
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500446int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200447{
448 rpdrv->drv.bus = &rpmsg_bus;
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500449 rpdrv->drv.owner = owner;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200450 return driver_register(&rpdrv->drv);
451}
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500452EXPORT_SYMBOL(__register_rpmsg_driver);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200453
454/**
455 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
456 * @rpdrv: pointer to a struct rpmsg_driver
457 *
458 * Returns 0 on success, and an appropriate error value on failure.
459 */
460void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
461{
462 driver_unregister(&rpdrv->drv);
463}
464EXPORT_SYMBOL(unregister_rpmsg_driver);
465
466static void rpmsg_release_device(struct device *dev)
467{
468 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
469
470 kfree(rpdev);
471}
472
473/*
474 * match an rpmsg channel with a channel info struct.
475 * this is used to make sure we're not creating rpmsg devices for channels
476 * that already exist.
477 */
478static int rpmsg_channel_match(struct device *dev, void *data)
479{
480 struct rpmsg_channel_info *chinfo = data;
481 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
482
483 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
484 return 0;
485
486 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
487 return 0;
488
489 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
490 return 0;
491
492 /* found a match ! */
493 return 1;
494}
495
496/*
497 * create an rpmsg channel using its name and address info.
498 * this function will be used to create both static and dynamic
499 * channels.
500 */
501static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
502 struct rpmsg_channel_info *chinfo)
503{
504 struct rpmsg_channel *rpdev;
505 struct device *tmp, *dev = &vrp->vdev->dev;
506 int ret;
507
508 /* make sure a similar channel doesn't already exist */
509 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
510 if (tmp) {
511 /* decrement the matched device's refcount back */
512 put_device(tmp);
513 dev_err(dev, "channel %s:%x:%x already exist\n",
514 chinfo->name, chinfo->src, chinfo->dst);
515 return NULL;
516 }
517
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500518 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
519 if (!rpdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200520 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200521
522 rpdev->vrp = vrp;
523 rpdev->src = chinfo->src;
524 rpdev->dst = chinfo->dst;
525
526 /*
527 * rpmsg server channels has predefined local address (for now),
528 * and their existence needs to be announced remotely
529 */
Andrew F. Davisc8ced112016-07-01 09:24:58 -0500530 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200531
532 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
533
534 /* very simple device indexing plumbing which is enough for now */
535 dev_set_name(&rpdev->dev, "rpmsg%d", rpmsg_dev_index++);
536
537 rpdev->dev.parent = &vrp->vdev->dev;
538 rpdev->dev.bus = &rpmsg_bus;
539 rpdev->dev.release = rpmsg_release_device;
540
541 ret = device_register(&rpdev->dev);
542 if (ret) {
543 dev_err(dev, "device_register failed: %d\n", ret);
544 put_device(&rpdev->dev);
545 return NULL;
546 }
547
548 return rpdev;
549}
550
551/*
552 * find an existing channel using its name + address properties,
553 * and destroy it
554 */
555static int rpmsg_destroy_channel(struct virtproc_info *vrp,
Anna, Suman09636792016-08-12 18:42:26 -0500556 struct rpmsg_channel_info *chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200557{
558 struct virtio_device *vdev = vrp->vdev;
559 struct device *dev;
560
561 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
562 if (!dev)
563 return -EINVAL;
564
565 device_unregister(dev);
566
567 put_device(dev);
568
569 return 0;
570}
571
572/* super simple buffer "allocator" that is just enough for now */
573static void *get_a_tx_buf(struct virtproc_info *vrp)
574{
575 unsigned int len;
576 void *ret;
577
578 /* support multiple concurrent senders */
579 mutex_lock(&vrp->tx_lock);
580
581 /*
582 * either pick the next unused tx buffer
583 * (half of our buffers are used for sending messages)
584 */
Suman Annab1b98912014-09-16 13:33:07 -0500585 if (vrp->last_sbuf < vrp->num_bufs / 2)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200586 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
587 /* or recycle a used one */
588 else
589 ret = virtqueue_get_buf(vrp->svq, &len);
590
591 mutex_unlock(&vrp->tx_lock);
592
593 return ret;
594}
595
596/**
597 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
598 * @vrp: virtual remote processor state
599 *
600 * This function is called before a sender is blocked, waiting for
601 * a tx buffer to become available.
602 *
603 * If we already have blocking senders, this function merely increases
604 * the "sleepers" reference count, and exits.
605 *
606 * Otherwise, if this is the first sender to block, we also enable
607 * virtio's tx callbacks, so we'd be immediately notified when a tx
608 * buffer is consumed (we rely on virtio's tx callback in order
609 * to wake up sleeping senders as soon as a tx buffer is used by the
610 * remote processor).
611 */
612static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
613{
614 /* support multiple concurrent senders */
615 mutex_lock(&vrp->tx_lock);
616
617 /* are we the first sleeping context waiting for tx buffers ? */
618 if (atomic_inc_return(&vrp->sleepers) == 1)
619 /* enable "tx-complete" interrupts before dozing off */
620 virtqueue_enable_cb(vrp->svq);
621
622 mutex_unlock(&vrp->tx_lock);
623}
624
625/**
626 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
627 * @vrp: virtual remote processor state
628 *
629 * This function is called after a sender, that waited for a tx buffer
630 * to become available, is unblocked.
631 *
632 * If we still have blocking senders, this function merely decreases
633 * the "sleepers" reference count, and exits.
634 *
635 * Otherwise, if there are no more blocking senders, we also disable
636 * virtio's tx callbacks, to avoid the overhead incurred with handling
637 * those (now redundant) interrupts.
638 */
639static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
640{
641 /* support multiple concurrent senders */
642 mutex_lock(&vrp->tx_lock);
643
644 /* are we the last sleeping context waiting for tx buffers ? */
645 if (atomic_dec_and_test(&vrp->sleepers))
646 /* disable "tx-complete" interrupts */
647 virtqueue_disable_cb(vrp->svq);
648
649 mutex_unlock(&vrp->tx_lock);
650}
651
652/**
653 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
654 * @rpdev: the rpmsg channel
655 * @src: source address
656 * @dst: destination address
657 * @data: payload of message
658 * @len: length of payload
659 * @wait: indicates whether caller should block in case no TX buffers available
660 *
661 * This function is the base implementation for all of the rpmsg sending API.
662 *
663 * It will send @data of length @len to @dst, and say it's from @src. The
664 * message will be sent to the remote processor which the @rpdev channel
665 * belongs to.
666 *
667 * The message is sent using one of the TX buffers that are available for
668 * communication with this remote processor.
669 *
670 * If @wait is true, the caller will be blocked until either a TX buffer is
671 * available, or 15 seconds elapses (we don't want callers to
672 * sleep indefinitely due to misbehaving remote processors), and in that
673 * case -ERESTARTSYS is returned. The number '15' itself was picked
674 * arbitrarily; there's little point in asking drivers to provide a timeout
675 * value themselves.
676 *
677 * Otherwise, if @wait is false, and there are no TX buffers available,
678 * the function will immediately fail, and -ENOMEM will be returned.
679 *
680 * Normally drivers shouldn't use this function directly; instead, drivers
681 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
682 * (see include/linux/rpmsg.h).
683 *
684 * Returns 0 on success and an appropriate error value on failure.
685 */
686int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500687 void *data, int len, bool wait)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200688{
689 struct virtproc_info *vrp = rpdev->vrp;
690 struct device *dev = &rpdev->dev;
691 struct scatterlist sg;
692 struct rpmsg_hdr *msg;
693 int err;
694
695 /* bcasting isn't allowed */
696 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
697 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
698 return -EINVAL;
699 }
700
701 /*
702 * We currently use fixed-sized buffers, and therefore the payload
703 * length is limited.
704 *
705 * One of the possible improvements here is either to support
706 * user-provided buffers (and then we can also support zero-copy
707 * messaging), or to improve the buffer allocator, to support
708 * variable-length buffer sizes.
709 */
710 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
711 dev_err(dev, "message is too big (%d)\n", len);
712 return -EMSGSIZE;
713 }
714
715 /* grab a buffer */
716 msg = get_a_tx_buf(vrp);
717 if (!msg && !wait)
718 return -ENOMEM;
719
720 /* no free buffer ? wait for one (but bail after 15 seconds) */
721 while (!msg) {
722 /* enable "tx-complete" interrupts, if not already enabled */
723 rpmsg_upref_sleepers(vrp);
724
725 /*
726 * sleep until a free buffer is available or 15 secs elapse.
727 * the timeout period is not configurable because there's
728 * little point in asking drivers to specify that.
729 * if later this happens to be required, it'd be easy to add.
730 */
731 err = wait_event_interruptible_timeout(vrp->sendq,
732 (msg = get_a_tx_buf(vrp)),
733 msecs_to_jiffies(15000));
734
735 /* disable "tx-complete" interrupts if we're the last sleeper */
736 rpmsg_downref_sleepers(vrp);
737
738 /* timeout ? */
739 if (!err) {
740 dev_err(dev, "timeout waiting for a tx buffer\n");
741 return -ERESTARTSYS;
742 }
743 }
744
745 msg->len = len;
746 msg->flags = 0;
747 msg->src = src;
748 msg->dst = dst;
749 msg->reserved = 0;
750 memcpy(msg->data, data, len);
751
752 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500753 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500754#if defined(CONFIG_DYNAMIC_DEBUG)
755 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
756 msg, sizeof(*msg) + msg->len, true);
757#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200758
759 sg_init_one(&sg, msg, sizeof(*msg) + len);
760
761 mutex_lock(&vrp->tx_lock);
762
763 /* add message to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030764 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030765 if (err) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200766 /*
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.
770 */
Rusty Russellcee51d62013-03-20 15:44:29 +1030771 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200772 goto out;
773 }
774
775 /* tell the remote processor it has a pending message to read */
776 virtqueue_kick(vrp->svq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200777out:
778 mutex_unlock(&vrp->tx_lock);
779 return err;
780}
781EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
782
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300783static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
784 struct rpmsg_hdr *msg, unsigned int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200785{
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200786 struct rpmsg_endpoint *ept;
787 struct scatterlist sg;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200788 int err;
789
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200790 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500791 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500792#if defined(CONFIG_DYNAMIC_DEBUG)
793 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
794 msg, sizeof(*msg) + msg->len, true);
795#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200796
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200797 /*
798 * We currently use fixed-sized buffers, so trivially sanitize
799 * the reported payload length.
800 */
801 if (len > RPMSG_BUF_SIZE ||
Anna, Suman09636792016-08-12 18:42:26 -0500802 msg->len > (len - sizeof(struct rpmsg_hdr))) {
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200803 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300804 return -EINVAL;
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200805 }
806
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200807 /* use the dst addr to fetch the callback of the appropriate user */
808 mutex_lock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300809
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200810 ept = idr_find(&vrp->endpoints, msg->dst);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300811
812 /* let's make sure no one deallocates ept while we use it */
813 if (ept)
814 kref_get(&ept->refcount);
815
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200816 mutex_unlock(&vrp->endpoints_lock);
817
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300818 if (ept) {
819 /* make sure ept->cb doesn't go away while we use it */
820 mutex_lock(&ept->cb_lock);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200821
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300822 if (ept->cb)
823 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
824 msg->src);
825
826 mutex_unlock(&ept->cb_lock);
827
828 /* farewell, ept, we don't need you anymore */
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300829 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300830 } else
Masanari Iida8a168ca2012-12-29 02:00:09 +0900831 dev_warn(dev, "msg received with no recipient\n");
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300832
Ohad Ben-Cohenf1d9e9c2012-02-28 16:11:28 +0200833 /* publish the real size of the buffer */
834 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200835
836 /* add the buffer back to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030837 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200838 if (err < 0) {
839 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300840 return err;
841 }
842
843 return 0;
844}
845
846/* called when an rx buffer is used, and it's time to digest a message */
847static void rpmsg_recv_done(struct virtqueue *rvq)
848{
849 struct virtproc_info *vrp = rvq->vdev->priv;
850 struct device *dev = &rvq->vdev->dev;
851 struct rpmsg_hdr *msg;
852 unsigned int len, msgs_received = 0;
853 int err;
854
855 msg = virtqueue_get_buf(rvq, &len);
856 if (!msg) {
857 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200858 return;
859 }
860
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300861 while (msg) {
862 err = rpmsg_recv_single(vrp, dev, msg, len);
863 if (err)
864 break;
865
866 msgs_received++;
867
868 msg = virtqueue_get_buf(rvq, &len);
Lee Jones6c49fbe2016-07-20 10:29:35 +0100869 }
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300870
871 dev_dbg(dev, "Received %u messages\n", msgs_received);
872
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200873 /* tell the remote processor we added another available rx buffer */
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300874 if (msgs_received)
875 virtqueue_kick(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200876}
877
878/*
879 * This is invoked whenever the remote processor completed processing
880 * a TX msg we just sent it, and the buffer is put back to the used ring.
881 *
882 * Normally, though, we suppress this "tx complete" interrupt in order to
883 * avoid the incurred overhead.
884 */
885static void rpmsg_xmit_done(struct virtqueue *svq)
886{
887 struct virtproc_info *vrp = svq->vdev->priv;
888
889 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
890
891 /* wake up potential senders that are waiting for a tx buffer */
892 wake_up_interruptible(&vrp->sendq);
893}
894
895/* invoked when a name service announcement arrives */
896static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
Anna, Suman09636792016-08-12 18:42:26 -0500897 void *priv, u32 src)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200898{
899 struct rpmsg_ns_msg *msg = data;
900 struct rpmsg_channel *newch;
901 struct rpmsg_channel_info chinfo;
902 struct virtproc_info *vrp = priv;
903 struct device *dev = &vrp->vdev->dev;
904 int ret;
905
Anna, Suman211e3a92016-08-12 18:42:27 -0500906#if defined(CONFIG_DYNAMIC_DEBUG)
907 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
908 data, len, true);
909#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200910
911 if (len != sizeof(*msg)) {
912 dev_err(dev, "malformed ns msg (%d)\n", len);
913 return;
914 }
915
916 /*
917 * the name service ept does _not_ belong to a real rpmsg channel,
918 * and is handled by the rpmsg bus itself.
919 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
920 * in somehow.
921 */
922 if (rpdev) {
923 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
924 return;
925 }
926
927 /* don't trust the remote processor for null terminating the name */
928 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
929
930 dev_info(dev, "%sing channel %s addr 0x%x\n",
Anna, Suman09636792016-08-12 18:42:26 -0500931 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
932 msg->name, msg->addr);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200933
934 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
935 chinfo.src = RPMSG_ADDR_ANY;
936 chinfo.dst = msg->addr;
937
938 if (msg->flags & RPMSG_NS_DESTROY) {
939 ret = rpmsg_destroy_channel(vrp, &chinfo);
940 if (ret)
941 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
942 } else {
943 newch = rpmsg_create_channel(vrp, &chinfo);
944 if (!newch)
945 dev_err(dev, "rpmsg_create_channel failed\n");
946 }
947}
948
949static int rpmsg_probe(struct virtio_device *vdev)
950{
951 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800952 static const char * const names[] = { "input", "output" };
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200953 struct virtqueue *vqs[2];
954 struct virtproc_info *vrp;
955 void *bufs_va;
956 int err = 0, i;
Suman Annab1b98912014-09-16 13:33:07 -0500957 size_t total_buf_space;
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030958 bool notify;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200959
960 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
961 if (!vrp)
962 return -ENOMEM;
963
964 vrp->vdev = vdev;
965
966 idr_init(&vrp->endpoints);
967 mutex_init(&vrp->endpoints_lock);
968 mutex_init(&vrp->tx_lock);
969 init_waitqueue_head(&vrp->sendq);
970
971 /* We expect two virtqueues, rx and tx (and in this order) */
972 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
973 if (err)
974 goto free_vrp;
975
976 vrp->rvq = vqs[0];
977 vrp->svq = vqs[1];
978
Suman Annab1b98912014-09-16 13:33:07 -0500979 /* we expect symmetric tx/rx vrings */
980 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
981 virtqueue_get_vring_size(vrp->svq));
982
983 /* we need less buffers if vrings are small */
984 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
985 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
986 else
987 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
988
989 total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
990
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200991 /* allocate coherent memory for the buffers */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300992 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
Suman Annab1b98912014-09-16 13:33:07 -0500993 total_buf_space, &vrp->bufs_dma,
994 GFP_KERNEL);
Wei Yongjun3119b482013-04-29 16:17:09 -0700995 if (!bufs_va) {
996 err = -ENOMEM;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200997 goto vqs_del;
Wei Yongjun3119b482013-04-29 16:17:09 -0700998 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200999
Anna, Suman8d95b322016-08-12 18:42:25 -05001000 dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
1001 bufs_va, &vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001002
1003 /* half of the buffers is dedicated for RX */
1004 vrp->rbufs = bufs_va;
1005
1006 /* and half is dedicated for TX */
Suman Annab1b98912014-09-16 13:33:07 -05001007 vrp->sbufs = bufs_va + total_buf_space / 2;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001008
1009 /* set up the receive buffers */
Suman Annab1b98912014-09-16 13:33:07 -05001010 for (i = 0; i < vrp->num_bufs / 2; i++) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001011 struct scatterlist sg;
1012 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
1013
1014 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
1015
Rusty Russellcee51d62013-03-20 15:44:29 +10301016 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
Anna, Suman09636792016-08-12 18:42:26 -05001017 GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +10301018 WARN_ON(err); /* sanity check; this can't really happen */
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001019 }
1020
1021 /* suppress "tx-complete" interrupts */
1022 virtqueue_disable_cb(vrp->svq);
1023
1024 vdev->priv = vrp;
1025
1026 /* if supported by the remote processor, enable the name service */
1027 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
1028 /* a dedicated endpoint handles the name service msgs */
1029 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
1030 vrp, RPMSG_NS_ADDR);
1031 if (!vrp->ns_ept) {
1032 dev_err(&vdev->dev, "failed to create the ns ept\n");
1033 err = -ENOMEM;
1034 goto free_coherent;
1035 }
1036 }
1037
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301038 /*
1039 * Prepare to kick but don't notify yet - we can't do this before
1040 * device is ready.
1041 */
1042 notify = virtqueue_kick_prepare(vrp->rvq);
1043
1044 /* From this point on, we can notify and get callbacks. */
1045 virtio_device_ready(vdev);
1046
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001047 /* tell the remote processor it can start sending messages */
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301048 /*
1049 * this might be concurrent with callbacks, but we are only
1050 * doing notify, not a full kick here, so that's ok.
1051 */
1052 if (notify)
1053 virtqueue_notify(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001054
1055 dev_info(&vdev->dev, "rpmsg host is online\n");
1056
1057 return 0;
1058
1059free_coherent:
Suman Annab1b98912014-09-16 13:33:07 -05001060 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1061 bufs_va, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001062vqs_del:
1063 vdev->config->del_vqs(vrp->vdev);
1064free_vrp:
1065 kfree(vrp);
1066 return err;
1067}
1068
1069static int rpmsg_remove_device(struct device *dev, void *data)
1070{
1071 device_unregister(dev);
1072
1073 return 0;
1074}
1075
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001076static void rpmsg_remove(struct virtio_device *vdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001077{
1078 struct virtproc_info *vrp = vdev->priv;
Suman Annab1b98912014-09-16 13:33:07 -05001079 size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001080 int ret;
1081
1082 vdev->config->reset(vdev);
1083
1084 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1085 if (ret)
1086 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1087
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +02001088 if (vrp->ns_ept)
1089 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1090
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001091 idr_destroy(&vrp->endpoints);
1092
1093 vdev->config->del_vqs(vrp->vdev);
1094
Suman Annab1b98912014-09-16 13:33:07 -05001095 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1096 vrp->rbufs, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001097
1098 kfree(vrp);
1099}
1100
1101static struct virtio_device_id id_table[] = {
1102 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1103 { 0 },
1104};
1105
1106static unsigned int features[] = {
1107 VIRTIO_RPMSG_F_NS,
1108};
1109
1110static struct virtio_driver virtio_ipc_driver = {
1111 .feature_table = features,
1112 .feature_table_size = ARRAY_SIZE(features),
1113 .driver.name = KBUILD_MODNAME,
1114 .driver.owner = THIS_MODULE,
1115 .id_table = id_table,
1116 .probe = rpmsg_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001117 .remove = rpmsg_remove,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001118};
1119
1120static int __init rpmsg_init(void)
1121{
1122 int ret;
1123
1124 ret = bus_register(&rpmsg_bus);
1125 if (ret) {
1126 pr_err("failed to register rpmsg bus: %d\n", ret);
1127 return ret;
1128 }
1129
1130 ret = register_virtio_driver(&virtio_ipc_driver);
1131 if (ret) {
1132 pr_err("failed to register virtio driver: %d\n", ret);
1133 bus_unregister(&rpmsg_bus);
1134 }
1135
1136 return ret;
1137}
Federico Fuga96342522012-07-16 10:36:51 +03001138subsys_initcall(rpmsg_init);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001139
1140static void __exit rpmsg_fini(void)
1141{
1142 unregister_virtio_driver(&virtio_ipc_driver);
1143 bus_unregister(&rpmsg_bus);
1144}
1145module_exit(rpmsg_fini);
1146
1147MODULE_DEVICE_TABLE(virtio, id_table);
1148MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1149MODULE_LICENSE("GPL v2");