xref: /openbmc/linux/arch/arm/include/asm/cacheflush.h (revision 695c312ec5a68e4373d063ee649c7b925ffb5da7)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
24baa9922SRussell King /*
34baa9922SRussell King  *  arch/arm/include/asm/cacheflush.h
44baa9922SRussell King  *
54baa9922SRussell King  *  Copyright (C) 1999-2002 Russell King
64baa9922SRussell King  */
74baa9922SRussell King #ifndef _ASMARM_CACHEFLUSH_H
84baa9922SRussell King #define _ASMARM_CACHEFLUSH_H
94baa9922SRussell King 
104baa9922SRussell King #include <linux/mm.h>
114baa9922SRussell King 
12753790e7SRussell King #include <asm/glue-cache.h>
134baa9922SRussell King #include <asm/shmparam.h>
14376e1421SCatalin Marinas #include <asm/cachetype.h>
1533f663ffSCatalin Marinas #include <asm/outercache.h>
164baa9922SRussell King 
174baa9922SRussell King #define CACHE_COLOUR(vaddr)	((vaddr & (SHMLBA - 1)) >> PAGE_SHIFT)
184baa9922SRussell King 
194baa9922SRussell King /*
20c0177800SCatalin Marinas  * This flag is used to indicate that the page pointed to by a pte is clean
21c0177800SCatalin Marinas  * and does not require cleaning before returning it to the user.
224baa9922SRussell King  */
23c0177800SCatalin Marinas #define PG_dcache_clean PG_arch_1
244baa9922SRussell King 
254baa9922SRussell King /*
264baa9922SRussell King  *	MM Cache Management
274baa9922SRussell King  *	===================
284baa9922SRussell King  *
294baa9922SRussell King  *	The arch/arm/mm/cache-*.S and arch/arm/mm/proc-*.S files
304baa9922SRussell King  *	implement these methods.
314baa9922SRussell King  *
324baa9922SRussell King  *	Start addresses are inclusive and end addresses are exclusive;
334baa9922SRussell King  *	start addresses should be rounded down, end addresses up.
344baa9922SRussell King  *
355fb94e9cSMauro Carvalho Chehab  *	See Documentation/core-api/cachetlb.rst for more information.
364baa9922SRussell King  *	Please note that the implementation of these, and the required
374baa9922SRussell King  *	effects are cache-type (VIVT/VIPT/PIPT) specific.
384baa9922SRussell King  *
3981d11955STony Lindgren  *	flush_icache_all()
4081d11955STony Lindgren  *
4181d11955STony Lindgren  *		Unconditionally clean and invalidate the entire icache.
4281d11955STony Lindgren  *		Currently only needed for cache-v6.S and cache-v7.S, see
4381d11955STony Lindgren  *		__flush_icache_all for the generic implementation.
4481d11955STony Lindgren  *
452045124fSTony Lindgren  *	flush_kern_all()
464baa9922SRussell King  *
474baa9922SRussell King  *		Unconditionally clean and invalidate the entire cache.
484baa9922SRussell King  *
49031bd879SLorenzo Pieralisi  *     flush_kern_louis()
50031bd879SLorenzo Pieralisi  *
51031bd879SLorenzo Pieralisi  *             Flush data cache levels up to the level of unification
52031bd879SLorenzo Pieralisi  *             inner shareable and invalidate the I-cache.
53031bd879SLorenzo Pieralisi  *             Only needed from v7 onwards, falls back to flush_cache_all()
54031bd879SLorenzo Pieralisi  *             for all other processor versions.
55031bd879SLorenzo Pieralisi  *
562045124fSTony Lindgren  *	flush_user_all()
574baa9922SRussell King  *
584baa9922SRussell King  *		Clean and invalidate all user space cache entries
594baa9922SRussell King  *		before a change of page tables.
604baa9922SRussell King  *
612045124fSTony Lindgren  *	flush_user_range(start, end, flags)
624baa9922SRussell King  *
634baa9922SRussell King  *		Clean and invalidate a range of cache entries in the
644baa9922SRussell King  *		specified address space before a change of page tables.
654baa9922SRussell King  *		- start - user start address (inclusive, page aligned)
664baa9922SRussell King  *		- end   - user end address   (exclusive, page aligned)
674baa9922SRussell King  *		- flags - vma->vm_flags field
684baa9922SRussell King  *
694baa9922SRussell King  *	coherent_kern_range(start, end)
704baa9922SRussell King  *
714baa9922SRussell King  *		Ensure coherency between the Icache and the Dcache in the
724baa9922SRussell King  *		region described by start, end.  If you have non-snooping
734baa9922SRussell King  *		Harvard caches, you need to implement this function.
744baa9922SRussell King  *		- start  - virtual start address
754baa9922SRussell King  *		- end    - virtual end address
764baa9922SRussell King  *
772045124fSTony Lindgren  *	coherent_user_range(start, end)
782045124fSTony Lindgren  *
792045124fSTony Lindgren  *		Ensure coherency between the Icache and the Dcache in the
802045124fSTony Lindgren  *		region described by start, end.  If you have non-snooping
812045124fSTony Lindgren  *		Harvard caches, you need to implement this function.
822045124fSTony Lindgren  *		- start  - virtual start address
832045124fSTony Lindgren  *		- end    - virtual end address
842045124fSTony Lindgren  *
852045124fSTony Lindgren  *	flush_kern_dcache_area(kaddr, size)
862045124fSTony Lindgren  *
872045124fSTony Lindgren  *		Ensure that the data held in page is written back.
882045124fSTony Lindgren  *		- kaddr  - page address
892045124fSTony Lindgren  *		- size   - region size
902045124fSTony Lindgren  *
914baa9922SRussell King  *	DMA Cache Coherency
924baa9922SRussell King  *	===================
934baa9922SRussell King  *
944baa9922SRussell King  *	dma_flush_range(start, end)
954baa9922SRussell King  *
964baa9922SRussell King  *		Clean and invalidate the specified virtual address range.
974baa9922SRussell King  *		- start  - virtual start address
984baa9922SRussell King  *		- end    - virtual end address
994baa9922SRussell King  */
1004baa9922SRussell King 
1014baa9922SRussell King struct cpu_cache_fns {
10281d11955STony Lindgren 	void (*flush_icache_all)(void);
1034baa9922SRussell King 	void (*flush_kern_all)(void);
104031bd879SLorenzo Pieralisi 	void (*flush_kern_louis)(void);
1054baa9922SRussell King 	void (*flush_user_all)(void);
1064baa9922SRussell King 	void (*flush_user_range)(unsigned long, unsigned long, unsigned int);
1074baa9922SRussell King 
1084baa9922SRussell King 	void (*coherent_kern_range)(unsigned long, unsigned long);
109c5102f59SWill Deacon 	int  (*coherent_user_range)(unsigned long, unsigned long);
1102c9b9c84SRussell King 	void (*flush_kern_dcache_area)(void *, size_t);
1114baa9922SRussell King 
112a9c9147eSRussell King 	void (*dma_map_area)(const void *, size_t, int);
113a9c9147eSRussell King 	void (*dma_unmap_area)(const void *, size_t, int);
114a9c9147eSRussell King 
1154baa9922SRussell King 	void (*dma_flush_range)(const void *, const void *);
1168acdf505SKees Cook } __no_randomize_layout;
1174baa9922SRussell King 
1184baa9922SRussell King /*
1194baa9922SRussell King  * Select the calling method
1204baa9922SRussell King  */
1214baa9922SRussell King #ifdef MULTI_CACHE
1224baa9922SRussell King 
1234baa9922SRussell King extern struct cpu_cache_fns cpu_cache;
1244baa9922SRussell King 
12581d11955STony Lindgren #define __cpuc_flush_icache_all		cpu_cache.flush_icache_all
1264baa9922SRussell King #define __cpuc_flush_kern_all		cpu_cache.flush_kern_all
127031bd879SLorenzo Pieralisi #define __cpuc_flush_kern_louis		cpu_cache.flush_kern_louis
1284baa9922SRussell King #define __cpuc_flush_user_all		cpu_cache.flush_user_all
1294baa9922SRussell King #define __cpuc_flush_user_range		cpu_cache.flush_user_range
1304baa9922SRussell King #define __cpuc_coherent_kern_range	cpu_cache.coherent_kern_range
1314baa9922SRussell King #define __cpuc_coherent_user_range	cpu_cache.coherent_user_range
1322c9b9c84SRussell King #define __cpuc_flush_dcache_area	cpu_cache.flush_kern_dcache_area
1334baa9922SRussell King 
1344baa9922SRussell King /*
1354baa9922SRussell King  * These are private to the dma-mapping API.  Do not use directly.
1364baa9922SRussell King  * Their sole purpose is to ensure that data held in the cache
1374baa9922SRussell King  * is visible to DMA, or data written by DMA to system memory is
1384baa9922SRussell King  * visible to the CPU.
1394baa9922SRussell King  */
1404baa9922SRussell King #define dmac_flush_range		cpu_cache.dma_flush_range
1414baa9922SRussell King 
1424baa9922SRussell King #else
1434baa9922SRussell King 
14481d11955STony Lindgren extern void __cpuc_flush_icache_all(void);
1454baa9922SRussell King extern void __cpuc_flush_kern_all(void);
146031bd879SLorenzo Pieralisi extern void __cpuc_flush_kern_louis(void);
1474baa9922SRussell King extern void __cpuc_flush_user_all(void);
1484baa9922SRussell King extern void __cpuc_flush_user_range(unsigned long, unsigned long, unsigned int);
1494baa9922SRussell King extern void __cpuc_coherent_kern_range(unsigned long, unsigned long);
150c5102f59SWill Deacon extern int  __cpuc_coherent_user_range(unsigned long, unsigned long);
1512c9b9c84SRussell King extern void __cpuc_flush_dcache_area(void *, size_t);
1524baa9922SRussell King 
1534baa9922SRussell King /*
1544baa9922SRussell King  * These are private to the dma-mapping API.  Do not use directly.
1554baa9922SRussell King  * Their sole purpose is to ensure that data held in the cache
1564baa9922SRussell King  * is visible to DMA, or data written by DMA to system memory is
1574baa9922SRussell King  * visible to the CPU.
1584baa9922SRussell King  */
1594baa9922SRussell King extern void dmac_flush_range(const void *, const void *);
1604baa9922SRussell King 
1614baa9922SRussell King #endif
1624baa9922SRussell King 
1634baa9922SRussell King /*
1644baa9922SRussell King  * Copy user data from/to a page which is mapped into a different
1654baa9922SRussell King  * processes address space.  Really, we want to allow our "user
1664baa9922SRussell King  * space" model to handle this.
1674baa9922SRussell King  */
1682ef7f3dbSRussell King extern void copy_to_user_page(struct vm_area_struct *, struct page *,
1692ef7f3dbSRussell King 	unsigned long, void *, const void *, unsigned long);
1704baa9922SRussell King #define copy_from_user_page(vma, page, vaddr, dst, src, len) \
1714baa9922SRussell King 	do {							\
1724baa9922SRussell King 		memcpy(dst, src, len);				\
1734baa9922SRussell King 	} while (0)
1744baa9922SRussell King 
1754baa9922SRussell King /*
1764baa9922SRussell King  * Convert calls to our calling convention.
1774baa9922SRussell King  */
17881d11955STony Lindgren 
17981d11955STony Lindgren /* Invalidate I-cache */
18081d11955STony Lindgren #define __flush_icache_all_generic()					\
18181d11955STony Lindgren 	asm("mcr	p15, 0, %0, c7, c5, 0"				\
18281d11955STony Lindgren 	    : : "r" (0));
18381d11955STony Lindgren 
18481d11955STony Lindgren /* Invalidate I-cache inner shareable */
18581d11955STony Lindgren #define __flush_icache_all_v7_smp()					\
18681d11955STony Lindgren 	asm("mcr	p15, 0, %0, c7, c1, 0"				\
18781d11955STony Lindgren 	    : : "r" (0));
18881d11955STony Lindgren 
18981d11955STony Lindgren /*
19081d11955STony Lindgren  * Optimized __flush_icache_all for the common cases. Note that UP ARMv7
19181d11955STony Lindgren  * will fall through to use __flush_icache_all_generic.
19281d11955STony Lindgren  */
193e399b1a4SRussell King #if (defined(CONFIG_CPU_V7) && \
194e399b1a4SRussell King      (defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K))) || \
19581d11955STony Lindgren 	defined(CONFIG_SMP_ON_UP)
19681d11955STony Lindgren #define __flush_icache_preferred	__cpuc_flush_icache_all
19781d11955STony Lindgren #elif __LINUX_ARM_ARCH__ >= 7 && defined(CONFIG_SMP)
19881d11955STony Lindgren #define __flush_icache_preferred	__flush_icache_all_v7_smp
19981d11955STony Lindgren #elif __LINUX_ARM_ARCH__ == 6 && defined(CONFIG_ARM_ERRATA_411920)
20081d11955STony Lindgren #define __flush_icache_preferred	__cpuc_flush_icache_all
20181d11955STony Lindgren #else
20281d11955STony Lindgren #define __flush_icache_preferred	__flush_icache_all_generic
20381d11955STony Lindgren #endif
20481d11955STony Lindgren 
__flush_icache_all(void)20581d11955STony Lindgren static inline void __flush_icache_all(void)
20681d11955STony Lindgren {
20781d11955STony Lindgren 	__flush_icache_preferred();
2089581960aSWill Deacon 	dsb(ishst);
20981d11955STony Lindgren }
21081d11955STony Lindgren 
211031bd879SLorenzo Pieralisi /*
212031bd879SLorenzo Pieralisi  * Flush caches up to Level of Unification Inner Shareable
213031bd879SLorenzo Pieralisi  */
214031bd879SLorenzo Pieralisi #define flush_cache_louis()		__cpuc_flush_kern_louis()
215031bd879SLorenzo Pieralisi 
2164baa9922SRussell King #define flush_cache_all()		__cpuc_flush_kern_all()
2172f0b1926SRussell King 
vivt_flush_cache_mm(struct mm_struct * mm)2182f0b1926SRussell King static inline void vivt_flush_cache_mm(struct mm_struct *mm)
2194baa9922SRussell King {
22056f8ba83SRusty Russell 	if (cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
2214baa9922SRussell King 		__cpuc_flush_user_all();
2224baa9922SRussell King }
2234baa9922SRussell King 
2244baa9922SRussell King static inline void
vivt_flush_cache_range(struct vm_area_struct * vma,unsigned long start,unsigned long end)2252f0b1926SRussell King vivt_flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
2264baa9922SRussell King {
227b74253f7SWill Deacon 	struct mm_struct *mm = vma->vm_mm;
228b74253f7SWill Deacon 
229b74253f7SWill Deacon 	if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm)))
2304baa9922SRussell King 		__cpuc_flush_user_range(start & PAGE_MASK, PAGE_ALIGN(end),
2314baa9922SRussell King 					vma->vm_flags);
2324baa9922SRussell King }
2334baa9922SRussell King 
vivt_flush_cache_pages(struct vm_area_struct * vma,unsigned long user_addr,unsigned long pfn,unsigned int nr)2348b5989f3SMatthew Wilcox (Oracle) static inline void vivt_flush_cache_pages(struct vm_area_struct *vma,
2358b5989f3SMatthew Wilcox (Oracle) 		unsigned long user_addr, unsigned long pfn, unsigned int nr)
2364baa9922SRussell King {
237b74253f7SWill Deacon 	struct mm_struct *mm = vma->vm_mm;
238b74253f7SWill Deacon 
239b74253f7SWill Deacon 	if (!mm || cpumask_test_cpu(smp_processor_id(), mm_cpumask(mm))) {
2404baa9922SRussell King 		unsigned long addr = user_addr & PAGE_MASK;
2418b5989f3SMatthew Wilcox (Oracle) 		__cpuc_flush_user_range(addr, addr + nr * PAGE_SIZE,
2428b5989f3SMatthew Wilcox (Oracle) 				vma->vm_flags);
2434baa9922SRussell King 	}
2444baa9922SRussell King }
2454baa9922SRussell King 
2462f0b1926SRussell King #ifndef CONFIG_CPU_CACHE_VIPT
2472f0b1926SRussell King #define flush_cache_mm(mm) \
2482f0b1926SRussell King 		vivt_flush_cache_mm(mm)
2492f0b1926SRussell King #define flush_cache_range(vma,start,end) \
2502f0b1926SRussell King 		vivt_flush_cache_range(vma,start,end)
2518b5989f3SMatthew Wilcox (Oracle) #define flush_cache_pages(vma, addr, pfn, nr) \
2528b5989f3SMatthew Wilcox (Oracle) 		vivt_flush_cache_pages(vma, addr, pfn, nr)
2534baa9922SRussell King #else
2548b5989f3SMatthew Wilcox (Oracle) void flush_cache_mm(struct mm_struct *mm);
2558b5989f3SMatthew Wilcox (Oracle) void flush_cache_range(struct vm_area_struct *vma, unsigned long start, unsigned long end);
2568b5989f3SMatthew Wilcox (Oracle) void flush_cache_pages(struct vm_area_struct *vma, unsigned long user_addr,
2578b5989f3SMatthew Wilcox (Oracle) 		unsigned long pfn, unsigned int nr);
2584baa9922SRussell King #endif
2594baa9922SRussell King 
2604baa9922SRussell King #define flush_cache_dup_mm(mm) flush_cache_mm(mm)
2618b5989f3SMatthew Wilcox (Oracle) #define flush_cache_page(vma, addr, pfn) flush_cache_pages(vma, addr, pfn, 1)
2624baa9922SRussell King 
2634baa9922SRussell King /*
264fca7f8e6SChristoph Hellwig  * flush_icache_user_range is used when we want to ensure that the
2654baa9922SRussell King  * Harvard caches are synchronised for the user space address range.
2664baa9922SRussell King  * This is used for the ARM private sys_cacheflush system call.
2674baa9922SRussell King  */
268fca7f8e6SChristoph Hellwig #define flush_icache_user_range(s,e)	__cpuc_coherent_user_range(s,e)
2694baa9922SRussell King 
2704baa9922SRussell King /*
2714baa9922SRussell King  * Perform necessary cache operations to ensure that data previously
2724baa9922SRussell King  * stored within this range of addresses can be executed by the CPU.
2734baa9922SRussell King  */
2744baa9922SRussell King #define flush_icache_range(s,e)		__cpuc_coherent_kern_range(s,e)
2754baa9922SRussell King 
2764baa9922SRussell King /*
2774baa9922SRussell King  * Perform necessary cache operations to ensure that the TLB will
2784baa9922SRussell King  * see data written in the specified area.
2794baa9922SRussell King  */
2804baa9922SRussell King #define clean_dcache_area(start,size)	cpu_dcache_clean_area(start, size)
2814baa9922SRussell King 
2824baa9922SRussell King /*
2834baa9922SRussell King  * flush_dcache_page is used when the kernel has written to the page
2844baa9922SRussell King  * cache page at virtual address page->virtual.
2854baa9922SRussell King  *
2864baa9922SRussell King  * If this page isn't mapped (ie, page_mapping == NULL), or it might
2874baa9922SRussell King  * have userspace mappings, then we _must_ always clean + invalidate
2884baa9922SRussell King  * the dcache entries associated with the kernel mapping.
2894baa9922SRussell King  *
2904baa9922SRussell King  * Otherwise we can defer the operation, and clean the cache when we are
2914baa9922SRussell King  * about to change to user space.  This is the same method as used on SPARC64.
2924baa9922SRussell King  * See update_mmu_cache for the user space part.
2934baa9922SRussell King  */
2942d4dc890SIlya Loginov #define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
2958b5989f3SMatthew Wilcox (Oracle) void flush_dcache_page(struct page *);
2968b5989f3SMatthew Wilcox (Oracle) void flush_dcache_folio(struct folio *folio);
2978b5989f3SMatthew Wilcox (Oracle) #define flush_dcache_folio flush_dcache_folio
2984baa9922SRussell King 
299f358afc5SChristoph Hellwig #define ARCH_IMPLEMENTS_FLUSH_KERNEL_VMAP_RANGE 1
flush_kernel_vmap_range(void * addr,int size)300252a9affSJames Bottomley static inline void flush_kernel_vmap_range(void *addr, int size)
301252a9affSJames Bottomley {
302252a9affSJames Bottomley 	if ((cache_is_vivt() || cache_is_vipt_aliasing()))
303252a9affSJames Bottomley 	  __cpuc_flush_dcache_area(addr, (size_t)size);
304252a9affSJames Bottomley }
invalidate_kernel_vmap_range(void * addr,int size)305252a9affSJames Bottomley static inline void invalidate_kernel_vmap_range(void *addr, int size)
306252a9affSJames Bottomley {
307252a9affSJames Bottomley 	if ((cache_is_vivt() || cache_is_vipt_aliasing()))
308252a9affSJames Bottomley 	  __cpuc_flush_dcache_area(addr, (size_t)size);
309252a9affSJames Bottomley }
3104baa9922SRussell King 
3114baa9922SRussell King #define ARCH_HAS_FLUSH_ANON_PAGE
flush_anon_page(struct vm_area_struct * vma,struct page * page,unsigned long vmaddr)3124baa9922SRussell King static inline void flush_anon_page(struct vm_area_struct *vma,
3134baa9922SRussell King 			 struct page *page, unsigned long vmaddr)
3144baa9922SRussell King {
3154baa9922SRussell King 	extern void __flush_anon_page(struct vm_area_struct *vma,
3164baa9922SRussell King 				struct page *, unsigned long);
3174baa9922SRussell King 	if (PageAnon(page))
3184baa9922SRussell King 		__flush_anon_page(vma, page, vmaddr);
3194baa9922SRussell King }
3204baa9922SRussell King 
321b93b0163SMatthew Wilcox #define flush_dcache_mmap_lock(mapping)		xa_lock_irq(&mapping->i_pages)
322b93b0163SMatthew Wilcox #define flush_dcache_mmap_unlock(mapping)	xa_unlock_irq(&mapping->i_pages)
3234baa9922SRussell King 
3244baa9922SRussell King /*
325376e1421SCatalin Marinas  * flush_cache_vmap() is used when creating mappings (eg, via vmap,
326376e1421SCatalin Marinas  * vmalloc, ioremap etc) in kernel space for pages.  On non-VIPT
327376e1421SCatalin Marinas  * caches, since the direct-mappings of these pages may contain cached
328376e1421SCatalin Marinas  * data, we need to do a full cache flush to ensure that writebacks
329376e1421SCatalin Marinas  * don't corrupt data placed into these pages via the new mappings.
330376e1421SCatalin Marinas  */
flush_cache_vmap(unsigned long start,unsigned long end)331376e1421SCatalin Marinas static inline void flush_cache_vmap(unsigned long start, unsigned long end)
332376e1421SCatalin Marinas {
333376e1421SCatalin Marinas 	if (!cache_is_vipt_nonaliasing())
334376e1421SCatalin Marinas 		flush_cache_all();
335376e1421SCatalin Marinas 	else
336376e1421SCatalin Marinas 		/*
337376e1421SCatalin Marinas 		 * set_pte_at() called from vmap_pte_range() does not
338376e1421SCatalin Marinas 		 * have a DSB after cleaning the cache line.
339376e1421SCatalin Marinas 		 */
3406af396a6SWill Deacon 		dsb(ishst);
341376e1421SCatalin Marinas }
342376e1421SCatalin Marinas 
343*c4a05cf0SAlexandre Ghiti #define flush_cache_vmap_early(start, end)	do { } while (0)
344*c4a05cf0SAlexandre Ghiti 
flush_cache_vunmap(unsigned long start,unsigned long end)345376e1421SCatalin Marinas static inline void flush_cache_vunmap(unsigned long start, unsigned long end)
346376e1421SCatalin Marinas {
347376e1421SCatalin Marinas 	if (!cache_is_vipt_nonaliasing())
348376e1421SCatalin Marinas 		flush_cache_all();
349376e1421SCatalin Marinas }
350376e1421SCatalin Marinas 
3510c91e7e0SNicolas Pitre /*
3520c91e7e0SNicolas Pitre  * Memory synchronization helpers for mixed cached vs non cached accesses.
3530c91e7e0SNicolas Pitre  *
3540c91e7e0SNicolas Pitre  * Some synchronization algorithms have to set states in memory with the
3550c91e7e0SNicolas Pitre  * cache enabled or disabled depending on the code path.  It is crucial
3560c91e7e0SNicolas Pitre  * to always ensure proper cache maintenance to update main memory right
3570c91e7e0SNicolas Pitre  * away in that case.
3580c91e7e0SNicolas Pitre  *
3590c91e7e0SNicolas Pitre  * Any cached write must be followed by a cache clean operation.
3600c91e7e0SNicolas Pitre  * Any cached read must be preceded by a cache invalidate operation.
3610c91e7e0SNicolas Pitre  * Yet, in the read case, a cache flush i.e. atomic clean+invalidate
3620c91e7e0SNicolas Pitre  * operation is needed to avoid discarding possible concurrent writes to the
3630c91e7e0SNicolas Pitre  * accessed memory.
3640c91e7e0SNicolas Pitre  *
3650c91e7e0SNicolas Pitre  * Also, in order to prevent a cached writer from interfering with an
3660c91e7e0SNicolas Pitre  * adjacent non-cached writer, each state variable must be located to
3670c91e7e0SNicolas Pitre  * a separate cache line.
3680c91e7e0SNicolas Pitre  */
3690c91e7e0SNicolas Pitre 
3700c91e7e0SNicolas Pitre /*
3710c91e7e0SNicolas Pitre  * This needs to be >= the max cache writeback size of all
3720c91e7e0SNicolas Pitre  * supported platforms included in the current kernel configuration.
3730c91e7e0SNicolas Pitre  * This is used to align state variables to their own cache lines.
3740c91e7e0SNicolas Pitre  */
3750c91e7e0SNicolas Pitre #define __CACHE_WRITEBACK_ORDER 6  /* guessed from existing platforms */
3760c91e7e0SNicolas Pitre #define __CACHE_WRITEBACK_GRANULE (1 << __CACHE_WRITEBACK_ORDER)
3770c91e7e0SNicolas Pitre 
3780c91e7e0SNicolas Pitre /*
3790c91e7e0SNicolas Pitre  * There is no __cpuc_clean_dcache_area but we use it anyway for
3800c91e7e0SNicolas Pitre  * code intent clarity, and alias it to __cpuc_flush_dcache_area.
3810c91e7e0SNicolas Pitre  */
3820c91e7e0SNicolas Pitre #define __cpuc_clean_dcache_area __cpuc_flush_dcache_area
3830c91e7e0SNicolas Pitre 
3840c91e7e0SNicolas Pitre /*
3850c91e7e0SNicolas Pitre  * Ensure preceding writes to *p by this CPU are visible to
3860c91e7e0SNicolas Pitre  * subsequent reads by other CPUs:
3870c91e7e0SNicolas Pitre  */
__sync_cache_range_w(volatile void * p,size_t size)3880c91e7e0SNicolas Pitre static inline void __sync_cache_range_w(volatile void *p, size_t size)
3890c91e7e0SNicolas Pitre {
3900c91e7e0SNicolas Pitre 	char *_p = (char *)p;
3910c91e7e0SNicolas Pitre 
3920c91e7e0SNicolas Pitre 	__cpuc_clean_dcache_area(_p, size);
3930c91e7e0SNicolas Pitre 	outer_clean_range(__pa(_p), __pa(_p + size));
3940c91e7e0SNicolas Pitre }
3950c91e7e0SNicolas Pitre 
3960c91e7e0SNicolas Pitre /*
3970c91e7e0SNicolas Pitre  * Ensure preceding writes to *p by other CPUs are visible to
3980c91e7e0SNicolas Pitre  * subsequent reads by this CPU.  We must be careful not to
3990c91e7e0SNicolas Pitre  * discard data simultaneously written by another CPU, hence the
4000c91e7e0SNicolas Pitre  * usage of flush rather than invalidate operations.
4010c91e7e0SNicolas Pitre  */
__sync_cache_range_r(volatile void * p,size_t size)4020c91e7e0SNicolas Pitre static inline void __sync_cache_range_r(volatile void *p, size_t size)
4030c91e7e0SNicolas Pitre {
4040c91e7e0SNicolas Pitre 	char *_p = (char *)p;
4050c91e7e0SNicolas Pitre 
4060c91e7e0SNicolas Pitre #ifdef CONFIG_OUTER_CACHE
4070c91e7e0SNicolas Pitre 	if (outer_cache.flush_range) {
4080c91e7e0SNicolas Pitre 		/*
4090c91e7e0SNicolas Pitre 		 * Ensure dirty data migrated from other CPUs into our cache
4100c91e7e0SNicolas Pitre 		 * are cleaned out safely before the outer cache is cleaned:
4110c91e7e0SNicolas Pitre 		 */
4120c91e7e0SNicolas Pitre 		__cpuc_clean_dcache_area(_p, size);
4130c91e7e0SNicolas Pitre 
4140c91e7e0SNicolas Pitre 		/* Clean and invalidate stale data for *p from outer ... */
4150c91e7e0SNicolas Pitre 		outer_flush_range(__pa(_p), __pa(_p + size));
4160c91e7e0SNicolas Pitre 	}
4170c91e7e0SNicolas Pitre #endif
4180c91e7e0SNicolas Pitre 
4190c91e7e0SNicolas Pitre 	/* ... and inner cache: */
4200c91e7e0SNicolas Pitre 	__cpuc_flush_dcache_area(_p, size);
4210c91e7e0SNicolas Pitre }
4220c91e7e0SNicolas Pitre 
4230c91e7e0SNicolas Pitre #define sync_cache_w(ptr) __sync_cache_range_w(ptr, sizeof *(ptr))
4240c91e7e0SNicolas Pitre #define sync_cache_r(ptr) __sync_cache_range_r(ptr, sizeof *(ptr))
4250c91e7e0SNicolas Pitre 
42639792c7cSNicolas Pitre /*
42739792c7cSNicolas Pitre  * Disabling cache access for one CPU in an ARMv7 SMP system is tricky.
42839792c7cSNicolas Pitre  * To do so we must:
42939792c7cSNicolas Pitre  *
43039792c7cSNicolas Pitre  * - Clear the SCTLR.C bit to prevent further cache allocations
43139792c7cSNicolas Pitre  * - Flush the desired level of cache
43239792c7cSNicolas Pitre  * - Clear the ACTLR "SMP" bit to disable local coherency
43339792c7cSNicolas Pitre  *
43439792c7cSNicolas Pitre  * ... and so without any intervening memory access in between those steps,
43539792c7cSNicolas Pitre  * not even to the stack.
43639792c7cSNicolas Pitre  *
43739792c7cSNicolas Pitre  * WARNING -- After this has been called:
43839792c7cSNicolas Pitre  *
43939792c7cSNicolas Pitre  * - No ldrex/strex (and similar) instructions must be used.
44039792c7cSNicolas Pitre  * - The CPU is obviously no longer coherent with the other CPUs.
44139792c7cSNicolas Pitre  * - This is unlikely to work as expected if Linux is running non-secure.
44239792c7cSNicolas Pitre  *
44339792c7cSNicolas Pitre  * Note:
44439792c7cSNicolas Pitre  *
44539792c7cSNicolas Pitre  * - This is known to apply to several ARMv7 processor implementations,
44639792c7cSNicolas Pitre  *   however some exceptions may exist.  Caveat emptor.
44739792c7cSNicolas Pitre  *
44839792c7cSNicolas Pitre  * - The clobber list is dictated by the call to v7_flush_dcache_*.
44939792c7cSNicolas Pitre  */
45039792c7cSNicolas Pitre #define v7_exit_coherency_flush(level) \
45139792c7cSNicolas Pitre 	asm volatile( \
452ebc77251SKrzysztof Kozlowski 	".arch	armv7-a \n\t" \
45339792c7cSNicolas Pitre 	"mrc	p15, 0, r0, c1, c0, 0	@ get SCTLR \n\t" \
45439792c7cSNicolas Pitre 	"bic	r0, r0, #"__stringify(CR_C)" \n\t" \
45539792c7cSNicolas Pitre 	"mcr	p15, 0, r0, c1, c0, 0	@ set SCTLR \n\t" \
45639792c7cSNicolas Pitre 	"isb	\n\t" \
45739792c7cSNicolas Pitre 	"bl	v7_flush_dcache_"__stringify(level)" \n\t" \
45839792c7cSNicolas Pitre 	"mrc	p15, 0, r0, c1, c0, 1	@ get ACTLR \n\t" \
45939792c7cSNicolas Pitre 	"bic	r0, r0, #(1 << 6)	@ disable local coherency \n\t" \
46039792c7cSNicolas Pitre 	"mcr	p15, 0, r0, c1, c0, 1	@ set ACTLR \n\t" \
46139792c7cSNicolas Pitre 	"isb	\n\t" \
4621f640552SArd Biesheuvel 	"dsb" \
4631f640552SArd Biesheuvel 	: : : "r0","r1","r2","r3","r4","r5","r6", \
4641f640552SArd Biesheuvel 	      "r9","r10","ip","lr","memory" )
46539792c7cSNicolas Pitre 
46672e6ae28SVictor Kamensky void flush_uprobe_xol_access(struct page *page, unsigned long uaddr,
46772e6ae28SVictor Kamensky 			     void *kaddr, unsigned long len);
46880d6b0c2SKees Cook 
4695f41f919SMarek Szyprowski 
4705f41f919SMarek Szyprowski #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
4715f41f919SMarek Szyprowski void check_cpu_icache_size(int cpuid);
4725f41f919SMarek Szyprowski #else
check_cpu_icache_size(int cpuid)4735f41f919SMarek Szyprowski static inline void check_cpu_icache_size(int cpuid) { }
4745f41f919SMarek Szyprowski #endif
4755f41f919SMarek Szyprowski 
4764baa9922SRussell King #endif
477