drv_ipc_fifo.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _DRV_IPC_FIFO_H_
  13. #define _DRV_IPC_FIFO_H_
  14. #include <stdint.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /**
  19. * \brief flag to indicate the fifo is ready
  20. */
  21. #define DRV_IPC_FIFO_READY (1 << 0)
  22. /**
  23. * \brief forward declaration
  24. */
  25. struct osiFifo;
  26. /**
  27. * \brief IPC fifo from the point of view of master
  28. *
  29. * There are 2 fifos for each IPC channel, one for send and the other
  30. * for receive. The fifo control data struct is located in the shared
  31. * memory. So, the fields of send for one side is the fields of receive
  32. * for the other side. This is the data struct for master.
  33. *
  34. * The data struct in shared memory should be 32 bytes aligned.
  35. *
  36. * For master side, it is recommened to define:
  37. * \code{.cpp}
  38. * typedef drvIpcFifoMasterCtrl_t drvIpcFifoCtrl_t;
  39. * \endcode
  40. */
  41. typedef struct
  42. {
  43. struct
  44. {
  45. uint32_t status; ///< status
  46. uint32_t send_base; ///< send fifo base address
  47. uint32_t recv_base; ///< receive fifo base address
  48. uint32_t send_size; ///< send fifo size
  49. uint32_t recv_size; ///< receive fifo size
  50. uint32_t send_wr; ///< send fifo write position
  51. uint32_t recv_rd; ///< receive fifo read position
  52. uint32_t reserved[1]; ///< reserved
  53. } th; ///< this side (master) writable struct
  54. struct
  55. {
  56. uint32_t status; ///< status
  57. uint32_t send_base; ///< send fifo base address
  58. uint32_t recv_base; ///< receive fifo base address
  59. uint32_t send_size; ///< send fifo size
  60. uint32_t recv_size; ///< receive fifo size
  61. uint32_t send_rd; ///< send fifo read position
  62. uint32_t recv_wr; ///< receive fifo write position
  63. uint32_t reserved[1]; ///< reserved
  64. } peer; ///< peer side (slave) writable struct
  65. } drvIpcFifoMasterCtrl_t;
  66. /**
  67. * \brief IPC fifo from the point of view of slave
  68. *
  69. * For slave side, it is recommened to define:
  70. * \code{.cpp}
  71. * typedef drvIpcFifoSlaveCtrl_t drvIpcFifoCtrl_t;
  72. * \endcode
  73. *
  74. * Based on \p drvIpcFifoCtrl_t, the source codes for master and slave
  75. * are identical.
  76. */
  77. typedef struct
  78. {
  79. struct
  80. {
  81. uint32_t status; ///< status
  82. uint32_t recv_base; ///< receive fifo base address
  83. uint32_t send_base; ///< send fifo base address
  84. uint32_t recv_size; ///< receive fifo size
  85. uint32_t send_size; ///< send fifo size
  86. uint32_t recv_wr; ///< receive fifo write position
  87. uint32_t send_rd; ///< send fifo read position
  88. uint32_t reserved[1]; ///< reserved
  89. } peer; ///< peer side (master) writable struct
  90. struct
  91. {
  92. uint32_t status; ///< status
  93. uint32_t recv_base; ///< receive fifo base address
  94. uint32_t send_base; ///< send fifo base address
  95. uint32_t recv_size; ///< receive fifo size
  96. uint32_t send_size; ///< send fifo size
  97. uint32_t recv_rd; ///< receive fifo write position
  98. uint32_t send_wr; ///< send fifo read position
  99. uint32_t reserved[1]; ///< reserved
  100. } th; ///< peer side (slave) writable struct
  101. } drvIpcFifoSlaveCtrl_t;
  102. /**
  103. * \brief ipc command header
  104. *
  105. * Usually, channel 0 is the control channel. And the data in control
  106. * channel is command packets.
  107. *
  108. * The command packet header is compatible with RPC packet. And \p opcode
  109. * shouldn't conflict with RPC when it is used for other purpose. Also,
  110. * multiple purposes can be mixed in one channel.
  111. *
  112. * \p size is required to be aligned to 8 bytes. When needed, the memory
  113. * in shared memory can be casted to \p drvIpcCmdHeader_t data struct.
  114. */
  115. typedef struct
  116. {
  117. uint32_t opcode; ///< operation in command header
  118. uint32_t size; ///< the whole command packet size, including header
  119. } drvIpcCmdHeader_t;
  120. /**
  121. * \brief initialze ipc fifo (master)
  122. *
  123. * \param pctrl ipc fifo control
  124. * \param send_base send fifo base address
  125. * \param send_size send fifo size
  126. * \param recv_base receive fifo base address
  127. * \param recv_size receive fifo size
  128. */
  129. void drvIpcFifoMasterInit(drvIpcFifoMasterCtrl_t *pctrl,
  130. unsigned send_base, unsigned send_size,
  131. unsigned recv_base, unsigned recv_size);
  132. /**
  133. * \brief ipc fifo read available byte count (master)
  134. *
  135. * \param pctrl ipc fifo control
  136. */
  137. unsigned drvIpcFifoMasterAvail(drvIpcFifoMasterCtrl_t *pctrl);
  138. /**
  139. * \brief ipc fifo write space byte count (master)
  140. *
  141. * \param pctrl ipc fifo control
  142. */
  143. unsigned drvIpcFifoMasterSpace(drvIpcFifoMasterCtrl_t *pctrl);
  144. /**
  145. * \brief read from ipc fifo (master)
  146. *
  147. * The returned real read size may be smaller than \p size.
  148. *
  149. * It is not thread safe. Thread safe shall be considered by caller.
  150. *
  151. * \param pctrl ipc fifo control
  152. * \param data memory for read
  153. * \param size memory size
  154. * \return real read size
  155. */
  156. int drvIpcFifoMasterRead(drvIpcFifoMasterCtrl_t *pctrl, void *data, unsigned size);
  157. /**
  158. * \brief write to ipc fifo (master)
  159. *
  160. * The returned real written size may be smaller than \p size.
  161. *
  162. * It is not thread safe. Thread safe shall be considered by caller.
  163. *
  164. * \param pctrl ipc fifo control
  165. * \param data data to be written
  166. * \param size data size
  167. * \return real written size
  168. */
  169. int drvIpcFifoMasterWrite(drvIpcFifoMasterCtrl_t *pctrl, const void *data, unsigned size);
  170. /**
  171. * \brief read from ipc fifo and write to local fifo (master)
  172. *
  173. * It is not thread safe. Thread safe shall be considered by caller.
  174. *
  175. * \param pctrl ipc fifo control
  176. * \param fifo local fifo
  177. * \return real read size
  178. */
  179. int drvIpcFifoMasterReadToFifo(drvIpcFifoMasterCtrl_t *pctrl, struct osiFifo *fifo);
  180. /**
  181. * \brief write to ipc fifo and read from local fifo (master)
  182. *
  183. * It is not thread safe. Thread safe shall be considered by caller.
  184. *
  185. * \param pctrl ipc fifo control
  186. * \param fifo local fifo
  187. * \return real written size
  188. */
  189. int drvIpcFifoMasterWriteFromFifo(drvIpcFifoMasterCtrl_t *pctrl, struct osiFifo *fifo);
  190. /**
  191. * \brief initialze ipc fifo (slave)
  192. *
  193. * \param pctrl ipc fifo control
  194. */
  195. void drvIpcFifoSlaveInit(drvIpcFifoSlaveCtrl_t *pctrl);
  196. /**
  197. * \brief ipc fifo read available byte count (slave)
  198. *
  199. * \param pctrl ipc fifo control
  200. */
  201. unsigned drvIpcFifoSlaveAvail(drvIpcFifoSlaveCtrl_t *pctrl);
  202. /**
  203. * \brief ipc fifo write space byte count (slave)
  204. *
  205. * \param pctrl ipc fifo control
  206. */
  207. unsigned drvIpcFifoSlaveSpace(drvIpcFifoSlaveCtrl_t *pctrl);
  208. /**
  209. * \brief read from ipc fifo (slave)
  210. *
  211. * It is similar to \p drvIpcFifoMasterRead, just the fifo control is
  212. * interpreted as slave.
  213. *
  214. * \param pctrl ipc fifo control
  215. * \param data memory for read
  216. * \param size memory size
  217. * \return real read size
  218. */
  219. int drvIpcFifoSlaveRead(drvIpcFifoSlaveCtrl_t *pctrl, void *data, unsigned size);
  220. /**
  221. * \brief write to ipc fifo (slave)
  222. *
  223. * It is similar to \p drvIpcFifoMasterWrite, just the fifo control is
  224. * interpreted as slave.
  225. *
  226. * \param pctrl ipc fifo control
  227. * \param data data to be written
  228. * \param size data size
  229. * \return real written size
  230. */
  231. int drvIpcFifoSlaveWrite(drvIpcFifoSlaveCtrl_t *pctrl, const void *data, unsigned size);
  232. /**
  233. * \brief read from ipc fifo and write to local fifo (slave)
  234. *
  235. * It is similar to \p drvIpcFifoMasterReadToFifo, just the fifo control is
  236. * interpreted as slave.
  237. *
  238. * \param pctrl ipc fifo control
  239. * \param fifo local fifo
  240. * \return real read size
  241. */
  242. int drvIpcFifoSlaveReadToFifo(drvIpcFifoSlaveCtrl_t *pctrl, struct osiFifo *fifo);
  243. /**
  244. * \brief write to ipc fifo and read from local fifo (slave)
  245. *
  246. * It is not thread safe. Thread safe shall be considered by caller.
  247. *
  248. * \param pctrl ipc fifo control
  249. * \param fifo local fifo
  250. * \return real written size
  251. */
  252. int drvIpcFifoSlaveWriteFromFifo(drvIpcFifoSlaveCtrl_t *pctrl, struct osiFifo *fifo);
  253. #ifdef __cplusplus
  254. }
  255. #endif
  256. #endif