xref: /openbmc/linux/arch/arm/mm/cache-xsc3l2.c (revision 15d07dc9c59eae51219c40253bdf920f62bb10f2)
1905a09d5SEric Miao /*
2905a09d5SEric Miao  * arch/arm/mm/cache-xsc3l2.c - XScale3 L2 cache controller support
3905a09d5SEric Miao  *
4905a09d5SEric Miao  * Copyright (C) 2007 ARM Limited
5905a09d5SEric Miao  *
6905a09d5SEric Miao  * This program is free software; you can redistribute it and/or modify
7905a09d5SEric Miao  * it under the terms of the GNU General Public License version 2 as
8905a09d5SEric Miao  * published by the Free Software Foundation.
9905a09d5SEric Miao  *
10905a09d5SEric Miao  * This program is distributed in the hope that it will be useful,
11905a09d5SEric Miao  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12905a09d5SEric Miao  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13905a09d5SEric Miao  * GNU General Public License for more details.
14905a09d5SEric Miao  *
15905a09d5SEric Miao  * You should have received a copy of the GNU General Public License
16905a09d5SEric Miao  * along with this program; if not, write to the Free Software
17905a09d5SEric Miao  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18905a09d5SEric Miao  */
19905a09d5SEric Miao #include <linux/init.h>
2025cbe454SNicolas Pitre #include <linux/highmem.h>
21*15d07dc9SRussell King #include <asm/cp15.h>
220ba8b9b2SRussell King #include <asm/cputype.h>
23905a09d5SEric Miao #include <asm/cacheflush.h>
24905a09d5SEric Miao 
25905a09d5SEric Miao #define CR_L2	(1 << 26)
26905a09d5SEric Miao 
27905a09d5SEric Miao #define CACHE_LINE_SIZE		32
28905a09d5SEric Miao #define CACHE_LINE_SHIFT	5
29905a09d5SEric Miao #define CACHE_WAY_PER_SET	8
30905a09d5SEric Miao 
31905a09d5SEric Miao #define CACHE_WAY_SIZE(l2ctype)	(8192 << (((l2ctype) >> 8) & 0xf))
32905a09d5SEric Miao #define CACHE_SET_SIZE(l2ctype)	(CACHE_WAY_SIZE(l2ctype) >> CACHE_LINE_SHIFT)
33905a09d5SEric Miao 
34905a09d5SEric Miao static inline int xsc3_l2_present(void)
35905a09d5SEric Miao {
36905a09d5SEric Miao 	unsigned long l2ctype;
37905a09d5SEric Miao 
38905a09d5SEric Miao 	__asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
39905a09d5SEric Miao 
40905a09d5SEric Miao 	return !!(l2ctype & 0xf8);
41905a09d5SEric Miao }
42905a09d5SEric Miao 
43905a09d5SEric Miao static inline void xsc3_l2_clean_mva(unsigned long addr)
44905a09d5SEric Miao {
45905a09d5SEric Miao 	__asm__("mcr p15, 1, %0, c7, c11, 1" : : "r" (addr));
46905a09d5SEric Miao }
47905a09d5SEric Miao 
48905a09d5SEric Miao static inline void xsc3_l2_inv_mva(unsigned long addr)
49905a09d5SEric Miao {
50905a09d5SEric Miao 	__asm__("mcr p15, 1, %0, c7, c7, 1" : : "r" (addr));
51905a09d5SEric Miao }
52905a09d5SEric Miao 
53905a09d5SEric Miao static inline void xsc3_l2_inv_all(void)
54905a09d5SEric Miao {
55905a09d5SEric Miao 	unsigned long l2ctype, set_way;
56905a09d5SEric Miao 	int set, way;
57905a09d5SEric Miao 
58905a09d5SEric Miao 	__asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
59905a09d5SEric Miao 
60905a09d5SEric Miao 	for (set = 0; set < CACHE_SET_SIZE(l2ctype); set++) {
61905a09d5SEric Miao 		for (way = 0; way < CACHE_WAY_PER_SET; way++) {
62905a09d5SEric Miao 			set_way = (way << 29) | (set << 5);
63905a09d5SEric Miao 			__asm__("mcr p15, 1, %0, c7, c11, 2" : : "r"(set_way));
64905a09d5SEric Miao 		}
65905a09d5SEric Miao 	}
66905a09d5SEric Miao 
67905a09d5SEric Miao 	dsb();
68905a09d5SEric Miao }
69905a09d5SEric Miao 
7025cbe454SNicolas Pitre static inline void l2_unmap_va(unsigned long va)
7125cbe454SNicolas Pitre {
723902a15eSNicolas Pitre #ifdef CONFIG_HIGHMEM
7325cbe454SNicolas Pitre 	if (va != -1)
7425cbe454SNicolas Pitre 		kunmap_atomic((void *)va);
753902a15eSNicolas Pitre #endif
7625cbe454SNicolas Pitre }
773902a15eSNicolas Pitre 
7825cbe454SNicolas Pitre static inline unsigned long l2_map_va(unsigned long pa, unsigned long prev_va)
793902a15eSNicolas Pitre {
803902a15eSNicolas Pitre #ifdef CONFIG_HIGHMEM
813902a15eSNicolas Pitre 	unsigned long va = prev_va & PAGE_MASK;
823902a15eSNicolas Pitre 	unsigned long pa_offset = pa << (32 - PAGE_SHIFT);
833902a15eSNicolas Pitre 	if (unlikely(pa_offset < (prev_va << (32 - PAGE_SHIFT)))) {
843902a15eSNicolas Pitre 		/*
853902a15eSNicolas Pitre 		 * Switching to a new page.  Because cache ops are
863902a15eSNicolas Pitre 		 * using virtual addresses only, we must put a mapping
8725cbe454SNicolas Pitre 		 * in place for it.
883902a15eSNicolas Pitre 		 */
8925cbe454SNicolas Pitre 		l2_unmap_va(prev_va);
9025cbe454SNicolas Pitre 		va = (unsigned long)kmap_atomic_pfn(pa >> PAGE_SHIFT);
913902a15eSNicolas Pitre 	}
923902a15eSNicolas Pitre 	return va + (pa_offset >> (32 - PAGE_SHIFT));
933902a15eSNicolas Pitre #else
943902a15eSNicolas Pitre 	return __phys_to_virt(pa);
953902a15eSNicolas Pitre #endif
963902a15eSNicolas Pitre }
973902a15eSNicolas Pitre 
98905a09d5SEric Miao static void xsc3_l2_inv_range(unsigned long start, unsigned long end)
99905a09d5SEric Miao {
10025cbe454SNicolas Pitre 	unsigned long vaddr;
1013902a15eSNicolas Pitre 
102905a09d5SEric Miao 	if (start == 0 && end == -1ul) {
103905a09d5SEric Miao 		xsc3_l2_inv_all();
104905a09d5SEric Miao 		return;
105905a09d5SEric Miao 	}
106905a09d5SEric Miao 
1073902a15eSNicolas Pitre 	vaddr = -1;  /* to force the first mapping */
1083902a15eSNicolas Pitre 
109905a09d5SEric Miao 	/*
110905a09d5SEric Miao 	 * Clean and invalidate partial first cache line.
111905a09d5SEric Miao 	 */
112905a09d5SEric Miao 	if (start & (CACHE_LINE_SIZE - 1)) {
11325cbe454SNicolas Pitre 		vaddr = l2_map_va(start & ~(CACHE_LINE_SIZE - 1), vaddr);
1143902a15eSNicolas Pitre 		xsc3_l2_clean_mva(vaddr);
1153902a15eSNicolas Pitre 		xsc3_l2_inv_mva(vaddr);
116905a09d5SEric Miao 		start = (start | (CACHE_LINE_SIZE - 1)) + 1;
117905a09d5SEric Miao 	}
118905a09d5SEric Miao 
119905a09d5SEric Miao 	/*
120905a09d5SEric Miao 	 * Invalidate all full cache lines between 'start' and 'end'.
121905a09d5SEric Miao 	 */
1223902a15eSNicolas Pitre 	while (start < (end & ~(CACHE_LINE_SIZE - 1))) {
12325cbe454SNicolas Pitre 		vaddr = l2_map_va(start, vaddr);
1243902a15eSNicolas Pitre 		xsc3_l2_inv_mva(vaddr);
125905a09d5SEric Miao 		start += CACHE_LINE_SIZE;
126905a09d5SEric Miao 	}
127905a09d5SEric Miao 
1283902a15eSNicolas Pitre 	/*
1293902a15eSNicolas Pitre 	 * Clean and invalidate partial last cache line.
1303902a15eSNicolas Pitre 	 */
1313902a15eSNicolas Pitre 	if (start < end) {
13225cbe454SNicolas Pitre 		vaddr = l2_map_va(start, vaddr);
1333902a15eSNicolas Pitre 		xsc3_l2_clean_mva(vaddr);
1343902a15eSNicolas Pitre 		xsc3_l2_inv_mva(vaddr);
1353902a15eSNicolas Pitre 	}
1363902a15eSNicolas Pitre 
13725cbe454SNicolas Pitre 	l2_unmap_va(vaddr);
1383902a15eSNicolas Pitre 
139905a09d5SEric Miao 	dsb();
140905a09d5SEric Miao }
141905a09d5SEric Miao 
142905a09d5SEric Miao static void xsc3_l2_clean_range(unsigned long start, unsigned long end)
143905a09d5SEric Miao {
14425cbe454SNicolas Pitre 	unsigned long vaddr;
1453902a15eSNicolas Pitre 
1463902a15eSNicolas Pitre 	vaddr = -1;  /* to force the first mapping */
1473902a15eSNicolas Pitre 
148905a09d5SEric Miao 	start &= ~(CACHE_LINE_SIZE - 1);
149905a09d5SEric Miao 	while (start < end) {
15025cbe454SNicolas Pitre 		vaddr = l2_map_va(start, vaddr);
1513902a15eSNicolas Pitre 		xsc3_l2_clean_mva(vaddr);
152905a09d5SEric Miao 		start += CACHE_LINE_SIZE;
153905a09d5SEric Miao 	}
154905a09d5SEric Miao 
15525cbe454SNicolas Pitre 	l2_unmap_va(vaddr);
1563902a15eSNicolas Pitre 
157905a09d5SEric Miao 	dsb();
158905a09d5SEric Miao }
159905a09d5SEric Miao 
160905a09d5SEric Miao /*
161905a09d5SEric Miao  * optimize L2 flush all operation by set/way format
162905a09d5SEric Miao  */
163905a09d5SEric Miao static inline void xsc3_l2_flush_all(void)
164905a09d5SEric Miao {
165905a09d5SEric Miao 	unsigned long l2ctype, set_way;
166905a09d5SEric Miao 	int set, way;
167905a09d5SEric Miao 
168905a09d5SEric Miao 	__asm__("mrc p15, 1, %0, c0, c0, 1" : "=r" (l2ctype));
169905a09d5SEric Miao 
170905a09d5SEric Miao 	for (set = 0; set < CACHE_SET_SIZE(l2ctype); set++) {
171905a09d5SEric Miao 		for (way = 0; way < CACHE_WAY_PER_SET; way++) {
172905a09d5SEric Miao 			set_way = (way << 29) | (set << 5);
173905a09d5SEric Miao 			__asm__("mcr p15, 1, %0, c7, c15, 2" : : "r"(set_way));
174905a09d5SEric Miao 		}
175905a09d5SEric Miao 	}
176905a09d5SEric Miao 
177905a09d5SEric Miao 	dsb();
178905a09d5SEric Miao }
179905a09d5SEric Miao 
180905a09d5SEric Miao static void xsc3_l2_flush_range(unsigned long start, unsigned long end)
181905a09d5SEric Miao {
18225cbe454SNicolas Pitre 	unsigned long vaddr;
1833902a15eSNicolas Pitre 
184905a09d5SEric Miao 	if (start == 0 && end == -1ul) {
185905a09d5SEric Miao 		xsc3_l2_flush_all();
186905a09d5SEric Miao 		return;
187905a09d5SEric Miao 	}
188905a09d5SEric Miao 
1893902a15eSNicolas Pitre 	vaddr = -1;  /* to force the first mapping */
1903902a15eSNicolas Pitre 
191905a09d5SEric Miao 	start &= ~(CACHE_LINE_SIZE - 1);
192905a09d5SEric Miao 	while (start < end) {
19325cbe454SNicolas Pitre 		vaddr = l2_map_va(start, vaddr);
1943902a15eSNicolas Pitre 		xsc3_l2_clean_mva(vaddr);
1953902a15eSNicolas Pitre 		xsc3_l2_inv_mva(vaddr);
196905a09d5SEric Miao 		start += CACHE_LINE_SIZE;
197905a09d5SEric Miao 	}
198905a09d5SEric Miao 
19925cbe454SNicolas Pitre 	l2_unmap_va(vaddr);
2003902a15eSNicolas Pitre 
201905a09d5SEric Miao 	dsb();
202905a09d5SEric Miao }
203905a09d5SEric Miao 
204905a09d5SEric Miao static int __init xsc3_l2_init(void)
205905a09d5SEric Miao {
206905a09d5SEric Miao 	if (!cpu_is_xsc3() || !xsc3_l2_present())
207905a09d5SEric Miao 		return 0;
208905a09d5SEric Miao 
209dc8601a2SHaojian Zhuang 	if (get_cr() & CR_L2) {
210905a09d5SEric Miao 		pr_info("XScale3 L2 cache enabled.\n");
211905a09d5SEric Miao 		xsc3_l2_inv_all();
212905a09d5SEric Miao 
213905a09d5SEric Miao 		outer_cache.inv_range = xsc3_l2_inv_range;
214905a09d5SEric Miao 		outer_cache.clean_range = xsc3_l2_clean_range;
215905a09d5SEric Miao 		outer_cache.flush_range = xsc3_l2_flush_range;
216dc8601a2SHaojian Zhuang 	}
217905a09d5SEric Miao 
218905a09d5SEric Miao 	return 0;
219905a09d5SEric Miao }
220905a09d5SEric Miao core_initcall(xsc3_l2_init);
221