]>
Commit | Line | Data |
---|---|---|
2f16ac9d AB |
1 | /* |
2 | * Copyright (C) 2013-2014 Synopsys, Inc. All rights reserved. | |
3 | * | |
4 | * SPDX-License-Identifier: GPL-2.0+ | |
5 | */ | |
6 | ||
7 | #include <config.h> | |
379b3280 | 8 | #include <common.h> |
ef639e6f AB |
9 | #include <linux/compiler.h> |
10 | #include <linux/kernel.h> | |
97a63144 | 11 | #include <linux/log2.h> |
2f16ac9d | 12 | #include <asm/arcregs.h> |
205e7a7b | 13 | #include <asm/cache.h> |
2f16ac9d AB |
14 | |
15 | /* Bit values in IC_CTRL */ | |
16 | #define IC_CTRL_CACHE_DISABLE (1 << 0) | |
17 | ||
18 | /* Bit values in DC_CTRL */ | |
19 | #define DC_CTRL_CACHE_DISABLE (1 << 0) | |
20 | #define DC_CTRL_INV_MODE_FLUSH (1 << 6) | |
21 | #define DC_CTRL_FLUSH_STATUS (1 << 8) | |
f8cf3d1e | 22 | #define CACHE_VER_NUM_MASK 0xF |
6eb15e50 | 23 | #define SLC_CTRL_SB (1 << 2) |
2f16ac9d | 24 | |
ef639e6f AB |
25 | #define OP_INV 0x1 |
26 | #define OP_FLUSH 0x2 | |
27 | #define OP_INV_IC 0x3 | |
28 | ||
ef639e6f AB |
29 | /* |
30 | * By default that variable will fall into .bss section. | |
31 | * But .bss section is not relocated and so it will be initilized before | |
32 | * relocation but will be used after being zeroed. | |
33 | */ | |
379b3280 AB |
34 | int l1_line_sz __section(".data"); |
35 | int dcache_exists __section(".data"); | |
36 | int icache_exists __section(".data"); | |
37 | ||
38 | #define CACHE_LINE_MASK (~(l1_line_sz - 1)) | |
39 | ||
40 | #ifdef CONFIG_ISA_ARCV2 | |
ef639e6f AB |
41 | int slc_line_sz __section(".data"); |
42 | int slc_exists __section(".data"); | |
db6ce231 | 43 | int ioc_exists __section(".data"); |
ef639e6f AB |
44 | |
45 | static unsigned int __before_slc_op(const int op) | |
46 | { | |
47 | unsigned int reg = reg; | |
48 | ||
49 | if (op == OP_INV) { | |
50 | /* | |
51 | * IM is set by default and implies Flush-n-inv | |
52 | * Clear it here for vanilla inv | |
53 | */ | |
54 | reg = read_aux_reg(ARC_AUX_SLC_CTRL); | |
55 | write_aux_reg(ARC_AUX_SLC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH); | |
56 | } | |
57 | ||
58 | return reg; | |
59 | } | |
60 | ||
61 | static void __after_slc_op(const int op, unsigned int reg) | |
62 | { | |
40a808f1 AB |
63 | if (op & OP_FLUSH) { /* flush / flush-n-inv both wait */ |
64 | /* | |
65 | * Make sure "busy" bit reports correct status, | |
66 | * see STAR 9001165532 | |
67 | */ | |
68 | read_aux_reg(ARC_AUX_SLC_CTRL); | |
ef639e6f AB |
69 | while (read_aux_reg(ARC_AUX_SLC_CTRL) & |
70 | DC_CTRL_FLUSH_STATUS) | |
71 | ; | |
40a808f1 | 72 | } |
ef639e6f AB |
73 | |
74 | /* Switch back to default Invalidate mode */ | |
75 | if (op == OP_INV) | |
76 | write_aux_reg(ARC_AUX_SLC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH); | |
77 | } | |
78 | ||
79 | static inline void __slc_line_loop(unsigned long paddr, unsigned long sz, | |
80 | const int op) | |
81 | { | |
82 | unsigned int aux_cmd; | |
83 | int num_lines; | |
84 | ||
85 | #define SLC_LINE_MASK (~(slc_line_sz - 1)) | |
86 | ||
87 | aux_cmd = op & OP_INV ? ARC_AUX_SLC_IVDL : ARC_AUX_SLC_FLDL; | |
88 | ||
89 | sz += paddr & ~SLC_LINE_MASK; | |
90 | paddr &= SLC_LINE_MASK; | |
91 | ||
92 | num_lines = DIV_ROUND_UP(sz, slc_line_sz); | |
93 | ||
94 | while (num_lines-- > 0) { | |
95 | write_aux_reg(aux_cmd, paddr); | |
96 | paddr += slc_line_sz; | |
97 | } | |
98 | } | |
99 | ||
100 | static inline void __slc_entire_op(const int cacheop) | |
101 | { | |
102 | int aux; | |
103 | unsigned int ctrl_reg = __before_slc_op(cacheop); | |
104 | ||
105 | if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */ | |
106 | aux = ARC_AUX_SLC_INVALIDATE; | |
107 | else | |
108 | aux = ARC_AUX_SLC_FLUSH; | |
109 | ||
110 | write_aux_reg(aux, 0x1); | |
111 | ||
112 | __after_slc_op(cacheop, ctrl_reg); | |
113 | } | |
114 | ||
115 | static inline void __slc_line_op(unsigned long paddr, unsigned long sz, | |
116 | const int cacheop) | |
117 | { | |
118 | unsigned int ctrl_reg = __before_slc_op(cacheop); | |
119 | __slc_line_loop(paddr, sz, cacheop); | |
120 | __after_slc_op(cacheop, ctrl_reg); | |
121 | } | |
122 | #else | |
123 | #define __slc_entire_op(cacheop) | |
124 | #define __slc_line_op(paddr, sz, cacheop) | |
125 | #endif | |
126 | ||
379b3280 AB |
127 | #ifdef CONFIG_ISA_ARCV2 |
128 | static void read_decode_cache_bcr_arcv2(void) | |
ef639e6f | 129 | { |
379b3280 AB |
130 | union { |
131 | struct { | |
132 | #ifdef CONFIG_CPU_BIG_ENDIAN | |
133 | unsigned int pad:24, way:2, lsz:2, sz:4; | |
134 | #else | |
135 | unsigned int sz:4, lsz:2, way:2, pad:24; | |
136 | #endif | |
137 | } fields; | |
138 | unsigned int word; | |
139 | } slc_cfg; | |
140 | ||
141 | union { | |
142 | struct { | |
143 | #ifdef CONFIG_CPU_BIG_ENDIAN | |
144 | unsigned int pad:24, ver:8; | |
145 | #else | |
146 | unsigned int ver:8, pad:24; | |
147 | #endif | |
148 | } fields; | |
149 | unsigned int word; | |
150 | } sbcr; | |
151 | ||
152 | sbcr.word = read_aux_reg(ARC_BCR_SLC); | |
153 | if (sbcr.fields.ver) { | |
154 | slc_cfg.word = read_aux_reg(ARC_AUX_SLC_CONFIG); | |
155 | slc_exists = 1; | |
156 | slc_line_sz = (slc_cfg.fields.lsz == 0) ? 128 : 64; | |
157 | } | |
db6ce231 AB |
158 | |
159 | union { | |
160 | struct bcr_clust_cfg { | |
161 | #ifdef CONFIG_CPU_BIG_ENDIAN | |
162 | unsigned int pad:7, c:1, num_entries:8, num_cores:8, ver:8; | |
163 | #else | |
164 | unsigned int ver:8, num_cores:8, num_entries:8, c:1, pad:7; | |
165 | #endif | |
166 | } fields; | |
167 | unsigned int word; | |
168 | } cbcr; | |
169 | ||
170 | cbcr.word = read_aux_reg(ARC_BCR_CLUSTER); | |
171 | if (cbcr.fields.c) | |
172 | ioc_exists = 1; | |
ef639e6f | 173 | } |
379b3280 | 174 | #endif |
ef639e6f | 175 | |
379b3280 | 176 | void read_decode_cache_bcr(void) |
ef639e6f | 177 | { |
379b3280 AB |
178 | int dc_line_sz = 0, ic_line_sz = 0; |
179 | ||
180 | union { | |
181 | struct { | |
182 | #ifdef CONFIG_CPU_BIG_ENDIAN | |
183 | unsigned int pad:12, line_len:4, sz:4, config:4, ver:8; | |
184 | #else | |
185 | unsigned int ver:8, config:4, sz:4, line_len:4, pad:12; | |
186 | #endif | |
187 | } fields; | |
188 | unsigned int word; | |
189 | } ibcr, dbcr; | |
190 | ||
191 | ibcr.word = read_aux_reg(ARC_BCR_IC_BUILD); | |
192 | if (ibcr.fields.ver) { | |
193 | icache_exists = 1; | |
194 | l1_line_sz = ic_line_sz = 8 << ibcr.fields.line_len; | |
195 | if (!ic_line_sz) | |
196 | panic("Instruction exists but line length is 0\n"); | |
197 | } | |
198 | ||
199 | dbcr.word = read_aux_reg(ARC_BCR_DC_BUILD); | |
200 | if (dbcr.fields.ver){ | |
201 | dcache_exists = 1; | |
202 | l1_line_sz = dc_line_sz = 16 << dbcr.fields.line_len; | |
203 | if (!dc_line_sz) | |
204 | panic("Data cache exists but line length is 0\n"); | |
205 | } | |
206 | ||
207 | if (ic_line_sz && dc_line_sz && (ic_line_sz != dc_line_sz)) | |
208 | panic("Instruction and data cache line lengths differ\n"); | |
ef639e6f AB |
209 | } |
210 | ||
211 | void cache_init(void) | |
212 | { | |
379b3280 AB |
213 | read_decode_cache_bcr(); |
214 | ||
ef639e6f | 215 | #ifdef CONFIG_ISA_ARCV2 |
379b3280 | 216 | read_decode_cache_bcr_arcv2(); |
db6ce231 AB |
217 | |
218 | if (ioc_exists) { | |
97a63144 AB |
219 | /* IOC Aperture start is equal to DDR start */ |
220 | unsigned int ap_base = CONFIG_SYS_SDRAM_BASE; | |
221 | /* IOC Aperture size is equal to DDR size */ | |
222 | long ap_size = CONFIG_SYS_SDRAM_SIZE; | |
223 | ||
a4a43fcf AB |
224 | flush_dcache_all(); |
225 | invalidate_dcache_all(); | |
226 | ||
97a63144 AB |
227 | if (!is_power_of_2(ap_size) || ap_size < 4096) |
228 | panic("IOC Aperture size must be power of 2 and bigger 4Kib"); | |
229 | ||
230 | /* | |
231 | * IOC Aperture size decoded as 2 ^ (SIZE + 2) KB, | |
232 | * so setting 0x11 implies 512M, 0x12 implies 1G... | |
233 | */ | |
234 | write_aux_reg(ARC_AUX_IO_COH_AP0_SIZE, | |
235 | order_base_2(ap_size/1024) - 2); | |
236 | ||
237 | ||
238 | /* IOC Aperture start must be aligned to the size of the aperture */ | |
239 | if (ap_base % ap_size != 0) | |
240 | panic("IOC Aperture start must be aligned to the size of the aperture"); | |
241 | ||
242 | write_aux_reg(ARC_AUX_IO_COH_AP0_BASE, ap_base >> 12); | |
db6ce231 | 243 | write_aux_reg(ARC_AUX_IO_COH_PARTIAL, 1); |
db6ce231 | 244 | write_aux_reg(ARC_AUX_IO_COH_ENABLE, 1); |
97a63144 | 245 | |
db6ce231 | 246 | } |
ef639e6f AB |
247 | #endif |
248 | } | |
249 | ||
2f16ac9d AB |
250 | int icache_status(void) |
251 | { | |
379b3280 | 252 | if (!icache_exists) |
f8cf3d1e IG |
253 | return 0; |
254 | ||
ef639e6f AB |
255 | if (read_aux_reg(ARC_AUX_IC_CTRL) & IC_CTRL_CACHE_DISABLE) |
256 | return 0; | |
257 | else | |
258 | return 1; | |
2f16ac9d AB |
259 | } |
260 | ||
261 | void icache_enable(void) | |
262 | { | |
379b3280 | 263 | if (icache_exists) |
ef639e6f AB |
264 | write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) & |
265 | ~IC_CTRL_CACHE_DISABLE); | |
2f16ac9d AB |
266 | } |
267 | ||
268 | void icache_disable(void) | |
269 | { | |
379b3280 | 270 | if (icache_exists) |
ef639e6f AB |
271 | write_aux_reg(ARC_AUX_IC_CTRL, read_aux_reg(ARC_AUX_IC_CTRL) | |
272 | IC_CTRL_CACHE_DISABLE); | |
2f16ac9d AB |
273 | } |
274 | ||
ef639e6f | 275 | #ifndef CONFIG_SYS_DCACHE_OFF |
2f16ac9d AB |
276 | void invalidate_icache_all(void) |
277 | { | |
2f16ac9d | 278 | /* Any write to IC_IVIC register triggers invalidation of entire I$ */ |
ef639e6f AB |
279 | if (icache_status()) { |
280 | write_aux_reg(ARC_AUX_IC_IVIC, 1); | |
281 | read_aux_reg(ARC_AUX_IC_CTRL); /* blocks */ | |
282 | } | |
2f16ac9d | 283 | } |
ef639e6f AB |
284 | #else |
285 | void invalidate_icache_all(void) | |
286 | { | |
287 | } | |
288 | #endif | |
2f16ac9d AB |
289 | |
290 | int dcache_status(void) | |
291 | { | |
379b3280 | 292 | if (!dcache_exists) |
f8cf3d1e IG |
293 | return 0; |
294 | ||
ef639e6f AB |
295 | if (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_CACHE_DISABLE) |
296 | return 0; | |
297 | else | |
298 | return 1; | |
2f16ac9d AB |
299 | } |
300 | ||
301 | void dcache_enable(void) | |
302 | { | |
379b3280 | 303 | if (!dcache_exists) |
f8cf3d1e IG |
304 | return; |
305 | ||
2f16ac9d AB |
306 | write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) & |
307 | ~(DC_CTRL_INV_MODE_FLUSH | DC_CTRL_CACHE_DISABLE)); | |
308 | } | |
309 | ||
310 | void dcache_disable(void) | |
311 | { | |
379b3280 | 312 | if (!dcache_exists) |
f8cf3d1e IG |
313 | return; |
314 | ||
2f16ac9d AB |
315 | write_aux_reg(ARC_AUX_DC_CTRL, read_aux_reg(ARC_AUX_DC_CTRL) | |
316 | DC_CTRL_CACHE_DISABLE); | |
317 | } | |
318 | ||
2f16ac9d | 319 | #ifndef CONFIG_SYS_DCACHE_OFF |
ef639e6f AB |
320 | /* |
321 | * Common Helper for Line Operations on {I,D}-Cache | |
322 | */ | |
323 | static inline void __cache_line_loop(unsigned long paddr, unsigned long sz, | |
324 | const int cacheop) | |
2f16ac9d | 325 | { |
ef639e6f | 326 | unsigned int aux_cmd; |
5ff40f3d | 327 | #if (CONFIG_ARC_MMU_VER == 3) |
ef639e6f | 328 | unsigned int aux_tag; |
2f16ac9d | 329 | #endif |
ef639e6f | 330 | int num_lines; |
2f16ac9d | 331 | |
ef639e6f AB |
332 | if (cacheop == OP_INV_IC) { |
333 | aux_cmd = ARC_AUX_IC_IVIL; | |
5ff40f3d | 334 | #if (CONFIG_ARC_MMU_VER == 3) |
ef639e6f | 335 | aux_tag = ARC_AUX_IC_PTAG; |
2f16ac9d | 336 | #endif |
ef639e6f AB |
337 | } else { |
338 | /* d$ cmd: INV (discard or wback-n-discard) OR FLUSH (wback) */ | |
339 | aux_cmd = cacheop & OP_INV ? ARC_AUX_DC_IVDL : ARC_AUX_DC_FLDL; | |
340 | #if (CONFIG_ARC_MMU_VER == 3) | |
341 | aux_tag = ARC_AUX_DC_PTAG; | |
342 | #endif | |
343 | } | |
2f16ac9d | 344 | |
ef639e6f AB |
345 | sz += paddr & ~CACHE_LINE_MASK; |
346 | paddr &= CACHE_LINE_MASK; | |
2f16ac9d | 347 | |
379b3280 | 348 | num_lines = DIV_ROUND_UP(sz, l1_line_sz); |
2f16ac9d | 349 | |
ef639e6f | 350 | while (num_lines-- > 0) { |
5ff40f3d | 351 | #if (CONFIG_ARC_MMU_VER == 3) |
ef639e6f | 352 | write_aux_reg(aux_tag, paddr); |
2f16ac9d | 353 | #endif |
ef639e6f | 354 | write_aux_reg(aux_cmd, paddr); |
379b3280 | 355 | paddr += l1_line_sz; |
2f16ac9d | 356 | } |
2f16ac9d AB |
357 | } |
358 | ||
ef639e6f | 359 | static unsigned int __before_dc_op(const int op) |
2f16ac9d | 360 | { |
ef639e6f AB |
361 | unsigned int reg; |
362 | ||
363 | if (op == OP_INV) { | |
364 | /* | |
365 | * IM is set by default and implies Flush-n-inv | |
366 | * Clear it here for vanilla inv | |
367 | */ | |
368 | reg = read_aux_reg(ARC_AUX_DC_CTRL); | |
369 | write_aux_reg(ARC_AUX_DC_CTRL, reg & ~DC_CTRL_INV_MODE_FLUSH); | |
370 | } | |
ae4a351a | 371 | |
ef639e6f | 372 | return reg; |
2f16ac9d AB |
373 | } |
374 | ||
ef639e6f | 375 | static void __after_dc_op(const int op, unsigned int reg) |
2f16ac9d | 376 | { |
ef639e6f AB |
377 | if (op & OP_FLUSH) /* flush / flush-n-inv both wait */ |
378 | while (read_aux_reg(ARC_AUX_DC_CTRL) & DC_CTRL_FLUSH_STATUS) | |
379 | ; | |
380 | ||
381 | /* Switch back to default Invalidate mode */ | |
382 | if (op == OP_INV) | |
383 | write_aux_reg(ARC_AUX_DC_CTRL, reg | DC_CTRL_INV_MODE_FLUSH); | |
2f16ac9d | 384 | } |
6eb15e50 | 385 | |
ef639e6f | 386 | static inline void __dc_entire_op(const int cacheop) |
6eb15e50 | 387 | { |
ef639e6f AB |
388 | int aux; |
389 | unsigned int ctrl_reg = __before_dc_op(cacheop); | |
6eb15e50 | 390 | |
ef639e6f AB |
391 | if (cacheop & OP_INV) /* Inv or flush-n-inv use same cmd reg */ |
392 | aux = ARC_AUX_DC_IVDC; | |
393 | else | |
394 | aux = ARC_AUX_DC_FLSH; | |
6eb15e50 | 395 | |
ef639e6f | 396 | write_aux_reg(aux, 0x1); |
6eb15e50 | 397 | |
ef639e6f | 398 | __after_dc_op(cacheop, ctrl_reg); |
6eb15e50 AB |
399 | } |
400 | ||
ef639e6f AB |
401 | static inline void __dc_line_op(unsigned long paddr, unsigned long sz, |
402 | const int cacheop) | |
6eb15e50 | 403 | { |
ef639e6f AB |
404 | unsigned int ctrl_reg = __before_dc_op(cacheop); |
405 | __cache_line_loop(paddr, sz, cacheop); | |
406 | __after_dc_op(cacheop, ctrl_reg); | |
407 | } | |
408 | #else | |
409 | #define __dc_entire_op(cacheop) | |
410 | #define __dc_line_op(paddr, sz, cacheop) | |
411 | #endif /* !CONFIG_SYS_DCACHE_OFF */ | |
6eb15e50 | 412 | |
ef639e6f AB |
413 | void invalidate_dcache_range(unsigned long start, unsigned long end) |
414 | { | |
ef639e6f | 415 | #ifdef CONFIG_ISA_ARCV2 |
db6ce231 AB |
416 | if (!ioc_exists) |
417 | #endif | |
418 | __dc_line_op(start, end - start, OP_INV); | |
419 | ||
420 | #ifdef CONFIG_ISA_ARCV2 | |
421 | if (slc_exists && !ioc_exists) | |
ef639e6f AB |
422 | __slc_line_op(start, end - start, OP_INV); |
423 | #endif | |
424 | } | |
6eb15e50 | 425 | |
ef639e6f AB |
426 | void flush_dcache_range(unsigned long start, unsigned long end) |
427 | { | |
ef639e6f | 428 | #ifdef CONFIG_ISA_ARCV2 |
db6ce231 AB |
429 | if (!ioc_exists) |
430 | #endif | |
431 | __dc_line_op(start, end - start, OP_FLUSH); | |
432 | ||
433 | #ifdef CONFIG_ISA_ARCV2 | |
434 | if (slc_exists && !ioc_exists) | |
ef639e6f AB |
435 | __slc_line_op(start, end - start, OP_FLUSH); |
436 | #endif | |
6eb15e50 AB |
437 | } |
438 | ||
ef639e6f | 439 | void flush_cache(unsigned long start, unsigned long size) |
6eb15e50 | 440 | { |
ef639e6f AB |
441 | flush_dcache_range(start, start + size); |
442 | } | |
6eb15e50 | 443 | |
ef639e6f AB |
444 | void invalidate_dcache_all(void) |
445 | { | |
bd91508b | 446 | __dc_entire_op(OP_INV); |
db6ce231 AB |
447 | |
448 | #ifdef CONFIG_ISA_ARCV2 | |
bd91508b | 449 | if (slc_exists) |
ef639e6f AB |
450 | __slc_entire_op(OP_INV); |
451 | #endif | |
6eb15e50 AB |
452 | } |
453 | ||
ef639e6f AB |
454 | void flush_dcache_all(void) |
455 | { | |
2a8382c6 | 456 | __dc_entire_op(OP_FLUSH); |
db6ce231 AB |
457 | |
458 | #ifdef CONFIG_ISA_ARCV2 | |
2a8382c6 | 459 | if (slc_exists) |
ef639e6f AB |
460 | __slc_entire_op(OP_FLUSH); |
461 | #endif | |
462 | } |