blob: 876344e1b4d54aa23858e611e5aa4c306566cf53 [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
10#include <common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070011#include <cpu_func.h>
Simon Glassf11478f2019-12-28 10:45:07 -070012#include <hang.h>
Simon Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
David Feng85fd5f12013-12-14 11:47:35 +080016#include <asm/system.h>
17#include <asm/armv8/mmu.h>
18
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
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800312 if (level >= 1 &&
313 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());
329
330 next_table = (u64 *)(table[i] & GENMASK_ULL(47, PAGE_SHIFT));
331 next_size = min(map_size - (virt & (map_size - 1)), size);
332
333 map_range(virt, phys, next_size, level + 1, next_table, attrs);
334
335 virt += next_size;
336 phys += next_size;
337 size -= next_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100338 }
339}
340
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800341static void add_map(struct mm_region *map)
342{
343 u64 attrs = map->attrs | PTE_TYPE_BLOCK | PTE_BLOCK_AF;
344 u64 va_bits;
345 int level = 0;
346
347 get_tcr(NULL, &va_bits);
348 if (va_bits < 39)
349 level = 1;
350
351 map_range(map->virt, map->phys, map->size, level,
352 (u64 *)gd->arch.tlb_addr, attrs);
353}
354
Alexander Grafe317fe82016-03-04 01:09:47 +0100355enum pte_type {
356 PTE_INVAL,
357 PTE_BLOCK,
358 PTE_LEVEL,
359};
360
361/*
362 * This is a recursively called function to count the number of
363 * page tables we need to cover a particular PTE range. If you
364 * call this with level = -1 you basically get the full 48 bit
365 * coverage.
366 */
367static int count_required_pts(u64 addr, int level, u64 maxaddr)
368{
369 int levelshift = level2shift(level);
370 u64 levelsize = 1ULL << levelshift;
371 u64 levelmask = levelsize - 1;
372 u64 levelend = addr + levelsize;
373 int r = 0;
374 int i;
375 enum pte_type pte_type = PTE_INVAL;
376
Alexander Graf6b3e7ca2016-03-04 01:09:48 +0100377 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++) {
Alexander Grafe317fe82016-03-04 01:09:47 +0100378 struct mm_region *map = &mem_map[i];
York Sunc7104e52016-06-24 16:46:22 -0700379 u64 start = map->virt;
Alexander Grafe317fe82016-03-04 01:09:47 +0100380 u64 end = start + map->size;
381
382 /* Check if the PTE would overlap with the map */
383 if (max(addr, start) <= min(levelend, end)) {
384 start = max(addr, start);
385 end = min(levelend, end);
386
387 /* We need a sub-pt for this level */
388 if ((start & levelmask) || (end & levelmask)) {
389 pte_type = PTE_LEVEL;
390 break;
391 }
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700392
Alexander Grafe317fe82016-03-04 01:09:47 +0100393 /* Lv0 can not do block PTEs, so do levels here too */
394 if (level <= 0) {
395 pte_type = PTE_LEVEL;
396 break;
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700397 }
398
Alexander Grafe317fe82016-03-04 01:09:47 +0100399 /* PTE is active, but fits into a block */
400 pte_type = PTE_BLOCK;
401 }
402 }
403
404 /*
405 * Block PTEs at this level are already covered by the parent page
406 * table, so we only need to count sub page tables.
407 */
408 if (pte_type == PTE_LEVEL) {
409 int sublevel = level + 1;
410 u64 sublevelsize = 1ULL << level2shift(sublevel);
411
412 /* Account for the new sub page table ... */
413 r = 1;
414
415 /* ... and for all child page tables that one might have */
416 for (i = 0; i < MAX_PTE_ENTRIES; i++) {
417 r += count_required_pts(addr, sublevel, maxaddr);
418 addr += sublevelsize;
419
420 if (addr >= maxaddr) {
421 /*
422 * We reached the end of address space, no need
423 * to look any further.
424 */
425 break;
426 }
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700427 }
428 }
Alexander Grafe317fe82016-03-04 01:09:47 +0100429
430 return r;
431}
432
433/* Returns the estimated required size of all page tables */
Alexander Grafbc78b922016-03-21 20:26:12 +0100434__weak u64 get_page_table_size(void)
Alexander Grafe317fe82016-03-04 01:09:47 +0100435{
436 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
437 u64 size = 0;
438 u64 va_bits;
439 int start_level = 0;
440
Andre Przywara630a7942022-06-14 00:11:10 +0100441 get_tcr(NULL, &va_bits);
Alexander Grafe317fe82016-03-04 01:09:47 +0100442 if (va_bits < 39)
443 start_level = 1;
444
445 /* Account for all page tables we would need to cover our memory map */
446 size = one_pt * count_required_pts(0, start_level - 1, 1ULL << va_bits);
447
448 /*
449 * We need to duplicate our page table once to have an emergency pt to
450 * resort to when splitting page tables later on
451 */
452 size *= 2;
453
454 /*
455 * We may need to split page tables later on if dcache settings change,
456 * so reserve up to 4 (random pick) page tables for that.
457 */
458 size += one_pt * 4;
459
460 return size;
461}
462
York Suna81fcd12016-06-24 16:46:20 -0700463void setup_pgtables(void)
Alexander Grafe317fe82016-03-04 01:09:47 +0100464{
465 int i;
466
York Suna81fcd12016-06-24 16:46:20 -0700467 if (!gd->arch.tlb_fillptr || !gd->arch.tlb_addr)
468 panic("Page table pointer not setup.");
469
Alexander Grafe317fe82016-03-04 01:09:47 +0100470 /*
471 * Allocate the first level we're on with invalidate entries.
472 * If the starting level is 0 (va_bits >= 39), then this is our
473 * Lv0 page table, otherwise it's the entry Lv1 page table.
474 */
475 create_table();
476
477 /* Now add all MMU table entries one after another to the table */
Alexander Graf6b3e7ca2016-03-04 01:09:48 +0100478 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
Alexander Grafe317fe82016-03-04 01:09:47 +0100479 add_map(&mem_map[i]);
Alexander Grafe317fe82016-03-04 01:09:47 +0100480}
481
482static void setup_all_pgtables(void)
483{
484 u64 tlb_addr = gd->arch.tlb_addr;
Alexander Graffa3754e2016-07-30 23:13:03 +0200485 u64 tlb_size = gd->arch.tlb_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100486
487 /* Reset the fill ptr */
488 gd->arch.tlb_fillptr = tlb_addr;
489
490 /* Create normal system page tables */
491 setup_pgtables();
492
493 /* Create emergency page tables */
Alexander Graffa3754e2016-07-30 23:13:03 +0200494 gd->arch.tlb_size -= (uintptr_t)gd->arch.tlb_fillptr -
495 (uintptr_t)gd->arch.tlb_addr;
Alexander Grafe317fe82016-03-04 01:09:47 +0100496 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
497 setup_pgtables();
498 gd->arch.tlb_emerg = gd->arch.tlb_addr;
499 gd->arch.tlb_addr = tlb_addr;
Alexander Graffa3754e2016-07-30 23:13:03 +0200500 gd->arch.tlb_size = tlb_size;
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700501}
502
David Feng85fd5f12013-12-14 11:47:35 +0800503/* to activate the MMU we need to set up virtual memory */
Stephen Warren7333c6a2015-10-05 12:09:00 -0600504__weak void mmu_setup(void)
David Feng85fd5f12013-12-14 11:47:35 +0800505{
Thierry Reding59c364d2015-07-22 17:10:11 -0600506 int el;
David Feng85fd5f12013-12-14 11:47:35 +0800507
Alexander Grafe317fe82016-03-04 01:09:47 +0100508 /* Set up page tables only once */
509 if (!gd->arch.tlb_fillptr)
510 setup_all_pgtables();
Alexander Graffb74cc12016-03-04 01:09:45 +0100511
512 el = current_el();
Andre Przywara630a7942022-06-14 00:11:10 +0100513 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(NULL, NULL),
Alexander Graffb74cc12016-03-04 01:09:45 +0100514 MEMORY_ATTRIBUTES);
Alexander Graffb74cc12016-03-04 01:09:45 +0100515
David Feng85fd5f12013-12-14 11:47:35 +0800516 /* enable the mmu */
517 set_sctlr(get_sctlr() | CR_M);
518}
519
520/*
521 * Performs a invalidation of the entire data cache at all levels
522 */
523void invalidate_dcache_all(void)
524{
Marc Zyngierb67855c2023-02-09 04:54:27 +0800525#ifndef CONFIG_CMO_BY_VA_ONLY
York Sunef042012014-02-26 13:26:04 -0800526 __asm_invalidate_dcache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600527 __asm_invalidate_l3_dcache();
Marc Zyngierb67855c2023-02-09 04:54:27 +0800528#else
529 apply_cmo_to_mappings(invalidate_dcache_range);
530#endif
David Feng85fd5f12013-12-14 11:47:35 +0800531}
532
533/*
York Sun1ce575f2015-01-06 13:18:42 -0800534 * Performs a clean & invalidation of the entire data cache at all levels.
535 * This function needs to be inline to avoid using stack.
Stephen Warrenddb0f632016-10-19 15:18:46 -0600536 * __asm_flush_l3_dcache return status of timeout
David Feng85fd5f12013-12-14 11:47:35 +0800537 */
York Sun1ce575f2015-01-06 13:18:42 -0800538inline void flush_dcache_all(void)
David Feng85fd5f12013-12-14 11:47:35 +0800539{
Marc Zyngierb67855c2023-02-09 04:54:27 +0800540#ifndef CONFIG_CMO_BY_VA_ONLY
York Sun1ce575f2015-01-06 13:18:42 -0800541 int ret;
542
David Feng85fd5f12013-12-14 11:47:35 +0800543 __asm_flush_dcache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600544 ret = __asm_flush_l3_dcache();
York Sun1ce575f2015-01-06 13:18:42 -0800545 if (ret)
546 debug("flushing dcache returns 0x%x\n", ret);
547 else
548 debug("flushing dcache successfully.\n");
Marc Zyngierb67855c2023-02-09 04:54:27 +0800549#else
550 apply_cmo_to_mappings(flush_dcache_range);
551#endif
David Feng85fd5f12013-12-14 11:47:35 +0800552}
553
Vignesh Raghavendra384c1412019-04-22 21:43:32 +0530554#ifndef CONFIG_SYS_DISABLE_DCACHE_OPS
David Feng85fd5f12013-12-14 11:47:35 +0800555/*
556 * Invalidates range in all levels of D-cache/unified cache
557 */
558void invalidate_dcache_range(unsigned long start, unsigned long stop)
559{
Simon Glass4415c3b2017-04-05 17:53:18 -0600560 __asm_invalidate_dcache_range(start, stop);
David Feng85fd5f12013-12-14 11:47:35 +0800561}
562
563/*
564 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
565 */
566void flush_dcache_range(unsigned long start, unsigned long stop)
567{
568 __asm_flush_dcache_range(start, stop);
569}
Vignesh Raghavendra384c1412019-04-22 21:43:32 +0530570#else
571void invalidate_dcache_range(unsigned long start, unsigned long stop)
572{
573}
574
575void flush_dcache_range(unsigned long start, unsigned long stop)
576{
577}
578#endif /* CONFIG_SYS_DISABLE_DCACHE_OPS */
David Feng85fd5f12013-12-14 11:47:35 +0800579
580void dcache_enable(void)
581{
582 /* The data cache is not active unless the mmu is enabled */
583 if (!(get_sctlr() & CR_M)) {
584 invalidate_dcache_all();
585 __asm_invalidate_tlb_all();
586 mmu_setup();
587 }
588
Pali Rohárfbddaee2022-09-14 13:37:46 +0200589 /* Set up page tables only once (it is done also by mmu_setup()) */
590 if (!gd->arch.tlb_fillptr)
591 setup_all_pgtables();
592
David Feng85fd5f12013-12-14 11:47:35 +0800593 set_sctlr(get_sctlr() | CR_C);
594}
595
596void dcache_disable(void)
597{
598 uint32_t sctlr;
599
600 sctlr = get_sctlr();
601
602 /* if cache isn't enabled no need to disable */
603 if (!(sctlr & CR_C))
604 return;
605
Marc Zyngierb67855c2023-02-09 04:54:27 +0800606 if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
607 /*
608 * When invalidating by VA, do it *before* turning the MMU
609 * off, so that at least our stack is coherent.
610 */
611 flush_dcache_all();
612 }
613
David Feng85fd5f12013-12-14 11:47:35 +0800614 set_sctlr(sctlr & ~(CR_C|CR_M));
615
Marc Zyngierb67855c2023-02-09 04:54:27 +0800616 if (!IS_ENABLED(CONFIG_CMO_BY_VA_ONLY))
617 flush_dcache_all();
618
David Feng85fd5f12013-12-14 11:47:35 +0800619 __asm_invalidate_tlb_all();
620}
621
622int dcache_status(void)
623{
624 return (get_sctlr() & CR_C) != 0;
625}
626
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530627u64 *__weak arch_get_page_table(void) {
628 puts("No page table offset defined\n");
629
630 return NULL;
631}
632
Alexander Grafe317fe82016-03-04 01:09:47 +0100633static bool is_aligned(u64 addr, u64 size, u64 align)
634{
635 return !(addr & (align - 1)) && !(size & (align - 1));
636}
637
York Sun5bb14e02017-03-06 09:02:33 -0800638/* Use flag to indicate if attrs has more than d-cache attributes */
639static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level)
Alexander Grafe317fe82016-03-04 01:09:47 +0100640{
641 int levelshift = level2shift(level);
642 u64 levelsize = 1ULL << levelshift;
643 u64 *pte = find_pte(start, level);
644
645 /* Can we can just modify the current level block PTE? */
646 if (is_aligned(start, size, levelsize)) {
York Sun5bb14e02017-03-06 09:02:33 -0800647 if (flag) {
648 *pte &= ~PMD_ATTRMASK;
649 *pte |= attrs & PMD_ATTRMASK;
650 } else {
651 *pte &= ~PMD_ATTRINDX_MASK;
652 *pte |= attrs & PMD_ATTRINDX_MASK;
653 }
Alexander Grafe317fe82016-03-04 01:09:47 +0100654 debug("Set attrs=%llx pte=%p level=%d\n", attrs, pte, level);
655
656 return levelsize;
657 }
658
659 /* Unaligned or doesn't fit, maybe split block into table */
660 debug("addr=%llx level=%d pte=%p (%llx)\n", start, level, pte, *pte);
661
662 /* Maybe we need to split the block into a table */
663 if (pte_type(pte) == PTE_TYPE_BLOCK)
664 split_block(pte, level);
665
666 /* And then double-check it became a table or already is one */
667 if (pte_type(pte) != PTE_TYPE_TABLE)
668 panic("PTE %p (%llx) for addr=%llx should be a table",
669 pte, *pte, start);
670
671 /* Roll on to the next page table level */
672 return 0;
673}
674
675void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
676 enum dcache_option option)
677{
Peng Fan41bad3e2020-05-11 16:41:07 +0800678 u64 attrs = PMD_ATTRINDX(option >> 2);
Alexander Grafe317fe82016-03-04 01:09:47 +0100679 u64 real_start = start;
680 u64 real_size = size;
681
682 debug("start=%lx size=%lx\n", (ulong)start, (ulong)size);
683
York Suna81fcd12016-06-24 16:46:20 -0700684 if (!gd->arch.tlb_emerg)
685 panic("Emergency page table not setup.");
686
Alexander Grafe317fe82016-03-04 01:09:47 +0100687 /*
688 * We can not modify page tables that we're currently running on,
689 * so we first need to switch to the "emergency" page tables where
690 * we can safely modify our primary page tables and then switch back
691 */
692 __asm_switch_ttbr(gd->arch.tlb_emerg);
693
694 /*
695 * Loop through the address range until we find a page granule that fits
696 * our alignment constraints, then set it to the new cache attributes
697 */
698 while (size > 0) {
699 int level;
700 u64 r;
701
702 for (level = 1; level < 4; level++) {
York Sun5bb14e02017-03-06 09:02:33 -0800703 /* Set d-cache attributes only */
704 r = set_one_region(start, size, attrs, false, level);
Alexander Grafe317fe82016-03-04 01:09:47 +0100705 if (r) {
706 /* PTE successfully replaced */
707 size -= r;
708 start += r;
709 break;
710 }
711 }
712
713 }
714
715 /* We're done modifying page tables, switch back to our primary ones */
716 __asm_switch_ttbr(gd->arch.tlb_addr);
717
718 /*
719 * Make sure there's nothing stale in dcache for a region that might
720 * have caches off now
721 */
722 flush_dcache_range(real_start, real_start + real_size);
723}
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700724
York Sun5bb14e02017-03-06 09:02:33 -0800725/*
726 * Modify MMU table for a region with updated PXN/UXN/Memory type/valid bits.
727 * The procecess is break-before-make. The target region will be marked as
728 * invalid during the process of changing.
729 */
730void mmu_change_region_attr(phys_addr_t addr, size_t siz, u64 attrs)
731{
732 int level;
733 u64 r, size, start;
734
735 start = addr;
736 size = siz;
737 /*
738 * Loop through the address range until we find a page granule that fits
739 * our alignment constraints, then set it to "invalid".
740 */
741 while (size > 0) {
742 for (level = 1; level < 4; level++) {
743 /* Set PTE to fault */
744 r = set_one_region(start, size, PTE_TYPE_FAULT, true,
745 level);
746 if (r) {
747 /* PTE successfully invalidated */
748 size -= r;
749 start += r;
750 break;
751 }
752 }
753 }
754
755 flush_dcache_range(gd->arch.tlb_addr,
756 gd->arch.tlb_addr + gd->arch.tlb_size);
757 __asm_invalidate_tlb_all();
758
759 /*
760 * Loop through the address range until we find a page granule that fits
761 * our alignment constraints, then set it to the new cache attributes
762 */
763 start = addr;
764 size = siz;
765 while (size > 0) {
766 for (level = 1; level < 4; level++) {
767 /* Set PTE to new attributes */
768 r = set_one_region(start, size, attrs, true, level);
769 if (r) {
770 /* PTE successfully updated */
771 size -= r;
772 start += r;
773 break;
774 }
775 }
776 }
777 flush_dcache_range(gd->arch.tlb_addr,
778 gd->arch.tlb_addr + gd->arch.tlb_size);
779 __asm_invalidate_tlb_all();
780}
781
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400782#else /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +0800783
Alexander Grafbc40da92016-03-04 01:09:55 +0100784/*
785 * For SPL builds, we may want to not have dcache enabled. Any real U-Boot
786 * running however really wants to have dcache and the MMU active. Check that
787 * everything is sane and give the developer a hint if it isn't.
788 */
789#ifndef CONFIG_SPL_BUILD
790#error Please describe your MMU layout in CONFIG_SYS_MEM_MAP and enable dcache.
791#endif
792
David Feng85fd5f12013-12-14 11:47:35 +0800793void invalidate_dcache_all(void)
794{
795}
796
797void flush_dcache_all(void)
798{
799}
800
David Feng85fd5f12013-12-14 11:47:35 +0800801void dcache_enable(void)
802{
803}
804
805void dcache_disable(void)
806{
807}
808
809int dcache_status(void)
810{
811 return 0;
812}
813
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530814void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
815 enum dcache_option option)
816{
817}
818
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400819#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +0800820
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400821#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
David Feng85fd5f12013-12-14 11:47:35 +0800822
823void icache_enable(void)
824{
Stephen Warrenddb0f632016-10-19 15:18:46 -0600825 invalidate_icache_all();
David Feng85fd5f12013-12-14 11:47:35 +0800826 set_sctlr(get_sctlr() | CR_I);
827}
828
829void icache_disable(void)
830{
831 set_sctlr(get_sctlr() & ~CR_I);
832}
833
834int icache_status(void)
835{
836 return (get_sctlr() & CR_I) != 0;
837}
838
Patrice Chotardee435c62021-07-19 11:21:51 +0200839int mmu_status(void)
840{
841 return (get_sctlr() & CR_M) != 0;
842}
843
David Feng85fd5f12013-12-14 11:47:35 +0800844void invalidate_icache_all(void)
845{
846 __asm_invalidate_icache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600847 __asm_invalidate_l3_icache();
David Feng85fd5f12013-12-14 11:47:35 +0800848}
849
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400850#else /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +0800851
852void icache_enable(void)
853{
854}
855
856void icache_disable(void)
857{
858}
859
860int icache_status(void)
861{
862 return 0;
863}
864
Patrice Chotardee435c62021-07-19 11:21:51 +0200865int mmu_status(void)
866{
867 return 0;
868}
869
David Feng85fd5f12013-12-14 11:47:35 +0800870void invalidate_icache_all(void)
871{
872}
873
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400874#endif /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +0800875
876/*
877 * Enable dCache & iCache, whether cache is actually enabled
878 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
879 */
York Suna84cd722014-06-23 15:15:54 -0700880void __weak enable_caches(void)
David Feng85fd5f12013-12-14 11:47:35 +0800881{
882 icache_enable();
883 dcache_enable();
884}