blob: 36554a9ea99b2b0f863b10eecb9f2ccb3db79bb7 [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 Mundta6198a22010-01-15 14:21:37 +09005 * Copyright (C) 2002 - 2010 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();
Paul Mundta6198a22010-01-15 14:21:37 +090044
Rich Felkera1e262f2016-02-15 18:36:33 +000045 /* Needing IPI for cross-core flush is SHX3-specific. */
46#ifdef CONFIG_CPU_SHX3
Paul Mundta6198a22010-01-15 14:21:37 +090047 /*
48 * It's possible that this gets called early on when IRQs are
49 * still disabled due to ioremapping by the boot CPU, so don't
50 * even attempt IPIs unless there are other CPUs online.
51 */
52 if (num_online_cpus() > 1)
53 smp_call_function(func, info, wait);
Rich Felkera1e262f2016-02-15 18:36:33 +000054#endif
Paul Mundta6198a22010-01-15 14:21:37 +090055
Paul Mundt6f379572009-09-01 21:21:36 +090056 func(info);
Paul Mundta6198a22010-01-15 14:21:37 +090057
Paul Mundt6f379572009-09-01 21:21:36 +090058 preempt_enable();
59}
60
Paul Mundtba1789e2007-11-05 16:18:16 +090061void copy_to_user_page(struct vm_area_struct *vma, struct page *page,
62 unsigned long vaddr, void *dst, const void *src,
63 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070064{
Kirill A. Shutemove1534ae2016-01-15 16:53:46 -080065 if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
Paul Mundt55661fc2010-12-01 15:39:51 +090066 test_bit(PG_dcache_clean, &page->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090067 void *vto = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
68 memcpy(vto, src, len);
Paul Mundt0906a3a2009-09-03 17:21:10 +090069 kunmap_coherent(vto);
Paul Mundt2277ab42009-07-22 19:20:49 +090070 } else {
71 memcpy(dst, src, len);
Paul Mundt0dfae7d2009-07-27 21:30:17 +090072 if (boot_cpu_data.dcache.n_aliases)
Paul Mundt55661fc2010-12-01 15:39:51 +090073 clear_bit(PG_dcache_clean, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +090074 }
Paul Mundtba1789e2007-11-05 16:18:16 +090075
76 if (vma->vm_flags & VM_EXEC)
77 flush_cache_page(vma, vaddr, page_to_pfn(page));
78}
79
80void copy_from_user_page(struct vm_area_struct *vma, struct page *page,
81 unsigned long vaddr, void *dst, const void *src,
82 unsigned long len)
83{
Kirill A. Shutemove1534ae2016-01-15 16:53:46 -080084 if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
Paul Mundt55661fc2010-12-01 15:39:51 +090085 test_bit(PG_dcache_clean, &page->flags)) {
Paul Mundt2277ab42009-07-22 19:20:49 +090086 void *vfrom = kmap_coherent(page, vaddr) + (vaddr & ~PAGE_MASK);
87 memcpy(dst, vfrom, len);
Paul Mundt0906a3a2009-09-03 17:21:10 +090088 kunmap_coherent(vfrom);
Paul Mundt2277ab42009-07-22 19:20:49 +090089 } else {
90 memcpy(dst, src, len);
Paul Mundt0dfae7d2009-07-27 21:30:17 +090091 if (boot_cpu_data.dcache.n_aliases)
Paul Mundt55661fc2010-12-01 15:39:51 +090092 clear_bit(PG_dcache_clean, &page->flags);
Paul Mundt2277ab42009-07-22 19:20:49 +090093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Paul Mundt39e688a2007-03-05 19:46:47 +090095
Paul Mundt7747b9a2007-11-05 16:12:32 +090096void copy_user_highpage(struct page *to, struct page *from,
97 unsigned long vaddr, struct vm_area_struct *vma)
98{
99 void *vfrom, *vto;
100
Cong Wangbc3e11b2011-11-25 23:14:16 +0800101 vto = kmap_atomic(to);
Paul Mundt7747b9a2007-11-05 16:12:32 +0900102
Kirill A. Shutemove1534ae2016-01-15 16:53:46 -0800103 if (boot_cpu_data.dcache.n_aliases && page_mapcount(from) &&
Paul Mundt55661fc2010-12-01 15:39:51 +0900104 test_bit(PG_dcache_clean, &from->flags)) {
Paul Mundt7e01c942009-12-04 15:14:52 +0900105 vfrom = kmap_coherent(from, vaddr);
Paul Mundt2277ab42009-07-22 19:20:49 +0900106 copy_page(vto, vfrom);
Paul Mundt7e01c942009-12-04 15:14:52 +0900107 kunmap_coherent(vfrom);
108 } else {
Cong Wangbc3e11b2011-11-25 23:14:16 +0800109 vfrom = kmap_atomic(from);
Paul Mundt7e01c942009-12-04 15:14:52 +0900110 copy_page(vto, vfrom);
Cong Wangbc3e11b2011-11-25 23:14:16 +0800111 kunmap_atomic(vfrom);
Paul Mundt7e01c942009-12-04 15:14:52 +0900112 }
Paul Mundt2277ab42009-07-22 19:20:49 +0900113
Stuart Menefya25bbe12011-01-31 17:50:29 +0000114 if (pages_do_alias((unsigned long)vto, vaddr & PAGE_MASK) ||
115 (vma->vm_flags & VM_EXEC))
Paul Mundt7e01c942009-12-04 15:14:52 +0900116 __flush_purge_region(vto, PAGE_SIZE);
117
Cong Wangbc3e11b2011-11-25 23:14:16 +0800118 kunmap_atomic(vto);
Paul Mundt7747b9a2007-11-05 16:12:32 +0900119 /* Make sure this page is cleared on other CPU's too before using it */
120 smp_wmb();
121}
122EXPORT_SYMBOL(copy_user_highpage);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900123
124void clear_user_highpage(struct page *page, unsigned long vaddr)
125{
Cong Wangbc3e11b2011-11-25 23:14:16 +0800126 void *kaddr = kmap_atomic(page);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900127
Paul Mundt7e01c942009-12-04 15:14:52 +0900128 clear_page(kaddr);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900129
Paul Mundt7e01c942009-12-04 15:14:52 +0900130 if (pages_do_alias((unsigned long)kaddr, vaddr & PAGE_MASK))
131 __flush_purge_region(kaddr, PAGE_SIZE);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900132
Cong Wangbc3e11b2011-11-25 23:14:16 +0800133 kunmap_atomic(kaddr);
Paul Mundtdfff0fa2009-07-27 20:53:22 +0900134}
135EXPORT_SYMBOL(clear_user_highpage);
Paul Mundt9cef7492009-07-29 00:12:17 +0900136
137void __update_cache(struct vm_area_struct *vma,
138 unsigned long address, pte_t pte)
139{
140 struct page *page;
141 unsigned long pfn = pte_pfn(pte);
142
143 if (!boot_cpu_data.dcache.n_aliases)
144 return;
145
146 page = pfn_to_page(pfn);
Paul Mundt964f7e52009-10-13 11:18:34 +0900147 if (pfn_valid(pfn)) {
Paul Mundt55661fc2010-12-01 15:39:51 +0900148 int dirty = !test_and_set_bit(PG_dcache_clean, &page->flags);
Markus Pietrek76382b52009-12-24 15:12:02 +0900149 if (dirty)
150 __flush_purge_region(page_address(page), PAGE_SIZE);
Paul Mundt9cef7492009-07-29 00:12:17 +0900151 }
152}
Paul Mundtc0fe4782009-08-04 16:02:43 +0900153
154void __flush_anon_page(struct page *page, unsigned long vmaddr)
155{
156 unsigned long addr = (unsigned long) page_address(page);
157
158 if (pages_do_alias(addr, vmaddr)) {
Kirill A. Shutemove1534ae2016-01-15 16:53:46 -0800159 if (boot_cpu_data.dcache.n_aliases && page_mapcount(page) &&
Paul Mundt55661fc2010-12-01 15:39:51 +0900160 test_bit(PG_dcache_clean, &page->flags)) {
Paul Mundtc0fe4782009-08-04 16:02:43 +0900161 void *kaddr;
162
163 kaddr = kmap_coherent(page, vmaddr);
Paul Mundt6e4154d2009-09-08 16:21:00 +0900164 /* XXX.. For now kunmap_coherent() does a purge */
165 /* __flush_purge_region((void *)kaddr, PAGE_SIZE); */
Paul Mundt0906a3a2009-09-03 17:21:10 +0900166 kunmap_coherent(kaddr);
Paul Mundtc0fe4782009-08-04 16:02:43 +0900167 } else
Paul Mundt6e4154d2009-09-08 16:21:00 +0900168 __flush_purge_region((void *)addr, PAGE_SIZE);
Paul Mundtc0fe4782009-08-04 16:02:43 +0900169 }
170}
Paul Mundtecba1062009-08-15 11:05:42 +0900171
Paul Mundtf26b2a52009-08-21 17:23:14 +0900172void flush_cache_all(void)
173{
Paul Mundt6f379572009-09-01 21:21:36 +0900174 cacheop_on_each_cpu(local_flush_cache_all, NULL, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900175}
Paul Mundt0a993b02009-10-27 10:51:35 +0900176EXPORT_SYMBOL(flush_cache_all);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900177
178void flush_cache_mm(struct mm_struct *mm)
179{
Paul Mundt654d3642009-09-09 14:04:06 +0900180 if (boot_cpu_data.dcache.n_aliases == 0)
181 return;
182
Paul Mundt6f379572009-09-01 21:21:36 +0900183 cacheop_on_each_cpu(local_flush_cache_mm, mm, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900184}
185
186void flush_cache_dup_mm(struct mm_struct *mm)
187{
Paul Mundt654d3642009-09-09 14:04:06 +0900188 if (boot_cpu_data.dcache.n_aliases == 0)
189 return;
190
Paul Mundt6f379572009-09-01 21:21:36 +0900191 cacheop_on_each_cpu(local_flush_cache_dup_mm, mm, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900192}
193
194void flush_cache_page(struct vm_area_struct *vma, unsigned long addr,
195 unsigned long pfn)
196{
197 struct flusher_data data;
198
199 data.vma = vma;
200 data.addr1 = addr;
201 data.addr2 = pfn;
202
Paul Mundt6f379572009-09-01 21:21:36 +0900203 cacheop_on_each_cpu(local_flush_cache_page, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900204}
205
206void flush_cache_range(struct vm_area_struct *vma, unsigned long start,
207 unsigned long end)
208{
209 struct flusher_data data;
210
211 data.vma = vma;
212 data.addr1 = start;
213 data.addr2 = end;
214
Paul Mundt6f379572009-09-01 21:21:36 +0900215 cacheop_on_each_cpu(local_flush_cache_range, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900216}
Paul Mundt0a993b02009-10-27 10:51:35 +0900217EXPORT_SYMBOL(flush_cache_range);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900218
219void flush_dcache_page(struct page *page)
220{
Paul Mundt6f379572009-09-01 21:21:36 +0900221 cacheop_on_each_cpu(local_flush_dcache_page, page, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900222}
Paul Mundt0a993b02009-10-27 10:51:35 +0900223EXPORT_SYMBOL(flush_dcache_page);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900224
225void flush_icache_range(unsigned long start, unsigned long end)
226{
227 struct flusher_data data;
228
229 data.vma = NULL;
230 data.addr1 = start;
231 data.addr2 = end;
232
Paul Mundt6f379572009-09-01 21:21:36 +0900233 cacheop_on_each_cpu(local_flush_icache_range, (void *)&data, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900234}
Pranith Kumare3560302014-08-29 15:19:09 -0700235EXPORT_SYMBOL(flush_icache_range);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900236
237void flush_icache_page(struct vm_area_struct *vma, struct page *page)
238{
239 /* Nothing uses the VMA, so just pass the struct page along */
Paul Mundt6f379572009-09-01 21:21:36 +0900240 cacheop_on_each_cpu(local_flush_icache_page, page, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900241}
242
243void flush_cache_sigtramp(unsigned long address)
244{
Paul Mundt6f379572009-09-01 21:21:36 +0900245 cacheop_on_each_cpu(local_flush_cache_sigtramp, (void *)address, 1);
Paul Mundtf26b2a52009-08-21 17:23:14 +0900246}
247
Paul Mundt27d59ec2009-08-15 11:11:16 +0900248static void compute_alias(struct cache_info *c)
249{
Rich Felker57155c62016-03-22 22:02:23 +0000250#ifdef CONFIG_MMU
Paul Mundt27d59ec2009-08-15 11:11:16 +0900251 c->alias_mask = ((c->sets - 1) << c->entry_shift) & ~(PAGE_SIZE - 1);
Rich Felker57155c62016-03-22 22:02:23 +0000252#else
253 c->alias_mask = 0;
254#endif
Paul Mundt27d59ec2009-08-15 11:11:16 +0900255 c->n_aliases = c->alias_mask ? (c->alias_mask >> PAGE_SHIFT) + 1 : 0;
256}
257
258static void __init emit_cache_params(void)
259{
260 printk(KERN_NOTICE "I-cache : n_ways=%d n_sets=%d way_incr=%d\n",
261 boot_cpu_data.icache.ways,
262 boot_cpu_data.icache.sets,
263 boot_cpu_data.icache.way_incr);
264 printk(KERN_NOTICE "I-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
265 boot_cpu_data.icache.entry_mask,
266 boot_cpu_data.icache.alias_mask,
267 boot_cpu_data.icache.n_aliases);
268 printk(KERN_NOTICE "D-cache : n_ways=%d n_sets=%d way_incr=%d\n",
269 boot_cpu_data.dcache.ways,
270 boot_cpu_data.dcache.sets,
271 boot_cpu_data.dcache.way_incr);
272 printk(KERN_NOTICE "D-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
273 boot_cpu_data.dcache.entry_mask,
274 boot_cpu_data.dcache.alias_mask,
275 boot_cpu_data.dcache.n_aliases);
276
277 /*
278 * Emit Secondary Cache parameters if the CPU has a probed L2.
279 */
280 if (boot_cpu_data.flags & CPU_HAS_L2_CACHE) {
281 printk(KERN_NOTICE "S-cache : n_ways=%d n_sets=%d way_incr=%d\n",
282 boot_cpu_data.scache.ways,
283 boot_cpu_data.scache.sets,
284 boot_cpu_data.scache.way_incr);
285 printk(KERN_NOTICE "S-cache : entry_mask=0x%08x alias_mask=0x%08x n_aliases=%d\n",
286 boot_cpu_data.scache.entry_mask,
287 boot_cpu_data.scache.alias_mask,
288 boot_cpu_data.scache.n_aliases);
289 }
290}
291
Paul Mundtecba1062009-08-15 11:05:42 +0900292void __init cpu_cache_init(void)
293{
Paul Mundt3af539e2009-11-12 17:03:28 +0900294 unsigned int cache_disabled = 0;
295
Geert Uytterhoevena5f6ea22014-03-03 15:38:33 -0800296#ifdef SH_CCR
297 cache_disabled = !(__raw_readl(SH_CCR) & CCR_CACHE_ENABLE);
Paul Mundt3af539e2009-11-12 17:03:28 +0900298#endif
Magnus Damm5fb80ae2009-10-16 14:38:48 +0900299
Paul Mundt27d59ec2009-08-15 11:11:16 +0900300 compute_alias(&boot_cpu_data.icache);
301 compute_alias(&boot_cpu_data.dcache);
302 compute_alias(&boot_cpu_data.scache);
303
Paul Mundt37443ef2009-08-15 12:29:49 +0900304 __flush_wback_region = noop__flush_region;
305 __flush_purge_region = noop__flush_region;
306 __flush_invalidate_region = noop__flush_region;
307
Magnus Damm5fb80ae2009-10-16 14:38:48 +0900308 /*
309 * No flushing is necessary in the disabled cache case so we can
310 * just keep the noop functions in local_flush_..() and __flush_..()
311 */
312 if (unlikely(cache_disabled))
313 goto skip;
314
Rich Felker5a846ab2016-03-17 23:09:37 +0000315 if (boot_cpu_data.type == CPU_J2) {
316 extern void __weak j2_cache_init(void);
317
318 j2_cache_init();
319 } else if (boot_cpu_data.family == CPU_FAMILY_SH2) {
Paul Mundt109b44a2009-08-15 12:35:15 +0900320 extern void __weak sh2_cache_init(void);
321
322 sh2_cache_init();
323 }
324
Paul Mundta58e1a22009-08-15 12:38:29 +0900325 if (boot_cpu_data.family == CPU_FAMILY_SH2A) {
326 extern void __weak sh2a_cache_init(void);
327
328 sh2a_cache_init();
329 }
330
Paul Mundt79f1c9d2009-08-15 12:42:55 +0900331 if (boot_cpu_data.family == CPU_FAMILY_SH3) {
332 extern void __weak sh3_cache_init(void);
333
334 sh3_cache_init();
Paul Mundt0d051d92009-08-15 12:53:39 +0900335
336 if ((boot_cpu_data.type == CPU_SH7705) &&
337 (boot_cpu_data.dcache.sets == 512)) {
338 extern void __weak sh7705_cache_init(void);
339
340 sh7705_cache_init();
341 }
Paul Mundt79f1c9d2009-08-15 12:42:55 +0900342 }
343
Paul Mundtecba1062009-08-15 11:05:42 +0900344 if ((boot_cpu_data.family == CPU_FAMILY_SH4) ||
345 (boot_cpu_data.family == CPU_FAMILY_SH4A) ||
346 (boot_cpu_data.family == CPU_FAMILY_SH4AL_DSP)) {
347 extern void __weak sh4_cache_init(void);
348
349 sh4_cache_init();
Paul Mundt3cf6fa12010-04-19 17:27:17 +0900350
351 if ((boot_cpu_data.type == CPU_SH7786) ||
352 (boot_cpu_data.type == CPU_SHX3)) {
353 extern void __weak shx3_cache_init(void);
354
355 shx3_cache_init();
356 }
Paul Mundtecba1062009-08-15 11:05:42 +0900357 }
Paul Mundt27d59ec2009-08-15 11:11:16 +0900358
Paul Mundt2b431512009-08-16 02:16:44 +0900359 if (boot_cpu_data.family == CPU_FAMILY_SH5) {
360 extern void __weak sh5_cache_init(void);
361
362 sh5_cache_init();
363 }
364
Magnus Damm5fb80ae2009-10-16 14:38:48 +0900365skip:
Paul Mundt27d59ec2009-08-15 11:11:16 +0900366 emit_cache_params();
Paul Mundtecba1062009-08-15 11:05:42 +0900367}