18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci#ifndef _ASM_POWERPC_NOHASH_PGTABLE_H
38c2ecf20Sopenharmony_ci#define _ASM_POWERPC_NOHASH_PGTABLE_H
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC64)
68c2ecf20Sopenharmony_ci#include <asm/nohash/64/pgtable.h>
78c2ecf20Sopenharmony_ci#else
88c2ecf20Sopenharmony_ci#include <asm/nohash/32/pgtable.h>
98c2ecf20Sopenharmony_ci#endif
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci/* Permission masks used for kernel mappings */
128c2ecf20Sopenharmony_ci#define PAGE_KERNEL	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RW)
138c2ecf20Sopenharmony_ci#define PAGE_KERNEL_NC	__pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | _PAGE_NO_CACHE)
148c2ecf20Sopenharmony_ci#define PAGE_KERNEL_NCG	__pgprot(_PAGE_BASE_NC | _PAGE_KERNEL_RW | \
158c2ecf20Sopenharmony_ci				 _PAGE_NO_CACHE | _PAGE_GUARDED)
168c2ecf20Sopenharmony_ci#define PAGE_KERNEL_X	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RWX)
178c2ecf20Sopenharmony_ci#define PAGE_KERNEL_RO	__pgprot(_PAGE_BASE | _PAGE_KERNEL_RO)
188c2ecf20Sopenharmony_ci#define PAGE_KERNEL_ROX	__pgprot(_PAGE_BASE | _PAGE_KERNEL_ROX)
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/*
218c2ecf20Sopenharmony_ci * Protection used for kernel text. We want the debuggers to be able to
228c2ecf20Sopenharmony_ci * set breakpoints anywhere, so don't write protect the kernel text
238c2ecf20Sopenharmony_ci * on platforms where such control is possible.
248c2ecf20Sopenharmony_ci */
258c2ecf20Sopenharmony_ci#if defined(CONFIG_KGDB) || defined(CONFIG_XMON) || defined(CONFIG_BDI_SWITCH) ||\
268c2ecf20Sopenharmony_ci	defined(CONFIG_KPROBES) || defined(CONFIG_DYNAMIC_FTRACE)
278c2ecf20Sopenharmony_ci#define PAGE_KERNEL_TEXT	PAGE_KERNEL_X
288c2ecf20Sopenharmony_ci#else
298c2ecf20Sopenharmony_ci#define PAGE_KERNEL_TEXT	PAGE_KERNEL_ROX
308c2ecf20Sopenharmony_ci#endif
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Make modules code happy. We don't set RO yet */
338c2ecf20Sopenharmony_ci#define PAGE_KERNEL_EXEC	PAGE_KERNEL_X
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* Advertise special mapping type for AGP */
368c2ecf20Sopenharmony_ci#define PAGE_AGP		(PAGE_KERNEL_NC)
378c2ecf20Sopenharmony_ci#define HAVE_PAGE_AGP
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#ifndef __ASSEMBLY__
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Generic accessors to PTE bits */
428c2ecf20Sopenharmony_ci#ifndef pte_write
438c2ecf20Sopenharmony_cistatic inline int pte_write(pte_t pte)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	return pte_val(pte) & _PAGE_RW;
468c2ecf20Sopenharmony_ci}
478c2ecf20Sopenharmony_ci#endif
488c2ecf20Sopenharmony_ci#ifndef pte_read
498c2ecf20Sopenharmony_cistatic inline int pte_read(pte_t pte)		{ return 1; }
508c2ecf20Sopenharmony_ci#endif
518c2ecf20Sopenharmony_cistatic inline int pte_dirty(pte_t pte)		{ return pte_val(pte) & _PAGE_DIRTY; }
528c2ecf20Sopenharmony_cistatic inline int pte_special(pte_t pte)	{ return pte_val(pte) & _PAGE_SPECIAL; }
538c2ecf20Sopenharmony_cistatic inline int pte_none(pte_t pte)		{ return (pte_val(pte) & ~_PTE_NONE_MASK) == 0; }
548c2ecf20Sopenharmony_cistatic inline bool pte_hashpte(pte_t pte)	{ return false; }
558c2ecf20Sopenharmony_cistatic inline bool pte_ci(pte_t pte)		{ return pte_val(pte) & _PAGE_NO_CACHE; }
568c2ecf20Sopenharmony_cistatic inline bool pte_exec(pte_t pte)		{ return pte_val(pte) & _PAGE_EXEC; }
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA_BALANCING
598c2ecf20Sopenharmony_ci/*
608c2ecf20Sopenharmony_ci * These work without NUMA balancing but the kernel does not care. See the
618c2ecf20Sopenharmony_ci * comment in include/linux/pgtable.h . On powerpc, this will only
628c2ecf20Sopenharmony_ci * work for user pages and always return true for kernel pages.
638c2ecf20Sopenharmony_ci */
648c2ecf20Sopenharmony_cistatic inline int pte_protnone(pte_t pte)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	return pte_present(pte) && !pte_user(pte);
678c2ecf20Sopenharmony_ci}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic inline int pmd_protnone(pmd_t pmd)
708c2ecf20Sopenharmony_ci{
718c2ecf20Sopenharmony_ci	return pte_protnone(pmd_pte(pmd));
728c2ecf20Sopenharmony_ci}
738c2ecf20Sopenharmony_ci#endif /* CONFIG_NUMA_BALANCING */
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_cistatic inline int pte_present(pte_t pte)
768c2ecf20Sopenharmony_ci{
778c2ecf20Sopenharmony_ci	return pte_val(pte) & _PAGE_PRESENT;
788c2ecf20Sopenharmony_ci}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic inline bool pte_hw_valid(pte_t pte)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	return pte_val(pte) & _PAGE_PRESENT;
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Don't just check for any non zero bits in __PAGE_USER, since for book3e
878c2ecf20Sopenharmony_ci * and PTE_64BIT, PAGE_KERNEL_X contains _PAGE_BAP_SR which is also in
888c2ecf20Sopenharmony_ci * _PAGE_USER.  Need to explicitly match _PAGE_BAP_UR bit in that case too.
898c2ecf20Sopenharmony_ci */
908c2ecf20Sopenharmony_ci#ifndef pte_user
918c2ecf20Sopenharmony_cistatic inline bool pte_user(pte_t pte)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	return (pte_val(pte) & _PAGE_USER) == _PAGE_USER;
948c2ecf20Sopenharmony_ci}
958c2ecf20Sopenharmony_ci#endif
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/*
988c2ecf20Sopenharmony_ci * We only find page table entry in the last level
998c2ecf20Sopenharmony_ci * Hence no need for other accessors
1008c2ecf20Sopenharmony_ci */
1018c2ecf20Sopenharmony_ci#define pte_access_permitted pte_access_permitted
1028c2ecf20Sopenharmony_cistatic inline bool pte_access_permitted(pte_t pte, bool write)
1038c2ecf20Sopenharmony_ci{
1048c2ecf20Sopenharmony_ci	/*
1058c2ecf20Sopenharmony_ci	 * A read-only access is controlled by _PAGE_USER bit.
1068c2ecf20Sopenharmony_ci	 * We have _PAGE_READ set for WRITE and EXECUTE
1078c2ecf20Sopenharmony_ci	 */
1088c2ecf20Sopenharmony_ci	if (!pte_present(pte) || !pte_user(pte) || !pte_read(pte))
1098c2ecf20Sopenharmony_ci		return false;
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	if (write && !pte_write(pte))
1128c2ecf20Sopenharmony_ci		return false;
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci	return true;
1158c2ecf20Sopenharmony_ci}
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* Conversion functions: convert a page and protection to a page entry,
1188c2ecf20Sopenharmony_ci * and a page entry and page directory to the page they refer to.
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci * Even if PTEs can be unsigned long long, a PFN is always an unsigned
1218c2ecf20Sopenharmony_ci * long for now.
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_cistatic inline pte_t pfn_pte(unsigned long pfn, pgprot_t pgprot) {
1248c2ecf20Sopenharmony_ci	return __pte(((pte_basic_t)(pfn) << PTE_RPN_SHIFT) |
1258c2ecf20Sopenharmony_ci		     pgprot_val(pgprot)); }
1268c2ecf20Sopenharmony_cistatic inline unsigned long pte_pfn(pte_t pte)	{
1278c2ecf20Sopenharmony_ci	return pte_val(pte) >> PTE_RPN_SHIFT; }
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/* Generic modifiers for PTE bits */
1308c2ecf20Sopenharmony_cistatic inline pte_t pte_exprotect(pte_t pte)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_EXEC);
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic inline pte_t pte_mkclean(pte_t pte)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_DIRTY);
1388c2ecf20Sopenharmony_ci}
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_cistatic inline pte_t pte_mkold(pte_t pte)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_ACCESSED);
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_cistatic inline pte_t pte_mkspecial(pte_t pte)
1468c2ecf20Sopenharmony_ci{
1478c2ecf20Sopenharmony_ci	return __pte(pte_val(pte) | _PAGE_SPECIAL);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci#ifndef pte_mkhuge
1518c2ecf20Sopenharmony_cistatic inline pte_t pte_mkhuge(pte_t pte)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	return __pte(pte_val(pte));
1548c2ecf20Sopenharmony_ci}
1558c2ecf20Sopenharmony_ci#endif
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci#ifndef pte_mkprivileged
1588c2ecf20Sopenharmony_cistatic inline pte_t pte_mkprivileged(pte_t pte)
1598c2ecf20Sopenharmony_ci{
1608c2ecf20Sopenharmony_ci	return __pte(pte_val(pte) & ~_PAGE_USER);
1618c2ecf20Sopenharmony_ci}
1628c2ecf20Sopenharmony_ci#endif
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci#ifndef pte_mkuser
1658c2ecf20Sopenharmony_cistatic inline pte_t pte_mkuser(pte_t pte)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	return __pte(pte_val(pte) | _PAGE_USER);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci#endif
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	return __pte((pte_val(pte) & _PAGE_CHG_MASK) | pgprot_val(newprot));
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci/* Insert a PTE, top-level function is out of line. It uses an inline
1778c2ecf20Sopenharmony_ci * low level function in the respective pgtable-* files
1788c2ecf20Sopenharmony_ci */
1798c2ecf20Sopenharmony_ciextern void set_pte_at(struct mm_struct *mm, unsigned long addr, pte_t *ptep,
1808c2ecf20Sopenharmony_ci		       pte_t pte);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/* This low level function performs the actual PTE insertion
1838c2ecf20Sopenharmony_ci * Setting the PTE depends on the MMU type and other factors. It's
1848c2ecf20Sopenharmony_ci * an horrible mess that I'm not going to try to clean up now but
1858c2ecf20Sopenharmony_ci * I'm keeping it in one place rather than spread around
1868c2ecf20Sopenharmony_ci */
1878c2ecf20Sopenharmony_cistatic inline void __set_pte_at(struct mm_struct *mm, unsigned long addr,
1888c2ecf20Sopenharmony_ci				pte_t *ptep, pte_t pte, int percpu)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	/* Second case is 32-bit with 64-bit PTE.  In this case, we
1918c2ecf20Sopenharmony_ci	 * can just store as long as we do the two halves in the right order
1928c2ecf20Sopenharmony_ci	 * with a barrier in between.
1938c2ecf20Sopenharmony_ci	 * In the percpu case, we also fallback to the simple update
1948c2ecf20Sopenharmony_ci	 */
1958c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_PPC32) && IS_ENABLED(CONFIG_PTE_64BIT) && !percpu) {
1968c2ecf20Sopenharmony_ci		__asm__ __volatile__("\
1978c2ecf20Sopenharmony_ci			stw%X0 %2,%0\n\
1988c2ecf20Sopenharmony_ci			eieio\n\
1998c2ecf20Sopenharmony_ci			stw%X1 %L2,%1"
2008c2ecf20Sopenharmony_ci		: "=m" (*ptep), "=m" (*((unsigned char *)ptep+4))
2018c2ecf20Sopenharmony_ci		: "r" (pte) : "memory");
2028c2ecf20Sopenharmony_ci		return;
2038c2ecf20Sopenharmony_ci	}
2048c2ecf20Sopenharmony_ci	/* Anything else just stores the PTE normally. That covers all 64-bit
2058c2ecf20Sopenharmony_ci	 * cases, and 32-bit non-hash with 32-bit PTEs.
2068c2ecf20Sopenharmony_ci	 */
2078c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC_8xx) && defined(CONFIG_PPC_16K_PAGES)
2088c2ecf20Sopenharmony_ci	ptep->pte = ptep->pte1 = ptep->pte2 = ptep->pte3 = pte_val(pte);
2098c2ecf20Sopenharmony_ci#else
2108c2ecf20Sopenharmony_ci	*ptep = pte;
2118c2ecf20Sopenharmony_ci#endif
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	/*
2148c2ecf20Sopenharmony_ci	 * With hardware tablewalk, a sync is needed to ensure that
2158c2ecf20Sopenharmony_ci	 * subsequent accesses see the PTE we just wrote.  Unlike userspace
2168c2ecf20Sopenharmony_ci	 * mappings, we can't tolerate spurious faults, so make sure
2178c2ecf20Sopenharmony_ci	 * the new PTE will be seen the first time.
2188c2ecf20Sopenharmony_ci	 */
2198c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_PPC_BOOK3E_64) && is_kernel_addr(addr))
2208c2ecf20Sopenharmony_ci		mb();
2218c2ecf20Sopenharmony_ci}
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci#define __HAVE_ARCH_PTEP_SET_ACCESS_FLAGS
2258c2ecf20Sopenharmony_ciextern int ptep_set_access_flags(struct vm_area_struct *vma, unsigned long address,
2268c2ecf20Sopenharmony_ci				 pte_t *ptep, pte_t entry, int dirty);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci/*
2298c2ecf20Sopenharmony_ci * Macro to mark a page protection value as "uncacheable".
2308c2ecf20Sopenharmony_ci */
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci#define _PAGE_CACHE_CTL	(_PAGE_COHERENT | _PAGE_GUARDED | _PAGE_NO_CACHE | \
2338c2ecf20Sopenharmony_ci			 _PAGE_WRITETHRU)
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci#define pgprot_noncached(prot)	  (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
2368c2ecf20Sopenharmony_ci				            _PAGE_NO_CACHE | _PAGE_GUARDED))
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci#define pgprot_noncached_wc(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
2398c2ecf20Sopenharmony_ci				            _PAGE_NO_CACHE))
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci#define pgprot_cached(prot)       (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
2428c2ecf20Sopenharmony_ci				            _PAGE_COHERENT))
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci#if _PAGE_WRITETHRU != 0
2458c2ecf20Sopenharmony_ci#define pgprot_cached_wthru(prot) (__pgprot((pgprot_val(prot) & ~_PAGE_CACHE_CTL) | \
2468c2ecf20Sopenharmony_ci				            _PAGE_COHERENT | _PAGE_WRITETHRU))
2478c2ecf20Sopenharmony_ci#else
2488c2ecf20Sopenharmony_ci#define pgprot_cached_wthru(prot)	pgprot_noncached(prot)
2498c2ecf20Sopenharmony_ci#endif
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci#define pgprot_cached_noncoherent(prot) \
2528c2ecf20Sopenharmony_ci		(__pgprot(pgprot_val(prot) & ~_PAGE_CACHE_CTL))
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci#define pgprot_writecombine pgprot_noncached_wc
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistruct file;
2578c2ecf20Sopenharmony_ciextern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
2588c2ecf20Sopenharmony_ci				     unsigned long size, pgprot_t vma_prot);
2598c2ecf20Sopenharmony_ci#define __HAVE_PHYS_MEM_ACCESS_PROT
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci#ifdef CONFIG_HUGETLB_PAGE
2628c2ecf20Sopenharmony_cistatic inline int hugepd_ok(hugepd_t hpd)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_8xx
2658c2ecf20Sopenharmony_ci	return ((hpd_val(hpd) & _PMD_PAGE_MASK) == _PMD_PAGE_8M);
2668c2ecf20Sopenharmony_ci#else
2678c2ecf20Sopenharmony_ci	/* We clear the top bit to indicate hugepd */
2688c2ecf20Sopenharmony_ci	return (hpd_val(hpd) && (hpd_val(hpd) & PD_HUGE) == 0);
2698c2ecf20Sopenharmony_ci#endif
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_cistatic inline int pmd_huge(pmd_t pmd)
2738c2ecf20Sopenharmony_ci{
2748c2ecf20Sopenharmony_ci	return 0;
2758c2ecf20Sopenharmony_ci}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_cistatic inline int pud_huge(pud_t pud)
2788c2ecf20Sopenharmony_ci{
2798c2ecf20Sopenharmony_ci	return 0;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic inline int pgd_huge(pgd_t pgd)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	return 0;
2858c2ecf20Sopenharmony_ci}
2868c2ecf20Sopenharmony_ci#define pgd_huge		pgd_huge
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci#define is_hugepd(hpd)		(hugepd_ok(hpd))
2898c2ecf20Sopenharmony_ci#endif
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci/*
2928c2ecf20Sopenharmony_ci * This gets called at the end of handling a page fault, when
2938c2ecf20Sopenharmony_ci * the kernel has put a new PTE into the page table for the process.
2948c2ecf20Sopenharmony_ci * We use it to ensure coherency between the i-cache and d-cache
2958c2ecf20Sopenharmony_ci * for the page which has just been mapped in.
2968c2ecf20Sopenharmony_ci */
2978c2ecf20Sopenharmony_ci#if defined(CONFIG_PPC_FSL_BOOK3E) && defined(CONFIG_HUGETLB_PAGE)
2988c2ecf20Sopenharmony_civoid update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep);
2998c2ecf20Sopenharmony_ci#else
3008c2ecf20Sopenharmony_cistatic inline
3018c2ecf20Sopenharmony_civoid update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep) {}
3028c2ecf20Sopenharmony_ci#endif
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci#endif /* __ASSEMBLY__ */
3058c2ecf20Sopenharmony_ci#endif
306