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