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/cacheops.h>
21 #include <asm/cpu-features.h>
24 #include <asm/pgtable.h>
25 #include <asm/r4kcache.h>
26 #include <asm/system.h>
27 #include <asm/mmu_context.h>
30 static unsigned long icache_size, dcache_size, scache_size;
33 * Dummy cache handling routines for machines without boardcaches
35 static void no_sc_noop(void) {}
37 static struct bcache_ops no_sc_ops = {
38 .bc_enable = (void *)no_sc_noop,
39 .bc_disable = (void *)no_sc_noop,
40 .bc_wback_inv = (void *)no_sc_noop,
41 .bc_inv = (void *)no_sc_noop
44 struct bcache_ops *bcops = &no_sc_ops;
46 #define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x2010)
47 #define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x2020)
49 #define R4600_HIT_CACHEOP_WAR_IMPL \
51 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
52 *(volatile unsigned long *)CKSEG1; \
53 if (R4600_V1_HIT_CACHEOP_WAR) \
54 __asm__ __volatile__("nop;nop;nop;nop"); \
57 static void (*r4k_blast_dcache_page)(unsigned long addr);
59 static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
61 R4600_HIT_CACHEOP_WAR_IMPL;
62 blast_dcache32_page(addr);
65 static inline void r4k_blast_dcache_page_setup(void)
67 unsigned long dc_lsize = cpu_dcache_line_size();
70 r4k_blast_dcache_page = blast_dcache16_page;
71 else if (dc_lsize == 32)
72 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
75 static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
77 static inline void r4k_blast_dcache_page_indexed_setup(void)
79 unsigned long dc_lsize = cpu_dcache_line_size();
82 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
83 else if (dc_lsize == 32)
84 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
87 static void (* r4k_blast_dcache)(void);
89 static inline void r4k_blast_dcache_setup(void)
91 unsigned long dc_lsize = cpu_dcache_line_size();
94 r4k_blast_dcache = blast_dcache16;
95 else if (dc_lsize == 32)
96 r4k_blast_dcache = blast_dcache32;
99 /* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
100 #define JUMP_TO_ALIGN(order) \
101 __asm__ __volatile__( \
103 ".align\t" #order "\n\t" \
106 #define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
107 #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
109 static inline void blast_r4600_v1_icache32(void)
113 local_irq_save(flags);
115 local_irq_restore(flags);
118 static inline void tx49_blast_icache32(void)
120 unsigned long start = INDEX_BASE;
121 unsigned long end = start + current_cpu_data.icache.waysize;
122 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
123 unsigned long ws_end = current_cpu_data.icache.ways <<
124 current_cpu_data.icache.waybit;
125 unsigned long ws, addr;
127 CACHE32_UNROLL32_ALIGN2;
128 /* I'm in even chunk. blast odd chunks */
129 for (ws = 0; ws < ws_end; ws += ws_inc)
130 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
131 cache32_unroll32(addr|ws,Index_Invalidate_I);
132 CACHE32_UNROLL32_ALIGN;
133 /* I'm in odd chunk. blast even chunks */
134 for (ws = 0; ws < ws_end; ws += ws_inc)
135 for (addr = start; addr < end; addr += 0x400 * 2)
136 cache32_unroll32(addr|ws,Index_Invalidate_I);
139 static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
143 local_irq_save(flags);
144 blast_icache32_page_indexed(page);
145 local_irq_restore(flags);
148 static inline void tx49_blast_icache32_page_indexed(unsigned long page)
150 unsigned long start = page;
151 unsigned long end = start + PAGE_SIZE;
152 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
153 unsigned long ws_end = current_cpu_data.icache.ways <<
154 current_cpu_data.icache.waybit;
155 unsigned long ws, addr;
157 CACHE32_UNROLL32_ALIGN2;
158 /* I'm in even chunk. blast odd chunks */
159 for (ws = 0; ws < ws_end; ws += ws_inc)
160 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
161 cache32_unroll32(addr|ws,Index_Invalidate_I);
162 CACHE32_UNROLL32_ALIGN;
163 /* I'm in odd chunk. blast even chunks */
164 for (ws = 0; ws < ws_end; ws += ws_inc)
165 for (addr = start; addr < end; addr += 0x400 * 2)
166 cache32_unroll32(addr|ws,Index_Invalidate_I);
169 static void (* r4k_blast_icache_page)(unsigned long addr);
171 static inline void r4k_blast_icache_page_setup(void)
173 unsigned long ic_lsize = cpu_icache_line_size();
176 r4k_blast_icache_page = blast_icache16_page;
177 else if (ic_lsize == 32)
178 r4k_blast_icache_page = blast_icache32_page;
179 else if (ic_lsize == 64)
180 r4k_blast_icache_page = blast_icache64_page;
184 static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
186 static inline void r4k_blast_icache_page_indexed_setup(void)
188 unsigned long ic_lsize = cpu_icache_line_size();
191 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
192 else if (ic_lsize == 32) {
193 if (TX49XX_ICACHE_INDEX_INV_WAR)
194 r4k_blast_icache_page_indexed =
195 tx49_blast_icache32_page_indexed;
196 else if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
197 r4k_blast_icache_page_indexed =
198 blast_icache32_r4600_v1_page_indexed;
200 r4k_blast_icache_page_indexed =
201 blast_icache32_page_indexed;
202 } else if (ic_lsize == 64)
203 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
206 static void (* r4k_blast_icache)(void);
208 static inline void r4k_blast_icache_setup(void)
210 unsigned long ic_lsize = cpu_icache_line_size();
213 r4k_blast_icache = blast_icache16;
214 else if (ic_lsize == 32) {
215 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
216 r4k_blast_icache = blast_r4600_v1_icache32;
217 else if (TX49XX_ICACHE_INDEX_INV_WAR)
218 r4k_blast_icache = tx49_blast_icache32;
220 r4k_blast_icache = blast_icache32;
221 } else if (ic_lsize == 64)
222 r4k_blast_icache = blast_icache64;
225 static void (* r4k_blast_scache_page)(unsigned long addr);
227 static inline void r4k_blast_scache_page_setup(void)
229 unsigned long sc_lsize = cpu_scache_line_size();
232 r4k_blast_scache_page = blast_scache16_page;
233 else if (sc_lsize == 32)
234 r4k_blast_scache_page = blast_scache32_page;
235 else if (sc_lsize == 64)
236 r4k_blast_scache_page = blast_scache64_page;
237 else if (sc_lsize == 128)
238 r4k_blast_scache_page = blast_scache128_page;
241 static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
243 static inline void r4k_blast_scache_page_indexed_setup(void)
245 unsigned long sc_lsize = cpu_scache_line_size();
248 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
249 else if (sc_lsize == 32)
250 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
251 else if (sc_lsize == 64)
252 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
253 else if (sc_lsize == 128)
254 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
257 static void (* r4k_blast_scache)(void);
259 static inline void r4k_blast_scache_setup(void)
261 unsigned long sc_lsize = cpu_scache_line_size();
264 r4k_blast_scache = blast_scache16;
265 else if (sc_lsize == 32)
266 r4k_blast_scache = blast_scache32;
267 else if (sc_lsize == 64)
268 r4k_blast_scache = blast_scache64;
269 else if (sc_lsize == 128)
270 r4k_blast_scache = blast_scache128;
274 * This is former mm's flush_cache_all() which really should be
275 * flush_cache_vunmap these days ...
277 static inline void local_r4k_flush_cache_all(void * args)
283 static void r4k_flush_cache_all(void)
285 if (!cpu_has_dc_aliases)
288 on_each_cpu(local_r4k_flush_cache_all, NULL, 1, 1);
291 static inline void local_r4k___flush_cache_all(void * args)
296 switch (current_cpu_data.cputype) {
307 static void r4k___flush_cache_all(void)
309 on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
312 static inline void local_r4k_flush_cache_range(void * args)
314 struct vm_area_struct *vma = args;
317 if (!(cpu_context(smp_processor_id(), vma->vm_mm)))
320 exec = vma->vm_flags & VM_EXEC;
321 if (cpu_has_dc_aliases || exec)
327 static void r4k_flush_cache_range(struct vm_area_struct *vma,
328 unsigned long start, unsigned long end)
330 on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
333 static inline void local_r4k_flush_cache_mm(void * args)
335 struct mm_struct *mm = args;
337 if (!cpu_context(smp_processor_id(), mm))
344 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
345 * only flush the primary caches but R10000 and R12000 behave sane ...
347 if (current_cpu_data.cputype == CPU_R4000SC ||
348 current_cpu_data.cputype == CPU_R4000MC ||
349 current_cpu_data.cputype == CPU_R4400SC ||
350 current_cpu_data.cputype == CPU_R4400MC)
354 static void r4k_flush_cache_mm(struct mm_struct *mm)
356 if (!cpu_has_dc_aliases)
359 on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
362 struct flush_cache_page_args {
363 struct vm_area_struct *vma;
367 static inline void local_r4k_flush_cache_page(void *args)
369 struct flush_cache_page_args *fcp_args = args;
370 struct vm_area_struct *vma = fcp_args->vma;
371 unsigned long page = fcp_args->page;
372 int exec = vma->vm_flags & VM_EXEC;
373 struct mm_struct *mm = vma->vm_mm;
379 pgdp = pgd_offset(mm, page);
380 pmdp = pmd_offset(pgdp, page);
381 ptep = pte_offset(pmdp, page);
384 * If the page isn't marked valid, the page cannot possibly be
387 if (!(pte_val(*ptep) & _PAGE_PRESENT))
391 * Doing flushes for another ASID than the current one is
392 * too difficult since stupid R4k caches do a TLB translation
393 * for every cache flush operation. So we do indexed flushes
394 * in that case, which doesn't overly flush the cache too much.
396 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
397 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
398 r4k_blast_dcache_page(page);
399 if (exec && !cpu_icache_snoops_remote_store)
400 r4k_blast_scache_page(page);
403 r4k_blast_icache_page(page);
409 * Do indexed flush, too much work to get the (possible) TLB refills
412 page = INDEX_BASE + (page & (dcache_size - 1));
413 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
414 r4k_blast_dcache_page_indexed(page);
415 if (exec && !cpu_icache_snoops_remote_store)
416 r4k_blast_scache_page_indexed(page);
419 if (cpu_has_vtag_icache) {
420 int cpu = smp_processor_id();
422 if (cpu_context(cpu, vma->vm_mm) != 0)
423 drop_mmu_context(vma->vm_mm, cpu);
425 r4k_blast_icache_page_indexed(page);
429 static void r4k_flush_cache_page(struct vm_area_struct *vma,
432 struct flush_cache_page_args args;
435 * If ownes no valid ASID yet, cannot possibly have gotten
436 * this page into the cache.
438 if (cpu_context(smp_processor_id(), vma->vm_mm) == 0)
444 on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
447 static inline void local_r4k_flush_data_cache_page(void * addr)
449 r4k_blast_dcache_page((unsigned long) addr);
452 static void r4k_flush_data_cache_page(unsigned long addr)
454 on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr, 1, 1);
457 struct flush_icache_range_args {
462 static inline void local_r4k_flush_icache_range(void *args)
464 struct flush_icache_range_args *fir_args = args;
465 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
466 unsigned long ic_lsize = current_cpu_data.icache.linesz;
467 unsigned long sc_lsize = current_cpu_data.scache.linesz;
468 unsigned long start = fir_args->start;
469 unsigned long end = fir_args->end;
470 unsigned long addr, aend;
472 if (!cpu_has_ic_fills_f_dc) {
473 if (end - start > dcache_size) {
476 addr = start & ~(dc_lsize - 1);
477 aend = (end - 1) & ~(dc_lsize - 1);
480 /* Hit_Writeback_Inv_D */
481 protected_writeback_dcache_line(addr);
488 if (!cpu_icache_snoops_remote_store) {
489 if (end - start > scache_size) {
492 addr = start & ~(sc_lsize - 1);
493 aend = (end - 1) & ~(sc_lsize - 1);
496 /* Hit_Writeback_Inv_D */
497 protected_writeback_scache_line(addr);
506 if (end - start > icache_size)
509 addr = start & ~(ic_lsize - 1);
510 aend = (end - 1) & ~(ic_lsize - 1);
512 /* Hit_Invalidate_I */
513 protected_flush_icache_line(addr);
521 static void r4k_flush_icache_range(unsigned long start, unsigned long end)
523 struct flush_icache_range_args args;
528 on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
532 * Ok, this seriously sucks. We use them to flush a user page but don't
533 * know the virtual address, so we have to blast away the whole icache
534 * which is significantly more expensive than the real thing. Otoh we at
535 * least know the kernel address of the page so we can flush it
539 struct flush_icache_page_args {
540 struct vm_area_struct *vma;
544 static inline void local_r4k_flush_icache_page(void *args)
546 struct flush_icache_page_args *fip_args = args;
547 struct vm_area_struct *vma = fip_args->vma;
548 struct page *page = fip_args->page;
551 * Tricky ... Because we don't know the virtual address we've got the
552 * choice of either invalidating the entire primary and secondary
553 * caches or invalidating the secondary caches also. With the subset
554 * enforcment on R4000SC, R4400SC, R10000 and R12000 invalidating the
555 * secondary cache will result in any entries in the primary caches
556 * also getting invalidated which hopefully is a bit more economical.
558 if (cpu_has_subset_pcaches) {
559 unsigned long addr = (unsigned long) page_address(page);
561 r4k_blast_scache_page(addr);
562 ClearPageDcacheDirty(page);
567 if (!cpu_has_ic_fills_f_dc) {
568 unsigned long addr = (unsigned long) page_address(page);
569 r4k_blast_dcache_page(addr);
570 if (!cpu_icache_snoops_remote_store)
571 r4k_blast_scache_page(addr);
572 ClearPageDcacheDirty(page);
576 * We're not sure of the virtual address(es) involved here, so
577 * we have to flush the entire I-cache.
579 if (cpu_has_vtag_icache) {
580 int cpu = smp_processor_id();
582 if (cpu_context(cpu, vma->vm_mm) != 0)
583 drop_mmu_context(vma->vm_mm, cpu);
588 static void r4k_flush_icache_page(struct vm_area_struct *vma,
591 struct flush_icache_page_args args;
594 * If there's no context yet, or the page isn't executable, no I-cache
597 if (!(vma->vm_flags & VM_EXEC))
603 on_each_cpu(local_r4k_flush_icache_page, &args, 1, 1);
607 #ifdef CONFIG_DMA_NONCOHERENT
609 static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
611 unsigned long end, a;
613 /* Catch bad driver code */
616 if (cpu_has_subset_pcaches) {
617 unsigned long sc_lsize = current_cpu_data.scache.linesz;
619 if (size >= scache_size) {
624 a = addr & ~(sc_lsize - 1);
625 end = (addr + size - 1) & ~(sc_lsize - 1);
627 flush_scache_line(a); /* Hit_Writeback_Inv_SD */
636 * Either no secondary cache or the available caches don't have the
637 * subset property so we have to flush the primary caches
640 if (size >= dcache_size) {
643 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
645 R4600_HIT_CACHEOP_WAR_IMPL;
646 a = addr & ~(dc_lsize - 1);
647 end = (addr + size - 1) & ~(dc_lsize - 1);
649 flush_dcache_line(a); /* Hit_Writeback_Inv_D */
656 bc_wback_inv(addr, size);
659 static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
661 unsigned long end, a;
663 /* Catch bad driver code */
666 if (cpu_has_subset_pcaches) {
667 unsigned long sc_lsize = current_cpu_data.scache.linesz;
669 if (size >= scache_size) {
674 a = addr & ~(sc_lsize - 1);
675 end = (addr + size - 1) & ~(sc_lsize - 1);
677 flush_scache_line(a); /* Hit_Writeback_Inv_SD */
685 if (size >= dcache_size) {
688 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
690 R4600_HIT_CACHEOP_WAR_IMPL;
691 a = addr & ~(dc_lsize - 1);
692 end = (addr + size - 1) & ~(dc_lsize - 1);
694 flush_dcache_line(a); /* Hit_Writeback_Inv_D */
703 #endif /* CONFIG_DMA_NONCOHERENT */
706 * While we're protected against bad userland addresses we don't care
707 * very much about what happens in that case. Usually a segmentation
708 * fault will dump the process later on anyway ...
710 static void local_r4k_flush_cache_sigtramp(void * arg)
712 unsigned long ic_lsize = current_cpu_data.icache.linesz;
713 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
714 unsigned long sc_lsize = current_cpu_data.scache.linesz;
715 unsigned long addr = (unsigned long) arg;
717 R4600_HIT_CACHEOP_WAR_IMPL;
718 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
719 if (!cpu_icache_snoops_remote_store)
720 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
721 protected_flush_icache_line(addr & ~(ic_lsize - 1));
722 if (MIPS4K_ICACHE_REFILL_WAR) {
723 __asm__ __volatile__ (
738 : "i" (Hit_Invalidate_I));
740 if (MIPS_CACHE_SYNC_WAR)
741 __asm__ __volatile__ ("sync");
744 static void r4k_flush_cache_sigtramp(unsigned long addr)
746 on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
749 static void r4k_flush_icache_all(void)
751 if (cpu_has_vtag_icache)
755 static inline void rm7k_erratum31(void)
757 const unsigned long ic_lsize = 32;
760 /* RM7000 erratum #31. The icache is screwed at startup. */
764 for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
765 __asm__ __volatile__ (
768 "cache\t%1, 0(%0)\n\t"
769 "cache\t%1, 0x1000(%0)\n\t"
770 "cache\t%1, 0x2000(%0)\n\t"
771 "cache\t%1, 0x3000(%0)\n\t"
772 "cache\t%2, 0(%0)\n\t"
773 "cache\t%2, 0x1000(%0)\n\t"
774 "cache\t%2, 0x2000(%0)\n\t"
775 "cache\t%2, 0x3000(%0)\n\t"
776 "cache\t%1, 0(%0)\n\t"
777 "cache\t%1, 0x1000(%0)\n\t"
778 "cache\t%1, 0x2000(%0)\n\t"
779 "cache\t%1, 0x3000(%0)\n\t"
783 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
787 static char *way_string[] __initdata = { NULL, "direct mapped", "2-way",
788 "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
791 static void __init probe_pcache(void)
793 struct cpuinfo_mips *c = ¤t_cpu_data;
794 unsigned int config = read_c0_config();
795 unsigned int prid = read_c0_prid();
796 unsigned long config1;
799 switch (c->cputype) {
800 case CPU_R4600: /* QED style two way caches? */
804 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
805 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
807 c->icache.waybit = ffs(icache_size/2) - 1;
809 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
810 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
812 c->dcache.waybit= ffs(dcache_size/2) - 1;
814 c->options |= MIPS_CPU_CACHE_CDEX_P;
819 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
820 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
824 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
825 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
827 c->dcache.waybit = 0;
829 c->options |= MIPS_CPU_CACHE_CDEX_P;
833 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
834 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
838 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
839 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
841 c->dcache.waybit = 0;
843 c->options |= MIPS_CPU_CACHE_CDEX_P;
853 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
854 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
856 c->icache.waybit = 0; /* doesn't matter */
858 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
859 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
861 c->dcache.waybit = 0; /* does not matter */
863 c->options |= MIPS_CPU_CACHE_CDEX_P;
868 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
869 c->icache.linesz = 64;
871 c->icache.waybit = 0;
873 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
874 c->dcache.linesz = 32;
876 c->dcache.waybit = 0;
878 c->options |= MIPS_CPU_PREFETCH;
882 write_c0_config(config & ~CONF_EB);
884 /* Workaround for cache instruction bug of VR4131 */
885 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
886 c->processor_id == 0x0c82U) {
887 config &= ~0x00000030U;
888 config |= 0x00410000U;
889 write_c0_config(config);
891 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
892 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
894 c->icache.waybit = ffs(icache_size/2) - 1;
896 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
897 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
899 c->dcache.waybit = ffs(dcache_size/2) - 1;
901 c->options |= MIPS_CPU_CACHE_CDEX_P;
910 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
911 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
913 c->icache.waybit = 0; /* doesn't matter */
915 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
916 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
918 c->dcache.waybit = 0; /* does not matter */
920 c->options |= MIPS_CPU_CACHE_CDEX_P;
927 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
928 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
930 c->icache.waybit = ffs(icache_size / c->icache.ways) - 1;
932 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
933 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
935 c->dcache.waybit = ffs(dcache_size / c->dcache.ways) - 1;
937 #if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
938 c->options |= MIPS_CPU_CACHE_CDEX_P;
940 c->options |= MIPS_CPU_PREFETCH;
944 if (!(config & MIPS_CONF_M))
945 panic("Don't know how to probe P-caches on this cpu.");
948 * So we seem to be a MIPS32 or MIPS64 CPU
949 * So let's probe the I-cache ...
951 config1 = read_c0_config1();
953 if ((lsize = ((config1 >> 19) & 7)))
954 c->icache.linesz = 2 << lsize;
956 c->icache.linesz = lsize;
957 c->icache.sets = 64 << ((config1 >> 22) & 7);
958 c->icache.ways = 1 + ((config1 >> 16) & 7);
960 icache_size = c->icache.sets *
963 c->icache.waybit = ffs(icache_size/c->icache.ways) - 1;
965 if (config & 0x8) /* VI bit */
966 c->icache.flags |= MIPS_CACHE_VTAG;
969 * Now probe the MIPS32 / MIPS64 data cache.
973 if ((lsize = ((config1 >> 10) & 7)))
974 c->dcache.linesz = 2 << lsize;
976 c->dcache.linesz= lsize;
977 c->dcache.sets = 64 << ((config1 >> 13) & 7);
978 c->dcache.ways = 1 + ((config1 >> 7) & 7);
980 dcache_size = c->dcache.sets *
983 c->dcache.waybit = ffs(dcache_size/c->dcache.ways) - 1;
985 c->options |= MIPS_CPU_PREFETCH;
990 * Processor configuration sanity check for the R4000SC erratum
991 * #5. With page sizes larger than 32kB there is no possibility
992 * to get a VCE exception anymore so we don't care about this
993 * misconfiguration. The case is rather theoretical anyway;
994 * presumably no vendor is shipping his hardware in the "bad"
997 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
998 !(config & CONF_SC) && c->icache.linesz != 16 &&
1000 panic("Improper R4000SC processor configuration detected");
1002 /* compute a couple of other cache variables */
1003 c->icache.waysize = icache_size / c->icache.ways;
1004 c->dcache.waysize = dcache_size / c->dcache.ways;
1006 c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
1007 c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
1010 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
1011 * 2-way virtually indexed so normally would suffer from aliases. So
1012 * normally they'd suffer from aliases but magic in the hardware deals
1013 * with that for us so we don't need to take care ourselves.
1015 if (c->cputype != CPU_R10000 && c->cputype != CPU_R12000)
1016 if (c->dcache.waysize > PAGE_SIZE)
1017 c->dcache.flags |= MIPS_CACHE_ALIASES;
1019 switch (c->cputype) {
1022 * Some older 20Kc chips doesn't have the 'VI' bit in
1023 * the config register.
1025 c->icache.flags |= MIPS_CACHE_VTAG;
1029 c->icache.flags |= MIPS_CACHE_IC_F_DC;
1033 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1035 cpu_has_vtag_icache ? "virtually tagged" : "physically tagged",
1036 way_string[c->icache.ways], c->icache.linesz);
1038 printk("Primary data cache %ldkB, %s, linesize %d bytes.\n",
1039 dcache_size >> 10, way_string[c->dcache.ways], c->dcache.linesz);
1043 * If you even _breathe_ on this function, look at the gcc output and make sure
1044 * it does not pop things on and off the stack for the cache sizing loop that
1045 * executes in KSEG1 space or else you will crash and burn badly. You have
1048 static int __init probe_scache(void)
1050 extern unsigned long stext;
1051 unsigned long flags, addr, begin, end, pow2;
1052 unsigned int config = read_c0_config();
1053 struct cpuinfo_mips *c = ¤t_cpu_data;
1056 if (config & CONF_SC)
1059 begin = (unsigned long) &stext;
1060 begin &= ~((4 * 1024 * 1024) - 1);
1061 end = begin + (4 * 1024 * 1024);
1064 * This is such a bitch, you'd think they would make it easy to do
1065 * this. Away you daemons of stupidity!
1067 local_irq_save(flags);
1069 /* Fill each size-multiple cache line with a valid tag. */
1071 for (addr = begin; addr < end; addr = (begin + pow2)) {
1072 unsigned long *p = (unsigned long *) addr;
1073 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1077 /* Load first line with zero (therefore invalid) tag. */
1080 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1081 cache_op(Index_Store_Tag_I, begin);
1082 cache_op(Index_Store_Tag_D, begin);
1083 cache_op(Index_Store_Tag_SD, begin);
1085 /* Now search for the wrap around point. */
1086 pow2 = (128 * 1024);
1088 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1089 cache_op(Index_Load_Tag_SD, addr);
1090 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1091 if (!read_c0_taglo())
1095 local_irq_restore(flags);
1099 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1101 c->dcache.waybit = 0; /* does not matter */
1106 typedef int (*probe_func_t)(unsigned long);
1107 extern int r5k_sc_init(void);
1108 extern int rm7k_sc_init(void);
1110 static void __init setup_scache(void)
1112 struct cpuinfo_mips *c = ¤t_cpu_data;
1113 unsigned int config = read_c0_config();
1114 probe_func_t probe_scache_kseg1;
1118 * Do the probing thing on R4000SC and R4400SC processors. Other
1119 * processors don't have a S-cache that would be relevant to the
1120 * Linux memory managment.
1122 switch (c->cputype) {
1127 probe_scache_kseg1 = (probe_func_t) (CKSEG1ADDR(&probe_scache));
1128 sc_present = probe_scache_kseg1(config);
1130 c->options |= MIPS_CPU_CACHE_CDEX_S;
1135 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1136 c->scache.linesz = 64 << ((config >> 13) & 1);
1138 c->scache.waybit= 0;
1144 #ifdef CONFIG_R5000_CPU_SCACHE
1151 #ifdef CONFIG_RM7000_CPU_SCACHE
1163 if ((c->isa_level == MIPS_CPU_ISA_M32 ||
1164 c->isa_level == MIPS_CPU_ISA_M64) &&
1165 !(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1166 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1168 /* compute a couple of other cache variables */
1169 c->scache.waysize = scache_size / c->scache.ways;
1171 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1173 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1174 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1176 c->options |= MIPS_CPU_SUBSET_CACHES;
1179 static inline void coherency_setup(void)
1181 change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
1184 * c0_status.cu=0 specifies that updates by the sc instruction use
1185 * the coherency mode specified by the TLB; 1 means cachable
1186 * coherent update on write will be used. Not all processors have
1187 * this bit and; some wire it to zero, others like Toshiba had the
1188 * silly idea of putting something else there ...
1190 switch (current_cpu_data.cputype) {
1197 clear_c0_config(CONF_CU);
1202 void __init ld_mmu_r4xx0(void)
1204 extern void build_clear_page(void);
1205 extern void build_copy_page(void);
1206 extern char except_vec2_generic;
1207 struct cpuinfo_mips *c = ¤t_cpu_data;
1209 /* Default cache error handler for R4000 and R5000 family */
1210 memcpy((void *)(CAC_BASE + 0x100), &except_vec2_generic, 0x80);
1211 memcpy((void *)(UNCAC_BASE + 0x100), &except_vec2_generic, 0x80);
1216 if (c->dcache.sets * c->dcache.ways > PAGE_SIZE)
1217 c->dcache.flags |= MIPS_CACHE_ALIASES;
1219 r4k_blast_dcache_page_setup();
1220 r4k_blast_dcache_page_indexed_setup();
1221 r4k_blast_dcache_setup();
1222 r4k_blast_icache_page_setup();
1223 r4k_blast_icache_page_indexed_setup();
1224 r4k_blast_icache_setup();
1225 r4k_blast_scache_page_setup();
1226 r4k_blast_scache_page_indexed_setup();
1227 r4k_blast_scache_setup();
1230 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1231 * This code supports virtually indexed processors and will be
1232 * unnecessarily inefficient on physically indexed processors.
1234 shm_align_mask = max_t( unsigned long,
1235 c->dcache.sets * c->dcache.linesz - 1,
1238 flush_cache_all = r4k_flush_cache_all;
1239 __flush_cache_all = r4k___flush_cache_all;
1240 flush_cache_mm = r4k_flush_cache_mm;
1241 flush_cache_page = r4k_flush_cache_page;
1242 flush_icache_page = r4k_flush_icache_page;
1243 flush_cache_range = r4k_flush_cache_range;
1245 flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1246 flush_icache_all = r4k_flush_icache_all;
1247 flush_data_cache_page = r4k_flush_data_cache_page;
1248 flush_icache_range = r4k_flush_icache_range;
1250 #ifdef CONFIG_DMA_NONCOHERENT
1251 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1252 _dma_cache_wback = r4k_dma_cache_wback_inv;
1253 _dma_cache_inv = r4k_dma_cache_inv;
1256 __flush_cache_all();