optee_private.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2015-2021, Linaro Limited
  4. */
  5. #ifndef OPTEE_PRIVATE_H
  6. #define OPTEE_PRIVATE_H
  7. #include <linux/arm-smccc.h>
  8. #include <linux/rhashtable.h>
  9. #include <linux/semaphore.h>
  10. #include <linux/tee_drv.h>
  11. #include <linux/types.h>
  12. #include "optee_msg.h"
  13. #define DRIVER_NAME "optee"
  14. #define OPTEE_MAX_ARG_SIZE 1024
  15. /* Some Global Platform error codes used in this driver */
  16. #define TEEC_SUCCESS 0x00000000
  17. #define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
  18. #define TEEC_ERROR_NOT_SUPPORTED 0xFFFF000A
  19. #define TEEC_ERROR_COMMUNICATION 0xFFFF000E
  20. #define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
  21. #define TEEC_ERROR_BUSY 0xFFFF000D
  22. #define TEEC_ERROR_SHORT_BUFFER 0xFFFF0010
  23. #define TEEC_ORIGIN_COMMS 0x00000002
  24. /*
  25. * This value should be larger than the number threads in secure world to
  26. * meet the need from secure world. The number of threads in secure world
  27. * are usually not even close to 255 so we should be safe for now.
  28. */
  29. #define OPTEE_DEFAULT_MAX_NOTIF_VALUE 255
  30. typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
  31. unsigned long, unsigned long, unsigned long,
  32. unsigned long, unsigned long,
  33. struct arm_smccc_res *);
  34. struct optee_call_waiter {
  35. struct list_head list_node;
  36. struct completion c;
  37. };
  38. struct optee_call_queue {
  39. /* Serializes access to this struct */
  40. struct mutex mutex;
  41. struct list_head waiters;
  42. };
  43. struct optee_notif {
  44. u_int max_key;
  45. /* Serializes access to the elements below in this struct */
  46. spinlock_t lock;
  47. struct list_head db;
  48. u_long *bitmap;
  49. };
  50. #define OPTEE_SHM_ARG_ALLOC_PRIV BIT(0)
  51. #define OPTEE_SHM_ARG_SHARED BIT(1)
  52. struct optee_shm_arg_entry;
  53. struct optee_shm_arg_cache {
  54. u32 flags;
  55. /* Serializes access to this struct */
  56. struct mutex mutex;
  57. struct list_head shm_args;
  58. };
  59. /**
  60. * struct optee_supp - supplicant synchronization struct
  61. * @ctx the context of current connected supplicant.
  62. * if !NULL the supplicant device is available for use,
  63. * else busy
  64. * @mutex: held while accessing content of this struct
  65. * @req_id: current request id if supplicant is doing synchronous
  66. * communication, else -1
  67. * @reqs: queued request not yet retrieved by supplicant
  68. * @idr: IDR holding all requests currently being processed
  69. * by supplicant
  70. * @reqs_c: completion used by supplicant when waiting for a
  71. * request to be queued.
  72. */
  73. struct optee_supp {
  74. /* Serializes access to this struct */
  75. struct mutex mutex;
  76. struct tee_context *ctx;
  77. int req_id;
  78. struct list_head reqs;
  79. struct idr idr;
  80. struct completion reqs_c;
  81. };
  82. struct optee_smc {
  83. optee_invoke_fn *invoke_fn;
  84. void *memremaped_shm;
  85. u32 sec_caps;
  86. unsigned int notif_irq;
  87. };
  88. /**
  89. * struct optee_ffa_data - FFA communication struct
  90. * @ffa_dev FFA device, contains the destination id, the id of
  91. * OP-TEE in secure world
  92. * @ffa_ops FFA operations
  93. * @mutex Serializes access to @global_ids
  94. * @global_ids FF-A shared memory global handle translation
  95. */
  96. struct optee_ffa {
  97. struct ffa_device *ffa_dev;
  98. /* Serializes access to @global_ids */
  99. struct mutex mutex;
  100. struct rhashtable global_ids;
  101. };
  102. struct optee;
  103. /**
  104. * struct optee_ops - OP-TEE driver internal operations
  105. * @do_call_with_arg: enters OP-TEE in secure world
  106. * @to_msg_param: converts from struct tee_param to OPTEE_MSG parameters
  107. * @from_msg_param: converts from OPTEE_MSG parameters to struct tee_param
  108. *
  109. * These OPs are only supposed to be used internally in the OP-TEE driver
  110. * as a way of abstracting the different methogs of entering OP-TEE in
  111. * secure world.
  112. */
  113. struct optee_ops {
  114. int (*do_call_with_arg)(struct tee_context *ctx,
  115. struct tee_shm *shm_arg, u_int offs);
  116. int (*to_msg_param)(struct optee *optee,
  117. struct optee_msg_param *msg_params,
  118. size_t num_params, const struct tee_param *params);
  119. int (*from_msg_param)(struct optee *optee, struct tee_param *params,
  120. size_t num_params,
  121. const struct optee_msg_param *msg_params);
  122. };
  123. /**
  124. * struct optee - main service struct
  125. * @supp_teedev: supplicant device
  126. * @teedev: client device
  127. * @ops: internal callbacks for different ways to reach secure
  128. * world
  129. * @ctx: driver internal TEE context
  130. * @smc: specific to SMC ABI
  131. * @ffa: specific to FF-A ABI
  132. * @call_queue: queue of threads waiting to call @invoke_fn
  133. * @notif: notification synchronization struct
  134. * @supp: supplicant synchronization struct for RPC to supplicant
  135. * @pool: shared memory pool
  136. * @rpc_param_count: If > 0 number of RPC parameters to make room for
  137. * @scan_bus_done flag if device registation was already done.
  138. * @scan_bus_wq workqueue to scan optee bus and register optee drivers
  139. * @scan_bus_work workq to scan optee bus and register optee drivers
  140. */
  141. struct optee {
  142. struct tee_device *supp_teedev;
  143. struct tee_device *teedev;
  144. const struct optee_ops *ops;
  145. struct tee_context *ctx;
  146. union {
  147. struct optee_smc smc;
  148. struct optee_ffa ffa;
  149. };
  150. struct optee_shm_arg_cache shm_arg_cache;
  151. struct optee_call_queue call_queue;
  152. struct optee_notif notif;
  153. struct optee_supp supp;
  154. struct tee_shm_pool *pool;
  155. unsigned int rpc_param_count;
  156. bool scan_bus_done;
  157. struct workqueue_struct *scan_bus_wq;
  158. struct work_struct scan_bus_work;
  159. };
  160. struct optee_session {
  161. struct list_head list_node;
  162. u32 session_id;
  163. };
  164. struct optee_context_data {
  165. /* Serializes access to this struct */
  166. struct mutex mutex;
  167. struct list_head sess_list;
  168. };
  169. struct optee_rpc_param {
  170. u32 a0;
  171. u32 a1;
  172. u32 a2;
  173. u32 a3;
  174. u32 a4;
  175. u32 a5;
  176. u32 a6;
  177. u32 a7;
  178. };
  179. /* Holds context that is preserved during one STD call */
  180. struct optee_call_ctx {
  181. /* information about pages list used in last allocation */
  182. void *pages_list;
  183. size_t num_entries;
  184. };
  185. int optee_notif_init(struct optee *optee, u_int max_key);
  186. void optee_notif_uninit(struct optee *optee);
  187. int optee_notif_wait(struct optee *optee, u_int key);
  188. int optee_notif_send(struct optee *optee, u_int key);
  189. u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
  190. struct tee_param *param);
  191. int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
  192. int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
  193. void optee_supp_init(struct optee_supp *supp);
  194. void optee_supp_uninit(struct optee_supp *supp);
  195. void optee_supp_release(struct optee_supp *supp);
  196. int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
  197. struct tee_param *param);
  198. int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
  199. struct tee_param *param);
  200. int optee_open_session(struct tee_context *ctx,
  201. struct tee_ioctl_open_session_arg *arg,
  202. struct tee_param *param);
  203. int optee_close_session_helper(struct tee_context *ctx, u32 session);
  204. int optee_close_session(struct tee_context *ctx, u32 session);
  205. int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
  206. struct tee_param *param);
  207. int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
  208. #define PTA_CMD_GET_DEVICES 0x0
  209. #define PTA_CMD_GET_DEVICES_SUPP 0x1
  210. int optee_enumerate_devices(u32 func);
  211. void optee_unregister_devices(void);
  212. int optee_pool_op_alloc_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
  213. size_t size, size_t align,
  214. int (*shm_register)(struct tee_context *ctx,
  215. struct tee_shm *shm,
  216. struct page **pages,
  217. size_t num_pages,
  218. unsigned long start));
  219. void optee_pool_op_free_helper(struct tee_shm_pool *pool, struct tee_shm *shm,
  220. int (*shm_unregister)(struct tee_context *ctx,
  221. struct tee_shm *shm));
  222. void optee_remove_common(struct optee *optee);
  223. int optee_open(struct tee_context *ctx, bool cap_memref_null);
  224. void optee_release(struct tee_context *ctx);
  225. void optee_release_supp(struct tee_context *ctx);
  226. static inline void optee_from_msg_param_value(struct tee_param *p, u32 attr,
  227. const struct optee_msg_param *mp)
  228. {
  229. p->attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT +
  230. attr - OPTEE_MSG_ATTR_TYPE_VALUE_INPUT;
  231. p->u.value.a = mp->u.value.a;
  232. p->u.value.b = mp->u.value.b;
  233. p->u.value.c = mp->u.value.c;
  234. }
  235. static inline void optee_to_msg_param_value(struct optee_msg_param *mp,
  236. const struct tee_param *p)
  237. {
  238. mp->attr = OPTEE_MSG_ATTR_TYPE_VALUE_INPUT + p->attr -
  239. TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
  240. mp->u.value.a = p->u.value.a;
  241. mp->u.value.b = p->u.value.b;
  242. mp->u.value.c = p->u.value.c;
  243. }
  244. void optee_cq_wait_init(struct optee_call_queue *cq,
  245. struct optee_call_waiter *w);
  246. void optee_cq_wait_for_completion(struct optee_call_queue *cq,
  247. struct optee_call_waiter *w);
  248. void optee_cq_wait_final(struct optee_call_queue *cq,
  249. struct optee_call_waiter *w);
  250. int optee_check_mem_type(unsigned long start, size_t num_pages);
  251. void optee_shm_arg_cache_init(struct optee *optee, u32 flags);
  252. void optee_shm_arg_cache_uninit(struct optee *optee);
  253. struct optee_msg_arg *optee_get_msg_arg(struct tee_context *ctx,
  254. size_t num_params,
  255. struct optee_shm_arg_entry **entry,
  256. struct tee_shm **shm_ret,
  257. u_int *offs);
  258. void optee_free_msg_arg(struct tee_context *ctx,
  259. struct optee_shm_arg_entry *entry, u_int offs);
  260. size_t optee_msg_arg_size(size_t rpc_param_count);
  261. struct tee_shm *optee_rpc_cmd_alloc_suppl(struct tee_context *ctx, size_t sz);
  262. void optee_rpc_cmd_free_suppl(struct tee_context *ctx, struct tee_shm *shm);
  263. void optee_rpc_cmd(struct tee_context *ctx, struct optee *optee,
  264. struct optee_msg_arg *arg);
  265. /*
  266. * Small helpers
  267. */
  268. static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
  269. {
  270. return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
  271. }
  272. static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
  273. {
  274. *reg0 = val >> 32;
  275. *reg1 = val;
  276. }
  277. /* Registration of the ABIs */
  278. int optee_smc_abi_register(void);
  279. void optee_smc_abi_unregister(void);
  280. int optee_ffa_abi_register(void);
  281. void optee_ffa_abi_unregister(void);
  282. #endif /*OPTEE_PRIVATE_H*/