xref: /openbmc/linux/arch/arm64/mm/mmu.c (revision 67e7fdfcc6824a4f768d76d89377b33baad58fad)
1c1cc1552SCatalin Marinas /*
2c1cc1552SCatalin Marinas  * Based on arch/arm/mm/mmu.c
3c1cc1552SCatalin Marinas  *
4c1cc1552SCatalin Marinas  * Copyright (C) 1995-2005 Russell King
5c1cc1552SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
6c1cc1552SCatalin Marinas  *
7c1cc1552SCatalin Marinas  * This program is free software; you can redistribute it and/or modify
8c1cc1552SCatalin Marinas  * it under the terms of the GNU General Public License version 2 as
9c1cc1552SCatalin Marinas  * published by the Free Software Foundation.
10c1cc1552SCatalin Marinas  *
11c1cc1552SCatalin Marinas  * This program is distributed in the hope that it will be useful,
12c1cc1552SCatalin Marinas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c1cc1552SCatalin Marinas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14c1cc1552SCatalin Marinas  * GNU General Public License for more details.
15c1cc1552SCatalin Marinas  *
16c1cc1552SCatalin Marinas  * You should have received a copy of the GNU General Public License
17c1cc1552SCatalin Marinas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18c1cc1552SCatalin Marinas  */
19c1cc1552SCatalin Marinas 
205a9e3e15SJisheng Zhang #include <linux/cache.h>
21c1cc1552SCatalin Marinas #include <linux/export.h>
22c1cc1552SCatalin Marinas #include <linux/kernel.h>
23c1cc1552SCatalin Marinas #include <linux/errno.h>
24c1cc1552SCatalin Marinas #include <linux/init.h>
2598d2e153STakahiro Akashi #include <linux/ioport.h>
2698d2e153STakahiro Akashi #include <linux/kexec.h>
2761bd93ceSArd Biesheuvel #include <linux/libfdt.h>
28c1cc1552SCatalin Marinas #include <linux/mman.h>
29c1cc1552SCatalin Marinas #include <linux/nodemask.h>
30c1cc1552SCatalin Marinas #include <linux/memblock.h>
31c1cc1552SCatalin Marinas #include <linux/fs.h>
322475ff9dSCatalin Marinas #include <linux/io.h>
332077be67SLaura Abbott #include <linux/mm.h>
346efd8499STobias Klauser #include <linux/vmalloc.h>
35c1cc1552SCatalin Marinas 
3621ab99c2SMark Rutland #include <asm/barrier.h>
37c1cc1552SCatalin Marinas #include <asm/cputype.h>
38af86e597SLaura Abbott #include <asm/fixmap.h>
39068a17a5SMark Rutland #include <asm/kasan.h>
40b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h>
41c1cc1552SCatalin Marinas #include <asm/sections.h>
42c1cc1552SCatalin Marinas #include <asm/setup.h>
43c1cc1552SCatalin Marinas #include <asm/sizes.h>
44c1cc1552SCatalin Marinas #include <asm/tlb.h>
45c79b954bSJungseok Lee #include <asm/memblock.h>
46c1cc1552SCatalin Marinas #include <asm/mmu_context.h>
471404d6f1SLaura Abbott #include <asm/ptdump.h>
48ec28bb9cSChintan Pandya #include <asm/tlbflush.h>
49c1cc1552SCatalin Marinas 
50c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS	BIT(0)
51d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS	BIT(1)
52c0951366SArd Biesheuvel 
53dd006da2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
54fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
55*67e7fdfcSSteve Capper u64 vabits_user __ro_after_init;
56dd006da2SArd Biesheuvel 
575a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init;
58a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset);
59a7f8de16SArd Biesheuvel 
60c1cc1552SCatalin Marinas /*
61c1cc1552SCatalin Marinas  * Empty_zero_page is a special page that is used for zero-initialized data
62c1cc1552SCatalin Marinas  * and COW.
63c1cc1552SCatalin Marinas  */
645227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
65c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page);
66c1cc1552SCatalin Marinas 
67f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
68f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
69f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
70f9040773SArd Biesheuvel 
712330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock);
722330b7caSJun Yao 
732330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
742330b7caSJun Yao {
752330b7caSJun Yao 	pgd_t *fixmap_pgdp;
762330b7caSJun Yao 
772330b7caSJun Yao 	spin_lock(&swapper_pgdir_lock);
7826a6f87eSJames Morse 	fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
792330b7caSJun Yao 	WRITE_ONCE(*fixmap_pgdp, pgd);
802330b7caSJun Yao 	/*
812330b7caSJun Yao 	 * We need dsb(ishst) here to ensure the page-table-walker sees
822330b7caSJun Yao 	 * our new entry before set_p?d() returns. The fixmap's
832330b7caSJun Yao 	 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
842330b7caSJun Yao 	 */
852330b7caSJun Yao 	pgd_clear_fixmap();
862330b7caSJun Yao 	spin_unlock(&swapper_pgdir_lock);
872330b7caSJun Yao }
882330b7caSJun Yao 
89c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
90c1cc1552SCatalin Marinas 			      unsigned long size, pgprot_t vma_prot)
91c1cc1552SCatalin Marinas {
92c1cc1552SCatalin Marinas 	if (!pfn_valid(pfn))
93c1cc1552SCatalin Marinas 		return pgprot_noncached(vma_prot);
94c1cc1552SCatalin Marinas 	else if (file->f_flags & O_SYNC)
95c1cc1552SCatalin Marinas 		return pgprot_writecombine(vma_prot);
96c1cc1552SCatalin Marinas 	return vma_prot;
97c1cc1552SCatalin Marinas }
98c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot);
99c1cc1552SCatalin Marinas 
100f4710445SMark Rutland static phys_addr_t __init early_pgtable_alloc(void)
101c1cc1552SCatalin Marinas {
1027142392dSSuzuki K. Poulose 	phys_addr_t phys;
1037142392dSSuzuki K. Poulose 	void *ptr;
1047142392dSSuzuki K. Poulose 
1059a8dd708SMike Rapoport 	phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
106f4710445SMark Rutland 
107f4710445SMark Rutland 	/*
108f4710445SMark Rutland 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
109f4710445SMark Rutland 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
110f4710445SMark Rutland 	 * any level of table.
111f4710445SMark Rutland 	 */
112f4710445SMark Rutland 	ptr = pte_set_fixmap(phys);
113f4710445SMark Rutland 
11421ab99c2SMark Rutland 	memset(ptr, 0, PAGE_SIZE);
11521ab99c2SMark Rutland 
116f4710445SMark Rutland 	/*
117f4710445SMark Rutland 	 * Implicit barriers also ensure the zeroed page is visible to the page
118f4710445SMark Rutland 	 * table walker
119f4710445SMark Rutland 	 */
120f4710445SMark Rutland 	pte_clear_fixmap();
121f4710445SMark Rutland 
122f4710445SMark Rutland 	return phys;
123c1cc1552SCatalin Marinas }
124c1cc1552SCatalin Marinas 
125e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new)
126e98216b5SArd Biesheuvel {
127e98216b5SArd Biesheuvel 	/*
128e98216b5SArd Biesheuvel 	 * The following mapping attributes may be updated in live
129e98216b5SArd Biesheuvel 	 * kernel mappings without the need for break-before-make.
130e98216b5SArd Biesheuvel 	 */
131753e8abcSArd Biesheuvel 	static const pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
132e98216b5SArd Biesheuvel 
133141d1497SArd Biesheuvel 	/* creating or taking down mappings is always safe */
134141d1497SArd Biesheuvel 	if (old == 0 || new == 0)
135141d1497SArd Biesheuvel 		return true;
136141d1497SArd Biesheuvel 
137141d1497SArd Biesheuvel 	/* live contiguous mappings may not be manipulated at all */
138141d1497SArd Biesheuvel 	if ((old | new) & PTE_CONT)
139141d1497SArd Biesheuvel 		return false;
140141d1497SArd Biesheuvel 
141753e8abcSArd Biesheuvel 	/* Transitioning from Non-Global to Global is unsafe */
142753e8abcSArd Biesheuvel 	if (old & ~new & PTE_NG)
143753e8abcSArd Biesheuvel 		return false;
1444e602056SWill Deacon 
145141d1497SArd Biesheuvel 	return ((old ^ new) & ~mask) == 0;
146e98216b5SArd Biesheuvel }
147e98216b5SArd Biesheuvel 
14820a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
149d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot)
150c1cc1552SCatalin Marinas {
15120a004e7SWill Deacon 	pte_t *ptep;
152c1cc1552SCatalin Marinas 
15320a004e7SWill Deacon 	ptep = pte_set_fixmap_offset(pmdp, addr);
154c1cc1552SCatalin Marinas 	do {
15520a004e7SWill Deacon 		pte_t old_pte = READ_ONCE(*ptep);
156e98216b5SArd Biesheuvel 
15720a004e7SWill Deacon 		set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
158e98216b5SArd Biesheuvel 
159e98216b5SArd Biesheuvel 		/*
160e98216b5SArd Biesheuvel 		 * After the PTE entry has been populated once, we
161e98216b5SArd Biesheuvel 		 * only allow updates to the permission attributes.
162e98216b5SArd Biesheuvel 		 */
16320a004e7SWill Deacon 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
16420a004e7SWill Deacon 					      READ_ONCE(pte_val(*ptep))));
165e98216b5SArd Biesheuvel 
166e393cf40SArd Biesheuvel 		phys += PAGE_SIZE;
16720a004e7SWill Deacon 	} while (ptep++, addr += PAGE_SIZE, addr != end);
168f4710445SMark Rutland 
169f4710445SMark Rutland 	pte_clear_fixmap();
170c1cc1552SCatalin Marinas }
171c1cc1552SCatalin Marinas 
17220a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
173d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
174d27cfa1fSArd Biesheuvel 				pgprot_t prot,
17553e1b329SArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void),
176c0951366SArd Biesheuvel 				int flags)
177c1cc1552SCatalin Marinas {
178c1cc1552SCatalin Marinas 	unsigned long next;
17920a004e7SWill Deacon 	pmd_t pmd = READ_ONCE(*pmdp);
180c1cc1552SCatalin Marinas 
18120a004e7SWill Deacon 	BUG_ON(pmd_sect(pmd));
18220a004e7SWill Deacon 	if (pmd_none(pmd)) {
183d27cfa1fSArd Biesheuvel 		phys_addr_t pte_phys;
184132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
185d27cfa1fSArd Biesheuvel 		pte_phys = pgtable_alloc();
18620a004e7SWill Deacon 		__pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
18720a004e7SWill Deacon 		pmd = READ_ONCE(*pmdp);
188c1cc1552SCatalin Marinas 	}
18920a004e7SWill Deacon 	BUG_ON(pmd_bad(pmd));
190d27cfa1fSArd Biesheuvel 
191d27cfa1fSArd Biesheuvel 	do {
192d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
193d27cfa1fSArd Biesheuvel 
194d27cfa1fSArd Biesheuvel 		next = pte_cont_addr_end(addr, end);
195d27cfa1fSArd Biesheuvel 
196d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
197d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
198d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
199d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
200d27cfa1fSArd Biesheuvel 
20120a004e7SWill Deacon 		init_pte(pmdp, addr, next, phys, __prot);
202d27cfa1fSArd Biesheuvel 
203d27cfa1fSArd Biesheuvel 		phys += next - addr;
204d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
205d27cfa1fSArd Biesheuvel }
206d27cfa1fSArd Biesheuvel 
20720a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
208d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot,
209d27cfa1fSArd Biesheuvel 		     phys_addr_t (*pgtable_alloc)(void), int flags)
210d27cfa1fSArd Biesheuvel {
211d27cfa1fSArd Biesheuvel 	unsigned long next;
21220a004e7SWill Deacon 	pmd_t *pmdp;
213c1cc1552SCatalin Marinas 
21420a004e7SWill Deacon 	pmdp = pmd_set_fixmap_offset(pudp, addr);
215c1cc1552SCatalin Marinas 	do {
21620a004e7SWill Deacon 		pmd_t old_pmd = READ_ONCE(*pmdp);
217e98216b5SArd Biesheuvel 
218c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
219e98216b5SArd Biesheuvel 
220c1cc1552SCatalin Marinas 		/* try section mapping first */
22183863f25SLaura Abbott 		if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
222c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
22320a004e7SWill Deacon 			pmd_set_huge(pmdp, phys, prot);
224e98216b5SArd Biesheuvel 
225a55f9929SCatalin Marinas 			/*
226e98216b5SArd Biesheuvel 			 * After the PMD entry has been populated once, we
227e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
228a55f9929SCatalin Marinas 			 */
229e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
23020a004e7SWill Deacon 						      READ_ONCE(pmd_val(*pmdp))));
231a55f9929SCatalin Marinas 		} else {
23220a004e7SWill Deacon 			alloc_init_cont_pte(pmdp, addr, next, phys, prot,
233d27cfa1fSArd Biesheuvel 					    pgtable_alloc, flags);
234e98216b5SArd Biesheuvel 
235e98216b5SArd Biesheuvel 			BUG_ON(pmd_val(old_pmd) != 0 &&
23620a004e7SWill Deacon 			       pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
237a55f9929SCatalin Marinas 		}
238c1cc1552SCatalin Marinas 		phys += next - addr;
23920a004e7SWill Deacon 	} while (pmdp++, addr = next, addr != end);
240f4710445SMark Rutland 
241f4710445SMark Rutland 	pmd_clear_fixmap();
242c1cc1552SCatalin Marinas }
243c1cc1552SCatalin Marinas 
24420a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
245d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
246d27cfa1fSArd Biesheuvel 				pgprot_t prot,
247d27cfa1fSArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void), int flags)
248d27cfa1fSArd Biesheuvel {
249d27cfa1fSArd Biesheuvel 	unsigned long next;
25020a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
251d27cfa1fSArd Biesheuvel 
252d27cfa1fSArd Biesheuvel 	/*
253d27cfa1fSArd Biesheuvel 	 * Check for initial section mappings in the pgd/pud.
254d27cfa1fSArd Biesheuvel 	 */
25520a004e7SWill Deacon 	BUG_ON(pud_sect(pud));
25620a004e7SWill Deacon 	if (pud_none(pud)) {
257d27cfa1fSArd Biesheuvel 		phys_addr_t pmd_phys;
258d27cfa1fSArd Biesheuvel 		BUG_ON(!pgtable_alloc);
259d27cfa1fSArd Biesheuvel 		pmd_phys = pgtable_alloc();
26020a004e7SWill Deacon 		__pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
26120a004e7SWill Deacon 		pud = READ_ONCE(*pudp);
262d27cfa1fSArd Biesheuvel 	}
26320a004e7SWill Deacon 	BUG_ON(pud_bad(pud));
264d27cfa1fSArd Biesheuvel 
265d27cfa1fSArd Biesheuvel 	do {
266d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
267d27cfa1fSArd Biesheuvel 
268d27cfa1fSArd Biesheuvel 		next = pmd_cont_addr_end(addr, end);
269d27cfa1fSArd Biesheuvel 
270d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
271d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
272d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
273d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
274d27cfa1fSArd Biesheuvel 
27520a004e7SWill Deacon 		init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
276d27cfa1fSArd Biesheuvel 
277d27cfa1fSArd Biesheuvel 		phys += next - addr;
278d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
279d27cfa1fSArd Biesheuvel }
280d27cfa1fSArd Biesheuvel 
281da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next,
282da141706SLaura Abbott 			unsigned long phys)
283da141706SLaura Abbott {
284da141706SLaura Abbott 	if (PAGE_SHIFT != 12)
285da141706SLaura Abbott 		return false;
286da141706SLaura Abbott 
287da141706SLaura Abbott 	if (((addr | next | phys) & ~PUD_MASK) != 0)
288da141706SLaura Abbott 		return false;
289da141706SLaura Abbott 
290da141706SLaura Abbott 	return true;
291da141706SLaura Abbott }
292da141706SLaura Abbott 
29320a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
294da141706SLaura Abbott 			   phys_addr_t phys, pgprot_t prot,
29553e1b329SArd Biesheuvel 			   phys_addr_t (*pgtable_alloc)(void),
296c0951366SArd Biesheuvel 			   int flags)
297c1cc1552SCatalin Marinas {
298c1cc1552SCatalin Marinas 	unsigned long next;
29920a004e7SWill Deacon 	pud_t *pudp;
30020a004e7SWill Deacon 	pgd_t pgd = READ_ONCE(*pgdp);
301c1cc1552SCatalin Marinas 
30220a004e7SWill Deacon 	if (pgd_none(pgd)) {
303132233a7SLaura Abbott 		phys_addr_t pud_phys;
304132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
305132233a7SLaura Abbott 		pud_phys = pgtable_alloc();
30620a004e7SWill Deacon 		__pgd_populate(pgdp, pud_phys, PUD_TYPE_TABLE);
30720a004e7SWill Deacon 		pgd = READ_ONCE(*pgdp);
308c79b954bSJungseok Lee 	}
30920a004e7SWill Deacon 	BUG_ON(pgd_bad(pgd));
310c79b954bSJungseok Lee 
31120a004e7SWill Deacon 	pudp = pud_set_fixmap_offset(pgdp, addr);
312c1cc1552SCatalin Marinas 	do {
31320a004e7SWill Deacon 		pud_t old_pud = READ_ONCE(*pudp);
314e98216b5SArd Biesheuvel 
315c1cc1552SCatalin Marinas 		next = pud_addr_end(addr, end);
316206a2a73SSteve Capper 
317206a2a73SSteve Capper 		/*
318206a2a73SSteve Capper 		 * For 4K granule only, attempt to put down a 1GB block
319206a2a73SSteve Capper 		 */
320c0951366SArd Biesheuvel 		if (use_1G_block(addr, next, phys) &&
321c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
32220a004e7SWill Deacon 			pud_set_huge(pudp, phys, prot);
323206a2a73SSteve Capper 
324206a2a73SSteve Capper 			/*
325e98216b5SArd Biesheuvel 			 * After the PUD entry has been populated once, we
326e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
327206a2a73SSteve Capper 			 */
328e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
32920a004e7SWill Deacon 						      READ_ONCE(pud_val(*pudp))));
330206a2a73SSteve Capper 		} else {
33120a004e7SWill Deacon 			alloc_init_cont_pmd(pudp, addr, next, phys, prot,
332c0951366SArd Biesheuvel 					    pgtable_alloc, flags);
333e98216b5SArd Biesheuvel 
334e98216b5SArd Biesheuvel 			BUG_ON(pud_val(old_pud) != 0 &&
33520a004e7SWill Deacon 			       pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
336206a2a73SSteve Capper 		}
337c1cc1552SCatalin Marinas 		phys += next - addr;
33820a004e7SWill Deacon 	} while (pudp++, addr = next, addr != end);
339f4710445SMark Rutland 
340f4710445SMark Rutland 	pud_clear_fixmap();
341c1cc1552SCatalin Marinas }
342c1cc1552SCatalin Marinas 
34340f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
34440f87d31SArd Biesheuvel 				 unsigned long virt, phys_addr_t size,
34540f87d31SArd Biesheuvel 				 pgprot_t prot,
34653e1b329SArd Biesheuvel 				 phys_addr_t (*pgtable_alloc)(void),
347c0951366SArd Biesheuvel 				 int flags)
348c1cc1552SCatalin Marinas {
349c1cc1552SCatalin Marinas 	unsigned long addr, length, end, next;
35020a004e7SWill Deacon 	pgd_t *pgdp = pgd_offset_raw(pgdir, virt);
351c1cc1552SCatalin Marinas 
352cc5d2b3bSMark Rutland 	/*
353cc5d2b3bSMark Rutland 	 * If the virtual and physical address don't have the same offset
354cc5d2b3bSMark Rutland 	 * within a page, we cannot map the region as the caller expects.
355cc5d2b3bSMark Rutland 	 */
356cc5d2b3bSMark Rutland 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
357cc5d2b3bSMark Rutland 		return;
358cc5d2b3bSMark Rutland 
3599c4e08a3SMark Rutland 	phys &= PAGE_MASK;
360c1cc1552SCatalin Marinas 	addr = virt & PAGE_MASK;
361c1cc1552SCatalin Marinas 	length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
362c1cc1552SCatalin Marinas 
363c1cc1552SCatalin Marinas 	end = addr + length;
364c1cc1552SCatalin Marinas 	do {
365c1cc1552SCatalin Marinas 		next = pgd_addr_end(addr, end);
36620a004e7SWill Deacon 		alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
367c0951366SArd Biesheuvel 			       flags);
368c1cc1552SCatalin Marinas 		phys += next - addr;
36920a004e7SWill Deacon 	} while (pgdp++, addr = next, addr != end);
370c1cc1552SCatalin Marinas }
371c1cc1552SCatalin Marinas 
3721378dc3dSArd Biesheuvel static phys_addr_t pgd_pgtable_alloc(void)
373da141706SLaura Abbott {
37421ab99c2SMark Rutland 	void *ptr = (void *)__get_free_page(PGALLOC_GFP);
3751378dc3dSArd Biesheuvel 	if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
3761378dc3dSArd Biesheuvel 		BUG();
37721ab99c2SMark Rutland 
37821ab99c2SMark Rutland 	/* Ensure the zeroed page is visible to the page table walker */
37921ab99c2SMark Rutland 	dsb(ishst);
380f4710445SMark Rutland 	return __pa(ptr);
381da141706SLaura Abbott }
382da141706SLaura Abbott 
383132233a7SLaura Abbott /*
384132233a7SLaura Abbott  * This function can only be used to modify existing table entries,
385132233a7SLaura Abbott  * without allocating new levels of table. Note that this permits the
386132233a7SLaura Abbott  * creation of new section or page entries.
387132233a7SLaura Abbott  */
388132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
389da141706SLaura Abbott 				  phys_addr_t size, pgprot_t prot)
390d7ecbddfSMark Salter {
391d7ecbddfSMark Salter 	if (virt < VMALLOC_START) {
392d7ecbddfSMark Salter 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
393d7ecbddfSMark Salter 			&phys, virt);
394d7ecbddfSMark Salter 		return;
395d7ecbddfSMark Salter 	}
396d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
397d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
398d7ecbddfSMark Salter }
399d7ecbddfSMark Salter 
4008ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
4018ce837ceSArd Biesheuvel 			       unsigned long virt, phys_addr_t size,
402f14c66ceSArd Biesheuvel 			       pgprot_t prot, bool page_mappings_only)
4038ce837ceSArd Biesheuvel {
404c0951366SArd Biesheuvel 	int flags = 0;
405c0951366SArd Biesheuvel 
4061378dc3dSArd Biesheuvel 	BUG_ON(mm == &init_mm);
4071378dc3dSArd Biesheuvel 
408c0951366SArd Biesheuvel 	if (page_mappings_only)
409d27cfa1fSArd Biesheuvel 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
410c0951366SArd Biesheuvel 
41111509a30SMark Rutland 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
412c0951366SArd Biesheuvel 			     pgd_pgtable_alloc, flags);
413d7ecbddfSMark Salter }
414d7ecbddfSMark Salter 
415aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
416da141706SLaura Abbott 				phys_addr_t size, pgprot_t prot)
417da141706SLaura Abbott {
418da141706SLaura Abbott 	if (virt < VMALLOC_START) {
419aa8c09beSArd Biesheuvel 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
420da141706SLaura Abbott 			&phys, virt);
421da141706SLaura Abbott 		return;
422da141706SLaura Abbott 	}
423da141706SLaura Abbott 
424d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
425d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
426aa8c09beSArd Biesheuvel 
427aa8c09beSArd Biesheuvel 	/* flush the TLBs after updating live kernel mappings */
428aa8c09beSArd Biesheuvel 	flush_tlb_kernel_range(virt, virt + size);
429da141706SLaura Abbott }
430da141706SLaura Abbott 
43120a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
43298d2e153STakahiro Akashi 				  phys_addr_t end, pgprot_t prot, int flags)
433da141706SLaura Abbott {
43420a004e7SWill Deacon 	__create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
43598d2e153STakahiro Akashi 			     prot, early_pgtable_alloc, flags);
436da141706SLaura Abbott }
437da141706SLaura Abbott 
4385ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void)
4395ea5306cSArd Biesheuvel {
4405ea5306cSArd Biesheuvel 	/*
4415ea5306cSArd Biesheuvel 	 * Remove the write permissions from the linear alias of .text/.rodata
4425ea5306cSArd Biesheuvel 	 */
4435ea5306cSArd Biesheuvel 	update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
4445ea5306cSArd Biesheuvel 			    (unsigned long)__init_begin - (unsigned long)_text,
4455ea5306cSArd Biesheuvel 			    PAGE_KERNEL_RO);
4465ea5306cSArd Biesheuvel }
4475ea5306cSArd Biesheuvel 
44820a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp)
449c1cc1552SCatalin Marinas {
45098d2e153STakahiro Akashi 	phys_addr_t kernel_start = __pa_symbol(_text);
45198d2e153STakahiro Akashi 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
452c1cc1552SCatalin Marinas 	struct memblock_region *reg;
45398d2e153STakahiro Akashi 	int flags = 0;
45498d2e153STakahiro Akashi 
455c55191e9SArd Biesheuvel 	if (rodata_full || debug_pagealloc_enabled())
45698d2e153STakahiro Akashi 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
45798d2e153STakahiro Akashi 
45898d2e153STakahiro Akashi 	/*
45998d2e153STakahiro Akashi 	 * Take care not to create a writable alias for the
46098d2e153STakahiro Akashi 	 * read-only text and rodata sections of the kernel image.
46198d2e153STakahiro Akashi 	 * So temporarily mark them as NOMAP to skip mappings in
46298d2e153STakahiro Akashi 	 * the following for-loop
46398d2e153STakahiro Akashi 	 */
46498d2e153STakahiro Akashi 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
46598d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
46698d2e153STakahiro Akashi 	if (crashk_res.end)
46798d2e153STakahiro Akashi 		memblock_mark_nomap(crashk_res.start,
46898d2e153STakahiro Akashi 				    resource_size(&crashk_res));
46998d2e153STakahiro Akashi #endif
470f6bc87c3SSteve Capper 
471c1cc1552SCatalin Marinas 	/* map all the memory banks */
472c1cc1552SCatalin Marinas 	for_each_memblock(memory, reg) {
473c1cc1552SCatalin Marinas 		phys_addr_t start = reg->base;
474c1cc1552SCatalin Marinas 		phys_addr_t end = start + reg->size;
475c1cc1552SCatalin Marinas 
476c1cc1552SCatalin Marinas 		if (start >= end)
477c1cc1552SCatalin Marinas 			break;
47868709f45SArd Biesheuvel 		if (memblock_is_nomap(reg))
47968709f45SArd Biesheuvel 			continue;
480c1cc1552SCatalin Marinas 
48120a004e7SWill Deacon 		__map_memblock(pgdp, start, end, PAGE_KERNEL, flags);
482c1cc1552SCatalin Marinas 	}
48398d2e153STakahiro Akashi 
48498d2e153STakahiro Akashi 	/*
48598d2e153STakahiro Akashi 	 * Map the linear alias of the [_text, __init_begin) interval
48698d2e153STakahiro Akashi 	 * as non-executable now, and remove the write permission in
48798d2e153STakahiro Akashi 	 * mark_linear_text_alias_ro() below (which will be called after
48898d2e153STakahiro Akashi 	 * alternative patching has completed). This makes the contents
48998d2e153STakahiro Akashi 	 * of the region accessible to subsystems such as hibernate,
49098d2e153STakahiro Akashi 	 * but protects it from inadvertent modification or execution.
49198d2e153STakahiro Akashi 	 * Note that contiguous mappings cannot be remapped in this way,
49298d2e153STakahiro Akashi 	 * so we should avoid them here.
49398d2e153STakahiro Akashi 	 */
49420a004e7SWill Deacon 	__map_memblock(pgdp, kernel_start, kernel_end,
49598d2e153STakahiro Akashi 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
49698d2e153STakahiro Akashi 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
49798d2e153STakahiro Akashi 
49898d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
49998d2e153STakahiro Akashi 	/*
50098d2e153STakahiro Akashi 	 * Use page-level mappings here so that we can shrink the region
50198d2e153STakahiro Akashi 	 * in page granularity and put back unused memory to buddy system
50298d2e153STakahiro Akashi 	 * through /sys/kernel/kexec_crash_size interface.
50398d2e153STakahiro Akashi 	 */
50498d2e153STakahiro Akashi 	if (crashk_res.end) {
50520a004e7SWill Deacon 		__map_memblock(pgdp, crashk_res.start, crashk_res.end + 1,
50698d2e153STakahiro Akashi 			       PAGE_KERNEL,
50798d2e153STakahiro Akashi 			       NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
50898d2e153STakahiro Akashi 		memblock_clear_nomap(crashk_res.start,
50998d2e153STakahiro Akashi 				     resource_size(&crashk_res));
51098d2e153STakahiro Akashi 	}
51198d2e153STakahiro Akashi #endif
512c1cc1552SCatalin Marinas }
513c1cc1552SCatalin Marinas 
514da141706SLaura Abbott void mark_rodata_ro(void)
515da141706SLaura Abbott {
5162f39b5f9SJeremy Linton 	unsigned long section_size;
517f9040773SArd Biesheuvel 
5182f39b5f9SJeremy Linton 	/*
5199fdc14c5SArd Biesheuvel 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
5209fdc14c5SArd Biesheuvel 	 * to cover NOTES and EXCEPTION_TABLE.
5212f39b5f9SJeremy Linton 	 */
5229fdc14c5SArd Biesheuvel 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
523aa8c09beSArd Biesheuvel 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
5242f39b5f9SJeremy Linton 			    section_size, PAGE_KERNEL_RO);
525e98216b5SArd Biesheuvel 
5261404d6f1SLaura Abbott 	debug_checkwx();
527da141706SLaura Abbott }
528da141706SLaura Abbott 
52920a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
530d27cfa1fSArd Biesheuvel 				      pgprot_t prot, struct vm_struct *vma,
53192bbd16eSWill Deacon 				      int flags, unsigned long vm_flags)
532068a17a5SMark Rutland {
5332077be67SLaura Abbott 	phys_addr_t pa_start = __pa_symbol(va_start);
534068a17a5SMark Rutland 	unsigned long size = va_end - va_start;
535068a17a5SMark Rutland 
536068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(pa_start));
537068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(size));
538068a17a5SMark Rutland 
53920a004e7SWill Deacon 	__create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
540d27cfa1fSArd Biesheuvel 			     early_pgtable_alloc, flags);
541f9040773SArd Biesheuvel 
54292bbd16eSWill Deacon 	if (!(vm_flags & VM_NO_GUARD))
54392bbd16eSWill Deacon 		size += PAGE_SIZE;
54492bbd16eSWill Deacon 
545f9040773SArd Biesheuvel 	vma->addr	= va_start;
546f9040773SArd Biesheuvel 	vma->phys_addr	= pa_start;
547f9040773SArd Biesheuvel 	vma->size	= size;
54892bbd16eSWill Deacon 	vma->flags	= VM_MAP | vm_flags;
549f9040773SArd Biesheuvel 	vma->caller	= __builtin_return_address(0);
550f9040773SArd Biesheuvel 
551f9040773SArd Biesheuvel 	vm_area_add_early(vma);
552068a17a5SMark Rutland }
553068a17a5SMark Rutland 
55428b066daSArd Biesheuvel static int __init parse_rodata(char *arg)
55528b066daSArd Biesheuvel {
556c55191e9SArd Biesheuvel 	int ret = strtobool(arg, &rodata_enabled);
557c55191e9SArd Biesheuvel 	if (!ret) {
558c55191e9SArd Biesheuvel 		rodata_full = false;
559c55191e9SArd Biesheuvel 		return 0;
560c55191e9SArd Biesheuvel 	}
561c55191e9SArd Biesheuvel 
562c55191e9SArd Biesheuvel 	/* permit 'full' in addition to boolean options */
563c55191e9SArd Biesheuvel 	if (strcmp(arg, "full"))
564c55191e9SArd Biesheuvel 		return -EINVAL;
565c55191e9SArd Biesheuvel 
566c55191e9SArd Biesheuvel 	rodata_enabled = true;
567c55191e9SArd Biesheuvel 	rodata_full = true;
568c55191e9SArd Biesheuvel 	return 0;
56928b066daSArd Biesheuvel }
57028b066daSArd Biesheuvel early_param("rodata", parse_rodata);
57128b066daSArd Biesheuvel 
57251a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
57351a0048bSWill Deacon static int __init map_entry_trampoline(void)
57451a0048bSWill Deacon {
57551a0048bSWill Deacon 	pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
57651a0048bSWill Deacon 	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
57751a0048bSWill Deacon 
57851a0048bSWill Deacon 	/* The trampoline is always mapped and can therefore be global */
57951a0048bSWill Deacon 	pgprot_val(prot) &= ~PTE_NG;
58051a0048bSWill Deacon 
58151a0048bSWill Deacon 	/* Map only the text into the trampoline page table */
58251a0048bSWill Deacon 	memset(tramp_pg_dir, 0, PGD_SIZE);
58351a0048bSWill Deacon 	__create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE,
58451a0048bSWill Deacon 			     prot, pgd_pgtable_alloc, 0);
58551a0048bSWill Deacon 
5866c27c408SWill Deacon 	/* Map both the text and data into the kernel page table */
58751a0048bSWill Deacon 	__set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot);
5886c27c408SWill Deacon 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
5896c27c408SWill Deacon 		extern char __entry_tramp_data_start[];
5906c27c408SWill Deacon 
5916c27c408SWill Deacon 		__set_fixmap(FIX_ENTRY_TRAMP_DATA,
5926c27c408SWill Deacon 			     __pa_symbol(__entry_tramp_data_start),
5936c27c408SWill Deacon 			     PAGE_KERNEL_RO);
5946c27c408SWill Deacon 	}
5956c27c408SWill Deacon 
59651a0048bSWill Deacon 	return 0;
59751a0048bSWill Deacon }
59851a0048bSWill Deacon core_initcall(map_entry_trampoline);
59951a0048bSWill Deacon #endif
60051a0048bSWill Deacon 
601068a17a5SMark Rutland /*
602068a17a5SMark Rutland  * Create fine-grained mappings for the kernel.
603068a17a5SMark Rutland  */
60420a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp)
605068a17a5SMark Rutland {
6062ebe088bSArd Biesheuvel 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
6072ebe088bSArd Biesheuvel 				vmlinux_initdata, vmlinux_data;
608068a17a5SMark Rutland 
60928b066daSArd Biesheuvel 	/*
61028b066daSArd Biesheuvel 	 * External debuggers may need to write directly to the text
61128b066daSArd Biesheuvel 	 * mapping to install SW breakpoints. Allow this (only) when
61228b066daSArd Biesheuvel 	 * explicitly requested with rodata=off.
61328b066daSArd Biesheuvel 	 */
61428b066daSArd Biesheuvel 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
61528b066daSArd Biesheuvel 
616d27cfa1fSArd Biesheuvel 	/*
617d27cfa1fSArd Biesheuvel 	 * Only rodata will be remapped with different permissions later on,
618d27cfa1fSArd Biesheuvel 	 * all other segments are allowed to use contiguous mappings.
619d27cfa1fSArd Biesheuvel 	 */
62020a004e7SWill Deacon 	map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0,
62192bbd16eSWill Deacon 			   VM_NO_GUARD);
62220a004e7SWill Deacon 	map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
62392bbd16eSWill Deacon 			   &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
62420a004e7SWill Deacon 	map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
62592bbd16eSWill Deacon 			   &vmlinux_inittext, 0, VM_NO_GUARD);
62620a004e7SWill Deacon 	map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
62792bbd16eSWill Deacon 			   &vmlinux_initdata, 0, VM_NO_GUARD);
62820a004e7SWill Deacon 	map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
629068a17a5SMark Rutland 
63020a004e7SWill Deacon 	if (!READ_ONCE(pgd_val(*pgd_offset_raw(pgdp, FIXADDR_START)))) {
631068a17a5SMark Rutland 		/*
632f9040773SArd Biesheuvel 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
633f9040773SArd Biesheuvel 		 * live in the carveout for the swapper_pg_dir. We can simply
634f9040773SArd Biesheuvel 		 * re-use the existing dir for the fixmap.
635068a17a5SMark Rutland 		 */
63620a004e7SWill Deacon 		set_pgd(pgd_offset_raw(pgdp, FIXADDR_START),
63720a004e7SWill Deacon 			READ_ONCE(*pgd_offset_k(FIXADDR_START)));
638f9040773SArd Biesheuvel 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
639f9040773SArd Biesheuvel 		/*
640f9040773SArd Biesheuvel 		 * The fixmap shares its top level pgd entry with the kernel
641f9040773SArd Biesheuvel 		 * mapping. This can really only occur when we are running
642f9040773SArd Biesheuvel 		 * with 16k/4 levels, so we can simply reuse the pud level
643f9040773SArd Biesheuvel 		 * entry instead.
644f9040773SArd Biesheuvel 		 */
645f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
64620a004e7SWill Deacon 		pud_populate(&init_mm,
64720a004e7SWill Deacon 			     pud_set_fixmap_offset(pgdp, FIXADDR_START),
64819338304SKristina Martsenko 			     lm_alias(bm_pmd));
649f9040773SArd Biesheuvel 		pud_clear_fixmap();
650f9040773SArd Biesheuvel 	} else {
651f9040773SArd Biesheuvel 		BUG();
652f9040773SArd Biesheuvel 	}
653068a17a5SMark Rutland 
65420a004e7SWill Deacon 	kasan_copy_shadow(pgdp);
655068a17a5SMark Rutland }
656068a17a5SMark Rutland 
657c1cc1552SCatalin Marinas /*
658c1cc1552SCatalin Marinas  * paging_init() sets up the page tables, initialises the zone memory
659c1cc1552SCatalin Marinas  * maps and sets up the zero page.
660c1cc1552SCatalin Marinas  */
661c1cc1552SCatalin Marinas void __init paging_init(void)
662c1cc1552SCatalin Marinas {
6632330b7caSJun Yao 	pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
664068a17a5SMark Rutland 
66520a004e7SWill Deacon 	map_kernel(pgdp);
66620a004e7SWill Deacon 	map_mem(pgdp);
667068a17a5SMark Rutland 
668068a17a5SMark Rutland 	pgd_clear_fixmap();
669068a17a5SMark Rutland 
670068a17a5SMark Rutland 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
6712b5548b6SJun Yao 	init_mm.pgd = swapper_pg_dir;
672068a17a5SMark Rutland 
6732b5548b6SJun Yao 	memblock_free(__pa_symbol(init_pg_dir),
6742b5548b6SJun Yao 		      __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
67524cc61d8SArd Biesheuvel 
67624cc61d8SArd Biesheuvel 	memblock_allow_resize();
677c1cc1552SCatalin Marinas }
678c1cc1552SCatalin Marinas 
679c1cc1552SCatalin Marinas /*
680c1cc1552SCatalin Marinas  * Check whether a kernel address is valid (derived from arch/x86/).
681c1cc1552SCatalin Marinas  */
682c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr)
683c1cc1552SCatalin Marinas {
68420a004e7SWill Deacon 	pgd_t *pgdp;
68520a004e7SWill Deacon 	pud_t *pudp, pud;
68620a004e7SWill Deacon 	pmd_t *pmdp, pmd;
68720a004e7SWill Deacon 	pte_t *ptep, pte;
688c1cc1552SCatalin Marinas 
689c1cc1552SCatalin Marinas 	if ((((long)addr) >> VA_BITS) != -1UL)
690c1cc1552SCatalin Marinas 		return 0;
691c1cc1552SCatalin Marinas 
69220a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
69320a004e7SWill Deacon 	if (pgd_none(READ_ONCE(*pgdp)))
694c1cc1552SCatalin Marinas 		return 0;
695c1cc1552SCatalin Marinas 
69620a004e7SWill Deacon 	pudp = pud_offset(pgdp, addr);
69720a004e7SWill Deacon 	pud = READ_ONCE(*pudp);
69820a004e7SWill Deacon 	if (pud_none(pud))
699c1cc1552SCatalin Marinas 		return 0;
700c1cc1552SCatalin Marinas 
70120a004e7SWill Deacon 	if (pud_sect(pud))
70220a004e7SWill Deacon 		return pfn_valid(pud_pfn(pud));
703206a2a73SSteve Capper 
70420a004e7SWill Deacon 	pmdp = pmd_offset(pudp, addr);
70520a004e7SWill Deacon 	pmd = READ_ONCE(*pmdp);
70620a004e7SWill Deacon 	if (pmd_none(pmd))
707c1cc1552SCatalin Marinas 		return 0;
708c1cc1552SCatalin Marinas 
70920a004e7SWill Deacon 	if (pmd_sect(pmd))
71020a004e7SWill Deacon 		return pfn_valid(pmd_pfn(pmd));
711da6e4cb6SDave Anderson 
71220a004e7SWill Deacon 	ptep = pte_offset_kernel(pmdp, addr);
71320a004e7SWill Deacon 	pte = READ_ONCE(*ptep);
71420a004e7SWill Deacon 	if (pte_none(pte))
715c1cc1552SCatalin Marinas 		return 0;
716c1cc1552SCatalin Marinas 
71720a004e7SWill Deacon 	return pfn_valid(pte_pfn(pte));
718c1cc1552SCatalin Marinas }
719c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP
720b433dce0SSuzuki K. Poulose #if !ARM64_SWAPPER_USES_SECTION_MAPS
7217b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
7227b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
723c1cc1552SCatalin Marinas {
7240aad818bSJohannes Weiner 	return vmemmap_populate_basepages(start, end, node);
725c1cc1552SCatalin Marinas }
726b433dce0SSuzuki K. Poulose #else	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
7277b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
7287b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
729c1cc1552SCatalin Marinas {
7300aad818bSJohannes Weiner 	unsigned long addr = start;
731c1cc1552SCatalin Marinas 	unsigned long next;
73220a004e7SWill Deacon 	pgd_t *pgdp;
73320a004e7SWill Deacon 	pud_t *pudp;
73420a004e7SWill Deacon 	pmd_t *pmdp;
735c1cc1552SCatalin Marinas 
736c1cc1552SCatalin Marinas 	do {
737c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
738c1cc1552SCatalin Marinas 
73920a004e7SWill Deacon 		pgdp = vmemmap_pgd_populate(addr, node);
74020a004e7SWill Deacon 		if (!pgdp)
741c1cc1552SCatalin Marinas 			return -ENOMEM;
742c1cc1552SCatalin Marinas 
74320a004e7SWill Deacon 		pudp = vmemmap_pud_populate(pgdp, addr, node);
74420a004e7SWill Deacon 		if (!pudp)
745c1cc1552SCatalin Marinas 			return -ENOMEM;
746c1cc1552SCatalin Marinas 
74720a004e7SWill Deacon 		pmdp = pmd_offset(pudp, addr);
74820a004e7SWill Deacon 		if (pmd_none(READ_ONCE(*pmdp))) {
749c1cc1552SCatalin Marinas 			void *p = NULL;
750c1cc1552SCatalin Marinas 
751c1cc1552SCatalin Marinas 			p = vmemmap_alloc_block_buf(PMD_SIZE, node);
752c1cc1552SCatalin Marinas 			if (!p)
753c1cc1552SCatalin Marinas 				return -ENOMEM;
754c1cc1552SCatalin Marinas 
75520a004e7SWill Deacon 			pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
756c1cc1552SCatalin Marinas 		} else
75720a004e7SWill Deacon 			vmemmap_verify((pte_t *)pmdp, node, addr, next);
758c1cc1552SCatalin Marinas 	} while (addr = next, addr != end);
759c1cc1552SCatalin Marinas 
760c1cc1552SCatalin Marinas 	return 0;
761c1cc1552SCatalin Marinas }
762c1cc1552SCatalin Marinas #endif	/* CONFIG_ARM64_64K_PAGES */
76324b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end,
76424b6d416SChristoph Hellwig 		struct vmem_altmap *altmap)
7650197518cSTang Chen {
7660197518cSTang Chen }
767c1cc1552SCatalin Marinas #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
768af86e597SLaura Abbott 
769af86e597SLaura Abbott static inline pud_t * fixmap_pud(unsigned long addr)
770af86e597SLaura Abbott {
77120a004e7SWill Deacon 	pgd_t *pgdp = pgd_offset_k(addr);
77220a004e7SWill Deacon 	pgd_t pgd = READ_ONCE(*pgdp);
773af86e597SLaura Abbott 
77420a004e7SWill Deacon 	BUG_ON(pgd_none(pgd) || pgd_bad(pgd));
775af86e597SLaura Abbott 
77620a004e7SWill Deacon 	return pud_offset_kimg(pgdp, addr);
777af86e597SLaura Abbott }
778af86e597SLaura Abbott 
779af86e597SLaura Abbott static inline pmd_t * fixmap_pmd(unsigned long addr)
780af86e597SLaura Abbott {
78120a004e7SWill Deacon 	pud_t *pudp = fixmap_pud(addr);
78220a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
783af86e597SLaura Abbott 
78420a004e7SWill Deacon 	BUG_ON(pud_none(pud) || pud_bad(pud));
785af86e597SLaura Abbott 
78620a004e7SWill Deacon 	return pmd_offset_kimg(pudp, addr);
787af86e597SLaura Abbott }
788af86e597SLaura Abbott 
789af86e597SLaura Abbott static inline pte_t * fixmap_pte(unsigned long addr)
790af86e597SLaura Abbott {
791157962f5SArd Biesheuvel 	return &bm_pte[pte_index(addr)];
792af86e597SLaura Abbott }
793af86e597SLaura Abbott 
7942077be67SLaura Abbott /*
7952077be67SLaura Abbott  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
7962077be67SLaura Abbott  * directly on kernel symbols (bm_p*d). This function is called too early to use
7972077be67SLaura Abbott  * lm_alias so __p*d_populate functions must be used to populate with the
7982077be67SLaura Abbott  * physical address from __pa_symbol.
7992077be67SLaura Abbott  */
800af86e597SLaura Abbott void __init early_fixmap_init(void)
801af86e597SLaura Abbott {
80220a004e7SWill Deacon 	pgd_t *pgdp, pgd;
80320a004e7SWill Deacon 	pud_t *pudp;
80420a004e7SWill Deacon 	pmd_t *pmdp;
805af86e597SLaura Abbott 	unsigned long addr = FIXADDR_START;
806af86e597SLaura Abbott 
80720a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
80820a004e7SWill Deacon 	pgd = READ_ONCE(*pgdp);
809f80fb3a3SArd Biesheuvel 	if (CONFIG_PGTABLE_LEVELS > 3 &&
81020a004e7SWill Deacon 	    !(pgd_none(pgd) || pgd_page_paddr(pgd) == __pa_symbol(bm_pud))) {
811f9040773SArd Biesheuvel 		/*
812f9040773SArd Biesheuvel 		 * We only end up here if the kernel mapping and the fixmap
813f9040773SArd Biesheuvel 		 * share the top level pgd entry, which should only happen on
814f9040773SArd Biesheuvel 		 * 16k/4 levels configurations.
815f9040773SArd Biesheuvel 		 */
816f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
81720a004e7SWill Deacon 		pudp = pud_offset_kimg(pgdp, addr);
818f9040773SArd Biesheuvel 	} else {
81920a004e7SWill Deacon 		if (pgd_none(pgd))
82020a004e7SWill Deacon 			__pgd_populate(pgdp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
82120a004e7SWill Deacon 		pudp = fixmap_pud(addr);
822f9040773SArd Biesheuvel 	}
82320a004e7SWill Deacon 	if (pud_none(READ_ONCE(*pudp)))
82420a004e7SWill Deacon 		__pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
82520a004e7SWill Deacon 	pmdp = fixmap_pmd(addr);
82620a004e7SWill Deacon 	__pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
827af86e597SLaura Abbott 
828af86e597SLaura Abbott 	/*
829af86e597SLaura Abbott 	 * The boot-ioremap range spans multiple pmds, for which
830157962f5SArd Biesheuvel 	 * we are not prepared:
831af86e597SLaura Abbott 	 */
832af86e597SLaura Abbott 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
833af86e597SLaura Abbott 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
834af86e597SLaura Abbott 
83520a004e7SWill Deacon 	if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
83620a004e7SWill Deacon 	     || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
837af86e597SLaura Abbott 		WARN_ON(1);
83820a004e7SWill Deacon 		pr_warn("pmdp %p != %p, %p\n",
83920a004e7SWill Deacon 			pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
840af86e597SLaura Abbott 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
841af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
842af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_BEGIN));
843af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
844af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_END));
845af86e597SLaura Abbott 
846af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
847af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
848af86e597SLaura Abbott 	}
849af86e597SLaura Abbott }
850af86e597SLaura Abbott 
85118b4b276SJames Morse /*
85218b4b276SJames Morse  * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
85318b4b276SJames Morse  * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
85418b4b276SJames Morse  */
855af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx,
856af86e597SLaura Abbott 			       phys_addr_t phys, pgprot_t flags)
857af86e597SLaura Abbott {
858af86e597SLaura Abbott 	unsigned long addr = __fix_to_virt(idx);
85920a004e7SWill Deacon 	pte_t *ptep;
860af86e597SLaura Abbott 
861b63dbef9SMark Rutland 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
862af86e597SLaura Abbott 
86320a004e7SWill Deacon 	ptep = fixmap_pte(addr);
864af86e597SLaura Abbott 
865af86e597SLaura Abbott 	if (pgprot_val(flags)) {
86620a004e7SWill Deacon 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
867af86e597SLaura Abbott 	} else {
86820a004e7SWill Deacon 		pte_clear(&init_mm, addr, ptep);
869af86e597SLaura Abbott 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
870af86e597SLaura Abbott 	}
871af86e597SLaura Abbott }
87261bd93ceSArd Biesheuvel 
873f80fb3a3SArd Biesheuvel void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
87461bd93ceSArd Biesheuvel {
87561bd93ceSArd Biesheuvel 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
876f80fb3a3SArd Biesheuvel 	int offset;
87761bd93ceSArd Biesheuvel 	void *dt_virt;
87861bd93ceSArd Biesheuvel 
87961bd93ceSArd Biesheuvel 	/*
88061bd93ceSArd Biesheuvel 	 * Check whether the physical FDT address is set and meets the minimum
88161bd93ceSArd Biesheuvel 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
88204a84810SArd Biesheuvel 	 * at least 8 bytes so that we can always access the magic and size
88304a84810SArd Biesheuvel 	 * fields of the FDT header after mapping the first chunk, double check
88404a84810SArd Biesheuvel 	 * here if that is indeed the case.
88561bd93ceSArd Biesheuvel 	 */
88661bd93ceSArd Biesheuvel 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
88761bd93ceSArd Biesheuvel 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
88861bd93ceSArd Biesheuvel 		return NULL;
88961bd93ceSArd Biesheuvel 
89061bd93ceSArd Biesheuvel 	/*
89161bd93ceSArd Biesheuvel 	 * Make sure that the FDT region can be mapped without the need to
89261bd93ceSArd Biesheuvel 	 * allocate additional translation table pages, so that it is safe
893132233a7SLaura Abbott 	 * to call create_mapping_noalloc() this early.
89461bd93ceSArd Biesheuvel 	 *
89561bd93ceSArd Biesheuvel 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
89661bd93ceSArd Biesheuvel 	 * be in the same PMD as the rest of the fixmap.
89761bd93ceSArd Biesheuvel 	 * On 4k pages, we'll use section mappings for the FDT so we only
89861bd93ceSArd Biesheuvel 	 * have to be in the same PUD.
89961bd93ceSArd Biesheuvel 	 */
90061bd93ceSArd Biesheuvel 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
90161bd93ceSArd Biesheuvel 
902b433dce0SSuzuki K. Poulose 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
903b433dce0SSuzuki K. Poulose 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
90461bd93ceSArd Biesheuvel 
905b433dce0SSuzuki K. Poulose 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
90661bd93ceSArd Biesheuvel 	dt_virt = (void *)dt_virt_base + offset;
90761bd93ceSArd Biesheuvel 
90861bd93ceSArd Biesheuvel 	/* map the first chunk so we can read the size from the header */
909132233a7SLaura Abbott 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
910132233a7SLaura Abbott 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
91161bd93ceSArd Biesheuvel 
91204a84810SArd Biesheuvel 	if (fdt_magic(dt_virt) != FDT_MAGIC)
91361bd93ceSArd Biesheuvel 		return NULL;
91461bd93ceSArd Biesheuvel 
915f80fb3a3SArd Biesheuvel 	*size = fdt_totalsize(dt_virt);
916f80fb3a3SArd Biesheuvel 	if (*size > MAX_FDT_SIZE)
91761bd93ceSArd Biesheuvel 		return NULL;
91861bd93ceSArd Biesheuvel 
919f80fb3a3SArd Biesheuvel 	if (offset + *size > SWAPPER_BLOCK_SIZE)
920132233a7SLaura Abbott 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
921f80fb3a3SArd Biesheuvel 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
922f80fb3a3SArd Biesheuvel 
923f80fb3a3SArd Biesheuvel 	return dt_virt;
924f80fb3a3SArd Biesheuvel }
925f80fb3a3SArd Biesheuvel 
926f80fb3a3SArd Biesheuvel void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
927f80fb3a3SArd Biesheuvel {
928f80fb3a3SArd Biesheuvel 	void *dt_virt;
929f80fb3a3SArd Biesheuvel 	int size;
930f80fb3a3SArd Biesheuvel 
931f80fb3a3SArd Biesheuvel 	dt_virt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
932f80fb3a3SArd Biesheuvel 	if (!dt_virt)
933f80fb3a3SArd Biesheuvel 		return NULL;
93461bd93ceSArd Biesheuvel 
93561bd93ceSArd Biesheuvel 	memblock_reserve(dt_phys, size);
93661bd93ceSArd Biesheuvel 	return dt_virt;
93761bd93ceSArd Biesheuvel }
938324420bfSArd Biesheuvel 
939324420bfSArd Biesheuvel int __init arch_ioremap_pud_supported(void)
940324420bfSArd Biesheuvel {
941324420bfSArd Biesheuvel 	/* only 4k granule supports level 1 block mappings */
942324420bfSArd Biesheuvel 	return IS_ENABLED(CONFIG_ARM64_4K_PAGES);
943324420bfSArd Biesheuvel }
944324420bfSArd Biesheuvel 
945324420bfSArd Biesheuvel int __init arch_ioremap_pmd_supported(void)
946324420bfSArd Biesheuvel {
947324420bfSArd Biesheuvel 	return 1;
948324420bfSArd Biesheuvel }
949324420bfSArd Biesheuvel 
95020a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
951324420bfSArd Biesheuvel {
95219338304SKristina Martsenko 	pgprot_t sect_prot = __pgprot(PUD_TYPE_SECT |
95319338304SKristina Martsenko 					pgprot_val(mk_sect_prot(prot)));
95482034c23SLaura Abbott 	pud_t new_pud = pfn_pud(__phys_to_pfn(phys), sect_prot);
95515122ee2SWill Deacon 
95682034c23SLaura Abbott 	/* Only allow permission changes for now */
95782034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
95882034c23SLaura Abbott 				   pud_val(new_pud)))
95915122ee2SWill Deacon 		return 0;
96015122ee2SWill Deacon 
961324420bfSArd Biesheuvel 	BUG_ON(phys & ~PUD_MASK);
96282034c23SLaura Abbott 	set_pud(pudp, new_pud);
963324420bfSArd Biesheuvel 	return 1;
964324420bfSArd Biesheuvel }
965324420bfSArd Biesheuvel 
96620a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
967324420bfSArd Biesheuvel {
96819338304SKristina Martsenko 	pgprot_t sect_prot = __pgprot(PMD_TYPE_SECT |
96919338304SKristina Martsenko 					pgprot_val(mk_sect_prot(prot)));
97082034c23SLaura Abbott 	pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), sect_prot);
97115122ee2SWill Deacon 
97282034c23SLaura Abbott 	/* Only allow permission changes for now */
97382034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
97482034c23SLaura Abbott 				   pmd_val(new_pmd)))
97515122ee2SWill Deacon 		return 0;
97615122ee2SWill Deacon 
977324420bfSArd Biesheuvel 	BUG_ON(phys & ~PMD_MASK);
97882034c23SLaura Abbott 	set_pmd(pmdp, new_pmd);
979324420bfSArd Biesheuvel 	return 1;
980324420bfSArd Biesheuvel }
981324420bfSArd Biesheuvel 
98220a004e7SWill Deacon int pud_clear_huge(pud_t *pudp)
983324420bfSArd Biesheuvel {
98420a004e7SWill Deacon 	if (!pud_sect(READ_ONCE(*pudp)))
985324420bfSArd Biesheuvel 		return 0;
98620a004e7SWill Deacon 	pud_clear(pudp);
987324420bfSArd Biesheuvel 	return 1;
988324420bfSArd Biesheuvel }
989324420bfSArd Biesheuvel 
99020a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp)
991324420bfSArd Biesheuvel {
99220a004e7SWill Deacon 	if (!pmd_sect(READ_ONCE(*pmdp)))
993324420bfSArd Biesheuvel 		return 0;
99420a004e7SWill Deacon 	pmd_clear(pmdp);
995324420bfSArd Biesheuvel 	return 1;
996324420bfSArd Biesheuvel }
997b6bdb751SToshi Kani 
998ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
999b6bdb751SToshi Kani {
1000ec28bb9cSChintan Pandya 	pte_t *table;
1001ec28bb9cSChintan Pandya 	pmd_t pmd;
1002ec28bb9cSChintan Pandya 
1003ec28bb9cSChintan Pandya 	pmd = READ_ONCE(*pmdp);
1004ec28bb9cSChintan Pandya 
1005fac880c7SMark Rutland 	if (!pmd_present(pmd))
1006fac880c7SMark Rutland 		return 1;
1007fac880c7SMark Rutland 	if (!pmd_table(pmd)) {
1008ec28bb9cSChintan Pandya 		VM_WARN_ON(!pmd_table(pmd));
1009ec28bb9cSChintan Pandya 		return 1;
1010b6bdb751SToshi Kani 	}
1011b6bdb751SToshi Kani 
1012ec28bb9cSChintan Pandya 	table = pte_offset_kernel(pmdp, addr);
1013ec28bb9cSChintan Pandya 	pmd_clear(pmdp);
1014ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1015ec28bb9cSChintan Pandya 	pte_free_kernel(NULL, table);
1016ec28bb9cSChintan Pandya 	return 1;
1017ec28bb9cSChintan Pandya }
1018ec28bb9cSChintan Pandya 
1019ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1020b6bdb751SToshi Kani {
1021ec28bb9cSChintan Pandya 	pmd_t *table;
1022ec28bb9cSChintan Pandya 	pmd_t *pmdp;
1023ec28bb9cSChintan Pandya 	pud_t pud;
1024ec28bb9cSChintan Pandya 	unsigned long next, end;
1025ec28bb9cSChintan Pandya 
1026ec28bb9cSChintan Pandya 	pud = READ_ONCE(*pudp);
1027ec28bb9cSChintan Pandya 
1028fac880c7SMark Rutland 	if (!pud_present(pud))
1029fac880c7SMark Rutland 		return 1;
1030fac880c7SMark Rutland 	if (!pud_table(pud)) {
1031ec28bb9cSChintan Pandya 		VM_WARN_ON(!pud_table(pud));
1032ec28bb9cSChintan Pandya 		return 1;
1033ec28bb9cSChintan Pandya 	}
1034ec28bb9cSChintan Pandya 
1035ec28bb9cSChintan Pandya 	table = pmd_offset(pudp, addr);
1036ec28bb9cSChintan Pandya 	pmdp = table;
1037ec28bb9cSChintan Pandya 	next = addr;
1038ec28bb9cSChintan Pandya 	end = addr + PUD_SIZE;
1039ec28bb9cSChintan Pandya 	do {
1040ec28bb9cSChintan Pandya 		pmd_free_pte_page(pmdp, next);
1041ec28bb9cSChintan Pandya 	} while (pmdp++, next += PMD_SIZE, next != end);
1042ec28bb9cSChintan Pandya 
1043ec28bb9cSChintan Pandya 	pud_clear(pudp);
1044ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1045ec28bb9cSChintan Pandya 	pmd_free(NULL, table);
1046ec28bb9cSChintan Pandya 	return 1;
1047b6bdb751SToshi Kani }
1048