sp_min_main.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (c) 2016-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <platform_def.h>
  11. #include <arch.h>
  12. #include <arch_helpers.h>
  13. #include <common/bl_common.h>
  14. #include <common/build_message.h>
  15. #include <common/debug.h>
  16. #include <common/runtime_svc.h>
  17. #include <context.h>
  18. #include <drivers/console.h>
  19. #include <lib/el3_runtime/context_mgmt.h>
  20. #include <lib/pmf/pmf.h>
  21. #include <lib/psci/psci.h>
  22. #include <lib/runtime_instr.h>
  23. #include <lib/utils.h>
  24. #include <plat/common/platform.h>
  25. #include <platform_sp_min.h>
  26. #include <services/std_svc.h>
  27. #include <smccc_helpers.h>
  28. #include "sp_min_private.h"
  29. #if ENABLE_RUNTIME_INSTRUMENTATION
  30. PMF_REGISTER_SERVICE_SMC(rt_instr_svc, PMF_RT_INSTR_SVC_ID,
  31. RT_INSTR_TOTAL_IDS, PMF_STORE_ENABLE)
  32. #endif
  33. /* Pointers to per-core cpu contexts */
  34. static void *sp_min_cpu_ctx_ptr[PLATFORM_CORE_COUNT];
  35. /* SP_MIN only stores the non secure smc context */
  36. static smc_ctx_t sp_min_smc_context[PLATFORM_CORE_COUNT];
  37. /******************************************************************************
  38. * Define the smccc helper library APIs
  39. *****************************************************************************/
  40. void *smc_get_ctx(unsigned int security_state)
  41. {
  42. assert(security_state == NON_SECURE);
  43. return &sp_min_smc_context[plat_my_core_pos()];
  44. }
  45. void smc_set_next_ctx(unsigned int security_state)
  46. {
  47. assert(security_state == NON_SECURE);
  48. /* SP_MIN stores only non secure smc context. Nothing to do here */
  49. }
  50. void *smc_get_next_ctx(void)
  51. {
  52. return &sp_min_smc_context[plat_my_core_pos()];
  53. }
  54. /*******************************************************************************
  55. * This function returns a pointer to the most recent 'cpu_context' structure
  56. * for the calling CPU that was set as the context for the specified security
  57. * state. NULL is returned if no such structure has been specified.
  58. ******************************************************************************/
  59. void *cm_get_context(uint32_t security_state)
  60. {
  61. assert(security_state == NON_SECURE);
  62. return sp_min_cpu_ctx_ptr[plat_my_core_pos()];
  63. }
  64. /*******************************************************************************
  65. * This function sets the pointer to the current 'cpu_context' structure for the
  66. * specified security state for the calling CPU
  67. ******************************************************************************/
  68. void cm_set_context(void *context, uint32_t security_state)
  69. {
  70. assert(security_state == NON_SECURE);
  71. sp_min_cpu_ctx_ptr[plat_my_core_pos()] = context;
  72. }
  73. /*******************************************************************************
  74. * This function returns a pointer to the most recent 'cpu_context' structure
  75. * for the CPU identified by `cpu_idx` that was set as the context for the
  76. * specified security state. NULL is returned if no such structure has been
  77. * specified.
  78. ******************************************************************************/
  79. void *cm_get_context_by_index(unsigned int cpu_idx,
  80. unsigned int security_state)
  81. {
  82. assert(security_state == NON_SECURE);
  83. return sp_min_cpu_ctx_ptr[cpu_idx];
  84. }
  85. /*******************************************************************************
  86. * This function sets the pointer to the current 'cpu_context' structure for the
  87. * specified security state for the CPU identified by CPU index.
  88. ******************************************************************************/
  89. void cm_set_context_by_index(unsigned int cpu_idx, void *context,
  90. unsigned int security_state)
  91. {
  92. assert(security_state == NON_SECURE);
  93. sp_min_cpu_ctx_ptr[cpu_idx] = context;
  94. }
  95. static void copy_cpu_ctx_to_smc_stx(const regs_t *cpu_reg_ctx,
  96. smc_ctx_t *next_smc_ctx)
  97. {
  98. next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
  99. next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
  100. next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
  101. next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
  102. next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
  103. next_smc_ctx->scr = read_ctx_reg(cpu_reg_ctx, CTX_SCR);
  104. }
  105. /*******************************************************************************
  106. * This function invokes the PSCI library interface to initialize the
  107. * non secure cpu context and copies the relevant cpu context register values
  108. * to smc context. These registers will get programmed during `smc_exit`.
  109. ******************************************************************************/
  110. static void sp_min_prepare_next_image_entry(void)
  111. {
  112. entry_point_info_t *next_image_info;
  113. cpu_context_t *ctx = cm_get_context(NON_SECURE);
  114. u_register_t ns_sctlr;
  115. /* Program system registers to proceed to non-secure */
  116. next_image_info = sp_min_plat_get_bl33_ep_info();
  117. assert(next_image_info);
  118. assert(NON_SECURE == GET_SECURITY_STATE(next_image_info->h.attr));
  119. INFO("SP_MIN: Preparing exit to normal world\n");
  120. print_entry_point_info(next_image_info);
  121. psci_prepare_next_non_secure_ctx(next_image_info);
  122. smc_set_next_ctx(NON_SECURE);
  123. /* Copy r0, lr and spsr from cpu context to SMC context */
  124. copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)),
  125. smc_get_next_ctx());
  126. /* Temporarily set the NS bit to access NS SCTLR */
  127. write_scr(read_scr() | SCR_NS_BIT);
  128. isb();
  129. ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
  130. write_sctlr(ns_sctlr);
  131. isb();
  132. write_scr(read_scr() & ~SCR_NS_BIT);
  133. isb();
  134. }
  135. /******************************************************************************
  136. * Implement the ARM Standard Service function to get arguments for a
  137. * particular service.
  138. *****************************************************************************/
  139. uintptr_t get_arm_std_svc_args(unsigned int svc_mask)
  140. {
  141. /* Setup the arguments for PSCI Library */
  142. DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, sp_min_warm_entrypoint);
  143. /* PSCI is the only ARM Standard Service implemented */
  144. assert(svc_mask == PSCI_FID_MASK);
  145. return (uintptr_t)&psci_args;
  146. }
  147. /******************************************************************************
  148. * The SP_MIN setup function. Calls platforms init functions
  149. *****************************************************************************/
  150. void sp_min_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2,
  151. u_register_t arg3)
  152. {
  153. /* Enable early console if EARLY_CONSOLE flag is enabled */
  154. plat_setup_early_console();
  155. /* Perform early platform-specific setup */
  156. sp_min_early_platform_setup2(arg0, arg1, arg2, arg3);
  157. sp_min_plat_arch_setup();
  158. }
  159. /******************************************************************************
  160. * The SP_MIN main function. Do the platform and PSCI Library setup. Also
  161. * initialize the runtime service framework.
  162. *****************************************************************************/
  163. void sp_min_main(void)
  164. {
  165. NOTICE("SP_MIN: %s\n", build_version_string);
  166. NOTICE("SP_MIN: %s\n", build_message);
  167. /* Perform the SP_MIN platform setup */
  168. sp_min_platform_setup();
  169. /* Initialize the runtime services e.g. psci */
  170. INFO("SP_MIN: Initializing runtime services\n");
  171. runtime_svc_init();
  172. /*
  173. * We are ready to enter the next EL. Prepare entry into the image
  174. * corresponding to the desired security state after the next ERET.
  175. */
  176. sp_min_prepare_next_image_entry();
  177. /*
  178. * Perform any platform specific runtime setup prior to cold boot exit
  179. * from SP_MIN.
  180. */
  181. sp_min_plat_runtime_setup();
  182. console_flush();
  183. }
  184. /******************************************************************************
  185. * This function is invoked during warm boot. Invoke the PSCI library
  186. * warm boot entry point which takes care of Architectural and platform setup/
  187. * restore. Copy the relevant cpu_context register values to smc context which
  188. * will get programmed during `smc_exit`.
  189. *****************************************************************************/
  190. void sp_min_warm_boot(void)
  191. {
  192. smc_ctx_t *next_smc_ctx;
  193. cpu_context_t *ctx = cm_get_context(NON_SECURE);
  194. u_register_t ns_sctlr;
  195. psci_warmboot_entrypoint();
  196. smc_set_next_ctx(NON_SECURE);
  197. next_smc_ctx = smc_get_next_ctx();
  198. zeromem(next_smc_ctx, sizeof(smc_ctx_t));
  199. copy_cpu_ctx_to_smc_stx(get_regs_ctx(cm_get_context(NON_SECURE)),
  200. next_smc_ctx);
  201. /* Temporarily set the NS bit to access NS SCTLR */
  202. write_scr(read_scr() | SCR_NS_BIT);
  203. isb();
  204. ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
  205. write_sctlr(ns_sctlr);
  206. isb();
  207. write_scr(read_scr() & ~SCR_NS_BIT);
  208. isb();
  209. }
  210. #if SP_MIN_WITH_SECURE_FIQ
  211. /******************************************************************************
  212. * This function is invoked on secure interrupts. By construction of the
  213. * SP_MIN, secure interrupts can only be handled when core executes in non
  214. * secure state.
  215. *****************************************************************************/
  216. void sp_min_fiq(void)
  217. {
  218. uint32_t id;
  219. id = plat_ic_acknowledge_interrupt();
  220. sp_min_plat_fiq_handler(id);
  221. plat_ic_end_of_interrupt(id);
  222. }
  223. #endif /* SP_MIN_WITH_SECURE_FIQ */