Commit | Line | Data |
---|---|---|
50acfb2b | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
07037db5 PD |
2 | /* |
3 | * Copyright (C) 2009 Chen Liqin <liqin.chen@sunplusct.com> | |
4 | * Copyright (C) 2012 Regents of the University of California | |
07037db5 PD |
5 | */ |
6 | ||
7 | #ifndef _ASM_RISCV_PGALLOC_H | |
8 | #define _ASM_RISCV_PGALLOC_H | |
9 | ||
10 | #include <linux/mm.h> | |
dc892fb4 | 11 | #include <asm/sbi.h> |
07037db5 PD |
12 | #include <asm/tlb.h> |
13 | ||
6bd33e1e | 14 | #ifdef CONFIG_MMU |
e8a62cc2 | 15 | #define __HAVE_ARCH_PUD_FREE |
1355c31e | 16 | #include <asm-generic/pgalloc.h> |
d1b46fe5 | 17 | |
07037db5 PD |
18 | static inline void pmd_populate_kernel(struct mm_struct *mm, |
19 | pmd_t *pmd, pte_t *pte) | |
20 | { | |
21 | unsigned long pfn = virt_to_pfn(pte); | |
22 | ||
23 | set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); | |
24 | } | |
25 | ||
26 | static inline void pmd_populate(struct mm_struct *mm, | |
27 | pmd_t *pmd, pgtable_t pte) | |
28 | { | |
29 | unsigned long pfn = virt_to_pfn(page_address(pte)); | |
30 | ||
31 | set_pmd(pmd, __pmd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); | |
32 | } | |
33 | ||
34 | #ifndef __PAGETABLE_PMD_FOLDED | |
35 | static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd) | |
36 | { | |
37 | unsigned long pfn = virt_to_pfn(pmd); | |
38 | ||
39 | set_pud(pud, __pud((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); | |
40 | } | |
e8a62cc2 AG |
41 | |
42 | static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud) | |
43 | { | |
44 | if (pgtable_l4_enabled) { | |
45 | unsigned long pfn = virt_to_pfn(pud); | |
46 | ||
47 | set_p4d(p4d, __p4d((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); | |
48 | } | |
49 | } | |
50 | ||
51 | static inline void p4d_populate_safe(struct mm_struct *mm, p4d_t *p4d, | |
52 | pud_t *pud) | |
53 | { | |
54 | if (pgtable_l4_enabled) { | |
55 | unsigned long pfn = virt_to_pfn(pud); | |
56 | ||
57 | set_p4d_safe(p4d, | |
58 | __p4d((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); | |
59 | } | |
60 | } | |
61 | ||
d10efa21 QP |
62 | static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgd, p4d_t *p4d) |
63 | { | |
64 | if (pgtable_l5_enabled) { | |
65 | unsigned long pfn = virt_to_pfn(p4d); | |
66 | ||
67 | set_pgd(pgd, __pgd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); | |
68 | } | |
69 | } | |
70 | ||
71 | static inline void pgd_populate_safe(struct mm_struct *mm, pgd_t *pgd, | |
72 | p4d_t *p4d) | |
73 | { | |
74 | if (pgtable_l5_enabled) { | |
75 | unsigned long pfn = virt_to_pfn(p4d); | |
76 | ||
77 | set_pgd_safe(pgd, | |
78 | __pgd((pfn << _PAGE_PFN_SHIFT) | _PAGE_TABLE)); | |
79 | } | |
80 | } | |
81 | ||
e8a62cc2 AG |
82 | #define pud_free pud_free |
83 | static inline void pud_free(struct mm_struct *mm, pud_t *pud) | |
84 | { | |
85 | if (pgtable_l4_enabled) | |
86 | __pud_free(mm, pud); | |
87 | } | |
88 | ||
40d1bb92 JZ |
89 | static inline void __pud_free_tlb(struct mmu_gather *tlb, pud_t *pud, |
90 | unsigned long addr) | |
91 | { | |
deab5a35 | 92 | if (pgtable_l4_enabled) |
4239c198 | 93 | tlb_remove_ptdesc(tlb, virt_to_ptdesc(pud)); |
40d1bb92 | 94 | } |
d10efa21 | 95 | |
40d1bb92 JZ |
96 | static inline void __p4d_free_tlb(struct mmu_gather *tlb, p4d_t *p4d, |
97 | unsigned long addr) | |
98 | { | |
deab5a35 | 99 | if (pgtable_l5_enabled) |
4239c198 | 100 | tlb_remove_ptdesc(tlb, virt_to_ptdesc(p4d)); |
40d1bb92 | 101 | } |
07037db5 PD |
102 | #endif /* __PAGETABLE_PMD_FOLDED */ |
103 | ||
3f105a74 AG |
104 | static inline void sync_kernel_mappings(pgd_t *pgd) |
105 | { | |
106 | memcpy(pgd + USER_PTRS_PER_PGD, | |
107 | init_mm.pgd + USER_PTRS_PER_PGD, | |
108 | (PTRS_PER_PGD - USER_PTRS_PER_PGD) * sizeof(pgd_t)); | |
109 | } | |
110 | ||
07037db5 PD |
111 | static inline pgd_t *pgd_alloc(struct mm_struct *mm) |
112 | { | |
113 | pgd_t *pgd; | |
114 | ||
a9b3c355 | 115 | pgd = __pgd_alloc(mm, 0); |
07037db5 | 116 | if (likely(pgd != NULL)) { |
07037db5 | 117 | /* Copy kernel mappings */ |
3f105a74 | 118 | sync_kernel_mappings(pgd); |
07037db5 PD |
119 | } |
120 | return pgd; | |
121 | } | |
122 | ||
07037db5 PD |
123 | #ifndef __PAGETABLE_PMD_FOLDED |
124 | ||
40d1bb92 JZ |
125 | static inline void __pmd_free_tlb(struct mmu_gather *tlb, pmd_t *pmd, |
126 | unsigned long addr) | |
127 | { | |
4239c198 | 128 | tlb_remove_ptdesc(tlb, virt_to_ptdesc(pmd)); |
40d1bb92 | 129 | } |
07037db5 PD |
130 | |
131 | #endif /* __PAGETABLE_PMD_FOLDED */ | |
132 | ||
40d1bb92 JZ |
133 | static inline void __pte_free_tlb(struct mmu_gather *tlb, pgtable_t pte, |
134 | unsigned long addr) | |
135 | { | |
4239c198 | 136 | tlb_remove_ptdesc(tlb, page_ptdesc(pte)); |
40d1bb92 | 137 | } |
6bd33e1e | 138 | #endif /* CONFIG_MMU */ |
07037db5 | 139 | |
07037db5 | 140 | #endif /* _ASM_RISCV_PGALLOC_H */ |