1 /*
2 * arch/arm/mm/cache-l2x0.c - L210/L220 cache controller support
3 *
4 * Copyright (C) 2007 ARM Limited
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 #include <linux/cpu.h>
20 #include <linux/err.h>
21 #include <linux/init.h>
22 #include <linux/smp.h>
23 #include <linux/spinlock.h>
24 #include <linux/log2.h>
25 #include <linux/io.h>
26 #include <linux/of.h>
27 #include <linux/of_address.h>
28
29 #include <asm/cacheflush.h>
30 #include <asm/cp15.h>
31 #include <asm/cputype.h>
32 #include <asm/hardware/cache-l2x0.h>
33 #include "cache-tauros3.h"
34 #include "cache-aurora-l2.h"
35
36 struct l2c_init_data {
37 const char *type;
38 unsigned way_size_0;
39 unsigned num_lock;
40 void (*of_parse)(const struct device_node *, u32 *, u32 *);
41 void (*enable)(void __iomem *, u32, unsigned);
42 void (*fixup)(void __iomem *, u32, struct outer_cache_fns *);
43 void (*save)(void __iomem *);
44 struct outer_cache_fns outer_cache;
45 };
46
47 #define CACHE_LINE_SIZE 32
48
49 static void __iomem *l2x0_base;
50 static DEFINE_RAW_SPINLOCK(l2x0_lock);
51 static u32 l2x0_way_mask; /* Bitmask of active ways */
52 static u32 l2x0_size;
53 static unsigned long sync_reg_offset = L2X0_CACHE_SYNC;
54
55 struct l2x0_regs l2x0_saved_regs;
56
57 /*
58 * Common code for all cache controllers.
59 */
l2c_wait_mask(void __iomem * reg,unsigned long mask)60 static inline void l2c_wait_mask(void __iomem *reg, unsigned long mask)
61 {
62 /* wait for cache operation by line or way to complete */
63 while (readl_relaxed(reg) & mask)
64 cpu_relax();
65 }
66
67 /*
68 * By default, we write directly to secure registers. Platforms must
69 * override this if they are running non-secure.
70 */
l2c_write_sec(unsigned long val,void __iomem * base,unsigned reg)71 static void l2c_write_sec(unsigned long val, void __iomem *base, unsigned reg)
72 {
73 if (val == readl_relaxed(base + reg))
74 return;
75 if (outer_cache.write_sec)
76 outer_cache.write_sec(val, reg);
77 else
78 writel_relaxed(val, base + reg);
79 }
80
81 /*
82 * This should only be called when we have a requirement that the
83 * register be written due to a work-around, as platforms running
84 * in non-secure mode may not be able to access this register.
85 */
l2c_set_debug(void __iomem * base,unsigned long val)86 static inline void l2c_set_debug(void __iomem *base, unsigned long val)
87 {
88 l2c_write_sec(val, base, L2X0_DEBUG_CTRL);
89 }
90
__l2c_op_way(void __iomem * reg)91 static void __l2c_op_way(void __iomem *reg)
92 {
93 writel_relaxed(l2x0_way_mask, reg);
94 l2c_wait_mask(reg, l2x0_way_mask);
95 }
96
l2c_unlock(void __iomem * base,unsigned num)97 static inline void l2c_unlock(void __iomem *base, unsigned num)
98 {
99 unsigned i;
100
101 for (i = 0; i < num; i++) {
102 writel_relaxed(0, base + L2X0_LOCKDOWN_WAY_D_BASE +
103 i * L2X0_LOCKDOWN_STRIDE);
104 writel_relaxed(0, base + L2X0_LOCKDOWN_WAY_I_BASE +
105 i * L2X0_LOCKDOWN_STRIDE);
106 }
107 }
108
109 /*
110 * Enable the L2 cache controller. This function must only be
111 * called when the cache controller is known to be disabled.
112 */
l2c_enable(void __iomem * base,u32 aux,unsigned num_lock)113 static void l2c_enable(void __iomem *base, u32 aux, unsigned num_lock)
114 {
115 unsigned long flags;
116
117 l2c_write_sec(aux, base, L2X0_AUX_CTRL);
118
119 l2c_unlock(base, num_lock);
120
121 local_irq_save(flags);
122 __l2c_op_way(base + L2X0_INV_WAY);
123 writel_relaxed(0, base + sync_reg_offset);
124 l2c_wait_mask(base + sync_reg_offset, 1);
125 local_irq_restore(flags);
126
127 l2c_write_sec(L2X0_CTRL_EN, base, L2X0_CTRL);
128 }
129
l2c_disable(void)130 static void l2c_disable(void)
131 {
132 void __iomem *base = l2x0_base;
133
134 outer_cache.flush_all();
135 l2c_write_sec(0, base, L2X0_CTRL);
136 dsb(st);
137 }
138
l2c_save(void __iomem * base)139 static void l2c_save(void __iomem *base)
140 {
141 l2x0_saved_regs.aux_ctrl = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
142 }
143
144 /*
145 * L2C-210 specific code.
146 *
147 * The L2C-2x0 PA, set/way and sync operations are atomic, but we must
148 * ensure that no background operation is running. The way operations
149 * are all background tasks.
150 *
151 * While a background operation is in progress, any new operation is
152 * ignored (unspecified whether this causes an error.) Thankfully, not
153 * used on SMP.
154 *
155 * Never has a different sync register other than L2X0_CACHE_SYNC, but
156 * we use sync_reg_offset here so we can share some of this with L2C-310.
157 */
__l2c210_cache_sync(void __iomem * base)158 static void __l2c210_cache_sync(void __iomem *base)
159 {
160 writel_relaxed(0, base + sync_reg_offset);
161 }
162
__l2c210_op_pa_range(void __iomem * reg,unsigned long start,unsigned long end)163 static void __l2c210_op_pa_range(void __iomem *reg, unsigned long start,
164 unsigned long end)
165 {
166 while (start < end) {
167 writel_relaxed(start, reg);
168 start += CACHE_LINE_SIZE;
169 }
170 }
171
l2c210_inv_range(unsigned long start,unsigned long end)172 static void l2c210_inv_range(unsigned long start, unsigned long end)
173 {
174 void __iomem *base = l2x0_base;
175
176 if (start & (CACHE_LINE_SIZE - 1)) {
177 start &= ~(CACHE_LINE_SIZE - 1);
178 writel_relaxed(start, base + L2X0_CLEAN_INV_LINE_PA);
179 start += CACHE_LINE_SIZE;
180 }
181
182 if (end & (CACHE_LINE_SIZE - 1)) {
183 end &= ~(CACHE_LINE_SIZE - 1);
184 writel_relaxed(end, base + L2X0_CLEAN_INV_LINE_PA);
185 }
186
187 __l2c210_op_pa_range(base + L2X0_INV_LINE_PA, start, end);
188 __l2c210_cache_sync(base);
189 }
190
l2c210_clean_range(unsigned long start,unsigned long end)191 static void l2c210_clean_range(unsigned long start, unsigned long end)
192 {
193 void __iomem *base = l2x0_base;
194
195 start &= ~(CACHE_LINE_SIZE - 1);
196 __l2c210_op_pa_range(base + L2X0_CLEAN_LINE_PA, start, end);
197 __l2c210_cache_sync(base);
198 }
199
l2c210_flush_range(unsigned long start,unsigned long end)200 static void l2c210_flush_range(unsigned long start, unsigned long end)
201 {
202 void __iomem *base = l2x0_base;
203
204 start &= ~(CACHE_LINE_SIZE - 1);
205 __l2c210_op_pa_range(base + L2X0_CLEAN_INV_LINE_PA, start, end);
206 __l2c210_cache_sync(base);
207 }
208
l2c210_flush_all(void)209 static void l2c210_flush_all(void)
210 {
211 void __iomem *base = l2x0_base;
212
213 BUG_ON(!irqs_disabled());
214
215 __l2c_op_way(base + L2X0_CLEAN_INV_WAY);
216 __l2c210_cache_sync(base);
217 }
218
l2c210_sync(void)219 static void l2c210_sync(void)
220 {
221 __l2c210_cache_sync(l2x0_base);
222 }
223
l2c210_resume(void)224 static void l2c210_resume(void)
225 {
226 void __iomem *base = l2x0_base;
227
228 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN))
229 l2c_enable(base, l2x0_saved_regs.aux_ctrl, 1);
230 }
231
232 static const struct l2c_init_data l2c210_data __initconst = {
233 .type = "L2C-210",
234 .way_size_0 = SZ_8K,
235 .num_lock = 1,
236 .enable = l2c_enable,
237 .save = l2c_save,
238 .outer_cache = {
239 .inv_range = l2c210_inv_range,
240 .clean_range = l2c210_clean_range,
241 .flush_range = l2c210_flush_range,
242 .flush_all = l2c210_flush_all,
243 .disable = l2c_disable,
244 .sync = l2c210_sync,
245 .resume = l2c210_resume,
246 },
247 };
248
249 /*
250 * L2C-220 specific code.
251 *
252 * All operations are background operations: they have to be waited for.
253 * Conflicting requests generate a slave error (which will cause an
254 * imprecise abort.) Never uses sync_reg_offset, so we hard-code the
255 * sync register here.
256 *
257 * However, we can re-use the l2c210_resume call.
258 */
__l2c220_cache_sync(void __iomem * base)259 static inline void __l2c220_cache_sync(void __iomem *base)
260 {
261 writel_relaxed(0, base + L2X0_CACHE_SYNC);
262 l2c_wait_mask(base + L2X0_CACHE_SYNC, 1);
263 }
264
l2c220_op_way(void __iomem * base,unsigned reg)265 static void l2c220_op_way(void __iomem *base, unsigned reg)
266 {
267 unsigned long flags;
268
269 raw_spin_lock_irqsave(&l2x0_lock, flags);
270 __l2c_op_way(base + reg);
271 __l2c220_cache_sync(base);
272 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
273 }
274
l2c220_op_pa_range(void __iomem * reg,unsigned long start,unsigned long end,unsigned long flags)275 static unsigned long l2c220_op_pa_range(void __iomem *reg, unsigned long start,
276 unsigned long end, unsigned long flags)
277 {
278 raw_spinlock_t *lock = &l2x0_lock;
279
280 while (start < end) {
281 unsigned long blk_end = start + min(end - start, 4096UL);
282
283 while (start < blk_end) {
284 l2c_wait_mask(reg, 1);
285 writel_relaxed(start, reg);
286 start += CACHE_LINE_SIZE;
287 }
288
289 if (blk_end < end) {
290 raw_spin_unlock_irqrestore(lock, flags);
291 raw_spin_lock_irqsave(lock, flags);
292 }
293 }
294
295 return flags;
296 }
297
l2c220_inv_range(unsigned long start,unsigned long end)298 static void l2c220_inv_range(unsigned long start, unsigned long end)
299 {
300 void __iomem *base = l2x0_base;
301 unsigned long flags;
302
303 raw_spin_lock_irqsave(&l2x0_lock, flags);
304 if ((start | end) & (CACHE_LINE_SIZE - 1)) {
305 if (start & (CACHE_LINE_SIZE - 1)) {
306 start &= ~(CACHE_LINE_SIZE - 1);
307 writel_relaxed(start, base + L2X0_CLEAN_INV_LINE_PA);
308 start += CACHE_LINE_SIZE;
309 }
310
311 if (end & (CACHE_LINE_SIZE - 1)) {
312 end &= ~(CACHE_LINE_SIZE - 1);
313 l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
314 writel_relaxed(end, base + L2X0_CLEAN_INV_LINE_PA);
315 }
316 }
317
318 flags = l2c220_op_pa_range(base + L2X0_INV_LINE_PA,
319 start, end, flags);
320 l2c_wait_mask(base + L2X0_INV_LINE_PA, 1);
321 __l2c220_cache_sync(base);
322 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
323 }
324
l2c220_clean_range(unsigned long start,unsigned long end)325 static void l2c220_clean_range(unsigned long start, unsigned long end)
326 {
327 void __iomem *base = l2x0_base;
328 unsigned long flags;
329
330 start &= ~(CACHE_LINE_SIZE - 1);
331 if ((end - start) >= l2x0_size) {
332 l2c220_op_way(base, L2X0_CLEAN_WAY);
333 return;
334 }
335
336 raw_spin_lock_irqsave(&l2x0_lock, flags);
337 flags = l2c220_op_pa_range(base + L2X0_CLEAN_LINE_PA,
338 start, end, flags);
339 l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
340 __l2c220_cache_sync(base);
341 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
342 }
343
l2c220_flush_range(unsigned long start,unsigned long end)344 static void l2c220_flush_range(unsigned long start, unsigned long end)
345 {
346 void __iomem *base = l2x0_base;
347 unsigned long flags;
348
349 start &= ~(CACHE_LINE_SIZE - 1);
350 if ((end - start) >= l2x0_size) {
351 l2c220_op_way(base, L2X0_CLEAN_INV_WAY);
352 return;
353 }
354
355 raw_spin_lock_irqsave(&l2x0_lock, flags);
356 flags = l2c220_op_pa_range(base + L2X0_CLEAN_INV_LINE_PA,
357 start, end, flags);
358 l2c_wait_mask(base + L2X0_CLEAN_INV_LINE_PA, 1);
359 __l2c220_cache_sync(base);
360 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
361 }
362
l2c220_flush_all(void)363 static void l2c220_flush_all(void)
364 {
365 l2c220_op_way(l2x0_base, L2X0_CLEAN_INV_WAY);
366 }
367
l2c220_sync(void)368 static void l2c220_sync(void)
369 {
370 unsigned long flags;
371
372 raw_spin_lock_irqsave(&l2x0_lock, flags);
373 __l2c220_cache_sync(l2x0_base);
374 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
375 }
376
l2c220_enable(void __iomem * base,u32 aux,unsigned num_lock)377 static void l2c220_enable(void __iomem *base, u32 aux, unsigned num_lock)
378 {
379 /*
380 * Always enable non-secure access to the lockdown registers -
381 * we write to them as part of the L2C enable sequence so they
382 * need to be accessible.
383 */
384 aux |= L220_AUX_CTRL_NS_LOCKDOWN;
385
386 l2c_enable(base, aux, num_lock);
387 }
388
389 static const struct l2c_init_data l2c220_data = {
390 .type = "L2C-220",
391 .way_size_0 = SZ_8K,
392 .num_lock = 1,
393 .enable = l2c220_enable,
394 .save = l2c_save,
395 .outer_cache = {
396 .inv_range = l2c220_inv_range,
397 .clean_range = l2c220_clean_range,
398 .flush_range = l2c220_flush_range,
399 .flush_all = l2c220_flush_all,
400 .disable = l2c_disable,
401 .sync = l2c220_sync,
402 .resume = l2c210_resume,
403 },
404 };
405
406 /*
407 * L2C-310 specific code.
408 *
409 * Very similar to L2C-210, the PA, set/way and sync operations are atomic,
410 * and the way operations are all background tasks. However, issuing an
411 * operation while a background operation is in progress results in a
412 * SLVERR response. We can reuse:
413 *
414 * __l2c210_cache_sync (using sync_reg_offset)
415 * l2c210_sync
416 * l2c210_inv_range (if 588369 is not applicable)
417 * l2c210_clean_range
418 * l2c210_flush_range (if 588369 is not applicable)
419 * l2c210_flush_all (if 727915 is not applicable)
420 *
421 * Errata:
422 * 588369: PL310 R0P0->R1P0, fixed R2P0.
423 * Affects: all clean+invalidate operations
424 * clean and invalidate skips the invalidate step, so we need to issue
425 * separate operations. We also require the above debug workaround
426 * enclosing this code fragment on affected parts. On unaffected parts,
427 * we must not use this workaround without the debug register writes
428 * to avoid exposing a problem similar to 727915.
429 *
430 * 727915: PL310 R2P0->R3P0, fixed R3P1.
431 * Affects: clean+invalidate by way
432 * clean and invalidate by way runs in the background, and a store can
433 * hit the line between the clean operation and invalidate operation,
434 * resulting in the store being lost.
435 *
436 * 752271: PL310 R3P0->R3P1-50REL0, fixed R3P2.
437 * Affects: 8x64-bit (double fill) line fetches
438 * double fill line fetches can fail to cause dirty data to be evicted
439 * from the cache before the new data overwrites the second line.
440 *
441 * 753970: PL310 R3P0, fixed R3P1.
442 * Affects: sync
443 * prevents merging writes after the sync operation, until another L2C
444 * operation is performed (or a number of other conditions.)
445 *
446 * 769419: PL310 R0P0->R3P1, fixed R3P2.
447 * Affects: store buffer
448 * store buffer is not automatically drained.
449 */
l2c310_inv_range_erratum(unsigned long start,unsigned long end)450 static void l2c310_inv_range_erratum(unsigned long start, unsigned long end)
451 {
452 void __iomem *base = l2x0_base;
453
454 if ((start | end) & (CACHE_LINE_SIZE - 1)) {
455 unsigned long flags;
456
457 /* Erratum 588369 for both clean+invalidate operations */
458 raw_spin_lock_irqsave(&l2x0_lock, flags);
459 l2c_set_debug(base, 0x03);
460
461 if (start & (CACHE_LINE_SIZE - 1)) {
462 start &= ~(CACHE_LINE_SIZE - 1);
463 writel_relaxed(start, base + L2X0_CLEAN_LINE_PA);
464 writel_relaxed(start, base + L2X0_INV_LINE_PA);
465 start += CACHE_LINE_SIZE;
466 }
467
468 if (end & (CACHE_LINE_SIZE - 1)) {
469 end &= ~(CACHE_LINE_SIZE - 1);
470 writel_relaxed(end, base + L2X0_CLEAN_LINE_PA);
471 writel_relaxed(end, base + L2X0_INV_LINE_PA);
472 }
473
474 l2c_set_debug(base, 0x00);
475 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
476 }
477
478 __l2c210_op_pa_range(base + L2X0_INV_LINE_PA, start, end);
479 __l2c210_cache_sync(base);
480 }
481
l2c310_flush_range_erratum(unsigned long start,unsigned long end)482 static void l2c310_flush_range_erratum(unsigned long start, unsigned long end)
483 {
484 raw_spinlock_t *lock = &l2x0_lock;
485 unsigned long flags;
486 void __iomem *base = l2x0_base;
487
488 raw_spin_lock_irqsave(lock, flags);
489 while (start < end) {
490 unsigned long blk_end = start + min(end - start, 4096UL);
491
492 l2c_set_debug(base, 0x03);
493 while (start < blk_end) {
494 writel_relaxed(start, base + L2X0_CLEAN_LINE_PA);
495 writel_relaxed(start, base + L2X0_INV_LINE_PA);
496 start += CACHE_LINE_SIZE;
497 }
498 l2c_set_debug(base, 0x00);
499
500 if (blk_end < end) {
501 raw_spin_unlock_irqrestore(lock, flags);
502 raw_spin_lock_irqsave(lock, flags);
503 }
504 }
505 raw_spin_unlock_irqrestore(lock, flags);
506 __l2c210_cache_sync(base);
507 }
508
l2c310_flush_all_erratum(void)509 static void l2c310_flush_all_erratum(void)
510 {
511 void __iomem *base = l2x0_base;
512 unsigned long flags;
513
514 raw_spin_lock_irqsave(&l2x0_lock, flags);
515 l2c_set_debug(base, 0x03);
516 __l2c_op_way(base + L2X0_CLEAN_INV_WAY);
517 l2c_set_debug(base, 0x00);
518 __l2c210_cache_sync(base);
519 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
520 }
521
l2c310_save(void __iomem * base)522 static void __init l2c310_save(void __iomem *base)
523 {
524 unsigned revision;
525
526 l2c_save(base);
527
528 l2x0_saved_regs.tag_latency = readl_relaxed(base +
529 L310_TAG_LATENCY_CTRL);
530 l2x0_saved_regs.data_latency = readl_relaxed(base +
531 L310_DATA_LATENCY_CTRL);
532 l2x0_saved_regs.filter_end = readl_relaxed(base +
533 L310_ADDR_FILTER_END);
534 l2x0_saved_regs.filter_start = readl_relaxed(base +
535 L310_ADDR_FILTER_START);
536
537 revision = readl_relaxed(base + L2X0_CACHE_ID) &
538 L2X0_CACHE_ID_RTL_MASK;
539
540 /* From r2p0, there is Prefetch offset/control register */
541 if (revision >= L310_CACHE_ID_RTL_R2P0)
542 l2x0_saved_regs.prefetch_ctrl = readl_relaxed(base +
543 L310_PREFETCH_CTRL);
544
545 /* From r3p0, there is Power control register */
546 if (revision >= L310_CACHE_ID_RTL_R3P0)
547 l2x0_saved_regs.pwr_ctrl = readl_relaxed(base +
548 L310_POWER_CTRL);
549 }
550
l2c310_resume(void)551 static void l2c310_resume(void)
552 {
553 void __iomem *base = l2x0_base;
554
555 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
556 unsigned revision;
557
558 /* restore pl310 setup */
559 writel_relaxed(l2x0_saved_regs.tag_latency,
560 base + L310_TAG_LATENCY_CTRL);
561 writel_relaxed(l2x0_saved_regs.data_latency,
562 base + L310_DATA_LATENCY_CTRL);
563 writel_relaxed(l2x0_saved_regs.filter_end,
564 base + L310_ADDR_FILTER_END);
565 writel_relaxed(l2x0_saved_regs.filter_start,
566 base + L310_ADDR_FILTER_START);
567
568 revision = readl_relaxed(base + L2X0_CACHE_ID) &
569 L2X0_CACHE_ID_RTL_MASK;
570
571 if (revision >= L310_CACHE_ID_RTL_R2P0)
572 l2c_write_sec(l2x0_saved_regs.prefetch_ctrl, base,
573 L310_PREFETCH_CTRL);
574 if (revision >= L310_CACHE_ID_RTL_R3P0)
575 l2c_write_sec(l2x0_saved_regs.pwr_ctrl, base,
576 L310_POWER_CTRL);
577
578 l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
579
580 /* Re-enable full-line-of-zeros for Cortex-A9 */
581 if (l2x0_saved_regs.aux_ctrl & L310_AUX_CTRL_FULL_LINE_ZERO)
582 set_auxcr(get_auxcr() | BIT(3) | BIT(2) | BIT(1));
583 }
584 }
585
l2c310_cpu_enable_flz(struct notifier_block * nb,unsigned long act,void * data)586 static int l2c310_cpu_enable_flz(struct notifier_block *nb, unsigned long act, void *data)
587 {
588 switch (act & ~CPU_TASKS_FROZEN) {
589 case CPU_STARTING:
590 set_auxcr(get_auxcr() | BIT(3) | BIT(2) | BIT(1));
591 break;
592 case CPU_DYING:
593 set_auxcr(get_auxcr() & ~(BIT(3) | BIT(2) | BIT(1)));
594 break;
595 }
596 return NOTIFY_OK;
597 }
598
l2c310_enable(void __iomem * base,u32 aux,unsigned num_lock)599 static void __init l2c310_enable(void __iomem *base, u32 aux, unsigned num_lock)
600 {
601 unsigned rev = readl_relaxed(base + L2X0_CACHE_ID) & L2X0_CACHE_ID_RTL_MASK;
602 bool cortex_a9 = read_cpuid_part() == ARM_CPU_PART_CORTEX_A9;
603
604 if (rev >= L310_CACHE_ID_RTL_R2P0) {
605 if (cortex_a9) {
606 aux |= L310_AUX_CTRL_EARLY_BRESP;
607 pr_info("L2C-310 enabling early BRESP for Cortex-A9\n");
608 } else if (aux & L310_AUX_CTRL_EARLY_BRESP) {
609 pr_warn("L2C-310 early BRESP only supported with Cortex-A9\n");
610 aux &= ~L310_AUX_CTRL_EARLY_BRESP;
611 }
612 }
613
614 if (cortex_a9) {
615 u32 aux_cur = readl_relaxed(base + L2X0_AUX_CTRL);
616 u32 acr = get_auxcr();
617
618 pr_debug("Cortex-A9 ACR=0x%08x\n", acr);
619
620 if (acr & BIT(3) && !(aux_cur & L310_AUX_CTRL_FULL_LINE_ZERO))
621 pr_err("L2C-310: full line of zeros enabled in Cortex-A9 but not L2C-310 - invalid\n");
622
623 if (aux & L310_AUX_CTRL_FULL_LINE_ZERO && !(acr & BIT(3)))
624 pr_err("L2C-310: enabling full line of zeros but not enabled in Cortex-A9\n");
625
626 if (!(aux & L310_AUX_CTRL_FULL_LINE_ZERO) && !outer_cache.write_sec) {
627 aux |= L310_AUX_CTRL_FULL_LINE_ZERO;
628 pr_info("L2C-310 full line of zeros enabled for Cortex-A9\n");
629 }
630 } else if (aux & (L310_AUX_CTRL_FULL_LINE_ZERO | L310_AUX_CTRL_EARLY_BRESP)) {
631 pr_err("L2C-310: disabling Cortex-A9 specific feature bits\n");
632 aux &= ~(L310_AUX_CTRL_FULL_LINE_ZERO | L310_AUX_CTRL_EARLY_BRESP);
633 }
634
635 if (aux & (L310_AUX_CTRL_DATA_PREFETCH | L310_AUX_CTRL_INSTR_PREFETCH)) {
636 u32 prefetch = readl_relaxed(base + L310_PREFETCH_CTRL);
637
638 pr_info("L2C-310 %s%s prefetch enabled, offset %u lines\n",
639 aux & L310_AUX_CTRL_INSTR_PREFETCH ? "I" : "",
640 aux & L310_AUX_CTRL_DATA_PREFETCH ? "D" : "",
641 1 + (prefetch & L310_PREFETCH_CTRL_OFFSET_MASK));
642 }
643
644 /* r3p0 or later has power control register */
645 if (rev >= L310_CACHE_ID_RTL_R3P0) {
646 u32 power_ctrl;
647
648 l2c_write_sec(L310_DYNAMIC_CLK_GATING_EN | L310_STNDBY_MODE_EN,
649 base, L310_POWER_CTRL);
650 power_ctrl = readl_relaxed(base + L310_POWER_CTRL);
651 pr_info("L2C-310 dynamic clock gating %sabled, standby mode %sabled\n",
652 power_ctrl & L310_DYNAMIC_CLK_GATING_EN ? "en" : "dis",
653 power_ctrl & L310_STNDBY_MODE_EN ? "en" : "dis");
654 }
655
656 /*
657 * Always enable non-secure access to the lockdown registers -
658 * we write to them as part of the L2C enable sequence so they
659 * need to be accessible.
660 */
661 aux |= L310_AUX_CTRL_NS_LOCKDOWN;
662
663 l2c_enable(base, aux, num_lock);
664
665 if (aux & L310_AUX_CTRL_FULL_LINE_ZERO) {
666 set_auxcr(get_auxcr() | BIT(3) | BIT(2) | BIT(1));
667 cpu_notifier(l2c310_cpu_enable_flz, 0);
668 }
669 }
670
l2c310_fixup(void __iomem * base,u32 cache_id,struct outer_cache_fns * fns)671 static void __init l2c310_fixup(void __iomem *base, u32 cache_id,
672 struct outer_cache_fns *fns)
673 {
674 unsigned revision = cache_id & L2X0_CACHE_ID_RTL_MASK;
675 const char *errata[8];
676 unsigned n = 0;
677
678 if (IS_ENABLED(CONFIG_PL310_ERRATA_588369) &&
679 revision < L310_CACHE_ID_RTL_R2P0 &&
680 /* For bcm compatibility */
681 fns->inv_range == l2c210_inv_range) {
682 fns->inv_range = l2c310_inv_range_erratum;
683 fns->flush_range = l2c310_flush_range_erratum;
684 errata[n++] = "588369";
685 }
686
687 if (IS_ENABLED(CONFIG_PL310_ERRATA_727915) &&
688 revision >= L310_CACHE_ID_RTL_R2P0 &&
689 revision < L310_CACHE_ID_RTL_R3P1) {
690 fns->flush_all = l2c310_flush_all_erratum;
691 errata[n++] = "727915";
692 }
693
694 if (revision >= L310_CACHE_ID_RTL_R3P0 &&
695 revision < L310_CACHE_ID_RTL_R3P2) {
696 u32 val = readl_relaxed(base + L310_PREFETCH_CTRL);
697 /* I don't think bit23 is required here... but iMX6 does so */
698 if (val & (BIT(30) | BIT(23))) {
699 val &= ~(BIT(30) | BIT(23));
700 l2c_write_sec(val, base, L310_PREFETCH_CTRL);
701 errata[n++] = "752271";
702 }
703 }
704
705 if (IS_ENABLED(CONFIG_PL310_ERRATA_753970) &&
706 revision == L310_CACHE_ID_RTL_R3P0) {
707 sync_reg_offset = L2X0_DUMMY_REG;
708 errata[n++] = "753970";
709 }
710
711 if (IS_ENABLED(CONFIG_PL310_ERRATA_769419))
712 errata[n++] = "769419";
713
714 if (n) {
715 unsigned i;
716
717 pr_info("L2C-310 errat%s", n > 1 ? "a" : "um");
718 for (i = 0; i < n; i++)
719 pr_cont(" %s", errata[i]);
720 pr_cont(" enabled\n");
721 }
722 }
723
l2c310_disable(void)724 static void l2c310_disable(void)
725 {
726 /*
727 * If full-line-of-zeros is enabled, we must first disable it in the
728 * Cortex-A9 auxiliary control register before disabling the L2 cache.
729 */
730 if (l2x0_saved_regs.aux_ctrl & L310_AUX_CTRL_FULL_LINE_ZERO)
731 set_auxcr(get_auxcr() & ~(BIT(3) | BIT(2) | BIT(1)));
732
733 l2c_disable();
734 }
735
736 static const struct l2c_init_data l2c310_init_fns __initconst = {
737 .type = "L2C-310",
738 .way_size_0 = SZ_8K,
739 .num_lock = 8,
740 .enable = l2c310_enable,
741 .fixup = l2c310_fixup,
742 .save = l2c310_save,
743 .outer_cache = {
744 .inv_range = l2c210_inv_range,
745 .clean_range = l2c210_clean_range,
746 .flush_range = l2c210_flush_range,
747 .flush_all = l2c210_flush_all,
748 .disable = l2c310_disable,
749 .sync = l2c210_sync,
750 .resume = l2c310_resume,
751 },
752 };
753
__l2c_init(const struct l2c_init_data * data,u32 aux_val,u32 aux_mask,u32 cache_id)754 static void __init __l2c_init(const struct l2c_init_data *data,
755 u32 aux_val, u32 aux_mask, u32 cache_id)
756 {
757 struct outer_cache_fns fns;
758 unsigned way_size_bits, ways;
759 u32 aux, old_aux;
760
761 /*
762 * Sanity check the aux values. aux_mask is the bits we preserve
763 * from reading the hardware register, and aux_val is the bits we
764 * set.
765 */
766 if (aux_val & aux_mask)
767 pr_alert("L2C: platform provided aux values permit register corruption.\n");
768
769 old_aux = aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
770 aux &= aux_mask;
771 aux |= aux_val;
772
773 if (old_aux != aux)
774 pr_warn("L2C: DT/platform modifies aux control register: 0x%08x -> 0x%08x\n",
775 old_aux, aux);
776
777 /* Determine the number of ways */
778 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
779 case L2X0_CACHE_ID_PART_L310:
780 if ((aux_val | ~aux_mask) & (L2C_AUX_CTRL_WAY_SIZE_MASK | L310_AUX_CTRL_ASSOCIATIVITY_16))
781 pr_warn("L2C: DT/platform tries to modify or specify cache size\n");
782 if (aux & (1 << 16))
783 ways = 16;
784 else
785 ways = 8;
786 break;
787
788 case L2X0_CACHE_ID_PART_L210:
789 case L2X0_CACHE_ID_PART_L220:
790 ways = (aux >> 13) & 0xf;
791 break;
792
793 case AURORA_CACHE_ID:
794 ways = (aux >> 13) & 0xf;
795 ways = 2 << ((ways + 1) >> 2);
796 break;
797
798 default:
799 /* Assume unknown chips have 8 ways */
800 ways = 8;
801 break;
802 }
803
804 l2x0_way_mask = (1 << ways) - 1;
805
806 /*
807 * way_size_0 is the size that a way_size value of zero would be
808 * given the calculation: way_size = way_size_0 << way_size_bits.
809 * So, if way_size_bits=0 is reserved, but way_size_bits=1 is 16k,
810 * then way_size_0 would be 8k.
811 *
812 * L2 cache size = number of ways * way size.
813 */
814 way_size_bits = (aux & L2C_AUX_CTRL_WAY_SIZE_MASK) >>
815 L2C_AUX_CTRL_WAY_SIZE_SHIFT;
816 l2x0_size = ways * (data->way_size_0 << way_size_bits);
817
818 fns = data->outer_cache;
819 fns.write_sec = outer_cache.write_sec;
820 if (data->fixup)
821 data->fixup(l2x0_base, cache_id, &fns);
822
823 /*
824 * Check if l2x0 controller is already enabled. If we are booting
825 * in non-secure mode accessing the below registers will fault.
826 */
827 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
828 data->enable(l2x0_base, aux, data->num_lock);
829
830 outer_cache = fns;
831
832 /*
833 * It is strange to save the register state before initialisation,
834 * but hey, this is what the DT implementations decided to do.
835 */
836 if (data->save)
837 data->save(l2x0_base);
838
839 /* Re-read it in case some bits are reserved. */
840 aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
841
842 pr_info("%s cache controller enabled, %d ways, %d kB\n",
843 data->type, ways, l2x0_size >> 10);
844 pr_info("%s: CACHE_ID 0x%08x, AUX_CTRL 0x%08x\n",
845 data->type, cache_id, aux);
846 }
847
l2x0_init(void __iomem * base,u32 aux_val,u32 aux_mask)848 void __init l2x0_init(void __iomem *base, u32 aux_val, u32 aux_mask)
849 {
850 const struct l2c_init_data *data;
851 u32 cache_id;
852
853 l2x0_base = base;
854
855 cache_id = readl_relaxed(base + L2X0_CACHE_ID);
856
857 switch (cache_id & L2X0_CACHE_ID_PART_MASK) {
858 default:
859 case L2X0_CACHE_ID_PART_L210:
860 data = &l2c210_data;
861 break;
862
863 case L2X0_CACHE_ID_PART_L220:
864 data = &l2c220_data;
865 break;
866
867 case L2X0_CACHE_ID_PART_L310:
868 data = &l2c310_init_fns;
869 break;
870 }
871
872 __l2c_init(data, aux_val, aux_mask, cache_id);
873 }
874
875 #ifdef CONFIG_OF
876 static int l2_wt_override;
877
878 /* Aurora don't have the cache ID register available, so we have to
879 * pass it though the device tree */
880 static u32 cache_id_part_number_from_dt;
881
882 /**
883 * l2x0_cache_size_of_parse() - read cache size parameters from DT
884 * @np: the device tree node for the l2 cache
885 * @aux_val: pointer to machine-supplied auxilary register value, to
886 * be augmented by the call (bits to be set to 1)
887 * @aux_mask: pointer to machine-supplied auxilary register mask, to
888 * be augmented by the call (bits to be set to 0)
889 * @associativity: variable to return the calculated associativity in
890 * @max_way_size: the maximum size in bytes for the cache ways
891 */
l2x0_cache_size_of_parse(const struct device_node * np,u32 * aux_val,u32 * aux_mask,u32 * associativity,u32 max_way_size)892 static int __init l2x0_cache_size_of_parse(const struct device_node *np,
893 u32 *aux_val, u32 *aux_mask,
894 u32 *associativity,
895 u32 max_way_size)
896 {
897 u32 mask = 0, val = 0;
898 u32 cache_size = 0, sets = 0;
899 u32 way_size_bits = 1;
900 u32 way_size = 0;
901 u32 block_size = 0;
902 u32 line_size = 0;
903
904 of_property_read_u32(np, "cache-size", &cache_size);
905 of_property_read_u32(np, "cache-sets", &sets);
906 of_property_read_u32(np, "cache-block-size", &block_size);
907 of_property_read_u32(np, "cache-line-size", &line_size);
908
909 if (!cache_size || !sets)
910 return -ENODEV;
911
912 /* All these l2 caches have the same line = block size actually */
913 if (!line_size) {
914 if (block_size) {
915 /* If linesize if not given, it is equal to blocksize */
916 line_size = block_size;
917 } else {
918 /* Fall back to known size */
919 pr_warn("L2C OF: no cache block/line size given: "
920 "falling back to default size %d bytes\n",
921 CACHE_LINE_SIZE);
922 line_size = CACHE_LINE_SIZE;
923 }
924 }
925
926 if (line_size != CACHE_LINE_SIZE)
927 pr_warn("L2C OF: DT supplied line size %d bytes does "
928 "not match hardware line size of %d bytes\n",
929 line_size,
930 CACHE_LINE_SIZE);
931
932 /*
933 * Since:
934 * set size = cache size / sets
935 * ways = cache size / (sets * line size)
936 * way size = cache size / (cache size / (sets * line size))
937 * way size = sets * line size
938 * associativity = ways = cache size / way size
939 */
940 way_size = sets * line_size;
941 *associativity = cache_size / way_size;
942
943 if (way_size > max_way_size) {
944 pr_err("L2C OF: set size %dKB is too large\n", way_size);
945 return -EINVAL;
946 }
947
948 pr_info("L2C OF: override cache size: %d bytes (%dKB)\n",
949 cache_size, cache_size >> 10);
950 pr_info("L2C OF: override line size: %d bytes\n", line_size);
951 pr_info("L2C OF: override way size: %d bytes (%dKB)\n",
952 way_size, way_size >> 10);
953 pr_info("L2C OF: override associativity: %d\n", *associativity);
954
955 /*
956 * Calculates the bits 17:19 to set for way size:
957 * 512KB -> 6, 256KB -> 5, ... 16KB -> 1
958 */
959 way_size_bits = ilog2(way_size >> 10) - 3;
960 if (way_size_bits < 1 || way_size_bits > 6) {
961 pr_err("L2C OF: cache way size illegal: %dKB is not mapped\n",
962 way_size);
963 return -EINVAL;
964 }
965
966 mask |= L2C_AUX_CTRL_WAY_SIZE_MASK;
967 val |= (way_size_bits << L2C_AUX_CTRL_WAY_SIZE_SHIFT);
968
969 *aux_val &= ~mask;
970 *aux_val |= val;
971 *aux_mask &= ~mask;
972
973 return 0;
974 }
975
l2x0_of_parse(const struct device_node * np,u32 * aux_val,u32 * aux_mask)976 static void __init l2x0_of_parse(const struct device_node *np,
977 u32 *aux_val, u32 *aux_mask)
978 {
979 u32 data[2] = { 0, 0 };
980 u32 tag = 0;
981 u32 dirty = 0;
982 u32 val = 0, mask = 0;
983 u32 assoc;
984 int ret;
985
986 of_property_read_u32(np, "arm,tag-latency", &tag);
987 if (tag) {
988 mask |= L2X0_AUX_CTRL_TAG_LATENCY_MASK;
989 val |= (tag - 1) << L2X0_AUX_CTRL_TAG_LATENCY_SHIFT;
990 }
991
992 of_property_read_u32_array(np, "arm,data-latency",
993 data, ARRAY_SIZE(data));
994 if (data[0] && data[1]) {
995 mask |= L2X0_AUX_CTRL_DATA_RD_LATENCY_MASK |
996 L2X0_AUX_CTRL_DATA_WR_LATENCY_MASK;
997 val |= ((data[0] - 1) << L2X0_AUX_CTRL_DATA_RD_LATENCY_SHIFT) |
998 ((data[1] - 1) << L2X0_AUX_CTRL_DATA_WR_LATENCY_SHIFT);
999 }
1000
1001 of_property_read_u32(np, "arm,dirty-latency", &dirty);
1002 if (dirty) {
1003 mask |= L2X0_AUX_CTRL_DIRTY_LATENCY_MASK;
1004 val |= (dirty - 1) << L2X0_AUX_CTRL_DIRTY_LATENCY_SHIFT;
1005 }
1006
1007 ret = l2x0_cache_size_of_parse(np, aux_val, aux_mask, &assoc, SZ_256K);
1008 if (ret)
1009 return;
1010
1011 if (assoc > 8) {
1012 pr_err("l2x0 of: cache setting yield too high associativity\n");
1013 pr_err("l2x0 of: %d calculated, max 8\n", assoc);
1014 } else {
1015 mask |= L2X0_AUX_CTRL_ASSOC_MASK;
1016 val |= (assoc << L2X0_AUX_CTRL_ASSOC_SHIFT);
1017 }
1018
1019 *aux_val &= ~mask;
1020 *aux_val |= val;
1021 *aux_mask &= ~mask;
1022 }
1023
1024 static const struct l2c_init_data of_l2c210_data __initconst = {
1025 .type = "L2C-210",
1026 .way_size_0 = SZ_8K,
1027 .num_lock = 1,
1028 .of_parse = l2x0_of_parse,
1029 .enable = l2c_enable,
1030 .save = l2c_save,
1031 .outer_cache = {
1032 .inv_range = l2c210_inv_range,
1033 .clean_range = l2c210_clean_range,
1034 .flush_range = l2c210_flush_range,
1035 .flush_all = l2c210_flush_all,
1036 .disable = l2c_disable,
1037 .sync = l2c210_sync,
1038 .resume = l2c210_resume,
1039 },
1040 };
1041
1042 static const struct l2c_init_data of_l2c220_data __initconst = {
1043 .type = "L2C-220",
1044 .way_size_0 = SZ_8K,
1045 .num_lock = 1,
1046 .of_parse = l2x0_of_parse,
1047 .enable = l2c220_enable,
1048 .save = l2c_save,
1049 .outer_cache = {
1050 .inv_range = l2c220_inv_range,
1051 .clean_range = l2c220_clean_range,
1052 .flush_range = l2c220_flush_range,
1053 .flush_all = l2c220_flush_all,
1054 .disable = l2c_disable,
1055 .sync = l2c220_sync,
1056 .resume = l2c210_resume,
1057 },
1058 };
1059
l2c310_of_parse(const struct device_node * np,u32 * aux_val,u32 * aux_mask)1060 static void __init l2c310_of_parse(const struct device_node *np,
1061 u32 *aux_val, u32 *aux_mask)
1062 {
1063 u32 data[3] = { 0, 0, 0 };
1064 u32 tag[3] = { 0, 0, 0 };
1065 u32 filter[2] = { 0, 0 };
1066 u32 assoc;
1067 int ret;
1068
1069 of_property_read_u32_array(np, "arm,tag-latency", tag, ARRAY_SIZE(tag));
1070 if (tag[0] && tag[1] && tag[2])
1071 writel_relaxed(
1072 L310_LATENCY_CTRL_RD(tag[0] - 1) |
1073 L310_LATENCY_CTRL_WR(tag[1] - 1) |
1074 L310_LATENCY_CTRL_SETUP(tag[2] - 1),
1075 l2x0_base + L310_TAG_LATENCY_CTRL);
1076
1077 of_property_read_u32_array(np, "arm,data-latency",
1078 data, ARRAY_SIZE(data));
1079 if (data[0] && data[1] && data[2])
1080 writel_relaxed(
1081 L310_LATENCY_CTRL_RD(data[0] - 1) |
1082 L310_LATENCY_CTRL_WR(data[1] - 1) |
1083 L310_LATENCY_CTRL_SETUP(data[2] - 1),
1084 l2x0_base + L310_DATA_LATENCY_CTRL);
1085
1086 of_property_read_u32_array(np, "arm,filter-ranges",
1087 filter, ARRAY_SIZE(filter));
1088 if (filter[1]) {
1089 writel_relaxed(ALIGN(filter[0] + filter[1], SZ_1M),
1090 l2x0_base + L310_ADDR_FILTER_END);
1091 writel_relaxed((filter[0] & ~(SZ_1M - 1)) | L310_ADDR_FILTER_EN,
1092 l2x0_base + L310_ADDR_FILTER_START);
1093 }
1094
1095 ret = l2x0_cache_size_of_parse(np, aux_val, aux_mask, &assoc, SZ_512K);
1096 if (ret)
1097 return;
1098
1099 switch (assoc) {
1100 case 16:
1101 *aux_val &= ~L2X0_AUX_CTRL_ASSOC_MASK;
1102 *aux_val |= L310_AUX_CTRL_ASSOCIATIVITY_16;
1103 *aux_mask &= ~L2X0_AUX_CTRL_ASSOC_MASK;
1104 break;
1105 case 8:
1106 *aux_val &= ~L2X0_AUX_CTRL_ASSOC_MASK;
1107 *aux_mask &= ~L2X0_AUX_CTRL_ASSOC_MASK;
1108 break;
1109 default:
1110 pr_err("L2C-310 OF cache associativity %d invalid, only 8 or 16 permitted\n",
1111 assoc);
1112 break;
1113 }
1114 }
1115
1116 static const struct l2c_init_data of_l2c310_data __initconst = {
1117 .type = "L2C-310",
1118 .way_size_0 = SZ_8K,
1119 .num_lock = 8,
1120 .of_parse = l2c310_of_parse,
1121 .enable = l2c310_enable,
1122 .fixup = l2c310_fixup,
1123 .save = l2c310_save,
1124 .outer_cache = {
1125 .inv_range = l2c210_inv_range,
1126 .clean_range = l2c210_clean_range,
1127 .flush_range = l2c210_flush_range,
1128 .flush_all = l2c210_flush_all,
1129 .disable = l2c310_disable,
1130 .sync = l2c210_sync,
1131 .resume = l2c310_resume,
1132 },
1133 };
1134
1135 /*
1136 * This is a variant of the of_l2c310_data with .sync set to
1137 * NULL. Outer sync operations are not needed when the system is I/O
1138 * coherent, and potentially harmful in certain situations (PCIe/PL310
1139 * deadlock on Armada 375/38x due to hardware I/O coherency). The
1140 * other operations are kept because they are infrequent (therefore do
1141 * not cause the deadlock in practice) and needed for secondary CPU
1142 * boot and other power management activities.
1143 */
1144 static const struct l2c_init_data of_l2c310_coherent_data __initconst = {
1145 .type = "L2C-310 Coherent",
1146 .way_size_0 = SZ_8K,
1147 .num_lock = 8,
1148 .of_parse = l2c310_of_parse,
1149 .enable = l2c310_enable,
1150 .fixup = l2c310_fixup,
1151 .save = l2c310_save,
1152 .outer_cache = {
1153 .inv_range = l2c210_inv_range,
1154 .clean_range = l2c210_clean_range,
1155 .flush_range = l2c210_flush_range,
1156 .flush_all = l2c210_flush_all,
1157 .disable = l2c310_disable,
1158 .resume = l2c310_resume,
1159 },
1160 };
1161
1162 /*
1163 * Note that the end addresses passed to Linux primitives are
1164 * noninclusive, while the hardware cache range operations use
1165 * inclusive start and end addresses.
1166 */
calc_range_end(unsigned long start,unsigned long end)1167 static unsigned long calc_range_end(unsigned long start, unsigned long end)
1168 {
1169 /*
1170 * Limit the number of cache lines processed at once,
1171 * since cache range operations stall the CPU pipeline
1172 * until completion.
1173 */
1174 if (end > start + MAX_RANGE_SIZE)
1175 end = start + MAX_RANGE_SIZE;
1176
1177 /*
1178 * Cache range operations can't straddle a page boundary.
1179 */
1180 if (end > PAGE_ALIGN(start+1))
1181 end = PAGE_ALIGN(start+1);
1182
1183 return end;
1184 }
1185
1186 /*
1187 * Make sure 'start' and 'end' reference the same page, as L2 is PIPT
1188 * and range operations only do a TLB lookup on the start address.
1189 */
aurora_pa_range(unsigned long start,unsigned long end,unsigned long offset)1190 static void aurora_pa_range(unsigned long start, unsigned long end,
1191 unsigned long offset)
1192 {
1193 void __iomem *base = l2x0_base;
1194 unsigned long flags;
1195
1196 raw_spin_lock_irqsave(&l2x0_lock, flags);
1197 writel_relaxed(start, base + AURORA_RANGE_BASE_ADDR_REG);
1198 writel_relaxed(end, base + offset);
1199 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
1200
1201 writel_relaxed(0, base + AURORA_SYNC_REG);
1202 }
1203
aurora_inv_range(unsigned long start,unsigned long end)1204 static void aurora_inv_range(unsigned long start, unsigned long end)
1205 {
1206 /*
1207 * round start and end adresses up to cache line size
1208 */
1209 start &= ~(CACHE_LINE_SIZE - 1);
1210 end = ALIGN(end, CACHE_LINE_SIZE);
1211
1212 /*
1213 * Invalidate all full cache lines between 'start' and 'end'.
1214 */
1215 while (start < end) {
1216 unsigned long range_end = calc_range_end(start, end);
1217 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
1218 AURORA_INVAL_RANGE_REG);
1219 start = range_end;
1220 }
1221 }
1222
aurora_clean_range(unsigned long start,unsigned long end)1223 static void aurora_clean_range(unsigned long start, unsigned long end)
1224 {
1225 /*
1226 * If L2 is forced to WT, the L2 will always be clean and we
1227 * don't need to do anything here.
1228 */
1229 if (!l2_wt_override) {
1230 start &= ~(CACHE_LINE_SIZE - 1);
1231 end = ALIGN(end, CACHE_LINE_SIZE);
1232 while (start != end) {
1233 unsigned long range_end = calc_range_end(start, end);
1234 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
1235 AURORA_CLEAN_RANGE_REG);
1236 start = range_end;
1237 }
1238 }
1239 }
1240
aurora_flush_range(unsigned long start,unsigned long end)1241 static void aurora_flush_range(unsigned long start, unsigned long end)
1242 {
1243 start &= ~(CACHE_LINE_SIZE - 1);
1244 end = ALIGN(end, CACHE_LINE_SIZE);
1245 while (start != end) {
1246 unsigned long range_end = calc_range_end(start, end);
1247 /*
1248 * If L2 is forced to WT, the L2 will always be clean and we
1249 * just need to invalidate.
1250 */
1251 if (l2_wt_override)
1252 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
1253 AURORA_INVAL_RANGE_REG);
1254 else
1255 aurora_pa_range(start, range_end - CACHE_LINE_SIZE,
1256 AURORA_FLUSH_RANGE_REG);
1257 start = range_end;
1258 }
1259 }
1260
aurora_flush_all(void)1261 static void aurora_flush_all(void)
1262 {
1263 void __iomem *base = l2x0_base;
1264 unsigned long flags;
1265
1266 /* clean all ways */
1267 raw_spin_lock_irqsave(&l2x0_lock, flags);
1268 __l2c_op_way(base + L2X0_CLEAN_INV_WAY);
1269 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
1270
1271 writel_relaxed(0, base + AURORA_SYNC_REG);
1272 }
1273
aurora_cache_sync(void)1274 static void aurora_cache_sync(void)
1275 {
1276 writel_relaxed(0, l2x0_base + AURORA_SYNC_REG);
1277 }
1278
aurora_disable(void)1279 static void aurora_disable(void)
1280 {
1281 void __iomem *base = l2x0_base;
1282 unsigned long flags;
1283
1284 raw_spin_lock_irqsave(&l2x0_lock, flags);
1285 __l2c_op_way(base + L2X0_CLEAN_INV_WAY);
1286 writel_relaxed(0, base + AURORA_SYNC_REG);
1287 l2c_write_sec(0, base, L2X0_CTRL);
1288 dsb(st);
1289 raw_spin_unlock_irqrestore(&l2x0_lock, flags);
1290 }
1291
aurora_save(void __iomem * base)1292 static void aurora_save(void __iomem *base)
1293 {
1294 l2x0_saved_regs.ctrl = readl_relaxed(base + L2X0_CTRL);
1295 l2x0_saved_regs.aux_ctrl = readl_relaxed(base + L2X0_AUX_CTRL);
1296 }
1297
aurora_resume(void)1298 static void aurora_resume(void)
1299 {
1300 void __iomem *base = l2x0_base;
1301
1302 if (!(readl(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
1303 writel_relaxed(l2x0_saved_regs.aux_ctrl, base + L2X0_AUX_CTRL);
1304 writel_relaxed(l2x0_saved_regs.ctrl, base + L2X0_CTRL);
1305 }
1306 }
1307
1308 /*
1309 * For Aurora cache in no outer mode, enable via the CP15 coprocessor
1310 * broadcasting of cache commands to L2.
1311 */
aurora_enable_no_outer(void __iomem * base,u32 aux,unsigned num_lock)1312 static void __init aurora_enable_no_outer(void __iomem *base, u32 aux,
1313 unsigned num_lock)
1314 {
1315 u32 u;
1316
1317 asm volatile("mrc p15, 1, %0, c15, c2, 0" : "=r" (u));
1318 u |= AURORA_CTRL_FW; /* Set the FW bit */
1319 asm volatile("mcr p15, 1, %0, c15, c2, 0" : : "r" (u));
1320
1321 isb();
1322
1323 l2c_enable(base, aux, num_lock);
1324 }
1325
aurora_fixup(void __iomem * base,u32 cache_id,struct outer_cache_fns * fns)1326 static void __init aurora_fixup(void __iomem *base, u32 cache_id,
1327 struct outer_cache_fns *fns)
1328 {
1329 sync_reg_offset = AURORA_SYNC_REG;
1330 }
1331
aurora_of_parse(const struct device_node * np,u32 * aux_val,u32 * aux_mask)1332 static void __init aurora_of_parse(const struct device_node *np,
1333 u32 *aux_val, u32 *aux_mask)
1334 {
1335 u32 val = AURORA_ACR_REPLACEMENT_TYPE_SEMIPLRU;
1336 u32 mask = AURORA_ACR_REPLACEMENT_MASK;
1337
1338 of_property_read_u32(np, "cache-id-part",
1339 &cache_id_part_number_from_dt);
1340
1341 /* Determine and save the write policy */
1342 l2_wt_override = of_property_read_bool(np, "wt-override");
1343
1344 if (l2_wt_override) {
1345 val |= AURORA_ACR_FORCE_WRITE_THRO_POLICY;
1346 mask |= AURORA_ACR_FORCE_WRITE_POLICY_MASK;
1347 }
1348
1349 *aux_val &= ~mask;
1350 *aux_val |= val;
1351 *aux_mask &= ~mask;
1352 }
1353
1354 static const struct l2c_init_data of_aurora_with_outer_data __initconst = {
1355 .type = "Aurora",
1356 .way_size_0 = SZ_4K,
1357 .num_lock = 4,
1358 .of_parse = aurora_of_parse,
1359 .enable = l2c_enable,
1360 .fixup = aurora_fixup,
1361 .save = aurora_save,
1362 .outer_cache = {
1363 .inv_range = aurora_inv_range,
1364 .clean_range = aurora_clean_range,
1365 .flush_range = aurora_flush_range,
1366 .flush_all = aurora_flush_all,
1367 .disable = aurora_disable,
1368 .sync = aurora_cache_sync,
1369 .resume = aurora_resume,
1370 },
1371 };
1372
1373 static const struct l2c_init_data of_aurora_no_outer_data __initconst = {
1374 .type = "Aurora",
1375 .way_size_0 = SZ_4K,
1376 .num_lock = 4,
1377 .of_parse = aurora_of_parse,
1378 .enable = aurora_enable_no_outer,
1379 .fixup = aurora_fixup,
1380 .save = aurora_save,
1381 .outer_cache = {
1382 .resume = aurora_resume,
1383 },
1384 };
1385
1386 /*
1387 * For certain Broadcom SoCs, depending on the address range, different offsets
1388 * need to be added to the address before passing it to L2 for
1389 * invalidation/clean/flush
1390 *
1391 * Section Address Range Offset EMI
1392 * 1 0x00000000 - 0x3FFFFFFF 0x80000000 VC
1393 * 2 0x40000000 - 0xBFFFFFFF 0x40000000 SYS
1394 * 3 0xC0000000 - 0xFFFFFFFF 0x80000000 VC
1395 *
1396 * When the start and end addresses have crossed two different sections, we
1397 * need to break the L2 operation into two, each within its own section.
1398 * For example, if we need to invalidate addresses starts at 0xBFFF0000 and
1399 * ends at 0xC0001000, we need do invalidate 1) 0xBFFF0000 - 0xBFFFFFFF and 2)
1400 * 0xC0000000 - 0xC0001000
1401 *
1402 * Note 1:
1403 * By breaking a single L2 operation into two, we may potentially suffer some
1404 * performance hit, but keep in mind the cross section case is very rare
1405 *
1406 * Note 2:
1407 * We do not need to handle the case when the start address is in
1408 * Section 1 and the end address is in Section 3, since it is not a valid use
1409 * case
1410 *
1411 * Note 3:
1412 * Section 1 in practical terms can no longer be used on rev A2. Because of
1413 * that the code does not need to handle section 1 at all.
1414 *
1415 */
1416 #define BCM_SYS_EMI_START_ADDR 0x40000000UL
1417 #define BCM_VC_EMI_SEC3_START_ADDR 0xC0000000UL
1418
1419 #define BCM_SYS_EMI_OFFSET 0x40000000UL
1420 #define BCM_VC_EMI_OFFSET 0x80000000UL
1421
bcm_addr_is_sys_emi(unsigned long addr)1422 static inline int bcm_addr_is_sys_emi(unsigned long addr)
1423 {
1424 return (addr >= BCM_SYS_EMI_START_ADDR) &&
1425 (addr < BCM_VC_EMI_SEC3_START_ADDR);
1426 }
1427
bcm_l2_phys_addr(unsigned long addr)1428 static inline unsigned long bcm_l2_phys_addr(unsigned long addr)
1429 {
1430 if (bcm_addr_is_sys_emi(addr))
1431 return addr + BCM_SYS_EMI_OFFSET;
1432 else
1433 return addr + BCM_VC_EMI_OFFSET;
1434 }
1435
bcm_inv_range(unsigned long start,unsigned long end)1436 static void bcm_inv_range(unsigned long start, unsigned long end)
1437 {
1438 unsigned long new_start, new_end;
1439
1440 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
1441
1442 if (unlikely(end <= start))
1443 return;
1444
1445 new_start = bcm_l2_phys_addr(start);
1446 new_end = bcm_l2_phys_addr(end);
1447
1448 /* normal case, no cross section between start and end */
1449 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
1450 l2c210_inv_range(new_start, new_end);
1451 return;
1452 }
1453
1454 /* They cross sections, so it can only be a cross from section
1455 * 2 to section 3
1456 */
1457 l2c210_inv_range(new_start,
1458 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
1459 l2c210_inv_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
1460 new_end);
1461 }
1462
bcm_clean_range(unsigned long start,unsigned long end)1463 static void bcm_clean_range(unsigned long start, unsigned long end)
1464 {
1465 unsigned long new_start, new_end;
1466
1467 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
1468
1469 if (unlikely(end <= start))
1470 return;
1471
1472 new_start = bcm_l2_phys_addr(start);
1473 new_end = bcm_l2_phys_addr(end);
1474
1475 /* normal case, no cross section between start and end */
1476 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
1477 l2c210_clean_range(new_start, new_end);
1478 return;
1479 }
1480
1481 /* They cross sections, so it can only be a cross from section
1482 * 2 to section 3
1483 */
1484 l2c210_clean_range(new_start,
1485 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
1486 l2c210_clean_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
1487 new_end);
1488 }
1489
bcm_flush_range(unsigned long start,unsigned long end)1490 static void bcm_flush_range(unsigned long start, unsigned long end)
1491 {
1492 unsigned long new_start, new_end;
1493
1494 BUG_ON(start < BCM_SYS_EMI_START_ADDR);
1495
1496 if (unlikely(end <= start))
1497 return;
1498
1499 if ((end - start) >= l2x0_size) {
1500 outer_cache.flush_all();
1501 return;
1502 }
1503
1504 new_start = bcm_l2_phys_addr(start);
1505 new_end = bcm_l2_phys_addr(end);
1506
1507 /* normal case, no cross section between start and end */
1508 if (likely(bcm_addr_is_sys_emi(end) || !bcm_addr_is_sys_emi(start))) {
1509 l2c210_flush_range(new_start, new_end);
1510 return;
1511 }
1512
1513 /* They cross sections, so it can only be a cross from section
1514 * 2 to section 3
1515 */
1516 l2c210_flush_range(new_start,
1517 bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR-1));
1518 l2c210_flush_range(bcm_l2_phys_addr(BCM_VC_EMI_SEC3_START_ADDR),
1519 new_end);
1520 }
1521
1522 /* Broadcom L2C-310 start from ARMs R3P2 or later, and require no fixups */
1523 static const struct l2c_init_data of_bcm_l2x0_data __initconst = {
1524 .type = "BCM-L2C-310",
1525 .way_size_0 = SZ_8K,
1526 .num_lock = 8,
1527 .of_parse = l2c310_of_parse,
1528 .enable = l2c310_enable,
1529 .save = l2c310_save,
1530 .outer_cache = {
1531 .inv_range = bcm_inv_range,
1532 .clean_range = bcm_clean_range,
1533 .flush_range = bcm_flush_range,
1534 .flush_all = l2c210_flush_all,
1535 .disable = l2c310_disable,
1536 .sync = l2c210_sync,
1537 .resume = l2c310_resume,
1538 },
1539 };
1540
tauros3_save(void __iomem * base)1541 static void __init tauros3_save(void __iomem *base)
1542 {
1543 l2c_save(base);
1544
1545 l2x0_saved_regs.aux2_ctrl =
1546 readl_relaxed(base + TAUROS3_AUX2_CTRL);
1547 l2x0_saved_regs.prefetch_ctrl =
1548 readl_relaxed(base + L310_PREFETCH_CTRL);
1549 }
1550
tauros3_resume(void)1551 static void tauros3_resume(void)
1552 {
1553 void __iomem *base = l2x0_base;
1554
1555 if (!(readl_relaxed(base + L2X0_CTRL) & L2X0_CTRL_EN)) {
1556 writel_relaxed(l2x0_saved_regs.aux2_ctrl,
1557 base + TAUROS3_AUX2_CTRL);
1558 writel_relaxed(l2x0_saved_regs.prefetch_ctrl,
1559 base + L310_PREFETCH_CTRL);
1560
1561 l2c_enable(base, l2x0_saved_regs.aux_ctrl, 8);
1562 }
1563 }
1564
1565 static const struct l2c_init_data of_tauros3_data __initconst = {
1566 .type = "Tauros3",
1567 .way_size_0 = SZ_8K,
1568 .num_lock = 8,
1569 .enable = l2c_enable,
1570 .save = tauros3_save,
1571 /* Tauros3 broadcasts L1 cache operations to L2 */
1572 .outer_cache = {
1573 .resume = tauros3_resume,
1574 },
1575 };
1576
1577 #define L2C_ID(name, fns) { .compatible = name, .data = (void *)&fns }
1578 static const struct of_device_id l2x0_ids[] __initconst = {
1579 L2C_ID("arm,l210-cache", of_l2c210_data),
1580 L2C_ID("arm,l220-cache", of_l2c220_data),
1581 L2C_ID("arm,pl310-cache", of_l2c310_data),
1582 L2C_ID("brcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
1583 L2C_ID("marvell,aurora-outer-cache", of_aurora_with_outer_data),
1584 L2C_ID("marvell,aurora-system-cache", of_aurora_no_outer_data),
1585 L2C_ID("marvell,tauros3-cache", of_tauros3_data),
1586 /* Deprecated IDs */
1587 L2C_ID("bcm,bcm11351-a2-pl310-cache", of_bcm_l2x0_data),
1588 {}
1589 };
1590
l2x0_of_init(u32 aux_val,u32 aux_mask)1591 int __init l2x0_of_init(u32 aux_val, u32 aux_mask)
1592 {
1593 const struct l2c_init_data *data;
1594 struct device_node *np;
1595 struct resource res;
1596 u32 cache_id, old_aux;
1597
1598 np = of_find_matching_node(NULL, l2x0_ids);
1599 if (!np)
1600 return -ENODEV;
1601
1602 if (of_address_to_resource(np, 0, &res))
1603 return -ENODEV;
1604
1605 l2x0_base = ioremap(res.start, resource_size(&res));
1606 if (!l2x0_base)
1607 return -ENOMEM;
1608
1609 l2x0_saved_regs.phy_base = res.start;
1610
1611 data = of_match_node(l2x0_ids, np)->data;
1612
1613 if (of_device_is_compatible(np, "arm,pl310-cache") &&
1614 of_property_read_bool(np, "arm,io-coherent"))
1615 data = &of_l2c310_coherent_data;
1616
1617 old_aux = readl_relaxed(l2x0_base + L2X0_AUX_CTRL);
1618 if (old_aux != ((old_aux & aux_mask) | aux_val)) {
1619 pr_warn("L2C: platform modifies aux control register: 0x%08x -> 0x%08x\n",
1620 old_aux, (old_aux & aux_mask) | aux_val);
1621 } else if (aux_mask != ~0U && aux_val != 0) {
1622 pr_alert("L2C: platform provided aux values match the hardware, so have no effect. Please remove them.\n");
1623 }
1624
1625 /* All L2 caches are unified, so this property should be specified */
1626 if (!of_property_read_bool(np, "cache-unified"))
1627 pr_err("L2C: device tree omits to specify unified cache\n");
1628
1629 /* L2 configuration can only be changed if the cache is disabled */
1630 if (!(readl_relaxed(l2x0_base + L2X0_CTRL) & L2X0_CTRL_EN))
1631 if (data->of_parse)
1632 data->of_parse(np, &aux_val, &aux_mask);
1633
1634 if (cache_id_part_number_from_dt)
1635 cache_id = cache_id_part_number_from_dt;
1636 else
1637 cache_id = readl_relaxed(l2x0_base + L2X0_CACHE_ID);
1638
1639 __l2c_init(data, aux_val, aux_mask, cache_id);
1640
1641 return 0;
1642 }
1643 #endif
1644