162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * This file is subject to the terms and conditions of the GNU General Public
362306a36Sopenharmony_ci * License.  See the file "COPYING" in the main directory of this archive
462306a36Sopenharmony_ci * for more details.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Copyright (C) 1994 - 2003, 06, 07 by Ralf Baechle (ralf@linux-mips.org)
762306a36Sopenharmony_ci * Copyright (C) 2007 MIPS Technologies, Inc.
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci#include <linux/fs.h>
1062306a36Sopenharmony_ci#include <linux/fcntl.h>
1162306a36Sopenharmony_ci#include <linux/kernel.h>
1262306a36Sopenharmony_ci#include <linux/linkage.h>
1362306a36Sopenharmony_ci#include <linux/export.h>
1462306a36Sopenharmony_ci#include <linux/sched.h>
1562306a36Sopenharmony_ci#include <linux/syscalls.h>
1662306a36Sopenharmony_ci#include <linux/mm.h>
1762306a36Sopenharmony_ci#include <linux/highmem.h>
1862306a36Sopenharmony_ci#include <linux/pagemap.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci#include <asm/bcache.h>
2162306a36Sopenharmony_ci#include <asm/cacheflush.h>
2262306a36Sopenharmony_ci#include <asm/processor.h>
2362306a36Sopenharmony_ci#include <asm/cpu.h>
2462306a36Sopenharmony_ci#include <asm/cpu-features.h>
2562306a36Sopenharmony_ci#include <asm/setup.h>
2662306a36Sopenharmony_ci#include <asm/pgtable.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/* Cache operations. */
2962306a36Sopenharmony_civoid (*flush_cache_all)(void);
3062306a36Sopenharmony_civoid (*__flush_cache_all)(void);
3162306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__flush_cache_all);
3262306a36Sopenharmony_civoid (*flush_cache_mm)(struct mm_struct *mm);
3362306a36Sopenharmony_civoid (*flush_cache_range)(struct vm_area_struct *vma, unsigned long start,
3462306a36Sopenharmony_ci	unsigned long end);
3562306a36Sopenharmony_civoid (*flush_cache_page)(struct vm_area_struct *vma, unsigned long page,
3662306a36Sopenharmony_ci	unsigned long pfn);
3762306a36Sopenharmony_civoid (*flush_icache_range)(unsigned long start, unsigned long end);
3862306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(flush_icache_range);
3962306a36Sopenharmony_civoid (*local_flush_icache_range)(unsigned long start, unsigned long end);
4062306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(local_flush_icache_range);
4162306a36Sopenharmony_civoid (*__flush_icache_user_range)(unsigned long start, unsigned long end);
4262306a36Sopenharmony_civoid (*__local_flush_icache_user_range)(unsigned long start, unsigned long end);
4362306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__local_flush_icache_user_range);
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_civoid (*__flush_cache_vmap)(void);
4662306a36Sopenharmony_civoid (*__flush_cache_vunmap)(void);
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_civoid (*__flush_kernel_vmap_range)(unsigned long vaddr, int size);
4962306a36Sopenharmony_ciEXPORT_SYMBOL_GPL(__flush_kernel_vmap_range);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* MIPS specific cache operations */
5262306a36Sopenharmony_civoid (*flush_data_cache_page)(unsigned long addr);
5362306a36Sopenharmony_civoid (*flush_icache_all)(void);
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ciEXPORT_SYMBOL(flush_data_cache_page);
5662306a36Sopenharmony_ciEXPORT_SYMBOL(flush_icache_all);
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci/*
5962306a36Sopenharmony_ci * Dummy cache handling routine
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_civoid cache_noop(void) {}
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#ifdef CONFIG_BOARD_SCACHE
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_cistatic struct bcache_ops no_sc_ops = {
6762306a36Sopenharmony_ci	.bc_enable = (void *)cache_noop,
6862306a36Sopenharmony_ci	.bc_disable = (void *)cache_noop,
6962306a36Sopenharmony_ci	.bc_wback_inv = (void *)cache_noop,
7062306a36Sopenharmony_ci	.bc_inv = (void *)cache_noop
7162306a36Sopenharmony_ci};
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_cistruct bcache_ops *bcops = &no_sc_ops;
7462306a36Sopenharmony_ci#endif
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#ifdef CONFIG_DMA_NONCOHERENT
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci/* DMA cache operations. */
7962306a36Sopenharmony_civoid (*_dma_cache_wback_inv)(unsigned long start, unsigned long size);
8062306a36Sopenharmony_civoid (*_dma_cache_wback)(unsigned long start, unsigned long size);
8162306a36Sopenharmony_civoid (*_dma_cache_inv)(unsigned long start, unsigned long size);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci#endif /* CONFIG_DMA_NONCOHERENT */
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/*
8662306a36Sopenharmony_ci * We could optimize the case where the cache argument is not BCACHE but
8762306a36Sopenharmony_ci * that seems very atypical use ...
8862306a36Sopenharmony_ci */
8962306a36Sopenharmony_ciSYSCALL_DEFINE3(cacheflush, unsigned long, addr, unsigned long, bytes,
9062306a36Sopenharmony_ci	unsigned int, cache)
9162306a36Sopenharmony_ci{
9262306a36Sopenharmony_ci	if (bytes == 0)
9362306a36Sopenharmony_ci		return 0;
9462306a36Sopenharmony_ci	if (!access_ok((void __user *) addr, bytes))
9562306a36Sopenharmony_ci		return -EFAULT;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	__flush_icache_user_range(addr, addr + bytes);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	return 0;
10062306a36Sopenharmony_ci}
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_civoid __flush_dcache_pages(struct page *page, unsigned int nr)
10362306a36Sopenharmony_ci{
10462306a36Sopenharmony_ci	struct folio *folio = page_folio(page);
10562306a36Sopenharmony_ci	struct address_space *mapping = folio_flush_mapping(folio);
10662306a36Sopenharmony_ci	unsigned long addr;
10762306a36Sopenharmony_ci	unsigned int i;
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	if (mapping && !mapping_mapped(mapping)) {
11062306a36Sopenharmony_ci		folio_set_dcache_dirty(folio);
11162306a36Sopenharmony_ci		return;
11262306a36Sopenharmony_ci	}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	/*
11562306a36Sopenharmony_ci	 * We could delay the flush for the !page_mapping case too.  But that
11662306a36Sopenharmony_ci	 * case is for exec env/arg pages and those are %99 certainly going to
11762306a36Sopenharmony_ci	 * get faulted into the tlb (and thus flushed) anyways.
11862306a36Sopenharmony_ci	 */
11962306a36Sopenharmony_ci	for (i = 0; i < nr; i++) {
12062306a36Sopenharmony_ci		addr = (unsigned long)kmap_local_page(nth_page(page, i));
12162306a36Sopenharmony_ci		flush_data_cache_page(addr);
12262306a36Sopenharmony_ci		kunmap_local((void *)addr);
12362306a36Sopenharmony_ci	}
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ciEXPORT_SYMBOL(__flush_dcache_pages);
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_civoid __flush_anon_page(struct page *page, unsigned long vmaddr)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	unsigned long addr = (unsigned long) page_address(page);
13062306a36Sopenharmony_ci	struct folio *folio = page_folio(page);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	if (pages_do_alias(addr, vmaddr)) {
13362306a36Sopenharmony_ci		if (folio_mapped(folio) && !folio_test_dcache_dirty(folio)) {
13462306a36Sopenharmony_ci			void *kaddr;
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci			kaddr = kmap_coherent(page, vmaddr);
13762306a36Sopenharmony_ci			flush_data_cache_page((unsigned long)kaddr);
13862306a36Sopenharmony_ci			kunmap_coherent();
13962306a36Sopenharmony_ci		} else
14062306a36Sopenharmony_ci			flush_data_cache_page(addr);
14162306a36Sopenharmony_ci	}
14262306a36Sopenharmony_ci}
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ciEXPORT_SYMBOL(__flush_anon_page);
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_civoid __update_cache(unsigned long address, pte_t pte)
14762306a36Sopenharmony_ci{
14862306a36Sopenharmony_ci	struct folio *folio;
14962306a36Sopenharmony_ci	unsigned long pfn, addr;
15062306a36Sopenharmony_ci	int exec = !pte_no_exec(pte) && !cpu_has_ic_fills_f_dc;
15162306a36Sopenharmony_ci	unsigned int i;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	pfn = pte_pfn(pte);
15462306a36Sopenharmony_ci	if (unlikely(!pfn_valid(pfn)))
15562306a36Sopenharmony_ci		return;
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	folio = page_folio(pfn_to_page(pfn));
15862306a36Sopenharmony_ci	address &= PAGE_MASK;
15962306a36Sopenharmony_ci	address -= offset_in_folio(folio, pfn << PAGE_SHIFT);
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_ci	if (folio_test_dcache_dirty(folio)) {
16262306a36Sopenharmony_ci		for (i = 0; i < folio_nr_pages(folio); i++) {
16362306a36Sopenharmony_ci			addr = (unsigned long)kmap_local_folio(folio, i);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci			if (exec || pages_do_alias(addr, address))
16662306a36Sopenharmony_ci				flush_data_cache_page(addr);
16762306a36Sopenharmony_ci			kunmap_local((void *)addr);
16862306a36Sopenharmony_ci			address += PAGE_SIZE;
16962306a36Sopenharmony_ci		}
17062306a36Sopenharmony_ci		folio_clear_dcache_dirty(folio);
17162306a36Sopenharmony_ci	}
17262306a36Sopenharmony_ci}
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ciunsigned long _page_cachable_default;
17562306a36Sopenharmony_ciEXPORT_SYMBOL(_page_cachable_default);
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci#define PM(p)	__pgprot(_page_cachable_default | (p))
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_cistatic pgprot_t protection_map[16] __ro_after_init;
18062306a36Sopenharmony_ciDECLARE_VM_GET_PAGE_PROT
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_cistatic inline void setup_protection_map(void)
18362306a36Sopenharmony_ci{
18462306a36Sopenharmony_ci	protection_map[0]  = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
18562306a36Sopenharmony_ci	protection_map[1]  = PM(_PAGE_PRESENT | _PAGE_NO_EXEC);
18662306a36Sopenharmony_ci	protection_map[2]  = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
18762306a36Sopenharmony_ci	protection_map[3]  = PM(_PAGE_PRESENT | _PAGE_NO_EXEC);
18862306a36Sopenharmony_ci	protection_map[4]  = PM(_PAGE_PRESENT);
18962306a36Sopenharmony_ci	protection_map[5]  = PM(_PAGE_PRESENT);
19062306a36Sopenharmony_ci	protection_map[6]  = PM(_PAGE_PRESENT);
19162306a36Sopenharmony_ci	protection_map[7]  = PM(_PAGE_PRESENT);
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	protection_map[8]  = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_NO_READ);
19462306a36Sopenharmony_ci	protection_map[9]  = PM(_PAGE_PRESENT | _PAGE_NO_EXEC);
19562306a36Sopenharmony_ci	protection_map[10] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE |
19662306a36Sopenharmony_ci				_PAGE_NO_READ);
19762306a36Sopenharmony_ci	protection_map[11] = PM(_PAGE_PRESENT | _PAGE_NO_EXEC | _PAGE_WRITE);
19862306a36Sopenharmony_ci	protection_map[12] = PM(_PAGE_PRESENT);
19962306a36Sopenharmony_ci	protection_map[13] = PM(_PAGE_PRESENT);
20062306a36Sopenharmony_ci	protection_map[14] = PM(_PAGE_PRESENT | _PAGE_WRITE);
20162306a36Sopenharmony_ci	protection_map[15] = PM(_PAGE_PRESENT | _PAGE_WRITE);
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci#undef PM
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_civoid cpu_cache_init(void)
20762306a36Sopenharmony_ci{
20862306a36Sopenharmony_ci	if (cpu_has_3k_cache) {
20962306a36Sopenharmony_ci		extern void __weak r3k_cache_init(void);
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci		r3k_cache_init();
21262306a36Sopenharmony_ci	}
21362306a36Sopenharmony_ci	if (cpu_has_4k_cache) {
21462306a36Sopenharmony_ci		extern void __weak r4k_cache_init(void);
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci		r4k_cache_init();
21762306a36Sopenharmony_ci	}
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	if (cpu_has_octeon_cache) {
22062306a36Sopenharmony_ci		extern void __weak octeon_cache_init(void);
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci		octeon_cache_init();
22362306a36Sopenharmony_ci	}
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	setup_protection_map();
22662306a36Sopenharmony_ci}
227