2 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/compiler.h>
9 #include <linux/kernel.h>
10 #include <asm/arcregs.h>
11 #include <asm/cache.h>
13 #define CACHE_LINE_MASK (~(CONFIG_SYS_CACHELINE_SIZE - 1))
15 /* Bit values in IC_CTRL */
16 #define IC_CTRL_CACHE_DISABLE (1 << 0)
18 /* Bit values in DC_CTRL */
19 #define DC_CTRL_CACHE_DISABLE (1 << 0)
20 #define DC_CTRL_INV_MODE_FLUSH (1 << 6)
21 #define DC_CTRL_FLUSH_STATUS (1 << 8)
22 #define CACHE_VER_NUM_MASK 0xF
23 #define SLC_CTRL_SB (1 << 2)
29 #ifdef CONFIG_ISA_ARCV2
31 * By default that variable will fall into .bss section.
32 * But .bss section is not relocated and so it will be initilized before
33 * relocation but will be used after being zeroed.
35 int slc_line_sz __section(".data");
36 int slc_exists __section(".data");
38 static unsigned int __before_slc_op(const int op)
40 unsigned int reg = reg;
44 * IM is set by default and implies Flush-n-inv
45 * Clear it here for vanilla inv
47 reg = read_aux_reg(ARC_AUX_SLC_CTRL);
48 write_aux_reg(ARC_AUX_SLC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH);
54 static void __after_slc_op(const int op, unsigned int reg)
56 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
57 while (read_aux_reg(ARC_AUX_SLC_CTRL) &
61 /* Switch back to default Invalidate mode */
63 write_aux_reg(ARC_AUX_SLC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH);
66 static inline void __slc_line_loop(unsigned long paddr, unsigned long sz,
72 #define SLC_LINE_MASK (~(slc_line_sz - 1))
74 aux_cmd = op & OP_INV ? ARC_AUX_SLC_IVDL : ARC_AUX_SLC_FLDL;
76 sz += paddr & ~SLC_LINE_MASK;
77 paddr &= SLC_LINE_MASK;
79 num_lines = DIV_ROUND_UP(sz, slc_line_sz);
81 while (num_lines-- > 0) {
82 write_aux_reg(aux_cmd, paddr);
87 static inline void __slc_entire_op(const int cacheop)
90 unsigned int ctrl_reg = __before_slc_op(cacheop);
92 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
93 aux = ARC_AUX_SLC_INVALIDATE;
95 aux = ARC_AUX_SLC_FLUSH;
97 write_aux_reg(aux, 0x1);
99 __after_slc_op(cacheop, ctrl_reg);
102 static inline void __slc_line_op(unsigned long paddr, unsigned long sz,
105 unsigned int ctrl_reg = __before_slc_op(cacheop);
106 __slc_line_loop(paddr, sz, cacheop);
107 __after_slc_op(cacheop, ctrl_reg);
110 #define __slc_entire_op(cacheop)
111 #define __slc_line_op(paddr, sz, cacheop)
114 static inline int icache_exists(void)
116 /* Check if Instruction Cache is available */
117 if (read_aux_reg(ARC_BCR_IC_BUILD) & CACHE_VER_NUM_MASK)
123 static inline int dcache_exists(void)
125 /* Check if Data Cache is available */
126 if (read_aux_reg(ARC_BCR_DC_BUILD) & CACHE_VER_NUM_MASK)
132 void cache_init(void)
134 #ifdef CONFIG_ISA_ARCV2
135 /* Check if System-Level Cache (SLC) is available */
136 if (read_aux_reg(ARC_BCR_SLC) & CACHE_VER_NUM_MASK) {
137 #define LSIZE_OFFSET 4
139 if (read_aux_reg(ARC_AUX_SLC_CONFIG) &
140 (LSIZE_MASK << LSIZE_OFFSET))
151 int icache_status(void)
153 if (!icache_exists())
156 if (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE)
162 void icache_enable(void)
165 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
166 ~IC_CTRL_CACHE_DISABLE);
169 void icache_disable(void)
172 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
173 IC_CTRL_CACHE_DISABLE);
176 #ifndef CONFIG_SYS_DCACHE_OFF
177 void invalidate_icache_all(void)
179 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
180 if (icache_status()) {
181 write_aux_reg(ARC_AUX_IC_IVIC, 1);
182 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
186 void invalidate_icache_all(void)
191 int dcache_status(void)
193 if (!dcache_exists())
196 if (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE)
202 void dcache_enable(void)
204 if (!dcache_exists())
207 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
208 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
211 void dcache_disable(void)
213 if (!dcache_exists())
216 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
217 DC_CTRL_CACHE_DISABLE);
220 #ifndef CONFIG_SYS_DCACHE_OFF
222 * Common Helper for Line Operations on {I,D}-Cache
224 static inline void __cache_line_loop(unsigned long paddr, unsigned long sz,
227 unsigned int aux_cmd;
228 #if (CONFIG_ARC_MMU_VER == 3)
229 unsigned int aux_tag;
233 if (cacheop == OP_INV_IC) {
234 aux_cmd = ARC_AUX_IC_IVIL;
235 #if (CONFIG_ARC_MMU_VER == 3)
236 aux_tag = ARC_AUX_IC_PTAG;
239 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
240 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
241 #if (CONFIG_ARC_MMU_VER == 3)
242 aux_tag = ARC_AUX_DC_PTAG;
246 sz += paddr & ~CACHE_LINE_MASK;
247 paddr &= CACHE_LINE_MASK;
249 num_lines = DIV_ROUND_UP(sz, CONFIG_SYS_CACHELINE_SIZE);
251 while (num_lines-- > 0) {
252 #if (CONFIG_ARC_MMU_VER == 3)
253 write_aux_reg(aux_tag, paddr);
255 write_aux_reg(aux_cmd, paddr);
256 paddr += CONFIG_SYS_CACHELINE_SIZE;
260 static unsigned int __before_dc_op(const int op)
266 * IM is set by default and implies Flush-n-inv
267 * Clear it here for vanilla inv
269 reg = read_aux_reg(ARC_AUX_DC_CTRL);
270 write_aux_reg(ARC_AUX_DC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH);
276 static void __after_dc_op(const int op, unsigned int reg)
278 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
279 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
282 /* Switch back to default Invalidate mode */
284 write_aux_reg(ARC_AUX_DC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH);
287 static inline void __dc_entire_op(const int cacheop)
290 unsigned int ctrl_reg = __before_dc_op(cacheop);
292 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
293 aux = ARC_AUX_DC_IVDC;
295 aux = ARC_AUX_DC_FLSH;
297 write_aux_reg(aux, 0x1);
299 __after_dc_op(cacheop, ctrl_reg);
302 static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
305 unsigned int ctrl_reg = __before_dc_op(cacheop);
306 __cache_line_loop(paddr, sz, cacheop);
307 __after_dc_op(cacheop, ctrl_reg);
310 #define __dc_entire_op(cacheop)
311 #define __dc_line_op(paddr, sz, cacheop)
312 #endif /* !CONFIG_SYS_DCACHE_OFF */
314 void invalidate_dcache_range(unsigned long start, unsigned long end)
316 __dc_line_op(start, end - start, OP_INV);
317 #ifdef CONFIG_ISA_ARCV2
319 __slc_line_op(start, end - start, OP_INV);
323 void flush_dcache_range(unsigned long start, unsigned long end)
325 __dc_line_op(start, end - start, OP_FLUSH);
326 #ifdef CONFIG_ISA_ARCV2
328 __slc_line_op(start, end - start, OP_FLUSH);
332 void flush_cache(unsigned long start, unsigned long size)
334 flush_dcache_range(start, start + size);
337 void invalidate_dcache_all(void)
339 __dc_entire_op(OP_INV);
340 #ifdef CONFIG_ISA_ARCV2
342 __slc_entire_op(OP_INV);
346 void flush_dcache_all(void)
348 __dc_entire_op(OP_FLUSH);
349 #ifdef CONFIG_ISA_ARCV2
351 __slc_entire_op(OP_FLUSH);