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 */ 6bc149bfcSSoby Mathew #include <arch.h> 7bc149bfcSSoby Mathew #include <arch_helpers.h> 83b211ff5SAntonio Nino Diaz #include <arm_xlat_tables.h> 9bc149bfcSSoby Mathew #include <assert.h> 10bc149bfcSSoby Mathew #include <debug.h> 11bc149bfcSSoby Mathew #include <mmio.h> 12bc149bfcSSoby Mathew #include <plat_arm.h> 131af540efSRoberto Vargas #include <platform.h> 14*1eb735d7SRoberto Vargas #include <platform_def.h> 15*1eb735d7SRoberto Vargas #include <romlib.h> 16e29efeb1SAntonio Nino Diaz #include <secure_partition.h> 17bc149bfcSSoby Mathew 18bc149bfcSSoby Mathew /* Weak definitions may be overridden in specific ARM standard platform */ 19bc149bfcSSoby Mathew #pragma weak plat_get_ns_image_entrypoint 20bc149bfcSSoby Mathew #pragma weak plat_arm_get_mmap 21bc149bfcSSoby Mathew 22bc149bfcSSoby Mathew /* Conditionally provide a weak definition of plat_get_syscnt_freq2 to avoid 23bc149bfcSSoby Mathew * conflicts with the definition in plat/common. */ 24bc149bfcSSoby Mathew #if ERROR_DEPRECATED 25bc149bfcSSoby Mathew #pragma weak plat_get_syscnt_freq2 26bc149bfcSSoby Mathew #endif 27bc149bfcSSoby Mathew 28*1eb735d7SRoberto Vargas 29*1eb735d7SRoberto Vargas void arm_setup_romlib(void) 30*1eb735d7SRoberto Vargas { 31*1eb735d7SRoberto Vargas #if USE_ROMLIB 32*1eb735d7SRoberto Vargas if (!rom_lib_init(ROMLIB_VERSION)) 33*1eb735d7SRoberto Vargas panic(); 34*1eb735d7SRoberto Vargas #endif 35*1eb735d7SRoberto Vargas } 36*1eb735d7SRoberto Vargas 37bc149bfcSSoby Mathew /* 38bc149bfcSSoby Mathew * Set up the page tables for the generic and platform-specific memory regions. 39d323af9eSDaniel Boulby * The size of the Trusted SRAM seen by the BL image must be specified as well 40d323af9eSDaniel Boulby * as an array specifying the generic memory regions which can be; 41bc149bfcSSoby Mathew * - Code section; 42bc149bfcSSoby Mathew * - Read-only data section; 43bc149bfcSSoby Mathew * - Coherent memory region, if applicable. 44bc149bfcSSoby Mathew */ 45d323af9eSDaniel Boulby 46d323af9eSDaniel Boulby void arm_setup_page_tables(const mmap_region_t bl_regions[], 47d323af9eSDaniel Boulby const mmap_region_t plat_regions[]) 48bc149bfcSSoby Mathew { 49d323af9eSDaniel Boulby #if LOG_LEVEL >= LOG_LEVEL_VERBOSE 50d323af9eSDaniel Boulby const mmap_region_t *regions = bl_regions; 51d323af9eSDaniel Boulby 52d323af9eSDaniel Boulby while (regions->size != 0U) { 53d323af9eSDaniel Boulby VERBOSE("Region: 0x%lx - 0x%lx has attributes 0x%x\n", 54d323af9eSDaniel Boulby regions->base_va, 55d323af9eSDaniel Boulby (regions->base_va + regions->size), 56d323af9eSDaniel Boulby regions->attr); 57d323af9eSDaniel Boulby regions++; 58d323af9eSDaniel Boulby } 59d323af9eSDaniel Boulby #endif 60bc149bfcSSoby Mathew /* 61bc149bfcSSoby Mathew * Map the Trusted SRAM with appropriate memory attributes. 62bc149bfcSSoby Mathew * Subsequent mappings will adjust the attributes for specific regions. 63bc149bfcSSoby Mathew */ 64d323af9eSDaniel Boulby mmap_add(bl_regions); 65bc149bfcSSoby Mathew /* Now (re-)map the platform-specific memory regions */ 66d323af9eSDaniel Boulby mmap_add(plat_regions); 67bc149bfcSSoby Mathew 68bc149bfcSSoby Mathew /* Create the page tables to reflect the above mappings */ 69bc149bfcSSoby Mathew init_xlat_tables(); 70bc149bfcSSoby Mathew } 71bc149bfcSSoby Mathew 72bc149bfcSSoby Mathew uintptr_t plat_get_ns_image_entrypoint(void) 73bc149bfcSSoby Mathew { 7448ac1df9SSoby Mathew #ifdef PRELOADED_BL33_BASE 7548ac1df9SSoby Mathew return PRELOADED_BL33_BASE; 7648ac1df9SSoby Mathew #else 77bc149bfcSSoby Mathew return PLAT_ARM_NS_IMAGE_OFFSET; 7848ac1df9SSoby Mathew #endif 79bc149bfcSSoby Mathew } 80bc149bfcSSoby Mathew 81bc149bfcSSoby Mathew /******************************************************************************* 82bc149bfcSSoby Mathew * Gets SPSR for BL32 entry 83bc149bfcSSoby Mathew ******************************************************************************/ 84bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl32_entry(void) 85bc149bfcSSoby Mathew { 86bc149bfcSSoby Mathew /* 87bc149bfcSSoby Mathew * The Secure Payload Dispatcher service is responsible for 88bc149bfcSSoby Mathew * setting the SPSR prior to entry into the BL32 image. 89bc149bfcSSoby Mathew */ 90bc149bfcSSoby Mathew return 0; 91bc149bfcSSoby Mathew } 92bc149bfcSSoby Mathew 93bc149bfcSSoby Mathew /******************************************************************************* 94bc149bfcSSoby Mathew * Gets SPSR for BL33 entry 95bc149bfcSSoby Mathew ******************************************************************************/ 96877cf3ffSSoby Mathew #ifndef AARCH32 97bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void) 98bc149bfcSSoby Mathew { 99bc149bfcSSoby Mathew unsigned int mode; 100bc149bfcSSoby Mathew uint32_t spsr; 101bc149bfcSSoby Mathew 102bc149bfcSSoby Mathew /* Figure out what mode we enter the non-secure world in */ 103f4c8aa90SJeenu Viswambharan mode = EL_IMPLEMENTED(2) ? MODE_EL2 : MODE_EL1; 104bc149bfcSSoby Mathew 105bc149bfcSSoby Mathew /* 106bc149bfcSSoby Mathew * TODO: Consider the possibility of specifying the SPSR in 107bc149bfcSSoby Mathew * the FIP ToC and allowing the platform to have a say as 108bc149bfcSSoby Mathew * well. 109bc149bfcSSoby Mathew */ 110bc149bfcSSoby Mathew spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 111bc149bfcSSoby Mathew return spsr; 112bc149bfcSSoby Mathew } 113877cf3ffSSoby Mathew #else 114877cf3ffSSoby Mathew /******************************************************************************* 115877cf3ffSSoby Mathew * Gets SPSR for BL33 entry 116877cf3ffSSoby Mathew ******************************************************************************/ 117877cf3ffSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void) 118877cf3ffSSoby Mathew { 119877cf3ffSSoby Mathew unsigned int hyp_status, mode, spsr; 120877cf3ffSSoby Mathew 121877cf3ffSSoby Mathew hyp_status = GET_VIRT_EXT(read_id_pfr1()); 122877cf3ffSSoby Mathew 123877cf3ffSSoby Mathew mode = (hyp_status) ? MODE32_hyp : MODE32_svc; 124877cf3ffSSoby Mathew 125877cf3ffSSoby Mathew /* 126877cf3ffSSoby Mathew * TODO: Consider the possibility of specifying the SPSR in 127877cf3ffSSoby Mathew * the FIP ToC and allowing the platform to have a say as 128877cf3ffSSoby Mathew * well. 129877cf3ffSSoby Mathew */ 130877cf3ffSSoby Mathew spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1, 131877cf3ffSSoby Mathew SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); 132877cf3ffSSoby Mathew return spsr; 133877cf3ffSSoby Mathew } 134877cf3ffSSoby Mathew #endif /* AARCH32 */ 135bc149bfcSSoby Mathew 136bc149bfcSSoby Mathew /******************************************************************************* 137bc149bfcSSoby Mathew * Configures access to the system counter timer module. 138bc149bfcSSoby Mathew ******************************************************************************/ 139bc149bfcSSoby Mathew #ifdef ARM_SYS_TIMCTL_BASE 140bc149bfcSSoby Mathew void arm_configure_sys_timer(void) 141bc149bfcSSoby Mathew { 142bc149bfcSSoby Mathew unsigned int reg_val; 143bc149bfcSSoby Mathew 144342d6220SSoby Mathew /* Read the frequency of the system counter */ 145342d6220SSoby Mathew unsigned int freq_val = plat_get_syscnt_freq2(); 146342d6220SSoby Mathew 147bc149bfcSSoby Mathew #if ARM_CONFIG_CNTACR 148bc149bfcSSoby Mathew reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT); 149bc149bfcSSoby Mathew reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT); 150bc149bfcSSoby Mathew reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT); 151bc149bfcSSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTACR_BASE(PLAT_ARM_NSTIMER_FRAME_ID), reg_val); 152bc149bfcSSoby Mathew #endif /* ARM_CONFIG_CNTACR */ 153bc149bfcSSoby Mathew 154bc149bfcSSoby Mathew reg_val = (1 << CNTNSAR_NS_SHIFT(PLAT_ARM_NSTIMER_FRAME_ID)); 155bc149bfcSSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTNSAR, reg_val); 156342d6220SSoby Mathew 157342d6220SSoby Mathew /* 158342d6220SSoby Mathew * Initialize CNTFRQ register in CNTCTLBase frame. The CNTFRQ 159342d6220SSoby Mathew * system register initialized during psci_arch_setup() is different 160342d6220SSoby Mathew * from this and has to be updated independently. 161342d6220SSoby Mathew */ 162342d6220SSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTCTLBASE_CNTFRQ, freq_val); 163342d6220SSoby Mathew 164342d6220SSoby Mathew #ifdef PLAT_juno 165342d6220SSoby Mathew /* 166342d6220SSoby Mathew * Initialize CNTFRQ register in Non-secure CNTBase frame. 167342d6220SSoby Mathew * This is only required for Juno, because it doesn't follow ARM ARM 168342d6220SSoby Mathew * in that the value updated in CNTFRQ is not reflected in CNTBASE_CNTFRQ. 169342d6220SSoby Mathew * Hence update the value manually. 170342d6220SSoby Mathew */ 171342d6220SSoby Mathew mmio_write_32(ARM_SYS_CNT_BASE_NS + CNTBASE_CNTFRQ, freq_val); 172342d6220SSoby Mathew #endif 173bc149bfcSSoby Mathew } 174bc149bfcSSoby Mathew #endif /* ARM_SYS_TIMCTL_BASE */ 175bc149bfcSSoby Mathew 176bc149bfcSSoby Mathew /******************************************************************************* 177bc149bfcSSoby Mathew * Returns ARM platform specific memory map regions. 178bc149bfcSSoby Mathew ******************************************************************************/ 179bc149bfcSSoby Mathew const mmap_region_t *plat_arm_get_mmap(void) 180bc149bfcSSoby Mathew { 181bc149bfcSSoby Mathew return plat_arm_mmap; 182bc149bfcSSoby Mathew } 183bc149bfcSSoby Mathew 184bc149bfcSSoby Mathew #ifdef ARM_SYS_CNTCTL_BASE 185bc149bfcSSoby Mathew 186bc149bfcSSoby Mathew unsigned int plat_get_syscnt_freq2(void) 187bc149bfcSSoby Mathew { 188bc149bfcSSoby Mathew unsigned int counter_base_frequency; 189bc149bfcSSoby Mathew 190bc149bfcSSoby Mathew /* Read the frequency from Frequency modes table */ 191bc149bfcSSoby Mathew counter_base_frequency = mmio_read_32(ARM_SYS_CNTCTL_BASE + CNTFID_OFF); 192bc149bfcSSoby Mathew 193bc149bfcSSoby Mathew /* The first entry of the frequency modes table must not be 0 */ 194bc149bfcSSoby Mathew if (counter_base_frequency == 0) 195bc149bfcSSoby Mathew panic(); 196bc149bfcSSoby Mathew 197bc149bfcSSoby Mathew return counter_base_frequency; 198bc149bfcSSoby Mathew } 199bc149bfcSSoby Mathew 200bc149bfcSSoby Mathew #endif /* ARM_SYS_CNTCTL_BASE */ 201781f4aacSJeenu Viswambharan 202781f4aacSJeenu Viswambharan #if SDEI_SUPPORT 203781f4aacSJeenu Viswambharan /* 204781f4aacSJeenu Viswambharan * Translate SDEI entry point to PA, and perform standard ARM entry point 205781f4aacSJeenu Viswambharan * validation on it. 206781f4aacSJeenu Viswambharan */ 207781f4aacSJeenu Viswambharan int plat_sdei_validate_entry_point(uintptr_t ep, unsigned int client_mode) 208781f4aacSJeenu Viswambharan { 209781f4aacSJeenu Viswambharan uint64_t par, pa; 210781f4aacSJeenu Viswambharan uint32_t scr_el3; 211781f4aacSJeenu Viswambharan 212781f4aacSJeenu Viswambharan /* Doing Non-secure address translation requires SCR_EL3.NS set */ 213781f4aacSJeenu Viswambharan scr_el3 = read_scr_el3(); 214781f4aacSJeenu Viswambharan write_scr_el3(scr_el3 | SCR_NS_BIT); 215781f4aacSJeenu Viswambharan isb(); 216781f4aacSJeenu Viswambharan 217781f4aacSJeenu Viswambharan assert((client_mode == MODE_EL2) || (client_mode == MODE_EL1)); 218781f4aacSJeenu Viswambharan if (client_mode == MODE_EL2) { 219781f4aacSJeenu Viswambharan /* 220781f4aacSJeenu Viswambharan * Translate entry point to Physical Address using the EL2 221781f4aacSJeenu Viswambharan * translation regime. 222781f4aacSJeenu Viswambharan */ 223781f4aacSJeenu Viswambharan ats1e2r(ep); 224781f4aacSJeenu Viswambharan } else { 225781f4aacSJeenu Viswambharan /* 226781f4aacSJeenu Viswambharan * Translate entry point to Physical Address using the EL1&0 227781f4aacSJeenu Viswambharan * translation regime, including stage 2. 228781f4aacSJeenu Viswambharan */ 229781f4aacSJeenu Viswambharan ats12e1r(ep); 230781f4aacSJeenu Viswambharan } 231781f4aacSJeenu Viswambharan isb(); 232781f4aacSJeenu Viswambharan par = read_par_el1(); 233781f4aacSJeenu Viswambharan 234781f4aacSJeenu Viswambharan /* Restore original SCRL_EL3 */ 235781f4aacSJeenu Viswambharan write_scr_el3(scr_el3); 236781f4aacSJeenu Viswambharan isb(); 237781f4aacSJeenu Viswambharan 238781f4aacSJeenu Viswambharan /* If the translation resulted in fault, return failure */ 239781f4aacSJeenu Viswambharan if ((par & PAR_F_MASK) != 0) 240781f4aacSJeenu Viswambharan return -1; 241781f4aacSJeenu Viswambharan 242781f4aacSJeenu Viswambharan /* Extract Physical Address from PAR */ 243781f4aacSJeenu Viswambharan pa = (par & (PAR_ADDR_MASK << PAR_ADDR_SHIFT)); 244781f4aacSJeenu Viswambharan 245781f4aacSJeenu Viswambharan /* Perform NS entry point validation on the physical address */ 246781f4aacSJeenu Viswambharan return arm_validate_ns_entrypoint(pa); 247781f4aacSJeenu Viswambharan } 248781f4aacSJeenu Viswambharan #endif 249