blob: 8a1d67870a82d629b7e51fda124ee632ef917145 [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>
Alexey Brodkindff5df22015-12-14 17:14:46 +03007#include <common.h>
Simon Glass1d91ba72019-11-14 12:57:37 -07008#include <cpu_func.h>
Alexey Brodkin9f916ee2015-05-18 16:56:26 +03009#include <linux/compiler.h>
10#include <linux/kernel.h>
Alexey Brodkin982f6bf2017-06-26 11:46:47 +030011#include <linux/log2.h>
Alexey Brodkin3a59d912014-02-04 12:56:14 +040012#include <asm/arcregs.h>
Eugeniy Paltsev589ac752018-03-21 15:58:52 +030013#include <asm/arc-bcr.h>
Alexey Brodkin6b95cca2015-02-03 13:58:13 +030014#include <asm/cache.h>
Alexey Brodkin3a59d912014-02-04 12:56:14 +040015
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +030016/*
17 * [ NOTE 1 ]:
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'.
24 *
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.
28 *
29 * void flush_dcache_all() {
30 * __dc_entire_op(OP_FLUSH);
31 * // Other code //
32 * }
33 *
34 * void invalidate_dcache_all() {
35 * __dc_entire_op(OP_INV);
36 * // Other code //
37 * }
38 *
39 * void foo(void) {
40 * flush_dcache_all();
41 * invalidate_dcache_all();
42 * }
43 *
44 * Now let's see what really happens during that code execution:
45 *
46 * foo()
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]
52 * [flush L1 D$]
53 * return [jump to BLINK]
54 * <<------
55 * [other flush_dcache_all code]
56 * [pop BLINK] (get from stack)
57 * return [jump to BLINK]
58 * <<------
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]
65 * // Oops!!!
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]
69 * <<------
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]
79 * <<------
80 * // As BLINK points to invalidate_dcache_all, we call it again and
81 * // loop forever.
82 *
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
87 * and disable.
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +030088 *
89 *
90 * [ NOTE 2 ]:
91 * As of today we only support the following cache configurations on ARC.
Eugeniy Paltsev67c34922020-03-11 15:00:43 +030092 * Other configurations may exist in HW but we don't support it in SW.
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +030093 * 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 * |______________________|
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300122 * always on (ARCv2, HS < 3.0)
123 * on/off (ARCv2, HS >= 3.0)
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300124 * ___|______________|____
125 * | |
126 * | main memory |
127 * |______________________|
128 *
129 * Configuration 3:
130 * ______________________
131 * | |
132 * | ARC CPU |
133 * |______________________|
134 * ___|___ ___|___
135 * | | | |
136 * | L1 I$ | | L1 D$ |
137 * |_______| |_______|
138 * on/off must be on
139 * ___|______________|____ _______
140 * | | | |
141 * | L2 (SL$) |-----| IOC |
142 * |______________________| |_______|
143 * always must be on on/off
144 * ___|______________|____
145 * | |
146 * | main memory |
147 * |______________________|
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300148 */
149
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300150DECLARE_GLOBAL_DATA_PTR;
151
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400152/* Bit values in IC_CTRL */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300153#define IC_CTRL_CACHE_DISABLE BIT(0)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400154
155/* Bit values in DC_CTRL */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300156#define DC_CTRL_CACHE_DISABLE BIT(0)
157#define DC_CTRL_INV_MODE_FLUSH BIT(6)
158#define DC_CTRL_FLUSH_STATUS BIT(8)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400159
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300160#define OP_INV BIT(0)
161#define OP_FLUSH BIT(1)
162#define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300163
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300164/* Bit val in SLC_CONTROL */
165#define SLC_CTRL_DIS 0x001
166#define SLC_CTRL_IM 0x040
167#define SLC_CTRL_BUSY 0x100
168#define SLC_CTRL_RGN_OP_INV 0x200
169
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300170#define CACHE_LINE_MASK (~(gd->arch.l1_line_sz - 1))
Alexey Brodkindff5df22015-12-14 17:14:46 +0300171
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300172/*
173 * We don't want to use '__always_inline' macro here as it can be redefined
174 * to simple 'inline' in some cases which breaks stuff. See [ NOTE 1 ] for more
175 * details about the reasons we need to use always_inline functions.
176 */
177#define inlined_cachefunc inline __attribute__((always_inline))
178
179static inlined_cachefunc void __ic_entire_invalidate(void);
180static inlined_cachefunc void __dc_entire_op(const int cacheop);
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300181static inlined_cachefunc void __slc_entire_op(const int op);
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300182static inlined_cachefunc bool ioc_enabled(void);
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300183
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300184static inline bool pae_exists(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300185{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300186 /* TODO: should we compare mmu version from BCR and from CONFIG? */
187#if (CONFIG_ARC_MMU_VER >= 4)
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300188 union bcr_mmu_4 mmu4;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300189
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300190 mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300191
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300192 if (mmu4.fields.pae)
193 return true;
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300194#endif /* (CONFIG_ARC_MMU_VER >= 4) */
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300195
196 return false;
197}
198
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300199static inlined_cachefunc bool icache_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300200{
201 union bcr_di_cache ibcr;
202
203 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
204 return !!ibcr.fields.ver;
205}
206
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300207static inlined_cachefunc bool icache_enabled(void)
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300208{
209 if (!icache_exists())
210 return false;
211
212 return !(read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE);
213}
214
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300215static inlined_cachefunc bool dcache_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300216{
217 union bcr_di_cache dbcr;
218
219 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
220 return !!dbcr.fields.ver;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300221}
222
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300223static inlined_cachefunc bool dcache_enabled(void)
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300224{
225 if (!dcache_exists())
226 return false;
227
228 return !(read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE);
229}
230
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300231static inlined_cachefunc bool slc_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300232{
233 if (is_isa_arcv2()) {
234 union bcr_generic sbcr;
235
236 sbcr.word = read_aux_reg(ARC_BCR_SLC);
237 return !!sbcr.fields.ver;
238 }
239
240 return false;
241}
242
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300243enum slc_dis_status {
244 ST_SLC_MISSING = 0,
245 ST_SLC_NO_DISABLE_CTRL,
246 ST_SLC_DISABLE_CTRL
247};
248
249/*
250 * ARCv1 -> ST_SLC_MISSING
251 * ARCv2 && SLC absent -> ST_SLC_MISSING
252 * ARCv2 && SLC exists && SLC version <= 2 -> ST_SLC_NO_DISABLE_CTRL
253 * ARCv2 && SLC exists && SLC version > 2 -> ST_SLC_DISABLE_CTRL
254 */
255static inlined_cachefunc enum slc_dis_status slc_disable_supported(void)
256{
257 if (is_isa_arcv2()) {
258 union bcr_generic sbcr;
259
260 sbcr.word = read_aux_reg(ARC_BCR_SLC);
261 if (sbcr.fields.ver == 0)
262 return ST_SLC_MISSING;
263 else if (sbcr.fields.ver <= 2)
264 return ST_SLC_NO_DISABLE_CTRL;
265 else
266 return ST_SLC_DISABLE_CTRL;
267 }
268
269 return ST_SLC_MISSING;
270}
271
272static inlined_cachefunc bool __slc_enabled(void)
273{
274 return !(read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_DIS);
275}
276
277static inlined_cachefunc void __slc_enable(void)
278{
279 unsigned int ctrl;
280
281 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
282 ctrl &= ~SLC_CTRL_DIS;
283 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
284}
285
286static inlined_cachefunc void __slc_disable(void)
287{
288 unsigned int ctrl;
289
290 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
291 ctrl |= SLC_CTRL_DIS;
292 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
293}
294
295static inlined_cachefunc bool slc_enabled(void)
296{
297 enum slc_dis_status slc_status = slc_disable_supported();
298
299 if (slc_status == ST_SLC_MISSING)
300 return false;
301 else if (slc_status == ST_SLC_NO_DISABLE_CTRL)
302 return true;
303 else
304 return __slc_enabled();
305}
306
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300307static inlined_cachefunc bool slc_data_bypass(void)
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300308{
309 /*
310 * If L1 data cache is disabled SL$ is bypassed and all load/store
311 * requests are sent directly to main memory.
312 */
313 return !dcache_enabled();
314}
315
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300316void slc_enable(void)
317{
318 if (slc_disable_supported() != ST_SLC_DISABLE_CTRL)
319 return;
320
321 if (__slc_enabled())
322 return;
323
324 __slc_enable();
325}
326
327/* TODO: warn if we are not able to disable SLC */
328void slc_disable(void)
329{
330 if (slc_disable_supported() != ST_SLC_DISABLE_CTRL)
331 return;
332
333 /* we don't support SLC disabling if we use IOC */
334 if (ioc_enabled())
335 return;
336
337 if (!__slc_enabled())
338 return;
339
340 /*
341 * We need to flush L1D$ to guarantee that we won't have any
342 * writeback operations during SLC disabling.
343 */
344 __dc_entire_op(OP_FLUSH);
345 __slc_entire_op(OP_FLUSH_N_INV);
346 __slc_disable();
347}
348
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300349static inlined_cachefunc bool ioc_exists(void)
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300350{
351 if (is_isa_arcv2()) {
352 union bcr_clust_cfg cbcr;
353
354 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
355 return cbcr.fields.c;
356 }
357
358 return false;
359}
360
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300361static inlined_cachefunc bool ioc_enabled(void)
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300362{
363 /*
364 * We check only CONFIG option instead of IOC HW state check as IOC
365 * must be disabled by default.
366 */
367 if (is_ioc_enabled())
368 return ioc_exists();
369
370 return false;
371}
372
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300373static inlined_cachefunc void __slc_entire_op(const int op)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300374{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300375 unsigned int ctrl;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300376
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300377 if (!slc_enabled())
Eugeniy Paltsevbb8155a2018-03-21 15:58:55 +0300378 return;
379
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300380 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
381
382 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
383 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
384 else
385 ctrl |= SLC_CTRL_IM;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300386
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300387 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300388
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300389 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
390 write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
391 else
392 write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300393
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300394 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
395 read_aux_reg(ARC_AUX_SLC_CTRL);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300396
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300397 /* Important to wait for flush to complete */
398 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300399}
400
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300401static void slc_upper_region_init(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300402{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300403 /*
Eugeniy Paltsev0627b3a2018-03-21 15:58:58 +0300404 * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
405 * only if PAE exists in current HW. So we had to check pae_exist
406 * before using them.
407 */
408 if (!pae_exists())
409 return;
410
411 /*
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300412 * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
413 * as we don't use PAE40.
414 */
415 write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
416 write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
417}
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300418
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300419static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
420{
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300421#ifdef CONFIG_ISA_ARCV2
422
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300423 unsigned int ctrl;
424 unsigned long end;
425
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300426 if (!slc_enabled())
Eugeniy Paltsevbb8155a2018-03-21 15:58:55 +0300427 return;
428
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300429 /*
430 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
431 * - b'000 (default) is Flush,
432 * - b'001 is Invalidate if CTRL.IM == 0
433 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
434 */
435 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
436
437 /* Don't rely on default value of IM bit */
438 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
439 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300440 else
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300441 ctrl |= SLC_CTRL_IM;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300442
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300443 if (op & OP_INV)
444 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
445 else
446 ctrl &= ~SLC_CTRL_RGN_OP_INV;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300447
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300448 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300449
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300450 /*
451 * Lower bits are ignored, no need to clip
452 * END needs to be setup before START (latter triggers the operation)
453 * END can't be same as START, so add (l2_line_sz - 1) to sz
454 */
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300455 end = paddr + sz + gd->arch.slc_line_sz - 1;
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300456
457 /*
458 * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
459 * are always == 0 as we don't use PAE40, so we only setup lower ones
460 * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
461 */
462 write_aux_reg(ARC_AUX_SLC_RGN_END, end);
463 write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
464
465 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
466 read_aux_reg(ARC_AUX_SLC_CTRL);
467
468 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300469
470#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300471}
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300472
473static void arc_ioc_setup(void)
474{
475 /* IOC Aperture start is equal to DDR start */
476 unsigned int ap_base = CONFIG_SYS_SDRAM_BASE;
477 /* IOC Aperture size is equal to DDR size */
478 long ap_size = CONFIG_SYS_SDRAM_SIZE;
479
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300480 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
481 if (!slc_exists())
482 panic("Try to enable IOC but SLC is not present");
483
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300484 if (!slc_enabled())
485 panic("Try to enable IOC but SLC is disabled");
486
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300487 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
488 if (!dcache_enabled())
489 panic("Try to enable IOC but L1 D$ is disabled");
490
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300491 if (!is_power_of_2(ap_size) || ap_size < 4096)
492 panic("IOC Aperture size must be power of 2 and bigger 4Kib");
493
Eugeniy Paltsev6305e2b2018-03-21 15:59:05 +0300494 /* IOC Aperture start must be aligned to the size of the aperture */
495 if (ap_base % ap_size != 0)
496 panic("IOC Aperture start must be aligned to the size of the aperture");
497
498 flush_n_invalidate_dcache_all();
499
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300500 /*
501 * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
502 * so setting 0x11 implies 512M, 0x12 implies 1G...
503 */
504 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
505 order_base_2(ap_size / 1024) - 2);
506
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300507 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
508 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
509 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
510}
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300511
Alexey Brodkindff5df22015-12-14 17:14:46 +0300512static void read_decode_cache_bcr_arcv2(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300513{
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300514#ifdef CONFIG_ISA_ARCV2
515
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300516 union bcr_slc_cfg slc_cfg;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300517
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300518 if (slc_exists()) {
Alexey Brodkindff5df22015-12-14 17:14:46 +0300519 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300520 gd->arch.slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300521
522 /*
523 * We don't support configuration where L1 I$ or L1 D$ is
524 * absent but SL$ exists. See [ NOTE 2 ] for more details.
525 */
526 if (!icache_exists() || !dcache_exists())
527 panic("Unsupported cache configuration: SLC exists but one of L1 caches is absent");
Alexey Brodkindff5df22015-12-14 17:14:46 +0300528 }
Alexey Brodkin4764d262015-12-14 17:15:13 +0300529
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300530#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300531}
532
Alexey Brodkindff5df22015-12-14 17:14:46 +0300533void read_decode_cache_bcr(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300534{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300535 int dc_line_sz = 0, ic_line_sz = 0;
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300536 union bcr_di_cache ibcr, dbcr;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300537
Alexey Brodkincf4f3232018-05-25 20:22:23 +0300538 /*
539 * We don't care much about I$ line length really as there're
540 * no per-line ops on I$ instead we only do full invalidation of it
541 * on occasion of relocation and right before jumping to the OS.
542 * Still we check insane config with zero-encoded line length in
543 * presense of version field in I$ BCR. Just in case.
544 */
Alexey Brodkindff5df22015-12-14 17:14:46 +0300545 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
546 if (ibcr.fields.ver) {
Alexey Brodkincf4f3232018-05-25 20:22:23 +0300547 ic_line_sz = 8 << ibcr.fields.line_len;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300548 if (!ic_line_sz)
549 panic("Instruction exists but line length is 0\n");
550 }
551
552 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300553 if (dbcr.fields.ver) {
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300554 gd->arch.l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300555 if (!dc_line_sz)
556 panic("Data cache exists but line length is 0\n");
557 }
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300558}
559
560void cache_init(void)
561{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300562 read_decode_cache_bcr();
563
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300564 if (is_isa_arcv2())
565 read_decode_cache_bcr_arcv2();
Alexey Brodkin4764d262015-12-14 17:15:13 +0300566
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300567 if (is_isa_arcv2() && ioc_enabled())
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300568 arc_ioc_setup();
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300569
Eugeniy Paltsev0627b3a2018-03-21 15:58:58 +0300570 if (is_isa_arcv2() && slc_exists())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300571 slc_upper_region_init();
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300572}
573
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400574int icache_status(void)
575{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300576 return icache_enabled();
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400577}
578
579void icache_enable(void)
580{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300581 if (icache_exists())
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300582 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
583 ~IC_CTRL_CACHE_DISABLE);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400584}
585
586void icache_disable(void)
587{
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300588 if (!icache_exists())
589 return;
590
591 __ic_entire_invalidate();
592
593 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
594 IC_CTRL_CACHE_DISABLE);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400595}
596
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300597/* IC supports only invalidation */
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300598static inlined_cachefunc void __ic_entire_invalidate(void)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400599{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300600 if (!icache_enabled())
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300601 return;
602
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400603 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300604 write_aux_reg(ARC_AUX_IC_IVIC, 1);
605 /*
606 * As per ARC HS databook (see chapter 5.3.3.2)
607 * it is required to add 3 NOPs after each write to IC_IVIC.
608 */
609 __builtin_arc_nop();
610 __builtin_arc_nop();
611 __builtin_arc_nop();
612 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
613}
614
615void invalidate_icache_all(void)
616{
617 __ic_entire_invalidate();
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300618
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300619 /*
620 * If SL$ is bypassed for data it is used only for instructions,
621 * so we need to invalidate it too.
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300622 */
623 if (is_isa_arcv2() && slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300624 __slc_entire_op(OP_INV);
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300625}
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400626
627int dcache_status(void)
628{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300629 return dcache_enabled();
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400630}
631
632void dcache_enable(void)
633{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300634 if (!dcache_exists())
Igor Guryanovbd889f92014-12-24 16:07:07 +0300635 return;
636
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400637 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
638 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
639}
640
641void dcache_disable(void)
642{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300643 if (!dcache_exists())
Igor Guryanovbd889f92014-12-24 16:07:07 +0300644 return;
645
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300646 __dc_entire_op(OP_FLUSH_N_INV);
647
648 /*
649 * As SLC will be bypassed for data after L1 D$ disable we need to
650 * flush it first before L1 D$ disable. Also we invalidate SLC to
651 * avoid any inconsistent data problems after enabling L1 D$ again with
652 * dcache_enable function.
653 */
654 if (is_isa_arcv2())
655 __slc_entire_op(OP_FLUSH_N_INV);
656
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400657 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
658 DC_CTRL_CACHE_DISABLE);
659}
660
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300661/* Common Helper for Line Operations on D-cache */
662static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
663 const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400664{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300665 unsigned int aux_cmd;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300666 int num_lines;
667
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300668 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
669 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400670
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300671 sz += paddr & ~CACHE_LINE_MASK;
672 paddr &= CACHE_LINE_MASK;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400673
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300674 num_lines = DIV_ROUND_UP(sz, gd->arch.l1_line_sz);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300675
676 while (num_lines-- > 0) {
Alexey Brodkin6da8cfc2015-02-03 13:58:12 +0300677#if (CONFIG_ARC_MMU_VER == 3)
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300678 write_aux_reg(ARC_AUX_DC_PTAG, paddr);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400679#endif
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300680 write_aux_reg(aux_cmd, paddr);
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300681 paddr += gd->arch.l1_line_sz;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300682 }
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400683}
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400684
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300685static inlined_cachefunc void __before_dc_op(const int op)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400686{
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300687 unsigned int ctrl;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400688
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300689 ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400690
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300691 /* IM bit implies flush-n-inv, instead of vanilla inv */
692 if (op == OP_INV)
693 ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
694 else
695 ctrl |= DC_CTRL_INV_MODE_FLUSH;
696
697 write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400698}
699
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300700static inlined_cachefunc void __after_dc_op(const int op)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400701{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300702 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300703 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400704}
705
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300706static inlined_cachefunc void __dc_entire_op(const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400707{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300708 int aux;
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300709
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300710 if (!dcache_enabled())
Eugeniy Paltsev7fd7e0a2018-03-21 15:58:53 +0300711 return;
712
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300713 __before_dc_op(cacheop);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300714
715 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
716 aux = ARC_AUX_DC_IVDC;
717 else
718 aux = ARC_AUX_DC_FLSH;
Alexey Brodkin35221a62015-03-27 12:47:29 +0300719
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300720 write_aux_reg(aux, 0x1);
721
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300722 __after_dc_op(cacheop);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400723}
724
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300725static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
726 const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400727{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300728 if (!dcache_enabled())
Eugeniy Paltsev7fd7e0a2018-03-21 15:58:53 +0300729 return;
730
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300731 __before_dc_op(cacheop);
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300732 __dcache_line_loop(paddr, sz, cacheop);
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300733 __after_dc_op(cacheop);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400734}
Alexey Brodkin275583e2015-03-30 13:36:04 +0300735
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300736void invalidate_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300737{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300738 if (start >= end)
739 return;
740
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300741 /*
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300742 * ARCv1 -> call __dc_line_op
743 * ARCv2 && L1 D$ disabled -> nothing
744 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
745 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300746 */
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300747 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkin4764d262015-12-14 17:15:13 +0300748 __dc_line_op(start, end - start, OP_INV);
749
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300750 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300751 __slc_rgn_op(start, end - start, OP_INV);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300752}
753
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300754void flush_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300755{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300756 if (start >= end)
757 return;
758
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300759 /*
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300760 * ARCv1 -> call __dc_line_op
761 * ARCv2 && L1 D$ disabled -> nothing
762 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
763 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300764 */
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300765 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkin4764d262015-12-14 17:15:13 +0300766 __dc_line_op(start, end - start, OP_FLUSH);
767
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300768 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300769 __slc_rgn_op(start, end - start, OP_FLUSH);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300770}
771
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300772void flush_cache(unsigned long start, unsigned long size)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300773{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300774 flush_dcache_range(start, start + size);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300775}
776
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300777/*
778 * As invalidate_dcache_all() is not used in generic U-Boot code and as we
779 * don't need it in arch/arc code alone (invalidate without flush) we implement
780 * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
781 * it's much safer. See [ NOTE 1 ] for more details.
782 */
783void flush_n_invalidate_dcache_all(void)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300784{
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300785 __dc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin4764d262015-12-14 17:15:13 +0300786
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300787 if (is_isa_arcv2() && !slc_data_bypass())
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300788 __slc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300789}
790
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300791void flush_dcache_all(void)
792{
Alexey Brodkin5f541692016-04-16 15:28:30 +0300793 __dc_entire_op(OP_FLUSH);
Alexey Brodkin4764d262015-12-14 17:15:13 +0300794
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300795 if (is_isa_arcv2() && !slc_data_bypass())
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300796 __slc_entire_op(OP_FLUSH);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300797}
Eugeniy Paltsev67fd56a2018-03-21 15:59:02 +0300798
799/*
800 * This is function to cleanup all caches (and therefore sync I/D caches) which
801 * can be used for cleanup before linux launch or to sync caches during
802 * relocation.
803 */
804void sync_n_cleanup_cache_all(void)
805{
806 __dc_entire_op(OP_FLUSH_N_INV);
807
808 /*
809 * If SL$ is bypassed for data it is used only for instructions,
810 * and we shouldn't flush it. So invalidate it instead of flush_n_inv.
811 */
812 if (is_isa_arcv2()) {
813 if (slc_data_bypass())
814 __slc_entire_op(OP_INV);
815 else
816 __slc_entire_op(OP_FLUSH_N_INV);
817 }
818
819 __ic_entire_invalidate();
820}