blob: 6bde1cf6a00e01f97f258fd38733de8db179152a [file] [log] [blame]
David Feng0ae76532013-12-14 11:47:35 +08001/*
2 * (C) Copyright 2013
3 * David Feng <fenghua@phytium.com.cn>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <asm/system.h>
10#include <asm/armv8/mmu.h>
11
12DECLARE_GLOBAL_DATA_PTR;
13
14#ifndef CONFIG_SYS_DCACHE_OFF
Alison Wang99799222015-08-18 11:22:05 +080015inline void set_pgtable_section(u64 *page_table, u64 index, u64 section,
16 u64 memory_type, u64 share)
David Feng0ae76532013-12-14 11:47:35 +080017{
David Feng0ae76532013-12-14 11:47:35 +080018 u64 value;
19
York Sun22932ff2014-06-23 15:15:53 -070020 value = section | PMD_TYPE_SECT | PMD_SECT_AF;
David Feng0ae76532013-12-14 11:47:35 +080021 value |= PMD_ATTRINDX(memory_type);
Alison Wang99799222015-08-18 11:22:05 +080022 value |= share;
23 page_table[index] = value;
24}
25
26inline void set_pgtable_table(u64 *page_table, u64 index, u64 *table_addr)
27{
28 u64 value;
29
30 value = (u64)table_addr | PMD_TYPE_TABLE;
York Sun22932ff2014-06-23 15:15:53 -070031 page_table[index] = value;
David Feng0ae76532013-12-14 11:47:35 +080032}
33
34/* to activate the MMU we need to set up virtual memory */
35static void mmu_setup(void)
36{
David Feng0ae76532013-12-14 11:47:35 +080037 bd_t *bd = gd->bd;
Thierry Reding8b19dff2015-07-22 17:10:11 -060038 u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
39 int el;
David Feng0ae76532013-12-14 11:47:35 +080040
41 /* Setup an identity-mapping for all spaces */
York Sun22932ff2014-06-23 15:15:53 -070042 for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
43 set_pgtable_section(page_table, i, i << SECTION_SHIFT,
Alison Wang99799222015-08-18 11:22:05 +080044 MT_DEVICE_NGNRNE, PMD_SECT_NON_SHARE);
York Sun22932ff2014-06-23 15:15:53 -070045 }
David Feng0ae76532013-12-14 11:47:35 +080046
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++) {
York Sun22932ff2014-06-23 15:15:53 -070053 set_pgtable_section(page_table, j, j << SECTION_SHIFT,
Alison Wang99799222015-08-18 11:22:05 +080054 MT_NORMAL, PMD_SECT_NON_SHARE);
David Feng0ae76532013-12-14 11:47:35 +080055 }
56 }
57
58 /* load TTBR0 */
59 el = current_el();
York Sunf5222cf2014-02-26 13:26:02 -080060 if (el == 1) {
York Sun22932ff2014-06-23 15:15:53 -070061 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
62 TCR_FLAGS | TCR_EL1_IPS_BITS,
63 MEMORY_ATTRIBUTES);
York Sunf5222cf2014-02-26 13:26:02 -080064 } else if (el == 2) {
York Sun22932ff2014-06-23 15:15:53 -070065 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
66 TCR_FLAGS | TCR_EL2_IPS_BITS,
67 MEMORY_ATTRIBUTES);
York Sunf5222cf2014-02-26 13:26:02 -080068 } else {
York Sun22932ff2014-06-23 15:15:53 -070069 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
70 TCR_FLAGS | TCR_EL3_IPS_BITS,
71 MEMORY_ATTRIBUTES);
York Sunf5222cf2014-02-26 13:26:02 -080072 }
David Feng0ae76532013-12-14 11:47:35 +080073 /* enable the mmu */
74 set_sctlr(get_sctlr() | CR_M);
75}
76
77/*
78 * Performs a invalidation of the entire data cache at all levels
79 */
80void invalidate_dcache_all(void)
81{
York Sun1e6ad552014-02-26 13:26:04 -080082 __asm_invalidate_dcache_all();
David Feng0ae76532013-12-14 11:47:35 +080083}
84
85/*
York Sundcd468b2015-01-06 13:18:42 -080086 * 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
David Feng0ae76532013-12-14 11:47:35 +080089 */
York Sundcd468b2015-01-06 13:18:42 -080090inline void flush_dcache_all(void)
David Feng0ae76532013-12-14 11:47:35 +080091{
York Sundcd468b2015-01-06 13:18:42 -080092 int ret;
93
David Feng0ae76532013-12-14 11:47:35 +080094 __asm_flush_dcache_all();
York Sundcd468b2015-01-06 13:18:42 -080095 ret = __asm_flush_l3_cache();
96 if (ret)
97 debug("flushing dcache returns 0x%x\n", ret);
98 else
99 debug("flushing dcache successfully.\n");
David Feng0ae76532013-12-14 11:47:35 +0800100}
101
102/*
103 * Invalidates range in all levels of D-cache/unified cache
104 */
105void invalidate_dcache_range(unsigned long start, unsigned long stop)
106{
107 __asm_flush_dcache_range(start, stop);
108}
109
110/*
111 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
112 */
113void flush_dcache_range(unsigned long start, unsigned long stop)
114{
115 __asm_flush_dcache_range(start, stop);
116}
117
118void dcache_enable(void)
119{
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();
124 mmu_setup();
125 }
126
127 set_sctlr(get_sctlr() | CR_C);
128}
129
130void dcache_disable(void)
131{
132 uint32_t sctlr;
133
134 sctlr = get_sctlr();
135
136 /* if cache isn't enabled no need to disable */
137 if (!(sctlr & CR_C))
138 return;
139
140 set_sctlr(sctlr & ~(CR_C|CR_M));
141
142 flush_dcache_all();
143 __asm_invalidate_tlb_all();
144}
145
146int dcache_status(void)
147{
148 return (get_sctlr() & CR_C) != 0;
149}
150
Siva Durga Prasad Paladugudad17fd2015-06-26 18:05:07 +0530151u64 *__weak arch_get_page_table(void) {
152 puts("No page table offset defined\n");
153
154 return NULL;
155}
156
157void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
158 enum dcache_option option)
159{
160 u64 *page_table = arch_get_page_table();
161 u64 upto, end;
162
163 if (page_table == NULL)
164 return;
165
166 end = ALIGN(start + size, (1 << MMU_SECTION_SHIFT)) >>
167 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);
172 }
173 asm volatile("dsb sy");
174 __asm_invalidate_tlb_all();
175 asm volatile("dsb sy");
176 asm volatile("isb");
177 start = start << MMU_SECTION_SHIFT;
178 end = end << MMU_SECTION_SHIFT;
179 flush_dcache_range(start, end);
180 asm volatile("dsb sy");
181}
David Feng0ae76532013-12-14 11:47:35 +0800182#else /* CONFIG_SYS_DCACHE_OFF */
183
184void invalidate_dcache_all(void)
185{
186}
187
188void flush_dcache_all(void)
189{
190}
191
David Feng0ae76532013-12-14 11:47:35 +0800192void dcache_enable(void)
193{
194}
195
196void dcache_disable(void)
197{
198}
199
200int dcache_status(void)
201{
202 return 0;
203}
204
Siva Durga Prasad Paladugudad17fd2015-06-26 18:05:07 +0530205void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
206 enum dcache_option option)
207{
208}
209
David Feng0ae76532013-12-14 11:47:35 +0800210#endif /* CONFIG_SYS_DCACHE_OFF */
211
212#ifndef CONFIG_SYS_ICACHE_OFF
213
214void icache_enable(void)
215{
York Sun1e6ad552014-02-26 13:26:04 -0800216 __asm_invalidate_icache_all();
David Feng0ae76532013-12-14 11:47:35 +0800217 set_sctlr(get_sctlr() | CR_I);
218}
219
220void icache_disable(void)
221{
222 set_sctlr(get_sctlr() & ~CR_I);
223}
224
225int icache_status(void)
226{
227 return (get_sctlr() & CR_I) != 0;
228}
229
230void invalidate_icache_all(void)
231{
232 __asm_invalidate_icache_all();
233}
234
235#else /* CONFIG_SYS_ICACHE_OFF */
236
237void icache_enable(void)
238{
239}
240
241void icache_disable(void)
242{
243}
244
245int icache_status(void)
246{
247 return 0;
248}
249
250void invalidate_icache_all(void)
251{
252}
253
254#endif /* CONFIG_SYS_ICACHE_OFF */
255
256/*
257 * Enable dCache & iCache, whether cache is actually enabled
258 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
259 */
York Sun2f78eae2014-06-23 15:15:54 -0700260void __weak enable_caches(void)
David Feng0ae76532013-12-14 11:47:35 +0800261{
262 icache_enable();
263 dcache_enable();
264}