xref: /rk3399_ARM-atf/lib/xlat_tables/aarch32/xlat_tables.c (revision 1a92a0e00a5093c4b46a55b1eadb48187688caf7)
1b2bca61dSSoby Mathew /*
292bec97fSJeenu Viswambharan  * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
3b2bca61dSSoby Mathew  *
482cb2c1aSdp-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 <platform_def.h>
11b2bca61dSSoby Mathew #include <utils.h>
128933c34bSSandrine Bailleux #include <xlat_tables_arch.h>
13b2bca61dSSoby Mathew #include <xlat_tables.h>
14b2bca61dSSoby Mathew #include "../xlat_tables_private.h"
15b2bca61dSSoby Mathew 
16e7b9886cSAntonio Nino Diaz #if (ARM_ARCH_MAJOR == 7) && !defined(ARMV7_SUPPORTS_LARGE_PAGE_ADDRESSING)
1751b992ecSEtienne Carriere #error ARMv7 target does not support LPAE MMU descriptors
1851b992ecSEtienne Carriere #endif
1951b992ecSEtienne Carriere 
208933c34bSSandrine Bailleux #define XLAT_TABLE_LEVEL_BASE	\
218933c34bSSandrine Bailleux        GET_XLAT_TABLE_LEVEL_BASE(PLAT_VIRT_ADDR_SPACE_SIZE)
22b2bca61dSSoby Mathew 
230029624fSAntonio Nino Diaz #define NUM_BASE_LEVEL_ENTRIES	\
248933c34bSSandrine Bailleux        GET_NUM_BASE_LEVEL_ENTRIES(PLAT_VIRT_ADDR_SPACE_SIZE)
25e8719552SAntonio Nino Diaz 
26e8719552SAntonio Nino Diaz static uint64_t base_xlation_table[NUM_BASE_LEVEL_ENTRIES]
27e8719552SAntonio Nino Diaz 		__aligned(NUM_BASE_LEVEL_ENTRIES * sizeof(uint64_t));
28b2bca61dSSoby Mathew 
29aa61368eSAntonio Nino Diaz #if ENABLE_ASSERTIONS
300029624fSAntonio Nino Diaz static unsigned long long get_max_supported_pa(void)
310029624fSAntonio Nino Diaz {
320029624fSAntonio Nino Diaz 	/* Physical address space size for long descriptor format. */
330029624fSAntonio Nino Diaz 	return (1ULL << 40) - 1ULL;
340029624fSAntonio Nino Diaz }
35aa61368eSAntonio Nino Diaz #endif /* ENABLE_ASSERTIONS */
360029624fSAntonio Nino Diaz 
37e7b9886cSAntonio Nino Diaz unsigned int xlat_arch_current_el(void)
38a5640252SAntonio Nino Diaz {
39a5640252SAntonio Nino Diaz 	/*
40a5640252SAntonio Nino Diaz 	 * If EL3 is in AArch32 mode, all secure PL1 modes (Monitor, System,
41a5640252SAntonio Nino Diaz 	 * SVC, Abort, UND, IRQ and FIQ modes) execute at EL3.
42a5640252SAntonio Nino Diaz 	 */
43e7b9886cSAntonio Nino Diaz 	return 3U;
44a5640252SAntonio Nino Diaz }
45a5640252SAntonio Nino Diaz 
46e7b9886cSAntonio Nino Diaz uint64_t xlat_arch_get_xn_desc(unsigned int el __unused)
47a5640252SAntonio Nino Diaz {
48a5640252SAntonio Nino Diaz 	return UPPER_ATTRS(XN);
49a5640252SAntonio Nino Diaz }
50a5640252SAntonio Nino Diaz 
51b2bca61dSSoby Mathew void init_xlat_tables(void)
52b2bca61dSSoby Mathew {
53b2bca61dSSoby Mathew 	unsigned long long max_pa;
54b2bca61dSSoby Mathew 	uintptr_t max_va;
55b2bca61dSSoby Mathew 	print_mmap();
56e7b9886cSAntonio Nino Diaz 	init_xlation_table(0U, base_xlation_table, XLAT_TABLE_LEVEL_BASE,
57e8719552SAntonio Nino Diaz 						&max_va, &max_pa);
580029624fSAntonio Nino Diaz 
59e7b9886cSAntonio Nino Diaz 	assert(max_va <= (PLAT_VIRT_ADDR_SPACE_SIZE - 1U));
60e7b9886cSAntonio Nino Diaz 	assert(max_pa <= (PLAT_PHY_ADDR_SPACE_SIZE - 1U));
61e7b9886cSAntonio Nino Diaz 	assert((PLAT_PHY_ADDR_SPACE_SIZE - 1U) <= get_max_supported_pa());
62b2bca61dSSoby Mathew }
63b2bca61dSSoby Mathew 
64b2bca61dSSoby Mathew /*******************************************************************************
65b2bca61dSSoby Mathew  * Function for enabling the MMU in Secure PL1, assuming that the
66b2bca61dSSoby Mathew  * page-tables have already been created.
67b2bca61dSSoby Mathew  ******************************************************************************/
68*1a92a0e0SAntonio Nino Diaz #if !ERROR_DEPRECATED
69b2bca61dSSoby Mathew void enable_mmu_secure(unsigned int flags)
70b2bca61dSSoby Mathew {
71*1a92a0e0SAntonio Nino Diaz 	enable_mmu_svc_mon(flags);
72*1a92a0e0SAntonio Nino Diaz }
73*1a92a0e0SAntonio Nino Diaz 
74*1a92a0e0SAntonio Nino Diaz void enable_mmu_direct(unsigned int flags)
75*1a92a0e0SAntonio Nino Diaz {
76*1a92a0e0SAntonio Nino Diaz 	enable_mmu_direct_svc_mon(flags);
77*1a92a0e0SAntonio Nino Diaz }
78*1a92a0e0SAntonio Nino Diaz #endif
79*1a92a0e0SAntonio Nino Diaz 
80*1a92a0e0SAntonio Nino Diaz void enable_mmu_svc_mon(unsigned int flags)
81*1a92a0e0SAntonio Nino Diaz {
82b2bca61dSSoby Mathew 	unsigned int mair0, ttbcr, sctlr;
83b2bca61dSSoby Mathew 	uint64_t ttbr0;
84b2bca61dSSoby Mathew 
85b2bca61dSSoby Mathew 	assert(IS_IN_SECURE());
86e7b9886cSAntonio Nino Diaz 	assert((read_sctlr() & SCTLR_M_BIT) == 0U);
87b2bca61dSSoby Mathew 
88b2bca61dSSoby Mathew 	/* Set attributes in the right indices of the MAIR */
89b2bca61dSSoby Mathew 	mair0 = MAIR0_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);
90b2bca61dSSoby Mathew 	mair0 |= MAIR0_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,
91b2bca61dSSoby Mathew 			ATTR_IWBWA_OWBWA_NTR_INDEX);
92b2bca61dSSoby Mathew 	mair0 |= MAIR0_ATTR_SET(ATTR_NON_CACHEABLE,
93b2bca61dSSoby Mathew 			ATTR_NON_CACHEABLE_INDEX);
94b2bca61dSSoby Mathew 	write_mair0(mair0);
95b2bca61dSSoby Mathew 
96b2bca61dSSoby Mathew 	/* Invalidate TLBs at the current exception level */
97b2bca61dSSoby Mathew 	tlbiall();
98b2bca61dSSoby Mathew 
99b2bca61dSSoby Mathew 	/*
1005d21b037SSummer Qin 	 * Set TTBCR bits as well. Set TTBR0 table properties. Disable TTBR1.
101b2bca61dSSoby Mathew 	 */
102e7b9886cSAntonio Nino Diaz 	int t0sz = 32 - __builtin_ctzll(PLAT_VIRT_ADDR_SPACE_SIZE);
103e7b9886cSAntonio Nino Diaz 
104e7b9886cSAntonio Nino Diaz 	if ((flags & XLAT_TABLE_NC) != 0U) {
1055d21b037SSummer Qin 		/* Inner & outer non-cacheable non-shareable. */
1065d21b037SSummer Qin 		ttbcr = TTBCR_EAE_BIT |
1075d21b037SSummer Qin 			TTBCR_SH0_NON_SHAREABLE | TTBCR_RGN0_OUTER_NC |
108e7b9886cSAntonio Nino Diaz 			TTBCR_RGN0_INNER_NC | (uint32_t) t0sz;
1095d21b037SSummer Qin 	} else {
1105d21b037SSummer Qin 		/* Inner & outer WBWA & shareable. */
111b2bca61dSSoby Mathew 		ttbcr = TTBCR_EAE_BIT |
112b2bca61dSSoby Mathew 			TTBCR_SH0_INNER_SHAREABLE | TTBCR_RGN0_OUTER_WBA |
113e7b9886cSAntonio Nino Diaz 			TTBCR_RGN0_INNER_WBA | (uint32_t) t0sz;
1145d21b037SSummer Qin 	}
115b2bca61dSSoby Mathew 	ttbcr |= TTBCR_EPD1_BIT;
116b2bca61dSSoby Mathew 	write_ttbcr(ttbcr);
117b2bca61dSSoby Mathew 
118b2bca61dSSoby Mathew 	/* Set TTBR0 bits as well */
119e8719552SAntonio Nino Diaz 	ttbr0 = (uintptr_t) base_xlation_table;
120b2bca61dSSoby Mathew 	write64_ttbr0(ttbr0);
121e7b9886cSAntonio Nino Diaz 	write64_ttbr1(0U);
122b2bca61dSSoby Mathew 
123b2bca61dSSoby Mathew 	/*
124b2bca61dSSoby Mathew 	 * Ensure all translation table writes have drained
125b2bca61dSSoby Mathew 	 * into memory, the TLB invalidation is complete,
126b2bca61dSSoby Mathew 	 * and translation register writes are committed
127b2bca61dSSoby Mathew 	 * before enabling the MMU
128b2bca61dSSoby Mathew 	 */
1296f512a3dSDimitris Papastamos 	dsbish();
130b2bca61dSSoby Mathew 	isb();
131b2bca61dSSoby Mathew 
132b2bca61dSSoby Mathew 	sctlr = read_sctlr();
133b2bca61dSSoby Mathew 	sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT;
134b2bca61dSSoby Mathew 
135e7b9886cSAntonio Nino Diaz 	if ((flags & DISABLE_DCACHE) != 0U)
136b2bca61dSSoby Mathew 		sctlr &= ~SCTLR_C_BIT;
137b2bca61dSSoby Mathew 	else
138b2bca61dSSoby Mathew 		sctlr |= SCTLR_C_BIT;
139b2bca61dSSoby Mathew 
140b2bca61dSSoby Mathew 	write_sctlr(sctlr);
141b2bca61dSSoby Mathew 
142b2bca61dSSoby Mathew 	/* Ensure the MMU enable takes effect immediately */
143b2bca61dSSoby Mathew 	isb();
144b2bca61dSSoby Mathew }
14592bec97fSJeenu Viswambharan 
146*1a92a0e0SAntonio Nino Diaz void enable_mmu_direct_svc_mon(unsigned int flags)
14792bec97fSJeenu Viswambharan {
148*1a92a0e0SAntonio Nino Diaz 	enable_mmu_svc_mon(flags);
14992bec97fSJeenu Viswambharan }
150