qcomtee_msg.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
  4. */
  5. #ifndef QCOMTEE_MSG_H
  6. #define QCOMTEE_MSG_H
  7. #include <linux/bitfield.h>
  8. /**
  9. * DOC: ''Qualcomm TEE'' (QTEE) Transport Message
  10. *
  11. * There are two buffers shared with QTEE: inbound and outbound buffers.
  12. * The inbound buffer is used for direct object invocation, and the outbound
  13. * buffer is used to make a request from QTEE to the kernel; i.e., a callback
  14. * request.
  15. *
  16. * The unused tail of the outbound buffer is also used for sending and
  17. * receiving asynchronous messages. An asynchronous message is independent of
  18. * the current object invocation (i.e., contents of the inbound buffer) or
  19. * callback request (i.e., the head of the outbound buffer); see
  20. * qcomtee_get_async_buffer(). It is used by endpoints (QTEE or kernel) as an
  21. * optimization to reduce the number of context switches between the secure and
  22. * non-secure worlds.
  23. *
  24. * For instance, QTEE never sends an explicit callback request to release an
  25. * object in the kernel. Instead, it sends asynchronous release messages in the
  26. * outbound buffer when QTEE returns from the previous direct object invocation,
  27. * or appends asynchronous release messages after the current callback request.
  28. *
  29. * QTEE supports two types of arguments in a message: buffer and object
  30. * arguments. Depending on the direction of data flow, they could be input
  31. * buffer (IO) to QTEE, output buffer (OB) from QTEE, input object (IO) to QTEE,
  32. * or output object (OO) from QTEE. Object arguments hold object IDs. Buffer
  33. * arguments hold (offset, size) pairs into the inbound or outbound buffers.
  34. *
  35. * QTEE holds an object table for objects it hosts and exposes to the kernel.
  36. * An object ID is an index to the object table in QTEE.
  37. *
  38. * For the direct object invocation message format in the inbound buffer, see
  39. * &struct qcomtee_msg_object_invoke. For the callback request message format
  40. * in the outbound buffer, see &struct qcomtee_msg_callback. For the message
  41. * format for asynchronous messages in the outbound buffer, see
  42. * &struct qcomtee_async_msg_hdr.
  43. */
  44. /**
  45. * define QCOMTEE_MSG_OBJECT_NS_BIT - Non-secure bit
  46. *
  47. * Object ID is a globally unique 32-bit number. IDs referencing objects
  48. * in the kernel should have %QCOMTEE_MSG_OBJECT_NS_BIT set.
  49. */
  50. #define QCOMTEE_MSG_OBJECT_NS_BIT BIT(31)
  51. /* Static object IDs recognized by QTEE. */
  52. #define QCOMTEE_MSG_OBJECT_NULL (0U)
  53. #define QCOMTEE_MSG_OBJECT_ROOT (1U)
  54. /* Definitions from QTEE as part of the transport protocol. */
  55. /* qcomtee_msg_arg is an argument as recognized by QTEE. */
  56. union qcomtee_msg_arg {
  57. struct {
  58. u32 offset;
  59. u32 size;
  60. } b;
  61. u32 o;
  62. };
  63. /* BI and BO payloads in QTEE messages should be at 64-bit boundaries. */
  64. #define qcomtee_msg_offset_align(o) ALIGN((o), sizeof(u64))
  65. /* Operations for objects are 32-bit. Transport uses the upper 16 bits. */
  66. #define QCOMTEE_MSG_OBJECT_OP_MASK GENMASK(15, 0)
  67. /* Reserved Operation IDs sent to QTEE: */
  68. /* QCOMTEE_MSG_OBJECT_OP_RELEASE - Reduces the refcount and releases the object.
  69. * QCOMTEE_MSG_OBJECT_OP_RETAIN - Increases the refcount.
  70. *
  71. * These operation IDs are valid for all objects.
  72. */
  73. #define QCOMTEE_MSG_OBJECT_OP_RELEASE (QCOMTEE_MSG_OBJECT_OP_MASK - 0)
  74. #define QCOMTEE_MSG_OBJECT_OP_RETAIN (QCOMTEE_MSG_OBJECT_OP_MASK - 1)
  75. /* Subset of operations supported by QTEE root object. */
  76. #define QCOMTEE_ROOT_OP_REG_WITH_CREDENTIALS 5
  77. #define QCOMTEE_ROOT_OP_NOTIFY_DOMAIN_CHANGE 4
  78. #define QCOMTEE_ROOT_OP_ADCI_ACCEPT 8
  79. #define QCOMTEE_ROOT_OP_ADCI_SHUTDOWN 9
  80. /* Subset of operations supported by client_env object. */
  81. #define QCOMTEE_CLIENT_ENV_OPEN 0
  82. /* List of available QTEE service UIDs and subset of operations. */
  83. #define QCOMTEE_FEATURE_VER_UID 2033
  84. #define QCOMTEE_FEATURE_VER_OP_GET 0
  85. /* Get QTEE version number. */
  86. #define QCOMTEE_FEATURE_VER_OP_GET_QTEE_ID 10
  87. #define QTEE_VERSION_GET_MAJOR(x) (((x) >> 22) & 0xffU)
  88. #define QTEE_VERSION_GET_MINOR(x) (((x) >> 12) & 0xffU)
  89. #define QTEE_VERSION_GET_PATCH(x) ((x) >> 0 & 0xfffU)
  90. /* Response types as returned from qcomtee_object_invoke_ctx_invoke(). */
  91. /* The message contains a callback request. */
  92. #define QCOMTEE_RESULT_INBOUND_REQ_NEEDED 3
  93. /**
  94. * struct qcomtee_msg_object_invoke - Direct object invocation message.
  95. * @ctx: object ID hosted in QTEE.
  96. * @op: operation for the object.
  97. * @counts: number of different types of arguments in @args.
  98. * @args: array of arguments.
  99. *
  100. * @counts consists of 4 * 4-bit fields. Bits 0 - 3 represent the number of
  101. * input buffers, bits 4 - 7 represent the number of output buffers,
  102. * bits 8 - 11 represent the number of input objects, and bits 12 - 15
  103. * represent the number of output objects. The remaining bits should be zero.
  104. *
  105. * 15 12 11 8 7 4 3 0
  106. * +----------------+----------------+----------------+----------------+
  107. * | #OO objects | #IO objects | #OB buffers | #IB buffers |
  108. * +----------------+----------------+----------------+----------------+
  109. *
  110. * The maximum number of arguments of each type is defined by
  111. * %QCOMTEE_ARGS_PER_TYPE.
  112. */
  113. struct qcomtee_msg_object_invoke {
  114. u32 cxt;
  115. u32 op;
  116. u32 counts;
  117. union qcomtee_msg_arg args[];
  118. };
  119. /* Bit masks for the four 4-bit nibbles holding the counts. */
  120. #define QCOMTEE_MASK_IB GENMASK(3, 0)
  121. #define QCOMTEE_MASK_OB GENMASK(7, 4)
  122. #define QCOMTEE_MASK_IO GENMASK(11, 8)
  123. #define QCOMTEE_MASK_OO GENMASK(15, 12)
  124. /**
  125. * struct qcomtee_msg_callback - Callback request message.
  126. * @result: result of operation @op on the object referenced by @cxt.
  127. * @cxt: object ID hosted in the kernel.
  128. * @op: operation for the object.
  129. * @counts: number of different types of arguments in @args.
  130. * @args: array of arguments.
  131. *
  132. * For details of @counts, see &qcomtee_msg_object_invoke.counts.
  133. */
  134. struct qcomtee_msg_callback {
  135. u32 result;
  136. u32 cxt;
  137. u32 op;
  138. u32 counts;
  139. union qcomtee_msg_arg args[];
  140. };
  141. /* Offset in the message for the beginning of the buffer argument's contents. */
  142. #define qcomtee_msg_buffer_args(t, n) \
  143. qcomtee_msg_offset_align(struct_size_t(t, args, n))
  144. /* Pointer to the beginning of a buffer argument's content at an offset. */
  145. #define qcomtee_msg_offset_to_ptr(m, off) ((void *)&((char *)(m))[(off)])
  146. /* Some helpers to manage msg.counts. */
  147. static inline unsigned int qcomtee_msg_num_ib(u32 counts)
  148. {
  149. return FIELD_GET(QCOMTEE_MASK_IB, counts);
  150. }
  151. static inline unsigned int qcomtee_msg_num_ob(u32 counts)
  152. {
  153. return FIELD_GET(QCOMTEE_MASK_OB, counts);
  154. }
  155. static inline unsigned int qcomtee_msg_num_io(u32 counts)
  156. {
  157. return FIELD_GET(QCOMTEE_MASK_IO, counts);
  158. }
  159. static inline unsigned int qcomtee_msg_num_oo(u32 counts)
  160. {
  161. return FIELD_GET(QCOMTEE_MASK_OO, counts);
  162. }
  163. static inline unsigned int qcomtee_msg_idx_ib(u32 counts)
  164. {
  165. return 0;
  166. }
  167. static inline unsigned int qcomtee_msg_idx_ob(u32 counts)
  168. {
  169. return qcomtee_msg_num_ib(counts);
  170. }
  171. static inline unsigned int qcomtee_msg_idx_io(u32 counts)
  172. {
  173. return qcomtee_msg_idx_ob(counts) + qcomtee_msg_num_ob(counts);
  174. }
  175. static inline unsigned int qcomtee_msg_idx_oo(u32 counts)
  176. {
  177. return qcomtee_msg_idx_io(counts) + qcomtee_msg_num_io(counts);
  178. }
  179. #define qcomtee_msg_for_each(i, first, num) \
  180. for ((i) = (first); (i) < (first) + (num); (i)++)
  181. #define qcomtee_msg_for_each_input_buffer(i, m) \
  182. qcomtee_msg_for_each(i, qcomtee_msg_idx_ib((m)->counts), \
  183. qcomtee_msg_num_ib((m)->counts))
  184. #define qcomtee_msg_for_each_output_buffer(i, m) \
  185. qcomtee_msg_for_each(i, qcomtee_msg_idx_ob((m)->counts), \
  186. qcomtee_msg_num_ob((m)->counts))
  187. #define qcomtee_msg_for_each_input_object(i, m) \
  188. qcomtee_msg_for_each(i, qcomtee_msg_idx_io((m)->counts), \
  189. qcomtee_msg_num_io((m)->counts))
  190. #define qcomtee_msg_for_each_output_object(i, m) \
  191. qcomtee_msg_for_each(i, qcomtee_msg_idx_oo((m)->counts), \
  192. qcomtee_msg_num_oo((m)->counts))
  193. /* Sum of arguments in a message. */
  194. #define qcomtee_msg_args(m) \
  195. (qcomtee_msg_idx_oo((m)->counts) + qcomtee_msg_num_oo((m)->counts))
  196. static inline void qcomtee_msg_init(struct qcomtee_msg_object_invoke *msg,
  197. u32 cxt, u32 op, int in_buffer,
  198. int out_buffer, int in_object,
  199. int out_object)
  200. {
  201. u32 counts = 0;
  202. counts |= (in_buffer & 0xfU);
  203. counts |= ((out_buffer - in_buffer) & 0xfU) << 4;
  204. counts |= ((in_object - out_buffer) & 0xfU) << 8;
  205. counts |= ((out_object - in_object) & 0xfU) << 12;
  206. msg->cxt = cxt;
  207. msg->op = op;
  208. msg->counts = counts;
  209. }
  210. /* Generic error codes. */
  211. #define QCOMTEE_MSG_OK 0 /* non-specific success code. */
  212. #define QCOMTEE_MSG_ERROR 1 /* non-specific error. */
  213. #define QCOMTEE_MSG_ERROR_INVALID 2 /* unsupported/unrecognized request. */
  214. #define QCOMTEE_MSG_ERROR_SIZE_IN 3 /* supplied buffer/string too large. */
  215. #define QCOMTEE_MSG_ERROR_SIZE_OUT 4 /* supplied output buffer too small. */
  216. #define QCOMTEE_MSG_ERROR_USERBASE 10 /* start of user-defined error range. */
  217. /* Transport layer error codes. */
  218. #define QCOMTEE_MSG_ERROR_DEFUNCT -90 /* object no longer exists. */
  219. #define QCOMTEE_MSG_ERROR_ABORT -91 /* calling thread must exit. */
  220. #define QCOMTEE_MSG_ERROR_BADOBJ -92 /* invalid object context. */
  221. #define QCOMTEE_MSG_ERROR_NOSLOTS -93 /* caller's object table full. */
  222. #define QCOMTEE_MSG_ERROR_MAXARGS -94 /* too many args. */
  223. #define QCOMTEE_MSG_ERROR_MAXDATA -95 /* buffers too large. */
  224. #define QCOMTEE_MSG_ERROR_UNAVAIL -96 /* the request could not be processed. */
  225. #define QCOMTEE_MSG_ERROR_KMEM -97 /* kernel out of memory. */
  226. #define QCOMTEE_MSG_ERROR_REMOTE -98 /* local method sent to remote object. */
  227. #define QCOMTEE_MSG_ERROR_BUSY -99 /* Object is busy. */
  228. #define QCOMTEE_MSG_ERROR_TIMEOUT -103 /* Call Back Object invocation timed out. */
  229. static inline void qcomtee_msg_set_result(struct qcomtee_msg_callback *cb_msg,
  230. int err)
  231. {
  232. if (!err) {
  233. cb_msg->result = QCOMTEE_MSG_OK;
  234. } else if (err < 0) {
  235. /* If err < 0, then it is a transport error. */
  236. switch (err) {
  237. case -ENOMEM:
  238. cb_msg->result = QCOMTEE_MSG_ERROR_KMEM;
  239. break;
  240. case -ENODEV:
  241. cb_msg->result = QCOMTEE_MSG_ERROR_DEFUNCT;
  242. break;
  243. case -ENOSPC:
  244. case -EBUSY:
  245. cb_msg->result = QCOMTEE_MSG_ERROR_BUSY;
  246. break;
  247. case -EBADF:
  248. case -EINVAL:
  249. cb_msg->result = QCOMTEE_MSG_ERROR_UNAVAIL;
  250. break;
  251. default:
  252. cb_msg->result = QCOMTEE_MSG_ERROR;
  253. }
  254. } else {
  255. /* If err > 0, then it is user defined error, pass it as is. */
  256. cb_msg->result = err;
  257. }
  258. }
  259. #endif /* QCOMTEE_MSG_H */