blob: 71f0020c7f9ea74619536b004a00b22293b4ab7d [file] [log] [blame]
David Feng85fd5f12013-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
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -070015
16#ifdef CONFIG_SYS_FULL_VA
17static void set_ptl1_entry(u64 index, u64 ptl2_entry)
18{
19 u64 *pgd = (u64 *)gd->arch.tlb_addr;
20 u64 value;
21
22 value = ptl2_entry | PTL1_TYPE_TABLE;
23 pgd[index] = value;
24}
25
26static void set_ptl2_block(u64 ptl1, u64 bfn, u64 address, u64 memory_attrs)
27{
28 u64 *pmd = (u64 *)ptl1;
29 u64 value;
30
31 value = address | PTL2_TYPE_BLOCK | PTL2_BLOCK_AF;
32 value |= memory_attrs;
33 pmd[bfn] = value;
34}
35
36static struct mm_region mem_map[] = CONFIG_SYS_MEM_MAP;
37
38#define PTL1_ENTRIES CONFIG_SYS_PTL1_ENTRIES
39#define PTL2_ENTRIES CONFIG_SYS_PTL2_ENTRIES
40
41static void setup_pgtables(void)
42{
43 int l1_e, l2_e;
44 unsigned long pmd = 0;
45 unsigned long address;
46
47 /* Setup the PMD pointers */
48 for (l1_e = 0; l1_e < CONFIG_SYS_MEM_MAP_SIZE; l1_e++) {
49 gd->arch.pmd_addr[l1_e] = gd->arch.tlb_addr +
50 PTL1_ENTRIES * sizeof(u64);
51 gd->arch.pmd_addr[l1_e] += PTL2_ENTRIES * sizeof(u64) * l1_e;
52 gd->arch.pmd_addr[l1_e] = ALIGN(gd->arch.pmd_addr[l1_e],
53 0x10000UL);
54 }
55
56 /* Setup the page tables */
57 for (l1_e = 0; l1_e < PTL1_ENTRIES; l1_e++) {
58 if (mem_map[pmd].base ==
59 (uintptr_t)l1_e << PTL2_BITS) {
60 set_ptl1_entry(l1_e, gd->arch.pmd_addr[pmd]);
61
62 for (l2_e = 0; l2_e < PTL2_ENTRIES; l2_e++) {
63 address = mem_map[pmd].base
64 + (uintptr_t)l2_e * BLOCK_SIZE;
65 set_ptl2_block(gd->arch.pmd_addr[pmd], l2_e,
66 address, mem_map[pmd].attrs);
67 }
68
69 pmd++;
70 } else {
71 set_ptl1_entry(l1_e, 0);
72 }
73 }
74}
75
76#else
77
Alison Wang7f8e1782015-08-18 11:22:05 +080078inline void set_pgtable_section(u64 *page_table, u64 index, u64 section,
Alison Wange28e18c2015-11-05 11:15:49 +080079 u64 memory_type, u64 attribute)
David Feng85fd5f12013-12-14 11:47:35 +080080{
David Feng85fd5f12013-12-14 11:47:35 +080081 u64 value;
82
York Sunef631942014-06-23 15:15:53 -070083 value = section | PMD_TYPE_SECT | PMD_SECT_AF;
David Feng85fd5f12013-12-14 11:47:35 +080084 value |= PMD_ATTRINDX(memory_type);
Alison Wange28e18c2015-11-05 11:15:49 +080085 value |= attribute;
York Sunef631942014-06-23 15:15:53 -070086 page_table[index] = value;
David Feng85fd5f12013-12-14 11:47:35 +080087}
88
Alison Wang7f8e1782015-08-18 11:22:05 +080089inline void set_pgtable_table(u64 *page_table, u64 index, u64 *table_addr)
90{
91 u64 value;
92
93 value = (u64)table_addr | PMD_TYPE_TABLE;
94 page_table[index] = value;
95}
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -070096#endif
Alison Wang7f8e1782015-08-18 11:22:05 +080097
David Feng85fd5f12013-12-14 11:47:35 +080098/* to activate the MMU we need to set up virtual memory */
Stephen Warren7333c6a2015-10-05 12:09:00 -060099__weak void mmu_setup(void)
David Feng85fd5f12013-12-14 11:47:35 +0800100{
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700101#ifndef CONFIG_SYS_FULL_VA
David Feng85fd5f12013-12-14 11:47:35 +0800102 bd_t *bd = gd->bd;
Thierry Reding59c364d2015-07-22 17:10:11 -0600103 u64 *page_table = (u64 *)gd->arch.tlb_addr, i, j;
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700104#endif
Thierry Reding59c364d2015-07-22 17:10:11 -0600105 int el;
David Feng85fd5f12013-12-14 11:47:35 +0800106
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700107#ifdef CONFIG_SYS_FULL_VA
108 unsigned long coreid = read_mpidr() & CONFIG_COREID_MASK;
109
110 /* Set up page tables only on BSP */
111 if (coreid == BSP_COREID)
112 setup_pgtables();
113#else
David Feng85fd5f12013-12-14 11:47:35 +0800114 /* Setup an identity-mapping for all spaces */
York Sunef631942014-06-23 15:15:53 -0700115 for (i = 0; i < (PGTABLE_SIZE >> 3); i++) {
116 set_pgtable_section(page_table, i, i << SECTION_SHIFT,
Alison Wang7f8e1782015-08-18 11:22:05 +0800117 MT_DEVICE_NGNRNE, PMD_SECT_NON_SHARE);
York Sunef631942014-06-23 15:15:53 -0700118 }
David Feng85fd5f12013-12-14 11:47:35 +0800119
120 /* Setup an identity-mapping for all RAM space */
121 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
122 ulong start = bd->bi_dram[i].start;
123 ulong end = bd->bi_dram[i].start + bd->bi_dram[i].size;
124 for (j = start >> SECTION_SHIFT;
125 j < end >> SECTION_SHIFT; j++) {
York Sunef631942014-06-23 15:15:53 -0700126 set_pgtable_section(page_table, j, j << SECTION_SHIFT,
Alison Wang7f8e1782015-08-18 11:22:05 +0800127 MT_NORMAL, PMD_SECT_NON_SHARE);
David Feng85fd5f12013-12-14 11:47:35 +0800128 }
129 }
130
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700131#endif
David Feng85fd5f12013-12-14 11:47:35 +0800132 /* load TTBR0 */
133 el = current_el();
York Sun897947c2014-02-26 13:26:02 -0800134 if (el == 1) {
York Sunef631942014-06-23 15:15:53 -0700135 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
Thierry Redinga3e45ab2015-08-20 11:52:14 +0200136 TCR_EL1_RSVD | TCR_FLAGS | TCR_EL1_IPS_BITS,
York Sunef631942014-06-23 15:15:53 -0700137 MEMORY_ATTRIBUTES);
York Sun897947c2014-02-26 13:26:02 -0800138 } else if (el == 2) {
York Sunef631942014-06-23 15:15:53 -0700139 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
Thierry Redinga3e45ab2015-08-20 11:52:14 +0200140 TCR_EL2_RSVD | TCR_FLAGS | TCR_EL2_IPS_BITS,
York Sunef631942014-06-23 15:15:53 -0700141 MEMORY_ATTRIBUTES);
York Sun897947c2014-02-26 13:26:02 -0800142 } else {
York Sunef631942014-06-23 15:15:53 -0700143 set_ttbr_tcr_mair(el, gd->arch.tlb_addr,
Thierry Redinga3e45ab2015-08-20 11:52:14 +0200144 TCR_EL3_RSVD | TCR_FLAGS | TCR_EL3_IPS_BITS,
York Sunef631942014-06-23 15:15:53 -0700145 MEMORY_ATTRIBUTES);
York Sun897947c2014-02-26 13:26:02 -0800146 }
David Feng85fd5f12013-12-14 11:47:35 +0800147 /* enable the mmu */
148 set_sctlr(get_sctlr() | CR_M);
149}
150
151/*
152 * Performs a invalidation of the entire data cache at all levels
153 */
154void invalidate_dcache_all(void)
155{
York Sunef042012014-02-26 13:26:04 -0800156 __asm_invalidate_dcache_all();
David Feng85fd5f12013-12-14 11:47:35 +0800157}
158
159/*
York Sun1ce575f2015-01-06 13:18:42 -0800160 * Performs a clean & invalidation of the entire data cache at all levels.
161 * This function needs to be inline to avoid using stack.
162 * __asm_flush_l3_cache return status of timeout
David Feng85fd5f12013-12-14 11:47:35 +0800163 */
York Sun1ce575f2015-01-06 13:18:42 -0800164inline void flush_dcache_all(void)
David Feng85fd5f12013-12-14 11:47:35 +0800165{
York Sun1ce575f2015-01-06 13:18:42 -0800166 int ret;
167
David Feng85fd5f12013-12-14 11:47:35 +0800168 __asm_flush_dcache_all();
York Sun1ce575f2015-01-06 13:18:42 -0800169 ret = __asm_flush_l3_cache();
170 if (ret)
171 debug("flushing dcache returns 0x%x\n", ret);
172 else
173 debug("flushing dcache successfully.\n");
David Feng85fd5f12013-12-14 11:47:35 +0800174}
175
176/*
177 * Invalidates range in all levels of D-cache/unified cache
178 */
179void invalidate_dcache_range(unsigned long start, unsigned long stop)
180{
181 __asm_flush_dcache_range(start, stop);
182}
183
184/*
185 * Flush range(clean & invalidate) from all levels of D-cache/unified cache
186 */
187void flush_dcache_range(unsigned long start, unsigned long stop)
188{
189 __asm_flush_dcache_range(start, stop);
190}
191
192void dcache_enable(void)
193{
194 /* The data cache is not active unless the mmu is enabled */
195 if (!(get_sctlr() & CR_M)) {
196 invalidate_dcache_all();
197 __asm_invalidate_tlb_all();
198 mmu_setup();
199 }
200
201 set_sctlr(get_sctlr() | CR_C);
202}
203
204void dcache_disable(void)
205{
206 uint32_t sctlr;
207
208 sctlr = get_sctlr();
209
210 /* if cache isn't enabled no need to disable */
211 if (!(sctlr & CR_C))
212 return;
213
214 set_sctlr(sctlr & ~(CR_C|CR_M));
215
216 flush_dcache_all();
217 __asm_invalidate_tlb_all();
218}
219
220int dcache_status(void)
221{
222 return (get_sctlr() & CR_C) != 0;
223}
224
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530225u64 *__weak arch_get_page_table(void) {
226 puts("No page table offset defined\n");
227
228 return NULL;
229}
230
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700231#ifndef CONFIG_SYS_FULL_VA
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530232void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
233 enum dcache_option option)
234{
235 u64 *page_table = arch_get_page_table();
236 u64 upto, end;
237
238 if (page_table == NULL)
239 return;
240
241 end = ALIGN(start + size, (1 << MMU_SECTION_SHIFT)) >>
242 MMU_SECTION_SHIFT;
243 start = start >> MMU_SECTION_SHIFT;
244 for (upto = start; upto < end; upto++) {
245 page_table[upto] &= ~PMD_ATTRINDX_MASK;
246 page_table[upto] |= PMD_ATTRINDX(option);
247 }
248 asm volatile("dsb sy");
249 __asm_invalidate_tlb_all();
250 asm volatile("dsb sy");
251 asm volatile("isb");
252 start = start << MMU_SECTION_SHIFT;
253 end = end << MMU_SECTION_SHIFT;
254 flush_dcache_range(start, end);
255 asm volatile("dsb sy");
256}
Sergey Temerkhanov78eaa492015-10-14 09:55:45 -0700257#endif
258
David Feng85fd5f12013-12-14 11:47:35 +0800259#else /* CONFIG_SYS_DCACHE_OFF */
260
261void invalidate_dcache_all(void)
262{
263}
264
265void flush_dcache_all(void)
266{
267}
268
David Feng85fd5f12013-12-14 11:47:35 +0800269void dcache_enable(void)
270{
271}
272
273void dcache_disable(void)
274{
275}
276
277int dcache_status(void)
278{
279 return 0;
280}
281
Siva Durga Prasad Paladuguba2432a2015-06-26 18:05:07 +0530282void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
283 enum dcache_option option)
284{
285}
286
David Feng85fd5f12013-12-14 11:47:35 +0800287#endif /* CONFIG_SYS_DCACHE_OFF */
288
289#ifndef CONFIG_SYS_ICACHE_OFF
290
291void icache_enable(void)
292{
York Sunef042012014-02-26 13:26:04 -0800293 __asm_invalidate_icache_all();
David Feng85fd5f12013-12-14 11:47:35 +0800294 set_sctlr(get_sctlr() | CR_I);
295}
296
297void icache_disable(void)
298{
299 set_sctlr(get_sctlr() & ~CR_I);
300}
301
302int icache_status(void)
303{
304 return (get_sctlr() & CR_I) != 0;
305}
306
307void invalidate_icache_all(void)
308{
309 __asm_invalidate_icache_all();
310}
311
312#else /* CONFIG_SYS_ICACHE_OFF */
313
314void icache_enable(void)
315{
316}
317
318void icache_disable(void)
319{
320}
321
322int icache_status(void)
323{
324 return 0;
325}
326
327void invalidate_icache_all(void)
328{
329}
330
331#endif /* CONFIG_SYS_ICACHE_OFF */
332
333/*
334 * Enable dCache & iCache, whether cache is actually enabled
335 * depend on CONFIG_SYS_DCACHE_OFF and CONFIG_SYS_ICACHE_OFF
336 */
York Suna84cd722014-06-23 15:15:54 -0700337void __weak enable_caches(void)
David Feng85fd5f12013-12-14 11:47:35 +0800338{
339 icache_enable();
340 dcache_enable();
341}