xref: /rk3399_ARM-atf/lib/el3_runtime/aarch32/context_mgmt.c (revision 495f3d3c51096de3559cc7fb77494a16fc158e26)
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 
899e3b4cbbSSoby Mathew 	reg_ctx = get_regs_ctx(ctx);
909e3b4cbbSSoby 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;
119b7b0787dSSoby Mathew 		/*
120b7b0787dSSoby Mathew 		 * In addition to SCTLR_RES1, set the CP15_BEN, nTWI & nTWE
121b7b0787dSSoby Mathew 		 * bits that architecturally reset to 1.
122b7b0787dSSoby Mathew 		 */
123b7b0787dSSoby Mathew 		sctlr |= SCTLR_RES1 | SCTLR_CP15BEN_BIT |
124b7b0787dSSoby Mathew 				SCTLR_NTWI_BIT | SCTLR_NTWE_BIT;
125e33b78a6SSoby Mathew 		write_ctx_reg(reg_ctx, CTX_NS_SCTLR, sctlr);
126e33b78a6SSoby Mathew 	}
127e33b78a6SSoby Mathew 
128e33b78a6SSoby Mathew 	if (GET_M32(ep->spsr) == MODE32_hyp)
129e33b78a6SSoby Mathew 		scr |= SCR_HCE_BIT;
130e33b78a6SSoby Mathew 
131e33b78a6SSoby Mathew 	write_ctx_reg(reg_ctx, CTX_SCR, scr);
132e33b78a6SSoby Mathew 	write_ctx_reg(reg_ctx, CTX_LR, ep->pc);
133e33b78a6SSoby Mathew 	write_ctx_reg(reg_ctx, CTX_SPSR, ep->spsr);
134e33b78a6SSoby Mathew 
135e33b78a6SSoby Mathew 	/*
136e33b78a6SSoby Mathew 	 * Store the r0-r3 value from the entrypoint into the context
137e33b78a6SSoby Mathew 	 * Use memcpy as we are in control of the layout of the structures
138e33b78a6SSoby Mathew 	 */
139e33b78a6SSoby Mathew 	memcpy((void *)reg_ctx, (void *)&ep->args, sizeof(aapcs32_params_t));
140e33b78a6SSoby Mathew }
141e33b78a6SSoby Mathew 
142e33b78a6SSoby Mathew /*******************************************************************************
143e33b78a6SSoby Mathew  * The following function initializes the cpu_context for a CPU specified by
144e33b78a6SSoby Mathew  * its `cpu_idx` for first use, and sets the initial entrypoint state as
145e33b78a6SSoby Mathew  * specified by the entry_point_info structure.
146e33b78a6SSoby Mathew  ******************************************************************************/
147e33b78a6SSoby Mathew void cm_init_context_by_index(unsigned int cpu_idx,
148e33b78a6SSoby Mathew 			      const entry_point_info_t *ep)
149e33b78a6SSoby Mathew {
150e33b78a6SSoby Mathew 	cpu_context_t *ctx;
151e33b78a6SSoby Mathew 	ctx = cm_get_context_by_index(cpu_idx, GET_SECURITY_STATE(ep->h.attr));
152e33b78a6SSoby Mathew 	cm_init_context_common(ctx, ep);
153e33b78a6SSoby Mathew }
154e33b78a6SSoby Mathew 
155e33b78a6SSoby Mathew /*******************************************************************************
156e33b78a6SSoby Mathew  * The following function initializes the cpu_context for the current CPU
157e33b78a6SSoby Mathew  * for first use, and sets the initial entrypoint state as specified by the
158e33b78a6SSoby Mathew  * entry_point_info structure.
159e33b78a6SSoby Mathew  ******************************************************************************/
160e33b78a6SSoby Mathew void cm_init_my_context(const entry_point_info_t *ep)
161e33b78a6SSoby Mathew {
162e33b78a6SSoby Mathew 	cpu_context_t *ctx;
163e33b78a6SSoby Mathew 	ctx = cm_get_context(GET_SECURITY_STATE(ep->h.attr));
164e33b78a6SSoby Mathew 	cm_init_context_common(ctx, ep);
165e33b78a6SSoby Mathew }
166e33b78a6SSoby Mathew 
167e33b78a6SSoby Mathew /*******************************************************************************
168e33b78a6SSoby Mathew  * Prepare the CPU system registers for first entry into secure or normal world
169e33b78a6SSoby Mathew  *
170e33b78a6SSoby Mathew  * If execution is requested to hyp mode, HSCTLR is initialized
171e33b78a6SSoby Mathew  * If execution is requested to non-secure PL1, and the CPU supports
172e33b78a6SSoby Mathew  * HYP mode then HYP mode is disabled by configuring all necessary HYP mode
173e33b78a6SSoby Mathew  * registers.
174e33b78a6SSoby Mathew  ******************************************************************************/
175e33b78a6SSoby Mathew void cm_prepare_el3_exit(uint32_t security_state)
176e33b78a6SSoby Mathew {
177e33b78a6SSoby Mathew 	uint32_t sctlr, scr, hcptr;
178e33b78a6SSoby Mathew 	cpu_context_t *ctx = cm_get_context(security_state);
179e33b78a6SSoby Mathew 
180e33b78a6SSoby Mathew 	assert(ctx);
181e33b78a6SSoby Mathew 
182e33b78a6SSoby Mathew 	if (security_state == NON_SECURE) {
183e33b78a6SSoby Mathew 		scr = read_ctx_reg(get_regs_ctx(ctx), CTX_SCR);
184e33b78a6SSoby Mathew 		if (scr & SCR_HCE_BIT) {
185e33b78a6SSoby Mathew 			/* Use SCTLR value to initialize HSCTLR */
186e33b78a6SSoby Mathew 			sctlr = read_ctx_reg(get_regs_ctx(ctx),
187e33b78a6SSoby Mathew 						 CTX_NS_SCTLR);
188e33b78a6SSoby Mathew 			sctlr |= HSCTLR_RES1;
189e33b78a6SSoby Mathew 			/* Temporarily set the NS bit to access HSCTLR */
190e33b78a6SSoby Mathew 			write_scr(read_scr() | SCR_NS_BIT);
191e33b78a6SSoby Mathew 			/*
192e33b78a6SSoby Mathew 			 * Make sure the write to SCR is complete so that
193e33b78a6SSoby Mathew 			 * we can access HSCTLR
194e33b78a6SSoby Mathew 			 */
195e33b78a6SSoby Mathew 			isb();
196e33b78a6SSoby Mathew 			write_hsctlr(sctlr);
197e33b78a6SSoby Mathew 			isb();
198e33b78a6SSoby Mathew 
199e33b78a6SSoby Mathew 			write_scr(read_scr() & ~SCR_NS_BIT);
200e33b78a6SSoby Mathew 			isb();
201e33b78a6SSoby Mathew 		} else if (read_id_pfr1() &
202e33b78a6SSoby Mathew 			(ID_PFR1_VIRTEXT_MASK << ID_PFR1_VIRTEXT_SHIFT)) {
203*495f3d3cSDavid Cunado 			/*
204*495f3d3cSDavid Cunado 			 * Set the NS bit to access NS copies of certain banked
205*495f3d3cSDavid Cunado 			 * registers
206*495f3d3cSDavid Cunado 			 */
207e33b78a6SSoby Mathew 			write_scr(read_scr() | SCR_NS_BIT);
208e33b78a6SSoby Mathew 			isb();
209e33b78a6SSoby Mathew 
210e33b78a6SSoby Mathew 			/* PL2 present but unused, need to disable safely */
211e33b78a6SSoby Mathew 			write_hcr(0);
212e33b78a6SSoby Mathew 
213e33b78a6SSoby Mathew 			/* HSCTLR : can be ignored when bypassing */
214e33b78a6SSoby Mathew 
215e33b78a6SSoby Mathew 			/* HCPTR : disable all traps TCPAC, TTA, TCP */
216e33b78a6SSoby Mathew 			hcptr = read_hcptr();
217e33b78a6SSoby Mathew 			hcptr &= ~(TCPAC_BIT | TTA_BIT | TCP11_BIT | TCP10_BIT);
218e33b78a6SSoby Mathew 			write_hcptr(hcptr);
219e33b78a6SSoby Mathew 
220e33b78a6SSoby Mathew 			/* Enable EL1 access to timer */
221e33b78a6SSoby Mathew 			write_cnthctl(PL1PCEN_BIT | PL1PCTEN_BIT);
222e33b78a6SSoby Mathew 
223e33b78a6SSoby Mathew 			/* Reset CNTVOFF_EL2 */
224e33b78a6SSoby Mathew 			write64_cntvoff(0);
225e33b78a6SSoby Mathew 
226e33b78a6SSoby Mathew 			/* Set VPIDR, VMPIDR to match MIDR, MPIDR */
227e33b78a6SSoby Mathew 			write_vpidr(read_midr());
228e33b78a6SSoby Mathew 			write_vmpidr(read_mpidr());
229e33b78a6SSoby Mathew 
230e33b78a6SSoby Mathew 			/*
231e33b78a6SSoby Mathew 			 * Reset VTTBR.
232e33b78a6SSoby Mathew 			 * Needed because cache maintenance operations depend on
233e33b78a6SSoby Mathew 			 * the VMID even when non-secure EL1&0 stage 2 address
234e33b78a6SSoby Mathew 			 * translation are disabled.
235e33b78a6SSoby Mathew 			 */
236e33b78a6SSoby Mathew 			write64_vttbr(0);
237*495f3d3cSDavid Cunado 
238*495f3d3cSDavid Cunado 			/*
239*495f3d3cSDavid Cunado 			 * Avoid unexpected debug traps in case where HDCR
240*495f3d3cSDavid Cunado 			 * is not completely reset by the hardware - set
241*495f3d3cSDavid Cunado 			 * HDCR.HPMN to PMCR.N and zero the remaining bits.
242*495f3d3cSDavid Cunado 			 * The HDCR.HPMN and PMCR.N fields are the same size
243*495f3d3cSDavid Cunado 			 * (5 bits) and HPMN is at offset zero within HDCR.
244*495f3d3cSDavid Cunado 			 */
245*495f3d3cSDavid Cunado 			write_hdcr((read_pmcr() & PMCR_N_BITS) >> PMCR_N_SHIFT);
246e33b78a6SSoby Mathew 			isb();
247e33b78a6SSoby Mathew 
248e33b78a6SSoby Mathew 			write_scr(read_scr() & ~SCR_NS_BIT);
249e33b78a6SSoby Mathew 			isb();
250e33b78a6SSoby Mathew 		}
251e33b78a6SSoby Mathew 	}
252e33b78a6SSoby Mathew }
253