blob: 2c4f7e11f0d55fe746d142f764b0e4024f78d686 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
7 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Ralf Baechle (ralf@gnu.org)
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
9 */
Ralf Baechlea754f702007-11-03 01:01:37 +000010#include <linux/hardirq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
Ralf Baechledb813fe2007-09-27 18:26:43 +010012#include <linux/highmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/kernel.h>
Ralf Baechle641e97f2007-10-11 23:46:05 +010014#include <linux/linkage.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sched.h>
16#include <linux/mm.h>
17#include <linux/bitops.h>
18
19#include <asm/bcache.h>
20#include <asm/bootinfo.h>
Ralf Baechleec74e362005-07-13 11:48:45 +000021#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/cacheops.h>
23#include <asm/cpu.h>
24#include <asm/cpu-features.h>
25#include <asm/io.h>
26#include <asm/page.h>
27#include <asm/pgtable.h>
28#include <asm/r4kcache.h>
Ralf Baechlee001e522007-07-28 12:45:47 +010029#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/system.h>
31#include <asm/mmu_context.h>
32#include <asm/war.h>
Thiemo Seuferba5187d2005-04-25 16:36:23 +000033#include <asm/cacheflush.h> /* for run_uncached() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Ralf Baechle7f3f1d02006-05-12 13:20:06 +010035
36/*
37 * Special Variant of smp_call_function for use by cache functions:
38 *
39 * o No return value
40 * o collapses to normal function call on UP kernels
41 * o collapses to normal function call on systems with a single shared
42 * primary cache.
43 */
44static inline void r4k_on_each_cpu(void (*func) (void *info), void *info,
45 int retry, int wait)
46{
47 preempt_disable();
48
49#if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
50 smp_call_function(func, info, retry, wait);
51#endif
52 func(info);
53 preempt_enable();
54}
55
Ralf Baechleec74e362005-07-13 11:48:45 +000056/*
57 * Must die.
58 */
59static unsigned long icache_size __read_mostly;
60static unsigned long dcache_size __read_mostly;
61static unsigned long scache_size __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/*
64 * Dummy cache handling routines for machines without boardcaches
65 */
Chris Dearman73f40352006-06-20 18:06:52 +010066static void cache_noop(void) {}
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static struct bcache_ops no_sc_ops = {
Chris Dearman73f40352006-06-20 18:06:52 +010069 .bc_enable = (void *)cache_noop,
70 .bc_disable = (void *)cache_noop,
71 .bc_wback_inv = (void *)cache_noop,
72 .bc_inv = (void *)cache_noop
Linus Torvalds1da177e2005-04-16 15:20:36 -070073};
74
75struct bcache_ops *bcops = &no_sc_ops;
76
Thiemo Seufer330cfe02005-09-01 18:33:58 +000077#define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
78#define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define R4600_HIT_CACHEOP_WAR_IMPL \
81do { \
82 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
83 *(volatile unsigned long *)CKSEG1; \
84 if (R4600_V1_HIT_CACHEOP_WAR) \
85 __asm__ __volatile__("nop;nop;nop;nop"); \
86} while (0)
87
88static void (*r4k_blast_dcache_page)(unsigned long addr);
89
90static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
91{
92 R4600_HIT_CACHEOP_WAR_IMPL;
93 blast_dcache32_page(addr);
94}
95
Ralf Baechlea00f6312006-08-01 23:39:42 +010096static void __init r4k_blast_dcache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 unsigned long dc_lsize = cpu_dcache_line_size();
99
Chris Dearman73f40352006-06-20 18:06:52 +0100100 if (dc_lsize == 0)
101 r4k_blast_dcache_page = (void *)cache_noop;
102 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 r4k_blast_dcache_page = blast_dcache16_page;
104 else if (dc_lsize == 32)
105 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
106}
107
108static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
109
Ralf Baechlea00f6312006-08-01 23:39:42 +0100110static void __init r4k_blast_dcache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 unsigned long dc_lsize = cpu_dcache_line_size();
113
Chris Dearman73f40352006-06-20 18:06:52 +0100114 if (dc_lsize == 0)
115 r4k_blast_dcache_page_indexed = (void *)cache_noop;
116 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
118 else if (dc_lsize == 32)
119 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
120}
121
122static void (* r4k_blast_dcache)(void);
123
Ralf Baechlea00f6312006-08-01 23:39:42 +0100124static void __init r4k_blast_dcache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 unsigned long dc_lsize = cpu_dcache_line_size();
127
Chris Dearman73f40352006-06-20 18:06:52 +0100128 if (dc_lsize == 0)
129 r4k_blast_dcache = (void *)cache_noop;
130 else if (dc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 r4k_blast_dcache = blast_dcache16;
132 else if (dc_lsize == 32)
133 r4k_blast_dcache = blast_dcache32;
134}
135
136/* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
137#define JUMP_TO_ALIGN(order) \
138 __asm__ __volatile__( \
139 "b\t1f\n\t" \
140 ".align\t" #order "\n\t" \
141 "1:\n\t" \
142 )
143#define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
144#define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
145
146static inline void blast_r4600_v1_icache32(void)
147{
148 unsigned long flags;
149
150 local_irq_save(flags);
151 blast_icache32();
152 local_irq_restore(flags);
153}
154
155static inline void tx49_blast_icache32(void)
156{
157 unsigned long start = INDEX_BASE;
158 unsigned long end = start + current_cpu_data.icache.waysize;
159 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
160 unsigned long ws_end = current_cpu_data.icache.ways <<
161 current_cpu_data.icache.waybit;
162 unsigned long ws, addr;
163
164 CACHE32_UNROLL32_ALIGN2;
165 /* I'm in even chunk. blast odd chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700166 for (ws = 0; ws < ws_end; ws += ws_inc)
167 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100168 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 CACHE32_UNROLL32_ALIGN;
170 /* I'm in odd chunk. blast even chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700171 for (ws = 0; ws < ws_end; ws += ws_inc)
172 for (addr = start; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100173 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174}
175
176static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
177{
178 unsigned long flags;
179
180 local_irq_save(flags);
181 blast_icache32_page_indexed(page);
182 local_irq_restore(flags);
183}
184
185static inline void tx49_blast_icache32_page_indexed(unsigned long page)
186{
Atsushi Nemoto67a3f6d2006-04-04 17:34:14 +0900187 unsigned long indexmask = current_cpu_data.icache.waysize - 1;
188 unsigned long start = INDEX_BASE + (page & indexmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 unsigned long end = start + PAGE_SIZE;
190 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
191 unsigned long ws_end = current_cpu_data.icache.ways <<
192 current_cpu_data.icache.waybit;
193 unsigned long ws, addr;
194
195 CACHE32_UNROLL32_ALIGN2;
196 /* I'm in even chunk. blast odd chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700197 for (ws = 0; ws < ws_end; ws += ws_inc)
198 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100199 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 CACHE32_UNROLL32_ALIGN;
201 /* I'm in odd chunk. blast even chunks */
Ralf Baechle42a3b4f2005-09-03 15:56:17 -0700202 for (ws = 0; ws < ws_end; ws += ws_inc)
203 for (addr = start; addr < end; addr += 0x400 * 2)
Ralf Baechle21a151d2007-10-11 23:46:15 +0100204 cache32_unroll32(addr|ws, Index_Invalidate_I);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
207static void (* r4k_blast_icache_page)(unsigned long addr);
208
Ralf Baechlea00f6312006-08-01 23:39:42 +0100209static void __init r4k_blast_icache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 unsigned long ic_lsize = cpu_icache_line_size();
212
Chris Dearman73f40352006-06-20 18:06:52 +0100213 if (ic_lsize == 0)
214 r4k_blast_icache_page = (void *)cache_noop;
215 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 r4k_blast_icache_page = blast_icache16_page;
217 else if (ic_lsize == 32)
218 r4k_blast_icache_page = blast_icache32_page;
219 else if (ic_lsize == 64)
220 r4k_blast_icache_page = blast_icache64_page;
221}
222
223
224static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
225
Ralf Baechlea00f6312006-08-01 23:39:42 +0100226static void __init r4k_blast_icache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 unsigned long ic_lsize = cpu_icache_line_size();
229
Chris Dearman73f40352006-06-20 18:06:52 +0100230 if (ic_lsize == 0)
231 r4k_blast_icache_page_indexed = (void *)cache_noop;
232 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
234 else if (ic_lsize == 32) {
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000235 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 r4k_blast_icache_page_indexed =
237 blast_icache32_r4600_v1_page_indexed;
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000238 else if (TX49XX_ICACHE_INDEX_INV_WAR)
239 r4k_blast_icache_page_indexed =
240 tx49_blast_icache32_page_indexed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 else
242 r4k_blast_icache_page_indexed =
243 blast_icache32_page_indexed;
244 } else if (ic_lsize == 64)
245 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
246}
247
248static void (* r4k_blast_icache)(void);
249
Ralf Baechlea00f6312006-08-01 23:39:42 +0100250static void __init r4k_blast_icache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 unsigned long ic_lsize = cpu_icache_line_size();
253
Chris Dearman73f40352006-06-20 18:06:52 +0100254 if (ic_lsize == 0)
255 r4k_blast_icache = (void *)cache_noop;
256 else if (ic_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 r4k_blast_icache = blast_icache16;
258 else if (ic_lsize == 32) {
259 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
260 r4k_blast_icache = blast_r4600_v1_icache32;
261 else if (TX49XX_ICACHE_INDEX_INV_WAR)
262 r4k_blast_icache = tx49_blast_icache32;
263 else
264 r4k_blast_icache = blast_icache32;
265 } else if (ic_lsize == 64)
266 r4k_blast_icache = blast_icache64;
267}
268
269static void (* r4k_blast_scache_page)(unsigned long addr);
270
Ralf Baechlea00f6312006-08-01 23:39:42 +0100271static void __init r4k_blast_scache_page_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
273 unsigned long sc_lsize = cpu_scache_line_size();
274
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000275 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100276 r4k_blast_scache_page = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000277 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 r4k_blast_scache_page = blast_scache16_page;
279 else if (sc_lsize == 32)
280 r4k_blast_scache_page = blast_scache32_page;
281 else if (sc_lsize == 64)
282 r4k_blast_scache_page = blast_scache64_page;
283 else if (sc_lsize == 128)
284 r4k_blast_scache_page = blast_scache128_page;
285}
286
287static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
288
Ralf Baechlea00f6312006-08-01 23:39:42 +0100289static void __init r4k_blast_scache_page_indexed_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 unsigned long sc_lsize = cpu_scache_line_size();
292
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000293 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100294 r4k_blast_scache_page_indexed = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000295 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
297 else if (sc_lsize == 32)
298 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
299 else if (sc_lsize == 64)
300 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
301 else if (sc_lsize == 128)
302 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
303}
304
305static void (* r4k_blast_scache)(void);
306
Ralf Baechlea00f6312006-08-01 23:39:42 +0100307static void __init r4k_blast_scache_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 unsigned long sc_lsize = cpu_scache_line_size();
310
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000311 if (scache_size == 0)
Chris Dearman73f40352006-06-20 18:06:52 +0100312 r4k_blast_scache = (void *)cache_noop;
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000313 else if (sc_lsize == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 r4k_blast_scache = blast_scache16;
315 else if (sc_lsize == 32)
316 r4k_blast_scache = blast_scache32;
317 else if (sc_lsize == 64)
318 r4k_blast_scache = blast_scache64;
319 else if (sc_lsize == 128)
320 r4k_blast_scache = blast_scache128;
321}
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323static inline void local_r4k___flush_cache_all(void * args)
324{
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800325#if defined(CONFIG_CPU_LOONGSON2)
326 r4k_blast_scache();
327 return;
328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 r4k_blast_dcache();
330 r4k_blast_icache();
331
Ralf Baechle10cc3522007-10-11 23:46:15 +0100332 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 case CPU_R4000SC:
334 case CPU_R4000MC:
335 case CPU_R4400SC:
336 case CPU_R4400MC:
337 case CPU_R10000:
338 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400339 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 r4k_blast_scache();
341 }
342}
343
344static void r4k___flush_cache_all(void)
345{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100346 r4k_on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100349static inline int has_valid_asid(const struct mm_struct *mm)
350{
351#if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
352 int i;
353
354 for_each_online_cpu(i)
355 if (cpu_context(i, mm))
356 return 1;
357
358 return 0;
359#else
360 return cpu_context(smp_processor_id(), mm);
361#endif
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364static inline void local_r4k_flush_cache_range(void * args)
365{
366 struct vm_area_struct *vma = args;
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000367 int exec = vma->vm_flags & VM_EXEC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100369 if (!(has_valid_asid(vma->vm_mm)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return;
371
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900372 r4k_blast_dcache();
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000373 if (exec)
374 r4k_blast_icache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
377static void r4k_flush_cache_range(struct vm_area_struct *vma,
378 unsigned long start, unsigned long end)
379{
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000380 int exec = vma->vm_flags & VM_EXEC;
Atsushi Nemoto0550d9d2006-08-22 21:15:47 +0900381
Ralf Baechle2eaa7ec2008-02-11 14:51:40 +0000382 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
383 r4k_on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386static inline void local_r4k_flush_cache_mm(void * args)
387{
388 struct mm_struct *mm = args;
389
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100390 if (!has_valid_asid(mm))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return;
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /*
394 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
395 * only flush the primary caches but R10000 and R12000 behave sane ...
Ralf Baechle617667b2006-11-30 01:14:48 +0000396 * R4000SC and R4400SC indexed S-cache ops also invalidate primary
397 * caches, so we can bail out early.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 */
Ralf Baechle10cc3522007-10-11 23:46:15 +0100399 if (current_cpu_type() == CPU_R4000SC ||
400 current_cpu_type() == CPU_R4000MC ||
401 current_cpu_type() == CPU_R4400SC ||
402 current_cpu_type() == CPU_R4400MC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 r4k_blast_scache();
Ralf Baechle617667b2006-11-30 01:14:48 +0000404 return;
405 }
406
407 r4k_blast_dcache();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410static void r4k_flush_cache_mm(struct mm_struct *mm)
411{
412 if (!cpu_has_dc_aliases)
413 return;
414
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100415 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
418struct flush_cache_page_args {
419 struct vm_area_struct *vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100420 unsigned long addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900421 unsigned long pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422};
423
424static inline void local_r4k_flush_cache_page(void *args)
425{
426 struct flush_cache_page_args *fcp_args = args;
427 struct vm_area_struct *vma = fcp_args->vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100428 unsigned long addr = fcp_args->addr;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100429 struct page *page = pfn_to_page(fcp_args->pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 int exec = vma->vm_flags & VM_EXEC;
431 struct mm_struct *mm = vma->vm_mm;
432 pgd_t *pgdp;
Ralf Baechlec6e8b582005-02-10 12:19:59 +0000433 pud_t *pudp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 pmd_t *pmdp;
435 pte_t *ptep;
Ralf Baechledb813fe2007-09-27 18:26:43 +0100436 void *vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Ralf Baechle79acf832005-02-10 13:54:37 +0000438 /*
439 * If ownes no valid ASID yet, cannot possibly have gotten
440 * this page into the cache.
441 */
Ralf Baechlea76ab5c2007-10-08 16:38:37 +0100442 if (!has_valid_asid(mm))
Ralf Baechle79acf832005-02-10 13:54:37 +0000443 return;
444
Ralf Baechle6ec25802005-10-12 00:02:34 +0100445 addr &= PAGE_MASK;
446 pgdp = pgd_offset(mm, addr);
447 pudp = pud_offset(pgdp, addr);
448 pmdp = pmd_offset(pudp, addr);
449 ptep = pte_offset(pmdp, addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /*
452 * If the page isn't marked valid, the page cannot possibly be
453 * in the cache.
454 */
Ralf Baechle526af352008-01-29 10:14:55 +0000455 if (!(pte_present(*ptep)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 return;
457
Ralf Baechledb813fe2007-09-27 18:26:43 +0100458 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
459 vaddr = NULL;
460 else {
461 /*
462 * Use kmap_coherent or kmap_atomic to do flushes for
463 * another ASID than the current one.
464 */
465 if (cpu_has_dc_aliases)
466 vaddr = kmap_coherent(page, addr);
467 else
468 vaddr = kmap_atomic(page, KM_USER0);
469 addr = (unsigned long)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100473 r4k_blast_dcache_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
475 if (exec) {
Ralf Baechledb813fe2007-09-27 18:26:43 +0100476 if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 int cpu = smp_processor_id();
478
Thiemo Seufer26a51b22005-02-19 13:32:02 +0000479 if (cpu_context(cpu, mm) != 0)
480 drop_mmu_context(mm, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 } else
Ralf Baechledb813fe2007-09-27 18:26:43 +0100482 r4k_blast_icache_page(addr);
483 }
484
485 if (vaddr) {
486 if (cpu_has_dc_aliases)
487 kunmap_coherent();
488 else
489 kunmap_atomic(vaddr, KM_USER0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491}
492
Ralf Baechle6ec25802005-10-12 00:02:34 +0100493static void r4k_flush_cache_page(struct vm_area_struct *vma,
494 unsigned long addr, unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 struct flush_cache_page_args args;
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 args.vma = vma;
Ralf Baechle6ec25802005-10-12 00:02:34 +0100499 args.addr = addr;
Atsushi Nemotode628932006-03-13 18:23:03 +0900500 args.pfn = pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100502 r4k_on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505static inline void local_r4k_flush_data_cache_page(void * addr)
506{
507 r4k_blast_dcache_page((unsigned long) addr);
508}
509
510static void r4k_flush_data_cache_page(unsigned long addr)
511{
Ralf Baechlea754f702007-11-03 01:01:37 +0000512 if (in_atomic())
513 local_r4k_flush_data_cache_page((void *)addr);
514 else
515 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr,
516 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519struct flush_icache_range_args {
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900520 unsigned long start;
521 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522};
523
524static inline void local_r4k_flush_icache_range(void *args)
525{
526 struct flush_icache_range_args *fir_args = args;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 unsigned long start = fir_args->start;
528 unsigned long end = fir_args->end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 if (!cpu_has_ic_fills_f_dc) {
Chris Dearman73f40352006-06-20 18:06:52 +0100531 if (end - start >= dcache_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 r4k_blast_dcache();
533 } else {
Thiemo Seufer10a3dab2005-09-09 20:26:54 +0000534 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900535 protected_blast_dcache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538
539 if (end - start > icache_size)
540 r4k_blast_icache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900541 else
542 protected_blast_icache_range(start, end);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Atsushi Nemotod4264f12006-01-29 02:27:51 +0900545static void r4k_flush_icache_range(unsigned long start, unsigned long end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 struct flush_icache_range_args args;
548
549 args.start = start;
550 args.end = end;
551
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100552 r4k_on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
Ralf Baechlecc61c1f2005-07-12 18:35:38 +0000553 instruction_hazard();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#ifdef CONFIG_DMA_NONCOHERENT
557
558static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
559{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* Catch bad driver code */
561 BUG_ON(size == 0);
562
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100563 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900564 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900566 else
567 blast_scache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return;
569 }
570
571 /*
572 * Either no secondary cache or the available caches don't have the
573 * subset property so we have to flush the primary caches
574 * explicitly
575 */
576 if (size >= dcache_size) {
577 r4k_blast_dcache();
578 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 R4600_HIT_CACHEOP_WAR_IMPL;
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900580 blast_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
582
583 bc_wback_inv(addr, size);
584}
585
586static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
587{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 /* Catch bad driver code */
589 BUG_ON(size == 0);
590
Ralf Baechlefc5d2d22006-07-06 13:04:01 +0100591 if (cpu_has_inclusive_pcaches) {
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900592 if (size >= scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 r4k_blast_scache();
Atsushi Nemoto41700e72006-02-10 00:39:06 +0900594 else
Thomas Bogendoerfere9c33572007-11-26 23:40:01 +0100595 blast_inv_scache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 return;
597 }
598
599 if (size >= dcache_size) {
600 r4k_blast_dcache();
601 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 R4600_HIT_CACHEOP_WAR_IMPL;
Thomas Bogendoerfere9c33572007-11-26 23:40:01 +0100603 blast_inv_dcache_range(addr, addr + size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 bc_inv(addr, size);
607}
608#endif /* CONFIG_DMA_NONCOHERENT */
609
610/*
611 * While we're protected against bad userland addresses we don't care
612 * very much about what happens in that case. Usually a segmentation
613 * fault will dump the process later on anyway ...
614 */
615static void local_r4k_flush_cache_sigtramp(void * arg)
616{
Thiemo Seufer02fe2c92005-09-09 19:45:41 +0000617 unsigned long ic_lsize = cpu_icache_line_size();
618 unsigned long dc_lsize = cpu_dcache_line_size();
619 unsigned long sc_lsize = cpu_scache_line_size();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 unsigned long addr = (unsigned long) arg;
621
622 R4600_HIT_CACHEOP_WAR_IMPL;
Chris Dearman73f40352006-06-20 18:06:52 +0100623 if (dc_lsize)
624 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
Ralf Baechle4debe4f2006-02-27 19:05:55 +0000625 if (!cpu_icache_snoops_remote_store && scache_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
Chris Dearman73f40352006-06-20 18:06:52 +0100627 if (ic_lsize)
628 protected_flush_icache_line(addr & ~(ic_lsize - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (MIPS4K_ICACHE_REFILL_WAR) {
630 __asm__ __volatile__ (
631 ".set push\n\t"
632 ".set noat\n\t"
633 ".set mips3\n\t"
Ralf Baechle875d43e2005-09-03 15:56:16 -0700634#ifdef CONFIG_32BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 "la $at,1f\n\t"
636#endif
Ralf Baechle875d43e2005-09-03 15:56:16 -0700637#ifdef CONFIG_64BIT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 "dla $at,1f\n\t"
639#endif
640 "cache %0,($at)\n\t"
641 "nop; nop; nop\n"
642 "1:\n\t"
643 ".set pop"
644 :
645 : "i" (Hit_Invalidate_I));
646 }
647 if (MIPS_CACHE_SYNC_WAR)
648 __asm__ __volatile__ ("sync");
649}
650
651static void r4k_flush_cache_sigtramp(unsigned long addr)
652{
Ralf Baechle7f3f1d02006-05-12 13:20:06 +0100653 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656static void r4k_flush_icache_all(void)
657{
658 if (cpu_has_vtag_icache)
659 r4k_blast_icache();
660}
661
662static inline void rm7k_erratum31(void)
663{
664 const unsigned long ic_lsize = 32;
665 unsigned long addr;
666
667 /* RM7000 erratum #31. The icache is screwed at startup. */
668 write_c0_taglo(0);
669 write_c0_taghi(0);
670
671 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
672 __asm__ __volatile__ (
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000673 ".set push\n\t"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 ".set noreorder\n\t"
675 ".set mips3\n\t"
676 "cache\t%1, 0(%0)\n\t"
677 "cache\t%1, 0x1000(%0)\n\t"
678 "cache\t%1, 0x2000(%0)\n\t"
679 "cache\t%1, 0x3000(%0)\n\t"
680 "cache\t%2, 0(%0)\n\t"
681 "cache\t%2, 0x1000(%0)\n\t"
682 "cache\t%2, 0x2000(%0)\n\t"
683 "cache\t%2, 0x3000(%0)\n\t"
684 "cache\t%1, 0(%0)\n\t"
685 "cache\t%1, 0x1000(%0)\n\t"
686 "cache\t%1, 0x2000(%0)\n\t"
687 "cache\t%1, 0x3000(%0)\n\t"
Thiemo Seuferd8748a32005-09-02 09:56:12 +0000688 ".set pop\n"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 :
690 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
691 }
692}
693
694static char *way_string[] __initdata = { NULL, "direct mapped", "2-way",
695 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
696};
697
698static void __init probe_pcache(void)
699{
700 struct cpuinfo_mips *c = &current_cpu_data;
701 unsigned int config = read_c0_config();
702 unsigned int prid = read_c0_prid();
703 unsigned long config1;
704 unsigned int lsize;
705
706 switch (c->cputype) {
707 case CPU_R4600: /* QED style two way caches? */
708 case CPU_R4700:
709 case CPU_R5000:
710 case CPU_NEVADA:
711 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
712 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
713 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900714 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
717 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
718 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900719 c->dcache.waybit= __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 c->options |= MIPS_CPU_CACHE_CDEX_P;
722 break;
723
724 case CPU_R5432:
725 case CPU_R5500:
726 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
727 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
728 c->icache.ways = 2;
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 = 2;
734 c->dcache.waybit = 0;
735
736 c->options |= MIPS_CPU_CACHE_CDEX_P;
737 break;
738
739 case CPU_TX49XX:
740 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
741 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
742 c->icache.ways = 4;
743 c->icache.waybit= 0;
744
745 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
746 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
747 c->dcache.ways = 4;
748 c->dcache.waybit = 0;
749
750 c->options |= MIPS_CPU_CACHE_CDEX_P;
Atsushi Nemotode862b42006-03-17 12:59:22 +0900751 c->options |= MIPS_CPU_PREFETCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 break;
753
754 case CPU_R4000PC:
755 case CPU_R4000SC:
756 case CPU_R4000MC:
757 case CPU_R4400PC:
758 case CPU_R4400SC:
759 case CPU_R4400MC:
760 case CPU_R4300:
761 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
762 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
763 c->icache.ways = 1;
764 c->icache.waybit = 0; /* doesn't matter */
765
766 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
767 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
768 c->dcache.ways = 1;
769 c->dcache.waybit = 0; /* does not matter */
770
771 c->options |= MIPS_CPU_CACHE_CDEX_P;
772 break;
773
774 case CPU_R10000:
775 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400776 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
778 c->icache.linesz = 64;
779 c->icache.ways = 2;
780 c->icache.waybit = 0;
781
782 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
783 c->dcache.linesz = 32;
784 c->dcache.ways = 2;
785 c->dcache.waybit = 0;
786
787 c->options |= MIPS_CPU_PREFETCH;
788 break;
789
790 case CPU_VR4133:
Yoichi Yuasa2874fe52006-07-08 00:42:12 +0900791 write_c0_config(config & ~VR41_CONF_P4K);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 case CPU_VR4131:
793 /* Workaround for cache instruction bug of VR4131 */
794 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
795 c->processor_id == 0x0c82U) {
Yoichi Yuasa4e8ab362006-07-04 22:59:41 +0900796 config |= 0x00400000U;
797 if (c->processor_id == 0x0c80U)
798 config |= VR41_CONF_BP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 write_c0_config(config);
Yoichi Yuasa1058ecd2006-07-08 00:42:01 +0900800 } else
801 c->options |= MIPS_CPU_CACHE_CDEX_P;
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
804 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
805 c->icache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900806 c->icache.waybit = __ffs(icache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
809 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
810 c->dcache.ways = 2;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900811 c->dcache.waybit = __ffs(dcache_size/2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813
814 case CPU_VR41XX:
815 case CPU_VR4111:
816 case CPU_VR4121:
817 case CPU_VR4122:
818 case CPU_VR4181:
819 case CPU_VR4181A:
820 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
821 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
822 c->icache.ways = 1;
823 c->icache.waybit = 0; /* doesn't matter */
824
825 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
826 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
827 c->dcache.ways = 1;
828 c->dcache.waybit = 0; /* does not matter */
829
830 c->options |= MIPS_CPU_CACHE_CDEX_P;
831 break;
832
833 case CPU_RM7000:
834 rm7k_erratum31();
835
836 case CPU_RM9000:
837 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
838 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
839 c->icache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900840 c->icache.waybit = __ffs(icache_size / c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
843 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
844 c->dcache.ways = 4;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900845 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847#if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
848 c->options |= MIPS_CPU_CACHE_CDEX_P;
849#endif
850 c->options |= MIPS_CPU_PREFETCH;
851 break;
852
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800853 case CPU_LOONGSON2:
854 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
855 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
856 if (prid & 0x3)
857 c->icache.ways = 4;
858 else
859 c->icache.ways = 2;
860 c->icache.waybit = 0;
861
862 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
863 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
864 if (prid & 0x3)
865 c->dcache.ways = 4;
866 else
867 c->dcache.ways = 2;
868 c->dcache.waybit = 0;
869 break;
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 default:
872 if (!(config & MIPS_CONF_M))
873 panic("Don't know how to probe P-caches on this cpu.");
874
875 /*
876 * So we seem to be a MIPS32 or MIPS64 CPU
877 * So let's probe the I-cache ...
878 */
879 config1 = read_c0_config1();
880
881 if ((lsize = ((config1 >> 19) & 7)))
882 c->icache.linesz = 2 << lsize;
883 else
884 c->icache.linesz = lsize;
885 c->icache.sets = 64 << ((config1 >> 22) & 7);
886 c->icache.ways = 1 + ((config1 >> 16) & 7);
887
888 icache_size = c->icache.sets *
889 c->icache.ways *
890 c->icache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900891 c->icache.waybit = __ffs(icache_size/c->icache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 if (config & 0x8) /* VI bit */
894 c->icache.flags |= MIPS_CACHE_VTAG;
895
896 /*
897 * Now probe the MIPS32 / MIPS64 data cache.
898 */
899 c->dcache.flags = 0;
900
901 if ((lsize = ((config1 >> 10) & 7)))
902 c->dcache.linesz = 2 << lsize;
903 else
904 c->dcache.linesz= lsize;
905 c->dcache.sets = 64 << ((config1 >> 13) & 7);
906 c->dcache.ways = 1 + ((config1 >> 7) & 7);
907
908 dcache_size = c->dcache.sets *
909 c->dcache.ways *
910 c->dcache.linesz;
Atsushi Nemoto3c68da72006-04-08 01:33:31 +0900911 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
913 c->options |= MIPS_CPU_PREFETCH;
914 break;
915 }
916
917 /*
918 * Processor configuration sanity check for the R4000SC erratum
919 * #5. With page sizes larger than 32kB there is no possibility
920 * to get a VCE exception anymore so we don't care about this
921 * misconfiguration. The case is rather theoretical anyway;
922 * presumably no vendor is shipping his hardware in the "bad"
923 * configuration.
924 */
925 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
926 !(config & CONF_SC) && c->icache.linesz != 16 &&
927 PAGE_SIZE <= 0x8000)
928 panic("Improper R4000SC processor configuration detected");
929
930 /* compute a couple of other cache variables */
931 c->icache.waysize = icache_size / c->icache.ways;
932 c->dcache.waysize = dcache_size / c->dcache.ways;
933
Chris Dearman73f40352006-06-20 18:06:52 +0100934 c->icache.sets = c->icache.linesz ?
935 icache_size / (c->icache.linesz * c->icache.ways) : 0;
936 c->dcache.sets = c->dcache.linesz ?
937 dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /*
940 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
941 * 2-way virtually indexed so normally would suffer from aliases. So
942 * normally they'd suffer from aliases but magic in the hardware deals
943 * with that for us so we don't need to take care ourselves.
944 */
Ralf Baechled1e344e2005-02-04 15:51:26 +0000945 switch (c->cputype) {
Ralf Baechlea95970f2005-02-07 21:41:32 +0000946 case CPU_20KC:
Ralf Baechle505403b2005-02-07 21:53:39 +0000947 case CPU_25KF:
Ralf Baechle641e97f2007-10-11 23:46:05 +0100948 case CPU_SB1:
949 case CPU_SB1A:
Atsushi Nemotode628932006-03-13 18:23:03 +0900950 c->dcache.flags |= MIPS_CACHE_PINDEX;
Ralf Baechle641e97f2007-10-11 23:46:05 +0100951 break;
952
Ralf Baechled1e344e2005-02-04 15:51:26 +0000953 case CPU_R10000:
954 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -0400955 case CPU_R14000:
Ralf Baechled1e344e2005-02-04 15:51:26 +0000956 break;
Ralf Baechle641e97f2007-10-11 23:46:05 +0100957
Ralf Baechled1e344e2005-02-04 15:51:26 +0000958 case CPU_24K:
Nigel Stephens98a41de2006-04-27 15:50:32 +0100959 case CPU_34K:
Ralf Baechle2e78ae32006-06-23 18:48:21 +0100960 case CPU_74K:
Ralf Baechlebeab3752006-06-19 21:56:25 +0100961 if ((read_c0_config7() & (1 << 16))) {
962 /* effectively physically indexed dcache,
963 thus no virtual aliases. */
964 c->dcache.flags |= MIPS_CACHE_PINDEX;
965 break;
966 }
Ralf Baechled1e344e2005-02-04 15:51:26 +0000967 default:
Ralf Baechlebeab3752006-06-19 21:56:25 +0100968 if (c->dcache.waysize > PAGE_SIZE)
969 c->dcache.flags |= MIPS_CACHE_ALIASES;
Ralf Baechled1e344e2005-02-04 15:51:26 +0000970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 switch (c->cputype) {
973 case CPU_20KC:
974 /*
975 * Some older 20Kc chips doesn't have the 'VI' bit in
976 * the config register.
977 */
978 c->icache.flags |= MIPS_CACHE_VTAG;
979 break;
980
Pete Popove3ad1c22005-03-01 06:33:16 +0000981 case CPU_AU1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 case CPU_AU1500:
Pete Popove3ad1c22005-03-01 06:33:16 +0000983 case CPU_AU1100:
984 case CPU_AU1550:
985 case CPU_AU1200:
Manuel Lauss237cfee2007-12-06 09:07:55 +0100986 case CPU_AU1210:
987 case CPU_AU1250:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 c->icache.flags |= MIPS_CACHE_IC_F_DC;
989 break;
990 }
991
Fuxin Zhang2a21c732007-06-06 14:52:43 +0800992#ifdef CONFIG_CPU_LOONGSON2
993 /*
994 * LOONGSON2 has 4 way icache, but when using indexed cache op,
995 * one op will act on all 4 ways
996 */
997 c->icache.ways = 1;
998#endif
999
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1001 icache_size >> 10,
Ralf Baechle64bfca52007-10-15 16:35:45 +01001002 cpu_has_vtag_icache ? "VIVT" : "VIPT",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 way_string[c->icache.ways], c->icache.linesz);
1004
Ralf Baechle64bfca52007-10-15 16:35:45 +01001005 printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
1006 dcache_size >> 10, way_string[c->dcache.ways],
1007 (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
1008 (c->dcache.flags & MIPS_CACHE_ALIASES) ?
1009 "cache aliases" : "no aliases",
1010 c->dcache.linesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
1013/*
1014 * If you even _breathe_ on this function, look at the gcc output and make sure
1015 * it does not pop things on and off the stack for the cache sizing loop that
1016 * executes in KSEG1 space or else you will crash and burn badly. You have
1017 * been warned.
1018 */
1019static int __init probe_scache(void)
1020{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 unsigned long flags, addr, begin, end, pow2;
1022 unsigned int config = read_c0_config();
1023 struct cpuinfo_mips *c = &current_cpu_data;
1024 int tmp;
1025
1026 if (config & CONF_SC)
1027 return 0;
1028
Ralf Baechlee001e522007-07-28 12:45:47 +01001029 begin = (unsigned long) &_stext;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 begin &= ~((4 * 1024 * 1024) - 1);
1031 end = begin + (4 * 1024 * 1024);
1032
1033 /*
1034 * This is such a bitch, you'd think they would make it easy to do
1035 * this. Away you daemons of stupidity!
1036 */
1037 local_irq_save(flags);
1038
1039 /* Fill each size-multiple cache line with a valid tag. */
1040 pow2 = (64 * 1024);
1041 for (addr = begin; addr < end; addr = (begin + pow2)) {
1042 unsigned long *p = (unsigned long *) addr;
1043 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1044 pow2 <<= 1;
1045 }
1046
1047 /* Load first line with zero (therefore invalid) tag. */
1048 write_c0_taglo(0);
1049 write_c0_taghi(0);
1050 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1051 cache_op(Index_Store_Tag_I, begin);
1052 cache_op(Index_Store_Tag_D, begin);
1053 cache_op(Index_Store_Tag_SD, begin);
1054
1055 /* Now search for the wrap around point. */
1056 pow2 = (128 * 1024);
1057 tmp = 0;
1058 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1059 cache_op(Index_Load_Tag_SD, addr);
1060 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1061 if (!read_c0_taglo())
1062 break;
1063 pow2 <<= 1;
1064 }
1065 local_irq_restore(flags);
1066 addr -= begin;
1067
1068 scache_size = addr;
1069 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1070 c->scache.ways = 1;
1071 c->dcache.waybit = 0; /* does not matter */
1072
1073 return 1;
1074}
1075
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001076#if defined(CONFIG_CPU_LOONGSON2)
1077static void __init loongson2_sc_init(void)
1078{
1079 struct cpuinfo_mips *c = &current_cpu_data;
1080
1081 scache_size = 512*1024;
1082 c->scache.linesz = 32;
1083 c->scache.ways = 4;
1084 c->scache.waybit = 0;
1085 c->scache.waysize = scache_size / (c->scache.ways);
1086 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1087 pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1088 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1089
1090 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1091}
1092#endif
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094extern int r5k_sc_init(void);
1095extern int rm7k_sc_init(void);
Chris Dearman9318c512006-06-20 17:15:20 +01001096extern int mips_sc_init(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098static void __init setup_scache(void)
1099{
1100 struct cpuinfo_mips *c = &current_cpu_data;
1101 unsigned int config = read_c0_config();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 int sc_present = 0;
1103
1104 /*
1105 * Do the probing thing on R4000SC and R4400SC processors. Other
1106 * processors don't have a S-cache that would be relevant to the
Joe Perches603e82e2008-02-03 16:54:53 +02001107 * Linux memory management.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 */
1109 switch (c->cputype) {
1110 case CPU_R4000SC:
1111 case CPU_R4000MC:
1112 case CPU_R4400SC:
1113 case CPU_R4400MC:
Thiemo Seuferba5187d2005-04-25 16:36:23 +00001114 sc_present = run_uncached(probe_scache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (sc_present)
1116 c->options |= MIPS_CPU_CACHE_CDEX_S;
1117 break;
1118
1119 case CPU_R10000:
1120 case CPU_R12000:
Kumba44d921b2006-05-16 22:23:59 -04001121 case CPU_R14000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1123 c->scache.linesz = 64 << ((config >> 13) & 1);
1124 c->scache.ways = 2;
1125 c->scache.waybit= 0;
1126 sc_present = 1;
1127 break;
1128
1129 case CPU_R5000:
1130 case CPU_NEVADA:
1131#ifdef CONFIG_R5000_CPU_SCACHE
1132 r5k_sc_init();
1133#endif
1134 return;
1135
1136 case CPU_RM7000:
1137 case CPU_RM9000:
1138#ifdef CONFIG_RM7000_CPU_SCACHE
1139 rm7k_sc_init();
1140#endif
1141 return;
1142
Fuxin Zhang2a21c732007-06-06 14:52:43 +08001143#if defined(CONFIG_CPU_LOONGSON2)
1144 case CPU_LOONGSON2:
1145 loongson2_sc_init();
1146 return;
1147#endif
1148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 default:
Chris Dearman9318c512006-06-20 17:15:20 +01001150 if (c->isa_level == MIPS_CPU_ISA_M32R1 ||
1151 c->isa_level == MIPS_CPU_ISA_M32R2 ||
1152 c->isa_level == MIPS_CPU_ISA_M64R1 ||
1153 c->isa_level == MIPS_CPU_ISA_M64R2) {
1154#ifdef CONFIG_MIPS_CPU_SCACHE
1155 if (mips_sc_init ()) {
1156 scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
1157 printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
1158 scache_size >> 10,
1159 way_string[c->scache.ways], c->scache.linesz);
1160 }
1161#else
1162 if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1163 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1164#endif
1165 return;
1166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 sc_present = 0;
1168 }
1169
1170 if (!sc_present)
1171 return;
1172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 /* compute a couple of other cache variables */
1174 c->scache.waysize = scache_size / c->scache.ways;
1175
1176 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1177
1178 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1179 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1180
Ralf Baechlefc5d2d22006-07-06 13:04:01 +01001181 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001184void au1x00_fixup_config_od(void)
1185{
1186 /*
1187 * c0_config.od (bit 19) was write only (and read as 0)
1188 * on the early revisions of Alchemy SOCs. It disables the bus
1189 * transaction overlapping and needs to be set to fix various errata.
1190 */
1191 switch (read_c0_prid()) {
1192 case 0x00030100: /* Au1000 DA */
1193 case 0x00030201: /* Au1000 HA */
1194 case 0x00030202: /* Au1000 HB */
1195 case 0x01030200: /* Au1500 AB */
1196 /*
1197 * Au1100 errata actually keeps silence about this bit, so we set it
1198 * just in case for those revisions that require it to be set according
1199 * to arch/mips/au1000/common/cputable.c
1200 */
1201 case 0x02030200: /* Au1100 AB */
1202 case 0x02030201: /* Au1100 BA */
1203 case 0x02030202: /* Au1100 BC */
1204 set_c0_config(1 << 19);
1205 break;
1206 }
1207}
1208
Ralf Baechlea00f6312006-08-01 23:39:42 +01001209static void __init coherency_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
1211 change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
1212
1213 /*
1214 * c0_status.cu=0 specifies that updates by the sc instruction use
1215 * the coherency mode specified by the TLB; 1 means cachable
1216 * coherent update on write will be used. Not all processors have
1217 * this bit and; some wire it to zero, others like Toshiba had the
1218 * silly idea of putting something else there ...
1219 */
Ralf Baechle10cc3522007-10-11 23:46:15 +01001220 switch (current_cpu_type()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 case CPU_R4000PC:
1222 case CPU_R4000SC:
1223 case CPU_R4000MC:
1224 case CPU_R4400PC:
1225 case CPU_R4400SC:
1226 case CPU_R4400MC:
1227 clear_c0_config(CONF_CU);
1228 break;
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001229 /*
Ralf Baechledf586d52006-08-01 23:42:30 +01001230 * We need to catch the early Alchemy SOCs with
Sergei Shtylyov9370b352006-05-26 19:44:54 +04001231 * the write-only co_config.od bit and set it back to one...
1232 */
1233 case CPU_AU1000: /* rev. DA, HA, HB */
1234 case CPU_AU1100: /* rev. AB, BA, BC ?? */
1235 case CPU_AU1500: /* rev. AB */
1236 au1x00_fixup_config_od();
1237 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 }
1239}
1240
Ralf Baechle02cf2112005-10-01 13:06:32 +01001241void __init r4k_cache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
1243 extern void build_clear_page(void);
1244 extern void build_copy_page(void);
Ralf Baechle641e97f2007-10-11 23:46:05 +01001245 extern char __weak except_vec2_generic;
1246 extern char __weak except_vec2_sb1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct cpuinfo_mips *c = &current_cpu_data;
1248
Ralf Baechle641e97f2007-10-11 23:46:05 +01001249 switch (c->cputype) {
1250 case CPU_SB1:
1251 case CPU_SB1A:
1252 set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1253 break;
1254
1255 default:
1256 set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1257 break;
1258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 probe_pcache();
1261 setup_scache();
1262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 r4k_blast_dcache_page_setup();
1264 r4k_blast_dcache_page_indexed_setup();
1265 r4k_blast_dcache_setup();
1266 r4k_blast_icache_page_setup();
1267 r4k_blast_icache_page_indexed_setup();
1268 r4k_blast_icache_setup();
1269 r4k_blast_scache_page_setup();
1270 r4k_blast_scache_page_indexed_setup();
1271 r4k_blast_scache_setup();
1272
1273 /*
1274 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1275 * This code supports virtually indexed processors and will be
1276 * unnecessarily inefficient on physically indexed processors.
1277 */
Chris Dearman73f40352006-06-20 18:06:52 +01001278 if (c->dcache.linesz)
1279 shm_align_mask = max_t( unsigned long,
1280 c->dcache.sets * c->dcache.linesz - 1,
1281 PAGE_SIZE - 1);
1282 else
1283 shm_align_mask = PAGE_SIZE-1;
Ralf Baechledb813fe2007-09-27 18:26:43 +01001284 flush_cache_all = cache_noop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 __flush_cache_all = r4k___flush_cache_all;
1286 flush_cache_mm = r4k_flush_cache_mm;
1287 flush_cache_page = r4k_flush_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 flush_cache_range = r4k_flush_cache_range;
1289
1290 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1291 flush_icache_all = r4k_flush_icache_all;
Ralf Baechle7e3bfc72006-04-05 20:42:04 +01001292 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 flush_data_cache_page = r4k_flush_data_cache_page;
1294 flush_icache_range = r4k_flush_icache_range;
1295
1296#ifdef CONFIG_DMA_NONCOHERENT
1297 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1298 _dma_cache_wback = r4k_dma_cache_wback_inv;
1299 _dma_cache_inv = r4k_dma_cache_inv;
1300#endif
1301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 build_clear_page();
1303 build_copy_page();
Ralf Baechle1d40cfc2005-07-15 15:23:23 +00001304 local_r4k___flush_cache_all(NULL);
1305 coherency_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}