opteed_private.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef OPTEED_PRIVATE_H
  7. #define OPTEED_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. * OPTEE PM state information e.g. OPTEE is suspended, uninitialised etc
  15. * and macros to access the state information in the per-cpu 'state' flags
  16. ******************************************************************************/
  17. #define OPTEE_PSTATE_OFF 1
  18. #define OPTEE_PSTATE_ON 2
  19. #define OPTEE_PSTATE_SUSPEND 3
  20. #define OPTEE_PSTATE_UNKNOWN 0
  21. #define OPTEE_PSTATE_SHIFT 0
  22. #define OPTEE_PSTATE_MASK 0x3
  23. #define get_optee_pstate(state) ((state >> OPTEE_PSTATE_SHIFT) & \
  24. OPTEE_PSTATE_MASK)
  25. #define clr_optee_pstate(state) (state &= ~(OPTEE_PSTATE_MASK \
  26. << OPTEE_PSTATE_SHIFT))
  27. #define set_optee_pstate(st, pst) do { \
  28. clr_optee_pstate(st); \
  29. st |= (pst & OPTEE_PSTATE_MASK) << \
  30. OPTEE_PSTATE_SHIFT; \
  31. } while (0)
  32. /*******************************************************************************
  33. * OPTEE execution state information i.e. aarch32 or aarch64
  34. ******************************************************************************/
  35. #define OPTEE_AARCH32 MODE_RW_32
  36. #define OPTEE_AARCH64 MODE_RW_64
  37. /*******************************************************************************
  38. * The OPTEED should know the type of OPTEE
  39. ******************************************************************************/
  40. #define OPTEE_TYPE_UP PSCI_TOS_NOT_UP_MIG_CAP
  41. #define OPTEE_TYPE_UPM PSCI_TOS_UP_MIG_CAP
  42. #define OPTEE_TYPE_MP PSCI_TOS_NOT_PRESENT_MP
  43. /*******************************************************************************
  44. * OPTEE migrate type information as known to the OPTEED. We assume that
  45. * the OPTEED is dealing with an MP Secure Payload.
  46. ******************************************************************************/
  47. #define OPTEE_MIGRATE_INFO OPTEE_TYPE_MP
  48. /*******************************************************************************
  49. * Number of cpus that the present on this platform. TODO: Rely on a topology
  50. * tree to determine this in the future to avoid assumptions about mpidr
  51. * allocation
  52. ******************************************************************************/
  53. #define OPTEED_CORE_COUNT PLATFORM_CORE_COUNT
  54. /*******************************************************************************
  55. * Constants that allow assembler code to preserve callee-saved registers of the
  56. * C runtime context while performing a security state switch.
  57. ******************************************************************************/
  58. #define OPTEED_C_RT_CTX_X19 0x0
  59. #define OPTEED_C_RT_CTX_X20 0x8
  60. #define OPTEED_C_RT_CTX_X21 0x10
  61. #define OPTEED_C_RT_CTX_X22 0x18
  62. #define OPTEED_C_RT_CTX_X23 0x20
  63. #define OPTEED_C_RT_CTX_X24 0x28
  64. #define OPTEED_C_RT_CTX_X25 0x30
  65. #define OPTEED_C_RT_CTX_X26 0x38
  66. #define OPTEED_C_RT_CTX_X27 0x40
  67. #define OPTEED_C_RT_CTX_X28 0x48
  68. #define OPTEED_C_RT_CTX_X29 0x50
  69. #define OPTEED_C_RT_CTX_X30 0x58
  70. #define OPTEED_C_RT_CTX_SIZE 0x60
  71. #define OPTEED_C_RT_CTX_ENTRIES (OPTEED_C_RT_CTX_SIZE >> DWORD_SHIFT)
  72. #ifndef __ASSEMBLER__
  73. #include <stdint.h>
  74. #include <lib/cassert.h>
  75. typedef uint32_t optee_vector_isn_t;
  76. typedef struct optee_vectors {
  77. optee_vector_isn_t yield_smc_entry;
  78. optee_vector_isn_t fast_smc_entry;
  79. optee_vector_isn_t cpu_on_entry;
  80. optee_vector_isn_t cpu_off_entry;
  81. optee_vector_isn_t cpu_resume_entry;
  82. optee_vector_isn_t cpu_suspend_entry;
  83. optee_vector_isn_t fiq_entry;
  84. optee_vector_isn_t system_off_entry;
  85. optee_vector_isn_t system_reset_entry;
  86. } optee_vectors_t;
  87. /*
  88. * The number of arguments to save during a SMC call for OPTEE.
  89. * Currently only x1 and x2 are used by OPTEE.
  90. */
  91. #define OPTEE_NUM_ARGS 0x2
  92. /* AArch64 callee saved general purpose register context structure. */
  93. DEFINE_REG_STRUCT(c_rt_regs, OPTEED_C_RT_CTX_ENTRIES);
  94. /*
  95. * Compile time assertion to ensure that both the compiler and linker
  96. * have the same double word aligned view of the size of the C runtime
  97. * register context.
  98. */
  99. CASSERT(OPTEED_C_RT_CTX_SIZE == sizeof(c_rt_regs_t),
  100. assert_spd_c_rt_regs_size_mismatch);
  101. /*******************************************************************************
  102. * Structure which helps the OPTEED to maintain the per-cpu state of OPTEE.
  103. * 'state' - collection of flags to track OPTEE state e.g. on/off
  104. * 'mpidr' - mpidr to associate a context with a cpu
  105. * 'c_rt_ctx' - stack address to restore C runtime context from after
  106. * returning from a synchronous entry into OPTEE.
  107. * 'cpu_ctx' - space to maintain OPTEE architectural state
  108. ******************************************************************************/
  109. typedef struct optee_context {
  110. uint32_t state;
  111. uint64_t mpidr;
  112. uint64_t c_rt_ctx;
  113. cpu_context_t cpu_ctx;
  114. } optee_context_t;
  115. /* OPTEED power management handlers */
  116. extern const spd_pm_ops_t opteed_pm;
  117. /*******************************************************************************
  118. * Forward declarations
  119. ******************************************************************************/
  120. struct optee_vectors;
  121. /*******************************************************************************
  122. * Function & Data prototypes
  123. ******************************************************************************/
  124. uint64_t opteed_enter_sp(uint64_t *c_rt_ctx);
  125. void __dead2 opteed_exit_sp(uint64_t c_rt_ctx, uint64_t ret);
  126. uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx);
  127. void __dead2 opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret);
  128. void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point,
  129. uint32_t rw, uint64_t pc, uint64_t arg0,
  130. uint64_t arg1, uint64_t arg2, uint64_t arg3,
  131. optee_context_t *optee_ctx);
  132. void opteed_cpu_on_finish_handler(u_register_t unused);
  133. extern optee_context_t opteed_sp_context[OPTEED_CORE_COUNT];
  134. extern uint32_t opteed_rw;
  135. extern struct optee_vectors *optee_vector_table;
  136. #endif /*__ASSEMBLER__*/
  137. #endif /* OPTEED_PRIVATE_H */