blob: 54c528cd85519bcc869ddd70e3f6e7ac90dc24c5 [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{
197 uint64_t desc = addr_pa;
198 int mem_type;
199
200 desc |= level == 3 ? TABLE_DESC : BLOCK_DESC;
201
202 desc |= attr & MT_NS ? LOWER_ATTRS(NS) : 0;
203
204 desc |= attr & MT_RW ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO);
205
206 desc |= LOWER_ATTRS(ACCESS_FLAG);
207
208 mem_type = MT_TYPE(attr);
209 if (mem_type == MT_MEMORY) {
210 desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH);
211 if (attr & MT_RW)
212 desc |= UPPER_ATTRS(XN);
213 } else if (mem_type == MT_NON_CACHEABLE) {
214 desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH);
215 if (attr & MT_RW)
216 desc |= UPPER_ATTRS(XN);
217 } else {
218 assert(mem_type == MT_DEVICE);
219 desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH);
220 desc |= UPPER_ATTRS(XN);
221 }
222
223 debug_print((mem_type == MT_MEMORY) ? "MEM" :
224 ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV"));
225 debug_print(attr & MT_RW ? "-RW" : "-RO");
226 debug_print(attr & MT_NS ? "-NS" : "-S");
227
228 return desc;
229}
230
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100231/*
232 * Returns attributes of area at `base_va` with size `size`. It returns the
233 * attributes of the innermost region that contains it. If there are partial
234 * overlaps, it returns -1, as a smaller size is needed.
235 */
Soby Mathew44170c42016-03-22 15:51:08 +0000236static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va,
237 size_t size)
238{
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100239 /* Don't assume that the area is contained in the first region */
240 int attr = -1;
Soby Mathew44170c42016-03-22 15:51:08 +0000241
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100242 /*
243 * Get attributes from last (innermost) region that contains the
244 * requested area. Don't stop as soon as one region doesn't contain it
245 * because there may be other internal regions that contain this area:
246 *
247 * |-----------------------------1-----------------------------|
248 * |----2----| |-------3-------| |----5----|
249 * |--4--|
250 *
251 * |---| <- Area we want the attributes of.
252 *
253 * In this example, the area is contained in regions 1, 3 and 4 but not
254 * in region 2. The loop shouldn't stop at region 2 as inner regions
255 * have priority over outer regions, it should stop at region 5.
256 */
257 for (;; ++mm) {
Soby Mathew44170c42016-03-22 15:51:08 +0000258
259 if (!mm->size)
260 return attr; /* Reached end of list */
261
262 if (mm->base_va >= base_va + size)
263 return attr; /* Next region is after area so end */
264
265 if (mm->base_va + mm->size <= base_va)
266 continue; /* Next region has already been overtaken */
267
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100268 if (mm->attr == attr)
Soby Mathew44170c42016-03-22 15:51:08 +0000269 continue; /* Region doesn't override attribs so skip */
270
Soby Mathew44170c42016-03-22 15:51:08 +0000271 if (mm->base_va > base_va ||
272 mm->base_va + mm->size < base_va + size)
273 return -1; /* Region doesn't fully cover our area */
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100274
275 attr = mm->attr;
Soby Mathew44170c42016-03-22 15:51:08 +0000276 }
277}
278
279static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm,
280 uintptr_t base_va,
281 uint64_t *table,
282 int level)
283{
284 unsigned level_size_shift = L1_XLAT_ADDRESS_SHIFT - (level - 1) *
285 XLAT_TABLE_ENTRIES_SHIFT;
286 unsigned level_size = 1 << level_size_shift;
287 unsigned long long level_index_mask = XLAT_TABLE_ENTRIES_MASK <<
288 level_size_shift;
289
290 assert(level > 0 && level <= 3);
291
292 debug_print("New xlat table:\n");
293
294 do {
295 uint64_t desc = UNSET_DESC;
296
297 if (!mm->size) {
298 /* Done mapping regions; finish zeroing the table */
299 desc = INVALID_DESC;
300 } else if (mm->base_va + mm->size <= base_va) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100301 /* This area is after the region so get next region */
Soby Mathew44170c42016-03-22 15:51:08 +0000302 ++mm;
303 continue;
304 }
305
306 debug_print("%s VA:%p size:0x%x ", get_level_spacer(level),
307 (void *)base_va, level_size);
308
309 if (mm->base_va >= base_va + level_size) {
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100310 /* Next region is after this area. Nothing to map yet */
Soby Mathew44170c42016-03-22 15:51:08 +0000311 desc = INVALID_DESC;
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100312 } else {
313 /*
314 * Try to get attributes of this area. It will fail if
315 * there are partially overlapping regions. On success,
316 * it will return the innermost region's attributes.
317 */
Soby Mathew44170c42016-03-22 15:51:08 +0000318 int attr = mmap_region_attr(mm, base_va, level_size);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100319 if (attr >= 0) {
Soby Mathew44170c42016-03-22 15:51:08 +0000320 desc = mmap_desc(attr,
321 base_va - mm->base_va + mm->base_pa,
322 level);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100323 }
Soby Mathew44170c42016-03-22 15:51:08 +0000324 }
Soby Mathew44170c42016-03-22 15:51:08 +0000325
326 if (desc == UNSET_DESC) {
327 /* Area not covered by a region so need finer table */
328 uint64_t *new_table = xlat_tables[next_xlat++];
329 assert(next_xlat <= MAX_XLAT_TABLES);
Antonio Nino Diazb5e7f772016-03-30 15:45:57 +0100330 desc = TABLE_DESC | (uint64_t)new_table;
Soby Mathew44170c42016-03-22 15:51:08 +0000331
332 /* Recurse to fill in new table */
333 mm = init_xlation_table_inner(mm, base_va,
334 new_table, level+1);
335 }
336
337 debug_print("\n");
338
339 *table++ = desc;
340 base_va += level_size;
341 } while ((base_va & level_index_mask) && (base_va < ADDR_SPACE_SIZE));
342
343 return mm;
344}
345
346void init_xlation_table(uintptr_t base_va, uint64_t *table,
347 int level, uintptr_t *max_va,
348 unsigned long long *max_pa)
349{
350
351 init_xlation_table_inner(mmap, base_va, table, level);
352 *max_va = xlat_max_va;
353 *max_pa = xlat_max_pa;
354}