xref: /rk3399_ARM-atf/lib/el3_runtime/aarch32/context_mgmt.c (revision 9e3b4cbbade36bb38a4a96381f0aca1b48ccaa0c)
1e33b78a6SSoby Mathew /*
2e33b78a6SSoby Mathew  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3e33b78a6SSoby Mathew  *
4e33b78a6SSoby Mathew  * Redistribution and use in source and binary forms, with or without
5e33b78a6SSoby Mathew  * modification, are permitted provided that the following conditions are met:
6e33b78a6SSoby Mathew  *
7e33b78a6SSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8e33b78a6SSoby Mathew  * list of conditions and the following disclaimer.
9e33b78a6SSoby Mathew  *
10e33b78a6SSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11e33b78a6SSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12e33b78a6SSoby Mathew  * and/or other materials provided with the distribution.
13e33b78a6SSoby Mathew  *
14e33b78a6SSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15e33b78a6SSoby Mathew  * to endorse or promote products derived from this software without specific
16e33b78a6SSoby Mathew  * prior written permission.
17e33b78a6SSoby Mathew  *
18e33b78a6SSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19e33b78a6SSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20e33b78a6SSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21e33b78a6SSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22e33b78a6SSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23e33b78a6SSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24e33b78a6SSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25e33b78a6SSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26e33b78a6SSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27e33b78a6SSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28e33b78a6SSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29e33b78a6SSoby Mathew  */
30e33b78a6SSoby Mathew 
31e33b78a6SSoby Mathew #include <arch.h>
32e33b78a6SSoby Mathew #include <arch_helpers.h>
33e33b78a6SSoby Mathew #include <assert.h>
34e33b78a6SSoby Mathew #include <bl_common.h>
35e33b78a6SSoby Mathew #include <context.h>
36e33b78a6SSoby Mathew #include <context_mgmt.h>
37e33b78a6SSoby Mathew #include <platform.h>
38e33b78a6SSoby Mathew #include <platform_def.h>
39e33b78a6SSoby Mathew #include <smcc_helpers.h>
40e33b78a6SSoby Mathew #include <string.h>
41e33b78a6SSoby Mathew 
42e33b78a6SSoby Mathew /*******************************************************************************
43e33b78a6SSoby Mathew  * Context management library initialisation routine. This library is used by
44e33b78a6SSoby Mathew  * runtime services to share pointers to 'cpu_context' structures for the secure
45e33b78a6SSoby Mathew  * and non-secure states. Management of the structures and their associated
46e33b78a6SSoby Mathew  * memory is not done by the context management library e.g. the PSCI service
47e33b78a6SSoby Mathew  * manages the cpu context used for entry from and exit to the non-secure state.
48e33b78a6SSoby Mathew  * The Secure payload manages the context(s) corresponding to the secure state.
49e33b78a6SSoby Mathew  * It also uses this library to get access to the non-secure
50e33b78a6SSoby Mathew  * state cpu context pointers.
51e33b78a6SSoby Mathew  ******************************************************************************/
52e33b78a6SSoby Mathew void cm_init(void)
53e33b78a6SSoby Mathew {
54e33b78a6SSoby Mathew 	/*
55e33b78a6SSoby Mathew 	 * The context management library has only global data to initialize, but
56e33b78a6SSoby Mathew 	 * that will be done when the BSS is zeroed out
57e33b78a6SSoby Mathew 	 */
58e33b78a6SSoby Mathew }
59e33b78a6SSoby Mathew 
60e33b78a6SSoby Mathew /*******************************************************************************
61e33b78a6SSoby Mathew  * The following function initializes the cpu_context 'ctx' for
62e33b78a6SSoby Mathew  * first use, and sets the initial entrypoint state as specified by the
63e33b78a6SSoby Mathew  * entry_point_info structure.
64e33b78a6SSoby Mathew  *
65e33b78a6SSoby Mathew  * The security state to initialize is determined by the SECURE attribute
66e33b78a6SSoby Mathew  * of the entry_point_info. The function returns a pointer to the initialized
67e33b78a6SSoby Mathew  * context and sets this as the next context to return to.
68e33b78a6SSoby Mathew  *
69e33b78a6SSoby Mathew  * The EE and ST attributes are used to configure the endianness and secure
70e33b78a6SSoby Mathew  * timer availability for the new execution context.
71e33b78a6SSoby Mathew  *
72e33b78a6SSoby Mathew  * To prepare the register state for entry call cm_prepare_el3_exit() and
73e33b78a6SSoby Mathew  * el3_exit(). For Secure-EL1 cm_prepare_el3_exit() is equivalent to
74e33b78a6SSoby Mathew  * cm_e1_sysreg_context_restore().
75e33b78a6SSoby Mathew  ******************************************************************************/
76e33b78a6SSoby Mathew static void cm_init_context_common(cpu_context_t *ctx, const entry_point_info_t *ep)
77e33b78a6SSoby Mathew {
78e33b78a6SSoby Mathew 	unsigned int security_state;
79e33b78a6SSoby Mathew 	uint32_t scr, sctlr;
80e33b78a6SSoby Mathew 	regs_t *reg_ctx;
81e33b78a6SSoby Mathew 
82e33b78a6SSoby Mathew 	assert(ctx);
83e33b78a6SSoby Mathew 
84e33b78a6SSoby Mathew 	security_state = GET_SECURITY_STATE(ep->h.attr);
85e33b78a6SSoby Mathew 
86e33b78a6SSoby Mathew 	/* Clear any residual register values from the context */
87e33b78a6SSoby Mathew 	memset(ctx, 0, sizeof(*ctx));
88e33b78a6SSoby Mathew 
89*9e3b4cbbSSoby Mathew 	reg_ctx = get_regs_ctx(ctx);
90*9e3b4cbbSSoby Mathew 
91e33b78a6SSoby Mathew 	/*
92e33b78a6SSoby Mathew 	 * Base the context SCR on the current value, adjust for entry point
93e33b78a6SSoby Mathew 	 * specific requirements
94e33b78a6SSoby Mathew 	 */
95e33b78a6SSoby Mathew 	scr = read_scr();
96e33b78a6SSoby Mathew 	scr &= ~(SCR_NS_BIT | SCR_HCE_BIT);
97e33b78a6SSoby Mathew 
98e33b78a6SSoby Mathew 	if (security_state != SECURE)
99e33b78a6SSoby Mathew 		scr |= SCR_NS_BIT;
100e33b78a6SSoby Mathew 
101e33b78a6SSoby Mathew 	/*
102e33b78a6SSoby Mathew 	 * Set up SCTLR for the Non Secure context.
103e33b78a6SSoby Mathew 	 * EE bit is taken from the entrypoint attributes
104e33b78a6SSoby Mathew 	 * M, C and I bits must be zero (as required by PSCI specification)
105e33b78a6SSoby Mathew 	 *
106e33b78a6SSoby Mathew 	 * The target exception level is based on the spsr mode requested.
107e33b78a6SSoby Mathew 	 * If execution is requested to hyp mode, HVC is enabled
108e33b78a6SSoby Mathew 	 * via SCR.HCE.
109e33b78a6SSoby Mathew 	 *
110e33b78a6SSoby Mathew 	 * Always compute the SCTLR_EL1 value and save in the cpu_context
111e33b78a6SSoby Mathew 	 * - the HYP registers are set up by cm_preapre_ns_entry() as they
112e33b78a6SSoby Mathew 	 * are not part of the stored cpu_context
113e33b78a6SSoby Mathew 	 *
114e33b78a6SSoby Mathew 	 * TODO: In debug builds the spsr should be validated and checked
115e33b78a6SSoby Mathew 	 * against the CPU support, security state, endianness and pc
116e33b78a6SSoby Mathew 	 */
117e33b78a6SSoby Mathew 	if (security_state != SECURE) {
118e33b78a6SSoby Mathew 		sctlr = EP_GET_EE(ep->h.attr) ? SCTLR_EE_BIT : 0;
119e33b78a6SSoby Mathew 		sctlr |= SCTLR_RES1;
120e33b78a6SSoby Mathew 		write_ctx_reg(reg_ctx, CTX_NS_SCTLR, sctlr);
121e33b78a6SSoby Mathew 	}
122e33b78a6SSoby Mathew 
123e33b78a6SSoby Mathew 	if (GET_M32(ep->spsr) == MODE32_hyp)
124e33b78a6SSoby Mathew 		scr |= SCR_HCE_BIT;
125e33b78a6SSoby Mathew 
126e33b78a6SSoby Mathew 	write_ctx_reg(reg_ctx, CTX_SCR, scr);
127e33b78a6SSoby Mathew 	write_ctx_reg(reg_ctx, CTX_LR, ep->pc);
128e33b78a6SSoby Mathew 	write_ctx_reg(reg_ctx, CTX_SPSR, ep->spsr);
129e33b78a6SSoby Mathew 
130e33b78a6SSoby Mathew 	/*
131e33b78a6SSoby Mathew 	 * Store the r0-r3 value from the entrypoint into the context
132e33b78a6SSoby Mathew 	 * Use memcpy as we are in control of the layout of the structures
133e33b78a6SSoby Mathew 	 */
134e33b78a6SSoby Mathew 	memcpy((void *)reg_ctx, (void *)&ep->args, sizeof(aapcs32_params_t));
135e33b78a6SSoby Mathew }
136e33b78a6SSoby Mathew 
137e33b78a6SSoby Mathew /*******************************************************************************
138e33b78a6SSoby Mathew  * The following function initializes the cpu_context for a CPU specified by
139e33b78a6SSoby Mathew  * its `cpu_idx` for first use, and sets the initial entrypoint state as
140e33b78a6SSoby Mathew  * specified by the entry_point_info structure.
141e33b78a6SSoby Mathew  ******************************************************************************/
142e33b78a6SSoby Mathew void cm_init_context_by_index(unsigned int cpu_idx,
143e33b78a6SSoby Mathew 			      const entry_point_info_t *ep)
144e33b78a6SSoby Mathew {
145e33b78a6SSoby Mathew 	cpu_context_t *ctx;
146e33b78a6SSoby Mathew 	ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
147e33b78a6SSoby Mathew 	cm_init_context_common(ctx, ep);
148e33b78a6SSoby Mathew }
149e33b78a6SSoby Mathew 
150e33b78a6SSoby Mathew /*******************************************************************************
151e33b78a6SSoby Mathew  * The following function initializes the cpu_context for the current CPU
152e33b78a6SSoby Mathew  * for first use, and sets the initial entrypoint state as specified by the
153e33b78a6SSoby Mathew  * entry_point_info structure.
154e33b78a6SSoby Mathew  ******************************************************************************/
155e33b78a6SSoby Mathew void cm_init_my_context(const entry_point_info_t *ep)
156e33b78a6SSoby Mathew {
157e33b78a6SSoby Mathew 	cpu_context_t *ctx;
158e33b78a6SSoby Mathew 	ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
159e33b78a6SSoby Mathew 	cm_init_context_common(ctx, ep);
160e33b78a6SSoby Mathew }
161e33b78a6SSoby Mathew 
162e33b78a6SSoby Mathew /*******************************************************************************
163e33b78a6SSoby Mathew  * Prepare the CPU system registers for first entry into secure or normal world
164e33b78a6SSoby Mathew  *
165e33b78a6SSoby Mathew  * If execution is requested to hyp mode, HSCTLR is initialized
166e33b78a6SSoby Mathew  * If execution is requested to non-secure PL1, and the CPU supports
167e33b78a6SSoby Mathew  * HYP mode then HYP mode is disabled by configuring all necessary HYP mode
168e33b78a6SSoby Mathew  * registers.
169e33b78a6SSoby Mathew  ******************************************************************************/
170e33b78a6SSoby Mathew void cm_prepare_el3_exit(uint32_t security_state)
171e33b78a6SSoby Mathew {
172e33b78a6SSoby Mathew 	uint32_t sctlr, scr, hcptr;
173e33b78a6SSoby Mathew 	cpu_context_t *ctx = cm_get_context(security_state);
174e33b78a6SSoby Mathew 
175e33b78a6SSoby Mathew 	assert(ctx);
176e33b78a6SSoby Mathew 
177e33b78a6SSoby Mathew 	if (security_state == NON_SECURE) {
178e33b78a6SSoby Mathew 		scr = read_ctx_reg(get_regs_ctx(ctx), CTX_SCR);
179e33b78a6SSoby Mathew 		if (scr & SCR_HCE_BIT) {
180e33b78a6SSoby Mathew 			/* Use SCTLR value to initialize HSCTLR */
181e33b78a6SSoby Mathew 			sctlr = read_ctx_reg(get_regs_ctx(ctx),
182e33b78a6SSoby Mathew 						 CTX_NS_SCTLR);
183e33b78a6SSoby Mathew 			sctlr |= HSCTLR_RES1;
184e33b78a6SSoby Mathew 			/* Temporarily set the NS bit to access HSCTLR */
185e33b78a6SSoby Mathew 			write_scr(read_scr() | SCR_NS_BIT);
186e33b78a6SSoby Mathew 			/*
187e33b78a6SSoby Mathew 			 * Make sure the write to SCR is complete so that
188e33b78a6SSoby Mathew 			 * we can access HSCTLR
189e33b78a6SSoby Mathew 			 */
190e33b78a6SSoby Mathew 			isb();
191e33b78a6SSoby Mathew 			write_hsctlr(sctlr);
192e33b78a6SSoby Mathew 			isb();
193e33b78a6SSoby Mathew 
194e33b78a6SSoby Mathew 			write_scr(read_scr() & ~SCR_NS_BIT);
195e33b78a6SSoby Mathew 			isb();
196e33b78a6SSoby Mathew 		} else if (read_id_pfr1() &
197e33b78a6SSoby Mathew 			(ID_PFR1_VIRTEXT_MASK << ID_PFR1_VIRTEXT_SHIFT)) {
198e33b78a6SSoby Mathew 			/* Set the NS bit to access HCR, HCPTR, CNTHCTL, VPIDR, VMPIDR */
199e33b78a6SSoby Mathew 			write_scr(read_scr() | SCR_NS_BIT);
200e33b78a6SSoby Mathew 			isb();
201e33b78a6SSoby Mathew 
202e33b78a6SSoby Mathew 			/* PL2 present but unused, need to disable safely */
203e33b78a6SSoby Mathew 			write_hcr(0);
204e33b78a6SSoby Mathew 
205e33b78a6SSoby Mathew 			/* HSCTLR : can be ignored when bypassing */
206e33b78a6SSoby Mathew 
207e33b78a6SSoby Mathew 			/* HCPTR : disable all traps TCPAC, TTA, TCP */
208e33b78a6SSoby Mathew 			hcptr = read_hcptr();
209e33b78a6SSoby Mathew 			hcptr &= ~(TCPAC_BIT | TTA_BIT | TCP11_BIT | TCP10_BIT);
210e33b78a6SSoby Mathew 			write_hcptr(hcptr);
211e33b78a6SSoby Mathew 
212e33b78a6SSoby Mathew 			/* Enable EL1 access to timer */
213e33b78a6SSoby Mathew 			write_cnthctl(PL1PCEN_BIT | PL1PCTEN_BIT);
214e33b78a6SSoby Mathew 
215e33b78a6SSoby Mathew 			/* Reset CNTVOFF_EL2 */
216e33b78a6SSoby Mathew 			write64_cntvoff(0);
217e33b78a6SSoby Mathew 
218e33b78a6SSoby Mathew 			/* Set VPIDR, VMPIDR to match MIDR, MPIDR */
219e33b78a6SSoby Mathew 			write_vpidr(read_midr());
220e33b78a6SSoby Mathew 			write_vmpidr(read_mpidr());
221e33b78a6SSoby Mathew 
222e33b78a6SSoby Mathew 			/*
223e33b78a6SSoby Mathew 			 * Reset VTTBR.
224e33b78a6SSoby Mathew 			 * Needed because cache maintenance operations depend on
225e33b78a6SSoby Mathew 			 * the VMID even when non-secure EL1&0 stage 2 address
226e33b78a6SSoby Mathew 			 * translation are disabled.
227e33b78a6SSoby Mathew 			 */
228e33b78a6SSoby Mathew 			write64_vttbr(0);
229e33b78a6SSoby Mathew 			isb();
230e33b78a6SSoby Mathew 
231e33b78a6SSoby Mathew 			write_scr(read_scr() & ~SCR_NS_BIT);
232e33b78a6SSoby Mathew 			isb();
233e33b78a6SSoby Mathew 		}
234e33b78a6SSoby Mathew 	}
235e33b78a6SSoby Mathew }
236