1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2010
4  * Texas Instruments, <www.ti.com>
5  * Aneesh V <aneesh@ti.com>
6  */
7 #include <cpu_func.h>
8 #include <asm/cache.h>
9 #include <linux/types.h>
10 #include <common.h>
11 #include <asm/armv7.h>
12 #include <asm/utils.h>
13 
14 #define ARMV7_DCACHE_INVAL_RANGE	1
15 #define ARMV7_DCACHE_CLEAN_INVAL_RANGE	2
16 
17 #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
18 
19 /* Asm functions from cache_v7_asm.S */
20 void v7_flush_dcache_all(void);
21 void v7_invalidate_dcache_all(void);
22 
get_ccsidr(void)23 static u32 get_ccsidr(void)
24 {
25 	u32 ccsidr;
26 
27 	/* Read current CP15 Cache Size ID Register */
28 	asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
29 	return ccsidr;
30 }
31 
v7_dcache_clean_inval_range(u32 start,u32 stop,u32 line_len)32 static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len)
33 {
34 	u32 mva;
35 
36 	/* Align start to cache line boundary */
37 	start &= ~(line_len - 1);
38 	for (mva = start; mva < stop; mva = mva + line_len) {
39 		/* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */
40 		asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
41 	}
42 }
43 
v7_dcache_inval_range(u32 start,u32 stop,u32 line_len)44 static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len)
45 {
46 	u32 mva;
47 
48 	if (!check_cache_range(start, stop))
49 		return;
50 
51 	for (mva = start; mva < stop; mva = mva + line_len) {
52 		/* DCIMVAC - Invalidate data cache by MVA to PoC */
53 		asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva));
54 	}
55 }
56 
v7_dcache_maint_range(u32 start,u32 stop,u32 range_op)57 static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op)
58 {
59 	u32 line_len, ccsidr;
60 
61 	ccsidr = get_ccsidr();
62 	line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
63 			CCSIDR_LINE_SIZE_OFFSET) + 2;
64 	/* Converting from words to bytes */
65 	line_len += 2;
66 	/* converting from log2(linelen) to linelen */
67 	line_len = 1 << line_len;
68 
69 	switch (range_op) {
70 	case ARMV7_DCACHE_CLEAN_INVAL_RANGE:
71 		v7_dcache_clean_inval_range(start, stop, line_len);
72 		break;
73 	case ARMV7_DCACHE_INVAL_RANGE:
74 		v7_dcache_inval_range(start, stop, line_len);
75 		break;
76 	}
77 
78 	/* DSB to make sure the operation is complete */
79 	dsb();
80 }
81 
82 /* Invalidate TLB */
v7_inval_tlb(void)83 static void v7_inval_tlb(void)
84 {
85 	/* Invalidate entire unified TLB */
86 	asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0));
87 	/* Invalidate entire data TLB */
88 	asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0));
89 	/* Invalidate entire instruction TLB */
90 	asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0));
91 	/* Full system DSB - make sure that the invalidation is complete */
92 	dsb();
93 	/* Full system ISB - make sure the instruction stream sees it */
94 	isb();
95 }
96 
invalidate_dcache_all(void)97 void invalidate_dcache_all(void)
98 {
99 	v7_invalidate_dcache_all();
100 
101 	v7_outer_cache_inval_all();
102 }
103 
104 /*
105  * Performs a clean & invalidation of the entire data cache
106  * at all levels
107  */
flush_dcache_all(void)108 void flush_dcache_all(void)
109 {
110 	v7_flush_dcache_all();
111 
112 	v7_outer_cache_flush_all();
113 }
114 
115 /*
116  * Invalidates range in all levels of D-cache/unified cache used:
117  * Affects the range [start, stop - 1]
118  */
invalidate_dcache_range(unsigned long start,unsigned long stop)119 void invalidate_dcache_range(unsigned long start, unsigned long stop)
120 {
121 	check_cache_range(start, stop);
122 
123 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE);
124 
125 	v7_outer_cache_inval_range(start, stop);
126 }
127 
128 /*
129  * Flush range(clean & invalidate) from all levels of D-cache/unified
130  * cache used:
131  * Affects the range [start, stop - 1]
132  */
flush_dcache_range(unsigned long start,unsigned long stop)133 void flush_dcache_range(unsigned long start, unsigned long stop)
134 {
135 	check_cache_range(start, stop);
136 
137 	v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE);
138 
139 	v7_outer_cache_flush_range(start, stop);
140 }
141 
arm_init_before_mmu(void)142 void arm_init_before_mmu(void)
143 {
144 	v7_outer_cache_enable();
145 	invalidate_dcache_all();
146 	v7_inval_tlb();
147 }
148 
mmu_page_table_flush(unsigned long start,unsigned long stop)149 void mmu_page_table_flush(unsigned long start, unsigned long stop)
150 {
151 	flush_dcache_range(start, stop);
152 	v7_inval_tlb();
153 }
154 #else /* #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
invalidate_dcache_all(void)155 void invalidate_dcache_all(void)
156 {
157 }
158 
flush_dcache_all(void)159 void flush_dcache_all(void)
160 {
161 }
162 
invalidate_dcache_range(unsigned long start,unsigned long stop)163 void invalidate_dcache_range(unsigned long start, unsigned long stop)
164 {
165 }
166 
flush_dcache_range(unsigned long start,unsigned long stop)167 void flush_dcache_range(unsigned long start, unsigned long stop)
168 {
169 }
170 
arm_init_before_mmu(void)171 void arm_init_before_mmu(void)
172 {
173 }
174 
mmu_page_table_flush(unsigned long start,unsigned long stop)175 void mmu_page_table_flush(unsigned long start, unsigned long stop)
176 {
177 }
178 
179 #endif /* #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) */
180 
181 #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
182 /* Invalidate entire I-cache and branch predictor array */
invalidate_icache_all(void)183 void invalidate_icache_all(void)
184 {
185 	/*
186 	 * Invalidate all instruction caches to PoU.
187 	 * Also flushes branch target cache.
188 	 */
189 	asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
190 
191 	/* Invalidate entire branch predictor array */
192 	asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
193 
194 	/* Full system DSB - make sure that the invalidation is complete */
195 	dsb();
196 
197 	/* ISB - make sure the instruction stream sees it */
198 	isb();
199 }
200 #else
invalidate_icache_all(void)201 void invalidate_icache_all(void)
202 {
203 }
204 #endif
205 
206 /*  Stub implementations for outer cache operations */
v7_outer_cache_enable(void)207 __weak void v7_outer_cache_enable(void) {}
v7_outer_cache_disable(void)208 __weak void v7_outer_cache_disable(void) {}
v7_outer_cache_flush_all(void)209 __weak void v7_outer_cache_flush_all(void) {}
v7_outer_cache_inval_all(void)210 __weak void v7_outer_cache_inval_all(void) {}
v7_outer_cache_flush_range(u32 start,u32 end)211 __weak void v7_outer_cache_flush_range(u32 start, u32 end) {}
v7_outer_cache_inval_range(u32 start,u32 end)212 __weak void v7_outer_cache_inval_range(u32 start, u32 end) {}
213