rpmsg_core.c 17 KB

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