162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci#ifndef _ASM_POWERPC_NOHASH_PGTABLE_H
362306a36Sopenharmony_ci#define _ASM_POWERPC_NOHASH_PGTABLE_H
462306a36Sopenharmony_ci
562306a36Sopenharmony_ci#if defined(CONFIG_PPC64)
662306a36Sopenharmony_ci#include <asm/nohash/64/pgtable.h>
762306a36Sopenharmony_ci#else
862306a36Sopenharmony_ci#include <asm/nohash/32/pgtable.h>
962306a36Sopenharmony_ci#endif
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/* Permission masks used for kernel mappings */
1262306a36Sopenharmony_ci#define PAGE_KERNEL	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RW)
1362306a36Sopenharmony_ci#define PAGE_KERNEL_NC	__pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE)
1462306a36Sopenharmony_ci#define PAGE_KERNEL_NCG	__pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE | _PAGE_GUARDED)
1562306a36Sopenharmony_ci#define PAGE_KERNEL_X	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX)
1662306a36Sopenharmony_ci#define PAGE_KERNEL_RO	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RO)
1762306a36Sopenharmony_ci#define PAGE_KERNEL_ROX	__pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#ifndef __ASSEMBLY__
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/* Generic accessors to PTE bits */
2262306a36Sopenharmony_ci#ifndef pte_write
2362306a36Sopenharmony_cistatic inline int pte_write(pte_t pte)
2462306a36Sopenharmony_ci{
2562306a36Sopenharmony_ci	return pte_val(pte) & _PAGE_RW;
2662306a36Sopenharmony_ci}
2762306a36Sopenharmony_ci#endif
2862306a36Sopenharmony_ci#ifndef pte_read
2962306a36Sopenharmony_cistatic inline int pte_read(pte_t pte)		{ return 1; }
3062306a36Sopenharmony_ci#endif
3162306a36Sopenharmony_cistatic inline int pte_dirty(pte_t pte)		{ return pte_val(pte) & _PAGE_DIRTY; }
3262306a36Sopenharmony_cistatic inline int pte_special(pte_t pte)	{ return pte_val(pte) & _PAGE_SPECIAL; }
3362306a36Sopenharmony_cistatic inline int pte_none(pte_t pte)		{ return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; }
3462306a36Sopenharmony_cistatic inline bool pte_hashpte(pte_t pte)	{ return false; }
3562306a36Sopenharmony_cistatic inline bool pte_ci(pte_t pte)		{ return pte_val(pte) & _PAGE_NO_CACHE; }
3662306a36Sopenharmony_cistatic inline bool pte_exec(pte_t pte)		{ return pte_val(pte) & _PAGE_EXEC; }
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci#ifdef CONFIG_NUMA_BALANCING
3962306a36Sopenharmony_ci/*
4062306a36Sopenharmony_ci * These work without NUMA balancing but the kernel does not care. See the
4162306a36Sopenharmony_ci * comment in include/linux/pgtable.h . On powerpc, this will only
4262306a36Sopenharmony_ci * work for user pages and always return true for kernel pages.
4362306a36Sopenharmony_ci */
4462306a36Sopenharmony_cistatic inline int pte_protnone(pte_t pte)
4562306a36Sopenharmony_ci{
4662306a36Sopenharmony_ci	return pte_present(pte) && !pte_user(pte);
4762306a36Sopenharmony_ci}
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_cistatic inline int pmd_protnone(pmd_t pmd)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	return pte_protnone(pmd_pte(pmd));
5262306a36Sopenharmony_ci}
5362306a36Sopenharmony_ci#endif /* CONFIG_NUMA_BALANCING */
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_cistatic inline int pte_present(pte_t pte)
5662306a36Sopenharmony_ci{
5762306a36Sopenharmony_ci	return pte_val(pte) & _PAGE_PRESENT;
5862306a36Sopenharmony_ci}
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_cistatic inline bool pte_hw_valid(pte_t pte)
6162306a36Sopenharmony_ci{
6262306a36Sopenharmony_ci	return pte_val(pte) & _PAGE_PRESENT;
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci/*
6662306a36Sopenharmony_ci * Don't just check for any non zero bits in __PAGE_USER, since for book3e
6762306a36Sopenharmony_ci * and PTE_64BIT, PAGE_KERNEL_X contains _PAGE_BAP_SR which is also in
6862306a36Sopenharmony_ci * _PAGE_USER.  Need to explicitly match _PAGE_BAP_UR bit in that case too.
6962306a36Sopenharmony_ci */
7062306a36Sopenharmony_ci#ifndef pte_user
7162306a36Sopenharmony_cistatic inline bool pte_user(pte_t pte)
7262306a36Sopenharmony_ci{
7362306a36Sopenharmony_ci	return (pte_val(pte) & _PAGE_USER) == _PAGE_USER;
7462306a36Sopenharmony_ci}
7562306a36Sopenharmony_ci#endif
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci/*
7862306a36Sopenharmony_ci * We only find page table entry in the last level
7962306a36Sopenharmony_ci * Hence no need for other accessors
8062306a36Sopenharmony_ci */
8162306a36Sopenharmony_ci#define pte_access_permitted pte_access_permitted
8262306a36Sopenharmony_cistatic inline bool pte_access_permitted(pte_t pte, bool write)
8362306a36Sopenharmony_ci{
8462306a36Sopenharmony_ci	/*
8562306a36Sopenharmony_ci	 * A read-only access is controlled by _PAGE_USER bit.
8662306a36Sopenharmony_ci	 * We have _PAGE_READ set for WRITE and EXECUTE
8762306a36Sopenharmony_ci	 */
8862306a36Sopenharmony_ci	if (!pte_present(pte) || !pte_user(pte) || !pte_read(pte))
8962306a36Sopenharmony_ci		return false;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	if (write && !pte_write(pte))
9262306a36Sopenharmony_ci		return false;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	return true;
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci/* Conversion functions: convert a page and protection to a page entry,
9862306a36Sopenharmony_ci * and a page entry and page directory to the page they refer to.
9962306a36Sopenharmony_ci *
10062306a36Sopenharmony_ci * Even if PTEs can be unsigned long long, a PFN is always an unsigned
10162306a36Sopenharmony_ci * long for now.
10262306a36Sopenharmony_ci */
10362306a36Sopenharmony_cistatic inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot) {
10462306a36Sopenharmony_ci	return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) |
10562306a36Sopenharmony_ci		     pgprot_val(pgprot)); }
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci/* Generic modifiers for PTE bits */
10862306a36Sopenharmony_cistatic inline pte_t pte_exprotect(pte_t pte)
10962306a36Sopenharmony_ci{
11062306a36Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_EXEC);
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_cistatic inline pte_t pte_mkclean(pte_t pte)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_DIRTY);
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_cistatic inline pte_t pte_mkold(pte_t pte)
11962306a36Sopenharmony_ci{
12062306a36Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_ACCESSED);
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_cistatic inline pte_t pte_mkspecial(pte_t pte)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	return __pte(pte_val(pte) | _PAGE_SPECIAL);
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ci#ifndef pte_mkhuge
12962306a36Sopenharmony_cistatic inline pte_t pte_mkhuge(pte_t pte)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	return __pte(pte_val(pte));
13262306a36Sopenharmony_ci}
13362306a36Sopenharmony_ci#endif
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci#ifndef pte_mkprivileged
13662306a36Sopenharmony_cistatic inline pte_t pte_mkprivileged(pte_t pte)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_USER);
13962306a36Sopenharmony_ci}
14062306a36Sopenharmony_ci#endif
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci#ifndef pte_mkuser
14362306a36Sopenharmony_cistatic inline pte_t pte_mkuser(pte_t pte)
14462306a36Sopenharmony_ci{
14562306a36Sopenharmony_ci	return __pte(pte_val(pte) | _PAGE_USER);
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_ci#endif
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cistatic inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cistatic inline int pte_swp_exclusive(pte_t pte)
15562306a36Sopenharmony_ci{
15662306a36Sopenharmony_ci	return pte_val(pte) & _PAGE_SWP_EXCLUSIVE;
15762306a36Sopenharmony_ci}
15862306a36Sopenharmony_ci
15962306a36Sopenharmony_cistatic inline pte_t pte_swp_mkexclusive(pte_t pte)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	return __pte(pte_val(pte) | _PAGE_SWP_EXCLUSIVE);
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cistatic inline pte_t pte_swp_clear_exclusive(pte_t pte)
16562306a36Sopenharmony_ci{
16662306a36Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_SWP_EXCLUSIVE);
16762306a36Sopenharmony_ci}
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci/* This low level function performs the actual PTE insertion
17062306a36Sopenharmony_ci * Setting the PTE depends on the MMU type and other factors. It's
17162306a36Sopenharmony_ci * an horrible mess that I'm not going to try to clean up now but
17262306a36Sopenharmony_ci * I'm keeping it in one place rather than spread around
17362306a36Sopenharmony_ci */
17462306a36Sopenharmony_cistatic inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
17562306a36Sopenharmony_ci				pte_t *ptep, pte_t pte, int percpu)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	/* Second case is 32-bit with 64-bit PTE.  In this case, we
17862306a36Sopenharmony_ci	 * can just store as long as we do the two halves in the right order
17962306a36Sopenharmony_ci	 * with a barrier in between.
18062306a36Sopenharmony_ci	 * In the percpu case, we also fallback to the simple update
18162306a36Sopenharmony_ci	 */
18262306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_PPC32) && IS_ENABLED(CONFIG_PTE_64BIT) && !percpu) {
18362306a36Sopenharmony_ci		__asm__ __volatile__("\
18462306a36Sopenharmony_ci			stw%X0 %2,%0\n\
18562306a36Sopenharmony_ci			mbar\n\
18662306a36Sopenharmony_ci			stw%X1 %L2,%1"
18762306a36Sopenharmony_ci		: "=m" (*ptep), "=m" (*((unsigned char *)ptep+4))
18862306a36Sopenharmony_ci		: "r" (pte) : "memory");
18962306a36Sopenharmony_ci		return;
19062306a36Sopenharmony_ci	}
19162306a36Sopenharmony_ci	/* Anything else just stores the PTE normally. That covers all 64-bit
19262306a36Sopenharmony_ci	 * cases, and 32-bit non-hash with 32-bit PTEs.
19362306a36Sopenharmony_ci	 */
19462306a36Sopenharmony_ci#if defined(CONFIG_PPC_8xx) && defined(CONFIG_PPC_16K_PAGES)
19562306a36Sopenharmony_ci	ptep->pte3 = ptep->pte2 = ptep->pte1 = ptep->pte = pte_val(pte);
19662306a36Sopenharmony_ci#else
19762306a36Sopenharmony_ci	*ptep = pte;
19862306a36Sopenharmony_ci#endif
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	/*
20162306a36Sopenharmony_ci	 * With hardware tablewalk, a sync is needed to ensure that
20262306a36Sopenharmony_ci	 * subsequent accesses see the PTE we just wrote.  Unlike userspace
20362306a36Sopenharmony_ci	 * mappings, we can't tolerate spurious faults, so make sure
20462306a36Sopenharmony_ci	 * the new PTE will be seen the first time.
20562306a36Sopenharmony_ci	 */
20662306a36Sopenharmony_ci	if (IS_ENABLED(CONFIG_PPC_BOOK3E_64) && is_kernel_addr(addr))
20762306a36Sopenharmony_ci		mb();
20862306a36Sopenharmony_ci}
20962306a36Sopenharmony_ci
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
21262306a36Sopenharmony_ciextern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
21362306a36Sopenharmony_ci				 pte_t *ptep, pte_t entry, int dirty);
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ci/*
21662306a36Sopenharmony_ci * Macro to mark a page protection value as "uncacheable".
21762306a36Sopenharmony_ci */
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci#define _PAGE_CACHE_CTL	(_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \
22062306a36Sopenharmony_ci			 _PAGE_WRITETHRU)
22162306a36Sopenharmony_ci
22262306a36Sopenharmony_ci#define pgprot_noncached(prot)	  (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
22362306a36Sopenharmony_ci				            _PAGE_NO_CACHE | _PAGE_GUARDED))
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci#define pgprot_noncached_wc(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
22662306a36Sopenharmony_ci				            _PAGE_NO_CACHE))
22762306a36Sopenharmony_ci
22862306a36Sopenharmony_ci#define pgprot_cached(prot)       (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
22962306a36Sopenharmony_ci				            _PAGE_COHERENT))
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci#if _PAGE_WRITETHRU != 0
23262306a36Sopenharmony_ci#define pgprot_cached_wthru(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
23362306a36Sopenharmony_ci				            _PAGE_COHERENT | _PAGE_WRITETHRU))
23462306a36Sopenharmony_ci#else
23562306a36Sopenharmony_ci#define pgprot_cached_wthru(prot)	pgprot_noncached(prot)
23662306a36Sopenharmony_ci#endif
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci#define pgprot_cached_noncoherent(prot) \
23962306a36Sopenharmony_ci		(__pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL))
24062306a36Sopenharmony_ci
24162306a36Sopenharmony_ci#define pgprot_writecombine pgprot_noncached_wc
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_cistruct file;
24462306a36Sopenharmony_ciextern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
24562306a36Sopenharmony_ci				     unsigned long size, pgprot_t vma_prot);
24662306a36Sopenharmony_ci#define __HAVE_PHYS_MEM_ACCESS_PROT
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE
24962306a36Sopenharmony_cistatic inline int hugepd_ok(hugepd_t hpd)
25062306a36Sopenharmony_ci{
25162306a36Sopenharmony_ci#ifdef CONFIG_PPC_8xx
25262306a36Sopenharmony_ci	return ((hpd_val(hpd) & _PMD_PAGE_MASK) == _PMD_PAGE_8M);
25362306a36Sopenharmony_ci#else
25462306a36Sopenharmony_ci	/* We clear the top bit to indicate hugepd */
25562306a36Sopenharmony_ci	return (hpd_val(hpd) && (hpd_val(hpd) & PD_HUGE) == 0);
25662306a36Sopenharmony_ci#endif
25762306a36Sopenharmony_ci}
25862306a36Sopenharmony_ci
25962306a36Sopenharmony_cistatic inline int pmd_huge(pmd_t pmd)
26062306a36Sopenharmony_ci{
26162306a36Sopenharmony_ci	return 0;
26262306a36Sopenharmony_ci}
26362306a36Sopenharmony_ci
26462306a36Sopenharmony_cistatic inline int pud_huge(pud_t pud)
26562306a36Sopenharmony_ci{
26662306a36Sopenharmony_ci	return 0;
26762306a36Sopenharmony_ci}
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci#define is_hugepd(hpd)		(hugepd_ok(hpd))
27062306a36Sopenharmony_ci#endif
27162306a36Sopenharmony_ci
27262306a36Sopenharmony_ci/*
27362306a36Sopenharmony_ci * This gets called at the end of handling a page fault, when
27462306a36Sopenharmony_ci * the kernel has put a new PTE into the page table for the process.
27562306a36Sopenharmony_ci * We use it to ensure coherency between the i-cache and d-cache
27662306a36Sopenharmony_ci * for the page which has just been mapped in.
27762306a36Sopenharmony_ci */
27862306a36Sopenharmony_ci#if defined(CONFIG_PPC_E500) && defined(CONFIG_HUGETLB_PAGE)
27962306a36Sopenharmony_civoid update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
28062306a36Sopenharmony_ci		unsigned long address, pte_t *ptep, unsigned int nr);
28162306a36Sopenharmony_ci#else
28262306a36Sopenharmony_cistatic inline void update_mmu_cache_range(struct vm_fault *vmf,
28362306a36Sopenharmony_ci		struct vm_area_struct *vma, unsigned long address,
28462306a36Sopenharmony_ci		pte_t *ptep, unsigned int nr) {}
28562306a36Sopenharmony_ci#endif
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_ci#endif /* __ASSEMBLY__ */
28862306a36Sopenharmony_ci#endif
289