xref: /openbmc/linux/arch/arm64/mm/mmu.c (revision fa2a8445)
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>
48c1cc1552SCatalin Marinas 
49c0951366SArd Biesheuvel #define NO_BLOCK_MAPPINGS	BIT(0)
50d27cfa1fSArd Biesheuvel #define NO_CONT_MAPPINGS	BIT(1)
51c0951366SArd Biesheuvel 
52dd006da2SArd Biesheuvel u64 idmap_t0sz = TCR_T0SZ(VA_BITS);
53fa2a8445SKristina Martsenko u64 idmap_ptrs_per_pgd = PTRS_PER_PGD;
54dd006da2SArd Biesheuvel 
555a9e3e15SJisheng Zhang u64 kimage_voffset __ro_after_init;
56a7f8de16SArd Biesheuvel EXPORT_SYMBOL(kimage_voffset);
57a7f8de16SArd Biesheuvel 
58c1cc1552SCatalin Marinas /*
59c1cc1552SCatalin Marinas  * Empty_zero_page is a special page that is used for zero-initialized data
60c1cc1552SCatalin Marinas  * and COW.
61c1cc1552SCatalin Marinas  */
625227cfa7SMark Rutland unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)] __page_aligned_bss;
63c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page);
64c1cc1552SCatalin Marinas 
65f9040773SArd Biesheuvel static pte_t bm_pte[PTRS_PER_PTE] __page_aligned_bss;
66f9040773SArd Biesheuvel static pmd_t bm_pmd[PTRS_PER_PMD] __page_aligned_bss __maybe_unused;
67f9040773SArd Biesheuvel static pud_t bm_pud[PTRS_PER_PUD] __page_aligned_bss __maybe_unused;
68f9040773SArd Biesheuvel 
69c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
70c1cc1552SCatalin Marinas 			      unsigned long size, pgprot_t vma_prot)
71c1cc1552SCatalin Marinas {
72c1cc1552SCatalin Marinas 	if (!pfn_valid(pfn))
73c1cc1552SCatalin Marinas 		return pgprot_noncached(vma_prot);
74c1cc1552SCatalin Marinas 	else if (file->f_flags & O_SYNC)
75c1cc1552SCatalin Marinas 		return pgprot_writecombine(vma_prot);
76c1cc1552SCatalin Marinas 	return vma_prot;
77c1cc1552SCatalin Marinas }
78c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot);
79c1cc1552SCatalin Marinas 
80f4710445SMark Rutland static phys_addr_t __init early_pgtable_alloc(void)
81c1cc1552SCatalin Marinas {
827142392dSSuzuki K. Poulose 	phys_addr_t phys;
837142392dSSuzuki K. Poulose 	void *ptr;
847142392dSSuzuki K. Poulose 
8521ab99c2SMark Rutland 	phys = memblock_alloc(PAGE_SIZE, PAGE_SIZE);
86f4710445SMark Rutland 
87f4710445SMark Rutland 	/*
88f4710445SMark Rutland 	 * The FIX_{PGD,PUD,PMD} slots may be in active use, but the FIX_PTE
89f4710445SMark Rutland 	 * slot will be free, so we can (ab)use the FIX_PTE slot to initialise
90f4710445SMark Rutland 	 * any level of table.
91f4710445SMark Rutland 	 */
92f4710445SMark Rutland 	ptr = pte_set_fixmap(phys);
93f4710445SMark Rutland 
9421ab99c2SMark Rutland 	memset(ptr, 0, PAGE_SIZE);
9521ab99c2SMark Rutland 
96f4710445SMark Rutland 	/*
97f4710445SMark Rutland 	 * Implicit barriers also ensure the zeroed page is visible to the page
98f4710445SMark Rutland 	 * table walker
99f4710445SMark Rutland 	 */
100f4710445SMark Rutland 	pte_clear_fixmap();
101f4710445SMark Rutland 
102f4710445SMark Rutland 	return phys;
103c1cc1552SCatalin Marinas }
104c1cc1552SCatalin Marinas 
105e98216b5SArd Biesheuvel static bool pgattr_change_is_safe(u64 old, u64 new)
106e98216b5SArd Biesheuvel {
107e98216b5SArd Biesheuvel 	/*
108e98216b5SArd Biesheuvel 	 * The following mapping attributes may be updated in live
109e98216b5SArd Biesheuvel 	 * kernel mappings without the need for break-before-make.
110e98216b5SArd Biesheuvel 	 */
111e98216b5SArd Biesheuvel 	static const pteval_t mask = PTE_PXN | PTE_RDONLY | PTE_WRITE;
112e98216b5SArd Biesheuvel 
113141d1497SArd Biesheuvel 	/* creating or taking down mappings is always safe */
114141d1497SArd Biesheuvel 	if (old == 0 || new == 0)
115141d1497SArd Biesheuvel 		return true;
116141d1497SArd Biesheuvel 
117141d1497SArd Biesheuvel 	/* live contiguous mappings may not be manipulated at all */
118141d1497SArd Biesheuvel 	if ((old | new) & PTE_CONT)
119141d1497SArd Biesheuvel 		return false;
120141d1497SArd Biesheuvel 
121141d1497SArd Biesheuvel 	return ((old ^ new) & ~mask) == 0;
122e98216b5SArd Biesheuvel }
123e98216b5SArd Biesheuvel 
124d27cfa1fSArd Biesheuvel static void init_pte(pmd_t *pmd, unsigned long addr, unsigned long end,
125d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot)
126c1cc1552SCatalin Marinas {
127c1cc1552SCatalin Marinas 	pte_t *pte;
128c1cc1552SCatalin Marinas 
129f4710445SMark Rutland 	pte = pte_set_fixmap_offset(pmd, addr);
130c1cc1552SCatalin Marinas 	do {
131e98216b5SArd Biesheuvel 		pte_t old_pte = *pte;
132e98216b5SArd Biesheuvel 
133e393cf40SArd Biesheuvel 		set_pte(pte, pfn_pte(__phys_to_pfn(phys), prot));
134e98216b5SArd Biesheuvel 
135e98216b5SArd Biesheuvel 		/*
136e98216b5SArd Biesheuvel 		 * After the PTE entry has been populated once, we
137e98216b5SArd Biesheuvel 		 * only allow updates to the permission attributes.
138e98216b5SArd Biesheuvel 		 */
139e98216b5SArd Biesheuvel 		BUG_ON(!pgattr_change_is_safe(pte_val(old_pte), pte_val(*pte)));
140e98216b5SArd Biesheuvel 
141e393cf40SArd Biesheuvel 		phys += PAGE_SIZE;
142667c2759SCatalin Marinas 	} while (pte++, addr += PAGE_SIZE, addr != end);
143f4710445SMark Rutland 
144f4710445SMark Rutland 	pte_clear_fixmap();
145c1cc1552SCatalin Marinas }
146c1cc1552SCatalin Marinas 
147d27cfa1fSArd Biesheuvel static void alloc_init_cont_pte(pmd_t *pmd, unsigned long addr,
148d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
149d27cfa1fSArd Biesheuvel 				pgprot_t prot,
15053e1b329SArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void),
151c0951366SArd Biesheuvel 				int flags)
152c1cc1552SCatalin Marinas {
153c1cc1552SCatalin Marinas 	unsigned long next;
154c1cc1552SCatalin Marinas 
155d27cfa1fSArd Biesheuvel 	BUG_ON(pmd_sect(*pmd));
156d27cfa1fSArd Biesheuvel 	if (pmd_none(*pmd)) {
157d27cfa1fSArd Biesheuvel 		phys_addr_t pte_phys;
158132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
159d27cfa1fSArd Biesheuvel 		pte_phys = pgtable_alloc();
160d27cfa1fSArd Biesheuvel 		__pmd_populate(pmd, pte_phys, PMD_TYPE_TABLE);
161c1cc1552SCatalin Marinas 	}
162d27cfa1fSArd Biesheuvel 	BUG_ON(pmd_bad(*pmd));
163d27cfa1fSArd Biesheuvel 
164d27cfa1fSArd Biesheuvel 	do {
165d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
166d27cfa1fSArd Biesheuvel 
167d27cfa1fSArd Biesheuvel 		next = pte_cont_addr_end(addr, end);
168d27cfa1fSArd Biesheuvel 
169d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
170d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PTE_MASK) == 0) &&
171d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
172d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
173d27cfa1fSArd Biesheuvel 
174d27cfa1fSArd Biesheuvel 		init_pte(pmd, addr, next, phys, __prot);
175d27cfa1fSArd Biesheuvel 
176d27cfa1fSArd Biesheuvel 		phys += next - addr;
177d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
178d27cfa1fSArd Biesheuvel }
179d27cfa1fSArd Biesheuvel 
180d27cfa1fSArd Biesheuvel static void init_pmd(pud_t *pud, unsigned long addr, unsigned long end,
181d27cfa1fSArd Biesheuvel 		     phys_addr_t phys, pgprot_t prot,
182d27cfa1fSArd Biesheuvel 		     phys_addr_t (*pgtable_alloc)(void), int flags)
183d27cfa1fSArd Biesheuvel {
184d27cfa1fSArd Biesheuvel 	unsigned long next;
185d27cfa1fSArd Biesheuvel 	pmd_t *pmd;
186c1cc1552SCatalin Marinas 
187f4710445SMark Rutland 	pmd = pmd_set_fixmap_offset(pud, addr);
188c1cc1552SCatalin Marinas 	do {
189e98216b5SArd Biesheuvel 		pmd_t old_pmd = *pmd;
190e98216b5SArd Biesheuvel 
191c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
192e98216b5SArd Biesheuvel 
193c1cc1552SCatalin Marinas 		/* try section mapping first */
19483863f25SLaura Abbott 		if (((addr | next | phys) & ~SECTION_MASK) == 0 &&
195c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
196d81bbe6dSMark Rutland 			pmd_set_huge(pmd, phys, prot);
197e98216b5SArd Biesheuvel 
198a55f9929SCatalin Marinas 			/*
199e98216b5SArd Biesheuvel 			 * After the PMD entry has been populated once, we
200e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
201a55f9929SCatalin Marinas 			 */
202e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pmd_val(old_pmd),
203e98216b5SArd Biesheuvel 						      pmd_val(*pmd)));
204a55f9929SCatalin Marinas 		} else {
205d27cfa1fSArd Biesheuvel 			alloc_init_cont_pte(pmd, addr, next, phys, prot,
206d27cfa1fSArd Biesheuvel 					    pgtable_alloc, flags);
207e98216b5SArd Biesheuvel 
208e98216b5SArd Biesheuvel 			BUG_ON(pmd_val(old_pmd) != 0 &&
209e98216b5SArd Biesheuvel 			       pmd_val(old_pmd) != pmd_val(*pmd));
210a55f9929SCatalin Marinas 		}
211c1cc1552SCatalin Marinas 		phys += next - addr;
212c1cc1552SCatalin Marinas 	} while (pmd++, addr = next, addr != end);
213f4710445SMark Rutland 
214f4710445SMark Rutland 	pmd_clear_fixmap();
215c1cc1552SCatalin Marinas }
216c1cc1552SCatalin Marinas 
217d27cfa1fSArd Biesheuvel static void alloc_init_cont_pmd(pud_t *pud, unsigned long addr,
218d27cfa1fSArd Biesheuvel 				unsigned long end, phys_addr_t phys,
219d27cfa1fSArd Biesheuvel 				pgprot_t prot,
220d27cfa1fSArd Biesheuvel 				phys_addr_t (*pgtable_alloc)(void), int flags)
221d27cfa1fSArd Biesheuvel {
222d27cfa1fSArd Biesheuvel 	unsigned long next;
223d27cfa1fSArd Biesheuvel 
224d27cfa1fSArd Biesheuvel 	/*
225d27cfa1fSArd Biesheuvel 	 * Check for initial section mappings in the pgd/pud.
226d27cfa1fSArd Biesheuvel 	 */
227d27cfa1fSArd Biesheuvel 	BUG_ON(pud_sect(*pud));
228d27cfa1fSArd Biesheuvel 	if (pud_none(*pud)) {
229d27cfa1fSArd Biesheuvel 		phys_addr_t pmd_phys;
230d27cfa1fSArd Biesheuvel 		BUG_ON(!pgtable_alloc);
231d27cfa1fSArd Biesheuvel 		pmd_phys = pgtable_alloc();
232d27cfa1fSArd Biesheuvel 		__pud_populate(pud, pmd_phys, PUD_TYPE_TABLE);
233d27cfa1fSArd Biesheuvel 	}
234d27cfa1fSArd Biesheuvel 	BUG_ON(pud_bad(*pud));
235d27cfa1fSArd Biesheuvel 
236d27cfa1fSArd Biesheuvel 	do {
237d27cfa1fSArd Biesheuvel 		pgprot_t __prot = prot;
238d27cfa1fSArd Biesheuvel 
239d27cfa1fSArd Biesheuvel 		next = pmd_cont_addr_end(addr, end);
240d27cfa1fSArd Biesheuvel 
241d27cfa1fSArd Biesheuvel 		/* use a contiguous mapping if the range is suitably aligned */
242d27cfa1fSArd Biesheuvel 		if ((((addr | next | phys) & ~CONT_PMD_MASK) == 0) &&
243d27cfa1fSArd Biesheuvel 		    (flags & NO_CONT_MAPPINGS) == 0)
244d27cfa1fSArd Biesheuvel 			__prot = __pgprot(pgprot_val(prot) | PTE_CONT);
245d27cfa1fSArd Biesheuvel 
246d27cfa1fSArd Biesheuvel 		init_pmd(pud, addr, next, phys, __prot, pgtable_alloc, flags);
247d27cfa1fSArd Biesheuvel 
248d27cfa1fSArd Biesheuvel 		phys += next - addr;
249d27cfa1fSArd Biesheuvel 	} while (addr = next, addr != end);
250d27cfa1fSArd Biesheuvel }
251d27cfa1fSArd Biesheuvel 
252da141706SLaura Abbott static inline bool use_1G_block(unsigned long addr, unsigned long next,
253da141706SLaura Abbott 			unsigned long phys)
254da141706SLaura Abbott {
255da141706SLaura Abbott 	if (PAGE_SHIFT != 12)
256da141706SLaura Abbott 		return false;
257da141706SLaura Abbott 
258da141706SLaura Abbott 	if (((addr | next | phys) & ~PUD_MASK) != 0)
259da141706SLaura Abbott 		return false;
260da141706SLaura Abbott 
261da141706SLaura Abbott 	return true;
262da141706SLaura Abbott }
263da141706SLaura Abbott 
26411509a30SMark Rutland static void alloc_init_pud(pgd_t *pgd, unsigned long addr, unsigned long end,
265da141706SLaura Abbott 				  phys_addr_t phys, pgprot_t prot,
26653e1b329SArd Biesheuvel 				  phys_addr_t (*pgtable_alloc)(void),
267c0951366SArd Biesheuvel 				  int flags)
268c1cc1552SCatalin Marinas {
269c79b954bSJungseok Lee 	pud_t *pud;
270c1cc1552SCatalin Marinas 	unsigned long next;
271c1cc1552SCatalin Marinas 
272c79b954bSJungseok Lee 	if (pgd_none(*pgd)) {
273132233a7SLaura Abbott 		phys_addr_t pud_phys;
274132233a7SLaura Abbott 		BUG_ON(!pgtable_alloc);
275132233a7SLaura Abbott 		pud_phys = pgtable_alloc();
276f4710445SMark Rutland 		__pgd_populate(pgd, pud_phys, PUD_TYPE_TABLE);
277c79b954bSJungseok Lee 	}
278c79b954bSJungseok Lee 	BUG_ON(pgd_bad(*pgd));
279c79b954bSJungseok Lee 
280f4710445SMark Rutland 	pud = pud_set_fixmap_offset(pgd, addr);
281c1cc1552SCatalin Marinas 	do {
282e98216b5SArd Biesheuvel 		pud_t old_pud = *pud;
283e98216b5SArd Biesheuvel 
284c1cc1552SCatalin Marinas 		next = pud_addr_end(addr, end);
285206a2a73SSteve Capper 
286206a2a73SSteve Capper 		/*
287206a2a73SSteve Capper 		 * For 4K granule only, attempt to put down a 1GB block
288206a2a73SSteve Capper 		 */
289c0951366SArd Biesheuvel 		if (use_1G_block(addr, next, phys) &&
290c0951366SArd Biesheuvel 		    (flags & NO_BLOCK_MAPPINGS) == 0) {
291c661cb1cSMark Rutland 			pud_set_huge(pud, phys, prot);
292206a2a73SSteve Capper 
293206a2a73SSteve Capper 			/*
294e98216b5SArd Biesheuvel 			 * After the PUD entry has been populated once, we
295e98216b5SArd Biesheuvel 			 * only allow updates to the permission attributes.
296206a2a73SSteve Capper 			 */
297e98216b5SArd Biesheuvel 			BUG_ON(!pgattr_change_is_safe(pud_val(old_pud),
298e98216b5SArd Biesheuvel 						      pud_val(*pud)));
299206a2a73SSteve Capper 		} else {
300d27cfa1fSArd Biesheuvel 			alloc_init_cont_pmd(pud, addr, next, phys, prot,
301c0951366SArd Biesheuvel 					    pgtable_alloc, flags);
302e98216b5SArd Biesheuvel 
303e98216b5SArd Biesheuvel 			BUG_ON(pud_val(old_pud) != 0 &&
304e98216b5SArd Biesheuvel 			       pud_val(old_pud) != pud_val(*pud));
305206a2a73SSteve Capper 		}
306c1cc1552SCatalin Marinas 		phys += next - addr;
307c1cc1552SCatalin Marinas 	} while (pud++, addr = next, addr != end);
308f4710445SMark Rutland 
309f4710445SMark Rutland 	pud_clear_fixmap();
310c1cc1552SCatalin Marinas }
311c1cc1552SCatalin Marinas 
31240f87d31SArd Biesheuvel static void __create_pgd_mapping(pgd_t *pgdir, phys_addr_t phys,
31340f87d31SArd Biesheuvel 				 unsigned long virt, phys_addr_t size,
31440f87d31SArd Biesheuvel 				 pgprot_t prot,
31553e1b329SArd Biesheuvel 				 phys_addr_t (*pgtable_alloc)(void),
316c0951366SArd Biesheuvel 				 int flags)
317c1cc1552SCatalin Marinas {
318c1cc1552SCatalin Marinas 	unsigned long addr, length, end, next;
31940f87d31SArd Biesheuvel 	pgd_t *pgd = pgd_offset_raw(pgdir, virt);
320c1cc1552SCatalin Marinas 
321cc5d2b3bSMark Rutland 	/*
322cc5d2b3bSMark Rutland 	 * If the virtual and physical address don't have the same offset
323cc5d2b3bSMark Rutland 	 * within a page, we cannot map the region as the caller expects.
324cc5d2b3bSMark Rutland 	 */
325cc5d2b3bSMark Rutland 	if (WARN_ON((phys ^ virt) & ~PAGE_MASK))
326cc5d2b3bSMark Rutland 		return;
327cc5d2b3bSMark Rutland 
3289c4e08a3SMark Rutland 	phys &= PAGE_MASK;
329c1cc1552SCatalin Marinas 	addr = virt & PAGE_MASK;
330c1cc1552SCatalin Marinas 	length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
331c1cc1552SCatalin Marinas 
332c1cc1552SCatalin Marinas 	end = addr + length;
333c1cc1552SCatalin Marinas 	do {
334c1cc1552SCatalin Marinas 		next = pgd_addr_end(addr, end);
33553e1b329SArd Biesheuvel 		alloc_init_pud(pgd, addr, next, phys, prot, pgtable_alloc,
336c0951366SArd Biesheuvel 			       flags);
337c1cc1552SCatalin Marinas 		phys += next - addr;
338c1cc1552SCatalin Marinas 	} while (pgd++, addr = next, addr != end);
339c1cc1552SCatalin Marinas }
340c1cc1552SCatalin Marinas 
3411378dc3dSArd Biesheuvel static phys_addr_t pgd_pgtable_alloc(void)
342da141706SLaura Abbott {
34321ab99c2SMark Rutland 	void *ptr = (void *)__get_free_page(PGALLOC_GFP);
3441378dc3dSArd Biesheuvel 	if (!ptr || !pgtable_page_ctor(virt_to_page(ptr)))
3451378dc3dSArd Biesheuvel 		BUG();
34621ab99c2SMark Rutland 
34721ab99c2SMark Rutland 	/* Ensure the zeroed page is visible to the page table walker */
34821ab99c2SMark Rutland 	dsb(ishst);
349f4710445SMark Rutland 	return __pa(ptr);
350da141706SLaura Abbott }
351da141706SLaura Abbott 
352132233a7SLaura Abbott /*
353132233a7SLaura Abbott  * This function can only be used to modify existing table entries,
354132233a7SLaura Abbott  * without allocating new levels of table. Note that this permits the
355132233a7SLaura Abbott  * creation of new section or page entries.
356132233a7SLaura Abbott  */
357132233a7SLaura Abbott static void __init create_mapping_noalloc(phys_addr_t phys, unsigned long virt,
358da141706SLaura Abbott 				  phys_addr_t size, pgprot_t prot)
359d7ecbddfSMark Salter {
360d7ecbddfSMark Salter 	if (virt < VMALLOC_START) {
361d7ecbddfSMark Salter 		pr_warn("BUG: not creating mapping for %pa at 0x%016lx - outside kernel range\n",
362d7ecbddfSMark Salter 			&phys, virt);
363d7ecbddfSMark Salter 		return;
364d7ecbddfSMark Salter 	}
365d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
366d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
367d7ecbddfSMark Salter }
368d7ecbddfSMark Salter 
3698ce837ceSArd Biesheuvel void __init create_pgd_mapping(struct mm_struct *mm, phys_addr_t phys,
3708ce837ceSArd Biesheuvel 			       unsigned long virt, phys_addr_t size,
371f14c66ceSArd Biesheuvel 			       pgprot_t prot, bool page_mappings_only)
3728ce837ceSArd Biesheuvel {
373c0951366SArd Biesheuvel 	int flags = 0;
374c0951366SArd Biesheuvel 
3751378dc3dSArd Biesheuvel 	BUG_ON(mm == &init_mm);
3761378dc3dSArd Biesheuvel 
377c0951366SArd Biesheuvel 	if (page_mappings_only)
378d27cfa1fSArd Biesheuvel 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
379c0951366SArd Biesheuvel 
38011509a30SMark Rutland 	__create_pgd_mapping(mm->pgd, phys, virt, size, prot,
381c0951366SArd Biesheuvel 			     pgd_pgtable_alloc, flags);
382d7ecbddfSMark Salter }
383d7ecbddfSMark Salter 
384aa8c09beSArd Biesheuvel static void update_mapping_prot(phys_addr_t phys, unsigned long virt,
385da141706SLaura Abbott 				phys_addr_t size, pgprot_t prot)
386da141706SLaura Abbott {
387da141706SLaura Abbott 	if (virt < VMALLOC_START) {
388aa8c09beSArd Biesheuvel 		pr_warn("BUG: not updating mapping for %pa at 0x%016lx - outside kernel range\n",
389da141706SLaura Abbott 			&phys, virt);
390da141706SLaura Abbott 		return;
391da141706SLaura Abbott 	}
392da141706SLaura Abbott 
393d27cfa1fSArd Biesheuvel 	__create_pgd_mapping(init_mm.pgd, phys, virt, size, prot, NULL,
394d27cfa1fSArd Biesheuvel 			     NO_CONT_MAPPINGS);
395aa8c09beSArd Biesheuvel 
396aa8c09beSArd Biesheuvel 	/* flush the TLBs after updating live kernel mappings */
397aa8c09beSArd Biesheuvel 	flush_tlb_kernel_range(virt, virt + size);
398da141706SLaura Abbott }
399da141706SLaura Abbott 
40098d2e153STakahiro Akashi static void __init __map_memblock(pgd_t *pgd, phys_addr_t start,
40198d2e153STakahiro Akashi 				  phys_addr_t end, pgprot_t prot, int flags)
402da141706SLaura Abbott {
40398d2e153STakahiro Akashi 	__create_pgd_mapping(pgd, start, __phys_to_virt(start), end - start,
40498d2e153STakahiro Akashi 			     prot, early_pgtable_alloc, flags);
405da141706SLaura Abbott }
406da141706SLaura Abbott 
4075ea5306cSArd Biesheuvel void __init mark_linear_text_alias_ro(void)
4085ea5306cSArd Biesheuvel {
4095ea5306cSArd Biesheuvel 	/*
4105ea5306cSArd Biesheuvel 	 * Remove the write permissions from the linear alias of .text/.rodata
4115ea5306cSArd Biesheuvel 	 */
4125ea5306cSArd Biesheuvel 	update_mapping_prot(__pa_symbol(_text), (unsigned long)lm_alias(_text),
4135ea5306cSArd Biesheuvel 			    (unsigned long)__init_begin - (unsigned long)_text,
4145ea5306cSArd Biesheuvel 			    PAGE_KERNEL_RO);
4155ea5306cSArd Biesheuvel }
4165ea5306cSArd Biesheuvel 
417068a17a5SMark Rutland static void __init map_mem(pgd_t *pgd)
418c1cc1552SCatalin Marinas {
41998d2e153STakahiro Akashi 	phys_addr_t kernel_start = __pa_symbol(_text);
42098d2e153STakahiro Akashi 	phys_addr_t kernel_end = __pa_symbol(__init_begin);
421c1cc1552SCatalin Marinas 	struct memblock_region *reg;
42298d2e153STakahiro Akashi 	int flags = 0;
42398d2e153STakahiro Akashi 
42498d2e153STakahiro Akashi 	if (debug_pagealloc_enabled())
42598d2e153STakahiro Akashi 		flags = NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS;
42698d2e153STakahiro Akashi 
42798d2e153STakahiro Akashi 	/*
42898d2e153STakahiro Akashi 	 * Take care not to create a writable alias for the
42998d2e153STakahiro Akashi 	 * read-only text and rodata sections of the kernel image.
43098d2e153STakahiro Akashi 	 * So temporarily mark them as NOMAP to skip mappings in
43198d2e153STakahiro Akashi 	 * the following for-loop
43298d2e153STakahiro Akashi 	 */
43398d2e153STakahiro Akashi 	memblock_mark_nomap(kernel_start, kernel_end - kernel_start);
43498d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
43598d2e153STakahiro Akashi 	if (crashk_res.end)
43698d2e153STakahiro Akashi 		memblock_mark_nomap(crashk_res.start,
43798d2e153STakahiro Akashi 				    resource_size(&crashk_res));
43898d2e153STakahiro Akashi #endif
439f6bc87c3SSteve Capper 
440c1cc1552SCatalin Marinas 	/* map all the memory banks */
441c1cc1552SCatalin Marinas 	for_each_memblock(memory, reg) {
442c1cc1552SCatalin Marinas 		phys_addr_t start = reg->base;
443c1cc1552SCatalin Marinas 		phys_addr_t end = start + reg->size;
444c1cc1552SCatalin Marinas 
445c1cc1552SCatalin Marinas 		if (start >= end)
446c1cc1552SCatalin Marinas 			break;
44768709f45SArd Biesheuvel 		if (memblock_is_nomap(reg))
44868709f45SArd Biesheuvel 			continue;
449c1cc1552SCatalin Marinas 
45098d2e153STakahiro Akashi 		__map_memblock(pgd, start, end, PAGE_KERNEL, flags);
451c1cc1552SCatalin Marinas 	}
45298d2e153STakahiro Akashi 
45398d2e153STakahiro Akashi 	/*
45498d2e153STakahiro Akashi 	 * Map the linear alias of the [_text, __init_begin) interval
45598d2e153STakahiro Akashi 	 * as non-executable now, and remove the write permission in
45698d2e153STakahiro Akashi 	 * mark_linear_text_alias_ro() below (which will be called after
45798d2e153STakahiro Akashi 	 * alternative patching has completed). This makes the contents
45898d2e153STakahiro Akashi 	 * of the region accessible to subsystems such as hibernate,
45998d2e153STakahiro Akashi 	 * but protects it from inadvertent modification or execution.
46098d2e153STakahiro Akashi 	 * Note that contiguous mappings cannot be remapped in this way,
46198d2e153STakahiro Akashi 	 * so we should avoid them here.
46298d2e153STakahiro Akashi 	 */
46398d2e153STakahiro Akashi 	__map_memblock(pgd, kernel_start, kernel_end,
46498d2e153STakahiro Akashi 		       PAGE_KERNEL, NO_CONT_MAPPINGS);
46598d2e153STakahiro Akashi 	memblock_clear_nomap(kernel_start, kernel_end - kernel_start);
46698d2e153STakahiro Akashi 
46798d2e153STakahiro Akashi #ifdef CONFIG_KEXEC_CORE
46898d2e153STakahiro Akashi 	/*
46998d2e153STakahiro Akashi 	 * Use page-level mappings here so that we can shrink the region
47098d2e153STakahiro Akashi 	 * in page granularity and put back unused memory to buddy system
47198d2e153STakahiro Akashi 	 * through /sys/kernel/kexec_crash_size interface.
47298d2e153STakahiro Akashi 	 */
47398d2e153STakahiro Akashi 	if (crashk_res.end) {
47498d2e153STakahiro Akashi 		__map_memblock(pgd, crashk_res.start, crashk_res.end + 1,
47598d2e153STakahiro Akashi 			       PAGE_KERNEL,
47698d2e153STakahiro Akashi 			       NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS);
47798d2e153STakahiro Akashi 		memblock_clear_nomap(crashk_res.start,
47898d2e153STakahiro Akashi 				     resource_size(&crashk_res));
47998d2e153STakahiro Akashi 	}
48098d2e153STakahiro Akashi #endif
481c1cc1552SCatalin Marinas }
482c1cc1552SCatalin Marinas 
483da141706SLaura Abbott void mark_rodata_ro(void)
484da141706SLaura Abbott {
4852f39b5f9SJeremy Linton 	unsigned long section_size;
486f9040773SArd Biesheuvel 
4872f39b5f9SJeremy Linton 	/*
4889fdc14c5SArd Biesheuvel 	 * mark .rodata as read only. Use __init_begin rather than __end_rodata
4899fdc14c5SArd Biesheuvel 	 * to cover NOTES and EXCEPTION_TABLE.
4902f39b5f9SJeremy Linton 	 */
4919fdc14c5SArd Biesheuvel 	section_size = (unsigned long)__init_begin - (unsigned long)__start_rodata;
492aa8c09beSArd Biesheuvel 	update_mapping_prot(__pa_symbol(__start_rodata), (unsigned long)__start_rodata,
4932f39b5f9SJeremy Linton 			    section_size, PAGE_KERNEL_RO);
494e98216b5SArd Biesheuvel 
4951404d6f1SLaura Abbott 	debug_checkwx();
496da141706SLaura Abbott }
497da141706SLaura Abbott 
4982c09ec06SArd Biesheuvel static void __init map_kernel_segment(pgd_t *pgd, void *va_start, void *va_end,
499d27cfa1fSArd Biesheuvel 				      pgprot_t prot, struct vm_struct *vma,
50092bbd16eSWill Deacon 				      int flags, unsigned long vm_flags)
501068a17a5SMark Rutland {
5022077be67SLaura Abbott 	phys_addr_t pa_start = __pa_symbol(va_start);
503068a17a5SMark Rutland 	unsigned long size = va_end - va_start;
504068a17a5SMark Rutland 
505068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(pa_start));
506068a17a5SMark Rutland 	BUG_ON(!PAGE_ALIGNED(size));
507068a17a5SMark Rutland 
508068a17a5SMark Rutland 	__create_pgd_mapping(pgd, pa_start, (unsigned long)va_start, size, prot,
509d27cfa1fSArd Biesheuvel 			     early_pgtable_alloc, flags);
510f9040773SArd Biesheuvel 
51192bbd16eSWill Deacon 	if (!(vm_flags & VM_NO_GUARD))
51292bbd16eSWill Deacon 		size += PAGE_SIZE;
51392bbd16eSWill Deacon 
514f9040773SArd Biesheuvel 	vma->addr	= va_start;
515f9040773SArd Biesheuvel 	vma->phys_addr	= pa_start;
516f9040773SArd Biesheuvel 	vma->size	= size;
51792bbd16eSWill Deacon 	vma->flags	= VM_MAP | vm_flags;
518f9040773SArd Biesheuvel 	vma->caller	= __builtin_return_address(0);
519f9040773SArd Biesheuvel 
520f9040773SArd Biesheuvel 	vm_area_add_early(vma);
521068a17a5SMark Rutland }
522068a17a5SMark Rutland 
52328b066daSArd Biesheuvel static int __init parse_rodata(char *arg)
52428b066daSArd Biesheuvel {
52528b066daSArd Biesheuvel 	return strtobool(arg, &rodata_enabled);
52628b066daSArd Biesheuvel }
52728b066daSArd Biesheuvel early_param("rodata", parse_rodata);
52828b066daSArd Biesheuvel 
529068a17a5SMark Rutland /*
530068a17a5SMark Rutland  * Create fine-grained mappings for the kernel.
531068a17a5SMark Rutland  */
532068a17a5SMark Rutland static void __init map_kernel(pgd_t *pgd)
533068a17a5SMark Rutland {
5342ebe088bSArd Biesheuvel 	static struct vm_struct vmlinux_text, vmlinux_rodata, vmlinux_inittext,
5352ebe088bSArd Biesheuvel 				vmlinux_initdata, vmlinux_data;
536068a17a5SMark Rutland 
53728b066daSArd Biesheuvel 	/*
53828b066daSArd Biesheuvel 	 * External debuggers may need to write directly to the text
53928b066daSArd Biesheuvel 	 * mapping to install SW breakpoints. Allow this (only) when
54028b066daSArd Biesheuvel 	 * explicitly requested with rodata=off.
54128b066daSArd Biesheuvel 	 */
54228b066daSArd Biesheuvel 	pgprot_t text_prot = rodata_enabled ? PAGE_KERNEL_ROX : PAGE_KERNEL_EXEC;
54328b066daSArd Biesheuvel 
544d27cfa1fSArd Biesheuvel 	/*
545d27cfa1fSArd Biesheuvel 	 * Only rodata will be remapped with different permissions later on,
546d27cfa1fSArd Biesheuvel 	 * all other segments are allowed to use contiguous mappings.
547d27cfa1fSArd Biesheuvel 	 */
54892bbd16eSWill Deacon 	map_kernel_segment(pgd, _text, _etext, text_prot, &vmlinux_text, 0,
54992bbd16eSWill Deacon 			   VM_NO_GUARD);
5502ebe088bSArd Biesheuvel 	map_kernel_segment(pgd, __start_rodata, __inittext_begin, PAGE_KERNEL,
55192bbd16eSWill Deacon 			   &vmlinux_rodata, NO_CONT_MAPPINGS, VM_NO_GUARD);
5522ebe088bSArd Biesheuvel 	map_kernel_segment(pgd, __inittext_begin, __inittext_end, text_prot,
55392bbd16eSWill Deacon 			   &vmlinux_inittext, 0, VM_NO_GUARD);
5542ebe088bSArd Biesheuvel 	map_kernel_segment(pgd, __initdata_begin, __initdata_end, PAGE_KERNEL,
55592bbd16eSWill Deacon 			   &vmlinux_initdata, 0, VM_NO_GUARD);
55692bbd16eSWill Deacon 	map_kernel_segment(pgd, _data, _end, PAGE_KERNEL, &vmlinux_data, 0, 0);
557068a17a5SMark Rutland 
558f9040773SArd Biesheuvel 	if (!pgd_val(*pgd_offset_raw(pgd, FIXADDR_START))) {
559068a17a5SMark Rutland 		/*
560f9040773SArd Biesheuvel 		 * The fixmap falls in a separate pgd to the kernel, and doesn't
561f9040773SArd Biesheuvel 		 * live in the carveout for the swapper_pg_dir. We can simply
562f9040773SArd Biesheuvel 		 * re-use the existing dir for the fixmap.
563068a17a5SMark Rutland 		 */
564f9040773SArd Biesheuvel 		set_pgd(pgd_offset_raw(pgd, FIXADDR_START),
565f9040773SArd Biesheuvel 			*pgd_offset_k(FIXADDR_START));
566f9040773SArd Biesheuvel 	} else if (CONFIG_PGTABLE_LEVELS > 3) {
567f9040773SArd Biesheuvel 		/*
568f9040773SArd Biesheuvel 		 * The fixmap shares its top level pgd entry with the kernel
569f9040773SArd Biesheuvel 		 * mapping. This can really only occur when we are running
570f9040773SArd Biesheuvel 		 * with 16k/4 levels, so we can simply reuse the pud level
571f9040773SArd Biesheuvel 		 * entry instead.
572f9040773SArd Biesheuvel 		 */
573f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
57419338304SKristina Martsenko 		pud_populate(&init_mm, pud_set_fixmap_offset(pgd, FIXADDR_START),
57519338304SKristina Martsenko 			     lm_alias(bm_pmd));
576f9040773SArd Biesheuvel 		pud_clear_fixmap();
577f9040773SArd Biesheuvel 	} else {
578f9040773SArd Biesheuvel 		BUG();
579f9040773SArd Biesheuvel 	}
580068a17a5SMark Rutland 
581068a17a5SMark Rutland 	kasan_copy_shadow(pgd);
582068a17a5SMark Rutland }
583068a17a5SMark Rutland 
584c1cc1552SCatalin Marinas /*
585c1cc1552SCatalin Marinas  * paging_init() sets up the page tables, initialises the zone memory
586c1cc1552SCatalin Marinas  * maps and sets up the zero page.
587c1cc1552SCatalin Marinas  */
588c1cc1552SCatalin Marinas void __init paging_init(void)
589c1cc1552SCatalin Marinas {
590068a17a5SMark Rutland 	phys_addr_t pgd_phys = early_pgtable_alloc();
591068a17a5SMark Rutland 	pgd_t *pgd = pgd_set_fixmap(pgd_phys);
592068a17a5SMark Rutland 
593068a17a5SMark Rutland 	map_kernel(pgd);
594068a17a5SMark Rutland 	map_mem(pgd);
595068a17a5SMark Rutland 
596068a17a5SMark Rutland 	/*
597068a17a5SMark Rutland 	 * We want to reuse the original swapper_pg_dir so we don't have to
598068a17a5SMark Rutland 	 * communicate the new address to non-coherent secondaries in
599068a17a5SMark Rutland 	 * secondary_entry, and so cpu_switch_mm can generate the address with
600068a17a5SMark Rutland 	 * adrp+add rather than a load from some global variable.
601068a17a5SMark Rutland 	 *
602068a17a5SMark Rutland 	 * To do this we need to go via a temporary pgd.
603068a17a5SMark Rutland 	 */
604068a17a5SMark Rutland 	cpu_replace_ttbr1(__va(pgd_phys));
60512f043ffSArnd Bergmann 	memcpy(swapper_pg_dir, pgd, PGD_SIZE);
6062077be67SLaura Abbott 	cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
607068a17a5SMark Rutland 
608068a17a5SMark Rutland 	pgd_clear_fixmap();
609068a17a5SMark Rutland 	memblock_free(pgd_phys, PAGE_SIZE);
610068a17a5SMark Rutland 
611068a17a5SMark Rutland 	/*
612068a17a5SMark Rutland 	 * We only reuse the PGD from the swapper_pg_dir, not the pud + pmd
613068a17a5SMark Rutland 	 * allocated with it.
614068a17a5SMark Rutland 	 */
6152077be67SLaura Abbott 	memblock_free(__pa_symbol(swapper_pg_dir) + PAGE_SIZE,
616068a17a5SMark Rutland 		      SWAPPER_DIR_SIZE - PAGE_SIZE);
617c1cc1552SCatalin Marinas }
618c1cc1552SCatalin Marinas 
619c1cc1552SCatalin Marinas /*
620c1cc1552SCatalin Marinas  * Check whether a kernel address is valid (derived from arch/x86/).
621c1cc1552SCatalin Marinas  */
622c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr)
623c1cc1552SCatalin Marinas {
624c1cc1552SCatalin Marinas 	pgd_t *pgd;
625c1cc1552SCatalin Marinas 	pud_t *pud;
626c1cc1552SCatalin Marinas 	pmd_t *pmd;
627c1cc1552SCatalin Marinas 	pte_t *pte;
628c1cc1552SCatalin Marinas 
629c1cc1552SCatalin Marinas 	if ((((long)addr) >> VA_BITS) != -1UL)
630c1cc1552SCatalin Marinas 		return 0;
631c1cc1552SCatalin Marinas 
632c1cc1552SCatalin Marinas 	pgd = pgd_offset_k(addr);
633c1cc1552SCatalin Marinas 	if (pgd_none(*pgd))
634c1cc1552SCatalin Marinas 		return 0;
635c1cc1552SCatalin Marinas 
636c1cc1552SCatalin Marinas 	pud = pud_offset(pgd, addr);
637c1cc1552SCatalin Marinas 	if (pud_none(*pud))
638c1cc1552SCatalin Marinas 		return 0;
639c1cc1552SCatalin Marinas 
640206a2a73SSteve Capper 	if (pud_sect(*pud))
641206a2a73SSteve Capper 		return pfn_valid(pud_pfn(*pud));
642206a2a73SSteve Capper 
643c1cc1552SCatalin Marinas 	pmd = pmd_offset(pud, addr);
644c1cc1552SCatalin Marinas 	if (pmd_none(*pmd))
645c1cc1552SCatalin Marinas 		return 0;
646c1cc1552SCatalin Marinas 
647da6e4cb6SDave Anderson 	if (pmd_sect(*pmd))
648da6e4cb6SDave Anderson 		return pfn_valid(pmd_pfn(*pmd));
649da6e4cb6SDave Anderson 
650c1cc1552SCatalin Marinas 	pte = pte_offset_kernel(pmd, addr);
651c1cc1552SCatalin Marinas 	if (pte_none(*pte))
652c1cc1552SCatalin Marinas 		return 0;
653c1cc1552SCatalin Marinas 
654c1cc1552SCatalin Marinas 	return pfn_valid(pte_pfn(*pte));
655c1cc1552SCatalin Marinas }
656c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP
657b433dce0SSuzuki K. Poulose #if !ARM64_SWAPPER_USES_SECTION_MAPS
6580aad818bSJohannes Weiner int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
659c1cc1552SCatalin Marinas {
6600aad818bSJohannes Weiner 	return vmemmap_populate_basepages(start, end, node);
661c1cc1552SCatalin Marinas }
662b433dce0SSuzuki K. Poulose #else	/* !ARM64_SWAPPER_USES_SECTION_MAPS */
6630aad818bSJohannes Weiner int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node)
664c1cc1552SCatalin Marinas {
6650aad818bSJohannes Weiner 	unsigned long addr = start;
666c1cc1552SCatalin Marinas 	unsigned long next;
667c1cc1552SCatalin Marinas 	pgd_t *pgd;
668c1cc1552SCatalin Marinas 	pud_t *pud;
669c1cc1552SCatalin Marinas 	pmd_t *pmd;
670c1cc1552SCatalin Marinas 
671c1cc1552SCatalin Marinas 	do {
672c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
673c1cc1552SCatalin Marinas 
674c1cc1552SCatalin Marinas 		pgd = vmemmap_pgd_populate(addr, node);
675c1cc1552SCatalin Marinas 		if (!pgd)
676c1cc1552SCatalin Marinas 			return -ENOMEM;
677c1cc1552SCatalin Marinas 
678c1cc1552SCatalin Marinas 		pud = vmemmap_pud_populate(pgd, addr, node);
679c1cc1552SCatalin Marinas 		if (!pud)
680c1cc1552SCatalin Marinas 			return -ENOMEM;
681c1cc1552SCatalin Marinas 
682c1cc1552SCatalin Marinas 		pmd = pmd_offset(pud, addr);
683c1cc1552SCatalin Marinas 		if (pmd_none(*pmd)) {
684c1cc1552SCatalin Marinas 			void *p = NULL;
685c1cc1552SCatalin Marinas 
686c1cc1552SCatalin Marinas 			p = vmemmap_alloc_block_buf(PMD_SIZE, node);
687c1cc1552SCatalin Marinas 			if (!p)
688c1cc1552SCatalin Marinas 				return -ENOMEM;
689c1cc1552SCatalin Marinas 
69019338304SKristina Martsenko 			pmd_set_huge(pmd, __pa(p), __pgprot(PROT_SECT_NORMAL));
691c1cc1552SCatalin Marinas 		} else
692c1cc1552SCatalin Marinas 			vmemmap_verify((pte_t *)pmd, node, addr, next);
693c1cc1552SCatalin Marinas 	} while (addr = next, addr != end);
694c1cc1552SCatalin Marinas 
695c1cc1552SCatalin Marinas 	return 0;
696c1cc1552SCatalin Marinas }
697c1cc1552SCatalin Marinas #endif	/* CONFIG_ARM64_64K_PAGES */
6980aad818bSJohannes Weiner void vmemmap_free(unsigned long start, unsigned long end)
6990197518cSTang Chen {
7000197518cSTang Chen }
701c1cc1552SCatalin Marinas #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
702af86e597SLaura Abbott 
703af86e597SLaura Abbott static inline pud_t * fixmap_pud(unsigned long addr)
704af86e597SLaura Abbott {
705af86e597SLaura Abbott 	pgd_t *pgd = pgd_offset_k(addr);
706af86e597SLaura Abbott 
707af86e597SLaura Abbott 	BUG_ON(pgd_none(*pgd) || pgd_bad(*pgd));
708af86e597SLaura Abbott 
709157962f5SArd Biesheuvel 	return pud_offset_kimg(pgd, addr);
710af86e597SLaura Abbott }
711af86e597SLaura Abbott 
712af86e597SLaura Abbott static inline pmd_t * fixmap_pmd(unsigned long addr)
713af86e597SLaura Abbott {
714af86e597SLaura Abbott 	pud_t *pud = fixmap_pud(addr);
715af86e597SLaura Abbott 
716af86e597SLaura Abbott 	BUG_ON(pud_none(*pud) || pud_bad(*pud));
717af86e597SLaura Abbott 
718157962f5SArd Biesheuvel 	return pmd_offset_kimg(pud, addr);
719af86e597SLaura Abbott }
720af86e597SLaura Abbott 
721af86e597SLaura Abbott static inline pte_t * fixmap_pte(unsigned long addr)
722af86e597SLaura Abbott {
723157962f5SArd Biesheuvel 	return &bm_pte[pte_index(addr)];
724af86e597SLaura Abbott }
725af86e597SLaura Abbott 
7262077be67SLaura Abbott /*
7272077be67SLaura Abbott  * The p*d_populate functions call virt_to_phys implicitly so they can't be used
7282077be67SLaura Abbott  * directly on kernel symbols (bm_p*d). This function is called too early to use
7292077be67SLaura Abbott  * lm_alias so __p*d_populate functions must be used to populate with the
7302077be67SLaura Abbott  * physical address from __pa_symbol.
7312077be67SLaura Abbott  */
732af86e597SLaura Abbott void __init early_fixmap_init(void)
733af86e597SLaura Abbott {
734af86e597SLaura Abbott 	pgd_t *pgd;
735af86e597SLaura Abbott 	pud_t *pud;
736af86e597SLaura Abbott 	pmd_t *pmd;
737af86e597SLaura Abbott 	unsigned long addr = FIXADDR_START;
738af86e597SLaura Abbott 
739af86e597SLaura Abbott 	pgd = pgd_offset_k(addr);
740f80fb3a3SArd Biesheuvel 	if (CONFIG_PGTABLE_LEVELS > 3 &&
7412077be67SLaura Abbott 	    !(pgd_none(*pgd) || pgd_page_paddr(*pgd) == __pa_symbol(bm_pud))) {
742f9040773SArd Biesheuvel 		/*
743f9040773SArd Biesheuvel 		 * We only end up here if the kernel mapping and the fixmap
744f9040773SArd Biesheuvel 		 * share the top level pgd entry, which should only happen on
745f9040773SArd Biesheuvel 		 * 16k/4 levels configurations.
746f9040773SArd Biesheuvel 		 */
747f9040773SArd Biesheuvel 		BUG_ON(!IS_ENABLED(CONFIG_ARM64_16K_PAGES));
748f9040773SArd Biesheuvel 		pud = pud_offset_kimg(pgd, addr);
749f9040773SArd Biesheuvel 	} else {
7502077be67SLaura Abbott 		if (pgd_none(*pgd))
7512077be67SLaura Abbott 			__pgd_populate(pgd, __pa_symbol(bm_pud), PUD_TYPE_TABLE);
752157962f5SArd Biesheuvel 		pud = fixmap_pud(addr);
753f9040773SArd Biesheuvel 	}
7542077be67SLaura Abbott 	if (pud_none(*pud))
7552077be67SLaura Abbott 		__pud_populate(pud, __pa_symbol(bm_pmd), PMD_TYPE_TABLE);
756157962f5SArd Biesheuvel 	pmd = fixmap_pmd(addr);
7572077be67SLaura Abbott 	__pmd_populate(pmd, __pa_symbol(bm_pte), PMD_TYPE_TABLE);
758af86e597SLaura Abbott 
759af86e597SLaura Abbott 	/*
760af86e597SLaura Abbott 	 * The boot-ioremap range spans multiple pmds, for which
761157962f5SArd Biesheuvel 	 * we are not prepared:
762af86e597SLaura Abbott 	 */
763af86e597SLaura Abbott 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
764af86e597SLaura Abbott 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
765af86e597SLaura Abbott 
766af86e597SLaura Abbott 	if ((pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)))
767af86e597SLaura Abbott 	     || pmd != fixmap_pmd(fix_to_virt(FIX_BTMAP_END))) {
768af86e597SLaura Abbott 		WARN_ON(1);
769af86e597SLaura Abbott 		pr_warn("pmd %p != %p, %p\n",
770af86e597SLaura Abbott 			pmd, fixmap_pmd(fix_to_virt(FIX_BTMAP_BEGIN)),
771af86e597SLaura Abbott 			fixmap_pmd(fix_to_virt(FIX_BTMAP_END)));
772af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
773af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_BEGIN));
774af86e597SLaura Abbott 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
775af86e597SLaura Abbott 			fix_to_virt(FIX_BTMAP_END));
776af86e597SLaura Abbott 
777af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
778af86e597SLaura Abbott 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
779af86e597SLaura Abbott 	}
780af86e597SLaura Abbott }
781af86e597SLaura Abbott 
78218b4b276SJames Morse /*
78318b4b276SJames Morse  * Unusually, this is also called in IRQ context (ghes_iounmap_irq) so if we
78418b4b276SJames Morse  * ever need to use IPIs for TLB broadcasting, then we're in trouble here.
78518b4b276SJames Morse  */
786af86e597SLaura Abbott void __set_fixmap(enum fixed_addresses idx,
787af86e597SLaura Abbott 			       phys_addr_t phys, pgprot_t flags)
788af86e597SLaura Abbott {
789af86e597SLaura Abbott 	unsigned long addr = __fix_to_virt(idx);
790af86e597SLaura Abbott 	pte_t *pte;
791af86e597SLaura Abbott 
792b63dbef9SMark Rutland 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
793af86e597SLaura Abbott 
794af86e597SLaura Abbott 	pte = fixmap_pte(addr);
795af86e597SLaura Abbott 
796af86e597SLaura Abbott 	if (pgprot_val(flags)) {
797af86e597SLaura Abbott 		set_pte(pte, pfn_pte(phys >> PAGE_SHIFT, flags));
798af86e597SLaura Abbott 	} else {
799af86e597SLaura Abbott 		pte_clear(&init_mm, addr, pte);
800af86e597SLaura Abbott 		flush_tlb_kernel_range(addr, addr+PAGE_SIZE);
801af86e597SLaura Abbott 	}
802af86e597SLaura Abbott }
80361bd93ceSArd Biesheuvel 
804f80fb3a3SArd Biesheuvel void *__init __fixmap_remap_fdt(phys_addr_t dt_phys, int *size, pgprot_t prot)
80561bd93ceSArd Biesheuvel {
80661bd93ceSArd Biesheuvel 	const u64 dt_virt_base = __fix_to_virt(FIX_FDT);
807f80fb3a3SArd Biesheuvel 	int offset;
80861bd93ceSArd Biesheuvel 	void *dt_virt;
80961bd93ceSArd Biesheuvel 
81061bd93ceSArd Biesheuvel 	/*
81161bd93ceSArd Biesheuvel 	 * Check whether the physical FDT address is set and meets the minimum
81261bd93ceSArd Biesheuvel 	 * alignment requirement. Since we are relying on MIN_FDT_ALIGN to be
81304a84810SArd Biesheuvel 	 * at least 8 bytes so that we can always access the magic and size
81404a84810SArd Biesheuvel 	 * fields of the FDT header after mapping the first chunk, double check
81504a84810SArd Biesheuvel 	 * here if that is indeed the case.
81661bd93ceSArd Biesheuvel 	 */
81761bd93ceSArd Biesheuvel 	BUILD_BUG_ON(MIN_FDT_ALIGN < 8);
81861bd93ceSArd Biesheuvel 	if (!dt_phys || dt_phys % MIN_FDT_ALIGN)
81961bd93ceSArd Biesheuvel 		return NULL;
82061bd93ceSArd Biesheuvel 
82161bd93ceSArd Biesheuvel 	/*
82261bd93ceSArd Biesheuvel 	 * Make sure that the FDT region can be mapped without the need to
82361bd93ceSArd Biesheuvel 	 * allocate additional translation table pages, so that it is safe
824132233a7SLaura Abbott 	 * to call create_mapping_noalloc() this early.
82561bd93ceSArd Biesheuvel 	 *
82661bd93ceSArd Biesheuvel 	 * On 64k pages, the FDT will be mapped using PTEs, so we need to
82761bd93ceSArd Biesheuvel 	 * be in the same PMD as the rest of the fixmap.
82861bd93ceSArd Biesheuvel 	 * On 4k pages, we'll use section mappings for the FDT so we only
82961bd93ceSArd Biesheuvel 	 * have to be in the same PUD.
83061bd93ceSArd Biesheuvel 	 */
83161bd93ceSArd Biesheuvel 	BUILD_BUG_ON(dt_virt_base % SZ_2M);
83261bd93ceSArd Biesheuvel 
833b433dce0SSuzuki K. Poulose 	BUILD_BUG_ON(__fix_to_virt(FIX_FDT_END) >> SWAPPER_TABLE_SHIFT !=
834b433dce0SSuzuki K. Poulose 		     __fix_to_virt(FIX_BTMAP_BEGIN) >> SWAPPER_TABLE_SHIFT);
83561bd93ceSArd Biesheuvel 
836b433dce0SSuzuki K. Poulose 	offset = dt_phys % SWAPPER_BLOCK_SIZE;
83761bd93ceSArd Biesheuvel 	dt_virt = (void *)dt_virt_base + offset;
83861bd93ceSArd Biesheuvel 
83961bd93ceSArd Biesheuvel 	/* map the first chunk so we can read the size from the header */
840132233a7SLaura Abbott 	create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE),
841132233a7SLaura Abbott 			dt_virt_base, SWAPPER_BLOCK_SIZE, prot);
84261bd93ceSArd Biesheuvel 
84304a84810SArd Biesheuvel 	if (fdt_magic(dt_virt) != FDT_MAGIC)
84461bd93ceSArd Biesheuvel 		return NULL;
84561bd93ceSArd Biesheuvel 
846f80fb3a3SArd Biesheuvel 	*size = fdt_totalsize(dt_virt);
847f80fb3a3SArd Biesheuvel 	if (*size > MAX_FDT_SIZE)
84861bd93ceSArd Biesheuvel 		return NULL;
84961bd93ceSArd Biesheuvel 
850f80fb3a3SArd Biesheuvel 	if (offset + *size > SWAPPER_BLOCK_SIZE)
851132233a7SLaura Abbott 		create_mapping_noalloc(round_down(dt_phys, SWAPPER_BLOCK_SIZE), dt_virt_base,
852f80fb3a3SArd Biesheuvel 			       round_up(offset + *size, SWAPPER_BLOCK_SIZE), prot);
853f80fb3a3SArd Biesheuvel 
854f80fb3a3SArd Biesheuvel 	return dt_virt;
855f80fb3a3SArd Biesheuvel }
856f80fb3a3SArd Biesheuvel 
857f80fb3a3SArd Biesheuvel void *__init fixmap_remap_fdt(phys_addr_t dt_phys)
858f80fb3a3SArd Biesheuvel {
859f80fb3a3SArd Biesheuvel 	void *dt_virt;
860f80fb3a3SArd Biesheuvel 	int size;
861f80fb3a3SArd Biesheuvel 
862f80fb3a3SArd Biesheuvel 	dt_virt = __fixmap_remap_fdt(dt_phys, &size, PAGE_KERNEL_RO);
863f80fb3a3SArd Biesheuvel 	if (!dt_virt)
864f80fb3a3SArd Biesheuvel 		return NULL;
86561bd93ceSArd Biesheuvel 
86661bd93ceSArd Biesheuvel 	memblock_reserve(dt_phys, size);
86761bd93ceSArd Biesheuvel 	return dt_virt;
86861bd93ceSArd Biesheuvel }
869324420bfSArd Biesheuvel 
870324420bfSArd Biesheuvel int __init arch_ioremap_pud_supported(void)
871324420bfSArd Biesheuvel {
872324420bfSArd Biesheuvel 	/* only 4k granule supports level 1 block mappings */
873324420bfSArd Biesheuvel 	return IS_ENABLED(CONFIG_ARM64_4K_PAGES);
874324420bfSArd Biesheuvel }
875324420bfSArd Biesheuvel 
876324420bfSArd Biesheuvel int __init arch_ioremap_pmd_supported(void)
877324420bfSArd Biesheuvel {
878324420bfSArd Biesheuvel 	return 1;
879324420bfSArd Biesheuvel }
880324420bfSArd Biesheuvel 
881324420bfSArd Biesheuvel int pud_set_huge(pud_t *pud, phys_addr_t phys, pgprot_t prot)
882324420bfSArd Biesheuvel {
88319338304SKristina Martsenko 	pgprot_t sect_prot = __pgprot(PUD_TYPE_SECT |
88419338304SKristina Martsenko 					pgprot_val(mk_sect_prot(prot)));
885324420bfSArd Biesheuvel 	BUG_ON(phys & ~PUD_MASK);
88619338304SKristina Martsenko 	set_pud(pud, pfn_pud(__phys_to_pfn(phys), sect_prot));
887324420bfSArd Biesheuvel 	return 1;
888324420bfSArd Biesheuvel }
889324420bfSArd Biesheuvel 
890324420bfSArd Biesheuvel int pmd_set_huge(pmd_t *pmd, phys_addr_t phys, pgprot_t prot)
891324420bfSArd Biesheuvel {
89219338304SKristina Martsenko 	pgprot_t sect_prot = __pgprot(PMD_TYPE_SECT |
89319338304SKristina Martsenko 					pgprot_val(mk_sect_prot(prot)));
894324420bfSArd Biesheuvel 	BUG_ON(phys & ~PMD_MASK);
89519338304SKristina Martsenko 	set_pmd(pmd, pfn_pmd(__phys_to_pfn(phys), sect_prot));
896324420bfSArd Biesheuvel 	return 1;
897324420bfSArd Biesheuvel }
898324420bfSArd Biesheuvel 
899324420bfSArd Biesheuvel int pud_clear_huge(pud_t *pud)
900324420bfSArd Biesheuvel {
901324420bfSArd Biesheuvel 	if (!pud_sect(*pud))
902324420bfSArd Biesheuvel 		return 0;
903324420bfSArd Biesheuvel 	pud_clear(pud);
904324420bfSArd Biesheuvel 	return 1;
905324420bfSArd Biesheuvel }
906324420bfSArd Biesheuvel 
907324420bfSArd Biesheuvel int pmd_clear_huge(pmd_t *pmd)
908324420bfSArd Biesheuvel {
909324420bfSArd Biesheuvel 	if (!pmd_sect(*pmd))
910324420bfSArd Biesheuvel 		return 0;
911324420bfSArd Biesheuvel 	pmd_clear(pmd);
912324420bfSArd Biesheuvel 	return 1;
913324420bfSArd Biesheuvel }
914