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_dcache_segment_1way(unsigned long start, |
| 30 | unsigned long extent); |
| 31 | static void __flush_dcache_segment_2way(unsigned long start, |
| 32 | unsigned long extent); |
| 33 | static void __flush_dcache_segment_4way(unsigned long start, |
| 34 | unsigned long extent); |
| 35 | |
| 36 | static void __flush_cache_4096(unsigned long addr, unsigned long phys, |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 37 | unsigned long exec_offset); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * This is initialised here to ensure that it is not placed in the BSS. If |
| 41 | * that were to happen, note that cache_init gets called before the BSS is |
| 42 | * cleared, so this would get nulled out which would be hopeless. |
| 43 | */ |
| 44 | static void (*__flush_dcache_segment_fn)(unsigned long, unsigned long) = |
| 45 | (void (*)(unsigned long, unsigned long))0xdeadbeef; |
| 46 | |
| 47 | static void compute_alias(struct cache_info *c) |
| 48 | { |
| 49 | c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1); |
Paul Mundt | d10040f | 2007-09-24 16:38:25 +0900 | [diff] [blame] | 50 | c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static void __init emit_cache_params(void) |
| 54 | { |
| 55 | printk("PVR=%08x CVR=%08x PRR=%08x\n", |
| 56 | ctrl_inl(CCN_PVR), |
| 57 | ctrl_inl(CCN_CVR), |
| 58 | ctrl_inl(CCN_PRR)); |
| 59 | printk("I-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 60 | boot_cpu_data.icache.ways, |
| 61 | boot_cpu_data.icache.sets, |
| 62 | boot_cpu_data.icache.way_incr); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 63 | printk("I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 64 | boot_cpu_data.icache.entry_mask, |
| 65 | boot_cpu_data.icache.alias_mask, |
| 66 | boot_cpu_data.icache.n_aliases); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 67 | printk("D-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 68 | boot_cpu_data.dcache.ways, |
| 69 | boot_cpu_data.dcache.sets, |
| 70 | boot_cpu_data.dcache.way_incr); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 71 | printk("D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 72 | boot_cpu_data.dcache.entry_mask, |
| 73 | boot_cpu_data.dcache.alias_mask, |
| 74 | boot_cpu_data.dcache.n_aliases); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 75 | |
Paul Mundt | ab27f62 | 2007-09-24 17:00:45 +0900 | [diff] [blame] | 76 | /* |
| 77 | * Emit Secondary Cache parameters if the CPU has a probed L2. |
| 78 | */ |
| 79 | if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) { |
| 80 | printk("S-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
| 81 | boot_cpu_data.scache.ways, |
| 82 | boot_cpu_data.scache.sets, |
| 83 | boot_cpu_data.scache.way_incr); |
| 84 | printk("S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
| 85 | boot_cpu_data.scache.entry_mask, |
| 86 | boot_cpu_data.scache.alias_mask, |
| 87 | boot_cpu_data.scache.n_aliases); |
| 88 | } |
| 89 | |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 90 | if (!__flush_dcache_segment_fn) |
| 91 | panic("unknown number of cache ways\n"); |
| 92 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * SH-4 has virtually indexed and physically tagged cache. |
| 96 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | void __init p3_cache_init(void) |
| 98 | { |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 99 | compute_alias(&boot_cpu_data.icache); |
| 100 | compute_alias(&boot_cpu_data.dcache); |
Paul Mundt | ab27f62 | 2007-09-24 17:00:45 +0900 | [diff] [blame] | 101 | compute_alias(&boot_cpu_data.scache); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 102 | |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 103 | switch (boot_cpu_data.dcache.ways) { |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 104 | case 1: |
| 105 | __flush_dcache_segment_fn = __flush_dcache_segment_1way; |
| 106 | break; |
| 107 | case 2: |
| 108 | __flush_dcache_segment_fn = __flush_dcache_segment_2way; |
| 109 | break; |
| 110 | case 4: |
| 111 | __flush_dcache_segment_fn = __flush_dcache_segment_4way; |
| 112 | break; |
| 113 | default: |
| 114 | __flush_dcache_segment_fn = NULL; |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | emit_cache_params(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * Write back the range of D-cache, and purge the I-cache. |
| 123 | * |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 124 | * Called from kernel/module.c:sys_init_module and routine for a.out format, |
| 125 | * signal handler code and kprobes code |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | */ |
| 127 | void flush_icache_range(unsigned long start, unsigned long end) |
| 128 | { |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 129 | int icacheaddr; |
| 130 | unsigned long flags, v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | int i; |
| 132 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 133 | /* If there are too many pages then just blow the caches */ |
| 134 | if (((end - start) >> PAGE_SHIFT) >= MAX_ICACHE_PAGES) { |
| 135 | flush_cache_all(); |
| 136 | } else { |
| 137 | /* selectively flush d-cache then invalidate the i-cache */ |
| 138 | /* this is inefficient, so only use for small ranges */ |
| 139 | start &= ~(L1_CACHE_BYTES-1); |
| 140 | end += L1_CACHE_BYTES-1; |
| 141 | end &= ~(L1_CACHE_BYTES-1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 143 | local_irq_save(flags); |
| 144 | jump_to_uncached(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 146 | for (v = start; v < end; v+=L1_CACHE_BYTES) { |
| 147 | asm volatile("ocbwb %0" |
| 148 | : /* no output */ |
| 149 | : "m" (__m(v))); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 150 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 151 | icacheaddr = CACHE_IC_ADDRESS_ARRAY | ( |
| 152 | v & cpu_data->icache.entry_mask); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 153 | |
Chris Smith | 09b5a10 | 2008-07-02 15:17:11 +0900 | [diff] [blame] | 154 | for (i = 0; i < cpu_data->icache.ways; |
| 155 | i++, icacheaddr += cpu_data->icache.way_incr) |
| 156 | /* Clear i-cache line valid-bit */ |
| 157 | ctrl_outl(0, icacheaddr); |
| 158 | } |
| 159 | |
| 160 | back_to_cached(); |
| 161 | local_irq_restore(flags); |
| 162 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static inline void flush_cache_4096(unsigned long start, |
| 166 | unsigned long phys) |
| 167 | { |
Paul Mundt | 33573c0 | 2006-09-27 18:37:30 +0900 | [diff] [blame] | 168 | unsigned long flags, exec_offset = 0; |
| 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | /* |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 171 | * All types of SH-4 require PC to be in P2 to operate on the I-cache. |
| 172 | * 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] | 173 | */ |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 174 | if ((boot_cpu_data.flags & CPU_HAS_P2_FLUSH_BUG) || |
Paul Mundt | 33573c0 | 2006-09-27 18:37:30 +0900 | [diff] [blame] | 175 | (start < CACHE_OC_ADDRESS_ARRAY)) |
Paul Mundt | 510c72ad | 2006-11-27 12:06:26 +0900 | [diff] [blame] | 176 | exec_offset = 0x20000000; |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 177 | |
Paul Mundt | 33573c0 | 2006-09-27 18:37:30 +0900 | [diff] [blame] | 178 | local_irq_save(flags); |
| 179 | __flush_cache_4096(start | SH_CACHE_ASSOC, |
| 180 | P1SEGADDR(phys), exec_offset); |
| 181 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* |
| 185 | * Write back & invalidate the D-cache of the page. |
| 186 | * (To avoid "alias" issues) |
| 187 | */ |
| 188 | void flush_dcache_page(struct page *page) |
| 189 | { |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 190 | struct address_space *mapping = page_mapping(page); |
| 191 | |
| 192 | #ifndef CONFIG_SMP |
| 193 | if (mapping && !mapping_mapped(mapping)) |
| 194 | set_bit(PG_dcache_dirty, &page->flags); |
| 195 | else |
| 196 | #endif |
| 197 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | unsigned long phys = PHYSADDR(page_address(page)); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 199 | unsigned long addr = CACHE_OC_ADDRESS_ARRAY; |
| 200 | int i, n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | /* Loop all the D-cache */ |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 203 | n = boot_cpu_data.dcache.n_aliases; |
Paul Mundt | 510c72ad | 2006-11-27 12:06:26 +0900 | [diff] [blame] | 204 | for (i = 0; i < n; i++, addr += 4096) |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 205 | flush_cache_4096(addr, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | } |
Paul Mundt | fdfc74f | 2006-09-27 14:05:52 +0900 | [diff] [blame] | 207 | |
| 208 | wmb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 211 | /* TODO: Selective icache invalidation through IC address array.. */ |
Paul Mundt | 205a3b4 | 2008-09-05 18:00:29 +0900 | [diff] [blame] | 212 | static void __uses_jump_to_uncached flush_icache_all(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | unsigned long flags, ccr; |
| 215 | |
| 216 | local_irq_save(flags); |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 217 | jump_to_uncached(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
| 219 | /* Flush I-cache */ |
| 220 | ccr = ctrl_inl(CCR); |
| 221 | ccr |= CCR_CACHE_ICI; |
| 222 | ctrl_outl(ccr, CCR); |
| 223 | |
Paul Mundt | 2984762 | 2006-09-27 14:57:44 +0900 | [diff] [blame] | 224 | /* |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 225 | * 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] | 226 | * another one! |
| 227 | */ |
| 228 | |
Stuart Menefy | cbaa118 | 2007-11-30 17:06:36 +0900 | [diff] [blame] | 229 | back_to_cached(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | local_irq_restore(flags); |
| 231 | } |
| 232 | |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 233 | void flush_dcache_all(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 235 | (*__flush_dcache_segment_fn)(0UL, boot_cpu_data.dcache.way_size); |
Paul Mundt | fdfc74f | 2006-09-27 14:05:52 +0900 | [diff] [blame] | 236 | wmb(); |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | void flush_cache_all(void) |
| 240 | { |
| 241 | flush_dcache_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | flush_icache_all(); |
| 243 | } |
| 244 | |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 245 | static void __flush_cache_mm(struct mm_struct *mm, unsigned long start, |
| 246 | unsigned long end) |
| 247 | { |
| 248 | unsigned long d = 0, p = start & PAGE_MASK; |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 249 | unsigned long alias_mask = boot_cpu_data.dcache.alias_mask; |
| 250 | unsigned long n_aliases = boot_cpu_data.dcache.n_aliases; |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 251 | unsigned long select_bit; |
| 252 | unsigned long all_aliases_mask; |
| 253 | unsigned long addr_offset; |
| 254 | pgd_t *dir; |
| 255 | pmd_t *pmd; |
| 256 | pud_t *pud; |
| 257 | pte_t *pte; |
| 258 | int i; |
| 259 | |
| 260 | dir = pgd_offset(mm, p); |
| 261 | pud = pud_offset(dir, p); |
| 262 | pmd = pmd_offset(pud, p); |
| 263 | end = PAGE_ALIGN(end); |
| 264 | |
| 265 | all_aliases_mask = (1 << n_aliases) - 1; |
| 266 | |
| 267 | do { |
| 268 | if (pmd_none(*pmd) || unlikely(pmd_bad(*pmd))) { |
| 269 | p &= PMD_MASK; |
| 270 | p += PMD_SIZE; |
| 271 | pmd++; |
| 272 | |
| 273 | continue; |
| 274 | } |
| 275 | |
| 276 | pte = pte_offset_kernel(pmd, p); |
| 277 | |
| 278 | do { |
| 279 | unsigned long phys; |
| 280 | pte_t entry = *pte; |
| 281 | |
| 282 | if (!(pte_val(entry) & _PAGE_PRESENT)) { |
| 283 | pte++; |
| 284 | p += PAGE_SIZE; |
| 285 | continue; |
| 286 | } |
| 287 | |
| 288 | phys = pte_val(entry) & PTE_PHYS_MASK; |
| 289 | |
| 290 | if ((p ^ phys) & alias_mask) { |
| 291 | d |= 1 << ((p & alias_mask) >> PAGE_SHIFT); |
| 292 | d |= 1 << ((phys & alias_mask) >> PAGE_SHIFT); |
| 293 | |
| 294 | if (d == all_aliases_mask) |
| 295 | goto loop_exit; |
| 296 | } |
| 297 | |
| 298 | pte++; |
| 299 | p += PAGE_SIZE; |
| 300 | } while (p < end && ((unsigned long)pte & ~PAGE_MASK)); |
| 301 | pmd++; |
| 302 | } while (p < end); |
| 303 | |
| 304 | loop_exit: |
| 305 | addr_offset = 0; |
| 306 | select_bit = 1; |
| 307 | |
| 308 | for (i = 0; i < n_aliases; i++) { |
| 309 | if (d & select_bit) { |
| 310 | (*__flush_dcache_segment_fn)(addr_offset, PAGE_SIZE); |
| 311 | wmb(); |
| 312 | } |
| 313 | |
| 314 | select_bit <<= 1; |
| 315 | addr_offset += PAGE_SIZE; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /* |
| 320 | * Note : (RPC) since the caches are physically tagged, the only point |
| 321 | * of flush_cache_mm for SH-4 is to get rid of aliases from the |
| 322 | * D-cache. The assumption elsewhere, e.g. flush_cache_range, is that |
| 323 | * lines can stay resident so long as the virtual address they were |
| 324 | * accessed with (hence cache set) is in accord with the physical |
| 325 | * address (i.e. tag). It's no different here. So I reckon we don't |
| 326 | * need to flush the I-cache, since aliases don't matter for that. We |
| 327 | * should try that. |
| 328 | * |
| 329 | * Caller takes mm->mmap_sem. |
| 330 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | void flush_cache_mm(struct mm_struct *mm) |
| 332 | { |
Paul Mundt | e7b8b7f | 2009-08-15 02:21:16 +0900 | [diff] [blame^] | 333 | if (cpu_context(smp_processor_id(), mm) == NO_CONTEXT) |
| 334 | return; |
| 335 | |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 336 | /* |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 337 | * If cache is only 4k-per-way, there are never any 'aliases'. Since |
| 338 | * 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] | 339 | */ |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 340 | if (boot_cpu_data.dcache.n_aliases == 0) |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 341 | return; |
| 342 | |
| 343 | /* |
| 344 | * Don't bother groveling around the dcache for the VMA ranges |
| 345 | * if there are too many PTEs to make it worthwhile. |
| 346 | */ |
| 347 | if (mm->nr_ptes >= MAX_DCACHE_PAGES) |
| 348 | flush_dcache_all(); |
| 349 | else { |
| 350 | struct vm_area_struct *vma; |
| 351 | |
| 352 | /* |
| 353 | * In this case there are reasonably sized ranges to flush, |
| 354 | * iterate through the VMA list and take care of any aliases. |
| 355 | */ |
| 356 | for (vma = mm->mmap; vma; vma = vma->vm_next) |
| 357 | __flush_cache_mm(mm, vma->vm_start, vma->vm_end); |
| 358 | } |
| 359 | |
| 360 | /* Only touch the icache if one of the VMAs has VM_EXEC set. */ |
| 361 | if (mm->exec_vm) |
| 362 | flush_icache_all(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Write back and invalidate I/D-caches for the page. |
| 367 | * |
| 368 | * ADDR: Virtual Address (U0 address) |
| 369 | * PFN: Physical page number |
| 370 | */ |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 371 | void flush_cache_page(struct vm_area_struct *vma, unsigned long address, |
| 372 | unsigned long pfn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | { |
| 374 | unsigned long phys = pfn << PAGE_SHIFT; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 375 | unsigned int alias_mask; |
| 376 | |
Paul Mundt | e7b8b7f | 2009-08-15 02:21:16 +0900 | [diff] [blame^] | 377 | if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT) |
| 378 | return; |
| 379 | |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 380 | alias_mask = boot_cpu_data.dcache.alias_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
| 382 | /* We only need to flush D-cache when we have alias */ |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 383 | if ((address^phys) & alias_mask) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | /* Loop 4K of the D-cache */ |
| 385 | flush_cache_4096( |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 386 | CACHE_OC_ADDRESS_ARRAY | (address & alias_mask), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | phys); |
| 388 | /* Loop another 4K of the D-cache */ |
| 389 | flush_cache_4096( |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 390 | CACHE_OC_ADDRESS_ARRAY | (phys & alias_mask), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | phys); |
| 392 | } |
| 393 | |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 394 | alias_mask = boot_cpu_data.icache.alias_mask; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 395 | if (vma->vm_flags & VM_EXEC) { |
| 396 | /* |
| 397 | * Evict entries from the portion of the cache from which code |
| 398 | * may have been executed at this address (virtual). There's |
| 399 | * no need to evict from the portion corresponding to the |
| 400 | * physical address as for the D-cache, because we know the |
| 401 | * kernel has never executed the code through its identity |
| 402 | * translation. |
| 403 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | flush_cache_4096( |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 405 | CACHE_IC_ADDRESS_ARRAY | (address & alias_mask), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | phys); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 407 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Write back and invalidate D-caches. |
| 412 | * |
| 413 | * START, END: Virtual Address (U0 address) |
| 414 | * |
| 415 | * NOTE: We need to flush the _physical_ page entry. |
| 416 | * Flushing the cache lines for U0 only isn't enough. |
| 417 | * We need to flush for P1 too, which may contain aliases. |
| 418 | */ |
| 419 | void flush_cache_range(struct vm_area_struct *vma, unsigned long start, |
| 420 | unsigned long end) |
| 421 | { |
Paul Mundt | e7b8b7f | 2009-08-15 02:21:16 +0900 | [diff] [blame^] | 422 | if (cpu_context(smp_processor_id(), vma->vm_mm) == NO_CONTEXT) |
| 423 | return; |
| 424 | |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 425 | /* |
| 426 | * If cache is only 4k-per-way, there are never any 'aliases'. Since |
| 427 | * the cache is physically tagged, the data can just be left in there. |
| 428 | */ |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 429 | if (boot_cpu_data.dcache.n_aliases == 0) |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 430 | return; |
| 431 | |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 432 | /* |
| 433 | * Don't bother with the lookup and alias check if we have a |
| 434 | * wide range to cover, just blow away the dcache in its |
| 435 | * entirety instead. -- PFM. |
| 436 | */ |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 437 | if (((end - start) >> PAGE_SHIFT) >= MAX_DCACHE_PAGES) |
Paul Mundt | a252710 | 2006-09-27 11:29:55 +0900 | [diff] [blame] | 438 | flush_dcache_all(); |
Paul Mundt | 28ccf7f | 2006-09-27 18:30:07 +0900 | [diff] [blame] | 439 | else |
| 440 | __flush_cache_mm(vma->vm_mm, start, end); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 441 | |
| 442 | if (vma->vm_flags & VM_EXEC) { |
| 443 | /* |
| 444 | * TODO: Is this required??? Need to look at how I-cache |
| 445 | * coherency is assured when new programs are loaded to see if |
| 446 | * this matters. |
| 447 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | flush_icache_all(); |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | /* |
| 453 | * flush_icache_user_range |
| 454 | * @vma: VMA of the process |
| 455 | * @page: page |
| 456 | * @addr: U0 address |
| 457 | * @len: length of the range (< page size) |
| 458 | */ |
| 459 | void flush_icache_user_range(struct vm_area_struct *vma, |
| 460 | struct page *page, unsigned long addr, int len) |
| 461 | { |
| 462 | flush_cache_page(vma, addr, page_to_pfn(page)); |
Paul Mundt | fdfc74f | 2006-09-27 14:05:52 +0900 | [diff] [blame] | 463 | mb(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 466 | /** |
| 467 | * __flush_cache_4096 |
| 468 | * |
| 469 | * @addr: address in memory mapped cache array |
| 470 | * @phys: P1 address to flush (has to match tags if addr has 'A' bit |
| 471 | * set i.e. associative write) |
| 472 | * @exec_offset: set to 0x20000000 if flush has to be executed from P2 |
| 473 | * region else 0x0 |
| 474 | * |
| 475 | * The offset into the cache array implied by 'addr' selects the |
| 476 | * 'colour' of the virtual address range that will be flushed. The |
| 477 | * operation (purge/write-back) is selected by the lower 2 bits of |
| 478 | * 'phys'. |
| 479 | */ |
| 480 | static void __flush_cache_4096(unsigned long addr, unsigned long phys, |
| 481 | unsigned long exec_offset) |
| 482 | { |
| 483 | int way_count; |
| 484 | unsigned long base_addr = addr; |
| 485 | struct cache_info *dcache; |
| 486 | unsigned long way_incr; |
| 487 | unsigned long a, ea, p; |
| 488 | unsigned long temp_pc; |
| 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 | /* Write this way for better assembly. */ |
| 492 | way_count = dcache->ways; |
| 493 | way_incr = dcache->way_incr; |
| 494 | |
| 495 | /* |
| 496 | * Apply exec_offset (i.e. branch to P2 if required.). |
| 497 | * |
| 498 | * FIXME: |
| 499 | * |
| 500 | * If I write "=r" for the (temp_pc), it puts this in r6 hence |
| 501 | * trashing exec_offset before it's been added on - why? Hence |
| 502 | * "=&r" as a 'workaround' |
| 503 | */ |
| 504 | asm volatile("mov.l 1f, %0\n\t" |
| 505 | "add %1, %0\n\t" |
| 506 | "jmp @%0\n\t" |
| 507 | "nop\n\t" |
| 508 | ".balign 4\n\t" |
| 509 | "1: .long 2f\n\t" |
| 510 | "2:\n" : "=&r" (temp_pc) : "r" (exec_offset)); |
| 511 | |
| 512 | /* |
| 513 | * We know there will be >=1 iteration, so write as do-while to avoid |
| 514 | * pointless nead-of-loop check for 0 iterations. |
| 515 | */ |
| 516 | do { |
| 517 | ea = base_addr + PAGE_SIZE; |
| 518 | a = base_addr; |
| 519 | p = phys; |
| 520 | |
| 521 | do { |
| 522 | *(volatile unsigned long *)a = p; |
| 523 | /* |
| 524 | * Next line: intentionally not p+32, saves an add, p |
| 525 | * will do since only the cache tag bits need to |
| 526 | * match. |
| 527 | */ |
| 528 | *(volatile unsigned long *)(a+32) = p; |
| 529 | a += 64; |
| 530 | p += 64; |
| 531 | } while (a < ea); |
| 532 | |
| 533 | base_addr += way_incr; |
| 534 | } while (--way_count != 0); |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Break the 1, 2 and 4 way variants of this out into separate functions to |
| 539 | * avoid nearly all the overhead of having the conditional stuff in the function |
| 540 | * bodies (+ the 1 and 2 way cases avoid saving any registers too). |
| 541 | */ |
| 542 | static void __flush_dcache_segment_1way(unsigned long start, |
| 543 | unsigned long extent_per_way) |
| 544 | { |
| 545 | unsigned long orig_sr, sr_with_bl; |
| 546 | unsigned long base_addr; |
| 547 | unsigned long way_incr, linesz, way_size; |
| 548 | struct cache_info *dcache; |
| 549 | register unsigned long a0, a0e; |
| 550 | |
| 551 | asm volatile("stc sr, %0" : "=r" (orig_sr)); |
| 552 | sr_with_bl = orig_sr | (1<<28); |
| 553 | base_addr = ((unsigned long)&empty_zero_page[0]); |
| 554 | |
| 555 | /* |
| 556 | * The previous code aligned base_addr to 16k, i.e. the way_size of all |
| 557 | * existing SH-4 D-caches. Whilst I don't see a need to have this |
| 558 | * aligned to any better than the cache line size (which it will be |
| 559 | * anyway by construction), let's align it to at least the way_size of |
| 560 | * any existing or conceivable SH-4 D-cache. -- RPC |
| 561 | */ |
| 562 | base_addr = ((base_addr >> 16) << 16); |
| 563 | base_addr |= start; |
| 564 | |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 565 | dcache = &boot_cpu_data.dcache; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 566 | linesz = dcache->linesz; |
| 567 | way_incr = dcache->way_incr; |
| 568 | way_size = dcache->way_size; |
| 569 | |
| 570 | a0 = base_addr; |
| 571 | a0e = base_addr + extent_per_way; |
| 572 | do { |
| 573 | asm volatile("ldc %0, sr" : : "r" (sr_with_bl)); |
| 574 | asm volatile("movca.l r0, @%0\n\t" |
| 575 | "ocbi @%0" : : "r" (a0)); |
| 576 | a0 += linesz; |
| 577 | asm volatile("movca.l r0, @%0\n\t" |
| 578 | "ocbi @%0" : : "r" (a0)); |
| 579 | a0 += linesz; |
| 580 | asm volatile("movca.l r0, @%0\n\t" |
| 581 | "ocbi @%0" : : "r" (a0)); |
| 582 | a0 += linesz; |
| 583 | asm volatile("movca.l r0, @%0\n\t" |
| 584 | "ocbi @%0" : : "r" (a0)); |
| 585 | asm volatile("ldc %0, sr" : : "r" (orig_sr)); |
| 586 | a0 += linesz; |
| 587 | } while (a0 < a0e); |
| 588 | } |
| 589 | |
| 590 | static void __flush_dcache_segment_2way(unsigned long start, |
| 591 | unsigned long extent_per_way) |
| 592 | { |
| 593 | unsigned long orig_sr, sr_with_bl; |
| 594 | unsigned long base_addr; |
| 595 | unsigned long way_incr, linesz, way_size; |
| 596 | struct cache_info *dcache; |
| 597 | register unsigned long a0, a1, a0e; |
| 598 | |
| 599 | asm volatile("stc sr, %0" : "=r" (orig_sr)); |
| 600 | sr_with_bl = orig_sr | (1<<28); |
| 601 | base_addr = ((unsigned long)&empty_zero_page[0]); |
| 602 | |
| 603 | /* See comment under 1-way above */ |
| 604 | base_addr = ((base_addr >> 16) << 16); |
| 605 | base_addr |= start; |
| 606 | |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 607 | dcache = &boot_cpu_data.dcache; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 608 | linesz = dcache->linesz; |
| 609 | way_incr = dcache->way_incr; |
| 610 | way_size = dcache->way_size; |
| 611 | |
| 612 | a0 = base_addr; |
| 613 | a1 = a0 + way_incr; |
| 614 | a0e = base_addr + extent_per_way; |
| 615 | do { |
| 616 | asm volatile("ldc %0, sr" : : "r" (sr_with_bl)); |
| 617 | asm volatile("movca.l r0, @%0\n\t" |
| 618 | "movca.l r0, @%1\n\t" |
| 619 | "ocbi @%0\n\t" |
| 620 | "ocbi @%1" : : |
| 621 | "r" (a0), "r" (a1)); |
| 622 | a0 += linesz; |
| 623 | a1 += linesz; |
| 624 | asm volatile("movca.l r0, @%0\n\t" |
| 625 | "movca.l r0, @%1\n\t" |
| 626 | "ocbi @%0\n\t" |
| 627 | "ocbi @%1" : : |
| 628 | "r" (a0), "r" (a1)); |
| 629 | a0 += linesz; |
| 630 | a1 += linesz; |
| 631 | asm volatile("movca.l r0, @%0\n\t" |
| 632 | "movca.l r0, @%1\n\t" |
| 633 | "ocbi @%0\n\t" |
| 634 | "ocbi @%1" : : |
| 635 | "r" (a0), "r" (a1)); |
| 636 | a0 += linesz; |
| 637 | a1 += linesz; |
| 638 | asm volatile("movca.l r0, @%0\n\t" |
| 639 | "movca.l r0, @%1\n\t" |
| 640 | "ocbi @%0\n\t" |
| 641 | "ocbi @%1" : : |
| 642 | "r" (a0), "r" (a1)); |
| 643 | asm volatile("ldc %0, sr" : : "r" (orig_sr)); |
| 644 | a0 += linesz; |
| 645 | a1 += linesz; |
| 646 | } while (a0 < a0e); |
| 647 | } |
| 648 | |
| 649 | static void __flush_dcache_segment_4way(unsigned long start, |
| 650 | unsigned long extent_per_way) |
| 651 | { |
| 652 | unsigned long orig_sr, sr_with_bl; |
| 653 | unsigned long base_addr; |
| 654 | unsigned long way_incr, linesz, way_size; |
| 655 | struct cache_info *dcache; |
| 656 | register unsigned long a0, a1, a2, a3, a0e; |
| 657 | |
| 658 | asm volatile("stc sr, %0" : "=r" (orig_sr)); |
| 659 | sr_with_bl = orig_sr | (1<<28); |
| 660 | base_addr = ((unsigned long)&empty_zero_page[0]); |
| 661 | |
| 662 | /* See comment under 1-way above */ |
| 663 | base_addr = ((base_addr >> 16) << 16); |
| 664 | base_addr |= start; |
| 665 | |
Paul Mundt | 7ec9d6f | 2007-09-21 18:05:20 +0900 | [diff] [blame] | 666 | dcache = &boot_cpu_data.dcache; |
Richard Curnow | b638d0b | 2006-09-27 14:09:26 +0900 | [diff] [blame] | 667 | linesz = dcache->linesz; |
| 668 | way_incr = dcache->way_incr; |
| 669 | way_size = dcache->way_size; |
| 670 | |
| 671 | a0 = base_addr; |
| 672 | a1 = a0 + way_incr; |
| 673 | a2 = a1 + way_incr; |
| 674 | a3 = a2 + way_incr; |
| 675 | a0e = base_addr + extent_per_way; |
| 676 | do { |
| 677 | asm volatile("ldc %0, sr" : : "r" (sr_with_bl)); |
| 678 | asm volatile("movca.l r0, @%0\n\t" |
| 679 | "movca.l r0, @%1\n\t" |
| 680 | "movca.l r0, @%2\n\t" |
| 681 | "movca.l r0, @%3\n\t" |
| 682 | "ocbi @%0\n\t" |
| 683 | "ocbi @%1\n\t" |
| 684 | "ocbi @%2\n\t" |
| 685 | "ocbi @%3\n\t" : : |
| 686 | "r" (a0), "r" (a1), "r" (a2), "r" (a3)); |
| 687 | a0 += linesz; |
| 688 | a1 += linesz; |
| 689 | a2 += linesz; |
| 690 | a3 += linesz; |
| 691 | asm volatile("movca.l r0, @%0\n\t" |
| 692 | "movca.l r0, @%1\n\t" |
| 693 | "movca.l r0, @%2\n\t" |
| 694 | "movca.l r0, @%3\n\t" |
| 695 | "ocbi @%0\n\t" |
| 696 | "ocbi @%1\n\t" |
| 697 | "ocbi @%2\n\t" |
| 698 | "ocbi @%3\n\t" : : |
| 699 | "r" (a0), "r" (a1), "r" (a2), "r" (a3)); |
| 700 | a0 += linesz; |
| 701 | a1 += linesz; |
| 702 | a2 += linesz; |
| 703 | a3 += linesz; |
| 704 | asm volatile("movca.l r0, @%0\n\t" |
| 705 | "movca.l r0, @%1\n\t" |
| 706 | "movca.l r0, @%2\n\t" |
| 707 | "movca.l r0, @%3\n\t" |
| 708 | "ocbi @%0\n\t" |
| 709 | "ocbi @%1\n\t" |
| 710 | "ocbi @%2\n\t" |
| 711 | "ocbi @%3\n\t" : : |
| 712 | "r" (a0), "r" (a1), "r" (a2), "r" (a3)); |
| 713 | a0 += linesz; |
| 714 | a1 += linesz; |
| 715 | a2 += linesz; |
| 716 | a3 += linesz; |
| 717 | asm volatile("movca.l r0, @%0\n\t" |
| 718 | "movca.l r0, @%1\n\t" |
| 719 | "movca.l r0, @%2\n\t" |
| 720 | "movca.l r0, @%3\n\t" |
| 721 | "ocbi @%0\n\t" |
| 722 | "ocbi @%1\n\t" |
| 723 | "ocbi @%2\n\t" |
| 724 | "ocbi @%3\n\t" : : |
| 725 | "r" (a0), "r" (a1), "r" (a2), "r" (a3)); |
| 726 | asm volatile("ldc %0, sr" : : "r" (orig_sr)); |
| 727 | a0 += linesz; |
| 728 | a1 += linesz; |
| 729 | a2 += linesz; |
| 730 | a3 += linesz; |
| 731 | } while (a0 < a0e); |
| 732 | } |