xref: /openbmc/linux/arch/arm64/mm/mmu.c (revision 840b2398)
1caab277bSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c1cc1552SCatalin Marinas /*
3c1cc1552SCatalin Marinas  * Based on arch/arm/mm/mmu.c
4c1cc1552SCatalin Marinas  *
5c1cc1552SCatalin Marinas  * Copyright (C) 1995-2005 Russell King
6c1cc1552SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
7c1cc1552SCatalin Marinas  */
8c1cc1552SCatalin Marinas 
95a9e3e15SJisheng Zhang #include <linux/cache.h>
10c1cc1552SCatalin Marinas #include <linux/export.h>
11c1cc1552SCatalin Marinas #include <linux/kernel.h>
12c1cc1552SCatalin Marinas #include <linux/errno.h>
13c1cc1552SCatalin Marinas #include <linux/init.h>
1498d2e153STakahiro Akashi #include <linux/ioport.h>
1598d2e153STakahiro Akashi #include <linux/kexec.h>
1661bd93ceSArd Biesheuvel #include <linux/libfdt.h>
17c1cc1552SCatalin Marinas #include <linux/mman.h>
18c1cc1552SCatalin Marinas #include <linux/nodemask.h>
19c1cc1552SCatalin Marinas #include <linux/memblock.h>
20bbd6ec60SAnshuman Khandual #include <linux/memory.h>
21c1cc1552SCatalin Marinas #include <linux/fs.h>
222475ff9dSCatalin Marinas #include <linux/io.h>
232077be67SLaura Abbott #include <linux/mm.h>
246efd8499STobias Klauser #include <linux/vmalloc.h>
25c1cc1552SCatalin Marinas 
2621ab99c2SMark Rutland #include <asm/barrier.h>
27c1cc1552SCatalin Marinas #include <asm/cputype.h>
28af86e597SLaura Abbott #include <asm/fixmap.h>
29068a17a5SMark Rutland #include <asm/kasan.h>
30b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h>
31c1cc1552SCatalin Marinas #include <asm/sections.h>
32c1cc1552SCatalin Marinas #include <asm/setup.h>
3387dfb311SMasahiro Yamada #include <linux/sizes.h>
34c1cc1552SCatalin Marinas #include <asm/tlb.h>
35c1cc1552SCatalin Marinas #include <asm/mmu_context.h>
361404d6f1SLaura Abbott #include <asm/ptdump.h>
37ec28bb9cSChintan Pandya #include <asm/tlbflush.h>
38ca15ca40SMike Rapoport #include <asm/pgalloc.h>
39c1cc1552SCatalin Marinas 
40c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS	BIT(0)
41d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS	BIT(1)
42c0951366SArd Biesheuvel 
43dd006da2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
44fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
45dd006da2SArd Biesheuvel 
465383cc6eSSteve Capper u64 __section(".mmuoff.data.write") vabits_actual;
475383cc6eSSteve Capper EXPORT_SYMBOL(vabits_actual);
48c1cc1552SCatalin Marinas 
495a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init;
50a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset);
51a7f8de16SArd Biesheuvel 
52c1cc1552SCatalin Marinas /*
53c1cc1552SCatalin Marinas  * Empty_zero_page is a special page that is used for zero-initialized data
54c1cc1552SCatalin Marinas  * and COW.
55c1cc1552SCatalin Marinas  */
565227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
57c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page);
58c1cc1552SCatalin Marinas 
59f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
60f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
61f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
62f9040773SArd Biesheuvel 
632330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock);
642330b7caSJun Yao 
652330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
662330b7caSJun Yao {
672330b7caSJun Yao 	pgd_t *fixmap_pgdp;
682330b7caSJun Yao 
692330b7caSJun Yao 	spin_lock(&swapper_pgdir_lock);
7026a6f87eSJames Morse 	fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
712330b7caSJun Yao 	WRITE_ONCE(*fixmap_pgdp, pgd);
722330b7caSJun Yao 	/*
732330b7caSJun Yao 	 * We need dsb(ishst) here to ensure the page-table-walker sees
742330b7caSJun Yao 	 * our new entry before set_p?d() returns. The fixmap's
752330b7caSJun Yao 	 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
762330b7caSJun Yao 	 */
772330b7caSJun Yao 	pgd_clear_fixmap();
782330b7caSJun Yao 	spin_unlock(&swapper_pgdir_lock);
792330b7caSJun Yao }
802330b7caSJun Yao 
81c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
82c1cc1552SCatalin Marinas 			      unsigned long size, pgprot_t vma_prot)
83c1cc1552SCatalin Marinas {
84c1cc1552SCatalin Marinas 	if (!pfn_valid(pfn))
85c1cc1552SCatalin Marinas 		return pgprot_noncached(vma_prot);
86c1cc1552SCatalin Marinas 	else if (file->f_flags & O_SYNC)
87c1cc1552SCatalin Marinas 		return pgprot_writecombine(vma_prot);
88c1cc1552SCatalin Marinas 	return vma_prot;
89c1cc1552SCatalin Marinas }
90c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot);
91c1cc1552SCatalin Marinas 
9290292acaSYu Zhao static phys_addr_t __init early_pgtable_alloc(int shift)
93c1cc1552SCatalin Marinas {
947142392dSSuzuki K. Poulose 	phys_addr_t phys;
957142392dSSuzuki K. Poulose 	void *ptr;
967142392dSSuzuki K. Poulose 
979a8dd708SMike Rapoport 	phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
98ecc3e771SMike Rapoport 	if (!phys)
99ecc3e771SMike Rapoport 		panic("Failed to allocate page table page\n");
100f4710445SMark Rutland 
101f4710445SMark Rutland 	/*
102f4710445SMark Rutland 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
103f4710445SMark Rutland 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
104f4710445SMark Rutland 	 * any level of table.
105f4710445SMark Rutland 	 */
106f4710445SMark Rutland 	ptr = pte_set_fixmap(phys);
107f4710445SMark Rutland 
10821ab99c2SMark Rutland 	memset(ptr, 0, PAGE_SIZE);
10921ab99c2SMark Rutland 
110f4710445SMark Rutland 	/*
111f4710445SMark Rutland 	 * Implicit barriers also ensure the zeroed page is visible to the page
112f4710445SMark Rutland 	 * table walker
113f4710445SMark Rutland 	 */
114f4710445SMark Rutland 	pte_clear_fixmap();
115f4710445SMark Rutland 
116f4710445SMark Rutland 	return phys;
117c1cc1552SCatalin Marinas }
118c1cc1552SCatalin Marinas 
119e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new)
120e98216b5SArd Biesheuvel {
121e98216b5SArd Biesheuvel 	/*
122e98216b5SArd Biesheuvel 	 * The following mapping attributes may be updated in live
123e98216b5SArd Biesheuvel 	 * kernel mappings without the need for break-before-make.
124e98216b5SArd Biesheuvel 	 */
1250178dc76SCatalin Marinas 	pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
126e98216b5SArd Biesheuvel 
127141d1497SArd Biesheuvel 	/* creating or taking down mappings is always safe */
128141d1497SArd Biesheuvel 	if (old == 0 || new == 0)
129141d1497SArd Biesheuvel 		return true;
130141d1497SArd Biesheuvel 
131141d1497SArd Biesheuvel 	/* live contiguous mappings may not be manipulated at all */
132141d1497SArd Biesheuvel 	if ((old | new) & PTE_CONT)
133141d1497SArd Biesheuvel 		return false;
134141d1497SArd Biesheuvel 
135753e8abcSArd Biesheuvel 	/* Transitioning from Non-Global to Global is unsafe */
136753e8abcSArd Biesheuvel 	if (old & ~new & PTE_NG)
137753e8abcSArd Biesheuvel 		return false;
1384e602056SWill Deacon 
1390178dc76SCatalin Marinas 	/*
1400178dc76SCatalin Marinas 	 * Changing the memory type between Normal and Normal-Tagged is safe
1410178dc76SCatalin Marinas 	 * since Tagged is considered a permission attribute from the
1420178dc76SCatalin Marinas 	 * mismatched attribute aliases perspective.
1430178dc76SCatalin Marinas 	 */
1440178dc76SCatalin Marinas 	if (((old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
1450178dc76SCatalin Marinas 	     (old & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)) &&
1460178dc76SCatalin Marinas 	    ((new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL) ||
1470178dc76SCatalin Marinas 	     (new & PTE_ATTRINDX_MASK) == PTE_ATTRINDX(MT_NORMAL_TAGGED)))
1480178dc76SCatalin Marinas 		mask |= PTE_ATTRINDX_MASK;
1490178dc76SCatalin Marinas 
150141d1497SArd Biesheuvel 	return ((old ^ new) & ~mask) == 0;
151e98216b5SArd Biesheuvel }
152e98216b5SArd Biesheuvel 
15320a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
154d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot)
155c1cc1552SCatalin Marinas {
15620a004e7SWill Deacon 	pte_t *ptep;
157c1cc1552SCatalin Marinas 
15820a004e7SWill Deacon 	ptep = pte_set_fixmap_offset(pmdp, addr);
159c1cc1552SCatalin Marinas 	do {
16020a004e7SWill Deacon 		pte_t old_pte = READ_ONCE(*ptep);
161e98216b5SArd Biesheuvel 
16220a004e7SWill Deacon 		set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
163e98216b5SArd Biesheuvel 
164e98216b5SArd Biesheuvel 		/*
165e98216b5SArd Biesheuvel 		 * After the PTE entry has been populated once, we
166e98216b5SArd Biesheuvel 		 * only allow updates to the permission attributes.
167e98216b5SArd Biesheuvel 		 */
16820a004e7SWill Deacon 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
16920a004e7SWill Deacon 					      READ_ONCE(pte_val(*ptep))));
170e98216b5SArd Biesheuvel 
171e393cf40SArd Biesheuvel 		phys += PAGE_SIZE;
17220a004e7SWill Deacon 	} while (ptep++, addr += PAGE_SIZE, addr != end);
173f4710445SMark Rutland 
174f4710445SMark Rutland 	pte_clear_fixmap();
175c1cc1552SCatalin Marinas }
176c1cc1552SCatalin Marinas 
17720a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
178d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
179d27cfa1fSArd Biesheuvel 				pgprot_t prot,
18090292acaSYu Zhao 				phys_addr_t (*pgtable_alloc)(int),
181c0951366SArd Biesheuvel 				int flags)
182c1cc1552SCatalin Marinas {
183c1cc1552SCatalin Marinas 	unsigned long next;
18420a004e7SWill Deacon 	pmd_t pmd = READ_ONCE(*pmdp);
185c1cc1552SCatalin Marinas 
18620a004e7SWill Deacon 	BUG_ON(pmd_sect(pmd));
18720a004e7SWill Deacon 	if (pmd_none(pmd)) {
188d27cfa1fSArd Biesheuvel 		phys_addr_t pte_phys;
189132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
19090292acaSYu Zhao 		pte_phys = pgtable_alloc(PAGE_SHIFT);
19120a004e7SWill Deacon 		__pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
19220a004e7SWill Deacon 		pmd = READ_ONCE(*pmdp);
193c1cc1552SCatalin Marinas 	}
19420a004e7SWill Deacon 	BUG_ON(pmd_bad(pmd));
195d27cfa1fSArd Biesheuvel 
196d27cfa1fSArd Biesheuvel 	do {
197d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
198d27cfa1fSArd Biesheuvel 
199d27cfa1fSArd Biesheuvel 		next = pte_cont_addr_end(addr, end);
200d27cfa1fSArd Biesheuvel 
201d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
202d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
203d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
204d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
205d27cfa1fSArd Biesheuvel 
20620a004e7SWill Deacon 		init_pte(pmdp, addr, next, phys, __prot);
207d27cfa1fSArd Biesheuvel 
208d27cfa1fSArd Biesheuvel 		phys += next - addr;
209d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
210d27cfa1fSArd Biesheuvel }
211d27cfa1fSArd Biesheuvel 
21220a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
213d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot,
21490292acaSYu Zhao 		     phys_addr_t (*pgtable_alloc)(int), int flags)
215d27cfa1fSArd Biesheuvel {
216d27cfa1fSArd Biesheuvel 	unsigned long next;
21720a004e7SWill Deacon 	pmd_t *pmdp;
218c1cc1552SCatalin Marinas 
21920a004e7SWill Deacon 	pmdp = pmd_set_fixmap_offset(pudp, addr);
220c1cc1552SCatalin Marinas 	do {
22120a004e7SWill Deacon 		pmd_t old_pmd = READ_ONCE(*pmdp);
222e98216b5SArd Biesheuvel 
223c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
224e98216b5SArd Biesheuvel 
225c1cc1552SCatalin Marinas 		/* try section mapping first */
22683863f25SLaura Abbott 		if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
227c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
22820a004e7SWill Deacon 			pmd_set_huge(pmdp, phys, prot);
229e98216b5SArd Biesheuvel 
230a55f9929SCatalin Marinas 			/*
231e98216b5SArd Biesheuvel 			 * After the PMD entry has been populated once, we
232e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
233a55f9929SCatalin Marinas 			 */
234e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
23520a004e7SWill Deacon 						      READ_ONCE(pmd_val(*pmdp))));
236a55f9929SCatalin Marinas 		} else {
23720a004e7SWill Deacon 			alloc_init_cont_pte(pmdp, addr, next, phys, prot,
238d27cfa1fSArd Biesheuvel 					    pgtable_alloc, flags);
239e98216b5SArd Biesheuvel 
240e98216b5SArd Biesheuvel 			BUG_ON(pmd_val(old_pmd) != 0 &&
24120a004e7SWill Deacon 			       pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
242a55f9929SCatalin Marinas 		}
243c1cc1552SCatalin Marinas 		phys += next - addr;
24420a004e7SWill Deacon 	} while (pmdp++, addr = next, addr != end);
245f4710445SMark Rutland 
246f4710445SMark Rutland 	pmd_clear_fixmap();
247c1cc1552SCatalin Marinas }
248c1cc1552SCatalin Marinas 
24920a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
250d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
251d27cfa1fSArd Biesheuvel 				pgprot_t prot,
25290292acaSYu Zhao 				phys_addr_t (*pgtable_alloc)(int), int flags)
253d27cfa1fSArd Biesheuvel {
254d27cfa1fSArd Biesheuvel 	unsigned long next;
25520a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
256d27cfa1fSArd Biesheuvel 
257d27cfa1fSArd Biesheuvel 	/*
258d27cfa1fSArd Biesheuvel 	 * Check for initial section mappings in the pgd/pud.
259d27cfa1fSArd Biesheuvel 	 */
26020a004e7SWill Deacon 	BUG_ON(pud_sect(pud));
26120a004e7SWill Deacon 	if (pud_none(pud)) {
262d27cfa1fSArd Biesheuvel 		phys_addr_t pmd_phys;
263d27cfa1fSArd Biesheuvel 		BUG_ON(!pgtable_alloc);
26490292acaSYu Zhao 		pmd_phys = pgtable_alloc(PMD_SHIFT);
26520a004e7SWill Deacon 		__pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
26620a004e7SWill Deacon 		pud = READ_ONCE(*pudp);
267d27cfa1fSArd Biesheuvel 	}
26820a004e7SWill Deacon 	BUG_ON(pud_bad(pud));
269d27cfa1fSArd Biesheuvel 
270d27cfa1fSArd Biesheuvel 	do {
271d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
272d27cfa1fSArd Biesheuvel 
273d27cfa1fSArd Biesheuvel 		next = pmd_cont_addr_end(addr, end);
274d27cfa1fSArd Biesheuvel 
275d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
276d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
277d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
278d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
279d27cfa1fSArd Biesheuvel 
28020a004e7SWill Deacon 		init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
281d27cfa1fSArd Biesheuvel 
282d27cfa1fSArd Biesheuvel 		phys += next - addr;
283d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
284d27cfa1fSArd Biesheuvel }
285d27cfa1fSArd Biesheuvel 
286da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next,
287da141706SLaura Abbott 			unsigned long phys)
288da141706SLaura Abbott {
289da141706SLaura Abbott 	if (PAGE_SHIFT != 12)
290da141706SLaura Abbott 		return false;
291da141706SLaura Abbott 
292da141706SLaura Abbott 	if (((addr | next | phys) & ~PUD_MASK) != 0)
293da141706SLaura Abbott 		return false;
294da141706SLaura Abbott 
295da141706SLaura Abbott 	return true;
296da141706SLaura Abbott }
297da141706SLaura Abbott 
29820a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
299da141706SLaura Abbott 			   phys_addr_t phys, pgprot_t prot,
30090292acaSYu Zhao 			   phys_addr_t (*pgtable_alloc)(int),
301c0951366SArd Biesheuvel 			   int flags)
302c1cc1552SCatalin Marinas {
303c1cc1552SCatalin Marinas 	unsigned long next;
30420a004e7SWill Deacon 	pud_t *pudp;
305e9f63768SMike Rapoport 	p4d_t *p4dp = p4d_offset(pgdp, addr);
306e9f63768SMike Rapoport 	p4d_t p4d = READ_ONCE(*p4dp);
307c1cc1552SCatalin Marinas 
308e9f63768SMike Rapoport 	if (p4d_none(p4d)) {
309132233a7SLaura Abbott 		phys_addr_t pud_phys;
310132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
31190292acaSYu Zhao 		pud_phys = pgtable_alloc(PUD_SHIFT);
312e9f63768SMike Rapoport 		__p4d_populate(p4dp, pud_phys, PUD_TYPE_TABLE);
313e9f63768SMike Rapoport 		p4d = READ_ONCE(*p4dp);
314c79b954bSJungseok Lee 	}
315e9f63768SMike Rapoport 	BUG_ON(p4d_bad(p4d));
316c79b954bSJungseok Lee 
317e9f63768SMike Rapoport 	pudp = pud_set_fixmap_offset(p4dp, addr);
318c1cc1552SCatalin Marinas 	do {
31920a004e7SWill Deacon 		pud_t old_pud = READ_ONCE(*pudp);
320e98216b5SArd Biesheuvel 
321c1cc1552SCatalin Marinas 		next = pud_addr_end(addr, end);
322206a2a73SSteve Capper 
323206a2a73SSteve Capper 		/*
324206a2a73SSteve Capper 		 * For 4K granule only, attempt to put down a 1GB block
325206a2a73SSteve Capper 		 */
326c0951366SArd Biesheuvel 		if (use_1G_block(addr, next, phys) &&
327c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
32820a004e7SWill Deacon 			pud_set_huge(pudp, phys, prot);
329206a2a73SSteve Capper 
330206a2a73SSteve Capper 			/*
331e98216b5SArd Biesheuvel 			 * After the PUD entry has been populated once, we
332e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
333206a2a73SSteve Capper 			 */
334e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
33520a004e7SWill Deacon 						      READ_ONCE(pud_val(*pudp))));
336206a2a73SSteve Capper 		} else {
33720a004e7SWill Deacon 			alloc_init_cont_pmd(pudp, addr, next, phys, prot,
338c0951366SArd Biesheuvel 					    pgtable_alloc, flags);
339e98216b5SArd Biesheuvel 
340e98216b5SArd Biesheuvel 			BUG_ON(pud_val(old_pud) != 0 &&
34120a004e7SWill Deacon 			       pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
342206a2a73SSteve Capper 		}
343c1cc1552SCatalin Marinas 		phys += next - addr;
34420a004e7SWill Deacon 	} while (pudp++, addr = next, addr != end);
345f4710445SMark Rutland 
346f4710445SMark Rutland 	pud_clear_fixmap();
347c1cc1552SCatalin Marinas }
348c1cc1552SCatalin Marinas 
34940f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
35040f87d31SArd Biesheuvel 				 unsigned long virt, phys_addr_t size,
35140f87d31SArd Biesheuvel 				 pgprot_t prot,
35290292acaSYu Zhao 				 phys_addr_t (*pgtable_alloc)(int),
353c0951366SArd Biesheuvel 				 int flags)
354c1cc1552SCatalin Marinas {
35532d18708SMasahiro Yamada 	unsigned long addr, end, next;
356974b9b2cSMike Rapoport 	pgd_t *pgdp = pgd_offset_pgd(pgdir, virt);
357c1cc1552SCatalin Marinas 
358cc5d2b3bSMark Rutland 	/*
359cc5d2b3bSMark Rutland 	 * If the virtual and physical address don't have the same offset
360cc5d2b3bSMark Rutland 	 * within a page, we cannot map the region as the caller expects.
361cc5d2b3bSMark Rutland 	 */
362cc5d2b3bSMark Rutland 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
363cc5d2b3bSMark Rutland 		return;
364cc5d2b3bSMark Rutland 
3659c4e08a3SMark Rutland 	phys &= PAGE_MASK;
366c1cc1552SCatalin Marinas 	addr = virt & PAGE_MASK;
36732d18708SMasahiro Yamada 	end = PAGE_ALIGN(virt + size);
368c1cc1552SCatalin Marinas 
369c1cc1552SCatalin Marinas 	do {
370c1cc1552SCatalin Marinas 		next = pgd_addr_end(addr, end);
37120a004e7SWill Deacon 		alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
372c0951366SArd Biesheuvel 			       flags);
373c1cc1552SCatalin Marinas 		phys += next - addr;
37420a004e7SWill Deacon 	} while (pgdp++, addr = next, addr != end);
375c1cc1552SCatalin Marinas }
376c1cc1552SCatalin Marinas 
377475ba3fcSWill Deacon static phys_addr_t __pgd_pgtable_alloc(int shift)
378369aaab8SYu Zhao {
37950f11a8aSMike Rapoport 	void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL);
380369aaab8SYu Zhao 	BUG_ON(!ptr);
381369aaab8SYu Zhao 
382369aaab8SYu Zhao 	/* Ensure the zeroed page is visible to the page table walker */
383369aaab8SYu Zhao 	dsb(ishst);
384369aaab8SYu Zhao 	return __pa(ptr);
385369aaab8SYu Zhao }
386369aaab8SYu Zhao 
38790292acaSYu Zhao static phys_addr_t pgd_pgtable_alloc(int shift)
388da141706SLaura Abbott {
389475ba3fcSWill Deacon 	phys_addr_t pa = __pgd_pgtable_alloc(shift);
39090292acaSYu Zhao 
39190292acaSYu Zhao 	/*
39290292acaSYu Zhao 	 * Call proper page table ctor in case later we need to
39390292acaSYu Zhao 	 * call core mm functions like apply_to_page_range() on
39490292acaSYu Zhao 	 * this pre-allocated page table.
39590292acaSYu Zhao 	 *
39690292acaSYu Zhao 	 * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is
39790292acaSYu Zhao 	 * folded, and if so pgtable_pmd_page_ctor() becomes nop.
39890292acaSYu Zhao 	 */
39990292acaSYu Zhao 	if (shift == PAGE_SHIFT)
400b4ed71f5SMark Rutland 		BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa)));
40190292acaSYu Zhao 	else if (shift == PMD_SHIFT)
402475ba3fcSWill Deacon 		BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa)));
40321ab99c2SMark Rutland 
404475ba3fcSWill Deacon 	return pa;
405da141706SLaura Abbott }
406da141706SLaura Abbott 
407132233a7SLaura Abbott /*
408132233a7SLaura Abbott  * This function can only be used to modify existing table entries,
409132233a7SLaura Abbott  * without allocating new levels of table. Note that this permits the
410132233a7SLaura Abbott  * creation of new section or page entries.
411132233a7SLaura Abbott  */
412132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
413da141706SLaura Abbott 				  phys_addr_t size, pgprot_t prot)
414d7ecbddfSMark Salter {
41577ad4ce6SMark Rutland 	if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
416d7ecbddfSMark Salter 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
417d7ecbddfSMark Salter 			&phys, virt);
418d7ecbddfSMark Salter 		return;
419d7ecbddfSMark Salter 	}
420d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
421d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
422d7ecbddfSMark Salter }
423d7ecbddfSMark Salter 
4248ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
4258ce837ceSArd Biesheuvel 			       unsigned long virt, phys_addr_t size,
426f14c66ceSArd Biesheuvel 			       pgprot_t prot, bool page_mappings_only)
4278ce837ceSArd Biesheuvel {
428c0951366SArd Biesheuvel 	int flags = 0;
429c0951366SArd Biesheuvel 
4301378dc3dSArd Biesheuvel 	BUG_ON(mm == &init_mm);
4311378dc3dSArd Biesheuvel 
432c0951366SArd Biesheuvel 	if (page_mappings_only)
433d27cfa1fSArd Biesheuvel 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
434c0951366SArd Biesheuvel 
43511509a30SMark Rutland 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
436c0951366SArd Biesheuvel 			     pgd_pgtable_alloc, flags);
437d7ecbddfSMark Salter }
438d7ecbddfSMark Salter 
439aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
440da141706SLaura Abbott 				phys_addr_t size, pgprot_t prot)
441da141706SLaura Abbott {
44277ad4ce6SMark Rutland 	if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
443aa8c09beSArd Biesheuvel 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
444da141706SLaura Abbott 			&phys, virt);
445da141706SLaura Abbott 		return;
446da141706SLaura Abbott 	}
447da141706SLaura Abbott 
448d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
449d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
450aa8c09beSArd Biesheuvel 
451aa8c09beSArd Biesheuvel 	/* flush the TLBs after updating live kernel mappings */
452aa8c09beSArd Biesheuvel 	flush_tlb_kernel_range(virt, virt + size);
453da141706SLaura Abbott }
454da141706SLaura Abbott 
45520a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
45698d2e153STakahiro Akashi 				  phys_addr_t end, pgprot_t prot, int flags)
457da141706SLaura Abbott {
45820a004e7SWill Deacon 	__create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
45998d2e153STakahiro Akashi 			     prot, early_pgtable_alloc, flags);
460da141706SLaura Abbott }
461da141706SLaura Abbott 
4625ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void)
4635ea5306cSArd Biesheuvel {
4645ea5306cSArd Biesheuvel 	/*
4655ea5306cSArd Biesheuvel 	 * Remove the write permissions from the linear alias of .text/.rodata
4665ea5306cSArd Biesheuvel 	 */
467e2a073ddSArd Biesheuvel 	update_mapping_prot(__pa_symbol(_stext), (unsigned long)lm_alias(_stext),
468e2a073ddSArd Biesheuvel 			    (unsigned long)__init_begin - (unsigned long)_stext,
4695ea5306cSArd Biesheuvel 			    PAGE_KERNEL_RO);
4705ea5306cSArd Biesheuvel }
4715ea5306cSArd Biesheuvel 
4722687275aSCatalin Marinas static bool crash_mem_map __initdata;
4732687275aSCatalin Marinas 
4742687275aSCatalin Marinas static int __init enable_crash_mem_map(char *arg)
4752687275aSCatalin Marinas {
4762687275aSCatalin Marinas 	/*
4772687275aSCatalin Marinas 	 * Proper parameter parsing is done by reserve_crashkernel(). We only
4782687275aSCatalin Marinas 	 * need to know if the linear map has to avoid block mappings so that
4792687275aSCatalin Marinas 	 * the crashkernel reservations can be unmapped later.
4802687275aSCatalin Marinas 	 */
4812687275aSCatalin Marinas 	crash_mem_map = true;
4822687275aSCatalin Marinas 
4832687275aSCatalin Marinas 	return 0;
4842687275aSCatalin Marinas }
4852687275aSCatalin Marinas early_param("crashkernel", enable_crash_mem_map);
4862687275aSCatalin Marinas 
48720a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp)
488c1cc1552SCatalin Marinas {
489e2a073ddSArd Biesheuvel 	phys_addr_t kernel_start = __pa_symbol(_stext);
49098d2e153STakahiro Akashi 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
491b10d6bcaSMike Rapoport 	phys_addr_t start, end;
49298d2e153STakahiro Akashi 	int flags = 0;
493b10d6bcaSMike Rapoport 	u64 i;
49498d2e153STakahiro Akashi 
4952687275aSCatalin Marinas 	if (rodata_full || crash_mem_map || debug_pagealloc_enabled())
49698d2e153STakahiro Akashi 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
49798d2e153STakahiro Akashi 
49898d2e153STakahiro Akashi 	/*
49998d2e153STakahiro Akashi 	 * Take care not to create a writable alias for the
50098d2e153STakahiro Akashi 	 * read-only text and rodata sections of the kernel image.
50198d2e153STakahiro Akashi 	 * So temporarily mark them as NOMAP to skip mappings in
50298d2e153STakahiro Akashi 	 * the following for-loop
50398d2e153STakahiro Akashi 	 */
50498d2e153STakahiro Akashi 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
505f6bc87c3SSteve Capper 
506c1cc1552SCatalin Marinas 	/* map all the memory banks */
507b10d6bcaSMike Rapoport 	for_each_mem_range(i, &start, &end) {
508c1cc1552SCatalin Marinas 		if (start >= end)
509c1cc1552SCatalin Marinas 			break;
5100178dc76SCatalin Marinas 		/*
5110178dc76SCatalin Marinas 		 * The linear map must allow allocation tags reading/writing
5120178dc76SCatalin Marinas 		 * if MTE is present. Otherwise, it has the same attributes as
5130178dc76SCatalin Marinas 		 * PAGE_KERNEL.
5140178dc76SCatalin Marinas 		 */
5150178dc76SCatalin Marinas 		__map_memblock(pgdp, start, end, PAGE_KERNEL_TAGGED, flags);
516c1cc1552SCatalin Marinas 	}
51798d2e153STakahiro Akashi 
51898d2e153STakahiro Akashi 	/*
519e2a073ddSArd Biesheuvel 	 * Map the linear alias of the [_stext, __init_begin) interval
52098d2e153STakahiro Akashi 	 * as non-executable now, and remove the write permission in
52198d2e153STakahiro Akashi 	 * mark_linear_text_alias_ro() below (which will be called after
52298d2e153STakahiro Akashi 	 * alternative patching has completed). This makes the contents
52398d2e153STakahiro Akashi 	 * of the region accessible to subsystems such as hibernate,
52498d2e153STakahiro Akashi 	 * but protects it from inadvertent modification or execution.
52598d2e153STakahiro Akashi 	 * Note that contiguous mappings cannot be remapped in this way,
52698d2e153STakahiro Akashi 	 * so we should avoid them here.
52798d2e153STakahiro Akashi 	 */
52820a004e7SWill Deacon 	__map_memblock(pgdp, kernel_start, kernel_end,
52998d2e153STakahiro Akashi 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
53098d2e153STakahiro Akashi 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
531c1cc1552SCatalin Marinas }
532c1cc1552SCatalin Marinas 
533da141706SLaura Abbott void mark_rodata_ro(void)
534da141706SLaura Abbott {
5352f39b5f9SJeremy Linton 	unsigned long section_size;
536f9040773SArd Biesheuvel 
5372f39b5f9SJeremy Linton 	/*
5389fdc14c5SArd Biesheuvel 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
5399fdc14c5SArd Biesheuvel 	 * to cover NOTES and EXCEPTION_TABLE.
5402f39b5f9SJeremy Linton 	 */
5419fdc14c5SArd Biesheuvel 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
542aa8c09beSArd Biesheuvel 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
5432f39b5f9SJeremy Linton 			    section_size, PAGE_KERNEL_RO);
544e98216b5SArd Biesheuvel 
5451404d6f1SLaura Abbott 	debug_checkwx();
546da141706SLaura Abbott }
547da141706SLaura Abbott 
54820a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
549d27cfa1fSArd Biesheuvel 				      pgprot_t prot, struct vm_struct *vma,
55092bbd16eSWill Deacon 				      int flags, unsigned long vm_flags)
551068a17a5SMark Rutland {
5522077be67SLaura Abbott 	phys_addr_t pa_start = __pa_symbol(va_start);
553068a17a5SMark Rutland 	unsigned long size = va_end - va_start;
554068a17a5SMark Rutland 
555068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(pa_start));
556068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(size));
557068a17a5SMark Rutland 
55820a004e7SWill Deacon 	__create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
559d27cfa1fSArd Biesheuvel 			     early_pgtable_alloc, flags);
560f9040773SArd Biesheuvel 
56192bbd16eSWill Deacon 	if (!(vm_flags & VM_NO_GUARD))
56292bbd16eSWill Deacon 		size += PAGE_SIZE;
56392bbd16eSWill Deacon 
564f9040773SArd Biesheuvel 	vma->addr	= va_start;
565f9040773SArd Biesheuvel 	vma->phys_addr	= pa_start;
566f9040773SArd Biesheuvel 	vma->size	= size;
56792bbd16eSWill Deacon 	vma->flags	= VM_MAP | vm_flags;
568f9040773SArd Biesheuvel 	vma->caller	= __builtin_return_address(0);
569f9040773SArd Biesheuvel 
570f9040773SArd Biesheuvel 	vm_area_add_early(vma);
571068a17a5SMark Rutland }
572068a17a5SMark Rutland 
57328b066daSArd Biesheuvel static int __init parse_rodata(char *arg)
57428b066daSArd Biesheuvel {
575c55191e9SArd Biesheuvel 	int ret = strtobool(arg, &rodata_enabled);
576c55191e9SArd Biesheuvel 	if (!ret) {
577c55191e9SArd Biesheuvel 		rodata_full = false;
578c55191e9SArd Biesheuvel 		return 0;
579c55191e9SArd Biesheuvel 	}
580c55191e9SArd Biesheuvel 
581c55191e9SArd Biesheuvel 	/* permit 'full' in addition to boolean options */
582c55191e9SArd Biesheuvel 	if (strcmp(arg, "full"))
583c55191e9SArd Biesheuvel 		return -EINVAL;
584c55191e9SArd Biesheuvel 
585c55191e9SArd Biesheuvel 	rodata_enabled = true;
586c55191e9SArd Biesheuvel 	rodata_full = true;
587c55191e9SArd Biesheuvel 	return 0;
58828b066daSArd Biesheuvel }
58928b066daSArd Biesheuvel early_param("rodata", parse_rodata);
59028b066daSArd Biesheuvel 
59151a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
59251a0048bSWill Deacon static int __init map_entry_trampoline(void)
59351a0048bSWill Deacon {
59451a0048bSWill Deacon 	pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
59551a0048bSWill Deacon 	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
59651a0048bSWill Deacon 
59751a0048bSWill Deacon 	/* The trampoline is always mapped and can therefore be global */
59851a0048bSWill Deacon 	pgprot_val(prot) &= ~PTE_NG;
59951a0048bSWill Deacon 
60051a0048bSWill Deacon 	/* Map only the text into the trampoline page table */
60151a0048bSWill Deacon 	memset(tramp_pg_dir, 0, PGD_SIZE);
60251a0048bSWill Deacon 	__create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE,
603475ba3fcSWill Deacon 			     prot, __pgd_pgtable_alloc, 0);
60451a0048bSWill Deacon 
6056c27c408SWill Deacon 	/* Map both the text and data into the kernel page table */
60651a0048bSWill Deacon 	__set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot);
6076c27c408SWill Deacon 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
6086c27c408SWill Deacon 		extern char __entry_tramp_data_start[];
6096c27c408SWill Deacon 
6106c27c408SWill Deacon 		__set_fixmap(FIX_ENTRY_TRAMP_DATA,
6116c27c408SWill Deacon 			     __pa_symbol(__entry_tramp_data_start),
6126c27c408SWill Deacon 			     PAGE_KERNEL_RO);
6136c27c408SWill Deacon 	}
6146c27c408SWill Deacon 
61551a0048bSWill Deacon 	return 0;
61651a0048bSWill Deacon }
61751a0048bSWill Deacon core_initcall(map_entry_trampoline);
61851a0048bSWill Deacon #endif
61951a0048bSWill Deacon 
620068a17a5SMark Rutland /*
621c8027285SMark Brown  * Open coded check for BTI, only for use to determine configuration
622c8027285SMark Brown  * for early mappings for before the cpufeature code has run.
623c8027285SMark Brown  */
624c8027285SMark Brown static bool arm64_early_this_cpu_has_bti(void)
625c8027285SMark Brown {
626c8027285SMark Brown 	u64 pfr1;
627c8027285SMark Brown 
628c8027285SMark Brown 	if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
629c8027285SMark Brown 		return false;
630c8027285SMark Brown 
63193ad55b7SMarc Zyngier 	pfr1 = __read_sysreg_by_encoding(SYS_ID_AA64PFR1_EL1);
632c8027285SMark Brown 	return cpuid_feature_extract_unsigned_field(pfr1,
633c8027285SMark Brown 						    ID_AA64PFR1_BT_SHIFT);
634c8027285SMark Brown }
635c8027285SMark Brown 
636c8027285SMark Brown /*
637068a17a5SMark Rutland  * Create fine-grained mappings for the kernel.
638068a17a5SMark Rutland  */
63920a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp)
640068a17a5SMark Rutland {
6412ebe088bSArd Biesheuvel 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
6422ebe088bSArd Biesheuvel 				vmlinux_initdata, vmlinux_data;
643068a17a5SMark Rutland 
64428b066daSArd Biesheuvel 	/*
64528b066daSArd Biesheuvel 	 * External debuggers may need to write directly to the text
64628b066daSArd Biesheuvel 	 * mapping to install SW breakpoints. Allow this (only) when
64728b066daSArd Biesheuvel 	 * explicitly requested with rodata=off.
64828b066daSArd Biesheuvel 	 */
64928b066daSArd Biesheuvel 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
65028b066daSArd Biesheuvel 
651d27cfa1fSArd Biesheuvel 	/*
652c8027285SMark Brown 	 * If we have a CPU that supports BTI and a kernel built for
653c8027285SMark Brown 	 * BTI then mark the kernel executable text as guarded pages
654c8027285SMark Brown 	 * now so we don't have to rewrite the page tables later.
655c8027285SMark Brown 	 */
656c8027285SMark Brown 	if (arm64_early_this_cpu_has_bti())
657c8027285SMark Brown 		text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
658c8027285SMark Brown 
659c8027285SMark Brown 	/*
660d27cfa1fSArd Biesheuvel 	 * Only rodata will be remapped with different permissions later on,
661d27cfa1fSArd Biesheuvel 	 * all other segments are allowed to use contiguous mappings.
662d27cfa1fSArd Biesheuvel 	 */
663e2a073ddSArd Biesheuvel 	map_kernel_segment(pgdp, _stext, _etext, text_prot, &vmlinux_text, 0,
66492bbd16eSWill Deacon 			   VM_NO_GUARD);
66520a004e7SWill Deacon 	map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
66692bbd16eSWill Deacon 			   &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
66720a004e7SWill Deacon 	map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
66892bbd16eSWill Deacon 			   &vmlinux_inittext, 0, VM_NO_GUARD);
66920a004e7SWill Deacon 	map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
67092bbd16eSWill Deacon 			   &vmlinux_initdata, 0, VM_NO_GUARD);
67120a004e7SWill Deacon 	map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
672068a17a5SMark Rutland 
673974b9b2cSMike Rapoport 	if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) {
674068a17a5SMark Rutland 		/*
675f9040773SArd Biesheuvel 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
676f9040773SArd Biesheuvel 		 * live in the carveout for the swapper_pg_dir. We can simply
677f9040773SArd Biesheuvel 		 * re-use the existing dir for the fixmap.
678068a17a5SMark Rutland 		 */
679974b9b2cSMike Rapoport 		set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START),
68020a004e7SWill Deacon 			READ_ONCE(*pgd_offset_k(FIXADDR_START)));
681f9040773SArd Biesheuvel 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
682b333b0baSMark Rutland 		pgd_t *bm_pgdp;
683e9f63768SMike Rapoport 		p4d_t *bm_p4dp;
684b333b0baSMark Rutland 		pud_t *bm_pudp;
685f9040773SArd Biesheuvel 		/*
686f9040773SArd Biesheuvel 		 * The fixmap shares its top level pgd entry with the kernel
687f9040773SArd Biesheuvel 		 * mapping. This can really only occur when we are running
688f9040773SArd Biesheuvel 		 * with 16k/4 levels, so we can simply reuse the pud level
689f9040773SArd Biesheuvel 		 * entry instead.
690f9040773SArd Biesheuvel 		 */
691f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
692974b9b2cSMike Rapoport 		bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START);
693e9f63768SMike Rapoport 		bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START);
694e9f63768SMike Rapoport 		bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START);
695b333b0baSMark Rutland 		pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd));
696f9040773SArd Biesheuvel 		pud_clear_fixmap();
697f9040773SArd Biesheuvel 	} else {
698f9040773SArd Biesheuvel 		BUG();
699f9040773SArd Biesheuvel 	}
700068a17a5SMark Rutland 
70120a004e7SWill Deacon 	kasan_copy_shadow(pgdp);
702068a17a5SMark Rutland }
703068a17a5SMark Rutland 
704c1cc1552SCatalin Marinas void __init paging_init(void)
705c1cc1552SCatalin Marinas {
7062330b7caSJun Yao 	pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
707068a17a5SMark Rutland 
70820a004e7SWill Deacon 	map_kernel(pgdp);
70920a004e7SWill Deacon 	map_mem(pgdp);
710068a17a5SMark Rutland 
711068a17a5SMark Rutland 	pgd_clear_fixmap();
712068a17a5SMark Rutland 
713068a17a5SMark Rutland 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
7142b5548b6SJun Yao 	init_mm.pgd = swapper_pg_dir;
715068a17a5SMark Rutland 
7162b5548b6SJun Yao 	memblock_free(__pa_symbol(init_pg_dir),
7172b5548b6SJun Yao 		      __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
71824cc61d8SArd Biesheuvel 
71924cc61d8SArd Biesheuvel 	memblock_allow_resize();
720c1cc1552SCatalin Marinas }
721c1cc1552SCatalin Marinas 
722c1cc1552SCatalin Marinas /*
723c1cc1552SCatalin Marinas  * Check whether a kernel address is valid (derived from arch/x86/).
724c1cc1552SCatalin Marinas  */
725c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr)
726c1cc1552SCatalin Marinas {
72720a004e7SWill Deacon 	pgd_t *pgdp;
728e9f63768SMike Rapoport 	p4d_t *p4dp;
72920a004e7SWill Deacon 	pud_t *pudp, pud;
73020a004e7SWill Deacon 	pmd_t *pmdp, pmd;
73120a004e7SWill Deacon 	pte_t *ptep, pte;
732c1cc1552SCatalin Marinas 
7338dd4daa0SShyam Thombre 	addr = arch_kasan_reset_tag(addr);
734c1cc1552SCatalin Marinas 	if ((((long)addr) >> VA_BITS) != -1UL)
735c1cc1552SCatalin Marinas 		return 0;
736c1cc1552SCatalin Marinas 
73720a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
73820a004e7SWill Deacon 	if (pgd_none(READ_ONCE(*pgdp)))
739c1cc1552SCatalin Marinas 		return 0;
740c1cc1552SCatalin Marinas 
741e9f63768SMike Rapoport 	p4dp = p4d_offset(pgdp, addr);
742e9f63768SMike Rapoport 	if (p4d_none(READ_ONCE(*p4dp)))
743e9f63768SMike Rapoport 		return 0;
744e9f63768SMike Rapoport 
745e9f63768SMike Rapoport 	pudp = pud_offset(p4dp, addr);
74620a004e7SWill Deacon 	pud = READ_ONCE(*pudp);
74720a004e7SWill Deacon 	if (pud_none(pud))
748c1cc1552SCatalin Marinas 		return 0;
749c1cc1552SCatalin Marinas 
75020a004e7SWill Deacon 	if (pud_sect(pud))
75120a004e7SWill Deacon 		return pfn_valid(pud_pfn(pud));
752206a2a73SSteve Capper 
75320a004e7SWill Deacon 	pmdp = pmd_offset(pudp, addr);
75420a004e7SWill Deacon 	pmd = READ_ONCE(*pmdp);
75520a004e7SWill Deacon 	if (pmd_none(pmd))
756c1cc1552SCatalin Marinas 		return 0;
757c1cc1552SCatalin Marinas 
75820a004e7SWill Deacon 	if (pmd_sect(pmd))
75920a004e7SWill Deacon 		return pfn_valid(pmd_pfn(pmd));
760da6e4cb6SDave Anderson 
76120a004e7SWill Deacon 	ptep = pte_offset_kernel(pmdp, addr);
76220a004e7SWill Deacon 	pte = READ_ONCE(*ptep);
76320a004e7SWill Deacon 	if (pte_none(pte))
764c1cc1552SCatalin Marinas 		return 0;
765c1cc1552SCatalin Marinas 
76620a004e7SWill Deacon 	return pfn_valid(pte_pfn(pte));
767c1cc1552SCatalin Marinas }
768bbd6ec60SAnshuman Khandual 
769bbd6ec60SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG
770eee07935SAnshuman Khandual static void free_hotplug_page_range(struct page *page, size_t size,
771eee07935SAnshuman Khandual 				    struct vmem_altmap *altmap)
772bbd6ec60SAnshuman Khandual {
773eee07935SAnshuman Khandual 	if (altmap) {
774eee07935SAnshuman Khandual 		vmem_altmap_free(altmap, size >> PAGE_SHIFT);
775eee07935SAnshuman Khandual 	} else {
776bbd6ec60SAnshuman Khandual 		WARN_ON(PageReserved(page));
777bbd6ec60SAnshuman Khandual 		free_pages((unsigned long)page_address(page), get_order(size));
778bbd6ec60SAnshuman Khandual 	}
779eee07935SAnshuman Khandual }
780bbd6ec60SAnshuman Khandual 
781bbd6ec60SAnshuman Khandual static void free_hotplug_pgtable_page(struct page *page)
782bbd6ec60SAnshuman Khandual {
783eee07935SAnshuman Khandual 	free_hotplug_page_range(page, PAGE_SIZE, NULL);
784bbd6ec60SAnshuman Khandual }
785bbd6ec60SAnshuman Khandual 
786bbd6ec60SAnshuman Khandual static bool pgtable_range_aligned(unsigned long start, unsigned long end,
787bbd6ec60SAnshuman Khandual 				  unsigned long floor, unsigned long ceiling,
788bbd6ec60SAnshuman Khandual 				  unsigned long mask)
789bbd6ec60SAnshuman Khandual {
790bbd6ec60SAnshuman Khandual 	start &= mask;
791bbd6ec60SAnshuman Khandual 	if (start < floor)
792bbd6ec60SAnshuman Khandual 		return false;
793bbd6ec60SAnshuman Khandual 
794bbd6ec60SAnshuman Khandual 	if (ceiling) {
795bbd6ec60SAnshuman Khandual 		ceiling &= mask;
796bbd6ec60SAnshuman Khandual 		if (!ceiling)
797bbd6ec60SAnshuman Khandual 			return false;
798bbd6ec60SAnshuman Khandual 	}
799bbd6ec60SAnshuman Khandual 
800bbd6ec60SAnshuman Khandual 	if (end - 1 > ceiling - 1)
801bbd6ec60SAnshuman Khandual 		return false;
802bbd6ec60SAnshuman Khandual 	return true;
803bbd6ec60SAnshuman Khandual }
804bbd6ec60SAnshuman Khandual 
805bbd6ec60SAnshuman Khandual static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
806eee07935SAnshuman Khandual 				    unsigned long end, bool free_mapped,
807eee07935SAnshuman Khandual 				    struct vmem_altmap *altmap)
808bbd6ec60SAnshuman Khandual {
809bbd6ec60SAnshuman Khandual 	pte_t *ptep, pte;
810bbd6ec60SAnshuman Khandual 
811bbd6ec60SAnshuman Khandual 	do {
812bbd6ec60SAnshuman Khandual 		ptep = pte_offset_kernel(pmdp, addr);
813bbd6ec60SAnshuman Khandual 		pte = READ_ONCE(*ptep);
814bbd6ec60SAnshuman Khandual 		if (pte_none(pte))
815bbd6ec60SAnshuman Khandual 			continue;
816bbd6ec60SAnshuman Khandual 
817bbd6ec60SAnshuman Khandual 		WARN_ON(!pte_present(pte));
818bbd6ec60SAnshuman Khandual 		pte_clear(&init_mm, addr, ptep);
819bbd6ec60SAnshuman Khandual 		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
820bbd6ec60SAnshuman Khandual 		if (free_mapped)
821eee07935SAnshuman Khandual 			free_hotplug_page_range(pte_page(pte),
822eee07935SAnshuman Khandual 						PAGE_SIZE, altmap);
823bbd6ec60SAnshuman Khandual 	} while (addr += PAGE_SIZE, addr < end);
824bbd6ec60SAnshuman Khandual }
825bbd6ec60SAnshuman Khandual 
826bbd6ec60SAnshuman Khandual static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
827eee07935SAnshuman Khandual 				    unsigned long end, bool free_mapped,
828eee07935SAnshuman Khandual 				    struct vmem_altmap *altmap)
829bbd6ec60SAnshuman Khandual {
830bbd6ec60SAnshuman Khandual 	unsigned long next;
831bbd6ec60SAnshuman Khandual 	pmd_t *pmdp, pmd;
832bbd6ec60SAnshuman Khandual 
833bbd6ec60SAnshuman Khandual 	do {
834bbd6ec60SAnshuman Khandual 		next = pmd_addr_end(addr, end);
835bbd6ec60SAnshuman Khandual 		pmdp = pmd_offset(pudp, addr);
836bbd6ec60SAnshuman Khandual 		pmd = READ_ONCE(*pmdp);
837bbd6ec60SAnshuman Khandual 		if (pmd_none(pmd))
838bbd6ec60SAnshuman Khandual 			continue;
839bbd6ec60SAnshuman Khandual 
840bbd6ec60SAnshuman Khandual 		WARN_ON(!pmd_present(pmd));
841bbd6ec60SAnshuman Khandual 		if (pmd_sect(pmd)) {
842bbd6ec60SAnshuman Khandual 			pmd_clear(pmdp);
843bbd6ec60SAnshuman Khandual 
844bbd6ec60SAnshuman Khandual 			/*
845bbd6ec60SAnshuman Khandual 			 * One TLBI should be sufficient here as the PMD_SIZE
846bbd6ec60SAnshuman Khandual 			 * range is mapped with a single block entry.
847bbd6ec60SAnshuman Khandual 			 */
848bbd6ec60SAnshuman Khandual 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
849bbd6ec60SAnshuman Khandual 			if (free_mapped)
850bbd6ec60SAnshuman Khandual 				free_hotplug_page_range(pmd_page(pmd),
851eee07935SAnshuman Khandual 							PMD_SIZE, altmap);
852bbd6ec60SAnshuman Khandual 			continue;
853bbd6ec60SAnshuman Khandual 		}
854bbd6ec60SAnshuman Khandual 		WARN_ON(!pmd_table(pmd));
855eee07935SAnshuman Khandual 		unmap_hotplug_pte_range(pmdp, addr, next, free_mapped, altmap);
856bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
857bbd6ec60SAnshuman Khandual }
858bbd6ec60SAnshuman Khandual 
859bbd6ec60SAnshuman Khandual static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
860eee07935SAnshuman Khandual 				    unsigned long end, bool free_mapped,
861eee07935SAnshuman Khandual 				    struct vmem_altmap *altmap)
862bbd6ec60SAnshuman Khandual {
863bbd6ec60SAnshuman Khandual 	unsigned long next;
864bbd6ec60SAnshuman Khandual 	pud_t *pudp, pud;
865bbd6ec60SAnshuman Khandual 
866bbd6ec60SAnshuman Khandual 	do {
867bbd6ec60SAnshuman Khandual 		next = pud_addr_end(addr, end);
868bbd6ec60SAnshuman Khandual 		pudp = pud_offset(p4dp, addr);
869bbd6ec60SAnshuman Khandual 		pud = READ_ONCE(*pudp);
870bbd6ec60SAnshuman Khandual 		if (pud_none(pud))
871bbd6ec60SAnshuman Khandual 			continue;
872bbd6ec60SAnshuman Khandual 
873bbd6ec60SAnshuman Khandual 		WARN_ON(!pud_present(pud));
874bbd6ec60SAnshuman Khandual 		if (pud_sect(pud)) {
875bbd6ec60SAnshuman Khandual 			pud_clear(pudp);
876bbd6ec60SAnshuman Khandual 
877bbd6ec60SAnshuman Khandual 			/*
878bbd6ec60SAnshuman Khandual 			 * One TLBI should be sufficient here as the PUD_SIZE
879bbd6ec60SAnshuman Khandual 			 * range is mapped with a single block entry.
880bbd6ec60SAnshuman Khandual 			 */
881bbd6ec60SAnshuman Khandual 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
882bbd6ec60SAnshuman Khandual 			if (free_mapped)
883bbd6ec60SAnshuman Khandual 				free_hotplug_page_range(pud_page(pud),
884eee07935SAnshuman Khandual 							PUD_SIZE, altmap);
885bbd6ec60SAnshuman Khandual 			continue;
886bbd6ec60SAnshuman Khandual 		}
887bbd6ec60SAnshuman Khandual 		WARN_ON(!pud_table(pud));
888eee07935SAnshuman Khandual 		unmap_hotplug_pmd_range(pudp, addr, next, free_mapped, altmap);
889bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
890bbd6ec60SAnshuman Khandual }
891bbd6ec60SAnshuman Khandual 
892bbd6ec60SAnshuman Khandual static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
893eee07935SAnshuman Khandual 				    unsigned long end, bool free_mapped,
894eee07935SAnshuman Khandual 				    struct vmem_altmap *altmap)
895bbd6ec60SAnshuman Khandual {
896bbd6ec60SAnshuman Khandual 	unsigned long next;
897bbd6ec60SAnshuman Khandual 	p4d_t *p4dp, p4d;
898bbd6ec60SAnshuman Khandual 
899bbd6ec60SAnshuman Khandual 	do {
900bbd6ec60SAnshuman Khandual 		next = p4d_addr_end(addr, end);
901bbd6ec60SAnshuman Khandual 		p4dp = p4d_offset(pgdp, addr);
902bbd6ec60SAnshuman Khandual 		p4d = READ_ONCE(*p4dp);
903bbd6ec60SAnshuman Khandual 		if (p4d_none(p4d))
904bbd6ec60SAnshuman Khandual 			continue;
905bbd6ec60SAnshuman Khandual 
906bbd6ec60SAnshuman Khandual 		WARN_ON(!p4d_present(p4d));
907eee07935SAnshuman Khandual 		unmap_hotplug_pud_range(p4dp, addr, next, free_mapped, altmap);
908bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
909bbd6ec60SAnshuman Khandual }
910bbd6ec60SAnshuman Khandual 
911bbd6ec60SAnshuman Khandual static void unmap_hotplug_range(unsigned long addr, unsigned long end,
912eee07935SAnshuman Khandual 				bool free_mapped, struct vmem_altmap *altmap)
913bbd6ec60SAnshuman Khandual {
914bbd6ec60SAnshuman Khandual 	unsigned long next;
915bbd6ec60SAnshuman Khandual 	pgd_t *pgdp, pgd;
916bbd6ec60SAnshuman Khandual 
917eee07935SAnshuman Khandual 	/*
918eee07935SAnshuman Khandual 	 * altmap can only be used as vmemmap mapping backing memory.
919eee07935SAnshuman Khandual 	 * In case the backing memory itself is not being freed, then
920eee07935SAnshuman Khandual 	 * altmap is irrelevant. Warn about this inconsistency when
921eee07935SAnshuman Khandual 	 * encountered.
922eee07935SAnshuman Khandual 	 */
923eee07935SAnshuman Khandual 	WARN_ON(!free_mapped && altmap);
924eee07935SAnshuman Khandual 
925bbd6ec60SAnshuman Khandual 	do {
926bbd6ec60SAnshuman Khandual 		next = pgd_addr_end(addr, end);
927bbd6ec60SAnshuman Khandual 		pgdp = pgd_offset_k(addr);
928bbd6ec60SAnshuman Khandual 		pgd = READ_ONCE(*pgdp);
929bbd6ec60SAnshuman Khandual 		if (pgd_none(pgd))
930bbd6ec60SAnshuman Khandual 			continue;
931bbd6ec60SAnshuman Khandual 
932bbd6ec60SAnshuman Khandual 		WARN_ON(!pgd_present(pgd));
933eee07935SAnshuman Khandual 		unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped, altmap);
934bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
935bbd6ec60SAnshuman Khandual }
936bbd6ec60SAnshuman Khandual 
937bbd6ec60SAnshuman Khandual static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
938bbd6ec60SAnshuman Khandual 				 unsigned long end, unsigned long floor,
939bbd6ec60SAnshuman Khandual 				 unsigned long ceiling)
940bbd6ec60SAnshuman Khandual {
941bbd6ec60SAnshuman Khandual 	pte_t *ptep, pte;
942bbd6ec60SAnshuman Khandual 	unsigned long i, start = addr;
943bbd6ec60SAnshuman Khandual 
944bbd6ec60SAnshuman Khandual 	do {
945bbd6ec60SAnshuman Khandual 		ptep = pte_offset_kernel(pmdp, addr);
946bbd6ec60SAnshuman Khandual 		pte = READ_ONCE(*ptep);
947bbd6ec60SAnshuman Khandual 
948bbd6ec60SAnshuman Khandual 		/*
949bbd6ec60SAnshuman Khandual 		 * This is just a sanity check here which verifies that
950bbd6ec60SAnshuman Khandual 		 * pte clearing has been done by earlier unmap loops.
951bbd6ec60SAnshuman Khandual 		 */
952bbd6ec60SAnshuman Khandual 		WARN_ON(!pte_none(pte));
953bbd6ec60SAnshuman Khandual 	} while (addr += PAGE_SIZE, addr < end);
954bbd6ec60SAnshuman Khandual 
955bbd6ec60SAnshuman Khandual 	if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
956bbd6ec60SAnshuman Khandual 		return;
957bbd6ec60SAnshuman Khandual 
958bbd6ec60SAnshuman Khandual 	/*
959bbd6ec60SAnshuman Khandual 	 * Check whether we can free the pte page if the rest of the
960bbd6ec60SAnshuman Khandual 	 * entries are empty. Overlap with other regions have been
961bbd6ec60SAnshuman Khandual 	 * handled by the floor/ceiling check.
962bbd6ec60SAnshuman Khandual 	 */
963bbd6ec60SAnshuman Khandual 	ptep = pte_offset_kernel(pmdp, 0UL);
964bbd6ec60SAnshuman Khandual 	for (i = 0; i < PTRS_PER_PTE; i++) {
965bbd6ec60SAnshuman Khandual 		if (!pte_none(READ_ONCE(ptep[i])))
966bbd6ec60SAnshuman Khandual 			return;
967bbd6ec60SAnshuman Khandual 	}
968bbd6ec60SAnshuman Khandual 
969bbd6ec60SAnshuman Khandual 	pmd_clear(pmdp);
970bbd6ec60SAnshuman Khandual 	__flush_tlb_kernel_pgtable(start);
971bbd6ec60SAnshuman Khandual 	free_hotplug_pgtable_page(virt_to_page(ptep));
972bbd6ec60SAnshuman Khandual }
973bbd6ec60SAnshuman Khandual 
974bbd6ec60SAnshuman Khandual static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
975bbd6ec60SAnshuman Khandual 				 unsigned long end, unsigned long floor,
976bbd6ec60SAnshuman Khandual 				 unsigned long ceiling)
977bbd6ec60SAnshuman Khandual {
978bbd6ec60SAnshuman Khandual 	pmd_t *pmdp, pmd;
979bbd6ec60SAnshuman Khandual 	unsigned long i, next, start = addr;
980bbd6ec60SAnshuman Khandual 
981bbd6ec60SAnshuman Khandual 	do {
982bbd6ec60SAnshuman Khandual 		next = pmd_addr_end(addr, end);
983bbd6ec60SAnshuman Khandual 		pmdp = pmd_offset(pudp, addr);
984bbd6ec60SAnshuman Khandual 		pmd = READ_ONCE(*pmdp);
985bbd6ec60SAnshuman Khandual 		if (pmd_none(pmd))
986bbd6ec60SAnshuman Khandual 			continue;
987bbd6ec60SAnshuman Khandual 
988bbd6ec60SAnshuman Khandual 		WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
989bbd6ec60SAnshuman Khandual 		free_empty_pte_table(pmdp, addr, next, floor, ceiling);
990bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
991bbd6ec60SAnshuman Khandual 
992bbd6ec60SAnshuman Khandual 	if (CONFIG_PGTABLE_LEVELS <= 2)
993bbd6ec60SAnshuman Khandual 		return;
994bbd6ec60SAnshuman Khandual 
995bbd6ec60SAnshuman Khandual 	if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
996bbd6ec60SAnshuman Khandual 		return;
997bbd6ec60SAnshuman Khandual 
998bbd6ec60SAnshuman Khandual 	/*
999bbd6ec60SAnshuman Khandual 	 * Check whether we can free the pmd page if the rest of the
1000bbd6ec60SAnshuman Khandual 	 * entries are empty. Overlap with other regions have been
1001bbd6ec60SAnshuman Khandual 	 * handled by the floor/ceiling check.
1002bbd6ec60SAnshuman Khandual 	 */
1003bbd6ec60SAnshuman Khandual 	pmdp = pmd_offset(pudp, 0UL);
1004bbd6ec60SAnshuman Khandual 	for (i = 0; i < PTRS_PER_PMD; i++) {
1005bbd6ec60SAnshuman Khandual 		if (!pmd_none(READ_ONCE(pmdp[i])))
1006bbd6ec60SAnshuman Khandual 			return;
1007bbd6ec60SAnshuman Khandual 	}
1008bbd6ec60SAnshuman Khandual 
1009bbd6ec60SAnshuman Khandual 	pud_clear(pudp);
1010bbd6ec60SAnshuman Khandual 	__flush_tlb_kernel_pgtable(start);
1011bbd6ec60SAnshuman Khandual 	free_hotplug_pgtable_page(virt_to_page(pmdp));
1012bbd6ec60SAnshuman Khandual }
1013bbd6ec60SAnshuman Khandual 
1014bbd6ec60SAnshuman Khandual static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
1015bbd6ec60SAnshuman Khandual 				 unsigned long end, unsigned long floor,
1016bbd6ec60SAnshuman Khandual 				 unsigned long ceiling)
1017bbd6ec60SAnshuman Khandual {
1018bbd6ec60SAnshuman Khandual 	pud_t *pudp, pud;
1019bbd6ec60SAnshuman Khandual 	unsigned long i, next, start = addr;
1020bbd6ec60SAnshuman Khandual 
1021bbd6ec60SAnshuman Khandual 	do {
1022bbd6ec60SAnshuman Khandual 		next = pud_addr_end(addr, end);
1023bbd6ec60SAnshuman Khandual 		pudp = pud_offset(p4dp, addr);
1024bbd6ec60SAnshuman Khandual 		pud = READ_ONCE(*pudp);
1025bbd6ec60SAnshuman Khandual 		if (pud_none(pud))
1026bbd6ec60SAnshuman Khandual 			continue;
1027bbd6ec60SAnshuman Khandual 
1028bbd6ec60SAnshuman Khandual 		WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
1029bbd6ec60SAnshuman Khandual 		free_empty_pmd_table(pudp, addr, next, floor, ceiling);
1030bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
1031bbd6ec60SAnshuman Khandual 
1032bbd6ec60SAnshuman Khandual 	if (CONFIG_PGTABLE_LEVELS <= 3)
1033bbd6ec60SAnshuman Khandual 		return;
1034bbd6ec60SAnshuman Khandual 
1035bbd6ec60SAnshuman Khandual 	if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
1036bbd6ec60SAnshuman Khandual 		return;
1037bbd6ec60SAnshuman Khandual 
1038bbd6ec60SAnshuman Khandual 	/*
1039bbd6ec60SAnshuman Khandual 	 * Check whether we can free the pud page if the rest of the
1040bbd6ec60SAnshuman Khandual 	 * entries are empty. Overlap with other regions have been
1041bbd6ec60SAnshuman Khandual 	 * handled by the floor/ceiling check.
1042bbd6ec60SAnshuman Khandual 	 */
1043bbd6ec60SAnshuman Khandual 	pudp = pud_offset(p4dp, 0UL);
1044bbd6ec60SAnshuman Khandual 	for (i = 0; i < PTRS_PER_PUD; i++) {
1045bbd6ec60SAnshuman Khandual 		if (!pud_none(READ_ONCE(pudp[i])))
1046bbd6ec60SAnshuman Khandual 			return;
1047bbd6ec60SAnshuman Khandual 	}
1048bbd6ec60SAnshuman Khandual 
1049bbd6ec60SAnshuman Khandual 	p4d_clear(p4dp);
1050bbd6ec60SAnshuman Khandual 	__flush_tlb_kernel_pgtable(start);
1051bbd6ec60SAnshuman Khandual 	free_hotplug_pgtable_page(virt_to_page(pudp));
1052bbd6ec60SAnshuman Khandual }
1053bbd6ec60SAnshuman Khandual 
1054bbd6ec60SAnshuman Khandual static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
1055bbd6ec60SAnshuman Khandual 				 unsigned long end, unsigned long floor,
1056bbd6ec60SAnshuman Khandual 				 unsigned long ceiling)
1057bbd6ec60SAnshuman Khandual {
1058bbd6ec60SAnshuman Khandual 	unsigned long next;
1059bbd6ec60SAnshuman Khandual 	p4d_t *p4dp, p4d;
1060bbd6ec60SAnshuman Khandual 
1061bbd6ec60SAnshuman Khandual 	do {
1062bbd6ec60SAnshuman Khandual 		next = p4d_addr_end(addr, end);
1063bbd6ec60SAnshuman Khandual 		p4dp = p4d_offset(pgdp, addr);
1064bbd6ec60SAnshuman Khandual 		p4d = READ_ONCE(*p4dp);
1065bbd6ec60SAnshuman Khandual 		if (p4d_none(p4d))
1066bbd6ec60SAnshuman Khandual 			continue;
1067bbd6ec60SAnshuman Khandual 
1068bbd6ec60SAnshuman Khandual 		WARN_ON(!p4d_present(p4d));
1069bbd6ec60SAnshuman Khandual 		free_empty_pud_table(p4dp, addr, next, floor, ceiling);
1070bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
1071bbd6ec60SAnshuman Khandual }
1072bbd6ec60SAnshuman Khandual 
1073bbd6ec60SAnshuman Khandual static void free_empty_tables(unsigned long addr, unsigned long end,
1074bbd6ec60SAnshuman Khandual 			      unsigned long floor, unsigned long ceiling)
1075bbd6ec60SAnshuman Khandual {
1076bbd6ec60SAnshuman Khandual 	unsigned long next;
1077bbd6ec60SAnshuman Khandual 	pgd_t *pgdp, pgd;
1078bbd6ec60SAnshuman Khandual 
1079bbd6ec60SAnshuman Khandual 	do {
1080bbd6ec60SAnshuman Khandual 		next = pgd_addr_end(addr, end);
1081bbd6ec60SAnshuman Khandual 		pgdp = pgd_offset_k(addr);
1082bbd6ec60SAnshuman Khandual 		pgd = READ_ONCE(*pgdp);
1083bbd6ec60SAnshuman Khandual 		if (pgd_none(pgd))
1084bbd6ec60SAnshuman Khandual 			continue;
1085bbd6ec60SAnshuman Khandual 
1086bbd6ec60SAnshuman Khandual 		WARN_ON(!pgd_present(pgd));
1087bbd6ec60SAnshuman Khandual 		free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
1088bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
1089bbd6ec60SAnshuman Khandual }
1090bbd6ec60SAnshuman Khandual #endif
1091bbd6ec60SAnshuman Khandual 
1092c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP
1093b433dce0SSuzuki K. Poulose #if !ARM64_SWAPPER_USES_SECTION_MAPS
10947b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
10957b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
1096c1cc1552SCatalin Marinas {
1097edb739eeSAnshuman Khandual 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1098eee07935SAnshuman Khandual 	return vmemmap_populate_basepages(start, end, node, altmap);
1099c1cc1552SCatalin Marinas }
1100b433dce0SSuzuki K. Poulose #else	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
11017b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
11027b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
1103c1cc1552SCatalin Marinas {
11040aad818bSJohannes Weiner 	unsigned long addr = start;
1105c1cc1552SCatalin Marinas 	unsigned long next;
110620a004e7SWill Deacon 	pgd_t *pgdp;
1107e9f63768SMike Rapoport 	p4d_t *p4dp;
110820a004e7SWill Deacon 	pud_t *pudp;
110920a004e7SWill Deacon 	pmd_t *pmdp;
1110c1cc1552SCatalin Marinas 
1111edb739eeSAnshuman Khandual 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1112c1cc1552SCatalin Marinas 	do {
1113c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
1114c1cc1552SCatalin Marinas 
111520a004e7SWill Deacon 		pgdp = vmemmap_pgd_populate(addr, node);
111620a004e7SWill Deacon 		if (!pgdp)
1117c1cc1552SCatalin Marinas 			return -ENOMEM;
1118c1cc1552SCatalin Marinas 
1119e9f63768SMike Rapoport 		p4dp = vmemmap_p4d_populate(pgdp, addr, node);
1120e9f63768SMike Rapoport 		if (!p4dp)
1121e9f63768SMike Rapoport 			return -ENOMEM;
1122e9f63768SMike Rapoport 
1123e9f63768SMike Rapoport 		pudp = vmemmap_pud_populate(p4dp, addr, node);
112420a004e7SWill Deacon 		if (!pudp)
1125c1cc1552SCatalin Marinas 			return -ENOMEM;
1126c1cc1552SCatalin Marinas 
112720a004e7SWill Deacon 		pmdp = pmd_offset(pudp, addr);
112820a004e7SWill Deacon 		if (pmd_none(READ_ONCE(*pmdp))) {
1129c1cc1552SCatalin Marinas 			void *p = NULL;
1130c1cc1552SCatalin Marinas 
1131eee07935SAnshuman Khandual 			p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
11329f84f39fSSudarshan Rajagopalan 			if (!p) {
11339f84f39fSSudarshan Rajagopalan 				if (vmemmap_populate_basepages(addr, next, node, altmap))
1134c1cc1552SCatalin Marinas 					return -ENOMEM;
11359f84f39fSSudarshan Rajagopalan 				continue;
11369f84f39fSSudarshan Rajagopalan 			}
1137c1cc1552SCatalin Marinas 
113820a004e7SWill Deacon 			pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
1139c1cc1552SCatalin Marinas 		} else
114020a004e7SWill Deacon 			vmemmap_verify((pte_t *)pmdp, node, addr, next);
1141c1cc1552SCatalin Marinas 	} while (addr = next, addr != end);
1142c1cc1552SCatalin Marinas 
1143c1cc1552SCatalin Marinas 	return 0;
1144c1cc1552SCatalin Marinas }
11458e01076aSOdin Ugedal #endif	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
114624b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end,
114724b6d416SChristoph Hellwig 		struct vmem_altmap *altmap)
11480197518cSTang Chen {
1149bbd6ec60SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG
1150bbd6ec60SAnshuman Khandual 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1151bbd6ec60SAnshuman Khandual 
1152eee07935SAnshuman Khandual 	unmap_hotplug_range(start, end, true, altmap);
1153bbd6ec60SAnshuman Khandual 	free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
1154bbd6ec60SAnshuman Khandual #endif
11550197518cSTang Chen }
1156c1cc1552SCatalin Marinas #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
1157af86e597SLaura Abbott 
1158af86e597SLaura Abbott static inline pud_t * fixmap_pud(unsigned long addr)
1159af86e597SLaura Abbott {
116020a004e7SWill Deacon 	pgd_t *pgdp = pgd_offset_k(addr);
1161e9f63768SMike Rapoport 	p4d_t *p4dp = p4d_offset(pgdp, addr);
1162e9f63768SMike Rapoport 	p4d_t p4d = READ_ONCE(*p4dp);
1163af86e597SLaura Abbott 
1164e9f63768SMike Rapoport 	BUG_ON(p4d_none(p4d) || p4d_bad(p4d));
1165af86e597SLaura Abbott 
1166e9f63768SMike Rapoport 	return pud_offset_kimg(p4dp, addr);
1167af86e597SLaura Abbott }
1168af86e597SLaura Abbott 
1169af86e597SLaura Abbott static inline pmd_t * fixmap_pmd(unsigned long addr)
1170af86e597SLaura Abbott {
117120a004e7SWill Deacon 	pud_t *pudp = fixmap_pud(addr);
117220a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
1173af86e597SLaura Abbott 
117420a004e7SWill Deacon 	BUG_ON(pud_none(pud) || pud_bad(pud));
1175af86e597SLaura Abbott 
117620a004e7SWill Deacon 	return pmd_offset_kimg(pudp, addr);
1177af86e597SLaura Abbott }
1178af86e597SLaura Abbott 
1179af86e597SLaura Abbott static inline pte_t * fixmap_pte(unsigned long addr)
1180af86e597SLaura Abbott {
1181157962f5SArd Biesheuvel 	return &bm_pte[pte_index(addr)];
1182af86e597SLaura Abbott }
1183af86e597SLaura Abbott 
11842077be67SLaura Abbott /*
11852077be67SLaura Abbott  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
11862077be67SLaura Abbott  * directly on kernel symbols (bm_p*d). This function is called too early to use
11872077be67SLaura Abbott  * lm_alias so __p*d_populate functions must be used to populate with the
11882077be67SLaura Abbott  * physical address from __pa_symbol.
11892077be67SLaura Abbott  */
1190af86e597SLaura Abbott void __init early_fixmap_init(void)
1191af86e597SLaura Abbott {
1192e9f63768SMike Rapoport 	pgd_t *pgdp;
1193e9f63768SMike Rapoport 	p4d_t *p4dp, p4d;
119420a004e7SWill Deacon 	pud_t *pudp;
119520a004e7SWill Deacon 	pmd_t *pmdp;
1196af86e597SLaura Abbott 	unsigned long addr = FIXADDR_START;
1197af86e597SLaura Abbott 
119820a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
1199e9f63768SMike Rapoport 	p4dp = p4d_offset(pgdp, addr);
1200e9f63768SMike Rapoport 	p4d = READ_ONCE(*p4dp);
1201f80fb3a3SArd Biesheuvel 	if (CONFIG_PGTABLE_LEVELS > 3 &&
1202e9f63768SMike Rapoport 	    !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) {
1203f9040773SArd Biesheuvel 		/*
1204f9040773SArd Biesheuvel 		 * We only end up here if the kernel mapping and the fixmap
1205f9040773SArd Biesheuvel 		 * share the top level pgd entry, which should only happen on
1206f9040773SArd Biesheuvel 		 * 16k/4 levels configurations.
1207f9040773SArd Biesheuvel 		 */
1208f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
1209e9f63768SMike Rapoport 		pudp = pud_offset_kimg(p4dp, addr);
1210f9040773SArd Biesheuvel 	} else {
1211e9f63768SMike Rapoport 		if (p4d_none(p4d))
1212e9f63768SMike Rapoport 			__p4d_populate(p4dp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
121320a004e7SWill Deacon 		pudp = fixmap_pud(addr);
1214f9040773SArd Biesheuvel 	}
121520a004e7SWill Deacon 	if (pud_none(READ_ONCE(*pudp)))
121620a004e7SWill Deacon 		__pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
121720a004e7SWill Deacon 	pmdp = fixmap_pmd(addr);
121820a004e7SWill Deacon 	__pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
1219af86e597SLaura Abbott 
1220af86e597SLaura Abbott 	/*
1221af86e597SLaura Abbott 	 * The boot-ioremap range spans multiple pmds, for which
1222157962f5SArd Biesheuvel 	 * we are not prepared:
1223af86e597SLaura Abbott 	 */
1224af86e597SLaura Abbott 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
1225af86e597SLaura Abbott 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
1226af86e597SLaura Abbott 
122720a004e7SWill Deacon 	if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
122820a004e7SWill Deacon 	     || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
1229af86e597SLaura Abbott 		WARN_ON(1);
123020a004e7SWill Deacon 		pr_warn("pmdp %p != %p, %p\n",
123120a004e7SWill Deacon 			pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
1232af86e597SLaura Abbott 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
1233af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
1234af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_BEGIN));
1235af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
1236af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_END));
1237af86e597SLaura Abbott 
1238af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
1239af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
1240af86e597SLaura Abbott 	}
1241af86e597SLaura Abbott }
1242af86e597SLaura Abbott 
124318b4b276SJames Morse /*
124418b4b276SJames Morse  * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
124518b4b276SJames Morse  * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
124618b4b276SJames Morse  */
1247af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx,
1248af86e597SLaura Abbott 			       phys_addr_t phys, pgprot_t flags)
1249af86e597SLaura Abbott {
1250af86e597SLaura Abbott 	unsigned long addr = __fix_to_virt(idx);
125120a004e7SWill Deacon 	pte_t *ptep;
1252af86e597SLaura Abbott 
1253b63dbef9SMark Rutland 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
1254af86e597SLaura Abbott 
125520a004e7SWill Deacon 	ptep = fixmap_pte(addr);
1256af86e597SLaura Abbott 
1257af86e597SLaura Abbott 	if (pgprot_val(flags)) {
125820a004e7SWill Deacon 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
1259af86e597SLaura Abbott 	} else {
126020a004e7SWill Deacon 		pte_clear(&init_mm, addr, ptep);
1261af86e597SLaura Abbott 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
1262af86e597SLaura Abbott 	}
1263af86e597SLaura Abbott }
126461bd93ceSArd Biesheuvel 
1265e112b032SHsin-Yi Wang void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
126661bd93ceSArd Biesheuvel {
126761bd93ceSArd Biesheuvel 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
1268f80fb3a3SArd Biesheuvel 	int offset;
126961bd93ceSArd Biesheuvel 	void *dt_virt;
127061bd93ceSArd Biesheuvel 
127161bd93ceSArd Biesheuvel 	/*
127261bd93ceSArd Biesheuvel 	 * Check whether the physical FDT address is set and meets the minimum
127361bd93ceSArd Biesheuvel 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
127404a84810SArd Biesheuvel 	 * at least 8 bytes so that we can always access the magic and size
127504a84810SArd Biesheuvel 	 * fields of the FDT header after mapping the first chunk, double check
127604a84810SArd Biesheuvel 	 * here if that is indeed the case.
127761bd93ceSArd Biesheuvel 	 */
127861bd93ceSArd Biesheuvel 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
127961bd93ceSArd Biesheuvel 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
128061bd93ceSArd Biesheuvel 		return NULL;
128161bd93ceSArd Biesheuvel 
128261bd93ceSArd Biesheuvel 	/*
128361bd93ceSArd Biesheuvel 	 * Make sure that the FDT region can be mapped without the need to
128461bd93ceSArd Biesheuvel 	 * allocate additional translation table pages, so that it is safe
1285132233a7SLaura Abbott 	 * to call create_mapping_noalloc() this early.
128661bd93ceSArd Biesheuvel 	 *
128761bd93ceSArd Biesheuvel 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
128861bd93ceSArd Biesheuvel 	 * be in the same PMD as the rest of the fixmap.
128961bd93ceSArd Biesheuvel 	 * On 4k pages, we'll use section mappings for the FDT so we only
129061bd93ceSArd Biesheuvel 	 * have to be in the same PUD.
129161bd93ceSArd Biesheuvel 	 */
129261bd93ceSArd Biesheuvel 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
129361bd93ceSArd Biesheuvel 
1294b433dce0SSuzuki K. Poulose 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
1295b433dce0SSuzuki K. Poulose 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
129661bd93ceSArd Biesheuvel 
1297b433dce0SSuzuki K. Poulose 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
129861bd93ceSArd Biesheuvel 	dt_virt = (void *)dt_virt_base + offset;
129961bd93ceSArd Biesheuvel 
130061bd93ceSArd Biesheuvel 	/* map the first chunk so we can read the size from the header */
1301132233a7SLaura Abbott 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
1302132233a7SLaura Abbott 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
130361bd93ceSArd Biesheuvel 
130404a84810SArd Biesheuvel 	if (fdt_magic(dt_virt) != FDT_MAGIC)
130561bd93ceSArd Biesheuvel 		return NULL;
130661bd93ceSArd Biesheuvel 
1307f80fb3a3SArd Biesheuvel 	*size = fdt_totalsize(dt_virt);
1308f80fb3a3SArd Biesheuvel 	if (*size > MAX_FDT_SIZE)
130961bd93ceSArd Biesheuvel 		return NULL;
131061bd93ceSArd Biesheuvel 
1311f80fb3a3SArd Biesheuvel 	if (offset + *size > SWAPPER_BLOCK_SIZE)
1312132233a7SLaura Abbott 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
1313f80fb3a3SArd Biesheuvel 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
1314f80fb3a3SArd Biesheuvel 
1315f80fb3a3SArd Biesheuvel 	return dt_virt;
1316f80fb3a3SArd Biesheuvel }
1317f80fb3a3SArd Biesheuvel 
13180f472d04SAnshuman Khandual int __init arch_ioremap_p4d_supported(void)
13190f472d04SAnshuman Khandual {
13200f472d04SAnshuman Khandual 	return 0;
13210f472d04SAnshuman Khandual }
13220f472d04SAnshuman Khandual 
1323324420bfSArd Biesheuvel int __init arch_ioremap_pud_supported(void)
1324324420bfSArd Biesheuvel {
13257ba36eccSMark Rutland 	/*
13267ba36eccSMark Rutland 	 * Only 4k granule supports level 1 block mappings.
13277ba36eccSMark Rutland 	 * SW table walks can't handle removal of intermediate entries.
13287ba36eccSMark Rutland 	 */
13297ba36eccSMark Rutland 	return IS_ENABLED(CONFIG_ARM64_4K_PAGES) &&
1330102f45fdSSteven Price 	       !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
1331324420bfSArd Biesheuvel }
1332324420bfSArd Biesheuvel 
1333324420bfSArd Biesheuvel int __init arch_ioremap_pmd_supported(void)
1334324420bfSArd Biesheuvel {
13357ba36eccSMark Rutland 	/* See arch_ioremap_pud_supported() */
1336102f45fdSSteven Price 	return !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
1337324420bfSArd Biesheuvel }
1338324420bfSArd Biesheuvel 
133920a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
1340324420bfSArd Biesheuvel {
1341f7f0097aSAnshuman Khandual 	pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
134215122ee2SWill Deacon 
134382034c23SLaura Abbott 	/* Only allow permission changes for now */
134482034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
134582034c23SLaura Abbott 				   pud_val(new_pud)))
134615122ee2SWill Deacon 		return 0;
134715122ee2SWill Deacon 
134887dedf7cSAnshuman Khandual 	VM_BUG_ON(phys & ~PUD_MASK);
134982034c23SLaura Abbott 	set_pud(pudp, new_pud);
1350324420bfSArd Biesheuvel 	return 1;
1351324420bfSArd Biesheuvel }
1352324420bfSArd Biesheuvel 
135320a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
1354324420bfSArd Biesheuvel {
1355f7f0097aSAnshuman Khandual 	pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
135615122ee2SWill Deacon 
135782034c23SLaura Abbott 	/* Only allow permission changes for now */
135882034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
135982034c23SLaura Abbott 				   pmd_val(new_pmd)))
136015122ee2SWill Deacon 		return 0;
136115122ee2SWill Deacon 
136287dedf7cSAnshuman Khandual 	VM_BUG_ON(phys & ~PMD_MASK);
136382034c23SLaura Abbott 	set_pmd(pmdp, new_pmd);
1364324420bfSArd Biesheuvel 	return 1;
1365324420bfSArd Biesheuvel }
1366324420bfSArd Biesheuvel 
136720a004e7SWill Deacon int pud_clear_huge(pud_t *pudp)
1368324420bfSArd Biesheuvel {
136920a004e7SWill Deacon 	if (!pud_sect(READ_ONCE(*pudp)))
1370324420bfSArd Biesheuvel 		return 0;
137120a004e7SWill Deacon 	pud_clear(pudp);
1372324420bfSArd Biesheuvel 	return 1;
1373324420bfSArd Biesheuvel }
1374324420bfSArd Biesheuvel 
137520a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp)
1376324420bfSArd Biesheuvel {
137720a004e7SWill Deacon 	if (!pmd_sect(READ_ONCE(*pmdp)))
1378324420bfSArd Biesheuvel 		return 0;
137920a004e7SWill Deacon 	pmd_clear(pmdp);
1380324420bfSArd Biesheuvel 	return 1;
1381324420bfSArd Biesheuvel }
1382b6bdb751SToshi Kani 
1383ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
1384b6bdb751SToshi Kani {
1385ec28bb9cSChintan Pandya 	pte_t *table;
1386ec28bb9cSChintan Pandya 	pmd_t pmd;
1387ec28bb9cSChintan Pandya 
1388ec28bb9cSChintan Pandya 	pmd = READ_ONCE(*pmdp);
1389ec28bb9cSChintan Pandya 
1390fac880c7SMark Rutland 	if (!pmd_table(pmd)) {
13919c006972SWill Deacon 		VM_WARN_ON(1);
1392ec28bb9cSChintan Pandya 		return 1;
1393b6bdb751SToshi Kani 	}
1394b6bdb751SToshi Kani 
1395ec28bb9cSChintan Pandya 	table = pte_offset_kernel(pmdp, addr);
1396ec28bb9cSChintan Pandya 	pmd_clear(pmdp);
1397ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1398ec28bb9cSChintan Pandya 	pte_free_kernel(NULL, table);
1399ec28bb9cSChintan Pandya 	return 1;
1400ec28bb9cSChintan Pandya }
1401ec28bb9cSChintan Pandya 
1402ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1403b6bdb751SToshi Kani {
1404ec28bb9cSChintan Pandya 	pmd_t *table;
1405ec28bb9cSChintan Pandya 	pmd_t *pmdp;
1406ec28bb9cSChintan Pandya 	pud_t pud;
1407ec28bb9cSChintan Pandya 	unsigned long next, end;
1408ec28bb9cSChintan Pandya 
1409ec28bb9cSChintan Pandya 	pud = READ_ONCE(*pudp);
1410ec28bb9cSChintan Pandya 
1411fac880c7SMark Rutland 	if (!pud_table(pud)) {
14129c006972SWill Deacon 		VM_WARN_ON(1);
1413ec28bb9cSChintan Pandya 		return 1;
1414ec28bb9cSChintan Pandya 	}
1415ec28bb9cSChintan Pandya 
1416ec28bb9cSChintan Pandya 	table = pmd_offset(pudp, addr);
1417ec28bb9cSChintan Pandya 	pmdp = table;
1418ec28bb9cSChintan Pandya 	next = addr;
1419ec28bb9cSChintan Pandya 	end = addr + PUD_SIZE;
1420ec28bb9cSChintan Pandya 	do {
1421ec28bb9cSChintan Pandya 		pmd_free_pte_page(pmdp, next);
1422ec28bb9cSChintan Pandya 	} while (pmdp++, next += PMD_SIZE, next != end);
1423ec28bb9cSChintan Pandya 
1424ec28bb9cSChintan Pandya 	pud_clear(pudp);
1425ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1426ec28bb9cSChintan Pandya 	pmd_free(NULL, table);
1427ec28bb9cSChintan Pandya 	return 1;
1428b6bdb751SToshi Kani }
14294ab21506SRobin Murphy 
14308e2d4340SWill Deacon int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
14318e2d4340SWill Deacon {
14328e2d4340SWill Deacon 	return 0;	/* Don't attempt a block mapping */
14338e2d4340SWill Deacon }
14348e2d4340SWill Deacon 
14354ab21506SRobin Murphy #ifdef CONFIG_MEMORY_HOTPLUG
1436bbd6ec60SAnshuman Khandual static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
1437bbd6ec60SAnshuman Khandual {
1438bbd6ec60SAnshuman Khandual 	unsigned long end = start + size;
1439bbd6ec60SAnshuman Khandual 
1440bbd6ec60SAnshuman Khandual 	WARN_ON(pgdir != init_mm.pgd);
1441bbd6ec60SAnshuman Khandual 	WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
1442bbd6ec60SAnshuman Khandual 
1443eee07935SAnshuman Khandual 	unmap_hotplug_range(start, end, false, NULL);
1444bbd6ec60SAnshuman Khandual 	free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
1445bbd6ec60SAnshuman Khandual }
1446bbd6ec60SAnshuman Khandual 
144703aaf83fSAnshuman Khandual struct range arch_get_mappable_range(void)
144858284a90SAnshuman Khandual {
144903aaf83fSAnshuman Khandual 	struct range mhp_range;
145003aaf83fSAnshuman Khandual 
145158284a90SAnshuman Khandual 	/*
145258284a90SAnshuman Khandual 	 * Linear mapping region is the range [PAGE_OFFSET..(PAGE_END - 1)]
145358284a90SAnshuman Khandual 	 * accommodating both its ends but excluding PAGE_END. Max physical
145458284a90SAnshuman Khandual 	 * range which can be mapped inside this linear mapping range, must
145558284a90SAnshuman Khandual 	 * also be derived from its end points.
145658284a90SAnshuman Khandual 	 */
145703aaf83fSAnshuman Khandual 	mhp_range.start = __pa(_PAGE_OFFSET(vabits_actual));
145803aaf83fSAnshuman Khandual 	mhp_range.end =  __pa(PAGE_END - 1);
145903aaf83fSAnshuman Khandual 	return mhp_range;
146058284a90SAnshuman Khandual }
146158284a90SAnshuman Khandual 
1462940519f0SMichal Hocko int arch_add_memory(int nid, u64 start, u64 size,
1463f5637d3bSLogan Gunthorpe 		    struct mhp_params *params)
14644ab21506SRobin Murphy {
1465bbd6ec60SAnshuman Khandual 	int ret, flags = 0;
14664ab21506SRobin Murphy 
146703aaf83fSAnshuman Khandual 	VM_BUG_ON(!mhp_range_allowed(start, size, true));
1468*840b2398SMarco Elver 
1469*840b2398SMarco Elver 	/*
1470*840b2398SMarco Elver 	 * KFENCE requires linear map to be mapped at page granularity, so that
1471*840b2398SMarco Elver 	 * it is possible to protect/unprotect single pages in the KFENCE pool.
1472*840b2398SMarco Elver 	 */
1473*840b2398SMarco Elver 	if (rodata_full || debug_pagealloc_enabled() ||
1474*840b2398SMarco Elver 	    IS_ENABLED(CONFIG_KFENCE))
14754ab21506SRobin Murphy 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
14764ab21506SRobin Murphy 
14774ab21506SRobin Murphy 	__create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
1478bfeb022fSLogan Gunthorpe 			     size, params->pgprot, __pgd_pgtable_alloc,
1479bfeb022fSLogan Gunthorpe 			     flags);
14804ab21506SRobin Murphy 
148116993c0fSDan Williams 	memblock_clear_nomap(start, size);
148216993c0fSDan Williams 
1483bbd6ec60SAnshuman Khandual 	ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
1484f5637d3bSLogan Gunthorpe 			   params);
1485bbd6ec60SAnshuman Khandual 	if (ret)
1486bbd6ec60SAnshuman Khandual 		__remove_pgd_mapping(swapper_pg_dir,
1487bbd6ec60SAnshuman Khandual 				     __phys_to_virt(start), size);
1488bbd6ec60SAnshuman Khandual 	return ret;
14894ab21506SRobin Murphy }
1490bbd6ec60SAnshuman Khandual 
149122eb6346SDavid Hildenbrand void arch_remove_memory(int nid, u64 start, u64 size,
149222eb6346SDavid Hildenbrand 			struct vmem_altmap *altmap)
149322eb6346SDavid Hildenbrand {
149422eb6346SDavid Hildenbrand 	unsigned long start_pfn = start >> PAGE_SHIFT;
149522eb6346SDavid Hildenbrand 	unsigned long nr_pages = size >> PAGE_SHIFT;
149622eb6346SDavid Hildenbrand 
1497feee6b29SDavid Hildenbrand 	__remove_pages(start_pfn, nr_pages, altmap);
1498bbd6ec60SAnshuman Khandual 	__remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
149922eb6346SDavid Hildenbrand }
1500bbd6ec60SAnshuman Khandual 
1501bbd6ec60SAnshuman Khandual /*
1502bbd6ec60SAnshuman Khandual  * This memory hotplug notifier helps prevent boot memory from being
1503bbd6ec60SAnshuman Khandual  * inadvertently removed as it blocks pfn range offlining process in
1504bbd6ec60SAnshuman Khandual  * __offline_pages(). Hence this prevents both offlining as well as
1505bbd6ec60SAnshuman Khandual  * removal process for boot memory which is initially always online.
1506bbd6ec60SAnshuman Khandual  * In future if and when boot memory could be removed, this notifier
1507bbd6ec60SAnshuman Khandual  * should be dropped and free_hotplug_page_range() should handle any
1508bbd6ec60SAnshuman Khandual  * reserved pages allocated during boot.
1509bbd6ec60SAnshuman Khandual  */
1510bbd6ec60SAnshuman Khandual static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
1511bbd6ec60SAnshuman Khandual 					   unsigned long action, void *data)
1512bbd6ec60SAnshuman Khandual {
1513bbd6ec60SAnshuman Khandual 	struct mem_section *ms;
1514bbd6ec60SAnshuman Khandual 	struct memory_notify *arg = data;
1515bbd6ec60SAnshuman Khandual 	unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
1516bbd6ec60SAnshuman Khandual 	unsigned long pfn = arg->start_pfn;
1517bbd6ec60SAnshuman Khandual 
15189fb3d4a3SAnshuman Khandual 	if ((action != MEM_GOING_OFFLINE) && (action != MEM_OFFLINE))
1519bbd6ec60SAnshuman Khandual 		return NOTIFY_OK;
1520bbd6ec60SAnshuman Khandual 
1521bbd6ec60SAnshuman Khandual 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
15229fb3d4a3SAnshuman Khandual 		unsigned long start = PFN_PHYS(pfn);
15239fb3d4a3SAnshuman Khandual 		unsigned long end = start + (1UL << PA_SECTION_SHIFT);
15249fb3d4a3SAnshuman Khandual 
1525bbd6ec60SAnshuman Khandual 		ms = __pfn_to_section(pfn);
15269fb3d4a3SAnshuman Khandual 		if (!early_section(ms))
15279fb3d4a3SAnshuman Khandual 			continue;
15289fb3d4a3SAnshuman Khandual 
15299fb3d4a3SAnshuman Khandual 		if (action == MEM_GOING_OFFLINE) {
15309fb3d4a3SAnshuman Khandual 			/*
15319fb3d4a3SAnshuman Khandual 			 * Boot memory removal is not supported. Prevent
15329fb3d4a3SAnshuman Khandual 			 * it via blocking any attempted offline request
15339fb3d4a3SAnshuman Khandual 			 * for the boot memory and just report it.
15349fb3d4a3SAnshuman Khandual 			 */
15359fb3d4a3SAnshuman Khandual 			pr_warn("Boot memory [%lx %lx] offlining attempted\n", start, end);
1536bbd6ec60SAnshuman Khandual 			return NOTIFY_BAD;
15379fb3d4a3SAnshuman Khandual 		} else if (action == MEM_OFFLINE) {
15389fb3d4a3SAnshuman Khandual 			/*
15399fb3d4a3SAnshuman Khandual 			 * This should have never happened. Boot memory
15409fb3d4a3SAnshuman Khandual 			 * offlining should have been prevented by this
15419fb3d4a3SAnshuman Khandual 			 * very notifier. Probably some memory removal
15429fb3d4a3SAnshuman Khandual 			 * procedure might have changed which would then
15439fb3d4a3SAnshuman Khandual 			 * require further debug.
15449fb3d4a3SAnshuman Khandual 			 */
15459fb3d4a3SAnshuman Khandual 			pr_err("Boot memory [%lx %lx] offlined\n", start, end);
15469fb3d4a3SAnshuman Khandual 
15479fb3d4a3SAnshuman Khandual 			/*
15489fb3d4a3SAnshuman Khandual 			 * Core memory hotplug does not process a return
15499fb3d4a3SAnshuman Khandual 			 * code from the notifier for MEM_OFFLINE events.
15509fb3d4a3SAnshuman Khandual 			 * The error condition has been reported. Return
15519fb3d4a3SAnshuman Khandual 			 * from here as if ignored.
15529fb3d4a3SAnshuman Khandual 			 */
15539fb3d4a3SAnshuman Khandual 			return NOTIFY_DONE;
15549fb3d4a3SAnshuman Khandual 		}
1555bbd6ec60SAnshuman Khandual 	}
1556bbd6ec60SAnshuman Khandual 	return NOTIFY_OK;
1557bbd6ec60SAnshuman Khandual }
1558bbd6ec60SAnshuman Khandual 
1559bbd6ec60SAnshuman Khandual static struct notifier_block prevent_bootmem_remove_nb = {
1560bbd6ec60SAnshuman Khandual 	.notifier_call = prevent_bootmem_remove_notifier,
1561bbd6ec60SAnshuman Khandual };
1562bbd6ec60SAnshuman Khandual 
1563fdd99a41SAnshuman Khandual /*
1564fdd99a41SAnshuman Khandual  * This ensures that boot memory sections on the platform are online
1565fdd99a41SAnshuman Khandual  * from early boot. Memory sections could not be prevented from being
1566fdd99a41SAnshuman Khandual  * offlined, unless for some reason they are not online to begin with.
1567fdd99a41SAnshuman Khandual  * This helps validate the basic assumption on which the above memory
1568fdd99a41SAnshuman Khandual  * event notifier works to prevent boot memory section offlining and
1569fdd99a41SAnshuman Khandual  * its possible removal.
1570fdd99a41SAnshuman Khandual  */
1571fdd99a41SAnshuman Khandual static void validate_bootmem_online(void)
1572fdd99a41SAnshuman Khandual {
1573fdd99a41SAnshuman Khandual 	phys_addr_t start, end, addr;
1574fdd99a41SAnshuman Khandual 	struct mem_section *ms;
1575fdd99a41SAnshuman Khandual 	u64 i;
1576fdd99a41SAnshuman Khandual 
1577fdd99a41SAnshuman Khandual 	/*
1578fdd99a41SAnshuman Khandual 	 * Scanning across all memblock might be expensive
1579fdd99a41SAnshuman Khandual 	 * on some big memory systems. Hence enable this
1580fdd99a41SAnshuman Khandual 	 * validation only with DEBUG_VM.
1581fdd99a41SAnshuman Khandual 	 */
1582fdd99a41SAnshuman Khandual 	if (!IS_ENABLED(CONFIG_DEBUG_VM))
1583fdd99a41SAnshuman Khandual 		return;
1584fdd99a41SAnshuman Khandual 
1585fdd99a41SAnshuman Khandual 	for_each_mem_range(i, &start, &end) {
1586fdd99a41SAnshuman Khandual 		for (addr = start; addr < end; addr += (1UL << PA_SECTION_SHIFT)) {
1587fdd99a41SAnshuman Khandual 			ms = __pfn_to_section(PHYS_PFN(addr));
1588fdd99a41SAnshuman Khandual 
1589fdd99a41SAnshuman Khandual 			/*
1590fdd99a41SAnshuman Khandual 			 * All memory ranges in the system at this point
1591fdd99a41SAnshuman Khandual 			 * should have been marked as early sections.
1592fdd99a41SAnshuman Khandual 			 */
1593fdd99a41SAnshuman Khandual 			WARN_ON(!early_section(ms));
1594fdd99a41SAnshuman Khandual 
1595fdd99a41SAnshuman Khandual 			/*
1596fdd99a41SAnshuman Khandual 			 * Memory notifier mechanism here to prevent boot
1597fdd99a41SAnshuman Khandual 			 * memory offlining depends on the fact that each
1598fdd99a41SAnshuman Khandual 			 * early section memory on the system is initially
1599fdd99a41SAnshuman Khandual 			 * online. Otherwise a given memory section which
1600fdd99a41SAnshuman Khandual 			 * is already offline will be overlooked and can
1601fdd99a41SAnshuman Khandual 			 * be removed completely. Call out such sections.
1602fdd99a41SAnshuman Khandual 			 */
1603fdd99a41SAnshuman Khandual 			if (!online_section(ms))
1604fdd99a41SAnshuman Khandual 				pr_err("Boot memory [%llx %llx] is offline, can be removed\n",
1605fdd99a41SAnshuman Khandual 					addr, addr + (1UL << PA_SECTION_SHIFT));
1606fdd99a41SAnshuman Khandual 		}
1607fdd99a41SAnshuman Khandual 	}
1608fdd99a41SAnshuman Khandual }
1609fdd99a41SAnshuman Khandual 
1610bbd6ec60SAnshuman Khandual static int __init prevent_bootmem_remove_init(void)
1611bbd6ec60SAnshuman Khandual {
1612cb45babeSAnshuman Khandual 	int ret = 0;
1613cb45babeSAnshuman Khandual 
1614cb45babeSAnshuman Khandual 	if (!IS_ENABLED(CONFIG_MEMORY_HOTREMOVE))
1615cb45babeSAnshuman Khandual 		return ret;
1616cb45babeSAnshuman Khandual 
1617fdd99a41SAnshuman Khandual 	validate_bootmem_online();
1618cb45babeSAnshuman Khandual 	ret = register_memory_notifier(&prevent_bootmem_remove_nb);
1619cb45babeSAnshuman Khandual 	if (ret)
1620cb45babeSAnshuman Khandual 		pr_err("%s: Notifier registration failed %d\n", __func__, ret);
1621cb45babeSAnshuman Khandual 
1622cb45babeSAnshuman Khandual 	return ret;
1623bbd6ec60SAnshuman Khandual }
1624cb45babeSAnshuman Khandual early_initcall(prevent_bootmem_remove_init);
162522eb6346SDavid Hildenbrand #endif
1626