blob: 345036b485d7fc27d6f7a7dc4509a4b3a39f7b11 [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
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200139static ssize_t modalias_show(struct device *dev,
140 struct device_attribute *attr, char *buf)
141{
142 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
143
144 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
145}
146
147static struct device_attribute rpmsg_dev_attrs[] = {
148 __ATTR_RO(name),
149 __ATTR_RO(modalias),
150 __ATTR_RO(dst),
151 __ATTR_RO(src),
152 __ATTR_RO(announce),
153 __ATTR_NULL
154};
155
156/* rpmsg devices and drivers are matched using the service name */
157static inline int rpmsg_id_match(const struct rpmsg_channel *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500158 const struct rpmsg_device_id *id)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200159{
160 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
161}
162
163/* match rpmsg channel and rpmsg driver */
164static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
165{
166 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
167 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
168 const struct rpmsg_device_id *ids = rpdrv->id_table;
169 unsigned int i;
170
Bjorn Anderssona16644c2016-09-01 15:27:53 -0700171 if (ids)
172 for (i = 0; ids[i].name[0]; i++)
173 if (rpmsg_id_match(rpdev, &ids[i]))
174 return 1;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200175
Bjorn Anderssona16644c2016-09-01 15:27:53 -0700176 return of_driver_match_device(dev, drv);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200177}
178
179static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
180{
181 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
182
183 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
184 rpdev->id.name);
185}
186
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300187/**
188 * __ept_release() - deallocate an rpmsg endpoint
189 * @kref: the ept's reference count
190 *
191 * This function deallocates an ept, and is invoked when its @kref refcount
192 * drops to zero.
193 *
194 * Never invoke this function directly!
195 */
196static void __ept_release(struct kref *kref)
197{
198 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
199 refcount);
200 /*
201 * At this point no one holds a reference to ept anymore,
202 * so we can directly free it
203 */
204 kfree(ept);
205}
206
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200207/* for more info, see below documentation of rpmsg_create_ept() */
208static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
Anna, Suman09636792016-08-12 18:42:26 -0500209 struct rpmsg_channel *rpdev,
210 rpmsg_rx_cb_t cb,
211 void *priv, u32 addr)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200212{
Tejun Heod0ffce72013-02-27 17:04:40 -0800213 int id_min, id_max, id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200214 struct rpmsg_endpoint *ept;
215 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
216
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200217 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500218 if (!ept)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200219 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200220
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300221 kref_init(&ept->refcount);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300222 mutex_init(&ept->cb_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300223
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200224 ept->rpdev = rpdev;
225 ept->cb = cb;
226 ept->priv = priv;
227
228 /* do we need to allocate a local address ? */
Tejun Heod0ffce72013-02-27 17:04:40 -0800229 if (addr == RPMSG_ADDR_ANY) {
230 id_min = RPMSG_RESERVED_ADDRESSES;
231 id_max = 0;
232 } else {
233 id_min = addr;
234 id_max = addr + 1;
235 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200236
237 mutex_lock(&vrp->endpoints_lock);
238
239 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
Tejun Heod0ffce72013-02-27 17:04:40 -0800240 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
241 if (id < 0) {
242 dev_err(dev, "idr_alloc failed: %d\n", id);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200243 goto free_ept;
244 }
Tejun Heod0ffce72013-02-27 17:04:40 -0800245 ept->addr = id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200246
247 mutex_unlock(&vrp->endpoints_lock);
248
249 return ept;
250
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200251free_ept:
252 mutex_unlock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300253 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200254 return NULL;
255}
256
257/**
258 * rpmsg_create_ept() - create a new rpmsg_endpoint
259 * @rpdev: rpmsg channel device
260 * @cb: rx callback handler
261 * @priv: private data for the driver's use
262 * @addr: local rpmsg address to bind with @cb
263 *
264 * Every rpmsg address in the system is bound to an rx callback (so when
265 * inbound messages arrive, they are dispatched by the rpmsg bus using the
266 * appropriate callback handler) by means of an rpmsg_endpoint struct.
267 *
268 * This function allows drivers to create such an endpoint, and by that,
269 * bind a callback, and possibly some private data too, to an rpmsg address
270 * (either one that is known in advance, or one that will be dynamically
271 * assigned for them).
272 *
273 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
274 * is already created for them when they are probed by the rpmsg bus
275 * (using the rx callback provided when they registered to the rpmsg bus).
276 *
277 * So things should just work for simple drivers: they already have an
278 * endpoint, their rx callback is bound to their rpmsg address, and when
279 * relevant inbound messages arrive (i.e. messages which their dst address
280 * equals to the src address of their rpmsg channel), the driver's handler
281 * is invoked to process it.
282 *
283 * That said, more complicated drivers might do need to allocate
284 * additional rpmsg addresses, and bind them to different rx callbacks.
285 * To accomplish that, those drivers need to call this function.
286 *
287 * Drivers should provide their @rpdev channel (so the new endpoint would belong
288 * to the same remote processor their channel belongs to), an rx callback
289 * function, an optional private data (which is provided back when the
290 * rx callback is invoked), and an address they want to bind with the
291 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
292 * dynamically assign them an available rpmsg address (drivers should have
293 * a very good reason why not to always use RPMSG_ADDR_ANY here).
294 *
295 * Returns a pointer to the endpoint on success, or NULL on error.
296 */
297struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500298 rpmsg_rx_cb_t cb, void *priv, u32 addr)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200299{
300 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, addr);
301}
302EXPORT_SYMBOL(rpmsg_create_ept);
303
304/**
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200305 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
306 * @vrp: virtproc which owns this ept
307 * @ept: endpoing to destroy
308 *
309 * An internal function which destroy an ept without assuming it is
310 * bound to an rpmsg channel. This is needed for handling the internal
311 * name service endpoint, which isn't bound to an rpmsg channel.
312 * See also __rpmsg_create_ept().
313 */
314static void
315__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
316{
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300317 /* make sure new inbound messages can't find this ept anymore */
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200318 mutex_lock(&vrp->endpoints_lock);
319 idr_remove(&vrp->endpoints, ept->addr);
320 mutex_unlock(&vrp->endpoints_lock);
321
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300322 /* make sure in-flight inbound messages won't invoke cb anymore */
323 mutex_lock(&ept->cb_lock);
324 ept->cb = NULL;
325 mutex_unlock(&ept->cb_lock);
326
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300327 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200328}
329
330/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200331 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
332 * @ept: endpoing to destroy
333 *
334 * Should be used by drivers to destroy an rpmsg endpoint previously
335 * created with rpmsg_create_ept().
336 */
337void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
338{
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200339 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200340}
341EXPORT_SYMBOL(rpmsg_destroy_ept);
342
343/*
344 * when an rpmsg driver is probed with a channel, we seamlessly create
345 * it an endpoint, binding its rx callback to a unique local rpmsg
346 * address.
347 *
348 * if we need to, we also announce about this channel to the remote
349 * processor (needed in case the driver is exposing an rpmsg service).
350 */
351static int rpmsg_dev_probe(struct device *dev)
352{
353 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
354 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
355 struct virtproc_info *vrp = rpdev->vrp;
356 struct rpmsg_endpoint *ept;
357 int err;
358
359 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, rpdev->src);
360 if (!ept) {
361 dev_err(dev, "failed to create endpoint\n");
362 err = -ENOMEM;
363 goto out;
364 }
365
366 rpdev->ept = ept;
367 rpdev->src = ept->addr;
368
369 err = rpdrv->probe(rpdev);
370 if (err) {
371 dev_err(dev, "%s: failed: %d\n", __func__, err);
372 rpmsg_destroy_ept(ept);
373 goto out;
374 }
375
376 /* need to tell remote processor's name service about this channel ? */
377 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500378 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200379 struct rpmsg_ns_msg nsm;
380
381 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700382 nsm.addr = rpdev->ept->addr;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200383 nsm.flags = RPMSG_NS_CREATE;
384
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700385 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200386 if (err)
387 dev_err(dev, "failed to announce service %d\n", err);
388 }
389
390out:
391 return err;
392}
393
394static int rpmsg_dev_remove(struct device *dev)
395{
396 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
397 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
398 struct virtproc_info *vrp = rpdev->vrp;
399 int err = 0;
400
401 /* tell remote processor's name service we're removing this channel */
402 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500403 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200404 struct rpmsg_ns_msg nsm;
405
406 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
407 nsm.addr = rpdev->src;
408 nsm.flags = RPMSG_NS_DESTROY;
409
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700410 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200411 if (err)
412 dev_err(dev, "failed to announce service %d\n", err);
413 }
414
415 rpdrv->remove(rpdev);
416
417 rpmsg_destroy_ept(rpdev->ept);
418
419 return err;
420}
421
422static struct bus_type rpmsg_bus = {
423 .name = "rpmsg",
424 .match = rpmsg_dev_match,
425 .dev_attrs = rpmsg_dev_attrs,
426 .uevent = rpmsg_uevent,
427 .probe = rpmsg_dev_probe,
428 .remove = rpmsg_dev_remove,
429};
430
431/**
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500432 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200433 * @rpdrv: pointer to a struct rpmsg_driver
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500434 * @owner: owning module/driver
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200435 *
436 * Returns 0 on success, and an appropriate error value on failure.
437 */
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500438int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200439{
440 rpdrv->drv.bus = &rpmsg_bus;
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500441 rpdrv->drv.owner = owner;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200442 return driver_register(&rpdrv->drv);
443}
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500444EXPORT_SYMBOL(__register_rpmsg_driver);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200445
446/**
447 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
448 * @rpdrv: pointer to a struct rpmsg_driver
449 *
450 * Returns 0 on success, and an appropriate error value on failure.
451 */
452void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
453{
454 driver_unregister(&rpdrv->drv);
455}
456EXPORT_SYMBOL(unregister_rpmsg_driver);
457
458static void rpmsg_release_device(struct device *dev)
459{
460 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
461
462 kfree(rpdev);
463}
464
465/*
466 * match an rpmsg channel with a channel info struct.
467 * this is used to make sure we're not creating rpmsg devices for channels
468 * that already exist.
469 */
470static int rpmsg_channel_match(struct device *dev, void *data)
471{
472 struct rpmsg_channel_info *chinfo = data;
473 struct rpmsg_channel *rpdev = to_rpmsg_channel(dev);
474
475 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
476 return 0;
477
478 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
479 return 0;
480
481 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
482 return 0;
483
484 /* found a match ! */
485 return 1;
486}
487
488/*
489 * create an rpmsg channel using its name and address info.
490 * this function will be used to create both static and dynamic
491 * channels.
492 */
493static struct rpmsg_channel *rpmsg_create_channel(struct virtproc_info *vrp,
494 struct rpmsg_channel_info *chinfo)
495{
496 struct rpmsg_channel *rpdev;
497 struct device *tmp, *dev = &vrp->vdev->dev;
498 int ret;
499
500 /* make sure a similar channel doesn't already exist */
501 tmp = device_find_child(dev, chinfo, rpmsg_channel_match);
502 if (tmp) {
503 /* decrement the matched device's refcount back */
504 put_device(tmp);
505 dev_err(dev, "channel %s:%x:%x already exist\n",
506 chinfo->name, chinfo->src, chinfo->dst);
507 return NULL;
508 }
509
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500510 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
511 if (!rpdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200512 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200513
514 rpdev->vrp = vrp;
515 rpdev->src = chinfo->src;
516 rpdev->dst = chinfo->dst;
517
518 /*
519 * rpmsg server channels has predefined local address (for now),
520 * and their existence needs to be announced remotely
521 */
Andrew F. Davisc8ced112016-07-01 09:24:58 -0500522 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200523
524 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
525
Bjorn Andersson4dffed52016-09-01 15:27:54 -0700526 dev_set_name(&rpdev->dev, "%s:%s",
527 dev_name(dev->parent), rpdev->id.name);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200528
529 rpdev->dev.parent = &vrp->vdev->dev;
530 rpdev->dev.bus = &rpmsg_bus;
531 rpdev->dev.release = rpmsg_release_device;
532
533 ret = device_register(&rpdev->dev);
534 if (ret) {
535 dev_err(dev, "device_register failed: %d\n", ret);
536 put_device(&rpdev->dev);
537 return NULL;
538 }
539
540 return rpdev;
541}
542
543/*
544 * find an existing channel using its name + address properties,
545 * and destroy it
546 */
547static int rpmsg_destroy_channel(struct virtproc_info *vrp,
Anna, Suman09636792016-08-12 18:42:26 -0500548 struct rpmsg_channel_info *chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200549{
550 struct virtio_device *vdev = vrp->vdev;
551 struct device *dev;
552
553 dev = device_find_child(&vdev->dev, chinfo, rpmsg_channel_match);
554 if (!dev)
555 return -EINVAL;
556
557 device_unregister(dev);
558
559 put_device(dev);
560
561 return 0;
562}
563
564/* super simple buffer "allocator" that is just enough for now */
565static void *get_a_tx_buf(struct virtproc_info *vrp)
566{
567 unsigned int len;
568 void *ret;
569
570 /* support multiple concurrent senders */
571 mutex_lock(&vrp->tx_lock);
572
573 /*
574 * either pick the next unused tx buffer
575 * (half of our buffers are used for sending messages)
576 */
Suman Annab1b98912014-09-16 13:33:07 -0500577 if (vrp->last_sbuf < vrp->num_bufs / 2)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200578 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
579 /* or recycle a used one */
580 else
581 ret = virtqueue_get_buf(vrp->svq, &len);
582
583 mutex_unlock(&vrp->tx_lock);
584
585 return ret;
586}
587
588/**
589 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
590 * @vrp: virtual remote processor state
591 *
592 * This function is called before a sender is blocked, waiting for
593 * a tx buffer to become available.
594 *
595 * If we already have blocking senders, this function merely increases
596 * the "sleepers" reference count, and exits.
597 *
598 * Otherwise, if this is the first sender to block, we also enable
599 * virtio's tx callbacks, so we'd be immediately notified when a tx
600 * buffer is consumed (we rely on virtio's tx callback in order
601 * to wake up sleeping senders as soon as a tx buffer is used by the
602 * remote processor).
603 */
604static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
605{
606 /* support multiple concurrent senders */
607 mutex_lock(&vrp->tx_lock);
608
609 /* are we the first sleeping context waiting for tx buffers ? */
610 if (atomic_inc_return(&vrp->sleepers) == 1)
611 /* enable "tx-complete" interrupts before dozing off */
612 virtqueue_enable_cb(vrp->svq);
613
614 mutex_unlock(&vrp->tx_lock);
615}
616
617/**
618 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
619 * @vrp: virtual remote processor state
620 *
621 * This function is called after a sender, that waited for a tx buffer
622 * to become available, is unblocked.
623 *
624 * If we still have blocking senders, this function merely decreases
625 * the "sleepers" reference count, and exits.
626 *
627 * Otherwise, if there are no more blocking senders, we also disable
628 * virtio's tx callbacks, to avoid the overhead incurred with handling
629 * those (now redundant) interrupts.
630 */
631static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
632{
633 /* support multiple concurrent senders */
634 mutex_lock(&vrp->tx_lock);
635
636 /* are we the last sleeping context waiting for tx buffers ? */
637 if (atomic_dec_and_test(&vrp->sleepers))
638 /* disable "tx-complete" interrupts */
639 virtqueue_disable_cb(vrp->svq);
640
641 mutex_unlock(&vrp->tx_lock);
642}
643
644/**
645 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
646 * @rpdev: the rpmsg channel
647 * @src: source address
648 * @dst: destination address
649 * @data: payload of message
650 * @len: length of payload
651 * @wait: indicates whether caller should block in case no TX buffers available
652 *
653 * This function is the base implementation for all of the rpmsg sending API.
654 *
655 * It will send @data of length @len to @dst, and say it's from @src. The
656 * message will be sent to the remote processor which the @rpdev channel
657 * belongs to.
658 *
659 * The message is sent using one of the TX buffers that are available for
660 * communication with this remote processor.
661 *
662 * If @wait is true, the caller will be blocked until either a TX buffer is
663 * available, or 15 seconds elapses (we don't want callers to
664 * sleep indefinitely due to misbehaving remote processors), and in that
665 * case -ERESTARTSYS is returned. The number '15' itself was picked
666 * arbitrarily; there's little point in asking drivers to provide a timeout
667 * value themselves.
668 *
669 * Otherwise, if @wait is false, and there are no TX buffers available,
670 * the function will immediately fail, and -ENOMEM will be returned.
671 *
672 * Normally drivers shouldn't use this function directly; instead, drivers
673 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
674 * (see include/linux/rpmsg.h).
675 *
676 * Returns 0 on success and an appropriate error value on failure.
677 */
678int rpmsg_send_offchannel_raw(struct rpmsg_channel *rpdev, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500679 void *data, int len, bool wait)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200680{
681 struct virtproc_info *vrp = rpdev->vrp;
682 struct device *dev = &rpdev->dev;
683 struct scatterlist sg;
684 struct rpmsg_hdr *msg;
685 int err;
686
687 /* bcasting isn't allowed */
688 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
689 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
690 return -EINVAL;
691 }
692
693 /*
694 * We currently use fixed-sized buffers, and therefore the payload
695 * length is limited.
696 *
697 * One of the possible improvements here is either to support
698 * user-provided buffers (and then we can also support zero-copy
699 * messaging), or to improve the buffer allocator, to support
700 * variable-length buffer sizes.
701 */
702 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
703 dev_err(dev, "message is too big (%d)\n", len);
704 return -EMSGSIZE;
705 }
706
707 /* grab a buffer */
708 msg = get_a_tx_buf(vrp);
709 if (!msg && !wait)
710 return -ENOMEM;
711
712 /* no free buffer ? wait for one (but bail after 15 seconds) */
713 while (!msg) {
714 /* enable "tx-complete" interrupts, if not already enabled */
715 rpmsg_upref_sleepers(vrp);
716
717 /*
718 * sleep until a free buffer is available or 15 secs elapse.
719 * the timeout period is not configurable because there's
720 * little point in asking drivers to specify that.
721 * if later this happens to be required, it'd be easy to add.
722 */
723 err = wait_event_interruptible_timeout(vrp->sendq,
724 (msg = get_a_tx_buf(vrp)),
725 msecs_to_jiffies(15000));
726
727 /* disable "tx-complete" interrupts if we're the last sleeper */
728 rpmsg_downref_sleepers(vrp);
729
730 /* timeout ? */
731 if (!err) {
732 dev_err(dev, "timeout waiting for a tx buffer\n");
733 return -ERESTARTSYS;
734 }
735 }
736
737 msg->len = len;
738 msg->flags = 0;
739 msg->src = src;
740 msg->dst = dst;
741 msg->reserved = 0;
742 memcpy(msg->data, data, len);
743
744 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500745 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500746#if defined(CONFIG_DYNAMIC_DEBUG)
747 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
748 msg, sizeof(*msg) + msg->len, true);
749#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200750
751 sg_init_one(&sg, msg, sizeof(*msg) + len);
752
753 mutex_lock(&vrp->tx_lock);
754
755 /* add message to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030756 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030757 if (err) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200758 /*
759 * need to reclaim the buffer here, otherwise it's lost
760 * (memory won't leak, but rpmsg won't use it again for TX).
761 * this will wait for a buffer management overhaul.
762 */
Rusty Russellcee51d62013-03-20 15:44:29 +1030763 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200764 goto out;
765 }
766
767 /* tell the remote processor it has a pending message to read */
768 virtqueue_kick(vrp->svq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200769out:
770 mutex_unlock(&vrp->tx_lock);
771 return err;
772}
773EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
774
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300775static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
776 struct rpmsg_hdr *msg, unsigned int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200777{
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200778 struct rpmsg_endpoint *ept;
779 struct scatterlist sg;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200780 int err;
781
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200782 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500783 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500784#if defined(CONFIG_DYNAMIC_DEBUG)
785 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
786 msg, sizeof(*msg) + msg->len, true);
787#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200788
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200789 /*
790 * We currently use fixed-sized buffers, so trivially sanitize
791 * the reported payload length.
792 */
793 if (len > RPMSG_BUF_SIZE ||
Anna, Suman09636792016-08-12 18:42:26 -0500794 msg->len > (len - sizeof(struct rpmsg_hdr))) {
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200795 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300796 return -EINVAL;
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200797 }
798
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200799 /* use the dst addr to fetch the callback of the appropriate user */
800 mutex_lock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300801
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200802 ept = idr_find(&vrp->endpoints, msg->dst);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300803
804 /* let's make sure no one deallocates ept while we use it */
805 if (ept)
806 kref_get(&ept->refcount);
807
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200808 mutex_unlock(&vrp->endpoints_lock);
809
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300810 if (ept) {
811 /* make sure ept->cb doesn't go away while we use it */
812 mutex_lock(&ept->cb_lock);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200813
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300814 if (ept->cb)
815 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
816 msg->src);
817
818 mutex_unlock(&ept->cb_lock);
819
820 /* farewell, ept, we don't need you anymore */
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300821 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300822 } else
Masanari Iida8a168ca2012-12-29 02:00:09 +0900823 dev_warn(dev, "msg received with no recipient\n");
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300824
Ohad Ben-Cohenf1d9e9c2012-02-28 16:11:28 +0200825 /* publish the real size of the buffer */
826 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200827
828 /* add the buffer back to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030829 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200830 if (err < 0) {
831 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300832 return err;
833 }
834
835 return 0;
836}
837
838/* called when an rx buffer is used, and it's time to digest a message */
839static void rpmsg_recv_done(struct virtqueue *rvq)
840{
841 struct virtproc_info *vrp = rvq->vdev->priv;
842 struct device *dev = &rvq->vdev->dev;
843 struct rpmsg_hdr *msg;
844 unsigned int len, msgs_received = 0;
845 int err;
846
847 msg = virtqueue_get_buf(rvq, &len);
848 if (!msg) {
849 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200850 return;
851 }
852
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300853 while (msg) {
854 err = rpmsg_recv_single(vrp, dev, msg, len);
855 if (err)
856 break;
857
858 msgs_received++;
859
860 msg = virtqueue_get_buf(rvq, &len);
Lee Jones6c49fbe2016-07-20 10:29:35 +0100861 }
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300862
863 dev_dbg(dev, "Received %u messages\n", msgs_received);
864
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200865 /* tell the remote processor we added another available rx buffer */
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300866 if (msgs_received)
867 virtqueue_kick(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200868}
869
870/*
871 * This is invoked whenever the remote processor completed processing
872 * a TX msg we just sent it, and the buffer is put back to the used ring.
873 *
874 * Normally, though, we suppress this "tx complete" interrupt in order to
875 * avoid the incurred overhead.
876 */
877static void rpmsg_xmit_done(struct virtqueue *svq)
878{
879 struct virtproc_info *vrp = svq->vdev->priv;
880
881 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
882
883 /* wake up potential senders that are waiting for a tx buffer */
884 wake_up_interruptible(&vrp->sendq);
885}
886
887/* invoked when a name service announcement arrives */
888static void rpmsg_ns_cb(struct rpmsg_channel *rpdev, void *data, int len,
Anna, Suman09636792016-08-12 18:42:26 -0500889 void *priv, u32 src)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200890{
891 struct rpmsg_ns_msg *msg = data;
892 struct rpmsg_channel *newch;
893 struct rpmsg_channel_info chinfo;
894 struct virtproc_info *vrp = priv;
895 struct device *dev = &vrp->vdev->dev;
896 int ret;
897
Anna, Suman211e3a92016-08-12 18:42:27 -0500898#if defined(CONFIG_DYNAMIC_DEBUG)
899 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
900 data, len, true);
901#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200902
903 if (len != sizeof(*msg)) {
904 dev_err(dev, "malformed ns msg (%d)\n", len);
905 return;
906 }
907
908 /*
909 * the name service ept does _not_ belong to a real rpmsg channel,
910 * and is handled by the rpmsg bus itself.
911 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
912 * in somehow.
913 */
914 if (rpdev) {
915 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
916 return;
917 }
918
919 /* don't trust the remote processor for null terminating the name */
920 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
921
922 dev_info(dev, "%sing channel %s addr 0x%x\n",
Anna, Suman09636792016-08-12 18:42:26 -0500923 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
924 msg->name, msg->addr);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200925
926 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
927 chinfo.src = RPMSG_ADDR_ANY;
928 chinfo.dst = msg->addr;
929
930 if (msg->flags & RPMSG_NS_DESTROY) {
931 ret = rpmsg_destroy_channel(vrp, &chinfo);
932 if (ret)
933 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
934 } else {
935 newch = rpmsg_create_channel(vrp, &chinfo);
936 if (!newch)
937 dev_err(dev, "rpmsg_create_channel failed\n");
938 }
939}
940
941static int rpmsg_probe(struct virtio_device *vdev)
942{
943 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800944 static const char * const names[] = { "input", "output" };
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200945 struct virtqueue *vqs[2];
946 struct virtproc_info *vrp;
947 void *bufs_va;
948 int err = 0, i;
Suman Annab1b98912014-09-16 13:33:07 -0500949 size_t total_buf_space;
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030950 bool notify;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200951
952 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
953 if (!vrp)
954 return -ENOMEM;
955
956 vrp->vdev = vdev;
957
958 idr_init(&vrp->endpoints);
959 mutex_init(&vrp->endpoints_lock);
960 mutex_init(&vrp->tx_lock);
961 init_waitqueue_head(&vrp->sendq);
962
963 /* We expect two virtqueues, rx and tx (and in this order) */
964 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
965 if (err)
966 goto free_vrp;
967
968 vrp->rvq = vqs[0];
969 vrp->svq = vqs[1];
970
Suman Annab1b98912014-09-16 13:33:07 -0500971 /* we expect symmetric tx/rx vrings */
972 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
973 virtqueue_get_vring_size(vrp->svq));
974
975 /* we need less buffers if vrings are small */
976 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
977 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
978 else
979 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
980
981 total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
982
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200983 /* allocate coherent memory for the buffers */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300984 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
Suman Annab1b98912014-09-16 13:33:07 -0500985 total_buf_space, &vrp->bufs_dma,
986 GFP_KERNEL);
Wei Yongjun3119b482013-04-29 16:17:09 -0700987 if (!bufs_va) {
988 err = -ENOMEM;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200989 goto vqs_del;
Wei Yongjun3119b482013-04-29 16:17:09 -0700990 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200991
Anna, Suman8d95b322016-08-12 18:42:25 -0500992 dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
993 bufs_va, &vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200994
995 /* half of the buffers is dedicated for RX */
996 vrp->rbufs = bufs_va;
997
998 /* and half is dedicated for TX */
Suman Annab1b98912014-09-16 13:33:07 -0500999 vrp->sbufs = bufs_va + total_buf_space / 2;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001000
1001 /* set up the receive buffers */
Suman Annab1b98912014-09-16 13:33:07 -05001002 for (i = 0; i < vrp->num_bufs / 2; i++) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001003 struct scatterlist sg;
1004 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
1005
1006 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
1007
Rusty Russellcee51d62013-03-20 15:44:29 +10301008 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
Anna, Suman09636792016-08-12 18:42:26 -05001009 GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +10301010 WARN_ON(err); /* sanity check; this can't really happen */
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001011 }
1012
1013 /* suppress "tx-complete" interrupts */
1014 virtqueue_disable_cb(vrp->svq);
1015
1016 vdev->priv = vrp;
1017
1018 /* if supported by the remote processor, enable the name service */
1019 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
1020 /* a dedicated endpoint handles the name service msgs */
1021 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
1022 vrp, RPMSG_NS_ADDR);
1023 if (!vrp->ns_ept) {
1024 dev_err(&vdev->dev, "failed to create the ns ept\n");
1025 err = -ENOMEM;
1026 goto free_coherent;
1027 }
1028 }
1029
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301030 /*
1031 * Prepare to kick but don't notify yet - we can't do this before
1032 * device is ready.
1033 */
1034 notify = virtqueue_kick_prepare(vrp->rvq);
1035
1036 /* From this point on, we can notify and get callbacks. */
1037 virtio_device_ready(vdev);
1038
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001039 /* tell the remote processor it can start sending messages */
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301040 /*
1041 * this might be concurrent with callbacks, but we are only
1042 * doing notify, not a full kick here, so that's ok.
1043 */
1044 if (notify)
1045 virtqueue_notify(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001046
1047 dev_info(&vdev->dev, "rpmsg host is online\n");
1048
1049 return 0;
1050
1051free_coherent:
Suman Annab1b98912014-09-16 13:33:07 -05001052 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1053 bufs_va, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001054vqs_del:
1055 vdev->config->del_vqs(vrp->vdev);
1056free_vrp:
1057 kfree(vrp);
1058 return err;
1059}
1060
1061static int rpmsg_remove_device(struct device *dev, void *data)
1062{
1063 device_unregister(dev);
1064
1065 return 0;
1066}
1067
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001068static void rpmsg_remove(struct virtio_device *vdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001069{
1070 struct virtproc_info *vrp = vdev->priv;
Suman Annab1b98912014-09-16 13:33:07 -05001071 size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001072 int ret;
1073
1074 vdev->config->reset(vdev);
1075
1076 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1077 if (ret)
1078 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1079
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +02001080 if (vrp->ns_ept)
1081 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1082
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001083 idr_destroy(&vrp->endpoints);
1084
1085 vdev->config->del_vqs(vrp->vdev);
1086
Suman Annab1b98912014-09-16 13:33:07 -05001087 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1088 vrp->rbufs, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001089
1090 kfree(vrp);
1091}
1092
1093static struct virtio_device_id id_table[] = {
1094 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1095 { 0 },
1096};
1097
1098static unsigned int features[] = {
1099 VIRTIO_RPMSG_F_NS,
1100};
1101
1102static struct virtio_driver virtio_ipc_driver = {
1103 .feature_table = features,
1104 .feature_table_size = ARRAY_SIZE(features),
1105 .driver.name = KBUILD_MODNAME,
1106 .driver.owner = THIS_MODULE,
1107 .id_table = id_table,
1108 .probe = rpmsg_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001109 .remove = rpmsg_remove,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001110};
1111
1112static int __init rpmsg_init(void)
1113{
1114 int ret;
1115
1116 ret = bus_register(&rpmsg_bus);
1117 if (ret) {
1118 pr_err("failed to register rpmsg bus: %d\n", ret);
1119 return ret;
1120 }
1121
1122 ret = register_virtio_driver(&virtio_ipc_driver);
1123 if (ret) {
1124 pr_err("failed to register virtio driver: %d\n", ret);
1125 bus_unregister(&rpmsg_bus);
1126 }
1127
1128 return ret;
1129}
Federico Fuga96342522012-07-16 10:36:51 +03001130subsys_initcall(rpmsg_init);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001131
1132static void __exit rpmsg_fini(void)
1133{
1134 unregister_virtio_driver(&virtio_ipc_driver);
1135 bus_unregister(&rpmsg_bus);
1136}
1137module_exit(rpmsg_fini);
1138
1139MODULE_DEVICE_TABLE(virtio, id_table);
1140MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1141MODULE_LICENSE("GPL v2");