1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
4  */
5 #ifndef _ASM_PGALLOC_H
6 #define _ASM_PGALLOC_H
7 
8 #include <linux/mm.h>
9 #include <linux/sched.h>
10 
11 #define __HAVE_ARCH_PMD_ALLOC_ONE
12 #define __HAVE_ARCH_PUD_ALLOC_ONE
13 #include <asm-generic/pgalloc.h>
14 
15 static inline void pmd_populate_kernel(struct mm_struct *mm,
16 				       pmd_t *pmd, pte_t *pte)
17 {
18 	set_pmd(pmd, __pmd((unsigned long)pte));
19 }
20 
21 static inline void pmd_populate(struct mm_struct *mm, pmd_t *pmd, pgtable_t pte)
22 {
23 	set_pmd(pmd, __pmd((unsigned long)page_address(pte)));
24 }
25 
26 #ifndef __PAGETABLE_PMD_FOLDED
27 
28 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
29 {
30 	set_pud(pud, __pud((unsigned long)pmd));
31 }
32 #endif
33 
34 #ifndef __PAGETABLE_PUD_FOLDED
35 
36 static inline void p4d_populate(struct mm_struct *mm, p4d_t *p4d, pud_t *pud)
37 {
38 	set_p4d(p4d, __p4d((unsigned long)pud));
39 }
40 
41 #endif /* __PAGETABLE_PUD_FOLDED */
42 
43 extern void pagetable_init(void);
44 
45 /*
46  * Initialize a new pmd table with invalid pointers.
47  */
48 extern void pmd_init(unsigned long page, unsigned long pagetable);
49 
50 /*
51  * Initialize a new pgd / pmd table with invalid pointers.
52  */
53 extern void pgd_init(unsigned long page);
54 extern pgd_t *pgd_alloc(struct mm_struct *mm);
55 
56 #define __pte_free_tlb(tlb, pte, address)			\
57 do {							\
58 	pgtable_pte_page_dtor(pte);			\
59 	tlb_remove_page((tlb), pte);			\
60 } while (0)
61 
62 #ifndef __PAGETABLE_PMD_FOLDED
63 
64 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long address)
65 {
66 	pmd_t *pmd;
67 	struct page *pg;
68 
69 	pg = alloc_pages(GFP_KERNEL_ACCOUNT, PMD_ORDER);
70 	if (!pg)
71 		return NULL;
72 
73 	if (!pgtable_pmd_page_ctor(pg)) {
74 		__free_pages(pg, PMD_ORDER);
75 		return NULL;
76 	}
77 
78 	pmd = (pmd_t *)page_address(pg);
79 	pmd_init((unsigned long)pmd, (unsigned long)invalid_pte_table);
80 	return pmd;
81 }
82 
83 #define __pmd_free_tlb(tlb, x, addr)	pmd_free((tlb)->mm, x)
84 
85 #endif
86 
87 #ifndef __PAGETABLE_PUD_FOLDED
88 
89 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long address)
90 {
91 	pud_t *pud;
92 
93 	pud = (pud_t *) __get_free_pages(GFP_KERNEL, PUD_ORDER);
94 	if (pud)
95 		pud_init((unsigned long)pud, (unsigned long)invalid_pmd_table);
96 	return pud;
97 }
98 
99 #define __pud_free_tlb(tlb, x, addr)	pud_free((tlb)->mm, x)
100 
101 #endif /* __PAGETABLE_PUD_FOLDED */
102 
103 #endif /* _ASM_PGALLOC_H */
104