xref: /openbmc/linux/arch/arm/mm/init.c (revision a32c1c61)
1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  linux/arch/arm/mm/init.c
41da177e4SLinus Torvalds  *
590072059SRussell King  *  Copyright (C) 1995-2005 Russell King
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds #include <linux/kernel.h>
81da177e4SLinus Torvalds #include <linux/errno.h>
91da177e4SLinus Torvalds #include <linux/swap.h>
101da177e4SLinus Torvalds #include <linux/init.h>
111da177e4SLinus Torvalds #include <linux/mman.h>
123f07c014SIngo Molnar #include <linux/sched/signal.h>
1329930025SIngo Molnar #include <linux/sched/task.h>
14dc28094bSPaul Gortmaker #include <linux/export.h>
151da177e4SLinus Torvalds #include <linux/nodemask.h>
161da177e4SLinus Torvalds #include <linux/initrd.h>
179eb8f674SGrant Likely #include <linux/of_fdt.h>
183835f6cbSNicolas Pitre #include <linux/highmem.h>
195a0e3ad6STejun Heo #include <linux/gfp.h>
202778f620SRussell King #include <linux/memblock.h>
21c7909509SMarek Szyprowski #include <linux/dma-contiguous.h>
22158e8bfeSAlessandro Rubini #include <linux/sizes.h>
2308925c2fSLaura Abbott #include <linux/stop_machine.h>
24ad3c7b18SChristoph Hellwig #include <linux/swiotlb.h>
251da177e4SLinus Torvalds 
26b4b20ad8SRussell King #include <asm/cp15.h>
271da177e4SLinus Torvalds #include <asm/mach-types.h>
28716a3dc2SRussell King #include <asm/memblock.h>
29d2ca5f24SAfzal Mohammed #include <asm/memory.h>
3093c02ab4SGrant Likely #include <asm/prom.h>
3137efe642SRussell King #include <asm/sections.h>
321da177e4SLinus Torvalds #include <asm/setup.h>
339110f3e7SBen Dooks (Codethink) #include <asm/set_memory.h>
341e6b4811SKees Cook #include <asm/system_info.h>
351da177e4SLinus Torvalds #include <asm/tlb.h>
36db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h>
37a8e53c15SJinbum Park #include <asm/ptdump.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #include <asm/mach/arch.h>
401da177e4SLinus Torvalds #include <asm/mach/map.h>
411da177e4SLinus Torvalds 
421b2e2b73SRussell King #include "mm.h"
431b2e2b73SRussell King 
44b4b20ad8SRussell King #ifdef CONFIG_CPU_CP15_MMU
45b4b20ad8SRussell King unsigned long __init __clear_cr(unsigned long mask)
46b4b20ad8SRussell King {
47b4b20ad8SRussell King 	cr_alignment = cr_alignment & ~mask;
48b4b20ad8SRussell King 	return cr_alignment;
49b4b20ad8SRussell King }
50b4b20ad8SRussell King #endif
51b4b20ad8SRussell King 
52b1ab95c6SFlorian Fainelli #ifdef CONFIG_BLK_DEV_INITRD
53012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag)
54012d1f4aSRussell King {
554ed89f22SRussell King 	pr_warn("ATAG_INITRD is deprecated; "
56012d1f4aSRussell King 		"please update your bootloader.\n");
57012d1f4aSRussell King 	phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
58012d1f4aSRussell King 	phys_initrd_size = tag->u.initrd.size;
59012d1f4aSRussell King 	return 0;
60012d1f4aSRussell King }
61012d1f4aSRussell King 
62012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd);
63012d1f4aSRussell King 
64012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag)
65012d1f4aSRussell King {
66012d1f4aSRussell King 	phys_initrd_start = tag->u.initrd.start;
67012d1f4aSRussell King 	phys_initrd_size = tag->u.initrd.size;
68012d1f4aSRussell King 	return 0;
69012d1f4aSRussell King }
70012d1f4aSRussell King 
71012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2);
72b1ab95c6SFlorian Fainelli #endif
731da177e4SLinus Torvalds 
74f25b4b4cSRussell King static void __init find_limits(unsigned long *min, unsigned long *max_low,
75f25b4b4cSRussell King 			       unsigned long *max_high)
76dde5828fSRussell King {
771c2f87c2SLaura Abbott 	*max_low = PFN_DOWN(memblock_get_current_limit());
781c2f87c2SLaura Abbott 	*min = PFN_UP(memblock_start_of_DRAM());
791c2f87c2SLaura Abbott 	*max_high = PFN_DOWN(memblock_end_of_DRAM());
80dde5828fSRussell King }
81dde5828fSRussell King 
82be20902bSRussell King #ifdef CONFIG_ZONE_DMA
8365032018SNicolas Pitre 
84364230b9SRob Herring phys_addr_t arm_dma_zone_size __read_mostly;
8565032018SNicolas Pitre EXPORT_SYMBOL(arm_dma_zone_size);
8665032018SNicolas Pitre 
87022ae537SRussell King /*
88022ae537SRussell King  * The DMA mask corresponding to the maximum bus address allocatable
89022ae537SRussell King  * using GFP_DMA.  The default here places no restriction on DMA
90022ae537SRussell King  * allocations.  This must be the smallest DMA mask in the system,
91022ae537SRussell King  * so a successful GFP_DMA allocation will always satisfy this.
92022ae537SRussell King  */
934986e5c7SMarek Szyprowski phys_addr_t arm_dma_limit;
944dcfa600SRussell King unsigned long arm_dma_pfn_limit;
95be20902bSRussell King #endif
96be20902bSRussell King 
97ff69a4c8SRussell King void __init setup_dma_zone(const struct machine_desc *mdesc)
98c7909509SMarek Szyprowski {
99c7909509SMarek Szyprowski #ifdef CONFIG_ZONE_DMA
100c7909509SMarek Szyprowski 	if (mdesc->dma_zone_size) {
101c7909509SMarek Szyprowski 		arm_dma_zone_size = mdesc->dma_zone_size;
1026bcac805SRussell King 		arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1;
103c7909509SMarek Szyprowski 	} else
104c7909509SMarek Szyprowski 		arm_dma_limit = 0xffffffff;
1054dcfa600SRussell King 	arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT;
106c7909509SMarek Szyprowski #endif
107c7909509SMarek Szyprowski }
108c7909509SMarek Szyprowski 
10984f452b1SSantosh Shilimkar static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
110a2c54d2aSRussell King 	unsigned long max_high)
111b7a69ac3SRussell King {
112a32c1c61SMike Rapoport 	unsigned long max_zone_pfn[MAX_NR_ZONES] = { 0 };
11390072059SRussell King 
11465032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA
115a32c1c61SMike Rapoport 	max_zone_pfn[ZONE_DMA] = min(arm_dma_pfn_limit, max_low);
116be20902bSRussell King #endif
117a32c1c61SMike Rapoport 	max_zone_pfn[ZONE_NORMAL] = max_low;
118a32c1c61SMike Rapoport #ifdef CONFIG_HIGHMEM
119a32c1c61SMike Rapoport 	max_zone_pfn[ZONE_HIGHMEM] = max_high;
120a32c1c61SMike Rapoport #endif
121a32c1c61SMike Rapoport 	free_area_init(max_zone_pfn);
12290072059SRussell King }
12390072059SRussell King 
1247b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID
125b7cfda9fSRussell King int pfn_valid(unsigned long pfn)
126b7cfda9fSRussell King {
1275b3efa4fSzhaoyang 	phys_addr_t addr = __pfn_to_phys(pfn);
1285b3efa4fSzhaoyang 
1295b3efa4fSzhaoyang 	if (__phys_to_pfn(addr) != pfn)
1305b3efa4fSzhaoyang 		return 0;
1315b3efa4fSzhaoyang 
132032be728SClemens Gruber 	return memblock_is_map_memory(addr);
133b7cfda9fSRussell King }
134b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid);
1357b7bf499SWill Deacon #endif
136657e12fdSRussell King 
137716a3dc2SRussell King static bool arm_memblock_steal_permitted = true;
138716a3dc2SRussell King 
139bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align)
140716a3dc2SRussell King {
141716a3dc2SRussell King 	phys_addr_t phys;
142716a3dc2SRussell King 
143716a3dc2SRussell King 	BUG_ON(!arm_memblock_steal_permitted);
144716a3dc2SRussell King 
145f240ec09SMike Rapoport 	phys = memblock_phys_alloc(size, align);
146ecc3e771SMike Rapoport 	if (!phys)
147ecc3e771SMike Rapoport 		panic("Failed to steal %pa bytes at %pS\n",
148ecc3e771SMike Rapoport 		      &size, (void *)_RET_IP_);
149ecc3e771SMike Rapoport 
150716a3dc2SRussell King 	memblock_free(phys, size);
151716a3dc2SRussell King 	memblock_remove(phys, size);
152716a3dc2SRussell King 
153716a3dc2SRussell King 	return phys;
154716a3dc2SRussell King }
155716a3dc2SRussell King 
15639286248SRussell King static void __init arm_initrd_init(void)
1572778f620SRussell King {
1582778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD
159cdcc5fa0SRussell King 	phys_addr_t start;
160cdcc5fa0SRussell King 	unsigned long size;
161cdcc5fa0SRussell King 
1624c235cb9SBen Peddell 	initrd_start = initrd_end = 0;
16368b32f36SRussell King 
16468b32f36SRussell King 	if (!phys_initrd_size)
16568b32f36SRussell King 		return;
16668b32f36SRussell King 
167cdcc5fa0SRussell King 	/*
168cdcc5fa0SRussell King 	 * Round the memory region to page boundaries as per free_initrd_mem()
169cdcc5fa0SRussell King 	 * This allows us to detect whether the pages overlapping the initrd
170cdcc5fa0SRussell King 	 * are in use, but more importantly, reserves the entire set of pages
171cdcc5fa0SRussell King 	 * as we don't want these pages allocated for other purposes.
172cdcc5fa0SRussell King 	 */
173cdcc5fa0SRussell King 	start = round_down(phys_initrd_start, PAGE_SIZE);
174cdcc5fa0SRussell King 	size = phys_initrd_size + (phys_initrd_start - start);
175cdcc5fa0SRussell King 	size = round_up(size, PAGE_SIZE);
176cdcc5fa0SRussell King 
177cdcc5fa0SRussell King 	if (!memblock_is_region_memory(start, size)) {
178de22cc6eSVitaly Andrianov 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n",
179cdcc5fa0SRussell King 		       (u64)start, size);
18068b32f36SRussell King 		return;
1818f4b8c76SRussell King 	}
18268b32f36SRussell King 
183cdcc5fa0SRussell King 	if (memblock_is_region_reserved(start, size)) {
184de22cc6eSVitaly Andrianov 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n",
185cdcc5fa0SRussell King 		       (u64)start, size);
18668b32f36SRussell King 		return;
187b0a2679dSRussell King 	}
18868b32f36SRussell King 
189cdcc5fa0SRussell King 	memblock_reserve(start, size);
1902778f620SRussell King 
1912778f620SRussell King 	/* Now convert initrd to virtual addresses */
1922778f620SRussell King 	initrd_start = __phys_to_virt(phys_initrd_start);
1932778f620SRussell King 	initrd_end = initrd_start + phys_initrd_size;
1942778f620SRussell King #endif
19539286248SRussell King }
19639286248SRussell King 
1975f41f919SMarek Szyprowski #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
1985f41f919SMarek Szyprowski void check_cpu_icache_size(int cpuid)
1995f41f919SMarek Szyprowski {
2005f41f919SMarek Szyprowski 	u32 size, ctr;
2015f41f919SMarek Szyprowski 
2025f41f919SMarek Szyprowski 	asm("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
2035f41f919SMarek Szyprowski 
2045f41f919SMarek Szyprowski 	size = 1 << ((ctr & 0xf) + 2);
2055f41f919SMarek Szyprowski 	if (cpuid != 0 && icache_size != size)
2065f41f919SMarek Szyprowski 		pr_info("CPU%u: detected I-Cache line size mismatch, workaround enabled\n",
2075f41f919SMarek Szyprowski 			cpuid);
2085f41f919SMarek Szyprowski 	if (icache_size > size)
2095f41f919SMarek Szyprowski 		icache_size = size;
2105f41f919SMarek Szyprowski }
2115f41f919SMarek Szyprowski #endif
2125f41f919SMarek Szyprowski 
21339286248SRussell King void __init arm_memblock_init(const struct machine_desc *mdesc)
21439286248SRussell King {
21539286248SRussell King 	/* Register the kernel text, kernel data and initrd with memblock. */
21639286248SRussell King 	memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START);
21739286248SRussell King 
21839286248SRussell King 	arm_initrd_init();
2192778f620SRussell King 
2202778f620SRussell King 	arm_mm_memblock_reserve();
2212778f620SRussell King 
2228d717a52SRussell King 	/* reserve any platform specific memblock areas */
2238d717a52SRussell King 	if (mdesc->reserve)
2248d717a52SRussell King 		mdesc->reserve();
2258d717a52SRussell King 
22624bbd929SArd Biesheuvel 	early_init_fdt_reserve_self();
227bcedb5f9SMarek Szyprowski 	early_init_fdt_scan_reserved_mem();
228bcedb5f9SMarek Szyprowski 
22999a468d7SGeorge G. Davis 	/* reserve memory for DMA contiguous allocations */
23095b0e655SMarek Szyprowski 	dma_contiguous_reserve(arm_dma_limit);
231c7909509SMarek Szyprowski 
232716a3dc2SRussell King 	arm_memblock_steal_permitted = false;
2332778f620SRussell King 	memblock_dump_all();
2342778f620SRussell King }
2352778f620SRussell King 
2368d717a52SRussell King void __init bootmem_init(void)
23790072059SRussell King {
2388e58caefSGrygorii Strashko 	memblock_allow_resize();
239dde5828fSRussell King 
240071d184aSDoug Berger 	find_limits(&min_low_pfn, &max_low_pfn, &max_pfn);
241dde5828fSRussell King 
242071d184aSDoug Berger 	early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
243071d184aSDoug Berger 		      (phys_addr_t)max_low_pfn << PAGE_SHIFT);
244d30eae47SVladimir Murzin 
245b7a69ac3SRussell King 	/*
246657e12fdSRussell King 	 * Sparsemem tries to allocate bootmem in memory_present(),
247657e12fdSRussell King 	 * so must be done after the fixed reservations
248657e12fdSRussell King 	 */
24914b5f54bSPeng Fan 	memblocks_present();
25090072059SRussell King 
251b7a69ac3SRussell King 	/*
252b7a69ac3SRussell King 	 * sparse_init() needs the bootmem allocator up and running.
253b7a69ac3SRussell King 	 */
254b7a69ac3SRussell King 	sparse_init();
255b7a69ac3SRussell King 
256b7a69ac3SRussell King 	/*
257a32c1c61SMike Rapoport 	 * Now free the memory - free_area_init needs
258b7a69ac3SRussell King 	 * the sparse mem_map arrays initialized by sparse_init()
259b7a69ac3SRussell King 	 * for memmap_init_zone(), otherwise all PFNs are invalid.
260b7a69ac3SRussell King 	 */
261071d184aSDoug Berger 	zone_sizes_init(min_low_pfn, max_low_pfn, max_pfn);
26290072059SRussell King }
26390072059SRussell King 
26454d52573SStephen Boyd /*
26554d52573SStephen Boyd  * Poison init memory with an undefined instruction (ARM) or a branch to an
26654d52573SStephen Boyd  * undefined instruction (Thumb).
26754d52573SStephen Boyd  */
26854d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count)
26954d52573SStephen Boyd {
27054d52573SStephen Boyd 	u32 *p = (u32 *)s;
271bf912d99SJamie Iles 	for (; count != 0; count -= 4)
27254d52573SStephen Boyd 		*p++ = 0xe7fddef0;
27354d52573SStephen Boyd }
27454d52573SStephen Boyd 
27531f3010eSOlof Johansson static inline void __init
276be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn)
277a013053dSRussell King {
278a013053dSRussell King 	struct page *start_pg, *end_pg;
27956bc6286SVitaly Andrianov 	phys_addr_t pg, pgend;
280a013053dSRussell King 
281a013053dSRussell King 	/*
282a013053dSRussell King 	 * Convert start_pfn/end_pfn to a struct page pointer.
283a013053dSRussell King 	 */
2843257f43dSCatalin Marinas 	start_pg = pfn_to_page(start_pfn - 1) + 1;
2859af386c8SWill Deacon 	end_pg = pfn_to_page(end_pfn - 1) + 1;
286a013053dSRussell King 
287a013053dSRussell King 	/*
288a013053dSRussell King 	 * Convert to physical addresses, and
289a013053dSRussell King 	 * round start upwards and end downwards.
290a013053dSRussell King 	 */
29156bc6286SVitaly Andrianov 	pg = PAGE_ALIGN(__pa(start_pg));
29256bc6286SVitaly Andrianov 	pgend = __pa(end_pg) & PAGE_MASK;
293a013053dSRussell King 
294a013053dSRussell King 	/*
295a013053dSRussell King 	 * If there are free pages between these,
296a013053dSRussell King 	 * free the section of the memmap array.
297a013053dSRussell King 	 */
298a013053dSRussell King 	if (pg < pgend)
299cfb66586SSantosh Shilimkar 		memblock_free_early(pg, pgend - pg);
300a013053dSRussell King }
301a013053dSRussell King 
302a013053dSRussell King /*
303a013053dSRussell King  * The mem_map array can get very big.  Free the unused area of the memory map.
304a013053dSRussell King  */
3051c2f87c2SLaura Abbott static void __init free_unused_memmap(void)
306a013053dSRussell King {
3071c2f87c2SLaura Abbott 	unsigned long start, prev_end = 0;
3081c2f87c2SLaura Abbott 	struct memblock_region *reg;
309a013053dSRussell King 
310a013053dSRussell King 	/*
3113260e529SMichael Bohan 	 * This relies on each bank being in address order.
3123260e529SMichael Bohan 	 * The banks are sorted previously in bootmem_init().
313a013053dSRussell King 	 */
3141c2f87c2SLaura Abbott 	for_each_memblock(memory, reg) {
3151c2f87c2SLaura Abbott 		start = memblock_region_memory_base_pfn(reg);
316a013053dSRussell King 
3179af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM
3189af386c8SWill Deacon 		/*
3199af386c8SWill Deacon 		 * Take care not to free memmap entries that don't exist
3209af386c8SWill Deacon 		 * due to SPARSEMEM sections which aren't present.
3219af386c8SWill Deacon 		 */
3221c2f87c2SLaura Abbott 		start = min(start,
3231c2f87c2SLaura Abbott 				 ALIGN(prev_end, PAGES_PER_SECTION));
324002ea9eeSLinus Walleij #else
325002ea9eeSLinus Walleij 		/*
326002ea9eeSLinus Walleij 		 * Align down here since the VM subsystem insists that the
327002ea9eeSLinus Walleij 		 * memmap entries are valid from the bank start aligned to
328002ea9eeSLinus Walleij 		 * MAX_ORDER_NR_PAGES.
329002ea9eeSLinus Walleij 		 */
3301c2f87c2SLaura Abbott 		start = round_down(start, MAX_ORDER_NR_PAGES);
3319af386c8SWill Deacon #endif
332a013053dSRussell King 		/*
333a013053dSRussell King 		 * If we had a previous bank, and there is a space
334a013053dSRussell King 		 * between the current bank and the previous, free it.
335a013053dSRussell King 		 */
3361c2f87c2SLaura Abbott 		if (prev_end && prev_end < start)
3371c2f87c2SLaura Abbott 			free_memmap(prev_end, start);
338a013053dSRussell King 
3393260e529SMichael Bohan 		/*
3403260e529SMichael Bohan 		 * Align up here since the VM subsystem insists that the
3413260e529SMichael Bohan 		 * memmap entries are valid from the bank end aligned to
3423260e529SMichael Bohan 		 * MAX_ORDER_NR_PAGES.
3433260e529SMichael Bohan 		 */
3441c2f87c2SLaura Abbott 		prev_end = ALIGN(memblock_region_memory_end_pfn(reg),
3451c2f87c2SLaura Abbott 				 MAX_ORDER_NR_PAGES);
346a013053dSRussell King 	}
3479af386c8SWill Deacon 
3489af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM
3491c2f87c2SLaura Abbott 	if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
3501c2f87c2SLaura Abbott 		free_memmap(prev_end,
3511c2f87c2SLaura Abbott 			    ALIGN(prev_end, PAGES_PER_SECTION));
3529af386c8SWill Deacon #endif
353a013053dSRussell King }
354a013053dSRussell King 
35583db0384SJiang Liu #ifdef CONFIG_HIGHMEM
35683db0384SJiang Liu static inline void free_area_high(unsigned long pfn, unsigned long end)
35783db0384SJiang Liu {
358dd6911efSJiang Liu 	for (; pfn < end; pfn++)
359dd6911efSJiang Liu 		free_highmem_page(pfn_to_page(pfn));
36083db0384SJiang Liu }
36183db0384SJiang Liu #endif
36283db0384SJiang Liu 
363d0e775afSRussell King static void __init free_highpages(void)
364d0e775afSRussell King {
365d0e775afSRussell King #ifdef CONFIG_HIGHMEM
36626ba47b1SSantosh Shilimkar 	unsigned long max_low = max_low_pfn;
367df4f14c7SRussell King 	struct memblock_region *mem, *res;
368d0e775afSRussell King 
369d0e775afSRussell King 	/* set highmem page free */
370df4f14c7SRussell King 	for_each_memblock(memory, mem) {
371df4f14c7SRussell King 		unsigned long start = memblock_region_memory_base_pfn(mem);
372df4f14c7SRussell King 		unsigned long end = memblock_region_memory_end_pfn(mem);
373df4f14c7SRussell King 
374df4f14c7SRussell King 		/* Ignore complete lowmem entries */
375df4f14c7SRussell King 		if (end <= max_low)
376df4f14c7SRussell King 			continue;
377df4f14c7SRussell King 
37809414d00SArd Biesheuvel 		if (memblock_is_nomap(mem))
37909414d00SArd Biesheuvel 			continue;
38009414d00SArd Biesheuvel 
381df4f14c7SRussell King 		/* Truncate partial highmem entries */
382df4f14c7SRussell King 		if (start < max_low)
383df4f14c7SRussell King 			start = max_low;
384df4f14c7SRussell King 
385df4f14c7SRussell King 		/* Find and exclude any reserved regions */
386df4f14c7SRussell King 		for_each_memblock(reserved, res) {
387df4f14c7SRussell King 			unsigned long res_start, res_end;
388df4f14c7SRussell King 
389df4f14c7SRussell King 			res_start = memblock_region_reserved_base_pfn(res);
390df4f14c7SRussell King 			res_end = memblock_region_reserved_end_pfn(res);
391df4f14c7SRussell King 
392df4f14c7SRussell King 			if (res_end < start)
393df4f14c7SRussell King 				continue;
394df4f14c7SRussell King 			if (res_start < start)
395df4f14c7SRussell King 				res_start = start;
396df4f14c7SRussell King 			if (res_start > end)
397df4f14c7SRussell King 				res_start = end;
398df4f14c7SRussell King 			if (res_end > end)
399df4f14c7SRussell King 				res_end = end;
400df4f14c7SRussell King 			if (res_start != start)
40183db0384SJiang Liu 				free_area_high(start, res_start);
402df4f14c7SRussell King 			start = res_end;
403df4f14c7SRussell King 			if (start == end)
404df4f14c7SRussell King 				break;
405df4f14c7SRussell King 		}
406df4f14c7SRussell King 
407df4f14c7SRussell King 		/* And now free anything which remains */
408df4f14c7SRussell King 		if (start < end)
40983db0384SJiang Liu 			free_area_high(start, end);
410d0e775afSRussell King 	}
411d0e775afSRussell King #endif
412d0e775afSRussell King }
413d0e775afSRussell King 
4141da177e4SLinus Torvalds /*
4151da177e4SLinus Torvalds  * mem_init() marks the free areas in the mem_map and tells us how much
4161da177e4SLinus Torvalds  * memory is free.  This is done after various parts of the system have
4171da177e4SLinus Torvalds  * claimed their memory after the kernel image.
4181da177e4SLinus Torvalds  */
4191da177e4SLinus Torvalds void __init mem_init(void)
4201da177e4SLinus Torvalds {
421ad3c7b18SChristoph Hellwig #ifdef CONFIG_ARM_LPAE
422ad3c7b18SChristoph Hellwig 	swiotlb_init(1);
423ad3c7b18SChristoph Hellwig #endif
424ad3c7b18SChristoph Hellwig 
425b3ba41f2SSantosh Shilimkar 	set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
4261da177e4SLinus Torvalds 
4271da177e4SLinus Torvalds 	/* this will put all unused low memory onto the freelists */
4281c2f87c2SLaura Abbott 	free_unused_memmap();
429c6ffc5caSMike Rapoport 	memblock_free_all();
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds #ifdef CONFIG_SA1111
4321da177e4SLinus Torvalds 	/* now that our DMA memory is actually so designated, we can free it */
433bfd65dd9SLinus Torvalds 	free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL);
4341da177e4SLinus Torvalds #endif
4351da177e4SLinus Torvalds 
436d0e775afSRussell King 	free_highpages();
4373835f6cbSNicolas Pitre 
4382450c973SJiang Liu 	mem_init_print_info(NULL);
4391da177e4SLinus Torvalds 
440a1839272SFenkart/Bostandzhyan 	/*
441a1839272SFenkart/Bostandzhyan 	 * Check boundaries twice: Some fundamental inconsistencies can
442a1839272SFenkart/Bostandzhyan 	 * be detected at build time already.
443a1839272SFenkart/Bostandzhyan 	 */
444a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU
445a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(TASK_SIZE				> MODULES_VADDR);
446a1839272SFenkart/Bostandzhyan 	BUG_ON(TASK_SIZE 				> MODULES_VADDR);
447a1839272SFenkart/Bostandzhyan #endif
448a1839272SFenkart/Bostandzhyan 
449a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
450a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
451a1839272SFenkart/Bostandzhyan 	BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE	> PAGE_OFFSET);
452a1839272SFenkart/Bostandzhyan #endif
4531da177e4SLinus Torvalds }
4541da177e4SLinus Torvalds 
4550f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
4561e6b4811SKees Cook struct section_perm {
45725362dc4SKees Cook 	const char *name;
4581e6b4811SKees Cook 	unsigned long start;
4591e6b4811SKees Cook 	unsigned long end;
4601e6b4811SKees Cook 	pmdval_t mask;
4611e6b4811SKees Cook 	pmdval_t prot;
46280d6b0c2SKees Cook 	pmdval_t clear;
4631e6b4811SKees Cook };
4641e6b4811SKees Cook 
46564ac2e74SKees Cook /* First section-aligned location at or after __start_rodata. */
46664ac2e74SKees Cook extern char __start_rodata_section_aligned[];
46764ac2e74SKees Cook 
46880d6b0c2SKees Cook static struct section_perm nx_perms[] = {
4691e6b4811SKees Cook 	/* Make pages tables, etc before _stext RW (set NX). */
4701e6b4811SKees Cook 	{
47125362dc4SKees Cook 		.name	= "pre-text NX",
4721e6b4811SKees Cook 		.start	= PAGE_OFFSET,
4731e6b4811SKees Cook 		.end	= (unsigned long)_stext,
4741e6b4811SKees Cook 		.mask	= ~PMD_SECT_XN,
4751e6b4811SKees Cook 		.prot	= PMD_SECT_XN,
4761e6b4811SKees Cook 	},
4771e6b4811SKees Cook 	/* Make init RW (set NX). */
4781e6b4811SKees Cook 	{
47925362dc4SKees Cook 		.name	= "init NX",
4801e6b4811SKees Cook 		.start	= (unsigned long)__init_begin,
4811e6b4811SKees Cook 		.end	= (unsigned long)_sdata,
4821e6b4811SKees Cook 		.mask	= ~PMD_SECT_XN,
4831e6b4811SKees Cook 		.prot	= PMD_SECT_XN,
4841e6b4811SKees Cook 	},
48580d6b0c2SKees Cook 	/* Make rodata NX (set RO in ro_perms below). */
48680d6b0c2SKees Cook 	{
48725362dc4SKees Cook 		.name	= "rodata NX",
48864ac2e74SKees Cook 		.start  = (unsigned long)__start_rodata_section_aligned,
48980d6b0c2SKees Cook 		.end    = (unsigned long)__init_begin,
49080d6b0c2SKees Cook 		.mask   = ~PMD_SECT_XN,
49180d6b0c2SKees Cook 		.prot   = PMD_SECT_XN,
49280d6b0c2SKees Cook 	},
4931e6b4811SKees Cook };
4941e6b4811SKees Cook 
49580d6b0c2SKees Cook static struct section_perm ro_perms[] = {
49680d6b0c2SKees Cook 	/* Make kernel code and rodata RX (set RO). */
49780d6b0c2SKees Cook 	{
49825362dc4SKees Cook 		.name	= "text/rodata RO",
49980d6b0c2SKees Cook 		.start  = (unsigned long)_stext,
50080d6b0c2SKees Cook 		.end    = (unsigned long)__init_begin,
50180d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE
502400eeffaSPhilip Derrin 		.mask   = ~(L_PMD_SECT_RDONLY | PMD_SECT_AP2),
503400eeffaSPhilip Derrin 		.prot   = L_PMD_SECT_RDONLY | PMD_SECT_AP2,
50480d6b0c2SKees Cook #else
50580d6b0c2SKees Cook 		.mask   = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE),
50680d6b0c2SKees Cook 		.prot   = PMD_SECT_APX | PMD_SECT_AP_WRITE,
50780d6b0c2SKees Cook 		.clear  = PMD_SECT_AP_WRITE,
50880d6b0c2SKees Cook #endif
50980d6b0c2SKees Cook 	},
51080d6b0c2SKees Cook };
51180d6b0c2SKees Cook 
5121e6b4811SKees Cook /*
5131e6b4811SKees Cook  * Updates section permissions only for the current mm (sections are
5141e6b4811SKees Cook  * copied into each mm). During startup, this is the init_mm. Is only
5151e6b4811SKees Cook  * safe to be called with preemption disabled, as under stop_machine().
5161e6b4811SKees Cook  */
5171e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask,
51808925c2fSLaura Abbott 				  pmdval_t prot, struct mm_struct *mm)
5191e6b4811SKees Cook {
5201e6b4811SKees Cook 	pmd_t *pmd;
5211e6b4811SKees Cook 
5221e6b4811SKees Cook 	pmd = pmd_offset(pud_offset(pgd_offset(mm, addr), addr), addr);
5231e6b4811SKees Cook 
5241e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE
5251e6b4811SKees Cook 	pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
5261e6b4811SKees Cook #else
5271e6b4811SKees Cook 	if (addr & SECTION_SIZE)
5281e6b4811SKees Cook 		pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot);
5291e6b4811SKees Cook 	else
5301e6b4811SKees Cook 		pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
5311e6b4811SKees Cook #endif
5321e6b4811SKees Cook 	flush_pmd_entry(pmd);
5331e6b4811SKees Cook 	local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE);
5341e6b4811SKees Cook }
5351e6b4811SKees Cook 
5361e6b4811SKees Cook /* Make sure extended page tables are in use. */
5371e6b4811SKees Cook static inline bool arch_has_strict_perms(void)
5381e6b4811SKees Cook {
5391e6b4811SKees Cook 	if (cpu_architecture() < CPU_ARCH_ARMv6)
5401e6b4811SKees Cook 		return false;
5411e6b4811SKees Cook 
5421e6b4811SKees Cook 	return !!(get_cr() & CR_XP);
5431e6b4811SKees Cook }
5441e6b4811SKees Cook 
545ea5379beSBen Dooks (Codethink) static void set_section_perms(struct section_perm *perms, int n, bool set,
54608925c2fSLaura Abbott 			      struct mm_struct *mm)
54708925c2fSLaura Abbott {
54808925c2fSLaura Abbott 	size_t i;
54908925c2fSLaura Abbott 	unsigned long addr;
55008925c2fSLaura Abbott 
55108925c2fSLaura Abbott 	if (!arch_has_strict_perms())
55208925c2fSLaura Abbott 		return;
55308925c2fSLaura Abbott 
55408925c2fSLaura Abbott 	for (i = 0; i < n; i++) {
55508925c2fSLaura Abbott 		if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) ||
55608925c2fSLaura Abbott 		    !IS_ALIGNED(perms[i].end, SECTION_SIZE)) {
55725362dc4SKees Cook 			pr_err("BUG: %s section %lx-%lx not aligned to %lx\n",
55825362dc4SKees Cook 				perms[i].name, perms[i].start, perms[i].end,
55908925c2fSLaura Abbott 				SECTION_SIZE);
56008925c2fSLaura Abbott 			continue;
5611e6b4811SKees Cook 		}
5621e6b4811SKees Cook 
56308925c2fSLaura Abbott 		for (addr = perms[i].start;
56408925c2fSLaura Abbott 		     addr < perms[i].end;
56508925c2fSLaura Abbott 		     addr += SECTION_SIZE)
56608925c2fSLaura Abbott 			section_update(addr, perms[i].mask,
56708925c2fSLaura Abbott 				set ? perms[i].prot : perms[i].clear, mm);
56808925c2fSLaura Abbott 	}
56908925c2fSLaura Abbott 
57008925c2fSLaura Abbott }
57108925c2fSLaura Abbott 
57211ce4b33SGrygorii Strashko /**
57311ce4b33SGrygorii Strashko  * update_sections_early intended to be called only through stop_machine
57411ce4b33SGrygorii Strashko  * framework and executed by only one CPU while all other CPUs will spin and
57511ce4b33SGrygorii Strashko  * wait, so no locking is required in this function.
57611ce4b33SGrygorii Strashko  */
57708925c2fSLaura Abbott static void update_sections_early(struct section_perm perms[], int n)
5781e6b4811SKees Cook {
57908925c2fSLaura Abbott 	struct task_struct *t, *s;
58008925c2fSLaura Abbott 
58108925c2fSLaura Abbott 	for_each_process(t) {
58208925c2fSLaura Abbott 		if (t->flags & PF_KTHREAD)
58308925c2fSLaura Abbott 			continue;
58408925c2fSLaura Abbott 		for_each_thread(t, s)
585c51bc12dSDoug Berger 			if (s->mm)
58608925c2fSLaura Abbott 				set_section_perms(perms, n, true, s->mm);
58708925c2fSLaura Abbott 	}
58808925c2fSLaura Abbott 	set_section_perms(perms, n, true, current->active_mm);
58908925c2fSLaura Abbott 	set_section_perms(perms, n, true, &init_mm);
59008925c2fSLaura Abbott }
59108925c2fSLaura Abbott 
59211ce4b33SGrygorii Strashko static int __fix_kernmem_perms(void *unused)
59308925c2fSLaura Abbott {
59408925c2fSLaura Abbott 	update_sections_early(nx_perms, ARRAY_SIZE(nx_perms));
59508925c2fSLaura Abbott 	return 0;
59608925c2fSLaura Abbott }
59708925c2fSLaura Abbott 
59811ce4b33SGrygorii Strashko static void fix_kernmem_perms(void)
59908925c2fSLaura Abbott {
60008925c2fSLaura Abbott 	stop_machine(__fix_kernmem_perms, NULL, NULL);
6011e6b4811SKees Cook }
60280d6b0c2SKees Cook 
60311ce4b33SGrygorii Strashko static int __mark_rodata_ro(void *unused)
60408925c2fSLaura Abbott {
60508925c2fSLaura Abbott 	update_sections_early(ro_perms, ARRAY_SIZE(ro_perms));
60608925c2fSLaura Abbott 	return 0;
60708925c2fSLaura Abbott }
60808925c2fSLaura Abbott 
609b4c7e2bdSSteven Rostedt (VMware) static int kernel_set_to_readonly __read_mostly;
610b4c7e2bdSSteven Rostedt (VMware) 
61180d6b0c2SKees Cook void mark_rodata_ro(void)
61280d6b0c2SKees Cook {
613b4c7e2bdSSteven Rostedt (VMware) 	kernel_set_to_readonly = 1;
61408925c2fSLaura Abbott 	stop_machine(__mark_rodata_ro, NULL, NULL);
615a8e53c15SJinbum Park 	debug_checkwx();
61680d6b0c2SKees Cook }
61780d6b0c2SKees Cook 
61880d6b0c2SKees Cook void set_kernel_text_rw(void)
61980d6b0c2SKees Cook {
620b4c7e2bdSSteven Rostedt (VMware) 	if (!kernel_set_to_readonly)
621b4c7e2bdSSteven Rostedt (VMware) 		return;
622b4c7e2bdSSteven Rostedt (VMware) 
62308925c2fSLaura Abbott 	set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), false,
62408925c2fSLaura Abbott 				current->active_mm);
62580d6b0c2SKees Cook }
62680d6b0c2SKees Cook 
62780d6b0c2SKees Cook void set_kernel_text_ro(void)
62880d6b0c2SKees Cook {
629b4c7e2bdSSteven Rostedt (VMware) 	if (!kernel_set_to_readonly)
630b4c7e2bdSSteven Rostedt (VMware) 		return;
631b4c7e2bdSSteven Rostedt (VMware) 
63208925c2fSLaura Abbott 	set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), true,
63308925c2fSLaura Abbott 				current->active_mm);
63480d6b0c2SKees Cook }
63580d6b0c2SKees Cook 
6361e6b4811SKees Cook #else
6371e6b4811SKees Cook static inline void fix_kernmem_perms(void) { }
6380f5bf6d0SLaura Abbott #endif /* CONFIG_STRICT_KERNEL_RWX */
6391e6b4811SKees Cook 
6401e6b4811SKees Cook void free_initmem(void)
6411e6b4811SKees Cook {
6421e6b4811SKees Cook 	fix_kernmem_perms();
643bc581770SLinus Walleij 
64454d52573SStephen Boyd 	poison_init_mem(__init_begin, __init_end - __init_begin);
6456db015e4SNicolas Pitre 	if (!machine_is_integrator() && !machine_is_cintegrator())
646dbe67df4SJiang Liu 		free_initmem_default(-1);
6471da177e4SLinus Torvalds }
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
6501da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end)
6511da177e4SLinus Torvalds {
652421520baSYalin Wang 	if (start == initrd_start)
653421520baSYalin Wang 		start = round_down(start, PAGE_SIZE);
654421520baSYalin Wang 	if (end == initrd_end)
655421520baSYalin Wang 		end = round_up(end, PAGE_SIZE);
656421520baSYalin Wang 
65754d52573SStephen Boyd 	poison_init_mem((void *)start, PAGE_ALIGN(end) - start);
658dbe67df4SJiang Liu 	free_reserved_area((void *)start, (void *)end, -1, "initrd");
6591da177e4SLinus Torvalds }
6601da177e4SLinus Torvalds #endif
661