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 
__pud_populate(pud_t * pudp,phys_addr_t pmdp,pudval_t prot)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 
pud_populate(struct mm_struct * mm,pud_t * pudp,pmd_t * pmdp)29 static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp)
30 {
31 	pudval_t pudval = PUD_TYPE_TABLE | PUD_TABLE_AF;
32 
33 	pudval |= (mm == &init_mm) ? PUD_TABLE_UXN : PUD_TABLE_PXN;
34 	__pud_populate(pudp, __pa(pmdp), pudval);
35 }
36 #else
__pud_populate(pud_t * pudp,phys_addr_t pmdp,pudval_t prot)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 
__p4d_populate(p4d_t * p4dp,phys_addr_t pudp,p4dval_t prot)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 
p4d_populate(struct mm_struct * mm,p4d_t * p4dp,pud_t * pudp)51 static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4dp, pud_t *pudp)
52 {
53 	p4dval_t p4dval = P4D_TYPE_TABLE | P4D_TABLE_AF;
54 
55 	p4dval |= (mm == &init_mm) ? P4D_TABLE_UXN : P4D_TABLE_PXN;
56 	__p4d_populate(p4dp, __pa(pudp), p4dval);
57 }
58 
pud_free(struct mm_struct * mm,pud_t * pud)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
__p4d_populate(p4d_t * p4dp,phys_addr_t pudp,p4dval_t prot)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 
__pgd_populate(pgd_t * pgdp,phys_addr_t p4dp,pgdval_t prot)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 
pgd_populate(struct mm_struct * mm,pgd_t * pgdp,p4d_t * p4dp)80 static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, p4d_t *p4dp)
81 {
82 	pgdval_t pgdval = PGD_TYPE_TABLE | PGD_TABLE_AF;
83 
84 	pgdval |= (mm == &init_mm) ? PGD_TABLE_UXN : PGD_TABLE_PXN;
85 	__pgd_populate(pgdp, __pa(p4dp), pgdval);
86 }
87 
88 #else
__pgd_populate(pgd_t * pgdp,phys_addr_t p4dp,pgdval_t prot)89 static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t p4dp, pgdval_t prot)
90 {
91 	BUILD_BUG();
92 }
93 #endif	/* CONFIG_PGTABLE_LEVELS > 4 */
94 
95 extern pgd_t *pgd_alloc(struct mm_struct *mm);
96 extern void pgd_free(struct mm_struct *mm, pgd_t *pgdp);
97 
__pmd_populate(pmd_t * pmdp,phys_addr_t ptep,pmdval_t prot)98 static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t ptep,
99 				  pmdval_t prot)
100 {
101 	set_pmd(pmdp, __pmd(__phys_to_pmd_val(ptep) | prot));
102 }
103 
104 /*
105  * Populate the pmdp entry with a pointer to the pte.  This pmd is part
106  * of the mm address space.
107  */
108 static inline void
pmd_populate_kernel(struct mm_struct * mm,pmd_t * pmdp,pte_t * ptep)109 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
110 {
111 	VM_BUG_ON(mm && mm != &init_mm);
112 	__pmd_populate(pmdp, __pa(ptep),
113 		       PMD_TYPE_TABLE | PMD_TABLE_AF | PMD_TABLE_UXN);
114 }
115 
116 static inline void
pmd_populate(struct mm_struct * mm,pmd_t * pmdp,pgtable_t ptep)117 pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)
118 {
119 	VM_BUG_ON(mm == &init_mm);
120 	__pmd_populate(pmdp, page_to_phys(ptep),
121 		       PMD_TYPE_TABLE | PMD_TABLE_AF | PMD_TABLE_PXN);
122 }
123 
124 #endif
125