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