blob: 2f1beebaa721890c893f419f8ea46614b979861a [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Zelalem Aweked20052f2022-04-04 17:42:48 -05002 * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved.
Varun Wadekar2b287272022-09-13 12:38:47 +01003 * Copyright (c) 2022, NVIDIA Corporation. All rights reserved.
Achin Gupta7aea9082014-02-01 07:51:28 +00004 *
dp-arm82cb2c12017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta7aea9082014-02-01 07:51:28 +00006 */
7
Dan Handley97043ac2014-04-09 13:14:54 +01008#include <assert.h>
Antonio Nino Diaz40daecc2018-10-25 16:52:26 +01009#include <stdbool.h>
Andrew Thoelke167a9352014-06-04 21:10:52 +010010#include <string.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000011
12#include <platform_def.h>
13
14#include <arch.h>
15#include <arch_helpers.h>
Soby Mathewb7e398d2019-07-12 09:23:38 +010016#include <arch_features.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000017#include <bl31/interrupt_mgmt.h>
18#include <common/bl_common.h>
Claus Pedersen885e2682022-09-12 22:42:58 +000019#include <common/debug.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000020#include <context.h>
Zelalem Aweke8b95e842022-01-31 16:59:42 -060021#include <drivers/arm/gicv3.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000022#include <lib/el3_runtime/context_mgmt.h>
23#include <lib/el3_runtime/pubsub_events.h>
24#include <lib/extensions/amu.h>
johpow01744ad972022-01-28 17:06:20 -060025#include <lib/extensions/brbe.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000026#include <lib/extensions/mpam.h>
johpow01dc78e622021-07-08 14:14:00 -050027#include <lib/extensions/sme.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000028#include <lib/extensions/spe.h>
29#include <lib/extensions/sve.h>
Manish V Badarkhed4582d32021-06-29 11:44:20 +010030#include <lib/extensions/sys_reg_trace.h>
Manish V Badarkhe813524e2021-07-02 09:10:56 +010031#include <lib/extensions/trbe.h>
Manish V Badarkhe8fcd3d92021-07-08 09:33:18 +010032#include <lib/extensions/trf.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000033#include <lib/utils.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000034
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +010035#if ENABLE_FEAT_TWED
36/* Make sure delay value fits within the range(0-15) */
37CASSERT(((TWED_DELAY & ~SCR_TWEDEL_MASK) == 0U), assert_twed_delay_value_check);
38#endif /* ENABLE_FEAT_TWED */
Achin Gupta7aea9082014-02-01 07:51:28 +000039
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +010040static void manage_extensions_secure(cpu_context_t *ctx);
Zelalem Awekeb515f542022-04-08 16:48:05 -050041
42static void setup_el1_context(cpu_context_t *ctx, const struct entry_point_info *ep)
43{
44 u_register_t sctlr_elx, actlr_elx;
45
46 /*
47 * Initialise SCTLR_EL1 to the reset value corresponding to the target
48 * execution state setting all fields rather than relying on the hw.
49 * Some fields have architecturally UNKNOWN reset values and these are
50 * set to zero.
51 *
52 * SCTLR.EE: Endianness is taken from the entrypoint attributes.
53 *
54 * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as
55 * required by PSCI specification)
56 */
57 sctlr_elx = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
58 if (GET_RW(ep->spsr) == MODE_RW_64) {
59 sctlr_elx |= SCTLR_EL1_RES1;
60 } else {
61 /*
62 * If the target execution state is AArch32 then the following
63 * fields need to be set.
64 *
65 * SCTRL_EL1.nTWE: Set to one so that EL0 execution of WFE
66 * instructions are not trapped to EL1.
67 *
68 * SCTLR_EL1.nTWI: Set to one so that EL0 execution of WFI
69 * instructions are not trapped to EL1.
70 *
71 * SCTLR_EL1.CP15BEN: Set to one to enable EL0 execution of the
72 * CP15DMB, CP15DSB, and CP15ISB instructions.
73 */
74 sctlr_elx |= SCTLR_AARCH32_EL1_RES1 | SCTLR_CP15BEN_BIT
75 | SCTLR_NTWI_BIT | SCTLR_NTWE_BIT;
76 }
77
78#if ERRATA_A75_764081
79 /*
80 * If workaround of errata 764081 for Cortex-A75 is used then set
81 * SCTLR_EL1.IESB to enable Implicit Error Synchronization Barrier.
82 */
83 sctlr_elx |= SCTLR_IESB_BIT;
84#endif
85 /* Store the initialised SCTLR_EL1 value in the cpu_context */
86 write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
87
88 /*
89 * Base the context ACTLR_EL1 on the current value, as it is
90 * implementation defined. The context restore process will write
91 * the value from the context to the actual register and can cause
92 * problems for processor cores that don't expect certain bits to
93 * be zero.
94 */
95 actlr_elx = read_actlr_el1();
96 write_ctx_reg((get_el1_sysregs_ctx(ctx)), (CTX_ACTLR_EL1), (actlr_elx));
97}
98
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -060099/******************************************************************************
100 * This function performs initializations that are specific to SECURE state
101 * and updates the cpu context specified by 'ctx'.
102 *****************************************************************************/
103static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep)
Achin Gupta7aea9082014-02-01 07:51:28 +0000104{
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600105 u_register_t scr_el3;
106 el3_state_t *state;
107
108 state = get_el3state_ctx(ctx);
109 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
110
111#if defined(IMAGE_BL31) && !defined(SPD_spmd)
Achin Gupta7aea9082014-02-01 07:51:28 +0000112 /*
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600113 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
114 * indicated by the interrupt routing model for BL31.
Achin Gupta7aea9082014-02-01 07:51:28 +0000115 */
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600116 scr_el3 |= get_scr_el3_from_routing_model(SECURE);
117#endif
118
119#if !CTX_INCLUDE_MTE_REGS || ENABLE_ASSERTIONS
120 /* Get Memory Tagging Extension support level */
121 unsigned int mte = get_armv8_5_mte_support();
122#endif
123 /*
124 * Allow access to Allocation Tags when CTX_INCLUDE_MTE_REGS
125 * is set, or when MTE is only implemented at EL0.
126 */
127#if CTX_INCLUDE_MTE_REGS
128 assert((mte == MTE_IMPLEMENTED_ELX) || (mte == MTE_IMPLEMENTED_ASY));
129 scr_el3 |= SCR_ATA_BIT;
130#else
131 if (mte == MTE_IMPLEMENTED_EL0) {
132 scr_el3 |= SCR_ATA_BIT;
133 }
134#endif /* CTX_INCLUDE_MTE_REGS */
135
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600136 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
137
Zelalem Awekeb515f542022-04-08 16:48:05 -0500138 /*
139 * Initialize EL1 context registers unless SPMC is running
140 * at S-EL2.
141 */
142#if !SPMD_SPM_AT_SEL2
143 setup_el1_context(ctx, ep);
144#endif
145
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600146 manage_extensions_secure(ctx);
147}
148
149#if ENABLE_RME
150/******************************************************************************
151 * This function performs initializations that are specific to REALM state
152 * and updates the cpu context specified by 'ctx'.
153 *****************************************************************************/
154static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep)
155{
156 u_register_t scr_el3;
157 el3_state_t *state;
158
159 state = get_el3state_ctx(ctx);
160 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
161
162 scr_el3 |= SCR_NS_BIT | SCR_NSE_BIT | SCR_EnSCXT_BIT;
163
164 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
165}
166#endif /* ENABLE_RME */
167
168/******************************************************************************
169 * This function performs initializations that are specific to NON-SECURE state
170 * and updates the cpu context specified by 'ctx'.
171 *****************************************************************************/
172static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep)
173{
174 u_register_t scr_el3;
175 el3_state_t *state;
176
177 state = get_el3state_ctx(ctx);
178 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
179
180 /* SCR_NS: Set the NS bit */
181 scr_el3 |= SCR_NS_BIT;
182
183#if !CTX_INCLUDE_PAUTH_REGS
184 /*
185 * If the pointer authentication registers aren't saved during world
186 * switches the value of the registers can be leaked from the Secure to
187 * the Non-secure world. To prevent this, rather than enabling pointer
188 * authentication everywhere, we only enable it in the Non-secure world.
189 *
190 * If the Secure world wants to use pointer authentication,
191 * CTX_INCLUDE_PAUTH_REGS must be set to 1.
192 */
193 scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
194#endif /* !CTX_INCLUDE_PAUTH_REGS */
195
196 /* Allow access to Allocation Tags when MTE is implemented. */
197 scr_el3 |= SCR_ATA_BIT;
198
Manish Pandey46cc41d2022-10-10 11:43:08 +0100199#if HANDLE_EA_EL3_FIRST_NS
200 /* SCR_EL3.EA: Route External Abort and SError Interrupt to EL3. */
201 scr_el3 |= SCR_EA_BIT;
202#endif
203
Manish Pandey00e8f792022-09-27 14:30:34 +0100204#if RAS_TRAP_NS_ERR_REC_ACCESS
205 /*
206 * SCR_EL3.TERR: Trap Error record accesses. Accesses to the RAS ERR
207 * and RAS ERX registers from EL1 and EL2(from any security state)
208 * are trapped to EL3.
209 * Set here to trap only for NS EL1/EL2
210 *
211 */
212 scr_el3 |= SCR_TERR_BIT;
213#endif
214
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600215#ifdef IMAGE_BL31
216 /*
217 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
218 * indicated by the interrupt routing model for BL31.
219 */
220 scr_el3 |= get_scr_el3_from_routing_model(NON_SECURE);
221#endif
222 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600223
Zelalem Awekeb515f542022-04-08 16:48:05 -0500224 /* Initialize EL1 context registers */
225 setup_el1_context(ctx, ep);
226
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600227 /* Initialize EL2 context registers */
228#if CTX_INCLUDE_EL2_REGS
229
230 /*
231 * Initialize SCTLR_EL2 context register using Endianness value
232 * taken from the entrypoint attribute.
233 */
234 u_register_t sctlr_el2 = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
235 sctlr_el2 |= SCTLR_EL2_RES1;
236 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_SCTLR_EL2,
237 sctlr_el2);
238
239 /*
Varun Wadekar2b287272022-09-13 12:38:47 +0100240 * Program the ICC_SRE_EL2 to make sure the correct bits are set
241 * when restoring NS context.
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600242 */
Varun Wadekar2b287272022-09-13 12:38:47 +0100243 u_register_t icc_sre_el2 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT |
244 ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT;
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600245 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_ICC_SRE_EL2,
246 icc_sre_el2);
Boyan Karatotev7f856192022-10-26 15:10:39 +0100247
248 /*
249 * Initialize MDCR_EL2.HPMN to its hardware reset value so we don't
250 * throw anyone off who expects this to be sensible.
251 * TODO: A similar thing happens in cm_prepare_el3_exit. They should be
252 * unified with the proper PMU implementation
253 */
254 u_register_t mdcr_el2 = ((read_pmcr_el0() >> PMCR_EL0_N_SHIFT) &
255 PMCR_EL0_N_MASK);
256 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_MDCR_EL2, mdcr_el2);
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600257#endif /* CTX_INCLUDE_EL2_REGS */
Achin Gupta7aea9082014-02-01 07:51:28 +0000258}
259
260/*******************************************************************************
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600261 * The following function performs initialization of the cpu_context 'ctx'
262 * for first use that is common to all security states, and sets the
263 * initial entrypoint state as specified by the entry_point_info structure.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100264 *
Paul Beesley8aabea32019-01-11 18:26:51 +0000265 * The EE and ST attributes are used to configure the endianness and secure
Soby Mathew12d0d002015-04-09 13:40:55 +0100266 * timer availability for the new execution context.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100267 ******************************************************************************/
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600268static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
Andrew Thoelke167a9352014-06-04 21:10:52 +0100269{
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000270 u_register_t scr_el3;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100271 el3_state_t *state;
272 gp_regs_t *gp_regs;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100273
Andrew Thoelke167a9352014-06-04 21:10:52 +0100274 /* Clear any residual register values from the context */
Douglas Raillard32f0d3c2017-01-26 15:54:44 +0000275 zeromem(ctx, sizeof(*ctx));
Andrew Thoelke167a9352014-06-04 21:10:52 +0100276
277 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100278 * SCR_EL3 was initialised during reset sequence in macro
279 * el3_arch_init_common. This code modifies the SCR_EL3 fields that
280 * affect the next EL.
281 *
282 * The following fields are initially set to zero and then updated to
283 * the required value depending on the state of the SPSR_EL3 and the
284 * Security state and entrypoint attributes of the next EL.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100285 */
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000286 scr_el3 = read_scr();
Manish Pandey46cc41d2022-10-10 11:43:08 +0100287 scr_el3 &= ~(SCR_NS_BIT | SCR_RW_BIT | SCR_EA_BIT | SCR_FIQ_BIT | SCR_IRQ_BIT |
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600288 SCR_ST_BIT | SCR_HCE_BIT | SCR_NSE_BIT);
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500289
David Cunado18f2efd2017-04-13 22:38:29 +0100290 /*
291 * SCR_EL3.RW: Set the execution state, AArch32 or AArch64, for next
292 * Exception level as specified by SPSR.
293 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500294 if (GET_RW(ep->spsr) == MODE_RW_64) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100295 scr_el3 |= SCR_RW_BIT;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500296 }
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600297
David Cunado18f2efd2017-04-13 22:38:29 +0100298 /*
299 * SCR_EL3.ST: Traps Secure EL1 accesses to the Counter-timer Physical
Zelalem Awekeb515f542022-04-08 16:48:05 -0500300 * Secure timer registers to EL3, from AArch64 state only, if specified
301 * by the entrypoint attributes. If SEL2 is present and enabled, the ST
302 * bit always behaves as 1 (i.e. secure physical timer register access
303 * is not trapped)
David Cunado18f2efd2017-04-13 22:38:29 +0100304 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500305 if (EP_GET_ST(ep->h.attr) != 0U) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100306 scr_el3 |= SCR_ST_BIT;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500307 }
Andrew Thoelke167a9352014-06-04 21:10:52 +0100308
johpow01cb4ec472021-08-04 19:38:18 -0500309 /*
310 * If FEAT_HCX is enabled, enable access to HCRX_EL2 by setting
311 * SCR_EL3.HXEn.
312 */
313#if ENABLE_FEAT_HCX
314 scr_el3 |= SCR_HXEn_BIT;
315#endif
316
Juan Pablo Condeff86e0b2022-07-12 16:40:29 -0400317 /*
318 * If FEAT_RNG_TRAP is enabled, all reads of the RNDR and RNDRRS
319 * registers are trapped to EL3.
320 */
321#if ENABLE_FEAT_RNG_TRAP
322 scr_el3 |= SCR_TRNDR_BIT;
323#endif
324
Jeenu Viswambharan1a7c1cf2017-12-08 12:13:51 +0000325#if FAULT_INJECTION_SUPPORT
326 /* Enable fault injection from lower ELs */
327 scr_el3 |= SCR_FIEN_BIT;
328#endif
329
Antonio Nino Diaz52839622019-01-31 11:58:00 +0000330 /*
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600331 * CPTR_EL3 was initialized out of reset, copy that value to the
332 * context register.
Antonio Nino Diaz52839622019-01-31 11:58:00 +0000333 */
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100334 write_ctx_reg(get_el3state_ctx(ctx), CTX_CPTR_EL3, read_cptr_el3());
Andrew Thoelke167a9352014-06-04 21:10:52 +0100335
336 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100337 * SCR_EL3.HCE: Enable HVC instructions if next execution state is
338 * AArch64 and next EL is EL2, or if next execution state is AArch32 and
339 * next mode is Hyp.
Jimmy Brisson110ee432020-04-16 10:47:56 -0500340 * SCR_EL3.FGTEn: Enable Fine Grained Virtualization Traps under the
341 * same conditions as HVC instructions and when the processor supports
342 * ARMv8.6-FGT.
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500343 * SCR_EL3.ECVEn: Enable Enhanced Counter Virtualization (ECV)
344 * CNTPOFF_EL2 register under the same conditions as HVC instructions
345 * and when the processor supports ECV.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100346 */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000347 if (((GET_RW(ep->spsr) == MODE_RW_64) && (GET_EL(ep->spsr) == MODE_EL2))
348 || ((GET_RW(ep->spsr) != MODE_RW_64)
349 && (GET_M32(ep->spsr) == MODE32_hyp))) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100350 scr_el3 |= SCR_HCE_BIT;
Jimmy Brisson110ee432020-04-16 10:47:56 -0500351
352 if (is_armv8_6_fgt_present()) {
353 scr_el3 |= SCR_FGTEN_BIT;
354 }
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500355
356 if (get_armv8_6_ecv_support()
357 == ID_AA64MMFR0_EL1_ECV_SELF_SYNCH) {
358 scr_el3 |= SCR_ECVEN_BIT;
359 }
Andrew Thoelke167a9352014-06-04 21:10:52 +0100360 }
361
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +0100362#if ENABLE_FEAT_TWED
johpow016cac7242020-04-22 14:05:13 -0500363 /* Enable WFE trap delay in SCR_EL3 if supported and configured */
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +0100364 /* Set delay in SCR_EL3 */
365 scr_el3 &= ~(SCR_TWEDEL_MASK << SCR_TWEDEL_SHIFT);
366 scr_el3 |= ((TWED_DELAY & SCR_TWEDEL_MASK)
367 << SCR_TWEDEL_SHIFT);
johpow016cac7242020-04-22 14:05:13 -0500368
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +0100369 /* Enable WFE delay */
370 scr_el3 |= SCR_TWEDEn_BIT;
371#endif /* ENABLE_FEAT_TWED */
johpow016cac7242020-04-22 14:05:13 -0500372
Manish Pandey1e785ab2024-03-13 10:41:11 +0000373#if IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2
374 /* Enable S-EL2 if FEAT_SEL2 is implemented for all the contexts. */
375 if (is_armv8_4_sel2_present()) {
376 scr_el3 |= SCR_EEL2_BIT;
377 }
378#endif /* (IMAGE_BL31 && defined(SPD_spmd) && SPMD_SPM_AT_SEL2) */
379
David Cunado18f2efd2017-04-13 22:38:29 +0100380 /*
Alexei Fedorove290a8f2019-08-13 15:17:53 +0100381 * Populate EL3 state so that we've the right context
382 * before doing ERET
383 */
Andrew Thoelke167a9352014-06-04 21:10:52 +0100384 state = get_el3state_ctx(ctx);
385 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
386 write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
387 write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
388
389 /*
390 * Store the X0-X7 value from the entrypoint into the context
391 * Use memcpy as we are in control of the layout of the structures
392 */
393 gp_regs = get_gpregs_ctx(ctx);
394 memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
395}
396
397/*******************************************************************************
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600398 * Context management library initialization routine. This library is used by
399 * runtime services to share pointers to 'cpu_context' structures for secure
400 * non-secure and realm states. Management of the structures and their associated
401 * memory is not done by the context management library e.g. the PSCI service
402 * manages the cpu context used for entry from and exit to the non-secure state.
403 * The Secure payload dispatcher service manages the context(s) corresponding to
404 * the secure state. It also uses this library to get access to the non-secure
405 * state cpu context pointers.
406 * Lastly, this library provides the API to make SP_EL3 point to the cpu context
407 * which will be used for programming an entry into a lower EL. The same context
408 * will be used to save state upon exception entry from that EL.
409 ******************************************************************************/
410void __init cm_init(void)
411{
412 /*
413 * The context management library has only global data to intialize, but
414 * that will be done when the BSS is zeroed out.
415 */
416}
417
418/*******************************************************************************
419 * This is the high-level function used to initialize the cpu_context 'ctx' for
420 * first use. It performs initializations that are common to all security states
421 * and initializations specific to the security state specified in 'ep'
422 ******************************************************************************/
423void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
424{
425 unsigned int security_state;
426
427 assert(ctx != NULL);
428
429 /*
430 * Perform initializations that are common
431 * to all security states
432 */
433 setup_context_common(ctx, ep);
434
435 security_state = GET_SECURITY_STATE(ep->h.attr);
436
437 /* Perform security state specific initializations */
438 switch (security_state) {
439 case SECURE:
440 setup_secure_context(ctx, ep);
441 break;
442#if ENABLE_RME
443 case REALM:
444 setup_realm_context(ctx, ep);
445 break;
446#endif
447 case NON_SECURE:
448 setup_ns_context(ctx, ep);
449 break;
450 default:
451 ERROR("Invalid security state\n");
452 panic();
453 break;
454 }
455}
456
457/*******************************************************************************
Dimitris Papastamos0fd0f222017-11-07 09:55:29 +0000458 * Enable architecture extensions on first entry to Non-secure world.
459 * When EL2 is implemented but unused `el2_unused` is non-zero, otherwise
460 * it is zero.
461 ******************************************************************************/
johpow01dc78e622021-07-08 14:14:00 -0500462static void manage_extensions_nonsecure(bool el2_unused, cpu_context_t *ctx)
Dimitris Papastamos0fd0f222017-11-07 09:55:29 +0000463{
464#if IMAGE_BL31
Dimitris Papastamos281a08c2017-10-13 12:06:06 +0100465#if ENABLE_SPE_FOR_LOWER_ELS
466 spe_enable(el2_unused);
467#endif
Dimitris Papastamos380559c2017-10-12 13:02:29 +0100468
469#if ENABLE_AMU
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100470 amu_enable(el2_unused, ctx);
471#endif
472
johpow01dc78e622021-07-08 14:14:00 -0500473#if ENABLE_SME_FOR_NS
474 /* Enable SME, SVE, and FPU/SIMD for non-secure world. */
475 sme_enable(ctx);
476#elif ENABLE_SVE_FOR_NS
477 /* Enable SVE and FPU/SIMD for non-secure world. */
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100478 sve_enable(ctx);
Dimitris Papastamos380559c2017-10-12 13:02:29 +0100479#endif
David Cunado1a853372017-10-20 11:30:57 +0100480
Jeenu Viswambharan5f835912018-07-31 16:13:33 +0100481#if ENABLE_MPAM_FOR_LOWER_ELS
482 mpam_enable(el2_unused);
483#endif
Manish V Badarkhe813524e2021-07-02 09:10:56 +0100484
485#if ENABLE_TRBE_FOR_NS
486 trbe_enable();
487#endif /* ENABLE_TRBE_FOR_NS */
488
johpow01744ad972022-01-28 17:06:20 -0600489#if ENABLE_BRBE_FOR_NS
490 brbe_enable();
491#endif /* ENABLE_BRBE_FOR_NS */
492
Manish V Badarkhed4582d32021-06-29 11:44:20 +0100493#if ENABLE_SYS_REG_TRACE_FOR_NS
494 sys_reg_trace_enable(ctx);
495#endif /* ENABLE_SYS_REG_TRACE_FOR_NS */
496
Manish V Badarkhe8fcd3d92021-07-08 09:33:18 +0100497#if ENABLE_TRF_FOR_NS
498 trf_enable();
499#endif /* ENABLE_TRF_FOR_NS */
Dimitris Papastamos0fd0f222017-11-07 09:55:29 +0000500#endif
501}
502
503/*******************************************************************************
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100504 * Enable architecture extensions on first entry to Secure world.
505 ******************************************************************************/
johpow01dc78e622021-07-08 14:14:00 -0500506static void manage_extensions_secure(cpu_context_t *ctx)
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100507{
508#if IMAGE_BL31
johpow01dc78e622021-07-08 14:14:00 -0500509 #if ENABLE_SME_FOR_NS
510 #if ENABLE_SME_FOR_SWD
511 /*
512 * Enable SME, SVE, FPU/SIMD in secure context, secure manager must
513 * ensure SME, SVE, and FPU/SIMD context properly managed.
514 */
515 sme_enable(ctx);
516 #else /* ENABLE_SME_FOR_SWD */
517 /*
518 * Disable SME, SVE, FPU/SIMD in secure context so non-secure world can
519 * safely use the associated registers.
520 */
521 sme_disable(ctx);
522 #endif /* ENABLE_SME_FOR_SWD */
523 #elif ENABLE_SVE_FOR_NS
524 #if ENABLE_SVE_FOR_SWD
525 /*
526 * Enable SVE and FPU in secure context, secure manager must ensure that
527 * the SVE and FPU register contexts are properly managed.
528 */
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100529 sve_enable(ctx);
johpow01dc78e622021-07-08 14:14:00 -0500530 #else /* ENABLE_SVE_FOR_SWD */
531 /*
532 * Disable SVE and FPU in secure context so non-secure world can safely
533 * use them.
534 */
535 sve_disable(ctx);
536 #endif /* ENABLE_SVE_FOR_SWD */
537 #endif /* ENABLE_SVE_FOR_NS */
538#endif /* IMAGE_BL31 */
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100539}
540
541/*******************************************************************************
Soby Mathew12d0d002015-04-09 13:40:55 +0100542 * The following function initializes the cpu_context for a CPU specified by
543 * its `cpu_idx` for first use, and sets the initial entrypoint state as
544 * specified by the entry_point_info structure.
545 ******************************************************************************/
546void cm_init_context_by_index(unsigned int cpu_idx,
547 const entry_point_info_t *ep)
548{
549 cpu_context_t *ctx;
550 ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +0100551 cm_setup_context(ctx, ep);
Soby Mathew12d0d002015-04-09 13:40:55 +0100552}
553
554/*******************************************************************************
555 * The following function initializes the cpu_context for the current CPU
556 * for first use, and sets the initial entrypoint state as specified by the
557 * entry_point_info structure.
558 ******************************************************************************/
559void cm_init_my_context(const entry_point_info_t *ep)
560{
561 cpu_context_t *ctx;
562 ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +0100563 cm_setup_context(ctx, ep);
Soby Mathew12d0d002015-04-09 13:40:55 +0100564}
565
566/*******************************************************************************
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500567 * Prepare the CPU system registers for first entry into realm, secure, or
568 * normal world.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100569 *
570 * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
571 * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
572 * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
573 * For all entries, the EL1 registers are initialized from the cpu_context
574 ******************************************************************************/
575void cm_prepare_el3_exit(uint32_t security_state)
576{
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000577 u_register_t sctlr_elx, scr_el3, mdcr_el2;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100578 cpu_context_t *ctx = cm_get_context(security_state);
Antonio Nino Diaz40daecc2018-10-25 16:52:26 +0100579 bool el2_unused = false;
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000580 uint64_t hcr_el2 = 0U;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100581
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000582 assert(ctx != NULL);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100583
584 if (security_state == NON_SECURE) {
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000585 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx),
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000586 CTX_SCR_EL3);
587 if ((scr_el3 & SCR_HCE_BIT) != 0U) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100588 /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
Max Shvetsov28259462020-02-17 16:15:47 +0000589 sctlr_elx = read_ctx_reg(get_el1_sysregs_ctx(ctx),
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000590 CTX_SCTLR_EL1);
Ken Kuang2e09d4f2017-08-23 16:03:29 +0800591 sctlr_elx &= SCTLR_EE_BIT;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100592 sctlr_elx |= SCTLR_EL2_RES1;
Louis Mayencourt5f5d1ed2019-02-20 12:11:41 +0000593#if ERRATA_A75_764081
594 /*
595 * If workaround of errata 764081 for Cortex-A75 is used
596 * then set SCTLR_EL2.IESB to enable Implicit Error
597 * Synchronization Barrier.
598 */
599 sctlr_elx |= SCTLR_IESB_BIT;
600#endif
Andrew Thoelke167a9352014-06-04 21:10:52 +0100601 write_sctlr_el2(sctlr_elx);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000602 } else if (el_implemented(2) != EL_IMPL_NONE) {
Antonio Nino Diaz40daecc2018-10-25 16:52:26 +0100603 el2_unused = true;
Dimitris Papastamos0fd0f222017-11-07 09:55:29 +0000604
David Cunado18f2efd2017-04-13 22:38:29 +0100605 /*
606 * EL2 present but unused, need to disable safely.
607 * SCTLR_EL2 can be ignored in this case.
608 *
Jeenu Viswambharan3ff4aaa2018-08-15 14:29:29 +0100609 * Set EL2 register width appropriately: Set HCR_EL2
610 * field to match SCR_EL3.RW.
David Cunado18f2efd2017-04-13 22:38:29 +0100611 */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000612 if ((scr_el3 & SCR_RW_BIT) != 0U)
Jeenu Viswambharan3ff4aaa2018-08-15 14:29:29 +0100613 hcr_el2 |= HCR_RW_BIT;
614
615 /*
616 * For Armv8.3 pointer authentication feature, disable
617 * traps to EL2 when accessing key registers or using
618 * pointer authentication instructions from lower ELs.
619 */
620 hcr_el2 |= (HCR_API_BIT | HCR_APK_BIT);
621
622 write_hcr_el2(hcr_el2);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100623
David Cunado18f2efd2017-04-13 22:38:29 +0100624 /*
625 * Initialise CPTR_EL2 setting all fields rather than
626 * relying on the hw. All fields have architecturally
627 * UNKNOWN reset values.
628 *
629 * CPTR_EL2.TCPAC: Set to zero so that Non-secure EL1
630 * accesses to the CPACR_EL1 or CPACR from both
631 * Execution states do not trap to EL2.
632 *
633 * CPTR_EL2.TTA: Set to zero so that Non-secure System
634 * register accesses to the trace registers from both
635 * Execution states do not trap to EL2.
Manish V Badarkhed4582d32021-06-29 11:44:20 +0100636 * If PE trace unit System registers are not implemented
637 * then this bit is reserved, and must be set to zero.
David Cunado18f2efd2017-04-13 22:38:29 +0100638 *
639 * CPTR_EL2.TFP: Set to zero so that Non-secure accesses
640 * to SIMD and floating-point functionality from both
641 * Execution states do not trap to EL2.
642 */
643 write_cptr_el2(CPTR_EL2_RESET_VAL &
644 ~(CPTR_EL2_TCPAC_BIT | CPTR_EL2_TTA_BIT
645 | CPTR_EL2_TFP_BIT));
Andrew Thoelke167a9352014-06-04 21:10:52 +0100646
David Cunado18f2efd2017-04-13 22:38:29 +0100647 /*
Paul Beesley8aabea32019-01-11 18:26:51 +0000648 * Initialise CNTHCTL_EL2. All fields are
David Cunado18f2efd2017-04-13 22:38:29 +0100649 * architecturally UNKNOWN on reset and are set to zero
650 * except for field(s) listed below.
651 *
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500652 * CNTHCTL_EL2.EL1PTEN: Set to one to disable traps to
David Cunado18f2efd2017-04-13 22:38:29 +0100653 * Hyp mode of Non-secure EL0 and EL1 accesses to the
654 * physical timer registers.
655 *
656 * CNTHCTL_EL2.EL1PCTEN: Set to one to disable traps to
657 * Hyp mode of Non-secure EL0 and EL1 accesses to the
658 * physical counter registers.
659 */
660 write_cnthctl_el2(CNTHCTL_RESET_VAL |
661 EL1PCEN_BIT | EL1PCTEN_BIT);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100662
David Cunado18f2efd2017-04-13 22:38:29 +0100663 /*
664 * Initialise CNTVOFF_EL2 to zero as it resets to an
665 * architecturally UNKNOWN value.
666 */
Soby Mathew14c05262014-08-29 14:41:58 +0100667 write_cntvoff_el2(0);
668
David Cunado18f2efd2017-04-13 22:38:29 +0100669 /*
670 * Set VPIDR_EL2 and VMPIDR_EL2 to match MIDR_EL1 and
671 * MPIDR_EL1 respectively.
672 */
Andrew Thoelke167a9352014-06-04 21:10:52 +0100673 write_vpidr_el2(read_midr_el1());
674 write_vmpidr_el2(read_mpidr_el1());
Sandrine Bailleux85d80e52015-11-25 17:00:44 +0000675
676 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100677 * Initialise VTTBR_EL2. All fields are architecturally
678 * UNKNOWN on reset.
679 *
680 * VTTBR_EL2.VMID: Set to zero. Even though EL1&0 stage
681 * 2 address translation is disabled, cache maintenance
682 * operations depend on the VMID.
683 *
684 * VTTBR_EL2.BADDR: Set to zero as EL1&0 stage 2 address
685 * translation is disabled.
Sandrine Bailleux85d80e52015-11-25 17:00:44 +0000686 */
David Cunado18f2efd2017-04-13 22:38:29 +0100687 write_vttbr_el2(VTTBR_RESET_VAL &
688 ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT)
689 | (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
690
David Cunado495f3d32016-10-31 17:37:34 +0000691 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100692 * Initialise MDCR_EL2, setting all fields rather than
693 * relying on hw. Some fields are architecturally
694 * UNKNOWN on reset.
695 *
Alexei Fedorove290a8f2019-08-13 15:17:53 +0100696 * MDCR_EL2.HLP: Set to one so that event counter
697 * overflow, that is recorded in PMOVSCLR_EL0[0-30],
698 * occurs on the increment that changes
699 * PMEVCNTR<n>_EL0[63] from 1 to 0, when ARMv8.5-PMU is
700 * implemented. This bit is RES0 in versions of the
701 * architecture earlier than ARMv8.5, setting it to 1
702 * doesn't have any effect on them.
703 *
704 * MDCR_EL2.TTRF: Set to zero so that access to Trace
705 * Filter Control register TRFCR_EL1 at EL1 is not
706 * trapped to EL2. This bit is RES0 in versions of
707 * the architecture earlier than ARMv8.4.
708 *
709 * MDCR_EL2.HPMD: Set to one so that event counting is
710 * prohibited at EL2. This bit is RES0 in versions of
711 * the architecture earlier than ARMv8.1, setting it
712 * to 1 doesn't have any effect on them.
713 *
714 * MDCR_EL2.TPMS: Set to zero so that accesses to
715 * Statistical Profiling control registers from EL1
716 * do not trap to EL2. This bit is RES0 when SPE is
717 * not implemented.
718 *
David Cunado18f2efd2017-04-13 22:38:29 +0100719 * MDCR_EL2.TDRA: Set to zero so that Non-secure EL0 and
720 * EL1 System register accesses to the Debug ROM
721 * registers are not trapped to EL2.
722 *
723 * MDCR_EL2.TDOSA: Set to zero so that Non-secure EL1
724 * System register accesses to the powerdown debug
725 * registers are not trapped to EL2.
726 *
727 * MDCR_EL2.TDA: Set to zero so that System register
728 * accesses to the debug registers do not trap to EL2.
729 *
730 * MDCR_EL2.TDE: Set to zero so that debug exceptions
731 * are not routed to EL2.
732 *
733 * MDCR_EL2.HPME: Set to zero to disable EL2 Performance
734 * Monitors.
735 *
736 * MDCR_EL2.TPM: Set to zero so that Non-secure EL0 and
737 * EL1 accesses to all Performance Monitors registers
738 * are not trapped to EL2.
739 *
740 * MDCR_EL2.TPMCR: Set to zero so that Non-secure EL0
741 * and EL1 accesses to the PMCR_EL0 or PMCR are not
742 * trapped to EL2.
743 *
744 * MDCR_EL2.HPMN: Set to value of PMCR_EL0.N which is the
745 * architecturally-defined reset value.
Manish V Badarkhe40ff9072021-06-23 20:02:39 +0100746 *
747 * MDCR_EL2.E2TB: Set to zero so that the trace Buffer
748 * owning exception level is NS-EL1 and, tracing is
749 * prohibited at NS-EL2. These bits are RES0 when
750 * FEAT_TRBE is not implemented.
David Cunado495f3d32016-10-31 17:37:34 +0000751 */
Alexei Fedorove290a8f2019-08-13 15:17:53 +0100752 mdcr_el2 = ((MDCR_EL2_RESET_VAL | MDCR_EL2_HLP |
753 MDCR_EL2_HPMD) |
754 ((read_pmcr_el0() & PMCR_EL0_N_BITS)
755 >> PMCR_EL0_N_SHIFT)) &
756 ~(MDCR_EL2_TTRF | MDCR_EL2_TPMS |
757 MDCR_EL2_TDRA_BIT | MDCR_EL2_TDOSA_BIT |
758 MDCR_EL2_TDA_BIT | MDCR_EL2_TDE_BIT |
759 MDCR_EL2_HPME_BIT | MDCR_EL2_TPM_BIT |
Manish V Badarkhe40ff9072021-06-23 20:02:39 +0100760 MDCR_EL2_TPMCR_BIT |
761 MDCR_EL2_E2TB(MDCR_EL2_E2TB_EL1));
dp-armd832aee2017-05-23 09:32:49 +0100762
dp-armd832aee2017-05-23 09:32:49 +0100763 write_mdcr_el2(mdcr_el2);
764
David Cunado939f66d2016-11-25 00:21:59 +0000765 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100766 * Initialise HSTR_EL2. All fields are architecturally
767 * UNKNOWN on reset.
768 *
769 * HSTR_EL2.T<n>: Set all these fields to zero so that
770 * Non-secure EL0 or EL1 accesses to System registers
771 * do not trap to EL2.
David Cunado939f66d2016-11-25 00:21:59 +0000772 */
David Cunado18f2efd2017-04-13 22:38:29 +0100773 write_hstr_el2(HSTR_EL2_RESET_VAL & ~(HSTR_EL2_T_MASK));
David Cunado939f66d2016-11-25 00:21:59 +0000774 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100775 * Initialise CNTHP_CTL_EL2. All fields are
776 * architecturally UNKNOWN on reset.
777 *
778 * CNTHP_CTL_EL2:ENABLE: Set to zero to disable the EL2
779 * physical timer and prevent timer interrupts.
David Cunado939f66d2016-11-25 00:21:59 +0000780 */
David Cunado18f2efd2017-04-13 22:38:29 +0100781 write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL &
782 ~(CNTHP_CTL_ENABLE_BIT));
Andrew Thoelke167a9352014-06-04 21:10:52 +0100783 }
johpow01dc78e622021-07-08 14:14:00 -0500784 manage_extensions_nonsecure(el2_unused, ctx);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100785 }
786
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +0100787 cm_el1_sysregs_context_restore(security_state);
788 cm_set_next_eret_context(security_state);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100789}
790
Max Shvetsov28f39f02020-02-25 13:56:19 +0000791#if CTX_INCLUDE_EL2_REGS
792/*******************************************************************************
793 * Save EL2 sysreg context
794 ******************************************************************************/
795void cm_el2_sysregs_context_save(uint32_t security_state)
796{
797 u_register_t scr_el3 = read_scr();
798
799 /*
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500800 * Always save the non-secure and realm EL2 context, only save the
Max Shvetsov28f39f02020-02-25 13:56:19 +0000801 * S-EL2 context if S-EL2 is enabled.
802 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500803 if ((security_state != SECURE) ||
Ruari Phipps6b704da2020-07-28 11:26:29 +0100804 ((security_state == SECURE) && ((scr_el3 & SCR_EEL2_BIT) != 0U))) {
Max Shvetsov28f39f02020-02-25 13:56:19 +0000805 cpu_context_t *ctx;
Zelalem Aweked20052f2022-04-04 17:42:48 -0500806 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsov28f39f02020-02-25 13:56:19 +0000807
808 ctx = cm_get_context(security_state);
809 assert(ctx != NULL);
810
Zelalem Aweked20052f2022-04-04 17:42:48 -0500811 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
812
813 el2_sysregs_context_save_common(el2_sysregs_ctx);
814#if ENABLE_SPE_FOR_LOWER_ELS
815 el2_sysregs_context_save_spe(el2_sysregs_ctx);
816#endif
817#if CTX_INCLUDE_MTE_REGS
818 el2_sysregs_context_save_mte(el2_sysregs_ctx);
819#endif
820#if ENABLE_MPAM_FOR_LOWER_ELS
821 el2_sysregs_context_save_mpam(el2_sysregs_ctx);
822#endif
823#if ENABLE_FEAT_FGT
824 el2_sysregs_context_save_fgt(el2_sysregs_ctx);
825#endif
826#if ENABLE_FEAT_ECV
827 el2_sysregs_context_save_ecv(el2_sysregs_ctx);
828#endif
829#if ENABLE_FEAT_VHE
830 el2_sysregs_context_save_vhe(el2_sysregs_ctx);
831#endif
832#if RAS_EXTENSION
833 el2_sysregs_context_save_ras(el2_sysregs_ctx);
834#endif
835#if CTX_INCLUDE_NEVE_REGS
836 el2_sysregs_context_save_nv2(el2_sysregs_ctx);
837#endif
838#if ENABLE_TRF_FOR_NS
839 el2_sysregs_context_save_trf(el2_sysregs_ctx);
840#endif
841#if ENABLE_FEAT_CSV2_2
842 el2_sysregs_context_save_csv2(el2_sysregs_ctx);
843#endif
844#if ENABLE_FEAT_HCX
845 el2_sysregs_context_save_hcx(el2_sysregs_ctx);
846#endif
Max Shvetsov28f39f02020-02-25 13:56:19 +0000847 }
848}
849
850/*******************************************************************************
851 * Restore EL2 sysreg context
852 ******************************************************************************/
853void cm_el2_sysregs_context_restore(uint32_t security_state)
854{
855 u_register_t scr_el3 = read_scr();
856
857 /*
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500858 * Always restore the non-secure and realm EL2 context, only restore the
Max Shvetsov28f39f02020-02-25 13:56:19 +0000859 * S-EL2 context if S-EL2 is enabled.
860 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500861 if ((security_state != SECURE) ||
Ruari Phipps6b704da2020-07-28 11:26:29 +0100862 ((security_state == SECURE) && ((scr_el3 & SCR_EEL2_BIT) != 0U))) {
Max Shvetsov28f39f02020-02-25 13:56:19 +0000863 cpu_context_t *ctx;
Zelalem Aweked20052f2022-04-04 17:42:48 -0500864 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsov28f39f02020-02-25 13:56:19 +0000865
866 ctx = cm_get_context(security_state);
867 assert(ctx != NULL);
868
Zelalem Aweked20052f2022-04-04 17:42:48 -0500869 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
870
871 el2_sysregs_context_restore_common(el2_sysregs_ctx);
872#if ENABLE_SPE_FOR_LOWER_ELS
873 el2_sysregs_context_restore_spe(el2_sysregs_ctx);
874#endif
875#if CTX_INCLUDE_MTE_REGS
876 el2_sysregs_context_restore_mte(el2_sysregs_ctx);
877#endif
878#if ENABLE_MPAM_FOR_LOWER_ELS
879 el2_sysregs_context_restore_mpam(el2_sysregs_ctx);
880#endif
881#if ENABLE_FEAT_FGT
882 el2_sysregs_context_restore_fgt(el2_sysregs_ctx);
883#endif
884#if ENABLE_FEAT_ECV
885 el2_sysregs_context_restore_ecv(el2_sysregs_ctx);
886#endif
887#if ENABLE_FEAT_VHE
888 el2_sysregs_context_restore_vhe(el2_sysregs_ctx);
889#endif
890#if RAS_EXTENSION
891 el2_sysregs_context_restore_ras(el2_sysregs_ctx);
892#endif
893#if CTX_INCLUDE_NEVE_REGS
894 el2_sysregs_context_restore_nv2(el2_sysregs_ctx);
895#endif
896#if ENABLE_TRF_FOR_NS
897 el2_sysregs_context_restore_trf(el2_sysregs_ctx);
898#endif
899#if ENABLE_FEAT_CSV2_2
900 el2_sysregs_context_restore_csv2(el2_sysregs_ctx);
901#endif
902#if ENABLE_FEAT_HCX
903 el2_sysregs_context_restore_hcx(el2_sysregs_ctx);
904#endif
Max Shvetsov28f39f02020-02-25 13:56:19 +0000905 }
906}
907#endif /* CTX_INCLUDE_EL2_REGS */
908
Andrew Thoelke167a9352014-06-04 21:10:52 +0100909/*******************************************************************************
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600910 * This function is used to exit to Non-secure world. If CTX_INCLUDE_EL2_REGS
911 * is enabled, it restores EL1 and EL2 sysreg contexts instead of directly
912 * updating EL1 and EL2 registers. Otherwise, it calls the generic
913 * cm_prepare_el3_exit function.
914 ******************************************************************************/
915void cm_prepare_el3_exit_ns(void)
916{
917#if CTX_INCLUDE_EL2_REGS
918 cpu_context_t *ctx = cm_get_context(NON_SECURE);
919 assert(ctx != NULL);
920
Zelalem Awekeb515f542022-04-08 16:48:05 -0500921 /* Assert that EL2 is used. */
922#if ENABLE_ASSERTIONS
923 el3_state_t *state = get_el3state_ctx(ctx);
924 u_register_t scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
925#endif
926 assert(((scr_el3 & SCR_HCE_BIT) != 0UL) &&
927 (el_implemented(2U) != EL_IMPL_NONE));
928
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600929 /*
930 * Currently some extensions are configured using
931 * direct register updates. Therefore, do this here
932 * instead of when setting up context.
933 */
934 manage_extensions_nonsecure(0, ctx);
935
936 /*
937 * Set the NS bit to be able to access the ICC_SRE_EL2
938 * register when restoring context.
939 */
940 write_scr_el3(read_scr_el3() | SCR_NS_BIT);
941
Olivier Deprez04825032022-05-09 17:34:02 +0200942 /*
943 * Ensure the NS bit change is committed before the EL2/EL1
944 * state restoration.
945 */
946 isb();
947
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600948 /* Restore EL2 and EL1 sysreg contexts */
949 cm_el2_sysregs_context_restore(NON_SECURE);
950 cm_el1_sysregs_context_restore(NON_SECURE);
951 cm_set_next_eret_context(NON_SECURE);
952#else
953 cm_prepare_el3_exit(NON_SECURE);
954#endif /* CTX_INCLUDE_EL2_REGS */
955}
956
957/*******************************************************************************
Soby Mathewfdfabec2014-07-04 16:02:26 +0100958 * The next four functions are used by runtime services to save and restore
959 * EL1 context on the 'cpu_context' structure for the specified security
Achin Gupta7aea9082014-02-01 07:51:28 +0000960 * state.
961 ******************************************************************************/
Achin Gupta7aea9082014-02-01 07:51:28 +0000962void cm_el1_sysregs_context_save(uint32_t security_state)
963{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100964 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000965
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100966 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000967 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +0000968
Max Shvetsov28259462020-02-17 16:15:47 +0000969 el1_sysregs_context_save(get_el1_sysregs_ctx(ctx));
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +0100970
971#if IMAGE_BL31
972 if (security_state == SECURE)
973 PUBLISH_EVENT(cm_exited_secure_world);
974 else
975 PUBLISH_EVENT(cm_exited_normal_world);
976#endif
Achin Gupta7aea9082014-02-01 07:51:28 +0000977}
978
979void cm_el1_sysregs_context_restore(uint32_t security_state)
980{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100981 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +0000982
Andrew Thoelke08ab89d2014-05-14 17:09:32 +0100983 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000984 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +0000985
Max Shvetsov28259462020-02-17 16:15:47 +0000986 el1_sysregs_context_restore(get_el1_sysregs_ctx(ctx));
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +0100987
988#if IMAGE_BL31
989 if (security_state == SECURE)
990 PUBLISH_EVENT(cm_entering_secure_world);
991 else
992 PUBLISH_EVENT(cm_entering_normal_world);
993#endif
Achin Gupta7aea9082014-02-01 07:51:28 +0000994}
995
996/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +0100997 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
998 * given security state with the given entrypoint
Achin Gupta607084e2014-02-09 18:24:19 +0000999 ******************************************************************************/
Soby Mathew4c0d0392016-06-16 14:52:04 +01001000void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
Achin Gupta607084e2014-02-09 18:24:19 +00001001{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001002 cpu_context_t *ctx;
1003 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +00001004
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001005 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001006 assert(ctx != NULL);
Achin Gupta607084e2014-02-09 18:24:19 +00001007
1008 /* Populate EL3 state so that ERET jumps to the correct entry */
1009 state = get_el3state_ctx(ctx);
1010 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
1011}
1012
1013/*******************************************************************************
Andrew Thoelke167a9352014-06-04 21:10:52 +01001014 * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
1015 * pertaining to the given security state
1016 ******************************************************************************/
1017void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathew4c0d0392016-06-16 14:52:04 +01001018 uintptr_t entrypoint, uint32_t spsr)
Andrew Thoelke167a9352014-06-04 21:10:52 +01001019{
1020 cpu_context_t *ctx;
1021 el3_state_t *state;
1022
1023 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001024 assert(ctx != NULL);
Andrew Thoelke167a9352014-06-04 21:10:52 +01001025
1026 /* Populate EL3 state so that ERET jumps to the correct entry */
1027 state = get_el3state_ctx(ctx);
1028 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
1029 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
1030}
1031
1032/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +01001033 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
1034 * pertaining to the given security state using the value and bit position
1035 * specified in the parameters. It preserves all other bits.
1036 ******************************************************************************/
1037void cm_write_scr_el3_bit(uint32_t security_state,
1038 uint32_t bit_pos,
1039 uint32_t value)
1040{
1041 cpu_context_t *ctx;
1042 el3_state_t *state;
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001043 u_register_t scr_el3;
Achin Guptac429b5e2014-05-04 18:38:28 +01001044
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001045 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001046 assert(ctx != NULL);
Achin Guptac429b5e2014-05-04 18:38:28 +01001047
1048 /* Ensure that the bit position is a valid one */
Jimmy Brissond7b5f402020-08-04 16:18:52 -05001049 assert(((1UL << bit_pos) & SCR_VALID_BIT_MASK) != 0U);
Achin Guptac429b5e2014-05-04 18:38:28 +01001050
1051 /* Ensure that the 'value' is only a bit wide */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001052 assert(value <= 1U);
Achin Guptac429b5e2014-05-04 18:38:28 +01001053
1054 /*
1055 * Get the SCR_EL3 value from the cpu context, clear the desired bit
1056 * and set it to its new value.
1057 */
1058 state = get_el3state_ctx(ctx);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001059 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
Jimmy Brissond7b5f402020-08-04 16:18:52 -05001060 scr_el3 &= ~(1UL << bit_pos);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001061 scr_el3 |= (u_register_t)value << bit_pos;
Achin Guptac429b5e2014-05-04 18:38:28 +01001062 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
1063}
1064
1065/*******************************************************************************
1066 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
1067 * given security state.
1068 ******************************************************************************/
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001069u_register_t cm_get_scr_el3(uint32_t security_state)
Achin Guptac429b5e2014-05-04 18:38:28 +01001070{
1071 cpu_context_t *ctx;
1072 el3_state_t *state;
1073
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001074 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001075 assert(ctx != NULL);
Achin Guptac429b5e2014-05-04 18:38:28 +01001076
1077 /* Populate EL3 state so that ERET jumps to the correct entry */
1078 state = get_el3state_ctx(ctx);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001079 return read_ctx_reg(state, CTX_SCR_EL3);
Achin Guptac429b5e2014-05-04 18:38:28 +01001080}
1081
1082/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001083 * This function is used to program the context that's used for exception
1084 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
1085 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +00001086 ******************************************************************************/
1087void cm_set_next_eret_context(uint32_t security_state)
1088{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001089 cpu_context_t *ctx;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001090
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001091 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001092 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001093
Andrew Thoelke167a9352014-06-04 21:10:52 +01001094 cm_set_next_context(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +00001095}