xref: /rk3399_ARM-atf/lib/xlat_tables/aarch32/xlat_tables.c (revision 82cb2c1ad9897473743f08437d0a3995bed561b9)
1b2bca61dSSoby Mathew /*
2aa61368eSAntonio Nino Diaz  * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3b2bca61dSSoby Mathew  *
4*82cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
5b2bca61dSSoby Mathew  */
6b2bca61dSSoby Mathew 
7b2bca61dSSoby Mathew #include <arch.h>
8b2bca61dSSoby Mathew #include <arch_helpers.h>
9b2bca61dSSoby Mathew #include <assert.h>
10b2bca61dSSoby Mathew #include <cassert.h>
11b2bca61dSSoby Mathew #include <platform_def.h>
12b2bca61dSSoby Mathew #include <utils.h>
13b2bca61dSSoby Mathew #include <xlat_tables.h>
14b2bca61dSSoby Mathew #include "../xlat_tables_private.h"
15b2bca61dSSoby Mathew 
16b2bca61dSSoby Mathew /*
17e8719552SAntonio Nino Diaz  * Each platform can define the size of the virtual address space, which is
180029624fSAntonio Nino Diaz  * defined in PLAT_VIRT_ADDR_SPACE_SIZE. TTBCR.TxSZ is calculated as 32 minus
190029624fSAntonio Nino Diaz  * the width of said address space. The value of TTBCR.TxSZ must be in the
200029624fSAntonio Nino Diaz  * range 0 to 7 [1], which means that the virtual address space width must be
210029624fSAntonio Nino Diaz  * in the range 32 to 25 bits.
22e8719552SAntonio Nino Diaz  *
230029624fSAntonio Nino Diaz  * Here we calculate the initial lookup level from the value of
240029624fSAntonio Nino Diaz  * PLAT_VIRT_ADDR_SPACE_SIZE. For a 4 KB page size, level 1 supports virtual
250029624fSAntonio Nino Diaz  * address spaces of widths 32 to 31 bits, and level 2 from 30 to 25. Wider or
260029624fSAntonio Nino Diaz  * narrower address spaces are not supported. As a result, level 3 cannot be
270029624fSAntonio Nino Diaz  * used as initial lookup level with 4 KB granularity [1].
28e8719552SAntonio Nino Diaz  *
290029624fSAntonio Nino Diaz  * For example, for a 31-bit address space (i.e. PLAT_VIRT_ADDR_SPACE_SIZE ==
300029624fSAntonio Nino Diaz  * 1 << 31), TTBCR.TxSZ will be programmed to (32 - 31) = 1. According to Table
310029624fSAntonio Nino Diaz  * G4-5 in the ARM ARM, the initial lookup level for an address space like that
320029624fSAntonio Nino Diaz  * is 1.
33e8719552SAntonio Nino Diaz  *
34e8719552SAntonio Nino Diaz  * See the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more
35e8719552SAntonio Nino Diaz  * information:
36e8719552SAntonio Nino Diaz  * [1] Section G4.6.5
37b2bca61dSSoby Mathew  */
38b2bca61dSSoby Mathew 
390029624fSAntonio Nino Diaz #if PLAT_VIRT_ADDR_SPACE_SIZE > (1ULL << (32 - TTBCR_TxSZ_MIN))
40b2bca61dSSoby Mathew 
410029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too big."
42e8719552SAntonio Nino Diaz 
430029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE > (1 << L1_XLAT_ADDRESS_SHIFT)
44e8719552SAntonio Nino Diaz 
45e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE	1
460029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES	\
470029624fSAntonio Nino Diaz 		(PLAT_VIRT_ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT)
48e8719552SAntonio Nino Diaz 
490029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE >= (1 << (32 - TTBCR_TxSZ_MAX))
50e8719552SAntonio Nino Diaz 
51e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE	2
520029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES	\
530029624fSAntonio Nino Diaz 		(PLAT_VIRT_ADDR_SPACE_SIZE >> L2_XLAT_ADDRESS_SHIFT)
54e8719552SAntonio Nino Diaz 
55e8719552SAntonio Nino Diaz #else
56e8719552SAntonio Nino Diaz 
570029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too small."
58e8719552SAntonio Nino Diaz 
59e8719552SAntonio Nino Diaz #endif
60e8719552SAntonio Nino Diaz 
61e8719552SAntonio Nino Diaz static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES]
62e8719552SAntonio Nino Diaz 		__aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t));
63b2bca61dSSoby Mathew 
64aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
650029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void)
660029624fSAntonio Nino Diaz {
670029624fSAntonio Nino Diaz 	/* Physical address space size for long descriptor format. */
680029624fSAntonio Nino Diaz 	return (1ULL << 40) - 1ULL;
690029624fSAntonio Nino Diaz }
70aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
710029624fSAntonio Nino Diaz 
72b2bca61dSSoby Mathew void init_xlat_tables(void)
73b2bca61dSSoby Mathew {
74b2bca61dSSoby Mathew 	unsigned long long max_pa;
75b2bca61dSSoby Mathew 	uintptr_t max_va;
76b2bca61dSSoby Mathew 	print_mmap();
77e8719552SAntonio Nino Diaz 	init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE,
78e8719552SAntonio Nino Diaz 						&max_va, &max_pa);
790029624fSAntonio Nino Diaz 
800029624fSAntonio Nino Diaz 	assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1);
810029624fSAntonio Nino Diaz 	assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1);
820029624fSAntonio Nino Diaz 	assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= get_max_supported_pa());
83b2bca61dSSoby Mathew }
84b2bca61dSSoby Mathew 
85b2bca61dSSoby Mathew /*******************************************************************************
86b2bca61dSSoby Mathew  * Function for enabling the MMU in Secure PL1, assuming that the
87b2bca61dSSoby Mathew  * page-tables have already been created.
88b2bca61dSSoby Mathew  ******************************************************************************/
89b2bca61dSSoby Mathew void enable_mmu_secure(unsigned int flags)
90b2bca61dSSoby Mathew {
91b2bca61dSSoby Mathew 	unsigned int mair0, ttbcr, sctlr;
92b2bca61dSSoby Mathew 	uint64_t ttbr0;
93b2bca61dSSoby Mathew 
94b2bca61dSSoby Mathew 	assert(IS_IN_SECURE());
95b2bca61dSSoby Mathew 	assert((read_sctlr() & SCTLR_M_BIT) == 0);
96b2bca61dSSoby Mathew 
97b2bca61dSSoby Mathew 	/* Set attributes in the right indices of the MAIR */
98b2bca61dSSoby Mathew 	mair0 = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);
99b2bca61dSSoby Mathew 	mair0 |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,
100b2bca61dSSoby Mathew 			ATTR_IWBWA_OWBWA_NTR_INDEX);
101b2bca61dSSoby Mathew 	mair0 |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE,
102b2bca61dSSoby Mathew 			ATTR_NON_CACHEABLE_INDEX);
103b2bca61dSSoby Mathew 	write_mair0(mair0);
104b2bca61dSSoby Mathew 
105b2bca61dSSoby Mathew 	/* Invalidate TLBs at the current exception level */
106b2bca61dSSoby Mathew 	tlbiall();
107b2bca61dSSoby Mathew 
108b2bca61dSSoby Mathew 	/*
1095d21b037SSummer Qin 	 * Set TTBCR bits as well. Set TTBR0 table properties. Disable TTBR1.
110b2bca61dSSoby Mathew 	 */
1115d21b037SSummer Qin 	if (flags & XLAT_TABLE_NC) {
1125d21b037SSummer Qin 		/* Inner & outer non-cacheable non-shareable. */
1135d21b037SSummer Qin 		ttbcr = TTBCR_EAE_BIT |
1145d21b037SSummer Qin 			TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC |
1155d21b037SSummer Qin 			TTBCR_RGN0_INNER_NC |
1165d21b037SSummer Qin 			(32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE));
1175d21b037SSummer Qin 	} else {
1185d21b037SSummer Qin 		/* Inner & outer WBWA & shareable. */
119b2bca61dSSoby Mathew 		ttbcr = TTBCR_EAE_BIT |
120b2bca61dSSoby Mathew 			TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA |
121b2bca61dSSoby Mathew 			TTBCR_RGN0_INNER_WBA |
1220029624fSAntonio Nino Diaz 			(32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE));
1235d21b037SSummer Qin 	}
124b2bca61dSSoby Mathew 	ttbcr |= TTBCR_EPD1_BIT;
125b2bca61dSSoby Mathew 	write_ttbcr(ttbcr);
126b2bca61dSSoby Mathew 
127b2bca61dSSoby Mathew 	/* Set TTBR0 bits as well */
128e8719552SAntonio Nino Diaz 	ttbr0 = (uintptr_t) base_xlation_table;
129b2bca61dSSoby Mathew 	write64_ttbr0(ttbr0);
130b2bca61dSSoby Mathew 	write64_ttbr1(0);
131b2bca61dSSoby Mathew 
132b2bca61dSSoby Mathew 	/*
133b2bca61dSSoby Mathew 	 * Ensure all translation table writes have drained
134b2bca61dSSoby Mathew 	 * into memory, the TLB invalidation is complete,
135b2bca61dSSoby Mathew 	 * and translation register writes are committed
136b2bca61dSSoby Mathew 	 * before enabling the MMU
137b2bca61dSSoby Mathew 	 */
138b2bca61dSSoby Mathew 	dsb();
139b2bca61dSSoby Mathew 	isb();
140b2bca61dSSoby Mathew 
141b2bca61dSSoby Mathew 	sctlr = read_sctlr();
142b2bca61dSSoby Mathew 	sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT;
143b2bca61dSSoby Mathew 
144b2bca61dSSoby Mathew 	if (flags & DISABLE_DCACHE)
145b2bca61dSSoby Mathew 		sctlr &= ~SCTLR_C_BIT;
146b2bca61dSSoby Mathew 	else
147b2bca61dSSoby Mathew 		sctlr |= SCTLR_C_BIT;
148b2bca61dSSoby Mathew 
149b2bca61dSSoby Mathew 	write_sctlr(sctlr);
150b2bca61dSSoby Mathew 
151b2bca61dSSoby Mathew 	/* Ensure the MMU enable takes effect immediately */
152b2bca61dSSoby Mathew 	isb();
153b2bca61dSSoby Mathew }
154