blob: 5169fc627fa5f49c8ae4b8e9ddf62a631dd9b442 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkin3a59d912014-02-04 12:56:14 +04002/*
3 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
Alexey Brodkin3a59d912014-02-04 12:56:14 +04004 */
5
6#include <config.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07007#include <cpu_func.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06008#include <asm/global_data.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -06009#include <linux/bitops.h>
Alexey Brodkin9f916ee2015-05-18 16:56:26 +030010#include <linux/compiler.h>
11#include <linux/kernel.h>
Alexey Brodkin982f6bf2017-06-26 11:46:47 +030012#include <linux/log2.h>
Alexey Brodkin3a59d912014-02-04 12:56:14 +040013#include <asm/arcregs.h>
Eugeniy Paltsev589ac752018-03-21 15:58:52 +030014#include <asm/arc-bcr.h>
Alexey Brodkin6b95cca2015-02-03 13:58:13 +030015#include <asm/cache.h>
Alexey Brodkin3a59d912014-02-04 12:56:14 +040016
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +030017/*
18 * [ NOTE 1 ]:
19 * Data cache (L1 D$ or SL$) entire invalidate operation or data cache disable
20 * operation may result in unexpected behavior and data loss even if we flush
21 * data cache right before invalidation. That may happens if we store any context
22 * on stack (like we store BLINK register on stack before function call).
23 * BLINK register is the register where return address is automatically saved
24 * when we do function call with instructions like 'bl'.
25 *
26 * There is the real example:
27 * We may hang in the next code as we store any BLINK register on stack in
28 * invalidate_dcache_all() function.
29 *
30 * void flush_dcache_all() {
31 * __dc_entire_op(OP_FLUSH);
32 * // Other code //
33 * }
34 *
35 * void invalidate_dcache_all() {
36 * __dc_entire_op(OP_INV);
37 * // Other code //
38 * }
39 *
40 * void foo(void) {
41 * flush_dcache_all();
42 * invalidate_dcache_all();
43 * }
44 *
45 * Now let's see what really happens during that code execution:
46 *
47 * foo()
48 * |->> call flush_dcache_all
49 * [return address is saved to BLINK register]
50 * [push BLINK] (save to stack) ![point 1]
51 * |->> call __dc_entire_op(OP_FLUSH)
52 * [return address is saved to BLINK register]
53 * [flush L1 D$]
54 * return [jump to BLINK]
55 * <<------
56 * [other flush_dcache_all code]
57 * [pop BLINK] (get from stack)
58 * return [jump to BLINK]
59 * <<------
60 * |->> call invalidate_dcache_all
61 * [return address is saved to BLINK register]
62 * [push BLINK] (save to stack) ![point 2]
63 * |->> call __dc_entire_op(OP_FLUSH)
64 * [return address is saved to BLINK register]
65 * [invalidate L1 D$] ![point 3]
66 * // Oops!!!
67 * // We lose return address from invalidate_dcache_all function:
68 * // we save it to stack and invalidate L1 D$ after that!
69 * return [jump to BLINK]
70 * <<------
71 * [other invalidate_dcache_all code]
72 * [pop BLINK] (get from stack)
73 * // we don't have this data in L1 dcache as we invalidated it in [point 3]
74 * // so we get it from next memory level (for example DDR memory)
75 * // but in the memory we have value which we save in [point 1], which
76 * // is return address from flush_dcache_all function (instead of
77 * // address from current invalidate_dcache_all function which we
78 * // saved in [point 2] !)
79 * return [jump to BLINK]
80 * <<------
81 * // As BLINK points to invalidate_dcache_all, we call it again and
82 * // loop forever.
83 *
84 * Fortunately we may fix that by using flush & invalidation of D$ with a single
85 * one instruction (instead of flush and invalidation instructions pair) and
86 * enabling force function inline with '__attribute__((always_inline))' gcc
87 * attribute to avoid any function call (and BLINK store) between cache flush
88 * and disable.
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +030089 *
90 *
91 * [ NOTE 2 ]:
92 * As of today we only support the following cache configurations on ARC.
Eugeniy Paltsev67c34922020-03-11 15:00:43 +030093 * Other configurations may exist in HW but we don't support it in SW.
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +030094 * Configuration 1:
95 * ______________________
96 * | |
97 * | ARC CPU |
98 * |______________________|
99 * ___|___ ___|___
100 * | | | |
101 * | L1 I$ | | L1 D$ |
102 * |_______| |_______|
103 * on/off on/off
104 * ___|______________|____
105 * | |
106 * | main memory |
107 * |______________________|
108 *
109 * Configuration 2:
110 * ______________________
111 * | |
112 * | ARC CPU |
113 * |______________________|
114 * ___|___ ___|___
115 * | | | |
116 * | L1 I$ | | L1 D$ |
117 * |_______| |_______|
118 * on/off on/off
119 * ___|______________|____
120 * | |
121 * | L2 (SL$) |
122 * |______________________|
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300123 * always on (ARCv2, HS < 3.0)
124 * on/off (ARCv2, HS >= 3.0)
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300125 * ___|______________|____
126 * | |
127 * | main memory |
128 * |______________________|
129 *
130 * Configuration 3:
131 * ______________________
132 * | |
133 * | ARC CPU |
134 * |______________________|
135 * ___|___ ___|___
136 * | | | |
137 * | L1 I$ | | L1 D$ |
138 * |_______| |_______|
139 * on/off must be on
140 * ___|______________|____ _______
141 * | | | |
142 * | L2 (SL$) |-----| IOC |
143 * |______________________| |_______|
144 * always must be on on/off
145 * ___|______________|____
146 * | |
147 * | main memory |
148 * |______________________|
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300149 */
150
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300151DECLARE_GLOBAL_DATA_PTR;
152
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400153/* Bit values in IC_CTRL */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300154#define IC_CTRL_CACHE_DISABLE BIT(0)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400155
156/* Bit values in DC_CTRL */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300157#define DC_CTRL_CACHE_DISABLE BIT(0)
158#define DC_CTRL_INV_MODE_FLUSH BIT(6)
159#define DC_CTRL_FLUSH_STATUS BIT(8)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400160
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300161#define OP_INV BIT(0)
162#define OP_FLUSH BIT(1)
163#define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300164
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300165/* Bit val in SLC_CONTROL */
166#define SLC_CTRL_DIS 0x001
167#define SLC_CTRL_IM 0x040
168#define SLC_CTRL_BUSY 0x100
169#define SLC_CTRL_RGN_OP_INV 0x200
170
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300171#define CACHE_LINE_MASK (~(gd->arch.l1_line_sz - 1))
Alexey Brodkindff5df22015-12-14 17:14:46 +0300172
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300173/*
174 * We don't want to use '__always_inline' macro here as it can be redefined
175 * to simple 'inline' in some cases which breaks stuff. See [ NOTE 1 ] for more
176 * details about the reasons we need to use always_inline functions.
177 */
178#define inlined_cachefunc inline __attribute__((always_inline))
179
180static inlined_cachefunc void __ic_entire_invalidate(void);
181static inlined_cachefunc void __dc_entire_op(const int cacheop);
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300182static inlined_cachefunc void __slc_entire_op(const int op);
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300183static inlined_cachefunc bool ioc_enabled(void);
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300184
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300185static inline bool pae_exists(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300186{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300187 /* TODO: should we compare mmu version from BCR and from CONFIG? */
188#if (CONFIG_ARC_MMU_VER >= 4)
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300189 union bcr_mmu_4 mmu4;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300190
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300191 mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300192
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300193 if (mmu4.fields.pae)
194 return true;
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300195#endif /* (CONFIG_ARC_MMU_VER >= 4) */
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300196
197 return false;
198}
199
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300200static inlined_cachefunc bool icache_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300201{
202 union bcr_di_cache ibcr;
203
204 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
205 return !!ibcr.fields.ver;
206}
207
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300208static inlined_cachefunc bool icache_enabled(void)
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300209{
210 if (!icache_exists())
211 return false;
212
213 return !(read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE);
214}
215
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300216static inlined_cachefunc bool dcache_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300217{
218 union bcr_di_cache dbcr;
219
220 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
221 return !!dbcr.fields.ver;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300222}
223
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300224static inlined_cachefunc bool dcache_enabled(void)
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300225{
226 if (!dcache_exists())
227 return false;
228
229 return !(read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE);
230}
231
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300232static inlined_cachefunc bool slc_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300233{
234 if (is_isa_arcv2()) {
235 union bcr_generic sbcr;
236
237 sbcr.word = read_aux_reg(ARC_BCR_SLC);
238 return !!sbcr.fields.ver;
239 }
240
241 return false;
242}
243
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300244enum slc_dis_status {
245 ST_SLC_MISSING = 0,
246 ST_SLC_NO_DISABLE_CTRL,
247 ST_SLC_DISABLE_CTRL
248};
249
250/*
251 * ARCv1 -> ST_SLC_MISSING
252 * ARCv2 && SLC absent -> ST_SLC_MISSING
253 * ARCv2 && SLC exists && SLC version <= 2 -> ST_SLC_NO_DISABLE_CTRL
254 * ARCv2 && SLC exists && SLC version > 2 -> ST_SLC_DISABLE_CTRL
255 */
256static inlined_cachefunc enum slc_dis_status slc_disable_supported(void)
257{
258 if (is_isa_arcv2()) {
259 union bcr_generic sbcr;
260
261 sbcr.word = read_aux_reg(ARC_BCR_SLC);
262 if (sbcr.fields.ver == 0)
263 return ST_SLC_MISSING;
264 else if (sbcr.fields.ver <= 2)
265 return ST_SLC_NO_DISABLE_CTRL;
266 else
267 return ST_SLC_DISABLE_CTRL;
268 }
269
270 return ST_SLC_MISSING;
271}
272
273static inlined_cachefunc bool __slc_enabled(void)
274{
275 return !(read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_DIS);
276}
277
278static inlined_cachefunc void __slc_enable(void)
279{
280 unsigned int ctrl;
281
282 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
283 ctrl &= ~SLC_CTRL_DIS;
284 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
285}
286
287static inlined_cachefunc void __slc_disable(void)
288{
289 unsigned int ctrl;
290
291 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
292 ctrl |= SLC_CTRL_DIS;
293 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
294}
295
296static inlined_cachefunc bool slc_enabled(void)
297{
298 enum slc_dis_status slc_status = slc_disable_supported();
299
300 if (slc_status == ST_SLC_MISSING)
301 return false;
302 else if (slc_status == ST_SLC_NO_DISABLE_CTRL)
303 return true;
304 else
305 return __slc_enabled();
306}
307
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300308static inlined_cachefunc bool slc_data_bypass(void)
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300309{
310 /*
311 * If L1 data cache is disabled SL$ is bypassed and all load/store
312 * requests are sent directly to main memory.
313 */
314 return !dcache_enabled();
315}
316
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300317void slc_enable(void)
318{
319 if (slc_disable_supported() != ST_SLC_DISABLE_CTRL)
320 return;
321
322 if (__slc_enabled())
323 return;
324
325 __slc_enable();
326}
327
328/* TODO: warn if we are not able to disable SLC */
329void slc_disable(void)
330{
331 if (slc_disable_supported() != ST_SLC_DISABLE_CTRL)
332 return;
333
334 /* we don't support SLC disabling if we use IOC */
335 if (ioc_enabled())
336 return;
337
338 if (!__slc_enabled())
339 return;
340
341 /*
342 * We need to flush L1D$ to guarantee that we won't have any
343 * writeback operations during SLC disabling.
344 */
345 __dc_entire_op(OP_FLUSH);
346 __slc_entire_op(OP_FLUSH_N_INV);
347 __slc_disable();
348}
349
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300350static inlined_cachefunc bool ioc_exists(void)
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300351{
352 if (is_isa_arcv2()) {
353 union bcr_clust_cfg cbcr;
354
355 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
356 return cbcr.fields.c;
357 }
358
359 return false;
360}
361
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300362static inlined_cachefunc bool ioc_enabled(void)
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300363{
364 /*
365 * We check only CONFIG option instead of IOC HW state check as IOC
366 * must be disabled by default.
367 */
368 if (is_ioc_enabled())
369 return ioc_exists();
370
371 return false;
372}
373
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300374static inlined_cachefunc void __slc_entire_op(const int op)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300375{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300376 unsigned int ctrl;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300377
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300378 if (!slc_enabled())
Eugeniy Paltsevbb8155a2018-03-21 15:58:55 +0300379 return;
380
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300381 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
382
383 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
384 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
385 else
386 ctrl |= SLC_CTRL_IM;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300387
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300388 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300389
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300390 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
391 write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
392 else
393 write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300394
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300395 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
396 read_aux_reg(ARC_AUX_SLC_CTRL);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300397
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300398 /* Important to wait for flush to complete */
399 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300400}
401
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300402static void slc_upper_region_init(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300403{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300404 /*
Eugeniy Paltsev0627b3a2018-03-21 15:58:58 +0300405 * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
406 * only if PAE exists in current HW. So we had to check pae_exist
407 * before using them.
408 */
409 if (!pae_exists())
410 return;
411
412 /*
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300413 * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
414 * as we don't use PAE40.
415 */
416 write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
417 write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
418}
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300419
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300420static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
421{
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300422#ifdef CONFIG_ISA_ARCV2
423
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300424 unsigned int ctrl;
425 unsigned long end;
426
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300427 if (!slc_enabled())
Eugeniy Paltsevbb8155a2018-03-21 15:58:55 +0300428 return;
429
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300430 /*
431 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
432 * - b'000 (default) is Flush,
433 * - b'001 is Invalidate if CTRL.IM == 0
434 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
435 */
436 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
437
438 /* Don't rely on default value of IM bit */
439 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
440 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300441 else
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300442 ctrl |= SLC_CTRL_IM;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300443
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300444 if (op & OP_INV)
445 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
446 else
447 ctrl &= ~SLC_CTRL_RGN_OP_INV;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300448
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300449 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300450
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300451 /*
452 * Lower bits are ignored, no need to clip
453 * END needs to be setup before START (latter triggers the operation)
454 * END can't be same as START, so add (l2_line_sz - 1) to sz
455 */
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300456 end = paddr + sz + gd->arch.slc_line_sz - 1;
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300457
458 /*
459 * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
460 * are always == 0 as we don't use PAE40, so we only setup lower ones
461 * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
462 */
463 write_aux_reg(ARC_AUX_SLC_RGN_END, end);
464 write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
465
466 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
467 read_aux_reg(ARC_AUX_SLC_CTRL);
468
469 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300470
471#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300472}
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300473
474static void arc_ioc_setup(void)
475{
476 /* IOC Aperture start is equal to DDR start */
Tom Rinibb4dd962022-11-16 13:10:37 -0500477 unsigned int ap_base = CFG_SYS_SDRAM_BASE;
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300478 /* IOC Aperture size is equal to DDR size */
Tom Rinibb4dd962022-11-16 13:10:37 -0500479 long ap_size = CFG_SYS_SDRAM_SIZE;
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300480
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300481 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
482 if (!slc_exists())
483 panic("Try to enable IOC but SLC is not present");
484
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300485 if (!slc_enabled())
486 panic("Try to enable IOC but SLC is disabled");
487
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300488 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
489 if (!dcache_enabled())
490 panic("Try to enable IOC but L1 D$ is disabled");
491
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300492 if (!is_power_of_2(ap_size) || ap_size < 4096)
493 panic("IOC Aperture size must be power of 2 and bigger 4Kib");
494
Eugeniy Paltsev6305e2b2018-03-21 15:59:05 +0300495 /* IOC Aperture start must be aligned to the size of the aperture */
496 if (ap_base % ap_size != 0)
497 panic("IOC Aperture start must be aligned to the size of the aperture");
498
499 flush_n_invalidate_dcache_all();
500
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300501 /*
502 * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
503 * so setting 0x11 implies 512M, 0x12 implies 1G...
504 */
505 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
506 order_base_2(ap_size / 1024) - 2);
507
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300508 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
509 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
510 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
511}
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300512
Alexey Brodkindff5df22015-12-14 17:14:46 +0300513static void read_decode_cache_bcr_arcv2(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300514{
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300515#ifdef CONFIG_ISA_ARCV2
516
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300517 union bcr_slc_cfg slc_cfg;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300518
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300519 if (slc_exists()) {
Alexey Brodkindff5df22015-12-14 17:14:46 +0300520 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300521 gd->arch.slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300522
523 /*
524 * We don't support configuration where L1 I$ or L1 D$ is
525 * absent but SL$ exists. See [ NOTE 2 ] for more details.
526 */
527 if (!icache_exists() || !dcache_exists())
528 panic("Unsupported cache configuration: SLC exists but one of L1 caches is absent");
Alexey Brodkindff5df22015-12-14 17:14:46 +0300529 }
Alexey Brodkin4764d262015-12-14 17:15:13 +0300530
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300531#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300532}
533
Alexey Brodkindff5df22015-12-14 17:14:46 +0300534void read_decode_cache_bcr(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300535{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300536 int dc_line_sz = 0, ic_line_sz = 0;
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300537 union bcr_di_cache ibcr, dbcr;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300538
Alexey Brodkincf4f3232018-05-25 20:22:23 +0300539 /*
540 * We don't care much about I$ line length really as there're
541 * no per-line ops on I$ instead we only do full invalidation of it
542 * on occasion of relocation and right before jumping to the OS.
543 * Still we check insane config with zero-encoded line length in
544 * presense of version field in I$ BCR. Just in case.
545 */
Alexey Brodkindff5df22015-12-14 17:14:46 +0300546 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
547 if (ibcr.fields.ver) {
Alexey Brodkincf4f3232018-05-25 20:22:23 +0300548 ic_line_sz = 8 << ibcr.fields.line_len;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300549 if (!ic_line_sz)
550 panic("Instruction exists but line length is 0\n");
551 }
552
553 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300554 if (dbcr.fields.ver) {
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300555 gd->arch.l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300556 if (!dc_line_sz)
557 panic("Data cache exists but line length is 0\n");
558 }
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300559}
560
561void cache_init(void)
562{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300563 read_decode_cache_bcr();
564
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300565 if (is_isa_arcv2())
566 read_decode_cache_bcr_arcv2();
Alexey Brodkin4764d262015-12-14 17:15:13 +0300567
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300568 if (is_isa_arcv2() && ioc_enabled())
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300569 arc_ioc_setup();
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300570
Eugeniy Paltsev0627b3a2018-03-21 15:58:58 +0300571 if (is_isa_arcv2() && slc_exists())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300572 slc_upper_region_init();
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300573}
574
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400575int icache_status(void)
576{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300577 return icache_enabled();
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400578}
579
580void icache_enable(void)
581{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300582 if (icache_exists())
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300583 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
584 ~IC_CTRL_CACHE_DISABLE);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400585}
586
587void icache_disable(void)
588{
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300589 if (!icache_exists())
590 return;
591
592 __ic_entire_invalidate();
593
594 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
595 IC_CTRL_CACHE_DISABLE);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400596}
597
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300598/* IC supports only invalidation */
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300599static inlined_cachefunc void __ic_entire_invalidate(void)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400600{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300601 if (!icache_enabled())
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300602 return;
603
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400604 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300605 write_aux_reg(ARC_AUX_IC_IVIC, 1);
606 /*
607 * As per ARC HS databook (see chapter 5.3.3.2)
608 * it is required to add 3 NOPs after each write to IC_IVIC.
609 */
610 __builtin_arc_nop();
611 __builtin_arc_nop();
612 __builtin_arc_nop();
613 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
614}
615
616void invalidate_icache_all(void)
617{
618 __ic_entire_invalidate();
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300619
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300620 /*
621 * If SL$ is bypassed for data it is used only for instructions,
622 * so we need to invalidate it too.
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300623 */
624 if (is_isa_arcv2() && slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300625 __slc_entire_op(OP_INV);
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300626}
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400627
628int dcache_status(void)
629{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300630 return dcache_enabled();
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400631}
632
633void dcache_enable(void)
634{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300635 if (!dcache_exists())
Igor Guryanovbd889f92014-12-24 16:07:07 +0300636 return;
637
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400638 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
639 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
640}
641
642void dcache_disable(void)
643{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300644 if (!dcache_exists())
Igor Guryanovbd889f92014-12-24 16:07:07 +0300645 return;
646
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300647 __dc_entire_op(OP_FLUSH_N_INV);
648
649 /*
650 * As SLC will be bypassed for data after L1 D$ disable we need to
651 * flush it first before L1 D$ disable. Also we invalidate SLC to
652 * avoid any inconsistent data problems after enabling L1 D$ again with
653 * dcache_enable function.
654 */
655 if (is_isa_arcv2())
656 __slc_entire_op(OP_FLUSH_N_INV);
657
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400658 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
659 DC_CTRL_CACHE_DISABLE);
660}
661
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300662/* Common Helper for Line Operations on D-cache */
663static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
664 const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400665{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300666 unsigned int aux_cmd;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300667 int num_lines;
668
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300669 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
670 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400671
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300672 sz += paddr & ~CACHE_LINE_MASK;
673 paddr &= CACHE_LINE_MASK;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400674
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300675 num_lines = DIV_ROUND_UP(sz, gd->arch.l1_line_sz);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300676
677 while (num_lines-- > 0) {
Alexey Brodkin6da8cfc2015-02-03 13:58:12 +0300678#if (CONFIG_ARC_MMU_VER == 3)
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300679 write_aux_reg(ARC_AUX_DC_PTAG, paddr);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400680#endif
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300681 write_aux_reg(aux_cmd, paddr);
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300682 paddr += gd->arch.l1_line_sz;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300683 }
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400684}
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400685
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300686static inlined_cachefunc void __before_dc_op(const int op)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400687{
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300688 unsigned int ctrl;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400689
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300690 ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400691
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300692 /* IM bit implies flush-n-inv, instead of vanilla inv */
693 if (op == OP_INV)
694 ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
695 else
696 ctrl |= DC_CTRL_INV_MODE_FLUSH;
697
698 write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400699}
700
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300701static inlined_cachefunc void __after_dc_op(const int op)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400702{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300703 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300704 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400705}
706
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300707static inlined_cachefunc void __dc_entire_op(const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400708{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300709 int aux;
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300710
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300711 if (!dcache_enabled())
Eugeniy Paltsev7fd7e0a2018-03-21 15:58:53 +0300712 return;
713
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300714 __before_dc_op(cacheop);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300715
716 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
717 aux = ARC_AUX_DC_IVDC;
718 else
719 aux = ARC_AUX_DC_FLSH;
Alexey Brodkin35221a62015-03-27 12:47:29 +0300720
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300721 write_aux_reg(aux, 0x1);
722
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300723 __after_dc_op(cacheop);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400724}
725
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300726static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
727 const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400728{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300729 if (!dcache_enabled())
Eugeniy Paltsev7fd7e0a2018-03-21 15:58:53 +0300730 return;
731
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300732 __before_dc_op(cacheop);
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300733 __dcache_line_loop(paddr, sz, cacheop);
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300734 __after_dc_op(cacheop);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400735}
Alexey Brodkin275583e2015-03-30 13:36:04 +0300736
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300737void invalidate_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300738{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300739 if (start >= end)
740 return;
741
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300742 /*
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300743 * ARCv1 -> call __dc_line_op
744 * ARCv2 && L1 D$ disabled -> nothing
745 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
746 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300747 */
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300748 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkin4764d262015-12-14 17:15:13 +0300749 __dc_line_op(start, end - start, OP_INV);
750
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300751 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300752 __slc_rgn_op(start, end - start, OP_INV);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300753}
754
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300755void flush_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300756{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300757 if (start >= end)
758 return;
759
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300760 /*
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300761 * ARCv1 -> call __dc_line_op
762 * ARCv2 && L1 D$ disabled -> nothing
763 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
764 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300765 */
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300766 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkin4764d262015-12-14 17:15:13 +0300767 __dc_line_op(start, end - start, OP_FLUSH);
768
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300769 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300770 __slc_rgn_op(start, end - start, OP_FLUSH);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300771}
772
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300773void flush_cache(unsigned long start, unsigned long size)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300774{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300775 flush_dcache_range(start, start + size);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300776}
777
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300778/*
779 * As invalidate_dcache_all() is not used in generic U-Boot code and as we
780 * don't need it in arch/arc code alone (invalidate without flush) we implement
781 * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
782 * it's much safer. See [ NOTE 1 ] for more details.
783 */
784void flush_n_invalidate_dcache_all(void)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300785{
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300786 __dc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin4764d262015-12-14 17:15:13 +0300787
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300788 if (is_isa_arcv2() && !slc_data_bypass())
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300789 __slc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300790}
791
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300792void flush_dcache_all(void)
793{
Alexey Brodkin5f541692016-04-16 15:28:30 +0300794 __dc_entire_op(OP_FLUSH);
Alexey Brodkin4764d262015-12-14 17:15:13 +0300795
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300796 if (is_isa_arcv2() && !slc_data_bypass())
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300797 __slc_entire_op(OP_FLUSH);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300798}
Eugeniy Paltsev67fd56a2018-03-21 15:59:02 +0300799
800/*
801 * This is function to cleanup all caches (and therefore sync I/D caches) which
802 * can be used for cleanup before linux launch or to sync caches during
803 * relocation.
804 */
805void sync_n_cleanup_cache_all(void)
806{
807 __dc_entire_op(OP_FLUSH_N_INV);
808
809 /*
810 * If SL$ is bypassed for data it is used only for instructions,
811 * and we shouldn't flush it. So invalidate it instead of flush_n_inv.
812 */
813 if (is_isa_arcv2()) {
814 if (slc_data_bypass())
815 __slc_entire_op(OP_INV);
816 else
817 __slc_entire_op(OP_FLUSH_N_INV);
818 }
819
820 __ic_entire_invalidate();
821}