blob: 1ac9fd871760ecaff1f9e6ad2fddc4cd8435ae8f [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
Bjorn Andersson92e1de52016-09-01 15:27:57 -070076#define to_rpmsg_device(d) container_of(d, struct rpmsg_device, dev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020077#define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
78
79/*
Suman Annab1b98912014-09-16 13:33:07 -050080 * We're allocating buffers of 512 bytes each for communications. The
81 * number of buffers will be computed from the number of buffers supported
82 * by the vring, upto a maximum of 512 buffers (256 in each direction).
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020083 *
84 * Each buffer will have 16 bytes for the msg header and 496 bytes for
85 * the payload.
86 *
Suman Annab1b98912014-09-16 13:33:07 -050087 * This will utilize a maximum total space of 256KB for the buffers.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020088 *
89 * We might also want to add support for user-provided buffers in time.
90 * This will allow bigger buffer size flexibility, and can also be used
91 * to achieve zero-copy messaging.
92 *
93 * Note that these numbers are purely a decision of this driver - we
94 * can change this without changing anything in the firmware of the remote
95 * processor.
96 */
Suman Annab1b98912014-09-16 13:33:07 -050097#define MAX_RPMSG_NUM_BUFS (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020098#define RPMSG_BUF_SIZE (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020099
100/*
101 * Local addresses are dynamically allocated on-demand.
102 * We do not dynamically assign addresses from the low 1024 range,
103 * in order to reserve that address range for predefined services.
104 */
105#define RPMSG_RESERVED_ADDRESSES (1024)
106
107/* Address 53 is reserved for advertising remote services */
108#define RPMSG_NS_ADDR (53)
109
110/* sysfs show configuration fields */
111#define rpmsg_show_attr(field, path, format_string) \
112static ssize_t \
113field##_show(struct device *dev, \
114 struct device_attribute *attr, char *buf) \
115{ \
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700116 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200117 \
118 return sprintf(buf, format_string, rpdev->path); \
119}
120
121/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
122rpmsg_show_attr(name, id.name, "%s\n");
123rpmsg_show_attr(src, src, "0x%x\n");
124rpmsg_show_attr(dst, dst, "0x%x\n");
125rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
126
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200127static ssize_t modalias_show(struct device *dev,
128 struct device_attribute *attr, char *buf)
129{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700130 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200131
132 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
133}
134
135static struct device_attribute rpmsg_dev_attrs[] = {
136 __ATTR_RO(name),
137 __ATTR_RO(modalias),
138 __ATTR_RO(dst),
139 __ATTR_RO(src),
140 __ATTR_RO(announce),
141 __ATTR_NULL
142};
143
144/* rpmsg devices and drivers are matched using the service name */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700145static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500146 const struct rpmsg_device_id *id)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200147{
148 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
149}
150
151/* match rpmsg channel and rpmsg driver */
152static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
153{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700154 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200155 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
156 const struct rpmsg_device_id *ids = rpdrv->id_table;
157 unsigned int i;
158
Bjorn Anderssona16644c2016-09-01 15:27:53 -0700159 if (ids)
160 for (i = 0; ids[i].name[0]; i++)
161 if (rpmsg_id_match(rpdev, &ids[i]))
162 return 1;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200163
Bjorn Anderssona16644c2016-09-01 15:27:53 -0700164 return of_driver_match_device(dev, drv);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200165}
166
167static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
168{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700169 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200170
171 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
172 rpdev->id.name);
173}
174
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300175/**
176 * __ept_release() - deallocate an rpmsg endpoint
177 * @kref: the ept's reference count
178 *
179 * This function deallocates an ept, and is invoked when its @kref refcount
180 * drops to zero.
181 *
182 * Never invoke this function directly!
183 */
184static void __ept_release(struct kref *kref)
185{
186 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
187 refcount);
188 /*
189 * At this point no one holds a reference to ept anymore,
190 * so we can directly free it
191 */
192 kfree(ept);
193}
194
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200195/* for more info, see below documentation of rpmsg_create_ept() */
196static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700197 struct rpmsg_device *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500198 rpmsg_rx_cb_t cb,
199 void *priv, u32 addr)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200200{
Tejun Heod0ffce72013-02-27 17:04:40 -0800201 int id_min, id_max, id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200202 struct rpmsg_endpoint *ept;
203 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
204
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200205 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500206 if (!ept)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200207 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200208
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300209 kref_init(&ept->refcount);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300210 mutex_init(&ept->cb_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300211
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200212 ept->rpdev = rpdev;
213 ept->cb = cb;
214 ept->priv = priv;
215
216 /* do we need to allocate a local address ? */
Tejun Heod0ffce72013-02-27 17:04:40 -0800217 if (addr == RPMSG_ADDR_ANY) {
218 id_min = RPMSG_RESERVED_ADDRESSES;
219 id_max = 0;
220 } else {
221 id_min = addr;
222 id_max = addr + 1;
223 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200224
225 mutex_lock(&vrp->endpoints_lock);
226
227 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
Tejun Heod0ffce72013-02-27 17:04:40 -0800228 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
229 if (id < 0) {
230 dev_err(dev, "idr_alloc failed: %d\n", id);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200231 goto free_ept;
232 }
Tejun Heod0ffce72013-02-27 17:04:40 -0800233 ept->addr = id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200234
235 mutex_unlock(&vrp->endpoints_lock);
236
237 return ept;
238
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200239free_ept:
240 mutex_unlock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300241 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200242 return NULL;
243}
244
245/**
246 * rpmsg_create_ept() - create a new rpmsg_endpoint
247 * @rpdev: rpmsg channel device
248 * @cb: rx callback handler
249 * @priv: private data for the driver's use
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700250 * @chinfo: channel_info with the local rpmsg address to bind with @cb
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200251 *
252 * Every rpmsg address in the system is bound to an rx callback (so when
253 * inbound messages arrive, they are dispatched by the rpmsg bus using the
254 * appropriate callback handler) by means of an rpmsg_endpoint struct.
255 *
256 * This function allows drivers to create such an endpoint, and by that,
257 * bind a callback, and possibly some private data too, to an rpmsg address
258 * (either one that is known in advance, or one that will be dynamically
259 * assigned for them).
260 *
261 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
262 * is already created for them when they are probed by the rpmsg bus
263 * (using the rx callback provided when they registered to the rpmsg bus).
264 *
265 * So things should just work for simple drivers: they already have an
266 * endpoint, their rx callback is bound to their rpmsg address, and when
267 * relevant inbound messages arrive (i.e. messages which their dst address
268 * equals to the src address of their rpmsg channel), the driver's handler
269 * is invoked to process it.
270 *
271 * That said, more complicated drivers might do need to allocate
272 * additional rpmsg addresses, and bind them to different rx callbacks.
273 * To accomplish that, those drivers need to call this function.
274 *
275 * Drivers should provide their @rpdev channel (so the new endpoint would belong
276 * to the same remote processor their channel belongs to), an rx callback
277 * function, an optional private data (which is provided back when the
278 * rx callback is invoked), and an address they want to bind with the
279 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
280 * dynamically assign them an available rpmsg address (drivers should have
281 * a very good reason why not to always use RPMSG_ADDR_ANY here).
282 *
283 * Returns a pointer to the endpoint on success, or NULL on error.
284 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700285struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev,
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700286 rpmsg_rx_cb_t cb, void *priv,
287 struct rpmsg_channel_info chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200288{
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700289 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, chinfo.src);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200290}
291EXPORT_SYMBOL(rpmsg_create_ept);
292
293/**
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200294 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
295 * @vrp: virtproc which owns this ept
296 * @ept: endpoing to destroy
297 *
298 * An internal function which destroy an ept without assuming it is
299 * bound to an rpmsg channel. This is needed for handling the internal
300 * name service endpoint, which isn't bound to an rpmsg channel.
301 * See also __rpmsg_create_ept().
302 */
303static void
304__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
305{
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300306 /* make sure new inbound messages can't find this ept anymore */
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200307 mutex_lock(&vrp->endpoints_lock);
308 idr_remove(&vrp->endpoints, ept->addr);
309 mutex_unlock(&vrp->endpoints_lock);
310
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300311 /* make sure in-flight inbound messages won't invoke cb anymore */
312 mutex_lock(&ept->cb_lock);
313 ept->cb = NULL;
314 mutex_unlock(&ept->cb_lock);
315
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300316 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200317}
318
319/**
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200320 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
321 * @ept: endpoing to destroy
322 *
323 * Should be used by drivers to destroy an rpmsg endpoint previously
324 * created with rpmsg_create_ept().
325 */
326void rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
327{
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200328 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200329}
330EXPORT_SYMBOL(rpmsg_destroy_ept);
331
332/*
333 * when an rpmsg driver is probed with a channel, we seamlessly create
334 * it an endpoint, binding its rx callback to a unique local rpmsg
335 * address.
336 *
337 * if we need to, we also announce about this channel to the remote
338 * processor (needed in case the driver is exposing an rpmsg service).
339 */
340static int rpmsg_dev_probe(struct device *dev)
341{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700342 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200343 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
344 struct virtproc_info *vrp = rpdev->vrp;
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700345 struct rpmsg_channel_info chinfo = {};
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200346 struct rpmsg_endpoint *ept;
347 int err;
348
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700349 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
350 chinfo.src = rpdev->src;
351 chinfo.dst = RPMSG_ADDR_ANY;
352
353 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200354 if (!ept) {
355 dev_err(dev, "failed to create endpoint\n");
356 err = -ENOMEM;
357 goto out;
358 }
359
360 rpdev->ept = ept;
361 rpdev->src = ept->addr;
362
363 err = rpdrv->probe(rpdev);
364 if (err) {
365 dev_err(dev, "%s: failed: %d\n", __func__, err);
366 rpmsg_destroy_ept(ept);
367 goto out;
368 }
369
370 /* need to tell remote processor's name service about this channel ? */
371 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500372 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200373 struct rpmsg_ns_msg nsm;
374
375 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700376 nsm.addr = rpdev->ept->addr;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200377 nsm.flags = RPMSG_NS_CREATE;
378
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700379 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200380 if (err)
381 dev_err(dev, "failed to announce service %d\n", err);
382 }
383
384out:
385 return err;
386}
387
388static int rpmsg_dev_remove(struct device *dev)
389{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700390 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200391 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
392 struct virtproc_info *vrp = rpdev->vrp;
393 int err = 0;
394
395 /* tell remote processor's name service we're removing this channel */
396 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500397 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200398 struct rpmsg_ns_msg nsm;
399
400 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
401 nsm.addr = rpdev->src;
402 nsm.flags = RPMSG_NS_DESTROY;
403
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700404 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200405 if (err)
406 dev_err(dev, "failed to announce service %d\n", err);
407 }
408
409 rpdrv->remove(rpdev);
410
411 rpmsg_destroy_ept(rpdev->ept);
412
413 return err;
414}
415
416static struct bus_type rpmsg_bus = {
417 .name = "rpmsg",
418 .match = rpmsg_dev_match,
419 .dev_attrs = rpmsg_dev_attrs,
420 .uevent = rpmsg_uevent,
421 .probe = rpmsg_dev_probe,
422 .remove = rpmsg_dev_remove,
423};
424
425/**
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500426 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200427 * @rpdrv: pointer to a struct rpmsg_driver
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500428 * @owner: owning module/driver
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200429 *
430 * Returns 0 on success, and an appropriate error value on failure.
431 */
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500432int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200433{
434 rpdrv->drv.bus = &rpmsg_bus;
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500435 rpdrv->drv.owner = owner;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200436 return driver_register(&rpdrv->drv);
437}
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500438EXPORT_SYMBOL(__register_rpmsg_driver);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200439
440/**
441 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
442 * @rpdrv: pointer to a struct rpmsg_driver
443 *
444 * Returns 0 on success, and an appropriate error value on failure.
445 */
446void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
447{
448 driver_unregister(&rpdrv->drv);
449}
450EXPORT_SYMBOL(unregister_rpmsg_driver);
451
452static void rpmsg_release_device(struct device *dev)
453{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700454 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200455
456 kfree(rpdev);
457}
458
459/*
460 * match an rpmsg channel with a channel info struct.
461 * this is used to make sure we're not creating rpmsg devices for channels
462 * that already exist.
463 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700464static int rpmsg_device_match(struct device *dev, void *data)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200465{
466 struct rpmsg_channel_info *chinfo = data;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700467 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200468
469 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src)
470 return 0;
471
472 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst)
473 return 0;
474
475 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE))
476 return 0;
477
478 /* found a match ! */
479 return 1;
480}
481
482/*
483 * create an rpmsg channel using its name and address info.
484 * this function will be used to create both static and dynamic
485 * channels.
486 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700487static struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp,
488 struct rpmsg_channel_info *chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200489{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700490 struct rpmsg_device *rpdev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200491 struct device *tmp, *dev = &vrp->vdev->dev;
492 int ret;
493
494 /* make sure a similar channel doesn't already exist */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700495 tmp = device_find_child(dev, chinfo, rpmsg_device_match);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200496 if (tmp) {
497 /* decrement the matched device's refcount back */
498 put_device(tmp);
499 dev_err(dev, "channel %s:%x:%x already exist\n",
500 chinfo->name, chinfo->src, chinfo->dst);
501 return NULL;
502 }
503
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500504 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
505 if (!rpdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200506 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200507
508 rpdev->vrp = vrp;
509 rpdev->src = chinfo->src;
510 rpdev->dst = chinfo->dst;
511
512 /*
513 * rpmsg server channels has predefined local address (for now),
514 * and their existence needs to be announced remotely
515 */
Andrew F. Davisc8ced112016-07-01 09:24:58 -0500516 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200517
518 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
519
Bjorn Andersson4dffed52016-09-01 15:27:54 -0700520 dev_set_name(&rpdev->dev, "%s:%s",
521 dev_name(dev->parent), rpdev->id.name);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200522
523 rpdev->dev.parent = &vrp->vdev->dev;
524 rpdev->dev.bus = &rpmsg_bus;
525 rpdev->dev.release = rpmsg_release_device;
526
527 ret = device_register(&rpdev->dev);
528 if (ret) {
529 dev_err(dev, "device_register failed: %d\n", ret);
530 put_device(&rpdev->dev);
531 return NULL;
532 }
533
534 return rpdev;
535}
536
537/*
538 * find an existing channel using its name + address properties,
539 * and destroy it
540 */
541static int rpmsg_destroy_channel(struct virtproc_info *vrp,
Anna, Suman09636792016-08-12 18:42:26 -0500542 struct rpmsg_channel_info *chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200543{
544 struct virtio_device *vdev = vrp->vdev;
545 struct device *dev;
546
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700547 dev = device_find_child(&vdev->dev, chinfo, rpmsg_device_match);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200548 if (!dev)
549 return -EINVAL;
550
551 device_unregister(dev);
552
553 put_device(dev);
554
555 return 0;
556}
557
558/* super simple buffer "allocator" that is just enough for now */
559static void *get_a_tx_buf(struct virtproc_info *vrp)
560{
561 unsigned int len;
562 void *ret;
563
564 /* support multiple concurrent senders */
565 mutex_lock(&vrp->tx_lock);
566
567 /*
568 * either pick the next unused tx buffer
569 * (half of our buffers are used for sending messages)
570 */
Suman Annab1b98912014-09-16 13:33:07 -0500571 if (vrp->last_sbuf < vrp->num_bufs / 2)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200572 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
573 /* or recycle a used one */
574 else
575 ret = virtqueue_get_buf(vrp->svq, &len);
576
577 mutex_unlock(&vrp->tx_lock);
578
579 return ret;
580}
581
582/**
583 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
584 * @vrp: virtual remote processor state
585 *
586 * This function is called before a sender is blocked, waiting for
587 * a tx buffer to become available.
588 *
589 * If we already have blocking senders, this function merely increases
590 * the "sleepers" reference count, and exits.
591 *
592 * Otherwise, if this is the first sender to block, we also enable
593 * virtio's tx callbacks, so we'd be immediately notified when a tx
594 * buffer is consumed (we rely on virtio's tx callback in order
595 * to wake up sleeping senders as soon as a tx buffer is used by the
596 * remote processor).
597 */
598static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
599{
600 /* support multiple concurrent senders */
601 mutex_lock(&vrp->tx_lock);
602
603 /* are we the first sleeping context waiting for tx buffers ? */
604 if (atomic_inc_return(&vrp->sleepers) == 1)
605 /* enable "tx-complete" interrupts before dozing off */
606 virtqueue_enable_cb(vrp->svq);
607
608 mutex_unlock(&vrp->tx_lock);
609}
610
611/**
612 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
613 * @vrp: virtual remote processor state
614 *
615 * This function is called after a sender, that waited for a tx buffer
616 * to become available, is unblocked.
617 *
618 * If we still have blocking senders, this function merely decreases
619 * the "sleepers" reference count, and exits.
620 *
621 * Otherwise, if there are no more blocking senders, we also disable
622 * virtio's tx callbacks, to avoid the overhead incurred with handling
623 * those (now redundant) interrupts.
624 */
625static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
626{
627 /* support multiple concurrent senders */
628 mutex_lock(&vrp->tx_lock);
629
630 /* are we the last sleeping context waiting for tx buffers ? */
631 if (atomic_dec_and_test(&vrp->sleepers))
632 /* disable "tx-complete" interrupts */
633 virtqueue_disable_cb(vrp->svq);
634
635 mutex_unlock(&vrp->tx_lock);
636}
637
638/**
639 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
640 * @rpdev: the rpmsg channel
641 * @src: source address
642 * @dst: destination address
643 * @data: payload of message
644 * @len: length of payload
645 * @wait: indicates whether caller should block in case no TX buffers available
646 *
647 * This function is the base implementation for all of the rpmsg sending API.
648 *
649 * It will send @data of length @len to @dst, and say it's from @src. The
650 * message will be sent to the remote processor which the @rpdev channel
651 * belongs to.
652 *
653 * The message is sent using one of the TX buffers that are available for
654 * communication with this remote processor.
655 *
656 * If @wait is true, the caller will be blocked until either a TX buffer is
657 * available, or 15 seconds elapses (we don't want callers to
658 * sleep indefinitely due to misbehaving remote processors), and in that
659 * case -ERESTARTSYS is returned. The number '15' itself was picked
660 * arbitrarily; there's little point in asking drivers to provide a timeout
661 * value themselves.
662 *
663 * Otherwise, if @wait is false, and there are no TX buffers available,
664 * the function will immediately fail, and -ENOMEM will be returned.
665 *
666 * Normally drivers shouldn't use this function directly; instead, drivers
667 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
668 * (see include/linux/rpmsg.h).
669 *
670 * Returns 0 on success and an appropriate error value on failure.
671 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700672int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev, u32 src, u32 dst,
Anna, Suman09636792016-08-12 18:42:26 -0500673 void *data, int len, bool wait)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200674{
675 struct virtproc_info *vrp = rpdev->vrp;
676 struct device *dev = &rpdev->dev;
677 struct scatterlist sg;
678 struct rpmsg_hdr *msg;
679 int err;
680
681 /* bcasting isn't allowed */
682 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
683 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
684 return -EINVAL;
685 }
686
687 /*
688 * We currently use fixed-sized buffers, and therefore the payload
689 * length is limited.
690 *
691 * One of the possible improvements here is either to support
692 * user-provided buffers (and then we can also support zero-copy
693 * messaging), or to improve the buffer allocator, to support
694 * variable-length buffer sizes.
695 */
696 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
697 dev_err(dev, "message is too big (%d)\n", len);
698 return -EMSGSIZE;
699 }
700
701 /* grab a buffer */
702 msg = get_a_tx_buf(vrp);
703 if (!msg && !wait)
704 return -ENOMEM;
705
706 /* no free buffer ? wait for one (but bail after 15 seconds) */
707 while (!msg) {
708 /* enable "tx-complete" interrupts, if not already enabled */
709 rpmsg_upref_sleepers(vrp);
710
711 /*
712 * sleep until a free buffer is available or 15 secs elapse.
713 * the timeout period is not configurable because there's
714 * little point in asking drivers to specify that.
715 * if later this happens to be required, it'd be easy to add.
716 */
717 err = wait_event_interruptible_timeout(vrp->sendq,
718 (msg = get_a_tx_buf(vrp)),
719 msecs_to_jiffies(15000));
720
721 /* disable "tx-complete" interrupts if we're the last sleeper */
722 rpmsg_downref_sleepers(vrp);
723
724 /* timeout ? */
725 if (!err) {
726 dev_err(dev, "timeout waiting for a tx buffer\n");
727 return -ERESTARTSYS;
728 }
729 }
730
731 msg->len = len;
732 msg->flags = 0;
733 msg->src = src;
734 msg->dst = dst;
735 msg->reserved = 0;
736 memcpy(msg->data, data, len);
737
738 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500739 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500740#if defined(CONFIG_DYNAMIC_DEBUG)
741 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
742 msg, sizeof(*msg) + msg->len, true);
743#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200744
745 sg_init_one(&sg, msg, sizeof(*msg) + len);
746
747 mutex_lock(&vrp->tx_lock);
748
749 /* add message to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030750 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030751 if (err) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200752 /*
753 * need to reclaim the buffer here, otherwise it's lost
754 * (memory won't leak, but rpmsg won't use it again for TX).
755 * this will wait for a buffer management overhaul.
756 */
Rusty Russellcee51d62013-03-20 15:44:29 +1030757 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200758 goto out;
759 }
760
761 /* tell the remote processor it has a pending message to read */
762 virtqueue_kick(vrp->svq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200763out:
764 mutex_unlock(&vrp->tx_lock);
765 return err;
766}
767EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
768
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300769static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
770 struct rpmsg_hdr *msg, unsigned int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200771{
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200772 struct rpmsg_endpoint *ept;
773 struct scatterlist sg;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200774 int err;
775
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200776 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500777 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500778#if defined(CONFIG_DYNAMIC_DEBUG)
779 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
780 msg, sizeof(*msg) + msg->len, true);
781#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200782
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200783 /*
784 * We currently use fixed-sized buffers, so trivially sanitize
785 * the reported payload length.
786 */
787 if (len > RPMSG_BUF_SIZE ||
Anna, Suman09636792016-08-12 18:42:26 -0500788 msg->len > (len - sizeof(struct rpmsg_hdr))) {
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200789 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300790 return -EINVAL;
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200791 }
792
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200793 /* use the dst addr to fetch the callback of the appropriate user */
794 mutex_lock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300795
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200796 ept = idr_find(&vrp->endpoints, msg->dst);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300797
798 /* let's make sure no one deallocates ept while we use it */
799 if (ept)
800 kref_get(&ept->refcount);
801
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200802 mutex_unlock(&vrp->endpoints_lock);
803
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300804 if (ept) {
805 /* make sure ept->cb doesn't go away while we use it */
806 mutex_lock(&ept->cb_lock);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200807
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300808 if (ept->cb)
809 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
810 msg->src);
811
812 mutex_unlock(&ept->cb_lock);
813
814 /* farewell, ept, we don't need you anymore */
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300815 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300816 } else
Masanari Iida8a168ca2012-12-29 02:00:09 +0900817 dev_warn(dev, "msg received with no recipient\n");
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300818
Ohad Ben-Cohenf1d9e9c2012-02-28 16:11:28 +0200819 /* publish the real size of the buffer */
820 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200821
822 /* add the buffer back to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030823 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200824 if (err < 0) {
825 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300826 return err;
827 }
828
829 return 0;
830}
831
832/* called when an rx buffer is used, and it's time to digest a message */
833static void rpmsg_recv_done(struct virtqueue *rvq)
834{
835 struct virtproc_info *vrp = rvq->vdev->priv;
836 struct device *dev = &rvq->vdev->dev;
837 struct rpmsg_hdr *msg;
838 unsigned int len, msgs_received = 0;
839 int err;
840
841 msg = virtqueue_get_buf(rvq, &len);
842 if (!msg) {
843 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200844 return;
845 }
846
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300847 while (msg) {
848 err = rpmsg_recv_single(vrp, dev, msg, len);
849 if (err)
850 break;
851
852 msgs_received++;
853
854 msg = virtqueue_get_buf(rvq, &len);
Lee Jones6c49fbe2016-07-20 10:29:35 +0100855 }
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300856
857 dev_dbg(dev, "Received %u messages\n", msgs_received);
858
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200859 /* tell the remote processor we added another available rx buffer */
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300860 if (msgs_received)
861 virtqueue_kick(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200862}
863
864/*
865 * This is invoked whenever the remote processor completed processing
866 * a TX msg we just sent it, and the buffer is put back to the used ring.
867 *
868 * Normally, though, we suppress this "tx complete" interrupt in order to
869 * avoid the incurred overhead.
870 */
871static void rpmsg_xmit_done(struct virtqueue *svq)
872{
873 struct virtproc_info *vrp = svq->vdev->priv;
874
875 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
876
877 /* wake up potential senders that are waiting for a tx buffer */
878 wake_up_interruptible(&vrp->sendq);
879}
880
881/* invoked when a name service announcement arrives */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700882static void rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
Anna, Suman09636792016-08-12 18:42:26 -0500883 void *priv, u32 src)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200884{
885 struct rpmsg_ns_msg *msg = data;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700886 struct rpmsg_device *newch;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200887 struct rpmsg_channel_info chinfo;
888 struct virtproc_info *vrp = priv;
889 struct device *dev = &vrp->vdev->dev;
890 int ret;
891
Anna, Suman211e3a92016-08-12 18:42:27 -0500892#if defined(CONFIG_DYNAMIC_DEBUG)
893 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
894 data, len, true);
895#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200896
897 if (len != sizeof(*msg)) {
898 dev_err(dev, "malformed ns msg (%d)\n", len);
899 return;
900 }
901
902 /*
903 * the name service ept does _not_ belong to a real rpmsg channel,
904 * and is handled by the rpmsg bus itself.
905 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
906 * in somehow.
907 */
908 if (rpdev) {
909 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
910 return;
911 }
912
913 /* don't trust the remote processor for null terminating the name */
914 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
915
916 dev_info(dev, "%sing channel %s addr 0x%x\n",
Anna, Suman09636792016-08-12 18:42:26 -0500917 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
918 msg->name, msg->addr);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200919
920 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
921 chinfo.src = RPMSG_ADDR_ANY;
922 chinfo.dst = msg->addr;
923
924 if (msg->flags & RPMSG_NS_DESTROY) {
925 ret = rpmsg_destroy_channel(vrp, &chinfo);
926 if (ret)
927 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
928 } else {
929 newch = rpmsg_create_channel(vrp, &chinfo);
930 if (!newch)
931 dev_err(dev, "rpmsg_create_channel failed\n");
932 }
933}
934
935static int rpmsg_probe(struct virtio_device *vdev)
936{
937 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800938 static const char * const names[] = { "input", "output" };
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200939 struct virtqueue *vqs[2];
940 struct virtproc_info *vrp;
941 void *bufs_va;
942 int err = 0, i;
Suman Annab1b98912014-09-16 13:33:07 -0500943 size_t total_buf_space;
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030944 bool notify;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200945
946 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
947 if (!vrp)
948 return -ENOMEM;
949
950 vrp->vdev = vdev;
951
952 idr_init(&vrp->endpoints);
953 mutex_init(&vrp->endpoints_lock);
954 mutex_init(&vrp->tx_lock);
955 init_waitqueue_head(&vrp->sendq);
956
957 /* We expect two virtqueues, rx and tx (and in this order) */
958 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
959 if (err)
960 goto free_vrp;
961
962 vrp->rvq = vqs[0];
963 vrp->svq = vqs[1];
964
Suman Annab1b98912014-09-16 13:33:07 -0500965 /* we expect symmetric tx/rx vrings */
966 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
967 virtqueue_get_vring_size(vrp->svq));
968
969 /* we need less buffers if vrings are small */
970 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
971 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
972 else
973 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
974
975 total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
976
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200977 /* allocate coherent memory for the buffers */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300978 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
Suman Annab1b98912014-09-16 13:33:07 -0500979 total_buf_space, &vrp->bufs_dma,
980 GFP_KERNEL);
Wei Yongjun3119b482013-04-29 16:17:09 -0700981 if (!bufs_va) {
982 err = -ENOMEM;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200983 goto vqs_del;
Wei Yongjun3119b482013-04-29 16:17:09 -0700984 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200985
Anna, Suman8d95b322016-08-12 18:42:25 -0500986 dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
987 bufs_va, &vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200988
989 /* half of the buffers is dedicated for RX */
990 vrp->rbufs = bufs_va;
991
992 /* and half is dedicated for TX */
Suman Annab1b98912014-09-16 13:33:07 -0500993 vrp->sbufs = bufs_va + total_buf_space / 2;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200994
995 /* set up the receive buffers */
Suman Annab1b98912014-09-16 13:33:07 -0500996 for (i = 0; i < vrp->num_bufs / 2; i++) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200997 struct scatterlist sg;
998 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
999
1000 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
1001
Rusty Russellcee51d62013-03-20 15:44:29 +10301002 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
Anna, Suman09636792016-08-12 18:42:26 -05001003 GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +10301004 WARN_ON(err); /* sanity check; this can't really happen */
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001005 }
1006
1007 /* suppress "tx-complete" interrupts */
1008 virtqueue_disable_cb(vrp->svq);
1009
1010 vdev->priv = vrp;
1011
1012 /* if supported by the remote processor, enable the name service */
1013 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
1014 /* a dedicated endpoint handles the name service msgs */
1015 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
1016 vrp, RPMSG_NS_ADDR);
1017 if (!vrp->ns_ept) {
1018 dev_err(&vdev->dev, "failed to create the ns ept\n");
1019 err = -ENOMEM;
1020 goto free_coherent;
1021 }
1022 }
1023
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301024 /*
1025 * Prepare to kick but don't notify yet - we can't do this before
1026 * device is ready.
1027 */
1028 notify = virtqueue_kick_prepare(vrp->rvq);
1029
1030 /* From this point on, we can notify and get callbacks. */
1031 virtio_device_ready(vdev);
1032
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001033 /* tell the remote processor it can start sending messages */
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301034 /*
1035 * this might be concurrent with callbacks, but we are only
1036 * doing notify, not a full kick here, so that's ok.
1037 */
1038 if (notify)
1039 virtqueue_notify(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001040
1041 dev_info(&vdev->dev, "rpmsg host is online\n");
1042
1043 return 0;
1044
1045free_coherent:
Suman Annab1b98912014-09-16 13:33:07 -05001046 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1047 bufs_va, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001048vqs_del:
1049 vdev->config->del_vqs(vrp->vdev);
1050free_vrp:
1051 kfree(vrp);
1052 return err;
1053}
1054
1055static int rpmsg_remove_device(struct device *dev, void *data)
1056{
1057 device_unregister(dev);
1058
1059 return 0;
1060}
1061
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001062static void rpmsg_remove(struct virtio_device *vdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001063{
1064 struct virtproc_info *vrp = vdev->priv;
Suman Annab1b98912014-09-16 13:33:07 -05001065 size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001066 int ret;
1067
1068 vdev->config->reset(vdev);
1069
1070 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1071 if (ret)
1072 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1073
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +02001074 if (vrp->ns_ept)
1075 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1076
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001077 idr_destroy(&vrp->endpoints);
1078
1079 vdev->config->del_vqs(vrp->vdev);
1080
Suman Annab1b98912014-09-16 13:33:07 -05001081 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1082 vrp->rbufs, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001083
1084 kfree(vrp);
1085}
1086
1087static struct virtio_device_id id_table[] = {
1088 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1089 { 0 },
1090};
1091
1092static unsigned int features[] = {
1093 VIRTIO_RPMSG_F_NS,
1094};
1095
1096static struct virtio_driver virtio_ipc_driver = {
1097 .feature_table = features,
1098 .feature_table_size = ARRAY_SIZE(features),
1099 .driver.name = KBUILD_MODNAME,
1100 .driver.owner = THIS_MODULE,
1101 .id_table = id_table,
1102 .probe = rpmsg_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001103 .remove = rpmsg_remove,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001104};
1105
1106static int __init rpmsg_init(void)
1107{
1108 int ret;
1109
1110 ret = bus_register(&rpmsg_bus);
1111 if (ret) {
1112 pr_err("failed to register rpmsg bus: %d\n", ret);
1113 return ret;
1114 }
1115
1116 ret = register_virtio_driver(&virtio_ipc_driver);
1117 if (ret) {
1118 pr_err("failed to register virtio driver: %d\n", ret);
1119 bus_unregister(&rpmsg_bus);
1120 }
1121
1122 return ret;
1123}
Federico Fuga96342522012-07-16 10:36:51 +03001124subsys_initcall(rpmsg_init);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001125
1126static void __exit rpmsg_fini(void)
1127{
1128 unregister_virtio_driver(&virtio_ipc_driver);
1129 bus_unregister(&rpmsg_bus);
1130}
1131module_exit(rpmsg_fini);
1132
1133MODULE_DEVICE_TABLE(virtio, id_table);
1134MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1135MODULE_LICENSE("GPL v2");