1b2bca61dSSoby Mathew /* 2aa61368eSAntonio Nino Diaz * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved. 3b2bca61dSSoby Mathew * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 5b2bca61dSSoby Mathew */ 6b2bca61dSSoby Mathew 7b2bca61dSSoby Mathew #include <arch.h> 8b2bca61dSSoby Mathew #include <arch_helpers.h> 9b2bca61dSSoby Mathew #include <assert.h> 10b2bca61dSSoby Mathew #include <cassert.h> 11b2bca61dSSoby Mathew #include <platform_def.h> 12b2bca61dSSoby Mathew #include <utils.h> 13b2bca61dSSoby Mathew #include <xlat_tables.h> 14b2bca61dSSoby Mathew #include "../xlat_tables_private.h" 15b2bca61dSSoby Mathew 16b2bca61dSSoby Mathew /* 17e8719552SAntonio Nino Diaz * Each platform can define the size of the virtual address space, which is 180029624fSAntonio Nino Diaz * defined in PLAT_VIRT_ADDR_SPACE_SIZE. TTBCR.TxSZ is calculated as 32 minus 190029624fSAntonio Nino Diaz * the width of said address space. The value of TTBCR.TxSZ must be in the 200029624fSAntonio Nino Diaz * range 0 to 7 [1], which means that the virtual address space width must be 210029624fSAntonio Nino Diaz * in the range 32 to 25 bits. 22e8719552SAntonio Nino Diaz * 230029624fSAntonio Nino Diaz * Here we calculate the initial lookup level from the value of 240029624fSAntonio Nino Diaz * PLAT_VIRT_ADDR_SPACE_SIZE. For a 4 KB page size, level 1 supports virtual 250029624fSAntonio Nino Diaz * address spaces of widths 32 to 31 bits, and level 2 from 30 to 25. Wider or 260029624fSAntonio Nino Diaz * narrower address spaces are not supported. As a result, level 3 cannot be 270029624fSAntonio Nino Diaz * used as initial lookup level with 4 KB granularity [1]. 28e8719552SAntonio Nino Diaz * 290029624fSAntonio Nino Diaz * For example, for a 31-bit address space (i.e. PLAT_VIRT_ADDR_SPACE_SIZE == 300029624fSAntonio Nino Diaz * 1 << 31), TTBCR.TxSZ will be programmed to (32 - 31) = 1. According to Table 310029624fSAntonio Nino Diaz * G4-5 in the ARM ARM, the initial lookup level for an address space like that 320029624fSAntonio Nino Diaz * is 1. 33e8719552SAntonio Nino Diaz * 34e8719552SAntonio Nino Diaz * See the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more 35e8719552SAntonio Nino Diaz * information: 36e8719552SAntonio Nino Diaz * [1] Section G4.6.5 37b2bca61dSSoby Mathew */ 38b2bca61dSSoby Mathew 390029624fSAntonio Nino Diaz #if PLAT_VIRT_ADDR_SPACE_SIZE > (1ULL << (32 - TTBCR_TxSZ_MIN)) 40b2bca61dSSoby Mathew 410029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too big." 42e8719552SAntonio Nino Diaz 430029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE > (1 << L1_XLAT_ADDRESS_SHIFT) 44e8719552SAntonio Nino Diaz 45e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE 1 460029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES \ 470029624fSAntonio Nino Diaz (PLAT_VIRT_ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT) 48e8719552SAntonio Nino Diaz 490029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE >= (1 << (32 - TTBCR_TxSZ_MAX)) 50e8719552SAntonio Nino Diaz 51e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE 2 520029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES \ 530029624fSAntonio Nino Diaz (PLAT_VIRT_ADDR_SPACE_SIZE >> L2_XLAT_ADDRESS_SHIFT) 54e8719552SAntonio Nino Diaz 55e8719552SAntonio Nino Diaz #else 56e8719552SAntonio Nino Diaz 570029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too small." 58e8719552SAntonio Nino Diaz 59e8719552SAntonio Nino Diaz #endif 60e8719552SAntonio Nino Diaz 61e8719552SAntonio Nino Diaz static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES] 62e8719552SAntonio Nino Diaz __aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t)); 63b2bca61dSSoby Mathew 64aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS 650029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void) 660029624fSAntonio Nino Diaz { 670029624fSAntonio Nino Diaz /* Physical address space size for long descriptor format. */ 680029624fSAntonio Nino Diaz return (1ULL << 40) - 1ULL; 690029624fSAntonio Nino Diaz } 70aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */ 710029624fSAntonio Nino Diaz 72a5640252SAntonio Nino Diaz int xlat_arch_current_el(void) 73a5640252SAntonio Nino Diaz { 74a5640252SAntonio Nino Diaz /* 75a5640252SAntonio Nino Diaz * If EL3 is in AArch32 mode, all secure PL1 modes (Monitor, System, 76a5640252SAntonio Nino Diaz * SVC, Abort, UND, IRQ and FIQ modes) execute at EL3. 77a5640252SAntonio Nino Diaz */ 78a5640252SAntonio Nino Diaz return 3; 79a5640252SAntonio Nino Diaz } 80a5640252SAntonio Nino Diaz 81a5640252SAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(int el __unused) 82a5640252SAntonio Nino Diaz { 83a5640252SAntonio Nino Diaz return UPPER_ATTRS(XN); 84a5640252SAntonio Nino Diaz } 85a5640252SAntonio Nino Diaz 86b2bca61dSSoby Mathew void init_xlat_tables(void) 87b2bca61dSSoby Mathew { 88b2bca61dSSoby Mathew unsigned long long max_pa; 89b2bca61dSSoby Mathew uintptr_t max_va; 90b2bca61dSSoby Mathew print_mmap(); 91e8719552SAntonio Nino Diaz init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE, 92e8719552SAntonio Nino Diaz &max_va, &max_pa); 930029624fSAntonio Nino Diaz 940029624fSAntonio Nino Diaz assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1); 950029624fSAntonio Nino Diaz assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1); 960029624fSAntonio Nino Diaz assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= get_max_supported_pa()); 97b2bca61dSSoby Mathew } 98b2bca61dSSoby Mathew 99b2bca61dSSoby Mathew /******************************************************************************* 100b2bca61dSSoby Mathew * Function for enabling the MMU in Secure PL1, assuming that the 101b2bca61dSSoby Mathew * page-tables have already been created. 102b2bca61dSSoby Mathew ******************************************************************************/ 103b2bca61dSSoby Mathew void enable_mmu_secure(unsigned int flags) 104b2bca61dSSoby Mathew { 105b2bca61dSSoby Mathew unsigned int mair0, ttbcr, sctlr; 106b2bca61dSSoby Mathew uint64_t ttbr0; 107b2bca61dSSoby Mathew 108b2bca61dSSoby Mathew assert(IS_IN_SECURE()); 109b2bca61dSSoby Mathew assert((read_sctlr() & SCTLR_M_BIT) == 0); 110b2bca61dSSoby Mathew 111b2bca61dSSoby Mathew /* Set attributes in the right indices of the MAIR */ 112b2bca61dSSoby Mathew mair0 = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); 113b2bca61dSSoby Mathew mair0 |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, 114b2bca61dSSoby Mathew ATTR_IWBWA_OWBWA_NTR_INDEX); 115b2bca61dSSoby Mathew mair0 |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE, 116b2bca61dSSoby Mathew ATTR_NON_CACHEABLE_INDEX); 117b2bca61dSSoby Mathew write_mair0(mair0); 118b2bca61dSSoby Mathew 119b2bca61dSSoby Mathew /* Invalidate TLBs at the current exception level */ 120b2bca61dSSoby Mathew tlbiall(); 121b2bca61dSSoby Mathew 122b2bca61dSSoby Mathew /* 1235d21b037SSummer Qin * Set TTBCR bits as well. Set TTBR0 table properties. Disable TTBR1. 124b2bca61dSSoby Mathew */ 1255d21b037SSummer Qin if (flags & XLAT_TABLE_NC) { 1265d21b037SSummer Qin /* Inner & outer non-cacheable non-shareable. */ 1275d21b037SSummer Qin ttbcr = TTBCR_EAE_BIT | 1285d21b037SSummer Qin TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC | 1295d21b037SSummer Qin TTBCR_RGN0_INNER_NC | 1305d21b037SSummer Qin (32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE)); 1315d21b037SSummer Qin } else { 1325d21b037SSummer Qin /* Inner & outer WBWA & shareable. */ 133b2bca61dSSoby Mathew ttbcr = TTBCR_EAE_BIT | 134b2bca61dSSoby Mathew TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA | 135b2bca61dSSoby Mathew TTBCR_RGN0_INNER_WBA | 1360029624fSAntonio Nino Diaz (32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE)); 1375d21b037SSummer Qin } 138b2bca61dSSoby Mathew ttbcr |= TTBCR_EPD1_BIT; 139b2bca61dSSoby Mathew write_ttbcr(ttbcr); 140b2bca61dSSoby Mathew 141b2bca61dSSoby Mathew /* Set TTBR0 bits as well */ 142e8719552SAntonio Nino Diaz ttbr0 = (uintptr_t) base_xlation_table; 143b2bca61dSSoby Mathew write64_ttbr0(ttbr0); 144b2bca61dSSoby Mathew write64_ttbr1(0); 145b2bca61dSSoby Mathew 146b2bca61dSSoby Mathew /* 147b2bca61dSSoby Mathew * Ensure all translation table writes have drained 148b2bca61dSSoby Mathew * into memory, the TLB invalidation is complete, 149b2bca61dSSoby Mathew * and translation register writes are committed 150b2bca61dSSoby Mathew * before enabling the MMU 151b2bca61dSSoby Mathew */ 152*6f512a3dSDimitris Papastamos dsbish(); 153b2bca61dSSoby Mathew isb(); 154b2bca61dSSoby Mathew 155b2bca61dSSoby Mathew sctlr = read_sctlr(); 156b2bca61dSSoby Mathew sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; 157b2bca61dSSoby Mathew 158b2bca61dSSoby Mathew if (flags & DISABLE_DCACHE) 159b2bca61dSSoby Mathew sctlr &= ~SCTLR_C_BIT; 160b2bca61dSSoby Mathew else 161b2bca61dSSoby Mathew sctlr |= SCTLR_C_BIT; 162b2bca61dSSoby Mathew 163b2bca61dSSoby Mathew write_sctlr(sctlr); 164b2bca61dSSoby Mathew 165b2bca61dSSoby Mathew /* Ensure the MMU enable takes effect immediately */ 166b2bca61dSSoby Mathew isb(); 167b2bca61dSSoby Mathew } 168