13ca9928dSSoby Mathew /* 2d3c4487cSAntonio Nino Diaz * Copyright (c) 2014-2018, ARM Limited and Contributors. All rights reserved. 33ca9928dSSoby Mathew * 482cb2c1aSdp-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> 110029624fSAntonio Nino Diaz #include <common_def.h> 123ca9928dSSoby Mathew #include <platform_def.h> 130029624fSAntonio Nino Diaz #include <sys/types.h> 14ed81f3ebSSandrine Bailleux #include <utils.h> 153ca9928dSSoby Mathew #include <xlat_tables.h> 168933c34bSSandrine Bailleux #include <xlat_tables_arch.h> 173ca9928dSSoby Mathew #include "../xlat_tables_private.h" 183ca9928dSSoby Mathew 198933c34bSSandrine Bailleux #define XLAT_TABLE_LEVEL_BASE \ 208933c34bSSandrine Bailleux GET_XLAT_TABLE_LEVEL_BASE(PLAT_VIRT_ADDR_SPACE_SIZE) 213ca9928dSSoby Mathew 220029624fSAntonio Nino Diaz #define NUM_BASE_LEVEL_ENTRIES \ 238933c34bSSandrine Bailleux GET_NUM_BASE_LEVEL_ENTRIES(PLAT_VIRT_ADDR_SPACE_SIZE) 24e8719552SAntonio Nino Diaz 25e8719552SAntonio Nino Diaz static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES] 26e8719552SAntonio Nino Diaz __aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t)); 273ca9928dSSoby Mathew 283ca9928dSSoby Mathew static unsigned long long tcr_ps_bits; 293ca9928dSSoby Mathew 303ca9928dSSoby Mathew static unsigned long long calc_physical_addr_size_bits( 313ca9928dSSoby Mathew unsigned long long max_addr) 323ca9928dSSoby Mathew { 333ca9928dSSoby Mathew /* Physical address can't exceed 48 bits */ 34*e7b9886cSAntonio Nino Diaz assert((max_addr & ADDR_MASK_48_TO_63) == 0U); 353ca9928dSSoby Mathew 363ca9928dSSoby Mathew /* 48 bits address */ 37*e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_44_TO_47) != 0U) 383ca9928dSSoby Mathew return TCR_PS_BITS_256TB; 393ca9928dSSoby Mathew 403ca9928dSSoby Mathew /* 44 bits address */ 41*e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_42_TO_43) != 0U) 423ca9928dSSoby Mathew return TCR_PS_BITS_16TB; 433ca9928dSSoby Mathew 443ca9928dSSoby Mathew /* 42 bits address */ 45*e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_40_TO_41) != 0U) 463ca9928dSSoby Mathew return TCR_PS_BITS_4TB; 473ca9928dSSoby Mathew 483ca9928dSSoby Mathew /* 40 bits address */ 49*e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_36_TO_39) != 0U) 503ca9928dSSoby Mathew return TCR_PS_BITS_1TB; 513ca9928dSSoby Mathew 523ca9928dSSoby Mathew /* 36 bits address */ 53*e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_32_TO_35) != 0U) 543ca9928dSSoby Mathew return TCR_PS_BITS_64GB; 553ca9928dSSoby Mathew 563ca9928dSSoby Mathew return TCR_PS_BITS_4GB; 573ca9928dSSoby Mathew } 583ca9928dSSoby Mathew 59aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS 60d3c4487cSAntonio Nino Diaz /* 61d3c4487cSAntonio Nino Diaz * Physical Address ranges supported in the AArch64 Memory Model. Value 0b110 is 62d3c4487cSAntonio Nino Diaz * supported in ARMv8.2 onwards. 63d3c4487cSAntonio Nino Diaz */ 640029624fSAntonio Nino Diaz static const unsigned int pa_range_bits_arr[] = { 650029624fSAntonio Nino Diaz PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011, PARANGE_0100, 66d3c4487cSAntonio Nino Diaz PARANGE_0101, PARANGE_0110 670029624fSAntonio Nino Diaz }; 680029624fSAntonio Nino Diaz 690029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void) 700029624fSAntonio Nino Diaz { 710029624fSAntonio Nino Diaz u_register_t pa_range = read_id_aa64mmfr0_el1() & 720029624fSAntonio Nino Diaz ID_AA64MMFR0_EL1_PARANGE_MASK; 730029624fSAntonio Nino Diaz 740029624fSAntonio Nino Diaz /* All other values are reserved */ 750029624fSAntonio Nino Diaz assert(pa_range < ARRAY_SIZE(pa_range_bits_arr)); 760029624fSAntonio Nino Diaz 770029624fSAntonio Nino Diaz return (1ULL << pa_range_bits_arr[pa_range]) - 1ULL; 780029624fSAntonio Nino Diaz } 79aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */ 800029624fSAntonio Nino Diaz 81*e7b9886cSAntonio Nino Diaz unsigned int xlat_arch_current_el(void) 82a5640252SAntonio Nino Diaz { 83*e7b9886cSAntonio Nino Diaz unsigned int el = (unsigned int)GET_EL(read_CurrentEl()); 84a5640252SAntonio Nino Diaz 85*e7b9886cSAntonio Nino Diaz assert(el > 0U); 86a5640252SAntonio Nino Diaz 87a5640252SAntonio Nino Diaz return el; 88a5640252SAntonio Nino Diaz } 89a5640252SAntonio Nino Diaz 90*e7b9886cSAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(unsigned int el) 91a5640252SAntonio Nino Diaz { 92*e7b9886cSAntonio Nino Diaz if (el == 3U) { 93a5640252SAntonio Nino Diaz return UPPER_ATTRS(XN); 94a5640252SAntonio Nino Diaz } else { 95*e7b9886cSAntonio Nino Diaz assert(el == 1U); 96a5640252SAntonio Nino Diaz return UPPER_ATTRS(PXN); 97a5640252SAntonio Nino Diaz } 98a5640252SAntonio Nino Diaz } 99a5640252SAntonio Nino Diaz 1003ca9928dSSoby Mathew void init_xlat_tables(void) 1013ca9928dSSoby Mathew { 1023ca9928dSSoby Mathew unsigned long long max_pa; 1033ca9928dSSoby Mathew uintptr_t max_va; 1043ca9928dSSoby Mathew print_mmap(); 105*e7b9886cSAntonio Nino Diaz init_xlation_table(0U, base_xlation_table, XLAT_TABLE_LEVEL_BASE, 106e8719552SAntonio Nino Diaz &max_va, &max_pa); 1070029624fSAntonio Nino Diaz 108*e7b9886cSAntonio Nino Diaz assert(max_va <= (PLAT_VIRT_ADDR_SPACE_SIZE - 1U)); 109*e7b9886cSAntonio Nino Diaz assert(max_pa <= (PLAT_PHY_ADDR_SPACE_SIZE - 1U)); 110*e7b9886cSAntonio Nino Diaz assert((PLAT_PHY_ADDR_SPACE_SIZE - 1U) <= get_max_supported_pa()); 1110029624fSAntonio Nino Diaz 1123ca9928dSSoby Mathew tcr_ps_bits = calc_physical_addr_size_bits(max_pa); 1133ca9928dSSoby Mathew } 1143ca9928dSSoby Mathew 1153ca9928dSSoby Mathew /******************************************************************************* 1163ca9928dSSoby Mathew * Macro generating the code for the function enabling the MMU in the given 1173ca9928dSSoby Mathew * exception level, assuming that the pagetables have already been created. 1183ca9928dSSoby Mathew * 1193ca9928dSSoby Mathew * _el: Exception level at which the function will run 1203ca9928dSSoby Mathew * _tcr_extra: Extra bits to set in the TCR register. This mask will 1213ca9928dSSoby Mathew * be OR'ed with the default TCR value. 1223ca9928dSSoby Mathew * _tlbi_fct: Function to invalidate the TLBs at the current 1233ca9928dSSoby Mathew * exception level 1243ca9928dSSoby Mathew ******************************************************************************/ 1253ca9928dSSoby Mathew #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \ 1263ca9928dSSoby Mathew void enable_mmu_el##_el(unsigned int flags) \ 1273ca9928dSSoby Mathew { \ 1283ca9928dSSoby Mathew uint64_t mair, tcr, ttbr; \ 1293ca9928dSSoby Mathew uint32_t sctlr; \ 1303ca9928dSSoby Mathew \ 1313ca9928dSSoby Mathew assert(IS_IN_EL(_el)); \ 132*e7b9886cSAntonio Nino Diaz assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0U); \ 1333ca9928dSSoby Mathew \ 1343ca9928dSSoby Mathew /* Set attributes in the right indices of the MAIR */ \ 1353ca9928dSSoby Mathew mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \ 1363ca9928dSSoby Mathew mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \ 1373ca9928dSSoby Mathew ATTR_IWBWA_OWBWA_NTR_INDEX); \ 1383ca9928dSSoby Mathew mair |= MAIR_ATTR_SET(ATTR_NON_CACHEABLE, \ 1393ca9928dSSoby Mathew ATTR_NON_CACHEABLE_INDEX); \ 1403ca9928dSSoby Mathew write_mair_el##_el(mair); \ 1413ca9928dSSoby Mathew \ 1423ca9928dSSoby Mathew /* Invalidate TLBs at the current exception level */ \ 1433ca9928dSSoby Mathew _tlbi_fct(); \ 1443ca9928dSSoby Mathew \ 1453ca9928dSSoby Mathew /* Set TCR bits as well. */ \ 146e8719552SAntonio Nino Diaz /* Set T0SZ to (64 - width of virtual address space) */ \ 147*e7b9886cSAntonio Nino Diaz int t0sz = 64 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE);\ 148*e7b9886cSAntonio Nino Diaz \ 149*e7b9886cSAntonio Nino Diaz if ((flags & XLAT_TABLE_NC) != 0U) { \ 1505d21b037SSummer Qin /* Inner & outer non-cacheable non-shareable. */\ 1515d21b037SSummer Qin tcr = TCR_SH_NON_SHAREABLE | \ 1525d21b037SSummer Qin TCR_RGN_OUTER_NC | TCR_RGN_INNER_NC | \ 153*e7b9886cSAntonio Nino Diaz (uint64_t) t0sz; \ 1545d21b037SSummer Qin } else { \ 1555d21b037SSummer Qin /* Inner & outer WBWA & shareable. */ \ 1565d21b037SSummer Qin tcr = TCR_SH_INNER_SHAREABLE | \ 1575d21b037SSummer Qin TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA | \ 158*e7b9886cSAntonio Nino Diaz (uint64_t) t0sz; \ 1595d21b037SSummer Qin } \ 1603ca9928dSSoby Mathew tcr |= _tcr_extra; \ 1613ca9928dSSoby Mathew write_tcr_el##_el(tcr); \ 1623ca9928dSSoby Mathew \ 1633ca9928dSSoby Mathew /* Set TTBR bits as well */ \ 164e8719552SAntonio Nino Diaz ttbr = (uint64_t) base_xlation_table; \ 1653ca9928dSSoby Mathew write_ttbr0_el##_el(ttbr); \ 1663ca9928dSSoby Mathew \ 1673ca9928dSSoby Mathew /* Ensure all translation table writes have drained */ \ 1683ca9928dSSoby Mathew /* into memory, the TLB invalidation is complete, */ \ 1693ca9928dSSoby Mathew /* and translation register writes are committed */ \ 1703ca9928dSSoby Mathew /* before enabling the MMU */ \ 171ccbec91cSAntonio Nino Diaz dsbish(); \ 1723ca9928dSSoby Mathew isb(); \ 1733ca9928dSSoby Mathew \ 1743ca9928dSSoby Mathew sctlr = read_sctlr_el##_el(); \ 1753ca9928dSSoby Mathew sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \ 1763ca9928dSSoby Mathew \ 177*e7b9886cSAntonio Nino Diaz if ((flags & DISABLE_DCACHE) != 0U) \ 1783ca9928dSSoby Mathew sctlr &= ~SCTLR_C_BIT; \ 1793ca9928dSSoby Mathew else \ 1803ca9928dSSoby Mathew sctlr |= SCTLR_C_BIT; \ 1813ca9928dSSoby Mathew \ 1823ca9928dSSoby Mathew write_sctlr_el##_el(sctlr); \ 1833ca9928dSSoby Mathew \ 1843ca9928dSSoby Mathew /* Ensure the MMU enable takes effect immediately */ \ 1853ca9928dSSoby Mathew isb(); \ 18692bec97fSJeenu Viswambharan } \ 18792bec97fSJeenu Viswambharan \ 18892bec97fSJeenu Viswambharan void enable_mmu_direct_el##_el(unsigned int flags) \ 18992bec97fSJeenu Viswambharan { \ 19092bec97fSJeenu Viswambharan enable_mmu_el##_el(flags); \ 1913ca9928dSSoby Mathew } 1923ca9928dSSoby Mathew 1933ca9928dSSoby Mathew /* Define EL1 and EL3 variants of the function enabling the MMU */ 1943ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(1, 1953388b38dSAntonio Nino Diaz /* 1963388b38dSAntonio Nino Diaz * TCR_EL1.EPD1: Disable translation table walk for addresses 1973388b38dSAntonio Nino Diaz * that are translated using TTBR1_EL1. 1983388b38dSAntonio Nino Diaz */ 1993388b38dSAntonio Nino Diaz TCR_EPD1_BIT | (tcr_ps_bits << TCR_EL1_IPS_SHIFT), 2003ca9928dSSoby Mathew tlbivmalle1) 2013ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(3, 2023ca9928dSSoby Mathew TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT), 2033ca9928dSSoby Mathew tlbialle3) 204