xref: /rk3399_ARM-atf/bl1/aarch32/bl1_context_mgmt.c (revision f3b4914be3b41eb2231184f7af80240296f668c5)
1*f3b4914bSYatharth Kochar /*
2*f3b4914bSYatharth Kochar  * Copyright (c) 2016, ARM Limited and Contributors. All rights reserved.
3*f3b4914bSYatharth Kochar  *
4*f3b4914bSYatharth Kochar  * Redistribution and use in source and binary forms, with or without
5*f3b4914bSYatharth Kochar  * modification, are permitted provided that the following conditions are met:
6*f3b4914bSYatharth Kochar  *
7*f3b4914bSYatharth Kochar  * Redistributions of source code must retain the above copyright notice, this
8*f3b4914bSYatharth Kochar  * list of conditions and the following disclaimer.
9*f3b4914bSYatharth Kochar  *
10*f3b4914bSYatharth Kochar  * Redistributions in binary form must reproduce the above copyright notice,
11*f3b4914bSYatharth Kochar  * this list of conditions and the following disclaimer in the documentation
12*f3b4914bSYatharth Kochar  * and/or other materials provided with the distribution.
13*f3b4914bSYatharth Kochar  *
14*f3b4914bSYatharth Kochar  * Neither the name of ARM nor the names of its contributors may be used
15*f3b4914bSYatharth Kochar  * to endorse or promote products derived from this software without specific
16*f3b4914bSYatharth Kochar  * prior written permission.
17*f3b4914bSYatharth Kochar  *
18*f3b4914bSYatharth Kochar  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*f3b4914bSYatharth Kochar  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*f3b4914bSYatharth Kochar  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*f3b4914bSYatharth Kochar  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*f3b4914bSYatharth Kochar  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*f3b4914bSYatharth Kochar  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*f3b4914bSYatharth Kochar  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*f3b4914bSYatharth Kochar  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*f3b4914bSYatharth Kochar  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*f3b4914bSYatharth Kochar  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*f3b4914bSYatharth Kochar  * POSSIBILITY OF SUCH DAMAGE.
29*f3b4914bSYatharth Kochar  */
30*f3b4914bSYatharth Kochar 
31*f3b4914bSYatharth Kochar #include <arch_helpers.h>
32*f3b4914bSYatharth Kochar #include <assert.h>
33*f3b4914bSYatharth Kochar #include <context.h>
34*f3b4914bSYatharth Kochar #include <context_mgmt.h>
35*f3b4914bSYatharth Kochar #include <debug.h>
36*f3b4914bSYatharth Kochar #include <platform.h>
37*f3b4914bSYatharth Kochar #include <smcc_helpers.h>
38*f3b4914bSYatharth Kochar 
39*f3b4914bSYatharth Kochar /*
40*f3b4914bSYatharth Kochar  * Following arrays will be used for context management.
41*f3b4914bSYatharth Kochar  * There are 2 instances, for the Secure and Non-Secure contexts.
42*f3b4914bSYatharth Kochar  */
43*f3b4914bSYatharth Kochar static cpu_context_t bl1_cpu_context[2];
44*f3b4914bSYatharth Kochar static smc_ctx_t bl1_smc_context[2];
45*f3b4914bSYatharth Kochar 
46*f3b4914bSYatharth Kochar /* Following contains the next cpu context pointer. */
47*f3b4914bSYatharth Kochar static void *bl1_next_cpu_context_ptr;
48*f3b4914bSYatharth Kochar 
49*f3b4914bSYatharth Kochar /* Following contains the next smc context pointer. */
50*f3b4914bSYatharth Kochar static void *bl1_next_smc_context_ptr;
51*f3b4914bSYatharth Kochar 
52*f3b4914bSYatharth Kochar /* Following functions are used for SMC context handling */
53*f3b4914bSYatharth Kochar void *smc_get_ctx(int security_state)
54*f3b4914bSYatharth Kochar {
55*f3b4914bSYatharth Kochar 	assert(sec_state_is_valid(security_state));
56*f3b4914bSYatharth Kochar 	return &bl1_smc_context[security_state];
57*f3b4914bSYatharth Kochar }
58*f3b4914bSYatharth Kochar 
59*f3b4914bSYatharth Kochar void smc_set_next_ctx(int security_state)
60*f3b4914bSYatharth Kochar {
61*f3b4914bSYatharth Kochar 	assert(sec_state_is_valid(security_state));
62*f3b4914bSYatharth Kochar 	bl1_next_smc_context_ptr = &bl1_smc_context[security_state];
63*f3b4914bSYatharth Kochar }
64*f3b4914bSYatharth Kochar 
65*f3b4914bSYatharth Kochar void *smc_get_next_ctx(void)
66*f3b4914bSYatharth Kochar {
67*f3b4914bSYatharth Kochar 	return bl1_next_smc_context_ptr;
68*f3b4914bSYatharth Kochar }
69*f3b4914bSYatharth Kochar 
70*f3b4914bSYatharth Kochar /* Following functions are used for CPU context handling */
71*f3b4914bSYatharth Kochar void *cm_get_context(uint32_t security_state)
72*f3b4914bSYatharth Kochar {
73*f3b4914bSYatharth Kochar 	assert(sec_state_is_valid(security_state));
74*f3b4914bSYatharth Kochar 	return &bl1_cpu_context[security_state];
75*f3b4914bSYatharth Kochar }
76*f3b4914bSYatharth Kochar 
77*f3b4914bSYatharth Kochar void cm_set_next_context(void *cpu_context)
78*f3b4914bSYatharth Kochar {
79*f3b4914bSYatharth Kochar 	assert(cpu_context);
80*f3b4914bSYatharth Kochar 	bl1_next_cpu_context_ptr = cpu_context;
81*f3b4914bSYatharth Kochar }
82*f3b4914bSYatharth Kochar 
83*f3b4914bSYatharth Kochar void *cm_get_next_context(void)
84*f3b4914bSYatharth Kochar {
85*f3b4914bSYatharth Kochar 	return bl1_next_cpu_context_ptr;
86*f3b4914bSYatharth Kochar }
87*f3b4914bSYatharth Kochar 
88*f3b4914bSYatharth Kochar /*******************************************************************************
89*f3b4914bSYatharth Kochar  * Following function copies GP regs r0-r4, lr and spsr,
90*f3b4914bSYatharth Kochar  * from the CPU context to the SMC context structures.
91*f3b4914bSYatharth Kochar  ******************************************************************************/
92*f3b4914bSYatharth Kochar static void copy_cpu_ctx_to_smc_ctx(const regs_t *cpu_reg_ctx,
93*f3b4914bSYatharth Kochar 		smc_ctx_t *next_smc_ctx)
94*f3b4914bSYatharth Kochar {
95*f3b4914bSYatharth Kochar 	next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
96*f3b4914bSYatharth Kochar 	next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
97*f3b4914bSYatharth Kochar 	next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
98*f3b4914bSYatharth Kochar 	next_smc_ctx->r3 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R3);
99*f3b4914bSYatharth Kochar 	next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
100*f3b4914bSYatharth Kochar 	next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
101*f3b4914bSYatharth Kochar }
102*f3b4914bSYatharth Kochar 
103*f3b4914bSYatharth Kochar /*******************************************************************************
104*f3b4914bSYatharth Kochar  * Following function flushes the SMC & CPU context pointer and its data.
105*f3b4914bSYatharth Kochar  ******************************************************************************/
106*f3b4914bSYatharth Kochar static void flush_smc_and_cpu_ctx(void)
107*f3b4914bSYatharth Kochar {
108*f3b4914bSYatharth Kochar 	flush_dcache_range((uintptr_t)&bl1_next_smc_context_ptr,
109*f3b4914bSYatharth Kochar 		sizeof(bl1_next_smc_context_ptr));
110*f3b4914bSYatharth Kochar 	flush_dcache_range((uintptr_t)bl1_next_smc_context_ptr,
111*f3b4914bSYatharth Kochar 		sizeof(smc_ctx_t));
112*f3b4914bSYatharth Kochar 
113*f3b4914bSYatharth Kochar 	flush_dcache_range((uintptr_t)&bl1_next_cpu_context_ptr,
114*f3b4914bSYatharth Kochar 		sizeof(bl1_next_cpu_context_ptr));
115*f3b4914bSYatharth Kochar 	flush_dcache_range((uintptr_t)bl1_next_cpu_context_ptr,
116*f3b4914bSYatharth Kochar 		sizeof(cpu_context_t));
117*f3b4914bSYatharth Kochar }
118*f3b4914bSYatharth Kochar 
119*f3b4914bSYatharth Kochar /*******************************************************************************
120*f3b4914bSYatharth Kochar  * This function prepares the context for Secure/Normal world images.
121*f3b4914bSYatharth Kochar  * Normal world images are transitioned to HYP(if supported) else SVC.
122*f3b4914bSYatharth Kochar  ******************************************************************************/
123*f3b4914bSYatharth Kochar void bl1_prepare_next_image(unsigned int image_id)
124*f3b4914bSYatharth Kochar {
125*f3b4914bSYatharth Kochar 	unsigned int security_state;
126*f3b4914bSYatharth Kochar 	image_desc_t *image_desc;
127*f3b4914bSYatharth Kochar 	entry_point_info_t *next_bl_ep;
128*f3b4914bSYatharth Kochar 
129*f3b4914bSYatharth Kochar 	/* Get the image descriptor. */
130*f3b4914bSYatharth Kochar 	image_desc = bl1_plat_get_image_desc(image_id);
131*f3b4914bSYatharth Kochar 	assert(image_desc);
132*f3b4914bSYatharth Kochar 
133*f3b4914bSYatharth Kochar 	/* Get the entry point info. */
134*f3b4914bSYatharth Kochar 	next_bl_ep = &image_desc->ep_info;
135*f3b4914bSYatharth Kochar 
136*f3b4914bSYatharth Kochar 	/* Get the image security state. */
137*f3b4914bSYatharth Kochar 	security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
138*f3b4914bSYatharth Kochar 
139*f3b4914bSYatharth Kochar 	/* Prepare the SPSR for the next BL image. */
140*f3b4914bSYatharth Kochar 	if (security_state == SECURE) {
141*f3b4914bSYatharth Kochar 		next_bl_ep->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
142*f3b4914bSYatharth Kochar 			SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
143*f3b4914bSYatharth Kochar 	} else {
144*f3b4914bSYatharth Kochar 		/* Use HYP mode if supported else use SVC. */
145*f3b4914bSYatharth Kochar 		if (GET_VIRT_EXT(read_id_pfr1()) == MODE32_hyp) {
146*f3b4914bSYatharth Kochar 			next_bl_ep->spsr = SPSR_MODE32(MODE32_hyp, SPSR_T_ARM,
147*f3b4914bSYatharth Kochar 				SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
148*f3b4914bSYatharth Kochar 		} else {
149*f3b4914bSYatharth Kochar 			next_bl_ep->spsr = SPSR_MODE32(MODE32_svc, SPSR_T_ARM,
150*f3b4914bSYatharth Kochar 				SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
151*f3b4914bSYatharth Kochar 		}
152*f3b4914bSYatharth Kochar 	}
153*f3b4914bSYatharth Kochar 
154*f3b4914bSYatharth Kochar 	/* Allow platform to make change */
155*f3b4914bSYatharth Kochar 	bl1_plat_set_ep_info(image_id, next_bl_ep);
156*f3b4914bSYatharth Kochar 
157*f3b4914bSYatharth Kochar 	/* Prepare the cpu context for the next BL image. */
158*f3b4914bSYatharth Kochar 	cm_init_my_context(next_bl_ep);
159*f3b4914bSYatharth Kochar 	cm_prepare_el3_exit(security_state);
160*f3b4914bSYatharth Kochar 	cm_set_next_context(cm_get_context(security_state));
161*f3b4914bSYatharth Kochar 
162*f3b4914bSYatharth Kochar 	/* Prepare the smc context for the next BL image. */
163*f3b4914bSYatharth Kochar 	smc_set_next_ctx(security_state);
164*f3b4914bSYatharth Kochar 	copy_cpu_ctx_to_smc_ctx(get_regs_ctx(cm_get_next_context()),
165*f3b4914bSYatharth Kochar 		smc_get_next_ctx());
166*f3b4914bSYatharth Kochar 
167*f3b4914bSYatharth Kochar 	/*
168*f3b4914bSYatharth Kochar 	 * Flush the SMC & CPU context and the (next)pointers,
169*f3b4914bSYatharth Kochar 	 * to access them after caches are disabled.
170*f3b4914bSYatharth Kochar 	 */
171*f3b4914bSYatharth Kochar 	flush_smc_and_cpu_ctx();
172*f3b4914bSYatharth Kochar 
173*f3b4914bSYatharth Kochar 	/* Indicate that image is in execution state. */
174*f3b4914bSYatharth Kochar 	image_desc->state = IMAGE_STATE_EXECUTED;
175*f3b4914bSYatharth Kochar 
176*f3b4914bSYatharth Kochar 	print_entry_point_info(next_bl_ep);
177*f3b4914bSYatharth Kochar }
178