1bc149bfcSSoby Mathew /* 2*bf75a371SAntonio Nino Diaz * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved. 3bc149bfcSSoby Mathew * 4bc149bfcSSoby Mathew * Redistribution and use in source and binary forms, with or without 5bc149bfcSSoby Mathew * modification, are permitted provided that the following conditions are met: 6bc149bfcSSoby Mathew * 7bc149bfcSSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8bc149bfcSSoby Mathew * list of conditions and the following disclaimer. 9bc149bfcSSoby Mathew * 10bc149bfcSSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11bc149bfcSSoby Mathew * this list of conditions and the following disclaimer in the documentation 12bc149bfcSSoby Mathew * and/or other materials provided with the distribution. 13bc149bfcSSoby Mathew * 14bc149bfcSSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15bc149bfcSSoby Mathew * to endorse or promote products derived from this software without specific 16bc149bfcSSoby Mathew * prior written permission. 17bc149bfcSSoby Mathew * 18bc149bfcSSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19bc149bfcSSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20bc149bfcSSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21bc149bfcSSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22bc149bfcSSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23bc149bfcSSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24bc149bfcSSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25bc149bfcSSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26bc149bfcSSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27bc149bfcSSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28bc149bfcSSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29bc149bfcSSoby Mathew */ 30bc149bfcSSoby Mathew #include <arch.h> 31bc149bfcSSoby Mathew #include <arch_helpers.h> 32bc149bfcSSoby Mathew #include <assert.h> 33bc149bfcSSoby Mathew #include <debug.h> 34bc149bfcSSoby Mathew #include <mmio.h> 35bc149bfcSSoby Mathew #include <plat_arm.h> 36bc149bfcSSoby Mathew #include <platform_def.h> 37*bf75a371SAntonio Nino Diaz #include <xlat_tables_v2.h> 38bc149bfcSSoby Mathew 39bc149bfcSSoby Mathew extern const mmap_region_t plat_arm_mmap[]; 40bc149bfcSSoby Mathew 41bc149bfcSSoby Mathew /* Weak definitions may be overridden in specific ARM standard platform */ 42bc149bfcSSoby Mathew #pragma weak plat_get_ns_image_entrypoint 43bc149bfcSSoby Mathew #pragma weak plat_arm_get_mmap 44bc149bfcSSoby Mathew 45bc149bfcSSoby Mathew /* Conditionally provide a weak definition of plat_get_syscnt_freq2 to avoid 46bc149bfcSSoby Mathew * conflicts with the definition in plat/common. */ 47bc149bfcSSoby Mathew #if ERROR_DEPRECATED 48bc149bfcSSoby Mathew #pragma weak plat_get_syscnt_freq2 49bc149bfcSSoby Mathew #endif 50bc149bfcSSoby Mathew 51bc149bfcSSoby Mathew /* 52bc149bfcSSoby Mathew * Set up the page tables for the generic and platform-specific memory regions. 53bc149bfcSSoby Mathew * The extents of the generic memory regions are specified by the function 54bc149bfcSSoby Mathew * arguments and consist of: 55bc149bfcSSoby Mathew * - Trusted SRAM seen by the BL image; 56bc149bfcSSoby Mathew * - Code section; 57bc149bfcSSoby Mathew * - Read-only data section; 58bc149bfcSSoby Mathew * - Coherent memory region, if applicable. 59bc149bfcSSoby Mathew */ 60bc149bfcSSoby Mathew void arm_setup_page_tables(uintptr_t total_base, 61bc149bfcSSoby Mathew size_t total_size, 62bc149bfcSSoby Mathew uintptr_t code_start, 63bc149bfcSSoby Mathew uintptr_t code_limit, 64bc149bfcSSoby Mathew uintptr_t rodata_start, 65bc149bfcSSoby Mathew uintptr_t rodata_limit 66bc149bfcSSoby Mathew #if USE_COHERENT_MEM 67bc149bfcSSoby Mathew , 68bc149bfcSSoby Mathew uintptr_t coh_start, 69bc149bfcSSoby Mathew uintptr_t coh_limit 70bc149bfcSSoby Mathew #endif 71bc149bfcSSoby Mathew ) 72bc149bfcSSoby Mathew { 73bc149bfcSSoby Mathew /* 74bc149bfcSSoby Mathew * Map the Trusted SRAM with appropriate memory attributes. 75bc149bfcSSoby Mathew * Subsequent mappings will adjust the attributes for specific regions. 76bc149bfcSSoby Mathew */ 77bc149bfcSSoby Mathew VERBOSE("Trusted SRAM seen by this BL image: %p - %p\n", 78bc149bfcSSoby Mathew (void *) total_base, (void *) (total_base + total_size)); 79bc149bfcSSoby Mathew mmap_add_region(total_base, total_base, 80bc149bfcSSoby Mathew total_size, 81bc149bfcSSoby Mathew MT_MEMORY | MT_RW | MT_SECURE); 82bc149bfcSSoby Mathew 83bc149bfcSSoby Mathew /* Re-map the code section */ 84bc149bfcSSoby Mathew VERBOSE("Code region: %p - %p\n", 85bc149bfcSSoby Mathew (void *) code_start, (void *) code_limit); 86bc149bfcSSoby Mathew mmap_add_region(code_start, code_start, 87bc149bfcSSoby Mathew code_limit - code_start, 88bc149bfcSSoby Mathew MT_CODE | MT_SECURE); 89bc149bfcSSoby Mathew 90bc149bfcSSoby Mathew /* Re-map the read-only data section */ 91bc149bfcSSoby Mathew VERBOSE("Read-only data region: %p - %p\n", 92bc149bfcSSoby Mathew (void *) rodata_start, (void *) rodata_limit); 93bc149bfcSSoby Mathew mmap_add_region(rodata_start, rodata_start, 94bc149bfcSSoby Mathew rodata_limit - rodata_start, 95bc149bfcSSoby Mathew MT_RO_DATA | MT_SECURE); 96bc149bfcSSoby Mathew 97bc149bfcSSoby Mathew #if USE_COHERENT_MEM 98bc149bfcSSoby Mathew /* Re-map the coherent memory region */ 99bc149bfcSSoby Mathew VERBOSE("Coherent region: %p - %p\n", 100bc149bfcSSoby Mathew (void *) coh_start, (void *) coh_limit); 101bc149bfcSSoby Mathew mmap_add_region(coh_start, coh_start, 102bc149bfcSSoby Mathew coh_limit - coh_start, 103bc149bfcSSoby Mathew MT_DEVICE | MT_RW | MT_SECURE); 104bc149bfcSSoby Mathew #endif 105bc149bfcSSoby Mathew 106bc149bfcSSoby Mathew /* Now (re-)map the platform-specific memory regions */ 107bc149bfcSSoby Mathew mmap_add(plat_arm_get_mmap()); 108bc149bfcSSoby Mathew 109bc149bfcSSoby Mathew /* Create the page tables to reflect the above mappings */ 110bc149bfcSSoby Mathew init_xlat_tables(); 111bc149bfcSSoby Mathew } 112bc149bfcSSoby Mathew 113bc149bfcSSoby Mathew uintptr_t plat_get_ns_image_entrypoint(void) 114bc149bfcSSoby Mathew { 11548ac1df9SSoby Mathew #ifdef PRELOADED_BL33_BASE 11648ac1df9SSoby Mathew return PRELOADED_BL33_BASE; 11748ac1df9SSoby Mathew #else 118bc149bfcSSoby Mathew return PLAT_ARM_NS_IMAGE_OFFSET; 11948ac1df9SSoby Mathew #endif 120bc149bfcSSoby Mathew } 121bc149bfcSSoby Mathew 122bc149bfcSSoby Mathew /******************************************************************************* 123bc149bfcSSoby Mathew * Gets SPSR for BL32 entry 124bc149bfcSSoby Mathew ******************************************************************************/ 125bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl32_entry(void) 126bc149bfcSSoby Mathew { 127bc149bfcSSoby Mathew /* 128bc149bfcSSoby Mathew * The Secure Payload Dispatcher service is responsible for 129bc149bfcSSoby Mathew * setting the SPSR prior to entry into the BL32 image. 130bc149bfcSSoby Mathew */ 131bc149bfcSSoby Mathew return 0; 132bc149bfcSSoby Mathew } 133bc149bfcSSoby Mathew 134bc149bfcSSoby Mathew /******************************************************************************* 135bc149bfcSSoby Mathew * Gets SPSR for BL33 entry 136bc149bfcSSoby Mathew ******************************************************************************/ 137877cf3ffSSoby Mathew #ifndef AARCH32 138bc149bfcSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void) 139bc149bfcSSoby Mathew { 140bc149bfcSSoby Mathew unsigned long el_status; 141bc149bfcSSoby Mathew unsigned int mode; 142bc149bfcSSoby Mathew uint32_t spsr; 143bc149bfcSSoby Mathew 144bc149bfcSSoby Mathew /* Figure out what mode we enter the non-secure world in */ 145bc149bfcSSoby Mathew el_status = read_id_aa64pfr0_el1() >> ID_AA64PFR0_EL2_SHIFT; 146bc149bfcSSoby Mathew el_status &= ID_AA64PFR0_ELX_MASK; 147bc149bfcSSoby Mathew 148bc149bfcSSoby Mathew mode = (el_status) ? MODE_EL2 : MODE_EL1; 149bc149bfcSSoby Mathew 150bc149bfcSSoby Mathew /* 151bc149bfcSSoby Mathew * TODO: Consider the possibility of specifying the SPSR in 152bc149bfcSSoby Mathew * the FIP ToC and allowing the platform to have a say as 153bc149bfcSSoby Mathew * well. 154bc149bfcSSoby Mathew */ 155bc149bfcSSoby Mathew spsr = SPSR_64(mode, MODE_SP_ELX, DISABLE_ALL_EXCEPTIONS); 156bc149bfcSSoby Mathew return spsr; 157bc149bfcSSoby Mathew } 158877cf3ffSSoby Mathew #else 159877cf3ffSSoby Mathew /******************************************************************************* 160877cf3ffSSoby Mathew * Gets SPSR for BL33 entry 161877cf3ffSSoby Mathew ******************************************************************************/ 162877cf3ffSSoby Mathew uint32_t arm_get_spsr_for_bl33_entry(void) 163877cf3ffSSoby Mathew { 164877cf3ffSSoby Mathew unsigned int hyp_status, mode, spsr; 165877cf3ffSSoby Mathew 166877cf3ffSSoby Mathew hyp_status = GET_VIRT_EXT(read_id_pfr1()); 167877cf3ffSSoby Mathew 168877cf3ffSSoby Mathew mode = (hyp_status) ? MODE32_hyp : MODE32_svc; 169877cf3ffSSoby Mathew 170877cf3ffSSoby Mathew /* 171877cf3ffSSoby Mathew * TODO: Consider the possibility of specifying the SPSR in 172877cf3ffSSoby Mathew * the FIP ToC and allowing the platform to have a say as 173877cf3ffSSoby Mathew * well. 174877cf3ffSSoby Mathew */ 175877cf3ffSSoby Mathew spsr = SPSR_MODE32(mode, plat_get_ns_image_entrypoint() & 0x1, 176877cf3ffSSoby Mathew SPSR_E_LITTLE, DISABLE_ALL_EXCEPTIONS); 177877cf3ffSSoby Mathew return spsr; 178877cf3ffSSoby Mathew } 179877cf3ffSSoby Mathew #endif /* AARCH32 */ 180bc149bfcSSoby Mathew 181bc149bfcSSoby Mathew /******************************************************************************* 182bc149bfcSSoby Mathew * Configures access to the system counter timer module. 183bc149bfcSSoby Mathew ******************************************************************************/ 184bc149bfcSSoby Mathew #ifdef ARM_SYS_TIMCTL_BASE 185bc149bfcSSoby Mathew void arm_configure_sys_timer(void) 186bc149bfcSSoby Mathew { 187bc149bfcSSoby Mathew unsigned int reg_val; 188bc149bfcSSoby Mathew 189bc149bfcSSoby Mathew #if ARM_CONFIG_CNTACR 190bc149bfcSSoby Mathew reg_val = (1 << CNTACR_RPCT_SHIFT) | (1 << CNTACR_RVCT_SHIFT); 191bc149bfcSSoby Mathew reg_val |= (1 << CNTACR_RFRQ_SHIFT) | (1 << CNTACR_RVOFF_SHIFT); 192bc149bfcSSoby Mathew reg_val |= (1 << CNTACR_RWVT_SHIFT) | (1 << CNTACR_RWPT_SHIFT); 193bc149bfcSSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTACR_BASE(PLAT_ARM_NSTIMER_FRAME_ID), reg_val); 194bc149bfcSSoby Mathew #endif /* ARM_CONFIG_CNTACR */ 195bc149bfcSSoby Mathew 196bc149bfcSSoby Mathew reg_val = (1 << CNTNSAR_NS_SHIFT(PLAT_ARM_NSTIMER_FRAME_ID)); 197bc149bfcSSoby Mathew mmio_write_32(ARM_SYS_TIMCTL_BASE + CNTNSAR, reg_val); 198bc149bfcSSoby Mathew } 199bc149bfcSSoby Mathew #endif /* ARM_SYS_TIMCTL_BASE */ 200bc149bfcSSoby Mathew 201bc149bfcSSoby Mathew /******************************************************************************* 202bc149bfcSSoby Mathew * Returns ARM platform specific memory map regions. 203bc149bfcSSoby Mathew ******************************************************************************/ 204bc149bfcSSoby Mathew const mmap_region_t *plat_arm_get_mmap(void) 205bc149bfcSSoby Mathew { 206bc149bfcSSoby Mathew return plat_arm_mmap; 207bc149bfcSSoby Mathew } 208bc149bfcSSoby Mathew 209bc149bfcSSoby Mathew #ifdef ARM_SYS_CNTCTL_BASE 210bc149bfcSSoby Mathew 211bc149bfcSSoby Mathew unsigned int plat_get_syscnt_freq2(void) 212bc149bfcSSoby Mathew { 213bc149bfcSSoby Mathew unsigned int counter_base_frequency; 214bc149bfcSSoby Mathew 215bc149bfcSSoby Mathew /* Read the frequency from Frequency modes table */ 216bc149bfcSSoby Mathew counter_base_frequency = mmio_read_32(ARM_SYS_CNTCTL_BASE + CNTFID_OFF); 217bc149bfcSSoby Mathew 218bc149bfcSSoby Mathew /* The first entry of the frequency modes table must not be 0 */ 219bc149bfcSSoby Mathew if (counter_base_frequency == 0) 220bc149bfcSSoby Mathew panic(); 221bc149bfcSSoby Mathew 222bc149bfcSSoby Mathew return counter_base_frequency; 223bc149bfcSSoby Mathew } 224bc149bfcSSoby Mathew 225bc149bfcSSoby Mathew #endif /* ARM_SYS_CNTCTL_BASE */ 226