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