1bc149bfcSSoby Mathew /* 21af540efSRoberto Vargas * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved. 3bc149bfcSSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5bc149bfcSSoby Mathew */ 6*09d40e0eSAntonio Nino Diaz 7*09d40e0eSAntonio Nino Diaz #include <assert.h> 8*09d40e0eSAntonio Nino Diaz 9*09d40e0eSAntonio Nino Diaz #include <platform_def.h> 10*09d40e0eSAntonio Nino Diaz 11bc149bfcSSoby Mathew #include <arch.h> 12bc149bfcSSoby Mathew #include <arch_helpers.h> 13*09d40e0eSAntonio Nino Diaz #include <common/debug.h> 14*09d40e0eSAntonio Nino Diaz #include <common/romlib.h> 15*09d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 16*09d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h> 17*09d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 18*09d40e0eSAntonio Nino Diaz #include <services/secure_partition.h> 19*09d40e0eSAntonio Nino Diaz 20bc149bfcSSoby Mathew #include <plat_arm.h> 21bc149bfcSSoby Mathew 22bc149bfcSSoby Mathew /* Weak definitions may be overridden in specific ARM standard platform */ 23bc149bfcSSoby Mathew #pragma weak plat_get_ns_image_entrypoint 24bc149bfcSSoby Mathew #pragma weak plat_arm_get_mmap 25bc149bfcSSoby Mathew 26bc149bfcSSoby Mathew /* Conditionally provide a weak definition of plat_get_syscnt_freq2 to avoid 27bc149bfcSSoby Mathew * conflicts with the definition in plat/common. */ 28bc149bfcSSoby Mathew #pragma weak plat_get_syscnt_freq2 29bc149bfcSSoby Mathew 301eb735d7SRoberto Vargas 311eb735d7SRoberto Vargas void arm_setup_romlib(void) 321eb735d7SRoberto Vargas { 331eb735d7SRoberto Vargas #if USE_ROMLIB 341eb735d7SRoberto Vargas if (!rom_lib_init(ROMLIB_VERSION)) 351eb735d7SRoberto Vargas panic(); 361eb735d7SRoberto Vargas #endif 371eb735d7SRoberto Vargas } 381eb735d7SRoberto Vargas 39bc149bfcSSoby Mathew uintptr_t plat_get_ns_image_entrypoint(void) 40bc149bfcSSoby Mathew { 4148ac1df9SSoby Mathew #ifdef PRELOADED_BL33_BASE 4248ac1df9SSoby Mathew return PRELOADED_BL33_BASE; 4348ac1df9SSoby Mathew #else 44bc149bfcSSoby Mathew return PLAT_ARM_NS_IMAGE_OFFSET; 4548ac1df9SSoby Mathew #endif 46bc149bfcSSoby Mathew } 47bc149bfcSSoby Mathew 48bc149bfcSSoby Mathew /******************************************************************************* 49bc149bfcSSoby Mathew * Gets SPSR for BL32 entry 50bc149bfcSSoby Mathew ******************************************************************************/ 51bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl32_entry(void) 52bc149bfcSSoby Mathew { 53bc149bfcSSoby Mathew /* 54bc149bfcSSoby Mathew * The Secure Payload Dispatcher service is responsible for 55bc149bfcSSoby Mathew * setting the SPSR prior to entry into the BL32 image. 56bc149bfcSSoby Mathew */ 57bc149bfcSSoby Mathew return 0; 58bc149bfcSSoby Mathew } 59bc149bfcSSoby Mathew 60bc149bfcSSoby Mathew /******************************************************************************* 61bc149bfcSSoby Mathew * Gets SPSR for BL33 entry 62bc149bfcSSoby Mathew ******************************************************************************/ 63877cf3ffSSoby Mathew #ifndef AARCH32 64bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void) 65bc149bfcSSoby Mathew { 66bc149bfcSSoby Mathew unsigned int mode; 67bc149bfcSSoby Mathew uint32_t spsr; 68bc149bfcSSoby Mathew 69bc149bfcSSoby Mathew /* Figure out what mode we enter the non-secure world in */ 70a0fee747SAntonio Nino Diaz mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1; 71bc149bfcSSoby Mathew 72bc149bfcSSoby Mathew /* 73bc149bfcSSoby Mathew * TODO: Consider the possibility of specifying the SPSR in 74bc149bfcSSoby Mathew * the FIP ToC and allowing the platform to have a say as 75bc149bfcSSoby Mathew * well. 76bc149bfcSSoby Mathew */ 77bc149bfcSSoby Mathew spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 78bc149bfcSSoby Mathew return spsr; 79bc149bfcSSoby Mathew } 80877cf3ffSSoby Mathew #else 81877cf3ffSSoby Mathew /******************************************************************************* 82877cf3ffSSoby Mathew * Gets SPSR for BL33 entry 83877cf3ffSSoby Mathew ******************************************************************************/ 84877cf3ffSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void) 85877cf3ffSSoby Mathew { 86877cf3ffSSoby Mathew unsigned int hyp_status, mode, spsr; 87877cf3ffSSoby Mathew 88877cf3ffSSoby Mathew hyp_status = GET_VIRT_EXT(read_id_pfr1()); 89877cf3ffSSoby Mathew 90877cf3ffSSoby Mathew mode = (hyp_status) ? MODE32_hyp : MODE32_svc; 91877cf3ffSSoby Mathew 92877cf3ffSSoby Mathew /* 93877cf3ffSSoby Mathew * TODO: Consider the possibility of specifying the SPSR in 94877cf3ffSSoby Mathew * the FIP ToC and allowing the platform to have a say as 95877cf3ffSSoby Mathew * well. 96877cf3ffSSoby Mathew */ 97877cf3ffSSoby Mathew spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1, 98877cf3ffSSoby Mathew SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); 99877cf3ffSSoby Mathew return spsr; 100877cf3ffSSoby Mathew } 101877cf3ffSSoby Mathew #endif /* AARCH32 */ 102bc149bfcSSoby Mathew 103bc149bfcSSoby Mathew /******************************************************************************* 104bc149bfcSSoby Mathew * Configures access to the system counter timer module. 105bc149bfcSSoby Mathew ******************************************************************************/ 106bc149bfcSSoby Mathew #ifdef ARM_SYS_TIMCTL_BASE 107bc149bfcSSoby Mathew void arm_configure_sys_timer(void) 108bc149bfcSSoby Mathew { 109bc149bfcSSoby Mathew unsigned int reg_val; 110bc149bfcSSoby Mathew 111342d6220SSoby Mathew /* Read the frequency of the system counter */ 112342d6220SSoby Mathew unsigned int freq_val = plat_get_syscnt_freq2(); 113342d6220SSoby Mathew 114bc149bfcSSoby Mathew #if ARM_CONFIG_CNTACR 115583e0791SAntonio Nino Diaz reg_val = (1U << CNTACR_RPCT_SHIFT) | (1U << CNTACR_RVCT_SHIFT); 116583e0791SAntonio Nino Diaz reg_val |= (1U << CNTACR_RFRQ_SHIFT) | (1U << CNTACR_RVOFF_SHIFT); 117583e0791SAntonio Nino Diaz reg_val |= (1U << CNTACR_RWVT_SHIFT) | (1U << CNTACR_RWPT_SHIFT); 118bc149bfcSSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTACR_BASE(PLAT_ARM_NSTIMER_FRAME_ID), reg_val); 119bc149bfcSSoby Mathew #endif /* ARM_CONFIG_CNTACR */ 120bc149bfcSSoby Mathew 121583e0791SAntonio Nino Diaz reg_val = (1U << CNTNSAR_NS_SHIFT(PLAT_ARM_NSTIMER_FRAME_ID)); 122bc149bfcSSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTNSAR, reg_val); 123342d6220SSoby Mathew 124342d6220SSoby Mathew /* 125342d6220SSoby Mathew * Initialize CNTFRQ register in CNTCTLBase frame. The CNTFRQ 126342d6220SSoby Mathew * system register initialized during psci_arch_setup() is different 127342d6220SSoby Mathew * from this and has to be updated independently. 128342d6220SSoby Mathew */ 129342d6220SSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTCTLBASE_CNTFRQ, freq_val); 130342d6220SSoby Mathew 131342d6220SSoby Mathew #ifdef PLAT_juno 132342d6220SSoby Mathew /* 133342d6220SSoby Mathew * Initialize CNTFRQ register in Non-secure CNTBase frame. 134342d6220SSoby Mathew * This is only required for Juno, because it doesn't follow ARM ARM 135932b3ae2SAntonio Nino Diaz * in that the value updated in CNTFRQ is not reflected in 136932b3ae2SAntonio Nino Diaz * CNTBASEN_CNTFRQ. Hence update the value manually. 137342d6220SSoby Mathew */ 138932b3ae2SAntonio Nino Diaz mmio_write_32(ARM_SYS_CNT_BASE_NS + CNTBASEN_CNTFRQ, freq_val); 139342d6220SSoby Mathew #endif 140bc149bfcSSoby Mathew } 141bc149bfcSSoby Mathew #endif /* ARM_SYS_TIMCTL_BASE */ 142bc149bfcSSoby Mathew 143bc149bfcSSoby Mathew /******************************************************************************* 144bc149bfcSSoby Mathew * Returns ARM platform specific memory map regions. 145bc149bfcSSoby Mathew ******************************************************************************/ 146bc149bfcSSoby Mathew const mmap_region_t *plat_arm_get_mmap(void) 147bc149bfcSSoby Mathew { 148bc149bfcSSoby Mathew return plat_arm_mmap; 149bc149bfcSSoby Mathew } 150bc149bfcSSoby Mathew 151bc149bfcSSoby Mathew #ifdef ARM_SYS_CNTCTL_BASE 152bc149bfcSSoby Mathew 153bc149bfcSSoby Mathew unsigned int plat_get_syscnt_freq2(void) 154bc149bfcSSoby Mathew { 155bc149bfcSSoby Mathew unsigned int counter_base_frequency; 156bc149bfcSSoby Mathew 157bc149bfcSSoby Mathew /* Read the frequency from Frequency modes table */ 158bc149bfcSSoby Mathew counter_base_frequency = mmio_read_32(ARM_SYS_CNTCTL_BASE + CNTFID_OFF); 159bc149bfcSSoby Mathew 160bc149bfcSSoby Mathew /* The first entry of the frequency modes table must not be 0 */ 161583e0791SAntonio Nino Diaz if (counter_base_frequency == 0U) 162bc149bfcSSoby Mathew panic(); 163bc149bfcSSoby Mathew 164bc149bfcSSoby Mathew return counter_base_frequency; 165bc149bfcSSoby Mathew } 166bc149bfcSSoby Mathew 167bc149bfcSSoby Mathew #endif /* ARM_SYS_CNTCTL_BASE */ 168781f4aacSJeenu Viswambharan 169781f4aacSJeenu Viswambharan #if SDEI_SUPPORT 170781f4aacSJeenu Viswambharan /* 171781f4aacSJeenu Viswambharan * Translate SDEI entry point to PA, and perform standard ARM entry point 172781f4aacSJeenu Viswambharan * validation on it. 173781f4aacSJeenu Viswambharan */ 174781f4aacSJeenu Viswambharan int plat_sdei_validate_entry_point(uintptr_t ep, unsigned int client_mode) 175781f4aacSJeenu Viswambharan { 176781f4aacSJeenu Viswambharan uint64_t par, pa; 177781f4aacSJeenu Viswambharan uint32_t scr_el3; 178781f4aacSJeenu Viswambharan 179781f4aacSJeenu Viswambharan /* Doing Non-secure address translation requires SCR_EL3.NS set */ 180781f4aacSJeenu Viswambharan scr_el3 = read_scr_el3(); 181781f4aacSJeenu Viswambharan write_scr_el3(scr_el3 | SCR_NS_BIT); 182781f4aacSJeenu Viswambharan isb(); 183781f4aacSJeenu Viswambharan 184781f4aacSJeenu Viswambharan assert((client_mode == MODE_EL2) || (client_mode == MODE_EL1)); 185781f4aacSJeenu Viswambharan if (client_mode == MODE_EL2) { 186781f4aacSJeenu Viswambharan /* 187781f4aacSJeenu Viswambharan * Translate entry point to Physical Address using the EL2 188781f4aacSJeenu Viswambharan * translation regime. 189781f4aacSJeenu Viswambharan */ 190781f4aacSJeenu Viswambharan ats1e2r(ep); 191781f4aacSJeenu Viswambharan } else { 192781f4aacSJeenu Viswambharan /* 193781f4aacSJeenu Viswambharan * Translate entry point to Physical Address using the EL1&0 194781f4aacSJeenu Viswambharan * translation regime, including stage 2. 195781f4aacSJeenu Viswambharan */ 196781f4aacSJeenu Viswambharan ats12e1r(ep); 197781f4aacSJeenu Viswambharan } 198781f4aacSJeenu Viswambharan isb(); 199781f4aacSJeenu Viswambharan par = read_par_el1(); 200781f4aacSJeenu Viswambharan 201781f4aacSJeenu Viswambharan /* Restore original SCRL_EL3 */ 202781f4aacSJeenu Viswambharan write_scr_el3(scr_el3); 203781f4aacSJeenu Viswambharan isb(); 204781f4aacSJeenu Viswambharan 205781f4aacSJeenu Viswambharan /* If the translation resulted in fault, return failure */ 206781f4aacSJeenu Viswambharan if ((par & PAR_F_MASK) != 0) 207781f4aacSJeenu Viswambharan return -1; 208781f4aacSJeenu Viswambharan 209781f4aacSJeenu Viswambharan /* Extract Physical Address from PAR */ 210781f4aacSJeenu Viswambharan pa = (par & (PAR_ADDR_MASK << PAR_ADDR_SHIFT)); 211781f4aacSJeenu Viswambharan 212781f4aacSJeenu Viswambharan /* Perform NS entry point validation on the physical address */ 213781f4aacSJeenu Viswambharan return arm_validate_ns_entrypoint(pa); 214781f4aacSJeenu Viswambharan } 215781f4aacSJeenu Viswambharan #endif 216