blob: 94ff48859eaeeca3455f9baeaf9386b4e4196a1a [file] [log] [blame]
Aneesh V960f5c02011-06-16 23:30:47 +00001/*
2 * (C) Copyright 2010
3 * Texas Instruments, <www.ti.com>
4 * Aneesh V <aneesh@ti.com>
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Aneesh V960f5c02011-06-16 23:30:47 +00007 */
8#include <linux/types.h>
9#include <common.h>
10#include <asm/armv7.h>
11#include <asm/utils.h>
12
13#define ARMV7_DCACHE_INVAL_ALL 1
14#define ARMV7_DCACHE_CLEAN_INVAL_ALL 2
15#define ARMV7_DCACHE_INVAL_RANGE 3
16#define ARMV7_DCACHE_CLEAN_INVAL_RANGE 4
17
18#ifndef CONFIG_SYS_DCACHE_OFF
Marek Vasutc28ad232015-07-27 22:34:17 +020019static int check_cache_range(unsigned long start, unsigned long stop)
20{
21 int ok = 1;
22
23 if (start & (CONFIG_SYS_CACHELINE_SIZE - 1))
24 ok = 0;
25
26 if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1))
27 ok = 0;
28
29 if (!ok)
30 debug("CACHE: Misaligned operation at range [%08lx, %08lx]\n",
31 start, stop);
32
33 return ok;
34}
35
Aneesh V960f5c02011-06-16 23:30:47 +000036/*
37 * Write the level and type you want to Cache Size Selection Register(CSSELR)
38 * to get size details from Current Cache Size ID Register(CCSIDR)
39 */
40static void set_csselr(u32 level, u32 type)
Thierry Reding0c597382014-08-26 17:34:20 +020041{
42 u32 csselr = level << 1 | type;
Aneesh V960f5c02011-06-16 23:30:47 +000043
44 /* Write to Cache Size Selection Register(CSSELR) */
45 asm volatile ("mcr p15, 2, %0, c0, c0, 0" : : "r" (csselr));
46}
47
48static u32 get_ccsidr(void)
49{
50 u32 ccsidr;
51
52 /* Read current CP15 Cache Size ID Register */
53 asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
54 return ccsidr;
55}
56
57static u32 get_clidr(void)
58{
59 u32 clidr;
60
61 /* Read current CP15 Cache Level ID Register */
62 asm volatile ("mrc p15,1,%0,c0,c0,1" : "=r" (clidr));
63 return clidr;
64}
65
66static void v7_inval_dcache_level_setway(u32 level, u32 num_sets,
67 u32 num_ways, u32 way_shift,
68 u32 log2_line_len)
69{
Thierry Reding0c597382014-08-26 17:34:20 +020070 int way, set;
71 u32 setway;
Aneesh V960f5c02011-06-16 23:30:47 +000072
73 /*
74 * For optimal assembly code:
75 * a. count down
76 * b. have bigger loop inside
77 */
78 for (way = num_ways - 1; way >= 0 ; way--) {
79 for (set = num_sets - 1; set >= 0; set--) {
80 setway = (level << 1) | (set << log2_line_len) |
81 (way << way_shift);
82 /* Invalidate data/unified cache line by set/way */
83 asm volatile (" mcr p15, 0, %0, c7, c6, 2"
84 : : "r" (setway));
85 }
86 }
Aneesh V517912b2011-08-11 04:35:44 +000087 /* DSB to make sure the operation is complete */
Valentine Barshak689bfa22015-03-20 18:16:17 +030088 DSB;
Aneesh V960f5c02011-06-16 23:30:47 +000089}
90
91static void v7_clean_inval_dcache_level_setway(u32 level, u32 num_sets,
92 u32 num_ways, u32 way_shift,
93 u32 log2_line_len)
94{
Thierry Reding0c597382014-08-26 17:34:20 +020095 int way, set;
96 u32 setway;
Aneesh V960f5c02011-06-16 23:30:47 +000097
98 /*
99 * For optimal assembly code:
100 * a. count down
101 * b. have bigger loop inside
102 */
103 for (way = num_ways - 1; way >= 0 ; way--) {
104 for (set = num_sets - 1; set >= 0; set--) {
105 setway = (level << 1) | (set << log2_line_len) |
106 (way << way_shift);
107 /*
108 * Clean & Invalidate data/unified
109 * cache line by set/way
110 */
111 asm volatile (" mcr p15, 0, %0, c7, c14, 2"
112 : : "r" (setway));
113 }
114 }
Aneesh V517912b2011-08-11 04:35:44 +0000115 /* DSB to make sure the operation is complete */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300116 DSB;
Aneesh V960f5c02011-06-16 23:30:47 +0000117}
118
119static void v7_maint_dcache_level_setway(u32 level, u32 operation)
120{
121 u32 ccsidr;
122 u32 num_sets, num_ways, log2_line_len, log2_num_ways;
123 u32 way_shift;
124
125 set_csselr(level, ARMV7_CSSELR_IND_DATA_UNIFIED);
126
127 ccsidr = get_ccsidr();
128
129 log2_line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
130 CCSIDR_LINE_SIZE_OFFSET) + 2;
131 /* Converting from words to bytes */
132 log2_line_len += 2;
133
134 num_ways = ((ccsidr & CCSIDR_ASSOCIATIVITY_MASK) >>
135 CCSIDR_ASSOCIATIVITY_OFFSET) + 1;
136 num_sets = ((ccsidr & CCSIDR_NUM_SETS_MASK) >>
137 CCSIDR_NUM_SETS_OFFSET) + 1;
138 /*
139 * According to ARMv7 ARM number of sets and number of ways need
140 * not be a power of 2
141 */
142 log2_num_ways = log_2_n_round_up(num_ways);
143
144 way_shift = (32 - log2_num_ways);
145 if (operation == ARMV7_DCACHE_INVAL_ALL) {
146 v7_inval_dcache_level_setway(level, num_sets, num_ways,
147 way_shift, log2_line_len);
148 } else if (operation == ARMV7_DCACHE_CLEAN_INVAL_ALL) {
149 v7_clean_inval_dcache_level_setway(level, num_sets, num_ways,
150 way_shift, log2_line_len);
151 }
152}
153
154static void v7_maint_dcache_all(u32 operation)
155{
156 u32 level, cache_type, level_start_bit = 0;
Aneesh V960f5c02011-06-16 23:30:47 +0000157 u32 clidr = get_clidr();
158
159 for (level = 0; level < 7; level++) {
160 cache_type = (clidr >> level_start_bit) & 0x7;
161 if ((cache_type == ARMV7_CLIDR_CTYPE_DATA_ONLY) ||
162 (cache_type == ARMV7_CLIDR_CTYPE_INSTRUCTION_DATA) ||
163 (cache_type == ARMV7_CLIDR_CTYPE_UNIFIED))
164 v7_maint_dcache_level_setway(level, operation);
165 level_start_bit += 3;
166 }
167}
168
Thierry Reding0c597382014-08-26 17:34:20 +0200169static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len)
Aneesh V960f5c02011-06-16 23:30:47 +0000170{
171 u32 mva;
172
173 /* Align start to cache line boundary */
174 start &= ~(line_len - 1);
175 for (mva = start; mva < stop; mva = mva + line_len) {
176 /* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */
177 asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
178 }
179}
180
181static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len)
182{
183 u32 mva;
184
185 /*
Aneesh V6a22a622011-08-11 04:35:45 +0000186 * If start address is not aligned to cache-line do not
187 * invalidate the first cache-line
Aneesh V960f5c02011-06-16 23:30:47 +0000188 */
189 if (start & (line_len - 1)) {
Aneesh V6a22a622011-08-11 04:35:45 +0000190 printf("ERROR: %s - start address is not aligned - 0x%08x\n",
191 __func__, start);
Aneesh V960f5c02011-06-16 23:30:47 +0000192 /* move to next cache line */
193 start = (start + line_len - 1) & ~(line_len - 1);
194 }
195
196 /*
Aneesh V6a22a622011-08-11 04:35:45 +0000197 * If stop address is not aligned to cache-line do not
198 * invalidate the last cache-line
Aneesh V960f5c02011-06-16 23:30:47 +0000199 */
200 if (stop & (line_len - 1)) {
Aneesh V6a22a622011-08-11 04:35:45 +0000201 printf("ERROR: %s - stop address is not aligned - 0x%08x\n",
202 __func__, stop);
Aneesh V960f5c02011-06-16 23:30:47 +0000203 /* align to the beginning of this cache line */
204 stop &= ~(line_len - 1);
205 }
206
207 for (mva = start; mva < stop; mva = mva + line_len) {
208 /* DCIMVAC - Invalidate data cache by MVA to PoC */
209 asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva));
210 }
211}
212
213static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op)
214{
215 u32 line_len, ccsidr;
216
217 ccsidr = get_ccsidr();
218 line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
219 CCSIDR_LINE_SIZE_OFFSET) + 2;
220 /* Converting from words to bytes */
221 line_len += 2;
222 /* converting from log2(linelen) to linelen */
223 line_len = 1 << line_len;
224
225 switch (range_op) {
226 case ARMV7_DCACHE_CLEAN_INVAL_RANGE:
227 v7_dcache_clean_inval_range(start, stop, line_len);
228 break;
229 case ARMV7_DCACHE_INVAL_RANGE:
230 v7_dcache_inval_range(start, stop, line_len);
231 break;
232 }
233
Aneesh V517912b2011-08-11 04:35:44 +0000234 /* DSB to make sure the operation is complete */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300235 DSB;
Aneesh V960f5c02011-06-16 23:30:47 +0000236}
237
238/* Invalidate TLB */
239static void v7_inval_tlb(void)
240{
241 /* Invalidate entire unified TLB */
242 asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0));
243 /* Invalidate entire data TLB */
244 asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0));
245 /* Invalidate entire instruction TLB */
246 asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0));
247 /* Full system DSB - make sure that the invalidation is complete */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300248 DSB;
Aneesh V960f5c02011-06-16 23:30:47 +0000249 /* Full system ISB - make sure the instruction stream sees it */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300250 ISB;
Aneesh V960f5c02011-06-16 23:30:47 +0000251}
252
253void invalidate_dcache_all(void)
254{
255 v7_maint_dcache_all(ARMV7_DCACHE_INVAL_ALL);
256
257 v7_outer_cache_inval_all();
258}
259
260/*
261 * Performs a clean & invalidation of the entire data cache
262 * at all levels
263 */
264void flush_dcache_all(void)
265{
266 v7_maint_dcache_all(ARMV7_DCACHE_CLEAN_INVAL_ALL);
267
268 v7_outer_cache_flush_all();
269}
270
271/*
272 * Invalidates range in all levels of D-cache/unified cache used:
273 * Affects the range [start, stop - 1]
274 */
275void invalidate_dcache_range(unsigned long start, unsigned long stop)
276{
Marek Vasutc28ad232015-07-27 22:34:17 +0200277 check_cache_range(start, stop);
278
Aneesh V960f5c02011-06-16 23:30:47 +0000279 v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE);
280
281 v7_outer_cache_inval_range(start, stop);
282}
283
284/*
285 * Flush range(clean & invalidate) from all levels of D-cache/unified
286 * cache used:
287 * Affects the range [start, stop - 1]
288 */
289void flush_dcache_range(unsigned long start, unsigned long stop)
290{
Marek Vasutc28ad232015-07-27 22:34:17 +0200291 check_cache_range(start, stop);
292
Aneesh V960f5c02011-06-16 23:30:47 +0000293 v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE);
294
295 v7_outer_cache_flush_range(start, stop);
296}
297
298void arm_init_before_mmu(void)
299{
300 v7_outer_cache_enable();
301 invalidate_dcache_all();
302 v7_inval_tlb();
303}
304
Simon Glassa4f20792012-10-17 13:24:53 +0000305void mmu_page_table_flush(unsigned long start, unsigned long stop)
306{
307 flush_dcache_range(start, stop);
308 v7_inval_tlb();
309}
Aneesh V960f5c02011-06-16 23:30:47 +0000310#else /* #ifndef CONFIG_SYS_DCACHE_OFF */
311void invalidate_dcache_all(void)
312{
313}
314
315void flush_dcache_all(void)
316{
317}
318
Aneesh V960f5c02011-06-16 23:30:47 +0000319void arm_init_before_mmu(void)
320{
321}
322
Simon Glassa4f20792012-10-17 13:24:53 +0000323void mmu_page_table_flush(unsigned long start, unsigned long stop)
324{
325}
326
R Sricharan06396c12013-03-04 20:04:45 +0000327void arm_init_domains(void)
328{
329}
Aneesh V960f5c02011-06-16 23:30:47 +0000330#endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
331
332#ifndef CONFIG_SYS_ICACHE_OFF
333/* Invalidate entire I-cache and branch predictor array */
334void invalidate_icache_all(void)
335{
336 /*
337 * Invalidate all instruction caches to PoU.
338 * Also flushes branch target cache.
339 */
340 asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
341
342 /* Invalidate entire branch predictor array */
343 asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
344
345 /* Full system DSB - make sure that the invalidation is complete */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300346 DSB;
Aneesh V960f5c02011-06-16 23:30:47 +0000347
348 /* ISB - make sure the instruction stream sees it */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300349 ISB;
Aneesh V960f5c02011-06-16 23:30:47 +0000350}
351#else
352void invalidate_icache_all(void)
353{
354}
355#endif
356
Jeroen Hofsteed7460772014-06-23 22:07:04 +0200357/* Stub implementations for outer cache operations */
358__weak void v7_outer_cache_enable(void) {}
359__weak void v7_outer_cache_disable(void) {}
360__weak void v7_outer_cache_flush_all(void) {}
361__weak void v7_outer_cache_inval_all(void) {}
362__weak void v7_outer_cache_flush_range(u32 start, u32 end) {}
363__weak void v7_outer_cache_inval_range(u32 start, u32 end) {}