13ca9928dSSoby Mathew /*
2aaaf2cc3SSona Mathew * Copyright (c) 2014-2024, Arm Limited and Contributors. All rights reserved.
33ca9928dSSoby Mathew *
482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause
53ca9928dSSoby Mathew */
63ca9928dSSoby Mathew
709d40e0eSAntonio Nino Diaz #include <assert.h>
809d40e0eSAntonio Nino Diaz #include <stdint.h>
909d40e0eSAntonio Nino Diaz
1009d40e0eSAntonio Nino Diaz #include <platform_def.h>
1109d40e0eSAntonio Nino Diaz
123ca9928dSSoby Mathew #include <arch.h>
13cedfa04bSSathees Balya #include <arch_features.h>
1409d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1509d40e0eSAntonio Nino Diaz #include <lib/utils.h>
1609d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables.h>
1709d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_arch.h>
1809d40e0eSAntonio Nino Diaz #include <plat/common/common_def.h>
1909d40e0eSAntonio Nino Diaz
203ca9928dSSoby Mathew #include "../xlat_tables_private.h"
213ca9928dSSoby Mathew
228933c34bSSandrine Bailleux #define XLAT_TABLE_LEVEL_BASE \
238933c34bSSandrine Bailleux GET_XLAT_TABLE_LEVEL_BASE(PLAT_VIRT_ADDR_SPACE_SIZE)
243ca9928dSSoby Mathew
250029624fSAntonio Nino Diaz #define NUM_BASE_LEVEL_ENTRIES \
268933c34bSSandrine Bailleux GET_NUM_BASE_LEVEL_ENTRIES(PLAT_VIRT_ADDR_SPACE_SIZE)
27e8719552SAntonio Nino Diaz
28e8719552SAntonio Nino Diaz static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES]
29e8719552SAntonio Nino Diaz __aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t));
303ca9928dSSoby Mathew
313ca9928dSSoby Mathew static unsigned long long tcr_ps_bits;
323ca9928dSSoby Mathew
calc_physical_addr_size_bits(unsigned long long max_addr)333ca9928dSSoby Mathew static unsigned long long calc_physical_addr_size_bits(
343ca9928dSSoby Mathew unsigned long long max_addr)
353ca9928dSSoby Mathew {
363ca9928dSSoby Mathew /* Physical address can't exceed 48 bits */
37e7b9886cSAntonio Nino Diaz assert((max_addr & ADDR_MASK_48_TO_63) == 0U);
383ca9928dSSoby Mathew
393ca9928dSSoby Mathew /* 48 bits address */
40e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_44_TO_47) != 0U)
413ca9928dSSoby Mathew return TCR_PS_BITS_256TB;
423ca9928dSSoby Mathew
433ca9928dSSoby Mathew /* 44 bits address */
44e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_42_TO_43) != 0U)
453ca9928dSSoby Mathew return TCR_PS_BITS_16TB;
463ca9928dSSoby Mathew
473ca9928dSSoby Mathew /* 42 bits address */
48e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_40_TO_41) != 0U)
493ca9928dSSoby Mathew return TCR_PS_BITS_4TB;
503ca9928dSSoby Mathew
513ca9928dSSoby Mathew /* 40 bits address */
52e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_36_TO_39) != 0U)
533ca9928dSSoby Mathew return TCR_PS_BITS_1TB;
543ca9928dSSoby Mathew
553ca9928dSSoby Mathew /* 36 bits address */
56e7b9886cSAntonio Nino Diaz if ((max_addr & ADDR_MASK_32_TO_35) != 0U)
573ca9928dSSoby Mathew return TCR_PS_BITS_64GB;
583ca9928dSSoby Mathew
593ca9928dSSoby Mathew return TCR_PS_BITS_4GB;
603ca9928dSSoby Mathew }
613ca9928dSSoby Mathew
62aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
63d3c4487cSAntonio Nino Diaz /*
64d3c4487cSAntonio Nino Diaz * Physical Address ranges supported in the AArch64 Memory Model. Value 0b110 is
65d3c4487cSAntonio Nino Diaz * supported in ARMv8.2 onwards.
66d3c4487cSAntonio Nino Diaz */
670029624fSAntonio Nino Diaz static const unsigned int pa_range_bits_arr[] = {
680029624fSAntonio Nino Diaz PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011, PARANGE_0100,
69*30655136SGovindraj Raja PARANGE_0101, PARANGE_0110, PARANGE_0111
700029624fSAntonio Nino Diaz };
710029624fSAntonio Nino Diaz
get_max_supported_pa(void)720029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void)
730029624fSAntonio Nino Diaz {
740029624fSAntonio Nino Diaz u_register_t pa_range = read_id_aa64mmfr0_el1() &
750029624fSAntonio Nino Diaz ID_AA64MMFR0_EL1_PARANGE_MASK;
760029624fSAntonio Nino Diaz
770029624fSAntonio Nino Diaz /* All other values are reserved */
780029624fSAntonio Nino Diaz assert(pa_range < ARRAY_SIZE(pa_range_bits_arr));
790029624fSAntonio Nino Diaz
800029624fSAntonio Nino Diaz return (1ULL << pa_range_bits_arr[pa_range]) - 1ULL;
810029624fSAntonio Nino Diaz }
82cedfa04bSSathees Balya
83cedfa04bSSathees Balya /*
84cedfa04bSSathees Balya * Return minimum virtual address space size supported by the architecture
85cedfa04bSSathees Balya */
xlat_get_min_virt_addr_space_size(void)86cedfa04bSSathees Balya static uintptr_t xlat_get_min_virt_addr_space_size(void)
87cedfa04bSSathees Balya {
88cedfa04bSSathees Balya uintptr_t ret;
89cedfa04bSSathees Balya
90aaaf2cc3SSona Mathew if (is_feat_ttst_present())
91cedfa04bSSathees Balya ret = MIN_VIRT_ADDR_SPACE_SIZE_TTST;
92cedfa04bSSathees Balya else
93cedfa04bSSathees Balya ret = MIN_VIRT_ADDR_SPACE_SIZE;
94cedfa04bSSathees Balya
95cedfa04bSSathees Balya return ret;
96cedfa04bSSathees Balya }
97aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
980029624fSAntonio Nino Diaz
xlat_arch_current_el(void)99e7b9886cSAntonio Nino Diaz unsigned int xlat_arch_current_el(void)
100a5640252SAntonio Nino Diaz {
101e7b9886cSAntonio Nino Diaz unsigned int el = (unsigned int)GET_EL(read_CurrentEl());
102a5640252SAntonio Nino Diaz
103e7b9886cSAntonio Nino Diaz assert(el > 0U);
104a5640252SAntonio Nino Diaz
105a5640252SAntonio Nino Diaz return el;
106a5640252SAntonio Nino Diaz }
107a5640252SAntonio Nino Diaz
xlat_arch_get_xn_desc(unsigned int el)108e7b9886cSAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(unsigned int el)
109a5640252SAntonio Nino Diaz {
110e7b9886cSAntonio Nino Diaz if (el == 3U) {
111a5640252SAntonio Nino Diaz return UPPER_ATTRS(XN);
112a5640252SAntonio Nino Diaz } else {
113e7b9886cSAntonio Nino Diaz assert(el == 1U);
114a5640252SAntonio Nino Diaz return UPPER_ATTRS(PXN);
115a5640252SAntonio Nino Diaz }
116a5640252SAntonio Nino Diaz }
117a5640252SAntonio Nino Diaz
init_xlat_tables(void)1183ca9928dSSoby Mathew void init_xlat_tables(void)
1193ca9928dSSoby Mathew {
1203ca9928dSSoby Mathew unsigned long long max_pa;
1213ca9928dSSoby Mathew uintptr_t max_va;
122cedfa04bSSathees Balya
123cedfa04bSSathees Balya assert(PLAT_VIRT_ADDR_SPACE_SIZE >=
124cedfa04bSSathees Balya (xlat_get_min_virt_addr_space_size() - 1U));
125cedfa04bSSathees Balya assert(PLAT_VIRT_ADDR_SPACE_SIZE <= MAX_VIRT_ADDR_SPACE_SIZE);
126cedfa04bSSathees Balya assert(IS_POWER_OF_TWO(PLAT_VIRT_ADDR_SPACE_SIZE));
127cedfa04bSSathees Balya
1283ca9928dSSoby Mathew print_mmap();
129e7b9886cSAntonio Nino Diaz init_xlation_table(0U, base_xlation_table, XLAT_TABLE_LEVEL_BASE,
130e8719552SAntonio Nino Diaz &max_va, &max_pa);
1310029624fSAntonio Nino Diaz
132e7b9886cSAntonio Nino Diaz assert(max_va <= (PLAT_VIRT_ADDR_SPACE_SIZE - 1U));
133e7b9886cSAntonio Nino Diaz assert(max_pa <= (PLAT_PHY_ADDR_SPACE_SIZE - 1U));
134e7b9886cSAntonio Nino Diaz assert((PLAT_PHY_ADDR_SPACE_SIZE - 1U) <= get_max_supported_pa());
1350029624fSAntonio Nino Diaz
1363ca9928dSSoby Mathew tcr_ps_bits = calc_physical_addr_size_bits(max_pa);
1373ca9928dSSoby Mathew }
1383ca9928dSSoby Mathew
1393ca9928dSSoby Mathew /*******************************************************************************
1403ca9928dSSoby Mathew * Macro generating the code for the function enabling the MMU in the given
1413ca9928dSSoby Mathew * exception level, assuming that the pagetables have already been created.
1423ca9928dSSoby Mathew *
1433ca9928dSSoby Mathew * _el: Exception level at which the function will run
1443ca9928dSSoby Mathew * _tcr_extra: Extra bits to set in the TCR register. This mask will
1453ca9928dSSoby Mathew * be OR'ed with the default TCR value.
1463ca9928dSSoby Mathew * _tlbi_fct: Function to invalidate the TLBs at the current
1473ca9928dSSoby Mathew * exception level
1483ca9928dSSoby Mathew ******************************************************************************/
1493ca9928dSSoby Mathew #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \
1503ca9928dSSoby Mathew void enable_mmu_el##_el(unsigned int flags) \
1513ca9928dSSoby Mathew { \
1523ca9928dSSoby Mathew uint64_t mair, tcr, ttbr; \
1533ca9928dSSoby Mathew uint32_t sctlr; \
1543ca9928dSSoby Mathew \
1553ca9928dSSoby Mathew assert(IS_IN_EL(_el)); \
156e7b9886cSAntonio Nino Diaz assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0U); \
1573ca9928dSSoby Mathew \
1583ca9928dSSoby Mathew /* Set attributes in the right indices of the MAIR */ \
1593ca9928dSSoby Mathew mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \
1603ca9928dSSoby Mathew mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \
1613ca9928dSSoby Mathew ATTR_IWBWA_OWBWA_NTR_INDEX); \
1623ca9928dSSoby Mathew mair |= MAIR_ATTR_SET(ATTR_NON_CACHEABLE, \
1633ca9928dSSoby Mathew ATTR_NON_CACHEABLE_INDEX); \
1643ca9928dSSoby Mathew write_mair_el##_el(mair); \
1653ca9928dSSoby Mathew \
1663ca9928dSSoby Mathew /* Invalidate TLBs at the current exception level */ \
1673ca9928dSSoby Mathew _tlbi_fct(); \
1683ca9928dSSoby Mathew \
1693ca9928dSSoby Mathew /* Set TCR bits as well. */ \
170e8719552SAntonio Nino Diaz /* Set T0SZ to (64 - width of virtual address space) */ \
171e7b9886cSAntonio Nino Diaz int t0sz = 64 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE);\
172e7b9886cSAntonio Nino Diaz \
173e7b9886cSAntonio Nino Diaz if ((flags & XLAT_TABLE_NC) != 0U) { \
1745d21b037SSummer Qin /* Inner & outer non-cacheable non-shareable. */\
1755d21b037SSummer Qin tcr = TCR_SH_NON_SHAREABLE | \
1765d21b037SSummer Qin TCR_RGN_OUTER_NC | TCR_RGN_INNER_NC | \
1776de6965bSAntonio Nino Diaz ((uint64_t)t0sz << TCR_T0SZ_SHIFT); \
1785d21b037SSummer Qin } else { \
1795d21b037SSummer Qin /* Inner & outer WBWA & shareable. */ \
1805d21b037SSummer Qin tcr = TCR_SH_INNER_SHAREABLE | \
1815d21b037SSummer Qin TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA | \
1826de6965bSAntonio Nino Diaz ((uint64_t)t0sz << TCR_T0SZ_SHIFT); \
1835d21b037SSummer Qin } \
1843ca9928dSSoby Mathew tcr |= _tcr_extra; \
1853ca9928dSSoby Mathew write_tcr_el##_el(tcr); \
1863ca9928dSSoby Mathew \
1873ca9928dSSoby Mathew /* Set TTBR bits as well */ \
188e8719552SAntonio Nino Diaz ttbr = (uint64_t) base_xlation_table; \
1893ca9928dSSoby Mathew write_ttbr0_el##_el(ttbr); \
1903ca9928dSSoby Mathew \
1913ca9928dSSoby Mathew /* Ensure all translation table writes have drained */ \
1923ca9928dSSoby Mathew /* into memory, the TLB invalidation is complete, */ \
1933ca9928dSSoby Mathew /* and translation register writes are committed */ \
1943ca9928dSSoby Mathew /* before enabling the MMU */ \
195ccbec91cSAntonio Nino Diaz dsbish(); \
1963ca9928dSSoby Mathew isb(); \
1973ca9928dSSoby Mathew \
1983ca9928dSSoby Mathew sctlr = read_sctlr_el##_el(); \
1993ca9928dSSoby Mathew sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \
2003ca9928dSSoby Mathew \
201e7b9886cSAntonio Nino Diaz if ((flags & DISABLE_DCACHE) != 0U) \
2023ca9928dSSoby Mathew sctlr &= ~SCTLR_C_BIT; \
2033ca9928dSSoby Mathew else \
2043ca9928dSSoby Mathew sctlr |= SCTLR_C_BIT; \
2053ca9928dSSoby Mathew \
2063ca9928dSSoby Mathew write_sctlr_el##_el(sctlr); \
2073ca9928dSSoby Mathew \
2083ca9928dSSoby Mathew /* Ensure the MMU enable takes effect immediately */ \
2093ca9928dSSoby Mathew isb(); \
21092bec97fSJeenu Viswambharan } \
21192bec97fSJeenu Viswambharan \
21292bec97fSJeenu Viswambharan void enable_mmu_direct_el##_el(unsigned int flags) \
21392bec97fSJeenu Viswambharan { \
21492bec97fSJeenu Viswambharan enable_mmu_el##_el(flags); \
2153ca9928dSSoby Mathew }
2163ca9928dSSoby Mathew
2173ca9928dSSoby Mathew /* Define EL1 and EL3 variants of the function enabling the MMU */
2183ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(1,
2193388b38dSAntonio Nino Diaz /*
2203388b38dSAntonio Nino Diaz * TCR_EL1.EPD1: Disable translation table walk for addresses
2213388b38dSAntonio Nino Diaz * that are translated using TTBR1_EL1.
2223388b38dSAntonio Nino Diaz */
2233388b38dSAntonio Nino Diaz TCR_EPD1_BIT | (tcr_ps_bits << TCR_EL1_IPS_SHIFT),
2243ca9928dSSoby Mathew tlbivmalle1)
2253ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(3,
2263ca9928dSSoby Mathew TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),
2273ca9928dSSoby Mathew tlbialle3)
228