xref: /openbmc/linux/arch/arm/include/asm/pgalloc.h (revision 4e95bc26)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *  arch/arm/include/asm/pgalloc.h
4  *
5  *  Copyright (C) 2000-2001 Russell King
6  */
7 #ifndef _ASMARM_PGALLOC_H
8 #define _ASMARM_PGALLOC_H
9 
10 #include <linux/pagemap.h>
11 
12 #include <asm/domain.h>
13 #include <asm/pgtable-hwdef.h>
14 #include <asm/processor.h>
15 #include <asm/cacheflush.h>
16 #include <asm/tlbflush.h>
17 
18 #define check_pgt_cache()		do { } while (0)
19 
20 #ifdef CONFIG_MMU
21 
22 #define _PAGE_USER_TABLE	(PMD_TYPE_TABLE | PMD_BIT4 | PMD_DOMAIN(DOMAIN_USER))
23 #define _PAGE_KERNEL_TABLE	(PMD_TYPE_TABLE | PMD_BIT4 | PMD_DOMAIN(DOMAIN_KERNEL))
24 
25 #ifdef CONFIG_ARM_LPAE
26 
27 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
28 {
29 	return (pmd_t *)get_zeroed_page(GFP_KERNEL);
30 }
31 
32 static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
33 {
34 	BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
35 	free_page((unsigned long)pmd);
36 }
37 
38 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
39 {
40 	set_pud(pud, __pud(__pa(pmd) | PMD_TYPE_TABLE));
41 }
42 
43 #else	/* !CONFIG_ARM_LPAE */
44 
45 /*
46  * Since we have only two-level page tables, these are trivial
47  */
48 #define pmd_alloc_one(mm,addr)		({ BUG(); ((pmd_t *)2); })
49 #define pmd_free(mm, pmd)		do { } while (0)
50 #define pud_populate(mm,pmd,pte)	BUG()
51 
52 #endif	/* CONFIG_ARM_LPAE */
53 
54 extern pgd_t *pgd_alloc(struct mm_struct *mm);
55 extern void pgd_free(struct mm_struct *mm, pgd_t *pgd);
56 
57 #define PGALLOC_GFP	(GFP_KERNEL | __GFP_ZERO)
58 
59 static inline void clean_pte_table(pte_t *pte)
60 {
61 	clean_dcache_area(pte + PTE_HWTABLE_PTRS, PTE_HWTABLE_SIZE);
62 }
63 
64 /*
65  * Allocate one PTE table.
66  *
67  * This actually allocates two hardware PTE tables, but we wrap this up
68  * into one table thus:
69  *
70  *  +------------+
71  *  | Linux pt 0 |
72  *  +------------+
73  *  | Linux pt 1 |
74  *  +------------+
75  *  |  h/w pt 0  |
76  *  +------------+
77  *  |  h/w pt 1  |
78  *  +------------+
79  */
80 static inline pte_t *
81 pte_alloc_one_kernel(struct mm_struct *mm)
82 {
83 	pte_t *pte;
84 
85 	pte = (pte_t *)__get_free_page(PGALLOC_GFP);
86 	if (pte)
87 		clean_pte_table(pte);
88 
89 	return pte;
90 }
91 
92 static inline pgtable_t
93 pte_alloc_one(struct mm_struct *mm)
94 {
95 	struct page *pte;
96 
97 #ifdef CONFIG_HIGHPTE
98 	pte = alloc_pages(PGALLOC_GFP | __GFP_HIGHMEM, 0);
99 #else
100 	pte = alloc_pages(PGALLOC_GFP, 0);
101 #endif
102 	if (!pte)
103 		return NULL;
104 	if (!PageHighMem(pte))
105 		clean_pte_table(page_address(pte));
106 	if (!pgtable_page_ctor(pte)) {
107 		__free_page(pte);
108 		return NULL;
109 	}
110 	return pte;
111 }
112 
113 /*
114  * Free one PTE table.
115  */
116 static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
117 {
118 	if (pte)
119 		free_page((unsigned long)pte);
120 }
121 
122 static inline void pte_free(struct mm_struct *mm, pgtable_t pte)
123 {
124 	pgtable_page_dtor(pte);
125 	__free_page(pte);
126 }
127 
128 static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t pte,
129 				  pmdval_t prot)
130 {
131 	pmdval_t pmdval = (pte + PTE_HWTABLE_OFF) | prot;
132 	pmdp[0] = __pmd(pmdval);
133 #ifndef CONFIG_ARM_LPAE
134 	pmdp[1] = __pmd(pmdval + 256 * sizeof(pte_t));
135 #endif
136 	flush_pmd_entry(pmdp);
137 }
138 
139 /*
140  * Populate the pmdp entry with a pointer to the pte.  This pmd is part
141  * of the mm address space.
142  *
143  * Ensure that we always set both PMD entries.
144  */
145 static inline void
146 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
147 {
148 	/*
149 	 * The pmd must be loaded with the physical address of the PTE table
150 	 */
151 	__pmd_populate(pmdp, __pa(ptep), _PAGE_KERNEL_TABLE);
152 }
153 
154 static inline void
155 pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)
156 {
157 	extern pmdval_t user_pmd_table;
158 	pmdval_t prot;
159 
160 	if (__LINUX_ARM_ARCH__ >= 6 && !IS_ENABLED(CONFIG_ARM_LPAE))
161 		prot = user_pmd_table;
162 	else
163 		prot = _PAGE_USER_TABLE;
164 
165 	__pmd_populate(pmdp, page_to_phys(ptep), prot);
166 }
167 #define pmd_pgtable(pmd) pmd_page(pmd)
168 
169 #endif /* CONFIG_MMU */
170 
171 #endif
172