optee_private.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2015, Linaro Limited
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #ifndef OPTEE_PRIVATE_H
  15. #define OPTEE_PRIVATE_H
  16. #include <linux/arm-smccc.h>
  17. #include <linux/semaphore.h>
  18. #include <linux/tee_drv.h>
  19. #include <linux/types.h>
  20. #include "optee_msg.h"
  21. #define OPTEE_MAX_ARG_SIZE 1024
  22. /* Some Global Platform error codes used in this driver */
  23. #define TEEC_SUCCESS 0x00000000
  24. #define TEEC_ERROR_BAD_PARAMETERS 0xFFFF0006
  25. #define TEEC_ERROR_COMMUNICATION 0xFFFF000E
  26. #define TEEC_ERROR_OUT_OF_MEMORY 0xFFFF000C
  27. #define TEEC_ORIGIN_COMMS 0x00000002
  28. typedef void (optee_invoke_fn)(unsigned long, unsigned long, unsigned long,
  29. unsigned long, unsigned long, unsigned long,
  30. unsigned long, unsigned long,
  31. struct arm_smccc_res *);
  32. struct optee_call_queue {
  33. /* Serializes access to this struct */
  34. struct mutex mutex;
  35. struct list_head waiters;
  36. };
  37. struct optee_wait_queue {
  38. /* Serializes access to this struct */
  39. struct mutex mu;
  40. struct list_head db;
  41. };
  42. /**
  43. * struct optee_supp - supplicant synchronization struct
  44. * @ctx the context of current connected supplicant.
  45. * if !NULL the supplicant device is available for use,
  46. * else busy
  47. * @mutex: held while accessing content of this struct
  48. * @req_id: current request id if supplicant is doing synchronous
  49. * communication, else -1
  50. * @reqs: queued request not yet retrieved by supplicant
  51. * @idr: IDR holding all requests currently being processed
  52. * by supplicant
  53. * @reqs_c: completion used by supplicant when waiting for a
  54. * request to be queued.
  55. */
  56. struct optee_supp {
  57. /* Serializes access to this struct */
  58. struct mutex mutex;
  59. struct tee_context *ctx;
  60. int req_id;
  61. struct list_head reqs;
  62. struct idr idr;
  63. struct completion reqs_c;
  64. };
  65. /**
  66. * struct optee - main service struct
  67. * @supp_teedev: supplicant device
  68. * @teedev: client device
  69. * @invoke_fn: function to issue smc or hvc
  70. * @call_queue: queue of threads waiting to call @invoke_fn
  71. * @wait_queue: queue of threads from secure world waiting for a
  72. * secure world sync object
  73. * @supp: supplicant synchronization struct for RPC to supplicant
  74. * @pool: shared memory pool
  75. * @memremaped_shm virtual address of memory in shared memory pool
  76. * @sec_caps: secure world capabilities defined by
  77. * OPTEE_SMC_SEC_CAP_* in optee_smc.h
  78. */
  79. struct optee {
  80. struct tee_device *supp_teedev;
  81. struct tee_device *teedev;
  82. optee_invoke_fn *invoke_fn;
  83. struct optee_call_queue call_queue;
  84. struct optee_wait_queue wait_queue;
  85. struct optee_supp supp;
  86. struct tee_shm_pool *pool;
  87. void *memremaped_shm;
  88. u32 sec_caps;
  89. };
  90. struct optee_session {
  91. struct list_head list_node;
  92. u32 session_id;
  93. };
  94. struct optee_context_data {
  95. /* Serializes access to this struct */
  96. struct mutex mutex;
  97. struct list_head sess_list;
  98. };
  99. struct optee_rpc_param {
  100. u32 a0;
  101. u32 a1;
  102. u32 a2;
  103. u32 a3;
  104. u32 a4;
  105. u32 a5;
  106. u32 a6;
  107. u32 a7;
  108. };
  109. /* Holds context that is preserved during one STD call */
  110. struct optee_call_ctx {
  111. /* information about pages list used in last allocation */
  112. void *pages_list;
  113. size_t num_entries;
  114. };
  115. void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param,
  116. struct optee_call_ctx *call_ctx);
  117. void optee_rpc_finalize_call(struct optee_call_ctx *call_ctx);
  118. void optee_wait_queue_init(struct optee_wait_queue *wq);
  119. void optee_wait_queue_exit(struct optee_wait_queue *wq);
  120. u32 optee_supp_thrd_req(struct tee_context *ctx, u32 func, size_t num_params,
  121. struct tee_param *param);
  122. int optee_supp_read(struct tee_context *ctx, void __user *buf, size_t len);
  123. int optee_supp_write(struct tee_context *ctx, void __user *buf, size_t len);
  124. void optee_supp_init(struct optee_supp *supp);
  125. void optee_supp_uninit(struct optee_supp *supp);
  126. void optee_supp_release(struct optee_supp *supp);
  127. int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
  128. struct tee_param *param);
  129. int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
  130. struct tee_param *param);
  131. u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg);
  132. int optee_open_session(struct tee_context *ctx,
  133. struct tee_ioctl_open_session_arg *arg,
  134. struct tee_param *param);
  135. int optee_close_session(struct tee_context *ctx, u32 session);
  136. int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
  137. struct tee_param *param);
  138. int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
  139. void optee_enable_shm_cache(struct optee *optee);
  140. void optee_disable_shm_cache(struct optee *optee);
  141. int optee_shm_register(struct tee_context *ctx, struct tee_shm *shm,
  142. struct page **pages, size_t num_pages,
  143. unsigned long start);
  144. int optee_shm_unregister(struct tee_context *ctx, struct tee_shm *shm);
  145. int optee_shm_register_supp(struct tee_context *ctx, struct tee_shm *shm,
  146. struct page **pages, size_t num_pages,
  147. unsigned long start);
  148. int optee_shm_unregister_supp(struct tee_context *ctx, struct tee_shm *shm);
  149. int optee_from_msg_param(struct tee_param *params, size_t num_params,
  150. const struct optee_msg_param *msg_params);
  151. int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
  152. const struct tee_param *params);
  153. u64 *optee_allocate_pages_list(size_t num_entries);
  154. void optee_free_pages_list(void *array, size_t num_entries);
  155. void optee_fill_pages_list(u64 *dst, struct page **pages, int num_pages,
  156. size_t page_offset);
  157. /*
  158. * Small helpers
  159. */
  160. static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
  161. {
  162. return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
  163. }
  164. static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
  165. {
  166. *reg0 = val >> 32;
  167. *reg1 = val;
  168. }
  169. #endif /*OPTEE_PRIVATE_H*/