2 * arch/sh/mm/cache-sh4.c
4 * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
5 * Copyright (C) 2001, 2002, 2003, 2004 Paul Mundt
6 * Copyright (C) 2003 Richard Curnow
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
13 #include <linux/config.h>
14 #include <linux/init.h>
15 #include <linux/mman.h>
17 #include <linux/threads.h>
18 #include <asm/addrspace.h>
20 #include <asm/pgtable.h>
21 #include <asm/processor.h>
22 #include <asm/cache.h>
24 #include <asm/uaccess.h>
25 #include <asm/pgalloc.h>
26 #include <asm/mmu_context.h>
27 #include <asm/cacheflush.h>
29 extern void __flush_cache_4096_all(unsigned long start
);
30 static void __flush_cache_4096_all_ex(unsigned long start
);
31 extern void __flush_dcache_all(void);
32 static void __flush_dcache_all_ex(void);
35 * SH-4 has virtually indexed and physically tagged cache.
38 struct semaphore p3map_sem
[4];
40 void __init
p3_cache_init(void)
42 if (remap_area_pages(P3SEG
, 0, PAGE_SIZE
*4, _PAGE_CACHABLE
))
43 panic("%s failed.", __FUNCTION__
);
45 sema_init (&p3map_sem
[0], 1);
46 sema_init (&p3map_sem
[1], 1);
47 sema_init (&p3map_sem
[2], 1);
48 sema_init (&p3map_sem
[3], 1);
52 * Write back the dirty D-caches, but not invalidate them.
54 * START: Virtual Address (U0, P1, or P3)
55 * SIZE: Size of the region.
57 void __flush_wback_region(void *start
, int size
)
60 unsigned long begin
, end
;
62 begin
= (unsigned long)start
& ~(L1_CACHE_BYTES
-1);
63 end
= ((unsigned long)start
+ size
+ L1_CACHE_BYTES
-1)
64 & ~(L1_CACHE_BYTES
-1);
65 for (v
= begin
; v
< end
; v
+=L1_CACHE_BYTES
) {
66 asm volatile("ocbwb %0"
73 * Write back the dirty D-caches and invalidate them.
75 * START: Virtual Address (U0, P1, or P3)
76 * SIZE: Size of the region.
78 void __flush_purge_region(void *start
, int size
)
81 unsigned long begin
, end
;
83 begin
= (unsigned long)start
& ~(L1_CACHE_BYTES
-1);
84 end
= ((unsigned long)start
+ size
+ L1_CACHE_BYTES
-1)
85 & ~(L1_CACHE_BYTES
-1);
86 for (v
= begin
; v
< end
; v
+=L1_CACHE_BYTES
) {
87 asm volatile("ocbp %0"
95 * No write back please
97 void __flush_invalidate_region(void *start
, int size
)
100 unsigned long begin
, end
;
102 begin
= (unsigned long)start
& ~(L1_CACHE_BYTES
-1);
103 end
= ((unsigned long)start
+ size
+ L1_CACHE_BYTES
-1)
104 & ~(L1_CACHE_BYTES
-1);
105 for (v
= begin
; v
< end
; v
+=L1_CACHE_BYTES
) {
106 asm volatile("ocbi %0"
112 static void __flush_dcache_all_ex(void)
114 unsigned long addr
, end_addr
, entry_offset
;
116 end_addr
= CACHE_OC_ADDRESS_ARRAY
+ (cpu_data
->dcache
.sets
<< cpu_data
->dcache
.entry_shift
) * cpu_data
->dcache
.ways
;
117 entry_offset
= 1 << cpu_data
->dcache
.entry_shift
;
118 for (addr
= CACHE_OC_ADDRESS_ARRAY
; addr
< end_addr
; addr
+= entry_offset
) {
123 static void __flush_cache_4096_all_ex(unsigned long start
)
125 unsigned long addr
, entry_offset
;
128 entry_offset
= 1 << cpu_data
->dcache
.entry_shift
;
129 for (i
= 0; i
< cpu_data
->dcache
.ways
; i
++, start
+= cpu_data
->dcache
.way_incr
) {
130 for (addr
= CACHE_OC_ADDRESS_ARRAY
+ start
;
131 addr
< CACHE_OC_ADDRESS_ARRAY
+ 4096 + start
;
132 addr
+= entry_offset
) {
138 void flush_cache_4096_all(unsigned long start
)
140 if (cpu_data
->dcache
.ways
== 1)
141 __flush_cache_4096_all(start
);
143 __flush_cache_4096_all_ex(start
);
147 * Write back the range of D-cache, and purge the I-cache.
149 * Called from kernel/module.c:sys_init_module and routine for a.out format.
151 void flush_icache_range(unsigned long start
, unsigned long end
)
157 * Write back the D-cache and purge the I-cache for signal trampoline.
158 * .. which happens to be the same behavior as flush_icache_range().
159 * So, we simply flush out a line.
161 void flush_cache_sigtramp(unsigned long addr
)
163 unsigned long v
, index
;
167 v
= addr
& ~(L1_CACHE_BYTES
-1);
168 asm volatile("ocbwb %0"
172 index
= CACHE_IC_ADDRESS_ARRAY
| (v
& cpu_data
->icache
.entry_mask
);
174 local_irq_save(flags
);
176 for(i
= 0; i
< cpu_data
->icache
.ways
; i
++, index
+= cpu_data
->icache
.way_incr
)
177 ctrl_outl(0, index
); /* Clear out Valid-bit */
179 local_irq_restore(flags
);
182 static inline void flush_cache_4096(unsigned long start
,
186 extern void __flush_cache_4096(unsigned long addr
, unsigned long phys
, unsigned long exec_offset
);
189 * SH7751, SH7751R, and ST40 have no restriction to handle cache.
190 * (While SH7750 must do that at P2 area.)
192 if ((cpu_data
->flags
& CPU_HAS_P2_FLUSH_BUG
)
193 || start
< CACHE_OC_ADDRESS_ARRAY
) {
194 local_irq_save(flags
);
195 __flush_cache_4096(start
| SH_CACHE_ASSOC
, P1SEGADDR(phys
), 0x20000000);
196 local_irq_restore(flags
);
198 __flush_cache_4096(start
| SH_CACHE_ASSOC
, P1SEGADDR(phys
), 0);
203 * Write back & invalidate the D-cache of the page.
204 * (To avoid "alias" issues)
206 void flush_dcache_page(struct page
*page
)
208 if (test_bit(PG_mapped
, &page
->flags
)) {
209 unsigned long phys
= PHYSADDR(page_address(page
));
211 /* Loop all the D-cache */
212 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY
, phys
);
213 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY
| 0x1000, phys
);
214 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY
| 0x2000, phys
);
215 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY
| 0x3000, phys
);
219 static inline void flush_icache_all(void)
221 unsigned long flags
, ccr
;
223 local_irq_save(flags
);
228 ccr
|= CCR_CACHE_ICI
;
232 local_irq_restore(flags
);
235 void flush_cache_all(void)
237 if (cpu_data
->dcache
.ways
== 1)
238 __flush_dcache_all();
240 __flush_dcache_all_ex();
244 void flush_cache_mm(struct mm_struct
*mm
)
246 /* Is there any good way? */
247 /* XXX: possibly call flush_cache_range for each vm area */
249 * FIXME: Really, the optimal solution here would be able to flush out
250 * individual lines created by the specified context, but this isn't
251 * feasible for a number of architectures (such as MIPS, and some
252 * SPARC) .. is this possible for SuperH?
254 * In the meantime, we'll just flush all of the caches.. this
255 * seems to be the simplest way to avoid at least a few wasted
256 * cache flushes. -Lethal
262 * Write back and invalidate I/D-caches for the page.
264 * ADDR: Virtual Address (U0 address)
265 * PFN: Physical page number
267 void flush_cache_page(struct vm_area_struct
*vma
, unsigned long address
, unsigned long pfn
)
269 unsigned long phys
= pfn
<< PAGE_SHIFT
;
271 /* We only need to flush D-cache when we have alias */
272 if ((address
^phys
) & CACHE_ALIAS
) {
273 /* Loop 4K of the D-cache */
275 CACHE_OC_ADDRESS_ARRAY
| (address
& CACHE_ALIAS
),
277 /* Loop another 4K of the D-cache */
279 CACHE_OC_ADDRESS_ARRAY
| (phys
& CACHE_ALIAS
),
283 if (vma
->vm_flags
& VM_EXEC
)
284 /* Loop 4K (half) of the I-cache */
286 CACHE_IC_ADDRESS_ARRAY
| (address
& 0x1000),
291 * Write back and invalidate D-caches.
293 * START, END: Virtual Address (U0 address)
295 * NOTE: We need to flush the _physical_ page entry.
296 * Flushing the cache lines for U0 only isn't enough.
297 * We need to flush for P1 too, which may contain aliases.
299 void flush_cache_range(struct vm_area_struct
*vma
, unsigned long start
,
302 unsigned long p
= start
& PAGE_MASK
;
310 dir
= pgd_offset(vma
->vm_mm
, p
);
311 pmd
= pmd_offset(dir
, p
);
314 if (pmd_none(*pmd
) || pmd_bad(*pmd
)) {
315 p
&= ~((1 << PMD_SHIFT
) -1);
316 p
+= (1 << PMD_SHIFT
);
320 pte
= pte_offset_kernel(pmd
, p
);
323 if ((pte_val(entry
) & _PAGE_PRESENT
)) {
324 phys
= pte_val(entry
)&PTE_PHYS_MASK
;
325 if ((p
^phys
) & CACHE_ALIAS
) {
326 d
|= 1 << ((p
& CACHE_ALIAS
)>>12);
327 d
|= 1 << ((phys
& CACHE_ALIAS
)>>12);
334 } while (p
< end
&& ((unsigned long)pte
& ~PAGE_MASK
));
339 flush_cache_4096_all(0);
341 flush_cache_4096_all(0x1000);
343 flush_cache_4096_all(0x2000);
345 flush_cache_4096_all(0x3000);
346 if (vma
->vm_flags
& VM_EXEC
)
351 * flush_icache_user_range
352 * @vma: VMA of the process
355 * @len: length of the range (< page size)
357 void flush_icache_user_range(struct vm_area_struct
*vma
,
358 struct page
*page
, unsigned long addr
, int len
)
360 flush_cache_page(vma
, addr
, page_to_pfn(page
));