1f3b4914bSYatharth Kochar /* 2*322107b1SBoyan Karatotev * Copyright (c) 2015-2025, 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 176c09af9fSZelalem Aweke entry_point_info_t *bl2_ep_info; 18f3b4914bSYatharth Kochar 19*322107b1SBoyan Karatotev /* 20*322107b1SBoyan Karatotev * Following array will be used for context management. 21*322107b1SBoyan Karatotev * There are 2 instances, for the Secure and Non-Secure contexts. 22*322107b1SBoyan Karatotev */ 23*322107b1SBoyan Karatotev static cpu_context_t bl1_cpu_context[2]; 24f3b4914bSYatharth Kochar 25f05b4894SMaheedhar Bollapalli void *cm_get_context(size_t security_state) 26f3b4914bSYatharth Kochar { 27f3b4914bSYatharth Kochar assert(sec_state_is_valid(security_state)); 28*322107b1SBoyan Karatotev return &bl1_cpu_context[security_state]; 29f3b4914bSYatharth Kochar } 30f3b4914bSYatharth Kochar 316c09af9fSZelalem Aweke #if ENABLE_RME 326c09af9fSZelalem Aweke /******************************************************************************* 336c09af9fSZelalem Aweke * This function prepares the entry point information to run BL2 in Root world, 346c09af9fSZelalem Aweke * i.e. EL3, for the case when FEAT_RME is enabled. 356c09af9fSZelalem Aweke ******************************************************************************/ 366c09af9fSZelalem Aweke void bl1_prepare_next_image(unsigned int image_id) 376c09af9fSZelalem Aweke { 386c09af9fSZelalem Aweke image_desc_t *bl2_desc; 396c09af9fSZelalem Aweke 406c09af9fSZelalem Aweke assert(image_id == BL2_IMAGE_ID); 416c09af9fSZelalem Aweke 426c09af9fSZelalem Aweke /* Get the image descriptor. */ 436c09af9fSZelalem Aweke bl2_desc = bl1_plat_get_image_desc(BL2_IMAGE_ID); 446c09af9fSZelalem Aweke assert(bl2_desc != NULL); 456c09af9fSZelalem Aweke 466c09af9fSZelalem Aweke /* Get the entry point info. */ 476c09af9fSZelalem Aweke bl2_ep_info = &bl2_desc->ep_info; 486c09af9fSZelalem Aweke 496c09af9fSZelalem Aweke bl2_ep_info->spsr = (uint32_t)SPSR_64(MODE_EL3, MODE_SP_ELX, 506c09af9fSZelalem Aweke DISABLE_ALL_EXCEPTIONS); 516c09af9fSZelalem Aweke 526c09af9fSZelalem Aweke /* 536c09af9fSZelalem Aweke * Flush cache since bl2_ep_info is accessed after MMU is disabled 546c09af9fSZelalem Aweke * before jumping to BL2. 556c09af9fSZelalem Aweke */ 566c09af9fSZelalem Aweke flush_dcache_range((uintptr_t)bl2_ep_info, sizeof(entry_point_info_t)); 576c09af9fSZelalem Aweke 586c09af9fSZelalem Aweke /* Indicate that image is in execution state. */ 596c09af9fSZelalem Aweke bl2_desc->state = IMAGE_STATE_EXECUTED; 606c09af9fSZelalem Aweke 616c09af9fSZelalem Aweke /* Print debug info and flush the console before running BL2. */ 626c09af9fSZelalem Aweke print_entry_point_info(bl2_ep_info); 636c09af9fSZelalem Aweke } 646c09af9fSZelalem Aweke #else 65f3b4914bSYatharth Kochar /******************************************************************************* 66f3b4914bSYatharth Kochar * This function prepares the context for Secure/Normal world images. 67f3b4914bSYatharth Kochar * Normal world images are transitioned to EL2(if supported) else EL1. 68f3b4914bSYatharth Kochar ******************************************************************************/ 69f3b4914bSYatharth Kochar void bl1_prepare_next_image(unsigned int image_id) 70f3b4914bSYatharth Kochar { 71d200f230SJohn Tsichritzis unsigned int security_state, mode = MODE_EL1; 723443a702SJohn Powell image_desc_t *desc; 73f3b4914bSYatharth Kochar entry_point_info_t *next_bl_ep; 74f3b4914bSYatharth Kochar 75f3b4914bSYatharth Kochar #if CTX_INCLUDE_AARCH32_REGS 76f3b4914bSYatharth Kochar /* 77f3b4914bSYatharth Kochar * Ensure that the build flag to save AArch32 system registers in CPU 78f3b4914bSYatharth Kochar * context is not set for AArch64-only platforms. 79f3b4914bSYatharth Kochar */ 80a0fee747SAntonio Nino Diaz if (el_implemented(1) == EL_IMPL_A64ONLY) { 81f3b4914bSYatharth Kochar ERROR("EL1 supports AArch64-only. Please set build flag " 82a0fee747SAntonio Nino Diaz "CTX_INCLUDE_AARCH32_REGS = 0\n"); 83f3b4914bSYatharth Kochar panic(); 84f3b4914bSYatharth Kochar } 85f3b4914bSYatharth Kochar #endif 86f3b4914bSYatharth Kochar 87f3b4914bSYatharth Kochar /* Get the image descriptor. */ 883443a702SJohn Powell desc = bl1_plat_get_image_desc(image_id); 893443a702SJohn Powell assert(desc != NULL); 90f3b4914bSYatharth Kochar 91f3b4914bSYatharth Kochar /* Get the entry point info. */ 923443a702SJohn Powell next_bl_ep = &desc->ep_info; 93f3b4914bSYatharth Kochar 94f3b4914bSYatharth Kochar /* Get the image security state. */ 95f3b4914bSYatharth Kochar security_state = GET_SECURITY_STATE(next_bl_ep->h.attr); 96f3b4914bSYatharth Kochar 97f3b4914bSYatharth Kochar /* Prepare the SPSR for the next BL image. */ 98d200f230SJohn Tsichritzis if ((security_state != SECURE) && (el_implemented(2) != EL_IMPL_NONE)) { 99d200f230SJohn Tsichritzis mode = MODE_EL2; 100f3b4914bSYatharth Kochar } 101d200f230SJohn Tsichritzis 102d7b5f408SJimmy Brisson next_bl_ep->spsr = (uint32_t)SPSR_64((uint64_t) mode, 103d7b5f408SJimmy Brisson (uint64_t)MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 104f3b4914bSYatharth Kochar 105f3b4914bSYatharth Kochar /* Allow platform to make change */ 106f3b4914bSYatharth Kochar bl1_plat_set_ep_info(image_id, next_bl_ep); 107f3b4914bSYatharth Kochar 108f3b4914bSYatharth Kochar /* Prepare the context for the next BL image. */ 109f3b4914bSYatharth Kochar cm_init_my_context(next_bl_ep); 110f3b4914bSYatharth Kochar cm_prepare_el3_exit(security_state); 111f3b4914bSYatharth Kochar 112f3b4914bSYatharth Kochar /* Indicate that image is in execution state. */ 1133443a702SJohn Powell desc->state = IMAGE_STATE_EXECUTED; 114f3b4914bSYatharth Kochar 115f3b4914bSYatharth Kochar print_entry_point_info(next_bl_ep); 116f3b4914bSYatharth Kochar } 1176c09af9fSZelalem Aweke #endif /* ENABLE_RME */ 118