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