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) {
342 static void r4k___flush_cache_all(void)
344 r4k_on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
347 static inline void local_r4k_flush_cache_range(void * args)
349 struct vm_area_struct *vma = args;
352 if (!(cpu_context(smp_processor_id(), vma->vm_mm)))
355 exec = vma->vm_flags & VM_EXEC;
356 if (cpu_has_dc_aliases || exec)
362 static void r4k_flush_cache_range(struct vm_area_struct *vma,
363 unsigned long start, unsigned long end)
365 r4k_on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
368 static inline void local_r4k_flush_cache_mm(void * args)
370 struct mm_struct *mm = args;
372 if (!cpu_context(smp_processor_id(), mm))
379 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
380 * only flush the primary caches but R10000 and R12000 behave sane ...
382 if (current_cpu_data.cputype == CPU_R4000SC ||
383 current_cpu_data.cputype == CPU_R4000MC ||
384 current_cpu_data.cputype == CPU_R4400SC ||
385 current_cpu_data.cputype == CPU_R4400MC)
389 static void r4k_flush_cache_mm(struct mm_struct *mm)
391 if (!cpu_has_dc_aliases)
394 r4k_on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
397 struct flush_cache_page_args {
398 struct vm_area_struct *vma;
403 static inline void local_r4k_flush_cache_page(void *args)
405 struct flush_cache_page_args *fcp_args = args;
406 struct vm_area_struct *vma = fcp_args->vma;
407 unsigned long addr = fcp_args->addr;
408 unsigned long paddr = fcp_args->pfn << PAGE_SHIFT;
409 int exec = vma->vm_flags & VM_EXEC;
410 struct mm_struct *mm = vma->vm_mm;
417 * If ownes no valid ASID yet, cannot possibly have gotten
418 * this page into the cache.
420 if (cpu_context(smp_processor_id(), mm) == 0)
424 pgdp = pgd_offset(mm, addr);
425 pudp = pud_offset(pgdp, addr);
426 pmdp = pmd_offset(pudp, addr);
427 ptep = pte_offset(pmdp, addr);
430 * If the page isn't marked valid, the page cannot possibly be
433 if (!(pte_val(*ptep) & _PAGE_PRESENT))
437 * Doing flushes for another ASID than the current one is
438 * too difficult since stupid R4k caches do a TLB translation
439 * for every cache flush operation. So we do indexed flushes
440 * in that case, which doesn't overly flush the cache too much.
442 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
443 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
444 r4k_blast_dcache_page(addr);
445 if (exec && !cpu_icache_snoops_remote_store)
446 r4k_blast_scache_page(addr);
449 r4k_blast_icache_page(addr);
455 * Do indexed flush, too much work to get the (possible) TLB refills
458 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
459 r4k_blast_dcache_page_indexed(cpu_has_pindexed_dcache ?
461 if (exec && !cpu_icache_snoops_remote_store) {
462 r4k_blast_scache_page_indexed(paddr);
466 if (cpu_has_vtag_icache) {
467 int cpu = smp_processor_id();
469 if (cpu_context(cpu, mm) != 0)
470 drop_mmu_context(mm, cpu);
472 r4k_blast_icache_page_indexed(addr);
476 static void r4k_flush_cache_page(struct vm_area_struct *vma,
477 unsigned long addr, unsigned long pfn)
479 struct flush_cache_page_args args;
485 r4k_on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
488 static inline void local_r4k_flush_data_cache_page(void * addr)
490 r4k_blast_dcache_page((unsigned long) addr);
493 static void r4k_flush_data_cache_page(unsigned long addr)
495 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr, 1, 1);
498 struct flush_icache_range_args {
503 static inline void local_r4k_flush_icache_range(void *args)
505 struct flush_icache_range_args *fir_args = args;
506 unsigned long start = fir_args->start;
507 unsigned long end = fir_args->end;
509 if (!cpu_has_ic_fills_f_dc) {
510 if (end - start > dcache_size) {
513 R4600_HIT_CACHEOP_WAR_IMPL;
514 protected_blast_dcache_range(start, end);
517 if (!cpu_icache_snoops_remote_store && scache_size) {
518 if (end - start > scache_size)
521 protected_blast_scache_range(start, end);
525 if (end - start > icache_size)
528 protected_blast_icache_range(start, end);
531 static void r4k_flush_icache_range(unsigned long start, unsigned long end)
533 struct flush_icache_range_args args;
538 r4k_on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
539 instruction_hazard();
543 * Ok, this seriously sucks. We use them to flush a user page but don't
544 * know the virtual address, so we have to blast away the whole icache
545 * which is significantly more expensive than the real thing. Otoh we at
546 * least know the kernel address of the page so we can flush it
550 struct flush_icache_page_args {
551 struct vm_area_struct *vma;
555 static inline void local_r4k_flush_icache_page(void *args)
557 struct flush_icache_page_args *fip_args = args;
558 struct vm_area_struct *vma = fip_args->vma;
559 struct page *page = fip_args->page;
562 * Tricky ... Because we don't know the virtual address we've got the
563 * choice of either invalidating the entire primary and secondary
564 * caches or invalidating the secondary caches also. With the subset
565 * enforcment on R4000SC, R4400SC, R10000 and R12000 invalidating the
566 * secondary cache will result in any entries in the primary caches
567 * also getting invalidated which hopefully is a bit more economical.
569 if (cpu_has_subset_pcaches) {
570 unsigned long addr = (unsigned long) page_address(page);
572 r4k_blast_scache_page(addr);
573 ClearPageDcacheDirty(page);
578 if (!cpu_has_ic_fills_f_dc) {
579 unsigned long addr = (unsigned long) page_address(page);
580 r4k_blast_dcache_page(addr);
581 if (!cpu_icache_snoops_remote_store)
582 r4k_blast_scache_page(addr);
583 ClearPageDcacheDirty(page);
587 * We're not sure of the virtual address(es) involved here, so
588 * we have to flush the entire I-cache.
590 if (cpu_has_vtag_icache) {
591 int cpu = smp_processor_id();
593 if (cpu_context(cpu, vma->vm_mm) != 0)
594 drop_mmu_context(vma->vm_mm, cpu);
599 static void r4k_flush_icache_page(struct vm_area_struct *vma,
602 struct flush_icache_page_args args;
605 * If there's no context yet, or the page isn't executable, no I-cache
608 if (!(vma->vm_flags & VM_EXEC))
614 r4k_on_each_cpu(local_r4k_flush_icache_page, &args, 1, 1);
618 #ifdef CONFIG_DMA_NONCOHERENT
620 static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
622 /* Catch bad driver code */
625 if (cpu_has_subset_pcaches) {
626 if (size >= scache_size)
629 blast_scache_range(addr, addr + size);
634 * Either no secondary cache or the available caches don't have the
635 * subset property so we have to flush the primary caches
638 if (size >= dcache_size) {
641 R4600_HIT_CACHEOP_WAR_IMPL;
642 blast_dcache_range(addr, addr + size);
645 bc_wback_inv(addr, size);
648 static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
650 /* Catch bad driver code */
653 if (cpu_has_subset_pcaches) {
654 if (size >= scache_size)
657 blast_scache_range(addr, addr + size);
661 if (size >= dcache_size) {
664 R4600_HIT_CACHEOP_WAR_IMPL;
665 blast_dcache_range(addr, addr + size);
670 #endif /* CONFIG_DMA_NONCOHERENT */
673 * While we're protected against bad userland addresses we don't care
674 * very much about what happens in that case. Usually a segmentation
675 * fault will dump the process later on anyway ...
677 static void local_r4k_flush_cache_sigtramp(void * arg)
679 unsigned long ic_lsize = cpu_icache_line_size();
680 unsigned long dc_lsize = cpu_dcache_line_size();
681 unsigned long sc_lsize = cpu_scache_line_size();
682 unsigned long addr = (unsigned long) arg;
684 R4600_HIT_CACHEOP_WAR_IMPL;
685 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
686 if (!cpu_icache_snoops_remote_store && scache_size)
687 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
688 protected_flush_icache_line(addr & ~(ic_lsize - 1));
689 if (MIPS4K_ICACHE_REFILL_WAR) {
690 __asm__ __volatile__ (
705 : "i" (Hit_Invalidate_I));
707 if (MIPS_CACHE_SYNC_WAR)
708 __asm__ __volatile__ ("sync");
711 static void r4k_flush_cache_sigtramp(unsigned long addr)
713 r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
716 static void r4k_flush_icache_all(void)
718 if (cpu_has_vtag_icache)
722 static inline void rm7k_erratum31(void)
724 const unsigned long ic_lsize = 32;
727 /* RM7000 erratum #31. The icache is screwed at startup. */
731 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
732 __asm__ __volatile__ (
736 "cache\t%1, 0(%0)\n\t"
737 "cache\t%1, 0x1000(%0)\n\t"
738 "cache\t%1, 0x2000(%0)\n\t"
739 "cache\t%1, 0x3000(%0)\n\t"
740 "cache\t%2, 0(%0)\n\t"
741 "cache\t%2, 0x1000(%0)\n\t"
742 "cache\t%2, 0x2000(%0)\n\t"
743 "cache\t%2, 0x3000(%0)\n\t"
744 "cache\t%1, 0(%0)\n\t"
745 "cache\t%1, 0x1000(%0)\n\t"
746 "cache\t%1, 0x2000(%0)\n\t"
747 "cache\t%1, 0x3000(%0)\n\t"
750 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
754 static char *way_string[] __initdata = { NULL, "direct mapped", "2-way",
755 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
758 static void __init probe_pcache(void)
760 struct cpuinfo_mips *c = ¤t_cpu_data;
761 unsigned int config = read_c0_config();
762 unsigned int prid = read_c0_prid();
763 unsigned long config1;
766 switch (c->cputype) {
767 case CPU_R4600: /* QED style two way caches? */
771 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
772 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
774 c->icache.waybit = __ffs(icache_size/2);
776 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
777 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
779 c->dcache.waybit= __ffs(dcache_size/2);
781 c->options |= MIPS_CPU_CACHE_CDEX_P;
786 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
787 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
791 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
792 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
794 c->dcache.waybit = 0;
796 c->options |= MIPS_CPU_CACHE_CDEX_P;
800 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
801 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
805 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
806 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
808 c->dcache.waybit = 0;
810 c->options |= MIPS_CPU_CACHE_CDEX_P;
811 c->options |= MIPS_CPU_PREFETCH;
821 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
822 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
824 c->icache.waybit = 0; /* doesn't matter */
826 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
827 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
829 c->dcache.waybit = 0; /* does not matter */
831 c->options |= MIPS_CPU_CACHE_CDEX_P;
836 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
837 c->icache.linesz = 64;
839 c->icache.waybit = 0;
841 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
842 c->dcache.linesz = 32;
844 c->dcache.waybit = 0;
846 c->options |= MIPS_CPU_PREFETCH;
850 write_c0_config(config & ~CONF_EB);
852 /* Workaround for cache instruction bug of VR4131 */
853 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
854 c->processor_id == 0x0c82U) {
855 config &= ~0x00000030U;
856 config |= 0x00410000U;
857 write_c0_config(config);
859 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
860 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
862 c->icache.waybit = __ffs(icache_size/2);
864 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
865 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
867 c->dcache.waybit = __ffs(dcache_size/2);
869 c->options |= MIPS_CPU_CACHE_CDEX_P;
878 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
879 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
881 c->icache.waybit = 0; /* doesn't matter */
883 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
884 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
886 c->dcache.waybit = 0; /* does not matter */
888 c->options |= MIPS_CPU_CACHE_CDEX_P;
895 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
896 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
898 c->icache.waybit = __ffs(icache_size / c->icache.ways);
900 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
901 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
903 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
905 #if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
906 c->options |= MIPS_CPU_CACHE_CDEX_P;
908 c->options |= MIPS_CPU_PREFETCH;
912 if (!(config & MIPS_CONF_M))
913 panic("Don't know how to probe P-caches on this cpu.");
916 * So we seem to be a MIPS32 or MIPS64 CPU
917 * So let's probe the I-cache ...
919 config1 = read_c0_config1();
921 if ((lsize = ((config1 >> 19) & 7)))
922 c->icache.linesz = 2 << lsize;
924 c->icache.linesz = lsize;
925 c->icache.sets = 64 << ((config1 >> 22) & 7);
926 c->icache.ways = 1 + ((config1 >> 16) & 7);
928 icache_size = c->icache.sets *
931 c->icache.waybit = __ffs(icache_size/c->icache.ways);
933 if (config & 0x8) /* VI bit */
934 c->icache.flags |= MIPS_CACHE_VTAG;
937 * Now probe the MIPS32 / MIPS64 data cache.
941 if ((lsize = ((config1 >> 10) & 7)))
942 c->dcache.linesz = 2 << lsize;
944 c->dcache.linesz= lsize;
945 c->dcache.sets = 64 << ((config1 >> 13) & 7);
946 c->dcache.ways = 1 + ((config1 >> 7) & 7);
948 dcache_size = c->dcache.sets *
951 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
953 c->options |= MIPS_CPU_PREFETCH;
958 * Processor configuration sanity check for the R4000SC erratum
959 * #5. With page sizes larger than 32kB there is no possibility
960 * to get a VCE exception anymore so we don't care about this
961 * misconfiguration. The case is rather theoretical anyway;
962 * presumably no vendor is shipping his hardware in the "bad"
965 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
966 !(config & CONF_SC) && c->icache.linesz != 16 &&
968 panic("Improper R4000SC processor configuration detected");
970 /* compute a couple of other cache variables */
971 c->icache.waysize = icache_size / c->icache.ways;
972 c->dcache.waysize = dcache_size / c->dcache.ways;
974 c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
975 c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
978 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
979 * 2-way virtually indexed so normally would suffer from aliases. So
980 * normally they'd suffer from aliases but magic in the hardware deals
981 * with that for us so we don't need to take care ourselves.
983 switch (c->cputype) {
986 c->dcache.flags |= MIPS_CACHE_PINDEX;
993 if (!(read_c0_config7() & (1 << 16)))
995 if (c->dcache.waysize > PAGE_SIZE)
996 c->dcache.flags |= MIPS_CACHE_ALIASES;
999 switch (c->cputype) {
1002 * Some older 20Kc chips doesn't have the 'VI' bit in
1003 * the config register.
1005 c->icache.flags |= MIPS_CACHE_VTAG;
1013 c->icache.flags |= MIPS_CACHE_IC_F_DC;
1017 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1019 cpu_has_vtag_icache ? "virtually tagged" : "physically tagged",
1020 way_string[c->icache.ways], c->icache.linesz);
1022 printk("Primary data cache %ldkB, %s, linesize %d bytes.\n",
1023 dcache_size >> 10, way_string[c->dcache.ways], c->dcache.linesz);
1027 * If you even _breathe_ on this function, look at the gcc output and make sure
1028 * it does not pop things on and off the stack for the cache sizing loop that
1029 * executes in KSEG1 space or else you will crash and burn badly. You have
1032 static int __init probe_scache(void)
1034 extern unsigned long stext;
1035 unsigned long flags, addr, begin, end, pow2;
1036 unsigned int config = read_c0_config();
1037 struct cpuinfo_mips *c = ¤t_cpu_data;
1040 if (config & CONF_SC)
1043 begin = (unsigned long) &stext;
1044 begin &= ~((4 * 1024 * 1024) - 1);
1045 end = begin + (4 * 1024 * 1024);
1048 * This is such a bitch, you'd think they would make it easy to do
1049 * this. Away you daemons of stupidity!
1051 local_irq_save(flags);
1053 /* Fill each size-multiple cache line with a valid tag. */
1055 for (addr = begin; addr < end; addr = (begin + pow2)) {
1056 unsigned long *p = (unsigned long *) addr;
1057 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1061 /* Load first line with zero (therefore invalid) tag. */
1064 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1065 cache_op(Index_Store_Tag_I, begin);
1066 cache_op(Index_Store_Tag_D, begin);
1067 cache_op(Index_Store_Tag_SD, begin);
1069 /* Now search for the wrap around point. */
1070 pow2 = (128 * 1024);
1072 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1073 cache_op(Index_Load_Tag_SD, addr);
1074 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1075 if (!read_c0_taglo())
1079 local_irq_restore(flags);
1083 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1085 c->dcache.waybit = 0; /* does not matter */
1090 extern int r5k_sc_init(void);
1091 extern int rm7k_sc_init(void);
1093 static void __init setup_scache(void)
1095 struct cpuinfo_mips *c = ¤t_cpu_data;
1096 unsigned int config = read_c0_config();
1100 * Do the probing thing on R4000SC and R4400SC processors. Other
1101 * processors don't have a S-cache that would be relevant to the
1102 * Linux memory managment.
1104 switch (c->cputype) {
1109 sc_present = run_uncached(probe_scache);
1111 c->options |= MIPS_CPU_CACHE_CDEX_S;
1116 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1117 c->scache.linesz = 64 << ((config >> 13) & 1);
1119 c->scache.waybit= 0;
1125 #ifdef CONFIG_R5000_CPU_SCACHE
1132 #ifdef CONFIG_RM7000_CPU_SCACHE
1144 if ((c->isa_level == MIPS_CPU_ISA_M32R1 ||
1145 c->isa_level == MIPS_CPU_ISA_M64R1) &&
1146 !(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1147 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1149 /* compute a couple of other cache variables */
1150 c->scache.waysize = scache_size / c->scache.ways;
1152 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1154 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1155 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1157 c->options |= MIPS_CPU_SUBSET_CACHES;
1160 static inline void coherency_setup(void)
1162 change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
1165 * c0_status.cu=0 specifies that updates by the sc instruction use
1166 * the coherency mode specified by the TLB; 1 means cachable
1167 * coherent update on write will be used. Not all processors have
1168 * this bit and; some wire it to zero, others like Toshiba had the
1169 * silly idea of putting something else there ...
1171 switch (current_cpu_data.cputype) {
1178 clear_c0_config(CONF_CU);
1183 void __init r4k_cache_init(void)
1185 extern void build_clear_page(void);
1186 extern void build_copy_page(void);
1187 extern char except_vec2_generic;
1188 struct cpuinfo_mips *c = ¤t_cpu_data;
1190 /* Default cache error handler for R4000 and R5000 family */
1191 set_uncached_handler (0x100, &except_vec2_generic, 0x80);
1196 r4k_blast_dcache_page_setup();
1197 r4k_blast_dcache_page_indexed_setup();
1198 r4k_blast_dcache_setup();
1199 r4k_blast_icache_page_setup();
1200 r4k_blast_icache_page_indexed_setup();
1201 r4k_blast_icache_setup();
1202 r4k_blast_scache_page_setup();
1203 r4k_blast_scache_page_indexed_setup();
1204 r4k_blast_scache_setup();
1207 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1208 * This code supports virtually indexed processors and will be
1209 * unnecessarily inefficient on physically indexed processors.
1211 shm_align_mask = max_t( unsigned long,
1212 c->dcache.sets * c->dcache.linesz - 1,
1215 flush_cache_all = r4k_flush_cache_all;
1216 __flush_cache_all = r4k___flush_cache_all;
1217 flush_cache_mm = r4k_flush_cache_mm;
1218 flush_cache_page = r4k_flush_cache_page;
1219 flush_icache_page = r4k_flush_icache_page;
1220 flush_cache_range = r4k_flush_cache_range;
1222 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1223 flush_icache_all = r4k_flush_icache_all;
1224 local_flush_data_cache_page = local_r4k_flush_data_cache_page;
1225 flush_data_cache_page = r4k_flush_data_cache_page;
1226 flush_icache_range = r4k_flush_icache_range;
1228 #ifdef CONFIG_DMA_NONCOHERENT
1229 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1230 _dma_cache_wback = r4k_dma_cache_wback_inv;
1231 _dma_cache_inv = r4k_dma_cache_inv;
1236 local_r4k___flush_cache_all(NULL);