xref: /linux/arch/arm64/include/asm/pgalloc.h (revision c4ee0af3fa0dc65f690fc908f02b8355f9576ea0)
1 /*
2  * Based on arch/arm/include/asm/pgalloc.h
3  *
4  * Copyright (C) 2000-2001 Russell King
5  * Copyright (C) 2012 ARM Ltd.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef __ASM_PGALLOC_H
20 #define __ASM_PGALLOC_H
21 
22 #include <asm/pgtable-hwdef.h>
23 #include <asm/processor.h>
24 #include <asm/cacheflush.h>
25 #include <asm/tlbflush.h>
26 
27 #define check_pgt_cache()		do { } while (0)
28 
29 #ifndef CONFIG_ARM64_64K_PAGES
30 
31 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
32 {
33 	return (pmd_t *)get_zeroed_page(GFP_KERNEL | __GFP_REPEAT);
34 }
35 
36 static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
37 {
38 	BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
39 	free_page((unsigned long)pmd);
40 }
41 
42 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
43 {
44 	set_pud(pud, __pud(__pa(pmd) | PMD_TYPE_TABLE));
45 }
46 
47 #endif	/* CONFIG_ARM64_64K_PAGES */
48 
49 extern pgd_t *pgd_alloc(struct mm_struct *mm);
50 extern void pgd_free(struct mm_struct *mm, pgd_t *pgd);
51 
52 #define PGALLOC_GFP	(GFP_KERNEL | __GFP_NOTRACK | __GFP_REPEAT | __GFP_ZERO)
53 
54 static inline pte_t *
55 pte_alloc_one_kernel(struct mm_struct *mm, unsigned long addr)
56 {
57 	return (pte_t *)__get_free_page(PGALLOC_GFP);
58 }
59 
60 static inline pgtable_t
61 pte_alloc_one(struct mm_struct *mm, unsigned long addr)
62 {
63 	struct page *pte;
64 
65 	pte = alloc_pages(PGALLOC_GFP, 0);
66 	if (!pte)
67 		return NULL;
68 	if (!pgtable_page_ctor(pte)) {
69 		__free_page(pte);
70 		return NULL;
71 	}
72 	return pte;
73 }
74 
75 /*
76  * Free a PTE table.
77  */
78 static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
79 {
80 	if (pte)
81 		free_page((unsigned long)pte);
82 }
83 
84 static inline void pte_free(struct mm_struct *mm, pgtable_t pte)
85 {
86 	pgtable_page_dtor(pte);
87 	__free_page(pte);
88 }
89 
90 static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t pte,
91 				  pmdval_t prot)
92 {
93 	set_pmd(pmdp, __pmd(pte | prot));
94 }
95 
96 /*
97  * Populate the pmdp entry with a pointer to the pte.  This pmd is part
98  * of the mm address space.
99  */
100 static inline void
101 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
102 {
103 	/*
104 	 * The pmd must be loaded with the physical address of the PTE table
105 	 */
106 	__pmd_populate(pmdp, __pa(ptep), PMD_TYPE_TABLE);
107 }
108 
109 static inline void
110 pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)
111 {
112 	__pmd_populate(pmdp, page_to_phys(ptep), PMD_TYPE_TABLE);
113 }
114 #define pmd_pgtable(pmd) pmd_page(pmd)
115 
116 #endif
117