3 * David Feng <fenghua@phytium.com.cn>
6 * Alexander Graf <agraf@suse.de>
8 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/system.h>
13 #include <asm/armv8/mmu.h>
15 DECLARE_GLOBAL_DATA_PTR;
17 #ifndef CONFIG_SYS_DCACHE_OFF
20 * With 4k page granule, a virtual address is split into 4 lookup parts
21 * spanning 9 bits each:
23 * _______________________________________________
25 * | 0 | Lv0 | Lv1 | Lv2 | Lv3 | off |
26 * |_______|_______|_______|_______|_______|_______|
27 * 63-48 47-39 38-30 29-21 20-12 11-00
31 * Lv0: FF8000000000 --
38 #ifdef CONFIG_SYS_FULL_VA
39 static u64 get_tcr(int el, u64 *pips, u64 *pva_bits)
46 /* Find the largest address we need to support */
47 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
48 max_addr = max(max_addr, mem_map[i].base + mem_map[i].size);
50 /* Calculate the maximum physical (and thus virtual) address */
51 if (max_addr > (1ULL << 44)) {
54 } else if (max_addr > (1ULL << 42)) {
57 } else if (max_addr > (1ULL << 40)) {
60 } else if (max_addr > (1ULL << 36)) {
63 } else if (max_addr > (1ULL << 32)) {
72 tcr = TCR_EL1_RSVD | (ips << 32) | TCR_EPD1_DISABLE;
74 tcr = TCR_EL2_RSVD | (ips << 16);
76 tcr = TCR_EL3_RSVD | (ips << 16);
79 /* PTWs cacheable, inner/outer WBWA and inner shareable */
80 tcr |= TCR_TG0_4K | TCR_SHARED_INNER | TCR_ORGN_WBWA | TCR_IRGN_WBWA;
81 tcr |= TCR_T0SZ(va_bits);
91 #define MAX_PTE_ENTRIES 512
93 static int pte_type(u64 *pte)
95 return *pte & PTE_TYPE_MASK;
98 /* Returns the LSB number for a PTE on level <level> */
99 static int level2shift(int level)
101 /* Page is 12 bits wide, every level translates 9 bits */
102 return (12 + 9 * (3 - level));
105 static u64 *find_pte(u64 addr, int level)
113 debug("addr=%llx level=%d\n", addr, level);
115 get_tcr(0, NULL, &va_bits);
119 if (level < start_level)
122 /* Walk through all page table levels to find our PTE */
123 pte = (u64*)gd->arch.tlb_addr;
124 for (i = start_level; i < 4; i++) {
125 idx = (addr >> level2shift(i)) & 0x1FF;
127 debug("idx=%llx PTE %p at level %d: %llx\n", idx, pte, i, *pte);
132 /* PTE is no table (either invalid or block), can't traverse */
133 if (pte_type(pte) != PTE_TYPE_TABLE)
135 /* Off to the next level */
136 pte = (u64*)(*pte & 0x0000fffffffff000ULL);
139 /* Should never reach here */
143 /* Returns and creates a new full table (512 entries) */
144 static u64 *create_table(void)
146 u64 *new_table = (u64*)gd->arch.tlb_fillptr;
147 u64 pt_len = MAX_PTE_ENTRIES * sizeof(u64);
149 /* Allocate MAX_PTE_ENTRIES pte entries */
150 gd->arch.tlb_fillptr += pt_len;
152 if (gd->arch.tlb_fillptr - gd->arch.tlb_addr > gd->arch.tlb_size)
153 panic("Insufficient RAM for page table: 0x%lx > 0x%lx. "
154 "Please increase the size in get_page_table_size()",
155 gd->arch.tlb_fillptr - gd->arch.tlb_addr,
158 /* Mark all entries as invalid */
159 memset(new_table, 0, pt_len);
164 static void set_pte_table(u64 *pte, u64 *table)
166 /* Point *pte to the new table */
167 debug("Setting %p to addr=%p\n", pte, table);
168 *pte = PTE_TYPE_TABLE | (ulong)table;
171 /* Add one mm_region map entry to the page tables */
172 static void add_map(struct mm_region *map)
175 u64 addr = map->base;
176 u64 size = map->size;
177 u64 attrs = map->attrs | PTE_TYPE_BLOCK | PTE_BLOCK_AF;
183 pte = find_pte(addr, 0);
184 if (pte && (pte_type(pte) == PTE_TYPE_FAULT)) {
185 debug("Creating table for addr 0x%llx\n", addr);
186 new_table = create_table();
187 set_pte_table(pte, new_table);
190 for (level = 1; level < 4; level++) {
191 pte = find_pte(addr, level);
192 blocksize = 1ULL << level2shift(level);
193 debug("Checking if pte fits for addr=%llx size=%llx "
194 "blocksize=%llx\n", addr, size, blocksize);
195 if (size >= blocksize && !(addr & (blocksize - 1))) {
196 /* Page fits, create block PTE */
197 debug("Setting PTE %p to block addr=%llx\n",
203 } else if ((pte_type(pte) == PTE_TYPE_FAULT)) {
204 /* Page doesn't fit, create subpages */
205 debug("Creating subtable for addr 0x%llx "
206 "blksize=%llx\n", addr, blocksize);
207 new_table = create_table();
208 set_pte_table(pte, new_table);
214 /* Splits a block PTE into table with subpages spanning the old block */
215 static void split_block(u64 *pte, int level)
220 /* level describes the parent level, we need the child ones */
221 int levelshift = level2shift(level + 1);
223 if (pte_type(pte) != PTE_TYPE_BLOCK)
224 panic("PTE %p (%llx) is not a block. Some driver code wants to "
225 "modify dcache settings for an range not covered in "
226 "mem_map.", pte, old_pte);
228 new_table = create_table();
229 debug("Splitting pte %p (%llx) into %p\n", pte, old_pte, new_table);
231 for (i = 0; i < MAX_PTE_ENTRIES; i++) {
232 new_table[i] = old_pte | (i << levelshift);
234 /* Level 3 block PTEs have the table type */
235 if ((level + 1) == 3)
236 new_table[i] |= PTE_TYPE_TABLE;
238 debug("Setting new_table[%lld] = %llx\n", i, new_table[i]);
241 /* Set the new table into effect */
242 set_pte_table(pte, new_table);
252 * This is a recursively called function to count the number of
253 * page tables we need to cover a particular PTE range. If you
254 * call this with level = -1 you basically get the full 48 bit
257 static int count_required_pts(u64 addr, int level, u64 maxaddr)
259 int levelshift = level2shift(level);
260 u64 levelsize = 1ULL << levelshift;
261 u64 levelmask = levelsize - 1;
262 u64 levelend = addr + levelsize;
265 enum pte_type pte_type = PTE_INVAL;
267 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++) {
268 struct mm_region *map = &mem_map[i];
269 u64 start = map->base;
270 u64 end = start + map->size;
272 /* Check if the PTE would overlap with the map */
273 if (max(addr, start) <= min(levelend, end)) {
274 start = max(addr, start);
275 end = min(levelend, end);
277 /* We need a sub-pt for this level */
278 if ((start & levelmask) || (end & levelmask)) {
279 pte_type = PTE_LEVEL;
283 /* Lv0 can not do block PTEs, so do levels here too */
285 pte_type = PTE_LEVEL;
289 /* PTE is active, but fits into a block */
290 pte_type = PTE_BLOCK;
295 * Block PTEs at this level are already covered by the parent page
296 * table, so we only need to count sub page tables.
298 if (pte_type == PTE_LEVEL) {
299 int sublevel = level + 1;
300 u64 sublevelsize = 1ULL << level2shift(sublevel);
302 /* Account for the new sub page table ... */
305 /* ... and for all child page tables that one might have */
306 for (i = 0; i < MAX_PTE_ENTRIES; i++) {
307 r += count_required_pts(addr, sublevel, maxaddr);
308 addr += sublevelsize;
310 if (addr >= maxaddr) {
312 * We reached the end of address space, no need
313 * to look any further.
323 /* Returns the estimated required size of all page tables */
324 u64 get_page_table_size(void)
326 u64 one_pt = MAX_PTE_ENTRIES * sizeof(u64);
331 get_tcr(0, NULL, &va_bits);
335 /* Account for all page tables we would need to cover our memory map */
336 size = one_pt * count_required_pts(0, start_level - 1, 1ULL << va_bits);
339 * We need to duplicate our page table once to have an emergency pt to
340 * resort to when splitting page tables later on
345 * We may need to split page tables later on if dcache settings change,
346 * so reserve up to 4 (random pick) page tables for that.
353 static void setup_pgtables(void)
358 * Allocate the first level we're on with invalidate entries.
359 * If the starting level is 0 (va_bits >= 39), then this is our
360 * Lv0 page table, otherwise it's the entry Lv1 page table.
364 /* Now add all MMU table entries one after another to the table */
365 for (i = 0; mem_map[i].size || mem_map[i].attrs; i++)
366 add_map(&mem_map[i]);
368 /* Create the same thing once more for our emergency page table */
372 static void setup_all_pgtables(void)
374 u64 tlb_addr = gd->arch.tlb_addr;
376 /* Reset the fill ptr */
377 gd->arch.tlb_fillptr = tlb_addr;
379 /* Create normal system page tables */
382 /* Create emergency page tables */
383 gd->arch.tlb_addr = gd->arch.tlb_fillptr;
385 gd->arch.tlb_emerg = gd->arch.tlb_addr;
386 gd->arch.tlb_addr = tlb_addr;
391 inline void set_pgtable_section(u64 *page_table, u64 index, u64 section,
392 u64 memory_type, u64 attribute)
396 value = section | PMD_TYPE_SECT | PMD_SECT_AF;
397 value |= PMD_ATTRINDX(memory_type);
399 page_table[index] = value;
402 inline void set_pgtable_table(u64 *page_table, u64 index, u64 *table_addr)
406 value = (u64)table_addr | PMD_TYPE_TABLE;
407 page_table[index] = value;
411 /* to activate the MMU we need to set up virtual memory */
412 __weak void mmu_setup(void)
414 #ifndef CONFIG_SYS_FULL_VA
416 u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
420 #ifdef CONFIG_SYS_FULL_VA
421 /* Set up page tables only once */
422 if (!gd->arch.tlb_fillptr)
423 setup_all_pgtables();
426 set_ttbr_tcr_mair(el, gd->arch.tlb_addr, get_tcr(el, NULL, NULL),
429 /* Setup an identity-mapping for all spaces */
430 for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
431 set_pgtable_section(page_table, i, i << SECTION_SHIFT,
432 MT_DEVICE_NGNRNE, PMD_SECT_NON_SHARE);
435 /* Setup an identity-mapping for all RAM space */
436 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
437 ulong start = bd->bi_dram[i].start;
438 ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
439 for (j = start >> SECTION_SHIFT;
440 j < end >> SECTION_SHIFT; j++) {
441 set_pgtable_section(page_table, j, j << SECTION_SHIFT,
442 MT_NORMAL, PMD_SECT_NON_SHARE);
449 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
450 TCR_EL1_RSVD | TCR_FLAGS | TCR_EL1_IPS_BITS,
452 } else if (el == 2) {
453 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
454 TCR_EL2_RSVD | TCR_FLAGS | TCR_EL2_IPS_BITS,
457 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
458 TCR_EL3_RSVD | TCR_FLAGS | TCR_EL3_IPS_BITS,
464 set_sctlr(get_sctlr() | CR_M);
468 * Performs a invalidation of the entire data cache at all levels
470 void invalidate_dcache_all(void)
472 __asm_invalidate_dcache_all();
476 * Performs a clean & invalidation of the entire data cache at all levels.
477 * This function needs to be inline to avoid using stack.
478 * __asm_flush_l3_cache return status of timeout
480 inline void flush_dcache_all(void)
484 __asm_flush_dcache_all();
485 ret = __asm_flush_l3_cache();
487 debug("flushing dcache returns 0x%x\n", ret);
489 debug("flushing dcache successfully.\n");
493 * Invalidates range in all levels of D-cache/unified cache
495 void invalidate_dcache_range(unsigned long start, unsigned long stop)
497 __asm_flush_dcache_range(start, stop);
501 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
503 void flush_dcache_range(unsigned long start, unsigned long stop)
505 __asm_flush_dcache_range(start, stop);
508 void dcache_enable(void)
510 /* The data cache is not active unless the mmu is enabled */
511 if (!(get_sctlr() & CR_M)) {
512 invalidate_dcache_all();
513 __asm_invalidate_tlb_all();
517 set_sctlr(get_sctlr() | CR_C);
520 void dcache_disable(void)
526 /* if cache isn't enabled no need to disable */
530 set_sctlr(sctlr & ~(CR_C|CR_M));
533 __asm_invalidate_tlb_all();
536 int dcache_status(void)
538 return (get_sctlr() & CR_C) != 0;
541 u64 *__weak arch_get_page_table(void) {
542 puts("No page table offset defined\n");
547 #ifndef CONFIG_SYS_FULL_VA
548 void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
549 enum dcache_option option)
551 u64 *page_table = arch_get_page_table();
554 if (page_table == NULL)
557 end = ALIGN(start + size, (1 << MMU_SECTION_SHIFT)) >>
559 start = start >> MMU_SECTION_SHIFT;
560 for (upto = start; upto < end; upto++) {
561 page_table[upto] &= ~PMD_ATTRINDX_MASK;
562 page_table[upto] |= PMD_ATTRINDX(option);
564 asm volatile("dsb sy");
565 __asm_invalidate_tlb_all();
566 asm volatile("dsb sy");
568 start = start << MMU_SECTION_SHIFT;
569 end = end << MMU_SECTION_SHIFT;
570 flush_dcache_range(start, end);
571 asm volatile("dsb sy");
574 static bool is_aligned(u64 addr, u64 size, u64 align)
576 return !(addr & (align - 1)) && !(size & (align - 1));
579 static u64 set_one_region(u64 start, u64 size, u64 attrs, int level)
581 int levelshift = level2shift(level);
582 u64 levelsize = 1ULL << levelshift;
583 u64 *pte = find_pte(start, level);
585 /* Can we can just modify the current level block PTE? */
586 if (is_aligned(start, size, levelsize)) {
587 *pte &= ~PMD_ATTRINDX_MASK;
589 debug("Set attrs=%llx pte=%p level=%d\n", attrs, pte, level);
594 /* Unaligned or doesn't fit, maybe split block into table */
595 debug("addr=%llx level=%d pte=%p (%llx)\n", start, level, pte, *pte);
597 /* Maybe we need to split the block into a table */
598 if (pte_type(pte) == PTE_TYPE_BLOCK)
599 split_block(pte, level);
601 /* And then double-check it became a table or already is one */
602 if (pte_type(pte) != PTE_TYPE_TABLE)
603 panic("PTE %p (%llx) for addr=%llx should be a table",
606 /* Roll on to the next page table level */
610 void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
611 enum dcache_option option)
613 u64 attrs = PMD_ATTRINDX(option);
614 u64 real_start = start;
615 u64 real_size = size;
617 debug("start=%lx size=%lx\n", (ulong)start, (ulong)size);
620 * We can not modify page tables that we're currently running on,
621 * so we first need to switch to the "emergency" page tables where
622 * we can safely modify our primary page tables and then switch back
624 __asm_switch_ttbr(gd->arch.tlb_emerg);
627 * Loop through the address range until we find a page granule that fits
628 * our alignment constraints, then set it to the new cache attributes
634 for (level = 1; level < 4; level++) {
635 r = set_one_region(start, size, attrs, level);
637 /* PTE successfully replaced */
646 /* We're done modifying page tables, switch back to our primary ones */
647 __asm_switch_ttbr(gd->arch.tlb_addr);
650 * Make sure there's nothing stale in dcache for a region that might
651 * have caches off now
653 flush_dcache_range(real_start, real_start + real_size);
657 #else /* CONFIG_SYS_DCACHE_OFF */
659 void invalidate_dcache_all(void)
663 void flush_dcache_all(void)
667 void dcache_enable(void)
671 void dcache_disable(void)
675 int dcache_status(void)
680 void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
681 enum dcache_option option)
685 #endif /* CONFIG_SYS_DCACHE_OFF */
687 #ifndef CONFIG_SYS_ICACHE_OFF
689 void icache_enable(void)
691 __asm_invalidate_icache_all();
692 set_sctlr(get_sctlr() | CR_I);
695 void icache_disable(void)
697 set_sctlr(get_sctlr() & ~CR_I);
700 int icache_status(void)
702 return (get_sctlr() & CR_I) != 0;
705 void invalidate_icache_all(void)
707 __asm_invalidate_icache_all();
710 #else /* CONFIG_SYS_ICACHE_OFF */
712 void icache_enable(void)
716 void icache_disable(void)
720 int icache_status(void)
725 void invalidate_icache_all(void)
729 #endif /* CONFIG_SYS_ICACHE_OFF */
732 * Enable dCache & iCache, whether cache is actually enabled
733 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
735 void __weak enable_caches(void)