Avoid SMP cacheflushes. This is a minor optimization of startup but
[firewire-audio.git] / arch / mips / mm / c-tx39.c
blob56c3fcdd282296b38b806d5d8404633dd0b1ab01
1 /*
2 * r2300.c: R2000 and R3000 specific mmu/cache code.
4 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
6 * with a lot of changes to make this thing work for R3000s
7 * Tx39XX R4k style caches added. HK
8 * Copyright (C) 1998, 1999, 2000 Harald Koerfgen
9 * Copyright (C) 1998 Gleb Raiko & Vladimir Roganov
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/mm.h>
16 #include <asm/cacheops.h>
17 #include <asm/page.h>
18 #include <asm/pgtable.h>
19 #include <asm/mmu_context.h>
20 #include <asm/system.h>
21 #include <asm/isadep.h>
22 #include <asm/io.h>
23 #include <asm/bootinfo.h>
24 #include <asm/cpu.h>
26 /* For R3000 cores with R4000 style caches */
27 static unsigned long icache_size, dcache_size; /* Size in bytes */
29 #include <asm/r4kcache.h>
31 extern int r3k_have_wired_reg; /* in r3k-tlb.c */
33 /* This sequence is required to ensure icache is disabled immediately */
34 #define TX39_STOP_STREAMING() \
35 __asm__ __volatile__( \
36 ".set push\n\t" \
37 ".set noreorder\n\t" \
38 "b 1f\n\t" \
39 "nop\n\t" \
40 "1:\n\t" \
41 ".set pop" \
44 /* TX39H-style cache flush routines. */
45 static void tx39h_flush_icache_all(void)
47 unsigned long start = KSEG0;
48 unsigned long end = (start + icache_size);
49 unsigned long flags, config;
51 /* disable icache (set ICE#) */
52 local_irq_save(flags);
53 config = read_c0_conf();
54 write_c0_conf(config & ~TX39_CONF_ICE);
55 TX39_STOP_STREAMING();
57 /* invalidate icache */
58 while (start < end) {
59 cache16_unroll32(start, Index_Invalidate_I);
60 start += 0x200;
63 write_c0_conf(config);
64 local_irq_restore(flags);
67 static void tx39h_dma_cache_wback_inv(unsigned long addr, unsigned long size)
69 unsigned long end, a;
70 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
72 /* Catch bad driver code */
73 BUG_ON(size == 0);
75 iob();
76 a = addr & ~(dc_lsize - 1);
77 end = (addr + size - 1) & ~(dc_lsize - 1);
78 while (1) {
79 invalidate_dcache_line(a); /* Hit_Invalidate_D */
80 if (a == end) break;
81 a += dc_lsize;
86 /* TX39H2,TX39H3 */
87 static inline void tx39_blast_dcache_page(unsigned long addr)
89 if (current_cpu_data.cputype != CPU_TX3912)
90 blast_dcache16_page(addr);
93 static inline void tx39_blast_dcache_page_indexed(unsigned long addr)
95 blast_dcache16_page_indexed(addr);
98 static inline void tx39_blast_dcache(void)
100 blast_dcache16();
103 static inline void tx39_blast_icache_page(unsigned long addr)
105 unsigned long flags, config;
106 /* disable icache (set ICE#) */
107 local_irq_save(flags);
108 config = read_c0_conf();
109 write_c0_conf(config & ~TX39_CONF_ICE);
110 TX39_STOP_STREAMING();
111 blast_icache16_page(addr);
112 write_c0_conf(config);
113 local_irq_restore(flags);
116 static inline void tx39_blast_icache_page_indexed(unsigned long addr)
118 unsigned long flags, config;
119 /* disable icache (set ICE#) */
120 local_irq_save(flags);
121 config = read_c0_conf();
122 write_c0_conf(config & ~TX39_CONF_ICE);
123 TX39_STOP_STREAMING();
124 blast_icache16_page_indexed(addr);
125 write_c0_conf(config);
126 local_irq_restore(flags);
129 static inline void tx39_blast_icache(void)
131 unsigned long flags, config;
132 /* disable icache (set ICE#) */
133 local_irq_save(flags);
134 config = read_c0_conf();
135 write_c0_conf(config & ~TX39_CONF_ICE);
136 TX39_STOP_STREAMING();
137 blast_icache16();
138 write_c0_conf(config);
139 local_irq_restore(flags);
142 static inline void tx39_flush_cache_all(void)
144 if (!cpu_has_dc_aliases)
145 return;
147 tx39_blast_dcache();
148 tx39_blast_icache();
151 static inline void tx39___flush_cache_all(void)
153 tx39_blast_dcache();
154 tx39_blast_icache();
157 static void tx39_flush_cache_mm(struct mm_struct *mm)
159 if (!cpu_has_dc_aliases)
160 return;
162 if (cpu_context(smp_processor_id(), mm) != 0) {
163 tx39_flush_cache_all();
167 static void tx39_flush_cache_range(struct vm_area_struct *vma,
168 unsigned long start, unsigned long end)
170 struct mm_struct *mm = vma->vm_mm;
172 if (!cpu_has_dc_aliases)
173 return;
175 if (cpu_context(smp_processor_id(), mm) != 0) {
176 tx39_blast_dcache();
177 tx39_blast_icache();
181 static void tx39_flush_cache_page(struct vm_area_struct *vma, unsigned long page, unsigned long pfn)
183 int exec = vma->vm_flags & VM_EXEC;
184 struct mm_struct *mm = vma->vm_mm;
185 pgd_t *pgdp;
186 pud_t *pudp;
187 pmd_t *pmdp;
188 pte_t *ptep;
191 * If ownes no valid ASID yet, cannot possibly have gotten
192 * this page into the cache.
194 if (cpu_context(smp_processor_id(), mm) == 0)
195 return;
197 page &= PAGE_MASK;
198 pgdp = pgd_offset(mm, page);
199 pudp = pud_offset(pgdp, page);
200 pmdp = pmd_offset(pudp, page);
201 ptep = pte_offset(pmdp, page);
204 * If the page isn't marked valid, the page cannot possibly be
205 * in the cache.
207 if (!(pte_val(*ptep) & _PAGE_PRESENT))
208 return;
211 * Doing flushes for another ASID than the current one is
212 * too difficult since stupid R4k caches do a TLB translation
213 * for every cache flush operation. So we do indexed flushes
214 * in that case, which doesn't overly flush the cache too much.
216 if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
217 if (cpu_has_dc_aliases || exec)
218 tx39_blast_dcache_page(page);
219 if (exec)
220 tx39_blast_icache_page(page);
222 return;
226 * Do indexed flush, too much work to get the (possible) TLB refills
227 * to work correctly.
229 page = (KSEG0 + (page & (dcache_size - 1)));
230 if (cpu_has_dc_aliases || exec)
231 tx39_blast_dcache_page_indexed(page);
232 if (exec)
233 tx39_blast_icache_page_indexed(page);
236 static void tx39_flush_data_cache_page(unsigned long addr)
238 tx39_blast_dcache_page(addr);
241 static void tx39_flush_icache_range(unsigned long start, unsigned long end)
243 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
244 unsigned long addr, aend;
246 if (end - start > dcache_size)
247 tx39_blast_dcache();
248 else {
249 addr = start & ~(dc_lsize - 1);
250 aend = (end - 1) & ~(dc_lsize - 1);
252 while (1) {
253 /* Hit_Writeback_Inv_D */
254 protected_writeback_dcache_line(addr);
255 if (addr == aend)
256 break;
257 addr += dc_lsize;
261 if (end - start > icache_size)
262 tx39_blast_icache();
263 else {
264 unsigned long flags, config;
265 addr = start & ~(dc_lsize - 1);
266 aend = (end - 1) & ~(dc_lsize - 1);
267 /* disable icache (set ICE#) */
268 local_irq_save(flags);
269 config = read_c0_conf();
270 write_c0_conf(config & ~TX39_CONF_ICE);
271 TX39_STOP_STREAMING();
272 while (1) {
273 /* Hit_Invalidate_I */
274 protected_flush_icache_line(addr);
275 if (addr == aend)
276 break;
277 addr += dc_lsize;
279 write_c0_conf(config);
280 local_irq_restore(flags);
285 * Ok, this seriously sucks. We use them to flush a user page but don't
286 * know the virtual address, so we have to blast away the whole icache
287 * which is significantly more expensive than the real thing. Otoh we at
288 * least know the kernel address of the page so we can flush it
289 * selectivly.
291 static void tx39_flush_icache_page(struct vm_area_struct *vma, struct page *page)
293 unsigned long addr;
295 * If there's no context yet, or the page isn't executable, no icache
296 * flush is needed.
298 if (!(vma->vm_flags & VM_EXEC))
299 return;
301 addr = (unsigned long) page_address(page);
302 tx39_blast_dcache_page(addr);
305 * We're not sure of the virtual address(es) involved here, so
306 * we have to flush the entire I-cache.
308 tx39_blast_icache();
311 static void tx39_dma_cache_wback_inv(unsigned long addr, unsigned long size)
313 unsigned long end, a;
315 if (((size | addr) & (PAGE_SIZE - 1)) == 0) {
316 end = addr + size;
317 do {
318 tx39_blast_dcache_page(addr);
319 addr += PAGE_SIZE;
320 } while(addr != end);
321 } else if (size > dcache_size) {
322 tx39_blast_dcache();
323 } else {
324 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
325 a = addr & ~(dc_lsize - 1);
326 end = (addr + size - 1) & ~(dc_lsize - 1);
327 while (1) {
328 flush_dcache_line(a); /* Hit_Writeback_Inv_D */
329 if (a == end) break;
330 a += dc_lsize;
335 static void tx39_dma_cache_inv(unsigned long addr, unsigned long size)
337 unsigned long end, a;
339 if (((size | addr) & (PAGE_SIZE - 1)) == 0) {
340 end = addr + size;
341 do {
342 tx39_blast_dcache_page(addr);
343 addr += PAGE_SIZE;
344 } while(addr != end);
345 } else if (size > dcache_size) {
346 tx39_blast_dcache();
347 } else {
348 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
349 a = addr & ~(dc_lsize - 1);
350 end = (addr + size - 1) & ~(dc_lsize - 1);
351 while (1) {
352 invalidate_dcache_line(a); /* Hit_Invalidate_D */
353 if (a == end) break;
354 a += dc_lsize;
359 static void tx39_flush_cache_sigtramp(unsigned long addr)
361 unsigned long ic_lsize = current_cpu_data.icache.linesz;
362 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
363 unsigned long config;
364 unsigned long flags;
366 protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
368 /* disable icache (set ICE#) */
369 local_irq_save(flags);
370 config = read_c0_conf();
371 write_c0_conf(config & ~TX39_CONF_ICE);
372 TX39_STOP_STREAMING();
373 protected_flush_icache_line(addr & ~(ic_lsize - 1));
374 write_c0_conf(config);
375 local_irq_restore(flags);
378 static __init void tx39_probe_cache(void)
380 unsigned long config;
382 config = read_c0_conf();
384 icache_size = 1 << (10 + ((config & TX39_CONF_ICS_MASK) >>
385 TX39_CONF_ICS_SHIFT));
386 dcache_size = 1 << (10 + ((config & TX39_CONF_DCS_MASK) >>
387 TX39_CONF_DCS_SHIFT));
389 current_cpu_data.icache.linesz = 16;
390 switch (current_cpu_data.cputype) {
391 case CPU_TX3912:
392 current_cpu_data.icache.ways = 1;
393 current_cpu_data.dcache.ways = 1;
394 current_cpu_data.dcache.linesz = 4;
395 break;
397 case CPU_TX3927:
398 current_cpu_data.icache.ways = 2;
399 current_cpu_data.dcache.ways = 2;
400 current_cpu_data.dcache.linesz = 16;
401 break;
403 case CPU_TX3922:
404 default:
405 current_cpu_data.icache.ways = 1;
406 current_cpu_data.dcache.ways = 1;
407 current_cpu_data.dcache.linesz = 16;
408 break;
412 void __init ld_mmu_tx39(void)
414 extern void build_clear_page(void);
415 extern void build_copy_page(void);
416 unsigned long config;
418 config = read_c0_conf();
419 config &= ~TX39_CONF_WBON;
420 write_c0_conf(config);
422 tx39_probe_cache();
424 switch (current_cpu_data.cputype) {
425 case CPU_TX3912:
426 /* TX39/H core (writethru direct-map cache) */
427 flush_cache_all = tx39h_flush_icache_all;
428 __flush_cache_all = tx39h_flush_icache_all;
429 flush_cache_mm = (void *) tx39h_flush_icache_all;
430 flush_cache_range = (void *) tx39h_flush_icache_all;
431 flush_cache_page = (void *) tx39h_flush_icache_all;
432 flush_icache_page = (void *) tx39h_flush_icache_all;
433 flush_icache_range = (void *) tx39h_flush_icache_all;
435 flush_cache_sigtramp = (void *) tx39h_flush_icache_all;
436 flush_data_cache_page = (void *) tx39h_flush_icache_all;
438 _dma_cache_wback_inv = tx39h_dma_cache_wback_inv;
440 shm_align_mask = PAGE_SIZE - 1;
442 break;
444 case CPU_TX3922:
445 case CPU_TX3927:
446 default:
447 /* TX39/H2,H3 core (writeback 2way-set-associative cache) */
448 r3k_have_wired_reg = 1;
449 write_c0_wired(0); /* set 8 on reset... */
450 /* board-dependent init code may set WBON */
452 flush_cache_all = tx39_flush_cache_all;
453 __flush_cache_all = tx39___flush_cache_all;
454 flush_cache_mm = tx39_flush_cache_mm;
455 flush_cache_range = tx39_flush_cache_range;
456 flush_cache_page = tx39_flush_cache_page;
457 flush_icache_page = tx39_flush_icache_page;
458 flush_icache_range = tx39_flush_icache_range;
460 flush_cache_sigtramp = tx39_flush_cache_sigtramp;
461 flush_data_cache_page = tx39_flush_data_cache_page;
463 _dma_cache_wback_inv = tx39_dma_cache_wback_inv;
464 _dma_cache_wback = tx39_dma_cache_wback_inv;
465 _dma_cache_inv = tx39_dma_cache_inv;
467 shm_align_mask = max_t(unsigned long,
468 (dcache_size / current_cpu_data.dcache.ways) - 1,
469 PAGE_SIZE - 1);
471 break;
474 current_cpu_data.icache.waysize = icache_size / current_cpu_data.icache.ways;
475 current_cpu_data.dcache.waysize = dcache_size / current_cpu_data.dcache.ways;
477 current_cpu_data.icache.sets =
478 current_cpu_data.icache.waysize / current_cpu_data.icache.linesz;
479 current_cpu_data.dcache.sets =
480 current_cpu_data.dcache.waysize / current_cpu_data.dcache.linesz;
482 if (current_cpu_data.dcache.waysize > PAGE_SIZE)
483 current_cpu_data.dcache.flags |= MIPS_CACHE_ALIASES;
485 current_cpu_data.icache.waybit = 0;
486 current_cpu_data.dcache.waybit = 0;
488 printk("Primary instruction cache %ldkB, linesize %d bytes\n",
489 icache_size >> 10, current_cpu_data.icache.linesz);
490 printk("Primary data cache %ldkB, linesize %d bytes\n",
491 dcache_size >> 10, current_cpu_data.dcache.linesz);
493 build_clear_page();
494 build_copy_page();
495 tx39h_flush_icache_all();