Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 1 | /* |
| 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 Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 34 | #include <cassert.h> |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 35 | #include <common_def.h> |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 36 | #include <debug.h> |
| 37 | #include <platform_def.h> |
| 38 | #include <string.h> |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 39 | #include <types.h> |
Sandrine Bailleux | 7659a26 | 2016-07-05 09:55:03 +0100 | [diff] [blame] | 40 | #include <utils.h> |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 41 | #include <xlat_tables.h> |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 42 | #include "xlat_tables_private.h" |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 43 | |
| 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 Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 58 | #define UNSET_DESC ~0ull |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 59 | |
| 60 | static uint64_t xlat_tables[MAX_XLAT_TABLES][XLAT_TABLE_ENTRIES] |
| 61 | __aligned(XLAT_TABLE_SIZE) __section("xlat_table"); |
| 62 | |
| 63 | static unsigned next_xlat; |
| 64 | static unsigned long long xlat_max_pa; |
| 65 | static uintptr_t xlat_max_va; |
| 66 | |
| 67 | /* |
| 68 | * Array of all memory regions stored in order of ascending base address. |
| 69 | * The list is terminated by the first entry with size == 0. |
| 70 | */ |
| 71 | static mmap_region_t mmap[MAX_MMAP_REGIONS + 1]; |
| 72 | |
| 73 | |
| 74 | void print_mmap(void) |
| 75 | { |
| 76 | #if LOG_LEVEL >= LOG_LEVEL_VERBOSE |
| 77 | debug_print("mmap:\n"); |
| 78 | mmap_region_t *mm = mmap; |
| 79 | while (mm->size) { |
| 80 | debug_print(" VA:%p PA:0x%llx size:0x%zx attr:0x%x\n", |
| 81 | (void *)mm->base_va, mm->base_pa, |
| 82 | mm->size, mm->attr); |
| 83 | ++mm; |
| 84 | }; |
| 85 | debug_print("\n"); |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, |
| 90 | size_t size, unsigned int attr) |
| 91 | { |
| 92 | mmap_region_t *mm = mmap; |
| 93 | mmap_region_t *mm_last = mm + ARRAY_SIZE(mmap) - 1; |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 94 | unsigned long long end_pa = base_pa + size - 1; |
| 95 | uintptr_t end_va = base_va + size - 1; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 96 | |
| 97 | assert(IS_PAGE_ALIGNED(base_pa)); |
| 98 | assert(IS_PAGE_ALIGNED(base_va)); |
| 99 | assert(IS_PAGE_ALIGNED(size)); |
| 100 | |
| 101 | if (!size) |
| 102 | return; |
| 103 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 104 | assert(base_pa < end_pa); /* Check for overflows */ |
| 105 | assert(base_va < end_va); |
| 106 | |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 107 | assert((base_va + (uintptr_t)size - (uintptr_t)1) <= |
| 108 | (PLAT_VIRT_ADDR_SPACE_SIZE - 1)); |
| 109 | assert((base_pa + (unsigned long long)size - 1ULL) <= |
| 110 | (PLAT_PHY_ADDR_SPACE_SIZE - 1)); |
| 111 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 112 | #if DEBUG |
| 113 | |
| 114 | /* Check for PAs and VAs overlaps with all other regions */ |
| 115 | for (mm = mmap; mm->size; ++mm) { |
| 116 | |
| 117 | uintptr_t mm_end_va = mm->base_va + mm->size - 1; |
| 118 | |
| 119 | /* |
| 120 | * Check if one of the regions is completely inside the other |
| 121 | * one. |
| 122 | */ |
| 123 | int fully_overlapped_va = |
| 124 | ((base_va >= mm->base_va) && (end_va <= mm_end_va)) || |
| 125 | ((mm->base_va >= base_va) && (mm_end_va <= end_va)); |
| 126 | |
| 127 | /* |
| 128 | * Full VA overlaps are only allowed if both regions are |
| 129 | * identity mapped (zero offset) or have the same VA to PA |
| 130 | * offset. Also, make sure that it's not the exact same area. |
| 131 | */ |
| 132 | if (fully_overlapped_va) { |
| 133 | assert((mm->base_va - mm->base_pa) == |
| 134 | (base_va - base_pa)); |
| 135 | assert((base_va != mm->base_va) || (size != mm->size)); |
| 136 | } else { |
| 137 | /* |
| 138 | * If the regions do not have fully overlapping VAs, |
| 139 | * then they must have fully separated VAs and PAs. |
| 140 | * Partial overlaps are not allowed |
| 141 | */ |
| 142 | |
| 143 | unsigned long long mm_end_pa = |
| 144 | mm->base_pa + mm->size - 1; |
| 145 | |
| 146 | int separated_pa = |
| 147 | (end_pa < mm->base_pa) || (base_pa > mm_end_pa); |
| 148 | int separated_va = |
| 149 | (end_va < mm->base_va) || (base_va > mm_end_va); |
| 150 | |
| 151 | assert(separated_va && separated_pa); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | mm = mmap; /* Restore pointer to the start of the array */ |
| 156 | |
| 157 | #endif /* DEBUG */ |
| 158 | |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 159 | /* Find correct place in mmap to insert new region */ |
| 160 | while (mm->base_va < base_va && mm->size) |
| 161 | ++mm; |
| 162 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 163 | /* |
| 164 | * If a section is contained inside another one with the same base |
| 165 | * address, it must be placed after the one it is contained in: |
| 166 | * |
| 167 | * 1st |-----------------------| |
| 168 | * 2nd |------------| |
| 169 | * 3rd |------| |
| 170 | * |
| 171 | * This is required for mmap_region_attr() to get the attributes of the |
| 172 | * small region correctly. |
| 173 | */ |
| 174 | while ((mm->base_va == base_va) && (mm->size > size)) |
| 175 | ++mm; |
| 176 | |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 177 | /* Make room for new region by moving other regions up by one place */ |
| 178 | memmove(mm + 1, mm, (uintptr_t)mm_last - (uintptr_t)mm); |
| 179 | |
| 180 | /* Check we haven't lost the empty sentinal from the end of the array */ |
| 181 | assert(mm_last->size == 0); |
| 182 | |
| 183 | mm->base_pa = base_pa; |
| 184 | mm->base_va = base_va; |
| 185 | mm->size = size; |
| 186 | mm->attr = attr; |
| 187 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 188 | if (end_pa > xlat_max_pa) |
| 189 | xlat_max_pa = end_pa; |
| 190 | if (end_va > xlat_max_va) |
| 191 | xlat_max_va = end_va; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void mmap_add(const mmap_region_t *mm) |
| 195 | { |
| 196 | while (mm->size) { |
| 197 | mmap_add_region(mm->base_pa, mm->base_va, mm->size, mm->attr); |
| 198 | ++mm; |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | static uint64_t mmap_desc(unsigned attr, unsigned long long addr_pa, |
| 203 | int level) |
| 204 | { |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 205 | uint64_t desc; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 206 | int mem_type; |
| 207 | |
Antonio Nino Diaz | 46a33a5 | 2016-12-08 16:03:46 +0000 | [diff] [blame] | 208 | /* Make sure that the granularity is fine enough to map this address. */ |
| 209 | assert((addr_pa & XLAT_BLOCK_MASK(level)) == 0); |
| 210 | |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 211 | desc = addr_pa; |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 212 | /* |
| 213 | * There are different translation table descriptors for level 3 and the |
| 214 | * rest. |
| 215 | */ |
| 216 | desc |= (level == XLAT_TABLE_LEVEL_MAX) ? PAGE_DESC : BLOCK_DESC; |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 217 | desc |= (attr & MT_NS) ? LOWER_ATTRS(NS) : 0; |
| 218 | desc |= (attr & MT_RW) ? LOWER_ATTRS(AP_RW) : LOWER_ATTRS(AP_RO); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 219 | desc |= LOWER_ATTRS(ACCESS_FLAG); |
| 220 | |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 221 | /* |
| 222 | * Deduce shareability domain and executability of the memory region |
| 223 | * from the memory type. |
| 224 | * |
| 225 | * Data accesses to device memory and non-cacheable normal memory are |
| 226 | * coherent for all observers in the system, and correspondingly are |
| 227 | * always treated as being Outer Shareable. Therefore, for these 2 types |
| 228 | * of memory, it is not strictly needed to set the shareability field |
| 229 | * in the translation tables. |
| 230 | */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 231 | mem_type = MT_TYPE(attr); |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 232 | if (mem_type == MT_DEVICE) { |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 233 | desc |= LOWER_ATTRS(ATTR_DEVICE_INDEX | OSH); |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 234 | /* |
| 235 | * Always map device memory as execute-never. |
| 236 | * This is to avoid the possibility of a speculative instruction |
| 237 | * fetch, which could be an issue if this memory region |
| 238 | * corresponds to a read-sensitive peripheral. |
| 239 | */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 240 | desc |= UPPER_ATTRS(XN); |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 241 | } else { /* Normal memory */ |
| 242 | /* |
| 243 | * Always map read-write normal memory as execute-never. |
| 244 | * (Trusted Firmware doesn't self-modify its code, therefore |
| 245 | * R/W memory is reserved for data storage, which must not be |
| 246 | * executable.) |
| 247 | * Note that setting the XN bit here is for consistency only. |
| 248 | * The enable_mmu_elx() function sets the SCTLR_EL3.WXN bit, |
| 249 | * which makes any writable memory region to be treated as |
| 250 | * execute-never, regardless of the value of the XN bit in the |
| 251 | * translation table. |
Sandrine Bailleux | ac3aa68 | 2016-06-14 16:31:09 +0100 | [diff] [blame] | 252 | * |
| 253 | * For read-only memory, rely on the MT_EXECUTE/MT_EXECUTE_NEVER |
| 254 | * attribute to figure out the value of the XN bit. |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 255 | */ |
Sandrine Bailleux | ac3aa68 | 2016-06-14 16:31:09 +0100 | [diff] [blame] | 256 | if ((attr & MT_RW) || (attr & MT_EXECUTE_NEVER)) |
Sandrine Bailleux | 7528b68 | 2016-06-14 16:29:04 +0100 | [diff] [blame] | 257 | desc |= UPPER_ATTRS(XN); |
| 258 | |
| 259 | if (mem_type == MT_MEMORY) { |
| 260 | desc |= LOWER_ATTRS(ATTR_IWBWA_OWBWA_NTR_INDEX | ISH); |
| 261 | } else { |
| 262 | assert(mem_type == MT_NON_CACHEABLE); |
| 263 | desc |= LOWER_ATTRS(ATTR_NON_CACHEABLE_INDEX | OSH); |
| 264 | } |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | debug_print((mem_type == MT_MEMORY) ? "MEM" : |
| 268 | ((mem_type == MT_NON_CACHEABLE) ? "NC" : "DEV")); |
| 269 | debug_print(attr & MT_RW ? "-RW" : "-RO"); |
| 270 | debug_print(attr & MT_NS ? "-NS" : "-S"); |
Sandrine Bailleux | ac3aa68 | 2016-06-14 16:31:09 +0100 | [diff] [blame] | 271 | debug_print(attr & MT_EXECUTE_NEVER ? "-XN" : "-EXEC"); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 272 | return desc; |
| 273 | } |
| 274 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 275 | /* |
| 276 | * Returns attributes of area at `base_va` with size `size`. It returns the |
| 277 | * attributes of the innermost region that contains it. If there are partial |
| 278 | * overlaps, it returns -1, as a smaller size is needed. |
| 279 | */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 280 | static int mmap_region_attr(mmap_region_t *mm, uintptr_t base_va, |
| 281 | size_t size) |
| 282 | { |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 283 | /* Don't assume that the area is contained in the first region */ |
| 284 | int attr = -1; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 285 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 286 | /* |
| 287 | * Get attributes from last (innermost) region that contains the |
| 288 | * requested area. Don't stop as soon as one region doesn't contain it |
| 289 | * because there may be other internal regions that contain this area: |
| 290 | * |
| 291 | * |-----------------------------1-----------------------------| |
| 292 | * |----2----| |-------3-------| |----5----| |
| 293 | * |--4--| |
| 294 | * |
| 295 | * |---| <- Area we want the attributes of. |
| 296 | * |
| 297 | * In this example, the area is contained in regions 1, 3 and 4 but not |
| 298 | * in region 2. The loop shouldn't stop at region 2 as inner regions |
| 299 | * have priority over outer regions, it should stop at region 5. |
| 300 | */ |
| 301 | for (;; ++mm) { |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 302 | |
| 303 | if (!mm->size) |
| 304 | return attr; /* Reached end of list */ |
| 305 | |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 306 | if (mm->base_va > base_va + size - 1) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 307 | return attr; /* Next region is after area so end */ |
| 308 | |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 309 | if (mm->base_va + mm->size - 1 < base_va) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 310 | continue; /* Next region has already been overtaken */ |
| 311 | |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 312 | if (mm->attr == attr) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 313 | continue; /* Region doesn't override attribs so skip */ |
| 314 | |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 315 | if (mm->base_va > base_va || |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 316 | mm->base_va + mm->size - 1 < base_va + size - 1) |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 317 | return -1; /* Region doesn't fully cover our area */ |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 318 | |
| 319 | attr = mm->attr; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | static mmap_region_t *init_xlation_table_inner(mmap_region_t *mm, |
| 324 | uintptr_t base_va, |
| 325 | uint64_t *table, |
| 326 | int level) |
| 327 | { |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 328 | assert(level >= XLAT_TABLE_LEVEL_MIN && level <= XLAT_TABLE_LEVEL_MAX); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 329 | |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 330 | unsigned int level_size_shift = |
| 331 | L0_XLAT_ADDRESS_SHIFT - level * XLAT_TABLE_ENTRIES_SHIFT; |
| 332 | u_register_t level_size = (u_register_t)1 << level_size_shift; |
| 333 | u_register_t level_index_mask = |
| 334 | ((u_register_t)XLAT_TABLE_ENTRIES_MASK) << level_size_shift; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 335 | |
| 336 | debug_print("New xlat table:\n"); |
| 337 | |
| 338 | do { |
| 339 | uint64_t desc = UNSET_DESC; |
| 340 | |
| 341 | if (!mm->size) { |
| 342 | /* Done mapping regions; finish zeroing the table */ |
| 343 | desc = INVALID_DESC; |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 344 | } else if (mm->base_va + mm->size - 1 < base_va) { |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 345 | /* This area is after the region so get next region */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 346 | ++mm; |
| 347 | continue; |
| 348 | } |
| 349 | |
Antonio Nino Diaz | d48ae61 | 2016-08-02 09:21:41 +0100 | [diff] [blame] | 350 | debug_print("%s VA:%p size:0x%llx ", get_level_spacer(level), |
| 351 | (void *)base_va, (unsigned long long)level_size); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 352 | |
Soby Mathew | fea233c | 2016-07-13 15:45:15 +0100 | [diff] [blame] | 353 | if (mm->base_va > base_va + level_size - 1) { |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 354 | /* Next region is after this area. Nothing to map yet */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 355 | desc = INVALID_DESC; |
Antonio Nino Diaz | 010888d | 2016-12-13 15:02:31 +0000 | [diff] [blame] | 356 | /* Make sure that the current level allows block descriptors */ |
| 357 | } else if (level >= XLAT_BLOCK_LEVEL_MIN) { |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 358 | /* |
| 359 | * Try to get attributes of this area. It will fail if |
| 360 | * there are partially overlapping regions. On success, |
| 361 | * it will return the innermost region's attributes. |
| 362 | */ |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 363 | int attr = mmap_region_attr(mm, base_va, level_size); |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 364 | if (attr >= 0) { |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 365 | desc = mmap_desc(attr, |
| 366 | base_va - mm->base_va + mm->base_pa, |
| 367 | level); |
Antonio Nino Diaz | b5e7f77 | 2016-03-30 15:45:57 +0100 | [diff] [blame] | 368 | } |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 369 | } |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 370 | |
| 371 | if (desc == UNSET_DESC) { |
| 372 | /* Area not covered by a region so need finer table */ |
| 373 | uint64_t *new_table = xlat_tables[next_xlat++]; |
| 374 | assert(next_xlat <= MAX_XLAT_TABLES); |
Soby Mathew | a0fedc4 | 2016-06-16 14:52:04 +0100 | [diff] [blame] | 375 | desc = TABLE_DESC | (uintptr_t)new_table; |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 376 | |
| 377 | /* Recurse to fill in new table */ |
| 378 | mm = init_xlation_table_inner(mm, base_va, |
| 379 | new_table, level+1); |
| 380 | } |
| 381 | |
| 382 | debug_print("\n"); |
| 383 | |
| 384 | *table++ = desc; |
| 385 | base_va += level_size; |
Antonio Nino Diaz | d1beee2 | 2016-12-13 15:28:54 +0000 | [diff] [blame] | 386 | } while ((base_va & level_index_mask) && |
| 387 | (base_va - 1 < PLAT_VIRT_ADDR_SPACE_SIZE - 1)); |
Soby Mathew | 44170c4 | 2016-03-22 15:51:08 +0000 | [diff] [blame] | 388 | |
| 389 | return mm; |
| 390 | } |
| 391 | |
| 392 | void init_xlation_table(uintptr_t base_va, uint64_t *table, |
| 393 | int level, uintptr_t *max_va, |
| 394 | unsigned long long *max_pa) |
| 395 | { |
| 396 | |
| 397 | init_xlation_table_inner(mmap, base_va, table, level); |
| 398 | *max_va = xlat_max_va; |
| 399 | *max_pa = xlat_max_pa; |
| 400 | } |