blob: d9ff0b6e6dd152d301e67b074dabed7359d1d448 [file] [log] [blame]
Achin Gupta7aea9082014-02-01 07:51:28 +00001/*
Maksims Svecovs1e25c5b2023-02-02 16:10:22 +00002 * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
Varun Wadekarcc238bb2022-09-13 12:38:47 +01003 * Copyright (c) 2022, NVIDIA Corporation. All rights reserved.
Achin Gupta7aea9082014-02-01 07:51:28 +00004 *
dp-armfa3cf0b2017-05-03 09:38:09 +01005 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta7aea9082014-02-01 07:51:28 +00006 */
7
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008#include <assert.h>
9#include <stdbool.h>
10#include <string.h>
11
12#include <platform_def.h>
13
Achin Gupta27b895e2014-05-04 18:38:28 +010014#include <arch.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000015#include <arch_helpers.h>
Soby Mathew830f0ad2019-07-12 09:23:38 +010016#include <arch_features.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <bl31/interrupt_mgmt.h>
18#include <common/bl_common.h>
Claus Pedersen785e66c2022-09-12 22:42:58 +000019#include <common/debug.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010020#include <context.h>
Zelalem Awekef92c0cb2022-01-31 16:59:42 -060021#include <drivers/arm/gicv3.h>
Antonio Nino Diaze0f90632018-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>
johpow0181865962022-01-28 17:06:20 -060025#include <lib/extensions/brbe.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000026#include <lib/extensions/mpam.h>
Boyan Karatotev05504ba2023-02-15 13:21:50 +000027#include <lib/extensions/pmuv3.h>
johpow019baade32021-07-08 14:14:00 -050028#include <lib/extensions/sme.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000029#include <lib/extensions/spe.h>
30#include <lib/extensions/sve.h>
Manish V Badarkhef356f7e2021-06-29 11:44:20 +010031#include <lib/extensions/sys_reg_trace.h>
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010032#include <lib/extensions/trbe.h>
Manish V Badarkhe51a97112021-07-08 09:33:18 +010033#include <lib/extensions/trf.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000034#include <lib/utils.h>
Achin Gupta7aea9082014-02-01 07:51:28 +000035
Jayanth Dodderi Chidanand4b5489c2022-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 Karatotev36cebf92023-03-08 11:56:49 +000041static void manage_extensions_nonsecure(cpu_context_t *ctx);
Jayanth Dodderi Chidanand4b5489c2022-03-28 15:28:55 +010042static void manage_extensions_secure(cpu_context_t *ctx);
Zelalem Aweke20126002022-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 Aweke42401112022-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 Aweke42401112022-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 Aweke42401112022-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.
117 */
118 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.
Achin Gupta7aea9082014-02-01 07:51:28 +0000128 */
Zelalem Aweke42401112022-01-05 17:12:24 -0600129#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 Przywara6dd2d062023-02-22 16:53:50 +0000139 if ((GET_EL(ep->spsr) == MODE_EL2) && is_feat_sel2_supported()) {
Zelalem Aweke42401112022-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 Aweke20126002022-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 Aweke42401112022-01-05 17:12:24 -0600158 manage_extensions_secure(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +0000159}
160
Zelalem Aweke42401112022-01-05 17:12:24 -0600161#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 Svecovs1e25c5b2023-02-02 16:10:22 +0000174 scr_el3 |= SCR_NS_BIT | SCR_NSE_BIT;
175
Andre Przywara902c9022022-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 Aweke42401112022-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 Pandey0e3379d2022-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 Pandey7c6fcb42022-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 Przywara902c9022022-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 Svecovs1e25c5b2023-02-02 16:10:22 +0000236
Zelalem Aweke42401112022-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 Awekef92c0cb2022-01-31 16:59:42 -0600245
Zelalem Aweke20126002022-04-08 16:48:05 -0500246 /* Initialize EL1 context registers */
247 setup_el1_context(ctx, ep);
248
Zelalem Awekef92c0cb2022-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 Wadekarcc238bb2022-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 Awekef92c0cb2022-01-31 16:59:42 -0600264 */
Varun Wadekarcc238bb2022-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 Awekef92c0cb2022-01-31 16:59:42 -0600267 write_ctx_reg(get_el2_sysregs_ctx(ctx), CTX_ICC_SRE_EL2,
268 icc_sre_el2);
Boyan Karatotevecd9f082022-10-26 15:10:39 +0100269
Juan Pablo Conde72e0da12023-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 }
Zelalem Awekef92c0cb2022-01-31 16:59:42 -0600282#endif /* CTX_INCLUDE_EL2_REGS */
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000283
284 manage_extensions_nonsecure(ctx);
Zelalem Aweke42401112022-01-05 17:12:24 -0600285}
286
Achin Gupta7aea9082014-02-01 07:51:28 +0000287/*******************************************************************************
Zelalem Aweke42401112022-01-05 17:12:24 -0600288 * The following function performs initialization of the cpu_context 'ctx'
289 * for first use that is common to all security states, and sets the
290 * initial entrypoint state as specified by the entry_point_info structure.
Andrew Thoelke4e126072014-06-04 21:10:52 +0100291 *
Paul Beesley1fbc97b2019-01-11 18:26:51 +0000292 * The EE and ST attributes are used to configure the endianness and secure
Soby Mathewb0082d22015-04-09 13:40:55 +0100293 * timer availability for the new execution context.
Andrew Thoelke4e126072014-06-04 21:10:52 +0100294 ******************************************************************************/
Zelalem Aweke42401112022-01-05 17:12:24 -0600295static void setup_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
Andrew Thoelke4e126072014-06-04 21:10:52 +0100296{
Louis Mayencourt1c819c32020-01-24 13:30:28 +0000297 u_register_t scr_el3;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100298 el3_state_t *state;
299 gp_regs_t *gp_regs;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100300
Andrew Thoelke4e126072014-06-04 21:10:52 +0100301 /* Clear any residual register values from the context */
Douglas Raillarda8954fc2017-01-26 15:54:44 +0000302 zeromem(ctx, sizeof(*ctx));
Andrew Thoelke4e126072014-06-04 21:10:52 +0100303
304 /*
David Cunadofee86532017-04-13 22:38:29 +0100305 * SCR_EL3 was initialised during reset sequence in macro
306 * el3_arch_init_common. This code modifies the SCR_EL3 fields that
307 * affect the next EL.
308 *
309 * The following fields are initially set to zero and then updated to
310 * the required value depending on the state of the SPSR_EL3 and the
311 * Security state and entrypoint attributes of the next EL.
Andrew Thoelke4e126072014-06-04 21:10:52 +0100312 */
Louis Mayencourt1c819c32020-01-24 13:30:28 +0000313 scr_el3 = read_scr();
Manish Pandey0e3379d2022-10-10 11:43:08 +0100314 scr_el3 &= ~(SCR_NS_BIT | SCR_RW_BIT | SCR_EA_BIT | SCR_FIQ_BIT | SCR_IRQ_BIT |
Zelalem Aweke42401112022-01-05 17:12:24 -0600315 SCR_ST_BIT | SCR_HCE_BIT | SCR_NSE_BIT);
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500316
David Cunadofee86532017-04-13 22:38:29 +0100317 /*
318 * SCR_EL3.RW: Set the execution state, AArch32 or AArch64, for next
319 * Exception level as specified by SPSR.
320 */
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500321 if (GET_RW(ep->spsr) == MODE_RW_64) {
Andrew Thoelke4e126072014-06-04 21:10:52 +0100322 scr_el3 |= SCR_RW_BIT;
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500323 }
Zelalem Aweke42401112022-01-05 17:12:24 -0600324
David Cunadofee86532017-04-13 22:38:29 +0100325 /*
326 * SCR_EL3.ST: Traps Secure EL1 accesses to the Counter-timer Physical
Zelalem Aweke20126002022-04-08 16:48:05 -0500327 * Secure timer registers to EL3, from AArch64 state only, if specified
328 * by the entrypoint attributes. If SEL2 is present and enabled, the ST
329 * bit always behaves as 1 (i.e. secure physical timer register access
330 * is not trapped)
David Cunadofee86532017-04-13 22:38:29 +0100331 */
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500332 if (EP_GET_ST(ep->h.attr) != 0U) {
Andrew Thoelke4e126072014-06-04 21:10:52 +0100333 scr_el3 |= SCR_ST_BIT;
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500334 }
Andrew Thoelke4e126072014-06-04 21:10:52 +0100335
johpow01f91e59f2021-08-04 19:38:18 -0500336 /*
337 * If FEAT_HCX is enabled, enable access to HCRX_EL2 by setting
338 * SCR_EL3.HXEn.
339 */
Andre Przywara1d8795e2022-11-15 11:45:19 +0000340 if (is_feat_hcx_supported()) {
341 scr_el3 |= SCR_HXEn_BIT;
342 }
johpow01f91e59f2021-08-04 19:38:18 -0500343
Juan Pablo Conde42305f22022-07-12 16:40:29 -0400344 /*
345 * If FEAT_RNG_TRAP is enabled, all reads of the RNDR and RNDRRS
346 * registers are trapped to EL3.
347 */
348#if ENABLE_FEAT_RNG_TRAP
349 scr_el3 |= SCR_TRNDR_BIT;
350#endif
351
Jeenu Viswambharanf00da742017-12-08 12:13:51 +0000352#if FAULT_INJECTION_SUPPORT
353 /* Enable fault injection from lower ELs */
354 scr_el3 |= SCR_FIEN_BIT;
355#endif
356
Antonio Nino Diaz594811b2019-01-31 11:58:00 +0000357 /*
Mark Brownc37eee72023-03-14 20:13:03 +0000358 * SCR_EL3.TCR2EN: Enable access to TCR2_ELx for AArch64 if present.
359 */
360 if (is_feat_tcr2_supported() && (GET_RW(ep->spsr) == MODE_RW_64)) {
361 scr_el3 |= SCR_TCR2EN_BIT;
362 }
363
364 /*
Mark Brown293a6612023-03-14 20:48:43 +0000365 * SCR_EL3.PIEN: Enable permission indirection and overlay
366 * registers for AArch64 if present.
367 */
368 if (is_feat_sxpie_supported() || is_feat_sxpoe_supported()) {
369 scr_el3 |= SCR_PIEN_BIT;
370 }
371
372 /*
Mark Brown326f2952023-03-14 21:33:04 +0000373 * SCR_EL3.GCSEn: Enable GCS registers for AArch64 if present.
374 */
375 if ((is_feat_gcs_supported()) && (GET_RW(ep->spsr) == MODE_RW_64)) {
376 scr_el3 |= SCR_GCSEn_BIT;
377 }
378
379 /*
Zelalem Aweke42401112022-01-05 17:12:24 -0600380 * CPTR_EL3 was initialized out of reset, copy that value to the
381 * context register.
Antonio Nino Diaz594811b2019-01-31 11:58:00 +0000382 */
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100383 write_ctx_reg(get_el3state_ctx(ctx), CTX_CPTR_EL3, read_cptr_el3());
Max Shvetsovc4502772021-03-22 11:59:37 +0000384
Andrew Thoelke4e126072014-06-04 21:10:52 +0100385 /*
David Cunadofee86532017-04-13 22:38:29 +0100386 * SCR_EL3.HCE: Enable HVC instructions if next execution state is
387 * AArch64 and next EL is EL2, or if next execution state is AArch32 and
388 * next mode is Hyp.
Jimmy Brissonecc3c672020-04-16 10:47:56 -0500389 * SCR_EL3.FGTEn: Enable Fine Grained Virtualization Traps under the
390 * same conditions as HVC instructions and when the processor supports
391 * ARMv8.6-FGT.
Jimmy Brisson83573892020-04-16 10:48:02 -0500392 * SCR_EL3.ECVEn: Enable Enhanced Counter Virtualization (ECV)
393 * CNTPOFF_EL2 register under the same conditions as HVC instructions
394 * and when the processor supports ECV.
David Cunadofee86532017-04-13 22:38:29 +0100395 */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000396 if (((GET_RW(ep->spsr) == MODE_RW_64) && (GET_EL(ep->spsr) == MODE_EL2))
397 || ((GET_RW(ep->spsr) != MODE_RW_64)
398 && (GET_M32(ep->spsr) == MODE32_hyp))) {
David Cunadofee86532017-04-13 22:38:29 +0100399 scr_el3 |= SCR_HCE_BIT;
Jimmy Brissonecc3c672020-04-16 10:47:56 -0500400
Andre Przywarae8920f62022-11-10 14:28:01 +0000401 if (is_feat_fgt_supported()) {
Jimmy Brissonecc3c672020-04-16 10:47:56 -0500402 scr_el3 |= SCR_FGTEN_BIT;
403 }
Jimmy Brisson83573892020-04-16 10:48:02 -0500404
Andre Przywarac3464182022-11-17 17:30:43 +0000405 if (is_feat_ecv_supported()) {
Jimmy Brisson83573892020-04-16 10:48:02 -0500406 scr_el3 |= SCR_ECVEN_BIT;
407 }
David Cunadofee86532017-04-13 22:38:29 +0100408 }
409
johpow013e24c162020-04-22 14:05:13 -0500410 /* Enable WFE trap delay in SCR_EL3 if supported and configured */
Andre Przywara0cf77402023-01-27 12:25:49 +0000411 if (is_feat_twed_supported()) {
412 /* Set delay in SCR_EL3 */
413 scr_el3 &= ~(SCR_TWEDEL_MASK << SCR_TWEDEL_SHIFT);
414 scr_el3 |= ((TWED_DELAY & SCR_TWEDEL_MASK)
415 << SCR_TWEDEL_SHIFT);
johpow013e24c162020-04-22 14:05:13 -0500416
Andre Przywara0cf77402023-01-27 12:25:49 +0000417 /* Enable WFE delay */
418 scr_el3 |= SCR_TWEDEn_BIT;
419 }
johpow013e24c162020-04-22 14:05:13 -0500420
David Cunadofee86532017-04-13 22:38:29 +0100421 /*
Alexei Fedorov503bbf32019-08-13 15:17:53 +0100422 * Populate EL3 state so that we've the right context
423 * before doing ERET
424 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100425 state = get_el3state_ctx(ctx);
426 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
427 write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
428 write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
429
430 /*
431 * Store the X0-X7 value from the entrypoint into the context
432 * Use memcpy as we are in control of the layout of the structures
433 */
434 gp_regs = get_gpregs_ctx(ctx);
435 memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
436}
437
438/*******************************************************************************
Zelalem Aweke42401112022-01-05 17:12:24 -0600439 * Context management library initialization routine. This library is used by
440 * runtime services to share pointers to 'cpu_context' structures for secure
441 * non-secure and realm states. Management of the structures and their associated
442 * memory is not done by the context management library e.g. the PSCI service
443 * manages the cpu context used for entry from and exit to the non-secure state.
444 * The Secure payload dispatcher service manages the context(s) corresponding to
445 * the secure state. It also uses this library to get access to the non-secure
446 * state cpu context pointers.
447 * Lastly, this library provides the API to make SP_EL3 point to the cpu context
448 * which will be used for programming an entry into a lower EL. The same context
449 * will be used to save state upon exception entry from that EL.
450 ******************************************************************************/
451void __init cm_init(void)
452{
453 /*
Elyes Haouas2be03c02023-02-13 09:14:48 +0100454 * The context management library has only global data to initialize, but
Zelalem Aweke42401112022-01-05 17:12:24 -0600455 * that will be done when the BSS is zeroed out.
456 */
457}
458
459/*******************************************************************************
460 * This is the high-level function used to initialize the cpu_context 'ctx' for
461 * first use. It performs initializations that are common to all security states
462 * and initializations specific to the security state specified in 'ep'
463 ******************************************************************************/
464void cm_setup_context(cpu_context_t *ctx, const entry_point_info_t *ep)
465{
466 unsigned int security_state;
467
468 assert(ctx != NULL);
469
470 /*
471 * Perform initializations that are common
472 * to all security states
473 */
474 setup_context_common(ctx, ep);
475
476 security_state = GET_SECURITY_STATE(ep->h.attr);
477
478 /* Perform security state specific initializations */
479 switch (security_state) {
480 case SECURE:
481 setup_secure_context(ctx, ep);
482 break;
483#if ENABLE_RME
484 case REALM:
485 setup_realm_context(ctx, ep);
486 break;
487#endif
488 case NON_SECURE:
489 setup_ns_context(ctx, ep);
490 break;
491 default:
492 ERROR("Invalid security state\n");
493 panic();
494 break;
495 }
496}
497
498/*******************************************************************************
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000499 * Enable architecture extensions for EL3 execution. This function only updates
500 * registers in-place which are expected to either never change or be
501 * overwritten by el3_exit.
502 ******************************************************************************/
503#if IMAGE_BL31
504void cm_manage_extensions_el3(void)
505{
506 if (is_feat_spe_supported()) {
507 spe_init_el3();
Boyan Karatotev7f5dcc72023-03-08 16:29:26 +0000508 }
509
Boyan Karatotev1e966f32023-03-27 17:02:43 +0100510 if (is_feat_amu_supported()) {
511 amu_init_el3();
512 }
513
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +0000514 if (is_feat_sme_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000515 sme_init_el3();
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +0000516 }
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100517
Andre Przywara84b86532022-11-17 16:42:09 +0000518 if (is_feat_mpam_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000519 mpam_init_el3();
Andre Przywara84b86532022-11-17 16:42:09 +0000520 }
Manish V Badarkhe20df29c2021-07-02 09:10:56 +0100521
Andre Przywara191eff62022-11-17 16:42:09 +0000522 if (is_feat_trbe_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000523 trbe_init_el3();
Andre Przywara191eff62022-11-17 16:42:09 +0000524 }
Manish V Badarkhe20df29c2021-07-02 09:10:56 +0100525
Andre Przywarac97c5512022-11-17 16:42:09 +0000526 if (is_feat_brbe_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000527 brbe_init_el3();
Andre Przywarac97c5512022-11-17 16:42:09 +0000528 }
johpow0181865962022-01-28 17:06:20 -0600529
Andre Przywara06ea44e2022-11-17 17:30:43 +0000530 if (is_feat_trf_supported()) {
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000531 trf_init_el3();
Andre Przywara06ea44e2022-11-17 17:30:43 +0000532 }
Dimitris Papastamos1e6f93e2017-11-07 09:55:29 +0000533
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000534 pmuv3_init_el3();
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000535}
536#endif /* IMAGE_BL31 */
537
538/*******************************************************************************
539 * Enable architecture extensions on first entry to Non-secure world.
540 ******************************************************************************/
541static void manage_extensions_nonsecure(cpu_context_t *ctx)
542{
543#if IMAGE_BL31
Boyan Karatotev1e966f32023-03-27 17:02:43 +0100544 if (is_feat_amu_supported()) {
545 amu_enable(ctx);
546 }
547
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000548 /* Enable SVE and FPU/SIMD */
549 if (is_feat_sve_supported()) {
550 sve_enable(ctx);
551 }
552
553 if (is_feat_sme_supported()) {
554 sme_enable(ctx);
555 }
556
557 if (is_feat_sys_reg_trace_supported()) {
558 sys_reg_trace_enable(ctx);
559 }
560
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000561 pmuv3_enable(ctx);
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000562#endif /* IMAGE_BL31 */
563}
564
565/*******************************************************************************
566 * Enable architecture extensions in-place at EL2 on first entry to Non-secure
567 * world when EL2 is empty and unused.
568 ******************************************************************************/
569static void manage_extensions_nonsecure_el2_unused(void)
570{
571#if IMAGE_BL31
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000572 if (is_feat_spe_supported()) {
573 spe_init_el2_unused();
574 }
575
Boyan Karatotev1e966f32023-03-27 17:02:43 +0100576 if (is_feat_amu_supported()) {
577 amu_init_el2_unused();
578 }
579
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000580 if (is_feat_mpam_supported()) {
581 mpam_init_el2_unused();
582 }
583
584 if (is_feat_trbe_supported()) {
585 trbe_init_el2_unused();
586 }
587
588 if (is_feat_sys_reg_trace_supported()) {
589 sys_reg_trace_init_el2_unused();
590 }
591
592 if (is_feat_trf_supported()) {
593 trf_init_el2_unused();
594 }
595
Boyan Karatotev05504ba2023-02-15 13:21:50 +0000596 pmuv3_init_el2_unused();
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000597
598 if (is_feat_sve_supported()) {
599 sve_init_el2_unused();
600 }
601
602 if (is_feat_sme_supported()) {
603 sme_init_el2_unused();
604 }
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000605#endif /* IMAGE_BL31 */
606}
607
608/*******************************************************************************
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100609 * Enable architecture extensions on first entry to Secure world.
610 ******************************************************************************/
johpow019baade32021-07-08 14:14:00 -0500611static void manage_extensions_secure(cpu_context_t *ctx)
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100612{
613#if IMAGE_BL31
Boyan Karatotev7f5dcc72023-03-08 16:29:26 +0000614 if (is_feat_sve_supported()) {
Jayanth Dodderi Chidanandd62c6812023-03-07 10:43:19 +0000615 if (ENABLE_SVE_FOR_SWD) {
616 /*
617 * Enable SVE and FPU in secure context, secure manager must
618 * ensure that the SVE and FPU register contexts are properly
619 * managed.
620 */
621 sve_enable(ctx);
622 } else {
623 /*
624 * Disable SVE and FPU in secure context so non-secure world
625 * can safely use them.
626 */
627 sve_disable(ctx);
628 }
629 }
630
Boyan Karatotev7f5dcc72023-03-08 16:29:26 +0000631 if (is_feat_sme_supported()) {
632 if (ENABLE_SME_FOR_SWD) {
633 /*
634 * Enable SME, SVE, FPU/SIMD in secure context, secure manager
635 * must ensure SME, SVE, and FPU/SIMD context properly managed.
636 */
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000637 sme_init_el3();
Boyan Karatotev7f5dcc72023-03-08 16:29:26 +0000638 sme_enable(ctx);
639 } else {
640 /*
641 * Disable SME, SVE, FPU/SIMD in secure context so non-secure
642 * world can safely use the associated registers.
643 */
644 sme_disable(ctx);
645 }
646 }
johpow019baade32021-07-08 14:14:00 -0500647#endif /* IMAGE_BL31 */
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +0100648}
649
650/*******************************************************************************
Soby Mathewb0082d22015-04-09 13:40:55 +0100651 * The following function initializes the cpu_context for a CPU specified by
652 * its `cpu_idx` for first use, and sets the initial entrypoint state as
653 * specified by the entry_point_info structure.
654 ******************************************************************************/
655void cm_init_context_by_index(unsigned int cpu_idx,
656 const entry_point_info_t *ep)
657{
658 cpu_context_t *ctx;
659 ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz28dce9e2018-05-22 10:09:10 +0100660 cm_setup_context(ctx, ep);
Soby Mathewb0082d22015-04-09 13:40:55 +0100661}
662
663/*******************************************************************************
664 * The following function initializes the cpu_context for the current CPU
665 * for first use, and sets the initial entrypoint state as specified by the
666 * entry_point_info structure.
667 ******************************************************************************/
668void cm_init_my_context(const entry_point_info_t *ep)
669{
670 cpu_context_t *ctx;
671 ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
Antonio Nino Diaz28dce9e2018-05-22 10:09:10 +0100672 cm_setup_context(ctx, ep);
Soby Mathewb0082d22015-04-09 13:40:55 +0100673}
674
675/*******************************************************************************
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500676 * Prepare the CPU system registers for first entry into realm, secure, or
677 * normal world.
Andrew Thoelke4e126072014-06-04 21:10:52 +0100678 *
679 * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
680 * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
681 * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
682 * For all entries, the EL1 registers are initialized from the cpu_context
683 ******************************************************************************/
684void cm_prepare_el3_exit(uint32_t security_state)
685{
Louis Mayencourt1c819c32020-01-24 13:30:28 +0000686 u_register_t sctlr_elx, scr_el3, mdcr_el2;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100687 cpu_context_t *ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000688 uint64_t hcr_el2 = 0U;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100689
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000690 assert(ctx != NULL);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100691
692 if (security_state == NON_SECURE) {
Juan Pablo Conde72e0da12023-02-22 10:09:52 -0600693 uint64_t el2_implemented = el_implemented(2);
694
Louis Mayencourt1c819c32020-01-24 13:30:28 +0000695 scr_el3 = read_ctx_reg(get_el3state_ctx(ctx),
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000696 CTX_SCR_EL3);
Juan Pablo Conde72e0da12023-02-22 10:09:52 -0600697
698 if (((scr_el3 & SCR_HCE_BIT) != 0U)
699 || (el2_implemented != EL_IMPL_NONE)) {
700 /*
701 * If context is not being used for EL2, initialize
702 * HCRX_EL2 with its init value here.
703 */
704 if (is_feat_hcx_supported()) {
705 write_hcrx_el2(HCRX_EL2_INIT_VAL);
706 }
707 }
708
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000709 if ((scr_el3 & SCR_HCE_BIT) != 0U) {
Andrew Thoelke4e126072014-06-04 21:10:52 +0100710 /* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
Max Shvetsovc9e2c922020-02-17 16:15:47 +0000711 sctlr_elx = read_ctx_reg(get_el1_sysregs_ctx(ctx),
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000712 CTX_SCTLR_EL1);
Ken Kuang00eac152017-08-23 16:03:29 +0800713 sctlr_elx &= SCTLR_EE_BIT;
Andrew Thoelke4e126072014-06-04 21:10:52 +0100714 sctlr_elx |= SCTLR_EL2_RES1;
Louis Mayencourt78a0aed2019-02-20 12:11:41 +0000715#if ERRATA_A75_764081
716 /*
717 * If workaround of errata 764081 for Cortex-A75 is used
718 * then set SCTLR_EL2.IESB to enable Implicit Error
719 * Synchronization Barrier.
720 */
721 sctlr_elx |= SCTLR_IESB_BIT;
722#endif
Andrew Thoelke4e126072014-06-04 21:10:52 +0100723 write_sctlr_el2(sctlr_elx);
Juan Pablo Conde72e0da12023-02-22 10:09:52 -0600724 } else if (el2_implemented != EL_IMPL_NONE) {
David Cunadofee86532017-04-13 22:38:29 +0100725 /*
726 * EL2 present but unused, need to disable safely.
727 * SCTLR_EL2 can be ignored in this case.
728 *
Jeenu Viswambharancbad6612018-08-15 14:29:29 +0100729 * Set EL2 register width appropriately: Set HCR_EL2
730 * field to match SCR_EL3.RW.
David Cunadofee86532017-04-13 22:38:29 +0100731 */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +0000732 if ((scr_el3 & SCR_RW_BIT) != 0U)
Jeenu Viswambharancbad6612018-08-15 14:29:29 +0100733 hcr_el2 |= HCR_RW_BIT;
734
735 /*
736 * For Armv8.3 pointer authentication feature, disable
737 * traps to EL2 when accessing key registers or using
738 * pointer authentication instructions from lower ELs.
739 */
740 hcr_el2 |= (HCR_API_BIT | HCR_APK_BIT);
741
742 write_hcr_el2(hcr_el2);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100743
David Cunadofee86532017-04-13 22:38:29 +0100744 /*
745 * Initialise CPTR_EL2 setting all fields rather than
746 * relying on the hw. All fields have architecturally
747 * UNKNOWN reset values.
David Cunadofee86532017-04-13 22:38:29 +0100748 */
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000749 write_cptr_el2(CPTR_EL2_RESET_VAL);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100750
David Cunadofee86532017-04-13 22:38:29 +0100751 /*
Paul Beesley1fbc97b2019-01-11 18:26:51 +0000752 * Initialise CNTHCTL_EL2. All fields are
David Cunadofee86532017-04-13 22:38:29 +0100753 * architecturally UNKNOWN on reset and are set to zero
754 * except for field(s) listed below.
755 *
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500756 * CNTHCTL_EL2.EL1PTEN: Set to one to disable traps to
David Cunadofee86532017-04-13 22:38:29 +0100757 * Hyp mode of Non-secure EL0 and EL1 accesses to the
758 * physical timer registers.
759 *
760 * CNTHCTL_EL2.EL1PCTEN: Set to one to disable traps to
761 * Hyp mode of Non-secure EL0 and EL1 accesses to the
762 * physical counter registers.
763 */
764 write_cnthctl_el2(CNTHCTL_RESET_VAL |
765 EL1PCEN_BIT | EL1PCTEN_BIT);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100766
David Cunadofee86532017-04-13 22:38:29 +0100767 /*
768 * Initialise CNTVOFF_EL2 to zero as it resets to an
769 * architecturally UNKNOWN value.
770 */
Soby Mathewfeddfcf2014-08-29 14:41:58 +0100771 write_cntvoff_el2(0);
772
David Cunadofee86532017-04-13 22:38:29 +0100773 /*
774 * Set VPIDR_EL2 and VMPIDR_EL2 to match MIDR_EL1 and
775 * MPIDR_EL1 respectively.
776 */
Andrew Thoelke4e126072014-06-04 21:10:52 +0100777 write_vpidr_el2(read_midr_el1());
778 write_vmpidr_el2(read_mpidr_el1());
Sandrine Bailleux8b0eafe2015-11-25 17:00:44 +0000779
780 /*
David Cunadofee86532017-04-13 22:38:29 +0100781 * Initialise VTTBR_EL2. All fields are architecturally
782 * UNKNOWN on reset.
783 *
784 * VTTBR_EL2.VMID: Set to zero. Even though EL1&0 stage
785 * 2 address translation is disabled, cache maintenance
786 * operations depend on the VMID.
787 *
788 * VTTBR_EL2.BADDR: Set to zero as EL1&0 stage 2 address
789 * translation is disabled.
Sandrine Bailleux8b0eafe2015-11-25 17:00:44 +0000790 */
David Cunadofee86532017-04-13 22:38:29 +0100791 write_vttbr_el2(VTTBR_RESET_VAL &
792 ~((VTTBR_VMID_MASK << VTTBR_VMID_SHIFT)
793 | (VTTBR_BADDR_MASK << VTTBR_BADDR_SHIFT)));
794
David Cunado5f55e282016-10-31 17:37:34 +0000795 /*
David Cunadofee86532017-04-13 22:38:29 +0100796 * Initialise MDCR_EL2, setting all fields rather than
797 * relying on hw. Some fields are architecturally
798 * UNKNOWN on reset.
799 *
800 * MDCR_EL2.TDRA: Set to zero so that Non-secure EL0 and
801 * EL1 System register accesses to the Debug ROM
802 * registers are not trapped to EL2.
803 *
804 * MDCR_EL2.TDOSA: Set to zero so that Non-secure EL1
805 * System register accesses to the powerdown debug
806 * registers are not trapped to EL2.
807 *
808 * MDCR_EL2.TDA: Set to zero so that System register
809 * accesses to the debug registers do not trap to EL2.
810 *
811 * MDCR_EL2.TDE: Set to zero so that debug exceptions
812 * are not routed to EL2.
David Cunado5f55e282016-10-31 17:37:34 +0000813 */
Boyan Karatotev6468d4a2023-02-16 15:12:45 +0000814 mdcr_el2 = ((MDCR_EL2_RESET_VAL) &
815 ~(MDCR_EL2_TDRA_BIT | MDCR_EL2_TDOSA_BIT |
816 MDCR_EL2_TDA_BIT | MDCR_EL2_TDE_BIT));
dp-armee3457b2017-05-23 09:32:49 +0100817
dp-armee3457b2017-05-23 09:32:49 +0100818 write_mdcr_el2(mdcr_el2);
819
David Cunadoc14b08e2016-11-25 00:21:59 +0000820 /*
David Cunadofee86532017-04-13 22:38:29 +0100821 * Initialise HSTR_EL2. All fields are architecturally
822 * UNKNOWN on reset.
823 *
824 * HSTR_EL2.T<n>: Set all these fields to zero so that
825 * Non-secure EL0 or EL1 accesses to System registers
826 * do not trap to EL2.
David Cunadoc14b08e2016-11-25 00:21:59 +0000827 */
David Cunadofee86532017-04-13 22:38:29 +0100828 write_hstr_el2(HSTR_EL2_RESET_VAL & ~(HSTR_EL2_T_MASK));
David Cunadoc14b08e2016-11-25 00:21:59 +0000829 /*
David Cunadofee86532017-04-13 22:38:29 +0100830 * Initialise CNTHP_CTL_EL2. All fields are
831 * architecturally UNKNOWN on reset.
832 *
833 * CNTHP_CTL_EL2:ENABLE: Set to zero to disable the EL2
834 * physical timer and prevent timer interrupts.
David Cunadoc14b08e2016-11-25 00:21:59 +0000835 */
David Cunadofee86532017-04-13 22:38:29 +0100836 write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL &
837 ~(CNTHP_CTL_ENABLE_BIT));
Boyan Karatotev36cebf92023-03-08 11:56:49 +0000838
839 manage_extensions_nonsecure_el2_unused();
Andrew Thoelke4e126072014-06-04 21:10:52 +0100840 }
841 }
842
Dimitris Papastamosa7921b92017-10-13 15:27:58 +0100843 cm_el1_sysregs_context_restore(security_state);
844 cm_set_next_eret_context(security_state);
Andrew Thoelke4e126072014-06-04 21:10:52 +0100845}
846
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000847#if CTX_INCLUDE_EL2_REGS
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000848
849static void el2_sysregs_context_save_fgt(el2_sysregs_t *ctx)
850{
Andre Przywara8258f142023-02-15 15:56:15 +0000851 write_ctx_reg(ctx, CTX_HDFGRTR_EL2, read_hdfgrtr_el2());
852 if (is_feat_amu_supported()) {
853 write_ctx_reg(ctx, CTX_HAFGRTR_EL2, read_hafgrtr_el2());
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000854 }
Andre Przywara8258f142023-02-15 15:56:15 +0000855 write_ctx_reg(ctx, CTX_HDFGWTR_EL2, read_hdfgwtr_el2());
856 write_ctx_reg(ctx, CTX_HFGITR_EL2, read_hfgitr_el2());
857 write_ctx_reg(ctx, CTX_HFGRTR_EL2, read_hfgrtr_el2());
858 write_ctx_reg(ctx, CTX_HFGWTR_EL2, read_hfgwtr_el2());
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000859}
860
861static void el2_sysregs_context_restore_fgt(el2_sysregs_t *ctx)
862{
Andre Przywara8258f142023-02-15 15:56:15 +0000863 write_hdfgrtr_el2(read_ctx_reg(ctx, CTX_HDFGRTR_EL2));
864 if (is_feat_amu_supported()) {
865 write_hafgrtr_el2(read_ctx_reg(ctx, CTX_HAFGRTR_EL2));
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000866 }
Andre Przywara8258f142023-02-15 15:56:15 +0000867 write_hdfgwtr_el2(read_ctx_reg(ctx, CTX_HDFGWTR_EL2));
868 write_hfgitr_el2(read_ctx_reg(ctx, CTX_HFGITR_EL2));
869 write_hfgrtr_el2(read_ctx_reg(ctx, CTX_HFGRTR_EL2));
870 write_hfgwtr_el2(read_ctx_reg(ctx, CTX_HFGWTR_EL2));
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000871}
872
Andre Przywara84b86532022-11-17 16:42:09 +0000873static void el2_sysregs_context_save_mpam(el2_sysregs_t *ctx)
874{
875 u_register_t mpam_idr = read_mpamidr_el1();
876
877 write_ctx_reg(ctx, CTX_MPAM2_EL2, read_mpam2_el2());
878
879 /*
880 * The context registers that we intend to save would be part of the
881 * PE's system register frame only if MPAMIDR_EL1.HAS_HCR == 1.
882 */
883 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
884 return;
885 }
886
887 /*
888 * MPAMHCR_EL2, MPAMVPMV_EL2 and MPAMVPM0_EL2 are always present if
889 * MPAMIDR_HAS_HCR_BIT == 1.
890 */
891 write_ctx_reg(ctx, CTX_MPAMHCR_EL2, read_mpamhcr_el2());
892 write_ctx_reg(ctx, CTX_MPAMVPM0_EL2, read_mpamvpm0_el2());
893 write_ctx_reg(ctx, CTX_MPAMVPMV_EL2, read_mpamvpmv_el2());
894
895 /*
896 * The number of MPAMVPM registers is implementation defined, their
897 * number is stored in the MPAMIDR_EL1 register.
898 */
899 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
900 case 7:
901 write_ctx_reg(ctx, CTX_MPAMVPM7_EL2, read_mpamvpm7_el2());
902 __fallthrough;
903 case 6:
904 write_ctx_reg(ctx, CTX_MPAMVPM6_EL2, read_mpamvpm6_el2());
905 __fallthrough;
906 case 5:
907 write_ctx_reg(ctx, CTX_MPAMVPM5_EL2, read_mpamvpm5_el2());
908 __fallthrough;
909 case 4:
910 write_ctx_reg(ctx, CTX_MPAMVPM4_EL2, read_mpamvpm4_el2());
911 __fallthrough;
912 case 3:
913 write_ctx_reg(ctx, CTX_MPAMVPM3_EL2, read_mpamvpm3_el2());
914 __fallthrough;
915 case 2:
916 write_ctx_reg(ctx, CTX_MPAMVPM2_EL2, read_mpamvpm2_el2());
917 __fallthrough;
918 case 1:
919 write_ctx_reg(ctx, CTX_MPAMVPM1_EL2, read_mpamvpm1_el2());
920 break;
921 }
922}
923
924static void el2_sysregs_context_restore_mpam(el2_sysregs_t *ctx)
925{
926 u_register_t mpam_idr = read_mpamidr_el1();
927
928 write_mpam2_el2(read_ctx_reg(ctx, CTX_MPAM2_EL2));
929
930 if ((mpam_idr & MPAMIDR_HAS_HCR_BIT) == 0U) {
931 return;
932 }
933
934 write_mpamhcr_el2(read_ctx_reg(ctx, CTX_MPAMHCR_EL2));
935 write_mpamvpm0_el2(read_ctx_reg(ctx, CTX_MPAMVPM0_EL2));
936 write_mpamvpmv_el2(read_ctx_reg(ctx, CTX_MPAMVPMV_EL2));
937
938 switch ((mpam_idr >> MPAMIDR_EL1_VPMR_MAX_SHIFT) & MPAMIDR_EL1_VPMR_MAX_MASK) {
939 case 7:
940 write_mpamvpm7_el2(read_ctx_reg(ctx, CTX_MPAMVPM7_EL2));
941 __fallthrough;
942 case 6:
943 write_mpamvpm6_el2(read_ctx_reg(ctx, CTX_MPAMVPM6_EL2));
944 __fallthrough;
945 case 5:
946 write_mpamvpm5_el2(read_ctx_reg(ctx, CTX_MPAMVPM5_EL2));
947 __fallthrough;
948 case 4:
949 write_mpamvpm4_el2(read_ctx_reg(ctx, CTX_MPAMVPM4_EL2));
950 __fallthrough;
951 case 3:
952 write_mpamvpm3_el2(read_ctx_reg(ctx, CTX_MPAMVPM3_EL2));
953 __fallthrough;
954 case 2:
955 write_mpamvpm2_el2(read_ctx_reg(ctx, CTX_MPAMVPM2_EL2));
956 __fallthrough;
957 case 1:
958 write_mpamvpm1_el2(read_ctx_reg(ctx, CTX_MPAMVPM1_EL2));
959 break;
960 }
961}
962
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000963/*******************************************************************************
964 * Save EL2 sysreg context
965 ******************************************************************************/
966void cm_el2_sysregs_context_save(uint32_t security_state)
967{
968 u_register_t scr_el3 = read_scr();
969
970 /*
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500971 * Always save the non-secure and realm EL2 context, only save the
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000972 * S-EL2 context if S-EL2 is enabled.
973 */
Zelalem Awekeb6301e62021-07-09 17:54:30 -0500974 if ((security_state != SECURE) ||
Ruari Phipps4283ed12020-07-28 11:26:29 +0100975 ((security_state == SECURE) && ((scr_el3 & SCR_EEL2_BIT) != 0U))) {
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000976 cpu_context_t *ctx;
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500977 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsovbdf502d2020-02-25 13:56:19 +0000978
979 ctx = cm_get_context(security_state);
980 assert(ctx != NULL);
981
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500982 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
983
984 el2_sysregs_context_save_common(el2_sysregs_ctx);
Zelalem Aweke5362beb2022-04-04 17:42:48 -0500985#if CTX_INCLUDE_MTE_REGS
986 el2_sysregs_context_save_mte(el2_sysregs_ctx);
987#endif
Andre Przywara84b86532022-11-17 16:42:09 +0000988 if (is_feat_mpam_supported()) {
989 el2_sysregs_context_save_mpam(el2_sysregs_ctx);
990 }
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000991
Andre Przywara8258f142023-02-15 15:56:15 +0000992 if (is_feat_fgt_supported()) {
993 el2_sysregs_context_save_fgt(el2_sysregs_ctx);
994 }
Andre Przywara5d6d2ab2022-11-10 14:40:37 +0000995
Andre Przywarac3464182022-11-17 17:30:43 +0000996 if (is_feat_ecv_v2_supported()) {
997 write_ctx_reg(el2_sysregs_ctx, CTX_CNTPOFF_EL2,
998 read_cntpoff_el2());
999 }
1000
Andre Przywara98908b32022-11-17 16:42:09 +00001001 if (is_feat_vhe_supported()) {
1002 write_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2,
1003 read_contextidr_el2());
1004 write_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2,
1005 read_ttbr1_el2());
1006 }
Andre Przywara870627e2023-01-27 12:25:49 +00001007
1008 if (is_feat_ras_supported()) {
1009 write_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2,
1010 read_vdisr_el2());
1011 write_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2,
1012 read_vsesr_el2());
1013 }
Andre Przywaraedc449d2023-01-27 14:09:20 +00001014
1015 if (is_feat_nv2_supported()) {
1016 write_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2,
1017 read_vncr_el2());
1018 }
1019
Andre Przywara06ea44e2022-11-17 17:30:43 +00001020 if (is_feat_trf_supported()) {
1021 write_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2, read_trfcr_el2());
1022 }
Andre Przywara902c9022022-11-17 17:30:43 +00001023
1024 if (is_feat_csv2_2_supported()) {
1025 write_ctx_reg(el2_sysregs_ctx, CTX_SCXTNUM_EL2,
1026 read_scxtnum_el2());
1027 }
1028
Andre Przywara1d8795e2022-11-15 11:45:19 +00001029 if (is_feat_hcx_supported()) {
1030 write_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2, read_hcrx_el2());
1031 }
Mark Brownc37eee72023-03-14 20:13:03 +00001032 if (is_feat_tcr2_supported()) {
1033 write_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2, read_tcr2_el2());
1034 }
Mark Brown293a6612023-03-14 20:48:43 +00001035 if (is_feat_sxpie_supported()) {
1036 write_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2, read_pire0_el2());
1037 write_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2, read_pir_el2());
1038 }
1039 if (is_feat_s2pie_supported()) {
1040 write_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2, read_s2pir_el2());
1041 }
1042 if (is_feat_sxpoe_supported()) {
1043 write_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2, read_por_el2());
1044 }
Mark Brown326f2952023-03-14 21:33:04 +00001045 if (is_feat_gcs_supported()) {
1046 write_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2, read_gcspr_el2());
1047 write_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2, read_gcscr_el2());
1048 }
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001049 }
1050}
1051
1052/*******************************************************************************
1053 * Restore EL2 sysreg context
1054 ******************************************************************************/
1055void cm_el2_sysregs_context_restore(uint32_t security_state)
1056{
1057 u_register_t scr_el3 = read_scr();
1058
1059 /*
Zelalem Awekeb6301e62021-07-09 17:54:30 -05001060 * Always restore the non-secure and realm EL2 context, only restore the
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001061 * S-EL2 context if S-EL2 is enabled.
1062 */
Zelalem Awekeb6301e62021-07-09 17:54:30 -05001063 if ((security_state != SECURE) ||
Ruari Phipps4283ed12020-07-28 11:26:29 +01001064 ((security_state == SECURE) && ((scr_el3 & SCR_EEL2_BIT) != 0U))) {
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001065 cpu_context_t *ctx;
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001066 el2_sysregs_t *el2_sysregs_ctx;
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001067
1068 ctx = cm_get_context(security_state);
1069 assert(ctx != NULL);
1070
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001071 el2_sysregs_ctx = get_el2_sysregs_ctx(ctx);
1072
1073 el2_sysregs_context_restore_common(el2_sysregs_ctx);
Zelalem Aweke5362beb2022-04-04 17:42:48 -05001074#if CTX_INCLUDE_MTE_REGS
1075 el2_sysregs_context_restore_mte(el2_sysregs_ctx);
1076#endif
Andre Przywara84b86532022-11-17 16:42:09 +00001077 if (is_feat_mpam_supported()) {
1078 el2_sysregs_context_restore_mpam(el2_sysregs_ctx);
1079 }
Andre Przywara5d6d2ab2022-11-10 14:40:37 +00001080
Andre Przywara8258f142023-02-15 15:56:15 +00001081 if (is_feat_fgt_supported()) {
1082 el2_sysregs_context_restore_fgt(el2_sysregs_ctx);
1083 }
Andre Przywara5d6d2ab2022-11-10 14:40:37 +00001084
Andre Przywarac3464182022-11-17 17:30:43 +00001085 if (is_feat_ecv_v2_supported()) {
1086 write_cntpoff_el2(read_ctx_reg(el2_sysregs_ctx,
1087 CTX_CNTPOFF_EL2));
1088 }
1089
Andre Przywara98908b32022-11-17 16:42:09 +00001090 if (is_feat_vhe_supported()) {
1091 write_contextidr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_CONTEXTIDR_EL2));
1092 write_ttbr1_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TTBR1_EL2));
1093 }
Andre Przywara870627e2023-01-27 12:25:49 +00001094
1095 if (is_feat_ras_supported()) {
1096 write_vdisr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VDISR_EL2));
1097 write_vsesr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VSESR_EL2));
1098 }
Andre Przywaraedc449d2023-01-27 14:09:20 +00001099
1100 if (is_feat_nv2_supported()) {
1101 write_vncr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_VNCR_EL2));
1102 }
Andre Przywara06ea44e2022-11-17 17:30:43 +00001103 if (is_feat_trf_supported()) {
1104 write_trfcr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TRFCR_EL2));
1105 }
Andre Przywara902c9022022-11-17 17:30:43 +00001106
1107 if (is_feat_csv2_2_supported()) {
1108 write_scxtnum_el2(read_ctx_reg(el2_sysregs_ctx,
1109 CTX_SCXTNUM_EL2));
1110 }
1111
Andre Przywara1d8795e2022-11-15 11:45:19 +00001112 if (is_feat_hcx_supported()) {
1113 write_hcrx_el2(read_ctx_reg(el2_sysregs_ctx, CTX_HCRX_EL2));
1114 }
Mark Brownc37eee72023-03-14 20:13:03 +00001115 if (is_feat_tcr2_supported()) {
1116 write_tcr2_el2(read_ctx_reg(el2_sysregs_ctx, CTX_TCR2_EL2));
1117 }
Mark Brown293a6612023-03-14 20:48:43 +00001118 if (is_feat_sxpie_supported()) {
1119 write_pire0_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIRE0_EL2));
1120 write_pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_PIR_EL2));
1121 }
1122 if (is_feat_s2pie_supported()) {
1123 write_s2pir_el2(read_ctx_reg(el2_sysregs_ctx, CTX_S2PIR_EL2));
1124 }
1125 if (is_feat_sxpoe_supported()) {
1126 write_por_el2(read_ctx_reg(el2_sysregs_ctx, CTX_POR_EL2));
1127 }
Mark Brown326f2952023-03-14 21:33:04 +00001128 if (is_feat_gcs_supported()) {
1129 write_gcscr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSCR_EL2));
1130 write_gcspr_el2(read_ctx_reg(el2_sysregs_ctx, CTX_GCSPR_EL2));
1131 }
Max Shvetsovbdf502d2020-02-25 13:56:19 +00001132 }
1133}
1134#endif /* CTX_INCLUDE_EL2_REGS */
1135
Andrew Thoelke4e126072014-06-04 21:10:52 +01001136/*******************************************************************************
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06001137 * This function is used to exit to Non-secure world. If CTX_INCLUDE_EL2_REGS
1138 * is enabled, it restores EL1 and EL2 sysreg contexts instead of directly
1139 * updating EL1 and EL2 registers. Otherwise, it calls the generic
1140 * cm_prepare_el3_exit function.
1141 ******************************************************************************/
1142void cm_prepare_el3_exit_ns(void)
1143{
1144#if CTX_INCLUDE_EL2_REGS
Boyan Karatotev1e966f32023-03-27 17:02:43 +01001145#if ENABLE_ASSERTIONS
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06001146 cpu_context_t *ctx = cm_get_context(NON_SECURE);
1147 assert(ctx != NULL);
1148
Zelalem Aweke20126002022-04-08 16:48:05 -05001149 /* Assert that EL2 is used. */
Boyan Karatotev1e966f32023-03-27 17:02:43 +01001150 u_register_t scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
Zelalem Aweke20126002022-04-08 16:48:05 -05001151 assert(((scr_el3 & SCR_HCE_BIT) != 0UL) &&
1152 (el_implemented(2U) != EL_IMPL_NONE));
Boyan Karatotev1e966f32023-03-27 17:02:43 +01001153#endif /* ENABLE_ASSERTIONS */
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06001154
1155 /*
1156 * Set the NS bit to be able to access the ICC_SRE_EL2
1157 * register when restoring context.
1158 */
1159 write_scr_el3(read_scr_el3() | SCR_NS_BIT);
1160
Olivier Depreze4793dd2022-05-09 17:34:02 +02001161 /*
1162 * Ensure the NS bit change is committed before the EL2/EL1
1163 * state restoration.
1164 */
1165 isb();
1166
Zelalem Awekef92c0cb2022-01-31 16:59:42 -06001167 /* Restore EL2 and EL1 sysreg contexts */
1168 cm_el2_sysregs_context_restore(NON_SECURE);
1169 cm_el1_sysregs_context_restore(NON_SECURE);
1170 cm_set_next_eret_context(NON_SECURE);
1171#else
1172 cm_prepare_el3_exit(NON_SECURE);
1173#endif /* CTX_INCLUDE_EL2_REGS */
1174}
1175
1176/*******************************************************************************
Soby Mathew2ed46e92014-07-04 16:02:26 +01001177 * The next four functions are used by runtime services to save and restore
1178 * EL1 context on the 'cpu_context' structure for the specified security
Achin Gupta7aea9082014-02-01 07:51:28 +00001179 * state.
1180 ******************************************************************************/
Achin Gupta7aea9082014-02-01 07:51:28 +00001181void cm_el1_sysregs_context_save(uint32_t security_state)
1182{
Dan Handleye2712bc2014-04-10 15:37:22 +01001183 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001184
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001185 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001186 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001187
Max Shvetsovc9e2c922020-02-17 16:15:47 +00001188 el1_sysregs_context_save(get_el1_sysregs_ctx(ctx));
Dimitris Papastamosa7921b92017-10-13 15:27:58 +01001189
1190#if IMAGE_BL31
1191 if (security_state == SECURE)
1192 PUBLISH_EVENT(cm_exited_secure_world);
1193 else
1194 PUBLISH_EVENT(cm_exited_normal_world);
1195#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001196}
1197
1198void cm_el1_sysregs_context_restore(uint32_t security_state)
1199{
Dan Handleye2712bc2014-04-10 15:37:22 +01001200 cpu_context_t *ctx;
Achin Gupta7aea9082014-02-01 07:51:28 +00001201
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001202 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001203 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001204
Max Shvetsovc9e2c922020-02-17 16:15:47 +00001205 el1_sysregs_context_restore(get_el1_sysregs_ctx(ctx));
Dimitris Papastamosa7921b92017-10-13 15:27:58 +01001206
1207#if IMAGE_BL31
1208 if (security_state == SECURE)
1209 PUBLISH_EVENT(cm_entering_secure_world);
1210 else
1211 PUBLISH_EVENT(cm_entering_normal_world);
1212#endif
Achin Gupta7aea9082014-02-01 07:51:28 +00001213}
1214
1215/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +01001216 * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
1217 * given security state with the given entrypoint
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001218 ******************************************************************************/
Soby Mathewa0fedc42016-06-16 14:52:04 +01001219void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001220{
Dan Handleye2712bc2014-04-10 15:37:22 +01001221 cpu_context_t *ctx;
1222 el3_state_t *state;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001223
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001224 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001225 assert(ctx != NULL);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001226
Andrew Thoelke4e126072014-06-04 21:10:52 +01001227 /* Populate EL3 state so that ERET jumps to the correct entry */
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001228 state = get_el3state_ctx(ctx);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001229 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001230}
1231
1232/*******************************************************************************
Andrew Thoelke4e126072014-06-04 21:10:52 +01001233 * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
1234 * pertaining to the given security state
Achin Gupta607084e2014-02-09 18:24:19 +00001235 ******************************************************************************/
Andrew Thoelke4e126072014-06-04 21:10:52 +01001236void cm_set_elr_spsr_el3(uint32_t security_state,
Soby Mathewa0fedc42016-06-16 14:52:04 +01001237 uintptr_t entrypoint, uint32_t spsr)
Achin Gupta607084e2014-02-09 18:24:19 +00001238{
Dan Handleye2712bc2014-04-10 15:37:22 +01001239 cpu_context_t *ctx;
1240 el3_state_t *state;
Achin Gupta607084e2014-02-09 18:24:19 +00001241
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001242 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001243 assert(ctx != NULL);
Achin Gupta607084e2014-02-09 18:24:19 +00001244
1245 /* Populate EL3 state so that ERET jumps to the correct entry */
1246 state = get_el3state_ctx(ctx);
1247 write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
Andrew Thoelke4e126072014-06-04 21:10:52 +01001248 write_ctx_reg(state, CTX_SPSR_EL3, spsr);
Achin Gupta607084e2014-02-09 18:24:19 +00001249}
1250
1251/*******************************************************************************
Achin Gupta27b895e2014-05-04 18:38:28 +01001252 * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
1253 * pertaining to the given security state using the value and bit position
1254 * specified in the parameters. It preserves all other bits.
1255 ******************************************************************************/
1256void cm_write_scr_el3_bit(uint32_t security_state,
1257 uint32_t bit_pos,
1258 uint32_t value)
1259{
1260 cpu_context_t *ctx;
1261 el3_state_t *state;
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001262 u_register_t scr_el3;
Achin Gupta27b895e2014-05-04 18:38:28 +01001263
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001264 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001265 assert(ctx != NULL);
Achin Gupta27b895e2014-05-04 18:38:28 +01001266
1267 /* Ensure that the bit position is a valid one */
Jimmy Brissoned202072020-08-04 16:18:52 -05001268 assert(((1UL << bit_pos) & SCR_VALID_BIT_MASK) != 0U);
Achin Gupta27b895e2014-05-04 18:38:28 +01001269
1270 /* Ensure that the 'value' is only a bit wide */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001271 assert(value <= 1U);
Achin Gupta27b895e2014-05-04 18:38:28 +01001272
1273 /*
1274 * Get the SCR_EL3 value from the cpu context, clear the desired bit
1275 * and set it to its new value.
1276 */
1277 state = get_el3state_ctx(ctx);
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001278 scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
Jimmy Brissoned202072020-08-04 16:18:52 -05001279 scr_el3 &= ~(1UL << bit_pos);
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001280 scr_el3 |= (u_register_t)value << bit_pos;
Achin Gupta27b895e2014-05-04 18:38:28 +01001281 write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
1282}
1283
1284/*******************************************************************************
1285 * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
1286 * given security state.
1287 ******************************************************************************/
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001288u_register_t cm_get_scr_el3(uint32_t security_state)
Achin Gupta27b895e2014-05-04 18:38:28 +01001289{
1290 cpu_context_t *ctx;
1291 el3_state_t *state;
1292
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001293 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001294 assert(ctx != NULL);
Achin Gupta27b895e2014-05-04 18:38:28 +01001295
1296 /* Populate EL3 state so that ERET jumps to the correct entry */
1297 state = get_el3state_ctx(ctx);
Louis Mayencourt1c819c32020-01-24 13:30:28 +00001298 return read_ctx_reg(state, CTX_SCR_EL3);
Achin Gupta27b895e2014-05-04 18:38:28 +01001299}
1300
1301/*******************************************************************************
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001302 * This function is used to program the context that's used for exception
1303 * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
1304 * the required security state
Achin Gupta7aea9082014-02-01 07:51:28 +00001305 ******************************************************************************/
1306void cm_set_next_eret_context(uint32_t security_state)
1307{
Dan Handleye2712bc2014-04-10 15:37:22 +01001308 cpu_context_t *ctx;
Jeenu Viswambharancaa84932014-02-06 10:36:15 +00001309
Andrew Thoelkea2f65532014-05-14 17:09:32 +01001310 ctx = cm_get_context(security_state);
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +00001311 assert(ctx != NULL);
Achin Gupta7aea9082014-02-01 07:51:28 +00001312
Andrew Thoelke4e126072014-06-04 21:10:52 +01001313 cm_set_next_context(ctx);
Achin Gupta7aea9082014-02-01 07:51:28 +00001314}