xref: /openbmc/linux/arch/arm/mm/init.c (revision 99a468d7)
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>
16dc28094bSPaul Gortmaker #include <linux/export.h>
171da177e4SLinus Torvalds #include <linux/nodemask.h>
181da177e4SLinus Torvalds #include <linux/initrd.h>
199eb8f674SGrant Likely #include <linux/of_fdt.h>
203835f6cbSNicolas Pitre #include <linux/highmem.h>
215a0e3ad6STejun Heo #include <linux/gfp.h>
222778f620SRussell King #include <linux/memblock.h>
23c7909509SMarek Szyprowski #include <linux/dma-contiguous.h>
24158e8bfeSAlessandro Rubini #include <linux/sizes.h>
251da177e4SLinus Torvalds 
26b4b20ad8SRussell King #include <asm/cp15.h>
271da177e4SLinus Torvalds #include <asm/mach-types.h>
28716a3dc2SRussell King #include <asm/memblock.h>
2993c02ab4SGrant Likely #include <asm/prom.h>
3037efe642SRussell King #include <asm/sections.h>
311da177e4SLinus Torvalds #include <asm/setup.h>
321e6b4811SKees Cook #include <asm/system_info.h>
331da177e4SLinus Torvalds #include <asm/tlb.h>
34db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #include <asm/mach/arch.h>
371da177e4SLinus Torvalds #include <asm/mach/map.h>
381da177e4SLinus Torvalds 
391b2e2b73SRussell King #include "mm.h"
401b2e2b73SRussell King 
41b4b20ad8SRussell King #ifdef CONFIG_CPU_CP15_MMU
42b4b20ad8SRussell King unsigned long __init __clear_cr(unsigned long mask)
43b4b20ad8SRussell King {
44b4b20ad8SRussell King 	cr_alignment = cr_alignment & ~mask;
45b4b20ad8SRussell King 	return cr_alignment;
46b4b20ad8SRussell King }
47b4b20ad8SRussell King #endif
48b4b20ad8SRussell King 
49de22cc6eSVitaly Andrianov static phys_addr_t phys_initrd_start __initdata = 0;
50012d1f4aSRussell King static unsigned long phys_initrd_size __initdata = 0;
51012d1f4aSRussell King 
522b0d8c25SJeremy Kerr static int __init early_initrd(char *p)
53012d1f4aSRussell King {
54de22cc6eSVitaly Andrianov 	phys_addr_t start;
55de22cc6eSVitaly Andrianov 	unsigned long size;
562b0d8c25SJeremy Kerr 	char *endp;
57012d1f4aSRussell King 
582b0d8c25SJeremy Kerr 	start = memparse(p, &endp);
592b0d8c25SJeremy Kerr 	if (*endp == ',') {
602b0d8c25SJeremy Kerr 		size = memparse(endp + 1, NULL);
61012d1f4aSRussell King 
62012d1f4aSRussell King 		phys_initrd_start = start;
63012d1f4aSRussell King 		phys_initrd_size = size;
64012d1f4aSRussell King 	}
652b0d8c25SJeremy Kerr 	return 0;
66012d1f4aSRussell King }
672b0d8c25SJeremy Kerr early_param("initrd", early_initrd);
68012d1f4aSRussell King 
69012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag)
70012d1f4aSRussell King {
714ed89f22SRussell King 	pr_warn("ATAG_INITRD is deprecated; "
72012d1f4aSRussell King 		"please update your bootloader.\n");
73012d1f4aSRussell King 	phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
74012d1f4aSRussell King 	phys_initrd_size = tag->u.initrd.size;
75012d1f4aSRussell King 	return 0;
76012d1f4aSRussell King }
77012d1f4aSRussell King 
78012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd);
79012d1f4aSRussell King 
80012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag)
81012d1f4aSRussell King {
82012d1f4aSRussell King 	phys_initrd_start = tag->u.initrd.start;
83012d1f4aSRussell King 	phys_initrd_size = tag->u.initrd.size;
84012d1f4aSRussell King 	return 0;
85012d1f4aSRussell King }
86012d1f4aSRussell King 
87012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2);
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds /*
904b5f32ceSNicolas Pitre  * This keeps memory configuration data used by a couple memory
914b5f32ceSNicolas Pitre  * initialization functions, as well as show_mem() for the skipping
924b5f32ceSNicolas Pitre  * of holes in the memory map.  It is populated by arm_add_memory().
931da177e4SLinus Torvalds  */
94b2b755b5SDavid Rientjes void show_mem(unsigned int filter)
951da177e4SLinus Torvalds {
961da177e4SLinus Torvalds 	int free = 0, total = 0, reserved = 0;
971c2f87c2SLaura Abbott 	int shared = 0, cached = 0, slab = 0;
981c2f87c2SLaura Abbott 	struct memblock_region *reg;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	printk("Mem-info:\n");
1017bf02ea2SDavid Rientjes 	show_free_areas(filter);
102be370302SRussell King 
1031c2f87c2SLaura Abbott 	for_each_memblock (memory, reg) {
1045e709827SRay Lehtiniemi 		unsigned int pfn1, pfn2;
1051da177e4SLinus Torvalds 		struct page *page, *end;
1061da177e4SLinus Torvalds 
1071c2f87c2SLaura Abbott 		pfn1 = memblock_region_memory_base_pfn(reg);
1081c2f87c2SLaura Abbott 		pfn2 = memblock_region_memory_end_pfn(reg);
1095e709827SRay Lehtiniemi 
110ea056df7SCatalin Marinas 		page = pfn_to_page(pfn1);
111ea056df7SCatalin Marinas 		end  = pfn_to_page(pfn2 - 1) + 1;
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 		do {
1141da177e4SLinus Torvalds 			total++;
1151da177e4SLinus Torvalds 			if (PageReserved(page))
1161da177e4SLinus Torvalds 				reserved++;
1171da177e4SLinus Torvalds 			else if (PageSwapCache(page))
1181da177e4SLinus Torvalds 				cached++;
1191da177e4SLinus Torvalds 			else if (PageSlab(page))
1201da177e4SLinus Torvalds 				slab++;
1211da177e4SLinus Torvalds 			else if (!page_count(page))
1221da177e4SLinus Torvalds 				free++;
1231da177e4SLinus Torvalds 			else
1241da177e4SLinus Torvalds 				shared += page_count(page) - 1;
1251c2f87c2SLaura Abbott 			pfn1++;
1261c2f87c2SLaura Abbott 			page = pfn_to_page(pfn1);
1271c2f87c2SLaura Abbott 		} while (pfn1 < pfn2);
1281da177e4SLinus Torvalds 	}
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 	printk("%d pages of RAM\n", total);
1311da177e4SLinus Torvalds 	printk("%d free pages\n", free);
1321da177e4SLinus Torvalds 	printk("%d reserved pages\n", reserved);
1331da177e4SLinus Torvalds 	printk("%d slab pages\n", slab);
1341da177e4SLinus Torvalds 	printk("%d pages shared\n", shared);
1351da177e4SLinus Torvalds 	printk("%d pages swap cached\n", cached);
1361da177e4SLinus Torvalds }
1371da177e4SLinus Torvalds 
138f25b4b4cSRussell King static void __init find_limits(unsigned long *min, unsigned long *max_low,
139f25b4b4cSRussell King 			       unsigned long *max_high)
140dde5828fSRussell King {
1411c2f87c2SLaura Abbott 	*max_low = PFN_DOWN(memblock_get_current_limit());
1421c2f87c2SLaura Abbott 	*min = PFN_UP(memblock_start_of_DRAM());
1431c2f87c2SLaura Abbott 	*max_high = PFN_DOWN(memblock_end_of_DRAM());
144dde5828fSRussell King }
145dde5828fSRussell King 
146be20902bSRussell King #ifdef CONFIG_ZONE_DMA
14765032018SNicolas Pitre 
148364230b9SRob Herring phys_addr_t arm_dma_zone_size __read_mostly;
14965032018SNicolas Pitre EXPORT_SYMBOL(arm_dma_zone_size);
15065032018SNicolas Pitre 
151022ae537SRussell King /*
152022ae537SRussell King  * The DMA mask corresponding to the maximum bus address allocatable
153022ae537SRussell King  * using GFP_DMA.  The default here places no restriction on DMA
154022ae537SRussell King  * allocations.  This must be the smallest DMA mask in the system,
155022ae537SRussell King  * so a successful GFP_DMA allocation will always satisfy this.
156022ae537SRussell King  */
1574986e5c7SMarek Szyprowski phys_addr_t arm_dma_limit;
1584dcfa600SRussell King unsigned long arm_dma_pfn_limit;
159022ae537SRussell King 
160be20902bSRussell King static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole,
161be20902bSRussell King 	unsigned long dma_size)
162be20902bSRussell King {
163be20902bSRussell King 	if (size[0] <= dma_size)
164be20902bSRussell King 		return;
165be20902bSRussell King 
166be20902bSRussell King 	size[ZONE_NORMAL] = size[0] - dma_size;
167be20902bSRussell King 	size[ZONE_DMA] = dma_size;
168be20902bSRussell King 	hole[ZONE_NORMAL] = hole[0];
169be20902bSRussell King 	hole[ZONE_DMA] = 0;
170be20902bSRussell King }
171be20902bSRussell King #endif
172be20902bSRussell King 
173ff69a4c8SRussell King void __init setup_dma_zone(const struct machine_desc *mdesc)
174c7909509SMarek Szyprowski {
175c7909509SMarek Szyprowski #ifdef CONFIG_ZONE_DMA
176c7909509SMarek Szyprowski 	if (mdesc->dma_zone_size) {
177c7909509SMarek Szyprowski 		arm_dma_zone_size = mdesc->dma_zone_size;
1786bcac805SRussell King 		arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1;
179c7909509SMarek Szyprowski 	} else
180c7909509SMarek Szyprowski 		arm_dma_limit = 0xffffffff;
1814dcfa600SRussell King 	arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT;
182c7909509SMarek Szyprowski #endif
183c7909509SMarek Szyprowski }
184c7909509SMarek Szyprowski 
18584f452b1SSantosh Shilimkar static void __init zone_sizes_init(unsigned long min, unsigned long max_low,
186a2c54d2aSRussell King 	unsigned long max_high)
187b7a69ac3SRussell King {
188b7a69ac3SRussell King 	unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
189a2c54d2aSRussell King 	struct memblock_region *reg;
190b7a69ac3SRussell King 
19190072059SRussell King 	/*
192be370302SRussell King 	 * initialise the zones.
19390072059SRussell King 	 */
19490072059SRussell King 	memset(zone_size, 0, sizeof(zone_size));
19590072059SRussell King 
19690072059SRussell King 	/*
197be370302SRussell King 	 * The memory size has already been determined.  If we need
198be370302SRussell King 	 * to do anything fancy with the allocation of this memory
199be370302SRussell King 	 * to the zones, now is the time to do it.
20090072059SRussell King 	 */
201dde5828fSRussell King 	zone_size[0] = max_low - min;
202dde5828fSRussell King #ifdef CONFIG_HIGHMEM
203dde5828fSRussell King 	zone_size[ZONE_HIGHMEM] = max_high - max_low;
204dde5828fSRussell King #endif
20590072059SRussell King 
20690072059SRussell King 	/*
207be370302SRussell King 	 * Calculate the size of the holes.
208be370302SRussell King 	 *  holes = node_size - sum(bank_sizes)
20990072059SRussell King 	 */
210dde5828fSRussell King 	memcpy(zhole_size, zone_size, sizeof(zhole_size));
211a2c54d2aSRussell King 	for_each_memblock(memory, reg) {
212a2c54d2aSRussell King 		unsigned long start = memblock_region_memory_base_pfn(reg);
213a2c54d2aSRussell King 		unsigned long end = memblock_region_memory_end_pfn(reg);
214a2c54d2aSRussell King 
215a2c54d2aSRussell King 		if (start < max_low) {
216a2c54d2aSRussell King 			unsigned long low_end = min(end, max_low);
217a2c54d2aSRussell King 			zhole_size[0] -= low_end - start;
218a2c54d2aSRussell King 		}
219dde5828fSRussell King #ifdef CONFIG_HIGHMEM
220a2c54d2aSRussell King 		if (end > max_low) {
221a2c54d2aSRussell King 			unsigned long high_start = max(start, max_low);
222a2c54d2aSRussell King 			zhole_size[ZONE_HIGHMEM] -= end - high_start;
223a2c54d2aSRussell King 		}
224dde5828fSRussell King #endif
225dde5828fSRussell King 	}
22690072059SRussell King 
22765032018SNicolas Pitre #ifdef CONFIG_ZONE_DMA
22890072059SRussell King 	/*
22990072059SRussell King 	 * Adjust the sizes according to any special requirements for
23090072059SRussell King 	 * this machine type.
23190072059SRussell King 	 */
232c7909509SMarek Szyprowski 	if (arm_dma_zone_size)
233be20902bSRussell King 		arm_adjust_dma_zone(zone_size, zhole_size,
23465032018SNicolas Pitre 			arm_dma_zone_size >> PAGE_SHIFT);
235be20902bSRussell King #endif
23690072059SRussell King 
237be370302SRussell King 	free_area_init_node(0, zone_size, min, zhole_size);
23890072059SRussell King }
23990072059SRussell King 
2407b7bf499SWill Deacon #ifdef CONFIG_HAVE_ARCH_PFN_VALID
241b7cfda9fSRussell King int pfn_valid(unsigned long pfn)
242b7cfda9fSRussell King {
243fb492c91SMark Rutland 	return memblock_is_memory(__pfn_to_phys(pfn));
244b7cfda9fSRussell King }
245b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid);
2467b7bf499SWill Deacon #endif
247657e12fdSRussell King 
2487b7bf499SWill Deacon #ifndef CONFIG_SPARSEMEM
24914904927SStephen Boyd static void __init arm_memory_present(void)
250657e12fdSRussell King {
251657e12fdSRussell King }
252657e12fdSRussell King #else
25314904927SStephen Boyd static void __init arm_memory_present(void)
254657e12fdSRussell King {
255719c1514SBenjamin Herrenschmidt 	struct memblock_region *reg;
256719c1514SBenjamin Herrenschmidt 
2577c996361SYinghai Lu 	for_each_memblock(memory, reg)
258c7fc2de0SYinghai Lu 		memory_present(0, memblock_region_memory_base_pfn(reg),
259c7fc2de0SYinghai Lu 			       memblock_region_memory_end_pfn(reg));
260657e12fdSRussell King }
261b7cfda9fSRussell King #endif
262b7cfda9fSRussell King 
263716a3dc2SRussell King static bool arm_memblock_steal_permitted = true;
264716a3dc2SRussell King 
265bc2827d0SRussell King phys_addr_t __init arm_memblock_steal(phys_addr_t size, phys_addr_t align)
266716a3dc2SRussell King {
267716a3dc2SRussell King 	phys_addr_t phys;
268716a3dc2SRussell King 
269716a3dc2SRussell King 	BUG_ON(!arm_memblock_steal_permitted);
270716a3dc2SRussell King 
2717ac68a4cSRussell King 	phys = memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ANYWHERE);
272716a3dc2SRussell King 	memblock_free(phys, size);
273716a3dc2SRussell King 	memblock_remove(phys, size);
274716a3dc2SRussell King 
275716a3dc2SRussell King 	return phys;
276716a3dc2SRussell King }
277716a3dc2SRussell King 
2781c2f87c2SLaura Abbott void __init arm_memblock_init(const struct machine_desc *mdesc)
2792778f620SRussell King {
2802778f620SRussell King 	/* Register the kernel text, kernel data and initrd with memblock. */
2812778f620SRussell King #ifdef CONFIG_XIP_KERNEL
282842eab40SRussell King 	memblock_reserve(__pa(_sdata), _end - _sdata);
2832778f620SRussell King #else
2842778f620SRussell King 	memblock_reserve(__pa(_stext), _end - _stext);
2852778f620SRussell King #endif
2862778f620SRussell King #ifdef CONFIG_BLK_DEV_INITRD
28765939301SRob Herring 	/* FDT scan will populate initrd_start */
2884c235cb9SBen Peddell 	if (initrd_start && !phys_initrd_size) {
28965939301SRob Herring 		phys_initrd_start = __virt_to_phys(initrd_start);
29065939301SRob Herring 		phys_initrd_size = initrd_end - initrd_start;
29165939301SRob Herring 	}
2924c235cb9SBen Peddell 	initrd_start = initrd_end = 0;
293b0a2679dSRussell King 	if (phys_initrd_size &&
2948f4b8c76SRussell King 	    !memblock_is_region_memory(phys_initrd_start, phys_initrd_size)) {
295de22cc6eSVitaly Andrianov 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region - disabling initrd\n",
296de22cc6eSVitaly Andrianov 		       (u64)phys_initrd_start, phys_initrd_size);
2978f4b8c76SRussell King 		phys_initrd_start = phys_initrd_size = 0;
2988f4b8c76SRussell King 	}
2998f4b8c76SRussell King 	if (phys_initrd_size &&
300b0a2679dSRussell King 	    memblock_is_region_reserved(phys_initrd_start, phys_initrd_size)) {
301de22cc6eSVitaly Andrianov 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region - disabling initrd\n",
302de22cc6eSVitaly Andrianov 		       (u64)phys_initrd_start, phys_initrd_size);
303b0a2679dSRussell King 		phys_initrd_start = phys_initrd_size = 0;
304b0a2679dSRussell King 	}
3052778f620SRussell King 	if (phys_initrd_size) {
3062778f620SRussell King 		memblock_reserve(phys_initrd_start, phys_initrd_size);
3072778f620SRussell King 
3082778f620SRussell King 		/* Now convert initrd to virtual addresses */
3092778f620SRussell King 		initrd_start = __phys_to_virt(phys_initrd_start);
3102778f620SRussell King 		initrd_end = initrd_start + phys_initrd_size;
3112778f620SRussell King 	}
3122778f620SRussell King #endif
3132778f620SRussell King 
3142778f620SRussell King 	arm_mm_memblock_reserve();
3152778f620SRussell King 
3168d717a52SRussell King 	/* reserve any platform specific memblock areas */
3178d717a52SRussell King 	if (mdesc->reserve)
3188d717a52SRussell King 		mdesc->reserve();
3198d717a52SRussell King 
320bcedb5f9SMarek Szyprowski 	early_init_fdt_scan_reserved_mem();
321bcedb5f9SMarek Szyprowski 
32299a468d7SGeorge G. Davis 	/* reserve memory for DMA contiguous allocations */
32395b0e655SMarek Szyprowski 	dma_contiguous_reserve(arm_dma_limit);
324c7909509SMarek Szyprowski 
325716a3dc2SRussell King 	arm_memblock_steal_permitted = false;
3262778f620SRussell King 	memblock_dump_all();
3272778f620SRussell King }
3282778f620SRussell King 
3298d717a52SRussell King void __init bootmem_init(void)
33090072059SRussell King {
331dde5828fSRussell King 	unsigned long min, max_low, max_high;
33290072059SRussell King 
3338e58caefSGrygorii Strashko 	memblock_allow_resize();
334dde5828fSRussell King 	max_low = max_high = 0;
335dde5828fSRussell King 
336f25b4b4cSRussell King 	find_limits(&min, &max_low, &max_high);
337dde5828fSRussell King 
338b7a69ac3SRussell King 	/*
339657e12fdSRussell King 	 * Sparsemem tries to allocate bootmem in memory_present(),
340657e12fdSRussell King 	 * so must be done after the fixed reservations
341657e12fdSRussell King 	 */
342eda2e5dcSRussell King 	arm_memory_present();
34390072059SRussell King 
344b7a69ac3SRussell King 	/*
345b7a69ac3SRussell King 	 * sparse_init() needs the bootmem allocator up and running.
346b7a69ac3SRussell King 	 */
347b7a69ac3SRussell King 	sparse_init();
348b7a69ac3SRussell King 
349b7a69ac3SRussell King 	/*
350be370302SRussell King 	 * Now free the memory - free_area_init_node needs
351b7a69ac3SRussell King 	 * the sparse mem_map arrays initialized by sparse_init()
352b7a69ac3SRussell King 	 * for memmap_init_zone(), otherwise all PFNs are invalid.
353b7a69ac3SRussell King 	 */
35484f452b1SSantosh Shilimkar 	zone_sizes_init(min, max_low, max_high);
355b7a69ac3SRussell King 
35690072059SRussell King 	/*
35790072059SRussell King 	 * This doesn't seem to be used by the Linux memory manager any
35890072059SRussell King 	 * more, but is used by ll_rw_block.  If we can get rid of it, we
35990072059SRussell King 	 * also get rid of some of the stuff above as well.
36090072059SRussell King 	 */
36126ba47b1SSantosh Shilimkar 	min_low_pfn = min;
36226ba47b1SSantosh Shilimkar 	max_low_pfn = max_low;
36326ba47b1SSantosh Shilimkar 	max_pfn = max_high;
36490072059SRussell King }
36590072059SRussell King 
36654d52573SStephen Boyd /*
36754d52573SStephen Boyd  * Poison init memory with an undefined instruction (ARM) or a branch to an
36854d52573SStephen Boyd  * undefined instruction (Thumb).
36954d52573SStephen Boyd  */
37054d52573SStephen Boyd static inline void poison_init_mem(void *s, size_t count)
37154d52573SStephen Boyd {
37254d52573SStephen Boyd 	u32 *p = (u32 *)s;
373bf912d99SJamie Iles 	for (; count != 0; count -= 4)
37454d52573SStephen Boyd 		*p++ = 0xe7fddef0;
37554d52573SStephen Boyd }
37654d52573SStephen Boyd 
377a013053dSRussell King static inline void
378be370302SRussell King free_memmap(unsigned long start_pfn, unsigned long end_pfn)
379a013053dSRussell King {
380a013053dSRussell King 	struct page *start_pg, *end_pg;
38156bc6286SVitaly Andrianov 	phys_addr_t pg, pgend;
382a013053dSRussell King 
383a013053dSRussell King 	/*
384a013053dSRussell King 	 * Convert start_pfn/end_pfn to a struct page pointer.
385a013053dSRussell King 	 */
3863257f43dSCatalin Marinas 	start_pg = pfn_to_page(start_pfn - 1) + 1;
3879af386c8SWill Deacon 	end_pg = pfn_to_page(end_pfn - 1) + 1;
388a013053dSRussell King 
389a013053dSRussell King 	/*
390a013053dSRussell King 	 * Convert to physical addresses, and
391a013053dSRussell King 	 * round start upwards and end downwards.
392a013053dSRussell King 	 */
39356bc6286SVitaly Andrianov 	pg = PAGE_ALIGN(__pa(start_pg));
39456bc6286SVitaly Andrianov 	pgend = __pa(end_pg) & PAGE_MASK;
395a013053dSRussell King 
396a013053dSRussell King 	/*
397a013053dSRussell King 	 * If there are free pages between these,
398a013053dSRussell King 	 * free the section of the memmap array.
399a013053dSRussell King 	 */
400a013053dSRussell King 	if (pg < pgend)
401cfb66586SSantosh Shilimkar 		memblock_free_early(pg, pgend - pg);
402a013053dSRussell King }
403a013053dSRussell King 
404a013053dSRussell King /*
405a013053dSRussell King  * The mem_map array can get very big.  Free the unused area of the memory map.
406a013053dSRussell King  */
4071c2f87c2SLaura Abbott static void __init free_unused_memmap(void)
408a013053dSRussell King {
4091c2f87c2SLaura Abbott 	unsigned long start, prev_end = 0;
4101c2f87c2SLaura Abbott 	struct memblock_region *reg;
411a013053dSRussell King 
412a013053dSRussell King 	/*
4133260e529SMichael Bohan 	 * This relies on each bank being in address order.
4143260e529SMichael Bohan 	 * The banks are sorted previously in bootmem_init().
415a013053dSRussell King 	 */
4161c2f87c2SLaura Abbott 	for_each_memblock(memory, reg) {
4171c2f87c2SLaura Abbott 		start = memblock_region_memory_base_pfn(reg);
418a013053dSRussell King 
4199af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM
4209af386c8SWill Deacon 		/*
4219af386c8SWill Deacon 		 * Take care not to free memmap entries that don't exist
4229af386c8SWill Deacon 		 * due to SPARSEMEM sections which aren't present.
4239af386c8SWill Deacon 		 */
4241c2f87c2SLaura Abbott 		start = min(start,
4251c2f87c2SLaura Abbott 				 ALIGN(prev_end, PAGES_PER_SECTION));
426002ea9eeSLinus Walleij #else
427002ea9eeSLinus Walleij 		/*
428002ea9eeSLinus Walleij 		 * Align down here since the VM subsystem insists that the
429002ea9eeSLinus Walleij 		 * memmap entries are valid from the bank start aligned to
430002ea9eeSLinus Walleij 		 * MAX_ORDER_NR_PAGES.
431002ea9eeSLinus Walleij 		 */
4321c2f87c2SLaura Abbott 		start = round_down(start, MAX_ORDER_NR_PAGES);
4339af386c8SWill Deacon #endif
434a013053dSRussell King 		/*
435a013053dSRussell King 		 * If we had a previous bank, and there is a space
436a013053dSRussell King 		 * between the current bank and the previous, free it.
437a013053dSRussell King 		 */
4381c2f87c2SLaura Abbott 		if (prev_end && prev_end < start)
4391c2f87c2SLaura Abbott 			free_memmap(prev_end, start);
440a013053dSRussell King 
4413260e529SMichael Bohan 		/*
4423260e529SMichael Bohan 		 * Align up here since the VM subsystem insists that the
4433260e529SMichael Bohan 		 * memmap entries are valid from the bank end aligned to
4443260e529SMichael Bohan 		 * MAX_ORDER_NR_PAGES.
4453260e529SMichael Bohan 		 */
4461c2f87c2SLaura Abbott 		prev_end = ALIGN(memblock_region_memory_end_pfn(reg),
4471c2f87c2SLaura Abbott 				 MAX_ORDER_NR_PAGES);
448a013053dSRussell King 	}
4499af386c8SWill Deacon 
4509af386c8SWill Deacon #ifdef CONFIG_SPARSEMEM
4511c2f87c2SLaura Abbott 	if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
4521c2f87c2SLaura Abbott 		free_memmap(prev_end,
4531c2f87c2SLaura Abbott 			    ALIGN(prev_end, PAGES_PER_SECTION));
4549af386c8SWill Deacon #endif
455a013053dSRussell King }
456a013053dSRussell King 
45783db0384SJiang Liu #ifdef CONFIG_HIGHMEM
45883db0384SJiang Liu static inline void free_area_high(unsigned long pfn, unsigned long end)
45983db0384SJiang Liu {
460dd6911efSJiang Liu 	for (; pfn < end; pfn++)
461dd6911efSJiang Liu 		free_highmem_page(pfn_to_page(pfn));
46283db0384SJiang Liu }
46383db0384SJiang Liu #endif
46483db0384SJiang Liu 
465d0e775afSRussell King static void __init free_highpages(void)
466d0e775afSRussell King {
467d0e775afSRussell King #ifdef CONFIG_HIGHMEM
46826ba47b1SSantosh Shilimkar 	unsigned long max_low = max_low_pfn;
469df4f14c7SRussell King 	struct memblock_region *mem, *res;
470d0e775afSRussell King 
471d0e775afSRussell King 	/* set highmem page free */
472df4f14c7SRussell King 	for_each_memblock(memory, mem) {
473df4f14c7SRussell King 		unsigned long start = memblock_region_memory_base_pfn(mem);
474df4f14c7SRussell King 		unsigned long end = memblock_region_memory_end_pfn(mem);
475df4f14c7SRussell King 
476df4f14c7SRussell King 		/* Ignore complete lowmem entries */
477df4f14c7SRussell King 		if (end <= max_low)
478df4f14c7SRussell King 			continue;
479df4f14c7SRussell King 
480df4f14c7SRussell King 		/* Truncate partial highmem entries */
481df4f14c7SRussell King 		if (start < max_low)
482df4f14c7SRussell King 			start = max_low;
483df4f14c7SRussell King 
484df4f14c7SRussell King 		/* Find and exclude any reserved regions */
485df4f14c7SRussell King 		for_each_memblock(reserved, res) {
486df4f14c7SRussell King 			unsigned long res_start, res_end;
487df4f14c7SRussell King 
488df4f14c7SRussell King 			res_start = memblock_region_reserved_base_pfn(res);
489df4f14c7SRussell King 			res_end = memblock_region_reserved_end_pfn(res);
490df4f14c7SRussell King 
491df4f14c7SRussell King 			if (res_end < start)
492df4f14c7SRussell King 				continue;
493df4f14c7SRussell King 			if (res_start < start)
494df4f14c7SRussell King 				res_start = start;
495df4f14c7SRussell King 			if (res_start > end)
496df4f14c7SRussell King 				res_start = end;
497df4f14c7SRussell King 			if (res_end > end)
498df4f14c7SRussell King 				res_end = end;
499df4f14c7SRussell King 			if (res_start != start)
50083db0384SJiang Liu 				free_area_high(start, res_start);
501df4f14c7SRussell King 			start = res_end;
502df4f14c7SRussell King 			if (start == end)
503df4f14c7SRussell King 				break;
504df4f14c7SRussell King 		}
505df4f14c7SRussell King 
506df4f14c7SRussell King 		/* And now free anything which remains */
507df4f14c7SRussell King 		if (start < end)
50883db0384SJiang Liu 			free_area_high(start, end);
509d0e775afSRussell King 	}
510d0e775afSRussell King #endif
511d0e775afSRussell King }
512d0e775afSRussell King 
5131da177e4SLinus Torvalds /*
5141da177e4SLinus Torvalds  * mem_init() marks the free areas in the mem_map and tells us how much
5151da177e4SLinus Torvalds  * memory is free.  This is done after various parts of the system have
5161da177e4SLinus Torvalds  * claimed their memory after the kernel image.
5171da177e4SLinus Torvalds  */
5181da177e4SLinus Torvalds void __init mem_init(void)
5191da177e4SLinus Torvalds {
5201dbd30e9SLinus Walleij #ifdef CONFIG_HAVE_TCM
5211dbd30e9SLinus Walleij 	/* These pointers are filled in on TCM detection */
5221dbd30e9SLinus Walleij 	extern u32 dtcm_end;
5231dbd30e9SLinus Walleij 	extern u32 itcm_end;
5241dbd30e9SLinus Walleij #endif
5251da177e4SLinus Torvalds 
526b3ba41f2SSantosh Shilimkar 	set_max_mapnr(pfn_to_page(max_pfn) - mem_map);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds 	/* this will put all unused low memory onto the freelists */
5291c2f87c2SLaura Abbott 	free_unused_memmap();
5300c988534SJiang Liu 	free_all_bootmem();
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds #ifdef CONFIG_SA1111
5331da177e4SLinus Torvalds 	/* now that our DMA memory is actually so designated, we can free it */
534bfd65dd9SLinus Torvalds 	free_reserved_area(__va(PHYS_OFFSET), swapper_pg_dir, -1, NULL);
5351da177e4SLinus Torvalds #endif
5361da177e4SLinus Torvalds 
537d0e775afSRussell King 	free_highpages();
5383835f6cbSNicolas Pitre 
5392450c973SJiang Liu 	mem_init_print_info(NULL);
5401da177e4SLinus Torvalds 
541db9ef1afSFenkart/Bostandzhyan #define MLK(b, t) b, t, ((t) - (b)) >> 10
542db9ef1afSFenkart/Bostandzhyan #define MLM(b, t) b, t, ((t) - (b)) >> 20
543db9ef1afSFenkart/Bostandzhyan #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
544db9ef1afSFenkart/Bostandzhyan 
5454ed89f22SRussell King 	pr_notice("Virtual kernel memory layout:\n"
546db9ef1afSFenkart/Bostandzhyan 			"    vector  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
54707d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM
54807d2a5c7SLinus Walleij 			"    DTCM    : 0x%08lx - 0x%08lx   (%4ld kB)\n"
54907d2a5c7SLinus Walleij 			"    ITCM    : 0x%08lx - 0x%08lx   (%4ld kB)\n"
55007d2a5c7SLinus Walleij #endif
551db9ef1afSFenkart/Bostandzhyan 			"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
552db9ef1afSFenkart/Bostandzhyan 			"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
553db9ef1afSFenkart/Bostandzhyan 			"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
554db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
555db9ef1afSFenkart/Bostandzhyan 			"    pkmap   : 0x%08lx - 0x%08lx   (%4ld MB)\n"
556db9ef1afSFenkart/Bostandzhyan #endif
557d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES
558db9ef1afSFenkart/Bostandzhyan 			"    modules : 0x%08lx - 0x%08lx   (%4ld MB)\n"
559d9277d51SUwe Kleine-König #endif
560178c3dfeSRussell King 			"      .text : 0x%p" " - 0x%p" "   (%4td kB)\n"
561178c3dfeSRussell King 			"      .init : 0x%p" " - 0x%p" "   (%4td kB)\n"
562178c3dfeSRussell King 			"      .data : 0x%p" " - 0x%p" "   (%4td kB)\n"
563178c3dfeSRussell King 			"       .bss : 0x%p" " - 0x%p" "   (%4td kB)\n",
564db9ef1afSFenkart/Bostandzhyan 
565db9ef1afSFenkart/Bostandzhyan 			MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
566db9ef1afSFenkart/Bostandzhyan 				(PAGE_SIZE)),
56707d2a5c7SLinus Walleij #ifdef CONFIG_HAVE_TCM
5681dbd30e9SLinus Walleij 			MLK(DTCM_OFFSET, (unsigned long) dtcm_end),
5691dbd30e9SLinus Walleij 			MLK(ITCM_OFFSET, (unsigned long) itcm_end),
57007d2a5c7SLinus Walleij #endif
571b615bbbfSMark Salter 			MLK(FIXADDR_START, FIXADDR_END),
572c931b4f6SFenkart/Bostandzhyan 			MLM(VMALLOC_START, VMALLOC_END),
573db9ef1afSFenkart/Bostandzhyan 			MLM(PAGE_OFFSET, (unsigned long)high_memory),
574db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
575db9ef1afSFenkart/Bostandzhyan 			MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
576db9ef1afSFenkart/Bostandzhyan 				(PAGE_SIZE)),
577db9ef1afSFenkart/Bostandzhyan #endif
578d9277d51SUwe Kleine-König #ifdef CONFIG_MODULES
579db9ef1afSFenkart/Bostandzhyan 			MLM(MODULES_VADDR, MODULES_END),
580d9277d51SUwe Kleine-König #endif
581db9ef1afSFenkart/Bostandzhyan 
582db9ef1afSFenkart/Bostandzhyan 			MLK_ROUNDUP(_text, _etext),
5833835d69aSRussell King 			MLK_ROUNDUP(__init_begin, __init_end),
58445f6d7e0SRabin Vincent 			MLK_ROUNDUP(_sdata, _edata),
58545f6d7e0SRabin Vincent 			MLK_ROUNDUP(__bss_start, __bss_stop));
586db9ef1afSFenkart/Bostandzhyan 
587db9ef1afSFenkart/Bostandzhyan #undef MLK
588db9ef1afSFenkart/Bostandzhyan #undef MLM
589db9ef1afSFenkart/Bostandzhyan #undef MLK_ROUNDUP
590db9ef1afSFenkart/Bostandzhyan 
591a1839272SFenkart/Bostandzhyan 	/*
592a1839272SFenkart/Bostandzhyan 	 * Check boundaries twice: Some fundamental inconsistencies can
593a1839272SFenkart/Bostandzhyan 	 * be detected at build time already.
594a1839272SFenkart/Bostandzhyan 	 */
595a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU
596a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(TASK_SIZE				> MODULES_VADDR);
597a1839272SFenkart/Bostandzhyan 	BUG_ON(TASK_SIZE 				> MODULES_VADDR);
598a1839272SFenkart/Bostandzhyan #endif
599a1839272SFenkart/Bostandzhyan 
600a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
601a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
602a1839272SFenkart/Bostandzhyan 	BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE	> PAGE_OFFSET);
603a1839272SFenkart/Bostandzhyan #endif
604a1839272SFenkart/Bostandzhyan 
6052450c973SJiang Liu 	if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
6061da177e4SLinus Torvalds 		extern int sysctl_overcommit_memory;
6071da177e4SLinus Torvalds 		/*
6081da177e4SLinus Torvalds 		 * On a machine this small we won't get
6091da177e4SLinus Torvalds 		 * anywhere without overcommit, so turn
6101da177e4SLinus Torvalds 		 * it on by default.
6111da177e4SLinus Torvalds 		 */
6121da177e4SLinus Torvalds 		sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
6131da177e4SLinus Torvalds 	}
6141da177e4SLinus Torvalds }
6151da177e4SLinus Torvalds 
6161e6b4811SKees Cook #ifdef CONFIG_ARM_KERNMEM_PERMS
6171e6b4811SKees Cook struct section_perm {
6181e6b4811SKees Cook 	unsigned long start;
6191e6b4811SKees Cook 	unsigned long end;
6201e6b4811SKees Cook 	pmdval_t mask;
6211e6b4811SKees Cook 	pmdval_t prot;
62280d6b0c2SKees Cook 	pmdval_t clear;
6231e6b4811SKees Cook };
6241e6b4811SKees Cook 
62580d6b0c2SKees Cook static struct section_perm nx_perms[] = {
6261e6b4811SKees Cook 	/* Make pages tables, etc before _stext RW (set NX). */
6271e6b4811SKees Cook 	{
6281e6b4811SKees Cook 		.start	= PAGE_OFFSET,
6291e6b4811SKees Cook 		.end	= (unsigned long)_stext,
6301e6b4811SKees Cook 		.mask	= ~PMD_SECT_XN,
6311e6b4811SKees Cook 		.prot	= PMD_SECT_XN,
6321e6b4811SKees Cook 	},
6331e6b4811SKees Cook 	/* Make init RW (set NX). */
6341e6b4811SKees Cook 	{
6351e6b4811SKees Cook 		.start	= (unsigned long)__init_begin,
6361e6b4811SKees Cook 		.end	= (unsigned long)_sdata,
6371e6b4811SKees Cook 		.mask	= ~PMD_SECT_XN,
6381e6b4811SKees Cook 		.prot	= PMD_SECT_XN,
6391e6b4811SKees Cook 	},
64080d6b0c2SKees Cook #ifdef CONFIG_DEBUG_RODATA
64180d6b0c2SKees Cook 	/* Make rodata NX (set RO in ro_perms below). */
64280d6b0c2SKees Cook 	{
64380d6b0c2SKees Cook 		.start  = (unsigned long)__start_rodata,
64480d6b0c2SKees Cook 		.end    = (unsigned long)__init_begin,
64580d6b0c2SKees Cook 		.mask   = ~PMD_SECT_XN,
64680d6b0c2SKees Cook 		.prot   = PMD_SECT_XN,
64780d6b0c2SKees Cook 	},
64880d6b0c2SKees Cook #endif
6491e6b4811SKees Cook };
6501e6b4811SKees Cook 
65180d6b0c2SKees Cook #ifdef CONFIG_DEBUG_RODATA
65280d6b0c2SKees Cook static struct section_perm ro_perms[] = {
65380d6b0c2SKees Cook 	/* Make kernel code and rodata RX (set RO). */
65480d6b0c2SKees Cook 	{
65580d6b0c2SKees Cook 		.start  = (unsigned long)_stext,
65680d6b0c2SKees Cook 		.end    = (unsigned long)__init_begin,
65780d6b0c2SKees Cook #ifdef CONFIG_ARM_LPAE
65880d6b0c2SKees Cook 		.mask   = ~PMD_SECT_RDONLY,
65980d6b0c2SKees Cook 		.prot   = PMD_SECT_RDONLY,
66080d6b0c2SKees Cook #else
66180d6b0c2SKees Cook 		.mask   = ~(PMD_SECT_APX | PMD_SECT_AP_WRITE),
66280d6b0c2SKees Cook 		.prot   = PMD_SECT_APX | PMD_SECT_AP_WRITE,
66380d6b0c2SKees Cook 		.clear  = PMD_SECT_AP_WRITE,
66480d6b0c2SKees Cook #endif
66580d6b0c2SKees Cook 	},
66680d6b0c2SKees Cook };
66780d6b0c2SKees Cook #endif
66880d6b0c2SKees Cook 
6691e6b4811SKees Cook /*
6701e6b4811SKees Cook  * Updates section permissions only for the current mm (sections are
6711e6b4811SKees Cook  * copied into each mm). During startup, this is the init_mm. Is only
6721e6b4811SKees Cook  * safe to be called with preemption disabled, as under stop_machine().
6731e6b4811SKees Cook  */
6741e6b4811SKees Cook static inline void section_update(unsigned long addr, pmdval_t mask,
6751e6b4811SKees Cook 				  pmdval_t prot)
6761e6b4811SKees Cook {
6771e6b4811SKees Cook 	struct mm_struct *mm;
6781e6b4811SKees Cook 	pmd_t *pmd;
6791e6b4811SKees Cook 
6801e6b4811SKees Cook 	mm = current->active_mm;
6811e6b4811SKees Cook 	pmd = pmd_offset(pud_offset(pgd_offset(mm, addr), addr), addr);
6821e6b4811SKees Cook 
6831e6b4811SKees Cook #ifdef CONFIG_ARM_LPAE
6841e6b4811SKees Cook 	pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
6851e6b4811SKees Cook #else
6861e6b4811SKees Cook 	if (addr & SECTION_SIZE)
6871e6b4811SKees Cook 		pmd[1] = __pmd((pmd_val(pmd[1]) & mask) | prot);
6881e6b4811SKees Cook 	else
6891e6b4811SKees Cook 		pmd[0] = __pmd((pmd_val(pmd[0]) & mask) | prot);
6901e6b4811SKees Cook #endif
6911e6b4811SKees Cook 	flush_pmd_entry(pmd);
6921e6b4811SKees Cook 	local_flush_tlb_kernel_range(addr, addr + SECTION_SIZE);
6931e6b4811SKees Cook }
6941e6b4811SKees Cook 
6951e6b4811SKees Cook /* Make sure extended page tables are in use. */
6961e6b4811SKees Cook static inline bool arch_has_strict_perms(void)
6971e6b4811SKees Cook {
6981e6b4811SKees Cook 	if (cpu_architecture() < CPU_ARCH_ARMv6)
6991e6b4811SKees Cook 		return false;
7001e6b4811SKees Cook 
7011e6b4811SKees Cook 	return !!(get_cr() & CR_XP);
7021e6b4811SKees Cook }
7031e6b4811SKees Cook 
7041e6b4811SKees Cook #define set_section_perms(perms, field)	{				\
7051e6b4811SKees Cook 	size_t i;							\
7061e6b4811SKees Cook 	unsigned long addr;						\
7071e6b4811SKees Cook 									\
7081e6b4811SKees Cook 	if (!arch_has_strict_perms())					\
7091e6b4811SKees Cook 		return;							\
7101e6b4811SKees Cook 									\
7111e6b4811SKees Cook 	for (i = 0; i < ARRAY_SIZE(perms); i++) {			\
7121e6b4811SKees Cook 		if (!IS_ALIGNED(perms[i].start, SECTION_SIZE) ||	\
7131e6b4811SKees Cook 		    !IS_ALIGNED(perms[i].end, SECTION_SIZE)) {		\
7141e6b4811SKees Cook 			pr_err("BUG: section %lx-%lx not aligned to %lx\n", \
7151e6b4811SKees Cook 				perms[i].start, perms[i].end,		\
7161e6b4811SKees Cook 				SECTION_SIZE);				\
7171e6b4811SKees Cook 			continue;					\
7181e6b4811SKees Cook 		}							\
7191e6b4811SKees Cook 									\
7201e6b4811SKees Cook 		for (addr = perms[i].start;				\
7211e6b4811SKees Cook 		     addr < perms[i].end;				\
7221e6b4811SKees Cook 		     addr += SECTION_SIZE)				\
7231e6b4811SKees Cook 			section_update(addr, perms[i].mask,		\
7241e6b4811SKees Cook 				       perms[i].field);			\
7251e6b4811SKees Cook 	}								\
7261e6b4811SKees Cook }
7271e6b4811SKees Cook 
7281e6b4811SKees Cook static inline void fix_kernmem_perms(void)
7291e6b4811SKees Cook {
7301e6b4811SKees Cook 	set_section_perms(nx_perms, prot);
7311e6b4811SKees Cook }
73280d6b0c2SKees Cook 
73380d6b0c2SKees Cook #ifdef CONFIG_DEBUG_RODATA
73480d6b0c2SKees Cook void mark_rodata_ro(void)
73580d6b0c2SKees Cook {
73680d6b0c2SKees Cook 	set_section_perms(ro_perms, prot);
73780d6b0c2SKees Cook }
73880d6b0c2SKees Cook 
73980d6b0c2SKees Cook void set_kernel_text_rw(void)
74080d6b0c2SKees Cook {
74180d6b0c2SKees Cook 	set_section_perms(ro_perms, clear);
74280d6b0c2SKees Cook }
74380d6b0c2SKees Cook 
74480d6b0c2SKees Cook void set_kernel_text_ro(void)
74580d6b0c2SKees Cook {
74680d6b0c2SKees Cook 	set_section_perms(ro_perms, prot);
74780d6b0c2SKees Cook }
74880d6b0c2SKees Cook #endif /* CONFIG_DEBUG_RODATA */
74980d6b0c2SKees Cook 
7501e6b4811SKees Cook #else
7511e6b4811SKees Cook static inline void fix_kernmem_perms(void) { }
7521e6b4811SKees Cook #endif /* CONFIG_ARM_KERNMEM_PERMS */
7531e6b4811SKees Cook 
7541e6b4811SKees Cook void free_tcmmem(void)
7551da177e4SLinus Torvalds {
756bc581770SLinus Walleij #ifdef CONFIG_HAVE_TCM
757ea208f64SLinus Walleij 	extern char __tcm_start, __tcm_end;
758bc581770SLinus Walleij 
75954d52573SStephen Boyd 	poison_init_mem(&__tcm_start, &__tcm_end - &__tcm_start);
760dbe67df4SJiang Liu 	free_reserved_area(&__tcm_start, &__tcm_end, -1, "TCM link");
761bc581770SLinus Walleij #endif
7621e6b4811SKees Cook }
7631e6b4811SKees Cook 
7641e6b4811SKees Cook void free_initmem(void)
7651e6b4811SKees Cook {
7661e6b4811SKees Cook 	fix_kernmem_perms();
7671e6b4811SKees Cook 	free_tcmmem();
768bc581770SLinus Walleij 
76954d52573SStephen Boyd 	poison_init_mem(__init_begin, __init_end - __init_begin);
7706db015e4SNicolas Pitre 	if (!machine_is_integrator() && !machine_is_cintegrator())
771dbe67df4SJiang Liu 		free_initmem_default(-1);
7721da177e4SLinus Torvalds }
7731da177e4SLinus Torvalds 
7741da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds static int keep_initrd;
7771da177e4SLinus Torvalds 
7781da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end)
7791da177e4SLinus Torvalds {
78054d52573SStephen Boyd 	if (!keep_initrd) {
781421520baSYalin Wang 		if (start == initrd_start)
782421520baSYalin Wang 			start = round_down(start, PAGE_SIZE);
783421520baSYalin Wang 		if (end == initrd_end)
784421520baSYalin Wang 			end = round_up(end, PAGE_SIZE);
785421520baSYalin Wang 
78654d52573SStephen Boyd 		poison_init_mem((void *)start, PAGE_ALIGN(end) - start);
787dbe67df4SJiang Liu 		free_reserved_area((void *)start, (void *)end, -1, "initrd");
7881da177e4SLinus Torvalds 	}
78954d52573SStephen Boyd }
7901da177e4SLinus Torvalds 
7911da177e4SLinus Torvalds static int __init keepinitrd_setup(char *__unused)
7921da177e4SLinus Torvalds {
7931da177e4SLinus Torvalds 	keep_initrd = 1;
7941da177e4SLinus Torvalds 	return 1;
7951da177e4SLinus Torvalds }
7961da177e4SLinus Torvalds 
7971da177e4SLinus Torvalds __setup("keepinitrd", keepinitrd_setup);
7981da177e4SLinus Torvalds #endif
799