blob: 6f52877643cbd167a33bc8f6a6e134818a012532 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkin2f16ac92014-02-04 12:56:14 +04002/*
3 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
Alexey Brodkin2f16ac92014-02-04 12:56:14 +04004 */
5
6#include <config.h>
Alexey Brodkin379b3282015-12-14 17:14:46 +03007#include <common.h>
Alexey Brodkinef639e62015-05-18 16:56:26 +03008#include <linux/compiler.h>
9#include <linux/kernel.h>
Alexey Brodkin97a63142017-06-26 11:46:47 +030010#include <linux/log2.h>
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040011#include <asm/arcregs.h>
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +030012#include <asm/arc-bcr.h>
Alexey Brodkin205e7a72015-02-03 13:58:13 +030013#include <asm/cache.h>
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040014
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +030015/*
16 * [ NOTE 1 ]:
17 * Data cache (L1 D$ or SL$) entire invalidate operation or data cache disable
18 * operation may result in unexpected behavior and data loss even if we flush
19 * data cache right before invalidation. That may happens if we store any context
20 * on stack (like we store BLINK register on stack before function call).
21 * BLINK register is the register where return address is automatically saved
22 * when we do function call with instructions like 'bl'.
23 *
24 * There is the real example:
25 * We may hang in the next code as we store any BLINK register on stack in
26 * invalidate_dcache_all() function.
27 *
28 * void flush_dcache_all() {
29 * __dc_entire_op(OP_FLUSH);
30 * // Other code //
31 * }
32 *
33 * void invalidate_dcache_all() {
34 * __dc_entire_op(OP_INV);
35 * // Other code //
36 * }
37 *
38 * void foo(void) {
39 * flush_dcache_all();
40 * invalidate_dcache_all();
41 * }
42 *
43 * Now let's see what really happens during that code execution:
44 *
45 * foo()
46 * |->> call flush_dcache_all
47 * [return address is saved to BLINK register]
48 * [push BLINK] (save to stack) ![point 1]
49 * |->> call __dc_entire_op(OP_FLUSH)
50 * [return address is saved to BLINK register]
51 * [flush L1 D$]
52 * return [jump to BLINK]
53 * <<------
54 * [other flush_dcache_all code]
55 * [pop BLINK] (get from stack)
56 * return [jump to BLINK]
57 * <<------
58 * |->> call invalidate_dcache_all
59 * [return address is saved to BLINK register]
60 * [push BLINK] (save to stack) ![point 2]
61 * |->> call __dc_entire_op(OP_FLUSH)
62 * [return address is saved to BLINK register]
63 * [invalidate L1 D$] ![point 3]
64 * // Oops!!!
65 * // We lose return address from invalidate_dcache_all function:
66 * // we save it to stack and invalidate L1 D$ after that!
67 * return [jump to BLINK]
68 * <<------
69 * [other invalidate_dcache_all code]
70 * [pop BLINK] (get from stack)
71 * // we don't have this data in L1 dcache as we invalidated it in [point 3]
72 * // so we get it from next memory level (for example DDR memory)
73 * // but in the memory we have value which we save in [point 1], which
74 * // is return address from flush_dcache_all function (instead of
75 * // address from current invalidate_dcache_all function which we
76 * // saved in [point 2] !)
77 * return [jump to BLINK]
78 * <<------
79 * // As BLINK points to invalidate_dcache_all, we call it again and
80 * // loop forever.
81 *
82 * Fortunately we may fix that by using flush & invalidation of D$ with a single
83 * one instruction (instead of flush and invalidation instructions pair) and
84 * enabling force function inline with '__attribute__((always_inline))' gcc
85 * attribute to avoid any function call (and BLINK store) between cache flush
86 * and disable.
Eugeniy Paltsev72419442018-03-21 15:59:03 +030087 *
88 *
89 * [ NOTE 2 ]:
90 * As of today we only support the following cache configurations on ARC.
91 * Other configurations may exist in HW (for example, since version 3.0 HS
92 * supports SL$ (L2 system level cache) disable) but we don't support it in SW.
93 * Configuration 1:
94 * ______________________
95 * | |
96 * | ARC CPU |
97 * |______________________|
98 * ___|___ ___|___
99 * | | | |
100 * | L1 I$ | | L1 D$ |
101 * |_______| |_______|
102 * on/off on/off
103 * ___|______________|____
104 * | |
105 * | main memory |
106 * |______________________|
107 *
108 * Configuration 2:
109 * ______________________
110 * | |
111 * | ARC CPU |
112 * |______________________|
113 * ___|___ ___|___
114 * | | | |
115 * | L1 I$ | | L1 D$ |
116 * |_______| |_______|
117 * on/off on/off
118 * ___|______________|____
119 * | |
120 * | L2 (SL$) |
121 * |______________________|
122 * always must be on
123 * ___|______________|____
124 * | |
125 * | main memory |
126 * |______________________|
127 *
128 * Configuration 3:
129 * ______________________
130 * | |
131 * | ARC CPU |
132 * |______________________|
133 * ___|___ ___|___
134 * | | | |
135 * | L1 I$ | | L1 D$ |
136 * |_______| |_______|
137 * on/off must be on
138 * ___|______________|____ _______
139 * | | | |
140 * | L2 (SL$) |-----| IOC |
141 * |______________________| |_______|
142 * always must be on on/off
143 * ___|______________|____
144 * | |
145 * | main memory |
146 * |______________________|
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300147 */
148
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300149DECLARE_GLOBAL_DATA_PTR;
150
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400151/* Bit values in IC_CTRL */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300152#define IC_CTRL_CACHE_DISABLE BIT(0)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400153
154/* Bit values in DC_CTRL */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300155#define DC_CTRL_CACHE_DISABLE BIT(0)
156#define DC_CTRL_INV_MODE_FLUSH BIT(6)
157#define DC_CTRL_FLUSH_STATUS BIT(8)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400158
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300159#define OP_INV BIT(0)
160#define OP_FLUSH BIT(1)
161#define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300162
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300163/* Bit val in SLC_CONTROL */
164#define SLC_CTRL_DIS 0x001
165#define SLC_CTRL_IM 0x040
166#define SLC_CTRL_BUSY 0x100
167#define SLC_CTRL_RGN_OP_INV 0x200
168
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300169#define CACHE_LINE_MASK (~(gd->arch.l1_line_sz - 1))
Alexey Brodkin379b3282015-12-14 17:14:46 +0300170
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300171/*
172 * We don't want to use '__always_inline' macro here as it can be redefined
173 * to simple 'inline' in some cases which breaks stuff. See [ NOTE 1 ] for more
174 * details about the reasons we need to use always_inline functions.
175 */
176#define inlined_cachefunc inline __attribute__((always_inline))
177
178static inlined_cachefunc void __ic_entire_invalidate(void);
179static inlined_cachefunc void __dc_entire_op(const int cacheop);
180
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300181static inline bool pae_exists(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300182{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300183 /* TODO: should we compare mmu version from BCR and from CONFIG? */
184#if (CONFIG_ARC_MMU_VER >= 4)
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300185 union bcr_mmu_4 mmu4;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300186
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300187 mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300188
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300189 if (mmu4.fields.pae)
190 return true;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300191#endif /* (CONFIG_ARC_MMU_VER >= 4) */
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300192
193 return false;
194}
195
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300196static inlined_cachefunc bool icache_exists(void)
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300197{
198 union bcr_di_cache ibcr;
199
200 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
201 return !!ibcr.fields.ver;
202}
203
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300204static inlined_cachefunc bool icache_enabled(void)
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300205{
206 if (!icache_exists())
207 return false;
208
209 return !(read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE);
210}
211
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300212static inlined_cachefunc bool dcache_exists(void)
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300213{
214 union bcr_di_cache dbcr;
215
216 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
217 return !!dbcr.fields.ver;
218}
219
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300220static inlined_cachefunc bool dcache_enabled(void)
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300221{
222 if (!dcache_exists())
223 return false;
224
225 return !(read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE);
226}
227
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300228static inlined_cachefunc bool slc_exists(void)
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300229{
230 if (is_isa_arcv2()) {
231 union bcr_generic sbcr;
232
233 sbcr.word = read_aux_reg(ARC_BCR_SLC);
234 return !!sbcr.fields.ver;
235 }
236
237 return false;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300238}
239
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300240static inlined_cachefunc bool slc_data_bypass(void)
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300241{
242 /*
243 * If L1 data cache is disabled SL$ is bypassed and all load/store
244 * requests are sent directly to main memory.
245 */
246 return !dcache_enabled();
247}
248
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300249static inline bool ioc_exists(void)
250{
251 if (is_isa_arcv2()) {
252 union bcr_clust_cfg cbcr;
253
254 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
255 return cbcr.fields.c;
256 }
257
258 return false;
259}
260
261static inline bool ioc_enabled(void)
262{
263 /*
264 * We check only CONFIG option instead of IOC HW state check as IOC
265 * must be disabled by default.
266 */
267 if (is_ioc_enabled())
268 return ioc_exists();
269
270 return false;
271}
272
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300273static inlined_cachefunc void __slc_entire_op(const int op)
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300274{
275 unsigned int ctrl;
276
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300277 if (!slc_exists())
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300278 return;
279
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300280 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
281
282 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
283 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
284 else
285 ctrl |= SLC_CTRL_IM;
286
287 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
288
289 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
290 write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
291 else
292 write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
293
294 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
295 read_aux_reg(ARC_AUX_SLC_CTRL);
296
297 /* Important to wait for flush to complete */
298 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
299}
300
301static void slc_upper_region_init(void)
302{
303 /*
Eugeniy Paltsev246ba282018-03-21 15:58:58 +0300304 * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
305 * only if PAE exists in current HW. So we had to check pae_exist
306 * before using them.
307 */
308 if (!pae_exists())
309 return;
310
311 /*
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300312 * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
313 * as we don't use PAE40.
314 */
315 write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
316 write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
317}
318
319static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
320{
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300321#ifdef CONFIG_ISA_ARCV2
322
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300323 unsigned int ctrl;
324 unsigned long end;
325
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300326 if (!slc_exists())
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300327 return;
328
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300329 /*
330 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
331 * - b'000 (default) is Flush,
332 * - b'001 is Invalidate if CTRL.IM == 0
333 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
334 */
335 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
336
337 /* Don't rely on default value of IM bit */
338 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
339 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
340 else
341 ctrl |= SLC_CTRL_IM;
342
343 if (op & OP_INV)
344 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
345 else
346 ctrl &= ~SLC_CTRL_RGN_OP_INV;
347
348 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
349
350 /*
351 * Lower bits are ignored, no need to clip
352 * END needs to be setup before START (latter triggers the operation)
353 * END can't be same as START, so add (l2_line_sz - 1) to sz
354 */
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300355 end = paddr + sz + gd->arch.slc_line_sz - 1;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300356
357 /*
358 * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
359 * are always == 0 as we don't use PAE40, so we only setup lower ones
360 * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
361 */
362 write_aux_reg(ARC_AUX_SLC_RGN_END, end);
363 write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
364
365 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
366 read_aux_reg(ARC_AUX_SLC_CTRL);
367
368 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300369
370#endif /* CONFIG_ISA_ARCV2 */
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300371}
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300372
373static void arc_ioc_setup(void)
374{
375 /* IOC Aperture start is equal to DDR start */
376 unsigned int ap_base = CONFIG_SYS_SDRAM_BASE;
377 /* IOC Aperture size is equal to DDR size */
378 long ap_size = CONFIG_SYS_SDRAM_SIZE;
379
Eugeniy Paltsev72419442018-03-21 15:59:03 +0300380 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
381 if (!slc_exists())
382 panic("Try to enable IOC but SLC is not present");
383
384 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
385 if (!dcache_enabled())
386 panic("Try to enable IOC but L1 D$ is disabled");
387
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300388 if (!is_power_of_2(ap_size) || ap_size < 4096)
389 panic("IOC Aperture size must be power of 2 and bigger 4Kib");
390
Eugeniy Paltsev6b85b262018-03-21 15:59:05 +0300391 /* IOC Aperture start must be aligned to the size of the aperture */
392 if (ap_base % ap_size != 0)
393 panic("IOC Aperture start must be aligned to the size of the aperture");
394
395 flush_n_invalidate_dcache_all();
396
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300397 /*
398 * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
399 * so setting 0x11 implies 512M, 0x12 implies 1G...
400 */
401 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
402 order_base_2(ap_size / 1024) - 2);
403
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300404 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
405 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
406 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
407}
Alexey Brodkinef639e62015-05-18 16:56:26 +0300408
Alexey Brodkin379b3282015-12-14 17:14:46 +0300409static void read_decode_cache_bcr_arcv2(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300410{
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300411#ifdef CONFIG_ISA_ARCV2
412
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300413 union bcr_slc_cfg slc_cfg;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300414
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300415 if (slc_exists()) {
Alexey Brodkin379b3282015-12-14 17:14:46 +0300416 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300417 gd->arch.slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
Eugeniy Paltsev72419442018-03-21 15:59:03 +0300418
419 /*
420 * We don't support configuration where L1 I$ or L1 D$ is
421 * absent but SL$ exists. See [ NOTE 2 ] for more details.
422 */
423 if (!icache_exists() || !dcache_exists())
424 panic("Unsupported cache configuration: SLC exists but one of L1 caches is absent");
Alexey Brodkin379b3282015-12-14 17:14:46 +0300425 }
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300426
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300427#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin379b3282015-12-14 17:14:46 +0300428}
Alexey Brodkin379b3282015-12-14 17:14:46 +0300429
430void read_decode_cache_bcr(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300431{
Alexey Brodkin379b3282015-12-14 17:14:46 +0300432 int dc_line_sz = 0, ic_line_sz = 0;
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300433 union bcr_di_cache ibcr, dbcr;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300434
435 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
436 if (ibcr.fields.ver) {
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300437 gd->arch.l1_line_sz = ic_line_sz = 8 << ibcr.fields.line_len;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300438 if (!ic_line_sz)
439 panic("Instruction exists but line length is 0\n");
440 }
441
442 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300443 if (dbcr.fields.ver) {
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300444 gd->arch.l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300445 if (!dc_line_sz)
446 panic("Data cache exists but line length is 0\n");
447 }
448
449 if (ic_line_sz && dc_line_sz && (ic_line_sz != dc_line_sz))
450 panic("Instruction and data cache line lengths differ\n");
Alexey Brodkinef639e62015-05-18 16:56:26 +0300451}
452
453void cache_init(void)
454{
Alexey Brodkin379b3282015-12-14 17:14:46 +0300455 read_decode_cache_bcr();
456
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300457 if (is_isa_arcv2())
458 read_decode_cache_bcr_arcv2();
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300459
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300460 if (is_isa_arcv2() && ioc_enabled())
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300461 arc_ioc_setup();
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300462
Eugeniy Paltsev246ba282018-03-21 15:58:58 +0300463 if (is_isa_arcv2() && slc_exists())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300464 slc_upper_region_init();
Alexey Brodkinef639e62015-05-18 16:56:26 +0300465}
466
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400467int icache_status(void)
468{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300469 return icache_enabled();
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400470}
471
472void icache_enable(void)
473{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300474 if (icache_exists())
Alexey Brodkinef639e62015-05-18 16:56:26 +0300475 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
476 ~IC_CTRL_CACHE_DISABLE);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400477}
478
479void icache_disable(void)
480{
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300481 if (!icache_exists())
482 return;
483
484 __ic_entire_invalidate();
485
486 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
487 IC_CTRL_CACHE_DISABLE);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400488}
489
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300490/* IC supports only invalidation */
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300491static inlined_cachefunc void __ic_entire_invalidate(void)
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300492{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300493 if (!icache_enabled())
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300494 return;
495
496 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
497 write_aux_reg(ARC_AUX_IC_IVIC, 1);
498 /*
499 * As per ARC HS databook (see chapter 5.3.3.2)
500 * it is required to add 3 NOPs after each write to IC_IVIC.
501 */
502 __builtin_arc_nop();
503 __builtin_arc_nop();
504 __builtin_arc_nop();
505 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
506}
507
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400508void invalidate_icache_all(void)
509{
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300510 __ic_entire_invalidate();
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300511
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300512 /*
513 * If SL$ is bypassed for data it is used only for instructions,
514 * so we need to invalidate it too.
515 * TODO: HS 3.0 supports SLC disable so we need to check slc
516 * enable/disable status here.
517 */
518 if (is_isa_arcv2() && slc_data_bypass())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300519 __slc_entire_op(OP_INV);
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300520}
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400521
522int dcache_status(void)
523{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300524 return dcache_enabled();
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400525}
526
527void dcache_enable(void)
528{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300529 if (!dcache_exists())
Igor Guryanovf8cf3d12014-12-24 16:07:07 +0300530 return;
531
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400532 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
533 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
534}
535
536void dcache_disable(void)
537{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300538 if (!dcache_exists())
Igor Guryanovf8cf3d12014-12-24 16:07:07 +0300539 return;
540
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300541 __dc_entire_op(OP_FLUSH_N_INV);
542
543 /*
544 * As SLC will be bypassed for data after L1 D$ disable we need to
545 * flush it first before L1 D$ disable. Also we invalidate SLC to
546 * avoid any inconsistent data problems after enabling L1 D$ again with
547 * dcache_enable function.
548 */
549 if (is_isa_arcv2())
550 __slc_entire_op(OP_FLUSH_N_INV);
551
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400552 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
553 DC_CTRL_CACHE_DISABLE);
554}
555
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300556/* Common Helper for Line Operations on D-cache */
557static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
558 const int cacheop)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400559{
Alexey Brodkinef639e62015-05-18 16:56:26 +0300560 unsigned int aux_cmd;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300561 int num_lines;
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400562
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300563 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
564 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300565
566 sz += paddr & ~CACHE_LINE_MASK;
567 paddr &= CACHE_LINE_MASK;
568
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300569 num_lines = DIV_ROUND_UP(sz, gd->arch.l1_line_sz);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300570
571 while (num_lines-- > 0) {
572#if (CONFIG_ARC_MMU_VER == 3)
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300573 write_aux_reg(ARC_AUX_DC_PTAG, paddr);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300574#endif
575 write_aux_reg(aux_cmd, paddr);
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300576 paddr += gd->arch.l1_line_sz;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300577 }
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400578}
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400579
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300580static inlined_cachefunc void __before_dc_op(const int op)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400581{
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300582 unsigned int ctrl;
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400583
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300584 ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400585
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300586 /* IM bit implies flush-n-inv, instead of vanilla inv */
587 if (op == OP_INV)
588 ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
589 else
590 ctrl |= DC_CTRL_INV_MODE_FLUSH;
591
592 write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400593}
594
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300595static inlined_cachefunc void __after_dc_op(const int op)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300596{
597 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300598 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300599}
600
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300601static inlined_cachefunc void __dc_entire_op(const int cacheop)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300602{
603 int aux;
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300604
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300605 if (!dcache_enabled())
Eugeniy Paltsevc877a892018-03-21 15:58:53 +0300606 return;
607
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300608 __before_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300609
610 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
611 aux = ARC_AUX_DC_IVDC;
612 else
613 aux = ARC_AUX_DC_FLSH;
614
615 write_aux_reg(aux, 0x1);
616
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300617 __after_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300618}
619
620static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
621 const int cacheop)
622{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300623 if (!dcache_enabled())
Eugeniy Paltsevc877a892018-03-21 15:58:53 +0300624 return;
625
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300626 __before_dc_op(cacheop);
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300627 __dcache_line_loop(paddr, sz, cacheop);
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300628 __after_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300629}
Alexey Brodkinef639e62015-05-18 16:56:26 +0300630
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400631void invalidate_dcache_range(unsigned long start, unsigned long end)
632{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300633 if (start >= end)
634 return;
635
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300636 /*
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300637 * ARCv1 -> call __dc_line_op
638 * ARCv2 && L1 D$ disabled -> nothing
639 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
640 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300641 */
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300642 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300643 __dc_line_op(start, end - start, OP_INV);
644
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300645 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300646 __slc_rgn_op(start, end - start, OP_INV);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400647}
648
Alexey Brodkinef639e62015-05-18 16:56:26 +0300649void flush_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400650{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300651 if (start >= end)
652 return;
653
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300654 /*
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300655 * ARCv1 -> call __dc_line_op
656 * ARCv2 && L1 D$ disabled -> nothing
657 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
658 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300659 */
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300660 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300661 __dc_line_op(start, end - start, OP_FLUSH);
662
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300663 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300664 __slc_rgn_op(start, end - start, OP_FLUSH);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400665}
666
667void flush_cache(unsigned long start, unsigned long size)
668{
669 flush_dcache_range(start, start + size);
670}
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300671
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300672/*
673 * As invalidate_dcache_all() is not used in generic U-Boot code and as we
674 * don't need it in arch/arc code alone (invalidate without flush) we implement
675 * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
676 * it's much safer. See [ NOTE 1 ] for more details.
677 */
678void flush_n_invalidate_dcache_all(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300679{
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300680 __dc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300681
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300682 if (is_isa_arcv2() && !slc_data_bypass())
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300683 __slc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300684}
685
Alexey Brodkinef639e62015-05-18 16:56:26 +0300686void flush_dcache_all(void)
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300687{
Alexey Brodkin2a8382c2016-04-16 15:28:30 +0300688 __dc_entire_op(OP_FLUSH);
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300689
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300690 if (is_isa_arcv2() && !slc_data_bypass())
Alexey Brodkinef639e62015-05-18 16:56:26 +0300691 __slc_entire_op(OP_FLUSH);
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300692}
Eugeniy Paltsev375945b2018-03-21 15:59:02 +0300693
694/*
695 * This is function to cleanup all caches (and therefore sync I/D caches) which
696 * can be used for cleanup before linux launch or to sync caches during
697 * relocation.
698 */
699void sync_n_cleanup_cache_all(void)
700{
701 __dc_entire_op(OP_FLUSH_N_INV);
702
703 /*
704 * If SL$ is bypassed for data it is used only for instructions,
705 * and we shouldn't flush it. So invalidate it instead of flush_n_inv.
706 */
707 if (is_isa_arcv2()) {
708 if (slc_data_bypass())
709 __slc_entire_op(OP_INV);
710 else
711 __slc_entire_op(OP_FLUSH_N_INV);
712 }
713
714 __ic_entire_invalidate();
715}