blob: 4ba180482c5e68d060d3f256adc8279bc931df10 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Alexey Brodkin2f16ac92014-02-04 12:56:14 +04002/*
3 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
Alexey Brodkin2f16ac92014-02-04 12:56:14 +04004 */
5
6#include <config.h>
Alexey Brodkin379b3282015-12-14 17:14:46 +03007#include <common.h>
Simon Glass9edefc22019-11-14 12:57:37 -07008#include <cpu_func.h>
Simon Glass401d1c42020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glasscd93d622020-05-10 11:40:13 -060010#include <linux/bitops.h>
Alexey Brodkinef639e62015-05-18 16:56:26 +030011#include <linux/compiler.h>
12#include <linux/kernel.h>
Alexey Brodkin97a63142017-06-26 11:46:47 +030013#include <linux/log2.h>
Marek Vasutcfa19712021-09-10 22:47:08 +020014#include <lmb.h>
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040015#include <asm/arcregs.h>
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +030016#include <asm/arc-bcr.h>
Alexey Brodkin205e7a72015-02-03 13:58:13 +030017#include <asm/cache.h>
Alexey Brodkin2f16ac92014-02-04 12:56:14 +040018
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +030019/*
20 * [ NOTE 1 ]:
21 * Data cache (L1 D$ or SL$) entire invalidate operation or data cache disable
22 * operation may result in unexpected behavior and data loss even if we flush
23 * data cache right before invalidation. That may happens if we store any context
24 * on stack (like we store BLINK register on stack before function call).
25 * BLINK register is the register where return address is automatically saved
26 * when we do function call with instructions like 'bl'.
27 *
28 * There is the real example:
29 * We may hang in the next code as we store any BLINK register on stack in
30 * invalidate_dcache_all() function.
31 *
32 * void flush_dcache_all() {
33 * __dc_entire_op(OP_FLUSH);
34 * // Other code //
35 * }
36 *
37 * void invalidate_dcache_all() {
38 * __dc_entire_op(OP_INV);
39 * // Other code //
40 * }
41 *
42 * void foo(void) {
43 * flush_dcache_all();
44 * invalidate_dcache_all();
45 * }
46 *
47 * Now let's see what really happens during that code execution:
48 *
49 * foo()
50 * |->> call flush_dcache_all
51 * [return address is saved to BLINK register]
52 * [push BLINK] (save to stack) ![point 1]
53 * |->> call __dc_entire_op(OP_FLUSH)
54 * [return address is saved to BLINK register]
55 * [flush L1 D$]
56 * return [jump to BLINK]
57 * <<------
58 * [other flush_dcache_all code]
59 * [pop BLINK] (get from stack)
60 * return [jump to BLINK]
61 * <<------
62 * |->> call invalidate_dcache_all
63 * [return address is saved to BLINK register]
64 * [push BLINK] (save to stack) ![point 2]
65 * |->> call __dc_entire_op(OP_FLUSH)
66 * [return address is saved to BLINK register]
67 * [invalidate L1 D$] ![point 3]
68 * // Oops!!!
69 * // We lose return address from invalidate_dcache_all function:
70 * // we save it to stack and invalidate L1 D$ after that!
71 * return [jump to BLINK]
72 * <<------
73 * [other invalidate_dcache_all code]
74 * [pop BLINK] (get from stack)
75 * // we don't have this data in L1 dcache as we invalidated it in [point 3]
76 * // so we get it from next memory level (for example DDR memory)
77 * // but in the memory we have value which we save in [point 1], which
78 * // is return address from flush_dcache_all function (instead of
79 * // address from current invalidate_dcache_all function which we
80 * // saved in [point 2] !)
81 * return [jump to BLINK]
82 * <<------
83 * // As BLINK points to invalidate_dcache_all, we call it again and
84 * // loop forever.
85 *
86 * Fortunately we may fix that by using flush & invalidation of D$ with a single
87 * one instruction (instead of flush and invalidation instructions pair) and
88 * enabling force function inline with '__attribute__((always_inline))' gcc
89 * attribute to avoid any function call (and BLINK store) between cache flush
90 * and disable.
Eugeniy Paltsev72419442018-03-21 15:59:03 +030091 *
92 *
93 * [ NOTE 2 ]:
94 * As of today we only support the following cache configurations on ARC.
Eugeniy Paltsevb15cb0b2020-03-11 15:00:43 +030095 * Other configurations may exist in HW but we don't support it in SW.
Eugeniy Paltsev72419442018-03-21 15:59:03 +030096 * Configuration 1:
97 * ______________________
98 * | |
99 * | ARC CPU |
100 * |______________________|
101 * ___|___ ___|___
102 * | | | |
103 * | L1 I$ | | L1 D$ |
104 * |_______| |_______|
105 * on/off on/off
106 * ___|______________|____
107 * | |
108 * | main memory |
109 * |______________________|
110 *
111 * Configuration 2:
112 * ______________________
113 * | |
114 * | ARC CPU |
115 * |______________________|
116 * ___|___ ___|___
117 * | | | |
118 * | L1 I$ | | L1 D$ |
119 * |_______| |_______|
120 * on/off on/off
121 * ___|______________|____
122 * | |
123 * | L2 (SL$) |
124 * |______________________|
Eugeniy Paltsevb15cb0b2020-03-11 15:00:43 +0300125 * always on (ARCv2, HS < 3.0)
126 * on/off (ARCv2, HS >= 3.0)
Eugeniy Paltsev72419442018-03-21 15:59:03 +0300127 * ___|______________|____
128 * | |
129 * | main memory |
130 * |______________________|
131 *
132 * Configuration 3:
133 * ______________________
134 * | |
135 * | ARC CPU |
136 * |______________________|
137 * ___|___ ___|___
138 * | | | |
139 * | L1 I$ | | L1 D$ |
140 * |_______| |_______|
141 * on/off must be on
142 * ___|______________|____ _______
143 * | | | |
144 * | L2 (SL$) |-----| IOC |
145 * |______________________| |_______|
146 * always must be on on/off
147 * ___|______________|____
148 * | |
149 * | main memory |
150 * |______________________|
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300151 */
152
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300153DECLARE_GLOBAL_DATA_PTR;
154
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400155/* Bit values in IC_CTRL */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300156#define IC_CTRL_CACHE_DISABLE BIT(0)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400157
158/* Bit values in DC_CTRL */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300159#define DC_CTRL_CACHE_DISABLE BIT(0)
160#define DC_CTRL_INV_MODE_FLUSH BIT(6)
161#define DC_CTRL_FLUSH_STATUS BIT(8)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400162
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300163#define OP_INV BIT(0)
164#define OP_FLUSH BIT(1)
165#define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300166
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300167/* Bit val in SLC_CONTROL */
168#define SLC_CTRL_DIS 0x001
169#define SLC_CTRL_IM 0x040
170#define SLC_CTRL_BUSY 0x100
171#define SLC_CTRL_RGN_OP_INV 0x200
172
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300173#define CACHE_LINE_MASK (~(gd->arch.l1_line_sz - 1))
Alexey Brodkin379b3282015-12-14 17:14:46 +0300174
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300175/*
176 * We don't want to use '__always_inline' macro here as it can be redefined
177 * to simple 'inline' in some cases which breaks stuff. See [ NOTE 1 ] for more
178 * details about the reasons we need to use always_inline functions.
179 */
180#define inlined_cachefunc inline __attribute__((always_inline))
181
182static inlined_cachefunc void __ic_entire_invalidate(void);
183static inlined_cachefunc void __dc_entire_op(const int cacheop);
Eugeniy Paltsevb15cb0b2020-03-11 15:00:43 +0300184static inlined_cachefunc void __slc_entire_op(const int op);
Eugeniy Paltsev04286d02020-03-11 15:00:44 +0300185static inlined_cachefunc bool ioc_enabled(void);
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300186
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300187static inline bool pae_exists(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300188{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300189 /* TODO: should we compare mmu version from BCR and from CONFIG? */
190#if (CONFIG_ARC_MMU_VER >= 4)
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300191 union bcr_mmu_4 mmu4;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300192
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300193 mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300194
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300195 if (mmu4.fields.pae)
196 return true;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300197#endif /* (CONFIG_ARC_MMU_VER >= 4) */
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300198
199 return false;
200}
201
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300202static inlined_cachefunc bool icache_exists(void)
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300203{
204 union bcr_di_cache ibcr;
205
206 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
207 return !!ibcr.fields.ver;
208}
209
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300210static inlined_cachefunc bool icache_enabled(void)
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300211{
212 if (!icache_exists())
213 return false;
214
215 return !(read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE);
216}
217
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300218static inlined_cachefunc bool dcache_exists(void)
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300219{
220 union bcr_di_cache dbcr;
221
222 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
223 return !!dbcr.fields.ver;
224}
225
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300226static inlined_cachefunc bool dcache_enabled(void)
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300227{
228 if (!dcache_exists())
229 return false;
230
231 return !(read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE);
232}
233
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300234static inlined_cachefunc bool slc_exists(void)
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300235{
236 if (is_isa_arcv2()) {
237 union bcr_generic sbcr;
238
239 sbcr.word = read_aux_reg(ARC_BCR_SLC);
240 return !!sbcr.fields.ver;
241 }
242
243 return false;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300244}
245
Eugeniy Paltsevb15cb0b2020-03-11 15:00:43 +0300246enum slc_dis_status {
247 ST_SLC_MISSING = 0,
248 ST_SLC_NO_DISABLE_CTRL,
249 ST_SLC_DISABLE_CTRL
250};
251
252/*
253 * ARCv1 -> ST_SLC_MISSING
254 * ARCv2 && SLC absent -> ST_SLC_MISSING
255 * ARCv2 && SLC exists && SLC version <= 2 -> ST_SLC_NO_DISABLE_CTRL
256 * ARCv2 && SLC exists && SLC version > 2 -> ST_SLC_DISABLE_CTRL
257 */
258static inlined_cachefunc enum slc_dis_status slc_disable_supported(void)
259{
260 if (is_isa_arcv2()) {
261 union bcr_generic sbcr;
262
263 sbcr.word = read_aux_reg(ARC_BCR_SLC);
264 if (sbcr.fields.ver == 0)
265 return ST_SLC_MISSING;
266 else if (sbcr.fields.ver <= 2)
267 return ST_SLC_NO_DISABLE_CTRL;
268 else
269 return ST_SLC_DISABLE_CTRL;
270 }
271
272 return ST_SLC_MISSING;
273}
274
275static inlined_cachefunc bool __slc_enabled(void)
276{
277 return !(read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_DIS);
278}
279
280static inlined_cachefunc void __slc_enable(void)
281{
282 unsigned int ctrl;
283
284 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
285 ctrl &= ~SLC_CTRL_DIS;
286 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
287}
288
289static inlined_cachefunc void __slc_disable(void)
290{
291 unsigned int ctrl;
292
293 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
294 ctrl |= SLC_CTRL_DIS;
295 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
296}
297
298static inlined_cachefunc bool slc_enabled(void)
299{
300 enum slc_dis_status slc_status = slc_disable_supported();
301
302 if (slc_status == ST_SLC_MISSING)
303 return false;
304 else if (slc_status == ST_SLC_NO_DISABLE_CTRL)
305 return true;
306 else
307 return __slc_enabled();
308}
309
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300310static inlined_cachefunc bool slc_data_bypass(void)
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300311{
312 /*
313 * If L1 data cache is disabled SL$ is bypassed and all load/store
314 * requests are sent directly to main memory.
315 */
316 return !dcache_enabled();
317}
318
Eugeniy Paltsevb15cb0b2020-03-11 15:00:43 +0300319void slc_enable(void)
320{
321 if (slc_disable_supported() != ST_SLC_DISABLE_CTRL)
322 return;
323
324 if (__slc_enabled())
325 return;
326
327 __slc_enable();
328}
329
330/* TODO: warn if we are not able to disable SLC */
331void slc_disable(void)
332{
333 if (slc_disable_supported() != ST_SLC_DISABLE_CTRL)
334 return;
335
336 /* we don't support SLC disabling if we use IOC */
337 if (ioc_enabled())
338 return;
339
340 if (!__slc_enabled())
341 return;
342
343 /*
344 * We need to flush L1D$ to guarantee that we won't have any
345 * writeback operations during SLC disabling.
346 */
347 __dc_entire_op(OP_FLUSH);
348 __slc_entire_op(OP_FLUSH_N_INV);
349 __slc_disable();
350}
351
Eugeniy Paltsev04286d02020-03-11 15:00:44 +0300352static inlined_cachefunc bool ioc_exists(void)
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300353{
354 if (is_isa_arcv2()) {
355 union bcr_clust_cfg cbcr;
356
357 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
358 return cbcr.fields.c;
359 }
360
361 return false;
362}
363
Eugeniy Paltsev04286d02020-03-11 15:00:44 +0300364static inlined_cachefunc bool ioc_enabled(void)
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300365{
366 /*
367 * We check only CONFIG option instead of IOC HW state check as IOC
368 * must be disabled by default.
369 */
370 if (is_ioc_enabled())
371 return ioc_exists();
372
373 return false;
374}
375
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300376static inlined_cachefunc void __slc_entire_op(const int op)
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300377{
378 unsigned int ctrl;
379
Eugeniy Paltsevb15cb0b2020-03-11 15:00:43 +0300380 if (!slc_enabled())
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300381 return;
382
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300383 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
384
385 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
386 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
387 else
388 ctrl |= SLC_CTRL_IM;
389
390 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
391
392 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
393 write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
394 else
395 write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
396
397 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
398 read_aux_reg(ARC_AUX_SLC_CTRL);
399
400 /* Important to wait for flush to complete */
401 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
402}
403
404static void slc_upper_region_init(void)
405{
406 /*
Eugeniy Paltsev246ba282018-03-21 15:58:58 +0300407 * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
408 * only if PAE exists in current HW. So we had to check pae_exist
409 * before using them.
410 */
411 if (!pae_exists())
412 return;
413
414 /*
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300415 * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
416 * as we don't use PAE40.
417 */
418 write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
419 write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
420}
421
422static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
423{
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300424#ifdef CONFIG_ISA_ARCV2
425
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300426 unsigned int ctrl;
427 unsigned long end;
428
Eugeniy Paltsevb15cb0b2020-03-11 15:00:43 +0300429 if (!slc_enabled())
Eugeniy Paltsevea9f6f12018-03-21 15:58:55 +0300430 return;
431
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300432 /*
433 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
434 * - b'000 (default) is Flush,
435 * - b'001 is Invalidate if CTRL.IM == 0
436 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
437 */
438 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
439
440 /* Don't rely on default value of IM bit */
441 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
442 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
443 else
444 ctrl |= SLC_CTRL_IM;
445
446 if (op & OP_INV)
447 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
448 else
449 ctrl &= ~SLC_CTRL_RGN_OP_INV;
450
451 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
452
453 /*
454 * Lower bits are ignored, no need to clip
455 * END needs to be setup before START (latter triggers the operation)
456 * END can't be same as START, so add (l2_line_sz - 1) to sz
457 */
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300458 end = paddr + sz + gd->arch.slc_line_sz - 1;
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300459
460 /*
461 * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
462 * are always == 0 as we don't use PAE40, so we only setup lower ones
463 * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
464 */
465 write_aux_reg(ARC_AUX_SLC_RGN_END, end);
466 write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
467
468 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
469 read_aux_reg(ARC_AUX_SLC_CTRL);
470
471 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300472
473#endif /* CONFIG_ISA_ARCV2 */
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300474}
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300475
476static void arc_ioc_setup(void)
477{
478 /* IOC Aperture start is equal to DDR start */
479 unsigned int ap_base = CONFIG_SYS_SDRAM_BASE;
480 /* IOC Aperture size is equal to DDR size */
481 long ap_size = CONFIG_SYS_SDRAM_SIZE;
482
Eugeniy Paltsev72419442018-03-21 15:59:03 +0300483 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
484 if (!slc_exists())
485 panic("Try to enable IOC but SLC is not present");
486
Eugeniy Paltsevb15cb0b2020-03-11 15:00:43 +0300487 if (!slc_enabled())
488 panic("Try to enable IOC but SLC is disabled");
489
Eugeniy Paltsev72419442018-03-21 15:59:03 +0300490 /* Unsupported configuration. See [ NOTE 2 ] for more details. */
491 if (!dcache_enabled())
492 panic("Try to enable IOC but L1 D$ is disabled");
493
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300494 if (!is_power_of_2(ap_size) || ap_size < 4096)
495 panic("IOC Aperture size must be power of 2 and bigger 4Kib");
496
Eugeniy Paltsev6b85b262018-03-21 15:59:05 +0300497 /* IOC Aperture start must be aligned to the size of the aperture */
498 if (ap_base % ap_size != 0)
499 panic("IOC Aperture start must be aligned to the size of the aperture");
500
501 flush_n_invalidate_dcache_all();
502
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300503 /*
504 * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
505 * so setting 0x11 implies 512M, 0x12 implies 1G...
506 */
507 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
508 order_base_2(ap_size / 1024) - 2);
509
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300510 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
511 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
512 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
513}
Alexey Brodkinef639e62015-05-18 16:56:26 +0300514
Alexey Brodkin379b3282015-12-14 17:14:46 +0300515static void read_decode_cache_bcr_arcv2(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300516{
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300517#ifdef CONFIG_ISA_ARCV2
518
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300519 union bcr_slc_cfg slc_cfg;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300520
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300521 if (slc_exists()) {
Alexey Brodkin379b3282015-12-14 17:14:46 +0300522 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300523 gd->arch.slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
Eugeniy Paltsev72419442018-03-21 15:59:03 +0300524
525 /*
526 * We don't support configuration where L1 I$ or L1 D$ is
527 * absent but SL$ exists. See [ NOTE 2 ] for more details.
528 */
529 if (!icache_exists() || !dcache_exists())
530 panic("Unsupported cache configuration: SLC exists but one of L1 caches is absent");
Alexey Brodkin379b3282015-12-14 17:14:46 +0300531 }
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300532
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300533#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin379b3282015-12-14 17:14:46 +0300534}
Alexey Brodkin379b3282015-12-14 17:14:46 +0300535
536void read_decode_cache_bcr(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300537{
Alexey Brodkin379b3282015-12-14 17:14:46 +0300538 int dc_line_sz = 0, ic_line_sz = 0;
Eugeniy Paltsev88ae27e2018-03-21 15:58:52 +0300539 union bcr_di_cache ibcr, dbcr;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300540
Alexey Brodkind0a50232018-05-25 20:22:23 +0300541 /*
542 * We don't care much about I$ line length really as there're
543 * no per-line ops on I$ instead we only do full invalidation of it
544 * on occasion of relocation and right before jumping to the OS.
545 * Still we check insane config with zero-encoded line length in
546 * presense of version field in I$ BCR. Just in case.
547 */
Alexey Brodkin379b3282015-12-14 17:14:46 +0300548 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
549 if (ibcr.fields.ver) {
Alexey Brodkind0a50232018-05-25 20:22:23 +0300550 ic_line_sz = 8 << ibcr.fields.line_len;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300551 if (!ic_line_sz)
552 panic("Instruction exists but line length is 0\n");
553 }
554
555 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300556 if (dbcr.fields.ver) {
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300557 gd->arch.l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
Alexey Brodkin379b3282015-12-14 17:14:46 +0300558 if (!dc_line_sz)
559 panic("Data cache exists but line length is 0\n");
560 }
Alexey Brodkinef639e62015-05-18 16:56:26 +0300561}
562
563void cache_init(void)
564{
Alexey Brodkin379b3282015-12-14 17:14:46 +0300565 read_decode_cache_bcr();
566
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300567 if (is_isa_arcv2())
568 read_decode_cache_bcr_arcv2();
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300569
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300570 if (is_isa_arcv2() && ioc_enabled())
Eugeniy Paltseva6f557c2018-03-21 15:58:51 +0300571 arc_ioc_setup();
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300572
Eugeniy Paltsev246ba282018-03-21 15:58:58 +0300573 if (is_isa_arcv2() && slc_exists())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300574 slc_upper_region_init();
Alexey Brodkinef639e62015-05-18 16:56:26 +0300575}
576
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400577int icache_status(void)
578{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300579 return icache_enabled();
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400580}
581
582void icache_enable(void)
583{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300584 if (icache_exists())
Alexey Brodkinef639e62015-05-18 16:56:26 +0300585 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
586 ~IC_CTRL_CACHE_DISABLE);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400587}
588
589void icache_disable(void)
590{
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300591 if (!icache_exists())
592 return;
593
594 __ic_entire_invalidate();
595
596 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
597 IC_CTRL_CACHE_DISABLE);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400598}
599
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300600/* IC supports only invalidation */
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300601static inlined_cachefunc void __ic_entire_invalidate(void)
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300602{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300603 if (!icache_enabled())
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300604 return;
605
606 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
607 write_aux_reg(ARC_AUX_IC_IVIC, 1);
608 /*
609 * As per ARC HS databook (see chapter 5.3.3.2)
610 * it is required to add 3 NOPs after each write to IC_IVIC.
611 */
612 __builtin_arc_nop();
613 __builtin_arc_nop();
614 __builtin_arc_nop();
615 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
616}
617
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400618void invalidate_icache_all(void)
619{
Eugeniy Paltsev16aeee82018-03-21 15:58:46 +0300620 __ic_entire_invalidate();
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300621
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300622 /*
623 * If SL$ is bypassed for data it is used only for instructions,
624 * so we need to invalidate it too.
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300625 */
626 if (is_isa_arcv2() && slc_data_bypass())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300627 __slc_entire_op(OP_INV);
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300628}
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400629
630int dcache_status(void)
631{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300632 return dcache_enabled();
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400633}
634
635void dcache_enable(void)
636{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300637 if (!dcache_exists())
Igor Guryanovf8cf3d12014-12-24 16:07:07 +0300638 return;
639
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400640 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
641 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
642}
643
644void dcache_disable(void)
645{
Eugeniy Paltsev75790872018-03-21 15:58:56 +0300646 if (!dcache_exists())
Igor Guryanovf8cf3d12014-12-24 16:07:07 +0300647 return;
648
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300649 __dc_entire_op(OP_FLUSH_N_INV);
650
651 /*
652 * As SLC will be bypassed for data after L1 D$ disable we need to
653 * flush it first before L1 D$ disable. Also we invalidate SLC to
654 * avoid any inconsistent data problems after enabling L1 D$ again with
655 * dcache_enable function.
656 */
657 if (is_isa_arcv2())
658 __slc_entire_op(OP_FLUSH_N_INV);
659
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400660 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
661 DC_CTRL_CACHE_DISABLE);
662}
663
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300664/* Common Helper for Line Operations on D-cache */
665static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
666 const int cacheop)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400667{
Alexey Brodkinef639e62015-05-18 16:56:26 +0300668 unsigned int aux_cmd;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300669 int num_lines;
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400670
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300671 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
672 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300673
674 sz += paddr & ~CACHE_LINE_MASK;
675 paddr &= CACHE_LINE_MASK;
676
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300677 num_lines = DIV_ROUND_UP(sz, gd->arch.l1_line_sz);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300678
679 while (num_lines-- > 0) {
680#if (CONFIG_ARC_MMU_VER == 3)
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300681 write_aux_reg(ARC_AUX_DC_PTAG, paddr);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300682#endif
683 write_aux_reg(aux_cmd, paddr);
Eugeniy Paltsevbf8974e2018-03-21 15:58:57 +0300684 paddr += gd->arch.l1_line_sz;
Alexey Brodkinef639e62015-05-18 16:56:26 +0300685 }
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400686}
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400687
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300688static inlined_cachefunc void __before_dc_op(const int op)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400689{
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300690 unsigned int ctrl;
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400691
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300692 ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400693
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300694 /* IM bit implies flush-n-inv, instead of vanilla inv */
695 if (op == OP_INV)
696 ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
697 else
698 ctrl |= DC_CTRL_INV_MODE_FLUSH;
699
700 write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400701}
702
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300703static inlined_cachefunc void __after_dc_op(const int op)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300704{
705 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
Eugeniy Paltsev19b10a42018-01-16 19:20:29 +0300706 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300707}
708
Eugeniy Paltsev9f0253c2018-03-21 15:59:04 +0300709static inlined_cachefunc void __dc_entire_op(const int cacheop)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300710{
711 int aux;
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300712
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300713 if (!dcache_enabled())
Eugeniy Paltsevc877a892018-03-21 15:58:53 +0300714 return;
715
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300716 __before_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300717
718 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
719 aux = ARC_AUX_DC_IVDC;
720 else
721 aux = ARC_AUX_DC_FLSH;
722
723 write_aux_reg(aux, 0x1);
724
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300725 __after_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300726}
727
728static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
729 const int cacheop)
730{
Eugeniy Paltsevc75eeb02018-03-21 15:59:00 +0300731 if (!dcache_enabled())
Eugeniy Paltsevc877a892018-03-21 15:58:53 +0300732 return;
733
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300734 __before_dc_op(cacheop);
Eugeniy Paltsevc4ef14d2018-03-21 15:58:47 +0300735 __dcache_line_loop(paddr, sz, cacheop);
Eugeniy Paltsev5d7a24d2018-03-21 15:58:48 +0300736 __after_dc_op(cacheop);
Alexey Brodkinef639e62015-05-18 16:56:26 +0300737}
Alexey Brodkinef639e62015-05-18 16:56:26 +0300738
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400739void invalidate_dcache_range(unsigned long start, unsigned long end)
740{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300741 if (start >= end)
742 return;
743
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300744 /*
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300745 * ARCv1 -> call __dc_line_op
746 * ARCv2 && L1 D$ disabled -> nothing
747 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
748 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300749 */
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300750 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300751 __dc_line_op(start, end - start, OP_INV);
752
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300753 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300754 __slc_rgn_op(start, end - start, OP_INV);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400755}
756
Alexey Brodkinef639e62015-05-18 16:56:26 +0300757void flush_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400758{
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300759 if (start >= end)
760 return;
761
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300762 /*
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300763 * ARCv1 -> call __dc_line_op
764 * ARCv2 && L1 D$ disabled -> nothing
765 * ARCv2 && L1 D$ enabled && IOC enabled -> nothing
766 * ARCv2 && L1 D$ enabled && no IOC -> call __dc_line_op; call __slc_rgn_op
Eugeniy Paltsev05c6a262018-03-21 15:58:54 +0300767 */
Eugeniy Paltsev48b04832018-03-21 15:58:59 +0300768 if (!is_isa_arcv2() || !ioc_enabled())
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300769 __dc_line_op(start, end - start, OP_FLUSH);
770
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300771 if (is_isa_arcv2() && !ioc_enabled() && !slc_data_bypass())
Eugeniy Paltsev41cada42018-01-16 19:20:26 +0300772 __slc_rgn_op(start, end - start, OP_FLUSH);
Alexey Brodkin2f16ac92014-02-04 12:56:14 +0400773}
774
775void flush_cache(unsigned long start, unsigned long size)
776{
777 flush_dcache_range(start, start + size);
778}
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300779
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300780/*
781 * As invalidate_dcache_all() is not used in generic U-Boot code and as we
782 * don't need it in arch/arc code alone (invalidate without flush) we implement
783 * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
784 * it's much safer. See [ NOTE 1 ] for more details.
785 */
786void flush_n_invalidate_dcache_all(void)
Alexey Brodkinef639e62015-05-18 16:56:26 +0300787{
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300788 __dc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300789
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300790 if (is_isa_arcv2() && !slc_data_bypass())
Eugeniy Paltsevc27814b2018-03-21 15:58:50 +0300791 __slc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300792}
793
Alexey Brodkinef639e62015-05-18 16:56:26 +0300794void flush_dcache_all(void)
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300795{
Alexey Brodkin2a8382c2016-04-16 15:28:30 +0300796 __dc_entire_op(OP_FLUSH);
Alexey Brodkindb6ce232015-12-14 17:15:13 +0300797
Eugeniy Paltsev95336732018-03-21 15:59:01 +0300798 if (is_isa_arcv2() && !slc_data_bypass())
Alexey Brodkinef639e62015-05-18 16:56:26 +0300799 __slc_entire_op(OP_FLUSH);
Alexey Brodkin6eb15e52015-03-30 13:36:04 +0300800}
Eugeniy Paltsev375945b2018-03-21 15:59:02 +0300801
802/*
803 * This is function to cleanup all caches (and therefore sync I/D caches) which
804 * can be used for cleanup before linux launch or to sync caches during
805 * relocation.
806 */
807void sync_n_cleanup_cache_all(void)
808{
809 __dc_entire_op(OP_FLUSH_N_INV);
810
811 /*
812 * If SL$ is bypassed for data it is used only for instructions,
813 * and we shouldn't flush it. So invalidate it instead of flush_n_inv.
814 */
815 if (is_isa_arcv2()) {
816 if (slc_data_bypass())
817 __slc_entire_op(OP_INV);
818 else
819 __slc_entire_op(OP_FLUSH_N_INV);
820 }
821
822 __ic_entire_invalidate();
823}
Marek Vasutcfa19712021-09-10 22:47:08 +0200824
825static ulong get_sp(void)
826{
827 ulong ret;
828
829 asm("mov %0, sp" : "=r"(ret) : );
830 return ret;
831}
832
833void arch_lmb_reserve(struct lmb *lmb)
834{
835 ulong sp;
836
837 /*
838 * Booting a (Linux) kernel image
839 *
840 * Allocate space for command line and board info - the
841 * address should be as high as possible within the reach of
842 * the kernel (see CONFIG_SYS_BOOTMAPSZ settings), but in unused
843 * memory, which means far enough below the current stack
844 * pointer.
845 */
846 sp = get_sp();
847 debug("## Current stack ends at 0x%08lx ", sp);
848
849 /* adjust sp by 4K to be safe */
850 sp -= 4096;
851 lmb_reserve(lmb, sp, (CONFIG_SYS_SDRAM_BASE + gd->ram_size - sp));
852}