blob: 56dd55a1b13e00b717dbc94bee1b54bdd855f640 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/sh/mm/cache-sh4.c
3 *
4 * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
Paul Mundtdeaef202009-09-09 16:06:39 +09005 * Copyright (C) 2001 - 2009 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2003 Richard Curnow
Chris Smith09b5a102008-07-02 15:17:11 +09007 * Copyright (c) 2007 STMicroelectronics (R&D) Ltd.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
11 * for more details.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/mm.h>
Paul Mundt52e27782006-11-21 11:09:41 +090015#include <linux/io.h>
16#include <linux/mutex.h>
Paul Mundt2277ab42009-07-22 19:20:49 +090017#include <linux/fs.h>
Paul Mundtdeaef202009-09-09 16:06:39 +090018#include <linux/highmem.h>
19#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/mmu_context.h>
21#include <asm/cacheflush.h>
22
Paul Mundt28ccf7f2006-09-27 18:30:07 +090023/*
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
26 * entirety.
27 */
Chris Smith09b5a102008-07-02 15:17:11 +090028#define MAX_ICACHE_PAGES 32
Paul Mundt28ccf7f2006-09-27 18:30:07 +090029
Richard Curnowb638d0b2006-09-27 14:09:26 +090030static void __flush_cache_4096(unsigned long addr, unsigned long phys,
Paul Mundta2527102006-09-27 11:29:55 +090031 unsigned long exec_offset);
Richard Curnowb638d0b2006-09-27 14:09:26 +090032
33/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 * Write back the range of D-cache, and purge the I-cache.
35 *
Chris Smith09b5a102008-07-02 15:17:11 +090036 * Called from kernel/module.c:sys_init_module and routine for a.out format,
37 * signal handler code and kprobes code
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 */
Matt Fleminga6325242009-10-06 21:22:21 +000039static void __uses_jump_to_uncached sh4_flush_icache_range(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Paul Mundtf26b2a52009-08-21 17:23:14 +090041 struct flusher_data *data = args;
Paul Mundtf26b2a52009-08-21 17:23:14 +090042 unsigned long start, end;
Paul Mundt983f4c52009-09-01 21:12:55 +090043 unsigned long flags, v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 int i;
45
Paul Mundtf26b2a52009-08-21 17:23:14 +090046 start = data->addr1;
47 end = data->addr2;
48
Paul Mundt682f88a2009-09-09 13:19:46 +090049 /* 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);
52 return;
Chris Smith09b5a102008-07-02 15:17:11 +090053 }
Paul Mundt682f88a2009-09-09 13:19:46 +090054
55 /*
56 * Selectively flush d-cache then invalidate the i-cache.
57 * This is inefficient, so only use this for small ranges.
58 */
59 start &= ~(L1_CACHE_BYTES-1);
60 end += L1_CACHE_BYTES-1;
61 end &= ~(L1_CACHE_BYTES-1);
62
63 local_irq_save(flags);
64 jump_to_uncached();
65
66 for (v = start; v < end; v += L1_CACHE_BYTES) {
67 unsigned long icacheaddr;
68
69 __ocbwb(v);
70
71 icacheaddr = CACHE_IC_ADDRESS_ARRAY | (v &
72 cpu_data->icache.entry_mask);
73
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;
78 }
79 }
80
81 back_to_cached();
82 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85static inline void flush_cache_4096(unsigned long start,
86 unsigned long phys)
87{
Paul Mundt983f4c52009-09-01 21:12:55 +090088 unsigned long flags, exec_offset = 0;
Paul Mundt33573c02006-09-27 18:37:30 +090089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /*
Matt Fleming1f69b6a2009-10-06 21:22:25 +000091 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +090094 if ((boot_cpu_data.flags & CPU_HAS_P2_FLUSH_BUG) ||
Paul Mundt33573c02006-09-27 18:37:30 +090095 (start < CACHE_OC_ADDRESS_ARRAY))
Matt Fleming1f69b6a2009-10-06 21:22:25 +000096 exec_offset = cached_to_uncached;
Paul Mundt28ccf7f2006-09-27 18:30:07 +090097
Paul Mundt983f4c52009-09-01 21:12:55 +090098 local_irq_save(flags);
Paul Mundt33573c02006-09-27 18:37:30 +090099 __flush_cache_4096(start | SH_CACHE_ASSOC,
Matt Fleming8bd642b2009-10-06 21:22:24 +0000100 virt_to_phys(phys), exec_offset);
Paul Mundt983f4c52009-09-01 21:12:55 +0900101 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
104/*
105 * Write back & invalidate the D-cache of the page.
106 * (To avoid "alias" issues)
107 */
Paul Mundte76a0132009-08-27 11:31:16 +0900108static void sh4_flush_dcache_page(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Paul Mundte76a0132009-08-27 11:31:16 +0900110 struct page *page = arg;
Paul Mundtc139a592009-08-20 15:24:41 +0900111#ifndef CONFIG_SMP
Paul Mundt2277ab42009-07-22 19:20:49 +0900112 struct address_space *mapping = page_mapping(page);
113
Paul Mundt2277ab42009-07-22 19:20:49 +0900114 if (mapping && !mapping_mapped(mapping))
115 set_bit(PG_dcache_dirty, &page->flags);
116 else
117#endif
118 {
Paul Mundt31c9efd2009-09-09 14:10:28 +0900119 unsigned long phys = page_to_phys(page);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900120 unsigned long addr = CACHE_OC_ADDRESS_ARRAY;
121 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 /* Loop all the D-cache */
Paul Mundt31c9efd2009-09-09 14:10:28 +0900124 n = boot_cpu_data.dcache.way_incr >> 12;
Paul Mundt510c72ad2006-11-27 12:06:26 +0900125 for (i = 0; i < n; i++, addr += 4096)
Richard Curnowb638d0b2006-09-27 14:09:26 +0900126 flush_cache_4096(addr, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900128
129 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900132/* TODO: Selective icache invalidation through IC address array.. */
Paul Mundt205a3b42008-09-05 18:00:29 +0900133static void __uses_jump_to_uncached flush_icache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Paul Mundt983f4c52009-09-01 21:12:55 +0900135 unsigned long flags, ccr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Paul Mundt983f4c52009-09-01 21:12:55 +0900137 local_irq_save(flags);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900138 jump_to_uncached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 /* Flush I-cache */
141 ccr = ctrl_inl(CCR);
142 ccr |= CCR_CACHE_ICI;
143 ctrl_outl(ccr, CCR);
144
Paul Mundt29847622006-09-27 14:57:44 +0900145 /*
Stuart Menefycbaa1182007-11-30 17:06:36 +0900146 * back_to_cached() will take care of the barrier for us, don't add
Paul Mundt29847622006-09-27 14:57:44 +0900147 * another one!
148 */
Paul Mundt983f4c52009-09-01 21:12:55 +0900149
Stuart Menefycbaa1182007-11-30 17:06:36 +0900150 back_to_cached();
Paul Mundt983f4c52009-09-01 21:12:55 +0900151 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Paul Mundtbd6df572009-09-09 14:22:15 +0900154static void flush_dcache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
Paul Mundtbd6df572009-09-09 14:22:15 +0900156 unsigned long addr, end_addr, entry_offset;
157
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;
162
163 entry_offset = 1 << current_cpu_data.dcache.entry_shift;
164
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;
174 }
Paul Mundta2527102006-09-27 11:29:55 +0900175}
176
Paul Mundtf26b2a52009-08-21 17:23:14 +0900177static void sh4_flush_cache_all(void *unused)
Paul Mundta2527102006-09-27 11:29:55 +0900178{
179 flush_dcache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 flush_icache_all();
181}
182
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900183/*
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
Paul Mundt654d3642009-09-09 14:04:06 +0900189 * address (i.e. tag). It's no different here.
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900190 *
191 * Caller takes mm->mmap_sem.
192 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900193static void sh4_flush_cache_mm(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900195 struct mm_struct *mm = arg;
196
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900197 if (cpu_context(smp_processor_id(), mm) == NO_CONTEXT)
198 return;
199
Paul Mundt654d3642009-09-09 14:04:06 +0900200 flush_dcache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
203/*
204 * Write back and invalidate I/D-caches for the page.
205 *
206 * ADDR: Virtual Address (U0 address)
207 * PFN: Physical page number
208 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900209static void sh4_flush_cache_page(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900211 struct flusher_data *data = args;
212 struct vm_area_struct *vma;
Paul Mundtdeaef202009-09-09 16:06:39 +0900213 struct page *page;
Paul Mundtf26b2a52009-08-21 17:23:14 +0900214 unsigned long address, pfn, phys;
Paul Mundtdeaef202009-09-09 16:06:39 +0900215 int map_coherent = 0;
216 pgd_t *pgd;
217 pud_t *pud;
218 pmd_t *pmd;
219 pte_t *pte;
220 void *vaddr;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900221
Paul Mundtf26b2a52009-08-21 17:23:14 +0900222 vma = data->vma;
223 address = data->addr1;
224 pfn = data->addr2;
225 phys = pfn << PAGE_SHIFT;
Paul Mundtdeaef202009-09-09 16:06:39 +0900226 page = pfn_to_page(pfn);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900227
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900228 if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
229 return;
230
Paul Mundtdeaef202009-09-09 16:06:39 +0900231 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Paul Mundtdeaef202009-09-09 16:06:39 +0900237 /* If the page isn't present, there is nothing to do here. */
238 if (!(pte_val(*pte) & _PAGE_PRESENT))
239 return;
240
241 if ((vma->vm_mm == current->active_mm))
242 vaddr = NULL;
243 else {
244 /*
245 * Use kmap_coherent or kmap_atomic to do flushes for
246 * another ASID than the current one.
247 */
248 map_coherent = (current_cpu_data.dcache.n_aliases &&
249 !test_bit(PG_dcache_dirty, &page->flags) &&
250 page_mapped(page));
251 if (map_coherent)
252 vaddr = kmap_coherent(page, address);
253 else
254 vaddr = kmap_atomic(page, KM_USER0);
255
256 address = (unsigned long)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Paul Mundtdeaef202009-09-09 16:06:39 +0900259 if (pages_do_alias(address, phys))
260 flush_cache_4096(CACHE_OC_ADDRESS_ARRAY |
261 (address & shm_align_mask), phys);
262
263 if (vma->vm_flags & VM_EXEC)
264 flush_icache_all();
265
266 if (vaddr) {
267 if (map_coherent)
268 kunmap_coherent(vaddr);
269 else
270 kunmap_atomic(vaddr, KM_USER0);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
274/*
275 * Write back and invalidate D-caches.
276 *
277 * START, END: Virtual Address (U0 address)
278 *
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.
282 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900283static void sh4_flush_cache_range(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900285 struct flusher_data *data = args;
286 struct vm_area_struct *vma;
287 unsigned long start, end;
288
289 vma = data->vma;
290 start = data->addr1;
291 end = data->addr2;
292
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900293 if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
294 return;
295
Richard Curnowb638d0b2006-09-27 14:09:26 +0900296 /*
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.
299 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900300 if (boot_cpu_data.dcache.n_aliases == 0)
Richard Curnowb638d0b2006-09-27 14:09:26 +0900301 return;
302
Paul Mundt654d3642009-09-09 14:04:06 +0900303 flush_dcache_all();
Richard Curnowb638d0b2006-09-27 14:09:26 +0900304
Paul Mundt654d3642009-09-09 14:04:06 +0900305 if (vma->vm_flags & VM_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 flush_icache_all();
307}
308
Richard Curnowb638d0b2006-09-27 14:09:26 +0900309/**
310 * __flush_cache_4096
311 *
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
316 * region else 0x0
317 *
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
321 * 'phys'.
322 */
323static void __flush_cache_4096(unsigned long addr, unsigned long phys,
324 unsigned long exec_offset)
325{
326 int way_count;
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;
332
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900333 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900334 /* Write this way for better assembly. */
335 way_count = dcache->ways;
336 way_incr = dcache->way_incr;
337
338 /*
339 * Apply exec_offset (i.e. branch to P2 if required.).
340 *
341 * FIXME:
342 *
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'
346 */
347 asm volatile("mov.l 1f, %0\n\t"
348 "add %1, %0\n\t"
349 "jmp @%0\n\t"
350 "nop\n\t"
351 ".balign 4\n\t"
352 "1: .long 2f\n\t"
353 "2:\n" : "=&r" (temp_pc) : "r" (exec_offset));
354
355 /*
356 * We know there will be >=1 iteration, so write as do-while to avoid
357 * pointless nead-of-loop check for 0 iterations.
358 */
359 do {
Paul Mundtc4845a42009-09-09 17:13:07 +0900360 ea = base_addr + 4096;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900361 a = base_addr;
362 p = phys;
363
364 do {
365 *(volatile unsigned long *)a = p;
366 /*
367 * Next line: intentionally not p+32, saves an add, p
368 * will do since only the cache tag bits need to
369 * match.
370 */
371 *(volatile unsigned long *)(a+32) = p;
372 a += 64;
373 p += 64;
374 } while (a < ea);
375
376 base_addr += way_incr;
377 } while (--way_count != 0);
378}
379
Paul Mundt37443ef2009-08-15 12:29:49 +0900380extern void __weak sh4__flush_region_init(void);
381
382/*
383 * SH-4 has virtually indexed and physically tagged cache.
384 */
385void __init sh4_cache_init(void)
386{
387 printk("PVR=%08x CVR=%08x PRR=%08x\n",
388 ctrl_inl(CCN_PVR),
389 ctrl_inl(CCN_CVR),
390 ctrl_inl(CCN_PRR));
391
Paul Mundtf26b2a52009-08-21 17:23:14 +0900392 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;
Paul Mundt37443ef2009-08-15 12:29:49 +0900399
400 sh4__flush_region_init();
401}