13ca9928dSSoby Mathew /* 25d21b037SSummer Qin * Copyright (c) 2014-2017, ARM Limited and Contributors. All rights reserved. 33ca9928dSSoby Mathew * 4*82cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 53ca9928dSSoby Mathew */ 63ca9928dSSoby Mathew 73ca9928dSSoby Mathew #include <arch.h> 83ca9928dSSoby Mathew #include <arch_helpers.h> 93ca9928dSSoby Mathew #include <assert.h> 100029624fSAntonio Nino Diaz #include <bl_common.h> 113ca9928dSSoby Mathew #include <cassert.h> 120029624fSAntonio Nino Diaz #include <common_def.h> 133ca9928dSSoby Mathew #include <platform_def.h> 140029624fSAntonio Nino Diaz #include <sys/types.h> 15ed81f3ebSSandrine Bailleux #include <utils.h> 163ca9928dSSoby Mathew #include <xlat_tables.h> 173ca9928dSSoby Mathew #include "../xlat_tables_private.h" 183ca9928dSSoby Mathew 193ca9928dSSoby Mathew /* 20e8719552SAntonio Nino Diaz * Each platform can define the size of the virtual address space, which is 210029624fSAntonio Nino Diaz * defined in PLAT_VIRT_ADDR_SPACE_SIZE. TCR.TxSZ is calculated as 64 minus the 220029624fSAntonio Nino Diaz * width of said address space. The value of TCR.TxSZ must be in the range 16 230029624fSAntonio Nino Diaz * to 39 [1], which means that the virtual address space width must be in the 240029624fSAntonio Nino Diaz * range 48 to 25 bits. 25e8719552SAntonio Nino Diaz * 260029624fSAntonio Nino Diaz * Here we calculate the initial lookup level from the value of 270029624fSAntonio Nino Diaz * PLAT_VIRT_ADDR_SPACE_SIZE. For a 4 KB page size, level 0 supports virtual 280029624fSAntonio Nino Diaz * address spaces of widths 48 to 40 bits, level 1 from 39 to 31, and level 2 290029624fSAntonio Nino Diaz * from 30 to 25. Wider or narrower address spaces are not supported. As a 300029624fSAntonio Nino Diaz * result, level 3 cannot be used as initial lookup level with 4 KB 310029624fSAntonio Nino Diaz * granularity. [2] 32e8719552SAntonio Nino Diaz * 330029624fSAntonio Nino Diaz * For example, for a 35-bit address space (i.e. PLAT_VIRT_ADDR_SPACE_SIZE == 340029624fSAntonio Nino Diaz * 1 << 35), TCR.TxSZ will be programmed to (64 - 35) = 29. According to Table 350029624fSAntonio Nino Diaz * D4-11 in the ARM ARM, the initial lookup level for an address space like 360029624fSAntonio Nino Diaz * that is 1. 37e8719552SAntonio Nino Diaz * 38e8719552SAntonio Nino Diaz * See the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more 39e8719552SAntonio Nino Diaz * information: 40e8719552SAntonio Nino Diaz * [1] Page 1730: 'Input address size', 'For all translation stages'. 41e8719552SAntonio Nino Diaz * [2] Section D4.2.5 423ca9928dSSoby Mathew */ 433ca9928dSSoby Mathew 440029624fSAntonio Nino Diaz #if PLAT_VIRT_ADDR_SPACE_SIZE > (1ULL << (64 - TCR_TxSZ_MIN)) 453ca9928dSSoby Mathew 460029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too big." 47e8719552SAntonio Nino Diaz 480029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE > (1ULL << L0_XLAT_ADDRESS_SHIFT) 49e8719552SAntonio Nino Diaz 50e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE 0 510029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES \ 520029624fSAntonio Nino Diaz (PLAT_VIRT_ADDR_SPACE_SIZE >> L0_XLAT_ADDRESS_SHIFT) 53e8719552SAntonio Nino Diaz 540029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE > (1 << L1_XLAT_ADDRESS_SHIFT) 55e8719552SAntonio Nino Diaz 56e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE 1 570029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES \ 580029624fSAntonio Nino Diaz (PLAT_VIRT_ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT) 59e8719552SAntonio Nino Diaz 600029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE >= (1 << (64 - TCR_TxSZ_MAX)) 61e8719552SAntonio Nino Diaz 62e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE 2 630029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES \ 640029624fSAntonio Nino Diaz (PLAT_VIRT_ADDR_SPACE_SIZE >> L2_XLAT_ADDRESS_SHIFT) 65e8719552SAntonio Nino Diaz 66e8719552SAntonio Nino Diaz #else 67e8719552SAntonio Nino Diaz 680029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too small." 69e8719552SAntonio Nino Diaz 70e8719552SAntonio Nino Diaz #endif 71e8719552SAntonio Nino Diaz 72e8719552SAntonio Nino Diaz static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES] 73e8719552SAntonio Nino Diaz __aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t)); 743ca9928dSSoby Mathew 753ca9928dSSoby Mathew static unsigned long long tcr_ps_bits; 763ca9928dSSoby Mathew 773ca9928dSSoby Mathew static unsigned long long calc_physical_addr_size_bits( 783ca9928dSSoby Mathew unsigned long long max_addr) 793ca9928dSSoby Mathew { 803ca9928dSSoby Mathew /* Physical address can't exceed 48 bits */ 813ca9928dSSoby Mathew assert((max_addr & ADDR_MASK_48_TO_63) == 0); 823ca9928dSSoby Mathew 833ca9928dSSoby Mathew /* 48 bits address */ 843ca9928dSSoby Mathew if (max_addr & ADDR_MASK_44_TO_47) 853ca9928dSSoby Mathew return TCR_PS_BITS_256TB; 863ca9928dSSoby Mathew 873ca9928dSSoby Mathew /* 44 bits address */ 883ca9928dSSoby Mathew if (max_addr & ADDR_MASK_42_TO_43) 893ca9928dSSoby Mathew return TCR_PS_BITS_16TB; 903ca9928dSSoby Mathew 913ca9928dSSoby Mathew /* 42 bits address */ 923ca9928dSSoby Mathew if (max_addr & ADDR_MASK_40_TO_41) 933ca9928dSSoby Mathew return TCR_PS_BITS_4TB; 943ca9928dSSoby Mathew 953ca9928dSSoby Mathew /* 40 bits address */ 963ca9928dSSoby Mathew if (max_addr & ADDR_MASK_36_TO_39) 973ca9928dSSoby Mathew return TCR_PS_BITS_1TB; 983ca9928dSSoby Mathew 993ca9928dSSoby Mathew /* 36 bits address */ 1003ca9928dSSoby Mathew if (max_addr & ADDR_MASK_32_TO_35) 1013ca9928dSSoby Mathew return TCR_PS_BITS_64GB; 1023ca9928dSSoby Mathew 1033ca9928dSSoby Mathew return TCR_PS_BITS_4GB; 1043ca9928dSSoby Mathew } 1053ca9928dSSoby Mathew 106aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS 1070029624fSAntonio Nino Diaz /* Physical Address ranges supported in the AArch64 Memory Model */ 1080029624fSAntonio Nino Diaz static const unsigned int pa_range_bits_arr[] = { 1090029624fSAntonio Nino Diaz PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011, PARANGE_0100, 1100029624fSAntonio Nino Diaz PARANGE_0101 1110029624fSAntonio Nino Diaz }; 1120029624fSAntonio Nino Diaz 1130029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void) 1140029624fSAntonio Nino Diaz { 1150029624fSAntonio Nino Diaz u_register_t pa_range = read_id_aa64mmfr0_el1() & 1160029624fSAntonio Nino Diaz ID_AA64MMFR0_EL1_PARANGE_MASK; 1170029624fSAntonio Nino Diaz 1180029624fSAntonio Nino Diaz /* All other values are reserved */ 1190029624fSAntonio Nino Diaz assert(pa_range < ARRAY_SIZE(pa_range_bits_arr)); 1200029624fSAntonio Nino Diaz 1210029624fSAntonio Nino Diaz return (1ULL << pa_range_bits_arr[pa_range]) - 1ULL; 1220029624fSAntonio Nino Diaz } 123aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */ 1240029624fSAntonio Nino Diaz 1253ca9928dSSoby Mathew void init_xlat_tables(void) 1263ca9928dSSoby Mathew { 1273ca9928dSSoby Mathew unsigned long long max_pa; 1283ca9928dSSoby Mathew uintptr_t max_va; 1293ca9928dSSoby Mathew print_mmap(); 130e8719552SAntonio Nino Diaz init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE, 131e8719552SAntonio Nino Diaz &max_va, &max_pa); 1320029624fSAntonio Nino Diaz 1330029624fSAntonio Nino Diaz assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1); 1340029624fSAntonio Nino Diaz assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1); 1350029624fSAntonio Nino Diaz assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= get_max_supported_pa()); 1360029624fSAntonio Nino Diaz 1373ca9928dSSoby Mathew tcr_ps_bits = calc_physical_addr_size_bits(max_pa); 1383ca9928dSSoby Mathew } 1393ca9928dSSoby Mathew 1403ca9928dSSoby Mathew /******************************************************************************* 1413ca9928dSSoby Mathew * Macro generating the code for the function enabling the MMU in the given 1423ca9928dSSoby Mathew * exception level, assuming that the pagetables have already been created. 1433ca9928dSSoby Mathew * 1443ca9928dSSoby Mathew * _el: Exception level at which the function will run 1453ca9928dSSoby Mathew * _tcr_extra: Extra bits to set in the TCR register. This mask will 1463ca9928dSSoby Mathew * be OR'ed with the default TCR value. 1473ca9928dSSoby Mathew * _tlbi_fct: Function to invalidate the TLBs at the current 1483ca9928dSSoby Mathew * exception level 1493ca9928dSSoby Mathew ******************************************************************************/ 1503ca9928dSSoby Mathew #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \ 1513ca9928dSSoby Mathew void enable_mmu_el##_el(unsigned int flags) \ 1523ca9928dSSoby Mathew { \ 1533ca9928dSSoby Mathew uint64_t mair, tcr, ttbr; \ 1543ca9928dSSoby Mathew uint32_t sctlr; \ 1553ca9928dSSoby Mathew \ 1563ca9928dSSoby Mathew assert(IS_IN_EL(_el)); \ 1573ca9928dSSoby Mathew assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0); \ 1583ca9928dSSoby Mathew \ 1593ca9928dSSoby Mathew /* Set attributes in the right indices of the MAIR */ \ 1603ca9928dSSoby Mathew mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \ 1613ca9928dSSoby Mathew mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \ 1623ca9928dSSoby Mathew ATTR_IWBWA_OWBWA_NTR_INDEX); \ 1633ca9928dSSoby Mathew mair |= MAIR_ATTR_SET(ATTR_NON_CACHEABLE, \ 1643ca9928dSSoby Mathew ATTR_NON_CACHEABLE_INDEX); \ 1653ca9928dSSoby Mathew write_mair_el##_el(mair); \ 1663ca9928dSSoby Mathew \ 1673ca9928dSSoby Mathew /* Invalidate TLBs at the current exception level */ \ 1683ca9928dSSoby Mathew _tlbi_fct(); \ 1693ca9928dSSoby Mathew \ 1703ca9928dSSoby Mathew /* Set TCR bits as well. */ \ 171e8719552SAntonio Nino Diaz /* Set T0SZ to (64 - width of virtual address space) */ \ 1725d21b037SSummer Qin if (flags & XLAT_TABLE_NC) { \ 1735d21b037SSummer Qin /* Inner & outer non-cacheable non-shareable. */\ 1745d21b037SSummer Qin tcr = TCR_SH_NON_SHAREABLE | \ 1755d21b037SSummer Qin TCR_RGN_OUTER_NC | TCR_RGN_INNER_NC | \ 1760029624fSAntonio Nino Diaz (64 - __builtin_ctzl(PLAT_VIRT_ADDR_SPACE_SIZE));\ 1775d21b037SSummer Qin } else { \ 1785d21b037SSummer Qin /* Inner & outer WBWA & shareable. */ \ 1795d21b037SSummer Qin tcr = TCR_SH_INNER_SHAREABLE | \ 1805d21b037SSummer Qin TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA | \ 1815d21b037SSummer Qin (64 - __builtin_ctzl(PLAT_VIRT_ADDR_SPACE_SIZE));\ 1825d21b037SSummer Qin } \ 1833ca9928dSSoby Mathew tcr |= _tcr_extra; \ 1843ca9928dSSoby Mathew write_tcr_el##_el(tcr); \ 1853ca9928dSSoby Mathew \ 1863ca9928dSSoby Mathew /* Set TTBR bits as well */ \ 187e8719552SAntonio Nino Diaz ttbr = (uint64_t) base_xlation_table; \ 1883ca9928dSSoby Mathew write_ttbr0_el##_el(ttbr); \ 1893ca9928dSSoby Mathew \ 1903ca9928dSSoby Mathew /* Ensure all translation table writes have drained */ \ 1913ca9928dSSoby Mathew /* into memory, the TLB invalidation is complete, */ \ 1923ca9928dSSoby Mathew /* and translation register writes are committed */ \ 1933ca9928dSSoby Mathew /* before enabling the MMU */ \ 194ccbec91cSAntonio Nino Diaz dsbish(); \ 1953ca9928dSSoby Mathew isb(); \ 1963ca9928dSSoby Mathew \ 1973ca9928dSSoby Mathew sctlr = read_sctlr_el##_el(); \ 1983ca9928dSSoby Mathew sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \ 1993ca9928dSSoby Mathew \ 2003ca9928dSSoby Mathew if (flags & DISABLE_DCACHE) \ 2013ca9928dSSoby Mathew sctlr &= ~SCTLR_C_BIT; \ 2023ca9928dSSoby Mathew else \ 2033ca9928dSSoby Mathew sctlr |= SCTLR_C_BIT; \ 2043ca9928dSSoby Mathew \ 2053ca9928dSSoby Mathew write_sctlr_el##_el(sctlr); \ 2063ca9928dSSoby Mathew \ 2073ca9928dSSoby Mathew /* Ensure the MMU enable takes effect immediately */ \ 2083ca9928dSSoby Mathew isb(); \ 2093ca9928dSSoby Mathew } 2103ca9928dSSoby Mathew 2113ca9928dSSoby Mathew /* Define EL1 and EL3 variants of the function enabling the MMU */ 2123ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(1, 2133ca9928dSSoby Mathew (tcr_ps_bits << TCR_EL1_IPS_SHIFT), 2143ca9928dSSoby Mathew tlbivmalle1) 2153ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(3, 2163ca9928dSSoby Mathew TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT), 2173ca9928dSSoby Mathew tlbialle3) 218