xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context_mgmt.c (revision 32f0d3c6c3fb1fb9353ec0b82ddb099281b9328c)
1532ed618SSoby Mathew /*
2*32f0d3c6SDouglas Raillard  * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
3532ed618SSoby Mathew  *
4532ed618SSoby Mathew  * Redistribution and use in source and binary forms, with or without
5532ed618SSoby Mathew  * modification, are permitted provided that the following conditions are met:
6532ed618SSoby Mathew  *
7532ed618SSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8532ed618SSoby Mathew  * list of conditions and the following disclaimer.
9532ed618SSoby Mathew  *
10532ed618SSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11532ed618SSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12532ed618SSoby Mathew  * and/or other materials provided with the distribution.
13532ed618SSoby Mathew  *
14532ed618SSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15532ed618SSoby Mathew  * to endorse or promote products derived from this software without specific
16532ed618SSoby Mathew  * prior written permission.
17532ed618SSoby Mathew  *
18532ed618SSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19532ed618SSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20532ed618SSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21532ed618SSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22532ed618SSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23532ed618SSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24532ed618SSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25532ed618SSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26532ed618SSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27532ed618SSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28532ed618SSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29532ed618SSoby Mathew  */
30532ed618SSoby Mathew 
31532ed618SSoby Mathew #include <arch.h>
32532ed618SSoby Mathew #include <arch_helpers.h>
33532ed618SSoby Mathew #include <assert.h>
34532ed618SSoby Mathew #include <bl_common.h>
35532ed618SSoby Mathew #include <context.h>
36532ed618SSoby Mathew #include <context_mgmt.h>
37532ed618SSoby Mathew #include <interrupt_mgmt.h>
38532ed618SSoby Mathew #include <platform.h>
39532ed618SSoby Mathew #include <platform_def.h>
40532ed618SSoby Mathew #include <smcc_helpers.h>
41532ed618SSoby Mathew #include <string.h>
42*32f0d3c6SDouglas Raillard #include <utils.h>
43532ed618SSoby Mathew 
44532ed618SSoby Mathew 
45532ed618SSoby Mathew /*******************************************************************************
46532ed618SSoby Mathew  * Context management library initialisation routine. This library is used by
47532ed618SSoby Mathew  * runtime services to share pointers to 'cpu_context' structures for the secure
48532ed618SSoby Mathew  * and non-secure states. Management of the structures and their associated
49532ed618SSoby Mathew  * memory is not done by the context management library e.g. the PSCI service
50532ed618SSoby Mathew  * manages the cpu context used for entry from and exit to the non-secure state.
51532ed618SSoby Mathew  * The Secure payload dispatcher service manages the context(s) corresponding to
52532ed618SSoby Mathew  * the secure state. It also uses this library to get access to the non-secure
53532ed618SSoby Mathew  * state cpu context pointers.
54532ed618SSoby Mathew  * Lastly, this library provides the api to make SP_EL3 point to the cpu context
55532ed618SSoby Mathew  * which will used for programming an entry into a lower EL. The same context
56532ed618SSoby Mathew  * will used to save state upon exception entry from that EL.
57532ed618SSoby Mathew  ******************************************************************************/
58532ed618SSoby Mathew void cm_init(void)
59532ed618SSoby Mathew {
60532ed618SSoby Mathew 	/*
61532ed618SSoby Mathew 	 * The context management library has only global data to intialize, but
62532ed618SSoby Mathew 	 * that will be done when the BSS is zeroed out
63532ed618SSoby Mathew 	 */
64532ed618SSoby Mathew }
65532ed618SSoby Mathew 
66532ed618SSoby Mathew /*******************************************************************************
67532ed618SSoby Mathew  * The following function initializes the cpu_context 'ctx' for
68532ed618SSoby Mathew  * first use, and sets the initial entrypoint state as specified by the
69532ed618SSoby Mathew  * entry_point_info structure.
70532ed618SSoby Mathew  *
71532ed618SSoby Mathew  * The security state to initialize is determined by the SECURE attribute
72532ed618SSoby Mathew  * of the entry_point_info. The function returns a pointer to the initialized
73532ed618SSoby Mathew  * context and sets this as the next context to return to.
74532ed618SSoby Mathew  *
75532ed618SSoby Mathew  * The EE and ST attributes are used to configure the endianess and secure
76532ed618SSoby Mathew  * timer availability for the new execution context.
77532ed618SSoby Mathew  *
78532ed618SSoby Mathew  * To prepare the register state for entry call cm_prepare_el3_exit() and
79532ed618SSoby Mathew  * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to
80532ed618SSoby Mathew  * cm_e1_sysreg_context_restore().
81532ed618SSoby Mathew  ******************************************************************************/
82532ed618SSoby Mathew static void cm_init_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
83532ed618SSoby Mathew {
84532ed618SSoby Mathew 	unsigned int security_state;
85532ed618SSoby Mathew 	uint32_t scr_el3;
86532ed618SSoby Mathew 	el3_state_t *state;
87532ed618SSoby Mathew 	gp_regs_t *gp_regs;
88532ed618SSoby Mathew 	unsigned long sctlr_elx;
89532ed618SSoby Mathew 
90532ed618SSoby Mathew 	assert(ctx);
91532ed618SSoby Mathew 
92532ed618SSoby Mathew 	security_state = GET_SECURITY_STATE(ep->h.attr);
93532ed618SSoby Mathew 
94532ed618SSoby Mathew 	/* Clear any residual register values from the context */
95*32f0d3c6SDouglas Raillard 	zeromem(ctx, sizeof(*ctx));
96532ed618SSoby Mathew 
97532ed618SSoby Mathew 	/*
98532ed618SSoby Mathew 	 * Base the context SCR on the current value, adjust for entry point
99532ed618SSoby Mathew 	 * specific requirements and set trap bits from the IMF
100532ed618SSoby Mathew 	 * TODO: provide the base/global SCR bits using another mechanism?
101532ed618SSoby Mathew 	 */
102532ed618SSoby Mathew 	scr_el3 = read_scr();
103532ed618SSoby Mathew 	scr_el3 &= ~(SCR_NS_BIT | SCR_RW_BIT | SCR_FIQ_BIT | SCR_IRQ_BIT |
104532ed618SSoby Mathew 			SCR_ST_BIT | SCR_HCE_BIT);
105532ed618SSoby Mathew 
106532ed618SSoby Mathew 	if (security_state != SECURE)
107532ed618SSoby Mathew 		scr_el3 |= SCR_NS_BIT;
108532ed618SSoby Mathew 
109532ed618SSoby Mathew 	if (GET_RW(ep->spsr) == MODE_RW_64)
110532ed618SSoby Mathew 		scr_el3 |= SCR_RW_BIT;
111532ed618SSoby Mathew 
112532ed618SSoby Mathew 	if (EP_GET_ST(ep->h.attr))
113532ed618SSoby Mathew 		scr_el3 |= SCR_ST_BIT;
114532ed618SSoby Mathew 
115532ed618SSoby Mathew #ifndef HANDLE_EA_EL3_FIRST
116532ed618SSoby Mathew 	/* Explicitly stop to trap aborts from lower exception levels. */
117532ed618SSoby Mathew 	scr_el3 &= ~SCR_EA_BIT;
118532ed618SSoby Mathew #endif
119532ed618SSoby Mathew 
1203d8256b2SMasahiro Yamada #ifdef IMAGE_BL31
121532ed618SSoby Mathew 	/*
122532ed618SSoby Mathew 	 * IRQ/FIQ bits only need setting if interrupt routing
123532ed618SSoby Mathew 	 * model has been set up for BL31.
124532ed618SSoby Mathew 	 */
125532ed618SSoby Mathew 	scr_el3 |= get_scr_el3_from_routing_model(security_state);
126532ed618SSoby Mathew #endif
127532ed618SSoby Mathew 
128532ed618SSoby Mathew 	/*
129532ed618SSoby Mathew 	 * Set up SCTLR_ELx for the target exception level:
130532ed618SSoby Mathew 	 * EE bit is taken from the entrypoint attributes
131532ed618SSoby Mathew 	 * M, C and I bits must be zero (as required by PSCI specification)
132532ed618SSoby Mathew 	 *
133532ed618SSoby Mathew 	 * The target exception level is based on the spsr mode requested.
134532ed618SSoby Mathew 	 * If execution is requested to EL2 or hyp mode, HVC is enabled
135532ed618SSoby Mathew 	 * via SCR_EL3.HCE.
136532ed618SSoby Mathew 	 *
137532ed618SSoby Mathew 	 * Always compute the SCTLR_EL1 value and save in the cpu_context
138532ed618SSoby Mathew 	 * - the EL2 registers are set up by cm_preapre_ns_entry() as they
139532ed618SSoby Mathew 	 * are not part of the stored cpu_context
140532ed618SSoby Mathew 	 *
141532ed618SSoby Mathew 	 * TODO: In debug builds the spsr should be validated and checked
142532ed618SSoby Mathew 	 * against the CPU support, security state, endianess and pc
143532ed618SSoby Mathew 	 */
144532ed618SSoby Mathew 	sctlr_elx = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0;
145532ed618SSoby Mathew 	if (GET_RW(ep->spsr) == MODE_RW_64)
146532ed618SSoby Mathew 		sctlr_elx |= SCTLR_EL1_RES1;
147b7b0787dSSoby Mathew 	else {
148532ed618SSoby Mathew 		sctlr_elx |= SCTLR_AARCH32_EL1_RES1;
149b7b0787dSSoby Mathew 		/*
150b7b0787dSSoby Mathew 		 * If lower non-secure EL is AArch32, enable the CP15BEN, nTWI
151b7b0787dSSoby Mathew 		 * & nTWI bits. This aligns with SCTLR initialization on
152b7b0787dSSoby Mathew 		 * systems with an AArch32 EL3, where these bits
153b7b0787dSSoby Mathew 		 * architecturally reset to 1.
154b7b0787dSSoby Mathew 		 */
155b7b0787dSSoby Mathew 		if (security_state != SECURE)
156b7b0787dSSoby Mathew 			sctlr_elx |= SCTLR_CP15BEN_BIT | SCTLR_NTWI_BIT
157b7b0787dSSoby Mathew 						| SCTLR_NTWE_BIT;
158b7b0787dSSoby Mathew 	}
159b7b0787dSSoby Mathew 
160532ed618SSoby Mathew 	write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
161532ed618SSoby Mathew 
162532ed618SSoby Mathew 	if ((GET_RW(ep->spsr) == MODE_RW_64
163532ed618SSoby Mathew 	     && GET_EL(ep->spsr) == MODE_EL2)
164532ed618SSoby Mathew 	    || (GET_RW(ep->spsr) != MODE_RW_64
165532ed618SSoby Mathew 		&& GET_M32(ep->spsr) == MODE32_hyp)) {
166532ed618SSoby Mathew 		scr_el3 |= SCR_HCE_BIT;
167532ed618SSoby Mathew 	}
168532ed618SSoby Mathew 
169532ed618SSoby Mathew 	/* Populate EL3 state so that we've the right context before doing ERET */
170532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
171532ed618SSoby Mathew 	write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
172532ed618SSoby Mathew 	write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
173532ed618SSoby Mathew 	write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
174532ed618SSoby Mathew 
175532ed618SSoby Mathew 	/*
176532ed618SSoby Mathew 	 * Store the X0-X7 value from the entrypoint into the context
177532ed618SSoby Mathew 	 * Use memcpy as we are in control of the layout of the structures
178532ed618SSoby Mathew 	 */
179532ed618SSoby Mathew 	gp_regs = get_gpregs_ctx(ctx);
180532ed618SSoby Mathew 	memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
181532ed618SSoby Mathew }
182532ed618SSoby Mathew 
183532ed618SSoby Mathew /*******************************************************************************
184532ed618SSoby Mathew  * The following function initializes the cpu_context for a CPU specified by
185532ed618SSoby Mathew  * its `cpu_idx` for first use, and sets the initial entrypoint state as
186532ed618SSoby Mathew  * specified by the entry_point_info structure.
187532ed618SSoby Mathew  ******************************************************************************/
188532ed618SSoby Mathew void cm_init_context_by_index(unsigned int cpu_idx,
189532ed618SSoby Mathew 			      const entry_point_info_t *ep)
190532ed618SSoby Mathew {
191532ed618SSoby Mathew 	cpu_context_t *ctx;
192532ed618SSoby Mathew 	ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
193532ed618SSoby Mathew 	cm_init_context_common(ctx, ep);
194532ed618SSoby Mathew }
195532ed618SSoby Mathew 
196532ed618SSoby Mathew /*******************************************************************************
197532ed618SSoby Mathew  * The following function initializes the cpu_context for the current CPU
198532ed618SSoby Mathew  * for first use, and sets the initial entrypoint state as specified by the
199532ed618SSoby Mathew  * entry_point_info structure.
200532ed618SSoby Mathew  ******************************************************************************/
201532ed618SSoby Mathew void cm_init_my_context(const entry_point_info_t *ep)
202532ed618SSoby Mathew {
203532ed618SSoby Mathew 	cpu_context_t *ctx;
204532ed618SSoby Mathew 	ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
205532ed618SSoby Mathew 	cm_init_context_common(ctx, ep);
206532ed618SSoby Mathew }
207532ed618SSoby Mathew 
208532ed618SSoby Mathew /*******************************************************************************
209532ed618SSoby Mathew  * Prepare the CPU system registers for first entry into secure or normal world
210532ed618SSoby Mathew  *
211532ed618SSoby Mathew  * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
212532ed618SSoby Mathew  * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
213532ed618SSoby Mathew  * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
214532ed618SSoby Mathew  * For all entries, the EL1 registers are initialized from the cpu_context
215532ed618SSoby Mathew  ******************************************************************************/
216532ed618SSoby Mathew void cm_prepare_el3_exit(uint32_t security_state)
217532ed618SSoby Mathew {
218532ed618SSoby Mathew 	uint32_t sctlr_elx, scr_el3, cptr_el2;
219532ed618SSoby Mathew 	cpu_context_t *ctx = cm_get_context(security_state);
220532ed618SSoby Mathew 
221532ed618SSoby Mathew 	assert(ctx);
222532ed618SSoby Mathew 
223532ed618SSoby Mathew 	if (security_state == NON_SECURE) {
224532ed618SSoby Mathew 		scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
225532ed618SSoby Mathew 		if (scr_el3 & SCR_HCE_BIT) {
226532ed618SSoby Mathew 			/* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
227532ed618SSoby Mathew 			sctlr_elx = read_ctx_reg(get_sysregs_ctx(ctx),
228532ed618SSoby Mathew 						 CTX_SCTLR_EL1);
229532ed618SSoby Mathew 			sctlr_elx &= ~SCTLR_EE_BIT;
230532ed618SSoby Mathew 			sctlr_elx |= SCTLR_EL2_RES1;
231532ed618SSoby Mathew 			write_sctlr_el2(sctlr_elx);
232532ed618SSoby Mathew 		} else if (read_id_aa64pfr0_el1() &
233532ed618SSoby Mathew 			   (ID_AA64PFR0_ELX_MASK << ID_AA64PFR0_EL2_SHIFT)) {
234532ed618SSoby Mathew 			/* EL2 present but unused, need to disable safely */
235532ed618SSoby Mathew 
236532ed618SSoby Mathew 			/* HCR_EL2 = 0, except RW bit set to match SCR_EL3 */
237532ed618SSoby Mathew 			write_hcr_el2((scr_el3 & SCR_RW_BIT) ? HCR_RW_BIT : 0);
238532ed618SSoby Mathew 
239532ed618SSoby Mathew 			/* SCTLR_EL2 : can be ignored when bypassing */
240532ed618SSoby Mathew 
241532ed618SSoby Mathew 			/* CPTR_EL2 : disable all traps TCPAC, TTA, TFP */
242532ed618SSoby Mathew 			cptr_el2 = read_cptr_el2();
243532ed618SSoby Mathew 			cptr_el2 &= ~(TCPAC_BIT | TTA_BIT | TFP_BIT);
244532ed618SSoby Mathew 			write_cptr_el2(cptr_el2);
245532ed618SSoby Mathew 
246532ed618SSoby Mathew 			/* Enable EL1 access to timer */
247532ed618SSoby Mathew 			write_cnthctl_el2(EL1PCEN_BIT | EL1PCTEN_BIT);
248532ed618SSoby Mathew 
249532ed618SSoby Mathew 			/* Reset CNTVOFF_EL2 */
250532ed618SSoby Mathew 			write_cntvoff_el2(0);
251532ed618SSoby Mathew 
252532ed618SSoby Mathew 			/* Set VPIDR, VMPIDR to match MIDR, MPIDR */
253532ed618SSoby Mathew 			write_vpidr_el2(read_midr_el1());
254532ed618SSoby Mathew 			write_vmpidr_el2(read_mpidr_el1());
255532ed618SSoby Mathew 
256532ed618SSoby Mathew 			/*
257532ed618SSoby Mathew 			 * Reset VTTBR_EL2.
258532ed618SSoby Mathew 			 * Needed because cache maintenance operations depend on
259532ed618SSoby Mathew 			 * the VMID even when non-secure EL1&0 stage 2 address
260532ed618SSoby Mathew 			 * translation are disabled.
261532ed618SSoby Mathew 			 */
262532ed618SSoby Mathew 			write_vttbr_el2(0);
263495f3d3cSDavid Cunado 			/*
264495f3d3cSDavid Cunado 			 * Avoid unexpected debug traps in case where MDCR_EL2
265495f3d3cSDavid Cunado 			 * is not completely reset by the hardware - set
266495f3d3cSDavid Cunado 			 * MDCR_EL2.HPMN to PMCR_EL0.N and zero the remaining
267495f3d3cSDavid Cunado 			 * bits.
268495f3d3cSDavid Cunado 			 * MDCR_EL2.HPMN and PMCR_EL0.N fields are the same size
269495f3d3cSDavid Cunado 			 * (5 bits) and HPMN is at offset zero within MDCR_EL2.
270495f3d3cSDavid Cunado 			 */
271495f3d3cSDavid Cunado 			write_mdcr_el2((read_pmcr_el0() & PMCR_EL0_N_BITS)
272495f3d3cSDavid Cunado 					>> PMCR_EL0_N_SHIFT);
273939f66d6SDavid Cunado 			/*
274939f66d6SDavid Cunado 			 * Avoid unexpected traps of non-secure access to
275939f66d6SDavid Cunado 			 * certain system registers at EL1 or lower where
276939f66d6SDavid Cunado 			 * HSTR_EL2 is not completely reset to zero by the
277939f66d6SDavid Cunado 			 * hardware - zero the entire register.
278939f66d6SDavid Cunado 			 */
279939f66d6SDavid Cunado 			write_hstr_el2(0);
280939f66d6SDavid Cunado 			/*
281939f66d6SDavid Cunado 			 * Reset CNTHP_CTL_EL2 to disable the EL2 physical timer
282939f66d6SDavid Cunado 			 * and therefore prevent timer interrupts.
283939f66d6SDavid Cunado 			 */
284939f66d6SDavid Cunado 			write_cnthp_ctl_el2(0);
285532ed618SSoby Mathew 		}
286532ed618SSoby Mathew 	}
287532ed618SSoby Mathew 
288532ed618SSoby Mathew 	el1_sysregs_context_restore(get_sysregs_ctx(ctx));
289532ed618SSoby Mathew 
290532ed618SSoby Mathew 	cm_set_next_context(ctx);
291532ed618SSoby Mathew }
292532ed618SSoby Mathew 
293532ed618SSoby Mathew /*******************************************************************************
294532ed618SSoby Mathew  * The next four functions are used by runtime services to save and restore
295532ed618SSoby Mathew  * EL1 context on the 'cpu_context' structure for the specified security
296532ed618SSoby Mathew  * state.
297532ed618SSoby Mathew  ******************************************************************************/
298532ed618SSoby Mathew void cm_el1_sysregs_context_save(uint32_t security_state)
299532ed618SSoby Mathew {
300532ed618SSoby Mathew 	cpu_context_t *ctx;
301532ed618SSoby Mathew 
302532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
303532ed618SSoby Mathew 	assert(ctx);
304532ed618SSoby Mathew 
305532ed618SSoby Mathew 	el1_sysregs_context_save(get_sysregs_ctx(ctx));
306532ed618SSoby Mathew }
307532ed618SSoby Mathew 
308532ed618SSoby Mathew void cm_el1_sysregs_context_restore(uint32_t security_state)
309532ed618SSoby Mathew {
310532ed618SSoby Mathew 	cpu_context_t *ctx;
311532ed618SSoby Mathew 
312532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
313532ed618SSoby Mathew 	assert(ctx);
314532ed618SSoby Mathew 
315532ed618SSoby Mathew 	el1_sysregs_context_restore(get_sysregs_ctx(ctx));
316532ed618SSoby Mathew }
317532ed618SSoby Mathew 
318532ed618SSoby Mathew /*******************************************************************************
319532ed618SSoby Mathew  * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
320532ed618SSoby Mathew  * given security state with the given entrypoint
321532ed618SSoby Mathew  ******************************************************************************/
322532ed618SSoby Mathew void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
323532ed618SSoby Mathew {
324532ed618SSoby Mathew 	cpu_context_t *ctx;
325532ed618SSoby Mathew 	el3_state_t *state;
326532ed618SSoby Mathew 
327532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
328532ed618SSoby Mathew 	assert(ctx);
329532ed618SSoby Mathew 
330532ed618SSoby Mathew 	/* Populate EL3 state so that ERET jumps to the correct entry */
331532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
332532ed618SSoby Mathew 	write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
333532ed618SSoby Mathew }
334532ed618SSoby Mathew 
335532ed618SSoby Mathew /*******************************************************************************
336532ed618SSoby Mathew  * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
337532ed618SSoby Mathew  * pertaining to the given security state
338532ed618SSoby Mathew  ******************************************************************************/
339532ed618SSoby Mathew void cm_set_elr_spsr_el3(uint32_t security_state,
340532ed618SSoby Mathew 			uintptr_t entrypoint, uint32_t spsr)
341532ed618SSoby Mathew {
342532ed618SSoby Mathew 	cpu_context_t *ctx;
343532ed618SSoby Mathew 	el3_state_t *state;
344532ed618SSoby Mathew 
345532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
346532ed618SSoby Mathew 	assert(ctx);
347532ed618SSoby Mathew 
348532ed618SSoby Mathew 	/* Populate EL3 state so that ERET jumps to the correct entry */
349532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
350532ed618SSoby Mathew 	write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
351532ed618SSoby Mathew 	write_ctx_reg(state, CTX_SPSR_EL3, spsr);
352532ed618SSoby Mathew }
353532ed618SSoby Mathew 
354532ed618SSoby Mathew /*******************************************************************************
355532ed618SSoby Mathew  * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
356532ed618SSoby Mathew  * pertaining to the given security state using the value and bit position
357532ed618SSoby Mathew  * specified in the parameters. It preserves all other bits.
358532ed618SSoby Mathew  ******************************************************************************/
359532ed618SSoby Mathew void cm_write_scr_el3_bit(uint32_t security_state,
360532ed618SSoby Mathew 			  uint32_t bit_pos,
361532ed618SSoby Mathew 			  uint32_t value)
362532ed618SSoby Mathew {
363532ed618SSoby Mathew 	cpu_context_t *ctx;
364532ed618SSoby Mathew 	el3_state_t *state;
365532ed618SSoby Mathew 	uint32_t scr_el3;
366532ed618SSoby Mathew 
367532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
368532ed618SSoby Mathew 	assert(ctx);
369532ed618SSoby Mathew 
370532ed618SSoby Mathew 	/* Ensure that the bit position is a valid one */
371532ed618SSoby Mathew 	assert((1 << bit_pos) & SCR_VALID_BIT_MASK);
372532ed618SSoby Mathew 
373532ed618SSoby Mathew 	/* Ensure that the 'value' is only a bit wide */
374532ed618SSoby Mathew 	assert(value <= 1);
375532ed618SSoby Mathew 
376532ed618SSoby Mathew 	/*
377532ed618SSoby Mathew 	 * Get the SCR_EL3 value from the cpu context, clear the desired bit
378532ed618SSoby Mathew 	 * and set it to its new value.
379532ed618SSoby Mathew 	 */
380532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
381532ed618SSoby Mathew 	scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
382532ed618SSoby Mathew 	scr_el3 &= ~(1 << bit_pos);
383532ed618SSoby Mathew 	scr_el3 |= value << bit_pos;
384532ed618SSoby Mathew 	write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
385532ed618SSoby Mathew }
386532ed618SSoby Mathew 
387532ed618SSoby Mathew /*******************************************************************************
388532ed618SSoby Mathew  * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
389532ed618SSoby Mathew  * given security state.
390532ed618SSoby Mathew  ******************************************************************************/
391532ed618SSoby Mathew uint32_t cm_get_scr_el3(uint32_t security_state)
392532ed618SSoby Mathew {
393532ed618SSoby Mathew 	cpu_context_t *ctx;
394532ed618SSoby Mathew 	el3_state_t *state;
395532ed618SSoby Mathew 
396532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
397532ed618SSoby Mathew 	assert(ctx);
398532ed618SSoby Mathew 
399532ed618SSoby Mathew 	/* Populate EL3 state so that ERET jumps to the correct entry */
400532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
401532ed618SSoby Mathew 	return read_ctx_reg(state, CTX_SCR_EL3);
402532ed618SSoby Mathew }
403532ed618SSoby Mathew 
404532ed618SSoby Mathew /*******************************************************************************
405532ed618SSoby Mathew  * This function is used to program the context that's used for exception
406532ed618SSoby Mathew  * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
407532ed618SSoby Mathew  * the required security state
408532ed618SSoby Mathew  ******************************************************************************/
409532ed618SSoby Mathew void cm_set_next_eret_context(uint32_t security_state)
410532ed618SSoby Mathew {
411532ed618SSoby Mathew 	cpu_context_t *ctx;
412532ed618SSoby Mathew 
413532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
414532ed618SSoby Mathew 	assert(ctx);
415532ed618SSoby Mathew 
416532ed618SSoby Mathew 	cm_set_next_context(ctx);
417532ed618SSoby Mathew }
418