xref: /openbmc/linux/arch/arm/mm/init.c (revision 3260e5293727f16ffdce9a6a6203fd9a6b149e58)
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>
161da177e4SLinus Torvalds #include <linux/nodemask.h>
171da177e4SLinus Torvalds #include <linux/initrd.h>
183835f6cbSNicolas Pitre #include <linux/highmem.h>
195a0e3ad6STejun Heo #include <linux/gfp.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <asm/mach-types.h>
2237efe642SRussell King #include <asm/sections.h>
231da177e4SLinus Torvalds #include <asm/setup.h>
2474d02fb9SRussell King #include <asm/sizes.h>
251da177e4SLinus Torvalds #include <asm/tlb.h>
26db9ef1afSFenkart/Bostandzhyan #include <asm/fixmap.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <asm/mach/arch.h>
291da177e4SLinus Torvalds #include <asm/mach/map.h>
301da177e4SLinus Torvalds 
311b2e2b73SRussell King #include "mm.h"
321b2e2b73SRussell King 
33012d1f4aSRussell King static unsigned long phys_initrd_start __initdata = 0;
34012d1f4aSRussell King static unsigned long phys_initrd_size __initdata = 0;
35012d1f4aSRussell King 
362b0d8c25SJeremy Kerr static int __init early_initrd(char *p)
37012d1f4aSRussell King {
38012d1f4aSRussell King 	unsigned long start, size;
392b0d8c25SJeremy Kerr 	char *endp;
40012d1f4aSRussell King 
412b0d8c25SJeremy Kerr 	start = memparse(p, &endp);
422b0d8c25SJeremy Kerr 	if (*endp == ',') {
432b0d8c25SJeremy Kerr 		size = memparse(endp + 1, NULL);
44012d1f4aSRussell King 
45012d1f4aSRussell King 		phys_initrd_start = start;
46012d1f4aSRussell King 		phys_initrd_size = size;
47012d1f4aSRussell King 	}
482b0d8c25SJeremy Kerr 	return 0;
49012d1f4aSRussell King }
502b0d8c25SJeremy Kerr early_param("initrd", early_initrd);
51012d1f4aSRussell King 
52012d1f4aSRussell King static int __init parse_tag_initrd(const struct tag *tag)
53012d1f4aSRussell King {
54012d1f4aSRussell King 	printk(KERN_WARNING "ATAG_INITRD is deprecated; "
55012d1f4aSRussell King 		"please update your bootloader.\n");
56012d1f4aSRussell King 	phys_initrd_start = __virt_to_phys(tag->u.initrd.start);
57012d1f4aSRussell King 	phys_initrd_size = tag->u.initrd.size;
58012d1f4aSRussell King 	return 0;
59012d1f4aSRussell King }
60012d1f4aSRussell King 
61012d1f4aSRussell King __tagtable(ATAG_INITRD, parse_tag_initrd);
62012d1f4aSRussell King 
63012d1f4aSRussell King static int __init parse_tag_initrd2(const struct tag *tag)
64012d1f4aSRussell King {
65012d1f4aSRussell King 	phys_initrd_start = tag->u.initrd.start;
66012d1f4aSRussell King 	phys_initrd_size = tag->u.initrd.size;
67012d1f4aSRussell King 	return 0;
68012d1f4aSRussell King }
69012d1f4aSRussell King 
70012d1f4aSRussell King __tagtable(ATAG_INITRD2, parse_tag_initrd2);
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds /*
734b5f32ceSNicolas Pitre  * This keeps memory configuration data used by a couple memory
744b5f32ceSNicolas Pitre  * initialization functions, as well as show_mem() for the skipping
754b5f32ceSNicolas Pitre  * of holes in the memory map.  It is populated by arm_add_memory().
761da177e4SLinus Torvalds  */
774b5f32ceSNicolas Pitre struct meminfo meminfo;
785e709827SRay Lehtiniemi 
791da177e4SLinus Torvalds void show_mem(void)
801da177e4SLinus Torvalds {
811da177e4SLinus Torvalds 	int free = 0, total = 0, reserved = 0;
825e709827SRay Lehtiniemi 	int shared = 0, cached = 0, slab = 0, node, i;
835e709827SRay Lehtiniemi 	struct meminfo * mi = &meminfo;
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	printk("Mem-info:\n");
861da177e4SLinus Torvalds 	show_free_areas();
871da177e4SLinus Torvalds 	for_each_online_node(node) {
885e709827SRay Lehtiniemi 		for_each_nodebank (i,mi,node) {
89d2a38ef9SRussell King 			struct membank *bank = &mi->bank[i];
905e709827SRay Lehtiniemi 			unsigned int pfn1, pfn2;
911da177e4SLinus Torvalds 			struct page *page, *end;
921da177e4SLinus Torvalds 
93d2a38ef9SRussell King 			pfn1 = bank_pfn_start(bank);
94d2a38ef9SRussell King 			pfn2 = bank_pfn_end(bank);
955e709827SRay Lehtiniemi 
96ea056df7SCatalin Marinas 			page = pfn_to_page(pfn1);
97ea056df7SCatalin Marinas 			end  = pfn_to_page(pfn2 - 1) + 1;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 			do {
1001da177e4SLinus Torvalds 				total++;
1011da177e4SLinus Torvalds 				if (PageReserved(page))
1021da177e4SLinus Torvalds 					reserved++;
1031da177e4SLinus Torvalds 				else if (PageSwapCache(page))
1041da177e4SLinus Torvalds 					cached++;
1051da177e4SLinus Torvalds 				else if (PageSlab(page))
1061da177e4SLinus Torvalds 					slab++;
1071da177e4SLinus Torvalds 				else if (!page_count(page))
1081da177e4SLinus Torvalds 					free++;
1091da177e4SLinus Torvalds 				else
1101da177e4SLinus Torvalds 					shared += page_count(page) - 1;
1111da177e4SLinus Torvalds 				page++;
1121da177e4SLinus Torvalds 			} while (page < end);
1131da177e4SLinus Torvalds 		}
1145e709827SRay Lehtiniemi 	}
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	printk("%d pages of RAM\n", total);
1171da177e4SLinus Torvalds 	printk("%d free pages\n", free);
1181da177e4SLinus Torvalds 	printk("%d reserved pages\n", reserved);
1191da177e4SLinus Torvalds 	printk("%d slab pages\n", slab);
1201da177e4SLinus Torvalds 	printk("%d pages shared\n", shared);
1211da177e4SLinus Torvalds 	printk("%d pages swap cached\n", cached);
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
124dde5828fSRussell King static void __init find_node_limits(int node, struct meminfo *mi,
125dde5828fSRussell King 	unsigned long *min, unsigned long *max_low, unsigned long *max_high)
126dde5828fSRussell King {
127dde5828fSRussell King 	int i;
128dde5828fSRussell King 
129dde5828fSRussell King 	*min = -1UL;
130dde5828fSRussell King 	*max_low = *max_high = 0;
131dde5828fSRussell King 
132dde5828fSRussell King 	for_each_nodebank(i, mi, node) {
133dde5828fSRussell King 		struct membank *bank = &mi->bank[i];
134dde5828fSRussell King 		unsigned long start, end;
135dde5828fSRussell King 
136dde5828fSRussell King 		start = bank_pfn_start(bank);
137dde5828fSRussell King 		end = bank_pfn_end(bank);
138dde5828fSRussell King 
139dde5828fSRussell King 		if (*min > start)
140dde5828fSRussell King 			*min = start;
141dde5828fSRussell King 		if (*max_high < end)
142dde5828fSRussell King 			*max_high = end;
143dde5828fSRussell King 		if (bank->highmem)
144dde5828fSRussell King 			continue;
145dde5828fSRussell King 		if (*max_low < end)
146dde5828fSRussell King 			*max_low = end;
147dde5828fSRussell King 	}
148dde5828fSRussell King }
149dde5828fSRussell King 
1501da177e4SLinus Torvalds /*
1511da177e4SLinus Torvalds  * FIXME: We really want to avoid allocating the bootmap bitmap
1521da177e4SLinus Torvalds  * over the top of the initrd.  Hopefully, this is located towards
1531da177e4SLinus Torvalds  * the start of a bank, so if we allocate the bootmap bitmap at
1541da177e4SLinus Torvalds  * the end, we won't clash.
1551da177e4SLinus Torvalds  */
1561da177e4SLinus Torvalds static unsigned int __init
1571da177e4SLinus Torvalds find_bootmap_pfn(int node, struct meminfo *mi, unsigned int bootmap_pages)
1581da177e4SLinus Torvalds {
159d2a38ef9SRussell King 	unsigned int start_pfn, i, bootmap_pfn;
1601da177e4SLinus Torvalds 
16137efe642SRussell King 	start_pfn   = PAGE_ALIGN(__pa(_end)) >> PAGE_SHIFT;
1621da177e4SLinus Torvalds 	bootmap_pfn = 0;
1631da177e4SLinus Torvalds 
164d2a38ef9SRussell King 	for_each_nodebank(i, mi, node) {
165d2a38ef9SRussell King 		struct membank *bank = &mi->bank[i];
1661da177e4SLinus Torvalds 		unsigned int start, end;
1671da177e4SLinus Torvalds 
168d2a38ef9SRussell King 		start = bank_pfn_start(bank);
169d2a38ef9SRussell King 		end   = bank_pfn_end(bank);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 		if (end < start_pfn)
1721da177e4SLinus Torvalds 			continue;
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 		if (start < start_pfn)
1751da177e4SLinus Torvalds 			start = start_pfn;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 		if (end <= start)
1781da177e4SLinus Torvalds 			continue;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds 		if (end - start >= bootmap_pages) {
1811da177e4SLinus Torvalds 			bootmap_pfn = start;
1821da177e4SLinus Torvalds 			break;
1831da177e4SLinus Torvalds 		}
1841da177e4SLinus Torvalds 	}
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds 	if (bootmap_pfn == 0)
1871da177e4SLinus Torvalds 		BUG();
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	return bootmap_pfn;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds static int __init check_initrd(struct meminfo *mi)
1931da177e4SLinus Torvalds {
1941da177e4SLinus Torvalds 	int initrd_node = -2;
1951da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
1961da177e4SLinus Torvalds 	unsigned long end = phys_initrd_start + phys_initrd_size;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds 	/*
1991da177e4SLinus Torvalds 	 * Make sure that the initrd is within a valid area of
2001da177e4SLinus Torvalds 	 * memory.
2011da177e4SLinus Torvalds 	 */
2021da177e4SLinus Torvalds 	if (phys_initrd_size) {
2031da177e4SLinus Torvalds 		unsigned int i;
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 		initrd_node = -1;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds 		for (i = 0; i < mi->nr_banks; i++) {
208d2a38ef9SRussell King 			struct membank *bank = &mi->bank[i];
209d2a38ef9SRussell King 			if (bank_phys_start(bank) <= phys_initrd_start &&
210d2a38ef9SRussell King 			    end <= bank_phys_end(bank))
211d2a38ef9SRussell King 				initrd_node = bank->node;
2121da177e4SLinus Torvalds 		}
2131da177e4SLinus Torvalds 	}
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	if (initrd_node == -1) {
216b962a286SRussell King 		printk(KERN_ERR "INITRD: 0x%08lx+0x%08lx extends beyond "
2171da177e4SLinus Torvalds 		       "physical memory - disabling initrd\n",
218b962a286SRussell King 		       phys_initrd_start, phys_initrd_size);
2191da177e4SLinus Torvalds 		phys_initrd_start = phys_initrd_size = 0;
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds #endif
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds 	return initrd_node;
2241da177e4SLinus Torvalds }
2251da177e4SLinus Torvalds 
226dde5828fSRussell King static void __init bootmem_init_node(int node, struct meminfo *mi,
227dde5828fSRussell King 	unsigned long start_pfn, unsigned long end_pfn)
2281da177e4SLinus Torvalds {
229dde5828fSRussell King 	unsigned long boot_pfn;
23090072059SRussell King 	unsigned int boot_pages;
23190072059SRussell King 	pg_data_t *pgdat;
23290072059SRussell King 	int i;
2331da177e4SLinus Torvalds 
23490072059SRussell King 	/*
23590072059SRussell King 	 * Allocate the bootmem bitmap page.
2361da177e4SLinus Torvalds 	 */
23790072059SRussell King 	boot_pages = bootmem_bootmap_pages(end_pfn - start_pfn);
23890072059SRussell King 	boot_pfn = find_bootmap_pfn(node, mi, boot_pages);
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 	/*
24190072059SRussell King 	 * Initialise the bootmem allocator for this node, handing the
24290072059SRussell King 	 * memory banks over to bootmem.
2431da177e4SLinus Torvalds 	 */
24490072059SRussell King 	node_set_online(node);
24590072059SRussell King 	pgdat = NODE_DATA(node);
24690072059SRussell King 	init_bootmem_node(pgdat, boot_pfn, start_pfn, end_pfn);
24790072059SRussell King 
248d2a38ef9SRussell King 	for_each_nodebank(i, mi, node) {
249d2a38ef9SRussell King 		struct membank *bank = &mi->bank[i];
250dde5828fSRussell King 		if (!bank->highmem)
251d2a38ef9SRussell King 			free_bootmem_node(pgdat, bank_phys_start(bank), bank_phys_size(bank));
252d2a38ef9SRussell King 	}
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	/*
25590072059SRussell King 	 * Reserve the bootmem bitmap for this node.
2561da177e4SLinus Torvalds 	 */
25790072059SRussell King 	reserve_bootmem_node(pgdat, boot_pfn << PAGE_SHIFT,
25872a7fe39SBernhard Walle 			     boot_pages << PAGE_SHIFT, BOOTMEM_DEFAULT);
259b7a69ac3SRussell King }
260b962a286SRussell King 
261b7a69ac3SRussell King static void __init bootmem_reserve_initrd(int node)
262b7a69ac3SRussell King {
2631da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
264b7a69ac3SRussell King 	pg_data_t *pgdat = NODE_DATA(node);
265b7a69ac3SRussell King 	int res;
266b7a69ac3SRussell King 
267b7a69ac3SRussell King 	res = reserve_bootmem_node(pgdat, phys_initrd_start,
268b962a286SRussell King 			     phys_initrd_size, BOOTMEM_EXCLUSIVE);
269b962a286SRussell King 
270b962a286SRussell King 	if (res == 0) {
2711da177e4SLinus Torvalds 		initrd_start = __phys_to_virt(phys_initrd_start);
2721da177e4SLinus Torvalds 		initrd_end = initrd_start + phys_initrd_size;
273b962a286SRussell King 	} else {
274b962a286SRussell King 		printk(KERN_ERR
275b962a286SRussell King 			"INITRD: 0x%08lx+0x%08lx overlaps in-use "
276b962a286SRussell King 			"memory region - disabling initrd\n",
277b962a286SRussell King 			phys_initrd_start, phys_initrd_size);
278b962a286SRussell King 	}
2791da177e4SLinus Torvalds #endif
280b7a69ac3SRussell King }
281b7a69ac3SRussell King 
282b7a69ac3SRussell King static void __init bootmem_free_node(int node, struct meminfo *mi)
283b7a69ac3SRussell King {
284b7a69ac3SRussell King 	unsigned long zone_size[MAX_NR_ZONES], zhole_size[MAX_NR_ZONES];
285dde5828fSRussell King 	unsigned long min, max_low, max_high;
286b7a69ac3SRussell King 	int i;
287b7a69ac3SRussell King 
288dde5828fSRussell King 	find_node_limits(node, mi, &min, &max_low, &max_high);
2891da177e4SLinus Torvalds 
29090072059SRussell King 	/*
29190072059SRussell King 	 * initialise the zones within this node.
29290072059SRussell King 	 */
29390072059SRussell King 	memset(zone_size, 0, sizeof(zone_size));
29490072059SRussell King 
29590072059SRussell King 	/*
29690072059SRussell King 	 * The size of this node has already been determined.  If we need
29790072059SRussell King 	 * to do anything fancy with the allocation of this memory to the
29890072059SRussell King 	 * zones, now is the time to do it.
29990072059SRussell King 	 */
300dde5828fSRussell King 	zone_size[0] = max_low - min;
301dde5828fSRussell King #ifdef CONFIG_HIGHMEM
302dde5828fSRussell King 	zone_size[ZONE_HIGHMEM] = max_high - max_low;
303dde5828fSRussell King #endif
30490072059SRussell King 
30590072059SRussell King 	/*
30690072059SRussell King 	 * For each bank in this node, calculate the size of the holes.
30790072059SRussell King 	 *  holes = node_size - sum(bank_sizes_in_node)
30890072059SRussell King 	 */
309dde5828fSRussell King 	memcpy(zhole_size, zone_size, sizeof(zhole_size));
310dde5828fSRussell King 	for_each_nodebank(i, mi, node) {
311dde5828fSRussell King 		int idx = 0;
312dde5828fSRussell King #ifdef CONFIG_HIGHMEM
313dde5828fSRussell King 		if (mi->bank[i].highmem)
314dde5828fSRussell King 			idx = ZONE_HIGHMEM;
315dde5828fSRussell King #endif
316dde5828fSRussell King 		zhole_size[idx] -= bank_pfn_size(&mi->bank[i]);
317dde5828fSRussell King 	}
31890072059SRussell King 
31990072059SRussell King 	/*
32090072059SRussell King 	 * Adjust the sizes according to any special requirements for
32190072059SRussell King 	 * this machine type.
32290072059SRussell King 	 */
32390072059SRussell King 	arch_adjust_zones(node, zone_size, zhole_size);
32490072059SRussell King 
325dde5828fSRussell King 	free_area_init_node(node, zone_size, min, zhole_size);
32690072059SRussell King }
32790072059SRussell King 
328b7cfda9fSRussell King #ifndef CONFIG_SPARSEMEM
329b7cfda9fSRussell King int pfn_valid(unsigned long pfn)
330b7cfda9fSRussell King {
331b7cfda9fSRussell King 	struct meminfo *mi = &meminfo;
332b7cfda9fSRussell King 	unsigned int left = 0, right = mi->nr_banks;
333b7cfda9fSRussell King 
334b7cfda9fSRussell King 	do {
335b7cfda9fSRussell King 		unsigned int mid = (right + left) / 2;
336b7cfda9fSRussell King 		struct membank *bank = &mi->bank[mid];
337b7cfda9fSRussell King 
338b7cfda9fSRussell King 		if (pfn < bank_pfn_start(bank))
339b7cfda9fSRussell King 			right = mid;
340b7cfda9fSRussell King 		else if (pfn >= bank_pfn_end(bank))
341b7cfda9fSRussell King 			left = mid + 1;
342b7cfda9fSRussell King 		else
343b7cfda9fSRussell King 			return 1;
344b7cfda9fSRussell King 	} while (left < right);
345b7cfda9fSRussell King 	return 0;
346b7cfda9fSRussell King }
347b7cfda9fSRussell King EXPORT_SYMBOL(pfn_valid);
348657e12fdSRussell King 
349657e12fdSRussell King static void arm_memory_present(struct meminfo *mi, int node)
350657e12fdSRussell King {
351657e12fdSRussell King }
352657e12fdSRussell King #else
353657e12fdSRussell King static void arm_memory_present(struct meminfo *mi, int node)
354657e12fdSRussell King {
355657e12fdSRussell King 	int i;
356657e12fdSRussell King 	for_each_nodebank(i, mi, node) {
357657e12fdSRussell King 		struct membank *bank = &mi->bank[i];
358657e12fdSRussell King 		memory_present(node, bank_pfn_start(bank), bank_pfn_end(bank));
359657e12fdSRussell King 	}
360657e12fdSRussell King }
361b7cfda9fSRussell King #endif
362b7cfda9fSRussell King 
3634b5f32ceSNicolas Pitre void __init bootmem_init(void)
36490072059SRussell King {
3654b5f32ceSNicolas Pitre 	struct meminfo *mi = &meminfo;
366dde5828fSRussell King 	unsigned long min, max_low, max_high;
367eca73214SRussell King 	int node, initrd_node;
36890072059SRussell King 
36990072059SRussell King 	/*
37090072059SRussell King 	 * Locate which node contains the ramdisk image, if any.
37190072059SRussell King 	 */
37290072059SRussell King 	initrd_node = check_initrd(mi);
37390072059SRussell King 
374dde5828fSRussell King 	max_low = max_high = 0;
375dde5828fSRussell King 
37690072059SRussell King 	/*
37790072059SRussell King 	 * Run through each node initialising the bootmem allocator.
37890072059SRussell King 	 */
37990072059SRussell King 	for_each_node(node) {
380dde5828fSRussell King 		unsigned long node_low, node_high;
381dde5828fSRussell King 
382dde5828fSRussell King 		find_node_limits(node, mi, &min, &node_low, &node_high);
383dde5828fSRussell King 
384dde5828fSRussell King 		if (node_low > max_low)
385dde5828fSRussell King 			max_low = node_low;
386dde5828fSRussell King 		if (node_high > max_high)
387dde5828fSRussell King 			max_high = node_high;
388dde5828fSRussell King 
389dde5828fSRussell King 		/*
390dde5828fSRussell King 		 * If there is no memory in this node, ignore it.
391dde5828fSRussell King 		 * (We can't have nodes which have no lowmem)
392dde5828fSRussell King 		 */
393dde5828fSRussell King 		if (node_low == 0)
394dde5828fSRussell King 			continue;
395dde5828fSRussell King 
396dde5828fSRussell King 		bootmem_init_node(node, mi, min, node_low);
39790072059SRussell King 
398b7a69ac3SRussell King 		/*
399b7a69ac3SRussell King 		 * Reserve any special node zero regions.
400b7a69ac3SRussell King 		 */
401b7a69ac3SRussell King 		if (node == 0)
402b7a69ac3SRussell King 			reserve_node_zero(NODE_DATA(node));
403b7a69ac3SRussell King 
404b7a69ac3SRussell King 		/*
405b7a69ac3SRussell King 		 * If the initrd is in this node, reserve its memory.
406b7a69ac3SRussell King 		 */
407b7a69ac3SRussell King 		if (node == initrd_node)
408b7a69ac3SRussell King 			bootmem_reserve_initrd(node);
409657e12fdSRussell King 
410657e12fdSRussell King 		/*
411657e12fdSRussell King 		 * Sparsemem tries to allocate bootmem in memory_present(),
412657e12fdSRussell King 		 * so must be done after the fixed reservations
413657e12fdSRussell King 		 */
414657e12fdSRussell King 		arm_memory_present(mi, node);
41590072059SRussell King 	}
41690072059SRussell King 
417b7a69ac3SRussell King 	/*
418b7a69ac3SRussell King 	 * sparse_init() needs the bootmem allocator up and running.
419b7a69ac3SRussell King 	 */
420b7a69ac3SRussell King 	sparse_init();
421b7a69ac3SRussell King 
422b7a69ac3SRussell King 	/*
423b7a69ac3SRussell King 	 * Now free memory in each node - free_area_init_node needs
424b7a69ac3SRussell King 	 * the sparse mem_map arrays initialized by sparse_init()
425b7a69ac3SRussell King 	 * for memmap_init_zone(), otherwise all PFNs are invalid.
426b7a69ac3SRussell King 	 */
427b7a69ac3SRussell King 	for_each_node(node)
428b7a69ac3SRussell King 		bootmem_free_node(node, mi);
429b7a69ac3SRussell King 
430dde5828fSRussell King 	high_memory = __va((max_low << PAGE_SHIFT) - 1) + 1;
43190072059SRussell King 
43290072059SRussell King 	/*
43390072059SRussell King 	 * This doesn't seem to be used by the Linux memory manager any
43490072059SRussell King 	 * more, but is used by ll_rw_block.  If we can get rid of it, we
43590072059SRussell King 	 * also get rid of some of the stuff above as well.
43690072059SRussell King 	 *
43790072059SRussell King 	 * Note: max_low_pfn and max_pfn reflect the number of _pages_ in
43890072059SRussell King 	 * the system, not the maximum PFN.
43990072059SRussell King 	 */
440dde5828fSRussell King 	max_low_pfn = max_low - PHYS_PFN_OFFSET;
441dde5828fSRussell King 	max_pfn = max_high - PHYS_PFN_OFFSET;
44290072059SRussell King }
44390072059SRussell King 
4446db015e4SNicolas Pitre static inline int free_area(unsigned long pfn, unsigned long end, char *s)
4451da177e4SLinus Torvalds {
4466db015e4SNicolas Pitre 	unsigned int pages = 0, size = (end - pfn) << (PAGE_SHIFT - 10);
4471da177e4SLinus Torvalds 
4486db015e4SNicolas Pitre 	for (; pfn < end; pfn++) {
4496db015e4SNicolas Pitre 		struct page *page = pfn_to_page(pfn);
4501da177e4SLinus Torvalds 		ClearPageReserved(page);
4517835e98bSNick Piggin 		init_page_count(page);
4526db015e4SNicolas Pitre 		__free_page(page);
4536db015e4SNicolas Pitre 		pages++;
4541da177e4SLinus Torvalds 	}
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	if (size && s)
4571da177e4SLinus Torvalds 		printk(KERN_INFO "Freeing %s memory: %dK\n", s, size);
4586db015e4SNicolas Pitre 
4596db015e4SNicolas Pitre 	return pages;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
462a013053dSRussell King static inline void
463a013053dSRussell King free_memmap(int node, unsigned long start_pfn, unsigned long end_pfn)
464a013053dSRussell King {
465a013053dSRussell King 	struct page *start_pg, *end_pg;
466a013053dSRussell King 	unsigned long pg, pgend;
467a013053dSRussell King 
468a013053dSRussell King 	/*
469a013053dSRussell King 	 * Convert start_pfn/end_pfn to a struct page pointer.
470a013053dSRussell King 	 */
4713257f43dSCatalin Marinas 	start_pg = pfn_to_page(start_pfn - 1) + 1;
472a013053dSRussell King 	end_pg = pfn_to_page(end_pfn);
473a013053dSRussell King 
474a013053dSRussell King 	/*
475a013053dSRussell King 	 * Convert to physical addresses, and
476a013053dSRussell King 	 * round start upwards and end downwards.
477a013053dSRussell King 	 */
478a013053dSRussell King 	pg = PAGE_ALIGN(__pa(start_pg));
479a013053dSRussell King 	pgend = __pa(end_pg) & PAGE_MASK;
480a013053dSRussell King 
481a013053dSRussell King 	/*
482a013053dSRussell King 	 * If there are free pages between these,
483a013053dSRussell King 	 * free the section of the memmap array.
484a013053dSRussell King 	 */
485a013053dSRussell King 	if (pg < pgend)
486a013053dSRussell King 		free_bootmem_node(NODE_DATA(node), pg, pgend - pg);
487a013053dSRussell King }
488a013053dSRussell King 
489a013053dSRussell King /*
490a013053dSRussell King  * The mem_map array can get very big.  Free the unused area of the memory map.
491a013053dSRussell King  */
492a013053dSRussell King static void __init free_unused_memmap_node(int node, struct meminfo *mi)
493a013053dSRussell King {
494a013053dSRussell King 	unsigned long bank_start, prev_bank_end = 0;
495a013053dSRussell King 	unsigned int i;
496a013053dSRussell King 
497a013053dSRussell King 	/*
498*3260e529SMichael Bohan 	 * This relies on each bank being in address order.
499*3260e529SMichael Bohan 	 * The banks are sorted previously in bootmem_init().
500a013053dSRussell King 	 */
50190072059SRussell King 	for_each_nodebank(i, mi, node) {
502d2a38ef9SRussell King 		struct membank *bank = &mi->bank[i];
503d2a38ef9SRussell King 
504d2a38ef9SRussell King 		bank_start = bank_pfn_start(bank);
505a013053dSRussell King 
506a013053dSRussell King 		/*
507a013053dSRussell King 		 * If we had a previous bank, and there is a space
508a013053dSRussell King 		 * between the current bank and the previous, free it.
509a013053dSRussell King 		 */
510*3260e529SMichael Bohan 		if (prev_bank_end && prev_bank_end < bank_start)
511a013053dSRussell King 			free_memmap(node, prev_bank_end, bank_start);
512a013053dSRussell King 
513*3260e529SMichael Bohan 		/*
514*3260e529SMichael Bohan 		 * Align up here since the VM subsystem insists that the
515*3260e529SMichael Bohan 		 * memmap entries are valid from the bank end aligned to
516*3260e529SMichael Bohan 		 * MAX_ORDER_NR_PAGES.
517*3260e529SMichael Bohan 		 */
518*3260e529SMichael Bohan 		prev_bank_end = ALIGN(bank_pfn_end(bank), MAX_ORDER_NR_PAGES);
519a013053dSRussell King 	}
520a013053dSRussell King }
521a013053dSRussell King 
5221da177e4SLinus Torvalds /*
5231da177e4SLinus Torvalds  * mem_init() marks the free areas in the mem_map and tells us how much
5241da177e4SLinus Torvalds  * memory is free.  This is done after various parts of the system have
5251da177e4SLinus Torvalds  * claimed their memory after the kernel image.
5261da177e4SLinus Torvalds  */
5271da177e4SLinus Torvalds void __init mem_init(void)
5281da177e4SLinus Torvalds {
529db9ef1afSFenkart/Bostandzhyan 	unsigned long reserved_pages, free_pages;
5301da177e4SLinus Torvalds 	int i, node;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds #ifndef CONFIG_DISCONTIGMEM
5333835f6cbSNicolas Pitre 	max_mapnr   = pfn_to_page(max_pfn + PHYS_PFN_OFFSET) - mem_map;
5341da177e4SLinus Torvalds #endif
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	/* this will put all unused low memory onto the freelists */
5371da177e4SLinus Torvalds 	for_each_online_node(node) {
5381da177e4SLinus Torvalds 		pg_data_t *pgdat = NODE_DATA(node);
5391da177e4SLinus Torvalds 
540a013053dSRussell King 		free_unused_memmap_node(node, &meminfo);
541a013053dSRussell King 
5421da177e4SLinus Torvalds 		if (pgdat->node_spanned_pages != 0)
5431da177e4SLinus Torvalds 			totalram_pages += free_all_bootmem_node(pgdat);
5441da177e4SLinus Torvalds 	}
5451da177e4SLinus Torvalds 
5461da177e4SLinus Torvalds #ifdef CONFIG_SA1111
5471da177e4SLinus Torvalds 	/* now that our DMA memory is actually so designated, we can free it */
5486db015e4SNicolas Pitre 	totalram_pages += free_area(PHYS_PFN_OFFSET,
5496db015e4SNicolas Pitre 				    __phys_to_pfn(__pa(swapper_pg_dir)), NULL);
5501da177e4SLinus Torvalds #endif
5511da177e4SLinus Torvalds 
5523835f6cbSNicolas Pitre #ifdef CONFIG_HIGHMEM
5533835f6cbSNicolas Pitre 	/* set highmem page free */
5543835f6cbSNicolas Pitre 	for_each_online_node(node) {
5553835f6cbSNicolas Pitre 		for_each_nodebank (i, &meminfo, node) {
5563835f6cbSNicolas Pitre 			unsigned long start = bank_pfn_start(&meminfo.bank[i]);
5573835f6cbSNicolas Pitre 			unsigned long end = bank_pfn_end(&meminfo.bank[i]);
5583835f6cbSNicolas Pitre 			if (start >= max_low_pfn + PHYS_PFN_OFFSET)
5593835f6cbSNicolas Pitre 				totalhigh_pages += free_area(start, end, NULL);
5603835f6cbSNicolas Pitre 		}
5613835f6cbSNicolas Pitre 	}
5623835f6cbSNicolas Pitre 	totalram_pages += totalhigh_pages;
5633835f6cbSNicolas Pitre #endif
5643835f6cbSNicolas Pitre 
565db9ef1afSFenkart/Bostandzhyan 	reserved_pages = free_pages = 0;
566db9ef1afSFenkart/Bostandzhyan 
567db9ef1afSFenkart/Bostandzhyan 	for_each_online_node(node) {
568db9ef1afSFenkart/Bostandzhyan 		for_each_nodebank(i, &meminfo, node) {
569db9ef1afSFenkart/Bostandzhyan 			struct membank *bank = &meminfo.bank[i];
570db9ef1afSFenkart/Bostandzhyan 			unsigned int pfn1, pfn2;
571db9ef1afSFenkart/Bostandzhyan 			struct page *page, *end;
572db9ef1afSFenkart/Bostandzhyan 
573db9ef1afSFenkart/Bostandzhyan 			pfn1 = bank_pfn_start(bank);
574db9ef1afSFenkart/Bostandzhyan 			pfn2 = bank_pfn_end(bank);
575db9ef1afSFenkart/Bostandzhyan 
576ea056df7SCatalin Marinas 			page = pfn_to_page(pfn1);
577ea056df7SCatalin Marinas 			end  = pfn_to_page(pfn2 - 1) + 1;
578db9ef1afSFenkart/Bostandzhyan 
579db9ef1afSFenkart/Bostandzhyan 			do {
580db9ef1afSFenkart/Bostandzhyan 				if (PageReserved(page))
581db9ef1afSFenkart/Bostandzhyan 					reserved_pages++;
582db9ef1afSFenkart/Bostandzhyan 				else if (!page_count(page))
583db9ef1afSFenkart/Bostandzhyan 					free_pages++;
584db9ef1afSFenkart/Bostandzhyan 				page++;
585db9ef1afSFenkart/Bostandzhyan 			} while (page < end);
586db9ef1afSFenkart/Bostandzhyan 		}
587db9ef1afSFenkart/Bostandzhyan 	}
588db9ef1afSFenkart/Bostandzhyan 
5891da177e4SLinus Torvalds 	/*
5901da177e4SLinus Torvalds 	 * Since our memory may not be contiguous, calculate the
5911da177e4SLinus Torvalds 	 * real number of pages we have in this system
5921da177e4SLinus Torvalds 	 */
5931da177e4SLinus Torvalds 	printk(KERN_INFO "Memory:");
5941da177e4SLinus Torvalds 	num_physpages = 0;
5951da177e4SLinus Torvalds 	for (i = 0; i < meminfo.nr_banks; i++) {
596d2a38ef9SRussell King 		num_physpages += bank_pfn_size(&meminfo.bank[i]);
597d2a38ef9SRussell King 		printk(" %ldMB", bank_phys_size(&meminfo.bank[i]) >> 20);
5981da177e4SLinus Torvalds 	}
5991da177e4SLinus Torvalds 	printk(" = %luMB total\n", num_physpages >> (20 - PAGE_SHIFT));
6006db015e4SNicolas Pitre 
601db9ef1afSFenkart/Bostandzhyan 	printk(KERN_NOTICE "Memory: %luk/%luk available, %luk reserved, %luK highmem\n",
602db9ef1afSFenkart/Bostandzhyan 		nr_free_pages() << (PAGE_SHIFT-10),
603db9ef1afSFenkart/Bostandzhyan 		free_pages << (PAGE_SHIFT-10),
604db9ef1afSFenkart/Bostandzhyan 		reserved_pages << (PAGE_SHIFT-10),
6054b529401SAndreas Fenkart 		totalhigh_pages << (PAGE_SHIFT-10));
6061da177e4SLinus Torvalds 
607db9ef1afSFenkart/Bostandzhyan #define MLK(b, t) b, t, ((t) - (b)) >> 10
608db9ef1afSFenkart/Bostandzhyan #define MLM(b, t) b, t, ((t) - (b)) >> 20
609db9ef1afSFenkart/Bostandzhyan #define MLK_ROUNDUP(b, t) b, t, DIV_ROUND_UP(((t) - (b)), SZ_1K)
610db9ef1afSFenkart/Bostandzhyan 
611db9ef1afSFenkart/Bostandzhyan 	printk(KERN_NOTICE "Virtual kernel memory layout:\n"
612db9ef1afSFenkart/Bostandzhyan 			"    vector  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
613db9ef1afSFenkart/Bostandzhyan 			"    fixmap  : 0x%08lx - 0x%08lx   (%4ld kB)\n"
614a7bd08c8SFenkart/Bostandzhyan #ifdef CONFIG_MMU
615a7bd08c8SFenkart/Bostandzhyan 			"    DMA     : 0x%08lx - 0x%08lx   (%4ld MB)\n"
616a7bd08c8SFenkart/Bostandzhyan #endif
617db9ef1afSFenkart/Bostandzhyan 			"    vmalloc : 0x%08lx - 0x%08lx   (%4ld MB)\n"
618db9ef1afSFenkart/Bostandzhyan 			"    lowmem  : 0x%08lx - 0x%08lx   (%4ld MB)\n"
619db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
620db9ef1afSFenkart/Bostandzhyan 			"    pkmap   : 0x%08lx - 0x%08lx   (%4ld MB)\n"
621db9ef1afSFenkart/Bostandzhyan #endif
622db9ef1afSFenkart/Bostandzhyan 			"    modules : 0x%08lx - 0x%08lx   (%4ld MB)\n"
623db9ef1afSFenkart/Bostandzhyan 			"      .init : 0x%p" " - 0x%p" "   (%4d kB)\n"
624db9ef1afSFenkart/Bostandzhyan 			"      .text : 0x%p" " - 0x%p" "   (%4d kB)\n"
625db9ef1afSFenkart/Bostandzhyan 			"      .data : 0x%p" " - 0x%p" "   (%4d kB)\n",
626db9ef1afSFenkart/Bostandzhyan 
627db9ef1afSFenkart/Bostandzhyan 			MLK(UL(CONFIG_VECTORS_BASE), UL(CONFIG_VECTORS_BASE) +
628db9ef1afSFenkart/Bostandzhyan 				(PAGE_SIZE)),
629db9ef1afSFenkart/Bostandzhyan 			MLK(FIXADDR_START, FIXADDR_TOP),
630a7bd08c8SFenkart/Bostandzhyan #ifdef CONFIG_MMU
631a7bd08c8SFenkart/Bostandzhyan 			MLM(CONSISTENT_BASE, CONSISTENT_END),
632a7bd08c8SFenkart/Bostandzhyan #endif
633c931b4f6SFenkart/Bostandzhyan 			MLM(VMALLOC_START, VMALLOC_END),
634db9ef1afSFenkart/Bostandzhyan 			MLM(PAGE_OFFSET, (unsigned long)high_memory),
635db9ef1afSFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
636db9ef1afSFenkart/Bostandzhyan 			MLM(PKMAP_BASE, (PKMAP_BASE) + (LAST_PKMAP) *
637db9ef1afSFenkart/Bostandzhyan 				(PAGE_SIZE)),
638db9ef1afSFenkart/Bostandzhyan #endif
639db9ef1afSFenkart/Bostandzhyan 			MLM(MODULES_VADDR, MODULES_END),
640db9ef1afSFenkart/Bostandzhyan 
641db9ef1afSFenkart/Bostandzhyan 			MLK_ROUNDUP(__init_begin, __init_end),
642db9ef1afSFenkart/Bostandzhyan 			MLK_ROUNDUP(_text, _etext),
643db9ef1afSFenkart/Bostandzhyan 			MLK_ROUNDUP(_data, _edata));
644db9ef1afSFenkart/Bostandzhyan 
645db9ef1afSFenkart/Bostandzhyan #undef MLK
646db9ef1afSFenkart/Bostandzhyan #undef MLM
647db9ef1afSFenkart/Bostandzhyan #undef MLK_ROUNDUP
648db9ef1afSFenkart/Bostandzhyan 
649a1839272SFenkart/Bostandzhyan 	/*
650a1839272SFenkart/Bostandzhyan 	 * Check boundaries twice: Some fundamental inconsistencies can
651a1839272SFenkart/Bostandzhyan 	 * be detected at build time already.
652a1839272SFenkart/Bostandzhyan 	 */
653a1839272SFenkart/Bostandzhyan #ifdef CONFIG_MMU
654a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(VMALLOC_END			> CONSISTENT_BASE);
655a1839272SFenkart/Bostandzhyan 	BUG_ON(VMALLOC_END				> CONSISTENT_BASE);
656a1839272SFenkart/Bostandzhyan 
657a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(TASK_SIZE				> MODULES_VADDR);
658a1839272SFenkart/Bostandzhyan 	BUG_ON(TASK_SIZE 				> MODULES_VADDR);
659a1839272SFenkart/Bostandzhyan #endif
660a1839272SFenkart/Bostandzhyan 
661a1839272SFenkart/Bostandzhyan #ifdef CONFIG_HIGHMEM
662a1839272SFenkart/Bostandzhyan 	BUILD_BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE > PAGE_OFFSET);
663a1839272SFenkart/Bostandzhyan 	BUG_ON(PKMAP_BASE + LAST_PKMAP * PAGE_SIZE	> PAGE_OFFSET);
664a1839272SFenkart/Bostandzhyan #endif
665a1839272SFenkart/Bostandzhyan 
6661da177e4SLinus Torvalds 	if (PAGE_SIZE >= 16384 && num_physpages <= 128) {
6671da177e4SLinus Torvalds 		extern int sysctl_overcommit_memory;
6681da177e4SLinus Torvalds 		/*
6691da177e4SLinus Torvalds 		 * On a machine this small we won't get
6701da177e4SLinus Torvalds 		 * anywhere without overcommit, so turn
6711da177e4SLinus Torvalds 		 * it on by default.
6721da177e4SLinus Torvalds 		 */
6731da177e4SLinus Torvalds 		sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
6741da177e4SLinus Torvalds 	}
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds void free_initmem(void)
6781da177e4SLinus Torvalds {
679bc581770SLinus Walleij #ifdef CONFIG_HAVE_TCM
680ea208f64SLinus Walleij 	extern char __tcm_start, __tcm_end;
681bc581770SLinus Walleij 
682ea208f64SLinus Walleij 	totalram_pages += free_area(__phys_to_pfn(__pa(&__tcm_start)),
683ea208f64SLinus Walleij 				    __phys_to_pfn(__pa(&__tcm_end)),
684bc581770SLinus Walleij 				    "TCM link");
685bc581770SLinus Walleij #endif
686bc581770SLinus Walleij 
6876db015e4SNicolas Pitre 	if (!machine_is_integrator() && !machine_is_cintegrator())
68837efe642SRussell King 		totalram_pages += free_area(__phys_to_pfn(__pa(__init_begin)),
68937efe642SRussell King 					    __phys_to_pfn(__pa(__init_end)),
6901da177e4SLinus Torvalds 					    "init");
6911da177e4SLinus Torvalds }
6921da177e4SLinus Torvalds 
6931da177e4SLinus Torvalds #ifdef CONFIG_BLK_DEV_INITRD
6941da177e4SLinus Torvalds 
6951da177e4SLinus Torvalds static int keep_initrd;
6961da177e4SLinus Torvalds 
6971da177e4SLinus Torvalds void free_initrd_mem(unsigned long start, unsigned long end)
6981da177e4SLinus Torvalds {
6991da177e4SLinus Torvalds 	if (!keep_initrd)
7006db015e4SNicolas Pitre 		totalram_pages += free_area(__phys_to_pfn(__pa(start)),
7016db015e4SNicolas Pitre 					    __phys_to_pfn(__pa(end)),
7026db015e4SNicolas Pitre 					    "initrd");
7031da177e4SLinus Torvalds }
7041da177e4SLinus Torvalds 
7051da177e4SLinus Torvalds static int __init keepinitrd_setup(char *__unused)
7061da177e4SLinus Torvalds {
7071da177e4SLinus Torvalds 	keep_initrd = 1;
7081da177e4SLinus Torvalds 	return 1;
7091da177e4SLinus Torvalds }
7101da177e4SLinus Torvalds 
7111da177e4SLinus Torvalds __setup("keepinitrd", keepinitrd_setup);
7121da177e4SLinus Torvalds #endif
713