tspd_private.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2013-2021, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef TSPD_PRIVATE_H
  7. #define TSPD_PRIVATE_H
  8. #include <platform_def.h>
  9. #include <arch.h>
  10. #include <bl31/interrupt_mgmt.h>
  11. #include <context.h>
  12. #include <lib/psci/psci.h>
  13. /*******************************************************************************
  14. * Secure Payload PM state information e.g. SP is suspended, uninitialised etc
  15. * and macros to access the state information in the per-cpu 'state' flags
  16. ******************************************************************************/
  17. #define TSP_PSTATE_OFF 0
  18. #define TSP_PSTATE_ON 1
  19. #define TSP_PSTATE_SUSPEND 2
  20. #define TSP_PSTATE_SHIFT 0
  21. #define TSP_PSTATE_MASK 0x3
  22. #define get_tsp_pstate(state) ((state >> TSP_PSTATE_SHIFT) & TSP_PSTATE_MASK)
  23. #define clr_tsp_pstate(state) (state &= ~(TSP_PSTATE_MASK \
  24. << TSP_PSTATE_SHIFT))
  25. #define set_tsp_pstate(st, pst) do { \
  26. clr_tsp_pstate(st); \
  27. st |= (pst & TSP_PSTATE_MASK) << \
  28. TSP_PSTATE_SHIFT; \
  29. } while (0);
  30. /*
  31. * This flag is used by the TSPD to determine if the TSP is servicing a yielding
  32. * SMC request prior to programming the next entry into the TSP e.g. if TSP
  33. * execution is preempted by a non-secure interrupt and handed control to the
  34. * normal world. If another request which is distinct from what the TSP was
  35. * previously doing arrives, then this flag will be help the TSPD to either
  36. * reject the new request or service it while ensuring that the previous context
  37. * is not corrupted.
  38. */
  39. #define YIELD_SMC_ACTIVE_FLAG_SHIFT 2
  40. #define YIELD_SMC_ACTIVE_FLAG_MASK 1
  41. #define get_yield_smc_active_flag(state) \
  42. ((state >> YIELD_SMC_ACTIVE_FLAG_SHIFT) \
  43. & YIELD_SMC_ACTIVE_FLAG_MASK)
  44. #define set_yield_smc_active_flag(state) (state |= \
  45. 1 << YIELD_SMC_ACTIVE_FLAG_SHIFT)
  46. #define clr_yield_smc_active_flag(state) (state &= \
  47. ~(YIELD_SMC_ACTIVE_FLAG_MASK \
  48. << YIELD_SMC_ACTIVE_FLAG_SHIFT))
  49. /*******************************************************************************
  50. * Secure Payload execution state information i.e. aarch32 or aarch64
  51. ******************************************************************************/
  52. #define TSP_AARCH32 MODE_RW_32
  53. #define TSP_AARCH64 MODE_RW_64
  54. /*******************************************************************************
  55. * The SPD should know the type of Secure Payload.
  56. ******************************************************************************/
  57. #define TSP_TYPE_UP PSCI_TOS_NOT_UP_MIG_CAP
  58. #define TSP_TYPE_UPM PSCI_TOS_UP_MIG_CAP
  59. #define TSP_TYPE_MP PSCI_TOS_NOT_PRESENT_MP
  60. /*******************************************************************************
  61. * Secure Payload migrate type information as known to the SPD. We assume that
  62. * the SPD is dealing with an MP Secure Payload.
  63. ******************************************************************************/
  64. #define TSP_MIGRATE_INFO TSP_TYPE_MP
  65. /*******************************************************************************
  66. * Number of cpus that the present on this platform. TODO: Rely on a topology
  67. * tree to determine this in the future to avoid assumptions about mpidr
  68. * allocation
  69. ******************************************************************************/
  70. #define TSPD_CORE_COUNT PLATFORM_CORE_COUNT
  71. /*******************************************************************************
  72. * Constants that allow assembler code to preserve callee-saved registers of the
  73. * C runtime context while performing a security state switch.
  74. ******************************************************************************/
  75. #define TSPD_C_RT_CTX_X19 0x0
  76. #define TSPD_C_RT_CTX_X20 0x8
  77. #define TSPD_C_RT_CTX_X21 0x10
  78. #define TSPD_C_RT_CTX_X22 0x18
  79. #define TSPD_C_RT_CTX_X23 0x20
  80. #define TSPD_C_RT_CTX_X24 0x28
  81. #define TSPD_C_RT_CTX_X25 0x30
  82. #define TSPD_C_RT_CTX_X26 0x38
  83. #define TSPD_C_RT_CTX_X27 0x40
  84. #define TSPD_C_RT_CTX_X28 0x48
  85. #define TSPD_C_RT_CTX_X29 0x50
  86. #define TSPD_C_RT_CTX_X30 0x58
  87. #define TSPD_C_RT_CTX_SIZE 0x60
  88. #define TSPD_C_RT_CTX_ENTRIES (TSPD_C_RT_CTX_SIZE >> DWORD_SHIFT)
  89. /*******************************************************************************
  90. * Constants that allow assembler code to preserve caller-saved registers of the
  91. * SP context while performing a TSP preemption.
  92. * Note: These offsets have to match with the offsets for the corresponding
  93. * registers in cpu_context as we are using memcpy to copy the values from
  94. * cpu_context to sp_ctx.
  95. ******************************************************************************/
  96. #define TSPD_SP_CTX_X0 0x0
  97. #define TSPD_SP_CTX_X1 0x8
  98. #define TSPD_SP_CTX_X2 0x10
  99. #define TSPD_SP_CTX_X3 0x18
  100. #define TSPD_SP_CTX_X4 0x20
  101. #define TSPD_SP_CTX_X5 0x28
  102. #define TSPD_SP_CTX_X6 0x30
  103. #define TSPD_SP_CTX_X7 0x38
  104. #define TSPD_SP_CTX_X8 0x40
  105. #define TSPD_SP_CTX_X9 0x48
  106. #define TSPD_SP_CTX_X10 0x50
  107. #define TSPD_SP_CTX_X11 0x58
  108. #define TSPD_SP_CTX_X12 0x60
  109. #define TSPD_SP_CTX_X13 0x68
  110. #define TSPD_SP_CTX_X14 0x70
  111. #define TSPD_SP_CTX_X15 0x78
  112. #define TSPD_SP_CTX_X16 0x80
  113. #define TSPD_SP_CTX_X17 0x88
  114. #define TSPD_SP_CTX_SIZE 0x90
  115. #define TSPD_SP_CTX_ENTRIES (TSPD_SP_CTX_SIZE >> DWORD_SHIFT)
  116. #ifndef __ASSEMBLER__
  117. #include <stdint.h>
  118. #include <lib/cassert.h>
  119. /*
  120. * The number of arguments to save during a SMC call for TSP.
  121. * Currently only x1 and x2 are used by TSP.
  122. */
  123. #define TSP_NUM_ARGS 0x2
  124. /* AArch64 callee saved general purpose register context structure. */
  125. DEFINE_REG_STRUCT(c_rt_regs, TSPD_C_RT_CTX_ENTRIES);
  126. /*
  127. * Compile time assertion to ensure that both the compiler and linker
  128. * have the same double word aligned view of the size of the C runtime
  129. * register context.
  130. */
  131. CASSERT(TSPD_C_RT_CTX_SIZE == sizeof(c_rt_regs_t),
  132. assert_spd_c_rt_regs_size_mismatch);
  133. /* SEL1 Secure payload (SP) caller saved register context structure. */
  134. DEFINE_REG_STRUCT(sp_ctx_regs, TSPD_SP_CTX_ENTRIES);
  135. /*
  136. * Compile time assertion to ensure that both the compiler and linker
  137. * have the same double word aligned view of the size of the C runtime
  138. * register context.
  139. */
  140. CASSERT(TSPD_SP_CTX_SIZE == sizeof(sp_ctx_regs_t),
  141. assert_spd_sp_regs_size_mismatch);
  142. /*******************************************************************************
  143. * Structure which helps the SPD to maintain the per-cpu state of the SP.
  144. * 'saved_spsr_el3' - temporary copy to allow S-EL1 interrupt handling when
  145. * the TSP has been preempted.
  146. * 'saved_elr_el3' - temporary copy to allow S-EL1 interrupt handling when
  147. * the TSP has been preempted.
  148. * 'state' - collection of flags to track SP state e.g. on/off
  149. * 'mpidr' - mpidr to associate a context with a cpu
  150. * 'c_rt_ctx' - stack address to restore C runtime context from after
  151. * returning from a synchronous entry into the SP.
  152. * 'cpu_ctx' - space to maintain SP architectural state
  153. * 'saved_tsp_args' - space to store arguments for TSP arithmetic operations
  154. * which will queried using the TSP_GET_ARGS SMC by TSP.
  155. * 'sp_ctx' - space to save the SEL1 Secure Payload(SP) caller saved
  156. * register context after it has been preempted by an EL3
  157. * routed NS interrupt and when a Secure Interrupt is taken
  158. * to SP.
  159. ******************************************************************************/
  160. typedef struct tsp_context {
  161. uint64_t saved_elr_el3;
  162. uint32_t saved_spsr_el3;
  163. uint32_t state;
  164. uint64_t mpidr;
  165. uint64_t c_rt_ctx;
  166. cpu_context_t cpu_ctx;
  167. uint64_t saved_tsp_args[TSP_NUM_ARGS];
  168. #if TSP_NS_INTR_ASYNC_PREEMPT
  169. sp_ctx_regs_t sp_ctx;
  170. bool preempted_by_sel1_intr;
  171. #endif
  172. } tsp_context_t;
  173. /* Helper macros to store and retrieve tsp args from tsp_context */
  174. #define store_tsp_args(_tsp_ctx, _x1, _x2) do {\
  175. _tsp_ctx->saved_tsp_args[0] = _x1;\
  176. _tsp_ctx->saved_tsp_args[1] = _x2;\
  177. } while (0)
  178. #define get_tsp_args(_tsp_ctx, _x1, _x2) do {\
  179. _x1 = _tsp_ctx->saved_tsp_args[0];\
  180. _x2 = _tsp_ctx->saved_tsp_args[1];\
  181. } while (0)
  182. /* TSPD power management handlers */
  183. extern const spd_pm_ops_t tspd_pm;
  184. /*******************************************************************************
  185. * Forward declarations
  186. ******************************************************************************/
  187. typedef struct tsp_vectors tsp_vectors_t;
  188. /*******************************************************************************
  189. * Function & Data prototypes
  190. ******************************************************************************/
  191. uint64_t tspd_enter_sp(uint64_t *c_rt_ctx);
  192. void __dead2 tspd_exit_sp(uint64_t c_rt_ctx, uint64_t ret);
  193. uint64_t tspd_synchronous_sp_entry(tsp_context_t *tsp_ctx);
  194. void __dead2 tspd_synchronous_sp_exit(tsp_context_t *tsp_ctx, uint64_t ret);
  195. void tspd_init_tsp_ep_state(struct entry_point_info *tsp_entry_point,
  196. uint32_t rw,
  197. uint64_t pc,
  198. tsp_context_t *tsp_ctx);
  199. int tspd_abort_preempted_smc(tsp_context_t *tsp_ctx);
  200. uint64_t tspd_handle_sp_preemption(void *handle);
  201. extern tsp_context_t tspd_sp_context[TSPD_CORE_COUNT];
  202. extern tsp_vectors_t *tsp_vectors;
  203. #endif /*__ASSEMBLER__*/
  204. #endif /* TSPD_PRIVATE_H */