xref: /openbmc/linux/arch/mips/mm/c-r4k.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * This file is subject to the terms and conditions of the GNU General Public
31da177e4SLinus Torvalds  * License.  See the file "COPYING" in the main directory of this archive
41da177e4SLinus Torvalds  * for more details.
51da177e4SLinus Torvalds  *
679add627SJustin P. Mattock  * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
71da177e4SLinus Torvalds  * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Ralf Baechle (ralf@gnu.org)
81da177e4SLinus Torvalds  * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
91da177e4SLinus Torvalds  */
1061d73044SJames Hogan #include <linux/cpu_pm.h>
11a754f708SRalf Baechle #include <linux/hardirq.h>
121da177e4SLinus Torvalds #include <linux/init.h>
13db813fe5SRalf Baechle #include <linux/highmem.h>
141da177e4SLinus Torvalds #include <linux/kernel.h>
15641e97f3SRalf Baechle #include <linux/linkage.h>
16ff522058SRalf Baechle #include <linux/preempt.h>
171da177e4SLinus Torvalds #include <linux/sched.h>
18631330f5SRalf Baechle #include <linux/smp.h>
191da177e4SLinus Torvalds #include <linux/mm.h>
20d9ba5778SPaul Gortmaker #include <linux/export.h>
211da177e4SLinus Torvalds #include <linux/bitops.h>
226d4e9a8eSChristoph Hellwig #include <linux/dma-map-ops.h> /* for dma_default_coherent */
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #include <asm/bcache.h>
251da177e4SLinus Torvalds #include <asm/bootinfo.h>
26ec74e361SRalf Baechle #include <asm/cache.h>
271da177e4SLinus Torvalds #include <asm/cacheops.h>
281da177e4SLinus Torvalds #include <asm/cpu.h>
291da177e4SLinus Torvalds #include <asm/cpu-features.h>
3069f24d17SRalf Baechle #include <asm/cpu-type.h>
311da177e4SLinus Torvalds #include <asm/io.h>
321da177e4SLinus Torvalds #include <asm/page.h>
331da177e4SLinus Torvalds #include <asm/r4kcache.h>
34e001e528SRalf Baechle #include <asm/sections.h>
351da177e4SLinus Torvalds #include <asm/mmu_context.h>
36ba5187dbSThiemo Seufer #include <asm/cacheflush.h> /* for run_uncached() */
379cd9669bSDavid Daney #include <asm/traps.h>
38e83f7e02SPaul Burton #include <asm/mips-cps.h>
397f3f1d01SRalf Baechle 
407f3f1d01SRalf Baechle /*
41d374d937SJames Hogan  * Bits describing what cache ops an SMP callback function may perform.
42d374d937SJames Hogan  *
43d374d937SJames Hogan  * R4K_HIT   -	Virtual user or kernel address based cache operations. The
44d374d937SJames Hogan  *		active_mm must be checked before using user addresses, falling
45d374d937SJames Hogan  *		back to kmap.
46d374d937SJames Hogan  * R4K_INDEX -	Index based cache operations.
47d374d937SJames Hogan  */
48d374d937SJames Hogan 
49d374d937SJames Hogan #define R4K_HIT		BIT(0)
50d374d937SJames Hogan #define R4K_INDEX	BIT(1)
51d374d937SJames Hogan 
52d374d937SJames Hogan /**
53d374d937SJames Hogan  * r4k_op_needs_ipi() - Decide if a cache op needs to be done on every core.
54d374d937SJames Hogan  * @type:	Type of cache operations (R4K_HIT or R4K_INDEX).
55d374d937SJames Hogan  *
56d374d937SJames Hogan  * Decides whether a cache op needs to be performed on every core in the system.
57640511aeSJames Hogan  * This may change depending on the @type of cache operation, as well as the set
58640511aeSJames Hogan  * of online CPUs, so preemption should be disabled by the caller to prevent CPU
59640511aeSJames Hogan  * hotplug from changing the result.
60d374d937SJames Hogan  *
61d374d937SJames Hogan  * Returns:	1 if the cache operation @type should be done on every core in
62d374d937SJames Hogan  *		the system.
63d374d937SJames Hogan  *		0 if the cache operation @type is globalized and only needs to
64d374d937SJames Hogan  *		be performed on a simple CPU.
65d374d937SJames Hogan  */
r4k_op_needs_ipi(unsigned int type)66d374d937SJames Hogan static inline bool r4k_op_needs_ipi(unsigned int type)
67d374d937SJames Hogan {
68d374d937SJames Hogan 	/* The MIPS Coherence Manager (CM) globalizes address-based cache ops */
6911f76903SJames Hogan 	if (type == R4K_HIT && mips_cm_present())
70d374d937SJames Hogan 		return false;
71d374d937SJames Hogan 
72d374d937SJames Hogan 	/*
73d374d937SJames Hogan 	 * Hardware doesn't globalize the required cache ops, so SMP calls may
74640511aeSJames Hogan 	 * be needed, but only if there are foreign CPUs (non-siblings with
75640511aeSJames Hogan 	 * separate caches).
76d374d937SJames Hogan 	 */
77640511aeSJames Hogan 	/* cpu_foreign_map[] undeclared when !CONFIG_SMP */
78640511aeSJames Hogan #ifdef CONFIG_SMP
79640511aeSJames Hogan 	return !cpumask_empty(&cpu_foreign_map[0]);
80640511aeSJames Hogan #else
81640511aeSJames Hogan 	return false;
82640511aeSJames Hogan #endif
83d374d937SJames Hogan }
84d374d937SJames Hogan 
85d374d937SJames Hogan /*
867f3f1d01SRalf Baechle  * Special Variant of smp_call_function for use by cache functions:
877f3f1d01SRalf Baechle  *
887f3f1d01SRalf Baechle  *  o No return value
897f3f1d01SRalf Baechle  *  o collapses to normal function call on UP kernels
907f3f1d01SRalf Baechle  *  o collapses to normal function call on systems with a single shared
917f3f1d01SRalf Baechle  *    primary cache.
92c8c5f3fdSRalf Baechle  *  o doesn't disable interrupts on the local CPU
937f3f1d01SRalf Baechle  */
r4k_on_each_cpu(unsigned int type,void (* func)(void * info),void * info)94d374d937SJames Hogan static inline void r4k_on_each_cpu(unsigned int type,
95d374d937SJames Hogan 				   void (*func)(void *info), void *info)
967f3f1d01SRalf Baechle {
977f3f1d01SRalf Baechle 	preempt_disable();
98d374d937SJames Hogan 	if (r4k_op_needs_ipi(type))
99640511aeSJames Hogan 		smp_call_function_many(&cpu_foreign_map[smp_processor_id()],
100640511aeSJames Hogan 				       func, info, 1);
1017f3f1d01SRalf Baechle 	func(info);
1027f3f1d01SRalf Baechle 	preempt_enable();
1037f3f1d01SRalf Baechle }
1047f3f1d01SRalf Baechle 
105ec74e361SRalf Baechle /*
106ec74e361SRalf Baechle  * Must die.
107ec74e361SRalf Baechle  */
108ec74e361SRalf Baechle static unsigned long icache_size __read_mostly;
109ec74e361SRalf Baechle static unsigned long dcache_size __read_mostly;
110b2edcfc8SHuacai Chen static unsigned long vcache_size __read_mostly;
111ec74e361SRalf Baechle static unsigned long scache_size __read_mostly;
1121da177e4SLinus Torvalds 
113330cfe01SThiemo Seufer #define cpu_is_r4600_v1_x()	((read_c0_prid() & 0xfffffff0) == 0x00002010)
114330cfe01SThiemo Seufer #define cpu_is_r4600_v2_x()	((read_c0_prid() & 0xfffffff0) == 0x00002020)
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds #define R4600_HIT_CACHEOP_WAR_IMPL					\
1171da177e4SLinus Torvalds do {									\
11844def342SThomas Bogendoerfer 	if (IS_ENABLED(CONFIG_WAR_R4600_V2_HIT_CACHEOP) &&		\
11944def342SThomas Bogendoerfer 	    cpu_is_r4600_v2_x())					\
1201da177e4SLinus Torvalds 		*(volatile unsigned long *)CKSEG1;			\
1215e5b6527SThomas Bogendoerfer 	if (IS_ENABLED(CONFIG_WAR_R4600_V1_HIT_CACHEOP))					\
1221da177e4SLinus Torvalds 		__asm__ __volatile__("nop;nop;nop;nop");		\
1231da177e4SLinus Torvalds } while (0)
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds static void (*r4k_blast_dcache_page)(unsigned long addr);
1261da177e4SLinus Torvalds 
r4k_blast_dcache_page_dc32(unsigned long addr)1271da177e4SLinus Torvalds static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	R4600_HIT_CACHEOP_WAR_IMPL;
1301da177e4SLinus Torvalds 	blast_dcache32_page(addr);
1311da177e4SLinus Torvalds }
1321da177e4SLinus Torvalds 
r4k_blast_dcache_page_dc64(unsigned long addr)133605b7ef7SKevin Cernekee static inline void r4k_blast_dcache_page_dc64(unsigned long addr)
134605b7ef7SKevin Cernekee {
135605b7ef7SKevin Cernekee 	blast_dcache64_page(addr);
136605b7ef7SKevin Cernekee }
137605b7ef7SKevin Cernekee 
r4k_blast_dcache_page_dc128(unsigned long addr)13818a8cd63SDavid Daney static inline void r4k_blast_dcache_page_dc128(unsigned long addr)
13918a8cd63SDavid Daney {
14018a8cd63SDavid Daney 	blast_dcache128_page(addr);
14118a8cd63SDavid Daney }
14218a8cd63SDavid Daney 
r4k_blast_dcache_page_setup(void)143078a55fcSPaul Gortmaker static void r4k_blast_dcache_page_setup(void)
1441da177e4SLinus Torvalds {
1451da177e4SLinus Torvalds 	unsigned long  dc_lsize = cpu_dcache_line_size();
1461da177e4SLinus Torvalds 
14718a8cd63SDavid Daney 	switch (dc_lsize) {
14818a8cd63SDavid Daney 	case 0:
14973f40352SChris Dearman 		r4k_blast_dcache_page = (void *)cache_noop;
15018a8cd63SDavid Daney 		break;
15118a8cd63SDavid Daney 	case 16:
1521da177e4SLinus Torvalds 		r4k_blast_dcache_page = blast_dcache16_page;
15318a8cd63SDavid Daney 		break;
15418a8cd63SDavid Daney 	case 32:
1551da177e4SLinus Torvalds 		r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
15618a8cd63SDavid Daney 		break;
15718a8cd63SDavid Daney 	case 64:
158605b7ef7SKevin Cernekee 		r4k_blast_dcache_page = r4k_blast_dcache_page_dc64;
15918a8cd63SDavid Daney 		break;
16018a8cd63SDavid Daney 	case 128:
16118a8cd63SDavid Daney 		r4k_blast_dcache_page = r4k_blast_dcache_page_dc128;
16218a8cd63SDavid Daney 		break;
16318a8cd63SDavid Daney 	default:
16418a8cd63SDavid Daney 		break;
16518a8cd63SDavid Daney 	}
1661da177e4SLinus Torvalds }
1671da177e4SLinus Torvalds 
1684caa906eSLeonid Yegoshin #ifndef CONFIG_EVA
1694caa906eSLeonid Yegoshin #define r4k_blast_dcache_user_page  r4k_blast_dcache_page
1704caa906eSLeonid Yegoshin #else
1714caa906eSLeonid Yegoshin 
1724caa906eSLeonid Yegoshin static void (*r4k_blast_dcache_user_page)(unsigned long addr);
1734caa906eSLeonid Yegoshin 
r4k_blast_dcache_user_page_setup(void)1744caa906eSLeonid Yegoshin static void r4k_blast_dcache_user_page_setup(void)
1754caa906eSLeonid Yegoshin {
1764caa906eSLeonid Yegoshin 	unsigned long  dc_lsize = cpu_dcache_line_size();
1774caa906eSLeonid Yegoshin 
1784caa906eSLeonid Yegoshin 	if (dc_lsize == 0)
1794caa906eSLeonid Yegoshin 		r4k_blast_dcache_user_page = (void *)cache_noop;
1804caa906eSLeonid Yegoshin 	else if (dc_lsize == 16)
1814caa906eSLeonid Yegoshin 		r4k_blast_dcache_user_page = blast_dcache16_user_page;
1824caa906eSLeonid Yegoshin 	else if (dc_lsize == 32)
1834caa906eSLeonid Yegoshin 		r4k_blast_dcache_user_page = blast_dcache32_user_page;
1844caa906eSLeonid Yegoshin 	else if (dc_lsize == 64)
1854caa906eSLeonid Yegoshin 		r4k_blast_dcache_user_page = blast_dcache64_user_page;
1864caa906eSLeonid Yegoshin }
1874caa906eSLeonid Yegoshin 
1884caa906eSLeonid Yegoshin #endif
1894caa906eSLeonid Yegoshin 
190f2e3656dSSanjay Lal void (* r4k_blast_dcache)(void);
191f2e3656dSSanjay Lal EXPORT_SYMBOL(r4k_blast_dcache);
1921da177e4SLinus Torvalds 
r4k_blast_dcache_setup(void)193078a55fcSPaul Gortmaker static void r4k_blast_dcache_setup(void)
1941da177e4SLinus Torvalds {
1951da177e4SLinus Torvalds 	unsigned long dc_lsize = cpu_dcache_line_size();
1961da177e4SLinus Torvalds 
19773f40352SChris Dearman 	if (dc_lsize == 0)
19873f40352SChris Dearman 		r4k_blast_dcache = (void *)cache_noop;
19973f40352SChris Dearman 	else if (dc_lsize == 16)
2001da177e4SLinus Torvalds 		r4k_blast_dcache = blast_dcache16;
2011da177e4SLinus Torvalds 	else if (dc_lsize == 32)
2021da177e4SLinus Torvalds 		r4k_blast_dcache = blast_dcache32;
203605b7ef7SKevin Cernekee 	else if (dc_lsize == 64)
204605b7ef7SKevin Cernekee 		r4k_blast_dcache = blast_dcache64;
20518a8cd63SDavid Daney 	else if (dc_lsize == 128)
20618a8cd63SDavid Daney 		r4k_blast_dcache = blast_dcache128;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds 
20924a1c023SThomas Bogendoerfer /* force code alignment (used for CONFIG_WAR_TX49XX_ICACHE_INDEX_INV) */
2101da177e4SLinus Torvalds #define JUMP_TO_ALIGN(order) \
2111da177e4SLinus Torvalds 	__asm__ __volatile__( \
2121da177e4SLinus Torvalds 		"b\t1f\n\t" \
2131da177e4SLinus Torvalds 		".align\t" #order "\n\t" \
2141da177e4SLinus Torvalds 		"1:\n\t" \
2151da177e4SLinus Torvalds 		)
2161da177e4SLinus Torvalds #define CACHE32_UNROLL32_ALIGN	JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
2171da177e4SLinus Torvalds #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
2181da177e4SLinus Torvalds 
blast_r4600_v1_icache32(void)2191da177e4SLinus Torvalds static inline void blast_r4600_v1_icache32(void)
2201da177e4SLinus Torvalds {
2211da177e4SLinus Torvalds 	unsigned long flags;
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	local_irq_save(flags);
2241da177e4SLinus Torvalds 	blast_icache32();
2251da177e4SLinus Torvalds 	local_irq_restore(flags);
2261da177e4SLinus Torvalds }
2271da177e4SLinus Torvalds 
tx49_blast_icache32(void)2281da177e4SLinus Torvalds static inline void tx49_blast_icache32(void)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	unsigned long start = INDEX_BASE;
2311da177e4SLinus Torvalds 	unsigned long end = start + current_cpu_data.icache.waysize;
2321da177e4SLinus Torvalds 	unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
2331da177e4SLinus Torvalds 	unsigned long ws_end = current_cpu_data.icache.ways <<
2341da177e4SLinus Torvalds 			       current_cpu_data.icache.waybit;
2351da177e4SLinus Torvalds 	unsigned long ws, addr;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	CACHE32_UNROLL32_ALIGN2;
2381da177e4SLinus Torvalds 	/* I'm in even chunk.  blast odd chunks */
2391da177e4SLinus Torvalds 	for (ws = 0; ws < ws_end; ws += ws_inc)
2401da177e4SLinus Torvalds 		for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
2416baaeadaSPaul Burton 			cache_unroll(32, kernel_cache, Index_Invalidate_I,
2426baaeadaSPaul Burton 				     addr | ws, 32);
2431da177e4SLinus Torvalds 	CACHE32_UNROLL32_ALIGN;
2441da177e4SLinus Torvalds 	/* I'm in odd chunk.  blast even chunks */
2451da177e4SLinus Torvalds 	for (ws = 0; ws < ws_end; ws += ws_inc)
2461da177e4SLinus Torvalds 		for (addr = start; addr < end; addr += 0x400 * 2)
2476baaeadaSPaul Burton 			cache_unroll(32, kernel_cache, Index_Invalidate_I,
2486baaeadaSPaul Burton 				     addr | ws, 32);
2491da177e4SLinus Torvalds }
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds static void (* r4k_blast_icache_page)(unsigned long addr);
2521da177e4SLinus Torvalds 
r4k_blast_icache_page_setup(void)253078a55fcSPaul Gortmaker static void r4k_blast_icache_page_setup(void)
2541da177e4SLinus Torvalds {
2551da177e4SLinus Torvalds 	unsigned long ic_lsize = cpu_icache_line_size();
2561da177e4SLinus Torvalds 
25773f40352SChris Dearman 	if (ic_lsize == 0)
25873f40352SChris Dearman 		r4k_blast_icache_page = (void *)cache_noop;
25973f40352SChris Dearman 	else if (ic_lsize == 16)
2601da177e4SLinus Torvalds 		r4k_blast_icache_page = blast_icache16_page;
261268a2d60SJiaxun Yang 	else if (ic_lsize == 32 && current_cpu_type() == CPU_LOONGSON2EF)
26243a06847SAaro Koskinen 		r4k_blast_icache_page = loongson2_blast_icache32_page;
2631da177e4SLinus Torvalds 	else if (ic_lsize == 32)
2641da177e4SLinus Torvalds 		r4k_blast_icache_page = blast_icache32_page;
2651da177e4SLinus Torvalds 	else if (ic_lsize == 64)
2661da177e4SLinus Torvalds 		r4k_blast_icache_page = blast_icache64_page;
26718a8cd63SDavid Daney 	else if (ic_lsize == 128)
26818a8cd63SDavid Daney 		r4k_blast_icache_page = blast_icache128_page;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds 
2714caa906eSLeonid Yegoshin #ifndef CONFIG_EVA
2724caa906eSLeonid Yegoshin #define r4k_blast_icache_user_page  r4k_blast_icache_page
2734caa906eSLeonid Yegoshin #else
2744caa906eSLeonid Yegoshin 
2754caa906eSLeonid Yegoshin static void (*r4k_blast_icache_user_page)(unsigned long addr);
2764caa906eSLeonid Yegoshin 
r4k_blast_icache_user_page_setup(void)2779a8f4ea0SPaul Gortmaker static void r4k_blast_icache_user_page_setup(void)
2784caa906eSLeonid Yegoshin {
2794caa906eSLeonid Yegoshin 	unsigned long ic_lsize = cpu_icache_line_size();
2804caa906eSLeonid Yegoshin 
2814caa906eSLeonid Yegoshin 	if (ic_lsize == 0)
2824caa906eSLeonid Yegoshin 		r4k_blast_icache_user_page = (void *)cache_noop;
2834caa906eSLeonid Yegoshin 	else if (ic_lsize == 16)
2844caa906eSLeonid Yegoshin 		r4k_blast_icache_user_page = blast_icache16_user_page;
2854caa906eSLeonid Yegoshin 	else if (ic_lsize == 32)
2864caa906eSLeonid Yegoshin 		r4k_blast_icache_user_page = blast_icache32_user_page;
2874caa906eSLeonid Yegoshin 	else if (ic_lsize == 64)
2884caa906eSLeonid Yegoshin 		r4k_blast_icache_user_page = blast_icache64_user_page;
2894caa906eSLeonid Yegoshin }
2904caa906eSLeonid Yegoshin 
2914caa906eSLeonid Yegoshin #endif
2921da177e4SLinus Torvalds 
293f2e3656dSSanjay Lal void (* r4k_blast_icache)(void);
294f2e3656dSSanjay Lal EXPORT_SYMBOL(r4k_blast_icache);
2951da177e4SLinus Torvalds 
r4k_blast_icache_setup(void)296078a55fcSPaul Gortmaker static void r4k_blast_icache_setup(void)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	unsigned long ic_lsize = cpu_icache_line_size();
2991da177e4SLinus Torvalds 
30073f40352SChris Dearman 	if (ic_lsize == 0)
30173f40352SChris Dearman 		r4k_blast_icache = (void *)cache_noop;
30273f40352SChris Dearman 	else if (ic_lsize == 16)
3031da177e4SLinus Torvalds 		r4k_blast_icache = blast_icache16;
3041da177e4SLinus Torvalds 	else if (ic_lsize == 32) {
305802b8362SThomas Bogendoerfer 		if (IS_ENABLED(CONFIG_WAR_R4600_V1_INDEX_ICACHEOP) &&
306802b8362SThomas Bogendoerfer 		    cpu_is_r4600_v1_x())
3071da177e4SLinus Torvalds 			r4k_blast_icache = blast_r4600_v1_icache32;
30824a1c023SThomas Bogendoerfer 		else if (IS_ENABLED(CONFIG_WAR_TX49XX_ICACHE_INDEX_INV))
3091da177e4SLinus Torvalds 			r4k_blast_icache = tx49_blast_icache32;
310268a2d60SJiaxun Yang 		else if (current_cpu_type() == CPU_LOONGSON2EF)
31143a06847SAaro Koskinen 			r4k_blast_icache = loongson2_blast_icache32;
3121da177e4SLinus Torvalds 		else
3131da177e4SLinus Torvalds 			r4k_blast_icache = blast_icache32;
3141da177e4SLinus Torvalds 	} else if (ic_lsize == 64)
3151da177e4SLinus Torvalds 		r4k_blast_icache = blast_icache64;
31618a8cd63SDavid Daney 	else if (ic_lsize == 128)
31718a8cd63SDavid Daney 		r4k_blast_icache = blast_icache128;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds static void (* r4k_blast_scache_page)(unsigned long addr);
3211da177e4SLinus Torvalds 
r4k_blast_scache_page_setup(void)322078a55fcSPaul Gortmaker static void r4k_blast_scache_page_setup(void)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds 	unsigned long sc_lsize = cpu_scache_line_size();
3251da177e4SLinus Torvalds 
3264debe4f9SRalf Baechle 	if (scache_size == 0)
32773f40352SChris Dearman 		r4k_blast_scache_page = (void *)cache_noop;
3284debe4f9SRalf Baechle 	else if (sc_lsize == 16)
3291da177e4SLinus Torvalds 		r4k_blast_scache_page = blast_scache16_page;
3301da177e4SLinus Torvalds 	else if (sc_lsize == 32)
3311da177e4SLinus Torvalds 		r4k_blast_scache_page = blast_scache32_page;
3321da177e4SLinus Torvalds 	else if (sc_lsize == 64)
3331da177e4SLinus Torvalds 		r4k_blast_scache_page = blast_scache64_page;
3341da177e4SLinus Torvalds 	else if (sc_lsize == 128)
3351da177e4SLinus Torvalds 		r4k_blast_scache_page = blast_scache128_page;
3361da177e4SLinus Torvalds }
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds static void (* r4k_blast_scache)(void);
3391da177e4SLinus Torvalds 
r4k_blast_scache_setup(void)340078a55fcSPaul Gortmaker static void r4k_blast_scache_setup(void)
3411da177e4SLinus Torvalds {
3421da177e4SLinus Torvalds 	unsigned long sc_lsize = cpu_scache_line_size();
3431da177e4SLinus Torvalds 
3444debe4f9SRalf Baechle 	if (scache_size == 0)
34573f40352SChris Dearman 		r4k_blast_scache = (void *)cache_noop;
3464debe4f9SRalf Baechle 	else if (sc_lsize == 16)
3471da177e4SLinus Torvalds 		r4k_blast_scache = blast_scache16;
3481da177e4SLinus Torvalds 	else if (sc_lsize == 32)
3491da177e4SLinus Torvalds 		r4k_blast_scache = blast_scache32;
3501da177e4SLinus Torvalds 	else if (sc_lsize == 64)
3511da177e4SLinus Torvalds 		r4k_blast_scache = blast_scache64;
3521da177e4SLinus Torvalds 	else if (sc_lsize == 128)
3531da177e4SLinus Torvalds 		r4k_blast_scache = blast_scache128;
3541da177e4SLinus Torvalds }
3551da177e4SLinus Torvalds 
356bb53fdf3SHuacai Chen static void (*r4k_blast_scache_node)(long node);
357bb53fdf3SHuacai Chen 
r4k_blast_scache_node_setup(void)358bb53fdf3SHuacai Chen static void r4k_blast_scache_node_setup(void)
359bb53fdf3SHuacai Chen {
360bb53fdf3SHuacai Chen 	unsigned long sc_lsize = cpu_scache_line_size();
361bb53fdf3SHuacai Chen 
362268a2d60SJiaxun Yang 	if (current_cpu_type() != CPU_LOONGSON64)
363bb53fdf3SHuacai Chen 		r4k_blast_scache_node = (void *)cache_noop;
364bb53fdf3SHuacai Chen 	else if (sc_lsize == 16)
365bb53fdf3SHuacai Chen 		r4k_blast_scache_node = blast_scache16_node;
366bb53fdf3SHuacai Chen 	else if (sc_lsize == 32)
367bb53fdf3SHuacai Chen 		r4k_blast_scache_node = blast_scache32_node;
368bb53fdf3SHuacai Chen 	else if (sc_lsize == 64)
369bb53fdf3SHuacai Chen 		r4k_blast_scache_node = blast_scache64_node;
370bb53fdf3SHuacai Chen 	else if (sc_lsize == 128)
371bb53fdf3SHuacai Chen 		r4k_blast_scache_node = blast_scache128_node;
372bb53fdf3SHuacai Chen }
373bb53fdf3SHuacai Chen 
local_r4k___flush_cache_all(void * args)3741da177e4SLinus Torvalds static inline void local_r4k___flush_cache_all(void * args)
3751da177e4SLinus Torvalds {
37610cc3529SRalf Baechle 	switch (current_cpu_type()) {
377268a2d60SJiaxun Yang 	case CPU_LOONGSON2EF:
3781da177e4SLinus Torvalds 	case CPU_R4000SC:
3791da177e4SLinus Torvalds 	case CPU_R4000MC:
3801da177e4SLinus Torvalds 	case CPU_R4400SC:
3811da177e4SLinus Torvalds 	case CPU_R4400MC:
3821da177e4SLinus Torvalds 	case CPU_R10000:
3831da177e4SLinus Torvalds 	case CPU_R12000:
38444d921b2SKumba 	case CPU_R14000:
38530577391SJoshua Kinard 	case CPU_R16000:
38614bd8c08SRalf Baechle 		/*
38714bd8c08SRalf Baechle 		 * These caches are inclusive caches, that is, if something
38814bd8c08SRalf Baechle 		 * is not cached in the S-cache, we know it also won't be
38914bd8c08SRalf Baechle 		 * in one of the primary caches.
39014bd8c08SRalf Baechle 		 */
3911da177e4SLinus Torvalds 		r4k_blast_scache();
39214bd8c08SRalf Baechle 		break;
39314bd8c08SRalf Baechle 
394268a2d60SJiaxun Yang 	case CPU_LOONGSON64:
395bb53fdf3SHuacai Chen 		/* Use get_ebase_cpunum() for both NUMA=y/n */
396bb53fdf3SHuacai Chen 		r4k_blast_scache_node(get_ebase_cpunum() >> 2);
397bb53fdf3SHuacai Chen 		break;
398bb53fdf3SHuacai Chen 
399f675843dSFlorian Fainelli 	case CPU_BMIPS5000:
400f675843dSFlorian Fainelli 		r4k_blast_scache();
401f675843dSFlorian Fainelli 		__sync();
402f675843dSFlorian Fainelli 		break;
403f675843dSFlorian Fainelli 
40414bd8c08SRalf Baechle 	default:
40514bd8c08SRalf Baechle 		r4k_blast_dcache();
40614bd8c08SRalf Baechle 		r4k_blast_icache();
40714bd8c08SRalf Baechle 		break;
4081da177e4SLinus Torvalds 	}
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
r4k___flush_cache_all(void)4111da177e4SLinus Torvalds static void r4k___flush_cache_all(void)
4121da177e4SLinus Torvalds {
413d374d937SJames Hogan 	r4k_on_each_cpu(R4K_INDEX, local_r4k___flush_cache_all, NULL);
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds 
4166d758bfcSJames Hogan /**
4176d758bfcSJames Hogan  * has_valid_asid() - Determine if an mm already has an ASID.
4186d758bfcSJames Hogan  * @mm:		Memory map.
4196d758bfcSJames Hogan  * @type:	R4K_HIT or R4K_INDEX, type of cache op.
4206d758bfcSJames Hogan  *
4216d758bfcSJames Hogan  * Determines whether @mm already has an ASID on any of the CPUs which cache ops
4226d758bfcSJames Hogan  * of type @type within an r4k_on_each_cpu() call will affect. If
4236d758bfcSJames Hogan  * r4k_on_each_cpu() does an SMP call to a single VPE in each core, then the
4246d758bfcSJames Hogan  * scope of the operation is confined to sibling CPUs, otherwise all online CPUs
4256d758bfcSJames Hogan  * will need to be checked.
4266d758bfcSJames Hogan  *
4276d758bfcSJames Hogan  * Must be called in non-preemptive context.
4286d758bfcSJames Hogan  *
4296d758bfcSJames Hogan  * Returns:	1 if the CPUs affected by @type cache ops have an ASID for @mm.
4306d758bfcSJames Hogan  *		0 otherwise.
4316d758bfcSJames Hogan  */
has_valid_asid(const struct mm_struct * mm,unsigned int type)4326d758bfcSJames Hogan static inline int has_valid_asid(const struct mm_struct *mm, unsigned int type)
433a76ab5c1SRalf Baechle {
4346d758bfcSJames Hogan 	unsigned int i;
4356d758bfcSJames Hogan 	const cpumask_t *mask = cpu_present_mask;
436a76ab5c1SRalf Baechle 
437c8790d65SPaul Burton 	if (cpu_has_mmid)
438c8790d65SPaul Burton 		return cpu_context(0, mm) != 0;
439c8790d65SPaul Burton 
4406d758bfcSJames Hogan 	/* cpu_sibling_map[] undeclared when !CONFIG_SMP */
4416d758bfcSJames Hogan #ifdef CONFIG_SMP
4426d758bfcSJames Hogan 	/*
4436d758bfcSJames Hogan 	 * If r4k_on_each_cpu does SMP calls, it does them to a single VPE in
4446d758bfcSJames Hogan 	 * each foreign core, so we only need to worry about siblings.
4456d758bfcSJames Hogan 	 * Otherwise we need to worry about all present CPUs.
4466d758bfcSJames Hogan 	 */
4476d758bfcSJames Hogan 	if (r4k_op_needs_ipi(type))
4486d758bfcSJames Hogan 		mask = &cpu_sibling_map[smp_processor_id()];
4496d758bfcSJames Hogan #endif
4506d758bfcSJames Hogan 	for_each_cpu(i, mask)
451a76ab5c1SRalf Baechle 		if (cpu_context(i, mm))
452a76ab5c1SRalf Baechle 			return 1;
453a76ab5c1SRalf Baechle 	return 0;
454a76ab5c1SRalf Baechle }
455a76ab5c1SRalf Baechle 
r4k__flush_cache_vmap(void)4569c5a3d72SRalf Baechle static void r4k__flush_cache_vmap(void)
4579c5a3d72SRalf Baechle {
4589c5a3d72SRalf Baechle 	r4k_blast_dcache();
4599c5a3d72SRalf Baechle }
4609c5a3d72SRalf Baechle 
r4k__flush_cache_vunmap(void)4619c5a3d72SRalf Baechle static void r4k__flush_cache_vunmap(void)
4629c5a3d72SRalf Baechle {
4639c5a3d72SRalf Baechle 	r4k_blast_dcache();
4649c5a3d72SRalf Baechle }
4659c5a3d72SRalf Baechle 
466a05c3920SJames Hogan /*
467a05c3920SJames Hogan  * Note: flush_tlb_range() assumes flush_cache_range() sufficiently flushes
468a05c3920SJames Hogan  * whole caches when vma is executable.
469a05c3920SJames Hogan  */
local_r4k_flush_cache_range(void * args)4701da177e4SLinus Torvalds static inline void local_r4k_flush_cache_range(void * args)
4711da177e4SLinus Torvalds {
4721da177e4SLinus Torvalds 	struct vm_area_struct *vma = args;
4732eaa7ec2SRalf Baechle 	int exec = vma->vm_flags & VM_EXEC;
4741da177e4SLinus Torvalds 
4756d758bfcSJames Hogan 	if (!has_valid_asid(vma->vm_mm, R4K_INDEX))
4761da177e4SLinus Torvalds 		return;
4771da177e4SLinus Torvalds 
478b2a3c5beSJames Hogan 	/*
479b2a3c5beSJames Hogan 	 * If dcache can alias, we must blast it since mapping is changing.
480b2a3c5beSJames Hogan 	 * If executable, we must ensure any dirty lines are written back far
481b2a3c5beSJames Hogan 	 * enough to be visible to icache.
482b2a3c5beSJames Hogan 	 */
483b2a3c5beSJames Hogan 	if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
4841da177e4SLinus Torvalds 		r4k_blast_dcache();
485b2a3c5beSJames Hogan 	/* If executable, blast stale lines from icache */
4862eaa7ec2SRalf Baechle 	if (exec)
4872eaa7ec2SRalf Baechle 		r4k_blast_icache();
4881da177e4SLinus Torvalds }
4891da177e4SLinus Torvalds 
r4k_flush_cache_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)4901da177e4SLinus Torvalds static void r4k_flush_cache_range(struct vm_area_struct *vma,
4911da177e4SLinus Torvalds 	unsigned long start, unsigned long end)
4921da177e4SLinus Torvalds {
4932eaa7ec2SRalf Baechle 	int exec = vma->vm_flags & VM_EXEC;
4940550d9d1SAtsushi Nemoto 
495b2a3c5beSJames Hogan 	if (cpu_has_dc_aliases || exec)
496d374d937SJames Hogan 		r4k_on_each_cpu(R4K_INDEX, local_r4k_flush_cache_range, vma);
4971da177e4SLinus Torvalds }
4981da177e4SLinus Torvalds 
local_r4k_flush_cache_mm(void * args)4991da177e4SLinus Torvalds static inline void local_r4k_flush_cache_mm(void * args)
5001da177e4SLinus Torvalds {
5011da177e4SLinus Torvalds 	struct mm_struct *mm = args;
5021da177e4SLinus Torvalds 
5036d758bfcSJames Hogan 	if (!has_valid_asid(mm, R4K_INDEX))
5041da177e4SLinus Torvalds 		return;
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds 	/*
5071da177e4SLinus Torvalds 	 * Kludge alert.  For obscure reasons R4000SC and R4400SC go nuts if we
50830577391SJoshua Kinard 	 * only flush the primary caches but R1x000 behave sane ...
509617667baSRalf Baechle 	 * R4000SC and R4400SC indexed S-cache ops also invalidate primary
510617667baSRalf Baechle 	 * caches, so we can bail out early.
5111da177e4SLinus Torvalds 	 */
51210cc3529SRalf Baechle 	if (current_cpu_type() == CPU_R4000SC ||
51310cc3529SRalf Baechle 	    current_cpu_type() == CPU_R4000MC ||
51410cc3529SRalf Baechle 	    current_cpu_type() == CPU_R4400SC ||
51510cc3529SRalf Baechle 	    current_cpu_type() == CPU_R4400MC) {
5161da177e4SLinus Torvalds 		r4k_blast_scache();
517617667baSRalf Baechle 		return;
518617667baSRalf Baechle 	}
519617667baSRalf Baechle 
520617667baSRalf Baechle 	r4k_blast_dcache();
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
r4k_flush_cache_mm(struct mm_struct * mm)5231da177e4SLinus Torvalds static void r4k_flush_cache_mm(struct mm_struct *mm)
5241da177e4SLinus Torvalds {
5251da177e4SLinus Torvalds 	if (!cpu_has_dc_aliases)
5261da177e4SLinus Torvalds 		return;
5271da177e4SLinus Torvalds 
528d374d937SJames Hogan 	r4k_on_each_cpu(R4K_INDEX, local_r4k_flush_cache_mm, mm);
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds struct flush_cache_page_args {
5321da177e4SLinus Torvalds 	struct vm_area_struct *vma;
5336ec25809SRalf Baechle 	unsigned long addr;
534de62893bSAtsushi Nemoto 	unsigned long pfn;
5351da177e4SLinus Torvalds };
5361da177e4SLinus Torvalds 
local_r4k_flush_cache_page(void * args)5371da177e4SLinus Torvalds static inline void local_r4k_flush_cache_page(void *args)
5381da177e4SLinus Torvalds {
5391da177e4SLinus Torvalds 	struct flush_cache_page_args *fcp_args = args;
5401da177e4SLinus Torvalds 	struct vm_area_struct *vma = fcp_args->vma;
5416ec25809SRalf Baechle 	unsigned long addr = fcp_args->addr;
542db813fe5SRalf Baechle 	struct page *page = pfn_to_page(fcp_args->pfn);
5431da177e4SLinus Torvalds 	int exec = vma->vm_flags & VM_EXEC;
5441da177e4SLinus Torvalds 	struct mm_struct *mm = vma->vm_mm;
545c9c5023dSRalf Baechle 	int map_coherent = 0;
5461da177e4SLinus Torvalds 	pmd_t *pmdp;
5471da177e4SLinus Torvalds 	pte_t *ptep;
548db813fe5SRalf Baechle 	void *vaddr;
5491da177e4SLinus Torvalds 
55079acf83eSRalf Baechle 	/*
5516d758bfcSJames Hogan 	 * If owns no valid ASID yet, cannot possibly have gotten
55279acf83eSRalf Baechle 	 * this page into the cache.
55379acf83eSRalf Baechle 	 */
5546d758bfcSJames Hogan 	if (!has_valid_asid(mm, R4K_HIT))
55579acf83eSRalf Baechle 		return;
55679acf83eSRalf Baechle 
5576ec25809SRalf Baechle 	addr &= PAGE_MASK;
558e05c7b1fSMike Rapoport 	pmdp = pmd_off(mm, addr);
559e05c7b1fSMike Rapoport 	ptep = pte_offset_kernel(pmdp, addr);
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	/*
5621da177e4SLinus Torvalds 	 * If the page isn't marked valid, the page cannot possibly be
5631da177e4SLinus Torvalds 	 * in the cache.
5641da177e4SLinus Torvalds 	 */
565526af35eSRalf Baechle 	if (!(pte_present(*ptep)))
5661da177e4SLinus Torvalds 		return;
5671da177e4SLinus Torvalds 
568db813fe5SRalf Baechle 	if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
569db813fe5SRalf Baechle 		vaddr = NULL;
570db813fe5SRalf Baechle 	else {
571*15fa3e8eSMatthew Wilcox (Oracle) 		struct folio *folio = page_folio(page);
5721da177e4SLinus Torvalds 		/*
573db813fe5SRalf Baechle 		 * Use kmap_coherent or kmap_atomic to do flushes for
574db813fe5SRalf Baechle 		 * another ASID than the current one.
5751da177e4SLinus Torvalds 		 */
576c9c5023dSRalf Baechle 		map_coherent = (cpu_has_dc_aliases &&
577*15fa3e8eSMatthew Wilcox (Oracle) 				folio_mapped(folio) &&
578*15fa3e8eSMatthew Wilcox (Oracle) 				!folio_test_dcache_dirty(folio));
579c9c5023dSRalf Baechle 		if (map_coherent)
580db813fe5SRalf Baechle 			vaddr = kmap_coherent(page, addr);
581db813fe5SRalf Baechle 		else
5829c02048fSCong Wang 			vaddr = kmap_atomic(page);
583db813fe5SRalf Baechle 		addr = (unsigned long)vaddr;
584db813fe5SRalf Baechle 	}
585db813fe5SRalf Baechle 
5861da177e4SLinus Torvalds 	if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
58780ca69f4SMarkos Chandras 		vaddr ? r4k_blast_dcache_page(addr) :
58880ca69f4SMarkos Chandras 			r4k_blast_dcache_user_page(addr);
58939b8d525SRalf Baechle 		if (exec && !cpu_icache_snoops_remote_store)
59039b8d525SRalf Baechle 			r4k_blast_scache_page(addr);
5911da177e4SLinus Torvalds 	}
5921da177e4SLinus Torvalds 	if (exec) {
593db813fe5SRalf Baechle 		if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
5949a27324fSPaul Burton 			drop_mmu_context(mm);
5951da177e4SLinus Torvalds 		} else
59680ca69f4SMarkos Chandras 			vaddr ? r4k_blast_icache_page(addr) :
59780ca69f4SMarkos Chandras 				r4k_blast_icache_user_page(addr);
598db813fe5SRalf Baechle 	}
599db813fe5SRalf Baechle 
600db813fe5SRalf Baechle 	if (vaddr) {
601c9c5023dSRalf Baechle 		if (map_coherent)
602db813fe5SRalf Baechle 			kunmap_coherent();
603db813fe5SRalf Baechle 		else
6049c02048fSCong Wang 			kunmap_atomic(vaddr);
6051da177e4SLinus Torvalds 	}
6061da177e4SLinus Torvalds }
6071da177e4SLinus Torvalds 
r4k_flush_cache_page(struct vm_area_struct * vma,unsigned long addr,unsigned long pfn)6086ec25809SRalf Baechle static void r4k_flush_cache_page(struct vm_area_struct *vma,
6096ec25809SRalf Baechle 	unsigned long addr, unsigned long pfn)
6101da177e4SLinus Torvalds {
6111da177e4SLinus Torvalds 	struct flush_cache_page_args args;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	args.vma = vma;
6146ec25809SRalf Baechle 	args.addr = addr;
615de62893bSAtsushi Nemoto 	args.pfn = pfn;
6161da177e4SLinus Torvalds 
617d374d937SJames Hogan 	r4k_on_each_cpu(R4K_HIT, local_r4k_flush_cache_page, &args);
6181da177e4SLinus Torvalds }
6191da177e4SLinus Torvalds 
local_r4k_flush_data_cache_page(void * addr)6201da177e4SLinus Torvalds static inline void local_r4k_flush_data_cache_page(void * addr)
6211da177e4SLinus Torvalds {
6221da177e4SLinus Torvalds 	r4k_blast_dcache_page((unsigned long) addr);
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds 
r4k_flush_data_cache_page(unsigned long addr)6251da177e4SLinus Torvalds static void r4k_flush_data_cache_page(unsigned long addr)
6261da177e4SLinus Torvalds {
627a754f708SRalf Baechle 	if (in_atomic())
628a754f708SRalf Baechle 		local_r4k_flush_data_cache_page((void *)addr);
629a754f708SRalf Baechle 	else
630d374d937SJames Hogan 		r4k_on_each_cpu(R4K_HIT, local_r4k_flush_data_cache_page,
631d374d937SJames Hogan 				(void *) addr);
6321da177e4SLinus Torvalds }
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds struct flush_icache_range_args {
635d4264f18SAtsushi Nemoto 	unsigned long start;
636d4264f18SAtsushi Nemoto 	unsigned long end;
63727b93d9cSJames Hogan 	unsigned int type;
638b2ff7171SJames Hogan 	bool user;
6391da177e4SLinus Torvalds };
6401da177e4SLinus Torvalds 
__local_r4k_flush_icache_range(unsigned long start,unsigned long end,unsigned int type,bool user)64127b93d9cSJames Hogan static inline void __local_r4k_flush_icache_range(unsigned long start,
64227b93d9cSJames Hogan 						  unsigned long end,
643b2ff7171SJames Hogan 						  unsigned int type,
644b2ff7171SJames Hogan 						  bool user)
6451da177e4SLinus Torvalds {
6461da177e4SLinus Torvalds 	if (!cpu_has_ic_fills_f_dc) {
64727b93d9cSJames Hogan 		if (type == R4K_INDEX ||
64827b93d9cSJames Hogan 		    (type & R4K_INDEX && end - start >= dcache_size)) {
6491da177e4SLinus Torvalds 			r4k_blast_dcache();
6501da177e4SLinus Torvalds 		} else {
65110a3dabdSThiemo Seufer 			R4600_HIT_CACHEOP_WAR_IMPL;
652b2ff7171SJames Hogan 			if (user)
65341700e73SAtsushi Nemoto 				protected_blast_dcache_range(start, end);
654b2ff7171SJames Hogan 			else
655b2ff7171SJames Hogan 				blast_dcache_range(start, end);
6561da177e4SLinus Torvalds 		}
6571da177e4SLinus Torvalds 	}
6581da177e4SLinus Torvalds 
65927b93d9cSJames Hogan 	if (type == R4K_INDEX ||
66027b93d9cSJames Hogan 	    (type & R4K_INDEX && end - start > icache_size))
6611da177e4SLinus Torvalds 		r4k_blast_icache();
66214bd8c08SRalf Baechle 	else {
66314bd8c08SRalf Baechle 		switch (boot_cpu_type()) {
664268a2d60SJiaxun Yang 		case CPU_LOONGSON2EF:
665bad009feSHuacai Chen 			protected_loongson2_blast_icache_range(start, end);
66614bd8c08SRalf Baechle 			break;
66714bd8c08SRalf Baechle 
66814bd8c08SRalf Baechle 		default:
669b2ff7171SJames Hogan 			if (user)
670bad009feSHuacai Chen 				protected_blast_icache_range(start, end);
671b2ff7171SJames Hogan 			else
672b2ff7171SJames Hogan 				blast_icache_range(start, end);
67314bd8c08SRalf Baechle 			break;
67414bd8c08SRalf Baechle 		}
67514bd8c08SRalf Baechle 	}
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
local_r4k_flush_icache_range(unsigned long start,unsigned long end)67827b93d9cSJames Hogan static inline void local_r4k_flush_icache_range(unsigned long start,
67927b93d9cSJames Hogan 						unsigned long end)
68027b93d9cSJames Hogan {
681b2ff7171SJames Hogan 	__local_r4k_flush_icache_range(start, end, R4K_HIT | R4K_INDEX, false);
682b2ff7171SJames Hogan }
683b2ff7171SJames Hogan 
local_r4k_flush_icache_user_range(unsigned long start,unsigned long end)684b2ff7171SJames Hogan static inline void local_r4k_flush_icache_user_range(unsigned long start,
685b2ff7171SJames Hogan 						     unsigned long end)
686b2ff7171SJames Hogan {
687b2ff7171SJames Hogan 	__local_r4k_flush_icache_range(start, end, R4K_HIT | R4K_INDEX, true);
68827b93d9cSJames Hogan }
68927b93d9cSJames Hogan 
local_r4k_flush_icache_range_ipi(void * args)690e0cee3eeSThomas Bogendoerfer static inline void local_r4k_flush_icache_range_ipi(void *args)
691e0cee3eeSThomas Bogendoerfer {
692e0cee3eeSThomas Bogendoerfer 	struct flush_icache_range_args *fir_args = args;
693e0cee3eeSThomas Bogendoerfer 	unsigned long start = fir_args->start;
694e0cee3eeSThomas Bogendoerfer 	unsigned long end = fir_args->end;
69527b93d9cSJames Hogan 	unsigned int type = fir_args->type;
696b2ff7171SJames Hogan 	bool user = fir_args->user;
697e0cee3eeSThomas Bogendoerfer 
698b2ff7171SJames Hogan 	__local_r4k_flush_icache_range(start, end, type, user);
699e0cee3eeSThomas Bogendoerfer }
700e0cee3eeSThomas Bogendoerfer 
__r4k_flush_icache_range(unsigned long start,unsigned long end,bool user)701b2ff7171SJames Hogan static void __r4k_flush_icache_range(unsigned long start, unsigned long end,
702b2ff7171SJames Hogan 				     bool user)
7031da177e4SLinus Torvalds {
7041da177e4SLinus Torvalds 	struct flush_icache_range_args args;
705f70ddc07SJames Hogan 	unsigned long size, cache_size;
7061da177e4SLinus Torvalds 
7071da177e4SLinus Torvalds 	args.start = start;
7081da177e4SLinus Torvalds 	args.end = end;
70927b93d9cSJames Hogan 	args.type = R4K_HIT | R4K_INDEX;
710b2ff7171SJames Hogan 	args.user = user;
7111da177e4SLinus Torvalds 
712f70ddc07SJames Hogan 	/*
713f70ddc07SJames Hogan 	 * Indexed cache ops require an SMP call.
714f70ddc07SJames Hogan 	 * Consider if that can or should be avoided.
715f70ddc07SJames Hogan 	 */
716f70ddc07SJames Hogan 	preempt_disable();
717f70ddc07SJames Hogan 	if (r4k_op_needs_ipi(R4K_INDEX) && !r4k_op_needs_ipi(R4K_HIT)) {
718f70ddc07SJames Hogan 		/*
719f70ddc07SJames Hogan 		 * If address-based cache ops don't require an SMP call, then
720f70ddc07SJames Hogan 		 * use them exclusively for small flushes.
721f70ddc07SJames Hogan 		 */
722801f823dSPaul Burton 		size = end - start;
723f70ddc07SJames Hogan 		cache_size = icache_size;
724f70ddc07SJames Hogan 		if (!cpu_has_ic_fills_f_dc) {
725f70ddc07SJames Hogan 			size *= 2;
726f70ddc07SJames Hogan 			cache_size += dcache_size;
727f70ddc07SJames Hogan 		}
728f70ddc07SJames Hogan 		if (size <= cache_size)
729f70ddc07SJames Hogan 			args.type &= ~R4K_INDEX;
730f70ddc07SJames Hogan 	}
73127b93d9cSJames Hogan 	r4k_on_each_cpu(args.type, local_r4k_flush_icache_range_ipi, &args);
732f70ddc07SJames Hogan 	preempt_enable();
733cc61c1feSRalf Baechle 	instruction_hazard();
7341da177e4SLinus Torvalds }
7351da177e4SLinus Torvalds 
r4k_flush_icache_range(unsigned long start,unsigned long end)736b2ff7171SJames Hogan static void r4k_flush_icache_range(unsigned long start, unsigned long end)
737b2ff7171SJames Hogan {
738b2ff7171SJames Hogan 	return __r4k_flush_icache_range(start, end, false);
739b2ff7171SJames Hogan }
740b2ff7171SJames Hogan 
r4k_flush_icache_user_range(unsigned long start,unsigned long end)741b2ff7171SJames Hogan static void r4k_flush_icache_user_range(unsigned long start, unsigned long end)
742b2ff7171SJames Hogan {
743b2ff7171SJames Hogan 	return __r4k_flush_icache_range(start, end, true);
744b2ff7171SJames Hogan }
745b2ff7171SJames Hogan 
746972dc3b7SChristoph Hellwig #ifdef CONFIG_DMA_NONCOHERENT
7471da177e4SLinus Torvalds 
r4k_dma_cache_wback_inv(unsigned long addr,unsigned long size)7481da177e4SLinus Torvalds static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
7491da177e4SLinus Torvalds {
7501da177e4SLinus Torvalds 	/* Catch bad driver code */
751d4da0e97SPaul Burton 	if (WARN_ON(size == 0))
752d4da0e97SPaul Burton 		return;
7531da177e4SLinus Torvalds 
754ff522058SRalf Baechle 	preempt_disable();
755fc5d2d27SRalf Baechle 	if (cpu_has_inclusive_pcaches) {
756bb53fdf3SHuacai Chen 		if (size >= scache_size) {
757268a2d60SJiaxun Yang 			if (current_cpu_type() != CPU_LOONGSON64)
7581da177e4SLinus Torvalds 				r4k_blast_scache();
75941700e73SAtsushi Nemoto 			else
760bb53fdf3SHuacai Chen 				r4k_blast_scache_node(pa_to_nid(addr));
761bb53fdf3SHuacai Chen 		} else {
76241700e73SAtsushi Nemoto 			blast_scache_range(addr, addr + size);
763bb53fdf3SHuacai Chen 		}
7645596b0b2SYoichi Yuasa 		preempt_enable();
765d0023c4aSKevin Cernekee 		__sync();
7661da177e4SLinus Torvalds 		return;
7671da177e4SLinus Torvalds 	}
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	/*
7701da177e4SLinus Torvalds 	 * Either no secondary cache or the available caches don't have the
7711da177e4SLinus Torvalds 	 * subset property so we have to flush the primary caches
77255a2aa08SNeilBrown 	 * explicitly.
77355a2aa08SNeilBrown 	 * If we would need IPI to perform an INDEX-type operation, then
77455a2aa08SNeilBrown 	 * we have to use the HIT-type alternative as IPI cannot be used
77555a2aa08SNeilBrown 	 * here due to interrupts possibly being disabled.
7761da177e4SLinus Torvalds 	 */
77755a2aa08SNeilBrown 	if (!r4k_op_needs_ipi(R4K_INDEX) && size >= dcache_size) {
7781da177e4SLinus Torvalds 		r4k_blast_dcache();
7791da177e4SLinus Torvalds 	} else {
7801da177e4SLinus Torvalds 		R4600_HIT_CACHEOP_WAR_IMPL;
78141700e73SAtsushi Nemoto 		blast_dcache_range(addr, addr + size);
7821da177e4SLinus Torvalds 	}
783ff522058SRalf Baechle 	preempt_enable();
7841da177e4SLinus Torvalds 
7851da177e4SLinus Torvalds 	bc_wback_inv(addr, size);
786d0023c4aSKevin Cernekee 	__sync();
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
prefetch_cache_inv(unsigned long addr,unsigned long size)789be280764SKamal Dasu static void prefetch_cache_inv(unsigned long addr, unsigned long size)
790be280764SKamal Dasu {
791be280764SKamal Dasu 	unsigned int linesz = cpu_scache_line_size();
792be280764SKamal Dasu 	unsigned long addr0 = addr, addr1;
793be280764SKamal Dasu 
794be280764SKamal Dasu 	addr0 &= ~(linesz - 1);
795be280764SKamal Dasu 	addr1 = (addr0 + size - 1) & ~(linesz - 1);
796be280764SKamal Dasu 
797be280764SKamal Dasu 	protected_writeback_scache_line(addr0);
798be280764SKamal Dasu 	if (likely(addr1 != addr0))
799be280764SKamal Dasu 		protected_writeback_scache_line(addr1);
800be280764SKamal Dasu 	else
801be280764SKamal Dasu 		return;
802be280764SKamal Dasu 
803be280764SKamal Dasu 	addr0 += linesz;
804be280764SKamal Dasu 	if (likely(addr1 != addr0))
805be280764SKamal Dasu 		protected_writeback_scache_line(addr0);
806be280764SKamal Dasu 	else
807be280764SKamal Dasu 		return;
808be280764SKamal Dasu 
809be280764SKamal Dasu 	addr1 -= linesz;
810be280764SKamal Dasu 	if (likely(addr1 > addr0))
811be280764SKamal Dasu 		protected_writeback_scache_line(addr0);
812be280764SKamal Dasu }
813be280764SKamal Dasu 
r4k_dma_cache_inv(unsigned long addr,unsigned long size)8141da177e4SLinus Torvalds static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
8151da177e4SLinus Torvalds {
8161da177e4SLinus Torvalds 	/* Catch bad driver code */
817d4da0e97SPaul Burton 	if (WARN_ON(size == 0))
818d4da0e97SPaul Burton 		return;
8191da177e4SLinus Torvalds 
820ff522058SRalf Baechle 	preempt_disable();
821be280764SKamal Dasu 
822be280764SKamal Dasu 	if (current_cpu_type() == CPU_BMIPS5000)
823be280764SKamal Dasu 		prefetch_cache_inv(addr, size);
824be280764SKamal Dasu 
825fc5d2d27SRalf Baechle 	if (cpu_has_inclusive_pcaches) {
826bb53fdf3SHuacai Chen 		if (size >= scache_size) {
827268a2d60SJiaxun Yang 			if (current_cpu_type() != CPU_LOONGSON64)
8281da177e4SLinus Torvalds 				r4k_blast_scache();
829bb53fdf3SHuacai Chen 			else
830bb53fdf3SHuacai Chen 				r4k_blast_scache_node(pa_to_nid(addr));
831bb53fdf3SHuacai Chen 		} else {
832a8ca8b64SRalf Baechle 			/*
833a8ca8b64SRalf Baechle 			 * There is no clearly documented alignment requirement
834a8ca8b64SRalf Baechle 			 * for the cache instruction on MIPS processors and
835a8ca8b64SRalf Baechle 			 * some processors, among them the RM5200 and RM7000
836a8ca8b64SRalf Baechle 			 * QED processors will throw an address error for cache
837a8ca8b64SRalf Baechle 			 * hit ops with insufficient alignment.	 Solved by
838a8ca8b64SRalf Baechle 			 * aligning the address to cache line size.
839a8ca8b64SRalf Baechle 			 */
840e9c33572SThomas Bogendoerfer 			blast_inv_scache_range(addr, addr + size);
841a8ca8b64SRalf Baechle 		}
8425596b0b2SYoichi Yuasa 		preempt_enable();
843d0023c4aSKevin Cernekee 		__sync();
8441da177e4SLinus Torvalds 		return;
8451da177e4SLinus Torvalds 	}
8461da177e4SLinus Torvalds 
84755a2aa08SNeilBrown 	if (!r4k_op_needs_ipi(R4K_INDEX) && size >= dcache_size) {
8481da177e4SLinus Torvalds 		r4k_blast_dcache();
8491da177e4SLinus Torvalds 	} else {
8501da177e4SLinus Torvalds 		R4600_HIT_CACHEOP_WAR_IMPL;
851e9c33572SThomas Bogendoerfer 		blast_inv_dcache_range(addr, addr + size);
8521da177e4SLinus Torvalds 	}
853ff522058SRalf Baechle 	preempt_enable();
8541da177e4SLinus Torvalds 
8551da177e4SLinus Torvalds 	bc_inv(addr, size);
856d0023c4aSKevin Cernekee 	__sync();
8571da177e4SLinus Torvalds }
858972dc3b7SChristoph Hellwig #endif /* CONFIG_DMA_NONCOHERENT */
8591da177e4SLinus Torvalds 
r4k_flush_icache_all(void)8601da177e4SLinus Torvalds static void r4k_flush_icache_all(void)
8611da177e4SLinus Torvalds {
8621da177e4SLinus Torvalds 	if (cpu_has_vtag_icache)
8631da177e4SLinus Torvalds 		r4k_blast_icache();
8641da177e4SLinus Torvalds }
8651da177e4SLinus Torvalds 
866d9cdc901SRalf Baechle struct flush_kernel_vmap_range_args {
867d9cdc901SRalf Baechle 	unsigned long	vaddr;
868d9cdc901SRalf Baechle 	int		size;
869d9cdc901SRalf Baechle };
870d9cdc901SRalf Baechle 
local_r4k_flush_kernel_vmap_range_index(void * args)871a9341ae2SJames Hogan static inline void local_r4k_flush_kernel_vmap_range_index(void *args)
872a9341ae2SJames Hogan {
873a9341ae2SJames Hogan 	/*
874a9341ae2SJames Hogan 	 * Aliases only affect the primary caches so don't bother with
875a9341ae2SJames Hogan 	 * S-caches or T-caches.
876a9341ae2SJames Hogan 	 */
877a9341ae2SJames Hogan 	r4k_blast_dcache();
878a9341ae2SJames Hogan }
879a9341ae2SJames Hogan 
local_r4k_flush_kernel_vmap_range(void * args)880d9cdc901SRalf Baechle static inline void local_r4k_flush_kernel_vmap_range(void *args)
881d9cdc901SRalf Baechle {
882d9cdc901SRalf Baechle 	struct flush_kernel_vmap_range_args *vmra = args;
883d9cdc901SRalf Baechle 	unsigned long vaddr = vmra->vaddr;
884d9cdc901SRalf Baechle 	int size = vmra->size;
885d9cdc901SRalf Baechle 
886d9cdc901SRalf Baechle 	/*
887d9cdc901SRalf Baechle 	 * Aliases only affect the primary caches so don't bother with
888d9cdc901SRalf Baechle 	 * S-caches or T-caches.
889d9cdc901SRalf Baechle 	 */
890d9cdc901SRalf Baechle 	R4600_HIT_CACHEOP_WAR_IMPL;
891d9cdc901SRalf Baechle 	blast_dcache_range(vaddr, vaddr + size);
892d9cdc901SRalf Baechle }
893d9cdc901SRalf Baechle 
r4k_flush_kernel_vmap_range(unsigned long vaddr,int size)894d9cdc901SRalf Baechle static void r4k_flush_kernel_vmap_range(unsigned long vaddr, int size)
895d9cdc901SRalf Baechle {
896d9cdc901SRalf Baechle 	struct flush_kernel_vmap_range_args args;
897d9cdc901SRalf Baechle 
898d9cdc901SRalf Baechle 	args.vaddr = (unsigned long) vaddr;
899d9cdc901SRalf Baechle 	args.size = size;
900d9cdc901SRalf Baechle 
901a9341ae2SJames Hogan 	if (size >= dcache_size)
902a9341ae2SJames Hogan 		r4k_on_each_cpu(R4K_INDEX,
903a9341ae2SJames Hogan 				local_r4k_flush_kernel_vmap_range_index, NULL);
904a9341ae2SJames Hogan 	else
905a9341ae2SJames Hogan 		r4k_on_each_cpu(R4K_HIT, local_r4k_flush_kernel_vmap_range,
906d374d937SJames Hogan 				&args);
907d9cdc901SRalf Baechle }
908d9cdc901SRalf Baechle 
rm7k_erratum31(void)9091da177e4SLinus Torvalds static inline void rm7k_erratum31(void)
9101da177e4SLinus Torvalds {
9111da177e4SLinus Torvalds 	const unsigned long ic_lsize = 32;
9121da177e4SLinus Torvalds 	unsigned long addr;
9131da177e4SLinus Torvalds 
9141da177e4SLinus Torvalds 	/* RM7000 erratum #31. The icache is screwed at startup. */
9151da177e4SLinus Torvalds 	write_c0_taglo(0);
9161da177e4SLinus Torvalds 	write_c0_taghi(0);
9171da177e4SLinus Torvalds 
9181da177e4SLinus Torvalds 	for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
9191da177e4SLinus Torvalds 		__asm__ __volatile__ (
920d8748a3aSThiemo Seufer 			".set push\n\t"
9211da177e4SLinus Torvalds 			".set noreorder\n\t"
9221da177e4SLinus Torvalds 			".set mips3\n\t"
9231da177e4SLinus Torvalds 			"cache\t%1, 0(%0)\n\t"
9241da177e4SLinus Torvalds 			"cache\t%1, 0x1000(%0)\n\t"
9251da177e4SLinus Torvalds 			"cache\t%1, 0x2000(%0)\n\t"
9261da177e4SLinus Torvalds 			"cache\t%1, 0x3000(%0)\n\t"
9271da177e4SLinus Torvalds 			"cache\t%2, 0(%0)\n\t"
9281da177e4SLinus Torvalds 			"cache\t%2, 0x1000(%0)\n\t"
9291da177e4SLinus Torvalds 			"cache\t%2, 0x2000(%0)\n\t"
9301da177e4SLinus Torvalds 			"cache\t%2, 0x3000(%0)\n\t"
9311da177e4SLinus Torvalds 			"cache\t%1, 0(%0)\n\t"
9321da177e4SLinus Torvalds 			"cache\t%1, 0x1000(%0)\n\t"
9331da177e4SLinus Torvalds 			"cache\t%1, 0x2000(%0)\n\t"
9341da177e4SLinus Torvalds 			"cache\t%1, 0x3000(%0)\n\t"
935d8748a3aSThiemo Seufer 			".set pop\n"
9361da177e4SLinus Torvalds 			:
937a44f8309SHuacai Chen 			: "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill_I));
9381da177e4SLinus Torvalds 	}
9391da177e4SLinus Torvalds }
9401da177e4SLinus Torvalds 
alias_74k_erratum(struct cpuinfo_mips * c)941e2e7f29aSMaciej W. Rozycki static inline int alias_74k_erratum(struct cpuinfo_mips *c)
942006a851bSSteven J. Hill {
9439213ad77SMaciej W. Rozycki 	unsigned int imp = c->processor_id & PRID_IMP_MASK;
9449213ad77SMaciej W. Rozycki 	unsigned int rev = c->processor_id & PRID_REV_MASK;
945e2e7f29aSMaciej W. Rozycki 	int present = 0;
9469213ad77SMaciej W. Rozycki 
947006a851bSSteven J. Hill 	/*
948006a851bSSteven J. Hill 	 * Early versions of the 74K do not update the cache tags on a
949006a851bSSteven J. Hill 	 * vtag miss/ptag hit which can occur in the case of KSEG0/KUSEG
950006a851bSSteven J. Hill 	 * aliases.  In this case it is better to treat the cache as always
951e2e7f29aSMaciej W. Rozycki 	 * having aliases.  Also disable the synonym tag update feature
952e2e7f29aSMaciej W. Rozycki 	 * where available.  In this case no opportunistic tag update will
953e2e7f29aSMaciej W. Rozycki 	 * happen where a load causes a virtual address miss but a physical
954e2e7f29aSMaciej W. Rozycki 	 * address hit during a D-cache look-up.
955006a851bSSteven J. Hill 	 */
9569213ad77SMaciej W. Rozycki 	switch (imp) {
9579213ad77SMaciej W. Rozycki 	case PRID_IMP_74K:
9589213ad77SMaciej W. Rozycki 		if (rev <= PRID_REV_ENCODE_332(2, 4, 0))
959e2e7f29aSMaciej W. Rozycki 			present = 1;
9609213ad77SMaciej W. Rozycki 		if (rev == PRID_REV_ENCODE_332(2, 4, 0))
96104ef32afSHuacai Chen 			write_c0_config6(read_c0_config6() | MTI_CONF6_SYND);
9629213ad77SMaciej W. Rozycki 		break;
9639213ad77SMaciej W. Rozycki 	case PRID_IMP_1074K:
9649213ad77SMaciej W. Rozycki 		if (rev <= PRID_REV_ENCODE_332(1, 1, 0)) {
965e2e7f29aSMaciej W. Rozycki 			present = 1;
96604ef32afSHuacai Chen 			write_c0_config6(read_c0_config6() | MTI_CONF6_SYND);
967006a851bSSteven J. Hill 		}
9689213ad77SMaciej W. Rozycki 		break;
9699213ad77SMaciej W. Rozycki 	default:
9709213ad77SMaciej W. Rozycki 		BUG();
9719213ad77SMaciej W. Rozycki 	}
972e2e7f29aSMaciej W. Rozycki 
973e2e7f29aSMaciej W. Rozycki 	return present;
974006a851bSSteven J. Hill }
975006a851bSSteven J. Hill 
b5k_instruction_hazard(void)976d74b0172SKevin Cernekee static void b5k_instruction_hazard(void)
977d74b0172SKevin Cernekee {
978d74b0172SKevin Cernekee 	__sync();
979d74b0172SKevin Cernekee 	__sync();
980d74b0172SKevin Cernekee 	__asm__ __volatile__(
981d74b0172SKevin Cernekee 	"       nop; nop; nop; nop; nop; nop; nop; nop\n"
982d74b0172SKevin Cernekee 	"       nop; nop; nop; nop; nop; nop; nop; nop\n"
983d74b0172SKevin Cernekee 	"       nop; nop; nop; nop; nop; nop; nop; nop\n"
984d74b0172SKevin Cernekee 	"       nop; nop; nop; nop; nop; nop; nop; nop\n"
985d74b0172SKevin Cernekee 	: : : "memory");
986d74b0172SKevin Cernekee }
987d74b0172SKevin Cernekee 
988078a55fcSPaul Gortmaker static char *way_string[] = { NULL, "direct mapped", "2-way",
9891e18ac7aSPaul Burton 	"3-way", "4-way", "5-way", "6-way", "7-way", "8-way",
9901e18ac7aSPaul Burton 	"9-way", "10-way", "11-way", "12-way",
9911e18ac7aSPaul Burton 	"13-way", "14-way", "15-way", "16-way",
9921da177e4SLinus Torvalds };
9931da177e4SLinus Torvalds 
probe_pcache(void)994078a55fcSPaul Gortmaker static void probe_pcache(void)
9951da177e4SLinus Torvalds {
9961da177e4SLinus Torvalds 	struct cpuinfo_mips *c = &current_cpu_data;
9971da177e4SLinus Torvalds 	unsigned int config = read_c0_config();
9981da177e4SLinus Torvalds 	unsigned int prid = read_c0_prid();
999e2e7f29aSMaciej W. Rozycki 	int has_74k_erratum = 0;
10001da177e4SLinus Torvalds 	unsigned long config1;
10011da177e4SLinus Torvalds 	unsigned int lsize;
10021da177e4SLinus Torvalds 
100369f24d17SRalf Baechle 	switch (current_cpu_type()) {
10041da177e4SLinus Torvalds 	case CPU_R4600:			/* QED style two way caches? */
10051da177e4SLinus Torvalds 	case CPU_R4700:
10061da177e4SLinus Torvalds 	case CPU_R5000:
10071da177e4SLinus Torvalds 	case CPU_NEVADA:
10081da177e4SLinus Torvalds 		icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
10091da177e4SLinus Torvalds 		c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
10101da177e4SLinus Torvalds 		c->icache.ways = 2;
10113c68da79SAtsushi Nemoto 		c->icache.waybit = __ffs(icache_size/2);
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 		dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
10141da177e4SLinus Torvalds 		c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
10151da177e4SLinus Torvalds 		c->dcache.ways = 2;
10163c68da79SAtsushi Nemoto 		c->dcache.waybit= __ffs(dcache_size/2);
10171da177e4SLinus Torvalds 
10181da177e4SLinus Torvalds 		c->options |= MIPS_CPU_CACHE_CDEX_P;
10191da177e4SLinus Torvalds 		break;
10201da177e4SLinus Torvalds 
10211da177e4SLinus Torvalds 	case CPU_R5500:
10221da177e4SLinus Torvalds 		icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
10231da177e4SLinus Torvalds 		c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
10241da177e4SLinus Torvalds 		c->icache.ways = 2;
10251da177e4SLinus Torvalds 		c->icache.waybit= 0;
10261da177e4SLinus Torvalds 
10271da177e4SLinus Torvalds 		dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
10281da177e4SLinus Torvalds 		c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
10291da177e4SLinus Torvalds 		c->dcache.ways = 2;
10301da177e4SLinus Torvalds 		c->dcache.waybit = 0;
10311da177e4SLinus Torvalds 
10325864810bSShinya Kuribayashi 		c->options |= MIPS_CPU_CACHE_CDEX_P | MIPS_CPU_PREFETCH;
10331da177e4SLinus Torvalds 		break;
10341da177e4SLinus Torvalds 
10351da177e4SLinus Torvalds 	case CPU_TX49XX:
10361da177e4SLinus Torvalds 		icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
10371da177e4SLinus Torvalds 		c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
10381da177e4SLinus Torvalds 		c->icache.ways = 4;
10391da177e4SLinus Torvalds 		c->icache.waybit= 0;
10401da177e4SLinus Torvalds 
10411da177e4SLinus Torvalds 		dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
10421da177e4SLinus Torvalds 		c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
10431da177e4SLinus Torvalds 		c->dcache.ways = 4;
10441da177e4SLinus Torvalds 		c->dcache.waybit = 0;
10451da177e4SLinus Torvalds 
10461da177e4SLinus Torvalds 		c->options |= MIPS_CPU_CACHE_CDEX_P;
1047de862b48SAtsushi Nemoto 		c->options |= MIPS_CPU_PREFETCH;
10481da177e4SLinus Torvalds 		break;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	case CPU_R4000PC:
10511da177e4SLinus Torvalds 	case CPU_R4000SC:
10521da177e4SLinus Torvalds 	case CPU_R4000MC:
10531da177e4SLinus Torvalds 	case CPU_R4400PC:
10541da177e4SLinus Torvalds 	case CPU_R4400SC:
10551da177e4SLinus Torvalds 	case CPU_R4400MC:
105665ce6197SLauri Kasanen 	case CPU_R4300:
10571da177e4SLinus Torvalds 		icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
10581da177e4SLinus Torvalds 		c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
10591da177e4SLinus Torvalds 		c->icache.ways = 1;
10601da177e4SLinus Torvalds 		c->icache.waybit = 0;	/* doesn't matter */
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 		dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
10631da177e4SLinus Torvalds 		c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
10641da177e4SLinus Torvalds 		c->dcache.ways = 1;
10651da177e4SLinus Torvalds 		c->dcache.waybit = 0;	/* does not matter */
10661da177e4SLinus Torvalds 
10671da177e4SLinus Torvalds 		c->options |= MIPS_CPU_CACHE_CDEX_P;
10681da177e4SLinus Torvalds 		break;
10691da177e4SLinus Torvalds 
10701da177e4SLinus Torvalds 	case CPU_R10000:
10711da177e4SLinus Torvalds 	case CPU_R12000:
107244d921b2SKumba 	case CPU_R14000:
107330577391SJoshua Kinard 	case CPU_R16000:
10741da177e4SLinus Torvalds 		icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
10751da177e4SLinus Torvalds 		c->icache.linesz = 64;
10761da177e4SLinus Torvalds 		c->icache.ways = 2;
10771da177e4SLinus Torvalds 		c->icache.waybit = 0;
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 		dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
10801da177e4SLinus Torvalds 		c->dcache.linesz = 32;
10811da177e4SLinus Torvalds 		c->dcache.ways = 2;
10821da177e4SLinus Torvalds 		c->dcache.waybit = 0;
10831da177e4SLinus Torvalds 
10841da177e4SLinus Torvalds 		c->options |= MIPS_CPU_PREFETCH;
10851da177e4SLinus Torvalds 		break;
10861da177e4SLinus Torvalds 
10871da177e4SLinus Torvalds 	case CPU_RM7000:
10881da177e4SLinus Torvalds 		rm7k_erratum31();
10891da177e4SLinus Torvalds 
10901da177e4SLinus Torvalds 		icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
10911da177e4SLinus Torvalds 		c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
10921da177e4SLinus Torvalds 		c->icache.ways = 4;
10933c68da79SAtsushi Nemoto 		c->icache.waybit = __ffs(icache_size / c->icache.ways);
10941da177e4SLinus Torvalds 
10951da177e4SLinus Torvalds 		dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
10961da177e4SLinus Torvalds 		c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
10971da177e4SLinus Torvalds 		c->dcache.ways = 4;
10983c68da79SAtsushi Nemoto 		c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
10991da177e4SLinus Torvalds 
11001da177e4SLinus Torvalds 		c->options |= MIPS_CPU_CACHE_CDEX_P;
11011da177e4SLinus Torvalds 		c->options |= MIPS_CPU_PREFETCH;
11021da177e4SLinus Torvalds 		break;
11031da177e4SLinus Torvalds 
1104268a2d60SJiaxun Yang 	case CPU_LOONGSON2EF:
11052a21c730SFuxin Zhang 		icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
11062a21c730SFuxin Zhang 		c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
11072a21c730SFuxin Zhang 		if (prid & 0x3)
11082a21c730SFuxin Zhang 			c->icache.ways = 4;
11092a21c730SFuxin Zhang 		else
11102a21c730SFuxin Zhang 			c->icache.ways = 2;
11112a21c730SFuxin Zhang 		c->icache.waybit = 0;
11122a21c730SFuxin Zhang 
11132a21c730SFuxin Zhang 		dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
11142a21c730SFuxin Zhang 		c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
11152a21c730SFuxin Zhang 		if (prid & 0x3)
11162a21c730SFuxin Zhang 			c->dcache.ways = 4;
11172a21c730SFuxin Zhang 		else
11182a21c730SFuxin Zhang 			c->dcache.ways = 2;
11192a21c730SFuxin Zhang 		c->dcache.waybit = 0;
11202a21c730SFuxin Zhang 		break;
11212a21c730SFuxin Zhang 
1122268a2d60SJiaxun Yang 	case CPU_LOONGSON64:
1123c579d310SHuacai Chen 		config1 = read_c0_config1();
1124c579d310SHuacai Chen 		lsize = (config1 >> 19) & 7;
1125c579d310SHuacai Chen 		if (lsize)
1126c579d310SHuacai Chen 			c->icache.linesz = 2 << lsize;
1127c579d310SHuacai Chen 		else
1128c579d310SHuacai Chen 			c->icache.linesz = 0;
1129c579d310SHuacai Chen 		c->icache.sets = 64 << ((config1 >> 22) & 7);
1130c579d310SHuacai Chen 		c->icache.ways = 1 + ((config1 >> 16) & 7);
1131c579d310SHuacai Chen 		icache_size = c->icache.sets *
1132c579d310SHuacai Chen 					  c->icache.ways *
1133c579d310SHuacai Chen 					  c->icache.linesz;
1134c579d310SHuacai Chen 		c->icache.waybit = 0;
1135c579d310SHuacai Chen 
1136c579d310SHuacai Chen 		lsize = (config1 >> 10) & 7;
1137c579d310SHuacai Chen 		if (lsize)
1138c579d310SHuacai Chen 			c->dcache.linesz = 2 << lsize;
1139c579d310SHuacai Chen 		else
1140c579d310SHuacai Chen 			c->dcache.linesz = 0;
1141c579d310SHuacai Chen 		c->dcache.sets = 64 << ((config1 >> 13) & 7);
1142c579d310SHuacai Chen 		c->dcache.ways = 1 + ((config1 >> 7) & 7);
1143c579d310SHuacai Chen 		dcache_size = c->dcache.sets *
1144c579d310SHuacai Chen 					  c->dcache.ways *
1145c579d310SHuacai Chen 					  c->dcache.linesz;
1146c579d310SHuacai Chen 		c->dcache.waybit = 0;
11477507445bSHuacai Chen 		if ((c->processor_id & (PRID_IMP_MASK | PRID_REV_MASK)) >=
11480cf2ea11SJiaxun Yang 				(PRID_IMP_LOONGSON_64C | PRID_REV_LOONGSON3A_R2_0) ||
11490cf2ea11SJiaxun Yang 				(c->processor_id & PRID_IMP_MASK) == PRID_IMP_LOONGSON_64R)
11501e820da3SHuacai Chen 			c->options |= MIPS_CPU_PREFETCH;
1151c579d310SHuacai Chen 		break;
1152c579d310SHuacai Chen 
115318a8cd63SDavid Daney 	case CPU_CAVIUM_OCTEON3:
115418a8cd63SDavid Daney 		/* For now lie about the number of ways. */
115518a8cd63SDavid Daney 		c->icache.linesz = 128;
115618a8cd63SDavid Daney 		c->icache.sets = 16;
115718a8cd63SDavid Daney 		c->icache.ways = 8;
115818a8cd63SDavid Daney 		c->icache.flags |= MIPS_CACHE_VTAG;
115918a8cd63SDavid Daney 		icache_size = c->icache.sets * c->icache.ways * c->icache.linesz;
116018a8cd63SDavid Daney 
116118a8cd63SDavid Daney 		c->dcache.linesz = 128;
116218a8cd63SDavid Daney 		c->dcache.ways = 8;
116318a8cd63SDavid Daney 		c->dcache.sets = 8;
116418a8cd63SDavid Daney 		dcache_size = c->dcache.sets * c->dcache.ways * c->dcache.linesz;
116518a8cd63SDavid Daney 		c->options |= MIPS_CPU_PREFETCH;
116618a8cd63SDavid Daney 		break;
116718a8cd63SDavid Daney 
11681da177e4SLinus Torvalds 	default:
11691da177e4SLinus Torvalds 		if (!(config & MIPS_CONF_M))
11701da177e4SLinus Torvalds 			panic("Don't know how to probe P-caches on this cpu.");
11711da177e4SLinus Torvalds 
11721da177e4SLinus Torvalds 		/*
11731da177e4SLinus Torvalds 		 * So we seem to be a MIPS32 or MIPS64 CPU
11741da177e4SLinus Torvalds 		 * So let's probe the I-cache ...
11751da177e4SLinus Torvalds 		 */
11761da177e4SLinus Torvalds 		config1 = read_c0_config1();
11771da177e4SLinus Torvalds 
1178175cba8cSMarkos Chandras 		lsize = (config1 >> 19) & 7;
1179175cba8cSMarkos Chandras 
1180175cba8cSMarkos Chandras 		/* IL == 7 is reserved */
1181175cba8cSMarkos Chandras 		if (lsize == 7)
1182175cba8cSMarkos Chandras 			panic("Invalid icache line size");
1183175cba8cSMarkos Chandras 
1184175cba8cSMarkos Chandras 		c->icache.linesz = lsize ? 2 << lsize : 0;
1185175cba8cSMarkos Chandras 
1186dc34b05fSDouglas Leung 		c->icache.sets = 32 << (((config1 >> 22) + 1) & 7);
11871da177e4SLinus Torvalds 		c->icache.ways = 1 + ((config1 >> 16) & 7);
11881da177e4SLinus Torvalds 
11891da177e4SLinus Torvalds 		icache_size = c->icache.sets *
11901da177e4SLinus Torvalds 			      c->icache.ways *
11911da177e4SLinus Torvalds 			      c->icache.linesz;
11923c68da79SAtsushi Nemoto 		c->icache.waybit = __ffs(icache_size/c->icache.ways);
11931da177e4SLinus Torvalds 
11944b34bca0SJames Hogan 		if (config & MIPS_CONF_VI)
11951da177e4SLinus Torvalds 			c->icache.flags |= MIPS_CACHE_VTAG;
11961da177e4SLinus Torvalds 
11971da177e4SLinus Torvalds 		/*
11981da177e4SLinus Torvalds 		 * Now probe the MIPS32 / MIPS64 data cache.
11991da177e4SLinus Torvalds 		 */
12001da177e4SLinus Torvalds 		c->dcache.flags = 0;
12011da177e4SLinus Torvalds 
1202175cba8cSMarkos Chandras 		lsize = (config1 >> 10) & 7;
1203175cba8cSMarkos Chandras 
1204175cba8cSMarkos Chandras 		/* DL == 7 is reserved */
1205175cba8cSMarkos Chandras 		if (lsize == 7)
1206175cba8cSMarkos Chandras 			panic("Invalid dcache line size");
1207175cba8cSMarkos Chandras 
1208175cba8cSMarkos Chandras 		c->dcache.linesz = lsize ? 2 << lsize : 0;
1209175cba8cSMarkos Chandras 
1210dc34b05fSDouglas Leung 		c->dcache.sets = 32 << (((config1 >> 13) + 1) & 7);
12111da177e4SLinus Torvalds 		c->dcache.ways = 1 + ((config1 >> 7) & 7);
12121da177e4SLinus Torvalds 
12131da177e4SLinus Torvalds 		dcache_size = c->dcache.sets *
12141da177e4SLinus Torvalds 			      c->dcache.ways *
12151da177e4SLinus Torvalds 			      c->dcache.linesz;
12163c68da79SAtsushi Nemoto 		c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 		c->options |= MIPS_CPU_PREFETCH;
12191da177e4SLinus Torvalds 		break;
12201da177e4SLinus Torvalds 	}
12211da177e4SLinus Torvalds 
12221da177e4SLinus Torvalds 	/*
12231da177e4SLinus Torvalds 	 * Processor configuration sanity check for the R4000SC erratum
12241da177e4SLinus Torvalds 	 * #5.	With page sizes larger than 32kB there is no possibility
12251da177e4SLinus Torvalds 	 * to get a VCE exception anymore so we don't care about this
12261da177e4SLinus Torvalds 	 * misconfiguration.  The case is rather theoretical anyway;
12271da177e4SLinus Torvalds 	 * presumably no vendor is shipping his hardware in the "bad"
12281da177e4SLinus Torvalds 	 * configuration.
12291da177e4SLinus Torvalds 	 */
12308ff374b9SMaciej W. Rozycki 	if ((prid & PRID_IMP_MASK) == PRID_IMP_R4000 &&
12318ff374b9SMaciej W. Rozycki 	    (prid & PRID_REV_MASK) < PRID_REV_R4400 &&
12321da177e4SLinus Torvalds 	    !(config & CONF_SC) && c->icache.linesz != 16 &&
12331da177e4SLinus Torvalds 	    PAGE_SIZE <= 0x8000)
12341da177e4SLinus Torvalds 		panic("Improper R4000SC processor configuration detected");
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 	/* compute a couple of other cache variables */
12371da177e4SLinus Torvalds 	c->icache.waysize = icache_size / c->icache.ways;
12381da177e4SLinus Torvalds 	c->dcache.waysize = dcache_size / c->dcache.ways;
12391da177e4SLinus Torvalds 
124073f40352SChris Dearman 	c->icache.sets = c->icache.linesz ?
124173f40352SChris Dearman 		icache_size / (c->icache.linesz * c->icache.ways) : 0;
124273f40352SChris Dearman 	c->dcache.sets = c->dcache.linesz ?
124373f40352SChris Dearman 		dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	/*
124630577391SJoshua Kinard 	 * R1x000 P-caches are odd in a positive way.  They're 32kB 2-way
124730577391SJoshua Kinard 	 * virtually indexed so normally would suffer from aliases.  So
12481da177e4SLinus Torvalds 	 * normally they'd suffer from aliases but magic in the hardware deals
12491da177e4SLinus Torvalds 	 * with that for us so we don't need to take care ourselves.
12501da177e4SLinus Torvalds 	 */
125169f24d17SRalf Baechle 	switch (current_cpu_type()) {
1252a95970f3SRalf Baechle 	case CPU_20KC:
1253505403b6SRalf Baechle 	case CPU_25KF:
1254819da1eaSPaul Burton 	case CPU_I6400:
1255859aeb1bSPaul Burton 	case CPU_I6500:
1256641e97f3SRalf Baechle 	case CPU_SB1:
1257641e97f3SRalf Baechle 	case CPU_SB1A:
1258de62893bSAtsushi Nemoto 		c->dcache.flags |= MIPS_CACHE_PINDEX;
1259641e97f3SRalf Baechle 		break;
1260641e97f3SRalf Baechle 
1261d1e344e5SRalf Baechle 	case CPU_R10000:
1262d1e344e5SRalf Baechle 	case CPU_R12000:
126344d921b2SKumba 	case CPU_R14000:
126430577391SJoshua Kinard 	case CPU_R16000:
1265d1e344e5SRalf Baechle 		break;
1266641e97f3SRalf Baechle 
1267bf4aac07SMaciej W. Rozycki 	case CPU_74K:
1268bf4aac07SMaciej W. Rozycki 	case CPU_1074K:
1269e2e7f29aSMaciej W. Rozycki 		has_74k_erratum = alias_74k_erratum(c);
1270c9b02990SLiangliang Huang 		fallthrough;
1271113c62d9SSteven J. Hill 	case CPU_M14KC:
1272f8fa4811SSteven J. Hill 	case CPU_M14KEC:
1273d1e344e5SRalf Baechle 	case CPU_24K:
127498a41de9SNigel Stephens 	case CPU_34K:
127539b8d525SRalf Baechle 	case CPU_1004K:
127626ab96dfSLeonid Yegoshin 	case CPU_INTERAPTIV:
1277aced4cbdSJames Hogan 	case CPU_P5600:
1278708ac4b8SLeonid Yegoshin 	case CPU_PROAPTIV:
1279f36c4720SLeonid Yegoshin 	case CPU_M5150:
12804695089fSLeonid Yegoshin 	case CPU_QEMU_GENERIC:
12811091bfa2SPaul Burton 	case CPU_P6600:
12821dbf6a81SPaul Burton 	case CPU_M6250:
128302dc6bfbSMarkos Chandras 		if (!(read_c0_config7() & MIPS_CONF7_IAR) &&
128402dc6bfbSMarkos Chandras 		    (c->icache.waysize > PAGE_SIZE))
128502dc6bfbSMarkos Chandras 			c->icache.flags |= MIPS_CACHE_ALIASES;
1286e2e7f29aSMaciej W. Rozycki 		if (!has_74k_erratum && (read_c0_config7() & MIPS_CONF7_AR)) {
128702dc6bfbSMarkos Chandras 			/*
128802dc6bfbSMarkos Chandras 			 * Effectively physically indexed dcache,
128902dc6bfbSMarkos Chandras 			 * thus no virtual aliases.
129002dc6bfbSMarkos Chandras 			*/
1291beab375aSRalf Baechle 			c->dcache.flags |= MIPS_CACHE_PINDEX;
1292beab375aSRalf Baechle 			break;
1293beab375aSRalf Baechle 		}
1294c9b02990SLiangliang Huang 		fallthrough;
1295d1e344e5SRalf Baechle 	default:
1296e2e7f29aSMaciej W. Rozycki 		if (has_74k_erratum || c->dcache.waysize > PAGE_SIZE)
12971da177e4SLinus Torvalds 			c->dcache.flags |= MIPS_CACHE_ALIASES;
1298d1e344e5SRalf Baechle 	}
12991da177e4SLinus Torvalds 
1300d66f99bcSPaul Burton 	/* Physically indexed caches don't suffer from virtual aliasing */
1301d66f99bcSPaul Burton 	if (c->dcache.flags & MIPS_CACHE_PINDEX)
1302d66f99bcSPaul Burton 		c->dcache.flags &= ~MIPS_CACHE_ALIASES;
1303d66f99bcSPaul Burton 
1304d1c5872cSPaul Burton 	/*
1305d1c5872cSPaul Burton 	 * In systems with CM the icache fills from L2 or closer caches, and
1306d1c5872cSPaul Burton 	 * thus sees remote stores without needing to write them back any
1307d1c5872cSPaul Burton 	 * further than that.
1308d1c5872cSPaul Burton 	 */
1309d1c5872cSPaul Burton 	if (mips_cm_present())
1310d1c5872cSPaul Burton 		c->icache.flags |= MIPS_IC_SNOOPS_REMOTE;
1311d1c5872cSPaul Burton 
131269f24d17SRalf Baechle 	switch (current_cpu_type()) {
13131da177e4SLinus Torvalds 	case CPU_20KC:
13141da177e4SLinus Torvalds 		/*
13151da177e4SLinus Torvalds 		 * Some older 20Kc chips doesn't have the 'VI' bit in
13161da177e4SLinus Torvalds 		 * the config register.
13171da177e4SLinus Torvalds 		 */
13181da177e4SLinus Torvalds 		c->icache.flags |= MIPS_CACHE_VTAG;
13191da177e4SLinus Torvalds 		break;
13201da177e4SLinus Torvalds 
1321270717a8SManuel Lauss 	case CPU_ALCHEMY:
132247f2ac50SJames Hogan 	case CPU_I6400:
1323859aeb1bSPaul Burton 	case CPU_I6500:
13241da177e4SLinus Torvalds 		c->icache.flags |= MIPS_CACHE_IC_F_DC;
13251da177e4SLinus Torvalds 		break;
13261da177e4SLinus Torvalds 
1327c130d2fdSFlorian Fainelli 	case CPU_BMIPS5000:
1328c130d2fdSFlorian Fainelli 		c->icache.flags |= MIPS_CACHE_IC_F_DC;
132973c4ca04SFlorian Fainelli 		/* Cache aliases are handled in hardware; allow HIGHMEM */
133073c4ca04SFlorian Fainelli 		c->dcache.flags &= ~MIPS_CACHE_ALIASES;
1331c130d2fdSFlorian Fainelli 		break;
1332c130d2fdSFlorian Fainelli 
1333268a2d60SJiaxun Yang 	case CPU_LOONGSON2EF:
13342a21c730SFuxin Zhang 		/*
13352a21c730SFuxin Zhang 		 * LOONGSON2 has 4 way icache, but when using indexed cache op,
13362a21c730SFuxin Zhang 		 * one op will act on all 4 ways
13372a21c730SFuxin Zhang 		 */
13382a21c730SFuxin Zhang 		c->icache.ways = 1;
133914bd8c08SRalf Baechle 	}
13402a21c730SFuxin Zhang 
1341bea176fbSOleksij Rempel 	pr_info("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
13421da177e4SLinus Torvalds 		icache_size >> 10,
13437fc7316aSRalf Baechle 		c->icache.flags & MIPS_CACHE_VTAG ? "VIVT" : "VIPT",
13441da177e4SLinus Torvalds 		way_string[c->icache.ways], c->icache.linesz);
13451da177e4SLinus Torvalds 
1346bea176fbSOleksij Rempel 	pr_info("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
134764bfca5cSRalf Baechle 		dcache_size >> 10, way_string[c->dcache.ways],
134864bfca5cSRalf Baechle 		(c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
134964bfca5cSRalf Baechle 		(c->dcache.flags & MIPS_CACHE_ALIASES) ?
135064bfca5cSRalf Baechle 			"cache aliases" : "no aliases",
135164bfca5cSRalf Baechle 		c->dcache.linesz);
13521da177e4SLinus Torvalds }
13531da177e4SLinus Torvalds 
probe_vcache(void)1354b2edcfc8SHuacai Chen static void probe_vcache(void)
1355b2edcfc8SHuacai Chen {
1356b2edcfc8SHuacai Chen 	struct cpuinfo_mips *c = &current_cpu_data;
1357b2edcfc8SHuacai Chen 	unsigned int config2, lsize;
1358b2edcfc8SHuacai Chen 
1359268a2d60SJiaxun Yang 	if (current_cpu_type() != CPU_LOONGSON64)
1360b2edcfc8SHuacai Chen 		return;
1361b2edcfc8SHuacai Chen 
1362b2edcfc8SHuacai Chen 	config2 = read_c0_config2();
1363b2edcfc8SHuacai Chen 	if ((lsize = ((config2 >> 20) & 15)))
1364b2edcfc8SHuacai Chen 		c->vcache.linesz = 2 << lsize;
1365b2edcfc8SHuacai Chen 	else
1366b2edcfc8SHuacai Chen 		c->vcache.linesz = lsize;
1367b2edcfc8SHuacai Chen 
1368b2edcfc8SHuacai Chen 	c->vcache.sets = 64 << ((config2 >> 24) & 15);
1369b2edcfc8SHuacai Chen 	c->vcache.ways = 1 + ((config2 >> 16) & 15);
1370b2edcfc8SHuacai Chen 
1371b2edcfc8SHuacai Chen 	vcache_size = c->vcache.sets * c->vcache.ways * c->vcache.linesz;
1372b2edcfc8SHuacai Chen 
1373b2edcfc8SHuacai Chen 	c->vcache.waybit = 0;
13740be032c1SHuacai Chen 	c->vcache.waysize = vcache_size / c->vcache.ways;
1375b2edcfc8SHuacai Chen 
1376b2edcfc8SHuacai Chen 	pr_info("Unified victim cache %ldkB %s, linesize %d bytes.\n",
1377b2edcfc8SHuacai Chen 		vcache_size >> 10, way_string[c->vcache.ways], c->vcache.linesz);
1378b2edcfc8SHuacai Chen }
1379b2edcfc8SHuacai Chen 
13801da177e4SLinus Torvalds /*
13811da177e4SLinus Torvalds  * If you even _breathe_ on this function, look at the gcc output and make sure
13821da177e4SLinus Torvalds  * it does not pop things on and off the stack for the cache sizing loop that
13831da177e4SLinus Torvalds  * executes in KSEG1 space or else you will crash and burn badly.  You have
13841da177e4SLinus Torvalds  * been warned.
13851da177e4SLinus Torvalds  */
probe_scache(void)1386078a55fcSPaul Gortmaker static int probe_scache(void)
13871da177e4SLinus Torvalds {
13881da177e4SLinus Torvalds 	unsigned long flags, addr, begin, end, pow2;
13891da177e4SLinus Torvalds 	unsigned int config = read_c0_config();
13901da177e4SLinus Torvalds 	struct cpuinfo_mips *c = &current_cpu_data;
13911da177e4SLinus Torvalds 
13921da177e4SLinus Torvalds 	if (config & CONF_SC)
13931da177e4SLinus Torvalds 		return 0;
13941da177e4SLinus Torvalds 
1395e001e528SRalf Baechle 	begin = (unsigned long) &_stext;
13961da177e4SLinus Torvalds 	begin &= ~((4 * 1024 * 1024) - 1);
13971da177e4SLinus Torvalds 	end = begin + (4 * 1024 * 1024);
13981da177e4SLinus Torvalds 
13991da177e4SLinus Torvalds 	/*
14001da177e4SLinus Torvalds 	 * This is such a bitch, you'd think they would make it easy to do
14011da177e4SLinus Torvalds 	 * this.  Away you daemons of stupidity!
14021da177e4SLinus Torvalds 	 */
14031da177e4SLinus Torvalds 	local_irq_save(flags);
14041da177e4SLinus Torvalds 
14051da177e4SLinus Torvalds 	/* Fill each size-multiple cache line with a valid tag. */
14061da177e4SLinus Torvalds 	pow2 = (64 * 1024);
14071da177e4SLinus Torvalds 	for (addr = begin; addr < end; addr = (begin + pow2)) {
14081da177e4SLinus Torvalds 		unsigned long *p = (unsigned long *) addr;
14091da177e4SLinus Torvalds 		__asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
14101da177e4SLinus Torvalds 		pow2 <<= 1;
14111da177e4SLinus Torvalds 	}
14121da177e4SLinus Torvalds 
14131da177e4SLinus Torvalds 	/* Load first line with zero (therefore invalid) tag. */
14141da177e4SLinus Torvalds 	write_c0_taglo(0);
14151da177e4SLinus Torvalds 	write_c0_taghi(0);
14161da177e4SLinus Torvalds 	__asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
14171da177e4SLinus Torvalds 	cache_op(Index_Store_Tag_I, begin);
14181da177e4SLinus Torvalds 	cache_op(Index_Store_Tag_D, begin);
14191da177e4SLinus Torvalds 	cache_op(Index_Store_Tag_SD, begin);
14201da177e4SLinus Torvalds 
14211da177e4SLinus Torvalds 	/* Now search for the wrap around point. */
14221da177e4SLinus Torvalds 	pow2 = (128 * 1024);
14231da177e4SLinus Torvalds 	for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
14241da177e4SLinus Torvalds 		cache_op(Index_Load_Tag_SD, addr);
14251da177e4SLinus Torvalds 		__asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
14261da177e4SLinus Torvalds 		if (!read_c0_taglo())
14271da177e4SLinus Torvalds 			break;
14281da177e4SLinus Torvalds 		pow2 <<= 1;
14291da177e4SLinus Torvalds 	}
14301da177e4SLinus Torvalds 	local_irq_restore(flags);
14311da177e4SLinus Torvalds 	addr -= begin;
14321da177e4SLinus Torvalds 
14331da177e4SLinus Torvalds 	scache_size = addr;
14341da177e4SLinus Torvalds 	c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
14351da177e4SLinus Torvalds 	c->scache.ways = 1;
1436755af33bSJoshua Kinard 	c->scache.waybit = 0;		/* does not matter */
14371da177e4SLinus Torvalds 
14381da177e4SLinus Torvalds 	return 1;
14391da177e4SLinus Torvalds }
14401da177e4SLinus Torvalds 
loongson2_sc_init(void)1441c58734eeSNathan Chancellor static void loongson2_sc_init(void)
14422a21c730SFuxin Zhang {
14432a21c730SFuxin Zhang 	struct cpuinfo_mips *c = &current_cpu_data;
14442a21c730SFuxin Zhang 
14452a21c730SFuxin Zhang 	scache_size = 512*1024;
14462a21c730SFuxin Zhang 	c->scache.linesz = 32;
14472a21c730SFuxin Zhang 	c->scache.ways = 4;
14482a21c730SFuxin Zhang 	c->scache.waybit = 0;
14492a21c730SFuxin Zhang 	c->scache.waysize = scache_size / (c->scache.ways);
14502a21c730SFuxin Zhang 	c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
14512a21c730SFuxin Zhang 	pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
14522a21c730SFuxin Zhang 	       scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
14532a21c730SFuxin Zhang 
14542a21c730SFuxin Zhang 	c->options |= MIPS_CPU_INCLUSIVE_CACHES;
14552a21c730SFuxin Zhang }
14562a21c730SFuxin Zhang 
loongson3_sc_init(void)1457ad4fddefSAnders Roxell static void loongson3_sc_init(void)
1458c579d310SHuacai Chen {
1459c579d310SHuacai Chen 	struct cpuinfo_mips *c = &current_cpu_data;
1460c579d310SHuacai Chen 	unsigned int config2, lsize;
1461c579d310SHuacai Chen 
1462c579d310SHuacai Chen 	config2 = read_c0_config2();
1463c579d310SHuacai Chen 	lsize = (config2 >> 4) & 15;
1464c579d310SHuacai Chen 	if (lsize)
1465c579d310SHuacai Chen 		c->scache.linesz = 2 << lsize;
1466c579d310SHuacai Chen 	else
1467c579d310SHuacai Chen 		c->scache.linesz = 0;
1468c579d310SHuacai Chen 	c->scache.sets = 64 << ((config2 >> 8) & 15);
1469c579d310SHuacai Chen 	c->scache.ways = 1 + (config2 & 15);
1470c579d310SHuacai Chen 
14710cf2ea11SJiaxun Yang 	/* Loongson-3 has 4-Scache banks, while Loongson-2K have only 2 banks */
14720cf2ea11SJiaxun Yang 	if ((c->processor_id & PRID_IMP_MASK) == PRID_IMP_LOONGSON_64R)
147391c7a7e0STiezhu Yang 		c->scache.sets *= 2;
14740cf2ea11SJiaxun Yang 	else
147591c7a7e0STiezhu Yang 		c->scache.sets *= 4;
147691c7a7e0STiezhu Yang 
147791c7a7e0STiezhu Yang 	scache_size = c->scache.sets * c->scache.ways * c->scache.linesz;
14780cf2ea11SJiaxun Yang 
1479c579d310SHuacai Chen 	c->scache.waybit = 0;
14800be032c1SHuacai Chen 	c->scache.waysize = scache_size / c->scache.ways;
1481c579d310SHuacai Chen 	pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1482c579d310SHuacai Chen 	       scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1483c579d310SHuacai Chen 	if (scache_size)
1484c579d310SHuacai Chen 		c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1485c579d310SHuacai Chen 	return;
1486c579d310SHuacai Chen }
1487c579d310SHuacai Chen 
14881da177e4SLinus Torvalds extern int r5k_sc_init(void);
14891da177e4SLinus Torvalds extern int rm7k_sc_init(void);
14909318c51aSChris Dearman extern int mips_sc_init(void);
14911da177e4SLinus Torvalds 
setup_scache(void)1492078a55fcSPaul Gortmaker static void setup_scache(void)
14931da177e4SLinus Torvalds {
14941da177e4SLinus Torvalds 	struct cpuinfo_mips *c = &current_cpu_data;
14951da177e4SLinus Torvalds 	unsigned int config = read_c0_config();
14961da177e4SLinus Torvalds 	int sc_present = 0;
14971da177e4SLinus Torvalds 
14981da177e4SLinus Torvalds 	/*
14991da177e4SLinus Torvalds 	 * Do the probing thing on R4000SC and R4400SC processors.  Other
15001da177e4SLinus Torvalds 	 * processors don't have a S-cache that would be relevant to the
1501603e82edSJoe Perches 	 * Linux memory management.
15021da177e4SLinus Torvalds 	 */
150369f24d17SRalf Baechle 	switch (current_cpu_type()) {
15041da177e4SLinus Torvalds 	case CPU_R4000SC:
15051da177e4SLinus Torvalds 	case CPU_R4000MC:
15061da177e4SLinus Torvalds 	case CPU_R4400SC:
15071da177e4SLinus Torvalds 	case CPU_R4400MC:
1508ba5187dbSThiemo Seufer 		sc_present = run_uncached(probe_scache);
15091da177e4SLinus Torvalds 		if (sc_present)
15101da177e4SLinus Torvalds 			c->options |= MIPS_CPU_CACHE_CDEX_S;
15111da177e4SLinus Torvalds 		break;
15121da177e4SLinus Torvalds 
15131da177e4SLinus Torvalds 	case CPU_R10000:
15141da177e4SLinus Torvalds 	case CPU_R12000:
151544d921b2SKumba 	case CPU_R14000:
151630577391SJoshua Kinard 	case CPU_R16000:
15171da177e4SLinus Torvalds 		scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
15181da177e4SLinus Torvalds 		c->scache.linesz = 64 << ((config >> 13) & 1);
15191da177e4SLinus Torvalds 		c->scache.ways = 2;
15201da177e4SLinus Torvalds 		c->scache.waybit= 0;
15211da177e4SLinus Torvalds 		sc_present = 1;
15221da177e4SLinus Torvalds 		break;
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	case CPU_R5000:
15251da177e4SLinus Torvalds 	case CPU_NEVADA:
15261da177e4SLinus Torvalds #ifdef CONFIG_R5000_CPU_SCACHE
15271da177e4SLinus Torvalds 		r5k_sc_init();
15281da177e4SLinus Torvalds #endif
15291da177e4SLinus Torvalds 		return;
15301da177e4SLinus Torvalds 
15311da177e4SLinus Torvalds 	case CPU_RM7000:
15321da177e4SLinus Torvalds #ifdef CONFIG_RM7000_CPU_SCACHE
15331da177e4SLinus Torvalds 		rm7k_sc_init();
15341da177e4SLinus Torvalds #endif
15351da177e4SLinus Torvalds 		return;
15361da177e4SLinus Torvalds 
1537268a2d60SJiaxun Yang 	case CPU_LOONGSON2EF:
15382a21c730SFuxin Zhang 		loongson2_sc_init();
15392a21c730SFuxin Zhang 		return;
154014bd8c08SRalf Baechle 
1541268a2d60SJiaxun Yang 	case CPU_LOONGSON64:
1542c579d310SHuacai Chen 		loongson3_sc_init();
1543c579d310SHuacai Chen 		return;
1544c579d310SHuacai Chen 
154518a8cd63SDavid Daney 	case CPU_CAVIUM_OCTEON3:
1546a3d4fb2dSJayachandran C 		/* don't need to worry about L2, fully coherent */
1547a3d4fb2dSJayachandran C 		return;
15482a21c730SFuxin Zhang 
15491da177e4SLinus Torvalds 	default:
1550ab7c01fdSSerge Semin 		if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M64R1 |
1551ab7c01fdSSerge Semin 				    MIPS_CPU_ISA_M32R2 | MIPS_CPU_ISA_M64R2 |
1552ab7c01fdSSerge Semin 				    MIPS_CPU_ISA_M32R5 | MIPS_CPU_ISA_M64R5 |
1553ab7c01fdSSerge Semin 				    MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R6)) {
15549318c51aSChris Dearman #ifdef CONFIG_MIPS_CPU_SCACHE
15559318c51aSChris Dearman 			if (mips_sc_init ()) {
15569318c51aSChris Dearman 				scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
15579318c51aSChris Dearman 				printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
15589318c51aSChris Dearman 				       scache_size >> 10,
15599318c51aSChris Dearman 				       way_string[c->scache.ways], c->scache.linesz);
1560dbfc95f9SFlorian Fainelli 
1561dbfc95f9SFlorian Fainelli 				if (current_cpu_type() == CPU_BMIPS5000)
1562dbfc95f9SFlorian Fainelli 					c->options |= MIPS_CPU_INCLUSIVE_CACHES;
15639318c51aSChris Dearman 			}
1564dbfc95f9SFlorian Fainelli 
15659318c51aSChris Dearman #else
15669318c51aSChris Dearman 			if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
15679318c51aSChris Dearman 				panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
15689318c51aSChris Dearman #endif
15699318c51aSChris Dearman 			return;
15709318c51aSChris Dearman 		}
15711da177e4SLinus Torvalds 		sc_present = 0;
15721da177e4SLinus Torvalds 	}
15731da177e4SLinus Torvalds 
15741da177e4SLinus Torvalds 	if (!sc_present)
15751da177e4SLinus Torvalds 		return;
15761da177e4SLinus Torvalds 
15771da177e4SLinus Torvalds 	/* compute a couple of other cache variables */
15781da177e4SLinus Torvalds 	c->scache.waysize = scache_size / c->scache.ways;
15791da177e4SLinus Torvalds 
15801da177e4SLinus Torvalds 	c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
15811da177e4SLinus Torvalds 
15821da177e4SLinus Torvalds 	printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
15831da177e4SLinus Torvalds 	       scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
15841da177e4SLinus Torvalds 
1585fc5d2d27SRalf Baechle 	c->options |= MIPS_CPU_INCLUSIVE_CACHES;
15861da177e4SLinus Torvalds }
15871da177e4SLinus Torvalds 
au1x00_fixup_config_od(void)15889370b351SSergei Shtylyov void au1x00_fixup_config_od(void)
15899370b351SSergei Shtylyov {
15909370b351SSergei Shtylyov 	/*
15919370b351SSergei Shtylyov 	 * c0_config.od (bit 19) was write only (and read as 0)
15929370b351SSergei Shtylyov 	 * on the early revisions of Alchemy SOCs.  It disables the bus
15939370b351SSergei Shtylyov 	 * transaction overlapping and needs to be set to fix various errata.
15949370b351SSergei Shtylyov 	 */
15959370b351SSergei Shtylyov 	switch (read_c0_prid()) {
15969370b351SSergei Shtylyov 	case 0x00030100: /* Au1000 DA */
15979370b351SSergei Shtylyov 	case 0x00030201: /* Au1000 HA */
15989370b351SSergei Shtylyov 	case 0x00030202: /* Au1000 HB */
15999370b351SSergei Shtylyov 	case 0x01030200: /* Au1500 AB */
16009370b351SSergei Shtylyov 	/*
16019370b351SSergei Shtylyov 	 * Au1100 errata actually keeps silence about this bit, so we set it
16029370b351SSergei Shtylyov 	 * just in case for those revisions that require it to be set according
1603270717a8SManuel Lauss 	 * to the (now gone) cpu table.
16049370b351SSergei Shtylyov 	 */
16059370b351SSergei Shtylyov 	case 0x02030200: /* Au1100 AB */
16069370b351SSergei Shtylyov 	case 0x02030201: /* Au1100 BA */
16079370b351SSergei Shtylyov 	case 0x02030202: /* Au1100 BC */
16089370b351SSergei Shtylyov 		set_c0_config(1 << 19);
16099370b351SSergei Shtylyov 		break;
16109370b351SSergei Shtylyov 	}
16119370b351SSergei Shtylyov }
16129370b351SSergei Shtylyov 
161389052bd7SRalf Baechle /* CP0 hazard avoidance. */
161489052bd7SRalf Baechle #define NXP_BARRIER()							\
161589052bd7SRalf Baechle 	 __asm__ __volatile__(						\
161689052bd7SRalf Baechle 	".set noreorder\n\t"						\
161789052bd7SRalf Baechle 	"nop; nop; nop; nop; nop; nop;\n\t"				\
161889052bd7SRalf Baechle 	".set reorder\n\t")
161989052bd7SRalf Baechle 
nxp_pr4450_fixup_config(void)162089052bd7SRalf Baechle static void nxp_pr4450_fixup_config(void)
162189052bd7SRalf Baechle {
162289052bd7SRalf Baechle 	unsigned long config0;
162389052bd7SRalf Baechle 
162489052bd7SRalf Baechle 	config0 = read_c0_config();
162589052bd7SRalf Baechle 
162689052bd7SRalf Baechle 	/* clear all three cache coherency fields */
162789052bd7SRalf Baechle 	config0 &= ~(0x7 | (7 << 25) | (7 << 28));
162889052bd7SRalf Baechle 	config0 |= (((_page_cachable_default >> _CACHE_SHIFT) <<  0) |
162989052bd7SRalf Baechle 		    ((_page_cachable_default >> _CACHE_SHIFT) << 25) |
163089052bd7SRalf Baechle 		    ((_page_cachable_default >> _CACHE_SHIFT) << 28));
163189052bd7SRalf Baechle 	write_c0_config(config0);
163289052bd7SRalf Baechle 	NXP_BARRIER();
163389052bd7SRalf Baechle }
163489052bd7SRalf Baechle 
1635078a55fcSPaul Gortmaker static int cca = -1;
163635133692SChris Dearman 
cca_setup(char * str)163735133692SChris Dearman static int __init cca_setup(char *str)
163835133692SChris Dearman {
163935133692SChris Dearman 	get_option(&str, &cca);
164035133692SChris Dearman 
1641b5b64f2bSShane McDonald 	return 0;
164235133692SChris Dearman }
164335133692SChris Dearman 
1644b5b64f2bSShane McDonald early_param("cca", cca_setup);
164535133692SChris Dearman 
coherency_setup(void)1646078a55fcSPaul Gortmaker static void coherency_setup(void)
16471da177e4SLinus Torvalds {
164835133692SChris Dearman 	if (cca < 0 || cca > 7)
164935133692SChris Dearman 		cca = read_c0_config() & CONF_CM_CMASK;
165035133692SChris Dearman 	_page_cachable_default = cca << _CACHE_SHIFT;
165135133692SChris Dearman 
165235133692SChris Dearman 	pr_debug("Using cache attribute %d\n", cca);
165335133692SChris Dearman 	change_c0_config(CONF_CM_CMASK, cca);
16541da177e4SLinus Torvalds 
16551da177e4SLinus Torvalds 	/*
16561da177e4SLinus Torvalds 	 * c0_status.cu=0 specifies that updates by the sc instruction use
16571da177e4SLinus Torvalds 	 * the coherency mode specified by the TLB; 1 means cachable
16581da177e4SLinus Torvalds 	 * coherent update on write will be used.  Not all processors have
16591da177e4SLinus Torvalds 	 * this bit and; some wire it to zero, others like Toshiba had the
16601da177e4SLinus Torvalds 	 * silly idea of putting something else there ...
16611da177e4SLinus Torvalds 	 */
166210cc3529SRalf Baechle 	switch (current_cpu_type()) {
16631da177e4SLinus Torvalds 	case CPU_R4000PC:
16641da177e4SLinus Torvalds 	case CPU_R4000SC:
16651da177e4SLinus Torvalds 	case CPU_R4000MC:
16661da177e4SLinus Torvalds 	case CPU_R4400PC:
16671da177e4SLinus Torvalds 	case CPU_R4400SC:
16681da177e4SLinus Torvalds 	case CPU_R4400MC:
16691da177e4SLinus Torvalds 		clear_c0_config(CONF_CU);
16701da177e4SLinus Torvalds 		break;
16719370b351SSergei Shtylyov 	/*
1672df586d59SRalf Baechle 	 * We need to catch the early Alchemy SOCs with
1673270717a8SManuel Lauss 	 * the write-only co_config.od bit and set it back to one on:
1674270717a8SManuel Lauss 	 * Au1000 rev DA, HA, HB;  Au1100 AB, BA, BC, Au1500 AB
16759370b351SSergei Shtylyov 	 */
1676270717a8SManuel Lauss 	case CPU_ALCHEMY:
16779370b351SSergei Shtylyov 		au1x00_fixup_config_od();
16789370b351SSergei Shtylyov 		break;
167989052bd7SRalf Baechle 
168089052bd7SRalf Baechle 	case PRID_IMP_PR4450:
168189052bd7SRalf Baechle 		nxp_pr4450_fixup_config();
168289052bd7SRalf Baechle 		break;
16831da177e4SLinus Torvalds 	}
16841da177e4SLinus Torvalds }
16851da177e4SLinus Torvalds 
r4k_cache_error_setup(void)1686078a55fcSPaul Gortmaker static void r4k_cache_error_setup(void)
16871da177e4SLinus Torvalds {
1688641e97f3SRalf Baechle 	extern char __weak except_vec2_generic;
1689641e97f3SRalf Baechle 	extern char __weak except_vec2_sb1;
16901da177e4SLinus Torvalds 
169169f24d17SRalf Baechle 	switch (current_cpu_type()) {
1692641e97f3SRalf Baechle 	case CPU_SB1:
1693641e97f3SRalf Baechle 	case CPU_SB1A:
1694641e97f3SRalf Baechle 		set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1695641e97f3SRalf Baechle 		break;
1696641e97f3SRalf Baechle 
1697641e97f3SRalf Baechle 	default:
1698e01402b1SRalf Baechle 		set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1699641e97f3SRalf Baechle 		break;
1700641e97f3SRalf Baechle 	}
17019cd9669bSDavid Daney }
17029cd9669bSDavid Daney 
r4k_cache_init(void)1703078a55fcSPaul Gortmaker void r4k_cache_init(void)
17049cd9669bSDavid Daney {
17059cd9669bSDavid Daney 	extern void build_clear_page(void);
17069cd9669bSDavid Daney 	extern void build_copy_page(void);
17079cd9669bSDavid Daney 	struct cpuinfo_mips *c = &current_cpu_data;
17081da177e4SLinus Torvalds 
17091da177e4SLinus Torvalds 	probe_pcache();
1710b2edcfc8SHuacai Chen 	probe_vcache();
17111da177e4SLinus Torvalds 	setup_scache();
17121da177e4SLinus Torvalds 
17131da177e4SLinus Torvalds 	r4k_blast_dcache_page_setup();
17141da177e4SLinus Torvalds 	r4k_blast_dcache_setup();
17151da177e4SLinus Torvalds 	r4k_blast_icache_page_setup();
17161da177e4SLinus Torvalds 	r4k_blast_icache_setup();
17171da177e4SLinus Torvalds 	r4k_blast_scache_page_setup();
17181da177e4SLinus Torvalds 	r4k_blast_scache_setup();
1719bb53fdf3SHuacai Chen 	r4k_blast_scache_node_setup();
17204caa906eSLeonid Yegoshin #ifdef CONFIG_EVA
17214caa906eSLeonid Yegoshin 	r4k_blast_dcache_user_page_setup();
17224caa906eSLeonid Yegoshin 	r4k_blast_icache_user_page_setup();
17234caa906eSLeonid Yegoshin #endif
17241da177e4SLinus Torvalds 
17251da177e4SLinus Torvalds 	/*
17261da177e4SLinus Torvalds 	 * Some MIPS32 and MIPS64 processors have physically indexed caches.
17271da177e4SLinus Torvalds 	 * This code supports virtually indexed processors and will be
17281da177e4SLinus Torvalds 	 * unnecessarily inefficient on physically indexed processors.
17291da177e4SLinus Torvalds 	 */
1730cb80b2a3SLeonid Yegoshin 	if (c->dcache.linesz && cpu_has_dc_aliases)
17311da177e4SLinus Torvalds 		shm_align_mask = max_t( unsigned long,
17321da177e4SLinus Torvalds 					c->dcache.sets * c->dcache.linesz - 1,
17331da177e4SLinus Torvalds 					PAGE_SIZE - 1);
173473f40352SChris Dearman 	else
173573f40352SChris Dearman 		shm_align_mask = PAGE_SIZE-1;
17369c5a3d72SRalf Baechle 
17379c5a3d72SRalf Baechle 	__flush_cache_vmap	= r4k__flush_cache_vmap;
17389c5a3d72SRalf Baechle 	__flush_cache_vunmap	= r4k__flush_cache_vunmap;
17399c5a3d72SRalf Baechle 
1740db813fe5SRalf Baechle 	flush_cache_all		= cache_noop;
17411da177e4SLinus Torvalds 	__flush_cache_all	= r4k___flush_cache_all;
17421da177e4SLinus Torvalds 	flush_cache_mm		= r4k_flush_cache_mm;
17431da177e4SLinus Torvalds 	flush_cache_page	= r4k_flush_cache_page;
17441da177e4SLinus Torvalds 	flush_cache_range	= r4k_flush_cache_range;
17451da177e4SLinus Torvalds 
1746d9cdc901SRalf Baechle 	__flush_kernel_vmap_range = r4k_flush_kernel_vmap_range;
1747d9cdc901SRalf Baechle 
17481da177e4SLinus Torvalds 	flush_icache_all	= r4k_flush_icache_all;
17491da177e4SLinus Torvalds 	flush_data_cache_page	= r4k_flush_data_cache_page;
17501da177e4SLinus Torvalds 	flush_icache_range	= r4k_flush_icache_range;
1751e0cee3eeSThomas Bogendoerfer 	local_flush_icache_range	= local_r4k_flush_icache_range;
1752b2ff7171SJames Hogan 	__flush_icache_user_range	= r4k_flush_icache_user_range;
1753b2ff7171SJames Hogan 	__local_flush_icache_user_range	= local_r4k_flush_icache_user_range;
17541da177e4SLinus Torvalds 
17555748e1b3SChristoph Hellwig #ifdef CONFIG_DMA_NONCOHERENT
17561da177e4SLinus Torvalds 	_dma_cache_wback_inv	= r4k_dma_cache_wback_inv;
17571da177e4SLinus Torvalds 	_dma_cache_wback	= r4k_dma_cache_wback_inv;
17581da177e4SLinus Torvalds 	_dma_cache_inv		= r4k_dma_cache_inv;
17595748e1b3SChristoph Hellwig #endif /* CONFIG_DMA_NONCOHERENT */
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds 	build_clear_page();
17621da177e4SLinus Torvalds 	build_copy_page();
1763b6d92b4aSSteven J. Hill 
1764b6d92b4aSSteven J. Hill 	/*
1765b6d92b4aSSteven J. Hill 	 * We want to run CMP kernels on core with and without coherent
1766b6d92b4aSSteven J. Hill 	 * caches. Therefore, do not use CONFIG_MIPS_CMP to decide whether
1767b6d92b4aSSteven J. Hill 	 * or not to flush caches.
1768b6d92b4aSSteven J. Hill 	 */
17691d40cfcdSRalf Baechle 	local_r4k___flush_cache_all(NULL);
1770b6d92b4aSSteven J. Hill 
17711d40cfcdSRalf Baechle 	coherency_setup();
17729cd9669bSDavid Daney 	board_cache_error_setup = r4k_cache_error_setup;
1773d74b0172SKevin Cernekee 
1774d74b0172SKevin Cernekee 	/*
1775d74b0172SKevin Cernekee 	 * Per-CPU overrides
1776d74b0172SKevin Cernekee 	 */
1777d74b0172SKevin Cernekee 	switch (current_cpu_type()) {
1778d74b0172SKevin Cernekee 	case CPU_BMIPS4350:
1779d74b0172SKevin Cernekee 	case CPU_BMIPS4380:
1780d74b0172SKevin Cernekee 		/* No IPI is needed because all CPUs share the same D$ */
1781d74b0172SKevin Cernekee 		flush_data_cache_page = r4k_blast_dcache_page;
1782d74b0172SKevin Cernekee 		break;
1783d74b0172SKevin Cernekee 	case CPU_BMIPS5000:
1784d74b0172SKevin Cernekee 		/* We lose our superpowers if L2 is disabled */
1785d74b0172SKevin Cernekee 		if (c->scache.flags & MIPS_CACHE_NOT_PRESENT)
1786d74b0172SKevin Cernekee 			break;
1787d74b0172SKevin Cernekee 
1788d74b0172SKevin Cernekee 		/* I$ fills from D$ just by emptying the write buffers */
1789d74b0172SKevin Cernekee 		flush_cache_page = (void *)b5k_instruction_hazard;
1790d74b0172SKevin Cernekee 		flush_cache_range = (void *)b5k_instruction_hazard;
1791d74b0172SKevin Cernekee 		flush_data_cache_page = (void *)b5k_instruction_hazard;
1792d74b0172SKevin Cernekee 		flush_icache_range = (void *)b5k_instruction_hazard;
1793d74b0172SKevin Cernekee 		local_flush_icache_range = (void *)b5k_instruction_hazard;
1794d74b0172SKevin Cernekee 
1795d74b0172SKevin Cernekee 
1796d74b0172SKevin Cernekee 		/* Optimization: an L2 flush implicitly flushes the L1 */
1797d74b0172SKevin Cernekee 		current_cpu_data.options |= MIPS_CPU_INCLUSIVE_CACHES;
1798d74b0172SKevin Cernekee 		break;
1799268a2d60SJiaxun Yang 	case CPU_LOONGSON64:
180037fbe8faSHuacai Chen 		/* Loongson-3 maintains cache coherency by hardware */
180137fbe8faSHuacai Chen 		__flush_cache_all	= cache_noop;
180237fbe8faSHuacai Chen 		__flush_cache_vmap	= cache_noop;
180337fbe8faSHuacai Chen 		__flush_cache_vunmap	= cache_noop;
180437fbe8faSHuacai Chen 		__flush_kernel_vmap_range = (void *)cache_noop;
180537fbe8faSHuacai Chen 		flush_cache_mm		= (void *)cache_noop;
180637fbe8faSHuacai Chen 		flush_cache_page	= (void *)cache_noop;
180737fbe8faSHuacai Chen 		flush_cache_range	= (void *)cache_noop;
180837fbe8faSHuacai Chen 		flush_icache_all	= (void *)cache_noop;
180937fbe8faSHuacai Chen 		flush_data_cache_page	= (void *)cache_noop;
181037fbe8faSHuacai Chen 		break;
1811d74b0172SKevin Cernekee 	}
18121da177e4SLinus Torvalds }
181361d73044SJames Hogan 
r4k_cache_pm_notifier(struct notifier_block * self,unsigned long cmd,void * v)181461d73044SJames Hogan static int r4k_cache_pm_notifier(struct notifier_block *self, unsigned long cmd,
181561d73044SJames Hogan 			       void *v)
181661d73044SJames Hogan {
181761d73044SJames Hogan 	switch (cmd) {
181861d73044SJames Hogan 	case CPU_PM_ENTER_FAILED:
181961d73044SJames Hogan 	case CPU_PM_EXIT:
182061d73044SJames Hogan 		coherency_setup();
182161d73044SJames Hogan 		break;
182261d73044SJames Hogan 	}
182361d73044SJames Hogan 
182461d73044SJames Hogan 	return NOTIFY_OK;
182561d73044SJames Hogan }
182661d73044SJames Hogan 
182761d73044SJames Hogan static struct notifier_block r4k_cache_pm_notifier_block = {
182861d73044SJames Hogan 	.notifier_call = r4k_cache_pm_notifier,
182961d73044SJames Hogan };
183061d73044SJames Hogan 
r4k_cache_init_pm(void)183161d73044SJames Hogan int __init r4k_cache_init_pm(void)
183261d73044SJames Hogan {
183361d73044SJames Hogan 	return cpu_pm_register_notifier(&r4k_cache_pm_notifier_block);
183461d73044SJames Hogan }
183561d73044SJames Hogan arch_initcall(r4k_cache_init_pm);
1836