rpmsg.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. Remote Processor Messaging (rpmsg) Framework
  2. Note: this document describes the rpmsg bus and how to write rpmsg drivers.
  3. To learn how to add rpmsg support for new platforms, check out remoteproc.txt
  4. (also a resident of Documentation/).
  5. 1. Introduction
  6. Modern SoCs typically employ heterogeneous remote processor devices in
  7. asymmetric multiprocessing (AMP) configurations, which may be running
  8. different instances of operating system, whether it's Linux or any other
  9. flavor of real-time OS.
  10. OMAP4, for example, has dual Cortex-A9, dual Cortex-M3 and a C64x+ DSP.
  11. Typically, the dual cortex-A9 is running Linux in a SMP configuration,
  12. and each of the other three cores (two M3 cores and a DSP) is running
  13. its own instance of RTOS in an AMP configuration.
  14. Typically AMP remote processors employ dedicated DSP codecs and multimedia
  15. hardware accelerators, and therefore are often used to offload CPU-intensive
  16. multimedia tasks from the main application processor.
  17. These remote processors could also be used to control latency-sensitive
  18. sensors, drive random hardware blocks, or just perform background tasks
  19. while the main CPU is idling.
  20. Users of those remote processors can either be userland apps (e.g. multimedia
  21. frameworks talking with remote OMX components) or kernel drivers (controlling
  22. hardware accessible only by the remote processor, reserving kernel-controlled
  23. resources on behalf of the remote processor, etc..).
  24. Rpmsg is a virtio-based messaging bus that allows kernel drivers to communicate
  25. with remote processors available on the system. In turn, drivers could then
  26. expose appropriate user space interfaces, if needed.
  27. When writing a driver that exposes rpmsg communication to userland, please
  28. keep in mind that remote processors might have direct access to the
  29. system's physical memory and other sensitive hardware resources (e.g. on
  30. OMAP4, remote cores and hardware accelerators may have direct access to the
  31. physical memory, gpio banks, dma controllers, i2c bus, gptimers, mailbox
  32. devices, hwspinlocks, etc..). Moreover, those remote processors might be
  33. running RTOS where every task can access the entire memory/devices exposed
  34. to the processor. To minimize the risks of rogue (or buggy) userland code
  35. exploiting remote bugs, and by that taking over the system, it is often
  36. desired to limit userland to specific rpmsg channels (see definition below)
  37. it can send messages on, and if possible, minimize how much control
  38. it has over the content of the messages.
  39. Every rpmsg device is a communication channel with a remote processor (thus
  40. rpmsg devices are called channels). Channels are identified by a textual name
  41. and have a local ("source") rpmsg address, and remote ("destination") rpmsg
  42. address.
  43. When a driver starts listening on a channel, its rx callback is bound with
  44. a unique rpmsg local address (a 32-bit integer). This way when inbound messages
  45. arrive, the rpmsg core dispatches them to the appropriate driver according
  46. to their destination address (this is done by invoking the driver's rx handler
  47. with the payload of the inbound message).
  48. 2. User API
  49. int rpmsg_send(struct rpmsg_channel *rpdev, void *data, int len);
  50. - sends a message across to the remote processor on a given channel.
  51. The caller should specify the channel, the data it wants to send,
  52. and its length (in bytes). The message will be sent on the specified
  53. channel, i.e. its source and destination address fields will be
  54. set to the channel's src and dst addresses.
  55. In case there are no TX buffers available, the function will block until
  56. one becomes available (i.e. until the remote processor consumes
  57. a tx buffer and puts it back on virtio's used descriptor ring),
  58. or a timeout of 15 seconds elapses. When the latter happens,
  59. -ERESTARTSYS is returned.
  60. The function can only be called from a process context (for now).
  61. Returns 0 on success and an appropriate error value on failure.
  62. int rpmsg_sendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst);
  63. - sends a message across to the remote processor on a given channel,
  64. to a destination address provided by the caller.
  65. The caller should specify the channel, the data it wants to send,
  66. its length (in bytes), and an explicit destination address.
  67. The message will then be sent to the remote processor to which the
  68. channel belongs, using the channel's src address, and the user-provided
  69. dst address (thus the channel's dst address will be ignored).
  70. In case there are no TX buffers available, the function will block until
  71. one becomes available (i.e. until the remote processor consumes
  72. a tx buffer and puts it back on virtio's used descriptor ring),
  73. or a timeout of 15 seconds elapses. When the latter happens,
  74. -ERESTARTSYS is returned.
  75. The function can only be called from a process context (for now).
  76. Returns 0 on success and an appropriate error value on failure.
  77. int rpmsg_send_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
  78. void *data, int len);
  79. - sends a message across to the remote processor, using the src and dst
  80. addresses provided by the user.
  81. The caller should specify the channel, the data it wants to send,
  82. its length (in bytes), and explicit source and destination addresses.
  83. The message will then be sent to the remote processor to which the
  84. channel belongs, but the channel's src and dst addresses will be
  85. ignored (and the user-provided addresses will be used instead).
  86. In case there are no TX buffers available, the function will block until
  87. one becomes available (i.e. until the remote processor consumes
  88. a tx buffer and puts it back on virtio's used descriptor ring),
  89. or a timeout of 15 seconds elapses. When the latter happens,
  90. -ERESTARTSYS is returned.
  91. The function can only be called from a process context (for now).
  92. Returns 0 on success and an appropriate error value on failure.
  93. int rpmsg_trysend(struct rpmsg_channel *rpdev, void *data, int len);
  94. - sends a message across to the remote processor on a given channel.
  95. The caller should specify the channel, the data it wants to send,
  96. and its length (in bytes). The message will be sent on the specified
  97. channel, i.e. its source and destination address fields will be
  98. set to the channel's src and dst addresses.
  99. In case there are no TX buffers available, the function will immediately
  100. return -ENOMEM without waiting until one becomes available.
  101. The function can only be called from a process context (for now).
  102. Returns 0 on success and an appropriate error value on failure.
  103. int rpmsg_trysendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
  104. - sends a message across to the remote processor on a given channel,
  105. to a destination address provided by the user.
  106. The user should specify the channel, the data it wants to send,
  107. its length (in bytes), and an explicit destination address.
  108. The message will then be sent to the remote processor to which the
  109. channel belongs, using the channel's src address, and the user-provided
  110. dst address (thus the channel's dst address will be ignored).
  111. In case there are no TX buffers available, the function will immediately
  112. return -ENOMEM without waiting until one becomes available.
  113. The function can only be called from a process context (for now).
  114. Returns 0 on success and an appropriate error value on failure.
  115. int rpmsg_trysend_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
  116. void *data, int len);
  117. - sends a message across to the remote processor, using source and
  118. destination addresses provided by the user.
  119. The user should specify the channel, the data it wants to send,
  120. its length (in bytes), and explicit source and destination addresses.
  121. The message will then be sent to the remote processor to which the
  122. channel belongs, but the channel's src and dst addresses will be
  123. ignored (and the user-provided addresses will be used instead).
  124. In case there are no TX buffers available, the function will immediately
  125. return -ENOMEM without waiting until one becomes available.
  126. The function can only be called from a process context (for now).
  127. Returns 0 on success and an appropriate error value on failure.
  128. struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *rpdev,
  129. void (*cb)(struct rpmsg_channel *, void *, int, void *, u32),
  130. void *priv, u32 addr);
  131. - every rpmsg address in the system is bound to an rx callback (so when
  132. inbound messages arrive, they are dispatched by the rpmsg bus using the
  133. appropriate callback handler) by means of an rpmsg_endpoint struct.
  134. This function allows drivers to create such an endpoint, and by that,
  135. bind a callback, and possibly some private data too, to an rpmsg address
  136. (either one that is known in advance, or one that will be dynamically
  137. assigned for them).
  138. Simple rpmsg drivers need not call rpmsg_create_ept, because an endpoint
  139. is already created for them when they are probed by the rpmsg bus
  140. (using the rx callback they provide when they registered to the rpmsg bus).
  141. So things should just work for simple drivers: they already have an
  142. endpoint, their rx callback is bound to their rpmsg address, and when
  143. relevant inbound messages arrive (i.e. messages which their dst address
  144. equals to the src address of their rpmsg channel), the driver's handler
  145. is invoked to process it.
  146. That said, more complicated drivers might do need to allocate
  147. additional rpmsg addresses, and bind them to different rx callbacks.
  148. To accomplish that, those drivers need to call this function.
  149. Drivers should provide their channel (so the new endpoint would bind
  150. to the same remote processor their channel belongs to), an rx callback
  151. function, an optional private data (which is provided back when the
  152. rx callback is invoked), and an address they want to bind with the
  153. callback. If addr is RPMSG_ADDR_ANY, then rpmsg_create_ept will
  154. dynamically assign them an available rpmsg address (drivers should have
  155. a very good reason why not to always use RPMSG_ADDR_ANY here).
  156. Returns a pointer to the endpoint on success, or NULL on error.
  157. void rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
  158. - destroys an existing rpmsg endpoint. user should provide a pointer
  159. to an rpmsg endpoint that was previously created with rpmsg_create_ept().
  160. int register_rpmsg_driver(struct rpmsg_driver *rpdrv);
  161. - registers an rpmsg driver with the rpmsg bus. user should provide
  162. a pointer to an rpmsg_driver struct, which contains the driver's
  163. ->probe() and ->remove() functions, an rx callback, and an id_table
  164. specifying the names of the channels this driver is interested to
  165. be probed with.
  166. void unregister_rpmsg_driver(struct rpmsg_driver *rpdrv);
  167. - unregisters an rpmsg driver from the rpmsg bus. user should provide
  168. a pointer to a previously-registered rpmsg_driver struct.
  169. Returns 0 on success, and an appropriate error value on failure.
  170. 3. Typical usage
  171. The following is a simple rpmsg driver, that sends an "hello!" message
  172. on probe(), and whenever it receives an incoming message, it dumps its
  173. content to the console.
  174. #include <linux/kernel.h>
  175. #include <linux/module.h>
  176. #include <linux/rpmsg.h>
  177. static void rpmsg_sample_cb(struct rpmsg_channel *rpdev, void *data, int len,
  178. void *priv, u32 src)
  179. {
  180. print_hex_dump(KERN_INFO, "incoming message:", DUMP_PREFIX_NONE,
  181. 16, 1, data, len, true);
  182. }
  183. static int rpmsg_sample_probe(struct rpmsg_channel *rpdev)
  184. {
  185. int err;
  186. dev_info(&rpdev->dev, "chnl: 0x%x -> 0x%x\n", rpdev->src, rpdev->dst);
  187. /* send a message on our channel */
  188. err = rpmsg_send(rpdev, "hello!", 6);
  189. if (err) {
  190. pr_err("rpmsg_send failed: %d\n", err);
  191. return err;
  192. }
  193. return 0;
  194. }
  195. static void rpmsg_sample_remove(struct rpmsg_channel *rpdev)
  196. {
  197. dev_info(&rpdev->dev, "rpmsg sample client driver is removed\n");
  198. }
  199. static struct rpmsg_device_id rpmsg_driver_sample_id_table[] = {
  200. { .name = "rpmsg-client-sample" },
  201. { },
  202. };
  203. MODULE_DEVICE_TABLE(rpmsg, rpmsg_driver_sample_id_table);
  204. static struct rpmsg_driver rpmsg_sample_client = {
  205. .drv.name = KBUILD_MODNAME,
  206. .drv.owner = THIS_MODULE,
  207. .id_table = rpmsg_driver_sample_id_table,
  208. .probe = rpmsg_sample_probe,
  209. .callback = rpmsg_sample_cb,
  210. .remove = rpmsg_sample_remove,
  211. };
  212. static int __init init(void)
  213. {
  214. return register_rpmsg_driver(&rpmsg_sample_client);
  215. }
  216. module_init(init);
  217. static void __exit fini(void)
  218. {
  219. unregister_rpmsg_driver(&rpmsg_sample_client);
  220. }
  221. module_exit(fini);
  222. Note: a similar sample which can be built and loaded can be found
  223. in samples/rpmsg/.
  224. 4. Allocations of rpmsg channels:
  225. At this point we only support dynamic allocations of rpmsg channels.
  226. This is possible only with remote processors that have the VIRTIO_RPMSG_F_NS
  227. virtio device feature set. This feature bit means that the remote
  228. processor supports dynamic name service announcement messages.
  229. When this feature is enabled, creation of rpmsg devices (i.e. channels)
  230. is completely dynamic: the remote processor announces the existence of a
  231. remote rpmsg service by sending a name service message (which contains
  232. the name and rpmsg addr of the remote service, see struct rpmsg_ns_msg).
  233. This message is then handled by the rpmsg bus, which in turn dynamically
  234. creates and registers an rpmsg channel (which represents the remote service).
  235. If/when a relevant rpmsg driver is registered, it will be immediately probed
  236. by the bus, and can then start sending messages to the remote service.
  237. The plan is also to add static creation of rpmsg channels via the virtio
  238. config space, but it's not implemented yet.