xref: /openbmc/linux/arch/arm64/mm/mmu.c (revision ecc3e771f4ca98c52a072e41804434b4979bdf84)
1c1cc1552SCatalin Marinas /*
2c1cc1552SCatalin Marinas  * Based on arch/arm/mm/mmu.c
3c1cc1552SCatalin Marinas  *
4c1cc1552SCatalin Marinas  * Copyright (C) 1995-2005 Russell King
5c1cc1552SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
6c1cc1552SCatalin Marinas  *
7c1cc1552SCatalin Marinas  * This program is free software; you can redistribute it and/or modify
8c1cc1552SCatalin Marinas  * it under the terms of the GNU General Public License version 2 as
9c1cc1552SCatalin Marinas  * published by the Free Software Foundation.
10c1cc1552SCatalin Marinas  *
11c1cc1552SCatalin Marinas  * This program is distributed in the hope that it will be useful,
12c1cc1552SCatalin Marinas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c1cc1552SCatalin Marinas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14c1cc1552SCatalin Marinas  * GNU General Public License for more details.
15c1cc1552SCatalin Marinas  *
16c1cc1552SCatalin Marinas  * You should have received a copy of the GNU General Public License
17c1cc1552SCatalin Marinas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18c1cc1552SCatalin Marinas  */
19c1cc1552SCatalin Marinas 
205a9e3e15SJisheng Zhang #include <linux/cache.h>
21c1cc1552SCatalin Marinas #include <linux/export.h>
22c1cc1552SCatalin Marinas #include <linux/kernel.h>
23c1cc1552SCatalin Marinas #include <linux/errno.h>
24c1cc1552SCatalin Marinas #include <linux/init.h>
2598d2e153STakahiro Akashi #include <linux/ioport.h>
2698d2e153STakahiro Akashi #include <linux/kexec.h>
2761bd93ceSArd Biesheuvel #include <linux/libfdt.h>
28c1cc1552SCatalin Marinas #include <linux/mman.h>
29c1cc1552SCatalin Marinas #include <linux/nodemask.h>
30c1cc1552SCatalin Marinas #include <linux/memblock.h>
31c1cc1552SCatalin Marinas #include <linux/fs.h>
322475ff9dSCatalin Marinas #include <linux/io.h>
332077be67SLaura Abbott #include <linux/mm.h>
346efd8499STobias Klauser #include <linux/vmalloc.h>
35c1cc1552SCatalin Marinas 
3621ab99c2SMark Rutland #include <asm/barrier.h>
37c1cc1552SCatalin Marinas #include <asm/cputype.h>
38af86e597SLaura Abbott #include <asm/fixmap.h>
39068a17a5SMark Rutland #include <asm/kasan.h>
40b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h>
41c1cc1552SCatalin Marinas #include <asm/sections.h>
42c1cc1552SCatalin Marinas #include <asm/setup.h>
43c1cc1552SCatalin Marinas #include <asm/sizes.h>
44c1cc1552SCatalin Marinas #include <asm/tlb.h>
45c1cc1552SCatalin Marinas #include <asm/mmu_context.h>
461404d6f1SLaura Abbott #include <asm/ptdump.h>
47ec28bb9cSChintan Pandya #include <asm/tlbflush.h>
48c1cc1552SCatalin Marinas 
49c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS	BIT(0)
50d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS	BIT(1)
51c0951366SArd Biesheuvel 
52dd006da2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
53fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
5467e7fdfcSSteve Capper u64 vabits_user __ro_after_init;
554a1daf29SWill Deacon EXPORT_SYMBOL(vabits_user);
56dd006da2SArd Biesheuvel 
575a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init;
58a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset);
59a7f8de16SArd Biesheuvel 
60c1cc1552SCatalin Marinas /*
61c1cc1552SCatalin Marinas  * Empty_zero_page is a special page that is used for zero-initialized data
62c1cc1552SCatalin Marinas  * and COW.
63c1cc1552SCatalin Marinas  */
645227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
65c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page);
66c1cc1552SCatalin Marinas 
67f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
68f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
69f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
70f9040773SArd Biesheuvel 
712330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock);
722330b7caSJun Yao 
732330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
742330b7caSJun Yao {
752330b7caSJun Yao 	pgd_t *fixmap_pgdp;
762330b7caSJun Yao 
772330b7caSJun Yao 	spin_lock(&swapper_pgdir_lock);
7826a6f87eSJames Morse 	fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
792330b7caSJun Yao 	WRITE_ONCE(*fixmap_pgdp, pgd);
802330b7caSJun Yao 	/*
812330b7caSJun Yao 	 * We need dsb(ishst) here to ensure the page-table-walker sees
822330b7caSJun Yao 	 * our new entry before set_p?d() returns. The fixmap's
832330b7caSJun Yao 	 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
842330b7caSJun Yao 	 */
852330b7caSJun Yao 	pgd_clear_fixmap();
862330b7caSJun Yao 	spin_unlock(&swapper_pgdir_lock);
872330b7caSJun Yao }
882330b7caSJun Yao 
89c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
90c1cc1552SCatalin Marinas 			      unsigned long size, pgprot_t vma_prot)
91c1cc1552SCatalin Marinas {
92c1cc1552SCatalin Marinas 	if (!pfn_valid(pfn))
93c1cc1552SCatalin Marinas 		return pgprot_noncached(vma_prot);
94c1cc1552SCatalin Marinas 	else if (file->f_flags & O_SYNC)
95c1cc1552SCatalin Marinas 		return pgprot_writecombine(vma_prot);
96c1cc1552SCatalin Marinas 	return vma_prot;
97c1cc1552SCatalin Marinas }
98c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot);
99c1cc1552SCatalin Marinas 
100f4710445SMark Rutland static phys_addr_t __init early_pgtable_alloc(void)
101c1cc1552SCatalin Marinas {
1027142392dSSuzuki K. Poulose 	phys_addr_t phys;
1037142392dSSuzuki K. Poulose 	void *ptr;
1047142392dSSuzuki K. Poulose 
1059a8dd708SMike Rapoport 	phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
106*ecc3e771SMike Rapoport 	if (!phys)
107*ecc3e771SMike Rapoport 		panic("Failed to allocate page table page\n");
108f4710445SMark Rutland 
109f4710445SMark Rutland 	/*
110f4710445SMark Rutland 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
111f4710445SMark Rutland 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
112f4710445SMark Rutland 	 * any level of table.
113f4710445SMark Rutland 	 */
114f4710445SMark Rutland 	ptr = pte_set_fixmap(phys);
115f4710445SMark Rutland 
11621ab99c2SMark Rutland 	memset(ptr, 0, PAGE_SIZE);
11721ab99c2SMark Rutland 
118f4710445SMark Rutland 	/*
119f4710445SMark Rutland 	 * Implicit barriers also ensure the zeroed page is visible to the page
120f4710445SMark Rutland 	 * table walker
121f4710445SMark Rutland 	 */
122f4710445SMark Rutland 	pte_clear_fixmap();
123f4710445SMark Rutland 
124f4710445SMark Rutland 	return phys;
125c1cc1552SCatalin Marinas }
126c1cc1552SCatalin Marinas 
127e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new)
128e98216b5SArd Biesheuvel {
129e98216b5SArd Biesheuvel 	/*
130e98216b5SArd Biesheuvel 	 * The following mapping attributes may be updated in live
131e98216b5SArd Biesheuvel 	 * kernel mappings without the need for break-before-make.
132e98216b5SArd Biesheuvel 	 */
133753e8abcSArd Biesheuvel 	static const pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
134e98216b5SArd Biesheuvel 
135141d1497SArd Biesheuvel 	/* creating or taking down mappings is always safe */
136141d1497SArd Biesheuvel 	if (old == 0 || new == 0)
137141d1497SArd Biesheuvel 		return true;
138141d1497SArd Biesheuvel 
139141d1497SArd Biesheuvel 	/* live contiguous mappings may not be manipulated at all */
140141d1497SArd Biesheuvel 	if ((old | new) & PTE_CONT)
141141d1497SArd Biesheuvel 		return false;
142141d1497SArd Biesheuvel 
143753e8abcSArd Biesheuvel 	/* Transitioning from Non-Global to Global is unsafe */
144753e8abcSArd Biesheuvel 	if (old & ~new & PTE_NG)
145753e8abcSArd Biesheuvel 		return false;
1464e602056SWill Deacon 
147141d1497SArd Biesheuvel 	return ((old ^ new) & ~mask) == 0;
148e98216b5SArd Biesheuvel }
149e98216b5SArd Biesheuvel 
15020a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
151d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot)
152c1cc1552SCatalin Marinas {
15320a004e7SWill Deacon 	pte_t *ptep;
154c1cc1552SCatalin Marinas 
15520a004e7SWill Deacon 	ptep = pte_set_fixmap_offset(pmdp, addr);
156c1cc1552SCatalin Marinas 	do {
15720a004e7SWill Deacon 		pte_t old_pte = READ_ONCE(*ptep);
158e98216b5SArd Biesheuvel 
15920a004e7SWill Deacon 		set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
160e98216b5SArd Biesheuvel 
161e98216b5SArd Biesheuvel 		/*
162e98216b5SArd Biesheuvel 		 * After the PTE entry has been populated once, we
163e98216b5SArd Biesheuvel 		 * only allow updates to the permission attributes.
164e98216b5SArd Biesheuvel 		 */
16520a004e7SWill Deacon 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
16620a004e7SWill Deacon 					      READ_ONCE(pte_val(*ptep))));
167e98216b5SArd Biesheuvel 
168e393cf40SArd Biesheuvel 		phys += PAGE_SIZE;
16920a004e7SWill Deacon 	} while (ptep++, addr += PAGE_SIZE, addr != end);
170f4710445SMark Rutland 
171f4710445SMark Rutland 	pte_clear_fixmap();
172c1cc1552SCatalin Marinas }
173c1cc1552SCatalin Marinas 
17420a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
175d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
176d27cfa1fSArd Biesheuvel 				pgprot_t prot,
17753e1b329SArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void),
178c0951366SArd Biesheuvel 				int flags)
179c1cc1552SCatalin Marinas {
180c1cc1552SCatalin Marinas 	unsigned long next;
18120a004e7SWill Deacon 	pmd_t pmd = READ_ONCE(*pmdp);
182c1cc1552SCatalin Marinas 
18320a004e7SWill Deacon 	BUG_ON(pmd_sect(pmd));
18420a004e7SWill Deacon 	if (pmd_none(pmd)) {
185d27cfa1fSArd Biesheuvel 		phys_addr_t pte_phys;
186132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
187d27cfa1fSArd Biesheuvel 		pte_phys = pgtable_alloc();
18820a004e7SWill Deacon 		__pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
18920a004e7SWill Deacon 		pmd = READ_ONCE(*pmdp);
190c1cc1552SCatalin Marinas 	}
19120a004e7SWill Deacon 	BUG_ON(pmd_bad(pmd));
192d27cfa1fSArd Biesheuvel 
193d27cfa1fSArd Biesheuvel 	do {
194d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
195d27cfa1fSArd Biesheuvel 
196d27cfa1fSArd Biesheuvel 		next = pte_cont_addr_end(addr, end);
197d27cfa1fSArd Biesheuvel 
198d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
199d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
200d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
201d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
202d27cfa1fSArd Biesheuvel 
20320a004e7SWill Deacon 		init_pte(pmdp, addr, next, phys, __prot);
204d27cfa1fSArd Biesheuvel 
205d27cfa1fSArd Biesheuvel 		phys += next - addr;
206d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
207d27cfa1fSArd Biesheuvel }
208d27cfa1fSArd Biesheuvel 
20920a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
210d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot,
211d27cfa1fSArd Biesheuvel 		     phys_addr_t (*pgtable_alloc)(void), int flags)
212d27cfa1fSArd Biesheuvel {
213d27cfa1fSArd Biesheuvel 	unsigned long next;
21420a004e7SWill Deacon 	pmd_t *pmdp;
215c1cc1552SCatalin Marinas 
21620a004e7SWill Deacon 	pmdp = pmd_set_fixmap_offset(pudp, addr);
217c1cc1552SCatalin Marinas 	do {
21820a004e7SWill Deacon 		pmd_t old_pmd = READ_ONCE(*pmdp);
219e98216b5SArd Biesheuvel 
220c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
221e98216b5SArd Biesheuvel 
222c1cc1552SCatalin Marinas 		/* try section mapping first */
22383863f25SLaura Abbott 		if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
224c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
22520a004e7SWill Deacon 			pmd_set_huge(pmdp, phys, prot);
226e98216b5SArd Biesheuvel 
227a55f9929SCatalin Marinas 			/*
228e98216b5SArd Biesheuvel 			 * After the PMD entry has been populated once, we
229e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
230a55f9929SCatalin Marinas 			 */
231e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
23220a004e7SWill Deacon 						      READ_ONCE(pmd_val(*pmdp))));
233a55f9929SCatalin Marinas 		} else {
23420a004e7SWill Deacon 			alloc_init_cont_pte(pmdp, addr, next, phys, prot,
235d27cfa1fSArd Biesheuvel 					    pgtable_alloc, flags);
236e98216b5SArd Biesheuvel 
237e98216b5SArd Biesheuvel 			BUG_ON(pmd_val(old_pmd) != 0 &&
23820a004e7SWill Deacon 			       pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
239a55f9929SCatalin Marinas 		}
240c1cc1552SCatalin Marinas 		phys += next - addr;
24120a004e7SWill Deacon 	} while (pmdp++, addr = next, addr != end);
242f4710445SMark Rutland 
243f4710445SMark Rutland 	pmd_clear_fixmap();
244c1cc1552SCatalin Marinas }
245c1cc1552SCatalin Marinas 
24620a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
247d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
248d27cfa1fSArd Biesheuvel 				pgprot_t prot,
249d27cfa1fSArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void), int flags)
250d27cfa1fSArd Biesheuvel {
251d27cfa1fSArd Biesheuvel 	unsigned long next;
25220a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
253d27cfa1fSArd Biesheuvel 
254d27cfa1fSArd Biesheuvel 	/*
255d27cfa1fSArd Biesheuvel 	 * Check for initial section mappings in the pgd/pud.
256d27cfa1fSArd Biesheuvel 	 */
25720a004e7SWill Deacon 	BUG_ON(pud_sect(pud));
25820a004e7SWill Deacon 	if (pud_none(pud)) {
259d27cfa1fSArd Biesheuvel 		phys_addr_t pmd_phys;
260d27cfa1fSArd Biesheuvel 		BUG_ON(!pgtable_alloc);
261d27cfa1fSArd Biesheuvel 		pmd_phys = pgtable_alloc();
26220a004e7SWill Deacon 		__pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
26320a004e7SWill Deacon 		pud = READ_ONCE(*pudp);
264d27cfa1fSArd Biesheuvel 	}
26520a004e7SWill Deacon 	BUG_ON(pud_bad(pud));
266d27cfa1fSArd Biesheuvel 
267d27cfa1fSArd Biesheuvel 	do {
268d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
269d27cfa1fSArd Biesheuvel 
270d27cfa1fSArd Biesheuvel 		next = pmd_cont_addr_end(addr, end);
271d27cfa1fSArd Biesheuvel 
272d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
273d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
274d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
275d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
276d27cfa1fSArd Biesheuvel 
27720a004e7SWill Deacon 		init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
278d27cfa1fSArd Biesheuvel 
279d27cfa1fSArd Biesheuvel 		phys += next - addr;
280d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
281d27cfa1fSArd Biesheuvel }
282d27cfa1fSArd Biesheuvel 
283da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next,
284da141706SLaura Abbott 			unsigned long phys)
285da141706SLaura Abbott {
286da141706SLaura Abbott 	if (PAGE_SHIFT != 12)
287da141706SLaura Abbott 		return false;
288da141706SLaura Abbott 
289da141706SLaura Abbott 	if (((addr | next | phys) & ~PUD_MASK) != 0)
290da141706SLaura Abbott 		return false;
291da141706SLaura Abbott 
292da141706SLaura Abbott 	return true;
293da141706SLaura Abbott }
294da141706SLaura Abbott 
29520a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
296da141706SLaura Abbott 			   phys_addr_t phys, pgprot_t prot,
29753e1b329SArd Biesheuvel 			   phys_addr_t (*pgtable_alloc)(void),
298c0951366SArd Biesheuvel 			   int flags)
299c1cc1552SCatalin Marinas {
300c1cc1552SCatalin Marinas 	unsigned long next;
30120a004e7SWill Deacon 	pud_t *pudp;
30220a004e7SWill Deacon 	pgd_t pgd = READ_ONCE(*pgdp);
303c1cc1552SCatalin Marinas 
30420a004e7SWill Deacon 	if (pgd_none(pgd)) {
305132233a7SLaura Abbott 		phys_addr_t pud_phys;
306132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
307132233a7SLaura Abbott 		pud_phys = pgtable_alloc();
30820a004e7SWill Deacon 		__pgd_populate(pgdp, pud_phys, PUD_TYPE_TABLE);
30920a004e7SWill Deacon 		pgd = READ_ONCE(*pgdp);
310c79b954bSJungseok Lee 	}
31120a004e7SWill Deacon 	BUG_ON(pgd_bad(pgd));
312c79b954bSJungseok Lee 
31320a004e7SWill Deacon 	pudp = pud_set_fixmap_offset(pgdp, addr);
314c1cc1552SCatalin Marinas 	do {
31520a004e7SWill Deacon 		pud_t old_pud = READ_ONCE(*pudp);
316e98216b5SArd Biesheuvel 
317c1cc1552SCatalin Marinas 		next = pud_addr_end(addr, end);
318206a2a73SSteve Capper 
319206a2a73SSteve Capper 		/*
320206a2a73SSteve Capper 		 * For 4K granule only, attempt to put down a 1GB block
321206a2a73SSteve Capper 		 */
322c0951366SArd Biesheuvel 		if (use_1G_block(addr, next, phys) &&
323c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
32420a004e7SWill Deacon 			pud_set_huge(pudp, phys, prot);
325206a2a73SSteve Capper 
326206a2a73SSteve Capper 			/*
327e98216b5SArd Biesheuvel 			 * After the PUD entry has been populated once, we
328e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
329206a2a73SSteve Capper 			 */
330e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
33120a004e7SWill Deacon 						      READ_ONCE(pud_val(*pudp))));
332206a2a73SSteve Capper 		} else {
33320a004e7SWill Deacon 			alloc_init_cont_pmd(pudp, addr, next, phys, prot,
334c0951366SArd Biesheuvel 					    pgtable_alloc, flags);
335e98216b5SArd Biesheuvel 
336e98216b5SArd Biesheuvel 			BUG_ON(pud_val(old_pud) != 0 &&
33720a004e7SWill Deacon 			       pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
338206a2a73SSteve Capper 		}
339c1cc1552SCatalin Marinas 		phys += next - addr;
34020a004e7SWill Deacon 	} while (pudp++, addr = next, addr != end);
341f4710445SMark Rutland 
342f4710445SMark Rutland 	pud_clear_fixmap();
343c1cc1552SCatalin Marinas }
344c1cc1552SCatalin Marinas 
34540f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
34640f87d31SArd Biesheuvel 				 unsigned long virt, phys_addr_t size,
34740f87d31SArd Biesheuvel 				 pgprot_t prot,
34853e1b329SArd Biesheuvel 				 phys_addr_t (*pgtable_alloc)(void),
349c0951366SArd Biesheuvel 				 int flags)
350c1cc1552SCatalin Marinas {
351c1cc1552SCatalin Marinas 	unsigned long addr, length, end, next;
35220a004e7SWill Deacon 	pgd_t *pgdp = pgd_offset_raw(pgdir, virt);
353c1cc1552SCatalin Marinas 
354cc5d2b3bSMark Rutland 	/*
355cc5d2b3bSMark Rutland 	 * If the virtual and physical address don't have the same offset
356cc5d2b3bSMark Rutland 	 * within a page, we cannot map the region as the caller expects.
357cc5d2b3bSMark Rutland 	 */
358cc5d2b3bSMark Rutland 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
359cc5d2b3bSMark Rutland 		return;
360cc5d2b3bSMark Rutland 
3619c4e08a3SMark Rutland 	phys &= PAGE_MASK;
362c1cc1552SCatalin Marinas 	addr = virt & PAGE_MASK;
363c1cc1552SCatalin Marinas 	length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
364c1cc1552SCatalin Marinas 
365c1cc1552SCatalin Marinas 	end = addr + length;
366c1cc1552SCatalin Marinas 	do {
367c1cc1552SCatalin Marinas 		next = pgd_addr_end(addr, end);
36820a004e7SWill Deacon 		alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
369c0951366SArd Biesheuvel 			       flags);
370c1cc1552SCatalin Marinas 		phys += next - addr;
37120a004e7SWill Deacon 	} while (pgdp++, addr = next, addr != end);
372c1cc1552SCatalin Marinas }
373c1cc1552SCatalin Marinas 
3741378dc3dSArd Biesheuvel static phys_addr_t pgd_pgtable_alloc(void)
375da141706SLaura Abbott {
37621ab99c2SMark Rutland 	void *ptr = (void *)__get_free_page(PGALLOC_GFP);
3771378dc3dSArd Biesheuvel 	if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
3781378dc3dSArd Biesheuvel 		BUG();
37921ab99c2SMark Rutland 
38021ab99c2SMark Rutland 	/* Ensure the zeroed page is visible to the page table walker */
38121ab99c2SMark Rutland 	dsb(ishst);
382f4710445SMark Rutland 	return __pa(ptr);
383da141706SLaura Abbott }
384da141706SLaura Abbott 
385132233a7SLaura Abbott /*
386132233a7SLaura Abbott  * This function can only be used to modify existing table entries,
387132233a7SLaura Abbott  * without allocating new levels of table. Note that this permits the
388132233a7SLaura Abbott  * creation of new section or page entries.
389132233a7SLaura Abbott  */
390132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
391da141706SLaura Abbott 				  phys_addr_t size, pgprot_t prot)
392d7ecbddfSMark Salter {
393d7ecbddfSMark Salter 	if (virt < VMALLOC_START) {
394d7ecbddfSMark Salter 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
395d7ecbddfSMark Salter 			&phys, virt);
396d7ecbddfSMark Salter 		return;
397d7ecbddfSMark Salter 	}
398d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
399d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
400d7ecbddfSMark Salter }
401d7ecbddfSMark Salter 
4028ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
4038ce837ceSArd Biesheuvel 			       unsigned long virt, phys_addr_t size,
404f14c66ceSArd Biesheuvel 			       pgprot_t prot, bool page_mappings_only)
4058ce837ceSArd Biesheuvel {
406c0951366SArd Biesheuvel 	int flags = 0;
407c0951366SArd Biesheuvel 
4081378dc3dSArd Biesheuvel 	BUG_ON(mm == &init_mm);
4091378dc3dSArd Biesheuvel 
410c0951366SArd Biesheuvel 	if (page_mappings_only)
411d27cfa1fSArd Biesheuvel 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
412c0951366SArd Biesheuvel 
41311509a30SMark Rutland 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
414c0951366SArd Biesheuvel 			     pgd_pgtable_alloc, flags);
415d7ecbddfSMark Salter }
416d7ecbddfSMark Salter 
417aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
418da141706SLaura Abbott 				phys_addr_t size, pgprot_t prot)
419da141706SLaura Abbott {
420da141706SLaura Abbott 	if (virt < VMALLOC_START) {
421aa8c09beSArd Biesheuvel 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
422da141706SLaura Abbott 			&phys, virt);
423da141706SLaura Abbott 		return;
424da141706SLaura Abbott 	}
425da141706SLaura Abbott 
426d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
427d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
428aa8c09beSArd Biesheuvel 
429aa8c09beSArd Biesheuvel 	/* flush the TLBs after updating live kernel mappings */
430aa8c09beSArd Biesheuvel 	flush_tlb_kernel_range(virt, virt + size);
431da141706SLaura Abbott }
432da141706SLaura Abbott 
43320a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
43498d2e153STakahiro Akashi 				  phys_addr_t end, pgprot_t prot, int flags)
435da141706SLaura Abbott {
43620a004e7SWill Deacon 	__create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
43798d2e153STakahiro Akashi 			     prot, early_pgtable_alloc, flags);
438da141706SLaura Abbott }
439da141706SLaura Abbott 
4405ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void)
4415ea5306cSArd Biesheuvel {
4425ea5306cSArd Biesheuvel 	/*
4435ea5306cSArd Biesheuvel 	 * Remove the write permissions from the linear alias of .text/.rodata
4445ea5306cSArd Biesheuvel 	 */
4455ea5306cSArd Biesheuvel 	update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
4465ea5306cSArd Biesheuvel 			    (unsigned long)__init_begin - (unsigned long)_text,
4475ea5306cSArd Biesheuvel 			    PAGE_KERNEL_RO);
4485ea5306cSArd Biesheuvel }
4495ea5306cSArd Biesheuvel 
45020a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp)
451c1cc1552SCatalin Marinas {
45298d2e153STakahiro Akashi 	phys_addr_t kernel_start = __pa_symbol(_text);
45398d2e153STakahiro Akashi 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
454c1cc1552SCatalin Marinas 	struct memblock_region *reg;
45598d2e153STakahiro Akashi 	int flags = 0;
45698d2e153STakahiro Akashi 
457c55191e9SArd Biesheuvel 	if (rodata_full || debug_pagealloc_enabled())
45898d2e153STakahiro Akashi 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
45998d2e153STakahiro Akashi 
46098d2e153STakahiro Akashi 	/*
46198d2e153STakahiro Akashi 	 * Take care not to create a writable alias for the
46298d2e153STakahiro Akashi 	 * read-only text and rodata sections of the kernel image.
46398d2e153STakahiro Akashi 	 * So temporarily mark them as NOMAP to skip mappings in
46498d2e153STakahiro Akashi 	 * the following for-loop
46598d2e153STakahiro Akashi 	 */
46698d2e153STakahiro Akashi 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
46798d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
46898d2e153STakahiro Akashi 	if (crashk_res.end)
46998d2e153STakahiro Akashi 		memblock_mark_nomap(crashk_res.start,
47098d2e153STakahiro Akashi 				    resource_size(&crashk_res));
47198d2e153STakahiro Akashi #endif
472f6bc87c3SSteve Capper 
473c1cc1552SCatalin Marinas 	/* map all the memory banks */
474c1cc1552SCatalin Marinas 	for_each_memblock(memory, reg) {
475c1cc1552SCatalin Marinas 		phys_addr_t start = reg->base;
476c1cc1552SCatalin Marinas 		phys_addr_t end = start + reg->size;
477c1cc1552SCatalin Marinas 
478c1cc1552SCatalin Marinas 		if (start >= end)
479c1cc1552SCatalin Marinas 			break;
48068709f45SArd Biesheuvel 		if (memblock_is_nomap(reg))
48168709f45SArd Biesheuvel 			continue;
482c1cc1552SCatalin Marinas 
48320a004e7SWill Deacon 		__map_memblock(pgdp, start, end, PAGE_KERNEL, flags);
484c1cc1552SCatalin Marinas 	}
48598d2e153STakahiro Akashi 
48698d2e153STakahiro Akashi 	/*
48798d2e153STakahiro Akashi 	 * Map the linear alias of the [_text, __init_begin) interval
48898d2e153STakahiro Akashi 	 * as non-executable now, and remove the write permission in
48998d2e153STakahiro Akashi 	 * mark_linear_text_alias_ro() below (which will be called after
49098d2e153STakahiro Akashi 	 * alternative patching has completed). This makes the contents
49198d2e153STakahiro Akashi 	 * of the region accessible to subsystems such as hibernate,
49298d2e153STakahiro Akashi 	 * but protects it from inadvertent modification or execution.
49398d2e153STakahiro Akashi 	 * Note that contiguous mappings cannot be remapped in this way,
49498d2e153STakahiro Akashi 	 * so we should avoid them here.
49598d2e153STakahiro Akashi 	 */
49620a004e7SWill Deacon 	__map_memblock(pgdp, kernel_start, kernel_end,
49798d2e153STakahiro Akashi 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
49898d2e153STakahiro Akashi 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
49998d2e153STakahiro Akashi 
50098d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
50198d2e153STakahiro Akashi 	/*
50298d2e153STakahiro Akashi 	 * Use page-level mappings here so that we can shrink the region
50398d2e153STakahiro Akashi 	 * in page granularity and put back unused memory to buddy system
50498d2e153STakahiro Akashi 	 * through /sys/kernel/kexec_crash_size interface.
50598d2e153STakahiro Akashi 	 */
50698d2e153STakahiro Akashi 	if (crashk_res.end) {
50720a004e7SWill Deacon 		__map_memblock(pgdp, crashk_res.start, crashk_res.end + 1,
50898d2e153STakahiro Akashi 			       PAGE_KERNEL,
50998d2e153STakahiro Akashi 			       NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
51098d2e153STakahiro Akashi 		memblock_clear_nomap(crashk_res.start,
51198d2e153STakahiro Akashi 				     resource_size(&crashk_res));
51298d2e153STakahiro Akashi 	}
51398d2e153STakahiro Akashi #endif
514c1cc1552SCatalin Marinas }
515c1cc1552SCatalin Marinas 
516da141706SLaura Abbott void mark_rodata_ro(void)
517da141706SLaura Abbott {
5182f39b5f9SJeremy Linton 	unsigned long section_size;
519f9040773SArd Biesheuvel 
5202f39b5f9SJeremy Linton 	/*
5219fdc14c5SArd Biesheuvel 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
5229fdc14c5SArd Biesheuvel 	 * to cover NOTES and EXCEPTION_TABLE.
5232f39b5f9SJeremy Linton 	 */
5249fdc14c5SArd Biesheuvel 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
525aa8c09beSArd Biesheuvel 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
5262f39b5f9SJeremy Linton 			    section_size, PAGE_KERNEL_RO);
527e98216b5SArd Biesheuvel 
5281404d6f1SLaura Abbott 	debug_checkwx();
529da141706SLaura Abbott }
530da141706SLaura Abbott 
53120a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
532d27cfa1fSArd Biesheuvel 				      pgprot_t prot, struct vm_struct *vma,
53392bbd16eSWill Deacon 				      int flags, unsigned long vm_flags)
534068a17a5SMark Rutland {
5352077be67SLaura Abbott 	phys_addr_t pa_start = __pa_symbol(va_start);
536068a17a5SMark Rutland 	unsigned long size = va_end - va_start;
537068a17a5SMark Rutland 
538068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(pa_start));
539068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(size));
540068a17a5SMark Rutland 
54120a004e7SWill Deacon 	__create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
542d27cfa1fSArd Biesheuvel 			     early_pgtable_alloc, flags);
543f9040773SArd Biesheuvel 
54492bbd16eSWill Deacon 	if (!(vm_flags & VM_NO_GUARD))
54592bbd16eSWill Deacon 		size += PAGE_SIZE;
54692bbd16eSWill Deacon 
547f9040773SArd Biesheuvel 	vma->addr	= va_start;
548f9040773SArd Biesheuvel 	vma->phys_addr	= pa_start;
549f9040773SArd Biesheuvel 	vma->size	= size;
55092bbd16eSWill Deacon 	vma->flags	= VM_MAP | vm_flags;
551f9040773SArd Biesheuvel 	vma->caller	= __builtin_return_address(0);
552f9040773SArd Biesheuvel 
553f9040773SArd Biesheuvel 	vm_area_add_early(vma);
554068a17a5SMark Rutland }
555068a17a5SMark Rutland 
55628b066daSArd Biesheuvel static int __init parse_rodata(char *arg)
55728b066daSArd Biesheuvel {
558c55191e9SArd Biesheuvel 	int ret = strtobool(arg, &rodata_enabled);
559c55191e9SArd Biesheuvel 	if (!ret) {
560c55191e9SArd Biesheuvel 		rodata_full = false;
561c55191e9SArd Biesheuvel 		return 0;
562c55191e9SArd Biesheuvel 	}
563c55191e9SArd Biesheuvel 
564c55191e9SArd Biesheuvel 	/* permit 'full' in addition to boolean options */
565c55191e9SArd Biesheuvel 	if (strcmp(arg, "full"))
566c55191e9SArd Biesheuvel 		return -EINVAL;
567c55191e9SArd Biesheuvel 
568c55191e9SArd Biesheuvel 	rodata_enabled = true;
569c55191e9SArd Biesheuvel 	rodata_full = true;
570c55191e9SArd Biesheuvel 	return 0;
57128b066daSArd Biesheuvel }
57228b066daSArd Biesheuvel early_param("rodata", parse_rodata);
57328b066daSArd Biesheuvel 
57451a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
57551a0048bSWill Deacon static int __init map_entry_trampoline(void)
57651a0048bSWill Deacon {
57751a0048bSWill Deacon 	pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
57851a0048bSWill Deacon 	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
57951a0048bSWill Deacon 
58051a0048bSWill Deacon 	/* The trampoline is always mapped and can therefore be global */
58151a0048bSWill Deacon 	pgprot_val(prot) &= ~PTE_NG;
58251a0048bSWill Deacon 
58351a0048bSWill Deacon 	/* Map only the text into the trampoline page table */
58451a0048bSWill Deacon 	memset(tramp_pg_dir, 0, PGD_SIZE);
58551a0048bSWill Deacon 	__create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE,
58651a0048bSWill Deacon 			     prot, pgd_pgtable_alloc, 0);
58751a0048bSWill Deacon 
5886c27c408SWill Deacon 	/* Map both the text and data into the kernel page table */
58951a0048bSWill Deacon 	__set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot);
5906c27c408SWill Deacon 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
5916c27c408SWill Deacon 		extern char __entry_tramp_data_start[];
5926c27c408SWill Deacon 
5936c27c408SWill Deacon 		__set_fixmap(FIX_ENTRY_TRAMP_DATA,
5946c27c408SWill Deacon 			     __pa_symbol(__entry_tramp_data_start),
5956c27c408SWill Deacon 			     PAGE_KERNEL_RO);
5966c27c408SWill Deacon 	}
5976c27c408SWill Deacon 
59851a0048bSWill Deacon 	return 0;
59951a0048bSWill Deacon }
60051a0048bSWill Deacon core_initcall(map_entry_trampoline);
60151a0048bSWill Deacon #endif
60251a0048bSWill Deacon 
603068a17a5SMark Rutland /*
604068a17a5SMark Rutland  * Create fine-grained mappings for the kernel.
605068a17a5SMark Rutland  */
60620a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp)
607068a17a5SMark Rutland {
6082ebe088bSArd Biesheuvel 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
6092ebe088bSArd Biesheuvel 				vmlinux_initdata, vmlinux_data;
610068a17a5SMark Rutland 
61128b066daSArd Biesheuvel 	/*
61228b066daSArd Biesheuvel 	 * External debuggers may need to write directly to the text
61328b066daSArd Biesheuvel 	 * mapping to install SW breakpoints. Allow this (only) when
61428b066daSArd Biesheuvel 	 * explicitly requested with rodata=off.
61528b066daSArd Biesheuvel 	 */
61628b066daSArd Biesheuvel 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
61728b066daSArd Biesheuvel 
618d27cfa1fSArd Biesheuvel 	/*
619d27cfa1fSArd Biesheuvel 	 * Only rodata will be remapped with different permissions later on,
620d27cfa1fSArd Biesheuvel 	 * all other segments are allowed to use contiguous mappings.
621d27cfa1fSArd Biesheuvel 	 */
62220a004e7SWill Deacon 	map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0,
62392bbd16eSWill Deacon 			   VM_NO_GUARD);
62420a004e7SWill Deacon 	map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
62592bbd16eSWill Deacon 			   &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
62620a004e7SWill Deacon 	map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
62792bbd16eSWill Deacon 			   &vmlinux_inittext, 0, VM_NO_GUARD);
62820a004e7SWill Deacon 	map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
62992bbd16eSWill Deacon 			   &vmlinux_initdata, 0, VM_NO_GUARD);
63020a004e7SWill Deacon 	map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
631068a17a5SMark Rutland 
63220a004e7SWill Deacon 	if (!READ_ONCE(pgd_val(*pgd_offset_raw(pgdp, FIXADDR_START)))) {
633068a17a5SMark Rutland 		/*
634f9040773SArd Biesheuvel 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
635f9040773SArd Biesheuvel 		 * live in the carveout for the swapper_pg_dir. We can simply
636f9040773SArd Biesheuvel 		 * re-use the existing dir for the fixmap.
637068a17a5SMark Rutland 		 */
63820a004e7SWill Deacon 		set_pgd(pgd_offset_raw(pgdp, FIXADDR_START),
63920a004e7SWill Deacon 			READ_ONCE(*pgd_offset_k(FIXADDR_START)));
640f9040773SArd Biesheuvel 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
641f9040773SArd Biesheuvel 		/*
642f9040773SArd Biesheuvel 		 * The fixmap shares its top level pgd entry with the kernel
643f9040773SArd Biesheuvel 		 * mapping. This can really only occur when we are running
644f9040773SArd Biesheuvel 		 * with 16k/4 levels, so we can simply reuse the pud level
645f9040773SArd Biesheuvel 		 * entry instead.
646f9040773SArd Biesheuvel 		 */
647f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
64820a004e7SWill Deacon 		pud_populate(&init_mm,
64920a004e7SWill Deacon 			     pud_set_fixmap_offset(pgdp, FIXADDR_START),
65019338304SKristina Martsenko 			     lm_alias(bm_pmd));
651f9040773SArd Biesheuvel 		pud_clear_fixmap();
652f9040773SArd Biesheuvel 	} else {
653f9040773SArd Biesheuvel 		BUG();
654f9040773SArd Biesheuvel 	}
655068a17a5SMark Rutland 
65620a004e7SWill Deacon 	kasan_copy_shadow(pgdp);
657068a17a5SMark Rutland }
658068a17a5SMark Rutland 
659c1cc1552SCatalin Marinas void __init paging_init(void)
660c1cc1552SCatalin Marinas {
6612330b7caSJun Yao 	pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
662068a17a5SMark Rutland 
66320a004e7SWill Deacon 	map_kernel(pgdp);
66420a004e7SWill Deacon 	map_mem(pgdp);
665068a17a5SMark Rutland 
666068a17a5SMark Rutland 	pgd_clear_fixmap();
667068a17a5SMark Rutland 
668068a17a5SMark Rutland 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
6692b5548b6SJun Yao 	init_mm.pgd = swapper_pg_dir;
670068a17a5SMark Rutland 
6712b5548b6SJun Yao 	memblock_free(__pa_symbol(init_pg_dir),
6722b5548b6SJun Yao 		      __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
67324cc61d8SArd Biesheuvel 
67424cc61d8SArd Biesheuvel 	memblock_allow_resize();
675c1cc1552SCatalin Marinas }
676c1cc1552SCatalin Marinas 
677c1cc1552SCatalin Marinas /*
678c1cc1552SCatalin Marinas  * Check whether a kernel address is valid (derived from arch/x86/).
679c1cc1552SCatalin Marinas  */
680c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr)
681c1cc1552SCatalin Marinas {
68220a004e7SWill Deacon 	pgd_t *pgdp;
68320a004e7SWill Deacon 	pud_t *pudp, pud;
68420a004e7SWill Deacon 	pmd_t *pmdp, pmd;
68520a004e7SWill Deacon 	pte_t *ptep, pte;
686c1cc1552SCatalin Marinas 
687c1cc1552SCatalin Marinas 	if ((((long)addr) >> VA_BITS) != -1UL)
688c1cc1552SCatalin Marinas 		return 0;
689c1cc1552SCatalin Marinas 
69020a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
69120a004e7SWill Deacon 	if (pgd_none(READ_ONCE(*pgdp)))
692c1cc1552SCatalin Marinas 		return 0;
693c1cc1552SCatalin Marinas 
69420a004e7SWill Deacon 	pudp = pud_offset(pgdp, addr);
69520a004e7SWill Deacon 	pud = READ_ONCE(*pudp);
69620a004e7SWill Deacon 	if (pud_none(pud))
697c1cc1552SCatalin Marinas 		return 0;
698c1cc1552SCatalin Marinas 
69920a004e7SWill Deacon 	if (pud_sect(pud))
70020a004e7SWill Deacon 		return pfn_valid(pud_pfn(pud));
701206a2a73SSteve Capper 
70220a004e7SWill Deacon 	pmdp = pmd_offset(pudp, addr);
70320a004e7SWill Deacon 	pmd = READ_ONCE(*pmdp);
70420a004e7SWill Deacon 	if (pmd_none(pmd))
705c1cc1552SCatalin Marinas 		return 0;
706c1cc1552SCatalin Marinas 
70720a004e7SWill Deacon 	if (pmd_sect(pmd))
70820a004e7SWill Deacon 		return pfn_valid(pmd_pfn(pmd));
709da6e4cb6SDave Anderson 
71020a004e7SWill Deacon 	ptep = pte_offset_kernel(pmdp, addr);
71120a004e7SWill Deacon 	pte = READ_ONCE(*ptep);
71220a004e7SWill Deacon 	if (pte_none(pte))
713c1cc1552SCatalin Marinas 		return 0;
714c1cc1552SCatalin Marinas 
71520a004e7SWill Deacon 	return pfn_valid(pte_pfn(pte));
716c1cc1552SCatalin Marinas }
717c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP
718b433dce0SSuzuki K. Poulose #if !ARM64_SWAPPER_USES_SECTION_MAPS
7197b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
7207b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
721c1cc1552SCatalin Marinas {
7220aad818bSJohannes Weiner 	return vmemmap_populate_basepages(start, end, node);
723c1cc1552SCatalin Marinas }
724b433dce0SSuzuki K. Poulose #else	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
7257b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
7267b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
727c1cc1552SCatalin Marinas {
7280aad818bSJohannes Weiner 	unsigned long addr = start;
729c1cc1552SCatalin Marinas 	unsigned long next;
73020a004e7SWill Deacon 	pgd_t *pgdp;
73120a004e7SWill Deacon 	pud_t *pudp;
73220a004e7SWill Deacon 	pmd_t *pmdp;
733c1cc1552SCatalin Marinas 
734c1cc1552SCatalin Marinas 	do {
735c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
736c1cc1552SCatalin Marinas 
73720a004e7SWill Deacon 		pgdp = vmemmap_pgd_populate(addr, node);
73820a004e7SWill Deacon 		if (!pgdp)
739c1cc1552SCatalin Marinas 			return -ENOMEM;
740c1cc1552SCatalin Marinas 
74120a004e7SWill Deacon 		pudp = vmemmap_pud_populate(pgdp, addr, node);
74220a004e7SWill Deacon 		if (!pudp)
743c1cc1552SCatalin Marinas 			return -ENOMEM;
744c1cc1552SCatalin Marinas 
74520a004e7SWill Deacon 		pmdp = pmd_offset(pudp, addr);
74620a004e7SWill Deacon 		if (pmd_none(READ_ONCE(*pmdp))) {
747c1cc1552SCatalin Marinas 			void *p = NULL;
748c1cc1552SCatalin Marinas 
749c1cc1552SCatalin Marinas 			p = vmemmap_alloc_block_buf(PMD_SIZE, node);
750c1cc1552SCatalin Marinas 			if (!p)
751c1cc1552SCatalin Marinas 				return -ENOMEM;
752c1cc1552SCatalin Marinas 
75320a004e7SWill Deacon 			pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
754c1cc1552SCatalin Marinas 		} else
75520a004e7SWill Deacon 			vmemmap_verify((pte_t *)pmdp, node, addr, next);
756c1cc1552SCatalin Marinas 	} while (addr = next, addr != end);
757c1cc1552SCatalin Marinas 
758c1cc1552SCatalin Marinas 	return 0;
759c1cc1552SCatalin Marinas }
760c1cc1552SCatalin Marinas #endif	/* CONFIG_ARM64_64K_PAGES */
76124b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end,
76224b6d416SChristoph Hellwig 		struct vmem_altmap *altmap)
7630197518cSTang Chen {
7640197518cSTang Chen }
765c1cc1552SCatalin Marinas #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
766af86e597SLaura Abbott 
767af86e597SLaura Abbott static inline pud_t * fixmap_pud(unsigned long addr)
768af86e597SLaura Abbott {
76920a004e7SWill Deacon 	pgd_t *pgdp = pgd_offset_k(addr);
77020a004e7SWill Deacon 	pgd_t pgd = READ_ONCE(*pgdp);
771af86e597SLaura Abbott 
77220a004e7SWill Deacon 	BUG_ON(pgd_none(pgd) || pgd_bad(pgd));
773af86e597SLaura Abbott 
77420a004e7SWill Deacon 	return pud_offset_kimg(pgdp, addr);
775af86e597SLaura Abbott }
776af86e597SLaura Abbott 
777af86e597SLaura Abbott static inline pmd_t * fixmap_pmd(unsigned long addr)
778af86e597SLaura Abbott {
77920a004e7SWill Deacon 	pud_t *pudp = fixmap_pud(addr);
78020a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
781af86e597SLaura Abbott 
78220a004e7SWill Deacon 	BUG_ON(pud_none(pud) || pud_bad(pud));
783af86e597SLaura Abbott 
78420a004e7SWill Deacon 	return pmd_offset_kimg(pudp, addr);
785af86e597SLaura Abbott }
786af86e597SLaura Abbott 
787af86e597SLaura Abbott static inline pte_t * fixmap_pte(unsigned long addr)
788af86e597SLaura Abbott {
789157962f5SArd Biesheuvel 	return &bm_pte[pte_index(addr)];
790af86e597SLaura Abbott }
791af86e597SLaura Abbott 
7922077be67SLaura Abbott /*
7932077be67SLaura Abbott  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
7942077be67SLaura Abbott  * directly on kernel symbols (bm_p*d). This function is called too early to use
7952077be67SLaura Abbott  * lm_alias so __p*d_populate functions must be used to populate with the
7962077be67SLaura Abbott  * physical address from __pa_symbol.
7972077be67SLaura Abbott  */
798af86e597SLaura Abbott void __init early_fixmap_init(void)
799af86e597SLaura Abbott {
80020a004e7SWill Deacon 	pgd_t *pgdp, pgd;
80120a004e7SWill Deacon 	pud_t *pudp;
80220a004e7SWill Deacon 	pmd_t *pmdp;
803af86e597SLaura Abbott 	unsigned long addr = FIXADDR_START;
804af86e597SLaura Abbott 
80520a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
80620a004e7SWill Deacon 	pgd = READ_ONCE(*pgdp);
807f80fb3a3SArd Biesheuvel 	if (CONFIG_PGTABLE_LEVELS > 3 &&
80820a004e7SWill Deacon 	    !(pgd_none(pgd) || pgd_page_paddr(pgd) == __pa_symbol(bm_pud))) {
809f9040773SArd Biesheuvel 		/*
810f9040773SArd Biesheuvel 		 * We only end up here if the kernel mapping and the fixmap
811f9040773SArd Biesheuvel 		 * share the top level pgd entry, which should only happen on
812f9040773SArd Biesheuvel 		 * 16k/4 levels configurations.
813f9040773SArd Biesheuvel 		 */
814f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
81520a004e7SWill Deacon 		pudp = pud_offset_kimg(pgdp, addr);
816f9040773SArd Biesheuvel 	} else {
81720a004e7SWill Deacon 		if (pgd_none(pgd))
81820a004e7SWill Deacon 			__pgd_populate(pgdp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
81920a004e7SWill Deacon 		pudp = fixmap_pud(addr);
820f9040773SArd Biesheuvel 	}
82120a004e7SWill Deacon 	if (pud_none(READ_ONCE(*pudp)))
82220a004e7SWill Deacon 		__pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
82320a004e7SWill Deacon 	pmdp = fixmap_pmd(addr);
82420a004e7SWill Deacon 	__pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
825af86e597SLaura Abbott 
826af86e597SLaura Abbott 	/*
827af86e597SLaura Abbott 	 * The boot-ioremap range spans multiple pmds, for which
828157962f5SArd Biesheuvel 	 * we are not prepared:
829af86e597SLaura Abbott 	 */
830af86e597SLaura Abbott 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
831af86e597SLaura Abbott 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
832af86e597SLaura Abbott 
83320a004e7SWill Deacon 	if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
83420a004e7SWill Deacon 	     || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
835af86e597SLaura Abbott 		WARN_ON(1);
83620a004e7SWill Deacon 		pr_warn("pmdp %p != %p, %p\n",
83720a004e7SWill Deacon 			pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
838af86e597SLaura Abbott 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
839af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
840af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_BEGIN));
841af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
842af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_END));
843af86e597SLaura Abbott 
844af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
845af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
846af86e597SLaura Abbott 	}
847af86e597SLaura Abbott }
848af86e597SLaura Abbott 
84918b4b276SJames Morse /*
85018b4b276SJames Morse  * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
85118b4b276SJames Morse  * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
85218b4b276SJames Morse  */
853af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx,
854af86e597SLaura Abbott 			       phys_addr_t phys, pgprot_t flags)
855af86e597SLaura Abbott {
856af86e597SLaura Abbott 	unsigned long addr = __fix_to_virt(idx);
85720a004e7SWill Deacon 	pte_t *ptep;
858af86e597SLaura Abbott 
859b63dbef9SMark Rutland 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
860af86e597SLaura Abbott 
86120a004e7SWill Deacon 	ptep = fixmap_pte(addr);
862af86e597SLaura Abbott 
863af86e597SLaura Abbott 	if (pgprot_val(flags)) {
86420a004e7SWill Deacon 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
865af86e597SLaura Abbott 	} else {
86620a004e7SWill Deacon 		pte_clear(&init_mm, addr, ptep);
867af86e597SLaura Abbott 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
868af86e597SLaura Abbott 	}
869af86e597SLaura Abbott }
87061bd93ceSArd Biesheuvel 
871f80fb3a3SArd Biesheuvel void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
87261bd93ceSArd Biesheuvel {
87361bd93ceSArd Biesheuvel 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
874f80fb3a3SArd Biesheuvel 	int offset;
87561bd93ceSArd Biesheuvel 	void *dt_virt;
87661bd93ceSArd Biesheuvel 
87761bd93ceSArd Biesheuvel 	/*
87861bd93ceSArd Biesheuvel 	 * Check whether the physical FDT address is set and meets the minimum
87961bd93ceSArd Biesheuvel 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
88004a84810SArd Biesheuvel 	 * at least 8 bytes so that we can always access the magic and size
88104a84810SArd Biesheuvel 	 * fields of the FDT header after mapping the first chunk, double check
88204a84810SArd Biesheuvel 	 * here if that is indeed the case.
88361bd93ceSArd Biesheuvel 	 */
88461bd93ceSArd Biesheuvel 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
88561bd93ceSArd Biesheuvel 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
88661bd93ceSArd Biesheuvel 		return NULL;
88761bd93ceSArd Biesheuvel 
88861bd93ceSArd Biesheuvel 	/*
88961bd93ceSArd Biesheuvel 	 * Make sure that the FDT region can be mapped without the need to
89061bd93ceSArd Biesheuvel 	 * allocate additional translation table pages, so that it is safe
891132233a7SLaura Abbott 	 * to call create_mapping_noalloc() this early.
89261bd93ceSArd Biesheuvel 	 *
89361bd93ceSArd Biesheuvel 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
89461bd93ceSArd Biesheuvel 	 * be in the same PMD as the rest of the fixmap.
89561bd93ceSArd Biesheuvel 	 * On 4k pages, we'll use section mappings for the FDT so we only
89661bd93ceSArd Biesheuvel 	 * have to be in the same PUD.
89761bd93ceSArd Biesheuvel 	 */
89861bd93ceSArd Biesheuvel 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
89961bd93ceSArd Biesheuvel 
900b433dce0SSuzuki K. Poulose 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
901b433dce0SSuzuki K. Poulose 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
90261bd93ceSArd Biesheuvel 
903b433dce0SSuzuki K. Poulose 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
90461bd93ceSArd Biesheuvel 	dt_virt = (void *)dt_virt_base + offset;
90561bd93ceSArd Biesheuvel 
90661bd93ceSArd Biesheuvel 	/* map the first chunk so we can read the size from the header */
907132233a7SLaura Abbott 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
908132233a7SLaura Abbott 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
90961bd93ceSArd Biesheuvel 
91004a84810SArd Biesheuvel 	if (fdt_magic(dt_virt) != FDT_MAGIC)
91161bd93ceSArd Biesheuvel 		return NULL;
91261bd93ceSArd Biesheuvel 
913f80fb3a3SArd Biesheuvel 	*size = fdt_totalsize(dt_virt);
914f80fb3a3SArd Biesheuvel 	if (*size > MAX_FDT_SIZE)
91561bd93ceSArd Biesheuvel 		return NULL;
91661bd93ceSArd Biesheuvel 
917f80fb3a3SArd Biesheuvel 	if (offset + *size > SWAPPER_BLOCK_SIZE)
918132233a7SLaura Abbott 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
919f80fb3a3SArd Biesheuvel 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
920f80fb3a3SArd Biesheuvel 
921f80fb3a3SArd Biesheuvel 	return dt_virt;
922f80fb3a3SArd Biesheuvel }
923f80fb3a3SArd Biesheuvel 
924f80fb3a3SArd Biesheuvel void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
925f80fb3a3SArd Biesheuvel {
926f80fb3a3SArd Biesheuvel 	void *dt_virt;
927f80fb3a3SArd Biesheuvel 	int size;
928f80fb3a3SArd Biesheuvel 
929f80fb3a3SArd Biesheuvel 	dt_virt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
930f80fb3a3SArd Biesheuvel 	if (!dt_virt)
931f80fb3a3SArd Biesheuvel 		return NULL;
93261bd93ceSArd Biesheuvel 
93361bd93ceSArd Biesheuvel 	memblock_reserve(dt_phys, size);
93461bd93ceSArd Biesheuvel 	return dt_virt;
93561bd93ceSArd Biesheuvel }
936324420bfSArd Biesheuvel 
937324420bfSArd Biesheuvel int __init arch_ioremap_pud_supported(void)
938324420bfSArd Biesheuvel {
939324420bfSArd Biesheuvel 	/* only 4k granule supports level 1 block mappings */
940324420bfSArd Biesheuvel 	return IS_ENABLED(CONFIG_ARM64_4K_PAGES);
941324420bfSArd Biesheuvel }
942324420bfSArd Biesheuvel 
943324420bfSArd Biesheuvel int __init arch_ioremap_pmd_supported(void)
944324420bfSArd Biesheuvel {
945324420bfSArd Biesheuvel 	return 1;
946324420bfSArd Biesheuvel }
947324420bfSArd Biesheuvel 
94820a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
949324420bfSArd Biesheuvel {
95019338304SKristina Martsenko 	pgprot_t sect_prot = __pgprot(PUD_TYPE_SECT |
95119338304SKristina Martsenko 					pgprot_val(mk_sect_prot(prot)));
95282034c23SLaura Abbott 	pud_t new_pud = pfn_pud(__phys_to_pfn(phys), sect_prot);
95315122ee2SWill Deacon 
95482034c23SLaura Abbott 	/* Only allow permission changes for now */
95582034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
95682034c23SLaura Abbott 				   pud_val(new_pud)))
95715122ee2SWill Deacon 		return 0;
95815122ee2SWill Deacon 
959324420bfSArd Biesheuvel 	BUG_ON(phys & ~PUD_MASK);
96082034c23SLaura Abbott 	set_pud(pudp, new_pud);
961324420bfSArd Biesheuvel 	return 1;
962324420bfSArd Biesheuvel }
963324420bfSArd Biesheuvel 
96420a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
965324420bfSArd Biesheuvel {
96619338304SKristina Martsenko 	pgprot_t sect_prot = __pgprot(PMD_TYPE_SECT |
96719338304SKristina Martsenko 					pgprot_val(mk_sect_prot(prot)));
96882034c23SLaura Abbott 	pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), sect_prot);
96915122ee2SWill Deacon 
97082034c23SLaura Abbott 	/* Only allow permission changes for now */
97182034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
97282034c23SLaura Abbott 				   pmd_val(new_pmd)))
97315122ee2SWill Deacon 		return 0;
97415122ee2SWill Deacon 
975324420bfSArd Biesheuvel 	BUG_ON(phys & ~PMD_MASK);
97682034c23SLaura Abbott 	set_pmd(pmdp, new_pmd);
977324420bfSArd Biesheuvel 	return 1;
978324420bfSArd Biesheuvel }
979324420bfSArd Biesheuvel 
98020a004e7SWill Deacon int pud_clear_huge(pud_t *pudp)
981324420bfSArd Biesheuvel {
98220a004e7SWill Deacon 	if (!pud_sect(READ_ONCE(*pudp)))
983324420bfSArd Biesheuvel 		return 0;
98420a004e7SWill Deacon 	pud_clear(pudp);
985324420bfSArd Biesheuvel 	return 1;
986324420bfSArd Biesheuvel }
987324420bfSArd Biesheuvel 
98820a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp)
989324420bfSArd Biesheuvel {
99020a004e7SWill Deacon 	if (!pmd_sect(READ_ONCE(*pmdp)))
991324420bfSArd Biesheuvel 		return 0;
99220a004e7SWill Deacon 	pmd_clear(pmdp);
993324420bfSArd Biesheuvel 	return 1;
994324420bfSArd Biesheuvel }
995b6bdb751SToshi Kani 
996ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
997b6bdb751SToshi Kani {
998ec28bb9cSChintan Pandya 	pte_t *table;
999ec28bb9cSChintan Pandya 	pmd_t pmd;
1000ec28bb9cSChintan Pandya 
1001ec28bb9cSChintan Pandya 	pmd = READ_ONCE(*pmdp);
1002ec28bb9cSChintan Pandya 
1003fac880c7SMark Rutland 	if (!pmd_table(pmd)) {
10049c006972SWill Deacon 		VM_WARN_ON(1);
1005ec28bb9cSChintan Pandya 		return 1;
1006b6bdb751SToshi Kani 	}
1007b6bdb751SToshi Kani 
1008ec28bb9cSChintan Pandya 	table = pte_offset_kernel(pmdp, addr);
1009ec28bb9cSChintan Pandya 	pmd_clear(pmdp);
1010ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1011ec28bb9cSChintan Pandya 	pte_free_kernel(NULL, table);
1012ec28bb9cSChintan Pandya 	return 1;
1013ec28bb9cSChintan Pandya }
1014ec28bb9cSChintan Pandya 
1015ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1016b6bdb751SToshi Kani {
1017ec28bb9cSChintan Pandya 	pmd_t *table;
1018ec28bb9cSChintan Pandya 	pmd_t *pmdp;
1019ec28bb9cSChintan Pandya 	pud_t pud;
1020ec28bb9cSChintan Pandya 	unsigned long next, end;
1021ec28bb9cSChintan Pandya 
1022ec28bb9cSChintan Pandya 	pud = READ_ONCE(*pudp);
1023ec28bb9cSChintan Pandya 
1024fac880c7SMark Rutland 	if (!pud_table(pud)) {
10259c006972SWill Deacon 		VM_WARN_ON(1);
1026ec28bb9cSChintan Pandya 		return 1;
1027ec28bb9cSChintan Pandya 	}
1028ec28bb9cSChintan Pandya 
1029ec28bb9cSChintan Pandya 	table = pmd_offset(pudp, addr);
1030ec28bb9cSChintan Pandya 	pmdp = table;
1031ec28bb9cSChintan Pandya 	next = addr;
1032ec28bb9cSChintan Pandya 	end = addr + PUD_SIZE;
1033ec28bb9cSChintan Pandya 	do {
1034ec28bb9cSChintan Pandya 		pmd_free_pte_page(pmdp, next);
1035ec28bb9cSChintan Pandya 	} while (pmdp++, next += PMD_SIZE, next != end);
1036ec28bb9cSChintan Pandya 
1037ec28bb9cSChintan Pandya 	pud_clear(pudp);
1038ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1039ec28bb9cSChintan Pandya 	pmd_free(NULL, table);
1040ec28bb9cSChintan Pandya 	return 1;
1041b6bdb751SToshi Kani }
10424ab21506SRobin Murphy 
10438e2d4340SWill Deacon int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
10448e2d4340SWill Deacon {
10458e2d4340SWill Deacon 	return 0;	/* Don't attempt a block mapping */
10468e2d4340SWill Deacon }
10478e2d4340SWill Deacon 
10484ab21506SRobin Murphy #ifdef CONFIG_MEMORY_HOTPLUG
10494ab21506SRobin Murphy int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap,
10504ab21506SRobin Murphy 		    bool want_memblock)
10514ab21506SRobin Murphy {
10524ab21506SRobin Murphy 	int flags = 0;
10534ab21506SRobin Murphy 
10544ab21506SRobin Murphy 	if (rodata_full || debug_pagealloc_enabled())
10554ab21506SRobin Murphy 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
10564ab21506SRobin Murphy 
10574ab21506SRobin Murphy 	__create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
10584ab21506SRobin Murphy 			     size, PAGE_KERNEL, pgd_pgtable_alloc, flags);
10594ab21506SRobin Murphy 
10604ab21506SRobin Murphy 	return __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
10614ab21506SRobin Murphy 			   altmap, want_memblock);
10624ab21506SRobin Murphy }
10634ab21506SRobin Murphy #endif
1064