rpmsg_core.c 17 KB

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