blob: 4aa9260545316f5ff944afe8b8319055b6aabb3d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Paul Mundtf26b2a52009-08-21 17:23:14 +09002 * arch/sh/mm/cache.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1999, 2000, 2002 Niibe Yutaka
Paul Mundtdfff0fa2009-07-27 20:53:22 +09005 * Copyright (C) 2002 - 2009 Paul Mundt
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Released under the terms of the GNU GPL v2.0.
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/mm.h>
Paul Mundtacca4f42008-11-10 20:00:45 +090010#include <linux/init.h>
Paul Mundt52e27782006-11-21 11:09:41 +090011#include <linux/mutex.h>
Paul Mundte06c4e52007-07-31 13:01:43 +090012#include <linux/fs.h>
Paul Mundtf26b2a52009-08-21 17:23:14 +090013#include <linux/smp.h>
Paul Mundt7747b9a2007-11-05 16:12:32 +090014#include <linux/highmem.h>
15#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/mmu_context.h>
17#include <asm/cacheflush.h>
18
Paul Mundtf26b2a52009-08-21 17:23:14 +090019void (*local_flush_cache_all)(void *args) = cache_noop;
20void (*local_flush_cache_mm)(void *args) = cache_noop;
21void (*local_flush_cache_dup_mm)(void *args) = cache_noop;
22void (*local_flush_cache_page)(void *args) = cache_noop;
23void (*local_flush_cache_range)(void *args) = cache_noop;
24void (*local_flush_dcache_page)(void *args) = cache_noop;
25void (*local_flush_icache_range)(void *args) = cache_noop;
26void (*local_flush_icache_page)(void *args) = cache_noop;
27void (*local_flush_cache_sigtramp)(void *args) = cache_noop;
28
Paul Mundt37443ef2009-08-15 12:29:49 +090029void (*__flush_wback_region)(void *start, int size);
30void (*__flush_purge_region)(void *start, int size);
31void (*__flush_invalidate_region)(void *start, int size);
32
Paul Mundt37443ef2009-08-15 12:29:49 +090033static inline void noop__flush_region(void *start, int size)
34{
35}
36
Paul Mundt6f379572009-09-01 21:21:36 +090037static 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 Mundtba1789e2007-11-05 16:18:16 +090046void 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 Torvalds1da177e2005-04-16 15:20:36 -070049{
Paul Mundt0dfae7d2009-07-27 21:30:17 +090050 if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
51 !test_bit(PG_dcache_dirty, &page->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090052 void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
53 memcpy(vto, src, len);
Paul Mundt0906a3a2009-09-03 17:21:10 +090054 kunmap_coherent(vto);
Paul Mundt2277ab42009-07-22 19:20:49 +090055 } else {
56 memcpy(dst, src, len);
Paul Mundt0dfae7d2009-07-27 21:30:17 +090057 if (boot_cpu_data.dcache.n_aliases)
58 set_bit(PG_dcache_dirty, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +090059 }
Paul Mundtba1789e2007-11-05 16:18:16 +090060
61 if (vma->vm_flags & VM_EXEC)
62 flush_cache_page(vma, vaddr, page_to_pfn(page));
63}
64
65void 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 Mundt0dfae7d2009-07-27 21:30:17 +090069 if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
70 !test_bit(PG_dcache_dirty, &page->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090071 void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
72 memcpy(dst, vfrom, len);
Paul Mundt0906a3a2009-09-03 17:21:10 +090073 kunmap_coherent(vfrom);
Paul Mundt2277ab42009-07-22 19:20:49 +090074 } else {
75 memcpy(dst, src, len);
Paul Mundt0dfae7d2009-07-27 21:30:17 +090076 if (boot_cpu_data.dcache.n_aliases)
77 set_bit(PG_dcache_dirty, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +090078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Paul Mundt39e688a2007-03-05 19:46:47 +090080
Paul Mundt7747b9a2007-11-05 16:12:32 +090081void 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 Mundt7747b9a2007-11-05 16:12:32 +090086 vto = kmap_atomic(to, KM_USER1);
Paul Mundt7747b9a2007-11-05 16:12:32 +090087
Paul Mundt0dfae7d2009-07-27 21:30:17 +090088 if (boot_cpu_data.dcache.n_aliases && page_mapped(from) &&
89 !test_bit(PG_dcache_dirty, &from->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090090 vfrom = kmap_coherent(from, vaddr);
91 copy_page(vto, vfrom);
Paul Mundt0906a3a2009-09-03 17:21:10 +090092 kunmap_coherent(vfrom);
Paul Mundt2277ab42009-07-22 19:20:49 +090093 } 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 Mundt6e4154d2009-09-08 16:21:00 +0900100 __flush_purge_region(vto, PAGE_SIZE);
Paul Mundt7747b9a2007-11-05 16:12:32 +0900101
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}
106EXPORT_SYMBOL(copy_user_highpage);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900107
108void 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))
Paul Mundt6e4154d2009-09-08 16:21:00 +0900115 __flush_purge_region(kaddr, PAGE_SIZE);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900116
117 kunmap_atomic(kaddr, KM_USER0);
118}
119EXPORT_SYMBOL(clear_user_highpage);
Paul Mundt9cef7492009-07-29 00:12:17 +0900120
121void __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))
Paul Mundt6e4154d2009-09-08 16:21:00 +0900137 __flush_purge_region((void *)addr, PAGE_SIZE);
Paul Mundt9cef7492009-07-29 00:12:17 +0900138 }
139 }
140}
Paul Mundtc0fe4782009-08-04 16:02:43 +0900141
142void __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);
Paul Mundt6e4154d2009-09-08 16:21:00 +0900152 /* XXX.. For now kunmap_coherent() does a purge */
153 /* __flush_purge_region((void *)kaddr, PAGE_SIZE); */
Paul Mundt0906a3a2009-09-03 17:21:10 +0900154 kunmap_coherent(kaddr);
Paul Mundtc0fe4782009-08-04 16:02:43 +0900155 } else
Paul Mundt6e4154d2009-09-08 16:21:00 +0900156 __flush_purge_region((void *)addr, PAGE_SIZE);
Paul Mundtc0fe4782009-08-04 16:02:43 +0900157 }
158}
Paul Mundtecba1062009-08-15 11:05:42 +0900159
Paul Mundtf26b2a52009-08-21 17:23:14 +0900160void flush_cache_all(void)
161{
Paul Mundt6f379572009-09-01 21:21:36 +0900162 cacheop_on_each_cpu(local_flush_cache_all, NULL, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900163}
164
165void flush_cache_mm(struct mm_struct *mm)
166{
Paul Mundt654d3642009-09-09 14:04:06 +0900167 if (boot_cpu_data.dcache.n_aliases == 0)
168 return;
169
Paul Mundt6f379572009-09-01 21:21:36 +0900170 cacheop_on_each_cpu(local_flush_cache_mm, mm, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900171}
172
173void flush_cache_dup_mm(struct mm_struct *mm)
174{
Paul Mundt654d3642009-09-09 14:04:06 +0900175 if (boot_cpu_data.dcache.n_aliases == 0)
176 return;
177
Paul Mundt6f379572009-09-01 21:21:36 +0900178 cacheop_on_each_cpu(local_flush_cache_dup_mm, mm, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900179}
180
181void flush_cache_page(struct vm_area_struct *vma, unsigned long addr,
182 unsigned long pfn)
183{
184 struct flusher_data data;
185
186 data.vma = vma;
187 data.addr1 = addr;
188 data.addr2 = pfn;
189
Paul Mundt6f379572009-09-01 21:21:36 +0900190 cacheop_on_each_cpu(local_flush_cache_page, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900191}
192
193void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
194 unsigned long end)
195{
196 struct flusher_data data;
197
198 data.vma = vma;
199 data.addr1 = start;
200 data.addr2 = end;
201
Paul Mundt6f379572009-09-01 21:21:36 +0900202 cacheop_on_each_cpu(local_flush_cache_range, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900203}
204
205void flush_dcache_page(struct page *page)
206{
Paul Mundt6f379572009-09-01 21:21:36 +0900207 cacheop_on_each_cpu(local_flush_dcache_page, page, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900208}
209
210void flush_icache_range(unsigned long start, unsigned long end)
211{
212 struct flusher_data data;
213
214 data.vma = NULL;
215 data.addr1 = start;
216 data.addr2 = end;
217
Paul Mundt6f379572009-09-01 21:21:36 +0900218 cacheop_on_each_cpu(local_flush_icache_range, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900219}
220
221void flush_icache_page(struct vm_area_struct *vma, struct page *page)
222{
223 /* Nothing uses the VMA, so just pass the struct page along */
Paul Mundt6f379572009-09-01 21:21:36 +0900224 cacheop_on_each_cpu(local_flush_icache_page, page, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900225}
226
227void flush_cache_sigtramp(unsigned long address)
228{
Paul Mundt6f379572009-09-01 21:21:36 +0900229 cacheop_on_each_cpu(local_flush_cache_sigtramp, (void *)address, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900230}
231
Paul Mundt27d59ec2009-08-15 11:11:16 +0900232static void compute_alias(struct cache_info *c)
233{
234 c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1);
235 c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0;
236}
237
238static void __init emit_cache_params(void)
239{
240 printk(KERN_NOTICE "I-cache : n_ways=%d n_sets=%d way_incr=%d\n",
241 boot_cpu_data.icache.ways,
242 boot_cpu_data.icache.sets,
243 boot_cpu_data.icache.way_incr);
244 printk(KERN_NOTICE "I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
245 boot_cpu_data.icache.entry_mask,
246 boot_cpu_data.icache.alias_mask,
247 boot_cpu_data.icache.n_aliases);
248 printk(KERN_NOTICE "D-cache : n_ways=%d n_sets=%d way_incr=%d\n",
249 boot_cpu_data.dcache.ways,
250 boot_cpu_data.dcache.sets,
251 boot_cpu_data.dcache.way_incr);
252 printk(KERN_NOTICE "D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
253 boot_cpu_data.dcache.entry_mask,
254 boot_cpu_data.dcache.alias_mask,
255 boot_cpu_data.dcache.n_aliases);
256
257 /*
258 * Emit Secondary Cache parameters if the CPU has a probed L2.
259 */
260 if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) {
261 printk(KERN_NOTICE "S-cache : n_ways=%d n_sets=%d way_incr=%d\n",
262 boot_cpu_data.scache.ways,
263 boot_cpu_data.scache.sets,
264 boot_cpu_data.scache.way_incr);
265 printk(KERN_NOTICE "S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
266 boot_cpu_data.scache.entry_mask,
267 boot_cpu_data.scache.alias_mask,
268 boot_cpu_data.scache.n_aliases);
269 }
270}
271
Paul Mundtecba1062009-08-15 11:05:42 +0900272void __init cpu_cache_init(void)
273{
Paul Mundt27d59ec2009-08-15 11:11:16 +0900274 compute_alias(&boot_cpu_data.icache);
275 compute_alias(&boot_cpu_data.dcache);
276 compute_alias(&boot_cpu_data.scache);
277
Paul Mundt37443ef2009-08-15 12:29:49 +0900278 __flush_wback_region = noop__flush_region;
279 __flush_purge_region = noop__flush_region;
280 __flush_invalidate_region = noop__flush_region;
281
Paul Mundt109b44a2009-08-15 12:35:15 +0900282 if (boot_cpu_data.family == CPU_FAMILY_SH2) {
283 extern void __weak sh2_cache_init(void);
284
285 sh2_cache_init();
286 }
287
Paul Mundta58e1a22009-08-15 12:38:29 +0900288 if (boot_cpu_data.family == CPU_FAMILY_SH2A) {
289 extern void __weak sh2a_cache_init(void);
290
291 sh2a_cache_init();
292 }
293
Paul Mundt79f1c9d2009-08-15 12:42:55 +0900294 if (boot_cpu_data.family == CPU_FAMILY_SH3) {
295 extern void __weak sh3_cache_init(void);
296
297 sh3_cache_init();
Paul Mundt0d051d92009-08-15 12:53:39 +0900298
299 if ((boot_cpu_data.type == CPU_SH7705) &&
300 (boot_cpu_data.dcache.sets == 512)) {
301 extern void __weak sh7705_cache_init(void);
302
303 sh7705_cache_init();
304 }
Paul Mundt79f1c9d2009-08-15 12:42:55 +0900305 }
306
Paul Mundtecba1062009-08-15 11:05:42 +0900307 if ((boot_cpu_data.family == CPU_FAMILY_SH4) ||
308 (boot_cpu_data.family == CPU_FAMILY_SH4A) ||
309 (boot_cpu_data.family == CPU_FAMILY_SH4AL_DSP)) {
310 extern void __weak sh4_cache_init(void);
311
312 sh4_cache_init();
313 }
Paul Mundt27d59ec2009-08-15 11:11:16 +0900314
Paul Mundt2b431512009-08-16 02:16:44 +0900315 if (boot_cpu_data.family == CPU_FAMILY_SH5) {
316 extern void __weak sh5_cache_init(void);
317
318 sh5_cache_init();
319 }
320
Paul Mundt27d59ec2009-08-15 11:11:16 +0900321 emit_cache_params();
Paul Mundtecba1062009-08-15 11:05:42 +0900322}