blob: dc309dac909a1aefeb8490da03db23cab0dff8d9 [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
Hans de Goedeba3bf9b2016-04-09 13:53:49 +020013#define ARMV7_DCACHE_INVAL_RANGE 1
14#define ARMV7_DCACHE_CLEAN_INVAL_RANGE 2
Aneesh V960f5c02011-06-16 23:30:47 +000015
16#ifndef CONFIG_SYS_DCACHE_OFF
Hans de Goede076e8412016-04-09 13:53:48 +020017
18/* Asm functions from cache_v7_asm.S */
19void v7_flush_dcache_all(void);
Hans de Goedeba3bf9b2016-04-09 13:53:49 +020020void v7_invalidate_dcache_all(void);
Hans de Goede076e8412016-04-09 13:53:48 +020021
Marek Vasutc28ad232015-07-27 22:34:17 +020022static int check_cache_range(unsigned long start, unsigned long stop)
23{
24 int ok = 1;
25
26 if (start & (CONFIG_SYS_CACHELINE_SIZE - 1))
27 ok = 0;
28
29 if (stop & (CONFIG_SYS_CACHELINE_SIZE - 1))
30 ok = 0;
31
32 if (!ok)
33 debug("CACHE: Misaligned operation at range [%08lx, %08lx]\n",
34 start, stop);
35
36 return ok;
37}
38
Aneesh V960f5c02011-06-16 23:30:47 +000039static u32 get_ccsidr(void)
40{
41 u32 ccsidr;
42
43 /* Read current CP15 Cache Size ID Register */
44 asm volatile ("mrc p15, 1, %0, c0, c0, 0" : "=r" (ccsidr));
45 return ccsidr;
46}
47
Thierry Reding0c597382014-08-26 17:34:20 +020048static void v7_dcache_clean_inval_range(u32 start, u32 stop, u32 line_len)
Aneesh V960f5c02011-06-16 23:30:47 +000049{
50 u32 mva;
51
52 /* Align start to cache line boundary */
53 start &= ~(line_len - 1);
54 for (mva = start; mva < stop; mva = mva + line_len) {
55 /* DCCIMVAC - Clean & Invalidate data cache by MVA to PoC */
56 asm volatile ("mcr p15, 0, %0, c7, c14, 1" : : "r" (mva));
57 }
58}
59
60static void v7_dcache_inval_range(u32 start, u32 stop, u32 line_len)
61{
62 u32 mva;
63
64 /*
Aneesh V6a22a622011-08-11 04:35:45 +000065 * If start address is not aligned to cache-line do not
66 * invalidate the first cache-line
Aneesh V960f5c02011-06-16 23:30:47 +000067 */
68 if (start & (line_len - 1)) {
Aneesh V6a22a622011-08-11 04:35:45 +000069 printf("ERROR: %s - start address is not aligned - 0x%08x\n",
70 __func__, start);
Aneesh V960f5c02011-06-16 23:30:47 +000071 /* move to next cache line */
72 start = (start + line_len - 1) & ~(line_len - 1);
73 }
74
75 /*
Aneesh V6a22a622011-08-11 04:35:45 +000076 * If stop address is not aligned to cache-line do not
77 * invalidate the last cache-line
Aneesh V960f5c02011-06-16 23:30:47 +000078 */
79 if (stop & (line_len - 1)) {
Aneesh V6a22a622011-08-11 04:35:45 +000080 printf("ERROR: %s - stop address is not aligned - 0x%08x\n",
81 __func__, stop);
Aneesh V960f5c02011-06-16 23:30:47 +000082 /* align to the beginning of this cache line */
83 stop &= ~(line_len - 1);
84 }
85
86 for (mva = start; mva < stop; mva = mva + line_len) {
87 /* DCIMVAC - Invalidate data cache by MVA to PoC */
88 asm volatile ("mcr p15, 0, %0, c7, c6, 1" : : "r" (mva));
89 }
90}
91
92static void v7_dcache_maint_range(u32 start, u32 stop, u32 range_op)
93{
94 u32 line_len, ccsidr;
95
96 ccsidr = get_ccsidr();
97 line_len = ((ccsidr & CCSIDR_LINE_SIZE_MASK) >>
98 CCSIDR_LINE_SIZE_OFFSET) + 2;
99 /* Converting from words to bytes */
100 line_len += 2;
101 /* converting from log2(linelen) to linelen */
102 line_len = 1 << line_len;
103
104 switch (range_op) {
105 case ARMV7_DCACHE_CLEAN_INVAL_RANGE:
106 v7_dcache_clean_inval_range(start, stop, line_len);
107 break;
108 case ARMV7_DCACHE_INVAL_RANGE:
109 v7_dcache_inval_range(start, stop, line_len);
110 break;
111 }
112
Aneesh V517912b2011-08-11 04:35:44 +0000113 /* DSB to make sure the operation is complete */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300114 DSB;
Aneesh V960f5c02011-06-16 23:30:47 +0000115}
116
117/* Invalidate TLB */
118static void v7_inval_tlb(void)
119{
120 /* Invalidate entire unified TLB */
121 asm volatile ("mcr p15, 0, %0, c8, c7, 0" : : "r" (0));
122 /* Invalidate entire data TLB */
123 asm volatile ("mcr p15, 0, %0, c8, c6, 0" : : "r" (0));
124 /* Invalidate entire instruction TLB */
125 asm volatile ("mcr p15, 0, %0, c8, c5, 0" : : "r" (0));
126 /* Full system DSB - make sure that the invalidation is complete */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300127 DSB;
Aneesh V960f5c02011-06-16 23:30:47 +0000128 /* Full system ISB - make sure the instruction stream sees it */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300129 ISB;
Aneesh V960f5c02011-06-16 23:30:47 +0000130}
131
132void invalidate_dcache_all(void)
133{
Hans de Goedeba3bf9b2016-04-09 13:53:49 +0200134 v7_invalidate_dcache_all();
Aneesh V960f5c02011-06-16 23:30:47 +0000135
136 v7_outer_cache_inval_all();
137}
138
139/*
140 * Performs a clean & invalidation of the entire data cache
141 * at all levels
142 */
143void flush_dcache_all(void)
144{
Hans de Goede076e8412016-04-09 13:53:48 +0200145 v7_flush_dcache_all();
Aneesh V960f5c02011-06-16 23:30:47 +0000146
147 v7_outer_cache_flush_all();
148}
149
150/*
151 * Invalidates range in all levels of D-cache/unified cache used:
152 * Affects the range [start, stop - 1]
153 */
154void invalidate_dcache_range(unsigned long start, unsigned long stop)
155{
Marek Vasutc28ad232015-07-27 22:34:17 +0200156 check_cache_range(start, stop);
157
Aneesh V960f5c02011-06-16 23:30:47 +0000158 v7_dcache_maint_range(start, stop, ARMV7_DCACHE_INVAL_RANGE);
159
160 v7_outer_cache_inval_range(start, stop);
161}
162
163/*
164 * Flush range(clean & invalidate) from all levels of D-cache/unified
165 * cache used:
166 * Affects the range [start, stop - 1]
167 */
168void flush_dcache_range(unsigned long start, unsigned long stop)
169{
Marek Vasutc28ad232015-07-27 22:34:17 +0200170 check_cache_range(start, stop);
171
Aneesh V960f5c02011-06-16 23:30:47 +0000172 v7_dcache_maint_range(start, stop, ARMV7_DCACHE_CLEAN_INVAL_RANGE);
173
174 v7_outer_cache_flush_range(start, stop);
175}
176
177void arm_init_before_mmu(void)
178{
179 v7_outer_cache_enable();
180 invalidate_dcache_all();
181 v7_inval_tlb();
182}
183
Simon Glassa4f20792012-10-17 13:24:53 +0000184void mmu_page_table_flush(unsigned long start, unsigned long stop)
185{
186 flush_dcache_range(start, stop);
187 v7_inval_tlb();
188}
Aneesh V960f5c02011-06-16 23:30:47 +0000189#else /* #ifndef CONFIG_SYS_DCACHE_OFF */
190void invalidate_dcache_all(void)
191{
192}
193
194void flush_dcache_all(void)
195{
196}
197
Aneesh V960f5c02011-06-16 23:30:47 +0000198void arm_init_before_mmu(void)
199{
200}
201
Simon Glassa4f20792012-10-17 13:24:53 +0000202void mmu_page_table_flush(unsigned long start, unsigned long stop)
203{
204}
205
R Sricharan06396c12013-03-04 20:04:45 +0000206void arm_init_domains(void)
207{
208}
Aneesh V960f5c02011-06-16 23:30:47 +0000209#endif /* #ifndef CONFIG_SYS_DCACHE_OFF */
210
211#ifndef CONFIG_SYS_ICACHE_OFF
212/* Invalidate entire I-cache and branch predictor array */
213void invalidate_icache_all(void)
214{
215 /*
216 * Invalidate all instruction caches to PoU.
217 * Also flushes branch target cache.
218 */
219 asm volatile ("mcr p15, 0, %0, c7, c5, 0" : : "r" (0));
220
221 /* Invalidate entire branch predictor array */
222 asm volatile ("mcr p15, 0, %0, c7, c5, 6" : : "r" (0));
223
224 /* Full system DSB - make sure that the invalidation is complete */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300225 DSB;
Aneesh V960f5c02011-06-16 23:30:47 +0000226
227 /* ISB - make sure the instruction stream sees it */
Valentine Barshak689bfa22015-03-20 18:16:17 +0300228 ISB;
Aneesh V960f5c02011-06-16 23:30:47 +0000229}
230#else
231void invalidate_icache_all(void)
232{
233}
234#endif
235
Jeroen Hofsteed7460772014-06-23 22:07:04 +0200236/* Stub implementations for outer cache operations */
237__weak void v7_outer_cache_enable(void) {}
238__weak void v7_outer_cache_disable(void) {}
239__weak void v7_outer_cache_flush_all(void) {}
240__weak void v7_outer_cache_inval_all(void) {}
241__weak void v7_outer_cache_flush_range(u32 start, u32 end) {}
242__weak void v7_outer_cache_inval_range(u32 start, u32 end) {}