18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  arch/arm/include/asm/cacheflush.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (C) 1999-2002 Russell King
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci#ifndef _ASMARM_CACHEFLUSH_H
88c2ecf20Sopenharmony_ci#define _ASMARM_CACHEFLUSH_H
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/mm.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <asm/glue-cache.h>
138c2ecf20Sopenharmony_ci#include <asm/shmparam.h>
148c2ecf20Sopenharmony_ci#include <asm/cachetype.h>
158c2ecf20Sopenharmony_ci#include <asm/outercache.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define CACHE_COLOUR(vaddr)	((vaddr & (SHMLBA - 1)) >> PAGE_SHIFT)
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/*
208c2ecf20Sopenharmony_ci * This flag is used to indicate that the page pointed to by a pte is clean
218c2ecf20Sopenharmony_ci * and does not require cleaning before returning it to the user.
228c2ecf20Sopenharmony_ci */
238c2ecf20Sopenharmony_ci#define PG_dcache_clean PG_arch_1
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci *	MM Cache Management
278c2ecf20Sopenharmony_ci *	===================
288c2ecf20Sopenharmony_ci *
298c2ecf20Sopenharmony_ci *	The arch/arm/mm/cache-*.S and arch/arm/mm/proc-*.S files
308c2ecf20Sopenharmony_ci *	implement these methods.
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci *	Start addresses are inclusive and end addresses are exclusive;
338c2ecf20Sopenharmony_ci *	start addresses should be rounded down, end addresses up.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci *	See Documentation/core-api/cachetlb.rst for more information.
368c2ecf20Sopenharmony_ci *	Please note that the implementation of these, and the required
378c2ecf20Sopenharmony_ci *	effects are cache-type (VIVT/VIPT/PIPT) specific.
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci *	flush_icache_all()
408c2ecf20Sopenharmony_ci *
418c2ecf20Sopenharmony_ci *		Unconditionally clean and invalidate the entire icache.
428c2ecf20Sopenharmony_ci *		Currently only needed for cache-v6.S and cache-v7.S, see
438c2ecf20Sopenharmony_ci *		__flush_icache_all for the generic implementation.
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci *	flush_kern_all()
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci *		Unconditionally clean and invalidate the entire cache.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci *     flush_kern_louis()
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci *             Flush data cache levels up to the level of unification
528c2ecf20Sopenharmony_ci *             inner shareable and invalidate the I-cache.
538c2ecf20Sopenharmony_ci *             Only needed from v7 onwards, falls back to flush_cache_all()
548c2ecf20Sopenharmony_ci *             for all other processor versions.
558c2ecf20Sopenharmony_ci *
568c2ecf20Sopenharmony_ci *	flush_user_all()
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci *		Clean and invalidate all user space cache entries
598c2ecf20Sopenharmony_ci *		before a change of page tables.
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci *	flush_user_range(start, end, flags)
628c2ecf20Sopenharmony_ci *
638c2ecf20Sopenharmony_ci *		Clean and invalidate a range of cache entries in the
648c2ecf20Sopenharmony_ci *		specified address space before a change of page tables.
658c2ecf20Sopenharmony_ci *		- start - user start address (inclusive, page aligned)
668c2ecf20Sopenharmony_ci *		- end   - user end address   (exclusive, page aligned)
678c2ecf20Sopenharmony_ci *		- flags - vma->vm_flags field
688c2ecf20Sopenharmony_ci *
698c2ecf20Sopenharmony_ci *	coherent_kern_range(start, end)
708c2ecf20Sopenharmony_ci *
718c2ecf20Sopenharmony_ci *		Ensure coherency between the Icache and the Dcache in the
728c2ecf20Sopenharmony_ci *		region described by start, end.  If you have non-snooping
738c2ecf20Sopenharmony_ci *		Harvard caches, you need to implement this function.
748c2ecf20Sopenharmony_ci *		- start  - virtual start address
758c2ecf20Sopenharmony_ci *		- end    - virtual end address
768c2ecf20Sopenharmony_ci *
778c2ecf20Sopenharmony_ci *	coherent_user_range(start, end)
788c2ecf20Sopenharmony_ci *
798c2ecf20Sopenharmony_ci *		Ensure coherency between the Icache and the Dcache in the
808c2ecf20Sopenharmony_ci *		region described by start, end.  If you have non-snooping
818c2ecf20Sopenharmony_ci *		Harvard caches, you need to implement this function.
828c2ecf20Sopenharmony_ci *		- start  - virtual start address
838c2ecf20Sopenharmony_ci *		- end    - virtual end address
848c2ecf20Sopenharmony_ci *
858c2ecf20Sopenharmony_ci *	flush_kern_dcache_area(kaddr, size)
868c2ecf20Sopenharmony_ci *
878c2ecf20Sopenharmony_ci *		Ensure that the data held in page is written back.
888c2ecf20Sopenharmony_ci *		- kaddr  - page address
898c2ecf20Sopenharmony_ci *		- size   - region size
908c2ecf20Sopenharmony_ci *
918c2ecf20Sopenharmony_ci *	DMA Cache Coherency
928c2ecf20Sopenharmony_ci *	===================
938c2ecf20Sopenharmony_ci *
948c2ecf20Sopenharmony_ci *	dma_flush_range(start, end)
958c2ecf20Sopenharmony_ci *
968c2ecf20Sopenharmony_ci *		Clean and invalidate the specified virtual address range.
978c2ecf20Sopenharmony_ci *		- start  - virtual start address
988c2ecf20Sopenharmony_ci *		- end    - virtual end address
998c2ecf20Sopenharmony_ci */
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistruct cpu_cache_fns {
1028c2ecf20Sopenharmony_ci	void (*flush_icache_all)(void);
1038c2ecf20Sopenharmony_ci	void (*flush_kern_all)(void);
1048c2ecf20Sopenharmony_ci	void (*flush_kern_louis)(void);
1058c2ecf20Sopenharmony_ci	void (*flush_user_all)(void);
1068c2ecf20Sopenharmony_ci	void (*flush_user_range)(unsigned long, unsigned long, unsigned int);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	void (*coherent_kern_range)(unsigned long, unsigned long);
1098c2ecf20Sopenharmony_ci	int  (*coherent_user_range)(unsigned long, unsigned long);
1108c2ecf20Sopenharmony_ci	void (*flush_kern_dcache_area)(void *, size_t);
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	void (*dma_map_area)(const void *, size_t, int);
1138c2ecf20Sopenharmony_ci	void (*dma_unmap_area)(const void *, size_t, int);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	void (*dma_flush_range)(const void *, const void *);
1168c2ecf20Sopenharmony_ci} __no_randomize_layout;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci/*
1198c2ecf20Sopenharmony_ci * Select the calling method
1208c2ecf20Sopenharmony_ci */
1218c2ecf20Sopenharmony_ci#ifdef MULTI_CACHE
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ciextern struct cpu_cache_fns cpu_cache;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#define __cpuc_flush_icache_all		cpu_cache.flush_icache_all
1268c2ecf20Sopenharmony_ci#define __cpuc_flush_kern_all		cpu_cache.flush_kern_all
1278c2ecf20Sopenharmony_ci#define __cpuc_flush_kern_louis		cpu_cache.flush_kern_louis
1288c2ecf20Sopenharmony_ci#define __cpuc_flush_user_all		cpu_cache.flush_user_all
1298c2ecf20Sopenharmony_ci#define __cpuc_flush_user_range		cpu_cache.flush_user_range
1308c2ecf20Sopenharmony_ci#define __cpuc_coherent_kern_range	cpu_cache.coherent_kern_range
1318c2ecf20Sopenharmony_ci#define __cpuc_coherent_user_range	cpu_cache.coherent_user_range
1328c2ecf20Sopenharmony_ci#define __cpuc_flush_dcache_area	cpu_cache.flush_kern_dcache_area
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/*
1358c2ecf20Sopenharmony_ci * These are private to the dma-mapping API.  Do not use directly.
1368c2ecf20Sopenharmony_ci * Their sole purpose is to ensure that data held in the cache
1378c2ecf20Sopenharmony_ci * is visible to DMA, or data written by DMA to system memory is
1388c2ecf20Sopenharmony_ci * visible to the CPU.
1398c2ecf20Sopenharmony_ci */
1408c2ecf20Sopenharmony_ci#define dmac_flush_range		cpu_cache.dma_flush_range
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci#else
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ciextern void __cpuc_flush_icache_all(void);
1458c2ecf20Sopenharmony_ciextern void __cpuc_flush_kern_all(void);
1468c2ecf20Sopenharmony_ciextern void __cpuc_flush_kern_louis(void);
1478c2ecf20Sopenharmony_ciextern void __cpuc_flush_user_all(void);
1488c2ecf20Sopenharmony_ciextern void __cpuc_flush_user_range(unsigned long, unsigned long, unsigned int);
1498c2ecf20Sopenharmony_ciextern void __cpuc_coherent_kern_range(unsigned long, unsigned long);
1508c2ecf20Sopenharmony_ciextern int  __cpuc_coherent_user_range(unsigned long, unsigned long);
1518c2ecf20Sopenharmony_ciextern void __cpuc_flush_dcache_area(void *, size_t);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/*
1548c2ecf20Sopenharmony_ci * These are private to the dma-mapping API.  Do not use directly.
1558c2ecf20Sopenharmony_ci * Their sole purpose is to ensure that data held in the cache
1568c2ecf20Sopenharmony_ci * is visible to DMA, or data written by DMA to system memory is
1578c2ecf20Sopenharmony_ci * visible to the CPU.
1588c2ecf20Sopenharmony_ci */
1598c2ecf20Sopenharmony_ciextern void dmac_flush_range(const void *, const void *);
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci#endif
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci/*
1648c2ecf20Sopenharmony_ci * Copy user data from/to a page which is mapped into a different
1658c2ecf20Sopenharmony_ci * processes address space.  Really, we want to allow our "user
1668c2ecf20Sopenharmony_ci * space" model to handle this.
1678c2ecf20Sopenharmony_ci */
1688c2ecf20Sopenharmony_ciextern void copy_to_user_page(struct vm_area_struct *, struct page *,
1698c2ecf20Sopenharmony_ci	unsigned long, void *, const void *, unsigned long);
1708c2ecf20Sopenharmony_ci#define copy_from_user_page(vma, page, vaddr, dst, src, len) \
1718c2ecf20Sopenharmony_ci	do {							\
1728c2ecf20Sopenharmony_ci		memcpy(dst, src, len);				\
1738c2ecf20Sopenharmony_ci	} while (0)
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/*
1768c2ecf20Sopenharmony_ci * Convert calls to our calling convention.
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci/* Invalidate I-cache */
1808c2ecf20Sopenharmony_ci#define __flush_icache_all_generic()					\
1818c2ecf20Sopenharmony_ci	asm("mcr	p15, 0, %0, c7, c5, 0"				\
1828c2ecf20Sopenharmony_ci	    : : "r" (0));
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci/* Invalidate I-cache inner shareable */
1858c2ecf20Sopenharmony_ci#define __flush_icache_all_v7_smp()					\
1868c2ecf20Sopenharmony_ci	asm("mcr	p15, 0, %0, c7, c1, 0"				\
1878c2ecf20Sopenharmony_ci	    : : "r" (0));
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci/*
1908c2ecf20Sopenharmony_ci * Optimized __flush_icache_all for the common cases. Note that UP ARMv7
1918c2ecf20Sopenharmony_ci * will fall through to use __flush_icache_all_generic.
1928c2ecf20Sopenharmony_ci */
1938c2ecf20Sopenharmony_ci#if (defined(CONFIG_CPU_V7) && \
1948c2ecf20Sopenharmony_ci     (defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K))) || \
1958c2ecf20Sopenharmony_ci	defined(CONFIG_SMP_ON_UP)
1968c2ecf20Sopenharmony_ci#define __flush_icache_preferred	__cpuc_flush_icache_all
1978c2ecf20Sopenharmony_ci#elif __LINUX_ARM_ARCH__ >= 7 && defined(CONFIG_SMP)
1988c2ecf20Sopenharmony_ci#define __flush_icache_preferred	__flush_icache_all_v7_smp
1998c2ecf20Sopenharmony_ci#elif __LINUX_ARM_ARCH__ == 6 && defined(CONFIG_ARM_ERRATA_411920)
2008c2ecf20Sopenharmony_ci#define __flush_icache_preferred	__cpuc_flush_icache_all
2018c2ecf20Sopenharmony_ci#else
2028c2ecf20Sopenharmony_ci#define __flush_icache_preferred	__flush_icache_all_generic
2038c2ecf20Sopenharmony_ci#endif
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_cistatic inline void __flush_icache_all(void)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	__flush_icache_preferred();
2088c2ecf20Sopenharmony_ci	dsb(ishst);
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci/*
2128c2ecf20Sopenharmony_ci * Flush caches up to Level of Unification Inner Shareable
2138c2ecf20Sopenharmony_ci */
2148c2ecf20Sopenharmony_ci#define flush_cache_louis()		__cpuc_flush_kern_louis()
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci#define flush_cache_all()		__cpuc_flush_kern_all()
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic inline void vivt_flush_cache_mm(struct mm_struct *mm)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
2218c2ecf20Sopenharmony_ci		__cpuc_flush_user_all();
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic inline void
2258c2ecf20Sopenharmony_civivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct mm_struct *mm = vma->vm_mm;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
2308c2ecf20Sopenharmony_ci		__cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
2318c2ecf20Sopenharmony_ci					vma->vm_flags);
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic inline void
2358c2ecf20Sopenharmony_civivt_flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	struct mm_struct *mm = vma->vm_mm;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
2408c2ecf20Sopenharmony_ci		unsigned long addr = user_addr & PAGE_MASK;
2418c2ecf20Sopenharmony_ci		__cpuc_flush_user_range(addr, addr + PAGE_SIZE, vma->vm_flags);
2428c2ecf20Sopenharmony_ci	}
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci#ifndef CONFIG_CPU_CACHE_VIPT
2468c2ecf20Sopenharmony_ci#define flush_cache_mm(mm) \
2478c2ecf20Sopenharmony_ci		vivt_flush_cache_mm(mm)
2488c2ecf20Sopenharmony_ci#define flush_cache_range(vma,start,end) \
2498c2ecf20Sopenharmony_ci		vivt_flush_cache_range(vma,start,end)
2508c2ecf20Sopenharmony_ci#define flush_cache_page(vma,addr,pfn) \
2518c2ecf20Sopenharmony_ci		vivt_flush_cache_page(vma,addr,pfn)
2528c2ecf20Sopenharmony_ci#else
2538c2ecf20Sopenharmony_ciextern void flush_cache_mm(struct mm_struct *mm);
2548c2ecf20Sopenharmony_ciextern void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
2558c2ecf20Sopenharmony_ciextern void flush_cache_page(struct vm_area_struct *vma, unsigned long user_addr, unsigned long pfn);
2568c2ecf20Sopenharmony_ci#endif
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci#define flush_cache_dup_mm(mm) flush_cache_mm(mm)
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/*
2618c2ecf20Sopenharmony_ci * flush_icache_user_range is used when we want to ensure that the
2628c2ecf20Sopenharmony_ci * Harvard caches are synchronised for the user space address range.
2638c2ecf20Sopenharmony_ci * This is used for the ARM private sys_cacheflush system call.
2648c2ecf20Sopenharmony_ci */
2658c2ecf20Sopenharmony_ci#define flush_icache_user_range(s,e)	__cpuc_coherent_user_range(s,e)
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/*
2688c2ecf20Sopenharmony_ci * Perform necessary cache operations to ensure that data previously
2698c2ecf20Sopenharmony_ci * stored within this range of addresses can be executed by the CPU.
2708c2ecf20Sopenharmony_ci */
2718c2ecf20Sopenharmony_ci#define flush_icache_range(s,e)		__cpuc_coherent_kern_range(s,e)
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci/*
2748c2ecf20Sopenharmony_ci * Perform necessary cache operations to ensure that the TLB will
2758c2ecf20Sopenharmony_ci * see data written in the specified area.
2768c2ecf20Sopenharmony_ci */
2778c2ecf20Sopenharmony_ci#define clean_dcache_area(start,size)	cpu_dcache_clean_area(start, size)
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci/*
2808c2ecf20Sopenharmony_ci * flush_dcache_page is used when the kernel has written to the page
2818c2ecf20Sopenharmony_ci * cache page at virtual address page->virtual.
2828c2ecf20Sopenharmony_ci *
2838c2ecf20Sopenharmony_ci * If this page isn't mapped (ie, page_mapping == NULL), or it might
2848c2ecf20Sopenharmony_ci * have userspace mappings, then we _must_ always clean + invalidate
2858c2ecf20Sopenharmony_ci * the dcache entries associated with the kernel mapping.
2868c2ecf20Sopenharmony_ci *
2878c2ecf20Sopenharmony_ci * Otherwise we can defer the operation, and clean the cache when we are
2888c2ecf20Sopenharmony_ci * about to change to user space.  This is the same method as used on SPARC64.
2898c2ecf20Sopenharmony_ci * See update_mmu_cache for the user space part.
2908c2ecf20Sopenharmony_ci */
2918c2ecf20Sopenharmony_ci#define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
2928c2ecf20Sopenharmony_ciextern void flush_dcache_page(struct page *);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_cistatic inline void flush_kernel_vmap_range(void *addr, int size)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	if ((cache_is_vivt() || cache_is_vipt_aliasing()))
2978c2ecf20Sopenharmony_ci	  __cpuc_flush_dcache_area(addr, (size_t)size);
2988c2ecf20Sopenharmony_ci}
2998c2ecf20Sopenharmony_cistatic inline void invalidate_kernel_vmap_range(void *addr, int size)
3008c2ecf20Sopenharmony_ci{
3018c2ecf20Sopenharmony_ci	if ((cache_is_vivt() || cache_is_vipt_aliasing()))
3028c2ecf20Sopenharmony_ci	  __cpuc_flush_dcache_area(addr, (size_t)size);
3038c2ecf20Sopenharmony_ci}
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci#define ARCH_HAS_FLUSH_ANON_PAGE
3068c2ecf20Sopenharmony_cistatic inline void flush_anon_page(struct vm_area_struct *vma,
3078c2ecf20Sopenharmony_ci			 struct page *page, unsigned long vmaddr)
3088c2ecf20Sopenharmony_ci{
3098c2ecf20Sopenharmony_ci	extern void __flush_anon_page(struct vm_area_struct *vma,
3108c2ecf20Sopenharmony_ci				struct page *, unsigned long);
3118c2ecf20Sopenharmony_ci	if (PageAnon(page))
3128c2ecf20Sopenharmony_ci		__flush_anon_page(vma, page, vmaddr);
3138c2ecf20Sopenharmony_ci}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci#define ARCH_HAS_FLUSH_KERNEL_DCACHE_PAGE
3168c2ecf20Sopenharmony_ciextern void flush_kernel_dcache_page(struct page *);
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci#define flush_dcache_mmap_lock(mapping)		xa_lock_irq(&mapping->i_pages)
3198c2ecf20Sopenharmony_ci#define flush_dcache_mmap_unlock(mapping)	xa_unlock_irq(&mapping->i_pages)
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci/*
3228c2ecf20Sopenharmony_ci * We don't appear to need to do anything here.  In fact, if we did, we'd
3238c2ecf20Sopenharmony_ci * duplicate cache flushing elsewhere performed by flush_dcache_page().
3248c2ecf20Sopenharmony_ci */
3258c2ecf20Sopenharmony_ci#define flush_icache_page(vma,page)	do { } while (0)
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci/*
3288c2ecf20Sopenharmony_ci * flush_cache_vmap() is used when creating mappings (eg, via vmap,
3298c2ecf20Sopenharmony_ci * vmalloc, ioremap etc) in kernel space for pages.  On non-VIPT
3308c2ecf20Sopenharmony_ci * caches, since the direct-mappings of these pages may contain cached
3318c2ecf20Sopenharmony_ci * data, we need to do a full cache flush to ensure that writebacks
3328c2ecf20Sopenharmony_ci * don't corrupt data placed into these pages via the new mappings.
3338c2ecf20Sopenharmony_ci */
3348c2ecf20Sopenharmony_cistatic inline void flush_cache_vmap(unsigned long start, unsigned long end)
3358c2ecf20Sopenharmony_ci{
3368c2ecf20Sopenharmony_ci	if (!cache_is_vipt_nonaliasing())
3378c2ecf20Sopenharmony_ci		flush_cache_all();
3388c2ecf20Sopenharmony_ci	else
3398c2ecf20Sopenharmony_ci		/*
3408c2ecf20Sopenharmony_ci		 * set_pte_at() called from vmap_pte_range() does not
3418c2ecf20Sopenharmony_ci		 * have a DSB after cleaning the cache line.
3428c2ecf20Sopenharmony_ci		 */
3438c2ecf20Sopenharmony_ci		dsb(ishst);
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistatic inline void flush_cache_vunmap(unsigned long start, unsigned long end)
3478c2ecf20Sopenharmony_ci{
3488c2ecf20Sopenharmony_ci	if (!cache_is_vipt_nonaliasing())
3498c2ecf20Sopenharmony_ci		flush_cache_all();
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci/*
3538c2ecf20Sopenharmony_ci * Memory synchronization helpers for mixed cached vs non cached accesses.
3548c2ecf20Sopenharmony_ci *
3558c2ecf20Sopenharmony_ci * Some synchronization algorithms have to set states in memory with the
3568c2ecf20Sopenharmony_ci * cache enabled or disabled depending on the code path.  It is crucial
3578c2ecf20Sopenharmony_ci * to always ensure proper cache maintenance to update main memory right
3588c2ecf20Sopenharmony_ci * away in that case.
3598c2ecf20Sopenharmony_ci *
3608c2ecf20Sopenharmony_ci * Any cached write must be followed by a cache clean operation.
3618c2ecf20Sopenharmony_ci * Any cached read must be preceded by a cache invalidate operation.
3628c2ecf20Sopenharmony_ci * Yet, in the read case, a cache flush i.e. atomic clean+invalidate
3638c2ecf20Sopenharmony_ci * operation is needed to avoid discarding possible concurrent writes to the
3648c2ecf20Sopenharmony_ci * accessed memory.
3658c2ecf20Sopenharmony_ci *
3668c2ecf20Sopenharmony_ci * Also, in order to prevent a cached writer from interfering with an
3678c2ecf20Sopenharmony_ci * adjacent non-cached writer, each state variable must be located to
3688c2ecf20Sopenharmony_ci * a separate cache line.
3698c2ecf20Sopenharmony_ci */
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci/*
3728c2ecf20Sopenharmony_ci * This needs to be >= the max cache writeback size of all
3738c2ecf20Sopenharmony_ci * supported platforms included in the current kernel configuration.
3748c2ecf20Sopenharmony_ci * This is used to align state variables to their own cache lines.
3758c2ecf20Sopenharmony_ci */
3768c2ecf20Sopenharmony_ci#define __CACHE_WRITEBACK_ORDER 6  /* guessed from existing platforms */
3778c2ecf20Sopenharmony_ci#define __CACHE_WRITEBACK_GRANULE (1 << __CACHE_WRITEBACK_ORDER)
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci/*
3808c2ecf20Sopenharmony_ci * There is no __cpuc_clean_dcache_area but we use it anyway for
3818c2ecf20Sopenharmony_ci * code intent clarity, and alias it to __cpuc_flush_dcache_area.
3828c2ecf20Sopenharmony_ci */
3838c2ecf20Sopenharmony_ci#define __cpuc_clean_dcache_area __cpuc_flush_dcache_area
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci/*
3868c2ecf20Sopenharmony_ci * Ensure preceding writes to *p by this CPU are visible to
3878c2ecf20Sopenharmony_ci * subsequent reads by other CPUs:
3888c2ecf20Sopenharmony_ci */
3898c2ecf20Sopenharmony_cistatic inline void __sync_cache_range_w(volatile void *p, size_t size)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	char *_p = (char *)p;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	__cpuc_clean_dcache_area(_p, size);
3948c2ecf20Sopenharmony_ci	outer_clean_range(__pa(_p), __pa(_p + size));
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci/*
3988c2ecf20Sopenharmony_ci * Ensure preceding writes to *p by other CPUs are visible to
3998c2ecf20Sopenharmony_ci * subsequent reads by this CPU.  We must be careful not to
4008c2ecf20Sopenharmony_ci * discard data simultaneously written by another CPU, hence the
4018c2ecf20Sopenharmony_ci * usage of flush rather than invalidate operations.
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_cistatic inline void __sync_cache_range_r(volatile void *p, size_t size)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	char *_p = (char *)p;
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci#ifdef CONFIG_OUTER_CACHE
4088c2ecf20Sopenharmony_ci	if (outer_cache.flush_range) {
4098c2ecf20Sopenharmony_ci		/*
4108c2ecf20Sopenharmony_ci		 * Ensure dirty data migrated from other CPUs into our cache
4118c2ecf20Sopenharmony_ci		 * are cleaned out safely before the outer cache is cleaned:
4128c2ecf20Sopenharmony_ci		 */
4138c2ecf20Sopenharmony_ci		__cpuc_clean_dcache_area(_p, size);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci		/* Clean and invalidate stale data for *p from outer ... */
4168c2ecf20Sopenharmony_ci		outer_flush_range(__pa(_p), __pa(_p + size));
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci#endif
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	/* ... and inner cache: */
4218c2ecf20Sopenharmony_ci	__cpuc_flush_dcache_area(_p, size);
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_ci#define sync_cache_w(ptr) __sync_cache_range_w(ptr, sizeof *(ptr))
4258c2ecf20Sopenharmony_ci#define sync_cache_r(ptr) __sync_cache_range_r(ptr, sizeof *(ptr))
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci/*
4288c2ecf20Sopenharmony_ci * Disabling cache access for one CPU in an ARMv7 SMP system is tricky.
4298c2ecf20Sopenharmony_ci * To do so we must:
4308c2ecf20Sopenharmony_ci *
4318c2ecf20Sopenharmony_ci * - Clear the SCTLR.C bit to prevent further cache allocations
4328c2ecf20Sopenharmony_ci * - Flush the desired level of cache
4338c2ecf20Sopenharmony_ci * - Clear the ACTLR "SMP" bit to disable local coherency
4348c2ecf20Sopenharmony_ci *
4358c2ecf20Sopenharmony_ci * ... and so without any intervening memory access in between those steps,
4368c2ecf20Sopenharmony_ci * not even to the stack.
4378c2ecf20Sopenharmony_ci *
4388c2ecf20Sopenharmony_ci * WARNING -- After this has been called:
4398c2ecf20Sopenharmony_ci *
4408c2ecf20Sopenharmony_ci * - No ldrex/strex (and similar) instructions must be used.
4418c2ecf20Sopenharmony_ci * - The CPU is obviously no longer coherent with the other CPUs.
4428c2ecf20Sopenharmony_ci * - This is unlikely to work as expected if Linux is running non-secure.
4438c2ecf20Sopenharmony_ci *
4448c2ecf20Sopenharmony_ci * Note:
4458c2ecf20Sopenharmony_ci *
4468c2ecf20Sopenharmony_ci * - This is known to apply to several ARMv7 processor implementations,
4478c2ecf20Sopenharmony_ci *   however some exceptions may exist.  Caveat emptor.
4488c2ecf20Sopenharmony_ci *
4498c2ecf20Sopenharmony_ci * - The clobber list is dictated by the call to v7_flush_dcache_*.
4508c2ecf20Sopenharmony_ci *   fp is preserved to the stack explicitly prior disabling the cache
4518c2ecf20Sopenharmony_ci *   since adding it to the clobber list is incompatible with having
4528c2ecf20Sopenharmony_ci *   CONFIG_FRAME_POINTER=y.  ip is saved as well if ever r12-clobbering
4538c2ecf20Sopenharmony_ci *   trampoline are inserted by the linker and to keep sp 64-bit aligned.
4548c2ecf20Sopenharmony_ci */
4558c2ecf20Sopenharmony_ci#define v7_exit_coherency_flush(level) \
4568c2ecf20Sopenharmony_ci	asm volatile( \
4578c2ecf20Sopenharmony_ci	".arch	armv7-a \n\t" \
4588c2ecf20Sopenharmony_ci	"stmfd	sp!, {fp, ip} \n\t" \
4598c2ecf20Sopenharmony_ci	"mrc	p15, 0, r0, c1, c0, 0	@ get SCTLR \n\t" \
4608c2ecf20Sopenharmony_ci	"bic	r0, r0, #"__stringify(CR_C)" \n\t" \
4618c2ecf20Sopenharmony_ci	"mcr	p15, 0, r0, c1, c0, 0	@ set SCTLR \n\t" \
4628c2ecf20Sopenharmony_ci	"isb	\n\t" \
4638c2ecf20Sopenharmony_ci	"bl	v7_flush_dcache_"__stringify(level)" \n\t" \
4648c2ecf20Sopenharmony_ci	"mrc	p15, 0, r0, c1, c0, 1	@ get ACTLR \n\t" \
4658c2ecf20Sopenharmony_ci	"bic	r0, r0, #(1 << 6)	@ disable local coherency \n\t" \
4668c2ecf20Sopenharmony_ci	"mcr	p15, 0, r0, c1, c0, 1	@ set ACTLR \n\t" \
4678c2ecf20Sopenharmony_ci	"isb	\n\t" \
4688c2ecf20Sopenharmony_ci	"dsb	\n\t" \
4698c2ecf20Sopenharmony_ci	"ldmfd	sp!, {fp, ip}" \
4708c2ecf20Sopenharmony_ci	: : : "r0","r1","r2","r3","r4","r5","r6","r7", \
4718c2ecf20Sopenharmony_ci	      "r9","r10","lr","memory" )
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_civoid flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
4748c2ecf20Sopenharmony_ci			     void *kaddr, unsigned long len);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci#ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
4788c2ecf20Sopenharmony_civoid check_cpu_icache_size(int cpuid);
4798c2ecf20Sopenharmony_ci#else
4808c2ecf20Sopenharmony_cistatic inline void check_cpu_icache_size(int cpuid) { }
4818c2ecf20Sopenharmony_ci#endif
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci#endif
484