xref: /openbmc/linux/arch/arm64/mm/mmu.c (revision c1cc1552616d0f354d040823151e61634e7ad01f)
1*c1cc1552SCatalin Marinas /*
2*c1cc1552SCatalin Marinas  * Based on arch/arm/mm/mmu.c
3*c1cc1552SCatalin Marinas  *
4*c1cc1552SCatalin Marinas  * Copyright (C) 1995-2005 Russell King
5*c1cc1552SCatalin Marinas  * Copyright (C) 2012 ARM Ltd.
6*c1cc1552SCatalin Marinas  *
7*c1cc1552SCatalin Marinas  * This program is free software; you can redistribute it and/or modify
8*c1cc1552SCatalin Marinas  * it under the terms of the GNU General Public License version 2 as
9*c1cc1552SCatalin Marinas  * published by the Free Software Foundation.
10*c1cc1552SCatalin Marinas  *
11*c1cc1552SCatalin Marinas  * This program is distributed in the hope that it will be useful,
12*c1cc1552SCatalin Marinas  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*c1cc1552SCatalin Marinas  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*c1cc1552SCatalin Marinas  * GNU General Public License for more details.
15*c1cc1552SCatalin Marinas  *
16*c1cc1552SCatalin Marinas  * You should have received a copy of the GNU General Public License
17*c1cc1552SCatalin Marinas  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18*c1cc1552SCatalin Marinas  */
19*c1cc1552SCatalin Marinas 
20*c1cc1552SCatalin Marinas #include <linux/export.h>
21*c1cc1552SCatalin Marinas #include <linux/kernel.h>
22*c1cc1552SCatalin Marinas #include <linux/errno.h>
23*c1cc1552SCatalin Marinas #include <linux/init.h>
24*c1cc1552SCatalin Marinas #include <linux/mman.h>
25*c1cc1552SCatalin Marinas #include <linux/nodemask.h>
26*c1cc1552SCatalin Marinas #include <linux/memblock.h>
27*c1cc1552SCatalin Marinas #include <linux/fs.h>
28*c1cc1552SCatalin Marinas 
29*c1cc1552SCatalin Marinas #include <asm/cputype.h>
30*c1cc1552SCatalin Marinas #include <asm/sections.h>
31*c1cc1552SCatalin Marinas #include <asm/setup.h>
32*c1cc1552SCatalin Marinas #include <asm/sizes.h>
33*c1cc1552SCatalin Marinas #include <asm/tlb.h>
34*c1cc1552SCatalin Marinas #include <asm/mmu_context.h>
35*c1cc1552SCatalin Marinas 
36*c1cc1552SCatalin Marinas #include "mm.h"
37*c1cc1552SCatalin Marinas 
38*c1cc1552SCatalin Marinas /*
39*c1cc1552SCatalin Marinas  * Empty_zero_page is a special page that is used for zero-initialized data
40*c1cc1552SCatalin Marinas  * and COW.
41*c1cc1552SCatalin Marinas  */
42*c1cc1552SCatalin Marinas struct page *empty_zero_page;
43*c1cc1552SCatalin Marinas EXPORT_SYMBOL(empty_zero_page);
44*c1cc1552SCatalin Marinas 
45*c1cc1552SCatalin Marinas pgprot_t pgprot_default;
46*c1cc1552SCatalin Marinas EXPORT_SYMBOL(pgprot_default);
47*c1cc1552SCatalin Marinas 
48*c1cc1552SCatalin Marinas static pmdval_t prot_sect_kernel;
49*c1cc1552SCatalin Marinas 
50*c1cc1552SCatalin Marinas struct cachepolicy {
51*c1cc1552SCatalin Marinas 	const char	policy[16];
52*c1cc1552SCatalin Marinas 	u64		mair;
53*c1cc1552SCatalin Marinas 	u64		tcr;
54*c1cc1552SCatalin Marinas };
55*c1cc1552SCatalin Marinas 
56*c1cc1552SCatalin Marinas static struct cachepolicy cache_policies[] __initdata = {
57*c1cc1552SCatalin Marinas 	{
58*c1cc1552SCatalin Marinas 		.policy		= "uncached",
59*c1cc1552SCatalin Marinas 		.mair		= 0x44,			/* inner, outer non-cacheable */
60*c1cc1552SCatalin Marinas 		.tcr		= TCR_IRGN_NC | TCR_ORGN_NC,
61*c1cc1552SCatalin Marinas 	}, {
62*c1cc1552SCatalin Marinas 		.policy		= "writethrough",
63*c1cc1552SCatalin Marinas 		.mair		= 0xaa,			/* inner, outer write-through, read-allocate */
64*c1cc1552SCatalin Marinas 		.tcr		= TCR_IRGN_WT | TCR_ORGN_WT,
65*c1cc1552SCatalin Marinas 	}, {
66*c1cc1552SCatalin Marinas 		.policy		= "writeback",
67*c1cc1552SCatalin Marinas 		.mair		= 0xee,			/* inner, outer write-back, read-allocate */
68*c1cc1552SCatalin Marinas 		.tcr		= TCR_IRGN_WBnWA | TCR_ORGN_WBnWA,
69*c1cc1552SCatalin Marinas 	}
70*c1cc1552SCatalin Marinas };
71*c1cc1552SCatalin Marinas 
72*c1cc1552SCatalin Marinas /*
73*c1cc1552SCatalin Marinas  * These are useful for identifying cache coherency problems by allowing the
74*c1cc1552SCatalin Marinas  * cache or the cache and writebuffer to be turned off. It changes the Normal
75*c1cc1552SCatalin Marinas  * memory caching attributes in the MAIR_EL1 register.
76*c1cc1552SCatalin Marinas  */
77*c1cc1552SCatalin Marinas static int __init early_cachepolicy(char *p)
78*c1cc1552SCatalin Marinas {
79*c1cc1552SCatalin Marinas 	int i;
80*c1cc1552SCatalin Marinas 	u64 tmp;
81*c1cc1552SCatalin Marinas 
82*c1cc1552SCatalin Marinas 	for (i = 0; i < ARRAY_SIZE(cache_policies); i++) {
83*c1cc1552SCatalin Marinas 		int len = strlen(cache_policies[i].policy);
84*c1cc1552SCatalin Marinas 
85*c1cc1552SCatalin Marinas 		if (memcmp(p, cache_policies[i].policy, len) == 0)
86*c1cc1552SCatalin Marinas 			break;
87*c1cc1552SCatalin Marinas 	}
88*c1cc1552SCatalin Marinas 	if (i == ARRAY_SIZE(cache_policies)) {
89*c1cc1552SCatalin Marinas 		pr_err("ERROR: unknown or unsupported cache policy: %s\n", p);
90*c1cc1552SCatalin Marinas 		return 0;
91*c1cc1552SCatalin Marinas 	}
92*c1cc1552SCatalin Marinas 
93*c1cc1552SCatalin Marinas 	flush_cache_all();
94*c1cc1552SCatalin Marinas 
95*c1cc1552SCatalin Marinas 	/*
96*c1cc1552SCatalin Marinas 	 * Modify MT_NORMAL attributes in MAIR_EL1.
97*c1cc1552SCatalin Marinas 	 */
98*c1cc1552SCatalin Marinas 	asm volatile(
99*c1cc1552SCatalin Marinas 	"	mrs	%0, mair_el1\n"
100*c1cc1552SCatalin Marinas 	"	bfi	%0, %1, #%2, #8\n"
101*c1cc1552SCatalin Marinas 	"	msr	mair_el1, %0\n"
102*c1cc1552SCatalin Marinas 	"	isb\n"
103*c1cc1552SCatalin Marinas 	: "=&r" (tmp)
104*c1cc1552SCatalin Marinas 	: "r" (cache_policies[i].mair), "i" (MT_NORMAL * 8));
105*c1cc1552SCatalin Marinas 
106*c1cc1552SCatalin Marinas 	/*
107*c1cc1552SCatalin Marinas 	 * Modify TCR PTW cacheability attributes.
108*c1cc1552SCatalin Marinas 	 */
109*c1cc1552SCatalin Marinas 	asm volatile(
110*c1cc1552SCatalin Marinas 	"	mrs	%0, tcr_el1\n"
111*c1cc1552SCatalin Marinas 	"	bic	%0, %0, %2\n"
112*c1cc1552SCatalin Marinas 	"	orr	%0, %0, %1\n"
113*c1cc1552SCatalin Marinas 	"	msr	tcr_el1, %0\n"
114*c1cc1552SCatalin Marinas 	"	isb\n"
115*c1cc1552SCatalin Marinas 	: "=&r" (tmp)
116*c1cc1552SCatalin Marinas 	: "r" (cache_policies[i].tcr), "r" (TCR_IRGN_MASK | TCR_ORGN_MASK));
117*c1cc1552SCatalin Marinas 
118*c1cc1552SCatalin Marinas 	flush_cache_all();
119*c1cc1552SCatalin Marinas 
120*c1cc1552SCatalin Marinas 	return 0;
121*c1cc1552SCatalin Marinas }
122*c1cc1552SCatalin Marinas early_param("cachepolicy", early_cachepolicy);
123*c1cc1552SCatalin Marinas 
124*c1cc1552SCatalin Marinas /*
125*c1cc1552SCatalin Marinas  * Adjust the PMD section entries according to the CPU in use.
126*c1cc1552SCatalin Marinas  */
127*c1cc1552SCatalin Marinas static void __init init_mem_pgprot(void)
128*c1cc1552SCatalin Marinas {
129*c1cc1552SCatalin Marinas 	pteval_t default_pgprot;
130*c1cc1552SCatalin Marinas 	int i;
131*c1cc1552SCatalin Marinas 
132*c1cc1552SCatalin Marinas 	default_pgprot = PTE_ATTRINDX(MT_NORMAL);
133*c1cc1552SCatalin Marinas 	prot_sect_kernel = PMD_TYPE_SECT | PMD_SECT_AF | PMD_ATTRINDX(MT_NORMAL);
134*c1cc1552SCatalin Marinas 
135*c1cc1552SCatalin Marinas #ifdef CONFIG_SMP
136*c1cc1552SCatalin Marinas 	/*
137*c1cc1552SCatalin Marinas 	 * Mark memory with the "shared" attribute for SMP systems
138*c1cc1552SCatalin Marinas 	 */
139*c1cc1552SCatalin Marinas 	default_pgprot |= PTE_SHARED;
140*c1cc1552SCatalin Marinas 	prot_sect_kernel |= PMD_SECT_S;
141*c1cc1552SCatalin Marinas #endif
142*c1cc1552SCatalin Marinas 
143*c1cc1552SCatalin Marinas 	for (i = 0; i < 16; i++) {
144*c1cc1552SCatalin Marinas 		unsigned long v = pgprot_val(protection_map[i]);
145*c1cc1552SCatalin Marinas 		protection_map[i] = __pgprot(v | default_pgprot);
146*c1cc1552SCatalin Marinas 	}
147*c1cc1552SCatalin Marinas 
148*c1cc1552SCatalin Marinas 	pgprot_default = __pgprot(PTE_TYPE_PAGE | PTE_AF | default_pgprot);
149*c1cc1552SCatalin Marinas }
150*c1cc1552SCatalin Marinas 
151*c1cc1552SCatalin Marinas pgprot_t phys_mem_access_prot(struct file *file, unsigned long pfn,
152*c1cc1552SCatalin Marinas 			      unsigned long size, pgprot_t vma_prot)
153*c1cc1552SCatalin Marinas {
154*c1cc1552SCatalin Marinas 	if (!pfn_valid(pfn))
155*c1cc1552SCatalin Marinas 		return pgprot_noncached(vma_prot);
156*c1cc1552SCatalin Marinas 	else if (file->f_flags & O_SYNC)
157*c1cc1552SCatalin Marinas 		return pgprot_writecombine(vma_prot);
158*c1cc1552SCatalin Marinas 	return vma_prot;
159*c1cc1552SCatalin Marinas }
160*c1cc1552SCatalin Marinas EXPORT_SYMBOL(phys_mem_access_prot);
161*c1cc1552SCatalin Marinas 
162*c1cc1552SCatalin Marinas static void __init *early_alloc(unsigned long sz)
163*c1cc1552SCatalin Marinas {
164*c1cc1552SCatalin Marinas 	void *ptr = __va(memblock_alloc(sz, sz));
165*c1cc1552SCatalin Marinas 	memset(ptr, 0, sz);
166*c1cc1552SCatalin Marinas 	return ptr;
167*c1cc1552SCatalin Marinas }
168*c1cc1552SCatalin Marinas 
169*c1cc1552SCatalin Marinas static void __init alloc_init_pte(pmd_t *pmd, unsigned long addr,
170*c1cc1552SCatalin Marinas 				  unsigned long end, unsigned long pfn)
171*c1cc1552SCatalin Marinas {
172*c1cc1552SCatalin Marinas 	pte_t *pte;
173*c1cc1552SCatalin Marinas 
174*c1cc1552SCatalin Marinas 	if (pmd_none(*pmd)) {
175*c1cc1552SCatalin Marinas 		pte = early_alloc(PTRS_PER_PTE * sizeof(pte_t));
176*c1cc1552SCatalin Marinas 		__pmd_populate(pmd, __pa(pte), PMD_TYPE_TABLE);
177*c1cc1552SCatalin Marinas 	}
178*c1cc1552SCatalin Marinas 	BUG_ON(pmd_bad(*pmd));
179*c1cc1552SCatalin Marinas 
180*c1cc1552SCatalin Marinas 	pte = pte_offset_kernel(pmd, addr);
181*c1cc1552SCatalin Marinas 	do {
182*c1cc1552SCatalin Marinas 		set_pte(pte, pfn_pte(pfn, PAGE_KERNEL_EXEC));
183*c1cc1552SCatalin Marinas 		pfn++;
184*c1cc1552SCatalin Marinas 	} while (pte++, addr += PAGE_SIZE, addr != end);
185*c1cc1552SCatalin Marinas }
186*c1cc1552SCatalin Marinas 
187*c1cc1552SCatalin Marinas static void __init alloc_init_pmd(pud_t *pud, unsigned long addr,
188*c1cc1552SCatalin Marinas 				  unsigned long end, phys_addr_t phys)
189*c1cc1552SCatalin Marinas {
190*c1cc1552SCatalin Marinas 	pmd_t *pmd;
191*c1cc1552SCatalin Marinas 	unsigned long next;
192*c1cc1552SCatalin Marinas 
193*c1cc1552SCatalin Marinas 	/*
194*c1cc1552SCatalin Marinas 	 * Check for initial section mappings in the pgd/pud and remove them.
195*c1cc1552SCatalin Marinas 	 */
196*c1cc1552SCatalin Marinas 	if (pud_none(*pud) || pud_bad(*pud)) {
197*c1cc1552SCatalin Marinas 		pmd = early_alloc(PTRS_PER_PMD * sizeof(pmd_t));
198*c1cc1552SCatalin Marinas 		pud_populate(&init_mm, pud, pmd);
199*c1cc1552SCatalin Marinas 	}
200*c1cc1552SCatalin Marinas 
201*c1cc1552SCatalin Marinas 	pmd = pmd_offset(pud, addr);
202*c1cc1552SCatalin Marinas 	do {
203*c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
204*c1cc1552SCatalin Marinas 		/* try section mapping first */
205*c1cc1552SCatalin Marinas 		if (((addr | next | phys) & ~SECTION_MASK) == 0)
206*c1cc1552SCatalin Marinas 			set_pmd(pmd, __pmd(phys | prot_sect_kernel));
207*c1cc1552SCatalin Marinas 		else
208*c1cc1552SCatalin Marinas 			alloc_init_pte(pmd, addr, next, __phys_to_pfn(phys));
209*c1cc1552SCatalin Marinas 		phys += next - addr;
210*c1cc1552SCatalin Marinas 	} while (pmd++, addr = next, addr != end);
211*c1cc1552SCatalin Marinas }
212*c1cc1552SCatalin Marinas 
213*c1cc1552SCatalin Marinas static void __init alloc_init_pud(pgd_t *pgd, unsigned long addr,
214*c1cc1552SCatalin Marinas 				  unsigned long end, unsigned long phys)
215*c1cc1552SCatalin Marinas {
216*c1cc1552SCatalin Marinas 	pud_t *pud = pud_offset(pgd, addr);
217*c1cc1552SCatalin Marinas 	unsigned long next;
218*c1cc1552SCatalin Marinas 
219*c1cc1552SCatalin Marinas 	do {
220*c1cc1552SCatalin Marinas 		next = pud_addr_end(addr, end);
221*c1cc1552SCatalin Marinas 		alloc_init_pmd(pud, addr, next, phys);
222*c1cc1552SCatalin Marinas 		phys += next - addr;
223*c1cc1552SCatalin Marinas 	} while (pud++, addr = next, addr != end);
224*c1cc1552SCatalin Marinas }
225*c1cc1552SCatalin Marinas 
226*c1cc1552SCatalin Marinas /*
227*c1cc1552SCatalin Marinas  * Create the page directory entries and any necessary page tables for the
228*c1cc1552SCatalin Marinas  * mapping specified by 'md'.
229*c1cc1552SCatalin Marinas  */
230*c1cc1552SCatalin Marinas static void __init create_mapping(phys_addr_t phys, unsigned long virt,
231*c1cc1552SCatalin Marinas 				  phys_addr_t size)
232*c1cc1552SCatalin Marinas {
233*c1cc1552SCatalin Marinas 	unsigned long addr, length, end, next;
234*c1cc1552SCatalin Marinas 	pgd_t *pgd;
235*c1cc1552SCatalin Marinas 
236*c1cc1552SCatalin Marinas 	if (virt < VMALLOC_START) {
237*c1cc1552SCatalin Marinas 		pr_warning("BUG: not creating mapping for 0x%016llx at 0x%016lx - outside kernel range\n",
238*c1cc1552SCatalin Marinas 			   phys, virt);
239*c1cc1552SCatalin Marinas 		return;
240*c1cc1552SCatalin Marinas 	}
241*c1cc1552SCatalin Marinas 
242*c1cc1552SCatalin Marinas 	addr = virt & PAGE_MASK;
243*c1cc1552SCatalin Marinas 	length = PAGE_ALIGN(size + (virt & ~PAGE_MASK));
244*c1cc1552SCatalin Marinas 
245*c1cc1552SCatalin Marinas 	pgd = pgd_offset_k(addr);
246*c1cc1552SCatalin Marinas 	end = addr + length;
247*c1cc1552SCatalin Marinas 	do {
248*c1cc1552SCatalin Marinas 		next = pgd_addr_end(addr, end);
249*c1cc1552SCatalin Marinas 		alloc_init_pud(pgd, addr, next, phys);
250*c1cc1552SCatalin Marinas 		phys += next - addr;
251*c1cc1552SCatalin Marinas 	} while (pgd++, addr = next, addr != end);
252*c1cc1552SCatalin Marinas }
253*c1cc1552SCatalin Marinas 
254*c1cc1552SCatalin Marinas static void __init map_mem(void)
255*c1cc1552SCatalin Marinas {
256*c1cc1552SCatalin Marinas 	struct memblock_region *reg;
257*c1cc1552SCatalin Marinas 
258*c1cc1552SCatalin Marinas 	/* map all the memory banks */
259*c1cc1552SCatalin Marinas 	for_each_memblock(memory, reg) {
260*c1cc1552SCatalin Marinas 		phys_addr_t start = reg->base;
261*c1cc1552SCatalin Marinas 		phys_addr_t end = start + reg->size;
262*c1cc1552SCatalin Marinas 
263*c1cc1552SCatalin Marinas 		if (start >= end)
264*c1cc1552SCatalin Marinas 			break;
265*c1cc1552SCatalin Marinas 
266*c1cc1552SCatalin Marinas 		create_mapping(start, __phys_to_virt(start), end - start);
267*c1cc1552SCatalin Marinas 	}
268*c1cc1552SCatalin Marinas }
269*c1cc1552SCatalin Marinas 
270*c1cc1552SCatalin Marinas /*
271*c1cc1552SCatalin Marinas  * paging_init() sets up the page tables, initialises the zone memory
272*c1cc1552SCatalin Marinas  * maps and sets up the zero page.
273*c1cc1552SCatalin Marinas  */
274*c1cc1552SCatalin Marinas void __init paging_init(void)
275*c1cc1552SCatalin Marinas {
276*c1cc1552SCatalin Marinas 	void *zero_page;
277*c1cc1552SCatalin Marinas 
278*c1cc1552SCatalin Marinas 	/*
279*c1cc1552SCatalin Marinas 	 * Maximum PGDIR_SIZE addressable via the initial direct kernel
280*c1cc1552SCatalin Marinas 	 * mapping in swapper_pg_dir.
281*c1cc1552SCatalin Marinas 	 */
282*c1cc1552SCatalin Marinas 	memblock_set_current_limit((PHYS_OFFSET & PGDIR_MASK) + PGDIR_SIZE);
283*c1cc1552SCatalin Marinas 
284*c1cc1552SCatalin Marinas 	init_mem_pgprot();
285*c1cc1552SCatalin Marinas 	map_mem();
286*c1cc1552SCatalin Marinas 
287*c1cc1552SCatalin Marinas 	/*
288*c1cc1552SCatalin Marinas 	 * Finally flush the caches and tlb to ensure that we're in a
289*c1cc1552SCatalin Marinas 	 * consistent state.
290*c1cc1552SCatalin Marinas 	 */
291*c1cc1552SCatalin Marinas 	flush_cache_all();
292*c1cc1552SCatalin Marinas 	flush_tlb_all();
293*c1cc1552SCatalin Marinas 
294*c1cc1552SCatalin Marinas 	/* allocate the zero page. */
295*c1cc1552SCatalin Marinas 	zero_page = early_alloc(PAGE_SIZE);
296*c1cc1552SCatalin Marinas 
297*c1cc1552SCatalin Marinas 	bootmem_init();
298*c1cc1552SCatalin Marinas 
299*c1cc1552SCatalin Marinas 	empty_zero_page = virt_to_page(zero_page);
300*c1cc1552SCatalin Marinas 	__flush_dcache_page(empty_zero_page);
301*c1cc1552SCatalin Marinas 
302*c1cc1552SCatalin Marinas 	/*
303*c1cc1552SCatalin Marinas 	 * TTBR0 is only used for the identity mapping at this stage. Make it
304*c1cc1552SCatalin Marinas 	 * point to zero page to avoid speculatively fetching new entries.
305*c1cc1552SCatalin Marinas 	 */
306*c1cc1552SCatalin Marinas 	cpu_set_reserved_ttbr0();
307*c1cc1552SCatalin Marinas 	flush_tlb_all();
308*c1cc1552SCatalin Marinas }
309*c1cc1552SCatalin Marinas 
310*c1cc1552SCatalin Marinas /*
311*c1cc1552SCatalin Marinas  * Enable the identity mapping to allow the MMU disabling.
312*c1cc1552SCatalin Marinas  */
313*c1cc1552SCatalin Marinas void setup_mm_for_reboot(void)
314*c1cc1552SCatalin Marinas {
315*c1cc1552SCatalin Marinas 	cpu_switch_mm(idmap_pg_dir, &init_mm);
316*c1cc1552SCatalin Marinas 	flush_tlb_all();
317*c1cc1552SCatalin Marinas }
318*c1cc1552SCatalin Marinas 
319*c1cc1552SCatalin Marinas /*
320*c1cc1552SCatalin Marinas  * Check whether a kernel address is valid (derived from arch/x86/).
321*c1cc1552SCatalin Marinas  */
322*c1cc1552SCatalin Marinas int kern_addr_valid(unsigned long addr)
323*c1cc1552SCatalin Marinas {
324*c1cc1552SCatalin Marinas 	pgd_t *pgd;
325*c1cc1552SCatalin Marinas 	pud_t *pud;
326*c1cc1552SCatalin Marinas 	pmd_t *pmd;
327*c1cc1552SCatalin Marinas 	pte_t *pte;
328*c1cc1552SCatalin Marinas 
329*c1cc1552SCatalin Marinas 	if ((((long)addr) >> VA_BITS) != -1UL)
330*c1cc1552SCatalin Marinas 		return 0;
331*c1cc1552SCatalin Marinas 
332*c1cc1552SCatalin Marinas 	pgd = pgd_offset_k(addr);
333*c1cc1552SCatalin Marinas 	if (pgd_none(*pgd))
334*c1cc1552SCatalin Marinas 		return 0;
335*c1cc1552SCatalin Marinas 
336*c1cc1552SCatalin Marinas 	pud = pud_offset(pgd, addr);
337*c1cc1552SCatalin Marinas 	if (pud_none(*pud))
338*c1cc1552SCatalin Marinas 		return 0;
339*c1cc1552SCatalin Marinas 
340*c1cc1552SCatalin Marinas 	pmd = pmd_offset(pud, addr);
341*c1cc1552SCatalin Marinas 	if (pmd_none(*pmd))
342*c1cc1552SCatalin Marinas 		return 0;
343*c1cc1552SCatalin Marinas 
344*c1cc1552SCatalin Marinas 	pte = pte_offset_kernel(pmd, addr);
345*c1cc1552SCatalin Marinas 	if (pte_none(*pte))
346*c1cc1552SCatalin Marinas 		return 0;
347*c1cc1552SCatalin Marinas 
348*c1cc1552SCatalin Marinas 	return pfn_valid(pte_pfn(*pte));
349*c1cc1552SCatalin Marinas }
350*c1cc1552SCatalin Marinas #ifdef CONFIG_SPARSEMEM_VMEMMAP
351*c1cc1552SCatalin Marinas #ifdef CONFIG_ARM64_64K_PAGES
352*c1cc1552SCatalin Marinas int __meminit vmemmap_populate(struct page *start_page,
353*c1cc1552SCatalin Marinas 			       unsigned long size, int node)
354*c1cc1552SCatalin Marinas {
355*c1cc1552SCatalin Marinas 	return vmemmap_populate_basepages(start_page, size, node);
356*c1cc1552SCatalin Marinas }
357*c1cc1552SCatalin Marinas #else	/* !CONFIG_ARM64_64K_PAGES */
358*c1cc1552SCatalin Marinas int __meminit vmemmap_populate(struct page *start_page,
359*c1cc1552SCatalin Marinas 			       unsigned long size, int node)
360*c1cc1552SCatalin Marinas {
361*c1cc1552SCatalin Marinas 	unsigned long addr = (unsigned long)start_page;
362*c1cc1552SCatalin Marinas 	unsigned long end = (unsigned long)(start_page + size);
363*c1cc1552SCatalin Marinas 	unsigned long next;
364*c1cc1552SCatalin Marinas 	pgd_t *pgd;
365*c1cc1552SCatalin Marinas 	pud_t *pud;
366*c1cc1552SCatalin Marinas 	pmd_t *pmd;
367*c1cc1552SCatalin Marinas 
368*c1cc1552SCatalin Marinas 	do {
369*c1cc1552SCatalin Marinas 		next = pmd_addr_end(addr, end);
370*c1cc1552SCatalin Marinas 
371*c1cc1552SCatalin Marinas 		pgd = vmemmap_pgd_populate(addr, node);
372*c1cc1552SCatalin Marinas 		if (!pgd)
373*c1cc1552SCatalin Marinas 			return -ENOMEM;
374*c1cc1552SCatalin Marinas 
375*c1cc1552SCatalin Marinas 		pud = vmemmap_pud_populate(pgd, addr, node);
376*c1cc1552SCatalin Marinas 		if (!pud)
377*c1cc1552SCatalin Marinas 			return -ENOMEM;
378*c1cc1552SCatalin Marinas 
379*c1cc1552SCatalin Marinas 		pmd = pmd_offset(pud, addr);
380*c1cc1552SCatalin Marinas 		if (pmd_none(*pmd)) {
381*c1cc1552SCatalin Marinas 			void *p = NULL;
382*c1cc1552SCatalin Marinas 
383*c1cc1552SCatalin Marinas 			p = vmemmap_alloc_block_buf(PMD_SIZE, node);
384*c1cc1552SCatalin Marinas 			if (!p)
385*c1cc1552SCatalin Marinas 				return -ENOMEM;
386*c1cc1552SCatalin Marinas 
387*c1cc1552SCatalin Marinas 			set_pmd(pmd, __pmd(__pa(p) | prot_sect_kernel));
388*c1cc1552SCatalin Marinas 		} else
389*c1cc1552SCatalin Marinas 			vmemmap_verify((pte_t *)pmd, node, addr, next);
390*c1cc1552SCatalin Marinas 	} while (addr = next, addr != end);
391*c1cc1552SCatalin Marinas 
392*c1cc1552SCatalin Marinas 	return 0;
393*c1cc1552SCatalin Marinas }
394*c1cc1552SCatalin Marinas #endif	/* CONFIG_ARM64_64K_PAGES */
395*c1cc1552SCatalin Marinas #endif	/* CONFIG_SPARSEMEM_VMEMMAP */
396