xref: /openbmc/linux/arch/riscv/mm/init.c (revision 6262f661ff5d7d6a2613b95d0b7820c60b46b0b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2012 Regents of the University of California
4  * Copyright (C) 2019 Western Digital Corporation or its affiliates.
5  */
6 
7 #include <linux/init.h>
8 #include <linux/mm.h>
9 #include <linux/memblock.h>
10 #include <linux/initrd.h>
11 #include <linux/swap.h>
12 #include <linux/sizes.h>
13 #include <linux/of_fdt.h>
14 #include <linux/libfdt.h>
15 #include <linux/set_memory.h>
16 
17 #include <asm/fixmap.h>
18 #include <asm/tlbflush.h>
19 #include <asm/sections.h>
20 #include <asm/soc.h>
21 #include <asm/io.h>
22 #include <asm/ptdump.h>
23 
24 #include "../kernel/head.h"
25 
26 unsigned long empty_zero_page[PAGE_SIZE / sizeof(unsigned long)]
27 							__page_aligned_bss;
28 EXPORT_SYMBOL(empty_zero_page);
29 
30 extern char _start[];
31 #define DTB_EARLY_BASE_VA      PGDIR_SIZE
32 void *dtb_early_va __initdata;
33 uintptr_t dtb_early_pa __initdata;
34 
35 static void __init zone_sizes_init(void)
36 {
37 	unsigned long max_zone_pfns[MAX_NR_ZONES] = { 0, };
38 
39 #ifdef CONFIG_ZONE_DMA32
40 	max_zone_pfns[ZONE_DMA32] = PFN_DOWN(min(4UL * SZ_1G,
41 			(unsigned long) PFN_PHYS(max_low_pfn)));
42 #endif
43 	max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
44 
45 	free_area_init(max_zone_pfns);
46 }
47 
48 static void setup_zero_page(void)
49 {
50 	memset((void *)empty_zero_page, 0, PAGE_SIZE);
51 }
52 
53 #if defined(CONFIG_MMU) && defined(CONFIG_DEBUG_VM)
54 static inline void print_mlk(char *name, unsigned long b, unsigned long t)
55 {
56 	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld kB)\n", name, b, t,
57 		  (((t) - (b)) >> 10));
58 }
59 
60 static inline void print_mlm(char *name, unsigned long b, unsigned long t)
61 {
62 	pr_notice("%12s : 0x%08lx - 0x%08lx   (%4ld MB)\n", name, b, t,
63 		  (((t) - (b)) >> 20));
64 }
65 
66 static void print_vm_layout(void)
67 {
68 	pr_notice("Virtual kernel memory layout:\n");
69 	print_mlk("fixmap", (unsigned long)FIXADDR_START,
70 		  (unsigned long)FIXADDR_TOP);
71 	print_mlm("pci io", (unsigned long)PCI_IO_START,
72 		  (unsigned long)PCI_IO_END);
73 	print_mlm("vmemmap", (unsigned long)VMEMMAP_START,
74 		  (unsigned long)VMEMMAP_END);
75 	print_mlm("vmalloc", (unsigned long)VMALLOC_START,
76 		  (unsigned long)VMALLOC_END);
77 	print_mlm("lowmem", (unsigned long)PAGE_OFFSET,
78 		  (unsigned long)high_memory);
79 }
80 #else
81 static void print_vm_layout(void) { }
82 #endif /* CONFIG_DEBUG_VM */
83 
84 void __init mem_init(void)
85 {
86 #ifdef CONFIG_FLATMEM
87 	BUG_ON(!mem_map);
88 #endif /* CONFIG_FLATMEM */
89 
90 	high_memory = (void *)(__va(PFN_PHYS(max_low_pfn)));
91 	memblock_free_all();
92 
93 	mem_init_print_info(NULL);
94 	print_vm_layout();
95 }
96 
97 #ifdef CONFIG_BLK_DEV_INITRD
98 static void __init setup_initrd(void)
99 {
100 	phys_addr_t start;
101 	unsigned long size;
102 
103 	/* Ignore the virtul address computed during device tree parsing */
104 	initrd_start = initrd_end = 0;
105 
106 	if (!phys_initrd_size)
107 		return;
108 	/*
109 	 * Round the memory region to page boundaries as per free_initrd_mem()
110 	 * This allows us to detect whether the pages overlapping the initrd
111 	 * are in use, but more importantly, reserves the entire set of pages
112 	 * as we don't want these pages allocated for other purposes.
113 	 */
114 	start = round_down(phys_initrd_start, PAGE_SIZE);
115 	size = phys_initrd_size + (phys_initrd_start - start);
116 	size = round_up(size, PAGE_SIZE);
117 
118 	if (!memblock_is_region_memory(start, size)) {
119 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region",
120 		       (u64)start, size);
121 		goto disable;
122 	}
123 
124 	if (memblock_is_region_reserved(start, size)) {
125 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n",
126 		       (u64)start, size);
127 		goto disable;
128 	}
129 
130 	memblock_reserve(start, size);
131 	/* Now convert initrd to virtual addresses */
132 	initrd_start = (unsigned long)__va(phys_initrd_start);
133 	initrd_end = initrd_start + phys_initrd_size;
134 	initrd_below_start_ok = 1;
135 
136 	pr_info("Initial ramdisk at: 0x%p (%lu bytes)\n",
137 		(void *)(initrd_start), size);
138 	return;
139 disable:
140 	pr_cont(" - disabling initrd\n");
141 	initrd_start = 0;
142 	initrd_end = 0;
143 }
144 #endif /* CONFIG_BLK_DEV_INITRD */
145 
146 void __init setup_bootmem(void)
147 {
148 	struct memblock_region *reg;
149 	phys_addr_t mem_size = 0;
150 	phys_addr_t total_mem = 0;
151 	phys_addr_t mem_start, end = 0;
152 	phys_addr_t vmlinux_end = __pa_symbol(&_end);
153 	phys_addr_t vmlinux_start = __pa_symbol(&_start);
154 
155 	/* Find the memory region containing the kernel */
156 	for_each_memblock(memory, reg) {
157 		end = reg->base + reg->size;
158 		if (!total_mem)
159 			mem_start = reg->base;
160 		if (reg->base <= vmlinux_start && vmlinux_end <= end)
161 			BUG_ON(reg->size == 0);
162 		total_mem = total_mem + reg->size;
163 	}
164 
165 	/*
166 	 * Remove memblock from the end of usable area to the
167 	 * end of region
168 	 */
169 	mem_size = min(total_mem, (phys_addr_t)-PAGE_OFFSET);
170 	if (mem_start + mem_size < end)
171 		memblock_remove(mem_start + mem_size,
172 				end - mem_start - mem_size);
173 
174 	/* Reserve from the start of the kernel to the end of the kernel */
175 	memblock_reserve(vmlinux_start, vmlinux_end - vmlinux_start);
176 
177 	max_pfn = PFN_DOWN(memblock_end_of_DRAM());
178 	max_low_pfn = max_pfn;
179 	set_max_mapnr(max_low_pfn);
180 
181 #ifdef CONFIG_BLK_DEV_INITRD
182 	setup_initrd();
183 #endif /* CONFIG_BLK_DEV_INITRD */
184 
185 	/*
186 	 * Avoid using early_init_fdt_reserve_self() since __pa() does
187 	 * not work for DTB pointers that are fixmap addresses
188 	 */
189 	memblock_reserve(dtb_early_pa, fdt_totalsize(dtb_early_va));
190 
191 	early_init_fdt_scan_reserved_mem();
192 	memblock_allow_resize();
193 	memblock_dump_all();
194 
195 	for_each_memblock(memory, reg) {
196 		unsigned long start_pfn = memblock_region_memory_base_pfn(reg);
197 		unsigned long end_pfn = memblock_region_memory_end_pfn(reg);
198 
199 		memblock_set_node(PFN_PHYS(start_pfn),
200 				  PFN_PHYS(end_pfn - start_pfn),
201 				  &memblock.memory, 0);
202 	}
203 }
204 
205 #ifdef CONFIG_MMU
206 unsigned long va_pa_offset;
207 EXPORT_SYMBOL(va_pa_offset);
208 unsigned long pfn_base;
209 EXPORT_SYMBOL(pfn_base);
210 
211 pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
212 pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss;
213 pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss;
214 static bool mmu_enabled;
215 
216 #define MAX_EARLY_MAPPING_SIZE	SZ_128M
217 
218 pgd_t early_pg_dir[PTRS_PER_PGD] __initdata __aligned(PAGE_SIZE);
219 
220 void __set_fixmap(enum fixed_addresses idx, phys_addr_t phys, pgprot_t prot)
221 {
222 	unsigned long addr = __fix_to_virt(idx);
223 	pte_t *ptep;
224 
225 	BUG_ON(idx <= FIX_HOLE || idx >= __end_of_fixed_addresses);
226 
227 	ptep = &fixmap_pte[pte_index(addr)];
228 
229 	if (pgprot_val(prot)) {
230 		set_pte(ptep, pfn_pte(phys >> PAGE_SHIFT, prot));
231 	} else {
232 		pte_clear(&init_mm, addr, ptep);
233 		local_flush_tlb_page(addr);
234 	}
235 }
236 
237 static pte_t *__init get_pte_virt(phys_addr_t pa)
238 {
239 	if (mmu_enabled) {
240 		clear_fixmap(FIX_PTE);
241 		return (pte_t *)set_fixmap_offset(FIX_PTE, pa);
242 	} else {
243 		return (pte_t *)((uintptr_t)pa);
244 	}
245 }
246 
247 static phys_addr_t __init alloc_pte(uintptr_t va)
248 {
249 	/*
250 	 * We only create PMD or PGD early mappings so we
251 	 * should never reach here with MMU disabled.
252 	 */
253 	BUG_ON(!mmu_enabled);
254 
255 	return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
256 }
257 
258 static void __init create_pte_mapping(pte_t *ptep,
259 				      uintptr_t va, phys_addr_t pa,
260 				      phys_addr_t sz, pgprot_t prot)
261 {
262 	uintptr_t pte_idx = pte_index(va);
263 
264 	BUG_ON(sz != PAGE_SIZE);
265 
266 	if (pte_none(ptep[pte_idx]))
267 		ptep[pte_idx] = pfn_pte(PFN_DOWN(pa), prot);
268 }
269 
270 #ifndef __PAGETABLE_PMD_FOLDED
271 
272 pmd_t trampoline_pmd[PTRS_PER_PMD] __page_aligned_bss;
273 pmd_t fixmap_pmd[PTRS_PER_PMD] __page_aligned_bss;
274 
275 #if MAX_EARLY_MAPPING_SIZE < PGDIR_SIZE
276 #define NUM_EARLY_PMDS		1UL
277 #else
278 #define NUM_EARLY_PMDS		(1UL + MAX_EARLY_MAPPING_SIZE / PGDIR_SIZE)
279 #endif
280 pmd_t early_pmd[PTRS_PER_PMD * NUM_EARLY_PMDS] __initdata __aligned(PAGE_SIZE);
281 
282 static pmd_t *__init get_pmd_virt(phys_addr_t pa)
283 {
284 	if (mmu_enabled) {
285 		clear_fixmap(FIX_PMD);
286 		return (pmd_t *)set_fixmap_offset(FIX_PMD, pa);
287 	} else {
288 		return (pmd_t *)((uintptr_t)pa);
289 	}
290 }
291 
292 static phys_addr_t __init alloc_pmd(uintptr_t va)
293 {
294 	uintptr_t pmd_num;
295 
296 	if (mmu_enabled)
297 		return memblock_phys_alloc(PAGE_SIZE, PAGE_SIZE);
298 
299 	pmd_num = (va - PAGE_OFFSET) >> PGDIR_SHIFT;
300 	BUG_ON(pmd_num >= NUM_EARLY_PMDS);
301 	return (uintptr_t)&early_pmd[pmd_num * PTRS_PER_PMD];
302 }
303 
304 static void __init create_pmd_mapping(pmd_t *pmdp,
305 				      uintptr_t va, phys_addr_t pa,
306 				      phys_addr_t sz, pgprot_t prot)
307 {
308 	pte_t *ptep;
309 	phys_addr_t pte_phys;
310 	uintptr_t pmd_idx = pmd_index(va);
311 
312 	if (sz == PMD_SIZE) {
313 		if (pmd_none(pmdp[pmd_idx]))
314 			pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pa), prot);
315 		return;
316 	}
317 
318 	if (pmd_none(pmdp[pmd_idx])) {
319 		pte_phys = alloc_pte(va);
320 		pmdp[pmd_idx] = pfn_pmd(PFN_DOWN(pte_phys), PAGE_TABLE);
321 		ptep = get_pte_virt(pte_phys);
322 		memset(ptep, 0, PAGE_SIZE);
323 	} else {
324 		pte_phys = PFN_PHYS(_pmd_pfn(pmdp[pmd_idx]));
325 		ptep = get_pte_virt(pte_phys);
326 	}
327 
328 	create_pte_mapping(ptep, va, pa, sz, prot);
329 }
330 
331 #define pgd_next_t		pmd_t
332 #define alloc_pgd_next(__va)	alloc_pmd(__va)
333 #define get_pgd_next_virt(__pa)	get_pmd_virt(__pa)
334 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
335 	create_pmd_mapping(__nextp, __va, __pa, __sz, __prot)
336 #define fixmap_pgd_next		fixmap_pmd
337 #else
338 #define pgd_next_t		pte_t
339 #define alloc_pgd_next(__va)	alloc_pte(__va)
340 #define get_pgd_next_virt(__pa)	get_pte_virt(__pa)
341 #define create_pgd_next_mapping(__nextp, __va, __pa, __sz, __prot)	\
342 	create_pte_mapping(__nextp, __va, __pa, __sz, __prot)
343 #define fixmap_pgd_next		fixmap_pte
344 #endif
345 
346 static void __init create_pgd_mapping(pgd_t *pgdp,
347 				      uintptr_t va, phys_addr_t pa,
348 				      phys_addr_t sz, pgprot_t prot)
349 {
350 	pgd_next_t *nextp;
351 	phys_addr_t next_phys;
352 	uintptr_t pgd_idx = pgd_index(va);
353 
354 	if (sz == PGDIR_SIZE) {
355 		if (pgd_val(pgdp[pgd_idx]) == 0)
356 			pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(pa), prot);
357 		return;
358 	}
359 
360 	if (pgd_val(pgdp[pgd_idx]) == 0) {
361 		next_phys = alloc_pgd_next(va);
362 		pgdp[pgd_idx] = pfn_pgd(PFN_DOWN(next_phys), PAGE_TABLE);
363 		nextp = get_pgd_next_virt(next_phys);
364 		memset(nextp, 0, PAGE_SIZE);
365 	} else {
366 		next_phys = PFN_PHYS(_pgd_pfn(pgdp[pgd_idx]));
367 		nextp = get_pgd_next_virt(next_phys);
368 	}
369 
370 	create_pgd_next_mapping(nextp, va, pa, sz, prot);
371 }
372 
373 static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size)
374 {
375 	/* Upgrade to PMD_SIZE mappings whenever possible */
376 	if ((base & (PMD_SIZE - 1)) || (size & (PMD_SIZE - 1)))
377 		return PAGE_SIZE;
378 
379 	return PMD_SIZE;
380 }
381 
382 /*
383  * setup_vm() is called from head.S with MMU-off.
384  *
385  * Following requirements should be honoured for setup_vm() to work
386  * correctly:
387  * 1) It should use PC-relative addressing for accessing kernel symbols.
388  *    To achieve this we always use GCC cmodel=medany.
389  * 2) The compiler instrumentation for FTRACE will not work for setup_vm()
390  *    so disable compiler instrumentation when FTRACE is enabled.
391  *
392  * Currently, the above requirements are honoured by using custom CFLAGS
393  * for init.o in mm/Makefile.
394  */
395 
396 #ifndef __riscv_cmodel_medany
397 #error "setup_vm() is called from head.S before relocate so it should not use absolute addressing."
398 #endif
399 
400 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
401 {
402 	uintptr_t va, pa, end_va;
403 	uintptr_t load_pa = (uintptr_t)(&_start);
404 	uintptr_t load_sz = (uintptr_t)(&_end) - load_pa;
405 	uintptr_t map_size = best_map_size(load_pa, MAX_EARLY_MAPPING_SIZE);
406 #ifndef __PAGETABLE_PMD_FOLDED
407 	pmd_t fix_bmap_spmd, fix_bmap_epmd;
408 #endif
409 
410 	va_pa_offset = PAGE_OFFSET - load_pa;
411 	pfn_base = PFN_DOWN(load_pa);
412 
413 	/*
414 	 * Enforce boot alignment requirements of RV32 and
415 	 * RV64 by only allowing PMD or PGD mappings.
416 	 */
417 	BUG_ON(map_size == PAGE_SIZE);
418 
419 	/* Sanity check alignment and size */
420 	BUG_ON((PAGE_OFFSET % PGDIR_SIZE) != 0);
421 	BUG_ON((load_pa % map_size) != 0);
422 	BUG_ON(load_sz > MAX_EARLY_MAPPING_SIZE);
423 
424 	/* Setup early PGD for fixmap */
425 	create_pgd_mapping(early_pg_dir, FIXADDR_START,
426 			   (uintptr_t)fixmap_pgd_next, PGDIR_SIZE, PAGE_TABLE);
427 
428 #ifndef __PAGETABLE_PMD_FOLDED
429 	/* Setup fixmap PMD */
430 	create_pmd_mapping(fixmap_pmd, FIXADDR_START,
431 			   (uintptr_t)fixmap_pte, PMD_SIZE, PAGE_TABLE);
432 	/* Setup trampoline PGD and PMD */
433 	create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
434 			   (uintptr_t)trampoline_pmd, PGDIR_SIZE, PAGE_TABLE);
435 	create_pmd_mapping(trampoline_pmd, PAGE_OFFSET,
436 			   load_pa, PMD_SIZE, PAGE_KERNEL_EXEC);
437 #else
438 	/* Setup trampoline PGD */
439 	create_pgd_mapping(trampoline_pg_dir, PAGE_OFFSET,
440 			   load_pa, PGDIR_SIZE, PAGE_KERNEL_EXEC);
441 #endif
442 
443 	/*
444 	 * Setup early PGD covering entire kernel which will allows
445 	 * us to reach paging_init(). We map all memory banks later
446 	 * in setup_vm_final() below.
447 	 */
448 	end_va = PAGE_OFFSET + load_sz;
449 	for (va = PAGE_OFFSET; va < end_va; va += map_size)
450 		create_pgd_mapping(early_pg_dir, va,
451 				   load_pa + (va - PAGE_OFFSET),
452 				   map_size, PAGE_KERNEL_EXEC);
453 
454 	/* Create two consecutive PGD mappings for FDT early scan */
455 	pa = dtb_pa & ~(PGDIR_SIZE - 1);
456 	create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA,
457 			   pa, PGDIR_SIZE, PAGE_KERNEL);
458 	create_pgd_mapping(early_pg_dir, DTB_EARLY_BASE_VA + PGDIR_SIZE,
459 			   pa + PGDIR_SIZE, PGDIR_SIZE, PAGE_KERNEL);
460 	dtb_early_va = (void *)DTB_EARLY_BASE_VA + (dtb_pa & (PGDIR_SIZE - 1));
461 	dtb_early_pa = dtb_pa;
462 
463 	/*
464 	 * Bootime fixmap only can handle PMD_SIZE mapping. Thus, boot-ioremap
465 	 * range can not span multiple pmds.
466 	 */
467 	BUILD_BUG_ON((__fix_to_virt(FIX_BTMAP_BEGIN) >> PMD_SHIFT)
468 		     != (__fix_to_virt(FIX_BTMAP_END) >> PMD_SHIFT));
469 
470 #ifndef __PAGETABLE_PMD_FOLDED
471 	/*
472 	 * Early ioremap fixmap is already created as it lies within first 2MB
473 	 * of fixmap region. We always map PMD_SIZE. Thus, both FIX_BTMAP_END
474 	 * FIX_BTMAP_BEGIN should lie in the same pmd. Verify that and warn
475 	 * the user if not.
476 	 */
477 	fix_bmap_spmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_BEGIN))];
478 	fix_bmap_epmd = fixmap_pmd[pmd_index(__fix_to_virt(FIX_BTMAP_END))];
479 	if (pmd_val(fix_bmap_spmd) != pmd_val(fix_bmap_epmd)) {
480 		WARN_ON(1);
481 		pr_warn("fixmap btmap start [%08lx] != end [%08lx]\n",
482 			pmd_val(fix_bmap_spmd), pmd_val(fix_bmap_epmd));
483 		pr_warn("fix_to_virt(FIX_BTMAP_BEGIN): %08lx\n",
484 			fix_to_virt(FIX_BTMAP_BEGIN));
485 		pr_warn("fix_to_virt(FIX_BTMAP_END):   %08lx\n",
486 			fix_to_virt(FIX_BTMAP_END));
487 
488 		pr_warn("FIX_BTMAP_END:       %d\n", FIX_BTMAP_END);
489 		pr_warn("FIX_BTMAP_BEGIN:     %d\n", FIX_BTMAP_BEGIN);
490 	}
491 #endif
492 }
493 
494 static void __init setup_vm_final(void)
495 {
496 	uintptr_t va, map_size;
497 	phys_addr_t pa, start, end;
498 	struct memblock_region *reg;
499 
500 	/* Set mmu_enabled flag */
501 	mmu_enabled = true;
502 
503 	/* Setup swapper PGD for fixmap */
504 	create_pgd_mapping(swapper_pg_dir, FIXADDR_START,
505 			   __pa_symbol(fixmap_pgd_next),
506 			   PGDIR_SIZE, PAGE_TABLE);
507 
508 	/* Map all memory banks */
509 	for_each_memblock(memory, reg) {
510 		start = reg->base;
511 		end = start + reg->size;
512 
513 		if (start >= end)
514 			break;
515 		if (memblock_is_nomap(reg))
516 			continue;
517 		if (start <= __pa(PAGE_OFFSET) &&
518 		    __pa(PAGE_OFFSET) < end)
519 			start = __pa(PAGE_OFFSET);
520 
521 		map_size = best_map_size(start, end - start);
522 		for (pa = start; pa < end; pa += map_size) {
523 			va = (uintptr_t)__va(pa);
524 			create_pgd_mapping(swapper_pg_dir, va, pa,
525 					   map_size, PAGE_KERNEL_EXEC);
526 		}
527 	}
528 
529 	/* Clear fixmap PTE and PMD mappings */
530 	clear_fixmap(FIX_PTE);
531 	clear_fixmap(FIX_PMD);
532 
533 	/* Move to swapper page table */
534 	csr_write(CSR_SATP, PFN_DOWN(__pa_symbol(swapper_pg_dir)) | SATP_MODE);
535 	local_flush_tlb_all();
536 }
537 #else
538 asmlinkage void __init setup_vm(uintptr_t dtb_pa)
539 {
540 #ifdef CONFIG_BUILTIN_DTB
541 	dtb_early_va = soc_lookup_builtin_dtb();
542 	if (!dtb_early_va) {
543 		/* Fallback to first available DTS */
544 		dtb_early_va = (void *) __dtb_start;
545 	}
546 #else
547 	dtb_early_va = (void *)dtb_pa;
548 #endif
549 	dtb_early_pa = dtb_pa;
550 }
551 
552 static inline void setup_vm_final(void)
553 {
554 }
555 #endif /* CONFIG_MMU */
556 
557 #ifdef CONFIG_STRICT_KERNEL_RWX
558 void mark_rodata_ro(void)
559 {
560 	unsigned long text_start = (unsigned long)_text;
561 	unsigned long text_end = (unsigned long)_etext;
562 	unsigned long rodata_start = (unsigned long)__start_rodata;
563 	unsigned long data_start = (unsigned long)_data;
564 	unsigned long max_low = (unsigned long)(__va(PFN_PHYS(max_low_pfn)));
565 
566 	set_memory_ro(text_start, (text_end - text_start) >> PAGE_SHIFT);
567 	set_memory_ro(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
568 	set_memory_nx(rodata_start, (data_start - rodata_start) >> PAGE_SHIFT);
569 	set_memory_nx(data_start, (max_low - data_start) >> PAGE_SHIFT);
570 
571 	debug_checkwx();
572 }
573 #endif
574 
575 static void __init resource_init(void)
576 {
577 	struct memblock_region *region;
578 
579 	for_each_memblock(memory, region) {
580 		struct resource *res;
581 
582 		res = memblock_alloc(sizeof(struct resource), SMP_CACHE_BYTES);
583 		if (!res)
584 			panic("%s: Failed to allocate %zu bytes\n", __func__,
585 			      sizeof(struct resource));
586 
587 		if (memblock_is_nomap(region)) {
588 			res->name = "reserved";
589 			res->flags = IORESOURCE_MEM;
590 		} else {
591 			res->name = "System RAM";
592 			res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
593 		}
594 		res->start = __pfn_to_phys(memblock_region_memory_base_pfn(region));
595 		res->end = __pfn_to_phys(memblock_region_memory_end_pfn(region)) - 1;
596 
597 		request_resource(&iomem_resource, res);
598 	}
599 }
600 
601 void __init paging_init(void)
602 {
603 	setup_vm_final();
604 	sparse_init();
605 	setup_zero_page();
606 	zone_sizes_init();
607 	resource_init();
608 }
609 
610 #ifdef CONFIG_SPARSEMEM_VMEMMAP
611 int __meminit vmemmap_populate(unsigned long start, unsigned long end, int node,
612 			       struct vmem_altmap *altmap)
613 {
614 	return vmemmap_populate_basepages(start, end, node, NULL);
615 }
616 #endif
617