xref: /linux/arch/mips/mm/cache.c (revision 55fdcb2d56b6edc027657fde9da0c4f224d32303)
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>
191da177e4SLinus Torvalds #include <asm/processor.h>
201da177e4SLinus Torvalds #include <asm/cpu.h>
211da177e4SLinus Torvalds #include <asm/cpu-features.h>
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /* Cache operations. */
241da177e4SLinus Torvalds void (*flush_cache_all)(void);
251da177e4SLinus Torvalds void (*__flush_cache_all)(void);
261da177e4SLinus Torvalds void (*flush_cache_mm)(struct mm_struct *mm);
271da177e4SLinus Torvalds void (*flush_cache_range)(struct vm_area_struct *vma, unsigned long start,
281da177e4SLinus Torvalds 	unsigned long end);
2953de0d47SRalf Baechle void (*flush_cache_page)(struct vm_area_struct *vma, unsigned long page,
3053de0d47SRalf Baechle 	unsigned long pfn);
31d4264f18SAtsushi Nemoto void (*flush_icache_range)(unsigned long start, unsigned long end);
328229f1a0SKees Cook EXPORT_SYMBOL_GPL(flush_icache_range);
33e0cee3eeSThomas Bogendoerfer void (*local_flush_icache_range)(unsigned long start, unsigned long end);
3490f91356SJames Hogan EXPORT_SYMBOL_GPL(local_flush_icache_range);
351da177e4SLinus Torvalds 
369c5a3d72SRalf Baechle void (*__flush_cache_vmap)(void);
379c5a3d72SRalf Baechle void (*__flush_cache_vunmap)(void);
389c5a3d72SRalf Baechle 
39d9cdc901SRalf Baechle void (*__flush_kernel_vmap_range)(unsigned long vaddr, int size);
40d9cdc901SRalf Baechle EXPORT_SYMBOL_GPL(__flush_kernel_vmap_range);
418229f1a0SKees Cook void (*__invalidate_kernel_vmap_range)(unsigned long vaddr, int size);
42d9cdc901SRalf Baechle 
431da177e4SLinus Torvalds /* MIPS specific cache operations */
441da177e4SLinus Torvalds void (*flush_cache_sigtramp)(unsigned long addr);
457e3bfc7cSRalf Baechle void (*local_flush_data_cache_page)(void * addr);
461da177e4SLinus Torvalds void (*flush_data_cache_page)(unsigned long addr);
471da177e4SLinus Torvalds void (*flush_icache_all)(void);
481da177e4SLinus Torvalds 
499202f325SRalf Baechle EXPORT_SYMBOL_GPL(local_flush_data_cache_page);
509ff77c46SRalf Baechle EXPORT_SYMBOL(flush_data_cache_page);
51f2e3656dSSanjay Lal EXPORT_SYMBOL(flush_icache_all);
529ff77c46SRalf Baechle 
538005711cSManuel Lauss #if defined(CONFIG_DMA_NONCOHERENT) || defined(CONFIG_DMA_MAYBE_COHERENT)
541da177e4SLinus Torvalds 
551da177e4SLinus Torvalds /* DMA cache operations. */
561da177e4SLinus Torvalds void (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
571da177e4SLinus Torvalds void (*_dma_cache_wback)(unsigned long start, unsigned long size);
581da177e4SLinus Torvalds void (*_dma_cache_inv)(unsigned long start, unsigned long size);
591da177e4SLinus Torvalds 
601da177e4SLinus Torvalds EXPORT_SYMBOL(_dma_cache_wback_inv);
611da177e4SLinus Torvalds 
628005711cSManuel Lauss #endif /* CONFIG_DMA_NONCOHERENT || CONFIG_DMA_MAYBE_COHERENT */
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /*
651da177e4SLinus Torvalds  * We could optimize the case where the cache argument is not BCACHE but
661da177e4SLinus Torvalds  * that seems very atypical use ...
671da177e4SLinus Torvalds  */
68dbda6ac0SRalf Baechle SYSCALL_DEFINE3(cacheflush, unsigned long, addr, unsigned long, bytes,
69dbda6ac0SRalf Baechle 	unsigned int, cache)
701da177e4SLinus Torvalds {
71750ccf68SAtsushi Nemoto 	if (bytes == 0)
72750ccf68SAtsushi Nemoto 		return 0;
73fe00f943SRalf Baechle 	if (!access_ok(VERIFY_WRITE, (void __user *) addr, bytes))
741da177e4SLinus Torvalds 		return -EFAULT;
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds 	flush_icache_range(addr, addr + bytes);
771da177e4SLinus Torvalds 
781da177e4SLinus Torvalds 	return 0;
791da177e4SLinus Torvalds }
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds void __flush_dcache_page(struct page *page)
821da177e4SLinus Torvalds {
831da177e4SLinus Torvalds 	struct address_space *mapping = page_mapping(page);
841da177e4SLinus Torvalds 	unsigned long addr;
851da177e4SLinus Torvalds 
86585fa724SRalf Baechle 	if (PageHighMem(page))
87585fa724SRalf Baechle 		return;
881da177e4SLinus Torvalds 	if (mapping && !mapping_mapped(mapping)) {
891da177e4SLinus Torvalds 		SetPageDcacheDirty(page);
901da177e4SLinus Torvalds 		return;
911da177e4SLinus Torvalds 	}
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds 	/*
941da177e4SLinus Torvalds 	 * We could delay the flush for the !page_mapping case too.  But that
951da177e4SLinus Torvalds 	 * case is for exec env/arg pages and those are %99 certainly going to
961da177e4SLinus Torvalds 	 * get faulted into the tlb (and thus flushed) anyways.
971da177e4SLinus Torvalds 	 */
981da177e4SLinus Torvalds 	addr = (unsigned long) page_address(page);
991da177e4SLinus Torvalds 	flush_data_cache_page(addr);
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds EXPORT_SYMBOL(__flush_dcache_page);
1031da177e4SLinus Torvalds 
1047575a49fSRalf Baechle void __flush_anon_page(struct page *page, unsigned long vmaddr)
1057575a49fSRalf Baechle {
1069a74b3ebSRalf Baechle 	unsigned long addr = (unsigned long) page_address(page);
1079a74b3ebSRalf Baechle 
1089a74b3ebSRalf Baechle 	if (pages_do_alias(addr, vmaddr)) {
1099a74b3ebSRalf Baechle 		if (page_mapped(page) && !Page_dcache_dirty(page)) {
1107575a49fSRalf Baechle 			void *kaddr;
1117575a49fSRalf Baechle 
1127575a49fSRalf Baechle 			kaddr = kmap_coherent(page, vmaddr);
1137575a49fSRalf Baechle 			flush_data_cache_page((unsigned long)kaddr);
114eacb9d61SRalf Baechle 			kunmap_coherent();
1159a74b3ebSRalf Baechle 		} else
1169a74b3ebSRalf Baechle 			flush_data_cache_page(addr);
1177575a49fSRalf Baechle 	}
1187575a49fSRalf Baechle }
1197575a49fSRalf Baechle 
1207575a49fSRalf Baechle EXPORT_SYMBOL(__flush_anon_page);
1217575a49fSRalf Baechle 
1224d46a67aSLars Persson void __flush_icache_page(struct vm_area_struct *vma, struct page *page)
1234d46a67aSLars Persson {
1244d46a67aSLars Persson 	unsigned long addr;
1254d46a67aSLars Persson 
1264d46a67aSLars Persson 	if (PageHighMem(page))
1274d46a67aSLars Persson 		return;
1284d46a67aSLars Persson 
1294d46a67aSLars Persson 	addr = (unsigned long) page_address(page);
1304d46a67aSLars Persson 	flush_data_cache_page(addr);
1314d46a67aSLars Persson }
1324d46a67aSLars Persson EXPORT_SYMBOL_GPL(__flush_icache_page);
1334d46a67aSLars Persson 
1345b9593f3SLars Persson void __update_cache(struct vm_area_struct *vma, unsigned long address,
1355b9593f3SLars Persson 	pte_t pte)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	struct page *page;
1385b9593f3SLars Persson 	unsigned long pfn, addr;
1395b9593f3SLars Persson 	int exec = (vma->vm_flags & VM_EXEC) && !cpu_has_ic_fills_f_dc;
1401da177e4SLinus Torvalds 
1415b9593f3SLars Persson 	pfn = pte_pfn(pte);
142585fa724SRalf Baechle 	if (unlikely(!pfn_valid(pfn)))
143585fa724SRalf Baechle 		return;
144585fa724SRalf Baechle 	page = pfn_to_page(pfn);
145585fa724SRalf Baechle 	if (page_mapping(page) && Page_dcache_dirty(page)) {
1465b9593f3SLars Persson 		addr = (unsigned long) page_address(page);
1475b9593f3SLars Persson 		if (exec || pages_do_alias(addr, address & PAGE_MASK))
1485b9593f3SLars Persson 			flush_data_cache_page(addr);
1491da177e4SLinus Torvalds 		ClearPageDcacheDirty(page);
1501da177e4SLinus Torvalds 	}
1511da177e4SLinus Torvalds }
1521da177e4SLinus Torvalds 
15335133692SChris Dearman unsigned long _page_cachable_default;
1547b3e543dSAnton Altaparmakov EXPORT_SYMBOL(_page_cachable_default);
15535133692SChris Dearman 
15635133692SChris Dearman static inline void setup_protection_map(void)
15735133692SChris Dearman {
15805857c64SSteven J. Hill 	if (cpu_has_rixi) {
1596dd9344cSDavid Daney 		protection_map[0]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1606dd9344cSDavid Daney 		protection_map[1]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
1616dd9344cSDavid Daney 		protection_map[2]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1626dd9344cSDavid Daney 		protection_map[3]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
163*55fdcb2dSRalf Baechle 		protection_map[4]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1646dd9344cSDavid Daney 		protection_map[5]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
165*55fdcb2dSRalf Baechle 		protection_map[6]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1666dd9344cSDavid Daney 		protection_map[7]  = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1676dd9344cSDavid Daney 
1686dd9344cSDavid Daney 		protection_map[8]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
1696dd9344cSDavid Daney 		protection_map[9]  = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC);
1706dd9344cSDavid Daney 		protection_map[10] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE | _PAGE_NO_READ);
1716dd9344cSDavid Daney 		protection_map[11] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE);
172*55fdcb2dSRalf Baechle 		protection_map[12] = __pgprot(_page_cachable_default | _PAGE_PRESENT);
1736dd9344cSDavid Daney 		protection_map[13] = __pgprot(_page_cachable_default | _PAGE_PRESENT);
174*55fdcb2dSRalf Baechle 		protection_map[14] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_WRITE);
1756dd9344cSDavid Daney 		protection_map[15] = __pgprot(_page_cachable_default | _PAGE_PRESENT | _PAGE_WRITE);
1766dd9344cSDavid Daney 
1776dd9344cSDavid Daney 	} else {
17835133692SChris Dearman 		protection_map[0] = PAGE_NONE;
17935133692SChris Dearman 		protection_map[1] = PAGE_READONLY;
18035133692SChris Dearman 		protection_map[2] = PAGE_COPY;
18135133692SChris Dearman 		protection_map[3] = PAGE_COPY;
18235133692SChris Dearman 		protection_map[4] = PAGE_READONLY;
18335133692SChris Dearman 		protection_map[5] = PAGE_READONLY;
18435133692SChris Dearman 		protection_map[6] = PAGE_COPY;
18535133692SChris Dearman 		protection_map[7] = PAGE_COPY;
18635133692SChris Dearman 		protection_map[8] = PAGE_NONE;
18735133692SChris Dearman 		protection_map[9] = PAGE_READONLY;
18835133692SChris Dearman 		protection_map[10] = PAGE_SHARED;
18935133692SChris Dearman 		protection_map[11] = PAGE_SHARED;
19035133692SChris Dearman 		protection_map[12] = PAGE_READONLY;
19135133692SChris Dearman 		protection_map[13] = PAGE_READONLY;
19235133692SChris Dearman 		protection_map[14] = PAGE_SHARED;
19335133692SChris Dearman 		protection_map[15] = PAGE_SHARED;
19435133692SChris Dearman 	}
1956dd9344cSDavid Daney }
1961da177e4SLinus Torvalds 
197078a55fcSPaul Gortmaker void cpu_cache_init(void)
1981da177e4SLinus Torvalds {
19902cf2119SRalf Baechle 	if (cpu_has_3k_cache) {
20002cf2119SRalf Baechle 		extern void __weak r3k_cache_init(void);
2011da177e4SLinus Torvalds 
20202cf2119SRalf Baechle 		r3k_cache_init();
2031da177e4SLinus Torvalds 	}
20402cf2119SRalf Baechle 	if (cpu_has_6k_cache) {
20502cf2119SRalf Baechle 		extern void __weak r6k_cache_init(void);
20602cf2119SRalf Baechle 
20702cf2119SRalf Baechle 		r6k_cache_init();
20802cf2119SRalf Baechle 	}
20902cf2119SRalf Baechle 	if (cpu_has_4k_cache) {
21002cf2119SRalf Baechle 		extern void __weak r4k_cache_init(void);
21102cf2119SRalf Baechle 
21202cf2119SRalf Baechle 		r4k_cache_init();
21302cf2119SRalf Baechle 	}
21402cf2119SRalf Baechle 	if (cpu_has_8k_cache) {
21502cf2119SRalf Baechle 		extern void __weak r8k_cache_init(void);
21602cf2119SRalf Baechle 
21702cf2119SRalf Baechle 		r8k_cache_init();
21802cf2119SRalf Baechle 	}
21902cf2119SRalf Baechle 	if (cpu_has_tx39_cache) {
22002cf2119SRalf Baechle 		extern void __weak tx39_cache_init(void);
22102cf2119SRalf Baechle 
22202cf2119SRalf Baechle 		tx39_cache_init();
22302cf2119SRalf Baechle 	}
22402cf2119SRalf Baechle 
22547d979ecSDavid Daney 	if (cpu_has_octeon_cache) {
22647d979ecSDavid Daney 		extern void __weak octeon_cache_init(void);
22747d979ecSDavid Daney 
22847d979ecSDavid Daney 		octeon_cache_init();
22947d979ecSDavid Daney 	}
23047d979ecSDavid Daney 
23135133692SChris Dearman 	setup_protection_map();
2321da177e4SLinus Torvalds }
23324e9d0b9SRalf Baechle 
23424e9d0b9SRalf Baechle int __weak __uncached_access(struct file *file, unsigned long addr)
23524e9d0b9SRalf Baechle {
2366b2f3d1fSChristoph Hellwig 	if (file->f_flags & O_DSYNC)
23724e9d0b9SRalf Baechle 		return 1;
23824e9d0b9SRalf Baechle 
23924e9d0b9SRalf Baechle 	return addr >= __pa(high_memory);
24024e9d0b9SRalf Baechle }
241