opteed_pm.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <arch_helpers.h>
  8. #include <common/bl_common.h>
  9. #include <common/debug.h>
  10. #include <lib/el3_runtime/context_mgmt.h>
  11. #include <plat/common/platform.h>
  12. #include "opteed_private.h"
  13. /*******************************************************************************
  14. * The target cpu is being turned on. Allow the OPTEED/OPTEE to perform any
  15. * actions needed. Nothing at the moment.
  16. ******************************************************************************/
  17. static void opteed_cpu_on_handler(u_register_t target_cpu)
  18. {
  19. }
  20. /*******************************************************************************
  21. * This cpu is being turned off. Allow the OPTEED/OPTEE to perform any actions
  22. * needed
  23. ******************************************************************************/
  24. static int32_t opteed_cpu_off_handler(u_register_t unused)
  25. {
  26. int32_t rc = 0;
  27. uint32_t linear_id = plat_my_core_pos();
  28. optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
  29. if (get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_UNKNOWN) {
  30. return 0;
  31. }
  32. assert(optee_vector_table);
  33. assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);
  34. /* Program the entry point and enter OPTEE */
  35. cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_off_entry);
  36. rc = opteed_synchronous_sp_entry(optee_ctx);
  37. /*
  38. * Read the response from OPTEE. A non-zero return means that
  39. * something went wrong while communicating with OPTEE.
  40. */
  41. if (rc != 0)
  42. panic();
  43. /*
  44. * Reset OPTEE's context for a fresh start when this cpu is turned on
  45. * subsequently.
  46. */
  47. set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_OFF);
  48. return 0;
  49. }
  50. /*******************************************************************************
  51. * This cpu is being suspended. S-EL1 state must have been saved in the
  52. * resident cpu (mpidr format) if it is a UP/UP migratable OPTEE.
  53. ******************************************************************************/
  54. static void opteed_cpu_suspend_handler(u_register_t max_off_pwrlvl)
  55. {
  56. int32_t rc = 0;
  57. uint32_t linear_id = plat_my_core_pos();
  58. optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
  59. if (get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_UNKNOWN) {
  60. return;
  61. }
  62. assert(optee_vector_table);
  63. assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);
  64. write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx), CTX_GPREG_X0,
  65. max_off_pwrlvl);
  66. /* Program the entry point and enter OPTEE */
  67. cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_suspend_entry);
  68. rc = opteed_synchronous_sp_entry(optee_ctx);
  69. /*
  70. * Read the response from OPTEE. A non-zero return means that
  71. * something went wrong while communicating with OPTEE.
  72. */
  73. if (rc != 0)
  74. panic();
  75. /* Update its context to reflect the state OPTEE is in */
  76. set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_SUSPEND);
  77. }
  78. /*******************************************************************************
  79. * This cpu has been turned on. Enter OPTEE to initialise S-EL1 and other bits
  80. * before passing control back to the Secure Monitor. Entry in S-El1 is done
  81. * after initialising minimal architectural state that guarantees safe
  82. * execution.
  83. ******************************************************************************/
  84. void opteed_cpu_on_finish_handler(u_register_t unused)
  85. {
  86. int32_t rc = 0;
  87. uint32_t linear_id = plat_my_core_pos();
  88. optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
  89. entry_point_info_t optee_on_entrypoint;
  90. assert(optee_vector_table);
  91. assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_OFF ||
  92. get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_UNKNOWN);
  93. opteed_init_optee_ep_state(&optee_on_entrypoint, opteed_rw,
  94. (uint64_t)&optee_vector_table->cpu_on_entry,
  95. 0, 0, 0, 0, optee_ctx);
  96. /* Initialise this cpu's secure context */
  97. cm_init_my_context(&optee_on_entrypoint);
  98. /* Enter OPTEE */
  99. rc = opteed_synchronous_sp_entry(optee_ctx);
  100. /*
  101. * Read the response from OPTEE. A non-zero return means that
  102. * something went wrong while communicating with OPTEE.
  103. */
  104. if (rc != 0)
  105. panic();
  106. /* Update its context to reflect the state OPTEE is in */
  107. set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_ON);
  108. }
  109. /*******************************************************************************
  110. * This cpu has resumed from suspend. The OPTEED saved the OPTEE context when it
  111. * completed the preceding suspend call. Use that context to program an entry
  112. * into OPTEE to allow it to do any remaining book keeping
  113. ******************************************************************************/
  114. static void opteed_cpu_suspend_finish_handler(u_register_t max_off_pwrlvl)
  115. {
  116. int32_t rc = 0;
  117. uint32_t linear_id = plat_my_core_pos();
  118. optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
  119. if (get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_UNKNOWN) {
  120. return;
  121. }
  122. assert(optee_vector_table);
  123. assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_SUSPEND);
  124. /* Program the entry point, max_off_pwrlvl and enter the SP */
  125. write_ctx_reg(get_gpregs_ctx(&optee_ctx->cpu_ctx),
  126. CTX_GPREG_X0,
  127. max_off_pwrlvl);
  128. cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->cpu_resume_entry);
  129. rc = opteed_synchronous_sp_entry(optee_ctx);
  130. /*
  131. * Read the response from OPTEE. A non-zero return means that
  132. * something went wrong while communicating with OPTEE.
  133. */
  134. if (rc != 0)
  135. panic();
  136. /* Update its context to reflect the state OPTEE is in */
  137. set_optee_pstate(optee_ctx->state, OPTEE_PSTATE_ON);
  138. }
  139. /*******************************************************************************
  140. * Return the type of OPTEE the OPTEED is dealing with. Report the current
  141. * resident cpu (mpidr format) if it is a UP/UP migratable OPTEE.
  142. ******************************************************************************/
  143. static int32_t opteed_cpu_migrate_info(u_register_t *resident_cpu)
  144. {
  145. return OPTEE_MIGRATE_INFO;
  146. }
  147. /*******************************************************************************
  148. * System is about to be switched off. Allow the OPTEED/OPTEE to perform
  149. * any actions needed.
  150. ******************************************************************************/
  151. static void opteed_system_off(void)
  152. {
  153. uint32_t linear_id = plat_my_core_pos();
  154. optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
  155. /*
  156. * OP-TEE must have been initialized in order to reach this location so
  157. * it is safe to init the CPU context if not already done for this core.
  158. */
  159. if (get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_UNKNOWN) {
  160. opteed_cpu_on_finish_handler(0);
  161. }
  162. assert(optee_vector_table);
  163. assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);
  164. /* Program the entry point */
  165. cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->system_off_entry);
  166. /* Enter OPTEE. We do not care about the return value because we
  167. * must continue the shutdown anyway */
  168. opteed_synchronous_sp_entry(optee_ctx);
  169. }
  170. /*******************************************************************************
  171. * System is about to be reset. Allow the OPTEED/OPTEE to perform
  172. * any actions needed.
  173. ******************************************************************************/
  174. static void opteed_system_reset(void)
  175. {
  176. uint32_t linear_id = plat_my_core_pos();
  177. optee_context_t *optee_ctx = &opteed_sp_context[linear_id];
  178. /*
  179. * OP-TEE must have been initialized in order to reach this location so
  180. * it is safe to init the CPU context if not already done for this core.
  181. */
  182. if (get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_UNKNOWN) {
  183. opteed_cpu_on_finish_handler(0);
  184. }
  185. assert(optee_vector_table);
  186. assert(get_optee_pstate(optee_ctx->state) == OPTEE_PSTATE_ON);
  187. /* Program the entry point */
  188. cm_set_elr_el3(SECURE, (uint64_t) &optee_vector_table->system_reset_entry);
  189. /* Enter OPTEE. We do not care about the return value because we
  190. * must continue the reset anyway */
  191. opteed_synchronous_sp_entry(optee_ctx);
  192. }
  193. /*******************************************************************************
  194. * Structure populated by the OPTEE Dispatcher to be given a chance to
  195. * perform any OPTEE bookkeeping before PSCI executes a power mgmt.
  196. * operation.
  197. ******************************************************************************/
  198. const spd_pm_ops_t opteed_pm = {
  199. .svc_on = opteed_cpu_on_handler,
  200. .svc_off = opteed_cpu_off_handler,
  201. .svc_suspend = opteed_cpu_suspend_handler,
  202. .svc_on_finish = opteed_cpu_on_finish_handler,
  203. .svc_suspend_finish = opteed_cpu_suspend_finish_handler,
  204. .svc_migrate = NULL,
  205. .svc_migrate_info = opteed_cpu_migrate_info,
  206. .svc_system_off = opteed_system_off,
  207. .svc_system_reset = opteed_system_reset,
  208. };