optee_private.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * @ctx_mutex: held while accessing @ctx
  48. * @func: supplicant function id to call
  49. * @ret: call return value
  50. * @num_params: number of elements in @param
  51. * @param: parameters for @func
  52. * @req_posted: if true, a request has been posted to the supplicant
  53. * @supp_next_send: if true, next step is for supplicant to send response
  54. * @thrd_mutex: held by the thread doing a request to supplicant
  55. * @supp_mutex: held by supplicant while operating on this struct
  56. * @data_to_supp: supplicant is waiting on this for next request
  57. * @data_from_supp: requesting thread is waiting on this to get the result
  58. */
  59. struct optee_supp {
  60. struct tee_context *ctx;
  61. /* Serializes access of ctx */
  62. struct mutex ctx_mutex;
  63. u32 func;
  64. u32 ret;
  65. size_t num_params;
  66. struct tee_param *param;
  67. bool req_posted;
  68. bool supp_next_send;
  69. /* Serializes access to this struct for requesting thread */
  70. struct mutex thrd_mutex;
  71. /* Serializes access to this struct for supplicant threads */
  72. struct mutex supp_mutex;
  73. struct completion data_to_supp;
  74. struct completion data_from_supp;
  75. };
  76. /**
  77. * struct optee - main service struct
  78. * @supp_teedev: supplicant device
  79. * @teedev: client device
  80. * @invoke_fn: function to issue smc or hvc
  81. * @call_queue: queue of threads waiting to call @invoke_fn
  82. * @wait_queue: queue of threads from secure world waiting for a
  83. * secure world sync object
  84. * @supp: supplicant synchronization struct for RPC to supplicant
  85. * @pool: shared memory pool
  86. * @memremaped_shm virtual address of memory in shared memory pool
  87. */
  88. struct optee {
  89. struct tee_device *supp_teedev;
  90. struct tee_device *teedev;
  91. optee_invoke_fn *invoke_fn;
  92. struct optee_call_queue call_queue;
  93. struct optee_wait_queue wait_queue;
  94. struct optee_supp supp;
  95. struct tee_shm_pool *pool;
  96. void *memremaped_shm;
  97. };
  98. struct optee_session {
  99. struct list_head list_node;
  100. u32 session_id;
  101. };
  102. struct optee_context_data {
  103. /* Serializes access to this struct */
  104. struct mutex mutex;
  105. struct list_head sess_list;
  106. };
  107. struct optee_rpc_param {
  108. u32 a0;
  109. u32 a1;
  110. u32 a2;
  111. u32 a3;
  112. u32 a4;
  113. u32 a5;
  114. u32 a6;
  115. u32 a7;
  116. };
  117. void optee_handle_rpc(struct tee_context *ctx, struct optee_rpc_param *param);
  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. int optee_supp_recv(struct tee_context *ctx, u32 *func, u32 *num_params,
  127. struct tee_param *param);
  128. int optee_supp_send(struct tee_context *ctx, u32 ret, u32 num_params,
  129. struct tee_param *param);
  130. u32 optee_do_call_with_arg(struct tee_context *ctx, phys_addr_t parg);
  131. int optee_open_session(struct tee_context *ctx,
  132. struct tee_ioctl_open_session_arg *arg,
  133. struct tee_param *param);
  134. int optee_close_session(struct tee_context *ctx, u32 session);
  135. int optee_invoke_func(struct tee_context *ctx, struct tee_ioctl_invoke_arg *arg,
  136. struct tee_param *param);
  137. int optee_cancel_req(struct tee_context *ctx, u32 cancel_id, u32 session);
  138. void optee_enable_shm_cache(struct optee *optee);
  139. void optee_disable_shm_cache(struct optee *optee);
  140. int optee_from_msg_param(struct tee_param *params, size_t num_params,
  141. const struct optee_msg_param *msg_params);
  142. int optee_to_msg_param(struct optee_msg_param *msg_params, size_t num_params,
  143. const struct tee_param *params);
  144. /*
  145. * Small helpers
  146. */
  147. static inline void *reg_pair_to_ptr(u32 reg0, u32 reg1)
  148. {
  149. return (void *)(unsigned long)(((u64)reg0 << 32) | reg1);
  150. }
  151. static inline void reg_pair_from_64(u32 *reg0, u32 *reg1, u64 val)
  152. {
  153. *reg0 = val >> 32;
  154. *reg1 = val;
  155. }
  156. #endif /*OPTEE_PRIVATE_H*/