arm64: mm: Add support for folding PUDs at runtime
[sfrench/cifs-2.6.git] / arch / arm64 / include / asm / pgalloc.h
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         BUG_ON((unsigned long)pud & (PAGE_SIZE-1));
64         free_page((unsigned long)pud);
65 }
66 #else
67 static inline void __p4d_populate(p4d_t *p4dp, phys_addr_t pudp, p4dval_t prot)
68 {
69         BUILD_BUG();
70 }
71 #endif  /* CONFIG_PGTABLE_LEVELS > 3 */
72
73 #if CONFIG_PGTABLE_LEVELS > 4
74
75 static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t p4dp, pgdval_t prot)
76 {
77         if (pgtable_l5_enabled())
78                 set_pgd(pgdp, __pgd(__phys_to_pgd_val(p4dp) | prot));
79 }
80
81 static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp)
82 {
83         pgdval_t pgdval = PGD_TYPE_TABLE;
84
85         pgdval |= (mm == &init_mm) ? PGD_TABLE_UXN : PGD_TABLE_PXN;
86         __pgd_populate(pgdp, __pa(p4dp), pgdval);
87 }
88
89 static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr)
90 {
91         gfp_t gfp = GFP_PGTABLE_USER;
92
93         if (mm == &init_mm)
94                 gfp = GFP_PGTABLE_KERNEL;
95         return (p4d_t *)get_zeroed_page(gfp);
96 }
97
98 static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
99 {
100         if (!pgtable_l5_enabled())
101                 return;
102         BUG_ON((unsigned long)p4d & (PAGE_SIZE-1));
103         free_page((unsigned long)p4d);
104 }
105
106 #define __p4d_free_tlb(tlb, p4d, addr)  p4d_free((tlb)->mm, p4d)
107 #else
108 static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t p4dp, pgdval_t prot)
109 {
110         BUILD_BUG();
111 }
112 #endif  /* CONFIG_PGTABLE_LEVELS > 4 */
113
114 extern pgd_t *pgd_alloc(struct mm_struct *mm);
115 extern void pgd_free(struct mm_struct *mm, pgd_t *pgdp);
116
117 static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t ptep,
118                                   pmdval_t prot)
119 {
120         set_pmd(pmdp, __pmd(__phys_to_pmd_val(ptep) | prot));
121 }
122
123 /*
124  * Populate the pmdp entry with a pointer to the pte.  This pmd is part
125  * of the mm address space.
126  */
127 static inline void
128 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
129 {
130         VM_BUG_ON(mm && mm != &init_mm);
131         __pmd_populate(pmdp, __pa(ptep), PMD_TYPE_TABLE | PMD_TABLE_UXN);
132 }
133
134 static inline void
135 pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)
136 {
137         VM_BUG_ON(mm == &init_mm);
138         __pmd_populate(pmdp, page_to_phys(ptep), PMD_TYPE_TABLE | PMD_TABLE_PXN);
139 }
140
141 #endif