Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 2 | * arch/sh/mm/pg-mmu.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 | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 13 | #include <linux/highmem.h> |
| 14 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <asm/mmu_context.h> |
| 16 | #include <asm/cacheflush.h> |
| 17 | |
Paul Mundt | 37443ef | 2009-08-15 12:29:49 +0900 | [diff] [blame] | 18 | void (*flush_cache_all)(void); |
| 19 | void (*flush_cache_mm)(struct mm_struct *mm); |
| 20 | void (*flush_cache_dup_mm)(struct mm_struct *mm); |
| 21 | void (*flush_cache_page)(struct vm_area_struct *vma, |
| 22 | unsigned long addr, unsigned long pfn); |
| 23 | void (*flush_cache_range)(struct vm_area_struct *vma, |
| 24 | unsigned long start, unsigned long end); |
| 25 | void (*flush_dcache_page)(struct page *page); |
| 26 | void (*flush_icache_range)(unsigned long start, unsigned long end); |
| 27 | void (*flush_icache_page)(struct vm_area_struct *vma, |
| 28 | struct page *page); |
| 29 | void (*flush_cache_sigtramp)(unsigned long address); |
| 30 | void (*__flush_wback_region)(void *start, int size); |
| 31 | void (*__flush_purge_region)(void *start, int size); |
| 32 | void (*__flush_invalidate_region)(void *start, int size); |
| 33 | |
| 34 | static inline void noop_flush_cache_all(void) |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | static inline void noop_flush_cache_mm(struct mm_struct *mm) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | static inline void noop_flush_cache_page(struct vm_area_struct *vma, |
| 43 | unsigned long addr, unsigned long pfn) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | static inline void noop_flush_cache_range(struct vm_area_struct *vma, |
| 48 | unsigned long start, unsigned long end) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | static inline void noop_flush_dcache_page(struct page *page) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | static inline void noop_flush_icache_range(unsigned long start, |
| 57 | unsigned long end) |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | static inline void noop_flush_icache_page(struct vm_area_struct *vma, |
| 62 | struct page *page) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | static inline void noop_flush_cache_sigtramp(unsigned long address) |
| 67 | { |
| 68 | } |
| 69 | |
| 70 | static inline void noop__flush_region(void *start, int size) |
| 71 | { |
| 72 | } |
| 73 | |
Paul Mundt | ba1789e | 2007-11-05 16:18:16 +0900 | [diff] [blame] | 74 | void copy_to_user_page(struct vm_area_struct *vma, struct page *page, |
| 75 | unsigned long vaddr, void *dst, const void *src, |
| 76 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 78 | if (boot_cpu_data.dcache.n_aliases && page_mapped(page) && |
| 79 | !test_bit(PG_dcache_dirty, &page->flags)) { |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 80 | void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK); |
| 81 | memcpy(vto, src, len); |
Paul Mundt | b5eb10a | 2009-08-04 16:00:36 +0900 | [diff] [blame] | 82 | kunmap_coherent(); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 83 | } else { |
| 84 | memcpy(dst, src, len); |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 85 | if (boot_cpu_data.dcache.n_aliases) |
| 86 | set_bit(PG_dcache_dirty, &page->flags); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 87 | } |
Paul Mundt | ba1789e | 2007-11-05 16:18:16 +0900 | [diff] [blame] | 88 | |
| 89 | if (vma->vm_flags & VM_EXEC) |
| 90 | flush_cache_page(vma, vaddr, page_to_pfn(page)); |
| 91 | } |
| 92 | |
| 93 | void copy_from_user_page(struct vm_area_struct *vma, struct page *page, |
| 94 | unsigned long vaddr, void *dst, const void *src, |
| 95 | unsigned long len) |
| 96 | { |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 97 | if (boot_cpu_data.dcache.n_aliases && page_mapped(page) && |
| 98 | !test_bit(PG_dcache_dirty, &page->flags)) { |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 99 | void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK); |
| 100 | memcpy(dst, vfrom, len); |
Paul Mundt | b5eb10a | 2009-08-04 16:00:36 +0900 | [diff] [blame] | 101 | kunmap_coherent(); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 102 | } else { |
| 103 | memcpy(dst, src, len); |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 104 | if (boot_cpu_data.dcache.n_aliases) |
| 105 | set_bit(PG_dcache_dirty, &page->flags); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
Paul Mundt | 39e688a | 2007-03-05 19:46:47 +0900 | [diff] [blame] | 108 | |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 109 | void copy_user_highpage(struct page *to, struct page *from, |
| 110 | unsigned long vaddr, struct vm_area_struct *vma) |
| 111 | { |
| 112 | void *vfrom, *vto; |
| 113 | |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 114 | vto = kmap_atomic(to, KM_USER1); |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 115 | |
Paul Mundt | 0dfae7d | 2009-07-27 21:30:17 +0900 | [diff] [blame] | 116 | if (boot_cpu_data.dcache.n_aliases && page_mapped(from) && |
| 117 | !test_bit(PG_dcache_dirty, &from->flags)) { |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 118 | vfrom = kmap_coherent(from, vaddr); |
| 119 | copy_page(vto, vfrom); |
Paul Mundt | b5eb10a | 2009-08-04 16:00:36 +0900 | [diff] [blame] | 120 | kunmap_coherent(); |
Paul Mundt | 2277ab4 | 2009-07-22 19:20:49 +0900 | [diff] [blame] | 121 | } else { |
| 122 | vfrom = kmap_atomic(from, KM_USER0); |
| 123 | copy_page(vto, vfrom); |
| 124 | kunmap_atomic(vfrom, KM_USER0); |
| 125 | } |
| 126 | |
| 127 | if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK)) |
Paul Mundt | 7747b9a | 2007-11-05 16:12:32 +0900 | [diff] [blame] | 128 | __flush_wback_region(vto, PAGE_SIZE); |
| 129 | |
| 130 | kunmap_atomic(vto, KM_USER1); |
| 131 | /* Make sure this page is cleared on other CPU's too before using it */ |
| 132 | smp_wmb(); |
| 133 | } |
| 134 | EXPORT_SYMBOL(copy_user_highpage); |
Paul Mundt | dfff0fa | 2009-07-27 20:53:22 +0900 | [diff] [blame] | 135 | |
| 136 | void clear_user_highpage(struct page *page, unsigned long vaddr) |
| 137 | { |
| 138 | void *kaddr = kmap_atomic(page, KM_USER0); |
| 139 | |
| 140 | clear_page(kaddr); |
| 141 | |
| 142 | if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK)) |
| 143 | __flush_wback_region(kaddr, PAGE_SIZE); |
| 144 | |
| 145 | kunmap_atomic(kaddr, KM_USER0); |
| 146 | } |
| 147 | EXPORT_SYMBOL(clear_user_highpage); |
Paul Mundt | 9cef749 | 2009-07-29 00:12:17 +0900 | [diff] [blame] | 148 | |
| 149 | void __update_cache(struct vm_area_struct *vma, |
| 150 | unsigned long address, pte_t pte) |
| 151 | { |
| 152 | struct page *page; |
| 153 | unsigned long pfn = pte_pfn(pte); |
| 154 | |
| 155 | if (!boot_cpu_data.dcache.n_aliases) |
| 156 | return; |
| 157 | |
| 158 | page = pfn_to_page(pfn); |
| 159 | if (pfn_valid(pfn) && page_mapping(page)) { |
| 160 | int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags); |
| 161 | if (dirty) { |
| 162 | unsigned long addr = (unsigned long)page_address(page); |
| 163 | |
| 164 | if (pages_do_alias(addr, address & PAGE_MASK)) |
| 165 | __flush_wback_region((void *)addr, PAGE_SIZE); |
| 166 | } |
| 167 | } |
| 168 | } |
Paul Mundt | c0fe478 | 2009-08-04 16:02:43 +0900 | [diff] [blame] | 169 | |
| 170 | void __flush_anon_page(struct page *page, unsigned long vmaddr) |
| 171 | { |
| 172 | unsigned long addr = (unsigned long) page_address(page); |
| 173 | |
| 174 | if (pages_do_alias(addr, vmaddr)) { |
| 175 | if (boot_cpu_data.dcache.n_aliases && page_mapped(page) && |
| 176 | !test_bit(PG_dcache_dirty, &page->flags)) { |
| 177 | void *kaddr; |
| 178 | |
| 179 | kaddr = kmap_coherent(page, vmaddr); |
| 180 | __flush_wback_region((void *)kaddr, PAGE_SIZE); |
| 181 | kunmap_coherent(); |
| 182 | } else |
| 183 | __flush_wback_region((void *)addr, PAGE_SIZE); |
| 184 | } |
| 185 | } |
Paul Mundt | ecba106 | 2009-08-15 11:05:42 +0900 | [diff] [blame] | 186 | |
Paul Mundt | 27d59ec | 2009-08-15 11:11:16 +0900 | [diff] [blame] | 187 | static void compute_alias(struct cache_info *c) |
| 188 | { |
| 189 | c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1); |
| 190 | c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0; |
| 191 | } |
| 192 | |
| 193 | static void __init emit_cache_params(void) |
| 194 | { |
| 195 | printk(KERN_NOTICE "I-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
| 196 | boot_cpu_data.icache.ways, |
| 197 | boot_cpu_data.icache.sets, |
| 198 | boot_cpu_data.icache.way_incr); |
| 199 | printk(KERN_NOTICE "I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
| 200 | boot_cpu_data.icache.entry_mask, |
| 201 | boot_cpu_data.icache.alias_mask, |
| 202 | boot_cpu_data.icache.n_aliases); |
| 203 | printk(KERN_NOTICE "D-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
| 204 | boot_cpu_data.dcache.ways, |
| 205 | boot_cpu_data.dcache.sets, |
| 206 | boot_cpu_data.dcache.way_incr); |
| 207 | printk(KERN_NOTICE "D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
| 208 | boot_cpu_data.dcache.entry_mask, |
| 209 | boot_cpu_data.dcache.alias_mask, |
| 210 | boot_cpu_data.dcache.n_aliases); |
| 211 | |
| 212 | /* |
| 213 | * Emit Secondary Cache parameters if the CPU has a probed L2. |
| 214 | */ |
| 215 | if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) { |
| 216 | printk(KERN_NOTICE "S-cache : n_ways=%d n_sets=%d way_incr=%d\n", |
| 217 | boot_cpu_data.scache.ways, |
| 218 | boot_cpu_data.scache.sets, |
| 219 | boot_cpu_data.scache.way_incr); |
| 220 | printk(KERN_NOTICE "S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n", |
| 221 | boot_cpu_data.scache.entry_mask, |
| 222 | boot_cpu_data.scache.alias_mask, |
| 223 | boot_cpu_data.scache.n_aliases); |
| 224 | } |
| 225 | } |
| 226 | |
Paul Mundt | ecba106 | 2009-08-15 11:05:42 +0900 | [diff] [blame] | 227 | void __init cpu_cache_init(void) |
| 228 | { |
Paul Mundt | 27d59ec | 2009-08-15 11:11:16 +0900 | [diff] [blame] | 229 | compute_alias(&boot_cpu_data.icache); |
| 230 | compute_alias(&boot_cpu_data.dcache); |
| 231 | compute_alias(&boot_cpu_data.scache); |
| 232 | |
Paul Mundt | 37443ef | 2009-08-15 12:29:49 +0900 | [diff] [blame] | 233 | flush_cache_all = noop_flush_cache_all; |
| 234 | flush_cache_mm = noop_flush_cache_mm; |
| 235 | flush_cache_dup_mm = noop_flush_cache_mm; |
| 236 | flush_cache_page = noop_flush_cache_page; |
| 237 | flush_cache_range = noop_flush_cache_range; |
| 238 | flush_dcache_page = noop_flush_dcache_page; |
| 239 | flush_icache_range = noop_flush_icache_range; |
| 240 | flush_icache_page = noop_flush_icache_page; |
| 241 | flush_cache_sigtramp = noop_flush_cache_sigtramp; |
| 242 | |
| 243 | __flush_wback_region = noop__flush_region; |
| 244 | __flush_purge_region = noop__flush_region; |
| 245 | __flush_invalidate_region = noop__flush_region; |
| 246 | |
Paul Mundt | 109b44a | 2009-08-15 12:35:15 +0900 | [diff] [blame] | 247 | if (boot_cpu_data.family == CPU_FAMILY_SH2) { |
| 248 | extern void __weak sh2_cache_init(void); |
| 249 | |
| 250 | sh2_cache_init(); |
| 251 | } |
| 252 | |
Paul Mundt | a58e1a2 | 2009-08-15 12:38:29 +0900 | [diff] [blame] | 253 | if (boot_cpu_data.family == CPU_FAMILY_SH2A) { |
| 254 | extern void __weak sh2a_cache_init(void); |
| 255 | |
| 256 | sh2a_cache_init(); |
| 257 | } |
| 258 | |
Paul Mundt | 79f1c9d | 2009-08-15 12:42:55 +0900 | [diff] [blame^] | 259 | if (boot_cpu_data.family == CPU_FAMILY_SH3) { |
| 260 | extern void __weak sh3_cache_init(void); |
| 261 | |
| 262 | sh3_cache_init(); |
| 263 | } |
| 264 | |
Paul Mundt | ecba106 | 2009-08-15 11:05:42 +0900 | [diff] [blame] | 265 | if ((boot_cpu_data.family == CPU_FAMILY_SH4) || |
| 266 | (boot_cpu_data.family == CPU_FAMILY_SH4A) || |
| 267 | (boot_cpu_data.family == CPU_FAMILY_SH4AL_DSP)) { |
| 268 | extern void __weak sh4_cache_init(void); |
| 269 | |
| 270 | sh4_cache_init(); |
| 271 | } |
Paul Mundt | 27d59ec | 2009-08-15 11:11:16 +0900 | [diff] [blame] | 272 | |
| 273 | emit_cache_params(); |
Paul Mundt | ecba106 | 2009-08-15 11:05:42 +0900 | [diff] [blame] | 274 | } |