Linux 5.7.6
[linux/fpc-iii.git] / arch / arm / mm / pgd.c
blob478bd2c6aa501a9e78b427506aaa1f8bbaf11c82
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * linux/arch/arm/mm/pgd.c
5 * Copyright (C) 1998-2005 Russell King
6 */
7 #include <linux/mm.h>
8 #include <linux/gfp.h>
9 #include <linux/highmem.h>
10 #include <linux/slab.h>
12 #include <asm/cp15.h>
13 #include <asm/pgalloc.h>
14 #include <asm/page.h>
15 #include <asm/tlbflush.h>
17 #include "mm.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)
22 #else
23 #define __pgd_alloc() (pgd_t *)__get_free_pages(GFP_KERNEL, 2)
24 #define __pgd_free(pgd) free_pages((unsigned long)pgd, 2)
25 #endif
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();
38 if (!new_pgd)
39 goto no_pgd;
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),
57 MODULES_VADDR);
58 if (!new_pud)
59 goto no_pud;
61 new_pmd = pmd_alloc(mm, new_pud, 0);
62 if (!new_pmd)
63 goto no_pmd;
64 #endif
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
70 * with LPAE.
72 new_pud = pud_alloc(mm, new_pgd, 0);
73 if (!new_pud)
74 goto no_pud;
76 new_pmd = pmd_alloc(mm, new_pud, 0);
77 if (!new_pmd)
78 goto no_pmd;
80 new_pte = pte_alloc_map(mm, new_pmd, 0);
81 if (!new_pte)
82 goto no_pte;
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
88 * being unmapped.
90 pmd_val(*new_pmd) &= ~PMD_DOMAIN_MASK;
91 pmd_val(*new_pmd) |= PMD_DOMAIN(DOMAIN_VECTORS);
92 #endif
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);
99 pte_unmap(init_pte);
100 pte_unmap(new_pte);
103 return new_pgd;
105 no_pte:
106 pmd_free(mm, new_pmd);
107 mm_dec_nr_pmds(mm);
108 no_pmd:
109 pud_free(mm, new_pud);
110 no_pud:
111 __pgd_free(new_pgd);
112 no_pgd:
113 return NULL;
116 void pgd_free(struct mm_struct *mm, pgd_t *pgd_base)
118 pgd_t *pgd;
119 pud_t *pud;
120 pmd_t *pmd;
121 pgtable_t pte;
123 if (!pgd_base)
124 return;
126 pgd = pgd_base + pgd_index(0);
127 if (pgd_none_or_clear_bad(pgd))
128 goto no_pgd;
130 pud = pud_offset(pgd, 0);
131 if (pud_none_or_clear_bad(pud))
132 goto no_pud;
134 pmd = pmd_offset(pud, 0);
135 if (pmd_none_or_clear_bad(pmd))
136 goto no_pmd;
138 pte = pmd_pgtable(*pmd);
139 pmd_clear(pmd);
140 pte_free(mm, pte);
141 mm_dec_nr_ptes(mm);
142 no_pmd:
143 pud_clear(pud);
144 pmd_free(mm, pmd);
145 mm_dec_nr_pmds(mm);
146 no_pud:
147 pgd_clear(pgd);
148 pud_free(mm, pud);
149 no_pgd:
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))
156 continue;
157 if (pgd_val(*pgd) & L_PGD_SWAPPER)
158 continue;
159 pud = pud_offset(pgd, 0);
160 if (pud_none_or_clear_bad(pud))
161 continue;
162 pmd = pmd_offset(pud, 0);
163 pud_clear(pud);
164 pmd_free(mm, pmd);
165 mm_dec_nr_pmds(mm);
166 pgd_clear(pgd);
167 pud_free(mm, pud);
169 #endif
170 __pgd_free(pgd_base);