blob: b9a7e5ba60f084410f321327f92451812f40130c [file] [log] [blame]
Yatharth Kochara65be2f2015-10-09 18:06:13 +01001/*
Zelalem Aweke688fbf72021-07-09 11:37:10 -05002 * Copyright (c) 2015-2021, Arm Limited and Contributors. All rights reserved.
Yatharth Kochara65be2f2015-10-09 18:06:13 +01003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Yatharth Kochara65be2f2015-10-09 18:06:13 +01005 */
6
Yatharth Kochara65be2f2015-10-09 18:06:13 +01007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <arch_helpers.h>
Yatharth Kochara65be2f2015-10-09 18:06:13 +010010#include <context.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <common/debug.h>
12#include <lib/el3_runtime/context_mgmt.h>
13#include <plat/common/platform.h>
14
Etienne Carriereba7c3d52017-06-07 16:41:50 +020015#include "../bl1_private.h"
Yatharth Kochara65be2f2015-10-09 18:06:13 +010016
Yatharth Kochara65be2f2015-10-09 18:06:13 +010017/* Following contains the cpu context pointers. */
18static void *bl1_cpu_context_ptr[2];
Zelalem Aweke688fbf72021-07-09 11:37:10 -050019entry_point_info_t *bl2_ep_info;
Yatharth Kochara65be2f2015-10-09 18:06:13 +010020
21
22void *cm_get_context(uint32_t security_state)
23{
24 assert(sec_state_is_valid(security_state));
25 return bl1_cpu_context_ptr[security_state];
26}
27
28void cm_set_context(void *context, uint32_t security_state)
29{
30 assert(sec_state_is_valid(security_state));
31 bl1_cpu_context_ptr[security_state] = context;
32}
33
Zelalem Aweke688fbf72021-07-09 11:37:10 -050034#if ENABLE_RME
35/*******************************************************************************
36 * This function prepares the entry point information to run BL2 in Root world,
37 * i.e. EL3, for the case when FEAT_RME is enabled.
38 ******************************************************************************/
39void bl1_prepare_next_image(unsigned int image_id)
40{
41 image_desc_t *bl2_desc;
42
43 assert(image_id == BL2_IMAGE_ID);
44
45 /* Get the image descriptor. */
46 bl2_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID);
47 assert(bl2_desc != NULL);
48
49 /* Get the entry point info. */
50 bl2_ep_info = &bl2_desc->ep_info;
51
52 bl2_ep_info->spsr = (uint32_t)SPSR_64(MODE_EL3, MODE_SP_ELX,
53 DISABLE_ALL_EXCEPTIONS);
54
55 /*
56 * Flush cache since bl2_ep_info is accessed after MMU is disabled
57 * before jumping to BL2.
58 */
59 flush_dcache_range((uintptr_t)bl2_ep_info, sizeof(entry_point_info_t));
60
61 /* Indicate that image is in execution state. */
62 bl2_desc->state = IMAGE_STATE_EXECUTED;
63
64 /* Print debug info and flush the console before running BL2. */
65 print_entry_point_info(bl2_ep_info);
66}
67#else
Yatharth Kochara65be2f2015-10-09 18:06:13 +010068/*******************************************************************************
69 * This function prepares the context for Secure/Normal world images.
70 * Normal world images are transitioned to EL2(if supported) else EL1.
71 ******************************************************************************/
72void bl1_prepare_next_image(unsigned int image_id)
73{
Jimmy Brissonf94399a2020-08-04 16:27:51 -050074
75 /*
76 * Following array will be used for context management.
77 * There are 2 instances, for the Secure and Non-Secure contexts.
78 */
79 static cpu_context_t bl1_cpu_context[2];
80
John Tsichritzisa533ae72019-07-01 14:27:33 +010081 unsigned int security_state, mode = MODE_EL1;
John Powella5c66362020-03-20 14:21:05 -050082 image_desc_t *desc;
Yatharth Kochara65be2f2015-10-09 18:06:13 +010083 entry_point_info_t *next_bl_ep;
84
Soby Mathewd75d2ba2016-05-17 14:01:32 +010085#if CTX_INCLUDE_AARCH32_REGS
86 /*
87 * Ensure that the build flag to save AArch32 system registers in CPU
88 * context is not set for AArch64-only platforms.
89 */
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +000090 if (el_implemented(1) == EL_IMPL_A64ONLY) {
Soby Mathewd75d2ba2016-05-17 14:01:32 +010091 ERROR("EL1 supports AArch64-only. Please set build flag "
Antonio Nino Diaz864ca6f2018-10-31 15:25:35 +000092 "CTX_INCLUDE_AARCH32_REGS = 0\n");
Soby Mathewd75d2ba2016-05-17 14:01:32 +010093 panic();
94 }
95#endif
96
Yatharth Kochara65be2f2015-10-09 18:06:13 +010097 /* Get the image descriptor. */
John Powella5c66362020-03-20 14:21:05 -050098 desc = bl1_plat_get_image_desc(image_id);
99 assert(desc != NULL);
Yatharth Kochara65be2f2015-10-09 18:06:13 +0100100
101 /* Get the entry point info. */
John Powella5c66362020-03-20 14:21:05 -0500102 next_bl_ep = &desc->ep_info;
Yatharth Kochara65be2f2015-10-09 18:06:13 +0100103
104 /* Get the image security state. */
Yatharth Kocharf11b29a2016-02-01 11:04:46 +0000105 security_state = GET_SECURITY_STATE(next_bl_ep->h.attr);
Yatharth Kochara65be2f2015-10-09 18:06:13 +0100106
107 /* Setup the Secure/Non-Secure context if not done already. */
Zelaleme8dadb12020-02-05 14:12:39 -0600108 if (cm_get_context(security_state) == NULL)
Yatharth Kochara65be2f2015-10-09 18:06:13 +0100109 cm_set_context(&bl1_cpu_context[security_state], security_state);
110
111 /* Prepare the SPSR for the next BL image. */
John Tsichritzisa533ae72019-07-01 14:27:33 +0100112 if ((security_state != SECURE) && (el_implemented(2) != EL_IMPL_NONE)) {
113 mode = MODE_EL2;
Yatharth Kochara65be2f2015-10-09 18:06:13 +0100114 }
115
Jimmy Brissoned202072020-08-04 16:18:52 -0500116 next_bl_ep->spsr = (uint32_t)SPSR_64((uint64_t) mode,
117 (uint64_t)MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS);
John Tsichritzisa533ae72019-07-01 14:27:33 +0100118
Yatharth Kochara65be2f2015-10-09 18:06:13 +0100119 /* Allow platform to make change */
120 bl1_plat_set_ep_info(image_id, next_bl_ep);
121
122 /* Prepare the context for the next BL image. */
123 cm_init_my_context(next_bl_ep);
124 cm_prepare_el3_exit(security_state);
125
126 /* Indicate that image is in execution state. */
John Powella5c66362020-03-20 14:21:05 -0500127 desc->state = IMAGE_STATE_EXECUTED;
Yatharth Kochara65be2f2015-10-09 18:06:13 +0100128
129 print_entry_point_info(next_bl_ep);
130}
Zelalem Aweke688fbf72021-07-09 11:37:10 -0500131#endif /* ENABLE_RME */