blob: e48cc22724d9e8d7d215544b4904f977c2551418 [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 Mundt28ccf7f2006-09-27 18:30:07 +09005 * Copyright (C) 2001 - 2006 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Copyright (C) 2003 Richard Curnow
7 *
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
10 * for more details.
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/addrspace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <asm/pgtable.h>
16#include <asm/processor.h>
17#include <asm/cache.h>
18#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/pgalloc.h>
20#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 */
28#define MAX_DCACHE_PAGES 64 /* XXX: Tune for ways */
29
Richard Curnowb638d0b2006-09-27 14:09:26 +090030static void __flush_dcache_segment_1way(unsigned long start,
31 unsigned long extent);
32static void __flush_dcache_segment_2way(unsigned long start,
33 unsigned long extent);
34static void __flush_dcache_segment_4way(unsigned long start,
35 unsigned long extent);
36
37static void __flush_cache_4096(unsigned long addr, unsigned long phys,
Paul Mundta2527102006-09-27 11:29:55 +090038 unsigned long exec_offset);
Richard Curnowb638d0b2006-09-27 14:09:26 +090039
40/*
41 * This is initialised here to ensure that it is not placed in the BSS. If
42 * that were to happen, note that cache_init gets called before the BSS is
43 * cleared, so this would get nulled out which would be hopeless.
44 */
45static void (*__flush_dcache_segment_fn)(unsigned long, unsigned long) =
46 (void (*)(unsigned long, unsigned long))0xdeadbeef;
47
48static void compute_alias(struct cache_info *c)
49{
50 c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1);
51 c->n_aliases = (c->alias_mask >> PAGE_SHIFT) + 1;
52}
53
54static void __init emit_cache_params(void)
55{
56 printk("PVR=%08x CVR=%08x PRR=%08x\n",
57 ctrl_inl(CCN_PVR),
58 ctrl_inl(CCN_CVR),
59 ctrl_inl(CCN_PRR));
60 printk("I-cache : n_ways=%d n_sets=%d way_incr=%d\n",
61 cpu_data->icache.ways,
62 cpu_data->icache.sets,
63 cpu_data->icache.way_incr);
64 printk("I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
65 cpu_data->icache.entry_mask,
66 cpu_data->icache.alias_mask,
67 cpu_data->icache.n_aliases);
68 printk("D-cache : n_ways=%d n_sets=%d way_incr=%d\n",
69 cpu_data->dcache.ways,
70 cpu_data->dcache.sets,
71 cpu_data->dcache.way_incr);
72 printk("D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
73 cpu_data->dcache.entry_mask,
74 cpu_data->dcache.alias_mask,
75 cpu_data->dcache.n_aliases);
76
77 if (!__flush_dcache_segment_fn)
78 panic("unknown number of cache ways\n");
79}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81/*
82 * SH-4 has virtually indexed and physically tagged cache.
83 */
84
Richard Curnowb638d0b2006-09-27 14:09:26 +090085/* Worst case assumed to be 64k cache, direct-mapped i.e. 4 synonym bits. */
86#define MAX_P3_SEMAPHORES 16
87
88struct semaphore p3map_sem[MAX_P3_SEMAPHORES];
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90void __init p3_cache_init(void)
91{
Richard Curnowb638d0b2006-09-27 14:09:26 +090092 int i;
93
94 compute_alias(&cpu_data->icache);
95 compute_alias(&cpu_data->dcache);
96
97 switch (cpu_data->dcache.ways) {
98 case 1:
99 __flush_dcache_segment_fn = __flush_dcache_segment_1way;
100 break;
101 case 2:
102 __flush_dcache_segment_fn = __flush_dcache_segment_2way;
103 break;
104 case 4:
105 __flush_dcache_segment_fn = __flush_dcache_segment_4way;
106 break;
107 default:
108 __flush_dcache_segment_fn = NULL;
109 break;
110 }
111
112 emit_cache_params();
113
114 if (remap_area_pages(P3SEG, 0, PAGE_SIZE * 4, _PAGE_CACHABLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 panic("%s failed.", __FUNCTION__);
116
Richard Curnowb638d0b2006-09-27 14:09:26 +0900117 for (i = 0; i < cpu_data->dcache.n_aliases; i++)
118 sema_init(&p3map_sem[i], 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
121/*
122 * Write back the dirty D-caches, but not invalidate them.
123 *
124 * START: Virtual Address (U0, P1, or P3)
125 * SIZE: Size of the region.
126 */
127void __flush_wback_region(void *start, int size)
128{
129 unsigned long v;
130 unsigned long begin, end;
131
132 begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
133 end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
134 & ~(L1_CACHE_BYTES-1);
135 for (v = begin; v < end; v+=L1_CACHE_BYTES) {
136 asm volatile("ocbwb %0"
137 : /* no output */
138 : "m" (__m(v)));
139 }
140}
141
142/*
143 * Write back the dirty D-caches and invalidate them.
144 *
145 * START: Virtual Address (U0, P1, or P3)
146 * SIZE: Size of the region.
147 */
148void __flush_purge_region(void *start, int size)
149{
150 unsigned long v;
151 unsigned long begin, end;
152
153 begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
154 end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
155 & ~(L1_CACHE_BYTES-1);
156 for (v = begin; v < end; v+=L1_CACHE_BYTES) {
157 asm volatile("ocbp %0"
158 : /* no output */
159 : "m" (__m(v)));
160 }
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
164 * No write back please
165 */
166void __flush_invalidate_region(void *start, int size)
167{
168 unsigned long v;
169 unsigned long begin, end;
170
171 begin = (unsigned long)start & ~(L1_CACHE_BYTES-1);
172 end = ((unsigned long)start + size + L1_CACHE_BYTES-1)
173 & ~(L1_CACHE_BYTES-1);
174 for (v = begin; v < end; v+=L1_CACHE_BYTES) {
175 asm volatile("ocbi %0"
176 : /* no output */
177 : "m" (__m(v)));
178 }
179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*
182 * Write back the range of D-cache, and purge the I-cache.
183 *
184 * Called from kernel/module.c:sys_init_module and routine for a.out format.
185 */
186void flush_icache_range(unsigned long start, unsigned long end)
187{
188 flush_cache_all();
189}
190
191/*
Paul Mundta2527102006-09-27 11:29:55 +0900192 * Write back the D-cache and purge the I-cache for signal trampoline.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * .. which happens to be the same behavior as flush_icache_range().
194 * So, we simply flush out a line.
195 */
196void flush_cache_sigtramp(unsigned long addr)
197{
198 unsigned long v, index;
Paul Mundta2527102006-09-27 11:29:55 +0900199 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int i;
201
202 v = addr & ~(L1_CACHE_BYTES-1);
203 asm volatile("ocbwb %0"
204 : /* no output */
205 : "m" (__m(v)));
206
207 index = CACHE_IC_ADDRESS_ARRAY | (v & cpu_data->icache.entry_mask);
208
209 local_irq_save(flags);
210 jump_to_P2();
Richard Curnowb638d0b2006-09-27 14:09:26 +0900211
Paul Mundta2527102006-09-27 11:29:55 +0900212 for (i = 0; i < cpu_data->icache.ways;
213 i++, index += cpu_data->icache.way_incr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ctrl_outl(0, index); /* Clear out Valid-bit */
Richard Curnowb638d0b2006-09-27 14:09:26 +0900215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 back_to_P1();
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900217 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 local_irq_restore(flags);
219}
220
221static inline void flush_cache_4096(unsigned long start,
222 unsigned long phys)
223{
Paul Mundt33573c02006-09-27 18:37:30 +0900224 unsigned long flags, exec_offset = 0;
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /*
Richard Curnowb638d0b2006-09-27 14:09:26 +0900227 * All types of SH-4 require PC to be in P2 to operate on the I-cache.
228 * Some types of SH-4 require PC to be in P2 to operate on the D-cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900230 if ((cpu_data->flags & CPU_HAS_P2_FLUSH_BUG) ||
Paul Mundt33573c02006-09-27 18:37:30 +0900231 (start < CACHE_OC_ADDRESS_ARRAY))
232 exec_offset = 0x20000000;
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900233
Paul Mundt33573c02006-09-27 18:37:30 +0900234 local_irq_save(flags);
235 __flush_cache_4096(start | SH_CACHE_ASSOC,
236 P1SEGADDR(phys), exec_offset);
237 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240/*
241 * Write back & invalidate the D-cache of the page.
242 * (To avoid "alias" issues)
243 */
244void flush_dcache_page(struct page *page)
245{
246 if (test_bit(PG_mapped, &page->flags)) {
247 unsigned long phys = PHYSADDR(page_address(page));
Richard Curnowb638d0b2006-09-27 14:09:26 +0900248 unsigned long addr = CACHE_OC_ADDRESS_ARRAY;
249 int i, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* Loop all the D-cache */
Richard Curnowb638d0b2006-09-27 14:09:26 +0900252 n = cpu_data->dcache.n_aliases;
253 for (i = 0; i < n; i++, addr += PAGE_SIZE)
254 flush_cache_4096(addr, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900256
257 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900260/* TODO: Selective icache invalidation through IC address array.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static inline void flush_icache_all(void)
262{
263 unsigned long flags, ccr;
264
265 local_irq_save(flags);
266 jump_to_P2();
267
268 /* Flush I-cache */
269 ccr = ctrl_inl(CCR);
270 ccr |= CCR_CACHE_ICI;
271 ctrl_outl(ccr, CCR);
272
Paul Mundt29847622006-09-27 14:57:44 +0900273 /*
274 * back_to_P1() will take care of the barrier for us, don't add
275 * another one!
276 */
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 back_to_P1();
279 local_irq_restore(flags);
280}
281
Paul Mundta2527102006-09-27 11:29:55 +0900282void flush_dcache_all(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Richard Curnowb638d0b2006-09-27 14:09:26 +0900284 (*__flush_dcache_segment_fn)(0UL, cpu_data->dcache.way_size);
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900285 wmb();
Paul Mundta2527102006-09-27 11:29:55 +0900286}
287
288void flush_cache_all(void)
289{
290 flush_dcache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 flush_icache_all();
292}
293
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900294static void __flush_cache_mm(struct mm_struct *mm, unsigned long start,
295 unsigned long end)
296{
297 unsigned long d = 0, p = start & PAGE_MASK;
298 unsigned long alias_mask = cpu_data->dcache.alias_mask;
299 unsigned long n_aliases = cpu_data->dcache.n_aliases;
300 unsigned long select_bit;
301 unsigned long all_aliases_mask;
302 unsigned long addr_offset;
303 pgd_t *dir;
304 pmd_t *pmd;
305 pud_t *pud;
306 pte_t *pte;
307 int i;
308
309 dir = pgd_offset(mm, p);
310 pud = pud_offset(dir, p);
311 pmd = pmd_offset(pud, p);
312 end = PAGE_ALIGN(end);
313
314 all_aliases_mask = (1 << n_aliases) - 1;
315
316 do {
317 if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) {
318 p &= PMD_MASK;
319 p += PMD_SIZE;
320 pmd++;
321
322 continue;
323 }
324
325 pte = pte_offset_kernel(pmd, p);
326
327 do {
328 unsigned long phys;
329 pte_t entry = *pte;
330
331 if (!(pte_val(entry) & _PAGE_PRESENT)) {
332 pte++;
333 p += PAGE_SIZE;
334 continue;
335 }
336
337 phys = pte_val(entry) & PTE_PHYS_MASK;
338
339 if ((p ^ phys) & alias_mask) {
340 d |= 1 << ((p & alias_mask) >> PAGE_SHIFT);
341 d |= 1 << ((phys & alias_mask) >> PAGE_SHIFT);
342
343 if (d == all_aliases_mask)
344 goto loop_exit;
345 }
346
347 pte++;
348 p += PAGE_SIZE;
349 } while (p < end && ((unsigned long)pte & ~PAGE_MASK));
350 pmd++;
351 } while (p < end);
352
353loop_exit:
354 addr_offset = 0;
355 select_bit = 1;
356
357 for (i = 0; i < n_aliases; i++) {
358 if (d & select_bit) {
359 (*__flush_dcache_segment_fn)(addr_offset, PAGE_SIZE);
360 wmb();
361 }
362
363 select_bit <<= 1;
364 addr_offset += PAGE_SIZE;
365 }
366}
367
368/*
369 * Note : (RPC) since the caches are physically tagged, the only point
370 * of flush_cache_mm for SH-4 is to get rid of aliases from the
371 * D-cache. The assumption elsewhere, e.g. flush_cache_range, is that
372 * lines can stay resident so long as the virtual address they were
373 * accessed with (hence cache set) is in accord with the physical
374 * address (i.e. tag). It's no different here. So I reckon we don't
375 * need to flush the I-cache, since aliases don't matter for that. We
376 * should try that.
377 *
378 * Caller takes mm->mmap_sem.
379 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380void flush_cache_mm(struct mm_struct *mm)
381{
Richard Curnowb638d0b2006-09-27 14:09:26 +0900382 /*
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900383 * If cache is only 4k-per-way, there are never any 'aliases'. Since
384 * the cache is physically tagged, the data can just be left in there.
Richard Curnowb638d0b2006-09-27 14:09:26 +0900385 */
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900386 if (cpu_data->dcache.n_aliases == 0)
387 return;
388
389 /*
390 * Don't bother groveling around the dcache for the VMA ranges
391 * if there are too many PTEs to make it worthwhile.
392 */
393 if (mm->nr_ptes >= MAX_DCACHE_PAGES)
394 flush_dcache_all();
395 else {
396 struct vm_area_struct *vma;
397
398 /*
399 * In this case there are reasonably sized ranges to flush,
400 * iterate through the VMA list and take care of any aliases.
401 */
402 for (vma = mm->mmap; vma; vma = vma->vm_next)
403 __flush_cache_mm(mm, vma->vm_start, vma->vm_end);
404 }
405
406 /* Only touch the icache if one of the VMAs has VM_EXEC set. */
407 if (mm->exec_vm)
408 flush_icache_all();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411/*
412 * Write back and invalidate I/D-caches for the page.
413 *
414 * ADDR: Virtual Address (U0 address)
415 * PFN: Physical page number
416 */
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900417void flush_cache_page(struct vm_area_struct *vma, unsigned long address,
418 unsigned long pfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 unsigned long phys = pfn << PAGE_SHIFT;
Richard Curnowb638d0b2006-09-27 14:09:26 +0900421 unsigned int alias_mask;
422
423 alias_mask = cpu_data->dcache.alias_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 /* We only need to flush D-cache when we have alias */
Richard Curnowb638d0b2006-09-27 14:09:26 +0900426 if ((address^phys) & alias_mask) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /* Loop 4K of the D-cache */
428 flush_cache_4096(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900429 CACHE_OC_ADDRESS_ARRAY | (address & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 phys);
431 /* Loop another 4K of the D-cache */
432 flush_cache_4096(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900433 CACHE_OC_ADDRESS_ARRAY | (phys & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 phys);
435 }
436
Richard Curnowb638d0b2006-09-27 14:09:26 +0900437 alias_mask = cpu_data->icache.alias_mask;
438 if (vma->vm_flags & VM_EXEC) {
439 /*
440 * Evict entries from the portion of the cache from which code
441 * may have been executed at this address (virtual). There's
442 * no need to evict from the portion corresponding to the
443 * physical address as for the D-cache, because we know the
444 * kernel has never executed the code through its identity
445 * translation.
446 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 flush_cache_4096(
Richard Curnowb638d0b2006-09-27 14:09:26 +0900448 CACHE_IC_ADDRESS_ARRAY | (address & alias_mask),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 phys);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900450 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453/*
454 * Write back and invalidate D-caches.
455 *
456 * START, END: Virtual Address (U0 address)
457 *
458 * NOTE: We need to flush the _physical_ page entry.
459 * Flushing the cache lines for U0 only isn't enough.
460 * We need to flush for P1 too, which may contain aliases.
461 */
462void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
463 unsigned long end)
464{
Richard Curnowb638d0b2006-09-27 14:09:26 +0900465 /*
466 * If cache is only 4k-per-way, there are never any 'aliases'. Since
467 * the cache is physically tagged, the data can just be left in there.
468 */
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900469 if (cpu_data->dcache.n_aliases == 0)
Richard Curnowb638d0b2006-09-27 14:09:26 +0900470 return;
471
Paul Mundta2527102006-09-27 11:29:55 +0900472 /*
473 * Don't bother with the lookup and alias check if we have a
474 * wide range to cover, just blow away the dcache in its
475 * entirety instead. -- PFM.
476 */
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900477 if (((end - start) >> PAGE_SHIFT) >= MAX_DCACHE_PAGES)
Paul Mundta2527102006-09-27 11:29:55 +0900478 flush_dcache_all();
Paul Mundt28ccf7f2006-09-27 18:30:07 +0900479 else
480 __flush_cache_mm(vma->vm_mm, start, end);
Richard Curnowb638d0b2006-09-27 14:09:26 +0900481
482 if (vma->vm_flags & VM_EXEC) {
483 /*
484 * TODO: Is this required??? Need to look at how I-cache
485 * coherency is assured when new programs are loaded to see if
486 * this matters.
487 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 flush_icache_all();
Richard Curnowb638d0b2006-09-27 14:09:26 +0900489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492/*
493 * flush_icache_user_range
494 * @vma: VMA of the process
495 * @page: page
496 * @addr: U0 address
497 * @len: length of the range (< page size)
498 */
499void flush_icache_user_range(struct vm_area_struct *vma,
500 struct page *page, unsigned long addr, int len)
501{
502 flush_cache_page(vma, addr, page_to_pfn(page));
Paul Mundtfdfc74f2006-09-27 14:05:52 +0900503 mb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
Richard Curnowb638d0b2006-09-27 14:09:26 +0900506/**
507 * __flush_cache_4096
508 *
509 * @addr: address in memory mapped cache array
510 * @phys: P1 address to flush (has to match tags if addr has 'A' bit
511 * set i.e. associative write)
512 * @exec_offset: set to 0x20000000 if flush has to be executed from P2
513 * region else 0x0
514 *
515 * The offset into the cache array implied by 'addr' selects the
516 * 'colour' of the virtual address range that will be flushed. The
517 * operation (purge/write-back) is selected by the lower 2 bits of
518 * 'phys'.
519 */
520static void __flush_cache_4096(unsigned long addr, unsigned long phys,
521 unsigned long exec_offset)
522{
523 int way_count;
524 unsigned long base_addr = addr;
525 struct cache_info *dcache;
526 unsigned long way_incr;
527 unsigned long a, ea, p;
528 unsigned long temp_pc;
529
530 dcache = &cpu_data->dcache;
531 /* Write this way for better assembly. */
532 way_count = dcache->ways;
533 way_incr = dcache->way_incr;
534
535 /*
536 * Apply exec_offset (i.e. branch to P2 if required.).
537 *
538 * FIXME:
539 *
540 * If I write "=r" for the (temp_pc), it puts this in r6 hence
541 * trashing exec_offset before it's been added on - why? Hence
542 * "=&r" as a 'workaround'
543 */
544 asm volatile("mov.l 1f, %0\n\t"
545 "add %1, %0\n\t"
546 "jmp @%0\n\t"
547 "nop\n\t"
548 ".balign 4\n\t"
549 "1: .long 2f\n\t"
550 "2:\n" : "=&r" (temp_pc) : "r" (exec_offset));
551
552 /*
553 * We know there will be >=1 iteration, so write as do-while to avoid
554 * pointless nead-of-loop check for 0 iterations.
555 */
556 do {
557 ea = base_addr + PAGE_SIZE;
558 a = base_addr;
559 p = phys;
560
561 do {
562 *(volatile unsigned long *)a = p;
563 /*
564 * Next line: intentionally not p+32, saves an add, p
565 * will do since only the cache tag bits need to
566 * match.
567 */
568 *(volatile unsigned long *)(a+32) = p;
569 a += 64;
570 p += 64;
571 } while (a < ea);
572
573 base_addr += way_incr;
574 } while (--way_count != 0);
575}
576
577/*
578 * Break the 1, 2 and 4 way variants of this out into separate functions to
579 * avoid nearly all the overhead of having the conditional stuff in the function
580 * bodies (+ the 1 and 2 way cases avoid saving any registers too).
581 */
582static void __flush_dcache_segment_1way(unsigned long start,
583 unsigned long extent_per_way)
584{
585 unsigned long orig_sr, sr_with_bl;
586 unsigned long base_addr;
587 unsigned long way_incr, linesz, way_size;
588 struct cache_info *dcache;
589 register unsigned long a0, a0e;
590
591 asm volatile("stc sr, %0" : "=r" (orig_sr));
592 sr_with_bl = orig_sr | (1<<28);
593 base_addr = ((unsigned long)&empty_zero_page[0]);
594
595 /*
596 * The previous code aligned base_addr to 16k, i.e. the way_size of all
597 * existing SH-4 D-caches. Whilst I don't see a need to have this
598 * aligned to any better than the cache line size (which it will be
599 * anyway by construction), let's align it to at least the way_size of
600 * any existing or conceivable SH-4 D-cache. -- RPC
601 */
602 base_addr = ((base_addr >> 16) << 16);
603 base_addr |= start;
604
605 dcache = &cpu_data->dcache;
606 linesz = dcache->linesz;
607 way_incr = dcache->way_incr;
608 way_size = dcache->way_size;
609
610 a0 = base_addr;
611 a0e = base_addr + extent_per_way;
612 do {
613 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
614 asm volatile("movca.l r0, @%0\n\t"
615 "ocbi @%0" : : "r" (a0));
616 a0 += linesz;
617 asm volatile("movca.l r0, @%0\n\t"
618 "ocbi @%0" : : "r" (a0));
619 a0 += linesz;
620 asm volatile("movca.l r0, @%0\n\t"
621 "ocbi @%0" : : "r" (a0));
622 a0 += linesz;
623 asm volatile("movca.l r0, @%0\n\t"
624 "ocbi @%0" : : "r" (a0));
625 asm volatile("ldc %0, sr" : : "r" (orig_sr));
626 a0 += linesz;
627 } while (a0 < a0e);
628}
629
630static void __flush_dcache_segment_2way(unsigned long start,
631 unsigned long extent_per_way)
632{
633 unsigned long orig_sr, sr_with_bl;
634 unsigned long base_addr;
635 unsigned long way_incr, linesz, way_size;
636 struct cache_info *dcache;
637 register unsigned long a0, a1, a0e;
638
639 asm volatile("stc sr, %0" : "=r" (orig_sr));
640 sr_with_bl = orig_sr | (1<<28);
641 base_addr = ((unsigned long)&empty_zero_page[0]);
642
643 /* See comment under 1-way above */
644 base_addr = ((base_addr >> 16) << 16);
645 base_addr |= start;
646
647 dcache = &cpu_data->dcache;
648 linesz = dcache->linesz;
649 way_incr = dcache->way_incr;
650 way_size = dcache->way_size;
651
652 a0 = base_addr;
653 a1 = a0 + way_incr;
654 a0e = base_addr + extent_per_way;
655 do {
656 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
657 asm volatile("movca.l r0, @%0\n\t"
658 "movca.l r0, @%1\n\t"
659 "ocbi @%0\n\t"
660 "ocbi @%1" : :
661 "r" (a0), "r" (a1));
662 a0 += linesz;
663 a1 += linesz;
664 asm volatile("movca.l r0, @%0\n\t"
665 "movca.l r0, @%1\n\t"
666 "ocbi @%0\n\t"
667 "ocbi @%1" : :
668 "r" (a0), "r" (a1));
669 a0 += linesz;
670 a1 += linesz;
671 asm volatile("movca.l r0, @%0\n\t"
672 "movca.l r0, @%1\n\t"
673 "ocbi @%0\n\t"
674 "ocbi @%1" : :
675 "r" (a0), "r" (a1));
676 a0 += linesz;
677 a1 += linesz;
678 asm volatile("movca.l r0, @%0\n\t"
679 "movca.l r0, @%1\n\t"
680 "ocbi @%0\n\t"
681 "ocbi @%1" : :
682 "r" (a0), "r" (a1));
683 asm volatile("ldc %0, sr" : : "r" (orig_sr));
684 a0 += linesz;
685 a1 += linesz;
686 } while (a0 < a0e);
687}
688
689static void __flush_dcache_segment_4way(unsigned long start,
690 unsigned long extent_per_way)
691{
692 unsigned long orig_sr, sr_with_bl;
693 unsigned long base_addr;
694 unsigned long way_incr, linesz, way_size;
695 struct cache_info *dcache;
696 register unsigned long a0, a1, a2, a3, a0e;
697
698 asm volatile("stc sr, %0" : "=r" (orig_sr));
699 sr_with_bl = orig_sr | (1<<28);
700 base_addr = ((unsigned long)&empty_zero_page[0]);
701
702 /* See comment under 1-way above */
703 base_addr = ((base_addr >> 16) << 16);
704 base_addr |= start;
705
706 dcache = &cpu_data->dcache;
707 linesz = dcache->linesz;
708 way_incr = dcache->way_incr;
709 way_size = dcache->way_size;
710
711 a0 = base_addr;
712 a1 = a0 + way_incr;
713 a2 = a1 + way_incr;
714 a3 = a2 + way_incr;
715 a0e = base_addr + extent_per_way;
716 do {
717 asm volatile("ldc %0, sr" : : "r" (sr_with_bl));
718 asm volatile("movca.l r0, @%0\n\t"
719 "movca.l r0, @%1\n\t"
720 "movca.l r0, @%2\n\t"
721 "movca.l r0, @%3\n\t"
722 "ocbi @%0\n\t"
723 "ocbi @%1\n\t"
724 "ocbi @%2\n\t"
725 "ocbi @%3\n\t" : :
726 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
727 a0 += linesz;
728 a1 += linesz;
729 a2 += linesz;
730 a3 += linesz;
731 asm volatile("movca.l r0, @%0\n\t"
732 "movca.l r0, @%1\n\t"
733 "movca.l r0, @%2\n\t"
734 "movca.l r0, @%3\n\t"
735 "ocbi @%0\n\t"
736 "ocbi @%1\n\t"
737 "ocbi @%2\n\t"
738 "ocbi @%3\n\t" : :
739 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
740 a0 += linesz;
741 a1 += linesz;
742 a2 += linesz;
743 a3 += linesz;
744 asm volatile("movca.l r0, @%0\n\t"
745 "movca.l r0, @%1\n\t"
746 "movca.l r0, @%2\n\t"
747 "movca.l r0, @%3\n\t"
748 "ocbi @%0\n\t"
749 "ocbi @%1\n\t"
750 "ocbi @%2\n\t"
751 "ocbi @%3\n\t" : :
752 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
753 a0 += linesz;
754 a1 += linesz;
755 a2 += linesz;
756 a3 += linesz;
757 asm volatile("movca.l r0, @%0\n\t"
758 "movca.l r0, @%1\n\t"
759 "movca.l r0, @%2\n\t"
760 "movca.l r0, @%3\n\t"
761 "ocbi @%0\n\t"
762 "ocbi @%1\n\t"
763 "ocbi @%2\n\t"
764 "ocbi @%3\n\t" : :
765 "r" (a0), "r" (a1), "r" (a2), "r" (a3));
766 asm volatile("ldc %0, sr" : : "r" (orig_sr));
767 a0 += linesz;
768 a1 += linesz;
769 a2 += linesz;
770 a3 += linesz;
771 } while (a0 < a0e);
772}