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