blob: ac22412421882598950dc814bea3ec7a309affaa [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 Andersson3bf950f2016-09-01 15:28:06 -070078/**
79 * @vrp: the remote processor this channel belongs to
80 */
81struct virtio_rpmsg_channel {
82 struct rpmsg_device rpdev;
83
84 struct virtproc_info *vrp;
85};
86
87#define to_virtio_rpmsg_channel(_rpdev) \
88 container_of(_rpdev, struct virtio_rpmsg_channel, rpdev)
89
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020090/*
Suman Annab1b98912014-09-16 13:33:07 -050091 * We're allocating buffers of 512 bytes each for communications. The
92 * number of buffers will be computed from the number of buffers supported
93 * by the vring, upto a maximum of 512 buffers (256 in each direction).
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020094 *
95 * Each buffer will have 16 bytes for the msg header and 496 bytes for
96 * the payload.
97 *
Suman Annab1b98912014-09-16 13:33:07 -050098 * This will utilize a maximum total space of 256KB for the buffers.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020099 *
100 * We might also want to add support for user-provided buffers in time.
101 * This will allow bigger buffer size flexibility, and can also be used
102 * to achieve zero-copy messaging.
103 *
104 * Note that these numbers are purely a decision of this driver - we
105 * can change this without changing anything in the firmware of the remote
106 * processor.
107 */
Suman Annab1b98912014-09-16 13:33:07 -0500108#define MAX_RPMSG_NUM_BUFS (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200109#define RPMSG_BUF_SIZE (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200110
111/*
112 * Local addresses are dynamically allocated on-demand.
113 * We do not dynamically assign addresses from the low 1024 range,
114 * in order to reserve that address range for predefined services.
115 */
116#define RPMSG_RESERVED_ADDRESSES (1024)
117
118/* Address 53 is reserved for advertising remote services */
119#define RPMSG_NS_ADDR (53)
120
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700121static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
122static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
123static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
124 u32 dst);
125static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
126 u32 dst, void *data, int len);
127static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
128static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
129 int len, u32 dst);
130static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
131 u32 dst, void *data, int len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200132
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700133static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
134 .destroy_ept = virtio_rpmsg_destroy_ept,
135 .send = virtio_rpmsg_send,
136 .sendto = virtio_rpmsg_sendto,
137 .send_offchannel = virtio_rpmsg_send_offchannel,
138 .trysend = virtio_rpmsg_trysend,
139 .trysendto = virtio_rpmsg_trysendto,
140 .trysend_offchannel = virtio_rpmsg_trysend_offchannel,
141};
142
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300143/**
144 * __ept_release() - deallocate an rpmsg endpoint
145 * @kref: the ept's reference count
146 *
147 * This function deallocates an ept, and is invoked when its @kref refcount
148 * drops to zero.
149 *
150 * Never invoke this function directly!
151 */
152static void __ept_release(struct kref *kref)
153{
154 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
155 refcount);
156 /*
157 * At this point no one holds a reference to ept anymore,
158 * so we can directly free it
159 */
160 kfree(ept);
161}
162
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200163/* for more info, see below documentation of rpmsg_create_ept() */
164static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700165 struct rpmsg_device *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500166 rpmsg_rx_cb_t cb,
167 void *priv, u32 addr)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200168{
Tejun Heod0ffce72013-02-27 17:04:40 -0800169 int id_min, id_max, id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200170 struct rpmsg_endpoint *ept;
171 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
172
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200173 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500174 if (!ept)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200175 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200176
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300177 kref_init(&ept->refcount);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300178 mutex_init(&ept->cb_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300179
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200180 ept->rpdev = rpdev;
181 ept->cb = cb;
182 ept->priv = priv;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700183 ept->ops = &virtio_endpoint_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200184
185 /* do we need to allocate a local address ? */
Tejun Heod0ffce72013-02-27 17:04:40 -0800186 if (addr == RPMSG_ADDR_ANY) {
187 id_min = RPMSG_RESERVED_ADDRESSES;
188 id_max = 0;
189 } else {
190 id_min = addr;
191 id_max = addr + 1;
192 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200193
194 mutex_lock(&vrp->endpoints_lock);
195
196 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
Tejun Heod0ffce72013-02-27 17:04:40 -0800197 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
198 if (id < 0) {
199 dev_err(dev, "idr_alloc failed: %d\n", id);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200200 goto free_ept;
201 }
Tejun Heod0ffce72013-02-27 17:04:40 -0800202 ept->addr = id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200203
204 mutex_unlock(&vrp->endpoints_lock);
205
206 return ept;
207
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200208free_ept:
209 mutex_unlock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300210 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200211 return NULL;
212}
213
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700214static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev,
215 rpmsg_rx_cb_t cb,
216 void *priv,
217 struct rpmsg_channel_info chinfo)
218{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700219 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
220
221 return __rpmsg_create_ept(vch->vrp, rpdev, cb, priv, chinfo.src);
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700222}
223
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200224/**
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200225 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
226 * @vrp: virtproc which owns this ept
227 * @ept: endpoing to destroy
228 *
229 * An internal function which destroy an ept without assuming it is
230 * bound to an rpmsg channel. This is needed for handling the internal
231 * name service endpoint, which isn't bound to an rpmsg channel.
232 * See also __rpmsg_create_ept().
233 */
234static void
235__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
236{
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300237 /* make sure new inbound messages can't find this ept anymore */
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200238 mutex_lock(&vrp->endpoints_lock);
239 idr_remove(&vrp->endpoints, ept->addr);
240 mutex_unlock(&vrp->endpoints_lock);
241
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300242 /* make sure in-flight inbound messages won't invoke cb anymore */
243 mutex_lock(&ept->cb_lock);
244 ept->cb = NULL;
245 mutex_unlock(&ept->cb_lock);
246
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300247 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200248}
249
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700250static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
251{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700252 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(ept->rpdev);
253
254 __rpmsg_destroy_ept(vch->vrp, ept);
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700255}
256
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700257static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
258{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700259 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
260 struct virtproc_info *vrp = vch->vrp;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700261 struct device *dev = &rpdev->dev;
262 int err = 0;
263
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200264 /* need to tell remote processor's name service about this channel ? */
265 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500266 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200267 struct rpmsg_ns_msg nsm;
268
269 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700270 nsm.addr = rpdev->ept->addr;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200271 nsm.flags = RPMSG_NS_CREATE;
272
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700273 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200274 if (err)
275 dev_err(dev, "failed to announce service %d\n", err);
276 }
277
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200278 return err;
279}
280
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700281static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200282{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700283 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
284 struct virtproc_info *vrp = vch->vrp;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700285 struct device *dev = &rpdev->dev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200286 int err = 0;
287
288 /* tell remote processor's name service we're removing this channel */
289 if (rpdev->announce &&
Anna, Suman09636792016-08-12 18:42:26 -0500290 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200291 struct rpmsg_ns_msg nsm;
292
293 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
294 nsm.addr = rpdev->src;
295 nsm.flags = RPMSG_NS_DESTROY;
296
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700297 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200298 if (err)
299 dev_err(dev, "failed to announce service %d\n", err);
300 }
301
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700302 return err;
303}
304
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700305static const struct rpmsg_device_ops virtio_rpmsg_ops = {
306 .create_ept = virtio_rpmsg_create_ept,
307 .announce_create = virtio_rpmsg_announce_create,
308 .announce_destroy = virtio_rpmsg_announce_destroy,
309};
310
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200311/*
312 * create an rpmsg channel using its name and address info.
313 * this function will be used to create both static and dynamic
314 * channels.
315 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700316static struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp,
317 struct rpmsg_channel_info *chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200318{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700319 struct virtio_rpmsg_channel *vch;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700320 struct rpmsg_device *rpdev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200321 struct device *tmp, *dev = &vrp->vdev->dev;
322 int ret;
323
324 /* make sure a similar channel doesn't already exist */
Bjorn Andersson8b881c02016-09-01 15:28:02 -0700325 tmp = rpmsg_find_device(dev, chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200326 if (tmp) {
327 /* decrement the matched device's refcount back */
328 put_device(tmp);
329 dev_err(dev, "channel %s:%x:%x already exist\n",
330 chinfo->name, chinfo->src, chinfo->dst);
331 return NULL;
332 }
333
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700334 vch = kzalloc(sizeof(*vch), GFP_KERNEL);
335 if (!vch)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200336 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200337
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700338 /* Link the channel to our vrp */
339 vch->vrp = vrp;
340
341 /* Assign callbacks for rpmsg_channel */
342 vch->rpdev.ops = &virtio_rpmsg_ops;
343
344 /* Assign public information to the rpmsg_device */
345 rpdev = &vch->rpdev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200346 rpdev->src = chinfo->src;
347 rpdev->dst = chinfo->dst;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700348 rpdev->ops = &virtio_rpmsg_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200349
350 /*
351 * rpmsg server channels has predefined local address (for now),
352 * and their existence needs to be announced remotely
353 */
Andrew F. Davisc8ced112016-07-01 09:24:58 -0500354 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200355
356 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
357
Bjorn Andersson6eed5982016-09-01 15:28:03 -0700358 rpdev->dev.parent = &vrp->vdev->dev;
359 ret = rpmsg_register_device(rpdev);
360 if (ret)
361 return NULL;
362
363 return rpdev;
364}
365
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200366/* super simple buffer "allocator" that is just enough for now */
367static void *get_a_tx_buf(struct virtproc_info *vrp)
368{
369 unsigned int len;
370 void *ret;
371
372 /* support multiple concurrent senders */
373 mutex_lock(&vrp->tx_lock);
374
375 /*
376 * either pick the next unused tx buffer
377 * (half of our buffers are used for sending messages)
378 */
Suman Annab1b98912014-09-16 13:33:07 -0500379 if (vrp->last_sbuf < vrp->num_bufs / 2)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200380 ret = vrp->sbufs + RPMSG_BUF_SIZE * vrp->last_sbuf++;
381 /* or recycle a used one */
382 else
383 ret = virtqueue_get_buf(vrp->svq, &len);
384
385 mutex_unlock(&vrp->tx_lock);
386
387 return ret;
388}
389
390/**
391 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
392 * @vrp: virtual remote processor state
393 *
394 * This function is called before a sender is blocked, waiting for
395 * a tx buffer to become available.
396 *
397 * If we already have blocking senders, this function merely increases
398 * the "sleepers" reference count, and exits.
399 *
400 * Otherwise, if this is the first sender to block, we also enable
401 * virtio's tx callbacks, so we'd be immediately notified when a tx
402 * buffer is consumed (we rely on virtio's tx callback in order
403 * to wake up sleeping senders as soon as a tx buffer is used by the
404 * remote processor).
405 */
406static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
407{
408 /* support multiple concurrent senders */
409 mutex_lock(&vrp->tx_lock);
410
411 /* are we the first sleeping context waiting for tx buffers ? */
412 if (atomic_inc_return(&vrp->sleepers) == 1)
413 /* enable "tx-complete" interrupts before dozing off */
414 virtqueue_enable_cb(vrp->svq);
415
416 mutex_unlock(&vrp->tx_lock);
417}
418
419/**
420 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
421 * @vrp: virtual remote processor state
422 *
423 * This function is called after a sender, that waited for a tx buffer
424 * to become available, is unblocked.
425 *
426 * If we still have blocking senders, this function merely decreases
427 * the "sleepers" reference count, and exits.
428 *
429 * Otherwise, if there are no more blocking senders, we also disable
430 * virtio's tx callbacks, to avoid the overhead incurred with handling
431 * those (now redundant) interrupts.
432 */
433static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
434{
435 /* support multiple concurrent senders */
436 mutex_lock(&vrp->tx_lock);
437
438 /* are we the last sleeping context waiting for tx buffers ? */
439 if (atomic_dec_and_test(&vrp->sleepers))
440 /* disable "tx-complete" interrupts */
441 virtqueue_disable_cb(vrp->svq);
442
443 mutex_unlock(&vrp->tx_lock);
444}
445
446/**
447 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
448 * @rpdev: the rpmsg channel
449 * @src: source address
450 * @dst: destination address
451 * @data: payload of message
452 * @len: length of payload
453 * @wait: indicates whether caller should block in case no TX buffers available
454 *
455 * This function is the base implementation for all of the rpmsg sending API.
456 *
457 * It will send @data of length @len to @dst, and say it's from @src. The
458 * message will be sent to the remote processor which the @rpdev channel
459 * belongs to.
460 *
461 * The message is sent using one of the TX buffers that are available for
462 * communication with this remote processor.
463 *
464 * If @wait is true, the caller will be blocked until either a TX buffer is
465 * available, or 15 seconds elapses (we don't want callers to
466 * sleep indefinitely due to misbehaving remote processors), and in that
467 * case -ERESTARTSYS is returned. The number '15' itself was picked
468 * arbitrarily; there's little point in asking drivers to provide a timeout
469 * value themselves.
470 *
471 * Otherwise, if @wait is false, and there are no TX buffers available,
472 * the function will immediately fail, and -ENOMEM will be returned.
473 *
474 * Normally drivers shouldn't use this function directly; instead, drivers
475 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
476 * (see include/linux/rpmsg.h).
477 *
478 * Returns 0 on success and an appropriate error value on failure.
479 */
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700480static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
481 u32 src, u32 dst,
482 void *data, int len, bool wait)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200483{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700484 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
485 struct virtproc_info *vrp = vch->vrp;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200486 struct device *dev = &rpdev->dev;
487 struct scatterlist sg;
488 struct rpmsg_hdr *msg;
489 int err;
490
491 /* bcasting isn't allowed */
492 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
493 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
494 return -EINVAL;
495 }
496
497 /*
498 * We currently use fixed-sized buffers, and therefore the payload
499 * length is limited.
500 *
501 * One of the possible improvements here is either to support
502 * user-provided buffers (and then we can also support zero-copy
503 * messaging), or to improve the buffer allocator, to support
504 * variable-length buffer sizes.
505 */
506 if (len > RPMSG_BUF_SIZE - sizeof(struct rpmsg_hdr)) {
507 dev_err(dev, "message is too big (%d)\n", len);
508 return -EMSGSIZE;
509 }
510
511 /* grab a buffer */
512 msg = get_a_tx_buf(vrp);
513 if (!msg && !wait)
514 return -ENOMEM;
515
516 /* no free buffer ? wait for one (but bail after 15 seconds) */
517 while (!msg) {
518 /* enable "tx-complete" interrupts, if not already enabled */
519 rpmsg_upref_sleepers(vrp);
520
521 /*
522 * sleep until a free buffer is available or 15 secs elapse.
523 * the timeout period is not configurable because there's
524 * little point in asking drivers to specify that.
525 * if later this happens to be required, it'd be easy to add.
526 */
527 err = wait_event_interruptible_timeout(vrp->sendq,
528 (msg = get_a_tx_buf(vrp)),
529 msecs_to_jiffies(15000));
530
531 /* disable "tx-complete" interrupts if we're the last sleeper */
532 rpmsg_downref_sleepers(vrp);
533
534 /* timeout ? */
535 if (!err) {
536 dev_err(dev, "timeout waiting for a tx buffer\n");
537 return -ERESTARTSYS;
538 }
539 }
540
541 msg->len = len;
542 msg->flags = 0;
543 msg->src = src;
544 msg->dst = dst;
545 msg->reserved = 0;
546 memcpy(msg->data, data, len);
547
548 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500549 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500550#if defined(CONFIG_DYNAMIC_DEBUG)
551 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
552 msg, sizeof(*msg) + msg->len, true);
553#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200554
555 sg_init_one(&sg, msg, sizeof(*msg) + len);
556
557 mutex_lock(&vrp->tx_lock);
558
559 /* add message to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030560 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030561 if (err) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200562 /*
563 * need to reclaim the buffer here, otherwise it's lost
564 * (memory won't leak, but rpmsg won't use it again for TX).
565 * this will wait for a buffer management overhaul.
566 */
Rusty Russellcee51d62013-03-20 15:44:29 +1030567 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200568 goto out;
569 }
570
571 /* tell the remote processor it has a pending message to read */
572 virtqueue_kick(vrp->svq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200573out:
574 mutex_unlock(&vrp->tx_lock);
575 return err;
576}
577EXPORT_SYMBOL(rpmsg_send_offchannel_raw);
578
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700579static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
580{
581 struct rpmsg_device *rpdev = ept->rpdev;
582 u32 src = ept->addr, dst = rpdev->dst;
583
584 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
585}
586
587static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
588 u32 dst)
589{
590 struct rpmsg_device *rpdev = ept->rpdev;
591 u32 src = ept->addr;
592
593 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
594}
595
596static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
597 u32 dst, void *data, int len)
598{
599 struct rpmsg_device *rpdev = ept->rpdev;
600
601 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
602}
603
604static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
605{
606 struct rpmsg_device *rpdev = ept->rpdev;
607 u32 src = ept->addr, dst = rpdev->dst;
608
609 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
610}
611
612static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
613 int len, u32 dst)
614{
615 struct rpmsg_device *rpdev = ept->rpdev;
616 u32 src = ept->addr;
617
618 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
619}
620
621static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
622 u32 dst, void *data, int len)
623{
624 struct rpmsg_device *rpdev = ept->rpdev;
625
626 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
627}
628
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300629static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
630 struct rpmsg_hdr *msg, unsigned int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200631{
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200632 struct rpmsg_endpoint *ept;
633 struct scatterlist sg;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200634 int err;
635
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200636 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500637 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500638#if defined(CONFIG_DYNAMIC_DEBUG)
639 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
640 msg, sizeof(*msg) + msg->len, true);
641#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200642
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200643 /*
644 * We currently use fixed-sized buffers, so trivially sanitize
645 * the reported payload length.
646 */
647 if (len > RPMSG_BUF_SIZE ||
Anna, Suman09636792016-08-12 18:42:26 -0500648 msg->len > (len - sizeof(struct rpmsg_hdr))) {
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200649 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300650 return -EINVAL;
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200651 }
652
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200653 /* use the dst addr to fetch the callback of the appropriate user */
654 mutex_lock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300655
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200656 ept = idr_find(&vrp->endpoints, msg->dst);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300657
658 /* let's make sure no one deallocates ept while we use it */
659 if (ept)
660 kref_get(&ept->refcount);
661
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200662 mutex_unlock(&vrp->endpoints_lock);
663
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300664 if (ept) {
665 /* make sure ept->cb doesn't go away while we use it */
666 mutex_lock(&ept->cb_lock);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200667
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300668 if (ept->cb)
669 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
670 msg->src);
671
672 mutex_unlock(&ept->cb_lock);
673
674 /* farewell, ept, we don't need you anymore */
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300675 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300676 } else
Masanari Iida8a168ca2012-12-29 02:00:09 +0900677 dev_warn(dev, "msg received with no recipient\n");
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300678
Ohad Ben-Cohenf1d9e9c2012-02-28 16:11:28 +0200679 /* publish the real size of the buffer */
680 sg_init_one(&sg, msg, RPMSG_BUF_SIZE);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200681
682 /* add the buffer back to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030683 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200684 if (err < 0) {
685 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300686 return err;
687 }
688
689 return 0;
690}
691
692/* called when an rx buffer is used, and it's time to digest a message */
693static void rpmsg_recv_done(struct virtqueue *rvq)
694{
695 struct virtproc_info *vrp = rvq->vdev->priv;
696 struct device *dev = &rvq->vdev->dev;
697 struct rpmsg_hdr *msg;
698 unsigned int len, msgs_received = 0;
699 int err;
700
701 msg = virtqueue_get_buf(rvq, &len);
702 if (!msg) {
703 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200704 return;
705 }
706
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300707 while (msg) {
708 err = rpmsg_recv_single(vrp, dev, msg, len);
709 if (err)
710 break;
711
712 msgs_received++;
713
714 msg = virtqueue_get_buf(rvq, &len);
Lee Jones6c49fbe2016-07-20 10:29:35 +0100715 }
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300716
717 dev_dbg(dev, "Received %u messages\n", msgs_received);
718
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200719 /* tell the remote processor we added another available rx buffer */
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300720 if (msgs_received)
721 virtqueue_kick(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200722}
723
724/*
725 * This is invoked whenever the remote processor completed processing
726 * a TX msg we just sent it, and the buffer is put back to the used ring.
727 *
728 * Normally, though, we suppress this "tx complete" interrupt in order to
729 * avoid the incurred overhead.
730 */
731static void rpmsg_xmit_done(struct virtqueue *svq)
732{
733 struct virtproc_info *vrp = svq->vdev->priv;
734
735 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
736
737 /* wake up potential senders that are waiting for a tx buffer */
738 wake_up_interruptible(&vrp->sendq);
739}
740
741/* invoked when a name service announcement arrives */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700742static void rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
Anna, Suman09636792016-08-12 18:42:26 -0500743 void *priv, u32 src)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200744{
745 struct rpmsg_ns_msg *msg = data;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700746 struct rpmsg_device *newch;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200747 struct rpmsg_channel_info chinfo;
748 struct virtproc_info *vrp = priv;
749 struct device *dev = &vrp->vdev->dev;
750 int ret;
751
Anna, Suman211e3a92016-08-12 18:42:27 -0500752#if defined(CONFIG_DYNAMIC_DEBUG)
753 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
754 data, len, true);
755#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200756
757 if (len != sizeof(*msg)) {
758 dev_err(dev, "malformed ns msg (%d)\n", len);
759 return;
760 }
761
762 /*
763 * the name service ept does _not_ belong to a real rpmsg channel,
764 * and is handled by the rpmsg bus itself.
765 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
766 * in somehow.
767 */
768 if (rpdev) {
769 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
770 return;
771 }
772
773 /* don't trust the remote processor for null terminating the name */
774 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
775
776 dev_info(dev, "%sing channel %s addr 0x%x\n",
Anna, Suman09636792016-08-12 18:42:26 -0500777 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
778 msg->name, msg->addr);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200779
780 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
781 chinfo.src = RPMSG_ADDR_ANY;
782 chinfo.dst = msg->addr;
783
784 if (msg->flags & RPMSG_NS_DESTROY) {
Bjorn Andersson5e619b42016-09-01 15:28:04 -0700785 ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200786 if (ret)
787 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
788 } else {
789 newch = rpmsg_create_channel(vrp, &chinfo);
790 if (!newch)
791 dev_err(dev, "rpmsg_create_channel failed\n");
792 }
793}
794
795static int rpmsg_probe(struct virtio_device *vdev)
796{
797 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800798 static const char * const names[] = { "input", "output" };
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200799 struct virtqueue *vqs[2];
800 struct virtproc_info *vrp;
801 void *bufs_va;
802 int err = 0, i;
Suman Annab1b98912014-09-16 13:33:07 -0500803 size_t total_buf_space;
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030804 bool notify;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200805
806 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
807 if (!vrp)
808 return -ENOMEM;
809
810 vrp->vdev = vdev;
811
812 idr_init(&vrp->endpoints);
813 mutex_init(&vrp->endpoints_lock);
814 mutex_init(&vrp->tx_lock);
815 init_waitqueue_head(&vrp->sendq);
816
817 /* We expect two virtqueues, rx and tx (and in this order) */
818 err = vdev->config->find_vqs(vdev, 2, vqs, vq_cbs, names);
819 if (err)
820 goto free_vrp;
821
822 vrp->rvq = vqs[0];
823 vrp->svq = vqs[1];
824
Suman Annab1b98912014-09-16 13:33:07 -0500825 /* we expect symmetric tx/rx vrings */
826 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
827 virtqueue_get_vring_size(vrp->svq));
828
829 /* we need less buffers if vrings are small */
830 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
831 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
832 else
833 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
834
835 total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
836
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200837 /* allocate coherent memory for the buffers */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300838 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
Suman Annab1b98912014-09-16 13:33:07 -0500839 total_buf_space, &vrp->bufs_dma,
840 GFP_KERNEL);
Wei Yongjun3119b482013-04-29 16:17:09 -0700841 if (!bufs_va) {
842 err = -ENOMEM;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200843 goto vqs_del;
Wei Yongjun3119b482013-04-29 16:17:09 -0700844 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200845
Anna, Suman8d95b322016-08-12 18:42:25 -0500846 dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
847 bufs_va, &vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200848
849 /* half of the buffers is dedicated for RX */
850 vrp->rbufs = bufs_va;
851
852 /* and half is dedicated for TX */
Suman Annab1b98912014-09-16 13:33:07 -0500853 vrp->sbufs = bufs_va + total_buf_space / 2;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200854
855 /* set up the receive buffers */
Suman Annab1b98912014-09-16 13:33:07 -0500856 for (i = 0; i < vrp->num_bufs / 2; i++) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200857 struct scatterlist sg;
858 void *cpu_addr = vrp->rbufs + i * RPMSG_BUF_SIZE;
859
860 sg_init_one(&sg, cpu_addr, RPMSG_BUF_SIZE);
861
Rusty Russellcee51d62013-03-20 15:44:29 +1030862 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
Anna, Suman09636792016-08-12 18:42:26 -0500863 GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030864 WARN_ON(err); /* sanity check; this can't really happen */
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200865 }
866
867 /* suppress "tx-complete" interrupts */
868 virtqueue_disable_cb(vrp->svq);
869
870 vdev->priv = vrp;
871
872 /* if supported by the remote processor, enable the name service */
873 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
874 /* a dedicated endpoint handles the name service msgs */
875 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
876 vrp, RPMSG_NS_ADDR);
877 if (!vrp->ns_ept) {
878 dev_err(&vdev->dev, "failed to create the ns ept\n");
879 err = -ENOMEM;
880 goto free_coherent;
881 }
882 }
883
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030884 /*
885 * Prepare to kick but don't notify yet - we can't do this before
886 * device is ready.
887 */
888 notify = virtqueue_kick_prepare(vrp->rvq);
889
890 /* From this point on, we can notify and get callbacks. */
891 virtio_device_ready(vdev);
892
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200893 /* tell the remote processor it can start sending messages */
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030894 /*
895 * this might be concurrent with callbacks, but we are only
896 * doing notify, not a full kick here, so that's ok.
897 */
898 if (notify)
899 virtqueue_notify(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200900
901 dev_info(&vdev->dev, "rpmsg host is online\n");
902
903 return 0;
904
905free_coherent:
Suman Annab1b98912014-09-16 13:33:07 -0500906 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
907 bufs_va, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200908vqs_del:
909 vdev->config->del_vqs(vrp->vdev);
910free_vrp:
911 kfree(vrp);
912 return err;
913}
914
915static int rpmsg_remove_device(struct device *dev, void *data)
916{
917 device_unregister(dev);
918
919 return 0;
920}
921
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800922static void rpmsg_remove(struct virtio_device *vdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200923{
924 struct virtproc_info *vrp = vdev->priv;
Suman Annab1b98912014-09-16 13:33:07 -0500925 size_t total_buf_space = vrp->num_bufs * RPMSG_BUF_SIZE;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200926 int ret;
927
928 vdev->config->reset(vdev);
929
930 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
931 if (ret)
932 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
933
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200934 if (vrp->ns_ept)
935 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
936
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200937 idr_destroy(&vrp->endpoints);
938
939 vdev->config->del_vqs(vrp->vdev);
940
Suman Annab1b98912014-09-16 13:33:07 -0500941 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
942 vrp->rbufs, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200943
944 kfree(vrp);
945}
946
947static struct virtio_device_id id_table[] = {
948 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
949 { 0 },
950};
951
952static unsigned int features[] = {
953 VIRTIO_RPMSG_F_NS,
954};
955
956static struct virtio_driver virtio_ipc_driver = {
957 .feature_table = features,
958 .feature_table_size = ARRAY_SIZE(features),
959 .driver.name = KBUILD_MODNAME,
960 .driver.owner = THIS_MODULE,
961 .id_table = id_table,
962 .probe = rpmsg_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800963 .remove = rpmsg_remove,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200964};
965
966static int __init rpmsg_init(void)
967{
968 int ret;
969
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200970 ret = register_virtio_driver(&virtio_ipc_driver);
Bjorn Andersson5e619b42016-09-01 15:28:04 -0700971 if (ret)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200972 pr_err("failed to register virtio driver: %d\n", ret);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200973
974 return ret;
975}
Federico Fuga96342522012-07-16 10:36:51 +0300976subsys_initcall(rpmsg_init);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200977
978static void __exit rpmsg_fini(void)
979{
980 unregister_virtio_driver(&virtio_ipc_driver);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200981}
982module_exit(rpmsg_fini);
983
984MODULE_DEVICE_TABLE(virtio, id_table);
985MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
986MODULE_LICENSE("GPL v2");