ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / sh / mm / cache-sh4.c
1 /* $Id: cache-sh4.c,v 1.26 2004/02/19 12:47:24 lethal Exp $
2  *
3  *  linux/arch/sh/mm/cache-sh4.c
4  *
5  * Copyright (C) 1999, 2000, 2002  Niibe Yutaka
6  * Copyright (C) 2001, 2002, 2003, 2004  Paul Mundt
7  * Copyright (C) 2003  Richard Curnow
8  */
9
10 #include <linux/config.h>
11 #include <linux/init.h>
12 #include <linux/mman.h>
13 #include <linux/mm.h>
14 #include <linux/threads.h>
15 #include <asm/addrspace.h>
16 #include <asm/page.h>
17 #include <asm/pgtable.h>
18 #include <asm/processor.h>
19 #include <asm/cache.h>
20 #include <asm/io.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgalloc.h>
23 #include <asm/mmu_context.h>
24 #include <asm/cacheflush.h>
25
26 extern void __flush_cache_4096_all(unsigned long start);
27 static void __flush_cache_4096_all_ex(unsigned long start);
28 extern void __flush_dcache_all(void);
29 static void __flush_dcache_all_ex(void);
30
31 int __init detect_cpu_and_cache_system(void)
32 {
33         unsigned long pvr, prr, ccr, cvr;
34         unsigned long size;
35
36         static unsigned long sizes[16] = {
37                 [1] = (1 << 12),
38                 [2] = (1 << 13),
39                 [4] = (1 << 14),
40                 [8] = (1 << 15),
41                 [9] = (1 << 16)
42         };
43
44         pvr = (ctrl_inl(CCN_PVR) >> 8) & 0xffff;
45         prr = (ctrl_inl(CCN_PRR) >> 4) & 0xff;
46         cvr = (ctrl_inl(CCN_CVR));
47
48         /*
49          * Setup some sane SH-4 defaults for the icache
50          */
51         cpu_data->icache.way_shift      = 13;
52         cpu_data->icache.entry_shift    = 5;
53         cpu_data->icache.entry_mask     = 0x1fe0;
54         cpu_data->icache.sets           = 256;
55         cpu_data->icache.ways           = 1;
56         cpu_data->icache.linesz         = L1_CACHE_BYTES;
57
58         /*
59          * And again for the dcache ..
60          */
61         cpu_data->dcache.way_shift      = 14;
62         cpu_data->dcache.entry_shift    = 5;
63         cpu_data->dcache.entry_mask     = 0x3fe0;
64         cpu_data->dcache.sets           = 512;
65         cpu_data->dcache.ways           = 1;
66         cpu_data->dcache.linesz         = L1_CACHE_BYTES;
67
68         /* Set the FPU flag, virtually all SH-4's have one */
69         set_bit(CPU_HAS_FPU, &(cpu_data->flags));
70
71         /*
72          * Probe the underlying processor version/revision and
73          * adjust cpu_data setup accordingly.
74          */
75         switch (pvr) {
76         case 0x205:
77                 cpu_data->type = CPU_SH7750;
78                 set_bit(CPU_HAS_P2_FLUSH_BUG, &(cpu_data->flags));
79                 break;
80         case 0x206:
81                 cpu_data->type = CPU_SH7750S;
82
83                 /* 
84                  * FIXME: This is needed for 7750, but do we need it for the
85                  * 7750S too? For now, assume we do.. -- PFM
86                  */
87                 set_bit(CPU_HAS_P2_FLUSH_BUG, &(cpu_data->flags));
88
89                 break;
90         case 0x1100:
91                 cpu_data->type = CPU_SH7751;
92                 break;
93         case 0x8000:
94                 cpu_data->type = CPU_ST40RA;
95                 break;
96         case 0x8100:
97                 cpu_data->type = CPU_ST40GX1;
98                 break;
99         case 0x700:
100                 cpu_data->type = CPU_SH4_501;
101                 cpu_data->icache.ways = 2;
102                 cpu_data->dcache.ways = 2;
103
104                 /* No FPU on the SH4-500 series.. */
105                 clear_bit(CPU_HAS_FPU, &(cpu_data->flags));
106                 break;
107         case 0x600:
108                 cpu_data->type = CPU_SH4_202;
109                 cpu_data->icache.ways = 2;
110                 cpu_data->dcache.ways = 2;
111                 break;
112         case 0x500 ... 0x501:
113                 switch (prr) {
114                     case 0x10: cpu_data->type = CPU_SH7750R; break;
115                     case 0x11: cpu_data->type = CPU_SH7751R; break;
116                     case 0x50: cpu_data->type = CPU_SH7760;  break;
117                 }
118
119                 cpu_data->icache.ways = 2;
120                 cpu_data->dcache.ways = 2;
121
122                 break;
123         default:
124                 cpu_data->type = CPU_SH_NONE;
125                 break;
126         }
127
128         /*
129          * On anything that's not a direct-mapped cache, look to the CVR
130          * for I/D-cache specifics.
131          */
132         if (cpu_data->dcache.ways > 1) {
133                 jump_to_P2();
134                 ccr = ctrl_inl(CCR);
135
136                 /* Force EMODE */
137                 if (!(ccr & CCR_CACHE_EMODE)) {
138                         ccr |= CCR_CACHE_EMODE;
139                         ctrl_outl(ccr, CCR);
140                 }
141
142                 back_to_P1();
143
144                 size = sizes[(cvr >> 20) & 0xf];
145                 cpu_data->icache.way_shift      =  (size >> 1);
146                 cpu_data->icache.entry_mask     = ((size >> 2) - (1 << 5));
147                 cpu_data->icache.sets           =  (size >> 6);
148
149                 size = sizes[(cvr >> 16) & 0xf];
150                 cpu_data->dcache.way_shift      =  (size >> 1);
151                 cpu_data->dcache.entry_mask     = ((size >> 2) - (1 << 5));
152                 cpu_data->dcache.sets           =  (size >> 6);
153         }
154
155         return 0;
156 }
157
158 /*
159  * SH-4 has virtually indexed and physically tagged cache.
160  */
161
162 struct semaphore p3map_sem[4];
163
164 void __init p3_cache_init(void)
165 {
166         if (remap_area_pages(P3SEG, 0, PAGE_SIZE*4, _PAGE_CACHABLE))
167                 panic("%s failed.", __FUNCTION__);
168
169         sema_init (&p3map_sem[0], 1);
170         sema_init (&p3map_sem[1], 1);
171         sema_init (&p3map_sem[2], 1);
172         sema_init (&p3map_sem[3], 1);
173 }
174
175 /*
176  * Write back the dirty D-caches, but not invalidate them.
177  *
178  * START: Virtual Address (U0, P1, or P3)
179  * SIZE: Size of the region.
180  */
181 void __flush_wback_region(void *start, int size)
182 {
183         unsigned long v;
184         unsigned long begin, end;
185
186         begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
187         end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
188                 & ~(L1_CACHE_BYTES-1);
189         for (v = begin; v < end; v+=L1_CACHE_BYTES) {
190                 asm volatile("ocbwb     %0"
191                              : /* no output */
192                              : "m" (__m(v)));
193         }
194 }
195
196 /*
197  * Write back the dirty D-caches and invalidate them.
198  *
199  * START: Virtual Address (U0, P1, or P3)
200  * SIZE: Size of the region.
201  */
202 void __flush_purge_region(void *start, int size)
203 {
204         unsigned long v;
205         unsigned long begin, end;
206
207         begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
208         end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
209                 & ~(L1_CACHE_BYTES-1);
210         for (v = begin; v < end; v+=L1_CACHE_BYTES) {
211                 asm volatile("ocbp      %0"
212                              : /* no output */
213                              : "m" (__m(v)));
214         }
215 }
216
217
218 /*
219  * No write back please
220  */
221 void __flush_invalidate_region(void *start, int size)
222 {
223         unsigned long v;
224         unsigned long begin, end;
225
226         begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
227         end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
228                 & ~(L1_CACHE_BYTES-1);
229         for (v = begin; v < end; v+=L1_CACHE_BYTES) {
230                 asm volatile("ocbi      %0"
231                              : /* no output */
232                              : "m" (__m(v)));
233         }
234 }
235
236 static void __flush_dcache_all_ex(void)
237 {
238         unsigned long addr, end_addr, entry_offset;
239
240         end_addr = CACHE_OC_ADDRESS_ARRAY + (cpu_data->dcache.sets << cpu_data->dcache.entry_shift) * cpu_data->dcache.ways;
241         entry_offset = 1 << cpu_data->dcache.entry_shift;
242         for (addr = CACHE_OC_ADDRESS_ARRAY; addr < end_addr; addr += entry_offset) {
243                 ctrl_outl(0, addr);
244         }
245 }
246
247 static void __flush_cache_4096_all_ex(unsigned long start)
248 {
249         unsigned long addr, entry_offset;
250         int i;
251
252         entry_offset = 1 << cpu_data->dcache.entry_shift;
253         for (i = 0; i < cpu_data->dcache.ways; i++, start += (1 << cpu_data->dcache.way_shift)) {
254                 for (addr = CACHE_OC_ADDRESS_ARRAY + start;
255                      addr < CACHE_OC_ADDRESS_ARRAY + 4096 + start;
256                      addr += entry_offset) {
257                         ctrl_outl(0, addr);
258                 }
259         }
260 }
261
262 void flush_cache_4096_all(unsigned long start)
263 {
264         if (cpu_data->dcache.ways == 1)
265                 __flush_cache_4096_all(start);
266         else
267                 __flush_cache_4096_all_ex(start);
268 }
269
270 /*
271  * Write back the range of D-cache, and purge the I-cache.
272  *
273  * Called from kernel/module.c:sys_init_module and routine for a.out format.
274  */
275 void flush_icache_range(unsigned long start, unsigned long end)
276 {
277         flush_cache_all();
278 }
279
280 /*
281  * Write back the D-cache and purge the I-cache for signal trampoline. 
282  * .. which happens to be the same behavior as flush_icache_range().
283  * So, we simply flush out a line.
284  */
285 void flush_cache_sigtramp(unsigned long addr)
286 {
287         unsigned long v, index;
288         unsigned long flags; 
289         int i;
290
291         v = addr & ~(L1_CACHE_BYTES-1);
292         asm volatile("ocbwb     %0"
293                      : /* no output */
294                      : "m" (__m(v)));
295
296         index = CACHE_IC_ADDRESS_ARRAY | (v & cpu_data->icache.entry_mask);
297
298         local_irq_save(flags);
299         jump_to_P2();
300         for(i = 0; i < cpu_data->icache.ways; i++, index += (1 << cpu_data->icache.way_shift))
301                 ctrl_outl(0, index);    /* Clear out Valid-bit */
302         back_to_P1();
303         local_irq_restore(flags);
304 }
305
306 static inline void flush_cache_4096(unsigned long start,
307                                     unsigned long phys)
308 {
309         unsigned long flags; 
310         extern void __flush_cache_4096(unsigned long addr, unsigned long phys, unsigned long exec_offset);
311
312         /*
313          * SH7751, SH7751R, and ST40 have no restriction to handle cache.
314          * (While SH7750 must do that at P2 area.)
315          */
316         if (test_bit(CPU_HAS_P2_FLUSH_BUG, &(cpu_data->flags))) {
317                 local_irq_save(flags);
318                 __flush_cache_4096(start | SH_CACHE_ASSOC, phys | 0x80000000, 0x20000000);
319                 local_irq_restore(flags);
320         } else if (start >= CACHE_OC_ADDRESS_ARRAY) {
321                 __flush_cache_4096(start | SH_CACHE_ASSOC, phys | 0x80000000, 0);
322         }
323 }
324
325 /*
326  * Write back & invalidate the D-cache of the page.
327  * (To avoid "alias" issues)
328  */
329 void flush_dcache_page(struct page *page)
330 {
331         if (test_bit(PG_mapped, &page->flags)) {
332                 unsigned long phys = PHYSADDR(page_address(page));
333
334                 /* Loop all the D-cache */
335                 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY,          phys);
336                 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY | 0x1000, phys);
337                 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY | 0x2000, phys);
338                 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY | 0x3000, phys);
339         }
340 }
341
342 static inline void flush_icache_all(void)
343 {
344         unsigned long flags, ccr;
345
346         local_irq_save(flags);
347         jump_to_P2();
348
349         /* Flush I-cache */
350         ccr = ctrl_inl(CCR);
351         ccr |= CCR_CACHE_ICI;
352         ctrl_outl(ccr, CCR);
353
354         back_to_P1();
355         local_irq_restore(flags);
356 }
357
358 void flush_cache_all(void)
359 {
360         if (cpu_data->dcache.ways == 1)
361                 __flush_dcache_all();
362         else
363                 __flush_dcache_all_ex();
364         flush_icache_all();
365 }
366
367 void flush_cache_mm(struct mm_struct *mm)
368 {
369         /* Is there any good way? */
370         /* XXX: possibly call flush_cache_range for each vm area */
371         /* 
372          * FIXME: Really, the optimal solution here would be able to flush out
373          * individual lines created by the specified context, but this isn't
374          * feasible for a number of architectures (such as MIPS, and some
375          * SPARC) .. is this possible for SuperH?
376          *
377          * In the meantime, we'll just flush all of the caches.. this
378          * seems to be the simplest way to avoid at least a few wasted
379          * cache flushes. -Lethal
380          */
381         flush_cache_all();
382 }
383
384 static void __flush_cache_page(struct vm_area_struct *vma,
385                                unsigned long address,
386                                unsigned long phys)
387 {
388         /* We only need to flush D-cache when we have alias */
389         if ((address^phys) & CACHE_ALIAS) {
390                 /* Loop 4K of the D-cache */
391                 flush_cache_4096(
392                         CACHE_OC_ADDRESS_ARRAY | (address & CACHE_ALIAS),
393                         phys);
394                 /* Loop another 4K of the D-cache */
395                 flush_cache_4096(
396                         CACHE_OC_ADDRESS_ARRAY | (phys & CACHE_ALIAS),
397                         phys);
398         }
399
400         if (vma->vm_flags & VM_EXEC)
401                 /* Loop 4K (half) of the I-cache */
402                 flush_cache_4096(
403                         CACHE_IC_ADDRESS_ARRAY | (address & 0x1000),
404                         phys);
405 }
406
407 /*
408  * Write back and invalidate D-caches.
409  *
410  * START, END: Virtual Address (U0 address)
411  *
412  * NOTE: We need to flush the _physical_ page entry.
413  * Flushing the cache lines for U0 only isn't enough.
414  * We need to flush for P1 too, which may contain aliases.
415  */
416 void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
417                        unsigned long end)
418 {
419         unsigned long p = start & PAGE_MASK;
420         pgd_t *dir;
421         pmd_t *pmd;
422         pte_t *pte;
423         pte_t entry;
424         unsigned long phys;
425         unsigned long d = 0;
426
427         dir = pgd_offset(vma->vm_mm, p);
428         pmd = pmd_offset(dir, p);
429
430         do {
431                 if (pmd_none(*pmd) || pmd_bad(*pmd)) {
432                         p &= ~((1 << PMD_SHIFT) -1);
433                         p += (1 << PMD_SHIFT);
434                         pmd++;
435                         continue;
436                 }
437                 pte = pte_offset_kernel(pmd, p);
438                 do {
439                         entry = *pte;
440                         if ((pte_val(entry) & _PAGE_PRESENT)) {
441                                 phys = pte_val(entry)&PTE_PHYS_MASK;
442                                 if ((p^phys) & CACHE_ALIAS) {
443                                         d |= 1 << ((p & CACHE_ALIAS)>>12); 
444                                         d |= 1 << ((phys & CACHE_ALIAS)>>12);
445                                         if (d == 0x0f)
446                                                 goto loop_exit;
447                                 }
448                         }
449                         pte++;
450                         p += PAGE_SIZE;
451                 } while (p < end && ((unsigned long)pte & ~PAGE_MASK));
452                 pmd++;
453         } while (p < end);
454  loop_exit:
455         if (d & 1)
456                 flush_cache_4096_all(0);
457         if (d & 2)
458                 flush_cache_4096_all(0x1000);
459         if (d & 4)
460                 flush_cache_4096_all(0x2000);
461         if (d & 8)
462                 flush_cache_4096_all(0x3000);
463         if (vma->vm_flags & VM_EXEC)
464                 flush_icache_all();
465 }
466
467 /*
468  * Write back and invalidate I/D-caches for the page.
469  *
470  * ADDR: Virtual Address (U0 address)
471  */
472 void flush_cache_page(struct vm_area_struct *vma, unsigned long address)
473 {
474         pgd_t *dir;
475         pmd_t *pmd;
476         pte_t *pte;
477         pte_t entry;
478         unsigned long phys;
479
480         dir = pgd_offset(vma->vm_mm, address);
481         pmd = pmd_offset(dir, address);
482         if (pmd_none(*pmd) || pmd_bad(*pmd))
483                 return;
484         pte = pte_offset_kernel(pmd, address);
485         entry = *pte;
486         if (!(pte_val(entry) & _PAGE_PRESENT))
487                 return;
488
489         phys = pte_val(entry)&PTE_PHYS_MASK;
490         __flush_cache_page(vma, address, phys);
491 }
492
493 /*
494  * flush_icache_user_range
495  * @vma: VMA of the process
496  * @page: page
497  * @addr: U0 address
498  * @len: length of the range (< page size)
499  */
500 void flush_icache_user_range(struct vm_area_struct *vma,
501                              struct page *page, unsigned long addr, int len)
502 {
503         __flush_cache_page(vma, addr, PHYSADDR(page_address(page)));
504 }
505