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