blob: ef7f925dd1b0285ee743587962150f1592ac22af [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
Justin P. Mattock79add622011-04-04 14:15:29 -07006 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Ralf Baechle (ralf@gnu.org)
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
James Hogan61d73042014-03-04 10:23:57 +000010#include <linux/cpu_pm.h>
Ralf Baechlea754f702007-11-03 01:01:37 +000011#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Ralf Baechledb813fe2007-09-27 18:26:43 +010013#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/kernel.h>
Ralf Baechle641e97f2007-10-11 23:46:05 +010015#include <linux/linkage.h>
Ralf Baechleff522052013-09-17 12:44:31 +020016#include <linux/preempt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/sched.h>
Ralf Baechle631330f2009-06-19 14:05:26 +010018#include <linux/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mm.h>
Chris Dearman35133692007-09-19 00:58:24 +010020#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/bitops.h>
22
23#include <asm/bcache.h>
24#include <asm/bootinfo.h>
Ralf Baechleec74e362005-07-13 11:48:45 +000025#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/cacheops.h>
27#include <asm/cpu.h>
28#include <asm/cpu-features.h>
Ralf Baechle69f24d12013-09-17 10:25:47 +020029#include <asm/cpu-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
31#include <asm/page.h>
32#include <asm/pgtable.h>
33#include <asm/r4kcache.h>
Ralf Baechlee001e522007-07-28 12:45:47 +010034#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <asm/mmu_context.h>
36#include <asm/war.h>
Thiemo Seuferba5187d2005-04-25 16:36:23 +000037#include <asm/cacheflush.h> /* for run_uncached() */
David Daney9cd9669b2012-05-15 00:04:49 -070038#include <asm/traps.h>
Steven J. Hillb6d92b42013-03-25 13:47:29 -050039#include <asm/dma-coherence.h>
Markos Chandrascccf34e2015-07-10 09:29:10 +010040#include <asm/mips-cm.h>
Ralf Baechle7f3f1d02006-05-12 13:20:06 +010041
42/*
43 * Special Variant of smp_call_function for use by cache functions:
44 *
45 * o No return value
46 * o collapses to normal function call on UP kernels
47 * o collapses to normal function call on systems with a single shared
48 * primary cache.
Ralf Baechlec8c5f3f2010-10-29 19:08:25 +010049 * o doesn't disable interrupts on the local CPU
Ralf Baechle7f3f1d02006-05-12 13:20:06 +010050 */
Ralf Baechle48a26e62010-10-29 19:08:25 +010051static inline void r4k_on_each_cpu(void (*func) (void *info), void *info)
Ralf Baechle7f3f1d02006-05-12 13:20:06 +010052{
53 preempt_disable();
54
Markos Chandrascccf34e2015-07-10 09:29:10 +010055 /*
56 * The Coherent Manager propagates address-based cache ops to other
57 * cores but not index-based ops. However, r4k_on_each_cpu is used
58 * in both cases so there is no easy way to tell what kind of op is
59 * executed to the other cores. The best we can probably do is
60 * to restrict that call when a CM is not present because both
61 * CM-based SMP protocols (CMP & CPS) restrict index-based cache ops.
62 */
63 if (!mips_cm_present())
64 smp_call_function_many(&cpu_foreign_map, func, info, 1);
Ralf Baechle7f3f1d02006-05-12 13:20:06 +010065 func(info);
66 preempt_enable();
67}
68
Paul Burton0ee958e2014-01-15 10:31:53 +000069#if defined(CONFIG_MIPS_CMP) || defined(CONFIG_MIPS_CPS)
Ralf Baechle39b8d522008-04-28 17:14:26 +010070#define cpu_has_safe_index_cacheops 0
71#else
72#define cpu_has_safe_index_cacheops 1
73#endif
74
Ralf Baechleec74e362005-07-13 11:48:45 +000075/*
76 * Must die.
77 */
78static unsigned long icache_size __read_mostly;
79static unsigned long dcache_size __read_mostly;
Huacai Chenb2edcfc2016-03-03 09:45:09 +080080static unsigned long vcache_size __read_mostly;
Ralf Baechleec74e362005-07-13 11:48:45 +000081static unsigned long scache_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83/*
84 * Dummy cache handling routines for machines without boardcaches
85 */
Chris Dearman73f40352006-06-20 18:06:52 +010086static void cache_noop(void) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88static struct bcache_ops no_sc_ops = {
Chris Dearman73f40352006-06-20 18:06:52 +010089 .bc_enable = (void *)cache_noop,
90 .bc_disable = (void *)cache_noop,
91 .bc_wback_inv = (void *)cache_noop,
92 .bc_inv = (void *)cache_noop
Linus Torvalds1da177e2005-04-16 15:20:36 -070093};
94
95struct bcache_ops *bcops = &no_sc_ops;
96
Thiemo Seufer330cfe02005-09-01 18:33:58 +000097#define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
98#define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100#define R4600_HIT_CACHEOP_WAR_IMPL \
101do { \
102 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
103 *(volatile unsigned long *)CKSEG1; \
104 if (R4600_V1_HIT_CACHEOP_WAR) \
105 __asm__ __volatile__("nop;nop;nop;nop"); \
106} while (0)
107
108static void (*r4k_blast_dcache_page)(unsigned long addr);
109
110static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
111{
112 R4600_HIT_CACHEOP_WAR_IMPL;
113 blast_dcache32_page(addr);
114}
115
Kevin Cernekee605b7ef2009-04-23 17:36:53 -0700116static inline void r4k_blast_dcache_page_dc64(unsigned long addr)
117{
Kevin Cernekee605b7ef2009-04-23 17:36:53 -0700118 blast_dcache64_page(addr);
119}
120
David Daney18a8cd62014-05-28 23:52:09 +0200121static inline void r4k_blast_dcache_page_dc128(unsigned long addr)
122{
123 blast_dcache128_page(addr);
124}
125
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000126static void r4k_blast_dcache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127{
128 unsigned long dc_lsize = cpu_dcache_line_size();
129
David Daney18a8cd62014-05-28 23:52:09 +0200130 switch (dc_lsize) {
131 case 0:
Chris Dearman73f40352006-06-20 18:06:52 +0100132 r4k_blast_dcache_page = (void *)cache_noop;
David Daney18a8cd62014-05-28 23:52:09 +0200133 break;
134 case 16:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 r4k_blast_dcache_page = blast_dcache16_page;
David Daney18a8cd62014-05-28 23:52:09 +0200136 break;
137 case 32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
David Daney18a8cd62014-05-28 23:52:09 +0200139 break;
140 case 64:
Kevin Cernekee605b7ef2009-04-23 17:36:53 -0700141 r4k_blast_dcache_page = r4k_blast_dcache_page_dc64;
David Daney18a8cd62014-05-28 23:52:09 +0200142 break;
143 case 128:
144 r4k_blast_dcache_page = r4k_blast_dcache_page_dc128;
145 break;
146 default:
147 break;
148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Leonid Yegoshin4caa9062014-01-15 14:47:28 +0000151#ifndef CONFIG_EVA
152#define r4k_blast_dcache_user_page r4k_blast_dcache_page
153#else
154
155static void (*r4k_blast_dcache_user_page)(unsigned long addr);
156
157static void r4k_blast_dcache_user_page_setup(void)
158{
159 unsigned long dc_lsize = cpu_dcache_line_size();
160
161 if (dc_lsize == 0)
162 r4k_blast_dcache_user_page = (void *)cache_noop;
163 else if (dc_lsize == 16)
164 r4k_blast_dcache_user_page = blast_dcache16_user_page;
165 else if (dc_lsize == 32)
166 r4k_blast_dcache_user_page = blast_dcache32_user_page;
167 else if (dc_lsize == 64)
168 r4k_blast_dcache_user_page = blast_dcache64_user_page;
169}
170
171#endif
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
174
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000175static void r4k_blast_dcache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 unsigned long dc_lsize = cpu_dcache_line_size();
178
Chris Dearman73f40352006-06-20 18:06:52 +0100179 if (dc_lsize == 0)
180 r4k_blast_dcache_page_indexed = (void *)cache_noop;
181 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
183 else if (dc_lsize == 32)
184 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
Kevin Cernekee605b7ef2009-04-23 17:36:53 -0700185 else if (dc_lsize == 64)
186 r4k_blast_dcache_page_indexed = blast_dcache64_page_indexed;
David Daney18a8cd62014-05-28 23:52:09 +0200187 else if (dc_lsize == 128)
188 r4k_blast_dcache_page_indexed = blast_dcache128_page_indexed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Sanjay Lalf2e36562012-11-21 18:34:10 -0800191void (* r4k_blast_dcache)(void);
192EXPORT_SYMBOL(r4k_blast_dcache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000194static void r4k_blast_dcache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 unsigned long dc_lsize = cpu_dcache_line_size();
197
Chris Dearman73f40352006-06-20 18:06:52 +0100198 if (dc_lsize == 0)
199 r4k_blast_dcache = (void *)cache_noop;
200 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 r4k_blast_dcache = blast_dcache16;
202 else if (dc_lsize == 32)
203 r4k_blast_dcache = blast_dcache32;
Kevin Cernekee605b7ef2009-04-23 17:36:53 -0700204 else if (dc_lsize == 64)
205 r4k_blast_dcache = blast_dcache64;
David Daney18a8cd62014-05-28 23:52:09 +0200206 else if (dc_lsize == 128)
207 r4k_blast_dcache = blast_dcache128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208}
209
210/* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
211#define JUMP_TO_ALIGN(order) \
212 __asm__ __volatile__( \
213 "b\t1f\n\t" \
214 ".align\t" #order "\n\t" \
215 "1:\n\t" \
216 )
217#define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
Ralf Baechle70342282013-01-22 12:59:30 +0100218#define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220static inline void blast_r4600_v1_icache32(void)
221{
222 unsigned long flags;
223
224 local_irq_save(flags);
225 blast_icache32();
226 local_irq_restore(flags);
227}
228
229static inline void tx49_blast_icache32(void)
230{
231 unsigned long start = INDEX_BASE;
232 unsigned long end = start + current_cpu_data.icache.waysize;
233 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
234 unsigned long ws_end = current_cpu_data.icache.ways <<
Ralf Baechle70342282013-01-22 12:59:30 +0100235 current_cpu_data.icache.waybit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 unsigned long ws, addr;
237
238 CACHE32_UNROLL32_ALIGN2;
239 /* I'm in even chunk. blast odd chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700240 for (ws = 0; ws < ws_end; ws += ws_inc)
241 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100242 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 CACHE32_UNROLL32_ALIGN;
244 /* I'm in odd chunk. blast even chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700245 for (ws = 0; ws < ws_end; ws += ws_inc)
246 for (addr = start; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100247 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248}
249
250static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
251{
252 unsigned long flags;
253
254 local_irq_save(flags);
255 blast_icache32_page_indexed(page);
256 local_irq_restore(flags);
257}
258
259static inline void tx49_blast_icache32_page_indexed(unsigned long page)
260{
Atsushi Nemoto67a3f6de2006-04-04 17:34:14 +0900261 unsigned long indexmask = current_cpu_data.icache.waysize - 1;
262 unsigned long start = INDEX_BASE + (page & indexmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 unsigned long end = start + PAGE_SIZE;
264 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
265 unsigned long ws_end = current_cpu_data.icache.ways <<
Ralf Baechle70342282013-01-22 12:59:30 +0100266 current_cpu_data.icache.waybit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 unsigned long ws, addr;
268
269 CACHE32_UNROLL32_ALIGN2;
270 /* I'm in even chunk. blast odd chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700271 for (ws = 0; ws < ws_end; ws += ws_inc)
272 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100273 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 CACHE32_UNROLL32_ALIGN;
275 /* I'm in odd chunk. blast even chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700276 for (ws = 0; ws < ws_end; ws += ws_inc)
277 for (addr = start; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100278 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static void (* r4k_blast_icache_page)(unsigned long addr);
282
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000283static void r4k_blast_icache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 unsigned long ic_lsize = cpu_icache_line_size();
286
Chris Dearman73f40352006-06-20 18:06:52 +0100287 if (ic_lsize == 0)
288 r4k_blast_icache_page = (void *)cache_noop;
289 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 r4k_blast_icache_page = blast_icache16_page;
Aaro Koskinen43a06842014-01-14 17:56:38 -0800291 else if (ic_lsize == 32 && current_cpu_type() == CPU_LOONGSON2)
292 r4k_blast_icache_page = loongson2_blast_icache32_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 else if (ic_lsize == 32)
294 r4k_blast_icache_page = blast_icache32_page;
295 else if (ic_lsize == 64)
296 r4k_blast_icache_page = blast_icache64_page;
David Daney18a8cd62014-05-28 23:52:09 +0200297 else if (ic_lsize == 128)
298 r4k_blast_icache_page = blast_icache128_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Leonid Yegoshin4caa9062014-01-15 14:47:28 +0000301#ifndef CONFIG_EVA
302#define r4k_blast_icache_user_page r4k_blast_icache_page
303#else
304
305static void (*r4k_blast_icache_user_page)(unsigned long addr);
306
Paul Gortmaker9a8f4ea2015-04-27 18:47:57 -0400307static void r4k_blast_icache_user_page_setup(void)
Leonid Yegoshin4caa9062014-01-15 14:47:28 +0000308{
309 unsigned long ic_lsize = cpu_icache_line_size();
310
311 if (ic_lsize == 0)
312 r4k_blast_icache_user_page = (void *)cache_noop;
313 else if (ic_lsize == 16)
314 r4k_blast_icache_user_page = blast_icache16_user_page;
315 else if (ic_lsize == 32)
316 r4k_blast_icache_user_page = blast_icache32_user_page;
317 else if (ic_lsize == 64)
318 r4k_blast_icache_user_page = blast_icache64_user_page;
319}
320
321#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
324
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000325static void r4k_blast_icache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 unsigned long ic_lsize = cpu_icache_line_size();
328
Chris Dearman73f40352006-06-20 18:06:52 +0100329 if (ic_lsize == 0)
330 r4k_blast_icache_page_indexed = (void *)cache_noop;
331 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
333 else if (ic_lsize == 32) {
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000334 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 r4k_blast_icache_page_indexed =
336 blast_icache32_r4600_v1_page_indexed;
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000337 else if (TX49XX_ICACHE_INDEX_INV_WAR)
338 r4k_blast_icache_page_indexed =
339 tx49_blast_icache32_page_indexed;
Aaro Koskinen43a06842014-01-14 17:56:38 -0800340 else if (current_cpu_type() == CPU_LOONGSON2)
341 r4k_blast_icache_page_indexed =
342 loongson2_blast_icache32_page_indexed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 else
344 r4k_blast_icache_page_indexed =
345 blast_icache32_page_indexed;
346 } else if (ic_lsize == 64)
347 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
348}
349
Sanjay Lalf2e36562012-11-21 18:34:10 -0800350void (* r4k_blast_icache)(void);
351EXPORT_SYMBOL(r4k_blast_icache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000353static void r4k_blast_icache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 unsigned long ic_lsize = cpu_icache_line_size();
356
Chris Dearman73f40352006-06-20 18:06:52 +0100357 if (ic_lsize == 0)
358 r4k_blast_icache = (void *)cache_noop;
359 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 r4k_blast_icache = blast_icache16;
361 else if (ic_lsize == 32) {
362 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
363 r4k_blast_icache = blast_r4600_v1_icache32;
364 else if (TX49XX_ICACHE_INDEX_INV_WAR)
365 r4k_blast_icache = tx49_blast_icache32;
Aaro Koskinen43a06842014-01-14 17:56:38 -0800366 else if (current_cpu_type() == CPU_LOONGSON2)
367 r4k_blast_icache = loongson2_blast_icache32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 else
369 r4k_blast_icache = blast_icache32;
370 } else if (ic_lsize == 64)
371 r4k_blast_icache = blast_icache64;
David Daney18a8cd62014-05-28 23:52:09 +0200372 else if (ic_lsize == 128)
373 r4k_blast_icache = blast_icache128;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
376static void (* r4k_blast_scache_page)(unsigned long addr);
377
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000378static void r4k_blast_scache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 unsigned long sc_lsize = cpu_scache_line_size();
381
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000382 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100383 r4k_blast_scache_page = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000384 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 r4k_blast_scache_page = blast_scache16_page;
386 else if (sc_lsize == 32)
387 r4k_blast_scache_page = blast_scache32_page;
388 else if (sc_lsize == 64)
389 r4k_blast_scache_page = blast_scache64_page;
390 else if (sc_lsize == 128)
391 r4k_blast_scache_page = blast_scache128_page;
392}
393
394static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
395
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000396static void r4k_blast_scache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
398 unsigned long sc_lsize = cpu_scache_line_size();
399
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000400 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100401 r4k_blast_scache_page_indexed = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000402 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
404 else if (sc_lsize == 32)
405 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
406 else if (sc_lsize == 64)
407 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
408 else if (sc_lsize == 128)
409 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
410}
411
412static void (* r4k_blast_scache)(void);
413
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000414static void r4k_blast_scache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 unsigned long sc_lsize = cpu_scache_line_size();
417
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000418 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100419 r4k_blast_scache = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000420 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 r4k_blast_scache = blast_scache16;
422 else if (sc_lsize == 32)
423 r4k_blast_scache = blast_scache32;
424 else if (sc_lsize == 64)
425 r4k_blast_scache = blast_scache64;
426 else if (sc_lsize == 128)
427 r4k_blast_scache = blast_scache128;
428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static inline void local_r4k___flush_cache_all(void * args)
431{
Ralf Baechle10cc3522007-10-11 23:46:15 +0100432 switch (current_cpu_type()) {
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200433 case CPU_LOONGSON2:
Huacai Chenc579d312014-03-21 18:44:00 +0800434 case CPU_LOONGSON3:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 case CPU_R4000SC:
436 case CPU_R4000MC:
437 case CPU_R4400SC:
438 case CPU_R4400MC:
439 case CPU_R10000:
440 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400441 case CPU_R14000:
Joshua Kinard30577392015-01-21 07:59:45 -0500442 case CPU_R16000:
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200443 /*
444 * These caches are inclusive caches, that is, if something
445 * is not cached in the S-cache, we know it also won't be
446 * in one of the primary caches.
447 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 r4k_blast_scache();
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200449 break;
450
Florian Fainellif6758432016-04-04 10:55:36 -0700451 case CPU_BMIPS5000:
452 r4k_blast_scache();
453 __sync();
454 break;
455
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200456 default:
457 r4k_blast_dcache();
458 r4k_blast_icache();
459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461}
462
463static void r4k___flush_cache_all(void)
464{
Ralf Baechle48a26e62010-10-29 19:08:25 +0100465 r4k_on_each_cpu(local_r4k___flush_cache_all, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100468static inline int has_valid_asid(const struct mm_struct *mm)
469{
Ralf Baechleb6336482014-05-23 16:29:44 +0200470#ifdef CONFIG_MIPS_MT_SMP
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100471 int i;
472
473 for_each_online_cpu(i)
474 if (cpu_context(i, mm))
475 return 1;
476
477 return 0;
478#else
479 return cpu_context(smp_processor_id(), mm);
480#endif
481}
482
Ralf Baechle9c5a3d72008-04-05 15:13:23 +0100483static void r4k__flush_cache_vmap(void)
484{
485 r4k_blast_dcache();
486}
487
488static void r4k__flush_cache_vunmap(void)
489{
490 r4k_blast_dcache();
491}
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493static inline void local_r4k_flush_cache_range(void * args)
494{
495 struct vm_area_struct *vma = args;
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000496 int exec = vma->vm_flags & VM_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100498 if (!(has_valid_asid(vma->vm_mm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 return;
500
James Hoganb2a3c5b2016-01-22 10:58:25 +0000501 /*
502 * If dcache can alias, we must blast it since mapping is changing.
503 * If executable, we must ensure any dirty lines are written back far
504 * enough to be visible to icache.
505 */
506 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
507 r4k_blast_dcache();
508 /* If executable, blast stale lines from icache */
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000509 if (exec)
510 r4k_blast_icache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513static void r4k_flush_cache_range(struct vm_area_struct *vma,
514 unsigned long start, unsigned long end)
515{
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000516 int exec = vma->vm_flags & VM_EXEC;
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900517
James Hoganb2a3c5b2016-01-22 10:58:25 +0000518 if (cpu_has_dc_aliases || exec)
Ralf Baechle48a26e62010-10-29 19:08:25 +0100519 r4k_on_each_cpu(local_r4k_flush_cache_range, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
522static inline void local_r4k_flush_cache_mm(void * args)
523{
524 struct mm_struct *mm = args;
525
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100526 if (!has_valid_asid(mm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return;
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
Joshua Kinard30577392015-01-21 07:59:45 -0500531 * only flush the primary caches but R1x000 behave sane ...
Ralf Baechle617667b2006-11-30 01:14:48 +0000532 * R4000SC and R4400SC indexed S-cache ops also invalidate primary
533 * caches, so we can bail out early.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 */
Ralf Baechle10cc3522007-10-11 23:46:15 +0100535 if (current_cpu_type() == CPU_R4000SC ||
536 current_cpu_type() == CPU_R4000MC ||
537 current_cpu_type() == CPU_R4400SC ||
538 current_cpu_type() == CPU_R4400MC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 r4k_blast_scache();
Ralf Baechle617667b2006-11-30 01:14:48 +0000540 return;
541 }
542
543 r4k_blast_dcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
546static void r4k_flush_cache_mm(struct mm_struct *mm)
547{
548 if (!cpu_has_dc_aliases)
549 return;
550
Ralf Baechle48a26e62010-10-29 19:08:25 +0100551 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
554struct flush_cache_page_args {
555 struct vm_area_struct *vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100556 unsigned long addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900557 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558};
559
560static inline void local_r4k_flush_cache_page(void *args)
561{
562 struct flush_cache_page_args *fcp_args = args;
563 struct vm_area_struct *vma = fcp_args->vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100564 unsigned long addr = fcp_args->addr;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100565 struct page *page = pfn_to_page(fcp_args->pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 int exec = vma->vm_flags & VM_EXEC;
567 struct mm_struct *mm = vma->vm_mm;
Ralf Baechlec9c50232008-06-14 22:22:08 +0100568 int map_coherent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 pgd_t *pgdp;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000570 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 pmd_t *pmdp;
572 pte_t *ptep;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100573 void *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Ralf Baechle79acf832005-02-10 13:54:37 +0000575 /*
576 * If ownes no valid ASID yet, cannot possibly have gotten
577 * this page into the cache.
578 */
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100579 if (!has_valid_asid(mm))
Ralf Baechle79acf832005-02-10 13:54:37 +0000580 return;
581
Ralf Baechle6ec25802005-10-12 00:02:34 +0100582 addr &= PAGE_MASK;
583 pgdp = pgd_offset(mm, addr);
584 pudp = pud_offset(pgdp, addr);
585 pmdp = pmd_offset(pudp, addr);
586 ptep = pte_offset(pmdp, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /*
589 * If the page isn't marked valid, the page cannot possibly be
590 * in the cache.
591 */
Ralf Baechle526af352008-01-29 10:14:55 +0000592 if (!(pte_present(*ptep)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return;
594
Ralf Baechledb813fe2007-09-27 18:26:43 +0100595 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
596 vaddr = NULL;
597 else {
598 /*
599 * Use kmap_coherent or kmap_atomic to do flushes for
600 * another ASID than the current one.
601 */
Ralf Baechlec9c50232008-06-14 22:22:08 +0100602 map_coherent = (cpu_has_dc_aliases &&
Kirill A. Shutemove1534ae2016-01-15 16:53:46 -0800603 page_mapcount(page) &&
604 !Page_dcache_dirty(page));
Ralf Baechlec9c50232008-06-14 22:22:08 +0100605 if (map_coherent)
Ralf Baechledb813fe2007-09-27 18:26:43 +0100606 vaddr = kmap_coherent(page, addr);
607 else
Cong Wang9c020482011-11-25 23:14:15 +0800608 vaddr = kmap_atomic(page);
Ralf Baechledb813fe2007-09-27 18:26:43 +0100609 addr = (unsigned long)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
Markos Chandras80ca69f2014-01-16 13:11:08 +0000613 vaddr ? r4k_blast_dcache_page(addr) :
614 r4k_blast_dcache_user_page(addr);
Ralf Baechle39b8d522008-04-28 17:14:26 +0100615 if (exec && !cpu_icache_snoops_remote_store)
616 r4k_blast_scache_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618 if (exec) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100619 if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int cpu = smp_processor_id();
621
Thiemo Seufer26a51b22005-02-19 13:32:02 +0000622 if (cpu_context(cpu, mm) != 0)
623 drop_mmu_context(mm, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 } else
Markos Chandras80ca69f2014-01-16 13:11:08 +0000625 vaddr ? r4k_blast_icache_page(addr) :
626 r4k_blast_icache_user_page(addr);
Ralf Baechledb813fe2007-09-27 18:26:43 +0100627 }
628
629 if (vaddr) {
Ralf Baechlec9c50232008-06-14 22:22:08 +0100630 if (map_coherent)
Ralf Baechledb813fe2007-09-27 18:26:43 +0100631 kunmap_coherent();
632 else
Cong Wang9c020482011-11-25 23:14:15 +0800633 kunmap_atomic(vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635}
636
Ralf Baechle6ec25802005-10-12 00:02:34 +0100637static void r4k_flush_cache_page(struct vm_area_struct *vma,
638 unsigned long addr, unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
640 struct flush_cache_page_args args;
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 args.vma = vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100643 args.addr = addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900644 args.pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Ralf Baechle48a26e62010-10-29 19:08:25 +0100646 r4k_on_each_cpu(local_r4k_flush_cache_page, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649static inline void local_r4k_flush_data_cache_page(void * addr)
650{
651 r4k_blast_dcache_page((unsigned long) addr);
652}
653
654static void r4k_flush_data_cache_page(unsigned long addr)
655{
Ralf Baechlea754f702007-11-03 01:01:37 +0000656 if (in_atomic())
657 local_r4k_flush_data_cache_page((void *)addr);
658 else
Ralf Baechle48a26e62010-10-29 19:08:25 +0100659 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662struct flush_icache_range_args {
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900663 unsigned long start;
664 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665};
666
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +0200667static inline void local_r4k_flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (!cpu_has_ic_fills_f_dc) {
Chris Dearman73f40352006-06-20 18:06:52 +0100670 if (end - start >= dcache_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 r4k_blast_dcache();
672 } else {
Thiemo Seufer10a3dab2005-09-09 20:26:54 +0000673 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900674 protected_blast_dcache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677
678 if (end - start > icache_size)
679 r4k_blast_icache();
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200680 else {
681 switch (boot_cpu_type()) {
682 case CPU_LOONGSON2:
Huacai Chenbad009f2014-01-14 17:56:37 -0800683 protected_loongson2_blast_icache_range(start, end);
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200684 break;
685
686 default:
Huacai Chenbad009f2014-01-14 17:56:37 -0800687 protected_blast_icache_range(start, end);
Ralf Baechle14bd8c02013-09-25 18:21:26 +0200688 break;
689 }
690 }
Leonid Yegoshin4676f932014-01-21 09:48:48 +0000691#ifdef CONFIG_EVA
692 /*
693 * Due to all possible segment mappings, there might cache aliases
694 * caused by the bootloader being in non-EVA mode, and the CPU switching
695 * to EVA during early kernel init. It's best to flush the scache
696 * to avoid having secondary cores fetching stale data and lead to
697 * kernel crashes.
698 */
699 bc_wback_inv(start, (end - start));
700 __sync();
701#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702}
703
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +0200704static inline void local_r4k_flush_icache_range_ipi(void *args)
705{
706 struct flush_icache_range_args *fir_args = args;
707 unsigned long start = fir_args->start;
708 unsigned long end = fir_args->end;
709
710 local_r4k_flush_icache_range(start, end);
711}
712
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900713static void r4k_flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 struct flush_icache_range_args args;
716
717 args.start = start;
718 args.end = end;
719
Ralf Baechle48a26e62010-10-29 19:08:25 +0100720 r4k_on_each_cpu(local_r4k_flush_icache_range_ipi, &args);
Ralf Baechlecc61c1f2005-07-12 18:35:38 +0000721 instruction_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Manuel Lauss80057112014-02-20 14:59:22 +0100724#if defined(CONFIG_DMA_NONCOHERENT) || defined(CONFIG_DMA_MAYBE_COHERENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 /* Catch bad driver code */
729 BUG_ON(size == 0);
730
Ralf Baechleff522052013-09-17 12:44:31 +0200731 preempt_disable();
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100732 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900733 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900735 else
736 blast_scache_range(addr, addr + size);
Yoichi Yuasa5596b0b2013-10-02 15:03:03 +0900737 preempt_enable();
Kevin Cernekeed0023c42010-09-06 21:03:46 -0700738 __sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return;
740 }
741
742 /*
743 * Either no secondary cache or the available caches don't have the
744 * subset property so we have to flush the primary caches
745 * explicitly
746 */
Ralf Baechle39b8d522008-04-28 17:14:26 +0100747 if (cpu_has_safe_index_cacheops && size >= dcache_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 r4k_blast_dcache();
749 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900751 blast_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Ralf Baechleff522052013-09-17 12:44:31 +0200753 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 bc_wback_inv(addr, size);
Kevin Cernekeed0023c42010-09-06 21:03:46 -0700756 __sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 /* Catch bad driver code */
762 BUG_ON(size == 0);
763
Ralf Baechleff522052013-09-17 12:44:31 +0200764 preempt_disable();
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100765 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900766 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 r4k_blast_scache();
Ralf Baechlea8ca8b62009-01-11 18:44:49 +0000768 else {
Ralf Baechlea8ca8b62009-01-11 18:44:49 +0000769 /*
770 * There is no clearly documented alignment requirement
771 * for the cache instruction on MIPS processors and
772 * some processors, among them the RM5200 and RM7000
773 * QED processors will throw an address error for cache
Ralf Baechle70342282013-01-22 12:59:30 +0100774 * hit ops with insufficient alignment. Solved by
Ralf Baechlea8ca8b62009-01-11 18:44:49 +0000775 * aligning the address to cache line size.
776 */
Thomas Bogendoerfere9c33572007-11-26 23:40:01 +0100777 blast_inv_scache_range(addr, addr + size);
Ralf Baechlea8ca8b62009-01-11 18:44:49 +0000778 }
Yoichi Yuasa5596b0b2013-10-02 15:03:03 +0900779 preempt_enable();
Kevin Cernekeed0023c42010-09-06 21:03:46 -0700780 __sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return;
782 }
783
Ralf Baechle39b8d522008-04-28 17:14:26 +0100784 if (cpu_has_safe_index_cacheops && size >= dcache_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 r4k_blast_dcache();
786 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 R4600_HIT_CACHEOP_WAR_IMPL;
Thomas Bogendoerfere9c33572007-11-26 23:40:01 +0100788 blast_inv_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Ralf Baechleff522052013-09-17 12:44:31 +0200790 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 bc_inv(addr, size);
Kevin Cernekeed0023c42010-09-06 21:03:46 -0700793 __sync();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794}
Manuel Lauss80057112014-02-20 14:59:22 +0100795#endif /* CONFIG_DMA_NONCOHERENT || CONFIG_DMA_MAYBE_COHERENT */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797/*
798 * While we're protected against bad userland addresses we don't care
799 * very much about what happens in that case. Usually a segmentation
800 * fault will dump the process later on anyway ...
801 */
802static void local_r4k_flush_cache_sigtramp(void * arg)
803{
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000804 unsigned long ic_lsize = cpu_icache_line_size();
805 unsigned long dc_lsize = cpu_dcache_line_size();
806 unsigned long sc_lsize = cpu_scache_line_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 unsigned long addr = (unsigned long) arg;
808
809 R4600_HIT_CACHEOP_WAR_IMPL;
Chris Dearman73f40352006-06-20 18:06:52 +0100810 if (dc_lsize)
811 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000812 if (!cpu_icache_snoops_remote_store && scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
Chris Dearman73f40352006-06-20 18:06:52 +0100814 if (ic_lsize)
815 protected_flush_icache_line(addr & ~(ic_lsize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 if (MIPS4K_ICACHE_REFILL_WAR) {
817 __asm__ __volatile__ (
818 ".set push\n\t"
819 ".set noat\n\t"
Markos Chandras4ee48622014-12-02 15:30:19 +0000820 ".set "MIPS_ISA_LEVEL"\n\t"
Ralf Baechle875d43e2005-09-03 15:56:16 -0700821#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 "la $at,1f\n\t"
823#endif
Ralf Baechle875d43e2005-09-03 15:56:16 -0700824#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 "dla $at,1f\n\t"
826#endif
827 "cache %0,($at)\n\t"
828 "nop; nop; nop\n"
829 "1:\n\t"
830 ".set pop"
831 :
832 : "i" (Hit_Invalidate_I));
833 }
834 if (MIPS_CACHE_SYNC_WAR)
835 __asm__ __volatile__ ("sync");
836}
837
838static void r4k_flush_cache_sigtramp(unsigned long addr)
839{
Ralf Baechle48a26e62010-10-29 19:08:25 +0100840 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
843static void r4k_flush_icache_all(void)
844{
845 if (cpu_has_vtag_icache)
846 r4k_blast_icache();
847}
848
Ralf Baechled9cdc9012011-06-17 16:20:28 +0100849struct flush_kernel_vmap_range_args {
850 unsigned long vaddr;
851 int size;
852};
853
854static inline void local_r4k_flush_kernel_vmap_range(void *args)
855{
856 struct flush_kernel_vmap_range_args *vmra = args;
857 unsigned long vaddr = vmra->vaddr;
858 int size = vmra->size;
859
860 /*
861 * Aliases only affect the primary caches so don't bother with
862 * S-caches or T-caches.
863 */
864 if (cpu_has_safe_index_cacheops && size >= dcache_size)
865 r4k_blast_dcache();
866 else {
867 R4600_HIT_CACHEOP_WAR_IMPL;
868 blast_dcache_range(vaddr, vaddr + size);
869 }
870}
871
872static void r4k_flush_kernel_vmap_range(unsigned long vaddr, int size)
873{
874 struct flush_kernel_vmap_range_args args;
875
876 args.vaddr = (unsigned long) vaddr;
877 args.size = size;
878
879 r4k_on_each_cpu(local_r4k_flush_kernel_vmap_range, &args);
880}
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882static inline void rm7k_erratum31(void)
883{
884 const unsigned long ic_lsize = 32;
885 unsigned long addr;
886
887 /* RM7000 erratum #31. The icache is screwed at startup. */
888 write_c0_taglo(0);
889 write_c0_taghi(0);
890
891 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
892 __asm__ __volatile__ (
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000893 ".set push\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 ".set noreorder\n\t"
895 ".set mips3\n\t"
896 "cache\t%1, 0(%0)\n\t"
897 "cache\t%1, 0x1000(%0)\n\t"
898 "cache\t%1, 0x2000(%0)\n\t"
899 "cache\t%1, 0x3000(%0)\n\t"
900 "cache\t%2, 0(%0)\n\t"
901 "cache\t%2, 0x1000(%0)\n\t"
902 "cache\t%2, 0x2000(%0)\n\t"
903 "cache\t%2, 0x3000(%0)\n\t"
904 "cache\t%1, 0(%0)\n\t"
905 "cache\t%1, 0x1000(%0)\n\t"
906 "cache\t%1, 0x2000(%0)\n\t"
907 "cache\t%1, 0x3000(%0)\n\t"
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000908 ".set pop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 :
910 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
911 }
912}
913
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +0000914static inline int alias_74k_erratum(struct cpuinfo_mips *c)
Steven J. Hill006a8512012-06-26 04:11:03 +0000915{
Maciej W. Rozycki9213ad72013-09-18 19:08:15 +0100916 unsigned int imp = c->processor_id & PRID_IMP_MASK;
917 unsigned int rev = c->processor_id & PRID_REV_MASK;
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +0000918 int present = 0;
Maciej W. Rozycki9213ad72013-09-18 19:08:15 +0100919
Steven J. Hill006a8512012-06-26 04:11:03 +0000920 /*
921 * Early versions of the 74K do not update the cache tags on a
922 * vtag miss/ptag hit which can occur in the case of KSEG0/KUSEG
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +0000923 * aliases. In this case it is better to treat the cache as always
924 * having aliases. Also disable the synonym tag update feature
925 * where available. In this case no opportunistic tag update will
926 * happen where a load causes a virtual address miss but a physical
927 * address hit during a D-cache look-up.
Steven J. Hill006a8512012-06-26 04:11:03 +0000928 */
Maciej W. Rozycki9213ad72013-09-18 19:08:15 +0100929 switch (imp) {
930 case PRID_IMP_74K:
931 if (rev <= PRID_REV_ENCODE_332(2, 4, 0))
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +0000932 present = 1;
Maciej W. Rozycki9213ad72013-09-18 19:08:15 +0100933 if (rev == PRID_REV_ENCODE_332(2, 4, 0))
934 write_c0_config6(read_c0_config6() | MIPS_CONF6_SYND);
935 break;
936 case PRID_IMP_1074K:
937 if (rev <= PRID_REV_ENCODE_332(1, 1, 0)) {
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +0000938 present = 1;
Maciej W. Rozycki9213ad72013-09-18 19:08:15 +0100939 write_c0_config6(read_c0_config6() | MIPS_CONF6_SYND);
940 }
941 break;
942 default:
943 BUG();
Steven J. Hill006a8512012-06-26 04:11:03 +0000944 }
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +0000945
946 return present;
Steven J. Hill006a8512012-06-26 04:11:03 +0000947}
948
Kevin Cernekeed74b0172014-10-20 21:28:00 -0700949static void b5k_instruction_hazard(void)
950{
951 __sync();
952 __sync();
953 __asm__ __volatile__(
954 " nop; nop; nop; nop; nop; nop; nop; nop\n"
955 " nop; nop; nop; nop; nop; nop; nop; nop\n"
956 " nop; nop; nop; nop; nop; nop; nop; nop\n"
957 " nop; nop; nop; nop; nop; nop; nop; nop\n"
958 : : : "memory");
959}
960
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000961static char *way_string[] = { NULL, "direct mapped", "2-way",
Paul Burton1e18ac72015-07-09 10:40:41 +0100962 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way",
963 "9-way", "10-way", "11-way", "12-way",
964 "13-way", "14-way", "15-way", "16-way",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965};
966
Paul Gortmaker078a55f2013-06-18 13:38:59 +0000967static void probe_pcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct cpuinfo_mips *c = &current_cpu_data;
970 unsigned int config = read_c0_config();
971 unsigned int prid = read_c0_prid();
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +0000972 int has_74k_erratum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 unsigned long config1;
974 unsigned int lsize;
975
Ralf Baechle69f24d12013-09-17 10:25:47 +0200976 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 case CPU_R4600: /* QED style two way caches? */
978 case CPU_R4700:
979 case CPU_R5000:
980 case CPU_NEVADA:
981 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
982 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
983 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900984 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
986 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
987 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
988 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900989 c->dcache.waybit= __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990
991 c->options |= MIPS_CPU_CACHE_CDEX_P;
992 break;
993
994 case CPU_R5432:
995 case CPU_R5500:
996 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
997 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
998 c->icache.ways = 2;
999 c->icache.waybit= 0;
1000
1001 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
1002 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1003 c->dcache.ways = 2;
1004 c->dcache.waybit = 0;
1005
Shinya Kuribayashi58648102009-03-18 09:04:01 +09001006 c->options |= MIPS_CPU_CACHE_CDEX_P | MIPS_CPU_PREFETCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008
1009 case CPU_TX49XX:
1010 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
1011 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1012 c->icache.ways = 4;
1013 c->icache.waybit= 0;
1014
1015 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
1016 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1017 c->dcache.ways = 4;
1018 c->dcache.waybit = 0;
1019
1020 c->options |= MIPS_CPU_CACHE_CDEX_P;
Atsushi Nemotode862b42006-03-17 12:59:22 +09001021 c->options |= MIPS_CPU_PREFETCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 break;
1023
1024 case CPU_R4000PC:
1025 case CPU_R4000SC:
1026 case CPU_R4000MC:
1027 case CPU_R4400PC:
1028 case CPU_R4400SC:
1029 case CPU_R4400MC:
1030 case CPU_R4300:
1031 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
1032 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1033 c->icache.ways = 1;
Ralf Baechle70342282013-01-22 12:59:30 +01001034 c->icache.waybit = 0; /* doesn't matter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
1037 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1038 c->dcache.ways = 1;
1039 c->dcache.waybit = 0; /* does not matter */
1040
1041 c->options |= MIPS_CPU_CACHE_CDEX_P;
1042 break;
1043
1044 case CPU_R10000:
1045 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -04001046 case CPU_R14000:
Joshua Kinard30577392015-01-21 07:59:45 -05001047 case CPU_R16000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
1049 c->icache.linesz = 64;
1050 c->icache.ways = 2;
1051 c->icache.waybit = 0;
1052
1053 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
1054 c->dcache.linesz = 32;
1055 c->dcache.ways = 2;
1056 c->dcache.waybit = 0;
1057
1058 c->options |= MIPS_CPU_PREFETCH;
1059 break;
1060
1061 case CPU_VR4133:
Yoichi Yuasa2874fe52006-07-08 00:42:12 +09001062 write_c0_config(config & ~VR41_CONF_P4K);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 case CPU_VR4131:
1064 /* Workaround for cache instruction bug of VR4131 */
1065 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
1066 c->processor_id == 0x0c82U) {
Yoichi Yuasa4e8ab362006-07-04 22:59:41 +09001067 config |= 0x00400000U;
1068 if (c->processor_id == 0x0c80U)
1069 config |= VR41_CONF_BP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 write_c0_config(config);
Yoichi Yuasa1058ecd2006-07-08 00:42:01 +09001071 } else
1072 c->options |= MIPS_CPU_CACHE_CDEX_P;
1073
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
1075 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1076 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +09001077 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
1080 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1081 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +09001082 c->dcache.waybit = __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 break;
1084
1085 case CPU_VR41XX:
1086 case CPU_VR4111:
1087 case CPU_VR4121:
1088 case CPU_VR4122:
1089 case CPU_VR4181:
1090 case CPU_VR4181A:
1091 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
1092 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1093 c->icache.ways = 1;
Ralf Baechle70342282013-01-22 12:59:30 +01001094 c->icache.waybit = 0; /* doesn't matter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
1096 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
1097 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1098 c->dcache.ways = 1;
1099 c->dcache.waybit = 0; /* does not matter */
1100
1101 c->options |= MIPS_CPU_CACHE_CDEX_P;
1102 break;
1103
1104 case CPU_RM7000:
1105 rm7k_erratum31();
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
1108 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1109 c->icache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +09001110 c->icache.waybit = __ffs(icache_size / c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
1113 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1114 c->dcache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +09001115 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 c->options |= MIPS_CPU_CACHE_CDEX_P;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 c->options |= MIPS_CPU_PREFETCH;
1119 break;
1120
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001121 case CPU_LOONGSON2:
1122 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
1123 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1124 if (prid & 0x3)
1125 c->icache.ways = 4;
1126 else
1127 c->icache.ways = 2;
1128 c->icache.waybit = 0;
1129
1130 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
1131 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1132 if (prid & 0x3)
1133 c->dcache.ways = 4;
1134 else
1135 c->dcache.ways = 2;
1136 c->dcache.waybit = 0;
1137 break;
1138
Huacai Chenc579d312014-03-21 18:44:00 +08001139 case CPU_LOONGSON3:
1140 config1 = read_c0_config1();
1141 lsize = (config1 >> 19) & 7;
1142 if (lsize)
1143 c->icache.linesz = 2 << lsize;
1144 else
1145 c->icache.linesz = 0;
1146 c->icache.sets = 64 << ((config1 >> 22) & 7);
1147 c->icache.ways = 1 + ((config1 >> 16) & 7);
1148 icache_size = c->icache.sets *
1149 c->icache.ways *
1150 c->icache.linesz;
1151 c->icache.waybit = 0;
1152
1153 lsize = (config1 >> 10) & 7;
1154 if (lsize)
1155 c->dcache.linesz = 2 << lsize;
1156 else
1157 c->dcache.linesz = 0;
1158 c->dcache.sets = 64 << ((config1 >> 13) & 7);
1159 c->dcache.ways = 1 + ((config1 >> 7) & 7);
1160 dcache_size = c->dcache.sets *
1161 c->dcache.ways *
1162 c->dcache.linesz;
1163 c->dcache.waybit = 0;
Huacai Chen1e820da32016-03-03 09:45:13 +08001164 if ((prid & PRID_REV_MASK) >= PRID_REV_LOONGSON3A_R2)
1165 c->options |= MIPS_CPU_PREFETCH;
Huacai Chenc579d312014-03-21 18:44:00 +08001166 break;
1167
David Daney18a8cd62014-05-28 23:52:09 +02001168 case CPU_CAVIUM_OCTEON3:
1169 /* For now lie about the number of ways. */
1170 c->icache.linesz = 128;
1171 c->icache.sets = 16;
1172 c->icache.ways = 8;
1173 c->icache.flags |= MIPS_CACHE_VTAG;
1174 icache_size = c->icache.sets * c->icache.ways * c->icache.linesz;
1175
1176 c->dcache.linesz = 128;
1177 c->dcache.ways = 8;
1178 c->dcache.sets = 8;
1179 dcache_size = c->dcache.sets * c->dcache.ways * c->dcache.linesz;
1180 c->options |= MIPS_CPU_PREFETCH;
1181 break;
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 default:
1184 if (!(config & MIPS_CONF_M))
1185 panic("Don't know how to probe P-caches on this cpu.");
1186
1187 /*
1188 * So we seem to be a MIPS32 or MIPS64 CPU
1189 * So let's probe the I-cache ...
1190 */
1191 config1 = read_c0_config1();
1192
Markos Chandras175cba82013-09-19 18:18:41 +01001193 lsize = (config1 >> 19) & 7;
1194
1195 /* IL == 7 is reserved */
1196 if (lsize == 7)
1197 panic("Invalid icache line size");
1198
1199 c->icache.linesz = lsize ? 2 << lsize : 0;
1200
Douglas Leungdc34b052012-07-19 09:11:13 +02001201 c->icache.sets = 32 << (((config1 >> 22) + 1) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 c->icache.ways = 1 + ((config1 >> 16) & 7);
1203
1204 icache_size = c->icache.sets *
Ralf Baechle70342282013-01-22 12:59:30 +01001205 c->icache.ways *
1206 c->icache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +09001207 c->icache.waybit = __ffs(icache_size/c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 if (config & 0x8) /* VI bit */
1210 c->icache.flags |= MIPS_CACHE_VTAG;
1211
1212 /*
1213 * Now probe the MIPS32 / MIPS64 data cache.
1214 */
1215 c->dcache.flags = 0;
1216
Markos Chandras175cba82013-09-19 18:18:41 +01001217 lsize = (config1 >> 10) & 7;
1218
1219 /* DL == 7 is reserved */
1220 if (lsize == 7)
1221 panic("Invalid dcache line size");
1222
1223 c->dcache.linesz = lsize ? 2 << lsize : 0;
1224
Douglas Leungdc34b052012-07-19 09:11:13 +02001225 c->dcache.sets = 32 << (((config1 >> 13) + 1) & 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 c->dcache.ways = 1 + ((config1 >> 7) & 7);
1227
1228 dcache_size = c->dcache.sets *
Ralf Baechle70342282013-01-22 12:59:30 +01001229 c->dcache.ways *
1230 c->dcache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +09001231 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 c->options |= MIPS_CPU_PREFETCH;
1234 break;
1235 }
1236
1237 /*
1238 * Processor configuration sanity check for the R4000SC erratum
Ralf Baechle70342282013-01-22 12:59:30 +01001239 * #5. With page sizes larger than 32kB there is no possibility
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * to get a VCE exception anymore so we don't care about this
1241 * misconfiguration. The case is rather theoretical anyway;
1242 * presumably no vendor is shipping his hardware in the "bad"
1243 * configuration.
1244 */
Maciej W. Rozycki8ff374b2013-09-17 16:58:10 +01001245 if ((prid & PRID_IMP_MASK) == PRID_IMP_R4000 &&
1246 (prid & PRID_REV_MASK) < PRID_REV_R4400 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 !(config & CONF_SC) && c->icache.linesz != 16 &&
1248 PAGE_SIZE <= 0x8000)
1249 panic("Improper R4000SC processor configuration detected");
1250
1251 /* compute a couple of other cache variables */
1252 c->icache.waysize = icache_size / c->icache.ways;
1253 c->dcache.waysize = dcache_size / c->dcache.ways;
1254
Chris Dearman73f40352006-06-20 18:06:52 +01001255 c->icache.sets = c->icache.linesz ?
1256 icache_size / (c->icache.linesz * c->icache.ways) : 0;
1257 c->dcache.sets = c->dcache.linesz ?
1258 dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /*
Joshua Kinard30577392015-01-21 07:59:45 -05001261 * R1x000 P-caches are odd in a positive way. They're 32kB 2-way
1262 * virtually indexed so normally would suffer from aliases. So
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 * normally they'd suffer from aliases but magic in the hardware deals
1264 * with that for us so we don't need to take care ourselves.
1265 */
Ralf Baechle69f24d12013-09-17 10:25:47 +02001266 switch (current_cpu_type()) {
Ralf Baechlea95970f2005-02-07 21:41:32 +00001267 case CPU_20KC:
Ralf Baechle505403b2005-02-07 21:53:39 +00001268 case CPU_25KF:
Ralf Baechle641e97f2007-10-11 23:46:05 +01001269 case CPU_SB1:
1270 case CPU_SB1A:
Jayachandran Cefa0f812011-05-07 01:36:21 +05301271 case CPU_XLR:
Atsushi Nemotode628932006-03-13 18:23:03 +09001272 c->dcache.flags |= MIPS_CACHE_PINDEX;
Ralf Baechle641e97f2007-10-11 23:46:05 +01001273 break;
1274
Ralf Baechled1e344e2005-02-04 15:51:26 +00001275 case CPU_R10000:
1276 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -04001277 case CPU_R14000:
Joshua Kinard30577392015-01-21 07:59:45 -05001278 case CPU_R16000:
Ralf Baechled1e344e2005-02-04 15:51:26 +00001279 break;
Ralf Baechle641e97f2007-10-11 23:46:05 +01001280
Maciej W. Rozyckibf4aac02014-06-28 23:28:08 +01001281 case CPU_74K:
1282 case CPU_1074K:
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +00001283 has_74k_erratum = alias_74k_erratum(c);
Maciej W. Rozyckibf4aac02014-06-28 23:28:08 +01001284 /* Fall through. */
Steven J. Hill113c62d2012-07-06 23:56:00 +02001285 case CPU_M14KC:
Steven J. Hillf8fa4812012-12-07 03:51:35 +00001286 case CPU_M14KEC:
Ralf Baechled1e344e2005-02-04 15:51:26 +00001287 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +01001288 case CPU_34K:
Ralf Baechle39b8d522008-04-28 17:14:26 +01001289 case CPU_1004K:
Leonid Yegoshin26ab96d2013-11-27 10:07:53 +00001290 case CPU_INTERAPTIV:
James Hoganaced4cb2014-01-22 16:19:38 +00001291 case CPU_P5600:
Leonid Yegoshin708ac4b2013-11-14 16:12:27 +00001292 case CPU_PROAPTIV:
Leonid Yegoshinf36c4722014-03-04 13:34:43 +00001293 case CPU_M5150:
Leonid Yegoshin46950892014-11-24 12:59:01 +00001294 case CPU_QEMU_GENERIC:
Markos Chandras4e88a862015-07-09 10:40:36 +01001295 case CPU_I6400:
Paul Burton1091bfa2016-02-03 03:26:38 +00001296 case CPU_P6600:
Paul Burton1dbf6a82016-02-03 16:17:29 +00001297 case CPU_M6250:
Markos Chandras02dc6bf2014-01-30 17:21:29 +00001298 if (!(read_c0_config7() & MIPS_CONF7_IAR) &&
1299 (c->icache.waysize > PAGE_SIZE))
1300 c->icache.flags |= MIPS_CACHE_ALIASES;
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +00001301 if (!has_74k_erratum && (read_c0_config7() & MIPS_CONF7_AR)) {
Markos Chandras02dc6bf2014-01-30 17:21:29 +00001302 /*
1303 * Effectively physically indexed dcache,
1304 * thus no virtual aliases.
1305 */
Ralf Baechlebeab3752006-06-19 21:56:25 +01001306 c->dcache.flags |= MIPS_CACHE_PINDEX;
1307 break;
1308 }
Ralf Baechled1e344e2005-02-04 15:51:26 +00001309 default:
Maciej W. Rozyckie2e7f292014-11-16 01:02:29 +00001310 if (has_74k_erratum || c->dcache.waysize > PAGE_SIZE)
Ralf Baechlebeab3752006-06-19 21:56:25 +01001311 c->dcache.flags |= MIPS_CACHE_ALIASES;
Ralf Baechled1e344e2005-02-04 15:51:26 +00001312 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Ralf Baechle69f24d12013-09-17 10:25:47 +02001314 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 case CPU_20KC:
1316 /*
1317 * Some older 20Kc chips doesn't have the 'VI' bit in
1318 * the config register.
1319 */
1320 c->icache.flags |= MIPS_CACHE_VTAG;
1321 break;
1322
Manuel Lauss270717a2009-03-25 17:49:28 +01001323 case CPU_ALCHEMY:
James Hogan47f2ac52016-01-22 10:58:26 +00001324 case CPU_I6400:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 c->icache.flags |= MIPS_CACHE_IC_F_DC;
1326 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Florian Fainellic130d2f2016-04-04 10:55:34 -07001328 case CPU_BMIPS5000:
1329 c->icache.flags |= MIPS_CACHE_IC_F_DC;
Florian Fainelli73c4ca02016-04-04 10:55:35 -07001330 /* Cache aliases are handled in hardware; allow HIGHMEM */
1331 c->dcache.flags &= ~MIPS_CACHE_ALIASES;
Florian Fainellic130d2f2016-04-04 10:55:34 -07001332 break;
1333
Ralf Baechle14bd8c02013-09-25 18:21:26 +02001334 case CPU_LOONGSON2:
1335 /*
1336 * LOONGSON2 has 4 way icache, but when using indexed cache op,
1337 * one op will act on all 4 ways
1338 */
1339 c->icache.ways = 1;
1340 }
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1343 icache_size >> 10,
Ralf Baechle7fc73162009-04-01 16:11:53 +02001344 c->icache.flags & MIPS_CACHE_VTAG ? "VIVT" : "VIPT",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 way_string[c->icache.ways], c->icache.linesz);
1346
Ralf Baechle64bfca52007-10-15 16:35:45 +01001347 printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
1348 dcache_size >> 10, way_string[c->dcache.ways],
1349 (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
1350 (c->dcache.flags & MIPS_CACHE_ALIASES) ?
1351 "cache aliases" : "no aliases",
1352 c->dcache.linesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
Huacai Chenb2edcfc2016-03-03 09:45:09 +08001355static void probe_vcache(void)
1356{
1357 struct cpuinfo_mips *c = &current_cpu_data;
1358 unsigned int config2, lsize;
1359
1360 if (current_cpu_type() != CPU_LOONGSON3)
1361 return;
1362
1363 config2 = read_c0_config2();
1364 if ((lsize = ((config2 >> 20) & 15)))
1365 c->vcache.linesz = 2 << lsize;
1366 else
1367 c->vcache.linesz = lsize;
1368
1369 c->vcache.sets = 64 << ((config2 >> 24) & 15);
1370 c->vcache.ways = 1 + ((config2 >> 16) & 15);
1371
1372 vcache_size = c->vcache.sets * c->vcache.ways * c->vcache.linesz;
1373
1374 c->vcache.waybit = 0;
1375
1376 pr_info("Unified victim cache %ldkB %s, linesize %d bytes.\n",
1377 vcache_size >> 10, way_string[c->vcache.ways], c->vcache.linesz);
1378}
1379
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380/*
1381 * If you even _breathe_ on this function, look at the gcc output and make sure
1382 * it does not pop things on and off the stack for the cache sizing loop that
1383 * executes in KSEG1 space or else you will crash and burn badly. You have
1384 * been warned.
1385 */
Paul Gortmaker078a55f2013-06-18 13:38:59 +00001386static int probe_scache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 unsigned long flags, addr, begin, end, pow2;
1389 unsigned int config = read_c0_config();
1390 struct cpuinfo_mips *c = &current_cpu_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 if (config & CONF_SC)
1393 return 0;
1394
Ralf Baechlee001e522007-07-28 12:45:47 +01001395 begin = (unsigned long) &_stext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 begin &= ~((4 * 1024 * 1024) - 1);
1397 end = begin + (4 * 1024 * 1024);
1398
1399 /*
1400 * This is such a bitch, you'd think they would make it easy to do
1401 * this. Away you daemons of stupidity!
1402 */
1403 local_irq_save(flags);
1404
1405 /* Fill each size-multiple cache line with a valid tag. */
1406 pow2 = (64 * 1024);
1407 for (addr = begin; addr < end; addr = (begin + pow2)) {
1408 unsigned long *p = (unsigned long *) addr;
1409 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1410 pow2 <<= 1;
1411 }
1412
1413 /* Load first line with zero (therefore invalid) tag. */
1414 write_c0_taglo(0);
1415 write_c0_taghi(0);
1416 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1417 cache_op(Index_Store_Tag_I, begin);
1418 cache_op(Index_Store_Tag_D, begin);
1419 cache_op(Index_Store_Tag_SD, begin);
1420
1421 /* Now search for the wrap around point. */
1422 pow2 = (128 * 1024);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1424 cache_op(Index_Load_Tag_SD, addr);
1425 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1426 if (!read_c0_taglo())
1427 break;
1428 pow2 <<= 1;
1429 }
1430 local_irq_restore(flags);
1431 addr -= begin;
1432
1433 scache_size = addr;
1434 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1435 c->scache.ways = 1;
Joshua Kinard755af332015-06-02 16:55:22 -04001436 c->scache.waybit = 0; /* does not matter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
1438 return 1;
1439}
1440
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001441static void __init loongson2_sc_init(void)
1442{
1443 struct cpuinfo_mips *c = &current_cpu_data;
1444
1445 scache_size = 512*1024;
1446 c->scache.linesz = 32;
1447 c->scache.ways = 4;
1448 c->scache.waybit = 0;
1449 c->scache.waysize = scache_size / (c->scache.ways);
1450 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1451 pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1452 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1453
1454 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1455}
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001456
Huacai Chenc579d312014-03-21 18:44:00 +08001457static void __init loongson3_sc_init(void)
1458{
1459 struct cpuinfo_mips *c = &current_cpu_data;
1460 unsigned int config2, lsize;
1461
1462 config2 = read_c0_config2();
1463 lsize = (config2 >> 4) & 15;
1464 if (lsize)
1465 c->scache.linesz = 2 << lsize;
1466 else
1467 c->scache.linesz = 0;
1468 c->scache.sets = 64 << ((config2 >> 8) & 15);
1469 c->scache.ways = 1 + (config2 & 15);
1470
1471 scache_size = c->scache.sets *
1472 c->scache.ways *
1473 c->scache.linesz;
1474 /* Loongson-3 has 4 cores, 1MB scache for each. scaches are shared */
1475 scache_size *= 4;
1476 c->scache.waybit = 0;
1477 pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1478 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1479 if (scache_size)
1480 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1481 return;
1482}
1483
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484extern int r5k_sc_init(void);
1485extern int rm7k_sc_init(void);
Chris Dearman9318c512006-06-20 17:15:20 +01001486extern int mips_sc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Paul Gortmaker078a55f2013-06-18 13:38:59 +00001488static void setup_scache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 struct cpuinfo_mips *c = &current_cpu_data;
1491 unsigned int config = read_c0_config();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 int sc_present = 0;
1493
1494 /*
1495 * Do the probing thing on R4000SC and R4400SC processors. Other
1496 * processors don't have a S-cache that would be relevant to the
Joe Perches603e82e2008-02-03 16:54:53 +02001497 * Linux memory management.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Ralf Baechle69f24d12013-09-17 10:25:47 +02001499 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 case CPU_R4000SC:
1501 case CPU_R4000MC:
1502 case CPU_R4400SC:
1503 case CPU_R4400MC:
Thiemo Seuferba5187d2005-04-25 16:36:23 +00001504 sc_present = run_uncached(probe_scache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 if (sc_present)
1506 c->options |= MIPS_CPU_CACHE_CDEX_S;
1507 break;
1508
1509 case CPU_R10000:
1510 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -04001511 case CPU_R14000:
Joshua Kinard30577392015-01-21 07:59:45 -05001512 case CPU_R16000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1514 c->scache.linesz = 64 << ((config >> 13) & 1);
1515 c->scache.ways = 2;
1516 c->scache.waybit= 0;
1517 sc_present = 1;
1518 break;
1519
1520 case CPU_R5000:
1521 case CPU_NEVADA:
1522#ifdef CONFIG_R5000_CPU_SCACHE
1523 r5k_sc_init();
1524#endif
Ralf Baechle70342282013-01-22 12:59:30 +01001525 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526
1527 case CPU_RM7000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528#ifdef CONFIG_RM7000_CPU_SCACHE
1529 rm7k_sc_init();
1530#endif
1531 return;
1532
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001533 case CPU_LOONGSON2:
1534 loongson2_sc_init();
1535 return;
Ralf Baechle14bd8c02013-09-25 18:21:26 +02001536
Huacai Chenc579d312014-03-21 18:44:00 +08001537 case CPU_LOONGSON3:
1538 loongson3_sc_init();
1539 return;
1540
David Daney18a8cd62014-05-28 23:52:09 +02001541 case CPU_CAVIUM_OCTEON3:
Jayachandran Ca3d4fb22011-11-16 00:21:20 +00001542 case CPU_XLP:
1543 /* don't need to worry about L2, fully coherent */
1544 return;
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 default:
Deng-Cheng Zhuadb37892013-04-01 18:14:28 +00001547 if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M32R2 |
Markos Chandrasb5ad2c22015-01-15 10:28:29 +00001548 MIPS_CPU_ISA_M32R6 | MIPS_CPU_ISA_M64R1 |
1549 MIPS_CPU_ISA_M64R2 | MIPS_CPU_ISA_M64R6)) {
Chris Dearman9318c512006-06-20 17:15:20 +01001550#ifdef CONFIG_MIPS_CPU_SCACHE
1551 if (mips_sc_init ()) {
1552 scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
1553 printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
1554 scache_size >> 10,
1555 way_string[c->scache.ways], c->scache.linesz);
1556 }
1557#else
1558 if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1559 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1560#endif
1561 return;
1562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 sc_present = 0;
1564 }
1565
1566 if (!sc_present)
1567 return;
1568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 /* compute a couple of other cache variables */
1570 c->scache.waysize = scache_size / c->scache.ways;
1571
1572 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1573
1574 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1575 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1576
Ralf Baechlefc5d2d22006-07-06 13:04:01 +01001577 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578}
1579
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001580void au1x00_fixup_config_od(void)
1581{
1582 /*
1583 * c0_config.od (bit 19) was write only (and read as 0)
1584 * on the early revisions of Alchemy SOCs. It disables the bus
1585 * transaction overlapping and needs to be set to fix various errata.
1586 */
1587 switch (read_c0_prid()) {
1588 case 0x00030100: /* Au1000 DA */
1589 case 0x00030201: /* Au1000 HA */
1590 case 0x00030202: /* Au1000 HB */
1591 case 0x01030200: /* Au1500 AB */
1592 /*
1593 * Au1100 errata actually keeps silence about this bit, so we set it
1594 * just in case for those revisions that require it to be set according
Manuel Lauss270717a2009-03-25 17:49:28 +01001595 * to the (now gone) cpu table.
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001596 */
1597 case 0x02030200: /* Au1100 AB */
1598 case 0x02030201: /* Au1100 BA */
1599 case 0x02030202: /* Au1100 BC */
1600 set_c0_config(1 << 19);
1601 break;
1602 }
1603}
1604
Ralf Baechle89052bd2008-06-12 17:26:02 +01001605/* CP0 hazard avoidance. */
1606#define NXP_BARRIER() \
1607 __asm__ __volatile__( \
1608 ".set noreorder\n\t" \
1609 "nop; nop; nop; nop; nop; nop;\n\t" \
1610 ".set reorder\n\t")
1611
1612static void nxp_pr4450_fixup_config(void)
1613{
1614 unsigned long config0;
1615
1616 config0 = read_c0_config();
1617
1618 /* clear all three cache coherency fields */
1619 config0 &= ~(0x7 | (7 << 25) | (7 << 28));
1620 config0 |= (((_page_cachable_default >> _CACHE_SHIFT) << 0) |
1621 ((_page_cachable_default >> _CACHE_SHIFT) << 25) |
1622 ((_page_cachable_default >> _CACHE_SHIFT) << 28));
1623 write_c0_config(config0);
1624 NXP_BARRIER();
1625}
1626
Paul Gortmaker078a55f2013-06-18 13:38:59 +00001627static int cca = -1;
Chris Dearman35133692007-09-19 00:58:24 +01001628
1629static int __init cca_setup(char *str)
1630{
1631 get_option(&str, &cca);
1632
Shane McDonaldb5b64f22012-06-14 02:26:40 +00001633 return 0;
Chris Dearman35133692007-09-19 00:58:24 +01001634}
1635
Shane McDonaldb5b64f22012-06-14 02:26:40 +00001636early_param("cca", cca_setup);
Chris Dearman35133692007-09-19 00:58:24 +01001637
Paul Gortmaker078a55f2013-06-18 13:38:59 +00001638static void coherency_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639{
Chris Dearman35133692007-09-19 00:58:24 +01001640 if (cca < 0 || cca > 7)
1641 cca = read_c0_config() & CONF_CM_CMASK;
1642 _page_cachable_default = cca << _CACHE_SHIFT;
1643
1644 pr_debug("Using cache attribute %d\n", cca);
1645 change_c0_config(CONF_CM_CMASK, cca);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
1647 /*
1648 * c0_status.cu=0 specifies that updates by the sc instruction use
1649 * the coherency mode specified by the TLB; 1 means cachable
1650 * coherent update on write will be used. Not all processors have
1651 * this bit and; some wire it to zero, others like Toshiba had the
1652 * silly idea of putting something else there ...
1653 */
Ralf Baechle10cc3522007-10-11 23:46:15 +01001654 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 case CPU_R4000PC:
1656 case CPU_R4000SC:
1657 case CPU_R4000MC:
1658 case CPU_R4400PC:
1659 case CPU_R4400SC:
1660 case CPU_R4400MC:
1661 clear_c0_config(CONF_CU);
1662 break;
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001663 /*
Ralf Baechledf586d52006-08-01 23:42:30 +01001664 * We need to catch the early Alchemy SOCs with
Manuel Lauss270717a2009-03-25 17:49:28 +01001665 * the write-only co_config.od bit and set it back to one on:
1666 * Au1000 rev DA, HA, HB; Au1100 AB, BA, BC, Au1500 AB
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001667 */
Manuel Lauss270717a2009-03-25 17:49:28 +01001668 case CPU_ALCHEMY:
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001669 au1x00_fixup_config_od();
1670 break;
Ralf Baechle89052bd2008-06-12 17:26:02 +01001671
1672 case PRID_IMP_PR4450:
1673 nxp_pr4450_fixup_config();
1674 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 }
1676}
1677
Paul Gortmaker078a55f2013-06-18 13:38:59 +00001678static void r4k_cache_error_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679{
Ralf Baechle641e97f2007-10-11 23:46:05 +01001680 extern char __weak except_vec2_generic;
1681 extern char __weak except_vec2_sb1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Ralf Baechle69f24d12013-09-17 10:25:47 +02001683 switch (current_cpu_type()) {
Ralf Baechle641e97f2007-10-11 23:46:05 +01001684 case CPU_SB1:
1685 case CPU_SB1A:
1686 set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1687 break;
1688
1689 default:
1690 set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1691 break;
1692 }
David Daney9cd9669b2012-05-15 00:04:49 -07001693}
1694
Paul Gortmaker078a55f2013-06-18 13:38:59 +00001695void r4k_cache_init(void)
David Daney9cd9669b2012-05-15 00:04:49 -07001696{
1697 extern void build_clear_page(void);
1698 extern void build_copy_page(void);
1699 struct cpuinfo_mips *c = &current_cpu_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
1701 probe_pcache();
Huacai Chenb2edcfc2016-03-03 09:45:09 +08001702 probe_vcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 setup_scache();
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 r4k_blast_dcache_page_setup();
1706 r4k_blast_dcache_page_indexed_setup();
1707 r4k_blast_dcache_setup();
1708 r4k_blast_icache_page_setup();
1709 r4k_blast_icache_page_indexed_setup();
1710 r4k_blast_icache_setup();
1711 r4k_blast_scache_page_setup();
1712 r4k_blast_scache_page_indexed_setup();
1713 r4k_blast_scache_setup();
Leonid Yegoshin4caa9062014-01-15 14:47:28 +00001714#ifdef CONFIG_EVA
1715 r4k_blast_dcache_user_page_setup();
1716 r4k_blast_icache_user_page_setup();
1717#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 /*
1720 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1721 * This code supports virtually indexed processors and will be
1722 * unnecessarily inefficient on physically indexed processors.
1723 */
Leonid Yegoshincb80b2a2015-11-19 17:38:21 -08001724 if (c->dcache.linesz && cpu_has_dc_aliases)
Chris Dearman73f40352006-06-20 18:06:52 +01001725 shm_align_mask = max_t( unsigned long,
1726 c->dcache.sets * c->dcache.linesz - 1,
1727 PAGE_SIZE - 1);
1728 else
1729 shm_align_mask = PAGE_SIZE-1;
Ralf Baechle9c5a3d72008-04-05 15:13:23 +01001730
1731 __flush_cache_vmap = r4k__flush_cache_vmap;
1732 __flush_cache_vunmap = r4k__flush_cache_vunmap;
1733
Ralf Baechledb813fe2007-09-27 18:26:43 +01001734 flush_cache_all = cache_noop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 __flush_cache_all = r4k___flush_cache_all;
1736 flush_cache_mm = r4k_flush_cache_mm;
1737 flush_cache_page = r4k_flush_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 flush_cache_range = r4k_flush_cache_range;
1739
Ralf Baechled9cdc9012011-06-17 16:20:28 +01001740 __flush_kernel_vmap_range = r4k_flush_kernel_vmap_range;
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1743 flush_icache_all = r4k_flush_icache_all;
Ralf Baechle7e3bfc72006-04-05 20:42:04 +01001744 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 flush_data_cache_page = r4k_flush_data_cache_page;
1746 flush_icache_range = r4k_flush_icache_range;
Thomas Bogendoerfere0cee3e2008-08-04 20:53:57 +02001747 local_flush_icache_range = local_r4k_flush_icache_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Manuel Lauss80057112014-02-20 14:59:22 +01001749#if defined(CONFIG_DMA_NONCOHERENT) || defined(CONFIG_DMA_MAYBE_COHERENT)
Ralf Baechle39b8d522008-04-28 17:14:26 +01001750 if (coherentio) {
1751 _dma_cache_wback_inv = (void *)cache_noop;
1752 _dma_cache_wback = (void *)cache_noop;
1753 _dma_cache_inv = (void *)cache_noop;
1754 } else {
1755 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1756 _dma_cache_wback = r4k_dma_cache_wback_inv;
1757 _dma_cache_inv = r4k_dma_cache_inv;
1758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759#endif
1760
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 build_clear_page();
1762 build_copy_page();
Steven J. Hillb6d92b42013-03-25 13:47:29 -05001763
1764 /*
1765 * We want to run CMP kernels on core with and without coherent
1766 * caches. Therefore, do not use CONFIG_MIPS_CMP to decide whether
1767 * or not to flush caches.
1768 */
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001769 local_r4k___flush_cache_all(NULL);
Steven J. Hillb6d92b42013-03-25 13:47:29 -05001770
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001771 coherency_setup();
David Daney9cd9669b2012-05-15 00:04:49 -07001772 board_cache_error_setup = r4k_cache_error_setup;
Kevin Cernekeed74b0172014-10-20 21:28:00 -07001773
1774 /*
1775 * Per-CPU overrides
1776 */
1777 switch (current_cpu_type()) {
1778 case CPU_BMIPS4350:
1779 case CPU_BMIPS4380:
1780 /* No IPI is needed because all CPUs share the same D$ */
1781 flush_data_cache_page = r4k_blast_dcache_page;
1782 break;
1783 case CPU_BMIPS5000:
1784 /* We lose our superpowers if L2 is disabled */
1785 if (c->scache.flags & MIPS_CACHE_NOT_PRESENT)
1786 break;
1787
1788 /* I$ fills from D$ just by emptying the write buffers */
1789 flush_cache_page = (void *)b5k_instruction_hazard;
1790 flush_cache_range = (void *)b5k_instruction_hazard;
1791 flush_cache_sigtramp = (void *)b5k_instruction_hazard;
1792 local_flush_data_cache_page = (void *)b5k_instruction_hazard;
1793 flush_data_cache_page = (void *)b5k_instruction_hazard;
1794 flush_icache_range = (void *)b5k_instruction_hazard;
1795 local_flush_icache_range = (void *)b5k_instruction_hazard;
1796
Kevin Cernekeed74b0172014-10-20 21:28:00 -07001797
1798 /* Optimization: an L2 flush implicitly flushes the L1 */
1799 current_cpu_data.options |= MIPS_CPU_INCLUSIVE_CACHES;
1800 break;
Huacai Chen37fbe8f2016-03-03 09:45:10 +08001801 case CPU_LOONGSON3:
1802 /* Loongson-3 maintains cache coherency by hardware */
1803 __flush_cache_all = cache_noop;
1804 __flush_cache_vmap = cache_noop;
1805 __flush_cache_vunmap = cache_noop;
1806 __flush_kernel_vmap_range = (void *)cache_noop;
1807 flush_cache_mm = (void *)cache_noop;
1808 flush_cache_page = (void *)cache_noop;
1809 flush_cache_range = (void *)cache_noop;
1810 flush_cache_sigtramp = (void *)cache_noop;
1811 flush_icache_all = (void *)cache_noop;
1812 flush_data_cache_page = (void *)cache_noop;
1813 local_flush_data_cache_page = (void *)cache_noop;
1814 break;
Kevin Cernekeed74b0172014-10-20 21:28:00 -07001815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
James Hogan61d73042014-03-04 10:23:57 +00001817
1818static int r4k_cache_pm_notifier(struct notifier_block *self, unsigned long cmd,
1819 void *v)
1820{
1821 switch (cmd) {
1822 case CPU_PM_ENTER_FAILED:
1823 case CPU_PM_EXIT:
1824 coherency_setup();
1825 break;
1826 }
1827
1828 return NOTIFY_OK;
1829}
1830
1831static struct notifier_block r4k_cache_pm_notifier_block = {
1832 .notifier_call = r4k_cache_pm_notifier,
1833};
1834
1835int __init r4k_cache_init_pm(void)
1836{
1837 return cpu_pm_register_notifier(&r4k_cache_pm_notifier_block);
1838}
1839arch_initcall(r4k_cache_init_pm);