xref: /openbmc/linux/arch/arm/mm/init.c (revision 9110f3e7)
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;
95022ae537SRussell King 
96be20902bSRussell King static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole,
97be20902bSRussell King 	unsigned long dma_size)
98be20902bSRussell King {
99be20902bSRussell King 	if (size[0] <= dma_size)
100be20902bSRussell King 		return;
101be20902bSRussell King 
102be20902bSRussell King 	size[ZONE_NORMAL] = size[0] - dma_size;
103be20902bSRussell King 	size[ZONE_DMA] = dma_size;
104be20902bSRussell King 	hole[ZONE_NORMAL] = hole[0];
105be20902bSRussell King 	hole[ZONE_DMA] = 0;
106be20902bSRussell King }
107be20902bSRussell King #endif
108be20902bSRussell King 
109ff69a4c8SRussell King void __init setup_dma_zone(const struct machine_desc *mdesc)
110c7909509SMarek Szyprowski {
111c7909509SMarek Szyprowski #ifdef CONFIG_ZONE_DMA
112c7909509SMarek Szyprowski 	if (mdesc->dma_zone_size) {
113c7909509SMarek Szyprowski 		arm_dma_zone_size = mdesc->dma_zone_size;
1146bcac805SRussell King 		arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1;
115c7909509SMarek Szyprowski 	} else
116c7909509SMarek Szyprowski 		arm_dma_limit = 0xffffffff;
1174dcfa600SRussell King 	arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT;
118c7909509SMarek Szyprowski #endif
119c7909509SMarek Szyprowski }
120c7909509SMarek Szyprowski 
12184f452b1SSantosh Shilimkar static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
122a2c54d2aSRussell King 	unsigned long max_high)
123b7a69ac3SRussell King {
124b7a69ac3SRussell King 	unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
125a2c54d2aSRussell King 	struct memblock_region *reg;
126b7a69ac3SRussell King 
12790072059SRussell King 	/*
128be370302SRussell King 	 * initialise the zones.
12990072059SRussell King 	 */
13090072059SRussell King 	memset(zone_size, 0, sizeof(zone_size));
13190072059SRussell King 
13290072059SRussell King 	/*
133be370302SRussell King 	 * The memory size has already been determined.  If we need
134be370302SRussell King 	 * to do anything fancy with the allocation of this memory
135be370302SRussell King 	 * to the zones, now is the time to do it.
13690072059SRussell King 	 */
137dde5828fSRussell King 	zone_size[0] = max_low - min;
138dde5828fSRussell King #ifdef CONFIG_HIGHMEM
139dde5828fSRussell King 	zone_size[ZONE_HIGHMEM] = max_high - max_low;
140dde5828fSRussell King #endif
14190072059SRussell King 
14290072059SRussell King 	/*
143be370302SRussell King 	 * Calculate the size of the holes.
144be370302SRussell King 	 *  holes = node_size - sum(bank_sizes)
14590072059SRussell King 	 */
146dde5828fSRussell King 	memcpy(zhole_size, zone_size, sizeof(zhole_size));
147a2c54d2aSRussell King 	for_each_memblock(memory, reg) {
148a2c54d2aSRussell King 		unsigned long start = memblock_region_memory_base_pfn(reg);
149a2c54d2aSRussell King 		unsigned long end = memblock_region_memory_end_pfn(reg);
150a2c54d2aSRussell King 
151a2c54d2aSRussell King 		if (start < max_low) {
152a2c54d2aSRussell King 			unsigned long low_end = min(end, max_low);
153a2c54d2aSRussell King 			zhole_size[0] -= low_end - start;
154a2c54d2aSRussell King 		}
155dde5828fSRussell King #ifdef CONFIG_HIGHMEM
156a2c54d2aSRussell King 		if (end > max_low) {
157a2c54d2aSRussell King 			unsigned long high_start = max(start, max_low);
158a2c54d2aSRussell King 			zhole_size[ZONE_HIGHMEM] -= end - high_start;
159a2c54d2aSRussell King 		}
160dde5828fSRussell King #endif
161dde5828fSRussell King 	}
16290072059SRussell King 
16365032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA
16490072059SRussell King 	/*
16590072059SRussell King 	 * Adjust the sizes according to any special requirements for
16690072059SRussell King 	 * this machine type.
16790072059SRussell King 	 */
168c7909509SMarek Szyprowski 	if (arm_dma_zone_size)
169be20902bSRussell King 		arm_adjust_dma_zone(zone_size, zhole_size,
17065032018SNicolas Pitre 			arm_dma_zone_size >> PAGE_SHIFT);
171be20902bSRussell King #endif
17290072059SRussell King 
173be370302SRussell King 	free_area_init_node(0, zone_size, min, zhole_size);
17490072059SRussell King }
17590072059SRussell King 
1767b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID
177b7cfda9fSRussell King int pfn_valid(unsigned long pfn)
178b7cfda9fSRussell King {
1795b3efa4fSzhaoyang 	phys_addr_t addr = __pfn_to_phys(pfn);
1805b3efa4fSzhaoyang 
1815b3efa4fSzhaoyang 	if (__phys_to_pfn(addr) != pfn)
1825b3efa4fSzhaoyang 		return 0;
1835b3efa4fSzhaoyang 
184032be728SClemens Gruber 	return memblock_is_map_memory(addr);
185b7cfda9fSRussell King }
186b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid);
1877b7bf499SWill Deacon #endif
188657e12fdSRussell King 
189716a3dc2SRussell King static bool arm_memblock_steal_permitted = true;
190716a3dc2SRussell King 
191bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align)
192716a3dc2SRussell King {
193716a3dc2SRussell King 	phys_addr_t phys;
194716a3dc2SRussell King 
195716a3dc2SRussell King 	BUG_ON(!arm_memblock_steal_permitted);
196716a3dc2SRussell King 
197f240ec09SMike Rapoport 	phys = memblock_phys_alloc(size, align);
198ecc3e771SMike Rapoport 	if (!phys)
199ecc3e771SMike Rapoport 		panic("Failed to steal %pa bytes at %pS\n",
200ecc3e771SMike Rapoport 		      &size, (void *)_RET_IP_);
201ecc3e771SMike Rapoport 
202716a3dc2SRussell King 	memblock_free(phys, size);
203716a3dc2SRussell King 	memblock_remove(phys, size);
204716a3dc2SRussell King 
205716a3dc2SRussell King 	return phys;
206716a3dc2SRussell King }
207716a3dc2SRussell King 
20839286248SRussell King static void __init arm_initrd_init(void)
2092778f620SRussell King {
2102778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD
211cdcc5fa0SRussell King 	phys_addr_t start;
212cdcc5fa0SRussell King 	unsigned long size;
213cdcc5fa0SRussell King 
2144c235cb9SBen Peddell 	initrd_start = initrd_end = 0;
21568b32f36SRussell King 
21668b32f36SRussell King 	if (!phys_initrd_size)
21768b32f36SRussell King 		return;
21868b32f36SRussell King 
219cdcc5fa0SRussell King 	/*
220cdcc5fa0SRussell King 	 * Round the memory region to page boundaries as per free_initrd_mem()
221cdcc5fa0SRussell King 	 * This allows us to detect whether the pages overlapping the initrd
222cdcc5fa0SRussell King 	 * are in use, but more importantly, reserves the entire set of pages
223cdcc5fa0SRussell King 	 * as we don't want these pages allocated for other purposes.
224cdcc5fa0SRussell King 	 */
225cdcc5fa0SRussell King 	start = round_down(phys_initrd_start, PAGE_SIZE);
226cdcc5fa0SRussell King 	size = phys_initrd_size + (phys_initrd_start - start);
227cdcc5fa0SRussell King 	size = round_up(size, PAGE_SIZE);
228cdcc5fa0SRussell King 
229cdcc5fa0SRussell King 	if (!memblock_is_region_memory(start, size)) {
230de22cc6eSVitaly Andrianov 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n",
231cdcc5fa0SRussell King 		       (u64)start, size);
23268b32f36SRussell King 		return;
2338f4b8c76SRussell King 	}
23468b32f36SRussell King 
235cdcc5fa0SRussell King 	if (memblock_is_region_reserved(start, size)) {
236de22cc6eSVitaly Andrianov 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n",
237cdcc5fa0SRussell King 		       (u64)start, size);
23868b32f36SRussell King 		return;
239b0a2679dSRussell King 	}
24068b32f36SRussell King 
241cdcc5fa0SRussell King 	memblock_reserve(start, size);
2422778f620SRussell King 
2432778f620SRussell King 	/* Now convert initrd to virtual addresses */
2442778f620SRussell King 	initrd_start = __phys_to_virt(phys_initrd_start);
2452778f620SRussell King 	initrd_end = initrd_start + phys_initrd_size;
2462778f620SRussell King #endif
24739286248SRussell King }
24839286248SRussell King 
2495f41f919SMarek Szyprowski #ifdef CONFIG_CPU_ICACHE_MISMATCH_WORKAROUND
2505f41f919SMarek Szyprowski void check_cpu_icache_size(int cpuid)
2515f41f919SMarek Szyprowski {
2525f41f919SMarek Szyprowski 	u32 size, ctr;
2535f41f919SMarek Szyprowski 
2545f41f919SMarek Szyprowski 	asm("mrc p15, 0, %0, c0, c0, 1" : "=r" (ctr));
2555f41f919SMarek Szyprowski 
2565f41f919SMarek Szyprowski 	size = 1 << ((ctr & 0xf) + 2);
2575f41f919SMarek Szyprowski 	if (cpuid != 0 && icache_size != size)
2585f41f919SMarek Szyprowski 		pr_info("CPU%u: detected I-Cache line size mismatch, workaround enabled\n",
2595f41f919SMarek Szyprowski 			cpuid);
2605f41f919SMarek Szyprowski 	if (icache_size > size)
2615f41f919SMarek Szyprowski 		icache_size = size;
2625f41f919SMarek Szyprowski }
2635f41f919SMarek Szyprowski #endif
2645f41f919SMarek Szyprowski 
26539286248SRussell King void __init arm_memblock_init(const struct machine_desc *mdesc)
26639286248SRussell King {
26739286248SRussell King 	/* Register the kernel text, kernel data and initrd with memblock. */
26839286248SRussell King 	memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START);
26939286248SRussell King 
27039286248SRussell King 	arm_initrd_init();
2712778f620SRussell King 
2722778f620SRussell King 	arm_mm_memblock_reserve();
2732778f620SRussell King 
2748d717a52SRussell King 	/* reserve any platform specific memblock areas */
2758d717a52SRussell King 	if (mdesc->reserve)
2768d717a52SRussell King 		mdesc->reserve();
2778d717a52SRussell King 
27824bbd929SArd Biesheuvel 	early_init_fdt_reserve_self();
279bcedb5f9SMarek Szyprowski 	early_init_fdt_scan_reserved_mem();
280bcedb5f9SMarek Szyprowski 
28199a468d7SGeorge G. Davis 	/* reserve memory for DMA contiguous allocations */
28295b0e655SMarek Szyprowski 	dma_contiguous_reserve(arm_dma_limit);
283c7909509SMarek Szyprowski 
284716a3dc2SRussell King 	arm_memblock_steal_permitted = false;
2852778f620SRussell King 	memblock_dump_all();
2862778f620SRussell King }
2872778f620SRussell King 
2888d717a52SRussell King void __init bootmem_init(void)
28990072059SRussell King {
2908e58caefSGrygorii Strashko 	memblock_allow_resize();
291dde5828fSRussell King 
292071d184aSDoug Berger 	find_limits(&min_low_pfn, &max_low_pfn, &max_pfn);
293dde5828fSRussell King 
294071d184aSDoug Berger 	early_memtest((phys_addr_t)min_low_pfn << PAGE_SHIFT,
295071d184aSDoug Berger 		      (phys_addr_t)max_low_pfn << PAGE_SHIFT);
296d30eae47SVladimir Murzin 
297b7a69ac3SRussell King 	/*
298657e12fdSRussell King 	 * Sparsemem tries to allocate bootmem in memory_present(),
299657e12fdSRussell King 	 * so must be done after the fixed reservations
300657e12fdSRussell King 	 */
30114b5f54bSPeng Fan 	memblocks_present();
30290072059SRussell King 
303b7a69ac3SRussell King 	/*
304b7a69ac3SRussell King 	 * sparse_init() needs the bootmem allocator up and running.
305b7a69ac3SRussell King 	 */
306b7a69ac3SRussell King 	sparse_init();
307b7a69ac3SRussell King 
308b7a69ac3SRussell King 	/*
309be370302SRussell King 	 * Now free the memory - free_area_init_node needs
310b7a69ac3SRussell King 	 * the sparse mem_map arrays initialized by sparse_init()
311b7a69ac3SRussell King 	 * for memmap_init_zone(), otherwise all PFNs are invalid.
312b7a69ac3SRussell King 	 */
313071d184aSDoug Berger 	zone_sizes_init(min_low_pfn, max_low_pfn, max_pfn);
31490072059SRussell King }
31590072059SRussell King 
31654d52573SStephen Boyd /*
31754d52573SStephen Boyd  * Poison init memory with an undefined instruction (ARM) or a branch to an
31854d52573SStephen Boyd  * undefined instruction (Thumb).
31954d52573SStephen Boyd  */
32054d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count)
32154d52573SStephen Boyd {
32254d52573SStephen Boyd 	u32 *p = (u32 *)s;
323bf912d99SJamie Iles 	for (; count != 0; count -= 4)
32454d52573SStephen Boyd 		*p++ = 0xe7fddef0;
32554d52573SStephen Boyd }
32654d52573SStephen Boyd 
327a013053dSRussell King static inline void
328be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn)
329a013053dSRussell King {
330a013053dSRussell King 	struct page *start_pg, *end_pg;
33156bc6286SVitaly Andrianov 	phys_addr_t pg, pgend;
332a013053dSRussell King 
333a013053dSRussell King 	/*
334a013053dSRussell King 	 * Convert start_pfn/end_pfn to a struct page pointer.
335a013053dSRussell King 	 */
3363257f43dSCatalin Marinas 	start_pg = pfn_to_page(start_pfn - 1) + 1;
3379af386c8SWill Deacon 	end_pg = pfn_to_page(end_pfn - 1) + 1;
338a013053dSRussell King 
339a013053dSRussell King 	/*
340a013053dSRussell King 	 * Convert to physical addresses, and
341a013053dSRussell King 	 * round start upwards and end downwards.
342a013053dSRussell King 	 */
34356bc6286SVitaly Andrianov 	pg = PAGE_ALIGN(__pa(start_pg));
34456bc6286SVitaly Andrianov 	pgend = __pa(end_pg) & PAGE_MASK;
345a013053dSRussell King 
346a013053dSRussell King 	/*
347a013053dSRussell King 	 * If there are free pages between these,
348a013053dSRussell King 	 * free the section of the memmap array.
349a013053dSRussell King 	 */
350a013053dSRussell King 	if (pg < pgend)
351cfb66586SSantosh Shilimkar 		memblock_free_early(pg, pgend - pg);
352a013053dSRussell King }
353a013053dSRussell King 
354a013053dSRussell King /*
355a013053dSRussell King  * The mem_map array can get very big.  Free the unused area of the memory map.
356a013053dSRussell King  */
3571c2f87c2SLaura Abbott static void __init free_unused_memmap(void)
358a013053dSRussell King {
3591c2f87c2SLaura Abbott 	unsigned long start, prev_end = 0;
3601c2f87c2SLaura Abbott 	struct memblock_region *reg;
361a013053dSRussell King 
362a013053dSRussell King 	/*
3633260e529SMichael Bohan 	 * This relies on each bank being in address order.
3643260e529SMichael Bohan 	 * The banks are sorted previously in bootmem_init().
365a013053dSRussell King 	 */
3661c2f87c2SLaura Abbott 	for_each_memblock(memory, reg) {
3671c2f87c2SLaura Abbott 		start = memblock_region_memory_base_pfn(reg);
368a013053dSRussell King 
3699af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM
3709af386c8SWill Deacon 		/*
3719af386c8SWill Deacon 		 * Take care not to free memmap entries that don't exist
3729af386c8SWill Deacon 		 * due to SPARSEMEM sections which aren't present.
3739af386c8SWill Deacon 		 */
3741c2f87c2SLaura Abbott 		start = min(start,
3751c2f87c2SLaura Abbott 				 ALIGN(prev_end, PAGES_PER_SECTION));
376002ea9eeSLinus Walleij #else
377002ea9eeSLinus Walleij 		/*
378002ea9eeSLinus Walleij 		 * Align down here since the VM subsystem insists that the
379002ea9eeSLinus Walleij 		 * memmap entries are valid from the bank start aligned to
380002ea9eeSLinus Walleij 		 * MAX_ORDER_NR_PAGES.
381002ea9eeSLinus Walleij 		 */
3821c2f87c2SLaura Abbott 		start = round_down(start, MAX_ORDER_NR_PAGES);
3839af386c8SWill Deacon #endif
384a013053dSRussell King 		/*
385a013053dSRussell King 		 * If we had a previous bank, and there is a space
386a013053dSRussell King 		 * between the current bank and the previous, free it.
387a013053dSRussell King 		 */
3881c2f87c2SLaura Abbott 		if (prev_end && prev_end < start)
3891c2f87c2SLaura Abbott 			free_memmap(prev_end, start);
390a013053dSRussell King 
3913260e529SMichael Bohan 		/*
3923260e529SMichael Bohan 		 * Align up here since the VM subsystem insists that the
3933260e529SMichael Bohan 		 * memmap entries are valid from the bank end aligned to
3943260e529SMichael Bohan 		 * MAX_ORDER_NR_PAGES.
3953260e529SMichael Bohan 		 */
3961c2f87c2SLaura Abbott 		prev_end = ALIGN(memblock_region_memory_end_pfn(reg),
3971c2f87c2SLaura Abbott 				 MAX_ORDER_NR_PAGES);
398a013053dSRussell King 	}
3999af386c8SWill Deacon 
4009af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM
4011c2f87c2SLaura Abbott 	if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
4021c2f87c2SLaura Abbott 		free_memmap(prev_end,
4031c2f87c2SLaura Abbott 			    ALIGN(prev_end, PAGES_PER_SECTION));
4049af386c8SWill Deacon #endif
405a013053dSRussell King }
406a013053dSRussell King 
40783db0384SJiang Liu #ifdef CONFIG_HIGHMEM
40883db0384SJiang Liu static inline void free_area_high(unsigned long pfn, unsigned long end)
40983db0384SJiang Liu {
410dd6911efSJiang Liu 	for (; pfn < end; pfn++)
411dd6911efSJiang Liu 		free_highmem_page(pfn_to_page(pfn));
41283db0384SJiang Liu }
41383db0384SJiang Liu #endif
41483db0384SJiang Liu 
415d0e775afSRussell King static void __init free_highpages(void)
416d0e775afSRussell King {
417d0e775afSRussell King #ifdef CONFIG_HIGHMEM
41826ba47b1SSantosh Shilimkar 	unsigned long max_low = max_low_pfn;
419df4f14c7SRussell King 	struct memblock_region *mem, *res;
420d0e775afSRussell King 
421d0e775afSRussell King 	/* set highmem page free */
422df4f14c7SRussell King 	for_each_memblock(memory, mem) {
423df4f14c7SRussell King 		unsigned long start = memblock_region_memory_base_pfn(mem);
424df4f14c7SRussell King 		unsigned long end = memblock_region_memory_end_pfn(mem);
425df4f14c7SRussell King 
426df4f14c7SRussell King 		/* Ignore complete lowmem entries */
427df4f14c7SRussell King 		if (end <= max_low)
428df4f14c7SRussell King 			continue;
429df4f14c7SRussell King 
43009414d00SArd Biesheuvel 		if (memblock_is_nomap(mem))
43109414d00SArd Biesheuvel 			continue;
43209414d00SArd Biesheuvel 
433df4f14c7SRussell King 		/* Truncate partial highmem entries */
434df4f14c7SRussell King 		if (start < max_low)
435df4f14c7SRussell King 			start = max_low;
436df4f14c7SRussell King 
437df4f14c7SRussell King 		/* Find and exclude any reserved regions */
438df4f14c7SRussell King 		for_each_memblock(reserved, res) {
439df4f14c7SRussell King 			unsigned long res_start, res_end;
440df4f14c7SRussell King 
441df4f14c7SRussell King 			res_start = memblock_region_reserved_base_pfn(res);
442df4f14c7SRussell King 			res_end = memblock_region_reserved_end_pfn(res);
443df4f14c7SRussell King 
444df4f14c7SRussell King 			if (res_end < start)
445df4f14c7SRussell King 				continue;
446df4f14c7SRussell King 			if (res_start < start)
447df4f14c7SRussell King 				res_start = start;
448df4f14c7SRussell King 			if (res_start > end)
449df4f14c7SRussell King 				res_start = end;
450df4f14c7SRussell King 			if (res_end > end)
451df4f14c7SRussell King 				res_end = end;
452df4f14c7SRussell King 			if (res_start != start)
45383db0384SJiang Liu 				free_area_high(start, res_start);
454df4f14c7SRussell King 			start = res_end;
455df4f14c7SRussell King 			if (start == end)
456df4f14c7SRussell King 				break;
457df4f14c7SRussell King 		}
458df4f14c7SRussell King 
459df4f14c7SRussell King 		/* And now free anything which remains */
460df4f14c7SRussell King 		if (start < end)
46183db0384SJiang Liu 			free_area_high(start, end);
462d0e775afSRussell King 	}
463d0e775afSRussell King #endif
464d0e775afSRussell King }
465d0e775afSRussell King 
4661da177e4SLinus Torvalds /*
4671da177e4SLinus Torvalds  * mem_init() marks the free areas in the mem_map and tells us how much
4681da177e4SLinus Torvalds  * memory is free.  This is done after various parts of the system have
4691da177e4SLinus Torvalds  * claimed their memory after the kernel image.
4701da177e4SLinus Torvalds  */
4711da177e4SLinus Torvalds void __init mem_init(void)
4721da177e4SLinus Torvalds {
473ad3c7b18SChristoph Hellwig #ifdef CONFIG_ARM_LPAE
474ad3c7b18SChristoph Hellwig 	swiotlb_init(1);
475ad3c7b18SChristoph Hellwig #endif
476ad3c7b18SChristoph Hellwig 
477b3ba41f2SSantosh Shilimkar 	set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	/* this will put all unused low memory onto the freelists */
4801c2f87c2SLaura Abbott 	free_unused_memmap();
481c6ffc5caSMike Rapoport 	memblock_free_all();
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds #ifdef CONFIG_SA1111
4841da177e4SLinus Torvalds 	/* now that our DMA memory is actually so designated, we can free it */
485bfd65dd9SLinus Torvalds 	free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL);
4861da177e4SLinus Torvalds #endif
4871da177e4SLinus Torvalds 
488d0e775afSRussell King 	free_highpages();
4893835f6cbSNicolas Pitre 
4902450c973SJiang Liu 	mem_init_print_info(NULL);
4911da177e4SLinus Torvalds 
492a1839272SFenkart/Bostandzhyan 	/*
493a1839272SFenkart/Bostandzhyan 	 * Check boundaries twice: Some fundamental inconsistencies can
494a1839272SFenkart/Bostandzhyan 	 * be detected at build time already.
495a1839272SFenkart/Bostandzhyan 	 */
496a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU
497a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(TASK_SIZE				> MODULES_VADDR);
498a1839272SFenkart/Bostandzhyan 	BUG_ON(TASK_SIZE 				> MODULES_VADDR);
499a1839272SFenkart/Bostandzhyan #endif
500a1839272SFenkart/Bostandzhyan 
501a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
502a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
503a1839272SFenkart/Bostandzhyan 	BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE	> PAGE_OFFSET);
504a1839272SFenkart/Bostandzhyan #endif
5051da177e4SLinus Torvalds }
5061da177e4SLinus Torvalds 
5070f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
5081e6b4811SKees Cook struct section_perm {
50925362dc4SKees Cook 	const char *name;
5101e6b4811SKees Cook 	unsigned long start;
5111e6b4811SKees Cook 	unsigned long end;
5121e6b4811SKees Cook 	pmdval_t mask;
5131e6b4811SKees Cook 	pmdval_t prot;
51480d6b0c2SKees Cook 	pmdval_t clear;
5151e6b4811SKees Cook };
5161e6b4811SKees Cook 
51764ac2e74SKees Cook /* First section-aligned location at or after __start_rodata. */
51864ac2e74SKees Cook extern char __start_rodata_section_aligned[];
51964ac2e74SKees Cook 
52080d6b0c2SKees Cook static struct section_perm nx_perms[] = {
5211e6b4811SKees Cook 	/* Make pages tables, etc before _stext RW (set NX). */
5221e6b4811SKees Cook 	{
52325362dc4SKees Cook 		.name	= "pre-text NX",
5241e6b4811SKees Cook 		.start	= PAGE_OFFSET,
5251e6b4811SKees Cook 		.end	= (unsigned long)_stext,
5261e6b4811SKees Cook 		.mask	= ~PMD_SECT_XN,
5271e6b4811SKees Cook 		.prot	= PMD_SECT_XN,
5281e6b4811SKees Cook 	},
5291e6b4811SKees Cook 	/* Make init RW (set NX). */
5301e6b4811SKees Cook 	{
53125362dc4SKees Cook 		.name	= "init NX",
5321e6b4811SKees Cook 		.start	= (unsigned long)__init_begin,
5331e6b4811SKees Cook 		.end	= (unsigned long)_sdata,
5341e6b4811SKees Cook 		.mask	= ~PMD_SECT_XN,
5351e6b4811SKees Cook 		.prot	= PMD_SECT_XN,
5361e6b4811SKees Cook 	},
53780d6b0c2SKees Cook 	/* Make rodata NX (set RO in ro_perms below). */
53880d6b0c2SKees Cook 	{
53925362dc4SKees Cook 		.name	= "rodata NX",
54064ac2e74SKees Cook 		.start  = (unsigned long)__start_rodata_section_aligned,
54180d6b0c2SKees Cook 		.end    = (unsigned long)__init_begin,
54280d6b0c2SKees Cook 		.mask   = ~PMD_SECT_XN,
54380d6b0c2SKees Cook 		.prot   = PMD_SECT_XN,
54480d6b0c2SKees Cook 	},
5451e6b4811SKees Cook };
5461e6b4811SKees Cook 
54780d6b0c2SKees Cook static struct section_perm ro_perms[] = {
54880d6b0c2SKees Cook 	/* Make kernel code and rodata RX (set RO). */
54980d6b0c2SKees Cook 	{
55025362dc4SKees Cook 		.name	= "text/rodata RO",
55180d6b0c2SKees Cook 		.start  = (unsigned long)_stext,
55280d6b0c2SKees Cook 		.end    = (unsigned long)__init_begin,
55380d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE
554400eeffaSPhilip Derrin 		.mask   = ~(L_PMD_SECT_RDONLY | PMD_SECT_AP2),
555400eeffaSPhilip Derrin 		.prot   = L_PMD_SECT_RDONLY | PMD_SECT_AP2,
55680d6b0c2SKees Cook #else
55780d6b0c2SKees Cook 		.mask   = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE),
55880d6b0c2SKees Cook 		.prot   = PMD_SECT_APX | PMD_SECT_AP_WRITE,
55980d6b0c2SKees Cook 		.clear  = PMD_SECT_AP_WRITE,
56080d6b0c2SKees Cook #endif
56180d6b0c2SKees Cook 	},
56280d6b0c2SKees Cook };
56380d6b0c2SKees Cook 
5641e6b4811SKees Cook /*
5651e6b4811SKees Cook  * Updates section permissions only for the current mm (sections are
5661e6b4811SKees Cook  * copied into each mm). During startup, this is the init_mm. Is only
5671e6b4811SKees Cook  * safe to be called with preemption disabled, as under stop_machine().
5681e6b4811SKees Cook  */
5691e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask,
57008925c2fSLaura Abbott 				  pmdval_t prot, struct mm_struct *mm)
5711e6b4811SKees Cook {
5721e6b4811SKees Cook 	pmd_t *pmd;
5731e6b4811SKees Cook 
5741e6b4811SKees Cook 	pmd = pmd_offset(pud_offset(pgd_offset(mm, addr), addr), addr);
5751e6b4811SKees Cook 
5761e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE
5771e6b4811SKees Cook 	pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
5781e6b4811SKees Cook #else
5791e6b4811SKees Cook 	if (addr & SECTION_SIZE)
5801e6b4811SKees Cook 		pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot);
5811e6b4811SKees Cook 	else
5821e6b4811SKees Cook 		pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
5831e6b4811SKees Cook #endif
5841e6b4811SKees Cook 	flush_pmd_entry(pmd);
5851e6b4811SKees Cook 	local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE);
5861e6b4811SKees Cook }
5871e6b4811SKees Cook 
5881e6b4811SKees Cook /* Make sure extended page tables are in use. */
5891e6b4811SKees Cook static inline bool arch_has_strict_perms(void)
5901e6b4811SKees Cook {
5911e6b4811SKees Cook 	if (cpu_architecture() < CPU_ARCH_ARMv6)
5921e6b4811SKees Cook 		return false;
5931e6b4811SKees Cook 
5941e6b4811SKees Cook 	return !!(get_cr() & CR_XP);
5951e6b4811SKees Cook }
5961e6b4811SKees Cook 
597ea5379beSBen Dooks (Codethink) static void set_section_perms(struct section_perm *perms, int n, bool set,
59808925c2fSLaura Abbott 			      struct mm_struct *mm)
59908925c2fSLaura Abbott {
60008925c2fSLaura Abbott 	size_t i;
60108925c2fSLaura Abbott 	unsigned long addr;
60208925c2fSLaura Abbott 
60308925c2fSLaura Abbott 	if (!arch_has_strict_perms())
60408925c2fSLaura Abbott 		return;
60508925c2fSLaura Abbott 
60608925c2fSLaura Abbott 	for (i = 0; i < n; i++) {
60708925c2fSLaura Abbott 		if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) ||
60808925c2fSLaura Abbott 		    !IS_ALIGNED(perms[i].end, SECTION_SIZE)) {
60925362dc4SKees Cook 			pr_err("BUG: %s section %lx-%lx not aligned to %lx\n",
61025362dc4SKees Cook 				perms[i].name, perms[i].start, perms[i].end,
61108925c2fSLaura Abbott 				SECTION_SIZE);
61208925c2fSLaura Abbott 			continue;
6131e6b4811SKees Cook 		}
6141e6b4811SKees Cook 
61508925c2fSLaura Abbott 		for (addr = perms[i].start;
61608925c2fSLaura Abbott 		     addr < perms[i].end;
61708925c2fSLaura Abbott 		     addr += SECTION_SIZE)
61808925c2fSLaura Abbott 			section_update(addr, perms[i].mask,
61908925c2fSLaura Abbott 				set ? perms[i].prot : perms[i].clear, mm);
62008925c2fSLaura Abbott 	}
62108925c2fSLaura Abbott 
62208925c2fSLaura Abbott }
62308925c2fSLaura Abbott 
62411ce4b33SGrygorii Strashko /**
62511ce4b33SGrygorii Strashko  * update_sections_early intended to be called only through stop_machine
62611ce4b33SGrygorii Strashko  * framework and executed by only one CPU while all other CPUs will spin and
62711ce4b33SGrygorii Strashko  * wait, so no locking is required in this function.
62811ce4b33SGrygorii Strashko  */
62908925c2fSLaura Abbott static void update_sections_early(struct section_perm perms[], int n)
6301e6b4811SKees Cook {
63108925c2fSLaura Abbott 	struct task_struct *t, *s;
63208925c2fSLaura Abbott 
63308925c2fSLaura Abbott 	for_each_process(t) {
63408925c2fSLaura Abbott 		if (t->flags & PF_KTHREAD)
63508925c2fSLaura Abbott 			continue;
63608925c2fSLaura Abbott 		for_each_thread(t, s)
637c51bc12dSDoug Berger 			if (s->mm)
63808925c2fSLaura Abbott 				set_section_perms(perms, n, true, s->mm);
63908925c2fSLaura Abbott 	}
64008925c2fSLaura Abbott 	set_section_perms(perms, n, true, current->active_mm);
64108925c2fSLaura Abbott 	set_section_perms(perms, n, true, &init_mm);
64208925c2fSLaura Abbott }
64308925c2fSLaura Abbott 
64411ce4b33SGrygorii Strashko static int __fix_kernmem_perms(void *unused)
64508925c2fSLaura Abbott {
64608925c2fSLaura Abbott 	update_sections_early(nx_perms, ARRAY_SIZE(nx_perms));
64708925c2fSLaura Abbott 	return 0;
64808925c2fSLaura Abbott }
64908925c2fSLaura Abbott 
65011ce4b33SGrygorii Strashko static void fix_kernmem_perms(void)
65108925c2fSLaura Abbott {
65208925c2fSLaura Abbott 	stop_machine(__fix_kernmem_perms, NULL, NULL);
6531e6b4811SKees Cook }
65480d6b0c2SKees Cook 
65511ce4b33SGrygorii Strashko static int __mark_rodata_ro(void *unused)
65608925c2fSLaura Abbott {
65708925c2fSLaura Abbott 	update_sections_early(ro_perms, ARRAY_SIZE(ro_perms));
65808925c2fSLaura Abbott 	return 0;
65908925c2fSLaura Abbott }
66008925c2fSLaura Abbott 
661b4c7e2bdSSteven Rostedt (VMware) static int kernel_set_to_readonly __read_mostly;
662b4c7e2bdSSteven Rostedt (VMware) 
66380d6b0c2SKees Cook void mark_rodata_ro(void)
66480d6b0c2SKees Cook {
665b4c7e2bdSSteven Rostedt (VMware) 	kernel_set_to_readonly = 1;
66608925c2fSLaura Abbott 	stop_machine(__mark_rodata_ro, NULL, NULL);
667a8e53c15SJinbum Park 	debug_checkwx();
66880d6b0c2SKees Cook }
66980d6b0c2SKees Cook 
67080d6b0c2SKees Cook void set_kernel_text_rw(void)
67180d6b0c2SKees Cook {
672b4c7e2bdSSteven Rostedt (VMware) 	if (!kernel_set_to_readonly)
673b4c7e2bdSSteven Rostedt (VMware) 		return;
674b4c7e2bdSSteven Rostedt (VMware) 
67508925c2fSLaura Abbott 	set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), false,
67608925c2fSLaura Abbott 				current->active_mm);
67780d6b0c2SKees Cook }
67880d6b0c2SKees Cook 
67980d6b0c2SKees Cook void set_kernel_text_ro(void)
68080d6b0c2SKees Cook {
681b4c7e2bdSSteven Rostedt (VMware) 	if (!kernel_set_to_readonly)
682b4c7e2bdSSteven Rostedt (VMware) 		return;
683b4c7e2bdSSteven Rostedt (VMware) 
68408925c2fSLaura Abbott 	set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), true,
68508925c2fSLaura Abbott 				current->active_mm);
68680d6b0c2SKees Cook }
68780d6b0c2SKees Cook 
6881e6b4811SKees Cook #else
6891e6b4811SKees Cook static inline void fix_kernmem_perms(void) { }
6900f5bf6d0SLaura Abbott #endif /* CONFIG_STRICT_KERNEL_RWX */
6911e6b4811SKees Cook 
6921e6b4811SKees Cook void free_initmem(void)
6931e6b4811SKees Cook {
6941e6b4811SKees Cook 	fix_kernmem_perms();
695bc581770SLinus Walleij 
69654d52573SStephen Boyd 	poison_init_mem(__init_begin, __init_end - __init_begin);
6976db015e4SNicolas Pitre 	if (!machine_is_integrator() && !machine_is_cintegrator())
698dbe67df4SJiang Liu 		free_initmem_default(-1);
6991da177e4SLinus Torvalds }
7001da177e4SLinus Torvalds 
7011da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
7021da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end)
7031da177e4SLinus Torvalds {
704421520baSYalin Wang 	if (start == initrd_start)
705421520baSYalin Wang 		start = round_down(start, PAGE_SIZE);
706421520baSYalin Wang 	if (end == initrd_end)
707421520baSYalin Wang 		end = round_up(end, PAGE_SIZE);
708421520baSYalin Wang 
70954d52573SStephen Boyd 	poison_init_mem((void *)start, PAGE_ALIGN(end) - start);
710dbe67df4SJiang Liu 	free_reserved_area((void *)start, (void *)end, -1, "initrd");
7111da177e4SLinus Torvalds }
7121da177e4SLinus Torvalds #endif
713