1b2bca61dSSoby Mathew /* 2aa61368eSAntonio Nino Diaz * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3b2bca61dSSoby Mathew * 4b2bca61dSSoby Mathew * Redistribution and use in source and binary forms, with or without 5b2bca61dSSoby Mathew * modification, are permitted provided that the following conditions are met: 6b2bca61dSSoby Mathew * 7b2bca61dSSoby Mathew * Redistributions of source code must retain the above copyright notice, this 8b2bca61dSSoby Mathew * list of conditions and the following disclaimer. 9b2bca61dSSoby Mathew * 10b2bca61dSSoby Mathew * Redistributions in binary form must reproduce the above copyright notice, 11b2bca61dSSoby Mathew * this list of conditions and the following disclaimer in the documentation 12b2bca61dSSoby Mathew * and/or other materials provided with the distribution. 13b2bca61dSSoby Mathew * 14b2bca61dSSoby Mathew * Neither the name of ARM nor the names of its contributors may be used 15b2bca61dSSoby Mathew * to endorse or promote products derived from this software without specific 16b2bca61dSSoby Mathew * prior written permission. 17b2bca61dSSoby Mathew * 18b2bca61dSSoby Mathew * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19b2bca61dSSoby Mathew * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20b2bca61dSSoby Mathew * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21b2bca61dSSoby Mathew * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22b2bca61dSSoby Mathew * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23b2bca61dSSoby Mathew * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24b2bca61dSSoby Mathew * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25b2bca61dSSoby Mathew * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26b2bca61dSSoby Mathew * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27b2bca61dSSoby Mathew * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28b2bca61dSSoby Mathew * POSSIBILITY OF SUCH DAMAGE. 29b2bca61dSSoby Mathew */ 30b2bca61dSSoby Mathew 31b2bca61dSSoby Mathew #include <arch.h> 32b2bca61dSSoby Mathew #include <arch_helpers.h> 33b2bca61dSSoby Mathew #include <assert.h> 34b2bca61dSSoby Mathew #include <cassert.h> 35b2bca61dSSoby Mathew #include <platform_def.h> 36b2bca61dSSoby Mathew #include <utils.h> 37b2bca61dSSoby Mathew #include <xlat_tables.h> 38b2bca61dSSoby Mathew #include "../xlat_tables_private.h" 39b2bca61dSSoby Mathew 40b2bca61dSSoby Mathew /* 41e8719552SAntonio Nino Diaz * Each platform can define the size of the virtual address space, which is 420029624fSAntonio Nino Diaz * defined in PLAT_VIRT_ADDR_SPACE_SIZE. TTBCR.TxSZ is calculated as 32 minus 430029624fSAntonio Nino Diaz * the width of said address space. The value of TTBCR.TxSZ must be in the 440029624fSAntonio Nino Diaz * range 0 to 7 [1], which means that the virtual address space width must be 450029624fSAntonio Nino Diaz * in the range 32 to 25 bits. 46e8719552SAntonio Nino Diaz * 470029624fSAntonio Nino Diaz * Here we calculate the initial lookup level from the value of 480029624fSAntonio Nino Diaz * PLAT_VIRT_ADDR_SPACE_SIZE. For a 4 KB page size, level 1 supports virtual 490029624fSAntonio Nino Diaz * address spaces of widths 32 to 31 bits, and level 2 from 30 to 25. Wider or 500029624fSAntonio Nino Diaz * narrower address spaces are not supported. As a result, level 3 cannot be 510029624fSAntonio Nino Diaz * used as initial lookup level with 4 KB granularity [1]. 52e8719552SAntonio Nino Diaz * 530029624fSAntonio Nino Diaz * For example, for a 31-bit address space (i.e. PLAT_VIRT_ADDR_SPACE_SIZE == 540029624fSAntonio Nino Diaz * 1 << 31), TTBCR.TxSZ will be programmed to (32 - 31) = 1. According to Table 550029624fSAntonio Nino Diaz * G4-5 in the ARM ARM, the initial lookup level for an address space like that 560029624fSAntonio Nino Diaz * is 1. 57e8719552SAntonio Nino Diaz * 58e8719552SAntonio Nino Diaz * See the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more 59e8719552SAntonio Nino Diaz * information: 60e8719552SAntonio Nino Diaz * [1] Section G4.6.5 61b2bca61dSSoby Mathew */ 62b2bca61dSSoby Mathew 630029624fSAntonio Nino Diaz #if PLAT_VIRT_ADDR_SPACE_SIZE > (1ULL << (32 - TTBCR_TxSZ_MIN)) 64b2bca61dSSoby Mathew 650029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too big." 66e8719552SAntonio Nino Diaz 670029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE > (1 << L1_XLAT_ADDRESS_SHIFT) 68e8719552SAntonio Nino Diaz 69e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE 1 700029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES \ 710029624fSAntonio Nino Diaz (PLAT_VIRT_ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT) 72e8719552SAntonio Nino Diaz 730029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE >= (1 << (32 - TTBCR_TxSZ_MAX)) 74e8719552SAntonio Nino Diaz 75e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE 2 760029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES \ 770029624fSAntonio Nino Diaz (PLAT_VIRT_ADDR_SPACE_SIZE >> L2_XLAT_ADDRESS_SHIFT) 78e8719552SAntonio Nino Diaz 79e8719552SAntonio Nino Diaz #else 80e8719552SAntonio Nino Diaz 810029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too small." 82e8719552SAntonio Nino Diaz 83e8719552SAntonio Nino Diaz #endif 84e8719552SAntonio Nino Diaz 85e8719552SAntonio Nino Diaz static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES] 86e8719552SAntonio Nino Diaz __aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t)); 87b2bca61dSSoby Mathew 88aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS 890029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void) 900029624fSAntonio Nino Diaz { 910029624fSAntonio Nino Diaz /* Physical address space size for long descriptor format. */ 920029624fSAntonio Nino Diaz return (1ULL << 40) - 1ULL; 930029624fSAntonio Nino Diaz } 94aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */ 950029624fSAntonio Nino Diaz 96*a5640252SAntonio Nino Diaz int xlat_arch_current_el(void) 97*a5640252SAntonio Nino Diaz { 98*a5640252SAntonio Nino Diaz /* 99*a5640252SAntonio Nino Diaz * If EL3 is in AArch32 mode, all secure PL1 modes (Monitor, System, 100*a5640252SAntonio Nino Diaz * SVC, Abort, UND, IRQ and FIQ modes) execute at EL3. 101*a5640252SAntonio Nino Diaz */ 102*a5640252SAntonio Nino Diaz return 3; 103*a5640252SAntonio Nino Diaz } 104*a5640252SAntonio Nino Diaz 105*a5640252SAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(int el __unused) 106*a5640252SAntonio Nino Diaz { 107*a5640252SAntonio Nino Diaz return UPPER_ATTRS(XN); 108*a5640252SAntonio Nino Diaz } 109*a5640252SAntonio Nino Diaz 110b2bca61dSSoby Mathew void init_xlat_tables(void) 111b2bca61dSSoby Mathew { 112b2bca61dSSoby Mathew unsigned long long max_pa; 113b2bca61dSSoby Mathew uintptr_t max_va; 114b2bca61dSSoby Mathew print_mmap(); 115e8719552SAntonio Nino Diaz init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE, 116e8719552SAntonio Nino Diaz &max_va, &max_pa); 1170029624fSAntonio Nino Diaz 1180029624fSAntonio Nino Diaz assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1); 1190029624fSAntonio Nino Diaz assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1); 1200029624fSAntonio Nino Diaz assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= get_max_supported_pa()); 121b2bca61dSSoby Mathew } 122b2bca61dSSoby Mathew 123b2bca61dSSoby Mathew /******************************************************************************* 124b2bca61dSSoby Mathew * Function for enabling the MMU in Secure PL1, assuming that the 125b2bca61dSSoby Mathew * page-tables have already been created. 126b2bca61dSSoby Mathew ******************************************************************************/ 127b2bca61dSSoby Mathew void enable_mmu_secure(unsigned int flags) 128b2bca61dSSoby Mathew { 129b2bca61dSSoby Mathew unsigned int mair0, ttbcr, sctlr; 130b2bca61dSSoby Mathew uint64_t ttbr0; 131b2bca61dSSoby Mathew 132b2bca61dSSoby Mathew assert(IS_IN_SECURE()); 133b2bca61dSSoby Mathew assert((read_sctlr() & SCTLR_M_BIT) == 0); 134b2bca61dSSoby Mathew 135b2bca61dSSoby Mathew /* Set attributes in the right indices of the MAIR */ 136b2bca61dSSoby Mathew mair0 = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); 137b2bca61dSSoby Mathew mair0 |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, 138b2bca61dSSoby Mathew ATTR_IWBWA_OWBWA_NTR_INDEX); 139b2bca61dSSoby Mathew mair0 |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE, 140b2bca61dSSoby Mathew ATTR_NON_CACHEABLE_INDEX); 141b2bca61dSSoby Mathew write_mair0(mair0); 142b2bca61dSSoby Mathew 143b2bca61dSSoby Mathew /* Invalidate TLBs at the current exception level */ 144b2bca61dSSoby Mathew tlbiall(); 145b2bca61dSSoby Mathew 146b2bca61dSSoby Mathew /* 1475d21b037SSummer Qin * Set TTBCR bits as well. Set TTBR0 table properties. Disable TTBR1. 148b2bca61dSSoby Mathew */ 1495d21b037SSummer Qin if (flags & XLAT_TABLE_NC) { 1505d21b037SSummer Qin /* Inner & outer non-cacheable non-shareable. */ 1515d21b037SSummer Qin ttbcr = TTBCR_EAE_BIT | 1525d21b037SSummer Qin TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC | 1535d21b037SSummer Qin TTBCR_RGN0_INNER_NC | 1545d21b037SSummer Qin (32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE)); 1555d21b037SSummer Qin } else { 1565d21b037SSummer Qin /* Inner & outer WBWA & shareable. */ 157b2bca61dSSoby Mathew ttbcr = TTBCR_EAE_BIT | 158b2bca61dSSoby Mathew TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA | 159b2bca61dSSoby Mathew TTBCR_RGN0_INNER_WBA | 1600029624fSAntonio Nino Diaz (32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE)); 1615d21b037SSummer Qin } 162b2bca61dSSoby Mathew ttbcr |= TTBCR_EPD1_BIT; 163b2bca61dSSoby Mathew write_ttbcr(ttbcr); 164b2bca61dSSoby Mathew 165b2bca61dSSoby Mathew /* Set TTBR0 bits as well */ 166e8719552SAntonio Nino Diaz ttbr0 = (uintptr_t) base_xlation_table; 167b2bca61dSSoby Mathew write64_ttbr0(ttbr0); 168b2bca61dSSoby Mathew write64_ttbr1(0); 169b2bca61dSSoby Mathew 170b2bca61dSSoby Mathew /* 171b2bca61dSSoby Mathew * Ensure all translation table writes have drained 172b2bca61dSSoby Mathew * into memory, the TLB invalidation is complete, 173b2bca61dSSoby Mathew * and translation register writes are committed 174b2bca61dSSoby Mathew * before enabling the MMU 175b2bca61dSSoby Mathew */ 176b2bca61dSSoby Mathew dsb(); 177b2bca61dSSoby Mathew isb(); 178b2bca61dSSoby Mathew 179b2bca61dSSoby Mathew sctlr = read_sctlr(); 180b2bca61dSSoby Mathew sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; 181b2bca61dSSoby Mathew 182b2bca61dSSoby Mathew if (flags & DISABLE_DCACHE) 183b2bca61dSSoby Mathew sctlr &= ~SCTLR_C_BIT; 184b2bca61dSSoby Mathew else 185b2bca61dSSoby Mathew sctlr |= SCTLR_C_BIT; 186b2bca61dSSoby Mathew 187b2bca61dSSoby Mathew write_sctlr(sctlr); 188b2bca61dSSoby Mathew 189b2bca61dSSoby Mathew /* Ensure the MMU enable takes effect immediately */ 190b2bca61dSSoby Mathew isb(); 191b2bca61dSSoby Mathew } 192