Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 2 | * arch/sh/mm/cache.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1999, 2000, 2002 Niibe Yutaka |
Paul Mundt | dfff0fa | 2009-07-27 20:53:22 +0900 | [diff] [blame] | 5 | * Copyright (C) 2002 - 2009 Paul Mundt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Released under the terms of the GNU GPL v2.0. |
| 8 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/mm.h> |
Paul Mundt | acca4f4 | 2008-11-10 20:00:45 +0900 | [diff] [blame] | 10 | #include <linux/init.h> |
Paul Mundt | 52e2778 | 2006-11-21 11:09:41 +0900 | [diff] [blame] | 11 | #include <linux/mutex.h> |
Paul Mundt | e06c4e5 | 2007-07-31 13:01:43 +0900 | [diff] [blame] | 12 | #include <linux/fs.h> |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 13 | #include <linux/smp.h> |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 14 | #include <linux/highmem.h> |
| 15 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/mmu_context.h> |
| 17 | #include <asm/cacheflush.h> |
| 18 | |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 19 | void (*local_flush_cache_all)(void *args) = cache_noop; |
| 20 | void (*local_flush_cache_mm)(void *args) = cache_noop; |
| 21 | void (*local_flush_cache_dup_mm)(void *args) = cache_noop; |
| 22 | void (*local_flush_cache_page)(void *args) = cache_noop; |
| 23 | void (*local_flush_cache_range)(void *args) = cache_noop; |
| 24 | void (*local_flush_dcache_page)(void *args) = cache_noop; |
| 25 | void (*local_flush_icache_range)(void *args) = cache_noop; |
| 26 | void (*local_flush_icache_page)(void *args) = cache_noop; |
| 27 | void (*local_flush_cache_sigtramp)(void *args) = cache_noop; |
| 28 | |
Paul Mundt | 37443ef | 2009-08-15 12:29:49 +0900 | [diff] [blame] | 29 | void (*__flush_wback_region)(void *start, int size); |
| 30 | void (*__flush_purge_region)(void *start, int size); |
| 31 | void (*__flush_invalidate_region)(void *start, int size); |
| 32 | |
Paul Mundt | 37443ef | 2009-08-15 12:29:49 +0900 | [diff] [blame] | 33 | static inline void noop__flush_region(void *start, int size) |
| 34 | { |
| 35 | } |
| 36 | |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 37 | static inline void cacheop_on_each_cpu(void (*func) (void *info), void *info, |
| 38 | int wait) |
| 39 | { |
| 40 | preempt_disable(); |
| 41 | smp_call_function(func, info, wait); |
| 42 | func(info); |
| 43 | preempt_enable(); |
| 44 | } |
| 45 | |
Paul Mundt | ba1789e | 2007-11-05 16:18:16 +0900 | [diff] [blame] | 46 | void copy_to_user_page(struct vm_area_struct *vma, struct page *page, |
| 47 | unsigned long vaddr, void *dst, const void *src, |
| 48 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 50 | if (boot_cpu_data.dcache.n_aliases && page_mapped(page) && |
| 51 | !test_bit(PG_dcache_dirty, &page->flags)) { |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 52 | void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK); |
| 53 | memcpy(vto, src, len); |
Paul Mundt | b5eb10a | 2009-08-04 16:00:36 +0900 | [diff] [blame] | 54 | kunmap_coherent(); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 55 | } else { |
| 56 | memcpy(dst, src, len); |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 57 | if (boot_cpu_data.dcache.n_aliases) |
| 58 | set_bit(PG_dcache_dirty, &page->flags); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 59 | } |
Paul Mundt | ba1789e | 2007-11-05 16:18:16 +0900 | [diff] [blame] | 60 | |
| 61 | if (vma->vm_flags & VM_EXEC) |
| 62 | flush_cache_page(vma, vaddr, page_to_pfn(page)); |
| 63 | } |
| 64 | |
| 65 | void copy_from_user_page(struct vm_area_struct *vma, struct page *page, |
| 66 | unsigned long vaddr, void *dst, const void *src, |
| 67 | unsigned long len) |
| 68 | { |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 69 | if (boot_cpu_data.dcache.n_aliases && page_mapped(page) && |
| 70 | !test_bit(PG_dcache_dirty, &page->flags)) { |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 71 | void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK); |
| 72 | memcpy(dst, vfrom, len); |
Paul Mundt | b5eb10a | 2009-08-04 16:00:36 +0900 | [diff] [blame] | 73 | kunmap_coherent(); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 74 | } else { |
| 75 | memcpy(dst, src, len); |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 76 | if (boot_cpu_data.dcache.n_aliases) |
| 77 | set_bit(PG_dcache_dirty, &page->flags); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | } |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 80 | |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 81 | void copy_user_highpage(struct page *to, struct page *from, |
| 82 | unsigned long vaddr, struct vm_area_struct *vma) |
| 83 | { |
| 84 | void *vfrom, *vto; |
| 85 | |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 86 | vto = kmap_atomic(to, KM_USER1); |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 87 | |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 88 | if (boot_cpu_data.dcache.n_aliases && page_mapped(from) && |
| 89 | !test_bit(PG_dcache_dirty, &from->flags)) { |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 90 | vfrom = kmap_coherent(from, vaddr); |
| 91 | copy_page(vto, vfrom); |
Paul Mundt | b5eb10a | 2009-08-04 16:00:36 +0900 | [diff] [blame] | 92 | kunmap_coherent(); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 93 | } else { |
| 94 | vfrom = kmap_atomic(from, KM_USER0); |
| 95 | copy_page(vto, vfrom); |
| 96 | kunmap_atomic(vfrom, KM_USER0); |
| 97 | } |
| 98 | |
| 99 | if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK)) |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 100 | __flush_wback_region(vto, PAGE_SIZE); |
| 101 | |
| 102 | kunmap_atomic(vto, KM_USER1); |
| 103 | /* Make sure this page is cleared on other CPU's too before using it */ |
| 104 | smp_wmb(); |
| 105 | } |
| 106 | EXPORT_SYMBOL(copy_user_highpage); |
Paul Mundt | dfff0fa | 2009-07-27 20:53:22 +0900 | [diff] [blame] | 107 | |
| 108 | void clear_user_highpage(struct page *page, unsigned long vaddr) |
| 109 | { |
| 110 | void *kaddr = kmap_atomic(page, KM_USER0); |
| 111 | |
| 112 | clear_page(kaddr); |
| 113 | |
| 114 | if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK)) |
| 115 | __flush_wback_region(kaddr, PAGE_SIZE); |
| 116 | |
| 117 | kunmap_atomic(kaddr, KM_USER0); |
| 118 | } |
| 119 | EXPORT_SYMBOL(clear_user_highpage); |
Paul Mundt | 9cef749 | 2009-07-29 00:12:17 +0900 | [diff] [blame] | 120 | |
| 121 | void __update_cache(struct vm_area_struct *vma, |
| 122 | unsigned long address, pte_t pte) |
| 123 | { |
| 124 | struct page *page; |
| 125 | unsigned long pfn = pte_pfn(pte); |
| 126 | |
| 127 | if (!boot_cpu_data.dcache.n_aliases) |
| 128 | return; |
| 129 | |
| 130 | page = pfn_to_page(pfn); |
| 131 | if (pfn_valid(pfn) && page_mapping(page)) { |
| 132 | int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags); |
| 133 | if (dirty) { |
| 134 | unsigned long addr = (unsigned long)page_address(page); |
| 135 | |
| 136 | if (pages_do_alias(addr, address & PAGE_MASK)) |
| 137 | __flush_wback_region((void *)addr, PAGE_SIZE); |
| 138 | } |
| 139 | } |
| 140 | } |
Paul Mundt | c0fe478 | 2009-08-04 16:02:43 +0900 | [diff] [blame] | 141 | |
| 142 | void __flush_anon_page(struct page *page, unsigned long vmaddr) |
| 143 | { |
| 144 | unsigned long addr = (unsigned long) page_address(page); |
| 145 | |
| 146 | if (pages_do_alias(addr, vmaddr)) { |
| 147 | if (boot_cpu_data.dcache.n_aliases && page_mapped(page) && |
| 148 | !test_bit(PG_dcache_dirty, &page->flags)) { |
| 149 | void *kaddr; |
| 150 | |
| 151 | kaddr = kmap_coherent(page, vmaddr); |
| 152 | __flush_wback_region((void *)kaddr, PAGE_SIZE); |
| 153 | kunmap_coherent(); |
| 154 | } else |
| 155 | __flush_wback_region((void *)addr, PAGE_SIZE); |
| 156 | } |
| 157 | } |
Paul Mundt | ecba106 | 2009-08-15 11:05:42 +0900 | [diff] [blame] | 158 | |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 159 | void flush_cache_all(void) |
| 160 | { |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 161 | cacheop_on_each_cpu(local_flush_cache_all, NULL, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void flush_cache_mm(struct mm_struct *mm) |
| 165 | { |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 166 | cacheop_on_each_cpu(local_flush_cache_mm, mm, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void flush_cache_dup_mm(struct mm_struct *mm) |
| 170 | { |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 171 | cacheop_on_each_cpu(local_flush_cache_dup_mm, mm, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void flush_cache_page(struct vm_area_struct *vma, unsigned long addr, |
| 175 | unsigned long pfn) |
| 176 | { |
| 177 | struct flusher_data data; |
| 178 | |
| 179 | data.vma = vma; |
| 180 | data.addr1 = addr; |
| 181 | data.addr2 = pfn; |
| 182 | |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 183 | cacheop_on_each_cpu(local_flush_cache_page, (void *)&data, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void flush_cache_range(struct vm_area_struct *vma, unsigned long start, |
| 187 | unsigned long end) |
| 188 | { |
| 189 | struct flusher_data data; |
| 190 | |
| 191 | data.vma = vma; |
| 192 | data.addr1 = start; |
| 193 | data.addr2 = end; |
| 194 | |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 195 | cacheop_on_each_cpu(local_flush_cache_range, (void *)&data, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void flush_dcache_page(struct page *page) |
| 199 | { |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 200 | cacheop_on_each_cpu(local_flush_dcache_page, page, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | void flush_icache_range(unsigned long start, unsigned long end) |
| 204 | { |
| 205 | struct flusher_data data; |
| 206 | |
| 207 | data.vma = NULL; |
| 208 | data.addr1 = start; |
| 209 | data.addr2 = end; |
| 210 | |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 211 | cacheop_on_each_cpu(local_flush_icache_range, (void *)&data, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | void flush_icache_page(struct vm_area_struct *vma, struct page *page) |
| 215 | { |
| 216 | /* Nothing uses the VMA, so just pass the struct page along */ |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 217 | cacheop_on_each_cpu(local_flush_icache_page, page, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | void flush_cache_sigtramp(unsigned long address) |
| 221 | { |
Paul Mundt | 6f37957 | 2009-09-01 21:21:36 +0900 | [diff] [blame^] | 222 | cacheop_on_each_cpu(local_flush_cache_sigtramp, (void *)address, 1); |
Paul Mundt | f26b2a5 | 2009-08-21 17:23:14 +0900 | [diff] [blame] | 223 | } |
| 224 | |
Paul Mundt | 27d59ec | 2009-08-15 11:11:16 +0900 | [diff] [blame] | 225 | static void compute_alias(struct cache_info *c) |
| 226 | { |
| 227 | c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1); |
| 228 | c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0; |
| 229 | } |
| 230 | |
| 231 | static void __init emit_cache_params(void) |
| 232 | { |
| 233 | printk(KERN_NOTICE "I-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
| 234 | boot_cpu_data.icache.ways, |
| 235 | boot_cpu_data.icache.sets, |
| 236 | boot_cpu_data.icache.way_incr); |
| 237 | printk(KERN_NOTICE "I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
| 238 | boot_cpu_data.icache.entry_mask, |
| 239 | boot_cpu_data.icache.alias_mask, |
| 240 | boot_cpu_data.icache.n_aliases); |
| 241 | printk(KERN_NOTICE "D-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
| 242 | boot_cpu_data.dcache.ways, |
| 243 | boot_cpu_data.dcache.sets, |
| 244 | boot_cpu_data.dcache.way_incr); |
| 245 | printk(KERN_NOTICE "D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
| 246 | boot_cpu_data.dcache.entry_mask, |
| 247 | boot_cpu_data.dcache.alias_mask, |
| 248 | boot_cpu_data.dcache.n_aliases); |
| 249 | |
| 250 | /* |
| 251 | * Emit Secondary Cache parameters if the CPU has a probed L2. |
| 252 | */ |
| 253 | if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) { |
| 254 | printk(KERN_NOTICE "S-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
| 255 | boot_cpu_data.scache.ways, |
| 256 | boot_cpu_data.scache.sets, |
| 257 | boot_cpu_data.scache.way_incr); |
| 258 | printk(KERN_NOTICE "S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
| 259 | boot_cpu_data.scache.entry_mask, |
| 260 | boot_cpu_data.scache.alias_mask, |
| 261 | boot_cpu_data.scache.n_aliases); |
| 262 | } |
| 263 | } |
| 264 | |
Paul Mundt | ecba106 | 2009-08-15 11:05:42 +0900 | [diff] [blame] | 265 | void __init cpu_cache_init(void) |
| 266 | { |
Paul Mundt | 27d59ec | 2009-08-15 11:11:16 +0900 | [diff] [blame] | 267 | compute_alias(&boot_cpu_data.icache); |
| 268 | compute_alias(&boot_cpu_data.dcache); |
| 269 | compute_alias(&boot_cpu_data.scache); |
| 270 | |
Paul Mundt | 37443ef | 2009-08-15 12:29:49 +0900 | [diff] [blame] | 271 | __flush_wback_region = noop__flush_region; |
| 272 | __flush_purge_region = noop__flush_region; |
| 273 | __flush_invalidate_region = noop__flush_region; |
| 274 | |
Paul Mundt | 109b44a | 2009-08-15 12:35:15 +0900 | [diff] [blame] | 275 | if (boot_cpu_data.family == CPU_FAMILY_SH2) { |
| 276 | extern void __weak sh2_cache_init(void); |
| 277 | |
| 278 | sh2_cache_init(); |
| 279 | } |
| 280 | |
Paul Mundt | a58e1a2 | 2009-08-15 12:38:29 +0900 | [diff] [blame] | 281 | if (boot_cpu_data.family == CPU_FAMILY_SH2A) { |
| 282 | extern void __weak sh2a_cache_init(void); |
| 283 | |
| 284 | sh2a_cache_init(); |
| 285 | } |
| 286 | |
Paul Mundt | 79f1c9d | 2009-08-15 12:42:55 +0900 | [diff] [blame] | 287 | if (boot_cpu_data.family == CPU_FAMILY_SH3) { |
| 288 | extern void __weak sh3_cache_init(void); |
| 289 | |
| 290 | sh3_cache_init(); |
Paul Mundt | 0d051d9 | 2009-08-15 12:53:39 +0900 | [diff] [blame] | 291 | |
| 292 | if ((boot_cpu_data.type == CPU_SH7705) && |
| 293 | (boot_cpu_data.dcache.sets == 512)) { |
| 294 | extern void __weak sh7705_cache_init(void); |
| 295 | |
| 296 | sh7705_cache_init(); |
| 297 | } |
Paul Mundt | 79f1c9d | 2009-08-15 12:42:55 +0900 | [diff] [blame] | 298 | } |
| 299 | |
Paul Mundt | ecba106 | 2009-08-15 11:05:42 +0900 | [diff] [blame] | 300 | if ((boot_cpu_data.family == CPU_FAMILY_SH4) || |
| 301 | (boot_cpu_data.family == CPU_FAMILY_SH4A) || |
| 302 | (boot_cpu_data.family == CPU_FAMILY_SH4AL_DSP)) { |
| 303 | extern void __weak sh4_cache_init(void); |
| 304 | |
| 305 | sh4_cache_init(); |
| 306 | } |
Paul Mundt | 27d59ec | 2009-08-15 11:11:16 +0900 | [diff] [blame] | 307 | |
Paul Mundt | 2b43151 | 2009-08-16 02:16:44 +0900 | [diff] [blame] | 308 | if (boot_cpu_data.family == CPU_FAMILY_SH5) { |
| 309 | extern void __weak sh5_cache_init(void); |
| 310 | |
| 311 | sh5_cache_init(); |
| 312 | } |
| 313 | |
Paul Mundt | 27d59ec | 2009-08-15 11:11:16 +0900 | [diff] [blame] | 314 | emit_cache_params(); |
Paul Mundt | ecba106 | 2009-08-15 11:05:42 +0900 | [diff] [blame] | 315 | } |