blob: d4c64f2d60d9aeef5fa2ac51acf10222ad0cc8c4 [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
Marc Zyngierfeb0ec22023-02-14 21:38:13 +0800342static void add_map(struct mm_region *map)
343{
344 u64 attrs = map->attrs | PTE_TYPE_BLOCK | PTE_BLOCK_AF;
345 u64 va_bits;
346 int level = 0;
347
348 get_tcr(NULL, &va_bits);
349 if (va_bits < 39)
350 level = 1;
351
352 map_range(map->virt, map->phys, map->size, level,
353 (u64 *)gd->arch.tlb_addr, attrs);
354}
355
Marc Zyngier6da328e2023-02-14 21:38:14 +0800356static void count_range(u64 virt, u64 size, int level, int *cntp)
Alexander Grafe317fe82016-03-04 01:09:47 +0100357{
Marc Zyngier6da328e2023-02-14 21:38:14 +0800358 u64 map_size = BIT_ULL(level2shift(level));
359 int i, idx;
Alexander Grafe317fe82016-03-04 01:09:47 +0100360
Marc Zyngier6da328e2023-02-14 21:38:14 +0800361 idx = (virt >> level2shift(level)) & (MAX_PTE_ENTRIES - 1);
362 for (i = idx; size; i++) {
363 u64 next_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100364
Chris Packham978814f2023-10-27 13:23:53 +1300365 if (level >= 1 &&
Marc Zyngier6da328e2023-02-14 21:38:14 +0800366 size >= map_size && !(virt & (map_size - 1))) {
367 virt += map_size;
368 size -= map_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100369
Marc Zyngier6da328e2023-02-14 21:38:14 +0800370 continue;
371 }
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700372
Marc Zyngier6da328e2023-02-14 21:38:14 +0800373 /* Going one level down */
374 (*cntp)++;
375 next_size = min(map_size - (virt & (map_size - 1)), size);
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700376
Marc Zyngier6da328e2023-02-14 21:38:14 +0800377 count_range(virt, next_size, level + 1, cntp);
Alexander Grafe317fe82016-03-04 01:09:47 +0100378
Marc Zyngier6da328e2023-02-14 21:38:14 +0800379 virt += next_size;
380 size -= next_size;
381 }
382}
Alexander Grafe317fe82016-03-04 01:09:47 +0100383
Marc Zyngier6da328e2023-02-14 21:38:14 +0800384static int count_ranges(void)
385{
386 int i, count = 0, level = 0;
387 u64 va_bits;
Alexander Grafe317fe82016-03-04 01:09:47 +0100388
Marc Zyngier6da328e2023-02-14 21:38:14 +0800389 get_tcr(NULL, &va_bits);
390 if (va_bits < 39)
391 level = 1;
Alexander Grafe317fe82016-03-04 01:09:47 +0100392
Marc Zyngier6da328e2023-02-14 21:38:14 +0800393 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
394 count_range(mem_map[i].virt, mem_map[i].size, level, &count);
Alexander Grafe317fe82016-03-04 01:09:47 +0100395
Marc Zyngier6da328e2023-02-14 21:38:14 +0800396 return count;
Alexander Grafe317fe82016-03-04 01:09:47 +0100397}
398
399/* Returns the estimated required size of all page tables */
Alexander Grafbc78b922016-03-21 20:26:12 +0100400__weak u64 get_page_table_size(void)
Alexander Grafe317fe82016-03-04 01:09:47 +0100401{
402 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
Chris Packhama6c68c62023-10-27 13:23:54 +1300403 u64 size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100404
405 /* Account for all page tables we would need to cover our memory map */
Marc Zyngier6da328e2023-02-14 21:38:14 +0800406 size = one_pt * count_ranges();
Alexander Grafe317fe82016-03-04 01:09:47 +0100407
408 /*
409 * We need to duplicate our page table once to have an emergency pt to
410 * resort to when splitting page tables later on
411 */
412 size *= 2;
413
414 /*
415 * We may need to split page tables later on if dcache settings change,
416 * so reserve up to 4 (random pick) page tables for that.
417 */
418 size += one_pt * 4;
419
420 return size;
421}
422
York Suna81fcd12016-06-24 16:46:20 -0700423void setup_pgtables(void)
Alexander Grafe317fe82016-03-04 01:09:47 +0100424{
425 int i;
426
York Suna81fcd12016-06-24 16:46:20 -0700427 if (!gd->arch.tlb_fillptr || !gd->arch.tlb_addr)
428 panic("Page table pointer not setup.");
429
Alexander Grafe317fe82016-03-04 01:09:47 +0100430 /*
431 * Allocate the first level we're on with invalidate entries.
432 * If the starting level is 0 (va_bits >= 39), then this is our
433 * Lv0 page table, otherwise it's the entry Lv1 page table.
434 */
435 create_table();
436
437 /* Now add all MMU table entries one after another to the table */
Alexander Graf6b3e7ca2016-03-04 01:09:48 +0100438 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
Alexander Grafe317fe82016-03-04 01:09:47 +0100439 add_map(&mem_map[i]);
Alexander Grafe317fe82016-03-04 01:09:47 +0100440}
441
442static void setup_all_pgtables(void)
443{
444 u64 tlb_addr = gd->arch.tlb_addr;
Alexander Graffa3754e2016-07-30 23:13:03 +0200445 u64 tlb_size = gd->arch.tlb_size;
Alexander Grafe317fe82016-03-04 01:09:47 +0100446
447 /* Reset the fill ptr */
448 gd->arch.tlb_fillptr = tlb_addr;
449
450 /* Create normal system page tables */
451 setup_pgtables();
452
453 /* Create emergency page tables */
Alexander Graffa3754e2016-07-30 23:13:03 +0200454 gd->arch.tlb_size -= (uintptr_t)gd->arch.tlb_fillptr -
455 (uintptr_t)gd->arch.tlb_addr;
Alexander Grafe317fe82016-03-04 01:09:47 +0100456 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
457 setup_pgtables();
458 gd->arch.tlb_emerg = gd->arch.tlb_addr;
459 gd->arch.tlb_addr = tlb_addr;
Alexander Graffa3754e2016-07-30 23:13:03 +0200460 gd->arch.tlb_size = tlb_size;
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700461}
462
David Feng85fd5f12013-12-14 11:47:35 +0800463/* to activate the MMU we need to set up virtual memory */
Stephen Warren7333c6a2015-10-05 12:09:00 -0600464__weak void mmu_setup(void)
David Feng85fd5f12013-12-14 11:47:35 +0800465{
Thierry Reding59c364d2015-07-22 17:10:11 -0600466 int el;
David Feng85fd5f12013-12-14 11:47:35 +0800467
Alexander Grafe317fe82016-03-04 01:09:47 +0100468 /* Set up page tables only once */
469 if (!gd->arch.tlb_fillptr)
470 setup_all_pgtables();
Alexander Graffb74cc12016-03-04 01:09:45 +0100471
472 el = current_el();
Andre Przywara630a7942022-06-14 00:11:10 +0100473 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(NULL, NULL),
Alexander Graffb74cc12016-03-04 01:09:45 +0100474 MEMORY_ATTRIBUTES);
Alexander Graffb74cc12016-03-04 01:09:45 +0100475
David Feng85fd5f12013-12-14 11:47:35 +0800476 /* enable the mmu */
477 set_sctlr(get_sctlr() | CR_M);
478}
479
480/*
481 * Performs a invalidation of the entire data cache at all levels
482 */
483void invalidate_dcache_all(void)
484{
Marc Zyngierb67855c2023-02-09 04:54:27 +0800485#ifndef CONFIG_CMO_BY_VA_ONLY
York Sunef042012014-02-26 13:26:04 -0800486 __asm_invalidate_dcache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600487 __asm_invalidate_l3_dcache();
Marc Zyngierb67855c2023-02-09 04:54:27 +0800488#else
489 apply_cmo_to_mappings(invalidate_dcache_range);
490#endif
David Feng85fd5f12013-12-14 11:47:35 +0800491}
492
493/*
York Sun1ce575f2015-01-06 13:18:42 -0800494 * Performs a clean & invalidation of the entire data cache at all levels.
495 * This function needs to be inline to avoid using stack.
Stephen Warrenddb0f632016-10-19 15:18:46 -0600496 * __asm_flush_l3_dcache return status of timeout
David Feng85fd5f12013-12-14 11:47:35 +0800497 */
York Sun1ce575f2015-01-06 13:18:42 -0800498inline void flush_dcache_all(void)
David Feng85fd5f12013-12-14 11:47:35 +0800499{
Marc Zyngierb67855c2023-02-09 04:54:27 +0800500#ifndef CONFIG_CMO_BY_VA_ONLY
York Sun1ce575f2015-01-06 13:18:42 -0800501 int ret;
502
David Feng85fd5f12013-12-14 11:47:35 +0800503 __asm_flush_dcache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600504 ret = __asm_flush_l3_dcache();
York Sun1ce575f2015-01-06 13:18:42 -0800505 if (ret)
506 debug("flushing dcache returns 0x%x\n", ret);
507 else
508 debug("flushing dcache successfully.\n");
Marc Zyngierb67855c2023-02-09 04:54:27 +0800509#else
510 apply_cmo_to_mappings(flush_dcache_range);
511#endif
David Feng85fd5f12013-12-14 11:47:35 +0800512}
513
Vignesh Raghavendra384c1412019-04-22 21:43:32 +0530514#ifndef CONFIG_SYS_DISABLE_DCACHE_OPS
David Feng85fd5f12013-12-14 11:47:35 +0800515/*
516 * Invalidates range in all levels of D-cache/unified cache
517 */
518void invalidate_dcache_range(unsigned long start, unsigned long stop)
519{
Simon Glass4415c3b2017-04-05 17:53:18 -0600520 __asm_invalidate_dcache_range(start, stop);
David Feng85fd5f12013-12-14 11:47:35 +0800521}
522
523/*
524 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
525 */
526void flush_dcache_range(unsigned long start, unsigned long stop)
527{
528 __asm_flush_dcache_range(start, stop);
529}
Vignesh Raghavendra384c1412019-04-22 21:43:32 +0530530#else
531void invalidate_dcache_range(unsigned long start, unsigned long stop)
532{
533}
534
535void flush_dcache_range(unsigned long start, unsigned long stop)
536{
537}
538#endif /* CONFIG_SYS_DISABLE_DCACHE_OPS */
David Feng85fd5f12013-12-14 11:47:35 +0800539
540void dcache_enable(void)
541{
542 /* The data cache is not active unless the mmu is enabled */
543 if (!(get_sctlr() & CR_M)) {
544 invalidate_dcache_all();
545 __asm_invalidate_tlb_all();
546 mmu_setup();
547 }
548
Pali Rohárfbddaee2022-09-14 13:37:46 +0200549 /* Set up page tables only once (it is done also by mmu_setup()) */
550 if (!gd->arch.tlb_fillptr)
551 setup_all_pgtables();
552
David Feng85fd5f12013-12-14 11:47:35 +0800553 set_sctlr(get_sctlr() | CR_C);
554}
555
556void dcache_disable(void)
557{
558 uint32_t sctlr;
559
560 sctlr = get_sctlr();
561
562 /* if cache isn't enabled no need to disable */
563 if (!(sctlr & CR_C))
564 return;
565
Marc Zyngierb67855c2023-02-09 04:54:27 +0800566 if (IS_ENABLED(CONFIG_CMO_BY_VA_ONLY)) {
567 /*
568 * When invalidating by VA, do it *before* turning the MMU
569 * off, so that at least our stack is coherent.
570 */
571 flush_dcache_all();
572 }
573
David Feng85fd5f12013-12-14 11:47:35 +0800574 set_sctlr(sctlr & ~(CR_C|CR_M));
575
Marc Zyngierb67855c2023-02-09 04:54:27 +0800576 if (!IS_ENABLED(CONFIG_CMO_BY_VA_ONLY))
577 flush_dcache_all();
578
David Feng85fd5f12013-12-14 11:47:35 +0800579 __asm_invalidate_tlb_all();
580}
581
582int dcache_status(void)
583{
584 return (get_sctlr() & CR_C) != 0;
585}
586
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530587u64 *__weak arch_get_page_table(void) {
588 puts("No page table offset defined\n");
589
590 return NULL;
591}
592
Alexander Grafe317fe82016-03-04 01:09:47 +0100593static bool is_aligned(u64 addr, u64 size, u64 align)
594{
595 return !(addr & (align - 1)) && !(size & (align - 1));
596}
597
York Sun5bb14e02017-03-06 09:02:33 -0800598/* Use flag to indicate if attrs has more than d-cache attributes */
599static u64 set_one_region(u64 start, u64 size, u64 attrs, bool flag, int level)
Alexander Grafe317fe82016-03-04 01:09:47 +0100600{
601 int levelshift = level2shift(level);
602 u64 levelsize = 1ULL << levelshift;
603 u64 *pte = find_pte(start, level);
604
605 /* Can we can just modify the current level block PTE? */
606 if (is_aligned(start, size, levelsize)) {
York Sun5bb14e02017-03-06 09:02:33 -0800607 if (flag) {
608 *pte &= ~PMD_ATTRMASK;
609 *pte |= attrs & PMD_ATTRMASK;
610 } else {
611 *pte &= ~PMD_ATTRINDX_MASK;
612 *pte |= attrs & PMD_ATTRINDX_MASK;
613 }
Alexander Grafe317fe82016-03-04 01:09:47 +0100614 debug("Set attrs=%llx pte=%p level=%d\n", attrs, pte, level);
615
616 return levelsize;
617 }
618
619 /* Unaligned or doesn't fit, maybe split block into table */
620 debug("addr=%llx level=%d pte=%p (%llx)\n", start, level, pte, *pte);
621
622 /* Maybe we need to split the block into a table */
623 if (pte_type(pte) == PTE_TYPE_BLOCK)
624 split_block(pte, level);
625
626 /* And then double-check it became a table or already is one */
627 if (pte_type(pte) != PTE_TYPE_TABLE)
628 panic("PTE %p (%llx) for addr=%llx should be a table",
629 pte, *pte, start);
630
631 /* Roll on to the next page table level */
632 return 0;
633}
634
635void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
636 enum dcache_option option)
637{
Peng Fan41bad3e2020-05-11 16:41:07 +0800638 u64 attrs = PMD_ATTRINDX(option >> 2);
Alexander Grafe317fe82016-03-04 01:09:47 +0100639 u64 real_start = start;
640 u64 real_size = size;
641
642 debug("start=%lx size=%lx\n", (ulong)start, (ulong)size);
643
York Suna81fcd12016-06-24 16:46:20 -0700644 if (!gd->arch.tlb_emerg)
645 panic("Emergency page table not setup.");
646
Alexander Grafe317fe82016-03-04 01:09:47 +0100647 /*
648 * We can not modify page tables that we're currently running on,
649 * so we first need to switch to the "emergency" page tables where
650 * we can safely modify our primary page tables and then switch back
651 */
652 __asm_switch_ttbr(gd->arch.tlb_emerg);
653
654 /*
655 * Loop through the address range until we find a page granule that fits
656 * our alignment constraints, then set it to the new cache attributes
657 */
658 while (size > 0) {
659 int level;
660 u64 r;
661
662 for (level = 1; level < 4; level++) {
York Sun5bb14e02017-03-06 09:02:33 -0800663 /* Set d-cache attributes only */
664 r = set_one_region(start, size, attrs, false, level);
Alexander Grafe317fe82016-03-04 01:09:47 +0100665 if (r) {
666 /* PTE successfully replaced */
667 size -= r;
668 start += r;
669 break;
670 }
671 }
672
673 }
674
675 /* We're done modifying page tables, switch back to our primary ones */
676 __asm_switch_ttbr(gd->arch.tlb_addr);
677
678 /*
679 * Make sure there's nothing stale in dcache for a region that might
680 * have caches off now
681 */
682 flush_dcache_range(real_start, real_start + real_size);
683}
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700684
York Sun5bb14e02017-03-06 09:02:33 -0800685/*
686 * Modify MMU table for a region with updated PXN/UXN/Memory type/valid bits.
687 * The procecess is break-before-make. The target region will be marked as
688 * invalid during the process of changing.
689 */
690void mmu_change_region_attr(phys_addr_t addr, size_t siz, u64 attrs)
691{
692 int level;
693 u64 r, size, start;
694
695 start = addr;
696 size = siz;
697 /*
698 * Loop through the address range until we find a page granule that fits
699 * our alignment constraints, then set it to "invalid".
700 */
701 while (size > 0) {
702 for (level = 1; level < 4; level++) {
703 /* Set PTE to fault */
704 r = set_one_region(start, size, PTE_TYPE_FAULT, true,
705 level);
706 if (r) {
707 /* PTE successfully invalidated */
708 size -= r;
709 start += r;
710 break;
711 }
712 }
713 }
714
715 flush_dcache_range(gd->arch.tlb_addr,
716 gd->arch.tlb_addr + gd->arch.tlb_size);
717 __asm_invalidate_tlb_all();
718
719 /*
720 * Loop through the address range until we find a page granule that fits
721 * our alignment constraints, then set it to the new cache attributes
722 */
723 start = addr;
724 size = siz;
725 while (size > 0) {
726 for (level = 1; level < 4; level++) {
727 /* Set PTE to new attributes */
728 r = set_one_region(start, size, attrs, true, level);
729 if (r) {
730 /* PTE successfully updated */
731 size -= r;
732 start += r;
733 break;
734 }
735 }
736 }
737 flush_dcache_range(gd->arch.tlb_addr,
738 gd->arch.tlb_addr + gd->arch.tlb_size);
739 __asm_invalidate_tlb_all();
740}
741
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400742#else /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +0800743
Alexander Grafbc40da92016-03-04 01:09:55 +0100744/*
745 * For SPL builds, we may want to not have dcache enabled. Any real U-Boot
746 * running however really wants to have dcache and the MMU active. Check that
747 * everything is sane and give the developer a hint if it isn't.
748 */
749#ifndef CONFIG_SPL_BUILD
750#error Please describe your MMU layout in CONFIG_SYS_MEM_MAP and enable dcache.
751#endif
752
David Feng85fd5f12013-12-14 11:47:35 +0800753void invalidate_dcache_all(void)
754{
755}
756
757void flush_dcache_all(void)
758{
759}
760
David Feng85fd5f12013-12-14 11:47:35 +0800761void dcache_enable(void)
762{
763}
764
765void dcache_disable(void)
766{
767}
768
769int dcache_status(void)
770{
771 return 0;
772}
773
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530774void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
775 enum dcache_option option)
776{
777}
778
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400779#endif /* !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +0800780
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400781#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
David Feng85fd5f12013-12-14 11:47:35 +0800782
783void icache_enable(void)
784{
Stephen Warrenddb0f632016-10-19 15:18:46 -0600785 invalidate_icache_all();
David Feng85fd5f12013-12-14 11:47:35 +0800786 set_sctlr(get_sctlr() | CR_I);
787}
788
789void icache_disable(void)
790{
791 set_sctlr(get_sctlr() & ~CR_I);
792}
793
794int icache_status(void)
795{
796 return (get_sctlr() & CR_I) != 0;
797}
798
Patrice Chotardee435c62021-07-19 11:21:51 +0200799int mmu_status(void)
800{
801 return (get_sctlr() & CR_M) != 0;
802}
803
David Feng85fd5f12013-12-14 11:47:35 +0800804void invalidate_icache_all(void)
805{
806 __asm_invalidate_icache_all();
Stephen Warrenddb0f632016-10-19 15:18:46 -0600807 __asm_invalidate_l3_icache();
David Feng85fd5f12013-12-14 11:47:35 +0800808}
809
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400810#else /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +0800811
812void icache_enable(void)
813{
814}
815
816void icache_disable(void)
817{
818}
819
820int icache_status(void)
821{
822 return 0;
823}
824
Patrice Chotardee435c62021-07-19 11:21:51 +0200825int mmu_status(void)
826{
827 return 0;
828}
829
David Feng85fd5f12013-12-14 11:47:35 +0800830void invalidate_icache_all(void)
831{
832}
833
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400834#endif /* !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) */
David Feng85fd5f12013-12-14 11:47:35 +0800835
836/*
837 * Enable dCache & iCache, whether cache is actually enabled
838 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
839 */
York Suna84cd722014-06-23 15:15:54 -0700840void __weak enable_caches(void)
David Feng85fd5f12013-12-14 11:47:35 +0800841{
842 icache_enable();
843 dcache_enable();
844}