1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (C) 2009 Chen Liqin <liqin.chen@sunplusct.com>
4 * Copyright (C) 2012 Regents of the University of California
7 #ifndef _ASM_RISCV_PGALLOC_H
8 #define _ASM_RISCV_PGALLOC_H
15 #define __HAVE_ARCH_PUD_ALLOC_ONE
16 #define __HAVE_ARCH_PUD_FREE
17 #include <asm-generic/pgalloc.h>
19 static inline void riscv_tlb_remove_ptdesc(struct mmu_gather
*tlb
, void *pt
)
21 if (riscv_use_sbi_for_rfence())
22 tlb_remove_ptdesc(tlb
, pt
);
24 tlb_remove_page_ptdesc(tlb
, pt
);
27 static inline void pmd_populate_kernel(struct mm_struct
*mm
,
28 pmd_t
*pmd
, pte_t
*pte
)
30 unsigned long pfn
= virt_to_pfn(pte
);
32 set_pmd(pmd
, __pmd((pfn
<< _PAGE_PFN_SHIFT
) | _PAGE_TABLE
));
35 static inline void pmd_populate(struct mm_struct
*mm
,
36 pmd_t
*pmd
, pgtable_t pte
)
38 unsigned long pfn
= virt_to_pfn(page_address(pte
));
40 set_pmd(pmd
, __pmd((pfn
<< _PAGE_PFN_SHIFT
) | _PAGE_TABLE
));
43 #ifndef __PAGETABLE_PMD_FOLDED
44 static inline void pud_populate(struct mm_struct
*mm
, pud_t
*pud
, pmd_t
*pmd
)
46 unsigned long pfn
= virt_to_pfn(pmd
);
48 set_pud(pud
, __pud((pfn
<< _PAGE_PFN_SHIFT
) | _PAGE_TABLE
));
51 static inline void p4d_populate(struct mm_struct
*mm
, p4d_t
*p4d
, pud_t
*pud
)
53 if (pgtable_l4_enabled
) {
54 unsigned long pfn
= virt_to_pfn(pud
);
56 set_p4d(p4d
, __p4d((pfn
<< _PAGE_PFN_SHIFT
) | _PAGE_TABLE
));
60 static inline void p4d_populate_safe(struct mm_struct
*mm
, p4d_t
*p4d
,
63 if (pgtable_l4_enabled
) {
64 unsigned long pfn
= virt_to_pfn(pud
);
67 __p4d((pfn
<< _PAGE_PFN_SHIFT
) | _PAGE_TABLE
));
71 static inline void pgd_populate(struct mm_struct
*mm
, pgd_t
*pgd
, p4d_t
*p4d
)
73 if (pgtable_l5_enabled
) {
74 unsigned long pfn
= virt_to_pfn(p4d
);
76 set_pgd(pgd
, __pgd((pfn
<< _PAGE_PFN_SHIFT
) | _PAGE_TABLE
));
80 static inline void pgd_populate_safe(struct mm_struct
*mm
, pgd_t
*pgd
,
83 if (pgtable_l5_enabled
) {
84 unsigned long pfn
= virt_to_pfn(p4d
);
87 __pgd((pfn
<< _PAGE_PFN_SHIFT
) | _PAGE_TABLE
));
91 #define pud_alloc_one pud_alloc_one
92 static inline pud_t
*pud_alloc_one(struct mm_struct
*mm
, unsigned long addr
)
94 if (pgtable_l4_enabled
)
95 return __pud_alloc_one(mm
, addr
);
100 #define pud_free pud_free
101 static inline void pud_free(struct mm_struct
*mm
, pud_t
*pud
)
103 if (pgtable_l4_enabled
)
107 static inline void __pud_free_tlb(struct mmu_gather
*tlb
, pud_t
*pud
,
110 if (pgtable_l4_enabled
) {
111 struct ptdesc
*ptdesc
= virt_to_ptdesc(pud
);
113 pagetable_pud_dtor(ptdesc
);
114 riscv_tlb_remove_ptdesc(tlb
, ptdesc
);
118 #define p4d_alloc_one p4d_alloc_one
119 static inline p4d_t
*p4d_alloc_one(struct mm_struct
*mm
, unsigned long addr
)
121 if (pgtable_l5_enabled
) {
122 gfp_t gfp
= GFP_PGTABLE_USER
;
125 gfp
= GFP_PGTABLE_KERNEL
;
126 return (p4d_t
*)get_zeroed_page(gfp
);
132 static inline void __p4d_free(struct mm_struct
*mm
, p4d_t
*p4d
)
134 BUG_ON((unsigned long)p4d
& (PAGE_SIZE
-1));
135 free_page((unsigned long)p4d
);
138 #define p4d_free p4d_free
139 static inline void p4d_free(struct mm_struct
*mm
, p4d_t
*p4d
)
141 if (pgtable_l5_enabled
)
145 static inline void __p4d_free_tlb(struct mmu_gather
*tlb
, p4d_t
*p4d
,
148 if (pgtable_l5_enabled
)
149 riscv_tlb_remove_ptdesc(tlb
, virt_to_ptdesc(p4d
));
151 #endif /* __PAGETABLE_PMD_FOLDED */
153 static inline void sync_kernel_mappings(pgd_t
*pgd
)
155 memcpy(pgd
+ USER_PTRS_PER_PGD
,
156 init_mm
.pgd
+ USER_PTRS_PER_PGD
,
157 (PTRS_PER_PGD
- USER_PTRS_PER_PGD
) * sizeof(pgd_t
));
160 static inline pgd_t
*pgd_alloc(struct mm_struct
*mm
)
164 pgd
= (pgd_t
*)__get_free_page(GFP_KERNEL
);
165 if (likely(pgd
!= NULL
)) {
166 memset(pgd
, 0, USER_PTRS_PER_PGD
* sizeof(pgd_t
));
167 /* Copy kernel mappings */
168 sync_kernel_mappings(pgd
);
173 #ifndef __PAGETABLE_PMD_FOLDED
175 static inline void __pmd_free_tlb(struct mmu_gather
*tlb
, pmd_t
*pmd
,
178 struct ptdesc
*ptdesc
= virt_to_ptdesc(pmd
);
180 pagetable_pmd_dtor(ptdesc
);
181 riscv_tlb_remove_ptdesc(tlb
, ptdesc
);
184 #endif /* __PAGETABLE_PMD_FOLDED */
186 static inline void __pte_free_tlb(struct mmu_gather
*tlb
, pgtable_t pte
,
189 struct ptdesc
*ptdesc
= page_ptdesc(pte
);
191 pagetable_pte_dtor(ptdesc
);
192 riscv_tlb_remove_ptdesc(tlb
, ptdesc
);
194 #endif /* CONFIG_MMU */
196 #endif /* _ASM_RISCV_PGALLOC_H */