xref: /rk3399_ARM-atf/bl1/aarch32/bl1_context_mgmt.c (revision ee37db50c018e6f590e795a9f3537f37aa026f33)
1f3b4914bSYatharth Kochar /*
23443a702SJohn Powell  * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved.
3f3b4914bSYatharth Kochar  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5f3b4914bSYatharth Kochar  */
6f3b4914bSYatharth Kochar 
7f3b4914bSYatharth Kochar #include <assert.h>
809d40e0eSAntonio Nino Diaz 
909d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
10f3b4914bSYatharth Kochar #include <context.h>
1109d40e0eSAntonio Nino Diaz #include <common/debug.h>
1209d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h>
1309d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
14085e80ecSAntonio Nino Diaz #include <smccc_helpers.h>
1509d40e0eSAntonio Nino Diaz 
16c04d59cfSEtienne Carriere #include "../bl1_private.h"
17f3b4914bSYatharth Kochar 
18f3b4914bSYatharth Kochar /*
19f3b4914bSYatharth Kochar  * Following arrays will be used for context management.
20f3b4914bSYatharth Kochar  * There are 2 instances, for the Secure and Non-Secure contexts.
21f3b4914bSYatharth Kochar  */
22f3b4914bSYatharth Kochar static cpu_context_t bl1_cpu_context[2];
23f3b4914bSYatharth Kochar static smc_ctx_t bl1_smc_context[2];
24f3b4914bSYatharth Kochar 
25f3b4914bSYatharth Kochar /* Following contains the next cpu context pointer. */
26f3b4914bSYatharth Kochar static void *bl1_next_cpu_context_ptr;
27f3b4914bSYatharth Kochar 
28f3b4914bSYatharth Kochar /* Following contains the next smc context pointer. */
29f3b4914bSYatharth Kochar static void *bl1_next_smc_context_ptr;
30f3b4914bSYatharth Kochar 
31f3b4914bSYatharth Kochar /* Following functions are used for SMC context handling */
smc_get_ctx(unsigned int security_state)3255074083SEtienne Carriere void *smc_get_ctx(unsigned int security_state)
33f3b4914bSYatharth Kochar {
34f3b4914bSYatharth Kochar 	assert(sec_state_is_valid(security_state));
35f3b4914bSYatharth Kochar 	return &bl1_smc_context[security_state];
36f3b4914bSYatharth Kochar }
37f3b4914bSYatharth Kochar 
smc_set_next_ctx(unsigned int security_state)3855074083SEtienne Carriere void smc_set_next_ctx(unsigned int security_state)
39f3b4914bSYatharth Kochar {
40f3b4914bSYatharth Kochar 	assert(sec_state_is_valid(security_state));
41f3b4914bSYatharth Kochar 	bl1_next_smc_context_ptr = &bl1_smc_context[security_state];
42f3b4914bSYatharth Kochar }
43f3b4914bSYatharth Kochar 
smc_get_next_ctx(void)44f3b4914bSYatharth Kochar void *smc_get_next_ctx(void)
45f3b4914bSYatharth Kochar {
46f3b4914bSYatharth Kochar 	return bl1_next_smc_context_ptr;
47f3b4914bSYatharth Kochar }
48f3b4914bSYatharth Kochar 
49f3b4914bSYatharth Kochar /* Following functions are used for CPU context handling */
cm_get_context(size_t security_state)50*f05b4894SMaheedhar Bollapalli void *cm_get_context(size_t security_state)
51f3b4914bSYatharth Kochar {
52f3b4914bSYatharth Kochar 	assert(sec_state_is_valid(security_state));
53f3b4914bSYatharth Kochar 	return &bl1_cpu_context[security_state];
54f3b4914bSYatharth Kochar }
55f3b4914bSYatharth Kochar 
cm_set_next_context(void * context)563443a702SJohn Powell void cm_set_next_context(void *context)
57f3b4914bSYatharth Kochar {
583443a702SJohn Powell 	assert(context != NULL);
593443a702SJohn Powell 	bl1_next_cpu_context_ptr = context;
60f3b4914bSYatharth Kochar }
61f3b4914bSYatharth Kochar 
cm_get_next_context(void)62f3b4914bSYatharth Kochar void *cm_get_next_context(void)
63f3b4914bSYatharth Kochar {
64f3b4914bSYatharth Kochar 	return bl1_next_cpu_context_ptr;
65f3b4914bSYatharth Kochar }
66f3b4914bSYatharth Kochar 
67f3b4914bSYatharth Kochar /*******************************************************************************
68f3b4914bSYatharth Kochar  * Following function copies GP regs r0-r4, lr and spsr,
69f3b4914bSYatharth Kochar  * from the CPU context to the SMC context structures.
70f3b4914bSYatharth Kochar  ******************************************************************************/
copy_cpu_ctx_to_smc_ctx(const regs_t * cpu_reg_ctx,smc_ctx_t * next_smc_ctx)71f3b4914bSYatharth Kochar static void copy_cpu_ctx_to_smc_ctx(const regs_t *cpu_reg_ctx,
72f3b4914bSYatharth Kochar 		smc_ctx_t *next_smc_ctx)
73f3b4914bSYatharth Kochar {
74f3b4914bSYatharth Kochar 	next_smc_ctx->r0 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R0);
75f3b4914bSYatharth Kochar 	next_smc_ctx->r1 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R1);
76f3b4914bSYatharth Kochar 	next_smc_ctx->r2 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R2);
77f3b4914bSYatharth Kochar 	next_smc_ctx->r3 = read_ctx_reg(cpu_reg_ctx, CTX_GPREG_R3);
78f3b4914bSYatharth Kochar 	next_smc_ctx->lr_mon = read_ctx_reg(cpu_reg_ctx, CTX_LR);
79f3b4914bSYatharth Kochar 	next_smc_ctx->spsr_mon = read_ctx_reg(cpu_reg_ctx, CTX_SPSR);
80b6285d64SSoby Mathew 	next_smc_ctx->scr = read_ctx_reg(cpu_reg_ctx, CTX_SCR);
81f3b4914bSYatharth Kochar }
82f3b4914bSYatharth Kochar 
83f3b4914bSYatharth Kochar /*******************************************************************************
84f3b4914bSYatharth Kochar  * Following function flushes the SMC & CPU context pointer and its data.
85f3b4914bSYatharth Kochar  ******************************************************************************/
flush_smc_and_cpu_ctx(void)86f3b4914bSYatharth Kochar static void flush_smc_and_cpu_ctx(void)
87f3b4914bSYatharth Kochar {
88f3b4914bSYatharth Kochar 	flush_dcache_range((uintptr_t)&bl1_next_smc_context_ptr,
89f3b4914bSYatharth Kochar 		sizeof(bl1_next_smc_context_ptr));
90f3b4914bSYatharth Kochar 	flush_dcache_range((uintptr_t)bl1_next_smc_context_ptr,
91f3b4914bSYatharth Kochar 		sizeof(smc_ctx_t));
92f3b4914bSYatharth Kochar 
93f3b4914bSYatharth Kochar 	flush_dcache_range((uintptr_t)&bl1_next_cpu_context_ptr,
94f3b4914bSYatharth Kochar 		sizeof(bl1_next_cpu_context_ptr));
95f3b4914bSYatharth Kochar 	flush_dcache_range((uintptr_t)bl1_next_cpu_context_ptr,
96f3b4914bSYatharth Kochar 		sizeof(cpu_context_t));
97f3b4914bSYatharth Kochar }
98f3b4914bSYatharth Kochar 
99f3b4914bSYatharth Kochar /*******************************************************************************
100f3b4914bSYatharth Kochar  * This function prepares the context for Secure/Normal world images.
101f3b4914bSYatharth Kochar  * Normal world images are transitioned to HYP(if supported) else SVC.
102f3b4914bSYatharth Kochar  ******************************************************************************/
bl1_prepare_next_image(unsigned int image_id)103f3b4914bSYatharth Kochar void bl1_prepare_next_image(unsigned int image_id)
104f3b4914bSYatharth Kochar {
105d200f230SJohn Tsichritzis 	unsigned int security_state, mode = MODE32_svc;
1063443a702SJohn Powell 	image_desc_t *desc;
107f3b4914bSYatharth Kochar 	entry_point_info_t *next_bl_ep;
108f3b4914bSYatharth Kochar 
109f3b4914bSYatharth Kochar 	/* Get the image descriptor. */
1103443a702SJohn Powell 	desc = bl1_plat_get_image_desc(image_id);
1113443a702SJohn Powell 	assert(desc != NULL);
112f3b4914bSYatharth Kochar 
113f3b4914bSYatharth Kochar 	/* Get the entry point info. */
1143443a702SJohn Powell 	next_bl_ep = &desc->ep_info;
115f3b4914bSYatharth Kochar 
116f3b4914bSYatharth Kochar 	/* Get the image security state. */
117f3b4914bSYatharth Kochar 	security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
118f3b4914bSYatharth Kochar 
119f3b4914bSYatharth Kochar 	/* Prepare the SPSR for the next BL image. */
1203443a702SJohn Powell 	if ((security_state != SECURE) && (GET_VIRT_EXT(read_id_pfr1()) != 0U)) {
121d200f230SJohn Tsichritzis 		mode = MODE32_hyp;
122f3b4914bSYatharth Kochar 	}
123d200f230SJohn Tsichritzis 
124d200f230SJohn Tsichritzis 	next_bl_ep->spsr = SPSR_MODE32(mode, SPSR_T_ARM,
125d200f230SJohn Tsichritzis 				SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS);
126f3b4914bSYatharth Kochar 
127f3b4914bSYatharth Kochar 	/* Allow platform to make change */
128f3b4914bSYatharth Kochar 	bl1_plat_set_ep_info(image_id, next_bl_ep);
129f3b4914bSYatharth Kochar 
130f3b4914bSYatharth Kochar 	/* Prepare the cpu context for the next BL image. */
131f3b4914bSYatharth Kochar 	cm_init_my_context(next_bl_ep);
132f3b4914bSYatharth Kochar 	cm_prepare_el3_exit(security_state);
133f3b4914bSYatharth Kochar 	cm_set_next_context(cm_get_context(security_state));
134f3b4914bSYatharth Kochar 
135f3b4914bSYatharth Kochar 	/* Prepare the smc context for the next BL image. */
136f3b4914bSYatharth Kochar 	smc_set_next_ctx(security_state);
137f3b4914bSYatharth Kochar 	copy_cpu_ctx_to_smc_ctx(get_regs_ctx(cm_get_next_context()),
138f3b4914bSYatharth Kochar 		smc_get_next_ctx());
139f3b4914bSYatharth Kochar 
140f3b4914bSYatharth Kochar 	/*
141b6285d64SSoby Mathew 	 * If the next image is non-secure, then we need to program the banked
142b6285d64SSoby Mathew 	 * non secure sctlr. This is not required when the next image is secure
143b6285d64SSoby Mathew 	 * because in AArch32, we expect the secure world to have the same
144b6285d64SSoby Mathew 	 * SCTLR settings.
145b6285d64SSoby Mathew 	 */
146b6285d64SSoby Mathew 	if (security_state == NON_SECURE) {
147b6285d64SSoby Mathew 		cpu_context_t *ctx = cm_get_context(security_state);
148b6285d64SSoby Mathew 		u_register_t ns_sctlr;
149b6285d64SSoby Mathew 
150b6285d64SSoby Mathew 		/* Temporarily set the NS bit to access NS SCTLR */
151b6285d64SSoby Mathew 		write_scr(read_scr() | SCR_NS_BIT);
152b6285d64SSoby Mathew 		isb();
153b6285d64SSoby Mathew 
154b6285d64SSoby Mathew 		ns_sctlr = read_ctx_reg(get_regs_ctx(ctx), CTX_NS_SCTLR);
155b6285d64SSoby Mathew 		write_sctlr(ns_sctlr);
156b6285d64SSoby Mathew 		isb();
157b6285d64SSoby Mathew 
158b6285d64SSoby Mathew 		write_scr(read_scr() & ~SCR_NS_BIT);
159b6285d64SSoby Mathew 		isb();
160b6285d64SSoby Mathew 	}
161b6285d64SSoby Mathew 
162b6285d64SSoby Mathew 	/*
163f3b4914bSYatharth Kochar 	 * Flush the SMC & CPU context and the (next)pointers,
164f3b4914bSYatharth Kochar 	 * to access them after caches are disabled.
165f3b4914bSYatharth Kochar 	 */
166f3b4914bSYatharth Kochar 	flush_smc_and_cpu_ctx();
167f3b4914bSYatharth Kochar 
168f3b4914bSYatharth Kochar 	/* Indicate that image is in execution state. */
1693443a702SJohn Powell 	desc->state = IMAGE_STATE_EXECUTED;
170f3b4914bSYatharth Kochar 
171f3b4914bSYatharth Kochar 	print_entry_point_info(next_bl_ep);
172f3b4914bSYatharth Kochar }
173