xref: /rk3399_ARM-atf/lib/xlat_tables/aarch32/xlat_tables.c (revision aa61368eb554e9910c503f78560153805a2d6859)
1b2bca61dSSoby Mathew /*
2*aa61368eSAntonio Nino Diaz  * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
3b2bca61dSSoby Mathew  *
4b2bca61dSSoby Mathew  * Redistribution and use in source and binary forms, with or without
5b2bca61dSSoby Mathew  * modification, are permitted provided that the following conditions are met:
6b2bca61dSSoby Mathew  *
7b2bca61dSSoby Mathew  * Redistributions of source code must retain the above copyright notice, this
8b2bca61dSSoby Mathew  * list of conditions and the following disclaimer.
9b2bca61dSSoby Mathew  *
10b2bca61dSSoby Mathew  * Redistributions in binary form must reproduce the above copyright notice,
11b2bca61dSSoby Mathew  * this list of conditions and the following disclaimer in the documentation
12b2bca61dSSoby Mathew  * and/or other materials provided with the distribution.
13b2bca61dSSoby Mathew  *
14b2bca61dSSoby Mathew  * Neither the name of ARM nor the names of its contributors may be used
15b2bca61dSSoby Mathew  * to endorse or promote products derived from this software without specific
16b2bca61dSSoby Mathew  * prior written permission.
17b2bca61dSSoby Mathew  *
18b2bca61dSSoby Mathew  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19b2bca61dSSoby Mathew  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20b2bca61dSSoby Mathew  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21b2bca61dSSoby Mathew  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22b2bca61dSSoby Mathew  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23b2bca61dSSoby Mathew  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24b2bca61dSSoby Mathew  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25b2bca61dSSoby Mathew  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26b2bca61dSSoby Mathew  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27b2bca61dSSoby Mathew  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28b2bca61dSSoby Mathew  * POSSIBILITY OF SUCH DAMAGE.
29b2bca61dSSoby Mathew  */
30b2bca61dSSoby Mathew 
31b2bca61dSSoby Mathew #include <arch.h>
32b2bca61dSSoby Mathew #include <arch_helpers.h>
33b2bca61dSSoby Mathew #include <assert.h>
34b2bca61dSSoby Mathew #include <cassert.h>
35b2bca61dSSoby Mathew #include <platform_def.h>
36b2bca61dSSoby Mathew #include <utils.h>
37b2bca61dSSoby Mathew #include <xlat_tables.h>
38b2bca61dSSoby Mathew #include "../xlat_tables_private.h"
39b2bca61dSSoby Mathew 
40b2bca61dSSoby Mathew /*
41e8719552SAntonio Nino Diaz  * Each platform can define the size of the virtual address space, which is
420029624fSAntonio Nino Diaz  * defined in PLAT_VIRT_ADDR_SPACE_SIZE. TTBCR.TxSZ is calculated as 32 minus
430029624fSAntonio Nino Diaz  * the width of said address space. The value of TTBCR.TxSZ must be in the
440029624fSAntonio Nino Diaz  * range 0 to 7 [1], which means that the virtual address space width must be
450029624fSAntonio Nino Diaz  * in the range 32 to 25 bits.
46e8719552SAntonio Nino Diaz  *
470029624fSAntonio Nino Diaz  * Here we calculate the initial lookup level from the value of
480029624fSAntonio Nino Diaz  * PLAT_VIRT_ADDR_SPACE_SIZE. For a 4 KB page size, level 1 supports virtual
490029624fSAntonio Nino Diaz  * address spaces of widths 32 to 31 bits, and level 2 from 30 to 25. Wider or
500029624fSAntonio Nino Diaz  * narrower address spaces are not supported. As a result, level 3 cannot be
510029624fSAntonio Nino Diaz  * used as initial lookup level with 4 KB granularity [1].
52e8719552SAntonio Nino Diaz  *
530029624fSAntonio Nino Diaz  * For example, for a 31-bit address space (i.e. PLAT_VIRT_ADDR_SPACE_SIZE ==
540029624fSAntonio Nino Diaz  * 1 << 31), TTBCR.TxSZ will be programmed to (32 - 31) = 1. According to Table
550029624fSAntonio Nino Diaz  * G4-5 in the ARM ARM, the initial lookup level for an address space like that
560029624fSAntonio Nino Diaz  * is 1.
57e8719552SAntonio Nino Diaz  *
58e8719552SAntonio Nino Diaz  * See the ARMv8-A Architecture Reference Manual (DDI 0487A.j) for more
59e8719552SAntonio Nino Diaz  * information:
60e8719552SAntonio Nino Diaz  * [1] Section G4.6.5
61b2bca61dSSoby Mathew  */
62b2bca61dSSoby Mathew 
630029624fSAntonio Nino Diaz #if PLAT_VIRT_ADDR_SPACE_SIZE > (1ULL << (32 - TTBCR_TxSZ_MIN))
64b2bca61dSSoby Mathew 
650029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too big."
66e8719552SAntonio Nino Diaz 
670029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE > (1 << L1_XLAT_ADDRESS_SHIFT)
68e8719552SAntonio Nino Diaz 
69e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE	1
700029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES	\
710029624fSAntonio Nino Diaz 		(PLAT_VIRT_ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT)
72e8719552SAntonio Nino Diaz 
730029624fSAntonio Nino Diaz #elif PLAT_VIRT_ADDR_SPACE_SIZE >= (1 << (32 - TTBCR_TxSZ_MAX))
74e8719552SAntonio Nino Diaz 
75e8719552SAntonio Nino Diaz # define XLAT_TABLE_LEVEL_BASE	2
760029624fSAntonio Nino Diaz # define NUM_BASE_LEVEL_ENTRIES	\
770029624fSAntonio Nino Diaz 		(PLAT_VIRT_ADDR_SPACE_SIZE >> L2_XLAT_ADDRESS_SHIFT)
78e8719552SAntonio Nino Diaz 
79e8719552SAntonio Nino Diaz #else
80e8719552SAntonio Nino Diaz 
810029624fSAntonio Nino Diaz # error "PLAT_VIRT_ADDR_SPACE_SIZE is too small."
82e8719552SAntonio Nino Diaz 
83e8719552SAntonio Nino Diaz #endif
84e8719552SAntonio Nino Diaz 
85e8719552SAntonio Nino Diaz static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES]
86e8719552SAntonio Nino Diaz 		__aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t));
87b2bca61dSSoby Mathew 
88*aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
890029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void)
900029624fSAntonio Nino Diaz {
910029624fSAntonio Nino Diaz 	/* Physical address space size for long descriptor format. */
920029624fSAntonio Nino Diaz 	return (1ULL << 40) - 1ULL;
930029624fSAntonio Nino Diaz }
94*aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
950029624fSAntonio Nino Diaz 
96b2bca61dSSoby Mathew void init_xlat_tables(void)
97b2bca61dSSoby Mathew {
98b2bca61dSSoby Mathew 	unsigned long long max_pa;
99b2bca61dSSoby Mathew 	uintptr_t max_va;
100b2bca61dSSoby Mathew 	print_mmap();
101e8719552SAntonio Nino Diaz 	init_xlation_table(0, base_xlation_table, XLAT_TABLE_LEVEL_BASE,
102e8719552SAntonio Nino Diaz 						&max_va, &max_pa);
1030029624fSAntonio Nino Diaz 
1040029624fSAntonio Nino Diaz 	assert(max_va <= PLAT_VIRT_ADDR_SPACE_SIZE - 1);
1050029624fSAntonio Nino Diaz 	assert(max_pa <= PLAT_PHY_ADDR_SPACE_SIZE - 1);
1060029624fSAntonio Nino Diaz 	assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <= get_max_supported_pa());
107b2bca61dSSoby Mathew }
108b2bca61dSSoby Mathew 
109b2bca61dSSoby Mathew /*******************************************************************************
110b2bca61dSSoby Mathew  * Function for enabling the MMU in Secure PL1, assuming that the
111b2bca61dSSoby Mathew  * page-tables have already been created.
112b2bca61dSSoby Mathew  ******************************************************************************/
113b2bca61dSSoby Mathew void enable_mmu_secure(unsigned int flags)
114b2bca61dSSoby Mathew {
115b2bca61dSSoby Mathew 	unsigned int mair0, ttbcr, sctlr;
116b2bca61dSSoby Mathew 	uint64_t ttbr0;
117b2bca61dSSoby Mathew 
118b2bca61dSSoby Mathew 	assert(IS_IN_SECURE());
119b2bca61dSSoby Mathew 	assert((read_sctlr() & SCTLR_M_BIT) == 0);
120b2bca61dSSoby Mathew 
121b2bca61dSSoby Mathew 	/* Set attributes in the right indices of the MAIR */
122b2bca61dSSoby Mathew 	mair0 = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);
123b2bca61dSSoby Mathew 	mair0 |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,
124b2bca61dSSoby Mathew 			ATTR_IWBWA_OWBWA_NTR_INDEX);
125b2bca61dSSoby Mathew 	mair0 |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE,
126b2bca61dSSoby Mathew 			ATTR_NON_CACHEABLE_INDEX);
127b2bca61dSSoby Mathew 	write_mair0(mair0);
128b2bca61dSSoby Mathew 
129b2bca61dSSoby Mathew 	/* Invalidate TLBs at the current exception level */
130b2bca61dSSoby Mathew 	tlbiall();
131b2bca61dSSoby Mathew 
132b2bca61dSSoby Mathew 	/*
1335d21b037SSummer Qin 	 * Set TTBCR bits as well. Set TTBR0 table properties. Disable TTBR1.
134b2bca61dSSoby Mathew 	 */
1355d21b037SSummer Qin 	if (flags & XLAT_TABLE_NC) {
1365d21b037SSummer Qin 		/* Inner & outer non-cacheable non-shareable. */
1375d21b037SSummer Qin 		ttbcr = TTBCR_EAE_BIT |
1385d21b037SSummer Qin 			TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC |
1395d21b037SSummer Qin 			TTBCR_RGN0_INNER_NC |
1405d21b037SSummer Qin 			(32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE));
1415d21b037SSummer Qin 	} else {
1425d21b037SSummer Qin 		/* Inner & outer WBWA & shareable. */
143b2bca61dSSoby Mathew 		ttbcr = TTBCR_EAE_BIT |
144b2bca61dSSoby Mathew 			TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA |
145b2bca61dSSoby Mathew 			TTBCR_RGN0_INNER_WBA |
1460029624fSAntonio Nino Diaz 			(32 - __builtin_ctzl((uintptr_t)PLAT_VIRT_ADDR_SPACE_SIZE));
1475d21b037SSummer Qin 	}
148b2bca61dSSoby Mathew 	ttbcr |= TTBCR_EPD1_BIT;
149b2bca61dSSoby Mathew 	write_ttbcr(ttbcr);
150b2bca61dSSoby Mathew 
151b2bca61dSSoby Mathew 	/* Set TTBR0 bits as well */
152e8719552SAntonio Nino Diaz 	ttbr0 = (uintptr_t) base_xlation_table;
153b2bca61dSSoby Mathew 	write64_ttbr0(ttbr0);
154b2bca61dSSoby Mathew 	write64_ttbr1(0);
155b2bca61dSSoby Mathew 
156b2bca61dSSoby Mathew 	/*
157b2bca61dSSoby Mathew 	 * Ensure all translation table writes have drained
158b2bca61dSSoby Mathew 	 * into memory, the TLB invalidation is complete,
159b2bca61dSSoby Mathew 	 * and translation register writes are committed
160b2bca61dSSoby Mathew 	 * before enabling the MMU
161b2bca61dSSoby Mathew 	 */
162b2bca61dSSoby Mathew 	dsb();
163b2bca61dSSoby Mathew 	isb();
164b2bca61dSSoby Mathew 
165b2bca61dSSoby Mathew 	sctlr = read_sctlr();
166b2bca61dSSoby Mathew 	sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT;
167b2bca61dSSoby Mathew 
168b2bca61dSSoby Mathew 	if (flags & DISABLE_DCACHE)
169b2bca61dSSoby Mathew 		sctlr &= ~SCTLR_C_BIT;
170b2bca61dSSoby Mathew 	else
171b2bca61dSSoby Mathew 		sctlr |= SCTLR_C_BIT;
172b2bca61dSSoby Mathew 
173b2bca61dSSoby Mathew 	write_sctlr(sctlr);
174b2bca61dSSoby Mathew 
175b2bca61dSSoby Mathew 	/* Ensure the MMU enable takes effect immediately */
176b2bca61dSSoby Mathew 	isb();
177b2bca61dSSoby Mathew }
178