blob: a84018943d281b0bd48983289525d10f8b587afa [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>
34#include <bl_common.h>
35#include <cassert.h>
36#include <debug.h>
37#include <platform_def.h>
38#include <string.h>
39#include <xlat_tables.h>
40
41#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__)
51#else
52#define debug_print(...) ((void)0)
53#endif
54
55#define UNSET_DESC ~0ul
56
57static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES]
58 __aligned(XLAT_TABLE_SIZE) __section("xlat_table");
59
60static unsigned next_xlat;
61static unsigned long long xlat_max_pa;
62static uintptr_t xlat_max_va;
63
64/*
65 * Array of all memory regions stored in order of ascending base address.
66 * The list is terminated by the first entry with size == 0.
67 */
68static mmap_region_t mmap[MAX_MMAP_REGIONS + 1];
69
70
71void print_mmap(void)
72{
73#if LOG_LEVEL >= LOG_LEVEL_VERBOSE
74 debug_print("mmap:\n");
75 mmap_region_t *mm = mmap;
76 while (mm->size) {
77 debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n",
78 (void *)mm->base_va, mm->base_pa,
79 mm->size, mm->attr);
80 ++mm;
81 };
82 debug_print("\n");
83#endif
84}
85
86void mmap_add_region(unsigned long long base_pa, uintptr_t base_va,
87 size_t size, unsigned int attr)
88{
89 mmap_region_t *mm = mmap;
90 mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1;
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +010091 unsigned long long end_pa = base_pa + size - 1;
92 uintptr_t end_va = base_va + size - 1;
Soby Mathew44170c42016-03-22 15:51:08 +000093
94 assert(IS_PAGE_ALIGNED(base_pa));
95 assert(IS_PAGE_ALIGNED(base_va));
96 assert(IS_PAGE_ALIGNED(size));
97
98 if (!size)
99 return;
100
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100101 assert(base_pa < end_pa); /* Check for overflows */
102 assert(base_va < end_va);
103
104#if DEBUG
105
106 /* Check for PAs and VAs overlaps with all other regions */
107 for (mm = mmap; mm->size; ++mm) {
108
109 uintptr_t mm_end_va = mm->base_va + mm->size - 1;
110
111 /*
112 * Check if one of the regions is completely inside the other
113 * one.
114 */
115 int fully_overlapped_va =
116 ((base_va >= mm->base_va) && (end_va <= mm_end_va)) ||
117 ((mm->base_va >= base_va) && (mm_end_va <= end_va));
118
119 /*
120 * Full VA overlaps are only allowed if both regions are
121 * identity mapped (zero offset) or have the same VA to PA
122 * offset. Also, make sure that it's not the exact same area.
123 */
124 if (fully_overlapped_va) {
125 assert((mm->base_va - mm->base_pa) ==
126 (base_va - base_pa));
127 assert((base_va != mm->base_va) || (size != mm->size));
128 } else {
129 /*
130 * If the regions do not have fully overlapping VAs,
131 * then they must have fully separated VAs and PAs.
132 * Partial overlaps are not allowed
133 */
134
135 unsigned long long mm_end_pa =
136 mm->base_pa + mm->size - 1;
137
138 int separated_pa =
139 (end_pa < mm->base_pa) || (base_pa > mm_end_pa);
140 int separated_va =
141 (end_va < mm->base_va) || (base_va > mm_end_va);
142
143 assert(separated_va && separated_pa);
144 }
145 }
146
147 mm = mmap; /* Restore pointer to the start of the array */
148
149#endif /* DEBUG */
150
Soby Mathew44170c42016-03-22 15:51:08 +0000151 /* Find correct place in mmap to insert new region */
152 while (mm->base_va < base_va && mm->size)
153 ++mm;
154
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100155 /*
156 * If a section is contained inside another one with the same base
157 * address, it must be placed after the one it is contained in:
158 *
159 * 1st |-----------------------|
160 * 2nd |------------|
161 * 3rd |------|
162 *
163 * This is required for mmap_region_attr() to get the attributes of the
164 * small region correctly.
165 */
166 while ((mm->base_va == base_va) && (mm->size > size))
167 ++mm;
168
Soby Mathew44170c42016-03-22 15:51:08 +0000169 /* Make room for new region by moving other regions up by one place */
170 memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm);
171
172 /* Check we haven't lost the empty sentinal from the end of the array */
173 assert(mm_last->size == 0);
174
175 mm->base_pa = base_pa;
176 mm->base_va = base_va;
177 mm->size = size;
178 mm->attr = attr;
179
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100180 if (end_pa > xlat_max_pa)
181 xlat_max_pa = end_pa;
182 if (end_va > xlat_max_va)
183 xlat_max_va = end_va;
Soby Mathew44170c42016-03-22 15:51:08 +0000184}
185
186void mmap_add(const mmap_region_t *mm)
187{
188 while (mm->size) {
189 mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr);
190 ++mm;
191 }
192}
193
194static uint64_t mmap_desc(unsigned attr, unsigned long long addr_pa,
195 int level)
196{
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100197 uint64_t desc;
Soby Mathew44170c42016-03-22 15:51:08 +0000198 int mem_type;
199
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100200 desc = addr_pa;
201 desc |= (level == 3) ? TABLE_DESC : BLOCK_DESC;
202 desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0;
203 desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
Soby Mathew44170c42016-03-22 15:51:08 +0000204 desc |= LOWER_ATTRS(ACCESS_FLAG);
205
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100206 /*
207 * Deduce shareability domain and executability of the memory region
208 * from the memory type.
209 *
210 * Data accesses to device memory and non-cacheable normal memory are
211 * coherent for all observers in the system, and correspondingly are
212 * always treated as being Outer Shareable. Therefore, for these 2 types
213 * of memory, it is not strictly needed to set the shareability field
214 * in the translation tables.
215 */
Soby Mathew44170c42016-03-22 15:51:08 +0000216 mem_type = MT_TYPE(attr);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100217 if (mem_type == MT_DEVICE) {
Soby Mathew44170c42016-03-22 15:51:08 +0000218 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100219 /*
220 * Always map device memory as execute-never.
221 * This is to avoid the possibility of a speculative instruction
222 * fetch, which could be an issue if this memory region
223 * corresponds to a read-sensitive peripheral.
224 */
Soby Mathew44170c42016-03-22 15:51:08 +0000225 desc |= UPPER_ATTRS(XN);
Sandrine Bailleux7528b682016-06-14 16:29:04 +0100226 } else { /* Normal memory */
227 /*
228 * Always map read-write normal memory as execute-never.
229 * (Trusted Firmware doesn't self-modify its code, therefore
230 * R/W memory is reserved for data storage, which must not be
231 * executable.)
232 * Note that setting the XN bit here is for consistency only.
233 * The enable_mmu_elx() function sets the SCTLR_EL3.WXN bit,
234 * which makes any writable memory region to be treated as
235 * execute-never, regardless of the value of the XN bit in the
236 * translation table.
237 */
238 if (attr & MT_RW)
239 desc |= UPPER_ATTRS(XN);
240
241 if (mem_type == MT_MEMORY) {
242 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
243 } else {
244 assert(mem_type == MT_NON_CACHEABLE);
245 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
246 }
Soby Mathew44170c42016-03-22 15:51:08 +0000247 }
248
249 debug_print((mem_type == MT_MEMORY) ? "MEM" :
250 ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV"));
251 debug_print(attr & MT_RW ? "-RW" : "-RO");
252 debug_print(attr & MT_NS ? "-NS" : "-S");
253
254 return desc;
255}
256
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100257/*
258 * Returns attributes of area at `base_va` with size `size`. It returns the
259 * attributes of the innermost region that contains it. If there are partial
260 * overlaps, it returns -1, as a smaller size is needed.
261 */
Soby Mathew44170c42016-03-22 15:51:08 +0000262static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va,
263 size_t size)
264{
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100265 /* Don't assume that the area is contained in the first region */
266 int attr = -1;
Soby Mathew44170c42016-03-22 15:51:08 +0000267
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100268 /*
269 * Get attributes from last (innermost) region that contains the
270 * requested area. Don't stop as soon as one region doesn't contain it
271 * because there may be other internal regions that contain this area:
272 *
273 * |-----------------------------1-----------------------------|
274 * |----2----| |-------3-------| |----5----|
275 * |--4--|
276 *
277 * |---| <- Area we want the attributes of.
278 *
279 * In this example, the area is contained in regions 1, 3 and 4 but not
280 * in region 2. The loop shouldn't stop at region 2 as inner regions
281 * have priority over outer regions, it should stop at region 5.
282 */
283 for (;; ++mm) {
Soby Mathew44170c42016-03-22 15:51:08 +0000284
285 if (!mm->size)
286 return attr; /* Reached end of list */
287
288 if (mm->base_va >= base_va + size)
289 return attr; /* Next region is after area so end */
290
291 if (mm->base_va + mm->size <= base_va)
292 continue; /* Next region has already been overtaken */
293
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100294 if (mm->attr == attr)
Soby Mathew44170c42016-03-22 15:51:08 +0000295 continue; /* Region doesn't override attribs so skip */
296
Soby Mathew44170c42016-03-22 15:51:08 +0000297 if (mm->base_va > base_va ||
298 mm->base_va + mm->size < base_va + size)
299 return -1; /* Region doesn't fully cover our area */
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100300
301 attr = mm->attr;
Soby Mathew44170c42016-03-22 15:51:08 +0000302 }
303}
304
305static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm,
306 uintptr_t base_va,
307 uint64_t *table,
308 int level)
309{
310 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
311 XLAT_TABLE_ENTRIES_SHIFT;
312 unsigned level_size = 1 << level_size_shift;
Sandrine Bailleux4ba6b0b2016-04-22 10:47:33 +0100313 unsigned long long level_index_mask =
314 ((unsigned long long) XLAT_TABLE_ENTRIES_MASK)
315 << level_size_shift;
Soby Mathew44170c42016-03-22 15:51:08 +0000316
317 assert(level > 0 && level <= 3);
318
319 debug_print("New xlat table:\n");
320
321 do {
322 uint64_t desc = UNSET_DESC;
323
324 if (!mm->size) {
325 /* Done mapping regions; finish zeroing the table */
326 desc = INVALID_DESC;
327 } else if (mm->base_va + mm->size <= base_va) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100328 /* This area is after the region so get next region */
Soby Mathew44170c42016-03-22 15:51:08 +0000329 ++mm;
330 continue;
331 }
332
333 debug_print("%s VA:%p size:0x%x ", get_level_spacer(level),
334 (void *)base_va, level_size);
335
336 if (mm->base_va >= base_va + level_size) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100337 /* Next region is after this area. Nothing to map yet */
Soby Mathew44170c42016-03-22 15:51:08 +0000338 desc = INVALID_DESC;
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100339 } else {
340 /*
341 * Try to get attributes of this area. It will fail if
342 * there are partially overlapping regions. On success,
343 * it will return the innermost region's attributes.
344 */
Soby Mathew44170c42016-03-22 15:51:08 +0000345 int attr = mmap_region_attr(mm, base_va, level_size);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100346 if (attr >= 0) {
Soby Mathew44170c42016-03-22 15:51:08 +0000347 desc = mmap_desc(attr,
348 base_va - mm->base_va + mm->base_pa,
349 level);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100350 }
Soby Mathew44170c42016-03-22 15:51:08 +0000351 }
Soby Mathew44170c42016-03-22 15:51:08 +0000352
353 if (desc == UNSET_DESC) {
354 /* Area not covered by a region so need finer table */
355 uint64_t *new_table = xlat_tables[next_xlat++];
356 assert(next_xlat <= MAX_XLAT_TABLES);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100357 desc = TABLE_DESC | (uint64_t)new_table;
Soby Mathew44170c42016-03-22 15:51:08 +0000358
359 /* Recurse to fill in new table */
360 mm = init_xlation_table_inner(mm, base_va,
361 new_table, level+1);
362 }
363
364 debug_print("\n");
365
366 *table++ = desc;
367 base_va += level_size;
368 } while ((base_va & level_index_mask) && (base_va < ADDR_SPACE_SIZE));
369
370 return mm;
371}
372
373void init_xlation_table(uintptr_t base_va, uint64_t *table,
374 int level, uintptr_t *max_va,
375 unsigned long long *max_pa)
376{
377
378 init_xlation_table_inner(mmap, base_va, table, level);
379 *max_va = xlat_max_va;
380 *max_pa = xlat_max_pa;
381}