blob: 3d3e53651341000f3f4b0eb6c5da1576c708254c [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 *
6 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
7 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Ralf Baechle (ralf@gnu.org)
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
Ralf Baechlea754f702007-11-03 01:01:37 +000010#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
Ralf Baechledb813fe2007-09-27 18:26:43 +010012#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Ralf Baechle641e97f2007-10-11 23:46:05 +010014#include <linux/linkage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sched.h>
16#include <linux/mm.h>
Chris Dearman35133692007-09-19 00:58:24 +010017#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/bitops.h>
19
20#include <asm/bcache.h>
21#include <asm/bootinfo.h>
Ralf Baechleec74e362005-07-13 11:48:45 +000022#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/cacheops.h>
24#include <asm/cpu.h>
25#include <asm/cpu-features.h>
26#include <asm/io.h>
27#include <asm/page.h>
28#include <asm/pgtable.h>
29#include <asm/r4kcache.h>
Ralf Baechlee001e522007-07-28 12:45:47 +010030#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/system.h>
32#include <asm/mmu_context.h>
33#include <asm/war.h>
Thiemo Seuferba5187d2005-04-25 16:36:23 +000034#include <asm/cacheflush.h> /* for run_uncached() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Ralf Baechle7f3f1d02006-05-12 13:20:06 +010036
37/*
38 * Special Variant of smp_call_function for use by cache functions:
39 *
40 * o No return value
41 * o collapses to normal function call on UP kernels
42 * o collapses to normal function call on systems with a single shared
43 * primary cache.
44 */
45static inline void r4k_on_each_cpu(void (*func) (void *info), void *info,
46 int retry, int wait)
47{
48 preempt_disable();
49
50#if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
51 smp_call_function(func, info, retry, wait);
52#endif
53 func(info);
54 preempt_enable();
55}
56
Ralf Baechleec74e362005-07-13 11:48:45 +000057/*
58 * Must die.
59 */
60static unsigned long icache_size __read_mostly;
61static unsigned long dcache_size __read_mostly;
62static unsigned long scache_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64/*
65 * Dummy cache handling routines for machines without boardcaches
66 */
Chris Dearman73f40352006-06-20 18:06:52 +010067static void cache_noop(void) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static struct bcache_ops no_sc_ops = {
Chris Dearman73f40352006-06-20 18:06:52 +010070 .bc_enable = (void *)cache_noop,
71 .bc_disable = (void *)cache_noop,
72 .bc_wback_inv = (void *)cache_noop,
73 .bc_inv = (void *)cache_noop
Linus Torvalds1da177e2005-04-16 15:20:36 -070074};
75
76struct bcache_ops *bcops = &no_sc_ops;
77
Thiemo Seufer330cfe02005-09-01 18:33:58 +000078#define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
79#define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81#define R4600_HIT_CACHEOP_WAR_IMPL \
82do { \
83 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
84 *(volatile unsigned long *)CKSEG1; \
85 if (R4600_V1_HIT_CACHEOP_WAR) \
86 __asm__ __volatile__("nop;nop;nop;nop"); \
87} while (0)
88
89static void (*r4k_blast_dcache_page)(unsigned long addr);
90
91static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
92{
93 R4600_HIT_CACHEOP_WAR_IMPL;
94 blast_dcache32_page(addr);
95}
96
Ralf Baechle234fcd12008-03-08 09:56:28 +000097static void __cpuinit r4k_blast_dcache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 unsigned long dc_lsize = cpu_dcache_line_size();
100
Chris Dearman73f40352006-06-20 18:06:52 +0100101 if (dc_lsize == 0)
102 r4k_blast_dcache_page = (void *)cache_noop;
103 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 r4k_blast_dcache_page = blast_dcache16_page;
105 else if (dc_lsize == 32)
106 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
107}
108
109static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
110
Ralf Baechle234fcd12008-03-08 09:56:28 +0000111static void __cpuinit r4k_blast_dcache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 unsigned long dc_lsize = cpu_dcache_line_size();
114
Chris Dearman73f40352006-06-20 18:06:52 +0100115 if (dc_lsize == 0)
116 r4k_blast_dcache_page_indexed = (void *)cache_noop;
117 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
119 else if (dc_lsize == 32)
120 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
121}
122
123static void (* r4k_blast_dcache)(void);
124
Ralf Baechle234fcd12008-03-08 09:56:28 +0000125static void __cpuinit r4k_blast_dcache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
127 unsigned long dc_lsize = cpu_dcache_line_size();
128
Chris Dearman73f40352006-06-20 18:06:52 +0100129 if (dc_lsize == 0)
130 r4k_blast_dcache = (void *)cache_noop;
131 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 r4k_blast_dcache = blast_dcache16;
133 else if (dc_lsize == 32)
134 r4k_blast_dcache = blast_dcache32;
135}
136
137/* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
138#define JUMP_TO_ALIGN(order) \
139 __asm__ __volatile__( \
140 "b\t1f\n\t" \
141 ".align\t" #order "\n\t" \
142 "1:\n\t" \
143 )
144#define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
145#define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
146
147static inline void blast_r4600_v1_icache32(void)
148{
149 unsigned long flags;
150
151 local_irq_save(flags);
152 blast_icache32();
153 local_irq_restore(flags);
154}
155
156static inline void tx49_blast_icache32(void)
157{
158 unsigned long start = INDEX_BASE;
159 unsigned long end = start + current_cpu_data.icache.waysize;
160 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
161 unsigned long ws_end = current_cpu_data.icache.ways <<
162 current_cpu_data.icache.waybit;
163 unsigned long ws, addr;
164
165 CACHE32_UNROLL32_ALIGN2;
166 /* I'm in even chunk. blast odd chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700167 for (ws = 0; ws < ws_end; ws += ws_inc)
168 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100169 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 CACHE32_UNROLL32_ALIGN;
171 /* I'm in odd chunk. blast even chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700172 for (ws = 0; ws < ws_end; ws += ws_inc)
173 for (addr = start; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100174 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
177static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
178{
179 unsigned long flags;
180
181 local_irq_save(flags);
182 blast_icache32_page_indexed(page);
183 local_irq_restore(flags);
184}
185
186static inline void tx49_blast_icache32_page_indexed(unsigned long page)
187{
Atsushi Nemoto67a3f6de2006-04-04 17:34:14 +0900188 unsigned long indexmask = current_cpu_data.icache.waysize - 1;
189 unsigned long start = INDEX_BASE + (page & indexmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 unsigned long end = start + PAGE_SIZE;
191 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
192 unsigned long ws_end = current_cpu_data.icache.ways <<
193 current_cpu_data.icache.waybit;
194 unsigned long ws, addr;
195
196 CACHE32_UNROLL32_ALIGN2;
197 /* I'm in even chunk. blast odd chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700198 for (ws = 0; ws < ws_end; ws += ws_inc)
199 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100200 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 CACHE32_UNROLL32_ALIGN;
202 /* I'm in odd chunk. blast even chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700203 for (ws = 0; ws < ws_end; ws += ws_inc)
204 for (addr = start; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100205 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static void (* r4k_blast_icache_page)(unsigned long addr);
209
Ralf Baechle234fcd12008-03-08 09:56:28 +0000210static void __cpuinit r4k_blast_icache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 unsigned long ic_lsize = cpu_icache_line_size();
213
Chris Dearman73f40352006-06-20 18:06:52 +0100214 if (ic_lsize == 0)
215 r4k_blast_icache_page = (void *)cache_noop;
216 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 r4k_blast_icache_page = blast_icache16_page;
218 else if (ic_lsize == 32)
219 r4k_blast_icache_page = blast_icache32_page;
220 else if (ic_lsize == 64)
221 r4k_blast_icache_page = blast_icache64_page;
222}
223
224
225static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
226
Ralf Baechle234fcd12008-03-08 09:56:28 +0000227static void __cpuinit r4k_blast_icache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 unsigned long ic_lsize = cpu_icache_line_size();
230
Chris Dearman73f40352006-06-20 18:06:52 +0100231 if (ic_lsize == 0)
232 r4k_blast_icache_page_indexed = (void *)cache_noop;
233 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
235 else if (ic_lsize == 32) {
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000236 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 r4k_blast_icache_page_indexed =
238 blast_icache32_r4600_v1_page_indexed;
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000239 else if (TX49XX_ICACHE_INDEX_INV_WAR)
240 r4k_blast_icache_page_indexed =
241 tx49_blast_icache32_page_indexed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 else
243 r4k_blast_icache_page_indexed =
244 blast_icache32_page_indexed;
245 } else if (ic_lsize == 64)
246 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
247}
248
249static void (* r4k_blast_icache)(void);
250
Ralf Baechle234fcd12008-03-08 09:56:28 +0000251static void __cpuinit r4k_blast_icache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 unsigned long ic_lsize = cpu_icache_line_size();
254
Chris Dearman73f40352006-06-20 18:06:52 +0100255 if (ic_lsize == 0)
256 r4k_blast_icache = (void *)cache_noop;
257 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 r4k_blast_icache = blast_icache16;
259 else if (ic_lsize == 32) {
260 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
261 r4k_blast_icache = blast_r4600_v1_icache32;
262 else if (TX49XX_ICACHE_INDEX_INV_WAR)
263 r4k_blast_icache = tx49_blast_icache32;
264 else
265 r4k_blast_icache = blast_icache32;
266 } else if (ic_lsize == 64)
267 r4k_blast_icache = blast_icache64;
268}
269
270static void (* r4k_blast_scache_page)(unsigned long addr);
271
Ralf Baechle234fcd12008-03-08 09:56:28 +0000272static void __cpuinit r4k_blast_scache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 unsigned long sc_lsize = cpu_scache_line_size();
275
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000276 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100277 r4k_blast_scache_page = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000278 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 r4k_blast_scache_page = blast_scache16_page;
280 else if (sc_lsize == 32)
281 r4k_blast_scache_page = blast_scache32_page;
282 else if (sc_lsize == 64)
283 r4k_blast_scache_page = blast_scache64_page;
284 else if (sc_lsize == 128)
285 r4k_blast_scache_page = blast_scache128_page;
286}
287
288static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
289
Ralf Baechle234fcd12008-03-08 09:56:28 +0000290static void __cpuinit r4k_blast_scache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned long sc_lsize = cpu_scache_line_size();
293
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000294 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100295 r4k_blast_scache_page_indexed = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000296 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
298 else if (sc_lsize == 32)
299 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
300 else if (sc_lsize == 64)
301 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
302 else if (sc_lsize == 128)
303 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
304}
305
306static void (* r4k_blast_scache)(void);
307
Ralf Baechle234fcd12008-03-08 09:56:28 +0000308static void __cpuinit r4k_blast_scache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 unsigned long sc_lsize = cpu_scache_line_size();
311
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000312 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100313 r4k_blast_scache = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000314 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 r4k_blast_scache = blast_scache16;
316 else if (sc_lsize == 32)
317 r4k_blast_scache = blast_scache32;
318 else if (sc_lsize == 64)
319 r4k_blast_scache = blast_scache64;
320 else if (sc_lsize == 128)
321 r4k_blast_scache = blast_scache128;
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324static inline void local_r4k___flush_cache_all(void * args)
325{
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800326#if defined(CONFIG_CPU_LOONGSON2)
327 r4k_blast_scache();
328 return;
329#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 r4k_blast_dcache();
331 r4k_blast_icache();
332
Ralf Baechle10cc3522007-10-11 23:46:15 +0100333 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 case CPU_R4000SC:
335 case CPU_R4000MC:
336 case CPU_R4400SC:
337 case CPU_R4400MC:
338 case CPU_R10000:
339 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400340 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 r4k_blast_scache();
342 }
343}
344
345static void r4k___flush_cache_all(void)
346{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100347 r4k_on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100350static inline int has_valid_asid(const struct mm_struct *mm)
351{
352#if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
353 int i;
354
355 for_each_online_cpu(i)
356 if (cpu_context(i, mm))
357 return 1;
358
359 return 0;
360#else
361 return cpu_context(smp_processor_id(), mm);
362#endif
363}
364
Ralf Baechle9c5a3d72008-04-05 15:13:23 +0100365static void r4k__flush_cache_vmap(void)
366{
367 r4k_blast_dcache();
368}
369
370static void r4k__flush_cache_vunmap(void)
371{
372 r4k_blast_dcache();
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375static inline void local_r4k_flush_cache_range(void * args)
376{
377 struct vm_area_struct *vma = args;
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000378 int exec = vma->vm_flags & VM_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100380 if (!(has_valid_asid(vma->vm_mm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 return;
382
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900383 r4k_blast_dcache();
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000384 if (exec)
385 r4k_blast_icache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388static void r4k_flush_cache_range(struct vm_area_struct *vma,
389 unsigned long start, unsigned long end)
390{
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000391 int exec = vma->vm_flags & VM_EXEC;
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900392
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000393 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
394 r4k_on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397static inline void local_r4k_flush_cache_mm(void * args)
398{
399 struct mm_struct *mm = args;
400
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100401 if (!has_valid_asid(mm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return;
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 /*
405 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
406 * only flush the primary caches but R10000 and R12000 behave sane ...
Ralf Baechle617667b2006-11-30 01:14:48 +0000407 * R4000SC and R4400SC indexed S-cache ops also invalidate primary
408 * caches, so we can bail out early.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Ralf Baechle10cc3522007-10-11 23:46:15 +0100410 if (current_cpu_type() == CPU_R4000SC ||
411 current_cpu_type() == CPU_R4000MC ||
412 current_cpu_type() == CPU_R4400SC ||
413 current_cpu_type() == CPU_R4400MC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 r4k_blast_scache();
Ralf Baechle617667b2006-11-30 01:14:48 +0000415 return;
416 }
417
418 r4k_blast_dcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421static void r4k_flush_cache_mm(struct mm_struct *mm)
422{
423 if (!cpu_has_dc_aliases)
424 return;
425
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100426 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
429struct flush_cache_page_args {
430 struct vm_area_struct *vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100431 unsigned long addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900432 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433};
434
435static inline void local_r4k_flush_cache_page(void *args)
436{
437 struct flush_cache_page_args *fcp_args = args;
438 struct vm_area_struct *vma = fcp_args->vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100439 unsigned long addr = fcp_args->addr;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100440 struct page *page = pfn_to_page(fcp_args->pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int exec = vma->vm_flags & VM_EXEC;
442 struct mm_struct *mm = vma->vm_mm;
443 pgd_t *pgdp;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000444 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 pmd_t *pmdp;
446 pte_t *ptep;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100447 void *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Ralf Baechle79acf832005-02-10 13:54:37 +0000449 /*
450 * If ownes no valid ASID yet, cannot possibly have gotten
451 * this page into the cache.
452 */
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100453 if (!has_valid_asid(mm))
Ralf Baechle79acf832005-02-10 13:54:37 +0000454 return;
455
Ralf Baechle6ec25802005-10-12 00:02:34 +0100456 addr &= PAGE_MASK;
457 pgdp = pgd_offset(mm, addr);
458 pudp = pud_offset(pgdp, addr);
459 pmdp = pmd_offset(pudp, addr);
460 ptep = pte_offset(pmdp, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 /*
463 * If the page isn't marked valid, the page cannot possibly be
464 * in the cache.
465 */
Ralf Baechle526af352008-01-29 10:14:55 +0000466 if (!(pte_present(*ptep)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return;
468
Ralf Baechledb813fe2007-09-27 18:26:43 +0100469 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
470 vaddr = NULL;
471 else {
472 /*
473 * Use kmap_coherent or kmap_atomic to do flushes for
474 * another ASID than the current one.
475 */
476 if (cpu_has_dc_aliases)
477 vaddr = kmap_coherent(page, addr);
478 else
479 vaddr = kmap_atomic(page, KM_USER0);
480 addr = (unsigned long)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100484 r4k_blast_dcache_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 if (exec) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100487 if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int cpu = smp_processor_id();
489
Thiemo Seufer26a51b22005-02-19 13:32:02 +0000490 if (cpu_context(cpu, mm) != 0)
491 drop_mmu_context(mm, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 } else
Ralf Baechledb813fe2007-09-27 18:26:43 +0100493 r4k_blast_icache_page(addr);
494 }
495
496 if (vaddr) {
497 if (cpu_has_dc_aliases)
498 kunmap_coherent();
499 else
500 kunmap_atomic(vaddr, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502}
503
Ralf Baechle6ec25802005-10-12 00:02:34 +0100504static void r4k_flush_cache_page(struct vm_area_struct *vma,
505 unsigned long addr, unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct flush_cache_page_args args;
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 args.vma = vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100510 args.addr = addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900511 args.pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100513 r4k_on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516static inline void local_r4k_flush_data_cache_page(void * addr)
517{
518 r4k_blast_dcache_page((unsigned long) addr);
519}
520
521static void r4k_flush_data_cache_page(unsigned long addr)
522{
Ralf Baechlea754f702007-11-03 01:01:37 +0000523 if (in_atomic())
524 local_r4k_flush_data_cache_page((void *)addr);
525 else
526 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr,
527 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
530struct flush_icache_range_args {
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900531 unsigned long start;
532 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533};
534
535static inline void local_r4k_flush_icache_range(void *args)
536{
537 struct flush_icache_range_args *fir_args = args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 unsigned long start = fir_args->start;
539 unsigned long end = fir_args->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (!cpu_has_ic_fills_f_dc) {
Chris Dearman73f40352006-06-20 18:06:52 +0100542 if (end - start >= dcache_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 r4k_blast_dcache();
544 } else {
Thiemo Seufer10a3dab2005-09-09 20:26:54 +0000545 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900546 protected_blast_dcache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
550 if (end - start > icache_size)
551 r4k_blast_icache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900552 else
553 protected_blast_icache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900556static void r4k_flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
558 struct flush_icache_range_args args;
559
560 args.start = start;
561 args.end = end;
562
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100563 r4k_on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
Ralf Baechlecc61c1f2005-07-12 18:35:38 +0000564 instruction_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#ifdef CONFIG_DMA_NONCOHERENT
568
569static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
570{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Catch bad driver code */
572 BUG_ON(size == 0);
573
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100574 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900575 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900577 else
578 blast_scache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return;
580 }
581
582 /*
583 * Either no secondary cache or the available caches don't have the
584 * subset property so we have to flush the primary caches
585 * explicitly
586 */
587 if (size >= dcache_size) {
588 r4k_blast_dcache();
589 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900591 blast_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 bc_wback_inv(addr, size);
595}
596
597static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
598{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 /* Catch bad driver code */
600 BUG_ON(size == 0);
601
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100602 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900603 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900605 else
Thomas Bogendoerfere9c33572007-11-26 23:40:01 +0100606 blast_inv_scache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return;
608 }
609
610 if (size >= dcache_size) {
611 r4k_blast_dcache();
612 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 R4600_HIT_CACHEOP_WAR_IMPL;
Thomas Bogendoerfere9c33572007-11-26 23:40:01 +0100614 blast_inv_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 }
616
617 bc_inv(addr, size);
618}
619#endif /* CONFIG_DMA_NONCOHERENT */
620
621/*
622 * While we're protected against bad userland addresses we don't care
623 * very much about what happens in that case. Usually a segmentation
624 * fault will dump the process later on anyway ...
625 */
626static void local_r4k_flush_cache_sigtramp(void * arg)
627{
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000628 unsigned long ic_lsize = cpu_icache_line_size();
629 unsigned long dc_lsize = cpu_dcache_line_size();
630 unsigned long sc_lsize = cpu_scache_line_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 unsigned long addr = (unsigned long) arg;
632
633 R4600_HIT_CACHEOP_WAR_IMPL;
Chris Dearman73f40352006-06-20 18:06:52 +0100634 if (dc_lsize)
635 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000636 if (!cpu_icache_snoops_remote_store && scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
Chris Dearman73f40352006-06-20 18:06:52 +0100638 if (ic_lsize)
639 protected_flush_icache_line(addr & ~(ic_lsize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (MIPS4K_ICACHE_REFILL_WAR) {
641 __asm__ __volatile__ (
642 ".set push\n\t"
643 ".set noat\n\t"
644 ".set mips3\n\t"
Ralf Baechle875d43e2005-09-03 15:56:16 -0700645#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 "la $at,1f\n\t"
647#endif
Ralf Baechle875d43e2005-09-03 15:56:16 -0700648#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 "dla $at,1f\n\t"
650#endif
651 "cache %0,($at)\n\t"
652 "nop; nop; nop\n"
653 "1:\n\t"
654 ".set pop"
655 :
656 : "i" (Hit_Invalidate_I));
657 }
658 if (MIPS_CACHE_SYNC_WAR)
659 __asm__ __volatile__ ("sync");
660}
661
662static void r4k_flush_cache_sigtramp(unsigned long addr)
663{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100664 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667static void r4k_flush_icache_all(void)
668{
669 if (cpu_has_vtag_icache)
670 r4k_blast_icache();
671}
672
673static inline void rm7k_erratum31(void)
674{
675 const unsigned long ic_lsize = 32;
676 unsigned long addr;
677
678 /* RM7000 erratum #31. The icache is screwed at startup. */
679 write_c0_taglo(0);
680 write_c0_taghi(0);
681
682 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
683 __asm__ __volatile__ (
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000684 ".set push\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ".set noreorder\n\t"
686 ".set mips3\n\t"
687 "cache\t%1, 0(%0)\n\t"
688 "cache\t%1, 0x1000(%0)\n\t"
689 "cache\t%1, 0x2000(%0)\n\t"
690 "cache\t%1, 0x3000(%0)\n\t"
691 "cache\t%2, 0(%0)\n\t"
692 "cache\t%2, 0x1000(%0)\n\t"
693 "cache\t%2, 0x2000(%0)\n\t"
694 "cache\t%2, 0x3000(%0)\n\t"
695 "cache\t%1, 0(%0)\n\t"
696 "cache\t%1, 0x1000(%0)\n\t"
697 "cache\t%1, 0x2000(%0)\n\t"
698 "cache\t%1, 0x3000(%0)\n\t"
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000699 ".set pop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 :
701 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
702 }
703}
704
Ralf Baechle234fcd12008-03-08 09:56:28 +0000705static char *way_string[] __cpuinitdata = { NULL, "direct mapped", "2-way",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
707};
708
Ralf Baechle234fcd12008-03-08 09:56:28 +0000709static void __cpuinit probe_pcache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct cpuinfo_mips *c = &current_cpu_data;
712 unsigned int config = read_c0_config();
713 unsigned int prid = read_c0_prid();
714 unsigned long config1;
715 unsigned int lsize;
716
717 switch (c->cputype) {
718 case CPU_R4600: /* QED style two way caches? */
719 case CPU_R4700:
720 case CPU_R5000:
721 case CPU_NEVADA:
722 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
723 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
724 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900725 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
728 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
729 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900730 c->dcache.waybit= __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 c->options |= MIPS_CPU_CACHE_CDEX_P;
733 break;
734
735 case CPU_R5432:
736 case CPU_R5500:
737 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
738 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
739 c->icache.ways = 2;
740 c->icache.waybit= 0;
741
742 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
743 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
744 c->dcache.ways = 2;
745 c->dcache.waybit = 0;
746
747 c->options |= MIPS_CPU_CACHE_CDEX_P;
748 break;
749
750 case CPU_TX49XX:
751 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
752 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
753 c->icache.ways = 4;
754 c->icache.waybit= 0;
755
756 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
757 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
758 c->dcache.ways = 4;
759 c->dcache.waybit = 0;
760
761 c->options |= MIPS_CPU_CACHE_CDEX_P;
Atsushi Nemotode862b42006-03-17 12:59:22 +0900762 c->options |= MIPS_CPU_PREFETCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
764
765 case CPU_R4000PC:
766 case CPU_R4000SC:
767 case CPU_R4000MC:
768 case CPU_R4400PC:
769 case CPU_R4400SC:
770 case CPU_R4400MC:
771 case CPU_R4300:
772 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
773 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
774 c->icache.ways = 1;
775 c->icache.waybit = 0; /* doesn't matter */
776
777 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
778 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
779 c->dcache.ways = 1;
780 c->dcache.waybit = 0; /* does not matter */
781
782 c->options |= MIPS_CPU_CACHE_CDEX_P;
783 break;
784
785 case CPU_R10000:
786 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400787 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
789 c->icache.linesz = 64;
790 c->icache.ways = 2;
791 c->icache.waybit = 0;
792
793 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
794 c->dcache.linesz = 32;
795 c->dcache.ways = 2;
796 c->dcache.waybit = 0;
797
798 c->options |= MIPS_CPU_PREFETCH;
799 break;
800
801 case CPU_VR4133:
Yoichi Yuasa2874fe52006-07-08 00:42:12 +0900802 write_c0_config(config & ~VR41_CONF_P4K);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 case CPU_VR4131:
804 /* Workaround for cache instruction bug of VR4131 */
805 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
806 c->processor_id == 0x0c82U) {
Yoichi Yuasa4e8ab362006-07-04 22:59:41 +0900807 config |= 0x00400000U;
808 if (c->processor_id == 0x0c80U)
809 config |= VR41_CONF_BP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 write_c0_config(config);
Yoichi Yuasa1058ecd2006-07-08 00:42:01 +0900811 } else
812 c->options |= MIPS_CPU_CACHE_CDEX_P;
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
815 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
816 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900817 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
820 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
821 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900822 c->dcache.waybit = __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 break;
824
825 case CPU_VR41XX:
826 case CPU_VR4111:
827 case CPU_VR4121:
828 case CPU_VR4122:
829 case CPU_VR4181:
830 case CPU_VR4181A:
831 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
832 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
833 c->icache.ways = 1;
834 c->icache.waybit = 0; /* doesn't matter */
835
836 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
837 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
838 c->dcache.ways = 1;
839 c->dcache.waybit = 0; /* does not matter */
840
841 c->options |= MIPS_CPU_CACHE_CDEX_P;
842 break;
843
844 case CPU_RM7000:
845 rm7k_erratum31();
846
847 case CPU_RM9000:
848 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
849 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
850 c->icache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900851 c->icache.waybit = __ffs(icache_size / c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
854 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
855 c->dcache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900856 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858#if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
859 c->options |= MIPS_CPU_CACHE_CDEX_P;
860#endif
861 c->options |= MIPS_CPU_PREFETCH;
862 break;
863
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800864 case CPU_LOONGSON2:
865 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
866 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
867 if (prid & 0x3)
868 c->icache.ways = 4;
869 else
870 c->icache.ways = 2;
871 c->icache.waybit = 0;
872
873 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
874 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
875 if (prid & 0x3)
876 c->dcache.ways = 4;
877 else
878 c->dcache.ways = 2;
879 c->dcache.waybit = 0;
880 break;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 default:
883 if (!(config & MIPS_CONF_M))
884 panic("Don't know how to probe P-caches on this cpu.");
885
886 /*
887 * So we seem to be a MIPS32 or MIPS64 CPU
888 * So let's probe the I-cache ...
889 */
890 config1 = read_c0_config1();
891
892 if ((lsize = ((config1 >> 19) & 7)))
893 c->icache.linesz = 2 << lsize;
894 else
895 c->icache.linesz = lsize;
896 c->icache.sets = 64 << ((config1 >> 22) & 7);
897 c->icache.ways = 1 + ((config1 >> 16) & 7);
898
899 icache_size = c->icache.sets *
900 c->icache.ways *
901 c->icache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900902 c->icache.waybit = __ffs(icache_size/c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (config & 0x8) /* VI bit */
905 c->icache.flags |= MIPS_CACHE_VTAG;
906
907 /*
908 * Now probe the MIPS32 / MIPS64 data cache.
909 */
910 c->dcache.flags = 0;
911
912 if ((lsize = ((config1 >> 10) & 7)))
913 c->dcache.linesz = 2 << lsize;
914 else
915 c->dcache.linesz= lsize;
916 c->dcache.sets = 64 << ((config1 >> 13) & 7);
917 c->dcache.ways = 1 + ((config1 >> 7) & 7);
918
919 dcache_size = c->dcache.sets *
920 c->dcache.ways *
921 c->dcache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900922 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 c->options |= MIPS_CPU_PREFETCH;
925 break;
926 }
927
928 /*
929 * Processor configuration sanity check for the R4000SC erratum
930 * #5. With page sizes larger than 32kB there is no possibility
931 * to get a VCE exception anymore so we don't care about this
932 * misconfiguration. The case is rather theoretical anyway;
933 * presumably no vendor is shipping his hardware in the "bad"
934 * configuration.
935 */
936 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
937 !(config & CONF_SC) && c->icache.linesz != 16 &&
938 PAGE_SIZE <= 0x8000)
939 panic("Improper R4000SC processor configuration detected");
940
941 /* compute a couple of other cache variables */
942 c->icache.waysize = icache_size / c->icache.ways;
943 c->dcache.waysize = dcache_size / c->dcache.ways;
944
Chris Dearman73f40352006-06-20 18:06:52 +0100945 c->icache.sets = c->icache.linesz ?
946 icache_size / (c->icache.linesz * c->icache.ways) : 0;
947 c->dcache.sets = c->dcache.linesz ?
948 dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
950 /*
951 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
952 * 2-way virtually indexed so normally would suffer from aliases. So
953 * normally they'd suffer from aliases but magic in the hardware deals
954 * with that for us so we don't need to take care ourselves.
955 */
Ralf Baechled1e344e2005-02-04 15:51:26 +0000956 switch (c->cputype) {
Ralf Baechlea95970f2005-02-07 21:41:32 +0000957 case CPU_20KC:
Ralf Baechle505403b2005-02-07 21:53:39 +0000958 case CPU_25KF:
Ralf Baechle641e97f2007-10-11 23:46:05 +0100959 case CPU_SB1:
960 case CPU_SB1A:
Atsushi Nemotode628932006-03-13 18:23:03 +0900961 c->dcache.flags |= MIPS_CACHE_PINDEX;
Ralf Baechle641e97f2007-10-11 23:46:05 +0100962 break;
963
Ralf Baechled1e344e2005-02-04 15:51:26 +0000964 case CPU_R10000:
965 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400966 case CPU_R14000:
Ralf Baechled1e344e2005-02-04 15:51:26 +0000967 break;
Ralf Baechle641e97f2007-10-11 23:46:05 +0100968
Ralf Baechled1e344e2005-02-04 15:51:26 +0000969 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +0100970 case CPU_34K:
Ralf Baechle2e78ae32006-06-23 18:48:21 +0100971 case CPU_74K:
Ralf Baechlebeab3752006-06-19 21:56:25 +0100972 if ((read_c0_config7() & (1 << 16))) {
973 /* effectively physically indexed dcache,
974 thus no virtual aliases. */
975 c->dcache.flags |= MIPS_CACHE_PINDEX;
976 break;
977 }
Ralf Baechled1e344e2005-02-04 15:51:26 +0000978 default:
Ralf Baechlebeab3752006-06-19 21:56:25 +0100979 if (c->dcache.waysize > PAGE_SIZE)
980 c->dcache.flags |= MIPS_CACHE_ALIASES;
Ralf Baechled1e344e2005-02-04 15:51:26 +0000981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
983 switch (c->cputype) {
984 case CPU_20KC:
985 /*
986 * Some older 20Kc chips doesn't have the 'VI' bit in
987 * the config register.
988 */
989 c->icache.flags |= MIPS_CACHE_VTAG;
990 break;
991
Pete Popove3ad1c22005-03-01 06:33:16 +0000992 case CPU_AU1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 case CPU_AU1500:
Pete Popove3ad1c22005-03-01 06:33:16 +0000994 case CPU_AU1100:
995 case CPU_AU1550:
996 case CPU_AU1200:
Manuel Lauss237cfee2007-12-06 09:07:55 +0100997 case CPU_AU1210:
998 case CPU_AU1250:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 c->icache.flags |= MIPS_CACHE_IC_F_DC;
1000 break;
1001 }
1002
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001003#ifdef CONFIG_CPU_LOONGSON2
1004 /*
1005 * LOONGSON2 has 4 way icache, but when using indexed cache op,
1006 * one op will act on all 4 ways
1007 */
1008 c->icache.ways = 1;
1009#endif
1010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1012 icache_size >> 10,
Ralf Baechle64bfca52007-10-15 16:35:45 +01001013 cpu_has_vtag_icache ? "VIVT" : "VIPT",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 way_string[c->icache.ways], c->icache.linesz);
1015
Ralf Baechle64bfca52007-10-15 16:35:45 +01001016 printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
1017 dcache_size >> 10, way_string[c->dcache.ways],
1018 (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
1019 (c->dcache.flags & MIPS_CACHE_ALIASES) ?
1020 "cache aliases" : "no aliases",
1021 c->dcache.linesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024/*
1025 * If you even _breathe_ on this function, look at the gcc output and make sure
1026 * it does not pop things on and off the stack for the cache sizing loop that
1027 * executes in KSEG1 space or else you will crash and burn badly. You have
1028 * been warned.
1029 */
Ralf Baechle234fcd12008-03-08 09:56:28 +00001030static int __cpuinit probe_scache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 unsigned long flags, addr, begin, end, pow2;
1033 unsigned int config = read_c0_config();
1034 struct cpuinfo_mips *c = &current_cpu_data;
1035 int tmp;
1036
1037 if (config & CONF_SC)
1038 return 0;
1039
Ralf Baechlee001e522007-07-28 12:45:47 +01001040 begin = (unsigned long) &_stext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 begin &= ~((4 * 1024 * 1024) - 1);
1042 end = begin + (4 * 1024 * 1024);
1043
1044 /*
1045 * This is such a bitch, you'd think they would make it easy to do
1046 * this. Away you daemons of stupidity!
1047 */
1048 local_irq_save(flags);
1049
1050 /* Fill each size-multiple cache line with a valid tag. */
1051 pow2 = (64 * 1024);
1052 for (addr = begin; addr < end; addr = (begin + pow2)) {
1053 unsigned long *p = (unsigned long *) addr;
1054 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1055 pow2 <<= 1;
1056 }
1057
1058 /* Load first line with zero (therefore invalid) tag. */
1059 write_c0_taglo(0);
1060 write_c0_taghi(0);
1061 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1062 cache_op(Index_Store_Tag_I, begin);
1063 cache_op(Index_Store_Tag_D, begin);
1064 cache_op(Index_Store_Tag_SD, begin);
1065
1066 /* Now search for the wrap around point. */
1067 pow2 = (128 * 1024);
1068 tmp = 0;
1069 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1070 cache_op(Index_Load_Tag_SD, addr);
1071 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1072 if (!read_c0_taglo())
1073 break;
1074 pow2 <<= 1;
1075 }
1076 local_irq_restore(flags);
1077 addr -= begin;
1078
1079 scache_size = addr;
1080 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1081 c->scache.ways = 1;
1082 c->dcache.waybit = 0; /* does not matter */
1083
1084 return 1;
1085}
1086
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001087#if defined(CONFIG_CPU_LOONGSON2)
1088static void __init loongson2_sc_init(void)
1089{
1090 struct cpuinfo_mips *c = &current_cpu_data;
1091
1092 scache_size = 512*1024;
1093 c->scache.linesz = 32;
1094 c->scache.ways = 4;
1095 c->scache.waybit = 0;
1096 c->scache.waysize = scache_size / (c->scache.ways);
1097 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1098 pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1099 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1100
1101 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1102}
1103#endif
1104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105extern int r5k_sc_init(void);
1106extern int rm7k_sc_init(void);
Chris Dearman9318c512006-06-20 17:15:20 +01001107extern int mips_sc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Ralf Baechle234fcd12008-03-08 09:56:28 +00001109static void __cpuinit setup_scache(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct cpuinfo_mips *c = &current_cpu_data;
1112 unsigned int config = read_c0_config();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int sc_present = 0;
1114
1115 /*
1116 * Do the probing thing on R4000SC and R4400SC processors. Other
1117 * processors don't have a S-cache that would be relevant to the
Joe Perches603e82e2008-02-03 16:54:53 +02001118 * Linux memory management.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 */
1120 switch (c->cputype) {
1121 case CPU_R4000SC:
1122 case CPU_R4000MC:
1123 case CPU_R4400SC:
1124 case CPU_R4400MC:
Thiemo Seuferba5187d2005-04-25 16:36:23 +00001125 sc_present = run_uncached(probe_scache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (sc_present)
1127 c->options |= MIPS_CPU_CACHE_CDEX_S;
1128 break;
1129
1130 case CPU_R10000:
1131 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -04001132 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1134 c->scache.linesz = 64 << ((config >> 13) & 1);
1135 c->scache.ways = 2;
1136 c->scache.waybit= 0;
1137 sc_present = 1;
1138 break;
1139
1140 case CPU_R5000:
1141 case CPU_NEVADA:
1142#ifdef CONFIG_R5000_CPU_SCACHE
1143 r5k_sc_init();
1144#endif
1145 return;
1146
1147 case CPU_RM7000:
1148 case CPU_RM9000:
1149#ifdef CONFIG_RM7000_CPU_SCACHE
1150 rm7k_sc_init();
1151#endif
1152 return;
1153
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001154#if defined(CONFIG_CPU_LOONGSON2)
1155 case CPU_LOONGSON2:
1156 loongson2_sc_init();
1157 return;
1158#endif
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 default:
Chris Dearman9318c512006-06-20 17:15:20 +01001161 if (c->isa_level == MIPS_CPU_ISA_M32R1 ||
1162 c->isa_level == MIPS_CPU_ISA_M32R2 ||
1163 c->isa_level == MIPS_CPU_ISA_M64R1 ||
1164 c->isa_level == MIPS_CPU_ISA_M64R2) {
1165#ifdef CONFIG_MIPS_CPU_SCACHE
1166 if (mips_sc_init ()) {
1167 scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
1168 printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
1169 scache_size >> 10,
1170 way_string[c->scache.ways], c->scache.linesz);
1171 }
1172#else
1173 if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1174 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1175#endif
1176 return;
1177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 sc_present = 0;
1179 }
1180
1181 if (!sc_present)
1182 return;
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* compute a couple of other cache variables */
1185 c->scache.waysize = scache_size / c->scache.ways;
1186
1187 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1188
1189 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1190 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1191
Ralf Baechlefc5d2d22006-07-06 13:04:01 +01001192 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001195void au1x00_fixup_config_od(void)
1196{
1197 /*
1198 * c0_config.od (bit 19) was write only (and read as 0)
1199 * on the early revisions of Alchemy SOCs. It disables the bus
1200 * transaction overlapping and needs to be set to fix various errata.
1201 */
1202 switch (read_c0_prid()) {
1203 case 0x00030100: /* Au1000 DA */
1204 case 0x00030201: /* Au1000 HA */
1205 case 0x00030202: /* Au1000 HB */
1206 case 0x01030200: /* Au1500 AB */
1207 /*
1208 * Au1100 errata actually keeps silence about this bit, so we set it
1209 * just in case for those revisions that require it to be set according
1210 * to arch/mips/au1000/common/cputable.c
1211 */
1212 case 0x02030200: /* Au1100 AB */
1213 case 0x02030201: /* Au1100 BA */
1214 case 0x02030202: /* Au1100 BC */
1215 set_c0_config(1 << 19);
1216 break;
1217 }
1218}
1219
Chris Dearman35133692007-09-19 00:58:24 +01001220static int __cpuinitdata cca = -1;
1221
1222static int __init cca_setup(char *str)
1223{
1224 get_option(&str, &cca);
1225
1226 return 1;
1227}
1228
1229__setup("cca=", cca_setup);
1230
Ralf Baechle234fcd12008-03-08 09:56:28 +00001231static void __cpuinit coherency_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Chris Dearman35133692007-09-19 00:58:24 +01001233 if (cca < 0 || cca > 7)
1234 cca = read_c0_config() & CONF_CM_CMASK;
1235 _page_cachable_default = cca << _CACHE_SHIFT;
1236
1237 pr_debug("Using cache attribute %d\n", cca);
1238 change_c0_config(CONF_CM_CMASK, cca);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 /*
1241 * c0_status.cu=0 specifies that updates by the sc instruction use
1242 * the coherency mode specified by the TLB; 1 means cachable
1243 * coherent update on write will be used. Not all processors have
1244 * this bit and; some wire it to zero, others like Toshiba had the
1245 * silly idea of putting something else there ...
1246 */
Ralf Baechle10cc3522007-10-11 23:46:15 +01001247 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 case CPU_R4000PC:
1249 case CPU_R4000SC:
1250 case CPU_R4000MC:
1251 case CPU_R4400PC:
1252 case CPU_R4400SC:
1253 case CPU_R4400MC:
1254 clear_c0_config(CONF_CU);
1255 break;
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001256 /*
Ralf Baechledf586d52006-08-01 23:42:30 +01001257 * We need to catch the early Alchemy SOCs with
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001258 * the write-only co_config.od bit and set it back to one...
1259 */
1260 case CPU_AU1000: /* rev. DA, HA, HB */
1261 case CPU_AU1100: /* rev. AB, BA, BC ?? */
1262 case CPU_AU1500: /* rev. AB */
1263 au1x00_fixup_config_od();
1264 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266}
1267
Ralf Baechle234fcd12008-03-08 09:56:28 +00001268void __cpuinit r4k_cache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269{
1270 extern void build_clear_page(void);
1271 extern void build_copy_page(void);
Ralf Baechle641e97f2007-10-11 23:46:05 +01001272 extern char __weak except_vec2_generic;
1273 extern char __weak except_vec2_sb1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct cpuinfo_mips *c = &current_cpu_data;
1275
Ralf Baechle641e97f2007-10-11 23:46:05 +01001276 switch (c->cputype) {
1277 case CPU_SB1:
1278 case CPU_SB1A:
1279 set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1280 break;
1281
1282 default:
1283 set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1284 break;
1285 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 probe_pcache();
1288 setup_scache();
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 r4k_blast_dcache_page_setup();
1291 r4k_blast_dcache_page_indexed_setup();
1292 r4k_blast_dcache_setup();
1293 r4k_blast_icache_page_setup();
1294 r4k_blast_icache_page_indexed_setup();
1295 r4k_blast_icache_setup();
1296 r4k_blast_scache_page_setup();
1297 r4k_blast_scache_page_indexed_setup();
1298 r4k_blast_scache_setup();
1299
1300 /*
1301 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1302 * This code supports virtually indexed processors and will be
1303 * unnecessarily inefficient on physically indexed processors.
1304 */
Chris Dearman73f40352006-06-20 18:06:52 +01001305 if (c->dcache.linesz)
1306 shm_align_mask = max_t( unsigned long,
1307 c->dcache.sets * c->dcache.linesz - 1,
1308 PAGE_SIZE - 1);
1309 else
1310 shm_align_mask = PAGE_SIZE-1;
Ralf Baechle9c5a3d72008-04-05 15:13:23 +01001311
1312 __flush_cache_vmap = r4k__flush_cache_vmap;
1313 __flush_cache_vunmap = r4k__flush_cache_vunmap;
1314
Ralf Baechledb813fe2007-09-27 18:26:43 +01001315 flush_cache_all = cache_noop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 __flush_cache_all = r4k___flush_cache_all;
1317 flush_cache_mm = r4k_flush_cache_mm;
1318 flush_cache_page = r4k_flush_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 flush_cache_range = r4k_flush_cache_range;
1320
1321 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1322 flush_icache_all = r4k_flush_icache_all;
Ralf Baechle7e3bfc72006-04-05 20:42:04 +01001323 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 flush_data_cache_page = r4k_flush_data_cache_page;
1325 flush_icache_range = r4k_flush_icache_range;
1326
1327#ifdef CONFIG_DMA_NONCOHERENT
1328 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1329 _dma_cache_wback = r4k_dma_cache_wback_inv;
1330 _dma_cache_inv = r4k_dma_cache_inv;
1331#endif
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 build_clear_page();
1334 build_copy_page();
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001335 local_r4k___flush_cache_all(NULL);
1336 coherency_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}