xref: /openbmc/linux/arch/arm64/mm/mmu.c (revision 8dd4daa0)
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>
38c1cc1552SCatalin Marinas 
39c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS	BIT(0)
40d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS	BIT(1)
41c0951366SArd Biesheuvel 
42dd006da2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
43fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
44dd006da2SArd Biesheuvel 
455383cc6eSSteve Capper u64 __section(".mmuoff.data.write") vabits_actual;
465383cc6eSSteve Capper EXPORT_SYMBOL(vabits_actual);
47c1cc1552SCatalin Marinas 
485a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init;
49a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset);
50a7f8de16SArd Biesheuvel 
51c1cc1552SCatalin Marinas /*
52c1cc1552SCatalin Marinas  * Empty_zero_page is a special page that is used for zero-initialized data
53c1cc1552SCatalin Marinas  * and COW.
54c1cc1552SCatalin Marinas  */
555227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
56c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page);
57c1cc1552SCatalin Marinas 
58f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
59f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
60f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
61f9040773SArd Biesheuvel 
622330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock);
632330b7caSJun Yao 
642330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
652330b7caSJun Yao {
662330b7caSJun Yao 	pgd_t *fixmap_pgdp;
672330b7caSJun Yao 
682330b7caSJun Yao 	spin_lock(&swapper_pgdir_lock);
6926a6f87eSJames Morse 	fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
702330b7caSJun Yao 	WRITE_ONCE(*fixmap_pgdp, pgd);
712330b7caSJun Yao 	/*
722330b7caSJun Yao 	 * We need dsb(ishst) here to ensure the page-table-walker sees
732330b7caSJun Yao 	 * our new entry before set_p?d() returns. The fixmap's
742330b7caSJun Yao 	 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
752330b7caSJun Yao 	 */
762330b7caSJun Yao 	pgd_clear_fixmap();
772330b7caSJun Yao 	spin_unlock(&swapper_pgdir_lock);
782330b7caSJun Yao }
792330b7caSJun Yao 
80c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
81c1cc1552SCatalin Marinas 			      unsigned long size, pgprot_t vma_prot)
82c1cc1552SCatalin Marinas {
83c1cc1552SCatalin Marinas 	if (!pfn_valid(pfn))
84c1cc1552SCatalin Marinas 		return pgprot_noncached(vma_prot);
85c1cc1552SCatalin Marinas 	else if (file->f_flags & O_SYNC)
86c1cc1552SCatalin Marinas 		return pgprot_writecombine(vma_prot);
87c1cc1552SCatalin Marinas 	return vma_prot;
88c1cc1552SCatalin Marinas }
89c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot);
90c1cc1552SCatalin Marinas 
9190292acaSYu Zhao static phys_addr_t __init early_pgtable_alloc(int shift)
92c1cc1552SCatalin Marinas {
937142392dSSuzuki K. Poulose 	phys_addr_t phys;
947142392dSSuzuki K. Poulose 	void *ptr;
957142392dSSuzuki K. Poulose 
969a8dd708SMike Rapoport 	phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
97ecc3e771SMike Rapoport 	if (!phys)
98ecc3e771SMike Rapoport 		panic("Failed to allocate page table page\n");
99f4710445SMark Rutland 
100f4710445SMark Rutland 	/*
101f4710445SMark Rutland 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
102f4710445SMark Rutland 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
103f4710445SMark Rutland 	 * any level of table.
104f4710445SMark Rutland 	 */
105f4710445SMark Rutland 	ptr = pte_set_fixmap(phys);
106f4710445SMark Rutland 
10721ab99c2SMark Rutland 	memset(ptr, 0, PAGE_SIZE);
10821ab99c2SMark Rutland 
109f4710445SMark Rutland 	/*
110f4710445SMark Rutland 	 * Implicit barriers also ensure the zeroed page is visible to the page
111f4710445SMark Rutland 	 * table walker
112f4710445SMark Rutland 	 */
113f4710445SMark Rutland 	pte_clear_fixmap();
114f4710445SMark Rutland 
115f4710445SMark Rutland 	return phys;
116c1cc1552SCatalin Marinas }
117c1cc1552SCatalin Marinas 
118e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new)
119e98216b5SArd Biesheuvel {
120e98216b5SArd Biesheuvel 	/*
121e98216b5SArd Biesheuvel 	 * The following mapping attributes may be updated in live
122e98216b5SArd Biesheuvel 	 * kernel mappings without the need for break-before-make.
123e98216b5SArd Biesheuvel 	 */
124753e8abcSArd Biesheuvel 	static const pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
125e98216b5SArd Biesheuvel 
126141d1497SArd Biesheuvel 	/* creating or taking down mappings is always safe */
127141d1497SArd Biesheuvel 	if (old == 0 || new == 0)
128141d1497SArd Biesheuvel 		return true;
129141d1497SArd Biesheuvel 
130141d1497SArd Biesheuvel 	/* live contiguous mappings may not be manipulated at all */
131141d1497SArd Biesheuvel 	if ((old | new) & PTE_CONT)
132141d1497SArd Biesheuvel 		return false;
133141d1497SArd Biesheuvel 
134753e8abcSArd Biesheuvel 	/* Transitioning from Non-Global to Global is unsafe */
135753e8abcSArd Biesheuvel 	if (old & ~new & PTE_NG)
136753e8abcSArd Biesheuvel 		return false;
1374e602056SWill Deacon 
138141d1497SArd Biesheuvel 	return ((old ^ new) & ~mask) == 0;
139e98216b5SArd Biesheuvel }
140e98216b5SArd Biesheuvel 
14120a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
142d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot)
143c1cc1552SCatalin Marinas {
14420a004e7SWill Deacon 	pte_t *ptep;
145c1cc1552SCatalin Marinas 
14620a004e7SWill Deacon 	ptep = pte_set_fixmap_offset(pmdp, addr);
147c1cc1552SCatalin Marinas 	do {
14820a004e7SWill Deacon 		pte_t old_pte = READ_ONCE(*ptep);
149e98216b5SArd Biesheuvel 
15020a004e7SWill Deacon 		set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
151e98216b5SArd Biesheuvel 
152e98216b5SArd Biesheuvel 		/*
153e98216b5SArd Biesheuvel 		 * After the PTE entry has been populated once, we
154e98216b5SArd Biesheuvel 		 * only allow updates to the permission attributes.
155e98216b5SArd Biesheuvel 		 */
15620a004e7SWill Deacon 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
15720a004e7SWill Deacon 					      READ_ONCE(pte_val(*ptep))));
158e98216b5SArd Biesheuvel 
159e393cf40SArd Biesheuvel 		phys += PAGE_SIZE;
16020a004e7SWill Deacon 	} while (ptep++, addr += PAGE_SIZE, addr != end);
161f4710445SMark Rutland 
162f4710445SMark Rutland 	pte_clear_fixmap();
163c1cc1552SCatalin Marinas }
164c1cc1552SCatalin Marinas 
16520a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
166d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
167d27cfa1fSArd Biesheuvel 				pgprot_t prot,
16890292acaSYu Zhao 				phys_addr_t (*pgtable_alloc)(int),
169c0951366SArd Biesheuvel 				int flags)
170c1cc1552SCatalin Marinas {
171c1cc1552SCatalin Marinas 	unsigned long next;
17220a004e7SWill Deacon 	pmd_t pmd = READ_ONCE(*pmdp);
173c1cc1552SCatalin Marinas 
17420a004e7SWill Deacon 	BUG_ON(pmd_sect(pmd));
17520a004e7SWill Deacon 	if (pmd_none(pmd)) {
176d27cfa1fSArd Biesheuvel 		phys_addr_t pte_phys;
177132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
17890292acaSYu Zhao 		pte_phys = pgtable_alloc(PAGE_SHIFT);
17920a004e7SWill Deacon 		__pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
18020a004e7SWill Deacon 		pmd = READ_ONCE(*pmdp);
181c1cc1552SCatalin Marinas 	}
18220a004e7SWill Deacon 	BUG_ON(pmd_bad(pmd));
183d27cfa1fSArd Biesheuvel 
184d27cfa1fSArd Biesheuvel 	do {
185d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
186d27cfa1fSArd Biesheuvel 
187d27cfa1fSArd Biesheuvel 		next = pte_cont_addr_end(addr, end);
188d27cfa1fSArd Biesheuvel 
189d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
190d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
191d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
192d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
193d27cfa1fSArd Biesheuvel 
19420a004e7SWill Deacon 		init_pte(pmdp, addr, next, phys, __prot);
195d27cfa1fSArd Biesheuvel 
196d27cfa1fSArd Biesheuvel 		phys += next - addr;
197d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
198d27cfa1fSArd Biesheuvel }
199d27cfa1fSArd Biesheuvel 
20020a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
201d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot,
20290292acaSYu Zhao 		     phys_addr_t (*pgtable_alloc)(int), int flags)
203d27cfa1fSArd Biesheuvel {
204d27cfa1fSArd Biesheuvel 	unsigned long next;
20520a004e7SWill Deacon 	pmd_t *pmdp;
206c1cc1552SCatalin Marinas 
20720a004e7SWill Deacon 	pmdp = pmd_set_fixmap_offset(pudp, addr);
208c1cc1552SCatalin Marinas 	do {
20920a004e7SWill Deacon 		pmd_t old_pmd = READ_ONCE(*pmdp);
210e98216b5SArd Biesheuvel 
211c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
212e98216b5SArd Biesheuvel 
213c1cc1552SCatalin Marinas 		/* try section mapping first */
21483863f25SLaura Abbott 		if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
215c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
21620a004e7SWill Deacon 			pmd_set_huge(pmdp, phys, prot);
217e98216b5SArd Biesheuvel 
218a55f9929SCatalin Marinas 			/*
219e98216b5SArd Biesheuvel 			 * After the PMD entry has been populated once, we
220e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
221a55f9929SCatalin Marinas 			 */
222e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
22320a004e7SWill Deacon 						      READ_ONCE(pmd_val(*pmdp))));
224a55f9929SCatalin Marinas 		} else {
22520a004e7SWill Deacon 			alloc_init_cont_pte(pmdp, addr, next, phys, prot,
226d27cfa1fSArd Biesheuvel 					    pgtable_alloc, flags);
227e98216b5SArd Biesheuvel 
228e98216b5SArd Biesheuvel 			BUG_ON(pmd_val(old_pmd) != 0 &&
22920a004e7SWill Deacon 			       pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
230a55f9929SCatalin Marinas 		}
231c1cc1552SCatalin Marinas 		phys += next - addr;
23220a004e7SWill Deacon 	} while (pmdp++, addr = next, addr != end);
233f4710445SMark Rutland 
234f4710445SMark Rutland 	pmd_clear_fixmap();
235c1cc1552SCatalin Marinas }
236c1cc1552SCatalin Marinas 
23720a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
238d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
239d27cfa1fSArd Biesheuvel 				pgprot_t prot,
24090292acaSYu Zhao 				phys_addr_t (*pgtable_alloc)(int), int flags)
241d27cfa1fSArd Biesheuvel {
242d27cfa1fSArd Biesheuvel 	unsigned long next;
24320a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
244d27cfa1fSArd Biesheuvel 
245d27cfa1fSArd Biesheuvel 	/*
246d27cfa1fSArd Biesheuvel 	 * Check for initial section mappings in the pgd/pud.
247d27cfa1fSArd Biesheuvel 	 */
24820a004e7SWill Deacon 	BUG_ON(pud_sect(pud));
24920a004e7SWill Deacon 	if (pud_none(pud)) {
250d27cfa1fSArd Biesheuvel 		phys_addr_t pmd_phys;
251d27cfa1fSArd Biesheuvel 		BUG_ON(!pgtable_alloc);
25290292acaSYu Zhao 		pmd_phys = pgtable_alloc(PMD_SHIFT);
25320a004e7SWill Deacon 		__pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
25420a004e7SWill Deacon 		pud = READ_ONCE(*pudp);
255d27cfa1fSArd Biesheuvel 	}
25620a004e7SWill Deacon 	BUG_ON(pud_bad(pud));
257d27cfa1fSArd Biesheuvel 
258d27cfa1fSArd Biesheuvel 	do {
259d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
260d27cfa1fSArd Biesheuvel 
261d27cfa1fSArd Biesheuvel 		next = pmd_cont_addr_end(addr, end);
262d27cfa1fSArd Biesheuvel 
263d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
264d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
265d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
266d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
267d27cfa1fSArd Biesheuvel 
26820a004e7SWill Deacon 		init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
269d27cfa1fSArd Biesheuvel 
270d27cfa1fSArd Biesheuvel 		phys += next - addr;
271d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
272d27cfa1fSArd Biesheuvel }
273d27cfa1fSArd Biesheuvel 
274da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next,
275da141706SLaura Abbott 			unsigned long phys)
276da141706SLaura Abbott {
277da141706SLaura Abbott 	if (PAGE_SHIFT != 12)
278da141706SLaura Abbott 		return false;
279da141706SLaura Abbott 
280da141706SLaura Abbott 	if (((addr | next | phys) & ~PUD_MASK) != 0)
281da141706SLaura Abbott 		return false;
282da141706SLaura Abbott 
283da141706SLaura Abbott 	return true;
284da141706SLaura Abbott }
285da141706SLaura Abbott 
28620a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
287da141706SLaura Abbott 			   phys_addr_t phys, pgprot_t prot,
28890292acaSYu Zhao 			   phys_addr_t (*pgtable_alloc)(int),
289c0951366SArd Biesheuvel 			   int flags)
290c1cc1552SCatalin Marinas {
291c1cc1552SCatalin Marinas 	unsigned long next;
29220a004e7SWill Deacon 	pud_t *pudp;
293e9f63768SMike Rapoport 	p4d_t *p4dp = p4d_offset(pgdp, addr);
294e9f63768SMike Rapoport 	p4d_t p4d = READ_ONCE(*p4dp);
295c1cc1552SCatalin Marinas 
296e9f63768SMike Rapoport 	if (p4d_none(p4d)) {
297132233a7SLaura Abbott 		phys_addr_t pud_phys;
298132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
29990292acaSYu Zhao 		pud_phys = pgtable_alloc(PUD_SHIFT);
300e9f63768SMike Rapoport 		__p4d_populate(p4dp, pud_phys, PUD_TYPE_TABLE);
301e9f63768SMike Rapoport 		p4d = READ_ONCE(*p4dp);
302c79b954bSJungseok Lee 	}
303e9f63768SMike Rapoport 	BUG_ON(p4d_bad(p4d));
304c79b954bSJungseok Lee 
305e9f63768SMike Rapoport 	pudp = pud_set_fixmap_offset(p4dp, addr);
306c1cc1552SCatalin Marinas 	do {
30720a004e7SWill Deacon 		pud_t old_pud = READ_ONCE(*pudp);
308e98216b5SArd Biesheuvel 
309c1cc1552SCatalin Marinas 		next = pud_addr_end(addr, end);
310206a2a73SSteve Capper 
311206a2a73SSteve Capper 		/*
312206a2a73SSteve Capper 		 * For 4K granule only, attempt to put down a 1GB block
313206a2a73SSteve Capper 		 */
314c0951366SArd Biesheuvel 		if (use_1G_block(addr, next, phys) &&
315c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
31620a004e7SWill Deacon 			pud_set_huge(pudp, phys, prot);
317206a2a73SSteve Capper 
318206a2a73SSteve Capper 			/*
319e98216b5SArd Biesheuvel 			 * After the PUD entry has been populated once, we
320e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
321206a2a73SSteve Capper 			 */
322e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
32320a004e7SWill Deacon 						      READ_ONCE(pud_val(*pudp))));
324206a2a73SSteve Capper 		} else {
32520a004e7SWill Deacon 			alloc_init_cont_pmd(pudp, addr, next, phys, prot,
326c0951366SArd Biesheuvel 					    pgtable_alloc, flags);
327e98216b5SArd Biesheuvel 
328e98216b5SArd Biesheuvel 			BUG_ON(pud_val(old_pud) != 0 &&
32920a004e7SWill Deacon 			       pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
330206a2a73SSteve Capper 		}
331c1cc1552SCatalin Marinas 		phys += next - addr;
33220a004e7SWill Deacon 	} while (pudp++, addr = next, addr != end);
333f4710445SMark Rutland 
334f4710445SMark Rutland 	pud_clear_fixmap();
335c1cc1552SCatalin Marinas }
336c1cc1552SCatalin Marinas 
33740f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
33840f87d31SArd Biesheuvel 				 unsigned long virt, phys_addr_t size,
33940f87d31SArd Biesheuvel 				 pgprot_t prot,
34090292acaSYu Zhao 				 phys_addr_t (*pgtable_alloc)(int),
341c0951366SArd Biesheuvel 				 int flags)
342c1cc1552SCatalin Marinas {
34332d18708SMasahiro Yamada 	unsigned long addr, end, next;
344974b9b2cSMike Rapoport 	pgd_t *pgdp = pgd_offset_pgd(pgdir, virt);
345c1cc1552SCatalin Marinas 
346cc5d2b3bSMark Rutland 	/*
347cc5d2b3bSMark Rutland 	 * If the virtual and physical address don't have the same offset
348cc5d2b3bSMark Rutland 	 * within a page, we cannot map the region as the caller expects.
349cc5d2b3bSMark Rutland 	 */
350cc5d2b3bSMark Rutland 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
351cc5d2b3bSMark Rutland 		return;
352cc5d2b3bSMark Rutland 
3539c4e08a3SMark Rutland 	phys &= PAGE_MASK;
354c1cc1552SCatalin Marinas 	addr = virt & PAGE_MASK;
35532d18708SMasahiro Yamada 	end = PAGE_ALIGN(virt + size);
356c1cc1552SCatalin Marinas 
357c1cc1552SCatalin Marinas 	do {
358c1cc1552SCatalin Marinas 		next = pgd_addr_end(addr, end);
35920a004e7SWill Deacon 		alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
360c0951366SArd Biesheuvel 			       flags);
361c1cc1552SCatalin Marinas 		phys += next - addr;
36220a004e7SWill Deacon 	} while (pgdp++, addr = next, addr != end);
363c1cc1552SCatalin Marinas }
364c1cc1552SCatalin Marinas 
365475ba3fcSWill Deacon static phys_addr_t __pgd_pgtable_alloc(int shift)
366369aaab8SYu Zhao {
36750f11a8aSMike Rapoport 	void *ptr = (void *)__get_free_page(GFP_PGTABLE_KERNEL);
368369aaab8SYu Zhao 	BUG_ON(!ptr);
369369aaab8SYu Zhao 
370369aaab8SYu Zhao 	/* Ensure the zeroed page is visible to the page table walker */
371369aaab8SYu Zhao 	dsb(ishst);
372369aaab8SYu Zhao 	return __pa(ptr);
373369aaab8SYu Zhao }
374369aaab8SYu Zhao 
37590292acaSYu Zhao static phys_addr_t pgd_pgtable_alloc(int shift)
376da141706SLaura Abbott {
377475ba3fcSWill Deacon 	phys_addr_t pa = __pgd_pgtable_alloc(shift);
37890292acaSYu Zhao 
37990292acaSYu Zhao 	/*
38090292acaSYu Zhao 	 * Call proper page table ctor in case later we need to
38190292acaSYu Zhao 	 * call core mm functions like apply_to_page_range() on
38290292acaSYu Zhao 	 * this pre-allocated page table.
38390292acaSYu Zhao 	 *
38490292acaSYu Zhao 	 * We don't select ARCH_ENABLE_SPLIT_PMD_PTLOCK if pmd is
38590292acaSYu Zhao 	 * folded, and if so pgtable_pmd_page_ctor() becomes nop.
38690292acaSYu Zhao 	 */
38790292acaSYu Zhao 	if (shift == PAGE_SHIFT)
388b4ed71f5SMark Rutland 		BUG_ON(!pgtable_pte_page_ctor(phys_to_page(pa)));
38990292acaSYu Zhao 	else if (shift == PMD_SHIFT)
390475ba3fcSWill Deacon 		BUG_ON(!pgtable_pmd_page_ctor(phys_to_page(pa)));
39121ab99c2SMark Rutland 
392475ba3fcSWill Deacon 	return pa;
393da141706SLaura Abbott }
394da141706SLaura Abbott 
395132233a7SLaura Abbott /*
396132233a7SLaura Abbott  * This function can only be used to modify existing table entries,
397132233a7SLaura Abbott  * without allocating new levels of table. Note that this permits the
398132233a7SLaura Abbott  * creation of new section or page entries.
399132233a7SLaura Abbott  */
400132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
401da141706SLaura Abbott 				  phys_addr_t size, pgprot_t prot)
402d7ecbddfSMark Salter {
40377ad4ce6SMark Rutland 	if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
404d7ecbddfSMark Salter 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
405d7ecbddfSMark Salter 			&phys, virt);
406d7ecbddfSMark Salter 		return;
407d7ecbddfSMark Salter 	}
408d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
409d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
410d7ecbddfSMark Salter }
411d7ecbddfSMark Salter 
4128ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
4138ce837ceSArd Biesheuvel 			       unsigned long virt, phys_addr_t size,
414f14c66ceSArd Biesheuvel 			       pgprot_t prot, bool page_mappings_only)
4158ce837ceSArd Biesheuvel {
416c0951366SArd Biesheuvel 	int flags = 0;
417c0951366SArd Biesheuvel 
4181378dc3dSArd Biesheuvel 	BUG_ON(mm == &init_mm);
4191378dc3dSArd Biesheuvel 
420c0951366SArd Biesheuvel 	if (page_mappings_only)
421d27cfa1fSArd Biesheuvel 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
422c0951366SArd Biesheuvel 
42311509a30SMark Rutland 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
424c0951366SArd Biesheuvel 			     pgd_pgtable_alloc, flags);
425d7ecbddfSMark Salter }
426d7ecbddfSMark Salter 
427aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
428da141706SLaura Abbott 				phys_addr_t size, pgprot_t prot)
429da141706SLaura Abbott {
43077ad4ce6SMark Rutland 	if ((virt >= PAGE_END) && (virt < VMALLOC_START)) {
431aa8c09beSArd Biesheuvel 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
432da141706SLaura Abbott 			&phys, virt);
433da141706SLaura Abbott 		return;
434da141706SLaura Abbott 	}
435da141706SLaura Abbott 
436d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
437d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
438aa8c09beSArd Biesheuvel 
439aa8c09beSArd Biesheuvel 	/* flush the TLBs after updating live kernel mappings */
440aa8c09beSArd Biesheuvel 	flush_tlb_kernel_range(virt, virt + size);
441da141706SLaura Abbott }
442da141706SLaura Abbott 
44320a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
44498d2e153STakahiro Akashi 				  phys_addr_t end, pgprot_t prot, int flags)
445da141706SLaura Abbott {
44620a004e7SWill Deacon 	__create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
44798d2e153STakahiro Akashi 			     prot, early_pgtable_alloc, flags);
448da141706SLaura Abbott }
449da141706SLaura Abbott 
4505ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void)
4515ea5306cSArd Biesheuvel {
4525ea5306cSArd Biesheuvel 	/*
4535ea5306cSArd Biesheuvel 	 * Remove the write permissions from the linear alias of .text/.rodata
4545ea5306cSArd Biesheuvel 	 */
4555ea5306cSArd Biesheuvel 	update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
4565ea5306cSArd Biesheuvel 			    (unsigned long)__init_begin - (unsigned long)_text,
4575ea5306cSArd Biesheuvel 			    PAGE_KERNEL_RO);
4585ea5306cSArd Biesheuvel }
4595ea5306cSArd Biesheuvel 
46020a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp)
461c1cc1552SCatalin Marinas {
46298d2e153STakahiro Akashi 	phys_addr_t kernel_start = __pa_symbol(_text);
46398d2e153STakahiro Akashi 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
464c1cc1552SCatalin Marinas 	struct memblock_region *reg;
46598d2e153STakahiro Akashi 	int flags = 0;
46698d2e153STakahiro Akashi 
467c55191e9SArd Biesheuvel 	if (rodata_full || debug_pagealloc_enabled())
46898d2e153STakahiro Akashi 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
46998d2e153STakahiro Akashi 
47098d2e153STakahiro Akashi 	/*
47198d2e153STakahiro Akashi 	 * Take care not to create a writable alias for the
47298d2e153STakahiro Akashi 	 * read-only text and rodata sections of the kernel image.
47398d2e153STakahiro Akashi 	 * So temporarily mark them as NOMAP to skip mappings in
47498d2e153STakahiro Akashi 	 * the following for-loop
47598d2e153STakahiro Akashi 	 */
47698d2e153STakahiro Akashi 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
47798d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
47898d2e153STakahiro Akashi 	if (crashk_res.end)
47998d2e153STakahiro Akashi 		memblock_mark_nomap(crashk_res.start,
48098d2e153STakahiro Akashi 				    resource_size(&crashk_res));
48198d2e153STakahiro Akashi #endif
482f6bc87c3SSteve Capper 
483c1cc1552SCatalin Marinas 	/* map all the memory banks */
484c1cc1552SCatalin Marinas 	for_each_memblock(memory, reg) {
485c1cc1552SCatalin Marinas 		phys_addr_t start = reg->base;
486c1cc1552SCatalin Marinas 		phys_addr_t end = start + reg->size;
487c1cc1552SCatalin Marinas 
488c1cc1552SCatalin Marinas 		if (start >= end)
489c1cc1552SCatalin Marinas 			break;
49068709f45SArd Biesheuvel 		if (memblock_is_nomap(reg))
49168709f45SArd Biesheuvel 			continue;
492c1cc1552SCatalin Marinas 
49320a004e7SWill Deacon 		__map_memblock(pgdp, start, end, PAGE_KERNEL, flags);
494c1cc1552SCatalin Marinas 	}
49598d2e153STakahiro Akashi 
49698d2e153STakahiro Akashi 	/*
49798d2e153STakahiro Akashi 	 * Map the linear alias of the [_text, __init_begin) interval
49898d2e153STakahiro Akashi 	 * as non-executable now, and remove the write permission in
49998d2e153STakahiro Akashi 	 * mark_linear_text_alias_ro() below (which will be called after
50098d2e153STakahiro Akashi 	 * alternative patching has completed). This makes the contents
50198d2e153STakahiro Akashi 	 * of the region accessible to subsystems such as hibernate,
50298d2e153STakahiro Akashi 	 * but protects it from inadvertent modification or execution.
50398d2e153STakahiro Akashi 	 * Note that contiguous mappings cannot be remapped in this way,
50498d2e153STakahiro Akashi 	 * so we should avoid them here.
50598d2e153STakahiro Akashi 	 */
50620a004e7SWill Deacon 	__map_memblock(pgdp, kernel_start, kernel_end,
50798d2e153STakahiro Akashi 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
50898d2e153STakahiro Akashi 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
50998d2e153STakahiro Akashi 
51098d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
51198d2e153STakahiro Akashi 	/*
51298d2e153STakahiro Akashi 	 * Use page-level mappings here so that we can shrink the region
51398d2e153STakahiro Akashi 	 * in page granularity and put back unused memory to buddy system
51498d2e153STakahiro Akashi 	 * through /sys/kernel/kexec_crash_size interface.
51598d2e153STakahiro Akashi 	 */
51698d2e153STakahiro Akashi 	if (crashk_res.end) {
51720a004e7SWill Deacon 		__map_memblock(pgdp, crashk_res.start, crashk_res.end + 1,
51898d2e153STakahiro Akashi 			       PAGE_KERNEL,
51998d2e153STakahiro Akashi 			       NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
52098d2e153STakahiro Akashi 		memblock_clear_nomap(crashk_res.start,
52198d2e153STakahiro Akashi 				     resource_size(&crashk_res));
52298d2e153STakahiro Akashi 	}
52398d2e153STakahiro Akashi #endif
524c1cc1552SCatalin Marinas }
525c1cc1552SCatalin Marinas 
526da141706SLaura Abbott void mark_rodata_ro(void)
527da141706SLaura Abbott {
5282f39b5f9SJeremy Linton 	unsigned long section_size;
529f9040773SArd Biesheuvel 
5302f39b5f9SJeremy Linton 	/*
5319fdc14c5SArd Biesheuvel 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
5329fdc14c5SArd Biesheuvel 	 * to cover NOTES and EXCEPTION_TABLE.
5332f39b5f9SJeremy Linton 	 */
5349fdc14c5SArd Biesheuvel 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
535aa8c09beSArd Biesheuvel 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
5362f39b5f9SJeremy Linton 			    section_size, PAGE_KERNEL_RO);
537e98216b5SArd Biesheuvel 
5381404d6f1SLaura Abbott 	debug_checkwx();
539da141706SLaura Abbott }
540da141706SLaura Abbott 
54120a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
542d27cfa1fSArd Biesheuvel 				      pgprot_t prot, struct vm_struct *vma,
54392bbd16eSWill Deacon 				      int flags, unsigned long vm_flags)
544068a17a5SMark Rutland {
5452077be67SLaura Abbott 	phys_addr_t pa_start = __pa_symbol(va_start);
546068a17a5SMark Rutland 	unsigned long size = va_end - va_start;
547068a17a5SMark Rutland 
548068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(pa_start));
549068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(size));
550068a17a5SMark Rutland 
55120a004e7SWill Deacon 	__create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
552d27cfa1fSArd Biesheuvel 			     early_pgtable_alloc, flags);
553f9040773SArd Biesheuvel 
55492bbd16eSWill Deacon 	if (!(vm_flags & VM_NO_GUARD))
55592bbd16eSWill Deacon 		size += PAGE_SIZE;
55692bbd16eSWill Deacon 
557f9040773SArd Biesheuvel 	vma->addr	= va_start;
558f9040773SArd Biesheuvel 	vma->phys_addr	= pa_start;
559f9040773SArd Biesheuvel 	vma->size	= size;
56092bbd16eSWill Deacon 	vma->flags	= VM_MAP | vm_flags;
561f9040773SArd Biesheuvel 	vma->caller	= __builtin_return_address(0);
562f9040773SArd Biesheuvel 
563f9040773SArd Biesheuvel 	vm_area_add_early(vma);
564068a17a5SMark Rutland }
565068a17a5SMark Rutland 
56628b066daSArd Biesheuvel static int __init parse_rodata(char *arg)
56728b066daSArd Biesheuvel {
568c55191e9SArd Biesheuvel 	int ret = strtobool(arg, &rodata_enabled);
569c55191e9SArd Biesheuvel 	if (!ret) {
570c55191e9SArd Biesheuvel 		rodata_full = false;
571c55191e9SArd Biesheuvel 		return 0;
572c55191e9SArd Biesheuvel 	}
573c55191e9SArd Biesheuvel 
574c55191e9SArd Biesheuvel 	/* permit 'full' in addition to boolean options */
575c55191e9SArd Biesheuvel 	if (strcmp(arg, "full"))
576c55191e9SArd Biesheuvel 		return -EINVAL;
577c55191e9SArd Biesheuvel 
578c55191e9SArd Biesheuvel 	rodata_enabled = true;
579c55191e9SArd Biesheuvel 	rodata_full = true;
580c55191e9SArd Biesheuvel 	return 0;
58128b066daSArd Biesheuvel }
58228b066daSArd Biesheuvel early_param("rodata", parse_rodata);
58328b066daSArd Biesheuvel 
58451a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
58551a0048bSWill Deacon static int __init map_entry_trampoline(void)
58651a0048bSWill Deacon {
58751a0048bSWill Deacon 	pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
58851a0048bSWill Deacon 	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
58951a0048bSWill Deacon 
59051a0048bSWill Deacon 	/* The trampoline is always mapped and can therefore be global */
59151a0048bSWill Deacon 	pgprot_val(prot) &= ~PTE_NG;
59251a0048bSWill Deacon 
59351a0048bSWill Deacon 	/* Map only the text into the trampoline page table */
59451a0048bSWill Deacon 	memset(tramp_pg_dir, 0, PGD_SIZE);
59551a0048bSWill Deacon 	__create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE,
596475ba3fcSWill Deacon 			     prot, __pgd_pgtable_alloc, 0);
59751a0048bSWill Deacon 
5986c27c408SWill Deacon 	/* Map both the text and data into the kernel page table */
59951a0048bSWill Deacon 	__set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot);
6006c27c408SWill Deacon 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
6016c27c408SWill Deacon 		extern char __entry_tramp_data_start[];
6026c27c408SWill Deacon 
6036c27c408SWill Deacon 		__set_fixmap(FIX_ENTRY_TRAMP_DATA,
6046c27c408SWill Deacon 			     __pa_symbol(__entry_tramp_data_start),
6056c27c408SWill Deacon 			     PAGE_KERNEL_RO);
6066c27c408SWill Deacon 	}
6076c27c408SWill Deacon 
60851a0048bSWill Deacon 	return 0;
60951a0048bSWill Deacon }
61051a0048bSWill Deacon core_initcall(map_entry_trampoline);
61151a0048bSWill Deacon #endif
61251a0048bSWill Deacon 
613068a17a5SMark Rutland /*
614c8027285SMark Brown  * Open coded check for BTI, only for use to determine configuration
615c8027285SMark Brown  * for early mappings for before the cpufeature code has run.
616c8027285SMark Brown  */
617c8027285SMark Brown static bool arm64_early_this_cpu_has_bti(void)
618c8027285SMark Brown {
619c8027285SMark Brown 	u64 pfr1;
620c8027285SMark Brown 
621c8027285SMark Brown 	if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
622c8027285SMark Brown 		return false;
623c8027285SMark Brown 
624c8027285SMark Brown 	pfr1 = read_sysreg_s(SYS_ID_AA64PFR1_EL1);
625c8027285SMark Brown 	return cpuid_feature_extract_unsigned_field(pfr1,
626c8027285SMark Brown 						    ID_AA64PFR1_BT_SHIFT);
627c8027285SMark Brown }
628c8027285SMark Brown 
629c8027285SMark Brown /*
630068a17a5SMark Rutland  * Create fine-grained mappings for the kernel.
631068a17a5SMark Rutland  */
63220a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp)
633068a17a5SMark Rutland {
6342ebe088bSArd Biesheuvel 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
6352ebe088bSArd Biesheuvel 				vmlinux_initdata, vmlinux_data;
636068a17a5SMark Rutland 
63728b066daSArd Biesheuvel 	/*
63828b066daSArd Biesheuvel 	 * External debuggers may need to write directly to the text
63928b066daSArd Biesheuvel 	 * mapping to install SW breakpoints. Allow this (only) when
64028b066daSArd Biesheuvel 	 * explicitly requested with rodata=off.
64128b066daSArd Biesheuvel 	 */
64228b066daSArd Biesheuvel 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
64328b066daSArd Biesheuvel 
644d27cfa1fSArd Biesheuvel 	/*
645c8027285SMark Brown 	 * If we have a CPU that supports BTI and a kernel built for
646c8027285SMark Brown 	 * BTI then mark the kernel executable text as guarded pages
647c8027285SMark Brown 	 * now so we don't have to rewrite the page tables later.
648c8027285SMark Brown 	 */
649c8027285SMark Brown 	if (arm64_early_this_cpu_has_bti())
650c8027285SMark Brown 		text_prot = __pgprot_modify(text_prot, PTE_GP, PTE_GP);
651c8027285SMark Brown 
652c8027285SMark Brown 	/*
653d27cfa1fSArd Biesheuvel 	 * Only rodata will be remapped with different permissions later on,
654d27cfa1fSArd Biesheuvel 	 * all other segments are allowed to use contiguous mappings.
655d27cfa1fSArd Biesheuvel 	 */
65620a004e7SWill Deacon 	map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0,
65792bbd16eSWill Deacon 			   VM_NO_GUARD);
65820a004e7SWill Deacon 	map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
65992bbd16eSWill Deacon 			   &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
66020a004e7SWill Deacon 	map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
66192bbd16eSWill Deacon 			   &vmlinux_inittext, 0, VM_NO_GUARD);
66220a004e7SWill Deacon 	map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
66392bbd16eSWill Deacon 			   &vmlinux_initdata, 0, VM_NO_GUARD);
66420a004e7SWill Deacon 	map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
665068a17a5SMark Rutland 
666974b9b2cSMike Rapoport 	if (!READ_ONCE(pgd_val(*pgd_offset_pgd(pgdp, FIXADDR_START)))) {
667068a17a5SMark Rutland 		/*
668f9040773SArd Biesheuvel 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
669f9040773SArd Biesheuvel 		 * live in the carveout for the swapper_pg_dir. We can simply
670f9040773SArd Biesheuvel 		 * re-use the existing dir for the fixmap.
671068a17a5SMark Rutland 		 */
672974b9b2cSMike Rapoport 		set_pgd(pgd_offset_pgd(pgdp, FIXADDR_START),
67320a004e7SWill Deacon 			READ_ONCE(*pgd_offset_k(FIXADDR_START)));
674f9040773SArd Biesheuvel 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
675b333b0baSMark Rutland 		pgd_t *bm_pgdp;
676e9f63768SMike Rapoport 		p4d_t *bm_p4dp;
677b333b0baSMark Rutland 		pud_t *bm_pudp;
678f9040773SArd Biesheuvel 		/*
679f9040773SArd Biesheuvel 		 * The fixmap shares its top level pgd entry with the kernel
680f9040773SArd Biesheuvel 		 * mapping. This can really only occur when we are running
681f9040773SArd Biesheuvel 		 * with 16k/4 levels, so we can simply reuse the pud level
682f9040773SArd Biesheuvel 		 * entry instead.
683f9040773SArd Biesheuvel 		 */
684f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
685974b9b2cSMike Rapoport 		bm_pgdp = pgd_offset_pgd(pgdp, FIXADDR_START);
686e9f63768SMike Rapoport 		bm_p4dp = p4d_offset(bm_pgdp, FIXADDR_START);
687e9f63768SMike Rapoport 		bm_pudp = pud_set_fixmap_offset(bm_p4dp, FIXADDR_START);
688b333b0baSMark Rutland 		pud_populate(&init_mm, bm_pudp, lm_alias(bm_pmd));
689f9040773SArd Biesheuvel 		pud_clear_fixmap();
690f9040773SArd Biesheuvel 	} else {
691f9040773SArd Biesheuvel 		BUG();
692f9040773SArd Biesheuvel 	}
693068a17a5SMark Rutland 
69420a004e7SWill Deacon 	kasan_copy_shadow(pgdp);
695068a17a5SMark Rutland }
696068a17a5SMark Rutland 
697c1cc1552SCatalin Marinas void __init paging_init(void)
698c1cc1552SCatalin Marinas {
6992330b7caSJun Yao 	pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
700068a17a5SMark Rutland 
70120a004e7SWill Deacon 	map_kernel(pgdp);
70220a004e7SWill Deacon 	map_mem(pgdp);
703068a17a5SMark Rutland 
704068a17a5SMark Rutland 	pgd_clear_fixmap();
705068a17a5SMark Rutland 
706068a17a5SMark Rutland 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
7072b5548b6SJun Yao 	init_mm.pgd = swapper_pg_dir;
708068a17a5SMark Rutland 
7092b5548b6SJun Yao 	memblock_free(__pa_symbol(init_pg_dir),
7102b5548b6SJun Yao 		      __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
71124cc61d8SArd Biesheuvel 
71224cc61d8SArd Biesheuvel 	memblock_allow_resize();
713c1cc1552SCatalin Marinas }
714c1cc1552SCatalin Marinas 
715c1cc1552SCatalin Marinas /*
716c1cc1552SCatalin Marinas  * Check whether a kernel address is valid (derived from arch/x86/).
717c1cc1552SCatalin Marinas  */
718c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr)
719c1cc1552SCatalin Marinas {
72020a004e7SWill Deacon 	pgd_t *pgdp;
721e9f63768SMike Rapoport 	p4d_t *p4dp;
72220a004e7SWill Deacon 	pud_t *pudp, pud;
72320a004e7SWill Deacon 	pmd_t *pmdp, pmd;
72420a004e7SWill Deacon 	pte_t *ptep, pte;
725c1cc1552SCatalin Marinas 
7268dd4daa0SShyam Thombre 	addr = arch_kasan_reset_tag(addr);
727c1cc1552SCatalin Marinas 	if ((((long)addr) >> VA_BITS) != -1UL)
728c1cc1552SCatalin Marinas 		return 0;
729c1cc1552SCatalin Marinas 
73020a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
73120a004e7SWill Deacon 	if (pgd_none(READ_ONCE(*pgdp)))
732c1cc1552SCatalin Marinas 		return 0;
733c1cc1552SCatalin Marinas 
734e9f63768SMike Rapoport 	p4dp = p4d_offset(pgdp, addr);
735e9f63768SMike Rapoport 	if (p4d_none(READ_ONCE(*p4dp)))
736e9f63768SMike Rapoport 		return 0;
737e9f63768SMike Rapoport 
738e9f63768SMike Rapoport 	pudp = pud_offset(p4dp, addr);
73920a004e7SWill Deacon 	pud = READ_ONCE(*pudp);
74020a004e7SWill Deacon 	if (pud_none(pud))
741c1cc1552SCatalin Marinas 		return 0;
742c1cc1552SCatalin Marinas 
74320a004e7SWill Deacon 	if (pud_sect(pud))
74420a004e7SWill Deacon 		return pfn_valid(pud_pfn(pud));
745206a2a73SSteve Capper 
74620a004e7SWill Deacon 	pmdp = pmd_offset(pudp, addr);
74720a004e7SWill Deacon 	pmd = READ_ONCE(*pmdp);
74820a004e7SWill Deacon 	if (pmd_none(pmd))
749c1cc1552SCatalin Marinas 		return 0;
750c1cc1552SCatalin Marinas 
75120a004e7SWill Deacon 	if (pmd_sect(pmd))
75220a004e7SWill Deacon 		return pfn_valid(pmd_pfn(pmd));
753da6e4cb6SDave Anderson 
75420a004e7SWill Deacon 	ptep = pte_offset_kernel(pmdp, addr);
75520a004e7SWill Deacon 	pte = READ_ONCE(*ptep);
75620a004e7SWill Deacon 	if (pte_none(pte))
757c1cc1552SCatalin Marinas 		return 0;
758c1cc1552SCatalin Marinas 
75920a004e7SWill Deacon 	return pfn_valid(pte_pfn(pte));
760c1cc1552SCatalin Marinas }
761bbd6ec60SAnshuman Khandual 
762bbd6ec60SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG
763bbd6ec60SAnshuman Khandual static void free_hotplug_page_range(struct page *page, size_t size)
764bbd6ec60SAnshuman Khandual {
765bbd6ec60SAnshuman Khandual 	WARN_ON(PageReserved(page));
766bbd6ec60SAnshuman Khandual 	free_pages((unsigned long)page_address(page), get_order(size));
767bbd6ec60SAnshuman Khandual }
768bbd6ec60SAnshuman Khandual 
769bbd6ec60SAnshuman Khandual static void free_hotplug_pgtable_page(struct page *page)
770bbd6ec60SAnshuman Khandual {
771bbd6ec60SAnshuman Khandual 	free_hotplug_page_range(page, PAGE_SIZE);
772bbd6ec60SAnshuman Khandual }
773bbd6ec60SAnshuman Khandual 
774bbd6ec60SAnshuman Khandual static bool pgtable_range_aligned(unsigned long start, unsigned long end,
775bbd6ec60SAnshuman Khandual 				  unsigned long floor, unsigned long ceiling,
776bbd6ec60SAnshuman Khandual 				  unsigned long mask)
777bbd6ec60SAnshuman Khandual {
778bbd6ec60SAnshuman Khandual 	start &= mask;
779bbd6ec60SAnshuman Khandual 	if (start < floor)
780bbd6ec60SAnshuman Khandual 		return false;
781bbd6ec60SAnshuman Khandual 
782bbd6ec60SAnshuman Khandual 	if (ceiling) {
783bbd6ec60SAnshuman Khandual 		ceiling &= mask;
784bbd6ec60SAnshuman Khandual 		if (!ceiling)
785bbd6ec60SAnshuman Khandual 			return false;
786bbd6ec60SAnshuman Khandual 	}
787bbd6ec60SAnshuman Khandual 
788bbd6ec60SAnshuman Khandual 	if (end - 1 > ceiling - 1)
789bbd6ec60SAnshuman Khandual 		return false;
790bbd6ec60SAnshuman Khandual 	return true;
791bbd6ec60SAnshuman Khandual }
792bbd6ec60SAnshuman Khandual 
793bbd6ec60SAnshuman Khandual static void unmap_hotplug_pte_range(pmd_t *pmdp, unsigned long addr,
794bbd6ec60SAnshuman Khandual 				    unsigned long end, bool free_mapped)
795bbd6ec60SAnshuman Khandual {
796bbd6ec60SAnshuman Khandual 	pte_t *ptep, pte;
797bbd6ec60SAnshuman Khandual 
798bbd6ec60SAnshuman Khandual 	do {
799bbd6ec60SAnshuman Khandual 		ptep = pte_offset_kernel(pmdp, addr);
800bbd6ec60SAnshuman Khandual 		pte = READ_ONCE(*ptep);
801bbd6ec60SAnshuman Khandual 		if (pte_none(pte))
802bbd6ec60SAnshuman Khandual 			continue;
803bbd6ec60SAnshuman Khandual 
804bbd6ec60SAnshuman Khandual 		WARN_ON(!pte_present(pte));
805bbd6ec60SAnshuman Khandual 		pte_clear(&init_mm, addr, ptep);
806bbd6ec60SAnshuman Khandual 		flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
807bbd6ec60SAnshuman Khandual 		if (free_mapped)
808bbd6ec60SAnshuman Khandual 			free_hotplug_page_range(pte_page(pte), PAGE_SIZE);
809bbd6ec60SAnshuman Khandual 	} while (addr += PAGE_SIZE, addr < end);
810bbd6ec60SAnshuman Khandual }
811bbd6ec60SAnshuman Khandual 
812bbd6ec60SAnshuman Khandual static void unmap_hotplug_pmd_range(pud_t *pudp, unsigned long addr,
813bbd6ec60SAnshuman Khandual 				    unsigned long end, bool free_mapped)
814bbd6ec60SAnshuman Khandual {
815bbd6ec60SAnshuman Khandual 	unsigned long next;
816bbd6ec60SAnshuman Khandual 	pmd_t *pmdp, pmd;
817bbd6ec60SAnshuman Khandual 
818bbd6ec60SAnshuman Khandual 	do {
819bbd6ec60SAnshuman Khandual 		next = pmd_addr_end(addr, end);
820bbd6ec60SAnshuman Khandual 		pmdp = pmd_offset(pudp, addr);
821bbd6ec60SAnshuman Khandual 		pmd = READ_ONCE(*pmdp);
822bbd6ec60SAnshuman Khandual 		if (pmd_none(pmd))
823bbd6ec60SAnshuman Khandual 			continue;
824bbd6ec60SAnshuman Khandual 
825bbd6ec60SAnshuman Khandual 		WARN_ON(!pmd_present(pmd));
826bbd6ec60SAnshuman Khandual 		if (pmd_sect(pmd)) {
827bbd6ec60SAnshuman Khandual 			pmd_clear(pmdp);
828bbd6ec60SAnshuman Khandual 
829bbd6ec60SAnshuman Khandual 			/*
830bbd6ec60SAnshuman Khandual 			 * One TLBI should be sufficient here as the PMD_SIZE
831bbd6ec60SAnshuman Khandual 			 * range is mapped with a single block entry.
832bbd6ec60SAnshuman Khandual 			 */
833bbd6ec60SAnshuman Khandual 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
834bbd6ec60SAnshuman Khandual 			if (free_mapped)
835bbd6ec60SAnshuman Khandual 				free_hotplug_page_range(pmd_page(pmd),
836bbd6ec60SAnshuman Khandual 							PMD_SIZE);
837bbd6ec60SAnshuman Khandual 			continue;
838bbd6ec60SAnshuman Khandual 		}
839bbd6ec60SAnshuman Khandual 		WARN_ON(!pmd_table(pmd));
840bbd6ec60SAnshuman Khandual 		unmap_hotplug_pte_range(pmdp, addr, next, free_mapped);
841bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
842bbd6ec60SAnshuman Khandual }
843bbd6ec60SAnshuman Khandual 
844bbd6ec60SAnshuman Khandual static void unmap_hotplug_pud_range(p4d_t *p4dp, unsigned long addr,
845bbd6ec60SAnshuman Khandual 				    unsigned long end, bool free_mapped)
846bbd6ec60SAnshuman Khandual {
847bbd6ec60SAnshuman Khandual 	unsigned long next;
848bbd6ec60SAnshuman Khandual 	pud_t *pudp, pud;
849bbd6ec60SAnshuman Khandual 
850bbd6ec60SAnshuman Khandual 	do {
851bbd6ec60SAnshuman Khandual 		next = pud_addr_end(addr, end);
852bbd6ec60SAnshuman Khandual 		pudp = pud_offset(p4dp, addr);
853bbd6ec60SAnshuman Khandual 		pud = READ_ONCE(*pudp);
854bbd6ec60SAnshuman Khandual 		if (pud_none(pud))
855bbd6ec60SAnshuman Khandual 			continue;
856bbd6ec60SAnshuman Khandual 
857bbd6ec60SAnshuman Khandual 		WARN_ON(!pud_present(pud));
858bbd6ec60SAnshuman Khandual 		if (pud_sect(pud)) {
859bbd6ec60SAnshuman Khandual 			pud_clear(pudp);
860bbd6ec60SAnshuman Khandual 
861bbd6ec60SAnshuman Khandual 			/*
862bbd6ec60SAnshuman Khandual 			 * One TLBI should be sufficient here as the PUD_SIZE
863bbd6ec60SAnshuman Khandual 			 * range is mapped with a single block entry.
864bbd6ec60SAnshuman Khandual 			 */
865bbd6ec60SAnshuman Khandual 			flush_tlb_kernel_range(addr, addr + PAGE_SIZE);
866bbd6ec60SAnshuman Khandual 			if (free_mapped)
867bbd6ec60SAnshuman Khandual 				free_hotplug_page_range(pud_page(pud),
868bbd6ec60SAnshuman Khandual 							PUD_SIZE);
869bbd6ec60SAnshuman Khandual 			continue;
870bbd6ec60SAnshuman Khandual 		}
871bbd6ec60SAnshuman Khandual 		WARN_ON(!pud_table(pud));
872bbd6ec60SAnshuman Khandual 		unmap_hotplug_pmd_range(pudp, addr, next, free_mapped);
873bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
874bbd6ec60SAnshuman Khandual }
875bbd6ec60SAnshuman Khandual 
876bbd6ec60SAnshuman Khandual static void unmap_hotplug_p4d_range(pgd_t *pgdp, unsigned long addr,
877bbd6ec60SAnshuman Khandual 				    unsigned long end, bool free_mapped)
878bbd6ec60SAnshuman Khandual {
879bbd6ec60SAnshuman Khandual 	unsigned long next;
880bbd6ec60SAnshuman Khandual 	p4d_t *p4dp, p4d;
881bbd6ec60SAnshuman Khandual 
882bbd6ec60SAnshuman Khandual 	do {
883bbd6ec60SAnshuman Khandual 		next = p4d_addr_end(addr, end);
884bbd6ec60SAnshuman Khandual 		p4dp = p4d_offset(pgdp, addr);
885bbd6ec60SAnshuman Khandual 		p4d = READ_ONCE(*p4dp);
886bbd6ec60SAnshuman Khandual 		if (p4d_none(p4d))
887bbd6ec60SAnshuman Khandual 			continue;
888bbd6ec60SAnshuman Khandual 
889bbd6ec60SAnshuman Khandual 		WARN_ON(!p4d_present(p4d));
890bbd6ec60SAnshuman Khandual 		unmap_hotplug_pud_range(p4dp, addr, next, free_mapped);
891bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
892bbd6ec60SAnshuman Khandual }
893bbd6ec60SAnshuman Khandual 
894bbd6ec60SAnshuman Khandual static void unmap_hotplug_range(unsigned long addr, unsigned long end,
895bbd6ec60SAnshuman Khandual 				bool free_mapped)
896bbd6ec60SAnshuman Khandual {
897bbd6ec60SAnshuman Khandual 	unsigned long next;
898bbd6ec60SAnshuman Khandual 	pgd_t *pgdp, pgd;
899bbd6ec60SAnshuman Khandual 
900bbd6ec60SAnshuman Khandual 	do {
901bbd6ec60SAnshuman Khandual 		next = pgd_addr_end(addr, end);
902bbd6ec60SAnshuman Khandual 		pgdp = pgd_offset_k(addr);
903bbd6ec60SAnshuman Khandual 		pgd = READ_ONCE(*pgdp);
904bbd6ec60SAnshuman Khandual 		if (pgd_none(pgd))
905bbd6ec60SAnshuman Khandual 			continue;
906bbd6ec60SAnshuman Khandual 
907bbd6ec60SAnshuman Khandual 		WARN_ON(!pgd_present(pgd));
908bbd6ec60SAnshuman Khandual 		unmap_hotplug_p4d_range(pgdp, addr, next, free_mapped);
909bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
910bbd6ec60SAnshuman Khandual }
911bbd6ec60SAnshuman Khandual 
912bbd6ec60SAnshuman Khandual static void free_empty_pte_table(pmd_t *pmdp, unsigned long addr,
913bbd6ec60SAnshuman Khandual 				 unsigned long end, unsigned long floor,
914bbd6ec60SAnshuman Khandual 				 unsigned long ceiling)
915bbd6ec60SAnshuman Khandual {
916bbd6ec60SAnshuman Khandual 	pte_t *ptep, pte;
917bbd6ec60SAnshuman Khandual 	unsigned long i, start = addr;
918bbd6ec60SAnshuman Khandual 
919bbd6ec60SAnshuman Khandual 	do {
920bbd6ec60SAnshuman Khandual 		ptep = pte_offset_kernel(pmdp, addr);
921bbd6ec60SAnshuman Khandual 		pte = READ_ONCE(*ptep);
922bbd6ec60SAnshuman Khandual 
923bbd6ec60SAnshuman Khandual 		/*
924bbd6ec60SAnshuman Khandual 		 * This is just a sanity check here which verifies that
925bbd6ec60SAnshuman Khandual 		 * pte clearing has been done by earlier unmap loops.
926bbd6ec60SAnshuman Khandual 		 */
927bbd6ec60SAnshuman Khandual 		WARN_ON(!pte_none(pte));
928bbd6ec60SAnshuman Khandual 	} while (addr += PAGE_SIZE, addr < end);
929bbd6ec60SAnshuman Khandual 
930bbd6ec60SAnshuman Khandual 	if (!pgtable_range_aligned(start, end, floor, ceiling, PMD_MASK))
931bbd6ec60SAnshuman Khandual 		return;
932bbd6ec60SAnshuman Khandual 
933bbd6ec60SAnshuman Khandual 	/*
934bbd6ec60SAnshuman Khandual 	 * Check whether we can free the pte page if the rest of the
935bbd6ec60SAnshuman Khandual 	 * entries are empty. Overlap with other regions have been
936bbd6ec60SAnshuman Khandual 	 * handled by the floor/ceiling check.
937bbd6ec60SAnshuman Khandual 	 */
938bbd6ec60SAnshuman Khandual 	ptep = pte_offset_kernel(pmdp, 0UL);
939bbd6ec60SAnshuman Khandual 	for (i = 0; i < PTRS_PER_PTE; i++) {
940bbd6ec60SAnshuman Khandual 		if (!pte_none(READ_ONCE(ptep[i])))
941bbd6ec60SAnshuman Khandual 			return;
942bbd6ec60SAnshuman Khandual 	}
943bbd6ec60SAnshuman Khandual 
944bbd6ec60SAnshuman Khandual 	pmd_clear(pmdp);
945bbd6ec60SAnshuman Khandual 	__flush_tlb_kernel_pgtable(start);
946bbd6ec60SAnshuman Khandual 	free_hotplug_pgtable_page(virt_to_page(ptep));
947bbd6ec60SAnshuman Khandual }
948bbd6ec60SAnshuman Khandual 
949bbd6ec60SAnshuman Khandual static void free_empty_pmd_table(pud_t *pudp, unsigned long addr,
950bbd6ec60SAnshuman Khandual 				 unsigned long end, unsigned long floor,
951bbd6ec60SAnshuman Khandual 				 unsigned long ceiling)
952bbd6ec60SAnshuman Khandual {
953bbd6ec60SAnshuman Khandual 	pmd_t *pmdp, pmd;
954bbd6ec60SAnshuman Khandual 	unsigned long i, next, start = addr;
955bbd6ec60SAnshuman Khandual 
956bbd6ec60SAnshuman Khandual 	do {
957bbd6ec60SAnshuman Khandual 		next = pmd_addr_end(addr, end);
958bbd6ec60SAnshuman Khandual 		pmdp = pmd_offset(pudp, addr);
959bbd6ec60SAnshuman Khandual 		pmd = READ_ONCE(*pmdp);
960bbd6ec60SAnshuman Khandual 		if (pmd_none(pmd))
961bbd6ec60SAnshuman Khandual 			continue;
962bbd6ec60SAnshuman Khandual 
963bbd6ec60SAnshuman Khandual 		WARN_ON(!pmd_present(pmd) || !pmd_table(pmd) || pmd_sect(pmd));
964bbd6ec60SAnshuman Khandual 		free_empty_pte_table(pmdp, addr, next, floor, ceiling);
965bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
966bbd6ec60SAnshuman Khandual 
967bbd6ec60SAnshuman Khandual 	if (CONFIG_PGTABLE_LEVELS <= 2)
968bbd6ec60SAnshuman Khandual 		return;
969bbd6ec60SAnshuman Khandual 
970bbd6ec60SAnshuman Khandual 	if (!pgtable_range_aligned(start, end, floor, ceiling, PUD_MASK))
971bbd6ec60SAnshuman Khandual 		return;
972bbd6ec60SAnshuman Khandual 
973bbd6ec60SAnshuman Khandual 	/*
974bbd6ec60SAnshuman Khandual 	 * Check whether we can free the pmd page if the rest of the
975bbd6ec60SAnshuman Khandual 	 * entries are empty. Overlap with other regions have been
976bbd6ec60SAnshuman Khandual 	 * handled by the floor/ceiling check.
977bbd6ec60SAnshuman Khandual 	 */
978bbd6ec60SAnshuman Khandual 	pmdp = pmd_offset(pudp, 0UL);
979bbd6ec60SAnshuman Khandual 	for (i = 0; i < PTRS_PER_PMD; i++) {
980bbd6ec60SAnshuman Khandual 		if (!pmd_none(READ_ONCE(pmdp[i])))
981bbd6ec60SAnshuman Khandual 			return;
982bbd6ec60SAnshuman Khandual 	}
983bbd6ec60SAnshuman Khandual 
984bbd6ec60SAnshuman Khandual 	pud_clear(pudp);
985bbd6ec60SAnshuman Khandual 	__flush_tlb_kernel_pgtable(start);
986bbd6ec60SAnshuman Khandual 	free_hotplug_pgtable_page(virt_to_page(pmdp));
987bbd6ec60SAnshuman Khandual }
988bbd6ec60SAnshuman Khandual 
989bbd6ec60SAnshuman Khandual static void free_empty_pud_table(p4d_t *p4dp, unsigned long addr,
990bbd6ec60SAnshuman Khandual 				 unsigned long end, unsigned long floor,
991bbd6ec60SAnshuman Khandual 				 unsigned long ceiling)
992bbd6ec60SAnshuman Khandual {
993bbd6ec60SAnshuman Khandual 	pud_t *pudp, pud;
994bbd6ec60SAnshuman Khandual 	unsigned long i, next, start = addr;
995bbd6ec60SAnshuman Khandual 
996bbd6ec60SAnshuman Khandual 	do {
997bbd6ec60SAnshuman Khandual 		next = pud_addr_end(addr, end);
998bbd6ec60SAnshuman Khandual 		pudp = pud_offset(p4dp, addr);
999bbd6ec60SAnshuman Khandual 		pud = READ_ONCE(*pudp);
1000bbd6ec60SAnshuman Khandual 		if (pud_none(pud))
1001bbd6ec60SAnshuman Khandual 			continue;
1002bbd6ec60SAnshuman Khandual 
1003bbd6ec60SAnshuman Khandual 		WARN_ON(!pud_present(pud) || !pud_table(pud) || pud_sect(pud));
1004bbd6ec60SAnshuman Khandual 		free_empty_pmd_table(pudp, addr, next, floor, ceiling);
1005bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
1006bbd6ec60SAnshuman Khandual 
1007bbd6ec60SAnshuman Khandual 	if (CONFIG_PGTABLE_LEVELS <= 3)
1008bbd6ec60SAnshuman Khandual 		return;
1009bbd6ec60SAnshuman Khandual 
1010bbd6ec60SAnshuman Khandual 	if (!pgtable_range_aligned(start, end, floor, ceiling, PGDIR_MASK))
1011bbd6ec60SAnshuman Khandual 		return;
1012bbd6ec60SAnshuman Khandual 
1013bbd6ec60SAnshuman Khandual 	/*
1014bbd6ec60SAnshuman Khandual 	 * Check whether we can free the pud page if the rest of the
1015bbd6ec60SAnshuman Khandual 	 * entries are empty. Overlap with other regions have been
1016bbd6ec60SAnshuman Khandual 	 * handled by the floor/ceiling check.
1017bbd6ec60SAnshuman Khandual 	 */
1018bbd6ec60SAnshuman Khandual 	pudp = pud_offset(p4dp, 0UL);
1019bbd6ec60SAnshuman Khandual 	for (i = 0; i < PTRS_PER_PUD; i++) {
1020bbd6ec60SAnshuman Khandual 		if (!pud_none(READ_ONCE(pudp[i])))
1021bbd6ec60SAnshuman Khandual 			return;
1022bbd6ec60SAnshuman Khandual 	}
1023bbd6ec60SAnshuman Khandual 
1024bbd6ec60SAnshuman Khandual 	p4d_clear(p4dp);
1025bbd6ec60SAnshuman Khandual 	__flush_tlb_kernel_pgtable(start);
1026bbd6ec60SAnshuman Khandual 	free_hotplug_pgtable_page(virt_to_page(pudp));
1027bbd6ec60SAnshuman Khandual }
1028bbd6ec60SAnshuman Khandual 
1029bbd6ec60SAnshuman Khandual static void free_empty_p4d_table(pgd_t *pgdp, unsigned long addr,
1030bbd6ec60SAnshuman Khandual 				 unsigned long end, unsigned long floor,
1031bbd6ec60SAnshuman Khandual 				 unsigned long ceiling)
1032bbd6ec60SAnshuman Khandual {
1033bbd6ec60SAnshuman Khandual 	unsigned long next;
1034bbd6ec60SAnshuman Khandual 	p4d_t *p4dp, p4d;
1035bbd6ec60SAnshuman Khandual 
1036bbd6ec60SAnshuman Khandual 	do {
1037bbd6ec60SAnshuman Khandual 		next = p4d_addr_end(addr, end);
1038bbd6ec60SAnshuman Khandual 		p4dp = p4d_offset(pgdp, addr);
1039bbd6ec60SAnshuman Khandual 		p4d = READ_ONCE(*p4dp);
1040bbd6ec60SAnshuman Khandual 		if (p4d_none(p4d))
1041bbd6ec60SAnshuman Khandual 			continue;
1042bbd6ec60SAnshuman Khandual 
1043bbd6ec60SAnshuman Khandual 		WARN_ON(!p4d_present(p4d));
1044bbd6ec60SAnshuman Khandual 		free_empty_pud_table(p4dp, addr, next, floor, ceiling);
1045bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
1046bbd6ec60SAnshuman Khandual }
1047bbd6ec60SAnshuman Khandual 
1048bbd6ec60SAnshuman Khandual static void free_empty_tables(unsigned long addr, unsigned long end,
1049bbd6ec60SAnshuman Khandual 			      unsigned long floor, unsigned long ceiling)
1050bbd6ec60SAnshuman Khandual {
1051bbd6ec60SAnshuman Khandual 	unsigned long next;
1052bbd6ec60SAnshuman Khandual 	pgd_t *pgdp, pgd;
1053bbd6ec60SAnshuman Khandual 
1054bbd6ec60SAnshuman Khandual 	do {
1055bbd6ec60SAnshuman Khandual 		next = pgd_addr_end(addr, end);
1056bbd6ec60SAnshuman Khandual 		pgdp = pgd_offset_k(addr);
1057bbd6ec60SAnshuman Khandual 		pgd = READ_ONCE(*pgdp);
1058bbd6ec60SAnshuman Khandual 		if (pgd_none(pgd))
1059bbd6ec60SAnshuman Khandual 			continue;
1060bbd6ec60SAnshuman Khandual 
1061bbd6ec60SAnshuman Khandual 		WARN_ON(!pgd_present(pgd));
1062bbd6ec60SAnshuman Khandual 		free_empty_p4d_table(pgdp, addr, next, floor, ceiling);
1063bbd6ec60SAnshuman Khandual 	} while (addr = next, addr < end);
1064bbd6ec60SAnshuman Khandual }
1065bbd6ec60SAnshuman Khandual #endif
1066bbd6ec60SAnshuman Khandual 
1067c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP
1068b433dce0SSuzuki K. Poulose #if !ARM64_SWAPPER_USES_SECTION_MAPS
10697b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
10707b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
1071c1cc1552SCatalin Marinas {
10720aad818bSJohannes Weiner 	return vmemmap_populate_basepages(start, end, node);
1073c1cc1552SCatalin Marinas }
1074b433dce0SSuzuki K. Poulose #else	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
10757b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
10767b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
1077c1cc1552SCatalin Marinas {
10780aad818bSJohannes Weiner 	unsigned long addr = start;
1079c1cc1552SCatalin Marinas 	unsigned long next;
108020a004e7SWill Deacon 	pgd_t *pgdp;
1081e9f63768SMike Rapoport 	p4d_t *p4dp;
108220a004e7SWill Deacon 	pud_t *pudp;
108320a004e7SWill Deacon 	pmd_t *pmdp;
1084c1cc1552SCatalin Marinas 
1085c1cc1552SCatalin Marinas 	do {
1086c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
1087c1cc1552SCatalin Marinas 
108820a004e7SWill Deacon 		pgdp = vmemmap_pgd_populate(addr, node);
108920a004e7SWill Deacon 		if (!pgdp)
1090c1cc1552SCatalin Marinas 			return -ENOMEM;
1091c1cc1552SCatalin Marinas 
1092e9f63768SMike Rapoport 		p4dp = vmemmap_p4d_populate(pgdp, addr, node);
1093e9f63768SMike Rapoport 		if (!p4dp)
1094e9f63768SMike Rapoport 			return -ENOMEM;
1095e9f63768SMike Rapoport 
1096e9f63768SMike Rapoport 		pudp = vmemmap_pud_populate(p4dp, addr, node);
109720a004e7SWill Deacon 		if (!pudp)
1098c1cc1552SCatalin Marinas 			return -ENOMEM;
1099c1cc1552SCatalin Marinas 
110020a004e7SWill Deacon 		pmdp = pmd_offset(pudp, addr);
110120a004e7SWill Deacon 		if (pmd_none(READ_ONCE(*pmdp))) {
1102c1cc1552SCatalin Marinas 			void *p = NULL;
1103c1cc1552SCatalin Marinas 
1104c1cc1552SCatalin Marinas 			p = vmemmap_alloc_block_buf(PMD_SIZE, node);
1105c1cc1552SCatalin Marinas 			if (!p)
1106c1cc1552SCatalin Marinas 				return -ENOMEM;
1107c1cc1552SCatalin Marinas 
110820a004e7SWill Deacon 			pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
1109c1cc1552SCatalin Marinas 		} else
111020a004e7SWill Deacon 			vmemmap_verify((pte_t *)pmdp, node, addr, next);
1111c1cc1552SCatalin Marinas 	} while (addr = next, addr != end);
1112c1cc1552SCatalin Marinas 
1113c1cc1552SCatalin Marinas 	return 0;
1114c1cc1552SCatalin Marinas }
11158e01076aSOdin Ugedal #endif	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
111624b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end,
111724b6d416SChristoph Hellwig 		struct vmem_altmap *altmap)
11180197518cSTang Chen {
1119bbd6ec60SAnshuman Khandual #ifdef CONFIG_MEMORY_HOTPLUG
1120bbd6ec60SAnshuman Khandual 	WARN_ON((start < VMEMMAP_START) || (end > VMEMMAP_END));
1121bbd6ec60SAnshuman Khandual 
1122bbd6ec60SAnshuman Khandual 	unmap_hotplug_range(start, end, true);
1123bbd6ec60SAnshuman Khandual 	free_empty_tables(start, end, VMEMMAP_START, VMEMMAP_END);
1124bbd6ec60SAnshuman Khandual #endif
11250197518cSTang Chen }
1126c1cc1552SCatalin Marinas #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
1127af86e597SLaura Abbott 
1128af86e597SLaura Abbott static inline pud_t * fixmap_pud(unsigned long addr)
1129af86e597SLaura Abbott {
113020a004e7SWill Deacon 	pgd_t *pgdp = pgd_offset_k(addr);
1131e9f63768SMike Rapoport 	p4d_t *p4dp = p4d_offset(pgdp, addr);
1132e9f63768SMike Rapoport 	p4d_t p4d = READ_ONCE(*p4dp);
1133af86e597SLaura Abbott 
1134e9f63768SMike Rapoport 	BUG_ON(p4d_none(p4d) || p4d_bad(p4d));
1135af86e597SLaura Abbott 
1136e9f63768SMike Rapoport 	return pud_offset_kimg(p4dp, addr);
1137af86e597SLaura Abbott }
1138af86e597SLaura Abbott 
1139af86e597SLaura Abbott static inline pmd_t * fixmap_pmd(unsigned long addr)
1140af86e597SLaura Abbott {
114120a004e7SWill Deacon 	pud_t *pudp = fixmap_pud(addr);
114220a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
1143af86e597SLaura Abbott 
114420a004e7SWill Deacon 	BUG_ON(pud_none(pud) || pud_bad(pud));
1145af86e597SLaura Abbott 
114620a004e7SWill Deacon 	return pmd_offset_kimg(pudp, addr);
1147af86e597SLaura Abbott }
1148af86e597SLaura Abbott 
1149af86e597SLaura Abbott static inline pte_t * fixmap_pte(unsigned long addr)
1150af86e597SLaura Abbott {
1151157962f5SArd Biesheuvel 	return &bm_pte[pte_index(addr)];
1152af86e597SLaura Abbott }
1153af86e597SLaura Abbott 
11542077be67SLaura Abbott /*
11552077be67SLaura Abbott  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
11562077be67SLaura Abbott  * directly on kernel symbols (bm_p*d). This function is called too early to use
11572077be67SLaura Abbott  * lm_alias so __p*d_populate functions must be used to populate with the
11582077be67SLaura Abbott  * physical address from __pa_symbol.
11592077be67SLaura Abbott  */
1160af86e597SLaura Abbott void __init early_fixmap_init(void)
1161af86e597SLaura Abbott {
1162e9f63768SMike Rapoport 	pgd_t *pgdp;
1163e9f63768SMike Rapoport 	p4d_t *p4dp, p4d;
116420a004e7SWill Deacon 	pud_t *pudp;
116520a004e7SWill Deacon 	pmd_t *pmdp;
1166af86e597SLaura Abbott 	unsigned long addr = FIXADDR_START;
1167af86e597SLaura Abbott 
116820a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
1169e9f63768SMike Rapoport 	p4dp = p4d_offset(pgdp, addr);
1170e9f63768SMike Rapoport 	p4d = READ_ONCE(*p4dp);
1171f80fb3a3SArd Biesheuvel 	if (CONFIG_PGTABLE_LEVELS > 3 &&
1172e9f63768SMike Rapoport 	    !(p4d_none(p4d) || p4d_page_paddr(p4d) == __pa_symbol(bm_pud))) {
1173f9040773SArd Biesheuvel 		/*
1174f9040773SArd Biesheuvel 		 * We only end up here if the kernel mapping and the fixmap
1175f9040773SArd Biesheuvel 		 * share the top level pgd entry, which should only happen on
1176f9040773SArd Biesheuvel 		 * 16k/4 levels configurations.
1177f9040773SArd Biesheuvel 		 */
1178f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
1179e9f63768SMike Rapoport 		pudp = pud_offset_kimg(p4dp, addr);
1180f9040773SArd Biesheuvel 	} else {
1181e9f63768SMike Rapoport 		if (p4d_none(p4d))
1182e9f63768SMike Rapoport 			__p4d_populate(p4dp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
118320a004e7SWill Deacon 		pudp = fixmap_pud(addr);
1184f9040773SArd Biesheuvel 	}
118520a004e7SWill Deacon 	if (pud_none(READ_ONCE(*pudp)))
118620a004e7SWill Deacon 		__pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
118720a004e7SWill Deacon 	pmdp = fixmap_pmd(addr);
118820a004e7SWill Deacon 	__pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
1189af86e597SLaura Abbott 
1190af86e597SLaura Abbott 	/*
1191af86e597SLaura Abbott 	 * The boot-ioremap range spans multiple pmds, for which
1192157962f5SArd Biesheuvel 	 * we are not prepared:
1193af86e597SLaura Abbott 	 */
1194af86e597SLaura Abbott 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
1195af86e597SLaura Abbott 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
1196af86e597SLaura Abbott 
119720a004e7SWill Deacon 	if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
119820a004e7SWill Deacon 	     || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
1199af86e597SLaura Abbott 		WARN_ON(1);
120020a004e7SWill Deacon 		pr_warn("pmdp %p != %p, %p\n",
120120a004e7SWill Deacon 			pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
1202af86e597SLaura Abbott 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
1203af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
1204af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_BEGIN));
1205af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
1206af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_END));
1207af86e597SLaura Abbott 
1208af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
1209af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
1210af86e597SLaura Abbott 	}
1211af86e597SLaura Abbott }
1212af86e597SLaura Abbott 
121318b4b276SJames Morse /*
121418b4b276SJames Morse  * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
121518b4b276SJames Morse  * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
121618b4b276SJames Morse  */
1217af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx,
1218af86e597SLaura Abbott 			       phys_addr_t phys, pgprot_t flags)
1219af86e597SLaura Abbott {
1220af86e597SLaura Abbott 	unsigned long addr = __fix_to_virt(idx);
122120a004e7SWill Deacon 	pte_t *ptep;
1222af86e597SLaura Abbott 
1223b63dbef9SMark Rutland 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
1224af86e597SLaura Abbott 
122520a004e7SWill Deacon 	ptep = fixmap_pte(addr);
1226af86e597SLaura Abbott 
1227af86e597SLaura Abbott 	if (pgprot_val(flags)) {
122820a004e7SWill Deacon 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
1229af86e597SLaura Abbott 	} else {
123020a004e7SWill Deacon 		pte_clear(&init_mm, addr, ptep);
1231af86e597SLaura Abbott 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
1232af86e597SLaura Abbott 	}
1233af86e597SLaura Abbott }
123461bd93ceSArd Biesheuvel 
1235e112b032SHsin-Yi Wang void *__init fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
123661bd93ceSArd Biesheuvel {
123761bd93ceSArd Biesheuvel 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
1238f80fb3a3SArd Biesheuvel 	int offset;
123961bd93ceSArd Biesheuvel 	void *dt_virt;
124061bd93ceSArd Biesheuvel 
124161bd93ceSArd Biesheuvel 	/*
124261bd93ceSArd Biesheuvel 	 * Check whether the physical FDT address is set and meets the minimum
124361bd93ceSArd Biesheuvel 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
124404a84810SArd Biesheuvel 	 * at least 8 bytes so that we can always access the magic and size
124504a84810SArd Biesheuvel 	 * fields of the FDT header after mapping the first chunk, double check
124604a84810SArd Biesheuvel 	 * here if that is indeed the case.
124761bd93ceSArd Biesheuvel 	 */
124861bd93ceSArd Biesheuvel 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
124961bd93ceSArd Biesheuvel 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
125061bd93ceSArd Biesheuvel 		return NULL;
125161bd93ceSArd Biesheuvel 
125261bd93ceSArd Biesheuvel 	/*
125361bd93ceSArd Biesheuvel 	 * Make sure that the FDT region can be mapped without the need to
125461bd93ceSArd Biesheuvel 	 * allocate additional translation table pages, so that it is safe
1255132233a7SLaura Abbott 	 * to call create_mapping_noalloc() this early.
125661bd93ceSArd Biesheuvel 	 *
125761bd93ceSArd Biesheuvel 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
125861bd93ceSArd Biesheuvel 	 * be in the same PMD as the rest of the fixmap.
125961bd93ceSArd Biesheuvel 	 * On 4k pages, we'll use section mappings for the FDT so we only
126061bd93ceSArd Biesheuvel 	 * have to be in the same PUD.
126161bd93ceSArd Biesheuvel 	 */
126261bd93ceSArd Biesheuvel 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
126361bd93ceSArd Biesheuvel 
1264b433dce0SSuzuki K. Poulose 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
1265b433dce0SSuzuki K. Poulose 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
126661bd93ceSArd Biesheuvel 
1267b433dce0SSuzuki K. Poulose 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
126861bd93ceSArd Biesheuvel 	dt_virt = (void *)dt_virt_base + offset;
126961bd93ceSArd Biesheuvel 
127061bd93ceSArd Biesheuvel 	/* map the first chunk so we can read the size from the header */
1271132233a7SLaura Abbott 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
1272132233a7SLaura Abbott 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
127361bd93ceSArd Biesheuvel 
127404a84810SArd Biesheuvel 	if (fdt_magic(dt_virt) != FDT_MAGIC)
127561bd93ceSArd Biesheuvel 		return NULL;
127661bd93ceSArd Biesheuvel 
1277f80fb3a3SArd Biesheuvel 	*size = fdt_totalsize(dt_virt);
1278f80fb3a3SArd Biesheuvel 	if (*size > MAX_FDT_SIZE)
127961bd93ceSArd Biesheuvel 		return NULL;
128061bd93ceSArd Biesheuvel 
1281f80fb3a3SArd Biesheuvel 	if (offset + *size > SWAPPER_BLOCK_SIZE)
1282132233a7SLaura Abbott 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
1283f80fb3a3SArd Biesheuvel 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
1284f80fb3a3SArd Biesheuvel 
1285f80fb3a3SArd Biesheuvel 	return dt_virt;
1286f80fb3a3SArd Biesheuvel }
1287f80fb3a3SArd Biesheuvel 
12880f472d04SAnshuman Khandual int __init arch_ioremap_p4d_supported(void)
12890f472d04SAnshuman Khandual {
12900f472d04SAnshuman Khandual 	return 0;
12910f472d04SAnshuman Khandual }
12920f472d04SAnshuman Khandual 
1293324420bfSArd Biesheuvel int __init arch_ioremap_pud_supported(void)
1294324420bfSArd Biesheuvel {
12957ba36eccSMark Rutland 	/*
12967ba36eccSMark Rutland 	 * Only 4k granule supports level 1 block mappings.
12977ba36eccSMark Rutland 	 * SW table walks can't handle removal of intermediate entries.
12987ba36eccSMark Rutland 	 */
12997ba36eccSMark Rutland 	return IS_ENABLED(CONFIG_ARM64_4K_PAGES) &&
1300102f45fdSSteven Price 	       !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
1301324420bfSArd Biesheuvel }
1302324420bfSArd Biesheuvel 
1303324420bfSArd Biesheuvel int __init arch_ioremap_pmd_supported(void)
1304324420bfSArd Biesheuvel {
13057ba36eccSMark Rutland 	/* See arch_ioremap_pud_supported() */
1306102f45fdSSteven Price 	return !IS_ENABLED(CONFIG_PTDUMP_DEBUGFS);
1307324420bfSArd Biesheuvel }
1308324420bfSArd Biesheuvel 
130920a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
1310324420bfSArd Biesheuvel {
1311f7f0097aSAnshuman Khandual 	pud_t new_pud = pfn_pud(__phys_to_pfn(phys), mk_pud_sect_prot(prot));
131215122ee2SWill Deacon 
131382034c23SLaura Abbott 	/* Only allow permission changes for now */
131482034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
131582034c23SLaura Abbott 				   pud_val(new_pud)))
131615122ee2SWill Deacon 		return 0;
131715122ee2SWill Deacon 
131887dedf7cSAnshuman Khandual 	VM_BUG_ON(phys & ~PUD_MASK);
131982034c23SLaura Abbott 	set_pud(pudp, new_pud);
1320324420bfSArd Biesheuvel 	return 1;
1321324420bfSArd Biesheuvel }
1322324420bfSArd Biesheuvel 
132320a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
1324324420bfSArd Biesheuvel {
1325f7f0097aSAnshuman Khandual 	pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), mk_pmd_sect_prot(prot));
132615122ee2SWill Deacon 
132782034c23SLaura Abbott 	/* Only allow permission changes for now */
132882034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
132982034c23SLaura Abbott 				   pmd_val(new_pmd)))
133015122ee2SWill Deacon 		return 0;
133115122ee2SWill Deacon 
133287dedf7cSAnshuman Khandual 	VM_BUG_ON(phys & ~PMD_MASK);
133382034c23SLaura Abbott 	set_pmd(pmdp, new_pmd);
1334324420bfSArd Biesheuvel 	return 1;
1335324420bfSArd Biesheuvel }
1336324420bfSArd Biesheuvel 
133720a004e7SWill Deacon int pud_clear_huge(pud_t *pudp)
1338324420bfSArd Biesheuvel {
133920a004e7SWill Deacon 	if (!pud_sect(READ_ONCE(*pudp)))
1340324420bfSArd Biesheuvel 		return 0;
134120a004e7SWill Deacon 	pud_clear(pudp);
1342324420bfSArd Biesheuvel 	return 1;
1343324420bfSArd Biesheuvel }
1344324420bfSArd Biesheuvel 
134520a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp)
1346324420bfSArd Biesheuvel {
134720a004e7SWill Deacon 	if (!pmd_sect(READ_ONCE(*pmdp)))
1348324420bfSArd Biesheuvel 		return 0;
134920a004e7SWill Deacon 	pmd_clear(pmdp);
1350324420bfSArd Biesheuvel 	return 1;
1351324420bfSArd Biesheuvel }
1352b6bdb751SToshi Kani 
1353ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
1354b6bdb751SToshi Kani {
1355ec28bb9cSChintan Pandya 	pte_t *table;
1356ec28bb9cSChintan Pandya 	pmd_t pmd;
1357ec28bb9cSChintan Pandya 
1358ec28bb9cSChintan Pandya 	pmd = READ_ONCE(*pmdp);
1359ec28bb9cSChintan Pandya 
1360fac880c7SMark Rutland 	if (!pmd_table(pmd)) {
13619c006972SWill Deacon 		VM_WARN_ON(1);
1362ec28bb9cSChintan Pandya 		return 1;
1363b6bdb751SToshi Kani 	}
1364b6bdb751SToshi Kani 
1365ec28bb9cSChintan Pandya 	table = pte_offset_kernel(pmdp, addr);
1366ec28bb9cSChintan Pandya 	pmd_clear(pmdp);
1367ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1368ec28bb9cSChintan Pandya 	pte_free_kernel(NULL, table);
1369ec28bb9cSChintan Pandya 	return 1;
1370ec28bb9cSChintan Pandya }
1371ec28bb9cSChintan Pandya 
1372ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1373b6bdb751SToshi Kani {
1374ec28bb9cSChintan Pandya 	pmd_t *table;
1375ec28bb9cSChintan Pandya 	pmd_t *pmdp;
1376ec28bb9cSChintan Pandya 	pud_t pud;
1377ec28bb9cSChintan Pandya 	unsigned long next, end;
1378ec28bb9cSChintan Pandya 
1379ec28bb9cSChintan Pandya 	pud = READ_ONCE(*pudp);
1380ec28bb9cSChintan Pandya 
1381fac880c7SMark Rutland 	if (!pud_table(pud)) {
13829c006972SWill Deacon 		VM_WARN_ON(1);
1383ec28bb9cSChintan Pandya 		return 1;
1384ec28bb9cSChintan Pandya 	}
1385ec28bb9cSChintan Pandya 
1386ec28bb9cSChintan Pandya 	table = pmd_offset(pudp, addr);
1387ec28bb9cSChintan Pandya 	pmdp = table;
1388ec28bb9cSChintan Pandya 	next = addr;
1389ec28bb9cSChintan Pandya 	end = addr + PUD_SIZE;
1390ec28bb9cSChintan Pandya 	do {
1391ec28bb9cSChintan Pandya 		pmd_free_pte_page(pmdp, next);
1392ec28bb9cSChintan Pandya 	} while (pmdp++, next += PMD_SIZE, next != end);
1393ec28bb9cSChintan Pandya 
1394ec28bb9cSChintan Pandya 	pud_clear(pudp);
1395ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1396ec28bb9cSChintan Pandya 	pmd_free(NULL, table);
1397ec28bb9cSChintan Pandya 	return 1;
1398b6bdb751SToshi Kani }
13994ab21506SRobin Murphy 
14008e2d4340SWill Deacon int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
14018e2d4340SWill Deacon {
14028e2d4340SWill Deacon 	return 0;	/* Don't attempt a block mapping */
14038e2d4340SWill Deacon }
14048e2d4340SWill Deacon 
14054ab21506SRobin Murphy #ifdef CONFIG_MEMORY_HOTPLUG
1406bbd6ec60SAnshuman Khandual static void __remove_pgd_mapping(pgd_t *pgdir, unsigned long start, u64 size)
1407bbd6ec60SAnshuman Khandual {
1408bbd6ec60SAnshuman Khandual 	unsigned long end = start + size;
1409bbd6ec60SAnshuman Khandual 
1410bbd6ec60SAnshuman Khandual 	WARN_ON(pgdir != init_mm.pgd);
1411bbd6ec60SAnshuman Khandual 	WARN_ON((start < PAGE_OFFSET) || (end > PAGE_END));
1412bbd6ec60SAnshuman Khandual 
1413bbd6ec60SAnshuman Khandual 	unmap_hotplug_range(start, end, false);
1414bbd6ec60SAnshuman Khandual 	free_empty_tables(start, end, PAGE_OFFSET, PAGE_END);
1415bbd6ec60SAnshuman Khandual }
1416bbd6ec60SAnshuman Khandual 
1417940519f0SMichal Hocko int arch_add_memory(int nid, u64 start, u64 size,
1418f5637d3bSLogan Gunthorpe 		    struct mhp_params *params)
14194ab21506SRobin Murphy {
1420bbd6ec60SAnshuman Khandual 	int ret, flags = 0;
14214ab21506SRobin Murphy 
14224ab21506SRobin Murphy 	if (rodata_full || debug_pagealloc_enabled())
14234ab21506SRobin Murphy 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
14244ab21506SRobin Murphy 
14254ab21506SRobin Murphy 	__create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
1426bfeb022fSLogan Gunthorpe 			     size, params->pgprot, __pgd_pgtable_alloc,
1427bfeb022fSLogan Gunthorpe 			     flags);
14284ab21506SRobin Murphy 
142916993c0fSDan Williams 	memblock_clear_nomap(start, size);
143016993c0fSDan Williams 
1431bbd6ec60SAnshuman Khandual 	ret = __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
1432f5637d3bSLogan Gunthorpe 			   params);
1433bbd6ec60SAnshuman Khandual 	if (ret)
1434bbd6ec60SAnshuman Khandual 		__remove_pgd_mapping(swapper_pg_dir,
1435bbd6ec60SAnshuman Khandual 				     __phys_to_virt(start), size);
1436bbd6ec60SAnshuman Khandual 	return ret;
14374ab21506SRobin Murphy }
1438bbd6ec60SAnshuman Khandual 
143922eb6346SDavid Hildenbrand void arch_remove_memory(int nid, u64 start, u64 size,
144022eb6346SDavid Hildenbrand 			struct vmem_altmap *altmap)
144122eb6346SDavid Hildenbrand {
144222eb6346SDavid Hildenbrand 	unsigned long start_pfn = start >> PAGE_SHIFT;
144322eb6346SDavid Hildenbrand 	unsigned long nr_pages = size >> PAGE_SHIFT;
144422eb6346SDavid Hildenbrand 
1445feee6b29SDavid Hildenbrand 	__remove_pages(start_pfn, nr_pages, altmap);
1446bbd6ec60SAnshuman Khandual 	__remove_pgd_mapping(swapper_pg_dir, __phys_to_virt(start), size);
144722eb6346SDavid Hildenbrand }
1448bbd6ec60SAnshuman Khandual 
1449bbd6ec60SAnshuman Khandual /*
1450bbd6ec60SAnshuman Khandual  * This memory hotplug notifier helps prevent boot memory from being
1451bbd6ec60SAnshuman Khandual  * inadvertently removed as it blocks pfn range offlining process in
1452bbd6ec60SAnshuman Khandual  * __offline_pages(). Hence this prevents both offlining as well as
1453bbd6ec60SAnshuman Khandual  * removal process for boot memory which is initially always online.
1454bbd6ec60SAnshuman Khandual  * In future if and when boot memory could be removed, this notifier
1455bbd6ec60SAnshuman Khandual  * should be dropped and free_hotplug_page_range() should handle any
1456bbd6ec60SAnshuman Khandual  * reserved pages allocated during boot.
1457bbd6ec60SAnshuman Khandual  */
1458bbd6ec60SAnshuman Khandual static int prevent_bootmem_remove_notifier(struct notifier_block *nb,
1459bbd6ec60SAnshuman Khandual 					   unsigned long action, void *data)
1460bbd6ec60SAnshuman Khandual {
1461bbd6ec60SAnshuman Khandual 	struct mem_section *ms;
1462bbd6ec60SAnshuman Khandual 	struct memory_notify *arg = data;
1463bbd6ec60SAnshuman Khandual 	unsigned long end_pfn = arg->start_pfn + arg->nr_pages;
1464bbd6ec60SAnshuman Khandual 	unsigned long pfn = arg->start_pfn;
1465bbd6ec60SAnshuman Khandual 
1466bbd6ec60SAnshuman Khandual 	if (action != MEM_GOING_OFFLINE)
1467bbd6ec60SAnshuman Khandual 		return NOTIFY_OK;
1468bbd6ec60SAnshuman Khandual 
1469bbd6ec60SAnshuman Khandual 	for (; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
1470bbd6ec60SAnshuman Khandual 		ms = __pfn_to_section(pfn);
1471bbd6ec60SAnshuman Khandual 		if (early_section(ms))
1472bbd6ec60SAnshuman Khandual 			return NOTIFY_BAD;
1473bbd6ec60SAnshuman Khandual 	}
1474bbd6ec60SAnshuman Khandual 	return NOTIFY_OK;
1475bbd6ec60SAnshuman Khandual }
1476bbd6ec60SAnshuman Khandual 
1477bbd6ec60SAnshuman Khandual static struct notifier_block prevent_bootmem_remove_nb = {
1478bbd6ec60SAnshuman Khandual 	.notifier_call = prevent_bootmem_remove_notifier,
1479bbd6ec60SAnshuman Khandual };
1480bbd6ec60SAnshuman Khandual 
1481bbd6ec60SAnshuman Khandual static int __init prevent_bootmem_remove_init(void)
1482bbd6ec60SAnshuman Khandual {
1483bbd6ec60SAnshuman Khandual 	return register_memory_notifier(&prevent_bootmem_remove_nb);
1484bbd6ec60SAnshuman Khandual }
1485bbd6ec60SAnshuman Khandual device_initcall(prevent_bootmem_remove_init);
148622eb6346SDavid Hildenbrand #endif
1487