xref: /rk3399_ARM-atf/lib/xlat_tables/aarch64/xlat_tables.c (revision 3388b38dc3426d28aeeb2462ee5125ec983e9f3a)
13ca9928dSSoby Mathew /*
25d21b037SSummer Qin  * Copyright (c) 2014-2017, 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
600029624fSAntonio Nino Diaz /* Physical Address ranges supported in the AArch64 Memory Model */
610029624fSAntonio Nino Diaz static const unsigned int pa_range_bits_arr[] = {
620029624fSAntonio Nino Diaz 	PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011, PARANGE_0100,
630029624fSAntonio Nino Diaz 	PARANGE_0101
640029624fSAntonio Nino Diaz };
650029624fSAntonio Nino Diaz 
660029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void)
670029624fSAntonio Nino Diaz {
680029624fSAntonio Nino Diaz 	u_register_t pa_range = read_id_aa64mmfr0_el1() &
690029624fSAntonio Nino Diaz 						ID_AA64MMFR0_EL1_PARANGE_MASK;
700029624fSAntonio Nino Diaz 
710029624fSAntonio Nino Diaz 	/* All other values are reserved */
720029624fSAntonio Nino Diaz 	assert(pa_range < ARRAY_SIZE(pa_range_bits_arr));
730029624fSAntonio Nino Diaz 
740029624fSAntonio Nino Diaz 	return (1ULL << pa_range_bits_arr[pa_range]) - 1ULL;
750029624fSAntonio Nino Diaz }
76aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
770029624fSAntonio Nino Diaz 
78a5640252SAntonio Nino Diaz int xlat_arch_current_el(void)
79a5640252SAntonio Nino Diaz {
80a5640252SAntonio Nino Diaz 	int el = GET_EL(read_CurrentEl());
81a5640252SAntonio Nino Diaz 
82a5640252SAntonio Nino Diaz 	assert(el > 0);
83a5640252SAntonio Nino Diaz 
84a5640252SAntonio Nino Diaz 	return el;
85a5640252SAntonio Nino Diaz }
86a5640252SAntonio Nino Diaz 
87a5640252SAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(int el)
88a5640252SAntonio Nino Diaz {
89a5640252SAntonio Nino Diaz 	if (el == 3) {
90a5640252SAntonio Nino Diaz 		return UPPER_ATTRS(XN);
91a5640252SAntonio Nino Diaz 	} else {
92a5640252SAntonio Nino Diaz 		assert(el == 1);
93a5640252SAntonio Nino Diaz 		return UPPER_ATTRS(PXN);
94a5640252SAntonio Nino Diaz 	}
95a5640252SAntonio Nino Diaz }
96a5640252SAntonio Nino Diaz 
973ca9928dSSoby Mathew void init_xlat_tables(void)
983ca9928dSSoby Mathew {
993ca9928dSSoby Mathew 	unsigned long long max_pa;
1003ca9928dSSoby Mathew 	uintptr_t max_va;
1013ca9928dSSoby Mathew 	print_mmap();
102e8719552SAntonio Nino Diaz 	init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE,
103e8719552SAntonio Nino Diaz 			   &max_va, &max_pa);
1040029624fSAntonio Nino Diaz 
1050029624fSAntonio Nino Diaz 	assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1);
1060029624fSAntonio Nino Diaz 	assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1);
1070029624fSAntonio Nino Diaz 	assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= get_max_supported_pa());
1080029624fSAntonio Nino Diaz 
1093ca9928dSSoby Mathew 	tcr_ps_bits = calc_physical_addr_size_bits(max_pa);
1103ca9928dSSoby Mathew }
1113ca9928dSSoby Mathew 
1123ca9928dSSoby Mathew /*******************************************************************************
1133ca9928dSSoby Mathew  * Macro generating the code for the function enabling the MMU in the given
1143ca9928dSSoby Mathew  * exception level, assuming that the pagetables have already been created.
1153ca9928dSSoby Mathew  *
1163ca9928dSSoby Mathew  *   _el:		Exception level at which the function will run
1173ca9928dSSoby Mathew  *   _tcr_extra:	Extra bits to set in the TCR register. This mask will
1183ca9928dSSoby Mathew  *			be OR'ed with the default TCR value.
1193ca9928dSSoby Mathew  *   _tlbi_fct:		Function to invalidate the TLBs at the current
1203ca9928dSSoby Mathew  *			exception level
1213ca9928dSSoby Mathew  ******************************************************************************/
1223ca9928dSSoby Mathew #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct)		\
1233ca9928dSSoby Mathew 	void enable_mmu_el##_el(unsigned int flags)				\
1243ca9928dSSoby Mathew 	{								\
1253ca9928dSSoby Mathew 		uint64_t mair, tcr, ttbr;				\
1263ca9928dSSoby Mathew 		uint32_t sctlr;						\
1273ca9928dSSoby Mathew 									\
1283ca9928dSSoby Mathew 		assert(IS_IN_EL(_el));					\
1293ca9928dSSoby Mathew 		assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0);	\
1303ca9928dSSoby Mathew 									\
1313ca9928dSSoby Mathew 		/* Set attributes in the right indices of the MAIR */	\
1323ca9928dSSoby Mathew 		mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);	\
1333ca9928dSSoby Mathew 		mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,		\
1343ca9928dSSoby Mathew 				ATTR_IWBWA_OWBWA_NTR_INDEX);		\
1353ca9928dSSoby Mathew 		mair |= MAIR_ATTR_SET(ATTR_NON_CACHEABLE,		\
1363ca9928dSSoby Mathew 				ATTR_NON_CACHEABLE_INDEX);		\
1373ca9928dSSoby Mathew 		write_mair_el##_el(mair);				\
1383ca9928dSSoby Mathew 									\
1393ca9928dSSoby Mathew 		/* Invalidate TLBs at the current exception level */	\
1403ca9928dSSoby Mathew 		_tlbi_fct();						\
1413ca9928dSSoby Mathew 									\
1423ca9928dSSoby Mathew 		/* Set TCR bits as well. */				\
143e8719552SAntonio Nino Diaz 		/* Set T0SZ to (64 - width of virtual address space) */	\
1445d21b037SSummer Qin 		if (flags & XLAT_TABLE_NC) {				\
1455d21b037SSummer Qin 			/* Inner & outer non-cacheable non-shareable. */\
1465d21b037SSummer Qin 			tcr = TCR_SH_NON_SHAREABLE |			\
1475d21b037SSummer Qin 				TCR_RGN_OUTER_NC | TCR_RGN_INNER_NC |	\
1480044231dSSandrine Bailleux 				(64 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE));\
1495d21b037SSummer Qin 		} else {						\
1505d21b037SSummer Qin 			/* Inner & outer WBWA & shareable. */		\
1515d21b037SSummer Qin 			tcr = TCR_SH_INNER_SHAREABLE |			\
1525d21b037SSummer Qin 				TCR_RGN_OUTER_WBA | TCR_RGN_INNER_WBA |	\
1530044231dSSandrine Bailleux 				(64 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE));\
1545d21b037SSummer Qin 		}							\
1553ca9928dSSoby Mathew 		tcr |= _tcr_extra;					\
1563ca9928dSSoby Mathew 		write_tcr_el##_el(tcr);					\
1573ca9928dSSoby Mathew 									\
1583ca9928dSSoby Mathew 		/* Set TTBR bits as well */				\
159e8719552SAntonio Nino Diaz 		ttbr = (uint64_t) base_xlation_table;			\
1603ca9928dSSoby Mathew 		write_ttbr0_el##_el(ttbr);				\
1613ca9928dSSoby Mathew 									\
1623ca9928dSSoby Mathew 		/* Ensure all translation table writes have drained */	\
1633ca9928dSSoby Mathew 		/* into memory, the TLB invalidation is complete, */	\
1643ca9928dSSoby Mathew 		/* and translation register writes are committed */	\
1653ca9928dSSoby Mathew 		/* before enabling the MMU */				\
166ccbec91cSAntonio Nino Diaz 		dsbish();						\
1673ca9928dSSoby Mathew 		isb();							\
1683ca9928dSSoby Mathew 									\
1693ca9928dSSoby Mathew 		sctlr = read_sctlr_el##_el();				\
1703ca9928dSSoby Mathew 		sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT;			\
1713ca9928dSSoby Mathew 									\
1723ca9928dSSoby Mathew 		if (flags & DISABLE_DCACHE)				\
1733ca9928dSSoby Mathew 			sctlr &= ~SCTLR_C_BIT;				\
1743ca9928dSSoby Mathew 		else							\
1753ca9928dSSoby Mathew 			sctlr |= SCTLR_C_BIT;				\
1763ca9928dSSoby Mathew 									\
1773ca9928dSSoby Mathew 		write_sctlr_el##_el(sctlr);				\
1783ca9928dSSoby Mathew 									\
1793ca9928dSSoby Mathew 		/* Ensure the MMU enable takes effect immediately */	\
1803ca9928dSSoby Mathew 		isb();							\
1813ca9928dSSoby Mathew 	}
1823ca9928dSSoby Mathew 
1833ca9928dSSoby Mathew /* Define EL1 and EL3 variants of the function enabling the MMU */
1843ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(1,
185*3388b38dSAntonio Nino Diaz 		/*
186*3388b38dSAntonio Nino Diaz 		 * TCR_EL1.EPD1: Disable translation table walk for addresses
187*3388b38dSAntonio Nino Diaz 		 * that are translated using TTBR1_EL1.
188*3388b38dSAntonio Nino Diaz 		 */
189*3388b38dSAntonio Nino Diaz 		TCR_EPD1_BIT | (tcr_ps_bits << TCR_EL1_IPS_SHIFT),
1903ca9928dSSoby Mathew 		tlbivmalle1)
1913ca9928dSSoby Mathew DEFINE_ENABLE_MMU_EL(3,
1923ca9928dSSoby Mathew 		TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),
1933ca9928dSSoby Mathew 		tlbialle3)
194