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/config.h>
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
15 #include <linux/bitops.h>
17 #include <asm/bcache.h>
18 #include <asm/bootinfo.h>
19 #include <asm/cache.h>
20 #include <asm/cacheops.h>
22 #include <asm/cpu-features.h>
25 #include <asm/pgtable.h>
26 #include <asm/r4kcache.h>
27 #include <asm/system.h>
28 #include <asm/mmu_context.h>
30 #include <asm/cacheflush.h> /* for run_uncached() */
34 * Special Variant of smp_call_function for use by cache functions:
37 * o collapses to normal function call on UP kernels
38 * o collapses to normal function call on systems with a single shared
41 static inline void r4k_on_each_cpu(void (*func) (void *info), void *info,
46 #if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
47 smp_call_function(func, info, retry, wait);
56 static unsigned long icache_size __read_mostly;
57 static unsigned long dcache_size __read_mostly;
58 static unsigned long scache_size __read_mostly;
61 * Dummy cache handling routines for machines without boardcaches
63 static void no_sc_noop(void) {}
65 static struct bcache_ops no_sc_ops = {
66 .bc_enable = (void *)no_sc_noop,
67 .bc_disable = (void *)no_sc_noop,
68 .bc_wback_inv = (void *)no_sc_noop,
69 .bc_inv = (void *)no_sc_noop
72 struct bcache_ops *bcops = &no_sc_ops;
74 #define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x00002010)
75 #define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x00002020)
77 #define R4600_HIT_CACHEOP_WAR_IMPL \
79 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
80 *(volatile unsigned long *)CKSEG1; \
81 if (R4600_V1_HIT_CACHEOP_WAR) \
82 __asm__ __volatile__("nop;nop;nop;nop"); \
85 static void (*r4k_blast_dcache_page)(unsigned long addr);
87 static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
89 R4600_HIT_CACHEOP_WAR_IMPL;
90 blast_dcache32_page(addr);
93 static inline void r4k_blast_dcache_page_setup(void)
95 unsigned long dc_lsize = cpu_dcache_line_size();
98 r4k_blast_dcache_page = blast_dcache16_page;
99 else if (dc_lsize == 32)
100 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
103 static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
105 static inline void r4k_blast_dcache_page_indexed_setup(void)
107 unsigned long dc_lsize = cpu_dcache_line_size();
110 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
111 else if (dc_lsize == 32)
112 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
115 static void (* r4k_blast_dcache)(void);
117 static inline void r4k_blast_dcache_setup(void)
119 unsigned long dc_lsize = cpu_dcache_line_size();
122 r4k_blast_dcache = blast_dcache16;
123 else if (dc_lsize == 32)
124 r4k_blast_dcache = blast_dcache32;
127 /* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
128 #define JUMP_TO_ALIGN(order) \
129 __asm__ __volatile__( \
131 ".align\t" #order "\n\t" \
134 #define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
135 #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
137 static inline void blast_r4600_v1_icache32(void)
141 local_irq_save(flags);
143 local_irq_restore(flags);
146 static inline void tx49_blast_icache32(void)
148 unsigned long start = INDEX_BASE;
149 unsigned long end = start + current_cpu_data.icache.waysize;
150 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
151 unsigned long ws_end = current_cpu_data.icache.ways <<
152 current_cpu_data.icache.waybit;
153 unsigned long ws, addr;
155 CACHE32_UNROLL32_ALIGN2;
156 /* I'm in even chunk. blast odd chunks */
157 for (ws = 0; ws < ws_end; ws += ws_inc)
158 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
159 cache32_unroll32(addr|ws,Index_Invalidate_I);
160 CACHE32_UNROLL32_ALIGN;
161 /* I'm in odd chunk. blast even chunks */
162 for (ws = 0; ws < ws_end; ws += ws_inc)
163 for (addr = start; addr < end; addr += 0x400 * 2)
164 cache32_unroll32(addr|ws,Index_Invalidate_I);
167 static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
171 local_irq_save(flags);
172 blast_icache32_page_indexed(page);
173 local_irq_restore(flags);
176 static inline void tx49_blast_icache32_page_indexed(unsigned long page)
178 unsigned long indexmask = current_cpu_data.icache.waysize - 1;
179 unsigned long start = INDEX_BASE + (page & indexmask);
180 unsigned long end = start + PAGE_SIZE;
181 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
182 unsigned long ws_end = current_cpu_data.icache.ways <<
183 current_cpu_data.icache.waybit;
184 unsigned long ws, addr;
186 CACHE32_UNROLL32_ALIGN2;
187 /* I'm in even chunk. blast odd chunks */
188 for (ws = 0; ws < ws_end; ws += ws_inc)
189 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
190 cache32_unroll32(addr|ws,Index_Invalidate_I);
191 CACHE32_UNROLL32_ALIGN;
192 /* I'm in odd chunk. blast even chunks */
193 for (ws = 0; ws < ws_end; ws += ws_inc)
194 for (addr = start; addr < end; addr += 0x400 * 2)
195 cache32_unroll32(addr|ws,Index_Invalidate_I);
198 static void (* r4k_blast_icache_page)(unsigned long addr);
200 static inline void r4k_blast_icache_page_setup(void)
202 unsigned long ic_lsize = cpu_icache_line_size();
205 r4k_blast_icache_page = blast_icache16_page;
206 else if (ic_lsize == 32)
207 r4k_blast_icache_page = blast_icache32_page;
208 else if (ic_lsize == 64)
209 r4k_blast_icache_page = blast_icache64_page;
213 static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
215 static inline void r4k_blast_icache_page_indexed_setup(void)
217 unsigned long ic_lsize = cpu_icache_line_size();
220 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
221 else if (ic_lsize == 32) {
222 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
223 r4k_blast_icache_page_indexed =
224 blast_icache32_r4600_v1_page_indexed;
225 else if (TX49XX_ICACHE_INDEX_INV_WAR)
226 r4k_blast_icache_page_indexed =
227 tx49_blast_icache32_page_indexed;
229 r4k_blast_icache_page_indexed =
230 blast_icache32_page_indexed;
231 } else if (ic_lsize == 64)
232 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
235 static void (* r4k_blast_icache)(void);
237 static inline void r4k_blast_icache_setup(void)
239 unsigned long ic_lsize = cpu_icache_line_size();
242 r4k_blast_icache = blast_icache16;
243 else if (ic_lsize == 32) {
244 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
245 r4k_blast_icache = blast_r4600_v1_icache32;
246 else if (TX49XX_ICACHE_INDEX_INV_WAR)
247 r4k_blast_icache = tx49_blast_icache32;
249 r4k_blast_icache = blast_icache32;
250 } else if (ic_lsize == 64)
251 r4k_blast_icache = blast_icache64;
254 static void (* r4k_blast_scache_page)(unsigned long addr);
256 static inline void r4k_blast_scache_page_setup(void)
258 unsigned long sc_lsize = cpu_scache_line_size();
260 if (scache_size == 0)
261 r4k_blast_scache_page = (void *)no_sc_noop;
262 else if (sc_lsize == 16)
263 r4k_blast_scache_page = blast_scache16_page;
264 else if (sc_lsize == 32)
265 r4k_blast_scache_page = blast_scache32_page;
266 else if (sc_lsize == 64)
267 r4k_blast_scache_page = blast_scache64_page;
268 else if (sc_lsize == 128)
269 r4k_blast_scache_page = blast_scache128_page;
272 static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
274 static inline void r4k_blast_scache_page_indexed_setup(void)
276 unsigned long sc_lsize = cpu_scache_line_size();
278 if (scache_size == 0)
279 r4k_blast_scache_page_indexed = (void *)no_sc_noop;
280 else if (sc_lsize == 16)
281 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
282 else if (sc_lsize == 32)
283 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
284 else if (sc_lsize == 64)
285 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
286 else if (sc_lsize == 128)
287 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
290 static void (* r4k_blast_scache)(void);
292 static inline void r4k_blast_scache_setup(void)
294 unsigned long sc_lsize = cpu_scache_line_size();
296 if (scache_size == 0)
297 r4k_blast_scache = (void *)no_sc_noop;
298 else if (sc_lsize == 16)
299 r4k_blast_scache = blast_scache16;
300 else if (sc_lsize == 32)
301 r4k_blast_scache = blast_scache32;
302 else if (sc_lsize == 64)
303 r4k_blast_scache = blast_scache64;
304 else if (sc_lsize == 128)
305 r4k_blast_scache = blast_scache128;
309 * This is former mm's flush_cache_all() which really should be
310 * flush_cache_vunmap these days ...
312 static inline void local_r4k_flush_cache_all(void * args)
318 static void r4k_flush_cache_all(void)
320 if (!cpu_has_dc_aliases)
323 r4k_on_each_cpu(local_r4k_flush_cache_all, NULL, 1, 1);
326 static inline void local_r4k___flush_cache_all(void * args)
331 switch (current_cpu_data.cputype) {
343 static void r4k___flush_cache_all(void)
345 r4k_on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
348 static inline void local_r4k_flush_cache_range(void * args)
350 struct vm_area_struct *vma = args;
353 if (!(cpu_context(smp_processor_id(), vma->vm_mm)))
356 exec = vma->vm_flags & VM_EXEC;
357 if (cpu_has_dc_aliases || exec)
363 static void r4k_flush_cache_range(struct vm_area_struct *vma,
364 unsigned long start, unsigned long end)
366 r4k_on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
369 static inline void local_r4k_flush_cache_mm(void * args)
371 struct mm_struct *mm = args;
373 if (!cpu_context(smp_processor_id(), mm))
380 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
381 * only flush the primary caches but R10000 and R12000 behave sane ...
383 if (current_cpu_data.cputype == CPU_R4000SC ||
384 current_cpu_data.cputype == CPU_R4000MC ||
385 current_cpu_data.cputype == CPU_R4400SC ||
386 current_cpu_data.cputype == CPU_R4400MC)
390 static void r4k_flush_cache_mm(struct mm_struct *mm)
392 if (!cpu_has_dc_aliases)
395 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
398 struct flush_cache_page_args {
399 struct vm_area_struct *vma;
404 static inline void local_r4k_flush_cache_page(void *args)
406 struct flush_cache_page_args *fcp_args = args;
407 struct vm_area_struct *vma = fcp_args->vma;
408 unsigned long addr = fcp_args->addr;
409 unsigned long paddr = fcp_args->pfn << PAGE_SHIFT;
410 int exec = vma->vm_flags & VM_EXEC;
411 struct mm_struct *mm = vma->vm_mm;
418 * If ownes no valid ASID yet, cannot possibly have gotten
419 * this page into the cache.
421 if (cpu_context(smp_processor_id(), mm) == 0)
425 pgdp = pgd_offset(mm, addr);
426 pudp = pud_offset(pgdp, addr);
427 pmdp = pmd_offset(pudp, addr);
428 ptep = pte_offset(pmdp, addr);
431 * If the page isn't marked valid, the page cannot possibly be
434 if (!(pte_val(*ptep) & _PAGE_PRESENT))
438 * Doing flushes for another ASID than the current one is
439 * too difficult since stupid R4k caches do a TLB translation
440 * for every cache flush operation. So we do indexed flushes
441 * in that case, which doesn't overly flush the cache too much.
443 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
444 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
445 r4k_blast_dcache_page(addr);
446 if (exec && !cpu_icache_snoops_remote_store)
447 r4k_blast_scache_page(addr);
450 r4k_blast_icache_page(addr);
456 * Do indexed flush, too much work to get the (possible) TLB refills
459 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
460 r4k_blast_dcache_page_indexed(cpu_has_pindexed_dcache ?
462 if (exec && !cpu_icache_snoops_remote_store) {
463 r4k_blast_scache_page_indexed(paddr);
467 if (cpu_has_vtag_icache) {
468 int cpu = smp_processor_id();
470 if (cpu_context(cpu, mm) != 0)
471 drop_mmu_context(mm, cpu);
473 r4k_blast_icache_page_indexed(addr);
477 static void r4k_flush_cache_page(struct vm_area_struct *vma,
478 unsigned long addr, unsigned long pfn)
480 struct flush_cache_page_args args;
486 r4k_on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
489 static inline void local_r4k_flush_data_cache_page(void * addr)
491 r4k_blast_dcache_page((unsigned long) addr);
494 static void r4k_flush_data_cache_page(unsigned long addr)
496 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr, 1, 1);
499 struct flush_icache_range_args {
504 static inline void local_r4k_flush_icache_range(void *args)
506 struct flush_icache_range_args *fir_args = args;
507 unsigned long start = fir_args->start;
508 unsigned long end = fir_args->end;
510 if (!cpu_has_ic_fills_f_dc) {
511 if (end - start > dcache_size) {
514 R4600_HIT_CACHEOP_WAR_IMPL;
515 protected_blast_dcache_range(start, end);
518 if (!cpu_icache_snoops_remote_store && scache_size) {
519 if (end - start > scache_size)
522 protected_blast_scache_range(start, end);
526 if (end - start > icache_size)
529 protected_blast_icache_range(start, end);
532 static void r4k_flush_icache_range(unsigned long start, unsigned long end)
534 struct flush_icache_range_args args;
539 r4k_on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
540 instruction_hazard();
544 * Ok, this seriously sucks. We use them to flush a user page but don't
545 * know the virtual address, so we have to blast away the whole icache
546 * which is significantly more expensive than the real thing. Otoh we at
547 * least know the kernel address of the page so we can flush it
551 struct flush_icache_page_args {
552 struct vm_area_struct *vma;
556 static inline void local_r4k_flush_icache_page(void *args)
558 struct flush_icache_page_args *fip_args = args;
559 struct vm_area_struct *vma = fip_args->vma;
560 struct page *page = fip_args->page;
563 * Tricky ... Because we don't know the virtual address we've got the
564 * choice of either invalidating the entire primary and secondary
565 * caches or invalidating the secondary caches also. With the subset
566 * enforcment on R4000SC, R4400SC, R10000 and R12000 invalidating the
567 * secondary cache will result in any entries in the primary caches
568 * also getting invalidated which hopefully is a bit more economical.
570 if (cpu_has_subset_pcaches) {
571 unsigned long addr = (unsigned long) page_address(page);
573 r4k_blast_scache_page(addr);
574 ClearPageDcacheDirty(page);
579 if (!cpu_has_ic_fills_f_dc) {
580 unsigned long addr = (unsigned long) page_address(page);
581 r4k_blast_dcache_page(addr);
582 if (!cpu_icache_snoops_remote_store)
583 r4k_blast_scache_page(addr);
584 ClearPageDcacheDirty(page);
588 * We're not sure of the virtual address(es) involved here, so
589 * we have to flush the entire I-cache.
591 if (cpu_has_vtag_icache) {
592 int cpu = smp_processor_id();
594 if (cpu_context(cpu, vma->vm_mm) != 0)
595 drop_mmu_context(vma->vm_mm, cpu);
600 static void r4k_flush_icache_page(struct vm_area_struct *vma,
603 struct flush_icache_page_args args;
606 * If there's no context yet, or the page isn't executable, no I-cache
609 if (!(vma->vm_flags & VM_EXEC))
615 r4k_on_each_cpu(local_r4k_flush_icache_page, &args, 1, 1);
619 #ifdef CONFIG_DMA_NONCOHERENT
621 static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
623 /* Catch bad driver code */
626 if (cpu_has_subset_pcaches) {
627 if (size >= scache_size)
630 blast_scache_range(addr, addr + size);
635 * Either no secondary cache or the available caches don't have the
636 * subset property so we have to flush the primary caches
639 if (size >= dcache_size) {
642 R4600_HIT_CACHEOP_WAR_IMPL;
643 blast_dcache_range(addr, addr + size);
646 bc_wback_inv(addr, size);
649 static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
651 /* Catch bad driver code */
654 if (cpu_has_subset_pcaches) {
655 if (size >= scache_size)
658 blast_scache_range(addr, addr + size);
662 if (size >= dcache_size) {
665 R4600_HIT_CACHEOP_WAR_IMPL;
666 blast_dcache_range(addr, addr + size);
671 #endif /* CONFIG_DMA_NONCOHERENT */
674 * While we're protected against bad userland addresses we don't care
675 * very much about what happens in that case. Usually a segmentation
676 * fault will dump the process later on anyway ...
678 static void local_r4k_flush_cache_sigtramp(void * arg)
680 unsigned long ic_lsize = cpu_icache_line_size();
681 unsigned long dc_lsize = cpu_dcache_line_size();
682 unsigned long sc_lsize = cpu_scache_line_size();
683 unsigned long addr = (unsigned long) arg;
685 R4600_HIT_CACHEOP_WAR_IMPL;
686 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
687 if (!cpu_icache_snoops_remote_store && scache_size)
688 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
689 protected_flush_icache_line(addr & ~(ic_lsize - 1));
690 if (MIPS4K_ICACHE_REFILL_WAR) {
691 __asm__ __volatile__ (
706 : "i" (Hit_Invalidate_I));
708 if (MIPS_CACHE_SYNC_WAR)
709 __asm__ __volatile__ ("sync");
712 static void r4k_flush_cache_sigtramp(unsigned long addr)
714 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
717 static void r4k_flush_icache_all(void)
719 if (cpu_has_vtag_icache)
723 static inline void rm7k_erratum31(void)
725 const unsigned long ic_lsize = 32;
728 /* RM7000 erratum #31. The icache is screwed at startup. */
732 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
733 __asm__ __volatile__ (
737 "cache\t%1, 0(%0)\n\t"
738 "cache\t%1, 0x1000(%0)\n\t"
739 "cache\t%1, 0x2000(%0)\n\t"
740 "cache\t%1, 0x3000(%0)\n\t"
741 "cache\t%2, 0(%0)\n\t"
742 "cache\t%2, 0x1000(%0)\n\t"
743 "cache\t%2, 0x2000(%0)\n\t"
744 "cache\t%2, 0x3000(%0)\n\t"
745 "cache\t%1, 0(%0)\n\t"
746 "cache\t%1, 0x1000(%0)\n\t"
747 "cache\t%1, 0x2000(%0)\n\t"
748 "cache\t%1, 0x3000(%0)\n\t"
751 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
755 static char *way_string[] __initdata = { NULL, "direct mapped", "2-way",
756 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
759 static void __init probe_pcache(void)
761 struct cpuinfo_mips *c = ¤t_cpu_data;
762 unsigned int config = read_c0_config();
763 unsigned int prid = read_c0_prid();
764 unsigned long config1;
767 switch (c->cputype) {
768 case CPU_R4600: /* QED style two way caches? */
772 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
773 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
775 c->icache.waybit = __ffs(icache_size/2);
777 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
778 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
780 c->dcache.waybit= __ffs(dcache_size/2);
782 c->options |= MIPS_CPU_CACHE_CDEX_P;
787 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
788 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
792 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
793 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
795 c->dcache.waybit = 0;
797 c->options |= MIPS_CPU_CACHE_CDEX_P;
801 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
802 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
806 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
807 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
809 c->dcache.waybit = 0;
811 c->options |= MIPS_CPU_CACHE_CDEX_P;
812 c->options |= MIPS_CPU_PREFETCH;
822 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
823 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
825 c->icache.waybit = 0; /* doesn't matter */
827 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
828 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
830 c->dcache.waybit = 0; /* does not matter */
832 c->options |= MIPS_CPU_CACHE_CDEX_P;
838 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
839 c->icache.linesz = 64;
841 c->icache.waybit = 0;
843 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
844 c->dcache.linesz = 32;
846 c->dcache.waybit = 0;
848 c->options |= MIPS_CPU_PREFETCH;
852 write_c0_config(config & ~CONF_EB);
854 /* Workaround for cache instruction bug of VR4131 */
855 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
856 c->processor_id == 0x0c82U) {
857 config &= ~0x00000030U;
858 config |= 0x00410000U;
859 write_c0_config(config);
861 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
862 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
864 c->icache.waybit = __ffs(icache_size/2);
866 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
867 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
869 c->dcache.waybit = __ffs(dcache_size/2);
871 c->options |= MIPS_CPU_CACHE_CDEX_P;
880 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
881 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
883 c->icache.waybit = 0; /* doesn't matter */
885 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
886 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
888 c->dcache.waybit = 0; /* does not matter */
890 c->options |= MIPS_CPU_CACHE_CDEX_P;
897 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
898 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
900 c->icache.waybit = __ffs(icache_size / c->icache.ways);
902 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
903 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
905 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
907 #if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
908 c->options |= MIPS_CPU_CACHE_CDEX_P;
910 c->options |= MIPS_CPU_PREFETCH;
914 if (!(config & MIPS_CONF_M))
915 panic("Don't know how to probe P-caches on this cpu.");
918 * So we seem to be a MIPS32 or MIPS64 CPU
919 * So let's probe the I-cache ...
921 config1 = read_c0_config1();
923 if ((lsize = ((config1 >> 19) & 7)))
924 c->icache.linesz = 2 << lsize;
926 c->icache.linesz = lsize;
927 c->icache.sets = 64 << ((config1 >> 22) & 7);
928 c->icache.ways = 1 + ((config1 >> 16) & 7);
930 icache_size = c->icache.sets *
933 c->icache.waybit = __ffs(icache_size/c->icache.ways);
935 if (config & 0x8) /* VI bit */
936 c->icache.flags |= MIPS_CACHE_VTAG;
939 * Now probe the MIPS32 / MIPS64 data cache.
943 if ((lsize = ((config1 >> 10) & 7)))
944 c->dcache.linesz = 2 << lsize;
946 c->dcache.linesz= lsize;
947 c->dcache.sets = 64 << ((config1 >> 13) & 7);
948 c->dcache.ways = 1 + ((config1 >> 7) & 7);
950 dcache_size = c->dcache.sets *
953 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
955 c->options |= MIPS_CPU_PREFETCH;
960 * Processor configuration sanity check for the R4000SC erratum
961 * #5. With page sizes larger than 32kB there is no possibility
962 * to get a VCE exception anymore so we don't care about this
963 * misconfiguration. The case is rather theoretical anyway;
964 * presumably no vendor is shipping his hardware in the "bad"
967 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
968 !(config & CONF_SC) && c->icache.linesz != 16 &&
970 panic("Improper R4000SC processor configuration detected");
972 /* compute a couple of other cache variables */
973 c->icache.waysize = icache_size / c->icache.ways;
974 c->dcache.waysize = dcache_size / c->dcache.ways;
976 c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
977 c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
980 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
981 * 2-way virtually indexed so normally would suffer from aliases. So
982 * normally they'd suffer from aliases but magic in the hardware deals
983 * with that for us so we don't need to take care ourselves.
985 switch (c->cputype) {
988 c->dcache.flags |= MIPS_CACHE_PINDEX;
996 if (!(read_c0_config7() & (1 << 16)))
998 if (c->dcache.waysize > PAGE_SIZE)
999 c->dcache.flags |= MIPS_CACHE_ALIASES;
1002 switch (c->cputype) {
1005 * Some older 20Kc chips doesn't have the 'VI' bit in
1006 * the config register.
1008 c->icache.flags |= MIPS_CACHE_VTAG;
1016 c->icache.flags |= MIPS_CACHE_IC_F_DC;
1020 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1022 cpu_has_vtag_icache ? "virtually tagged" : "physically tagged",
1023 way_string[c->icache.ways], c->icache.linesz);
1025 printk("Primary data cache %ldkB, %s, linesize %d bytes.\n",
1026 dcache_size >> 10, way_string[c->dcache.ways], c->dcache.linesz);
1030 * If you even _breathe_ on this function, look at the gcc output and make sure
1031 * it does not pop things on and off the stack for the cache sizing loop that
1032 * executes in KSEG1 space or else you will crash and burn badly. You have
1035 static int __init probe_scache(void)
1037 extern unsigned long stext;
1038 unsigned long flags, addr, begin, end, pow2;
1039 unsigned int config = read_c0_config();
1040 struct cpuinfo_mips *c = ¤t_cpu_data;
1043 if (config & CONF_SC)
1046 begin = (unsigned long) &stext;
1047 begin &= ~((4 * 1024 * 1024) - 1);
1048 end = begin + (4 * 1024 * 1024);
1051 * This is such a bitch, you'd think they would make it easy to do
1052 * this. Away you daemons of stupidity!
1054 local_irq_save(flags);
1056 /* Fill each size-multiple cache line with a valid tag. */
1058 for (addr = begin; addr < end; addr = (begin + pow2)) {
1059 unsigned long *p = (unsigned long *) addr;
1060 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1064 /* Load first line with zero (therefore invalid) tag. */
1067 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1068 cache_op(Index_Store_Tag_I, begin);
1069 cache_op(Index_Store_Tag_D, begin);
1070 cache_op(Index_Store_Tag_SD, begin);
1072 /* Now search for the wrap around point. */
1073 pow2 = (128 * 1024);
1075 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1076 cache_op(Index_Load_Tag_SD, addr);
1077 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1078 if (!read_c0_taglo())
1082 local_irq_restore(flags);
1086 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1088 c->dcache.waybit = 0; /* does not matter */
1093 extern int r5k_sc_init(void);
1094 extern int rm7k_sc_init(void);
1096 static void __init setup_scache(void)
1098 struct cpuinfo_mips *c = ¤t_cpu_data;
1099 unsigned int config = read_c0_config();
1103 * Do the probing thing on R4000SC and R4400SC processors. Other
1104 * processors don't have a S-cache that would be relevant to the
1105 * Linux memory managment.
1107 switch (c->cputype) {
1112 sc_present = run_uncached(probe_scache);
1114 c->options |= MIPS_CPU_CACHE_CDEX_S;
1120 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1121 c->scache.linesz = 64 << ((config >> 13) & 1);
1123 c->scache.waybit= 0;
1129 #ifdef CONFIG_R5000_CPU_SCACHE
1136 #ifdef CONFIG_RM7000_CPU_SCACHE
1148 if ((c->isa_level == MIPS_CPU_ISA_M32R1 ||
1149 c->isa_level == MIPS_CPU_ISA_M64R1) &&
1150 !(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1151 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1153 /* compute a couple of other cache variables */
1154 c->scache.waysize = scache_size / c->scache.ways;
1156 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1158 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1159 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1161 c->options |= MIPS_CPU_SUBSET_CACHES;
1164 void au1x00_fixup_config_od(void)
1167 * c0_config.od (bit 19) was write only (and read as 0)
1168 * on the early revisions of Alchemy SOCs. It disables the bus
1169 * transaction overlapping and needs to be set to fix various errata.
1171 switch (read_c0_prid()) {
1172 case 0x00030100: /* Au1000 DA */
1173 case 0x00030201: /* Au1000 HA */
1174 case 0x00030202: /* Au1000 HB */
1175 case 0x01030200: /* Au1500 AB */
1177 * Au1100 errata actually keeps silence about this bit, so we set it
1178 * just in case for those revisions that require it to be set according
1179 * to arch/mips/au1000/common/cputable.c
1181 case 0x02030200: /* Au1100 AB */
1182 case 0x02030201: /* Au1100 BA */
1183 case 0x02030202: /* Au1100 BC */
1184 set_c0_config(1 << 19);
1189 static inline void coherency_setup(void)
1191 change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
1194 * c0_status.cu=0 specifies that updates by the sc instruction use
1195 * the coherency mode specified by the TLB; 1 means cachable
1196 * coherent update on write will be used. Not all processors have
1197 * this bit and; some wire it to zero, others like Toshiba had the
1198 * silly idea of putting something else there ...
1200 switch (current_cpu_data.cputype) {
1207 clear_c0_config(CONF_CU);
1210 * We need to catch the ealry Alchemy SOCs with
1211 * the write-only co_config.od bit and set it back to one...
1213 case CPU_AU1000: /* rev. DA, HA, HB */
1214 case CPU_AU1100: /* rev. AB, BA, BC ?? */
1215 case CPU_AU1500: /* rev. AB */
1216 au1x00_fixup_config_od();
1221 void __init r4k_cache_init(void)
1223 extern void build_clear_page(void);
1224 extern void build_copy_page(void);
1225 extern char except_vec2_generic;
1226 struct cpuinfo_mips *c = ¤t_cpu_data;
1228 /* Default cache error handler for R4000 and R5000 family */
1229 set_uncached_handler (0x100, &except_vec2_generic, 0x80);
1234 r4k_blast_dcache_page_setup();
1235 r4k_blast_dcache_page_indexed_setup();
1236 r4k_blast_dcache_setup();
1237 r4k_blast_icache_page_setup();
1238 r4k_blast_icache_page_indexed_setup();
1239 r4k_blast_icache_setup();
1240 r4k_blast_scache_page_setup();
1241 r4k_blast_scache_page_indexed_setup();
1242 r4k_blast_scache_setup();
1245 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1246 * This code supports virtually indexed processors and will be
1247 * unnecessarily inefficient on physically indexed processors.
1249 shm_align_mask = max_t( unsigned long,
1250 c->dcache.sets * c->dcache.linesz - 1,
1253 flush_cache_all = r4k_flush_cache_all;
1254 __flush_cache_all = r4k___flush_cache_all;
1255 flush_cache_mm = r4k_flush_cache_mm;
1256 flush_cache_page = r4k_flush_cache_page;
1257 flush_icache_page = r4k_flush_icache_page;
1258 flush_cache_range = r4k_flush_cache_range;
1260 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1261 flush_icache_all = r4k_flush_icache_all;
1262 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
1263 flush_data_cache_page = r4k_flush_data_cache_page;
1264 flush_icache_range = r4k_flush_icache_range;
1266 #ifdef CONFIG_DMA_NONCOHERENT
1267 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1268 _dma_cache_wback = r4k_dma_cache_wback_inv;
1269 _dma_cache_inv = r4k_dma_cache_inv;
1274 local_r4k___flush_cache_all(NULL);