blob: 6806d58211b260931e5dcacfc527b966e0e13c6e [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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/init.h>
Ralf Baechledb813fe2007-09-27 18:26:43 +010011#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/kernel.h>
Ralf Baechle641e97f2007-10-11 23:46:05 +010013#include <linux/linkage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/sched.h>
15#include <linux/mm.h>
16#include <linux/bitops.h>
17
18#include <asm/bcache.h>
19#include <asm/bootinfo.h>
Ralf Baechleec74e362005-07-13 11:48:45 +000020#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/cacheops.h>
22#include <asm/cpu.h>
23#include <asm/cpu-features.h>
24#include <asm/io.h>
25#include <asm/page.h>
26#include <asm/pgtable.h>
27#include <asm/r4kcache.h>
Ralf Baechlee001e522007-07-28 12:45:47 +010028#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/system.h>
30#include <asm/mmu_context.h>
31#include <asm/war.h>
Thiemo Seuferba5187d2005-04-25 16:36:23 +000032#include <asm/cacheflush.h> /* for run_uncached() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Ralf Baechle7f3f1d02006-05-12 13:20:06 +010034
35/*
36 * Special Variant of smp_call_function for use by cache functions:
37 *
38 * o No return value
39 * o collapses to normal function call on UP kernels
40 * o collapses to normal function call on systems with a single shared
41 * primary cache.
42 */
43static inline void r4k_on_each_cpu(void (*func) (void *info), void *info,
44 int retry, int wait)
45{
46 preempt_disable();
47
48#if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
49 smp_call_function(func, info, retry, wait);
50#endif
51 func(info);
52 preempt_enable();
53}
54
Ralf Baechleec74e362005-07-13 11:48:45 +000055/*
56 * Must die.
57 */
58static unsigned long icache_size __read_mostly;
59static unsigned long dcache_size __read_mostly;
60static unsigned long scache_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * Dummy cache handling routines for machines without boardcaches
64 */
Chris Dearman73f40352006-06-20 18:06:52 +010065static void cache_noop(void) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67static struct bcache_ops no_sc_ops = {
Chris Dearman73f40352006-06-20 18:06:52 +010068 .bc_enable = (void *)cache_noop,
69 .bc_disable = (void *)cache_noop,
70 .bc_wback_inv = (void *)cache_noop,
71 .bc_inv = (void *)cache_noop
Linus Torvalds1da177e2005-04-16 15:20:36 -070072};
73
74struct bcache_ops *bcops = &no_sc_ops;
75
Thiemo Seufer330cfe02005-09-01 18:33:58 +000076#define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
77#define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#define R4600_HIT_CACHEOP_WAR_IMPL \
80do { \
81 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
82 *(volatile unsigned long *)CKSEG1; \
83 if (R4600_V1_HIT_CACHEOP_WAR) \
84 __asm__ __volatile__("nop;nop;nop;nop"); \
85} while (0)
86
87static void (*r4k_blast_dcache_page)(unsigned long addr);
88
89static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
90{
91 R4600_HIT_CACHEOP_WAR_IMPL;
92 blast_dcache32_page(addr);
93}
94
Ralf Baechlea00f6312006-08-01 23:39:42 +010095static void __init r4k_blast_dcache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 unsigned long dc_lsize = cpu_dcache_line_size();
98
Chris Dearman73f40352006-06-20 18:06:52 +010099 if (dc_lsize == 0)
100 r4k_blast_dcache_page = (void *)cache_noop;
101 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 r4k_blast_dcache_page = blast_dcache16_page;
103 else if (dc_lsize == 32)
104 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
105}
106
107static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
108
Ralf Baechlea00f6312006-08-01 23:39:42 +0100109static void __init r4k_blast_dcache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 unsigned long dc_lsize = cpu_dcache_line_size();
112
Chris Dearman73f40352006-06-20 18:06:52 +0100113 if (dc_lsize == 0)
114 r4k_blast_dcache_page_indexed = (void *)cache_noop;
115 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
117 else if (dc_lsize == 32)
118 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
119}
120
121static void (* r4k_blast_dcache)(void);
122
Ralf Baechlea00f6312006-08-01 23:39:42 +0100123static void __init r4k_blast_dcache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 unsigned long dc_lsize = cpu_dcache_line_size();
126
Chris Dearman73f40352006-06-20 18:06:52 +0100127 if (dc_lsize == 0)
128 r4k_blast_dcache = (void *)cache_noop;
129 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 r4k_blast_dcache = blast_dcache16;
131 else if (dc_lsize == 32)
132 r4k_blast_dcache = blast_dcache32;
133}
134
135/* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
136#define JUMP_TO_ALIGN(order) \
137 __asm__ __volatile__( \
138 "b\t1f\n\t" \
139 ".align\t" #order "\n\t" \
140 "1:\n\t" \
141 )
142#define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
143#define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
144
145static inline void blast_r4600_v1_icache32(void)
146{
147 unsigned long flags;
148
149 local_irq_save(flags);
150 blast_icache32();
151 local_irq_restore(flags);
152}
153
154static inline void tx49_blast_icache32(void)
155{
156 unsigned long start = INDEX_BASE;
157 unsigned long end = start + current_cpu_data.icache.waysize;
158 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
159 unsigned long ws_end = current_cpu_data.icache.ways <<
160 current_cpu_data.icache.waybit;
161 unsigned long ws, addr;
162
163 CACHE32_UNROLL32_ALIGN2;
164 /* I'm in even chunk. blast odd chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700165 for (ws = 0; ws < ws_end; ws += ws_inc)
166 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100167 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 CACHE32_UNROLL32_ALIGN;
169 /* I'm in odd chunk. blast even chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700170 for (ws = 0; ws < ws_end; ws += ws_inc)
171 for (addr = start; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100172 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
175static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
176{
177 unsigned long flags;
178
179 local_irq_save(flags);
180 blast_icache32_page_indexed(page);
181 local_irq_restore(flags);
182}
183
184static inline void tx49_blast_icache32_page_indexed(unsigned long page)
185{
Atsushi Nemoto67a3f6d2006-04-04 17:34:14 +0900186 unsigned long indexmask = current_cpu_data.icache.waysize - 1;
187 unsigned long start = INDEX_BASE + (page & indexmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 unsigned long end = start + PAGE_SIZE;
189 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
190 unsigned long ws_end = current_cpu_data.icache.ways <<
191 current_cpu_data.icache.waybit;
192 unsigned long ws, addr;
193
194 CACHE32_UNROLL32_ALIGN2;
195 /* I'm in even chunk. blast odd chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700196 for (ws = 0; ws < ws_end; ws += ws_inc)
197 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100198 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 CACHE32_UNROLL32_ALIGN;
200 /* I'm in odd chunk. blast even chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700201 for (ws = 0; ws < ws_end; ws += ws_inc)
202 for (addr = start; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100203 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206static void (* r4k_blast_icache_page)(unsigned long addr);
207
Ralf Baechlea00f6312006-08-01 23:39:42 +0100208static void __init r4k_blast_icache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 unsigned long ic_lsize = cpu_icache_line_size();
211
Chris Dearman73f40352006-06-20 18:06:52 +0100212 if (ic_lsize == 0)
213 r4k_blast_icache_page = (void *)cache_noop;
214 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 r4k_blast_icache_page = blast_icache16_page;
216 else if (ic_lsize == 32)
217 r4k_blast_icache_page = blast_icache32_page;
218 else if (ic_lsize == 64)
219 r4k_blast_icache_page = blast_icache64_page;
220}
221
222
223static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
224
Ralf Baechlea00f6312006-08-01 23:39:42 +0100225static void __init r4k_blast_icache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 unsigned long ic_lsize = cpu_icache_line_size();
228
Chris Dearman73f40352006-06-20 18:06:52 +0100229 if (ic_lsize == 0)
230 r4k_blast_icache_page_indexed = (void *)cache_noop;
231 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
233 else if (ic_lsize == 32) {
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000234 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 r4k_blast_icache_page_indexed =
236 blast_icache32_r4600_v1_page_indexed;
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000237 else if (TX49XX_ICACHE_INDEX_INV_WAR)
238 r4k_blast_icache_page_indexed =
239 tx49_blast_icache32_page_indexed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 else
241 r4k_blast_icache_page_indexed =
242 blast_icache32_page_indexed;
243 } else if (ic_lsize == 64)
244 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
245}
246
247static void (* r4k_blast_icache)(void);
248
Ralf Baechlea00f6312006-08-01 23:39:42 +0100249static void __init r4k_blast_icache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 unsigned long ic_lsize = cpu_icache_line_size();
252
Chris Dearman73f40352006-06-20 18:06:52 +0100253 if (ic_lsize == 0)
254 r4k_blast_icache = (void *)cache_noop;
255 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 r4k_blast_icache = blast_icache16;
257 else if (ic_lsize == 32) {
258 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
259 r4k_blast_icache = blast_r4600_v1_icache32;
260 else if (TX49XX_ICACHE_INDEX_INV_WAR)
261 r4k_blast_icache = tx49_blast_icache32;
262 else
263 r4k_blast_icache = blast_icache32;
264 } else if (ic_lsize == 64)
265 r4k_blast_icache = blast_icache64;
266}
267
268static void (* r4k_blast_scache_page)(unsigned long addr);
269
Ralf Baechlea00f6312006-08-01 23:39:42 +0100270static void __init r4k_blast_scache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 unsigned long sc_lsize = cpu_scache_line_size();
273
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000274 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100275 r4k_blast_scache_page = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000276 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 r4k_blast_scache_page = blast_scache16_page;
278 else if (sc_lsize == 32)
279 r4k_blast_scache_page = blast_scache32_page;
280 else if (sc_lsize == 64)
281 r4k_blast_scache_page = blast_scache64_page;
282 else if (sc_lsize == 128)
283 r4k_blast_scache_page = blast_scache128_page;
284}
285
286static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
287
Ralf Baechlea00f6312006-08-01 23:39:42 +0100288static void __init r4k_blast_scache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 unsigned long sc_lsize = cpu_scache_line_size();
291
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000292 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100293 r4k_blast_scache_page_indexed = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000294 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
296 else if (sc_lsize == 32)
297 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
298 else if (sc_lsize == 64)
299 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
300 else if (sc_lsize == 128)
301 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
302}
303
304static void (* r4k_blast_scache)(void);
305
Ralf Baechlea00f6312006-08-01 23:39:42 +0100306static void __init r4k_blast_scache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 unsigned long sc_lsize = cpu_scache_line_size();
309
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000310 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100311 r4k_blast_scache = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000312 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 r4k_blast_scache = blast_scache16;
314 else if (sc_lsize == 32)
315 r4k_blast_scache = blast_scache32;
316 else if (sc_lsize == 64)
317 r4k_blast_scache = blast_scache64;
318 else if (sc_lsize == 128)
319 r4k_blast_scache = blast_scache128;
320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static inline void local_r4k___flush_cache_all(void * args)
323{
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800324#if defined(CONFIG_CPU_LOONGSON2)
325 r4k_blast_scache();
326 return;
327#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 r4k_blast_dcache();
329 r4k_blast_icache();
330
Ralf Baechle10cc3522007-10-11 23:46:15 +0100331 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 case CPU_R4000SC:
333 case CPU_R4000MC:
334 case CPU_R4400SC:
335 case CPU_R4400MC:
336 case CPU_R10000:
337 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400338 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 r4k_blast_scache();
340 }
341}
342
343static void r4k___flush_cache_all(void)
344{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100345 r4k_on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346}
347
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100348static inline int has_valid_asid(const struct mm_struct *mm)
349{
350#if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
351 int i;
352
353 for_each_online_cpu(i)
354 if (cpu_context(i, mm))
355 return 1;
356
357 return 0;
358#else
359 return cpu_context(smp_processor_id(), mm);
360#endif
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363static inline void local_r4k_flush_cache_range(void * args)
364{
365 struct vm_area_struct *vma = args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100367 if (!(has_valid_asid(vma->vm_mm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return;
369
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900370 r4k_blast_dcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
373static void r4k_flush_cache_range(struct vm_area_struct *vma,
374 unsigned long start, unsigned long end)
375{
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900376 if (!cpu_has_dc_aliases)
377 return;
378
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100379 r4k_on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
382static inline void local_r4k_flush_cache_mm(void * args)
383{
384 struct mm_struct *mm = args;
385
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100386 if (!has_valid_asid(mm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return;
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
390 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
391 * only flush the primary caches but R10000 and R12000 behave sane ...
Ralf Baechle617667b2006-11-30 01:14:48 +0000392 * R4000SC and R4400SC indexed S-cache ops also invalidate primary
393 * caches, so we can bail out early.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 */
Ralf Baechle10cc3522007-10-11 23:46:15 +0100395 if (current_cpu_type() == CPU_R4000SC ||
396 current_cpu_type() == CPU_R4000MC ||
397 current_cpu_type() == CPU_R4400SC ||
398 current_cpu_type() == CPU_R4400MC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 r4k_blast_scache();
Ralf Baechle617667b2006-11-30 01:14:48 +0000400 return;
401 }
402
403 r4k_blast_dcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
405
406static void r4k_flush_cache_mm(struct mm_struct *mm)
407{
408 if (!cpu_has_dc_aliases)
409 return;
410
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100411 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414struct flush_cache_page_args {
415 struct vm_area_struct *vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100416 unsigned long addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900417 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418};
419
420static inline void local_r4k_flush_cache_page(void *args)
421{
422 struct flush_cache_page_args *fcp_args = args;
423 struct vm_area_struct *vma = fcp_args->vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100424 unsigned long addr = fcp_args->addr;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100425 struct page *page = pfn_to_page(fcp_args->pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 int exec = vma->vm_flags & VM_EXEC;
427 struct mm_struct *mm = vma->vm_mm;
428 pgd_t *pgdp;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000429 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 pmd_t *pmdp;
431 pte_t *ptep;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100432 void *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Ralf Baechle79acf832005-02-10 13:54:37 +0000434 /*
435 * If ownes no valid ASID yet, cannot possibly have gotten
436 * this page into the cache.
437 */
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100438 if (!has_valid_asid(mm))
Ralf Baechle79acf832005-02-10 13:54:37 +0000439 return;
440
Ralf Baechle6ec25802005-10-12 00:02:34 +0100441 addr &= PAGE_MASK;
442 pgdp = pgd_offset(mm, addr);
443 pudp = pud_offset(pgdp, addr);
444 pmdp = pmd_offset(pudp, addr);
445 ptep = pte_offset(pmdp, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /*
448 * If the page isn't marked valid, the page cannot possibly be
449 * in the cache.
450 */
451 if (!(pte_val(*ptep) & _PAGE_PRESENT))
452 return;
453
Ralf Baechledb813fe2007-09-27 18:26:43 +0100454 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
455 vaddr = NULL;
456 else {
457 /*
458 * Use kmap_coherent or kmap_atomic to do flushes for
459 * another ASID than the current one.
460 */
461 if (cpu_has_dc_aliases)
462 vaddr = kmap_coherent(page, addr);
463 else
464 vaddr = kmap_atomic(page, KM_USER0);
465 addr = (unsigned long)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 }
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100469 r4k_blast_dcache_page(addr);
470 if (exec && !cpu_icache_snoops_remote_store)
471 r4k_blast_scache_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 if (exec) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100474 if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 int cpu = smp_processor_id();
476
Thiemo Seufer26a51b22005-02-19 13:32:02 +0000477 if (cpu_context(cpu, mm) != 0)
478 drop_mmu_context(mm, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 } else
Ralf Baechledb813fe2007-09-27 18:26:43 +0100480 r4k_blast_icache_page(addr);
481 }
482
483 if (vaddr) {
484 if (cpu_has_dc_aliases)
485 kunmap_coherent();
486 else
487 kunmap_atomic(vaddr, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489}
490
Ralf Baechle6ec25802005-10-12 00:02:34 +0100491static void r4k_flush_cache_page(struct vm_area_struct *vma,
492 unsigned long addr, unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
494 struct flush_cache_page_args args;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 args.vma = vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100497 args.addr = addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900498 args.pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100500 r4k_on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
503static inline void local_r4k_flush_data_cache_page(void * addr)
504{
505 r4k_blast_dcache_page((unsigned long) addr);
506}
507
508static void r4k_flush_data_cache_page(unsigned long addr)
509{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100510 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513struct flush_icache_range_args {
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900514 unsigned long start;
515 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516};
517
518static inline void local_r4k_flush_icache_range(void *args)
519{
520 struct flush_icache_range_args *fir_args = args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 unsigned long start = fir_args->start;
522 unsigned long end = fir_args->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 if (!cpu_has_ic_fills_f_dc) {
Chris Dearman73f40352006-06-20 18:06:52 +0100525 if (end - start >= dcache_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 r4k_blast_dcache();
527 } else {
Thiemo Seufer10a3dab2005-09-09 20:26:54 +0000528 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900529 protected_blast_dcache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000532 if (!cpu_icache_snoops_remote_store && scache_size) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900533 if (end - start > scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900535 else
536 protected_blast_scache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 }
539
540 if (end - start > icache_size)
541 r4k_blast_icache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900542 else
543 protected_blast_icache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900546static void r4k_flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
548 struct flush_icache_range_args args;
549
550 args.start = start;
551 args.end = end;
552
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100553 r4k_on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
Ralf Baechlecc61c1f2005-07-12 18:35:38 +0000554 instruction_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#ifdef CONFIG_DMA_NONCOHERENT
558
559static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
560{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 /* Catch bad driver code */
562 BUG_ON(size == 0);
563
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100564 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900565 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900567 else
568 blast_scache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 return;
570 }
571
572 /*
573 * Either no secondary cache or the available caches don't have the
574 * subset property so we have to flush the primary caches
575 * explicitly
576 */
577 if (size >= dcache_size) {
578 r4k_blast_dcache();
579 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900581 blast_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
584 bc_wback_inv(addr, size);
585}
586
587static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
588{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Catch bad driver code */
590 BUG_ON(size == 0);
591
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100592 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900593 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900595 else
596 blast_scache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return;
598 }
599
600 if (size >= dcache_size) {
601 r4k_blast_dcache();
602 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900604 blast_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606
607 bc_inv(addr, size);
608}
609#endif /* CONFIG_DMA_NONCOHERENT */
610
611/*
612 * While we're protected against bad userland addresses we don't care
613 * very much about what happens in that case. Usually a segmentation
614 * fault will dump the process later on anyway ...
615 */
616static void local_r4k_flush_cache_sigtramp(void * arg)
617{
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000618 unsigned long ic_lsize = cpu_icache_line_size();
619 unsigned long dc_lsize = cpu_dcache_line_size();
620 unsigned long sc_lsize = cpu_scache_line_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned long addr = (unsigned long) arg;
622
623 R4600_HIT_CACHEOP_WAR_IMPL;
Chris Dearman73f40352006-06-20 18:06:52 +0100624 if (dc_lsize)
625 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000626 if (!cpu_icache_snoops_remote_store && scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
Chris Dearman73f40352006-06-20 18:06:52 +0100628 if (ic_lsize)
629 protected_flush_icache_line(addr & ~(ic_lsize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (MIPS4K_ICACHE_REFILL_WAR) {
631 __asm__ __volatile__ (
632 ".set push\n\t"
633 ".set noat\n\t"
634 ".set mips3\n\t"
Ralf Baechle875d43e2005-09-03 15:56:16 -0700635#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 "la $at,1f\n\t"
637#endif
Ralf Baechle875d43e2005-09-03 15:56:16 -0700638#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 "dla $at,1f\n\t"
640#endif
641 "cache %0,($at)\n\t"
642 "nop; nop; nop\n"
643 "1:\n\t"
644 ".set pop"
645 :
646 : "i" (Hit_Invalidate_I));
647 }
648 if (MIPS_CACHE_SYNC_WAR)
649 __asm__ __volatile__ ("sync");
650}
651
652static void r4k_flush_cache_sigtramp(unsigned long addr)
653{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100654 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657static void r4k_flush_icache_all(void)
658{
659 if (cpu_has_vtag_icache)
660 r4k_blast_icache();
661}
662
663static inline void rm7k_erratum31(void)
664{
665 const unsigned long ic_lsize = 32;
666 unsigned long addr;
667
668 /* RM7000 erratum #31. The icache is screwed at startup. */
669 write_c0_taglo(0);
670 write_c0_taghi(0);
671
672 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
673 __asm__ __volatile__ (
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000674 ".set push\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 ".set noreorder\n\t"
676 ".set mips3\n\t"
677 "cache\t%1, 0(%0)\n\t"
678 "cache\t%1, 0x1000(%0)\n\t"
679 "cache\t%1, 0x2000(%0)\n\t"
680 "cache\t%1, 0x3000(%0)\n\t"
681 "cache\t%2, 0(%0)\n\t"
682 "cache\t%2, 0x1000(%0)\n\t"
683 "cache\t%2, 0x2000(%0)\n\t"
684 "cache\t%2, 0x3000(%0)\n\t"
685 "cache\t%1, 0(%0)\n\t"
686 "cache\t%1, 0x1000(%0)\n\t"
687 "cache\t%1, 0x2000(%0)\n\t"
688 "cache\t%1, 0x3000(%0)\n\t"
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000689 ".set pop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 :
691 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
692 }
693}
694
695static char *way_string[] __initdata = { NULL, "direct mapped", "2-way",
696 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
697};
698
699static void __init probe_pcache(void)
700{
701 struct cpuinfo_mips *c = &current_cpu_data;
702 unsigned int config = read_c0_config();
703 unsigned int prid = read_c0_prid();
704 unsigned long config1;
705 unsigned int lsize;
706
707 switch (c->cputype) {
708 case CPU_R4600: /* QED style two way caches? */
709 case CPU_R4700:
710 case CPU_R5000:
711 case CPU_NEVADA:
712 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
713 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
714 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900715 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
718 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
719 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900720 c->dcache.waybit= __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 c->options |= MIPS_CPU_CACHE_CDEX_P;
723 break;
724
725 case CPU_R5432:
726 case CPU_R5500:
727 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
728 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
729 c->icache.ways = 2;
730 c->icache.waybit= 0;
731
732 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
733 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
734 c->dcache.ways = 2;
735 c->dcache.waybit = 0;
736
737 c->options |= MIPS_CPU_CACHE_CDEX_P;
738 break;
739
740 case CPU_TX49XX:
741 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
742 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
743 c->icache.ways = 4;
744 c->icache.waybit= 0;
745
746 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
747 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
748 c->dcache.ways = 4;
749 c->dcache.waybit = 0;
750
751 c->options |= MIPS_CPU_CACHE_CDEX_P;
Atsushi Nemotode862b42006-03-17 12:59:22 +0900752 c->options |= MIPS_CPU_PREFETCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754
755 case CPU_R4000PC:
756 case CPU_R4000SC:
757 case CPU_R4000MC:
758 case CPU_R4400PC:
759 case CPU_R4400SC:
760 case CPU_R4400MC:
761 case CPU_R4300:
762 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
763 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
764 c->icache.ways = 1;
765 c->icache.waybit = 0; /* doesn't matter */
766
767 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
768 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
769 c->dcache.ways = 1;
770 c->dcache.waybit = 0; /* does not matter */
771
772 c->options |= MIPS_CPU_CACHE_CDEX_P;
773 break;
774
775 case CPU_R10000:
776 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400777 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
779 c->icache.linesz = 64;
780 c->icache.ways = 2;
781 c->icache.waybit = 0;
782
783 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
784 c->dcache.linesz = 32;
785 c->dcache.ways = 2;
786 c->dcache.waybit = 0;
787
788 c->options |= MIPS_CPU_PREFETCH;
789 break;
790
791 case CPU_VR4133:
Yoichi Yuasa2874fe52006-07-08 00:42:12 +0900792 write_c0_config(config & ~VR41_CONF_P4K);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 case CPU_VR4131:
794 /* Workaround for cache instruction bug of VR4131 */
795 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
796 c->processor_id == 0x0c82U) {
Yoichi Yuasa4e8ab362006-07-04 22:59:41 +0900797 config |= 0x00400000U;
798 if (c->processor_id == 0x0c80U)
799 config |= VR41_CONF_BP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 write_c0_config(config);
Yoichi Yuasa1058ecd2006-07-08 00:42:01 +0900801 } else
802 c->options |= MIPS_CPU_CACHE_CDEX_P;
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
805 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
806 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900807 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
810 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
811 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900812 c->dcache.waybit = __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 break;
814
815 case CPU_VR41XX:
816 case CPU_VR4111:
817 case CPU_VR4121:
818 case CPU_VR4122:
819 case CPU_VR4181:
820 case CPU_VR4181A:
821 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
822 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
823 c->icache.ways = 1;
824 c->icache.waybit = 0; /* doesn't matter */
825
826 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
827 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
828 c->dcache.ways = 1;
829 c->dcache.waybit = 0; /* does not matter */
830
831 c->options |= MIPS_CPU_CACHE_CDEX_P;
832 break;
833
834 case CPU_RM7000:
835 rm7k_erratum31();
836
837 case CPU_RM9000:
838 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
839 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
840 c->icache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900841 c->icache.waybit = __ffs(icache_size / c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
844 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
845 c->dcache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900846 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848#if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
849 c->options |= MIPS_CPU_CACHE_CDEX_P;
850#endif
851 c->options |= MIPS_CPU_PREFETCH;
852 break;
853
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800854 case CPU_LOONGSON2:
855 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
856 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
857 if (prid & 0x3)
858 c->icache.ways = 4;
859 else
860 c->icache.ways = 2;
861 c->icache.waybit = 0;
862
863 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
864 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
865 if (prid & 0x3)
866 c->dcache.ways = 4;
867 else
868 c->dcache.ways = 2;
869 c->dcache.waybit = 0;
870 break;
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 default:
873 if (!(config & MIPS_CONF_M))
874 panic("Don't know how to probe P-caches on this cpu.");
875
876 /*
877 * So we seem to be a MIPS32 or MIPS64 CPU
878 * So let's probe the I-cache ...
879 */
880 config1 = read_c0_config1();
881
882 if ((lsize = ((config1 >> 19) & 7)))
883 c->icache.linesz = 2 << lsize;
884 else
885 c->icache.linesz = lsize;
886 c->icache.sets = 64 << ((config1 >> 22) & 7);
887 c->icache.ways = 1 + ((config1 >> 16) & 7);
888
889 icache_size = c->icache.sets *
890 c->icache.ways *
891 c->icache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900892 c->icache.waybit = __ffs(icache_size/c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 if (config & 0x8) /* VI bit */
895 c->icache.flags |= MIPS_CACHE_VTAG;
896
897 /*
898 * Now probe the MIPS32 / MIPS64 data cache.
899 */
900 c->dcache.flags = 0;
901
902 if ((lsize = ((config1 >> 10) & 7)))
903 c->dcache.linesz = 2 << lsize;
904 else
905 c->dcache.linesz= lsize;
906 c->dcache.sets = 64 << ((config1 >> 13) & 7);
907 c->dcache.ways = 1 + ((config1 >> 7) & 7);
908
909 dcache_size = c->dcache.sets *
910 c->dcache.ways *
911 c->dcache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900912 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 c->options |= MIPS_CPU_PREFETCH;
915 break;
916 }
917
918 /*
919 * Processor configuration sanity check for the R4000SC erratum
920 * #5. With page sizes larger than 32kB there is no possibility
921 * to get a VCE exception anymore so we don't care about this
922 * misconfiguration. The case is rather theoretical anyway;
923 * presumably no vendor is shipping his hardware in the "bad"
924 * configuration.
925 */
926 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
927 !(config & CONF_SC) && c->icache.linesz != 16 &&
928 PAGE_SIZE <= 0x8000)
929 panic("Improper R4000SC processor configuration detected");
930
931 /* compute a couple of other cache variables */
932 c->icache.waysize = icache_size / c->icache.ways;
933 c->dcache.waysize = dcache_size / c->dcache.ways;
934
Chris Dearman73f40352006-06-20 18:06:52 +0100935 c->icache.sets = c->icache.linesz ?
936 icache_size / (c->icache.linesz * c->icache.ways) : 0;
937 c->dcache.sets = c->dcache.linesz ?
938 dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
940 /*
941 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
942 * 2-way virtually indexed so normally would suffer from aliases. So
943 * normally they'd suffer from aliases but magic in the hardware deals
944 * with that for us so we don't need to take care ourselves.
945 */
Ralf Baechled1e344e2005-02-04 15:51:26 +0000946 switch (c->cputype) {
Ralf Baechlea95970f2005-02-07 21:41:32 +0000947 case CPU_20KC:
Ralf Baechle505403b2005-02-07 21:53:39 +0000948 case CPU_25KF:
Ralf Baechle641e97f2007-10-11 23:46:05 +0100949 case CPU_SB1:
950 case CPU_SB1A:
Atsushi Nemotode628932006-03-13 18:23:03 +0900951 c->dcache.flags |= MIPS_CACHE_PINDEX;
Ralf Baechle641e97f2007-10-11 23:46:05 +0100952 break;
953
Ralf Baechled1e344e2005-02-04 15:51:26 +0000954 case CPU_R10000:
955 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400956 case CPU_R14000:
Ralf Baechled1e344e2005-02-04 15:51:26 +0000957 break;
Ralf Baechle641e97f2007-10-11 23:46:05 +0100958
Ralf Baechled1e344e2005-02-04 15:51:26 +0000959 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +0100960 case CPU_34K:
Ralf Baechle2e78ae32006-06-23 18:48:21 +0100961 case CPU_74K:
Ralf Baechlebeab3752006-06-19 21:56:25 +0100962 if ((read_c0_config7() & (1 << 16))) {
963 /* effectively physically indexed dcache,
964 thus no virtual aliases. */
965 c->dcache.flags |= MIPS_CACHE_PINDEX;
966 break;
967 }
Ralf Baechled1e344e2005-02-04 15:51:26 +0000968 default:
Ralf Baechlebeab3752006-06-19 21:56:25 +0100969 if (c->dcache.waysize > PAGE_SIZE)
970 c->dcache.flags |= MIPS_CACHE_ALIASES;
Ralf Baechled1e344e2005-02-04 15:51:26 +0000971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 switch (c->cputype) {
974 case CPU_20KC:
975 /*
976 * Some older 20Kc chips doesn't have the 'VI' bit in
977 * the config register.
978 */
979 c->icache.flags |= MIPS_CACHE_VTAG;
980 break;
981
Pete Popove3ad1c22005-03-01 06:33:16 +0000982 case CPU_AU1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 case CPU_AU1500:
Pete Popove3ad1c22005-03-01 06:33:16 +0000984 case CPU_AU1100:
985 case CPU_AU1550:
986 case CPU_AU1200:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 c->icache.flags |= MIPS_CACHE_IC_F_DC;
988 break;
989 }
990
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800991#ifdef CONFIG_CPU_LOONGSON2
992 /*
993 * LOONGSON2 has 4 way icache, but when using indexed cache op,
994 * one op will act on all 4 ways
995 */
996 c->icache.ways = 1;
997#endif
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1000 icache_size >> 10,
Ralf Baechle64bfca52007-10-15 16:35:45 +01001001 cpu_has_vtag_icache ? "VIVT" : "VIPT",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 way_string[c->icache.ways], c->icache.linesz);
1003
Ralf Baechle64bfca52007-10-15 16:35:45 +01001004 printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
1005 dcache_size >> 10, way_string[c->dcache.ways],
1006 (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
1007 (c->dcache.flags & MIPS_CACHE_ALIASES) ?
1008 "cache aliases" : "no aliases",
1009 c->dcache.linesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012/*
1013 * If you even _breathe_ on this function, look at the gcc output and make sure
1014 * it does not pop things on and off the stack for the cache sizing loop that
1015 * executes in KSEG1 space or else you will crash and burn badly. You have
1016 * been warned.
1017 */
1018static int __init probe_scache(void)
1019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 unsigned long flags, addr, begin, end, pow2;
1021 unsigned int config = read_c0_config();
1022 struct cpuinfo_mips *c = &current_cpu_data;
1023 int tmp;
1024
1025 if (config & CONF_SC)
1026 return 0;
1027
Ralf Baechlee001e522007-07-28 12:45:47 +01001028 begin = (unsigned long) &_stext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 begin &= ~((4 * 1024 * 1024) - 1);
1030 end = begin + (4 * 1024 * 1024);
1031
1032 /*
1033 * This is such a bitch, you'd think they would make it easy to do
1034 * this. Away you daemons of stupidity!
1035 */
1036 local_irq_save(flags);
1037
1038 /* Fill each size-multiple cache line with a valid tag. */
1039 pow2 = (64 * 1024);
1040 for (addr = begin; addr < end; addr = (begin + pow2)) {
1041 unsigned long *p = (unsigned long *) addr;
1042 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1043 pow2 <<= 1;
1044 }
1045
1046 /* Load first line with zero (therefore invalid) tag. */
1047 write_c0_taglo(0);
1048 write_c0_taghi(0);
1049 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1050 cache_op(Index_Store_Tag_I, begin);
1051 cache_op(Index_Store_Tag_D, begin);
1052 cache_op(Index_Store_Tag_SD, begin);
1053
1054 /* Now search for the wrap around point. */
1055 pow2 = (128 * 1024);
1056 tmp = 0;
1057 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1058 cache_op(Index_Load_Tag_SD, addr);
1059 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1060 if (!read_c0_taglo())
1061 break;
1062 pow2 <<= 1;
1063 }
1064 local_irq_restore(flags);
1065 addr -= begin;
1066
1067 scache_size = addr;
1068 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1069 c->scache.ways = 1;
1070 c->dcache.waybit = 0; /* does not matter */
1071
1072 return 1;
1073}
1074
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001075#if defined(CONFIG_CPU_LOONGSON2)
1076static void __init loongson2_sc_init(void)
1077{
1078 struct cpuinfo_mips *c = &current_cpu_data;
1079
1080 scache_size = 512*1024;
1081 c->scache.linesz = 32;
1082 c->scache.ways = 4;
1083 c->scache.waybit = 0;
1084 c->scache.waysize = scache_size / (c->scache.ways);
1085 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1086 pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1087 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1088
1089 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1090}
1091#endif
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093extern int r5k_sc_init(void);
1094extern int rm7k_sc_init(void);
Chris Dearman9318c512006-06-20 17:15:20 +01001095extern int mips_sc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097static void __init setup_scache(void)
1098{
1099 struct cpuinfo_mips *c = &current_cpu_data;
1100 unsigned int config = read_c0_config();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 int sc_present = 0;
1102
1103 /*
1104 * Do the probing thing on R4000SC and R4400SC processors. Other
1105 * processors don't have a S-cache that would be relevant to the
1106 * Linux memory managment.
1107 */
1108 switch (c->cputype) {
1109 case CPU_R4000SC:
1110 case CPU_R4000MC:
1111 case CPU_R4400SC:
1112 case CPU_R4400MC:
Thiemo Seuferba5187d2005-04-25 16:36:23 +00001113 sc_present = run_uncached(probe_scache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (sc_present)
1115 c->options |= MIPS_CPU_CACHE_CDEX_S;
1116 break;
1117
1118 case CPU_R10000:
1119 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -04001120 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1122 c->scache.linesz = 64 << ((config >> 13) & 1);
1123 c->scache.ways = 2;
1124 c->scache.waybit= 0;
1125 sc_present = 1;
1126 break;
1127
1128 case CPU_R5000:
1129 case CPU_NEVADA:
1130#ifdef CONFIG_R5000_CPU_SCACHE
1131 r5k_sc_init();
1132#endif
1133 return;
1134
1135 case CPU_RM7000:
1136 case CPU_RM9000:
1137#ifdef CONFIG_RM7000_CPU_SCACHE
1138 rm7k_sc_init();
1139#endif
1140 return;
1141
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001142#if defined(CONFIG_CPU_LOONGSON2)
1143 case CPU_LOONGSON2:
1144 loongson2_sc_init();
1145 return;
1146#endif
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 default:
Chris Dearman9318c512006-06-20 17:15:20 +01001149 if (c->isa_level == MIPS_CPU_ISA_M32R1 ||
1150 c->isa_level == MIPS_CPU_ISA_M32R2 ||
1151 c->isa_level == MIPS_CPU_ISA_M64R1 ||
1152 c->isa_level == MIPS_CPU_ISA_M64R2) {
1153#ifdef CONFIG_MIPS_CPU_SCACHE
1154 if (mips_sc_init ()) {
1155 scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
1156 printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
1157 scache_size >> 10,
1158 way_string[c->scache.ways], c->scache.linesz);
1159 }
1160#else
1161 if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1162 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1163#endif
1164 return;
1165 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 sc_present = 0;
1167 }
1168
1169 if (!sc_present)
1170 return;
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* compute a couple of other cache variables */
1173 c->scache.waysize = scache_size / c->scache.ways;
1174
1175 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1176
1177 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1178 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1179
Ralf Baechlefc5d2d22006-07-06 13:04:01 +01001180 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001183void au1x00_fixup_config_od(void)
1184{
1185 /*
1186 * c0_config.od (bit 19) was write only (and read as 0)
1187 * on the early revisions of Alchemy SOCs. It disables the bus
1188 * transaction overlapping and needs to be set to fix various errata.
1189 */
1190 switch (read_c0_prid()) {
1191 case 0x00030100: /* Au1000 DA */
1192 case 0x00030201: /* Au1000 HA */
1193 case 0x00030202: /* Au1000 HB */
1194 case 0x01030200: /* Au1500 AB */
1195 /*
1196 * Au1100 errata actually keeps silence about this bit, so we set it
1197 * just in case for those revisions that require it to be set according
1198 * to arch/mips/au1000/common/cputable.c
1199 */
1200 case 0x02030200: /* Au1100 AB */
1201 case 0x02030201: /* Au1100 BA */
1202 case 0x02030202: /* Au1100 BC */
1203 set_c0_config(1 << 19);
1204 break;
1205 }
1206}
1207
Ralf Baechlea00f6312006-08-01 23:39:42 +01001208static void __init coherency_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
1211
1212 /*
1213 * c0_status.cu=0 specifies that updates by the sc instruction use
1214 * the coherency mode specified by the TLB; 1 means cachable
1215 * coherent update on write will be used. Not all processors have
1216 * this bit and; some wire it to zero, others like Toshiba had the
1217 * silly idea of putting something else there ...
1218 */
Ralf Baechle10cc3522007-10-11 23:46:15 +01001219 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case CPU_R4000PC:
1221 case CPU_R4000SC:
1222 case CPU_R4000MC:
1223 case CPU_R4400PC:
1224 case CPU_R4400SC:
1225 case CPU_R4400MC:
1226 clear_c0_config(CONF_CU);
1227 break;
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001228 /*
Ralf Baechledf586d52006-08-01 23:42:30 +01001229 * We need to catch the early Alchemy SOCs with
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001230 * the write-only co_config.od bit and set it back to one...
1231 */
1232 case CPU_AU1000: /* rev. DA, HA, HB */
1233 case CPU_AU1100: /* rev. AB, BA, BC ?? */
1234 case CPU_AU1500: /* rev. AB */
1235 au1x00_fixup_config_od();
1236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 }
1238}
1239
Ralf Baechle02cf2112005-10-01 13:06:32 +01001240void __init r4k_cache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241{
1242 extern void build_clear_page(void);
1243 extern void build_copy_page(void);
Ralf Baechle641e97f2007-10-11 23:46:05 +01001244 extern char __weak except_vec2_generic;
1245 extern char __weak except_vec2_sb1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 struct cpuinfo_mips *c = &current_cpu_data;
1247
Ralf Baechle641e97f2007-10-11 23:46:05 +01001248 switch (c->cputype) {
1249 case CPU_SB1:
1250 case CPU_SB1A:
1251 set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1252 break;
1253
1254 default:
1255 set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1256 break;
1257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 probe_pcache();
1260 setup_scache();
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 r4k_blast_dcache_page_setup();
1263 r4k_blast_dcache_page_indexed_setup();
1264 r4k_blast_dcache_setup();
1265 r4k_blast_icache_page_setup();
1266 r4k_blast_icache_page_indexed_setup();
1267 r4k_blast_icache_setup();
1268 r4k_blast_scache_page_setup();
1269 r4k_blast_scache_page_indexed_setup();
1270 r4k_blast_scache_setup();
1271
1272 /*
1273 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1274 * This code supports virtually indexed processors and will be
1275 * unnecessarily inefficient on physically indexed processors.
1276 */
Chris Dearman73f40352006-06-20 18:06:52 +01001277 if (c->dcache.linesz)
1278 shm_align_mask = max_t( unsigned long,
1279 c->dcache.sets * c->dcache.linesz - 1,
1280 PAGE_SIZE - 1);
1281 else
1282 shm_align_mask = PAGE_SIZE-1;
Ralf Baechledb813fe2007-09-27 18:26:43 +01001283 flush_cache_all = cache_noop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 __flush_cache_all = r4k___flush_cache_all;
1285 flush_cache_mm = r4k_flush_cache_mm;
1286 flush_cache_page = r4k_flush_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 flush_cache_range = r4k_flush_cache_range;
1288
1289 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1290 flush_icache_all = r4k_flush_icache_all;
Ralf Baechle7e3bfc72006-04-05 20:42:04 +01001291 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 flush_data_cache_page = r4k_flush_data_cache_page;
1293 flush_icache_range = r4k_flush_icache_range;
1294
1295#ifdef CONFIG_DMA_NONCOHERENT
1296 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1297 _dma_cache_wback = r4k_dma_cache_wback_inv;
1298 _dma_cache_inv = r4k_dma_cache_inv;
1299#endif
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 build_clear_page();
1302 build_copy_page();
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001303 local_r4k___flush_cache_all(NULL);
1304 coherency_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}