blob: fd7a033485cfacca9e6e3139e7b1527141b14243 [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
Loic Pallardyf93848f2017-03-28 13:49:43 +020048 * @buf_size: size of one rx or tx buffer
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020049 * @last_sbuf: index of last tx buffer used
50 * @bufs_dma: dma base addr of the buffers
51 * @tx_lock: protects svq, sbufs and sleepers, to allow concurrent senders.
52 * sending a message might require waking up a dozing remote
53 * processor, which involves sleeping, hence the mutex.
54 * @endpoints: idr of local endpoints, allows fast retrieval
55 * @endpoints_lock: lock of the endpoints set
56 * @sendq: wait queue of sending contexts waiting for a tx buffers
57 * @sleepers: number of senders that are waiting for a tx buffer
58 * @ns_ept: the bus's name service endpoint
59 *
60 * This structure stores the rpmsg state of a given virtio remote processor
61 * device (there might be several virtio proc devices for each physical
62 * remote processor).
63 */
64struct virtproc_info {
65 struct virtio_device *vdev;
66 struct virtqueue *rvq, *svq;
67 void *rbufs, *sbufs;
Suman Annab1b98912014-09-16 13:33:07 -050068 unsigned int num_bufs;
Loic Pallardyf93848f2017-03-28 13:49:43 +020069 unsigned int buf_size;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +020070 int last_sbuf;
71 dma_addr_t bufs_dma;
72 struct mutex tx_lock;
73 struct idr endpoints;
74 struct mutex endpoints_lock;
75 wait_queue_head_t sendq;
76 atomic_t sleepers;
77 struct rpmsg_endpoint *ns_ept;
78};
79
Bjorn Anderssone88dae52016-09-01 15:28:07 -070080/* The feature bitmap for virtio rpmsg */
81#define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
82
83/**
84 * struct rpmsg_hdr - common header for all rpmsg messages
85 * @src: source address
86 * @dst: destination address
87 * @reserved: reserved for future use
88 * @len: length of payload (in bytes)
89 * @flags: message flags
90 * @data: @len bytes of message payload data
91 *
92 * Every message sent(/received) on the rpmsg bus begins with this header.
93 */
94struct rpmsg_hdr {
95 u32 src;
96 u32 dst;
97 u32 reserved;
98 u16 len;
99 u16 flags;
100 u8 data[0];
101} __packed;
102
103/**
104 * struct rpmsg_ns_msg - dynamic name service announcement message
105 * @name: name of remote service that is published
106 * @addr: address of remote service that is published
107 * @flags: indicates whether service is created or destroyed
108 *
109 * This message is sent across to publish a new service, or announce
110 * about its removal. When we receive these messages, an appropriate
111 * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
112 * or ->remove() handler of the appropriate rpmsg driver will be invoked
113 * (if/as-soon-as one is registered).
114 */
115struct rpmsg_ns_msg {
116 char name[RPMSG_NAME_SIZE];
117 u32 addr;
118 u32 flags;
119} __packed;
120
121/**
122 * enum rpmsg_ns_flags - dynamic name service announcement flags
123 *
124 * @RPMSG_NS_CREATE: a new remote service was just created
125 * @RPMSG_NS_DESTROY: a known remote service was just destroyed
126 */
127enum rpmsg_ns_flags {
128 RPMSG_NS_CREATE = 0,
129 RPMSG_NS_DESTROY = 1,
130};
131
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700132/**
133 * @vrp: the remote processor this channel belongs to
134 */
135struct virtio_rpmsg_channel {
136 struct rpmsg_device rpdev;
137
138 struct virtproc_info *vrp;
139};
140
141#define to_virtio_rpmsg_channel(_rpdev) \
142 container_of(_rpdev, struct virtio_rpmsg_channel, rpdev)
143
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200144/*
Suman Annab1b98912014-09-16 13:33:07 -0500145 * We're allocating buffers of 512 bytes each for communications. The
146 * number of buffers will be computed from the number of buffers supported
147 * by the vring, upto a maximum of 512 buffers (256 in each direction).
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200148 *
149 * Each buffer will have 16 bytes for the msg header and 496 bytes for
150 * the payload.
151 *
Suman Annab1b98912014-09-16 13:33:07 -0500152 * This will utilize a maximum total space of 256KB for the buffers.
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200153 *
154 * We might also want to add support for user-provided buffers in time.
155 * This will allow bigger buffer size flexibility, and can also be used
156 * to achieve zero-copy messaging.
157 *
158 * Note that these numbers are purely a decision of this driver - we
159 * can change this without changing anything in the firmware of the remote
160 * processor.
161 */
Suman Annab1b98912014-09-16 13:33:07 -0500162#define MAX_RPMSG_NUM_BUFS (512)
Loic Pallardyf93848f2017-03-28 13:49:43 +0200163#define MAX_RPMSG_BUF_SIZE (512)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200164
165/*
166 * Local addresses are dynamically allocated on-demand.
167 * We do not dynamically assign addresses from the low 1024 range,
168 * in order to reserve that address range for predefined services.
169 */
170#define RPMSG_RESERVED_ADDRESSES (1024)
171
172/* Address 53 is reserved for advertising remote services */
173#define RPMSG_NS_ADDR (53)
174
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700175static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
176static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len);
177static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
178 u32 dst);
179static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
180 u32 dst, void *data, int len);
181static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len);
182static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
183 int len, u32 dst);
184static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
185 u32 dst, void *data, int len);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200186
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700187static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
188 .destroy_ept = virtio_rpmsg_destroy_ept,
189 .send = virtio_rpmsg_send,
190 .sendto = virtio_rpmsg_sendto,
191 .send_offchannel = virtio_rpmsg_send_offchannel,
192 .trysend = virtio_rpmsg_trysend,
193 .trysendto = virtio_rpmsg_trysendto,
194 .trysend_offchannel = virtio_rpmsg_trysend_offchannel,
195};
196
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300197/**
198 * __ept_release() - deallocate an rpmsg endpoint
199 * @kref: the ept's reference count
200 *
201 * This function deallocates an ept, and is invoked when its @kref refcount
202 * drops to zero.
203 *
204 * Never invoke this function directly!
205 */
206static void __ept_release(struct kref *kref)
207{
208 struct rpmsg_endpoint *ept = container_of(kref, struct rpmsg_endpoint,
209 refcount);
210 /*
211 * At this point no one holds a reference to ept anymore,
212 * so we can directly free it
213 */
214 kfree(ept);
215}
216
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200217/* for more info, see below documentation of rpmsg_create_ept() */
218static struct rpmsg_endpoint *__rpmsg_create_ept(struct virtproc_info *vrp,
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700219 struct rpmsg_device *rpdev,
Anna, Suman09636792016-08-12 18:42:26 -0500220 rpmsg_rx_cb_t cb,
221 void *priv, u32 addr)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200222{
Tejun Heod0ffce72013-02-27 17:04:40 -0800223 int id_min, id_max, id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200224 struct rpmsg_endpoint *ept;
225 struct device *dev = rpdev ? &rpdev->dev : &vrp->vdev->dev;
226
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200227 ept = kzalloc(sizeof(*ept), GFP_KERNEL);
Anna, Sumana8bb3fd2016-08-12 18:42:24 -0500228 if (!ept)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200229 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200230
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300231 kref_init(&ept->refcount);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300232 mutex_init(&ept->cb_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300233
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200234 ept->rpdev = rpdev;
235 ept->cb = cb;
236 ept->priv = priv;
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700237 ept->ops = &virtio_endpoint_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200238
239 /* do we need to allocate a local address ? */
Tejun Heod0ffce72013-02-27 17:04:40 -0800240 if (addr == RPMSG_ADDR_ANY) {
241 id_min = RPMSG_RESERVED_ADDRESSES;
242 id_max = 0;
243 } else {
244 id_min = addr;
245 id_max = addr + 1;
246 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200247
248 mutex_lock(&vrp->endpoints_lock);
249
250 /* bind the endpoint to an rpmsg address (and allocate one if needed) */
Tejun Heod0ffce72013-02-27 17:04:40 -0800251 id = idr_alloc(&vrp->endpoints, ept, id_min, id_max, GFP_KERNEL);
252 if (id < 0) {
253 dev_err(dev, "idr_alloc failed: %d\n", id);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200254 goto free_ept;
255 }
Tejun Heod0ffce72013-02-27 17:04:40 -0800256 ept->addr = id;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200257
258 mutex_unlock(&vrp->endpoints_lock);
259
260 return ept;
261
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200262free_ept:
263 mutex_unlock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300264 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200265 return NULL;
266}
267
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700268static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev,
269 rpmsg_rx_cb_t cb,
270 void *priv,
271 struct rpmsg_channel_info chinfo)
272{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700273 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
274
275 return __rpmsg_create_ept(vch->vrp, rpdev, cb, priv, chinfo.src);
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700276}
277
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200278/**
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200279 * __rpmsg_destroy_ept() - destroy an existing rpmsg endpoint
280 * @vrp: virtproc which owns this ept
281 * @ept: endpoing to destroy
282 *
283 * An internal function which destroy an ept without assuming it is
284 * bound to an rpmsg channel. This is needed for handling the internal
285 * name service endpoint, which isn't bound to an rpmsg channel.
286 * See also __rpmsg_create_ept().
287 */
288static void
289__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
290{
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300291 /* make sure new inbound messages can't find this ept anymore */
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200292 mutex_lock(&vrp->endpoints_lock);
293 idr_remove(&vrp->endpoints, ept->addr);
294 mutex_unlock(&vrp->endpoints_lock);
295
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300296 /* make sure in-flight inbound messages won't invoke cb anymore */
297 mutex_lock(&ept->cb_lock);
298 ept->cb = NULL;
299 mutex_unlock(&ept->cb_lock);
300
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300301 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200302}
303
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700304static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
305{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700306 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(ept->rpdev);
307
308 __rpmsg_destroy_ept(vch->vrp, ept);
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700309}
310
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700311static int virtio_rpmsg_announce_create(struct rpmsg_device *rpdev)
312{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700313 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
314 struct virtproc_info *vrp = vch->vrp;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700315 struct device *dev = &rpdev->dev;
316 int err = 0;
317
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200318 /* need to tell remote processor's name service about this channel ? */
Henri Roosenb2599eb2017-06-02 13:35:51 +0200319 if (rpdev->announce && rpdev->ept &&
Anna, Suman09636792016-08-12 18:42:26 -0500320 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200321 struct rpmsg_ns_msg nsm;
322
323 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700324 nsm.addr = rpdev->ept->addr;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200325 nsm.flags = RPMSG_NS_CREATE;
326
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700327 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200328 if (err)
329 dev_err(dev, "failed to announce service %d\n", err);
330 }
331
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200332 return err;
333}
334
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700335static int virtio_rpmsg_announce_destroy(struct rpmsg_device *rpdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200336{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700337 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
338 struct virtproc_info *vrp = vch->vrp;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700339 struct device *dev = &rpdev->dev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200340 int err = 0;
341
342 /* tell remote processor's name service we're removing this channel */
Henri Roosenb2599eb2017-06-02 13:35:51 +0200343 if (rpdev->announce && rpdev->ept &&
Anna, Suman09636792016-08-12 18:42:26 -0500344 virtio_has_feature(vrp->vdev, VIRTIO_RPMSG_F_NS)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200345 struct rpmsg_ns_msg nsm;
346
347 strncpy(nsm.name, rpdev->id.name, RPMSG_NAME_SIZE);
Henri Roosen85786722017-06-02 13:36:42 +0200348 nsm.addr = rpdev->ept->addr;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200349 nsm.flags = RPMSG_NS_DESTROY;
350
Bjorn Andersson2a48d732016-09-01 15:27:55 -0700351 err = rpmsg_sendto(rpdev->ept, &nsm, sizeof(nsm), RPMSG_NS_ADDR);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200352 if (err)
353 dev_err(dev, "failed to announce service %d\n", err);
354 }
355
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700356 return err;
357}
358
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700359static const struct rpmsg_device_ops virtio_rpmsg_ops = {
360 .create_ept = virtio_rpmsg_create_ept,
361 .announce_create = virtio_rpmsg_announce_create,
362 .announce_destroy = virtio_rpmsg_announce_destroy,
363};
364
Bjorn Anderssonb0b03b82017-03-15 22:18:35 -0700365static void virtio_rpmsg_release_device(struct device *dev)
366{
367 struct rpmsg_device *rpdev = to_rpmsg_device(dev);
368 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
369
370 kfree(vch);
371}
372
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200373/*
374 * create an rpmsg channel using its name and address info.
375 * this function will be used to create both static and dynamic
376 * channels.
377 */
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700378static struct rpmsg_device *rpmsg_create_channel(struct virtproc_info *vrp,
379 struct rpmsg_channel_info *chinfo)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200380{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700381 struct virtio_rpmsg_channel *vch;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700382 struct rpmsg_device *rpdev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200383 struct device *tmp, *dev = &vrp->vdev->dev;
384 int ret;
385
386 /* make sure a similar channel doesn't already exist */
Bjorn Andersson8b881c02016-09-01 15:28:02 -0700387 tmp = rpmsg_find_device(dev, chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200388 if (tmp) {
389 /* decrement the matched device's refcount back */
390 put_device(tmp);
391 dev_err(dev, "channel %s:%x:%x already exist\n",
392 chinfo->name, chinfo->src, chinfo->dst);
393 return NULL;
394 }
395
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700396 vch = kzalloc(sizeof(*vch), GFP_KERNEL);
397 if (!vch)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200398 return NULL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200399
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700400 /* Link the channel to our vrp */
401 vch->vrp = vrp;
402
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700403 /* Assign public information to the rpmsg_device */
404 rpdev = &vch->rpdev;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200405 rpdev->src = chinfo->src;
406 rpdev->dst = chinfo->dst;
Bjorn Andersson36b72c72016-09-01 15:27:58 -0700407 rpdev->ops = &virtio_rpmsg_ops;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200408
409 /*
410 * rpmsg server channels has predefined local address (for now),
411 * and their existence needs to be announced remotely
412 */
Andrew F. Davisc8ced112016-07-01 09:24:58 -0500413 rpdev->announce = rpdev->src != RPMSG_ADDR_ANY;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200414
415 strncpy(rpdev->id.name, chinfo->name, RPMSG_NAME_SIZE);
416
Bjorn Andersson6eed5982016-09-01 15:28:03 -0700417 rpdev->dev.parent = &vrp->vdev->dev;
Bjorn Anderssonb0b03b82017-03-15 22:18:35 -0700418 rpdev->dev.release = virtio_rpmsg_release_device;
Bjorn Andersson6eed5982016-09-01 15:28:03 -0700419 ret = rpmsg_register_device(rpdev);
420 if (ret)
421 return NULL;
422
423 return rpdev;
424}
425
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200426/* super simple buffer "allocator" that is just enough for now */
427static void *get_a_tx_buf(struct virtproc_info *vrp)
428{
429 unsigned int len;
430 void *ret;
431
432 /* support multiple concurrent senders */
433 mutex_lock(&vrp->tx_lock);
434
435 /*
436 * either pick the next unused tx buffer
437 * (half of our buffers are used for sending messages)
438 */
Suman Annab1b98912014-09-16 13:33:07 -0500439 if (vrp->last_sbuf < vrp->num_bufs / 2)
Loic Pallardyf93848f2017-03-28 13:49:43 +0200440 ret = vrp->sbufs + vrp->buf_size * vrp->last_sbuf++;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200441 /* or recycle a used one */
442 else
443 ret = virtqueue_get_buf(vrp->svq, &len);
444
445 mutex_unlock(&vrp->tx_lock);
446
447 return ret;
448}
449
450/**
451 * rpmsg_upref_sleepers() - enable "tx-complete" interrupts, if needed
452 * @vrp: virtual remote processor state
453 *
454 * This function is called before a sender is blocked, waiting for
455 * a tx buffer to become available.
456 *
457 * If we already have blocking senders, this function merely increases
458 * the "sleepers" reference count, and exits.
459 *
460 * Otherwise, if this is the first sender to block, we also enable
461 * virtio's tx callbacks, so we'd be immediately notified when a tx
462 * buffer is consumed (we rely on virtio's tx callback in order
463 * to wake up sleeping senders as soon as a tx buffer is used by the
464 * remote processor).
465 */
466static void rpmsg_upref_sleepers(struct virtproc_info *vrp)
467{
468 /* support multiple concurrent senders */
469 mutex_lock(&vrp->tx_lock);
470
471 /* are we the first sleeping context waiting for tx buffers ? */
472 if (atomic_inc_return(&vrp->sleepers) == 1)
473 /* enable "tx-complete" interrupts before dozing off */
474 virtqueue_enable_cb(vrp->svq);
475
476 mutex_unlock(&vrp->tx_lock);
477}
478
479/**
480 * rpmsg_downref_sleepers() - disable "tx-complete" interrupts, if needed
481 * @vrp: virtual remote processor state
482 *
483 * This function is called after a sender, that waited for a tx buffer
484 * to become available, is unblocked.
485 *
486 * If we still have blocking senders, this function merely decreases
487 * the "sleepers" reference count, and exits.
488 *
489 * Otherwise, if there are no more blocking senders, we also disable
490 * virtio's tx callbacks, to avoid the overhead incurred with handling
491 * those (now redundant) interrupts.
492 */
493static void rpmsg_downref_sleepers(struct virtproc_info *vrp)
494{
495 /* support multiple concurrent senders */
496 mutex_lock(&vrp->tx_lock);
497
498 /* are we the last sleeping context waiting for tx buffers ? */
499 if (atomic_dec_and_test(&vrp->sleepers))
500 /* disable "tx-complete" interrupts */
501 virtqueue_disable_cb(vrp->svq);
502
503 mutex_unlock(&vrp->tx_lock);
504}
505
506/**
507 * rpmsg_send_offchannel_raw() - send a message across to the remote processor
508 * @rpdev: the rpmsg channel
509 * @src: source address
510 * @dst: destination address
511 * @data: payload of message
512 * @len: length of payload
513 * @wait: indicates whether caller should block in case no TX buffers available
514 *
515 * This function is the base implementation for all of the rpmsg sending API.
516 *
517 * It will send @data of length @len to @dst, and say it's from @src. The
518 * message will be sent to the remote processor which the @rpdev channel
519 * belongs to.
520 *
521 * The message is sent using one of the TX buffers that are available for
522 * communication with this remote processor.
523 *
524 * If @wait is true, the caller will be blocked until either a TX buffer is
525 * available, or 15 seconds elapses (we don't want callers to
526 * sleep indefinitely due to misbehaving remote processors), and in that
527 * case -ERESTARTSYS is returned. The number '15' itself was picked
528 * arbitrarily; there's little point in asking drivers to provide a timeout
529 * value themselves.
530 *
531 * Otherwise, if @wait is false, and there are no TX buffers available,
532 * the function will immediately fail, and -ENOMEM will be returned.
533 *
534 * Normally drivers shouldn't use this function directly; instead, drivers
535 * should use the appropriate rpmsg_{try}send{to, _offchannel} API
536 * (see include/linux/rpmsg.h).
537 *
538 * Returns 0 on success and an appropriate error value on failure.
539 */
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700540static int rpmsg_send_offchannel_raw(struct rpmsg_device *rpdev,
541 u32 src, u32 dst,
542 void *data, int len, bool wait)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200543{
Bjorn Andersson3bf950f2016-09-01 15:28:06 -0700544 struct virtio_rpmsg_channel *vch = to_virtio_rpmsg_channel(rpdev);
545 struct virtproc_info *vrp = vch->vrp;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200546 struct device *dev = &rpdev->dev;
547 struct scatterlist sg;
548 struct rpmsg_hdr *msg;
549 int err;
550
551 /* bcasting isn't allowed */
552 if (src == RPMSG_ADDR_ANY || dst == RPMSG_ADDR_ANY) {
553 dev_err(dev, "invalid addr (src 0x%x, dst 0x%x)\n", src, dst);
554 return -EINVAL;
555 }
556
557 /*
558 * We currently use fixed-sized buffers, and therefore the payload
559 * length is limited.
560 *
561 * One of the possible improvements here is either to support
562 * user-provided buffers (and then we can also support zero-copy
563 * messaging), or to improve the buffer allocator, to support
564 * variable-length buffer sizes.
565 */
Loic Pallardyf93848f2017-03-28 13:49:43 +0200566 if (len > vrp->buf_size - sizeof(struct rpmsg_hdr)) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200567 dev_err(dev, "message is too big (%d)\n", len);
568 return -EMSGSIZE;
569 }
570
571 /* grab a buffer */
572 msg = get_a_tx_buf(vrp);
573 if (!msg && !wait)
574 return -ENOMEM;
575
576 /* no free buffer ? wait for one (but bail after 15 seconds) */
577 while (!msg) {
578 /* enable "tx-complete" interrupts, if not already enabled */
579 rpmsg_upref_sleepers(vrp);
580
581 /*
582 * sleep until a free buffer is available or 15 secs elapse.
583 * the timeout period is not configurable because there's
584 * little point in asking drivers to specify that.
585 * if later this happens to be required, it'd be easy to add.
586 */
587 err = wait_event_interruptible_timeout(vrp->sendq,
588 (msg = get_a_tx_buf(vrp)),
589 msecs_to_jiffies(15000));
590
591 /* disable "tx-complete" interrupts if we're the last sleeper */
592 rpmsg_downref_sleepers(vrp);
593
594 /* timeout ? */
595 if (!err) {
596 dev_err(dev, "timeout waiting for a tx buffer\n");
597 return -ERESTARTSYS;
598 }
599 }
600
601 msg->len = len;
602 msg->flags = 0;
603 msg->src = src;
604 msg->dst = dst;
605 msg->reserved = 0;
606 memcpy(msg->data, data, len);
607
608 dev_dbg(dev, "TX From 0x%x, To 0x%x, Len %d, Flags %d, Reserved %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500609 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500610#if defined(CONFIG_DYNAMIC_DEBUG)
611 dynamic_hex_dump("rpmsg_virtio TX: ", DUMP_PREFIX_NONE, 16, 1,
612 msg, sizeof(*msg) + msg->len, true);
613#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200614
615 sg_init_one(&sg, msg, sizeof(*msg) + len);
616
617 mutex_lock(&vrp->tx_lock);
618
619 /* add message to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030620 err = virtqueue_add_outbuf(vrp->svq, &sg, 1, msg, GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030621 if (err) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200622 /*
623 * need to reclaim the buffer here, otherwise it's lost
624 * (memory won't leak, but rpmsg won't use it again for TX).
625 * this will wait for a buffer management overhaul.
626 */
Rusty Russellcee51d62013-03-20 15:44:29 +1030627 dev_err(dev, "virtqueue_add_outbuf failed: %d\n", err);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200628 goto out;
629 }
630
631 /* tell the remote processor it has a pending message to read */
632 virtqueue_kick(vrp->svq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200633out:
634 mutex_unlock(&vrp->tx_lock);
635 return err;
636}
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200637
Bjorn Andersson8a228ec2016-09-01 15:28:00 -0700638static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len)
639{
640 struct rpmsg_device *rpdev = ept->rpdev;
641 u32 src = ept->addr, dst = rpdev->dst;
642
643 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
644}
645
646static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len,
647 u32 dst)
648{
649 struct rpmsg_device *rpdev = ept->rpdev;
650 u32 src = ept->addr;
651
652 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
653}
654
655static int virtio_rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src,
656 u32 dst, void *data, int len)
657{
658 struct rpmsg_device *rpdev = ept->rpdev;
659
660 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
661}
662
663static int virtio_rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len)
664{
665 struct rpmsg_device *rpdev = ept->rpdev;
666 u32 src = ept->addr, dst = rpdev->dst;
667
668 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
669}
670
671static int virtio_rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data,
672 int len, u32 dst)
673{
674 struct rpmsg_device *rpdev = ept->rpdev;
675 u32 src = ept->addr;
676
677 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
678}
679
680static int virtio_rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src,
681 u32 dst, void *data, int len)
682{
683 struct rpmsg_device *rpdev = ept->rpdev;
684
685 return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
686}
687
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300688static int rpmsg_recv_single(struct virtproc_info *vrp, struct device *dev,
689 struct rpmsg_hdr *msg, unsigned int len)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200690{
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200691 struct rpmsg_endpoint *ept;
692 struct scatterlist sg;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200693 int err;
694
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200695 dev_dbg(dev, "From: 0x%x, To: 0x%x, Len: %d, Flags: %d, Reserved: %d\n",
Anna, Suman09636792016-08-12 18:42:26 -0500696 msg->src, msg->dst, msg->len, msg->flags, msg->reserved);
Anna, Suman211e3a92016-08-12 18:42:27 -0500697#if defined(CONFIG_DYNAMIC_DEBUG)
698 dynamic_hex_dump("rpmsg_virtio RX: ", DUMP_PREFIX_NONE, 16, 1,
699 msg, sizeof(*msg) + msg->len, true);
700#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200701
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200702 /*
703 * We currently use fixed-sized buffers, so trivially sanitize
704 * the reported payload length.
705 */
Loic Pallardyf93848f2017-03-28 13:49:43 +0200706 if (len > vrp->buf_size ||
Anna, Suman09636792016-08-12 18:42:26 -0500707 msg->len > (len - sizeof(struct rpmsg_hdr))) {
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200708 dev_warn(dev, "inbound msg too big: (%d, %d)\n", len, msg->len);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300709 return -EINVAL;
Ohad Ben-Cohen96482242012-02-28 16:16:48 +0200710 }
711
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200712 /* use the dst addr to fetch the callback of the appropriate user */
713 mutex_lock(&vrp->endpoints_lock);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300714
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200715 ept = idr_find(&vrp->endpoints, msg->dst);
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300716
717 /* let's make sure no one deallocates ept while we use it */
718 if (ept)
719 kref_get(&ept->refcount);
720
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200721 mutex_unlock(&vrp->endpoints_lock);
722
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300723 if (ept) {
724 /* make sure ept->cb doesn't go away while we use it */
725 mutex_lock(&ept->cb_lock);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200726
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300727 if (ept->cb)
728 ept->cb(ept->rpdev, msg->data, msg->len, ept->priv,
729 msg->src);
730
731 mutex_unlock(&ept->cb_lock);
732
733 /* farewell, ept, we don't need you anymore */
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300734 kref_put(&ept->refcount, __ept_release);
Ohad Ben-Cohen15fd9432012-06-07 15:39:35 +0300735 } else
Masanari Iida8a168ca2012-12-29 02:00:09 +0900736 dev_warn(dev, "msg received with no recipient\n");
Ohad Ben-Cohen5a081ca2012-06-06 10:09:25 +0300737
Ohad Ben-Cohenf1d9e9c2012-02-28 16:11:28 +0200738 /* publish the real size of the buffer */
Loic Pallardyf93848f2017-03-28 13:49:43 +0200739 sg_init_one(&sg, msg, vrp->buf_size);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200740
741 /* add the buffer back to the remote processor's virtqueue */
Rusty Russellcee51d62013-03-20 15:44:29 +1030742 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, msg, GFP_KERNEL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200743 if (err < 0) {
744 dev_err(dev, "failed to add a virtqueue buffer: %d\n", err);
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300745 return err;
746 }
747
748 return 0;
749}
750
751/* called when an rx buffer is used, and it's time to digest a message */
752static void rpmsg_recv_done(struct virtqueue *rvq)
753{
754 struct virtproc_info *vrp = rvq->vdev->priv;
755 struct device *dev = &rvq->vdev->dev;
756 struct rpmsg_hdr *msg;
757 unsigned int len, msgs_received = 0;
758 int err;
759
760 msg = virtqueue_get_buf(rvq, &len);
761 if (!msg) {
762 dev_err(dev, "uhm, incoming signal, but no used buffer ?\n");
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200763 return;
764 }
765
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300766 while (msg) {
767 err = rpmsg_recv_single(vrp, dev, msg, len);
768 if (err)
769 break;
770
771 msgs_received++;
772
773 msg = virtqueue_get_buf(rvq, &len);
Lee Jones6c49fbe2016-07-20 10:29:35 +0100774 }
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300775
776 dev_dbg(dev, "Received %u messages\n", msgs_received);
777
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200778 /* tell the remote processor we added another available rx buffer */
Robert Tivy1aa7d6a2013-04-05 17:38:52 +0300779 if (msgs_received)
780 virtqueue_kick(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200781}
782
783/*
784 * This is invoked whenever the remote processor completed processing
785 * a TX msg we just sent it, and the buffer is put back to the used ring.
786 *
787 * Normally, though, we suppress this "tx complete" interrupt in order to
788 * avoid the incurred overhead.
789 */
790static void rpmsg_xmit_done(struct virtqueue *svq)
791{
792 struct virtproc_info *vrp = svq->vdev->priv;
793
794 dev_dbg(&svq->vdev->dev, "%s\n", __func__);
795
796 /* wake up potential senders that are waiting for a tx buffer */
797 wake_up_interruptible(&vrp->sendq);
798}
799
800/* invoked when a name service announcement arrives */
Bjorn Andersson4b83c522016-09-01 15:28:08 -0700801static int rpmsg_ns_cb(struct rpmsg_device *rpdev, void *data, int len,
802 void *priv, u32 src)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200803{
804 struct rpmsg_ns_msg *msg = data;
Bjorn Andersson92e1de52016-09-01 15:27:57 -0700805 struct rpmsg_device *newch;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200806 struct rpmsg_channel_info chinfo;
807 struct virtproc_info *vrp = priv;
808 struct device *dev = &vrp->vdev->dev;
809 int ret;
810
Anna, Suman211e3a92016-08-12 18:42:27 -0500811#if defined(CONFIG_DYNAMIC_DEBUG)
812 dynamic_hex_dump("NS announcement: ", DUMP_PREFIX_NONE, 16, 1,
813 data, len, true);
814#endif
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200815
816 if (len != sizeof(*msg)) {
817 dev_err(dev, "malformed ns msg (%d)\n", len);
Bjorn Andersson4b83c522016-09-01 15:28:08 -0700818 return -EINVAL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200819 }
820
821 /*
822 * the name service ept does _not_ belong to a real rpmsg channel,
823 * and is handled by the rpmsg bus itself.
824 * for sanity reasons, make sure a valid rpdev has _not_ sneaked
825 * in somehow.
826 */
827 if (rpdev) {
828 dev_err(dev, "anomaly: ns ept has an rpdev handle\n");
Bjorn Andersson4b83c522016-09-01 15:28:08 -0700829 return -EINVAL;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200830 }
831
832 /* don't trust the remote processor for null terminating the name */
833 msg->name[RPMSG_NAME_SIZE - 1] = '\0';
834
835 dev_info(dev, "%sing channel %s addr 0x%x\n",
Anna, Suman09636792016-08-12 18:42:26 -0500836 msg->flags & RPMSG_NS_DESTROY ? "destroy" : "creat",
837 msg->name, msg->addr);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200838
839 strncpy(chinfo.name, msg->name, sizeof(chinfo.name));
840 chinfo.src = RPMSG_ADDR_ANY;
841 chinfo.dst = msg->addr;
842
843 if (msg->flags & RPMSG_NS_DESTROY) {
Bjorn Andersson5e619b42016-09-01 15:28:04 -0700844 ret = rpmsg_unregister_device(&vrp->vdev->dev, &chinfo);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200845 if (ret)
846 dev_err(dev, "rpmsg_destroy_channel failed: %d\n", ret);
847 } else {
848 newch = rpmsg_create_channel(vrp, &chinfo);
849 if (!newch)
850 dev_err(dev, "rpmsg_create_channel failed\n");
851 }
Bjorn Andersson4b83c522016-09-01 15:28:08 -0700852
853 return 0;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200854}
855
856static int rpmsg_probe(struct virtio_device *vdev)
857{
858 vq_callback_t *vq_cbs[] = { rpmsg_recv_done, rpmsg_xmit_done };
Stefan Hajnoczif7ad26f2015-12-17 16:53:43 +0800859 static const char * const names[] = { "input", "output" };
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200860 struct virtqueue *vqs[2];
861 struct virtproc_info *vrp;
862 void *bufs_va;
863 int err = 0, i;
Suman Annab1b98912014-09-16 13:33:07 -0500864 size_t total_buf_space;
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030865 bool notify;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200866
867 vrp = kzalloc(sizeof(*vrp), GFP_KERNEL);
868 if (!vrp)
869 return -ENOMEM;
870
871 vrp->vdev = vdev;
872
873 idr_init(&vrp->endpoints);
874 mutex_init(&vrp->endpoints_lock);
875 mutex_init(&vrp->tx_lock);
876 init_waitqueue_head(&vrp->sendq);
877
878 /* We expect two virtqueues, rx and tx (and in this order) */
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +0200879 err = virtio_find_vqs(vdev, 2, vqs, vq_cbs, names, NULL);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200880 if (err)
881 goto free_vrp;
882
883 vrp->rvq = vqs[0];
884 vrp->svq = vqs[1];
885
Suman Annab1b98912014-09-16 13:33:07 -0500886 /* we expect symmetric tx/rx vrings */
887 WARN_ON(virtqueue_get_vring_size(vrp->rvq) !=
888 virtqueue_get_vring_size(vrp->svq));
889
890 /* we need less buffers if vrings are small */
891 if (virtqueue_get_vring_size(vrp->rvq) < MAX_RPMSG_NUM_BUFS / 2)
892 vrp->num_bufs = virtqueue_get_vring_size(vrp->rvq) * 2;
893 else
894 vrp->num_bufs = MAX_RPMSG_NUM_BUFS;
895
Loic Pallardyf93848f2017-03-28 13:49:43 +0200896 vrp->buf_size = MAX_RPMSG_BUF_SIZE;
897
898 total_buf_space = vrp->num_bufs * vrp->buf_size;
Suman Annab1b98912014-09-16 13:33:07 -0500899
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200900 /* allocate coherent memory for the buffers */
Ohad Ben-Cohenb5ab5e22012-05-30 22:01:25 +0300901 bufs_va = dma_alloc_coherent(vdev->dev.parent->parent,
Suman Annab1b98912014-09-16 13:33:07 -0500902 total_buf_space, &vrp->bufs_dma,
903 GFP_KERNEL);
Wei Yongjun3119b482013-04-29 16:17:09 -0700904 if (!bufs_va) {
905 err = -ENOMEM;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200906 goto vqs_del;
Wei Yongjun3119b482013-04-29 16:17:09 -0700907 }
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200908
Anna, Suman8d95b322016-08-12 18:42:25 -0500909 dev_dbg(&vdev->dev, "buffers: va %p, dma %pad\n",
910 bufs_va, &vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200911
912 /* half of the buffers is dedicated for RX */
913 vrp->rbufs = bufs_va;
914
915 /* and half is dedicated for TX */
Suman Annab1b98912014-09-16 13:33:07 -0500916 vrp->sbufs = bufs_va + total_buf_space / 2;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200917
918 /* set up the receive buffers */
Suman Annab1b98912014-09-16 13:33:07 -0500919 for (i = 0; i < vrp->num_bufs / 2; i++) {
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200920 struct scatterlist sg;
Loic Pallardyf93848f2017-03-28 13:49:43 +0200921 void *cpu_addr = vrp->rbufs + i * vrp->buf_size;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200922
Loic Pallardyf93848f2017-03-28 13:49:43 +0200923 sg_init_one(&sg, cpu_addr, vrp->buf_size);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200924
Rusty Russellcee51d62013-03-20 15:44:29 +1030925 err = virtqueue_add_inbuf(vrp->rvq, &sg, 1, cpu_addr,
Anna, Suman09636792016-08-12 18:42:26 -0500926 GFP_KERNEL);
Rusty Russell57e1a372012-10-16 23:56:15 +1030927 WARN_ON(err); /* sanity check; this can't really happen */
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200928 }
929
930 /* suppress "tx-complete" interrupts */
931 virtqueue_disable_cb(vrp->svq);
932
933 vdev->priv = vrp;
934
935 /* if supported by the remote processor, enable the name service */
936 if (virtio_has_feature(vdev, VIRTIO_RPMSG_F_NS)) {
937 /* a dedicated endpoint handles the name service msgs */
938 vrp->ns_ept = __rpmsg_create_ept(vrp, NULL, rpmsg_ns_cb,
939 vrp, RPMSG_NS_ADDR);
940 if (!vrp->ns_ept) {
941 dev_err(&vdev->dev, "failed to create the ns ept\n");
942 err = -ENOMEM;
943 goto free_coherent;
944 }
945 }
946
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030947 /*
948 * Prepare to kick but don't notify yet - we can't do this before
949 * device is ready.
950 */
951 notify = virtqueue_kick_prepare(vrp->rvq);
952
953 /* From this point on, we can notify and get callbacks. */
954 virtio_device_ready(vdev);
955
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200956 /* tell the remote processor it can start sending messages */
Michael S. Tsirkin71e4b8b2015-03-12 11:54:41 +1030957 /*
958 * this might be concurrent with callbacks, but we are only
959 * doing notify, not a full kick here, so that's ok.
960 */
961 if (notify)
962 virtqueue_notify(vrp->rvq);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200963
964 dev_info(&vdev->dev, "rpmsg host is online\n");
965
966 return 0;
967
968free_coherent:
Suman Annab1b98912014-09-16 13:33:07 -0500969 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
970 bufs_va, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200971vqs_del:
972 vdev->config->del_vqs(vrp->vdev);
973free_vrp:
974 kfree(vrp);
975 return err;
976}
977
978static int rpmsg_remove_device(struct device *dev, void *data)
979{
980 device_unregister(dev);
981
982 return 0;
983}
984
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -0800985static void rpmsg_remove(struct virtio_device *vdev)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200986{
987 struct virtproc_info *vrp = vdev->priv;
Loic Pallardyf93848f2017-03-28 13:49:43 +0200988 size_t total_buf_space = vrp->num_bufs * vrp->buf_size;
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +0200989 int ret;
990
991 vdev->config->reset(vdev);
992
993 ret = device_for_each_child(&vdev->dev, NULL, rpmsg_remove_device);
994 if (ret)
995 dev_warn(&vdev->dev, "can't remove rpmsg device: %d\n", ret);
996
Ohad Ben-Cohenfa2d7792012-02-09 15:16:41 +0200997 if (vrp->ns_ept)
998 __rpmsg_destroy_ept(vrp, vrp->ns_ept);
999
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001000 idr_destroy(&vrp->endpoints);
1001
1002 vdev->config->del_vqs(vrp->vdev);
1003
Suman Annab1b98912014-09-16 13:33:07 -05001004 dma_free_coherent(vdev->dev.parent->parent, total_buf_space,
1005 vrp->rbufs, vrp->bufs_dma);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001006
1007 kfree(vrp);
1008}
1009
1010static struct virtio_device_id id_table[] = {
1011 { VIRTIO_ID_RPMSG, VIRTIO_DEV_ANY_ID },
1012 { 0 },
1013};
1014
1015static unsigned int features[] = {
1016 VIRTIO_RPMSG_F_NS,
1017};
1018
1019static struct virtio_driver virtio_ipc_driver = {
1020 .feature_table = features,
1021 .feature_table_size = ARRAY_SIZE(features),
1022 .driver.name = KBUILD_MODNAME,
1023 .driver.owner = THIS_MODULE,
1024 .id_table = id_table,
1025 .probe = rpmsg_probe,
Greg Kroah-Hartman0fe763c2012-12-21 15:14:44 -08001026 .remove = rpmsg_remove,
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001027};
1028
1029static int __init rpmsg_init(void)
1030{
1031 int ret;
1032
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001033 ret = register_virtio_driver(&virtio_ipc_driver);
Bjorn Andersson5e619b42016-09-01 15:28:04 -07001034 if (ret)
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001035 pr_err("failed to register virtio driver: %d\n", ret);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001036
1037 return ret;
1038}
Federico Fuga96342522012-07-16 10:36:51 +03001039subsys_initcall(rpmsg_init);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001040
1041static void __exit rpmsg_fini(void)
1042{
1043 unregister_virtio_driver(&virtio_ipc_driver);
Ohad Ben-Cohenbcabbcc2011-10-20 21:10:55 +02001044}
1045module_exit(rpmsg_fini);
1046
1047MODULE_DEVICE_TABLE(virtio, id_table);
1048MODULE_DESCRIPTION("Virtio-based remote processor messaging bus");
1049MODULE_LICENSE("GPL v2");