blob: 9201b37c7cca7d501d2262890a6ca3216642c80a [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
Richard Curnowb638d0b2006-09-27 14:09:26 +090029static void __flush_cache_4096(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 */
Paul Mundtf26b2a52009-08-21 17:23:14 +090046static void 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;
Chris Smith09b5a102008-07-02 15:17:11 +090049 int icacheaddr;
Paul Mundtf26b2a52009-08-21 17:23:14 +090050 unsigned long start, end;
Chris Smith09b5a102008-07-02 15:17:11 +090051 unsigned long flags, v;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int i;
53
Paul Mundtf26b2a52009-08-21 17:23:14 +090054 start = data->addr1;
55 end = data->addr2;
56
Chris Smith09b5a102008-07-02 15:17:11 +090057 /* If there are too many pages then just blow the caches */
58 if (((end - start) >> PAGE_SHIFT) >= MAX_ICACHE_PAGES) {
Paul Mundtf26b2a52009-08-21 17:23:14 +090059 local_flush_cache_all(args);
Chris Smith09b5a102008-07-02 15:17:11 +090060 } else {
61 /* selectively flush d-cache then invalidate the i-cache */
62 /* this is inefficient, so only use for small ranges */
63 start &= ~(L1_CACHE_BYTES-1);
64 end += L1_CACHE_BYTES-1;
65 end &= ~(L1_CACHE_BYTES-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Chris Smith09b5a102008-07-02 15:17:11 +090067 local_irq_save(flags);
68 jump_to_uncached();
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Chris Smith09b5a102008-07-02 15:17:11 +090070 for (v = start; v < end; v+=L1_CACHE_BYTES) {
71 asm volatile("ocbwb %0"
72 : /* no output */
73 : "m" (__m(v)));
Richard Curnowb638d0b2006-09-27 14:09:26 +090074
Chris Smith09b5a102008-07-02 15:17:11 +090075 icacheaddr = CACHE_IC_ADDRESS_ARRAY | (
76 v & cpu_data->icache.entry_mask);
Richard Curnowb638d0b2006-09-27 14:09:26 +090077
Chris Smith09b5a102008-07-02 15:17:11 +090078 for (i = 0; i < cpu_data->icache.ways;
79 i++, icacheaddr += cpu_data->icache.way_incr)
80 /* Clear i-cache line valid-bit */
81 ctrl_outl(0, icacheaddr);
82 }
83
84 back_to_cached();
85 local_irq_restore(flags);
86 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
89static inline void flush_cache_4096(unsigned long start,
90 unsigned long phys)
91{
Paul Mundt33573c02006-09-27 18:37:30 +090092 unsigned long flags, exec_offset = 0;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /*
Richard Curnowb638d0b2006-09-27 14:09:26 +090095 * All types of SH-4 require PC to be in P2 to operate on the I-cache.
96 * Some types of SH-4 require PC to be in P2 to operate on the D-cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +090098 if ((boot_cpu_data.flags & CPU_HAS_P2_FLUSH_BUG) ||
Paul Mundt33573c02006-09-27 18:37:30 +090099 (start < CACHE_OC_ADDRESS_ARRAY))
Paul Mundt510c72ad2006-11-27 12:06:26 +0900100 exec_offset = 0x20000000;
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900101
Paul Mundt33573c02006-09-27 18:37:30 +0900102 local_irq_save(flags);
103 __flush_cache_4096(start | SH_CACHE_ASSOC,
104 P1SEGADDR(phys), exec_offset);
105 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/*
109 * Write back & invalidate the D-cache of the page.
110 * (To avoid "alias" issues)
111 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900112static void sh4_flush_dcache_page(void *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Paul Mundtc139a592009-08-20 15:24:41 +0900114#ifndef CONFIG_SMP
Paul Mundt2277ab42009-07-22 19:20:49 +0900115 struct address_space *mapping = page_mapping(page);
116
Paul Mundt2277ab42009-07-22 19:20:49 +0900117 if (mapping && !mapping_mapped(mapping))
118 set_bit(PG_dcache_dirty, &page->flags);
119 else
120#endif
121 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 unsigned long phys = PHYSADDR(page_address(page));
Richard Curnowb638d0b2006-09-27 14:09:26 +0900123 unsigned long addr = CACHE_OC_ADDRESS_ARRAY;
124 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 /* Loop all the D-cache */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900127 n = boot_cpu_data.dcache.n_aliases;
Paul Mundt510c72ad2006-11-27 12:06:26 +0900128 for (i = 0; i < n; i++, addr += 4096)
Richard Curnowb638d0b2006-09-27 14:09:26 +0900129 flush_cache_4096(addr, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 }
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900131
132 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900135/* TODO: Selective icache invalidation through IC address array.. */
Paul Mundt205a3b42008-09-05 18:00:29 +0900136static void __uses_jump_to_uncached flush_icache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
138 unsigned long flags, ccr;
139
140 local_irq_save(flags);
Stuart Menefycbaa1182007-11-30 17:06:36 +0900141 jump_to_uncached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143 /* Flush I-cache */
144 ccr = ctrl_inl(CCR);
145 ccr |= CCR_CACHE_ICI;
146 ctrl_outl(ccr, CCR);
147
Paul Mundt29847622006-09-27 14:57:44 +0900148 /*
Stuart Menefycbaa1182007-11-30 17:06:36 +0900149 * back_to_cached() will take care of the barrier for us, don't add
Paul Mundt29847622006-09-27 14:57:44 +0900150 * another one!
151 */
152
Stuart Menefycbaa1182007-11-30 17:06:36 +0900153 back_to_cached();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 local_irq_restore(flags);
155}
156
Paul Mundt0b445dc2009-08-15 11:22:50 +0900157static inline void flush_dcache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900159 (*__flush_dcache_segment_fn)(0UL, boot_cpu_data.dcache.way_size);
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900160 wmb();
Paul Mundta2527102006-09-27 11:29:55 +0900161}
162
Paul Mundtf26b2a52009-08-21 17:23:14 +0900163static void sh4_flush_cache_all(void *unused)
Paul Mundta2527102006-09-27 11:29:55 +0900164{
165 flush_dcache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 flush_icache_all();
167}
168
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900169static void __flush_cache_mm(struct mm_struct *mm, unsigned long start,
170 unsigned long end)
171{
172 unsigned long d = 0, p = start & PAGE_MASK;
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900173 unsigned long alias_mask = boot_cpu_data.dcache.alias_mask;
174 unsigned long n_aliases = boot_cpu_data.dcache.n_aliases;
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900175 unsigned long select_bit;
176 unsigned long all_aliases_mask;
177 unsigned long addr_offset;
178 pgd_t *dir;
179 pmd_t *pmd;
180 pud_t *pud;
181 pte_t *pte;
182 int i;
183
184 dir = pgd_offset(mm, p);
185 pud = pud_offset(dir, p);
186 pmd = pmd_offset(pud, p);
187 end = PAGE_ALIGN(end);
188
189 all_aliases_mask = (1 << n_aliases) - 1;
190
191 do {
192 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) {
193 p &= PMD_MASK;
194 p += PMD_SIZE;
195 pmd++;
196
197 continue;
198 }
199
200 pte = pte_offset_kernel(pmd, p);
201
202 do {
203 unsigned long phys;
204 pte_t entry = *pte;
205
206 if (!(pte_val(entry) & _PAGE_PRESENT)) {
207 pte++;
208 p += PAGE_SIZE;
209 continue;
210 }
211
212 phys = pte_val(entry) & PTE_PHYS_MASK;
213
214 if ((p ^ phys) & alias_mask) {
215 d |= 1 << ((p & alias_mask) >> PAGE_SHIFT);
216 d |= 1 << ((phys & alias_mask) >> PAGE_SHIFT);
217
218 if (d == all_aliases_mask)
219 goto loop_exit;
220 }
221
222 pte++;
223 p += PAGE_SIZE;
224 } while (p < end && ((unsigned long)pte & ~PAGE_MASK));
225 pmd++;
226 } while (p < end);
227
228loop_exit:
229 addr_offset = 0;
230 select_bit = 1;
231
232 for (i = 0; i < n_aliases; i++) {
233 if (d & select_bit) {
234 (*__flush_dcache_segment_fn)(addr_offset, PAGE_SIZE);
235 wmb();
236 }
237
238 select_bit <<= 1;
239 addr_offset += PAGE_SIZE;
240 }
241}
242
243/*
244 * Note : (RPC) since the caches are physically tagged, the only point
245 * of flush_cache_mm for SH-4 is to get rid of aliases from the
246 * D-cache. The assumption elsewhere, e.g. flush_cache_range, is that
247 * lines can stay resident so long as the virtual address they were
248 * accessed with (hence cache set) is in accord with the physical
249 * address (i.e. tag). It's no different here. So I reckon we don't
250 * need to flush the I-cache, since aliases don't matter for that. We
251 * should try that.
252 *
253 * Caller takes mm->mmap_sem.
254 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900255static void sh4_flush_cache_mm(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900257 struct mm_struct *mm = arg;
258
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900259 if (cpu_context(smp_processor_id(), mm) == NO_CONTEXT)
260 return;
261
Richard Curnowb638d0b2006-09-27 14:09:26 +0900262 /*
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900263 * If cache is only 4k-per-way, there are never any 'aliases'. Since
264 * the cache is physically tagged, the data can just be left in there.
Richard Curnowb638d0b2006-09-27 14:09:26 +0900265 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900266 if (boot_cpu_data.dcache.n_aliases == 0)
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900267 return;
268
269 /*
270 * Don't bother groveling around the dcache for the VMA ranges
271 * if there are too many PTEs to make it worthwhile.
272 */
273 if (mm->nr_ptes >= MAX_DCACHE_PAGES)
274 flush_dcache_all();
275 else {
276 struct vm_area_struct *vma;
277
278 /*
279 * In this case there are reasonably sized ranges to flush,
280 * iterate through the VMA list and take care of any aliases.
281 */
282 for (vma = mm->mmap; vma; vma = vma->vm_next)
283 __flush_cache_mm(mm, vma->vm_start, vma->vm_end);
284 }
285
286 /* Only touch the icache if one of the VMAs has VM_EXEC set. */
287 if (mm->exec_vm)
288 flush_icache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291/*
292 * Write back and invalidate I/D-caches for the page.
293 *
294 * ADDR: Virtual Address (U0 address)
295 * PFN: Physical page number
296 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900297static void sh4_flush_cache_page(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900299 struct flusher_data *data = args;
300 struct vm_area_struct *vma;
301 unsigned long address, pfn, phys;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900302 unsigned int alias_mask;
303
Paul Mundtf26b2a52009-08-21 17:23:14 +0900304 vma = data->vma;
305 address = data->addr1;
306 pfn = data->addr2;
307 phys = pfn << PAGE_SHIFT;
308
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900309 if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
310 return;
311
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900312 alias_mask = boot_cpu_data.dcache.alias_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314 /* We only need to flush D-cache when we have alias */
Richard Curnowb638d0b2006-09-27 14:09:26 +0900315 if ((address^phys) & alias_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* Loop 4K of the D-cache */
317 flush_cache_4096(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900318 CACHE_OC_ADDRESS_ARRAY | (address & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 phys);
320 /* Loop another 4K of the D-cache */
321 flush_cache_4096(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900322 CACHE_OC_ADDRESS_ARRAY | (phys & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 phys);
324 }
325
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900326 alias_mask = boot_cpu_data.icache.alias_mask;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900327 if (vma->vm_flags & VM_EXEC) {
328 /*
329 * Evict entries from the portion of the cache from which code
330 * may have been executed at this address (virtual). There's
331 * no need to evict from the portion corresponding to the
332 * physical address as for the D-cache, because we know the
333 * kernel has never executed the code through its identity
334 * translation.
335 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 flush_cache_4096(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900337 CACHE_IC_ADDRESS_ARRAY | (address & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 phys);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900339 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
342/*
343 * Write back and invalidate D-caches.
344 *
345 * START, END: Virtual Address (U0 address)
346 *
347 * NOTE: We need to flush the _physical_ page entry.
348 * Flushing the cache lines for U0 only isn't enough.
349 * We need to flush for P1 too, which may contain aliases.
350 */
Paul Mundtf26b2a52009-08-21 17:23:14 +0900351static void sh4_flush_cache_range(void *args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Paul Mundtf26b2a52009-08-21 17:23:14 +0900353 struct flusher_data *data = args;
354 struct vm_area_struct *vma;
355 unsigned long start, end;
356
357 vma = data->vma;
358 start = data->addr1;
359 end = data->addr2;
360
Paul Mundte7b8b7f2009-08-15 02:21:16 +0900361 if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT)
362 return;
363
Richard Curnowb638d0b2006-09-27 14:09:26 +0900364 /*
365 * If cache is only 4k-per-way, there are never any 'aliases'. Since
366 * the cache is physically tagged, the data can just be left in there.
367 */
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900368 if (boot_cpu_data.dcache.n_aliases == 0)
Richard Curnowb638d0b2006-09-27 14:09:26 +0900369 return;
370
Paul Mundta2527102006-09-27 11:29:55 +0900371 /*
372 * Don't bother with the lookup and alias check if we have a
373 * wide range to cover, just blow away the dcache in its
374 * entirety instead. -- PFM.
375 */
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900376 if (((end - start) >> PAGE_SHIFT) >= MAX_DCACHE_PAGES)
Paul Mundta2527102006-09-27 11:29:55 +0900377 flush_dcache_all();
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900378 else
379 __flush_cache_mm(vma->vm_mm, start, end);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900380
381 if (vma->vm_flags & VM_EXEC) {
382 /*
383 * TODO: Is this required??? Need to look at how I-cache
384 * coherency is assured when new programs are loaded to see if
385 * this matters.
386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 flush_icache_all();
Richard Curnowb638d0b2006-09-27 14:09:26 +0900388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Richard Curnowb638d0b2006-09-27 14:09:26 +0900391/**
392 * __flush_cache_4096
393 *
394 * @addr: address in memory mapped cache array
395 * @phys: P1 address to flush (has to match tags if addr has 'A' bit
396 * set i.e. associative write)
397 * @exec_offset: set to 0x20000000 if flush has to be executed from P2
398 * region else 0x0
399 *
400 * The offset into the cache array implied by 'addr' selects the
401 * 'colour' of the virtual address range that will be flushed. The
402 * operation (purge/write-back) is selected by the lower 2 bits of
403 * 'phys'.
404 */
405static void __flush_cache_4096(unsigned long addr, unsigned long phys,
406 unsigned long exec_offset)
407{
408 int way_count;
409 unsigned long base_addr = addr;
410 struct cache_info *dcache;
411 unsigned long way_incr;
412 unsigned long a, ea, p;
413 unsigned long temp_pc;
414
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900415 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900416 /* Write this way for better assembly. */
417 way_count = dcache->ways;
418 way_incr = dcache->way_incr;
419
420 /*
421 * Apply exec_offset (i.e. branch to P2 if required.).
422 *
423 * FIXME:
424 *
425 * If I write "=r" for the (temp_pc), it puts this in r6 hence
426 * trashing exec_offset before it's been added on - why? Hence
427 * "=&r" as a 'workaround'
428 */
429 asm volatile("mov.l 1f, %0\n\t"
430 "add %1, %0\n\t"
431 "jmp @%0\n\t"
432 "nop\n\t"
433 ".balign 4\n\t"
434 "1: .long 2f\n\t"
435 "2:\n" : "=&r" (temp_pc) : "r" (exec_offset));
436
437 /*
438 * We know there will be >=1 iteration, so write as do-while to avoid
439 * pointless nead-of-loop check for 0 iterations.
440 */
441 do {
442 ea = base_addr + PAGE_SIZE;
443 a = base_addr;
444 p = phys;
445
446 do {
447 *(volatile unsigned long *)a = p;
448 /*
449 * Next line: intentionally not p+32, saves an add, p
450 * will do since only the cache tag bits need to
451 * match.
452 */
453 *(volatile unsigned long *)(a+32) = p;
454 a += 64;
455 p += 64;
456 } while (a < ea);
457
458 base_addr += way_incr;
459 } while (--way_count != 0);
460}
461
462/*
463 * Break the 1, 2 and 4 way variants of this out into separate functions to
464 * avoid nearly all the overhead of having the conditional stuff in the function
465 * bodies (+ the 1 and 2 way cases avoid saving any registers too).
466 */
467static void __flush_dcache_segment_1way(unsigned long start,
468 unsigned long extent_per_way)
469{
470 unsigned long orig_sr, sr_with_bl;
471 unsigned long base_addr;
472 unsigned long way_incr, linesz, way_size;
473 struct cache_info *dcache;
474 register unsigned long a0, a0e;
475
476 asm volatile("stc sr, %0" : "=r" (orig_sr));
477 sr_with_bl = orig_sr | (1<<28);
478 base_addr = ((unsigned long)&empty_zero_page[0]);
479
480 /*
481 * The previous code aligned base_addr to 16k, i.e. the way_size of all
482 * existing SH-4 D-caches. Whilst I don't see a need to have this
483 * aligned to any better than the cache line size (which it will be
484 * anyway by construction), let's align it to at least the way_size of
485 * any existing or conceivable SH-4 D-cache. -- RPC
486 */
487 base_addr = ((base_addr >> 16) << 16);
488 base_addr |= start;
489
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900490 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900491 linesz = dcache->linesz;
492 way_incr = dcache->way_incr;
493 way_size = dcache->way_size;
494
495 a0 = base_addr;
496 a0e = base_addr + extent_per_way;
497 do {
498 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
499 asm volatile("movca.l r0, @%0\n\t"
500 "ocbi @%0" : : "r" (a0));
501 a0 += linesz;
502 asm volatile("movca.l r0, @%0\n\t"
503 "ocbi @%0" : : "r" (a0));
504 a0 += linesz;
505 asm volatile("movca.l r0, @%0\n\t"
506 "ocbi @%0" : : "r" (a0));
507 a0 += linesz;
508 asm volatile("movca.l r0, @%0\n\t"
509 "ocbi @%0" : : "r" (a0));
510 asm volatile("ldc %0, sr" : : "r" (orig_sr));
511 a0 += linesz;
512 } while (a0 < a0e);
513}
514
515static void __flush_dcache_segment_2way(unsigned long start,
516 unsigned long extent_per_way)
517{
518 unsigned long orig_sr, sr_with_bl;
519 unsigned long base_addr;
520 unsigned long way_incr, linesz, way_size;
521 struct cache_info *dcache;
522 register unsigned long a0, a1, a0e;
523
524 asm volatile("stc sr, %0" : "=r" (orig_sr));
525 sr_with_bl = orig_sr | (1<<28);
526 base_addr = ((unsigned long)&empty_zero_page[0]);
527
528 /* See comment under 1-way above */
529 base_addr = ((base_addr >> 16) << 16);
530 base_addr |= start;
531
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900532 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900533 linesz = dcache->linesz;
534 way_incr = dcache->way_incr;
535 way_size = dcache->way_size;
536
537 a0 = base_addr;
538 a1 = a0 + way_incr;
539 a0e = base_addr + extent_per_way;
540 do {
541 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
542 asm volatile("movca.l r0, @%0\n\t"
543 "movca.l r0, @%1\n\t"
544 "ocbi @%0\n\t"
545 "ocbi @%1" : :
546 "r" (a0), "r" (a1));
547 a0 += linesz;
548 a1 += linesz;
549 asm volatile("movca.l r0, @%0\n\t"
550 "movca.l r0, @%1\n\t"
551 "ocbi @%0\n\t"
552 "ocbi @%1" : :
553 "r" (a0), "r" (a1));
554 a0 += linesz;
555 a1 += linesz;
556 asm volatile("movca.l r0, @%0\n\t"
557 "movca.l r0, @%1\n\t"
558 "ocbi @%0\n\t"
559 "ocbi @%1" : :
560 "r" (a0), "r" (a1));
561 a0 += linesz;
562 a1 += linesz;
563 asm volatile("movca.l r0, @%0\n\t"
564 "movca.l r0, @%1\n\t"
565 "ocbi @%0\n\t"
566 "ocbi @%1" : :
567 "r" (a0), "r" (a1));
568 asm volatile("ldc %0, sr" : : "r" (orig_sr));
569 a0 += linesz;
570 a1 += linesz;
571 } while (a0 < a0e);
572}
573
574static void __flush_dcache_segment_4way(unsigned long start,
575 unsigned long extent_per_way)
576{
577 unsigned long orig_sr, sr_with_bl;
578 unsigned long base_addr;
579 unsigned long way_incr, linesz, way_size;
580 struct cache_info *dcache;
581 register unsigned long a0, a1, a2, a3, a0e;
582
583 asm volatile("stc sr, %0" : "=r" (orig_sr));
584 sr_with_bl = orig_sr | (1<<28);
585 base_addr = ((unsigned long)&empty_zero_page[0]);
586
587 /* See comment under 1-way above */
588 base_addr = ((base_addr >> 16) << 16);
589 base_addr |= start;
590
Paul Mundt7ec9d6f2007-09-21 18:05:20 +0900591 dcache = &boot_cpu_data.dcache;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900592 linesz = dcache->linesz;
593 way_incr = dcache->way_incr;
594 way_size = dcache->way_size;
595
596 a0 = base_addr;
597 a1 = a0 + way_incr;
598 a2 = a1 + way_incr;
599 a3 = a2 + way_incr;
600 a0e = base_addr + extent_per_way;
601 do {
602 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
603 asm volatile("movca.l r0, @%0\n\t"
604 "movca.l r0, @%1\n\t"
605 "movca.l r0, @%2\n\t"
606 "movca.l r0, @%3\n\t"
607 "ocbi @%0\n\t"
608 "ocbi @%1\n\t"
609 "ocbi @%2\n\t"
610 "ocbi @%3\n\t" : :
611 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
612 a0 += linesz;
613 a1 += linesz;
614 a2 += linesz;
615 a3 += linesz;
616 asm volatile("movca.l r0, @%0\n\t"
617 "movca.l r0, @%1\n\t"
618 "movca.l r0, @%2\n\t"
619 "movca.l r0, @%3\n\t"
620 "ocbi @%0\n\t"
621 "ocbi @%1\n\t"
622 "ocbi @%2\n\t"
623 "ocbi @%3\n\t" : :
624 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
625 a0 += linesz;
626 a1 += linesz;
627 a2 += linesz;
628 a3 += linesz;
629 asm volatile("movca.l r0, @%0\n\t"
630 "movca.l r0, @%1\n\t"
631 "movca.l r0, @%2\n\t"
632 "movca.l r0, @%3\n\t"
633 "ocbi @%0\n\t"
634 "ocbi @%1\n\t"
635 "ocbi @%2\n\t"
636 "ocbi @%3\n\t" : :
637 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
638 a0 += linesz;
639 a1 += linesz;
640 a2 += linesz;
641 a3 += linesz;
642 asm volatile("movca.l r0, @%0\n\t"
643 "movca.l r0, @%1\n\t"
644 "movca.l r0, @%2\n\t"
645 "movca.l r0, @%3\n\t"
646 "ocbi @%0\n\t"
647 "ocbi @%1\n\t"
648 "ocbi @%2\n\t"
649 "ocbi @%3\n\t" : :
650 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
651 asm volatile("ldc %0, sr" : : "r" (orig_sr));
652 a0 += linesz;
653 a1 += linesz;
654 a2 += linesz;
655 a3 += linesz;
656 } while (a0 < a0e);
657}
Paul Mundt37443ef2009-08-15 12:29:49 +0900658
659extern void __weak sh4__flush_region_init(void);
660
661/*
662 * SH-4 has virtually indexed and physically tagged cache.
663 */
664void __init sh4_cache_init(void)
665{
666 printk("PVR=%08x CVR=%08x PRR=%08x\n",
667 ctrl_inl(CCN_PVR),
668 ctrl_inl(CCN_CVR),
669 ctrl_inl(CCN_PRR));
670
671 switch (boot_cpu_data.dcache.ways) {
672 case 1:
673 __flush_dcache_segment_fn = __flush_dcache_segment_1way;
674 break;
675 case 2:
676 __flush_dcache_segment_fn = __flush_dcache_segment_2way;
677 break;
678 case 4:
679 __flush_dcache_segment_fn = __flush_dcache_segment_4way;
680 break;
681 default:
682 panic("unknown number of cache ways\n");
683 break;
684 }
685
Paul Mundtf26b2a52009-08-21 17:23:14 +0900686 local_flush_icache_range = sh4_flush_icache_range;
687 local_flush_dcache_page = sh4_flush_dcache_page;
688 local_flush_cache_all = sh4_flush_cache_all;
689 local_flush_cache_mm = sh4_flush_cache_mm;
690 local_flush_cache_dup_mm = sh4_flush_cache_mm;
691 local_flush_cache_page = sh4_flush_cache_page;
692 local_flush_cache_range = sh4_flush_cache_range;
Paul Mundt37443ef2009-08-15 12:29:49 +0900693
694 sh4__flush_region_init();
695}