blob: 411fe60584295070926c745be9d2033da1582161 [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 Mundtba1789e2007-11-05 16:18:16 +090037void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
38 unsigned long vaddr, void *dst, const void *src,
39 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Paul Mundt0dfae7d2009-07-27 21:30:17 +090041 if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
42 !test_bit(PG_dcache_dirty, &page->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090043 void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
44 memcpy(vto, src, len);
Paul Mundtb5eb10a2009-08-04 16:00:36 +090045 kunmap_coherent();
Paul Mundt2277ab42009-07-22 19:20:49 +090046 } else {
47 memcpy(dst, src, len);
Paul Mundt0dfae7d2009-07-27 21:30:17 +090048 if (boot_cpu_data.dcache.n_aliases)
49 set_bit(PG_dcache_dirty, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +090050 }
Paul Mundtba1789e2007-11-05 16:18:16 +090051
52 if (vma->vm_flags & VM_EXEC)
53 flush_cache_page(vma, vaddr, page_to_pfn(page));
54}
55
56void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
57 unsigned long vaddr, void *dst, const void *src,
58 unsigned long len)
59{
Paul Mundt0dfae7d2009-07-27 21:30:17 +090060 if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
61 !test_bit(PG_dcache_dirty, &page->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090062 void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
63 memcpy(dst, vfrom, len);
Paul Mundtb5eb10a2009-08-04 16:00:36 +090064 kunmap_coherent();
Paul Mundt2277ab42009-07-22 19:20:49 +090065 } else {
66 memcpy(dst, src, len);
Paul Mundt0dfae7d2009-07-27 21:30:17 +090067 if (boot_cpu_data.dcache.n_aliases)
68 set_bit(PG_dcache_dirty, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +090069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
Paul Mundt39e688a2007-03-05 19:46:47 +090071
Paul Mundt7747b9a2007-11-05 16:12:32 +090072void copy_user_highpage(struct page *to, struct page *from,
73 unsigned long vaddr, struct vm_area_struct *vma)
74{
75 void *vfrom, *vto;
76
Paul Mundt7747b9a2007-11-05 16:12:32 +090077 vto = kmap_atomic(to, KM_USER1);
Paul Mundt7747b9a2007-11-05 16:12:32 +090078
Paul Mundt0dfae7d2009-07-27 21:30:17 +090079 if (boot_cpu_data.dcache.n_aliases && page_mapped(from) &&
80 !test_bit(PG_dcache_dirty, &from->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090081 vfrom = kmap_coherent(from, vaddr);
82 copy_page(vto, vfrom);
Paul Mundtb5eb10a2009-08-04 16:00:36 +090083 kunmap_coherent();
Paul Mundt2277ab42009-07-22 19:20:49 +090084 } else {
85 vfrom = kmap_atomic(from, KM_USER0);
86 copy_page(vto, vfrom);
87 kunmap_atomic(vfrom, KM_USER0);
88 }
89
90 if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK))
Paul Mundt7747b9a2007-11-05 16:12:32 +090091 __flush_wback_region(vto, PAGE_SIZE);
92
93 kunmap_atomic(vto, KM_USER1);
94 /* Make sure this page is cleared on other CPU's too before using it */
95 smp_wmb();
96}
97EXPORT_SYMBOL(copy_user_highpage);
Paul Mundtdfff0fa2009-07-27 20:53:22 +090098
99void clear_user_highpage(struct page *page, unsigned long vaddr)
100{
101 void *kaddr = kmap_atomic(page, KM_USER0);
102
103 clear_page(kaddr);
104
105 if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK))
106 __flush_wback_region(kaddr, PAGE_SIZE);
107
108 kunmap_atomic(kaddr, KM_USER0);
109}
110EXPORT_SYMBOL(clear_user_highpage);
Paul Mundt9cef7492009-07-29 00:12:17 +0900111
112void __update_cache(struct vm_area_struct *vma,
113 unsigned long address, pte_t pte)
114{
115 struct page *page;
116 unsigned long pfn = pte_pfn(pte);
117
118 if (!boot_cpu_data.dcache.n_aliases)
119 return;
120
121 page = pfn_to_page(pfn);
122 if (pfn_valid(pfn) && page_mapping(page)) {
123 int dirty = test_and_clear_bit(PG_dcache_dirty, &page->flags);
124 if (dirty) {
125 unsigned long addr = (unsigned long)page_address(page);
126
127 if (pages_do_alias(addr, address & PAGE_MASK))
128 __flush_wback_region((void *)addr, PAGE_SIZE);
129 }
130 }
131}
Paul Mundtc0fe4782009-08-04 16:02:43 +0900132
133void __flush_anon_page(struct page *page, unsigned long vmaddr)
134{
135 unsigned long addr = (unsigned long) page_address(page);
136
137 if (pages_do_alias(addr, vmaddr)) {
138 if (boot_cpu_data.dcache.n_aliases && page_mapped(page) &&
139 !test_bit(PG_dcache_dirty, &page->flags)) {
140 void *kaddr;
141
142 kaddr = kmap_coherent(page, vmaddr);
143 __flush_wback_region((void *)kaddr, PAGE_SIZE);
144 kunmap_coherent();
145 } else
146 __flush_wback_region((void *)addr, PAGE_SIZE);
147 }
148}
Paul Mundtecba1062009-08-15 11:05:42 +0900149
Paul Mundtf26b2a52009-08-21 17:23:14 +0900150void flush_cache_all(void)
151{
152 on_each_cpu(local_flush_cache_all, NULL, 1);
153}
154
155void flush_cache_mm(struct mm_struct *mm)
156{
157 on_each_cpu(local_flush_cache_mm, mm, 1);
158}
159
160void flush_cache_dup_mm(struct mm_struct *mm)
161{
162 on_each_cpu(local_flush_cache_dup_mm, mm, 1);
163}
164
165void flush_cache_page(struct vm_area_struct *vma, unsigned long addr,
166 unsigned long pfn)
167{
168 struct flusher_data data;
169
170 data.vma = vma;
171 data.addr1 = addr;
172 data.addr2 = pfn;
173
174 on_each_cpu(local_flush_cache_page, (void *)&data, 1);
175}
176
177void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
178 unsigned long end)
179{
180 struct flusher_data data;
181
182 data.vma = vma;
183 data.addr1 = start;
184 data.addr2 = end;
185
186 on_each_cpu(local_flush_cache_range, (void *)&data, 1);
187}
188
189void flush_dcache_page(struct page *page)
190{
191 on_each_cpu(local_flush_dcache_page, page, 1);
192}
193
194void flush_icache_range(unsigned long start, unsigned long end)
195{
196 struct flusher_data data;
197
198 data.vma = NULL;
199 data.addr1 = start;
200 data.addr2 = end;
201
202 on_each_cpu(local_flush_icache_range, (void *)&data, 1);
203}
204
205void flush_icache_page(struct vm_area_struct *vma, struct page *page)
206{
207 /* Nothing uses the VMA, so just pass the struct page along */
208 on_each_cpu(local_flush_icache_page, page, 1);
209}
210
211void flush_cache_sigtramp(unsigned long address)
212{
213 on_each_cpu(local_flush_cache_sigtramp, (void *)address, 1);
214}
215
Paul Mundt27d59ec2009-08-15 11:11:16 +0900216static void compute_alias(struct cache_info *c)
217{
218 c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1);
219 c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0;
220}
221
222static void __init emit_cache_params(void)
223{
224 printk(KERN_NOTICE "I-cache : n_ways=%d n_sets=%d way_incr=%d\n",
225 boot_cpu_data.icache.ways,
226 boot_cpu_data.icache.sets,
227 boot_cpu_data.icache.way_incr);
228 printk(KERN_NOTICE "I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
229 boot_cpu_data.icache.entry_mask,
230 boot_cpu_data.icache.alias_mask,
231 boot_cpu_data.icache.n_aliases);
232 printk(KERN_NOTICE "D-cache : n_ways=%d n_sets=%d way_incr=%d\n",
233 boot_cpu_data.dcache.ways,
234 boot_cpu_data.dcache.sets,
235 boot_cpu_data.dcache.way_incr);
236 printk(KERN_NOTICE "D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
237 boot_cpu_data.dcache.entry_mask,
238 boot_cpu_data.dcache.alias_mask,
239 boot_cpu_data.dcache.n_aliases);
240
241 /*
242 * Emit Secondary Cache parameters if the CPU has a probed L2.
243 */
244 if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) {
245 printk(KERN_NOTICE "S-cache : n_ways=%d n_sets=%d way_incr=%d\n",
246 boot_cpu_data.scache.ways,
247 boot_cpu_data.scache.sets,
248 boot_cpu_data.scache.way_incr);
249 printk(KERN_NOTICE "S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
250 boot_cpu_data.scache.entry_mask,
251 boot_cpu_data.scache.alias_mask,
252 boot_cpu_data.scache.n_aliases);
253 }
254}
255
Paul Mundtecba1062009-08-15 11:05:42 +0900256void __init cpu_cache_init(void)
257{
Paul Mundt27d59ec2009-08-15 11:11:16 +0900258 compute_alias(&boot_cpu_data.icache);
259 compute_alias(&boot_cpu_data.dcache);
260 compute_alias(&boot_cpu_data.scache);
261
Paul Mundt37443ef2009-08-15 12:29:49 +0900262 __flush_wback_region = noop__flush_region;
263 __flush_purge_region = noop__flush_region;
264 __flush_invalidate_region = noop__flush_region;
265
Paul Mundt109b44a2009-08-15 12:35:15 +0900266 if (boot_cpu_data.family == CPU_FAMILY_SH2) {
267 extern void __weak sh2_cache_init(void);
268
269 sh2_cache_init();
270 }
271
Paul Mundta58e1a22009-08-15 12:38:29 +0900272 if (boot_cpu_data.family == CPU_FAMILY_SH2A) {
273 extern void __weak sh2a_cache_init(void);
274
275 sh2a_cache_init();
276 }
277
Paul Mundt79f1c9d2009-08-15 12:42:55 +0900278 if (boot_cpu_data.family == CPU_FAMILY_SH3) {
279 extern void __weak sh3_cache_init(void);
280
281 sh3_cache_init();
Paul Mundt0d051d92009-08-15 12:53:39 +0900282
283 if ((boot_cpu_data.type == CPU_SH7705) &&
284 (boot_cpu_data.dcache.sets == 512)) {
285 extern void __weak sh7705_cache_init(void);
286
287 sh7705_cache_init();
288 }
Paul Mundt79f1c9d2009-08-15 12:42:55 +0900289 }
290
Paul Mundtecba1062009-08-15 11:05:42 +0900291 if ((boot_cpu_data.family == CPU_FAMILY_SH4) ||
292 (boot_cpu_data.family == CPU_FAMILY_SH4A) ||
293 (boot_cpu_data.family == CPU_FAMILY_SH4AL_DSP)) {
294 extern void __weak sh4_cache_init(void);
295
296 sh4_cache_init();
297 }
Paul Mundt27d59ec2009-08-15 11:11:16 +0900298
Paul Mundt2b431512009-08-16 02:16:44 +0900299 if (boot_cpu_data.family == CPU_FAMILY_SH5) {
300 extern void __weak sh5_cache_init(void);
301
302 sh5_cache_init();
303 }
304
Paul Mundt27d59ec2009-08-15 11:11:16 +0900305 emit_cache_params();
Paul Mundtecba1062009-08-15 11:05:42 +0900306}