blob: 1f21470ce90b315197002cbf9309662afd55d782 [file] [log] [blame]
Soby Mathew44170c42016-03-22 15:51:08 +00001/*
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +00002 * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
Soby Mathew44170c42016-03-22 15:51:08 +00003 *
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>
Soby Mathew44170c42016-03-22 15:51:08 +000034#include <cassert.h>
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000035#include <common_def.h>
Soby Mathew44170c42016-03-22 15:51:08 +000036#include <debug.h>
37#include <platform_def.h>
38#include <string.h>
Soby Mathewa0fedc42016-06-16 14:52:04 +010039#include <types.h>
Sandrine Bailleux7659a262016-07-05 09:55:03 +010040#include <utils.h>
Soby Mathew44170c42016-03-22 15:51:08 +000041#include <xlat_tables.h>
Antonio Nino Diazd1beee22016-12-13 15:28:54 +000042#include "xlat_tables_private.h"
Soby Mathew44170c42016-03-22 15:51:08 +000043
44#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
45#define LVL0_SPACER ""
46#define LVL1_SPACER " "
47#define LVL2_SPACER " "
48#define LVL3_SPACER " "
49#define get_level_spacer(level) \
50 (((level) == 0) ? LVL0_SPACER : \
51 (((level) == 1) ? LVL1_SPACER : \
52 (((level) == 2) ? LVL2_SPACER : LVL3_SPACER)))
53#define debug_print(...) tf_printf(__VA_ARGS__)
54#else
55#define debug_print(...) ((void)0)
56#endif
57
Soby Mathewa0fedc42016-06-16 14:52:04 +010058#define UNSET_DESC ~0ull
Soby Mathew44170c42016-03-22 15:51:08 +000059
60static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
61 __aligned(XLAT_TABLE_SIZE) __section("xlat_table");
62
63static unsigned next_xlat;
64static unsigned long long xlat_max_pa;
65static uintptr_t xlat_max_va;
66
Antonio Nino Diazefabaa92017-04-27 13:30:22 +010067static uint64_t execute_never_mask;
68
Soby Mathew44170c42016-03-22 15:51:08 +000069/*
70 * Array of all memory regions stored in order of ascending base address.
71 * The list is terminated by the first entry with size == 0.
72 */
73static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
74
75
76void print_mmap(void)
77{
78#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
79 debug_print("mmap:\n");
80 mmap_region_t *mm = mmap;
81 while (mm->size) {
82 debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
83 (void *)mm->base_va, mm->base_pa,
84 mm->size, mm->attr);
85 ++mm;
86 };
87 debug_print("\n");
88#endif
89}
90
91void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
Sandrine Bailleux04980a32017-04-19 14:02:23 +010092 size_t size, mmap_attr_t attr)
Soby Mathew44170c42016-03-22 15:51:08 +000093{
94 mmap_region_t *mm = mmap;
95 mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +010096 unsigned long long end_pa = base_pa + size - 1;
97 uintptr_t end_va = base_va + size - 1;
Soby Mathew44170c42016-03-22 15:51:08 +000098
99 assert(IS_PAGE_ALIGNED(base_pa));
100 assert(IS_PAGE_ALIGNED(base_va));
101 assert(IS_PAGE_ALIGNED(size));
102
103 if (!size)
104 return;
105
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100106 assert(base_pa < end_pa); /* Check for overflows */
107 assert(base_va < end_va);
108
Antonio Nino Diazd1beee22016-12-13 15:28:54 +0000109 assert((base_va + (uintptr_t)size - (uintptr_t)1) <=
110 (PLAT_VIRT_ADDR_SPACE_SIZE - 1));
111 assert((base_pa + (unsigned long long)size - 1ULL) <=
112 (PLAT_PHY_ADDR_SPACE_SIZE - 1));
113
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +0000114#if ENABLE_ASSERTIONS
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100115
116 /* Check for PAs and VAs overlaps with all other regions */
117 for (mm = mmap; mm->size; ++mm) {
118
119 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
120
121 /*
122 * Check if one of the regions is completely inside the other
123 * one.
124 */
125 int fully_overlapped_va =
126 ((base_va >= mm->base_va) && (end_va <= mm_end_va)) ||
127 ((mm->base_va >= base_va) && (mm_end_va <= end_va));
128
129 /*
130 * Full VA overlaps are only allowed if both regions are
131 * identity mapped (zero offset) or have the same VA to PA
132 * offset. Also, make sure that it's not the exact same area.
133 */
134 if (fully_overlapped_va) {
135 assert((mm->base_va - mm->base_pa) ==
136 (base_va - base_pa));
137 assert((base_va != mm->base_va) || (size != mm->size));
138 } else {
139 /*
140 * If the regions do not have fully overlapping VAs,
141 * then they must have fully separated VAs and PAs.
142 * Partial overlaps are not allowed
143 */
144
145 unsigned long long mm_end_pa =
146 mm->base_pa + mm->size - 1;
147
148 int separated_pa =
149 (end_pa < mm->base_pa) || (base_pa > mm_end_pa);
150 int separated_va =
151 (end_va < mm->base_va) || (base_va > mm_end_va);
152
153 assert(separated_va && separated_pa);
154 }
155 }
156
157 mm = mmap; /* Restore pointer to the start of the array */
158
Antonio Nino Diaz3759e3f2017-03-22 15:48:51 +0000159#endif /* ENABLE_ASSERTIONS */
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100160
Soby Mathew44170c42016-03-22 15:51:08 +0000161 /* Find correct place in mmap to insert new region */
162 while (mm->base_va < base_va && mm->size)
163 ++mm;
164
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100165 /*
166 * If a section is contained inside another one with the same base
167 * address, it must be placed after the one it is contained in:
168 *
169 * 1st |-----------------------|
170 * 2nd |------------|
171 * 3rd |------|
172 *
173 * This is required for mmap_region_attr() to get the attributes of the
174 * small region correctly.
175 */
176 while ((mm->base_va == base_va) && (mm->size > size))
177 ++mm;
178
Soby Mathew44170c42016-03-22 15:51:08 +0000179 /* Make room for new region by moving other regions up by one place */
180 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
181
182 /* Check we haven't lost the empty sentinal from the end of the array */
183 assert(mm_last->size == 0);
184
185 mm->base_pa = base_pa;
186 mm->base_va = base_va;
187 mm->size = size;
188 mm->attr = attr;
189
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100190 if (end_pa > xlat_max_pa)
191 xlat_max_pa = end_pa;
192 if (end_va > xlat_max_va)
193 xlat_max_va = end_va;
Soby Mathew44170c42016-03-22 15:51:08 +0000194}
195
196void mmap_add(const mmap_region_t *mm)
197{
198 while (mm->size) {
199 mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
200 ++mm;
201 }
202}
203
Sandrine Bailleux04980a32017-04-19 14:02:23 +0100204static uint64_t mmap_desc(mmap_attr_t attr, unsigned long long addr_pa,
Soby Mathew44170c42016-03-22 15:51:08 +0000205 int level)
206{
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100207 uint64_t desc;
Soby Mathew44170c42016-03-22 15:51:08 +0000208 int mem_type;
209
Antonio Nino Diaz46a33a52016-12-08 16:03:46 +0000210 /* Make sure that the granularity is fine enough to map this address. */
211 assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0);
212
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100213 desc = addr_pa;
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100214 /*
215 * There are different translation table descriptors for level 3 and the
216 * rest.
217 */
218 desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC;
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100219 desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0;
220 desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Soby Mathew44170c42016-03-22 15:51:08 +0000221 desc |= LOWER_ATTRS(ACCESS_FLAG);
222
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100223 /*
224 * Deduce shareability domain and executability of the memory region
225 * from the memory type.
226 *
227 * Data accesses to device memory and non-cacheable normal memory are
228 * coherent for all observers in the system, and correspondingly are
229 * always treated as being Outer Shareable. Therefore, for these 2 types
230 * of memory, it is not strictly needed to set the shareability field
231 * in the translation tables.
232 */
Soby Mathew44170c42016-03-22 15:51:08 +0000233 mem_type = MT_TYPE(attr);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100234 if (mem_type == MT_DEVICE) {
Soby Mathew44170c42016-03-22 15:51:08 +0000235 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100236 /*
237 * Always map device memory as execute-never.
238 * This is to avoid the possibility of a speculative instruction
239 * fetch, which could be an issue if this memory region
240 * corresponds to a read-sensitive peripheral.
241 */
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100242 desc |= execute_never_mask;
243
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100244 } else { /* Normal memory */
245 /*
246 * Always map read-write normal memory as execute-never.
247 * (Trusted Firmware doesn't self-modify its code, therefore
248 * R/W memory is reserved for data storage, which must not be
249 * executable.)
250 * Note that setting the XN bit here is for consistency only.
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100251 * The function that enables the MMU sets the SCTLR_ELx.WXN bit,
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100252 * which makes any writable memory region to be treated as
253 * execute-never, regardless of the value of the XN bit in the
254 * translation table.
Sandrine Bailleuxac3aa682016-06-14 16:31:09 +0100255 *
256 * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER
257 * attribute to figure out the value of the XN bit.
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100258 */
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100259 if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) {
260 desc |= execute_never_mask;
261 }
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100262
263 if (mem_type == MT_MEMORY) {
264 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
265 } else {
266 assert(mem_type == MT_NON_CACHEABLE);
267 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
268 }
Soby Mathew44170c42016-03-22 15:51:08 +0000269 }
270
271 debug_print((mem_type == MT_MEMORY) ? "MEM" :
272 ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV"));
273 debug_print(attr & MT_RW ? "-RW" : "-RO");
274 debug_print(attr & MT_NS ? "-NS" : "-S");
Sandrine Bailleuxac3aa682016-06-14 16:31:09 +0100275 debug_print(attr & MT_EXECUTE_NEVER ? "-XN" : "-EXEC");
Soby Mathew44170c42016-03-22 15:51:08 +0000276 return desc;
277}
278
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100279/*
Nishanth Menon6f243462017-05-01 12:26:34 -0500280 * Look for the innermost region that contains the area at `base_va` with size
281 * `size`. Populate *attr with the attributes of this region.
282 *
283 * On success, this function returns 0.
284 * If there are partial overlaps (meaning that a smaller size is needed) or if
285 * the region can't be found in the given area, it returns -1. In this case the
286 * value pointed by attr should be ignored by the caller.
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100287 */
Nishanth Menon6f243462017-05-01 12:26:34 -0500288static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va,
289 size_t size, mmap_attr_t *attr)
Soby Mathew44170c42016-03-22 15:51:08 +0000290{
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100291 /* Don't assume that the area is contained in the first region */
Nishanth Menon6f243462017-05-01 12:26:34 -0500292 int ret = -1;
Soby Mathew44170c42016-03-22 15:51:08 +0000293
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100294 /*
295 * Get attributes from last (innermost) region that contains the
296 * requested area. Don't stop as soon as one region doesn't contain it
297 * because there may be other internal regions that contain this area:
298 *
299 * |-----------------------------1-----------------------------|
300 * |----2----| |-------3-------| |----5----|
301 * |--4--|
302 *
303 * |---| <- Area we want the attributes of.
304 *
305 * In this example, the area is contained in regions 1, 3 and 4 but not
306 * in region 2. The loop shouldn't stop at region 2 as inner regions
307 * have priority over outer regions, it should stop at region 5.
308 */
309 for (;; ++mm) {
Soby Mathew44170c42016-03-22 15:51:08 +0000310
311 if (!mm->size)
Nishanth Menon6f243462017-05-01 12:26:34 -0500312 return ret; /* Reached end of list */
Soby Mathew44170c42016-03-22 15:51:08 +0000313
Soby Mathewfea233c2016-07-13 15:45:15 +0100314 if (mm->base_va > base_va + size - 1)
Nishanth Menon6f243462017-05-01 12:26:34 -0500315 return ret; /* Next region is after area so end */
Soby Mathew44170c42016-03-22 15:51:08 +0000316
Soby Mathewfea233c2016-07-13 15:45:15 +0100317 if (mm->base_va + mm->size - 1 < base_va)
Soby Mathew44170c42016-03-22 15:51:08 +0000318 continue; /* Next region has already been overtaken */
319
Nishanth Menon6f243462017-05-01 12:26:34 -0500320 if (!ret && mm->attr == *attr)
Soby Mathew44170c42016-03-22 15:51:08 +0000321 continue; /* Region doesn't override attribs so skip */
322
Soby Mathew44170c42016-03-22 15:51:08 +0000323 if (mm->base_va > base_va ||
Soby Mathewfea233c2016-07-13 15:45:15 +0100324 mm->base_va + mm->size - 1 < base_va + size - 1)
Soby Mathew44170c42016-03-22 15:51:08 +0000325 return -1; /* Region doesn't fully cover our area */
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100326
Nishanth Menon6f243462017-05-01 12:26:34 -0500327 *attr = mm->attr;
328 ret = 0;
Soby Mathew44170c42016-03-22 15:51:08 +0000329 }
Nishanth Menon6f243462017-05-01 12:26:34 -0500330 return ret;
Soby Mathew44170c42016-03-22 15:51:08 +0000331}
332
333static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm,
334 uintptr_t base_va,
335 uint64_t *table,
336 int level)
337{
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100338 assert(level >= XLAT_TABLE_LEVEL_MIN && level <= XLAT_TABLE_LEVEL_MAX);
Soby Mathew44170c42016-03-22 15:51:08 +0000339
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100340 unsigned int level_size_shift =
341 L0_XLAT_ADDRESS_SHIFT - level * XLAT_TABLE_ENTRIES_SHIFT;
342 u_register_t level_size = (u_register_t)1 << level_size_shift;
343 u_register_t level_index_mask =
344 ((u_register_t)XLAT_TABLE_ENTRIES_MASK) << level_size_shift;
Soby Mathew44170c42016-03-22 15:51:08 +0000345
346 debug_print("New xlat table:\n");
347
348 do {
349 uint64_t desc = UNSET_DESC;
350
351 if (!mm->size) {
352 /* Done mapping regions; finish zeroing the table */
353 desc = INVALID_DESC;
Soby Mathewfea233c2016-07-13 15:45:15 +0100354 } else if (mm->base_va + mm->size - 1 < base_va) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100355 /* This area is after the region so get next region */
Soby Mathew44170c42016-03-22 15:51:08 +0000356 ++mm;
357 continue;
358 }
359
Antonio Nino Diazd48ae612016-08-02 09:21:41 +0100360 debug_print("%s VA:%p size:0x%llx ", get_level_spacer(level),
361 (void *)base_va, (unsigned long long)level_size);
Soby Mathew44170c42016-03-22 15:51:08 +0000362
Soby Mathewfea233c2016-07-13 15:45:15 +0100363 if (mm->base_va > base_va + level_size - 1) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100364 /* Next region is after this area. Nothing to map yet */
Soby Mathew44170c42016-03-22 15:51:08 +0000365 desc = INVALID_DESC;
Antonio Nino Diaz010888d2016-12-13 15:02:31 +0000366 /* Make sure that the current level allows block descriptors */
367 } else if (level >= XLAT_BLOCK_LEVEL_MIN) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100368 /*
369 * Try to get attributes of this area. It will fail if
370 * there are partially overlapping regions. On success,
371 * it will return the innermost region's attributes.
372 */
Nishanth Menon6f243462017-05-01 12:26:34 -0500373 mmap_attr_t attr;
374 int r = mmap_region_attr(mm, base_va, level_size, &attr);
375
376 if (!r) {
Soby Mathew44170c42016-03-22 15:51:08 +0000377 desc = mmap_desc(attr,
378 base_va - mm->base_va + mm->base_pa,
379 level);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100380 }
Soby Mathew44170c42016-03-22 15:51:08 +0000381 }
Soby Mathew44170c42016-03-22 15:51:08 +0000382
383 if (desc == UNSET_DESC) {
384 /* Area not covered by a region so need finer table */
385 uint64_t *new_table = xlat_tables[next_xlat++];
386 assert(next_xlat <= MAX_XLAT_TABLES);
Soby Mathewa0fedc42016-06-16 14:52:04 +0100387 desc = TABLE_DESC | (uintptr_t)new_table;
Soby Mathew44170c42016-03-22 15:51:08 +0000388
389 /* Recurse to fill in new table */
390 mm = init_xlation_table_inner(mm, base_va,
391 new_table, level+1);
392 }
393
394 debug_print("\n");
395
396 *table++ = desc;
397 base_va += level_size;
Antonio Nino Diazd1beee22016-12-13 15:28:54 +0000398 } while ((base_va & level_index_mask) &&
399 (base_va - 1 < PLAT_VIRT_ADDR_SPACE_SIZE - 1));
Soby Mathew44170c42016-03-22 15:51:08 +0000400
401 return mm;
402}
403
404void init_xlation_table(uintptr_t base_va, uint64_t *table,
405 int level, uintptr_t *max_va,
406 unsigned long long *max_pa)
407{
Antonio Nino Diazefabaa92017-04-27 13:30:22 +0100408 execute_never_mask = xlat_arch_get_xn_desc(xlat_arch_current_el());
Soby Mathew44170c42016-03-22 15:51:08 +0000409 init_xlation_table_inner(mmap, base_va, table, level);
410 *max_va = xlat_max_va;
411 *max_pa = xlat_max_pa;
412}