rpmsg.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #include <linux/mutex.h>
  41. /* The feature bitmap for virtio rpmsg */
  42. #define VIRTIO_RPMSG_F_NS 0 /* RP supports name service notifications */
  43. /**
  44. * struct rpmsg_hdr - common header for all rpmsg messages
  45. * @src: source address
  46. * @dst: destination address
  47. * @reserved: reserved for future use
  48. * @len: length of payload (in bytes)
  49. * @flags: message flags
  50. * @data: @len bytes of message payload data
  51. *
  52. * Every message sent(/received) on the rpmsg bus begins with this header.
  53. */
  54. struct rpmsg_hdr {
  55. u32 src;
  56. u32 dst;
  57. u32 reserved;
  58. u16 len;
  59. u16 flags;
  60. u8 data[0];
  61. } __packed;
  62. /**
  63. * struct rpmsg_ns_msg - dynamic name service announcement message
  64. * @name: name of remote service that is published
  65. * @addr: address of remote service that is published
  66. * @flags: indicates whether service is created or destroyed
  67. *
  68. * This message is sent across to publish a new service, or announce
  69. * about its removal. When we receive these messages, an appropriate
  70. * rpmsg channel (i.e device) is created/destroyed. In turn, the ->probe()
  71. * or ->remove() handler of the appropriate rpmsg driver will be invoked
  72. * (if/as-soon-as one is registered).
  73. */
  74. struct rpmsg_ns_msg {
  75. char name[RPMSG_NAME_SIZE];
  76. u32 addr;
  77. u32 flags;
  78. } __packed;
  79. /**
  80. * enum rpmsg_ns_flags - dynamic name service announcement flags
  81. *
  82. * @RPMSG_NS_CREATE: a new remote service was just created
  83. * @RPMSG_NS_DESTROY: a known remote service was just destroyed
  84. */
  85. enum rpmsg_ns_flags {
  86. RPMSG_NS_CREATE = 0,
  87. RPMSG_NS_DESTROY = 1,
  88. };
  89. #define RPMSG_ADDR_ANY 0xFFFFFFFF
  90. struct virtproc_info;
  91. /**
  92. * rpmsg_channel - devices that belong to the rpmsg bus are called channels
  93. * @vrp: the remote processor this channel belongs to
  94. * @dev: the device struct
  95. * @id: device id (used to match between rpmsg drivers and devices)
  96. * @src: local address
  97. * @dst: destination address
  98. * @ept: the rpmsg endpoint of this channel
  99. * @announce: if set, rpmsg will announce the creation/removal of this channel
  100. */
  101. struct rpmsg_channel {
  102. struct virtproc_info *vrp;
  103. struct device dev;
  104. struct rpmsg_device_id id;
  105. u32 src;
  106. u32 dst;
  107. struct rpmsg_endpoint *ept;
  108. bool announce;
  109. };
  110. typedef void (*rpmsg_rx_cb_t)(struct rpmsg_channel *, void *, int, void *, u32);
  111. /**
  112. * struct rpmsg_endpoint - binds a local rpmsg address to its user
  113. * @rpdev: rpmsg channel device
  114. * @refcount: when this drops to zero, the ept is deallocated
  115. * @cb: rx callback handler
  116. * @cb_lock: must be taken before accessing/changing @cb
  117. * @addr: local rpmsg address
  118. * @priv: private data for the driver's use
  119. *
  120. * In essence, an rpmsg endpoint represents a listener on the rpmsg bus, as
  121. * it binds an rpmsg address with an rx callback handler.
  122. *
  123. * Simple rpmsg drivers shouldn't use this struct directly, because
  124. * things just work: every rpmsg driver provides an rx callback upon
  125. * registering to the bus, and that callback is then bound to its rpmsg
  126. * address when the driver is probed. When relevant inbound messages arrive
  127. * (i.e. messages which their dst address equals to the src address of
  128. * the rpmsg channel), the driver's handler is invoked to process it.
  129. *
  130. * More complicated drivers though, that do need to allocate additional rpmsg
  131. * addresses, and bind them to different rx callbacks, must explicitly
  132. * create additional endpoints by themselves (see rpmsg_create_ept()).
  133. */
  134. struct rpmsg_endpoint {
  135. struct rpmsg_channel *rpdev;
  136. struct kref refcount;
  137. rpmsg_rx_cb_t cb;
  138. struct mutex cb_lock;
  139. u32 addr;
  140. void *priv;
  141. };
  142. /**
  143. * struct rpmsg_driver - rpmsg driver struct
  144. * @drv: underlying device driver
  145. * @id_table: rpmsg ids serviced by this driver
  146. * @probe: invoked when a matching rpmsg channel (i.e. device) is found
  147. * @remove: invoked when the rpmsg channel is removed
  148. * @callback: invoked when an inbound message is received on the channel
  149. */
  150. struct rpmsg_driver {
  151. struct device_driver drv;
  152. const struct rpmsg_device_id *id_table;
  153. int (*probe)(struct rpmsg_channel *dev);
  154. void (*remove)(struct rpmsg_channel *dev);
  155. void (*callback)(struct rpmsg_channel *, void *, int, void *, u32);
  156. };
  157. int register_rpmsg_device(struct rpmsg_channel *dev);
  158. void unregister_rpmsg_device(struct rpmsg_channel *dev);
  159. int register_rpmsg_driver(struct rpmsg_driver *drv);
  160. void unregister_rpmsg_driver(struct rpmsg_driver *drv);
  161. void rpmsg_destroy_ept(struct rpmsg_endpoint *);
  162. struct rpmsg_endpoint *rpmsg_create_ept(struct rpmsg_channel *,
  163. rpmsg_rx_cb_t cb, void *priv, u32 addr);
  164. int
  165. rpmsg_send_offchannel_raw(struct rpmsg_channel *, u32, u32, void *, int, bool);
  166. /**
  167. * rpmsg_send() - send a message across to the remote processor
  168. * @rpdev: the rpmsg channel
  169. * @data: payload of message
  170. * @len: length of payload
  171. *
  172. * This function sends @data of length @len on the @rpdev channel.
  173. * The message will be sent to the remote processor which the @rpdev
  174. * channel belongs to, using @rpdev's source and destination addresses.
  175. * In case there are no TX buffers available, the function will block until
  176. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  177. * happens, -ERESTARTSYS is returned.
  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. static inline int rpmsg_send(struct rpmsg_channel *rpdev, void *data, int len)
  184. {
  185. u32 src = rpdev->src, dst = rpdev->dst;
  186. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  187. }
  188. /**
  189. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  190. * @rpdev: the rpmsg channel
  191. * @data: payload of message
  192. * @len: length of payload
  193. * @dst: destination address
  194. *
  195. * This function sends @data of length @len to the remote @dst address.
  196. * The message will be sent to the remote processor which the @rpdev
  197. * channel belongs to, using @rpdev's source address.
  198. * In case there are no TX buffers available, the function will block until
  199. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  200. * happens, -ERESTARTSYS is returned.
  201. *
  202. * Can only be called from process context (for now).
  203. *
  204. * Returns 0 on success and an appropriate error value on failure.
  205. */
  206. static inline
  207. int rpmsg_sendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
  208. {
  209. u32 src = rpdev->src;
  210. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  211. }
  212. /**
  213. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  214. * @rpdev: the rpmsg channel
  215. * @src: source address
  216. * @dst: destination address
  217. * @data: payload of message
  218. * @len: length of payload
  219. *
  220. * This function sends @data of length @len to the remote @dst address,
  221. * and uses @src as the source address.
  222. * The message will be sent to the remote processor which the @rpdev
  223. * channel belongs to.
  224. * In case there are no TX buffers available, the function will block until
  225. * one becomes available, or a timeout of 15 seconds elapses. When the latter
  226. * happens, -ERESTARTSYS is returned.
  227. *
  228. * Can only be called from process context (for now).
  229. *
  230. * Returns 0 on success and an appropriate error value on failure.
  231. */
  232. static inline
  233. int rpmsg_send_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
  234. void *data, int len)
  235. {
  236. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, true);
  237. }
  238. /**
  239. * rpmsg_send() - send a message across to the remote processor
  240. * @rpdev: the rpmsg channel
  241. * @data: payload of message
  242. * @len: length of payload
  243. *
  244. * This function sends @data of length @len on the @rpdev channel.
  245. * The message will be sent to the remote processor which the @rpdev
  246. * channel belongs to, using @rpdev's source and destination addresses.
  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. static inline
  255. int rpmsg_trysend(struct rpmsg_channel *rpdev, void *data, int len)
  256. {
  257. u32 src = rpdev->src, dst = rpdev->dst;
  258. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  259. }
  260. /**
  261. * rpmsg_sendto() - send a message across to the remote processor, specify dst
  262. * @rpdev: the rpmsg channel
  263. * @data: payload of message
  264. * @len: length of payload
  265. * @dst: destination address
  266. *
  267. * This function sends @data of length @len to the remote @dst address.
  268. * The message will be sent to the remote processor which the @rpdev
  269. * channel belongs to, using @rpdev's source address.
  270. * In case there are no TX buffers available, the function will immediately
  271. * return -ENOMEM without waiting until one becomes available.
  272. *
  273. * Can only be called from process context (for now).
  274. *
  275. * Returns 0 on success and an appropriate error value on failure.
  276. */
  277. static inline
  278. int rpmsg_trysendto(struct rpmsg_channel *rpdev, void *data, int len, u32 dst)
  279. {
  280. u32 src = rpdev->src;
  281. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  282. }
  283. /**
  284. * rpmsg_send_offchannel() - send a message using explicit src/dst addresses
  285. * @rpdev: the rpmsg channel
  286. * @src: source address
  287. * @dst: destination address
  288. * @data: payload of message
  289. * @len: length of payload
  290. *
  291. * This function sends @data of length @len to the remote @dst address,
  292. * and uses @src as the source address.
  293. * The message will be sent to the remote processor which the @rpdev
  294. * channel belongs to.
  295. * In case there are no TX buffers available, the function will immediately
  296. * return -ENOMEM without waiting until one becomes available.
  297. *
  298. * Can only be called from process context (for now).
  299. *
  300. * Returns 0 on success and an appropriate error value on failure.
  301. */
  302. static inline
  303. int rpmsg_trysend_offchannel(struct rpmsg_channel *rpdev, u32 src, u32 dst,
  304. void *data, int len)
  305. {
  306. return rpmsg_send_offchannel_raw(rpdev, src, dst, data, len, false);
  307. }
  308. #endif /* _LINUX_RPMSG_H */