ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / mm / c-tx39.c
1 /*
2  * r2300.c: R2000 and R3000 specific mmu/cache code.
3  *
4  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
5  *
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
10  */
11 #include <linux/init.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/mm.h>
15
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>
25
26 /* For R3000 cores with R4000 style caches */
27 static unsigned long icache_size, dcache_size;          /* Size in bytes */
28
29 #include <asm/r4kcache.h>
30
31 extern int r3k_have_wired_reg;  /* in r3k-tlb.c */
32
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" \
42         )
43
44 /* TX39H-style cache flush routines. */
45 static void tx39h_flush_icache_all(void)
46 {
47         unsigned long start = KSEG0;
48         unsigned long end = (start + icache_size);
49         unsigned long flags, config;
50
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();
56
57         /* invalidate icache */
58         while (start < end) {
59                 cache16_unroll32(start, Index_Invalidate_I);
60                 start += 0x200;
61         }
62
63         write_c0_conf(config);
64         local_irq_restore(flags);
65 }
66
67 static void tx39h_dma_cache_wback_inv(unsigned long addr, unsigned long size)
68 {
69         unsigned long end, a;
70         unsigned long dc_lsize = current_cpu_data.dcache.linesz;
71
72         /* Catch bad driver code */
73         BUG_ON(size == 0);
74
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;
82         }
83 }
84
85
86 /* TX39H2,TX39H3 */
87 static inline void tx39_blast_dcache_page(unsigned long addr)
88 {
89         if (current_cpu_data.cputype != CPU_TX3912)
90                 blast_dcache16_page(addr);
91 }
92
93 static inline void tx39_blast_dcache_page_indexed(unsigned long addr)
94 {
95         blast_dcache16_page_indexed(addr);
96 }
97
98 static inline void tx39_blast_dcache(void)
99 {
100         blast_dcache16();
101 }
102
103 static inline void tx39_blast_icache_page(unsigned long addr)
104 {
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);
114 }
115
116 static inline void tx39_blast_icache_page_indexed(unsigned long addr)
117 {
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);
127 }
128
129 static inline void tx39_blast_icache(void)
130 {
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);
140 }
141
142 static inline void tx39_flush_cache_all(void)
143 {
144         if (!cpu_has_dc_aliases)
145                 return;
146
147         tx39_blast_dcache();
148         tx39_blast_icache();
149 }
150
151 static inline void tx39___flush_cache_all(void)
152 {
153         tx39_blast_dcache();
154         tx39_blast_icache();
155 }
156
157 static void tx39_flush_cache_mm(struct mm_struct *mm)
158 {
159         if (!cpu_has_dc_aliases)
160                 return;
161
162         if (cpu_context(smp_processor_id(), mm) != 0) {
163                 tx39_flush_cache_all();
164         }
165 }
166
167 static void tx39_flush_cache_range(struct vm_area_struct *vma,
168         unsigned long start, unsigned long end)
169 {
170         struct mm_struct *mm = vma->vm_mm;
171
172         if (!cpu_has_dc_aliases)
173                 return;
174
175         if (cpu_context(smp_processor_id(), mm) != 0) {
176                 tx39_blast_dcache();
177                 tx39_blast_icache();
178         }
179 }
180
181 static void tx39_flush_cache_page(struct vm_area_struct *vma,
182                                    unsigned long page)
183 {
184         int exec = vma->vm_flags & VM_EXEC;
185         struct mm_struct *mm = vma->vm_mm;
186         pgd_t *pgdp;
187         pmd_t *pmdp;
188         pte_t *ptep;
189
190         /*
191          * If ownes no valid ASID yet, cannot possibly have gotten
192          * this page into the cache.
193          */
194         if (cpu_context(smp_processor_id(), mm) == 0)
195                 return;
196
197         page &= PAGE_MASK;
198         pgdp = pgd_offset(mm, page);
199         pmdp = pmd_offset(pgdp, page);
200         ptep = pte_offset(pmdp, page);
201
202         /*
203          * If the page isn't marked valid, the page cannot possibly be
204          * in the cache.
205          */
206         if (!(pte_val(*ptep) & _PAGE_PRESENT))
207                 return;
208
209         /*
210          * Doing flushes for another ASID than the current one is
211          * too difficult since stupid R4k caches do a TLB translation
212          * for every cache flush operation.  So we do indexed flushes
213          * in that case, which doesn't overly flush the cache too much.
214          */
215         if ((mm == current->active_mm) && (pte_val(*ptep) & _PAGE_VALID)) {
216                 if (cpu_has_dc_aliases || exec)
217                         tx39_blast_dcache_page(page);
218                 if (exec)
219                         tx39_blast_icache_page(page);
220
221                 return;
222         }
223
224         /*
225          * Do indexed flush, too much work to get the (possible) TLB refills
226          * to work correctly.
227          */
228         page = (KSEG0 + (page & (dcache_size - 1)));
229         if (cpu_has_dc_aliases || exec)
230                 tx39_blast_dcache_page_indexed(page);
231         if (exec)
232                 tx39_blast_icache_page_indexed(page);
233 }
234
235 static void tx39_flush_data_cache_page(unsigned long addr)
236 {
237         tx39_blast_dcache_page(addr);
238 }
239
240 static void tx39_flush_icache_range(unsigned long start, unsigned long end)
241 {
242         unsigned long dc_lsize = current_cpu_data.dcache.linesz;
243         unsigned long addr, aend;
244
245         if (end - start > dcache_size)
246                 tx39_blast_dcache();
247         else {
248                 addr = start & ~(dc_lsize - 1);
249                 aend = (end - 1) & ~(dc_lsize - 1);
250
251                 while (1) {
252                         /* Hit_Writeback_Inv_D */
253                         protected_writeback_dcache_line(addr);
254                         if (addr == aend)
255                                 break;
256                         addr += dc_lsize;
257                 }
258         }
259
260         if (end - start > icache_size)
261                 tx39_blast_icache();
262         else {
263                 unsigned long flags, config;
264                 addr = start & ~(dc_lsize - 1);
265                 aend = (end - 1) & ~(dc_lsize - 1);
266                 /* disable icache (set ICE#) */
267                 local_irq_save(flags);
268                 config = read_c0_conf();
269                 write_c0_conf(config & ~TX39_CONF_ICE);
270                 TX39_STOP_STREAMING();
271                 while (1) {
272                         /* Hit_Invalidate_I */
273                         protected_flush_icache_line(addr);
274                         if (addr == aend)
275                                 break;
276                         addr += dc_lsize;
277                 }
278                 write_c0_conf(config);
279                 local_irq_restore(flags);
280         }
281 }
282
283 /*
284  * Ok, this seriously sucks.  We use them to flush a user page but don't
285  * know the virtual address, so we have to blast away the whole icache
286  * which is significantly more expensive than the real thing.  Otoh we at
287  * least know the kernel address of the page so we can flush it
288  * selectivly.
289  */
290 static void tx39_flush_icache_page(struct vm_area_struct *vma, struct page *page)
291 {
292         unsigned long addr;
293         /*
294          * If there's no context yet, or the page isn't executable, no icache
295          * flush is needed.
296          */
297         if (!(vma->vm_flags & VM_EXEC))
298                 return;
299
300         addr = (unsigned long) page_address(page);
301         tx39_blast_dcache_page(addr);
302
303         /*
304          * We're not sure of the virtual address(es) involved here, so
305          * we have to flush the entire I-cache.
306          */
307         tx39_blast_icache();
308 }
309
310 static void tx39_dma_cache_wback_inv(unsigned long addr, unsigned long size)
311 {
312         unsigned long end, a;
313
314         if (((size | addr) & (PAGE_SIZE - 1)) == 0) {
315                 end = addr + size;
316                 do {
317                         tx39_blast_dcache_page(addr);
318                         addr += PAGE_SIZE;
319                 } while(addr != end);
320         } else if (size > dcache_size) {
321                 tx39_blast_dcache();
322         } else {
323                 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
324                 a = addr & ~(dc_lsize - 1);
325                 end = (addr + size - 1) & ~(dc_lsize - 1);
326                 while (1) {
327                         flush_dcache_line(a); /* Hit_Writeback_Inv_D */
328                         if (a == end) break;
329                         a += dc_lsize;
330                 }
331         }
332 }
333
334 static void tx39_dma_cache_inv(unsigned long addr, unsigned long size)
335 {
336         unsigned long end, a;
337
338         if (((size | addr) & (PAGE_SIZE - 1)) == 0) {
339                 end = addr + size;
340                 do {
341                         tx39_blast_dcache_page(addr);
342                         addr += PAGE_SIZE;
343                 } while(addr != end);
344         } else if (size > dcache_size) {
345                 tx39_blast_dcache();
346         } else {
347                 unsigned long dc_lsize = current_cpu_data.dcache.linesz;
348                 a = addr & ~(dc_lsize - 1);
349                 end = (addr + size - 1) & ~(dc_lsize - 1);
350                 while (1) {
351                         invalidate_dcache_line(a); /* Hit_Invalidate_D */
352                         if (a == end) break;
353                         a += dc_lsize;
354                 }
355         }
356 }
357
358 static void tx39_flush_cache_sigtramp(unsigned long addr)
359 {
360         unsigned long ic_lsize = current_cpu_data.icache.linesz;
361         unsigned long dc_lsize = current_cpu_data.dcache.linesz;
362         unsigned long config;
363         unsigned long flags;
364
365         protected_writeback_dcache_line(addr & ~(dc_lsize - 1));
366
367         /* disable icache (set ICE#) */
368         local_irq_save(flags);
369         config = read_c0_conf();
370         write_c0_conf(config & ~TX39_CONF_ICE);
371         TX39_STOP_STREAMING();
372         protected_flush_icache_line(addr & ~(ic_lsize - 1));
373         write_c0_conf(config);
374         local_irq_restore(flags);
375 }
376
377 static __init void tx39_probe_cache(void)
378 {
379         unsigned long config;
380
381         config = read_c0_conf();
382
383         icache_size = 1 << (10 + ((config & TX39_CONF_ICS_MASK) >>
384                                   TX39_CONF_ICS_SHIFT));
385         dcache_size = 1 << (10 + ((config & TX39_CONF_DCS_MASK) >>
386                                   TX39_CONF_DCS_SHIFT));
387
388         current_cpu_data.icache.linesz = 16;
389         switch (current_cpu_data.cputype) {
390         case CPU_TX3912:
391                 current_cpu_data.icache.ways = 1;
392                 current_cpu_data.dcache.ways = 1;
393                 current_cpu_data.dcache.linesz = 4;
394                 break;
395
396         case CPU_TX3927:
397                 current_cpu_data.icache.ways = 2;
398                 current_cpu_data.dcache.ways = 2;
399                 current_cpu_data.dcache.linesz = 16;
400                 break;
401
402         case CPU_TX3922:
403         default:
404                 current_cpu_data.icache.ways = 1;
405                 current_cpu_data.dcache.ways = 1;
406                 current_cpu_data.dcache.linesz = 16;
407                 break;
408         }
409 }
410
411 void __init ld_mmu_tx39(void)
412 {
413         extern void build_clear_page(void);
414         extern void build_copy_page(void);
415         unsigned long config;
416
417         config = read_c0_conf();
418         config &= ~TX39_CONF_WBON;
419         write_c0_conf(config);
420
421         tx39_probe_cache();
422
423         switch (current_cpu_data.cputype) {
424         case CPU_TX3912:
425                 /* TX39/H core (writethru direct-map cache) */
426                 flush_cache_all = tx39h_flush_icache_all;
427                 __flush_cache_all       = tx39h_flush_icache_all;
428                 flush_cache_mm          = (void *) tx39h_flush_icache_all;
429                 flush_cache_range       = (void *) tx39h_flush_icache_all;
430                 flush_cache_page        = (void *) tx39h_flush_icache_all;
431                 flush_icache_page       = (void *) tx39h_flush_icache_all;
432                 flush_icache_range      = (void *) tx39h_flush_icache_all;
433
434                 flush_cache_sigtramp    = (void *) tx39h_flush_icache_all;
435                 flush_data_cache_page   = (void *) tx39h_flush_icache_all;
436
437                 _dma_cache_wback_inv    = tx39h_dma_cache_wback_inv;
438
439                 shm_align_mask          = PAGE_SIZE - 1;
440
441                 break;
442
443         case CPU_TX3922:
444         case CPU_TX3927:
445         default:
446                 /* TX39/H2,H3 core (writeback 2way-set-associative cache) */
447                 r3k_have_wired_reg = 1;
448                 write_c0_wired(0);      /* set 8 on reset... */
449                 /* board-dependent init code may set WBON */
450
451                 flush_cache_all = tx39_flush_cache_all;
452                 __flush_cache_all = tx39___flush_cache_all;
453                 flush_cache_mm = tx39_flush_cache_mm;
454                 flush_cache_range = tx39_flush_cache_range;
455                 flush_cache_page = tx39_flush_cache_page;
456                 flush_icache_page = tx39_flush_icache_page;
457                 flush_icache_range = tx39_flush_icache_range;
458
459                 flush_cache_sigtramp = tx39_flush_cache_sigtramp;
460                 flush_data_cache_page = tx39_flush_data_cache_page;
461
462                 _dma_cache_wback_inv = tx39_dma_cache_wback_inv;
463                 _dma_cache_wback = tx39_dma_cache_wback_inv;
464                 _dma_cache_inv = tx39_dma_cache_inv;
465
466                 shm_align_mask = max_t(unsigned long,
467                                        (dcache_size / current_cpu_data.dcache.ways) - 1,
468                                        PAGE_SIZE - 1);
469
470                 break;
471         }
472
473         current_cpu_data.icache.waysize = icache_size / current_cpu_data.icache.ways;
474         current_cpu_data.dcache.waysize = dcache_size / current_cpu_data.dcache.ways;
475
476         current_cpu_data.icache.sets =
477                 current_cpu_data.icache.waysize / current_cpu_data.icache.linesz;
478         current_cpu_data.dcache.sets =
479                 current_cpu_data.dcache.waysize / current_cpu_data.dcache.linesz;
480
481         if (current_cpu_data.dcache.waysize > PAGE_SIZE)
482                 current_cpu_data.dcache.flags |= MIPS_CACHE_ALIASES;
483
484         current_cpu_data.icache.waybit = 0;
485         current_cpu_data.dcache.waybit = 0;
486
487         printk("Primary instruction cache %ldkb, linesize %d bytes\n",
488                 icache_size >> 10, current_cpu_data.icache.linesz);
489         printk("Primary data cache %ldkb, linesize %d bytes\n",
490                 dcache_size >> 10, current_cpu_data.dcache.linesz);
491
492         build_clear_page();
493         build_copy_page();
494 }