2 * arch/sh/mm/cache-sh4.c
4 * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
5 * Copyright (C) 2001 - 2009 Paul Mundt
6 * Copyright (C) 2003 Richard Curnow
7 * Copyright (c) 2007 STMicroelectronics (R&D) Ltd.
9 * This file is subject to the terms and conditions of the GNU General Public
10 * License. See the file "COPYING" in the main directory of this archive
13 #include <linux/init.h>
16 #include <linux/mutex.h>
18 #include <linux/highmem.h>
19 #include <asm/pgtable.h>
20 #include <asm/mmu_context.h>
21 #include <asm/cacheflush.h>
24 * The maximum number of pages we support up to when doing ranged dcache
25 * flushing. Anything exceeding this will simply flush the dcache in its
28 #define MAX_ICACHE_PAGES 32
30 static void __flush_cache_4096(unsigned long addr
, unsigned long phys
,
31 unsigned long exec_offset
);
34 * Write back the range of D-cache, and purge the I-cache.
36 * Called from kernel/module.c:sys_init_module and routine for a.out format,
37 * signal handler code and kprobes code
39 static void __uses_jump_to_uncached
sh4_flush_icache_range(void *args
)
41 struct flusher_data
*data
= args
;
42 unsigned long start
, end
;
43 unsigned long flags
, v
;
49 /* If there are too many pages then just blow away the caches */
50 if (((end
- start
) >> PAGE_SHIFT
) >= MAX_ICACHE_PAGES
) {
51 local_flush_cache_all(NULL
);
56 * Selectively flush d-cache then invalidate the i-cache.
57 * This is inefficient, so only use this for small ranges.
59 start
&= ~(L1_CACHE_BYTES
-1);
60 end
+= L1_CACHE_BYTES
-1;
61 end
&= ~(L1_CACHE_BYTES
-1);
63 local_irq_save(flags
);
66 for (v
= start
; v
< end
; v
+= L1_CACHE_BYTES
) {
67 unsigned long icacheaddr
;
71 icacheaddr
= CACHE_IC_ADDRESS_ARRAY
| (v
&
72 cpu_data
->icache
.entry_mask
);
74 /* Clear i-cache line valid-bit */
75 for (i
= 0; i
< cpu_data
->icache
.ways
; i
++) {
76 __raw_writel(0, icacheaddr
);
77 icacheaddr
+= cpu_data
->icache
.way_incr
;
82 local_irq_restore(flags
);
85 static inline void flush_cache_4096(unsigned long start
,
88 unsigned long flags
, exec_offset
= 0;
91 * All types of SH-4 require PC to be uncached to operate on the I-cache.
92 * Some types of SH-4 require PC to be uncached to operate on the D-cache.
94 if ((boot_cpu_data
.flags
& CPU_HAS_P2_FLUSH_BUG
) ||
95 (start
< CACHE_OC_ADDRESS_ARRAY
))
96 exec_offset
= cached_to_uncached
;
98 local_irq_save(flags
);
99 __flush_cache_4096(start
| SH_CACHE_ASSOC
,
100 virt_to_phys(phys
), exec_offset
);
101 local_irq_restore(flags
);
105 * Write back & invalidate the D-cache of the page.
106 * (To avoid "alias" issues)
108 static void sh4_flush_dcache_page(void *arg
)
110 struct page
*page
= arg
;
112 struct address_space
*mapping
= page_mapping(page
);
114 if (mapping
&& !mapping_mapped(mapping
))
115 set_bit(PG_dcache_dirty
, &page
->flags
);
119 unsigned long phys
= page_to_phys(page
);
120 unsigned long addr
= CACHE_OC_ADDRESS_ARRAY
;
123 /* Loop all the D-cache */
124 n
= boot_cpu_data
.dcache
.way_incr
>> 12;
125 for (i
= 0; i
< n
; i
++, addr
+= 4096)
126 flush_cache_4096(addr
, phys
);
132 /* TODO: Selective icache invalidation through IC address array.. */
133 static void __uses_jump_to_uncached
flush_icache_all(void)
135 unsigned long flags
, ccr
;
137 local_irq_save(flags
);
142 ccr
|= CCR_CACHE_ICI
;
146 * back_to_cached() will take care of the barrier for us, don't add
151 local_irq_restore(flags
);
154 static void flush_dcache_all(void)
156 unsigned long addr
, end_addr
, entry_offset
;
158 end_addr
= CACHE_OC_ADDRESS_ARRAY
+
159 (current_cpu_data
.dcache
.sets
<<
160 current_cpu_data
.dcache
.entry_shift
) *
161 current_cpu_data
.dcache
.ways
;
163 entry_offset
= 1 << current_cpu_data
.dcache
.entry_shift
;
165 for (addr
= CACHE_OC_ADDRESS_ARRAY
; addr
< end_addr
; ) {
166 __raw_writel(0, addr
); addr
+= entry_offset
;
167 __raw_writel(0, addr
); addr
+= entry_offset
;
168 __raw_writel(0, addr
); addr
+= entry_offset
;
169 __raw_writel(0, addr
); addr
+= entry_offset
;
170 __raw_writel(0, addr
); addr
+= entry_offset
;
171 __raw_writel(0, addr
); addr
+= entry_offset
;
172 __raw_writel(0, addr
); addr
+= entry_offset
;
173 __raw_writel(0, addr
); addr
+= entry_offset
;
177 static void sh4_flush_cache_all(void *unused
)
184 * Note : (RPC) since the caches are physically tagged, the only point
185 * of flush_cache_mm for SH-4 is to get rid of aliases from the
186 * D-cache. The assumption elsewhere, e.g. flush_cache_range, is that
187 * lines can stay resident so long as the virtual address they were
188 * accessed with (hence cache set) is in accord with the physical
189 * address (i.e. tag). It's no different here.
191 * Caller takes mm->mmap_sem.
193 static void sh4_flush_cache_mm(void *arg
)
195 struct mm_struct
*mm
= arg
;
197 if (cpu_context(smp_processor_id(), mm
) == NO_CONTEXT
)
204 * Write back and invalidate I/D-caches for the page.
206 * ADDR: Virtual Address (U0 address)
207 * PFN: Physical page number
209 static void sh4_flush_cache_page(void *args
)
211 struct flusher_data
*data
= args
;
212 struct vm_area_struct
*vma
;
214 unsigned long address
, pfn
, phys
;
215 int map_coherent
= 0;
223 address
= data
->addr1
;
225 phys
= pfn
<< PAGE_SHIFT
;
226 page
= pfn_to_page(pfn
);
228 if (cpu_context(smp_processor_id(), vma
->vm_mm
) == NO_CONTEXT
)
231 address
&= PAGE_MASK
;
232 pgd
= pgd_offset(vma
->vm_mm
, address
);
233 pud
= pud_offset(pgd
, address
);
234 pmd
= pmd_offset(pud
, address
);
235 pte
= pte_offset_kernel(pmd
, address
);
237 /* If the page isn't present, there is nothing to do here. */
238 if (!(pte_val(*pte
) & _PAGE_PRESENT
))
241 if ((vma
->vm_mm
== current
->active_mm
))
245 * Use kmap_coherent or kmap_atomic to do flushes for
246 * another ASID than the current one.
248 map_coherent
= (current_cpu_data
.dcache
.n_aliases
&&
249 !test_bit(PG_dcache_dirty
, &page
->flags
) &&
252 vaddr
= kmap_coherent(page
, address
);
254 vaddr
= kmap_atomic(page
, KM_USER0
);
256 address
= (unsigned long)vaddr
;
259 if (pages_do_alias(address
, phys
))
260 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY
|
261 (address
& shm_align_mask
), phys
);
263 if (vma
->vm_flags
& VM_EXEC
)
268 kunmap_coherent(vaddr
);
270 kunmap_atomic(vaddr
, KM_USER0
);
275 * Write back and invalidate D-caches.
277 * START, END: Virtual Address (U0 address)
279 * NOTE: We need to flush the _physical_ page entry.
280 * Flushing the cache lines for U0 only isn't enough.
281 * We need to flush for P1 too, which may contain aliases.
283 static void sh4_flush_cache_range(void *args
)
285 struct flusher_data
*data
= args
;
286 struct vm_area_struct
*vma
;
287 unsigned long start
, end
;
293 if (cpu_context(smp_processor_id(), vma
->vm_mm
) == NO_CONTEXT
)
297 * If cache is only 4k-per-way, there are never any 'aliases'. Since
298 * the cache is physically tagged, the data can just be left in there.
300 if (boot_cpu_data
.dcache
.n_aliases
== 0)
305 if (vma
->vm_flags
& VM_EXEC
)
312 * @addr: address in memory mapped cache array
313 * @phys: P1 address to flush (has to match tags if addr has 'A' bit
314 * set i.e. associative write)
315 * @exec_offset: set to 0x20000000 if flush has to be executed from P2
318 * The offset into the cache array implied by 'addr' selects the
319 * 'colour' of the virtual address range that will be flushed. The
320 * operation (purge/write-back) is selected by the lower 2 bits of
323 static void __flush_cache_4096(unsigned long addr
, unsigned long phys
,
324 unsigned long exec_offset
)
327 unsigned long base_addr
= addr
;
328 struct cache_info
*dcache
;
329 unsigned long way_incr
;
330 unsigned long a
, ea
, p
;
331 unsigned long temp_pc
;
333 dcache
= &boot_cpu_data
.dcache
;
334 /* Write this way for better assembly. */
335 way_count
= dcache
->ways
;
336 way_incr
= dcache
->way_incr
;
339 * Apply exec_offset (i.e. branch to P2 if required.).
343 * If I write "=r" for the (temp_pc), it puts this in r6 hence
344 * trashing exec_offset before it's been added on - why? Hence
345 * "=&r" as a 'workaround'
347 asm volatile("mov.l 1f, %0\n\t"
353 "2:\n" : "=&r" (temp_pc
) : "r" (exec_offset
));
356 * We know there will be >=1 iteration, so write as do-while to avoid
357 * pointless nead-of-loop check for 0 iterations.
360 ea
= base_addr
+ 4096;
365 *(volatile unsigned long *)a
= p
;
367 * Next line: intentionally not p+32, saves an add, p
368 * will do since only the cache tag bits need to
371 *(volatile unsigned long *)(a
+32) = p
;
376 base_addr
+= way_incr
;
377 } while (--way_count
!= 0);
380 extern void __weak
sh4__flush_region_init(void);
383 * SH-4 has virtually indexed and physically tagged cache.
385 void __init
sh4_cache_init(void)
387 printk("PVR=%08x CVR=%08x PRR=%08x\n",
392 local_flush_icache_range
= sh4_flush_icache_range
;
393 local_flush_dcache_page
= sh4_flush_dcache_page
;
394 local_flush_cache_all
= sh4_flush_cache_all
;
395 local_flush_cache_mm
= sh4_flush_cache_mm
;
396 local_flush_cache_dup_mm
= sh4_flush_cache_mm
;
397 local_flush_cache_page
= sh4_flush_cache_page
;
398 local_flush_cache_range
= sh4_flush_cache_range
;
400 sh4__flush_region_init();