xref: /rk3399_ARM-atf/lib/xlat_tables_v2/aarch64/xlat_tables_arch.c (revision d50ece03d9e95ac2e35546ceff0f787199e4420e)
1 /*
2  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of ARM nor the names of its contributors may be used
15  * to endorse or promote products derived from this software without specific
16  * prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <arch.h>
32 #include <arch_helpers.h>
33 #include <assert.h>
34 #include <bl_common.h>
35 #include <cassert.h>
36 #include <common_def.h>
37 #include <platform_def.h>
38 #include <sys/types.h>
39 #include <utils.h>
40 #include <xlat_tables_v2.h>
41 #include "../xlat_tables_private.h"
42 
43 #if defined(IMAGE_BL1) || defined(IMAGE_BL31)
44 # define IMAGE_EL	3
45 #else
46 # define IMAGE_EL	1
47 #endif
48 
49 static unsigned long long tcr_ps_bits;
50 
51 static unsigned long long calc_physical_addr_size_bits(
52 					unsigned long long max_addr)
53 {
54 	/* Physical address can't exceed 48 bits */
55 	assert((max_addr & ADDR_MASK_48_TO_63) == 0);
56 
57 	/* 48 bits address */
58 	if (max_addr & ADDR_MASK_44_TO_47)
59 		return TCR_PS_BITS_256TB;
60 
61 	/* 44 bits address */
62 	if (max_addr & ADDR_MASK_42_TO_43)
63 		return TCR_PS_BITS_16TB;
64 
65 	/* 42 bits address */
66 	if (max_addr & ADDR_MASK_40_TO_41)
67 		return TCR_PS_BITS_4TB;
68 
69 	/* 40 bits address */
70 	if (max_addr & ADDR_MASK_36_TO_39)
71 		return TCR_PS_BITS_1TB;
72 
73 	/* 36 bits address */
74 	if (max_addr & ADDR_MASK_32_TO_35)
75 		return TCR_PS_BITS_64GB;
76 
77 	return TCR_PS_BITS_4GB;
78 }
79 
80 #if DEBUG
81 /* Physical Address ranges supported in the AArch64 Memory Model */
82 static const unsigned int pa_range_bits_arr[] = {
83 	PARANGE_0000, PARANGE_0001, PARANGE_0010, PARANGE_0011, PARANGE_0100,
84 	PARANGE_0101
85 };
86 
87 unsigned long long xlat_arch_get_max_supported_pa(void)
88 {
89 	u_register_t pa_range = read_id_aa64mmfr0_el1() &
90 						ID_AA64MMFR0_EL1_PARANGE_MASK;
91 
92 	/* All other values are reserved */
93 	assert(pa_range < ARRAY_SIZE(pa_range_bits_arr));
94 
95 	return (1ull << pa_range_bits_arr[pa_range]) - 1ull;
96 }
97 #endif /* DEBUG*/
98 
99 int is_mmu_enabled(void)
100 {
101 #if IMAGE_EL == 1
102 	assert(IS_IN_EL(1));
103 	return (read_sctlr_el1() & SCTLR_M_BIT) != 0;
104 #elif IMAGE_EL == 3
105 	assert(IS_IN_EL(3));
106 	return (read_sctlr_el3() & SCTLR_M_BIT) != 0;
107 #endif
108 }
109 
110 void init_xlat_tables_arch(unsigned long long max_pa)
111 {
112 	assert((PLAT_PHY_ADDR_SPACE_SIZE - 1) <=
113 	       xlat_arch_get_max_supported_pa());
114 
115 	tcr_ps_bits = calc_physical_addr_size_bits(max_pa);
116 }
117 
118 /*******************************************************************************
119  * Macro generating the code for the function enabling the MMU in the given
120  * exception level, assuming that the pagetables have already been created.
121  *
122  *   _el:		Exception level at which the function will run
123  *   _tcr_extra:	Extra bits to set in the TCR register. This mask will
124  *			be OR'ed with the default TCR value.
125  *   _tlbi_fct:		Function to invalidate the TLBs at the current
126  *			exception level
127  ******************************************************************************/
128 #define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct)		\
129 	void enable_mmu_internal_el##_el(unsigned int flags,		\
130 					 uint64_t *base_table)		\
131 	{								\
132 		uint64_t mair, tcr, ttbr;				\
133 		uint32_t sctlr;						\
134 									\
135 		assert(IS_IN_EL(_el));					\
136 		assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0);	\
137 									\
138 		/* Invalidate TLBs at the current exception level */	\
139 		_tlbi_fct();						\
140 									\
141 		/* Set attributes in the right indices of the MAIR */	\
142 		mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX);	\
143 		mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR,		\
144 				ATTR_IWBWA_OWBWA_NTR_INDEX);		\
145 		mair |= MAIR_ATTR_SET(ATTR_NON_CACHEABLE,		\
146 				ATTR_NON_CACHEABLE_INDEX);		\
147 		write_mair_el##_el(mair);				\
148 									\
149 		/* Set TCR bits as well. */				\
150 		/* Inner & outer WBWA & shareable. */			\
151 		/* Set T0SZ to (64 - width of virtual address space) */	\
152 		tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA |	\
153 			TCR_RGN_INNER_WBA |				\
154 			(64 - __builtin_ctzl(PLAT_VIRT_ADDR_SPACE_SIZE));\
155 		tcr |= _tcr_extra;					\
156 		write_tcr_el##_el(tcr);					\
157 									\
158 		/* Set TTBR bits as well */				\
159 		ttbr = (uint64_t) base_table;				\
160 		write_ttbr0_el##_el(ttbr);				\
161 									\
162 		/* Ensure all translation table writes have drained */	\
163 		/* into memory, the TLB invalidation is complete, */	\
164 		/* and translation register writes are committed */	\
165 		/* before enabling the MMU */				\
166 		dsb();							\
167 		isb();							\
168 									\
169 		sctlr = read_sctlr_el##_el();				\
170 		sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT;			\
171 									\
172 		if (flags & DISABLE_DCACHE)				\
173 			sctlr &= ~SCTLR_C_BIT;				\
174 		else							\
175 			sctlr |= SCTLR_C_BIT;				\
176 									\
177 		write_sctlr_el##_el(sctlr);				\
178 									\
179 		/* Ensure the MMU enable takes effect immediately */	\
180 		isb();							\
181 	}
182 
183 /* Define EL1 and EL3 variants of the function enabling the MMU */
184 #if IMAGE_EL == 1
185 DEFINE_ENABLE_MMU_EL(1,
186 		(tcr_ps_bits << TCR_EL1_IPS_SHIFT),
187 		tlbivmalle1)
188 #elif IMAGE_EL == 3
189 DEFINE_ENABLE_MMU_EL(3,
190 		TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),
191 		tlbialle3)
192 #endif
193 
194 void enable_mmu_arch(unsigned int flags, uint64_t *base_table)
195 {
196 #if IMAGE_EL == 1
197 	assert(IS_IN_EL(1));
198 	enable_mmu_internal_el1(flags, base_table);
199 #elif IMAGE_EL == 3
200 	assert(IS_IN_EL(3));
201 	enable_mmu_internal_el3(flags, base_table);
202 #endif
203 }
204