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
14 #define __HAVE_ARCH_PUD_ALLOC_ONE
15 #define __HAVE_ARCH_PUD_FREE
16 #include <asm-generic/pgalloc.h>
18 static inline void pmd_populate_kernel(struct mm_struct *mm,
19 pmd_t *pmd, pte_t *pte)
21 unsigned long pfn = virt_to_pfn(pte);
23 set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
26 static inline void pmd_populate(struct mm_struct *mm,
27 pmd_t *pmd, pgtable_t pte)
29 unsigned long pfn = virt_to_pfn(page_address(pte));
31 set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
34 #ifndef __PAGETABLE_PMD_FOLDED
35 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
37 unsigned long pfn = virt_to_pfn(pmd);
39 set_pud(pud, __pud((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
42 static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud)
44 if (pgtable_l4_enabled) {
45 unsigned long pfn = virt_to_pfn(pud);
47 set_p4d(p4d, __p4d((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
51 static inline void p4d_populate_safe(struct mm_struct *mm, p4d_t *p4d,
54 if (pgtable_l4_enabled) {
55 unsigned long pfn = virt_to_pfn(pud);
58 __p4d((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
62 static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d)
64 if (pgtable_l5_enabled) {
65 unsigned long pfn = virt_to_pfn(p4d);
67 set_pgd(pgd, __pgd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
71 static inline void pgd_populate_safe(struct mm_struct *mm, pgd_t *pgd,
74 if (pgtable_l5_enabled) {
75 unsigned long pfn = virt_to_pfn(p4d);
78 __pgd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE));
82 #define pud_alloc_one pud_alloc_one
83 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
85 if (pgtable_l4_enabled)
86 return __pud_alloc_one(mm, addr);
91 #define pud_free pud_free
92 static inline void pud_free(struct mm_struct *mm, pud_t *pud)
94 if (pgtable_l4_enabled)
98 #define __pud_free_tlb(tlb, pud, addr) pud_free((tlb)->mm, pud)
100 #define p4d_alloc_one p4d_alloc_one
101 static inline p4d_t *p4d_alloc_one(struct mm_struct *mm, unsigned long addr)
103 if (pgtable_l5_enabled) {
104 gfp_t gfp = GFP_PGTABLE_USER;
107 gfp = GFP_PGTABLE_KERNEL;
108 return (p4d_t *)get_zeroed_page(gfp);
114 static inline void __p4d_free(struct mm_struct *mm, p4d_t *p4d)
116 BUG_ON((unsigned long)p4d & (PAGE_SIZE-1));
117 free_page((unsigned long)p4d);
120 #define p4d_free p4d_free
121 static inline void p4d_free(struct mm_struct *mm, p4d_t *p4d)
123 if (pgtable_l5_enabled)
127 #define __p4d_free_tlb(tlb, p4d, addr) p4d_free((tlb)->mm, p4d)
128 #endif /* __PAGETABLE_PMD_FOLDED */
130 static inline pgd_t *pgd_alloc(struct mm_struct *mm)
134 pgd = (pgd_t *)__get_free_page(GFP_KERNEL);
135 if (likely(pgd != NULL)) {
136 memset(pgd, 0, USER_PTRS_PER_PGD * sizeof(pgd_t));
137 /* Copy kernel mappings */
138 memcpy(pgd + USER_PTRS_PER_PGD,
139 init_mm.pgd + USER_PTRS_PER_PGD,
140 (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t));
145 #ifndef __PAGETABLE_PMD_FOLDED
147 #define __pmd_free_tlb(tlb, pmd, addr) pmd_free((tlb)->mm, pmd)
149 #endif /* __PAGETABLE_PMD_FOLDED */
151 #define __pte_free_tlb(tlb, pte, buf) \
153 pgtable_pte_page_dtor(pte); \
154 tlb_remove_page((tlb), pte); \
156 #endif /* CONFIG_MMU */
158 #endif /* _ASM_RISCV_PGALLOC_H */