common/board_f: Move arm-specific reserve_mmu to arch/arm/lib/cache.c
[oweals/u-boot.git] / arch / arm / lib / cache.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2002
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 /* for now: just dummy functions to satisfy the linker */
8
9 #include <common.h>
10 #include <cpu_func.h>
11 #include <malloc.h>
12
13 DECLARE_GLOBAL_DATA_PTR;
14
15 /*
16  * Flush range from all levels of d-cache/unified-cache.
17  * Affects the range [start, start + size - 1].
18  */
19 __weak void flush_cache(unsigned long start, unsigned long size)
20 {
21         flush_dcache_range(start, start + size);
22 }
23
24 /*
25  * Default implementation:
26  * do a range flush for the entire range
27  */
28 __weak void flush_dcache_all(void)
29 {
30         flush_cache(0, ~0);
31 }
32
33 /*
34  * Default implementation of enable_caches()
35  * Real implementation should be in platform code
36  */
37 __weak void enable_caches(void)
38 {
39         puts("WARNING: Caches not enabled\n");
40 }
41
42 __weak void invalidate_dcache_range(unsigned long start, unsigned long stop)
43 {
44         /* An empty stub, real implementation should be in platform code */
45 }
46 __weak void flush_dcache_range(unsigned long start, unsigned long stop)
47 {
48         /* An empty stub, real implementation should be in platform code */
49 }
50
51 int check_cache_range(unsigned long start, unsigned long stop)
52 {
53         int ok = 1;
54
55         if (start & (CONFIG_SYS_CACHELINE_SIZE - 1))
56                 ok = 0;
57
58         if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1))
59                 ok = 0;
60
61         if (!ok) {
62                 warn_non_spl("CACHE: Misaligned operation at range [%08lx, %08lx]\n",
63                              start, stop);
64         }
65
66         return ok;
67 }
68
69 #ifdef CONFIG_SYS_NONCACHED_MEMORY
70 /*
71  * Reserve one MMU section worth of address space below the malloc() area that
72  * will be mapped uncached.
73  */
74 static unsigned long noncached_start;
75 static unsigned long noncached_end;
76 static unsigned long noncached_next;
77
78 void noncached_init(void)
79 {
80         phys_addr_t start, end;
81         size_t size;
82
83         /* If this calculation changes, update board_f.c:reserve_noncached() */
84         end = ALIGN(mem_malloc_start, MMU_SECTION_SIZE) - MMU_SECTION_SIZE;
85         size = ALIGN(CONFIG_SYS_NONCACHED_MEMORY, MMU_SECTION_SIZE);
86         start = end - size;
87
88         debug("mapping memory %pa-%pa non-cached\n", &start, &end);
89
90         noncached_start = start;
91         noncached_end = end;
92         noncached_next = start;
93
94 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
95         mmu_set_region_dcache_behaviour(noncached_start, size, DCACHE_OFF);
96 #endif
97 }
98
99 phys_addr_t noncached_alloc(size_t size, size_t align)
100 {
101         phys_addr_t next = ALIGN(noncached_next, align);
102
103         if (next >= noncached_end || (noncached_end - next) < size)
104                 return 0;
105
106         debug("allocated %zu bytes of uncached memory @%pa\n", size, &next);
107         noncached_next = next + size;
108
109         return next;
110 }
111 #endif /* CONFIG_SYS_NONCACHED_MEMORY */
112
113 #if CONFIG_IS_ENABLED(SYS_THUMB_BUILD)
114 void invalidate_l2_cache(void)
115 {
116         unsigned int val = 0;
117
118         asm volatile("mcr p15, 1, %0, c15, c11, 0 @ invl l2 cache"
119                 : : "r" (val) : "cc");
120         isb();
121 }
122 #endif
123
124 __weak int reserve_mmu(void)
125 {
126 #if !(CONFIG_IS_ENABLED(SYS_ICACHE_OFF) && CONFIG_IS_ENABLED(SYS_DCACHE_OFF))
127         /* reserve TLB table */
128         gd->arch.tlb_size = PGTABLE_SIZE;
129         gd->relocaddr -= gd->arch.tlb_size;
130
131         /* round down to next 64 kB limit */
132         gd->relocaddr &= ~(0x10000 - 1);
133
134         gd->arch.tlb_addr = gd->relocaddr;
135         debug("TLB table from %08lx to %08lx\n", gd->arch.tlb_addr,
136               gd->arch.tlb_addr + gd->arch.tlb_size);
137
138 #ifdef CONFIG_SYS_MEM_RESERVE_SECURE
139         /*
140          * Record allocated tlb_addr in case gd->tlb_addr to be overwritten
141          * with location within secure ram.
142          */
143         gd->arch.tlb_allocated = gd->arch.tlb_addr;
144 #endif
145 #endif
146
147         return 0;
148 }