xref: /openbmc/linux/arch/arm/mm/init.c (revision 11ce4b33)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  linux/arch/arm/mm/init.c
31da177e4SLinus Torvalds  *
490072059SRussell King  *  Copyright (C) 1995-2005 Russell King
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
71da177e4SLinus Torvalds  * it under the terms of the GNU General Public License version 2 as
81da177e4SLinus Torvalds  * published by the Free Software Foundation.
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds #include <linux/kernel.h>
111da177e4SLinus Torvalds #include <linux/errno.h>
121da177e4SLinus Torvalds #include <linux/swap.h>
131da177e4SLinus Torvalds #include <linux/init.h>
141da177e4SLinus Torvalds #include <linux/bootmem.h>
151da177e4SLinus Torvalds #include <linux/mman.h>
163f07c014SIngo Molnar #include <linux/sched/signal.h>
1729930025SIngo Molnar #include <linux/sched/task.h>
18dc28094bSPaul Gortmaker #include <linux/export.h>
191da177e4SLinus Torvalds #include <linux/nodemask.h>
201da177e4SLinus Torvalds #include <linux/initrd.h>
219eb8f674SGrant Likely #include <linux/of_fdt.h>
223835f6cbSNicolas Pitre #include <linux/highmem.h>
235a0e3ad6STejun Heo #include <linux/gfp.h>
242778f620SRussell King #include <linux/memblock.h>
25c7909509SMarek Szyprowski #include <linux/dma-contiguous.h>
26158e8bfeSAlessandro Rubini #include <linux/sizes.h>
2708925c2fSLaura Abbott #include <linux/stop_machine.h>
281da177e4SLinus Torvalds 
29b4b20ad8SRussell King #include <asm/cp15.h>
301da177e4SLinus Torvalds #include <asm/mach-types.h>
31716a3dc2SRussell King #include <asm/memblock.h>
32d2ca5f24SAfzal Mohammed #include <asm/memory.h>
3393c02ab4SGrant Likely #include <asm/prom.h>
3437efe642SRussell King #include <asm/sections.h>
351da177e4SLinus Torvalds #include <asm/setup.h>
361e6b4811SKees Cook #include <asm/system_info.h>
371da177e4SLinus Torvalds #include <asm/tlb.h>
38db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h>
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds #include <asm/mach/arch.h>
411da177e4SLinus Torvalds #include <asm/mach/map.h>
421da177e4SLinus Torvalds 
431b2e2b73SRussell King #include "mm.h"
441b2e2b73SRussell King 
45b4b20ad8SRussell King #ifdef CONFIG_CPU_CP15_MMU
46b4b20ad8SRussell King unsigned long __init __clear_cr(unsigned long mask)
47b4b20ad8SRussell King {
48b4b20ad8SRussell King 	cr_alignment = cr_alignment & ~mask;
49b4b20ad8SRussell King 	return cr_alignment;
50b4b20ad8SRussell King }
51b4b20ad8SRussell King #endif
52b4b20ad8SRussell King 
53de22cc6eSVitaly Andrianov static phys_addr_t phys_initrd_start __initdata = 0;
54012d1f4aSRussell King static unsigned long phys_initrd_size __initdata = 0;
55012d1f4aSRussell King 
562b0d8c25SJeremy Kerr static int __init early_initrd(char *p)
57012d1f4aSRussell King {
58de22cc6eSVitaly Andrianov 	phys_addr_t start;
59de22cc6eSVitaly Andrianov 	unsigned long size;
602b0d8c25SJeremy Kerr 	char *endp;
61012d1f4aSRussell King 
622b0d8c25SJeremy Kerr 	start = memparse(p, &endp);
632b0d8c25SJeremy Kerr 	if (*endp == ',') {
642b0d8c25SJeremy Kerr 		size = memparse(endp + 1, NULL);
65012d1f4aSRussell King 
66012d1f4aSRussell King 		phys_initrd_start = start;
67012d1f4aSRussell King 		phys_initrd_size = size;
68012d1f4aSRussell King 	}
692b0d8c25SJeremy Kerr 	return 0;
70012d1f4aSRussell King }
712b0d8c25SJeremy Kerr early_param("initrd", early_initrd);
72012d1f4aSRussell King 
73012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag)
74012d1f4aSRussell King {
754ed89f22SRussell King 	pr_warn("ATAG_INITRD is deprecated; "
76012d1f4aSRussell King 		"please update your bootloader.\n");
77012d1f4aSRussell King 	phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
78012d1f4aSRussell King 	phys_initrd_size = tag->u.initrd.size;
79012d1f4aSRussell King 	return 0;
80012d1f4aSRussell King }
81012d1f4aSRussell King 
82012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd);
83012d1f4aSRussell King 
84012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag)
85012d1f4aSRussell King {
86012d1f4aSRussell King 	phys_initrd_start = tag->u.initrd.start;
87012d1f4aSRussell King 	phys_initrd_size = tag->u.initrd.size;
88012d1f4aSRussell King 	return 0;
89012d1f4aSRussell King }
90012d1f4aSRussell King 
91012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2);
921da177e4SLinus Torvalds 
93f25b4b4cSRussell King static void __init find_limits(unsigned long *min, unsigned long *max_low,
94f25b4b4cSRussell King 			       unsigned long *max_high)
95dde5828fSRussell King {
961c2f87c2SLaura Abbott 	*max_low = PFN_DOWN(memblock_get_current_limit());
971c2f87c2SLaura Abbott 	*min = PFN_UP(memblock_start_of_DRAM());
981c2f87c2SLaura Abbott 	*max_high = PFN_DOWN(memblock_end_of_DRAM());
99dde5828fSRussell King }
100dde5828fSRussell King 
101be20902bSRussell King #ifdef CONFIG_ZONE_DMA
10265032018SNicolas Pitre 
103364230b9SRob Herring phys_addr_t arm_dma_zone_size __read_mostly;
10465032018SNicolas Pitre EXPORT_SYMBOL(arm_dma_zone_size);
10565032018SNicolas Pitre 
106022ae537SRussell King /*
107022ae537SRussell King  * The DMA mask corresponding to the maximum bus address allocatable
108022ae537SRussell King  * using GFP_DMA.  The default here places no restriction on DMA
109022ae537SRussell King  * allocations.  This must be the smallest DMA mask in the system,
110022ae537SRussell King  * so a successful GFP_DMA allocation will always satisfy this.
111022ae537SRussell King  */
1124986e5c7SMarek Szyprowski phys_addr_t arm_dma_limit;
1134dcfa600SRussell King unsigned long arm_dma_pfn_limit;
114022ae537SRussell King 
115be20902bSRussell King static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole,
116be20902bSRussell King 	unsigned long dma_size)
117be20902bSRussell King {
118be20902bSRussell King 	if (size[0] <= dma_size)
119be20902bSRussell King 		return;
120be20902bSRussell King 
121be20902bSRussell King 	size[ZONE_NORMAL] = size[0] - dma_size;
122be20902bSRussell King 	size[ZONE_DMA] = dma_size;
123be20902bSRussell King 	hole[ZONE_NORMAL] = hole[0];
124be20902bSRussell King 	hole[ZONE_DMA] = 0;
125be20902bSRussell King }
126be20902bSRussell King #endif
127be20902bSRussell King 
128ff69a4c8SRussell King void __init setup_dma_zone(const struct machine_desc *mdesc)
129c7909509SMarek Szyprowski {
130c7909509SMarek Szyprowski #ifdef CONFIG_ZONE_DMA
131c7909509SMarek Szyprowski 	if (mdesc->dma_zone_size) {
132c7909509SMarek Szyprowski 		arm_dma_zone_size = mdesc->dma_zone_size;
1336bcac805SRussell King 		arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1;
134c7909509SMarek Szyprowski 	} else
135c7909509SMarek Szyprowski 		arm_dma_limit = 0xffffffff;
1364dcfa600SRussell King 	arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT;
137c7909509SMarek Szyprowski #endif
138c7909509SMarek Szyprowski }
139c7909509SMarek Szyprowski 
14084f452b1SSantosh Shilimkar static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
141a2c54d2aSRussell King 	unsigned long max_high)
142b7a69ac3SRussell King {
143b7a69ac3SRussell King 	unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
144a2c54d2aSRussell King 	struct memblock_region *reg;
145b7a69ac3SRussell King 
14690072059SRussell King 	/*
147be370302SRussell King 	 * initialise the zones.
14890072059SRussell King 	 */
14990072059SRussell King 	memset(zone_size, 0, sizeof(zone_size));
15090072059SRussell King 
15190072059SRussell King 	/*
152be370302SRussell King 	 * The memory size has already been determined.  If we need
153be370302SRussell King 	 * to do anything fancy with the allocation of this memory
154be370302SRussell King 	 * to the zones, now is the time to do it.
15590072059SRussell King 	 */
156dde5828fSRussell King 	zone_size[0] = max_low - min;
157dde5828fSRussell King #ifdef CONFIG_HIGHMEM
158dde5828fSRussell King 	zone_size[ZONE_HIGHMEM] = max_high - max_low;
159dde5828fSRussell King #endif
16090072059SRussell King 
16190072059SRussell King 	/*
162be370302SRussell King 	 * Calculate the size of the holes.
163be370302SRussell King 	 *  holes = node_size - sum(bank_sizes)
16490072059SRussell King 	 */
165dde5828fSRussell King 	memcpy(zhole_size, zone_size, sizeof(zhole_size));
166a2c54d2aSRussell King 	for_each_memblock(memory, reg) {
167a2c54d2aSRussell King 		unsigned long start = memblock_region_memory_base_pfn(reg);
168a2c54d2aSRussell King 		unsigned long end = memblock_region_memory_end_pfn(reg);
169a2c54d2aSRussell King 
170a2c54d2aSRussell King 		if (start < max_low) {
171a2c54d2aSRussell King 			unsigned long low_end = min(end, max_low);
172a2c54d2aSRussell King 			zhole_size[0] -= low_end - start;
173a2c54d2aSRussell King 		}
174dde5828fSRussell King #ifdef CONFIG_HIGHMEM
175a2c54d2aSRussell King 		if (end > max_low) {
176a2c54d2aSRussell King 			unsigned long high_start = max(start, max_low);
177a2c54d2aSRussell King 			zhole_size[ZONE_HIGHMEM] -= end - high_start;
178a2c54d2aSRussell King 		}
179dde5828fSRussell King #endif
180dde5828fSRussell King 	}
18190072059SRussell King 
18265032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA
18390072059SRussell King 	/*
18490072059SRussell King 	 * Adjust the sizes according to any special requirements for
18590072059SRussell King 	 * this machine type.
18690072059SRussell King 	 */
187c7909509SMarek Szyprowski 	if (arm_dma_zone_size)
188be20902bSRussell King 		arm_adjust_dma_zone(zone_size, zhole_size,
18965032018SNicolas Pitre 			arm_dma_zone_size >> PAGE_SHIFT);
190be20902bSRussell King #endif
19190072059SRussell King 
192be370302SRussell King 	free_area_init_node(0, zone_size, min, zhole_size);
19390072059SRussell King }
19490072059SRussell King 
1957b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID
196b7cfda9fSRussell King int pfn_valid(unsigned long pfn)
197b7cfda9fSRussell King {
19809414d00SArd Biesheuvel 	return memblock_is_map_memory(__pfn_to_phys(pfn));
199b7cfda9fSRussell King }
200b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid);
2017b7bf499SWill Deacon #endif
202657e12fdSRussell King 
2037b7bf499SWill Deacon #ifndef CONFIG_SPARSEMEM
20414904927SStephen Boyd static void __init arm_memory_present(void)
205657e12fdSRussell King {
206657e12fdSRussell King }
207657e12fdSRussell King #else
20814904927SStephen Boyd static void __init arm_memory_present(void)
209657e12fdSRussell King {
210719c1514SBenjamin Herrenschmidt 	struct memblock_region *reg;
211719c1514SBenjamin Herrenschmidt 
2127c996361SYinghai Lu 	for_each_memblock(memory, reg)
213c7fc2de0SYinghai Lu 		memory_present(0, memblock_region_memory_base_pfn(reg),
214c7fc2de0SYinghai Lu 			       memblock_region_memory_end_pfn(reg));
215657e12fdSRussell King }
216b7cfda9fSRussell King #endif
217b7cfda9fSRussell King 
218716a3dc2SRussell King static bool arm_memblock_steal_permitted = true;
219716a3dc2SRussell King 
220bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align)
221716a3dc2SRussell King {
222716a3dc2SRussell King 	phys_addr_t phys;
223716a3dc2SRussell King 
224716a3dc2SRussell King 	BUG_ON(!arm_memblock_steal_permitted);
225716a3dc2SRussell King 
2267ac68a4cSRussell King 	phys = memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE);
227716a3dc2SRussell King 	memblock_free(phys, size);
228716a3dc2SRussell King 	memblock_remove(phys, size);
229716a3dc2SRussell King 
230716a3dc2SRussell King 	return phys;
231716a3dc2SRussell King }
232716a3dc2SRussell King 
23339286248SRussell King static void __init arm_initrd_init(void)
2342778f620SRussell King {
2352778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD
236cdcc5fa0SRussell King 	phys_addr_t start;
237cdcc5fa0SRussell King 	unsigned long size;
238cdcc5fa0SRussell King 
23965939301SRob Herring 	/* FDT scan will populate initrd_start */
2404c235cb9SBen Peddell 	if (initrd_start && !phys_initrd_size) {
24165939301SRob Herring 		phys_initrd_start = __virt_to_phys(initrd_start);
24265939301SRob Herring 		phys_initrd_size = initrd_end - initrd_start;
24365939301SRob Herring 	}
24468b32f36SRussell King 
2454c235cb9SBen Peddell 	initrd_start = initrd_end = 0;
24668b32f36SRussell King 
24768b32f36SRussell King 	if (!phys_initrd_size)
24868b32f36SRussell King 		return;
24968b32f36SRussell King 
250cdcc5fa0SRussell King 	/*
251cdcc5fa0SRussell King 	 * Round the memory region to page boundaries as per free_initrd_mem()
252cdcc5fa0SRussell King 	 * This allows us to detect whether the pages overlapping the initrd
253cdcc5fa0SRussell King 	 * are in use, but more importantly, reserves the entire set of pages
254cdcc5fa0SRussell King 	 * as we don't want these pages allocated for other purposes.
255cdcc5fa0SRussell King 	 */
256cdcc5fa0SRussell King 	start = round_down(phys_initrd_start, PAGE_SIZE);
257cdcc5fa0SRussell King 	size = phys_initrd_size + (phys_initrd_start - start);
258cdcc5fa0SRussell King 	size = round_up(size, PAGE_SIZE);
259cdcc5fa0SRussell King 
260cdcc5fa0SRussell King 	if (!memblock_is_region_memory(start, size)) {
261de22cc6eSVitaly Andrianov 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n",
262cdcc5fa0SRussell King 		       (u64)start, size);
26368b32f36SRussell King 		return;
2648f4b8c76SRussell King 	}
26568b32f36SRussell King 
266cdcc5fa0SRussell King 	if (memblock_is_region_reserved(start, size)) {
267de22cc6eSVitaly Andrianov 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n",
268cdcc5fa0SRussell King 		       (u64)start, size);
26968b32f36SRussell King 		return;
270b0a2679dSRussell King 	}
27168b32f36SRussell King 
272cdcc5fa0SRussell King 	memblock_reserve(start, size);
2732778f620SRussell King 
2742778f620SRussell King 	/* Now convert initrd to virtual addresses */
2752778f620SRussell King 	initrd_start = __phys_to_virt(phys_initrd_start);
2762778f620SRussell King 	initrd_end = initrd_start + phys_initrd_size;
2772778f620SRussell King #endif
27839286248SRussell King }
27939286248SRussell King 
28039286248SRussell King void __init arm_memblock_init(const struct machine_desc *mdesc)
28139286248SRussell King {
28239286248SRussell King 	/* Register the kernel text, kernel data and initrd with memblock. */
28339286248SRussell King 	memblock_reserve(__pa(KERNEL_START), KERNEL_END - KERNEL_START);
28439286248SRussell King 
28539286248SRussell King 	arm_initrd_init();
2862778f620SRussell King 
2872778f620SRussell King 	arm_mm_memblock_reserve();
2882778f620SRussell King 
2898d717a52SRussell King 	/* reserve any platform specific memblock areas */
2908d717a52SRussell King 	if (mdesc->reserve)
2918d717a52SRussell King 		mdesc->reserve();
2928d717a52SRussell King 
29324bbd929SArd Biesheuvel 	early_init_fdt_reserve_self();
294bcedb5f9SMarek Szyprowski 	early_init_fdt_scan_reserved_mem();
295bcedb5f9SMarek Szyprowski 
29699a468d7SGeorge G. Davis 	/* reserve memory for DMA contiguous allocations */
29795b0e655SMarek Szyprowski 	dma_contiguous_reserve(arm_dma_limit);
298c7909509SMarek Szyprowski 
299716a3dc2SRussell King 	arm_memblock_steal_permitted = false;
3002778f620SRussell King 	memblock_dump_all();
3012778f620SRussell King }
3022778f620SRussell King 
3038d717a52SRussell King void __init bootmem_init(void)
30490072059SRussell King {
305dde5828fSRussell King 	unsigned long min, max_low, max_high;
30690072059SRussell King 
3078e58caefSGrygorii Strashko 	memblock_allow_resize();
308dde5828fSRussell King 	max_low = max_high = 0;
309dde5828fSRussell King 
310f25b4b4cSRussell King 	find_limits(&min, &max_low, &max_high);
311dde5828fSRussell King 
312d30eae47SVladimir Murzin 	early_memtest((phys_addr_t)min << PAGE_SHIFT,
313d30eae47SVladimir Murzin 		      (phys_addr_t)max_low << PAGE_SHIFT);
314d30eae47SVladimir Murzin 
315b7a69ac3SRussell King 	/*
316657e12fdSRussell King 	 * Sparsemem tries to allocate bootmem in memory_present(),
317657e12fdSRussell King 	 * so must be done after the fixed reservations
318657e12fdSRussell King 	 */
319eda2e5dcSRussell King 	arm_memory_present();
32090072059SRussell King 
321b7a69ac3SRussell King 	/*
322b7a69ac3SRussell King 	 * sparse_init() needs the bootmem allocator up and running.
323b7a69ac3SRussell King 	 */
324b7a69ac3SRussell King 	sparse_init();
325b7a69ac3SRussell King 
326b7a69ac3SRussell King 	/*
327be370302SRussell King 	 * Now free the memory - free_area_init_node needs
328b7a69ac3SRussell King 	 * the sparse mem_map arrays initialized by sparse_init()
329b7a69ac3SRussell King 	 * for memmap_init_zone(), otherwise all PFNs are invalid.
330b7a69ac3SRussell King 	 */
33184f452b1SSantosh Shilimkar 	zone_sizes_init(min, max_low, max_high);
332b7a69ac3SRussell King 
33390072059SRussell King 	/*
33490072059SRussell King 	 * This doesn't seem to be used by the Linux memory manager any
33590072059SRussell King 	 * more, but is used by ll_rw_block.  If we can get rid of it, we
33690072059SRussell King 	 * also get rid of some of the stuff above as well.
33790072059SRussell King 	 */
33826ba47b1SSantosh Shilimkar 	min_low_pfn = min;
33926ba47b1SSantosh Shilimkar 	max_low_pfn = max_low;
34026ba47b1SSantosh Shilimkar 	max_pfn = max_high;
34190072059SRussell King }
34290072059SRussell King 
34354d52573SStephen Boyd /*
34454d52573SStephen Boyd  * Poison init memory with an undefined instruction (ARM) or a branch to an
34554d52573SStephen Boyd  * undefined instruction (Thumb).
34654d52573SStephen Boyd  */
34754d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count)
34854d52573SStephen Boyd {
34954d52573SStephen Boyd 	u32 *p = (u32 *)s;
350bf912d99SJamie Iles 	for (; count != 0; count -= 4)
35154d52573SStephen Boyd 		*p++ = 0xe7fddef0;
35254d52573SStephen Boyd }
35354d52573SStephen Boyd 
354a013053dSRussell King static inline void
355be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn)
356a013053dSRussell King {
357a013053dSRussell King 	struct page *start_pg, *end_pg;
35856bc6286SVitaly Andrianov 	phys_addr_t pg, pgend;
359a013053dSRussell King 
360a013053dSRussell King 	/*
361a013053dSRussell King 	 * Convert start_pfn/end_pfn to a struct page pointer.
362a013053dSRussell King 	 */
3633257f43dSCatalin Marinas 	start_pg = pfn_to_page(start_pfn - 1) + 1;
3649af386c8SWill Deacon 	end_pg = pfn_to_page(end_pfn - 1) + 1;
365a013053dSRussell King 
366a013053dSRussell King 	/*
367a013053dSRussell King 	 * Convert to physical addresses, and
368a013053dSRussell King 	 * round start upwards and end downwards.
369a013053dSRussell King 	 */
37056bc6286SVitaly Andrianov 	pg = PAGE_ALIGN(__pa(start_pg));
37156bc6286SVitaly Andrianov 	pgend = __pa(end_pg) & PAGE_MASK;
372a013053dSRussell King 
373a013053dSRussell King 	/*
374a013053dSRussell King 	 * If there are free pages between these,
375a013053dSRussell King 	 * free the section of the memmap array.
376a013053dSRussell King 	 */
377a013053dSRussell King 	if (pg < pgend)
378cfb66586SSantosh Shilimkar 		memblock_free_early(pg, pgend - pg);
379a013053dSRussell King }
380a013053dSRussell King 
381a013053dSRussell King /*
382a013053dSRussell King  * The mem_map array can get very big.  Free the unused area of the memory map.
383a013053dSRussell King  */
3841c2f87c2SLaura Abbott static void __init free_unused_memmap(void)
385a013053dSRussell King {
3861c2f87c2SLaura Abbott 	unsigned long start, prev_end = 0;
3871c2f87c2SLaura Abbott 	struct memblock_region *reg;
388a013053dSRussell King 
389a013053dSRussell King 	/*
3903260e529SMichael Bohan 	 * This relies on each bank being in address order.
3913260e529SMichael Bohan 	 * The banks are sorted previously in bootmem_init().
392a013053dSRussell King 	 */
3931c2f87c2SLaura Abbott 	for_each_memblock(memory, reg) {
3941c2f87c2SLaura Abbott 		start = memblock_region_memory_base_pfn(reg);
395a013053dSRussell King 
3969af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM
3979af386c8SWill Deacon 		/*
3989af386c8SWill Deacon 		 * Take care not to free memmap entries that don't exist
3999af386c8SWill Deacon 		 * due to SPARSEMEM sections which aren't present.
4009af386c8SWill Deacon 		 */
4011c2f87c2SLaura Abbott 		start = min(start,
4021c2f87c2SLaura Abbott 				 ALIGN(prev_end, PAGES_PER_SECTION));
403002ea9eeSLinus Walleij #else
404002ea9eeSLinus Walleij 		/*
405002ea9eeSLinus Walleij 		 * Align down here since the VM subsystem insists that the
406002ea9eeSLinus Walleij 		 * memmap entries are valid from the bank start aligned to
407002ea9eeSLinus Walleij 		 * MAX_ORDER_NR_PAGES.
408002ea9eeSLinus Walleij 		 */
4091c2f87c2SLaura Abbott 		start = round_down(start, MAX_ORDER_NR_PAGES);
4109af386c8SWill Deacon #endif
411a013053dSRussell King 		/*
412a013053dSRussell King 		 * If we had a previous bank, and there is a space
413a013053dSRussell King 		 * between the current bank and the previous, free it.
414a013053dSRussell King 		 */
4151c2f87c2SLaura Abbott 		if (prev_end && prev_end < start)
4161c2f87c2SLaura Abbott 			free_memmap(prev_end, start);
417a013053dSRussell King 
4183260e529SMichael Bohan 		/*
4193260e529SMichael Bohan 		 * Align up here since the VM subsystem insists that the
4203260e529SMichael Bohan 		 * memmap entries are valid from the bank end aligned to
4213260e529SMichael Bohan 		 * MAX_ORDER_NR_PAGES.
4223260e529SMichael Bohan 		 */
4231c2f87c2SLaura Abbott 		prev_end = ALIGN(memblock_region_memory_end_pfn(reg),
4241c2f87c2SLaura Abbott 				 MAX_ORDER_NR_PAGES);
425a013053dSRussell King 	}
4269af386c8SWill Deacon 
4279af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM
4281c2f87c2SLaura Abbott 	if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
4291c2f87c2SLaura Abbott 		free_memmap(prev_end,
4301c2f87c2SLaura Abbott 			    ALIGN(prev_end, PAGES_PER_SECTION));
4319af386c8SWill Deacon #endif
432a013053dSRussell King }
433a013053dSRussell King 
43483db0384SJiang Liu #ifdef CONFIG_HIGHMEM
43583db0384SJiang Liu static inline void free_area_high(unsigned long pfn, unsigned long end)
43683db0384SJiang Liu {
437dd6911efSJiang Liu 	for (; pfn < end; pfn++)
438dd6911efSJiang Liu 		free_highmem_page(pfn_to_page(pfn));
43983db0384SJiang Liu }
44083db0384SJiang Liu #endif
44183db0384SJiang Liu 
442d0e775afSRussell King static void __init free_highpages(void)
443d0e775afSRussell King {
444d0e775afSRussell King #ifdef CONFIG_HIGHMEM
44526ba47b1SSantosh Shilimkar 	unsigned long max_low = max_low_pfn;
446df4f14c7SRussell King 	struct memblock_region *mem, *res;
447d0e775afSRussell King 
448d0e775afSRussell King 	/* set highmem page free */
449df4f14c7SRussell King 	for_each_memblock(memory, mem) {
450df4f14c7SRussell King 		unsigned long start = memblock_region_memory_base_pfn(mem);
451df4f14c7SRussell King 		unsigned long end = memblock_region_memory_end_pfn(mem);
452df4f14c7SRussell King 
453df4f14c7SRussell King 		/* Ignore complete lowmem entries */
454df4f14c7SRussell King 		if (end <= max_low)
455df4f14c7SRussell King 			continue;
456df4f14c7SRussell King 
45709414d00SArd Biesheuvel 		if (memblock_is_nomap(mem))
45809414d00SArd Biesheuvel 			continue;
45909414d00SArd Biesheuvel 
460df4f14c7SRussell King 		/* Truncate partial highmem entries */
461df4f14c7SRussell King 		if (start < max_low)
462df4f14c7SRussell King 			start = max_low;
463df4f14c7SRussell King 
464df4f14c7SRussell King 		/* Find and exclude any reserved regions */
465df4f14c7SRussell King 		for_each_memblock(reserved, res) {
466df4f14c7SRussell King 			unsigned long res_start, res_end;
467df4f14c7SRussell King 
468df4f14c7SRussell King 			res_start = memblock_region_reserved_base_pfn(res);
469df4f14c7SRussell King 			res_end = memblock_region_reserved_end_pfn(res);
470df4f14c7SRussell King 
471df4f14c7SRussell King 			if (res_end < start)
472df4f14c7SRussell King 				continue;
473df4f14c7SRussell King 			if (res_start < start)
474df4f14c7SRussell King 				res_start = start;
475df4f14c7SRussell King 			if (res_start > end)
476df4f14c7SRussell King 				res_start = end;
477df4f14c7SRussell King 			if (res_end > end)
478df4f14c7SRussell King 				res_end = end;
479df4f14c7SRussell King 			if (res_start != start)
48083db0384SJiang Liu 				free_area_high(start, res_start);
481df4f14c7SRussell King 			start = res_end;
482df4f14c7SRussell King 			if (start == end)
483df4f14c7SRussell King 				break;
484df4f14c7SRussell King 		}
485df4f14c7SRussell King 
486df4f14c7SRussell King 		/* And now free anything which remains */
487df4f14c7SRussell King 		if (start < end)
48883db0384SJiang Liu 			free_area_high(start, end);
489d0e775afSRussell King 	}
490d0e775afSRussell King #endif
491d0e775afSRussell King }
492d0e775afSRussell King 
4931da177e4SLinus Torvalds /*
4941da177e4SLinus Torvalds  * mem_init() marks the free areas in the mem_map and tells us how much
4951da177e4SLinus Torvalds  * memory is free.  This is done after various parts of the system have
4961da177e4SLinus Torvalds  * claimed their memory after the kernel image.
4971da177e4SLinus Torvalds  */
4981da177e4SLinus Torvalds void __init mem_init(void)
4991da177e4SLinus Torvalds {
5001dbd30e9SLinus Walleij #ifdef CONFIG_HAVE_TCM
5011dbd30e9SLinus Walleij 	/* These pointers are filled in on TCM detection */
5021dbd30e9SLinus Walleij 	extern u32 dtcm_end;
5031dbd30e9SLinus Walleij 	extern u32 itcm_end;
5041dbd30e9SLinus Walleij #endif
5051da177e4SLinus Torvalds 
506b3ba41f2SSantosh Shilimkar 	set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	/* this will put all unused low memory onto the freelists */
5091c2f87c2SLaura Abbott 	free_unused_memmap();
5100c988534SJiang Liu 	free_all_bootmem();
5111da177e4SLinus Torvalds 
5121da177e4SLinus Torvalds #ifdef CONFIG_SA1111
5131da177e4SLinus Torvalds 	/* now that our DMA memory is actually so designated, we can free it */
514bfd65dd9SLinus Torvalds 	free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL);
5151da177e4SLinus Torvalds #endif
5161da177e4SLinus Torvalds 
517d0e775afSRussell King 	free_highpages();
5183835f6cbSNicolas Pitre 
5192450c973SJiang Liu 	mem_init_print_info(NULL);
5201da177e4SLinus Torvalds 
521db9ef1afSFenkart/Bostandzhyan #define MLK(b, t) b, t, ((t) - (b)) >> 10
522db9ef1afSFenkart/Bostandzhyan #define MLM(b, t) b, t, ((t) - (b)) >> 20
523db9ef1afSFenkart/Bostandzhyan #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
524db9ef1afSFenkart/Bostandzhyan 
5254ed89f22SRussell King 	pr_notice("Virtual kernel memory layout:\n"
526db9ef1afSFenkart/Bostandzhyan 			"    vector  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
52707d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM
52807d2a5c7SLinus Walleij 			"    DTCM    : 0x%08lx - 0x%08lx   (%4ld kB)\n"
52907d2a5c7SLinus Walleij 			"    ITCM    : 0x%08lx - 0x%08lx   (%4ld kB)\n"
53007d2a5c7SLinus Walleij #endif
531db9ef1afSFenkart/Bostandzhyan 			"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
532db9ef1afSFenkart/Bostandzhyan 			"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
533db9ef1afSFenkart/Bostandzhyan 			"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
534db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
535db9ef1afSFenkart/Bostandzhyan 			"    pkmap   : 0x%08lx - 0x%08lx   (%4ld MB)\n"
536db9ef1afSFenkart/Bostandzhyan #endif
537d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES
538db9ef1afSFenkart/Bostandzhyan 			"    modules : 0x%08lx - 0x%08lx   (%4ld MB)\n"
539d9277d51SUwe Kleine-König #endif
540178c3dfeSRussell King 			"      .text : 0x%p" " - 0x%p" "   (%4td kB)\n"
541178c3dfeSRussell King 			"      .init : 0x%p" " - 0x%p" "   (%4td kB)\n"
542178c3dfeSRussell King 			"      .data : 0x%p" " - 0x%p" "   (%4td kB)\n"
543178c3dfeSRussell King 			"       .bss : 0x%p" " - 0x%p" "   (%4td kB)\n",
544db9ef1afSFenkart/Bostandzhyan 
545d2ca5f24SAfzal Mohammed 			MLK(VECTORS_BASE, VECTORS_BASE + PAGE_SIZE),
54607d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM
5471dbd30e9SLinus Walleij 			MLK(DTCM_OFFSET, (unsigned long) dtcm_end),
5481dbd30e9SLinus Walleij 			MLK(ITCM_OFFSET, (unsigned long) itcm_end),
54907d2a5c7SLinus Walleij #endif
550b615bbbfSMark Salter 			MLK(FIXADDR_START, FIXADDR_END),
551c931b4f6SFenkart/Bostandzhyan 			MLM(VMALLOC_START, VMALLOC_END),
552db9ef1afSFenkart/Bostandzhyan 			MLM(PAGE_OFFSET, (unsigned long)high_memory),
553db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
554db9ef1afSFenkart/Bostandzhyan 			MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
555db9ef1afSFenkart/Bostandzhyan 				(PAGE_SIZE)),
556db9ef1afSFenkart/Bostandzhyan #endif
557d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES
558db9ef1afSFenkart/Bostandzhyan 			MLM(MODULES_VADDR, MODULES_END),
559d9277d51SUwe Kleine-König #endif
560db9ef1afSFenkart/Bostandzhyan 
561db9ef1afSFenkart/Bostandzhyan 			MLK_ROUNDUP(_text, _etext),
5623835d69aSRussell King 			MLK_ROUNDUP(__init_begin, __init_end),
56345f6d7e0SRabin Vincent 			MLK_ROUNDUP(_sdata, _edata),
56445f6d7e0SRabin Vincent 			MLK_ROUNDUP(__bss_start, __bss_stop));
565db9ef1afSFenkart/Bostandzhyan 
566db9ef1afSFenkart/Bostandzhyan #undef MLK
567db9ef1afSFenkart/Bostandzhyan #undef MLM
568db9ef1afSFenkart/Bostandzhyan #undef MLK_ROUNDUP
569db9ef1afSFenkart/Bostandzhyan 
570a1839272SFenkart/Bostandzhyan 	/*
571a1839272SFenkart/Bostandzhyan 	 * Check boundaries twice: Some fundamental inconsistencies can
572a1839272SFenkart/Bostandzhyan 	 * be detected at build time already.
573a1839272SFenkart/Bostandzhyan 	 */
574a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU
575a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(TASK_SIZE				> MODULES_VADDR);
576a1839272SFenkart/Bostandzhyan 	BUG_ON(TASK_SIZE 				> MODULES_VADDR);
577a1839272SFenkart/Bostandzhyan #endif
578a1839272SFenkart/Bostandzhyan 
579a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
580a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
581a1839272SFenkart/Bostandzhyan 	BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE	> PAGE_OFFSET);
582a1839272SFenkart/Bostandzhyan #endif
583a1839272SFenkart/Bostandzhyan 
5842450c973SJiang Liu 	if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
5851da177e4SLinus Torvalds 		extern int sysctl_overcommit_memory;
5861da177e4SLinus Torvalds 		/*
5871da177e4SLinus Torvalds 		 * On a machine this small we won't get
5881da177e4SLinus Torvalds 		 * anywhere without overcommit, so turn
5891da177e4SLinus Torvalds 		 * it on by default.
5901da177e4SLinus Torvalds 		 */
5911da177e4SLinus Torvalds 		sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
5921da177e4SLinus Torvalds 	}
5931da177e4SLinus Torvalds }
5941da177e4SLinus Torvalds 
5950f5bf6d0SLaura Abbott #ifdef CONFIG_STRICT_KERNEL_RWX
5961e6b4811SKees Cook struct section_perm {
59725362dc4SKees Cook 	const char *name;
5981e6b4811SKees Cook 	unsigned long start;
5991e6b4811SKees Cook 	unsigned long end;
6001e6b4811SKees Cook 	pmdval_t mask;
6011e6b4811SKees Cook 	pmdval_t prot;
60280d6b0c2SKees Cook 	pmdval_t clear;
6031e6b4811SKees Cook };
6041e6b4811SKees Cook 
60564ac2e74SKees Cook /* First section-aligned location at or after __start_rodata. */
60664ac2e74SKees Cook extern char __start_rodata_section_aligned[];
60764ac2e74SKees Cook 
60880d6b0c2SKees Cook static struct section_perm nx_perms[] = {
6091e6b4811SKees Cook 	/* Make pages tables, etc before _stext RW (set NX). */
6101e6b4811SKees Cook 	{
61125362dc4SKees Cook 		.name	= "pre-text NX",
6121e6b4811SKees Cook 		.start	= PAGE_OFFSET,
6131e6b4811SKees Cook 		.end	= (unsigned long)_stext,
6141e6b4811SKees Cook 		.mask	= ~PMD_SECT_XN,
6151e6b4811SKees Cook 		.prot	= PMD_SECT_XN,
6161e6b4811SKees Cook 	},
6171e6b4811SKees Cook 	/* Make init RW (set NX). */
6181e6b4811SKees Cook 	{
61925362dc4SKees Cook 		.name	= "init NX",
6201e6b4811SKees Cook 		.start	= (unsigned long)__init_begin,
6211e6b4811SKees Cook 		.end	= (unsigned long)_sdata,
6221e6b4811SKees Cook 		.mask	= ~PMD_SECT_XN,
6231e6b4811SKees Cook 		.prot	= PMD_SECT_XN,
6241e6b4811SKees Cook 	},
62580d6b0c2SKees Cook 	/* Make rodata NX (set RO in ro_perms below). */
62680d6b0c2SKees Cook 	{
62725362dc4SKees Cook 		.name	= "rodata NX",
62864ac2e74SKees Cook 		.start  = (unsigned long)__start_rodata_section_aligned,
62980d6b0c2SKees Cook 		.end    = (unsigned long)__init_begin,
63080d6b0c2SKees Cook 		.mask   = ~PMD_SECT_XN,
63180d6b0c2SKees Cook 		.prot   = PMD_SECT_XN,
63280d6b0c2SKees Cook 	},
6331e6b4811SKees Cook };
6341e6b4811SKees Cook 
63580d6b0c2SKees Cook static struct section_perm ro_perms[] = {
63680d6b0c2SKees Cook 	/* Make kernel code and rodata RX (set RO). */
63780d6b0c2SKees Cook 	{
63825362dc4SKees Cook 		.name	= "text/rodata RO",
63980d6b0c2SKees Cook 		.start  = (unsigned long)_stext,
64080d6b0c2SKees Cook 		.end    = (unsigned long)__init_begin,
64180d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE
6421e347922SVictor Kamensky 		.mask   = ~L_PMD_SECT_RDONLY,
6431e347922SVictor Kamensky 		.prot   = L_PMD_SECT_RDONLY,
64480d6b0c2SKees Cook #else
64580d6b0c2SKees Cook 		.mask   = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE),
64680d6b0c2SKees Cook 		.prot   = PMD_SECT_APX | PMD_SECT_AP_WRITE,
64780d6b0c2SKees Cook 		.clear  = PMD_SECT_AP_WRITE,
64880d6b0c2SKees Cook #endif
64980d6b0c2SKees Cook 	},
65080d6b0c2SKees Cook };
65180d6b0c2SKees Cook 
6521e6b4811SKees Cook /*
6531e6b4811SKees Cook  * Updates section permissions only for the current mm (sections are
6541e6b4811SKees Cook  * copied into each mm). During startup, this is the init_mm. Is only
6551e6b4811SKees Cook  * safe to be called with preemption disabled, as under stop_machine().
6561e6b4811SKees Cook  */
6571e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask,
65808925c2fSLaura Abbott 				  pmdval_t prot, struct mm_struct *mm)
6591e6b4811SKees Cook {
6601e6b4811SKees Cook 	pmd_t *pmd;
6611e6b4811SKees Cook 
6621e6b4811SKees Cook 	pmd = pmd_offset(pud_offset(pgd_offset(mm, addr), addr), addr);
6631e6b4811SKees Cook 
6641e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE
6651e6b4811SKees Cook 	pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
6661e6b4811SKees Cook #else
6671e6b4811SKees Cook 	if (addr & SECTION_SIZE)
6681e6b4811SKees Cook 		pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot);
6691e6b4811SKees Cook 	else
6701e6b4811SKees Cook 		pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
6711e6b4811SKees Cook #endif
6721e6b4811SKees Cook 	flush_pmd_entry(pmd);
6731e6b4811SKees Cook 	local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE);
6741e6b4811SKees Cook }
6751e6b4811SKees Cook 
6761e6b4811SKees Cook /* Make sure extended page tables are in use. */
6771e6b4811SKees Cook static inline bool arch_has_strict_perms(void)
6781e6b4811SKees Cook {
6791e6b4811SKees Cook 	if (cpu_architecture() < CPU_ARCH_ARMv6)
6801e6b4811SKees Cook 		return false;
6811e6b4811SKees Cook 
6821e6b4811SKees Cook 	return !!(get_cr() & CR_XP);
6831e6b4811SKees Cook }
6841e6b4811SKees Cook 
68508925c2fSLaura Abbott void set_section_perms(struct section_perm *perms, int n, bool set,
68608925c2fSLaura Abbott 			struct mm_struct *mm)
68708925c2fSLaura Abbott {
68808925c2fSLaura Abbott 	size_t i;
68908925c2fSLaura Abbott 	unsigned long addr;
69008925c2fSLaura Abbott 
69108925c2fSLaura Abbott 	if (!arch_has_strict_perms())
69208925c2fSLaura Abbott 		return;
69308925c2fSLaura Abbott 
69408925c2fSLaura Abbott 	for (i = 0; i < n; i++) {
69508925c2fSLaura Abbott 		if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) ||
69608925c2fSLaura Abbott 		    !IS_ALIGNED(perms[i].end, SECTION_SIZE)) {
69725362dc4SKees Cook 			pr_err("BUG: %s section %lx-%lx not aligned to %lx\n",
69825362dc4SKees Cook 				perms[i].name, perms[i].start, perms[i].end,
69908925c2fSLaura Abbott 				SECTION_SIZE);
70008925c2fSLaura Abbott 			continue;
7011e6b4811SKees Cook 		}
7021e6b4811SKees Cook 
70308925c2fSLaura Abbott 		for (addr = perms[i].start;
70408925c2fSLaura Abbott 		     addr < perms[i].end;
70508925c2fSLaura Abbott 		     addr += SECTION_SIZE)
70608925c2fSLaura Abbott 			section_update(addr, perms[i].mask,
70708925c2fSLaura Abbott 				set ? perms[i].prot : perms[i].clear, mm);
70808925c2fSLaura Abbott 	}
70908925c2fSLaura Abbott 
71008925c2fSLaura Abbott }
71108925c2fSLaura Abbott 
71211ce4b33SGrygorii Strashko /**
71311ce4b33SGrygorii Strashko  * update_sections_early intended to be called only through stop_machine
71411ce4b33SGrygorii Strashko  * framework and executed by only one CPU while all other CPUs will spin and
71511ce4b33SGrygorii Strashko  * wait, so no locking is required in this function.
71611ce4b33SGrygorii Strashko  */
71708925c2fSLaura Abbott static void update_sections_early(struct section_perm perms[], int n)
7181e6b4811SKees Cook {
71908925c2fSLaura Abbott 	struct task_struct *t, *s;
72008925c2fSLaura Abbott 
72108925c2fSLaura Abbott 	for_each_process(t) {
72208925c2fSLaura Abbott 		if (t->flags & PF_KTHREAD)
72308925c2fSLaura Abbott 			continue;
72408925c2fSLaura Abbott 		for_each_thread(t, s)
72508925c2fSLaura Abbott 			set_section_perms(perms, n, true, s->mm);
72608925c2fSLaura Abbott 	}
72708925c2fSLaura Abbott 	set_section_perms(perms, n, true, current->active_mm);
72808925c2fSLaura Abbott 	set_section_perms(perms, n, true, &init_mm);
72908925c2fSLaura Abbott }
73008925c2fSLaura Abbott 
73111ce4b33SGrygorii Strashko static int __fix_kernmem_perms(void *unused)
73208925c2fSLaura Abbott {
73308925c2fSLaura Abbott 	update_sections_early(nx_perms, ARRAY_SIZE(nx_perms));
73408925c2fSLaura Abbott 	return 0;
73508925c2fSLaura Abbott }
73608925c2fSLaura Abbott 
73711ce4b33SGrygorii Strashko static void fix_kernmem_perms(void)
73808925c2fSLaura Abbott {
73908925c2fSLaura Abbott 	stop_machine(__fix_kernmem_perms, NULL, NULL);
7401e6b4811SKees Cook }
74180d6b0c2SKees Cook 
74211ce4b33SGrygorii Strashko static int __mark_rodata_ro(void *unused)
74308925c2fSLaura Abbott {
74408925c2fSLaura Abbott 	update_sections_early(ro_perms, ARRAY_SIZE(ro_perms));
74508925c2fSLaura Abbott 	return 0;
74608925c2fSLaura Abbott }
74708925c2fSLaura Abbott 
74880d6b0c2SKees Cook void mark_rodata_ro(void)
74980d6b0c2SKees Cook {
75008925c2fSLaura Abbott 	stop_machine(__mark_rodata_ro, NULL, NULL);
75180d6b0c2SKees Cook }
75280d6b0c2SKees Cook 
75380d6b0c2SKees Cook void set_kernel_text_rw(void)
75480d6b0c2SKees Cook {
75508925c2fSLaura Abbott 	set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), false,
75608925c2fSLaura Abbott 				current->active_mm);
75780d6b0c2SKees Cook }
75880d6b0c2SKees Cook 
75980d6b0c2SKees Cook void set_kernel_text_ro(void)
76080d6b0c2SKees Cook {
76108925c2fSLaura Abbott 	set_section_perms(ro_perms, ARRAY_SIZE(ro_perms), true,
76208925c2fSLaura Abbott 				current->active_mm);
76380d6b0c2SKees Cook }
76480d6b0c2SKees Cook 
7651e6b4811SKees Cook #else
7661e6b4811SKees Cook static inline void fix_kernmem_perms(void) { }
7670f5bf6d0SLaura Abbott #endif /* CONFIG_STRICT_KERNEL_RWX */
7681e6b4811SKees Cook 
7691e6b4811SKees Cook void free_tcmmem(void)
7701da177e4SLinus Torvalds {
771bc581770SLinus Walleij #ifdef CONFIG_HAVE_TCM
772ea208f64SLinus Walleij 	extern char __tcm_start, __tcm_end;
773bc581770SLinus Walleij 
77454d52573SStephen Boyd 	poison_init_mem(&__tcm_start, &__tcm_end - &__tcm_start);
775dbe67df4SJiang Liu 	free_reserved_area(&__tcm_start, &__tcm_end, -1, "TCM link");
776bc581770SLinus Walleij #endif
7771e6b4811SKees Cook }
7781e6b4811SKees Cook 
7791e6b4811SKees Cook void free_initmem(void)
7801e6b4811SKees Cook {
7811e6b4811SKees Cook 	fix_kernmem_perms();
7821e6b4811SKees Cook 	free_tcmmem();
783bc581770SLinus Walleij 
78454d52573SStephen Boyd 	poison_init_mem(__init_begin, __init_end - __init_begin);
7856db015e4SNicolas Pitre 	if (!machine_is_integrator() && !machine_is_cintegrator())
786dbe67df4SJiang Liu 		free_initmem_default(-1);
7871da177e4SLinus Torvalds }
7881da177e4SLinus Torvalds 
7891da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds static int keep_initrd;
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end)
7941da177e4SLinus Torvalds {
79554d52573SStephen Boyd 	if (!keep_initrd) {
796421520baSYalin Wang 		if (start == initrd_start)
797421520baSYalin Wang 			start = round_down(start, PAGE_SIZE);
798421520baSYalin Wang 		if (end == initrd_end)
799421520baSYalin Wang 			end = round_up(end, PAGE_SIZE);
800421520baSYalin Wang 
80154d52573SStephen Boyd 		poison_init_mem((void *)start, PAGE_ALIGN(end) - start);
802dbe67df4SJiang Liu 		free_reserved_area((void *)start, (void *)end, -1, "initrd");
8031da177e4SLinus Torvalds 	}
80454d52573SStephen Boyd }
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds static int __init keepinitrd_setup(char *__unused)
8071da177e4SLinus Torvalds {
8081da177e4SLinus Torvalds 	keep_initrd = 1;
8091da177e4SLinus Torvalds 	return 1;
8101da177e4SLinus Torvalds }
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds __setup("keepinitrd", keepinitrd_setup);
8131da177e4SLinus Torvalds #endif
814