ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / mm / c-sb1.c
1 /*
2  * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
3  * Copyright (C) 1997, 2001 Ralf Baechle (ralf@gnu.org)
4  * Copyright (C) 2000, 2001, 2002, 2003 Broadcom Corporation
5  * Copyright (C) 2004  Maciej W. Rozycki
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21 #include <linux/config.h>
22 #include <linux/init.h>
23 #include <asm/mmu_context.h>
24 #include <asm/bootinfo.h>
25 #include <asm/cacheops.h>
26 #include <asm/cpu.h>
27 #include <asm/uaccess.h>
28
29 extern void sb1_dma_init(void);
30
31 /* These are probed at ld_mmu time */
32 static unsigned long icache_size;
33 static unsigned long dcache_size;
34
35 static unsigned long icache_line_size;
36 static unsigned long dcache_line_size;
37
38 static unsigned int icache_index_mask;
39 static unsigned int dcache_index_mask;
40
41 static unsigned long icache_assoc;
42 static unsigned long dcache_assoc;
43
44 static unsigned int icache_sets;
45 static unsigned int dcache_sets;
46
47 static unsigned int icache_range_cutoff;
48 static unsigned int dcache_range_cutoff;
49
50 /*
51  * The dcache is fully coherent to the system, with one
52  * big caveat:  the instruction stream.  In other words,
53  * if we miss in the icache, and have dirty data in the
54  * L1 dcache, then we'll go out to memory (or the L2) and
55  * get the not-as-recent data.
56  *
57  * So the only time we have to flush the dcache is when
58  * we're flushing the icache.  Since the L2 is fully
59  * coherent to everything, including I/O, we never have
60  * to flush it
61  */
62
63 #define cache_set_op(op, addr)                                          \
64         __asm__ __volatile__(                                           \
65         "       .set    noreorder               \n"                     \
66         "       .set    mips64\n\t              \n"                     \
67         "       cache   %0, (0<<13)(%1)         \n"                     \
68         "       cache   %0, (1<<13)(%1)         \n"                     \
69         "       cache   %0, (2<<13)(%1)         \n"                     \
70         "       cache   %0, (3<<13)(%1)         \n"                     \
71         "       .set    mips0                   \n"                     \
72         "       .set    reorder"                                        \
73         :                                                               \
74         : "i" (op), "r" (addr))
75
76 #define sync()                                                          \
77         __asm__ __volatile(                                             \
78         "       .set    mips64\n\t              \n"                     \
79         "       sync                            \n"                     \
80         "       .set    mips0")
81
82 #define mispredict()                                                    \
83         __asm__ __volatile__(                                           \
84         "       bnezl  $0, 1f           \n" /* Force mispredict */      \
85         "1:                             \n");
86
87 /*
88  * Writeback and invalidate the entire dcache
89  */
90 static inline void __sb1_writeback_inv_dcache_all(void)
91 {
92         unsigned long addr = 0;
93
94         while (addr < dcache_line_size * dcache_sets) {
95                 cache_set_op(Index_Writeback_Inv_D, addr);
96                 addr += dcache_line_size;
97         }
98 }
99
100 /*
101  * Writeback and invalidate a range of the dcache.  The addresses are
102  * virtual, and since we're using index ops and bit 12 is part of both
103  * the virtual frame and physical index, we have to clear both sets
104  * (bit 12 set and cleared).
105  */
106 static inline void __sb1_writeback_inv_dcache_range(unsigned long start,
107         unsigned long end)
108 {
109         unsigned long index;
110
111         start &= ~(dcache_line_size - 1);
112         end = (end + dcache_line_size - 1) & ~(dcache_line_size - 1);
113
114         while (start != end) {
115                 index = start & dcache_index_mask;
116                 cache_set_op(Index_Writeback_Inv_D, index);
117                 cache_set_op(Index_Writeback_Inv_D, index ^ (1<<12));
118                 start += dcache_line_size;
119         }
120         sync();
121 }
122
123 /*
124  * Writeback and invalidate a range of the dcache.  With physical
125  * addresseses, we don't have to worry about possible bit 12 aliasing.
126  * XXXKW is it worth turning on KX and using hit ops with xkphys?
127  */
128 static inline void __sb1_writeback_inv_dcache_phys_range(unsigned long start,
129         unsigned long end)
130 {
131         start &= ~(dcache_line_size - 1);
132         end = (end + dcache_line_size - 1) & ~(dcache_line_size - 1);
133
134         while (start != end) {
135                 cache_set_op(Index_Writeback_Inv_D, start & dcache_index_mask);
136                 start += dcache_line_size;
137         }
138         sync();
139 }
140
141
142 /*
143  * Invalidate the entire icache
144  */
145 static inline void __sb1_flush_icache_all(void)
146 {
147         unsigned long addr = 0;
148
149         while (addr < icache_line_size * icache_sets) {
150                 cache_set_op(Index_Invalidate_I, addr);
151                 addr += icache_line_size;
152         }
153 }
154
155 /*
156  * Flush the icache for a given physical page.  Need to writeback the
157  * dcache first, then invalidate the icache.  If the page isn't
158  * executable, nothing is required.
159  */
160 static void local_sb1_flush_cache_page(struct vm_area_struct *vma,
161         unsigned long addr)
162 {
163         int cpu = smp_processor_id();
164
165 #ifndef CONFIG_SMP
166         if (!(vma->vm_flags & VM_EXEC))
167                 return;
168 #endif
169
170         __sb1_writeback_inv_dcache_range(addr, addr + PAGE_SIZE);
171
172         /*
173          * Bumping the ASID is probably cheaper than the flush ...
174          */
175         if (cpu_context(cpu, vma->vm_mm) != 0)
176                 drop_mmu_context(vma->vm_mm, cpu);
177 }
178
179 #ifdef CONFIG_SMP
180 struct flush_cache_page_args {
181         struct vm_area_struct *vma;
182         unsigned long addr;
183 };
184
185 static void sb1_flush_cache_page_ipi(void *info)
186 {
187         struct flush_cache_page_args *args = info;
188
189         local_sb1_flush_cache_page(args->vma, args->addr);
190 }
191
192 /* Dirty dcache could be on another CPU, so do the IPIs */
193 static void sb1_flush_cache_page(struct vm_area_struct *vma, unsigned long addr)
194 {
195         struct flush_cache_page_args args;
196
197         if (!(vma->vm_flags & VM_EXEC))
198                 return;
199
200         addr &= PAGE_MASK;
201         args.vma = vma;
202         args.addr = addr;
203         on_each_cpu(sb1_flush_cache_page_ipi, (void *) &args, 1, 1);
204 }
205 #else
206 void sb1_flush_cache_page(struct vm_area_struct *vma, unsigned long addr)
207         __attribute__((alias("local_sb1_flush_cache_page")));
208 #endif
209
210 /*
211  * Invalidate a range of the icache.  The addresses are virtual, and
212  * the cache is virtually indexed and tagged.  However, we don't
213  * necessarily have the right ASID context, so use index ops instead
214  * of hit ops.
215  */
216 static inline void __sb1_flush_icache_range(unsigned long start,
217         unsigned long end)
218 {
219         start &= ~(icache_line_size - 1);
220         end = (end + icache_line_size - 1) & ~(icache_line_size - 1);
221
222         while (start != end) {
223                 cache_set_op(Index_Invalidate_I, start & icache_index_mask);
224                 start += icache_line_size;
225         }
226         mispredict();
227         sync();
228 }
229
230
231 /*
232  * Invalidate all caches on this CPU
233  */
234 static void local_sb1___flush_cache_all(void)
235 {
236         __sb1_writeback_inv_dcache_all();
237         __sb1_flush_icache_all();
238 }
239
240 #ifdef CONFIG_SMP
241 void sb1___flush_cache_all_ipi(void *ignored)
242         __attribute__((alias("local_sb1___flush_cache_all")));
243
244 static void sb1___flush_cache_all(void)
245 {
246         on_each_cpu(sb1___flush_cache_all_ipi, 0, 1, 1);
247 }
248 #else
249 void sb1___flush_cache_all(void)
250         __attribute__((alias("local_sb1___flush_cache_all")));
251 #endif
252
253 /*
254  * When flushing a range in the icache, we have to first writeback
255  * the dcache for the same range, so new ifetches will see any
256  * data that was dirty in the dcache.
257  *
258  * The start/end arguments are Kseg addresses (possibly mapped Kseg).
259  */
260
261 static void local_sb1_flush_icache_range(unsigned long start,
262         unsigned long end)
263 {
264         /* Just wb-inv the whole dcache if the range is big enough */
265         if ((end - start) > dcache_range_cutoff)
266                 __sb1_writeback_inv_dcache_all();
267         else
268                 __sb1_writeback_inv_dcache_range(start, end);
269         
270         /* Just flush the whole icache if the range is big enough */
271         if ((end - start) > icache_range_cutoff)
272                 __sb1_flush_icache_all();
273         else
274                 __sb1_flush_icache_range(start, end);
275 }
276
277 #ifdef CONFIG_SMP
278 struct flush_icache_range_args {
279         unsigned long start;
280         unsigned long end;
281 };
282
283 static void sb1_flush_icache_range_ipi(void *info)
284 {
285         struct flush_icache_range_args *args = info;
286
287         local_sb1_flush_icache_range(args->start, args->end);
288 }
289
290 void sb1_flush_icache_range(unsigned long start, unsigned long end)
291 {
292         struct flush_icache_range_args args;
293
294         args.start = start;
295         args.end = end;
296         on_each_cpu(sb1_flush_icache_range_ipi, &args, 1, 1);
297 }
298 #else
299 void sb1_flush_icache_range(unsigned long start, unsigned long end)
300         __attribute__((alias("local_sb1_flush_icache_range")));
301 #endif
302
303 /*
304  * Flush the icache for a given physical page.  Need to writeback the
305  * dcache first, then invalidate the icache.  If the page isn't
306  * executable, nothing is required.
307  */
308 static void local_sb1_flush_icache_page(struct vm_area_struct *vma,
309         struct page *page)
310 {
311         unsigned long start;
312         int cpu = smp_processor_id();
313
314 #ifndef CONFIG_SMP
315         if (!(vma->vm_flags & VM_EXEC))
316                 return;
317 #endif
318
319         /* Need to writeback any dirty data for that page, we have the PA */
320         start = (unsigned long)(page-mem_map) << PAGE_SHIFT;
321         __sb1_writeback_inv_dcache_phys_range(start, start + PAGE_SIZE);
322         /*
323          * If there's a context, bump the ASID (cheaper than a flush,
324          * since we don't know VAs!)
325          */
326         if (cpu_context(cpu, vma->vm_mm) != 0) {
327                 drop_mmu_context(vma->vm_mm, cpu);
328         }
329 }
330
331 #ifdef CONFIG_SMP
332 struct flush_icache_page_args {
333         struct vm_area_struct *vma;
334         struct page *page;
335 };
336
337 static void sb1_flush_icache_page_ipi(void *info)
338 {
339         struct flush_icache_page_args *args = info;
340         local_sb1_flush_icache_page(args->vma, args->page);
341 }
342
343 /* Dirty dcache could be on another CPU, so do the IPIs */
344 static void sb1_flush_icache_page(struct vm_area_struct *vma,
345         struct page *page)
346 {
347         struct flush_icache_page_args args;
348
349         if (!(vma->vm_flags & VM_EXEC))
350                 return;
351         args.vma = vma;
352         args.page = page;
353         on_each_cpu(sb1_flush_icache_page_ipi, (void *) &args, 1, 1);
354 }
355 #else
356 void sb1_flush_icache_page(struct vm_area_struct *vma, struct page *page)
357         __attribute__((alias("local_sb1_flush_icache_page")));
358 #endif
359
360 /*
361  * A signal trampoline must fit into a single cacheline.
362  */
363 static void local_sb1_flush_cache_sigtramp(unsigned long addr)
364 {
365         cache_set_op(Index_Writeback_Inv_D, addr & dcache_index_mask);
366         cache_set_op(Index_Writeback_Inv_D, (addr ^ (1<<12)) & dcache_index_mask);
367         cache_set_op(Index_Invalidate_I, addr & icache_index_mask);
368         mispredict();
369 }
370
371 #ifdef CONFIG_SMP
372 static void sb1_flush_cache_sigtramp_ipi(void *info)
373 {
374         unsigned long iaddr = (unsigned long) info;
375         local_sb1_flush_cache_sigtramp(iaddr);
376 }
377
378 static void sb1_flush_cache_sigtramp(unsigned long addr)
379 {
380         on_each_cpu(sb1_flush_cache_sigtramp_ipi, (void *) addr, 1, 1);
381 }
382 #else
383 void sb1_flush_cache_sigtramp(unsigned long addr)
384         __attribute__((alias("local_sb1_flush_cache_sigtramp")));
385 #endif
386
387
388 /*
389  * Anything that just flushes dcache state can be ignored, as we're always
390  * coherent in dcache space.  This is just a dummy function that all the
391  * nop'ed routines point to
392  */
393 static void sb1_nop(void)
394 {
395 }
396
397 /*
398  *  Cache set values (from the mips64 spec)
399  * 0 - 64
400  * 1 - 128
401  * 2 - 256
402  * 3 - 512
403  * 4 - 1024
404  * 5 - 2048
405  * 6 - 4096
406  * 7 - Reserved
407  */
408
409 static unsigned int decode_cache_sets(unsigned int config_field)
410 {
411         if (config_field == 7) {
412                 /* JDCXXX - Find a graceful way to abort. */
413                 return 0;
414         }
415         return (1<<(config_field + 6));
416 }
417
418 /*
419  *  Cache line size values (from the mips64 spec)
420  * 0 - No cache present.
421  * 1 - 4 bytes
422  * 2 - 8 bytes
423  * 3 - 16 bytes
424  * 4 - 32 bytes
425  * 5 - 64 bytes
426  * 6 - 128 bytes
427  * 7 - Reserved
428  */
429
430 static unsigned int decode_cache_line_size(unsigned int config_field)
431 {
432         if (config_field == 0) {
433                 return 0;
434         } else if (config_field == 7) {
435                 /* JDCXXX - Find a graceful way to abort. */
436                 return 0;
437         }
438         return (1<<(config_field + 1));
439 }
440
441 /*
442  * Relevant bits of the config1 register format (from the MIPS32/MIPS64 specs)
443  *
444  * 24:22 Icache sets per way
445  * 21:19 Icache line size
446  * 18:16 Icache Associativity
447  * 15:13 Dcache sets per way
448  * 12:10 Dcache line size
449  * 9:7   Dcache Associativity
450  */
451
452 static __init void probe_cache_sizes(void)
453 {
454         u32 config1;
455
456         config1 = read_c0_config1();
457         icache_line_size = decode_cache_line_size((config1 >> 19) & 0x7);
458         dcache_line_size = decode_cache_line_size((config1 >> 10) & 0x7);
459         icache_sets = decode_cache_sets((config1 >> 22) & 0x7);
460         dcache_sets = decode_cache_sets((config1 >> 13) & 0x7);
461         icache_assoc = ((config1 >> 16) & 0x7) + 1;
462         dcache_assoc = ((config1 >> 7) & 0x7) + 1;
463         icache_size = icache_line_size * icache_sets * icache_assoc;
464         dcache_size = dcache_line_size * dcache_sets * dcache_assoc;
465         /* Need to remove non-index bits for index ops */
466         icache_index_mask = (icache_sets - 1) * icache_line_size;
467         dcache_index_mask = (dcache_sets - 1) * dcache_line_size;
468         /*
469          * These are for choosing range (index ops) versus all.
470          * icache flushes all ways for each set, so drop icache_assoc.
471          * dcache flushes all ways and each setting of bit 12 for each
472          * index, so drop dcache_assoc and halve the dcache_sets.
473          */
474         icache_range_cutoff = icache_sets * icache_line_size;
475         dcache_range_cutoff = (dcache_sets / 2) * icache_line_size;
476 }
477
478 /*
479  * This is called from loadmmu.c.  We have to set up all the
480  * memory management function pointers, as well as initialize
481  * the caches and tlbs
482  */
483 void ld_mmu_sb1(void)
484 {
485         extern char except_vec2_sb1;
486         extern char handle_vec2_sb1;
487
488         /* Special cache error handler for SB1 */
489         memcpy((void *)(CAC_BASE   + 0x100), &except_vec2_sb1, 0x80);
490         memcpy((void *)(UNCAC_BASE + 0x100), &except_vec2_sb1, 0x80);
491         memcpy((void *)KSEG1ADDR(&handle_vec2_sb1), &handle_vec2_sb1, 0x80);
492
493         probe_cache_sizes();
494
495 #ifdef CONFIG_SIBYTE_DMA_PAGEOPS
496         sb1_dma_init();
497 #endif
498
499         /*
500          * None of these are needed for the SB1 - the Dcache is
501          * physically indexed and tagged, so no virtual aliasing can
502          * occur
503          */
504         flush_cache_range = (void *) sb1_nop;
505         flush_cache_mm = (void (*)(struct mm_struct *))sb1_nop;
506         flush_cache_all = sb1_nop;
507
508         /* These routines are for Icache coherence with the Dcache */
509         flush_icache_range = sb1_flush_icache_range;
510         flush_icache_page = sb1_flush_icache_page;
511         flush_icache_all = __sb1_flush_icache_all; /* local only */
512
513         /* This implies an Icache flush too, so can't be nop'ed */
514         flush_cache_page = sb1_flush_cache_page;
515
516         flush_cache_sigtramp = sb1_flush_cache_sigtramp;
517         flush_data_cache_page = (void *) sb1_nop;
518
519         /* Full flush */
520         __flush_cache_all = sb1___flush_cache_all;
521
522         change_c0_config(CONF_CM_CMASK, CONF_CM_DEFAULT);
523
524         /*
525          * This is the only way to force the update of K0 to complete
526          * before subsequent instruction fetch.
527          */
528         __asm__ __volatile__(
529         "       .set    noat                    \n"
530         "       .set    noreorder               \n"
531         "       .set    mips3\n\t               \n"
532         "       la      $1, 1f                  \n"
533         "       mtc0    $1, $14                 \n"
534         "       eret                            \n"
535         "1:     .set    mips0\n\t               \n"
536         "       .set    at                      \n"
537         "       .set    reorder"
538         :
539         :
540         : "memory");
541
542         flush_cache_all();
543 }