xref: /linux/arch/arm64/include/asm/pgalloc.h (revision 9c93c0b44be36fd5267fb79ae33453f989fbe909)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Based on arch/arm/include/asm/pgalloc.h
4  *
5  * Copyright (C) 2000-2001 Russell King
6  * Copyright (C) 2012 ARM Ltd.
7  */
8 #ifndef __ASM_PGALLOC_H
9 #define __ASM_PGALLOC_H
10 
11 #include <asm/pgtable-hwdef.h>
12 #include <asm/processor.h>
13 #include <asm/cacheflush.h>
14 #include <asm/tlbflush.h>
15 
16 #define __HAVE_ARCH_PGD_FREE
17 #define __HAVE_ARCH_PUD_FREE
18 #include <asm-generic/pgalloc.h>
19 
20 #define PGD_SIZE	(PTRS_PER_PGD * sizeof(pgd_t))
21 
22 #if CONFIG_PGTABLE_LEVELS > 2
23 
24 static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
25 {
26 	set_pud(pudp, __pud(__phys_to_pud_val(pmdp) | prot));
27 }
28 
29 static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp)
30 {
31 	pudval_t pudval = PUD_TYPE_TABLE;
32 
33 	pudval |= (mm == &init_mm) ? PUD_TABLE_UXN : PUD_TABLE_PXN;
34 	__pud_populate(pudp, __pa(pmdp), pudval);
35 }
36 #else
37 static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
38 {
39 	BUILD_BUG();
40 }
41 #endif	/* CONFIG_PGTABLE_LEVELS > 2 */
42 
43 #if CONFIG_PGTABLE_LEVELS > 3
44 
45 static inline void __p4d_populate(p4d_t *p4dp, phys_addr_t pudp, p4dval_t prot)
46 {
47 	if (pgtable_l4_enabled())
48 		set_p4d(p4dp, __p4d(__phys_to_p4d_val(pudp) | prot));
49 }
50 
51 static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4dp, pud_t *pudp)
52 {
53 	p4dval_t p4dval = P4D_TYPE_TABLE;
54 
55 	p4dval |= (mm == &init_mm) ? P4D_TABLE_UXN : P4D_TABLE_PXN;
56 	__p4d_populate(p4dp, __pa(pudp), p4dval);
57 }
58 
59 static inline void pud_free(struct mm_struct *mm, pud_t *pud)
60 {
61 	if (!pgtable_l4_enabled())
62 		return;
63 	__pud_free(mm, pud);
64 }
65 #else
66 static inline void __p4d_populate(p4d_t *p4dp, phys_addr_t pudp, p4dval_t prot)
67 {
68 	BUILD_BUG();
69 }
70 #endif	/* CONFIG_PGTABLE_LEVELS > 3 */
71 
72 #if CONFIG_PGTABLE_LEVELS > 4
73 
74 static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t p4dp, pgdval_t prot)
75 {
76 	if (pgtable_l5_enabled())
77 		set_pgd(pgdp, __pgd(__phys_to_pgd_val(p4dp) | prot));
78 }
79 
80 static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp)
81 {
82 	pgdval_t pgdval = PGD_TYPE_TABLE;
83 
84 	pgdval |= (mm == &init_mm) ? PGD_TABLE_UXN : PGD_TABLE_PXN;
85 	__pgd_populate(pgdp, __pa(p4dp), pgdval);
86 }
87 
88 static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr)
89 {
90 	gfp_t gfp = GFP_PGTABLE_USER;
91 
92 	if (mm == &init_mm)
93 		gfp = GFP_PGTABLE_KERNEL;
94 	return (p4d_t *)get_zeroed_page(gfp);
95 }
96 
97 static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
98 {
99 	if (!pgtable_l5_enabled())
100 		return;
101 	BUG_ON((unsigned long)p4d & (PAGE_SIZE-1));
102 	free_page((unsigned long)p4d);
103 }
104 
105 #define __p4d_free_tlb(tlb, p4d, addr)  p4d_free((tlb)->mm, p4d)
106 #else
107 static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t p4dp, pgdval_t prot)
108 {
109 	BUILD_BUG();
110 }
111 #endif	/* CONFIG_PGTABLE_LEVELS > 4 */
112 
113 extern pgd_t *pgd_alloc(struct mm_struct *mm);
114 extern void pgd_free(struct mm_struct *mm, pgd_t *pgdp);
115 
116 static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t ptep,
117 				  pmdval_t prot)
118 {
119 	set_pmd(pmdp, __pmd(__phys_to_pmd_val(ptep) | prot));
120 }
121 
122 /*
123  * Populate the pmdp entry with a pointer to the pte.  This pmd is part
124  * of the mm address space.
125  */
126 static inline void
127 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
128 {
129 	VM_BUG_ON(mm && mm != &init_mm);
130 	__pmd_populate(pmdp, __pa(ptep), PMD_TYPE_TABLE | PMD_TABLE_UXN);
131 }
132 
133 static inline void
134 pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)
135 {
136 	VM_BUG_ON(mm == &init_mm);
137 	__pmd_populate(pmdp, page_to_phys(ptep), PMD_TYPE_TABLE | PMD_TABLE_PXN);
138 }
139 
140 #endif
141