162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_BOOK3S_64_TLBFLUSH_H
362306a36Sopenharmony_ci#define _ASM_POWERPC_BOOK3S_64_TLBFLUSH_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#define MMU_NO_CONTEXT	~0UL
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/mm_types.h>
862306a36Sopenharmony_ci#include <linux/mmu_notifier.h>
962306a36Sopenharmony_ci#include <asm/book3s/64/tlbflush-hash.h>
1062306a36Sopenharmony_ci#include <asm/book3s/64/tlbflush-radix.h>
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci/* TLB flush actions. Used as argument to tlbiel_all() */
1362306a36Sopenharmony_cienum {
1462306a36Sopenharmony_ci	TLB_INVAL_SCOPE_GLOBAL = 0,	/* invalidate all TLBs */
1562306a36Sopenharmony_ci	TLB_INVAL_SCOPE_LPID = 1,	/* invalidate TLBs for current LPID */
1662306a36Sopenharmony_ci};
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_cistatic inline void tlbiel_all(void)
1962306a36Sopenharmony_ci{
2062306a36Sopenharmony_ci	/*
2162306a36Sopenharmony_ci	 * This is used for host machine check and bootup.
2262306a36Sopenharmony_ci	 *
2362306a36Sopenharmony_ci	 * This uses early_radix_enabled and implementations use
2462306a36Sopenharmony_ci	 * early_cpu_has_feature etc because that works early in boot
2562306a36Sopenharmony_ci	 * and this is the machine check path which is not performance
2662306a36Sopenharmony_ci	 * critical.
2762306a36Sopenharmony_ci	 */
2862306a36Sopenharmony_ci	if (early_radix_enabled())
2962306a36Sopenharmony_ci		radix__tlbiel_all(TLB_INVAL_SCOPE_GLOBAL);
3062306a36Sopenharmony_ci	else
3162306a36Sopenharmony_ci		hash__tlbiel_all(TLB_INVAL_SCOPE_GLOBAL);
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cistatic inline void tlbiel_all_lpid(bool radix)
3562306a36Sopenharmony_ci{
3662306a36Sopenharmony_ci	/*
3762306a36Sopenharmony_ci	 * This is used for guest machine check.
3862306a36Sopenharmony_ci	 */
3962306a36Sopenharmony_ci	if (radix)
4062306a36Sopenharmony_ci		radix__tlbiel_all(TLB_INVAL_SCOPE_LPID);
4162306a36Sopenharmony_ci	else
4262306a36Sopenharmony_ci		hash__tlbiel_all(TLB_INVAL_SCOPE_LPID);
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci#define __HAVE_ARCH_FLUSH_PMD_TLB_RANGE
4762306a36Sopenharmony_cistatic inline void flush_pmd_tlb_range(struct vm_area_struct *vma,
4862306a36Sopenharmony_ci				       unsigned long start, unsigned long end)
4962306a36Sopenharmony_ci{
5062306a36Sopenharmony_ci	if (radix_enabled())
5162306a36Sopenharmony_ci		radix__flush_pmd_tlb_range(vma, start, end);
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#define __HAVE_ARCH_FLUSH_PUD_TLB_RANGE
5562306a36Sopenharmony_cistatic inline void flush_pud_tlb_range(struct vm_area_struct *vma,
5662306a36Sopenharmony_ci				       unsigned long start, unsigned long end)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	if (radix_enabled())
5962306a36Sopenharmony_ci		radix__flush_pud_tlb_range(vma, start, end);
6062306a36Sopenharmony_ci}
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define __HAVE_ARCH_FLUSH_HUGETLB_TLB_RANGE
6362306a36Sopenharmony_cistatic inline void flush_hugetlb_tlb_range(struct vm_area_struct *vma,
6462306a36Sopenharmony_ci					   unsigned long start,
6562306a36Sopenharmony_ci					   unsigned long end)
6662306a36Sopenharmony_ci{
6762306a36Sopenharmony_ci	if (radix_enabled())
6862306a36Sopenharmony_ci		radix__flush_hugetlb_tlb_range(vma, start, end);
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic inline void flush_tlb_range(struct vm_area_struct *vma,
7262306a36Sopenharmony_ci				   unsigned long start, unsigned long end)
7362306a36Sopenharmony_ci{
7462306a36Sopenharmony_ci	if (radix_enabled())
7562306a36Sopenharmony_ci		radix__flush_tlb_range(vma, start, end);
7662306a36Sopenharmony_ci}
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_cistatic inline void flush_tlb_kernel_range(unsigned long start,
7962306a36Sopenharmony_ci					  unsigned long end)
8062306a36Sopenharmony_ci{
8162306a36Sopenharmony_ci	if (radix_enabled())
8262306a36Sopenharmony_ci		radix__flush_tlb_kernel_range(start, end);
8362306a36Sopenharmony_ci}
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_cistatic inline void local_flush_tlb_mm(struct mm_struct *mm)
8662306a36Sopenharmony_ci{
8762306a36Sopenharmony_ci	if (radix_enabled())
8862306a36Sopenharmony_ci		radix__local_flush_tlb_mm(mm);
8962306a36Sopenharmony_ci}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_cistatic inline void local_flush_tlb_page(struct vm_area_struct *vma,
9262306a36Sopenharmony_ci					unsigned long vmaddr)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	if (radix_enabled())
9562306a36Sopenharmony_ci		radix__local_flush_tlb_page(vma, vmaddr);
9662306a36Sopenharmony_ci}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic inline void local_flush_tlb_page_psize(struct mm_struct *mm,
9962306a36Sopenharmony_ci					      unsigned long vmaddr, int psize)
10062306a36Sopenharmony_ci{
10162306a36Sopenharmony_ci	if (radix_enabled())
10262306a36Sopenharmony_ci		radix__local_flush_tlb_page_psize(mm, vmaddr, psize);
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_cistatic inline void tlb_flush(struct mmu_gather *tlb)
10662306a36Sopenharmony_ci{
10762306a36Sopenharmony_ci	if (radix_enabled())
10862306a36Sopenharmony_ci		radix__tlb_flush(tlb);
10962306a36Sopenharmony_ci	else
11062306a36Sopenharmony_ci		hash__tlb_flush(tlb);
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci#ifdef CONFIG_SMP
11462306a36Sopenharmony_cistatic inline void flush_tlb_mm(struct mm_struct *mm)
11562306a36Sopenharmony_ci{
11662306a36Sopenharmony_ci	if (radix_enabled())
11762306a36Sopenharmony_ci		radix__flush_tlb_mm(mm);
11862306a36Sopenharmony_ci}
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_cistatic inline void flush_tlb_page(struct vm_area_struct *vma,
12162306a36Sopenharmony_ci				  unsigned long vmaddr)
12262306a36Sopenharmony_ci{
12362306a36Sopenharmony_ci	if (radix_enabled())
12462306a36Sopenharmony_ci		radix__flush_tlb_page(vma, vmaddr);
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci#else
12762306a36Sopenharmony_ci#define flush_tlb_mm(mm)		local_flush_tlb_mm(mm)
12862306a36Sopenharmony_ci#define flush_tlb_page(vma, addr)	local_flush_tlb_page(vma, addr)
12962306a36Sopenharmony_ci#endif /* CONFIG_SMP */
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci#define flush_tlb_fix_spurious_fault flush_tlb_fix_spurious_fault
13262306a36Sopenharmony_cistatic inline void flush_tlb_fix_spurious_fault(struct vm_area_struct *vma,
13362306a36Sopenharmony_ci						unsigned long address,
13462306a36Sopenharmony_ci						pte_t *ptep)
13562306a36Sopenharmony_ci{
13662306a36Sopenharmony_ci	/*
13762306a36Sopenharmony_ci	 * Book3S 64 does not require spurious fault flushes because the PTE
13862306a36Sopenharmony_ci	 * must be re-fetched in case of an access permission problem. So the
13962306a36Sopenharmony_ci	 * only reason for a spurious fault should be concurrent modification
14062306a36Sopenharmony_ci	 * to the PTE, in which case the PTE will eventually be re-fetched by
14162306a36Sopenharmony_ci	 * the MMU when it attempts the access again.
14262306a36Sopenharmony_ci	 *
14362306a36Sopenharmony_ci	 * See: Power ISA Version 3.1B, 6.10.1.2 Modifying a Translation Table
14462306a36Sopenharmony_ci	 * Entry, Setting a Reference or Change Bit or Upgrading Access
14562306a36Sopenharmony_ci	 * Authority (PTE Subject to Atomic Hardware Updates):
14662306a36Sopenharmony_ci	 *
14762306a36Sopenharmony_ci	 * "If the only change being made to a valid PTE that is subject to
14862306a36Sopenharmony_ci	 *  atomic hardware updates is to set the Reference or Change bit to
14962306a36Sopenharmony_ci	 *  1 or to upgrade access authority, a simpler sequence suffices
15062306a36Sopenharmony_ci	 *  because the translation hardware will refetch the PTE if an
15162306a36Sopenharmony_ci	 *  access is attempted for which the only problems were reference
15262306a36Sopenharmony_ci	 *  and/or change bits needing to be set or insufficient access
15362306a36Sopenharmony_ci	 *  authority."
15462306a36Sopenharmony_ci	 *
15562306a36Sopenharmony_ci	 * The nest MMU in POWER9 does not perform this PTE re-fetch, but
15662306a36Sopenharmony_ci	 * it avoids the spurious fault problem by flushing the TLB before
15762306a36Sopenharmony_ci	 * upgrading PTE permissions, see radix__ptep_set_access_flags.
15862306a36Sopenharmony_ci	 */
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci
16162306a36Sopenharmony_cistatic inline bool __pte_protnone(unsigned long pte)
16262306a36Sopenharmony_ci{
16362306a36Sopenharmony_ci	return (pte & (pgprot_val(PAGE_NONE) | _PAGE_RWX)) == pgprot_val(PAGE_NONE);
16462306a36Sopenharmony_ci}
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_cistatic inline bool __pte_flags_need_flush(unsigned long oldval,
16762306a36Sopenharmony_ci					  unsigned long newval)
16862306a36Sopenharmony_ci{
16962306a36Sopenharmony_ci	unsigned long delta = oldval ^ newval;
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_ci	/*
17262306a36Sopenharmony_ci	 * The return value of this function doesn't matter for hash,
17362306a36Sopenharmony_ci	 * ptep_modify_prot_start() does a pte_update() which does or schedules
17462306a36Sopenharmony_ci	 * any necessary hash table update and flush.
17562306a36Sopenharmony_ci	 */
17662306a36Sopenharmony_ci	if (!radix_enabled())
17762306a36Sopenharmony_ci		return true;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	/*
18062306a36Sopenharmony_ci	 * We do not expect kernel mappings or non-PTEs or not-present PTEs.
18162306a36Sopenharmony_ci	 */
18262306a36Sopenharmony_ci	VM_WARN_ON_ONCE(!__pte_protnone(oldval) && oldval & _PAGE_PRIVILEGED);
18362306a36Sopenharmony_ci	VM_WARN_ON_ONCE(!__pte_protnone(newval) && newval & _PAGE_PRIVILEGED);
18462306a36Sopenharmony_ci	VM_WARN_ON_ONCE(!(oldval & _PAGE_PTE));
18562306a36Sopenharmony_ci	VM_WARN_ON_ONCE(!(newval & _PAGE_PTE));
18662306a36Sopenharmony_ci	VM_WARN_ON_ONCE(!(oldval & _PAGE_PRESENT));
18762306a36Sopenharmony_ci	VM_WARN_ON_ONCE(!(newval & _PAGE_PRESENT));
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	/*
19062306a36Sopenharmony_ci	*  Must flush on any change except READ, WRITE, EXEC, DIRTY, ACCESSED.
19162306a36Sopenharmony_ci	*
19262306a36Sopenharmony_ci	 * In theory, some changed software bits could be tolerated, in
19362306a36Sopenharmony_ci	 * practice those should rarely if ever matter.
19462306a36Sopenharmony_ci	 */
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci	if (delta & ~(_PAGE_RWX | _PAGE_DIRTY | _PAGE_ACCESSED))
19762306a36Sopenharmony_ci		return true;
19862306a36Sopenharmony_ci
19962306a36Sopenharmony_ci	/*
20062306a36Sopenharmony_ci	 * If any of the above was present in old but cleared in new, flush.
20162306a36Sopenharmony_ci	 * With the exception of _PAGE_ACCESSED, don't worry about flushing
20262306a36Sopenharmony_ci	 * if that was cleared (see the comment in ptep_clear_flush_young()).
20362306a36Sopenharmony_ci	 */
20462306a36Sopenharmony_ci	if ((delta & ~_PAGE_ACCESSED) & oldval)
20562306a36Sopenharmony_ci		return true;
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	return false;
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_cistatic inline bool pte_needs_flush(pte_t oldpte, pte_t newpte)
21162306a36Sopenharmony_ci{
21262306a36Sopenharmony_ci	return __pte_flags_need_flush(pte_val(oldpte), pte_val(newpte));
21362306a36Sopenharmony_ci}
21462306a36Sopenharmony_ci#define pte_needs_flush pte_needs_flush
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_cistatic inline bool huge_pmd_needs_flush(pmd_t oldpmd, pmd_t newpmd)
21762306a36Sopenharmony_ci{
21862306a36Sopenharmony_ci	return __pte_flags_need_flush(pmd_val(oldpmd), pmd_val(newpmd));
21962306a36Sopenharmony_ci}
22062306a36Sopenharmony_ci#define huge_pmd_needs_flush huge_pmd_needs_flush
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ciextern bool tlbie_capable;
22362306a36Sopenharmony_ciextern bool tlbie_enabled;
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_cistatic inline bool cputlb_use_tlbie(void)
22662306a36Sopenharmony_ci{
22762306a36Sopenharmony_ci	return tlbie_enabled;
22862306a36Sopenharmony_ci}
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci#endif /*  _ASM_POWERPC_BOOK3S_64_TLBFLUSH_H */
231