tspd_pm.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2013-2016, 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 <bl32/tsp/tsp.h>
  9. #include <common/bl_common.h>
  10. #include <common/debug.h>
  11. #include <lib/el3_runtime/context_mgmt.h>
  12. #include <plat/common/platform.h>
  13. #include "tspd_private.h"
  14. /*******************************************************************************
  15. * The target cpu is being turned on. Allow the TSPD/TSP to perform any actions
  16. * needed. Nothing at the moment.
  17. ******************************************************************************/
  18. static void tspd_cpu_on_handler(u_register_t target_cpu)
  19. {
  20. }
  21. /*******************************************************************************
  22. * This cpu is being turned off. Allow the TSPD/TSP to perform any actions
  23. * needed
  24. ******************************************************************************/
  25. static int32_t tspd_cpu_off_handler(u_register_t unused)
  26. {
  27. int32_t rc = 0;
  28. uint32_t linear_id = plat_my_core_pos();
  29. tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
  30. assert(tsp_vectors);
  31. assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
  32. /*
  33. * Abort any preempted SMC request before overwriting the SECURE
  34. * context.
  35. */
  36. tspd_abort_preempted_smc(tsp_ctx);
  37. /* Program the entry point and enter the TSP */
  38. cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_off_entry);
  39. rc = tspd_synchronous_sp_entry(tsp_ctx);
  40. /*
  41. * Read the response from the TSP. A non-zero return means that
  42. * something went wrong while communicating with the TSP.
  43. */
  44. if (rc != 0)
  45. panic();
  46. /*
  47. * Reset TSP's context for a fresh start when this cpu is turned on
  48. * subsequently.
  49. */
  50. set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
  51. return 0;
  52. }
  53. /*******************************************************************************
  54. * This cpu is being suspended. S-EL1 state must have been saved in the
  55. * resident cpu (mpidr format) if it is a UP/UP migratable TSP.
  56. ******************************************************************************/
  57. static void tspd_cpu_suspend_handler(u_register_t max_off_pwrlvl)
  58. {
  59. int32_t rc = 0;
  60. uint32_t linear_id = plat_my_core_pos();
  61. tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
  62. assert(tsp_vectors);
  63. assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
  64. /*
  65. * Abort any preempted SMC request before overwriting the SECURE
  66. * context.
  67. */
  68. tspd_abort_preempted_smc(tsp_ctx);
  69. /* Program the entry point and enter the TSP */
  70. cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_suspend_entry);
  71. rc = tspd_synchronous_sp_entry(tsp_ctx);
  72. /*
  73. * Read the response from the TSP. A non-zero return means that
  74. * something went wrong while communicating with the TSP.
  75. */
  76. if (rc)
  77. panic();
  78. /* Update its context to reflect the state the TSP is in */
  79. set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_SUSPEND);
  80. }
  81. /*******************************************************************************
  82. * This cpu has been turned on. Enter the TSP to initialise S-EL1 and other bits
  83. * before passing control back to the Secure Monitor. Entry in S-EL1 is done
  84. * after initialising minimal architectural state that guarantees safe
  85. * execution.
  86. ******************************************************************************/
  87. static void tspd_cpu_on_finish_handler(u_register_t unused)
  88. {
  89. int32_t rc = 0;
  90. uint32_t linear_id = plat_my_core_pos();
  91. tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
  92. entry_point_info_t tsp_on_entrypoint;
  93. assert(tsp_vectors);
  94. assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_OFF);
  95. tspd_init_tsp_ep_state(&tsp_on_entrypoint,
  96. TSP_AARCH64,
  97. (uint64_t) &tsp_vectors->cpu_on_entry,
  98. tsp_ctx);
  99. /* Initialise this cpu's secure context */
  100. cm_init_my_context(&tsp_on_entrypoint);
  101. #if TSP_NS_INTR_ASYNC_PREEMPT
  102. /*
  103. * Disable the NS interrupt locally since it will be enabled globally
  104. * within cm_init_my_context.
  105. */
  106. disable_intr_rm_local(INTR_TYPE_NS, SECURE);
  107. #endif
  108. /* Enter the TSP */
  109. rc = tspd_synchronous_sp_entry(tsp_ctx);
  110. /*
  111. * Read the response from the TSP. A non-zero return means that
  112. * something went wrong while communicating with the SP.
  113. */
  114. if (rc != 0)
  115. panic();
  116. /* Update its context to reflect the state the SP is in */
  117. set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
  118. }
  119. /*******************************************************************************
  120. * This cpu has resumed from suspend. The SPD saved the TSP context when it
  121. * completed the preceding suspend call. Use that context to program an entry
  122. * into the TSP to allow it to do any remaining book keeping
  123. ******************************************************************************/
  124. static void tspd_cpu_suspend_finish_handler(u_register_t max_off_pwrlvl)
  125. {
  126. int32_t rc = 0;
  127. uint32_t linear_id = plat_my_core_pos();
  128. tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
  129. assert(tsp_vectors);
  130. assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_SUSPEND);
  131. /* Program the entry point, max_off_pwrlvl and enter the SP */
  132. write_ctx_reg(get_gpregs_ctx(&tsp_ctx->cpu_ctx),
  133. CTX_GPREG_X0,
  134. max_off_pwrlvl);
  135. cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_resume_entry);
  136. rc = tspd_synchronous_sp_entry(tsp_ctx);
  137. /*
  138. * Read the response from the TSP. A non-zero return means that
  139. * something went wrong while communicating with the TSP.
  140. */
  141. if (rc != 0)
  142. panic();
  143. /* Update its context to reflect the state the SP is in */
  144. set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
  145. }
  146. /*******************************************************************************
  147. * Return the type of TSP the TSPD is dealing with. Report the current resident
  148. * cpu (mpidr format) if it is a UP/UP migratable TSP.
  149. ******************************************************************************/
  150. static int32_t tspd_cpu_migrate_info(u_register_t *resident_cpu)
  151. {
  152. return TSP_MIGRATE_INFO;
  153. }
  154. /*******************************************************************************
  155. * System is about to be switched off. Allow the TSPD/TSP to perform
  156. * any actions needed.
  157. ******************************************************************************/
  158. static void tspd_system_off(void)
  159. {
  160. uint32_t linear_id = plat_my_core_pos();
  161. tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
  162. assert(tsp_vectors);
  163. assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
  164. /*
  165. * Abort any preempted SMC request before overwriting the SECURE
  166. * context.
  167. */
  168. tspd_abort_preempted_smc(tsp_ctx);
  169. /* Program the entry point */
  170. cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_off_entry);
  171. /* Enter the TSP. We do not care about the return value because we
  172. * must continue the shutdown anyway */
  173. tspd_synchronous_sp_entry(tsp_ctx);
  174. }
  175. /*******************************************************************************
  176. * System is about to be reset. Allow the TSPD/TSP to perform
  177. * any actions needed.
  178. ******************************************************************************/
  179. static void tspd_system_reset(void)
  180. {
  181. uint32_t linear_id = plat_my_core_pos();
  182. tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
  183. assert(tsp_vectors);
  184. assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
  185. /*
  186. * Abort any preempted SMC request before overwriting the SECURE
  187. * context.
  188. */
  189. tspd_abort_preempted_smc(tsp_ctx);
  190. /* Program the entry point */
  191. cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_reset_entry);
  192. /*
  193. * Enter the TSP. We do not care about the return value because we
  194. * must continue the reset anyway
  195. */
  196. tspd_synchronous_sp_entry(tsp_ctx);
  197. }
  198. /*******************************************************************************
  199. * Structure populated by the TSP Dispatcher to be given a chance to perform any
  200. * TSP bookkeeping before PSCI executes a power mgmt. operation.
  201. ******************************************************************************/
  202. const spd_pm_ops_t tspd_pm = {
  203. .svc_on = tspd_cpu_on_handler,
  204. .svc_off = tspd_cpu_off_handler,
  205. .svc_suspend = tspd_cpu_suspend_handler,
  206. .svc_on_finish = tspd_cpu_on_finish_handler,
  207. .svc_suspend_finish = tspd_cpu_suspend_finish_handler,
  208. .svc_migrate = NULL,
  209. .svc_migrate_info = tspd_cpu_migrate_info,
  210. .svc_system_off = tspd_system_off,
  211. .svc_system_reset = tspd_system_reset
  212. };