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>
23 #include <asm/pgtable.h>
24 #include <asm/r4kcache.h>
25 #include <asm/system.h>
26 #include <asm/mmu_context.h>
29 static unsigned long icache_size, dcache_size, scache_size;
32 * Dummy cache handling routines for machines without boardcaches
34 static void no_sc_noop(void) {}
36 static struct bcache_ops no_sc_ops = {
37 .bc_enable = (void *)no_sc_noop,
38 .bc_disable = (void *)no_sc_noop,
39 .bc_wback_inv = (void *)no_sc_noop,
40 .bc_inv = (void *)no_sc_noop
43 struct bcache_ops *bcops = &no_sc_ops;
45 #define cpu_is_r4600_v1_x() ((read_c0_prid() & 0xfffffff0) == 0x2010)
46 #define cpu_is_r4600_v2_x() ((read_c0_prid() & 0xfffffff0) == 0x2020)
48 #define R4600_HIT_CACHEOP_WAR_IMPL \
50 if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x()) \
51 *(volatile unsigned long *)KSEG1; \
52 if (R4600_V1_HIT_CACHEOP_WAR) \
53 __asm__ __volatile__("nop;nop;nop;nop"); \
56 static void (* r4k_blast_dcache_page)(unsigned long addr);
58 static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
60 R4600_HIT_CACHEOP_WAR_IMPL;
61 blast_dcache32_page(addr);
64 static inline void r4k_blast_dcache_page_setup(void)
66 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
69 r4k_blast_dcache_page = blast_dcache16_page;
70 else if (dc_lsize == 32)
71 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
74 static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
76 static void r4k_blast_dcache_page_indexed_setup(void)
78 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
81 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
82 else if (dc_lsize == 32)
83 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
86 static void (* r4k_blast_dcache)(void);
88 static inline void r4k_blast_dcache_setup(void)
90 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
93 r4k_blast_dcache = blast_dcache16;
94 else if (dc_lsize == 32)
95 r4k_blast_dcache = blast_dcache32;
98 /* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
99 #define JUMP_TO_ALIGN(order) \
100 __asm__ __volatile__( \
102 ".align\t" #order "\n\t" \
105 #define CACHE32_UNROLL32_ALIGN JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
106 #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
108 static inline void blast_r4600_v1_icache32(void)
112 local_irq_save(flags);
114 local_irq_restore(flags);
117 static inline void tx49_blast_icache32(void)
119 unsigned long start = KSEG0;
120 unsigned long end = start + current_cpu_data.icache.waysize;
121 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
122 unsigned long ws_end = current_cpu_data.icache.ways <<
123 current_cpu_data.icache.waybit;
124 unsigned long ws, addr;
126 CACHE32_UNROLL32_ALIGN2;
127 /* I'm in even chunk. blast odd chunks */
128 for (ws = 0; ws < ws_end; ws += ws_inc)
129 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
130 cache32_unroll32(addr|ws,Index_Invalidate_I);
131 CACHE32_UNROLL32_ALIGN;
132 /* I'm in odd chunk. blast even chunks */
133 for (ws = 0; ws < ws_end; ws += ws_inc)
134 for (addr = start; addr < end; addr += 0x400 * 2)
135 cache32_unroll32(addr|ws,Index_Invalidate_I);
138 static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
142 local_irq_save(flags);
143 blast_icache32_page_indexed(page);
144 local_irq_restore(flags);
147 static inline void tx49_blast_icache32_page_indexed(unsigned long page)
149 unsigned long start = page;
150 unsigned long end = start + PAGE_SIZE;
151 unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
152 unsigned long ws_end = current_cpu_data.icache.ways <<
153 current_cpu_data.icache.waybit;
154 unsigned long ws, addr;
156 CACHE32_UNROLL32_ALIGN2;
157 /* I'm in even chunk. blast odd chunks */
158 for (ws = 0; ws < ws_end; ws += ws_inc)
159 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
160 cache32_unroll32(addr|ws,Index_Invalidate_I);
161 CACHE32_UNROLL32_ALIGN;
162 /* I'm in odd chunk. blast even chunks */
163 for (ws = 0; ws < ws_end; ws += ws_inc)
164 for (addr = start; addr < end; addr += 0x400 * 2)
165 cache32_unroll32(addr|ws,Index_Invalidate_I);
168 static void (* r4k_blast_icache_page)(unsigned long addr);
170 static inline void r4k_blast_icache_page_setup(void)
172 unsigned long ic_lsize = current_cpu_data.icache.linesz;
175 r4k_blast_icache_page = blast_icache16_page;
176 else if (ic_lsize == 32)
177 r4k_blast_icache_page = blast_icache32_page;
178 else if (ic_lsize == 64)
179 r4k_blast_icache_page = blast_icache64_page;
182 static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
184 static inline void r4k_blast_icache_page_indexed_setup(void)
186 unsigned long ic_lsize = current_cpu_data.icache.linesz;
189 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
190 else if (ic_lsize == 32) {
191 if (TX49XX_ICACHE_INDEX_INV_WAR)
192 r4k_blast_icache_page_indexed =
193 tx49_blast_icache32_page_indexed;
194 else if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
195 r4k_blast_icache_page_indexed =
196 blast_icache32_r4600_v1_page_indexed;
198 r4k_blast_icache_page_indexed =
199 blast_icache32_page_indexed;
200 } else if (ic_lsize == 64)
201 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
204 static void (* r4k_blast_icache)(void);
206 static inline void r4k_blast_icache_setup(void)
208 unsigned long ic_lsize = current_cpu_data.icache.linesz;
211 r4k_blast_icache = blast_icache16;
212 else if (ic_lsize == 32) {
213 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
214 r4k_blast_icache = blast_r4600_v1_icache32;
215 else if (TX49XX_ICACHE_INDEX_INV_WAR)
216 r4k_blast_icache = tx49_blast_icache32;
217 else if (ic_lsize == 32)
218 r4k_blast_icache = blast_icache32;
219 } else if (ic_lsize == 64)
220 r4k_blast_icache = blast_icache64;
223 static void (* r4k_blast_scache_page)(unsigned long addr);
225 static inline void r4k_blast_scache_page_setup(void)
227 unsigned long sc_lsize = current_cpu_data.scache.linesz;
230 r4k_blast_scache_page = blast_scache16_page;
231 else if (sc_lsize == 32)
232 r4k_blast_scache_page = blast_scache32_page;
233 else if (sc_lsize == 64)
234 r4k_blast_scache_page = blast_scache64_page;
235 else if (sc_lsize == 128)
236 r4k_blast_scache_page = blast_scache128_page;
239 static void (* r4k_blast_scache)(void);
241 static inline void r4k_blast_scache_setup(void)
243 unsigned long sc_lsize = current_cpu_data.scache.linesz;
246 r4k_blast_scache = blast_scache16;
247 else if (sc_lsize == 32)
248 r4k_blast_scache = blast_scache32;
249 else if (sc_lsize == 64)
250 r4k_blast_scache = blast_scache64;
251 else if (sc_lsize == 128)
252 r4k_blast_scache = blast_scache128;
255 static void r4k_flush_cache_all(void)
257 if (!cpu_has_dc_aliases)
264 static void r4k___flush_cache_all(void)
269 switch (current_cpu_data.cputype) {
280 static void r4k_flush_cache_range(struct mm_struct *mm,
281 unsigned long start, unsigned long end)
283 if (!cpu_has_dc_aliases)
286 if (cpu_context(smp_processor_id(), mm) != 0) {
292 static void r4k_flush_cache_mm(struct mm_struct *mm)
294 if (!cpu_has_dc_aliases)
297 if (!cpu_context(smp_processor_id(), mm))
304 * Kludge alert. For obscure reasons R4000SC and R4400SC go nuts if we
305 * only flush the primary caches but R10000 and R12000 behave sane ...
307 if (current_cpu_data.cputype == CPU_R4000SC ||
308 current_cpu_data.cputype == CPU_R4000MC ||
309 current_cpu_data.cputype == CPU_R4400SC ||
310 current_cpu_data.cputype == CPU_R4400MC)
314 static void r4k_flush_cache_page(struct vm_area_struct *vma,
317 int exec = vma->vm_flags & VM_EXEC;
318 struct mm_struct *mm = vma->vm_mm;
324 * If ownes no valid ASID yet, cannot possibly have gotten
325 * this page into the cache.
327 if (cpu_context(smp_processor_id(), mm) == 0)
331 pgdp = pgd_offset(mm, page);
332 pmdp = pmd_offset(pgdp, page);
333 ptep = pte_offset(pmdp, page);
336 * If the page isn't marked valid, the page cannot possibly be
339 if (!(pte_val(*ptep) & _PAGE_PRESENT))
343 * Doing flushes for another ASID than the current one is
344 * too difficult since stupid R4k caches do a TLB translation
345 * for every cache flush operation. So we do indexed flushes
346 * in that case, which doesn't overly flush the cache too much.
348 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
349 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
350 r4k_blast_dcache_page(page);
352 r4k_blast_icache_page(page);
358 * Do indexed flush, too much work to get the (possible) TLB refills
361 page = (KSEG0 + (page & (dcache_size - 1)));
362 if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
363 r4k_blast_dcache_page_indexed(page);
365 if (cpu_has_vtag_icache) {
366 int cpu = smp_processor_id();
368 if (cpu_context(cpu, vma->vm_mm) != 0)
369 drop_mmu_context(vma->vm_mm, cpu);
371 r4k_blast_icache_page_indexed(page);
375 static void r4k_flush_data_cache_page(unsigned long addr)
377 r4k_blast_dcache_page(addr);
380 static void r4k_flush_icache_range(unsigned long start, unsigned long end)
382 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
383 unsigned long ic_lsize = current_cpu_data.icache.linesz;
384 unsigned long addr, aend;
386 if (!cpu_has_ic_fills_f_dc) {
387 if (end - start > dcache_size)
390 addr = start & ~(dc_lsize - 1);
391 aend = (end - 1) & ~(dc_lsize - 1);
394 /* Hit_Writeback_Inv_D */
395 protected_writeback_dcache_line(addr);
403 if (end - start > icache_size)
406 addr = start & ~(ic_lsize - 1);
407 aend = (end - 1) & ~(ic_lsize - 1);
409 /* Hit_Invalidate_I */
410 protected_flush_icache_line(addr);
419 * Ok, this seriously sucks. We use them to flush a user page but don't
420 * know the virtual address, so we have to blast away the whole icache
421 * which is significantly more expensive than the real thing. Otoh we at
422 * least know the kernel address of the page so we can flush it
425 static void r4k_flush_icache_page(struct vm_area_struct *vma,
429 * If there's no context yet, or the page isn't executable, no icache
432 if (!(vma->vm_flags & VM_EXEC))
436 * Tricky ... Because we don't know the virtual address we've got the
437 * choice of either invalidating the entire primary and secondary
438 * caches or invalidating the secondary caches also. With the subset
439 * enforcment on R4000SC, R4400SC, R10000 and R12000 invalidating the
440 * secondary cache will result in any entries in the primary caches
441 * also getting invalidated which hopefully is a bit more economical.
443 if (cpu_has_subset_pcaches) {
444 unsigned long addr = (unsigned long) page_address(page);
446 r4k_blast_scache_page(addr);
447 ClearPageDcacheDirty(page);
452 if (!cpu_has_ic_fills_f_dc) {
453 unsigned long addr = (unsigned long) page_address(page);
454 r4k_blast_dcache_page(addr);
455 ClearPageDcacheDirty(page);
459 * We're not sure of the virtual address(es) involved here, so
460 * we have to flush the entire I-cache.
462 if (cpu_has_vtag_icache) {
463 int cpu = smp_processor_id();
465 if (cpu_context(cpu, vma->vm_mm) != 0)
466 drop_mmu_context(vma->vm_mm, cpu);
471 #ifdef CONFIG_NONCOHERENT_IO
473 static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
475 unsigned long end, a;
477 /* Catch bad driver code */
480 if (cpu_has_subset_pcaches) {
481 unsigned long sc_lsize = current_cpu_data.scache.linesz;
483 if (size >= scache_size) {
488 a = addr & ~(sc_lsize - 1);
489 end = (addr + size - 1) & ~(sc_lsize - 1);
491 flush_scache_line(a); /* Hit_Writeback_Inv_SD */
500 * Either no secondary cache or the available caches don't have the
501 * subset property so we have to flush the primary caches
504 if (size >= dcache_size) {
507 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
509 R4600_HIT_CACHEOP_WAR_IMPL;
510 a = addr & ~(dc_lsize - 1);
511 end = (addr + size - 1) & ~(dc_lsize - 1);
513 flush_dcache_line(a); /* Hit_Writeback_Inv_D */
520 bc_wback_inv(addr, size);
523 static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
525 unsigned long end, a;
527 /* Catch bad driver code */
530 if (cpu_has_subset_pcaches) {
531 unsigned long sc_lsize = current_cpu_data.scache.linesz;
533 if (size >= scache_size) {
538 a = addr & ~(sc_lsize - 1);
539 end = (addr + size - 1) & ~(sc_lsize - 1);
541 flush_scache_line(a); /* Hit_Writeback_Inv_SD */
549 if (size >= dcache_size) {
552 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
554 R4600_HIT_CACHEOP_WAR_IMPL;
555 a = addr & ~(dc_lsize - 1);
556 end = (addr + size - 1) & ~(dc_lsize - 1);
558 flush_dcache_line(a); /* Hit_Writeback_Inv_D */
567 #endif /* CONFIG_NONCOHERENT_IO */
570 * While we're protected against bad userland addresses we don't care
571 * very much about what happens in that case. Usually a segmentation
572 * fault will dump the process later on anyway ...
574 static void r4k_flush_cache_sigtramp(unsigned long addr)
576 unsigned long ic_lsize = current_cpu_data.icache.linesz;
577 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
579 R4600_HIT_CACHEOP_WAR_IMPL;
580 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
581 protected_flush_icache_line(addr & ~(ic_lsize - 1));
582 if (MIPS4K_ICACHE_REFILL_WAR) {
583 __asm__ __volatile__ (
598 : "i" (Hit_Invalidate_I));
600 if (MIPS_CACHE_SYNC_WAR)
601 __asm__ __volatile__ ("sync");
604 static void r4k_flush_icache_all(void)
606 if (cpu_has_vtag_icache)
610 static inline void rm7k_erratum31(void)
612 const unsigned long ic_lsize = 32;
615 /* RM7000 erratum #31. The icache is screwed at startup. */
619 for (addr = KSEG0; addr <= KSEG0 + 4096; addr += ic_lsize) {
620 __asm__ __volatile__ (
623 "cache\t%1, 0(%0)\n\t"
624 "cache\t%1, 0x1000(%0)\n\t"
625 "cache\t%1, 0x2000(%0)\n\t"
626 "cache\t%1, 0x3000(%0)\n\t"
627 "cache\t%2, 0(%0)\n\t"
628 "cache\t%2, 0x1000(%0)\n\t"
629 "cache\t%2, 0x2000(%0)\n\t"
630 "cache\t%2, 0x3000(%0)\n\t"
631 "cache\t%1, 0(%0)\n\t"
632 "cache\t%1, 0x1000(%0)\n\t"
633 "cache\t%1, 0x2000(%0)\n\t"
634 "cache\t%1, 0x3000(%0)\n\t"
638 : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
642 static char *way_string[] = { NULL, "direct mapped", "2-way", "3-way", "4-way",
643 "5-way", "6-way", "7-way", "8-way"
646 static void __init probe_pcache(void)
648 struct cpuinfo_mips *c = ¤t_cpu_data;
649 unsigned int config = read_c0_config();
650 unsigned int prid = read_c0_prid();
651 unsigned long config1;
654 switch (c->cputype) {
655 case CPU_R4600: /* QED style two way caches? */
659 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
660 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
662 c->icache.waybit = ffs(icache_size/2) - 1;
664 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
665 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
667 c->dcache.waybit= ffs(dcache_size/2) - 1;
669 c->options |= MIPS_CPU_CACHE_CDEX_P;
674 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
675 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
679 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
680 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
682 c->dcache.waybit = 0;
684 c->options |= MIPS_CPU_CACHE_CDEX_P;
688 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
689 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
693 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
694 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
696 c->dcache.waybit = 0;
698 c->options |= MIPS_CPU_CACHE_CDEX_P;
708 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
709 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
711 c->icache.waybit = 0; /* doesn't matter */
713 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
714 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
716 c->dcache.waybit = 0; /* does not matter */
718 c->options |= MIPS_CPU_CACHE_CDEX_P;
723 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
724 c->icache.linesz = 64;
726 c->icache.waybit = 0;
728 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
729 c->dcache.linesz = 32;
731 c->dcache.waybit = 0;
733 c->options |= MIPS_CPU_PREFETCH;
737 write_c0_config(config & ~CONF_EB);
739 /* Workaround for cache instruction bug of VR4131 */
740 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
741 c->processor_id == 0x0c82U) {
742 config &= ~0x00000030U;
743 config |= 0x00410000U;
744 write_c0_config(config);
746 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
747 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
749 c->icache.waybit = ffs(icache_size/2) - 1;
751 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
752 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
754 c->dcache.waybit = ffs(dcache_size/2) - 1;
756 c->options |= MIPS_CPU_CACHE_CDEX_P;
765 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
766 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
768 c->icache.waybit = 0; /* doesn't matter */
770 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
771 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
773 c->dcache.waybit = 0; /* does not matter */
775 c->options |= MIPS_CPU_CACHE_CDEX_P;
782 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
783 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
785 c->icache.waybit = ffs(icache_size / c->icache.ways) - 1;
787 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
788 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
790 c->dcache.waybit = ffs(dcache_size / c->dcache.ways) - 1;
792 #if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
793 c->options |= MIPS_CPU_CACHE_CDEX_P;
795 c->options |= MIPS_CPU_PREFETCH;
799 if (!(config & MIPS_CONF_M))
800 panic("Don't know how to probe P-caches on this cpu.");
803 * So we seem to be a MIPS32 or MIPS64 CPU
804 * So let's probe the I-cache ...
806 config1 = read_c0_config1();
808 if ((lsize = ((config1 >> 19) & 7)))
809 c->icache.linesz = 2 << lsize;
811 c->icache.linesz = lsize;
812 c->icache.sets = 64 << ((config1 >> 22) & 7);
813 c->icache.ways = 1 + ((config1 >> 16) & 7);
815 icache_size = c->icache.sets *
818 c->icache.waybit = ffs(icache_size/c->icache.ways) - 1;
820 if (config & 0x8) /* VI bit */
821 c->icache.flags |= MIPS_CACHE_VTAG;
824 * Now probe the MIPS32 / MIPS64 data cache.
828 if ((lsize = ((config1 >> 10) & 7)))
829 c->dcache.linesz = 2 << lsize;
831 c->dcache.linesz= lsize;
832 c->dcache.sets = 64 << ((config1 >> 13) & 7);
833 c->dcache.ways = 1 + ((config1 >> 7) & 7);
835 dcache_size = c->dcache.sets *
838 c->dcache.waybit = ffs(dcache_size/c->dcache.ways) - 1;
840 c->options |= MIPS_CPU_PREFETCH;
845 * Processor configuration sanity check for the R4000SC erratum
846 * #5. With page sizes larger than 32kB there is no possibility
847 * to get a VCE exception anymore so we don't care about this
848 * misconfiguration. The case is rather theoretical anyway;
849 * presumably no vendor is shipping his hardware in the "bad"
852 if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
853 !(config & CONF_SC) && c->icache.linesz != 16 &&
855 panic("Improper R4000SC processor configuration detected");
857 /* compute a couple of other cache variables */
858 c->icache.waysize = icache_size / c->icache.ways;
859 c->dcache.waysize = dcache_size / c->dcache.ways;
861 c->icache.sets = icache_size / (c->icache.linesz * c->icache.ways);
862 c->dcache.sets = dcache_size / (c->dcache.linesz * c->dcache.ways);
865 * R10000 and R12000 P-caches are odd in a positive way. They're 32kB
866 * 2-way virtually indexed so normally would suffer from aliases. So
867 * normally they'd suffer from aliases but magic in the hardware deals
868 * with that for us so we don't need to take care ourselves.
870 if (c->cputype != CPU_R10000 && c->cputype != CPU_R12000)
871 if (c->dcache.waysize > PAGE_SIZE)
872 c->dcache.flags |= MIPS_CACHE_ALIASES;
874 switch (c->cputype) {
877 * Some older 20Kc chips doesn't have the 'VI' bit in
878 * the config register.
880 c->icache.flags |= MIPS_CACHE_VTAG;
884 c->icache.flags |= MIPS_CACHE_IC_F_DC;
888 printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
890 cpu_has_vtag_icache ? "virtually tagged" : "physically tagged",
891 way_string[c->icache.ways], c->icache.linesz);
893 printk("Primary data cache %ldkB, %s, linesize %d bytes.\n",
894 dcache_size >> 10, way_string[c->dcache.ways], c->dcache.linesz);
898 * If you even _breathe_ on this function, look at the gcc output and make sure
899 * it does not pop things on and off the stack for the cache sizing loop that
900 * executes in KSEG1 space or else you will crash and burn badly. You have
903 static int __init probe_scache(void)
905 extern unsigned long stext;
906 unsigned long flags, addr, begin, end, pow2;
907 unsigned int config = read_c0_config();
908 struct cpuinfo_mips *c = ¤t_cpu_data;
911 if (config & CONF_SC)
914 begin = (unsigned long) &stext;
915 begin &= ~((4 * 1024 * 1024) - 1);
916 end = begin + (4 * 1024 * 1024);
919 * This is such a bitch, you'd think they would make it easy to do
920 * this. Away you daemons of stupidity!
922 local_irq_save(flags);
924 /* Fill each size-multiple cache line with a valid tag. */
926 for (addr = begin; addr < end; addr = (begin + pow2)) {
927 unsigned long *p = (unsigned long *) addr;
928 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
932 /* Load first line with zero (therefore invalid) tag. */
935 __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
936 cache_op(Index_Store_Tag_I, begin);
937 cache_op(Index_Store_Tag_D, begin);
938 cache_op(Index_Store_Tag_SD, begin);
940 /* Now search for the wrap around point. */
943 for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
944 cache_op(Index_Load_Tag_SD, addr);
945 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
946 if (!read_c0_taglo())
950 local_irq_restore(flags);
954 c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
956 c->dcache.waybit = 0; /* does not matter */
961 typedef int (*probe_func_t)(unsigned long);
962 extern int r5k_sc_init(void);
963 extern int rm7k_sc_init(void);
965 static void __init setup_scache(void)
967 struct cpuinfo_mips *c = ¤t_cpu_data;
968 unsigned int config = read_c0_config();
969 probe_func_t probe_scache_kseg1;
973 * Do the probing thing on R4000SC and R4400SC processors. Other
974 * processors don't have a S-cache that would be relevant to the
975 * Linux memory managment.
977 switch (c->cputype) {
982 probe_scache_kseg1 = (probe_func_t) (KSEG1ADDR(&probe_scache));
983 sc_present = probe_scache_kseg1(config);
985 c->options |= MIPS_CPU_CACHE_CDEX_S;
990 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
991 c->scache.linesz = 64 << ((config >> 13) & 1);
999 #ifdef CONFIG_R5000_CPU_SCACHE
1006 #ifdef CONFIG_RM7000_CPU_SCACHE
1018 if ((c->isa_level == MIPS_CPU_ISA_M32 ||
1019 c->isa_level == MIPS_CPU_ISA_M64) &&
1020 !(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1021 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1023 /* compute a couple of other cache variables */
1024 c->scache.waysize = scache_size / c->scache.ways;
1026 c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1028 printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1029 scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1031 c->options |= MIPS_CPU_SUBSET_CACHES;
1034 static inline void coherency_setup(void)
1036 change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
1039 * c0_status.cu=0 specifies that updates by the sc instruction use
1040 * the coherency mode specified by the TLB; 1 means cachable
1041 * coherent update on write will be used. Not all processors have
1042 * this bit and; some wire it to zero, others like Toshiba had the
1043 * silly idea of putting something else there ...
1045 switch (current_cpu_data.cputype) {
1052 clear_c0_config(CONF_CU);
1058 void __init ld_mmu_r4xx0(void)
1060 extern void build_clear_page(void);
1061 extern void build_copy_page(void);
1062 extern char except_vec2_generic;
1063 struct cpuinfo_mips *c = ¤t_cpu_data;
1065 /* Default cache error handler for R4000 and R5000 family */
1066 memcpy((void *)(KSEG0 + 0x100), &except_vec2_generic, 0x80);
1067 memcpy((void *)(KSEG1 + 0x100), &except_vec2_generic, 0x80);
1072 if (c->dcache.sets * c->dcache.ways > PAGE_SIZE)
1073 c->dcache.flags |= MIPS_CACHE_ALIASES;
1075 r4k_blast_dcache_page_setup();
1076 r4k_blast_dcache_page_indexed_setup();
1077 r4k_blast_dcache_setup();
1078 r4k_blast_icache_page_setup();
1079 r4k_blast_icache_page_indexed_setup();
1080 r4k_blast_icache_setup();
1081 r4k_blast_scache_page_setup();
1082 r4k_blast_scache_setup();
1085 * Some MIPS32 and MIPS64 processors have physically indexed caches.
1086 * This code supports virtually indexed processors and will be
1087 * unnecessarily inefficient on physically indexed processors.
1089 shm_align_mask = max_t( unsigned long,
1090 c->dcache.sets * c->dcache.linesz - 1,
1093 _flush_cache_all = r4k_flush_cache_all;
1094 ___flush_cache_all = r4k___flush_cache_all;
1095 _flush_cache_mm = r4k_flush_cache_mm;
1096 _flush_cache_page = r4k_flush_cache_page;
1097 _flush_icache_page = r4k_flush_icache_page;
1098 _flush_cache_range = r4k_flush_cache_range;
1100 _flush_cache_sigtramp = r4k_flush_cache_sigtramp;
1101 _flush_icache_all = r4k_flush_icache_all;
1102 _flush_data_cache_page = r4k_flush_data_cache_page;
1103 _flush_icache_range = r4k_flush_icache_range;
1105 #ifdef CONFIG_NONCOHERENT_IO
1106 _dma_cache_wback_inv = r4k_dma_cache_wback_inv;
1107 _dma_cache_wback = r4k_dma_cache_wback_inv;
1108 _dma_cache_inv = r4k_dma_cache_inv;
1111 __flush_cache_all();