3 * David Feng <fenghua@phytium.com.cn>
5 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm/system.h>
10 #include <asm/armv8/mmu.h>
12 DECLARE_GLOBAL_DATA_PTR;
14 #ifndef CONFIG_SYS_DCACHE_OFF
15 inline void set_pgtable_section(u64 *page_table, u64 index, u64 section,
16 u64 memory_type, u64 share)
20 value = section | PMD_TYPE_SECT | PMD_SECT_AF;
21 value |= PMD_ATTRINDX(memory_type);
23 page_table[index] = value;
26 inline void set_pgtable_table(u64 *page_table, u64 index, u64 *table_addr)
30 value = (u64)table_addr | PMD_TYPE_TABLE;
31 page_table[index] = value;
34 /* to activate the MMU we need to set up virtual memory */
35 static void mmu_setup(void)
38 u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
41 /* Setup an identity-mapping for all spaces */
42 for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
43 set_pgtable_section(page_table, i, i << SECTION_SHIFT,
44 MT_DEVICE_NGNRNE, PMD_SECT_NON_SHARE);
47 /* Setup an identity-mapping for all RAM space */
48 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
49 ulong start = bd->bi_dram[i].start;
50 ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
51 for (j = start >> SECTION_SHIFT;
52 j < end >> SECTION_SHIFT; j++) {
53 set_pgtable_section(page_table, j, j << SECTION_SHIFT,
54 MT_NORMAL, PMD_SECT_NON_SHARE);
61 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
62 TCR_FLAGS | TCR_EL1_IPS_BITS,
65 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
66 TCR_FLAGS | TCR_EL2_IPS_BITS,
69 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
70 TCR_FLAGS | TCR_EL3_IPS_BITS,
74 set_sctlr(get_sctlr() | CR_M);
78 * Performs a invalidation of the entire data cache at all levels
80 void invalidate_dcache_all(void)
82 __asm_invalidate_dcache_all();
86 * Performs a clean & invalidation of the entire data cache at all levels.
87 * This function needs to be inline to avoid using stack.
88 * __asm_flush_l3_cache return status of timeout
90 inline void flush_dcache_all(void)
94 __asm_flush_dcache_all();
95 ret = __asm_flush_l3_cache();
97 debug("flushing dcache returns 0x%x\n", ret);
99 debug("flushing dcache successfully.\n");
103 * Invalidates range in all levels of D-cache/unified cache
105 void invalidate_dcache_range(unsigned long start, unsigned long stop)
107 __asm_flush_dcache_range(start, stop);
111 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
113 void flush_dcache_range(unsigned long start, unsigned long stop)
115 __asm_flush_dcache_range(start, stop);
118 void dcache_enable(void)
120 /* The data cache is not active unless the mmu is enabled */
121 if (!(get_sctlr() & CR_M)) {
122 invalidate_dcache_all();
123 __asm_invalidate_tlb_all();
127 set_sctlr(get_sctlr() | CR_C);
130 void dcache_disable(void)
136 /* if cache isn't enabled no need to disable */
140 set_sctlr(sctlr & ~(CR_C|CR_M));
143 __asm_invalidate_tlb_all();
146 int dcache_status(void)
148 return (get_sctlr() & CR_C) != 0;
151 u64 *__weak arch_get_page_table(void) {
152 puts("No page table offset defined\n");
157 void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
158 enum dcache_option option)
160 u64 *page_table = arch_get_page_table();
163 if (page_table == NULL)
166 end = ALIGN(start + size, (1 << MMU_SECTION_SHIFT)) >>
168 start = start >> MMU_SECTION_SHIFT;
169 for (upto = start; upto < end; upto++) {
170 page_table[upto] &= ~PMD_ATTRINDX_MASK;
171 page_table[upto] |= PMD_ATTRINDX(option);
173 asm volatile("dsb sy");
174 __asm_invalidate_tlb_all();
175 asm volatile("dsb sy");
177 start = start << MMU_SECTION_SHIFT;
178 end = end << MMU_SECTION_SHIFT;
179 flush_dcache_range(start, end);
180 asm volatile("dsb sy");
182 #else /* CONFIG_SYS_DCACHE_OFF */
184 void invalidate_dcache_all(void)
188 void flush_dcache_all(void)
192 void dcache_enable(void)
196 void dcache_disable(void)
200 int dcache_status(void)
205 void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
206 enum dcache_option option)
210 #endif /* CONFIG_SYS_DCACHE_OFF */
212 #ifndef CONFIG_SYS_ICACHE_OFF
214 void icache_enable(void)
216 __asm_invalidate_icache_all();
217 set_sctlr(get_sctlr() | CR_I);
220 void icache_disable(void)
222 set_sctlr(get_sctlr() & ~CR_I);
225 int icache_status(void)
227 return (get_sctlr() & CR_I) != 0;
230 void invalidate_icache_all(void)
232 __asm_invalidate_icache_all();
235 #else /* CONFIG_SYS_ICACHE_OFF */
237 void icache_enable(void)
241 void icache_disable(void)
245 int icache_status(void)
250 void invalidate_icache_all(void)
254 #endif /* CONFIG_SYS_ICACHE_OFF */
257 * Enable dCache & iCache, whether cache is actually enabled
258 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
260 void __weak enable_caches(void)