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