blob: 92eb98633ab05655fcdc3c4073f071798302e632 [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
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +090030static void __flush_cache_one(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 */
Paul Mundt2dc2f8e2010-01-21 16:05:25 +090039static void 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;
Matt Fleminga9d244a2009-11-05 23:14:39 +000068 int j, n;
Paul Mundt682f88a2009-09-09 13:19:46 +090069
70 __ocbwb(v);
71
72 icacheaddr = CACHE_IC_ADDRESS_ARRAY | (v &
73 cpu_data->icache.entry_mask);
74
75 /* Clear i-cache line valid-bit */
Matt Fleminga9d244a2009-11-05 23:14:39 +000076 n = boot_cpu_data.icache.n_aliases;
Paul Mundt682f88a2009-09-09 13:19:46 +090077 for (i = 0; i < cpu_data->icache.ways; i++) {
Matt Fleminga9d244a2009-11-05 23:14:39 +000078 for (j = 0; j < n; j++)
79 __raw_writel(0, icacheaddr + (j * PAGE_SIZE));
Paul Mundt682f88a2009-09-09 13:19:46 +090080 icacheaddr += cpu_data->icache.way_incr;
81 }
82 }
83
84 back_to_cached();
85 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +090088static inline void flush_cache_one(unsigned long start, unsigned long phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Paul Mundt983f4c52009-09-01 21:12:55 +090090 unsigned long flags, exec_offset = 0;
Paul Mundt33573c02006-09-27 18:37:30 +090091
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 /*
Matt Fleming1f69b6a2009-10-06 21:22:25 +000093 * All types of SH-4 require PC to be uncached to operate on the I-cache.
94 * Some types of SH-4 require PC to be uncached to operate on the D-cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +090096 if ((boot_cpu_data.flags & CPU_HAS_P2_FLUSH_BUG) ||
Paul Mundt33573c02006-09-27 18:37:30 +090097 (start < CACHE_OC_ADDRESS_ARRAY))
Matt Fleming1f69b6a2009-10-06 21:22:25 +000098 exec_offset = cached_to_uncached;
Paul Mundt28ccf7f2006-09-27 18:30:07 +090099
Paul Mundt983f4c52009-09-01 21:12:55 +0900100 local_irq_save(flags);
Matt Fleminga781d1e2009-12-04 16:18:11 +0900101 __flush_cache_one(start, phys, exec_offset);
Paul Mundt983f4c52009-09-01 21:12:55 +0900102 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/*
106 * Write back & invalidate the D-cache of the page.
107 * (To avoid "alias" issues)
108 */
Paul Mundte76a0132009-08-27 11:31:16 +0900109static void sh4_flush_dcache_page(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Paul Mundte76a0132009-08-27 11:31:16 +0900111 struct page *page = arg;
Matt Flemingb4c89272009-12-24 22:17:35 +0000112 unsigned long addr = (unsigned long)page_address(page);
Paul Mundtc139a592009-08-20 15:24:41 +0900113#ifndef CONFIG_SMP
Paul Mundt2277ab42009-07-22 19:20:49 +0900114 struct address_space *mapping = page_mapping(page);
115
Paul Mundt2277ab42009-07-22 19:20:49 +0900116 if (mapping && !mapping_mapped(mapping))
Paul Mundt55661fc2010-12-01 15:39:51 +0900117 clear_bit(PG_dcache_clean, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +0900118 else
119#endif
Matt Flemingb4c89272009-12-24 22:17:35 +0000120 flush_cache_one(CACHE_OC_ADDRESS_ARRAY |
121 (addr & shm_align_mask), page_to_phys(page));
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900122
123 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900126/* TODO: Selective icache invalidation through IC address array.. */
Paul Mundt2dc2f8e2010-01-21 16:05:25 +0900127static void flush_icache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Paul Mundt983f4c52009-09-01 21:12:55 +0900129 unsigned long flags, ccr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Paul Mundt983f4c52009-09-01 21:12:55 +0900131 local_irq_save(flags);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900132 jump_to_uncached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /* Flush I-cache */
Paul Mundt9d56dd32010-01-26 12:58:40 +0900135 ccr = __raw_readl(CCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 ccr |= CCR_CACHE_ICI;
Paul Mundt9d56dd32010-01-26 12:58:40 +0900137 __raw_writel(ccr, CCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Paul Mundt29847622006-09-27 14:57:44 +0900139 /*
Stuart Menefycbaa1182007-11-30 17:06:36 +0900140 * back_to_cached() will take care of the barrier for us, don't add
Paul Mundt29847622006-09-27 14:57:44 +0900141 * another one!
142 */
Paul Mundt983f4c52009-09-01 21:12:55 +0900143
Stuart Menefycbaa1182007-11-30 17:06:36 +0900144 back_to_cached();
Paul Mundt983f4c52009-09-01 21:12:55 +0900145 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Paul Mundtbd6df572009-09-09 14:22:15 +0900148static void flush_dcache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Paul Mundtbd6df572009-09-09 14:22:15 +0900150 unsigned long addr, end_addr, entry_offset;
151
152 end_addr = CACHE_OC_ADDRESS_ARRAY +
153 (current_cpu_data.dcache.sets <<
154 current_cpu_data.dcache.entry_shift) *
155 current_cpu_data.dcache.ways;
156
157 entry_offset = 1 << current_cpu_data.dcache.entry_shift;
158
159 for (addr = CACHE_OC_ADDRESS_ARRAY; addr < end_addr; ) {
160 __raw_writel(0, addr); addr += entry_offset;
161 __raw_writel(0, addr); addr += entry_offset;
162 __raw_writel(0, addr); addr += entry_offset;
163 __raw_writel(0, addr); addr += entry_offset;
164 __raw_writel(0, addr); addr += entry_offset;
165 __raw_writel(0, addr); addr += entry_offset;
166 __raw_writel(0, addr); addr += entry_offset;
167 __raw_writel(0, addr); addr += entry_offset;
168 }
Paul Mundta2527102006-09-27 11:29:55 +0900169}
170
Paul Mundtf26b2a52009-08-21 17:23:14 +0900171static void sh4_flush_cache_all(void *unused)
Paul Mundta2527102006-09-27 11:29:55 +0900172{
173 flush_dcache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 flush_icache_all();
175}
176
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900177/*
178 * Note : (RPC) since the caches are physically tagged, the only point
179 * of flush_cache_mm for SH-4 is to get rid of aliases from the
180 * D-cache. The assumption elsewhere, e.g. flush_cache_range, is that
181 * lines can stay resident so long as the virtual address they were
182 * accessed with (hence cache set) is in accord with the physical
Paul Mundt654d3642009-09-09 14:04:06 +0900183 * address (i.e. tag). It's no different here.
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900184 *
185 * Caller takes mm->mmap_sem.
186 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900187static void sh4_flush_cache_mm(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900189 struct mm_struct *mm = arg;
190
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900191 if (cpu_context(smp_processor_id(), mm) == NO_CONTEXT)
192 return;
193
Paul Mundt654d3642009-09-09 14:04:06 +0900194 flush_dcache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
198 * Write back and invalidate I/D-caches for the page.
199 *
200 * ADDR: Virtual Address (U0 address)
201 * PFN: Physical page number
202 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900203static void sh4_flush_cache_page(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900205 struct flusher_data *data = args;
206 struct vm_area_struct *vma;
Paul Mundtdeaef202009-09-09 16:06:39 +0900207 struct page *page;
Paul Mundtf26b2a52009-08-21 17:23:14 +0900208 unsigned long address, pfn, phys;
Paul Mundtdeaef202009-09-09 16:06:39 +0900209 int map_coherent = 0;
210 pgd_t *pgd;
211 pud_t *pud;
212 pmd_t *pmd;
213 pte_t *pte;
214 void *vaddr;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900215
Paul Mundtf26b2a52009-08-21 17:23:14 +0900216 vma = data->vma;
Paul Mundtabeaf332009-10-16 15:14:50 +0900217 address = data->addr1 & PAGE_MASK;
Paul Mundtf26b2a52009-08-21 17:23:14 +0900218 pfn = data->addr2;
219 phys = pfn << PAGE_SHIFT;
Paul Mundtdeaef202009-09-09 16:06:39 +0900220 page = pfn_to_page(pfn);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900221
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900222 if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
223 return;
224
Paul Mundtdeaef202009-09-09 16:06:39 +0900225 pgd = pgd_offset(vma->vm_mm, address);
226 pud = pud_offset(pgd, address);
227 pmd = pmd_offset(pud, address);
228 pte = pte_offset_kernel(pmd, address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Paul Mundtdeaef202009-09-09 16:06:39 +0900230 /* If the page isn't present, there is nothing to do here. */
231 if (!(pte_val(*pte) & _PAGE_PRESENT))
232 return;
233
234 if ((vma->vm_mm == current->active_mm))
235 vaddr = NULL;
236 else {
237 /*
238 * Use kmap_coherent or kmap_atomic to do flushes for
239 * another ASID than the current one.
240 */
241 map_coherent = (current_cpu_data.dcache.n_aliases &&
Paul Mundt55661fc2010-12-01 15:39:51 +0900242 test_bit(PG_dcache_clean, &page->flags) &&
Paul Mundtdeaef202009-09-09 16:06:39 +0900243 page_mapped(page));
244 if (map_coherent)
245 vaddr = kmap_coherent(page, address);
246 else
247 vaddr = kmap_atomic(page, KM_USER0);
248
249 address = (unsigned long)vaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Matt Fleminge717cc62009-12-08 14:23:11 +0000252 flush_cache_one(CACHE_OC_ADDRESS_ARRAY |
Paul Mundtdeaef202009-09-09 16:06:39 +0900253 (address & shm_align_mask), phys);
254
255 if (vma->vm_flags & VM_EXEC)
256 flush_icache_all();
257
258 if (vaddr) {
259 if (map_coherent)
260 kunmap_coherent(vaddr);
261 else
262 kunmap_atomic(vaddr, KM_USER0);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
266/*
267 * Write back and invalidate D-caches.
268 *
269 * START, END: Virtual Address (U0 address)
270 *
271 * NOTE: We need to flush the _physical_ page entry.
272 * Flushing the cache lines for U0 only isn't enough.
273 * We need to flush for P1 too, which may contain aliases.
274 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900275static void sh4_flush_cache_range(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900277 struct flusher_data *data = args;
278 struct vm_area_struct *vma;
279 unsigned long start, end;
280
281 vma = data->vma;
282 start = data->addr1;
283 end = data->addr2;
284
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900285 if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
286 return;
287
Richard Curnowb638d0b2006-09-27 14:09:26 +0900288 /*
289 * If cache is only 4k-per-way, there are never any 'aliases'. Since
290 * the cache is physically tagged, the data can just be left in there.
291 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900292 if (boot_cpu_data.dcache.n_aliases == 0)
Richard Curnowb638d0b2006-09-27 14:09:26 +0900293 return;
294
Paul Mundt654d3642009-09-09 14:04:06 +0900295 flush_dcache_all();
Richard Curnowb638d0b2006-09-27 14:09:26 +0900296
Paul Mundt654d3642009-09-09 14:04:06 +0900297 if (vma->vm_flags & VM_EXEC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 flush_icache_all();
299}
300
Richard Curnowb638d0b2006-09-27 14:09:26 +0900301/**
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900302 * __flush_cache_one
Richard Curnowb638d0b2006-09-27 14:09:26 +0900303 *
304 * @addr: address in memory mapped cache array
305 * @phys: P1 address to flush (has to match tags if addr has 'A' bit
306 * set i.e. associative write)
307 * @exec_offset: set to 0x20000000 if flush has to be executed from P2
308 * region else 0x0
309 *
310 * The offset into the cache array implied by 'addr' selects the
311 * 'colour' of the virtual address range that will be flushed. The
312 * operation (purge/write-back) is selected by the lower 2 bits of
313 * 'phys'.
314 */
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900315static void __flush_cache_one(unsigned long addr, unsigned long phys,
Richard Curnowb638d0b2006-09-27 14:09:26 +0900316 unsigned long exec_offset)
317{
318 int way_count;
319 unsigned long base_addr = addr;
320 struct cache_info *dcache;
321 unsigned long way_incr;
322 unsigned long a, ea, p;
323 unsigned long temp_pc;
324
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900325 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900326 /* Write this way for better assembly. */
327 way_count = dcache->ways;
328 way_incr = dcache->way_incr;
329
330 /*
331 * Apply exec_offset (i.e. branch to P2 if required.).
332 *
333 * FIXME:
334 *
335 * If I write "=r" for the (temp_pc), it puts this in r6 hence
336 * trashing exec_offset before it's been added on - why? Hence
337 * "=&r" as a 'workaround'
338 */
339 asm volatile("mov.l 1f, %0\n\t"
340 "add %1, %0\n\t"
341 "jmp @%0\n\t"
342 "nop\n\t"
343 ".balign 4\n\t"
344 "1: .long 2f\n\t"
345 "2:\n" : "=&r" (temp_pc) : "r" (exec_offset));
346
347 /*
348 * We know there will be >=1 iteration, so write as do-while to avoid
349 * pointless nead-of-loop check for 0 iterations.
350 */
351 do {
352 ea = base_addr + PAGE_SIZE;
353 a = base_addr;
354 p = phys;
355
356 do {
357 *(volatile unsigned long *)a = p;
358 /*
359 * Next line: intentionally not p+32, saves an add, p
360 * will do since only the cache tag bits need to
361 * match.
362 */
363 *(volatile unsigned long *)(a+32) = p;
364 a += 64;
365 p += 64;
366 } while (a < ea);
367
368 base_addr += way_incr;
369 } while (--way_count != 0);
370}
371
Paul Mundt37443ef2009-08-15 12:29:49 +0900372extern void __weak sh4__flush_region_init(void);
373
374/*
375 * SH-4 has virtually indexed and physically tagged cache.
376 */
377void __init sh4_cache_init(void)
378{
379 printk("PVR=%08x CVR=%08x PRR=%08x\n",
Paul Mundt9d56dd32010-01-26 12:58:40 +0900380 __raw_readl(CCN_PVR),
381 __raw_readl(CCN_CVR),
382 __raw_readl(CCN_PRR));
Paul Mundt37443ef2009-08-15 12:29:49 +0900383
Paul Mundtf26b2a52009-08-21 17:23:14 +0900384 local_flush_icache_range = sh4_flush_icache_range;
385 local_flush_dcache_page = sh4_flush_dcache_page;
386 local_flush_cache_all = sh4_flush_cache_all;
387 local_flush_cache_mm = sh4_flush_cache_mm;
388 local_flush_cache_dup_mm = sh4_flush_cache_mm;
389 local_flush_cache_page = sh4_flush_cache_page;
390 local_flush_cache_range = sh4_flush_cache_range;
Paul Mundt37443ef2009-08-15 12:29:49 +0900391
392 sh4__flush_region_init();
393}