blob: 631d9efa5e10b2e126f25618e2cf52f139a942ad [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>
17
18DECLARE_GLOBAL_DATA_PTR;
19
Trevor Woerner43ec7e02019-05-03 09:41:00 -040020#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -070021
Alexander Grafe317fe82016-03-04 01:09:47 +010022/*
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 Temerkhanov78eaa492015-10-14 09:55:45 -070040
Andre Przywara630a7942022-06-14 00:11:10 +010041static int get_effective_el(void)
Alexander Graffb74cc12016-03-04 01:09:45 +010042{
Andre Przywara630a7942022-06-14 00:11:10 +010043 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
60u64 get_tcr(u64 *pips, u64 *pva_bits)
61{
62 int el = get_effective_el();
Alexander Graffb74cc12016-03-04 01:09:45 +010063 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 Graf6b3e7ca2016-03-04 01:09:48 +010069 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
York Sunc7104e52016-06-24 16:46:22 -070070 max_addr = max(max_addr, mem_map[i].virt + mem_map[i].size);
Alexander Graffb74cc12016-03-04 01:09:45 +010071
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 Graff03c0e42016-03-04 01:09:46 +010094 tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE;
Alexander Graffb74cc12016-03-04 01:09:45 +010095 } 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 Grafe317fe82016-03-04 01:09:47 +0100102 tcr |= TCR_TG0_4K | TCR_SHARED_INNER | TCR_ORGN_WBWA | TCR_IRGN_WBWA;
103 tcr |= TCR_T0SZ(va_bits);
Alexander Graffb74cc12016-03-04 01:09:45 +0100104
105 if (pips)
106 *pips = ips;
107 if (pva_bits)
108 *pva_bits = va_bits;
109
110 return tcr;
111}
112
Alexander Grafe317fe82016-03-04 01:09:47 +0100113#define MAX_PTE_ENTRIES 512
114
115static 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> */
121static int level2shift(int level)
122{
123 /* Page is 12 bits wide, every level translates 9 bits */
124 return (12 + 9 * (3 - level));
125}
126
127static 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 Przywara630a7942022-06-14 00:11:10 +0100137 get_tcr(NULL, &va_bits);
Alexander Grafe317fe82016-03-04 01:09:47 +0100138 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 Zyngierb67855c2023-02-09 04:54:27 +0800165#ifdef CONFIG_CMO_BY_VA_ONLY
166static 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
224static 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
239static inline void apply_cmo_to_mappings(void *dummy) {}
240#endif
241
Alexander Grafe317fe82016-03-04 01:09:47 +0100242/* Returns and creates a new full table (512 entries) */
243static 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
263static 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 Sunf44afe72016-06-24 16:46:21 -0700270/* Splits a block PTE into table with subpages spanning the old block */
271static 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 Zyngierfeb0ec22023-02-14 21:38:13 +0800301static void map_range(u64 virt, u64 phys, u64 size, int level,
302 u64 *table, u64 attrs)
Alexander Grafe317fe82016-03-04 01:09:47 +0100303{
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800304 u64 map_size = BIT_ULL(level2shift(level));
305 int i, idx;
Alexander Grafe317fe82016-03-04 01:09:47 +0100306
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800307 idx = (virt >> level2shift(level)) & (MAX_PTE_ENTRIES - 1);
308 for (i = idx; size; i++) {
309 u64 next_size, *next_table;
Alexander Grafe317fe82016-03-04 01:09:47 +0100310
Chris Packham978814f2023-10-27 13:23:53 +1300311 if (level >= 1 &&
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800312 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 Sunc7104e52016-06-24 16:46:22 -0700317
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800318 virt += map_size;
319 phys += map_size;
320 size -= map_size;
321
322 continue;
Alexander Grafe317fe82016-03-04 01:09:47 +0100323 }
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800324
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 Tosid8ceb202024-03-18 19:35:49 +0000328 else if (pte_type(&table[i]) != PTE_TYPE_TABLE)
329 split_block(&table[i], level);
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800330
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 Grafe317fe82016-03-04 01:09:47 +0100339 }
340}
341
Caleb Connolly27b05b52024-08-09 01:59:31 +0200342void mmu_map_region(phys_addr_t addr, u64 size, bool emergency)
343{
344 u64 va_bits;
345 int level = 0;
346 u64 attrs = PTE_BLOCK_MEMTYPE(MT_NORMAL) | PTE_BLOCK_INNER_SHARE;
347
348 attrs |= PTE_TYPE_BLOCK | PTE_BLOCK_AF;
349
350 get_tcr(NULL, &va_bits);
351 if (va_bits < 39)
352 level = 1;
353
354 if (emergency)
355 map_range(addr, addr, size, level,
356 (u64 *)gd->arch.tlb_emerg, attrs);
357
358 /* Switch pagetables while we update the primary one */
359 __asm_switch_ttbr(gd->arch.tlb_emerg);
360
361 map_range(addr, addr, size, level,
362 (u64 *)gd->arch.tlb_addr, attrs);
363
364 __asm_switch_ttbr(gd->arch.tlb_addr);
365}
366
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800367static void add_map(struct mm_region *map)
368{
369 u64 attrs = map->attrs | PTE_TYPE_BLOCK | PTE_BLOCK_AF;
370 u64 va_bits;
371 int level = 0;
372
373 get_tcr(NULL, &va_bits);
374 if (va_bits < 39)
375 level = 1;
376
377 map_range(map->virt, map->phys, map->size, level,
378 (u64 *)gd->arch.tlb_addr, attrs);
379}
380
Marc Zyngier6da328e2023-02-14 21:38:14 +0800381static void count_range(u64 virt, u64 size, int level, int *cntp)
Alexander Grafe317fe82016-03-04 01:09:47 +0100382{
Marc Zyngier6da328e2023-02-14 21:38:14 +0800383 u64 map_size = BIT_ULL(level2shift(level));
384 int i, idx;
Alexander Grafe317fe82016-03-04 01:09:47 +0100385
Marc Zyngier6da328e2023-02-14 21:38:14 +0800386 idx = (virt >> level2shift(level)) & (MAX_PTE_ENTRIES - 1);
387 for (i = idx; size; i++) {
388 u64 next_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100389
Chris Packham978814f2023-10-27 13:23:53 +1300390 if (level >= 1 &&
Marc Zyngier6da328e2023-02-14 21:38:14 +0800391 size >= map_size && !(virt & (map_size - 1))) {
392 virt += map_size;
393 size -= map_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100394
Marc Zyngier6da328e2023-02-14 21:38:14 +0800395 continue;
396 }
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700397
Marc Zyngier6da328e2023-02-14 21:38:14 +0800398 /* Going one level down */
399 (*cntp)++;
400 next_size = min(map_size - (virt & (map_size - 1)), size);
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700401
Marc Zyngier6da328e2023-02-14 21:38:14 +0800402 count_range(virt, next_size, level + 1, cntp);
Alexander Grafe317fe82016-03-04 01:09:47 +0100403
Marc Zyngier6da328e2023-02-14 21:38:14 +0800404 virt += next_size;
405 size -= next_size;
406 }
407}
Alexander Grafe317fe82016-03-04 01:09:47 +0100408
Marc Zyngier6da328e2023-02-14 21:38:14 +0800409static int count_ranges(void)
410{
411 int i, count = 0, level = 0;
412 u64 va_bits;
Alexander Grafe317fe82016-03-04 01:09:47 +0100413
Marc Zyngier6da328e2023-02-14 21:38:14 +0800414 get_tcr(NULL, &va_bits);
415 if (va_bits < 39)
416 level = 1;
Alexander Grafe317fe82016-03-04 01:09:47 +0100417
Marc Zyngier6da328e2023-02-14 21:38:14 +0800418 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
419 count_range(mem_map[i].virt, mem_map[i].size, level, &count);
Alexander Grafe317fe82016-03-04 01:09:47 +0100420
Marc Zyngier6da328e2023-02-14 21:38:14 +0800421 return count;
Alexander Grafe317fe82016-03-04 01:09:47 +0100422}
423
Caleb Connolly566907c2024-06-17 10:03:48 +0200424#define ALL_ATTRS (3 << 8 | PMD_ATTRINDX_MASK)
425#define PTE_IS_TABLE(pte, level) (pte_type(&(pte)) == PTE_TYPE_TABLE && (level) < 3)
426
427enum walker_state {
428 WALKER_STATE_START = 0,
429 WALKER_STATE_TABLE,
430 WALKER_STATE_REGION, /* block or page, depending on level */
431};
432
433
434/**
435 * __pagetable_walk() - Walk through the pagetable and call cb() for each memory region
436 *
437 * This is a software implementation of the ARMv8-A MMU translation table walk. As per
438 * section D5.4 of the ARMv8-A Architecture Reference Manual. It recursively walks the
439 * 4 or 3 levels of the page table and calls the callback function for each discrete
440 * region of memory (that being the discovery of a new table, a collection of blocks
441 * with the same attributes, or of pages with the same attributes).
442 *
443 * U-Boot picks the smallest number of virtual address (VA) bits that it can based on the
444 * memory map configured by the board. If this is less than 39 then the MMU will only use
445 * 3 levels of translation instead of 3 - skipping level 0.
446 *
447 * Each level has 512 entries of 64-bits each. Each entry includes attribute bits and
448 * an address. When the attribute bits indicate a table, the address is the physical
449 * address of the table, so we can recursively call _pagetable_walk() on it (after calling
450 * @cb). If instead they indicate a block or page, we record the start address and attributes
451 * and continue walking until we find a region with different attributes, or the end of the
452 * table, in either case we call @cb with the start and end address of the region.
453 *
454 * This approach can be used to fully emulate the MMU's translation table walk, as per
455 * Figure D5-25 of the ARMv8-A Architecture Reference Manual.
456 *
457 * @addr: The address of the table to walk
458 * @tcr: The TCR register value
459 * @level: The current level of the table
460 * @cb: The callback function to call for each region
461 * @priv: Private data to pass to the callback function
462 */
463static void __pagetable_walk(u64 addr, u64 tcr, int level, pte_walker_cb_t cb, void *priv)
464{
465 u64 *table = (u64 *)addr;
466 u64 attrs, last_attrs = 0, last_addr = 0, entry_start = 0;
467 int i;
468 u64 va_bits = 64 - (tcr & (BIT(6) - 1));
469 static enum walker_state state[4] = { 0 };
470 static bool exit;
471
472 if (!level) {
473 exit = false;
474 if (va_bits < 39)
475 level = 1;
476 }
477
478 state[level] = WALKER_STATE_START;
479
480 /* Walk through the table entries */
481 for (i = 0; i < MAX_PTE_ENTRIES; i++) {
482 u64 pte = table[i];
483 u64 _addr = pte & GENMASK_ULL(va_bits, PAGE_SHIFT);
484
485 if (exit)
486 return;
487
488 if (pte_type(&pte) == PTE_TYPE_FAULT)
489 continue;
490
491 attrs = pte & ALL_ATTRS;
492 /* If we're currently inside a block or set of pages */
493 if (state[level] > WALKER_STATE_START && state[level] != WALKER_STATE_TABLE) {
494 /*
495 * Continue walking if this entry has the same attributes as the last and
496 * is one page/block away -- it's a contiguous region.
497 */
498 if (attrs == last_attrs && _addr == last_addr + (1 << level2shift(level))) {
499 last_attrs = attrs;
500 last_addr = _addr;
501 continue;
502 } else {
503 /* We either hit a table or a new region */
504 exit = cb(entry_start, last_addr + (1 << level2shift(level)),
505 va_bits, level, priv);
506 if (exit)
507 return;
508 state[level] = WALKER_STATE_START;
509 }
510 }
511 last_attrs = attrs;
512 last_addr = _addr;
513
514 if (PTE_IS_TABLE(pte, level)) {
515 /* After the end of the table might be corrupted data */
516 if (!_addr || (pte & 0xfff) > 0x3ff)
517 return;
518 state[level] = WALKER_STATE_TABLE;
519 /* Signify the start of a table */
520 exit = cb(pte, 0, va_bits, level, priv);
521 if (exit)
522 return;
523
524 /* Go down a level */
525 __pagetable_walk(_addr, tcr, level + 1, cb, priv);
526 state[level] = WALKER_STATE_START;
527 } else if (pte_type(&pte) == PTE_TYPE_BLOCK || pte_type(&pte) == PTE_TYPE_PAGE) {
528 /* We foud a block or page, start walking */
529 entry_start = pte;
530 state[level] = WALKER_STATE_REGION;
531 }
532 }
533
534 if (state[level] > WALKER_STATE_START)
535 exit = cb(entry_start, last_addr + (1 << level2shift(level)), va_bits, level, priv);
536}
537
538static void pretty_print_pte_type(u64 pte)
539{
540 switch (pte_type(&pte)) {
541 case PTE_TYPE_FAULT:
542 printf(" %-5s", "Fault");
543 break;
544 case PTE_TYPE_BLOCK:
545 printf(" %-5s", "Block");
546 break;
547 case PTE_TYPE_PAGE:
548 printf(" %-5s", "Pages");
549 break;
550 default:
551 printf(" %-5s", "Unk");
552 }
553}
554
555static void pretty_print_table_attrs(u64 pte)
556{
557 int ap = (pte & PTE_TABLE_AP) >> 61;
558
559 printf(" | %2s %10s",
560 (ap & 2) ? "RO" : "",
561 (ap & 1) ? "!EL0" : "");
562 printf(" | %3s %2s %2s",
563 (pte & PTE_TABLE_PXN) ? "PXN" : "",
564 (pte & PTE_TABLE_XN) ? "XN" : "",
565 (pte & PTE_TABLE_NS) ? "NS" : "");
566}
567
568static void pretty_print_block_attrs(u64 pte)
569{
570 u64 attrs = pte & PMD_ATTRINDX_MASK;
571
572 switch (attrs) {
573 case PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRNE):
574 printf(" | %-13s", "Device-nGnRnE");
575 break;
576 case PTE_BLOCK_MEMTYPE(MT_DEVICE_NGNRE):
577 printf(" | %-13s", "Device-nGnRE");
578 break;
579 case PTE_BLOCK_MEMTYPE(MT_DEVICE_GRE):
580 printf(" | %-13s", "Device-GRE");
581 break;
582 case PTE_BLOCK_MEMTYPE(MT_NORMAL_NC):
583 printf(" | %-13s", "Normal-NC");
584 break;
585 case PTE_BLOCK_MEMTYPE(MT_NORMAL):
586 printf(" | %-13s", "Normal");
587 break;
588 default:
589 printf(" | %-13s", "Unknown");
590 }
591}
592
593static void pretty_print_block_memtype(u64 pte)
594{
595 u64 share = pte & (3 << 8);
596
597 switch (share) {
598 case PTE_BLOCK_NON_SHARE:
599 printf(" | %-16s", "Non-shareable");
600 break;
601 case PTE_BLOCK_OUTER_SHARE:
602 printf(" | %-16s", "Outer-shareable");
603 break;
604 case PTE_BLOCK_INNER_SHARE:
605 printf(" | %-16s", "Inner-shareable");
606 break;
607 default:
608 printf(" | %-16s", "Unknown");
609 }
610}
611
612static void print_pte(u64 pte, int level)
613{
614 if (PTE_IS_TABLE(pte, level)) {
615 printf(" %-5s", "Table");
616 pretty_print_table_attrs(pte);
617 } else {
618 pretty_print_pte_type(pte);
619 pretty_print_block_attrs(pte);
620 pretty_print_block_memtype(pte);
621 }
622 printf("\n");
623}
624
625/**
626 * pagetable_print_entry() - Callback function to print a single pagetable region
627 *
628 * This is the default callback used by @dump_pagetable(). It does some basic pretty
629 * printing (see example in the U-Boot arm64 documentation). It can be replaced by
630 * a custom callback function if more detailed information is needed.
631 *
632 * @start_attrs: The start address and attributes of the region (or table address)
633 * @end: The end address of the region (or 0 if it's a table)
634 * @va_bits: The number of bits used for the virtual address
635 * @level: The level of the region
636 * @priv: Private data for the callback (unused)
637 */
638static bool pagetable_print_entry(u64 start_attrs, u64 end, int va_bits, int level, void *priv)
639{
640 u64 _addr = start_attrs & GENMASK_ULL(va_bits, PAGE_SHIFT);
641 int indent = va_bits < 39 ? level - 1 : level;
642
643 printf("%*s", indent * 2, "");
644 if (PTE_IS_TABLE(start_attrs, level))
645 printf("[%#011llx]%14s", _addr, "");
646 else
647 printf("[%#011llx - %#011llx]", _addr, end);
648
649 printf("%*s | ", (3 - level) * 2, "");
650 print_pte(start_attrs, level);
651
652 return false;
653}
654
655void walk_pagetable(u64 ttbr, u64 tcr, pte_walker_cb_t cb, void *priv)
656{
657 __pagetable_walk(ttbr, tcr, 0, cb, priv);
658}
659
660void dump_pagetable(u64 ttbr, u64 tcr)
661{
662 u64 va_bits = 64 - (tcr & (BIT(6) - 1));
663
664 printf("Walking pagetable at %p, va_bits: %lld. Using %d levels\n", (void *)ttbr,
665 va_bits, va_bits < 39 ? 3 : 4);
666 walk_pagetable(ttbr, tcr, pagetable_print_entry, NULL);
667}
668
Alexander Grafe317fe82016-03-04 01:09:47 +0100669/* Returns the estimated required size of all page tables */
Alexander Grafbc78b922016-03-21 20:26:12 +0100670__weak u64 get_page_table_size(void)
Alexander Grafe317fe82016-03-04 01:09:47 +0100671{
672 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
Chris Packhama6c68c62023-10-27 13:23:54 +1300673 u64 size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100674
675 /* Account for all page tables we would need to cover our memory map */
Marc Zyngier6da328e2023-02-14 21:38:14 +0800676 size = one_pt * count_ranges();
Alexander Grafe317fe82016-03-04 01:09:47 +0100677
678 /*
679 * We need to duplicate our page table once to have an emergency pt to
680 * resort to when splitting page tables later on
681 */
682 size *= 2;
683
684 /*
685 * We may need to split page tables later on if dcache settings change,
686 * so reserve up to 4 (random pick) page tables for that.
687 */
688 size += one_pt * 4;
689
690 return size;
691}
692
York Suna81fcd12016-06-24 16:46:20 -0700693void setup_pgtables(void)
Alexander Grafe317fe82016-03-04 01:09:47 +0100694{
695 int i;
696
York Suna81fcd12016-06-24 16:46:20 -0700697 if (!gd->arch.tlb_fillptr || !gd->arch.tlb_addr)
698 panic("Page table pointer not setup.");
699
Alexander Grafe317fe82016-03-04 01:09:47 +0100700 /*
701 * Allocate the first level we're on with invalidate entries.
702 * If the starting level is 0 (va_bits >= 39), then this is our
703 * Lv0 page table, otherwise it's the entry Lv1 page table.
704 */
705 create_table();
706
707 /* Now add all MMU table entries one after another to the table */
Alexander Graf6b3e7ca2016-03-04 01:09:48 +0100708 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
Alexander Grafe317fe82016-03-04 01:09:47 +0100709 add_map(&mem_map[i]);
Alexander Grafe317fe82016-03-04 01:09:47 +0100710}
711
712static void setup_all_pgtables(void)
713{
714 u64 tlb_addr = gd->arch.tlb_addr;
Alexander Graffa3754e2016-07-30 23:13:03 +0200715 u64 tlb_size = gd->arch.tlb_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100716
717 /* Reset the fill ptr */
718 gd->arch.tlb_fillptr = tlb_addr;
719
720 /* Create normal system page tables */
721 setup_pgtables();
722
723 /* Create emergency page tables */
Alexander Graffa3754e2016-07-30 23:13:03 +0200724 gd->arch.tlb_size -= (uintptr_t)gd->arch.tlb_fillptr -
725 (uintptr_t)gd->arch.tlb_addr;
Alexander Grafe317fe82016-03-04 01:09:47 +0100726 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
727 setup_pgtables();
728 gd->arch.tlb_emerg = gd->arch.tlb_addr;
729 gd->arch.tlb_addr = tlb_addr;
Alexander Graffa3754e2016-07-30 23:13:03 +0200730 gd->arch.tlb_size = tlb_size;
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700731}
732
David Feng85fd5f12013-12-14 11:47:35 +0800733/* to activate the MMU we need to set up virtual memory */
Stephen Warren7333c6a2015-10-05 12:09:00 -0600734__weak void mmu_setup(void)
David Feng85fd5f12013-12-14 11:47:35 +0800735{
Thierry Reding59c364d2015-07-22 17:10:11 -0600736 int el;
David Feng85fd5f12013-12-14 11:47:35 +0800737
Alexander Grafe317fe82016-03-04 01:09:47 +0100738 /* Set up page tables only once */
739 if (!gd->arch.tlb_fillptr)
740 setup_all_pgtables();
Alexander Graffb74cc12016-03-04 01:09:45 +0100741
742 el = current_el();
Andre Przywara630a7942022-06-14 00:11:10 +0100743 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(NULL, NULL),
Alexander Graffb74cc12016-03-04 01:09:45 +0100744 MEMORY_ATTRIBUTES);
Alexander Graffb74cc12016-03-04 01:09:45 +0100745
David Feng85fd5f12013-12-14 11:47:35 +0800746 /* enable the mmu */
747 set_sctlr(get_sctlr() | CR_M);
748}
749
750/*
751 * Performs a invalidation of the entire data cache at all levels
752 */
753void invalidate_dcache_all(void)
754{
Marc Zyngierb67855c2023-02-09 04:54:27 +0800755#ifndef CONFIG_CMO_BY_VA_ONLY
York Sunef042012014-02-26 13:26:04 -0800756 __asm_invalidate_dcache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600757 __asm_invalidate_l3_dcache();
Marc Zyngierb67855c2023-02-09 04:54:27 +0800758#else
759 apply_cmo_to_mappings(invalidate_dcache_range);
760#endif
David Feng85fd5f12013-12-14 11:47:35 +0800761}
762
763/*
York Sun1ce575f2015-01-06 13:18:42 -0800764 * Performs a clean & invalidation of the entire data cache at all levels.
765 * This function needs to be inline to avoid using stack.
Stephen Warrenddb0f632016-10-19 15:18:46 -0600766 * __asm_flush_l3_dcache return status of timeout
David Feng85fd5f12013-12-14 11:47:35 +0800767 */
York Sun1ce575f2015-01-06 13:18:42 -0800768inline void flush_dcache_all(void)
David Feng85fd5f12013-12-14 11:47:35 +0800769{
Marc Zyngierb67855c2023-02-09 04:54:27 +0800770#ifndef CONFIG_CMO_BY_VA_ONLY
York Sun1ce575f2015-01-06 13:18:42 -0800771 int ret;
772
David Feng85fd5f12013-12-14 11:47:35 +0800773 __asm_flush_dcache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600774 ret = __asm_flush_l3_dcache();
York Sun1ce575f2015-01-06 13:18:42 -0800775 if (ret)
776 debug("flushing dcache returns 0x%x\n", ret);
777 else
778 debug("flushing dcache successfully.\n");
Marc Zyngierb67855c2023-02-09 04:54:27 +0800779#else
780 apply_cmo_to_mappings(flush_dcache_range);
781#endif
David Feng85fd5f12013-12-14 11:47:35 +0800782}
783
Vignesh Raghavendra384c1412019-04-22 21:43:32 +0530784#ifndef CONFIG_SYS_DISABLE_DCACHE_OPS
David Feng85fd5f12013-12-14 11:47:35 +0800785/*
786 * Invalidates range in all levels of D-cache/unified cache
787 */
788void invalidate_dcache_range(unsigned long start, unsigned long stop)
789{
Simon Glass4415c3b2017-04-05 17:53:18 -0600790 __asm_invalidate_dcache_range(start, stop);
David Feng85fd5f12013-12-14 11:47:35 +0800791}
792
793/*
794 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
795 */
796void flush_dcache_range(unsigned long start, unsigned long stop)
797{
798 __asm_flush_dcache_range(start, stop);
799}
Vignesh Raghavendra384c1412019-04-22 21:43:32 +0530800#else
801void invalidate_dcache_range(unsigned long start, unsigned long stop)
802{
803}
804
805void flush_dcache_range(unsigned long start, unsigned long stop)
806{
807}
808#endif /* CONFIG_SYS_DISABLE_DCACHE_OPS */
David Feng85fd5f12013-12-14 11:47:35 +0800809
810void dcache_enable(void)
811{
812 /* The data cache is not active unless the mmu is enabled */
813 if (!(get_sctlr() & CR_M)) {
814 invalidate_dcache_all();
815 __asm_invalidate_tlb_all();
816 mmu_setup();
817 }
818
Pali Rohárfbddaee2022-09-14 13:37:46 +0200819 /* Set up page tables only once (it is done also by mmu_setup()) */
820 if (!gd->arch.tlb_fillptr)
821 setup_all_pgtables();
822
David Feng85fd5f12013-12-14 11:47:35 +0800823 set_sctlr(get_sctlr() | CR_C);
824}
825
826void dcache_disable(void)
827{
828 uint32_t sctlr;
829
830 sctlr = get_sctlr();
831
832 /* if cache isn't enabled no need to disable */
833 if (!(sctlr & CR_C))
834 return;
835
Marc Zyngierb67855c2023-02-09 04:54:27 +0800836 if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
837 /*
838 * When invalidating by VA, do it *before* turning the MMU
839 * off, so that at least our stack is coherent.
840 */
841 flush_dcache_all();
842 }
843
David Feng85fd5f12013-12-14 11:47:35 +0800844 set_sctlr(sctlr & ~(CR_C|CR_M));
845
Marc Zyngierb67855c2023-02-09 04:54:27 +0800846 if (!IS_ENABLED(CONFIG_CMO_BY_VA_ONLY))
847 flush_dcache_all();
848
David Feng85fd5f12013-12-14 11:47:35 +0800849 __asm_invalidate_tlb_all();
850}
851
852int dcache_status(void)
853{
854 return (get_sctlr() & CR_C) != 0;
855}
856
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530857u64 *__weak arch_get_page_table(void) {
858 puts("No page table offset defined\n");
859
860 return NULL;
861}
862
Alexander Grafe317fe82016-03-04 01:09:47 +0100863static bool is_aligned(u64 addr, u64 size, u64 align)
864{
865 return !(addr & (align - 1)) && !(size & (align - 1));
866}
867
York Sun5bb14e02017-03-06 09:02:33 -0800868/* Use flag to indicate if attrs has more than d-cache attributes */
869static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level)
Alexander Grafe317fe82016-03-04 01:09:47 +0100870{
871 int levelshift = level2shift(level);
872 u64 levelsize = 1ULL << levelshift;
873 u64 *pte = find_pte(start, level);
874
875 /* Can we can just modify the current level block PTE? */
876 if (is_aligned(start, size, levelsize)) {
York Sun5bb14e02017-03-06 09:02:33 -0800877 if (flag) {
878 *pte &= ~PMD_ATTRMASK;
879 *pte |= attrs & PMD_ATTRMASK;
880 } else {
881 *pte &= ~PMD_ATTRINDX_MASK;
882 *pte |= attrs & PMD_ATTRINDX_MASK;
883 }
Alexander Grafe317fe82016-03-04 01:09:47 +0100884 debug("Set attrs=%llx pte=%p level=%d\n", attrs, pte, level);
885
886 return levelsize;
887 }
888
889 /* Unaligned or doesn't fit, maybe split block into table */
890 debug("addr=%llx level=%d pte=%p (%llx)\n", start, level, pte, *pte);
891
892 /* Maybe we need to split the block into a table */
893 if (pte_type(pte) == PTE_TYPE_BLOCK)
894 split_block(pte, level);
895
896 /* And then double-check it became a table or already is one */
897 if (pte_type(pte) != PTE_TYPE_TABLE)
898 panic("PTE %p (%llx) for addr=%llx should be a table",
899 pte, *pte, start);
900
901 /* Roll on to the next page table level */
902 return 0;
903}
904
905void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
906 enum dcache_option option)
907{
Peng Fan41bad3e2020-05-11 16:41:07 +0800908 u64 attrs = PMD_ATTRINDX(option >> 2);
Alexander Grafe317fe82016-03-04 01:09:47 +0100909 u64 real_start = start;
910 u64 real_size = size;
911
912 debug("start=%lx size=%lx\n", (ulong)start, (ulong)size);
913
York Suna81fcd12016-06-24 16:46:20 -0700914 if (!gd->arch.tlb_emerg)
915 panic("Emergency page table not setup.");
916
Alexander Grafe317fe82016-03-04 01:09:47 +0100917 /*
918 * We can not modify page tables that we're currently running on,
919 * so we first need to switch to the "emergency" page tables where
920 * we can safely modify our primary page tables and then switch back
921 */
922 __asm_switch_ttbr(gd->arch.tlb_emerg);
923
924 /*
925 * Loop through the address range until we find a page granule that fits
926 * our alignment constraints, then set it to the new cache attributes
927 */
928 while (size > 0) {
929 int level;
930 u64 r;
931
932 for (level = 1; level < 4; level++) {
York Sun5bb14e02017-03-06 09:02:33 -0800933 /* Set d-cache attributes only */
934 r = set_one_region(start, size, attrs, false, level);
Alexander Grafe317fe82016-03-04 01:09:47 +0100935 if (r) {
936 /* PTE successfully replaced */
937 size -= r;
938 start += r;
939 break;
940 }
941 }
942
943 }
944
945 /* We're done modifying page tables, switch back to our primary ones */
946 __asm_switch_ttbr(gd->arch.tlb_addr);
947
948 /*
949 * Make sure there's nothing stale in dcache for a region that might
950 * have caches off now
951 */
952 flush_dcache_range(real_start, real_start + real_size);
953}
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700954
York Sun5bb14e02017-03-06 09:02:33 -0800955/*
956 * Modify MMU table for a region with updated PXN/UXN/Memory type/valid bits.
957 * The procecess is break-before-make. The target region will be marked as
958 * invalid during the process of changing.
959 */
960void mmu_change_region_attr(phys_addr_t addr, size_t siz, u64 attrs)
961{
962 int level;
963 u64 r, size, start;
964
965 start = addr;
966 size = siz;
967 /*
968 * Loop through the address range until we find a page granule that fits
969 * our alignment constraints, then set it to "invalid".
970 */
971 while (size > 0) {
972 for (level = 1; level < 4; level++) {
973 /* Set PTE to fault */
974 r = set_one_region(start, size, PTE_TYPE_FAULT, true,
975 level);
976 if (r) {
977 /* PTE successfully invalidated */
978 size -= r;
979 start += r;
980 break;
981 }
982 }
983 }
984
985 flush_dcache_range(gd->arch.tlb_addr,
986 gd->arch.tlb_addr + gd->arch.tlb_size);
987 __asm_invalidate_tlb_all();
988
989 /*
990 * Loop through the address range until we find a page granule that fits
991 * our alignment constraints, then set it to the new cache attributes
992 */
993 start = addr;
994 size = siz;
995 while (size > 0) {
996 for (level = 1; level < 4; level++) {
997 /* Set PTE to new attributes */
998 r = set_one_region(start, size, attrs, true, level);
999 if (r) {
1000 /* PTE successfully updated */
1001 size -= r;
1002 start += r;
1003 break;
1004 }
1005 }
1006 }
1007 flush_dcache_range(gd->arch.tlb_addr,
1008 gd->arch.tlb_addr + gd->arch.tlb_size);
1009 __asm_invalidate_tlb_all();
1010}
1011
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001012#else /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +08001013
Alexander Grafbc40da92016-03-04 01:09:55 +01001014/*
1015 * For SPL builds, we may want to not have dcache enabled. Any real U-Boot
1016 * running however really wants to have dcache and the MMU active. Check that
1017 * everything is sane and give the developer a hint if it isn't.
1018 */
1019#ifndef CONFIG_SPL_BUILD
1020#error Please describe your MMU layout in CONFIG_SYS_MEM_MAP and enable dcache.
1021#endif
1022
David Feng85fd5f12013-12-14 11:47:35 +08001023void invalidate_dcache_all(void)
1024{
1025}
1026
1027void flush_dcache_all(void)
1028{
1029}
1030
David Feng85fd5f12013-12-14 11:47:35 +08001031void dcache_enable(void)
1032{
1033}
1034
1035void dcache_disable(void)
1036{
1037}
1038
1039int dcache_status(void)
1040{
1041 return 0;
1042}
1043
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +05301044void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
1045 enum dcache_option option)
1046{
1047}
1048
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001049#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +08001050
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001051#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
David Feng85fd5f12013-12-14 11:47:35 +08001052
1053void icache_enable(void)
1054{
Stephen Warrenddb0f632016-10-19 15:18:46 -06001055 invalidate_icache_all();
David Feng85fd5f12013-12-14 11:47:35 +08001056 set_sctlr(get_sctlr() | CR_I);
1057}
1058
1059void icache_disable(void)
1060{
1061 set_sctlr(get_sctlr() & ~CR_I);
1062}
1063
1064int icache_status(void)
1065{
1066 return (get_sctlr() & CR_I) != 0;
1067}
1068
Patrice Chotardee435c62021-07-19 11:21:51 +02001069int mmu_status(void)
1070{
1071 return (get_sctlr() & CR_M) != 0;
1072}
1073
David Feng85fd5f12013-12-14 11:47:35 +08001074void invalidate_icache_all(void)
1075{
1076 __asm_invalidate_icache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -06001077 __asm_invalidate_l3_icache();
David Feng85fd5f12013-12-14 11:47:35 +08001078}
1079
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001080#else /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +08001081
1082void icache_enable(void)
1083{
1084}
1085
1086void icache_disable(void)
1087{
1088}
1089
1090int icache_status(void)
1091{
1092 return 0;
1093}
1094
Patrice Chotardee435c62021-07-19 11:21:51 +02001095int mmu_status(void)
1096{
1097 return 0;
1098}
1099
David Feng85fd5f12013-12-14 11:47:35 +08001100void invalidate_icache_all(void)
1101{
1102}
1103
Trevor Woerner43ec7e02019-05-03 09:41:00 -04001104#endif /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +08001105
1106/*
1107 * Enable dCache & iCache, whether cache is actually enabled
1108 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
1109 */
York Suna84cd722014-06-23 15:15:54 -07001110void __weak enable_caches(void)
David Feng85fd5f12013-12-14 11:47:35 +08001111{
1112 icache_enable();
1113 dcache_enable();
1114}