xref: /linux/arch/arm64/include/asm/pgtable.h (revision 62d96c71d248834af2891293dc23cc344ae2ec36)
14f04d8f0SCatalin Marinas /*
24f04d8f0SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
34f04d8f0SCatalin Marinas  *
44f04d8f0SCatalin Marinas  * This program is free software; you can redistribute it and/or modify
54f04d8f0SCatalin Marinas  * it under the terms of the GNU General Public License version 2 as
64f04d8f0SCatalin Marinas  * published by the Free Software Foundation.
74f04d8f0SCatalin Marinas  *
84f04d8f0SCatalin Marinas  * This program is distributed in the hope that it will be useful,
94f04d8f0SCatalin Marinas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
104f04d8f0SCatalin Marinas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
114f04d8f0SCatalin Marinas  * GNU General Public License for more details.
124f04d8f0SCatalin Marinas  *
134f04d8f0SCatalin Marinas  * You should have received a copy of the GNU General Public License
144f04d8f0SCatalin Marinas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
154f04d8f0SCatalin Marinas  */
164f04d8f0SCatalin Marinas #ifndef __ASM_PGTABLE_H
174f04d8f0SCatalin Marinas #define __ASM_PGTABLE_H
184f04d8f0SCatalin Marinas 
192f4b829cSCatalin Marinas #include <asm/bug.h>
204f04d8f0SCatalin Marinas #include <asm/proc-fns.h>
214f04d8f0SCatalin Marinas 
224f04d8f0SCatalin Marinas #include <asm/memory.h>
234f04d8f0SCatalin Marinas #include <asm/pgtable-hwdef.h>
244f04d8f0SCatalin Marinas 
254f04d8f0SCatalin Marinas /*
264f04d8f0SCatalin Marinas  * Software defined PTE bits definition.
274f04d8f0SCatalin Marinas  */
28a6fadf7eSWill Deacon #define PTE_VALID		(_AT(pteval_t, 1) << 0)
294f04d8f0SCatalin Marinas #define PTE_DIRTY		(_AT(pteval_t, 1) << 55)
304f04d8f0SCatalin Marinas #define PTE_SPECIAL		(_AT(pteval_t, 1) << 56)
312f4b829cSCatalin Marinas #ifdef CONFIG_ARM64_HW_AFDBM
322f4b829cSCatalin Marinas #define PTE_WRITE		(PTE_DBM)		 /* same as DBM */
332f4b829cSCatalin Marinas #else
34c2c93e5bSSteve Capper #define PTE_WRITE		(_AT(pteval_t, 1) << 57)
352f4b829cSCatalin Marinas #endif
363676f9efSCatalin Marinas #define PTE_PROT_NONE		(_AT(pteval_t, 1) << 58) /* only when !PTE_VALID */
374f04d8f0SCatalin Marinas 
384f04d8f0SCatalin Marinas /*
394f04d8f0SCatalin Marinas  * VMALLOC and SPARSEMEM_VMEMMAP ranges.
4008375198SCatalin Marinas  *
4108375198SCatalin Marinas  * VMEMAP_SIZE: allows the whole VA space to be covered by a struct page array
4208375198SCatalin Marinas  *	(rounded up to PUD_SIZE).
4308375198SCatalin Marinas  * VMALLOC_START: beginning of the kernel VA space
4408375198SCatalin Marinas  * VMALLOC_END: extends to the available space below vmmemmap, PCI I/O space,
4508375198SCatalin Marinas  *	fixed mappings and modules
464f04d8f0SCatalin Marinas  */
4708375198SCatalin Marinas #define VMEMMAP_SIZE		ALIGN((1UL << (VA_BITS - PAGE_SHIFT)) * sizeof(struct page), PUD_SIZE)
48847264fbSCatalin Marinas #define VMALLOC_START		(UL(0xffffffffffffffff) << VA_BITS)
4908375198SCatalin Marinas #define VMALLOC_END		(PAGE_OFFSET - PUD_SIZE - VMEMMAP_SIZE - SZ_64K)
504f04d8f0SCatalin Marinas 
514f04d8f0SCatalin Marinas #define vmemmap			((struct page *)(VMALLOC_END + SZ_64K))
524f04d8f0SCatalin Marinas 
53d016bf7eSKirill A. Shutemov #define FIRST_USER_ADDRESS	0UL
544f04d8f0SCatalin Marinas 
554f04d8f0SCatalin Marinas #ifndef __ASSEMBLY__
562f4b829cSCatalin Marinas 
572f4b829cSCatalin Marinas #include <linux/mmdebug.h>
582f4b829cSCatalin Marinas 
594f04d8f0SCatalin Marinas extern void __pte_error(const char *file, int line, unsigned long val);
604f04d8f0SCatalin Marinas extern void __pmd_error(const char *file, int line, unsigned long val);
61c79b954bSJungseok Lee extern void __pud_error(const char *file, int line, unsigned long val);
624f04d8f0SCatalin Marinas extern void __pgd_error(const char *file, int line, unsigned long val);
634f04d8f0SCatalin Marinas 
64a501e324SCatalin Marinas #define PROT_DEFAULT		(PTE_TYPE_PAGE | PTE_AF | PTE_SHARED)
65a501e324SCatalin Marinas #define PROT_SECT_DEFAULT	(PMD_TYPE_SECT | PMD_SECT_AF | PMD_SECT_S)
664f04d8f0SCatalin Marinas 
67a501e324SCatalin Marinas #define PROT_DEVICE_nGnRE	(PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_ATTRINDX(MT_DEVICE_nGnRE))
68a501e324SCatalin Marinas #define PROT_NORMAL_NC		(PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_ATTRINDX(MT_NORMAL_NC))
69a501e324SCatalin Marinas #define PROT_NORMAL		(PROT_DEFAULT | PTE_PXN | PTE_UXN | PTE_ATTRINDX(MT_NORMAL))
704f04d8f0SCatalin Marinas 
71a501e324SCatalin Marinas #define PROT_SECT_DEVICE_nGnRE	(PROT_SECT_DEFAULT | PMD_SECT_PXN | PMD_SECT_UXN | PMD_ATTRINDX(MT_DEVICE_nGnRE))
72a501e324SCatalin Marinas #define PROT_SECT_NORMAL	(PROT_SECT_DEFAULT | PMD_SECT_PXN | PMD_SECT_UXN | PMD_ATTRINDX(MT_NORMAL))
73a501e324SCatalin Marinas #define PROT_SECT_NORMAL_EXEC	(PROT_SECT_DEFAULT | PMD_SECT_UXN | PMD_ATTRINDX(MT_NORMAL))
744f04d8f0SCatalin Marinas 
75a501e324SCatalin Marinas #define _PAGE_DEFAULT		(PROT_DEFAULT | PTE_ATTRINDX(MT_NORMAL))
76a6fadf7eSWill Deacon 
77a501e324SCatalin Marinas #define PAGE_KERNEL		__pgprot(_PAGE_DEFAULT | PTE_PXN | PTE_UXN | PTE_DIRTY | PTE_WRITE)
78a501e324SCatalin Marinas #define PAGE_KERNEL_EXEC	__pgprot(_PAGE_DEFAULT | PTE_UXN | PTE_DIRTY | PTE_WRITE)
794f04d8f0SCatalin Marinas 
80a501e324SCatalin Marinas #define PAGE_HYP		__pgprot(_PAGE_DEFAULT | PTE_HYP)
8136311607SMarc Zyngier #define PAGE_HYP_DEVICE		__pgprot(PROT_DEVICE_nGnRE | PTE_HYP)
8236311607SMarc Zyngier 
83a501e324SCatalin Marinas #define PAGE_S2			__pgprot(PROT_DEFAULT | PTE_S2_MEMATTR(MT_S2_NORMAL) | PTE_S2_RDONLY)
844a513fb0SArd Biesheuvel #define PAGE_S2_DEVICE		__pgprot(PROT_DEFAULT | PTE_S2_MEMATTR(MT_S2_DEVICE_nGnRE) | PTE_S2_RDONLY | PTE_UXN)
8536311607SMarc Zyngier 
86a501e324SCatalin Marinas #define PAGE_NONE		__pgprot(((_PAGE_DEFAULT) & ~PTE_TYPE_MASK) | PTE_PROT_NONE | PTE_PXN | PTE_UXN)
87a501e324SCatalin Marinas #define PAGE_SHARED		__pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN | PTE_UXN | PTE_WRITE)
88a501e324SCatalin Marinas #define PAGE_SHARED_EXEC	__pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN | PTE_WRITE)
89a501e324SCatalin Marinas #define PAGE_COPY		__pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN | PTE_UXN)
90a501e324SCatalin Marinas #define PAGE_COPY_EXEC		__pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN)
91a501e324SCatalin Marinas #define PAGE_READONLY		__pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN | PTE_UXN)
92a501e324SCatalin Marinas #define PAGE_READONLY_EXEC	__pgprot(_PAGE_DEFAULT | PTE_USER | PTE_NG | PTE_PXN)
934f04d8f0SCatalin Marinas 
94a501e324SCatalin Marinas #define __P000  PAGE_NONE
95a501e324SCatalin Marinas #define __P001  PAGE_READONLY
96a501e324SCatalin Marinas #define __P010  PAGE_COPY
97a501e324SCatalin Marinas #define __P011  PAGE_COPY
985a0fdfadSCatalin Marinas #define __P100  PAGE_READONLY_EXEC
99a501e324SCatalin Marinas #define __P101  PAGE_READONLY_EXEC
100a501e324SCatalin Marinas #define __P110  PAGE_COPY_EXEC
101a501e324SCatalin Marinas #define __P111  PAGE_COPY_EXEC
1024f04d8f0SCatalin Marinas 
103a501e324SCatalin Marinas #define __S000  PAGE_NONE
104a501e324SCatalin Marinas #define __S001  PAGE_READONLY
105a501e324SCatalin Marinas #define __S010  PAGE_SHARED
106a501e324SCatalin Marinas #define __S011  PAGE_SHARED
1075a0fdfadSCatalin Marinas #define __S100  PAGE_READONLY_EXEC
108a501e324SCatalin Marinas #define __S101  PAGE_READONLY_EXEC
109a501e324SCatalin Marinas #define __S110  PAGE_SHARED_EXEC
110a501e324SCatalin Marinas #define __S111  PAGE_SHARED_EXEC
1114f04d8f0SCatalin Marinas 
1124f04d8f0SCatalin Marinas /*
1134f04d8f0SCatalin Marinas  * ZERO_PAGE is a global shared page that is always zero: used
1144f04d8f0SCatalin Marinas  * for zero-mapped memory areas etc..
1154f04d8f0SCatalin Marinas  */
1164f04d8f0SCatalin Marinas extern struct page *empty_zero_page;
1174f04d8f0SCatalin Marinas #define ZERO_PAGE(vaddr)	(empty_zero_page)
1184f04d8f0SCatalin Marinas 
1197078db46SCatalin Marinas #define pte_ERROR(pte)		__pte_error(__FILE__, __LINE__, pte_val(pte))
1207078db46SCatalin Marinas 
1214f04d8f0SCatalin Marinas #define pte_pfn(pte)		((pte_val(pte) & PHYS_MASK) >> PAGE_SHIFT)
1224f04d8f0SCatalin Marinas 
1234f04d8f0SCatalin Marinas #define pfn_pte(pfn,prot)	(__pte(((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)))
1244f04d8f0SCatalin Marinas 
1254f04d8f0SCatalin Marinas #define pte_none(pte)		(!pte_val(pte))
1264f04d8f0SCatalin Marinas #define pte_clear(mm,addr,ptep)	set_pte(ptep, __pte(0))
1274f04d8f0SCatalin Marinas #define pte_page(pte)		(pfn_to_page(pte_pfn(pte)))
1287078db46SCatalin Marinas 
1297078db46SCatalin Marinas /* Find an entry in the third-level page table. */
1307078db46SCatalin Marinas #define pte_index(addr)		(((addr) >> PAGE_SHIFT) & (PTRS_PER_PTE - 1))
1317078db46SCatalin Marinas 
1329ab6d02fSWill Deacon #define pte_offset_kernel(dir,addr)	(pmd_page_vaddr(*(dir)) + pte_index(addr))
1334f04d8f0SCatalin Marinas 
1344f04d8f0SCatalin Marinas #define pte_offset_map(dir,addr)	pte_offset_kernel((dir), (addr))
1354f04d8f0SCatalin Marinas #define pte_offset_map_nested(dir,addr)	pte_offset_kernel((dir), (addr))
1364f04d8f0SCatalin Marinas #define pte_unmap(pte)			do { } while (0)
1374f04d8f0SCatalin Marinas #define pte_unmap_nested(pte)		do { } while (0)
1384f04d8f0SCatalin Marinas 
1394f04d8f0SCatalin Marinas /*
1404f04d8f0SCatalin Marinas  * The following only work if pte_present(). Undefined behaviour otherwise.
1414f04d8f0SCatalin Marinas  */
14284fe6826SSteve Capper #define pte_present(pte)	(!!(pte_val(pte) & (PTE_VALID | PTE_PROT_NONE)))
14384fe6826SSteve Capper #define pte_young(pte)		(!!(pte_val(pte) & PTE_AF))
14484fe6826SSteve Capper #define pte_special(pte)	(!!(pte_val(pte) & PTE_SPECIAL))
14584fe6826SSteve Capper #define pte_write(pte)		(!!(pte_val(pte) & PTE_WRITE))
1468e620b04SCatalin Marinas #define pte_exec(pte)		(!(pte_val(pte) & PTE_UXN))
1474f04d8f0SCatalin Marinas 
1482f4b829cSCatalin Marinas #ifdef CONFIG_ARM64_HW_AFDBM
149b847415cSCatalin Marinas #define pte_hw_dirty(pte)	(pte_write(pte) && !(pte_val(pte) & PTE_RDONLY))
1502f4b829cSCatalin Marinas #else
1512f4b829cSCatalin Marinas #define pte_hw_dirty(pte)	(0)
1522f4b829cSCatalin Marinas #endif
1532f4b829cSCatalin Marinas #define pte_sw_dirty(pte)	(!!(pte_val(pte) & PTE_DIRTY))
1542f4b829cSCatalin Marinas #define pte_dirty(pte)		(pte_sw_dirty(pte) || pte_hw_dirty(pte))
1552f4b829cSCatalin Marinas 
156766ffb69SWill Deacon #define pte_valid(pte)		(!!(pte_val(pte) & PTE_VALID))
157a6fadf7eSWill Deacon #define pte_valid_user(pte) \
15802522463SWill Deacon 	((pte_val(pte) & (PTE_VALID | PTE_USER)) == (PTE_VALID | PTE_USER))
1597f0b1bf0SCatalin Marinas #define pte_valid_not_user(pte) \
1607f0b1bf0SCatalin Marinas 	((pte_val(pte) & (PTE_VALID | PTE_USER)) == PTE_VALID)
1614f04d8f0SCatalin Marinas 
162b6d4f280SLaura Abbott static inline pte_t clear_pte_bit(pte_t pte, pgprot_t prot)
163b6d4f280SLaura Abbott {
164b6d4f280SLaura Abbott 	pte_val(pte) &= ~pgprot_val(prot);
165b6d4f280SLaura Abbott 	return pte;
166b6d4f280SLaura Abbott }
167b6d4f280SLaura Abbott 
168b6d4f280SLaura Abbott static inline pte_t set_pte_bit(pte_t pte, pgprot_t prot)
169b6d4f280SLaura Abbott {
170b6d4f280SLaura Abbott 	pte_val(pte) |= pgprot_val(prot);
171b6d4f280SLaura Abbott 	return pte;
172b6d4f280SLaura Abbott }
173b6d4f280SLaura Abbott 
17444b6dfc5SSteve Capper static inline pte_t pte_wrprotect(pte_t pte)
17544b6dfc5SSteve Capper {
176b6d4f280SLaura Abbott 	return clear_pte_bit(pte, __pgprot(PTE_WRITE));
17744b6dfc5SSteve Capper }
1784f04d8f0SCatalin Marinas 
17944b6dfc5SSteve Capper static inline pte_t pte_mkwrite(pte_t pte)
18044b6dfc5SSteve Capper {
181b6d4f280SLaura Abbott 	return set_pte_bit(pte, __pgprot(PTE_WRITE));
18244b6dfc5SSteve Capper }
18344b6dfc5SSteve Capper 
18444b6dfc5SSteve Capper static inline pte_t pte_mkclean(pte_t pte)
18544b6dfc5SSteve Capper {
186b6d4f280SLaura Abbott 	return clear_pte_bit(pte, __pgprot(PTE_DIRTY));
18744b6dfc5SSteve Capper }
18844b6dfc5SSteve Capper 
18944b6dfc5SSteve Capper static inline pte_t pte_mkdirty(pte_t pte)
19044b6dfc5SSteve Capper {
191b6d4f280SLaura Abbott 	return set_pte_bit(pte, __pgprot(PTE_DIRTY));
19244b6dfc5SSteve Capper }
19344b6dfc5SSteve Capper 
19444b6dfc5SSteve Capper static inline pte_t pte_mkold(pte_t pte)
19544b6dfc5SSteve Capper {
196b6d4f280SLaura Abbott 	return clear_pte_bit(pte, __pgprot(PTE_AF));
19744b6dfc5SSteve Capper }
19844b6dfc5SSteve Capper 
19944b6dfc5SSteve Capper static inline pte_t pte_mkyoung(pte_t pte)
20044b6dfc5SSteve Capper {
201b6d4f280SLaura Abbott 	return set_pte_bit(pte, __pgprot(PTE_AF));
20244b6dfc5SSteve Capper }
20344b6dfc5SSteve Capper 
20444b6dfc5SSteve Capper static inline pte_t pte_mkspecial(pte_t pte)
20544b6dfc5SSteve Capper {
206b6d4f280SLaura Abbott 	return set_pte_bit(pte, __pgprot(PTE_SPECIAL));
20744b6dfc5SSteve Capper }
2084f04d8f0SCatalin Marinas 
2094f04d8f0SCatalin Marinas static inline void set_pte(pte_t *ptep, pte_t pte)
2104f04d8f0SCatalin Marinas {
2114f04d8f0SCatalin Marinas 	*ptep = pte;
2127f0b1bf0SCatalin Marinas 
2137f0b1bf0SCatalin Marinas 	/*
2147f0b1bf0SCatalin Marinas 	 * Only if the new pte is valid and kernel, otherwise TLB maintenance
2157f0b1bf0SCatalin Marinas 	 * or update_mmu_cache() have the necessary barriers.
2167f0b1bf0SCatalin Marinas 	 */
2177f0b1bf0SCatalin Marinas 	if (pte_valid_not_user(pte)) {
2187f0b1bf0SCatalin Marinas 		dsb(ishst);
2197f0b1bf0SCatalin Marinas 		isb();
2207f0b1bf0SCatalin Marinas 	}
2214f04d8f0SCatalin Marinas }
2224f04d8f0SCatalin Marinas 
2232f4b829cSCatalin Marinas struct mm_struct;
2242f4b829cSCatalin Marinas struct vm_area_struct;
2252f4b829cSCatalin Marinas 
2264f04d8f0SCatalin Marinas extern void __sync_icache_dcache(pte_t pteval, unsigned long addr);
2274f04d8f0SCatalin Marinas 
2282f4b829cSCatalin Marinas /*
2292f4b829cSCatalin Marinas  * PTE bits configuration in the presence of hardware Dirty Bit Management
2302f4b829cSCatalin Marinas  * (PTE_WRITE == PTE_DBM):
2312f4b829cSCatalin Marinas  *
2322f4b829cSCatalin Marinas  * Dirty  Writable | PTE_RDONLY  PTE_WRITE  PTE_DIRTY (sw)
2332f4b829cSCatalin Marinas  *   0      0      |   1           0          0
2342f4b829cSCatalin Marinas  *   0      1      |   1           1          0
2352f4b829cSCatalin Marinas  *   1      0      |   1           0          1
2362f4b829cSCatalin Marinas  *   1      1      |   0           1          x
2372f4b829cSCatalin Marinas  *
2382f4b829cSCatalin Marinas  * When hardware DBM is not present, the sofware PTE_DIRTY bit is updated via
2392f4b829cSCatalin Marinas  * the page fault mechanism. Checking the dirty status of a pte becomes:
2402f4b829cSCatalin Marinas  *
241b847415cSCatalin Marinas  *   PTE_DIRTY || (PTE_WRITE && !PTE_RDONLY)
2422f4b829cSCatalin Marinas  */
2434f04d8f0SCatalin Marinas static inline void set_pte_at(struct mm_struct *mm, unsigned long addr,
2444f04d8f0SCatalin Marinas 			      pte_t *ptep, pte_t pte)
2454f04d8f0SCatalin Marinas {
246a6fadf7eSWill Deacon 	if (pte_valid_user(pte)) {
24771fdb6bfSCatalin Marinas 		if (!pte_special(pte) && pte_exec(pte))
2484f04d8f0SCatalin Marinas 			__sync_icache_dcache(pte, addr);
2492f4b829cSCatalin Marinas 		if (pte_sw_dirty(pte) && pte_write(pte))
250c2c93e5bSSteve Capper 			pte_val(pte) &= ~PTE_RDONLY;
251c2c93e5bSSteve Capper 		else
252c2c93e5bSSteve Capper 			pte_val(pte) |= PTE_RDONLY;
25302522463SWill Deacon 	}
25402522463SWill Deacon 
2552f4b829cSCatalin Marinas 	/*
2562f4b829cSCatalin Marinas 	 * If the existing pte is valid, check for potential race with
2572f4b829cSCatalin Marinas 	 * hardware updates of the pte (ptep_set_access_flags safely changes
2582f4b829cSCatalin Marinas 	 * valid ptes without going through an invalid entry).
2592f4b829cSCatalin Marinas 	 */
2602f4b829cSCatalin Marinas 	if (IS_ENABLED(CONFIG_DEBUG_VM) && IS_ENABLED(CONFIG_ARM64_HW_AFDBM) &&
2612f4b829cSCatalin Marinas 	    pte_valid(*ptep)) {
2622f4b829cSCatalin Marinas 		BUG_ON(!pte_young(pte));
2632f4b829cSCatalin Marinas 		BUG_ON(pte_write(*ptep) && !pte_dirty(pte));
2642f4b829cSCatalin Marinas 	}
2652f4b829cSCatalin Marinas 
2664f04d8f0SCatalin Marinas 	set_pte(ptep, pte);
2674f04d8f0SCatalin Marinas }
2684f04d8f0SCatalin Marinas 
2694f04d8f0SCatalin Marinas /*
2704f04d8f0SCatalin Marinas  * Huge pte definitions.
2714f04d8f0SCatalin Marinas  */
272084bd298SSteve Capper #define pte_huge(pte)		(!(pte_val(pte) & PTE_TABLE_BIT))
273084bd298SSteve Capper #define pte_mkhuge(pte)		(__pte(pte_val(pte) & ~PTE_TABLE_BIT))
274084bd298SSteve Capper 
275084bd298SSteve Capper /*
276084bd298SSteve Capper  * Hugetlb definitions.
277084bd298SSteve Capper  */
278084bd298SSteve Capper #define HUGE_MAX_HSTATE		2
279084bd298SSteve Capper #define HPAGE_SHIFT		PMD_SHIFT
280084bd298SSteve Capper #define HPAGE_SIZE		(_AC(1, UL) << HPAGE_SHIFT)
281084bd298SSteve Capper #define HPAGE_MASK		(~(HPAGE_SIZE - 1))
282084bd298SSteve Capper #define HUGETLB_PAGE_ORDER	(HPAGE_SHIFT - PAGE_SHIFT)
2834f04d8f0SCatalin Marinas 
2844f04d8f0SCatalin Marinas #define __HAVE_ARCH_PTE_SPECIAL
2854f04d8f0SCatalin Marinas 
28629e56940SSteve Capper static inline pte_t pud_pte(pud_t pud)
28729e56940SSteve Capper {
28829e56940SSteve Capper 	return __pte(pud_val(pud));
28929e56940SSteve Capper }
29029e56940SSteve Capper 
29129e56940SSteve Capper static inline pmd_t pud_pmd(pud_t pud)
29229e56940SSteve Capper {
29329e56940SSteve Capper 	return __pmd(pud_val(pud));
29429e56940SSteve Capper }
29529e56940SSteve Capper 
2969c7e535fSSteve Capper static inline pte_t pmd_pte(pmd_t pmd)
2979c7e535fSSteve Capper {
2989c7e535fSSteve Capper 	return __pte(pmd_val(pmd));
2999c7e535fSSteve Capper }
300af074848SSteve Capper 
3019c7e535fSSteve Capper static inline pmd_t pte_pmd(pte_t pte)
3029c7e535fSSteve Capper {
3039c7e535fSSteve Capper 	return __pmd(pte_val(pte));
3049c7e535fSSteve Capper }
305af074848SSteve Capper 
3068ce837ceSArd Biesheuvel static inline pgprot_t mk_sect_prot(pgprot_t prot)
3078ce837ceSArd Biesheuvel {
3088ce837ceSArd Biesheuvel 	return __pgprot(pgprot_val(prot) & ~PTE_TABLE_BIT);
3098ce837ceSArd Biesheuvel }
3108ce837ceSArd Biesheuvel 
311af074848SSteve Capper /*
312af074848SSteve Capper  * THP definitions.
313af074848SSteve Capper  */
314af074848SSteve Capper 
315af074848SSteve Capper #ifdef CONFIG_TRANSPARENT_HUGEPAGE
316af074848SSteve Capper #define pmd_trans_huge(pmd)	(pmd_val(pmd) && !(pmd_val(pmd) & PMD_TABLE_BIT))
3179c7e535fSSteve Capper #define pmd_trans_splitting(pmd)	pte_special(pmd_pte(pmd))
31829e56940SSteve Capper #ifdef CONFIG_HAVE_RCU_TABLE_FREE
31929e56940SSteve Capper #define __HAVE_ARCH_PMDP_SPLITTING_FLUSH
32029e56940SSteve Capper struct vm_area_struct;
32129e56940SSteve Capper void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
32229e56940SSteve Capper 			  pmd_t *pmdp);
32329e56940SSteve Capper #endif /* CONFIG_HAVE_RCU_TABLE_FREE */
32429e56940SSteve Capper #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
325af074848SSteve Capper 
326c164e038SKirill A. Shutemov #define pmd_dirty(pmd)		pte_dirty(pmd_pte(pmd))
3279c7e535fSSteve Capper #define pmd_young(pmd)		pte_young(pmd_pte(pmd))
3289c7e535fSSteve Capper #define pmd_wrprotect(pmd)	pte_pmd(pte_wrprotect(pmd_pte(pmd)))
3299c7e535fSSteve Capper #define pmd_mksplitting(pmd)	pte_pmd(pte_mkspecial(pmd_pte(pmd)))
3309c7e535fSSteve Capper #define pmd_mkold(pmd)		pte_pmd(pte_mkold(pmd_pte(pmd)))
3319c7e535fSSteve Capper #define pmd_mkwrite(pmd)	pte_pmd(pte_mkwrite(pmd_pte(pmd)))
3329c7e535fSSteve Capper #define pmd_mkdirty(pmd)	pte_pmd(pte_mkdirty(pmd_pte(pmd)))
3339c7e535fSSteve Capper #define pmd_mkyoung(pmd)	pte_pmd(pte_mkyoung(pmd_pte(pmd)))
334e3a920afSWill Deacon #define pmd_mknotpresent(pmd)	(__pmd(pmd_val(pmd) & ~PMD_TYPE_MASK))
335af074848SSteve Capper 
3369c7e535fSSteve Capper #define __HAVE_ARCH_PMD_WRITE
3379c7e535fSSteve Capper #define pmd_write(pmd)		pte_write(pmd_pte(pmd))
338af074848SSteve Capper 
339af074848SSteve Capper #define pmd_mkhuge(pmd)		(__pmd(pmd_val(pmd) & ~PMD_TABLE_BIT))
340af074848SSteve Capper 
341af074848SSteve Capper #define pmd_pfn(pmd)		(((pmd_val(pmd) & PMD_MASK) & PHYS_MASK) >> PAGE_SHIFT)
342af074848SSteve Capper #define pfn_pmd(pfn,prot)	(__pmd(((phys_addr_t)(pfn) << PAGE_SHIFT) | pgprot_val(prot)))
343af074848SSteve Capper #define mk_pmd(page,prot)	pfn_pmd(page_to_pfn(page),prot)
344af074848SSteve Capper 
34529e56940SSteve Capper #define pud_write(pud)		pte_write(pud_pte(pud))
346206a2a73SSteve Capper #define pud_pfn(pud)		(((pud_val(pud) & PUD_MASK) & PHYS_MASK) >> PAGE_SHIFT)
347af074848SSteve Capper 
348ceb21835SWill Deacon #define set_pmd_at(mm, addr, pmdp, pmd)	set_pte_at(mm, addr, (pte_t *)pmdp, pmd_pte(pmd))
349af074848SSteve Capper 
350af074848SSteve Capper static inline int has_transparent_hugepage(void)
351af074848SSteve Capper {
352af074848SSteve Capper 	return 1;
353af074848SSteve Capper }
354af074848SSteve Capper 
355a501e324SCatalin Marinas #define __pgprot_modify(prot,mask,bits) \
356a501e324SCatalin Marinas 	__pgprot((pgprot_val(prot) & ~(mask)) | (bits))
357a501e324SCatalin Marinas 
358af074848SSteve Capper /*
3594f04d8f0SCatalin Marinas  * Mark the prot value as uncacheable and unbufferable.
3604f04d8f0SCatalin Marinas  */
3614f04d8f0SCatalin Marinas #define pgprot_noncached(prot) \
362de2db743SCatalin Marinas 	__pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_DEVICE_nGnRnE) | PTE_PXN | PTE_UXN)
3634f04d8f0SCatalin Marinas #define pgprot_writecombine(prot) \
364de2db743SCatalin Marinas 	__pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_NORMAL_NC) | PTE_PXN | PTE_UXN)
365d1e6dc91SLiviu Dudau #define pgprot_device(prot) \
366d1e6dc91SLiviu Dudau 	__pgprot_modify(prot, PTE_ATTRINDX_MASK, PTE_ATTRINDX(MT_DEVICE_nGnRE) | PTE_PXN | PTE_UXN)
3674f04d8f0SCatalin Marinas #define __HAVE_PHYS_MEM_ACCESS_PROT
3684f04d8f0SCatalin Marinas struct file;
3694f04d8f0SCatalin Marinas extern pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
3704f04d8f0SCatalin Marinas 				     unsigned long size, pgprot_t vma_prot);
3714f04d8f0SCatalin Marinas 
3724f04d8f0SCatalin Marinas #define pmd_none(pmd)		(!pmd_val(pmd))
3734f04d8f0SCatalin Marinas #define pmd_present(pmd)	(pmd_val(pmd))
3744f04d8f0SCatalin Marinas 
3754f04d8f0SCatalin Marinas #define pmd_bad(pmd)		(!(pmd_val(pmd) & 2))
3764f04d8f0SCatalin Marinas 
37736311607SMarc Zyngier #define pmd_table(pmd)		((pmd_val(pmd) & PMD_TYPE_MASK) == \
37836311607SMarc Zyngier 				 PMD_TYPE_TABLE)
37936311607SMarc Zyngier #define pmd_sect(pmd)		((pmd_val(pmd) & PMD_TYPE_MASK) == \
38036311607SMarc Zyngier 				 PMD_TYPE_SECT)
38136311607SMarc Zyngier 
382f3b766a2SSteve Capper #ifdef CONFIG_ARM64_64K_PAGES
383206a2a73SSteve Capper #define pud_sect(pud)		(0)
384523d6e9fSzhichang.yuan #define pud_table(pud)		(1)
385206a2a73SSteve Capper #else
386206a2a73SSteve Capper #define pud_sect(pud)		((pud_val(pud) & PUD_TYPE_MASK) == \
387206a2a73SSteve Capper 				 PUD_TYPE_SECT)
388523d6e9fSzhichang.yuan #define pud_table(pud)		((pud_val(pud) & PUD_TYPE_MASK) == \
389523d6e9fSzhichang.yuan 				 PUD_TYPE_TABLE)
390206a2a73SSteve Capper #endif
39136311607SMarc Zyngier 
3924f04d8f0SCatalin Marinas static inline void set_pmd(pmd_t *pmdp, pmd_t pmd)
3934f04d8f0SCatalin Marinas {
3944f04d8f0SCatalin Marinas 	*pmdp = pmd;
39598f7685eSWill Deacon 	dsb(ishst);
3967f0b1bf0SCatalin Marinas 	isb();
3974f04d8f0SCatalin Marinas }
3984f04d8f0SCatalin Marinas 
3994f04d8f0SCatalin Marinas static inline void pmd_clear(pmd_t *pmdp)
4004f04d8f0SCatalin Marinas {
4014f04d8f0SCatalin Marinas 	set_pmd(pmdp, __pmd(0));
4024f04d8f0SCatalin Marinas }
4034f04d8f0SCatalin Marinas 
4044f04d8f0SCatalin Marinas static inline pte_t *pmd_page_vaddr(pmd_t pmd)
4054f04d8f0SCatalin Marinas {
4064f04d8f0SCatalin Marinas 	return __va(pmd_val(pmd) & PHYS_MASK & (s32)PAGE_MASK);
4074f04d8f0SCatalin Marinas }
4084f04d8f0SCatalin Marinas 
4094f04d8f0SCatalin Marinas #define pmd_page(pmd)		pfn_to_page(__phys_to_pfn(pmd_val(pmd) & PHYS_MASK))
4104f04d8f0SCatalin Marinas 
4114f04d8f0SCatalin Marinas /*
4124f04d8f0SCatalin Marinas  * Conversion functions: convert a page and protection to a page entry,
4134f04d8f0SCatalin Marinas  * and a page entry and page directory to the page they refer to.
4144f04d8f0SCatalin Marinas  */
4154f04d8f0SCatalin Marinas #define mk_pte(page,prot)	pfn_pte(page_to_pfn(page),prot)
4164f04d8f0SCatalin Marinas 
4179f25e6adSKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS > 2
4184f04d8f0SCatalin Marinas 
4197078db46SCatalin Marinas #define pmd_ERROR(pmd)		__pmd_error(__FILE__, __LINE__, pmd_val(pmd))
4207078db46SCatalin Marinas 
4214f04d8f0SCatalin Marinas #define pud_none(pud)		(!pud_val(pud))
4224f04d8f0SCatalin Marinas #define pud_bad(pud)		(!(pud_val(pud) & 2))
4234f04d8f0SCatalin Marinas #define pud_present(pud)	(pud_val(pud))
4244f04d8f0SCatalin Marinas 
4254f04d8f0SCatalin Marinas static inline void set_pud(pud_t *pudp, pud_t pud)
4264f04d8f0SCatalin Marinas {
4274f04d8f0SCatalin Marinas 	*pudp = pud;
42898f7685eSWill Deacon 	dsb(ishst);
4297f0b1bf0SCatalin Marinas 	isb();
4304f04d8f0SCatalin Marinas }
4314f04d8f0SCatalin Marinas 
4324f04d8f0SCatalin Marinas static inline void pud_clear(pud_t *pudp)
4334f04d8f0SCatalin Marinas {
4344f04d8f0SCatalin Marinas 	set_pud(pudp, __pud(0));
4354f04d8f0SCatalin Marinas }
4364f04d8f0SCatalin Marinas 
4374f04d8f0SCatalin Marinas static inline pmd_t *pud_page_vaddr(pud_t pud)
4384f04d8f0SCatalin Marinas {
4394f04d8f0SCatalin Marinas 	return __va(pud_val(pud) & PHYS_MASK & (s32)PAGE_MASK);
4404f04d8f0SCatalin Marinas }
4414f04d8f0SCatalin Marinas 
4427078db46SCatalin Marinas /* Find an entry in the second-level page table. */
4437078db46SCatalin Marinas #define pmd_index(addr)		(((addr) >> PMD_SHIFT) & (PTRS_PER_PMD - 1))
4447078db46SCatalin Marinas 
4457078db46SCatalin Marinas static inline pmd_t *pmd_offset(pud_t *pud, unsigned long addr)
4467078db46SCatalin Marinas {
4477078db46SCatalin Marinas 	return (pmd_t *)pud_page_vaddr(*pud) + pmd_index(addr);
4487078db46SCatalin Marinas }
4497078db46SCatalin Marinas 
4505d96e0cbSJungseok Lee #define pud_page(pud)		pfn_to_page(__phys_to_pfn(pud_val(pud) & PHYS_MASK))
45129e56940SSteve Capper 
4529f25e6adSKirill A. Shutemov #endif	/* CONFIG_PGTABLE_LEVELS > 2 */
4534f04d8f0SCatalin Marinas 
4549f25e6adSKirill A. Shutemov #if CONFIG_PGTABLE_LEVELS > 3
455c79b954bSJungseok Lee 
4567078db46SCatalin Marinas #define pud_ERROR(pud)		__pud_error(__FILE__, __LINE__, pud_val(pud))
4577078db46SCatalin Marinas 
458c79b954bSJungseok Lee #define pgd_none(pgd)		(!pgd_val(pgd))
459c79b954bSJungseok Lee #define pgd_bad(pgd)		(!(pgd_val(pgd) & 2))
460c79b954bSJungseok Lee #define pgd_present(pgd)	(pgd_val(pgd))
461c79b954bSJungseok Lee 
462c79b954bSJungseok Lee static inline void set_pgd(pgd_t *pgdp, pgd_t pgd)
463c79b954bSJungseok Lee {
464c79b954bSJungseok Lee 	*pgdp = pgd;
465c79b954bSJungseok Lee 	dsb(ishst);
466c79b954bSJungseok Lee }
467c79b954bSJungseok Lee 
468c79b954bSJungseok Lee static inline void pgd_clear(pgd_t *pgdp)
469c79b954bSJungseok Lee {
470c79b954bSJungseok Lee 	set_pgd(pgdp, __pgd(0));
471c79b954bSJungseok Lee }
472c79b954bSJungseok Lee 
473c79b954bSJungseok Lee static inline pud_t *pgd_page_vaddr(pgd_t pgd)
474c79b954bSJungseok Lee {
475c79b954bSJungseok Lee 	return __va(pgd_val(pgd) & PHYS_MASK & (s32)PAGE_MASK);
476c79b954bSJungseok Lee }
477c79b954bSJungseok Lee 
4787078db46SCatalin Marinas /* Find an entry in the frst-level page table. */
4797078db46SCatalin Marinas #define pud_index(addr)		(((addr) >> PUD_SHIFT) & (PTRS_PER_PUD - 1))
4807078db46SCatalin Marinas 
4817078db46SCatalin Marinas static inline pud_t *pud_offset(pgd_t *pgd, unsigned long addr)
4827078db46SCatalin Marinas {
4837078db46SCatalin Marinas 	return (pud_t *)pgd_page_vaddr(*pgd) + pud_index(addr);
4847078db46SCatalin Marinas }
4857078db46SCatalin Marinas 
4865d96e0cbSJungseok Lee #define pgd_page(pgd)		pfn_to_page(__phys_to_pfn(pgd_val(pgd) & PHYS_MASK))
4875d96e0cbSJungseok Lee 
4889f25e6adSKirill A. Shutemov #endif  /* CONFIG_PGTABLE_LEVELS > 3 */
489c79b954bSJungseok Lee 
4907078db46SCatalin Marinas #define pgd_ERROR(pgd)		__pgd_error(__FILE__, __LINE__, pgd_val(pgd))
4917078db46SCatalin Marinas 
4924f04d8f0SCatalin Marinas /* to find an entry in a page-table-directory */
4934f04d8f0SCatalin Marinas #define pgd_index(addr)		(((addr) >> PGDIR_SHIFT) & (PTRS_PER_PGD - 1))
4944f04d8f0SCatalin Marinas 
4954f04d8f0SCatalin Marinas #define pgd_offset(mm, addr)	((mm)->pgd+pgd_index(addr))
4964f04d8f0SCatalin Marinas 
4974f04d8f0SCatalin Marinas /* to find an entry in a kernel page-table-directory */
4984f04d8f0SCatalin Marinas #define pgd_offset_k(addr)	pgd_offset(&init_mm, addr)
4994f04d8f0SCatalin Marinas 
5004f04d8f0SCatalin Marinas static inline pte_t pte_modify(pte_t pte, pgprot_t newprot)
5014f04d8f0SCatalin Marinas {
502a6fadf7eSWill Deacon 	const pteval_t mask = PTE_USER | PTE_PXN | PTE_UXN | PTE_RDONLY |
5036910fa16SFeng Kan 			      PTE_PROT_NONE | PTE_WRITE | PTE_TYPE_MASK;
5042f4b829cSCatalin Marinas 	/* preserve the hardware dirty information */
5052f4b829cSCatalin Marinas 	if (pte_hw_dirty(pte))
506*62d96c71SCatalin Marinas 		pte = pte_mkdirty(pte);
5074f04d8f0SCatalin Marinas 	pte_val(pte) = (pte_val(pte) & ~mask) | (pgprot_val(newprot) & mask);
5084f04d8f0SCatalin Marinas 	return pte;
5094f04d8f0SCatalin Marinas }
5104f04d8f0SCatalin Marinas 
5119c7e535fSSteve Capper static inline pmd_t pmd_modify(pmd_t pmd, pgprot_t newprot)
5129c7e535fSSteve Capper {
5139c7e535fSSteve Capper 	return pte_pmd(pte_modify(pmd_pte(pmd), newprot));
5149c7e535fSSteve Capper }
5159c7e535fSSteve Capper 
5162f4b829cSCatalin Marinas #ifdef CONFIG_ARM64_HW_AFDBM
5172f4b829cSCatalin Marinas /*
5182f4b829cSCatalin Marinas  * Atomic pte/pmd modifications.
5192f4b829cSCatalin Marinas  */
5202f4b829cSCatalin Marinas #define __HAVE_ARCH_PTEP_TEST_AND_CLEAR_YOUNG
5212f4b829cSCatalin Marinas static inline int ptep_test_and_clear_young(struct vm_area_struct *vma,
5222f4b829cSCatalin Marinas 					    unsigned long address,
5232f4b829cSCatalin Marinas 					    pte_t *ptep)
5242f4b829cSCatalin Marinas {
5252f4b829cSCatalin Marinas 	pteval_t pteval;
5262f4b829cSCatalin Marinas 	unsigned int tmp, res;
5272f4b829cSCatalin Marinas 
5282f4b829cSCatalin Marinas 	asm volatile("//	ptep_test_and_clear_young\n"
5292f4b829cSCatalin Marinas 	"	prfm	pstl1strm, %2\n"
5302f4b829cSCatalin Marinas 	"1:	ldxr	%0, %2\n"
5312f4b829cSCatalin Marinas 	"	ubfx	%w3, %w0, %5, #1	// extract PTE_AF (young)\n"
5322f4b829cSCatalin Marinas 	"	and	%0, %0, %4		// clear PTE_AF\n"
5332f4b829cSCatalin Marinas 	"	stxr	%w1, %0, %2\n"
5342f4b829cSCatalin Marinas 	"	cbnz	%w1, 1b\n"
5352f4b829cSCatalin Marinas 	: "=&r" (pteval), "=&r" (tmp), "+Q" (pte_val(*ptep)), "=&r" (res)
5362f4b829cSCatalin Marinas 	: "L" (~PTE_AF), "I" (ilog2(PTE_AF)));
5372f4b829cSCatalin Marinas 
5382f4b829cSCatalin Marinas 	return res;
5392f4b829cSCatalin Marinas }
5402f4b829cSCatalin Marinas 
5412f4b829cSCatalin Marinas #ifdef CONFIG_TRANSPARENT_HUGEPAGE
5422f4b829cSCatalin Marinas #define __HAVE_ARCH_PMDP_TEST_AND_CLEAR_YOUNG
5432f4b829cSCatalin Marinas static inline int pmdp_test_and_clear_young(struct vm_area_struct *vma,
5442f4b829cSCatalin Marinas 					    unsigned long address,
5452f4b829cSCatalin Marinas 					    pmd_t *pmdp)
5462f4b829cSCatalin Marinas {
5472f4b829cSCatalin Marinas 	return ptep_test_and_clear_young(vma, address, (pte_t *)pmdp);
5482f4b829cSCatalin Marinas }
5492f4b829cSCatalin Marinas #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
5502f4b829cSCatalin Marinas 
5512f4b829cSCatalin Marinas #define __HAVE_ARCH_PTEP_GET_AND_CLEAR
5522f4b829cSCatalin Marinas static inline pte_t ptep_get_and_clear(struct mm_struct *mm,
5532f4b829cSCatalin Marinas 				       unsigned long address, pte_t *ptep)
5542f4b829cSCatalin Marinas {
5552f4b829cSCatalin Marinas 	pteval_t old_pteval;
5562f4b829cSCatalin Marinas 	unsigned int tmp;
5572f4b829cSCatalin Marinas 
5582f4b829cSCatalin Marinas 	asm volatile("//	ptep_get_and_clear\n"
5592f4b829cSCatalin Marinas 	"	prfm	pstl1strm, %2\n"
5602f4b829cSCatalin Marinas 	"1:	ldxr	%0, %2\n"
5612f4b829cSCatalin Marinas 	"	stxr	%w1, xzr, %2\n"
5622f4b829cSCatalin Marinas 	"	cbnz	%w1, 1b\n"
5632f4b829cSCatalin Marinas 	: "=&r" (old_pteval), "=&r" (tmp), "+Q" (pte_val(*ptep)));
5642f4b829cSCatalin Marinas 
5652f4b829cSCatalin Marinas 	return __pte(old_pteval);
5662f4b829cSCatalin Marinas }
5672f4b829cSCatalin Marinas 
5682f4b829cSCatalin Marinas #ifdef CONFIG_TRANSPARENT_HUGEPAGE
5692f4b829cSCatalin Marinas #define __HAVE_ARCH_PMDP_GET_AND_CLEAR
5702f4b829cSCatalin Marinas static inline pmd_t pmdp_get_and_clear(struct mm_struct *mm,
5712f4b829cSCatalin Marinas 				       unsigned long address, pmd_t *pmdp)
5722f4b829cSCatalin Marinas {
5732f4b829cSCatalin Marinas 	return pte_pmd(ptep_get_and_clear(mm, address, (pte_t *)pmdp));
5742f4b829cSCatalin Marinas }
5752f4b829cSCatalin Marinas #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
5762f4b829cSCatalin Marinas 
5772f4b829cSCatalin Marinas /*
5782f4b829cSCatalin Marinas  * ptep_set_wrprotect - mark read-only while trasferring potential hardware
5792f4b829cSCatalin Marinas  * dirty status (PTE_DBM && !PTE_RDONLY) to the software PTE_DIRTY bit.
5802f4b829cSCatalin Marinas  */
5812f4b829cSCatalin Marinas #define __HAVE_ARCH_PTEP_SET_WRPROTECT
5822f4b829cSCatalin Marinas static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep)
5832f4b829cSCatalin Marinas {
5842f4b829cSCatalin Marinas 	pteval_t pteval;
5852f4b829cSCatalin Marinas 	unsigned long tmp;
5862f4b829cSCatalin Marinas 
5872f4b829cSCatalin Marinas 	asm volatile("//	ptep_set_wrprotect\n"
5882f4b829cSCatalin Marinas 	"	prfm	pstl1strm, %2\n"
5892f4b829cSCatalin Marinas 	"1:	ldxr	%0, %2\n"
5902f4b829cSCatalin Marinas 	"	tst	%0, %4			// check for hw dirty (!PTE_RDONLY)\n"
5912f4b829cSCatalin Marinas 	"	csel	%1, %3, xzr, eq		// set PTE_DIRTY|PTE_RDONLY if dirty\n"
5922f4b829cSCatalin Marinas 	"	orr	%0, %0, %1		// if !dirty, PTE_RDONLY is already set\n"
5932f4b829cSCatalin Marinas 	"	and	%0, %0, %5		// clear PTE_WRITE/PTE_DBM\n"
5942f4b829cSCatalin Marinas 	"	stxr	%w1, %0, %2\n"
5952f4b829cSCatalin Marinas 	"	cbnz	%w1, 1b\n"
5962f4b829cSCatalin Marinas 	: "=&r" (pteval), "=&r" (tmp), "+Q" (pte_val(*ptep))
5972f4b829cSCatalin Marinas 	: "r" (PTE_DIRTY|PTE_RDONLY), "L" (PTE_RDONLY), "L" (~PTE_WRITE)
5982f4b829cSCatalin Marinas 	: "cc");
5992f4b829cSCatalin Marinas }
6002f4b829cSCatalin Marinas 
6012f4b829cSCatalin Marinas #ifdef CONFIG_TRANSPARENT_HUGEPAGE
6022f4b829cSCatalin Marinas #define __HAVE_ARCH_PMDP_SET_WRPROTECT
6032f4b829cSCatalin Marinas static inline void pmdp_set_wrprotect(struct mm_struct *mm,
6042f4b829cSCatalin Marinas 				      unsigned long address, pmd_t *pmdp)
6052f4b829cSCatalin Marinas {
6062f4b829cSCatalin Marinas 	ptep_set_wrprotect(mm, address, (pte_t *)pmdp);
6072f4b829cSCatalin Marinas }
6082f4b829cSCatalin Marinas #endif
6092f4b829cSCatalin Marinas #endif	/* CONFIG_ARM64_HW_AFDBM */
6102f4b829cSCatalin Marinas 
6114f04d8f0SCatalin Marinas extern pgd_t swapper_pg_dir[PTRS_PER_PGD];
6124f04d8f0SCatalin Marinas extern pgd_t idmap_pg_dir[PTRS_PER_PGD];
6134f04d8f0SCatalin Marinas 
6144f04d8f0SCatalin Marinas /*
6154f04d8f0SCatalin Marinas  * Encode and decode a swap entry:
6163676f9efSCatalin Marinas  *	bits 0-1:	present (must be zero)
6179b3e661eSKirill A. Shutemov  *	bits 2-7:	swap type
6189b3e661eSKirill A. Shutemov  *	bits 8-57:	swap offset
6194f04d8f0SCatalin Marinas  */
6209b3e661eSKirill A. Shutemov #define __SWP_TYPE_SHIFT	2
6214f04d8f0SCatalin Marinas #define __SWP_TYPE_BITS		6
6229b3e661eSKirill A. Shutemov #define __SWP_OFFSET_BITS	50
6234f04d8f0SCatalin Marinas #define __SWP_TYPE_MASK		((1 << __SWP_TYPE_BITS) - 1)
6244f04d8f0SCatalin Marinas #define __SWP_OFFSET_SHIFT	(__SWP_TYPE_BITS + __SWP_TYPE_SHIFT)
6253676f9efSCatalin Marinas #define __SWP_OFFSET_MASK	((1UL << __SWP_OFFSET_BITS) - 1)
6264f04d8f0SCatalin Marinas 
6274f04d8f0SCatalin Marinas #define __swp_type(x)		(((x).val >> __SWP_TYPE_SHIFT) & __SWP_TYPE_MASK)
6283676f9efSCatalin Marinas #define __swp_offset(x)		(((x).val >> __SWP_OFFSET_SHIFT) & __SWP_OFFSET_MASK)
6294f04d8f0SCatalin Marinas #define __swp_entry(type,offset) ((swp_entry_t) { ((type) << __SWP_TYPE_SHIFT) | ((offset) << __SWP_OFFSET_SHIFT) })
6304f04d8f0SCatalin Marinas 
6314f04d8f0SCatalin Marinas #define __pte_to_swp_entry(pte)	((swp_entry_t) { pte_val(pte) })
6324f04d8f0SCatalin Marinas #define __swp_entry_to_pte(swp)	((pte_t) { (swp).val })
6334f04d8f0SCatalin Marinas 
6344f04d8f0SCatalin Marinas /*
6354f04d8f0SCatalin Marinas  * Ensure that there are not more swap files than can be encoded in the kernel
636aad9061bSGeert Uytterhoeven  * PTEs.
6374f04d8f0SCatalin Marinas  */
6384f04d8f0SCatalin Marinas #define MAX_SWAPFILES_CHECK() BUILD_BUG_ON(MAX_SWAPFILES_SHIFT > __SWP_TYPE_BITS)
6394f04d8f0SCatalin Marinas 
6404f04d8f0SCatalin Marinas extern int kern_addr_valid(unsigned long addr);
6414f04d8f0SCatalin Marinas 
6424f04d8f0SCatalin Marinas #include <asm-generic/pgtable.h>
6434f04d8f0SCatalin Marinas 
6444f04d8f0SCatalin Marinas #define pgtable_cache_init() do { } while (0)
6454f04d8f0SCatalin Marinas 
646cba3574fSWill Deacon /*
647cba3574fSWill Deacon  * On AArch64, the cache coherency is handled via the set_pte_at() function.
648cba3574fSWill Deacon  */
649cba3574fSWill Deacon static inline void update_mmu_cache(struct vm_area_struct *vma,
650cba3574fSWill Deacon 				    unsigned long addr, pte_t *ptep)
651cba3574fSWill Deacon {
652cba3574fSWill Deacon 	/*
653cba3574fSWill Deacon 	 * set_pte() does not have a DSB for user mappings, so make sure that
654cba3574fSWill Deacon 	 * the page table write is visible.
655cba3574fSWill Deacon 	 */
656cba3574fSWill Deacon 	dsb(ishst);
657cba3574fSWill Deacon }
658cba3574fSWill Deacon 
659cba3574fSWill Deacon #define update_mmu_cache_pmd(vma, address, pmd) do { } while (0)
660cba3574fSWill Deacon 
6614f04d8f0SCatalin Marinas #endif /* !__ASSEMBLY__ */
6624f04d8f0SCatalin Marinas 
6634f04d8f0SCatalin Marinas #endif /* __ASM_PGTABLE_H */
664