xref: /rk3399_ARM-atf/lib/el3_runtime/aarch64/context_mgmt.c (revision 939f66d6c46a8fe8cac708ac8e52afea3ff7a095)
1532ed618SSoby Mathew /*
2532ed618SSoby Mathew  * Copyright (c) 2013-2016, 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>
42532ed618SSoby Mathew 
43532ed618SSoby Mathew 
44532ed618SSoby Mathew /*******************************************************************************
45532ed618SSoby Mathew  * Context management library initialisation routine. This library is used by
46532ed618SSoby Mathew  * runtime services to share pointers to 'cpu_context' structures for the secure
47532ed618SSoby Mathew  * and non-secure states. Management of the structures and their associated
48532ed618SSoby Mathew  * memory is not done by the context management library e.g. the PSCI service
49532ed618SSoby Mathew  * manages the cpu context used for entry from and exit to the non-secure state.
50532ed618SSoby Mathew  * The Secure payload dispatcher service manages the context(s) corresponding to
51532ed618SSoby Mathew  * the secure state. It also uses this library to get access to the non-secure
52532ed618SSoby Mathew  * state cpu context pointers.
53532ed618SSoby Mathew  * Lastly, this library provides the api to make SP_EL3 point to the cpu context
54532ed618SSoby Mathew  * which will used for programming an entry into a lower EL. The same context
55532ed618SSoby Mathew  * will used to save state upon exception entry from that EL.
56532ed618SSoby Mathew  ******************************************************************************/
57532ed618SSoby Mathew void cm_init(void)
58532ed618SSoby Mathew {
59532ed618SSoby Mathew 	/*
60532ed618SSoby Mathew 	 * The context management library has only global data to intialize, but
61532ed618SSoby Mathew 	 * that will be done when the BSS is zeroed out
62532ed618SSoby Mathew 	 */
63532ed618SSoby Mathew }
64532ed618SSoby Mathew 
65532ed618SSoby Mathew /*******************************************************************************
66532ed618SSoby Mathew  * The following function initializes the cpu_context 'ctx' for
67532ed618SSoby Mathew  * first use, and sets the initial entrypoint state as specified by the
68532ed618SSoby Mathew  * entry_point_info structure.
69532ed618SSoby Mathew  *
70532ed618SSoby Mathew  * The security state to initialize is determined by the SECURE attribute
71532ed618SSoby Mathew  * of the entry_point_info. The function returns a pointer to the initialized
72532ed618SSoby Mathew  * context and sets this as the next context to return to.
73532ed618SSoby Mathew  *
74532ed618SSoby Mathew  * The EE and ST attributes are used to configure the endianess and secure
75532ed618SSoby Mathew  * timer availability for the new execution context.
76532ed618SSoby Mathew  *
77532ed618SSoby Mathew  * To prepare the register state for entry call cm_prepare_el3_exit() and
78532ed618SSoby Mathew  * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to
79532ed618SSoby Mathew  * cm_e1_sysreg_context_restore().
80532ed618SSoby Mathew  ******************************************************************************/
81532ed618SSoby Mathew static void cm_init_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
82532ed618SSoby Mathew {
83532ed618SSoby Mathew 	unsigned int security_state;
84532ed618SSoby Mathew 	uint32_t scr_el3;
85532ed618SSoby Mathew 	el3_state_t *state;
86532ed618SSoby Mathew 	gp_regs_t *gp_regs;
87532ed618SSoby Mathew 	unsigned long sctlr_elx;
88532ed618SSoby Mathew 
89532ed618SSoby Mathew 	assert(ctx);
90532ed618SSoby Mathew 
91532ed618SSoby Mathew 	security_state = GET_SECURITY_STATE(ep->h.attr);
92532ed618SSoby Mathew 
93532ed618SSoby Mathew 	/* Clear any residual register values from the context */
94532ed618SSoby Mathew 	memset(ctx, 0, sizeof(*ctx));
95532ed618SSoby Mathew 
96532ed618SSoby Mathew 	/*
97532ed618SSoby Mathew 	 * Base the context SCR on the current value, adjust for entry point
98532ed618SSoby Mathew 	 * specific requirements and set trap bits from the IMF
99532ed618SSoby Mathew 	 * TODO: provide the base/global SCR bits using another mechanism?
100532ed618SSoby Mathew 	 */
101532ed618SSoby Mathew 	scr_el3 = read_scr();
102532ed618SSoby Mathew 	scr_el3 &= ~(SCR_NS_BIT | SCR_RW_BIT | SCR_FIQ_BIT | SCR_IRQ_BIT |
103532ed618SSoby Mathew 			SCR_ST_BIT | SCR_HCE_BIT);
104532ed618SSoby Mathew 
105532ed618SSoby Mathew 	if (security_state != SECURE)
106532ed618SSoby Mathew 		scr_el3 |= SCR_NS_BIT;
107532ed618SSoby Mathew 
108532ed618SSoby Mathew 	if (GET_RW(ep->spsr) == MODE_RW_64)
109532ed618SSoby Mathew 		scr_el3 |= SCR_RW_BIT;
110532ed618SSoby Mathew 
111532ed618SSoby Mathew 	if (EP_GET_ST(ep->h.attr))
112532ed618SSoby Mathew 		scr_el3 |= SCR_ST_BIT;
113532ed618SSoby Mathew 
114532ed618SSoby Mathew #ifndef HANDLE_EA_EL3_FIRST
115532ed618SSoby Mathew 	/* Explicitly stop to trap aborts from lower exception levels. */
116532ed618SSoby Mathew 	scr_el3 &= ~SCR_EA_BIT;
117532ed618SSoby Mathew #endif
118532ed618SSoby Mathew 
119532ed618SSoby Mathew #if IMAGE_BL31
120532ed618SSoby Mathew 	/*
121532ed618SSoby Mathew 	 * IRQ/FIQ bits only need setting if interrupt routing
122532ed618SSoby Mathew 	 * model has been set up for BL31.
123532ed618SSoby Mathew 	 */
124532ed618SSoby Mathew 	scr_el3 |= get_scr_el3_from_routing_model(security_state);
125532ed618SSoby Mathew #endif
126532ed618SSoby Mathew 
127532ed618SSoby Mathew 	/*
128532ed618SSoby Mathew 	 * Set up SCTLR_ELx for the target exception level:
129532ed618SSoby Mathew 	 * EE bit is taken from the entrypoint attributes
130532ed618SSoby Mathew 	 * M, C and I bits must be zero (as required by PSCI specification)
131532ed618SSoby Mathew 	 *
132532ed618SSoby Mathew 	 * The target exception level is based on the spsr mode requested.
133532ed618SSoby Mathew 	 * If execution is requested to EL2 or hyp mode, HVC is enabled
134532ed618SSoby Mathew 	 * via SCR_EL3.HCE.
135532ed618SSoby Mathew 	 *
136532ed618SSoby Mathew 	 * Always compute the SCTLR_EL1 value and save in the cpu_context
137532ed618SSoby Mathew 	 * - the EL2 registers are set up by cm_preapre_ns_entry() as they
138532ed618SSoby Mathew 	 * are not part of the stored cpu_context
139532ed618SSoby Mathew 	 *
140532ed618SSoby Mathew 	 * TODO: In debug builds the spsr should be validated and checked
141532ed618SSoby Mathew 	 * against the CPU support, security state, endianess and pc
142532ed618SSoby Mathew 	 */
143532ed618SSoby Mathew 	sctlr_elx = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0;
144532ed618SSoby Mathew 	if (GET_RW(ep->spsr) == MODE_RW_64)
145532ed618SSoby Mathew 		sctlr_elx |= SCTLR_EL1_RES1;
146b7b0787dSSoby Mathew 	else {
147532ed618SSoby Mathew 		sctlr_elx |= SCTLR_AARCH32_EL1_RES1;
148b7b0787dSSoby Mathew 		/*
149b7b0787dSSoby Mathew 		 * If lower non-secure EL is AArch32, enable the CP15BEN, nTWI
150b7b0787dSSoby Mathew 		 * & nTWI bits. This aligns with SCTLR initialization on
151b7b0787dSSoby Mathew 		 * systems with an AArch32 EL3, where these bits
152b7b0787dSSoby Mathew 		 * architecturally reset to 1.
153b7b0787dSSoby Mathew 		 */
154b7b0787dSSoby Mathew 		if (security_state != SECURE)
155b7b0787dSSoby Mathew 			sctlr_elx |= SCTLR_CP15BEN_BIT | SCTLR_NTWI_BIT
156b7b0787dSSoby Mathew 						| SCTLR_NTWE_BIT;
157b7b0787dSSoby Mathew 	}
158b7b0787dSSoby Mathew 
159532ed618SSoby Mathew 	write_ctx_reg(get_sysregs_ctx(ctx), CTX_SCTLR_EL1, sctlr_elx);
160532ed618SSoby Mathew 
161532ed618SSoby Mathew 	if ((GET_RW(ep->spsr) == MODE_RW_64
162532ed618SSoby Mathew 	     && GET_EL(ep->spsr) == MODE_EL2)
163532ed618SSoby Mathew 	    || (GET_RW(ep->spsr) != MODE_RW_64
164532ed618SSoby Mathew 		&& GET_M32(ep->spsr) == MODE32_hyp)) {
165532ed618SSoby Mathew 		scr_el3 |= SCR_HCE_BIT;
166532ed618SSoby Mathew 	}
167532ed618SSoby Mathew 
168532ed618SSoby Mathew 	/* Populate EL3 state so that we've the right context before doing ERET */
169532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
170532ed618SSoby Mathew 	write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
171532ed618SSoby Mathew 	write_ctx_reg(state, CTX_ELR_EL3, ep->pc);
172532ed618SSoby Mathew 	write_ctx_reg(state, CTX_SPSR_EL3, ep->spsr);
173532ed618SSoby Mathew 
174532ed618SSoby Mathew 	/*
175532ed618SSoby Mathew 	 * Store the X0-X7 value from the entrypoint into the context
176532ed618SSoby Mathew 	 * Use memcpy as we are in control of the layout of the structures
177532ed618SSoby Mathew 	 */
178532ed618SSoby Mathew 	gp_regs = get_gpregs_ctx(ctx);
179532ed618SSoby Mathew 	memcpy(gp_regs, (void *)&ep->args, sizeof(aapcs64_params_t));
180532ed618SSoby Mathew }
181532ed618SSoby Mathew 
182532ed618SSoby Mathew /*******************************************************************************
183532ed618SSoby Mathew  * The following function initializes the cpu_context for a CPU specified by
184532ed618SSoby Mathew  * its `cpu_idx` for first use, and sets the initial entrypoint state as
185532ed618SSoby Mathew  * specified by the entry_point_info structure.
186532ed618SSoby Mathew  ******************************************************************************/
187532ed618SSoby Mathew void cm_init_context_by_index(unsigned int cpu_idx,
188532ed618SSoby Mathew 			      const entry_point_info_t *ep)
189532ed618SSoby Mathew {
190532ed618SSoby Mathew 	cpu_context_t *ctx;
191532ed618SSoby Mathew 	ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
192532ed618SSoby Mathew 	cm_init_context_common(ctx, ep);
193532ed618SSoby Mathew }
194532ed618SSoby Mathew 
195532ed618SSoby Mathew /*******************************************************************************
196532ed618SSoby Mathew  * The following function initializes the cpu_context for the current CPU
197532ed618SSoby Mathew  * for first use, and sets the initial entrypoint state as specified by the
198532ed618SSoby Mathew  * entry_point_info structure.
199532ed618SSoby Mathew  ******************************************************************************/
200532ed618SSoby Mathew void cm_init_my_context(const entry_point_info_t *ep)
201532ed618SSoby Mathew {
202532ed618SSoby Mathew 	cpu_context_t *ctx;
203532ed618SSoby Mathew 	ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
204532ed618SSoby Mathew 	cm_init_context_common(ctx, ep);
205532ed618SSoby Mathew }
206532ed618SSoby Mathew 
207532ed618SSoby Mathew /*******************************************************************************
208532ed618SSoby Mathew  * Prepare the CPU system registers for first entry into secure or normal world
209532ed618SSoby Mathew  *
210532ed618SSoby Mathew  * If execution is requested to EL2 or hyp mode, SCTLR_EL2 is initialized
211532ed618SSoby Mathew  * If execution is requested to non-secure EL1 or svc mode, and the CPU supports
212532ed618SSoby Mathew  * EL2 then EL2 is disabled by configuring all necessary EL2 registers.
213532ed618SSoby Mathew  * For all entries, the EL1 registers are initialized from the cpu_context
214532ed618SSoby Mathew  ******************************************************************************/
215532ed618SSoby Mathew void cm_prepare_el3_exit(uint32_t security_state)
216532ed618SSoby Mathew {
217532ed618SSoby Mathew 	uint32_t sctlr_elx, scr_el3, cptr_el2;
218532ed618SSoby Mathew 	cpu_context_t *ctx = cm_get_context(security_state);
219532ed618SSoby Mathew 
220532ed618SSoby Mathew 	assert(ctx);
221532ed618SSoby Mathew 
222532ed618SSoby Mathew 	if (security_state == NON_SECURE) {
223532ed618SSoby Mathew 		scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3);
224532ed618SSoby Mathew 		if (scr_el3 & SCR_HCE_BIT) {
225532ed618SSoby Mathew 			/* Use SCTLR_EL1.EE value to initialise sctlr_el2 */
226532ed618SSoby Mathew 			sctlr_elx = read_ctx_reg(get_sysregs_ctx(ctx),
227532ed618SSoby Mathew 						 CTX_SCTLR_EL1);
228532ed618SSoby Mathew 			sctlr_elx &= ~SCTLR_EE_BIT;
229532ed618SSoby Mathew 			sctlr_elx |= SCTLR_EL2_RES1;
230532ed618SSoby Mathew 			write_sctlr_el2(sctlr_elx);
231532ed618SSoby Mathew 		} else if (read_id_aa64pfr0_el1() &
232532ed618SSoby Mathew 			   (ID_AA64PFR0_ELX_MASK << ID_AA64PFR0_EL2_SHIFT)) {
233532ed618SSoby Mathew 			/* EL2 present but unused, need to disable safely */
234532ed618SSoby Mathew 
235532ed618SSoby Mathew 			/* HCR_EL2 = 0, except RW bit set to match SCR_EL3 */
236532ed618SSoby Mathew 			write_hcr_el2((scr_el3 & SCR_RW_BIT) ? HCR_RW_BIT : 0);
237532ed618SSoby Mathew 
238532ed618SSoby Mathew 			/* SCTLR_EL2 : can be ignored when bypassing */
239532ed618SSoby Mathew 
240532ed618SSoby Mathew 			/* CPTR_EL2 : disable all traps TCPAC, TTA, TFP */
241532ed618SSoby Mathew 			cptr_el2 = read_cptr_el2();
242532ed618SSoby Mathew 			cptr_el2 &= ~(TCPAC_BIT | TTA_BIT | TFP_BIT);
243532ed618SSoby Mathew 			write_cptr_el2(cptr_el2);
244532ed618SSoby Mathew 
245532ed618SSoby Mathew 			/* Enable EL1 access to timer */
246532ed618SSoby Mathew 			write_cnthctl_el2(EL1PCEN_BIT | EL1PCTEN_BIT);
247532ed618SSoby Mathew 
248532ed618SSoby Mathew 			/* Reset CNTVOFF_EL2 */
249532ed618SSoby Mathew 			write_cntvoff_el2(0);
250532ed618SSoby Mathew 
251532ed618SSoby Mathew 			/* Set VPIDR, VMPIDR to match MIDR, MPIDR */
252532ed618SSoby Mathew 			write_vpidr_el2(read_midr_el1());
253532ed618SSoby Mathew 			write_vmpidr_el2(read_mpidr_el1());
254532ed618SSoby Mathew 
255532ed618SSoby Mathew 			/*
256532ed618SSoby Mathew 			 * Reset VTTBR_EL2.
257532ed618SSoby Mathew 			 * Needed because cache maintenance operations depend on
258532ed618SSoby Mathew 			 * the VMID even when non-secure EL1&0 stage 2 address
259532ed618SSoby Mathew 			 * translation are disabled.
260532ed618SSoby Mathew 			 */
261532ed618SSoby Mathew 			write_vttbr_el2(0);
262495f3d3cSDavid Cunado 			/*
263495f3d3cSDavid Cunado 			 * Avoid unexpected debug traps in case where MDCR_EL2
264495f3d3cSDavid Cunado 			 * is not completely reset by the hardware - set
265495f3d3cSDavid Cunado 			 * MDCR_EL2.HPMN to PMCR_EL0.N and zero the remaining
266495f3d3cSDavid Cunado 			 * bits.
267495f3d3cSDavid Cunado 			 * MDCR_EL2.HPMN and PMCR_EL0.N fields are the same size
268495f3d3cSDavid Cunado 			 * (5 bits) and HPMN is at offset zero within MDCR_EL2.
269495f3d3cSDavid Cunado 			 */
270495f3d3cSDavid Cunado 			write_mdcr_el2((read_pmcr_el0() & PMCR_EL0_N_BITS)
271495f3d3cSDavid Cunado 					>> PMCR_EL0_N_SHIFT);
272*939f66d6SDavid Cunado 			/*
273*939f66d6SDavid Cunado 			 * Avoid unexpected traps of non-secure access to
274*939f66d6SDavid Cunado 			 * certain system registers at EL1 or lower where
275*939f66d6SDavid Cunado 			 * HSTR_EL2 is not completely reset to zero by the
276*939f66d6SDavid Cunado 			 * hardware - zero the entire register.
277*939f66d6SDavid Cunado 			 */
278*939f66d6SDavid Cunado 			write_hstr_el2(0);
279*939f66d6SDavid Cunado 			/*
280*939f66d6SDavid Cunado 			 * Reset CNTHP_CTL_EL2 to disable the EL2 physical timer
281*939f66d6SDavid Cunado 			 * and therefore prevent timer interrupts.
282*939f66d6SDavid Cunado 			 */
283*939f66d6SDavid Cunado 			write_cnthp_ctl_el2(0);
284532ed618SSoby Mathew 		}
285532ed618SSoby Mathew 	}
286532ed618SSoby Mathew 
287532ed618SSoby Mathew 	el1_sysregs_context_restore(get_sysregs_ctx(ctx));
288532ed618SSoby Mathew 
289532ed618SSoby Mathew 	cm_set_next_context(ctx);
290532ed618SSoby Mathew }
291532ed618SSoby Mathew 
292532ed618SSoby Mathew /*******************************************************************************
293532ed618SSoby Mathew  * The next four functions are used by runtime services to save and restore
294532ed618SSoby Mathew  * EL1 context on the 'cpu_context' structure for the specified security
295532ed618SSoby Mathew  * state.
296532ed618SSoby Mathew  ******************************************************************************/
297532ed618SSoby Mathew void cm_el1_sysregs_context_save(uint32_t security_state)
298532ed618SSoby Mathew {
299532ed618SSoby Mathew 	cpu_context_t *ctx;
300532ed618SSoby Mathew 
301532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
302532ed618SSoby Mathew 	assert(ctx);
303532ed618SSoby Mathew 
304532ed618SSoby Mathew 	el1_sysregs_context_save(get_sysregs_ctx(ctx));
305532ed618SSoby Mathew }
306532ed618SSoby Mathew 
307532ed618SSoby Mathew void cm_el1_sysregs_context_restore(uint32_t security_state)
308532ed618SSoby Mathew {
309532ed618SSoby Mathew 	cpu_context_t *ctx;
310532ed618SSoby Mathew 
311532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
312532ed618SSoby Mathew 	assert(ctx);
313532ed618SSoby Mathew 
314532ed618SSoby Mathew 	el1_sysregs_context_restore(get_sysregs_ctx(ctx));
315532ed618SSoby Mathew }
316532ed618SSoby Mathew 
317532ed618SSoby Mathew /*******************************************************************************
318532ed618SSoby Mathew  * This function populates ELR_EL3 member of 'cpu_context' pertaining to the
319532ed618SSoby Mathew  * given security state with the given entrypoint
320532ed618SSoby Mathew  ******************************************************************************/
321532ed618SSoby Mathew void cm_set_elr_el3(uint32_t security_state, uintptr_t entrypoint)
322532ed618SSoby Mathew {
323532ed618SSoby Mathew 	cpu_context_t *ctx;
324532ed618SSoby Mathew 	el3_state_t *state;
325532ed618SSoby Mathew 
326532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
327532ed618SSoby Mathew 	assert(ctx);
328532ed618SSoby Mathew 
329532ed618SSoby Mathew 	/* Populate EL3 state so that ERET jumps to the correct entry */
330532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
331532ed618SSoby Mathew 	write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
332532ed618SSoby Mathew }
333532ed618SSoby Mathew 
334532ed618SSoby Mathew /*******************************************************************************
335532ed618SSoby Mathew  * This function populates ELR_EL3 and SPSR_EL3 members of 'cpu_context'
336532ed618SSoby Mathew  * pertaining to the given security state
337532ed618SSoby Mathew  ******************************************************************************/
338532ed618SSoby Mathew void cm_set_elr_spsr_el3(uint32_t security_state,
339532ed618SSoby Mathew 			uintptr_t entrypoint, uint32_t spsr)
340532ed618SSoby Mathew {
341532ed618SSoby Mathew 	cpu_context_t *ctx;
342532ed618SSoby Mathew 	el3_state_t *state;
343532ed618SSoby Mathew 
344532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
345532ed618SSoby Mathew 	assert(ctx);
346532ed618SSoby Mathew 
347532ed618SSoby Mathew 	/* Populate EL3 state so that ERET jumps to the correct entry */
348532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
349532ed618SSoby Mathew 	write_ctx_reg(state, CTX_ELR_EL3, entrypoint);
350532ed618SSoby Mathew 	write_ctx_reg(state, CTX_SPSR_EL3, spsr);
351532ed618SSoby Mathew }
352532ed618SSoby Mathew 
353532ed618SSoby Mathew /*******************************************************************************
354532ed618SSoby Mathew  * This function updates a single bit in the SCR_EL3 member of the 'cpu_context'
355532ed618SSoby Mathew  * pertaining to the given security state using the value and bit position
356532ed618SSoby Mathew  * specified in the parameters. It preserves all other bits.
357532ed618SSoby Mathew  ******************************************************************************/
358532ed618SSoby Mathew void cm_write_scr_el3_bit(uint32_t security_state,
359532ed618SSoby Mathew 			  uint32_t bit_pos,
360532ed618SSoby Mathew 			  uint32_t value)
361532ed618SSoby Mathew {
362532ed618SSoby Mathew 	cpu_context_t *ctx;
363532ed618SSoby Mathew 	el3_state_t *state;
364532ed618SSoby Mathew 	uint32_t scr_el3;
365532ed618SSoby Mathew 
366532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
367532ed618SSoby Mathew 	assert(ctx);
368532ed618SSoby Mathew 
369532ed618SSoby Mathew 	/* Ensure that the bit position is a valid one */
370532ed618SSoby Mathew 	assert((1 << bit_pos) & SCR_VALID_BIT_MASK);
371532ed618SSoby Mathew 
372532ed618SSoby Mathew 	/* Ensure that the 'value' is only a bit wide */
373532ed618SSoby Mathew 	assert(value <= 1);
374532ed618SSoby Mathew 
375532ed618SSoby Mathew 	/*
376532ed618SSoby Mathew 	 * Get the SCR_EL3 value from the cpu context, clear the desired bit
377532ed618SSoby Mathew 	 * and set it to its new value.
378532ed618SSoby Mathew 	 */
379532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
380532ed618SSoby Mathew 	scr_el3 = read_ctx_reg(state, CTX_SCR_EL3);
381532ed618SSoby Mathew 	scr_el3 &= ~(1 << bit_pos);
382532ed618SSoby Mathew 	scr_el3 |= value << bit_pos;
383532ed618SSoby Mathew 	write_ctx_reg(state, CTX_SCR_EL3, scr_el3);
384532ed618SSoby Mathew }
385532ed618SSoby Mathew 
386532ed618SSoby Mathew /*******************************************************************************
387532ed618SSoby Mathew  * This function retrieves SCR_EL3 member of 'cpu_context' pertaining to the
388532ed618SSoby Mathew  * given security state.
389532ed618SSoby Mathew  ******************************************************************************/
390532ed618SSoby Mathew uint32_t cm_get_scr_el3(uint32_t security_state)
391532ed618SSoby Mathew {
392532ed618SSoby Mathew 	cpu_context_t *ctx;
393532ed618SSoby Mathew 	el3_state_t *state;
394532ed618SSoby Mathew 
395532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
396532ed618SSoby Mathew 	assert(ctx);
397532ed618SSoby Mathew 
398532ed618SSoby Mathew 	/* Populate EL3 state so that ERET jumps to the correct entry */
399532ed618SSoby Mathew 	state = get_el3state_ctx(ctx);
400532ed618SSoby Mathew 	return read_ctx_reg(state, CTX_SCR_EL3);
401532ed618SSoby Mathew }
402532ed618SSoby Mathew 
403532ed618SSoby Mathew /*******************************************************************************
404532ed618SSoby Mathew  * This function is used to program the context that's used for exception
405532ed618SSoby Mathew  * return. This initializes the SP_EL3 to a pointer to a 'cpu_context' set for
406532ed618SSoby Mathew  * the required security state
407532ed618SSoby Mathew  ******************************************************************************/
408532ed618SSoby Mathew void cm_set_next_eret_context(uint32_t security_state)
409532ed618SSoby Mathew {
410532ed618SSoby Mathew 	cpu_context_t *ctx;
411532ed618SSoby Mathew 
412532ed618SSoby Mathew 	ctx = cm_get_context(security_state);
413532ed618SSoby Mathew 	assert(ctx);
414532ed618SSoby Mathew 
415532ed618SSoby Mathew 	cm_set_next_context(ctx);
416532ed618SSoby Mathew }
417