Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2013 |
| 4 | * David Feng <fenghua@phytium.com.cn> |
| 5 | * |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 6 | * (C) Copyright 2016 |
| 7 | * Alexander Graf <agraf@suse.de> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 11 | #include <cpu_func.h> |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 12 | #include <asm/system.h> |
| 13 | #include <asm/armv8/mmu.h> |
| 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 17 | #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 18 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 19 | /* |
| 20 | * With 4k page granule, a virtual address is split into 4 lookup parts |
| 21 | * spanning 9 bits each: |
| 22 | * |
| 23 | * _______________________________________________ |
| 24 | * | | | | | | | |
| 25 | * | 0 | Lv0 | Lv1 | Lv2 | Lv3 | off | |
| 26 | * |_______|_______|_______|_______|_______|_______| |
| 27 | * 63-48 47-39 38-30 29-21 20-12 11-00 |
| 28 | * |
| 29 | * mask page size |
| 30 | * |
| 31 | * Lv0: FF8000000000 -- |
| 32 | * Lv1: 7FC0000000 1G |
| 33 | * Lv2: 3FE00000 2M |
| 34 | * Lv3: 1FF000 4K |
| 35 | * off: FFF |
| 36 | */ |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 37 | |
York Sun | a81fcd1 | 2016-06-24 16:46:20 -0700 | [diff] [blame] | 38 | u64 get_tcr(int el, u64 *pips, u64 *pva_bits) |
Alexander Graf | fb74cc1 | 2016-03-04 01:09:45 +0100 | [diff] [blame] | 39 | { |
| 40 | u64 max_addr = 0; |
| 41 | u64 ips, va_bits; |
| 42 | u64 tcr; |
| 43 | int i; |
| 44 | |
| 45 | /* Find the largest address we need to support */ |
Alexander Graf | 6b3e7ca | 2016-03-04 01:09:48 +0100 | [diff] [blame] | 46 | for (i = 0; mem_map[i].size || mem_map[i].attrs; i++) |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 47 | max_addr = max(max_addr, mem_map[i].virt + mem_map[i].size); |
Alexander Graf | fb74cc1 | 2016-03-04 01:09:45 +0100 | [diff] [blame] | 48 | |
| 49 | /* Calculate the maximum physical (and thus virtual) address */ |
| 50 | if (max_addr > (1ULL << 44)) { |
| 51 | ips = 5; |
| 52 | va_bits = 48; |
| 53 | } else if (max_addr > (1ULL << 42)) { |
| 54 | ips = 4; |
| 55 | va_bits = 44; |
| 56 | } else if (max_addr > (1ULL << 40)) { |
| 57 | ips = 3; |
| 58 | va_bits = 42; |
| 59 | } else if (max_addr > (1ULL << 36)) { |
| 60 | ips = 2; |
| 61 | va_bits = 40; |
| 62 | } else if (max_addr > (1ULL << 32)) { |
| 63 | ips = 1; |
| 64 | va_bits = 36; |
| 65 | } else { |
| 66 | ips = 0; |
| 67 | va_bits = 32; |
| 68 | } |
| 69 | |
| 70 | if (el == 1) { |
Alexander Graf | f03c0e4 | 2016-03-04 01:09:46 +0100 | [diff] [blame] | 71 | tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE; |
Alexander Graf | fb74cc1 | 2016-03-04 01:09:45 +0100 | [diff] [blame] | 72 | } else if (el == 2) { |
| 73 | tcr = TCR_EL2_RSVD | (ips << 16); |
| 74 | } else { |
| 75 | tcr = TCR_EL3_RSVD | (ips << 16); |
| 76 | } |
| 77 | |
| 78 | /* PTWs cacheable, inner/outer WBWA and inner shareable */ |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 79 | tcr |= TCR_TG0_4K | TCR_SHARED_INNER | TCR_ORGN_WBWA | TCR_IRGN_WBWA; |
| 80 | tcr |= TCR_T0SZ(va_bits); |
Alexander Graf | fb74cc1 | 2016-03-04 01:09:45 +0100 | [diff] [blame] | 81 | |
| 82 | if (pips) |
| 83 | *pips = ips; |
| 84 | if (pva_bits) |
| 85 | *pva_bits = va_bits; |
| 86 | |
| 87 | return tcr; |
| 88 | } |
| 89 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 90 | #define MAX_PTE_ENTRIES 512 |
| 91 | |
| 92 | static int pte_type(u64 *pte) |
| 93 | { |
| 94 | return *pte & PTE_TYPE_MASK; |
| 95 | } |
| 96 | |
| 97 | /* Returns the LSB number for a PTE on level <level> */ |
| 98 | static int level2shift(int level) |
| 99 | { |
| 100 | /* Page is 12 bits wide, every level translates 9 bits */ |
| 101 | return (12 + 9 * (3 - level)); |
| 102 | } |
| 103 | |
| 104 | static u64 *find_pte(u64 addr, int level) |
| 105 | { |
| 106 | int start_level = 0; |
| 107 | u64 *pte; |
| 108 | u64 idx; |
| 109 | u64 va_bits; |
| 110 | int i; |
| 111 | |
| 112 | debug("addr=%llx level=%d\n", addr, level); |
| 113 | |
| 114 | get_tcr(0, NULL, &va_bits); |
| 115 | if (va_bits < 39) |
| 116 | start_level = 1; |
| 117 | |
| 118 | if (level < start_level) |
| 119 | return NULL; |
| 120 | |
| 121 | /* Walk through all page table levels to find our PTE */ |
| 122 | pte = (u64*)gd->arch.tlb_addr; |
| 123 | for (i = start_level; i < 4; i++) { |
| 124 | idx = (addr >> level2shift(i)) & 0x1FF; |
| 125 | pte += idx; |
| 126 | debug("idx=%llx PTE %p at level %d: %llx\n", idx, pte, i, *pte); |
| 127 | |
| 128 | /* Found it */ |
| 129 | if (i == level) |
| 130 | return pte; |
| 131 | /* PTE is no table (either invalid or block), can't traverse */ |
| 132 | if (pte_type(pte) != PTE_TYPE_TABLE) |
| 133 | return NULL; |
| 134 | /* Off to the next level */ |
| 135 | pte = (u64*)(*pte & 0x0000fffffffff000ULL); |
| 136 | } |
| 137 | |
| 138 | /* Should never reach here */ |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | /* Returns and creates a new full table (512 entries) */ |
| 143 | static u64 *create_table(void) |
| 144 | { |
| 145 | u64 *new_table = (u64*)gd->arch.tlb_fillptr; |
| 146 | u64 pt_len = MAX_PTE_ENTRIES * sizeof(u64); |
| 147 | |
| 148 | /* Allocate MAX_PTE_ENTRIES pte entries */ |
| 149 | gd->arch.tlb_fillptr += pt_len; |
| 150 | |
| 151 | if (gd->arch.tlb_fillptr - gd->arch.tlb_addr > gd->arch.tlb_size) |
| 152 | panic("Insufficient RAM for page table: 0x%lx > 0x%lx. " |
| 153 | "Please increase the size in get_page_table_size()", |
| 154 | gd->arch.tlb_fillptr - gd->arch.tlb_addr, |
| 155 | gd->arch.tlb_size); |
| 156 | |
| 157 | /* Mark all entries as invalid */ |
| 158 | memset(new_table, 0, pt_len); |
| 159 | |
| 160 | return new_table; |
| 161 | } |
| 162 | |
| 163 | static void set_pte_table(u64 *pte, u64 *table) |
| 164 | { |
| 165 | /* Point *pte to the new table */ |
| 166 | debug("Setting %p to addr=%p\n", pte, table); |
| 167 | *pte = PTE_TYPE_TABLE | (ulong)table; |
| 168 | } |
| 169 | |
York Sun | f44afe7 | 2016-06-24 16:46:21 -0700 | [diff] [blame] | 170 | /* Splits a block PTE into table with subpages spanning the old block */ |
| 171 | static void split_block(u64 *pte, int level) |
| 172 | { |
| 173 | u64 old_pte = *pte; |
| 174 | u64 *new_table; |
| 175 | u64 i = 0; |
| 176 | /* level describes the parent level, we need the child ones */ |
| 177 | int levelshift = level2shift(level + 1); |
| 178 | |
| 179 | if (pte_type(pte) != PTE_TYPE_BLOCK) |
| 180 | panic("PTE %p (%llx) is not a block. Some driver code wants to " |
| 181 | "modify dcache settings for an range not covered in " |
| 182 | "mem_map.", pte, old_pte); |
| 183 | |
| 184 | new_table = create_table(); |
| 185 | debug("Splitting pte %p (%llx) into %p\n", pte, old_pte, new_table); |
| 186 | |
| 187 | for (i = 0; i < MAX_PTE_ENTRIES; i++) { |
| 188 | new_table[i] = old_pte | (i << levelshift); |
| 189 | |
| 190 | /* Level 3 block PTEs have the table type */ |
| 191 | if ((level + 1) == 3) |
| 192 | new_table[i] |= PTE_TYPE_TABLE; |
| 193 | |
| 194 | debug("Setting new_table[%lld] = %llx\n", i, new_table[i]); |
| 195 | } |
| 196 | |
| 197 | /* Set the new table into effect */ |
| 198 | set_pte_table(pte, new_table); |
| 199 | } |
| 200 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 201 | /* Add one mm_region map entry to the page tables */ |
| 202 | static void add_map(struct mm_region *map) |
| 203 | { |
| 204 | u64 *pte; |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 205 | u64 virt = map->virt; |
| 206 | u64 phys = map->phys; |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 207 | u64 size = map->size; |
| 208 | u64 attrs = map->attrs | PTE_TYPE_BLOCK | PTE_BLOCK_AF; |
| 209 | u64 blocksize; |
| 210 | int level; |
| 211 | u64 *new_table; |
| 212 | |
| 213 | while (size) { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 214 | pte = find_pte(virt, 0); |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 215 | if (pte && (pte_type(pte) == PTE_TYPE_FAULT)) { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 216 | debug("Creating table for virt 0x%llx\n", virt); |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 217 | new_table = create_table(); |
| 218 | set_pte_table(pte, new_table); |
| 219 | } |
| 220 | |
| 221 | for (level = 1; level < 4; level++) { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 222 | pte = find_pte(virt, level); |
York Sun | f44afe7 | 2016-06-24 16:46:21 -0700 | [diff] [blame] | 223 | if (!pte) |
| 224 | panic("pte not found\n"); |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 225 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 226 | blocksize = 1ULL << level2shift(level); |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 227 | debug("Checking if pte fits for virt=%llx size=%llx blocksize=%llx\n", |
| 228 | virt, size, blocksize); |
| 229 | if (size >= blocksize && !(virt & (blocksize - 1))) { |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 230 | /* Page fits, create block PTE */ |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 231 | debug("Setting PTE %p to block virt=%llx\n", |
| 232 | pte, virt); |
Peng Fan | e0e9871 | 2017-11-28 10:31:28 +0800 | [diff] [blame] | 233 | if (level == 3) |
| 234 | *pte = phys | attrs | PTE_TYPE_PAGE; |
| 235 | else |
| 236 | *pte = phys | attrs; |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 237 | virt += blocksize; |
| 238 | phys += blocksize; |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 239 | size -= blocksize; |
| 240 | break; |
York Sun | f44afe7 | 2016-06-24 16:46:21 -0700 | [diff] [blame] | 241 | } else if (pte_type(pte) == PTE_TYPE_FAULT) { |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 242 | /* Page doesn't fit, create subpages */ |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 243 | debug("Creating subtable for virt 0x%llx blksize=%llx\n", |
| 244 | virt, blocksize); |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 245 | new_table = create_table(); |
| 246 | set_pte_table(pte, new_table); |
York Sun | f44afe7 | 2016-06-24 16:46:21 -0700 | [diff] [blame] | 247 | } else if (pte_type(pte) == PTE_TYPE_BLOCK) { |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 248 | debug("Split block into subtable for virt 0x%llx blksize=0x%llx\n", |
| 249 | virt, blocksize); |
York Sun | f44afe7 | 2016-06-24 16:46:21 -0700 | [diff] [blame] | 250 | split_block(pte, level); |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 256 | enum pte_type { |
| 257 | PTE_INVAL, |
| 258 | PTE_BLOCK, |
| 259 | PTE_LEVEL, |
| 260 | }; |
| 261 | |
| 262 | /* |
| 263 | * This is a recursively called function to count the number of |
| 264 | * page tables we need to cover a particular PTE range. If you |
| 265 | * call this with level = -1 you basically get the full 48 bit |
| 266 | * coverage. |
| 267 | */ |
| 268 | static int count_required_pts(u64 addr, int level, u64 maxaddr) |
| 269 | { |
| 270 | int levelshift = level2shift(level); |
| 271 | u64 levelsize = 1ULL << levelshift; |
| 272 | u64 levelmask = levelsize - 1; |
| 273 | u64 levelend = addr + levelsize; |
| 274 | int r = 0; |
| 275 | int i; |
| 276 | enum pte_type pte_type = PTE_INVAL; |
| 277 | |
Alexander Graf | 6b3e7ca | 2016-03-04 01:09:48 +0100 | [diff] [blame] | 278 | for (i = 0; mem_map[i].size || mem_map[i].attrs; i++) { |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 279 | struct mm_region *map = &mem_map[i]; |
York Sun | c7104e5 | 2016-06-24 16:46:22 -0700 | [diff] [blame] | 280 | u64 start = map->virt; |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 281 | u64 end = start + map->size; |
| 282 | |
| 283 | /* Check if the PTE would overlap with the map */ |
| 284 | if (max(addr, start) <= min(levelend, end)) { |
| 285 | start = max(addr, start); |
| 286 | end = min(levelend, end); |
| 287 | |
| 288 | /* We need a sub-pt for this level */ |
| 289 | if ((start & levelmask) || (end & levelmask)) { |
| 290 | pte_type = PTE_LEVEL; |
| 291 | break; |
| 292 | } |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 293 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 294 | /* Lv0 can not do block PTEs, so do levels here too */ |
| 295 | if (level <= 0) { |
| 296 | pte_type = PTE_LEVEL; |
| 297 | break; |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 300 | /* PTE is active, but fits into a block */ |
| 301 | pte_type = PTE_BLOCK; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /* |
| 306 | * Block PTEs at this level are already covered by the parent page |
| 307 | * table, so we only need to count sub page tables. |
| 308 | */ |
| 309 | if (pte_type == PTE_LEVEL) { |
| 310 | int sublevel = level + 1; |
| 311 | u64 sublevelsize = 1ULL << level2shift(sublevel); |
| 312 | |
| 313 | /* Account for the new sub page table ... */ |
| 314 | r = 1; |
| 315 | |
| 316 | /* ... and for all child page tables that one might have */ |
| 317 | for (i = 0; i < MAX_PTE_ENTRIES; i++) { |
| 318 | r += count_required_pts(addr, sublevel, maxaddr); |
| 319 | addr += sublevelsize; |
| 320 | |
| 321 | if (addr >= maxaddr) { |
| 322 | /* |
| 323 | * We reached the end of address space, no need |
| 324 | * to look any further. |
| 325 | */ |
| 326 | break; |
| 327 | } |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 330 | |
| 331 | return r; |
| 332 | } |
| 333 | |
| 334 | /* Returns the estimated required size of all page tables */ |
Alexander Graf | bc78b92 | 2016-03-21 20:26:12 +0100 | [diff] [blame] | 335 | __weak u64 get_page_table_size(void) |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 336 | { |
| 337 | u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64); |
| 338 | u64 size = 0; |
| 339 | u64 va_bits; |
| 340 | int start_level = 0; |
| 341 | |
| 342 | get_tcr(0, NULL, &va_bits); |
| 343 | if (va_bits < 39) |
| 344 | start_level = 1; |
| 345 | |
| 346 | /* Account for all page tables we would need to cover our memory map */ |
| 347 | size = one_pt * count_required_pts(0, start_level - 1, 1ULL << va_bits); |
| 348 | |
| 349 | /* |
| 350 | * We need to duplicate our page table once to have an emergency pt to |
| 351 | * resort to when splitting page tables later on |
| 352 | */ |
| 353 | size *= 2; |
| 354 | |
| 355 | /* |
| 356 | * We may need to split page tables later on if dcache settings change, |
| 357 | * so reserve up to 4 (random pick) page tables for that. |
| 358 | */ |
| 359 | size += one_pt * 4; |
| 360 | |
| 361 | return size; |
| 362 | } |
| 363 | |
York Sun | a81fcd1 | 2016-06-24 16:46:20 -0700 | [diff] [blame] | 364 | void setup_pgtables(void) |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 365 | { |
| 366 | int i; |
| 367 | |
York Sun | a81fcd1 | 2016-06-24 16:46:20 -0700 | [diff] [blame] | 368 | if (!gd->arch.tlb_fillptr || !gd->arch.tlb_addr) |
| 369 | panic("Page table pointer not setup."); |
| 370 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 371 | /* |
| 372 | * Allocate the first level we're on with invalidate entries. |
| 373 | * If the starting level is 0 (va_bits >= 39), then this is our |
| 374 | * Lv0 page table, otherwise it's the entry Lv1 page table. |
| 375 | */ |
| 376 | create_table(); |
| 377 | |
| 378 | /* Now add all MMU table entries one after another to the table */ |
Alexander Graf | 6b3e7ca | 2016-03-04 01:09:48 +0100 | [diff] [blame] | 379 | for (i = 0; mem_map[i].size || mem_map[i].attrs; i++) |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 380 | add_map(&mem_map[i]); |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | static void setup_all_pgtables(void) |
| 384 | { |
| 385 | u64 tlb_addr = gd->arch.tlb_addr; |
Alexander Graf | fa3754e | 2016-07-30 23:13:03 +0200 | [diff] [blame] | 386 | u64 tlb_size = gd->arch.tlb_size; |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 387 | |
| 388 | /* Reset the fill ptr */ |
| 389 | gd->arch.tlb_fillptr = tlb_addr; |
| 390 | |
| 391 | /* Create normal system page tables */ |
| 392 | setup_pgtables(); |
| 393 | |
| 394 | /* Create emergency page tables */ |
Alexander Graf | fa3754e | 2016-07-30 23:13:03 +0200 | [diff] [blame] | 395 | gd->arch.tlb_size -= (uintptr_t)gd->arch.tlb_fillptr - |
| 396 | (uintptr_t)gd->arch.tlb_addr; |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 397 | gd->arch.tlb_addr = gd->arch.tlb_fillptr; |
| 398 | setup_pgtables(); |
| 399 | gd->arch.tlb_emerg = gd->arch.tlb_addr; |
| 400 | gd->arch.tlb_addr = tlb_addr; |
Alexander Graf | fa3754e | 2016-07-30 23:13:03 +0200 | [diff] [blame] | 401 | gd->arch.tlb_size = tlb_size; |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 402 | } |
| 403 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 404 | /* to activate the MMU we need to set up virtual memory */ |
Stephen Warren | 7333c6a | 2015-10-05 12:09:00 -0600 | [diff] [blame] | 405 | __weak void mmu_setup(void) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 406 | { |
Thierry Reding | 59c364d | 2015-07-22 17:10:11 -0600 | [diff] [blame] | 407 | int el; |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 408 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 409 | /* Set up page tables only once */ |
| 410 | if (!gd->arch.tlb_fillptr) |
| 411 | setup_all_pgtables(); |
Alexander Graf | fb74cc1 | 2016-03-04 01:09:45 +0100 | [diff] [blame] | 412 | |
| 413 | el = current_el(); |
| 414 | set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL), |
| 415 | MEMORY_ATTRIBUTES); |
Alexander Graf | fb74cc1 | 2016-03-04 01:09:45 +0100 | [diff] [blame] | 416 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 417 | /* enable the mmu */ |
| 418 | set_sctlr(get_sctlr() | CR_M); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Performs a invalidation of the entire data cache at all levels |
| 423 | */ |
| 424 | void invalidate_dcache_all(void) |
| 425 | { |
York Sun | ef04201 | 2014-02-26 13:26:04 -0800 | [diff] [blame] | 426 | __asm_invalidate_dcache_all(); |
Stephen Warren | ddb0f63 | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 427 | __asm_invalidate_l3_dcache(); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /* |
York Sun | 1ce575f | 2015-01-06 13:18:42 -0800 | [diff] [blame] | 431 | * Performs a clean & invalidation of the entire data cache at all levels. |
| 432 | * This function needs to be inline to avoid using stack. |
Stephen Warren | ddb0f63 | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 433 | * __asm_flush_l3_dcache return status of timeout |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 434 | */ |
York Sun | 1ce575f | 2015-01-06 13:18:42 -0800 | [diff] [blame] | 435 | inline void flush_dcache_all(void) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 436 | { |
York Sun | 1ce575f | 2015-01-06 13:18:42 -0800 | [diff] [blame] | 437 | int ret; |
| 438 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 439 | __asm_flush_dcache_all(); |
Stephen Warren | ddb0f63 | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 440 | ret = __asm_flush_l3_dcache(); |
York Sun | 1ce575f | 2015-01-06 13:18:42 -0800 | [diff] [blame] | 441 | if (ret) |
| 442 | debug("flushing dcache returns 0x%x\n", ret); |
| 443 | else |
| 444 | debug("flushing dcache successfully.\n"); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 445 | } |
| 446 | |
Vignesh Raghavendra | 384c141 | 2019-04-22 21:43:32 +0530 | [diff] [blame] | 447 | #ifndef CONFIG_SYS_DISABLE_DCACHE_OPS |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 448 | /* |
| 449 | * Invalidates range in all levels of D-cache/unified cache |
| 450 | */ |
| 451 | void invalidate_dcache_range(unsigned long start, unsigned long stop) |
| 452 | { |
Simon Glass | 4415c3b | 2017-04-05 17:53:18 -0600 | [diff] [blame] | 453 | __asm_invalidate_dcache_range(start, stop); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Flush range(clean & invalidate) from all levels of D-cache/unified cache |
| 458 | */ |
| 459 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 460 | { |
| 461 | __asm_flush_dcache_range(start, stop); |
| 462 | } |
Vignesh Raghavendra | 384c141 | 2019-04-22 21:43:32 +0530 | [diff] [blame] | 463 | #else |
| 464 | void invalidate_dcache_range(unsigned long start, unsigned long stop) |
| 465 | { |
| 466 | } |
| 467 | |
| 468 | void flush_dcache_range(unsigned long start, unsigned long stop) |
| 469 | { |
| 470 | } |
| 471 | #endif /* CONFIG_SYS_DISABLE_DCACHE_OPS */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 472 | |
| 473 | void dcache_enable(void) |
| 474 | { |
| 475 | /* The data cache is not active unless the mmu is enabled */ |
| 476 | if (!(get_sctlr() & CR_M)) { |
| 477 | invalidate_dcache_all(); |
| 478 | __asm_invalidate_tlb_all(); |
| 479 | mmu_setup(); |
| 480 | } |
| 481 | |
| 482 | set_sctlr(get_sctlr() | CR_C); |
| 483 | } |
| 484 | |
| 485 | void dcache_disable(void) |
| 486 | { |
| 487 | uint32_t sctlr; |
| 488 | |
| 489 | sctlr = get_sctlr(); |
| 490 | |
| 491 | /* if cache isn't enabled no need to disable */ |
| 492 | if (!(sctlr & CR_C)) |
| 493 | return; |
| 494 | |
| 495 | set_sctlr(sctlr & ~(CR_C|CR_M)); |
| 496 | |
| 497 | flush_dcache_all(); |
| 498 | __asm_invalidate_tlb_all(); |
| 499 | } |
| 500 | |
| 501 | int dcache_status(void) |
| 502 | { |
| 503 | return (get_sctlr() & CR_C) != 0; |
| 504 | } |
| 505 | |
Siva Durga Prasad Paladugu | ba2432a | 2015-06-26 18:05:07 +0530 | [diff] [blame] | 506 | u64 *__weak arch_get_page_table(void) { |
| 507 | puts("No page table offset defined\n"); |
| 508 | |
| 509 | return NULL; |
| 510 | } |
| 511 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 512 | static bool is_aligned(u64 addr, u64 size, u64 align) |
| 513 | { |
| 514 | return !(addr & (align - 1)) && !(size & (align - 1)); |
| 515 | } |
| 516 | |
York Sun | 5bb14e0 | 2017-03-06 09:02:33 -0800 | [diff] [blame] | 517 | /* Use flag to indicate if attrs has more than d-cache attributes */ |
| 518 | static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level) |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 519 | { |
| 520 | int levelshift = level2shift(level); |
| 521 | u64 levelsize = 1ULL << levelshift; |
| 522 | u64 *pte = find_pte(start, level); |
| 523 | |
| 524 | /* Can we can just modify the current level block PTE? */ |
| 525 | if (is_aligned(start, size, levelsize)) { |
York Sun | 5bb14e0 | 2017-03-06 09:02:33 -0800 | [diff] [blame] | 526 | if (flag) { |
| 527 | *pte &= ~PMD_ATTRMASK; |
| 528 | *pte |= attrs & PMD_ATTRMASK; |
| 529 | } else { |
| 530 | *pte &= ~PMD_ATTRINDX_MASK; |
| 531 | *pte |= attrs & PMD_ATTRINDX_MASK; |
| 532 | } |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 533 | debug("Set attrs=%llx pte=%p level=%d\n", attrs, pte, level); |
| 534 | |
| 535 | return levelsize; |
| 536 | } |
| 537 | |
| 538 | /* Unaligned or doesn't fit, maybe split block into table */ |
| 539 | debug("addr=%llx level=%d pte=%p (%llx)\n", start, level, pte, *pte); |
| 540 | |
| 541 | /* Maybe we need to split the block into a table */ |
| 542 | if (pte_type(pte) == PTE_TYPE_BLOCK) |
| 543 | split_block(pte, level); |
| 544 | |
| 545 | /* And then double-check it became a table or already is one */ |
| 546 | if (pte_type(pte) != PTE_TYPE_TABLE) |
| 547 | panic("PTE %p (%llx) for addr=%llx should be a table", |
| 548 | pte, *pte, start); |
| 549 | |
| 550 | /* Roll on to the next page table level */ |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size, |
| 555 | enum dcache_option option) |
| 556 | { |
| 557 | u64 attrs = PMD_ATTRINDX(option); |
| 558 | u64 real_start = start; |
| 559 | u64 real_size = size; |
| 560 | |
| 561 | debug("start=%lx size=%lx\n", (ulong)start, (ulong)size); |
| 562 | |
York Sun | a81fcd1 | 2016-06-24 16:46:20 -0700 | [diff] [blame] | 563 | if (!gd->arch.tlb_emerg) |
| 564 | panic("Emergency page table not setup."); |
| 565 | |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 566 | /* |
| 567 | * We can not modify page tables that we're currently running on, |
| 568 | * so we first need to switch to the "emergency" page tables where |
| 569 | * we can safely modify our primary page tables and then switch back |
| 570 | */ |
| 571 | __asm_switch_ttbr(gd->arch.tlb_emerg); |
| 572 | |
| 573 | /* |
| 574 | * Loop through the address range until we find a page granule that fits |
| 575 | * our alignment constraints, then set it to the new cache attributes |
| 576 | */ |
| 577 | while (size > 0) { |
| 578 | int level; |
| 579 | u64 r; |
| 580 | |
| 581 | for (level = 1; level < 4; level++) { |
York Sun | 5bb14e0 | 2017-03-06 09:02:33 -0800 | [diff] [blame] | 582 | /* Set d-cache attributes only */ |
| 583 | r = set_one_region(start, size, attrs, false, level); |
Alexander Graf | e317fe8 | 2016-03-04 01:09:47 +0100 | [diff] [blame] | 584 | if (r) { |
| 585 | /* PTE successfully replaced */ |
| 586 | size -= r; |
| 587 | start += r; |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | } |
| 593 | |
| 594 | /* We're done modifying page tables, switch back to our primary ones */ |
| 595 | __asm_switch_ttbr(gd->arch.tlb_addr); |
| 596 | |
| 597 | /* |
| 598 | * Make sure there's nothing stale in dcache for a region that might |
| 599 | * have caches off now |
| 600 | */ |
| 601 | flush_dcache_range(real_start, real_start + real_size); |
| 602 | } |
Sergey Temerkhanov | 78eaa49 | 2015-10-14 09:55:45 -0700 | [diff] [blame] | 603 | |
York Sun | 5bb14e0 | 2017-03-06 09:02:33 -0800 | [diff] [blame] | 604 | /* |
| 605 | * Modify MMU table for a region with updated PXN/UXN/Memory type/valid bits. |
| 606 | * The procecess is break-before-make. The target region will be marked as |
| 607 | * invalid during the process of changing. |
| 608 | */ |
| 609 | void mmu_change_region_attr(phys_addr_t addr, size_t siz, u64 attrs) |
| 610 | { |
| 611 | int level; |
| 612 | u64 r, size, start; |
| 613 | |
| 614 | start = addr; |
| 615 | size = siz; |
| 616 | /* |
| 617 | * Loop through the address range until we find a page granule that fits |
| 618 | * our alignment constraints, then set it to "invalid". |
| 619 | */ |
| 620 | while (size > 0) { |
| 621 | for (level = 1; level < 4; level++) { |
| 622 | /* Set PTE to fault */ |
| 623 | r = set_one_region(start, size, PTE_TYPE_FAULT, true, |
| 624 | level); |
| 625 | if (r) { |
| 626 | /* PTE successfully invalidated */ |
| 627 | size -= r; |
| 628 | start += r; |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | flush_dcache_range(gd->arch.tlb_addr, |
| 635 | gd->arch.tlb_addr + gd->arch.tlb_size); |
| 636 | __asm_invalidate_tlb_all(); |
| 637 | |
| 638 | /* |
| 639 | * Loop through the address range until we find a page granule that fits |
| 640 | * our alignment constraints, then set it to the new cache attributes |
| 641 | */ |
| 642 | start = addr; |
| 643 | size = siz; |
| 644 | while (size > 0) { |
| 645 | for (level = 1; level < 4; level++) { |
| 646 | /* Set PTE to new attributes */ |
| 647 | r = set_one_region(start, size, attrs, true, level); |
| 648 | if (r) { |
| 649 | /* PTE successfully updated */ |
| 650 | size -= r; |
| 651 | start += r; |
| 652 | break; |
| 653 | } |
| 654 | } |
| 655 | } |
| 656 | flush_dcache_range(gd->arch.tlb_addr, |
| 657 | gd->arch.tlb_addr + gd->arch.tlb_size); |
| 658 | __asm_invalidate_tlb_all(); |
| 659 | } |
| 660 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 661 | #else /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 662 | |
Alexander Graf | bc40da9 | 2016-03-04 01:09:55 +0100 | [diff] [blame] | 663 | /* |
| 664 | * For SPL builds, we may want to not have dcache enabled. Any real U-Boot |
| 665 | * running however really wants to have dcache and the MMU active. Check that |
| 666 | * everything is sane and give the developer a hint if it isn't. |
| 667 | */ |
| 668 | #ifndef CONFIG_SPL_BUILD |
| 669 | #error Please describe your MMU layout in CONFIG_SYS_MEM_MAP and enable dcache. |
| 670 | #endif |
| 671 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 672 | void invalidate_dcache_all(void) |
| 673 | { |
| 674 | } |
| 675 | |
| 676 | void flush_dcache_all(void) |
| 677 | { |
| 678 | } |
| 679 | |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 680 | void dcache_enable(void) |
| 681 | { |
| 682 | } |
| 683 | |
| 684 | void dcache_disable(void) |
| 685 | { |
| 686 | } |
| 687 | |
| 688 | int dcache_status(void) |
| 689 | { |
| 690 | return 0; |
| 691 | } |
| 692 | |
Siva Durga Prasad Paladugu | ba2432a | 2015-06-26 18:05:07 +0530 | [diff] [blame] | 693 | void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size, |
| 694 | enum dcache_option option) |
| 695 | { |
| 696 | } |
| 697 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 698 | #endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 699 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 700 | #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 701 | |
| 702 | void icache_enable(void) |
| 703 | { |
Stephen Warren | ddb0f63 | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 704 | invalidate_icache_all(); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 705 | set_sctlr(get_sctlr() | CR_I); |
| 706 | } |
| 707 | |
| 708 | void icache_disable(void) |
| 709 | { |
| 710 | set_sctlr(get_sctlr() & ~CR_I); |
| 711 | } |
| 712 | |
| 713 | int icache_status(void) |
| 714 | { |
| 715 | return (get_sctlr() & CR_I) != 0; |
| 716 | } |
| 717 | |
| 718 | void invalidate_icache_all(void) |
| 719 | { |
| 720 | __asm_invalidate_icache_all(); |
Stephen Warren | ddb0f63 | 2016-10-19 15:18:46 -0600 | [diff] [blame] | 721 | __asm_invalidate_l3_icache(); |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 722 | } |
| 723 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 724 | #else /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 725 | |
| 726 | void icache_enable(void) |
| 727 | { |
| 728 | } |
| 729 | |
| 730 | void icache_disable(void) |
| 731 | { |
| 732 | } |
| 733 | |
| 734 | int icache_status(void) |
| 735 | { |
| 736 | return 0; |
| 737 | } |
| 738 | |
| 739 | void invalidate_icache_all(void) |
| 740 | { |
| 741 | } |
| 742 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 743 | #endif /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */ |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 744 | |
| 745 | /* |
| 746 | * Enable dCache & iCache, whether cache is actually enabled |
| 747 | * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF |
| 748 | */ |
York Sun | a84cd72 | 2014-06-23 15:15:54 -0700 | [diff] [blame] | 749 | void __weak enable_caches(void) |
David Feng | 85fd5f1 | 2013-12-14 11:47:35 +0800 | [diff] [blame] | 750 | { |
| 751 | icache_enable(); |
| 752 | dcache_enable(); |
| 753 | } |