xref: /linux/arch/mips/mm/cache.c (revision 4fa9de5a645a9770679032a7eea0604f9a36eaf3)
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>
13d9ba5778SPaul Gortmaker #include <linux/export.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);
27*4fa9de5aSJames Hogan EXPORT_SYMBOL_GPL(__flush_cache_all);
281da177e4SLinus Torvalds void (*flush_cache_mm)(struct mm_struct *mm);
291da177e4SLinus Torvalds void (*flush_cache_range)(struct vm_area_struct *vma, unsigned long start,
301da177e4SLinus Torvalds 	unsigned long end);
3153de0d47SRalf Baechle void (*flush_cache_page)(struct vm_area_struct *vma, unsigned long page,
3253de0d47SRalf Baechle 	unsigned long pfn);
33d4264f18SAtsushi Nemoto void (*flush_icache_range)(unsigned long start, unsigned long end);
348229f1a0SKees Cook EXPORT_SYMBOL_GPL(flush_icache_range);
35e0cee3eeSThomas Bogendoerfer void (*local_flush_icache_range)(unsigned long start, unsigned long end);
3690f91356SJames Hogan EXPORT_SYMBOL_GPL(local_flush_icache_range);
3701882b4dSJames Hogan void (*__flush_icache_user_range)(unsigned long start, unsigned long end);
3801882b4dSJames Hogan EXPORT_SYMBOL_GPL(__flush_icache_user_range);
3901882b4dSJames Hogan void (*__local_flush_icache_user_range)(unsigned long start, unsigned long end);
4001882b4dSJames Hogan EXPORT_SYMBOL_GPL(__local_flush_icache_user_range);
411da177e4SLinus Torvalds 
429c5a3d72SRalf Baechle void (*__flush_cache_vmap)(void);
439c5a3d72SRalf Baechle void (*__flush_cache_vunmap)(void);
449c5a3d72SRalf Baechle 
45d9cdc901SRalf Baechle void (*__flush_kernel_vmap_range)(unsigned long vaddr, int size);
46d9cdc901SRalf Baechle EXPORT_SYMBOL_GPL(__flush_kernel_vmap_range);
478229f1a0SKees Cook void (*__invalidate_kernel_vmap_range)(unsigned long vaddr, int size);
48d9cdc901SRalf Baechle 
491da177e4SLinus Torvalds /* MIPS specific cache operations */
501da177e4SLinus Torvalds void (*flush_cache_sigtramp)(unsigned long addr);
517e3bfc7cSRalf Baechle void (*local_flush_data_cache_page)(void * addr);
521da177e4SLinus Torvalds void (*flush_data_cache_page)(unsigned long addr);
531da177e4SLinus Torvalds void (*flush_icache_all)(void);
541da177e4SLinus Torvalds 
559202f325SRalf Baechle EXPORT_SYMBOL_GPL(local_flush_data_cache_page);
569ff77c46SRalf Baechle EXPORT_SYMBOL(flush_data_cache_page);
57f2e3656dSSanjay Lal EXPORT_SYMBOL(flush_icache_all);
589ff77c46SRalf Baechle 
598005711cSManuel Lauss #if defined(CONFIG_DMA_NONCOHERENT) || defined(CONFIG_DMA_MAYBE_COHERENT)
601da177e4SLinus Torvalds 
611da177e4SLinus Torvalds /* DMA cache operations. */
621da177e4SLinus Torvalds void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
631da177e4SLinus Torvalds void (*_dma_cache_wback)(unsigned long start, unsigned long size);
641da177e4SLinus Torvalds void (*_dma_cache_inv)(unsigned long start, unsigned long size);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds EXPORT_SYMBOL(_dma_cache_wback_inv);
671da177e4SLinus Torvalds 
688005711cSManuel Lauss #endif /* CONFIG_DMA_NONCOHERENT || CONFIG_DMA_MAYBE_COHERENT */
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /*
711da177e4SLinus Torvalds  * We could optimize the case where the cache argument is not BCACHE but
721da177e4SLinus Torvalds  * that seems very atypical use ...
731da177e4SLinus Torvalds  */
74dbda6ac0SRalf Baechle SYSCALL_DEFINE3(cacheflush, unsigned long, addr, unsigned long, bytes,
75dbda6ac0SRalf Baechle 	unsigned int, cache)
761da177e4SLinus Torvalds {
77750ccf68SAtsushi Nemoto 	if (bytes == 0)
78750ccf68SAtsushi Nemoto 		return 0;
79fe00f943SRalf Baechle 	if (!access_ok(VERIFY_WRITE, (void __user *) addr, bytes))
801da177e4SLinus Torvalds 		return -EFAULT;
811da177e4SLinus Torvalds 
828e3a9f4cSJames Hogan 	__flush_icache_user_range(addr, addr + bytes);
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	return 0;
851da177e4SLinus Torvalds }
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds void __flush_dcache_page(struct page *page)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	struct address_space *mapping = page_mapping(page);
901da177e4SLinus Torvalds 	unsigned long addr;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	if (mapping && !mapping_mapped(mapping)) {
931da177e4SLinus Torvalds 		SetPageDcacheDirty(page);
941da177e4SLinus Torvalds 		return;
951da177e4SLinus Torvalds 	}
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds 	/*
981da177e4SLinus Torvalds 	 * We could delay the flush for the !page_mapping case too.  But that
991da177e4SLinus Torvalds 	 * case is for exec env/arg pages and those are %99 certainly going to
1001da177e4SLinus Torvalds 	 * get faulted into the tlb (and thus flushed) anyways.
1011da177e4SLinus Torvalds 	 */
102234859e4SPaul Burton 	if (PageHighMem(page))
103234859e4SPaul Burton 		addr = (unsigned long)kmap_atomic(page);
104234859e4SPaul Burton 	else
1051da177e4SLinus Torvalds 		addr = (unsigned long)page_address(page);
106234859e4SPaul Burton 
1071da177e4SLinus Torvalds 	flush_data_cache_page(addr);
108234859e4SPaul Burton 
109234859e4SPaul Burton 	if (PageHighMem(page))
110234859e4SPaul Burton 		__kunmap_atomic((void *)addr);
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds EXPORT_SYMBOL(__flush_dcache_page);
1141da177e4SLinus Torvalds 
1157575a49fSRalf Baechle void __flush_anon_page(struct page *page, unsigned long vmaddr)
1167575a49fSRalf Baechle {
1179a74b3ebSRalf Baechle 	unsigned long addr = (unsigned long) page_address(page);
1189a74b3ebSRalf Baechle 
1199a74b3ebSRalf Baechle 	if (pages_do_alias(addr, vmaddr)) {
120e1534ae9SKirill A. Shutemov 		if (page_mapcount(page) && !Page_dcache_dirty(page)) {
1217575a49fSRalf Baechle 			void *kaddr;
1227575a49fSRalf Baechle 
1237575a49fSRalf Baechle 			kaddr = kmap_coherent(page, vmaddr);
1247575a49fSRalf Baechle 			flush_data_cache_page((unsigned long)kaddr);
125eacb9d61SRalf Baechle 			kunmap_coherent();
1269a74b3ebSRalf Baechle 		} else
1279a74b3ebSRalf Baechle 			flush_data_cache_page(addr);
1287575a49fSRalf Baechle 	}
1297575a49fSRalf Baechle }
1307575a49fSRalf Baechle 
1317575a49fSRalf Baechle EXPORT_SYMBOL(__flush_anon_page);
1327575a49fSRalf Baechle 
13337d22a0dSPaul Burton void __update_cache(unsigned long address, pte_t pte)
1341da177e4SLinus Torvalds {
1351da177e4SLinus Torvalds 	struct page *page;
1365b9593f3SLars Persson 	unsigned long pfn, addr;
13737d22a0dSPaul Burton 	int exec = !pte_no_exec(pte) && !cpu_has_ic_fills_f_dc;
1381da177e4SLinus Torvalds 
1395b9593f3SLars Persson 	pfn = pte_pfn(pte);
140585fa724SRalf Baechle 	if (unlikely(!pfn_valid(pfn)))
141585fa724SRalf Baechle 		return;
142585fa724SRalf Baechle 	page = pfn_to_page(pfn);
14337d22a0dSPaul Burton 	if (Page_dcache_dirty(page)) {
144f4281bbaSPaul Burton 		if (PageHighMem(page))
145f4281bbaSPaul Burton 			addr = (unsigned long)kmap_atomic(page);
146f4281bbaSPaul Burton 		else
1475b9593f3SLars Persson 			addr = (unsigned long)page_address(page);
148f4281bbaSPaul Burton 
1495b9593f3SLars Persson 		if (exec || pages_do_alias(addr, address & PAGE_MASK))
1505b9593f3SLars Persson 			flush_data_cache_page(addr);
151f4281bbaSPaul Burton 
152f4281bbaSPaul Burton 		if (PageHighMem(page))
153f4281bbaSPaul Burton 			__kunmap_atomic((void *)addr);
154f4281bbaSPaul Burton 
1551da177e4SLinus Torvalds 		ClearPageDcacheDirty(page);
1561da177e4SLinus Torvalds 	}
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
15935133692SChris Dearman unsigned long _page_cachable_default;
1607b3e543dSAnton Altaparmakov EXPORT_SYMBOL(_page_cachable_default);
16135133692SChris Dearman 
16235133692SChris Dearman static inline void setup_protection_map(void)
16335133692SChris Dearman {
16405857c64SSteven J. Hill 	if (cpu_has_rixi) {
1656dd9344cSDavid Daney 		protection_map[0]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1666dd9344cSDavid Daney 		protection_map[1]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
1676dd9344cSDavid Daney 		protection_map[2]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1686dd9344cSDavid Daney 		protection_map[3]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
16955fdcb2dSRalf Baechle 		protection_map[4]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1706dd9344cSDavid Daney 		protection_map[5]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
17155fdcb2dSRalf Baechle 		protection_map[6]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1726dd9344cSDavid Daney 		protection_map[7]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1736dd9344cSDavid Daney 
1746dd9344cSDavid Daney 		protection_map[8]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1756dd9344cSDavid Daney 		protection_map[9]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
1766dd9344cSDavid Daney 		protection_map[10] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE | _PAGE_NO_READ);
1776dd9344cSDavid Daney 		protection_map[11] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE);
17855fdcb2dSRalf Baechle 		protection_map[12] = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1796dd9344cSDavid Daney 		protection_map[13] = __pgprot(_page_cachable_default | _PAGE_PRESENT);
18055fdcb2dSRalf Baechle 		protection_map[14] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_WRITE);
1816dd9344cSDavid Daney 		protection_map[15] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_WRITE);
1826dd9344cSDavid Daney 
1836dd9344cSDavid Daney 	} else {
18435133692SChris Dearman 		protection_map[0] = PAGE_NONE;
18535133692SChris Dearman 		protection_map[1] = PAGE_READONLY;
18635133692SChris Dearman 		protection_map[2] = PAGE_COPY;
18735133692SChris Dearman 		protection_map[3] = PAGE_COPY;
18835133692SChris Dearman 		protection_map[4] = PAGE_READONLY;
18935133692SChris Dearman 		protection_map[5] = PAGE_READONLY;
19035133692SChris Dearman 		protection_map[6] = PAGE_COPY;
19135133692SChris Dearman 		protection_map[7] = PAGE_COPY;
19235133692SChris Dearman 		protection_map[8] = PAGE_NONE;
19335133692SChris Dearman 		protection_map[9] = PAGE_READONLY;
19435133692SChris Dearman 		protection_map[10] = PAGE_SHARED;
19535133692SChris Dearman 		protection_map[11] = PAGE_SHARED;
19635133692SChris Dearman 		protection_map[12] = PAGE_READONLY;
19735133692SChris Dearman 		protection_map[13] = PAGE_READONLY;
19835133692SChris Dearman 		protection_map[14] = PAGE_SHARED;
19935133692SChris Dearman 		protection_map[15] = PAGE_SHARED;
20035133692SChris Dearman 	}
2016dd9344cSDavid Daney }
2021da177e4SLinus Torvalds 
203078a55fcSPaul Gortmaker void cpu_cache_init(void)
2041da177e4SLinus Torvalds {
20502cf2119SRalf Baechle 	if (cpu_has_3k_cache) {
20602cf2119SRalf Baechle 		extern void __weak r3k_cache_init(void);
2071da177e4SLinus Torvalds 
20802cf2119SRalf Baechle 		r3k_cache_init();
2091da177e4SLinus Torvalds 	}
21002cf2119SRalf Baechle 	if (cpu_has_6k_cache) {
21102cf2119SRalf Baechle 		extern void __weak r6k_cache_init(void);
21202cf2119SRalf Baechle 
21302cf2119SRalf Baechle 		r6k_cache_init();
21402cf2119SRalf Baechle 	}
21502cf2119SRalf Baechle 	if (cpu_has_4k_cache) {
21602cf2119SRalf Baechle 		extern void __weak r4k_cache_init(void);
21702cf2119SRalf Baechle 
21802cf2119SRalf Baechle 		r4k_cache_init();
21902cf2119SRalf Baechle 	}
22002cf2119SRalf Baechle 	if (cpu_has_8k_cache) {
22102cf2119SRalf Baechle 		extern void __weak r8k_cache_init(void);
22202cf2119SRalf Baechle 
22302cf2119SRalf Baechle 		r8k_cache_init();
22402cf2119SRalf Baechle 	}
22502cf2119SRalf Baechle 	if (cpu_has_tx39_cache) {
22602cf2119SRalf Baechle 		extern void __weak tx39_cache_init(void);
22702cf2119SRalf Baechle 
22802cf2119SRalf Baechle 		tx39_cache_init();
22902cf2119SRalf Baechle 	}
23002cf2119SRalf Baechle 
23147d979ecSDavid Daney 	if (cpu_has_octeon_cache) {
23247d979ecSDavid Daney 		extern void __weak octeon_cache_init(void);
23347d979ecSDavid Daney 
23447d979ecSDavid Daney 		octeon_cache_init();
23547d979ecSDavid Daney 	}
23647d979ecSDavid Daney 
23735133692SChris Dearman 	setup_protection_map();
2381da177e4SLinus Torvalds }
23924e9d0b9SRalf Baechle 
24024e9d0b9SRalf Baechle int __weak __uncached_access(struct file *file, unsigned long addr)
24124e9d0b9SRalf Baechle {
2426b2f3d1fSChristoph Hellwig 	if (file->f_flags & O_DSYNC)
24324e9d0b9SRalf Baechle 		return 1;
24424e9d0b9SRalf Baechle 
24524e9d0b9SRalf Baechle 	return addr >= __pa(high_memory);
24624e9d0b9SRalf Baechle }
247