blob: 70e0ec0891e067b4c75720289de67cd9b9a0a941 [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Maksims Svecovs01cf14d2023-02-02 16:10:22 +00002 * Copyright (c) 2013-2023, 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>
Boyan Karatotevc73686a2023-02-15 13:21:50 +000027#include <lib/extensions/pmuv3.h>
johpow01dc78e622021-07-08 14:14:00 -050028#include <lib/extensions/sme.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000029#include <lib/extensions/spe.h>
30#include <lib/extensions/sve.h>
Manish V Badarkhed4582d32021-06-29 11:44:20 +010031#include <lib/extensions/sys_reg_trace.h>
Manish V Badarkhe813524e2021-07-02 09:10:56 +010032#include <lib/extensions/trbe.h>
Manish V Badarkhe8fcd3d92021-07-08 09:33:18 +010033#include <lib/extensions/trf.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000034#include <lib/utils.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000035
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +010036#if ENABLE_FEAT_TWED
37/* Make sure delay value fits within the range(0-15) */
38CASSERT(((TWED_DELAY & ~SCR_TWEDEL_MASK) == 0U), assert_twed_delay_value_check);
39#endif /* ENABLE_FEAT_TWED */
Achin Gupta7aea9082014-02-01 07:51:28 +000040
Boyan Karatotev24a70732023-03-08 11:56:49 +000041static void manage_extensions_nonsecure(cpu_context_t *ctx);
Jayanth Dodderi Chidanand781d07a2022-03-28 15:28:55 +010042static void manage_extensions_secure(cpu_context_t *ctx);
Zelalem Awekeb515f542022-04-08 16:48:05 -050043
44static void setup_el1_context(cpu_context_t *ctx, const struct entry_point_info *ep)
45{
46 u_register_t sctlr_elx, actlr_elx;
47
48 /*
49 * Initialise SCTLR_EL1 to the reset value corresponding to the target
50 * execution state setting all fields rather than relying on the hw.
51 * Some fields have architecturally UNKNOWN reset values and these are
52 * set to zero.
53 *
54 * SCTLR.EE: Endianness is taken from the entrypoint attributes.
55 *
56 * SCTLR.M, SCTLR.C and SCTLR.I: These fields must be zero (as
57 * required by PSCI specification)
58 */
59 sctlr_elx = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
60 if (GET_RW(ep->spsr) == MODE_RW_64) {
61 sctlr_elx |= SCTLR_EL1_RES1;
62 } else {
63 /*
64 * If the target execution state is AArch32 then the following
65 * fields need to be set.
66 *
67 * SCTRL_EL1.nTWE: Set to one so that EL0 execution of WFE
68 * instructions are not trapped to EL1.
69 *
70 * SCTLR_EL1.nTWI: Set to one so that EL0 execution of WFI
71 * instructions are not trapped to EL1.
72 *
73 * SCTLR_EL1.CP15BEN: Set to one to enable EL0 execution of the
74 * CP15DMB, CP15DSB, and CP15ISB instructions.
75 */
76 sctlr_elx |= SCTLR_AARCH32_EL1_RES1 | SCTLR_CP15BEN_BIT
77 | SCTLR_NTWI_BIT | SCTLR_NTWE_BIT;
78 }
79
80#if ERRATA_A75_764081
81 /*
82 * If workaround of errata 764081 for Cortex-A75 is used then set
83 * SCTLR_EL1.IESB to enable Implicit Error Synchronization Barrier.
84 */
85 sctlr_elx |= SCTLR_IESB_BIT;
86#endif
87 /* Store the initialised SCTLR_EL1 value in the cpu_context */
88 write_ctx_reg(get_el1_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
89
90 /*
91 * Base the context ACTLR_EL1 on the current value, as it is
92 * implementation defined. The context restore process will write
93 * the value from the context to the actual register and can cause
94 * problems for processor cores that don't expect certain bits to
95 * be zero.
96 */
97 actlr_elx = read_actlr_el1();
98 write_ctx_reg((get_el1_sysregs_ctx(ctx)), (CTX_ACTLR_EL1), (actlr_elx));
99}
100
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600101/******************************************************************************
102 * This function performs initializations that are specific to SECURE state
103 * and updates the cpu context specified by 'ctx'.
104 *****************************************************************************/
105static void setup_secure_context(cpu_context_t *ctx, const struct entry_point_info *ep)
Achin Gupta7aea9082014-02-01 07:51:28 +0000106{
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600107 u_register_t scr_el3;
108 el3_state_t *state;
109
110 state = get_el3state_ctx(ctx);
111 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
112
113#if defined(IMAGE_BL31) && !defined(SPD_spmd)
Achin Gupta7aea9082014-02-01 07:51:28 +0000114 /*
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600115 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
116 * indicated by the interrupt routing model for BL31.
Achin Gupta7aea9082014-02-01 07:51:28 +0000117 */
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600118 scr_el3 |= get_scr_el3_from_routing_model(SECURE);
119#endif
120
121#if !CTX_INCLUDE_MTE_REGS || ENABLE_ASSERTIONS
122 /* Get Memory Tagging Extension support level */
123 unsigned int mte = get_armv8_5_mte_support();
124#endif
125 /*
126 * Allow access to Allocation Tags when CTX_INCLUDE_MTE_REGS
127 * is set, or when MTE is only implemented at EL0.
128 */
129#if CTX_INCLUDE_MTE_REGS
130 assert((mte == MTE_IMPLEMENTED_ELX) || (mte == MTE_IMPLEMENTED_ASY));
131 scr_el3 |= SCR_ATA_BIT;
132#else
133 if (mte == MTE_IMPLEMENTED_EL0) {
134 scr_el3 |= SCR_ATA_BIT;
135 }
136#endif /* CTX_INCLUDE_MTE_REGS */
137
138 /* Enable S-EL2 if the next EL is EL2 and S-EL2 is present */
Andre Przywara623f6142023-02-22 16:53:50 +0000139 if ((GET_EL(ep->spsr) == MODE_EL2) && is_feat_sel2_supported()) {
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600140 if (GET_RW(ep->spsr) != MODE_RW_64) {
141 ERROR("S-EL2 can not be used in AArch32\n.");
142 panic();
143 }
144
145 scr_el3 |= SCR_EEL2_BIT;
146 }
147
148 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
149
Zelalem Awekeb515f542022-04-08 16:48:05 -0500150 /*
151 * Initialize EL1 context registers unless SPMC is running
152 * at S-EL2.
153 */
154#if !SPMD_SPM_AT_SEL2
155 setup_el1_context(ctx, ep);
156#endif
157
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600158 manage_extensions_secure(ctx);
159}
160
161#if ENABLE_RME
162/******************************************************************************
163 * This function performs initializations that are specific to REALM state
164 * and updates the cpu context specified by 'ctx'.
165 *****************************************************************************/
166static void setup_realm_context(cpu_context_t *ctx, const struct entry_point_info *ep)
167{
168 u_register_t scr_el3;
169 el3_state_t *state;
170
171 state = get_el3state_ctx(ctx);
172 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
173
Maksims Svecovs01cf14d2023-02-02 16:10:22 +0000174 scr_el3 |= SCR_NS_BIT | SCR_NSE_BIT;
175
Andre Przywara7db710f2022-11-17 17:30:43 +0000176 if (is_feat_csv2_2_supported()) {
177 /* Enable access to the SCXTNUM_ELx registers. */
178 scr_el3 |= SCR_EnSCXT_BIT;
179 }
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600180
181 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
182}
183#endif /* ENABLE_RME */
184
185/******************************************************************************
186 * This function performs initializations that are specific to NON-SECURE state
187 * and updates the cpu context specified by 'ctx'.
188 *****************************************************************************/
189static void setup_ns_context(cpu_context_t *ctx, const struct entry_point_info *ep)
190{
191 u_register_t scr_el3;
192 el3_state_t *state;
193
194 state = get_el3state_ctx(ctx);
195 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
196
197 /* SCR_NS: Set the NS bit */
198 scr_el3 |= SCR_NS_BIT;
199
200#if !CTX_INCLUDE_PAUTH_REGS
201 /*
202 * If the pointer authentication registers aren't saved during world
203 * switches the value of the registers can be leaked from the Secure to
204 * the Non-secure world. To prevent this, rather than enabling pointer
205 * authentication everywhere, we only enable it in the Non-secure world.
206 *
207 * If the Secure world wants to use pointer authentication,
208 * CTX_INCLUDE_PAUTH_REGS must be set to 1.
209 */
210 scr_el3 |= SCR_API_BIT | SCR_APK_BIT;
211#endif /* !CTX_INCLUDE_PAUTH_REGS */
212
213 /* Allow access to Allocation Tags when MTE is implemented. */
214 scr_el3 |= SCR_ATA_BIT;
215
Manish Pandey46cc41d2022-10-10 11:43:08 +0100216#if HANDLE_EA_EL3_FIRST_NS
217 /* SCR_EL3.EA: Route External Abort and SError Interrupt to EL3. */
218 scr_el3 |= SCR_EA_BIT;
219#endif
220
Manish Pandey00e8f792022-09-27 14:30:34 +0100221#if RAS_TRAP_NS_ERR_REC_ACCESS
222 /*
223 * SCR_EL3.TERR: Trap Error record accesses. Accesses to the RAS ERR
224 * and RAS ERX registers from EL1 and EL2(from any security state)
225 * are trapped to EL3.
226 * Set here to trap only for NS EL1/EL2
227 *
228 */
229 scr_el3 |= SCR_TERR_BIT;
230#endif
231
Andre Przywara7db710f2022-11-17 17:30:43 +0000232 if (is_feat_csv2_2_supported()) {
233 /* Enable access to the SCXTNUM_ELx registers. */
234 scr_el3 |= SCR_EnSCXT_BIT;
235 }
Maksims Svecovs01cf14d2023-02-02 16:10:22 +0000236
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600237#ifdef IMAGE_BL31
238 /*
239 * SCR_EL3.IRQ, SCR_EL3.FIQ: Enable the physical FIQ and IRQ routing as
240 * indicated by the interrupt routing model for BL31.
241 */
242 scr_el3 |= get_scr_el3_from_routing_model(NON_SECURE);
243#endif
244 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600245
Zelalem Awekeb515f542022-04-08 16:48:05 -0500246 /* Initialize EL1 context registers */
247 setup_el1_context(ctx, ep);
248
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600249 /* Initialize EL2 context registers */
250#if CTX_INCLUDE_EL2_REGS
251
252 /*
253 * Initialize SCTLR_EL2 context register using Endianness value
254 * taken from the entrypoint attribute.
255 */
256 u_register_t sctlr_el2 = (EP_GET_EE(ep->h.attr) != 0U) ? SCTLR_EE_BIT : 0UL;
257 sctlr_el2 |= SCTLR_EL2_RES1;
258 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_SCTLR_EL2,
259 sctlr_el2);
260
261 /*
Varun Wadekar2b287272022-09-13 12:38:47 +0100262 * Program the ICC_SRE_EL2 to make sure the correct bits are set
263 * when restoring NS context.
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600264 */
Varun Wadekar2b287272022-09-13 12:38:47 +0100265 u_register_t icc_sre_el2 = ICC_SRE_DIB_BIT | ICC_SRE_DFB_BIT |
266 ICC_SRE_EN_BIT | ICC_SRE_SRE_BIT;
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600267 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_ICC_SRE_EL2,
268 icc_sre_el2);
Boyan Karatotev7f856192022-10-26 15:10:39 +0100269
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600270 if (is_feat_hcx_supported()) {
271 /*
272 * Initialize register HCRX_EL2 with its init value.
273 * As the value of HCRX_EL2 is UNKNOWN on reset, there is a
274 * chance that this can lead to unexpected behavior in lower
275 * ELs that have not been updated since the introduction of
276 * this feature if not properly initialized, especially when
277 * it comes to those bits that enable/disable traps.
278 */
279 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HCRX_EL2,
280 HCRX_EL2_INIT_VAL);
281 }
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500282
283 if (is_feat_fgt_supported()) {
284 /*
285 * Initialize HFG*_EL2 registers with a default value so legacy
286 * systems unaware of FEAT_FGT do not get trapped due to their lack
287 * of initialization for this feature.
288 */
289 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGITR_EL2,
290 HFGITR_EL2_INIT_VAL);
291 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGRTR_EL2,
292 HFGRTR_EL2_INIT_VAL);
293 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_HFGWTR_EL2,
294 HFGWTR_EL2_INIT_VAL);
295 }
Zelalem Aweke8b95e842022-01-31 16:59:42 -0600296#endif /* CTX_INCLUDE_EL2_REGS */
Boyan Karatotev24a70732023-03-08 11:56:49 +0000297
298 manage_extensions_nonsecure(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +0000299}
300
301/*******************************************************************************
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600302 * The following function performs initialization of the cpu_context 'ctx'
303 * for first use that is common to all security states, and sets the
304 * initial entrypoint state as specified by the entry_point_info structure.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100305 *
Paul Beesley8aabea32019-01-11 18:26:51 +0000306 * The EE and ST attributes are used to configure the endianness and secure
Soby Mathew12d0d002015-04-09 13:40:55 +0100307 * timer availability for the new execution context.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100308 ******************************************************************************/
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600309static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
Andrew Thoelke167a9352014-06-04 21:10:52 +0100310{
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000311 u_register_t scr_el3;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100312 el3_state_t *state;
313 gp_regs_t *gp_regs;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100314
Andrew Thoelke167a9352014-06-04 21:10:52 +0100315 /* Clear any residual register values from the context */
Douglas Raillard32f0d3c2017-01-26 15:54:44 +0000316 zeromem(ctx, sizeof(*ctx));
Andrew Thoelke167a9352014-06-04 21:10:52 +0100317
318 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100319 * SCR_EL3 was initialised during reset sequence in macro
320 * el3_arch_init_common. This code modifies the SCR_EL3 fields that
321 * affect the next EL.
322 *
323 * The following fields are initially set to zero and then updated to
324 * the required value depending on the state of the SPSR_EL3 and the
325 * Security state and entrypoint attributes of the next EL.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100326 */
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000327 scr_el3 = read_scr();
Manish Pandey46cc41d2022-10-10 11:43:08 +0100328 scr_el3 &= ~(SCR_NS_BIT | SCR_RW_BIT | SCR_EA_BIT | SCR_FIQ_BIT | SCR_IRQ_BIT |
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600329 SCR_ST_BIT | SCR_HCE_BIT | SCR_NSE_BIT);
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500330
David Cunado18f2efd2017-04-13 22:38:29 +0100331 /*
332 * SCR_EL3.RW: Set the execution state, AArch32 or AArch64, for next
333 * Exception level as specified by SPSR.
334 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500335 if (GET_RW(ep->spsr) == MODE_RW_64) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100336 scr_el3 |= SCR_RW_BIT;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500337 }
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600338
David Cunado18f2efd2017-04-13 22:38:29 +0100339 /*
340 * SCR_EL3.ST: Traps Secure EL1 accesses to the Counter-timer Physical
Zelalem Awekeb515f542022-04-08 16:48:05 -0500341 * Secure timer registers to EL3, from AArch64 state only, if specified
342 * by the entrypoint attributes. If SEL2 is present and enabled, the ST
343 * bit always behaves as 1 (i.e. secure physical timer register access
344 * is not trapped)
David Cunado18f2efd2017-04-13 22:38:29 +0100345 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500346 if (EP_GET_ST(ep->h.attr) != 0U) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100347 scr_el3 |= SCR_ST_BIT;
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500348 }
Andrew Thoelke167a9352014-06-04 21:10:52 +0100349
johpow01cb4ec472021-08-04 19:38:18 -0500350 /*
351 * If FEAT_HCX is enabled, enable access to HCRX_EL2 by setting
352 * SCR_EL3.HXEn.
353 */
Andre Przywarac5a3ebb2022-11-15 11:45:19 +0000354 if (is_feat_hcx_supported()) {
355 scr_el3 |= SCR_HXEn_BIT;
356 }
johpow01cb4ec472021-08-04 19:38:18 -0500357
Juan Pablo Condeff86e0b2022-07-12 16:40:29 -0400358 /*
359 * If FEAT_RNG_TRAP is enabled, all reads of the RNDR and RNDRRS
360 * registers are trapped to EL3.
361 */
362#if ENABLE_FEAT_RNG_TRAP
363 scr_el3 |= SCR_TRNDR_BIT;
364#endif
365
Jeenu Viswambharan1a7c1cf2017-12-08 12:13:51 +0000366#if FAULT_INJECTION_SUPPORT
367 /* Enable fault injection from lower ELs */
368 scr_el3 |= SCR_FIEN_BIT;
369#endif
370
Antonio Nino Diaz52839622019-01-31 11:58:00 +0000371 /*
Mark Brownd3331602023-03-14 20:13:03 +0000372 * SCR_EL3.TCR2EN: Enable access to TCR2_ELx for AArch64 if present.
373 */
374 if (is_feat_tcr2_supported() && (GET_RW(ep->spsr) == MODE_RW_64)) {
375 scr_el3 |= SCR_TCR2EN_BIT;
376 }
377
378 /*
Mark Brown062b6c62023-03-14 20:48:43 +0000379 * SCR_EL3.PIEN: Enable permission indirection and overlay
380 * registers for AArch64 if present.
381 */
382 if (is_feat_sxpie_supported() || is_feat_sxpoe_supported()) {
383 scr_el3 |= SCR_PIEN_BIT;
384 }
385
386 /*
Mark Brown688ab572023-03-14 21:33:04 +0000387 * SCR_EL3.GCSEn: Enable GCS registers for AArch64 if present.
388 */
389 if ((is_feat_gcs_supported()) && (GET_RW(ep->spsr) == MODE_RW_64)) {
390 scr_el3 |= SCR_GCSEn_BIT;
391 }
392
393 /*
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600394 * CPTR_EL3 was initialized out of reset, copy that value to the
395 * context register.
Antonio Nino Diaz52839622019-01-31 11:58:00 +0000396 */
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100397 write_ctx_reg(get_el3state_ctx(ctx), CTX_CPTR_EL3, read_cptr_el3());
Andrew Thoelke167a9352014-06-04 21:10:52 +0100398
399 /*
David Cunado18f2efd2017-04-13 22:38:29 +0100400 * SCR_EL3.HCE: Enable HVC instructions if next execution state is
401 * AArch64 and next EL is EL2, or if next execution state is AArch32 and
402 * next mode is Hyp.
Jimmy Brisson110ee432020-04-16 10:47:56 -0500403 * SCR_EL3.FGTEn: Enable Fine Grained Virtualization Traps under the
404 * same conditions as HVC instructions and when the processor supports
405 * ARMv8.6-FGT.
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500406 * SCR_EL3.ECVEn: Enable Enhanced Counter Virtualization (ECV)
407 * CNTPOFF_EL2 register under the same conditions as HVC instructions
408 * and when the processor supports ECV.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100409 */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000410 if (((GET_RW(ep->spsr) == MODE_RW_64) && (GET_EL(ep->spsr) == MODE_EL2))
411 || ((GET_RW(ep->spsr) != MODE_RW_64)
412 && (GET_M32(ep->spsr) == MODE32_hyp))) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100413 scr_el3 |= SCR_HCE_BIT;
Jimmy Brisson110ee432020-04-16 10:47:56 -0500414
Andre Przywarace485952022-11-10 14:28:01 +0000415 if (is_feat_fgt_supported()) {
Jimmy Brisson110ee432020-04-16 10:47:56 -0500416 scr_el3 |= SCR_FGTEN_BIT;
417 }
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500418
Andre Przywarab8f03d22022-11-17 17:30:43 +0000419 if (is_feat_ecv_supported()) {
Jimmy Brisson29d0ee52020-04-16 10:48:02 -0500420 scr_el3 |= SCR_ECVEN_BIT;
421 }
Andrew Thoelke167a9352014-06-04 21:10:52 +0100422 }
423
johpow016cac7242020-04-22 14:05:13 -0500424 /* Enable WFE trap delay in SCR_EL3 if supported and configured */
Andre Przywara1223d2a2023-01-27 12:25:49 +0000425 if (is_feat_twed_supported()) {
426 /* Set delay in SCR_EL3 */
427 scr_el3 &= ~(SCR_TWEDEL_MASK << SCR_TWEDEL_SHIFT);
428 scr_el3 |= ((TWED_DELAY & SCR_TWEDEL_MASK)
429 << SCR_TWEDEL_SHIFT);
johpow016cac7242020-04-22 14:05:13 -0500430
Andre Przywara1223d2a2023-01-27 12:25:49 +0000431 /* Enable WFE delay */
432 scr_el3 |= SCR_TWEDEn_BIT;
433 }
johpow016cac7242020-04-22 14:05:13 -0500434
David Cunado18f2efd2017-04-13 22:38:29 +0100435 /*
Alexei Fedorove290a8f2019-08-13 15:17:53 +0100436 * Populate EL3 state so that we've the right context
437 * before doing ERET
438 */
Andrew Thoelke167a9352014-06-04 21:10:52 +0100439 state = get_el3state_ctx(ctx);
440 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
441 write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
442 write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
443
444 /*
445 * Store the X0-X7 value from the entrypoint into the context
446 * Use memcpy as we are in control of the layout of the structures
447 */
448 gp_regs = get_gpregs_ctx(ctx);
449 memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
450}
451
452/*******************************************************************************
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600453 * Context management library initialization routine. This library is used by
454 * runtime services to share pointers to 'cpu_context' structures for secure
455 * non-secure and realm states. Management of the structures and their associated
456 * memory is not done by the context management library e.g. the PSCI service
457 * manages the cpu context used for entry from and exit to the non-secure state.
458 * The Secure payload dispatcher service manages the context(s) corresponding to
459 * the secure state. It also uses this library to get access to the non-secure
460 * state cpu context pointers.
461 * Lastly, this library provides the API to make SP_EL3 point to the cpu context
462 * which will be used for programming an entry into a lower EL. The same context
463 * will be used to save state upon exception entry from that EL.
464 ******************************************************************************/
465void __init cm_init(void)
466{
467 /*
Elyes Haouas1b491ee2023-02-13 09:14:48 +0100468 * The context management library has only global data to initialize, but
Zelalem Aweke2bbad1d2022-01-05 17:12:24 -0600469 * that will be done when the BSS is zeroed out.
470 */
471}
472
473/*******************************************************************************
474 * This is the high-level function used to initialize the cpu_context 'ctx' for
475 * first use. It performs initializations that are common to all security states
476 * and initializations specific to the security state specified in 'ep'
477 ******************************************************************************/
478void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
479{
480 unsigned int security_state;
481
482 assert(ctx != NULL);
483
484 /*
485 * Perform initializations that are common
486 * to all security states
487 */
488 setup_context_common(ctx, ep);
489
490 security_state = GET_SECURITY_STATE(ep->h.attr);
491
492 /* Perform security state specific initializations */
493 switch (security_state) {
494 case SECURE:
495 setup_secure_context(ctx, ep);
496 break;
497#if ENABLE_RME
498 case REALM:
499 setup_realm_context(ctx, ep);
500 break;
501#endif
502 case NON_SECURE:
503 setup_ns_context(ctx, ep);
504 break;
505 default:
506 ERROR("Invalid security state\n");
507 panic();
508 break;
509 }
510}
511
512/*******************************************************************************
Boyan Karatotev24a70732023-03-08 11:56:49 +0000513 * Enable architecture extensions for EL3 execution. This function only updates
514 * registers in-place which are expected to either never change or be
515 * overwritten by el3_exit.
516 ******************************************************************************/
517#if IMAGE_BL31
518void cm_manage_extensions_el3(void)
519{
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000520 if (is_feat_spe_supported()) {
521 spe_init_el3();
522 }
523
Boyan Karatotev4085a022023-03-27 17:02:43 +0100524 if (is_feat_amu_supported()) {
525 amu_init_el3();
526 }
527
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000528 if (is_feat_sme_supported()) {
529 sme_init_el3();
530 }
531
532 if (is_feat_mpam_supported()) {
533 mpam_init_el3();
534 }
535
536 if (is_feat_trbe_supported()) {
537 trbe_init_el3();
538 }
539
540 if (is_feat_brbe_supported()) {
541 brbe_init_el3();
542 }
543
544 if (is_feat_trf_supported()) {
545 trf_init_el3();
546 }
547
548 pmuv3_init_el3();
Boyan Karatotev24a70732023-03-08 11:56:49 +0000549}
550#endif /* IMAGE_BL31 */
551
552/*******************************************************************************
553 * Enable architecture extensions on first entry to Non-secure world.
554 ******************************************************************************/
555static void manage_extensions_nonsecure(cpu_context_t *ctx)
556{
557#if IMAGE_BL31
Boyan Karatotev4085a022023-03-27 17:02:43 +0100558 if (is_feat_amu_supported()) {
559 amu_enable(ctx);
560 }
561
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000562 /* Enable SVE and FPU/SIMD */
563 if (is_feat_sve_supported()) {
564 sve_enable(ctx);
565 }
566
567 if (is_feat_sme_supported()) {
568 sme_enable(ctx);
569 }
570
571 if (is_feat_sys_reg_trace_supported()) {
572 sys_reg_trace_enable(ctx);
573 }
574
Boyan Karatotevc73686a2023-02-15 13:21:50 +0000575 pmuv3_enable(ctx);
Boyan Karatotev24a70732023-03-08 11:56:49 +0000576#endif /* IMAGE_BL31 */
577}
578
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000579/* TODO: move to lib/extensions/pauth when it has been ported to FEAT_STATE */
580static __unused void enable_pauth_el2(void)
581{
582 u_register_t hcr_el2 = read_hcr_el2();
583 /*
584 * For Armv8.3 pointer authentication feature, disable traps to EL2 when
585 * accessing key registers or using pointer authentication instructions
586 * from lower ELs.
587 */
588 hcr_el2 |= (HCR_API_BIT | HCR_APK_BIT);
589
590 write_hcr_el2(hcr_el2);
591}
592
Boyan Karatotev24a70732023-03-08 11:56:49 +0000593/*******************************************************************************
594 * Enable architecture extensions in-place at EL2 on first entry to Non-secure
595 * world when EL2 is empty and unused.
596 ******************************************************************************/
597static void manage_extensions_nonsecure_el2_unused(void)
598{
599#if IMAGE_BL31
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000600 if (is_feat_spe_supported()) {
601 spe_init_el2_unused();
602 }
603
Boyan Karatotev4085a022023-03-27 17:02:43 +0100604 if (is_feat_amu_supported()) {
605 amu_init_el2_unused();
606 }
607
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000608 if (is_feat_mpam_supported()) {
609 mpam_init_el2_unused();
610 }
611
612 if (is_feat_trbe_supported()) {
613 trbe_init_el2_unused();
614 }
615
616 if (is_feat_sys_reg_trace_supported()) {
617 sys_reg_trace_init_el2_unused();
618 }
619
620 if (is_feat_trf_supported()) {
621 trf_init_el2_unused();
622 }
623
Boyan Karatotevc73686a2023-02-15 13:21:50 +0000624 pmuv3_init_el2_unused();
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000625
626 if (is_feat_sve_supported()) {
627 sve_init_el2_unused();
628 }
629
630 if (is_feat_sme_supported()) {
631 sme_init_el2_unused();
632 }
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000633
634#if ENABLE_PAUTH
635 enable_pauth_el2();
636#endif /* ENABLE_PAUTH */
Boyan Karatotev24a70732023-03-08 11:56:49 +0000637#endif /* IMAGE_BL31 */
638}
639
640/*******************************************************************************
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100641 * Enable architecture extensions on first entry to Secure world.
642 ******************************************************************************/
johpow01dc78e622021-07-08 14:14:00 -0500643static void manage_extensions_secure(cpu_context_t *ctx)
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100644{
645#if IMAGE_BL31
Boyan Karatotev0d122942023-03-08 16:29:26 +0000646 if (is_feat_sve_supported()) {
Jayanth Dodderi Chidanand2b0bc4e2023-03-07 10:43:19 +0000647 if (ENABLE_SVE_FOR_SWD) {
648 /*
649 * Enable SVE and FPU in secure context, secure manager must
650 * ensure that the SVE and FPU register contexts are properly
651 * managed.
652 */
653 sve_enable(ctx);
654 } else {
655 /*
656 * Disable SVE and FPU in secure context so non-secure world
657 * can safely use them.
658 */
659 sve_disable(ctx);
660 }
661 }
662
Boyan Karatotev0d122942023-03-08 16:29:26 +0000663 if (is_feat_sme_supported()) {
664 if (ENABLE_SME_FOR_SWD) {
665 /*
666 * Enable SME, SVE, FPU/SIMD in secure context, secure manager
667 * must ensure SME, SVE, and FPU/SIMD context properly managed.
668 */
Boyan Karatotev60d330d2023-02-16 15:12:45 +0000669 sme_init_el3();
Boyan Karatotev0d122942023-03-08 16:29:26 +0000670 sme_enable(ctx);
671 } else {
672 /*
673 * Disable SME, SVE, FPU/SIMD in secure context so non-secure
674 * world can safely use the associated registers.
675 */
676 sme_disable(ctx);
677 }
678 }
Boyan Karatotevece8f7d2023-02-13 16:32:47 +0000679
680 /* NS can access this but Secure shouldn't */
681 if (is_feat_sys_reg_trace_supported()) {
682 sys_reg_trace_disable(ctx);
683 }
johpow01dc78e622021-07-08 14:14:00 -0500684#endif /* IMAGE_BL31 */
Arunachalam Ganapathy68ac5ed2021-07-08 09:35:57 +0100685}
686
687/*******************************************************************************
Soby Mathew12d0d002015-04-09 13:40:55 +0100688 * The following function initializes the cpu_context for a CPU specified by
689 * its `cpu_idx` for first use, and sets the initial entrypoint state as
690 * specified by the entry_point_info structure.
691 ******************************************************************************/
692void cm_init_context_by_index(unsigned int cpu_idx,
693 const entry_point_info_t *ep)
694{
695 cpu_context_t *ctx;
696 ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +0100697 cm_setup_context(ctx, ep);
Soby Mathew12d0d002015-04-09 13:40:55 +0100698}
699
700/*******************************************************************************
701 * The following function initializes the cpu_context for the current CPU
702 * for first use, and sets the initial entrypoint state as specified by the
703 * entry_point_info structure.
704 ******************************************************************************/
705void cm_init_my_context(const entry_point_info_t *ep)
706{
707 cpu_context_t *ctx;
708 ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz1634cae2018-05-22 10:09:10 +0100709 cm_setup_context(ctx, ep);
Soby Mathew12d0d002015-04-09 13:40:55 +0100710}
711
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000712/* EL2 present but unused, need to disable safely. SCTLR_EL2 can be ignored */
713static __unused void init_nonsecure_el2_unused(cpu_context_t *ctx)
714{
715 u_register_t hcr_el2 = HCR_RESET_VAL;
716 u_register_t mdcr_el2;
717 u_register_t scr_el3;
718
719 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
720
721 /* Set EL2 register width: Set HCR_EL2.RW to match SCR_EL3.RW */
722 if ((scr_el3 & SCR_RW_BIT) != 0U) {
723 hcr_el2 |= HCR_RW_BIT;
724 }
725
726 write_hcr_el2(hcr_el2);
727
728 /*
729 * Initialise CPTR_EL2 setting all fields rather than relying on the hw.
730 * All fields have architecturally UNKNOWN reset values.
731 */
732 write_cptr_el2(CPTR_EL2_RESET_VAL);
733
734 /*
735 * Initialise CNTHCTL_EL2. All fields are architecturally UNKNOWN on
736 * reset and are set to zero except for field(s) listed below.
737 *
738 * CNTHCTL_EL2.EL1PTEN: Set to one to disable traps to Hyp mode of
739 * Non-secure EL0 and EL1 accesses to the physical timer registers.
740 *
741 * CNTHCTL_EL2.EL1PCTEN: Set to one to disable traps to Hyp mode of
742 * Non-secure EL0 and EL1 accesses to the physical counter registers.
743 */
744 write_cnthctl_el2(CNTHCTL_RESET_VAL | EL1PCEN_BIT | EL1PCTEN_BIT);
745
746 /*
747 * Initialise CNTVOFF_EL2 to zero as it resets to an architecturally
748 * UNKNOWN value.
749 */
750 write_cntvoff_el2(0);
751
752 /*
753 * Set VPIDR_EL2 and VMPIDR_EL2 to match MIDR_EL1 and MPIDR_EL1
754 * respectively.
755 */
756 write_vpidr_el2(read_midr_el1());
757 write_vmpidr_el2(read_mpidr_el1());
758
759 /*
760 * Initialise VTTBR_EL2. All fields are architecturally UNKNOWN on reset.
761 *
762 * VTTBR_EL2.VMID: Set to zero. Even though EL1&0 stage 2 address
763 * translation is disabled, cache maintenance operations depend on the
764 * VMID.
765 *
766 * VTTBR_EL2.BADDR: Set to zero as EL1&0 stage 2 address translation is
767 * disabled.
768 */
769 write_vttbr_el2(VTTBR_RESET_VAL &
770 ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT) |
771 (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
772
773 /*
774 * Initialise MDCR_EL2, setting all fields rather than relying on hw.
775 * Some fields are architecturally UNKNOWN on reset.
776 *
777 * MDCR_EL2.TDRA: Set to zero so that Non-secure EL0 and EL1 System
778 * register accesses to the Debug ROM registers are not trapped to EL2.
779 *
780 * MDCR_EL2.TDOSA: Set to zero so that Non-secure EL1 System register
781 * accesses to the powerdown debug registers are not trapped to EL2.
782 *
783 * MDCR_EL2.TDA: Set to zero so that System register accesses to the
784 * debug registers do not trap to EL2.
785 *
786 * MDCR_EL2.TDE: Set to zero so that debug exceptions are not routed to
787 * EL2.
788 */
789 mdcr_el2 = MDCR_EL2_RESET_VAL &
790 ~(MDCR_EL2_TDRA_BIT | MDCR_EL2_TDOSA_BIT | MDCR_EL2_TDA_BIT |
791 MDCR_EL2_TDE_BIT);
792
793 write_mdcr_el2(mdcr_el2);
794
795 /*
796 * Initialise HSTR_EL2. All fields are architecturally UNKNOWN on reset.
797 *
798 * HSTR_EL2.T<n>: Set all these fields to zero so that Non-secure EL0 or
799 * EL1 accesses to System registers do not trap to EL2.
800 */
801 write_hstr_el2(HSTR_EL2_RESET_VAL & ~(HSTR_EL2_T_MASK));
802
803 /*
804 * Initialise CNTHP_CTL_EL2. All fields are architecturally UNKNOWN on
805 * reset.
806 *
807 * CNTHP_CTL_EL2:ENABLE: Set to zero to disable the EL2 physical timer
808 * and prevent timer interrupts.
809 */
810 write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT));
811
812 manage_extensions_nonsecure_el2_unused();
813}
814
Soby Mathew12d0d002015-04-09 13:40:55 +0100815/*******************************************************************************
Zelalem Awekec5ea4f82021-07-09 17:54:30 -0500816 * Prepare the CPU system registers for first entry into realm, secure, or
817 * normal world.
Andrew Thoelke167a9352014-06-04 21:10:52 +0100818 *
819 * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
820 * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
821 * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
822 * For all entries, the EL1 registers are initialized from the cpu_context
823 ******************************************************************************/
824void cm_prepare_el3_exit(uint32_t security_state)
825{
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000826 u_register_t sctlr_elx, scr_el3;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100827 cpu_context_t *ctx = cm_get_context(security_state);
828
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000829 assert(ctx != NULL);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100830
831 if (security_state == NON_SECURE) {
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600832 uint64_t el2_implemented = el_implemented(2);
833
Louis Mayencourtf1be00d2020-01-24 13:30:28 +0000834 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx),
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000835 CTX_SCR_EL3);
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600836
837 if (((scr_el3 & SCR_HCE_BIT) != 0U)
838 || (el2_implemented != EL_IMPL_NONE)) {
839 /*
840 * If context is not being used for EL2, initialize
841 * HCRX_EL2 with its init value here.
842 */
843 if (is_feat_hcx_supported()) {
844 write_hcrx_el2(HCRX_EL2_INIT_VAL);
845 }
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500846
847 /*
848 * Initialize Fine-grained trap registers introduced
849 * by FEAT_FGT so all traps are initially disabled when
850 * switching to EL2 or a lower EL, preventing undesired
851 * behavior.
852 */
853 if (is_feat_fgt_supported()) {
854 /*
855 * Initialize HFG*_EL2 registers with a default
856 * value so legacy systems unaware of FEAT_FGT
857 * do not get trapped due to their lack of
858 * initialization for this feature.
859 */
860 write_hfgitr_el2(HFGITR_EL2_INIT_VAL);
861 write_hfgrtr_el2(HFGRTR_EL2_INIT_VAL);
862 write_hfgwtr_el2(HFGWTR_EL2_INIT_VAL);
863 }
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600864 }
865
Juan Pablo Conde4a530b42023-07-10 16:00:41 -0500866
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000867 if ((scr_el3 & SCR_HCE_BIT) != 0U) {
Andrew Thoelke167a9352014-06-04 21:10:52 +0100868 /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
Max Shvetsov28259462020-02-17 16:15:47 +0000869 sctlr_elx = read_ctx_reg(get_el1_sysregs_ctx(ctx),
Antonio Nino Diaza0fee742018-10-31 15:25:35 +0000870 CTX_SCTLR_EL1);
Ken Kuang2e09d4f2017-08-23 16:03:29 +0800871 sctlr_elx &= SCTLR_EE_BIT;
Andrew Thoelke167a9352014-06-04 21:10:52 +0100872 sctlr_elx |= SCTLR_EL2_RES1;
Louis Mayencourt5f5d1ed2019-02-20 12:11:41 +0000873#if ERRATA_A75_764081
874 /*
875 * If workaround of errata 764081 for Cortex-A75 is used
876 * then set SCTLR_EL2.IESB to enable Implicit Error
877 * Synchronization Barrier.
878 */
879 sctlr_elx |= SCTLR_IESB_BIT;
880#endif
Andrew Thoelke167a9352014-06-04 21:10:52 +0100881 write_sctlr_el2(sctlr_elx);
Juan Pablo Condeddb615b2023-02-22 10:09:52 -0600882 } else if (el2_implemented != EL_IMPL_NONE) {
Boyan Karatotevb48bd792023-03-08 17:04:00 +0000883 init_nonsecure_el2_unused(ctx);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100884 }
885 }
886
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +0100887 cm_el1_sysregs_context_restore(security_state);
888 cm_set_next_eret_context(security_state);
Andrew Thoelke167a9352014-06-04 21:10:52 +0100889}
890
Max Shvetsov28f39f02020-02-25 13:56:19 +0000891#if CTX_INCLUDE_EL2_REGS
Andre Przywarabb7b85a2022-11-10 14:40:37 +0000892
893static void el2_sysregs_context_save_fgt(el2_sysregs_t *ctx)
894{
Andre Przywarade8c4892023-02-15 15:56:15 +0000895 write_ctx_reg(ctx, CTX_HDFGRTR_EL2, read_hdfgrtr_el2());
896 if (is_feat_amu_supported()) {
897 write_ctx_reg(ctx, CTX_HAFGRTR_EL2, read_hafgrtr_el2());
Andre Przywarabb7b85a2022-11-10 14:40:37 +0000898 }
Andre Przywarade8c4892023-02-15 15:56:15 +0000899 write_ctx_reg(ctx, CTX_HDFGWTR_EL2, read_hdfgwtr_el2());
900 write_ctx_reg(ctx, CTX_HFGITR_EL2, read_hfgitr_el2());
901 write_ctx_reg(ctx, CTX_HFGRTR_EL2, read_hfgrtr_el2());
902 write_ctx_reg(ctx, CTX_HFGWTR_EL2, read_hfgwtr_el2());
Andre Przywarabb7b85a2022-11-10 14:40:37 +0000903}
904
905static void el2_sysregs_context_restore_fgt(el2_sysregs_t *ctx)
906{
Andre Przywarade8c4892023-02-15 15:56:15 +0000907 write_hdfgrtr_el2(read_ctx_reg(ctx, CTX_HDFGRTR_EL2));
908 if (is_feat_amu_supported()) {
909 write_hafgrtr_el2(read_ctx_reg(ctx, CTX_HAFGRTR_EL2));
Andre Przywarabb7b85a2022-11-10 14:40:37 +0000910 }
Andre Przywarade8c4892023-02-15 15:56:15 +0000911 write_hdfgwtr_el2(read_ctx_reg(ctx, CTX_HDFGWTR_EL2));
912 write_hfgitr_el2(read_ctx_reg(ctx, CTX_HFGITR_EL2));
913 write_hfgrtr_el2(read_ctx_reg(ctx, CTX_HFGRTR_EL2));
914 write_hfgwtr_el2(read_ctx_reg(ctx, CTX_HFGWTR_EL2));
Andre Przywarabb7b85a2022-11-10 14:40:37 +0000915}
916
Andre Przywara9448f2b2022-11-17 16:42:09 +0000917static void el2_sysregs_context_save_mpam(el2_sysregs_t *ctx)
918{
919 u_register_t mpam_idr = read_mpamidr_el1();
920
921 write_ctx_reg(ctx, CTX_MPAM2_EL2, read_mpam2_el2());
922
923 /*
924 * The context registers that we intend to save would be part of the
925 * PE's system register frame only if MPAMIDR_EL1.HAS_HCR == 1.
926 */
927 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
928 return;
929 }
930
931 /*
932 * MPAMHCR_EL2, MPAMVPMV_EL2 and MPAMVPM0_EL2 are always present if
933 * MPAMIDR_HAS_HCR_BIT == 1.
934 */
935 write_ctx_reg(ctx, CTX_MPAMHCR_EL2, read_mpamhcr_el2());
936 write_ctx_reg(ctx, CTX_MPAMVPM0_EL2, read_mpamvpm0_el2());
937 write_ctx_reg(ctx, CTX_MPAMVPMV_EL2, read_mpamvpmv_el2());
938
939 /*
940 * The number of MPAMVPM registers is implementation defined, their
941 * number is stored in the MPAMIDR_EL1 register.
942 */
943 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
944 case 7:
945 write_ctx_reg(ctx, CTX_MPAMVPM7_EL2, read_mpamvpm7_el2());
946 __fallthrough;
947 case 6:
948 write_ctx_reg(ctx, CTX_MPAMVPM6_EL2, read_mpamvpm6_el2());
949 __fallthrough;
950 case 5:
951 write_ctx_reg(ctx, CTX_MPAMVPM5_EL2, read_mpamvpm5_el2());
952 __fallthrough;
953 case 4:
954 write_ctx_reg(ctx, CTX_MPAMVPM4_EL2, read_mpamvpm4_el2());
955 __fallthrough;
956 case 3:
957 write_ctx_reg(ctx, CTX_MPAMVPM3_EL2, read_mpamvpm3_el2());
958 __fallthrough;
959 case 2:
960 write_ctx_reg(ctx, CTX_MPAMVPM2_EL2, read_mpamvpm2_el2());
961 __fallthrough;
962 case 1:
963 write_ctx_reg(ctx, CTX_MPAMVPM1_EL2, read_mpamvpm1_el2());
964 break;
965 }
966}
967
968static void el2_sysregs_context_restore_mpam(el2_sysregs_t *ctx)
969{
970 u_register_t mpam_idr = read_mpamidr_el1();
971
972 write_mpam2_el2(read_ctx_reg(ctx, CTX_MPAM2_EL2));
973
974 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
975 return;
976 }
977
978 write_mpamhcr_el2(read_ctx_reg(ctx, CTX_MPAMHCR_EL2));
979 write_mpamvpm0_el2(read_ctx_reg(ctx, CTX_MPAMVPM0_EL2));
980 write_mpamvpmv_el2(read_ctx_reg(ctx, CTX_MPAMVPMV_EL2));
981
982 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
983 case 7:
984 write_mpamvpm7_el2(read_ctx_reg(ctx, CTX_MPAMVPM7_EL2));
985 __fallthrough;
986 case 6:
987 write_mpamvpm6_el2(read_ctx_reg(ctx, CTX_MPAMVPM6_EL2));
988 __fallthrough;
989 case 5:
990 write_mpamvpm5_el2(read_ctx_reg(ctx, CTX_MPAMVPM5_EL2));
991 __fallthrough;
992 case 4:
993 write_mpamvpm4_el2(read_ctx_reg(ctx, CTX_MPAMVPM4_EL2));
994 __fallthrough;
995 case 3:
996 write_mpamvpm3_el2(read_ctx_reg(ctx, CTX_MPAMVPM3_EL2));
997 __fallthrough;
998 case 2:
999 write_mpamvpm2_el2(read_ctx_reg(ctx, CTX_MPAMVPM2_EL2));
1000 __fallthrough;
1001 case 1:
1002 write_mpamvpm1_el2(read_ctx_reg(ctx, CTX_MPAMVPM1_EL2));
1003 break;
1004 }
1005}
1006
Max Shvetsov28f39f02020-02-25 13:56:19 +00001007/*******************************************************************************
1008 * Save EL2 sysreg context
1009 ******************************************************************************/
1010void cm_el2_sysregs_context_save(uint32_t security_state)
1011{
1012 u_register_t scr_el3 = read_scr();
1013
1014 /*
Zelalem Awekec5ea4f82021-07-09 17:54:30 -05001015 * Always save the non-secure and realm EL2 context, only save the
Max Shvetsov28f39f02020-02-25 13:56:19 +00001016 * S-EL2 context if S-EL2 is enabled.
1017 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -05001018 if ((security_state != SECURE) ||
Ruari Phipps6b704da2020-07-28 11:26:29 +01001019 ((security_state == SECURE) && ((scr_el3 & SCR_EEL2_BIT) != 0U))) {
Max Shvetsov28f39f02020-02-25 13:56:19 +00001020 cpu_context_t *ctx;
Zelalem Aweked20052f2022-04-04 17:42:48 -05001021 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsov28f39f02020-02-25 13:56:19 +00001022
1023 ctx = cm_get_context(security_state);
1024 assert(ctx != NULL);
1025
Zelalem Aweked20052f2022-04-04 17:42:48 -05001026 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
1027
1028 el2_sysregs_context_save_common(el2_sysregs_ctx);
Zelalem Aweked20052f2022-04-04 17:42:48 -05001029#if CTX_INCLUDE_MTE_REGS
1030 el2_sysregs_context_save_mte(el2_sysregs_ctx);
1031#endif
Andre Przywara9448f2b2022-11-17 16:42:09 +00001032 if (is_feat_mpam_supported()) {
1033 el2_sysregs_context_save_mpam(el2_sysregs_ctx);
1034 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001035
Andre Przywarade8c4892023-02-15 15:56:15 +00001036 if (is_feat_fgt_supported()) {
1037 el2_sysregs_context_save_fgt(el2_sysregs_ctx);
1038 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001039
Andre Przywarab8f03d22022-11-17 17:30:43 +00001040 if (is_feat_ecv_v2_supported()) {
1041 write_ctx_reg(el2_sysregs_ctx, CTX_CNTPOFF_EL2,
1042 read_cntpoff_el2());
1043 }
1044
Andre Przywaraea735bf2022-11-17 16:42:09 +00001045 if (is_feat_vhe_supported()) {
1046 write_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2,
1047 read_contextidr_el2());
1048 write_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2,
1049 read_ttbr1_el2());
1050 }
Andre Przywara6503ff22023-01-27 12:25:49 +00001051
1052 if (is_feat_ras_supported()) {
1053 write_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2,
1054 read_vdisr_el2());
1055 write_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2,
1056 read_vsesr_el2());
1057 }
Andre Przywarad5384b62023-01-27 14:09:20 +00001058
1059 if (is_feat_nv2_supported()) {
1060 write_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2,
1061 read_vncr_el2());
1062 }
1063
Andre Przywarafc8d2d32022-11-17 17:30:43 +00001064 if (is_feat_trf_supported()) {
1065 write_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2, read_trfcr_el2());
1066 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001067
1068 if (is_feat_csv2_2_supported()) {
1069 write_ctx_reg(el2_sysregs_ctx, CTX_SCXTNUM_EL2,
1070 read_scxtnum_el2());
1071 }
1072
Andre Przywarac5a3ebb2022-11-15 11:45:19 +00001073 if (is_feat_hcx_supported()) {
1074 write_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2, read_hcrx_el2());
1075 }
Mark Brownd3331602023-03-14 20:13:03 +00001076 if (is_feat_tcr2_supported()) {
1077 write_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2, read_tcr2_el2());
1078 }
Mark Brown062b6c62023-03-14 20:48:43 +00001079 if (is_feat_sxpie_supported()) {
1080 write_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2, read_pire0_el2());
1081 write_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2, read_pir_el2());
1082 }
1083 if (is_feat_s2pie_supported()) {
1084 write_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2, read_s2pir_el2());
1085 }
1086 if (is_feat_sxpoe_supported()) {
1087 write_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2, read_por_el2());
1088 }
Mark Brown688ab572023-03-14 21:33:04 +00001089 if (is_feat_gcs_supported()) {
1090 write_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2, read_gcspr_el2());
1091 write_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2, read_gcscr_el2());
1092 }
Max Shvetsov28f39f02020-02-25 13:56:19 +00001093 }
1094}
1095
1096/*******************************************************************************
1097 * Restore EL2 sysreg context
1098 ******************************************************************************/
1099void cm_el2_sysregs_context_restore(uint32_t security_state)
1100{
1101 u_register_t scr_el3 = read_scr();
1102
1103 /*
Zelalem Awekec5ea4f82021-07-09 17:54:30 -05001104 * Always restore the non-secure and realm EL2 context, only restore the
Max Shvetsov28f39f02020-02-25 13:56:19 +00001105 * S-EL2 context if S-EL2 is enabled.
1106 */
Zelalem Awekec5ea4f82021-07-09 17:54:30 -05001107 if ((security_state != SECURE) ||
Ruari Phipps6b704da2020-07-28 11:26:29 +01001108 ((security_state == SECURE) && ((scr_el3 & SCR_EEL2_BIT) != 0U))) {
Max Shvetsov28f39f02020-02-25 13:56:19 +00001109 cpu_context_t *ctx;
Zelalem Aweked20052f2022-04-04 17:42:48 -05001110 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsov28f39f02020-02-25 13:56:19 +00001111
1112 ctx = cm_get_context(security_state);
1113 assert(ctx != NULL);
1114
Zelalem Aweked20052f2022-04-04 17:42:48 -05001115 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
1116
1117 el2_sysregs_context_restore_common(el2_sysregs_ctx);
Zelalem Aweked20052f2022-04-04 17:42:48 -05001118#if CTX_INCLUDE_MTE_REGS
1119 el2_sysregs_context_restore_mte(el2_sysregs_ctx);
1120#endif
Andre Przywara9448f2b2022-11-17 16:42:09 +00001121 if (is_feat_mpam_supported()) {
1122 el2_sysregs_context_restore_mpam(el2_sysregs_ctx);
1123 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001124
Andre Przywarade8c4892023-02-15 15:56:15 +00001125 if (is_feat_fgt_supported()) {
1126 el2_sysregs_context_restore_fgt(el2_sysregs_ctx);
1127 }
Andre Przywarabb7b85a2022-11-10 14:40:37 +00001128
Andre Przywarab8f03d22022-11-17 17:30:43 +00001129 if (is_feat_ecv_v2_supported()) {
1130 write_cntpoff_el2(read_ctx_reg(el2_sysregs_ctx,
1131 CTX_CNTPOFF_EL2));
1132 }
1133
Andre Przywaraea735bf2022-11-17 16:42:09 +00001134 if (is_feat_vhe_supported()) {
1135 write_contextidr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2));
1136 write_ttbr1_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2));
1137 }
Andre Przywara6503ff22023-01-27 12:25:49 +00001138
1139 if (is_feat_ras_supported()) {
1140 write_vdisr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2));
1141 write_vsesr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2));
1142 }
Andre Przywarad5384b62023-01-27 14:09:20 +00001143
1144 if (is_feat_nv2_supported()) {
1145 write_vncr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2));
1146 }
Andre Przywarafc8d2d32022-11-17 17:30:43 +00001147 if (is_feat_trf_supported()) {
1148 write_trfcr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2));
1149 }
Andre Przywara7db710f2022-11-17 17:30:43 +00001150
1151 if (is_feat_csv2_2_supported()) {
1152 write_scxtnum_el2(read_ctx_reg(el2_sysregs_ctx,
1153 CTX_SCXTNUM_EL2));
1154 }
1155
Andre Przywarac5a3ebb2022-11-15 11:45:19 +00001156 if (is_feat_hcx_supported()) {
1157 write_hcrx_el2(read_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2));
1158 }
Mark Brownd3331602023-03-14 20:13:03 +00001159 if (is_feat_tcr2_supported()) {
1160 write_tcr2_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2));
1161 }
Mark Brown062b6c62023-03-14 20:48:43 +00001162 if (is_feat_sxpie_supported()) {
1163 write_pire0_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2));
1164 write_pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2));
1165 }
1166 if (is_feat_s2pie_supported()) {
1167 write_s2pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2));
1168 }
1169 if (is_feat_sxpoe_supported()) {
1170 write_por_el2(read_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2));
1171 }
Mark Brown688ab572023-03-14 21:33:04 +00001172 if (is_feat_gcs_supported()) {
1173 write_gcscr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2));
1174 write_gcspr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2));
1175 }
Max Shvetsov28f39f02020-02-25 13:56:19 +00001176 }
1177}
1178#endif /* CTX_INCLUDE_EL2_REGS */
1179
Andrew Thoelke167a9352014-06-04 21:10:52 +01001180/*******************************************************************************
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001181 * This function is used to exit to Non-secure world. If CTX_INCLUDE_EL2_REGS
1182 * is enabled, it restores EL1 and EL2 sysreg contexts instead of directly
1183 * updating EL1 and EL2 registers. Otherwise, it calls the generic
1184 * cm_prepare_el3_exit function.
1185 ******************************************************************************/
1186void cm_prepare_el3_exit_ns(void)
1187{
1188#if CTX_INCLUDE_EL2_REGS
Boyan Karatotev4085a022023-03-27 17:02:43 +01001189#if ENABLE_ASSERTIONS
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001190 cpu_context_t *ctx = cm_get_context(NON_SECURE);
1191 assert(ctx != NULL);
1192
Zelalem Awekeb515f542022-04-08 16:48:05 -05001193 /* Assert that EL2 is used. */
Boyan Karatotev4085a022023-03-27 17:02:43 +01001194 u_register_t scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
Zelalem Awekeb515f542022-04-08 16:48:05 -05001195 assert(((scr_el3 & SCR_HCE_BIT) != 0UL) &&
1196 (el_implemented(2U) != EL_IMPL_NONE));
Boyan Karatotev4085a022023-03-27 17:02:43 +01001197#endif /* ENABLE_ASSERTIONS */
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001198
1199 /*
1200 * Set the NS bit to be able to access the ICC_SRE_EL2
1201 * register when restoring context.
1202 */
1203 write_scr_el3(read_scr_el3() | SCR_NS_BIT);
1204
Olivier Deprez04825032022-05-09 17:34:02 +02001205 /*
1206 * Ensure the NS bit change is committed before the EL2/EL1
1207 * state restoration.
1208 */
1209 isb();
1210
Zelalem Aweke8b95e842022-01-31 16:59:42 -06001211 /* Restore EL2 and EL1 sysreg contexts */
1212 cm_el2_sysregs_context_restore(NON_SECURE);
1213 cm_el1_sysregs_context_restore(NON_SECURE);
1214 cm_set_next_eret_context(NON_SECURE);
1215#else
1216 cm_prepare_el3_exit(NON_SECURE);
1217#endif /* CTX_INCLUDE_EL2_REGS */
1218}
1219
1220/*******************************************************************************
Soby Mathewfdfabec2014-07-04 16:02:26 +01001221 * The next four functions are used by runtime services to save and restore
1222 * EL1 context on the 'cpu_context' structure for the specified security
Achin Gupta7aea9082014-02-01 07:51:28 +00001223 * state.
1224 ******************************************************************************/
Achin Gupta7aea9082014-02-01 07:51:28 +00001225void cm_el1_sysregs_context_save(uint32_t security_state)
1226{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001227 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001228
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001229 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001230 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001231
Max Shvetsov28259462020-02-17 16:15:47 +00001232 el1_sysregs_context_save(get_el1_sysregs_ctx(ctx));
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +01001233
1234#if IMAGE_BL31
1235 if (security_state == SECURE)
1236 PUBLISH_EVENT(cm_exited_secure_world);
1237 else
1238 PUBLISH_EVENT(cm_exited_normal_world);
1239#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001240}
1241
1242void cm_el1_sysregs_context_restore(uint32_t security_state)
1243{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001244 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001245
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001246 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001247 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001248
Max Shvetsov28259462020-02-17 16:15:47 +00001249 el1_sysregs_context_restore(get_el1_sysregs_ctx(ctx));
Dimitris Papastamos17b4c0d2017-10-13 15:27:58 +01001250
1251#if IMAGE_BL31
1252 if (security_state == SECURE)
1253 PUBLISH_EVENT(cm_entering_secure_world);
1254 else
1255 PUBLISH_EVENT(cm_entering_normal_world);
1256#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001257}
1258
1259/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +01001260 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
1261 * given security state with the given entrypoint
Achin Gupta607084e2014-02-09 18:24:19 +00001262 ******************************************************************************/
Soby Mathew4c0d0392016-06-16 14:52:04 +01001263void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
Achin Gupta607084e2014-02-09 18:24:19 +00001264{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001265 cpu_context_t *ctx;
1266 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +00001267
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001268 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001269 assert(ctx != NULL);
Achin Gupta607084e2014-02-09 18:24:19 +00001270
1271 /* Populate EL3 state so that ERET jumps to the correct entry */
1272 state = get_el3state_ctx(ctx);
1273 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
1274}
1275
1276/*******************************************************************************
Andrew Thoelke167a9352014-06-04 21:10:52 +01001277 * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
1278 * pertaining to the given security state
1279 ******************************************************************************/
1280void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathew4c0d0392016-06-16 14:52:04 +01001281 uintptr_t entrypoint, uint32_t spsr)
Andrew Thoelke167a9352014-06-04 21:10:52 +01001282{
1283 cpu_context_t *ctx;
1284 el3_state_t *state;
1285
1286 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001287 assert(ctx != NULL);
Andrew Thoelke167a9352014-06-04 21:10:52 +01001288
1289 /* Populate EL3 state so that ERET jumps to the correct entry */
1290 state = get_el3state_ctx(ctx);
1291 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
1292 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
1293}
1294
1295/*******************************************************************************
Achin Guptac429b5e2014-05-04 18:38:28 +01001296 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
1297 * pertaining to the given security state using the value and bit position
1298 * specified in the parameters. It preserves all other bits.
1299 ******************************************************************************/
1300void cm_write_scr_el3_bit(uint32_t security_state,
1301 uint32_t bit_pos,
1302 uint32_t value)
1303{
1304 cpu_context_t *ctx;
1305 el3_state_t *state;
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001306 u_register_t scr_el3;
Achin Guptac429b5e2014-05-04 18:38:28 +01001307
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001308 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001309 assert(ctx != NULL);
Achin Guptac429b5e2014-05-04 18:38:28 +01001310
1311 /* Ensure that the bit position is a valid one */
Jimmy Brissond7b5f402020-08-04 16:18:52 -05001312 assert(((1UL << bit_pos) & SCR_VALID_BIT_MASK) != 0U);
Achin Guptac429b5e2014-05-04 18:38:28 +01001313
1314 /* Ensure that the 'value' is only a bit wide */
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001315 assert(value <= 1U);
Achin Guptac429b5e2014-05-04 18:38:28 +01001316
1317 /*
1318 * Get the SCR_EL3 value from the cpu context, clear the desired bit
1319 * and set it to its new value.
1320 */
1321 state = get_el3state_ctx(ctx);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001322 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
Jimmy Brissond7b5f402020-08-04 16:18:52 -05001323 scr_el3 &= ~(1UL << bit_pos);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001324 scr_el3 |= (u_register_t)value << bit_pos;
Achin Guptac429b5e2014-05-04 18:38:28 +01001325 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
1326}
1327
1328/*******************************************************************************
1329 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
1330 * given security state.
1331 ******************************************************************************/
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001332u_register_t cm_get_scr_el3(uint32_t security_state)
Achin Guptac429b5e2014-05-04 18:38:28 +01001333{
1334 cpu_context_t *ctx;
1335 el3_state_t *state;
1336
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001337 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001338 assert(ctx != NULL);
Achin Guptac429b5e2014-05-04 18:38:28 +01001339
1340 /* Populate EL3 state so that ERET jumps to the correct entry */
1341 state = get_el3state_ctx(ctx);
Louis Mayencourtf1be00d2020-01-24 13:30:28 +00001342 return read_ctx_reg(state, CTX_SCR_EL3);
Achin Guptac429b5e2014-05-04 18:38:28 +01001343}
1344
1345/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001346 * This function is used to program the context that's used for exception
1347 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
1348 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +00001349 ******************************************************************************/
1350void cm_set_next_eret_context(uint32_t security_state)
1351{
Dan Handleyfb037bf2014-04-10 15:37:22 +01001352 cpu_context_t *ctx;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001353
Andrew Thoelke08ab89d2014-05-14 17:09:32 +01001354 ctx = cm_get_context(security_state);
Antonio Nino Diaza0fee742018-10-31 15:25:35 +00001355 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001356
Andrew Thoelke167a9352014-06-04 21:10:52 +01001357 cm_set_next_context(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +00001358}