blob: 1c1e33bec24f7728a3b8d4822548c24c5bab933e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
David Feng85fd5f12013-12-14 11:47:35 +08002/*
3 * (C) Copyright 2013
4 * David Feng <fenghua@phytium.com.cn>
5 *
Alexander Grafe317fe82016-03-04 01:09:47 +01006 * (C) Copyright 2016
7 * Alexander Graf <agraf@suse.de>
David Feng85fd5f12013-12-14 11:47:35 +08008 */
9
Simon Glass1d91ba72019-11-14 12:57:37 -070010#include <cpu_func.h>
Simon Glassf11478f2019-12-28 10:45:07 -070011#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060013#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
David Feng85fd5f12013-12-14 11:47:35 +080015#include <asm/system.h>
16#include <asm/armv8/mmu.h>
Ilias Apalodimase9e18652025-02-20 15:54:42 +020017#include <linux/errno.h>
David Feng85fd5f12013-12-14 11:47:35 +080018
19DECLARE_GLOBAL_DATA_PTR;
20
Trevor Woerner43ec7e02019-05-03 09:41:00 -040021#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -070022
Alexander Grafe317fe82016-03-04 01:09:47 +010023/*
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 Temerkhanov78eaa492015-10-14 09:55:45 -070041
Andre Przywara630a7942022-06-14 00:11:10 +010042static int get_effective_el(void)
Alexander Graffb74cc12016-03-04 01:09:45 +010043{
Andre Przywara630a7942022-06-14 00:11:10 +010044 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
61u64 get_tcr(u64 *pips, u64 *pva_bits)
62{
63 int el = get_effective_el();
Alexander Graffb74cc12016-03-04 01:09:45 +010064 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 Graf6b3e7ca2016-03-04 01:09:48 +010070 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
York Sunc7104e52016-06-24 16:46:22 -070071 max_addr = max(max_addr, mem_map[i].virt + mem_map[i].size);
Alexander Graffb74cc12016-03-04 01:09:45 +010072
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 Graff03c0e42016-03-04 01:09:46 +010095 tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE;
Alexander Graffb74cc12016-03-04 01:09:45 +010096 } 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 Grafe317fe82016-03-04 01:09:47 +0100103 tcr |= TCR_TG0_4K | TCR_SHARED_INNER | TCR_ORGN_WBWA | TCR_IRGN_WBWA;
104 tcr |= TCR_T0SZ(va_bits);
Alexander Graffb74cc12016-03-04 01:09:45 +0100105
106 if (pips)
107 *pips = ips;
108 if (pva_bits)
109 *pva_bits = va_bits;
110
111 return tcr;
112}
113
Alexander Grafe317fe82016-03-04 01:09:47 +0100114#define MAX_PTE_ENTRIES 512
115
116static 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> */
122static int level2shift(int level)
123{
124 /* Page is 12 bits wide, every level translates 9 bits */
125 return (12 + 9 * (3 - level));
126}
127
128static 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 Przywara630a7942022-06-14 00:11:10 +0100138 get_tcr(NULL, &va_bits);
Alexander Grafe317fe82016-03-04 01:09:47 +0100139 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
Marc Zyngierb67855c2023-02-09 04:54:27 +0800166#ifdef CONFIG_CMO_BY_VA_ONLY
167static void __cmo_on_leaves(void (*cmo_fn)(unsigned long, unsigned long),
168 u64 pte, int level, u64 base)
169{
170 u64 *ptep;
171 int i;
172
173 ptep = (u64 *)(pte & GENMASK_ULL(47, PAGE_SHIFT));
174 for (i = 0; i < PAGE_SIZE / sizeof(u64); i++) {
175 u64 end, va = base + i * BIT(level2shift(level));
176 u64 type, attrs;
177
178 pte = ptep[i];
179 type = pte & PTE_TYPE_MASK;
180 attrs = pte & PMD_ATTRINDX_MASK;
181 debug("PTE %llx at level %d VA %llx\n", pte, level, va);
182
183 /* Not valid? next! */
184 if (!(type & PTE_TYPE_VALID))
185 continue;
186
187 /* Not a leaf? Recurse on the next level */
188 if (!(type == PTE_TYPE_BLOCK ||
189 (level == 3 && type == PTE_TYPE_PAGE))) {
190 __cmo_on_leaves(cmo_fn, pte, level + 1, va);
191 continue;
192 }
193
194 /*
195 * From this point, this must be a leaf.
196 *
197 * Start excluding non memory mappings
198 */
199 if (attrs != PTE_BLOCK_MEMTYPE(MT_NORMAL) &&
200 attrs != PTE_BLOCK_MEMTYPE(MT_NORMAL_NC))
201 continue;
202
203 end = va + BIT(level2shift(level)) - 1;
204
205 /* No intersection with RAM? */
206 if (end < gd->ram_base ||
207 va >= (gd->ram_base + gd->ram_size))
208 continue;
209
210 /*
211 * OK, we have a partial RAM mapping. However, this
212 * can cover *more* than the RAM. Yes, u-boot is
213 * *that* braindead. Compute the intersection we care
214 * about, and not a byte more.
215 */
216 va = max(va, (u64)gd->ram_base);
217 end = min(end, gd->ram_base + gd->ram_size);
218
219 debug("Flush PTE %llx at level %d: %llx-%llx\n",
220 pte, level, va, end);
221 cmo_fn(va, end);
222 }
223}
224
225static void apply_cmo_to_mappings(void (*cmo_fn)(unsigned long, unsigned long))
226{
227 u64 va_bits;
228 int sl = 0;
229
230 if (!gd->arch.tlb_addr)
231 return;
232
233 get_tcr(NULL, &va_bits);
234 if (va_bits < 39)
235 sl = 1;
236
237 __cmo_on_leaves(cmo_fn, gd->arch.tlb_addr, sl, 0);
238}
239#else
240static inline void apply_cmo_to_mappings(void *dummy) {}
241#endif
242
Alexander Grafe317fe82016-03-04 01:09:47 +0100243/* Returns and creates a new full table (512 entries) */
244static u64 *create_table(void)
245{
246 u64 *new_table = (u64*)gd->arch.tlb_fillptr;
247 u64 pt_len = MAX_PTE_ENTRIES * sizeof(u64);
248
249 /* Allocate MAX_PTE_ENTRIES pte entries */
250 gd->arch.tlb_fillptr += pt_len;
251
252 if (gd->arch.tlb_fillptr - gd->arch.tlb_addr > gd->arch.tlb_size)
253 panic("Insufficient RAM for page table: 0x%lx > 0x%lx. "
254 "Please increase the size in get_page_table_size()",
255 gd->arch.tlb_fillptr - gd->arch.tlb_addr,
256 gd->arch.tlb_size);
257
258 /* Mark all entries as invalid */
259 memset(new_table, 0, pt_len);
260
261 return new_table;
262}
263
264static void set_pte_table(u64 *pte, u64 *table)
265{
266 /* Point *pte to the new table */
267 debug("Setting %p to addr=%p\n", pte, table);
268 *pte = PTE_TYPE_TABLE | (ulong)table;
269}
270
York Sunf44afe72016-06-24 16:46:21 -0700271/* Splits a block PTE into table with subpages spanning the old block */
272static void split_block(u64 *pte, int level)
273{
274 u64 old_pte = *pte;
275 u64 *new_table;
276 u64 i = 0;
277 /* level describes the parent level, we need the child ones */
278 int levelshift = level2shift(level + 1);
279
280 if (pte_type(pte) != PTE_TYPE_BLOCK)
281 panic("PTE %p (%llx) is not a block. Some driver code wants to "
282 "modify dcache settings for an range not covered in "
283 "mem_map.", pte, old_pte);
284
285 new_table = create_table();
286 debug("Splitting pte %p (%llx) into %p\n", pte, old_pte, new_table);
287
288 for (i = 0; i < MAX_PTE_ENTRIES; i++) {
289 new_table[i] = old_pte | (i << levelshift);
290
291 /* Level 3 block PTEs have the table type */
292 if ((level + 1) == 3)
293 new_table[i] |= PTE_TYPE_TABLE;
294
295 debug("Setting new_table[%lld] = %llx\n", i, new_table[i]);
296 }
297
298 /* Set the new table into effect */
299 set_pte_table(pte, new_table);
300}
301
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800302static void map_range(u64 virt, u64 phys, u64 size, int level,
303 u64 *table, u64 attrs)
Alexander Grafe317fe82016-03-04 01:09:47 +0100304{
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800305 u64 map_size = BIT_ULL(level2shift(level));
306 int i, idx;
Alexander Grafe317fe82016-03-04 01:09:47 +0100307
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800308 idx = (virt >> level2shift(level)) & (MAX_PTE_ENTRIES - 1);
309 for (i = idx; size; i++) {
310 u64 next_size, *next_table;
Alexander Grafe317fe82016-03-04 01:09:47 +0100311
Chris Packham978814f2023-10-27 13:23:53 +1300312 if (level >= 1 &&
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800313 size >= map_size && !(virt & (map_size - 1))) {
314 if (level == 3)
315 table[i] = phys | attrs | PTE_TYPE_PAGE;
316 else
317 table[i] = phys | attrs;
York Sunc7104e52016-06-24 16:46:22 -0700318
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800319 virt += map_size;
320 phys += map_size;
321 size -= map_size;
322
323 continue;
Alexander Grafe317fe82016-03-04 01:09:47 +0100324 }
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800325
326 /* Going one level down */
327 if (pte_type(&table[i]) == PTE_TYPE_FAULT)
328 set_pte_table(&table[i], create_table());
Pierre-Clément Tosid8ceb202024-03-18 19:35:49 +0000329 else if (pte_type(&table[i]) != PTE_TYPE_TABLE)
330 split_block(&table[i], level);
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800331
332 next_table = (u64 *)(table[i] & GENMASK_ULL(47, PAGE_SHIFT));
333 next_size = min(map_size - (virt & (map_size - 1)), size);
334
335 map_range(virt, phys, next_size, level + 1, next_table, attrs);
336
337 virt += next_size;
338 phys += next_size;
339 size -= next_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100340 }
341}
342
Caleb Connolly27b05b52024-08-09 01:59:31 +0200343void mmu_map_region(phys_addr_t addr, u64 size, bool emergency)
344{
345 u64 va_bits;
346 int level = 0;
347 u64 attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_INNER_SHARE;
348
349 attrs |= PTE_TYPE_BLOCK | PTE_BLOCK_AF;
350
351 get_tcr(NULL, &va_bits);
352 if (va_bits < 39)
353 level = 1;
354
355 if (emergency)
356 map_range(addr, addr, size, level,
357 (u64 *)gd->arch.tlb_emerg, attrs);
358
359 /* Switch pagetables while we update the primary one */
360 __asm_switch_ttbr(gd->arch.tlb_emerg);
361
362 map_range(addr, addr, size, level,
363 (u64 *)gd->arch.tlb_addr, attrs);
364
365 __asm_switch_ttbr(gd->arch.tlb_addr);
366}
367
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800368static void add_map(struct mm_region *map)
369{
370 u64 attrs = map->attrs | PTE_TYPE_BLOCK | PTE_BLOCK_AF;
371 u64 va_bits;
372 int level = 0;
373
374 get_tcr(NULL, &va_bits);
375 if (va_bits < 39)
376 level = 1;
377
378 map_range(map->virt, map->phys, map->size, level,
379 (u64 *)gd->arch.tlb_addr, attrs);
380}
381
Marc Zyngier6da328e2023-02-14 21:38:14 +0800382static void count_range(u64 virt, u64 size, int level, int *cntp)
Alexander Grafe317fe82016-03-04 01:09:47 +0100383{
Marc Zyngier6da328e2023-02-14 21:38:14 +0800384 u64 map_size = BIT_ULL(level2shift(level));
385 int i, idx;
Alexander Grafe317fe82016-03-04 01:09:47 +0100386
Marc Zyngier6da328e2023-02-14 21:38:14 +0800387 idx = (virt >> level2shift(level)) & (MAX_PTE_ENTRIES - 1);
388 for (i = idx; size; i++) {
389 u64 next_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100390
Chris Packham978814f2023-10-27 13:23:53 +1300391 if (level >= 1 &&
Marc Zyngier6da328e2023-02-14 21:38:14 +0800392 size >= map_size && !(virt & (map_size - 1))) {
393 virt += map_size;
394 size -= map_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100395
Marc Zyngier6da328e2023-02-14 21:38:14 +0800396 continue;
397 }
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700398
Marc Zyngier6da328e2023-02-14 21:38:14 +0800399 /* Going one level down */
400 (*cntp)++;
401 next_size = min(map_size - (virt & (map_size - 1)), size);
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700402
Marc Zyngier6da328e2023-02-14 21:38:14 +0800403 count_range(virt, next_size, level + 1, cntp);
Alexander Grafe317fe82016-03-04 01:09:47 +0100404
Marc Zyngier6da328e2023-02-14 21:38:14 +0800405 virt += next_size;
406 size -= next_size;
407 }
408}
Alexander Grafe317fe82016-03-04 01:09:47 +0100409
Marc Zyngier6da328e2023-02-14 21:38:14 +0800410static int count_ranges(void)
411{
412 int i, count = 0, level = 0;
413 u64 va_bits;
Alexander Grafe317fe82016-03-04 01:09:47 +0100414
Marc Zyngier6da328e2023-02-14 21:38:14 +0800415 get_tcr(NULL, &va_bits);
416 if (va_bits < 39)
417 level = 1;
Alexander Grafe317fe82016-03-04 01:09:47 +0100418
Marc Zyngier6da328e2023-02-14 21:38:14 +0800419 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
420 count_range(mem_map[i].virt, mem_map[i].size, level, &count);
Alexander Grafe317fe82016-03-04 01:09:47 +0100421
Marc Zyngier6da328e2023-02-14 21:38:14 +0800422 return count;
Alexander Grafe317fe82016-03-04 01:09:47 +0100423}
424
Ilias Apalodimasb3c5d252025-02-20 15:54:38 +0200425#define ALL_ATTRS (3 << 8 | PMD_ATTRMASK)
Caleb Connolly566907c2024-06-17 10:03:48 +0200426#define PTE_IS_TABLE(pte, level) (pte_type(&(pte)) == PTE_TYPE_TABLE && (level) < 3)
427
428enum walker_state {
429 WALKER_STATE_START = 0,
430 WALKER_STATE_TABLE,
431 WALKER_STATE_REGION, /* block or page, depending on level */
432};
433
434
435/**
436 * __pagetable_walk() - Walk through the pagetable and call cb() for each memory region
437 *
438 * This is a software implementation of the ARMv8-A MMU translation table walk. As per
439 * section D5.4 of the ARMv8-A Architecture Reference Manual. It recursively walks the
440 * 4 or 3 levels of the page table and calls the callback function for each discrete
441 * region of memory (that being the discovery of a new table, a collection of blocks
442 * with the same attributes, or of pages with the same attributes).
443 *
444 * U-Boot picks the smallest number of virtual address (VA) bits that it can based on the
445 * memory map configured by the board. If this is less than 39 then the MMU will only use
446 * 3 levels of translation instead of 3 - skipping level 0.
447 *
448 * Each level has 512 entries of 64-bits each. Each entry includes attribute bits and
449 * an address. When the attribute bits indicate a table, the address is the physical
450 * address of the table, so we can recursively call _pagetable_walk() on it (after calling
451 * @cb). If instead they indicate a block or page, we record the start address and attributes
452 * and continue walking until we find a region with different attributes, or the end of the
453 * table, in either case we call @cb with the start and end address of the region.
454 *
455 * This approach can be used to fully emulate the MMU's translation table walk, as per
456 * Figure D5-25 of the ARMv8-A Architecture Reference Manual.
457 *
458 * @addr: The address of the table to walk
459 * @tcr: The TCR register value
460 * @level: The current level of the table
461 * @cb: The callback function to call for each region
462 * @priv: Private data to pass to the callback function
463 */
464static void __pagetable_walk(u64 addr, u64 tcr, int level, pte_walker_cb_t cb, void *priv)
465{
466 u64 *table = (u64 *)addr;
467 u64 attrs, last_attrs = 0, last_addr = 0, entry_start = 0;
468 int i;
469 u64 va_bits = 64 - (tcr & (BIT(6) - 1));
470 static enum walker_state state[4] = { 0 };
471 static bool exit;
472
473 if (!level) {
474 exit = false;
475 if (va_bits < 39)
476 level = 1;
477 }
478
479 state[level] = WALKER_STATE_START;
480
481 /* Walk through the table entries */
482 for (i = 0; i < MAX_PTE_ENTRIES; i++) {
483 u64 pte = table[i];
484 u64 _addr = pte & GENMASK_ULL(va_bits, PAGE_SHIFT);
485
486 if (exit)
487 return;
488
489 if (pte_type(&pte) == PTE_TYPE_FAULT)
490 continue;
491
492 attrs = pte & ALL_ATTRS;
493 /* If we're currently inside a block or set of pages */
494 if (state[level] > WALKER_STATE_START && state[level] != WALKER_STATE_TABLE) {
495 /*
496 * Continue walking if this entry has the same attributes as the last and
497 * is one page/block away -- it's a contiguous region.
498 */
499 if (attrs == last_attrs && _addr == last_addr + (1 << level2shift(level))) {
500 last_attrs = attrs;
501 last_addr = _addr;
502 continue;
503 } else {
504 /* We either hit a table or a new region */
505 exit = cb(entry_start, last_addr + (1 << level2shift(level)),
506 va_bits, level, priv);
507 if (exit)
508 return;
509 state[level] = WALKER_STATE_START;
510 }
511 }
512 last_attrs = attrs;
513 last_addr = _addr;
514
515 if (PTE_IS_TABLE(pte, level)) {
516 /* After the end of the table might be corrupted data */
517 if (!_addr || (pte & 0xfff) > 0x3ff)
518 return;
519 state[level] = WALKER_STATE_TABLE;
520 /* Signify the start of a table */
521 exit = cb(pte, 0, va_bits, level, priv);
522 if (exit)
523 return;
524
525 /* Go down a level */
526 __pagetable_walk(_addr, tcr, level + 1, cb, priv);
527 state[level] = WALKER_STATE_START;
528 } else if (pte_type(&pte) == PTE_TYPE_BLOCK || pte_type(&pte) == PTE_TYPE_PAGE) {
529 /* We foud a block or page, start walking */
530 entry_start = pte;
531 state[level] = WALKER_STATE_REGION;
532 }
533 }
534
535 if (state[level] > WALKER_STATE_START)
536 exit = cb(entry_start, last_addr + (1 << level2shift(level)), va_bits, level, priv);
537}
538
539static void pretty_print_pte_type(u64 pte)
540{
541 switch (pte_type(&pte)) {
542 case PTE_TYPE_FAULT:
543 printf(" %-5s", "Fault");
544 break;
545 case PTE_TYPE_BLOCK:
546 printf(" %-5s", "Block");
547 break;
548 case PTE_TYPE_PAGE:
549 printf(" %-5s", "Pages");
550 break;
551 default:
552 printf(" %-5s", "Unk");
553 }
554}
555
556static void pretty_print_table_attrs(u64 pte)
557{
558 int ap = (pte & PTE_TABLE_AP) >> 61;
559
560 printf(" | %2s %10s",
561 (ap & 2) ? "RO" : "",
562 (ap & 1) ? "!EL0" : "");
563 printf(" | %3s %2s %2s",
564 (pte & PTE_TABLE_PXN) ? "PXN" : "",
565 (pte & PTE_TABLE_XN) ? "XN" : "",
566 (pte & PTE_TABLE_NS) ? "NS" : "");
567}
568
569static void pretty_print_block_attrs(u64 pte)
570{
571 u64 attrs = pte & PMD_ATTRINDX_MASK;
Ilias Apalodimasb3c5d252025-02-20 15:54:38 +0200572 u64 perm_attrs = pte & PMD_ATTRMASK;
573 char mem_attrs[16] = { 0 };
574 int cnt = 0;
575
576 if (perm_attrs & PTE_BLOCK_PXN)
577 cnt += snprintf(mem_attrs + cnt, sizeof(mem_attrs) - cnt, "PXN ");
Ilias Apalodimasebd7e542025-03-19 09:22:02 +0200578 if (perm_attrs & PTE_BLOCK_UXN) {
579 if (get_effective_el() == 1)
580 cnt += snprintf(mem_attrs + cnt, sizeof(mem_attrs) - cnt, "UXN ");
581 else
582 cnt += snprintf(mem_attrs + cnt, sizeof(mem_attrs) - cnt, "XN ");
583 }
Ilias Apalodimasb3c5d252025-02-20 15:54:38 +0200584 if (perm_attrs & PTE_BLOCK_RO)
585 cnt += snprintf(mem_attrs + cnt, sizeof(mem_attrs) - cnt, "RO");
586 if (!mem_attrs[0])
587 snprintf(mem_attrs, sizeof(mem_attrs), "RWX ");
588
589 printf(" | %-10s", mem_attrs);
Caleb Connolly566907c2024-06-17 10:03:48 +0200590
591 switch (attrs) {
592 case PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE):
593 printf(" | %-13s", "Device-nGnRnE");
594 break;
595 case PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE):
596 printf(" | %-13s", "Device-nGnRE");
597 break;
598 case PTE_BLOCK_MEMTYPE(MT_DEVICE_GRE):
599 printf(" | %-13s", "Device-GRE");
600 break;
601 case PTE_BLOCK_MEMTYPE(MT_NORMAL_NC):
602 printf(" | %-13s", "Normal-NC");
603 break;
604 case PTE_BLOCK_MEMTYPE(MT_NORMAL):
605 printf(" | %-13s", "Normal");
606 break;
607 default:
608 printf(" | %-13s", "Unknown");
609 }
610}
611
612static void pretty_print_block_memtype(u64 pte)
613{
614 u64 share = pte & (3 << 8);
615
616 switch (share) {
617 case PTE_BLOCK_NON_SHARE:
618 printf(" | %-16s", "Non-shareable");
619 break;
620 case PTE_BLOCK_OUTER_SHARE:
621 printf(" | %-16s", "Outer-shareable");
622 break;
623 case PTE_BLOCK_INNER_SHARE:
624 printf(" | %-16s", "Inner-shareable");
625 break;
626 default:
627 printf(" | %-16s", "Unknown");
628 }
629}
630
631static void print_pte(u64 pte, int level)
632{
633 if (PTE_IS_TABLE(pte, level)) {
634 printf(" %-5s", "Table");
Ilias Apalodimasb3c5d252025-02-20 15:54:38 +0200635 printf(" %-12s", "|");
Caleb Connolly566907c2024-06-17 10:03:48 +0200636 pretty_print_table_attrs(pte);
637 } else {
638 pretty_print_pte_type(pte);
639 pretty_print_block_attrs(pte);
640 pretty_print_block_memtype(pte);
641 }
642 printf("\n");
643}
644
645/**
646 * pagetable_print_entry() - Callback function to print a single pagetable region
647 *
648 * This is the default callback used by @dump_pagetable(). It does some basic pretty
649 * printing (see example in the U-Boot arm64 documentation). It can be replaced by
650 * a custom callback function if more detailed information is needed.
651 *
652 * @start_attrs: The start address and attributes of the region (or table address)
653 * @end: The end address of the region (or 0 if it's a table)
654 * @va_bits: The number of bits used for the virtual address
655 * @level: The level of the region
656 * @priv: Private data for the callback (unused)
657 */
658static bool pagetable_print_entry(u64 start_attrs, u64 end, int va_bits, int level, void *priv)
659{
660 u64 _addr = start_attrs & GENMASK_ULL(va_bits, PAGE_SHIFT);
661 int indent = va_bits < 39 ? level - 1 : level;
662
663 printf("%*s", indent * 2, "");
664 if (PTE_IS_TABLE(start_attrs, level))
Ilias Apalodimasb3c5d252025-02-20 15:54:38 +0200665 printf("[%#016llx]%19s", _addr, "");
Caleb Connolly566907c2024-06-17 10:03:48 +0200666 else
Ilias Apalodimasb3c5d252025-02-20 15:54:38 +0200667 printf("[%#016llx - %#016llx]", _addr, end);
Caleb Connolly566907c2024-06-17 10:03:48 +0200668
669 printf("%*s | ", (3 - level) * 2, "");
670 print_pte(start_attrs, level);
671
672 return false;
673}
674
675void walk_pagetable(u64 ttbr, u64 tcr, pte_walker_cb_t cb, void *priv)
676{
677 __pagetable_walk(ttbr, tcr, 0, cb, priv);
678}
679
680void dump_pagetable(u64 ttbr, u64 tcr)
681{
682 u64 va_bits = 64 - (tcr & (BIT(6) - 1));
683
684 printf("Walking pagetable at %p, va_bits: %lld. Using %d levels\n", (void *)ttbr,
685 va_bits, va_bits < 39 ? 3 : 4);
686 walk_pagetable(ttbr, tcr, pagetable_print_entry, NULL);
687}
688
Alexander Grafe317fe82016-03-04 01:09:47 +0100689/* Returns the estimated required size of all page tables */
Alexander Grafbc78b922016-03-21 20:26:12 +0100690__weak u64 get_page_table_size(void)
Alexander Grafe317fe82016-03-04 01:09:47 +0100691{
692 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
Chris Packhama6c68c62023-10-27 13:23:54 +1300693 u64 size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100694
695 /* Account for all page tables we would need to cover our memory map */
Marc Zyngier6da328e2023-02-14 21:38:14 +0800696 size = one_pt * count_ranges();
Alexander Grafe317fe82016-03-04 01:09:47 +0100697
698 /*
699 * We need to duplicate our page table once to have an emergency pt to
700 * resort to when splitting page tables later on
701 */
702 size *= 2;
703
704 /*
705 * We may need to split page tables later on if dcache settings change,
706 * so reserve up to 4 (random pick) page tables for that.
707 */
708 size += one_pt * 4;
709
710 return size;
711}
712
York Suna81fcd12016-06-24 16:46:20 -0700713void setup_pgtables(void)
Alexander Grafe317fe82016-03-04 01:09:47 +0100714{
715 int i;
716
York Suna81fcd12016-06-24 16:46:20 -0700717 if (!gd->arch.tlb_fillptr || !gd->arch.tlb_addr)
718 panic("Page table pointer not setup.");
719
Alexander Grafe317fe82016-03-04 01:09:47 +0100720 /*
721 * Allocate the first level we're on with invalidate entries.
722 * If the starting level is 0 (va_bits >= 39), then this is our
723 * Lv0 page table, otherwise it's the entry Lv1 page table.
724 */
725 create_table();
726
727 /* Now add all MMU table entries one after another to the table */
Alexander Graf6b3e7ca2016-03-04 01:09:48 +0100728 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
Alexander Grafe317fe82016-03-04 01:09:47 +0100729 add_map(&mem_map[i]);
Alexander Grafe317fe82016-03-04 01:09:47 +0100730}
731
732static void setup_all_pgtables(void)
733{
734 u64 tlb_addr = gd->arch.tlb_addr;
Alexander Graffa3754e2016-07-30 23:13:03 +0200735 u64 tlb_size = gd->arch.tlb_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100736
737 /* Reset the fill ptr */
738 gd->arch.tlb_fillptr = tlb_addr;
739
740 /* Create normal system page tables */
741 setup_pgtables();
742
743 /* Create emergency page tables */
Alexander Graffa3754e2016-07-30 23:13:03 +0200744 gd->arch.tlb_size -= (uintptr_t)gd->arch.tlb_fillptr -
745 (uintptr_t)gd->arch.tlb_addr;
Alexander Grafe317fe82016-03-04 01:09:47 +0100746 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
747 setup_pgtables();
748 gd->arch.tlb_emerg = gd->arch.tlb_addr;
749 gd->arch.tlb_addr = tlb_addr;
Alexander Graffa3754e2016-07-30 23:13:03 +0200750 gd->arch.tlb_size = tlb_size;
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700751}
752
David Feng85fd5f12013-12-14 11:47:35 +0800753/* to activate the MMU we need to set up virtual memory */
Stephen Warren7333c6a2015-10-05 12:09:00 -0600754__weak void mmu_setup(void)
David Feng85fd5f12013-12-14 11:47:35 +0800755{
Thierry Reding59c364d2015-07-22 17:10:11 -0600756 int el;
David Feng85fd5f12013-12-14 11:47:35 +0800757
Alexander Grafe317fe82016-03-04 01:09:47 +0100758 /* Set up page tables only once */
759 if (!gd->arch.tlb_fillptr)
760 setup_all_pgtables();
Alexander Graffb74cc12016-03-04 01:09:45 +0100761
762 el = current_el();
Andre Przywara630a7942022-06-14 00:11:10 +0100763 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(NULL, NULL),
Alexander Graffb74cc12016-03-04 01:09:45 +0100764 MEMORY_ATTRIBUTES);
Alexander Graffb74cc12016-03-04 01:09:45 +0100765
David Feng85fd5f12013-12-14 11:47:35 +0800766 /* enable the mmu */
767 set_sctlr(get_sctlr() | CR_M);
768}
769
770/*
771 * Performs a invalidation of the entire data cache at all levels
772 */
773void invalidate_dcache_all(void)
774{
Marc Zyngierb67855c2023-02-09 04:54:27 +0800775#ifndef CONFIG_CMO_BY_VA_ONLY
York Sunef042012014-02-26 13:26:04 -0800776 __asm_invalidate_dcache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600777 __asm_invalidate_l3_dcache();
Marc Zyngierb67855c2023-02-09 04:54:27 +0800778#else
779 apply_cmo_to_mappings(invalidate_dcache_range);
780#endif
David Feng85fd5f12013-12-14 11:47:35 +0800781}
782
783/*
York Sun1ce575f2015-01-06 13:18:42 -0800784 * Performs a clean & invalidation of the entire data cache at all levels.
785 * This function needs to be inline to avoid using stack.
Stephen Warrenddb0f632016-10-19 15:18:46 -0600786 * __asm_flush_l3_dcache return status of timeout
David Feng85fd5f12013-12-14 11:47:35 +0800787 */
York Sun1ce575f2015-01-06 13:18:42 -0800788inline void flush_dcache_all(void)
David Feng85fd5f12013-12-14 11:47:35 +0800789{
Marc Zyngierb67855c2023-02-09 04:54:27 +0800790#ifndef CONFIG_CMO_BY_VA_ONLY
York Sun1ce575f2015-01-06 13:18:42 -0800791 int ret;
792
David Feng85fd5f12013-12-14 11:47:35 +0800793 __asm_flush_dcache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600794 ret = __asm_flush_l3_dcache();
York Sun1ce575f2015-01-06 13:18:42 -0800795 if (ret)
796 debug("flushing dcache returns 0x%x\n", ret);
797 else
798 debug("flushing dcache successfully.\n");
Marc Zyngierb67855c2023-02-09 04:54:27 +0800799#else
800 apply_cmo_to_mappings(flush_dcache_range);
801#endif
David Feng85fd5f12013-12-14 11:47:35 +0800802}
803
Vignesh Raghavendra384c1412019-04-22 21:43:32 +0530804#ifndef CONFIG_SYS_DISABLE_DCACHE_OPS
David Feng85fd5f12013-12-14 11:47:35 +0800805/*
806 * Invalidates range in all levels of D-cache/unified cache
807 */
808void invalidate_dcache_range(unsigned long start, unsigned long stop)
809{
Simon Glass4415c3b2017-04-05 17:53:18 -0600810 __asm_invalidate_dcache_range(start, stop);
David Feng85fd5f12013-12-14 11:47:35 +0800811}
812
813/*
814 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
815 */
816void flush_dcache_range(unsigned long start, unsigned long stop)
817{
818 __asm_flush_dcache_range(start, stop);
819}
Vignesh Raghavendra384c1412019-04-22 21:43:32 +0530820#else
821void invalidate_dcache_range(unsigned long start, unsigned long stop)
822{
823}
824
825void flush_dcache_range(unsigned long start, unsigned long stop)
826{
827}
828#endif /* CONFIG_SYS_DISABLE_DCACHE_OPS */
David Feng85fd5f12013-12-14 11:47:35 +0800829
830void dcache_enable(void)
831{
832 /* The data cache is not active unless the mmu is enabled */
833 if (!(get_sctlr() & CR_M)) {
834 invalidate_dcache_all();
835 __asm_invalidate_tlb_all();
836 mmu_setup();
837 }
838
Pali Rohárfbddaee2022-09-14 13:37:46 +0200839 /* Set up page tables only once (it is done also by mmu_setup()) */
840 if (!gd->arch.tlb_fillptr)
841 setup_all_pgtables();
842
David Feng85fd5f12013-12-14 11:47:35 +0800843 set_sctlr(get_sctlr() | CR_C);
844}
845
846void dcache_disable(void)
847{
Sam Protsenkoe60b24e2024-11-06 20:58:30 -0600848 unsigned long sctlr;
David Feng85fd5f12013-12-14 11:47:35 +0800849
850 sctlr = get_sctlr();
851
852 /* if cache isn't enabled no need to disable */
853 if (!(sctlr & CR_C))
854 return;
855
Marc Zyngierb67855c2023-02-09 04:54:27 +0800856 if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
857 /*
858 * When invalidating by VA, do it *before* turning the MMU
859 * off, so that at least our stack is coherent.
860 */
861 flush_dcache_all();
862 }
863
David Feng85fd5f12013-12-14 11:47:35 +0800864 set_sctlr(sctlr & ~(CR_C|CR_M));
865
Marc Zyngierb67855c2023-02-09 04:54:27 +0800866 if (!IS_ENABLED(CONFIG_CMO_BY_VA_ONLY))
867 flush_dcache_all();
868
David Feng85fd5f12013-12-14 11:47:35 +0800869 __asm_invalidate_tlb_all();
870}
871
872int dcache_status(void)
873{
874 return (get_sctlr() & CR_C) != 0;
875}
876
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530877u64 *__weak arch_get_page_table(void) {
878 puts("No page table offset defined\n");
879
880 return NULL;
881}
882
Alexander Grafe317fe82016-03-04 01:09:47 +0100883static bool is_aligned(u64 addr, u64 size, u64 align)
884{
885 return !(addr & (align - 1)) && !(size & (align - 1));
886}
887
York Sun5bb14e02017-03-06 09:02:33 -0800888/* Use flag to indicate if attrs has more than d-cache attributes */
889static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level)
Alexander Grafe317fe82016-03-04 01:09:47 +0100890{
891 int levelshift = level2shift(level);
892 u64 levelsize = 1ULL << levelshift;
893 u64 *pte = find_pte(start, level);
894
895 /* Can we can just modify the current level block PTE? */
896 if (is_aligned(start, size, levelsize)) {
York Sun5bb14e02017-03-06 09:02:33 -0800897 if (flag) {
898 *pte &= ~PMD_ATTRMASK;
899 *pte |= attrs & PMD_ATTRMASK;
900 } else {
901 *pte &= ~PMD_ATTRINDX_MASK;
902 *pte |= attrs & PMD_ATTRINDX_MASK;
903 }
Alexander Grafe317fe82016-03-04 01:09:47 +0100904 debug("Set attrs=%llx pte=%p level=%d\n", attrs, pte, level);
905
906 return levelsize;
907 }
908
909 /* Unaligned or doesn't fit, maybe split block into table */
910 debug("addr=%llx level=%d pte=%p (%llx)\n", start, level, pte, *pte);
911
912 /* Maybe we need to split the block into a table */
913 if (pte_type(pte) == PTE_TYPE_BLOCK)
914 split_block(pte, level);
915
916 /* And then double-check it became a table or already is one */
917 if (pte_type(pte) != PTE_TYPE_TABLE)
918 panic("PTE %p (%llx) for addr=%llx should be a table",
919 pte, *pte, start);
920
921 /* Roll on to the next page table level */
922 return 0;
923}
924
925void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
926 enum dcache_option option)
927{
Peng Fan41bad3e2020-05-11 16:41:07 +0800928 u64 attrs = PMD_ATTRINDX(option >> 2);
Alexander Grafe317fe82016-03-04 01:09:47 +0100929 u64 real_start = start;
930 u64 real_size = size;
931
932 debug("start=%lx size=%lx\n", (ulong)start, (ulong)size);
933
York Suna81fcd12016-06-24 16:46:20 -0700934 if (!gd->arch.tlb_emerg)
935 panic("Emergency page table not setup.");
936
Alexander Grafe317fe82016-03-04 01:09:47 +0100937 /*
938 * We can not modify page tables that we're currently running on,
939 * so we first need to switch to the "emergency" page tables where
940 * we can safely modify our primary page tables and then switch back
941 */
942 __asm_switch_ttbr(gd->arch.tlb_emerg);
943
944 /*
945 * Loop through the address range until we find a page granule that fits
946 * our alignment constraints, then set it to the new cache attributes
947 */
948 while (size > 0) {
949 int level;
950 u64 r;
951
952 for (level = 1; level < 4; level++) {
York Sun5bb14e02017-03-06 09:02:33 -0800953 /* Set d-cache attributes only */
954 r = set_one_region(start, size, attrs, false, level);
Alexander Grafe317fe82016-03-04 01:09:47 +0100955 if (r) {
956 /* PTE successfully replaced */
957 size -= r;
958 start += r;
959 break;
960 }
961 }
962
963 }
964
965 /* We're done modifying page tables, switch back to our primary ones */
966 __asm_switch_ttbr(gd->arch.tlb_addr);
967
968 /*
969 * Make sure there's nothing stale in dcache for a region that might
970 * have caches off now
971 */
972 flush_dcache_range(real_start, real_start + real_size);
973}
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700974
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +0200975void mmu_change_region_attr_nobreak(phys_addr_t addr, size_t siz, u64 attrs)
York Sun5bb14e02017-03-06 09:02:33 -0800976{
977 int level;
978 u64 r, size, start;
979
York Sun5bb14e02017-03-06 09:02:33 -0800980 /*
981 * Loop through the address range until we find a page granule that fits
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +0200982 * our alignment constraints and set the new permissions
York Sun5bb14e02017-03-06 09:02:33 -0800983 */
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +0200984 start = addr;
985 size = siz;
York Sun5bb14e02017-03-06 09:02:33 -0800986 while (size > 0) {
987 for (level = 1; level < 4; level++) {
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +0200988 /* Set PTE to new attributes */
989 r = set_one_region(start, size, attrs, true, level);
York Sun5bb14e02017-03-06 09:02:33 -0800990 if (r) {
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +0200991 /* PTE successfully updated */
York Sun5bb14e02017-03-06 09:02:33 -0800992 size -= r;
993 start += r;
994 break;
995 }
996 }
997 }
York Sun5bb14e02017-03-06 09:02:33 -0800998 flush_dcache_range(gd->arch.tlb_addr,
999 gd->arch.tlb_addr + gd->arch.tlb_size);
1000 __asm_invalidate_tlb_all();
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +02001001}
York Sun5bb14e02017-03-06 09:02:33 -08001002
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +02001003/*
1004 * Modify MMU table for a region with updated PXN/UXN/Memory type/valid bits.
1005 * The procecess is break-before-make. The target region will be marked as
1006 * invalid during the process of changing.
1007 */
1008void mmu_change_region_attr(phys_addr_t addr, size_t siz, u64 attrs)
1009{
1010 int level;
1011 u64 r, size, start;
1012
1013 start = addr;
1014 size = siz;
York Sun5bb14e02017-03-06 09:02:33 -08001015 /*
1016 * Loop through the address range until we find a page granule that fits
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +02001017 * our alignment constraints, then set it to "invalid".
York Sun5bb14e02017-03-06 09:02:33 -08001018 */
York Sun5bb14e02017-03-06 09:02:33 -08001019 while (size > 0) {
1020 for (level = 1; level < 4; level++) {
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +02001021 /* Set PTE to fault */
1022 r = set_one_region(start, size, PTE_TYPE_FAULT, true,
1023 level);
York Sun5bb14e02017-03-06 09:02:33 -08001024 if (r) {
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +02001025 /* PTE successfully invalidated */
York Sun5bb14e02017-03-06 09:02:33 -08001026 size -= r;
1027 start += r;
1028 break;
1029 }
1030 }
1031 }
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +02001032
York Sun5bb14e02017-03-06 09:02:33 -08001033 flush_dcache_range(gd->arch.tlb_addr,
1034 gd->arch.tlb_addr + gd->arch.tlb_size);
1035 __asm_invalidate_tlb_all();
Ilias Apalodimasa6db58f2025-02-20 15:54:41 +02001036
1037 mmu_change_region_attr_nobreak(addr, siz, attrs);
York Sun5bb14e02017-03-06 09:02:33 -08001038}
1039
Ilias Apalodimase9e18652025-02-20 15:54:42 +02001040int pgprot_set_attrs(phys_addr_t addr, size_t size, enum pgprot_attrs perm)
1041{
1042 u64 attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_INNER_SHARE | PTE_TYPE_VALID;
1043
1044 switch (perm) {
1045 case MMU_ATTR_RO:
Ilias Apalodimasebd7e542025-03-19 09:22:02 +02001046 /*
1047 * get_effective_el() will return 1 if
1048 * - Running in EL1 so we assume an EL1 translation regime
1049 * with HCR_EL2.{NV, NV1} != {1,1}
1050 * - Running in EL2 with HCR_EL2.E2H = 1 so we assume an
1051 * EL2&0 translation regime. Since we don't have accesses
1052 * from EL0 we don't have to check HCR_EL2.TGE
1053 *
1054 * Both of these requires PXN to be set
1055 */
1056 if (get_effective_el() == 1)
1057 attrs |= PTE_BLOCK_PXN | PTE_BLOCK_UXN | PTE_BLOCK_RO;
1058 else
1059 attrs |= PTE_BLOCK_UXN | PTE_BLOCK_RO;
Ilias Apalodimase9e18652025-02-20 15:54:42 +02001060 break;
1061 case MMU_ATTR_RX:
1062 attrs |= PTE_BLOCK_RO;
1063 break;
1064 case MMU_ATTR_RW:
Ilias Apalodimasebd7e542025-03-19 09:22:02 +02001065 if (get_effective_el() == 1)
1066 attrs |= PTE_BLOCK_PXN | PTE_BLOCK_UXN;
1067 else
1068 attrs |= PTE_BLOCK_UXN;
Ilias Apalodimase9e18652025-02-20 15:54:42 +02001069 break;
1070 default:
1071 log_err("Unknown attribute %d\n", perm);
1072 return -EINVAL;
1073 }
1074
1075 mmu_change_region_attr_nobreak(addr, size, attrs);
1076
1077 return 0;
1078}
1079
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001080#else /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +08001081
Alexander Grafbc40da92016-03-04 01:09:55 +01001082/*
1083 * For SPL builds, we may want to not have dcache enabled. Any real U-Boot
1084 * running however really wants to have dcache and the MMU active. Check that
1085 * everything is sane and give the developer a hint if it isn't.
1086 */
Simon Glass85ed77d2024-09-29 19:49:46 -06001087#ifndef CONFIG_XPL_BUILD
Alexander Grafbc40da92016-03-04 01:09:55 +01001088#error Please describe your MMU layout in CONFIG_SYS_MEM_MAP and enable dcache.
1089#endif
1090
David Feng85fd5f12013-12-14 11:47:35 +08001091void invalidate_dcache_all(void)
1092{
1093}
1094
1095void flush_dcache_all(void)
1096{
1097}
1098
David Feng85fd5f12013-12-14 11:47:35 +08001099void dcache_enable(void)
1100{
1101}
1102
1103void dcache_disable(void)
1104{
1105}
1106
1107int dcache_status(void)
1108{
1109 return 0;
1110}
1111
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +05301112void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
1113 enum dcache_option option)
1114{
1115}
1116
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001117#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +08001118
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001119#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
David Feng85fd5f12013-12-14 11:47:35 +08001120
1121void icache_enable(void)
1122{
Stephen Warrenddb0f632016-10-19 15:18:46 -06001123 invalidate_icache_all();
David Feng85fd5f12013-12-14 11:47:35 +08001124 set_sctlr(get_sctlr() | CR_I);
1125}
1126
1127void icache_disable(void)
1128{
1129 set_sctlr(get_sctlr() & ~CR_I);
1130}
1131
1132int icache_status(void)
1133{
1134 return (get_sctlr() & CR_I) != 0;
1135}
1136
Patrice Chotardee435c62021-07-19 11:21:51 +02001137int mmu_status(void)
1138{
1139 return (get_sctlr() & CR_M) != 0;
1140}
1141
David Feng85fd5f12013-12-14 11:47:35 +08001142void invalidate_icache_all(void)
1143{
1144 __asm_invalidate_icache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -06001145 __asm_invalidate_l3_icache();
David Feng85fd5f12013-12-14 11:47:35 +08001146}
1147
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001148#else /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +08001149
1150void icache_enable(void)
1151{
1152}
1153
1154void icache_disable(void)
1155{
1156}
1157
1158int icache_status(void)
1159{
1160 return 0;
1161}
1162
Patrice Chotardee435c62021-07-19 11:21:51 +02001163int mmu_status(void)
1164{
1165 return 0;
1166}
1167
David Feng85fd5f12013-12-14 11:47:35 +08001168void invalidate_icache_all(void)
1169{
1170}
1171
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001172#endif /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +08001173
1174/*
1175 * Enable dCache & iCache, whether cache is actually enabled
1176 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
1177 */
York Suna84cd722014-06-23 15:15:54 -07001178void __weak enable_caches(void)
David Feng85fd5f12013-12-14 11:47:35 +08001179{
1180 icache_enable();
1181 dcache_enable();
1182}
Ilias Apalodimasb3c5d252025-02-20 15:54:38 +02001183
1184void arch_dump_mem_attrs(void)
1185{
1186 dump_pagetable(gd->arch.tlb_addr, get_tcr(NULL, NULL));
1187}