xref: /openbmc/linux/arch/arm64/mm/mmu.c (revision d27cfa1fc823d35a6cf45ba51f5623db8a14a9b9)
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>
2561bd93ceSArd Biesheuvel #include <linux/libfdt.h>
26c1cc1552SCatalin Marinas #include <linux/mman.h>
27c1cc1552SCatalin Marinas #include <linux/nodemask.h>
28c1cc1552SCatalin Marinas #include <linux/memblock.h>
29c1cc1552SCatalin Marinas #include <linux/fs.h>
302475ff9dSCatalin Marinas #include <linux/io.h>
312077be67SLaura Abbott #include <linux/mm.h>
32c1cc1552SCatalin Marinas 
3321ab99c2SMark Rutland #include <asm/barrier.h>
34c1cc1552SCatalin Marinas #include <asm/cputype.h>
35af86e597SLaura Abbott #include <asm/fixmap.h>
36068a17a5SMark Rutland #include <asm/kasan.h>
37b433dce0SSuzuki K. Poulose #include <asm/kernel-pgtable.h>
38c1cc1552SCatalin Marinas #include <asm/sections.h>
39c1cc1552SCatalin Marinas #include <asm/setup.h>
40c1cc1552SCatalin Marinas #include <asm/sizes.h>
41c1cc1552SCatalin Marinas #include <asm/tlb.h>
42c79b954bSJungseok Lee #include <asm/memblock.h>
43c1cc1552SCatalin Marinas #include <asm/mmu_context.h>
441404d6f1SLaura Abbott #include <asm/ptdump.h>
45c1cc1552SCatalin Marinas 
46c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS	BIT(0)
47*d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS	BIT(1)
48c0951366SArd Biesheuvel 
49dd006da2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
50dd006da2SArd Biesheuvel 
515a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init;
52a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset);
53a7f8de16SArd Biesheuvel 
54c1cc1552SCatalin Marinas /*
55c1cc1552SCatalin Marinas  * Empty_zero_page is a special page that is used for zero-initialized data
56c1cc1552SCatalin Marinas  * and COW.
57c1cc1552SCatalin Marinas  */
585227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
59c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page);
60c1cc1552SCatalin Marinas 
61f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
62f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
63f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
64f9040773SArd Biesheuvel 
65c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
66c1cc1552SCatalin Marinas 			      unsigned long size, pgprot_t vma_prot)
67c1cc1552SCatalin Marinas {
68c1cc1552SCatalin Marinas 	if (!pfn_valid(pfn))
69c1cc1552SCatalin Marinas 		return pgprot_noncached(vma_prot);
70c1cc1552SCatalin Marinas 	else if (file->f_flags & O_SYNC)
71c1cc1552SCatalin Marinas 		return pgprot_writecombine(vma_prot);
72c1cc1552SCatalin Marinas 	return vma_prot;
73c1cc1552SCatalin Marinas }
74c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot);
75c1cc1552SCatalin Marinas 
76f4710445SMark Rutland static phys_addr_t __init early_pgtable_alloc(void)
77c1cc1552SCatalin Marinas {
787142392dSSuzuki K. Poulose 	phys_addr_t phys;
797142392dSSuzuki K. Poulose 	void *ptr;
807142392dSSuzuki K. Poulose 
8121ab99c2SMark Rutland 	phys = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
82f4710445SMark Rutland 
83f4710445SMark Rutland 	/*
84f4710445SMark Rutland 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
85f4710445SMark Rutland 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
86f4710445SMark Rutland 	 * any level of table.
87f4710445SMark Rutland 	 */
88f4710445SMark Rutland 	ptr = pte_set_fixmap(phys);
89f4710445SMark Rutland 
9021ab99c2SMark Rutland 	memset(ptr, 0, PAGE_SIZE);
9121ab99c2SMark Rutland 
92f4710445SMark Rutland 	/*
93f4710445SMark Rutland 	 * Implicit barriers also ensure the zeroed page is visible to the page
94f4710445SMark Rutland 	 * table walker
95f4710445SMark Rutland 	 */
96f4710445SMark Rutland 	pte_clear_fixmap();
97f4710445SMark Rutland 
98f4710445SMark Rutland 	return phys;
99c1cc1552SCatalin Marinas }
100c1cc1552SCatalin Marinas 
101e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new)
102e98216b5SArd Biesheuvel {
103e98216b5SArd Biesheuvel 	/*
104e98216b5SArd Biesheuvel 	 * The following mapping attributes may be updated in live
105e98216b5SArd Biesheuvel 	 * kernel mappings without the need for break-before-make.
106e98216b5SArd Biesheuvel 	 */
107e98216b5SArd Biesheuvel 	static const pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE;
108e98216b5SArd Biesheuvel 
109141d1497SArd Biesheuvel 	/* creating or taking down mappings is always safe */
110141d1497SArd Biesheuvel 	if (old == 0 || new == 0)
111141d1497SArd Biesheuvel 		return true;
112141d1497SArd Biesheuvel 
113141d1497SArd Biesheuvel 	/* live contiguous mappings may not be manipulated at all */
114141d1497SArd Biesheuvel 	if ((old | new) & PTE_CONT)
115141d1497SArd Biesheuvel 		return false;
116141d1497SArd Biesheuvel 
117141d1497SArd Biesheuvel 	return ((old ^ new) & ~mask) == 0;
118e98216b5SArd Biesheuvel }
119e98216b5SArd Biesheuvel 
120*d27cfa1fSArd Biesheuvel static void init_pte(pmd_t *pmd, unsigned long addr, unsigned long end,
121*d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot)
122c1cc1552SCatalin Marinas {
123c1cc1552SCatalin Marinas 	pte_t *pte;
124c1cc1552SCatalin Marinas 
125f4710445SMark Rutland 	pte = pte_set_fixmap_offset(pmd, addr);
126c1cc1552SCatalin Marinas 	do {
127e98216b5SArd Biesheuvel 		pte_t old_pte = *pte;
128e98216b5SArd Biesheuvel 
129e393cf40SArd Biesheuvel 		set_pte(pte, pfn_pte(__phys_to_pfn(phys), prot));
130e98216b5SArd Biesheuvel 
131e98216b5SArd Biesheuvel 		/*
132e98216b5SArd Biesheuvel 		 * After the PTE entry has been populated once, we
133e98216b5SArd Biesheuvel 		 * only allow updates to the permission attributes.
134e98216b5SArd Biesheuvel 		 */
135e98216b5SArd Biesheuvel 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), pte_val(*pte)));
136e98216b5SArd Biesheuvel 
137e393cf40SArd Biesheuvel 		phys += PAGE_SIZE;
138667c2759SCatalin Marinas 	} while (pte++, addr += PAGE_SIZE, addr != end);
139f4710445SMark Rutland 
140f4710445SMark Rutland 	pte_clear_fixmap();
141c1cc1552SCatalin Marinas }
142c1cc1552SCatalin Marinas 
143*d27cfa1fSArd Biesheuvel static void alloc_init_cont_pte(pmd_t *pmd, unsigned long addr,
144*d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
145*d27cfa1fSArd Biesheuvel 				pgprot_t prot,
14653e1b329SArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void),
147c0951366SArd Biesheuvel 				int flags)
148c1cc1552SCatalin Marinas {
149c1cc1552SCatalin Marinas 	unsigned long next;
150c1cc1552SCatalin Marinas 
151*d27cfa1fSArd Biesheuvel 	BUG_ON(pmd_sect(*pmd));
152*d27cfa1fSArd Biesheuvel 	if (pmd_none(*pmd)) {
153*d27cfa1fSArd Biesheuvel 		phys_addr_t pte_phys;
154132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
155*d27cfa1fSArd Biesheuvel 		pte_phys = pgtable_alloc();
156*d27cfa1fSArd Biesheuvel 		__pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE);
157c1cc1552SCatalin Marinas 	}
158*d27cfa1fSArd Biesheuvel 	BUG_ON(pmd_bad(*pmd));
159*d27cfa1fSArd Biesheuvel 
160*d27cfa1fSArd Biesheuvel 	do {
161*d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
162*d27cfa1fSArd Biesheuvel 
163*d27cfa1fSArd Biesheuvel 		next = pte_cont_addr_end(addr, end);
164*d27cfa1fSArd Biesheuvel 
165*d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
166*d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
167*d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
168*d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
169*d27cfa1fSArd Biesheuvel 
170*d27cfa1fSArd Biesheuvel 		init_pte(pmd, addr, next, phys, __prot);
171*d27cfa1fSArd Biesheuvel 
172*d27cfa1fSArd Biesheuvel 		phys += next - addr;
173*d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
174*d27cfa1fSArd Biesheuvel }
175*d27cfa1fSArd Biesheuvel 
176*d27cfa1fSArd Biesheuvel static void init_pmd(pud_t *pud, unsigned long addr, unsigned long end,
177*d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot,
178*d27cfa1fSArd Biesheuvel 		     phys_addr_t (*pgtable_alloc)(void), int flags)
179*d27cfa1fSArd Biesheuvel {
180*d27cfa1fSArd Biesheuvel 	unsigned long next;
181*d27cfa1fSArd Biesheuvel 	pmd_t *pmd;
182c1cc1552SCatalin Marinas 
183f4710445SMark Rutland 	pmd = pmd_set_fixmap_offset(pud, addr);
184c1cc1552SCatalin Marinas 	do {
185e98216b5SArd Biesheuvel 		pmd_t old_pmd = *pmd;
186e98216b5SArd Biesheuvel 
187c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
188e98216b5SArd Biesheuvel 
189c1cc1552SCatalin Marinas 		/* try section mapping first */
19083863f25SLaura Abbott 		if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
191c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
192d81bbe6dSMark Rutland 			pmd_set_huge(pmd, phys, prot);
193e98216b5SArd Biesheuvel 
194a55f9929SCatalin Marinas 			/*
195e98216b5SArd Biesheuvel 			 * After the PMD entry has been populated once, we
196e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
197a55f9929SCatalin Marinas 			 */
198e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
199e98216b5SArd Biesheuvel 						      pmd_val(*pmd)));
200a55f9929SCatalin Marinas 		} else {
201*d27cfa1fSArd Biesheuvel 			alloc_init_cont_pte(pmd, addr, next, phys, prot,
202*d27cfa1fSArd Biesheuvel 					    pgtable_alloc, flags);
203e98216b5SArd Biesheuvel 
204e98216b5SArd Biesheuvel 			BUG_ON(pmd_val(old_pmd) != 0 &&
205e98216b5SArd Biesheuvel 			       pmd_val(old_pmd) != pmd_val(*pmd));
206a55f9929SCatalin Marinas 		}
207c1cc1552SCatalin Marinas 		phys += next - addr;
208c1cc1552SCatalin Marinas 	} while (pmd++, addr = next, addr != end);
209f4710445SMark Rutland 
210f4710445SMark Rutland 	pmd_clear_fixmap();
211c1cc1552SCatalin Marinas }
212c1cc1552SCatalin Marinas 
213*d27cfa1fSArd Biesheuvel static void alloc_init_cont_pmd(pud_t *pud, unsigned long addr,
214*d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
215*d27cfa1fSArd Biesheuvel 				pgprot_t prot,
216*d27cfa1fSArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void), int flags)
217*d27cfa1fSArd Biesheuvel {
218*d27cfa1fSArd Biesheuvel 	unsigned long next;
219*d27cfa1fSArd Biesheuvel 
220*d27cfa1fSArd Biesheuvel 	/*
221*d27cfa1fSArd Biesheuvel 	 * Check for initial section mappings in the pgd/pud.
222*d27cfa1fSArd Biesheuvel 	 */
223*d27cfa1fSArd Biesheuvel 	BUG_ON(pud_sect(*pud));
224*d27cfa1fSArd Biesheuvel 	if (pud_none(*pud)) {
225*d27cfa1fSArd Biesheuvel 		phys_addr_t pmd_phys;
226*d27cfa1fSArd Biesheuvel 		BUG_ON(!pgtable_alloc);
227*d27cfa1fSArd Biesheuvel 		pmd_phys = pgtable_alloc();
228*d27cfa1fSArd Biesheuvel 		__pud_populate(pud, pmd_phys, PUD_TYPE_TABLE);
229*d27cfa1fSArd Biesheuvel 	}
230*d27cfa1fSArd Biesheuvel 	BUG_ON(pud_bad(*pud));
231*d27cfa1fSArd Biesheuvel 
232*d27cfa1fSArd Biesheuvel 	do {
233*d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
234*d27cfa1fSArd Biesheuvel 
235*d27cfa1fSArd Biesheuvel 		next = pmd_cont_addr_end(addr, end);
236*d27cfa1fSArd Biesheuvel 
237*d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
238*d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
239*d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
240*d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
241*d27cfa1fSArd Biesheuvel 
242*d27cfa1fSArd Biesheuvel 		init_pmd(pud, addr, next, phys, __prot, pgtable_alloc, flags);
243*d27cfa1fSArd Biesheuvel 
244*d27cfa1fSArd Biesheuvel 		phys += next - addr;
245*d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
246*d27cfa1fSArd Biesheuvel }
247*d27cfa1fSArd Biesheuvel 
248da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next,
249da141706SLaura Abbott 			unsigned long phys)
250da141706SLaura Abbott {
251da141706SLaura Abbott 	if (PAGE_SHIFT != 12)
252da141706SLaura Abbott 		return false;
253da141706SLaura Abbott 
254da141706SLaura Abbott 	if (((addr | next | phys) & ~PUD_MASK) != 0)
255da141706SLaura Abbott 		return false;
256da141706SLaura Abbott 
257da141706SLaura Abbott 	return true;
258da141706SLaura Abbott }
259da141706SLaura Abbott 
26011509a30SMark Rutland static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
261da141706SLaura Abbott 				  phys_addr_t phys, pgprot_t prot,
26253e1b329SArd Biesheuvel 				  phys_addr_t (*pgtable_alloc)(void),
263c0951366SArd Biesheuvel 				  int flags)
264c1cc1552SCatalin Marinas {
265c79b954bSJungseok Lee 	pud_t *pud;
266c1cc1552SCatalin Marinas 	unsigned long next;
267c1cc1552SCatalin Marinas 
268c79b954bSJungseok Lee 	if (pgd_none(*pgd)) {
269132233a7SLaura Abbott 		phys_addr_t pud_phys;
270132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
271132233a7SLaura Abbott 		pud_phys = pgtable_alloc();
272f4710445SMark Rutland 		__pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE);
273c79b954bSJungseok Lee 	}
274c79b954bSJungseok Lee 	BUG_ON(pgd_bad(*pgd));
275c79b954bSJungseok Lee 
276f4710445SMark Rutland 	pud = pud_set_fixmap_offset(pgd, addr);
277c1cc1552SCatalin Marinas 	do {
278e98216b5SArd Biesheuvel 		pud_t old_pud = *pud;
279e98216b5SArd Biesheuvel 
280c1cc1552SCatalin Marinas 		next = pud_addr_end(addr, end);
281206a2a73SSteve Capper 
282206a2a73SSteve Capper 		/*
283206a2a73SSteve Capper 		 * For 4K granule only, attempt to put down a 1GB block
284206a2a73SSteve Capper 		 */
285c0951366SArd Biesheuvel 		if (use_1G_block(addr, next, phys) &&
286c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
287c661cb1cSMark Rutland 			pud_set_huge(pud, phys, prot);
288206a2a73SSteve Capper 
289206a2a73SSteve Capper 			/*
290e98216b5SArd Biesheuvel 			 * After the PUD entry has been populated once, we
291e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
292206a2a73SSteve Capper 			 */
293e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
294e98216b5SArd Biesheuvel 						      pud_val(*pud)));
295206a2a73SSteve Capper 		} else {
296*d27cfa1fSArd Biesheuvel 			alloc_init_cont_pmd(pud, addr, next, phys, prot,
297c0951366SArd Biesheuvel 					    pgtable_alloc, flags);
298e98216b5SArd Biesheuvel 
299e98216b5SArd Biesheuvel 			BUG_ON(pud_val(old_pud) != 0 &&
300e98216b5SArd Biesheuvel 			       pud_val(old_pud) != pud_val(*pud));
301206a2a73SSteve Capper 		}
302c1cc1552SCatalin Marinas 		phys += next - addr;
303c1cc1552SCatalin Marinas 	} while (pud++, addr = next, addr != end);
304f4710445SMark Rutland 
305f4710445SMark Rutland 	pud_clear_fixmap();
306c1cc1552SCatalin Marinas }
307c1cc1552SCatalin Marinas 
30840f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
30940f87d31SArd Biesheuvel 				 unsigned long virt, phys_addr_t size,
31040f87d31SArd Biesheuvel 				 pgprot_t prot,
31153e1b329SArd Biesheuvel 				 phys_addr_t (*pgtable_alloc)(void),
312c0951366SArd Biesheuvel 				 int flags)
313c1cc1552SCatalin Marinas {
314c1cc1552SCatalin Marinas 	unsigned long addr, length, end, next;
31540f87d31SArd Biesheuvel 	pgd_t *pgd = pgd_offset_raw(pgdir, virt);
316c1cc1552SCatalin Marinas 
317cc5d2b3bSMark Rutland 	/*
318cc5d2b3bSMark Rutland 	 * If the virtual and physical address don't have the same offset
319cc5d2b3bSMark Rutland 	 * within a page, we cannot map the region as the caller expects.
320cc5d2b3bSMark Rutland 	 */
321cc5d2b3bSMark Rutland 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
322cc5d2b3bSMark Rutland 		return;
323cc5d2b3bSMark Rutland 
3249c4e08a3SMark Rutland 	phys &= PAGE_MASK;
325c1cc1552SCatalin Marinas 	addr = virt & PAGE_MASK;
326c1cc1552SCatalin Marinas 	length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
327c1cc1552SCatalin Marinas 
328c1cc1552SCatalin Marinas 	end = addr + length;
329c1cc1552SCatalin Marinas 	do {
330c1cc1552SCatalin Marinas 		next = pgd_addr_end(addr, end);
33153e1b329SArd Biesheuvel 		alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc,
332c0951366SArd Biesheuvel 			       flags);
333c1cc1552SCatalin Marinas 		phys += next - addr;
334c1cc1552SCatalin Marinas 	} while (pgd++, addr = next, addr != end);
335c1cc1552SCatalin Marinas }
336c1cc1552SCatalin Marinas 
3371378dc3dSArd Biesheuvel static phys_addr_t pgd_pgtable_alloc(void)
338da141706SLaura Abbott {
33921ab99c2SMark Rutland 	void *ptr = (void *)__get_free_page(PGALLOC_GFP);
3401378dc3dSArd Biesheuvel 	if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
3411378dc3dSArd Biesheuvel 		BUG();
34221ab99c2SMark Rutland 
34321ab99c2SMark Rutland 	/* Ensure the zeroed page is visible to the page table walker */
34421ab99c2SMark Rutland 	dsb(ishst);
345f4710445SMark Rutland 	return __pa(ptr);
346da141706SLaura Abbott }
347da141706SLaura Abbott 
348132233a7SLaura Abbott /*
349132233a7SLaura Abbott  * This function can only be used to modify existing table entries,
350132233a7SLaura Abbott  * without allocating new levels of table. Note that this permits the
351132233a7SLaura Abbott  * creation of new section or page entries.
352132233a7SLaura Abbott  */
353132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
354da141706SLaura Abbott 				  phys_addr_t size, pgprot_t prot)
355d7ecbddfSMark Salter {
356d7ecbddfSMark Salter 	if (virt < VMALLOC_START) {
357d7ecbddfSMark Salter 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
358d7ecbddfSMark Salter 			&phys, virt);
359d7ecbddfSMark Salter 		return;
360d7ecbddfSMark Salter 	}
361*d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
362*d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
363d7ecbddfSMark Salter }
364d7ecbddfSMark Salter 
3658ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
3668ce837ceSArd Biesheuvel 			       unsigned long virt, phys_addr_t size,
367f14c66ceSArd Biesheuvel 			       pgprot_t prot, bool page_mappings_only)
3688ce837ceSArd Biesheuvel {
369c0951366SArd Biesheuvel 	int flags = 0;
370c0951366SArd Biesheuvel 
3711378dc3dSArd Biesheuvel 	BUG_ON(mm == &init_mm);
3721378dc3dSArd Biesheuvel 
373c0951366SArd Biesheuvel 	if (page_mappings_only)
374*d27cfa1fSArd Biesheuvel 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
375c0951366SArd Biesheuvel 
37611509a30SMark Rutland 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
377c0951366SArd Biesheuvel 			     pgd_pgtable_alloc, flags);
378d7ecbddfSMark Salter }
379d7ecbddfSMark Salter 
380aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
381da141706SLaura Abbott 				phys_addr_t size, pgprot_t prot)
382da141706SLaura Abbott {
383da141706SLaura Abbott 	if (virt < VMALLOC_START) {
384aa8c09beSArd Biesheuvel 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
385da141706SLaura Abbott 			&phys, virt);
386da141706SLaura Abbott 		return;
387da141706SLaura Abbott 	}
388da141706SLaura Abbott 
389*d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
390*d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
391aa8c09beSArd Biesheuvel 
392aa8c09beSArd Biesheuvel 	/* flush the TLBs after updating live kernel mappings */
393aa8c09beSArd Biesheuvel 	flush_tlb_kernel_range(virt, virt + size);
394da141706SLaura Abbott }
395da141706SLaura Abbott 
396068a17a5SMark Rutland static void __init __map_memblock(pgd_t *pgd, phys_addr_t start, phys_addr_t end)
397da141706SLaura Abbott {
398eac8017fSMiles Chen 	phys_addr_t kernel_start = __pa_symbol(_text);
399eac8017fSMiles Chen 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
400c0951366SArd Biesheuvel 	int flags = 0;
401c0951366SArd Biesheuvel 
402c0951366SArd Biesheuvel 	if (debug_pagealloc_enabled())
403*d27cfa1fSArd Biesheuvel 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
404068a17a5SMark Rutland 
405da141706SLaura Abbott 	/*
406f9040773SArd Biesheuvel 	 * Take care not to create a writable alias for the
407f9040773SArd Biesheuvel 	 * read-only text and rodata sections of the kernel image.
408da141706SLaura Abbott 	 */
409da141706SLaura Abbott 
4109fdc14c5SArd Biesheuvel 	/* No overlap with the kernel text/rodata */
411068a17a5SMark Rutland 	if (end < kernel_start || start >= kernel_end) {
412068a17a5SMark Rutland 		__create_pgd_mapping(pgd, start, __phys_to_virt(start),
413068a17a5SMark Rutland 				     end - start, PAGE_KERNEL,
414c0951366SArd Biesheuvel 				     early_pgtable_alloc, flags);
415068a17a5SMark Rutland 		return;
416da141706SLaura Abbott 	}
417da141706SLaura Abbott 
418068a17a5SMark Rutland 	/*
4199fdc14c5SArd Biesheuvel 	 * This block overlaps the kernel text/rodata mappings.
420f9040773SArd Biesheuvel 	 * Map the portion(s) which don't overlap.
421068a17a5SMark Rutland 	 */
422068a17a5SMark Rutland 	if (start < kernel_start)
423068a17a5SMark Rutland 		__create_pgd_mapping(pgd, start,
424068a17a5SMark Rutland 				     __phys_to_virt(start),
425068a17a5SMark Rutland 				     kernel_start - start, PAGE_KERNEL,
426c0951366SArd Biesheuvel 				     early_pgtable_alloc, flags);
427068a17a5SMark Rutland 	if (kernel_end < end)
428068a17a5SMark Rutland 		__create_pgd_mapping(pgd, kernel_end,
429068a17a5SMark Rutland 				     __phys_to_virt(kernel_end),
430068a17a5SMark Rutland 				     end - kernel_end, PAGE_KERNEL,
431c0951366SArd Biesheuvel 				     early_pgtable_alloc, flags);
432f9040773SArd Biesheuvel 
433f9040773SArd Biesheuvel 	/*
4345ea5306cSArd Biesheuvel 	 * Map the linear alias of the [_text, __init_begin) interval
4355ea5306cSArd Biesheuvel 	 * as non-executable now, and remove the write permission in
4365ea5306cSArd Biesheuvel 	 * mark_linear_text_alias_ro() below (which will be called after
4375ea5306cSArd Biesheuvel 	 * alternative patching has completed). This makes the contents
4385ea5306cSArd Biesheuvel 	 * of the region accessible to subsystems such as hibernate,
4395ea5306cSArd Biesheuvel 	 * but protects it from inadvertent modification or execution.
440*d27cfa1fSArd Biesheuvel 	 * Note that contiguous mappings cannot be remapped in this way,
441*d27cfa1fSArd Biesheuvel 	 * so we should avoid them here.
442f9040773SArd Biesheuvel 	 */
443f9040773SArd Biesheuvel 	__create_pgd_mapping(pgd, kernel_start, __phys_to_virt(kernel_start),
4445ea5306cSArd Biesheuvel 			     kernel_end - kernel_start, PAGE_KERNEL,
445*d27cfa1fSArd Biesheuvel 			     early_pgtable_alloc, NO_CONT_MAPPINGS);
446da141706SLaura Abbott }
447da141706SLaura Abbott 
4485ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void)
4495ea5306cSArd Biesheuvel {
4505ea5306cSArd Biesheuvel 	/*
4515ea5306cSArd Biesheuvel 	 * Remove the write permissions from the linear alias of .text/.rodata
4525ea5306cSArd Biesheuvel 	 */
4535ea5306cSArd Biesheuvel 	update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
4545ea5306cSArd Biesheuvel 			    (unsigned long)__init_begin - (unsigned long)_text,
4555ea5306cSArd Biesheuvel 			    PAGE_KERNEL_RO);
4565ea5306cSArd Biesheuvel }
4575ea5306cSArd Biesheuvel 
458068a17a5SMark Rutland static void __init map_mem(pgd_t *pgd)
459c1cc1552SCatalin Marinas {
460c1cc1552SCatalin Marinas 	struct memblock_region *reg;
461f6bc87c3SSteve Capper 
462c1cc1552SCatalin Marinas 	/* map all the memory banks */
463c1cc1552SCatalin Marinas 	for_each_memblock(memory, reg) {
464c1cc1552SCatalin Marinas 		phys_addr_t start = reg->base;
465c1cc1552SCatalin Marinas 		phys_addr_t end = start + reg->size;
466c1cc1552SCatalin Marinas 
467c1cc1552SCatalin Marinas 		if (start >= end)
468c1cc1552SCatalin Marinas 			break;
46968709f45SArd Biesheuvel 		if (memblock_is_nomap(reg))
47068709f45SArd Biesheuvel 			continue;
471c1cc1552SCatalin Marinas 
472068a17a5SMark Rutland 		__map_memblock(pgd, start, end);
473c1cc1552SCatalin Marinas 	}
474c1cc1552SCatalin Marinas }
475c1cc1552SCatalin Marinas 
476da141706SLaura Abbott void mark_rodata_ro(void)
477da141706SLaura Abbott {
4782f39b5f9SJeremy Linton 	unsigned long section_size;
479f9040773SArd Biesheuvel 
4802f39b5f9SJeremy Linton 	/*
4819fdc14c5SArd Biesheuvel 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
4829fdc14c5SArd Biesheuvel 	 * to cover NOTES and EXCEPTION_TABLE.
4832f39b5f9SJeremy Linton 	 */
4849fdc14c5SArd Biesheuvel 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
485aa8c09beSArd Biesheuvel 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
4862f39b5f9SJeremy Linton 			    section_size, PAGE_KERNEL_RO);
487e98216b5SArd Biesheuvel 
4881404d6f1SLaura Abbott 	debug_checkwx();
489da141706SLaura Abbott }
490da141706SLaura Abbott 
4912c09ec06SArd Biesheuvel static void __init map_kernel_segment(pgd_t *pgd, void *va_start, void *va_end,
492*d27cfa1fSArd Biesheuvel 				      pgprot_t prot, struct vm_struct *vma,
493*d27cfa1fSArd Biesheuvel 				      int flags)
494068a17a5SMark Rutland {
4952077be67SLaura Abbott 	phys_addr_t pa_start = __pa_symbol(va_start);
496068a17a5SMark Rutland 	unsigned long size = va_end - va_start;
497068a17a5SMark Rutland 
498068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(pa_start));
499068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(size));
500068a17a5SMark Rutland 
501068a17a5SMark Rutland 	__create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot,
502*d27cfa1fSArd Biesheuvel 			     early_pgtable_alloc, flags);
503f9040773SArd Biesheuvel 
504f9040773SArd Biesheuvel 	vma->addr	= va_start;
505f9040773SArd Biesheuvel 	vma->phys_addr	= pa_start;
506f9040773SArd Biesheuvel 	vma->size	= size;
507f9040773SArd Biesheuvel 	vma->flags	= VM_MAP;
508f9040773SArd Biesheuvel 	vma->caller	= __builtin_return_address(0);
509f9040773SArd Biesheuvel 
510f9040773SArd Biesheuvel 	vm_area_add_early(vma);
511068a17a5SMark Rutland }
512068a17a5SMark Rutland 
51328b066daSArd Biesheuvel static int __init parse_rodata(char *arg)
51428b066daSArd Biesheuvel {
51528b066daSArd Biesheuvel 	return strtobool(arg, &rodata_enabled);
51628b066daSArd Biesheuvel }
51728b066daSArd Biesheuvel early_param("rodata", parse_rodata);
51828b066daSArd Biesheuvel 
519068a17a5SMark Rutland /*
520068a17a5SMark Rutland  * Create fine-grained mappings for the kernel.
521068a17a5SMark Rutland  */
522068a17a5SMark Rutland static void __init map_kernel(pgd_t *pgd)
523068a17a5SMark Rutland {
5242ebe088bSArd Biesheuvel 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
5252ebe088bSArd Biesheuvel 				vmlinux_initdata, vmlinux_data;
526068a17a5SMark Rutland 
52728b066daSArd Biesheuvel 	/*
52828b066daSArd Biesheuvel 	 * External debuggers may need to write directly to the text
52928b066daSArd Biesheuvel 	 * mapping to install SW breakpoints. Allow this (only) when
53028b066daSArd Biesheuvel 	 * explicitly requested with rodata=off.
53128b066daSArd Biesheuvel 	 */
53228b066daSArd Biesheuvel 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
53328b066daSArd Biesheuvel 
534*d27cfa1fSArd Biesheuvel 	/*
535*d27cfa1fSArd Biesheuvel 	 * Only rodata will be remapped with different permissions later on,
536*d27cfa1fSArd Biesheuvel 	 * all other segments are allowed to use contiguous mappings.
537*d27cfa1fSArd Biesheuvel 	 */
538*d27cfa1fSArd Biesheuvel 	map_kernel_segment(pgd, _text, _etext, text_prot, &vmlinux_text, 0);
5392ebe088bSArd Biesheuvel 	map_kernel_segment(pgd, __start_rodata, __inittext_begin, PAGE_KERNEL,
540*d27cfa1fSArd Biesheuvel 			   &vmlinux_rodata, NO_CONT_MAPPINGS);
5412ebe088bSArd Biesheuvel 	map_kernel_segment(pgd, __inittext_begin, __inittext_end, text_prot,
542*d27cfa1fSArd Biesheuvel 			   &vmlinux_inittext, 0);
5432ebe088bSArd Biesheuvel 	map_kernel_segment(pgd, __initdata_begin, __initdata_end, PAGE_KERNEL,
544*d27cfa1fSArd Biesheuvel 			   &vmlinux_initdata, 0);
545*d27cfa1fSArd Biesheuvel 	map_kernel_segment(pgd, _data, _end, PAGE_KERNEL, &vmlinux_data, 0);
546068a17a5SMark Rutland 
547f9040773SArd Biesheuvel 	if (!pgd_val(*pgd_offset_raw(pgd, FIXADDR_START))) {
548068a17a5SMark Rutland 		/*
549f9040773SArd Biesheuvel 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
550f9040773SArd Biesheuvel 		 * live in the carveout for the swapper_pg_dir. We can simply
551f9040773SArd Biesheuvel 		 * re-use the existing dir for the fixmap.
552068a17a5SMark Rutland 		 */
553f9040773SArd Biesheuvel 		set_pgd(pgd_offset_raw(pgd, FIXADDR_START),
554f9040773SArd Biesheuvel 			*pgd_offset_k(FIXADDR_START));
555f9040773SArd Biesheuvel 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
556f9040773SArd Biesheuvel 		/*
557f9040773SArd Biesheuvel 		 * The fixmap shares its top level pgd entry with the kernel
558f9040773SArd Biesheuvel 		 * mapping. This can really only occur when we are running
559f9040773SArd Biesheuvel 		 * with 16k/4 levels, so we can simply reuse the pud level
560f9040773SArd Biesheuvel 		 * entry instead.
561f9040773SArd Biesheuvel 		 */
562f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
563f9040773SArd Biesheuvel 		set_pud(pud_set_fixmap_offset(pgd, FIXADDR_START),
5642077be67SLaura Abbott 			__pud(__pa_symbol(bm_pmd) | PUD_TYPE_TABLE));
565f9040773SArd Biesheuvel 		pud_clear_fixmap();
566f9040773SArd Biesheuvel 	} else {
567f9040773SArd Biesheuvel 		BUG();
568f9040773SArd Biesheuvel 	}
569068a17a5SMark Rutland 
570068a17a5SMark Rutland 	kasan_copy_shadow(pgd);
571068a17a5SMark Rutland }
572068a17a5SMark Rutland 
573c1cc1552SCatalin Marinas /*
574c1cc1552SCatalin Marinas  * paging_init() sets up the page tables, initialises the zone memory
575c1cc1552SCatalin Marinas  * maps and sets up the zero page.
576c1cc1552SCatalin Marinas  */
577c1cc1552SCatalin Marinas void __init paging_init(void)
578c1cc1552SCatalin Marinas {
579068a17a5SMark Rutland 	phys_addr_t pgd_phys = early_pgtable_alloc();
580068a17a5SMark Rutland 	pgd_t *pgd = pgd_set_fixmap(pgd_phys);
581068a17a5SMark Rutland 
582068a17a5SMark Rutland 	map_kernel(pgd);
583068a17a5SMark Rutland 	map_mem(pgd);
584068a17a5SMark Rutland 
585068a17a5SMark Rutland 	/*
586068a17a5SMark Rutland 	 * We want to reuse the original swapper_pg_dir so we don't have to
587068a17a5SMark Rutland 	 * communicate the new address to non-coherent secondaries in
588068a17a5SMark Rutland 	 * secondary_entry, and so cpu_switch_mm can generate the address with
589068a17a5SMark Rutland 	 * adrp+add rather than a load from some global variable.
590068a17a5SMark Rutland 	 *
591068a17a5SMark Rutland 	 * To do this we need to go via a temporary pgd.
592068a17a5SMark Rutland 	 */
593068a17a5SMark Rutland 	cpu_replace_ttbr1(__va(pgd_phys));
59412f043ffSArnd Bergmann 	memcpy(swapper_pg_dir, pgd, PGD_SIZE);
5952077be67SLaura Abbott 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
596068a17a5SMark Rutland 
597068a17a5SMark Rutland 	pgd_clear_fixmap();
598068a17a5SMark Rutland 	memblock_free(pgd_phys, PAGE_SIZE);
599068a17a5SMark Rutland 
600068a17a5SMark Rutland 	/*
601068a17a5SMark Rutland 	 * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd
602068a17a5SMark Rutland 	 * allocated with it.
603068a17a5SMark Rutland 	 */
6042077be67SLaura Abbott 	memblock_free(__pa_symbol(swapper_pg_dir) + PAGE_SIZE,
605068a17a5SMark Rutland 		      SWAPPER_DIR_SIZE - PAGE_SIZE);
606c1cc1552SCatalin Marinas }
607c1cc1552SCatalin Marinas 
608c1cc1552SCatalin Marinas /*
609c1cc1552SCatalin Marinas  * Check whether a kernel address is valid (derived from arch/x86/).
610c1cc1552SCatalin Marinas  */
611c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr)
612c1cc1552SCatalin Marinas {
613c1cc1552SCatalin Marinas 	pgd_t *pgd;
614c1cc1552SCatalin Marinas 	pud_t *pud;
615c1cc1552SCatalin Marinas 	pmd_t *pmd;
616c1cc1552SCatalin Marinas 	pte_t *pte;
617c1cc1552SCatalin Marinas 
618c1cc1552SCatalin Marinas 	if ((((long)addr) >> VA_BITS) != -1UL)
619c1cc1552SCatalin Marinas 		return 0;
620c1cc1552SCatalin Marinas 
621c1cc1552SCatalin Marinas 	pgd = pgd_offset_k(addr);
622c1cc1552SCatalin Marinas 	if (pgd_none(*pgd))
623c1cc1552SCatalin Marinas 		return 0;
624c1cc1552SCatalin Marinas 
625c1cc1552SCatalin Marinas 	pud = pud_offset(pgd, addr);
626c1cc1552SCatalin Marinas 	if (pud_none(*pud))
627c1cc1552SCatalin Marinas 		return 0;
628c1cc1552SCatalin Marinas 
629206a2a73SSteve Capper 	if (pud_sect(*pud))
630206a2a73SSteve Capper 		return pfn_valid(pud_pfn(*pud));
631206a2a73SSteve Capper 
632c1cc1552SCatalin Marinas 	pmd = pmd_offset(pud, addr);
633c1cc1552SCatalin Marinas 	if (pmd_none(*pmd))
634c1cc1552SCatalin Marinas 		return 0;
635c1cc1552SCatalin Marinas 
636da6e4cb6SDave Anderson 	if (pmd_sect(*pmd))
637da6e4cb6SDave Anderson 		return pfn_valid(pmd_pfn(*pmd));
638da6e4cb6SDave Anderson 
639c1cc1552SCatalin Marinas 	pte = pte_offset_kernel(pmd, addr);
640c1cc1552SCatalin Marinas 	if (pte_none(*pte))
641c1cc1552SCatalin Marinas 		return 0;
642c1cc1552SCatalin Marinas 
643c1cc1552SCatalin Marinas 	return pfn_valid(pte_pfn(*pte));
644c1cc1552SCatalin Marinas }
645c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP
646b433dce0SSuzuki K. Poulose #if !ARM64_SWAPPER_USES_SECTION_MAPS
6470aad818bSJohannes Weiner int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
648c1cc1552SCatalin Marinas {
6490aad818bSJohannes Weiner 	return vmemmap_populate_basepages(start, end, node);
650c1cc1552SCatalin Marinas }
651b433dce0SSuzuki K. Poulose #else	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
6520aad818bSJohannes Weiner int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
653c1cc1552SCatalin Marinas {
6540aad818bSJohannes Weiner 	unsigned long addr = start;
655c1cc1552SCatalin Marinas 	unsigned long next;
656c1cc1552SCatalin Marinas 	pgd_t *pgd;
657c1cc1552SCatalin Marinas 	pud_t *pud;
658c1cc1552SCatalin Marinas 	pmd_t *pmd;
659c1cc1552SCatalin Marinas 
660c1cc1552SCatalin Marinas 	do {
661c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
662c1cc1552SCatalin Marinas 
663c1cc1552SCatalin Marinas 		pgd = vmemmap_pgd_populate(addr, node);
664c1cc1552SCatalin Marinas 		if (!pgd)
665c1cc1552SCatalin Marinas 			return -ENOMEM;
666c1cc1552SCatalin Marinas 
667c1cc1552SCatalin Marinas 		pud = vmemmap_pud_populate(pgd, addr, node);
668c1cc1552SCatalin Marinas 		if (!pud)
669c1cc1552SCatalin Marinas 			return -ENOMEM;
670c1cc1552SCatalin Marinas 
671c1cc1552SCatalin Marinas 		pmd = pmd_offset(pud, addr);
672c1cc1552SCatalin Marinas 		if (pmd_none(*pmd)) {
673c1cc1552SCatalin Marinas 			void *p = NULL;
674c1cc1552SCatalin Marinas 
675c1cc1552SCatalin Marinas 			p = vmemmap_alloc_block_buf(PMD_SIZE, node);
676c1cc1552SCatalin Marinas 			if (!p)
677c1cc1552SCatalin Marinas 				return -ENOMEM;
678c1cc1552SCatalin Marinas 
679a501e324SCatalin Marinas 			set_pmd(pmd, __pmd(__pa(p) | PROT_SECT_NORMAL));
680c1cc1552SCatalin Marinas 		} else
681c1cc1552SCatalin Marinas 			vmemmap_verify((pte_t *)pmd, node, addr, next);
682c1cc1552SCatalin Marinas 	} while (addr = next, addr != end);
683c1cc1552SCatalin Marinas 
684c1cc1552SCatalin Marinas 	return 0;
685c1cc1552SCatalin Marinas }
686c1cc1552SCatalin Marinas #endif	/* CONFIG_ARM64_64K_PAGES */
6870aad818bSJohannes Weiner void vmemmap_free(unsigned long start, unsigned long end)
6880197518cSTang Chen {
6890197518cSTang Chen }
690c1cc1552SCatalin Marinas #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
691af86e597SLaura Abbott 
692af86e597SLaura Abbott static inline pud_t * fixmap_pud(unsigned long addr)
693af86e597SLaura Abbott {
694af86e597SLaura Abbott 	pgd_t *pgd = pgd_offset_k(addr);
695af86e597SLaura Abbott 
696af86e597SLaura Abbott 	BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
697af86e597SLaura Abbott 
698157962f5SArd Biesheuvel 	return pud_offset_kimg(pgd, addr);
699af86e597SLaura Abbott }
700af86e597SLaura Abbott 
701af86e597SLaura Abbott static inline pmd_t * fixmap_pmd(unsigned long addr)
702af86e597SLaura Abbott {
703af86e597SLaura Abbott 	pud_t *pud = fixmap_pud(addr);
704af86e597SLaura Abbott 
705af86e597SLaura Abbott 	BUG_ON(pud_none(*pud) || pud_bad(*pud));
706af86e597SLaura Abbott 
707157962f5SArd Biesheuvel 	return pmd_offset_kimg(pud, addr);
708af86e597SLaura Abbott }
709af86e597SLaura Abbott 
710af86e597SLaura Abbott static inline pte_t * fixmap_pte(unsigned long addr)
711af86e597SLaura Abbott {
712157962f5SArd Biesheuvel 	return &bm_pte[pte_index(addr)];
713af86e597SLaura Abbott }
714af86e597SLaura Abbott 
7152077be67SLaura Abbott /*
7162077be67SLaura Abbott  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
7172077be67SLaura Abbott  * directly on kernel symbols (bm_p*d). This function is called too early to use
7182077be67SLaura Abbott  * lm_alias so __p*d_populate functions must be used to populate with the
7192077be67SLaura Abbott  * physical address from __pa_symbol.
7202077be67SLaura Abbott  */
721af86e597SLaura Abbott void __init early_fixmap_init(void)
722af86e597SLaura Abbott {
723af86e597SLaura Abbott 	pgd_t *pgd;
724af86e597SLaura Abbott 	pud_t *pud;
725af86e597SLaura Abbott 	pmd_t *pmd;
726af86e597SLaura Abbott 	unsigned long addr = FIXADDR_START;
727af86e597SLaura Abbott 
728af86e597SLaura Abbott 	pgd = pgd_offset_k(addr);
729f80fb3a3SArd Biesheuvel 	if (CONFIG_PGTABLE_LEVELS > 3 &&
7302077be67SLaura Abbott 	    !(pgd_none(*pgd) || pgd_page_paddr(*pgd) == __pa_symbol(bm_pud))) {
731f9040773SArd Biesheuvel 		/*
732f9040773SArd Biesheuvel 		 * We only end up here if the kernel mapping and the fixmap
733f9040773SArd Biesheuvel 		 * share the top level pgd entry, which should only happen on
734f9040773SArd Biesheuvel 		 * 16k/4 levels configurations.
735f9040773SArd Biesheuvel 		 */
736f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
737f9040773SArd Biesheuvel 		pud = pud_offset_kimg(pgd, addr);
738f9040773SArd Biesheuvel 	} else {
7392077be67SLaura Abbott 		if (pgd_none(*pgd))
7402077be67SLaura Abbott 			__pgd_populate(pgd, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
741157962f5SArd Biesheuvel 		pud = fixmap_pud(addr);
742f9040773SArd Biesheuvel 	}
7432077be67SLaura Abbott 	if (pud_none(*pud))
7442077be67SLaura Abbott 		__pud_populate(pud, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
745157962f5SArd Biesheuvel 	pmd = fixmap_pmd(addr);
7462077be67SLaura Abbott 	__pmd_populate(pmd, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
747af86e597SLaura Abbott 
748af86e597SLaura Abbott 	/*
749af86e597SLaura Abbott 	 * The boot-ioremap range spans multiple pmds, for which
750157962f5SArd Biesheuvel 	 * we are not prepared:
751af86e597SLaura Abbott 	 */
752af86e597SLaura Abbott 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
753af86e597SLaura Abbott 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
754af86e597SLaura Abbott 
755af86e597SLaura Abbott 	if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
756af86e597SLaura Abbott 	     || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
757af86e597SLaura Abbott 		WARN_ON(1);
758af86e597SLaura Abbott 		pr_warn("pmd %p != %p, %p\n",
759af86e597SLaura Abbott 			pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
760af86e597SLaura Abbott 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
761af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
762af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_BEGIN));
763af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
764af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_END));
765af86e597SLaura Abbott 
766af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
767af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
768af86e597SLaura Abbott 	}
769af86e597SLaura Abbott }
770af86e597SLaura Abbott 
771af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx,
772af86e597SLaura Abbott 			       phys_addr_t phys, pgprot_t flags)
773af86e597SLaura Abbott {
774af86e597SLaura Abbott 	unsigned long addr = __fix_to_virt(idx);
775af86e597SLaura Abbott 	pte_t *pte;
776af86e597SLaura Abbott 
777b63dbef9SMark Rutland 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
778af86e597SLaura Abbott 
779af86e597SLaura Abbott 	pte = fixmap_pte(addr);
780af86e597SLaura Abbott 
781af86e597SLaura Abbott 	if (pgprot_val(flags)) {
782af86e597SLaura Abbott 		set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
783af86e597SLaura Abbott 	} else {
784af86e597SLaura Abbott 		pte_clear(&init_mm, addr, pte);
785af86e597SLaura Abbott 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
786af86e597SLaura Abbott 	}
787af86e597SLaura Abbott }
78861bd93ceSArd Biesheuvel 
789f80fb3a3SArd Biesheuvel void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
79061bd93ceSArd Biesheuvel {
79161bd93ceSArd Biesheuvel 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
792f80fb3a3SArd Biesheuvel 	int offset;
79361bd93ceSArd Biesheuvel 	void *dt_virt;
79461bd93ceSArd Biesheuvel 
79561bd93ceSArd Biesheuvel 	/*
79661bd93ceSArd Biesheuvel 	 * Check whether the physical FDT address is set and meets the minimum
79761bd93ceSArd Biesheuvel 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
79804a84810SArd Biesheuvel 	 * at least 8 bytes so that we can always access the magic and size
79904a84810SArd Biesheuvel 	 * fields of the FDT header after mapping the first chunk, double check
80004a84810SArd Biesheuvel 	 * here if that is indeed the case.
80161bd93ceSArd Biesheuvel 	 */
80261bd93ceSArd Biesheuvel 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
80361bd93ceSArd Biesheuvel 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
80461bd93ceSArd Biesheuvel 		return NULL;
80561bd93ceSArd Biesheuvel 
80661bd93ceSArd Biesheuvel 	/*
80761bd93ceSArd Biesheuvel 	 * Make sure that the FDT region can be mapped without the need to
80861bd93ceSArd Biesheuvel 	 * allocate additional translation table pages, so that it is safe
809132233a7SLaura Abbott 	 * to call create_mapping_noalloc() this early.
81061bd93ceSArd Biesheuvel 	 *
81161bd93ceSArd Biesheuvel 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
81261bd93ceSArd Biesheuvel 	 * be in the same PMD as the rest of the fixmap.
81361bd93ceSArd Biesheuvel 	 * On 4k pages, we'll use section mappings for the FDT so we only
81461bd93ceSArd Biesheuvel 	 * have to be in the same PUD.
81561bd93ceSArd Biesheuvel 	 */
81661bd93ceSArd Biesheuvel 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
81761bd93ceSArd Biesheuvel 
818b433dce0SSuzuki K. Poulose 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
819b433dce0SSuzuki K. Poulose 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
82061bd93ceSArd Biesheuvel 
821b433dce0SSuzuki K. Poulose 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
82261bd93ceSArd Biesheuvel 	dt_virt = (void *)dt_virt_base + offset;
82361bd93ceSArd Biesheuvel 
82461bd93ceSArd Biesheuvel 	/* map the first chunk so we can read the size from the header */
825132233a7SLaura Abbott 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
826132233a7SLaura Abbott 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
82761bd93ceSArd Biesheuvel 
82804a84810SArd Biesheuvel 	if (fdt_magic(dt_virt) != FDT_MAGIC)
82961bd93ceSArd Biesheuvel 		return NULL;
83061bd93ceSArd Biesheuvel 
831f80fb3a3SArd Biesheuvel 	*size = fdt_totalsize(dt_virt);
832f80fb3a3SArd Biesheuvel 	if (*size > MAX_FDT_SIZE)
83361bd93ceSArd Biesheuvel 		return NULL;
83461bd93ceSArd Biesheuvel 
835f80fb3a3SArd Biesheuvel 	if (offset + *size > SWAPPER_BLOCK_SIZE)
836132233a7SLaura Abbott 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
837f80fb3a3SArd Biesheuvel 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
838f80fb3a3SArd Biesheuvel 
839f80fb3a3SArd Biesheuvel 	return dt_virt;
840f80fb3a3SArd Biesheuvel }
841f80fb3a3SArd Biesheuvel 
842f80fb3a3SArd Biesheuvel void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
843f80fb3a3SArd Biesheuvel {
844f80fb3a3SArd Biesheuvel 	void *dt_virt;
845f80fb3a3SArd Biesheuvel 	int size;
846f80fb3a3SArd Biesheuvel 
847f80fb3a3SArd Biesheuvel 	dt_virt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
848f80fb3a3SArd Biesheuvel 	if (!dt_virt)
849f80fb3a3SArd Biesheuvel 		return NULL;
85061bd93ceSArd Biesheuvel 
85161bd93ceSArd Biesheuvel 	memblock_reserve(dt_phys, size);
85261bd93ceSArd Biesheuvel 	return dt_virt;
85361bd93ceSArd Biesheuvel }
854324420bfSArd Biesheuvel 
855324420bfSArd Biesheuvel int __init arch_ioremap_pud_supported(void)
856324420bfSArd Biesheuvel {
857324420bfSArd Biesheuvel 	/* only 4k granule supports level 1 block mappings */
858324420bfSArd Biesheuvel 	return IS_ENABLED(CONFIG_ARM64_4K_PAGES);
859324420bfSArd Biesheuvel }
860324420bfSArd Biesheuvel 
861324420bfSArd Biesheuvel int __init arch_ioremap_pmd_supported(void)
862324420bfSArd Biesheuvel {
863324420bfSArd Biesheuvel 	return 1;
864324420bfSArd Biesheuvel }
865324420bfSArd Biesheuvel 
866324420bfSArd Biesheuvel int pud_set_huge(pud_t *pud, phys_addr_t phys, pgprot_t prot)
867324420bfSArd Biesheuvel {
868324420bfSArd Biesheuvel 	BUG_ON(phys & ~PUD_MASK);
869324420bfSArd Biesheuvel 	set_pud(pud, __pud(phys | PUD_TYPE_SECT | pgprot_val(mk_sect_prot(prot))));
870324420bfSArd Biesheuvel 	return 1;
871324420bfSArd Biesheuvel }
872324420bfSArd Biesheuvel 
873324420bfSArd Biesheuvel int pmd_set_huge(pmd_t *pmd, phys_addr_t phys, pgprot_t prot)
874324420bfSArd Biesheuvel {
875324420bfSArd Biesheuvel 	BUG_ON(phys & ~PMD_MASK);
876324420bfSArd Biesheuvel 	set_pmd(pmd, __pmd(phys | PMD_TYPE_SECT | pgprot_val(mk_sect_prot(prot))));
877324420bfSArd Biesheuvel 	return 1;
878324420bfSArd Biesheuvel }
879324420bfSArd Biesheuvel 
880324420bfSArd Biesheuvel int pud_clear_huge(pud_t *pud)
881324420bfSArd Biesheuvel {
882324420bfSArd Biesheuvel 	if (!pud_sect(*pud))
883324420bfSArd Biesheuvel 		return 0;
884324420bfSArd Biesheuvel 	pud_clear(pud);
885324420bfSArd Biesheuvel 	return 1;
886324420bfSArd Biesheuvel }
887324420bfSArd Biesheuvel 
888324420bfSArd Biesheuvel int pmd_clear_huge(pmd_t *pmd)
889324420bfSArd Biesheuvel {
890324420bfSArd Biesheuvel 	if (!pmd_sect(*pmd))
891324420bfSArd Biesheuvel 		return 0;
892324420bfSArd Biesheuvel 	pmd_clear(pmd);
893324420bfSArd Biesheuvel 	return 1;
894324420bfSArd Biesheuvel }
895