ql_ipc.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef TRUSTY_QL_IPC_H_
  25. #define TRUSTY_QL_IPC_H_
  26. /*
  27. * handle_t is an opaque 32 bit value that is used to reference an
  28. * Trusty IPC channel
  29. */
  30. typedef uint32_t handle_t;
  31. #define INVALID_IPC_HANDLE 0
  32. /*
  33. * Error codes returned by Trusty IPC device function calls
  34. */
  35. enum trusty_err {
  36. TRUSTY_ERR_NONE = 0,
  37. TRUSTY_ERR_GENERIC = -1,
  38. TRUSTY_ERR_NOT_SUPPORTED = -2,
  39. TRUSTY_ERR_NO_MEMORY = -3,
  40. TRUSTY_ERR_INVALID_ARGS = -4,
  41. TRUSTY_ERR_SECOS_ERR = -5,
  42. TRUSTY_ERR_MSG_TOO_BIG = -6,
  43. TRUSTY_ERR_NO_MSG = -7,
  44. TRUSTY_ERR_CHANNEL_CLOSED = -8,
  45. TRUSTY_ERR_SEND_BLOCKED = -9,
  46. };
  47. /*
  48. * Return codes for successful Trusty IPC events (failures return trusty_err)
  49. */
  50. enum trusty_event_result {
  51. TRUSTY_EVENT_HANDLED = 1,
  52. TRUSTY_EVENT_NONE = 2,
  53. };
  54. /*
  55. * Combination of these values are used for the event field
  56. * of trusty_ipc_event structure.
  57. */
  58. enum trusty_ipc_event_type {
  59. IPC_HANDLE_POLL_NONE = 0x0,
  60. IPC_HANDLE_POLL_READY = 0x1,
  61. IPC_HANDLE_POLL_ERROR = 0x2,
  62. IPC_HANDLE_POLL_HUP = 0x4,
  63. IPC_HANDLE_POLL_MSG = 0x8,
  64. IPC_HANDLE_POLL_SEND_UNBLOCKED = 0x10,
  65. };
  66. struct trusty_dev;
  67. struct trusty_ipc_chan;
  68. /*
  69. * Trusty IPC event
  70. *
  71. * @event: event type
  72. * @handle: handle this event is related to
  73. * @cookie: cookie associated with handle
  74. */
  75. struct trusty_ipc_event {
  76. uint32_t event;
  77. uint32_t handle;
  78. uint64_t cookie;
  79. };
  80. struct trusty_ipc_iovec {
  81. void* base;
  82. size_t len;
  83. };
  84. /*
  85. * Trusty IPC device
  86. *
  87. * @buf_vaddr: virtual address of shared buffer associated with device
  88. * @buf_size: size of shared buffer
  89. * @buf_ns: physical address info of shared buffer
  90. * @tdev: trusty device
  91. */
  92. struct trusty_ipc_dev {
  93. void* buf_vaddr;
  94. size_t buf_size;
  95. struct ns_mem_page_info buf_ns;
  96. struct trusty_dev* tdev;
  97. };
  98. /*
  99. * Trusty IPC event handlers.
  100. */
  101. struct trusty_ipc_ops {
  102. int (*on_raw_event)(struct trusty_ipc_chan* chan,
  103. struct trusty_ipc_event* evt);
  104. int (*on_connect_complete)(struct trusty_ipc_chan* chan);
  105. int (*on_send_unblocked)(struct trusty_ipc_chan* chan);
  106. int (*on_message)(struct trusty_ipc_chan* chan);
  107. int (*on_disconnect)(struct trusty_ipc_chan* chan);
  108. };
  109. /*
  110. * Trusty IPC channel.
  111. *
  112. * @ops_ctx: refers to additional data that may be used by trusty_ipc_ops
  113. * @handle: identifier for channel
  114. * @complete: completion status of last event on channel
  115. * @dev: Trusty IPC device used by channel, initialized with
  116. trusty_ipc_dev_create
  117. * @ops: callbacks for Trusty events
  118. */
  119. struct trusty_ipc_chan {
  120. void* ops_ctx;
  121. handle_t handle;
  122. volatile int complete;
  123. struct trusty_ipc_dev* dev;
  124. struct trusty_ipc_ops* ops;
  125. };
  126. /*
  127. * Creates new Trusty IPC device on @tdev. Allocates shared buffer, and calls
  128. * trusty_dev_init_ipc to register with secure side. Returns a trusty_err.
  129. *
  130. * @ipc_dev: new Trusty IPC device to be initialized
  131. * @tdev: associated Trusty device
  132. * @shared_buf_size: size of shared buffer to be allocated
  133. */
  134. int trusty_ipc_dev_create(struct trusty_ipc_dev** ipc_dev,
  135. struct trusty_dev* tdev,
  136. size_t shared_buf_size);
  137. /*
  138. * Shutdown @dev. Frees shared buffer, and calls trusty_dev_shutdown_ipc
  139. * to shutdown on the secure side.
  140. */
  141. void trusty_ipc_dev_shutdown(struct trusty_ipc_dev* dev);
  142. /*
  143. * Calls into secure OS to initiate a new connection to a Trusty IPC service.
  144. * Returns handle for the new channel, a trusty_err on error.
  145. *
  146. * @dev: Trusty IPC device initialized with trusty_ipc_dev_create
  147. * @port: name of port to connect to on secure side
  148. * @cookie: cookie associated with new channel.
  149. */
  150. int trusty_ipc_dev_connect(struct trusty_ipc_dev* dev,
  151. const char* port,
  152. uint64_t cookie);
  153. /*
  154. * Calls into secure OS to close connection to Trusty IPC service.
  155. * Returns a trusty_err.
  156. *
  157. * @dev: Trusty IPC device
  158. * @chan: handle for connection, opened with trusty_ipc_dev_connect
  159. */
  160. int trusty_ipc_dev_close(struct trusty_ipc_dev* dev, handle_t chan);
  161. /*
  162. * Calls into secure OS to check if there is a pending event. Returns a bool.
  163. *
  164. * @dev: Trusty IPC device
  165. * @chan: handle for connection. Must be 0 which indicates any connection.
  166. */
  167. bool trusty_ipc_dev_has_event(struct trusty_ipc_dev* dev, handle_t chan);
  168. /*
  169. * Calls into secure OS to receive pending event. Returns a trusty_err.
  170. *
  171. * @dev: Trusty IPC device
  172. * @chan: handle for connection
  173. * @event: pointer to output event struct
  174. */
  175. int trusty_ipc_dev_get_event(struct trusty_ipc_dev* dev,
  176. handle_t chan,
  177. struct trusty_ipc_event* event);
  178. /*
  179. * Calls into secure OS to send message to channel. Returns a trusty_err.
  180. *
  181. * @dev: Trusty IPC device
  182. * @chan: handle for connection
  183. * @iovs: contains messages to be sent
  184. * @iovs_cnt: number of iovecs to be sent
  185. */
  186. int trusty_ipc_dev_send(struct trusty_ipc_dev* dev,
  187. handle_t chan,
  188. const struct trusty_ipc_iovec* iovs,
  189. size_t iovs_cnt);
  190. /*
  191. * Calls into secure OS to receive message on channel. Returns number of bytes
  192. * received on success, trusty_err on failure.
  193. *
  194. * @dev: Trusty IPC device
  195. * @chan: handle for connection
  196. * @iovs: contains received messages
  197. * @iovs_cnt: number of iovecs received
  198. */
  199. int trusty_ipc_dev_recv(struct trusty_ipc_dev* dev,
  200. handle_t chan,
  201. const struct trusty_ipc_iovec* iovs,
  202. size_t iovs_cnt);
  203. void trusty_ipc_dev_idle(struct trusty_ipc_dev* dev, bool event_poll);
  204. /*
  205. * Initializes @chan with default values and @dev.
  206. */
  207. void trusty_ipc_chan_init(struct trusty_ipc_chan* chan,
  208. struct trusty_ipc_dev* dev);
  209. /*
  210. * Calls trusty_ipc_dev_connect to get a handle for channel.
  211. * Returns a trusty_err.
  212. *
  213. * @chan: channel to initialize with new handle
  214. * @port: name of port to connect to on secure side
  215. * @wait: flag to wait for connect to complete by polling for
  216. * IPC_HANDLE_POLL_READY event
  217. */
  218. int trusty_ipc_connect(struct trusty_ipc_chan* chan,
  219. const char* port,
  220. bool wait);
  221. /*
  222. * Calls trusty_ipc_dev_close and invalidates @chan. Returns a trusty_err.
  223. */
  224. int trusty_ipc_close(struct trusty_ipc_chan* chan);
  225. /*
  226. * Calls trusty_ipc_dev_get_event to poll @dev for events. Handles
  227. * events by calling appropriate callbacks. Returns nonnegative on success.
  228. */
  229. int trusty_ipc_poll_for_event(struct trusty_ipc_dev* dev, handle_t handle);
  230. /*
  231. * Calls trusty_ipc_dev_send to send a message. Returns a trusty_err.
  232. *
  233. * @chan: handle for connection
  234. * @iovs: contains messages to be sent
  235. * @iovs_cnt: number of iovecs to be sent
  236. * @wait: flag to wait for send to complete
  237. */
  238. int trusty_ipc_send(struct trusty_ipc_chan* chan,
  239. const struct trusty_ipc_iovec* iovs,
  240. size_t iovs_cnt,
  241. bool wait);
  242. /*
  243. * Calls trusty_ipc_dev_recv to receive a message. Return number of bytes
  244. * received on success, trusty_err on failure.
  245. *
  246. * @chan: handle for connection
  247. * @iovs: contains received messages
  248. * @iovs_cnt: number of iovecs received
  249. * @wait: flag to wait for a message to receive
  250. */
  251. int trusty_ipc_recv(struct trusty_ipc_chan* chan,
  252. const struct trusty_ipc_iovec* iovs,
  253. size_t iovs_cnt,
  254. bool wait);
  255. #endif /* TRUSTY_QL_IPC_H_ */