162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci */
462306a36Sopenharmony_ci#ifndef _ASM_POWERPC_CACHEFLUSH_H
562306a36Sopenharmony_ci#define _ASM_POWERPC_CACHEFLUSH_H
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/mm.h>
862306a36Sopenharmony_ci#include <asm/cputable.h>
962306a36Sopenharmony_ci#include <asm/cpu_has_feature.h>
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/*
1262306a36Sopenharmony_ci * This flag is used to indicate that the page pointed to by a pte is clean
1362306a36Sopenharmony_ci * and does not require cleaning before returning it to the user.
1462306a36Sopenharmony_ci */
1562306a36Sopenharmony_ci#define PG_dcache_clean PG_arch_1
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#ifdef CONFIG_PPC_BOOK3S_64
1862306a36Sopenharmony_ci/*
1962306a36Sopenharmony_ci * Book3s has no ptesync after setting a pte, so without this ptesync it's
2062306a36Sopenharmony_ci * possible for a kernel virtual mapping access to return a spurious fault
2162306a36Sopenharmony_ci * if it's accessed right after the pte is set. The page fault handler does
2262306a36Sopenharmony_ci * not expect this type of fault. flush_cache_vmap is not exactly the right
2362306a36Sopenharmony_ci * place to put this, but it seems to work well enough.
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_cistatic inline void flush_cache_vmap(unsigned long start, unsigned long end)
2662306a36Sopenharmony_ci{
2762306a36Sopenharmony_ci	asm volatile("ptesync" ::: "memory");
2862306a36Sopenharmony_ci}
2962306a36Sopenharmony_ci#define flush_cache_vmap flush_cache_vmap
3062306a36Sopenharmony_ci#endif /* CONFIG_PPC_BOOK3S_64 */
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#define ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE 1
3362306a36Sopenharmony_ci/*
3462306a36Sopenharmony_ci * This is called when a page has been modified by the kernel.
3562306a36Sopenharmony_ci * It just marks the page as not i-cache clean.  We do the i-cache
3662306a36Sopenharmony_ci * flush later when the page is given to a user process, if necessary.
3762306a36Sopenharmony_ci */
3862306a36Sopenharmony_cistatic inline void flush_dcache_folio(struct folio *folio)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	if (cpu_has_feature(CPU_FTR_COHERENT_ICACHE))
4162306a36Sopenharmony_ci		return;
4262306a36Sopenharmony_ci	/* avoid an atomic op if possible */
4362306a36Sopenharmony_ci	if (test_bit(PG_dcache_clean, &folio->flags))
4462306a36Sopenharmony_ci		clear_bit(PG_dcache_clean, &folio->flags);
4562306a36Sopenharmony_ci}
4662306a36Sopenharmony_ci#define flush_dcache_folio flush_dcache_folio
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistatic inline void flush_dcache_page(struct page *page)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	flush_dcache_folio(page_folio(page));
5162306a36Sopenharmony_ci}
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_civoid flush_icache_range(unsigned long start, unsigned long stop);
5462306a36Sopenharmony_ci#define flush_icache_range flush_icache_range
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_civoid flush_icache_user_page(struct vm_area_struct *vma, struct page *page,
5762306a36Sopenharmony_ci		unsigned long addr, int len);
5862306a36Sopenharmony_ci#define flush_icache_user_page flush_icache_user_page
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_civoid flush_dcache_icache_folio(struct folio *folio);
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/**
6362306a36Sopenharmony_ci * flush_dcache_range(): Write any modified data cache blocks out to memory and
6462306a36Sopenharmony_ci * invalidate them. Does not invalidate the corresponding instruction cache
6562306a36Sopenharmony_ci * blocks.
6662306a36Sopenharmony_ci *
6762306a36Sopenharmony_ci * @start: the start address
6862306a36Sopenharmony_ci * @stop: the stop address (exclusive)
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_cistatic inline void flush_dcache_range(unsigned long start, unsigned long stop)
7162306a36Sopenharmony_ci{
7262306a36Sopenharmony_ci	unsigned long shift = l1_dcache_shift();
7362306a36Sopenharmony_ci	unsigned long bytes = l1_dcache_bytes();
7462306a36Sopenharmony_ci	void *addr = (void *)(start & ~(bytes - 1));
7562306a36Sopenharmony_ci	unsigned long size = stop - (unsigned long)addr + (bytes - 1);
7662306a36Sopenharmony_ci	unsigned long i;
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_PPC64))
7962306a36Sopenharmony_ci		mb();	/* sync */
8062306a36Sopenharmony_ci
8162306a36Sopenharmony_ci	for (i = 0; i < size >> shift; i++, addr += bytes)
8262306a36Sopenharmony_ci		dcbf(addr);
8362306a36Sopenharmony_ci	mb();	/* sync */
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci}
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci/*
8862306a36Sopenharmony_ci * Write any modified data cache blocks out to memory.
8962306a36Sopenharmony_ci * Does not invalidate the corresponding cache lines (especially for
9062306a36Sopenharmony_ci * any corresponding instruction cache).
9162306a36Sopenharmony_ci */
9262306a36Sopenharmony_cistatic inline void clean_dcache_range(unsigned long start, unsigned long stop)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	unsigned long shift = l1_dcache_shift();
9562306a36Sopenharmony_ci	unsigned long bytes = l1_dcache_bytes();
9662306a36Sopenharmony_ci	void *addr = (void *)(start & ~(bytes - 1));
9762306a36Sopenharmony_ci	unsigned long size = stop - (unsigned long)addr + (bytes - 1);
9862306a36Sopenharmony_ci	unsigned long i;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	for (i = 0; i < size >> shift; i++, addr += bytes)
10162306a36Sopenharmony_ci		dcbst(addr);
10262306a36Sopenharmony_ci	mb();	/* sync */
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci/*
10662306a36Sopenharmony_ci * Like above, but invalidate the D-cache.  This is used by the 8xx
10762306a36Sopenharmony_ci * to invalidate the cache so the PPC core doesn't get stale data
10862306a36Sopenharmony_ci * from the CPM (no cache snooping here :-).
10962306a36Sopenharmony_ci */
11062306a36Sopenharmony_cistatic inline void invalidate_dcache_range(unsigned long start,
11162306a36Sopenharmony_ci					   unsigned long stop)
11262306a36Sopenharmony_ci{
11362306a36Sopenharmony_ci	unsigned long shift = l1_dcache_shift();
11462306a36Sopenharmony_ci	unsigned long bytes = l1_dcache_bytes();
11562306a36Sopenharmony_ci	void *addr = (void *)(start & ~(bytes - 1));
11662306a36Sopenharmony_ci	unsigned long size = stop - (unsigned long)addr + (bytes - 1);
11762306a36Sopenharmony_ci	unsigned long i;
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	for (i = 0; i < size >> shift; i++, addr += bytes)
12062306a36Sopenharmony_ci		dcbi(addr);
12162306a36Sopenharmony_ci	mb();	/* sync */
12262306a36Sopenharmony_ci}
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci#ifdef CONFIG_4xx
12562306a36Sopenharmony_cistatic inline void flush_instruction_cache(void)
12662306a36Sopenharmony_ci{
12762306a36Sopenharmony_ci	iccci((void *)KERNELBASE);
12862306a36Sopenharmony_ci	isync();
12962306a36Sopenharmony_ci}
13062306a36Sopenharmony_ci#else
13162306a36Sopenharmony_civoid flush_instruction_cache(void);
13262306a36Sopenharmony_ci#endif
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#include <asm-generic/cacheflush.h>
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci#endif /* _ASM_POWERPC_CACHEFLUSH_H */
137