xref: /openbmc/linux/arch/x86/kernel/e820.c (revision 816ffd28002651a469e86d1118a225862e392ecd)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b79cd8f1SYinghai Lu /*
3640e1b38SIngo Molnar  * Low level x86 E820 memory map handling functions.
4b79cd8f1SYinghai Lu  *
5640e1b38SIngo Molnar  * The firmware and bootloader passes us the "E820 table", which is the primary
6640e1b38SIngo Molnar  * physical memory layout description available about x86 systems.
7b79cd8f1SYinghai Lu  *
8640e1b38SIngo Molnar  * The kernel takes the E820 memory layout and optionally modifies it with
9640e1b38SIngo Molnar  * quirks and other tweaks, and feeds that into the generic Linux memory
10640e1b38SIngo Molnar  * allocation code routines via a platform independent interface (memblock, etc.).
11b79cd8f1SYinghai Lu  */
1293a72052SOlaf Hering #include <linux/crash_dump.h>
1357c8a661SMike Rapoport #include <linux/memblock.h>
14bf62f398SYinghai Lu #include <linux/suspend.h>
15976513dbSRafael J. Wysocki #include <linux/acpi.h>
165dfcf14dSBernhard Walle #include <linux/firmware-map.h>
17d1bbdd66SMike Ditto #include <linux/sort.h>
18357b4da5SJuergen Gross #include <linux/memory_hotplug.h>
19b79cd8f1SYinghai Lu 
2066441bd3SIngo Molnar #include <asm/e820/api.h>
21b79cd8f1SYinghai Lu #include <asm/setup.h>
22b79cd8f1SYinghai Lu 
235dfcf14dSBernhard Walle /*
2412df216cSChen Yu  * We organize the E820 table into three main data structures:
25544a0f47SIngo Molnar  *
2612df216cSChen Yu  * - 'e820_table_firmware': the original firmware version passed to us by the
2712df216cSChen Yu  *   bootloader - not modified by the kernel. It is composed of two parts:
2812df216cSChen Yu  *   the first 128 E820 memory entries in boot_params.e820_table and the remaining
2912df216cSChen Yu  *   (if any) entries of the SETUP_E820_EXT nodes. We use this to:
30544a0f47SIngo Molnar  *
31544a0f47SIngo Molnar  *       - inform the user about the firmware's notion of memory layout
32544a0f47SIngo Molnar  *         via /sys/firmware/memmap
33544a0f47SIngo Molnar  *
34f5d1499aSChris von Recklinghausen  *       - the hibernation code uses it to generate a kernel-independent CRC32
35f5d1499aSChris von Recklinghausen  *         checksum of the physical memory layout of a system.
36544a0f47SIngo Molnar  *
3712df216cSChen Yu  * - 'e820_table_kexec': a slightly modified (by the kernel) firmware version
3812df216cSChen Yu  *   passed to us by the bootloader - the major difference between
3912df216cSChen Yu  *   e820_table_firmware[] and this one is that, the latter marks the setup_data
4012df216cSChen Yu  *   list created by the EFI boot stub as reserved, so that kexec can reuse the
4112df216cSChen Yu  *   setup_data information in the second kernel. Besides, e820_table_kexec[]
4212df216cSChen Yu  *   might also be modified by the kexec itself to fake a mptable.
4312df216cSChen Yu  *   We use this to:
4412df216cSChen Yu  *
45640e1b38SIngo Molnar  *       - kexec, which is a bootloader in disguise, uses the original E820
46544a0f47SIngo Molnar  *         layout to pass to the kexec-ed kernel. This way the original kernel
47640e1b38SIngo Molnar  *         can have a restricted E820 map while the kexec()-ed kexec-kernel
48544a0f47SIngo Molnar  *         can have access to full memory - etc.
49544a0f47SIngo Molnar  *
50640e1b38SIngo Molnar  * - 'e820_table': this is the main E820 table that is massaged by the
51544a0f47SIngo Molnar  *   low level x86 platform code, or modified by boot parameters, before
52544a0f47SIngo Molnar  *   passed on to higher level MM layers.
53544a0f47SIngo Molnar  *
54640e1b38SIngo Molnar  * Once the E820 map has been converted to the standard Linux memory layout
55544a0f47SIngo Molnar  * information its role stops - modifying it has no effect and does not get
56544a0f47SIngo Molnar  * re-propagated. So its main role is a temporary bootstrap storage of firmware
57544a0f47SIngo Molnar  * specific memory layout data during early bootup.
585dfcf14dSBernhard Walle  */
5961a50101SIngo Molnar static struct e820_table e820_table_init		__initdata;
60a09bae0fSChen Yu static struct e820_table e820_table_kexec_init		__initdata;
6112df216cSChen Yu static struct e820_table e820_table_firmware_init	__initdata;
62544a0f47SIngo Molnar 
6361a50101SIngo Molnar struct e820_table *e820_table __refdata			= &e820_table_init;
64a09bae0fSChen Yu struct e820_table *e820_table_kexec __refdata		= &e820_table_kexec_init;
6512df216cSChen Yu struct e820_table *e820_table_firmware __refdata	= &e820_table_firmware_init;
66b79cd8f1SYinghai Lu 
67b79cd8f1SYinghai Lu /* For PCI or other memory-mapped resources */
68b79cd8f1SYinghai Lu unsigned long pci_mem_start = 0xaeedbabe;
69b79cd8f1SYinghai Lu #ifdef CONFIG_PCI
70b79cd8f1SYinghai Lu EXPORT_SYMBOL(pci_mem_start);
71b79cd8f1SYinghai Lu #endif
72b79cd8f1SYinghai Lu 
73b79cd8f1SYinghai Lu /*
74b79cd8f1SYinghai Lu  * This function checks if any part of the range <start,end> is mapped
75b79cd8f1SYinghai Lu  * with type.
76b79cd8f1SYinghai Lu  */
_e820__mapped_any(struct e820_table * table,u64 start,u64 end,enum e820_type type)770c55671fSKarimAllah Ahmed static bool _e820__mapped_any(struct e820_table *table,
780c55671fSKarimAllah Ahmed 			      u64 start, u64 end, enum e820_type type)
79b79cd8f1SYinghai Lu {
80b79cd8f1SYinghai Lu 	int i;
81b79cd8f1SYinghai Lu 
820c55671fSKarimAllah Ahmed 	for (i = 0; i < table->nr_entries; i++) {
830c55671fSKarimAllah Ahmed 		struct e820_entry *entry = &table->entries[i];
84b79cd8f1SYinghai Lu 
85e5540f87SIngo Molnar 		if (type && entry->type != type)
86b79cd8f1SYinghai Lu 			continue;
87e5540f87SIngo Molnar 		if (entry->addr >= end || entry->addr + entry->size <= start)
88b79cd8f1SYinghai Lu 			continue;
89f709f814SYi Wang 		return true;
90b79cd8f1SYinghai Lu 	}
91f709f814SYi Wang 	return false;
92b79cd8f1SYinghai Lu }
930c55671fSKarimAllah Ahmed 
e820__mapped_raw_any(u64 start,u64 end,enum e820_type type)940c55671fSKarimAllah Ahmed bool e820__mapped_raw_any(u64 start, u64 end, enum e820_type type)
950c55671fSKarimAllah Ahmed {
960c55671fSKarimAllah Ahmed 	return _e820__mapped_any(e820_table_firmware, start, end, type);
970c55671fSKarimAllah Ahmed }
980c55671fSKarimAllah Ahmed EXPORT_SYMBOL_GPL(e820__mapped_raw_any);
990c55671fSKarimAllah Ahmed 
e820__mapped_any(u64 start,u64 end,enum e820_type type)1000c55671fSKarimAllah Ahmed bool e820__mapped_any(u64 start, u64 end, enum e820_type type)
1010c55671fSKarimAllah Ahmed {
1020c55671fSKarimAllah Ahmed 	return _e820__mapped_any(e820_table, start, end, type);
1030c55671fSKarimAllah Ahmed }
1043bce64f0SIngo Molnar EXPORT_SYMBOL_GPL(e820__mapped_any);
105b79cd8f1SYinghai Lu 
106b79cd8f1SYinghai Lu /*
107640e1b38SIngo Molnar  * This function checks if the entire <start,end> range is mapped with 'type'.
108b79cd8f1SYinghai Lu  *
109640e1b38SIngo Molnar  * Note: this function only works correctly once the E820 table is sorted and
110640e1b38SIngo Molnar  * not-overlapping (at least for the range specified), which is the case normally.
111b79cd8f1SYinghai Lu  */
__e820__mapped_all(u64 start,u64 end,enum e820_type type)112d68baa3fSTom Lendacky static struct e820_entry *__e820__mapped_all(u64 start, u64 end,
113d68baa3fSTom Lendacky 					     enum e820_type type)
114b79cd8f1SYinghai Lu {
115b79cd8f1SYinghai Lu 	int i;
116b79cd8f1SYinghai Lu 
117bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
118e5540f87SIngo Molnar 		struct e820_entry *entry = &e820_table->entries[i];
119b79cd8f1SYinghai Lu 
120e5540f87SIngo Molnar 		if (type && entry->type != type)
121b79cd8f1SYinghai Lu 			continue;
122640e1b38SIngo Molnar 
123640e1b38SIngo Molnar 		/* Is the region (part) in overlap with the current region? */
124e5540f87SIngo Molnar 		if (entry->addr >= end || entry->addr + entry->size <= start)
125b79cd8f1SYinghai Lu 			continue;
126b79cd8f1SYinghai Lu 
127640e1b38SIngo Molnar 		/*
128640e1b38SIngo Molnar 		 * If the region is at the beginning of <start,end> we move
129640e1b38SIngo Molnar 		 * 'start' to the end of the region since it's ok until there
130b79cd8f1SYinghai Lu 		 */
131e5540f87SIngo Molnar 		if (entry->addr <= start)
132e5540f87SIngo Molnar 			start = entry->addr + entry->size;
133640e1b38SIngo Molnar 
134b79cd8f1SYinghai Lu 		/*
135640e1b38SIngo Molnar 		 * If 'start' is now at or beyond 'end', we're done, full
136640e1b38SIngo Molnar 		 * coverage of the desired range exists:
137b79cd8f1SYinghai Lu 		 */
138b79cd8f1SYinghai Lu 		if (start >= end)
139d68baa3fSTom Lendacky 			return entry;
140b79cd8f1SYinghai Lu 	}
141d68baa3fSTom Lendacky 
142d68baa3fSTom Lendacky 	return NULL;
143d68baa3fSTom Lendacky }
144d68baa3fSTom Lendacky 
145d68baa3fSTom Lendacky /*
146d68baa3fSTom Lendacky  * This function checks if the entire range <start,end> is mapped with type.
147d68baa3fSTom Lendacky  */
e820__mapped_all(u64 start,u64 end,enum e820_type type)148d68baa3fSTom Lendacky bool __init e820__mapped_all(u64 start, u64 end, enum e820_type type)
149d68baa3fSTom Lendacky {
150d68baa3fSTom Lendacky 	return __e820__mapped_all(start, end, type);
151d68baa3fSTom Lendacky }
152d68baa3fSTom Lendacky 
153d68baa3fSTom Lendacky /*
154d68baa3fSTom Lendacky  * This function returns the type associated with the range <start,end>.
155d68baa3fSTom Lendacky  */
e820__get_entry_type(u64 start,u64 end)156d68baa3fSTom Lendacky int e820__get_entry_type(u64 start, u64 end)
157d68baa3fSTom Lendacky {
158d68baa3fSTom Lendacky 	struct e820_entry *entry = __e820__mapped_all(start, end, 0);
159d68baa3fSTom Lendacky 
160d68baa3fSTom Lendacky 	return entry ? entry->type : -EINVAL;
161b79cd8f1SYinghai Lu }
162b79cd8f1SYinghai Lu 
163b79cd8f1SYinghai Lu /*
164640e1b38SIngo Molnar  * Add a memory region to the kernel E820 map.
165b79cd8f1SYinghai Lu  */
__e820__range_add(struct e820_table * table,u64 start,u64 size,enum e820_type type)1666afc03b8SIngo Molnar static void __init __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type)
167b79cd8f1SYinghai Lu {
168bf495573SIngo Molnar 	int x = table->nr_entries;
169b79cd8f1SYinghai Lu 
170bf495573SIngo Molnar 	if (x >= ARRAY_SIZE(table->entries)) {
1711de392f5SJoe Perches 		pr_err("too many entries; ignoring [mem %#010llx-%#010llx]\n",
1721de392f5SJoe Perches 		       start, start + size - 1);
173b79cd8f1SYinghai Lu 		return;
174b79cd8f1SYinghai Lu 	}
175b79cd8f1SYinghai Lu 
176bf495573SIngo Molnar 	table->entries[x].addr = start;
177bf495573SIngo Molnar 	table->entries[x].size = size;
178bf495573SIngo Molnar 	table->entries[x].type = type;
179bf495573SIngo Molnar 	table->nr_entries++;
180773e673dSYinghai Lu }
181773e673dSYinghai Lu 
e820__range_add(u64 start,u64 size,enum e820_type type)1826afc03b8SIngo Molnar void __init e820__range_add(u64 start, u64 size, enum e820_type type)
183773e673dSYinghai Lu {
184ab6bc04cSIngo Molnar 	__e820__range_add(e820_table, start, size, type);
185b79cd8f1SYinghai Lu }
186b79cd8f1SYinghai Lu 
e820_print_type(enum e820_type type)1876afc03b8SIngo Molnar static void __init e820_print_type(enum e820_type type)
188c61cf4cfSYinghai Lu {
189c61cf4cfSYinghai Lu 	switch (type) {
19009821ff1SIngo Molnar 	case E820_TYPE_RAM:		/* Fall through: */
19109821ff1SIngo Molnar 	case E820_TYPE_RESERVED_KERN:	pr_cont("usable");			break;
19209821ff1SIngo Molnar 	case E820_TYPE_RESERVED:	pr_cont("reserved");			break;
193262b45aeSDan Williams 	case E820_TYPE_SOFT_RESERVED:	pr_cont("soft reserved");		break;
19409821ff1SIngo Molnar 	case E820_TYPE_ACPI:		pr_cont("ACPI data");			break;
19509821ff1SIngo Molnar 	case E820_TYPE_NVS:		pr_cont("ACPI NVS");			break;
19609821ff1SIngo Molnar 	case E820_TYPE_UNUSABLE:	pr_cont("unusable");			break;
19709821ff1SIngo Molnar 	case E820_TYPE_PMEM:		/* Fall through: */
19809821ff1SIngo Molnar 	case E820_TYPE_PRAM:		pr_cont("persistent (type %u)", type);	break;
19901259ef1SIngo Molnar 	default:			pr_cont("type %u", type);		break;
200c61cf4cfSYinghai Lu 	}
201c61cf4cfSYinghai Lu }
202c61cf4cfSYinghai Lu 
e820__print_table(char * who)203be0c3f0fSIngo Molnar void __init e820__print_table(char *who)
204b79cd8f1SYinghai Lu {
205b79cd8f1SYinghai Lu 	int i;
206b79cd8f1SYinghai Lu 
207bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
2081de392f5SJoe Perches 		pr_info("%s: [mem %#018Lx-%#018Lx] ",
2091de392f5SJoe Perches 			who,
210640e1b38SIngo Molnar 			e820_table->entries[i].addr,
211640e1b38SIngo Molnar 			e820_table->entries[i].addr + e820_table->entries[i].size - 1);
212640e1b38SIngo Molnar 
213bf495573SIngo Molnar 		e820_print_type(e820_table->entries[i].type);
21401259ef1SIngo Molnar 		pr_cont("\n");
215b79cd8f1SYinghai Lu 	}
216b79cd8f1SYinghai Lu }
217b79cd8f1SYinghai Lu 
218b79cd8f1SYinghai Lu /*
2199a02fd0fSIngo Molnar  * Sanitize an E820 map.
220b79cd8f1SYinghai Lu  *
2219a02fd0fSIngo Molnar  * Some E820 layouts include overlapping entries. The following
222640e1b38SIngo Molnar  * replaces the original E820 map with a new one, removing overlaps,
2235b7eb2e9SPaul Jackson  * and resolving conflicting memory types in favor of highest
2245b7eb2e9SPaul Jackson  * numbered type.
225b79cd8f1SYinghai Lu  *
2269a02fd0fSIngo Molnar  * The input parameter 'entries' points to an array of 'struct
2279a02fd0fSIngo Molnar  * e820_entry' which on entry has elements in the range [0, *nr_entries)
2289a02fd0fSIngo Molnar  * valid, and which has space for up to max_nr_entries entries.
229640e1b38SIngo Molnar  * On return, the resulting sanitized E820 map entries will be in
2309a02fd0fSIngo Molnar  * overwritten in the same location, starting at 'entries'.
2315b7eb2e9SPaul Jackson  *
2329a02fd0fSIngo Molnar  * The integer pointed to by nr_entries must be valid on entry (the
2339a02fd0fSIngo Molnar  * current number of valid entries located at 'entries'). If the
2349a02fd0fSIngo Molnar  * sanitizing succeeds the *nr_entries will be updated with the new
2359a02fd0fSIngo Molnar  * number of valid entries (something no more than max_nr_entries).
2365b7eb2e9SPaul Jackson  *
237f52355a9SIngo Molnar  * The return value from e820__update_table() is zero if it
2385b7eb2e9SPaul Jackson  * successfully 'sanitized' the map entries passed in, and is -1
2395b7eb2e9SPaul Jackson  * if it did nothing, which can happen if either of (1) it was
2405b7eb2e9SPaul Jackson  * only passed one map entry, or (2) any of the input map entries
2415b7eb2e9SPaul Jackson  * were invalid (start + size < start, meaning that the size was
2425b7eb2e9SPaul Jackson  * so big the described memory range wrapped around through zero.)
2435b7eb2e9SPaul Jackson  *
2445b7eb2e9SPaul Jackson  *	Visually we're performing the following
2455b7eb2e9SPaul Jackson  *	(1,2,3,4 = memory types)...
2465b7eb2e9SPaul Jackson  *
2475b7eb2e9SPaul Jackson  *	Sample memory map (w/overlaps):
2485b7eb2e9SPaul Jackson  *	   ____22__________________
2495b7eb2e9SPaul Jackson  *	   ______________________4_
2505b7eb2e9SPaul Jackson  *	   ____1111________________
2515b7eb2e9SPaul Jackson  *	   _44_____________________
2525b7eb2e9SPaul Jackson  *	   11111111________________
2535b7eb2e9SPaul Jackson  *	   ____________________33__
2545b7eb2e9SPaul Jackson  *	   ___________44___________
2555b7eb2e9SPaul Jackson  *	   __________33333_________
2565b7eb2e9SPaul Jackson  *	   ______________22________
2575b7eb2e9SPaul Jackson  *	   ___________________2222_
2585b7eb2e9SPaul Jackson  *	   _________111111111______
2595b7eb2e9SPaul Jackson  *	   _____________________11_
2605b7eb2e9SPaul Jackson  *	   _________________4______
2615b7eb2e9SPaul Jackson  *
2625b7eb2e9SPaul Jackson  *	Sanitized equivalent (no overlap):
2635b7eb2e9SPaul Jackson  *	   1_______________________
2645b7eb2e9SPaul Jackson  *	   _44_____________________
2655b7eb2e9SPaul Jackson  *	   ___1____________________
2665b7eb2e9SPaul Jackson  *	   ____22__________________
2675b7eb2e9SPaul Jackson  *	   ______11________________
2685b7eb2e9SPaul Jackson  *	   _________1______________
2695b7eb2e9SPaul Jackson  *	   __________3_____________
2705b7eb2e9SPaul Jackson  *	   ___________44___________
2715b7eb2e9SPaul Jackson  *	   _____________33_________
2725b7eb2e9SPaul Jackson  *	   _______________2________
2735b7eb2e9SPaul Jackson  *	   ________________1_______
2745b7eb2e9SPaul Jackson  *	   _________________4______
2755b7eb2e9SPaul Jackson  *	   ___________________2____
2765b7eb2e9SPaul Jackson  *	   ____________________33__
2775b7eb2e9SPaul Jackson  *	   ______________________4_
278b79cd8f1SYinghai Lu  */
279b79cd8f1SYinghai Lu struct change_member {
2809a02fd0fSIngo Molnar 	/* Pointer to the original entry: */
2819a02fd0fSIngo Molnar 	struct e820_entry	*entry;
282640e1b38SIngo Molnar 	/* Address for this change point: */
283640e1b38SIngo Molnar 	unsigned long long	addr;
284b79cd8f1SYinghai Lu };
285d1bbdd66SMike Ditto 
286441ac2f3SIngo Molnar static struct change_member	change_point_list[2*E820_MAX_ENTRIES]	__initdata;
287441ac2f3SIngo Molnar static struct change_member	*change_point[2*E820_MAX_ENTRIES]	__initdata;
288441ac2f3SIngo Molnar static struct e820_entry	*overlap_list[E820_MAX_ENTRIES]		__initdata;
289441ac2f3SIngo Molnar static struct e820_entry	new_entries[E820_MAX_ENTRIES]		__initdata;
290441ac2f3SIngo Molnar 
cpcompare(const void * a,const void * b)291d1bbdd66SMike Ditto static int __init cpcompare(const void *a, const void *b)
292d1bbdd66SMike Ditto {
293d1bbdd66SMike Ditto 	struct change_member * const *app = a, * const *bpp = b;
294d1bbdd66SMike Ditto 	const struct change_member *ap = *app, *bp = *bpp;
295d1bbdd66SMike Ditto 
296d1bbdd66SMike Ditto 	/*
297d1bbdd66SMike Ditto 	 * Inputs are pointers to two elements of change_point[].  If their
298640e1b38SIngo Molnar 	 * addresses are not equal, their difference dominates.  If the addresses
299d1bbdd66SMike Ditto 	 * are equal, then consider one that represents the end of its region
300d1bbdd66SMike Ditto 	 * to be greater than one that does not.
301d1bbdd66SMike Ditto 	 */
302d1bbdd66SMike Ditto 	if (ap->addr != bp->addr)
303d1bbdd66SMike Ditto 		return ap->addr > bp->addr ? 1 : -1;
304d1bbdd66SMike Ditto 
3059a02fd0fSIngo Molnar 	return (ap->addr != ap->entry->addr) - (bp->addr != bp->entry->addr);
306d1bbdd66SMike Ditto }
307d1bbdd66SMike Ditto 
e820_nomerge(enum e820_type type)30888e9a5b7SDan Williams static bool e820_nomerge(enum e820_type type)
30988e9a5b7SDan Williams {
31088e9a5b7SDan Williams 	/*
31188e9a5b7SDan Williams 	 * These types may indicate distinct platform ranges aligned to
31288e9a5b7SDan Williams 	 * numa node, protection domain, performance domain, or other
31388e9a5b7SDan Williams 	 * boundaries. Do not merge them.
31488e9a5b7SDan Williams 	 */
31588e9a5b7SDan Williams 	if (type == E820_TYPE_PRAM)
31688e9a5b7SDan Williams 		return true;
31788e9a5b7SDan Williams 	if (type == E820_TYPE_SOFT_RESERVED)
31888e9a5b7SDan Williams 		return true;
31988e9a5b7SDan Williams 	return false;
32088e9a5b7SDan Williams }
32188e9a5b7SDan Williams 
e820__update_table(struct e820_table * table)322441ac2f3SIngo Molnar int __init e820__update_table(struct e820_table *table)
323d1bbdd66SMike Ditto {
324441ac2f3SIngo Molnar 	struct e820_entry *entries = table->entries;
325441ac2f3SIngo Molnar 	u32 max_nr_entries = ARRAY_SIZE(table->entries);
3266afc03b8SIngo Molnar 	enum e820_type current_type, last_type;
327b79cd8f1SYinghai Lu 	unsigned long long last_addr;
328441ac2f3SIngo Molnar 	u32 new_nr_entries, overlap_entries;
329441ac2f3SIngo Molnar 	u32 i, chg_idx, chg_nr;
330b79cd8f1SYinghai Lu 
331640e1b38SIngo Molnar 	/* If there's only one memory region, don't bother: */
332441ac2f3SIngo Molnar 	if (table->nr_entries < 2)
333b79cd8f1SYinghai Lu 		return -1;
334b79cd8f1SYinghai Lu 
335441ac2f3SIngo Molnar 	BUG_ON(table->nr_entries > max_nr_entries);
336b79cd8f1SYinghai Lu 
3379a02fd0fSIngo Molnar 	/* Bail out if we find any unreasonable addresses in the map: */
338441ac2f3SIngo Molnar 	for (i = 0; i < table->nr_entries; i++) {
3399a02fd0fSIngo Molnar 		if (entries[i].addr + entries[i].size < entries[i].addr)
340b79cd8f1SYinghai Lu 			return -1;
341640e1b38SIngo Molnar 	}
342b79cd8f1SYinghai Lu 
343640e1b38SIngo Molnar 	/* Create pointers for initial change-point information (for sorting): */
344441ac2f3SIngo Molnar 	for (i = 0; i < 2 * table->nr_entries; i++)
345b79cd8f1SYinghai Lu 		change_point[i] = &change_point_list[i];
346b79cd8f1SYinghai Lu 
347640e1b38SIngo Molnar 	/*
348640e1b38SIngo Molnar 	 * Record all known change-points (starting and ending addresses),
349640e1b38SIngo Molnar 	 * omitting empty memory regions:
350640e1b38SIngo Molnar 	 */
351441ac2f3SIngo Molnar 	chg_idx = 0;
352441ac2f3SIngo Molnar 	for (i = 0; i < table->nr_entries; i++)	{
3539a02fd0fSIngo Molnar 		if (entries[i].size != 0) {
354441ac2f3SIngo Molnar 			change_point[chg_idx]->addr	= entries[i].addr;
355441ac2f3SIngo Molnar 			change_point[chg_idx++]->entry	= &entries[i];
356441ac2f3SIngo Molnar 			change_point[chg_idx]->addr	= entries[i].addr + entries[i].size;
357441ac2f3SIngo Molnar 			change_point[chg_idx++]->entry	= &entries[i];
358b79cd8f1SYinghai Lu 		}
359b79cd8f1SYinghai Lu 	}
360441ac2f3SIngo Molnar 	chg_nr = chg_idx;
361b79cd8f1SYinghai Lu 
362640e1b38SIngo Molnar 	/* Sort change-point list by memory addresses (low -> high): */
363d88961b5SIngo Molnar 	sort(change_point, chg_nr, sizeof(*change_point), cpcompare, NULL);
364b79cd8f1SYinghai Lu 
3659a02fd0fSIngo Molnar 	/* Create a new memory map, removing overlaps: */
366640e1b38SIngo Molnar 	overlap_entries = 0;	 /* Number of entries in the overlap table */
3679a02fd0fSIngo Molnar 	new_nr_entries = 0;	 /* Index for creating new map entries */
368640e1b38SIngo Molnar 	last_type = 0;		 /* Start with undefined memory type */
369640e1b38SIngo Molnar 	last_addr = 0;		 /* Start with 0 as last starting address */
370b79cd8f1SYinghai Lu 
3719a02fd0fSIngo Molnar 	/* Loop through change-points, determining effect on the new map: */
372441ac2f3SIngo Molnar 	for (chg_idx = 0; chg_idx < chg_nr; chg_idx++) {
3739a02fd0fSIngo Molnar 		/* Keep track of all overlapping entries */
374441ac2f3SIngo Molnar 		if (change_point[chg_idx]->addr == change_point[chg_idx]->entry->addr) {
375640e1b38SIngo Molnar 			/* Add map entry to overlap list (> 1 entry implies an overlap) */
376441ac2f3SIngo Molnar 			overlap_list[overlap_entries++] = change_point[chg_idx]->entry;
377b79cd8f1SYinghai Lu 		} else {
378640e1b38SIngo Molnar 			/* Remove entry from list (order independent, so swap with last): */
379b79cd8f1SYinghai Lu 			for (i = 0; i < overlap_entries; i++) {
380441ac2f3SIngo Molnar 				if (overlap_list[i] == change_point[chg_idx]->entry)
381640e1b38SIngo Molnar 					overlap_list[i] = overlap_list[overlap_entries-1];
382b79cd8f1SYinghai Lu 			}
383b79cd8f1SYinghai Lu 			overlap_entries--;
384b79cd8f1SYinghai Lu 		}
385b79cd8f1SYinghai Lu 		/*
386640e1b38SIngo Molnar 		 * If there are overlapping entries, decide which
387b79cd8f1SYinghai Lu 		 * "type" to use (larger value takes precedence --
388b79cd8f1SYinghai Lu 		 * 1=usable, 2,3,4,4+=unusable)
389b79cd8f1SYinghai Lu 		 */
390b79cd8f1SYinghai Lu 		current_type = 0;
391640e1b38SIngo Molnar 		for (i = 0; i < overlap_entries; i++) {
392b79cd8f1SYinghai Lu 			if (overlap_list[i]->type > current_type)
393b79cd8f1SYinghai Lu 				current_type = overlap_list[i]->type;
394640e1b38SIngo Molnar 		}
395640e1b38SIngo Molnar 
3969a02fd0fSIngo Molnar 		/* Continue building up new map based on this information: */
39788e9a5b7SDan Williams 		if (current_type != last_type || e820_nomerge(current_type)) {
39850c66d7bSYuntao Wang 			if (last_type) {
399441ac2f3SIngo Molnar 				new_entries[new_nr_entries].size = change_point[chg_idx]->addr - last_addr;
400640e1b38SIngo Molnar 				/* Move forward only if the new size was non-zero: */
4019a02fd0fSIngo Molnar 				if (new_entries[new_nr_entries].size != 0)
4029a02fd0fSIngo Molnar 					/* No more space left for new entries? */
4039a02fd0fSIngo Molnar 					if (++new_nr_entries >= max_nr_entries)
404b79cd8f1SYinghai Lu 						break;
405b79cd8f1SYinghai Lu 			}
40650c66d7bSYuntao Wang 			if (current_type) {
407441ac2f3SIngo Molnar 				new_entries[new_nr_entries].addr = change_point[chg_idx]->addr;
4089a02fd0fSIngo Molnar 				new_entries[new_nr_entries].type = current_type;
409441ac2f3SIngo Molnar 				last_addr = change_point[chg_idx]->addr;
410b79cd8f1SYinghai Lu 			}
411b79cd8f1SYinghai Lu 			last_type = current_type;
412b79cd8f1SYinghai Lu 		}
413b79cd8f1SYinghai Lu 	}
414640e1b38SIngo Molnar 
4159a02fd0fSIngo Molnar 	/* Copy the new entries into the original location: */
416441ac2f3SIngo Molnar 	memcpy(entries, new_entries, new_nr_entries*sizeof(*entries));
417441ac2f3SIngo Molnar 	table->nr_entries = new_nr_entries;
418b79cd8f1SYinghai Lu 
419b79cd8f1SYinghai Lu 	return 0;
420b79cd8f1SYinghai Lu }
421b79cd8f1SYinghai Lu 
__append_e820_table(struct boot_e820_entry * entries,u32 nr_entries)4227410aa1cSIngo Molnar static int __init __append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
4238c5beb50SHuang, Ying {
4247410aa1cSIngo Molnar 	struct boot_e820_entry *entry = entries;
4259a02fd0fSIngo Molnar 
4269a02fd0fSIngo Molnar 	while (nr_entries) {
4279a02fd0fSIngo Molnar 		u64 start = entry->addr;
4289a02fd0fSIngo Molnar 		u64 size = entry->size;
4293ec97965SWei Yang 		u64 end = start + size - 1;
4309a02fd0fSIngo Molnar 		u32 type = entry->type;
4318c5beb50SHuang, Ying 
432640e1b38SIngo Molnar 		/* Ignore the entry on 64-bit overflow: */
4333ec97965SWei Yang 		if (start > end && likely(size))
4348c5beb50SHuang, Ying 			return -1;
4358c5beb50SHuang, Ying 
436ab6bc04cSIngo Molnar 		e820__range_add(start, size, type);
4378c5beb50SHuang, Ying 
4389a02fd0fSIngo Molnar 		entry++;
4399a02fd0fSIngo Molnar 		nr_entries--;
4408c5beb50SHuang, Ying 	}
4418c5beb50SHuang, Ying 	return 0;
4428c5beb50SHuang, Ying }
4438c5beb50SHuang, Ying 
444b79cd8f1SYinghai Lu /*
445640e1b38SIngo Molnar  * Copy the BIOS E820 map into a safe place.
446b79cd8f1SYinghai Lu  *
447b79cd8f1SYinghai Lu  * Sanity-check it while we're at it..
448b79cd8f1SYinghai Lu  *
449b79cd8f1SYinghai Lu  * If we're lucky and live on a modern system, the setup code
450b79cd8f1SYinghai Lu  * will have given us a memory map that we can use to properly
451b79cd8f1SYinghai Lu  * set up memory.  If we aren't, we'll fake a memory map.
452b79cd8f1SYinghai Lu  */
append_e820_table(struct boot_e820_entry * entries,u32 nr_entries)4537410aa1cSIngo Molnar static int __init append_e820_table(struct boot_e820_entry *entries, u32 nr_entries)
454b79cd8f1SYinghai Lu {
455b79cd8f1SYinghai Lu 	/* Only one memory region (or negative)? Ignore it */
4569a02fd0fSIngo Molnar 	if (nr_entries < 2)
457b79cd8f1SYinghai Lu 		return -1;
458b79cd8f1SYinghai Lu 
4599a02fd0fSIngo Molnar 	return __append_e820_table(entries, nr_entries);
460b79cd8f1SYinghai Lu }
461b79cd8f1SYinghai Lu 
462640e1b38SIngo Molnar static u64 __init
__e820__range_update(struct e820_table * table,u64 start,u64 size,enum e820_type old_type,enum e820_type new_type)4636afc03b8SIngo Molnar __e820__range_update(struct e820_table *table, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
464b79cd8f1SYinghai Lu {
46578a8b35bSYinghai Lu 	u64 end;
466773e673dSYinghai Lu 	unsigned int i;
467b79cd8f1SYinghai Lu 	u64 real_updated_size = 0;
468b79cd8f1SYinghai Lu 
469b79cd8f1SYinghai Lu 	BUG_ON(old_type == new_type);
470b79cd8f1SYinghai Lu 
471232b957aSYinghai Lu 	if (size > (ULLONG_MAX - start))
472232b957aSYinghai Lu 		size = ULLONG_MAX - start;
473232b957aSYinghai Lu 
47478a8b35bSYinghai Lu 	end = start + size;
475e22af0beSBorislav Petkov 	printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ", start, end - 1);
476c61cf4cfSYinghai Lu 	e820_print_type(old_type);
47701259ef1SIngo Molnar 	pr_cont(" ==> ");
478c61cf4cfSYinghai Lu 	e820_print_type(new_type);
47901259ef1SIngo Molnar 	pr_cont("\n");
480c61cf4cfSYinghai Lu 
481bf495573SIngo Molnar 	for (i = 0; i < table->nr_entries; i++) {
482e5540f87SIngo Molnar 		struct e820_entry *entry = &table->entries[i];
483b79cd8f1SYinghai Lu 		u64 final_start, final_end;
484e5540f87SIngo Molnar 		u64 entry_end;
48578a8b35bSYinghai Lu 
486e5540f87SIngo Molnar 		if (entry->type != old_type)
487b79cd8f1SYinghai Lu 			continue;
48878a8b35bSYinghai Lu 
489e5540f87SIngo Molnar 		entry_end = entry->addr + entry->size;
490640e1b38SIngo Molnar 
491640e1b38SIngo Molnar 		/* Completely covered by new range? */
492e5540f87SIngo Molnar 		if (entry->addr >= start && entry_end <= end) {
493e5540f87SIngo Molnar 			entry->type = new_type;
494e5540f87SIngo Molnar 			real_updated_size += entry->size;
495b79cd8f1SYinghai Lu 			continue;
496b79cd8f1SYinghai Lu 		}
49778a8b35bSYinghai Lu 
498640e1b38SIngo Molnar 		/* New range is completely covered? */
499e5540f87SIngo Molnar 		if (entry->addr < start && entry_end > end) {
500ab6bc04cSIngo Molnar 			__e820__range_add(table, start, size, new_type);
501ab6bc04cSIngo Molnar 			__e820__range_add(table, end, entry_end - end, entry->type);
502e5540f87SIngo Molnar 			entry->size = start - entry->addr;
50378a8b35bSYinghai Lu 			real_updated_size += size;
50478a8b35bSYinghai Lu 			continue;
50578a8b35bSYinghai Lu 		}
50678a8b35bSYinghai Lu 
507640e1b38SIngo Molnar 		/* Partially covered: */
508e5540f87SIngo Molnar 		final_start = max(start, entry->addr);
509e5540f87SIngo Molnar 		final_end = min(end, entry_end);
510b79cd8f1SYinghai Lu 		if (final_start >= final_end)
511b79cd8f1SYinghai Lu 			continue;
5125c0e6f03SJan Beulich 
513ab6bc04cSIngo Molnar 		__e820__range_add(table, final_start, final_end - final_start, new_type);
5145c0e6f03SJan Beulich 
515b79cd8f1SYinghai Lu 		real_updated_size += final_end - final_start;
516976dd4dcSYinghai Lu 
517773e673dSYinghai Lu 		/*
518640e1b38SIngo Molnar 		 * Left range could be head or tail, so need to update
519640e1b38SIngo Molnar 		 * its size first:
520773e673dSYinghai Lu 		 */
521e5540f87SIngo Molnar 		entry->size -= final_end - final_start;
522e5540f87SIngo Molnar 		if (entry->addr < final_start)
523976dd4dcSYinghai Lu 			continue;
524640e1b38SIngo Molnar 
525e5540f87SIngo Molnar 		entry->addr = final_end;
526b79cd8f1SYinghai Lu 	}
527b79cd8f1SYinghai Lu 	return real_updated_size;
528b79cd8f1SYinghai Lu }
529b79cd8f1SYinghai Lu 
e820__range_update(u64 start,u64 size,enum e820_type old_type,enum e820_type new_type)5306afc03b8SIngo Molnar u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type)
531fc9036eaSYinghai Lu {
532ab6bc04cSIngo Molnar 	return __e820__range_update(e820_table, start, size, old_type, new_type);
533fc9036eaSYinghai Lu }
534fc9036eaSYinghai Lu 
e820__range_update_kexec(u64 start,u64 size,enum e820_type old_type,enum e820_type new_type)535a09bae0fSChen Yu static u64 __init e820__range_update_kexec(u64 start, u64 size, enum e820_type old_type, enum e820_type  new_type)
536fc9036eaSYinghai Lu {
537a09bae0fSChen Yu 	return __e820__range_update(e820_table_kexec, start, size, old_type, new_type);
538fc9036eaSYinghai Lu }
539fc9036eaSYinghai Lu 
540640e1b38SIngo Molnar /* Remove a range of memory from the E820 table: */
e820__range_remove(u64 start,u64 size,enum e820_type old_type,bool check_type)54181b3e090SIngo Molnar u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
5427a1fd986SYinghai Lu {
5437a1fd986SYinghai Lu 	int i;
5441b5576e6SYinghai Lu 	u64 end;
5457a1fd986SYinghai Lu 	u64 real_removed_size = 0;
5467a1fd986SYinghai Lu 
547232b957aSYinghai Lu 	if (size > (ULLONG_MAX - start))
548232b957aSYinghai Lu 		size = ULLONG_MAX - start;
549232b957aSYinghai Lu 
5501b5576e6SYinghai Lu 	end = start + size;
551e22af0beSBorislav Petkov 	printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1);
55281b3e090SIngo Molnar 	if (check_type)
5531b5576e6SYinghai Lu 		e820_print_type(old_type);
55401259ef1SIngo Molnar 	pr_cont("\n");
5551b5576e6SYinghai Lu 
556bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
557e5540f87SIngo Molnar 		struct e820_entry *entry = &e820_table->entries[i];
5587a1fd986SYinghai Lu 		u64 final_start, final_end;
559e5540f87SIngo Molnar 		u64 entry_end;
5607a1fd986SYinghai Lu 
56181b3e090SIngo Molnar 		if (check_type && entry->type != old_type)
5627a1fd986SYinghai Lu 			continue;
5639f3a5f52SYinghai Lu 
564e5540f87SIngo Molnar 		entry_end = entry->addr + entry->size;
565640e1b38SIngo Molnar 
566640e1b38SIngo Molnar 		/* Completely covered? */
567e5540f87SIngo Molnar 		if (entry->addr >= start && entry_end <= end) {
568e5540f87SIngo Molnar 			real_removed_size += entry->size;
569d88961b5SIngo Molnar 			memset(entry, 0, sizeof(*entry));
5707a1fd986SYinghai Lu 			continue;
5717a1fd986SYinghai Lu 		}
5729f3a5f52SYinghai Lu 
573640e1b38SIngo Molnar 		/* Is the new range completely covered? */
574e5540f87SIngo Molnar 		if (entry->addr < start && entry_end > end) {
575ab6bc04cSIngo Molnar 			e820__range_add(end, entry_end - end, entry->type);
576e5540f87SIngo Molnar 			entry->size = start - entry->addr;
5779f3a5f52SYinghai Lu 			real_removed_size += size;
5789f3a5f52SYinghai Lu 			continue;
5799f3a5f52SYinghai Lu 		}
5809f3a5f52SYinghai Lu 
581640e1b38SIngo Molnar 		/* Partially covered: */
582e5540f87SIngo Molnar 		final_start = max(start, entry->addr);
583e5540f87SIngo Molnar 		final_end = min(end, entry_end);
5847a1fd986SYinghai Lu 		if (final_start >= final_end)
5857a1fd986SYinghai Lu 			continue;
586640e1b38SIngo Molnar 
5877a1fd986SYinghai Lu 		real_removed_size += final_end - final_start;
5887a1fd986SYinghai Lu 
5899f3a5f52SYinghai Lu 		/*
590640e1b38SIngo Molnar 		 * Left range could be head or tail, so need to update
591640e1b38SIngo Molnar 		 * the size first:
5929f3a5f52SYinghai Lu 		 */
593e5540f87SIngo Molnar 		entry->size -= final_end - final_start;
594e5540f87SIngo Molnar 		if (entry->addr < final_start)
5957a1fd986SYinghai Lu 			continue;
596640e1b38SIngo Molnar 
597e5540f87SIngo Molnar 		entry->addr = final_end;
5987a1fd986SYinghai Lu 	}
5997a1fd986SYinghai Lu 	return real_removed_size;
6007a1fd986SYinghai Lu }
6017a1fd986SYinghai Lu 
e820__update_table_print(void)6026464d294SIngo Molnar void __init e820__update_table_print(void)
603b79cd8f1SYinghai Lu {
604f9748fa0SIngo Molnar 	if (e820__update_table(e820_table))
605b79cd8f1SYinghai Lu 		return;
606640e1b38SIngo Molnar 
6071de392f5SJoe Perches 	pr_info("modified physical RAM map:\n");
608be0c3f0fSIngo Molnar 	e820__print_table("modified");
609b79cd8f1SYinghai Lu }
610640e1b38SIngo Molnar 
e820__update_table_kexec(void)611a09bae0fSChen Yu static void __init e820__update_table_kexec(void)
612fc9036eaSYinghai Lu {
613a09bae0fSChen Yu 	e820__update_table(e820_table_kexec);
614fc9036eaSYinghai Lu }
615640e1b38SIngo Molnar 
616fd6493e1SAlok Kataria #define MAX_GAP_END 0x100000000ull
617640e1b38SIngo Molnar 
618b79cd8f1SYinghai Lu /*
619640e1b38SIngo Molnar  * Search for a gap in the E820 memory space from 0 to MAX_GAP_END (4GB).
620b79cd8f1SYinghai Lu  */
e820_search_gap(unsigned long * gapstart,unsigned long * gapsize)621640e1b38SIngo Molnar static int __init e820_search_gap(unsigned long *gapstart, unsigned long *gapsize)
622b79cd8f1SYinghai Lu {
623b4ed1d15SWei Yang 	unsigned long long last = MAX_GAP_END;
624bf495573SIngo Molnar 	int i = e820_table->nr_entries;
625b79cd8f1SYinghai Lu 	int found = 0;
626b79cd8f1SYinghai Lu 
627b79cd8f1SYinghai Lu 	while (--i >= 0) {
628bf495573SIngo Molnar 		unsigned long long start = e820_table->entries[i].addr;
629bf495573SIngo Molnar 		unsigned long long end = start + e820_table->entries[i].size;
630b79cd8f1SYinghai Lu 
631b79cd8f1SYinghai Lu 		/*
632b79cd8f1SYinghai Lu 		 * Since "last" is at most 4GB, we know we'll
633640e1b38SIngo Molnar 		 * fit in 32 bits if this condition is true:
634b79cd8f1SYinghai Lu 		 */
635b79cd8f1SYinghai Lu 		if (last > end) {
636b79cd8f1SYinghai Lu 			unsigned long gap = last - end;
637b79cd8f1SYinghai Lu 
6383381959dSAlok Kataria 			if (gap >= *gapsize) {
6393381959dSAlok Kataria 				*gapsize = gap;
6403381959dSAlok Kataria 				*gapstart = end;
641b79cd8f1SYinghai Lu 				found = 1;
642b79cd8f1SYinghai Lu 			}
643b79cd8f1SYinghai Lu 		}
644b79cd8f1SYinghai Lu 		if (start < last)
645b79cd8f1SYinghai Lu 			last = start;
646b79cd8f1SYinghai Lu 	}
6473381959dSAlok Kataria 	return found;
6483381959dSAlok Kataria }
6493381959dSAlok Kataria 
6503381959dSAlok Kataria /*
651640e1b38SIngo Molnar  * Search for the biggest gap in the low 32 bits of the E820
652640e1b38SIngo Molnar  * memory space. We pass this space to the PCI subsystem, so
653640e1b38SIngo Molnar  * that it can assign MMIO resources for hotplug or
654640e1b38SIngo Molnar  * unconfigured devices in.
655640e1b38SIngo Molnar  *
6563381959dSAlok Kataria  * Hopefully the BIOS let enough space left.
6573381959dSAlok Kataria  */
e820__setup_pci_gap(void)6582df908baSIngo Molnar __init void e820__setup_pci_gap(void)
6593381959dSAlok Kataria {
6605d423ccdSYinghai Lu 	unsigned long gapstart, gapsize;
6613381959dSAlok Kataria 	int found;
6623381959dSAlok Kataria 
6633381959dSAlok Kataria 	gapsize = 0x400000;
664b4ed1d15SWei Yang 	found  = e820_search_gap(&gapstart, &gapsize);
665b79cd8f1SYinghai Lu 
666b79cd8f1SYinghai Lu 	if (!found) {
667c19a5f35SArnd Bergmann #ifdef CONFIG_X86_64
668c987d12fSYinghai Lu 		gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
6691de392f5SJoe Perches 		pr_err("Cannot find an available gap in the 32-bit address range\n");
6701de392f5SJoe Perches 		pr_err("PCI devices with unassigned 32-bit BARs may not work!\n");
671c19a5f35SArnd Bergmann #else
672c19a5f35SArnd Bergmann 		gapstart = 0x10000000;
673b79cd8f1SYinghai Lu #endif
674c19a5f35SArnd Bergmann 	}
675b79cd8f1SYinghai Lu 
676b79cd8f1SYinghai Lu 	/*
6771506c8dcSIngo Molnar 	 * e820__reserve_resources_late() protects stolen RAM already:
678b79cd8f1SYinghai Lu 	 */
6795d423ccdSYinghai Lu 	pci_mem_start = gapstart;
680b79cd8f1SYinghai Lu 
6811de392f5SJoe Perches 	pr_info("[mem %#010lx-%#010lx] available for PCI devices\n",
6821de392f5SJoe Perches 		gapstart, gapstart + gapsize - 1);
683b79cd8f1SYinghai Lu }
684b79cd8f1SYinghai Lu 
68547533968SDenys Vlasenko /*
68647533968SDenys Vlasenko  * Called late during init, in free_initmem().
68747533968SDenys Vlasenko  *
688a09bae0fSChen Yu  * Initial e820_table and e820_table_kexec are largish __initdata arrays.
689640e1b38SIngo Molnar  *
690640e1b38SIngo Molnar  * Copy them to a (usually much smaller) dynamically allocated area that is
691640e1b38SIngo Molnar  * sized precisely after the number of e820 entries.
692640e1b38SIngo Molnar  *
693640e1b38SIngo Molnar  * This is done after we've performed all the fixes and tweaks to the tables.
694640e1b38SIngo Molnar  * All functions which modify them are __init functions, which won't exist
695640e1b38SIngo Molnar  * after free_initmem().
69647533968SDenys Vlasenko  */
e820__reallocate_tables(void)6970c6fc11aSIngo Molnar __init void e820__reallocate_tables(void)
69847533968SDenys Vlasenko {
69961a50101SIngo Molnar 	struct e820_table *n;
70047533968SDenys Vlasenko 	int size;
70147533968SDenys Vlasenko 
702bf495573SIngo Molnar 	size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table->nr_entries;
703345dca4cSHuang Zijiang 	n = kmemdup(e820_table, size, GFP_KERNEL);
70447533968SDenys Vlasenko 	BUG_ON(!n);
70561a50101SIngo Molnar 	e820_table = n;
70647533968SDenys Vlasenko 
707a09bae0fSChen Yu 	size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_kexec->nr_entries;
708345dca4cSHuang Zijiang 	n = kmemdup(e820_table_kexec, size, GFP_KERNEL);
70947533968SDenys Vlasenko 	BUG_ON(!n);
710a09bae0fSChen Yu 	e820_table_kexec = n;
71112df216cSChen Yu 
71212df216cSChen Yu 	size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_firmware->nr_entries;
713345dca4cSHuang Zijiang 	n = kmemdup(e820_table_firmware, size, GFP_KERNEL);
71412df216cSChen Yu 	BUG_ON(!n);
71512df216cSChen Yu 	e820_table_firmware = n;
71647533968SDenys Vlasenko }
71747533968SDenys Vlasenko 
718640e1b38SIngo Molnar /*
719640e1b38SIngo Molnar  * Because of the small fixed size of struct boot_params, only the first
720640e1b38SIngo Molnar  * 128 E820 memory entries are passed to the kernel via boot_params.e820_table,
721640e1b38SIngo Molnar  * the remaining (if any) entries are passed via the SETUP_E820_EXT node of
722640e1b38SIngo Molnar  * struct setup_data, which is parsed here.
7238c5beb50SHuang, Ying  */
e820__memory_setup_extended(u64 phys_addr,u32 data_len)724914053c0SIngo Molnar void __init e820__memory_setup_extended(u64 phys_addr, u32 data_len)
7258c5beb50SHuang, Ying {
7268c5beb50SHuang, Ying 	int entries;
7277410aa1cSIngo Molnar 	struct boot_e820_entry *extmap;
72830e46b57SLinn Crosetto 	struct setup_data *sdata;
7298c5beb50SHuang, Ying 
73030e46b57SLinn Crosetto 	sdata = early_memremap(phys_addr, data_len);
731d88961b5SIngo Molnar 	entries = sdata->len / sizeof(*extmap);
7327410aa1cSIngo Molnar 	extmap = (struct boot_e820_entry *)(sdata->data);
733640e1b38SIngo Molnar 
73461a50101SIngo Molnar 	__append_e820_table(extmap, entries);
735f9748fa0SIngo Molnar 	e820__update_table(e820_table);
736640e1b38SIngo Molnar 
737a09bae0fSChen Yu 	memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
73812df216cSChen Yu 	memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
739b7a67e02SChen Yu 
7408d4a40bcSJuergen Gross 	early_memunmap(sdata, data_len);
7411de392f5SJoe Perches 	pr_info("extended physical RAM map:\n");
742be0c3f0fSIngo Molnar 	e820__print_table("extended");
7438c5beb50SHuang, Ying }
7448c5beb50SHuang, Ying 
745090d7171SIngo Molnar /*
746bf62f398SYinghai Lu  * Find the ranges of physical addresses that do not correspond to
747090d7171SIngo Molnar  * E820 RAM areas and register the corresponding pages as 'nosave' for
748640e1b38SIngo Molnar  * hibernation (32-bit) or software suspend and suspend to RAM (64-bit).
749bf62f398SYinghai Lu  *
750640e1b38SIngo Molnar  * This function requires the E820 map to be sorted and without any
75184779575SLee, Chun-Yi  * overlapping entries.
752bf62f398SYinghai Lu  */
e820__register_nosave_regions(unsigned long limit_pfn)753090d7171SIngo Molnar void __init e820__register_nosave_regions(unsigned long limit_pfn)
754bf62f398SYinghai Lu {
755bf62f398SYinghai Lu 	int i;
75684779575SLee, Chun-Yi 	unsigned long pfn = 0;
757bf62f398SYinghai Lu 
758bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
759e5540f87SIngo Molnar 		struct e820_entry *entry = &e820_table->entries[i];
760bf62f398SYinghai Lu 
761e5540f87SIngo Molnar 		if (pfn < PFN_UP(entry->addr))
762e5540f87SIngo Molnar 			register_nosave_region(pfn, PFN_UP(entry->addr));
763bf62f398SYinghai Lu 
764e5540f87SIngo Molnar 		pfn = PFN_DOWN(entry->addr + entry->size);
765ec776ef6SChristoph Hellwig 
76609821ff1SIngo Molnar 		if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
767e5540f87SIngo Molnar 			register_nosave_region(PFN_UP(entry->addr), pfn);
768bf62f398SYinghai Lu 
769bf62f398SYinghai Lu 		if (pfn >= limit_pfn)
770bf62f398SYinghai Lu 			break;
771bf62f398SYinghai Lu 	}
772bf62f398SYinghai Lu }
773a4c81cf6SYinghai Lu 
774b54ac6d2SHuang Ying #ifdef CONFIG_ACPI
775640e1b38SIngo Molnar /*
776640e1b38SIngo Molnar  * Register ACPI NVS memory regions, so that we can save/restore them during
777640e1b38SIngo Molnar  * hibernation and the subsequent resume:
778b69edc76SRafael J. Wysocki  */
e820__register_nvs_regions(void)779090d7171SIngo Molnar static int __init e820__register_nvs_regions(void)
780b69edc76SRafael J. Wysocki {
781b69edc76SRafael J. Wysocki 	int i;
782b69edc76SRafael J. Wysocki 
783bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
784e5540f87SIngo Molnar 		struct e820_entry *entry = &e820_table->entries[i];
785b69edc76SRafael J. Wysocki 
78609821ff1SIngo Molnar 		if (entry->type == E820_TYPE_NVS)
787e5540f87SIngo Molnar 			acpi_nvs_register(entry->addr, entry->size);
788b69edc76SRafael J. Wysocki 	}
789b69edc76SRafael J. Wysocki 
790b69edc76SRafael J. Wysocki 	return 0;
791b69edc76SRafael J. Wysocki }
792090d7171SIngo Molnar core_initcall(e820__register_nvs_regions);
793b69edc76SRafael J. Wysocki #endif
794b69edc76SRafael J. Wysocki 
795a4c81cf6SYinghai Lu /*
796d9f6e12fSIngo Molnar  * Allocate the requested number of bytes with the requested alignment
7975da217caSIngo Molnar  * and return (the physical address) to the caller. Also register this
798a09bae0fSChen Yu  * range in the 'kexec' E820 table as a reserved range.
7995da217caSIngo Molnar  *
8005da217caSIngo Molnar  * This allows kexec to fake a new mptable, as if it came from the real
8015da217caSIngo Molnar  * system.
8022944e16bSYinghai Lu  */
e820__memblock_alloc_reserved(u64 size,u64 align)8035da217caSIngo Molnar u64 __init e820__memblock_alloc_reserved(u64 size, u64 align)
8042944e16bSYinghai Lu {
8052944e16bSYinghai Lu 	u64 addr;
8062944e16bSYinghai Lu 
80742b46aefSMike Rapoport 	addr = memblock_phys_alloc(size, align);
808ab5d140bSTejun Heo 	if (addr) {
809a09bae0fSChen Yu 		e820__range_update_kexec(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
8101de392f5SJoe Perches 		pr_info("update e820_table_kexec for e820__memblock_alloc_reserved()\n");
811a09bae0fSChen Yu 		e820__update_table_kexec();
812ab5d140bSTejun Heo 	}
8132944e16bSYinghai Lu 
8142944e16bSYinghai Lu 	return addr;
8152944e16bSYinghai Lu }
8162944e16bSYinghai Lu 
817ee0c80faSYinghai Lu #ifdef CONFIG_X86_32
818ee0c80faSYinghai Lu # ifdef CONFIG_X86_PAE
819ee0c80faSYinghai Lu #  define MAX_ARCH_PFN		(1ULL<<(36-PAGE_SHIFT))
820ee0c80faSYinghai Lu # else
821ee0c80faSYinghai Lu #  define MAX_ARCH_PFN		(1ULL<<(32-PAGE_SHIFT))
822ee0c80faSYinghai Lu # endif
823ee0c80faSYinghai Lu #else /* CONFIG_X86_32 */
824bd70e522SYinghai Lu # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
825ee0c80faSYinghai Lu #endif
826ee0c80faSYinghai Lu 
827ee0c80faSYinghai Lu /*
828ee0c80faSYinghai Lu  * Find the highest page frame number we have available
829ee0c80faSYinghai Lu  */
e820_end_pfn(unsigned long limit_pfn,enum e820_type type)8306afc03b8SIngo Molnar static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_type type)
831ee0c80faSYinghai Lu {
8322dc807b3SYinghai Lu 	int i;
8332dc807b3SYinghai Lu 	unsigned long last_pfn = 0;
834ee0c80faSYinghai Lu 	unsigned long max_arch_pfn = MAX_ARCH_PFN;
835ee0c80faSYinghai Lu 
836bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
837e5540f87SIngo Molnar 		struct e820_entry *entry = &e820_table->entries[i];
838f361a450SYinghai Lu 		unsigned long start_pfn;
8392dc807b3SYinghai Lu 		unsigned long end_pfn;
8402dc807b3SYinghai Lu 
841e5540f87SIngo Molnar 		if (entry->type != type)
842c22d4c18SYinghai Lu 			continue;
843c22d4c18SYinghai Lu 
844e5540f87SIngo Molnar 		start_pfn = entry->addr >> PAGE_SHIFT;
845e5540f87SIngo Molnar 		end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT;
846f361a450SYinghai Lu 
847f361a450SYinghai Lu 		if (start_pfn >= limit_pfn)
848f361a450SYinghai Lu 			continue;
849f361a450SYinghai Lu 		if (end_pfn > limit_pfn) {
850f361a450SYinghai Lu 			last_pfn = limit_pfn;
851f361a450SYinghai Lu 			break;
852f361a450SYinghai Lu 		}
8532dc807b3SYinghai Lu 		if (end_pfn > last_pfn)
8542dc807b3SYinghai Lu 			last_pfn = end_pfn;
8552dc807b3SYinghai Lu 	}
856ee0c80faSYinghai Lu 
857ee0c80faSYinghai Lu 	if (last_pfn > max_arch_pfn)
858ee0c80faSYinghai Lu 		last_pfn = max_arch_pfn;
859ee0c80faSYinghai Lu 
8601de392f5SJoe Perches 	pr_info("last_pfn = %#lx max_arch_pfn = %#lx\n",
861ee0c80faSYinghai Lu 		last_pfn, max_arch_pfn);
862ee0c80faSYinghai Lu 	return last_pfn;
863ee0c80faSYinghai Lu }
864640e1b38SIngo Molnar 
e820__end_of_ram_pfn(void)8650c6fc11aSIngo Molnar unsigned long __init e820__end_of_ram_pfn(void)
866f361a450SYinghai Lu {
86709821ff1SIngo Molnar 	return e820_end_pfn(MAX_ARCH_PFN, E820_TYPE_RAM);
868f361a450SYinghai Lu }
869ee0c80faSYinghai Lu 
e820__end_of_low_ram_pfn(void)8700c6fc11aSIngo Molnar unsigned long __init e820__end_of_low_ram_pfn(void)
871f361a450SYinghai Lu {
87209821ff1SIngo Molnar 	return e820_end_pfn(1UL << (32 - PAGE_SHIFT), E820_TYPE_RAM);
873f361a450SYinghai Lu }
874ee0c80faSYinghai Lu 
early_panic(char * msg)8758c2103f2SDenys Vlasenko static void __init early_panic(char *msg)
876ab4a465eSYinghai Lu {
877ab4a465eSYinghai Lu 	early_printk(msg);
878ab4a465eSYinghai Lu 	panic(msg);
879ab4a465eSYinghai Lu }
880ab4a465eSYinghai Lu 
88169a7704dSYinghai Lu static int userdef __initdata;
88269a7704dSYinghai Lu 
883640e1b38SIngo Molnar /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */
parse_memopt(char * p)884ab4a465eSYinghai Lu static int __init parse_memopt(char *p)
885ab4a465eSYinghai Lu {
886ab4a465eSYinghai Lu 	u64 mem_size;
887ab4a465eSYinghai Lu 
888ab4a465eSYinghai Lu 	if (!p)
889ab4a465eSYinghai Lu 		return -EINVAL;
890ab4a465eSYinghai Lu 
891ab4a465eSYinghai Lu 	if (!strcmp(p, "nopentium")) {
8929a6d44b9SKamal Mostafa #ifdef CONFIG_X86_32
893ab4a465eSYinghai Lu 		setup_clear_cpu_cap(X86_FEATURE_PSE);
894ab4a465eSYinghai Lu 		return 0;
8959a6d44b9SKamal Mostafa #else
89601259ef1SIngo Molnar 		pr_warn("mem=nopentium ignored! (only supported on x86_32)\n");
8979a6d44b9SKamal Mostafa 		return -EINVAL;
898ab4a465eSYinghai Lu #endif
8999a6d44b9SKamal Mostafa 	}
900ab4a465eSYinghai Lu 
90169a7704dSYinghai Lu 	userdef = 1;
902ab4a465eSYinghai Lu 	mem_size = memparse(p, &p);
903640e1b38SIngo Molnar 
904640e1b38SIngo Molnar 	/* Don't remove all memory when getting "mem={invalid}" parameter: */
90577eed821SKamal Mostafa 	if (mem_size == 0)
90677eed821SKamal Mostafa 		return -EINVAL;
907640e1b38SIngo Molnar 
90809821ff1SIngo Molnar 	e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
909611dfd78SBernhard Walle 
910357b4da5SJuergen Gross #ifdef CONFIG_MEMORY_HOTPLUG
911357b4da5SJuergen Gross 	max_mem_size = mem_size;
912357b4da5SJuergen Gross #endif
913357b4da5SJuergen Gross 
914ab4a465eSYinghai Lu 	return 0;
915ab4a465eSYinghai Lu }
916ab4a465eSYinghai Lu early_param("mem", parse_memopt);
917ab4a465eSYinghai Lu 
parse_memmap_one(char * p)9189710f581SYinghai Lu static int __init parse_memmap_one(char *p)
919ab4a465eSYinghai Lu {
920ab4a465eSYinghai Lu 	char *oldp;
921ab4a465eSYinghai Lu 	u64 start_at, mem_size;
922ab4a465eSYinghai Lu 
923a737abd1SCyrill Gorcunov 	if (!p)
924a737abd1SCyrill Gorcunov 		return -EINVAL;
925a737abd1SCyrill Gorcunov 
926d6be118aSPrarit Bhargava 	if (!strncmp(p, "exactmap", 8)) {
927bf495573SIngo Molnar 		e820_table->nr_entries = 0;
928ab4a465eSYinghai Lu 		userdef = 1;
929ab4a465eSYinghai Lu 		return 0;
930ab4a465eSYinghai Lu 	}
931ab4a465eSYinghai Lu 
932ab4a465eSYinghai Lu 	oldp = p;
933ab4a465eSYinghai Lu 	mem_size = memparse(p, &p);
934ab4a465eSYinghai Lu 	if (p == oldp)
935ab4a465eSYinghai Lu 		return -EINVAL;
936ab4a465eSYinghai Lu 
937ab4a465eSYinghai Lu 	userdef = 1;
938ab4a465eSYinghai Lu 	if (*p == '@') {
939ab4a465eSYinghai Lu 		start_at = memparse(p+1, &p);
94009821ff1SIngo Molnar 		e820__range_add(start_at, mem_size, E820_TYPE_RAM);
941ab4a465eSYinghai Lu 	} else if (*p == '#') {
942ab4a465eSYinghai Lu 		start_at = memparse(p+1, &p);
94309821ff1SIngo Molnar 		e820__range_add(start_at, mem_size, E820_TYPE_ACPI);
944ab4a465eSYinghai Lu 	} else if (*p == '$') {
945ab4a465eSYinghai Lu 		start_at = memparse(p+1, &p);
94609821ff1SIngo Molnar 		e820__range_add(start_at, mem_size, E820_TYPE_RESERVED);
947ec776ef6SChristoph Hellwig 	} else if (*p == '!') {
948ec776ef6SChristoph Hellwig 		start_at = memparse(p+1, &p);
94909821ff1SIngo Molnar 		e820__range_add(start_at, mem_size, E820_TYPE_PRAM);
950ef61f8a3SJan H. Schönherr 	} else if (*p == '%') {
951ef61f8a3SJan H. Schönherr 		enum e820_type from = 0, to = 0;
952ef61f8a3SJan H. Schönherr 
953ef61f8a3SJan H. Schönherr 		start_at = memparse(p + 1, &p);
954ef61f8a3SJan H. Schönherr 		if (*p == '-')
955ef61f8a3SJan H. Schönherr 			from = simple_strtoull(p + 1, &p, 0);
956ef61f8a3SJan H. Schönherr 		if (*p == '+')
957ef61f8a3SJan H. Schönherr 			to = simple_strtoull(p + 1, &p, 0);
958ef61f8a3SJan H. Schönherr 		if (*p != '\0')
959ef61f8a3SJan H. Schönherr 			return -EINVAL;
960ef61f8a3SJan H. Schönherr 		if (from && to)
961ef61f8a3SJan H. Schönherr 			e820__range_update(start_at, mem_size, from, to);
962ef61f8a3SJan H. Schönherr 		else if (to)
963ef61f8a3SJan H. Schönherr 			e820__range_add(start_at, mem_size, to);
964ef61f8a3SJan H. Schönherr 		else if (from)
965ef61f8a3SJan H. Schönherr 			e820__range_remove(start_at, mem_size, from, 1);
966ef61f8a3SJan H. Schönherr 		else
967ef61f8a3SJan H. Schönherr 			e820__range_remove(start_at, mem_size, 0, 0);
968640e1b38SIngo Molnar 	} else {
96909821ff1SIngo Molnar 		e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
970640e1b38SIngo Molnar 	}
9717b479becSYinghai Lu 
972ab4a465eSYinghai Lu 	return *p == '\0' ? 0 : -EINVAL;
973ab4a465eSYinghai Lu }
974640e1b38SIngo Molnar 
parse_memmap_opt(char * str)9759710f581SYinghai Lu static int __init parse_memmap_opt(char *str)
9769710f581SYinghai Lu {
9779710f581SYinghai Lu 	while (str) {
9789710f581SYinghai Lu 		char *k = strchr(str, ',');
9799710f581SYinghai Lu 
9809710f581SYinghai Lu 		if (k)
9819710f581SYinghai Lu 			*k++ = 0;
9829710f581SYinghai Lu 
9839710f581SYinghai Lu 		parse_memmap_one(str);
9849710f581SYinghai Lu 		str = k;
9859710f581SYinghai Lu 	}
9869710f581SYinghai Lu 
9879710f581SYinghai Lu 	return 0;
9889710f581SYinghai Lu }
989ab4a465eSYinghai Lu early_param("memmap", parse_memmap_opt);
990ab4a465eSYinghai Lu 
9911a127034SIngo Molnar /*
9921a127034SIngo Molnar  * Reserve all entries from the bootloader's extensible data nodes list,
9931a127034SIngo Molnar  * because if present we are going to use it later on to fetch e820
9941a127034SIngo Molnar  * entries from it:
9951a127034SIngo Molnar  */
e820__reserve_setup_data(void)9961a127034SIngo Molnar void __init e820__reserve_setup_data(void)
997da92139bSIngo Molnar {
9987228918bSRoss Philipson 	struct setup_indirect *indirect;
999da92139bSIngo Molnar 	struct setup_data *data;
10007228918bSRoss Philipson 	u64 pa_data, pa_next;
10017228918bSRoss Philipson 	u32 len;
1002da92139bSIngo Molnar 
1003da92139bSIngo Molnar 	pa_data = boot_params.hdr.setup_data;
1004da92139bSIngo Molnar 	if (!pa_data)
1005da92139bSIngo Molnar 		return;
1006da92139bSIngo Molnar 
1007da92139bSIngo Molnar 	while (pa_data) {
1008da92139bSIngo Molnar 		data = early_memremap(pa_data, sizeof(*data));
10097228918bSRoss Philipson 		if (!data) {
10107228918bSRoss Philipson 			pr_warn("e820: failed to memremap setup_data entry\n");
10117228918bSRoss Philipson 			return;
10127228918bSRoss Philipson 		}
10137228918bSRoss Philipson 
10147228918bSRoss Philipson 		len = sizeof(*data);
10157228918bSRoss Philipson 		pa_next = data->next;
10167228918bSRoss Philipson 
101709821ff1SIngo Molnar 		e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
10188efbc518SDave Young 
10198efbc518SDave Young 		/*
1020*c43b984fSJiri Bohac 		 * SETUP_EFI, SETUP_IMA and SETUP_RNG_SEED are supplied by
1021*c43b984fSJiri Bohac 		 * kexec and do not need to be reserved.
10228efbc518SDave Young 		 */
1023*c43b984fSJiri Bohac 		if (data->type != SETUP_EFI &&
1024*c43b984fSJiri Bohac 		    data->type != SETUP_IMA &&
1025*c43b984fSJiri Bohac 		    data->type != SETUP_RNG_SEED)
10268efbc518SDave Young 			e820__range_update_kexec(pa_data,
10278efbc518SDave Young 						 sizeof(*data) + data->len,
10288efbc518SDave Young 						 E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
1029b3c72fc9SDaniel Kiper 
10307228918bSRoss Philipson 		if (data->type == SETUP_INDIRECT) {
10317228918bSRoss Philipson 			len += data->len;
10327228918bSRoss Philipson 			early_memunmap(data, sizeof(*data));
10337228918bSRoss Philipson 			data = early_memremap(pa_data, len);
10347228918bSRoss Philipson 			if (!data) {
10357228918bSRoss Philipson 				pr_warn("e820: failed to memremap indirect setup_data\n");
10367228918bSRoss Philipson 				return;
1037b3c72fc9SDaniel Kiper 			}
1038b3c72fc9SDaniel Kiper 
10397228918bSRoss Philipson 			indirect = (struct setup_indirect *)data->data;
10407228918bSRoss Philipson 
10417228918bSRoss Philipson 			if (indirect->type != SETUP_INDIRECT) {
10427228918bSRoss Philipson 				e820__range_update(indirect->addr, indirect->len,
10437228918bSRoss Philipson 						   E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
10447228918bSRoss Philipson 				e820__range_update_kexec(indirect->addr, indirect->len,
10457228918bSRoss Philipson 							 E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
10467228918bSRoss Philipson 			}
10477228918bSRoss Philipson 		}
10487228918bSRoss Philipson 
10497228918bSRoss Philipson 		pa_data = pa_next;
10507228918bSRoss Philipson 		early_memunmap(data, len);
1051da92139bSIngo Molnar 	}
1052da92139bSIngo Molnar 
1053f9748fa0SIngo Molnar 	e820__update_table(e820_table);
1054a09bae0fSChen Yu 	e820__update_table(e820_table_kexec);
10551a127034SIngo Molnar 
10561a127034SIngo Molnar 	pr_info("extended physical RAM map:\n");
1057be0c3f0fSIngo Molnar 	e820__print_table("reserve setup_data");
1058da92139bSIngo Molnar }
1059da92139bSIngo Molnar 
10609641bdafSIngo Molnar /*
10619641bdafSIngo Molnar  * Called after parse_early_param(), after early parameters (such as mem=)
10629641bdafSIngo Molnar  * have been processed, in which case we already have an E820 table filled in
10639641bdafSIngo Molnar  * via the parameter callback function(s), but it's not sorted and printed yet:
10649641bdafSIngo Molnar  */
e820__finish_early_params(void)10659641bdafSIngo Molnar void __init e820__finish_early_params(void)
1066ab4a465eSYinghai Lu {
1067ab4a465eSYinghai Lu 	if (userdef) {
1068f9748fa0SIngo Molnar 		if (e820__update_table(e820_table) < 0)
1069ab4a465eSYinghai Lu 			early_panic("Invalid user supplied memory map");
1070ab4a465eSYinghai Lu 
10711de392f5SJoe Perches 		pr_info("user-defined physical RAM map:\n");
1072be0c3f0fSIngo Molnar 		e820__print_table("user");
1073ab4a465eSYinghai Lu 	}
1074ab4a465eSYinghai Lu }
107541c094fdSYinghai Lu 
e820_type_to_string(struct e820_entry * entry)1076c594761dSIngo Molnar static const char *__init e820_type_to_string(struct e820_entry *entry)
10775dfcf14dSBernhard Walle {
1078c594761dSIngo Molnar 	switch (entry->type) {
107909821ff1SIngo Molnar 	case E820_TYPE_RESERVED_KERN:	/* Fall-through: */
108009821ff1SIngo Molnar 	case E820_TYPE_RAM:		return "System RAM";
108109821ff1SIngo Molnar 	case E820_TYPE_ACPI:		return "ACPI Tables";
108209821ff1SIngo Molnar 	case E820_TYPE_NVS:		return "ACPI Non-volatile Storage";
108309821ff1SIngo Molnar 	case E820_TYPE_UNUSABLE:	return "Unusable memory";
108409821ff1SIngo Molnar 	case E820_TYPE_PRAM:		return "Persistent Memory (legacy)";
108509821ff1SIngo Molnar 	case E820_TYPE_PMEM:		return "Persistent Memory";
1086c5231a57SIngo Molnar 	case E820_TYPE_RESERVED:	return "Reserved";
1087262b45aeSDan Williams 	case E820_TYPE_SOFT_RESERVED:	return "Soft Reserved";
1088c5231a57SIngo Molnar 	default:			return "Unknown E820 type";
10895dfcf14dSBernhard Walle 	}
10905dfcf14dSBernhard Walle }
10915dfcf14dSBernhard Walle 
e820_type_to_iomem_type(struct e820_entry * entry)1092c594761dSIngo Molnar static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry)
1093f33b14a4SToshi Kani {
1094c594761dSIngo Molnar 	switch (entry->type) {
109509821ff1SIngo Molnar 	case E820_TYPE_RESERVED_KERN:	/* Fall-through: */
109609821ff1SIngo Molnar 	case E820_TYPE_RAM:		return IORESOURCE_SYSTEM_RAM;
109709821ff1SIngo Molnar 	case E820_TYPE_ACPI:		/* Fall-through: */
109809821ff1SIngo Molnar 	case E820_TYPE_NVS:		/* Fall-through: */
109909821ff1SIngo Molnar 	case E820_TYPE_UNUSABLE:	/* Fall-through: */
110009821ff1SIngo Molnar 	case E820_TYPE_PRAM:		/* Fall-through: */
110109821ff1SIngo Molnar 	case E820_TYPE_PMEM:		/* Fall-through: */
1102c5231a57SIngo Molnar 	case E820_TYPE_RESERVED:	/* Fall-through: */
1103262b45aeSDan Williams 	case E820_TYPE_SOFT_RESERVED:	/* Fall-through: */
1104640e1b38SIngo Molnar 	default:			return IORESOURCE_MEM;
1105f33b14a4SToshi Kani 	}
1106f33b14a4SToshi Kani }
1107f33b14a4SToshi Kani 
e820_type_to_iores_desc(struct e820_entry * entry)1108c594761dSIngo Molnar static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
1109f33b14a4SToshi Kani {
1110c594761dSIngo Molnar 	switch (entry->type) {
111109821ff1SIngo Molnar 	case E820_TYPE_ACPI:		return IORES_DESC_ACPI_TABLES;
111209821ff1SIngo Molnar 	case E820_TYPE_NVS:		return IORES_DESC_ACPI_NV_STORAGE;
111309821ff1SIngo Molnar 	case E820_TYPE_PMEM:		return IORES_DESC_PERSISTENT_MEMORY;
111409821ff1SIngo Molnar 	case E820_TYPE_PRAM:		return IORES_DESC_PERSISTENT_MEMORY_LEGACY;
1115ae9e13d6SLianbo Jiang 	case E820_TYPE_RESERVED:	return IORES_DESC_RESERVED;
1116262b45aeSDan Williams 	case E820_TYPE_SOFT_RESERVED:	return IORES_DESC_SOFT_RESERVED;
111709821ff1SIngo Molnar 	case E820_TYPE_RESERVED_KERN:	/* Fall-through: */
111809821ff1SIngo Molnar 	case E820_TYPE_RAM:		/* Fall-through: */
111909821ff1SIngo Molnar 	case E820_TYPE_UNUSABLE:	/* Fall-through: */
1120640e1b38SIngo Molnar 	default:			return IORES_DESC_NONE;
1121f33b14a4SToshi Kani 	}
1122f33b14a4SToshi Kani }
1123f33b14a4SToshi Kani 
do_mark_busy(enum e820_type type,struct resource * res)1124c5231a57SIngo Molnar static bool __init do_mark_busy(enum e820_type type, struct resource *res)
1125ad5fb870SDan Williams {
1126ad5fb870SDan Williams 	/* this is the legacy bios/dos rom-shadow + mmio region */
1127ad5fb870SDan Williams 	if (res->start < (1ULL<<20))
1128ad5fb870SDan Williams 		return true;
1129ad5fb870SDan Williams 
1130ad5fb870SDan Williams 	/*
1131262b45aeSDan Williams 	 * Treat persistent memory and other special memory ranges like
1132262b45aeSDan Williams 	 * device memory, i.e. reserve it for exclusive use of a driver
1133ad5fb870SDan Williams 	 */
1134ad5fb870SDan Williams 	switch (type) {
113509821ff1SIngo Molnar 	case E820_TYPE_RESERVED:
1136262b45aeSDan Williams 	case E820_TYPE_SOFT_RESERVED:
113709821ff1SIngo Molnar 	case E820_TYPE_PRAM:
113809821ff1SIngo Molnar 	case E820_TYPE_PMEM:
1139ad5fb870SDan Williams 		return false;
1140c5231a57SIngo Molnar 	case E820_TYPE_RESERVED_KERN:
1141c5231a57SIngo Molnar 	case E820_TYPE_RAM:
1142c5231a57SIngo Molnar 	case E820_TYPE_ACPI:
1143c5231a57SIngo Molnar 	case E820_TYPE_NVS:
1144c5231a57SIngo Molnar 	case E820_TYPE_UNUSABLE:
1145ad5fb870SDan Williams 	default:
1146ad5fb870SDan Williams 		return true;
1147ad5fb870SDan Williams 	}
1148ad5fb870SDan Williams }
1149ad5fb870SDan Williams 
115041c094fdSYinghai Lu /*
1151640e1b38SIngo Molnar  * Mark E820 reserved areas as busy for the resource manager:
115241c094fdSYinghai Lu  */
1153640e1b38SIngo Molnar 
1154a5444d15SIngo Molnar static struct resource __initdata *e820_res;
1155640e1b38SIngo Molnar 
e820__reserve_resources(void)11561506c8dcSIngo Molnar void __init e820__reserve_resources(void)
115741c094fdSYinghai Lu {
115841c094fdSYinghai Lu 	int i;
115958f7c988SYinghai Lu 	struct resource *res;
1160a5444d15SIngo Molnar 	u64 end;
116141c094fdSYinghai Lu 
11627e1c4e27SMike Rapoport 	res = memblock_alloc(sizeof(*res) * e820_table->nr_entries,
11637e1c4e27SMike Rapoport 			     SMP_CACHE_BYTES);
11648a7f97b9SMike Rapoport 	if (!res)
11658a7f97b9SMike Rapoport 		panic("%s: Failed to allocate %zu bytes\n", __func__,
11668a7f97b9SMike Rapoport 		      sizeof(*res) * e820_table->nr_entries);
116758f7c988SYinghai Lu 	e820_res = res;
1168c594761dSIngo Molnar 
1169bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
1170c594761dSIngo Molnar 		struct e820_entry *entry = e820_table->entries + i;
1171c594761dSIngo Molnar 
1172c594761dSIngo Molnar 		end = entry->addr + entry->size - 1;
11738308c54dSJeremy Fitzhardinge 		if (end != (resource_size_t)end) {
117441c094fdSYinghai Lu 			res++;
117541c094fdSYinghai Lu 			continue;
117641c094fdSYinghai Lu 		}
1177c594761dSIngo Molnar 		res->start = entry->addr;
1178b4df32f4SYinghai Lu 		res->end   = end;
1179c594761dSIngo Molnar 		res->name  = e820_type_to_string(entry);
1180c594761dSIngo Molnar 		res->flags = e820_type_to_iomem_type(entry);
1181c594761dSIngo Molnar 		res->desc  = e820_type_to_iores_desc(entry);
1182a5444d15SIngo Molnar 
1183a5444d15SIngo Molnar 		/*
11841506c8dcSIngo Molnar 		 * Don't register the region that could be conflicted with
11851506c8dcSIngo Molnar 		 * PCI device BAR resources and insert them later in
11861506c8dcSIngo Molnar 		 * pcibios_resource_survey():
1187a5444d15SIngo Molnar 		 */
1188c594761dSIngo Molnar 		if (do_mark_busy(entry->type, res)) {
11891f987577SLinus Torvalds 			res->flags |= IORESOURCE_BUSY;
119041c094fdSYinghai Lu 			insert_resource(&iomem_resource, res);
11911f987577SLinus Torvalds 		}
119241c094fdSYinghai Lu 		res++;
119341c094fdSYinghai Lu 	}
11945dfcf14dSBernhard Walle 
119512df216cSChen Yu 	/* Expose the bootloader-provided memory layout to the sysfs. */
119612df216cSChen Yu 	for (i = 0; i < e820_table_firmware->nr_entries; i++) {
119712df216cSChen Yu 		struct e820_entry *entry = e820_table_firmware->entries + i;
1198640e1b38SIngo Molnar 
1199c594761dSIngo Molnar 		firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry));
12005dfcf14dSBernhard Walle 	}
120141c094fdSYinghai Lu }
120241c094fdSYinghai Lu 
12031506c8dcSIngo Molnar /*
12041506c8dcSIngo Molnar  * How much should we pad the end of RAM, depending on where it is?
12051506c8dcSIngo Molnar  */
ram_alignment(resource_size_t pos)12068c2103f2SDenys Vlasenko static unsigned long __init ram_alignment(resource_size_t pos)
120745fbe3eeSLinus Torvalds {
120845fbe3eeSLinus Torvalds 	unsigned long mb = pos >> 20;
120945fbe3eeSLinus Torvalds 
121045fbe3eeSLinus Torvalds 	/* To 64kB in the first megabyte */
121145fbe3eeSLinus Torvalds 	if (!mb)
121245fbe3eeSLinus Torvalds 		return 64*1024;
121345fbe3eeSLinus Torvalds 
121445fbe3eeSLinus Torvalds 	/* To 1MB in the first 16MB */
121545fbe3eeSLinus Torvalds 	if (mb < 16)
121645fbe3eeSLinus Torvalds 		return 1024*1024;
121745fbe3eeSLinus Torvalds 
121815b812f1SYinghai Lu 	/* To 64MB for anything above that */
121915b812f1SYinghai Lu 	return 64*1024*1024;
122045fbe3eeSLinus Torvalds }
122145fbe3eeSLinus Torvalds 
12227c5371c4SYinghai Lu #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
12237c5371c4SYinghai Lu 
e820__reserve_resources_late(void)12241506c8dcSIngo Molnar void __init e820__reserve_resources_late(void)
122558f7c988SYinghai Lu {
122658f7c988SYinghai Lu 	int i;
122758f7c988SYinghai Lu 	struct resource *res;
122858f7c988SYinghai Lu 
122958f7c988SYinghai Lu 	res = e820_res;
1230bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
1231a5444d15SIngo Molnar 		if (!res->parent && res->end)
12321f987577SLinus Torvalds 			insert_resource_expand_to_fit(&iomem_resource, res);
123358f7c988SYinghai Lu 		res++;
123458f7c988SYinghai Lu 	}
123545fbe3eeSLinus Torvalds 
123645fbe3eeSLinus Torvalds 	/*
1237640e1b38SIngo Molnar 	 * Try to bump up RAM regions to reasonable boundaries, to
123845fbe3eeSLinus Torvalds 	 * avoid stolen RAM:
123945fbe3eeSLinus Torvalds 	 */
1240bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
1241bf495573SIngo Molnar 		struct e820_entry *entry = &e820_table->entries[i];
12427c5371c4SYinghai Lu 		u64 start, end;
124345fbe3eeSLinus Torvalds 
124409821ff1SIngo Molnar 		if (entry->type != E820_TYPE_RAM)
124545fbe3eeSLinus Torvalds 			continue;
1246640e1b38SIngo Molnar 
124745fbe3eeSLinus Torvalds 		start = entry->addr + entry->size;
12487c5371c4SYinghai Lu 		end = round_up(start, ram_alignment(start)) - 1;
12497c5371c4SYinghai Lu 		if (end > MAX_RESOURCE_SIZE)
12507c5371c4SYinghai Lu 			end = MAX_RESOURCE_SIZE;
12517c5371c4SYinghai Lu 		if (start >= end)
125245fbe3eeSLinus Torvalds 			continue;
1253640e1b38SIngo Molnar 
1254e22af0beSBorislav Petkov 		printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end);
1255640e1b38SIngo Molnar 		reserve_region_with_split(&iomem_resource, start, end, "RAM buffer");
125645fbe3eeSLinus Torvalds 	}
125758f7c988SYinghai Lu }
125858f7c988SYinghai Lu 
1259640e1b38SIngo Molnar /*
1260640e1b38SIngo Molnar  * Pass the firmware (bootloader) E820 map to the kernel and process it:
1261640e1b38SIngo Molnar  */
e820__memory_setup_default(void)1262103e2063SIngo Molnar char *__init e820__memory_setup_default(void)
1263064d25f1SYinghai Lu {
1264064d25f1SYinghai Lu 	char *who = "BIOS-e820";
1265640e1b38SIngo Molnar 
1266064d25f1SYinghai Lu 	/*
1267064d25f1SYinghai Lu 	 * Try to copy the BIOS-supplied E820-map.
1268064d25f1SYinghai Lu 	 *
1269064d25f1SYinghai Lu 	 * Otherwise fake a memory map; one section from 0k->640k,
1270064d25f1SYinghai Lu 	 * the next section from 1mb->appropriate_mem_k
1271064d25f1SYinghai Lu 	 */
1272640e1b38SIngo Molnar 	if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) {
127395a71a45SYinghai Lu 		u64 mem_size;
127441c094fdSYinghai Lu 
1275640e1b38SIngo Molnar 		/* Compare results from other methods and take the one that gives more RAM: */
1276640e1b38SIngo Molnar 		if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) {
1277064d25f1SYinghai Lu 			mem_size = boot_params.screen_info.ext_mem_k;
1278064d25f1SYinghai Lu 			who = "BIOS-88";
1279064d25f1SYinghai Lu 		} else {
1280064d25f1SYinghai Lu 			mem_size = boot_params.alt_mem_k;
1281064d25f1SYinghai Lu 			who = "BIOS-e801";
1282064d25f1SYinghai Lu 		}
1283064d25f1SYinghai Lu 
1284bf495573SIngo Molnar 		e820_table->nr_entries = 0;
128509821ff1SIngo Molnar 		e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM);
128609821ff1SIngo Molnar 		e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM);
1287064d25f1SYinghai Lu 	}
1288064d25f1SYinghai Lu 
12897410aa1cSIngo Molnar 	/* We just appended a lot of ranges, sanitize the table: */
12907410aa1cSIngo Molnar 	e820__update_table(e820_table);
12917410aa1cSIngo Molnar 
1292064d25f1SYinghai Lu 	return who;
1293064d25f1SYinghai Lu }
1294064d25f1SYinghai Lu 
1295103e2063SIngo Molnar /*
1296103e2063SIngo Molnar  * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader
1297103e2063SIngo Molnar  * E820 map - with an optional platform quirk available for virtual platforms
1298103e2063SIngo Molnar  * to override this method of boot environment processing:
1299103e2063SIngo Molnar  */
e820__memory_setup(void)1300103e2063SIngo Molnar void __init e820__memory_setup(void)
1301064d25f1SYinghai Lu {
13020be15526SYinghai Lu 	char *who;
13030be15526SYinghai Lu 
130409c51513SIngo Molnar 	/* This is a firmware interface ABI - make sure we don't break it: */
13057410aa1cSIngo Molnar 	BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20);
130609c51513SIngo Molnar 
13076b18ae3eSThomas Gleixner 	who = x86_init.resources.memory_setup();
1308640e1b38SIngo Molnar 
1309a09bae0fSChen Yu 	memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
131012df216cSChen Yu 	memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
1311640e1b38SIngo Molnar 
13121de392f5SJoe Perches 	pr_info("BIOS-provided physical RAM map:\n");
1313be0c3f0fSIngo Molnar 	e820__print_table(who);
1314064d25f1SYinghai Lu }
131572d7c3b3SYinghai Lu 
e820__memblock_setup(void)13164918e228SIngo Molnar void __init e820__memblock_setup(void)
131772d7c3b3SYinghai Lu {
131872d7c3b3SYinghai Lu 	int i;
131972d7c3b3SYinghai Lu 	u64 end;
132072d7c3b3SYinghai Lu 
132172d7c3b3SYinghai Lu 	/*
13224918e228SIngo Molnar 	 * The bootstrap memblock region count maximum is 128 entries
13234918e228SIngo Molnar 	 * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries
13244918e228SIngo Molnar 	 * than that - so allow memblock resizing.
13254918e228SIngo Molnar 	 *
13264918e228SIngo Molnar 	 * This is safe, because this call happens pretty late during x86 setup,
13274918e228SIngo Molnar 	 * so we know about reserved memory regions already. (This is important
13284918e228SIngo Molnar 	 * so that memblock resizing does no stomp over reserved areas.)
132972d7c3b3SYinghai Lu 	 */
13301aadc056STejun Heo 	memblock_allow_resize();
133172d7c3b3SYinghai Lu 
1332bf495573SIngo Molnar 	for (i = 0; i < e820_table->nr_entries; i++) {
1333e5540f87SIngo Molnar 		struct e820_entry *entry = &e820_table->entries[i];
133472d7c3b3SYinghai Lu 
1335e5540f87SIngo Molnar 		end = entry->addr + entry->size;
133672d7c3b3SYinghai Lu 		if (end != (resource_size_t)end)
133772d7c3b3SYinghai Lu 			continue;
133872d7c3b3SYinghai Lu 
1339262b45aeSDan Williams 		if (entry->type == E820_TYPE_SOFT_RESERVED)
1340262b45aeSDan Williams 			memblock_reserve(entry->addr, entry->size);
1341262b45aeSDan Williams 
134209821ff1SIngo Molnar 		if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
13439fd61bc9SMasayoshi Mizuma 			continue;
13449fd61bc9SMasayoshi Mizuma 
1345e5540f87SIngo Molnar 		memblock_add(entry->addr, entry->size);
134672d7c3b3SYinghai Lu 	}
134772d7c3b3SYinghai Lu 
13484918e228SIngo Molnar 	/* Throw away partial pages: */
13496ede1fd3SYinghai Lu 	memblock_trim_memory(PAGE_SIZE);
13506ede1fd3SYinghai Lu 
135172d7c3b3SYinghai Lu 	memblock_dump_all();
135272d7c3b3SYinghai Lu }
1353