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 3197043ac9SDan Handley #include <arch.h> 324f6ad66aSAchin Gupta #include <arch_helpers.h> 3397043ac9SDan Handley #include <assert.h> 344f6ad66aSAchin Gupta #include <bl_common.h> 354f6ad66aSAchin Gupta #include <bl31.h> 367aea9087SAchin Gupta #include <context_mgmt.h> 37dec5e0d1SDan Handley #include <platform.h> 3897043ac9SDan Handley #include <runtime_svc.h> 3997043ac9SDan Handley #include <stdio.h> 40*f05cb4a7SVikram Kanigiri #include <string.h> 417aea9087SAchin Gupta 427f366605SJeenu Viswambharan /******************************************************************************* 437f366605SJeenu Viswambharan * This function pointer is used to initialise the BL32 image. It's initialized 447f366605SJeenu Viswambharan * by SPD calling bl31_register_bl32_init after setting up all things necessary 457f366605SJeenu Viswambharan * for SP execution. In cases where both SPD and SP are absent, or when SPD 467f366605SJeenu Viswambharan * finds it impossible to execute SP, this pointer is left as NULL 477f366605SJeenu Viswambharan ******************************************************************************/ 486871c5d3SVikram Kanigiri static int32_t (*bl32_init)(void); 497f366605SJeenu Viswambharan 507aea9087SAchin Gupta /******************************************************************************* 5135ca3511SAchin Gupta * Variable to indicate whether next image to execute after BL31 is BL33 5235ca3511SAchin Gupta * (non-secure & default) or BL32 (secure). 5335ca3511SAchin Gupta ******************************************************************************/ 54625de1d4SDan Handley static uint32_t next_image_type; 5535ca3511SAchin Gupta 5635ca3511SAchin Gupta /******************************************************************************* 577aea9087SAchin Gupta * Simple function to initialise all BL31 helper libraries. 587aea9087SAchin Gupta ******************************************************************************/ 597aea9087SAchin Gupta void bl31_lib_init() 607aea9087SAchin Gupta { 617aea9087SAchin Gupta cm_init(); 627aea9087SAchin Gupta } 634f6ad66aSAchin Gupta 644f6ad66aSAchin Gupta /******************************************************************************* 654f6ad66aSAchin Gupta * BL31 is responsible for setting up the runtime services for the primary cpu 6635ca3511SAchin Gupta * before passing control to the bootloader or an Operating System. This 6735ca3511SAchin Gupta * function calls runtime_svc_init() which initializes all registered runtime 6835ca3511SAchin Gupta * services. The run time services would setup enough context for the core to 6935ca3511SAchin Gupta * swtich to the next exception level. When this function returns, the core will 7035ca3511SAchin Gupta * switch to the programmed exception level via. an ERET. 714f6ad66aSAchin Gupta ******************************************************************************/ 724f6ad66aSAchin Gupta void bl31_main(void) 734f6ad66aSAchin Gupta { 7435ca3511SAchin Gupta #if DEBUG 7535ca3511SAchin Gupta unsigned long mpidr = read_mpidr(); 7635ca3511SAchin Gupta #endif 774f6ad66aSAchin Gupta 784f6ad66aSAchin Gupta /* Perform remaining generic architectural setup from EL3 */ 794f6ad66aSAchin Gupta bl31_arch_setup(); 804f6ad66aSAchin Gupta 814f6ad66aSAchin Gupta /* Perform platform setup in BL1 */ 824f6ad66aSAchin Gupta bl31_platform_setup(); 834f6ad66aSAchin Gupta 84fb052462SJon Medhurst printf("BL31 %s\n\r", build_message); 85fb052462SJon Medhurst 867aea9087SAchin Gupta /* Initialise helper libraries */ 877aea9087SAchin Gupta bl31_lib_init(); 884f6ad66aSAchin Gupta 894f6ad66aSAchin Gupta /* Initialize the runtime services e.g. psci */ 907421b465SAchin Gupta runtime_svc_init(); 914f6ad66aSAchin Gupta 924f6ad66aSAchin Gupta /* Clean caches before re-entering normal world */ 934f6ad66aSAchin Gupta dcsw_op_all(DCCSW); 944f6ad66aSAchin Gupta 95caa84939SJeenu Viswambharan /* 9635ca3511SAchin Gupta * Use the more complex exception vectors now that context 9735ca3511SAchin Gupta * management is setup. SP_EL3 should point to a 'cpu_context' 9835ca3511SAchin Gupta * structure which has an exception stack allocated. The PSCI 9935ca3511SAchin Gupta * service should have set the context. 10035ca3511SAchin Gupta */ 10135ca3511SAchin Gupta assert(cm_get_context(mpidr, NON_SECURE)); 10235ca3511SAchin Gupta cm_set_next_eret_context(NON_SECURE); 103a43d431bSSoby Mathew cm_init_pcpu_ptr_cache(); 10435ca3511SAchin Gupta write_vbar_el3((uint64_t) runtime_exceptions); 1058cec598bSAndrew Thoelke isb(); 106625de1d4SDan Handley next_image_type = NON_SECURE; 10735ca3511SAchin Gupta 10835ca3511SAchin Gupta /* 1097f366605SJeenu Viswambharan * All the cold boot actions on the primary cpu are done. We now need to 1107f366605SJeenu Viswambharan * decide which is the next image (BL32 or BL33) and how to execute it. 1117f366605SJeenu Viswambharan * If the SPD runtime service is present, it would want to pass control 1127f366605SJeenu Viswambharan * to BL32 first in S-EL1. In that case, SPD would have registered a 1137f366605SJeenu Viswambharan * function to intialize bl32 where it takes responsibility of entering 1147f366605SJeenu Viswambharan * S-EL1 and returning control back to bl31_main. Once this is done we 1157f366605SJeenu Viswambharan * can prepare entry into BL33 as normal. 11635ca3511SAchin Gupta */ 11735ca3511SAchin Gupta 1187f366605SJeenu Viswambharan /* 1196871c5d3SVikram Kanigiri * If SPD had registerd an init hook, invoke it. 1207f366605SJeenu Viswambharan */ 12135ca3511SAchin Gupta if (bl32_init) 1226871c5d3SVikram Kanigiri (*bl32_init)(); 12335ca3511SAchin Gupta 12435ca3511SAchin Gupta /* 12535ca3511SAchin Gupta * We are ready to enter the next EL. Prepare entry into the image 12635ca3511SAchin Gupta * corresponding to the desired security state after the next ERET. 12735ca3511SAchin Gupta */ 12835ca3511SAchin Gupta bl31_prepare_next_image_entry(); 12935ca3511SAchin Gupta } 13035ca3511SAchin Gupta 13135ca3511SAchin Gupta /******************************************************************************* 13235ca3511SAchin Gupta * Accessor functions to help runtime services decide which image should be 13335ca3511SAchin Gupta * executed after BL31. This is BL33 or the non-secure bootloader image by 13435ca3511SAchin Gupta * default but the Secure payload dispatcher could override this by requesting 13535ca3511SAchin Gupta * an entry into BL32 (Secure payload) first. If it does so then it should use 13635ca3511SAchin Gupta * the same API to program an entry into BL33 once BL32 initialisation is 13735ca3511SAchin Gupta * complete. 13835ca3511SAchin Gupta ******************************************************************************/ 13935ca3511SAchin Gupta void bl31_set_next_image_type(uint32_t security_state) 14035ca3511SAchin Gupta { 14135ca3511SAchin Gupta assert(security_state == NON_SECURE || security_state == SECURE); 14235ca3511SAchin Gupta next_image_type = security_state; 14335ca3511SAchin Gupta } 14435ca3511SAchin Gupta 14535ca3511SAchin Gupta uint32_t bl31_get_next_image_type(void) 14635ca3511SAchin Gupta { 14735ca3511SAchin Gupta return next_image_type; 14835ca3511SAchin Gupta } 14935ca3511SAchin Gupta 15035ca3511SAchin Gupta /******************************************************************************* 15135ca3511SAchin Gupta * This function programs EL3 registers and performs other setup to enable entry 15235ca3511SAchin Gupta * into the next image after BL31 at the next ERET. 15335ca3511SAchin Gupta ******************************************************************************/ 15435ca3511SAchin Gupta void bl31_prepare_next_image_entry() 15535ca3511SAchin Gupta { 1564112bfa0SVikram Kanigiri entry_point_info_t *next_image_info; 15735ca3511SAchin Gupta uint32_t scr, image_type; 158*f05cb4a7SVikram Kanigiri cpu_context_t *ctx; 159*f05cb4a7SVikram Kanigiri gp_regs_t *gp_regs; 16035ca3511SAchin Gupta 16135ca3511SAchin Gupta /* Determine which image to execute next */ 16235ca3511SAchin Gupta image_type = bl31_get_next_image_type(); 16335ca3511SAchin Gupta 16435ca3511SAchin Gupta /* 165caa84939SJeenu Viswambharan * Setup minimal architectural state of the next highest EL to 166caa84939SJeenu Viswambharan * allow execution in it immediately upon entering it. 167caa84939SJeenu Viswambharan */ 16835ca3511SAchin Gupta bl31_next_el_arch_setup(image_type); 1694f6ad66aSAchin Gupta 170caa84939SJeenu Viswambharan /* Program EL3 registers to enable entry into the next EL */ 1719865ac15SDan Handley next_image_info = bl31_plat_get_next_image_ep_info(image_type); 17235ca3511SAchin Gupta assert(next_image_info); 173*f05cb4a7SVikram Kanigiri assert(image_type == GET_SECURITY_STATE(next_image_info->h.attr)); 17435ca3511SAchin Gupta 175caa84939SJeenu Viswambharan scr = read_scr(); 176bb5ffdbaSAndrew Thoelke scr &= ~SCR_NS_BIT; 17735ca3511SAchin Gupta if (image_type == NON_SECURE) 178caa84939SJeenu Viswambharan scr |= SCR_NS_BIT; 179caa84939SJeenu Viswambharan 180bb5ffdbaSAndrew Thoelke scr &= ~SCR_RW_BIT; 181bb5ffdbaSAndrew Thoelke if ((next_image_info->spsr & (1 << MODE_RW_SHIFT)) == 182bb5ffdbaSAndrew Thoelke (MODE_RW_64 << MODE_RW_SHIFT)) 183bb5ffdbaSAndrew Thoelke scr |= SCR_RW_BIT; 184bb5ffdbaSAndrew Thoelke 185caa84939SJeenu Viswambharan /* 186caa84939SJeenu Viswambharan * Tell the context mgmt. library to ensure that SP_EL3 points to 187caa84939SJeenu Viswambharan * the right context to exit from EL3 correctly. 188caa84939SJeenu Viswambharan */ 189*f05cb4a7SVikram Kanigiri cm_set_el3_eret_context(image_type, 1904112bfa0SVikram Kanigiri next_image_info->pc, 191caa84939SJeenu Viswambharan next_image_info->spsr, 192caa84939SJeenu Viswambharan scr); 193caa84939SJeenu Viswambharan 194*f05cb4a7SVikram Kanigiri /* 195*f05cb4a7SVikram Kanigiri * Save the args generated in BL2 for the image in the right context 196*f05cb4a7SVikram Kanigiri * used on its entry 197*f05cb4a7SVikram Kanigiri */ 198*f05cb4a7SVikram Kanigiri ctx = cm_get_context(read_mpidr(), image_type); 199*f05cb4a7SVikram Kanigiri gp_regs = get_gpregs_ctx(ctx); 200*f05cb4a7SVikram Kanigiri memcpy(gp_regs, (void *)&next_image_info->args, sizeof(aapcs64_params_t)); 201*f05cb4a7SVikram Kanigiri 202caa84939SJeenu Viswambharan /* Finally set the next context */ 203*f05cb4a7SVikram Kanigiri cm_set_next_eret_context(image_type); 2044f6ad66aSAchin Gupta } 2057f366605SJeenu Viswambharan 2067f366605SJeenu Viswambharan /******************************************************************************* 2077f366605SJeenu Viswambharan * This function initializes the pointer to BL32 init function. This is expected 2087f366605SJeenu Viswambharan * to be called by the SPD after it finishes all its initialization 2097f366605SJeenu Viswambharan ******************************************************************************/ 2106871c5d3SVikram Kanigiri void bl31_register_bl32_init(int32_t (*func)(void)) 2117f366605SJeenu Viswambharan { 2127f366605SJeenu Viswambharan bl32_init = func; 2137f366605SJeenu Viswambharan } 214