blob: 63c132998f24f81c5c2975ca87d26c815dad8dd5 [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);
Paul Mundt0a993b02009-10-27 10:51:35 +090030EXPORT_SYMBOL(__flush_wback_region);
Paul Mundt37443ef2009-08-15 12:29:49 +090031void (*__flush_purge_region)(void *start, int size);
Paul Mundt0a993b02009-10-27 10:51:35 +090032EXPORT_SYMBOL(__flush_purge_region);
Paul Mundt37443ef2009-08-15 12:29:49 +090033void (*__flush_invalidate_region)(void *start, int size);
Paul Mundt0a993b02009-10-27 10:51:35 +090034EXPORT_SYMBOL(__flush_invalidate_region);
Paul Mundt37443ef2009-08-15 12:29:49 +090035
Paul Mundt37443ef2009-08-15 12:29:49 +090036static inline void noop__flush_region(void *start, int size)
37{
38}
39
Paul Mundt6f379572009-09-01 21:21:36 +090040static inline void cacheop_on_each_cpu(void (*func) (void *info), void *info,
41 int wait)
42{
43 preempt_disable();
44 smp_call_function(func, info, wait);
45 func(info);
46 preempt_enable();
47}
48
Paul Mundtba1789e2007-11-05 16:18:16 +090049void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
50 unsigned long vaddr, void *dst, const void *src,
51 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Paul Mundt0dfae7d2009-07-27 21:30:17 +090053 if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
54 !test_bit(PG_dcache_dirty, &page->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090055 void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
56 memcpy(vto, src, len);
Paul Mundt0906a3a2009-09-03 17:21:10 +090057 kunmap_coherent(vto);
Paul Mundt2277ab42009-07-22 19:20:49 +090058 } else {
59 memcpy(dst, src, len);
Paul Mundt0dfae7d2009-07-27 21:30:17 +090060 if (boot_cpu_data.dcache.n_aliases)
61 set_bit(PG_dcache_dirty, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +090062 }
Paul Mundtba1789e2007-11-05 16:18:16 +090063
64 if (vma->vm_flags & VM_EXEC)
65 flush_cache_page(vma, vaddr, page_to_pfn(page));
66}
67
68void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
69 unsigned long vaddr, void *dst, const void *src,
70 unsigned long len)
71{
Paul Mundt0dfae7d2009-07-27 21:30:17 +090072 if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
73 !test_bit(PG_dcache_dirty, &page->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090074 void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
75 memcpy(dst, vfrom, len);
Paul Mundt0906a3a2009-09-03 17:21:10 +090076 kunmap_coherent(vfrom);
Paul Mundt2277ab42009-07-22 19:20:49 +090077 } else {
78 memcpy(dst, src, len);
Paul Mundt0dfae7d2009-07-27 21:30:17 +090079 if (boot_cpu_data.dcache.n_aliases)
80 set_bit(PG_dcache_dirty, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +090081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
Paul Mundt39e688a2007-03-05 19:46:47 +090083
Paul Mundt7747b9a2007-11-05 16:12:32 +090084void copy_user_highpage(struct page *to, struct page *from,
85 unsigned long vaddr, struct vm_area_struct *vma)
86{
87 void *vfrom, *vto;
88
Paul Mundt7747b9a2007-11-05 16:12:32 +090089 vto = kmap_atomic(to, KM_USER1);
Paul Mundt7747b9a2007-11-05 16:12:32 +090090
Paul Mundt0dfae7d2009-07-27 21:30:17 +090091 if (boot_cpu_data.dcache.n_aliases && page_mapped(from) &&
92 !test_bit(PG_dcache_dirty, &from->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090093 vfrom = kmap_coherent(from, vaddr);
94 copy_page(vto, vfrom);
Paul Mundt0906a3a2009-09-03 17:21:10 +090095 kunmap_coherent(vfrom);
Paul Mundt2277ab42009-07-22 19:20:49 +090096 } else {
97 vfrom = kmap_atomic(from, KM_USER0);
98 copy_page(vto, vfrom);
99 kunmap_atomic(vfrom, KM_USER0);
100 }
101
102 if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK))
Paul Mundt6e4154d2009-09-08 16:21:00 +0900103 __flush_purge_region(vto, PAGE_SIZE);
Paul Mundt7747b9a2007-11-05 16:12:32 +0900104
105 kunmap_atomic(vto, KM_USER1);
106 /* Make sure this page is cleared on other CPU's too before using it */
107 smp_wmb();
108}
109EXPORT_SYMBOL(copy_user_highpage);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900110
111void clear_user_highpage(struct page *page, unsigned long vaddr)
112{
113 void *kaddr = kmap_atomic(page, KM_USER0);
114
115 clear_page(kaddr);
116
117 if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK))
Paul Mundt6e4154d2009-09-08 16:21:00 +0900118 __flush_purge_region(kaddr, PAGE_SIZE);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900119
120 kunmap_atomic(kaddr, KM_USER0);
121}
122EXPORT_SYMBOL(clear_user_highpage);
Paul Mundt9cef7492009-07-29 00:12:17 +0900123
124void __update_cache(struct vm_area_struct *vma,
125 unsigned long address, pte_t pte)
126{
127 struct page *page;
128 unsigned long pfn = pte_pfn(pte);
129
130 if (!boot_cpu_data.dcache.n_aliases)
131 return;
132
133 page = pfn_to_page(pfn);
Paul Mundt964f7e52009-10-13 11:18:34 +0900134 if (pfn_valid(pfn)) {
Paul Mundt9cef7492009-07-29 00:12:17 +0900135 int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags);
136 if (dirty) {
137 unsigned long addr = (unsigned long)page_address(page);
138
139 if (pages_do_alias(addr, address & PAGE_MASK))
Paul Mundt6e4154d2009-09-08 16:21:00 +0900140 __flush_purge_region((void *)addr, PAGE_SIZE);
Paul Mundt9cef7492009-07-29 00:12:17 +0900141 }
142 }
143}
Paul Mundtc0fe4782009-08-04 16:02:43 +0900144
145void __flush_anon_page(struct page *page, unsigned long vmaddr)
146{
147 unsigned long addr = (unsigned long) page_address(page);
148
149 if (pages_do_alias(addr, vmaddr)) {
150 if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
151 !test_bit(PG_dcache_dirty, &page->flags)) {
152 void *kaddr;
153
154 kaddr = kmap_coherent(page, vmaddr);
Paul Mundt6e4154d2009-09-08 16:21:00 +0900155 /* XXX.. For now kunmap_coherent() does a purge */
156 /* __flush_purge_region((void *)kaddr, PAGE_SIZE); */
Paul Mundt0906a3a2009-09-03 17:21:10 +0900157 kunmap_coherent(kaddr);
Paul Mundtc0fe4782009-08-04 16:02:43 +0900158 } else
Paul Mundt6e4154d2009-09-08 16:21:00 +0900159 __flush_purge_region((void *)addr, PAGE_SIZE);
Paul Mundtc0fe4782009-08-04 16:02:43 +0900160 }
161}
Paul Mundtecba1062009-08-15 11:05:42 +0900162
Paul Mundtf26b2a52009-08-21 17:23:14 +0900163void flush_cache_all(void)
164{
Paul Mundt6f379572009-09-01 21:21:36 +0900165 cacheop_on_each_cpu(local_flush_cache_all, NULL, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900166}
Paul Mundt0a993b02009-10-27 10:51:35 +0900167EXPORT_SYMBOL(flush_cache_all);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900168
169void flush_cache_mm(struct mm_struct *mm)
170{
Paul Mundt654d3642009-09-09 14:04:06 +0900171 if (boot_cpu_data.dcache.n_aliases == 0)
172 return;
173
Paul Mundt6f379572009-09-01 21:21:36 +0900174 cacheop_on_each_cpu(local_flush_cache_mm, mm, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900175}
176
177void flush_cache_dup_mm(struct mm_struct *mm)
178{
Paul Mundt654d3642009-09-09 14:04:06 +0900179 if (boot_cpu_data.dcache.n_aliases == 0)
180 return;
181
Paul Mundt6f379572009-09-01 21:21:36 +0900182 cacheop_on_each_cpu(local_flush_cache_dup_mm, mm, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900183}
184
185void flush_cache_page(struct vm_area_struct *vma, unsigned long addr,
186 unsigned long pfn)
187{
188 struct flusher_data data;
189
190 data.vma = vma;
191 data.addr1 = addr;
192 data.addr2 = pfn;
193
Paul Mundt6f379572009-09-01 21:21:36 +0900194 cacheop_on_each_cpu(local_flush_cache_page, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900195}
196
197void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
198 unsigned long end)
199{
200 struct flusher_data data;
201
202 data.vma = vma;
203 data.addr1 = start;
204 data.addr2 = end;
205
Paul Mundt6f379572009-09-01 21:21:36 +0900206 cacheop_on_each_cpu(local_flush_cache_range, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900207}
Paul Mundt0a993b02009-10-27 10:51:35 +0900208EXPORT_SYMBOL(flush_cache_range);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900209
210void flush_dcache_page(struct page *page)
211{
Paul Mundt6f379572009-09-01 21:21:36 +0900212 cacheop_on_each_cpu(local_flush_dcache_page, page, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900213}
Paul Mundt0a993b02009-10-27 10:51:35 +0900214EXPORT_SYMBOL(flush_dcache_page);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900215
216void flush_icache_range(unsigned long start, unsigned long end)
217{
218 struct flusher_data data;
219
220 data.vma = NULL;
221 data.addr1 = start;
222 data.addr2 = end;
223
Paul Mundt6f379572009-09-01 21:21:36 +0900224 cacheop_on_each_cpu(local_flush_icache_range, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900225}
226
227void flush_icache_page(struct vm_area_struct *vma, struct page *page)
228{
229 /* Nothing uses the VMA, so just pass the struct page along */
Paul Mundt6f379572009-09-01 21:21:36 +0900230 cacheop_on_each_cpu(local_flush_icache_page, page, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900231}
232
233void flush_cache_sigtramp(unsigned long address)
234{
Paul Mundt6f379572009-09-01 21:21:36 +0900235 cacheop_on_each_cpu(local_flush_cache_sigtramp, (void *)address, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900236}
237
Paul Mundt27d59ec2009-08-15 11:11:16 +0900238static void compute_alias(struct cache_info *c)
239{
240 c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1);
241 c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0;
242}
243
244static void __init emit_cache_params(void)
245{
246 printk(KERN_NOTICE "I-cache : n_ways=%d n_sets=%d way_incr=%d\n",
247 boot_cpu_data.icache.ways,
248 boot_cpu_data.icache.sets,
249 boot_cpu_data.icache.way_incr);
250 printk(KERN_NOTICE "I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
251 boot_cpu_data.icache.entry_mask,
252 boot_cpu_data.icache.alias_mask,
253 boot_cpu_data.icache.n_aliases);
254 printk(KERN_NOTICE "D-cache : n_ways=%d n_sets=%d way_incr=%d\n",
255 boot_cpu_data.dcache.ways,
256 boot_cpu_data.dcache.sets,
257 boot_cpu_data.dcache.way_incr);
258 printk(KERN_NOTICE "D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
259 boot_cpu_data.dcache.entry_mask,
260 boot_cpu_data.dcache.alias_mask,
261 boot_cpu_data.dcache.n_aliases);
262
263 /*
264 * Emit Secondary Cache parameters if the CPU has a probed L2.
265 */
266 if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) {
267 printk(KERN_NOTICE "S-cache : n_ways=%d n_sets=%d way_incr=%d\n",
268 boot_cpu_data.scache.ways,
269 boot_cpu_data.scache.sets,
270 boot_cpu_data.scache.way_incr);
271 printk(KERN_NOTICE "S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
272 boot_cpu_data.scache.entry_mask,
273 boot_cpu_data.scache.alias_mask,
274 boot_cpu_data.scache.n_aliases);
275 }
276}
277
Paul Mundtecba1062009-08-15 11:05:42 +0900278void __init cpu_cache_init(void)
279{
Magnus Damm5fb80ae2009-10-16 14:38:48 +0900280 unsigned int cache_disabled = !(__raw_readl(CCR) & CCR_CACHE_ENABLE);
281
Paul Mundt27d59ec2009-08-15 11:11:16 +0900282 compute_alias(&boot_cpu_data.icache);
283 compute_alias(&boot_cpu_data.dcache);
284 compute_alias(&boot_cpu_data.scache);
285
Paul Mundt37443ef2009-08-15 12:29:49 +0900286 __flush_wback_region = noop__flush_region;
287 __flush_purge_region = noop__flush_region;
288 __flush_invalidate_region = noop__flush_region;
289
Magnus Damm5fb80ae2009-10-16 14:38:48 +0900290 /*
291 * No flushing is necessary in the disabled cache case so we can
292 * just keep the noop functions in local_flush_..() and __flush_..()
293 */
294 if (unlikely(cache_disabled))
295 goto skip;
296
Paul Mundt109b44a2009-08-15 12:35:15 +0900297 if (boot_cpu_data.family == CPU_FAMILY_SH2) {
298 extern void __weak sh2_cache_init(void);
299
300 sh2_cache_init();
301 }
302
Paul Mundta58e1a22009-08-15 12:38:29 +0900303 if (boot_cpu_data.family == CPU_FAMILY_SH2A) {
304 extern void __weak sh2a_cache_init(void);
305
306 sh2a_cache_init();
307 }
308
Paul Mundt79f1c9d2009-08-15 12:42:55 +0900309 if (boot_cpu_data.family == CPU_FAMILY_SH3) {
310 extern void __weak sh3_cache_init(void);
311
312 sh3_cache_init();
Paul Mundt0d051d92009-08-15 12:53:39 +0900313
314 if ((boot_cpu_data.type == CPU_SH7705) &&
315 (boot_cpu_data.dcache.sets == 512)) {
316 extern void __weak sh7705_cache_init(void);
317
318 sh7705_cache_init();
319 }
Paul Mundt79f1c9d2009-08-15 12:42:55 +0900320 }
321
Paul Mundtecba1062009-08-15 11:05:42 +0900322 if ((boot_cpu_data.family == CPU_FAMILY_SH4) ||
323 (boot_cpu_data.family == CPU_FAMILY_SH4A) ||
324 (boot_cpu_data.family == CPU_FAMILY_SH4AL_DSP)) {
325 extern void __weak sh4_cache_init(void);
326
327 sh4_cache_init();
328 }
Paul Mundt27d59ec2009-08-15 11:11:16 +0900329
Paul Mundt2b431512009-08-16 02:16:44 +0900330 if (boot_cpu_data.family == CPU_FAMILY_SH5) {
331 extern void __weak sh5_cache_init(void);
332
333 sh5_cache_init();
334 }
335
Magnus Damm5fb80ae2009-10-16 14:38:48 +0900336skip:
Paul Mundt27d59ec2009-08-15 11:11:16 +0900337 emit_cache_params();
Paul Mundtecba1062009-08-15 11:05:42 +0900338}