xref: /rk3399_ARM-atf/lib/xlat_tables/aarch64/xlat_tables.c (revision 09d40e0e08283a249e7dce0e106c07c5141f9b7e)
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 
7*09d40e0eSAntonio Nino Diaz #include <assert.h>
8*09d40e0eSAntonio Nino Diaz #include <stdint.h>
9*09d40e0eSAntonio Nino Diaz 
10*09d40e0eSAntonio Nino Diaz #include <platform_def.h>
11*09d40e0eSAntonio Nino Diaz 
123ca9928dSSoby Mathew #include <arch.h>
133ca9928dSSoby Mathew #include <arch_helpers.h>
14*09d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
15*09d40e0eSAntonio Nino Diaz #include <lib/utils.h>
16*09d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables.h>
17*09d40e0eSAntonio Nino Diaz #include <lib/xlat_tables/xlat_tables_arch.h>
18*09d40e0eSAntonio Nino Diaz #include <plat/common/common_def.h>
19*09d40e0eSAntonio 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 
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,
69d3c4487cSAntonio Nino Diaz 	PARANGE_0101, PARANGE_0110
700029624fSAntonio Nino Diaz };
710029624fSAntonio Nino Diaz 
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 }
82aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
830029624fSAntonio Nino Diaz 
84e7b9886cSAntonio Nino Diaz unsigned int xlat_arch_current_el(void)
85a5640252SAntonio Nino Diaz {
86e7b9886cSAntonio Nino Diaz 	unsigned int el = (unsigned int)GET_EL(read_CurrentEl());
87a5640252SAntonio Nino Diaz 
88e7b9886cSAntonio Nino Diaz 	assert(el > 0U);
89a5640252SAntonio Nino Diaz 
90a5640252SAntonio Nino Diaz 	return el;
91a5640252SAntonio Nino Diaz }
92a5640252SAntonio Nino Diaz 
93e7b9886cSAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(unsigned int el)
94a5640252SAntonio Nino Diaz {
95e7b9886cSAntonio Nino Diaz 	if (el == 3U) {
96a5640252SAntonio Nino Diaz 		return UPPER_ATTRS(XN);
97a5640252SAntonio Nino Diaz 	} else {
98e7b9886cSAntonio Nino Diaz 		assert(el == 1U);
99a5640252SAntonio Nino Diaz 		return UPPER_ATTRS(PXN);
100a5640252SAntonio Nino Diaz 	}
101a5640252SAntonio Nino Diaz }
102a5640252SAntonio Nino Diaz 
1033ca9928dSSoby Mathew void init_xlat_tables(void)
1043ca9928dSSoby Mathew {
1053ca9928dSSoby Mathew 	unsigned long long max_pa;
1063ca9928dSSoby Mathew 	uintptr_t max_va;
1073ca9928dSSoby Mathew 	print_mmap();
108e7b9886cSAntonio Nino Diaz 	init_xlation_table(0U, base_xlation_table, XLAT_TABLE_LEVEL_BASE,
109e8719552SAntonio Nino Diaz 			   &max_va, &max_pa);
1100029624fSAntonio Nino Diaz 
111e7b9886cSAntonio Nino Diaz 	assert(max_va <= (PLAT_VIRT_ADDR_SPACE_SIZE - 1U));
112e7b9886cSAntonio Nino Diaz 	assert(max_pa <= (PLAT_PHY_ADDR_SPACE_SIZE - 1U));
113e7b9886cSAntonio Nino Diaz 	assert((PLAT_PHY_ADDR_SPACE_SIZE - 1U) <= get_max_supported_pa());
1140029624fSAntonio Nino Diaz 
1153ca9928dSSoby Mathew 	tcr_ps_bits = calc_physical_addr_size_bits(max_pa);
1163ca9928dSSoby Mathew }
1173ca9928dSSoby Mathew 
1183ca9928dSSoby Mathew /*******************************************************************************
1193ca9928dSSoby Mathew  * Macro generating the code for the function enabling the MMU in the given
1203ca9928dSSoby Mathew  * exception level, assuming that the pagetables have already been created.
1213ca9928dSSoby Mathew  *
1223ca9928dSSoby Mathew  *   _el:		Exception level at which the function will run
1233ca9928dSSoby Mathew  *   _tcr_extra:	Extra bits to set in the TCR register. This mask will
1243ca9928dSSoby Mathew  *			be OR'ed with the default TCR value.
1253ca9928dSSoby Mathew  *   _tlbi_fct:		Function to invalidate the TLBs at the current
1263ca9928dSSoby Mathew  *			exception level
1273ca9928dSSoby Mathew  ******************************************************************************/
1283ca9928dSSoby Mathew #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct)		\
1293ca9928dSSoby Mathew 	void enable_mmu_el##_el(unsigned int flags)				\
1303ca9928dSSoby Mathew 	{								\
1313ca9928dSSoby Mathew 		uint64_t mair, tcr, ttbr;				\
1323ca9928dSSoby Mathew 		uint32_t sctlr;						\
1333ca9928dSSoby Mathew 									\
1343ca9928dSSoby Mathew 		assert(IS_IN_EL(_el));					\
135e7b9886cSAntonio Nino Diaz 		assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0U);	\
1363ca9928dSSoby Mathew 									\
1373ca9928dSSoby Mathew 		/* Set attributes in the right indices of the MAIR */	\
1383ca9928dSSoby Mathew 		mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);	\
1393ca9928dSSoby Mathew 		mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,		\
1403ca9928dSSoby Mathew 				ATTR_IWBWA_OWBWA_NTR_INDEX);		\
1413ca9928dSSoby Mathew 		mair |= MAIR_ATTR_SET(ATTR_NON_CACHEABLE,		\
1423ca9928dSSoby Mathew 				ATTR_NON_CACHEABLE_INDEX);		\
1433ca9928dSSoby Mathew 		write_mair_el##_el(mair);				\
1443ca9928dSSoby Mathew 									\
1453ca9928dSSoby Mathew 		/* Invalidate TLBs at the current exception level */	\
1463ca9928dSSoby Mathew 		_tlbi_fct();						\
1473ca9928dSSoby Mathew 									\
1483ca9928dSSoby Mathew 		/* Set TCR bits as well. */				\
149e8719552SAntonio Nino Diaz 		/* Set T0SZ to (64 - width of virtual address space) */	\
150e7b9886cSAntonio Nino Diaz 		int t0sz = 64 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE);\
151e7b9886cSAntonio Nino Diaz 									\
152e7b9886cSAntonio Nino Diaz 		if ((flags & XLAT_TABLE_NC) != 0U) {			\
1535d21b037SSummer Qin 			/* Inner & outer non-cacheable non-shareable. */\
1545d21b037SSummer Qin 			tcr = TCR_SH_NON_SHAREABLE |			\
1555d21b037SSummer Qin 				TCR_RGN_OUTER_NC | TCR_RGN_INNER_NC |	\
156e7b9886cSAntonio Nino Diaz 				(uint64_t) t0sz;			\
1575d21b037SSummer Qin 		} else {						\
1585d21b037SSummer Qin 			/* Inner & outer WBWA & shareable. */		\
1595d21b037SSummer Qin 			tcr = TCR_SH_INNER_SHAREABLE |			\
1605d21b037SSummer Qin 				TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA |	\
161e7b9886cSAntonio Nino Diaz 				(uint64_t) t0sz;			\
1625d21b037SSummer Qin 		}							\
1633ca9928dSSoby Mathew 		tcr |= _tcr_extra;					\
1643ca9928dSSoby Mathew 		write_tcr_el##_el(tcr);					\
1653ca9928dSSoby Mathew 									\
1663ca9928dSSoby Mathew 		/* Set TTBR bits as well */				\
167e8719552SAntonio Nino Diaz 		ttbr = (uint64_t) base_xlation_table;			\
1683ca9928dSSoby Mathew 		write_ttbr0_el##_el(ttbr);				\
1693ca9928dSSoby Mathew 									\
1703ca9928dSSoby Mathew 		/* Ensure all translation table writes have drained */	\
1713ca9928dSSoby Mathew 		/* into memory, the TLB invalidation is complete, */	\
1723ca9928dSSoby Mathew 		/* and translation register writes are committed */	\
1733ca9928dSSoby Mathew 		/* before enabling the MMU */				\
174ccbec91cSAntonio Nino Diaz 		dsbish();						\
1753ca9928dSSoby Mathew 		isb();							\
1763ca9928dSSoby Mathew 									\
1773ca9928dSSoby Mathew 		sctlr = read_sctlr_el##_el();				\
1783ca9928dSSoby Mathew 		sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT;			\
1793ca9928dSSoby Mathew 									\
180e7b9886cSAntonio Nino Diaz 		if ((flags & DISABLE_DCACHE) != 0U)			\
1813ca9928dSSoby Mathew 			sctlr &= ~SCTLR_C_BIT;				\
1823ca9928dSSoby Mathew 		else							\
1833ca9928dSSoby Mathew 			sctlr |= SCTLR_C_BIT;				\
1843ca9928dSSoby Mathew 									\
1853ca9928dSSoby Mathew 		write_sctlr_el##_el(sctlr);				\
1863ca9928dSSoby Mathew 									\
1873ca9928dSSoby Mathew 		/* Ensure the MMU enable takes effect immediately */	\
1883ca9928dSSoby Mathew 		isb();							\
18992bec97fSJeenu Viswambharan 	}								\
19092bec97fSJeenu Viswambharan 									\
19192bec97fSJeenu Viswambharan 	void enable_mmu_direct_el##_el(unsigned int flags)		\
19292bec97fSJeenu Viswambharan 	{								\
19392bec97fSJeenu Viswambharan 		enable_mmu_el##_el(flags);				\
1943ca9928dSSoby Mathew 	}
1953ca9928dSSoby Mathew 
1963ca9928dSSoby Mathew /* Define EL1 and EL3 variants of the function enabling the MMU */
1973ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(1,
1983388b38dSAntonio Nino Diaz 		/*
1993388b38dSAntonio Nino Diaz 		 * TCR_EL1.EPD1: Disable translation table walk for addresses
2003388b38dSAntonio Nino Diaz 		 * that are translated using TTBR1_EL1.
2013388b38dSAntonio Nino Diaz 		 */
2023388b38dSAntonio Nino Diaz 		TCR_EPD1_BIT | (tcr_ps_bits << TCR_EL1_IPS_SHIFT),
2033ca9928dSSoby Mathew 		tlbivmalle1)
2043ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(3,
2053ca9928dSSoby Mathew 		TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),
2063ca9928dSSoby Mathew 		tlbialle3)
207