1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * 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/kernel.h> 15 #include <linux/module.h> 16 #include <linux/rpmsg.h> 17 #include <linux/of_device.h> 18 #include <linux/pm_domain.h> 19 #include <linux/slab.h> 20 21 #include "rpmsg_internal.h" 22 23 /** 24 * rpmsg_create_channel() - create a new rpmsg channel 25 * using its name and address info. 26 * @rpdev: rpmsg device 27 * @chinfo: channel_info to bind 28 * 29 * Returns a pointer to the new rpmsg device on success, or NULL on error. 30 */ 31 struct rpmsg_device *rpmsg_create_channel(struct rpmsg_device *rpdev, 32 struct rpmsg_channel_info *chinfo) 33 { 34 if (WARN_ON(!rpdev)) 35 return NULL; 36 if (!rpdev->ops || !rpdev->ops->create_channel) { 37 dev_err(&rpdev->dev, "no create_channel ops found\n"); 38 return NULL; 39 } 40 41 return rpdev->ops->create_channel(rpdev, chinfo); 42 } 43 EXPORT_SYMBOL(rpmsg_create_channel); 44 45 /** 46 * rpmsg_release_channel() - release a rpmsg channel 47 * using its name and address info. 48 * @rpdev: rpmsg device 49 * @chinfo: channel_info to bind 50 * 51 * Returns 0 on success or an appropriate error value. 52 */ 53 int rpmsg_release_channel(struct rpmsg_device *rpdev, 54 struct rpmsg_channel_info *chinfo) 55 { 56 if (WARN_ON(!rpdev)) 57 return -EINVAL; 58 if (!rpdev->ops || !rpdev->ops->release_channel) { 59 dev_err(&rpdev->dev, "no release_channel ops found\n"); 60 return -ENXIO; 61 } 62 63 return rpdev->ops->release_channel(rpdev, chinfo); 64 } 65 EXPORT_SYMBOL(rpmsg_release_channel); 66 67 /** 68 * rpmsg_create_ept() - create a new rpmsg_endpoint 69 * @rpdev: rpmsg channel device 70 * @cb: rx callback handler 71 * @priv: private data for the driver's use 72 * @chinfo: channel_info with the local rpmsg address to bind with @cb 73 * 74 * Every rpmsg address in the system is bound to an rx callback (so when 75 * inbound messages arrive, they are dispatched by the rpmsg bus using the 76 * appropriate callback handler) by means of an rpmsg_endpoint struct. 77 * 78 * This function allows drivers to create such an endpoint, and by that, 79 * bind a callback, and possibly some private data too, to an rpmsg address 80 * (either one that is known in advance, or one that will be dynamically 81 * assigned for them). 82 * 83 * Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint 84 * is already created for them when they are probed by the rpmsg bus 85 * (using the rx callback provided when they registered to the rpmsg bus). 86 * 87 * So things should just work for simple drivers: they already have an 88 * endpoint, their rx callback is bound to their rpmsg address, and when 89 * relevant inbound messages arrive (i.e. messages which their dst address 90 * equals to the src address of their rpmsg channel), the driver's handler 91 * is invoked to process it. 92 * 93 * That said, more complicated drivers might need to allocate 94 * additional rpmsg addresses, and bind them to different rx callbacks. 95 * To accomplish that, those drivers need to call this function. 96 * 97 * Drivers should provide their @rpdev channel (so the new endpoint would belong 98 * to the same remote processor their channel belongs to), an rx callback 99 * function, an optional private data (which is provided back when the 100 * rx callback is invoked), and an address they want to bind with the 101 * callback. If @addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will 102 * dynamically assign them an available rpmsg address (drivers should have 103 * a very good reason why not to always use RPMSG_ADDR_ANY here). 104 * 105 * Returns a pointer to the endpoint on success, or NULL on error. 106 */ 107 struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_device *rpdev, 108 rpmsg_rx_cb_t cb, void *priv, 109 struct rpmsg_channel_info chinfo) 110 { 111 if (WARN_ON(!rpdev)) 112 return NULL; 113 114 return rpdev->ops->create_ept(rpdev, cb, priv, chinfo); 115 } 116 EXPORT_SYMBOL(rpmsg_create_ept); 117 118 /** 119 * rpmsg_destroy_ept() - destroy an existing rpmsg endpoint 120 * @ept: endpoing to destroy 121 * 122 * Should be used by drivers to destroy an rpmsg endpoint previously 123 * created with rpmsg_create_ept(). As with other types of "free" NULL 124 * is a valid parameter. 125 */ 126 void rpmsg_destroy_ept(struct rpmsg_endpoint *ept) 127 { 128 if (ept && ept->ops) 129 ept->ops->destroy_ept(ept); 130 } 131 EXPORT_SYMBOL(rpmsg_destroy_ept); 132 133 /** 134 * rpmsg_send() - send a message across to the remote processor 135 * @ept: the rpmsg endpoint 136 * @data: payload of message 137 * @len: length of payload 138 * 139 * This function sends @data of length @len on the @ept endpoint. 140 * The message will be sent to the remote processor which the @ept 141 * endpoint belongs to, using @ept's address and its associated rpmsg 142 * device destination addresses. 143 * In case there are no TX buffers available, the function will block until 144 * one becomes available, or a timeout of 15 seconds elapses. When the latter 145 * happens, -ERESTARTSYS is returned. 146 * 147 * Can only be called from process context (for now). 148 * 149 * Returns 0 on success and an appropriate error value on failure. 150 */ 151 int rpmsg_send(struct rpmsg_endpoint *ept, void *data, int len) 152 { 153 if (WARN_ON(!ept)) 154 return -EINVAL; 155 if (!ept->ops->send) 156 return -ENXIO; 157 158 return ept->ops->send(ept, data, len); 159 } 160 EXPORT_SYMBOL(rpmsg_send); 161 162 /** 163 * rpmsg_sendto() - send a message across to the remote processor, specify dst 164 * @ept: the rpmsg endpoint 165 * @data: payload of message 166 * @len: length of payload 167 * @dst: destination address 168 * 169 * This function sends @data of length @len to the remote @dst address. 170 * The message will be sent to the remote processor which the @ept 171 * endpoint belongs to, using @ept's address as source. 172 * In case there are no TX buffers available, the function will block until 173 * one becomes available, or a timeout of 15 seconds elapses. When the latter 174 * happens, -ERESTARTSYS is returned. 175 * 176 * Can only be called from process context (for now). 177 * 178 * Returns 0 on success and an appropriate error value on failure. 179 */ 180 int rpmsg_sendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst) 181 { 182 if (WARN_ON(!ept)) 183 return -EINVAL; 184 if (!ept->ops->sendto) 185 return -ENXIO; 186 187 return ept->ops->sendto(ept, data, len, dst); 188 } 189 EXPORT_SYMBOL(rpmsg_sendto); 190 191 /** 192 * rpmsg_send_offchannel() - send a message using explicit src/dst addresses 193 * @ept: the rpmsg endpoint 194 * @src: source address 195 * @dst: destination address 196 * @data: payload of message 197 * @len: length of payload 198 * 199 * This function sends @data of length @len to the remote @dst address, 200 * and uses @src as the source address. 201 * The message will be sent to the remote processor which the @ept 202 * endpoint belongs to. 203 * In case there are no TX buffers available, the function will block until 204 * one becomes available, or a timeout of 15 seconds elapses. When the latter 205 * happens, -ERESTARTSYS is returned. 206 * 207 * Can only be called from process context (for now). 208 * 209 * Returns 0 on success and an appropriate error value on failure. 210 */ 211 int rpmsg_send_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst, 212 void *data, int len) 213 { 214 if (WARN_ON(!ept)) 215 return -EINVAL; 216 if (!ept->ops->send_offchannel) 217 return -ENXIO; 218 219 return ept->ops->send_offchannel(ept, src, dst, data, len); 220 } 221 EXPORT_SYMBOL(rpmsg_send_offchannel); 222 223 /** 224 * rpmsg_trysend() - send a message across to the remote processor 225 * @ept: the rpmsg endpoint 226 * @data: payload of message 227 * @len: length of payload 228 * 229 * This function sends @data of length @len on the @ept endpoint. 230 * The message will be sent to the remote processor which the @ept 231 * endpoint belongs to, using @ept's address as source and its associated 232 * rpdev's address as destination. 233 * In case there are no TX buffers available, the function will immediately 234 * return -ENOMEM without waiting until one becomes available. 235 * 236 * Can only be called from process context (for now). 237 * 238 * Returns 0 on success and an appropriate error value on failure. 239 */ 240 int rpmsg_trysend(struct rpmsg_endpoint *ept, void *data, int len) 241 { 242 if (WARN_ON(!ept)) 243 return -EINVAL; 244 if (!ept->ops->trysend) 245 return -ENXIO; 246 247 return ept->ops->trysend(ept, data, len); 248 } 249 EXPORT_SYMBOL(rpmsg_trysend); 250 251 /** 252 * rpmsg_trysendto() - send a message across to the remote processor, specify dst 253 * @ept: the rpmsg endpoint 254 * @data: payload of message 255 * @len: length of payload 256 * @dst: destination address 257 * 258 * This function sends @data of length @len to the remote @dst address. 259 * The message will be sent to the remote processor which the @ept 260 * endpoint belongs to, using @ept's address as source. 261 * In case there are no TX buffers available, the function will immediately 262 * return -ENOMEM without waiting until one becomes available. 263 * 264 * Can only be called from process context (for now). 265 * 266 * Returns 0 on success and an appropriate error value on failure. 267 */ 268 int rpmsg_trysendto(struct rpmsg_endpoint *ept, void *data, int len, u32 dst) 269 { 270 if (WARN_ON(!ept)) 271 return -EINVAL; 272 if (!ept->ops->trysendto) 273 return -ENXIO; 274 275 return ept->ops->trysendto(ept, data, len, dst); 276 } 277 EXPORT_SYMBOL(rpmsg_trysendto); 278 279 /** 280 * rpmsg_poll() - poll the endpoint's send buffers 281 * @ept: the rpmsg endpoint 282 * @filp: file for poll_wait() 283 * @wait: poll_table for poll_wait() 284 * 285 * Returns mask representing the current state of the endpoint's send buffers 286 */ 287 __poll_t rpmsg_poll(struct rpmsg_endpoint *ept, struct file *filp, 288 poll_table *wait) 289 { 290 if (WARN_ON(!ept)) 291 return 0; 292 if (!ept->ops->poll) 293 return 0; 294 295 return ept->ops->poll(ept, filp, wait); 296 } 297 EXPORT_SYMBOL(rpmsg_poll); 298 299 /** 300 * rpmsg_trysend_offchannel() - send a message using explicit src/dst addresses 301 * @ept: the rpmsg endpoint 302 * @src: source address 303 * @dst: destination address 304 * @data: payload of message 305 * @len: length of payload 306 * 307 * This function sends @data of length @len to the remote @dst address, 308 * and uses @src as the source address. 309 * The message will be sent to the remote processor which the @ept 310 * endpoint belongs to. 311 * In case there are no TX buffers available, the function will immediately 312 * return -ENOMEM without waiting until one becomes available. 313 * 314 * Can only be called from process context (for now). 315 * 316 * Returns 0 on success and an appropriate error value on failure. 317 */ 318 int rpmsg_trysend_offchannel(struct rpmsg_endpoint *ept, u32 src, u32 dst, 319 void *data, int len) 320 { 321 if (WARN_ON(!ept)) 322 return -EINVAL; 323 if (!ept->ops->trysend_offchannel) 324 return -ENXIO; 325 326 return ept->ops->trysend_offchannel(ept, src, dst, data, len); 327 } 328 EXPORT_SYMBOL(rpmsg_trysend_offchannel); 329 330 /** 331 * rpmsg_get_mtu() - get maximum transmission buffer size for sending message. 332 * @ept: the rpmsg endpoint 333 * 334 * This function returns maximum buffer size available for a single outgoing message. 335 * 336 * Return: the maximum transmission size on success and an appropriate error 337 * value on failure. 338 */ 339 340 ssize_t rpmsg_get_mtu(struct rpmsg_endpoint *ept) 341 { 342 if (WARN_ON(!ept)) 343 return -EINVAL; 344 if (!ept->ops->get_mtu) 345 return -ENOTSUPP; 346 347 return ept->ops->get_mtu(ept); 348 } 349 EXPORT_SYMBOL(rpmsg_get_mtu); 350 351 /* 352 * match a rpmsg channel with a channel info struct. 353 * this is used to make sure we're not creating rpmsg devices for channels 354 * that already exist. 355 */ 356 static int rpmsg_device_match(struct device *dev, void *data) 357 { 358 struct rpmsg_channel_info *chinfo = data; 359 struct rpmsg_device *rpdev = to_rpmsg_device(dev); 360 361 if (chinfo->src != RPMSG_ADDR_ANY && chinfo->src != rpdev->src) 362 return 0; 363 364 if (chinfo->dst != RPMSG_ADDR_ANY && chinfo->dst != rpdev->dst) 365 return 0; 366 367 if (strncmp(chinfo->name, rpdev->id.name, RPMSG_NAME_SIZE)) 368 return 0; 369 370 /* found a match ! */ 371 return 1; 372 } 373 374 struct device *rpmsg_find_device(struct device *parent, 375 struct rpmsg_channel_info *chinfo) 376 { 377 return device_find_child(parent, chinfo, rpmsg_device_match); 378 379 } 380 EXPORT_SYMBOL(rpmsg_find_device); 381 382 /* sysfs show configuration fields */ 383 #define rpmsg_show_attr(field, path, format_string) \ 384 static ssize_t \ 385 field##_show(struct device *dev, \ 386 struct device_attribute *attr, char *buf) \ 387 { \ 388 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \ 389 \ 390 return sprintf(buf, format_string, rpdev->path); \ 391 } \ 392 static DEVICE_ATTR_RO(field); 393 394 #define rpmsg_string_attr(field, member) \ 395 static ssize_t \ 396 field##_store(struct device *dev, struct device_attribute *attr, \ 397 const char *buf, size_t sz) \ 398 { \ 399 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \ 400 char *new, *old; \ 401 \ 402 new = kstrndup(buf, sz, GFP_KERNEL); \ 403 if (!new) \ 404 return -ENOMEM; \ 405 new[strcspn(new, "\n")] = '\0'; \ 406 \ 407 device_lock(dev); \ 408 old = rpdev->member; \ 409 if (strlen(new)) { \ 410 rpdev->member = new; \ 411 } else { \ 412 kfree(new); \ 413 rpdev->member = NULL; \ 414 } \ 415 device_unlock(dev); \ 416 \ 417 kfree(old); \ 418 \ 419 return sz; \ 420 } \ 421 static ssize_t \ 422 field##_show(struct device *dev, \ 423 struct device_attribute *attr, char *buf) \ 424 { \ 425 struct rpmsg_device *rpdev = to_rpmsg_device(dev); \ 426 \ 427 return sprintf(buf, "%s\n", rpdev->member); \ 428 } \ 429 static DEVICE_ATTR_RW(field) 430 431 /* for more info, see Documentation/ABI/testing/sysfs-bus-rpmsg */ 432 rpmsg_show_attr(name, id.name, "%s\n"); 433 rpmsg_show_attr(src, src, "0x%x\n"); 434 rpmsg_show_attr(dst, dst, "0x%x\n"); 435 rpmsg_show_attr(announce, announce ? "true" : "false", "%s\n"); 436 rpmsg_string_attr(driver_override, driver_override); 437 438 static ssize_t modalias_show(struct device *dev, 439 struct device_attribute *attr, char *buf) 440 { 441 struct rpmsg_device *rpdev = to_rpmsg_device(dev); 442 ssize_t len; 443 444 len = of_device_modalias(dev, buf, PAGE_SIZE); 445 if (len != -ENODEV) 446 return len; 447 448 return sprintf(buf, RPMSG_DEVICE_MODALIAS_FMT "\n", rpdev->id.name); 449 } 450 static DEVICE_ATTR_RO(modalias); 451 452 static struct attribute *rpmsg_dev_attrs[] = { 453 &dev_attr_name.attr, 454 &dev_attr_modalias.attr, 455 &dev_attr_dst.attr, 456 &dev_attr_src.attr, 457 &dev_attr_announce.attr, 458 &dev_attr_driver_override.attr, 459 NULL, 460 }; 461 ATTRIBUTE_GROUPS(rpmsg_dev); 462 463 /* rpmsg devices and drivers are matched using the service name */ 464 static inline int rpmsg_id_match(const struct rpmsg_device *rpdev, 465 const struct rpmsg_device_id *id) 466 { 467 return strncmp(id->name, rpdev->id.name, RPMSG_NAME_SIZE) == 0; 468 } 469 470 /* match rpmsg channel and rpmsg driver */ 471 static int rpmsg_dev_match(struct device *dev, struct device_driver *drv) 472 { 473 struct rpmsg_device *rpdev = to_rpmsg_device(dev); 474 struct rpmsg_driver *rpdrv = to_rpmsg_driver(drv); 475 const struct rpmsg_device_id *ids = rpdrv->id_table; 476 unsigned int i; 477 478 if (rpdev->driver_override) 479 return !strcmp(rpdev->driver_override, drv->name); 480 481 if (ids) 482 for (i = 0; ids[i].name[0]; i++) 483 if (rpmsg_id_match(rpdev, &ids[i])) { 484 rpdev->id.driver_data = ids[i].driver_data; 485 return 1; 486 } 487 488 return of_driver_match_device(dev, drv); 489 } 490 491 static int rpmsg_uevent(struct device *dev, struct kobj_uevent_env *env) 492 { 493 struct rpmsg_device *rpdev = to_rpmsg_device(dev); 494 int ret; 495 496 ret = of_device_uevent_modalias(dev, env); 497 if (ret != -ENODEV) 498 return ret; 499 500 return add_uevent_var(env, "MODALIAS=" RPMSG_DEVICE_MODALIAS_FMT, 501 rpdev->id.name); 502 } 503 504 /* 505 * when an rpmsg driver is probed with a channel, we seamlessly create 506 * it an endpoint, binding its rx callback to a unique local rpmsg 507 * address. 508 * 509 * if we need to, we also announce about this channel to the remote 510 * processor (needed in case the driver is exposing an rpmsg service). 511 */ 512 static int rpmsg_dev_probe(struct device *dev) 513 { 514 struct rpmsg_device *rpdev = to_rpmsg_device(dev); 515 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver); 516 struct rpmsg_channel_info chinfo = {}; 517 struct rpmsg_endpoint *ept = NULL; 518 int err; 519 520 err = dev_pm_domain_attach(dev, true); 521 if (err) 522 goto out; 523 524 if (rpdrv->callback) { 525 strncpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); 526 chinfo.src = rpdev->src; 527 chinfo.dst = RPMSG_ADDR_ANY; 528 529 ept = rpmsg_create_ept(rpdev, rpdrv->callback, NULL, chinfo); 530 if (!ept) { 531 dev_err(dev, "failed to create endpoint\n"); 532 err = -ENOMEM; 533 goto out; 534 } 535 536 rpdev->ept = ept; 537 rpdev->src = ept->addr; 538 } 539 540 err = rpdrv->probe(rpdev); 541 if (err) { 542 dev_err(dev, "%s: failed: %d\n", __func__, err); 543 if (ept) 544 rpmsg_destroy_ept(ept); 545 goto out; 546 } 547 548 if (ept && rpdev->ops->announce_create) 549 err = rpdev->ops->announce_create(rpdev); 550 out: 551 return err; 552 } 553 554 static void rpmsg_dev_remove(struct device *dev) 555 { 556 struct rpmsg_device *rpdev = to_rpmsg_device(dev); 557 struct rpmsg_driver *rpdrv = to_rpmsg_driver(rpdev->dev.driver); 558 559 if (rpdev->ops->announce_destroy) 560 rpdev->ops->announce_destroy(rpdev); 561 562 if (rpdrv->remove) 563 rpdrv->remove(rpdev); 564 565 dev_pm_domain_detach(dev, true); 566 567 if (rpdev->ept) 568 rpmsg_destroy_ept(rpdev->ept); 569 } 570 571 static struct bus_type rpmsg_bus = { 572 .name = "rpmsg", 573 .match = rpmsg_dev_match, 574 .dev_groups = rpmsg_dev_groups, 575 .uevent = rpmsg_uevent, 576 .probe = rpmsg_dev_probe, 577 .remove = rpmsg_dev_remove, 578 }; 579 580 int rpmsg_register_device(struct rpmsg_device *rpdev) 581 { 582 struct device *dev = &rpdev->dev; 583 int ret; 584 585 dev_set_name(&rpdev->dev, "%s.%s.%d.%d", dev_name(dev->parent), 586 rpdev->id.name, rpdev->src, rpdev->dst); 587 588 rpdev->dev.bus = &rpmsg_bus; 589 590 ret = device_register(&rpdev->dev); 591 if (ret) { 592 dev_err(dev, "device_register failed: %d\n", ret); 593 put_device(&rpdev->dev); 594 } 595 596 return ret; 597 } 598 EXPORT_SYMBOL(rpmsg_register_device); 599 600 /* 601 * find an existing channel using its name + address properties, 602 * and destroy it 603 */ 604 int rpmsg_unregister_device(struct device *parent, 605 struct rpmsg_channel_info *chinfo) 606 { 607 struct device *dev; 608 609 dev = rpmsg_find_device(parent, chinfo); 610 if (!dev) 611 return -EINVAL; 612 613 device_unregister(dev); 614 615 put_device(dev); 616 617 return 0; 618 } 619 EXPORT_SYMBOL(rpmsg_unregister_device); 620 621 /** 622 * __register_rpmsg_driver() - register an rpmsg driver with the rpmsg bus 623 * @rpdrv: pointer to a struct rpmsg_driver 624 * @owner: owning module/driver 625 * 626 * Returns 0 on success, and an appropriate error value on failure. 627 */ 628 int __register_rpmsg_driver(struct rpmsg_driver *rpdrv, struct module *owner) 629 { 630 rpdrv->drv.bus = &rpmsg_bus; 631 rpdrv->drv.owner = owner; 632 return driver_register(&rpdrv->drv); 633 } 634 EXPORT_SYMBOL(__register_rpmsg_driver); 635 636 /** 637 * unregister_rpmsg_driver() - unregister an rpmsg driver from the rpmsg bus 638 * @rpdrv: pointer to a struct rpmsg_driver 639 * 640 * Returns 0 on success, and an appropriate error value on failure. 641 */ 642 void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv) 643 { 644 driver_unregister(&rpdrv->drv); 645 } 646 EXPORT_SYMBOL(unregister_rpmsg_driver); 647 648 649 static int __init rpmsg_init(void) 650 { 651 int ret; 652 653 ret = bus_register(&rpmsg_bus); 654 if (ret) 655 pr_err("failed to register rpmsg bus: %d\n", ret); 656 657 return ret; 658 } 659 postcore_initcall(rpmsg_init); 660 661 static void __exit rpmsg_fini(void) 662 { 663 bus_unregister(&rpmsg_bus); 664 } 665 module_exit(rpmsg_fini); 666 667 MODULE_DESCRIPTION("remote processor messaging bus"); 668 MODULE_LICENSE("GPL v2"); 669