rpmsg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * Remote processor messaging
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Copyright (C) 2011 Google, Inc.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * * Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * * Neither the name Texas Instruments nor the names of its
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  25. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  26. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  27. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  28. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  29. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  30. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  32. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #ifndef _LINUX_RPMSG_H
  35. #define _LINUX_RPMSG_H
  36. #include <linux/types.h>
  37. #include <linux/device.h>
  38. #include <linux/mod_devicetable.h>
  39. #include <linux/kref.h>
  40. /* The feature bitmap for virtio rpmsg */
  41. #define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
  42. /**
  43. * struct rpmsg_hdr - common header for all rpmsg messages
  44. * @src: source address
  45. * @dst: destination address
  46. * @reserved: reserved for future use
  47. * @len: length of payload (in bytes)
  48. * @flags: message flags
  49. * @data: @len bytes of message payload data
  50. *
  51. * Every message sent(/received) on the rpmsg bus begins with this header.
  52. */
  53. struct rpmsg_hdr {
  54. u32 src;
  55. u32 dst;
  56. u32 reserved;
  57. u16 len;
  58. u16 flags;
  59. u8 data[0];
  60. } __packed;
  61. /**
  62. * struct rpmsg_ns_msg - dynamic name service announcement message
  63. * @name: name of remote service that is published
  64. * @addr: address of remote service that is published
  65. * @flags: indicates whether service is created or destroyed
  66. *
  67. * This message is sent across to publish a new service, or announce
  68. * about its removal. When we receive these messages, an appropriate
  69. * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
  70. * or ->remove() handler of the appropriate rpmsg driver will be invoked
  71. * (if/as-soon-as one is registered).
  72. */
  73. struct rpmsg_ns_msg {
  74. char name[RPMSG_NAME_SIZE];
  75. u32 addr;
  76. u32 flags;
  77. } __packed;
  78. /**
  79. * enum rpmsg_ns_flags - dynamic name service announcement flags
  80. *
  81. * @RPMSG_NS_CREATE: a new remote service was just created
  82. * @RPMSG_NS_DESTROY: a known remote service was just destroyed
  83. */
  84. enum rpmsg_ns_flags {
  85. RPMSG_NS_CREATE = 0,
  86. RPMSG_NS_DESTROY = 1,
  87. };
  88. #define RPMSG_ADDR_ANY 0xFFFFFFFF
  89. struct virtproc_info;
  90. /**
  91. * rpmsg_channel - devices that belong to the rpmsg bus are called channels
  92. * @vrp: the remote processor this channel belongs to
  93. * @dev: the device struct
  94. * @id: device id (used to match between rpmsg drivers and devices)
  95. * @src: local address
  96. * @dst: destination address
  97. * @ept: the rpmsg endpoint of this channel
  98. * @announce: if set, rpmsg will announce the creation/removal of this channel
  99. */
  100. struct rpmsg_channel {
  101. struct virtproc_info *vrp;
  102. struct device dev;
  103. struct rpmsg_device_id id;
  104. u32 src;
  105. u32 dst;
  106. struct rpmsg_endpoint *ept;
  107. bool announce;
  108. };
  109. typedef void (*rpmsg_rx_cb_t)(struct rpmsg_channel *, void *, int, void *, u32);
  110. /**
  111. * struct rpmsg_endpoint - binds a local rpmsg address to its user
  112. * @rpdev: rpmsg channel device
  113. * @refcount: when this drops to zero, the ept is deallocated
  114. * @cb: rx callback handler
  115. * @addr: local rpmsg address
  116. * @priv: private data for the driver's use
  117. *
  118. * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
  119. * it binds an rpmsg address with an rx callback handler.
  120. *
  121. * Simple rpmsg drivers shouldn't use this struct directly, because
  122. * things just work: every rpmsg driver provides an rx callback upon
  123. * registering to the bus, and that callback is then bound to its rpmsg
  124. * address when the driver is probed. When relevant inbound messages arrive
  125. * (i.e. messages which their dst address equals to the src address of
  126. * the rpmsg channel), the driver's handler is invoked to process it.
  127. *
  128. * More complicated drivers though, that do need to allocate additional rpmsg
  129. * addresses, and bind them to different rx callbacks, must explicitly
  130. * create additional endpoints by themselves (see rpmsg_create_ept()).
  131. */
  132. struct rpmsg_endpoint {
  133. struct rpmsg_channel *rpdev;
  134. struct kref refcount;
  135. rpmsg_rx_cb_t cb;
  136. u32 addr;
  137. void *priv;
  138. };
  139. /**
  140. * struct rpmsg_driver - rpmsg driver struct
  141. * @drv: underlying device driver
  142. * @id_table: rpmsg ids serviced by this driver
  143. * @probe: invoked when a matching rpmsg channel (i.e. device) is found
  144. * @remove: invoked when the rpmsg channel is removed
  145. * @callback: invoked when an inbound message is received on the channel
  146. */
  147. struct rpmsg_driver {
  148. struct device_driver drv;
  149. const struct rpmsg_device_id *id_table;
  150. int (*probe)(struct rpmsg_channel *dev);
  151. void (*remove)(struct rpmsg_channel *dev);
  152. void (*callback)(struct rpmsg_channel *, void *, int, void *, u32);
  153. };
  154. int register_rpmsg_device(struct rpmsg_channel *dev);
  155. void unregister_rpmsg_device(struct rpmsg_channel *dev);
  156. int register_rpmsg_driver(struct rpmsg_driver *drv);
  157. void unregister_rpmsg_driver(struct rpmsg_driver *drv);
  158. void rpmsg_destroy_ept(struct rpmsg_endpoint *);
  159. struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *,
  160. rpmsg_rx_cb_t cb, void *priv, u32 addr);
  161. int
  162. rpmsg_send_offchannel_raw(struct rpmsg_channel *, u32, u32, void *, int, bool);
  163. /**
  164. * rpmsg_send() - send a message across to the remote processor
  165. * @rpdev: the rpmsg channel
  166. * @data: payload of message
  167. * @len: length of payload
  168. *
  169. * This function sends @data of length @len on the @rpdev channel.
  170. * The message will be sent to the remote processor which the @rpdev
  171. * channel belongs to, using @rpdev's source and destination addresses.
  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. static inline int rpmsg_send(struct rpmsg_channel *rpdev, void *data, int len)
  181. {
  182. u32 src = rpdev->src, dst = rpdev->dst;
  183. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  184. }
  185. /**
  186. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  187. * @rpdev: the rpmsg channel
  188. * @data: payload of message
  189. * @len: length of payload
  190. * @dst: destination address
  191. *
  192. * This function sends @data of length @len to the remote @dst address.
  193. * The message will be sent to the remote processor which the @rpdev
  194. * channel belongs to, using @rpdev's source address.
  195. * In case there are no TX buffers available, the function will block until
  196. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  197. * happens, -ERESTARTSYS is returned.
  198. *
  199. * Can only be called from process context (for now).
  200. *
  201. * Returns 0 on success and an appropriate error value on failure.
  202. */
  203. static inline
  204. int rpmsg_sendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
  205. {
  206. u32 src = rpdev->src;
  207. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  208. }
  209. /**
  210. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  211. * @rpdev: the rpmsg channel
  212. * @src: source address
  213. * @dst: destination address
  214. * @data: payload of message
  215. * @len: length of payload
  216. *
  217. * This function sends @data of length @len to the remote @dst address,
  218. * and uses @src as the source address.
  219. * The message will be sent to the remote processor which the @rpdev
  220. * channel belongs to.
  221. * In case there are no TX buffers available, the function will block until
  222. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  223. * happens, -ERESTARTSYS is returned.
  224. *
  225. * Can only be called from process context (for now).
  226. *
  227. * Returns 0 on success and an appropriate error value on failure.
  228. */
  229. static inline
  230. int rpmsg_send_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
  231. void *data, int len)
  232. {
  233. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  234. }
  235. /**
  236. * rpmsg_send() - send a message across to the remote processor
  237. * @rpdev: the rpmsg channel
  238. * @data: payload of message
  239. * @len: length of payload
  240. *
  241. * This function sends @data of length @len on the @rpdev channel.
  242. * The message will be sent to the remote processor which the @rpdev
  243. * channel belongs to, using @rpdev's source and destination addresses.
  244. * In case there are no TX buffers available, the function will immediately
  245. * return -ENOMEM without waiting until one becomes available.
  246. *
  247. * Can only be called from process context (for now).
  248. *
  249. * Returns 0 on success and an appropriate error value on failure.
  250. */
  251. static inline
  252. int rpmsg_trysend(struct rpmsg_channel *rpdev, void *data, int len)
  253. {
  254. u32 src = rpdev->src, dst = rpdev->dst;
  255. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  256. }
  257. /**
  258. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  259. * @rpdev: the rpmsg channel
  260. * @data: payload of message
  261. * @len: length of payload
  262. * @dst: destination address
  263. *
  264. * This function sends @data of length @len to the remote @dst address.
  265. * The message will be sent to the remote processor which the @rpdev
  266. * channel belongs to, using @rpdev's source address.
  267. * In case there are no TX buffers available, the function will immediately
  268. * return -ENOMEM without waiting until one becomes available.
  269. *
  270. * Can only be called from process context (for now).
  271. *
  272. * Returns 0 on success and an appropriate error value on failure.
  273. */
  274. static inline
  275. int rpmsg_trysendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
  276. {
  277. u32 src = rpdev->src;
  278. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  279. }
  280. /**
  281. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  282. * @rpdev: the rpmsg channel
  283. * @src: source address
  284. * @dst: destination address
  285. * @data: payload of message
  286. * @len: length of payload
  287. *
  288. * This function sends @data of length @len to the remote @dst address,
  289. * and uses @src as the source address.
  290. * The message will be sent to the remote processor which the @rpdev
  291. * channel belongs to.
  292. * In case there are no TX buffers available, the function will immediately
  293. * return -ENOMEM without waiting until one becomes available.
  294. *
  295. * Can only be called from process context (for now).
  296. *
  297. * Returns 0 on success and an appropriate error value on failure.
  298. */
  299. static inline
  300. int rpmsg_trysend_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
  301. void *data, int len)
  302. {
  303. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  304. }
  305. #endif /* _LINUX_RPMSG_H */