Merge patch series "RISC-V Hibernation Support"
[linux-block.git] / drivers / rpmsg / virtio_rpmsg_bus.c
CommitLineData
c3bab824 1// SPDX-License-Identifier: GPL-2.0
bcabbcca
OBC
2/*
3 * Virtio-based remote processor messaging bus
4 *
5 * Copyright (C) 2011 Texas Instruments, Inc.
6 * Copyright (C) 2011 Google, Inc.
7 *
8 * Ohad Ben-Cohen <ohad@wizery.com>
9 * Brian Swetland <swetland@google.com>
bcabbcca
OBC
10 */
11
12#define pr_fmt(fmt) "%s: " fmt, __func__
13
6de1c933
LP
14#include <linux/dma-mapping.h>
15#include <linux/idr.h>
16#include <linux/jiffies.h>
bcabbcca
OBC
17#include <linux/kernel.h>
18#include <linux/module.h>
6de1c933 19#include <linux/mutex.h>
6de1c933 20#include <linux/rpmsg.h>
5f2f6b7d 21#include <linux/rpmsg/byteorder.h>
c435a041 22#include <linux/rpmsg/ns.h>
bcabbcca 23#include <linux/scatterlist.h>
bcabbcca 24#include <linux/slab.h>
bcabbcca 25#include <linux/sched.h>
6de1c933
LP
26#include <linux/virtio.h>
27#include <linux/virtio_ids.h>
28#include <linux/virtio_config.h>
bcabbcca 29#include <linux/wait.h>
bcabbcca 30
8b881c07
BA
31#include "rpmsg_internal.h"
32
bcabbcca
OBC
33/**
34 * struct virtproc_info - virtual remote processor state
35 * @vdev: the virtio device
36 * @rvq: rx virtqueue
37 * @svq: tx virtqueue
38 * @rbufs: kernel address of rx buffers
39 * @sbufs: kernel address of tx buffers
b1b98914 40 * @num_bufs: total number of buffers for rx and tx
f93848f9 41 * @buf_size: size of one rx or tx buffer
bcabbcca
OBC
42 * @last_sbuf: index of last tx buffer used
43 * @bufs_dma: dma base addr of the buffers
44 * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
45 * sending a message might require waking up a dozing remote
46 * processor, which involves sleeping, hence the mutex.
47 * @endpoints: idr of local endpoints, allows fast retrieval
48 * @endpoints_lock: lock of the endpoints set
49 * @sendq: wait queue of sending contexts waiting for a tx buffers
50 * @sleepers: number of senders that are waiting for a tx buffer
bcabbcca
OBC
51 *
52 * This structure stores the rpmsg state of a given virtio remote processor
53 * device (there might be several virtio proc devices for each physical
54 * remote processor).
55 */
56struct virtproc_info {
57 struct virtio_device *vdev;
58 struct virtqueue *rvq, *svq;
59 void *rbufs, *sbufs;
b1b98914 60 unsigned int num_bufs;
f93848f9 61 unsigned int buf_size;
bcabbcca
OBC
62 int last_sbuf;
63 dma_addr_t bufs_dma;
64 struct mutex tx_lock;
65 struct idr endpoints;
66 struct mutex endpoints_lock;
67 wait_queue_head_t sendq;
68 atomic_t sleepers;
bcabbcca
OBC
69};
70
e88dae5d
BA
71/* The feature bitmap for virtio rpmsg */
72#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
73
74/**
75 * struct rpmsg_hdr - common header for all rpmsg messages
76 * @src: source address
77 * @dst: destination address
78 * @reserved: reserved for future use
79 * @len: length of payload (in bytes)
80 * @flags: message flags
81 * @data: @len bytes of message payload data
82 *
83 * Every message sent(/received) on the rpmsg bus begins with this header.
84 */
85struct rpmsg_hdr {
5f2f6b7d
MP
86 __rpmsg32 src;
87 __rpmsg32 dst;
88 __rpmsg32 reserved;
89 __rpmsg16 len;
90 __rpmsg16 flags;
4f05fc33 91 u8 data[];
e88dae5d
BA
92} __packed;
93
e88dae5d 94
3bf950ff 95/**
6c09ea0b
AP
96 * struct virtio_rpmsg_channel - rpmsg channel descriptor
97 * @rpdev: the rpmsg channel device
98 * @vrp: the virtio remote processor device this channel belongs to
99 *
100 * This structure stores the channel that links the rpmsg device to the virtio
101 * remote processor device.
3bf950ff
BA
102 */
103struct virtio_rpmsg_channel {
104 struct rpmsg_device rpdev;
105
106 struct virtproc_info *vrp;
107};
108
109#define to_virtio_rpmsg_channel(_rpdev) \
110 container_of(_rpdev, struct virtio_rpmsg_channel, rpdev)
111
bcabbcca 112/*
b1b98914
SA
113 * We're allocating buffers of 512 bytes each for communications. The
114 * number of buffers will be computed from the number of buffers supported
115 * by the vring, upto a maximum of 512 buffers (256 in each direction).
bcabbcca
OBC
116 *
117 * Each buffer will have 16 bytes for the msg header and 496 bytes for
118 * the payload.
119 *
b1b98914 120 * This will utilize a maximum total space of 256KB for the buffers.
bcabbcca
OBC
121 *
122 * We might also want to add support for user-provided buffers in time.
123 * This will allow bigger buffer size flexibility, and can also be used
124 * to achieve zero-copy messaging.
125 *
126 * Note that these numbers are purely a decision of this driver - we
127 * can change this without changing anything in the firmware of the remote
128 * processor.
129 */
b1b98914 130#define MAX_RPMSG_NUM_BUFS (512)
f93848f9 131#define MAX_RPMSG_BUF_SIZE (512)
bcabbcca
OBC
132
133/*
134 * Local addresses are dynamically allocated on-demand.
135 * We do not dynamically assign addresses from the low 1024 range,
136 * in order to reserve that address range for predefined services.
137 */
138#define RPMSG_RESERVED_ADDRESSES (1024)
139
8a228ecf
BA
140static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
141static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
142static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
143 u32 dst);
144static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
145 u32 dst, void *data, int len);
146static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
147static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
148 int len, u32 dst);
149static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
150 u32 dst, void *data, int len);
e279317e 151static ssize_t virtio_rpmsg_get_mtu(struct rpmsg_endpoint *ept);
1ee1e5e1
AP
152static struct rpmsg_device *__rpmsg_create_channel(struct virtproc_info *vrp,
153 struct rpmsg_channel_info *chinfo);
bcabbcca 154
8a228ecf
BA
155static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
156 .destroy_ept = virtio_rpmsg_destroy_ept,
157 .send = virtio_rpmsg_send,
158 .sendto = virtio_rpmsg_sendto,
159 .send_offchannel = virtio_rpmsg_send_offchannel,
160 .trysend = virtio_rpmsg_trysend,
161 .trysendto = virtio_rpmsg_trysendto,
162 .trysend_offchannel = virtio_rpmsg_trysend_offchannel,
e279317e 163 .get_mtu = virtio_rpmsg_get_mtu,
8a228ecf
BA
164};
165
9dd87c2a
LP
166/**
167 * rpmsg_sg_init - initialize scatterlist according to cpu address location
168 * @sg: scatterlist to fill
169 * @cpu_addr: virtual address of the buffer
170 * @len: buffer length
171 *
172 * An internal function filling scatterlist according to virtual address
173 * location (in vmalloc or in kernel).
174 */
175static void
176rpmsg_sg_init(struct scatterlist *sg, void *cpu_addr, unsigned int len)
177{
178 if (is_vmalloc_addr(cpu_addr)) {
179 sg_init_table(sg, 1);
180 sg_set_page(sg, vmalloc_to_page(cpu_addr), len,
181 offset_in_page(cpu_addr));
182 } else {
183 WARN_ON(!virt_addr_valid(cpu_addr));
184 sg_init_one(sg, cpu_addr, len);
185 }
186}
187
5a081caa
OBC
188/**
189 * __ept_release() - deallocate an rpmsg endpoint
190 * @kref: the ept's reference count
191 *
192 * This function deallocates an ept, and is invoked when its @kref refcount
193 * drops to zero.
194 *
195 * Never invoke this function directly!
196 */
197static void __ept_release(struct kref *kref)
198{
199 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
200 refcount);
201 /*
202 * At this point no one holds a reference to ept anymore,
203 * so we can directly free it
204 */
205 kfree(ept);
206}
207
bcabbcca
OBC
208/* for more info, see below documentation of rpmsg_create_ept() */
209static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
92e1de51 210 struct rpmsg_device *rpdev,
0963679c
AS
211 rpmsg_rx_cb_t cb,
212 void *priv, u32 addr)
bcabbcca 213{
d0ffce77 214 int id_min, id_max, id;
bcabbcca
OBC
215 struct rpmsg_endpoint *ept;
216 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
217
bcabbcca 218 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
a8bb3fd9 219 if (!ept)
bcabbcca 220 return NULL;
bcabbcca 221
5a081caa 222 kref_init(&ept->refcount);
15fd943a 223 mutex_init(&ept->cb_lock);
5a081caa 224
bcabbcca
OBC
225 ept->rpdev = rpdev;
226 ept->cb = cb;
227 ept->priv = priv;
8a228ecf 228 ept->ops = &virtio_endpoint_ops;
bcabbcca
OBC
229
230 /* do we need to allocate a local address ? */
d0ffce77
TH
231 if (addr == RPMSG_ADDR_ANY) {
232 id_min = RPMSG_RESERVED_ADDRESSES;
233 id_max = 0;
234 } else {
235 id_min = addr;
236 id_max = addr + 1;
237 }
bcabbcca
OBC
238
239 mutex_lock(&vrp->endpoints_lock);
240
241 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
d0ffce77
TH
242 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
243 if (id < 0) {
244 dev_err(dev, "idr_alloc failed: %d\n", id);
bcabbcca
OBC
245 goto free_ept;
246 }
d0ffce77 247 ept->addr = id;
bcabbcca
OBC
248
249 mutex_unlock(&vrp->endpoints_lock);
250
251 return ept;
252
bcabbcca
OBC
253free_ept:
254 mutex_unlock(&vrp->endpoints_lock);
5a081caa 255 kref_put(&ept->refcount, __ept_release);
bcabbcca
OBC
256 return NULL;
257}
258
1ee1e5e1
AP
259static struct rpmsg_device *virtio_rpmsg_create_channel(struct rpmsg_device *rpdev,
260 struct rpmsg_channel_info *chinfo)
261{
262 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
263 struct virtproc_info *vrp = vch->vrp;
264
265 return __rpmsg_create_channel(vrp, chinfo);
266}
267
268static int virtio_rpmsg_release_channel(struct rpmsg_device *rpdev,
269 struct rpmsg_channel_info *chinfo)
270{
271 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
272 struct virtproc_info *vrp = vch->vrp;
273
274 return rpmsg_unregister_device(&vrp->vdev->dev, chinfo);
275}
276
36b72c7d
BA
277static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev,
278 rpmsg_rx_cb_t cb,
279 void *priv,
280 struct rpmsg_channel_info chinfo)
281{
3bf950ff
BA
282 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
283
284 return __rpmsg_create_ept(vch->vrp, rpdev, cb, priv, chinfo.src);
36b72c7d
BA
285}
286
bcabbcca 287/**
fa2d7795
OBC
288 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
289 * @vrp: virtproc which owns this ept
bcabbcca
OBC
290 * @ept: endpoing to destroy
291 *
fa2d7795
OBC
292 * An internal function which destroy an ept without assuming it is
293 * bound to an rpmsg channel. This is needed for handling the internal
294 * name service endpoint, which isn't bound to an rpmsg channel.
295 * See also __rpmsg_create_ept().
bcabbcca 296 */
fa2d7795
OBC
297static void
298__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
bcabbcca 299{
15fd943a 300 /* make sure new inbound messages can't find this ept anymore */
bcabbcca
OBC
301 mutex_lock(&vrp->endpoints_lock);
302 idr_remove(&vrp->endpoints, ept->addr);
303 mutex_unlock(&vrp->endpoints_lock);
304
15fd943a
OBC
305 /* make sure in-flight inbound messages won't invoke cb anymore */
306 mutex_lock(&ept->cb_lock);
307 ept->cb = NULL;
308 mutex_unlock(&ept->cb_lock);
309
5a081caa 310 kref_put(&ept->refcount, __ept_release);
bcabbcca 311}
fa2d7795 312
8a228ecf
BA
313static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
314{
3bf950ff
BA
315 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(ept->rpdev);
316
317 __rpmsg_destroy_ept(vch->vrp, ept);
8a228ecf
BA
318}
319
36b72c7d
BA
320static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
321{
3bf950ff
BA
322 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
323 struct virtproc_info *vrp = vch->vrp;
36b72c7d
BA
324 struct device *dev = &rpdev->dev;
325 int err = 0;
326
bcabbcca 327 /* need to tell remote processor's name service about this channel ? */
b2599ebf 328 if (rpdev->announce && rpdev->ept &&
0963679c 329 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
bcabbcca
OBC
330 struct rpmsg_ns_msg nsm;
331
332 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
5f2f6b7d
MP
333 nsm.addr = cpu_to_rpmsg32(rpdev, rpdev->ept->addr);
334 nsm.flags = cpu_to_rpmsg32(rpdev, RPMSG_NS_CREATE);
bcabbcca 335
2a48d732 336 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
bcabbcca
OBC
337 if (err)
338 dev_err(dev, "failed to announce service %d\n", err);
339 }
340
bcabbcca
OBC
341 return err;
342}
343
36b72c7d 344static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
bcabbcca 345{
3bf950ff
BA
346 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
347 struct virtproc_info *vrp = vch->vrp;
36b72c7d 348 struct device *dev = &rpdev->dev;
bcabbcca
OBC
349 int err = 0;
350
351 /* tell remote processor's name service we're removing this channel */
b2599ebf 352 if (rpdev->announce && rpdev->ept &&
0963679c 353 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
bcabbcca
OBC
354 struct rpmsg_ns_msg nsm;
355
356 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
5f2f6b7d
MP
357 nsm.addr = cpu_to_rpmsg32(rpdev, rpdev->ept->addr);
358 nsm.flags = cpu_to_rpmsg32(rpdev, RPMSG_NS_DESTROY);
bcabbcca 359
2a48d732 360 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
bcabbcca
OBC
361 if (err)
362 dev_err(dev, "failed to announce service %d\n", err);
363 }
364
36b72c7d
BA
365 return err;
366}
367
36b72c7d 368static const struct rpmsg_device_ops virtio_rpmsg_ops = {
1ee1e5e1
AP
369 .create_channel = virtio_rpmsg_create_channel,
370 .release_channel = virtio_rpmsg_release_channel,
36b72c7d
BA
371 .create_ept = virtio_rpmsg_create_ept,
372 .announce_create = virtio_rpmsg_announce_create,
373 .announce_destroy = virtio_rpmsg_announce_destroy,
374};
375
b0b03b81
BA
376static void virtio_rpmsg_release_device(struct device *dev)
377{
378 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
379 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
380
381 kfree(vch);
382}
383
bcabbcca
OBC
384/*
385 * create an rpmsg channel using its name and address info.
386 * this function will be used to create both static and dynamic
387 * channels.
388 */
77d37298
AP
389static struct rpmsg_device *__rpmsg_create_channel(struct virtproc_info *vrp,
390 struct rpmsg_channel_info *chinfo)
bcabbcca 391{
3bf950ff 392 struct virtio_rpmsg_channel *vch;
92e1de51 393 struct rpmsg_device *rpdev;
bcabbcca
OBC
394 struct device *tmp, *dev = &vrp->vdev->dev;
395 int ret;
396
397 /* make sure a similar channel doesn't already exist */
8b881c07 398 tmp = rpmsg_find_device(dev, chinfo);
bcabbcca
OBC
399 if (tmp) {
400 /* decrement the matched device's refcount back */
401 put_device(tmp);
402 dev_err(dev, "channel %s:%x:%x already exist\n",
403 chinfo->name, chinfo->src, chinfo->dst);
404 return NULL;
405 }
406
3bf950ff
BA
407 vch = kzalloc(sizeof(*vch), GFP_KERNEL);
408 if (!vch)
bcabbcca 409 return NULL;
bcabbcca 410
3bf950ff
BA
411 /* Link the channel to our vrp */
412 vch->vrp = vrp;
413
3bf950ff
BA
414 /* Assign public information to the rpmsg_device */
415 rpdev = &vch->rpdev;
bcabbcca
OBC
416 rpdev->src = chinfo->src;
417 rpdev->dst = chinfo->dst;
36b72c7d 418 rpdev->ops = &virtio_rpmsg_ops;
5f2f6b7d 419 rpdev->little_endian = virtio_is_little_endian(vrp->vdev);
bcabbcca
OBC
420
421 /*
422 * rpmsg server channels has predefined local address (for now),
423 * and their existence needs to be announced remotely
424 */
c8ced113 425 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
bcabbcca
OBC
426
427 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
428
6eed598a 429 rpdev->dev.parent = &vrp->vdev->dev;
b0b03b81 430 rpdev->dev.release = virtio_rpmsg_release_device;
6eed598a
BA
431 ret = rpmsg_register_device(rpdev);
432 if (ret)
433 return NULL;
434
435 return rpdev;
436}
437
bcabbcca
OBC
438/* super simple buffer "allocator" that is just enough for now */
439static void *get_a_tx_buf(struct virtproc_info *vrp)
440{
441 unsigned int len;
442 void *ret;
443
444 /* support multiple concurrent senders */
445 mutex_lock(&vrp->tx_lock);
446
447 /*
448 * either pick the next unused tx buffer
449 * (half of our buffers are used for sending messages)
450 */
b1b98914 451 if (vrp->last_sbuf < vrp->num_bufs / 2)
f93848f9 452 ret = vrp->sbufs + vrp->buf_size * vrp->last_sbuf++;
bcabbcca
OBC
453 /* or recycle a used one */
454 else
455 ret = virtqueue_get_buf(vrp->svq, &len);
456
457 mutex_unlock(&vrp->tx_lock);
458
459 return ret;
460}
461
462/**
463 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
464 * @vrp: virtual remote processor state
465 *
466 * This function is called before a sender is blocked, waiting for
467 * a tx buffer to become available.
468 *
469 * If we already have blocking senders, this function merely increases
470 * the "sleepers" reference count, and exits.
471 *
472 * Otherwise, if this is the first sender to block, we also enable
473 * virtio's tx callbacks, so we'd be immediately notified when a tx
474 * buffer is consumed (we rely on virtio's tx callback in order
475 * to wake up sleeping senders as soon as a tx buffer is used by the
476 * remote processor).
477 */
478static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
479{
480 /* support multiple concurrent senders */
481 mutex_lock(&vrp->tx_lock);
482
483 /* are we the first sleeping context waiting for tx buffers ? */
484 if (atomic_inc_return(&vrp->sleepers) == 1)
485 /* enable "tx-complete" interrupts before dozing off */
486 virtqueue_enable_cb(vrp->svq);
487
488 mutex_unlock(&vrp->tx_lock);
489}
490
491/**
492 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
493 * @vrp: virtual remote processor state
494 *
495 * This function is called after a sender, that waited for a tx buffer
496 * to become available, is unblocked.
497 *
498 * If we still have blocking senders, this function merely decreases
499 * the "sleepers" reference count, and exits.
500 *
501 * Otherwise, if there are no more blocking senders, we also disable
502 * virtio's tx callbacks, to avoid the overhead incurred with handling
503 * those (now redundant) interrupts.
504 */
505static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
506{
507 /* support multiple concurrent senders */
508 mutex_lock(&vrp->tx_lock);
509
510 /* are we the last sleeping context waiting for tx buffers ? */
511 if (atomic_dec_and_test(&vrp->sleepers))
512 /* disable "tx-complete" interrupts */
513 virtqueue_disable_cb(vrp->svq);
514
515 mutex_unlock(&vrp->tx_lock);
516}
517
518/**
519 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
520 * @rpdev: the rpmsg channel
521 * @src: source address
522 * @dst: destination address
523 * @data: payload of message
524 * @len: length of payload
525 * @wait: indicates whether caller should block in case no TX buffers available
526 *
527 * This function is the base implementation for all of the rpmsg sending API.
528 *
529 * It will send @data of length @len to @dst, and say it's from @src. The
530 * message will be sent to the remote processor which the @rpdev channel
531 * belongs to.
532 *
533 * The message is sent using one of the TX buffers that are available for
534 * communication with this remote processor.
535 *
536 * If @wait is true, the caller will be blocked until either a TX buffer is
537 * available, or 15 seconds elapses (we don't want callers to
538 * sleep indefinitely due to misbehaving remote processors), and in that
539 * case -ERESTARTSYS is returned. The number '15' itself was picked
540 * arbitrarily; there's little point in asking drivers to provide a timeout
541 * value themselves.
542 *
543 * Otherwise, if @wait is false, and there are no TX buffers available,
544 * the function will immediately fail, and -ENOMEM will be returned.
545 *
546 * Normally drivers shouldn't use this function directly; instead, drivers
547 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
548 * (see include/linux/rpmsg.h).
549 *
631af6e0 550 * Return: 0 on success and an appropriate error value on failure.
bcabbcca 551 */
8a228ecf
BA
552static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
553 u32 src, u32 dst,
554 void *data, int len, bool wait)
bcabbcca 555{
3bf950ff
BA
556 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
557 struct virtproc_info *vrp = vch->vrp;
bcabbcca
OBC
558 struct device *dev = &rpdev->dev;
559 struct scatterlist sg;
560 struct rpmsg_hdr *msg;
561 int err;
562
563 /* bcasting isn't allowed */
564 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
565 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
566 return -EINVAL;
567 }
568
569 /*
570 * We currently use fixed-sized buffers, and therefore the payload
571 * length is limited.
572 *
573 * One of the possible improvements here is either to support
574 * user-provided buffers (and then we can also support zero-copy
575 * messaging), or to improve the buffer allocator, to support
576 * variable-length buffer sizes.
577 */
f93848f9 578 if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) {
bcabbcca
OBC
579 dev_err(dev, "message is too big (%d)\n", len);
580 return -EMSGSIZE;
581 }
582
583 /* grab a buffer */
584 msg = get_a_tx_buf(vrp);
585 if (!msg && !wait)
586 return -ENOMEM;
587
588 /* no free buffer ? wait for one (but bail after 15 seconds) */
589 while (!msg) {
590 /* enable "tx-complete" interrupts, if not already enabled */
591 rpmsg_upref_sleepers(vrp);
592
593 /*
594 * sleep until a free buffer is available or 15 secs elapse.
595 * the timeout period is not configurable because there's
596 * little point in asking drivers to specify that.
597 * if later this happens to be required, it'd be easy to add.
598 */
599 err = wait_event_interruptible_timeout(vrp->sendq,
600 (msg = get_a_tx_buf(vrp)),
601 msecs_to_jiffies(15000));
602
603 /* disable "tx-complete" interrupts if we're the last sleeper */
604 rpmsg_downref_sleepers(vrp);
605
606 /* timeout ? */
607 if (!err) {
608 dev_err(dev, "timeout waiting for a tx buffer\n");
609 return -ERESTARTSYS;
610 }
611 }
612
5f2f6b7d 613 msg->len = cpu_to_rpmsg16(rpdev, len);
bcabbcca 614 msg->flags = 0;
5f2f6b7d
MP
615 msg->src = cpu_to_rpmsg32(rpdev, src);
616 msg->dst = cpu_to_rpmsg32(rpdev, dst);
bcabbcca
OBC
617 msg->reserved = 0;
618 memcpy(msg->data, data, len);
619
620 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
111d1089 621 src, dst, len, msg->flags, msg->reserved);
211e3a93
AS
622#if defined(CONFIG_DYNAMIC_DEBUG)
623 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
111d1089 624 msg, sizeof(*msg) + len, true);
211e3a93 625#endif
bcabbcca 626
9dd87c2a 627 rpmsg_sg_init(&sg, msg, sizeof(*msg) + len);
bcabbcca
OBC
628
629 mutex_lock(&vrp->tx_lock);
630
631 /* add message to the remote processor's virtqueue */
cee51d69 632 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
57e1a373 633 if (err) {
bcabbcca
OBC
634 /*
635 * need to reclaim the buffer here, otherwise it's lost
636 * (memory won't leak, but rpmsg won't use it again for TX).
637 * this will wait for a buffer management overhaul.
638 */
cee51d69 639 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
bcabbcca
OBC
640 goto out;
641 }
642
643 /* tell the remote processor it has a pending message to read */
644 virtqueue_kick(vrp->svq);
bcabbcca
OBC
645out:
646 mutex_unlock(&vrp->tx_lock);
647 return err;
648}
bcabbcca 649
8a228ecf
BA
650static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
651{
652 struct rpmsg_device *rpdev = ept->rpdev;
653 u32 src = ept->addr, dst = rpdev->dst;
654
655 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
656}
657
658static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
659 u32 dst)
660{
661 struct rpmsg_device *rpdev = ept->rpdev;
662 u32 src = ept->addr;
663
664 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
665}
666
667static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
668 u32 dst, void *data, int len)
669{
670 struct rpmsg_device *rpdev = ept->rpdev;
671
672 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
673}
674
675static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
676{
677 struct rpmsg_device *rpdev = ept->rpdev;
678 u32 src = ept->addr, dst = rpdev->dst;
679
680 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
681}
682
683static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
684 int len, u32 dst)
685{
686 struct rpmsg_device *rpdev = ept->rpdev;
687 u32 src = ept->addr;
688
689 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
690}
691
692static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
693 u32 dst, void *data, int len)
694{
695 struct rpmsg_device *rpdev = ept->rpdev;
696
697 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
698}
699
e279317e
AP
700static ssize_t virtio_rpmsg_get_mtu(struct rpmsg_endpoint *ept)
701{
702 struct rpmsg_device *rpdev = ept->rpdev;
703 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
704
705 return vch->vrp->buf_size - sizeof(struct rpmsg_hdr);
706}
707
1aa7d6a5
RT
708static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
709 struct rpmsg_hdr *msg, unsigned int len)
bcabbcca 710{
bcabbcca
OBC
711 struct rpmsg_endpoint *ept;
712 struct scatterlist sg;
5f2f6b7d
MP
713 bool little_endian = virtio_is_little_endian(vrp->vdev);
714 unsigned int msg_len = __rpmsg16_to_cpu(little_endian, msg->len);
bcabbcca
OBC
715 int err;
716
bcabbcca 717 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
5f2f6b7d
MP
718 __rpmsg32_to_cpu(little_endian, msg->src),
719 __rpmsg32_to_cpu(little_endian, msg->dst), msg_len,
720 __rpmsg16_to_cpu(little_endian, msg->flags),
721 __rpmsg32_to_cpu(little_endian, msg->reserved));
211e3a93
AS
722#if defined(CONFIG_DYNAMIC_DEBUG)
723 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
111d1089 724 msg, sizeof(*msg) + msg_len, true);
211e3a93 725#endif
bcabbcca 726
9648224e
OBC
727 /*
728 * We currently use fixed-sized buffers, so trivially sanitize
729 * the reported payload length.
730 */
f93848f9 731 if (len > vrp->buf_size ||
111d1089
GL
732 msg_len > (len - sizeof(struct rpmsg_hdr))) {
733 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg_len);
1aa7d6a5 734 return -EINVAL;
9648224e
OBC
735 }
736
bcabbcca
OBC
737 /* use the dst addr to fetch the callback of the appropriate user */
738 mutex_lock(&vrp->endpoints_lock);
5a081caa 739
5f2f6b7d 740 ept = idr_find(&vrp->endpoints, __rpmsg32_to_cpu(little_endian, msg->dst));
5a081caa
OBC
741
742 /* let's make sure no one deallocates ept while we use it */
743 if (ept)
744 kref_get(&ept->refcount);
745
bcabbcca
OBC
746 mutex_unlock(&vrp->endpoints_lock);
747
15fd943a
OBC
748 if (ept) {
749 /* make sure ept->cb doesn't go away while we use it */
750 mutex_lock(&ept->cb_lock);
bcabbcca 751
15fd943a 752 if (ept->cb)
111d1089 753 ept->cb(ept->rpdev, msg->data, msg_len, ept->priv,
5f2f6b7d 754 __rpmsg32_to_cpu(little_endian, msg->src));
15fd943a
OBC
755
756 mutex_unlock(&ept->cb_lock);
757
758 /* farewell, ept, we don't need you anymore */
5a081caa 759 kref_put(&ept->refcount, __ept_release);
15fd943a 760 } else
63b8d799 761 dev_warn_ratelimited(dev, "msg received with no recipient\n");
5a081caa 762
f1d9e9c7 763 /* publish the real size of the buffer */
9dd87c2a 764 rpmsg_sg_init(&sg, msg, vrp->buf_size);
bcabbcca
OBC
765
766 /* add the buffer back to the remote processor's virtqueue */
cee51d69 767 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
bcabbcca
OBC
768 if (err < 0) {
769 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
1aa7d6a5
RT
770 return err;
771 }
772
773 return 0;
774}
775
776/* called when an rx buffer is used, and it's time to digest a message */
777static void rpmsg_recv_done(struct virtqueue *rvq)
778{
779 struct virtproc_info *vrp = rvq->vdev->priv;
780 struct device *dev = &rvq->vdev->dev;
781 struct rpmsg_hdr *msg;
782 unsigned int len, msgs_received = 0;
783 int err;
784
785 msg = virtqueue_get_buf(rvq, &len);
786 if (!msg) {
787 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
bcabbcca
OBC
788 return;
789 }
790
1aa7d6a5
RT
791 while (msg) {
792 err = rpmsg_recv_single(vrp, dev, msg, len);
793 if (err)
794 break;
795
796 msgs_received++;
797
798 msg = virtqueue_get_buf(rvq, &len);
6c49fbe3 799 }
1aa7d6a5
RT
800
801 dev_dbg(dev, "Received %u messages\n", msgs_received);
802
bcabbcca 803 /* tell the remote processor we added another available rx buffer */
1aa7d6a5
RT
804 if (msgs_received)
805 virtqueue_kick(vrp->rvq);
bcabbcca
OBC
806}
807
808/*
809 * This is invoked whenever the remote processor completed processing
810 * a TX msg we just sent it, and the buffer is put back to the used ring.
811 *
812 * Normally, though, we suppress this "tx complete" interrupt in order to
813 * avoid the incurred overhead.
814 */
815static void rpmsg_xmit_done(struct virtqueue *svq)
816{
817 struct virtproc_info *vrp = svq->vdev->priv;
818
819 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
820
821 /* wake up potential senders that are waiting for a tx buffer */
822 wake_up_interruptible(&vrp->sendq);
823}
824
c486682a
AP
825/*
826 * Called to expose to user a /dev/rpmsg_ctrlX interface allowing to
827 * create endpoint-to-endpoint communication without associated RPMsg channel.
828 * The endpoints are rattached to the ctrldev RPMsg device.
829 */
830static struct rpmsg_device *rpmsg_virtio_add_ctrl_dev(struct virtio_device *vdev)
831{
832 struct virtproc_info *vrp = vdev->priv;
833 struct virtio_rpmsg_channel *vch;
834 struct rpmsg_device *rpdev_ctrl;
835 int err = 0;
836
837 vch = kzalloc(sizeof(*vch), GFP_KERNEL);
838 if (!vch)
839 return ERR_PTR(-ENOMEM);
840
841 /* Link the channel to the vrp */
842 vch->vrp = vrp;
843
844 /* Assign public information to the rpmsg_device */
845 rpdev_ctrl = &vch->rpdev;
846 rpdev_ctrl->ops = &virtio_rpmsg_ops;
847
848 rpdev_ctrl->dev.parent = &vrp->vdev->dev;
849 rpdev_ctrl->dev.release = virtio_rpmsg_release_device;
850 rpdev_ctrl->little_endian = virtio_is_little_endian(vrp->vdev);
851
472f84ee 852 err = rpmsg_ctrldev_register_device(rpdev_ctrl);
c486682a 853 if (err) {
1680939e 854 /* vch will be free in virtio_rpmsg_release_device() */
c486682a
AP
855 return ERR_PTR(err);
856 }
857
858 return rpdev_ctrl;
859}
860
861static void rpmsg_virtio_del_ctrl_dev(struct rpmsg_device *rpdev_ctrl)
862{
863 if (!rpdev_ctrl)
864 return;
df191796 865 device_unregister(&rpdev_ctrl->dev);
c486682a
AP
866}
867
bcabbcca
OBC
868static int rpmsg_probe(struct virtio_device *vdev)
869{
870 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
f7ad26ff 871 static const char * const names[] = { "input", "output" };
bcabbcca
OBC
872 struct virtqueue *vqs[2];
873 struct virtproc_info *vrp;
c486682a
AP
874 struct virtio_rpmsg_channel *vch = NULL;
875 struct rpmsg_device *rpdev_ns, *rpdev_ctrl;
bcabbcca
OBC
876 void *bufs_va;
877 int err = 0, i;
b1b98914 878 size_t total_buf_space;
71e4b8bf 879 bool notify;
bcabbcca
OBC
880
881 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
882 if (!vrp)
883 return -ENOMEM;
884
885 vrp->vdev = vdev;
886
887 idr_init(&vrp->endpoints);
888 mutex_init(&vrp->endpoints_lock);
889 mutex_init(&vrp->tx_lock);
890 init_waitqueue_head(&vrp->sendq);
891
892 /* We expect two virtqueues, rx and tx (and in this order) */
9b2bbdb2 893 err = virtio_find_vqs(vdev, 2, vqs, vq_cbs, names, NULL);
bcabbcca
OBC
894 if (err)
895 goto free_vrp;
896
897 vrp->rvq = vqs[0];
898 vrp->svq = vqs[1];
899
b1b98914
SA
900 /* we expect symmetric tx/rx vrings */
901 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
902 virtqueue_get_vring_size(vrp->svq));
903
904 /* we need less buffers if vrings are small */
905 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
906 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
907 else
908 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
909
f93848f9
LP
910 vrp->buf_size = MAX_RPMSG_BUF_SIZE;
911
912 total_buf_space = vrp->num_bufs * vrp->buf_size;
b1b98914 913
bcabbcca 914 /* allocate coherent memory for the buffers */
d999b622 915 bufs_va = dma_alloc_coherent(vdev->dev.parent,
b1b98914
SA
916 total_buf_space, &vrp->bufs_dma,
917 GFP_KERNEL);
3119b487
WY
918 if (!bufs_va) {
919 err = -ENOMEM;
bcabbcca 920 goto vqs_del;
3119b487 921 }
bcabbcca 922
de4064af 923 dev_dbg(&vdev->dev, "buffers: va %pK, dma %pad\n",
8d95b322 924 bufs_va, &vrp->bufs_dma);
bcabbcca
OBC
925
926 /* half of the buffers is dedicated for RX */
927 vrp->rbufs = bufs_va;
928
929 /* and half is dedicated for TX */
b1b98914 930 vrp->sbufs = bufs_va + total_buf_space / 2;
bcabbcca
OBC
931
932 /* set up the receive buffers */
b1b98914 933 for (i = 0; i < vrp->num_bufs / 2; i++) {
bcabbcca 934 struct scatterlist sg;
f93848f9 935 void *cpu_addr = vrp->rbufs + i * vrp->buf_size;
bcabbcca 936
9dd87c2a 937 rpmsg_sg_init(&sg, cpu_addr, vrp->buf_size);
bcabbcca 938
cee51d69 939 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
0963679c 940 GFP_KERNEL);
57e1a373 941 WARN_ON(err); /* sanity check; this can't really happen */
bcabbcca
OBC
942 }
943
944 /* suppress "tx-complete" interrupts */
945 virtqueue_disable_cb(vrp->svq);
946
947 vdev->priv = vrp;
948
c486682a
AP
949 rpdev_ctrl = rpmsg_virtio_add_ctrl_dev(vdev);
950 if (IS_ERR(rpdev_ctrl)) {
951 err = PTR_ERR(rpdev_ctrl);
952 goto free_coherent;
953 }
954
bcabbcca
OBC
955 /* if supported by the remote processor, enable the name service */
956 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
950a7388
AP
957 vch = kzalloc(sizeof(*vch), GFP_KERNEL);
958 if (!vch) {
bcabbcca 959 err = -ENOMEM;
c486682a 960 goto free_ctrldev;
bcabbcca 961 }
950a7388
AP
962
963 /* Link the channel to our vrp */
964 vch->vrp = vrp;
965
966 /* Assign public information to the rpmsg_device */
967 rpdev_ns = &vch->rpdev;
968 rpdev_ns->ops = &virtio_rpmsg_ops;
969 rpdev_ns->little_endian = virtio_is_little_endian(vrp->vdev);
970
971 rpdev_ns->dev.parent = &vrp->vdev->dev;
972 rpdev_ns->dev.release = virtio_rpmsg_release_device;
973
974 err = rpmsg_ns_register_device(rpdev_ns);
975 if (err)
c2eecefe
HH
976 /* vch will be free in virtio_rpmsg_release_device() */
977 goto free_ctrldev;
bcabbcca
OBC
978 }
979
71e4b8bf
MT
980 /*
981 * Prepare to kick but don't notify yet - we can't do this before
982 * device is ready.
983 */
984 notify = virtqueue_kick_prepare(vrp->rvq);
985
986 /* From this point on, we can notify and get callbacks. */
987 virtio_device_ready(vdev);
988
bcabbcca 989 /* tell the remote processor it can start sending messages */
71e4b8bf
MT
990 /*
991 * this might be concurrent with callbacks, but we are only
992 * doing notify, not a full kick here, so that's ok.
993 */
994 if (notify)
995 virtqueue_notify(vrp->rvq);
bcabbcca
OBC
996
997 dev_info(&vdev->dev, "rpmsg host is online\n");
998
999 return 0;
1000
c486682a
AP
1001free_ctrldev:
1002 rpmsg_virtio_del_ctrl_dev(rpdev_ctrl);
1003free_coherent:
d999b622 1004 dma_free_coherent(vdev->dev.parent, total_buf_space,
b1b98914 1005 bufs_va, vrp->bufs_dma);
bcabbcca
OBC
1006vqs_del:
1007 vdev->config->del_vqs(vrp->vdev);
1008free_vrp:
1009 kfree(vrp);
1010 return err;
1011}
1012
1013static int rpmsg_remove_device(struct device *dev, void *data)
1014{
1015 device_unregister(dev);
1016
1017 return 0;
1018}
1019
0fe763c5 1020static void rpmsg_remove(struct virtio_device *vdev)
bcabbcca
OBC
1021{
1022 struct virtproc_info *vrp = vdev->priv;
f93848f9 1023 size_t total_buf_space = vrp->num_bufs * vrp->buf_size;
bcabbcca
OBC
1024 int ret;
1025
d9679d00 1026 virtio_reset_device(vdev);
bcabbcca
OBC
1027
1028 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
1029 if (ret)
1030 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
1031
bcabbcca
OBC
1032 idr_destroy(&vrp->endpoints);
1033
1034 vdev->config->del_vqs(vrp->vdev);
1035
d999b622 1036 dma_free_coherent(vdev->dev.parent, total_buf_space,
b1b98914 1037 vrp->rbufs, vrp->bufs_dma);
bcabbcca
OBC
1038
1039 kfree(vrp);
1040}
1041
1042static struct virtio_device_id id_table[] = {
1043 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1044 { 0 },
1045};
1046
1047static unsigned int features[] = {
1048 VIRTIO_RPMSG_F_NS,
1049};
1050
1051static struct virtio_driver virtio_ipc_driver = {
1052 .feature_table = features,
1053 .feature_table_size = ARRAY_SIZE(features),
1054 .driver.name = KBUILD_MODNAME,
1055 .driver.owner = THIS_MODULE,
1056 .id_table = id_table,
1057 .probe = rpmsg_probe,
0fe763c5 1058 .remove = rpmsg_remove,
bcabbcca
OBC
1059};
1060
1061static int __init rpmsg_init(void)
1062{
1063 int ret;
1064
bcabbcca 1065 ret = register_virtio_driver(&virtio_ipc_driver);
5e619b48 1066 if (ret)
bcabbcca 1067 pr_err("failed to register virtio driver: %d\n", ret);
bcabbcca
OBC
1068
1069 return ret;
1070}
96342526 1071subsys_initcall(rpmsg_init);
bcabbcca
OBC
1072
1073static void __exit rpmsg_fini(void)
1074{
1075 unregister_virtio_driver(&virtio_ipc_driver);
bcabbcca
OBC
1076}
1077module_exit(rpmsg_fini);
1078
1079MODULE_DEVICE_TABLE(virtio, id_table);
1080MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1081MODULE_LICENSE("GPL v2");