blob: fa1a03dabf33e086341e9efe3683cb5d0032939b [file] [log] [blame]
Jon Medhurstbb1fe202014-01-24 15:41:33 +00001/*
2 * Copyright (c) 2014, 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
Dan Handleyb226a4d2014-05-16 14:08:45 +010031#include <arch.h>
32#include <arch_helpers.h>
Jon Medhurstbb1fe202014-01-24 15:41:33 +000033#include <assert.h>
Vikram Kanigiri725b1332015-03-04 10:34:27 +000034#include <bl_common.h>
Lin Ma741a3822014-06-27 16:56:30 -070035#include <cassert.h>
Dan Handleyed6ff952014-05-14 17:44:19 +010036#include <platform_def.h>
Jon Medhurstbb1fe202014-01-24 15:41:33 +000037#include <string.h>
38#include <xlat_tables.h>
39
40
41#ifndef DEBUG_XLAT_TABLE
42#define DEBUG_XLAT_TABLE 0
43#endif
44
45#if DEBUG_XLAT_TABLE
46#define debug_print(...) printf(__VA_ARGS__)
47#else
48#define debug_print(...) ((void)0)
49#endif
50
Lin Ma741a3822014-06-27 16:56:30 -070051CASSERT(ADDR_SPACE_SIZE > 0, assert_valid_addr_space_size);
Jon Medhurstbb1fe202014-01-24 15:41:33 +000052
53#define UNSET_DESC ~0ul
54
55#define NUM_L1_ENTRIES (ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT)
56
Dan Handleyb226a4d2014-05-16 14:08:45 +010057static uint64_t l1_xlation_table[NUM_L1_ENTRIES]
Jon Medhurstbb1fe202014-01-24 15:41:33 +000058__aligned(NUM_L1_ENTRIES * sizeof(uint64_t));
59
60static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
61__aligned(XLAT_TABLE_SIZE) __attribute__((section("xlat_table")));
62
63static unsigned next_xlat;
Lin Ma741a3822014-06-27 16:56:30 -070064static unsigned long max_pa;
65static unsigned long max_va;
66static unsigned long tcr_ps_bits;
Jon Medhurstbb1fe202014-01-24 15:41:33 +000067
68/*
69 * Array of all memory regions stored in order of ascending base address.
70 * The list is terminated by the first entry with size == 0.
71 */
Dan Handleye2712bc2014-04-10 15:37:22 +010072static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
Jon Medhurstbb1fe202014-01-24 15:41:33 +000073
74
75static void print_mmap(void)
76{
77#if DEBUG_XLAT_TABLE
78 debug_print("mmap:\n");
Dan Handleye2712bc2014-04-10 15:37:22 +010079 mmap_region_t *mm = mmap;
Jon Medhurstbb1fe202014-01-24 15:41:33 +000080 while (mm->size) {
Lin Ma13592362014-06-02 11:45:36 -070081 debug_print(" %010lx %010lx %10lx %x\n", mm->base_va,
82 mm->base_pa, mm->size, mm->attr);
Jon Medhurstbb1fe202014-01-24 15:41:33 +000083 ++mm;
84 };
85 debug_print("\n");
86#endif
87}
88
Lin Ma13592362014-06-02 11:45:36 -070089void mmap_add_region(unsigned long base_pa, unsigned long base_va,
90 unsigned long size, unsigned attr)
Jon Medhurstbb1fe202014-01-24 15:41:33 +000091{
Dan Handleye2712bc2014-04-10 15:37:22 +010092 mmap_region_t *mm = mmap;
Vikram Kanigiri725b1332015-03-04 10:34:27 +000093 mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
Lin Ma741a3822014-06-27 16:56:30 -070094 unsigned long pa_end = base_pa + size - 1;
95 unsigned long va_end = base_va + size - 1;
Jon Medhurstbb1fe202014-01-24 15:41:33 +000096
Lin Ma13592362014-06-02 11:45:36 -070097 assert(IS_PAGE_ALIGNED(base_pa));
98 assert(IS_PAGE_ALIGNED(base_va));
Jon Medhurstbb1fe202014-01-24 15:41:33 +000099 assert(IS_PAGE_ALIGNED(size));
100
101 if (!size)
102 return;
103
104 /* Find correct place in mmap to insert new region */
Lin Ma13592362014-06-02 11:45:36 -0700105 while (mm->base_va < base_va && mm->size)
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000106 ++mm;
107
108 /* Make room for new region by moving other regions up by one place */
109 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
110
111 /* Check we haven't lost the empty sentinal from the end of the array */
112 assert(mm_last->size == 0);
113
Lin Ma13592362014-06-02 11:45:36 -0700114 mm->base_pa = base_pa;
115 mm->base_va = base_va;
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000116 mm->size = size;
117 mm->attr = attr;
Lin Ma741a3822014-06-27 16:56:30 -0700118
119 if (pa_end > max_pa)
120 max_pa = pa_end;
121 if (va_end > max_va)
122 max_va = va_end;
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000123}
124
Dan Handleye2712bc2014-04-10 15:37:22 +0100125void mmap_add(const mmap_region_t *mm)
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000126{
127 while (mm->size) {
Lin Ma13592362014-06-02 11:45:36 -0700128 mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000129 ++mm;
130 }
131}
132
Lin Ma13592362014-06-02 11:45:36 -0700133static unsigned long mmap_desc(unsigned attr, unsigned long addr_pa,
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000134 unsigned level)
135{
Lin Ma13592362014-06-02 11:45:36 -0700136 unsigned long desc = addr_pa;
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000137
138 desc |= level == 3 ? TABLE_DESC : BLOCK_DESC;
139
140 desc |= attr & MT_NS ? LOWER_ATTRS(NS) : 0;
141
142 desc |= attr & MT_RW ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
143
144 desc |= LOWER_ATTRS(ACCESS_FLAG);
145
146 if (attr & MT_MEMORY) {
147 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
148 if (attr & MT_RW)
149 desc |= UPPER_ATTRS(XN);
150 } else {
151 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
152 desc |= UPPER_ATTRS(XN);
153 }
154
155 debug_print(attr & MT_MEMORY ? "MEM" : "DEV");
156 debug_print(attr & MT_RW ? "-RW" : "-RO");
157 debug_print(attr & MT_NS ? "-NS" : "-S");
158
159 return desc;
160}
161
Lin Ma13592362014-06-02 11:45:36 -0700162static int mmap_region_attr(mmap_region_t *mm, unsigned long base_va,
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000163 unsigned long size)
164{
165 int attr = mm->attr;
166
167 for (;;) {
168 ++mm;
169
170 if (!mm->size)
171 return attr; /* Reached end of list */
172
Lin Ma13592362014-06-02 11:45:36 -0700173 if (mm->base_va >= base_va + size)
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000174 return attr; /* Next region is after area so end */
175
Lin Ma13592362014-06-02 11:45:36 -0700176 if (mm->base_va + mm->size <= base_va)
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000177 continue; /* Next region has already been overtaken */
178
179 if ((mm->attr & attr) == attr)
180 continue; /* Region doesn't override attribs so skip */
181
182 attr &= mm->attr;
183
Lin Ma13592362014-06-02 11:45:36 -0700184 if (mm->base_va > base_va ||
185 mm->base_va + mm->size < base_va + size)
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000186 return -1; /* Region doesn't fully cover our area */
187 }
188}
189
Lin Ma13592362014-06-02 11:45:36 -0700190static mmap_region_t *init_xlation_table(mmap_region_t *mm,
191 unsigned long base_va,
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000192 unsigned long *table, unsigned level)
193{
194 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
195 XLAT_TABLE_ENTRIES_SHIFT;
196 unsigned level_size = 1 << level_size_shift;
Lin Ma0b9d59f2014-05-20 11:25:55 -0700197 unsigned long level_index_mask = XLAT_TABLE_ENTRIES_MASK << level_size_shift;
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000198
199 assert(level <= 3);
200
201 debug_print("New xlat table:\n");
202
203 do {
204 unsigned long desc = UNSET_DESC;
205
Lin Ma13592362014-06-02 11:45:36 -0700206 if (mm->base_va + mm->size <= base_va) {
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000207 /* Area now after the region so skip it */
208 ++mm;
209 continue;
210 }
211
Lin Ma13592362014-06-02 11:45:36 -0700212 debug_print(" %010lx %8lx " + 6 - 2 * level, base_va,
213 level_size);
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000214
Lin Ma13592362014-06-02 11:45:36 -0700215 if (mm->base_va >= base_va + level_size) {
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000216 /* Next region is after area so nothing to map yet */
217 desc = INVALID_DESC;
Lin Ma13592362014-06-02 11:45:36 -0700218 } else if (mm->base_va <= base_va && mm->base_va + mm->size >=
219 base_va + level_size) {
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000220 /* Next region covers all of area */
Lin Ma13592362014-06-02 11:45:36 -0700221 int attr = mmap_region_attr(mm, base_va, level_size);
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000222 if (attr >= 0)
Lin Ma13592362014-06-02 11:45:36 -0700223 desc = mmap_desc(attr,
224 base_va - mm->base_va + mm->base_pa,
225 level);
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000226 }
227 /* else Next region only partially covers area, so need */
228
229 if (desc == UNSET_DESC) {
230 /* Area not covered by a region so need finer table */
231 unsigned long *new_table = xlat_tables[next_xlat++];
232 assert(next_xlat <= MAX_XLAT_TABLES);
233 desc = TABLE_DESC | (unsigned long)new_table;
234
235 /* Recurse to fill in new table */
Lin Ma13592362014-06-02 11:45:36 -0700236 mm = init_xlation_table(mm, base_va,
237 new_table, level+1);
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000238 }
239
240 debug_print("\n");
241
242 *table++ = desc;
Lin Ma13592362014-06-02 11:45:36 -0700243 base_va += level_size;
244 } while (mm->size && (base_va & level_index_mask));
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000245
246 return mm;
247}
248
Lin Ma741a3822014-06-27 16:56:30 -0700249static unsigned int calc_physical_addr_size_bits(unsigned long max_addr)
250{
251 /* Physical address can't exceed 48 bits */
252 assert((max_addr & ADDR_MASK_48_TO_63) == 0);
253
254 /* 48 bits address */
255 if (max_addr & ADDR_MASK_44_TO_47)
256 return TCR_PS_BITS_256TB;
257
258 /* 44 bits address */
259 if (max_addr & ADDR_MASK_42_TO_43)
260 return TCR_PS_BITS_16TB;
261
262 /* 42 bits address */
263 if (max_addr & ADDR_MASK_40_TO_41)
264 return TCR_PS_BITS_4TB;
265
266 /* 40 bits address */
267 if (max_addr & ADDR_MASK_36_TO_39)
268 return TCR_PS_BITS_1TB;
269
270 /* 36 bits address */
271 if (max_addr & ADDR_MASK_32_TO_35)
272 return TCR_PS_BITS_64GB;
273
274 return TCR_PS_BITS_4GB;
275}
276
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000277void init_xlat_tables(void)
278{
279 print_mmap();
280 init_xlation_table(mmap, 0, l1_xlation_table, 1);
Lin Ma741a3822014-06-27 16:56:30 -0700281 tcr_ps_bits = calc_physical_addr_size_bits(max_pa);
282 assert(max_va < ADDR_SPACE_SIZE);
Jon Medhurstbb1fe202014-01-24 15:41:33 +0000283}
Dan Handleyb226a4d2014-05-16 14:08:45 +0100284
285/*******************************************************************************
286 * Macro generating the code for the function enabling the MMU in the given
287 * exception level, assuming that the pagetables have already been created.
288 *
289 * _el: Exception level at which the function will run
290 * _tcr_extra: Extra bits to set in the TCR register. This mask will
291 * be OR'ed with the default TCR value.
292 * _tlbi_fct: Function to invalidate the TLBs at the current
293 * exception level
294 ******************************************************************************/
295#define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \
Achin Guptae9982542014-06-26 08:59:07 +0100296 void enable_mmu_el##_el(uint32_t flags) \
Dan Handleyb226a4d2014-05-16 14:08:45 +0100297 { \
298 uint64_t mair, tcr, ttbr; \
299 uint32_t sctlr; \
300 \
301 assert(IS_IN_EL(_el)); \
302 assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0); \
303 \
304 /* Set attributes in the right indices of the MAIR */ \
305 mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \
306 mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \
307 ATTR_IWBWA_OWBWA_NTR_INDEX); \
308 write_mair_el##_el(mair); \
309 \
310 /* Invalidate TLBs at the current exception level */ \
311 _tlbi_fct(); \
312 \
313 /* Set TCR bits as well. */ \
314 /* Inner & outer WBWA & shareable + T0SZ = 32 */ \
315 tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | \
Lin Ma741a3822014-06-27 16:56:30 -0700316 TCR_RGN_INNER_WBA | \
317 (64 - __builtin_ctzl(ADDR_SPACE_SIZE)); \
Dan Handleyb226a4d2014-05-16 14:08:45 +0100318 tcr |= _tcr_extra; \
319 write_tcr_el##_el(tcr); \
320 \
321 /* Set TTBR bits as well */ \
322 ttbr = (uint64_t) l1_xlation_table; \
323 write_ttbr0_el##_el(ttbr); \
324 \
325 /* Ensure all translation table writes have drained */ \
326 /* into memory, the TLB invalidation is complete, */ \
327 /* and translation register writes are committed */ \
328 /* before enabling the MMU */ \
329 dsb(); \
330 isb(); \
331 \
332 sctlr = read_sctlr_el##_el(); \
Achin Gupta9f098352014-07-18 18:38:28 +0100333 sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \
Achin Guptae9982542014-06-26 08:59:07 +0100334 \
335 if (flags & DISABLE_DCACHE) \
336 sctlr &= ~SCTLR_C_BIT; \
337 else \
338 sctlr |= SCTLR_C_BIT; \
339 \
Dan Handleyb226a4d2014-05-16 14:08:45 +0100340 write_sctlr_el##_el(sctlr); \
341 \
342 /* Ensure the MMU enable takes effect immediately */ \
343 isb(); \
344 }
345
346/* Define EL1 and EL3 variants of the function enabling the MMU */
Lin Ma741a3822014-06-27 16:56:30 -0700347DEFINE_ENABLE_MMU_EL(1,
348 (tcr_ps_bits << TCR_EL1_IPS_SHIFT),
349 tlbivmalle1)
350DEFINE_ENABLE_MMU_EL(3,
351 TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),
352 tlbialle3)