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 <linux/log2.h>
12 #include <asm/arcregs.h>
13 #include <asm/arc-bcr.h>
14 #include <asm/cache.h>
18 * Data cache (L1 D$ or SL$) entire invalidate operation or data cache disable
19 * operation may result in unexpected behavior and data loss even if we flush
20 * data cache right before invalidation. That may happens if we store any context
21 * on stack (like we store BLINK register on stack before function call).
22 * BLINK register is the register where return address is automatically saved
23 * when we do function call with instructions like 'bl'.
25 * There is the real example:
26 * We may hang in the next code as we store any BLINK register on stack in
27 * invalidate_dcache_all() function.
29 * void flush_dcache_all() {
30 * __dc_entire_op(OP_FLUSH);
34 * void invalidate_dcache_all() {
35 * __dc_entire_op(OP_INV);
41 * invalidate_dcache_all();
44 * Now let's see what really happens during that code execution:
47 * |->> call flush_dcache_all
48 * [return address is saved to BLINK register]
49 * [push BLINK] (save to stack) ![point 1]
50 * |->> call __dc_entire_op(OP_FLUSH)
51 * [return address is saved to BLINK register]
53 * return [jump to BLINK]
55 * [other flush_dcache_all code]
56 * [pop BLINK] (get from stack)
57 * return [jump to BLINK]
59 * |->> call invalidate_dcache_all
60 * [return address is saved to BLINK register]
61 * [push BLINK] (save to stack) ![point 2]
62 * |->> call __dc_entire_op(OP_FLUSH)
63 * [return address is saved to BLINK register]
64 * [invalidate L1 D$] ![point 3]
66 * // We lose return address from invalidate_dcache_all function:
67 * // we save it to stack and invalidate L1 D$ after that!
68 * return [jump to BLINK]
70 * [other invalidate_dcache_all code]
71 * [pop BLINK] (get from stack)
72 * // we don't have this data in L1 dcache as we invalidated it in [point 3]
73 * // so we get it from next memory level (for example DDR memory)
74 * // but in the memory we have value which we save in [point 1], which
75 * // is return address from flush_dcache_all function (instead of
76 * // address from current invalidate_dcache_all function which we
77 * // saved in [point 2] !)
78 * return [jump to BLINK]
80 * // As BLINK points to invalidate_dcache_all, we call it again and
83 * Fortunately we may fix that by using flush & invalidation of D$ with a single
84 * one instruction (instead of flush and invalidation instructions pair) and
85 * enabling force function inline with '__attribute__((always_inline))' gcc
86 * attribute to avoid any function call (and BLINK store) between cache flush
90 DECLARE_GLOBAL_DATA_PTR;
92 /* Bit values in IC_CTRL */
93 #define IC_CTRL_CACHE_DISABLE BIT(0)
95 /* Bit values in DC_CTRL */
96 #define DC_CTRL_CACHE_DISABLE BIT(0)
97 #define DC_CTRL_INV_MODE_FLUSH BIT(6)
98 #define DC_CTRL_FLUSH_STATUS BIT(8)
100 #define OP_INV BIT(0)
101 #define OP_FLUSH BIT(1)
102 #define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
104 /* Bit val in SLC_CONTROL */
105 #define SLC_CTRL_DIS 0x001
106 #define SLC_CTRL_IM 0x040
107 #define SLC_CTRL_BUSY 0x100
108 #define SLC_CTRL_RGN_OP_INV 0x200
110 #define CACHE_LINE_MASK (~(gd->arch.l1_line_sz - 1))
112 static inline bool pae_exists(void)
114 /* TODO: should we compare mmu version from BCR and from CONFIG? */
115 #if (CONFIG_ARC_MMU_VER >= 4)
116 union bcr_mmu_4 mmu4;
118 mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR);
122 #endif /* (CONFIG_ARC_MMU_VER >= 4) */
127 static inline bool icache_exists(void)
129 union bcr_di_cache ibcr;
131 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
132 return !!ibcr.fields.ver;
135 static inline bool icache_enabled(void)
137 if (!icache_exists())
140 return !(read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE);
143 static inline bool dcache_exists(void)
145 union bcr_di_cache dbcr;
147 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
148 return !!dbcr.fields.ver;
151 static inline bool dcache_enabled(void)
153 if (!dcache_exists())
156 return !(read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE);
159 static inline bool slc_exists(void)
161 if (is_isa_arcv2()) {
162 union bcr_generic sbcr;
164 sbcr.word = read_aux_reg(ARC_BCR_SLC);
165 return !!sbcr.fields.ver;
171 static inline bool slc_data_bypass(void)
174 * If L1 data cache is disabled SL$ is bypassed and all load/store
175 * requests are sent directly to main memory.
177 return !dcache_enabled();
180 static inline bool ioc_exists(void)
182 if (is_isa_arcv2()) {
183 union bcr_clust_cfg cbcr;
185 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
186 return cbcr.fields.c;
192 static inline bool ioc_enabled(void)
195 * We check only CONFIG option instead of IOC HW state check as IOC
196 * must be disabled by default.
198 if (is_ioc_enabled())
204 static void __slc_entire_op(const int op)
211 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
213 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
214 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
218 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
220 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
221 write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
223 write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
225 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
226 read_aux_reg(ARC_AUX_SLC_CTRL);
228 /* Important to wait for flush to complete */
229 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
232 static void slc_upper_region_init(void)
235 * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
236 * only if PAE exists in current HW. So we had to check pae_exist
243 * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
244 * as we don't use PAE40.
246 write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
247 write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
250 static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
252 #ifdef CONFIG_ISA_ARCV2
261 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
262 * - b'000 (default) is Flush,
263 * - b'001 is Invalidate if CTRL.IM == 0
264 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
266 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
268 /* Don't rely on default value of IM bit */
269 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
270 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
275 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
277 ctrl &= ~SLC_CTRL_RGN_OP_INV;
279 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
282 * Lower bits are ignored, no need to clip
283 * END needs to be setup before START (latter triggers the operation)
284 * END can't be same as START, so add (l2_line_sz - 1) to sz
286 end = paddr + sz + gd->arch.slc_line_sz - 1;
289 * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
290 * are always == 0 as we don't use PAE40, so we only setup lower ones
291 * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
293 write_aux_reg(ARC_AUX_SLC_RGN_END, end);
294 write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
296 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
297 read_aux_reg(ARC_AUX_SLC_CTRL);
299 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
301 #endif /* CONFIG_ISA_ARCV2 */
304 static void arc_ioc_setup(void)
306 /* IOC Aperture start is equal to DDR start */
307 unsigned int ap_base = CONFIG_SYS_SDRAM_BASE;
308 /* IOC Aperture size is equal to DDR size */
309 long ap_size = CONFIG_SYS_SDRAM_SIZE;
311 flush_n_invalidate_dcache_all();
313 if (!is_power_of_2(ap_size) || ap_size < 4096)
314 panic("IOC Aperture size must be power of 2 and bigger 4Kib");
317 * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
318 * so setting 0x11 implies 512M, 0x12 implies 1G...
320 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
321 order_base_2(ap_size / 1024) - 2);
323 /* IOC Aperture start must be aligned to the size of the aperture */
324 if (ap_base % ap_size != 0)
325 panic("IOC Aperture start must be aligned to the size of the aperture");
327 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
328 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
329 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
332 static void read_decode_cache_bcr_arcv2(void)
334 #ifdef CONFIG_ISA_ARCV2
336 union bcr_slc_cfg slc_cfg;
339 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
340 gd->arch.slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
343 #endif /* CONFIG_ISA_ARCV2 */
346 void read_decode_cache_bcr(void)
348 int dc_line_sz = 0, ic_line_sz = 0;
349 union bcr_di_cache ibcr, dbcr;
351 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
352 if (ibcr.fields.ver) {
353 gd->arch.l1_line_sz = ic_line_sz = 8 << ibcr.fields.line_len;
355 panic("Instruction exists but line length is 0\n");
358 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
359 if (dbcr.fields.ver) {
360 gd->arch.l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
362 panic("Data cache exists but line length is 0\n");
365 if (ic_line_sz && dc_line_sz && (ic_line_sz != dc_line_sz))
366 panic("Instruction and data cache line lengths differ\n");
369 void cache_init(void)
371 read_decode_cache_bcr();
374 read_decode_cache_bcr_arcv2();
376 if (is_isa_arcv2() && ioc_enabled())
379 if (is_isa_arcv2() && slc_exists())
380 slc_upper_region_init();
383 int icache_status(void)
385 return icache_enabled();
388 void icache_enable(void)
391 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
392 ~IC_CTRL_CACHE_DISABLE);
395 void icache_disable(void)
398 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
399 IC_CTRL_CACHE_DISABLE);
402 /* IC supports only invalidation */
403 static inline void __ic_entire_invalidate(void)
405 if (!icache_enabled())
408 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
409 write_aux_reg(ARC_AUX_IC_IVIC, 1);
411 * As per ARC HS databook (see chapter 5.3.3.2)
412 * it is required to add 3 NOPs after each write to IC_IVIC.
417 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
420 void invalidate_icache_all(void)
422 __ic_entire_invalidate();
425 * If SL$ is bypassed for data it is used only for instructions,
426 * so we need to invalidate it too.
427 * TODO: HS 3.0 supports SLC disable so we need to check slc
428 * enable/disable status here.
430 if (is_isa_arcv2() && slc_data_bypass())
431 __slc_entire_op(OP_INV);
434 int dcache_status(void)
436 return dcache_enabled();
439 void dcache_enable(void)
441 if (!dcache_exists())
444 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
445 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
448 void dcache_disable(void)
450 if (!dcache_exists())
453 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
454 DC_CTRL_CACHE_DISABLE);
457 /* Common Helper for Line Operations on D-cache */
458 static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
461 unsigned int aux_cmd;
464 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
465 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
467 sz += paddr & ~CACHE_LINE_MASK;
468 paddr &= CACHE_LINE_MASK;
470 num_lines = DIV_ROUND_UP(sz, gd->arch.l1_line_sz);
472 while (num_lines-- > 0) {
473 #if (CONFIG_ARC_MMU_VER == 3)
474 write_aux_reg(ARC_AUX_DC_PTAG, paddr);
476 write_aux_reg(aux_cmd, paddr);
477 paddr += gd->arch.l1_line_sz;
481 static void __before_dc_op(const int op)
485 ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
487 /* IM bit implies flush-n-inv, instead of vanilla inv */
489 ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
491 ctrl |= DC_CTRL_INV_MODE_FLUSH;
493 write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
496 static void __after_dc_op(const int op)
498 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
499 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
502 static inline void __dc_entire_op(const int cacheop)
506 if (!dcache_enabled())
509 __before_dc_op(cacheop);
511 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
512 aux = ARC_AUX_DC_IVDC;
514 aux = ARC_AUX_DC_FLSH;
516 write_aux_reg(aux, 0x1);
518 __after_dc_op(cacheop);
521 static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
524 if (!dcache_enabled())
527 __before_dc_op(cacheop);
528 __dcache_line_loop(paddr, sz, cacheop);
529 __after_dc_op(cacheop);
532 void invalidate_dcache_range(unsigned long start, unsigned long end)
538 * ARCv1 -> call __dc_line_op
539 * ARCv2 && L1 D$ disabled -> nothing
540 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
541 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
543 if (!is_isa_arcv2() || !ioc_enabled())
544 __dc_line_op(start, end - start, OP_INV);
546 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
547 __slc_rgn_op(start, end - start, OP_INV);
550 void flush_dcache_range(unsigned long start, unsigned long end)
556 * ARCv1 -> call __dc_line_op
557 * ARCv2 && L1 D$ disabled -> nothing
558 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
559 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
561 if (!is_isa_arcv2() || !ioc_enabled())
562 __dc_line_op(start, end - start, OP_FLUSH);
564 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
565 __slc_rgn_op(start, end - start, OP_FLUSH);
568 void flush_cache(unsigned long start, unsigned long size)
570 flush_dcache_range(start, start + size);
574 * As invalidate_dcache_all() is not used in generic U-Boot code and as we
575 * don't need it in arch/arc code alone (invalidate without flush) we implement
576 * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
577 * it's much safer. See [ NOTE 1 ] for more details.
579 void flush_n_invalidate_dcache_all(void)
581 __dc_entire_op(OP_FLUSH_N_INV);
583 if (is_isa_arcv2() && !slc_data_bypass())
584 __slc_entire_op(OP_FLUSH_N_INV);
587 void flush_dcache_all(void)
589 __dc_entire_op(OP_FLUSH);
591 if (is_isa_arcv2() && !slc_data_bypass())
592 __slc_entire_op(OP_FLUSH);