1 /* $Id: cache-sh4.c,v 1.26 2004/02/19 12:47:24 lethal Exp $
3 * linux/arch/sh/mm/cache-sh4.c
5 * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
6 * Copyright (C) 2001, 2002, 2003, 2004 Paul Mundt
7 * Copyright (C) 2003 Richard Curnow
10 #include <linux/config.h>
11 #include <linux/init.h>
12 #include <linux/mman.h>
14 #include <linux/threads.h>
15 #include <asm/addrspace.h>
17 #include <asm/pgtable.h>
18 #include <asm/processor.h>
19 #include <asm/cache.h>
21 #include <asm/uaccess.h>
22 #include <asm/pgalloc.h>
23 #include <asm/mmu_context.h>
24 #include <asm/cacheflush.h>
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);
31 int __init detect_cpu_and_cache_system(void)
33 unsigned long pvr, prr, ccr, cvr;
36 static unsigned long sizes[16] = {
44 pvr = (ctrl_inl(CCN_PVR) >> 8) & 0xffff;
45 prr = (ctrl_inl(CCN_PRR) >> 4) & 0xff;
46 cvr = (ctrl_inl(CCN_CVR));
49 * Setup some sane SH-4 defaults for the icache
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;
59 * And again for the dcache ..
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;
68 /* Set the FPU flag, virtually all SH-4's have one */
69 set_bit(CPU_HAS_FPU, &(cpu_data->flags));
72 * Probe the underlying processor version/revision and
73 * adjust cpu_data setup accordingly.
77 cpu_data->type = CPU_SH7750;
78 set_bit(CPU_HAS_P2_FLUSH_BUG, &(cpu_data->flags));
81 cpu_data->type = CPU_SH7750S;
84 * FIXME: This is needed for 7750, but do we need it for the
85 * 7750S too? For now, assume we do.. -- PFM
87 set_bit(CPU_HAS_P2_FLUSH_BUG, &(cpu_data->flags));
91 cpu_data->type = CPU_SH7751;
94 cpu_data->type = CPU_ST40RA;
97 cpu_data->type = CPU_ST40GX1;
100 cpu_data->type = CPU_SH4_501;
101 cpu_data->icache.ways = 2;
102 cpu_data->dcache.ways = 2;
104 /* No FPU on the SH4-500 series.. */
105 clear_bit(CPU_HAS_FPU, &(cpu_data->flags));
108 cpu_data->type = CPU_SH4_202;
109 cpu_data->icache.ways = 2;
110 cpu_data->dcache.ways = 2;
112 case 0x500 ... 0x501:
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;
119 cpu_data->icache.ways = 2;
120 cpu_data->dcache.ways = 2;
124 cpu_data->type = CPU_SH_NONE;
129 * On anything that's not a direct-mapped cache, look to the CVR
130 * for I/D-cache specifics.
132 if (cpu_data->dcache.ways > 1) {
137 if (!(ccr & CCR_CACHE_EMODE)) {
138 ccr |= CCR_CACHE_EMODE;
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);
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);
159 * SH-4 has virtually indexed and physically tagged cache.
162 struct semaphore p3map_sem[4];
164 void __init p3_cache_init(void)
166 if (remap_area_pages(P3SEG, 0, PAGE_SIZE*4, _PAGE_CACHABLE))
167 panic("%s failed.", __FUNCTION__);
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);
176 * Write back the dirty D-caches, but not invalidate them.
178 * START: Virtual Address (U0, P1, or P3)
179 * SIZE: Size of the region.
181 void __flush_wback_region(void *start, int size)
184 unsigned long begin, end;
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"
197 * Write back the dirty D-caches and invalidate them.
199 * START: Virtual Address (U0, P1, or P3)
200 * SIZE: Size of the region.
202 void __flush_purge_region(void *start, int size)
205 unsigned long begin, end;
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"
219 * No write back please
221 void __flush_invalidate_region(void *start, int size)
224 unsigned long begin, end;
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"
236 static void __flush_dcache_all_ex(void)
238 unsigned long addr, end_addr, entry_offset;
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) {
247 static void __flush_cache_4096_all_ex(unsigned long start)
249 unsigned long addr, entry_offset;
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) {
262 void flush_cache_4096_all(unsigned long start)
264 if (cpu_data->dcache.ways == 1)
265 __flush_cache_4096_all(start);
267 __flush_cache_4096_all_ex(start);
271 * Write back the range of D-cache, and purge the I-cache.
273 * Called from kernel/module.c:sys_init_module and routine for a.out format.
275 void flush_icache_range(unsigned long start, unsigned long end)
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.
285 void flush_cache_sigtramp(unsigned long addr)
287 unsigned long v, index;
291 v = addr & ~(L1_CACHE_BYTES-1);
292 asm volatile("ocbwb %0"
296 index = CACHE_IC_ADDRESS_ARRAY | (v & cpu_data->icache.entry_mask);
298 local_irq_save(flags);
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 */
303 local_irq_restore(flags);
306 static inline void flush_cache_4096(unsigned long start,
310 extern void __flush_cache_4096(unsigned long addr, unsigned long phys, unsigned long exec_offset);
313 * SH7751, SH7751R, and ST40 have no restriction to handle cache.
314 * (While SH7750 must do that at P2 area.)
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);
326 * Write back & invalidate the D-cache of the page.
327 * (To avoid "alias" issues)
329 void flush_dcache_page(struct page *page)
331 if (test_bit(PG_mapped, &page->flags)) {
332 unsigned long phys = PHYSADDR(page_address(page));
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);
342 static inline void flush_icache_all(void)
344 unsigned long flags, ccr;
346 local_irq_save(flags);
351 ccr |= CCR_CACHE_ICI;
355 local_irq_restore(flags);
358 void flush_cache_all(void)
360 if (cpu_data->dcache.ways == 1)
361 __flush_dcache_all();
363 __flush_dcache_all_ex();
367 void flush_cache_mm(struct mm_struct *mm)
369 /* Is there any good way? */
370 /* XXX: possibly call flush_cache_range for each vm area */
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?
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
384 static void __flush_cache_page(struct vm_area_struct *vma,
385 unsigned long address,
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 */
392 CACHE_OC_ADDRESS_ARRAY | (address & CACHE_ALIAS),
394 /* Loop another 4K of the D-cache */
396 CACHE_OC_ADDRESS_ARRAY | (phys & CACHE_ALIAS),
400 if (vma->vm_flags & VM_EXEC)
401 /* Loop 4K (half) of the I-cache */
403 CACHE_IC_ADDRESS_ARRAY | (address & 0x1000),
408 * Write back and invalidate D-caches.
410 * START, END: Virtual Address (U0 address)
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.
416 void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
419 unsigned long p = start & PAGE_MASK;
427 dir = pgd_offset(vma->vm_mm, p);
428 pmd = pmd_offset(dir, p);
431 if (pmd_none(*pmd) || pmd_bad(*pmd)) {
432 p &= ~((1 << PMD_SHIFT) -1);
433 p += (1 << PMD_SHIFT);
437 pte = pte_offset_kernel(pmd, p);
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);
451 } while (p < end && ((unsigned long)pte & ~PAGE_MASK));
456 flush_cache_4096_all(0);
458 flush_cache_4096_all(0x1000);
460 flush_cache_4096_all(0x2000);
462 flush_cache_4096_all(0x3000);
463 if (vma->vm_flags & VM_EXEC)
468 * Write back and invalidate I/D-caches for the page.
470 * ADDR: Virtual Address (U0 address)
472 void flush_cache_page(struct vm_area_struct *vma, unsigned long address)
480 dir = pgd_offset(vma->vm_mm, address);
481 pmd = pmd_offset(dir, address);
482 if (pmd_none(*pmd) || pmd_bad(*pmd))
484 pte = pte_offset_kernel(pmd, address);
486 if (!(pte_val(entry) & _PAGE_PRESENT))
489 phys = pte_val(entry)&PTE_PHYS_MASK;
490 __flush_cache_page(vma, address, phys);
494 * flush_icache_user_range
495 * @vma: VMA of the process
498 * @len: length of the range (< page size)
500 void flush_icache_user_range(struct vm_area_struct *vma,
501 struct page *page, unsigned long addr, int len)
503 __flush_cache_page(vma, addr, PHYSADDR(page_address(page)));