xref: /openbmc/linux/arch/arm64/mm/mmu.c (revision 8e2d4340)
1c1cc1552SCatalin Marinas /*
2c1cc1552SCatalin Marinas  * Based on arch/arm/mm/mmu.c
3c1cc1552SCatalin Marinas  *
4c1cc1552SCatalin Marinas  * Copyright (C) 1995-2005 Russell King
5c1cc1552SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
6c1cc1552SCatalin Marinas  *
7c1cc1552SCatalin Marinas  * This program is free software; you can redistribute it and/or modify
8c1cc1552SCatalin Marinas  * it under the terms of the GNU General Public License version 2 as
9c1cc1552SCatalin Marinas  * published by the Free Software Foundation.
10c1cc1552SCatalin Marinas  *
11c1cc1552SCatalin Marinas  * This program is distributed in the hope that it will be useful,
12c1cc1552SCatalin Marinas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13c1cc1552SCatalin Marinas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14c1cc1552SCatalin Marinas  * GNU General Public License for more details.
15c1cc1552SCatalin Marinas  *
16c1cc1552SCatalin Marinas  * You should have received a copy of the GNU General Public License
17c1cc1552SCatalin Marinas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18c1cc1552SCatalin Marinas  */
19c1cc1552SCatalin Marinas 
205a9e3e15SJisheng Zhang #include <linux/cache.h>
21c1cc1552SCatalin Marinas #include <linux/export.h>
22c1cc1552SCatalin Marinas #include <linux/kernel.h>
23c1cc1552SCatalin Marinas #include <linux/errno.h>
24c1cc1552SCatalin Marinas #include <linux/init.h>
2598d2e153STakahiro Akashi #include <linux/ioport.h>
2698d2e153STakahiro Akashi #include <linux/kexec.h>
2761bd93ceSArd Biesheuvel #include <linux/libfdt.h>
28c1cc1552SCatalin Marinas #include <linux/mman.h>
29c1cc1552SCatalin Marinas #include <linux/nodemask.h>
30c1cc1552SCatalin Marinas #include <linux/memblock.h>
31c1cc1552SCatalin Marinas #include <linux/fs.h>
322475ff9dSCatalin Marinas #include <linux/io.h>
332077be67SLaura Abbott #include <linux/mm.h>
346efd8499STobias Klauser #include <linux/vmalloc.h>
35c1cc1552SCatalin Marinas 
3621ab99c2SMark Rutland #include <asm/barrier.h>
37c1cc1552SCatalin Marinas #include <asm/cputype.h>
38af86e597SLaura Abbott #include <asm/fixmap.h>
39068a17a5SMark Rutland #include <asm/kasan.h>
40b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h>
41c1cc1552SCatalin Marinas #include <asm/sections.h>
42c1cc1552SCatalin Marinas #include <asm/setup.h>
43c1cc1552SCatalin Marinas #include <asm/sizes.h>
44c1cc1552SCatalin Marinas #include <asm/tlb.h>
45c79b954bSJungseok Lee #include <asm/memblock.h>
46c1cc1552SCatalin Marinas #include <asm/mmu_context.h>
471404d6f1SLaura Abbott #include <asm/ptdump.h>
48ec28bb9cSChintan Pandya #include <asm/tlbflush.h>
49c1cc1552SCatalin Marinas 
50c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS	BIT(0)
51d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS	BIT(1)
52c0951366SArd Biesheuvel 
53dd006da2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
54fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
5567e7fdfcSSteve Capper u64 vabits_user __ro_after_init;
564a1daf29SWill Deacon EXPORT_SYMBOL(vabits_user);
57dd006da2SArd Biesheuvel 
585a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init;
59a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset);
60a7f8de16SArd Biesheuvel 
61c1cc1552SCatalin Marinas /*
62c1cc1552SCatalin Marinas  * Empty_zero_page is a special page that is used for zero-initialized data
63c1cc1552SCatalin Marinas  * and COW.
64c1cc1552SCatalin Marinas  */
655227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
66c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page);
67c1cc1552SCatalin Marinas 
68f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
69f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
70f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
71f9040773SArd Biesheuvel 
722330b7caSJun Yao static DEFINE_SPINLOCK(swapper_pgdir_lock);
732330b7caSJun Yao 
742330b7caSJun Yao void set_swapper_pgd(pgd_t *pgdp, pgd_t pgd)
752330b7caSJun Yao {
762330b7caSJun Yao 	pgd_t *fixmap_pgdp;
772330b7caSJun Yao 
782330b7caSJun Yao 	spin_lock(&swapper_pgdir_lock);
7926a6f87eSJames Morse 	fixmap_pgdp = pgd_set_fixmap(__pa_symbol(pgdp));
802330b7caSJun Yao 	WRITE_ONCE(*fixmap_pgdp, pgd);
812330b7caSJun Yao 	/*
822330b7caSJun Yao 	 * We need dsb(ishst) here to ensure the page-table-walker sees
832330b7caSJun Yao 	 * our new entry before set_p?d() returns. The fixmap's
842330b7caSJun Yao 	 * flush_tlb_kernel_range() via clear_fixmap() does this for us.
852330b7caSJun Yao 	 */
862330b7caSJun Yao 	pgd_clear_fixmap();
872330b7caSJun Yao 	spin_unlock(&swapper_pgdir_lock);
882330b7caSJun Yao }
892330b7caSJun Yao 
90c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
91c1cc1552SCatalin Marinas 			      unsigned long size, pgprot_t vma_prot)
92c1cc1552SCatalin Marinas {
93c1cc1552SCatalin Marinas 	if (!pfn_valid(pfn))
94c1cc1552SCatalin Marinas 		return pgprot_noncached(vma_prot);
95c1cc1552SCatalin Marinas 	else if (file->f_flags & O_SYNC)
96c1cc1552SCatalin Marinas 		return pgprot_writecombine(vma_prot);
97c1cc1552SCatalin Marinas 	return vma_prot;
98c1cc1552SCatalin Marinas }
99c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot);
100c1cc1552SCatalin Marinas 
101f4710445SMark Rutland static phys_addr_t __init early_pgtable_alloc(void)
102c1cc1552SCatalin Marinas {
1037142392dSSuzuki K. Poulose 	phys_addr_t phys;
1047142392dSSuzuki K. Poulose 	void *ptr;
1057142392dSSuzuki K. Poulose 
1069a8dd708SMike Rapoport 	phys = memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
107f4710445SMark Rutland 
108f4710445SMark Rutland 	/*
109f4710445SMark Rutland 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
110f4710445SMark Rutland 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
111f4710445SMark Rutland 	 * any level of table.
112f4710445SMark Rutland 	 */
113f4710445SMark Rutland 	ptr = pte_set_fixmap(phys);
114f4710445SMark Rutland 
11521ab99c2SMark Rutland 	memset(ptr, 0, PAGE_SIZE);
11621ab99c2SMark Rutland 
117f4710445SMark Rutland 	/*
118f4710445SMark Rutland 	 * Implicit barriers also ensure the zeroed page is visible to the page
119f4710445SMark Rutland 	 * table walker
120f4710445SMark Rutland 	 */
121f4710445SMark Rutland 	pte_clear_fixmap();
122f4710445SMark Rutland 
123f4710445SMark Rutland 	return phys;
124c1cc1552SCatalin Marinas }
125c1cc1552SCatalin Marinas 
126e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new)
127e98216b5SArd Biesheuvel {
128e98216b5SArd Biesheuvel 	/*
129e98216b5SArd Biesheuvel 	 * The following mapping attributes may be updated in live
130e98216b5SArd Biesheuvel 	 * kernel mappings without the need for break-before-make.
131e98216b5SArd Biesheuvel 	 */
132753e8abcSArd Biesheuvel 	static const pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE | PTE_NG;
133e98216b5SArd Biesheuvel 
134141d1497SArd Biesheuvel 	/* creating or taking down mappings is always safe */
135141d1497SArd Biesheuvel 	if (old == 0 || new == 0)
136141d1497SArd Biesheuvel 		return true;
137141d1497SArd Biesheuvel 
138141d1497SArd Biesheuvel 	/* live contiguous mappings may not be manipulated at all */
139141d1497SArd Biesheuvel 	if ((old | new) & PTE_CONT)
140141d1497SArd Biesheuvel 		return false;
141141d1497SArd Biesheuvel 
142753e8abcSArd Biesheuvel 	/* Transitioning from Non-Global to Global is unsafe */
143753e8abcSArd Biesheuvel 	if (old & ~new & PTE_NG)
144753e8abcSArd Biesheuvel 		return false;
1454e602056SWill Deacon 
146141d1497SArd Biesheuvel 	return ((old ^ new) & ~mask) == 0;
147e98216b5SArd Biesheuvel }
148e98216b5SArd Biesheuvel 
14920a004e7SWill Deacon static void init_pte(pmd_t *pmdp, unsigned long addr, unsigned long end,
150d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot)
151c1cc1552SCatalin Marinas {
15220a004e7SWill Deacon 	pte_t *ptep;
153c1cc1552SCatalin Marinas 
15420a004e7SWill Deacon 	ptep = pte_set_fixmap_offset(pmdp, addr);
155c1cc1552SCatalin Marinas 	do {
15620a004e7SWill Deacon 		pte_t old_pte = READ_ONCE(*ptep);
157e98216b5SArd Biesheuvel 
15820a004e7SWill Deacon 		set_pte(ptep, pfn_pte(__phys_to_pfn(phys), prot));
159e98216b5SArd Biesheuvel 
160e98216b5SArd Biesheuvel 		/*
161e98216b5SArd Biesheuvel 		 * After the PTE entry has been populated once, we
162e98216b5SArd Biesheuvel 		 * only allow updates to the permission attributes.
163e98216b5SArd Biesheuvel 		 */
16420a004e7SWill Deacon 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte),
16520a004e7SWill Deacon 					      READ_ONCE(pte_val(*ptep))));
166e98216b5SArd Biesheuvel 
167e393cf40SArd Biesheuvel 		phys += PAGE_SIZE;
16820a004e7SWill Deacon 	} while (ptep++, addr += PAGE_SIZE, addr != end);
169f4710445SMark Rutland 
170f4710445SMark Rutland 	pte_clear_fixmap();
171c1cc1552SCatalin Marinas }
172c1cc1552SCatalin Marinas 
17320a004e7SWill Deacon static void alloc_init_cont_pte(pmd_t *pmdp, unsigned long addr,
174d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
175d27cfa1fSArd Biesheuvel 				pgprot_t prot,
17653e1b329SArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void),
177c0951366SArd Biesheuvel 				int flags)
178c1cc1552SCatalin Marinas {
179c1cc1552SCatalin Marinas 	unsigned long next;
18020a004e7SWill Deacon 	pmd_t pmd = READ_ONCE(*pmdp);
181c1cc1552SCatalin Marinas 
18220a004e7SWill Deacon 	BUG_ON(pmd_sect(pmd));
18320a004e7SWill Deacon 	if (pmd_none(pmd)) {
184d27cfa1fSArd Biesheuvel 		phys_addr_t pte_phys;
185132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
186d27cfa1fSArd Biesheuvel 		pte_phys = pgtable_alloc();
18720a004e7SWill Deacon 		__pmd_populate(pmdp, pte_phys, PMD_TYPE_TABLE);
18820a004e7SWill Deacon 		pmd = READ_ONCE(*pmdp);
189c1cc1552SCatalin Marinas 	}
19020a004e7SWill Deacon 	BUG_ON(pmd_bad(pmd));
191d27cfa1fSArd Biesheuvel 
192d27cfa1fSArd Biesheuvel 	do {
193d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
194d27cfa1fSArd Biesheuvel 
195d27cfa1fSArd Biesheuvel 		next = pte_cont_addr_end(addr, end);
196d27cfa1fSArd Biesheuvel 
197d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
198d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
199d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
200d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
201d27cfa1fSArd Biesheuvel 
20220a004e7SWill Deacon 		init_pte(pmdp, addr, next, phys, __prot);
203d27cfa1fSArd Biesheuvel 
204d27cfa1fSArd Biesheuvel 		phys += next - addr;
205d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
206d27cfa1fSArd Biesheuvel }
207d27cfa1fSArd Biesheuvel 
20820a004e7SWill Deacon static void init_pmd(pud_t *pudp, unsigned long addr, unsigned long end,
209d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot,
210d27cfa1fSArd Biesheuvel 		     phys_addr_t (*pgtable_alloc)(void), int flags)
211d27cfa1fSArd Biesheuvel {
212d27cfa1fSArd Biesheuvel 	unsigned long next;
21320a004e7SWill Deacon 	pmd_t *pmdp;
214c1cc1552SCatalin Marinas 
21520a004e7SWill Deacon 	pmdp = pmd_set_fixmap_offset(pudp, addr);
216c1cc1552SCatalin Marinas 	do {
21720a004e7SWill Deacon 		pmd_t old_pmd = READ_ONCE(*pmdp);
218e98216b5SArd Biesheuvel 
219c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
220e98216b5SArd Biesheuvel 
221c1cc1552SCatalin Marinas 		/* try section mapping first */
22283863f25SLaura Abbott 		if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
223c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
22420a004e7SWill Deacon 			pmd_set_huge(pmdp, phys, prot);
225e98216b5SArd Biesheuvel 
226a55f9929SCatalin Marinas 			/*
227e98216b5SArd Biesheuvel 			 * After the PMD entry has been populated once, we
228e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
229a55f9929SCatalin Marinas 			 */
230e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
23120a004e7SWill Deacon 						      READ_ONCE(pmd_val(*pmdp))));
232a55f9929SCatalin Marinas 		} else {
23320a004e7SWill Deacon 			alloc_init_cont_pte(pmdp, addr, next, phys, prot,
234d27cfa1fSArd Biesheuvel 					    pgtable_alloc, flags);
235e98216b5SArd Biesheuvel 
236e98216b5SArd Biesheuvel 			BUG_ON(pmd_val(old_pmd) != 0 &&
23720a004e7SWill Deacon 			       pmd_val(old_pmd) != READ_ONCE(pmd_val(*pmdp)));
238a55f9929SCatalin Marinas 		}
239c1cc1552SCatalin Marinas 		phys += next - addr;
24020a004e7SWill Deacon 	} while (pmdp++, addr = next, addr != end);
241f4710445SMark Rutland 
242f4710445SMark Rutland 	pmd_clear_fixmap();
243c1cc1552SCatalin Marinas }
244c1cc1552SCatalin Marinas 
24520a004e7SWill Deacon static void alloc_init_cont_pmd(pud_t *pudp, unsigned long addr,
246d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
247d27cfa1fSArd Biesheuvel 				pgprot_t prot,
248d27cfa1fSArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void), int flags)
249d27cfa1fSArd Biesheuvel {
250d27cfa1fSArd Biesheuvel 	unsigned long next;
25120a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
252d27cfa1fSArd Biesheuvel 
253d27cfa1fSArd Biesheuvel 	/*
254d27cfa1fSArd Biesheuvel 	 * Check for initial section mappings in the pgd/pud.
255d27cfa1fSArd Biesheuvel 	 */
25620a004e7SWill Deacon 	BUG_ON(pud_sect(pud));
25720a004e7SWill Deacon 	if (pud_none(pud)) {
258d27cfa1fSArd Biesheuvel 		phys_addr_t pmd_phys;
259d27cfa1fSArd Biesheuvel 		BUG_ON(!pgtable_alloc);
260d27cfa1fSArd Biesheuvel 		pmd_phys = pgtable_alloc();
26120a004e7SWill Deacon 		__pud_populate(pudp, pmd_phys, PUD_TYPE_TABLE);
26220a004e7SWill Deacon 		pud = READ_ONCE(*pudp);
263d27cfa1fSArd Biesheuvel 	}
26420a004e7SWill Deacon 	BUG_ON(pud_bad(pud));
265d27cfa1fSArd Biesheuvel 
266d27cfa1fSArd Biesheuvel 	do {
267d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
268d27cfa1fSArd Biesheuvel 
269d27cfa1fSArd Biesheuvel 		next = pmd_cont_addr_end(addr, end);
270d27cfa1fSArd Biesheuvel 
271d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
272d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
273d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
274d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
275d27cfa1fSArd Biesheuvel 
27620a004e7SWill Deacon 		init_pmd(pudp, addr, next, phys, __prot, pgtable_alloc, flags);
277d27cfa1fSArd Biesheuvel 
278d27cfa1fSArd Biesheuvel 		phys += next - addr;
279d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
280d27cfa1fSArd Biesheuvel }
281d27cfa1fSArd Biesheuvel 
282da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next,
283da141706SLaura Abbott 			unsigned long phys)
284da141706SLaura Abbott {
285da141706SLaura Abbott 	if (PAGE_SHIFT != 12)
286da141706SLaura Abbott 		return false;
287da141706SLaura Abbott 
288da141706SLaura Abbott 	if (((addr | next | phys) & ~PUD_MASK) != 0)
289da141706SLaura Abbott 		return false;
290da141706SLaura Abbott 
291da141706SLaura Abbott 	return true;
292da141706SLaura Abbott }
293da141706SLaura Abbott 
29420a004e7SWill Deacon static void alloc_init_pud(pgd_t *pgdp, unsigned long addr, unsigned long end,
295da141706SLaura Abbott 			   phys_addr_t phys, pgprot_t prot,
29653e1b329SArd Biesheuvel 			   phys_addr_t (*pgtable_alloc)(void),
297c0951366SArd Biesheuvel 			   int flags)
298c1cc1552SCatalin Marinas {
299c1cc1552SCatalin Marinas 	unsigned long next;
30020a004e7SWill Deacon 	pud_t *pudp;
30120a004e7SWill Deacon 	pgd_t pgd = READ_ONCE(*pgdp);
302c1cc1552SCatalin Marinas 
30320a004e7SWill Deacon 	if (pgd_none(pgd)) {
304132233a7SLaura Abbott 		phys_addr_t pud_phys;
305132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
306132233a7SLaura Abbott 		pud_phys = pgtable_alloc();
30720a004e7SWill Deacon 		__pgd_populate(pgdp, pud_phys, PUD_TYPE_TABLE);
30820a004e7SWill Deacon 		pgd = READ_ONCE(*pgdp);
309c79b954bSJungseok Lee 	}
31020a004e7SWill Deacon 	BUG_ON(pgd_bad(pgd));
311c79b954bSJungseok Lee 
31220a004e7SWill Deacon 	pudp = pud_set_fixmap_offset(pgdp, addr);
313c1cc1552SCatalin Marinas 	do {
31420a004e7SWill Deacon 		pud_t old_pud = READ_ONCE(*pudp);
315e98216b5SArd Biesheuvel 
316c1cc1552SCatalin Marinas 		next = pud_addr_end(addr, end);
317206a2a73SSteve Capper 
318206a2a73SSteve Capper 		/*
319206a2a73SSteve Capper 		 * For 4K granule only, attempt to put down a 1GB block
320206a2a73SSteve Capper 		 */
321c0951366SArd Biesheuvel 		if (use_1G_block(addr, next, phys) &&
322c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
32320a004e7SWill Deacon 			pud_set_huge(pudp, phys, prot);
324206a2a73SSteve Capper 
325206a2a73SSteve Capper 			/*
326e98216b5SArd Biesheuvel 			 * After the PUD entry has been populated once, we
327e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
328206a2a73SSteve Capper 			 */
329e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
33020a004e7SWill Deacon 						      READ_ONCE(pud_val(*pudp))));
331206a2a73SSteve Capper 		} else {
33220a004e7SWill Deacon 			alloc_init_cont_pmd(pudp, addr, next, phys, prot,
333c0951366SArd Biesheuvel 					    pgtable_alloc, flags);
334e98216b5SArd Biesheuvel 
335e98216b5SArd Biesheuvel 			BUG_ON(pud_val(old_pud) != 0 &&
33620a004e7SWill Deacon 			       pud_val(old_pud) != READ_ONCE(pud_val(*pudp)));
337206a2a73SSteve Capper 		}
338c1cc1552SCatalin Marinas 		phys += next - addr;
33920a004e7SWill Deacon 	} while (pudp++, addr = next, addr != end);
340f4710445SMark Rutland 
341f4710445SMark Rutland 	pud_clear_fixmap();
342c1cc1552SCatalin Marinas }
343c1cc1552SCatalin Marinas 
34440f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
34540f87d31SArd Biesheuvel 				 unsigned long virt, phys_addr_t size,
34640f87d31SArd Biesheuvel 				 pgprot_t prot,
34753e1b329SArd Biesheuvel 				 phys_addr_t (*pgtable_alloc)(void),
348c0951366SArd Biesheuvel 				 int flags)
349c1cc1552SCatalin Marinas {
350c1cc1552SCatalin Marinas 	unsigned long addr, length, end, next;
35120a004e7SWill Deacon 	pgd_t *pgdp = pgd_offset_raw(pgdir, virt);
352c1cc1552SCatalin Marinas 
353cc5d2b3bSMark Rutland 	/*
354cc5d2b3bSMark Rutland 	 * If the virtual and physical address don't have the same offset
355cc5d2b3bSMark Rutland 	 * within a page, we cannot map the region as the caller expects.
356cc5d2b3bSMark Rutland 	 */
357cc5d2b3bSMark Rutland 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
358cc5d2b3bSMark Rutland 		return;
359cc5d2b3bSMark Rutland 
3609c4e08a3SMark Rutland 	phys &= PAGE_MASK;
361c1cc1552SCatalin Marinas 	addr = virt & PAGE_MASK;
362c1cc1552SCatalin Marinas 	length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
363c1cc1552SCatalin Marinas 
364c1cc1552SCatalin Marinas 	end = addr + length;
365c1cc1552SCatalin Marinas 	do {
366c1cc1552SCatalin Marinas 		next = pgd_addr_end(addr, end);
36720a004e7SWill Deacon 		alloc_init_pud(pgdp, addr, next, phys, prot, pgtable_alloc,
368c0951366SArd Biesheuvel 			       flags);
369c1cc1552SCatalin Marinas 		phys += next - addr;
37020a004e7SWill Deacon 	} while (pgdp++, addr = next, addr != end);
371c1cc1552SCatalin Marinas }
372c1cc1552SCatalin Marinas 
3731378dc3dSArd Biesheuvel static phys_addr_t pgd_pgtable_alloc(void)
374da141706SLaura Abbott {
37521ab99c2SMark Rutland 	void *ptr = (void *)__get_free_page(PGALLOC_GFP);
3761378dc3dSArd Biesheuvel 	if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
3771378dc3dSArd Biesheuvel 		BUG();
37821ab99c2SMark Rutland 
37921ab99c2SMark Rutland 	/* Ensure the zeroed page is visible to the page table walker */
38021ab99c2SMark Rutland 	dsb(ishst);
381f4710445SMark Rutland 	return __pa(ptr);
382da141706SLaura Abbott }
383da141706SLaura Abbott 
384132233a7SLaura Abbott /*
385132233a7SLaura Abbott  * This function can only be used to modify existing table entries,
386132233a7SLaura Abbott  * without allocating new levels of table. Note that this permits the
387132233a7SLaura Abbott  * creation of new section or page entries.
388132233a7SLaura Abbott  */
389132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
390da141706SLaura Abbott 				  phys_addr_t size, pgprot_t prot)
391d7ecbddfSMark Salter {
392d7ecbddfSMark Salter 	if (virt < VMALLOC_START) {
393d7ecbddfSMark Salter 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
394d7ecbddfSMark Salter 			&phys, virt);
395d7ecbddfSMark Salter 		return;
396d7ecbddfSMark Salter 	}
397d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
398d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
399d7ecbddfSMark Salter }
400d7ecbddfSMark Salter 
4018ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
4028ce837ceSArd Biesheuvel 			       unsigned long virt, phys_addr_t size,
403f14c66ceSArd Biesheuvel 			       pgprot_t prot, bool page_mappings_only)
4048ce837ceSArd Biesheuvel {
405c0951366SArd Biesheuvel 	int flags = 0;
406c0951366SArd Biesheuvel 
4071378dc3dSArd Biesheuvel 	BUG_ON(mm == &init_mm);
4081378dc3dSArd Biesheuvel 
409c0951366SArd Biesheuvel 	if (page_mappings_only)
410d27cfa1fSArd Biesheuvel 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
411c0951366SArd Biesheuvel 
41211509a30SMark Rutland 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
413c0951366SArd Biesheuvel 			     pgd_pgtable_alloc, flags);
414d7ecbddfSMark Salter }
415d7ecbddfSMark Salter 
416aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
417da141706SLaura Abbott 				phys_addr_t size, pgprot_t prot)
418da141706SLaura Abbott {
419da141706SLaura Abbott 	if (virt < VMALLOC_START) {
420aa8c09beSArd Biesheuvel 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
421da141706SLaura Abbott 			&phys, virt);
422da141706SLaura Abbott 		return;
423da141706SLaura Abbott 	}
424da141706SLaura Abbott 
425d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
426d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
427aa8c09beSArd Biesheuvel 
428aa8c09beSArd Biesheuvel 	/* flush the TLBs after updating live kernel mappings */
429aa8c09beSArd Biesheuvel 	flush_tlb_kernel_range(virt, virt + size);
430da141706SLaura Abbott }
431da141706SLaura Abbott 
43220a004e7SWill Deacon static void __init __map_memblock(pgd_t *pgdp, phys_addr_t start,
43398d2e153STakahiro Akashi 				  phys_addr_t end, pgprot_t prot, int flags)
434da141706SLaura Abbott {
43520a004e7SWill Deacon 	__create_pgd_mapping(pgdp, start, __phys_to_virt(start), end - start,
43698d2e153STakahiro Akashi 			     prot, early_pgtable_alloc, flags);
437da141706SLaura Abbott }
438da141706SLaura Abbott 
4395ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void)
4405ea5306cSArd Biesheuvel {
4415ea5306cSArd Biesheuvel 	/*
4425ea5306cSArd Biesheuvel 	 * Remove the write permissions from the linear alias of .text/.rodata
4435ea5306cSArd Biesheuvel 	 */
4445ea5306cSArd Biesheuvel 	update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
4455ea5306cSArd Biesheuvel 			    (unsigned long)__init_begin - (unsigned long)_text,
4465ea5306cSArd Biesheuvel 			    PAGE_KERNEL_RO);
4475ea5306cSArd Biesheuvel }
4485ea5306cSArd Biesheuvel 
44920a004e7SWill Deacon static void __init map_mem(pgd_t *pgdp)
450c1cc1552SCatalin Marinas {
45198d2e153STakahiro Akashi 	phys_addr_t kernel_start = __pa_symbol(_text);
45298d2e153STakahiro Akashi 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
453c1cc1552SCatalin Marinas 	struct memblock_region *reg;
45498d2e153STakahiro Akashi 	int flags = 0;
45598d2e153STakahiro Akashi 
456c55191e9SArd Biesheuvel 	if (rodata_full || debug_pagealloc_enabled())
45798d2e153STakahiro Akashi 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
45898d2e153STakahiro Akashi 
45998d2e153STakahiro Akashi 	/*
46098d2e153STakahiro Akashi 	 * Take care not to create a writable alias for the
46198d2e153STakahiro Akashi 	 * read-only text and rodata sections of the kernel image.
46298d2e153STakahiro Akashi 	 * So temporarily mark them as NOMAP to skip mappings in
46398d2e153STakahiro Akashi 	 * the following for-loop
46498d2e153STakahiro Akashi 	 */
46598d2e153STakahiro Akashi 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
46698d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
46798d2e153STakahiro Akashi 	if (crashk_res.end)
46898d2e153STakahiro Akashi 		memblock_mark_nomap(crashk_res.start,
46998d2e153STakahiro Akashi 				    resource_size(&crashk_res));
47098d2e153STakahiro Akashi #endif
471f6bc87c3SSteve Capper 
472c1cc1552SCatalin Marinas 	/* map all the memory banks */
473c1cc1552SCatalin Marinas 	for_each_memblock(memory, reg) {
474c1cc1552SCatalin Marinas 		phys_addr_t start = reg->base;
475c1cc1552SCatalin Marinas 		phys_addr_t end = start + reg->size;
476c1cc1552SCatalin Marinas 
477c1cc1552SCatalin Marinas 		if (start >= end)
478c1cc1552SCatalin Marinas 			break;
47968709f45SArd Biesheuvel 		if (memblock_is_nomap(reg))
48068709f45SArd Biesheuvel 			continue;
481c1cc1552SCatalin Marinas 
48220a004e7SWill Deacon 		__map_memblock(pgdp, start, end, PAGE_KERNEL, flags);
483c1cc1552SCatalin Marinas 	}
48498d2e153STakahiro Akashi 
48598d2e153STakahiro Akashi 	/*
48698d2e153STakahiro Akashi 	 * Map the linear alias of the [_text, __init_begin) interval
48798d2e153STakahiro Akashi 	 * as non-executable now, and remove the write permission in
48898d2e153STakahiro Akashi 	 * mark_linear_text_alias_ro() below (which will be called after
48998d2e153STakahiro Akashi 	 * alternative patching has completed). This makes the contents
49098d2e153STakahiro Akashi 	 * of the region accessible to subsystems such as hibernate,
49198d2e153STakahiro Akashi 	 * but protects it from inadvertent modification or execution.
49298d2e153STakahiro Akashi 	 * Note that contiguous mappings cannot be remapped in this way,
49398d2e153STakahiro Akashi 	 * so we should avoid them here.
49498d2e153STakahiro Akashi 	 */
49520a004e7SWill Deacon 	__map_memblock(pgdp, kernel_start, kernel_end,
49698d2e153STakahiro Akashi 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
49798d2e153STakahiro Akashi 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
49898d2e153STakahiro Akashi 
49998d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
50098d2e153STakahiro Akashi 	/*
50198d2e153STakahiro Akashi 	 * Use page-level mappings here so that we can shrink the region
50298d2e153STakahiro Akashi 	 * in page granularity and put back unused memory to buddy system
50398d2e153STakahiro Akashi 	 * through /sys/kernel/kexec_crash_size interface.
50498d2e153STakahiro Akashi 	 */
50598d2e153STakahiro Akashi 	if (crashk_res.end) {
50620a004e7SWill Deacon 		__map_memblock(pgdp, crashk_res.start, crashk_res.end + 1,
50798d2e153STakahiro Akashi 			       PAGE_KERNEL,
50898d2e153STakahiro Akashi 			       NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
50998d2e153STakahiro Akashi 		memblock_clear_nomap(crashk_res.start,
51098d2e153STakahiro Akashi 				     resource_size(&crashk_res));
51198d2e153STakahiro Akashi 	}
51298d2e153STakahiro Akashi #endif
513c1cc1552SCatalin Marinas }
514c1cc1552SCatalin Marinas 
515da141706SLaura Abbott void mark_rodata_ro(void)
516da141706SLaura Abbott {
5172f39b5f9SJeremy Linton 	unsigned long section_size;
518f9040773SArd Biesheuvel 
5192f39b5f9SJeremy Linton 	/*
5209fdc14c5SArd Biesheuvel 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
5219fdc14c5SArd Biesheuvel 	 * to cover NOTES and EXCEPTION_TABLE.
5222f39b5f9SJeremy Linton 	 */
5239fdc14c5SArd Biesheuvel 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
524aa8c09beSArd Biesheuvel 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
5252f39b5f9SJeremy Linton 			    section_size, PAGE_KERNEL_RO);
526e98216b5SArd Biesheuvel 
5271404d6f1SLaura Abbott 	debug_checkwx();
528da141706SLaura Abbott }
529da141706SLaura Abbott 
53020a004e7SWill Deacon static void __init map_kernel_segment(pgd_t *pgdp, void *va_start, void *va_end,
531d27cfa1fSArd Biesheuvel 				      pgprot_t prot, struct vm_struct *vma,
53292bbd16eSWill Deacon 				      int flags, unsigned long vm_flags)
533068a17a5SMark Rutland {
5342077be67SLaura Abbott 	phys_addr_t pa_start = __pa_symbol(va_start);
535068a17a5SMark Rutland 	unsigned long size = va_end - va_start;
536068a17a5SMark Rutland 
537068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(pa_start));
538068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(size));
539068a17a5SMark Rutland 
54020a004e7SWill Deacon 	__create_pgd_mapping(pgdp, pa_start, (unsigned long)va_start, size, prot,
541d27cfa1fSArd Biesheuvel 			     early_pgtable_alloc, flags);
542f9040773SArd Biesheuvel 
54392bbd16eSWill Deacon 	if (!(vm_flags & VM_NO_GUARD))
54492bbd16eSWill Deacon 		size += PAGE_SIZE;
54592bbd16eSWill Deacon 
546f9040773SArd Biesheuvel 	vma->addr	= va_start;
547f9040773SArd Biesheuvel 	vma->phys_addr	= pa_start;
548f9040773SArd Biesheuvel 	vma->size	= size;
54992bbd16eSWill Deacon 	vma->flags	= VM_MAP | vm_flags;
550f9040773SArd Biesheuvel 	vma->caller	= __builtin_return_address(0);
551f9040773SArd Biesheuvel 
552f9040773SArd Biesheuvel 	vm_area_add_early(vma);
553068a17a5SMark Rutland }
554068a17a5SMark Rutland 
55528b066daSArd Biesheuvel static int __init parse_rodata(char *arg)
55628b066daSArd Biesheuvel {
557c55191e9SArd Biesheuvel 	int ret = strtobool(arg, &rodata_enabled);
558c55191e9SArd Biesheuvel 	if (!ret) {
559c55191e9SArd Biesheuvel 		rodata_full = false;
560c55191e9SArd Biesheuvel 		return 0;
561c55191e9SArd Biesheuvel 	}
562c55191e9SArd Biesheuvel 
563c55191e9SArd Biesheuvel 	/* permit 'full' in addition to boolean options */
564c55191e9SArd Biesheuvel 	if (strcmp(arg, "full"))
565c55191e9SArd Biesheuvel 		return -EINVAL;
566c55191e9SArd Biesheuvel 
567c55191e9SArd Biesheuvel 	rodata_enabled = true;
568c55191e9SArd Biesheuvel 	rodata_full = true;
569c55191e9SArd Biesheuvel 	return 0;
57028b066daSArd Biesheuvel }
57128b066daSArd Biesheuvel early_param("rodata", parse_rodata);
57228b066daSArd Biesheuvel 
57351a0048bSWill Deacon #ifdef CONFIG_UNMAP_KERNEL_AT_EL0
57451a0048bSWill Deacon static int __init map_entry_trampoline(void)
57551a0048bSWill Deacon {
57651a0048bSWill Deacon 	pgprot_t prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
57751a0048bSWill Deacon 	phys_addr_t pa_start = __pa_symbol(__entry_tramp_text_start);
57851a0048bSWill Deacon 
57951a0048bSWill Deacon 	/* The trampoline is always mapped and can therefore be global */
58051a0048bSWill Deacon 	pgprot_val(prot) &= ~PTE_NG;
58151a0048bSWill Deacon 
58251a0048bSWill Deacon 	/* Map only the text into the trampoline page table */
58351a0048bSWill Deacon 	memset(tramp_pg_dir, 0, PGD_SIZE);
58451a0048bSWill Deacon 	__create_pgd_mapping(tramp_pg_dir, pa_start, TRAMP_VALIAS, PAGE_SIZE,
58551a0048bSWill Deacon 			     prot, pgd_pgtable_alloc, 0);
58651a0048bSWill Deacon 
5876c27c408SWill Deacon 	/* Map both the text and data into the kernel page table */
58851a0048bSWill Deacon 	__set_fixmap(FIX_ENTRY_TRAMP_TEXT, pa_start, prot);
5896c27c408SWill Deacon 	if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
5906c27c408SWill Deacon 		extern char __entry_tramp_data_start[];
5916c27c408SWill Deacon 
5926c27c408SWill Deacon 		__set_fixmap(FIX_ENTRY_TRAMP_DATA,
5936c27c408SWill Deacon 			     __pa_symbol(__entry_tramp_data_start),
5946c27c408SWill Deacon 			     PAGE_KERNEL_RO);
5956c27c408SWill Deacon 	}
5966c27c408SWill Deacon 
59751a0048bSWill Deacon 	return 0;
59851a0048bSWill Deacon }
59951a0048bSWill Deacon core_initcall(map_entry_trampoline);
60051a0048bSWill Deacon #endif
60151a0048bSWill Deacon 
602068a17a5SMark Rutland /*
603068a17a5SMark Rutland  * Create fine-grained mappings for the kernel.
604068a17a5SMark Rutland  */
60520a004e7SWill Deacon static void __init map_kernel(pgd_t *pgdp)
606068a17a5SMark Rutland {
6072ebe088bSArd Biesheuvel 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
6082ebe088bSArd Biesheuvel 				vmlinux_initdata, vmlinux_data;
609068a17a5SMark Rutland 
61028b066daSArd Biesheuvel 	/*
61128b066daSArd Biesheuvel 	 * External debuggers may need to write directly to the text
61228b066daSArd Biesheuvel 	 * mapping to install SW breakpoints. Allow this (only) when
61328b066daSArd Biesheuvel 	 * explicitly requested with rodata=off.
61428b066daSArd Biesheuvel 	 */
61528b066daSArd Biesheuvel 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
61628b066daSArd Biesheuvel 
617d27cfa1fSArd Biesheuvel 	/*
618d27cfa1fSArd Biesheuvel 	 * Only rodata will be remapped with different permissions later on,
619d27cfa1fSArd Biesheuvel 	 * all other segments are allowed to use contiguous mappings.
620d27cfa1fSArd Biesheuvel 	 */
62120a004e7SWill Deacon 	map_kernel_segment(pgdp, _text, _etext, text_prot, &vmlinux_text, 0,
62292bbd16eSWill Deacon 			   VM_NO_GUARD);
62320a004e7SWill Deacon 	map_kernel_segment(pgdp, __start_rodata, __inittext_begin, PAGE_KERNEL,
62492bbd16eSWill Deacon 			   &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
62520a004e7SWill Deacon 	map_kernel_segment(pgdp, __inittext_begin, __inittext_end, text_prot,
62692bbd16eSWill Deacon 			   &vmlinux_inittext, 0, VM_NO_GUARD);
62720a004e7SWill Deacon 	map_kernel_segment(pgdp, __initdata_begin, __initdata_end, PAGE_KERNEL,
62892bbd16eSWill Deacon 			   &vmlinux_initdata, 0, VM_NO_GUARD);
62920a004e7SWill Deacon 	map_kernel_segment(pgdp, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
630068a17a5SMark Rutland 
63120a004e7SWill Deacon 	if (!READ_ONCE(pgd_val(*pgd_offset_raw(pgdp, FIXADDR_START)))) {
632068a17a5SMark Rutland 		/*
633f9040773SArd Biesheuvel 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
634f9040773SArd Biesheuvel 		 * live in the carveout for the swapper_pg_dir. We can simply
635f9040773SArd Biesheuvel 		 * re-use the existing dir for the fixmap.
636068a17a5SMark Rutland 		 */
63720a004e7SWill Deacon 		set_pgd(pgd_offset_raw(pgdp, FIXADDR_START),
63820a004e7SWill Deacon 			READ_ONCE(*pgd_offset_k(FIXADDR_START)));
639f9040773SArd Biesheuvel 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
640f9040773SArd Biesheuvel 		/*
641f9040773SArd Biesheuvel 		 * The fixmap shares its top level pgd entry with the kernel
642f9040773SArd Biesheuvel 		 * mapping. This can really only occur when we are running
643f9040773SArd Biesheuvel 		 * with 16k/4 levels, so we can simply reuse the pud level
644f9040773SArd Biesheuvel 		 * entry instead.
645f9040773SArd Biesheuvel 		 */
646f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
64720a004e7SWill Deacon 		pud_populate(&init_mm,
64820a004e7SWill Deacon 			     pud_set_fixmap_offset(pgdp, FIXADDR_START),
64919338304SKristina Martsenko 			     lm_alias(bm_pmd));
650f9040773SArd Biesheuvel 		pud_clear_fixmap();
651f9040773SArd Biesheuvel 	} else {
652f9040773SArd Biesheuvel 		BUG();
653f9040773SArd Biesheuvel 	}
654068a17a5SMark Rutland 
65520a004e7SWill Deacon 	kasan_copy_shadow(pgdp);
656068a17a5SMark Rutland }
657068a17a5SMark Rutland 
658c1cc1552SCatalin Marinas /*
659c1cc1552SCatalin Marinas  * paging_init() sets up the page tables, initialises the zone memory
660c1cc1552SCatalin Marinas  * maps and sets up the zero page.
661c1cc1552SCatalin Marinas  */
662c1cc1552SCatalin Marinas void __init paging_init(void)
663c1cc1552SCatalin Marinas {
6642330b7caSJun Yao 	pgd_t *pgdp = pgd_set_fixmap(__pa_symbol(swapper_pg_dir));
665068a17a5SMark Rutland 
66620a004e7SWill Deacon 	map_kernel(pgdp);
66720a004e7SWill Deacon 	map_mem(pgdp);
668068a17a5SMark Rutland 
669068a17a5SMark Rutland 	pgd_clear_fixmap();
670068a17a5SMark Rutland 
671068a17a5SMark Rutland 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
6722b5548b6SJun Yao 	init_mm.pgd = swapper_pg_dir;
673068a17a5SMark Rutland 
6742b5548b6SJun Yao 	memblock_free(__pa_symbol(init_pg_dir),
6752b5548b6SJun Yao 		      __pa_symbol(init_pg_end) - __pa_symbol(init_pg_dir));
67624cc61d8SArd Biesheuvel 
67724cc61d8SArd Biesheuvel 	memblock_allow_resize();
678c1cc1552SCatalin Marinas }
679c1cc1552SCatalin Marinas 
680c1cc1552SCatalin Marinas /*
681c1cc1552SCatalin Marinas  * Check whether a kernel address is valid (derived from arch/x86/).
682c1cc1552SCatalin Marinas  */
683c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr)
684c1cc1552SCatalin Marinas {
68520a004e7SWill Deacon 	pgd_t *pgdp;
68620a004e7SWill Deacon 	pud_t *pudp, pud;
68720a004e7SWill Deacon 	pmd_t *pmdp, pmd;
68820a004e7SWill Deacon 	pte_t *ptep, pte;
689c1cc1552SCatalin Marinas 
690c1cc1552SCatalin Marinas 	if ((((long)addr) >> VA_BITS) != -1UL)
691c1cc1552SCatalin Marinas 		return 0;
692c1cc1552SCatalin Marinas 
69320a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
69420a004e7SWill Deacon 	if (pgd_none(READ_ONCE(*pgdp)))
695c1cc1552SCatalin Marinas 		return 0;
696c1cc1552SCatalin Marinas 
69720a004e7SWill Deacon 	pudp = pud_offset(pgdp, addr);
69820a004e7SWill Deacon 	pud = READ_ONCE(*pudp);
69920a004e7SWill Deacon 	if (pud_none(pud))
700c1cc1552SCatalin Marinas 		return 0;
701c1cc1552SCatalin Marinas 
70220a004e7SWill Deacon 	if (pud_sect(pud))
70320a004e7SWill Deacon 		return pfn_valid(pud_pfn(pud));
704206a2a73SSteve Capper 
70520a004e7SWill Deacon 	pmdp = pmd_offset(pudp, addr);
70620a004e7SWill Deacon 	pmd = READ_ONCE(*pmdp);
70720a004e7SWill Deacon 	if (pmd_none(pmd))
708c1cc1552SCatalin Marinas 		return 0;
709c1cc1552SCatalin Marinas 
71020a004e7SWill Deacon 	if (pmd_sect(pmd))
71120a004e7SWill Deacon 		return pfn_valid(pmd_pfn(pmd));
712da6e4cb6SDave Anderson 
71320a004e7SWill Deacon 	ptep = pte_offset_kernel(pmdp, addr);
71420a004e7SWill Deacon 	pte = READ_ONCE(*ptep);
71520a004e7SWill Deacon 	if (pte_none(pte))
716c1cc1552SCatalin Marinas 		return 0;
717c1cc1552SCatalin Marinas 
71820a004e7SWill Deacon 	return pfn_valid(pte_pfn(pte));
719c1cc1552SCatalin Marinas }
720c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP
721b433dce0SSuzuki K. Poulose #if !ARM64_SWAPPER_USES_SECTION_MAPS
7227b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
7237b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
724c1cc1552SCatalin Marinas {
7250aad818bSJohannes Weiner 	return vmemmap_populate_basepages(start, end, node);
726c1cc1552SCatalin Marinas }
727b433dce0SSuzuki K. Poulose #else	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
7287b73d978SChristoph Hellwig int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
7297b73d978SChristoph Hellwig 		struct vmem_altmap *altmap)
730c1cc1552SCatalin Marinas {
7310aad818bSJohannes Weiner 	unsigned long addr = start;
732c1cc1552SCatalin Marinas 	unsigned long next;
73320a004e7SWill Deacon 	pgd_t *pgdp;
73420a004e7SWill Deacon 	pud_t *pudp;
73520a004e7SWill Deacon 	pmd_t *pmdp;
736c1cc1552SCatalin Marinas 
737c1cc1552SCatalin Marinas 	do {
738c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
739c1cc1552SCatalin Marinas 
74020a004e7SWill Deacon 		pgdp = vmemmap_pgd_populate(addr, node);
74120a004e7SWill Deacon 		if (!pgdp)
742c1cc1552SCatalin Marinas 			return -ENOMEM;
743c1cc1552SCatalin Marinas 
74420a004e7SWill Deacon 		pudp = vmemmap_pud_populate(pgdp, addr, node);
74520a004e7SWill Deacon 		if (!pudp)
746c1cc1552SCatalin Marinas 			return -ENOMEM;
747c1cc1552SCatalin Marinas 
74820a004e7SWill Deacon 		pmdp = pmd_offset(pudp, addr);
74920a004e7SWill Deacon 		if (pmd_none(READ_ONCE(*pmdp))) {
750c1cc1552SCatalin Marinas 			void *p = NULL;
751c1cc1552SCatalin Marinas 
752c1cc1552SCatalin Marinas 			p = vmemmap_alloc_block_buf(PMD_SIZE, node);
753c1cc1552SCatalin Marinas 			if (!p)
754c1cc1552SCatalin Marinas 				return -ENOMEM;
755c1cc1552SCatalin Marinas 
75620a004e7SWill Deacon 			pmd_set_huge(pmdp, __pa(p), __pgprot(PROT_SECT_NORMAL));
757c1cc1552SCatalin Marinas 		} else
75820a004e7SWill Deacon 			vmemmap_verify((pte_t *)pmdp, node, addr, next);
759c1cc1552SCatalin Marinas 	} while (addr = next, addr != end);
760c1cc1552SCatalin Marinas 
761c1cc1552SCatalin Marinas 	return 0;
762c1cc1552SCatalin Marinas }
763c1cc1552SCatalin Marinas #endif	/* CONFIG_ARM64_64K_PAGES */
76424b6d416SChristoph Hellwig void vmemmap_free(unsigned long start, unsigned long end,
76524b6d416SChristoph Hellwig 		struct vmem_altmap *altmap)
7660197518cSTang Chen {
7670197518cSTang Chen }
768c1cc1552SCatalin Marinas #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
769af86e597SLaura Abbott 
770af86e597SLaura Abbott static inline pud_t * fixmap_pud(unsigned long addr)
771af86e597SLaura Abbott {
77220a004e7SWill Deacon 	pgd_t *pgdp = pgd_offset_k(addr);
77320a004e7SWill Deacon 	pgd_t pgd = READ_ONCE(*pgdp);
774af86e597SLaura Abbott 
77520a004e7SWill Deacon 	BUG_ON(pgd_none(pgd) || pgd_bad(pgd));
776af86e597SLaura Abbott 
77720a004e7SWill Deacon 	return pud_offset_kimg(pgdp, addr);
778af86e597SLaura Abbott }
779af86e597SLaura Abbott 
780af86e597SLaura Abbott static inline pmd_t * fixmap_pmd(unsigned long addr)
781af86e597SLaura Abbott {
78220a004e7SWill Deacon 	pud_t *pudp = fixmap_pud(addr);
78320a004e7SWill Deacon 	pud_t pud = READ_ONCE(*pudp);
784af86e597SLaura Abbott 
78520a004e7SWill Deacon 	BUG_ON(pud_none(pud) || pud_bad(pud));
786af86e597SLaura Abbott 
78720a004e7SWill Deacon 	return pmd_offset_kimg(pudp, addr);
788af86e597SLaura Abbott }
789af86e597SLaura Abbott 
790af86e597SLaura Abbott static inline pte_t * fixmap_pte(unsigned long addr)
791af86e597SLaura Abbott {
792157962f5SArd Biesheuvel 	return &bm_pte[pte_index(addr)];
793af86e597SLaura Abbott }
794af86e597SLaura Abbott 
7952077be67SLaura Abbott /*
7962077be67SLaura Abbott  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
7972077be67SLaura Abbott  * directly on kernel symbols (bm_p*d). This function is called too early to use
7982077be67SLaura Abbott  * lm_alias so __p*d_populate functions must be used to populate with the
7992077be67SLaura Abbott  * physical address from __pa_symbol.
8002077be67SLaura Abbott  */
801af86e597SLaura Abbott void __init early_fixmap_init(void)
802af86e597SLaura Abbott {
80320a004e7SWill Deacon 	pgd_t *pgdp, pgd;
80420a004e7SWill Deacon 	pud_t *pudp;
80520a004e7SWill Deacon 	pmd_t *pmdp;
806af86e597SLaura Abbott 	unsigned long addr = FIXADDR_START;
807af86e597SLaura Abbott 
80820a004e7SWill Deacon 	pgdp = pgd_offset_k(addr);
80920a004e7SWill Deacon 	pgd = READ_ONCE(*pgdp);
810f80fb3a3SArd Biesheuvel 	if (CONFIG_PGTABLE_LEVELS > 3 &&
81120a004e7SWill Deacon 	    !(pgd_none(pgd) || pgd_page_paddr(pgd) == __pa_symbol(bm_pud))) {
812f9040773SArd Biesheuvel 		/*
813f9040773SArd Biesheuvel 		 * We only end up here if the kernel mapping and the fixmap
814f9040773SArd Biesheuvel 		 * share the top level pgd entry, which should only happen on
815f9040773SArd Biesheuvel 		 * 16k/4 levels configurations.
816f9040773SArd Biesheuvel 		 */
817f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
81820a004e7SWill Deacon 		pudp = pud_offset_kimg(pgdp, addr);
819f9040773SArd Biesheuvel 	} else {
82020a004e7SWill Deacon 		if (pgd_none(pgd))
82120a004e7SWill Deacon 			__pgd_populate(pgdp, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
82220a004e7SWill Deacon 		pudp = fixmap_pud(addr);
823f9040773SArd Biesheuvel 	}
82420a004e7SWill Deacon 	if (pud_none(READ_ONCE(*pudp)))
82520a004e7SWill Deacon 		__pud_populate(pudp, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
82620a004e7SWill Deacon 	pmdp = fixmap_pmd(addr);
82720a004e7SWill Deacon 	__pmd_populate(pmdp, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
828af86e597SLaura Abbott 
829af86e597SLaura Abbott 	/*
830af86e597SLaura Abbott 	 * The boot-ioremap range spans multiple pmds, for which
831157962f5SArd Biesheuvel 	 * we are not prepared:
832af86e597SLaura Abbott 	 */
833af86e597SLaura Abbott 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
834af86e597SLaura Abbott 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
835af86e597SLaura Abbott 
83620a004e7SWill Deacon 	if ((pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
83720a004e7SWill Deacon 	     || pmdp != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
838af86e597SLaura Abbott 		WARN_ON(1);
83920a004e7SWill Deacon 		pr_warn("pmdp %p != %p, %p\n",
84020a004e7SWill Deacon 			pmdp, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
841af86e597SLaura Abbott 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
842af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
843af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_BEGIN));
844af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
845af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_END));
846af86e597SLaura Abbott 
847af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
848af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
849af86e597SLaura Abbott 	}
850af86e597SLaura Abbott }
851af86e597SLaura Abbott 
85218b4b276SJames Morse /*
85318b4b276SJames Morse  * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
85418b4b276SJames Morse  * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
85518b4b276SJames Morse  */
856af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx,
857af86e597SLaura Abbott 			       phys_addr_t phys, pgprot_t flags)
858af86e597SLaura Abbott {
859af86e597SLaura Abbott 	unsigned long addr = __fix_to_virt(idx);
86020a004e7SWill Deacon 	pte_t *ptep;
861af86e597SLaura Abbott 
862b63dbef9SMark Rutland 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
863af86e597SLaura Abbott 
86420a004e7SWill Deacon 	ptep = fixmap_pte(addr);
865af86e597SLaura Abbott 
866af86e597SLaura Abbott 	if (pgprot_val(flags)) {
86720a004e7SWill Deacon 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, flags));
868af86e597SLaura Abbott 	} else {
86920a004e7SWill Deacon 		pte_clear(&init_mm, addr, ptep);
870af86e597SLaura Abbott 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
871af86e597SLaura Abbott 	}
872af86e597SLaura Abbott }
87361bd93ceSArd Biesheuvel 
874f80fb3a3SArd Biesheuvel void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
87561bd93ceSArd Biesheuvel {
87661bd93ceSArd Biesheuvel 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
877f80fb3a3SArd Biesheuvel 	int offset;
87861bd93ceSArd Biesheuvel 	void *dt_virt;
87961bd93ceSArd Biesheuvel 
88061bd93ceSArd Biesheuvel 	/*
88161bd93ceSArd Biesheuvel 	 * Check whether the physical FDT address is set and meets the minimum
88261bd93ceSArd Biesheuvel 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
88304a84810SArd Biesheuvel 	 * at least 8 bytes so that we can always access the magic and size
88404a84810SArd Biesheuvel 	 * fields of the FDT header after mapping the first chunk, double check
88504a84810SArd Biesheuvel 	 * here if that is indeed the case.
88661bd93ceSArd Biesheuvel 	 */
88761bd93ceSArd Biesheuvel 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
88861bd93ceSArd Biesheuvel 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
88961bd93ceSArd Biesheuvel 		return NULL;
89061bd93ceSArd Biesheuvel 
89161bd93ceSArd Biesheuvel 	/*
89261bd93ceSArd Biesheuvel 	 * Make sure that the FDT region can be mapped without the need to
89361bd93ceSArd Biesheuvel 	 * allocate additional translation table pages, so that it is safe
894132233a7SLaura Abbott 	 * to call create_mapping_noalloc() this early.
89561bd93ceSArd Biesheuvel 	 *
89661bd93ceSArd Biesheuvel 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
89761bd93ceSArd Biesheuvel 	 * be in the same PMD as the rest of the fixmap.
89861bd93ceSArd Biesheuvel 	 * On 4k pages, we'll use section mappings for the FDT so we only
89961bd93ceSArd Biesheuvel 	 * have to be in the same PUD.
90061bd93ceSArd Biesheuvel 	 */
90161bd93ceSArd Biesheuvel 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
90261bd93ceSArd Biesheuvel 
903b433dce0SSuzuki K. Poulose 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
904b433dce0SSuzuki K. Poulose 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
90561bd93ceSArd Biesheuvel 
906b433dce0SSuzuki K. Poulose 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
90761bd93ceSArd Biesheuvel 	dt_virt = (void *)dt_virt_base + offset;
90861bd93ceSArd Biesheuvel 
90961bd93ceSArd Biesheuvel 	/* map the first chunk so we can read the size from the header */
910132233a7SLaura Abbott 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
911132233a7SLaura Abbott 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
91261bd93ceSArd Biesheuvel 
91304a84810SArd Biesheuvel 	if (fdt_magic(dt_virt) != FDT_MAGIC)
91461bd93ceSArd Biesheuvel 		return NULL;
91561bd93ceSArd Biesheuvel 
916f80fb3a3SArd Biesheuvel 	*size = fdt_totalsize(dt_virt);
917f80fb3a3SArd Biesheuvel 	if (*size > MAX_FDT_SIZE)
91861bd93ceSArd Biesheuvel 		return NULL;
91961bd93ceSArd Biesheuvel 
920f80fb3a3SArd Biesheuvel 	if (offset + *size > SWAPPER_BLOCK_SIZE)
921132233a7SLaura Abbott 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
922f80fb3a3SArd Biesheuvel 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
923f80fb3a3SArd Biesheuvel 
924f80fb3a3SArd Biesheuvel 	return dt_virt;
925f80fb3a3SArd Biesheuvel }
926f80fb3a3SArd Biesheuvel 
927f80fb3a3SArd Biesheuvel void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
928f80fb3a3SArd Biesheuvel {
929f80fb3a3SArd Biesheuvel 	void *dt_virt;
930f80fb3a3SArd Biesheuvel 	int size;
931f80fb3a3SArd Biesheuvel 
932f80fb3a3SArd Biesheuvel 	dt_virt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
933f80fb3a3SArd Biesheuvel 	if (!dt_virt)
934f80fb3a3SArd Biesheuvel 		return NULL;
93561bd93ceSArd Biesheuvel 
93661bd93ceSArd Biesheuvel 	memblock_reserve(dt_phys, size);
93761bd93ceSArd Biesheuvel 	return dt_virt;
93861bd93ceSArd Biesheuvel }
939324420bfSArd Biesheuvel 
940324420bfSArd Biesheuvel int __init arch_ioremap_pud_supported(void)
941324420bfSArd Biesheuvel {
942324420bfSArd Biesheuvel 	/* only 4k granule supports level 1 block mappings */
943324420bfSArd Biesheuvel 	return IS_ENABLED(CONFIG_ARM64_4K_PAGES);
944324420bfSArd Biesheuvel }
945324420bfSArd Biesheuvel 
946324420bfSArd Biesheuvel int __init arch_ioremap_pmd_supported(void)
947324420bfSArd Biesheuvel {
948324420bfSArd Biesheuvel 	return 1;
949324420bfSArd Biesheuvel }
950324420bfSArd Biesheuvel 
95120a004e7SWill Deacon int pud_set_huge(pud_t *pudp, phys_addr_t phys, pgprot_t prot)
952324420bfSArd Biesheuvel {
95319338304SKristina Martsenko 	pgprot_t sect_prot = __pgprot(PUD_TYPE_SECT |
95419338304SKristina Martsenko 					pgprot_val(mk_sect_prot(prot)));
95582034c23SLaura Abbott 	pud_t new_pud = pfn_pud(__phys_to_pfn(phys), sect_prot);
95615122ee2SWill Deacon 
95782034c23SLaura Abbott 	/* Only allow permission changes for now */
95882034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pud_val(*pudp)),
95982034c23SLaura Abbott 				   pud_val(new_pud)))
96015122ee2SWill Deacon 		return 0;
96115122ee2SWill Deacon 
962324420bfSArd Biesheuvel 	BUG_ON(phys & ~PUD_MASK);
96382034c23SLaura Abbott 	set_pud(pudp, new_pud);
964324420bfSArd Biesheuvel 	return 1;
965324420bfSArd Biesheuvel }
966324420bfSArd Biesheuvel 
96720a004e7SWill Deacon int pmd_set_huge(pmd_t *pmdp, phys_addr_t phys, pgprot_t prot)
968324420bfSArd Biesheuvel {
96919338304SKristina Martsenko 	pgprot_t sect_prot = __pgprot(PMD_TYPE_SECT |
97019338304SKristina Martsenko 					pgprot_val(mk_sect_prot(prot)));
97182034c23SLaura Abbott 	pmd_t new_pmd = pfn_pmd(__phys_to_pfn(phys), sect_prot);
97215122ee2SWill Deacon 
97382034c23SLaura Abbott 	/* Only allow permission changes for now */
97482034c23SLaura Abbott 	if (!pgattr_change_is_safe(READ_ONCE(pmd_val(*pmdp)),
97582034c23SLaura Abbott 				   pmd_val(new_pmd)))
97615122ee2SWill Deacon 		return 0;
97715122ee2SWill Deacon 
978324420bfSArd Biesheuvel 	BUG_ON(phys & ~PMD_MASK);
97982034c23SLaura Abbott 	set_pmd(pmdp, new_pmd);
980324420bfSArd Biesheuvel 	return 1;
981324420bfSArd Biesheuvel }
982324420bfSArd Biesheuvel 
98320a004e7SWill Deacon int pud_clear_huge(pud_t *pudp)
984324420bfSArd Biesheuvel {
98520a004e7SWill Deacon 	if (!pud_sect(READ_ONCE(*pudp)))
986324420bfSArd Biesheuvel 		return 0;
98720a004e7SWill Deacon 	pud_clear(pudp);
988324420bfSArd Biesheuvel 	return 1;
989324420bfSArd Biesheuvel }
990324420bfSArd Biesheuvel 
99120a004e7SWill Deacon int pmd_clear_huge(pmd_t *pmdp)
992324420bfSArd Biesheuvel {
99320a004e7SWill Deacon 	if (!pmd_sect(READ_ONCE(*pmdp)))
994324420bfSArd Biesheuvel 		return 0;
99520a004e7SWill Deacon 	pmd_clear(pmdp);
996324420bfSArd Biesheuvel 	return 1;
997324420bfSArd Biesheuvel }
998b6bdb751SToshi Kani 
999ec28bb9cSChintan Pandya int pmd_free_pte_page(pmd_t *pmdp, unsigned long addr)
1000b6bdb751SToshi Kani {
1001ec28bb9cSChintan Pandya 	pte_t *table;
1002ec28bb9cSChintan Pandya 	pmd_t pmd;
1003ec28bb9cSChintan Pandya 
1004ec28bb9cSChintan Pandya 	pmd = READ_ONCE(*pmdp);
1005ec28bb9cSChintan Pandya 
1006fac880c7SMark Rutland 	if (!pmd_table(pmd)) {
10079c006972SWill Deacon 		VM_WARN_ON(1);
1008ec28bb9cSChintan Pandya 		return 1;
1009b6bdb751SToshi Kani 	}
1010b6bdb751SToshi Kani 
1011ec28bb9cSChintan Pandya 	table = pte_offset_kernel(pmdp, addr);
1012ec28bb9cSChintan Pandya 	pmd_clear(pmdp);
1013ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1014ec28bb9cSChintan Pandya 	pte_free_kernel(NULL, table);
1015ec28bb9cSChintan Pandya 	return 1;
1016ec28bb9cSChintan Pandya }
1017ec28bb9cSChintan Pandya 
1018ec28bb9cSChintan Pandya int pud_free_pmd_page(pud_t *pudp, unsigned long addr)
1019b6bdb751SToshi Kani {
1020ec28bb9cSChintan Pandya 	pmd_t *table;
1021ec28bb9cSChintan Pandya 	pmd_t *pmdp;
1022ec28bb9cSChintan Pandya 	pud_t pud;
1023ec28bb9cSChintan Pandya 	unsigned long next, end;
1024ec28bb9cSChintan Pandya 
1025ec28bb9cSChintan Pandya 	pud = READ_ONCE(*pudp);
1026ec28bb9cSChintan Pandya 
1027fac880c7SMark Rutland 	if (!pud_table(pud)) {
10289c006972SWill Deacon 		VM_WARN_ON(1);
1029ec28bb9cSChintan Pandya 		return 1;
1030ec28bb9cSChintan Pandya 	}
1031ec28bb9cSChintan Pandya 
1032ec28bb9cSChintan Pandya 	table = pmd_offset(pudp, addr);
1033ec28bb9cSChintan Pandya 	pmdp = table;
1034ec28bb9cSChintan Pandya 	next = addr;
1035ec28bb9cSChintan Pandya 	end = addr + PUD_SIZE;
1036ec28bb9cSChintan Pandya 	do {
1037ec28bb9cSChintan Pandya 		pmd_free_pte_page(pmdp, next);
1038ec28bb9cSChintan Pandya 	} while (pmdp++, next += PMD_SIZE, next != end);
1039ec28bb9cSChintan Pandya 
1040ec28bb9cSChintan Pandya 	pud_clear(pudp);
1041ec28bb9cSChintan Pandya 	__flush_tlb_kernel_pgtable(addr);
1042ec28bb9cSChintan Pandya 	pmd_free(NULL, table);
1043ec28bb9cSChintan Pandya 	return 1;
1044b6bdb751SToshi Kani }
10454ab21506SRobin Murphy 
10468e2d4340SWill Deacon int p4d_free_pud_page(p4d_t *p4d, unsigned long addr)
10478e2d4340SWill Deacon {
10488e2d4340SWill Deacon 	return 0;	/* Don't attempt a block mapping */
10498e2d4340SWill Deacon }
10508e2d4340SWill Deacon 
10514ab21506SRobin Murphy #ifdef CONFIG_MEMORY_HOTPLUG
10524ab21506SRobin Murphy int arch_add_memory(int nid, u64 start, u64 size, struct vmem_altmap *altmap,
10534ab21506SRobin Murphy 		    bool want_memblock)
10544ab21506SRobin Murphy {
10554ab21506SRobin Murphy 	int flags = 0;
10564ab21506SRobin Murphy 
10574ab21506SRobin Murphy 	if (rodata_full || debug_pagealloc_enabled())
10584ab21506SRobin Murphy 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
10594ab21506SRobin Murphy 
10604ab21506SRobin Murphy 	__create_pgd_mapping(swapper_pg_dir, start, __phys_to_virt(start),
10614ab21506SRobin Murphy 			     size, PAGE_KERNEL, pgd_pgtable_alloc, flags);
10624ab21506SRobin Murphy 
10634ab21506SRobin Murphy 	return __add_pages(nid, start >> PAGE_SHIFT, size >> PAGE_SHIFT,
10644ab21506SRobin Murphy 			   altmap, want_memblock);
10654ab21506SRobin Murphy }
10664ab21506SRobin Murphy #endif
1067