blob: b7f235c74d66c0a61a9c18f491d15e65155d037f [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 Mundtd10040f2007-09-24 16:38:25 +09005 * Copyright (C) 2001 - 2007 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/mmu_context.h>
19#include <asm/cacheflush.h>
20
Paul Mundt28ccf7f2006-09-27 18:30:07 +090021/*
22 * The maximum number of pages we support up to when doing ranged dcache
23 * flushing. Anything exceeding this will simply flush the dcache in its
24 * entirety.
25 */
26#define MAX_DCACHE_PAGES 64 /* XXX: Tune for ways */
Chris Smith09b5a102008-07-02 15:17:11 +090027#define MAX_ICACHE_PAGES 32
Paul Mundt28ccf7f2006-09-27 18:30:07 +090028
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +090029static void __flush_cache_one(unsigned long addr, unsigned long phys,
Paul Mundta2527102006-09-27 11:29:55 +090030 unsigned long exec_offset);
Richard Curnowb638d0b2006-09-27 14:09:26 +090031
32/*
33 * This is initialised here to ensure that it is not placed in the BSS. If
34 * that were to happen, note that cache_init gets called before the BSS is
35 * cleared, so this would get nulled out which would be hopeless.
36 */
37static void (*__flush_dcache_segment_fn)(unsigned long, unsigned long) =
38 (void (*)(unsigned long, unsigned long))0xdeadbeef;
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * Write back the range of D-cache, and purge the I-cache.
42 *
Chris Smith09b5a102008-07-02 15:17:11 +090043 * Called from kernel/module.c:sys_init_module and routine for a.out format,
44 * signal handler code and kprobes code
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 */
Matt Fleminga6325242009-10-06 21:22:21 +000046static void __uses_jump_to_uncached sh4_flush_icache_range(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
Paul Mundtf26b2a52009-08-21 17:23:14 +090048 struct flusher_data *data = args;
Paul Mundtf26b2a52009-08-21 17:23:14 +090049 unsigned long start, end;
Paul Mundt983f4c52009-09-01 21:12:55 +090050 unsigned long flags, v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int i;
52
Paul Mundtf26b2a52009-08-21 17:23:14 +090053 start = data->addr1;
54 end = data->addr2;
55
Paul Mundt682f88a2009-09-09 13:19:46 +090056 /* If there are too many pages then just blow away the caches */
57 if (((end - start) >> PAGE_SHIFT) >= MAX_ICACHE_PAGES) {
58 local_flush_cache_all(NULL);
59 return;
Chris Smith09b5a102008-07-02 15:17:11 +090060 }
Paul Mundt682f88a2009-09-09 13:19:46 +090061
62 /*
63 * Selectively flush d-cache then invalidate the i-cache.
64 * This is inefficient, so only use this for small ranges.
65 */
66 start &= ~(L1_CACHE_BYTES-1);
67 end += L1_CACHE_BYTES-1;
68 end &= ~(L1_CACHE_BYTES-1);
69
70 local_irq_save(flags);
71 jump_to_uncached();
72
73 for (v = start; v < end; v += L1_CACHE_BYTES) {
74 unsigned long icacheaddr;
Matt Fleminga9d244a2009-11-05 23:14:39 +000075 int j, n;
Paul Mundt682f88a2009-09-09 13:19:46 +090076
77 __ocbwb(v);
78
79 icacheaddr = CACHE_IC_ADDRESS_ARRAY | (v &
80 cpu_data->icache.entry_mask);
81
82 /* Clear i-cache line valid-bit */
Matt Fleminga9d244a2009-11-05 23:14:39 +000083 n = boot_cpu_data.icache.n_aliases;
Paul Mundt682f88a2009-09-09 13:19:46 +090084 for (i = 0; i < cpu_data->icache.ways; i++) {
Matt Fleminga9d244a2009-11-05 23:14:39 +000085 for (j = 0; j < n; j++)
86 __raw_writel(0, icacheaddr + (j * PAGE_SIZE));
Paul Mundt682f88a2009-09-09 13:19:46 +090087 icacheaddr += cpu_data->icache.way_incr;
88 }
89 }
90
91 back_to_cached();
92 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +090095static inline void flush_cache_one(unsigned long start, unsigned long phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
Paul Mundt983f4c52009-09-01 21:12:55 +090097 unsigned long flags, exec_offset = 0;
Paul Mundt33573c02006-09-27 18:37:30 +090098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /*
Richard Curnowb638d0b2006-09-27 14:09:26 +0900100 * All types of SH-4 require PC to be in P2 to operate on the I-cache.
101 * Some types of SH-4 require PC to be in P2 to operate on the D-cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900103 if ((boot_cpu_data.flags & CPU_HAS_P2_FLUSH_BUG) ||
Paul Mundt33573c02006-09-27 18:37:30 +0900104 (start < CACHE_OC_ADDRESS_ARRAY))
Paul Mundt510c72ad2006-11-27 12:06:26 +0900105 exec_offset = 0x20000000;
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900106
Paul Mundt983f4c52009-09-01 21:12:55 +0900107 local_irq_save(flags);
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900108 __flush_cache_one(start | SH_CACHE_ASSOC, P1SEGADDR(phys), exec_offset);
Paul Mundt983f4c52009-09-01 21:12:55 +0900109 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
112/*
113 * Write back & invalidate the D-cache of the page.
114 * (To avoid "alias" issues)
115 */
Paul Mundte76a0132009-08-27 11:31:16 +0900116static void sh4_flush_dcache_page(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Paul Mundte76a0132009-08-27 11:31:16 +0900118 struct page *page = arg;
Paul Mundtc139a592009-08-20 15:24:41 +0900119#ifndef CONFIG_SMP
Paul Mundt2277ab42009-07-22 19:20:49 +0900120 struct address_space *mapping = page_mapping(page);
121
Paul Mundt2277ab42009-07-22 19:20:49 +0900122 if (mapping && !mapping_mapped(mapping))
123 set_bit(PG_dcache_dirty, &page->flags);
124 else
125#endif
126 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 unsigned long phys = PHYSADDR(page_address(page));
Richard Curnowb638d0b2006-09-27 14:09:26 +0900128 unsigned long addr = CACHE_OC_ADDRESS_ARRAY;
129 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* Loop all the D-cache */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900132 n = boot_cpu_data.dcache.n_aliases;
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900133 for (i = 0; i < n; i++, addr += PAGE_SIZE)
134 flush_cache_one(addr, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900136
137 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900140/* TODO: Selective icache invalidation through IC address array.. */
Paul Mundt205a3b42008-09-05 18:00:29 +0900141static void __uses_jump_to_uncached flush_icache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Paul Mundt983f4c52009-09-01 21:12:55 +0900143 unsigned long flags, ccr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Paul Mundt983f4c52009-09-01 21:12:55 +0900145 local_irq_save(flags);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900146 jump_to_uncached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* Flush I-cache */
149 ccr = ctrl_inl(CCR);
150 ccr |= CCR_CACHE_ICI;
151 ctrl_outl(ccr, CCR);
152
Paul Mundt29847622006-09-27 14:57:44 +0900153 /*
Stuart Menefycbaa1182007-11-30 17:06:36 +0900154 * back_to_cached() will take care of the barrier for us, don't add
Paul Mundt29847622006-09-27 14:57:44 +0900155 * another one!
156 */
Paul Mundt983f4c52009-09-01 21:12:55 +0900157
Stuart Menefycbaa1182007-11-30 17:06:36 +0900158 back_to_cached();
Paul Mundt983f4c52009-09-01 21:12:55 +0900159 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Paul Mundt0b445dc2009-08-15 11:22:50 +0900162static inline void flush_dcache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900164 (*__flush_dcache_segment_fn)(0UL, boot_cpu_data.dcache.way_size);
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900165 wmb();
Paul Mundta2527102006-09-27 11:29:55 +0900166}
167
Paul Mundtf26b2a52009-08-21 17:23:14 +0900168static void sh4_flush_cache_all(void *unused)
Paul Mundta2527102006-09-27 11:29:55 +0900169{
170 flush_dcache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 flush_icache_all();
172}
173
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900174static void __flush_cache_mm(struct mm_struct *mm, unsigned long start,
175 unsigned long end)
176{
177 unsigned long d = 0, p = start & PAGE_MASK;
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900178 unsigned long alias_mask = boot_cpu_data.dcache.alias_mask;
179 unsigned long n_aliases = boot_cpu_data.dcache.n_aliases;
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900180 unsigned long select_bit;
181 unsigned long all_aliases_mask;
182 unsigned long addr_offset;
183 pgd_t *dir;
184 pmd_t *pmd;
185 pud_t *pud;
186 pte_t *pte;
187 int i;
188
189 dir = pgd_offset(mm, p);
190 pud = pud_offset(dir, p);
191 pmd = pmd_offset(pud, p);
192 end = PAGE_ALIGN(end);
193
194 all_aliases_mask = (1 << n_aliases) - 1;
195
196 do {
197 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) {
198 p &= PMD_MASK;
199 p += PMD_SIZE;
200 pmd++;
201
202 continue;
203 }
204
205 pte = pte_offset_kernel(pmd, p);
206
207 do {
208 unsigned long phys;
209 pte_t entry = *pte;
210
211 if (!(pte_val(entry) & _PAGE_PRESENT)) {
212 pte++;
213 p += PAGE_SIZE;
214 continue;
215 }
216
217 phys = pte_val(entry) & PTE_PHYS_MASK;
218
219 if ((p ^ phys) & alias_mask) {
220 d |= 1 << ((p & alias_mask) >> PAGE_SHIFT);
221 d |= 1 << ((phys & alias_mask) >> PAGE_SHIFT);
222
223 if (d == all_aliases_mask)
224 goto loop_exit;
225 }
226
227 pte++;
228 p += PAGE_SIZE;
229 } while (p < end && ((unsigned long)pte & ~PAGE_MASK));
230 pmd++;
231 } while (p < end);
232
233loop_exit:
234 addr_offset = 0;
235 select_bit = 1;
236
237 for (i = 0; i < n_aliases; i++) {
238 if (d & select_bit) {
239 (*__flush_dcache_segment_fn)(addr_offset, PAGE_SIZE);
240 wmb();
241 }
242
243 select_bit <<= 1;
244 addr_offset += PAGE_SIZE;
245 }
246}
247
248/*
249 * Note : (RPC) since the caches are physically tagged, the only point
250 * of flush_cache_mm for SH-4 is to get rid of aliases from the
251 * D-cache. The assumption elsewhere, e.g. flush_cache_range, is that
252 * lines can stay resident so long as the virtual address they were
253 * accessed with (hence cache set) is in accord with the physical
254 * address (i.e. tag). It's no different here. So I reckon we don't
255 * need to flush the I-cache, since aliases don't matter for that. We
256 * should try that.
257 *
258 * Caller takes mm->mmap_sem.
259 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900260static void sh4_flush_cache_mm(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900262 struct mm_struct *mm = arg;
263
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900264 if (cpu_context(smp_processor_id(), mm) == NO_CONTEXT)
265 return;
266
Richard Curnowb638d0b2006-09-27 14:09:26 +0900267 /*
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900268 * If cache is only 4k-per-way, there are never any 'aliases'. Since
269 * the cache is physically tagged, the data can just be left in there.
Richard Curnowb638d0b2006-09-27 14:09:26 +0900270 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900271 if (boot_cpu_data.dcache.n_aliases == 0)
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900272 return;
273
274 /*
275 * Don't bother groveling around the dcache for the VMA ranges
276 * if there are too many PTEs to make it worthwhile.
277 */
278 if (mm->nr_ptes >= MAX_DCACHE_PAGES)
279 flush_dcache_all();
280 else {
281 struct vm_area_struct *vma;
282
283 /*
284 * In this case there are reasonably sized ranges to flush,
285 * iterate through the VMA list and take care of any aliases.
286 */
287 for (vma = mm->mmap; vma; vma = vma->vm_next)
288 __flush_cache_mm(mm, vma->vm_start, vma->vm_end);
289 }
290
291 /* Only touch the icache if one of the VMAs has VM_EXEC set. */
292 if (mm->exec_vm)
293 flush_icache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/*
297 * Write back and invalidate I/D-caches for the page.
298 *
299 * ADDR: Virtual Address (U0 address)
300 * PFN: Physical page number
301 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900302static void sh4_flush_cache_page(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900304 struct flusher_data *data = args;
305 struct vm_area_struct *vma;
306 unsigned long address, pfn, phys;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900307 unsigned int alias_mask;
308
Paul Mundtf26b2a52009-08-21 17:23:14 +0900309 vma = data->vma;
310 address = data->addr1;
311 pfn = data->addr2;
312 phys = pfn << PAGE_SHIFT;
313
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900314 if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
315 return;
316
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900317 alias_mask = boot_cpu_data.dcache.alias_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /* We only need to flush D-cache when we have alias */
Richard Curnowb638d0b2006-09-27 14:09:26 +0900320 if ((address^phys) & alias_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* Loop 4K of the D-cache */
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900322 flush_cache_one(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900323 CACHE_OC_ADDRESS_ARRAY | (address & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 phys);
325 /* Loop another 4K of the D-cache */
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900326 flush_cache_one(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900327 CACHE_OC_ADDRESS_ARRAY | (phys & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 phys);
329 }
330
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900331 alias_mask = boot_cpu_data.icache.alias_mask;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900332 if (vma->vm_flags & VM_EXEC) {
333 /*
334 * Evict entries from the portion of the cache from which code
335 * may have been executed at this address (virtual). There's
336 * no need to evict from the portion corresponding to the
337 * physical address as for the D-cache, because we know the
338 * kernel has never executed the code through its identity
339 * translation.
340 */
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900341 flush_cache_one(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900342 CACHE_IC_ADDRESS_ARRAY | (address & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 phys);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347/*
348 * Write back and invalidate D-caches.
349 *
350 * START, END: Virtual Address (U0 address)
351 *
352 * NOTE: We need to flush the _physical_ page entry.
353 * Flushing the cache lines for U0 only isn't enough.
354 * We need to flush for P1 too, which may contain aliases.
355 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900356static void sh4_flush_cache_range(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900358 struct flusher_data *data = args;
359 struct vm_area_struct *vma;
360 unsigned long start, end;
361
362 vma = data->vma;
363 start = data->addr1;
364 end = data->addr2;
365
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900366 if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
367 return;
368
Richard Curnowb638d0b2006-09-27 14:09:26 +0900369 /*
370 * If cache is only 4k-per-way, there are never any 'aliases'. Since
371 * the cache is physically tagged, the data can just be left in there.
372 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900373 if (boot_cpu_data.dcache.n_aliases == 0)
Richard Curnowb638d0b2006-09-27 14:09:26 +0900374 return;
375
Paul Mundta2527102006-09-27 11:29:55 +0900376 /*
377 * Don't bother with the lookup and alias check if we have a
378 * wide range to cover, just blow away the dcache in its
379 * entirety instead. -- PFM.
380 */
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900381 if (((end - start) >> PAGE_SHIFT) >= MAX_DCACHE_PAGES)
Paul Mundta2527102006-09-27 11:29:55 +0900382 flush_dcache_all();
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900383 else
384 __flush_cache_mm(vma->vm_mm, start, end);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900385
386 if (vma->vm_flags & VM_EXEC) {
387 /*
388 * TODO: Is this required??? Need to look at how I-cache
389 * coherency is assured when new programs are loaded to see if
390 * this matters.
391 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 flush_icache_all();
Richard Curnowb638d0b2006-09-27 14:09:26 +0900393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
Richard Curnowb638d0b2006-09-27 14:09:26 +0900396/**
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900397 * __flush_cache_one
Richard Curnowb638d0b2006-09-27 14:09:26 +0900398 *
399 * @addr: address in memory mapped cache array
400 * @phys: P1 address to flush (has to match tags if addr has 'A' bit
401 * set i.e. associative write)
402 * @exec_offset: set to 0x20000000 if flush has to be executed from P2
403 * region else 0x0
404 *
405 * The offset into the cache array implied by 'addr' selects the
406 * 'colour' of the virtual address range that will be flushed. The
407 * operation (purge/write-back) is selected by the lower 2 bits of
408 * 'phys'.
409 */
Valentin Sitdikova7a7c0e2009-10-16 14:15:38 +0900410static void __flush_cache_one(unsigned long addr, unsigned long phys,
Richard Curnowb638d0b2006-09-27 14:09:26 +0900411 unsigned long exec_offset)
412{
413 int way_count;
414 unsigned long base_addr = addr;
415 struct cache_info *dcache;
416 unsigned long way_incr;
417 unsigned long a, ea, p;
418 unsigned long temp_pc;
419
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900420 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900421 /* Write this way for better assembly. */
422 way_count = dcache->ways;
423 way_incr = dcache->way_incr;
424
425 /*
426 * Apply exec_offset (i.e. branch to P2 if required.).
427 *
428 * FIXME:
429 *
430 * If I write "=r" for the (temp_pc), it puts this in r6 hence
431 * trashing exec_offset before it's been added on - why? Hence
432 * "=&r" as a 'workaround'
433 */
434 asm volatile("mov.l 1f, %0\n\t"
435 "add %1, %0\n\t"
436 "jmp @%0\n\t"
437 "nop\n\t"
438 ".balign 4\n\t"
439 "1: .long 2f\n\t"
440 "2:\n" : "=&r" (temp_pc) : "r" (exec_offset));
441
442 /*
443 * We know there will be >=1 iteration, so write as do-while to avoid
444 * pointless nead-of-loop check for 0 iterations.
445 */
446 do {
447 ea = base_addr + PAGE_SIZE;
448 a = base_addr;
449 p = phys;
450
451 do {
452 *(volatile unsigned long *)a = p;
453 /*
454 * Next line: intentionally not p+32, saves an add, p
455 * will do since only the cache tag bits need to
456 * match.
457 */
458 *(volatile unsigned long *)(a+32) = p;
459 a += 64;
460 p += 64;
461 } while (a < ea);
462
463 base_addr += way_incr;
464 } while (--way_count != 0);
465}
466
467/*
468 * Break the 1, 2 and 4 way variants of this out into separate functions to
469 * avoid nearly all the overhead of having the conditional stuff in the function
470 * bodies (+ the 1 and 2 way cases avoid saving any registers too).
Stuart Menefya5cf9e22009-08-24 17:36:24 +0900471 *
472 * We want to eliminate unnecessary bus transactions, so this code uses
473 * a non-obvious technique.
474 *
475 * Loop over a cache way sized block of, one cache line at a time. For each
476 * line, use movca.a to cause the current cache line contents to be written
477 * back, but without reading anything from main memory. However this has the
478 * side effect that the cache is now caching that memory location. So follow
479 * this with a cache invalidate to mark the cache line invalid. And do all
480 * this with interrupts disabled, to avoid the cache line being accidently
481 * evicted while it is holding garbage.
Stuart Menefyffad9d72009-08-24 18:39:39 +0900482 *
483 * This also breaks in a number of circumstances:
484 * - if there are modifications to the region of memory just above
485 * empty_zero_page (for example because a breakpoint has been placed
486 * there), then these can be lost.
487 *
488 * This is because the the memory address which the cache temporarily
489 * caches in the above description is empty_zero_page. So the
490 * movca.l hits the cache (it is assumed that it misses, or at least
491 * isn't dirty), modifies the line and then invalidates it, losing the
492 * required change.
493 *
494 * - If caches are disabled or configured in write-through mode, then
495 * the movca.l writes garbage directly into memory.
Richard Curnowb638d0b2006-09-27 14:09:26 +0900496 */
Matt Flemingce3f7cb2009-09-01 13:32:48 +0900497static void __flush_dcache_segment_writethrough(unsigned long start,
498 unsigned long extent_per_way)
499{
500 unsigned long addr;
501 int i;
502
503 addr = CACHE_OC_ADDRESS_ARRAY | (start & cpu_data->dcache.entry_mask);
504
505 while (extent_per_way) {
506 for (i = 0; i < cpu_data->dcache.ways; i++)
507 __raw_writel(0, addr + cpu_data->dcache.way_incr * i);
508
509 addr += cpu_data->dcache.linesz;
510 extent_per_way -= cpu_data->dcache.linesz;
511 }
512}
513
Richard Curnowb638d0b2006-09-27 14:09:26 +0900514static void __flush_dcache_segment_1way(unsigned long start,
515 unsigned long extent_per_way)
516{
517 unsigned long orig_sr, sr_with_bl;
518 unsigned long base_addr;
519 unsigned long way_incr, linesz, way_size;
520 struct cache_info *dcache;
521 register unsigned long a0, a0e;
522
523 asm volatile("stc sr, %0" : "=r" (orig_sr));
524 sr_with_bl = orig_sr | (1<<28);
525 base_addr = ((unsigned long)&empty_zero_page[0]);
526
527 /*
528 * The previous code aligned base_addr to 16k, i.e. the way_size of all
529 * existing SH-4 D-caches. Whilst I don't see a need to have this
530 * aligned to any better than the cache line size (which it will be
531 * anyway by construction), let's align it to at least the way_size of
532 * any existing or conceivable SH-4 D-cache. -- RPC
533 */
534 base_addr = ((base_addr >> 16) << 16);
535 base_addr |= start;
536
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900537 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900538 linesz = dcache->linesz;
539 way_incr = dcache->way_incr;
540 way_size = dcache->way_size;
541
542 a0 = base_addr;
543 a0e = base_addr + extent_per_way;
544 do {
545 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
546 asm volatile("movca.l r0, @%0\n\t"
547 "ocbi @%0" : : "r" (a0));
548 a0 += linesz;
549 asm volatile("movca.l r0, @%0\n\t"
550 "ocbi @%0" : : "r" (a0));
551 a0 += linesz;
552 asm volatile("movca.l r0, @%0\n\t"
553 "ocbi @%0" : : "r" (a0));
554 a0 += linesz;
555 asm volatile("movca.l r0, @%0\n\t"
556 "ocbi @%0" : : "r" (a0));
557 asm volatile("ldc %0, sr" : : "r" (orig_sr));
558 a0 += linesz;
559 } while (a0 < a0e);
560}
561
562static void __flush_dcache_segment_2way(unsigned long start,
563 unsigned long extent_per_way)
564{
565 unsigned long orig_sr, sr_with_bl;
566 unsigned long base_addr;
567 unsigned long way_incr, linesz, way_size;
568 struct cache_info *dcache;
569 register unsigned long a0, a1, a0e;
570
571 asm volatile("stc sr, %0" : "=r" (orig_sr));
572 sr_with_bl = orig_sr | (1<<28);
573 base_addr = ((unsigned long)&empty_zero_page[0]);
574
575 /* See comment under 1-way above */
576 base_addr = ((base_addr >> 16) << 16);
577 base_addr |= start;
578
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900579 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900580 linesz = dcache->linesz;
581 way_incr = dcache->way_incr;
582 way_size = dcache->way_size;
583
584 a0 = base_addr;
585 a1 = a0 + way_incr;
586 a0e = base_addr + extent_per_way;
587 do {
588 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
589 asm volatile("movca.l r0, @%0\n\t"
590 "movca.l r0, @%1\n\t"
591 "ocbi @%0\n\t"
592 "ocbi @%1" : :
593 "r" (a0), "r" (a1));
594 a0 += linesz;
595 a1 += linesz;
596 asm volatile("movca.l r0, @%0\n\t"
597 "movca.l r0, @%1\n\t"
598 "ocbi @%0\n\t"
599 "ocbi @%1" : :
600 "r" (a0), "r" (a1));
601 a0 += linesz;
602 a1 += linesz;
603 asm volatile("movca.l r0, @%0\n\t"
604 "movca.l r0, @%1\n\t"
605 "ocbi @%0\n\t"
606 "ocbi @%1" : :
607 "r" (a0), "r" (a1));
608 a0 += linesz;
609 a1 += linesz;
610 asm volatile("movca.l r0, @%0\n\t"
611 "movca.l r0, @%1\n\t"
612 "ocbi @%0\n\t"
613 "ocbi @%1" : :
614 "r" (a0), "r" (a1));
615 asm volatile("ldc %0, sr" : : "r" (orig_sr));
616 a0 += linesz;
617 a1 += linesz;
618 } while (a0 < a0e);
619}
620
621static void __flush_dcache_segment_4way(unsigned long start,
622 unsigned long extent_per_way)
623{
624 unsigned long orig_sr, sr_with_bl;
625 unsigned long base_addr;
626 unsigned long way_incr, linesz, way_size;
627 struct cache_info *dcache;
628 register unsigned long a0, a1, a2, a3, a0e;
629
630 asm volatile("stc sr, %0" : "=r" (orig_sr));
631 sr_with_bl = orig_sr | (1<<28);
632 base_addr = ((unsigned long)&empty_zero_page[0]);
633
634 /* See comment under 1-way above */
635 base_addr = ((base_addr >> 16) << 16);
636 base_addr |= start;
637
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900638 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900639 linesz = dcache->linesz;
640 way_incr = dcache->way_incr;
641 way_size = dcache->way_size;
642
643 a0 = base_addr;
644 a1 = a0 + way_incr;
645 a2 = a1 + way_incr;
646 a3 = a2 + way_incr;
647 a0e = base_addr + extent_per_way;
648 do {
649 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
650 asm volatile("movca.l r0, @%0\n\t"
651 "movca.l r0, @%1\n\t"
652 "movca.l r0, @%2\n\t"
653 "movca.l r0, @%3\n\t"
654 "ocbi @%0\n\t"
655 "ocbi @%1\n\t"
656 "ocbi @%2\n\t"
657 "ocbi @%3\n\t" : :
658 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
659 a0 += linesz;
660 a1 += linesz;
661 a2 += linesz;
662 a3 += linesz;
663 asm volatile("movca.l r0, @%0\n\t"
664 "movca.l r0, @%1\n\t"
665 "movca.l r0, @%2\n\t"
666 "movca.l r0, @%3\n\t"
667 "ocbi @%0\n\t"
668 "ocbi @%1\n\t"
669 "ocbi @%2\n\t"
670 "ocbi @%3\n\t" : :
671 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
672 a0 += linesz;
673 a1 += linesz;
674 a2 += linesz;
675 a3 += linesz;
676 asm volatile("movca.l r0, @%0\n\t"
677 "movca.l r0, @%1\n\t"
678 "movca.l r0, @%2\n\t"
679 "movca.l r0, @%3\n\t"
680 "ocbi @%0\n\t"
681 "ocbi @%1\n\t"
682 "ocbi @%2\n\t"
683 "ocbi @%3\n\t" : :
684 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
685 a0 += linesz;
686 a1 += linesz;
687 a2 += linesz;
688 a3 += linesz;
689 asm volatile("movca.l r0, @%0\n\t"
690 "movca.l r0, @%1\n\t"
691 "movca.l r0, @%2\n\t"
692 "movca.l r0, @%3\n\t"
693 "ocbi @%0\n\t"
694 "ocbi @%1\n\t"
695 "ocbi @%2\n\t"
696 "ocbi @%3\n\t" : :
697 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
698 asm volatile("ldc %0, sr" : : "r" (orig_sr));
699 a0 += linesz;
700 a1 += linesz;
701 a2 += linesz;
702 a3 += linesz;
703 } while (a0 < a0e);
704}
Paul Mundt37443ef2009-08-15 12:29:49 +0900705
706extern void __weak sh4__flush_region_init(void);
707
708/*
709 * SH-4 has virtually indexed and physically tagged cache.
710 */
711void __init sh4_cache_init(void)
712{
Paul Mundtac6a0cf2009-09-01 13:54:14 +0900713 unsigned int wt_enabled = !!(__raw_readl(CCR) & CCR_CACHE_WT);
714
Paul Mundt37443ef2009-08-15 12:29:49 +0900715 printk("PVR=%08x CVR=%08x PRR=%08x\n",
716 ctrl_inl(CCN_PVR),
717 ctrl_inl(CCN_CVR),
718 ctrl_inl(CCN_PRR));
719
Paul Mundtac6a0cf2009-09-01 13:54:14 +0900720 if (wt_enabled)
721 __flush_dcache_segment_fn = __flush_dcache_segment_writethrough;
722 else {
723 switch (boot_cpu_data.dcache.ways) {
724 case 1:
725 __flush_dcache_segment_fn = __flush_dcache_segment_1way;
726 break;
727 case 2:
728 __flush_dcache_segment_fn = __flush_dcache_segment_2way;
729 break;
730 case 4:
731 __flush_dcache_segment_fn = __flush_dcache_segment_4way;
732 break;
733 default:
734 panic("unknown number of cache ways\n");
735 break;
736 }
Paul Mundt37443ef2009-08-15 12:29:49 +0900737 }
738
Paul Mundtf26b2a52009-08-21 17:23:14 +0900739 local_flush_icache_range = sh4_flush_icache_range;
740 local_flush_dcache_page = sh4_flush_dcache_page;
741 local_flush_cache_all = sh4_flush_cache_all;
742 local_flush_cache_mm = sh4_flush_cache_mm;
743 local_flush_cache_dup_mm = sh4_flush_cache_mm;
744 local_flush_cache_page = sh4_flush_cache_page;
745 local_flush_cache_range = sh4_flush_cache_range;
Paul Mundt37443ef2009-08-15 12:29:49 +0900746
747 sh4__flush_region_init();
748}