xref: /linux/arch/mips/mm/cache.c (revision 8e3a9f4c3ab6dd0da5e8a89bd252518ff2ee5e3a)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * This file is subject to the terms and conditions of the GNU General Public
31da177e4SLinus Torvalds  * License.  See the file "COPYING" in the main directory of this archive
41da177e4SLinus Torvalds  * for more details.
51da177e4SLinus Torvalds  *
6641e97f3SRalf Baechle  * Copyright (C) 1994 - 2003, 06, 07 by Ralf Baechle (ralf@linux-mips.org)
77575a49fSRalf Baechle  * Copyright (C) 2007 MIPS Technologies, Inc.
81da177e4SLinus Torvalds  */
924e9d0b9SRalf Baechle #include <linux/fs.h>
1024e9d0b9SRalf Baechle #include <linux/fcntl.h>
111da177e4SLinus Torvalds #include <linux/kernel.h>
12641e97f3SRalf Baechle #include <linux/linkage.h>
131da177e4SLinus Torvalds #include <linux/module.h>
141da177e4SLinus Torvalds #include <linux/sched.h>
15dbda6ac0SRalf Baechle #include <linux/syscalls.h>
161da177e4SLinus Torvalds #include <linux/mm.h>
171da177e4SLinus Torvalds 
181da177e4SLinus Torvalds #include <asm/cacheflush.h>
19234859e4SPaul Burton #include <asm/highmem.h>
201da177e4SLinus Torvalds #include <asm/processor.h>
211da177e4SLinus Torvalds #include <asm/cpu.h>
221da177e4SLinus Torvalds #include <asm/cpu-features.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds /* Cache operations. */
251da177e4SLinus Torvalds void (*flush_cache_all)(void);
261da177e4SLinus Torvalds void (*__flush_cache_all)(void);
271da177e4SLinus Torvalds void (*flush_cache_mm)(struct mm_struct *mm);
281da177e4SLinus Torvalds void (*flush_cache_range)(struct vm_area_struct *vma, unsigned long start,
291da177e4SLinus Torvalds 	unsigned long end);
3053de0d47SRalf Baechle void (*flush_cache_page)(struct vm_area_struct *vma, unsigned long page,
3153de0d47SRalf Baechle 	unsigned long pfn);
32d4264f18SAtsushi Nemoto void (*flush_icache_range)(unsigned long start, unsigned long end);
338229f1a0SKees Cook EXPORT_SYMBOL_GPL(flush_icache_range);
34e0cee3eeSThomas Bogendoerfer void (*local_flush_icache_range)(unsigned long start, unsigned long end);
3590f91356SJames Hogan EXPORT_SYMBOL_GPL(local_flush_icache_range);
3601882b4dSJames Hogan void (*__flush_icache_user_range)(unsigned long start, unsigned long end);
3701882b4dSJames Hogan EXPORT_SYMBOL_GPL(__flush_icache_user_range);
3801882b4dSJames Hogan void (*__local_flush_icache_user_range)(unsigned long start, unsigned long end);
3901882b4dSJames Hogan EXPORT_SYMBOL_GPL(__local_flush_icache_user_range);
401da177e4SLinus Torvalds 
419c5a3d72SRalf Baechle void (*__flush_cache_vmap)(void);
429c5a3d72SRalf Baechle void (*__flush_cache_vunmap)(void);
439c5a3d72SRalf Baechle 
44d9cdc901SRalf Baechle void (*__flush_kernel_vmap_range)(unsigned long vaddr, int size);
45d9cdc901SRalf Baechle EXPORT_SYMBOL_GPL(__flush_kernel_vmap_range);
468229f1a0SKees Cook void (*__invalidate_kernel_vmap_range)(unsigned long vaddr, int size);
47d9cdc901SRalf Baechle 
481da177e4SLinus Torvalds /* MIPS specific cache operations */
491da177e4SLinus Torvalds void (*flush_cache_sigtramp)(unsigned long addr);
507e3bfc7cSRalf Baechle void (*local_flush_data_cache_page)(void * addr);
511da177e4SLinus Torvalds void (*flush_data_cache_page)(unsigned long addr);
521da177e4SLinus Torvalds void (*flush_icache_all)(void);
531da177e4SLinus Torvalds 
549202f325SRalf Baechle EXPORT_SYMBOL_GPL(local_flush_data_cache_page);
559ff77c46SRalf Baechle EXPORT_SYMBOL(flush_data_cache_page);
56f2e3656dSSanjay Lal EXPORT_SYMBOL(flush_icache_all);
579ff77c46SRalf Baechle 
588005711cSManuel Lauss #if defined(CONFIG_DMA_NONCOHERENT) || defined(CONFIG_DMA_MAYBE_COHERENT)
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds /* DMA cache operations. */
611da177e4SLinus Torvalds void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
621da177e4SLinus Torvalds void (*_dma_cache_wback)(unsigned long start, unsigned long size);
631da177e4SLinus Torvalds void (*_dma_cache_inv)(unsigned long start, unsigned long size);
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds EXPORT_SYMBOL(_dma_cache_wback_inv);
661da177e4SLinus Torvalds 
678005711cSManuel Lauss #endif /* CONFIG_DMA_NONCOHERENT || CONFIG_DMA_MAYBE_COHERENT */
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds /*
701da177e4SLinus Torvalds  * We could optimize the case where the cache argument is not BCACHE but
711da177e4SLinus Torvalds  * that seems very atypical use ...
721da177e4SLinus Torvalds  */
73dbda6ac0SRalf Baechle SYSCALL_DEFINE3(cacheflush, unsigned long, addr, unsigned long, bytes,
74dbda6ac0SRalf Baechle 	unsigned int, cache)
751da177e4SLinus Torvalds {
76750ccf68SAtsushi Nemoto 	if (bytes == 0)
77750ccf68SAtsushi Nemoto 		return 0;
78fe00f943SRalf Baechle 	if (!access_ok(VERIFY_WRITE, (void __user *) addr, bytes))
791da177e4SLinus Torvalds 		return -EFAULT;
801da177e4SLinus Torvalds 
81*8e3a9f4cSJames Hogan 	__flush_icache_user_range(addr, addr + bytes);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds 	return 0;
841da177e4SLinus Torvalds }
851da177e4SLinus Torvalds 
861da177e4SLinus Torvalds void __flush_dcache_page(struct page *page)
871da177e4SLinus Torvalds {
881da177e4SLinus Torvalds 	struct address_space *mapping = page_mapping(page);
891da177e4SLinus Torvalds 	unsigned long addr;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	if (mapping && !mapping_mapped(mapping)) {
921da177e4SLinus Torvalds 		SetPageDcacheDirty(page);
931da177e4SLinus Torvalds 		return;
941da177e4SLinus Torvalds 	}
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	/*
971da177e4SLinus Torvalds 	 * We could delay the flush for the !page_mapping case too.  But that
981da177e4SLinus Torvalds 	 * case is for exec env/arg pages and those are %99 certainly going to
991da177e4SLinus Torvalds 	 * get faulted into the tlb (and thus flushed) anyways.
1001da177e4SLinus Torvalds 	 */
101234859e4SPaul Burton 	if (PageHighMem(page))
102234859e4SPaul Burton 		addr = (unsigned long)kmap_atomic(page);
103234859e4SPaul Burton 	else
1041da177e4SLinus Torvalds 		addr = (unsigned long)page_address(page);
105234859e4SPaul Burton 
1061da177e4SLinus Torvalds 	flush_data_cache_page(addr);
107234859e4SPaul Burton 
108234859e4SPaul Burton 	if (PageHighMem(page))
109234859e4SPaul Burton 		__kunmap_atomic((void *)addr);
1101da177e4SLinus Torvalds }
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds EXPORT_SYMBOL(__flush_dcache_page);
1131da177e4SLinus Torvalds 
1147575a49fSRalf Baechle void __flush_anon_page(struct page *page, unsigned long vmaddr)
1157575a49fSRalf Baechle {
1169a74b3ebSRalf Baechle 	unsigned long addr = (unsigned long) page_address(page);
1179a74b3ebSRalf Baechle 
1189a74b3ebSRalf Baechle 	if (pages_do_alias(addr, vmaddr)) {
119e1534ae9SKirill A. Shutemov 		if (page_mapcount(page) && !Page_dcache_dirty(page)) {
1207575a49fSRalf Baechle 			void *kaddr;
1217575a49fSRalf Baechle 
1227575a49fSRalf Baechle 			kaddr = kmap_coherent(page, vmaddr);
1237575a49fSRalf Baechle 			flush_data_cache_page((unsigned long)kaddr);
124eacb9d61SRalf Baechle 			kunmap_coherent();
1259a74b3ebSRalf Baechle 		} else
1269a74b3ebSRalf Baechle 			flush_data_cache_page(addr);
1277575a49fSRalf Baechle 	}
1287575a49fSRalf Baechle }
1297575a49fSRalf Baechle 
1307575a49fSRalf Baechle EXPORT_SYMBOL(__flush_anon_page);
1317575a49fSRalf Baechle 
13237d22a0dSPaul Burton void __update_cache(unsigned long address, pte_t pte)
1331da177e4SLinus Torvalds {
1341da177e4SLinus Torvalds 	struct page *page;
1355b9593f3SLars Persson 	unsigned long pfn, addr;
13637d22a0dSPaul Burton 	int exec = !pte_no_exec(pte) && !cpu_has_ic_fills_f_dc;
1371da177e4SLinus Torvalds 
1385b9593f3SLars Persson 	pfn = pte_pfn(pte);
139585fa724SRalf Baechle 	if (unlikely(!pfn_valid(pfn)))
140585fa724SRalf Baechle 		return;
141585fa724SRalf Baechle 	page = pfn_to_page(pfn);
14237d22a0dSPaul Burton 	if (Page_dcache_dirty(page)) {
143f4281bbaSPaul Burton 		if (PageHighMem(page))
144f4281bbaSPaul Burton 			addr = (unsigned long)kmap_atomic(page);
145f4281bbaSPaul Burton 		else
1465b9593f3SLars Persson 			addr = (unsigned long)page_address(page);
147f4281bbaSPaul Burton 
1485b9593f3SLars Persson 		if (exec || pages_do_alias(addr, address & PAGE_MASK))
1495b9593f3SLars Persson 			flush_data_cache_page(addr);
150f4281bbaSPaul Burton 
151f4281bbaSPaul Burton 		if (PageHighMem(page))
152f4281bbaSPaul Burton 			__kunmap_atomic((void *)addr);
153f4281bbaSPaul Burton 
1541da177e4SLinus Torvalds 		ClearPageDcacheDirty(page);
1551da177e4SLinus Torvalds 	}
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds 
15835133692SChris Dearman unsigned long _page_cachable_default;
1597b3e543dSAnton Altaparmakov EXPORT_SYMBOL(_page_cachable_default);
16035133692SChris Dearman 
16135133692SChris Dearman static inline void setup_protection_map(void)
16235133692SChris Dearman {
16305857c64SSteven J. Hill 	if (cpu_has_rixi) {
1646dd9344cSDavid Daney 		protection_map[0]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1656dd9344cSDavid Daney 		protection_map[1]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
1666dd9344cSDavid Daney 		protection_map[2]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1676dd9344cSDavid Daney 		protection_map[3]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
16855fdcb2dSRalf Baechle 		protection_map[4]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1696dd9344cSDavid Daney 		protection_map[5]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
17055fdcb2dSRalf Baechle 		protection_map[6]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1716dd9344cSDavid Daney 		protection_map[7]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1726dd9344cSDavid Daney 
1736dd9344cSDavid Daney 		protection_map[8]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1746dd9344cSDavid Daney 		protection_map[9]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
1756dd9344cSDavid Daney 		protection_map[10] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE | _PAGE_NO_READ);
1766dd9344cSDavid Daney 		protection_map[11] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE);
17755fdcb2dSRalf Baechle 		protection_map[12] = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1786dd9344cSDavid Daney 		protection_map[13] = __pgprot(_page_cachable_default | _PAGE_PRESENT);
17955fdcb2dSRalf Baechle 		protection_map[14] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_WRITE);
1806dd9344cSDavid Daney 		protection_map[15] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_WRITE);
1816dd9344cSDavid Daney 
1826dd9344cSDavid Daney 	} else {
18335133692SChris Dearman 		protection_map[0] = PAGE_NONE;
18435133692SChris Dearman 		protection_map[1] = PAGE_READONLY;
18535133692SChris Dearman 		protection_map[2] = PAGE_COPY;
18635133692SChris Dearman 		protection_map[3] = PAGE_COPY;
18735133692SChris Dearman 		protection_map[4] = PAGE_READONLY;
18835133692SChris Dearman 		protection_map[5] = PAGE_READONLY;
18935133692SChris Dearman 		protection_map[6] = PAGE_COPY;
19035133692SChris Dearman 		protection_map[7] = PAGE_COPY;
19135133692SChris Dearman 		protection_map[8] = PAGE_NONE;
19235133692SChris Dearman 		protection_map[9] = PAGE_READONLY;
19335133692SChris Dearman 		protection_map[10] = PAGE_SHARED;
19435133692SChris Dearman 		protection_map[11] = PAGE_SHARED;
19535133692SChris Dearman 		protection_map[12] = PAGE_READONLY;
19635133692SChris Dearman 		protection_map[13] = PAGE_READONLY;
19735133692SChris Dearman 		protection_map[14] = PAGE_SHARED;
19835133692SChris Dearman 		protection_map[15] = PAGE_SHARED;
19935133692SChris Dearman 	}
2006dd9344cSDavid Daney }
2011da177e4SLinus Torvalds 
202078a55fcSPaul Gortmaker void cpu_cache_init(void)
2031da177e4SLinus Torvalds {
20402cf2119SRalf Baechle 	if (cpu_has_3k_cache) {
20502cf2119SRalf Baechle 		extern void __weak r3k_cache_init(void);
2061da177e4SLinus Torvalds 
20702cf2119SRalf Baechle 		r3k_cache_init();
2081da177e4SLinus Torvalds 	}
20902cf2119SRalf Baechle 	if (cpu_has_6k_cache) {
21002cf2119SRalf Baechle 		extern void __weak r6k_cache_init(void);
21102cf2119SRalf Baechle 
21202cf2119SRalf Baechle 		r6k_cache_init();
21302cf2119SRalf Baechle 	}
21402cf2119SRalf Baechle 	if (cpu_has_4k_cache) {
21502cf2119SRalf Baechle 		extern void __weak r4k_cache_init(void);
21602cf2119SRalf Baechle 
21702cf2119SRalf Baechle 		r4k_cache_init();
21802cf2119SRalf Baechle 	}
21902cf2119SRalf Baechle 	if (cpu_has_8k_cache) {
22002cf2119SRalf Baechle 		extern void __weak r8k_cache_init(void);
22102cf2119SRalf Baechle 
22202cf2119SRalf Baechle 		r8k_cache_init();
22302cf2119SRalf Baechle 	}
22402cf2119SRalf Baechle 	if (cpu_has_tx39_cache) {
22502cf2119SRalf Baechle 		extern void __weak tx39_cache_init(void);
22602cf2119SRalf Baechle 
22702cf2119SRalf Baechle 		tx39_cache_init();
22802cf2119SRalf Baechle 	}
22902cf2119SRalf Baechle 
23047d979ecSDavid Daney 	if (cpu_has_octeon_cache) {
23147d979ecSDavid Daney 		extern void __weak octeon_cache_init(void);
23247d979ecSDavid Daney 
23347d979ecSDavid Daney 		octeon_cache_init();
23447d979ecSDavid Daney 	}
23547d979ecSDavid Daney 
23635133692SChris Dearman 	setup_protection_map();
2371da177e4SLinus Torvalds }
23824e9d0b9SRalf Baechle 
23924e9d0b9SRalf Baechle int __weak __uncached_access(struct file *file, unsigned long addr)
24024e9d0b9SRalf Baechle {
2416b2f3d1fSChristoph Hellwig 	if (file->f_flags & O_DSYNC)
24224e9d0b9SRalf Baechle 		return 1;
24324e9d0b9SRalf Baechle 
24424e9d0b9SRalf Baechle 	return addr >= __pa(high_memory);
24524e9d0b9SRalf Baechle }
246