blob: 33784c2dbc43a7ed88be0f00c6561e9f6482dd9f [file] [log] [blame]
Soby Mathew44170c42016-03-22 15:51:08 +00001/*
2 * Copyright (c) 2016, 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>
Soby Mathew44170c42016-03-22 15:51:08 +000034#include <cassert.h>
35#include <debug.h>
36#include <platform_def.h>
37#include <string.h>
Soby Mathewa0fedc42016-06-16 14:52:04 +010038#include <types.h>
Sandrine Bailleux7659a262016-07-05 09:55:03 +010039#include <utils.h>
Soby Mathew44170c42016-03-22 15:51:08 +000040#include <xlat_tables.h>
41
42#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
43#define LVL0_SPACER ""
44#define LVL1_SPACER " "
45#define LVL2_SPACER " "
46#define LVL3_SPACER " "
47#define get_level_spacer(level) \
48 (((level) == 0) ? LVL0_SPACER : \
49 (((level) == 1) ? LVL1_SPACER : \
50 (((level) == 2) ? LVL2_SPACER : LVL3_SPACER)))
51#define debug_print(...) tf_printf(__VA_ARGS__)
52#else
53#define debug_print(...) ((void)0)
54#endif
55
Soby Mathewa0fedc42016-06-16 14:52:04 +010056#define UNSET_DESC ~0ull
Soby Mathew44170c42016-03-22 15:51:08 +000057
58static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
59 __aligned(XLAT_TABLE_SIZE) __section("xlat_table");
60
61static unsigned next_xlat;
62static unsigned long long xlat_max_pa;
63static uintptr_t xlat_max_va;
64
65/*
66 * Array of all memory regions stored in order of ascending base address.
67 * The list is terminated by the first entry with size == 0.
68 */
69static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
70
71
72void print_mmap(void)
73{
74#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
75 debug_print("mmap:\n");
76 mmap_region_t *mm = mmap;
77 while (mm->size) {
78 debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
79 (void *)mm->base_va, mm->base_pa,
80 mm->size, mm->attr);
81 ++mm;
82 };
83 debug_print("\n");
84#endif
85}
86
87void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
88 size_t size, unsigned int attr)
89{
90 mmap_region_t *mm = mmap;
91 mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +010092 unsigned long long end_pa = base_pa + size - 1;
93 uintptr_t end_va = base_va + size - 1;
Soby Mathew44170c42016-03-22 15:51:08 +000094
95 assert(IS_PAGE_ALIGNED(base_pa));
96 assert(IS_PAGE_ALIGNED(base_va));
97 assert(IS_PAGE_ALIGNED(size));
98
99 if (!size)
100 return;
101
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100102 assert(base_pa < end_pa); /* Check for overflows */
103 assert(base_va < end_va);
104
105#if DEBUG
106
107 /* Check for PAs and VAs overlaps with all other regions */
108 for (mm = mmap; mm->size; ++mm) {
109
110 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
111
112 /*
113 * Check if one of the regions is completely inside the other
114 * one.
115 */
116 int fully_overlapped_va =
117 ((base_va >= mm->base_va) && (end_va <= mm_end_va)) ||
118 ((mm->base_va >= base_va) && (mm_end_va <= end_va));
119
120 /*
121 * Full VA overlaps are only allowed if both regions are
122 * identity mapped (zero offset) or have the same VA to PA
123 * offset. Also, make sure that it's not the exact same area.
124 */
125 if (fully_overlapped_va) {
126 assert((mm->base_va - mm->base_pa) ==
127 (base_va - base_pa));
128 assert((base_va != mm->base_va) || (size != mm->size));
129 } else {
130 /*
131 * If the regions do not have fully overlapping VAs,
132 * then they must have fully separated VAs and PAs.
133 * Partial overlaps are not allowed
134 */
135
136 unsigned long long mm_end_pa =
137 mm->base_pa + mm->size - 1;
138
139 int separated_pa =
140 (end_pa < mm->base_pa) || (base_pa > mm_end_pa);
141 int separated_va =
142 (end_va < mm->base_va) || (base_va > mm_end_va);
143
144 assert(separated_va && separated_pa);
145 }
146 }
147
148 mm = mmap; /* Restore pointer to the start of the array */
149
150#endif /* DEBUG */
151
Soby Mathew44170c42016-03-22 15:51:08 +0000152 /* Find correct place in mmap to insert new region */
153 while (mm->base_va < base_va && mm->size)
154 ++mm;
155
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100156 /*
157 * If a section is contained inside another one with the same base
158 * address, it must be placed after the one it is contained in:
159 *
160 * 1st |-----------------------|
161 * 2nd |------------|
162 * 3rd |------|
163 *
164 * This is required for mmap_region_attr() to get the attributes of the
165 * small region correctly.
166 */
167 while ((mm->base_va == base_va) && (mm->size > size))
168 ++mm;
169
Soby Mathew44170c42016-03-22 15:51:08 +0000170 /* Make room for new region by moving other regions up by one place */
171 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
172
173 /* Check we haven't lost the empty sentinal from the end of the array */
174 assert(mm_last->size == 0);
175
176 mm->base_pa = base_pa;
177 mm->base_va = base_va;
178 mm->size = size;
179 mm->attr = attr;
180
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100181 if (end_pa > xlat_max_pa)
182 xlat_max_pa = end_pa;
183 if (end_va > xlat_max_va)
184 xlat_max_va = end_va;
Soby Mathew44170c42016-03-22 15:51:08 +0000185}
186
187void mmap_add(const mmap_region_t *mm)
188{
189 while (mm->size) {
190 mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
191 ++mm;
192 }
193}
194
195static uint64_t mmap_desc(unsigned attr, unsigned long long addr_pa,
196 int level)
197{
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100198 uint64_t desc;
Soby Mathew44170c42016-03-22 15:51:08 +0000199 int mem_type;
200
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100201 desc = addr_pa;
202 desc |= (level == 3) ? TABLE_DESC : BLOCK_DESC;
203 desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0;
204 desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Soby Mathew44170c42016-03-22 15:51:08 +0000205 desc |= LOWER_ATTRS(ACCESS_FLAG);
206
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100207 /*
208 * Deduce shareability domain and executability of the memory region
209 * from the memory type.
210 *
211 * Data accesses to device memory and non-cacheable normal memory are
212 * coherent for all observers in the system, and correspondingly are
213 * always treated as being Outer Shareable. Therefore, for these 2 types
214 * of memory, it is not strictly needed to set the shareability field
215 * in the translation tables.
216 */
Soby Mathew44170c42016-03-22 15:51:08 +0000217 mem_type = MT_TYPE(attr);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100218 if (mem_type == MT_DEVICE) {
Soby Mathew44170c42016-03-22 15:51:08 +0000219 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100220 /*
221 * Always map device memory as execute-never.
222 * This is to avoid the possibility of a speculative instruction
223 * fetch, which could be an issue if this memory region
224 * corresponds to a read-sensitive peripheral.
225 */
Soby Mathew44170c42016-03-22 15:51:08 +0000226 desc |= UPPER_ATTRS(XN);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100227 } else { /* Normal memory */
228 /*
229 * Always map read-write normal memory as execute-never.
230 * (Trusted Firmware doesn't self-modify its code, therefore
231 * R/W memory is reserved for data storage, which must not be
232 * executable.)
233 * Note that setting the XN bit here is for consistency only.
234 * The enable_mmu_elx() function sets the SCTLR_EL3.WXN bit,
235 * 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 */
Sandrine Bailleuxac3aa682016-06-14 16:31:09 +0100242 if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER))
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100243 desc |= UPPER_ATTRS(XN);
244
245 if (mem_type == MT_MEMORY) {
246 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
247 } else {
248 assert(mem_type == MT_NON_CACHEABLE);
249 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
250 }
Soby Mathew44170c42016-03-22 15:51:08 +0000251 }
252
253 debug_print((mem_type == MT_MEMORY) ? "MEM" :
254 ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV"));
255 debug_print(attr & MT_RW ? "-RW" : "-RO");
256 debug_print(attr & MT_NS ? "-NS" : "-S");
Sandrine Bailleuxac3aa682016-06-14 16:31:09 +0100257 debug_print(attr & MT_EXECUTE_NEVER ? "-XN" : "-EXEC");
Soby Mathew44170c42016-03-22 15:51:08 +0000258 return desc;
259}
260
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100261/*
262 * Returns attributes of area at `base_va` with size `size`. It returns the
263 * attributes of the innermost region that contains it. If there are partial
264 * overlaps, it returns -1, as a smaller size is needed.
265 */
Soby Mathew44170c42016-03-22 15:51:08 +0000266static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va,
267 size_t size)
268{
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100269 /* Don't assume that the area is contained in the first region */
270 int attr = -1;
Soby Mathew44170c42016-03-22 15:51:08 +0000271
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100272 /*
273 * Get attributes from last (innermost) region that contains the
274 * requested area. Don't stop as soon as one region doesn't contain it
275 * because there may be other internal regions that contain this area:
276 *
277 * |-----------------------------1-----------------------------|
278 * |----2----| |-------3-------| |----5----|
279 * |--4--|
280 *
281 * |---| <- Area we want the attributes of.
282 *
283 * In this example, the area is contained in regions 1, 3 and 4 but not
284 * in region 2. The loop shouldn't stop at region 2 as inner regions
285 * have priority over outer regions, it should stop at region 5.
286 */
287 for (;; ++mm) {
Soby Mathew44170c42016-03-22 15:51:08 +0000288
289 if (!mm->size)
290 return attr; /* Reached end of list */
291
292 if (mm->base_va >= base_va + size)
293 return attr; /* Next region is after area so end */
294
295 if (mm->base_va + mm->size <= base_va)
296 continue; /* Next region has already been overtaken */
297
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100298 if (mm->attr == attr)
Soby Mathew44170c42016-03-22 15:51:08 +0000299 continue; /* Region doesn't override attribs so skip */
300
Soby Mathew44170c42016-03-22 15:51:08 +0000301 if (mm->base_va > base_va ||
302 mm->base_va + mm->size < base_va + size)
303 return -1; /* Region doesn't fully cover our area */
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100304
305 attr = mm->attr;
Soby Mathew44170c42016-03-22 15:51:08 +0000306 }
307}
308
309static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm,
310 uintptr_t base_va,
311 uint64_t *table,
312 int level)
313{
314 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
315 XLAT_TABLE_ENTRIES_SHIFT;
316 unsigned level_size = 1 << level_size_shift;
Soby Mathewa0fedc42016-06-16 14:52:04 +0100317 u_register_t level_index_mask =
318 (u_register_t)(((u_register_t) XLAT_TABLE_ENTRIES_MASK)
319 << level_size_shift);
Soby Mathew44170c42016-03-22 15:51:08 +0000320
321 assert(level > 0 && level <= 3);
322
323 debug_print("New xlat table:\n");
324
325 do {
326 uint64_t desc = UNSET_DESC;
327
328 if (!mm->size) {
329 /* Done mapping regions; finish zeroing the table */
330 desc = INVALID_DESC;
331 } else if (mm->base_va + mm->size <= base_va) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100332 /* This area is after the region so get next region */
Soby Mathew44170c42016-03-22 15:51:08 +0000333 ++mm;
334 continue;
335 }
336
337 debug_print("%s VA:%p size:0x%x ", get_level_spacer(level),
338 (void *)base_va, level_size);
339
340 if (mm->base_va >= base_va + level_size) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100341 /* Next region is after this area. Nothing to map yet */
Soby Mathew44170c42016-03-22 15:51:08 +0000342 desc = INVALID_DESC;
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100343 } else {
344 /*
345 * Try to get attributes of this area. It will fail if
346 * there are partially overlapping regions. On success,
347 * it will return the innermost region's attributes.
348 */
Soby Mathew44170c42016-03-22 15:51:08 +0000349 int attr = mmap_region_attr(mm, base_va, level_size);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100350 if (attr >= 0) {
Soby Mathew44170c42016-03-22 15:51:08 +0000351 desc = mmap_desc(attr,
352 base_va - mm->base_va + mm->base_pa,
353 level);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100354 }
Soby Mathew44170c42016-03-22 15:51:08 +0000355 }
Soby Mathew44170c42016-03-22 15:51:08 +0000356
357 if (desc == UNSET_DESC) {
358 /* Area not covered by a region so need finer table */
359 uint64_t *new_table = xlat_tables[next_xlat++];
360 assert(next_xlat <= MAX_XLAT_TABLES);
Soby Mathewa0fedc42016-06-16 14:52:04 +0100361 desc = TABLE_DESC | (uintptr_t)new_table;
Soby Mathew44170c42016-03-22 15:51:08 +0000362
363 /* Recurse to fill in new table */
364 mm = init_xlation_table_inner(mm, base_va,
365 new_table, level+1);
366 }
367
368 debug_print("\n");
369
370 *table++ = desc;
371 base_va += level_size;
372 } while ((base_va & level_index_mask) && (base_va < ADDR_SPACE_SIZE));
373
374 return mm;
375}
376
377void init_xlation_table(uintptr_t base_va, uint64_t *table,
378 int level, uintptr_t *max_va,
379 unsigned long long *max_pa)
380{
381
382 init_xlation_table_inner(mmap, base_va, table, level);
383 *max_va = xlat_max_va;
384 *max_pa = xlat_max_pa;
385}