14f6ad66aSAchin Gupta /* 28aabea33SPaul Beesley * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved. 34f6ad66aSAchin Gupta * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 54f6ad66aSAchin Gupta */ 64f6ad66aSAchin Gupta 709d40e0eSAntonio Nino Diaz #include <assert.h> 809d40e0eSAntonio Nino Diaz #include <string.h> 909d40e0eSAntonio Nino Diaz 1097043ac9SDan Handley #include <arch.h> 11*ed108b56SAlexei Fedorov #include <arch_features.h> 124f6ad66aSAchin Gupta #include <arch_helpers.h> 1309d40e0eSAntonio Nino Diaz #include <bl31/bl31.h> 1409d40e0eSAntonio Nino Diaz #include <bl31/ehf.h> 1509d40e0eSAntonio Nino Diaz #include <common/bl_common.h> 1609d40e0eSAntonio Nino Diaz #include <common/debug.h> 1709d40e0eSAntonio Nino Diaz #include <common/runtime_svc.h> 1809d40e0eSAntonio Nino Diaz #include <drivers/console.h> 1909d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/context_mgmt.h> 2009d40e0eSAntonio Nino Diaz #include <lib/pmf/pmf.h> 2109d40e0eSAntonio Nino Diaz #include <lib/runtime_instr.h> 2209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2309d40e0eSAntonio Nino Diaz #include <services/std_svc.h> 247aea9087SAchin Gupta 25872be88aSdp-arm #if ENABLE_RUNTIME_INSTRUMENTATION 26872be88aSdp-arm PMF_REGISTER_SERVICE_SMC(rt_instr_svc, PMF_RT_INSTR_SVC_ID, 27872be88aSdp-arm RT_INSTR_TOTAL_IDS, PMF_STORE_ENABLE) 28872be88aSdp-arm #endif 29872be88aSdp-arm 307f366605SJeenu Viswambharan /******************************************************************************* 317f366605SJeenu Viswambharan * This function pointer is used to initialise the BL32 image. It's initialized 327f366605SJeenu Viswambharan * by SPD calling bl31_register_bl32_init after setting up all things necessary 337f366605SJeenu Viswambharan * for SP execution. In cases where both SPD and SP are absent, or when SPD 347f366605SJeenu Viswambharan * finds it impossible to execute SP, this pointer is left as NULL 357f366605SJeenu Viswambharan ******************************************************************************/ 366871c5d3SVikram Kanigiri static int32_t (*bl32_init)(void); 377f366605SJeenu Viswambharan 387aea9087SAchin Gupta /******************************************************************************* 3935ca3511SAchin Gupta * Variable to indicate whether next image to execute after BL31 is BL33 4035ca3511SAchin Gupta * (non-secure & default) or BL32 (secure). 4135ca3511SAchin Gupta ******************************************************************************/ 42faaa2e76SVikram Kanigiri static uint32_t next_image_type = NON_SECURE; 4335ca3511SAchin Gupta 4458e946aeSSoby Mathew /* 4558e946aeSSoby Mathew * Implement the ARM Standard Service function to get arguments for a 4658e946aeSSoby Mathew * particular service. 4758e946aeSSoby Mathew */ 4858e946aeSSoby Mathew uintptr_t get_arm_std_svc_args(unsigned int svc_mask) 4958e946aeSSoby Mathew { 5058e946aeSSoby Mathew /* Setup the arguments for PSCI Library */ 5158e946aeSSoby Mathew DEFINE_STATIC_PSCI_LIB_ARGS_V1(psci_args, bl31_warm_entrypoint); 5258e946aeSSoby Mathew 5358e946aeSSoby Mathew /* PSCI is the only ARM Standard Service implemented */ 5458e946aeSSoby Mathew assert(svc_mask == PSCI_FID_MASK); 5558e946aeSSoby Mathew 5658e946aeSSoby Mathew return (uintptr_t)&psci_args; 5758e946aeSSoby Mathew } 5858e946aeSSoby Mathew 5935ca3511SAchin Gupta /******************************************************************************* 607aea9087SAchin Gupta * Simple function to initialise all BL31 helper libraries. 617aea9087SAchin Gupta ******************************************************************************/ 6287c85134SDaniel Boulby void __init bl31_lib_init(void) 637aea9087SAchin Gupta { 647aea9087SAchin Gupta cm_init(); 657aea9087SAchin Gupta } 664f6ad66aSAchin Gupta 674f6ad66aSAchin Gupta /******************************************************************************* 6888cfd9a6SAntonio Nino Diaz * Setup function for BL31. 6988cfd9a6SAntonio Nino Diaz ******************************************************************************/ 7088cfd9a6SAntonio Nino Diaz void bl31_setup(u_register_t arg0, u_register_t arg1, u_register_t arg2, 7188cfd9a6SAntonio Nino Diaz u_register_t arg3) 7288cfd9a6SAntonio Nino Diaz { 7388cfd9a6SAntonio Nino Diaz /* Perform early platform-specific setup */ 7488cfd9a6SAntonio Nino Diaz bl31_early_platform_setup2(arg0, arg1, arg2, arg3); 7588cfd9a6SAntonio Nino Diaz 7688cfd9a6SAntonio Nino Diaz /* Perform late platform-specific setup */ 7788cfd9a6SAntonio Nino Diaz bl31_plat_arch_setup(); 78*ed108b56SAlexei Fedorov 79*ed108b56SAlexei Fedorov #if CTX_INCLUDE_PAUTH_REGS 80*ed108b56SAlexei Fedorov /* 81*ed108b56SAlexei Fedorov * Assert that the ARMv8.3-PAuth registers are present or an access 82*ed108b56SAlexei Fedorov * fault will be triggered when they are being saved or restored. 83*ed108b56SAlexei Fedorov */ 84*ed108b56SAlexei Fedorov assert(is_armv8_3_pauth_present()); 85*ed108b56SAlexei Fedorov #endif /* CTX_INCLUDE_PAUTH_REGS */ 8688cfd9a6SAntonio Nino Diaz } 8788cfd9a6SAntonio Nino Diaz 8888cfd9a6SAntonio Nino Diaz /******************************************************************************* 894f6ad66aSAchin Gupta * BL31 is responsible for setting up the runtime services for the primary cpu 9035ca3511SAchin Gupta * before passing control to the bootloader or an Operating System. This 9135ca3511SAchin Gupta * function calls runtime_svc_init() which initializes all registered runtime 9235ca3511SAchin Gupta * services. The run time services would setup enough context for the core to 938aabea33SPaul Beesley * switch to the next exception level. When this function returns, the core will 9473308618SAntonio Nino Diaz * switch to the programmed exception level via an ERET. 954f6ad66aSAchin Gupta ******************************************************************************/ 964f6ad66aSAchin Gupta void bl31_main(void) 974f6ad66aSAchin Gupta { 98d178637dSJuan Castillo NOTICE("BL31: %s\n", version_string); 99d178637dSJuan Castillo NOTICE("BL31: %s\n", build_message); 1006ad2e461SDan Handley 10178e61613SSoby Mathew /* Perform platform setup in BL31 */ 1024f6ad66aSAchin Gupta bl31_platform_setup(); 1034f6ad66aSAchin Gupta 1047aea9087SAchin Gupta /* Initialise helper libraries */ 1057aea9087SAchin Gupta bl31_lib_init(); 1064f6ad66aSAchin Gupta 10721b818c0SJeenu Viswambharan #if EL3_EXCEPTION_HANDLING 10821b818c0SJeenu Viswambharan INFO("BL31: Initialising Exception Handling Framework\n"); 10921b818c0SJeenu Viswambharan ehf_init(); 11021b818c0SJeenu Viswambharan #endif 11121b818c0SJeenu Viswambharan 11258e946aeSSoby Mathew /* Initialize the runtime services e.g. psci. */ 113d178637dSJuan Castillo INFO("BL31: Initializing runtime services\n"); 1147421b465SAchin Gupta runtime_svc_init(); 1154f6ad66aSAchin Gupta 11635ca3511SAchin Gupta /* 1177f366605SJeenu Viswambharan * All the cold boot actions on the primary cpu are done. We now need to 1187f366605SJeenu Viswambharan * decide which is the next image (BL32 or BL33) and how to execute it. 1197f366605SJeenu Viswambharan * If the SPD runtime service is present, it would want to pass control 1207f366605SJeenu Viswambharan * to BL32 first in S-EL1. In that case, SPD would have registered a 1218aabea33SPaul Beesley * function to initialize bl32 where it takes responsibility of entering 1227f366605SJeenu Viswambharan * S-EL1 and returning control back to bl31_main. Once this is done we 1237f366605SJeenu Viswambharan * can prepare entry into BL33 as normal. 12435ca3511SAchin Gupta */ 12535ca3511SAchin Gupta 1267f366605SJeenu Viswambharan /* 1278aabea33SPaul Beesley * If SPD had registered an init hook, invoke it. 1287f366605SJeenu Viswambharan */ 129c9512bcaSAntonio Nino Diaz if (bl32_init != NULL) { 130d178637dSJuan Castillo INFO("BL31: Initializing BL32\n"); 131c9512bcaSAntonio Nino Diaz 132c9512bcaSAntonio Nino Diaz int32_t rc = (*bl32_init)(); 133c9512bcaSAntonio Nino Diaz 13474ad948fSAntonio Nino Diaz if (rc == 0) 13574ad948fSAntonio Nino Diaz WARN("BL31: BL32 initialization failed\n"); 1366ad2e461SDan Handley } 13735ca3511SAchin Gupta /* 13835ca3511SAchin Gupta * We are ready to enter the next EL. Prepare entry into the image 13935ca3511SAchin Gupta * corresponding to the desired security state after the next ERET. 14035ca3511SAchin Gupta */ 14135ca3511SAchin Gupta bl31_prepare_next_image_entry(); 14278e61613SSoby Mathew 1430b32628eSAntonio Nino Diaz console_flush(); 1440b32628eSAntonio Nino Diaz 14578e61613SSoby Mathew /* 14678e61613SSoby Mathew * Perform any platform specific runtime setup prior to cold boot exit 14778e61613SSoby Mathew * from BL31 14878e61613SSoby Mathew */ 14978e61613SSoby Mathew bl31_plat_runtime_setup(); 15035ca3511SAchin Gupta } 15135ca3511SAchin Gupta 15235ca3511SAchin Gupta /******************************************************************************* 15335ca3511SAchin Gupta * Accessor functions to help runtime services decide which image should be 15435ca3511SAchin Gupta * executed after BL31. This is BL33 or the non-secure bootloader image by 15535ca3511SAchin Gupta * default but the Secure payload dispatcher could override this by requesting 15635ca3511SAchin Gupta * an entry into BL32 (Secure payload) first. If it does so then it should use 15735ca3511SAchin Gupta * the same API to program an entry into BL33 once BL32 initialisation is 15835ca3511SAchin Gupta * complete. 15935ca3511SAchin Gupta ******************************************************************************/ 16035ca3511SAchin Gupta void bl31_set_next_image_type(uint32_t security_state) 16135ca3511SAchin Gupta { 162d3280bebSJuan Castillo assert(sec_state_is_valid(security_state)); 16335ca3511SAchin Gupta next_image_type = security_state; 16435ca3511SAchin Gupta } 16535ca3511SAchin Gupta 16635ca3511SAchin Gupta uint32_t bl31_get_next_image_type(void) 16735ca3511SAchin Gupta { 16835ca3511SAchin Gupta return next_image_type; 16935ca3511SAchin Gupta } 17035ca3511SAchin Gupta 17135ca3511SAchin Gupta /******************************************************************************* 17235ca3511SAchin Gupta * This function programs EL3 registers and performs other setup to enable entry 17335ca3511SAchin Gupta * into the next image after BL31 at the next ERET. 17435ca3511SAchin Gupta ******************************************************************************/ 17587c85134SDaniel Boulby void __init bl31_prepare_next_image_entry(void) 17635ca3511SAchin Gupta { 1774112bfa0SVikram Kanigiri entry_point_info_t *next_image_info; 178167a9357SAndrew Thoelke uint32_t image_type; 17935ca3511SAchin Gupta 1808cd16e6bSSoby Mathew #if CTX_INCLUDE_AARCH32_REGS 1818cd16e6bSSoby Mathew /* 1828cd16e6bSSoby Mathew * Ensure that the build flag to save AArch32 system registers in CPU 1838cd16e6bSSoby Mathew * context is not set for AArch64-only platforms. 1848cd16e6bSSoby Mathew */ 185a0fee747SAntonio Nino Diaz if (el_implemented(1) == EL_IMPL_A64ONLY) { 1868cd16e6bSSoby Mathew ERROR("EL1 supports AArch64-only. Please set build flag " 187a0fee747SAntonio Nino Diaz "CTX_INCLUDE_AARCH32_REGS = 0\n"); 1888cd16e6bSSoby Mathew panic(); 1898cd16e6bSSoby Mathew } 1908cd16e6bSSoby Mathew #endif 1918cd16e6bSSoby Mathew 19235ca3511SAchin Gupta /* Determine which image to execute next */ 19335ca3511SAchin Gupta image_type = bl31_get_next_image_type(); 19435ca3511SAchin Gupta 195caa84939SJeenu Viswambharan /* Program EL3 registers to enable entry into the next EL */ 1969865ac15SDan Handley next_image_info = bl31_plat_get_next_image_ep_info(image_type); 197c9512bcaSAntonio Nino Diaz assert(next_image_info != NULL); 198f05cb4a7SVikram Kanigiri assert(image_type == GET_SECURITY_STATE(next_image_info->h.attr)); 19935ca3511SAchin Gupta 200d178637dSJuan Castillo INFO("BL31: Preparing for EL3 exit to %s world\n", 2016ad2e461SDan Handley (image_type == SECURE) ? "secure" : "normal"); 20268a68c92SSandrine Bailleux print_entry_point_info(next_image_info); 20385a181ceSSoby Mathew cm_init_my_context(next_image_info); 204167a9357SAndrew Thoelke cm_prepare_el3_exit(image_type); 2054f6ad66aSAchin Gupta } 2067f366605SJeenu Viswambharan 2077f366605SJeenu Viswambharan /******************************************************************************* 2087f366605SJeenu Viswambharan * This function initializes the pointer to BL32 init function. This is expected 2097f366605SJeenu Viswambharan * to be called by the SPD after it finishes all its initialization 2107f366605SJeenu Viswambharan ******************************************************************************/ 2116871c5d3SVikram Kanigiri void bl31_register_bl32_init(int32_t (*func)(void)) 2127f366605SJeenu Viswambharan { 2137f366605SJeenu Viswambharan bl32_init = func; 2147f366605SJeenu Viswambharan } 215