xref: /rk3399_ARM-atf/bl1/aarch64/bl1_context_mgmt.c (revision d7b5f40823d449cc79e6440174390997cf11a9d9)
1f3b4914bSYatharth Kochar /*
2466bb285SZelalem  * Copyright (c) 2015-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>
1409d40e0eSAntonio Nino Diaz 
15c04d59cfSEtienne Carriere #include "../bl1_private.h"
16f3b4914bSYatharth Kochar 
17f3b4914bSYatharth Kochar /* Following contains the cpu context pointers. */
18f3b4914bSYatharth Kochar static void *bl1_cpu_context_ptr[2];
19f3b4914bSYatharth Kochar 
20f3b4914bSYatharth Kochar 
21f3b4914bSYatharth Kochar void *cm_get_context(uint32_t security_state)
22f3b4914bSYatharth Kochar {
23f3b4914bSYatharth Kochar 	assert(sec_state_is_valid(security_state));
24f3b4914bSYatharth Kochar 	return bl1_cpu_context_ptr[security_state];
25f3b4914bSYatharth Kochar }
26f3b4914bSYatharth Kochar 
27f3b4914bSYatharth Kochar void cm_set_context(void *context, uint32_t security_state)
28f3b4914bSYatharth Kochar {
29f3b4914bSYatharth Kochar 	assert(sec_state_is_valid(security_state));
30f3b4914bSYatharth Kochar 	bl1_cpu_context_ptr[security_state] = context;
31f3b4914bSYatharth Kochar }
32f3b4914bSYatharth Kochar 
33f3b4914bSYatharth Kochar /*******************************************************************************
34f3b4914bSYatharth Kochar  * This function prepares the context for Secure/Normal world images.
35f3b4914bSYatharth Kochar  * Normal world images are transitioned to EL2(if supported) else EL1.
36f3b4914bSYatharth Kochar  ******************************************************************************/
37f3b4914bSYatharth Kochar void bl1_prepare_next_image(unsigned int image_id)
38f3b4914bSYatharth Kochar {
39a14988c6SJimmy Brisson 
40a14988c6SJimmy Brisson 	/*
41a14988c6SJimmy Brisson 	 * Following array will be used for context management.
42a14988c6SJimmy Brisson 	 * There are 2 instances, for the Secure and Non-Secure contexts.
43a14988c6SJimmy Brisson 	 */
44a14988c6SJimmy Brisson 	static cpu_context_t bl1_cpu_context[2];
45a14988c6SJimmy Brisson 
46d200f230SJohn Tsichritzis 	unsigned int security_state, mode = MODE_EL1;
473443a702SJohn Powell 	image_desc_t *desc;
48f3b4914bSYatharth Kochar 	entry_point_info_t *next_bl_ep;
49f3b4914bSYatharth Kochar 
50f3b4914bSYatharth Kochar #if CTX_INCLUDE_AARCH32_REGS
51f3b4914bSYatharth Kochar 	/*
52f3b4914bSYatharth Kochar 	 * Ensure that the build flag to save AArch32 system registers in CPU
53f3b4914bSYatharth Kochar 	 * context is not set for AArch64-only platforms.
54f3b4914bSYatharth Kochar 	 */
55a0fee747SAntonio Nino Diaz 	if (el_implemented(1) == EL_IMPL_A64ONLY) {
56f3b4914bSYatharth Kochar 		ERROR("EL1 supports AArch64-only. Please set build flag "
57a0fee747SAntonio Nino Diaz 				"CTX_INCLUDE_AARCH32_REGS = 0\n");
58f3b4914bSYatharth Kochar 		panic();
59f3b4914bSYatharth Kochar 	}
60f3b4914bSYatharth Kochar #endif
61f3b4914bSYatharth Kochar 
62f3b4914bSYatharth Kochar 	/* Get the image descriptor. */
633443a702SJohn Powell 	desc = bl1_plat_get_image_desc(image_id);
643443a702SJohn Powell 	assert(desc != NULL);
65f3b4914bSYatharth Kochar 
66f3b4914bSYatharth Kochar 	/* Get the entry point info. */
673443a702SJohn Powell 	next_bl_ep = &desc->ep_info;
68f3b4914bSYatharth Kochar 
69f3b4914bSYatharth Kochar 	/* Get the image security state. */
70f3b4914bSYatharth Kochar 	security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
71f3b4914bSYatharth Kochar 
72f3b4914bSYatharth Kochar 	/* Setup the Secure/Non-Secure context if not done already. */
73466bb285SZelalem 	if (cm_get_context(security_state) == NULL)
74f3b4914bSYatharth Kochar 		cm_set_context(&bl1_cpu_context[security_state], security_state);
75f3b4914bSYatharth Kochar 
76f3b4914bSYatharth Kochar 	/* Prepare the SPSR for the next BL image. */
77d200f230SJohn Tsichritzis 	if ((security_state != SECURE) && (el_implemented(2) != EL_IMPL_NONE)) {
78d200f230SJohn Tsichritzis 		mode = MODE_EL2;
79f3b4914bSYatharth Kochar 	}
80d200f230SJohn Tsichritzis 
81*d7b5f408SJimmy Brisson 	next_bl_ep->spsr = (uint32_t)SPSR_64((uint64_t) mode,
82*d7b5f408SJimmy Brisson 		(uint64_t)MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
83f3b4914bSYatharth Kochar 
84f3b4914bSYatharth Kochar 	/* Allow platform to make change */
85f3b4914bSYatharth Kochar 	bl1_plat_set_ep_info(image_id, next_bl_ep);
86f3b4914bSYatharth Kochar 
87f3b4914bSYatharth Kochar 	/* Prepare the context for the next BL image. */
88f3b4914bSYatharth Kochar 	cm_init_my_context(next_bl_ep);
89f3b4914bSYatharth Kochar 	cm_prepare_el3_exit(security_state);
90f3b4914bSYatharth Kochar 
91f3b4914bSYatharth Kochar 	/* Indicate that image is in execution state. */
923443a702SJohn Powell 	desc->state = IMAGE_STATE_EXECUTED;
93f3b4914bSYatharth Kochar 
94f3b4914bSYatharth Kochar 	print_entry_point_info(next_bl_ep);
95f3b4914bSYatharth Kochar }
96