1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/arch/arm/mm/pgd.c
5 * Copyright (C) 1998-2005 Russell King
9 #include <linux/highmem.h>
10 #include <linux/slab.h>
13 #include <asm/pgalloc.h>
15 #include <asm/tlbflush.h>
19 #ifdef CONFIG_ARM_LPAE
20 #define __pgd_alloc() kmalloc_array(PTRS_PER_PGD, sizeof(pgd_t), GFP_KERNEL)
21 #define __pgd_free(pgd) kfree(pgd)
23 #define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
24 #define __pgd_free(pgd) free_pages((unsigned long)pgd, 2)
28 * need to get a 16k page for level 1
30 pgd_t
*pgd_alloc(struct mm_struct
*mm
)
32 pgd_t
*new_pgd
, *init_pgd
;
33 pud_t
*new_pud
, *init_pud
;
34 pmd_t
*new_pmd
, *init_pmd
;
35 pte_t
*new_pte
, *init_pte
;
37 new_pgd
= __pgd_alloc();
41 memset(new_pgd
, 0, USER_PTRS_PER_PGD
* sizeof(pgd_t
));
44 * Copy over the kernel and IO PGD entries
46 init_pgd
= pgd_offset_k(0);
47 memcpy(new_pgd
+ USER_PTRS_PER_PGD
, init_pgd
+ USER_PTRS_PER_PGD
,
48 (PTRS_PER_PGD
- USER_PTRS_PER_PGD
) * sizeof(pgd_t
));
50 clean_dcache_area(new_pgd
, PTRS_PER_PGD
* sizeof(pgd_t
));
52 #ifdef CONFIG_ARM_LPAE
54 * Allocate PMD table for modules and pkmap mappings.
56 new_pud
= pud_alloc(mm
, new_pgd
+ pgd_index(MODULES_VADDR
),
61 new_pmd
= pmd_alloc(mm
, new_pud
, 0);
66 if (!vectors_high()) {
68 * On ARM, first page must always be allocated since it
69 * contains the machine vectors. The vectors are always high
72 new_pud
= pud_alloc(mm
, new_pgd
, 0);
76 new_pmd
= pmd_alloc(mm
, new_pud
, 0);
80 new_pte
= pte_alloc_map(mm
, new_pmd
, 0);
84 #ifndef CONFIG_ARM_LPAE
86 * Modify the PTE pointer to have the correct domain. This
87 * needs to be the vectors domain to avoid the low vectors
90 pmd_val(*new_pmd
) &= ~PMD_DOMAIN_MASK
;
91 pmd_val(*new_pmd
) |= PMD_DOMAIN(DOMAIN_VECTORS
);
94 init_pud
= pud_offset(init_pgd
, 0);
95 init_pmd
= pmd_offset(init_pud
, 0);
96 init_pte
= pte_offset_map(init_pmd
, 0);
97 set_pte_ext(new_pte
+ 0, init_pte
[0], 0);
98 set_pte_ext(new_pte
+ 1, init_pte
[1], 0);
106 pmd_free(mm
, new_pmd
);
109 pud_free(mm
, new_pud
);
116 void pgd_free(struct mm_struct
*mm
, pgd_t
*pgd_base
)
126 pgd
= pgd_base
+ pgd_index(0);
127 if (pgd_none_or_clear_bad(pgd
))
130 pud
= pud_offset(pgd
, 0);
131 if (pud_none_or_clear_bad(pud
))
134 pmd
= pmd_offset(pud
, 0);
135 if (pmd_none_or_clear_bad(pmd
))
138 pte
= pmd_pgtable(*pmd
);
150 #ifdef CONFIG_ARM_LPAE
152 * Free modules/pkmap or identity pmd tables.
154 for (pgd
= pgd_base
; pgd
< pgd_base
+ PTRS_PER_PGD
; pgd
++) {
155 if (pgd_none_or_clear_bad(pgd
))
157 if (pgd_val(*pgd
) & L_PGD_SWAPPER
)
159 pud
= pud_offset(pgd
, 0);
160 if (pud_none_or_clear_bad(pud
))
162 pmd
= pmd_offset(pud
, 0);
170 __pgd_free(pgd_base
);