blob: 354dbc600edeb82cdf9d99977953961b8589013b [file] [log] [blame]
Alexey Brodkin3a59d912014-02-04 12:56:14 +04001/*
2 * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include <config.h>
Alexey Brodkindff5df22015-12-14 17:14:46 +03008#include <common.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.
88 */
89
Alexey Brodkin3a59d912014-02-04 12:56:14 +040090/* Bit values in IC_CTRL */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +030091#define IC_CTRL_CACHE_DISABLE BIT(0)
Alexey Brodkin3a59d912014-02-04 12:56:14 +040092
93/* Bit values in DC_CTRL */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +030094#define DC_CTRL_CACHE_DISABLE BIT(0)
95#define DC_CTRL_INV_MODE_FLUSH BIT(6)
96#define DC_CTRL_FLUSH_STATUS BIT(8)
Igor Guryanovbd889f92014-12-24 16:07:07 +030097#define CACHE_VER_NUM_MASK 0xF
Alexey Brodkin3a59d912014-02-04 12:56:14 +040098
Eugeniy Paltseve256cb02018-03-21 15:58:48 +030099#define OP_INV BIT(0)
100#define OP_FLUSH BIT(1)
101#define OP_FLUSH_N_INV (OP_FLUSH | OP_INV)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300102
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300103/* Bit val in SLC_CONTROL */
104#define SLC_CTRL_DIS 0x001
105#define SLC_CTRL_IM 0x040
106#define SLC_CTRL_BUSY 0x100
107#define SLC_CTRL_RGN_OP_INV 0x200
108
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300109/*
110 * By default that variable will fall into .bss section.
111 * But .bss section is not relocated and so it will be initilized before
112 * relocation but will be used after being zeroed.
113 */
Alexey Brodkindff5df22015-12-14 17:14:46 +0300114int l1_line_sz __section(".data");
Eugeniy Paltsev570d5512017-11-30 17:41:32 +0300115bool dcache_exists __section(".data") = false;
116bool icache_exists __section(".data") = false;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300117
118#define CACHE_LINE_MASK (~(l1_line_sz - 1))
119
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300120int slc_line_sz __section(".data");
Eugeniy Paltsev570d5512017-11-30 17:41:32 +0300121bool slc_exists __section(".data") = false;
122bool ioc_exists __section(".data") = false;
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300123bool pae_exists __section(".data") = false;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300124
Eugeniy Paltsev111161e2018-01-16 19:20:28 +0300125/* To force enable IOC set ioc_enable to 'true' */
126bool ioc_enable __section(".data") = false;
127
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300128void read_decode_mmu_bcr(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300129{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300130 /* TODO: should we compare mmu version from BCR and from CONFIG? */
131#if (CONFIG_ARC_MMU_VER >= 4)
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300132 union bcr_mmu_4 mmu4;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300133
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300134 mmu4.word = read_aux_reg(ARC_AUX_MMU_BCR);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300135
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300136 pae_exists = !!mmu4.fields.pae;
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300137#endif /* (CONFIG_ARC_MMU_VER >= 4) */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300138}
139
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300140static void __slc_entire_op(const int op)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300141{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300142 unsigned int ctrl;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300143
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300144 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
145
146 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
147 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
148 else
149 ctrl |= SLC_CTRL_IM;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300150
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300151 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300152
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300153 if (op & OP_INV) /* Inv or flush-n-inv use same cmd reg */
154 write_aux_reg(ARC_AUX_SLC_INVALIDATE, 0x1);
155 else
156 write_aux_reg(ARC_AUX_SLC_FLUSH, 0x1);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300157
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300158 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
159 read_aux_reg(ARC_AUX_SLC_CTRL);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300160
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300161 /* Important to wait for flush to complete */
162 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300163}
164
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300165static void slc_upper_region_init(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300166{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300167 /*
168 * ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1 are always == 0
169 * as we don't use PAE40.
170 */
171 write_aux_reg(ARC_AUX_SLC_RGN_END1, 0);
172 write_aux_reg(ARC_AUX_SLC_RGN_START1, 0);
173}
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300174
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300175static void __slc_rgn_op(unsigned long paddr, unsigned long sz, const int op)
176{
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300177#ifdef CONFIG_ISA_ARCV2
178
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300179 unsigned int ctrl;
180 unsigned long end;
181
182 /*
183 * The Region Flush operation is specified by CTRL.RGN_OP[11..9]
184 * - b'000 (default) is Flush,
185 * - b'001 is Invalidate if CTRL.IM == 0
186 * - b'001 is Flush-n-Invalidate if CTRL.IM == 1
187 */
188 ctrl = read_aux_reg(ARC_AUX_SLC_CTRL);
189
190 /* Don't rely on default value of IM bit */
191 if (!(op & OP_FLUSH)) /* i.e. OP_INV */
192 ctrl &= ~SLC_CTRL_IM; /* clear IM: Disable flush before Inv */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300193 else
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300194 ctrl |= SLC_CTRL_IM;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300195
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300196 if (op & OP_INV)
197 ctrl |= SLC_CTRL_RGN_OP_INV; /* Inv or flush-n-inv */
198 else
199 ctrl &= ~SLC_CTRL_RGN_OP_INV;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300200
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300201 write_aux_reg(ARC_AUX_SLC_CTRL, ctrl);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300202
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300203 /*
204 * Lower bits are ignored, no need to clip
205 * END needs to be setup before START (latter triggers the operation)
206 * END can't be same as START, so add (l2_line_sz - 1) to sz
207 */
208 end = paddr + sz + slc_line_sz - 1;
209
210 /*
211 * Upper addresses (ARC_AUX_SLC_RGN_END1 and ARC_AUX_SLC_RGN_START1)
212 * are always == 0 as we don't use PAE40, so we only setup lower ones
213 * (ARC_AUX_SLC_RGN_END and ARC_AUX_SLC_RGN_START)
214 */
215 write_aux_reg(ARC_AUX_SLC_RGN_END, end);
216 write_aux_reg(ARC_AUX_SLC_RGN_START, paddr);
217
218 /* Make sure "busy" bit reports correct stataus, see STAR 9001165532 */
219 read_aux_reg(ARC_AUX_SLC_CTRL);
220
221 while (read_aux_reg(ARC_AUX_SLC_CTRL) & SLC_CTRL_BUSY);
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300222
223#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300224}
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300225
226static void arc_ioc_setup(void)
227{
228 /* IOC Aperture start is equal to DDR start */
229 unsigned int ap_base = CONFIG_SYS_SDRAM_BASE;
230 /* IOC Aperture size is equal to DDR size */
231 long ap_size = CONFIG_SYS_SDRAM_SIZE;
232
233 flush_n_invalidate_dcache_all();
234
235 if (!is_power_of_2(ap_size) || ap_size < 4096)
236 panic("IOC Aperture size must be power of 2 and bigger 4Kib");
237
238 /*
239 * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB,
240 * so setting 0x11 implies 512M, 0x12 implies 1G...
241 */
242 write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE,
243 order_base_2(ap_size / 1024) - 2);
244
245 /* IOC Aperture start must be aligned to the size of the aperture */
246 if (ap_base % ap_size != 0)
247 panic("IOC Aperture start must be aligned to the size of the aperture");
248
249 write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12);
250 write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1);
251 write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1);
252}
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300253
Alexey Brodkindff5df22015-12-14 17:14:46 +0300254static void read_decode_cache_bcr_arcv2(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300255{
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300256#ifdef CONFIG_ISA_ARCV2
257
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300258 union bcr_slc_cfg slc_cfg;
259 union bcr_clust_cfg cbcr;
260 union bcr_generic sbcr;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300261
262 sbcr.word = read_aux_reg(ARC_BCR_SLC);
263 if (sbcr.fields.ver) {
264 slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG);
Eugeniy Paltsev570d5512017-11-30 17:41:32 +0300265 slc_exists = true;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300266 slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64;
267 }
Alexey Brodkin4764d262015-12-14 17:15:13 +0300268
Alexey Brodkin4764d262015-12-14 17:15:13 +0300269 cbcr.word = read_aux_reg(ARC_BCR_CLUSTER);
Eugeniy Paltsev111161e2018-01-16 19:20:28 +0300270 if (cbcr.fields.c && ioc_enable)
Eugeniy Paltsev570d5512017-11-30 17:41:32 +0300271 ioc_exists = true;
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300272
273#endif /* CONFIG_ISA_ARCV2 */
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300274}
275
Alexey Brodkindff5df22015-12-14 17:14:46 +0300276void read_decode_cache_bcr(void)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300277{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300278 int dc_line_sz = 0, ic_line_sz = 0;
Eugeniy Paltsev589ac752018-03-21 15:58:52 +0300279 union bcr_di_cache ibcr, dbcr;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300280
281 ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD);
282 if (ibcr.fields.ver) {
Eugeniy Paltsev570d5512017-11-30 17:41:32 +0300283 icache_exists = true;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300284 l1_line_sz = ic_line_sz = 8 << ibcr.fields.line_len;
285 if (!ic_line_sz)
286 panic("Instruction exists but line length is 0\n");
287 }
288
289 dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD);
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300290 if (dbcr.fields.ver) {
Eugeniy Paltsev570d5512017-11-30 17:41:32 +0300291 dcache_exists = true;
Alexey Brodkindff5df22015-12-14 17:14:46 +0300292 l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len;
293 if (!dc_line_sz)
294 panic("Data cache exists but line length is 0\n");
295 }
296
297 if (ic_line_sz && dc_line_sz && (ic_line_sz != dc_line_sz))
298 panic("Instruction and data cache line lengths differ\n");
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300299}
300
301void cache_init(void)
302{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300303 read_decode_cache_bcr();
304
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300305 if (is_isa_arcv2())
306 read_decode_cache_bcr_arcv2();
Alexey Brodkin4764d262015-12-14 17:15:13 +0300307
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300308 if (is_isa_arcv2() && ioc_exists)
Eugeniy Paltsevd4c5b2a2018-03-21 15:58:51 +0300309 arc_ioc_setup();
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300310
311 read_decode_mmu_bcr();
312
313 /*
314 * ARC_AUX_SLC_RGN_START1 and ARC_AUX_SLC_RGN_END1 register exist
315 * only if PAE exists in current HW. So we had to check pae_exist
316 * before using them.
317 */
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300318 if (is_isa_arcv2() && slc_exists && pae_exists)
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300319 slc_upper_region_init();
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300320}
321
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400322int icache_status(void)
323{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300324 if (!icache_exists)
Igor Guryanovbd889f92014-12-24 16:07:07 +0300325 return 0;
326
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300327 if (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE)
328 return 0;
329 else
330 return 1;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400331}
332
333void icache_enable(void)
334{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300335 if (icache_exists)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300336 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) &
337 ~IC_CTRL_CACHE_DISABLE);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400338}
339
340void icache_disable(void)
341{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300342 if (icache_exists)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300343 write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) |
344 IC_CTRL_CACHE_DISABLE);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400345}
346
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300347/* IC supports only invalidation */
348static inline void __ic_entire_invalidate(void)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400349{
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300350 if (!icache_status())
351 return;
352
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400353 /* Any write to IC_IVIC register triggers invalidation of entire I$ */
Eugeniy Paltsevad8aef32018-03-21 15:58:46 +0300354 write_aux_reg(ARC_AUX_IC_IVIC, 1);
355 /*
356 * As per ARC HS databook (see chapter 5.3.3.2)
357 * it is required to add 3 NOPs after each write to IC_IVIC.
358 */
359 __builtin_arc_nop();
360 __builtin_arc_nop();
361 __builtin_arc_nop();
362 read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */
363}
364
365void invalidate_icache_all(void)
366{
367 __ic_entire_invalidate();
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300368
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300369 if (is_isa_arcv2() && slc_exists)
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300370 __slc_entire_op(OP_INV);
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300371}
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400372
373int dcache_status(void)
374{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300375 if (!dcache_exists)
Igor Guryanovbd889f92014-12-24 16:07:07 +0300376 return 0;
377
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300378 if (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE)
379 return 0;
380 else
381 return 1;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400382}
383
384void dcache_enable(void)
385{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300386 if (!dcache_exists)
Igor Guryanovbd889f92014-12-24 16:07:07 +0300387 return;
388
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400389 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) &
390 ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE));
391}
392
393void dcache_disable(void)
394{
Alexey Brodkindff5df22015-12-14 17:14:46 +0300395 if (!dcache_exists)
Igor Guryanovbd889f92014-12-24 16:07:07 +0300396 return;
397
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400398 write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) |
399 DC_CTRL_CACHE_DISABLE);
400}
401
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300402/* Common Helper for Line Operations on D-cache */
403static inline void __dcache_line_loop(unsigned long paddr, unsigned long sz,
404 const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400405{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300406 unsigned int aux_cmd;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300407 int num_lines;
408
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300409 /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */
410 aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400411
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300412 sz += paddr & ~CACHE_LINE_MASK;
413 paddr &= CACHE_LINE_MASK;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400414
Alexey Brodkindff5df22015-12-14 17:14:46 +0300415 num_lines = DIV_ROUND_UP(sz, l1_line_sz);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300416
417 while (num_lines-- > 0) {
Alexey Brodkin6da8cfc2015-02-03 13:58:12 +0300418#if (CONFIG_ARC_MMU_VER == 3)
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300419 write_aux_reg(ARC_AUX_DC_PTAG, paddr);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400420#endif
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300421 write_aux_reg(aux_cmd, paddr);
Alexey Brodkindff5df22015-12-14 17:14:46 +0300422 paddr += l1_line_sz;
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300423 }
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400424}
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400425
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300426static void __before_dc_op(const int op)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400427{
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300428 unsigned int ctrl;
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400429
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300430 ctrl = read_aux_reg(ARC_AUX_DC_CTRL);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400431
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300432 /* IM bit implies flush-n-inv, instead of vanilla inv */
433 if (op == OP_INV)
434 ctrl &= ~DC_CTRL_INV_MODE_FLUSH;
435 else
436 ctrl |= DC_CTRL_INV_MODE_FLUSH;
437
438 write_aux_reg(ARC_AUX_DC_CTRL, ctrl);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400439}
440
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300441static void __after_dc_op(const int op)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400442{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300443 if (op & OP_FLUSH) /* flush / flush-n-inv both wait */
Eugeniy Paltsev6e626f02018-01-16 19:20:29 +0300444 while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400445}
446
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300447static inline void __dc_entire_op(const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400448{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300449 int aux;
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300450
Eugeniy Paltsev7fd7e0a2018-03-21 15:58:53 +0300451 if (!dcache_status())
452 return;
453
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300454 __before_dc_op(cacheop);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300455
456 if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */
457 aux = ARC_AUX_DC_IVDC;
458 else
459 aux = ARC_AUX_DC_FLSH;
Alexey Brodkin35221a62015-03-27 12:47:29 +0300460
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300461 write_aux_reg(aux, 0x1);
462
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300463 __after_dc_op(cacheop);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400464}
465
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300466static inline void __dc_line_op(unsigned long paddr, unsigned long sz,
467 const int cacheop)
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400468{
Eugeniy Paltsev7fd7e0a2018-03-21 15:58:53 +0300469 if (!dcache_status())
470 return;
471
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300472 __before_dc_op(cacheop);
Eugeniy Paltsev988152c2018-03-21 15:58:47 +0300473 __dcache_line_loop(paddr, sz, cacheop);
Eugeniy Paltseve256cb02018-03-21 15:58:48 +0300474 __after_dc_op(cacheop);
Alexey Brodkin3a59d912014-02-04 12:56:14 +0400475}
Alexey Brodkin275583e2015-03-30 13:36:04 +0300476
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300477void invalidate_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300478{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300479 if (start >= end)
480 return;
481
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300482 /*
483 * ARCv1 -> call __dc_line_op
484 * ARCv2 && no IOC -> call __dc_line_op; call __slc_rgn_op
485 * ARCv2 && IOC enabled -> nothing
486 */
487 if (!is_isa_arcv2() || !ioc_exists)
Alexey Brodkin4764d262015-12-14 17:15:13 +0300488 __dc_line_op(start, end - start, OP_INV);
489
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300490 if (is_isa_arcv2() && slc_exists && !ioc_exists)
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300491 __slc_rgn_op(start, end - start, OP_INV);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300492}
493
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300494void flush_dcache_range(unsigned long start, unsigned long end)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300495{
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300496 if (start >= end)
497 return;
498
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300499 /*
500 * ARCv1 -> call __dc_line_op
501 * ARCv2 && no IOC -> call __dc_line_op; call __slc_rgn_op
502 * ARCv2 && IOC enabled -> nothing
503 */
504 if (!is_isa_arcv2() || !ioc_exists)
Alexey Brodkin4764d262015-12-14 17:15:13 +0300505 __dc_line_op(start, end - start, OP_FLUSH);
506
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300507 if (is_isa_arcv2() && slc_exists && !ioc_exists)
Eugeniy Paltsev1d0578e2018-01-16 19:20:26 +0300508 __slc_rgn_op(start, end - start, OP_FLUSH);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300509}
510
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300511void flush_cache(unsigned long start, unsigned long size)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300512{
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300513 flush_dcache_range(start, start + size);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300514}
515
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300516/*
517 * As invalidate_dcache_all() is not used in generic U-Boot code and as we
518 * don't need it in arch/arc code alone (invalidate without flush) we implement
519 * flush_n_invalidate_dcache_all (flush and invalidate in 1 operation) because
520 * it's much safer. See [ NOTE 1 ] for more details.
521 */
522void flush_n_invalidate_dcache_all(void)
Alexey Brodkin275583e2015-03-30 13:36:04 +0300523{
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300524 __dc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin4764d262015-12-14 17:15:13 +0300525
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300526 if (is_isa_arcv2() && slc_exists)
Eugeniy Paltsevbcedf4d2018-03-21 15:58:50 +0300527 __slc_entire_op(OP_FLUSH_N_INV);
Alexey Brodkin275583e2015-03-30 13:36:04 +0300528}
529
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300530void flush_dcache_all(void)
531{
Alexey Brodkin5f541692016-04-16 15:28:30 +0300532 __dc_entire_op(OP_FLUSH);
Alexey Brodkin4764d262015-12-14 17:15:13 +0300533
Eugeniy Paltsev76caa802018-03-21 15:58:54 +0300534 if (is_isa_arcv2() && slc_exists)
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300535 __slc_entire_op(OP_FLUSH);
Alexey Brodkin9f916ee2015-05-18 16:56:26 +0300536}