blob: d7088331fb0fa53f15a3849469ba17e9f46ee486 [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 Nemoto67a3f6de2006-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
348static inline void local_r4k_flush_cache_range(void * args)
349{
350 struct vm_area_struct *vma = args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (!(cpu_context(smp_processor_id(), vma->vm_mm)))
353 return;
354
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900355 r4k_blast_dcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
358static void r4k_flush_cache_range(struct vm_area_struct *vma,
359 unsigned long start, unsigned long end)
360{
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900361 if (!cpu_has_dc_aliases)
362 return;
363
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100364 r4k_on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
367static inline void local_r4k_flush_cache_mm(void * args)
368{
369 struct mm_struct *mm = args;
370
371 if (!cpu_context(smp_processor_id(), mm))
372 return;
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
375 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
376 * only flush the primary caches but R10000 and R12000 behave sane ...
Ralf Baechle617667b2006-11-30 01:14:48 +0000377 * R4000SC and R4400SC indexed S-cache ops also invalidate primary
378 * caches, so we can bail out early.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
Ralf Baechle10cc3522007-10-11 23:46:15 +0100380 if (current_cpu_type() == CPU_R4000SC ||
381 current_cpu_type() == CPU_R4000MC ||
382 current_cpu_type() == CPU_R4400SC ||
383 current_cpu_type() == CPU_R4400MC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 r4k_blast_scache();
Ralf Baechle617667b2006-11-30 01:14:48 +0000385 return;
386 }
387
388 r4k_blast_dcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391static void r4k_flush_cache_mm(struct mm_struct *mm)
392{
393 if (!cpu_has_dc_aliases)
394 return;
395
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100396 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397}
398
399struct flush_cache_page_args {
400 struct vm_area_struct *vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100401 unsigned long addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900402 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403};
404
405static inline void local_r4k_flush_cache_page(void *args)
406{
407 struct flush_cache_page_args *fcp_args = args;
408 struct vm_area_struct *vma = fcp_args->vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100409 unsigned long addr = fcp_args->addr;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100410 struct page *page = pfn_to_page(fcp_args->pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 int exec = vma->vm_flags & VM_EXEC;
412 struct mm_struct *mm = vma->vm_mm;
413 pgd_t *pgdp;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000414 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 pmd_t *pmdp;
416 pte_t *ptep;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100417 void *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Ralf Baechle79acf832005-02-10 13:54:37 +0000419 /*
420 * If ownes no valid ASID yet, cannot possibly have gotten
421 * this page into the cache.
422 */
Thiemo Seufer26a51b22005-02-19 13:32:02 +0000423 if (cpu_context(smp_processor_id(), mm) == 0)
Ralf Baechle79acf832005-02-10 13:54:37 +0000424 return;
425
Ralf Baechle6ec25802005-10-12 00:02:34 +0100426 addr &= PAGE_MASK;
427 pgdp = pgd_offset(mm, addr);
428 pudp = pud_offset(pgdp, addr);
429 pmdp = pmd_offset(pudp, addr);
430 ptep = pte_offset(pmdp, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
432 /*
433 * If the page isn't marked valid, the page cannot possibly be
434 * in the cache.
435 */
436 if (!(pte_val(*ptep) & _PAGE_PRESENT))
437 return;
438
Ralf Baechledb813fe2007-09-27 18:26:43 +0100439 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
440 vaddr = NULL;
441 else {
442 /*
443 * Use kmap_coherent or kmap_atomic to do flushes for
444 * another ASID than the current one.
445 */
446 if (cpu_has_dc_aliases)
447 vaddr = kmap_coherent(page, addr);
448 else
449 vaddr = kmap_atomic(page, KM_USER0);
450 addr = (unsigned long)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 }
452
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100454 r4k_blast_dcache_page(addr);
455 if (exec && !cpu_icache_snoops_remote_store)
456 r4k_blast_scache_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 if (exec) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100459 if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 int cpu = smp_processor_id();
461
Thiemo Seufer26a51b22005-02-19 13:32:02 +0000462 if (cpu_context(cpu, mm) != 0)
463 drop_mmu_context(mm, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else
Ralf Baechledb813fe2007-09-27 18:26:43 +0100465 r4k_blast_icache_page(addr);
466 }
467
468 if (vaddr) {
469 if (cpu_has_dc_aliases)
470 kunmap_coherent();
471 else
472 kunmap_atomic(vaddr, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474}
475
Ralf Baechle6ec25802005-10-12 00:02:34 +0100476static void r4k_flush_cache_page(struct vm_area_struct *vma,
477 unsigned long addr, unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct flush_cache_page_args args;
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 args.vma = vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100482 args.addr = addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900483 args.pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100485 r4k_on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488static inline void local_r4k_flush_data_cache_page(void * addr)
489{
490 r4k_blast_dcache_page((unsigned long) addr);
491}
492
493static void r4k_flush_data_cache_page(unsigned long addr)
494{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100495 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498struct flush_icache_range_args {
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900499 unsigned long start;
500 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501};
502
503static inline void local_r4k_flush_icache_range(void *args)
504{
505 struct flush_icache_range_args *fir_args = args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 unsigned long start = fir_args->start;
507 unsigned long end = fir_args->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509 if (!cpu_has_ic_fills_f_dc) {
Chris Dearman73f40352006-06-20 18:06:52 +0100510 if (end - start >= dcache_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 r4k_blast_dcache();
512 } else {
Thiemo Seufer10a3dab2005-09-09 20:26:54 +0000513 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900514 protected_blast_dcache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000517 if (!cpu_icache_snoops_remote_store && scache_size) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900518 if (end - start > scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900520 else
521 protected_blast_scache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
523 }
524
525 if (end - start > icache_size)
526 r4k_blast_icache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900527 else
528 protected_blast_icache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900531static void r4k_flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532{
533 struct flush_icache_range_args args;
534
535 args.start = start;
536 args.end = end;
537
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100538 r4k_on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
Ralf Baechlecc61c1f2005-07-12 18:35:38 +0000539 instruction_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542#ifdef CONFIG_DMA_NONCOHERENT
543
544static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
545{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Catch bad driver code */
547 BUG_ON(size == 0);
548
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100549 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900550 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900552 else
553 blast_scache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return;
555 }
556
557 /*
558 * Either no secondary cache or the available caches don't have the
559 * subset property so we have to flush the primary caches
560 * explicitly
561 */
562 if (size >= dcache_size) {
563 r4k_blast_dcache();
564 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900566 blast_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568
569 bc_wback_inv(addr, size);
570}
571
572static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
573{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* Catch bad driver code */
575 BUG_ON(size == 0);
576
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100577 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900578 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900580 else
581 blast_scache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return;
583 }
584
585 if (size >= dcache_size) {
586 r4k_blast_dcache();
587 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900589 blast_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
591
592 bc_inv(addr, size);
593}
594#endif /* CONFIG_DMA_NONCOHERENT */
595
596/*
597 * While we're protected against bad userland addresses we don't care
598 * very much about what happens in that case. Usually a segmentation
599 * fault will dump the process later on anyway ...
600 */
601static void local_r4k_flush_cache_sigtramp(void * arg)
602{
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000603 unsigned long ic_lsize = cpu_icache_line_size();
604 unsigned long dc_lsize = cpu_dcache_line_size();
605 unsigned long sc_lsize = cpu_scache_line_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 unsigned long addr = (unsigned long) arg;
607
608 R4600_HIT_CACHEOP_WAR_IMPL;
Chris Dearman73f40352006-06-20 18:06:52 +0100609 if (dc_lsize)
610 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000611 if (!cpu_icache_snoops_remote_store && scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
Chris Dearman73f40352006-06-20 18:06:52 +0100613 if (ic_lsize)
614 protected_flush_icache_line(addr & ~(ic_lsize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 if (MIPS4K_ICACHE_REFILL_WAR) {
616 __asm__ __volatile__ (
617 ".set push\n\t"
618 ".set noat\n\t"
619 ".set mips3\n\t"
Ralf Baechle875d43e2005-09-03 15:56:16 -0700620#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 "la $at,1f\n\t"
622#endif
Ralf Baechle875d43e2005-09-03 15:56:16 -0700623#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 "dla $at,1f\n\t"
625#endif
626 "cache %0,($at)\n\t"
627 "nop; nop; nop\n"
628 "1:\n\t"
629 ".set pop"
630 :
631 : "i" (Hit_Invalidate_I));
632 }
633 if (MIPS_CACHE_SYNC_WAR)
634 __asm__ __volatile__ ("sync");
635}
636
637static void r4k_flush_cache_sigtramp(unsigned long addr)
638{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100639 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640}
641
642static void r4k_flush_icache_all(void)
643{
644 if (cpu_has_vtag_icache)
645 r4k_blast_icache();
646}
647
648static inline void rm7k_erratum31(void)
649{
650 const unsigned long ic_lsize = 32;
651 unsigned long addr;
652
653 /* RM7000 erratum #31. The icache is screwed at startup. */
654 write_c0_taglo(0);
655 write_c0_taghi(0);
656
657 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
658 __asm__ __volatile__ (
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000659 ".set push\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 ".set noreorder\n\t"
661 ".set mips3\n\t"
662 "cache\t%1, 0(%0)\n\t"
663 "cache\t%1, 0x1000(%0)\n\t"
664 "cache\t%1, 0x2000(%0)\n\t"
665 "cache\t%1, 0x3000(%0)\n\t"
666 "cache\t%2, 0(%0)\n\t"
667 "cache\t%2, 0x1000(%0)\n\t"
668 "cache\t%2, 0x2000(%0)\n\t"
669 "cache\t%2, 0x3000(%0)\n\t"
670 "cache\t%1, 0(%0)\n\t"
671 "cache\t%1, 0x1000(%0)\n\t"
672 "cache\t%1, 0x2000(%0)\n\t"
673 "cache\t%1, 0x3000(%0)\n\t"
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000674 ".set pop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 :
676 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
677 }
678}
679
680static char *way_string[] __initdata = { NULL, "direct mapped", "2-way",
681 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
682};
683
684static void __init probe_pcache(void)
685{
686 struct cpuinfo_mips *c = &current_cpu_data;
687 unsigned int config = read_c0_config();
688 unsigned int prid = read_c0_prid();
689 unsigned long config1;
690 unsigned int lsize;
691
692 switch (c->cputype) {
693 case CPU_R4600: /* QED style two way caches? */
694 case CPU_R4700:
695 case CPU_R5000:
696 case CPU_NEVADA:
697 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
698 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
699 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900700 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
702 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
703 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
704 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900705 c->dcache.waybit= __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 c->options |= MIPS_CPU_CACHE_CDEX_P;
708 break;
709
710 case CPU_R5432:
711 case CPU_R5500:
712 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
713 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
714 c->icache.ways = 2;
715 c->icache.waybit= 0;
716
717 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
718 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
719 c->dcache.ways = 2;
720 c->dcache.waybit = 0;
721
722 c->options |= MIPS_CPU_CACHE_CDEX_P;
723 break;
724
725 case CPU_TX49XX:
726 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
727 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
728 c->icache.ways = 4;
729 c->icache.waybit= 0;
730
731 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
732 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
733 c->dcache.ways = 4;
734 c->dcache.waybit = 0;
735
736 c->options |= MIPS_CPU_CACHE_CDEX_P;
Atsushi Nemotode862b42006-03-17 12:59:22 +0900737 c->options |= MIPS_CPU_PREFETCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
739
740 case CPU_R4000PC:
741 case CPU_R4000SC:
742 case CPU_R4000MC:
743 case CPU_R4400PC:
744 case CPU_R4400SC:
745 case CPU_R4400MC:
746 case CPU_R4300:
747 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
748 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
749 c->icache.ways = 1;
750 c->icache.waybit = 0; /* doesn't matter */
751
752 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
753 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
754 c->dcache.ways = 1;
755 c->dcache.waybit = 0; /* does not matter */
756
757 c->options |= MIPS_CPU_CACHE_CDEX_P;
758 break;
759
760 case CPU_R10000:
761 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400762 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
764 c->icache.linesz = 64;
765 c->icache.ways = 2;
766 c->icache.waybit = 0;
767
768 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
769 c->dcache.linesz = 32;
770 c->dcache.ways = 2;
771 c->dcache.waybit = 0;
772
773 c->options |= MIPS_CPU_PREFETCH;
774 break;
775
776 case CPU_VR4133:
Yoichi Yuasa2874fe52006-07-08 00:42:12 +0900777 write_c0_config(config & ~VR41_CONF_P4K);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 case CPU_VR4131:
779 /* Workaround for cache instruction bug of VR4131 */
780 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
781 c->processor_id == 0x0c82U) {
Yoichi Yuasa4e8ab362006-07-04 22:59:41 +0900782 config |= 0x00400000U;
783 if (c->processor_id == 0x0c80U)
784 config |= VR41_CONF_BP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 write_c0_config(config);
Yoichi Yuasa1058ecd2006-07-08 00:42:01 +0900786 } else
787 c->options |= MIPS_CPU_CACHE_CDEX_P;
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
790 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
791 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900792 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
795 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
796 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900797 c->dcache.waybit = __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 break;
799
800 case CPU_VR41XX:
801 case CPU_VR4111:
802 case CPU_VR4121:
803 case CPU_VR4122:
804 case CPU_VR4181:
805 case CPU_VR4181A:
806 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
807 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
808 c->icache.ways = 1;
809 c->icache.waybit = 0; /* doesn't matter */
810
811 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
812 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
813 c->dcache.ways = 1;
814 c->dcache.waybit = 0; /* does not matter */
815
816 c->options |= MIPS_CPU_CACHE_CDEX_P;
817 break;
818
819 case CPU_RM7000:
820 rm7k_erratum31();
821
822 case CPU_RM9000:
823 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
824 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
825 c->icache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900826 c->icache.waybit = __ffs(icache_size / c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
829 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
830 c->dcache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900831 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833#if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
834 c->options |= MIPS_CPU_CACHE_CDEX_P;
835#endif
836 c->options |= MIPS_CPU_PREFETCH;
837 break;
838
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800839 case CPU_LOONGSON2:
840 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
841 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
842 if (prid & 0x3)
843 c->icache.ways = 4;
844 else
845 c->icache.ways = 2;
846 c->icache.waybit = 0;
847
848 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
849 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
850 if (prid & 0x3)
851 c->dcache.ways = 4;
852 else
853 c->dcache.ways = 2;
854 c->dcache.waybit = 0;
855 break;
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 default:
858 if (!(config & MIPS_CONF_M))
859 panic("Don't know how to probe P-caches on this cpu.");
860
861 /*
862 * So we seem to be a MIPS32 or MIPS64 CPU
863 * So let's probe the I-cache ...
864 */
865 config1 = read_c0_config1();
866
867 if ((lsize = ((config1 >> 19) & 7)))
868 c->icache.linesz = 2 << lsize;
869 else
870 c->icache.linesz = lsize;
871 c->icache.sets = 64 << ((config1 >> 22) & 7);
872 c->icache.ways = 1 + ((config1 >> 16) & 7);
873
874 icache_size = c->icache.sets *
875 c->icache.ways *
876 c->icache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900877 c->icache.waybit = __ffs(icache_size/c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 if (config & 0x8) /* VI bit */
880 c->icache.flags |= MIPS_CACHE_VTAG;
881
882 /*
883 * Now probe the MIPS32 / MIPS64 data cache.
884 */
885 c->dcache.flags = 0;
886
887 if ((lsize = ((config1 >> 10) & 7)))
888 c->dcache.linesz = 2 << lsize;
889 else
890 c->dcache.linesz= lsize;
891 c->dcache.sets = 64 << ((config1 >> 13) & 7);
892 c->dcache.ways = 1 + ((config1 >> 7) & 7);
893
894 dcache_size = c->dcache.sets *
895 c->dcache.ways *
896 c->dcache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900897 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899 c->options |= MIPS_CPU_PREFETCH;
900 break;
901 }
902
903 /*
904 * Processor configuration sanity check for the R4000SC erratum
905 * #5. With page sizes larger than 32kB there is no possibility
906 * to get a VCE exception anymore so we don't care about this
907 * misconfiguration. The case is rather theoretical anyway;
908 * presumably no vendor is shipping his hardware in the "bad"
909 * configuration.
910 */
911 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
912 !(config & CONF_SC) && c->icache.linesz != 16 &&
913 PAGE_SIZE <= 0x8000)
914 panic("Improper R4000SC processor configuration detected");
915
916 /* compute a couple of other cache variables */
917 c->icache.waysize = icache_size / c->icache.ways;
918 c->dcache.waysize = dcache_size / c->dcache.ways;
919
Chris Dearman73f40352006-06-20 18:06:52 +0100920 c->icache.sets = c->icache.linesz ?
921 icache_size / (c->icache.linesz * c->icache.ways) : 0;
922 c->dcache.sets = c->dcache.linesz ?
923 dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 /*
926 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
927 * 2-way virtually indexed so normally would suffer from aliases. So
928 * normally they'd suffer from aliases but magic in the hardware deals
929 * with that for us so we don't need to take care ourselves.
930 */
Ralf Baechled1e344e2005-02-04 15:51:26 +0000931 switch (c->cputype) {
Ralf Baechlea95970f2005-02-07 21:41:32 +0000932 case CPU_20KC:
Ralf Baechle505403b2005-02-07 21:53:39 +0000933 case CPU_25KF:
Ralf Baechle641e97f2007-10-11 23:46:05 +0100934 case CPU_SB1:
935 case CPU_SB1A:
Atsushi Nemotode628932006-03-13 18:23:03 +0900936 c->dcache.flags |= MIPS_CACHE_PINDEX;
Ralf Baechle641e97f2007-10-11 23:46:05 +0100937 break;
938
Ralf Baechled1e344e2005-02-04 15:51:26 +0000939 case CPU_R10000:
940 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400941 case CPU_R14000:
Ralf Baechled1e344e2005-02-04 15:51:26 +0000942 break;
Ralf Baechle641e97f2007-10-11 23:46:05 +0100943
Ralf Baechled1e344e2005-02-04 15:51:26 +0000944 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +0100945 case CPU_34K:
Ralf Baechle2e78ae32006-06-23 18:48:21 +0100946 case CPU_74K:
Ralf Baechlebeab3752006-06-19 21:56:25 +0100947 if ((read_c0_config7() & (1 << 16))) {
948 /* effectively physically indexed dcache,
949 thus no virtual aliases. */
950 c->dcache.flags |= MIPS_CACHE_PINDEX;
951 break;
952 }
Ralf Baechled1e344e2005-02-04 15:51:26 +0000953 default:
Ralf Baechlebeab3752006-06-19 21:56:25 +0100954 if (c->dcache.waysize > PAGE_SIZE)
955 c->dcache.flags |= MIPS_CACHE_ALIASES;
Ralf Baechled1e344e2005-02-04 15:51:26 +0000956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 switch (c->cputype) {
959 case CPU_20KC:
960 /*
961 * Some older 20Kc chips doesn't have the 'VI' bit in
962 * the config register.
963 */
964 c->icache.flags |= MIPS_CACHE_VTAG;
965 break;
966
Pete Popove3ad1c22005-03-01 06:33:16 +0000967 case CPU_AU1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 case CPU_AU1500:
Pete Popove3ad1c22005-03-01 06:33:16 +0000969 case CPU_AU1100:
970 case CPU_AU1550:
971 case CPU_AU1200:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 c->icache.flags |= MIPS_CACHE_IC_F_DC;
973 break;
974 }
975
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800976#ifdef CONFIG_CPU_LOONGSON2
977 /*
978 * LOONGSON2 has 4 way icache, but when using indexed cache op,
979 * one op will act on all 4 ways
980 */
981 c->icache.ways = 1;
982#endif
983
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
985 icache_size >> 10,
Ralf Baechle64bfca52007-10-15 16:35:45 +0100986 cpu_has_vtag_icache ? "VIVT" : "VIPT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 way_string[c->icache.ways], c->icache.linesz);
988
Ralf Baechle64bfca52007-10-15 16:35:45 +0100989 printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
990 dcache_size >> 10, way_string[c->dcache.ways],
991 (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
992 (c->dcache.flags & MIPS_CACHE_ALIASES) ?
993 "cache aliases" : "no aliases",
994 c->dcache.linesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997/*
998 * If you even _breathe_ on this function, look at the gcc output and make sure
999 * it does not pop things on and off the stack for the cache sizing loop that
1000 * executes in KSEG1 space or else you will crash and burn badly. You have
1001 * been warned.
1002 */
1003static int __init probe_scache(void)
1004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 unsigned long flags, addr, begin, end, pow2;
1006 unsigned int config = read_c0_config();
1007 struct cpuinfo_mips *c = &current_cpu_data;
1008 int tmp;
1009
1010 if (config & CONF_SC)
1011 return 0;
1012
Ralf Baechlee001e522007-07-28 12:45:47 +01001013 begin = (unsigned long) &_stext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 begin &= ~((4 * 1024 * 1024) - 1);
1015 end = begin + (4 * 1024 * 1024);
1016
1017 /*
1018 * This is such a bitch, you'd think they would make it easy to do
1019 * this. Away you daemons of stupidity!
1020 */
1021 local_irq_save(flags);
1022
1023 /* Fill each size-multiple cache line with a valid tag. */
1024 pow2 = (64 * 1024);
1025 for (addr = begin; addr < end; addr = (begin + pow2)) {
1026 unsigned long *p = (unsigned long *) addr;
1027 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1028 pow2 <<= 1;
1029 }
1030
1031 /* Load first line with zero (therefore invalid) tag. */
1032 write_c0_taglo(0);
1033 write_c0_taghi(0);
1034 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1035 cache_op(Index_Store_Tag_I, begin);
1036 cache_op(Index_Store_Tag_D, begin);
1037 cache_op(Index_Store_Tag_SD, begin);
1038
1039 /* Now search for the wrap around point. */
1040 pow2 = (128 * 1024);
1041 tmp = 0;
1042 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1043 cache_op(Index_Load_Tag_SD, addr);
1044 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1045 if (!read_c0_taglo())
1046 break;
1047 pow2 <<= 1;
1048 }
1049 local_irq_restore(flags);
1050 addr -= begin;
1051
1052 scache_size = addr;
1053 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1054 c->scache.ways = 1;
1055 c->dcache.waybit = 0; /* does not matter */
1056
1057 return 1;
1058}
1059
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001060#if defined(CONFIG_CPU_LOONGSON2)
1061static void __init loongson2_sc_init(void)
1062{
1063 struct cpuinfo_mips *c = &current_cpu_data;
1064
1065 scache_size = 512*1024;
1066 c->scache.linesz = 32;
1067 c->scache.ways = 4;
1068 c->scache.waybit = 0;
1069 c->scache.waysize = scache_size / (c->scache.ways);
1070 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1071 pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1072 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1073
1074 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1075}
1076#endif
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078extern int r5k_sc_init(void);
1079extern int rm7k_sc_init(void);
Chris Dearman9318c512006-06-20 17:15:20 +01001080extern int mips_sc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
1082static void __init setup_scache(void)
1083{
1084 struct cpuinfo_mips *c = &current_cpu_data;
1085 unsigned int config = read_c0_config();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int sc_present = 0;
1087
1088 /*
1089 * Do the probing thing on R4000SC and R4400SC processors. Other
1090 * processors don't have a S-cache that would be relevant to the
1091 * Linux memory managment.
1092 */
1093 switch (c->cputype) {
1094 case CPU_R4000SC:
1095 case CPU_R4000MC:
1096 case CPU_R4400SC:
1097 case CPU_R4400MC:
Thiemo Seuferba5187d2005-04-25 16:36:23 +00001098 sc_present = run_uncached(probe_scache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 if (sc_present)
1100 c->options |= MIPS_CPU_CACHE_CDEX_S;
1101 break;
1102
1103 case CPU_R10000:
1104 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -04001105 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1107 c->scache.linesz = 64 << ((config >> 13) & 1);
1108 c->scache.ways = 2;
1109 c->scache.waybit= 0;
1110 sc_present = 1;
1111 break;
1112
1113 case CPU_R5000:
1114 case CPU_NEVADA:
1115#ifdef CONFIG_R5000_CPU_SCACHE
1116 r5k_sc_init();
1117#endif
1118 return;
1119
1120 case CPU_RM7000:
1121 case CPU_RM9000:
1122#ifdef CONFIG_RM7000_CPU_SCACHE
1123 rm7k_sc_init();
1124#endif
1125 return;
1126
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001127#if defined(CONFIG_CPU_LOONGSON2)
1128 case CPU_LOONGSON2:
1129 loongson2_sc_init();
1130 return;
1131#endif
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 default:
Chris Dearman9318c512006-06-20 17:15:20 +01001134 if (c->isa_level == MIPS_CPU_ISA_M32R1 ||
1135 c->isa_level == MIPS_CPU_ISA_M32R2 ||
1136 c->isa_level == MIPS_CPU_ISA_M64R1 ||
1137 c->isa_level == MIPS_CPU_ISA_M64R2) {
1138#ifdef CONFIG_MIPS_CPU_SCACHE
1139 if (mips_sc_init ()) {
1140 scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
1141 printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
1142 scache_size >> 10,
1143 way_string[c->scache.ways], c->scache.linesz);
1144 }
1145#else
1146 if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1147 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1148#endif
1149 return;
1150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 sc_present = 0;
1152 }
1153
1154 if (!sc_present)
1155 return;
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 /* compute a couple of other cache variables */
1158 c->scache.waysize = scache_size / c->scache.ways;
1159
1160 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1161
1162 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1163 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1164
Ralf Baechlefc5d2d22006-07-06 13:04:01 +01001165 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166}
1167
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001168void au1x00_fixup_config_od(void)
1169{
1170 /*
1171 * c0_config.od (bit 19) was write only (and read as 0)
1172 * on the early revisions of Alchemy SOCs. It disables the bus
1173 * transaction overlapping and needs to be set to fix various errata.
1174 */
1175 switch (read_c0_prid()) {
1176 case 0x00030100: /* Au1000 DA */
1177 case 0x00030201: /* Au1000 HA */
1178 case 0x00030202: /* Au1000 HB */
1179 case 0x01030200: /* Au1500 AB */
1180 /*
1181 * Au1100 errata actually keeps silence about this bit, so we set it
1182 * just in case for those revisions that require it to be set according
1183 * to arch/mips/au1000/common/cputable.c
1184 */
1185 case 0x02030200: /* Au1100 AB */
1186 case 0x02030201: /* Au1100 BA */
1187 case 0x02030202: /* Au1100 BC */
1188 set_c0_config(1 << 19);
1189 break;
1190 }
1191}
1192
Ralf Baechlea00f6312006-08-01 23:39:42 +01001193static void __init coherency_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194{
1195 change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
1196
1197 /*
1198 * c0_status.cu=0 specifies that updates by the sc instruction use
1199 * the coherency mode specified by the TLB; 1 means cachable
1200 * coherent update on write will be used. Not all processors have
1201 * this bit and; some wire it to zero, others like Toshiba had the
1202 * silly idea of putting something else there ...
1203 */
Ralf Baechle10cc3522007-10-11 23:46:15 +01001204 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 case CPU_R4000PC:
1206 case CPU_R4000SC:
1207 case CPU_R4000MC:
1208 case CPU_R4400PC:
1209 case CPU_R4400SC:
1210 case CPU_R4400MC:
1211 clear_c0_config(CONF_CU);
1212 break;
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001213 /*
Ralf Baechledf586d52006-08-01 23:42:30 +01001214 * We need to catch the early Alchemy SOCs with
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001215 * the write-only co_config.od bit and set it back to one...
1216 */
1217 case CPU_AU1000: /* rev. DA, HA, HB */
1218 case CPU_AU1100: /* rev. AB, BA, BC ?? */
1219 case CPU_AU1500: /* rev. AB */
1220 au1x00_fixup_config_od();
1221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223}
1224
Ralf Baechle02cf2112005-10-01 13:06:32 +01001225void __init r4k_cache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226{
1227 extern void build_clear_page(void);
1228 extern void build_copy_page(void);
Ralf Baechle641e97f2007-10-11 23:46:05 +01001229 extern char __weak except_vec2_generic;
1230 extern char __weak except_vec2_sb1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 struct cpuinfo_mips *c = &current_cpu_data;
1232
Ralf Baechle641e97f2007-10-11 23:46:05 +01001233 switch (c->cputype) {
1234 case CPU_SB1:
1235 case CPU_SB1A:
1236 set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1237 break;
1238
1239 default:
1240 set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1241 break;
1242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 probe_pcache();
1245 setup_scache();
1246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 r4k_blast_dcache_page_setup();
1248 r4k_blast_dcache_page_indexed_setup();
1249 r4k_blast_dcache_setup();
1250 r4k_blast_icache_page_setup();
1251 r4k_blast_icache_page_indexed_setup();
1252 r4k_blast_icache_setup();
1253 r4k_blast_scache_page_setup();
1254 r4k_blast_scache_page_indexed_setup();
1255 r4k_blast_scache_setup();
1256
1257 /*
1258 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1259 * This code supports virtually indexed processors and will be
1260 * unnecessarily inefficient on physically indexed processors.
1261 */
Chris Dearman73f40352006-06-20 18:06:52 +01001262 if (c->dcache.linesz)
1263 shm_align_mask = max_t( unsigned long,
1264 c->dcache.sets * c->dcache.linesz - 1,
1265 PAGE_SIZE - 1);
1266 else
1267 shm_align_mask = PAGE_SIZE-1;
Ralf Baechledb813fe2007-09-27 18:26:43 +01001268 flush_cache_all = cache_noop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 __flush_cache_all = r4k___flush_cache_all;
1270 flush_cache_mm = r4k_flush_cache_mm;
1271 flush_cache_page = r4k_flush_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 flush_cache_range = r4k_flush_cache_range;
1273
1274 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1275 flush_icache_all = r4k_flush_icache_all;
Ralf Baechle7e3bfc72006-04-05 20:42:04 +01001276 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 flush_data_cache_page = r4k_flush_data_cache_page;
1278 flush_icache_range = r4k_flush_icache_range;
1279
1280#ifdef CONFIG_DMA_NONCOHERENT
1281 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1282 _dma_cache_wback = r4k_dma_cache_wback_inv;
1283 _dma_cache_inv = r4k_dma_cache_inv;
1284#endif
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 build_clear_page();
1287 build_copy_page();
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001288 local_r4k___flush_cache_all(NULL);
1289 coherency_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}