blob: e1052622c6a24c2bb1512dc52699972b97dd5354 [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
Bjorn Andersson8b881c02016-09-01 15:28:02 -070038#include "rpmsg_internal.h"
39
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020040/**
41 * struct virtproc_info - virtual remote processor state
42 * @vdev: the virtio device
43 * @rvq: rx virtqueue
44 * @svq: tx virtqueue
45 * @rbufs: kernel address of rx buffers
46 * @sbufs: kernel address of tx buffers
Suman Annab1b98912014-09-16 13:33:07 -050047 * @num_bufs: total number of buffers for rx and tx
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020048 * @last_sbuf: index of last tx buffer used
49 * @bufs_dma: dma base addr of the buffers
50 * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
51 * sending a message might require waking up a dozing remote
52 * processor, which involves sleeping, hence the mutex.
53 * @endpoints: idr of local endpoints, allows fast retrieval
54 * @endpoints_lock: lock of the endpoints set
55 * @sendq: wait queue of sending contexts waiting for a tx buffers
56 * @sleepers: number of senders that are waiting for a tx buffer
57 * @ns_ept: the bus's name service endpoint
58 *
59 * This structure stores the rpmsg state of a given virtio remote processor
60 * device (there might be several virtio proc devices for each physical
61 * remote processor).
62 */
63struct virtproc_info {
64 struct virtio_device *vdev;
65 struct virtqueue *rvq, *svq;
66 void *rbufs, *sbufs;
Suman Annab1b98912014-09-16 13:33:07 -050067 unsigned int num_bufs;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020068 int last_sbuf;
69 dma_addr_t bufs_dma;
70 struct mutex tx_lock;
71 struct idr endpoints;
72 struct mutex endpoints_lock;
73 wait_queue_head_t sendq;
74 atomic_t sleepers;
75 struct rpmsg_endpoint *ns_ept;
76};
77
Bjorn Andersson92e1de52016-09-01 15:27:57 -070078#define to_rpmsg_device(d) container_of(d, struct rpmsg_device, dev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020079#define to_rpmsg_driver(d) container_of(d, struct rpmsg_driver, drv)
80
81/*
Suman Annab1b98912014-09-16 13:33:07 -050082 * We're allocating buffers of 512 bytes each for communications. The
83 * number of buffers will be computed from the number of buffers supported
84 * by the vring, upto a maximum of 512 buffers (256 in each direction).
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020085 *
86 * Each buffer will have 16 bytes for the msg header and 496 bytes for
87 * the payload.
88 *
Suman Annab1b98912014-09-16 13:33:07 -050089 * This will utilize a maximum total space of 256KB for the buffers.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020090 *
91 * We might also want to add support for user-provided buffers in time.
92 * This will allow bigger buffer size flexibility, and can also be used
93 * to achieve zero-copy messaging.
94 *
95 * Note that these numbers are purely a decision of this driver - we
96 * can change this without changing anything in the firmware of the remote
97 * processor.
98 */
Suman Annab1b98912014-09-16 13:33:07 -050099#define MAX_RPMSG_NUM_BUFS (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200100#define RPMSG_BUF_SIZE (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200101
102/*
103 * Local addresses are dynamically allocated on-demand.
104 * We do not dynamically assign addresses from the low 1024 range,
105 * in order to reserve that address range for predefined services.
106 */
107#define RPMSG_RESERVED_ADDRESSES (1024)
108
109/* Address 53 is reserved for advertising remote services */
110#define RPMSG_NS_ADDR (53)
111
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700112static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
113static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
114static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
115 u32 dst);
116static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
117 u32 dst, void *data, int len);
118static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
119static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
120 int len, u32 dst);
121static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
122 u32 dst, void *data, int len);
Bjorn Andersson6eed5982016-09-01 15:28:03 -0700123static int rpmsg_register_device(struct rpmsg_device *rpdev);
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700124
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200125/* sysfs show configuration fields */
126#define rpmsg_show_attr(field, path, format_string) \
127static ssize_t \
128field##_show(struct device *dev, \
129 struct device_attribute *attr, char *buf) \
130{ \
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700131 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200132 \
133 return sprintf(buf, format_string, rpdev->path); \
134}
135
136/* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */
137rpmsg_show_attr(name, id.name, "%s\n");
138rpmsg_show_attr(src, src, "0x%x\n");
139rpmsg_show_attr(dst, dst, "0x%x\n");
140rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n");
141
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200142static ssize_t modalias_show(struct device *dev,
143 struct device_attribute *attr, char *buf)
144{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700145 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200146
147 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name);
148}
149
150static struct device_attribute rpmsg_dev_attrs[] = {
151 __ATTR_RO(name),
152 __ATTR_RO(modalias),
153 __ATTR_RO(dst),
154 __ATTR_RO(src),
155 __ATTR_RO(announce),
156 __ATTR_NULL
157};
158
159/* rpmsg devices and drivers are matched using the service name */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700160static inline int rpmsg_id_match(const struct rpmsg_device *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500161 const struct rpmsg_device_id *id)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200162{
163 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0;
164}
165
166/* match rpmsg channel and rpmsg driver */
167static int rpmsg_dev_match(struct device *dev, struct device_driver *drv)
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 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv);
171 const struct rpmsg_device_id *ids = rpdrv->id_table;
172 unsigned int i;
173
Bjorn Anderssona16644c2016-09-01 15:27:53 -0700174 if (ids)
175 for (i = 0; ids[i].name[0]; i++)
176 if (rpmsg_id_match(rpdev, &ids[i]))
177 return 1;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200178
Bjorn Anderssona16644c2016-09-01 15:27:53 -0700179 return of_driver_match_device(dev, drv);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200180}
181
182static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env)
183{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700184 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200185
186 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT,
187 rpdev->id.name);
188}
189
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700190static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
191 .destroy_ept = virtio_rpmsg_destroy_ept,
192 .send = virtio_rpmsg_send,
193 .sendto = virtio_rpmsg_sendto,
194 .send_offchannel = virtio_rpmsg_send_offchannel,
195 .trysend = virtio_rpmsg_trysend,
196 .trysendto = virtio_rpmsg_trysendto,
197 .trysend_offchannel = virtio_rpmsg_trysend_offchannel,
198};
199
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300200/**
201 * __ept_release() - deallocate an rpmsg endpoint
202 * @kref: the ept's reference count
203 *
204 * This function deallocates an ept, and is invoked when its @kref refcount
205 * drops to zero.
206 *
207 * Never invoke this function directly!
208 */
209static void __ept_release(struct kref *kref)
210{
211 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
212 refcount);
213 /*
214 * At this point no one holds a reference to ept anymore,
215 * so we can directly free it
216 */
217 kfree(ept);
218}
219
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200220/* for more info, see below documentation of rpmsg_create_ept() */
221static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700222 struct rpmsg_device *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500223 rpmsg_rx_cb_t cb,
224 void *priv, u32 addr)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200225{
Tejun Heod0ffce72013-02-27 17:04:40 -0800226 int id_min, id_max, id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200227 struct rpmsg_endpoint *ept;
228 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
229
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200230 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500231 if (!ept)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200232 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200233
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300234 kref_init(&ept->refcount);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300235 mutex_init(&ept->cb_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300236
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200237 ept->rpdev = rpdev;
238 ept->cb = cb;
239 ept->priv = priv;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700240 ept->ops = &virtio_endpoint_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200241
242 /* do we need to allocate a local address ? */
Tejun Heod0ffce72013-02-27 17:04:40 -0800243 if (addr == RPMSG_ADDR_ANY) {
244 id_min = RPMSG_RESERVED_ADDRESSES;
245 id_max = 0;
246 } else {
247 id_min = addr;
248 id_max = addr + 1;
249 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200250
251 mutex_lock(&vrp->endpoints_lock);
252
253 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
Tejun Heod0ffce72013-02-27 17:04:40 -0800254 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
255 if (id < 0) {
256 dev_err(dev, "idr_alloc failed: %d\n", id);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200257 goto free_ept;
258 }
Tejun Heod0ffce72013-02-27 17:04:40 -0800259 ept->addr = id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200260
261 mutex_unlock(&vrp->endpoints_lock);
262
263 return ept;
264
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200265free_ept:
266 mutex_unlock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300267 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200268 return NULL;
269}
270
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700271static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev,
272 rpmsg_rx_cb_t cb,
273 void *priv,
274 struct rpmsg_channel_info chinfo)
275{
276 return __rpmsg_create_ept(rpdev->vrp, rpdev, cb, priv, chinfo.src);
277}
278
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200279/**
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200280 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
281 * @vrp: virtproc which owns this ept
282 * @ept: endpoing to destroy
283 *
284 * An internal function which destroy an ept without assuming it is
285 * bound to an rpmsg channel. This is needed for handling the internal
286 * name service endpoint, which isn't bound to an rpmsg channel.
287 * See also __rpmsg_create_ept().
288 */
289static void
290__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
291{
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300292 /* make sure new inbound messages can't find this ept anymore */
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200293 mutex_lock(&vrp->endpoints_lock);
294 idr_remove(&vrp->endpoints, ept->addr);
295 mutex_unlock(&vrp->endpoints_lock);
296
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300297 /* make sure in-flight inbound messages won't invoke cb anymore */
298 mutex_lock(&ept->cb_lock);
299 ept->cb = NULL;
300 mutex_unlock(&ept->cb_lock);
301
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300302 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200303}
304
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700305static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
306{
307 __rpmsg_destroy_ept(ept->rpdev->vrp, ept);
308}
309
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200310/*
311 * when an rpmsg driver is probed with a channel, we seamlessly create
312 * it an endpoint, binding its rx callback to a unique local rpmsg
313 * address.
314 *
315 * if we need to, we also announce about this channel to the remote
316 * processor (needed in case the driver is exposing an rpmsg service).
317 */
318static int rpmsg_dev_probe(struct device *dev)
319{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700320 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200321 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700322 struct rpmsg_channel_info chinfo = {};
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200323 struct rpmsg_endpoint *ept;
324 int err;
325
Bjorn Andersson2b263d22016-09-01 15:27:56 -0700326 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE);
327 chinfo.src = rpdev->src;
328 chinfo.dst = RPMSG_ADDR_ANY;
329
330 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200331 if (!ept) {
332 dev_err(dev, "failed to create endpoint\n");
333 err = -ENOMEM;
334 goto out;
335 }
336
337 rpdev->ept = ept;
338 rpdev->src = ept->addr;
339
340 err = rpdrv->probe(rpdev);
341 if (err) {
342 dev_err(dev, "%s: failed: %d\n", __func__, err);
343 rpmsg_destroy_ept(ept);
344 goto out;
345 }
346
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700347 if (rpdev->ops->announce_create)
348 err = rpdev->ops->announce_create(rpdev);
349out:
350 return err;
351}
352
353static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
354{
355 struct virtproc_info *vrp = rpdev->vrp;
356 struct device *dev = &rpdev->dev;
357 int err = 0;
358
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200359 /* need to tell remote processor's name service about this channel ? */
360 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500361 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200362 struct rpmsg_ns_msg nsm;
363
364 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700365 nsm.addr = rpdev->ept->addr;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200366 nsm.flags = RPMSG_NS_CREATE;
367
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700368 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200369 if (err)
370 dev_err(dev, "failed to announce service %d\n", err);
371 }
372
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200373 return err;
374}
375
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700376static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200377{
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200378 struct virtproc_info *vrp = rpdev->vrp;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700379 struct device *dev = &rpdev->dev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200380 int err = 0;
381
382 /* tell remote processor's name service we're removing this channel */
383 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500384 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200385 struct rpmsg_ns_msg nsm;
386
387 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
388 nsm.addr = rpdev->src;
389 nsm.flags = RPMSG_NS_DESTROY;
390
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700391 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200392 if (err)
393 dev_err(dev, "failed to announce service %d\n", err);
394 }
395
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700396 return err;
397}
398
399static int rpmsg_dev_remove(struct device *dev)
400{
401 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
402 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver);
403 int err = 0;
404
405 if (rpdev->ops->announce_destroy)
406 err = rpdev->ops->announce_destroy(rpdev);
407
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200408 rpdrv->remove(rpdev);
409
410 rpmsg_destroy_ept(rpdev->ept);
411
412 return err;
413}
414
415static struct bus_type rpmsg_bus = {
416 .name = "rpmsg",
417 .match = rpmsg_dev_match,
418 .dev_attrs = rpmsg_dev_attrs,
419 .uevent = rpmsg_uevent,
420 .probe = rpmsg_dev_probe,
421 .remove = rpmsg_dev_remove,
422};
423
424/**
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500425 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200426 * @rpdrv: pointer to a struct rpmsg_driver
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500427 * @owner: owning module/driver
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200428 *
429 * Returns 0 on success, and an appropriate error value on failure.
430 */
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500431int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200432{
433 rpdrv->drv.bus = &rpmsg_bus;
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500434 rpdrv->drv.owner = owner;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200435 return driver_register(&rpdrv->drv);
436}
Andrew F. Davisbc3c57c2016-05-04 17:01:36 -0500437EXPORT_SYMBOL(__register_rpmsg_driver);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200438
439/**
440 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus
441 * @rpdrv: pointer to a struct rpmsg_driver
442 *
443 * Returns 0 on success, and an appropriate error value on failure.
444 */
445void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv)
446{
447 driver_unregister(&rpdrv->drv);
448}
449EXPORT_SYMBOL(unregister_rpmsg_driver);
450
451static void rpmsg_release_device(struct device *dev)
452{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700453 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200454
455 kfree(rpdev);
456}
457
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700458static const struct rpmsg_device_ops virtio_rpmsg_ops = {
459 .create_ept = virtio_rpmsg_create_ept,
460 .announce_create = virtio_rpmsg_announce_create,
461 .announce_destroy = virtio_rpmsg_announce_destroy,
462};
463
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200464/*
465 * create an rpmsg channel using its name and address info.
466 * this function will be used to create both static and dynamic
467 * channels.
468 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700469static struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp,
470 struct rpmsg_channel_info *chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200471{
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700472 struct rpmsg_device *rpdev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200473 struct device *tmp, *dev = &vrp->vdev->dev;
474 int ret;
475
476 /* make sure a similar channel doesn't already exist */
Bjorn Andersson8b881c02016-09-01 15:28:02 -0700477 tmp = rpmsg_find_device(dev, chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200478 if (tmp) {
479 /* decrement the matched device's refcount back */
480 put_device(tmp);
481 dev_err(dev, "channel %s:%x:%x already exist\n",
482 chinfo->name, chinfo->src, chinfo->dst);
483 return NULL;
484 }
485
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500486 rpdev = kzalloc(sizeof(*rpdev), GFP_KERNEL);
487 if (!rpdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200488 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200489
490 rpdev->vrp = vrp;
491 rpdev->src = chinfo->src;
492 rpdev->dst = chinfo->dst;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700493 rpdev->ops = &virtio_rpmsg_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200494
495 /*
496 * rpmsg server channels has predefined local address (for now),
497 * and their existence needs to be announced remotely
498 */
Andrew F. Davisc8ced112016-07-01 09:24:58 -0500499 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200500
501 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
502
Bjorn Andersson6eed5982016-09-01 15:28:03 -0700503 rpdev->dev.parent = &vrp->vdev->dev;
504 ret = rpmsg_register_device(rpdev);
505 if (ret)
506 return NULL;
507
508 return rpdev;
509}
510
511static int rpmsg_register_device(struct rpmsg_device *rpdev)
512{
513 struct device *dev = &rpdev->dev;
514 int ret;
515
Bjorn Andersson4dffed52016-09-01 15:27:54 -0700516 dev_set_name(&rpdev->dev, "%s:%s",
517 dev_name(dev->parent), rpdev->id.name);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200518
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200519 rpdev->dev.bus = &rpmsg_bus;
520 rpdev->dev.release = rpmsg_release_device;
521
522 ret = device_register(&rpdev->dev);
523 if (ret) {
524 dev_err(dev, "device_register failed: %d\n", ret);
525 put_device(&rpdev->dev);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200526 }
527
Bjorn Andersson6eed5982016-09-01 15:28:03 -0700528 return ret;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200529}
530
531/*
532 * find an existing channel using its name + address properties,
533 * and destroy it
534 */
535static int rpmsg_destroy_channel(struct virtproc_info *vrp,
Anna, Suman09636792016-08-12 18:42:26 -0500536 struct rpmsg_channel_info *chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200537{
538 struct virtio_device *vdev = vrp->vdev;
539 struct device *dev;
540
Bjorn Andersson8b881c02016-09-01 15:28:02 -0700541 dev = rpmsg_find_device(&vdev->dev, chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200542 if (!dev)
543 return -EINVAL;
544
545 device_unregister(dev);
546
547 put_device(dev);
548
549 return 0;
550}
551
552/* super simple buffer "allocator" that is just enough for now */
553static void *get_a_tx_buf(struct virtproc_info *vrp)
554{
555 unsigned int len;
556 void *ret;
557
558 /* support multiple concurrent senders */
559 mutex_lock(&vrp->tx_lock);
560
561 /*
562 * either pick the next unused tx buffer
563 * (half of our buffers are used for sending messages)
564 */
Suman Annab1b98912014-09-16 13:33:07 -0500565 if (vrp->last_sbuf < vrp->num_bufs / 2)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200566 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
567 /* or recycle a used one */
568 else
569 ret = virtqueue_get_buf(vrp->svq, &len);
570
571 mutex_unlock(&vrp->tx_lock);
572
573 return ret;
574}
575
576/**
577 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
578 * @vrp: virtual remote processor state
579 *
580 * This function is called before a sender is blocked, waiting for
581 * a tx buffer to become available.
582 *
583 * If we already have blocking senders, this function merely increases
584 * the "sleepers" reference count, and exits.
585 *
586 * Otherwise, if this is the first sender to block, we also enable
587 * virtio's tx callbacks, so we'd be immediately notified when a tx
588 * buffer is consumed (we rely on virtio's tx callback in order
589 * to wake up sleeping senders as soon as a tx buffer is used by the
590 * remote processor).
591 */
592static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
593{
594 /* support multiple concurrent senders */
595 mutex_lock(&vrp->tx_lock);
596
597 /* are we the first sleeping context waiting for tx buffers ? */
598 if (atomic_inc_return(&vrp->sleepers) == 1)
599 /* enable "tx-complete" interrupts before dozing off */
600 virtqueue_enable_cb(vrp->svq);
601
602 mutex_unlock(&vrp->tx_lock);
603}
604
605/**
606 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
607 * @vrp: virtual remote processor state
608 *
609 * This function is called after a sender, that waited for a tx buffer
610 * to become available, is unblocked.
611 *
612 * If we still have blocking senders, this function merely decreases
613 * the "sleepers" reference count, and exits.
614 *
615 * Otherwise, if there are no more blocking senders, we also disable
616 * virtio's tx callbacks, to avoid the overhead incurred with handling
617 * those (now redundant) interrupts.
618 */
619static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
620{
621 /* support multiple concurrent senders */
622 mutex_lock(&vrp->tx_lock);
623
624 /* are we the last sleeping context waiting for tx buffers ? */
625 if (atomic_dec_and_test(&vrp->sleepers))
626 /* disable "tx-complete" interrupts */
627 virtqueue_disable_cb(vrp->svq);
628
629 mutex_unlock(&vrp->tx_lock);
630}
631
632/**
633 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
634 * @rpdev: the rpmsg channel
635 * @src: source address
636 * @dst: destination address
637 * @data: payload of message
638 * @len: length of payload
639 * @wait: indicates whether caller should block in case no TX buffers available
640 *
641 * This function is the base implementation for all of the rpmsg sending API.
642 *
643 * It will send @data of length @len to @dst, and say it's from @src. The
644 * message will be sent to the remote processor which the @rpdev channel
645 * belongs to.
646 *
647 * The message is sent using one of the TX buffers that are available for
648 * communication with this remote processor.
649 *
650 * If @wait is true, the caller will be blocked until either a TX buffer is
651 * available, or 15 seconds elapses (we don't want callers to
652 * sleep indefinitely due to misbehaving remote processors), and in that
653 * case -ERESTARTSYS is returned. The number '15' itself was picked
654 * arbitrarily; there's little point in asking drivers to provide a timeout
655 * value themselves.
656 *
657 * Otherwise, if @wait is false, and there are no TX buffers available,
658 * the function will immediately fail, and -ENOMEM will be returned.
659 *
660 * Normally drivers shouldn't use this function directly; instead, drivers
661 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
662 * (see include/linux/rpmsg.h).
663 *
664 * Returns 0 on success and an appropriate error value on failure.
665 */
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700666static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
667 u32 src, u32 dst,
668 void *data, int len, bool wait)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200669{
670 struct virtproc_info *vrp = rpdev->vrp;
671 struct device *dev = &rpdev->dev;
672 struct scatterlist sg;
673 struct rpmsg_hdr *msg;
674 int err;
675
676 /* bcasting isn't allowed */
677 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
678 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
679 return -EINVAL;
680 }
681
682 /*
683 * We currently use fixed-sized buffers, and therefore the payload
684 * length is limited.
685 *
686 * One of the possible improvements here is either to support
687 * user-provided buffers (and then we can also support zero-copy
688 * messaging), or to improve the buffer allocator, to support
689 * variable-length buffer sizes.
690 */
691 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
692 dev_err(dev, "message is too big (%d)\n", len);
693 return -EMSGSIZE;
694 }
695
696 /* grab a buffer */
697 msg = get_a_tx_buf(vrp);
698 if (!msg && !wait)
699 return -ENOMEM;
700
701 /* no free buffer ? wait for one (but bail after 15 seconds) */
702 while (!msg) {
703 /* enable "tx-complete" interrupts, if not already enabled */
704 rpmsg_upref_sleepers(vrp);
705
706 /*
707 * sleep until a free buffer is available or 15 secs elapse.
708 * the timeout period is not configurable because there's
709 * little point in asking drivers to specify that.
710 * if later this happens to be required, it'd be easy to add.
711 */
712 err = wait_event_interruptible_timeout(vrp->sendq,
713 (msg = get_a_tx_buf(vrp)),
714 msecs_to_jiffies(15000));
715
716 /* disable "tx-complete" interrupts if we're the last sleeper */
717 rpmsg_downref_sleepers(vrp);
718
719 /* timeout ? */
720 if (!err) {
721 dev_err(dev, "timeout waiting for a tx buffer\n");
722 return -ERESTARTSYS;
723 }
724 }
725
726 msg->len = len;
727 msg->flags = 0;
728 msg->src = src;
729 msg->dst = dst;
730 msg->reserved = 0;
731 memcpy(msg->data, data, len);
732
733 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500734 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500735#if defined(CONFIG_DYNAMIC_DEBUG)
736 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
737 msg, sizeof(*msg) + msg->len, true);
738#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200739
740 sg_init_one(&sg, msg, sizeof(*msg) + len);
741
742 mutex_lock(&vrp->tx_lock);
743
744 /* add message to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030745 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030746 if (err) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200747 /*
748 * need to reclaim the buffer here, otherwise it's lost
749 * (memory won't leak, but rpmsg won't use it again for TX).
750 * this will wait for a buffer management overhaul.
751 */
Rusty Russellcee51d62013-03-20 15:44:29 +1030752 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200753 goto out;
754 }
755
756 /* tell the remote processor it has a pending message to read */
757 virtqueue_kick(vrp->svq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200758out:
759 mutex_unlock(&vrp->tx_lock);
760 return err;
761}
762EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
763
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700764static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
765{
766 struct rpmsg_device *rpdev = ept->rpdev;
767 u32 src = ept->addr, dst = rpdev->dst;
768
769 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
770}
771
772static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
773 u32 dst)
774{
775 struct rpmsg_device *rpdev = ept->rpdev;
776 u32 src = ept->addr;
777
778 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
779}
780
781static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
782 u32 dst, void *data, int len)
783{
784 struct rpmsg_device *rpdev = ept->rpdev;
785
786 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
787}
788
789static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
790{
791 struct rpmsg_device *rpdev = ept->rpdev;
792 u32 src = ept->addr, dst = rpdev->dst;
793
794 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
795}
796
797static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
798 int len, u32 dst)
799{
800 struct rpmsg_device *rpdev = ept->rpdev;
801 u32 src = ept->addr;
802
803 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
804}
805
806static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
807 u32 dst, void *data, int len)
808{
809 struct rpmsg_device *rpdev = ept->rpdev;
810
811 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
812}
813
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300814static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
815 struct rpmsg_hdr *msg, unsigned int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200816{
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200817 struct rpmsg_endpoint *ept;
818 struct scatterlist sg;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200819 int err;
820
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200821 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500822 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500823#if defined(CONFIG_DYNAMIC_DEBUG)
824 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
825 msg, sizeof(*msg) + msg->len, true);
826#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200827
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200828 /*
829 * We currently use fixed-sized buffers, so trivially sanitize
830 * the reported payload length.
831 */
832 if (len > RPMSG_BUF_SIZE ||
Anna, Suman09636792016-08-12 18:42:26 -0500833 msg->len > (len - sizeof(struct rpmsg_hdr))) {
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200834 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300835 return -EINVAL;
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200836 }
837
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200838 /* use the dst addr to fetch the callback of the appropriate user */
839 mutex_lock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300840
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200841 ept = idr_find(&vrp->endpoints, msg->dst);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300842
843 /* let's make sure no one deallocates ept while we use it */
844 if (ept)
845 kref_get(&ept->refcount);
846
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200847 mutex_unlock(&vrp->endpoints_lock);
848
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300849 if (ept) {
850 /* make sure ept->cb doesn't go away while we use it */
851 mutex_lock(&ept->cb_lock);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200852
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300853 if (ept->cb)
854 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
855 msg->src);
856
857 mutex_unlock(&ept->cb_lock);
858
859 /* farewell, ept, we don't need you anymore */
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300860 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300861 } else
Masanari Iida8a168ca2012-12-29 02:00:09 +0900862 dev_warn(dev, "msg received with no recipient\n");
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300863
Ohad Ben-Cohenf1d9e9c2012-02-28 16:11:28 +0200864 /* publish the real size of the buffer */
865 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200866
867 /* add the buffer back to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030868 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200869 if (err < 0) {
870 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300871 return err;
872 }
873
874 return 0;
875}
876
877/* called when an rx buffer is used, and it's time to digest a message */
878static void rpmsg_recv_done(struct virtqueue *rvq)
879{
880 struct virtproc_info *vrp = rvq->vdev->priv;
881 struct device *dev = &rvq->vdev->dev;
882 struct rpmsg_hdr *msg;
883 unsigned int len, msgs_received = 0;
884 int err;
885
886 msg = virtqueue_get_buf(rvq, &len);
887 if (!msg) {
888 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200889 return;
890 }
891
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300892 while (msg) {
893 err = rpmsg_recv_single(vrp, dev, msg, len);
894 if (err)
895 break;
896
897 msgs_received++;
898
899 msg = virtqueue_get_buf(rvq, &len);
Lee Jones6c49fbe2016-07-20 10:29:35 +0100900 }
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300901
902 dev_dbg(dev, "Received %u messages\n", msgs_received);
903
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200904 /* tell the remote processor we added another available rx buffer */
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300905 if (msgs_received)
906 virtqueue_kick(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200907}
908
909/*
910 * This is invoked whenever the remote processor completed processing
911 * a TX msg we just sent it, and the buffer is put back to the used ring.
912 *
913 * Normally, though, we suppress this "tx complete" interrupt in order to
914 * avoid the incurred overhead.
915 */
916static void rpmsg_xmit_done(struct virtqueue *svq)
917{
918 struct virtproc_info *vrp = svq->vdev->priv;
919
920 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
921
922 /* wake up potential senders that are waiting for a tx buffer */
923 wake_up_interruptible(&vrp->sendq);
924}
925
926/* invoked when a name service announcement arrives */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700927static void rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
Anna, Suman09636792016-08-12 18:42:26 -0500928 void *priv, u32 src)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200929{
930 struct rpmsg_ns_msg *msg = data;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700931 struct rpmsg_device *newch;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200932 struct rpmsg_channel_info chinfo;
933 struct virtproc_info *vrp = priv;
934 struct device *dev = &vrp->vdev->dev;
935 int ret;
936
Anna, Suman211e3a92016-08-12 18:42:27 -0500937#if defined(CONFIG_DYNAMIC_DEBUG)
938 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
939 data, len, true);
940#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200941
942 if (len != sizeof(*msg)) {
943 dev_err(dev, "malformed ns msg (%d)\n", len);
944 return;
945 }
946
947 /*
948 * the name service ept does _not_ belong to a real rpmsg channel,
949 * and is handled by the rpmsg bus itself.
950 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
951 * in somehow.
952 */
953 if (rpdev) {
954 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
955 return;
956 }
957
958 /* don't trust the remote processor for null terminating the name */
959 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
960
961 dev_info(dev, "%sing channel %s addr 0x%x\n",
Anna, Suman09636792016-08-12 18:42:26 -0500962 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
963 msg->name, msg->addr);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200964
965 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
966 chinfo.src = RPMSG_ADDR_ANY;
967 chinfo.dst = msg->addr;
968
969 if (msg->flags & RPMSG_NS_DESTROY) {
970 ret = rpmsg_destroy_channel(vrp, &chinfo);
971 if (ret)
972 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
973 } else {
974 newch = rpmsg_create_channel(vrp, &chinfo);
975 if (!newch)
976 dev_err(dev, "rpmsg_create_channel failed\n");
977 }
978}
979
980static int rpmsg_probe(struct virtio_device *vdev)
981{
982 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800983 static const char * const names[] = { "input", "output" };
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200984 struct virtqueue *vqs[2];
985 struct virtproc_info *vrp;
986 void *bufs_va;
987 int err = 0, i;
Suman Annab1b98912014-09-16 13:33:07 -0500988 size_t total_buf_space;
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030989 bool notify;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200990
991 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
992 if (!vrp)
993 return -ENOMEM;
994
995 vrp->vdev = vdev;
996
997 idr_init(&vrp->endpoints);
998 mutex_init(&vrp->endpoints_lock);
999 mutex_init(&vrp->tx_lock);
1000 init_waitqueue_head(&vrp->sendq);
1001
1002 /* We expect two virtqueues, rx and tx (and in this order) */
1003 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
1004 if (err)
1005 goto free_vrp;
1006
1007 vrp->rvq = vqs[0];
1008 vrp->svq = vqs[1];
1009
Suman Annab1b98912014-09-16 13:33:07 -05001010 /* we expect symmetric tx/rx vrings */
1011 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
1012 virtqueue_get_vring_size(vrp->svq));
1013
1014 /* we need less buffers if vrings are small */
1015 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
1016 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
1017 else
1018 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
1019
1020 total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
1021
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001022 /* allocate coherent memory for the buffers */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +03001023 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
Suman Annab1b98912014-09-16 13:33:07 -05001024 total_buf_space, &vrp->bufs_dma,
1025 GFP_KERNEL);
Wei Yongjun3119b482013-04-29 16:17:09 -07001026 if (!bufs_va) {
1027 err = -ENOMEM;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001028 goto vqs_del;
Wei Yongjun3119b482013-04-29 16:17:09 -07001029 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001030
Anna, Suman8d95b322016-08-12 18:42:25 -05001031 dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
1032 bufs_va, &vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001033
1034 /* half of the buffers is dedicated for RX */
1035 vrp->rbufs = bufs_va;
1036
1037 /* and half is dedicated for TX */
Suman Annab1b98912014-09-16 13:33:07 -05001038 vrp->sbufs = bufs_va + total_buf_space / 2;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001039
1040 /* set up the receive buffers */
Suman Annab1b98912014-09-16 13:33:07 -05001041 for (i = 0; i < vrp->num_bufs / 2; i++) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001042 struct scatterlist sg;
1043 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
1044
1045 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
1046
Rusty Russellcee51d62013-03-20 15:44:29 +10301047 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
Anna, Suman09636792016-08-12 18:42:26 -05001048 GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +10301049 WARN_ON(err); /* sanity check; this can't really happen */
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001050 }
1051
1052 /* suppress "tx-complete" interrupts */
1053 virtqueue_disable_cb(vrp->svq);
1054
1055 vdev->priv = vrp;
1056
1057 /* if supported by the remote processor, enable the name service */
1058 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
1059 /* a dedicated endpoint handles the name service msgs */
1060 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
1061 vrp, RPMSG_NS_ADDR);
1062 if (!vrp->ns_ept) {
1063 dev_err(&vdev->dev, "failed to create the ns ept\n");
1064 err = -ENOMEM;
1065 goto free_coherent;
1066 }
1067 }
1068
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301069 /*
1070 * Prepare to kick but don't notify yet - we can't do this before
1071 * device is ready.
1072 */
1073 notify = virtqueue_kick_prepare(vrp->rvq);
1074
1075 /* From this point on, we can notify and get callbacks. */
1076 virtio_device_ready(vdev);
1077
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001078 /* tell the remote processor it can start sending messages */
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +10301079 /*
1080 * this might be concurrent with callbacks, but we are only
1081 * doing notify, not a full kick here, so that's ok.
1082 */
1083 if (notify)
1084 virtqueue_notify(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001085
1086 dev_info(&vdev->dev, "rpmsg host is online\n");
1087
1088 return 0;
1089
1090free_coherent:
Suman Annab1b98912014-09-16 13:33:07 -05001091 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1092 bufs_va, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001093vqs_del:
1094 vdev->config->del_vqs(vrp->vdev);
1095free_vrp:
1096 kfree(vrp);
1097 return err;
1098}
1099
1100static int rpmsg_remove_device(struct device *dev, void *data)
1101{
1102 device_unregister(dev);
1103
1104 return 0;
1105}
1106
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001107static void rpmsg_remove(struct virtio_device *vdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001108{
1109 struct virtproc_info *vrp = vdev->priv;
Suman Annab1b98912014-09-16 13:33:07 -05001110 size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001111 int ret;
1112
1113 vdev->config->reset(vdev);
1114
1115 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1116 if (ret)
1117 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1118
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +02001119 if (vrp->ns_ept)
1120 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
1121
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001122 idr_destroy(&vrp->endpoints);
1123
1124 vdev->config->del_vqs(vrp->vdev);
1125
Suman Annab1b98912014-09-16 13:33:07 -05001126 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1127 vrp->rbufs, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001128
1129 kfree(vrp);
1130}
1131
1132static struct virtio_device_id id_table[] = {
1133 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1134 { 0 },
1135};
1136
1137static unsigned int features[] = {
1138 VIRTIO_RPMSG_F_NS,
1139};
1140
1141static struct virtio_driver virtio_ipc_driver = {
1142 .feature_table = features,
1143 .feature_table_size = ARRAY_SIZE(features),
1144 .driver.name = KBUILD_MODNAME,
1145 .driver.owner = THIS_MODULE,
1146 .id_table = id_table,
1147 .probe = rpmsg_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001148 .remove = rpmsg_remove,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001149};
1150
1151static int __init rpmsg_init(void)
1152{
1153 int ret;
1154
1155 ret = bus_register(&rpmsg_bus);
1156 if (ret) {
1157 pr_err("failed to register rpmsg bus: %d\n", ret);
1158 return ret;
1159 }
1160
1161 ret = register_virtio_driver(&virtio_ipc_driver);
1162 if (ret) {
1163 pr_err("failed to register virtio driver: %d\n", ret);
1164 bus_unregister(&rpmsg_bus);
1165 }
1166
1167 return ret;
1168}
Federico Fuga96342522012-07-16 10:36:51 +03001169subsys_initcall(rpmsg_init);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001170
1171static void __exit rpmsg_fini(void)
1172{
1173 unregister_virtio_driver(&virtio_ipc_driver);
1174 bus_unregister(&rpmsg_bus);
1175}
1176module_exit(rpmsg_fini);
1177
1178MODULE_DEVICE_TABLE(virtio, id_table);
1179MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1180MODULE_LICENSE("GPL v2");