Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * arch/sh/mm/cache-sh4.c |
| 3 | * |
| 4 | * Copyright (C) 1999, 2000, 2002 Niibe Yutaka |
Paul Mundt | d10040f | 2007-09-24 16:38:25 +0900 | [diff] [blame] | 5 | * Copyright (C) 2001 - 2007 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * Copyright (C) 2003 Richard Curnow |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 7 | * Copyright (c) 2007 STMicroelectronics (R&D) Ltd. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/mm.h> |
Paul Mundt | 52e2778 | 2006-11-21 11:09:41 +0900 | [diff] [blame] | 15 | #include <linux/io.h> |
| 16 | #include <linux/mutex.h> |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 17 | #include <linux/fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <asm/mmu_context.h> |
| 19 | #include <asm/cacheflush.h> |
| 20 | |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 21 | /* |
| 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 Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 27 | #define MAX_ICACHE_PAGES 32 |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 28 | |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 29 | static void __flush_cache_4096(unsigned long addr, unsigned long phys, |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 30 | unsigned long exec_offset); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 31 | |
| 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 | */ |
| 37 | static void (*__flush_dcache_segment_fn)(unsigned long, unsigned long) = |
| 38 | (void (*)(unsigned long, unsigned long))0xdeadbeef; |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | * Write back the range of D-cache, and purge the I-cache. |
| 42 | * |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 43 | * Called from kernel/module.c:sys_init_module and routine for a.out format, |
| 44 | * signal handler code and kprobes code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | */ |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 46 | static void sh4_flush_icache_range(void *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 48 | struct flusher_data *data = args; |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 49 | int icacheaddr; |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 50 | unsigned long start, end; |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 51 | unsigned long flags, v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | int i; |
| 53 | |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 54 | start = data->addr1; |
| 55 | end = data->addr2; |
| 56 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 57 | /* If there are too many pages then just blow the caches */ |
| 58 | if (((end - start) >> PAGE_SHIFT) >= MAX_ICACHE_PAGES) { |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 59 | local_flush_cache_all(args); |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 60 | } 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 67 | local_irq_save(flags); |
| 68 | jump_to_uncached(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 70 | for (v = start; v < end; v+=L1_CACHE_BYTES) { |
| 71 | asm volatile("ocbwb %0" |
| 72 | : /* no output */ |
| 73 | : "m" (__m(v))); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 74 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 75 | icacheaddr = CACHE_IC_ADDRESS_ARRAY | ( |
| 76 | v & cpu_data->icache.entry_mask); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 77 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 78 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static inline void flush_cache_4096(unsigned long start, |
| 90 | unsigned long phys) |
| 91 | { |
Paul Mundt | 33573c0 | 2006-09-27 18:37:30 +0900 | [diff] [blame] | 92 | unsigned long flags, exec_offset = 0; |
| 93 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 95 | * 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | */ |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 98 | if ((boot_cpu_data.flags & CPU_HAS_P2_FLUSH_BUG) || |
Paul Mundt | 33573c0 | 2006-09-27 18:37:30 +0900 | [diff] [blame] | 99 | (start < CACHE_OC_ADDRESS_ARRAY)) |
Paul Mundt | 510c72ad | 2006-11-27 12:06:26 +0900 | [diff] [blame] | 100 | exec_offset = 0x20000000; |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 101 | |
Paul Mundt | 33573c0 | 2006-09-27 18:37:30 +0900 | [diff] [blame] | 102 | local_irq_save(flags); |
| 103 | __flush_cache_4096(start | SH_CACHE_ASSOC, |
| 104 | P1SEGADDR(phys), exec_offset); |
| 105 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Write back & invalidate the D-cache of the page. |
| 110 | * (To avoid "alias" issues) |
| 111 | */ |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 112 | static void sh4_flush_dcache_page(void *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | { |
Paul Mundt | c139a59 | 2009-08-20 15:24:41 +0900 | [diff] [blame] | 114 | #ifndef CONFIG_SMP |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 115 | struct address_space *mapping = page_mapping(page); |
| 116 | |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 117 | if (mapping && !mapping_mapped(mapping)) |
| 118 | set_bit(PG_dcache_dirty, &page->flags); |
| 119 | else |
| 120 | #endif |
| 121 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | unsigned long phys = PHYSADDR(page_address(page)); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 123 | unsigned long addr = CACHE_OC_ADDRESS_ARRAY; |
| 124 | int i, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* Loop all the D-cache */ |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 127 | n = boot_cpu_data.dcache.n_aliases; |
Paul Mundt | 510c72ad | 2006-11-27 12:06:26 +0900 | [diff] [blame] | 128 | for (i = 0; i < n; i++, addr += 4096) |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 129 | flush_cache_4096(addr, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
Paul Mundt | fdfc74f | 2006-09-27 14:05:52 +0900 | [diff] [blame] | 131 | |
| 132 | wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 135 | /* TODO: Selective icache invalidation through IC address array.. */ |
Paul Mundt | 205a3b4 | 2008-09-05 18:00:29 +0900 | [diff] [blame] | 136 | static void __uses_jump_to_uncached flush_icache_all(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
| 138 | unsigned long flags, ccr; |
| 139 | |
| 140 | local_irq_save(flags); |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 141 | jump_to_uncached(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | /* Flush I-cache */ |
| 144 | ccr = ctrl_inl(CCR); |
| 145 | ccr |= CCR_CACHE_ICI; |
| 146 | ctrl_outl(ccr, CCR); |
| 147 | |
Paul Mundt | 2984762 | 2006-09-27 14:57:44 +0900 | [diff] [blame] | 148 | /* |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 149 | * back_to_cached() will take care of the barrier for us, don't add |
Paul Mundt | 2984762 | 2006-09-27 14:57:44 +0900 | [diff] [blame] | 150 | * another one! |
| 151 | */ |
| 152 | |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 153 | back_to_cached(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | local_irq_restore(flags); |
| 155 | } |
| 156 | |
Paul Mundt | 0b445dc | 2009-08-15 11:22:50 +0900 | [diff] [blame] | 157 | static inline void flush_dcache_all(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 159 | (*__flush_dcache_segment_fn)(0UL, boot_cpu_data.dcache.way_size); |
Paul Mundt | fdfc74f | 2006-09-27 14:05:52 +0900 | [diff] [blame] | 160 | wmb(); |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 161 | } |
| 162 | |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 163 | static void sh4_flush_cache_all(void *unused) |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 164 | { |
| 165 | flush_dcache_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | flush_icache_all(); |
| 167 | } |
| 168 | |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 169 | static 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 Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 173 | unsigned long alias_mask = boot_cpu_data.dcache.alias_mask; |
| 174 | unsigned long n_aliases = boot_cpu_data.dcache.n_aliases; |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 175 | 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 | |
| 228 | loop_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 Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 255 | static void sh4_flush_cache_mm(void *arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 257 | struct mm_struct *mm = arg; |
| 258 | |
Paul Mundt | e7b8b7f | 2009-08-15 02:21:16 +0900 | [diff] [blame] | 259 | if (cpu_context(smp_processor_id(), mm) == NO_CONTEXT) |
| 260 | return; |
| 261 | |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 262 | /* |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 263 | * 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 Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 265 | */ |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 266 | if (boot_cpu_data.dcache.n_aliases == 0) |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 267 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 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 Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 297 | static void sh4_flush_cache_page(void *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | { |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 299 | struct flusher_data *data = args; |
| 300 | struct vm_area_struct *vma; |
| 301 | unsigned long address, pfn, phys; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 302 | unsigned int alias_mask; |
| 303 | |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 304 | vma = data->vma; |
| 305 | address = data->addr1; |
| 306 | pfn = data->addr2; |
| 307 | phys = pfn << PAGE_SHIFT; |
| 308 | |
Paul Mundt | e7b8b7f | 2009-08-15 02:21:16 +0900 | [diff] [blame] | 309 | if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT) |
| 310 | return; |
| 311 | |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 312 | alias_mask = boot_cpu_data.dcache.alias_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | /* We only need to flush D-cache when we have alias */ |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 315 | if ((address^phys) & alias_mask) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | /* Loop 4K of the D-cache */ |
| 317 | flush_cache_4096( |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 318 | CACHE_OC_ADDRESS_ARRAY | (address & alias_mask), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | phys); |
| 320 | /* Loop another 4K of the D-cache */ |
| 321 | flush_cache_4096( |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 322 | CACHE_OC_ADDRESS_ARRAY | (phys & alias_mask), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | phys); |
| 324 | } |
| 325 | |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 326 | alias_mask = boot_cpu_data.icache.alias_mask; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 327 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | flush_cache_4096( |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 337 | CACHE_IC_ADDRESS_ARRAY | (address & alias_mask), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | phys); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 339 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | } |
| 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 Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 351 | static void sh4_flush_cache_range(void *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 353 | 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 Mundt | e7b8b7f | 2009-08-15 02:21:16 +0900 | [diff] [blame] | 361 | if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT) |
| 362 | return; |
| 363 | |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 364 | /* |
| 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 Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 368 | if (boot_cpu_data.dcache.n_aliases == 0) |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 369 | return; |
| 370 | |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 371 | /* |
| 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 Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 376 | if (((end - start) >> PAGE_SHIFT) >= MAX_DCACHE_PAGES) |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 377 | flush_dcache_all(); |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 378 | else |
| 379 | __flush_cache_mm(vma->vm_mm, start, end); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 380 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | flush_icache_all(); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 388 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 391 | /** |
| 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 | */ |
| 405 | static 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 Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 415 | dcache = &boot_cpu_data.dcache; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 416 | /* 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 | */ |
| 467 | static 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 Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 490 | dcache = &boot_cpu_data.dcache; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 491 | 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 | |
| 515 | static 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 Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 532 | dcache = &boot_cpu_data.dcache; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 533 | 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 | |
| 574 | static 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 Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 591 | dcache = &boot_cpu_data.dcache; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 592 | 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 Mundt | 37443ef | 2009-08-15 12:29:49 +0900 | [diff] [blame] | 658 | |
| 659 | extern void __weak sh4__flush_region_init(void); |
| 660 | |
| 661 | /* |
| 662 | * SH-4 has virtually indexed and physically tagged cache. |
| 663 | */ |
| 664 | void __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 Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame^] | 686 | 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 Mundt | 37443ef | 2009-08-15 12:29:49 +0900 | [diff] [blame] | 693 | |
| 694 | sh4__flush_region_init(); |
| 695 | } |