1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm/mm/cache-xsc3l2.c - XScale3 L2 cache controller support
5 * Copyright (C) 2007 ARM Limited
7 #include <linux/init.h>
8 #include <linux/highmem.h>
10 #include <asm/cputype.h>
11 #include <asm/cacheflush.h>
13 #define CR_L2 (1 << 26)
15 #define CACHE_LINE_SIZE 32
16 #define CACHE_LINE_SHIFT 5
17 #define CACHE_WAY_PER_SET 8
19 #define CACHE_WAY_SIZE(l2ctype) (8192 << (((l2ctype) >> 8) & 0xf))
20 #define CACHE_SET_SIZE(l2ctype) (CACHE_WAY_SIZE(l2ctype) >> CACHE_LINE_SHIFT)
22 static inline int xsc3_l2_present(void)
24 unsigned long l2ctype
;
26 __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype
));
28 return !!(l2ctype
& 0xf8);
31 static inline void xsc3_l2_clean_mva(unsigned long addr
)
33 __asm__("mcr p15, 1, %0, c7, c11, 1" : : "r" (addr
));
36 static inline void xsc3_l2_inv_mva(unsigned long addr
)
38 __asm__("mcr p15, 1, %0, c7, c7, 1" : : "r" (addr
));
41 static inline void xsc3_l2_inv_all(void)
43 unsigned long l2ctype
, set_way
;
46 __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype
));
48 for (set
= 0; set
< CACHE_SET_SIZE(l2ctype
); set
++) {
49 for (way
= 0; way
< CACHE_WAY_PER_SET
; way
++) {
50 set_way
= (way
<< 29) | (set
<< 5);
51 __asm__("mcr p15, 1, %0, c7, c11, 2" : : "r"(set_way
));
58 static inline void l2_unmap_va(unsigned long va
)
62 kunmap_atomic((void *)va
);
66 static inline unsigned long l2_map_va(unsigned long pa
, unsigned long prev_va
)
69 unsigned long va
= prev_va
& PAGE_MASK
;
70 unsigned long pa_offset
= pa
<< (32 - PAGE_SHIFT
);
71 if (unlikely(pa_offset
< (prev_va
<< (32 - PAGE_SHIFT
)))) {
73 * Switching to a new page. Because cache ops are
74 * using virtual addresses only, we must put a mapping
78 va
= (unsigned long)kmap_atomic_pfn(pa
>> PAGE_SHIFT
);
80 return va
+ (pa_offset
>> (32 - PAGE_SHIFT
));
82 return __phys_to_virt(pa
);
86 static void xsc3_l2_inv_range(unsigned long start
, unsigned long end
)
90 if (start
== 0 && end
== -1ul) {
95 vaddr
= -1; /* to force the first mapping */
98 * Clean and invalidate partial first cache line.
100 if (start
& (CACHE_LINE_SIZE
- 1)) {
101 vaddr
= l2_map_va(start
& ~(CACHE_LINE_SIZE
- 1), vaddr
);
102 xsc3_l2_clean_mva(vaddr
);
103 xsc3_l2_inv_mva(vaddr
);
104 start
= (start
| (CACHE_LINE_SIZE
- 1)) + 1;
108 * Invalidate all full cache lines between 'start' and 'end'.
110 while (start
< (end
& ~(CACHE_LINE_SIZE
- 1))) {
111 vaddr
= l2_map_va(start
, vaddr
);
112 xsc3_l2_inv_mva(vaddr
);
113 start
+= CACHE_LINE_SIZE
;
117 * Clean and invalidate partial last cache line.
120 vaddr
= l2_map_va(start
, vaddr
);
121 xsc3_l2_clean_mva(vaddr
);
122 xsc3_l2_inv_mva(vaddr
);
130 static void xsc3_l2_clean_range(unsigned long start
, unsigned long end
)
134 vaddr
= -1; /* to force the first mapping */
136 start
&= ~(CACHE_LINE_SIZE
- 1);
137 while (start
< end
) {
138 vaddr
= l2_map_va(start
, vaddr
);
139 xsc3_l2_clean_mva(vaddr
);
140 start
+= CACHE_LINE_SIZE
;
149 * optimize L2 flush all operation by set/way format
151 static inline void xsc3_l2_flush_all(void)
153 unsigned long l2ctype
, set_way
;
156 __asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype
));
158 for (set
= 0; set
< CACHE_SET_SIZE(l2ctype
); set
++) {
159 for (way
= 0; way
< CACHE_WAY_PER_SET
; way
++) {
160 set_way
= (way
<< 29) | (set
<< 5);
161 __asm__("mcr p15, 1, %0, c7, c15, 2" : : "r"(set_way
));
168 static void xsc3_l2_flush_range(unsigned long start
, unsigned long end
)
172 if (start
== 0 && end
== -1ul) {
177 vaddr
= -1; /* to force the first mapping */
179 start
&= ~(CACHE_LINE_SIZE
- 1);
180 while (start
< end
) {
181 vaddr
= l2_map_va(start
, vaddr
);
182 xsc3_l2_clean_mva(vaddr
);
183 xsc3_l2_inv_mva(vaddr
);
184 start
+= CACHE_LINE_SIZE
;
192 static int __init
xsc3_l2_init(void)
194 if (!cpu_is_xsc3() || !xsc3_l2_present())
197 if (get_cr() & CR_L2
) {
198 pr_info("XScale3 L2 cache enabled.\n");
201 outer_cache
.inv_range
= xsc3_l2_inv_range
;
202 outer_cache
.clean_range
= xsc3_l2_clean_range
;
203 outer_cache
.flush_range
= xsc3_l2_flush_range
;
208 core_initcall(xsc3_l2_init
);