2 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
4 * SPDX-License-Identifier: GPL-2.0+
9 #include <linux/compiler.h>
10 #include <linux/kernel.h>
11 #include <asm/arcregs.h>
12 #include <asm/cache.h>
14 /* Bit values in IC_CTRL */
15 #define IC_CTRL_CACHE_DISABLE (1 << 0)
17 /* Bit values in DC_CTRL */
18 #define DC_CTRL_CACHE_DISABLE (1 << 0)
19 #define DC_CTRL_INV_MODE_FLUSH (1 << 6)
20 #define DC_CTRL_FLUSH_STATUS (1 << 8)
21 #define CACHE_VER_NUM_MASK 0xF
22 #define SLC_CTRL_SB (1 << 2)
29 * By default that variable will fall into .bss section.
30 * But .bss section is not relocated and so it will be initilized before
31 * relocation but will be used after being zeroed.
33 int l1_line_sz __section(".data");
34 int dcache_exists __section(".data");
35 int icache_exists __section(".data");
37 #define CACHE_LINE_MASK (~(l1_line_sz - 1))
39 #ifdef CONFIG_ISA_ARCV2
40 int slc_line_sz __section(".data");
41 int slc_exists __section(".data");
42 int ioc_exists __section(".data");
44 static unsigned int __before_slc_op(const int op)
46 unsigned int reg = reg;
50 * IM is set by default and implies Flush-n-inv
51 * Clear it here for vanilla inv
53 reg = read_aux_reg(ARC_AUX_SLC_CTRL);
54 write_aux_reg(ARC_AUX_SLC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH);
60 static void __after_slc_op(const int op, unsigned int reg)
62 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
63 while (read_aux_reg(ARC_AUX_SLC_CTRL) &
67 /* Switch back to default Invalidate mode */
69 write_aux_reg(ARC_AUX_SLC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH);
72 static inline void __slc_line_loop(unsigned long paddr, unsigned long sz,
78 #define SLC_LINE_MASK (~(slc_line_sz - 1))
80 aux_cmd = op & OP_INV ? ARC_AUX_SLC_IVDL : ARC_AUX_SLC_FLDL;
82 sz += paddr & ~SLC_LINE_MASK;
83 paddr &= SLC_LINE_MASK;
85 num_lines = DIV_ROUND_UP(sz, slc_line_sz);
87 while (num_lines-- > 0) {
88 write_aux_reg(aux_cmd, paddr);
93 static inline void __slc_entire_op(const int cacheop)
96 unsigned int ctrl_reg = __before_slc_op(cacheop);
98 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
99 aux = ARC_AUX_SLC_INVALIDATE;
101 aux = ARC_AUX_SLC_FLUSH;
103 write_aux_reg(aux, 0x1);
105 __after_slc_op(cacheop, ctrl_reg);
108 static inline void __slc_line_op(unsigned long paddr, unsigned long sz,
111 unsigned int ctrl_reg = __before_slc_op(cacheop);
112 __slc_line_loop(paddr, sz, cacheop);
113 __after_slc_op(cacheop, ctrl_reg);
116 #define __slc_entire_op(cacheop)
117 #define __slc_line_op(paddr, sz, cacheop)
120 #ifdef CONFIG_ISA_ARCV2
121 static void read_decode_cache_bcr_arcv2(void)
125 #ifdef CONFIG_CPU_BIG_ENDIAN
126 unsigned int pad:24, way:2, lsz:2, sz:4;
128 unsigned int sz:4, lsz:2, way:2, pad:24;
136 #ifdef CONFIG_CPU_BIG_ENDIAN
137 unsigned int pad:24, ver:8;
139 unsigned int ver:8, pad:24;
145 sbcr.word = read_aux_reg(ARC_BCR_SLC);
146 if (sbcr.fields.ver) {
147 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
149 slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
153 struct bcr_clust_cfg {
154 #ifdef CONFIG_CPU_BIG_ENDIAN
155 unsigned int pad:7, c:1, num_entries:8, num_cores:8, ver:8;
157 unsigned int ver:8, num_cores:8, num_entries:8, c:1, pad:7;
163 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
169 void read_decode_cache_bcr(void)
171 int dc_line_sz = 0, ic_line_sz = 0;
175 #ifdef CONFIG_CPU_BIG_ENDIAN
176 unsigned int pad:12, line_len:4, sz:4, config:4, ver:8;
178 unsigned int ver:8, config:4, sz:4, line_len:4, pad:12;
184 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
185 if (ibcr.fields.ver) {
187 l1_line_sz = ic_line_sz = 8 << ibcr.fields.line_len;
189 panic("Instruction exists but line length is 0\n");
192 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
193 if (dbcr.fields.ver){
195 l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
197 panic("Data cache exists but line length is 0\n");
200 if (ic_line_sz && dc_line_sz && (ic_line_sz != dc_line_sz))
201 panic("Instruction and data cache line lengths differ\n");
204 void cache_init(void)
206 read_decode_cache_bcr();
208 #ifdef CONFIG_ISA_ARCV2
209 read_decode_cache_bcr_arcv2();
212 /* IO coherency base - 0x8z */
213 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, 0x80000);
214 /* IO coherency aperture size - 512Mb: 0x8z-0xAz */
215 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE, 0x11);
216 /* Enable partial writes */
217 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
218 /* Enable IO coherency */
219 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
224 int icache_status(void)
229 if (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE)
235 void icache_enable(void)
238 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
239 ~IC_CTRL_CACHE_DISABLE);
242 void icache_disable(void)
245 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
246 IC_CTRL_CACHE_DISABLE);
249 #ifndef CONFIG_SYS_DCACHE_OFF
250 void invalidate_icache_all(void)
252 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
253 if (icache_status()) {
254 write_aux_reg(ARC_AUX_IC_IVIC, 1);
255 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
259 void invalidate_icache_all(void)
264 int dcache_status(void)
269 if (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE)
275 void dcache_enable(void)
280 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
281 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
284 void dcache_disable(void)
289 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
290 DC_CTRL_CACHE_DISABLE);
293 #ifndef CONFIG_SYS_DCACHE_OFF
295 * Common Helper for Line Operations on {I,D}-Cache
297 static inline void __cache_line_loop(unsigned long paddr, unsigned long sz,
300 unsigned int aux_cmd;
301 #if (CONFIG_ARC_MMU_VER == 3)
302 unsigned int aux_tag;
306 if (cacheop == OP_INV_IC) {
307 aux_cmd = ARC_AUX_IC_IVIL;
308 #if (CONFIG_ARC_MMU_VER == 3)
309 aux_tag = ARC_AUX_IC_PTAG;
312 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
313 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
314 #if (CONFIG_ARC_MMU_VER == 3)
315 aux_tag = ARC_AUX_DC_PTAG;
319 sz += paddr & ~CACHE_LINE_MASK;
320 paddr &= CACHE_LINE_MASK;
322 num_lines = DIV_ROUND_UP(sz, l1_line_sz);
324 while (num_lines-- > 0) {
325 #if (CONFIG_ARC_MMU_VER == 3)
326 write_aux_reg(aux_tag, paddr);
328 write_aux_reg(aux_cmd, paddr);
333 static unsigned int __before_dc_op(const int op)
339 * IM is set by default and implies Flush-n-inv
340 * Clear it here for vanilla inv
342 reg = read_aux_reg(ARC_AUX_DC_CTRL);
343 write_aux_reg(ARC_AUX_DC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH);
349 static void __after_dc_op(const int op, unsigned int reg)
351 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
352 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS)
355 /* Switch back to default Invalidate mode */
357 write_aux_reg(ARC_AUX_DC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH);
360 static inline void __dc_entire_op(const int cacheop)
363 unsigned int ctrl_reg = __before_dc_op(cacheop);
365 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
366 aux = ARC_AUX_DC_IVDC;
368 aux = ARC_AUX_DC_FLSH;
370 write_aux_reg(aux, 0x1);
372 __after_dc_op(cacheop, ctrl_reg);
375 static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
378 unsigned int ctrl_reg = __before_dc_op(cacheop);
379 __cache_line_loop(paddr, sz, cacheop);
380 __after_dc_op(cacheop, ctrl_reg);
383 #define __dc_entire_op(cacheop)
384 #define __dc_line_op(paddr, sz, cacheop)
385 #endif /* !CONFIG_SYS_DCACHE_OFF */
387 void invalidate_dcache_range(unsigned long start, unsigned long end)
389 #ifdef CONFIG_ISA_ARCV2
392 __dc_line_op(start, end - start, OP_INV);
394 #ifdef CONFIG_ISA_ARCV2
395 if (slc_exists && !ioc_exists)
396 __slc_line_op(start, end - start, OP_INV);
400 void flush_dcache_range(unsigned long start, unsigned long end)
402 #ifdef CONFIG_ISA_ARCV2
405 __dc_line_op(start, end - start, OP_FLUSH);
407 #ifdef CONFIG_ISA_ARCV2
408 if (slc_exists && !ioc_exists)
409 __slc_line_op(start, end - start, OP_FLUSH);
413 void flush_cache(unsigned long start, unsigned long size)
415 flush_dcache_range(start, start + size);
418 void invalidate_dcache_all(void)
420 #ifdef CONFIG_ISA_ARCV2
423 __dc_entire_op(OP_INV);
425 #ifdef CONFIG_ISA_ARCV2
426 if (slc_exists && !ioc_exists)
427 __slc_entire_op(OP_INV);
431 void flush_dcache_all(void)
433 __dc_entire_op(OP_FLUSH);
435 #ifdef CONFIG_ISA_ARCV2
437 __slc_entire_op(OP_FLUSH);