blob: ce6e341a2fb4a31f27cd9a37f38a8d473d3809e9 [file] [log] [blame]
Soby Mathew44170c42016-03-22 15:51:08 +00001/*
Antonio Nino Diaz49074492018-04-26 12:59:08 +01002 * Copyright (c) 2016-2018, ARM Limited and Contributors. All rights reserved.
Soby Mathew44170c42016-03-22 15:51:08 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Soby Mathew44170c42016-03-22 15:51:08 +00005 */
6
7#include <arch.h>
8#include <arch_helpers.h>
9#include <assert.h>
Soby Mathew44170c42016-03-22 15:51:08 +000010#include <cassert.h>
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000011#include <common_def.h>
Soby Mathew44170c42016-03-22 15:51:08 +000012#include <debug.h>
13#include <platform_def.h>
14#include <string.h>
Soby Mathewa0fedc42016-06-16 14:52:04 +010015#include <types.h>
Sandrine Bailleux7659a262016-07-05 09:55:03 +010016#include <utils.h>
Soby Mathew44170c42016-03-22 15:51:08 +000017#include <xlat_tables.h>
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000018#include "xlat_tables_private.h"
Soby Mathew44170c42016-03-22 15:51:08 +000019
20#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
21#define LVL0_SPACER ""
22#define LVL1_SPACER " "
23#define LVL2_SPACER " "
24#define LVL3_SPACER " "
25#define get_level_spacer(level) \
David Cunado8a354f12017-06-21 16:52:45 +010026 (((level) == U(0)) ? LVL0_SPACER : \
27 (((level) == U(1)) ? LVL1_SPACER : \
28 (((level) == U(2)) ? LVL2_SPACER : LVL3_SPACER)))
Soby Mathew44170c42016-03-22 15:51:08 +000029#define debug_print(...) tf_printf(__VA_ARGS__)
30#else
31#define debug_print(...) ((void)0)
32#endif
33
David Cunadoc1503122018-02-16 21:12:58 +000034#define UNSET_DESC ~0ULL
Soby Mathew44170c42016-03-22 15:51:08 +000035
36static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
37 __aligned(XLAT_TABLE_SIZE) __section("xlat_table");
38
David Cunado8a354f12017-06-21 16:52:45 +010039static unsigned int next_xlat;
Soby Mathew44170c42016-03-22 15:51:08 +000040static unsigned long long xlat_max_pa;
41static uintptr_t xlat_max_va;
42
Antonio Nino Diazefabaa92017-04-27 13:30:22 +010043static uint64_t execute_never_mask;
Antonio Nino Diaz49074492018-04-26 12:59:08 +010044static uint64_t ap1_mask;
Antonio Nino Diazefabaa92017-04-27 13:30:22 +010045
Soby Mathew44170c42016-03-22 15:51:08 +000046/*
47 * Array of all memory regions stored in order of ascending base address.
48 * The list is terminated by the first entry with size == 0.
49 */
50static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
51
52
53void print_mmap(void)
54{
55#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
56 debug_print("mmap:\n");
57 mmap_region_t *mm = mmap;
58 while (mm->size) {
59 debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
60 (void *)mm->base_va, mm->base_pa,
61 mm->size, mm->attr);
62 ++mm;
63 };
64 debug_print("\n");
65#endif
66}
67
68void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
Antonio Nino Diaz8643a812018-06-21 14:39:16 +010069 size_t size, unsigned int attr)
Soby Mathew44170c42016-03-22 15:51:08 +000070{
71 mmap_region_t *mm = mmap;
72 mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +010073 unsigned long long end_pa = base_pa + size - 1;
74 uintptr_t end_va = base_va + size - 1;
Soby Mathew44170c42016-03-22 15:51:08 +000075
76 assert(IS_PAGE_ALIGNED(base_pa));
77 assert(IS_PAGE_ALIGNED(base_va));
78 assert(IS_PAGE_ALIGNED(size));
79
80 if (!size)
81 return;
82
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +010083 assert(base_pa < end_pa); /* Check for overflows */
84 assert(base_va < end_va);
85
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000086 assert((base_va + (uintptr_t)size - (uintptr_t)1) <=
87 (PLAT_VIRT_ADDR_SPACE_SIZE - 1));
88 assert((base_pa + (unsigned long long)size - 1ULL) <=
89 (PLAT_PHY_ADDR_SPACE_SIZE - 1));
90
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +000091#if ENABLE_ASSERTIONS
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +010092
93 /* Check for PAs and VAs overlaps with all other regions */
94 for (mm = mmap; mm->size; ++mm) {
95
96 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
97
98 /*
99 * Check if one of the regions is completely inside the other
100 * one.
101 */
102 int fully_overlapped_va =
103 ((base_va >= mm->base_va) && (end_va <= mm_end_va)) ||
104 ((mm->base_va >= base_va) && (mm_end_va <= end_va));
105
106 /*
107 * Full VA overlaps are only allowed if both regions are
108 * identity mapped (zero offset) or have the same VA to PA
109 * offset. Also, make sure that it's not the exact same area.
110 */
111 if (fully_overlapped_va) {
112 assert((mm->base_va - mm->base_pa) ==
113 (base_va - base_pa));
114 assert((base_va != mm->base_va) || (size != mm->size));
115 } else {
116 /*
117 * If the regions do not have fully overlapping VAs,
118 * then they must have fully separated VAs and PAs.
119 * Partial overlaps are not allowed
120 */
121
122 unsigned long long mm_end_pa =
123 mm->base_pa + mm->size - 1;
124
125 int separated_pa =
126 (end_pa < mm->base_pa) || (base_pa > mm_end_pa);
127 int separated_va =
128 (end_va < mm->base_va) || (base_va > mm_end_va);
129
130 assert(separated_va && separated_pa);
131 }
132 }
133
134 mm = mmap; /* Restore pointer to the start of the array */
135
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +0000136#endif /* ENABLE_ASSERTIONS */
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100137
Soby Mathew44170c42016-03-22 15:51:08 +0000138 /* Find correct place in mmap to insert new region */
139 while (mm->base_va < base_va && mm->size)
140 ++mm;
141
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100142 /*
143 * If a section is contained inside another one with the same base
144 * address, it must be placed after the one it is contained in:
145 *
146 * 1st |-----------------------|
147 * 2nd |------------|
148 * 3rd |------|
149 *
150 * This is required for mmap_region_attr() to get the attributes of the
151 * small region correctly.
152 */
153 while ((mm->base_va == base_va) && (mm->size > size))
154 ++mm;
155
Soby Mathew44170c42016-03-22 15:51:08 +0000156 /* Make room for new region by moving other regions up by one place */
157 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
158
159 /* Check we haven't lost the empty sentinal from the end of the array */
160 assert(mm_last->size == 0);
161
162 mm->base_pa = base_pa;
163 mm->base_va = base_va;
164 mm->size = size;
165 mm->attr = attr;
166
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100167 if (end_pa > xlat_max_pa)
168 xlat_max_pa = end_pa;
169 if (end_va > xlat_max_va)
170 xlat_max_va = end_va;
Soby Mathew44170c42016-03-22 15:51:08 +0000171}
172
173void mmap_add(const mmap_region_t *mm)
174{
175 while (mm->size) {
176 mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
177 ++mm;
178 }
179}
180
Antonio Nino Diaz8643a812018-06-21 14:39:16 +0100181static uint64_t mmap_desc(unsigned int attr, unsigned long long addr_pa,
182 unsigned int level)
Soby Mathew44170c42016-03-22 15:51:08 +0000183{
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100184 uint64_t desc;
Soby Mathew44170c42016-03-22 15:51:08 +0000185 int mem_type;
186
Antonio Nino Diaz46a33a52016-12-08 16:03:46 +0000187 /* Make sure that the granularity is fine enough to map this address. */
188 assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0);
189
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100190 desc = addr_pa;
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100191 /*
192 * There are different translation table descriptors for level 3 and the
193 * rest.
194 */
195 desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC;
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100196 desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0;
197 desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Antonio Nino Diaz0842bd62018-07-12 15:54:10 +0100198 /*
199 * Always set the access flag, as this library assumes access flag
200 * faults aren't managed.
201 */
Soby Mathew44170c42016-03-22 15:51:08 +0000202 desc |= LOWER_ATTRS(ACCESS_FLAG);
Antonio Nino Diaz49074492018-04-26 12:59:08 +0100203 desc |= ap1_mask;
Soby Mathew44170c42016-03-22 15:51:08 +0000204
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100205 /*
206 * Deduce shareability domain and executability of the memory region
207 * from the memory type.
208 *
209 * Data accesses to device memory and non-cacheable normal memory are
210 * coherent for all observers in the system, and correspondingly are
211 * always treated as being Outer Shareable. Therefore, for these 2 types
212 * of memory, it is not strictly needed to set the shareability field
213 * in the translation tables.
214 */
Soby Mathew44170c42016-03-22 15:51:08 +0000215 mem_type = MT_TYPE(attr);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100216 if (mem_type == MT_DEVICE) {
Soby Mathew44170c42016-03-22 15:51:08 +0000217 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100218 /*
219 * Always map device memory as execute-never.
220 * This is to avoid the possibility of a speculative instruction
221 * fetch, which could be an issue if this memory region
222 * corresponds to a read-sensitive peripheral.
223 */
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100224 desc |= execute_never_mask;
225
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100226 } else { /* Normal memory */
227 /*
228 * Always map read-write normal memory as execute-never.
Antonio Nino Diaz0842bd62018-07-12 15:54:10 +0100229 * This library assumes that it is used by software that does
230 * not self-modify its code, therefore R/W memory is reserved
231 * for data storage, which must not be executable.
232 *
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100233 * Note that setting the XN bit here is for consistency only.
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100234 * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100235 * which makes any writable memory region to be treated as
236 * execute-never, regardless of the value of the XN bit in the
237 * translation table.
Sandrine Bailleuxac3aa682016-06-14 16:31:09 +0100238 *
239 * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
240 * attribute to figure out the value of the XN bit.
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100241 */
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100242 if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) {
243 desc |= execute_never_mask;
244 }
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100245
246 if (mem_type == MT_MEMORY) {
247 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
248 } else {
249 assert(mem_type == MT_NON_CACHEABLE);
250 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
251 }
Soby Mathew44170c42016-03-22 15:51:08 +0000252 }
253
254 debug_print((mem_type == MT_MEMORY) ? "MEM" :
255 ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV"));
256 debug_print(attr & MT_RW ? "-RW" : "-RO");
257 debug_print(attr & MT_NS ? "-NS" : "-S");
Sandrine Bailleuxac3aa682016-06-14 16:31:09 +0100258 debug_print(attr & MT_EXECUTE_NEVER ? "-XN" : "-EXEC");
Soby Mathew44170c42016-03-22 15:51:08 +0000259 return desc;
260}
261
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100262/*
Nishanth Menon6f243462017-05-01 12:26:34 -0500263 * Look for the innermost region that contains the area at `base_va` with size
264 * `size`. Populate *attr with the attributes of this region.
265 *
266 * On success, this function returns 0.
267 * If there are partial overlaps (meaning that a smaller size is needed) or if
268 * the region can't be found in the given area, it returns -1. In this case the
269 * value pointed by attr should be ignored by the caller.
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100270 */
Nishanth Menon6f243462017-05-01 12:26:34 -0500271static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va,
Antonio Nino Diaz8643a812018-06-21 14:39:16 +0100272 size_t size, unsigned int *attr)
Soby Mathew44170c42016-03-22 15:51:08 +0000273{
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100274 /* Don't assume that the area is contained in the first region */
Nishanth Menon6f243462017-05-01 12:26:34 -0500275 int ret = -1;
Soby Mathew44170c42016-03-22 15:51:08 +0000276
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100277 /*
278 * Get attributes from last (innermost) region that contains the
279 * requested area. Don't stop as soon as one region doesn't contain it
280 * because there may be other internal regions that contain this area:
281 *
282 * |-----------------------------1-----------------------------|
283 * |----2----| |-------3-------| |----5----|
284 * |--4--|
285 *
286 * |---| <- Area we want the attributes of.
287 *
288 * In this example, the area is contained in regions 1, 3 and 4 but not
289 * in region 2. The loop shouldn't stop at region 2 as inner regions
290 * have priority over outer regions, it should stop at region 5.
291 */
292 for (;; ++mm) {
Soby Mathew44170c42016-03-22 15:51:08 +0000293
294 if (!mm->size)
Nishanth Menon6f243462017-05-01 12:26:34 -0500295 return ret; /* Reached end of list */
Soby Mathew44170c42016-03-22 15:51:08 +0000296
Soby Mathewfea233c2016-07-13 15:45:15 +0100297 if (mm->base_va > base_va + size - 1)
Nishanth Menon6f243462017-05-01 12:26:34 -0500298 return ret; /* Next region is after area so end */
Soby Mathew44170c42016-03-22 15:51:08 +0000299
Soby Mathewfea233c2016-07-13 15:45:15 +0100300 if (mm->base_va + mm->size - 1 < base_va)
Soby Mathew44170c42016-03-22 15:51:08 +0000301 continue; /* Next region has already been overtaken */
302
Nishanth Menon6f243462017-05-01 12:26:34 -0500303 if (!ret && mm->attr == *attr)
Soby Mathew44170c42016-03-22 15:51:08 +0000304 continue; /* Region doesn't override attribs so skip */
305
Soby Mathew44170c42016-03-22 15:51:08 +0000306 if (mm->base_va > base_va ||
Soby Mathewfea233c2016-07-13 15:45:15 +0100307 mm->base_va + mm->size - 1 < base_va + size - 1)
Soby Mathew44170c42016-03-22 15:51:08 +0000308 return -1; /* Region doesn't fully cover our area */
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100309
Nishanth Menon6f243462017-05-01 12:26:34 -0500310 *attr = mm->attr;
311 ret = 0;
Soby Mathew44170c42016-03-22 15:51:08 +0000312 }
Nishanth Menon6f243462017-05-01 12:26:34 -0500313 return ret;
Soby Mathew44170c42016-03-22 15:51:08 +0000314}
315
316static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm,
317 uintptr_t base_va,
318 uint64_t *table,
David Cunado8a354f12017-06-21 16:52:45 +0100319 unsigned int level)
Soby Mathew44170c42016-03-22 15:51:08 +0000320{
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100321 assert(level >= XLAT_TABLE_LEVEL_MIN && level <= XLAT_TABLE_LEVEL_MAX);
Soby Mathew44170c42016-03-22 15:51:08 +0000322
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100323 unsigned int level_size_shift =
324 L0_XLAT_ADDRESS_SHIFT - level * XLAT_TABLE_ENTRIES_SHIFT;
325 u_register_t level_size = (u_register_t)1 << level_size_shift;
326 u_register_t level_index_mask =
327 ((u_register_t)XLAT_TABLE_ENTRIES_MASK) << level_size_shift;
Soby Mathew44170c42016-03-22 15:51:08 +0000328
329 debug_print("New xlat table:\n");
330
331 do {
332 uint64_t desc = UNSET_DESC;
333
334 if (!mm->size) {
335 /* Done mapping regions; finish zeroing the table */
336 desc = INVALID_DESC;
Soby Mathewfea233c2016-07-13 15:45:15 +0100337 } else if (mm->base_va + mm->size - 1 < base_va) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100338 /* This area is after the region so get next region */
Soby Mathew44170c42016-03-22 15:51:08 +0000339 ++mm;
340 continue;
341 }
342
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100343 debug_print("%s VA:%p size:0x%llx ", get_level_spacer(level),
344 (void *)base_va, (unsigned long long)level_size);
Soby Mathew44170c42016-03-22 15:51:08 +0000345
Soby Mathewfea233c2016-07-13 15:45:15 +0100346 if (mm->base_va > base_va + level_size - 1) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100347 /* Next region is after this area. Nothing to map yet */
Soby Mathew44170c42016-03-22 15:51:08 +0000348 desc = INVALID_DESC;
Antonio Nino Diaz010888d2016-12-13 15:02:31 +0000349 /* Make sure that the current level allows block descriptors */
350 } else if (level >= XLAT_BLOCK_LEVEL_MIN) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100351 /*
352 * Try to get attributes of this area. It will fail if
353 * there are partially overlapping regions. On success,
354 * it will return the innermost region's attributes.
355 */
Antonio Nino Diaz8643a812018-06-21 14:39:16 +0100356 unsigned int attr;
Nishanth Menon6f243462017-05-01 12:26:34 -0500357 int r = mmap_region_attr(mm, base_va, level_size, &attr);
358
359 if (!r) {
Soby Mathew44170c42016-03-22 15:51:08 +0000360 desc = mmap_desc(attr,
361 base_va - mm->base_va + mm->base_pa,
362 level);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100363 }
Soby Mathew44170c42016-03-22 15:51:08 +0000364 }
Soby Mathew44170c42016-03-22 15:51:08 +0000365
366 if (desc == UNSET_DESC) {
367 /* Area not covered by a region so need finer table */
368 uint64_t *new_table = xlat_tables[next_xlat++];
369 assert(next_xlat <= MAX_XLAT_TABLES);
Soby Mathewa0fedc42016-06-16 14:52:04 +0100370 desc = TABLE_DESC | (uintptr_t)new_table;
Soby Mathew44170c42016-03-22 15:51:08 +0000371
372 /* Recurse to fill in new table */
373 mm = init_xlation_table_inner(mm, base_va,
374 new_table, level+1);
375 }
376
377 debug_print("\n");
378
379 *table++ = desc;
380 base_va += level_size;
Antonio Nino Diazd1beee22016-12-13 15:28:54 +0000381 } while ((base_va & level_index_mask) &&
382 (base_va - 1 < PLAT_VIRT_ADDR_SPACE_SIZE - 1));
Soby Mathew44170c42016-03-22 15:51:08 +0000383
384 return mm;
385}
386
387void init_xlation_table(uintptr_t base_va, uint64_t *table,
David Cunado8a354f12017-06-21 16:52:45 +0100388 unsigned int level, uintptr_t *max_va,
Soby Mathew44170c42016-03-22 15:51:08 +0000389 unsigned long long *max_pa)
390{
Antonio Nino Diaz49074492018-04-26 12:59:08 +0100391 int el = xlat_arch_current_el();
392
393 execute_never_mask = xlat_arch_get_xn_desc(el);
394
395 if (el == 3) {
396 ap1_mask = LOWER_ATTRS(AP_ONE_VA_RANGE_RES1);
397 } else {
398 assert(el == 1);
399 ap1_mask = 0;
400 }
401
Soby Mathew44170c42016-03-22 15:51:08 +0000402 init_xlation_table_inner(mmap, base_va, table, level);
403 *max_va = xlat_max_va;
404 *max_pa = xlat_max_pa;
405}