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 (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.
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/sched.h>
17 #include <linux/module.h>
18 #include <linux/bitops.h>
20 #include <asm/bcache.h>
21 #include <asm/bootinfo.h>
22 #include <asm/cache.h>
23 #include <asm/cacheops.h>
25 #include <asm/cpu-features.h>
28 #include <asm/pgtable.h>
29 #include <asm/r4kcache.h>
30 #include <asm/sections.h>
31 #include <asm/system.h>
32 #include <asm/mmu_context.h>
34 #include <asm/cacheflush.h> /* for run_uncached() */
38 * Special Variant of smp_call_function for use by cache functions:
41 * o collapses to normal function call on UP kernels
42 * o collapses to normal function call on systems with a single shared
45 static inline void r4k_on_each_cpu(void (*func) (void *info), void *info,
50 #if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
51 smp_call_function(func, info, retry, wait);
60 static unsigned long icache_size __read_mostly;
61 static unsigned long dcache_size __read_mostly;
62 static unsigned long scache_size __read_mostly;
65 * Dummy cache handling routines for machines without boardcaches
67 static void cache_noop(void) {}
69 static struct bcache_ops no_sc_ops = {
70 .bc_enable = (void *)cache_noop,
71 .bc_disable = (void *)cache_noop,
72 .bc_wback_inv = (void *)cache_noop,
73 .bc_inv = (void *)cache_noop
76 struct bcache_ops *bcops = &no_sc_ops;
78 #define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
79 #define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
81 #define R4600_HIT_CACHEOP_WAR_IMPL \
83 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
84 *(volatile unsigned long *)CKSEG1; \
85 if (R4600_V1_HIT_CACHEOP_WAR) \
86 __asm__ __volatile__("nop;nop;nop;nop"); \
89 static void (*r4k_blast_dcache_page)(unsigned long addr);
91 static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
93 R4600_HIT_CACHEOP_WAR_IMPL;
94 blast_dcache32_page(addr);
97 static void __cpuinit r4k_blast_dcache_page_setup(void)
99 unsigned long dc_lsize = cpu_dcache_line_size();
102 r4k_blast_dcache_page = (void *)cache_noop;
103 else if (dc_lsize == 16)
104 r4k_blast_dcache_page = blast_dcache16_page;
105 else if (dc_lsize == 32)
106 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
109 static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
111 static void __cpuinit r4k_blast_dcache_page_indexed_setup(void)
113 unsigned long dc_lsize = cpu_dcache_line_size();
116 r4k_blast_dcache_page_indexed = (void *)cache_noop;
117 else if (dc_lsize == 16)
118 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
119 else if (dc_lsize == 32)
120 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
123 static void (* r4k_blast_dcache)(void);
125 static void __cpuinit r4k_blast_dcache_setup(void)
127 unsigned long dc_lsize = cpu_dcache_line_size();
130 r4k_blast_dcache = (void *)cache_noop;
131 else if (dc_lsize == 16)
132 r4k_blast_dcache = blast_dcache16;
133 else if (dc_lsize == 32)
134 r4k_blast_dcache = blast_dcache32;
137 /* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
138 #define JUMP_TO_ALIGN(order) \
139 __asm__ __volatile__( \
141 ".align\t" #order "\n\t" \
144 #define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
145 #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
147 static inline void blast_r4600_v1_icache32(void)
151 local_irq_save(flags);
153 local_irq_restore(flags);
156 static inline void tx49_blast_icache32(void)
158 unsigned long start = INDEX_BASE;
159 unsigned long end = start + current_cpu_data.icache.waysize;
160 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
161 unsigned long ws_end = current_cpu_data.icache.ways <<
162 current_cpu_data.icache.waybit;
163 unsigned long ws, addr;
165 CACHE32_UNROLL32_ALIGN2;
166 /* I'm in even chunk. blast odd chunks */
167 for (ws = 0; ws < ws_end; ws += ws_inc)
168 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
169 cache32_unroll32(addr|ws, Index_Invalidate_I);
170 CACHE32_UNROLL32_ALIGN;
171 /* I'm in odd chunk. blast even chunks */
172 for (ws = 0; ws < ws_end; ws += ws_inc)
173 for (addr = start; addr < end; addr += 0x400 * 2)
174 cache32_unroll32(addr|ws, Index_Invalidate_I);
177 static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
181 local_irq_save(flags);
182 blast_icache32_page_indexed(page);
183 local_irq_restore(flags);
186 static inline void tx49_blast_icache32_page_indexed(unsigned long page)
188 unsigned long indexmask = current_cpu_data.icache.waysize - 1;
189 unsigned long start = INDEX_BASE + (page & indexmask);
190 unsigned long end = start + PAGE_SIZE;
191 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
192 unsigned long ws_end = current_cpu_data.icache.ways <<
193 current_cpu_data.icache.waybit;
194 unsigned long ws, addr;
196 CACHE32_UNROLL32_ALIGN2;
197 /* I'm in even chunk. blast odd chunks */
198 for (ws = 0; ws < ws_end; ws += ws_inc)
199 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
200 cache32_unroll32(addr|ws, Index_Invalidate_I);
201 CACHE32_UNROLL32_ALIGN;
202 /* I'm in odd chunk. blast even chunks */
203 for (ws = 0; ws < ws_end; ws += ws_inc)
204 for (addr = start; addr < end; addr += 0x400 * 2)
205 cache32_unroll32(addr|ws, Index_Invalidate_I);
208 static void (* r4k_blast_icache_page)(unsigned long addr);
210 static void __cpuinit r4k_blast_icache_page_setup(void)
212 unsigned long ic_lsize = cpu_icache_line_size();
215 r4k_blast_icache_page = (void *)cache_noop;
216 else if (ic_lsize == 16)
217 r4k_blast_icache_page = blast_icache16_page;
218 else if (ic_lsize == 32)
219 r4k_blast_icache_page = blast_icache32_page;
220 else if (ic_lsize == 64)
221 r4k_blast_icache_page = blast_icache64_page;
225 static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
227 static void __cpuinit r4k_blast_icache_page_indexed_setup(void)
229 unsigned long ic_lsize = cpu_icache_line_size();
232 r4k_blast_icache_page_indexed = (void *)cache_noop;
233 else if (ic_lsize == 16)
234 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
235 else if (ic_lsize == 32) {
236 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
237 r4k_blast_icache_page_indexed =
238 blast_icache32_r4600_v1_page_indexed;
239 else if (TX49XX_ICACHE_INDEX_INV_WAR)
240 r4k_blast_icache_page_indexed =
241 tx49_blast_icache32_page_indexed;
243 r4k_blast_icache_page_indexed =
244 blast_icache32_page_indexed;
245 } else if (ic_lsize == 64)
246 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
249 static void (* r4k_blast_icache)(void);
251 static void __cpuinit r4k_blast_icache_setup(void)
253 unsigned long ic_lsize = cpu_icache_line_size();
256 r4k_blast_icache = (void *)cache_noop;
257 else if (ic_lsize == 16)
258 r4k_blast_icache = blast_icache16;
259 else if (ic_lsize == 32) {
260 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
261 r4k_blast_icache = blast_r4600_v1_icache32;
262 else if (TX49XX_ICACHE_INDEX_INV_WAR)
263 r4k_blast_icache = tx49_blast_icache32;
265 r4k_blast_icache = blast_icache32;
266 } else if (ic_lsize == 64)
267 r4k_blast_icache = blast_icache64;
270 static void (* r4k_blast_scache_page)(unsigned long addr);
272 static void __cpuinit r4k_blast_scache_page_setup(void)
274 unsigned long sc_lsize = cpu_scache_line_size();
276 if (scache_size == 0)
277 r4k_blast_scache_page = (void *)cache_noop;
278 else if (sc_lsize == 16)
279 r4k_blast_scache_page = blast_scache16_page;
280 else if (sc_lsize == 32)
281 r4k_blast_scache_page = blast_scache32_page;
282 else if (sc_lsize == 64)
283 r4k_blast_scache_page = blast_scache64_page;
284 else if (sc_lsize == 128)
285 r4k_blast_scache_page = blast_scache128_page;
288 static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
290 static void __cpuinit r4k_blast_scache_page_indexed_setup(void)
292 unsigned long sc_lsize = cpu_scache_line_size();
294 if (scache_size == 0)
295 r4k_blast_scache_page_indexed = (void *)cache_noop;
296 else if (sc_lsize == 16)
297 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
298 else if (sc_lsize == 32)
299 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
300 else if (sc_lsize == 64)
301 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
302 else if (sc_lsize == 128)
303 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
306 static void (* r4k_blast_scache)(void);
308 static void __cpuinit r4k_blast_scache_setup(void)
310 unsigned long sc_lsize = cpu_scache_line_size();
312 if (scache_size == 0)
313 r4k_blast_scache = (void *)cache_noop;
314 else if (sc_lsize == 16)
315 r4k_blast_scache = blast_scache16;
316 else if (sc_lsize == 32)
317 r4k_blast_scache = blast_scache32;
318 else if (sc_lsize == 64)
319 r4k_blast_scache = blast_scache64;
320 else if (sc_lsize == 128)
321 r4k_blast_scache = blast_scache128;
324 static inline void local_r4k___flush_cache_all(void * args)
326 #if defined(CONFIG_CPU_LOONGSON2)
333 switch (current_cpu_type()) {
345 static void r4k___flush_cache_all(void)
347 r4k_on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
350 static inline int has_valid_asid(const struct mm_struct *mm)
352 #if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
355 for_each_online_cpu(i)
356 if (cpu_context(i, mm))
361 return cpu_context(smp_processor_id(), mm);
365 static void r4k__flush_cache_vmap(void)
370 static void r4k__flush_cache_vunmap(void)
375 static inline void local_r4k_flush_cache_range(void * args)
377 struct vm_area_struct *vma = args;
378 int exec = vma->vm_flags & VM_EXEC;
380 if (!(has_valid_asid(vma->vm_mm)))
388 static void r4k_flush_cache_range(struct vm_area_struct *vma,
389 unsigned long start, unsigned long end)
391 int exec = vma->vm_flags & VM_EXEC;
393 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
394 r4k_on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
397 static inline void local_r4k_flush_cache_mm(void * args)
399 struct mm_struct *mm = args;
401 if (!has_valid_asid(mm))
405 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
406 * only flush the primary caches but R10000 and R12000 behave sane ...
407 * R4000SC and R4400SC indexed S-cache ops also invalidate primary
408 * caches, so we can bail out early.
410 if (current_cpu_type() == CPU_R4000SC ||
411 current_cpu_type() == CPU_R4000MC ||
412 current_cpu_type() == CPU_R4400SC ||
413 current_cpu_type() == CPU_R4400MC) {
421 static void r4k_flush_cache_mm(struct mm_struct *mm)
423 if (!cpu_has_dc_aliases)
426 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
429 struct flush_cache_page_args {
430 struct vm_area_struct *vma;
435 static inline void local_r4k_flush_cache_page(void *args)
437 struct flush_cache_page_args *fcp_args = args;
438 struct vm_area_struct *vma = fcp_args->vma;
439 unsigned long addr = fcp_args->addr;
440 struct page *page = pfn_to_page(fcp_args->pfn);
441 int exec = vma->vm_flags & VM_EXEC;
442 struct mm_struct *mm = vma->vm_mm;
450 * If ownes no valid ASID yet, cannot possibly have gotten
451 * this page into the cache.
453 if (!has_valid_asid(mm))
457 pgdp = pgd_offset(mm, addr);
458 pudp = pud_offset(pgdp, addr);
459 pmdp = pmd_offset(pudp, addr);
460 ptep = pte_offset(pmdp, addr);
463 * If the page isn't marked valid, the page cannot possibly be
466 if (!(pte_present(*ptep)))
469 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
473 * Use kmap_coherent or kmap_atomic to do flushes for
474 * another ASID than the current one.
476 if (cpu_has_dc_aliases)
477 vaddr = kmap_coherent(page, addr);
479 vaddr = kmap_atomic(page, KM_USER0);
480 addr = (unsigned long)vaddr;
483 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
484 r4k_blast_dcache_page(addr);
487 if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
488 int cpu = smp_processor_id();
490 if (cpu_context(cpu, mm) != 0)
491 drop_mmu_context(mm, cpu);
493 r4k_blast_icache_page(addr);
497 if (cpu_has_dc_aliases)
500 kunmap_atomic(vaddr, KM_USER0);
504 static void r4k_flush_cache_page(struct vm_area_struct *vma,
505 unsigned long addr, unsigned long pfn)
507 struct flush_cache_page_args args;
513 r4k_on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
516 static inline void local_r4k_flush_data_cache_page(void * addr)
518 r4k_blast_dcache_page((unsigned long) addr);
521 static void r4k_flush_data_cache_page(unsigned long addr)
524 local_r4k_flush_data_cache_page((void *)addr);
526 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr,
530 struct flush_icache_range_args {
535 static inline void local_r4k_flush_icache_range(void *args)
537 struct flush_icache_range_args *fir_args = args;
538 unsigned long start = fir_args->start;
539 unsigned long end = fir_args->end;
541 if (!cpu_has_ic_fills_f_dc) {
542 if (end - start >= dcache_size) {
545 R4600_HIT_CACHEOP_WAR_IMPL;
546 protected_blast_dcache_range(start, end);
550 if (end - start > icache_size)
553 protected_blast_icache_range(start, end);
556 static void r4k_flush_icache_range(unsigned long start, unsigned long end)
558 struct flush_icache_range_args args;
563 r4k_on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
564 instruction_hazard();
567 #ifdef CONFIG_DMA_NONCOHERENT
569 static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
571 /* Catch bad driver code */
574 if (cpu_has_inclusive_pcaches) {
575 if (size >= scache_size)
578 blast_scache_range(addr, addr + size);
583 * Either no secondary cache or the available caches don't have the
584 * subset property so we have to flush the primary caches
587 if (size >= dcache_size) {
590 R4600_HIT_CACHEOP_WAR_IMPL;
591 blast_dcache_range(addr, addr + size);
594 bc_wback_inv(addr, size);
597 static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
599 /* Catch bad driver code */
602 if (cpu_has_inclusive_pcaches) {
603 if (size >= scache_size)
606 blast_inv_scache_range(addr, addr + size);
610 if (size >= dcache_size) {
613 R4600_HIT_CACHEOP_WAR_IMPL;
614 blast_inv_dcache_range(addr, addr + size);
619 #endif /* CONFIG_DMA_NONCOHERENT */
622 * While we're protected against bad userland addresses we don't care
623 * very much about what happens in that case. Usually a segmentation
624 * fault will dump the process later on anyway ...
626 static void local_r4k_flush_cache_sigtramp(void * arg)
628 unsigned long ic_lsize = cpu_icache_line_size();
629 unsigned long dc_lsize = cpu_dcache_line_size();
630 unsigned long sc_lsize = cpu_scache_line_size();
631 unsigned long addr = (unsigned long) arg;
633 R4600_HIT_CACHEOP_WAR_IMPL;
635 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
636 if (!cpu_icache_snoops_remote_store && scache_size)
637 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
639 protected_flush_icache_line(addr & ~(ic_lsize - 1));
640 if (MIPS4K_ICACHE_REFILL_WAR) {
641 __asm__ __volatile__ (
656 : "i" (Hit_Invalidate_I));
658 if (MIPS_CACHE_SYNC_WAR)
659 __asm__ __volatile__ ("sync");
662 static void r4k_flush_cache_sigtramp(unsigned long addr)
664 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
667 static void r4k_flush_icache_all(void)
669 if (cpu_has_vtag_icache)
673 static inline void rm7k_erratum31(void)
675 const unsigned long ic_lsize = 32;
678 /* RM7000 erratum #31. The icache is screwed at startup. */
682 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
683 __asm__ __volatile__ (
687 "cache\t%1, 0(%0)\n\t"
688 "cache\t%1, 0x1000(%0)\n\t"
689 "cache\t%1, 0x2000(%0)\n\t"
690 "cache\t%1, 0x3000(%0)\n\t"
691 "cache\t%2, 0(%0)\n\t"
692 "cache\t%2, 0x1000(%0)\n\t"
693 "cache\t%2, 0x2000(%0)\n\t"
694 "cache\t%2, 0x3000(%0)\n\t"
695 "cache\t%1, 0(%0)\n\t"
696 "cache\t%1, 0x1000(%0)\n\t"
697 "cache\t%1, 0x2000(%0)\n\t"
698 "cache\t%1, 0x3000(%0)\n\t"
701 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
705 static char *way_string[] __cpuinitdata = { NULL, "direct mapped", "2-way",
706 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
709 static void __cpuinit probe_pcache(void)
711 struct cpuinfo_mips *c = ¤t_cpu_data;
712 unsigned int config = read_c0_config();
713 unsigned int prid = read_c0_prid();
714 unsigned long config1;
717 switch (c->cputype) {
718 case CPU_R4600: /* QED style two way caches? */
722 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
723 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
725 c->icache.waybit = __ffs(icache_size/2);
727 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
728 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
730 c->dcache.waybit= __ffs(dcache_size/2);
732 c->options |= MIPS_CPU_CACHE_CDEX_P;
737 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
738 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
742 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
743 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
745 c->dcache.waybit = 0;
747 c->options |= MIPS_CPU_CACHE_CDEX_P;
751 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
752 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
756 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
757 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
759 c->dcache.waybit = 0;
761 c->options |= MIPS_CPU_CACHE_CDEX_P;
762 c->options |= MIPS_CPU_PREFETCH;
772 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
773 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
775 c->icache.waybit = 0; /* doesn't matter */
777 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
778 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
780 c->dcache.waybit = 0; /* does not matter */
782 c->options |= MIPS_CPU_CACHE_CDEX_P;
788 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
789 c->icache.linesz = 64;
791 c->icache.waybit = 0;
793 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
794 c->dcache.linesz = 32;
796 c->dcache.waybit = 0;
798 c->options |= MIPS_CPU_PREFETCH;
802 write_c0_config(config & ~VR41_CONF_P4K);
804 /* Workaround for cache instruction bug of VR4131 */
805 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
806 c->processor_id == 0x0c82U) {
807 config |= 0x00400000U;
808 if (c->processor_id == 0x0c80U)
809 config |= VR41_CONF_BP;
810 write_c0_config(config);
812 c->options |= MIPS_CPU_CACHE_CDEX_P;
814 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
815 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
817 c->icache.waybit = __ffs(icache_size/2);
819 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
820 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
822 c->dcache.waybit = __ffs(dcache_size/2);
831 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
832 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
834 c->icache.waybit = 0; /* doesn't matter */
836 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
837 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
839 c->dcache.waybit = 0; /* does not matter */
841 c->options |= MIPS_CPU_CACHE_CDEX_P;
848 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
849 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
851 c->icache.waybit = __ffs(icache_size / c->icache.ways);
853 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
854 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
856 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
858 #if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
859 c->options |= MIPS_CPU_CACHE_CDEX_P;
861 c->options |= MIPS_CPU_PREFETCH;
865 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
866 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
871 c->icache.waybit = 0;
873 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
874 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
879 c->dcache.waybit = 0;
883 if (!(config & MIPS_CONF_M))
884 panic("Don't know how to probe P-caches on this cpu.");
887 * So we seem to be a MIPS32 or MIPS64 CPU
888 * So let's probe the I-cache ...
890 config1 = read_c0_config1();
892 if ((lsize = ((config1 >> 19) & 7)))
893 c->icache.linesz = 2 << lsize;
895 c->icache.linesz = lsize;
896 c->icache.sets = 64 << ((config1 >> 22) & 7);
897 c->icache.ways = 1 + ((config1 >> 16) & 7);
899 icache_size = c->icache.sets *
902 c->icache.waybit = __ffs(icache_size/c->icache.ways);
904 if (config & 0x8) /* VI bit */
905 c->icache.flags |= MIPS_CACHE_VTAG;
908 * Now probe the MIPS32 / MIPS64 data cache.
912 if ((lsize = ((config1 >> 10) & 7)))
913 c->dcache.linesz = 2 << lsize;
915 c->dcache.linesz= lsize;
916 c->dcache.sets = 64 << ((config1 >> 13) & 7);
917 c->dcache.ways = 1 + ((config1 >> 7) & 7);
919 dcache_size = c->dcache.sets *
922 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
924 c->options |= MIPS_CPU_PREFETCH;
929 * Processor configuration sanity check for the R4000SC erratum
930 * #5. With page sizes larger than 32kB there is no possibility
931 * to get a VCE exception anymore so we don't care about this
932 * misconfiguration. The case is rather theoretical anyway;
933 * presumably no vendor is shipping his hardware in the "bad"
936 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
937 !(config & CONF_SC) && c->icache.linesz != 16 &&
939 panic("Improper R4000SC processor configuration detected");
941 /* compute a couple of other cache variables */
942 c->icache.waysize = icache_size / c->icache.ways;
943 c->dcache.waysize = dcache_size / c->dcache.ways;
945 c->icache.sets = c->icache.linesz ?
946 icache_size / (c->icache.linesz * c->icache.ways) : 0;
947 c->dcache.sets = c->dcache.linesz ?
948 dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
951 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
952 * 2-way virtually indexed so normally would suffer from aliases. So
953 * normally they'd suffer from aliases but magic in the hardware deals
954 * with that for us so we don't need to take care ourselves.
956 switch (c->cputype) {
961 c->dcache.flags |= MIPS_CACHE_PINDEX;
972 if ((read_c0_config7() & (1 << 16))) {
973 /* effectively physically indexed dcache,
974 thus no virtual aliases. */
975 c->dcache.flags |= MIPS_CACHE_PINDEX;
979 if (c->dcache.waysize > PAGE_SIZE)
980 c->dcache.flags |= MIPS_CACHE_ALIASES;
983 switch (c->cputype) {
986 * Some older 20Kc chips doesn't have the 'VI' bit in
987 * the config register.
989 c->icache.flags |= MIPS_CACHE_VTAG;
999 c->icache.flags |= MIPS_CACHE_IC_F_DC;
1003 #ifdef CONFIG_CPU_LOONGSON2
1005 * LOONGSON2 has 4 way icache, but when using indexed cache op,
1006 * one op will act on all 4 ways
1011 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1013 cpu_has_vtag_icache ? "VIVT" : "VIPT",
1014 way_string[c->icache.ways], c->icache.linesz);
1016 printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
1017 dcache_size >> 10, way_string[c->dcache.ways],
1018 (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
1019 (c->dcache.flags & MIPS_CACHE_ALIASES) ?
1020 "cache aliases" : "no aliases",
1025 * If you even _breathe_ on this function, look at the gcc output and make sure
1026 * it does not pop things on and off the stack for the cache sizing loop that
1027 * executes in KSEG1 space or else you will crash and burn badly. You have
1030 static int __cpuinit probe_scache(void)
1032 unsigned long flags, addr, begin, end, pow2;
1033 unsigned int config = read_c0_config();
1034 struct cpuinfo_mips *c = ¤t_cpu_data;
1037 if (config & CONF_SC)
1040 begin = (unsigned long) &_stext;
1041 begin &= ~((4 * 1024 * 1024) - 1);
1042 end = begin + (4 * 1024 * 1024);
1045 * This is such a bitch, you'd think they would make it easy to do
1046 * this. Away you daemons of stupidity!
1048 local_irq_save(flags);
1050 /* Fill each size-multiple cache line with a valid tag. */
1052 for (addr = begin; addr < end; addr = (begin + pow2)) {
1053 unsigned long *p = (unsigned long *) addr;
1054 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1058 /* Load first line with zero (therefore invalid) tag. */
1061 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1062 cache_op(Index_Store_Tag_I, begin);
1063 cache_op(Index_Store_Tag_D, begin);
1064 cache_op(Index_Store_Tag_SD, begin);
1066 /* Now search for the wrap around point. */
1067 pow2 = (128 * 1024);
1069 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1070 cache_op(Index_Load_Tag_SD, addr);
1071 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1072 if (!read_c0_taglo())
1076 local_irq_restore(flags);
1080 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1082 c->dcache.waybit = 0; /* does not matter */
1087 #if defined(CONFIG_CPU_LOONGSON2)
1088 static void __init loongson2_sc_init(void)
1090 struct cpuinfo_mips *c = ¤t_cpu_data;
1092 scache_size = 512*1024;
1093 c->scache.linesz = 32;
1095 c->scache.waybit = 0;
1096 c->scache.waysize = scache_size / (c->scache.ways);
1097 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1098 pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1099 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1101 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1105 extern int r5k_sc_init(void);
1106 extern int rm7k_sc_init(void);
1107 extern int mips_sc_init(void);
1109 static void __cpuinit setup_scache(void)
1111 struct cpuinfo_mips *c = ¤t_cpu_data;
1112 unsigned int config = read_c0_config();
1116 * Do the probing thing on R4000SC and R4400SC processors. Other
1117 * processors don't have a S-cache that would be relevant to the
1118 * Linux memory management.
1120 switch (c->cputype) {
1125 sc_present = run_uncached(probe_scache);
1127 c->options |= MIPS_CPU_CACHE_CDEX_S;
1133 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1134 c->scache.linesz = 64 << ((config >> 13) & 1);
1136 c->scache.waybit= 0;
1142 #ifdef CONFIG_R5000_CPU_SCACHE
1149 #ifdef CONFIG_RM7000_CPU_SCACHE
1154 #if defined(CONFIG_CPU_LOONGSON2)
1156 loongson2_sc_init();
1161 if (c->isa_level == MIPS_CPU_ISA_M32R1 ||
1162 c->isa_level == MIPS_CPU_ISA_M32R2 ||
1163 c->isa_level == MIPS_CPU_ISA_M64R1 ||
1164 c->isa_level == MIPS_CPU_ISA_M64R2) {
1165 #ifdef CONFIG_MIPS_CPU_SCACHE
1166 if (mips_sc_init ()) {
1167 scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
1168 printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
1170 way_string[c->scache.ways], c->scache.linesz);
1173 if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1174 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1184 /* compute a couple of other cache variables */
1185 c->scache.waysize = scache_size / c->scache.ways;
1187 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1189 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1190 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1192 c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1195 void au1x00_fixup_config_od(void)
1198 * c0_config.od (bit 19) was write only (and read as 0)
1199 * on the early revisions of Alchemy SOCs. It disables the bus
1200 * transaction overlapping and needs to be set to fix various errata.
1202 switch (read_c0_prid()) {
1203 case 0x00030100: /* Au1000 DA */
1204 case 0x00030201: /* Au1000 HA */
1205 case 0x00030202: /* Au1000 HB */
1206 case 0x01030200: /* Au1500 AB */
1208 * Au1100 errata actually keeps silence about this bit, so we set it
1209 * just in case for those revisions that require it to be set according
1210 * to arch/mips/au1000/common/cputable.c
1212 case 0x02030200: /* Au1100 AB */
1213 case 0x02030201: /* Au1100 BA */
1214 case 0x02030202: /* Au1100 BC */
1215 set_c0_config(1 << 19);
1220 static int __cpuinitdata cca = -1;
1222 static int __init cca_setup(char *str)
1224 get_option(&str, &cca);
1229 __setup("cca=", cca_setup);
1231 static void __cpuinit coherency_setup(void)
1233 if (cca < 0 || cca > 7)
1234 cca = read_c0_config() & CONF_CM_CMASK;
1235 _page_cachable_default = cca << _CACHE_SHIFT;
1237 pr_debug("Using cache attribute %d\n", cca);
1238 change_c0_config(CONF_CM_CMASK, cca);
1241 * c0_status.cu=0 specifies that updates by the sc instruction use
1242 * the coherency mode specified by the TLB; 1 means cachable
1243 * coherent update on write will be used. Not all processors have
1244 * this bit and; some wire it to zero, others like Toshiba had the
1245 * silly idea of putting something else there ...
1247 switch (current_cpu_type()) {
1254 clear_c0_config(CONF_CU);
1257 * We need to catch the early Alchemy SOCs with
1258 * the write-only co_config.od bit and set it back to one...
1260 case CPU_AU1000: /* rev. DA, HA, HB */
1261 case CPU_AU1100: /* rev. AB, BA, BC ?? */
1262 case CPU_AU1500: /* rev. AB */
1263 au1x00_fixup_config_od();
1268 void __cpuinit r4k_cache_init(void)
1270 extern void build_clear_page(void);
1271 extern void build_copy_page(void);
1272 extern char __weak except_vec2_generic;
1273 extern char __weak except_vec2_sb1;
1274 struct cpuinfo_mips *c = ¤t_cpu_data;
1276 switch (c->cputype) {
1279 set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1283 set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1290 r4k_blast_dcache_page_setup();
1291 r4k_blast_dcache_page_indexed_setup();
1292 r4k_blast_dcache_setup();
1293 r4k_blast_icache_page_setup();
1294 r4k_blast_icache_page_indexed_setup();
1295 r4k_blast_icache_setup();
1296 r4k_blast_scache_page_setup();
1297 r4k_blast_scache_page_indexed_setup();
1298 r4k_blast_scache_setup();
1301 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1302 * This code supports virtually indexed processors and will be
1303 * unnecessarily inefficient on physically indexed processors.
1305 if (c->dcache.linesz)
1306 shm_align_mask = max_t( unsigned long,
1307 c->dcache.sets * c->dcache.linesz - 1,
1310 shm_align_mask = PAGE_SIZE-1;
1312 __flush_cache_vmap = r4k__flush_cache_vmap;
1313 __flush_cache_vunmap = r4k__flush_cache_vunmap;
1315 flush_cache_all = cache_noop;
1316 __flush_cache_all = r4k___flush_cache_all;
1317 flush_cache_mm = r4k_flush_cache_mm;
1318 flush_cache_page = r4k_flush_cache_page;
1319 flush_cache_range = r4k_flush_cache_range;
1321 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1322 flush_icache_all = r4k_flush_icache_all;
1323 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
1324 flush_data_cache_page = r4k_flush_data_cache_page;
1325 flush_icache_range = r4k_flush_icache_range;
1327 #ifdef CONFIG_DMA_NONCOHERENT
1328 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1329 _dma_cache_wback = r4k_dma_cache_wback_inv;
1330 _dma_cache_inv = r4k_dma_cache_inv;
1335 local_r4k___flush_cache_all(NULL);