1bc149bfcSSoby Mathew /* 2*01907f3fSHarrison Mutai * Copyright (c) 2015-2025, ARM Limited and Contributors. All rights reserved. 3bc149bfcSSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5bc149bfcSSoby Mathew */ 609d40e0eSAntonio Nino Diaz 709d40e0eSAntonio Nino Diaz #include <assert.h> 809d40e0eSAntonio Nino Diaz 909d40e0eSAntonio Nino Diaz #include <platform_def.h> 1009d40e0eSAntonio Nino Diaz 11bc149bfcSSoby Mathew #include <arch.h> 12bc149bfcSSoby Mathew #include <arch_helpers.h> 1309d40e0eSAntonio Nino Diaz #include <common/debug.h> 1409d40e0eSAntonio Nino Diaz #include <common/romlib.h> 1530655136SGovindraj Raja #include <common/par.h> 1630655136SGovindraj Raja #include <lib/extensions/sysreg128.h> 1709d40e0eSAntonio Nino Diaz #include <lib/mmio.h> 18c7bacd40SManish V Badarkhe #include <lib/smccc.h> 1909d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_compat.h> 20c7bacd40SManish V Badarkhe #include <services/arm_arch_svc.h> 21bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h> 2209d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 2309d40e0eSAntonio Nino Diaz 24bc149bfcSSoby Mathew /* Weak definitions may be overridden in specific ARM standard platform */ 25bc149bfcSSoby Mathew #pragma weak plat_get_ns_image_entrypoint 26bc149bfcSSoby Mathew #pragma weak plat_arm_get_mmap 27bc149bfcSSoby Mathew 28bc149bfcSSoby Mathew /* Conditionally provide a weak definition of plat_get_syscnt_freq2 to avoid 29bc149bfcSSoby Mathew * conflicts with the definition in plat/common. */ 30bc149bfcSSoby Mathew #pragma weak plat_get_syscnt_freq2 31bc149bfcSSoby Mathew 320e753437SManish V Badarkhe /* Get ARM SOC-ID */ 330e753437SManish V Badarkhe #pragma weak plat_arm_get_soc_id 340e753437SManish V Badarkhe 3560e8f3cfSPetre-Ionut Tudor /******************************************************************************* 3660e8f3cfSPetre-Ionut Tudor * Changes the memory attributes for the region of mapped memory where the BL 3760e8f3cfSPetre-Ionut Tudor * image's translation tables are located such that the tables will have 3860e8f3cfSPetre-Ionut Tudor * read-only permissions. 3960e8f3cfSPetre-Ionut Tudor ******************************************************************************/ 4060e8f3cfSPetre-Ionut Tudor #if PLAT_RO_XLAT_TABLES 4160e8f3cfSPetre-Ionut Tudor void arm_xlat_make_tables_readonly(void) 4260e8f3cfSPetre-Ionut Tudor { 4360e8f3cfSPetre-Ionut Tudor int rc = xlat_make_tables_readonly(); 4460e8f3cfSPetre-Ionut Tudor 4560e8f3cfSPetre-Ionut Tudor if (rc != 0) { 4660e8f3cfSPetre-Ionut Tudor ERROR("Failed to make translation tables read-only at EL%u.\n", 4760e8f3cfSPetre-Ionut Tudor get_current_el()); 4860e8f3cfSPetre-Ionut Tudor panic(); 4960e8f3cfSPetre-Ionut Tudor } 5060e8f3cfSPetre-Ionut Tudor 5160e8f3cfSPetre-Ionut Tudor INFO("Translation tables are now read-only at EL%u.\n", 5260e8f3cfSPetre-Ionut Tudor get_current_el()); 5360e8f3cfSPetre-Ionut Tudor } 5460e8f3cfSPetre-Ionut Tudor #endif 551eb735d7SRoberto Vargas 561eb735d7SRoberto Vargas void arm_setup_romlib(void) 571eb735d7SRoberto Vargas { 581eb735d7SRoberto Vargas #if USE_ROMLIB 591eb735d7SRoberto Vargas if (!rom_lib_init(ROMLIB_VERSION)) 601eb735d7SRoberto Vargas panic(); 611eb735d7SRoberto Vargas #endif 621eb735d7SRoberto Vargas } 631eb735d7SRoberto Vargas 64bc149bfcSSoby Mathew uintptr_t plat_get_ns_image_entrypoint(void) 65bc149bfcSSoby Mathew { 6648ac1df9SSoby Mathew #ifdef PRELOADED_BL33_BASE 6748ac1df9SSoby Mathew return PRELOADED_BL33_BASE; 6848ac1df9SSoby Mathew #else 69ece6fd2dSSandrine Bailleux return PLAT_ARM_NS_IMAGE_BASE; 7048ac1df9SSoby Mathew #endif 71bc149bfcSSoby Mathew } 72bc149bfcSSoby Mathew 73bc149bfcSSoby Mathew /******************************************************************************* 74*01907f3fSHarrison Mutai * Gets SPSR for next stage images. 75bc149bfcSSoby Mathew ******************************************************************************/ 76*01907f3fSHarrison Mutai uint32_t arm_get_spsr(unsigned int image_id) 77bc149bfcSSoby Mathew { 78*01907f3fSHarrison Mutai unsigned int __unused hyp_status, mode, spsr; 79*01907f3fSHarrison Mutai 80*01907f3fSHarrison Mutai if (image_id == BL32_IMAGE_ID) { 81*01907f3fSHarrison Mutai /* The Secure Payload Dispatcher service is responsible for 82bc149bfcSSoby Mathew * setting the SPSR prior to entry into the BL32 image. 83bc149bfcSSoby Mathew */ 84bc149bfcSSoby Mathew return 0; 85bc149bfcSSoby Mathew } 86bc149bfcSSoby Mathew 87402b3cf8SJulius Werner #ifdef __aarch64__ 88bc149bfcSSoby Mathew /* Figure out what mode we enter the non-secure world in */ 89a0fee747SAntonio Nino Diaz mode = (el_implemented(2) != EL_IMPL_NONE) ? MODE_EL2 : MODE_EL1; 90bc149bfcSSoby Mathew 91bc149bfcSSoby Mathew /* 92bc149bfcSSoby Mathew * TODO: Consider the possibility of specifying the SPSR in 93bc149bfcSSoby Mathew * the FIP ToC and allowing the platform to have a say as 94bc149bfcSSoby Mathew * well. 95bc149bfcSSoby Mathew */ 96d7b5f408SJimmy Brisson spsr = SPSR_64((uint64_t)mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 97877cf3ffSSoby Mathew #else 98877cf3ffSSoby Mathew hyp_status = GET_VIRT_EXT(read_id_pfr1()); 99877cf3ffSSoby Mathew 100877cf3ffSSoby Mathew mode = (hyp_status) ? MODE32_hyp : MODE32_svc; 101877cf3ffSSoby Mathew 102877cf3ffSSoby Mathew /* 103877cf3ffSSoby Mathew * TODO: Consider the possibility of specifying the SPSR in 104877cf3ffSSoby Mathew * the FIP ToC and allowing the platform to have a say as 105877cf3ffSSoby Mathew * well. 106877cf3ffSSoby Mathew */ 107877cf3ffSSoby Mathew spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1, 108877cf3ffSSoby Mathew SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); 109*01907f3fSHarrison Mutai #endif /* __aarch64__ */ 110*01907f3fSHarrison Mutai 111877cf3ffSSoby Mathew return spsr; 112877cf3ffSSoby Mathew } 113bc149bfcSSoby Mathew 114bc149bfcSSoby Mathew /******************************************************************************* 115bc149bfcSSoby Mathew * Configures access to the system counter timer module. 116bc149bfcSSoby Mathew ******************************************************************************/ 117bc149bfcSSoby Mathew #ifdef ARM_SYS_TIMCTL_BASE 118bc149bfcSSoby Mathew void arm_configure_sys_timer(void) 119bc149bfcSSoby Mathew { 120bc149bfcSSoby Mathew unsigned int reg_val; 121bc149bfcSSoby Mathew 122342d6220SSoby Mathew /* Read the frequency of the system counter */ 123342d6220SSoby Mathew unsigned int freq_val = plat_get_syscnt_freq2(); 124342d6220SSoby Mathew 125bc149bfcSSoby Mathew #if ARM_CONFIG_CNTACR 126583e0791SAntonio Nino Diaz reg_val = (1U << CNTACR_RPCT_SHIFT) | (1U << CNTACR_RVCT_SHIFT); 127583e0791SAntonio Nino Diaz reg_val |= (1U << CNTACR_RFRQ_SHIFT) | (1U << CNTACR_RVOFF_SHIFT); 128583e0791SAntonio Nino Diaz reg_val |= (1U << CNTACR_RWVT_SHIFT) | (1U << CNTACR_RWPT_SHIFT); 129bc149bfcSSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTACR_BASE(PLAT_ARM_NSTIMER_FRAME_ID), reg_val); 130bc149bfcSSoby Mathew #endif /* ARM_CONFIG_CNTACR */ 131bc149bfcSSoby Mathew 132583e0791SAntonio Nino Diaz reg_val = (1U << CNTNSAR_NS_SHIFT(PLAT_ARM_NSTIMER_FRAME_ID)); 133bc149bfcSSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTNSAR, reg_val); 134342d6220SSoby Mathew 135342d6220SSoby Mathew /* 136342d6220SSoby Mathew * Initialize CNTFRQ register in CNTCTLBase frame. The CNTFRQ 137342d6220SSoby Mathew * system register initialized during psci_arch_setup() is different 138342d6220SSoby Mathew * from this and has to be updated independently. 139342d6220SSoby Mathew */ 140342d6220SSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTCTLBASE_CNTFRQ, freq_val); 141342d6220SSoby Mathew 1427f2d23d9SManoj Kumar #if defined(PLAT_juno) || defined(PLAT_n1sdp) || defined(PLAT_morello) 143342d6220SSoby Mathew /* 144342d6220SSoby Mathew * Initialize CNTFRQ register in Non-secure CNTBase frame. 1457f2d23d9SManoj Kumar * This is required for Juno, N1SDP and Morello because they do not 146603b372eSSami Mujawar * follow ARM ARM in that the value updated in CNTFRQ is not 147603b372eSSami Mujawar * reflected in CNTBASEN_CNTFRQ. Hence update the value manually. 148342d6220SSoby Mathew */ 149932b3ae2SAntonio Nino Diaz mmio_write_32(ARM_SYS_CNT_BASE_NS + CNTBASEN_CNTFRQ, freq_val); 150342d6220SSoby Mathew #endif 151bc149bfcSSoby Mathew } 152bc149bfcSSoby Mathew #endif /* ARM_SYS_TIMCTL_BASE */ 153bc149bfcSSoby Mathew 154bc149bfcSSoby Mathew /******************************************************************************* 155bc149bfcSSoby Mathew * Returns ARM platform specific memory map regions. 156bc149bfcSSoby Mathew ******************************************************************************/ 157bc149bfcSSoby Mathew const mmap_region_t *plat_arm_get_mmap(void) 158bc149bfcSSoby Mathew { 159bc149bfcSSoby Mathew return plat_arm_mmap; 160bc149bfcSSoby Mathew } 161bc149bfcSSoby Mathew 162bc149bfcSSoby Mathew #ifdef ARM_SYS_CNTCTL_BASE 163bc149bfcSSoby Mathew 164bc149bfcSSoby Mathew unsigned int plat_get_syscnt_freq2(void) 165bc149bfcSSoby Mathew { 166bc149bfcSSoby Mathew unsigned int counter_base_frequency; 167bc149bfcSSoby Mathew 168bc149bfcSSoby Mathew /* Read the frequency from Frequency modes table */ 169bc149bfcSSoby Mathew counter_base_frequency = mmio_read_32(ARM_SYS_CNTCTL_BASE + CNTFID_OFF); 170bc149bfcSSoby Mathew 171bc149bfcSSoby Mathew /* The first entry of the frequency modes table must not be 0 */ 172583e0791SAntonio Nino Diaz if (counter_base_frequency == 0U) 173bc149bfcSSoby Mathew panic(); 174bc149bfcSSoby Mathew 175bc149bfcSSoby Mathew return counter_base_frequency; 176bc149bfcSSoby Mathew } 177bc149bfcSSoby Mathew 178bc149bfcSSoby Mathew #endif /* ARM_SYS_CNTCTL_BASE */ 179781f4aacSJeenu Viswambharan 180781f4aacSJeenu Viswambharan #if SDEI_SUPPORT 181781f4aacSJeenu Viswambharan /* 182781f4aacSJeenu Viswambharan * Translate SDEI entry point to PA, and perform standard ARM entry point 183781f4aacSJeenu Viswambharan * validation on it. 184781f4aacSJeenu Viswambharan */ 185781f4aacSJeenu Viswambharan int plat_sdei_validate_entry_point(uintptr_t ep, unsigned int client_mode) 186781f4aacSJeenu Viswambharan { 18730655136SGovindraj Raja uint64_t pa; 18830655136SGovindraj Raja sysreg_t par; 189f1be00daSLouis Mayencourt u_register_t scr_el3; 190781f4aacSJeenu Viswambharan 191781f4aacSJeenu Viswambharan /* Doing Non-secure address translation requires SCR_EL3.NS set */ 192781f4aacSJeenu Viswambharan scr_el3 = read_scr_el3(); 193781f4aacSJeenu Viswambharan write_scr_el3(scr_el3 | SCR_NS_BIT); 194781f4aacSJeenu Viswambharan isb(); 195781f4aacSJeenu Viswambharan 196781f4aacSJeenu Viswambharan assert((client_mode == MODE_EL2) || (client_mode == MODE_EL1)); 197781f4aacSJeenu Viswambharan if (client_mode == MODE_EL2) { 198781f4aacSJeenu Viswambharan /* 199781f4aacSJeenu Viswambharan * Translate entry point to Physical Address using the EL2 200781f4aacSJeenu Viswambharan * translation regime. 201781f4aacSJeenu Viswambharan */ 202781f4aacSJeenu Viswambharan ats1e2r(ep); 203781f4aacSJeenu Viswambharan } else { 204781f4aacSJeenu Viswambharan /* 205781f4aacSJeenu Viswambharan * Translate entry point to Physical Address using the EL1&0 206781f4aacSJeenu Viswambharan * translation regime, including stage 2. 207781f4aacSJeenu Viswambharan */ 20886ba5853SManish V Badarkhe AT(ats12e1r, ep); 209781f4aacSJeenu Viswambharan } 210781f4aacSJeenu Viswambharan isb(); 211781f4aacSJeenu Viswambharan par = read_par_el1(); 212781f4aacSJeenu Viswambharan 213781f4aacSJeenu Viswambharan /* Restore original SCRL_EL3 */ 214781f4aacSJeenu Viswambharan write_scr_el3(scr_el3); 215781f4aacSJeenu Viswambharan isb(); 216781f4aacSJeenu Viswambharan 217781f4aacSJeenu Viswambharan /* If the translation resulted in fault, return failure */ 218781f4aacSJeenu Viswambharan if ((par & PAR_F_MASK) != 0) 219781f4aacSJeenu Viswambharan return -1; 220781f4aacSJeenu Viswambharan 221781f4aacSJeenu Viswambharan /* Extract Physical Address from PAR */ 22230655136SGovindraj Raja pa = get_par_el1_pa(par); 223781f4aacSJeenu Viswambharan 224781f4aacSJeenu Viswambharan /* Perform NS entry point validation on the physical address */ 225781f4aacSJeenu Viswambharan return arm_validate_ns_entrypoint(pa); 226781f4aacSJeenu Viswambharan } 227781f4aacSJeenu Viswambharan #endif 2280e753437SManish V Badarkhe 2292a1cdee4Sjohpow01 const mmap_region_t *plat_get_addr_mmap(void) 2302a1cdee4Sjohpow01 { 2312a1cdee4Sjohpow01 return plat_arm_mmap; 2322a1cdee4Sjohpow01 } 233341df6afSRohit Mathew 234341df6afSRohit Mathew #if ENABLE_RME 235341df6afSRohit Mathew void arm_gpt_setup(void) 236341df6afSRohit Mathew { 237341df6afSRohit Mathew /* 238341df6afSRohit Mathew * It is to be noted that any Arm platform that reuses arm_gpt_setup 239341df6afSRohit Mathew * must implement plat_arm_get_gpt_info within its platform code 240341df6afSRohit Mathew */ 241341df6afSRohit Mathew const arm_gpt_info_t *arm_gpt_info = 242341df6afSRohit Mathew plat_arm_get_gpt_info(); 243341df6afSRohit Mathew 244341df6afSRohit Mathew if (arm_gpt_info == NULL) { 245341df6afSRohit Mathew ERROR("arm_gpt_info not initialized!!\n"); 246341df6afSRohit Mathew panic(); 247341df6afSRohit Mathew } 248341df6afSRohit Mathew 249341df6afSRohit Mathew /* Initialize entire protected space to GPT_GPI_ANY. */ 250341df6afSRohit Mathew if (gpt_init_l0_tables(arm_gpt_info->pps, arm_gpt_info->l0_base, 251341df6afSRohit Mathew arm_gpt_info->l0_size) < 0) { 252341df6afSRohit Mathew ERROR("gpt_init_l0_tables() failed!\n"); 253341df6afSRohit Mathew panic(); 254341df6afSRohit Mathew } 255341df6afSRohit Mathew 256341df6afSRohit Mathew /* Carve out defined PAS ranges. */ 257341df6afSRohit Mathew if (gpt_init_pas_l1_tables(arm_gpt_info->pgs, 258341df6afSRohit Mathew arm_gpt_info->l1_base, 259341df6afSRohit Mathew arm_gpt_info->l1_size, 260341df6afSRohit Mathew arm_gpt_info->pas_region_base, 261341df6afSRohit Mathew arm_gpt_info->pas_region_count) < 0) { 262341df6afSRohit Mathew ERROR("gpt_init_pas_l1_tables() failed!\n"); 263341df6afSRohit Mathew panic(); 264341df6afSRohit Mathew } 265341df6afSRohit Mathew 266341df6afSRohit Mathew INFO("Enabling Granule Protection Checks\n"); 267341df6afSRohit Mathew if (gpt_enable() < 0) { 268341df6afSRohit Mathew ERROR("gpt_enable() failed!\n"); 269341df6afSRohit Mathew panic(); 270341df6afSRohit Mathew } 271341df6afSRohit Mathew } 272341df6afSRohit Mathew #endif /* ENABLE_RME */ 273