18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci */
48c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_CACHEFLUSH_H
58c2ecf20Sopenharmony_ci#define _ASM_POWERPC_CACHEFLUSH_H
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/mm.h>
88c2ecf20Sopenharmony_ci#include <asm/cputable.h>
98c2ecf20Sopenharmony_ci#include <asm/cpu_has_feature.h>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S_64
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * Book3s has no ptesync after setting a pte, so without this ptesync it's
148c2ecf20Sopenharmony_ci * possible for a kernel virtual mapping access to return a spurious fault
158c2ecf20Sopenharmony_ci * if it's accessed right after the pte is set. The page fault handler does
168c2ecf20Sopenharmony_ci * not expect this type of fault. flush_cache_vmap is not exactly the right
178c2ecf20Sopenharmony_ci * place to put this, but it seems to work well enough.
188c2ecf20Sopenharmony_ci */
198c2ecf20Sopenharmony_cistatic inline void flush_cache_vmap(unsigned long start, unsigned long end)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	asm volatile("ptesync" ::: "memory");
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci#define flush_cache_vmap flush_cache_vmap
248c2ecf20Sopenharmony_ci#endif /* CONFIG_PPC_BOOK3S_64 */
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
278c2ecf20Sopenharmony_ciextern void flush_dcache_page(struct page *page);
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_civoid flush_icache_range(unsigned long start, unsigned long stop);
308c2ecf20Sopenharmony_ci#define flush_icache_range flush_icache_range
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_civoid flush_icache_user_page(struct vm_area_struct *vma, struct page *page,
338c2ecf20Sopenharmony_ci		unsigned long addr, int len);
348c2ecf20Sopenharmony_ci#define flush_icache_user_page flush_icache_user_page
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_civoid flush_dcache_icache_page(struct page *page);
378c2ecf20Sopenharmony_civoid __flush_dcache_icache(void *page);
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/**
408c2ecf20Sopenharmony_ci * flush_dcache_range(): Write any modified data cache blocks out to memory and
418c2ecf20Sopenharmony_ci * invalidate them. Does not invalidate the corresponding instruction cache
428c2ecf20Sopenharmony_ci * blocks.
438c2ecf20Sopenharmony_ci *
448c2ecf20Sopenharmony_ci * @start: the start address
458c2ecf20Sopenharmony_ci * @stop: the stop address (exclusive)
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistatic inline void flush_dcache_range(unsigned long start, unsigned long stop)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	unsigned long shift = l1_dcache_shift();
508c2ecf20Sopenharmony_ci	unsigned long bytes = l1_dcache_bytes();
518c2ecf20Sopenharmony_ci	void *addr = (void *)(start & ~(bytes - 1));
528c2ecf20Sopenharmony_ci	unsigned long size = stop - (unsigned long)addr + (bytes - 1);
538c2ecf20Sopenharmony_ci	unsigned long i;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_PPC64))
568c2ecf20Sopenharmony_ci		mb();	/* sync */
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	for (i = 0; i < size >> shift; i++, addr += bytes)
598c2ecf20Sopenharmony_ci		dcbf(addr);
608c2ecf20Sopenharmony_ci	mb();	/* sync */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/*
658c2ecf20Sopenharmony_ci * Write any modified data cache blocks out to memory.
668c2ecf20Sopenharmony_ci * Does not invalidate the corresponding cache lines (especially for
678c2ecf20Sopenharmony_ci * any corresponding instruction cache).
688c2ecf20Sopenharmony_ci */
698c2ecf20Sopenharmony_cistatic inline void clean_dcache_range(unsigned long start, unsigned long stop)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	unsigned long shift = l1_dcache_shift();
728c2ecf20Sopenharmony_ci	unsigned long bytes = l1_dcache_bytes();
738c2ecf20Sopenharmony_ci	void *addr = (void *)(start & ~(bytes - 1));
748c2ecf20Sopenharmony_ci	unsigned long size = stop - (unsigned long)addr + (bytes - 1);
758c2ecf20Sopenharmony_ci	unsigned long i;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	for (i = 0; i < size >> shift; i++, addr += bytes)
788c2ecf20Sopenharmony_ci		dcbst(addr);
798c2ecf20Sopenharmony_ci	mb();	/* sync */
808c2ecf20Sopenharmony_ci}
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci/*
838c2ecf20Sopenharmony_ci * Like above, but invalidate the D-cache.  This is used by the 8xx
848c2ecf20Sopenharmony_ci * to invalidate the cache so the PPC core doesn't get stale data
858c2ecf20Sopenharmony_ci * from the CPM (no cache snooping here :-).
868c2ecf20Sopenharmony_ci */
878c2ecf20Sopenharmony_cistatic inline void invalidate_dcache_range(unsigned long start,
888c2ecf20Sopenharmony_ci					   unsigned long stop)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	unsigned long shift = l1_dcache_shift();
918c2ecf20Sopenharmony_ci	unsigned long bytes = l1_dcache_bytes();
928c2ecf20Sopenharmony_ci	void *addr = (void *)(start & ~(bytes - 1));
938c2ecf20Sopenharmony_ci	unsigned long size = stop - (unsigned long)addr + (bytes - 1);
948c2ecf20Sopenharmony_ci	unsigned long i;
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	for (i = 0; i < size >> shift; i++, addr += bytes)
978c2ecf20Sopenharmony_ci		dcbi(addr);
988c2ecf20Sopenharmony_ci	mb();	/* sync */
998c2ecf20Sopenharmony_ci}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#ifdef CONFIG_4xx
1028c2ecf20Sopenharmony_cistatic inline void flush_instruction_cache(void)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	iccci((void *)KERNELBASE);
1058c2ecf20Sopenharmony_ci	isync();
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci#else
1088c2ecf20Sopenharmony_civoid flush_instruction_cache(void);
1098c2ecf20Sopenharmony_ci#endif
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci#include <asm-generic/cacheflush.h>
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#endif /* _ASM_POWERPC_CACHEFLUSH_H */
114