1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
217ed9e31SAneesh Kumar K.V #ifndef _ASM_POWERPC_NOHASH_PGTABLE_H
317ed9e31SAneesh Kumar K.V #define _ASM_POWERPC_NOHASH_PGTABLE_H
417ed9e31SAneesh Kumar K.V 
517ed9e31SAneesh Kumar K.V #if defined(CONFIG_PPC64)
617ed9e31SAneesh Kumar K.V #include <asm/nohash/64/pgtable.h>
717ed9e31SAneesh Kumar K.V #else
817ed9e31SAneesh Kumar K.V #include <asm/nohash/32/pgtable.h>
917ed9e31SAneesh Kumar K.V #endif
1017ed9e31SAneesh Kumar K.V 
1117ed9e31SAneesh Kumar K.V #ifndef __ASSEMBLY__
1217ed9e31SAneesh Kumar K.V 
1317ed9e31SAneesh Kumar K.V /* Generic accessors to PTE bits */
1417ed9e31SAneesh Kumar K.V static inline int pte_write(pte_t pte)
1517ed9e31SAneesh Kumar K.V {
1617ed9e31SAneesh Kumar K.V 	return (pte_val(pte) & (_PAGE_RW | _PAGE_RO)) != _PAGE_RO;
1717ed9e31SAneesh Kumar K.V }
18ca8afd40SChristophe Leroy static inline int pte_read(pte_t pte)		{ return 1; }
1917ed9e31SAneesh Kumar K.V static inline int pte_dirty(pte_t pte)		{ return pte_val(pte) & _PAGE_DIRTY; }
2017ed9e31SAneesh Kumar K.V static inline int pte_special(pte_t pte)	{ return pte_val(pte) & _PAGE_SPECIAL; }
2117ed9e31SAneesh Kumar K.V static inline int pte_none(pte_t pte)		{ return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; }
2217ed9e31SAneesh Kumar K.V static inline pgprot_t pte_pgprot(pte_t pte)	{ return __pgprot(pte_val(pte) & PAGE_PROT_BITS); }
2317ed9e31SAneesh Kumar K.V 
2417ed9e31SAneesh Kumar K.V #ifdef CONFIG_NUMA_BALANCING
2517ed9e31SAneesh Kumar K.V /*
2617ed9e31SAneesh Kumar K.V  * These work without NUMA balancing but the kernel does not care. See the
2717ed9e31SAneesh Kumar K.V  * comment in include/asm-generic/pgtable.h . On powerpc, this will only
2817ed9e31SAneesh Kumar K.V  * work for user pages and always return true for kernel pages.
2917ed9e31SAneesh Kumar K.V  */
3017ed9e31SAneesh Kumar K.V static inline int pte_protnone(pte_t pte)
3117ed9e31SAneesh Kumar K.V {
3217ed9e31SAneesh Kumar K.V 	return (pte_val(pte) &
3317ed9e31SAneesh Kumar K.V 		(_PAGE_PRESENT | _PAGE_USER)) == _PAGE_PRESENT;
3417ed9e31SAneesh Kumar K.V }
3517ed9e31SAneesh Kumar K.V 
3617ed9e31SAneesh Kumar K.V static inline int pmd_protnone(pmd_t pmd)
3717ed9e31SAneesh Kumar K.V {
3817ed9e31SAneesh Kumar K.V 	return pte_protnone(pmd_pte(pmd));
3917ed9e31SAneesh Kumar K.V }
4017ed9e31SAneesh Kumar K.V #endif /* CONFIG_NUMA_BALANCING */
4117ed9e31SAneesh Kumar K.V 
4217ed9e31SAneesh Kumar K.V static inline int pte_present(pte_t pte)
4317ed9e31SAneesh Kumar K.V {
4417ed9e31SAneesh Kumar K.V 	return pte_val(pte) & _PAGE_PRESENT;
4517ed9e31SAneesh Kumar K.V }
4617ed9e31SAneesh Kumar K.V 
475769beafSAneesh Kumar K.V /*
485769beafSAneesh Kumar K.V  * We only find page table entry in the last level
495769beafSAneesh Kumar K.V  * Hence no need for other accessors
505769beafSAneesh Kumar K.V  */
515769beafSAneesh Kumar K.V #define pte_access_permitted pte_access_permitted
525769beafSAneesh Kumar K.V static inline bool pte_access_permitted(pte_t pte, bool write)
535769beafSAneesh Kumar K.V {
545769beafSAneesh Kumar K.V 	/*
555769beafSAneesh Kumar K.V 	 * A read-only access is controlled by _PAGE_USER bit.
565769beafSAneesh Kumar K.V 	 * We have _PAGE_READ set for WRITE and EXECUTE
575769beafSAneesh Kumar K.V 	 */
58810e9f86SChristophe Leroy 	if (!pte_present(pte) || !pte_user(pte) || !pte_read(pte))
59810e9f86SChristophe Leroy 		return false;
605769beafSAneesh Kumar K.V 
61810e9f86SChristophe Leroy 	if (write && !pte_write(pte))
625769beafSAneesh Kumar K.V 		return false;
635769beafSAneesh Kumar K.V 
645769beafSAneesh Kumar K.V 	return true;
655769beafSAneesh Kumar K.V }
665769beafSAneesh Kumar K.V 
6717ed9e31SAneesh Kumar K.V /* Conversion functions: convert a page and protection to a page entry,
6817ed9e31SAneesh Kumar K.V  * and a page entry and page directory to the page they refer to.
6917ed9e31SAneesh Kumar K.V  *
7017ed9e31SAneesh Kumar K.V  * Even if PTEs can be unsigned long long, a PFN is always an unsigned
7117ed9e31SAneesh Kumar K.V  * long for now.
7217ed9e31SAneesh Kumar K.V  */
7317ed9e31SAneesh Kumar K.V static inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot) {
7417ed9e31SAneesh Kumar K.V 	return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) |
7517ed9e31SAneesh Kumar K.V 		     pgprot_val(pgprot)); }
7617ed9e31SAneesh Kumar K.V static inline unsigned long pte_pfn(pte_t pte)	{
7717ed9e31SAneesh Kumar K.V 	return pte_val(pte) >> PTE_RPN_SHIFT; }
7817ed9e31SAneesh Kumar K.V 
7917ed9e31SAneesh Kumar K.V /* Generic modifiers for PTE bits */
8017ed9e31SAneesh Kumar K.V static inline pte_t pte_wrprotect(pte_t pte)
8117ed9e31SAneesh Kumar K.V {
8217ed9e31SAneesh Kumar K.V 	pte_basic_t ptev;
8317ed9e31SAneesh Kumar K.V 
8417ed9e31SAneesh Kumar K.V 	ptev = pte_val(pte) & ~(_PAGE_RW | _PAGE_HWWRITE);
8517ed9e31SAneesh Kumar K.V 	ptev |= _PAGE_RO;
8617ed9e31SAneesh Kumar K.V 	return __pte(ptev);
8717ed9e31SAneesh Kumar K.V }
8817ed9e31SAneesh Kumar K.V 
8917ed9e31SAneesh Kumar K.V static inline pte_t pte_mkclean(pte_t pte)
9017ed9e31SAneesh Kumar K.V {
9117ed9e31SAneesh Kumar K.V 	return __pte(pte_val(pte) & ~(_PAGE_DIRTY | _PAGE_HWWRITE));
9217ed9e31SAneesh Kumar K.V }
9317ed9e31SAneesh Kumar K.V 
9417ed9e31SAneesh Kumar K.V static inline pte_t pte_mkold(pte_t pte)
9517ed9e31SAneesh Kumar K.V {
9617ed9e31SAneesh Kumar K.V 	return __pte(pte_val(pte) & ~_PAGE_ACCESSED);
9717ed9e31SAneesh Kumar K.V }
9817ed9e31SAneesh Kumar K.V 
9917ed9e31SAneesh Kumar K.V static inline pte_t pte_mkwrite(pte_t pte)
10017ed9e31SAneesh Kumar K.V {
10117ed9e31SAneesh Kumar K.V 	pte_basic_t ptev;
10217ed9e31SAneesh Kumar K.V 
10317ed9e31SAneesh Kumar K.V 	ptev = pte_val(pte) & ~_PAGE_RO;
10417ed9e31SAneesh Kumar K.V 	ptev |= _PAGE_RW;
10517ed9e31SAneesh Kumar K.V 	return __pte(ptev);
10617ed9e31SAneesh Kumar K.V }
10717ed9e31SAneesh Kumar K.V 
10817ed9e31SAneesh Kumar K.V static inline pte_t pte_mkdirty(pte_t pte)
10917ed9e31SAneesh Kumar K.V {
11017ed9e31SAneesh Kumar K.V 	return __pte(pte_val(pte) | _PAGE_DIRTY);
11117ed9e31SAneesh Kumar K.V }
11217ed9e31SAneesh Kumar K.V 
11317ed9e31SAneesh Kumar K.V static inline pte_t pte_mkyoung(pte_t pte)
11417ed9e31SAneesh Kumar K.V {
11517ed9e31SAneesh Kumar K.V 	return __pte(pte_val(pte) | _PAGE_ACCESSED);
11617ed9e31SAneesh Kumar K.V }
11717ed9e31SAneesh Kumar K.V 
11817ed9e31SAneesh Kumar K.V static inline pte_t pte_mkspecial(pte_t pte)
11917ed9e31SAneesh Kumar K.V {
12017ed9e31SAneesh Kumar K.V 	return __pte(pte_val(pte) | _PAGE_SPECIAL);
12117ed9e31SAneesh Kumar K.V }
12217ed9e31SAneesh Kumar K.V 
12317ed9e31SAneesh Kumar K.V static inline pte_t pte_mkhuge(pte_t pte)
12417ed9e31SAneesh Kumar K.V {
125de0f9387SChristophe Leroy 	return __pte(pte_val(pte) | _PAGE_HUGE);
12617ed9e31SAneesh Kumar K.V }
12717ed9e31SAneesh Kumar K.V 
12817ed9e31SAneesh Kumar K.V static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
12917ed9e31SAneesh Kumar K.V {
13017ed9e31SAneesh Kumar K.V 	return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
13117ed9e31SAneesh Kumar K.V }
13217ed9e31SAneesh Kumar K.V 
13317ed9e31SAneesh Kumar K.V /* Insert a PTE, top-level function is out of line. It uses an inline
13417ed9e31SAneesh Kumar K.V  * low level function in the respective pgtable-* files
13517ed9e31SAneesh Kumar K.V  */
13617ed9e31SAneesh Kumar K.V extern void set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
13717ed9e31SAneesh Kumar K.V 		       pte_t pte);
13817ed9e31SAneesh Kumar K.V 
13917ed9e31SAneesh Kumar K.V /* This low level function performs the actual PTE insertion
14017ed9e31SAneesh Kumar K.V  * Setting the PTE depends on the MMU type and other factors. It's
14117ed9e31SAneesh Kumar K.V  * an horrible mess that I'm not going to try to clean up now but
14217ed9e31SAneesh Kumar K.V  * I'm keeping it in one place rather than spread around
14317ed9e31SAneesh Kumar K.V  */
14417ed9e31SAneesh Kumar K.V static inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
14517ed9e31SAneesh Kumar K.V 				pte_t *ptep, pte_t pte, int percpu)
14617ed9e31SAneesh Kumar K.V {
14717ed9e31SAneesh Kumar K.V 	/* Second case is 32-bit with 64-bit PTE.  In this case, we
14817ed9e31SAneesh Kumar K.V 	 * can just store as long as we do the two halves in the right order
14945201c87SChristophe Leroy 	 * with a barrier in between.
15045201c87SChristophe Leroy 	 * In the percpu case, we also fallback to the simple update
15117ed9e31SAneesh Kumar K.V 	 */
152d5808ffaSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC32) && IS_ENABLED(CONFIG_PTE_64BIT) && !percpu) {
15317ed9e31SAneesh Kumar K.V 		__asm__ __volatile__("\
15417ed9e31SAneesh Kumar K.V 			stw%U0%X0 %2,%0\n\
15517ed9e31SAneesh Kumar K.V 			eieio\n\
15617ed9e31SAneesh Kumar K.V 			stw%U0%X0 %L2,%1"
15717ed9e31SAneesh Kumar K.V 		: "=m" (*ptep), "=m" (*((unsigned char *)ptep+4))
15817ed9e31SAneesh Kumar K.V 		: "r" (pte) : "memory");
159d5808ffaSChristophe Leroy 		return;
160d5808ffaSChristophe Leroy 	}
16117ed9e31SAneesh Kumar K.V 	/* Anything else just stores the PTE normally. That covers all 64-bit
16217ed9e31SAneesh Kumar K.V 	 * cases, and 32-bit non-hash with 32-bit PTEs.
16317ed9e31SAneesh Kumar K.V 	 */
16417ed9e31SAneesh Kumar K.V 	*ptep = pte;
16517ed9e31SAneesh Kumar K.V 
16617ed9e31SAneesh Kumar K.V 	/*
16717ed9e31SAneesh Kumar K.V 	 * With hardware tablewalk, a sync is needed to ensure that
16817ed9e31SAneesh Kumar K.V 	 * subsequent accesses see the PTE we just wrote.  Unlike userspace
16917ed9e31SAneesh Kumar K.V 	 * mappings, we can't tolerate spurious faults, so make sure
17017ed9e31SAneesh Kumar K.V 	 * the new PTE will be seen the first time.
17117ed9e31SAneesh Kumar K.V 	 */
172d5808ffaSChristophe Leroy 	if (IS_ENABLED(CONFIG_PPC_BOOK3E_64) && is_kernel_addr(addr))
17317ed9e31SAneesh Kumar K.V 		mb();
17417ed9e31SAneesh Kumar K.V }
17517ed9e31SAneesh Kumar K.V 
17617ed9e31SAneesh Kumar K.V 
17717ed9e31SAneesh Kumar K.V #define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
17817ed9e31SAneesh Kumar K.V extern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
17917ed9e31SAneesh Kumar K.V 				 pte_t *ptep, pte_t entry, int dirty);
18017ed9e31SAneesh Kumar K.V 
18117ed9e31SAneesh Kumar K.V /*
18217ed9e31SAneesh Kumar K.V  * Macro to mark a page protection value as "uncacheable".
18317ed9e31SAneesh Kumar K.V  */
18417ed9e31SAneesh Kumar K.V 
18517ed9e31SAneesh Kumar K.V #define _PAGE_CACHE_CTL	(_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \
18617ed9e31SAneesh Kumar K.V 			 _PAGE_WRITETHRU)
18717ed9e31SAneesh Kumar K.V 
18817ed9e31SAneesh Kumar K.V #define pgprot_noncached(prot)	  (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
18917ed9e31SAneesh Kumar K.V 				            _PAGE_NO_CACHE | _PAGE_GUARDED))
19017ed9e31SAneesh Kumar K.V 
19117ed9e31SAneesh Kumar K.V #define pgprot_noncached_wc(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
19217ed9e31SAneesh Kumar K.V 				            _PAGE_NO_CACHE))
19317ed9e31SAneesh Kumar K.V 
19417ed9e31SAneesh Kumar K.V #define pgprot_cached(prot)       (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
19517ed9e31SAneesh Kumar K.V 				            _PAGE_COHERENT))
19617ed9e31SAneesh Kumar K.V 
1975f356497SChristophe Leroy #if _PAGE_WRITETHRU != 0
19817ed9e31SAneesh Kumar K.V #define pgprot_cached_wthru(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
19917ed9e31SAneesh Kumar K.V 				            _PAGE_COHERENT | _PAGE_WRITETHRU))
2005f356497SChristophe Leroy #endif
20117ed9e31SAneesh Kumar K.V 
20217ed9e31SAneesh Kumar K.V #define pgprot_cached_noncoherent(prot) \
20317ed9e31SAneesh Kumar K.V 		(__pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL))
20417ed9e31SAneesh Kumar K.V 
20517ed9e31SAneesh Kumar K.V #define pgprot_writecombine pgprot_noncached_wc
20617ed9e31SAneesh Kumar K.V 
20717ed9e31SAneesh Kumar K.V struct file;
20817ed9e31SAneesh Kumar K.V extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
20917ed9e31SAneesh Kumar K.V 				     unsigned long size, pgprot_t vma_prot);
21017ed9e31SAneesh Kumar K.V #define __HAVE_PHYS_MEM_ACCESS_PROT
21117ed9e31SAneesh Kumar K.V 
21226a344aeSAneesh Kumar K.V #ifdef CONFIG_HUGETLB_PAGE
21326a344aeSAneesh Kumar K.V static inline int hugepd_ok(hugepd_t hpd)
21426a344aeSAneesh Kumar K.V {
2154b914286SChristophe Leroy #ifdef CONFIG_PPC_8xx
21620717e1fSAneesh Kumar K.V 	return ((hpd_val(hpd) & 0x4) != 0);
2174b914286SChristophe Leroy #else
21820717e1fSAneesh Kumar K.V 	/* We clear the top bit to indicate hugepd */
2193fb66a70SLaurentiu Tudor 	return (hpd_val(hpd) && (hpd_val(hpd) & PD_HUGE) == 0);
2204b914286SChristophe Leroy #endif
22126a344aeSAneesh Kumar K.V }
22226a344aeSAneesh Kumar K.V 
22326a344aeSAneesh Kumar K.V static inline int pmd_huge(pmd_t pmd)
22426a344aeSAneesh Kumar K.V {
22526a344aeSAneesh Kumar K.V 	return 0;
22626a344aeSAneesh Kumar K.V }
22726a344aeSAneesh Kumar K.V 
22826a344aeSAneesh Kumar K.V static inline int pud_huge(pud_t pud)
22926a344aeSAneesh Kumar K.V {
23026a344aeSAneesh Kumar K.V 	return 0;
23126a344aeSAneesh Kumar K.V }
23226a344aeSAneesh Kumar K.V 
23326a344aeSAneesh Kumar K.V static inline int pgd_huge(pgd_t pgd)
23426a344aeSAneesh Kumar K.V {
23526a344aeSAneesh Kumar K.V 	return 0;
23626a344aeSAneesh Kumar K.V }
23726a344aeSAneesh Kumar K.V #define pgd_huge		pgd_huge
23826a344aeSAneesh Kumar K.V 
23926a344aeSAneesh Kumar K.V #define is_hugepd(hpd)		(hugepd_ok(hpd))
24026a344aeSAneesh Kumar K.V #endif
24126a344aeSAneesh Kumar K.V 
24217ed9e31SAneesh Kumar K.V #endif /* __ASSEMBLY__ */
24317ed9e31SAneesh Kumar K.V #endif
244