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
6 * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
7 * Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Ralf Baechle (ralf@gnu.org)
8 * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
10 #include <linux/hardirq.h>
11 #include <linux/init.h>
12 #include <linux/highmem.h>
13 #include <linux/kernel.h>
14 #include <linux/linkage.h>
15 #include <linux/preempt.h>
16 #include <linux/sched.h>
17 #include <linux/smp.h>
19 #include <linux/module.h>
20 #include <linux/bitops.h>
22 #include <asm/bcache.h>
23 #include <asm/bootinfo.h>
24 #include <asm/cache.h>
25 #include <asm/cacheops.h>
27 #include <asm/cpu-features.h>
28 #include <asm/cpu-type.h>
31 #include <asm/pgtable.h>
32 #include <asm/r4kcache.h>
33 #include <asm/sections.h>
34 #include <asm/mmu_context.h>
36 #include <asm/cacheflush.h> /* for run_uncached() */
37 #include <asm/traps.h>
38 #include <asm/dma-coherence.h>
41 * Special Variant of smp_call_function for use by cache functions:
44 * o collapses to normal function call on UP kernels
45 * o collapses to normal function call on systems with a single shared
47 * o doesn't disable interrupts on the local CPU
49 static inline void r4k_on_each_cpu(void (*func) (void *info), void *info)
53 #if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
54 smp_call_function(func, info, 1);
60 #if defined(CONFIG_MIPS_CMP) || defined(CONFIG_MIPS_CPS)
61 #define cpu_has_safe_index_cacheops 0
63 #define cpu_has_safe_index_cacheops 1
69 static unsigned long icache_size __read_mostly;
70 static unsigned long dcache_size __read_mostly;
71 static unsigned long scache_size __read_mostly;
74 * Dummy cache handling routines for machines without boardcaches
76 static void cache_noop(void) {}
78 static struct bcache_ops no_sc_ops = {
79 .bc_enable = (void *)cache_noop,
80 .bc_disable = (void *)cache_noop,
81 .bc_wback_inv = (void *)cache_noop,
82 .bc_inv = (void *)cache_noop
85 struct bcache_ops *bcops = &no_sc_ops;
87 #define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
88 #define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
90 #define R4600_HIT_CACHEOP_WAR_IMPL \
92 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
93 *(volatile unsigned long *)CKSEG1; \
94 if (R4600_V1_HIT_CACHEOP_WAR) \
95 __asm__ __volatile__("nop;nop;nop;nop"); \
98 static void (*r4k_blast_dcache_page)(unsigned long addr);
100 static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
102 R4600_HIT_CACHEOP_WAR_IMPL;
103 blast_dcache32_page(addr);
106 static inline void r4k_blast_dcache_page_dc64(unsigned long addr)
108 R4600_HIT_CACHEOP_WAR_IMPL;
109 blast_dcache64_page(addr);
112 static void r4k_blast_dcache_page_setup(void)
114 unsigned long dc_lsize = cpu_dcache_line_size();
117 r4k_blast_dcache_page = (void *)cache_noop;
118 else if (dc_lsize == 16)
119 r4k_blast_dcache_page = blast_dcache16_page;
120 else if (dc_lsize == 32)
121 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
122 else if (dc_lsize == 64)
123 r4k_blast_dcache_page = r4k_blast_dcache_page_dc64;
127 #define r4k_blast_dcache_user_page r4k_blast_dcache_page
130 static void (*r4k_blast_dcache_user_page)(unsigned long addr);
132 static void r4k_blast_dcache_user_page_setup(void)
134 unsigned long dc_lsize = cpu_dcache_line_size();
137 r4k_blast_dcache_user_page = (void *)cache_noop;
138 else if (dc_lsize == 16)
139 r4k_blast_dcache_user_page = blast_dcache16_user_page;
140 else if (dc_lsize == 32)
141 r4k_blast_dcache_user_page = blast_dcache32_user_page;
142 else if (dc_lsize == 64)
143 r4k_blast_dcache_user_page = blast_dcache64_user_page;
148 static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
150 static void r4k_blast_dcache_page_indexed_setup(void)
152 unsigned long dc_lsize = cpu_dcache_line_size();
155 r4k_blast_dcache_page_indexed = (void *)cache_noop;
156 else if (dc_lsize == 16)
157 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
158 else if (dc_lsize == 32)
159 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
160 else if (dc_lsize == 64)
161 r4k_blast_dcache_page_indexed = blast_dcache64_page_indexed;
164 void (* r4k_blast_dcache)(void);
165 EXPORT_SYMBOL(r4k_blast_dcache);
167 static void r4k_blast_dcache_setup(void)
169 unsigned long dc_lsize = cpu_dcache_line_size();
172 r4k_blast_dcache = (void *)cache_noop;
173 else if (dc_lsize == 16)
174 r4k_blast_dcache = blast_dcache16;
175 else if (dc_lsize == 32)
176 r4k_blast_dcache = blast_dcache32;
177 else if (dc_lsize == 64)
178 r4k_blast_dcache = blast_dcache64;
181 /* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
182 #define JUMP_TO_ALIGN(order) \
183 __asm__ __volatile__( \
185 ".align\t" #order "\n\t" \
188 #define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
189 #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
191 static inline void blast_r4600_v1_icache32(void)
195 local_irq_save(flags);
197 local_irq_restore(flags);
200 static inline void tx49_blast_icache32(void)
202 unsigned long start = INDEX_BASE;
203 unsigned long end = start + current_cpu_data.icache.waysize;
204 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
205 unsigned long ws_end = current_cpu_data.icache.ways <<
206 current_cpu_data.icache.waybit;
207 unsigned long ws, addr;
209 CACHE32_UNROLL32_ALIGN2;
210 /* I'm in even chunk. blast odd chunks */
211 for (ws = 0; ws < ws_end; ws += ws_inc)
212 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
213 cache32_unroll32(addr|ws, Index_Invalidate_I);
214 CACHE32_UNROLL32_ALIGN;
215 /* I'm in odd chunk. blast even chunks */
216 for (ws = 0; ws < ws_end; ws += ws_inc)
217 for (addr = start; addr < end; addr += 0x400 * 2)
218 cache32_unroll32(addr|ws, Index_Invalidate_I);
221 static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
225 local_irq_save(flags);
226 blast_icache32_page_indexed(page);
227 local_irq_restore(flags);
230 static inline void tx49_blast_icache32_page_indexed(unsigned long page)
232 unsigned long indexmask = current_cpu_data.icache.waysize - 1;
233 unsigned long start = INDEX_BASE + (page & indexmask);
234 unsigned long end = start + PAGE_SIZE;
235 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
236 unsigned long ws_end = current_cpu_data.icache.ways <<
237 current_cpu_data.icache.waybit;
238 unsigned long ws, addr;
240 CACHE32_UNROLL32_ALIGN2;
241 /* I'm in even chunk. blast odd chunks */
242 for (ws = 0; ws < ws_end; ws += ws_inc)
243 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
244 cache32_unroll32(addr|ws, Index_Invalidate_I);
245 CACHE32_UNROLL32_ALIGN;
246 /* I'm in odd chunk. blast even chunks */
247 for (ws = 0; ws < ws_end; ws += ws_inc)
248 for (addr = start; addr < end; addr += 0x400 * 2)
249 cache32_unroll32(addr|ws, Index_Invalidate_I);
252 static void (* r4k_blast_icache_page)(unsigned long addr);
254 static void r4k_blast_icache_page_setup(void)
256 unsigned long ic_lsize = cpu_icache_line_size();
259 r4k_blast_icache_page = (void *)cache_noop;
260 else if (ic_lsize == 16)
261 r4k_blast_icache_page = blast_icache16_page;
262 else if (ic_lsize == 32 && current_cpu_type() == CPU_LOONGSON2)
263 r4k_blast_icache_page = loongson2_blast_icache32_page;
264 else if (ic_lsize == 32)
265 r4k_blast_icache_page = blast_icache32_page;
266 else if (ic_lsize == 64)
267 r4k_blast_icache_page = blast_icache64_page;
271 #define r4k_blast_icache_user_page r4k_blast_icache_page
274 static void (*r4k_blast_icache_user_page)(unsigned long addr);
276 static void __cpuinit r4k_blast_icache_user_page_setup(void)
278 unsigned long ic_lsize = cpu_icache_line_size();
281 r4k_blast_icache_user_page = (void *)cache_noop;
282 else if (ic_lsize == 16)
283 r4k_blast_icache_user_page = blast_icache16_user_page;
284 else if (ic_lsize == 32)
285 r4k_blast_icache_user_page = blast_icache32_user_page;
286 else if (ic_lsize == 64)
287 r4k_blast_icache_user_page = blast_icache64_user_page;
292 static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
294 static void r4k_blast_icache_page_indexed_setup(void)
296 unsigned long ic_lsize = cpu_icache_line_size();
299 r4k_blast_icache_page_indexed = (void *)cache_noop;
300 else if (ic_lsize == 16)
301 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
302 else if (ic_lsize == 32) {
303 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
304 r4k_blast_icache_page_indexed =
305 blast_icache32_r4600_v1_page_indexed;
306 else if (TX49XX_ICACHE_INDEX_INV_WAR)
307 r4k_blast_icache_page_indexed =
308 tx49_blast_icache32_page_indexed;
309 else if (current_cpu_type() == CPU_LOONGSON2)
310 r4k_blast_icache_page_indexed =
311 loongson2_blast_icache32_page_indexed;
313 r4k_blast_icache_page_indexed =
314 blast_icache32_page_indexed;
315 } else if (ic_lsize == 64)
316 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
319 void (* r4k_blast_icache)(void);
320 EXPORT_SYMBOL(r4k_blast_icache);
322 static void r4k_blast_icache_setup(void)
324 unsigned long ic_lsize = cpu_icache_line_size();
327 r4k_blast_icache = (void *)cache_noop;
328 else if (ic_lsize == 16)
329 r4k_blast_icache = blast_icache16;
330 else if (ic_lsize == 32) {
331 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
332 r4k_blast_icache = blast_r4600_v1_icache32;
333 else if (TX49XX_ICACHE_INDEX_INV_WAR)
334 r4k_blast_icache = tx49_blast_icache32;
335 else if (current_cpu_type() == CPU_LOONGSON2)
336 r4k_blast_icache = loongson2_blast_icache32;
338 r4k_blast_icache = blast_icache32;
339 } else if (ic_lsize == 64)
340 r4k_blast_icache = blast_icache64;
343 static void (* r4k_blast_scache_page)(unsigned long addr);
345 static void r4k_blast_scache_page_setup(void)
347 unsigned long sc_lsize = cpu_scache_line_size();
349 if (scache_size == 0)
350 r4k_blast_scache_page = (void *)cache_noop;
351 else if (sc_lsize == 16)
352 r4k_blast_scache_page = blast_scache16_page;
353 else if (sc_lsize == 32)
354 r4k_blast_scache_page = blast_scache32_page;
355 else if (sc_lsize == 64)
356 r4k_blast_scache_page = blast_scache64_page;
357 else if (sc_lsize == 128)
358 r4k_blast_scache_page = blast_scache128_page;
361 static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
363 static void r4k_blast_scache_page_indexed_setup(void)
365 unsigned long sc_lsize = cpu_scache_line_size();
367 if (scache_size == 0)
368 r4k_blast_scache_page_indexed = (void *)cache_noop;
369 else if (sc_lsize == 16)
370 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
371 else if (sc_lsize == 32)
372 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
373 else if (sc_lsize == 64)
374 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
375 else if (sc_lsize == 128)
376 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
379 static void (* r4k_blast_scache)(void);
381 static void r4k_blast_scache_setup(void)
383 unsigned long sc_lsize = cpu_scache_line_size();
385 if (scache_size == 0)
386 r4k_blast_scache = (void *)cache_noop;
387 else if (sc_lsize == 16)
388 r4k_blast_scache = blast_scache16;
389 else if (sc_lsize == 32)
390 r4k_blast_scache = blast_scache32;
391 else if (sc_lsize == 64)
392 r4k_blast_scache = blast_scache64;
393 else if (sc_lsize == 128)
394 r4k_blast_scache = blast_scache128;
397 static inline void local_r4k___flush_cache_all(void * args)
399 switch (current_cpu_type()) {
409 * These caches are inclusive caches, that is, if something
410 * is not cached in the S-cache, we know it also won't be
411 * in one of the primary caches.
423 static void r4k___flush_cache_all(void)
425 r4k_on_each_cpu(local_r4k___flush_cache_all, NULL);
428 static inline int has_valid_asid(const struct mm_struct *mm)
430 #if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
433 for_each_online_cpu(i)
434 if (cpu_context(i, mm))
439 return cpu_context(smp_processor_id(), mm);
443 static void r4k__flush_cache_vmap(void)
448 static void r4k__flush_cache_vunmap(void)
453 static inline void local_r4k_flush_cache_range(void * args)
455 struct vm_area_struct *vma = args;
456 int exec = vma->vm_flags & VM_EXEC;
458 if (!(has_valid_asid(vma->vm_mm)))
466 static void r4k_flush_cache_range(struct vm_area_struct *vma,
467 unsigned long start, unsigned long end)
469 int exec = vma->vm_flags & VM_EXEC;
471 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
472 r4k_on_each_cpu(local_r4k_flush_cache_range, vma);
475 static inline void local_r4k_flush_cache_mm(void * args)
477 struct mm_struct *mm = args;
479 if (!has_valid_asid(mm))
483 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
484 * only flush the primary caches but R10000 and R12000 behave sane ...
485 * R4000SC and R4400SC indexed S-cache ops also invalidate primary
486 * caches, so we can bail out early.
488 if (current_cpu_type() == CPU_R4000SC ||
489 current_cpu_type() == CPU_R4000MC ||
490 current_cpu_type() == CPU_R4400SC ||
491 current_cpu_type() == CPU_R4400MC) {
499 static void r4k_flush_cache_mm(struct mm_struct *mm)
501 if (!cpu_has_dc_aliases)
504 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm);
507 struct flush_cache_page_args {
508 struct vm_area_struct *vma;
513 static inline void local_r4k_flush_cache_page(void *args)
515 struct flush_cache_page_args *fcp_args = args;
516 struct vm_area_struct *vma = fcp_args->vma;
517 unsigned long addr = fcp_args->addr;
518 struct page *page = pfn_to_page(fcp_args->pfn);
519 int exec = vma->vm_flags & VM_EXEC;
520 struct mm_struct *mm = vma->vm_mm;
521 int map_coherent = 0;
529 * If ownes no valid ASID yet, cannot possibly have gotten
530 * this page into the cache.
532 if (!has_valid_asid(mm))
536 pgdp = pgd_offset(mm, addr);
537 pudp = pud_offset(pgdp, addr);
538 pmdp = pmd_offset(pudp, addr);
539 ptep = pte_offset(pmdp, addr);
542 * If the page isn't marked valid, the page cannot possibly be
545 if (!(pte_present(*ptep)))
548 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
552 * Use kmap_coherent or kmap_atomic to do flushes for
553 * another ASID than the current one.
555 map_coherent = (cpu_has_dc_aliases &&
556 page_mapped(page) && !Page_dcache_dirty(page));
558 vaddr = kmap_coherent(page, addr);
560 vaddr = kmap_atomic(page);
561 addr = (unsigned long)vaddr;
564 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
565 vaddr ? r4k_blast_dcache_page(addr) :
566 r4k_blast_dcache_user_page(addr);
567 if (exec && !cpu_icache_snoops_remote_store)
568 r4k_blast_scache_page(addr);
571 if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
572 int cpu = smp_processor_id();
574 if (cpu_context(cpu, mm) != 0)
575 drop_mmu_context(mm, cpu);
577 vaddr ? r4k_blast_icache_page(addr) :
578 r4k_blast_icache_user_page(addr);
585 kunmap_atomic(vaddr);
589 static void r4k_flush_cache_page(struct vm_area_struct *vma,
590 unsigned long addr, unsigned long pfn)
592 struct flush_cache_page_args args;
598 r4k_on_each_cpu(local_r4k_flush_cache_page, &args);
601 static inline void local_r4k_flush_data_cache_page(void * addr)
603 r4k_blast_dcache_page((unsigned long) addr);
606 static void r4k_flush_data_cache_page(unsigned long addr)
609 local_r4k_flush_data_cache_page((void *)addr);
611 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr);
614 struct flush_icache_range_args {
619 static inline void local_r4k_flush_icache_range(unsigned long start, unsigned long end)
621 if (!cpu_has_ic_fills_f_dc) {
622 if (end - start >= dcache_size) {
625 R4600_HIT_CACHEOP_WAR_IMPL;
626 protected_blast_dcache_range(start, end);
630 if (end - start > icache_size)
633 switch (boot_cpu_type()) {
635 protected_loongson2_blast_icache_range(start, end);
639 protected_blast_icache_range(start, end);
645 static inline void local_r4k_flush_icache_range_ipi(void *args)
647 struct flush_icache_range_args *fir_args = args;
648 unsigned long start = fir_args->start;
649 unsigned long end = fir_args->end;
651 local_r4k_flush_icache_range(start, end);
654 static void r4k_flush_icache_range(unsigned long start, unsigned long end)
656 struct flush_icache_range_args args;
661 r4k_on_each_cpu(local_r4k_flush_icache_range_ipi, &args);
662 instruction_hazard();
665 #ifdef CONFIG_DMA_NONCOHERENT
667 static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
669 /* Catch bad driver code */
673 if (cpu_has_inclusive_pcaches) {
674 if (size >= scache_size)
677 blast_scache_range(addr, addr + size);
684 * Either no secondary cache or the available caches don't have the
685 * subset property so we have to flush the primary caches
688 if (cpu_has_safe_index_cacheops && size >= dcache_size) {
691 R4600_HIT_CACHEOP_WAR_IMPL;
692 blast_dcache_range(addr, addr + size);
696 bc_wback_inv(addr, size);
700 static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
702 /* Catch bad driver code */
706 if (cpu_has_inclusive_pcaches) {
707 if (size >= scache_size)
711 * There is no clearly documented alignment requirement
712 * for the cache instruction on MIPS processors and
713 * some processors, among them the RM5200 and RM7000
714 * QED processors will throw an address error for cache
715 * hit ops with insufficient alignment. Solved by
716 * aligning the address to cache line size.
718 blast_inv_scache_range(addr, addr + size);
725 if (cpu_has_safe_index_cacheops && size >= dcache_size) {
728 R4600_HIT_CACHEOP_WAR_IMPL;
729 blast_inv_dcache_range(addr, addr + size);
736 #endif /* CONFIG_DMA_NONCOHERENT */
739 * While we're protected against bad userland addresses we don't care
740 * very much about what happens in that case. Usually a segmentation
741 * fault will dump the process later on anyway ...
743 static void local_r4k_flush_cache_sigtramp(void * arg)
745 unsigned long ic_lsize = cpu_icache_line_size();
746 unsigned long dc_lsize = cpu_dcache_line_size();
747 unsigned long sc_lsize = cpu_scache_line_size();
748 unsigned long addr = (unsigned long) arg;
750 R4600_HIT_CACHEOP_WAR_IMPL;
752 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
753 if (!cpu_icache_snoops_remote_store && scache_size)
754 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
756 protected_flush_icache_line(addr & ~(ic_lsize - 1));
757 if (MIPS4K_ICACHE_REFILL_WAR) {
758 __asm__ __volatile__ (
773 : "i" (Hit_Invalidate_I));
775 if (MIPS_CACHE_SYNC_WAR)
776 __asm__ __volatile__ ("sync");
779 static void r4k_flush_cache_sigtramp(unsigned long addr)
781 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr);
784 static void r4k_flush_icache_all(void)
786 if (cpu_has_vtag_icache)
790 struct flush_kernel_vmap_range_args {
795 static inline void local_r4k_flush_kernel_vmap_range(void *args)
797 struct flush_kernel_vmap_range_args *vmra = args;
798 unsigned long vaddr = vmra->vaddr;
799 int size = vmra->size;
802 * Aliases only affect the primary caches so don't bother with
803 * S-caches or T-caches.
805 if (cpu_has_safe_index_cacheops && size >= dcache_size)
808 R4600_HIT_CACHEOP_WAR_IMPL;
809 blast_dcache_range(vaddr, vaddr + size);
813 static void r4k_flush_kernel_vmap_range(unsigned long vaddr, int size)
815 struct flush_kernel_vmap_range_args args;
817 args.vaddr = (unsigned long) vaddr;
820 r4k_on_each_cpu(local_r4k_flush_kernel_vmap_range, &args);
823 static inline void rm7k_erratum31(void)
825 const unsigned long ic_lsize = 32;
828 /* RM7000 erratum #31. The icache is screwed at startup. */
832 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
833 __asm__ __volatile__ (
837 "cache\t%1, 0(%0)\n\t"
838 "cache\t%1, 0x1000(%0)\n\t"
839 "cache\t%1, 0x2000(%0)\n\t"
840 "cache\t%1, 0x3000(%0)\n\t"
841 "cache\t%2, 0(%0)\n\t"
842 "cache\t%2, 0x1000(%0)\n\t"
843 "cache\t%2, 0x2000(%0)\n\t"
844 "cache\t%2, 0x3000(%0)\n\t"
845 "cache\t%1, 0(%0)\n\t"
846 "cache\t%1, 0x1000(%0)\n\t"
847 "cache\t%1, 0x2000(%0)\n\t"
848 "cache\t%1, 0x3000(%0)\n\t"
851 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
855 static inline void alias_74k_erratum(struct cpuinfo_mips *c)
857 unsigned int imp = c->processor_id & PRID_IMP_MASK;
858 unsigned int rev = c->processor_id & PRID_REV_MASK;
861 * Early versions of the 74K do not update the cache tags on a
862 * vtag miss/ptag hit which can occur in the case of KSEG0/KUSEG
863 * aliases. In this case it is better to treat the cache as always
868 if (rev <= PRID_REV_ENCODE_332(2, 4, 0))
869 c->dcache.flags |= MIPS_CACHE_VTAG;
870 if (rev == PRID_REV_ENCODE_332(2, 4, 0))
871 write_c0_config6(read_c0_config6() | MIPS_CONF6_SYND);
874 if (rev <= PRID_REV_ENCODE_332(1, 1, 0)) {
875 c->dcache.flags |= MIPS_CACHE_VTAG;
876 write_c0_config6(read_c0_config6() | MIPS_CONF6_SYND);
884 static char *way_string[] = { NULL, "direct mapped", "2-way",
885 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
888 static void probe_pcache(void)
890 struct cpuinfo_mips *c = ¤t_cpu_data;
891 unsigned int config = read_c0_config();
892 unsigned int prid = read_c0_prid();
893 unsigned long config1;
896 switch (current_cpu_type()) {
897 case CPU_R4600: /* QED style two way caches? */
901 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
902 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
904 c->icache.waybit = __ffs(icache_size/2);
906 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
907 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
909 c->dcache.waybit= __ffs(dcache_size/2);
911 c->options |= MIPS_CPU_CACHE_CDEX_P;
916 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
917 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
921 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
922 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
924 c->dcache.waybit = 0;
926 c->options |= MIPS_CPU_CACHE_CDEX_P | MIPS_CPU_PREFETCH;
930 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
931 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
935 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
936 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
938 c->dcache.waybit = 0;
940 c->options |= MIPS_CPU_CACHE_CDEX_P;
941 c->options |= MIPS_CPU_PREFETCH;
951 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
952 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
954 c->icache.waybit = 0; /* doesn't matter */
956 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
957 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
959 c->dcache.waybit = 0; /* does not matter */
961 c->options |= MIPS_CPU_CACHE_CDEX_P;
967 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
968 c->icache.linesz = 64;
970 c->icache.waybit = 0;
972 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
973 c->dcache.linesz = 32;
975 c->dcache.waybit = 0;
977 c->options |= MIPS_CPU_PREFETCH;
981 write_c0_config(config & ~VR41_CONF_P4K);
983 /* Workaround for cache instruction bug of VR4131 */
984 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
985 c->processor_id == 0x0c82U) {
986 config |= 0x00400000U;
987 if (c->processor_id == 0x0c80U)
988 config |= VR41_CONF_BP;
989 write_c0_config(config);
991 c->options |= MIPS_CPU_CACHE_CDEX_P;
993 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
994 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
996 c->icache.waybit = __ffs(icache_size/2);
998 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
999 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1001 c->dcache.waybit = __ffs(dcache_size/2);
1010 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
1011 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1013 c->icache.waybit = 0; /* doesn't matter */
1015 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
1016 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1018 c->dcache.waybit = 0; /* does not matter */
1020 c->options |= MIPS_CPU_CACHE_CDEX_P;
1026 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
1027 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1029 c->icache.waybit = __ffs(icache_size / c->icache.ways);
1031 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
1032 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1034 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
1036 c->options |= MIPS_CPU_CACHE_CDEX_P;
1037 c->options |= MIPS_CPU_PREFETCH;
1041 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
1042 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
1047 c->icache.waybit = 0;
1049 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
1050 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
1055 c->dcache.waybit = 0;
1059 if (!(config & MIPS_CONF_M))
1060 panic("Don't know how to probe P-caches on this cpu.");
1063 * So we seem to be a MIPS32 or MIPS64 CPU
1064 * So let's probe the I-cache ...
1066 config1 = read_c0_config1();
1068 lsize = (config1 >> 19) & 7;
1070 /* IL == 7 is reserved */
1072 panic("Invalid icache line size");
1074 c->icache.linesz = lsize ? 2 << lsize : 0;
1076 c->icache.sets = 32 << (((config1 >> 22) + 1) & 7);
1077 c->icache.ways = 1 + ((config1 >> 16) & 7);
1079 icache_size = c->icache.sets *
1082 c->icache.waybit = __ffs(icache_size/c->icache.ways);
1084 if (config & 0x8) /* VI bit */
1085 c->icache.flags |= MIPS_CACHE_VTAG;
1088 * Now probe the MIPS32 / MIPS64 data cache.
1090 c->dcache.flags = 0;
1092 lsize = (config1 >> 10) & 7;
1094 /* DL == 7 is reserved */
1096 panic("Invalid dcache line size");
1098 c->dcache.linesz = lsize ? 2 << lsize : 0;
1100 c->dcache.sets = 32 << (((config1 >> 13) + 1) & 7);
1101 c->dcache.ways = 1 + ((config1 >> 7) & 7);
1103 dcache_size = c->dcache.sets *
1106 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
1108 c->options |= MIPS_CPU_PREFETCH;
1113 * Processor configuration sanity check for the R4000SC erratum
1114 * #5. With page sizes larger than 32kB there is no possibility
1115 * to get a VCE exception anymore so we don't care about this
1116 * misconfiguration. The case is rather theoretical anyway;
1117 * presumably no vendor is shipping his hardware in the "bad"
1120 if ((prid & PRID_IMP_MASK) == PRID_IMP_R4000 &&
1121 (prid & PRID_REV_MASK) < PRID_REV_R4400 &&
1122 !(config & CONF_SC) && c->icache.linesz != 16 &&
1123 PAGE_SIZE <= 0x8000)
1124 panic("Improper R4000SC processor configuration detected");
1126 /* compute a couple of other cache variables */
1127 c->icache.waysize = icache_size / c->icache.ways;
1128 c->dcache.waysize = dcache_size / c->dcache.ways;
1130 c->icache.sets = c->icache.linesz ?
1131 icache_size / (c->icache.linesz * c->icache.ways) : 0;
1132 c->dcache.sets = c->dcache.linesz ?
1133 dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
1136 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
1137 * 2-way virtually indexed so normally would suffer from aliases. So
1138 * normally they'd suffer from aliases but magic in the hardware deals
1139 * with that for us so we don't need to take care ourselves.
1141 switch (current_cpu_type()) {
1147 c->dcache.flags |= MIPS_CACHE_PINDEX;
1162 case CPU_INTERAPTIV:
1165 if ((c->cputype == CPU_74K) || (c->cputype == CPU_1074K))
1166 alias_74k_erratum(c);
1167 if (!(read_c0_config7() & MIPS_CONF7_IAR) &&
1168 (c->icache.waysize > PAGE_SIZE))
1169 c->icache.flags |= MIPS_CACHE_ALIASES;
1170 if (read_c0_config7() & MIPS_CONF7_AR) {
1172 * Effectively physically indexed dcache,
1173 * thus no virtual aliases.
1175 c->dcache.flags |= MIPS_CACHE_PINDEX;
1179 if (c->dcache.waysize > PAGE_SIZE)
1180 c->dcache.flags |= MIPS_CACHE_ALIASES;
1183 switch (current_cpu_type()) {
1186 * Some older 20Kc chips doesn't have the 'VI' bit in
1187 * the config register.
1189 c->icache.flags |= MIPS_CACHE_VTAG;
1193 c->icache.flags |= MIPS_CACHE_IC_F_DC;
1198 * LOONGSON2 has 4 way icache, but when using indexed cache op,
1199 * one op will act on all 4 ways
1204 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1206 c->icache.flags & MIPS_CACHE_VTAG ? "VIVT" : "VIPT",
1207 way_string[c->icache.ways], c->icache.linesz);
1209 printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
1210 dcache_size >> 10, way_string[c->dcache.ways],
1211 (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
1212 (c->dcache.flags & MIPS_CACHE_ALIASES) ?
1213 "cache aliases" : "no aliases",
1218 * If you even _breathe_ on this function, look at the gcc output and make sure
1219 * it does not pop things on and off the stack for the cache sizing loop that
1220 * executes in KSEG1 space or else you will crash and burn badly. You have
1223 static int probe_scache(void)
1225 unsigned long flags, addr, begin, end, pow2;
1226 unsigned int config = read_c0_config();
1227 struct cpuinfo_mips *c = ¤t_cpu_data;
1229 if (config & CONF_SC)
1232 begin = (unsigned long) &_stext;
1233 begin &= ~((4 * 1024 * 1024) - 1);
1234 end = begin + (4 * 1024 * 1024);
1237 * This is such a bitch, you'd think they would make it easy to do
1238 * this. Away you daemons of stupidity!
1240 local_irq_save(flags);
1242 /* Fill each size-multiple cache line with a valid tag. */
1244 for (addr = begin; addr < end; addr = (begin + pow2)) {
1245 unsigned long *p = (unsigned long *) addr;
1246 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1250 /* Load first line with zero (therefore invalid) tag. */
1253 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1254 cache_op(Index_Store_Tag_I, begin);
1255 cache_op(Index_Store_Tag_D, begin);
1256 cache_op(Index_Store_Tag_SD, begin);
1258 /* Now search for the wrap around point. */
1259 pow2 = (128 * 1024);
1260 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1261 cache_op(Index_Load_Tag_SD, addr);
1262 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1263 if (!read_c0_taglo())
1267 local_irq_restore(flags);
1271 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1273 c->dcache.waybit = 0; /* does not matter */
1278 static void __init loongson2_sc_init(void)
1280 struct cpuinfo_mips *c = ¤t_cpu_data;
1282 scache_size = 512*1024;
1283 c->scache.linesz = 32;
1285 c->scache.waybit = 0;
1286 c->scache.waysize = scache_size / (c->scache.ways);
1287 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1288 pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1289 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1291 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1294 extern int r5k_sc_init(void);
1295 extern int rm7k_sc_init(void);
1296 extern int mips_sc_init(void);
1298 static void setup_scache(void)
1300 struct cpuinfo_mips *c = ¤t_cpu_data;
1301 unsigned int config = read_c0_config();
1305 * Do the probing thing on R4000SC and R4400SC processors. Other
1306 * processors don't have a S-cache that would be relevant to the
1307 * Linux memory management.
1309 switch (current_cpu_type()) {
1314 sc_present = run_uncached(probe_scache);
1316 c->options |= MIPS_CPU_CACHE_CDEX_S;
1322 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1323 c->scache.linesz = 64 << ((config >> 13) & 1);
1325 c->scache.waybit= 0;
1331 #ifdef CONFIG_R5000_CPU_SCACHE
1337 #ifdef CONFIG_RM7000_CPU_SCACHE
1343 loongson2_sc_init();
1347 /* don't need to worry about L2, fully coherent */
1351 if (c->isa_level & (MIPS_CPU_ISA_M32R1 | MIPS_CPU_ISA_M32R2 |
1352 MIPS_CPU_ISA_M64R1 | MIPS_CPU_ISA_M64R2)) {
1353 #ifdef CONFIG_MIPS_CPU_SCACHE
1354 if (mips_sc_init ()) {
1355 scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
1356 printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
1358 way_string[c->scache.ways], c->scache.linesz);
1361 if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1362 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1372 /* compute a couple of other cache variables */
1373 c->scache.waysize = scache_size / c->scache.ways;
1375 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1377 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1378 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1380 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1383 void au1x00_fixup_config_od(void)
1386 * c0_config.od (bit 19) was write only (and read as 0)
1387 * on the early revisions of Alchemy SOCs. It disables the bus
1388 * transaction overlapping and needs to be set to fix various errata.
1390 switch (read_c0_prid()) {
1391 case 0x00030100: /* Au1000 DA */
1392 case 0x00030201: /* Au1000 HA */
1393 case 0x00030202: /* Au1000 HB */
1394 case 0x01030200: /* Au1500 AB */
1396 * Au1100 errata actually keeps silence about this bit, so we set it
1397 * just in case for those revisions that require it to be set according
1398 * to the (now gone) cpu table.
1400 case 0x02030200: /* Au1100 AB */
1401 case 0x02030201: /* Au1100 BA */
1402 case 0x02030202: /* Au1100 BC */
1403 set_c0_config(1 << 19);
1408 /* CP0 hazard avoidance. */
1409 #define NXP_BARRIER() \
1410 __asm__ __volatile__( \
1411 ".set noreorder\n\t" \
1412 "nop; nop; nop; nop; nop; nop;\n\t" \
1415 static void nxp_pr4450_fixup_config(void)
1417 unsigned long config0;
1419 config0 = read_c0_config();
1421 /* clear all three cache coherency fields */
1422 config0 &= ~(0x7 | (7 << 25) | (7 << 28));
1423 config0 |= (((_page_cachable_default >> _CACHE_SHIFT) << 0) |
1424 ((_page_cachable_default >> _CACHE_SHIFT) << 25) |
1425 ((_page_cachable_default >> _CACHE_SHIFT) << 28));
1426 write_c0_config(config0);
1430 static int cca = -1;
1432 static int __init cca_setup(char *str)
1434 get_option(&str, &cca);
1439 early_param("cca", cca_setup);
1441 static void coherency_setup(void)
1443 if (cca < 0 || cca > 7)
1444 cca = read_c0_config() & CONF_CM_CMASK;
1445 _page_cachable_default = cca << _CACHE_SHIFT;
1447 pr_debug("Using cache attribute %d\n", cca);
1448 change_c0_config(CONF_CM_CMASK, cca);
1451 * c0_status.cu=0 specifies that updates by the sc instruction use
1452 * the coherency mode specified by the TLB; 1 means cachable
1453 * coherent update on write will be used. Not all processors have
1454 * this bit and; some wire it to zero, others like Toshiba had the
1455 * silly idea of putting something else there ...
1457 switch (current_cpu_type()) {
1464 clear_c0_config(CONF_CU);
1467 * We need to catch the early Alchemy SOCs with
1468 * the write-only co_config.od bit and set it back to one on:
1469 * Au1000 rev DA, HA, HB; Au1100 AB, BA, BC, Au1500 AB
1472 au1x00_fixup_config_od();
1475 case PRID_IMP_PR4450:
1476 nxp_pr4450_fixup_config();
1481 static void r4k_cache_error_setup(void)
1483 extern char __weak except_vec2_generic;
1484 extern char __weak except_vec2_sb1;
1486 switch (current_cpu_type()) {
1489 set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1493 set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1498 void r4k_cache_init(void)
1500 extern void build_clear_page(void);
1501 extern void build_copy_page(void);
1502 struct cpuinfo_mips *c = ¤t_cpu_data;
1507 r4k_blast_dcache_page_setup();
1508 r4k_blast_dcache_page_indexed_setup();
1509 r4k_blast_dcache_setup();
1510 r4k_blast_icache_page_setup();
1511 r4k_blast_icache_page_indexed_setup();
1512 r4k_blast_icache_setup();
1513 r4k_blast_scache_page_setup();
1514 r4k_blast_scache_page_indexed_setup();
1515 r4k_blast_scache_setup();
1517 r4k_blast_dcache_user_page_setup();
1518 r4k_blast_icache_user_page_setup();
1522 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1523 * This code supports virtually indexed processors and will be
1524 * unnecessarily inefficient on physically indexed processors.
1526 if (c->dcache.linesz)
1527 shm_align_mask = max_t( unsigned long,
1528 c->dcache.sets * c->dcache.linesz - 1,
1531 shm_align_mask = PAGE_SIZE-1;
1533 __flush_cache_vmap = r4k__flush_cache_vmap;
1534 __flush_cache_vunmap = r4k__flush_cache_vunmap;
1536 flush_cache_all = cache_noop;
1537 __flush_cache_all = r4k___flush_cache_all;
1538 flush_cache_mm = r4k_flush_cache_mm;
1539 flush_cache_page = r4k_flush_cache_page;
1540 flush_cache_range = r4k_flush_cache_range;
1542 __flush_kernel_vmap_range = r4k_flush_kernel_vmap_range;
1544 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1545 flush_icache_all = r4k_flush_icache_all;
1546 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
1547 flush_data_cache_page = r4k_flush_data_cache_page;
1548 flush_icache_range = r4k_flush_icache_range;
1549 local_flush_icache_range = local_r4k_flush_icache_range;
1551 #if defined(CONFIG_DMA_NONCOHERENT)
1553 _dma_cache_wback_inv = (void *)cache_noop;
1554 _dma_cache_wback = (void *)cache_noop;
1555 _dma_cache_inv = (void *)cache_noop;
1557 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1558 _dma_cache_wback = r4k_dma_cache_wback_inv;
1559 _dma_cache_inv = r4k_dma_cache_inv;
1567 * We want to run CMP kernels on core with and without coherent
1568 * caches. Therefore, do not use CONFIG_MIPS_CMP to decide whether
1569 * or not to flush caches.
1571 local_r4k___flush_cache_all(NULL);
1574 board_cache_error_setup = r4k_cache_error_setup;