blob: 1e748a36a96da9dcfe3d175d694912a702b0fba3 [file] [log] [blame]
Jon Medhurstc481c262014-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 Handleydff8e472014-05-16 14:08:45 +010031#include <arch.h>
32#include <arch_helpers.h>
Jon Medhurstc481c262014-01-24 15:41:33 +000033#include <assert.h>
Vikram Kanigiria7e98ad2015-03-04 10:34:27 +000034#include <bl_common.h>
Lin Ma73ad2572014-06-27 16:56:30 -070035#include <cassert.h>
Soby Mathewd30ac1c2016-01-19 17:52:28 +000036#include <debug.h>
Dan Handley5f0cdb02014-05-14 17:44:19 +010037#include <platform_def.h>
Jon Medhurstc481c262014-01-24 15:41:33 +000038#include <string.h>
39#include <xlat_tables.h>
40
Soby Mathewd30ac1c2016-01-19 17:52:28 +000041#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
42#define LVL0_SPACER ""
43#define LVL1_SPACER " "
44#define LVL2_SPACER " "
45#define LVL3_SPACER " "
46#define get_level_spacer(level) \
47 (((level) == 0) ? LVL0_SPACER : \
48 (((level) == 1) ? LVL1_SPACER : \
49 (((level) == 2) ? LVL2_SPACER : LVL3_SPACER)))
50#define debug_print(...) tf_printf(__VA_ARGS__)
Jon Medhurstc481c262014-01-24 15:41:33 +000051#else
52#define debug_print(...) ((void)0)
53#endif
54
Kristina Martsenko2af926d2016-02-11 18:11:56 +000055#define IS_POWER_OF_TWO(x) (((x) & ((x) - 1)) == 0)
56
57/*
58 * The virtual address space size must be a power of two (as set in TCR.T0SZ).
59 * As we start the initial lookup at level 1, it must also be between 2 GB and
60 * 512 GB (with the virtual address size therefore 31 to 39 bits). See section
61 * D4.2.5 in the ARMv8-A Architecture Reference Manual (DDI 0487A.i) for more
62 * information.
63 */
64CASSERT(ADDR_SPACE_SIZE >= (1ull << 31) && ADDR_SPACE_SIZE <= (1ull << 39) &&
65 IS_POWER_OF_TWO(ADDR_SPACE_SIZE), assert_valid_addr_space_size);
Jon Medhurstc481c262014-01-24 15:41:33 +000066
67#define UNSET_DESC ~0ul
68
69#define NUM_L1_ENTRIES (ADDR_SPACE_SIZE >> L1_XLAT_ADDRESS_SHIFT)
70
Dan Handleydff8e472014-05-16 14:08:45 +010071static uint64_t l1_xlation_table[NUM_L1_ENTRIES]
Jon Medhurstc481c262014-01-24 15:41:33 +000072__aligned(NUM_L1_ENTRIES * sizeof(uint64_t));
73
74static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
Soren Brinkmann65cd2992016-01-14 10:11:05 -080075__aligned(XLAT_TABLE_SIZE) __section("xlat_table");
Jon Medhurstc481c262014-01-24 15:41:33 +000076
77static unsigned next_xlat;
Lin Ma73ad2572014-06-27 16:56:30 -070078static unsigned long max_pa;
79static unsigned long max_va;
80static unsigned long tcr_ps_bits;
Jon Medhurstc481c262014-01-24 15:41:33 +000081
82/*
83 * Array of all memory regions stored in order of ascending base address.
84 * The list is terminated by the first entry with size == 0.
85 */
Dan Handleyfb037bf2014-04-10 15:37:22 +010086static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
Jon Medhurstc481c262014-01-24 15:41:33 +000087
88
89static void print_mmap(void)
90{
Soby Mathewd30ac1c2016-01-19 17:52:28 +000091#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
Jon Medhurstc481c262014-01-24 15:41:33 +000092 debug_print("mmap:\n");
Dan Handleyfb037bf2014-04-10 15:37:22 +010093 mmap_region_t *mm = mmap;
Jon Medhurstc481c262014-01-24 15:41:33 +000094 while (mm->size) {
Soby Mathewd30ac1c2016-01-19 17:52:28 +000095 debug_print(" VA:0x%lx PA:0x%lx size:0x%lx attr:0x%x\n",
96 mm->base_va, mm->base_pa, mm->size, mm->attr);
Jon Medhurstc481c262014-01-24 15:41:33 +000097 ++mm;
98 };
99 debug_print("\n");
100#endif
101}
102
Lin Maf984ce82014-06-02 11:45:36 -0700103void mmap_add_region(unsigned long base_pa, unsigned long base_va,
104 unsigned long size, unsigned attr)
Jon Medhurstc481c262014-01-24 15:41:33 +0000105{
Dan Handleyfb037bf2014-04-10 15:37:22 +0100106 mmap_region_t *mm = mmap;
Vikram Kanigiria7e98ad2015-03-04 10:34:27 +0000107 mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
Lin Ma73ad2572014-06-27 16:56:30 -0700108 unsigned long pa_end = base_pa + size - 1;
109 unsigned long va_end = base_va + size - 1;
Jon Medhurstc481c262014-01-24 15:41:33 +0000110
Lin Maf984ce82014-06-02 11:45:36 -0700111 assert(IS_PAGE_ALIGNED(base_pa));
112 assert(IS_PAGE_ALIGNED(base_va));
Jon Medhurstc481c262014-01-24 15:41:33 +0000113 assert(IS_PAGE_ALIGNED(size));
114
115 if (!size)
116 return;
117
118 /* Find correct place in mmap to insert new region */
Lin Maf984ce82014-06-02 11:45:36 -0700119 while (mm->base_va < base_va && mm->size)
Jon Medhurstc481c262014-01-24 15:41:33 +0000120 ++mm;
121
122 /* Make room for new region by moving other regions up by one place */
123 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
124
125 /* Check we haven't lost the empty sentinal from the end of the array */
126 assert(mm_last->size == 0);
127
Lin Maf984ce82014-06-02 11:45:36 -0700128 mm->base_pa = base_pa;
129 mm->base_va = base_va;
Jon Medhurstc481c262014-01-24 15:41:33 +0000130 mm->size = size;
131 mm->attr = attr;
Lin Ma73ad2572014-06-27 16:56:30 -0700132
133 if (pa_end > max_pa)
134 max_pa = pa_end;
135 if (va_end > max_va)
136 max_va = va_end;
Jon Medhurstc481c262014-01-24 15:41:33 +0000137}
138
Dan Handleyfb037bf2014-04-10 15:37:22 +0100139void mmap_add(const mmap_region_t *mm)
Jon Medhurstc481c262014-01-24 15:41:33 +0000140{
141 while (mm->size) {
Lin Maf984ce82014-06-02 11:45:36 -0700142 mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
Jon Medhurstc481c262014-01-24 15:41:33 +0000143 ++mm;
144 }
145}
146
Lin Maf984ce82014-06-02 11:45:36 -0700147static unsigned long mmap_desc(unsigned attr, unsigned long addr_pa,
Jon Medhurstc481c262014-01-24 15:41:33 +0000148 unsigned level)
149{
Lin Maf984ce82014-06-02 11:45:36 -0700150 unsigned long desc = addr_pa;
Jon Medhurstc481c262014-01-24 15:41:33 +0000151
152 desc |= level == 3 ? TABLE_DESC : BLOCK_DESC;
153
154 desc |= attr & MT_NS ? LOWER_ATTRS(NS) : 0;
155
156 desc |= attr & MT_RW ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
157
158 desc |= LOWER_ATTRS(ACCESS_FLAG);
159
160 if (attr & MT_MEMORY) {
161 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
162 if (attr & MT_RW)
163 desc |= UPPER_ATTRS(XN);
164 } else {
165 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
166 desc |= UPPER_ATTRS(XN);
167 }
168
169 debug_print(attr & MT_MEMORY ? "MEM" : "DEV");
170 debug_print(attr & MT_RW ? "-RW" : "-RO");
171 debug_print(attr & MT_NS ? "-NS" : "-S");
172
173 return desc;
174}
175
Lin Maf984ce82014-06-02 11:45:36 -0700176static int mmap_region_attr(mmap_region_t *mm, unsigned long base_va,
Jon Medhurstc481c262014-01-24 15:41:33 +0000177 unsigned long size)
178{
179 int attr = mm->attr;
180
181 for (;;) {
182 ++mm;
183
184 if (!mm->size)
185 return attr; /* Reached end of list */
186
Lin Maf984ce82014-06-02 11:45:36 -0700187 if (mm->base_va >= base_va + size)
Jon Medhurstc481c262014-01-24 15:41:33 +0000188 return attr; /* Next region is after area so end */
189
Lin Maf984ce82014-06-02 11:45:36 -0700190 if (mm->base_va + mm->size <= base_va)
Jon Medhurstc481c262014-01-24 15:41:33 +0000191 continue; /* Next region has already been overtaken */
192
193 if ((mm->attr & attr) == attr)
194 continue; /* Region doesn't override attribs so skip */
195
196 attr &= mm->attr;
197
Lin Maf984ce82014-06-02 11:45:36 -0700198 if (mm->base_va > base_va ||
199 mm->base_va + mm->size < base_va + size)
Jon Medhurstc481c262014-01-24 15:41:33 +0000200 return -1; /* Region doesn't fully cover our area */
201 }
202}
203
Lin Maf984ce82014-06-02 11:45:36 -0700204static mmap_region_t *init_xlation_table(mmap_region_t *mm,
205 unsigned long base_va,
Jon Medhurstc481c262014-01-24 15:41:33 +0000206 unsigned long *table, unsigned level)
207{
208 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
209 XLAT_TABLE_ENTRIES_SHIFT;
210 unsigned level_size = 1 << level_size_shift;
Lin Ma444281c2014-05-20 11:25:55 -0700211 unsigned long level_index_mask = XLAT_TABLE_ENTRIES_MASK << level_size_shift;
Jon Medhurstc481c262014-01-24 15:41:33 +0000212
213 assert(level <= 3);
214
215 debug_print("New xlat table:\n");
216
217 do {
218 unsigned long desc = UNSET_DESC;
219
Kristina Martsenko2af926d2016-02-11 18:11:56 +0000220 if (!mm->size) {
221 /* Done mapping regions; finish zeroing the table */
222 desc = INVALID_DESC;
223 } else if (mm->base_va + mm->size <= base_va) {
Jon Medhurstc481c262014-01-24 15:41:33 +0000224 /* Area now after the region so skip it */
225 ++mm;
226 continue;
227 }
228
Soby Mathewd30ac1c2016-01-19 17:52:28 +0000229 debug_print("%s VA:0x%lx size:0x%x ", get_level_spacer(level),
230 base_va, level_size);
Jon Medhurstc481c262014-01-24 15:41:33 +0000231
Lin Maf984ce82014-06-02 11:45:36 -0700232 if (mm->base_va >= base_va + level_size) {
Jon Medhurstc481c262014-01-24 15:41:33 +0000233 /* Next region is after area so nothing to map yet */
234 desc = INVALID_DESC;
Lin Maf984ce82014-06-02 11:45:36 -0700235 } else if (mm->base_va <= base_va && mm->base_va + mm->size >=
236 base_va + level_size) {
Jon Medhurstc481c262014-01-24 15:41:33 +0000237 /* Next region covers all of area */
Lin Maf984ce82014-06-02 11:45:36 -0700238 int attr = mmap_region_attr(mm, base_va, level_size);
Jon Medhurstc481c262014-01-24 15:41:33 +0000239 if (attr >= 0)
Lin Maf984ce82014-06-02 11:45:36 -0700240 desc = mmap_desc(attr,
241 base_va - mm->base_va + mm->base_pa,
242 level);
Jon Medhurstc481c262014-01-24 15:41:33 +0000243 }
244 /* else Next region only partially covers area, so need */
245
246 if (desc == UNSET_DESC) {
247 /* Area not covered by a region so need finer table */
248 unsigned long *new_table = xlat_tables[next_xlat++];
249 assert(next_xlat <= MAX_XLAT_TABLES);
250 desc = TABLE_DESC | (unsigned long)new_table;
251
252 /* Recurse to fill in new table */
Lin Maf984ce82014-06-02 11:45:36 -0700253 mm = init_xlation_table(mm, base_va,
254 new_table, level+1);
Jon Medhurstc481c262014-01-24 15:41:33 +0000255 }
256
257 debug_print("\n");
258
259 *table++ = desc;
Lin Maf984ce82014-06-02 11:45:36 -0700260 base_va += level_size;
Kristina Martsenko2af926d2016-02-11 18:11:56 +0000261 } while ((base_va & level_index_mask) && (base_va < ADDR_SPACE_SIZE));
Jon Medhurstc481c262014-01-24 15:41:33 +0000262
263 return mm;
264}
265
Lin Ma73ad2572014-06-27 16:56:30 -0700266static unsigned int calc_physical_addr_size_bits(unsigned long max_addr)
267{
268 /* Physical address can't exceed 48 bits */
269 assert((max_addr & ADDR_MASK_48_TO_63) == 0);
270
271 /* 48 bits address */
272 if (max_addr & ADDR_MASK_44_TO_47)
273 return TCR_PS_BITS_256TB;
274
275 /* 44 bits address */
276 if (max_addr & ADDR_MASK_42_TO_43)
277 return TCR_PS_BITS_16TB;
278
279 /* 42 bits address */
280 if (max_addr & ADDR_MASK_40_TO_41)
281 return TCR_PS_BITS_4TB;
282
283 /* 40 bits address */
284 if (max_addr & ADDR_MASK_36_TO_39)
285 return TCR_PS_BITS_1TB;
286
287 /* 36 bits address */
288 if (max_addr & ADDR_MASK_32_TO_35)
289 return TCR_PS_BITS_64GB;
290
291 return TCR_PS_BITS_4GB;
292}
293
Jon Medhurstc481c262014-01-24 15:41:33 +0000294void init_xlat_tables(void)
295{
296 print_mmap();
297 init_xlation_table(mmap, 0, l1_xlation_table, 1);
Lin Ma73ad2572014-06-27 16:56:30 -0700298 tcr_ps_bits = calc_physical_addr_size_bits(max_pa);
299 assert(max_va < ADDR_SPACE_SIZE);
Jon Medhurstc481c262014-01-24 15:41:33 +0000300}
Dan Handleydff8e472014-05-16 14:08:45 +0100301
302/*******************************************************************************
303 * Macro generating the code for the function enabling the MMU in the given
304 * exception level, assuming that the pagetables have already been created.
305 *
306 * _el: Exception level at which the function will run
307 * _tcr_extra: Extra bits to set in the TCR register. This mask will
308 * be OR'ed with the default TCR value.
309 * _tlbi_fct: Function to invalidate the TLBs at the current
310 * exception level
311 ******************************************************************************/
312#define DEFINE_ENABLE_MMU_EL(_el, _tcr_extra, _tlbi_fct) \
Achin Guptaafff8cb2014-06-26 08:59:07 +0100313 void enable_mmu_el##_el(uint32_t flags) \
Dan Handleydff8e472014-05-16 14:08:45 +0100314 { \
315 uint64_t mair, tcr, ttbr; \
316 uint32_t sctlr; \
317 \
318 assert(IS_IN_EL(_el)); \
319 assert((read_sctlr_el##_el() & SCTLR_M_BIT) == 0); \
320 \
321 /* Set attributes in the right indices of the MAIR */ \
322 mair = MAIR_ATTR_SET(ATTR_DEVICE, ATTR_DEVICE_INDEX); \
323 mair |= MAIR_ATTR_SET(ATTR_IWBWA_OWBWA_NTR, \
324 ATTR_IWBWA_OWBWA_NTR_INDEX); \
325 write_mair_el##_el(mair); \
326 \
327 /* Invalidate TLBs at the current exception level */ \
328 _tlbi_fct(); \
329 \
330 /* Set TCR bits as well. */ \
331 /* Inner & outer WBWA & shareable + T0SZ = 32 */ \
332 tcr = TCR_SH_INNER_SHAREABLE | TCR_RGN_OUTER_WBA | \
Lin Ma73ad2572014-06-27 16:56:30 -0700333 TCR_RGN_INNER_WBA | \
334 (64 - __builtin_ctzl(ADDR_SPACE_SIZE)); \
Dan Handleydff8e472014-05-16 14:08:45 +0100335 tcr |= _tcr_extra; \
336 write_tcr_el##_el(tcr); \
337 \
338 /* Set TTBR bits as well */ \
339 ttbr = (uint64_t) l1_xlation_table; \
340 write_ttbr0_el##_el(ttbr); \
341 \
342 /* Ensure all translation table writes have drained */ \
343 /* into memory, the TLB invalidation is complete, */ \
344 /* and translation register writes are committed */ \
345 /* before enabling the MMU */ \
346 dsb(); \
347 isb(); \
348 \
349 sctlr = read_sctlr_el##_el(); \
Achin Guptaec3c1002014-07-18 18:38:28 +0100350 sctlr |= SCTLR_WXN_BIT | SCTLR_M_BIT; \
Achin Guptaafff8cb2014-06-26 08:59:07 +0100351 \
352 if (flags & DISABLE_DCACHE) \
353 sctlr &= ~SCTLR_C_BIT; \
354 else \
355 sctlr |= SCTLR_C_BIT; \
356 \
Dan Handleydff8e472014-05-16 14:08:45 +0100357 write_sctlr_el##_el(sctlr); \
358 \
359 /* Ensure the MMU enable takes effect immediately */ \
360 isb(); \
361 }
362
363/* Define EL1 and EL3 variants of the function enabling the MMU */
Lin Ma73ad2572014-06-27 16:56:30 -0700364DEFINE_ENABLE_MMU_EL(1,
365 (tcr_ps_bits << TCR_EL1_IPS_SHIFT),
366 tlbivmalle1)
367DEFINE_ENABLE_MMU_EL(3,
368 TCR_EL3_RES1 | (tcr_ps_bits << TCR_EL3_PS_SHIFT),
369 tlbialle3)