blob: 22e748868a74ef61974a38e56dfbb48b2fb5f9b8 [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>
Marek Vasut8b848712021-09-10 22:47:08 +020013#include <lmb.h>
Alexey Brodkin3a59d912014-02-04 12:56:14 +040014#include <asm/arcregs.h>
Eugeniy Paltsev589ac752018-03-21 15:58:52 +030015#include <asm/arc-bcr.h>
Alexey Brodkin6b95cca2015-02-03 13:58:13 +030016#include <asm/cache.h>
Alexey Brodkin3a59d912014-02-04 12:56:14 +040017
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +030018/*
19 * [ NOTE 1 ]:
20 * Data cache (L1 D$ or SL$) entire invalidate operation or data cache disable
21 * operation may result in unexpected behavior and data loss even if we flush
22 * data cache right before invalidation. That may happens if we store any context
23 * on stack (like we store BLINK register on stack before function call).
24 * BLINK register is the register where return address is automatically saved
25 * when we do function call with instructions like 'bl'.
26 *
27 * There is the real example:
28 * We may hang in the next code as we store any BLINK register on stack in
29 * invalidate_dcache_all() function.
30 *
31 * void flush_dcache_all() {
32 * __dc_entire_op(OP_FLUSH);
33 * // Other code //
34 * }
35 *
36 * void invalidate_dcache_all() {
37 * __dc_entire_op(OP_INV);
38 * // Other code //
39 * }
40 *
41 * void foo(void) {
42 * flush_dcache_all();
43 * invalidate_dcache_all();
44 * }
45 *
46 * Now let's see what really happens during that code execution:
47 *
48 * foo()
49 * |->> call flush_dcache_all
50 * [return address is saved to BLINK register]
51 * [push BLINK] (save to stack) ![point 1]
52 * |->> call __dc_entire_op(OP_FLUSH)
53 * [return address is saved to BLINK register]
54 * [flush L1 D$]
55 * return [jump to BLINK]
56 * <<------
57 * [other flush_dcache_all code]
58 * [pop BLINK] (get from stack)
59 * return [jump to BLINK]
60 * <<------
61 * |->> call invalidate_dcache_all
62 * [return address is saved to BLINK register]
63 * [push BLINK] (save to stack) ![point 2]
64 * |->> call __dc_entire_op(OP_FLUSH)
65 * [return address is saved to BLINK register]
66 * [invalidate L1 D$] ![point 3]
67 * // Oops!!!
68 * // We lose return address from invalidate_dcache_all function:
69 * // we save it to stack and invalidate L1 D$ after that!
70 * return [jump to BLINK]
71 * <<------
72 * [other invalidate_dcache_all code]
73 * [pop BLINK] (get from stack)
74 * // we don't have this data in L1 dcache as we invalidated it in [point 3]
75 * // so we get it from next memory level (for example DDR memory)
76 * // but in the memory we have value which we save in [point 1], which
77 * // is return address from flush_dcache_all function (instead of
78 * // address from current invalidate_dcache_all function which we
79 * // saved in [point 2] !)
80 * return [jump to BLINK]
81 * <<------
82 * // As BLINK points to invalidate_dcache_all, we call it again and
83 * // loop forever.
84 *
85 * Fortunately we may fix that by using flush & invalidation of D$ with a single
86 * one instruction (instead of flush and invalidation instructions pair) and
87 * enabling force function inline with '__attribute__((always_inline))' gcc
88 * attribute to avoid any function call (and BLINK store) between cache flush
89 * and disable.
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +030090 *
91 *
92 * [ NOTE 2 ]:
93 * As of today we only support the following cache configurations on ARC.
Eugeniy Paltsev67c34922020-03-11 15:00:43 +030094 * Other configurations may exist in HW but we don't support it in SW.
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +030095 * Configuration 1:
96 * ______________________
97 * | |
98 * | ARC CPU |
99 * |______________________|
100 * ___|___ ___|___
101 * | | | |
102 * | L1 I$ | | L1 D$ |
103 * |_______| |_______|
104 * on/off on/off
105 * ___|______________|____
106 * | |
107 * | main memory |
108 * |______________________|
109 *
110 * Configuration 2:
111 * ______________________
112 * | |
113 * | ARC CPU |
114 * |______________________|
115 * ___|___ ___|___
116 * | | | |
117 * | L1 I$ | | L1 D$ |
118 * |_______| |_______|
119 * on/off on/off
120 * ___|______________|____
121 * | |
122 * | L2 (SL$) |
123 * |______________________|
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300124 * always on (ARCv2, HS < 3.0)
125 * on/off (ARCv2, HS >= 3.0)
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300126 * ___|______________|____
127 * | |
128 * | main memory |
129 * |______________________|
130 *
131 * Configuration 3:
132 * ______________________
133 * | |
134 * | ARC CPU |
135 * |______________________|
136 * ___|___ ___|___
137 * | | | |
138 * | L1 I$ | | L1 D$ |
139 * |_______| |_______|
140 * on/off must be on
141 * ___|______________|____ _______
142 * | | | |
143 * | L2 (SL$) |-----| IOC |
144 * |______________________| |_______|
145 * always must be on on/off
146 * ___|______________|____
147 * | |
148 * | main memory |
149 * |______________________|
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300150 */
151
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300152DECLARE_GLOBAL_DATA_PTR;
153
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400154/* Bit values in IC_CTRL */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300155#define IC_CTRL_CACHE_DISABLE BIT(0)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400156
157/* Bit values in DC_CTRL */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300158#define DC_CTRL_CACHE_DISABLE BIT(0)
159#define DC_CTRL_INV_MODE_FLUSH BIT(6)
160#define DC_CTRL_FLUSH_STATUS BIT(8)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400161
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300162#define OP_INV BIT(0)
163#define OP_FLUSH BIT(1)
164#define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300165
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300166/* Bit val in SLC_CONTROL */
167#define SLC_CTRL_DIS 0x001
168#define SLC_CTRL_IM 0x040
169#define SLC_CTRL_BUSY 0x100
170#define SLC_CTRL_RGN_OP_INV 0x200
171
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300172#define CACHE_LINE_MASK (~(gd->arch.l1_line_sz - 1))
Alexey Brodkindff5df22015-12-14 17:14:46 +0300173
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300174/*
175 * We don't want to use '__always_inline' macro here as it can be redefined
176 * to simple 'inline' in some cases which breaks stuff. See [ NOTE 1 ] for more
177 * details about the reasons we need to use always_inline functions.
178 */
179#define inlined_cachefunc inline __attribute__((always_inline))
180
181static inlined_cachefunc void __ic_entire_invalidate(void);
182static inlined_cachefunc void __dc_entire_op(const int cacheop);
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300183static inlined_cachefunc void __slc_entire_op(const int op);
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300184static inlined_cachefunc bool ioc_enabled(void);
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300185
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300186static inline bool pae_exists(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300187{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300188 /* TODO: should we compare mmu version from BCR and from CONFIG? */
189#if (CONFIG_ARC_MMU_VER >= 4)
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300190 union bcr_mmu_4 mmu4;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300191
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300192 mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300193
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300194 if (mmu4.fields.pae)
195 return true;
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300196#endif /* (CONFIG_ARC_MMU_VER >= 4) */
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300197
198 return false;
199}
200
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300201static inlined_cachefunc bool icache_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300202{
203 union bcr_di_cache ibcr;
204
205 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
206 return !!ibcr.fields.ver;
207}
208
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300209static inlined_cachefunc bool icache_enabled(void)
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300210{
211 if (!icache_exists())
212 return false;
213
214 return !(read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE);
215}
216
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300217static inlined_cachefunc bool dcache_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300218{
219 union bcr_di_cache dbcr;
220
221 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
222 return !!dbcr.fields.ver;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300223}
224
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300225static inlined_cachefunc bool dcache_enabled(void)
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300226{
227 if (!dcache_exists())
228 return false;
229
230 return !(read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE);
231}
232
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300233static inlined_cachefunc bool slc_exists(void)
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300234{
235 if (is_isa_arcv2()) {
236 union bcr_generic sbcr;
237
238 sbcr.word = read_aux_reg(ARC_BCR_SLC);
239 return !!sbcr.fields.ver;
240 }
241
242 return false;
243}
244
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300245enum slc_dis_status {
246 ST_SLC_MISSING = 0,
247 ST_SLC_NO_DISABLE_CTRL,
248 ST_SLC_DISABLE_CTRL
249};
250
251/*
252 * ARCv1 -> ST_SLC_MISSING
253 * ARCv2 && SLC absent -> ST_SLC_MISSING
254 * ARCv2 && SLC exists && SLC version <= 2 -> ST_SLC_NO_DISABLE_CTRL
255 * ARCv2 && SLC exists && SLC version > 2 -> ST_SLC_DISABLE_CTRL
256 */
257static inlined_cachefunc enum slc_dis_status slc_disable_supported(void)
258{
259 if (is_isa_arcv2()) {
260 union bcr_generic sbcr;
261
262 sbcr.word = read_aux_reg(ARC_BCR_SLC);
263 if (sbcr.fields.ver == 0)
264 return ST_SLC_MISSING;
265 else if (sbcr.fields.ver <= 2)
266 return ST_SLC_NO_DISABLE_CTRL;
267 else
268 return ST_SLC_DISABLE_CTRL;
269 }
270
271 return ST_SLC_MISSING;
272}
273
274static inlined_cachefunc bool __slc_enabled(void)
275{
276 return !(read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_DIS);
277}
278
279static inlined_cachefunc void __slc_enable(void)
280{
281 unsigned int ctrl;
282
283 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
284 ctrl &= ~SLC_CTRL_DIS;
285 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
286}
287
288static inlined_cachefunc void __slc_disable(void)
289{
290 unsigned int ctrl;
291
292 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
293 ctrl |= SLC_CTRL_DIS;
294 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
295}
296
297static inlined_cachefunc bool slc_enabled(void)
298{
299 enum slc_dis_status slc_status = slc_disable_supported();
300
301 if (slc_status == ST_SLC_MISSING)
302 return false;
303 else if (slc_status == ST_SLC_NO_DISABLE_CTRL)
304 return true;
305 else
306 return __slc_enabled();
307}
308
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300309static inlined_cachefunc bool slc_data_bypass(void)
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300310{
311 /*
312 * If L1 data cache is disabled SL$ is bypassed and all load/store
313 * requests are sent directly to main memory.
314 */
315 return !dcache_enabled();
316}
317
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300318void slc_enable(void)
319{
320 if (slc_disable_supported() != ST_SLC_DISABLE_CTRL)
321 return;
322
323 if (__slc_enabled())
324 return;
325
326 __slc_enable();
327}
328
329/* TODO: warn if we are not able to disable SLC */
330void slc_disable(void)
331{
332 if (slc_disable_supported() != ST_SLC_DISABLE_CTRL)
333 return;
334
335 /* we don't support SLC disabling if we use IOC */
336 if (ioc_enabled())
337 return;
338
339 if (!__slc_enabled())
340 return;
341
342 /*
343 * We need to flush L1D$ to guarantee that we won't have any
344 * writeback operations during SLC disabling.
345 */
346 __dc_entire_op(OP_FLUSH);
347 __slc_entire_op(OP_FLUSH_N_INV);
348 __slc_disable();
349}
350
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300351static inlined_cachefunc bool ioc_exists(void)
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300352{
353 if (is_isa_arcv2()) {
354 union bcr_clust_cfg cbcr;
355
356 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
357 return cbcr.fields.c;
358 }
359
360 return false;
361}
362
Eugeniy Paltsev637e3542020-03-11 15:00:44 +0300363static inlined_cachefunc bool ioc_enabled(void)
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300364{
365 /*
366 * We check only CONFIG option instead of IOC HW state check as IOC
367 * must be disabled by default.
368 */
369 if (is_ioc_enabled())
370 return ioc_exists();
371
372 return false;
373}
374
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300375static inlined_cachefunc void __slc_entire_op(const int op)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300376{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300377 unsigned int ctrl;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300378
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300379 if (!slc_enabled())
Eugeniy Paltsevbb8155a2018-03-21 15:58:55 +0300380 return;
381
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300382 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
383
384 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
385 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
386 else
387 ctrl |= SLC_CTRL_IM;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300388
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300389 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300390
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300391 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
392 write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
393 else
394 write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300395
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300396 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
397 read_aux_reg(ARC_AUX_SLC_CTRL);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300398
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300399 /* Important to wait for flush to complete */
400 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300401}
402
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300403static void slc_upper_region_init(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300404{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300405 /*
Eugeniy Paltsev0627b3a2018-03-21 15:58:58 +0300406 * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
407 * only if PAE exists in current HW. So we had to check pae_exist
408 * before using them.
409 */
410 if (!pae_exists())
411 return;
412
413 /*
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300414 * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
415 * as we don't use PAE40.
416 */
417 write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
418 write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
419}
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300420
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300421static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
422{
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300423#ifdef CONFIG_ISA_ARCV2
424
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300425 unsigned int ctrl;
426 unsigned long end;
427
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300428 if (!slc_enabled())
Eugeniy Paltsevbb8155a2018-03-21 15:58:55 +0300429 return;
430
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300431 /*
432 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
433 * - b'000 (default) is Flush,
434 * - b'001 is Invalidate if CTRL.IM == 0
435 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
436 */
437 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
438
439 /* Don't rely on default value of IM bit */
440 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
441 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300442 else
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300443 ctrl |= SLC_CTRL_IM;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300444
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300445 if (op & OP_INV)
446 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
447 else
448 ctrl &= ~SLC_CTRL_RGN_OP_INV;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300449
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300450 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300451
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300452 /*
453 * Lower bits are ignored, no need to clip
454 * END needs to be setup before START (latter triggers the operation)
455 * END can't be same as START, so add (l2_line_sz - 1) to sz
456 */
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300457 end = paddr + sz + gd->arch.slc_line_sz - 1;
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300458
459 /*
460 * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
461 * are always == 0 as we don't use PAE40, so we only setup lower ones
462 * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
463 */
464 write_aux_reg(ARC_AUX_SLC_RGN_END, end);
465 write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
466
467 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
468 read_aux_reg(ARC_AUX_SLC_CTRL);
469
470 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300471
472#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300473}
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300474
475static void arc_ioc_setup(void)
476{
477 /* IOC Aperture start is equal to DDR start */
Tom Rinibb4dd962022-11-16 13:10:37 -0500478 unsigned int ap_base = CFG_SYS_SDRAM_BASE;
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300479 /* IOC Aperture size is equal to DDR size */
Tom Rinibb4dd962022-11-16 13:10:37 -0500480 long ap_size = CFG_SYS_SDRAM_SIZE;
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300481
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300482 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
483 if (!slc_exists())
484 panic("Try to enable IOC but SLC is not present");
485
Eugeniy Paltsev67c34922020-03-11 15:00:43 +0300486 if (!slc_enabled())
487 panic("Try to enable IOC but SLC is disabled");
488
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300489 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
490 if (!dcache_enabled())
491 panic("Try to enable IOC but L1 D$ is disabled");
492
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300493 if (!is_power_of_2(ap_size) || ap_size < 4096)
494 panic("IOC Aperture size must be power of 2 and bigger 4Kib");
495
Eugeniy Paltsev6305e2b2018-03-21 15:59:05 +0300496 /* IOC Aperture start must be aligned to the size of the aperture */
497 if (ap_base % ap_size != 0)
498 panic("IOC Aperture start must be aligned to the size of the aperture");
499
500 flush_n_invalidate_dcache_all();
501
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300502 /*
503 * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
504 * so setting 0x11 implies 512M, 0x12 implies 1G...
505 */
506 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
507 order_base_2(ap_size / 1024) - 2);
508
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300509 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
510 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
511 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
512}
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300513
Alexey Brodkindff5df22015-12-14 17:14:46 +0300514static void read_decode_cache_bcr_arcv2(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300515{
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300516#ifdef CONFIG_ISA_ARCV2
517
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300518 union bcr_slc_cfg slc_cfg;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300519
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300520 if (slc_exists()) {
Alexey Brodkindff5df22015-12-14 17:14:46 +0300521 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300522 gd->arch.slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
Eugeniy Paltseva22fcc42018-03-21 15:59:03 +0300523
524 /*
525 * We don't support configuration where L1 I$ or L1 D$ is
526 * absent but SL$ exists. See [ NOTE 2 ] for more details.
527 */
528 if (!icache_exists() || !dcache_exists())
529 panic("Unsupported cache configuration: SLC exists but one of L1 caches is absent");
Alexey Brodkindff5df22015-12-14 17:14:46 +0300530 }
Alexey Brodkin4764d262015-12-14 17:15:13 +0300531
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300532#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300533}
534
Alexey Brodkindff5df22015-12-14 17:14:46 +0300535void read_decode_cache_bcr(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300536{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300537 int dc_line_sz = 0, ic_line_sz = 0;
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300538 union bcr_di_cache ibcr, dbcr;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300539
Alexey Brodkincf4f3232018-05-25 20:22:23 +0300540 /*
541 * We don't care much about I$ line length really as there're
542 * no per-line ops on I$ instead we only do full invalidation of it
543 * on occasion of relocation and right before jumping to the OS.
544 * Still we check insane config with zero-encoded line length in
545 * presense of version field in I$ BCR. Just in case.
546 */
Alexey Brodkindff5df22015-12-14 17:14:46 +0300547 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
548 if (ibcr.fields.ver) {
Alexey Brodkincf4f3232018-05-25 20:22:23 +0300549 ic_line_sz = 8 << ibcr.fields.line_len;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300550 if (!ic_line_sz)
551 panic("Instruction exists but line length is 0\n");
552 }
553
554 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300555 if (dbcr.fields.ver) {
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300556 gd->arch.l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300557 if (!dc_line_sz)
558 panic("Data cache exists but line length is 0\n");
559 }
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300560}
561
562void cache_init(void)
563{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300564 read_decode_cache_bcr();
565
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300566 if (is_isa_arcv2())
567 read_decode_cache_bcr_arcv2();
Alexey Brodkin4764d262015-12-14 17:15:13 +0300568
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300569 if (is_isa_arcv2() && ioc_enabled())
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300570 arc_ioc_setup();
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300571
Eugeniy Paltsev0627b3a2018-03-21 15:58:58 +0300572 if (is_isa_arcv2() && slc_exists())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300573 slc_upper_region_init();
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300574}
575
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400576int icache_status(void)
577{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300578 return icache_enabled();
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400579}
580
581void icache_enable(void)
582{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300583 if (icache_exists())
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300584 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
585 ~IC_CTRL_CACHE_DISABLE);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400586}
587
588void icache_disable(void)
589{
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300590 if (!icache_exists())
591 return;
592
593 __ic_entire_invalidate();
594
595 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
596 IC_CTRL_CACHE_DISABLE);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400597}
598
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300599/* IC supports only invalidation */
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300600static inlined_cachefunc void __ic_entire_invalidate(void)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400601{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300602 if (!icache_enabled())
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300603 return;
604
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400605 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300606 write_aux_reg(ARC_AUX_IC_IVIC, 1);
607 /*
608 * As per ARC HS databook (see chapter 5.3.3.2)
609 * it is required to add 3 NOPs after each write to IC_IVIC.
610 */
611 __builtin_arc_nop();
612 __builtin_arc_nop();
613 __builtin_arc_nop();
614 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
615}
616
617void invalidate_icache_all(void)
618{
619 __ic_entire_invalidate();
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300620
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300621 /*
622 * If SL$ is bypassed for data it is used only for instructions,
623 * so we need to invalidate it too.
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300624 */
625 if (is_isa_arcv2() && slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300626 __slc_entire_op(OP_INV);
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300627}
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400628
629int dcache_status(void)
630{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300631 return dcache_enabled();
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400632}
633
634void dcache_enable(void)
635{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300636 if (!dcache_exists())
Igor Guryanovbd889f92014-12-24 16:07:07 +0300637 return;
638
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400639 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
640 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
641}
642
643void dcache_disable(void)
644{
Eugeniy Paltsev6d5147b2018-03-21 15:58:56 +0300645 if (!dcache_exists())
Igor Guryanovbd889f92014-12-24 16:07:07 +0300646 return;
647
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300648 __dc_entire_op(OP_FLUSH_N_INV);
649
650 /*
651 * As SLC will be bypassed for data after L1 D$ disable we need to
652 * flush it first before L1 D$ disable. Also we invalidate SLC to
653 * avoid any inconsistent data problems after enabling L1 D$ again with
654 * dcache_enable function.
655 */
656 if (is_isa_arcv2())
657 __slc_entire_op(OP_FLUSH_N_INV);
658
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400659 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
660 DC_CTRL_CACHE_DISABLE);
661}
662
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300663/* Common Helper for Line Operations on D-cache */
664static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
665 const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400666{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300667 unsigned int aux_cmd;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300668 int num_lines;
669
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300670 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
671 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400672
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300673 sz += paddr & ~CACHE_LINE_MASK;
674 paddr &= CACHE_LINE_MASK;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400675
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300676 num_lines = DIV_ROUND_UP(sz, gd->arch.l1_line_sz);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300677
678 while (num_lines-- > 0) {
Alexey Brodkin6da8cfc2015-02-03 13:58:12 +0300679#if (CONFIG_ARC_MMU_VER == 3)
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300680 write_aux_reg(ARC_AUX_DC_PTAG, paddr);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400681#endif
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300682 write_aux_reg(aux_cmd, paddr);
Eugeniy Paltsevf3de8d62018-03-21 15:58:57 +0300683 paddr += gd->arch.l1_line_sz;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300684 }
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400685}
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400686
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300687static inlined_cachefunc void __before_dc_op(const int op)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400688{
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300689 unsigned int ctrl;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400690
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300691 ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400692
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300693 /* IM bit implies flush-n-inv, instead of vanilla inv */
694 if (op == OP_INV)
695 ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
696 else
697 ctrl |= DC_CTRL_INV_MODE_FLUSH;
698
699 write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400700}
701
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300702static inlined_cachefunc void __after_dc_op(const int op)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400703{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300704 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300705 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400706}
707
Eugeniy Paltsevdddc8862018-03-21 15:59:04 +0300708static inlined_cachefunc void __dc_entire_op(const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400709{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300710 int aux;
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300711
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300712 if (!dcache_enabled())
Eugeniy Paltsev7fd7e0a2018-03-21 15:58:53 +0300713 return;
714
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300715 __before_dc_op(cacheop);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300716
717 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
718 aux = ARC_AUX_DC_IVDC;
719 else
720 aux = ARC_AUX_DC_FLSH;
Alexey Brodkin35221a62015-03-27 12:47:29 +0300721
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300722 write_aux_reg(aux, 0x1);
723
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300724 __after_dc_op(cacheop);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400725}
726
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300727static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
728 const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400729{
Eugeniy Paltsev0dec96c2018-03-21 15:59:00 +0300730 if (!dcache_enabled())
Eugeniy Paltsev7fd7e0a2018-03-21 15:58:53 +0300731 return;
732
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300733 __before_dc_op(cacheop);
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300734 __dcache_line_loop(paddr, sz, cacheop);
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300735 __after_dc_op(cacheop);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400736}
Alexey Brodkin275583e2015-03-30 13:36:04 +0300737
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300738void invalidate_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300739{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300740 if (start >= end)
741 return;
742
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300743 /*
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300744 * ARCv1 -> call __dc_line_op
745 * ARCv2 && L1 D$ disabled -> nothing
746 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
747 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300748 */
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300749 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkin4764d262015-12-14 17:15:13 +0300750 __dc_line_op(start, end - start, OP_INV);
751
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300752 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300753 __slc_rgn_op(start, end - start, OP_INV);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300754}
755
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300756void flush_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300757{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300758 if (start >= end)
759 return;
760
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300761 /*
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300762 * ARCv1 -> call __dc_line_op
763 * ARCv2 && L1 D$ disabled -> nothing
764 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
765 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300766 */
Eugeniy Paltsev04011ab2018-03-21 15:58:59 +0300767 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkin4764d262015-12-14 17:15:13 +0300768 __dc_line_op(start, end - start, OP_FLUSH);
769
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300770 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300771 __slc_rgn_op(start, end - start, OP_FLUSH);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300772}
773
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300774void flush_cache(unsigned long start, unsigned long size)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300775{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300776 flush_dcache_range(start, start + size);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300777}
778
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300779/*
780 * As invalidate_dcache_all() is not used in generic U-Boot code and as we
781 * don't need it in arch/arc code alone (invalidate without flush) we implement
782 * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
783 * it's much safer. See [ NOTE 1 ] for more details.
784 */
785void flush_n_invalidate_dcache_all(void)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300786{
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300787 __dc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin4764d262015-12-14 17:15:13 +0300788
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300789 if (is_isa_arcv2() && !slc_data_bypass())
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300790 __slc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300791}
792
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300793void flush_dcache_all(void)
794{
Alexey Brodkin5f541692016-04-16 15:28:30 +0300795 __dc_entire_op(OP_FLUSH);
Alexey Brodkin4764d262015-12-14 17:15:13 +0300796
Eugeniy Paltsev767675f2018-03-21 15:59:01 +0300797 if (is_isa_arcv2() && !slc_data_bypass())
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300798 __slc_entire_op(OP_FLUSH);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300799}
Eugeniy Paltsev67fd56a2018-03-21 15:59:02 +0300800
801/*
802 * This is function to cleanup all caches (and therefore sync I/D caches) which
803 * can be used for cleanup before linux launch or to sync caches during
804 * relocation.
805 */
806void sync_n_cleanup_cache_all(void)
807{
808 __dc_entire_op(OP_FLUSH_N_INV);
809
810 /*
811 * If SL$ is bypassed for data it is used only for instructions,
812 * and we shouldn't flush it. So invalidate it instead of flush_n_inv.
813 */
814 if (is_isa_arcv2()) {
815 if (slc_data_bypass())
816 __slc_entire_op(OP_INV);
817 else
818 __slc_entire_op(OP_FLUSH_N_INV);
819 }
820
821 __ic_entire_invalidate();
822}
Marek Vasut8b848712021-09-10 22:47:08 +0200823
824static ulong get_sp(void)
825{
826 ulong ret;
827
828 asm("mov %0, sp" : "=r"(ret) : );
829 return ret;
830}
831
832void arch_lmb_reserve(struct lmb *lmb)
833{
Marek Vasutd0dd68f2021-09-10 22:47:10 +0200834 arch_lmb_reserve_generic(lmb, get_sp(), gd->ram_top, 4096);
Marek Vasut8b848712021-09-10 22:47:08 +0200835}