1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * arch/arm/include/asm/pgalloc.h
5 * Copyright (C) 2000-2001 Russell King
7 #ifndef _ASMARM_PGALLOC_H
8 #define _ASMARM_PGALLOC_H
10 #include <linux/pagemap.h>
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>
18 #define check_pgt_cache() do { } while (0)
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))
25 #ifdef CONFIG_ARM_LPAE
27 static inline pmd_t *pmd_alloc_one(struct mm_struct *mm, unsigned long addr)
29 return (pmd_t *)get_zeroed_page(GFP_KERNEL);
32 static inline void pmd_free(struct mm_struct *mm, pmd_t *pmd)
34 BUG_ON((unsigned long)pmd & (PAGE_SIZE-1));
35 free_page((unsigned long)pmd);
38 static inline void pud_populate(struct mm_struct *mm, pud_t *pud, pmd_t *pmd)
40 set_pud(pud, __pud(__pa(pmd) | PMD_TYPE_TABLE));
43 #else /* !CONFIG_ARM_LPAE */
46 * Since we have only two-level page tables, these are trivial
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()
52 #endif /* CONFIG_ARM_LPAE */
54 extern pgd_t *pgd_alloc(struct mm_struct *mm);
55 extern void pgd_free(struct mm_struct *mm, pgd_t *pgd);
57 #define PGALLOC_GFP (GFP_KERNEL | __GFP_ZERO)
59 static inline void clean_pte_table(pte_t *pte)
61 clean_dcache_area(pte + PTE_HWTABLE_PTRS, PTE_HWTABLE_SIZE);
65 * Allocate one PTE table.
67 * This actually allocates two hardware PTE tables, but we wrap this up
68 * into one table thus:
81 pte_alloc_one_kernel(struct mm_struct *mm)
85 pte = (pte_t *)__get_free_page(PGALLOC_GFP);
92 static inline pgtable_t
93 pte_alloc_one(struct mm_struct *mm)
98 pte = alloc_pages(PGALLOC_GFP | __GFP_HIGHMEM, 0);
100 pte = alloc_pages(PGALLOC_GFP, 0);
104 if (!PageHighMem(pte))
105 clean_pte_table(page_address(pte));
106 if (!pgtable_page_ctor(pte)) {
114 * Free one PTE table.
116 static inline void pte_free_kernel(struct mm_struct *mm, pte_t *pte)
119 free_page((unsigned long)pte);
122 static inline void pte_free(struct mm_struct *mm, pgtable_t pte)
124 pgtable_page_dtor(pte);
128 static inline void __pmd_populate(pmd_t *pmdp, phys_addr_t pte,
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));
136 flush_pmd_entry(pmdp);
140 * Populate the pmdp entry with a pointer to the pte. This pmd is part
141 * of the mm address space.
143 * Ensure that we always set both PMD entries.
146 pmd_populate_kernel(struct mm_struct *mm, pmd_t *pmdp, pte_t *ptep)
149 * The pmd must be loaded with the physical address of the PTE table
151 __pmd_populate(pmdp, __pa(ptep), _PAGE_KERNEL_TABLE);
155 pmd_populate(struct mm_struct *mm, pmd_t *pmdp, pgtable_t ptep)
157 extern pmdval_t user_pmd_table;
160 if (__LINUX_ARM_ARCH__ >= 6 && !IS_ENABLED(CONFIG_ARM_LPAE))
161 prot = user_pmd_table;
163 prot = _PAGE_USER_TABLE;
165 __pmd_populate(pmdp, page_to_phys(ptep), prot);
167 #define pmd_pgtable(pmd) pmd_page(pmd)
169 #endif /* CONFIG_MMU */