14f6ad66aSAchin Gupta /* 2e83b0cadSDan Handley * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved. 34f6ad66aSAchin Gupta * 44f6ad66aSAchin Gupta * Redistribution and use in source and binary forms, with or without 54f6ad66aSAchin Gupta * modification, are permitted provided that the following conditions are met: 64f6ad66aSAchin Gupta * 74f6ad66aSAchin Gupta * Redistributions of source code must retain the above copyright notice, this 84f6ad66aSAchin Gupta * list of conditions and the following disclaimer. 94f6ad66aSAchin Gupta * 104f6ad66aSAchin Gupta * Redistributions in binary form must reproduce the above copyright notice, 114f6ad66aSAchin Gupta * this list of conditions and the following disclaimer in the documentation 124f6ad66aSAchin Gupta * and/or other materials provided with the distribution. 134f6ad66aSAchin Gupta * 144f6ad66aSAchin Gupta * Neither the name of ARM nor the names of its contributors may be used 154f6ad66aSAchin Gupta * to endorse or promote products derived from this software without specific 164f6ad66aSAchin Gupta * prior written permission. 174f6ad66aSAchin Gupta * 184f6ad66aSAchin Gupta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 194f6ad66aSAchin Gupta * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 204f6ad66aSAchin Gupta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 214f6ad66aSAchin Gupta * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 224f6ad66aSAchin Gupta * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 234f6ad66aSAchin Gupta * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 244f6ad66aSAchin Gupta * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 254f6ad66aSAchin Gupta * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 264f6ad66aSAchin Gupta * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 274f6ad66aSAchin Gupta * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 284f6ad66aSAchin Gupta * POSSIBILITY OF SUCH DAMAGE. 294f6ad66aSAchin Gupta */ 304f6ad66aSAchin Gupta 314f6ad66aSAchin Gupta #include <stdio.h> 324f6ad66aSAchin Gupta #include <string.h> 334f6ad66aSAchin Gupta #include <assert.h> 344f6ad66aSAchin Gupta #include <arch_helpers.h> 354f6ad66aSAchin Gupta #include <console.h> 364f6ad66aSAchin Gupta #include <platform.h> 374f6ad66aSAchin Gupta #include <semihosting.h> 384f6ad66aSAchin Gupta #include <bl_common.h> 394f6ad66aSAchin Gupta #include <bl31.h> 404f6ad66aSAchin Gupta #include <runtime_svc.h> 417aea9087SAchin Gupta #include <context_mgmt.h> 427aea9087SAchin Gupta 437aea9087SAchin Gupta /******************************************************************************* 447aea9087SAchin Gupta * Simple function to initialise all BL31 helper libraries. 457aea9087SAchin Gupta ******************************************************************************/ 467aea9087SAchin Gupta void bl31_lib_init() 477aea9087SAchin Gupta { 487aea9087SAchin Gupta cm_init(); 497aea9087SAchin Gupta } 504f6ad66aSAchin Gupta 514f6ad66aSAchin Gupta /******************************************************************************* 524f6ad66aSAchin Gupta * BL31 is responsible for setting up the runtime services for the primary cpu 53*caa84939SJeenu Viswambharan * before passing control to the bootloader (UEFI) or Linux. This function calls 54*caa84939SJeenu Viswambharan * runtime_svc_init() which initializes all registered runtime services. The run 55*caa84939SJeenu Viswambharan * time services would setup enough context for the core to swtich to the next 56*caa84939SJeenu Viswambharan * exception level. When this function returns, the core will switch to the 57*caa84939SJeenu Viswambharan * programmed exception level via. an ERET. 584f6ad66aSAchin Gupta ******************************************************************************/ 594f6ad66aSAchin Gupta void bl31_main(void) 604f6ad66aSAchin Gupta { 61*caa84939SJeenu Viswambharan el_change_info *next_image_info; 62*caa84939SJeenu Viswambharan uint32_t scr; 634f6ad66aSAchin Gupta 644f6ad66aSAchin Gupta /* Perform remaining generic architectural setup from EL3 */ 654f6ad66aSAchin Gupta bl31_arch_setup(); 664f6ad66aSAchin Gupta 674f6ad66aSAchin Gupta /* Perform platform setup in BL1 */ 684f6ad66aSAchin Gupta bl31_platform_setup(); 694f6ad66aSAchin Gupta 704f6ad66aSAchin Gupta #if defined (__GNUC__) 714f6ad66aSAchin Gupta printf("BL31 Built : %s, %s\n\r", __TIME__, __DATE__); 724f6ad66aSAchin Gupta #endif 737aea9087SAchin Gupta /* Initialise helper libraries */ 747aea9087SAchin Gupta bl31_lib_init(); 754f6ad66aSAchin Gupta 764f6ad66aSAchin Gupta /* Initialize the runtime services e.g. psci */ 777421b465SAchin Gupta runtime_svc_init(); 784f6ad66aSAchin Gupta 794f6ad66aSAchin Gupta /* Clean caches before re-entering normal world */ 804f6ad66aSAchin Gupta dcsw_op_all(DCCSW); 814f6ad66aSAchin Gupta 82*caa84939SJeenu Viswambharan /* 83*caa84939SJeenu Viswambharan * Setup minimal architectural state of the next highest EL to 84*caa84939SJeenu Viswambharan * allow execution in it immediately upon entering it. 85*caa84939SJeenu Viswambharan */ 864f6ad66aSAchin Gupta bl31_arch_next_el_setup(); 874f6ad66aSAchin Gupta 88*caa84939SJeenu Viswambharan /* Program EL3 registers to enable entry into the next EL */ 89*caa84939SJeenu Viswambharan next_image_info = bl31_get_next_image_info(); 90*caa84939SJeenu Viswambharan scr = read_scr(); 91*caa84939SJeenu Viswambharan if (next_image_info->security_state == NON_SECURE) 92*caa84939SJeenu Viswambharan scr |= SCR_NS_BIT; 93*caa84939SJeenu Viswambharan 94*caa84939SJeenu Viswambharan /* 95*caa84939SJeenu Viswambharan * Tell the context mgmt. library to ensure that SP_EL3 points to 96*caa84939SJeenu Viswambharan * the right context to exit from EL3 correctly. 97*caa84939SJeenu Viswambharan */ 98*caa84939SJeenu Viswambharan cm_set_el3_eret_context(next_image_info->security_state, 99*caa84939SJeenu Viswambharan next_image_info->entrypoint, 100*caa84939SJeenu Viswambharan next_image_info->spsr, 101*caa84939SJeenu Viswambharan scr); 102*caa84939SJeenu Viswambharan 103*caa84939SJeenu Viswambharan /* Finally set the next context */ 104*caa84939SJeenu Viswambharan cm_set_next_eret_context(next_image_info->security_state); 1054f6ad66aSAchin Gupta } 106