3d3e53651341000f3f4b0eb6c5da1576c708254c
[safe/jmp/linux-2.6] / arch / mips / mm / c-r4k.c
1 /*
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
4  * for more details.
5  *
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.
9  */
10 #include <linux/hardirq.h>
11 #include <linux/init.h>
12 #include <linux/highmem.h>
13 #include <linux/kernel.h>
14 #include <linux/linkage.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/module.h>
18 #include <linux/bitops.h>
19
20 #include <asm/bcache.h>
21 #include <asm/bootinfo.h>
22 #include <asm/cache.h>
23 #include <asm/cacheops.h>
24 #include <asm/cpu.h>
25 #include <asm/cpu-features.h>
26 #include <asm/io.h>
27 #include <asm/page.h>
28 #include <asm/pgtable.h>
29 #include <asm/r4kcache.h>
30 #include <asm/sections.h>
31 #include <asm/system.h>
32 #include <asm/mmu_context.h>
33 #include <asm/war.h>
34 #include <asm/cacheflush.h> /* for run_uncached() */
35
36
37 /*
38  * Special Variant of smp_call_function for use by cache functions:
39  *
40  *  o No return value
41  *  o collapses to normal function call on UP kernels
42  *  o collapses to normal function call on systems with a single shared
43  *    primary cache.
44  */
45 static inline void r4k_on_each_cpu(void (*func) (void *info), void *info,
46                                    int retry, int wait)
47 {
48         preempt_disable();
49
50 #if !defined(CONFIG_MIPS_MT_SMP) && !defined(CONFIG_MIPS_MT_SMTC)
51         smp_call_function(func, info, retry, wait);
52 #endif
53         func(info);
54         preempt_enable();
55 }
56
57 /*
58  * Must die.
59  */
60 static unsigned long icache_size __read_mostly;
61 static unsigned long dcache_size __read_mostly;
62 static unsigned long scache_size __read_mostly;
63
64 /*
65  * Dummy cache handling routines for machines without boardcaches
66  */
67 static void cache_noop(void) {}
68
69 static struct bcache_ops no_sc_ops = {
70         .bc_enable = (void *)cache_noop,
71         .bc_disable = (void *)cache_noop,
72         .bc_wback_inv = (void *)cache_noop,
73         .bc_inv = (void *)cache_noop
74 };
75
76 struct bcache_ops *bcops = &no_sc_ops;
77
78 #define cpu_is_r4600_v1_x()     ((read_c0_prid() & 0xfffffff0) == 0x00002010)
79 #define cpu_is_r4600_v2_x()     ((read_c0_prid() & 0xfffffff0) == 0x00002020)
80
81 #define R4600_HIT_CACHEOP_WAR_IMPL                                      \
82 do {                                                                    \
83         if (R4600_V2_HIT_CACHEOP_WAR && cpu_is_r4600_v2_x())            \
84                 *(volatile unsigned long *)CKSEG1;                      \
85         if (R4600_V1_HIT_CACHEOP_WAR)                                   \
86                 __asm__ __volatile__("nop;nop;nop;nop");                \
87 } while (0)
88
89 static void (*r4k_blast_dcache_page)(unsigned long addr);
90
91 static inline void r4k_blast_dcache_page_dc32(unsigned long addr)
92 {
93         R4600_HIT_CACHEOP_WAR_IMPL;
94         blast_dcache32_page(addr);
95 }
96
97 static void __cpuinit r4k_blast_dcache_page_setup(void)
98 {
99         unsigned long  dc_lsize = cpu_dcache_line_size();
100
101         if (dc_lsize == 0)
102                 r4k_blast_dcache_page = (void *)cache_noop;
103         else if (dc_lsize == 16)
104                 r4k_blast_dcache_page = blast_dcache16_page;
105         else if (dc_lsize == 32)
106                 r4k_blast_dcache_page = r4k_blast_dcache_page_dc32;
107 }
108
109 static void (* r4k_blast_dcache_page_indexed)(unsigned long addr);
110
111 static void __cpuinit r4k_blast_dcache_page_indexed_setup(void)
112 {
113         unsigned long dc_lsize = cpu_dcache_line_size();
114
115         if (dc_lsize == 0)
116                 r4k_blast_dcache_page_indexed = (void *)cache_noop;
117         else if (dc_lsize == 16)
118                 r4k_blast_dcache_page_indexed = blast_dcache16_page_indexed;
119         else if (dc_lsize == 32)
120                 r4k_blast_dcache_page_indexed = blast_dcache32_page_indexed;
121 }
122
123 static void (* r4k_blast_dcache)(void);
124
125 static void __cpuinit r4k_blast_dcache_setup(void)
126 {
127         unsigned long dc_lsize = cpu_dcache_line_size();
128
129         if (dc_lsize == 0)
130                 r4k_blast_dcache = (void *)cache_noop;
131         else if (dc_lsize == 16)
132                 r4k_blast_dcache = blast_dcache16;
133         else if (dc_lsize == 32)
134                 r4k_blast_dcache = blast_dcache32;
135 }
136
137 /* force code alignment (used for TX49XX_ICACHE_INDEX_INV_WAR) */
138 #define JUMP_TO_ALIGN(order) \
139         __asm__ __volatile__( \
140                 "b\t1f\n\t" \
141                 ".align\t" #order "\n\t" \
142                 "1:\n\t" \
143                 )
144 #define CACHE32_UNROLL32_ALIGN  JUMP_TO_ALIGN(10) /* 32 * 32 = 1024 */
145 #define CACHE32_UNROLL32_ALIGN2 JUMP_TO_ALIGN(11)
146
147 static inline void blast_r4600_v1_icache32(void)
148 {
149         unsigned long flags;
150
151         local_irq_save(flags);
152         blast_icache32();
153         local_irq_restore(flags);
154 }
155
156 static inline void tx49_blast_icache32(void)
157 {
158         unsigned long start = INDEX_BASE;
159         unsigned long end = start + current_cpu_data.icache.waysize;
160         unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
161         unsigned long ws_end = current_cpu_data.icache.ways <<
162                                current_cpu_data.icache.waybit;
163         unsigned long ws, addr;
164
165         CACHE32_UNROLL32_ALIGN2;
166         /* I'm in even chunk.  blast odd chunks */
167         for (ws = 0; ws < ws_end; ws += ws_inc)
168                 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
169                         cache32_unroll32(addr|ws, Index_Invalidate_I);
170         CACHE32_UNROLL32_ALIGN;
171         /* I'm in odd chunk.  blast even chunks */
172         for (ws = 0; ws < ws_end; ws += ws_inc)
173                 for (addr = start; addr < end; addr += 0x400 * 2)
174                         cache32_unroll32(addr|ws, Index_Invalidate_I);
175 }
176
177 static inline void blast_icache32_r4600_v1_page_indexed(unsigned long page)
178 {
179         unsigned long flags;
180
181         local_irq_save(flags);
182         blast_icache32_page_indexed(page);
183         local_irq_restore(flags);
184 }
185
186 static inline void tx49_blast_icache32_page_indexed(unsigned long page)
187 {
188         unsigned long indexmask = current_cpu_data.icache.waysize - 1;
189         unsigned long start = INDEX_BASE + (page & indexmask);
190         unsigned long end = start + PAGE_SIZE;
191         unsigned long ws_inc = 1UL << current_cpu_data.icache.waybit;
192         unsigned long ws_end = current_cpu_data.icache.ways <<
193                                current_cpu_data.icache.waybit;
194         unsigned long ws, addr;
195
196         CACHE32_UNROLL32_ALIGN2;
197         /* I'm in even chunk.  blast odd chunks */
198         for (ws = 0; ws < ws_end; ws += ws_inc)
199                 for (addr = start + 0x400; addr < end; addr += 0x400 * 2)
200                         cache32_unroll32(addr|ws, Index_Invalidate_I);
201         CACHE32_UNROLL32_ALIGN;
202         /* I'm in odd chunk.  blast even chunks */
203         for (ws = 0; ws < ws_end; ws += ws_inc)
204                 for (addr = start; addr < end; addr += 0x400 * 2)
205                         cache32_unroll32(addr|ws, Index_Invalidate_I);
206 }
207
208 static void (* r4k_blast_icache_page)(unsigned long addr);
209
210 static void __cpuinit r4k_blast_icache_page_setup(void)
211 {
212         unsigned long ic_lsize = cpu_icache_line_size();
213
214         if (ic_lsize == 0)
215                 r4k_blast_icache_page = (void *)cache_noop;
216         else if (ic_lsize == 16)
217                 r4k_blast_icache_page = blast_icache16_page;
218         else if (ic_lsize == 32)
219                 r4k_blast_icache_page = blast_icache32_page;
220         else if (ic_lsize == 64)
221                 r4k_blast_icache_page = blast_icache64_page;
222 }
223
224
225 static void (* r4k_blast_icache_page_indexed)(unsigned long addr);
226
227 static void __cpuinit r4k_blast_icache_page_indexed_setup(void)
228 {
229         unsigned long ic_lsize = cpu_icache_line_size();
230
231         if (ic_lsize == 0)
232                 r4k_blast_icache_page_indexed = (void *)cache_noop;
233         else if (ic_lsize == 16)
234                 r4k_blast_icache_page_indexed = blast_icache16_page_indexed;
235         else if (ic_lsize == 32) {
236                 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
237                         r4k_blast_icache_page_indexed =
238                                 blast_icache32_r4600_v1_page_indexed;
239                 else if (TX49XX_ICACHE_INDEX_INV_WAR)
240                         r4k_blast_icache_page_indexed =
241                                 tx49_blast_icache32_page_indexed;
242                 else
243                         r4k_blast_icache_page_indexed =
244                                 blast_icache32_page_indexed;
245         } else if (ic_lsize == 64)
246                 r4k_blast_icache_page_indexed = blast_icache64_page_indexed;
247 }
248
249 static void (* r4k_blast_icache)(void);
250
251 static void __cpuinit r4k_blast_icache_setup(void)
252 {
253         unsigned long ic_lsize = cpu_icache_line_size();
254
255         if (ic_lsize == 0)
256                 r4k_blast_icache = (void *)cache_noop;
257         else if (ic_lsize == 16)
258                 r4k_blast_icache = blast_icache16;
259         else if (ic_lsize == 32) {
260                 if (R4600_V1_INDEX_ICACHEOP_WAR && cpu_is_r4600_v1_x())
261                         r4k_blast_icache = blast_r4600_v1_icache32;
262                 else if (TX49XX_ICACHE_INDEX_INV_WAR)
263                         r4k_blast_icache = tx49_blast_icache32;
264                 else
265                         r4k_blast_icache = blast_icache32;
266         } else if (ic_lsize == 64)
267                 r4k_blast_icache = blast_icache64;
268 }
269
270 static void (* r4k_blast_scache_page)(unsigned long addr);
271
272 static void __cpuinit r4k_blast_scache_page_setup(void)
273 {
274         unsigned long sc_lsize = cpu_scache_line_size();
275
276         if (scache_size == 0)
277                 r4k_blast_scache_page = (void *)cache_noop;
278         else if (sc_lsize == 16)
279                 r4k_blast_scache_page = blast_scache16_page;
280         else if (sc_lsize == 32)
281                 r4k_blast_scache_page = blast_scache32_page;
282         else if (sc_lsize == 64)
283                 r4k_blast_scache_page = blast_scache64_page;
284         else if (sc_lsize == 128)
285                 r4k_blast_scache_page = blast_scache128_page;
286 }
287
288 static void (* r4k_blast_scache_page_indexed)(unsigned long addr);
289
290 static void __cpuinit r4k_blast_scache_page_indexed_setup(void)
291 {
292         unsigned long sc_lsize = cpu_scache_line_size();
293
294         if (scache_size == 0)
295                 r4k_blast_scache_page_indexed = (void *)cache_noop;
296         else if (sc_lsize == 16)
297                 r4k_blast_scache_page_indexed = blast_scache16_page_indexed;
298         else if (sc_lsize == 32)
299                 r4k_blast_scache_page_indexed = blast_scache32_page_indexed;
300         else if (sc_lsize == 64)
301                 r4k_blast_scache_page_indexed = blast_scache64_page_indexed;
302         else if (sc_lsize == 128)
303                 r4k_blast_scache_page_indexed = blast_scache128_page_indexed;
304 }
305
306 static void (* r4k_blast_scache)(void);
307
308 static void __cpuinit r4k_blast_scache_setup(void)
309 {
310         unsigned long sc_lsize = cpu_scache_line_size();
311
312         if (scache_size == 0)
313                 r4k_blast_scache = (void *)cache_noop;
314         else if (sc_lsize == 16)
315                 r4k_blast_scache = blast_scache16;
316         else if (sc_lsize == 32)
317                 r4k_blast_scache = blast_scache32;
318         else if (sc_lsize == 64)
319                 r4k_blast_scache = blast_scache64;
320         else if (sc_lsize == 128)
321                 r4k_blast_scache = blast_scache128;
322 }
323
324 static inline void local_r4k___flush_cache_all(void * args)
325 {
326 #if defined(CONFIG_CPU_LOONGSON2)
327         r4k_blast_scache();
328         return;
329 #endif
330         r4k_blast_dcache();
331         r4k_blast_icache();
332
333         switch (current_cpu_type()) {
334         case CPU_R4000SC:
335         case CPU_R4000MC:
336         case CPU_R4400SC:
337         case CPU_R4400MC:
338         case CPU_R10000:
339         case CPU_R12000:
340         case CPU_R14000:
341                 r4k_blast_scache();
342         }
343 }
344
345 static void r4k___flush_cache_all(void)
346 {
347         r4k_on_each_cpu(local_r4k___flush_cache_all, NULL, 1, 1);
348 }
349
350 static inline int has_valid_asid(const struct mm_struct *mm)
351 {
352 #if defined(CONFIG_MIPS_MT_SMP) || defined(CONFIG_MIPS_MT_SMTC)
353         int i;
354
355         for_each_online_cpu(i)
356                 if (cpu_context(i, mm))
357                         return 1;
358
359         return 0;
360 #else
361         return cpu_context(smp_processor_id(), mm);
362 #endif
363 }
364
365 static void r4k__flush_cache_vmap(void)
366 {
367         r4k_blast_dcache();
368 }
369
370 static void r4k__flush_cache_vunmap(void)
371 {
372         r4k_blast_dcache();
373 }
374
375 static inline void local_r4k_flush_cache_range(void * args)
376 {
377         struct vm_area_struct *vma = args;
378         int exec = vma->vm_flags & VM_EXEC;
379
380         if (!(has_valid_asid(vma->vm_mm)))
381                 return;
382
383         r4k_blast_dcache();
384         if (exec)
385                 r4k_blast_icache();
386 }
387
388 static void r4k_flush_cache_range(struct vm_area_struct *vma,
389         unsigned long start, unsigned long end)
390 {
391         int exec = vma->vm_flags & VM_EXEC;
392
393         if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc))
394                 r4k_on_each_cpu(local_r4k_flush_cache_range, vma, 1, 1);
395 }
396
397 static inline void local_r4k_flush_cache_mm(void * args)
398 {
399         struct mm_struct *mm = args;
400
401         if (!has_valid_asid(mm))
402                 return;
403
404         /*
405          * Kludge alert.  For obscure reasons R4000SC and R4400SC go nuts if we
406          * only flush the primary caches but R10000 and R12000 behave sane ...
407          * R4000SC and R4400SC indexed S-cache ops also invalidate primary
408          * caches, so we can bail out early.
409          */
410         if (current_cpu_type() == CPU_R4000SC ||
411             current_cpu_type() == CPU_R4000MC ||
412             current_cpu_type() == CPU_R4400SC ||
413             current_cpu_type() == CPU_R4400MC) {
414                 r4k_blast_scache();
415                 return;
416         }
417
418         r4k_blast_dcache();
419 }
420
421 static void r4k_flush_cache_mm(struct mm_struct *mm)
422 {
423         if (!cpu_has_dc_aliases)
424                 return;
425
426         r4k_on_each_cpu(local_r4k_flush_cache_mm, mm, 1, 1);
427 }
428
429 struct flush_cache_page_args {
430         struct vm_area_struct *vma;
431         unsigned long addr;
432         unsigned long pfn;
433 };
434
435 static inline void local_r4k_flush_cache_page(void *args)
436 {
437         struct flush_cache_page_args *fcp_args = args;
438         struct vm_area_struct *vma = fcp_args->vma;
439         unsigned long addr = fcp_args->addr;
440         struct page *page = pfn_to_page(fcp_args->pfn);
441         int exec = vma->vm_flags & VM_EXEC;
442         struct mm_struct *mm = vma->vm_mm;
443         pgd_t *pgdp;
444         pud_t *pudp;
445         pmd_t *pmdp;
446         pte_t *ptep;
447         void *vaddr;
448
449         /*
450          * If ownes no valid ASID yet, cannot possibly have gotten
451          * this page into the cache.
452          */
453         if (!has_valid_asid(mm))
454                 return;
455
456         addr &= PAGE_MASK;
457         pgdp = pgd_offset(mm, addr);
458         pudp = pud_offset(pgdp, addr);
459         pmdp = pmd_offset(pudp, addr);
460         ptep = pte_offset(pmdp, addr);
461
462         /*
463          * If the page isn't marked valid, the page cannot possibly be
464          * in the cache.
465          */
466         if (!(pte_present(*ptep)))
467                 return;
468
469         if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID))
470                 vaddr = NULL;
471         else {
472                 /*
473                  * Use kmap_coherent or kmap_atomic to do flushes for
474                  * another ASID than the current one.
475                  */
476                 if (cpu_has_dc_aliases)
477                         vaddr = kmap_coherent(page, addr);
478                 else
479                         vaddr = kmap_atomic(page, KM_USER0);
480                 addr = (unsigned long)vaddr;
481         }
482
483         if (cpu_has_dc_aliases || (exec && !cpu_has_ic_fills_f_dc)) {
484                 r4k_blast_dcache_page(addr);
485         }
486         if (exec) {
487                 if (vaddr && cpu_has_vtag_icache && mm == current->active_mm) {
488                         int cpu = smp_processor_id();
489
490                         if (cpu_context(cpu, mm) != 0)
491                                 drop_mmu_context(mm, cpu);
492                 } else
493                         r4k_blast_icache_page(addr);
494         }
495
496         if (vaddr) {
497                 if (cpu_has_dc_aliases)
498                         kunmap_coherent();
499                 else
500                         kunmap_atomic(vaddr, KM_USER0);
501         }
502 }
503
504 static void r4k_flush_cache_page(struct vm_area_struct *vma,
505         unsigned long addr, unsigned long pfn)
506 {
507         struct flush_cache_page_args args;
508
509         args.vma = vma;
510         args.addr = addr;
511         args.pfn = pfn;
512
513         r4k_on_each_cpu(local_r4k_flush_cache_page, &args, 1, 1);
514 }
515
516 static inline void local_r4k_flush_data_cache_page(void * addr)
517 {
518         r4k_blast_dcache_page((unsigned long) addr);
519 }
520
521 static void r4k_flush_data_cache_page(unsigned long addr)
522 {
523         if (in_atomic())
524                 local_r4k_flush_data_cache_page((void *)addr);
525         else
526                 r4k_on_each_cpu(local_r4k_flush_data_cache_page, (void *) addr,
527                                 1, 1);
528 }
529
530 struct flush_icache_range_args {
531         unsigned long start;
532         unsigned long end;
533 };
534
535 static inline void local_r4k_flush_icache_range(void *args)
536 {
537         struct flush_icache_range_args *fir_args = args;
538         unsigned long start = fir_args->start;
539         unsigned long end = fir_args->end;
540
541         if (!cpu_has_ic_fills_f_dc) {
542                 if (end - start >= dcache_size) {
543                         r4k_blast_dcache();
544                 } else {
545                         R4600_HIT_CACHEOP_WAR_IMPL;
546                         protected_blast_dcache_range(start, end);
547                 }
548         }
549
550         if (end - start > icache_size)
551                 r4k_blast_icache();
552         else
553                 protected_blast_icache_range(start, end);
554 }
555
556 static void r4k_flush_icache_range(unsigned long start, unsigned long end)
557 {
558         struct flush_icache_range_args args;
559
560         args.start = start;
561         args.end = end;
562
563         r4k_on_each_cpu(local_r4k_flush_icache_range, &args, 1, 1);
564         instruction_hazard();
565 }
566
567 #ifdef CONFIG_DMA_NONCOHERENT
568
569 static void r4k_dma_cache_wback_inv(unsigned long addr, unsigned long size)
570 {
571         /* Catch bad driver code */
572         BUG_ON(size == 0);
573
574         if (cpu_has_inclusive_pcaches) {
575                 if (size >= scache_size)
576                         r4k_blast_scache();
577                 else
578                         blast_scache_range(addr, addr + size);
579                 return;
580         }
581
582         /*
583          * Either no secondary cache or the available caches don't have the
584          * subset property so we have to flush the primary caches
585          * explicitly
586          */
587         if (size >= dcache_size) {
588                 r4k_blast_dcache();
589         } else {
590                 R4600_HIT_CACHEOP_WAR_IMPL;
591                 blast_dcache_range(addr, addr + size);
592         }
593
594         bc_wback_inv(addr, size);
595 }
596
597 static void r4k_dma_cache_inv(unsigned long addr, unsigned long size)
598 {
599         /* Catch bad driver code */
600         BUG_ON(size == 0);
601
602         if (cpu_has_inclusive_pcaches) {
603                 if (size >= scache_size)
604                         r4k_blast_scache();
605                 else
606                         blast_inv_scache_range(addr, addr + size);
607                 return;
608         }
609
610         if (size >= dcache_size) {
611                 r4k_blast_dcache();
612         } else {
613                 R4600_HIT_CACHEOP_WAR_IMPL;
614                 blast_inv_dcache_range(addr, addr + size);
615         }
616
617         bc_inv(addr, size);
618 }
619 #endif /* CONFIG_DMA_NONCOHERENT */
620
621 /*
622  * While we're protected against bad userland addresses we don't care
623  * very much about what happens in that case.  Usually a segmentation
624  * fault will dump the process later on anyway ...
625  */
626 static void local_r4k_flush_cache_sigtramp(void * arg)
627 {
628         unsigned long ic_lsize = cpu_icache_line_size();
629         unsigned long dc_lsize = cpu_dcache_line_size();
630         unsigned long sc_lsize = cpu_scache_line_size();
631         unsigned long addr = (unsigned long) arg;
632
633         R4600_HIT_CACHEOP_WAR_IMPL;
634         if (dc_lsize)
635                 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
636         if (!cpu_icache_snoops_remote_store && scache_size)
637                 protected_writeback_scache_line(addr & ~(sc_lsize - 1));
638         if (ic_lsize)
639                 protected_flush_icache_line(addr & ~(ic_lsize - 1));
640         if (MIPS4K_ICACHE_REFILL_WAR) {
641                 __asm__ __volatile__ (
642                         ".set push\n\t"
643                         ".set noat\n\t"
644                         ".set mips3\n\t"
645 #ifdef CONFIG_32BIT
646                         "la     $at,1f\n\t"
647 #endif
648 #ifdef CONFIG_64BIT
649                         "dla    $at,1f\n\t"
650 #endif
651                         "cache  %0,($at)\n\t"
652                         "nop; nop; nop\n"
653                         "1:\n\t"
654                         ".set pop"
655                         :
656                         : "i" (Hit_Invalidate_I));
657         }
658         if (MIPS_CACHE_SYNC_WAR)
659                 __asm__ __volatile__ ("sync");
660 }
661
662 static void r4k_flush_cache_sigtramp(unsigned long addr)
663 {
664         r4k_on_each_cpu(local_r4k_flush_cache_sigtramp, (void *) addr, 1, 1);
665 }
666
667 static void r4k_flush_icache_all(void)
668 {
669         if (cpu_has_vtag_icache)
670                 r4k_blast_icache();
671 }
672
673 static inline void rm7k_erratum31(void)
674 {
675         const unsigned long ic_lsize = 32;
676         unsigned long addr;
677
678         /* RM7000 erratum #31. The icache is screwed at startup. */
679         write_c0_taglo(0);
680         write_c0_taghi(0);
681
682         for (addr = INDEX_BASE; addr <= INDEX_BASE + 4096; addr += ic_lsize) {
683                 __asm__ __volatile__ (
684                         ".set push\n\t"
685                         ".set noreorder\n\t"
686                         ".set mips3\n\t"
687                         "cache\t%1, 0(%0)\n\t"
688                         "cache\t%1, 0x1000(%0)\n\t"
689                         "cache\t%1, 0x2000(%0)\n\t"
690                         "cache\t%1, 0x3000(%0)\n\t"
691                         "cache\t%2, 0(%0)\n\t"
692                         "cache\t%2, 0x1000(%0)\n\t"
693                         "cache\t%2, 0x2000(%0)\n\t"
694                         "cache\t%2, 0x3000(%0)\n\t"
695                         "cache\t%1, 0(%0)\n\t"
696                         "cache\t%1, 0x1000(%0)\n\t"
697                         "cache\t%1, 0x2000(%0)\n\t"
698                         "cache\t%1, 0x3000(%0)\n\t"
699                         ".set pop\n"
700                         :
701                         : "r" (addr), "i" (Index_Store_Tag_I), "i" (Fill));
702         }
703 }
704
705 static char *way_string[] __cpuinitdata = { NULL, "direct mapped", "2-way",
706         "3-way", "4-way", "5-way", "6-way", "7-way", "8-way"
707 };
708
709 static void __cpuinit probe_pcache(void)
710 {
711         struct cpuinfo_mips *c = &current_cpu_data;
712         unsigned int config = read_c0_config();
713         unsigned int prid = read_c0_prid();
714         unsigned long config1;
715         unsigned int lsize;
716
717         switch (c->cputype) {
718         case CPU_R4600:                 /* QED style two way caches? */
719         case CPU_R4700:
720         case CPU_R5000:
721         case CPU_NEVADA:
722                 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
723                 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
724                 c->icache.ways = 2;
725                 c->icache.waybit = __ffs(icache_size/2);
726
727                 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
728                 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
729                 c->dcache.ways = 2;
730                 c->dcache.waybit= __ffs(dcache_size/2);
731
732                 c->options |= MIPS_CPU_CACHE_CDEX_P;
733                 break;
734
735         case CPU_R5432:
736         case CPU_R5500:
737                 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
738                 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
739                 c->icache.ways = 2;
740                 c->icache.waybit= 0;
741
742                 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
743                 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
744                 c->dcache.ways = 2;
745                 c->dcache.waybit = 0;
746
747                 c->options |= MIPS_CPU_CACHE_CDEX_P;
748                 break;
749
750         case CPU_TX49XX:
751                 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
752                 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
753                 c->icache.ways = 4;
754                 c->icache.waybit= 0;
755
756                 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
757                 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
758                 c->dcache.ways = 4;
759                 c->dcache.waybit = 0;
760
761                 c->options |= MIPS_CPU_CACHE_CDEX_P;
762                 c->options |= MIPS_CPU_PREFETCH;
763                 break;
764
765         case CPU_R4000PC:
766         case CPU_R4000SC:
767         case CPU_R4000MC:
768         case CPU_R4400PC:
769         case CPU_R4400SC:
770         case CPU_R4400MC:
771         case CPU_R4300:
772                 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
773                 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
774                 c->icache.ways = 1;
775                 c->icache.waybit = 0;   /* doesn't matter */
776
777                 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
778                 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
779                 c->dcache.ways = 1;
780                 c->dcache.waybit = 0;   /* does not matter */
781
782                 c->options |= MIPS_CPU_CACHE_CDEX_P;
783                 break;
784
785         case CPU_R10000:
786         case CPU_R12000:
787         case CPU_R14000:
788                 icache_size = 1 << (12 + ((config & R10K_CONF_IC) >> 29));
789                 c->icache.linesz = 64;
790                 c->icache.ways = 2;
791                 c->icache.waybit = 0;
792
793                 dcache_size = 1 << (12 + ((config & R10K_CONF_DC) >> 26));
794                 c->dcache.linesz = 32;
795                 c->dcache.ways = 2;
796                 c->dcache.waybit = 0;
797
798                 c->options |= MIPS_CPU_PREFETCH;
799                 break;
800
801         case CPU_VR4133:
802                 write_c0_config(config & ~VR41_CONF_P4K);
803         case CPU_VR4131:
804                 /* Workaround for cache instruction bug of VR4131 */
805                 if (c->processor_id == 0x0c80U || c->processor_id == 0x0c81U ||
806                     c->processor_id == 0x0c82U) {
807                         config |= 0x00400000U;
808                         if (c->processor_id == 0x0c80U)
809                                 config |= VR41_CONF_BP;
810                         write_c0_config(config);
811                 } else
812                         c->options |= MIPS_CPU_CACHE_CDEX_P;
813
814                 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
815                 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
816                 c->icache.ways = 2;
817                 c->icache.waybit = __ffs(icache_size/2);
818
819                 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
820                 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
821                 c->dcache.ways = 2;
822                 c->dcache.waybit = __ffs(dcache_size/2);
823                 break;
824
825         case CPU_VR41XX:
826         case CPU_VR4111:
827         case CPU_VR4121:
828         case CPU_VR4122:
829         case CPU_VR4181:
830         case CPU_VR4181A:
831                 icache_size = 1 << (10 + ((config & CONF_IC) >> 9));
832                 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
833                 c->icache.ways = 1;
834                 c->icache.waybit = 0;   /* doesn't matter */
835
836                 dcache_size = 1 << (10 + ((config & CONF_DC) >> 6));
837                 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
838                 c->dcache.ways = 1;
839                 c->dcache.waybit = 0;   /* does not matter */
840
841                 c->options |= MIPS_CPU_CACHE_CDEX_P;
842                 break;
843
844         case CPU_RM7000:
845                 rm7k_erratum31();
846
847         case CPU_RM9000:
848                 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
849                 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
850                 c->icache.ways = 4;
851                 c->icache.waybit = __ffs(icache_size / c->icache.ways);
852
853                 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
854                 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
855                 c->dcache.ways = 4;
856                 c->dcache.waybit = __ffs(dcache_size / c->dcache.ways);
857
858 #if !defined(CONFIG_SMP) || !defined(RM9000_CDEX_SMP_WAR)
859                 c->options |= MIPS_CPU_CACHE_CDEX_P;
860 #endif
861                 c->options |= MIPS_CPU_PREFETCH;
862                 break;
863
864         case CPU_LOONGSON2:
865                 icache_size = 1 << (12 + ((config & CONF_IC) >> 9));
866                 c->icache.linesz = 16 << ((config & CONF_IB) >> 5);
867                 if (prid & 0x3)
868                         c->icache.ways = 4;
869                 else
870                         c->icache.ways = 2;
871                 c->icache.waybit = 0;
872
873                 dcache_size = 1 << (12 + ((config & CONF_DC) >> 6));
874                 c->dcache.linesz = 16 << ((config & CONF_DB) >> 4);
875                 if (prid & 0x3)
876                         c->dcache.ways = 4;
877                 else
878                         c->dcache.ways = 2;
879                 c->dcache.waybit = 0;
880                 break;
881
882         default:
883                 if (!(config & MIPS_CONF_M))
884                         panic("Don't know how to probe P-caches on this cpu.");
885
886                 /*
887                  * So we seem to be a MIPS32 or MIPS64 CPU
888                  * So let's probe the I-cache ...
889                  */
890                 config1 = read_c0_config1();
891
892                 if ((lsize = ((config1 >> 19) & 7)))
893                         c->icache.linesz = 2 << lsize;
894                 else
895                         c->icache.linesz = lsize;
896                 c->icache.sets = 64 << ((config1 >> 22) & 7);
897                 c->icache.ways = 1 + ((config1 >> 16) & 7);
898
899                 icache_size = c->icache.sets *
900                               c->icache.ways *
901                               c->icache.linesz;
902                 c->icache.waybit = __ffs(icache_size/c->icache.ways);
903
904                 if (config & 0x8)               /* VI bit */
905                         c->icache.flags |= MIPS_CACHE_VTAG;
906
907                 /*
908                  * Now probe the MIPS32 / MIPS64 data cache.
909                  */
910                 c->dcache.flags = 0;
911
912                 if ((lsize = ((config1 >> 10) & 7)))
913                         c->dcache.linesz = 2 << lsize;
914                 else
915                         c->dcache.linesz= lsize;
916                 c->dcache.sets = 64 << ((config1 >> 13) & 7);
917                 c->dcache.ways = 1 + ((config1 >> 7) & 7);
918
919                 dcache_size = c->dcache.sets *
920                               c->dcache.ways *
921                               c->dcache.linesz;
922                 c->dcache.waybit = __ffs(dcache_size/c->dcache.ways);
923
924                 c->options |= MIPS_CPU_PREFETCH;
925                 break;
926         }
927
928         /*
929          * Processor configuration sanity check for the R4000SC erratum
930          * #5.  With page sizes larger than 32kB there is no possibility
931          * to get a VCE exception anymore so we don't care about this
932          * misconfiguration.  The case is rather theoretical anyway;
933          * presumably no vendor is shipping his hardware in the "bad"
934          * configuration.
935          */
936         if ((prid & 0xff00) == PRID_IMP_R4000 && (prid & 0xff) < 0x40 &&
937             !(config & CONF_SC) && c->icache.linesz != 16 &&
938             PAGE_SIZE <= 0x8000)
939                 panic("Improper R4000SC processor configuration detected");
940
941         /* compute a couple of other cache variables */
942         c->icache.waysize = icache_size / c->icache.ways;
943         c->dcache.waysize = dcache_size / c->dcache.ways;
944
945         c->icache.sets = c->icache.linesz ?
946                 icache_size / (c->icache.linesz * c->icache.ways) : 0;
947         c->dcache.sets = c->dcache.linesz ?
948                 dcache_size / (c->dcache.linesz * c->dcache.ways) : 0;
949
950         /*
951          * R10000 and R12000 P-caches are odd in a positive way.  They're 32kB
952          * 2-way virtually indexed so normally would suffer from aliases.  So
953          * normally they'd suffer from aliases but magic in the hardware deals
954          * with that for us so we don't need to take care ourselves.
955          */
956         switch (c->cputype) {
957         case CPU_20KC:
958         case CPU_25KF:
959         case CPU_SB1:
960         case CPU_SB1A:
961                 c->dcache.flags |= MIPS_CACHE_PINDEX;
962                 break;
963
964         case CPU_R10000:
965         case CPU_R12000:
966         case CPU_R14000:
967                 break;
968
969         case CPU_24K:
970         case CPU_34K:
971         case CPU_74K:
972                 if ((read_c0_config7() & (1 << 16))) {
973                         /* effectively physically indexed dcache,
974                            thus no virtual aliases. */
975                         c->dcache.flags |= MIPS_CACHE_PINDEX;
976                         break;
977                 }
978         default:
979                 if (c->dcache.waysize > PAGE_SIZE)
980                         c->dcache.flags |= MIPS_CACHE_ALIASES;
981         }
982
983         switch (c->cputype) {
984         case CPU_20KC:
985                 /*
986                  * Some older 20Kc chips doesn't have the 'VI' bit in
987                  * the config register.
988                  */
989                 c->icache.flags |= MIPS_CACHE_VTAG;
990                 break;
991
992         case CPU_AU1000:
993         case CPU_AU1500:
994         case CPU_AU1100:
995         case CPU_AU1550:
996         case CPU_AU1200:
997         case CPU_AU1210:
998         case CPU_AU1250:
999                 c->icache.flags |= MIPS_CACHE_IC_F_DC;
1000                 break;
1001         }
1002
1003 #ifdef  CONFIG_CPU_LOONGSON2
1004         /*
1005          * LOONGSON2 has 4 way icache, but when using indexed cache op,
1006          * one op will act on all 4 ways
1007          */
1008         c->icache.ways = 1;
1009 #endif
1010
1011         printk("Primary instruction cache %ldkB, %s, %s, linesize %d bytes.\n",
1012                icache_size >> 10,
1013                cpu_has_vtag_icache ? "VIVT" : "VIPT",
1014                way_string[c->icache.ways], c->icache.linesz);
1015
1016         printk("Primary data cache %ldkB, %s, %s, %s, linesize %d bytes\n",
1017                dcache_size >> 10, way_string[c->dcache.ways],
1018                (c->dcache.flags & MIPS_CACHE_PINDEX) ? "PIPT" : "VIPT",
1019                (c->dcache.flags & MIPS_CACHE_ALIASES) ?
1020                         "cache aliases" : "no aliases",
1021                c->dcache.linesz);
1022 }
1023
1024 /*
1025  * If you even _breathe_ on this function, look at the gcc output and make sure
1026  * it does not pop things on and off the stack for the cache sizing loop that
1027  * executes in KSEG1 space or else you will crash and burn badly.  You have
1028  * been warned.
1029  */
1030 static int __cpuinit probe_scache(void)
1031 {
1032         unsigned long flags, addr, begin, end, pow2;
1033         unsigned int config = read_c0_config();
1034         struct cpuinfo_mips *c = &current_cpu_data;
1035         int tmp;
1036
1037         if (config & CONF_SC)
1038                 return 0;
1039
1040         begin = (unsigned long) &_stext;
1041         begin &= ~((4 * 1024 * 1024) - 1);
1042         end = begin + (4 * 1024 * 1024);
1043
1044         /*
1045          * This is such a bitch, you'd think they would make it easy to do
1046          * this.  Away you daemons of stupidity!
1047          */
1048         local_irq_save(flags);
1049
1050         /* Fill each size-multiple cache line with a valid tag. */
1051         pow2 = (64 * 1024);
1052         for (addr = begin; addr < end; addr = (begin + pow2)) {
1053                 unsigned long *p = (unsigned long *) addr;
1054                 __asm__ __volatile__("nop" : : "r" (*p)); /* whee... */
1055                 pow2 <<= 1;
1056         }
1057
1058         /* Load first line with zero (therefore invalid) tag. */
1059         write_c0_taglo(0);
1060         write_c0_taghi(0);
1061         __asm__ __volatile__("nop; nop; nop; nop;"); /* avoid the hazard */
1062         cache_op(Index_Store_Tag_I, begin);
1063         cache_op(Index_Store_Tag_D, begin);
1064         cache_op(Index_Store_Tag_SD, begin);
1065
1066         /* Now search for the wrap around point. */
1067         pow2 = (128 * 1024);
1068         tmp = 0;
1069         for (addr = begin + (128 * 1024); addr < end; addr = begin + pow2) {
1070                 cache_op(Index_Load_Tag_SD, addr);
1071                 __asm__ __volatile__("nop; nop; nop; nop;"); /* hazard... */
1072                 if (!read_c0_taglo())
1073                         break;
1074                 pow2 <<= 1;
1075         }
1076         local_irq_restore(flags);
1077         addr -= begin;
1078
1079         scache_size = addr;
1080         c->scache.linesz = 16 << ((config & R4K_CONF_SB) >> 22);
1081         c->scache.ways = 1;
1082         c->dcache.waybit = 0;           /* does not matter */
1083
1084         return 1;
1085 }
1086
1087 #if defined(CONFIG_CPU_LOONGSON2)
1088 static void __init loongson2_sc_init(void)
1089 {
1090         struct cpuinfo_mips *c = &current_cpu_data;
1091
1092         scache_size = 512*1024;
1093         c->scache.linesz = 32;
1094         c->scache.ways = 4;
1095         c->scache.waybit = 0;
1096         c->scache.waysize = scache_size / (c->scache.ways);
1097         c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1098         pr_info("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1099                scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1100
1101         c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1102 }
1103 #endif
1104
1105 extern int r5k_sc_init(void);
1106 extern int rm7k_sc_init(void);
1107 extern int mips_sc_init(void);
1108
1109 static void __cpuinit setup_scache(void)
1110 {
1111         struct cpuinfo_mips *c = &current_cpu_data;
1112         unsigned int config = read_c0_config();
1113         int sc_present = 0;
1114
1115         /*
1116          * Do the probing thing on R4000SC and R4400SC processors.  Other
1117          * processors don't have a S-cache that would be relevant to the
1118          * Linux memory management.
1119          */
1120         switch (c->cputype) {
1121         case CPU_R4000SC:
1122         case CPU_R4000MC:
1123         case CPU_R4400SC:
1124         case CPU_R4400MC:
1125                 sc_present = run_uncached(probe_scache);
1126                 if (sc_present)
1127                         c->options |= MIPS_CPU_CACHE_CDEX_S;
1128                 break;
1129
1130         case CPU_R10000:
1131         case CPU_R12000:
1132         case CPU_R14000:
1133                 scache_size = 0x80000 << ((config & R10K_CONF_SS) >> 16);
1134                 c->scache.linesz = 64 << ((config >> 13) & 1);
1135                 c->scache.ways = 2;
1136                 c->scache.waybit= 0;
1137                 sc_present = 1;
1138                 break;
1139
1140         case CPU_R5000:
1141         case CPU_NEVADA:
1142 #ifdef CONFIG_R5000_CPU_SCACHE
1143                 r5k_sc_init();
1144 #endif
1145                 return;
1146
1147         case CPU_RM7000:
1148         case CPU_RM9000:
1149 #ifdef CONFIG_RM7000_CPU_SCACHE
1150                 rm7k_sc_init();
1151 #endif
1152                 return;
1153
1154 #if defined(CONFIG_CPU_LOONGSON2)
1155         case CPU_LOONGSON2:
1156                 loongson2_sc_init();
1157                 return;
1158 #endif
1159
1160         default:
1161                 if (c->isa_level == MIPS_CPU_ISA_M32R1 ||
1162                     c->isa_level == MIPS_CPU_ISA_M32R2 ||
1163                     c->isa_level == MIPS_CPU_ISA_M64R1 ||
1164                     c->isa_level == MIPS_CPU_ISA_M64R2) {
1165 #ifdef CONFIG_MIPS_CPU_SCACHE
1166                         if (mips_sc_init ()) {
1167                                 scache_size = c->scache.ways * c->scache.sets * c->scache.linesz;
1168                                 printk("MIPS secondary cache %ldkB, %s, linesize %d bytes.\n",
1169                                        scache_size >> 10,
1170                                        way_string[c->scache.ways], c->scache.linesz);
1171                         }
1172 #else
1173                         if (!(c->scache.flags & MIPS_CACHE_NOT_PRESENT))
1174                                 panic("Dunno how to handle MIPS32 / MIPS64 second level cache");
1175 #endif
1176                         return;
1177                 }
1178                 sc_present = 0;
1179         }
1180
1181         if (!sc_present)
1182                 return;
1183
1184         /* compute a couple of other cache variables */
1185         c->scache.waysize = scache_size / c->scache.ways;
1186
1187         c->scache.sets = scache_size / (c->scache.linesz * c->scache.ways);
1188
1189         printk("Unified secondary cache %ldkB %s, linesize %d bytes.\n",
1190                scache_size >> 10, way_string[c->scache.ways], c->scache.linesz);
1191
1192         c->options |= MIPS_CPU_INCLUSIVE_CACHES;
1193 }
1194
1195 void au1x00_fixup_config_od(void)
1196 {
1197         /*
1198          * c0_config.od (bit 19) was write only (and read as 0)
1199          * on the early revisions of Alchemy SOCs.  It disables the bus
1200          * transaction overlapping and needs to be set to fix various errata.
1201          */
1202         switch (read_c0_prid()) {
1203         case 0x00030100: /* Au1000 DA */
1204         case 0x00030201: /* Au1000 HA */
1205         case 0x00030202: /* Au1000 HB */
1206         case 0x01030200: /* Au1500 AB */
1207         /*
1208          * Au1100 errata actually keeps silence about this bit, so we set it
1209          * just in case for those revisions that require it to be set according
1210          * to arch/mips/au1000/common/cputable.c
1211          */
1212         case 0x02030200: /* Au1100 AB */
1213         case 0x02030201: /* Au1100 BA */
1214         case 0x02030202: /* Au1100 BC */
1215                 set_c0_config(1 << 19);
1216                 break;
1217         }
1218 }
1219
1220 static int __cpuinitdata cca = -1;
1221
1222 static int __init cca_setup(char *str)
1223 {
1224         get_option(&str, &cca);
1225
1226         return 1;
1227 }
1228
1229 __setup("cca=", cca_setup);
1230
1231 static void __cpuinit coherency_setup(void)
1232 {
1233         if (cca < 0 || cca > 7)
1234                 cca = read_c0_config() & CONF_CM_CMASK;
1235         _page_cachable_default = cca << _CACHE_SHIFT;
1236
1237         pr_debug("Using cache attribute %d\n", cca);
1238         change_c0_config(CONF_CM_CMASK, cca);
1239
1240         /*
1241          * c0_status.cu=0 specifies that updates by the sc instruction use
1242          * the coherency mode specified by the TLB; 1 means cachable
1243          * coherent update on write will be used.  Not all processors have
1244          * this bit and; some wire it to zero, others like Toshiba had the
1245          * silly idea of putting something else there ...
1246          */
1247         switch (current_cpu_type()) {
1248         case CPU_R4000PC:
1249         case CPU_R4000SC:
1250         case CPU_R4000MC:
1251         case CPU_R4400PC:
1252         case CPU_R4400SC:
1253         case CPU_R4400MC:
1254                 clear_c0_config(CONF_CU);
1255                 break;
1256         /*
1257          * We need to catch the early Alchemy SOCs with
1258          * the write-only co_config.od bit and set it back to one...
1259          */
1260         case CPU_AU1000: /* rev. DA, HA, HB */
1261         case CPU_AU1100: /* rev. AB, BA, BC ?? */
1262         case CPU_AU1500: /* rev. AB */
1263                 au1x00_fixup_config_od();
1264                 break;
1265         }
1266 }
1267
1268 void __cpuinit r4k_cache_init(void)
1269 {
1270         extern void build_clear_page(void);
1271         extern void build_copy_page(void);
1272         extern char __weak except_vec2_generic;
1273         extern char __weak except_vec2_sb1;
1274         struct cpuinfo_mips *c = &current_cpu_data;
1275
1276         switch (c->cputype) {
1277         case CPU_SB1:
1278         case CPU_SB1A:
1279                 set_uncached_handler(0x100, &except_vec2_sb1, 0x80);
1280                 break;
1281
1282         default:
1283                 set_uncached_handler(0x100, &except_vec2_generic, 0x80);
1284                 break;
1285         }
1286
1287         probe_pcache();
1288         setup_scache();
1289
1290         r4k_blast_dcache_page_setup();
1291         r4k_blast_dcache_page_indexed_setup();
1292         r4k_blast_dcache_setup();
1293         r4k_blast_icache_page_setup();
1294         r4k_blast_icache_page_indexed_setup();
1295         r4k_blast_icache_setup();
1296         r4k_blast_scache_page_setup();
1297         r4k_blast_scache_page_indexed_setup();
1298         r4k_blast_scache_setup();
1299
1300         /*
1301          * Some MIPS32 and MIPS64 processors have physically indexed caches.
1302          * This code supports virtually indexed processors and will be
1303          * unnecessarily inefficient on physically indexed processors.
1304          */
1305         if (c->dcache.linesz)
1306                 shm_align_mask = max_t( unsigned long,
1307                                         c->dcache.sets * c->dcache.linesz - 1,
1308                                         PAGE_SIZE - 1);
1309         else
1310                 shm_align_mask = PAGE_SIZE-1;
1311
1312         __flush_cache_vmap      = r4k__flush_cache_vmap;
1313         __flush_cache_vunmap    = r4k__flush_cache_vunmap;
1314
1315         flush_cache_all         = cache_noop;
1316         __flush_cache_all       = r4k___flush_cache_all;
1317         flush_cache_mm          = r4k_flush_cache_mm;
1318         flush_cache_page        = r4k_flush_cache_page;
1319         flush_cache_range       = r4k_flush_cache_range;
1320
1321         flush_cache_sigtramp    = r4k_flush_cache_sigtramp;
1322         flush_icache_all        = r4k_flush_icache_all;
1323         local_flush_data_cache_page     = local_r4k_flush_data_cache_page;
1324         flush_data_cache_page   = r4k_flush_data_cache_page;
1325         flush_icache_range      = r4k_flush_icache_range;
1326
1327 #ifdef CONFIG_DMA_NONCOHERENT
1328         _dma_cache_wback_inv    = r4k_dma_cache_wback_inv;
1329         _dma_cache_wback        = r4k_dma_cache_wback_inv;
1330         _dma_cache_inv          = r4k_dma_cache_inv;
1331 #endif
1332
1333         build_clear_page();
1334         build_copy_page();
1335         local_r4k___flush_cache_all(NULL);
1336         coherency_setup();
1337 }