xref: /openbmc/linux/arch/arm64/include/asm/pgalloc.h (revision a61127c2)
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 #define PGALLOC_GFP	(GFP_KERNEL | __GFP_ZERO)
30 #define PGD_SIZE	(PTRS_PER_PGD * sizeof(pgd_t))
31 
32 #if CONFIG_PGTABLE_LEVELS > 2
33 
34 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
35 {
36 	struct page *page;
37 
38 	page = alloc_page(PGALLOC_GFP);
39 	if (!page)
40 		return NULL;
41 	if (!pgtable_pmd_page_ctor(page)) {
42 		__free_page(page);
43 		return NULL;
44 	}
45 	return page_address(page);
46 }
47 
48 static inline void pmd_free(struct mm_struct *mm, pmd_t *pmdp)
49 {
50 	BUG_ON((unsigned long)pmdp & (PAGE_SIZE-1));
51 	pgtable_pmd_page_dtor(virt_to_page(pmdp));
52 	free_page((unsigned long)pmdp);
53 }
54 
55 static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
56 {
57 	set_pud(pudp, __pud(__phys_to_pud_val(pmdp) | prot));
58 }
59 
60 static inline void pud_populate(struct mm_struct *mm, pud_t *pudp, pmd_t *pmdp)
61 {
62 	__pud_populate(pudp, __pa(pmdp), PMD_TYPE_TABLE);
63 }
64 #else
65 static inline void __pud_populate(pud_t *pudp, phys_addr_t pmdp, pudval_t prot)
66 {
67 	BUILD_BUG();
68 }
69 #endif	/* CONFIG_PGTABLE_LEVELS > 2 */
70 
71 #if CONFIG_PGTABLE_LEVELS > 3
72 
73 static inline pud_t *pud_alloc_one(struct mm_struct *mm, unsigned long addr)
74 {
75 	return (pud_t *)__get_free_page(PGALLOC_GFP);
76 }
77 
78 static inline void pud_free(struct mm_struct *mm, pud_t *pudp)
79 {
80 	BUG_ON((unsigned long)pudp & (PAGE_SIZE-1));
81 	free_page((unsigned long)pudp);
82 }
83 
84 static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t pudp, pgdval_t prot)
85 {
86 	set_pgd(pgdp, __pgd(__phys_to_pgd_val(pudp) | prot));
87 }
88 
89 static inline void pgd_populate(struct mm_struct *mm, pgd_t *pgdp, pud_t *pudp)
90 {
91 	__pgd_populate(pgdp, __pa(pudp), PUD_TYPE_TABLE);
92 }
93 #else
94 static inline void __pgd_populate(pgd_t *pgdp, phys_addr_t pudp, pgdval_t prot)
95 {
96 	BUILD_BUG();
97 }
98 #endif	/* CONFIG_PGTABLE_LEVELS > 3 */
99 
100 extern pgd_t *pgd_alloc(struct mm_struct *mm);
101 extern void pgd_free(struct mm_struct *mm, pgd_t *pgdp);
102 
103 static inline pte_t *
104 pte_alloc_one_kernel(struct mm_struct *mm)
105 {
106 	return (pte_t *)__get_free_page(PGALLOC_GFP);
107 }
108 
109 static inline pgtable_t
110 pte_alloc_one(struct mm_struct *mm)
111 {
112 	struct page *pte;
113 
114 	pte = alloc_pages(PGALLOC_GFP, 0);
115 	if (!pte)
116 		return NULL;
117 	if (!pgtable_page_ctor(pte)) {
118 		__free_page(pte);
119 		return NULL;
120 	}
121 	return pte;
122 }
123 
124 /*
125  * Free a PTE table.
126  */
127 static inline void pte_free_kernel(struct mm_struct *mm, pte_t *ptep)
128 {
129 	if (ptep)
130 		free_page((unsigned long)ptep);
131 }
132 
133 static inline void pte_free(struct mm_struct *mm, pgtable_t pte)
134 {
135 	pgtable_page_dtor(pte);
136 	__free_page(pte);
137 }
138 
139 static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t ptep,
140 				  pmdval_t prot)
141 {
142 	set_pmd(pmdp, __pmd(__phys_to_pmd_val(ptep) | prot));
143 }
144 
145 /*
146  * Populate the pmdp entry with a pointer to the pte.  This pmd is part
147  * of the mm address space.
148  */
149 static inline void
150 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
151 {
152 	/*
153 	 * The pmd must be loaded with the physical address of the PTE table
154 	 */
155 	__pmd_populate(pmdp, __pa(ptep), PMD_TYPE_TABLE);
156 }
157 
158 static inline void
159 pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)
160 {
161 	__pmd_populate(pmdp, page_to_phys(ptep), PMD_TYPE_TABLE);
162 }
163 #define pmd_pgtable(pmd) pmd_page(pmd)
164 
165 #endif
166