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