xref: /rk3399_ARM-atf/lib/xlat_tables/aarch64/xlat_tables.c (revision d3c4487cd55a6beb14cb69b08c096e363e03db2a)
13ca9928dSSoby Mathew /*
2*d3c4487cSAntonio 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 */
343ca9928dSSoby Mathew 	assert((max_addr & ADDR_MASK_48_TO_63) == 0);
353ca9928dSSoby Mathew 
363ca9928dSSoby Mathew 	/* 48 bits address */
373ca9928dSSoby Mathew 	if (max_addr & ADDR_MASK_44_TO_47)
383ca9928dSSoby Mathew 		return TCR_PS_BITS_256TB;
393ca9928dSSoby Mathew 
403ca9928dSSoby Mathew 	/* 44 bits address */
413ca9928dSSoby Mathew 	if (max_addr & ADDR_MASK_42_TO_43)
423ca9928dSSoby Mathew 		return TCR_PS_BITS_16TB;
433ca9928dSSoby Mathew 
443ca9928dSSoby Mathew 	/* 42 bits address */
453ca9928dSSoby Mathew 	if (max_addr & ADDR_MASK_40_TO_41)
463ca9928dSSoby Mathew 		return TCR_PS_BITS_4TB;
473ca9928dSSoby Mathew 
483ca9928dSSoby Mathew 	/* 40 bits address */
493ca9928dSSoby Mathew 	if (max_addr & ADDR_MASK_36_TO_39)
503ca9928dSSoby Mathew 		return TCR_PS_BITS_1TB;
513ca9928dSSoby Mathew 
523ca9928dSSoby Mathew 	/* 36 bits address */
533ca9928dSSoby Mathew 	if (max_addr & ADDR_MASK_32_TO_35)
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
60*d3c4487cSAntonio Nino Diaz /*
61*d3c4487cSAntonio Nino Diaz  * Physical Address ranges supported in the AArch64 Memory Model. Value 0b110 is
62*d3c4487cSAntonio Nino Diaz  * supported in ARMv8.2 onwards.
63*d3c4487cSAntonio 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,
66*d3c4487cSAntonio 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 
81a5640252SAntonio Nino Diaz int xlat_arch_current_el(void)
82a5640252SAntonio Nino Diaz {
83a5640252SAntonio Nino Diaz 	int el = GET_EL(read_CurrentEl());
84a5640252SAntonio Nino Diaz 
85a5640252SAntonio Nino Diaz 	assert(el > 0);
86a5640252SAntonio Nino Diaz 
87a5640252SAntonio Nino Diaz 	return el;
88a5640252SAntonio Nino Diaz }
89a5640252SAntonio Nino Diaz 
90a5640252SAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(int el)
91a5640252SAntonio Nino Diaz {
92a5640252SAntonio Nino Diaz 	if (el == 3) {
93a5640252SAntonio Nino Diaz 		return UPPER_ATTRS(XN);
94a5640252SAntonio Nino Diaz 	} else {
95a5640252SAntonio Nino Diaz 		assert(el == 1);
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();
105e8719552SAntonio Nino Diaz 	init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE,
106e8719552SAntonio Nino Diaz 			   &max_va, &max_pa);
1070029624fSAntonio Nino Diaz 
1080029624fSAntonio Nino Diaz 	assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1);
1090029624fSAntonio Nino Diaz 	assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1);
1100029624fSAntonio Nino Diaz 	assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= 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));					\
1323ca9928dSSoby Mathew 		assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0);	\
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) */	\
1475d21b037SSummer Qin 		if (flags & XLAT_TABLE_NC) {				\
1485d21b037SSummer Qin 			/* Inner & outer non-cacheable non-shareable. */\
1495d21b037SSummer Qin 			tcr = TCR_SH_NON_SHAREABLE |			\
1505d21b037SSummer Qin 				TCR_RGN_OUTER_NC | TCR_RGN_INNER_NC |	\
1510044231dSSandrine Bailleux 				(64 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE));\
1525d21b037SSummer Qin 		} else {						\
1535d21b037SSummer Qin 			/* Inner & outer WBWA & shareable. */		\
1545d21b037SSummer Qin 			tcr = TCR_SH_INNER_SHAREABLE |			\
1555d21b037SSummer Qin 				TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA |	\
1560044231dSSandrine Bailleux 				(64 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE));\
1575d21b037SSummer Qin 		}							\
1583ca9928dSSoby Mathew 		tcr |= _tcr_extra;					\
1593ca9928dSSoby Mathew 		write_tcr_el##_el(tcr);					\
1603ca9928dSSoby Mathew 									\
1613ca9928dSSoby Mathew 		/* Set TTBR bits as well */				\
162e8719552SAntonio Nino Diaz 		ttbr = (uint64_t) base_xlation_table;			\
1633ca9928dSSoby Mathew 		write_ttbr0_el##_el(ttbr);				\
1643ca9928dSSoby Mathew 									\
1653ca9928dSSoby Mathew 		/* Ensure all translation table writes have drained */	\
1663ca9928dSSoby Mathew 		/* into memory, the TLB invalidation is complete, */	\
1673ca9928dSSoby Mathew 		/* and translation register writes are committed */	\
1683ca9928dSSoby Mathew 		/* before enabling the MMU */				\
169ccbec91cSAntonio Nino Diaz 		dsbish();						\
1703ca9928dSSoby Mathew 		isb();							\
1713ca9928dSSoby Mathew 									\
1723ca9928dSSoby Mathew 		sctlr = read_sctlr_el##_el();				\
1733ca9928dSSoby Mathew 		sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT;			\
1743ca9928dSSoby Mathew 									\
1753ca9928dSSoby Mathew 		if (flags & DISABLE_DCACHE)				\
1763ca9928dSSoby Mathew 			sctlr &= ~SCTLR_C_BIT;				\
1773ca9928dSSoby Mathew 		else							\
1783ca9928dSSoby Mathew 			sctlr |= SCTLR_C_BIT;				\
1793ca9928dSSoby Mathew 									\
1803ca9928dSSoby Mathew 		write_sctlr_el##_el(sctlr);				\
1813ca9928dSSoby Mathew 									\
1823ca9928dSSoby Mathew 		/* Ensure the MMU enable takes effect immediately */	\
1833ca9928dSSoby Mathew 		isb();							\
1843ca9928dSSoby Mathew 	}
1853ca9928dSSoby Mathew 
1863ca9928dSSoby Mathew /* Define EL1 and EL3 variants of the function enabling the MMU */
1873ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(1,
1883388b38dSAntonio Nino Diaz 		/*
1893388b38dSAntonio Nino Diaz 		 * TCR_EL1.EPD1: Disable translation table walk for addresses
1903388b38dSAntonio Nino Diaz 		 * that are translated using TTBR1_EL1.
1913388b38dSAntonio Nino Diaz 		 */
1923388b38dSAntonio Nino Diaz 		TCR_EPD1_BIT | (tcr_ps_bits << TCR_EL1_IPS_SHIFT),
1933ca9928dSSoby Mathew 		tlbivmalle1)
1943ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(3,
1953ca9928dSSoby Mathew 		TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),
1963ca9928dSSoby Mathew 		tlbialle3)
197