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_table(struct e820_table * t,u64 start,u64 size,enum e820_type old_type,enum e820_type new_type)535d6d85ac1SAshish Kalra u64 __init e820__range_update_table(struct e820_table *t, u64 start, u64 size,
536d6d85ac1SAshish Kalra enum e820_type old_type, enum e820_type new_type)
537fc9036eaSYinghai Lu {
538d6d85ac1SAshish Kalra return __e820__range_update(t, start, size, old_type, new_type);
539fc9036eaSYinghai Lu }
540fc9036eaSYinghai Lu
541640e1b38SIngo 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)54281b3e090SIngo Molnar u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
5437a1fd986SYinghai Lu {
5447a1fd986SYinghai Lu int i;
5451b5576e6SYinghai Lu u64 end;
5467a1fd986SYinghai Lu u64 real_removed_size = 0;
5477a1fd986SYinghai Lu
548232b957aSYinghai Lu if (size > (ULLONG_MAX - start))
549232b957aSYinghai Lu size = ULLONG_MAX - start;
550232b957aSYinghai Lu
5511b5576e6SYinghai Lu end = start + size;
552e22af0beSBorislav Petkov printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1);
55381b3e090SIngo Molnar if (check_type)
5541b5576e6SYinghai Lu e820_print_type(old_type);
55501259ef1SIngo Molnar pr_cont("\n");
5561b5576e6SYinghai Lu
557bf495573SIngo Molnar for (i = 0; i < e820_table->nr_entries; i++) {
558e5540f87SIngo Molnar struct e820_entry *entry = &e820_table->entries[i];
5597a1fd986SYinghai Lu u64 final_start, final_end;
560e5540f87SIngo Molnar u64 entry_end;
5617a1fd986SYinghai Lu
56281b3e090SIngo Molnar if (check_type && entry->type != old_type)
5637a1fd986SYinghai Lu continue;
5649f3a5f52SYinghai Lu
565e5540f87SIngo Molnar entry_end = entry->addr + entry->size;
566640e1b38SIngo Molnar
567640e1b38SIngo Molnar /* Completely covered? */
568e5540f87SIngo Molnar if (entry->addr >= start && entry_end <= end) {
569e5540f87SIngo Molnar real_removed_size += entry->size;
570d88961b5SIngo Molnar memset(entry, 0, sizeof(*entry));
5717a1fd986SYinghai Lu continue;
5727a1fd986SYinghai Lu }
5739f3a5f52SYinghai Lu
574640e1b38SIngo Molnar /* Is the new range completely covered? */
575e5540f87SIngo Molnar if (entry->addr < start && entry_end > end) {
576ab6bc04cSIngo Molnar e820__range_add(end, entry_end - end, entry->type);
577e5540f87SIngo Molnar entry->size = start - entry->addr;
5789f3a5f52SYinghai Lu real_removed_size += size;
5799f3a5f52SYinghai Lu continue;
5809f3a5f52SYinghai Lu }
5819f3a5f52SYinghai Lu
582640e1b38SIngo Molnar /* Partially covered: */
583e5540f87SIngo Molnar final_start = max(start, entry->addr);
584e5540f87SIngo Molnar final_end = min(end, entry_end);
5857a1fd986SYinghai Lu if (final_start >= final_end)
5867a1fd986SYinghai Lu continue;
587640e1b38SIngo Molnar
5887a1fd986SYinghai Lu real_removed_size += final_end - final_start;
5897a1fd986SYinghai Lu
5909f3a5f52SYinghai Lu /*
591640e1b38SIngo Molnar * Left range could be head or tail, so need to update
592640e1b38SIngo Molnar * the size first:
5939f3a5f52SYinghai Lu */
594e5540f87SIngo Molnar entry->size -= final_end - final_start;
595e5540f87SIngo Molnar if (entry->addr < final_start)
5967a1fd986SYinghai Lu continue;
597640e1b38SIngo Molnar
598e5540f87SIngo Molnar entry->addr = final_end;
5997a1fd986SYinghai Lu }
6007a1fd986SYinghai Lu return real_removed_size;
6017a1fd986SYinghai Lu }
6027a1fd986SYinghai Lu
e820__update_table_print(void)6036464d294SIngo Molnar void __init e820__update_table_print(void)
604b79cd8f1SYinghai Lu {
605f9748fa0SIngo Molnar if (e820__update_table(e820_table))
606b79cd8f1SYinghai Lu return;
607640e1b38SIngo Molnar
6081de392f5SJoe Perches pr_info("modified physical RAM map:\n");
609be0c3f0fSIngo Molnar e820__print_table("modified");
610b79cd8f1SYinghai Lu }
611640e1b38SIngo Molnar
e820__update_table_kexec(void)612a09bae0fSChen Yu static void __init e820__update_table_kexec(void)
613fc9036eaSYinghai Lu {
614a09bae0fSChen Yu e820__update_table(e820_table_kexec);
615fc9036eaSYinghai Lu }
616640e1b38SIngo Molnar
617fd6493e1SAlok Kataria #define MAX_GAP_END 0x100000000ull
618640e1b38SIngo Molnar
619b79cd8f1SYinghai Lu /*
620640e1b38SIngo Molnar * Search for a gap in the E820 memory space from 0 to MAX_GAP_END (4GB).
621b79cd8f1SYinghai Lu */
e820_search_gap(unsigned long * gapstart,unsigned long * gapsize)622640e1b38SIngo Molnar static int __init e820_search_gap(unsigned long *gapstart, unsigned long *gapsize)
623b79cd8f1SYinghai Lu {
624b4ed1d15SWei Yang unsigned long long last = MAX_GAP_END;
625bf495573SIngo Molnar int i = e820_table->nr_entries;
626b79cd8f1SYinghai Lu int found = 0;
627b79cd8f1SYinghai Lu
628b79cd8f1SYinghai Lu while (--i >= 0) {
629bf495573SIngo Molnar unsigned long long start = e820_table->entries[i].addr;
630bf495573SIngo Molnar unsigned long long end = start + e820_table->entries[i].size;
631b79cd8f1SYinghai Lu
632b79cd8f1SYinghai Lu /*
633b79cd8f1SYinghai Lu * Since "last" is at most 4GB, we know we'll
634640e1b38SIngo Molnar * fit in 32 bits if this condition is true:
635b79cd8f1SYinghai Lu */
636b79cd8f1SYinghai Lu if (last > end) {
637b79cd8f1SYinghai Lu unsigned long gap = last - end;
638b79cd8f1SYinghai Lu
6393381959dSAlok Kataria if (gap >= *gapsize) {
6403381959dSAlok Kataria *gapsize = gap;
6413381959dSAlok Kataria *gapstart = end;
642b79cd8f1SYinghai Lu found = 1;
643b79cd8f1SYinghai Lu }
644b79cd8f1SYinghai Lu }
645b79cd8f1SYinghai Lu if (start < last)
646b79cd8f1SYinghai Lu last = start;
647b79cd8f1SYinghai Lu }
6483381959dSAlok Kataria return found;
6493381959dSAlok Kataria }
6503381959dSAlok Kataria
6513381959dSAlok Kataria /*
652640e1b38SIngo Molnar * Search for the biggest gap in the low 32 bits of the E820
653640e1b38SIngo Molnar * memory space. We pass this space to the PCI subsystem, so
654640e1b38SIngo Molnar * that it can assign MMIO resources for hotplug or
655640e1b38SIngo Molnar * unconfigured devices in.
656640e1b38SIngo Molnar *
6573381959dSAlok Kataria * Hopefully the BIOS let enough space left.
6583381959dSAlok Kataria */
e820__setup_pci_gap(void)6592df908baSIngo Molnar __init void e820__setup_pci_gap(void)
6603381959dSAlok Kataria {
6615d423ccdSYinghai Lu unsigned long gapstart, gapsize;
6623381959dSAlok Kataria int found;
6633381959dSAlok Kataria
6643381959dSAlok Kataria gapsize = 0x400000;
665b4ed1d15SWei Yang found = e820_search_gap(&gapstart, &gapsize);
666b79cd8f1SYinghai Lu
667b79cd8f1SYinghai Lu if (!found) {
668c19a5f35SArnd Bergmann #ifdef CONFIG_X86_64
669c987d12fSYinghai Lu gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
6701de392f5SJoe Perches pr_err("Cannot find an available gap in the 32-bit address range\n");
6711de392f5SJoe Perches pr_err("PCI devices with unassigned 32-bit BARs may not work!\n");
672c19a5f35SArnd Bergmann #else
673c19a5f35SArnd Bergmann gapstart = 0x10000000;
674b79cd8f1SYinghai Lu #endif
675c19a5f35SArnd Bergmann }
676b79cd8f1SYinghai Lu
677b79cd8f1SYinghai Lu /*
6781506c8dcSIngo Molnar * e820__reserve_resources_late() protects stolen RAM already:
679b79cd8f1SYinghai Lu */
6805d423ccdSYinghai Lu pci_mem_start = gapstart;
681b79cd8f1SYinghai Lu
6821de392f5SJoe Perches pr_info("[mem %#010lx-%#010lx] available for PCI devices\n",
6831de392f5SJoe Perches gapstart, gapstart + gapsize - 1);
684b79cd8f1SYinghai Lu }
685b79cd8f1SYinghai Lu
68647533968SDenys Vlasenko /*
68747533968SDenys Vlasenko * Called late during init, in free_initmem().
68847533968SDenys Vlasenko *
689a09bae0fSChen Yu * Initial e820_table and e820_table_kexec are largish __initdata arrays.
690640e1b38SIngo Molnar *
691640e1b38SIngo Molnar * Copy them to a (usually much smaller) dynamically allocated area that is
692640e1b38SIngo Molnar * sized precisely after the number of e820 entries.
693640e1b38SIngo Molnar *
694640e1b38SIngo Molnar * This is done after we've performed all the fixes and tweaks to the tables.
695640e1b38SIngo Molnar * All functions which modify them are __init functions, which won't exist
696640e1b38SIngo Molnar * after free_initmem().
69747533968SDenys Vlasenko */
e820__reallocate_tables(void)6980c6fc11aSIngo Molnar __init void e820__reallocate_tables(void)
69947533968SDenys Vlasenko {
70061a50101SIngo Molnar struct e820_table *n;
70147533968SDenys Vlasenko int size;
70247533968SDenys Vlasenko
703bf495573SIngo Molnar size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table->nr_entries;
704345dca4cSHuang Zijiang n = kmemdup(e820_table, size, GFP_KERNEL);
70547533968SDenys Vlasenko BUG_ON(!n);
70661a50101SIngo Molnar e820_table = n;
70747533968SDenys Vlasenko
708a09bae0fSChen Yu size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_kexec->nr_entries;
709345dca4cSHuang Zijiang n = kmemdup(e820_table_kexec, size, GFP_KERNEL);
71047533968SDenys Vlasenko BUG_ON(!n);
711a09bae0fSChen Yu e820_table_kexec = n;
71212df216cSChen Yu
71312df216cSChen Yu size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_firmware->nr_entries;
714345dca4cSHuang Zijiang n = kmemdup(e820_table_firmware, size, GFP_KERNEL);
71512df216cSChen Yu BUG_ON(!n);
71612df216cSChen Yu e820_table_firmware = n;
71747533968SDenys Vlasenko }
71847533968SDenys Vlasenko
719640e1b38SIngo Molnar /*
720640e1b38SIngo Molnar * Because of the small fixed size of struct boot_params, only the first
721640e1b38SIngo Molnar * 128 E820 memory entries are passed to the kernel via boot_params.e820_table,
722640e1b38SIngo Molnar * the remaining (if any) entries are passed via the SETUP_E820_EXT node of
723640e1b38SIngo Molnar * struct setup_data, which is parsed here.
7248c5beb50SHuang, Ying */
e820__memory_setup_extended(u64 phys_addr,u32 data_len)725914053c0SIngo Molnar void __init e820__memory_setup_extended(u64 phys_addr, u32 data_len)
7268c5beb50SHuang, Ying {
7278c5beb50SHuang, Ying int entries;
7287410aa1cSIngo Molnar struct boot_e820_entry *extmap;
72930e46b57SLinn Crosetto struct setup_data *sdata;
7308c5beb50SHuang, Ying
73130e46b57SLinn Crosetto sdata = early_memremap(phys_addr, data_len);
732d88961b5SIngo Molnar entries = sdata->len / sizeof(*extmap);
7337410aa1cSIngo Molnar extmap = (struct boot_e820_entry *)(sdata->data);
734640e1b38SIngo Molnar
73561a50101SIngo Molnar __append_e820_table(extmap, entries);
736f9748fa0SIngo Molnar e820__update_table(e820_table);
737640e1b38SIngo Molnar
738a09bae0fSChen Yu memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
73912df216cSChen Yu memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
740b7a67e02SChen Yu
7418d4a40bcSJuergen Gross early_memunmap(sdata, data_len);
7421de392f5SJoe Perches pr_info("extended physical RAM map:\n");
743be0c3f0fSIngo Molnar e820__print_table("extended");
7448c5beb50SHuang, Ying }
7458c5beb50SHuang, Ying
746090d7171SIngo Molnar /*
747bf62f398SYinghai Lu * Find the ranges of physical addresses that do not correspond to
748090d7171SIngo Molnar * E820 RAM areas and register the corresponding pages as 'nosave' for
749640e1b38SIngo Molnar * hibernation (32-bit) or software suspend and suspend to RAM (64-bit).
750bf62f398SYinghai Lu *
751640e1b38SIngo Molnar * This function requires the E820 map to be sorted and without any
75284779575SLee, Chun-Yi * overlapping entries.
753bf62f398SYinghai Lu */
e820__register_nosave_regions(unsigned long limit_pfn)754090d7171SIngo Molnar void __init e820__register_nosave_regions(unsigned long limit_pfn)
755bf62f398SYinghai Lu {
756bf62f398SYinghai Lu int i;
75784779575SLee, Chun-Yi unsigned long pfn = 0;
758bf62f398SYinghai Lu
759bf495573SIngo Molnar for (i = 0; i < e820_table->nr_entries; i++) {
760e5540f87SIngo Molnar struct e820_entry *entry = &e820_table->entries[i];
761bf62f398SYinghai Lu
762e5540f87SIngo Molnar if (pfn < PFN_UP(entry->addr))
763e5540f87SIngo Molnar register_nosave_region(pfn, PFN_UP(entry->addr));
764bf62f398SYinghai Lu
765e5540f87SIngo Molnar pfn = PFN_DOWN(entry->addr + entry->size);
766ec776ef6SChristoph Hellwig
76709821ff1SIngo Molnar if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
768e5540f87SIngo Molnar register_nosave_region(PFN_UP(entry->addr), pfn);
769bf62f398SYinghai Lu
770bf62f398SYinghai Lu if (pfn >= limit_pfn)
771bf62f398SYinghai Lu break;
772bf62f398SYinghai Lu }
773bf62f398SYinghai Lu }
774a4c81cf6SYinghai Lu
775b54ac6d2SHuang Ying #ifdef CONFIG_ACPI
776640e1b38SIngo Molnar /*
777640e1b38SIngo Molnar * Register ACPI NVS memory regions, so that we can save/restore them during
778640e1b38SIngo Molnar * hibernation and the subsequent resume:
779b69edc76SRafael J. Wysocki */
e820__register_nvs_regions(void)780090d7171SIngo Molnar static int __init e820__register_nvs_regions(void)
781b69edc76SRafael J. Wysocki {
782b69edc76SRafael J. Wysocki int i;
783b69edc76SRafael J. Wysocki
784bf495573SIngo Molnar for (i = 0; i < e820_table->nr_entries; i++) {
785e5540f87SIngo Molnar struct e820_entry *entry = &e820_table->entries[i];
786b69edc76SRafael J. Wysocki
78709821ff1SIngo Molnar if (entry->type == E820_TYPE_NVS)
788e5540f87SIngo Molnar acpi_nvs_register(entry->addr, entry->size);
789b69edc76SRafael J. Wysocki }
790b69edc76SRafael J. Wysocki
791b69edc76SRafael J. Wysocki return 0;
792b69edc76SRafael J. Wysocki }
793090d7171SIngo Molnar core_initcall(e820__register_nvs_regions);
794b69edc76SRafael J. Wysocki #endif
795b69edc76SRafael J. Wysocki
796a4c81cf6SYinghai Lu /*
797d9f6e12fSIngo Molnar * Allocate the requested number of bytes with the requested alignment
7985da217caSIngo Molnar * and return (the physical address) to the caller. Also register this
799a09bae0fSChen Yu * range in the 'kexec' E820 table as a reserved range.
8005da217caSIngo Molnar *
8015da217caSIngo Molnar * This allows kexec to fake a new mptable, as if it came from the real
8025da217caSIngo Molnar * system.
8032944e16bSYinghai Lu */
e820__memblock_alloc_reserved(u64 size,u64 align)8045da217caSIngo Molnar u64 __init e820__memblock_alloc_reserved(u64 size, u64 align)
8052944e16bSYinghai Lu {
8062944e16bSYinghai Lu u64 addr;
8072944e16bSYinghai Lu
80842b46aefSMike Rapoport addr = memblock_phys_alloc(size, align);
809ab5d140bSTejun Heo if (addr) {
810d6d85ac1SAshish Kalra e820__range_update_table(e820_table_kexec, addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED);
8111de392f5SJoe Perches pr_info("update e820_table_kexec for e820__memblock_alloc_reserved()\n");
812a09bae0fSChen Yu e820__update_table_kexec();
813ab5d140bSTejun Heo }
8142944e16bSYinghai Lu
8152944e16bSYinghai Lu return addr;
8162944e16bSYinghai Lu }
8172944e16bSYinghai Lu
818ee0c80faSYinghai Lu #ifdef CONFIG_X86_32
819ee0c80faSYinghai Lu # ifdef CONFIG_X86_PAE
820ee0c80faSYinghai Lu # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
821ee0c80faSYinghai Lu # else
822ee0c80faSYinghai Lu # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
823ee0c80faSYinghai Lu # endif
824ee0c80faSYinghai Lu #else /* CONFIG_X86_32 */
825bd70e522SYinghai Lu # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
826ee0c80faSYinghai Lu #endif
827ee0c80faSYinghai Lu
828ee0c80faSYinghai Lu /*
829ee0c80faSYinghai Lu * Find the highest page frame number we have available
830ee0c80faSYinghai Lu */
e820__end_ram_pfn(unsigned long limit_pfn)831*06fa48d8SKirill A. Shutemov static unsigned long __init e820__end_ram_pfn(unsigned long limit_pfn)
832ee0c80faSYinghai Lu {
8332dc807b3SYinghai Lu int i;
8342dc807b3SYinghai Lu unsigned long last_pfn = 0;
835ee0c80faSYinghai Lu unsigned long max_arch_pfn = MAX_ARCH_PFN;
836ee0c80faSYinghai Lu
837bf495573SIngo Molnar for (i = 0; i < e820_table->nr_entries; i++) {
838e5540f87SIngo Molnar struct e820_entry *entry = &e820_table->entries[i];
839f361a450SYinghai Lu unsigned long start_pfn;
8402dc807b3SYinghai Lu unsigned long end_pfn;
8412dc807b3SYinghai Lu
842*06fa48d8SKirill A. Shutemov if (entry->type != E820_TYPE_RAM &&
843*06fa48d8SKirill A. Shutemov entry->type != E820_TYPE_ACPI)
844c22d4c18SYinghai Lu continue;
845c22d4c18SYinghai Lu
846e5540f87SIngo Molnar start_pfn = entry->addr >> PAGE_SHIFT;
847e5540f87SIngo Molnar end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT;
848f361a450SYinghai Lu
849f361a450SYinghai Lu if (start_pfn >= limit_pfn)
850f361a450SYinghai Lu continue;
851f361a450SYinghai Lu if (end_pfn > limit_pfn) {
852f361a450SYinghai Lu last_pfn = limit_pfn;
853f361a450SYinghai Lu break;
854f361a450SYinghai Lu }
8552dc807b3SYinghai Lu if (end_pfn > last_pfn)
8562dc807b3SYinghai Lu last_pfn = end_pfn;
8572dc807b3SYinghai Lu }
858ee0c80faSYinghai Lu
859ee0c80faSYinghai Lu if (last_pfn > max_arch_pfn)
860ee0c80faSYinghai Lu last_pfn = max_arch_pfn;
861ee0c80faSYinghai Lu
8621de392f5SJoe Perches pr_info("last_pfn = %#lx max_arch_pfn = %#lx\n",
863ee0c80faSYinghai Lu last_pfn, max_arch_pfn);
864ee0c80faSYinghai Lu return last_pfn;
865ee0c80faSYinghai Lu }
866640e1b38SIngo Molnar
e820__end_of_ram_pfn(void)8670c6fc11aSIngo Molnar unsigned long __init e820__end_of_ram_pfn(void)
868f361a450SYinghai Lu {
869*06fa48d8SKirill A. Shutemov return e820__end_ram_pfn(MAX_ARCH_PFN);
870f361a450SYinghai Lu }
871ee0c80faSYinghai Lu
e820__end_of_low_ram_pfn(void)8720c6fc11aSIngo Molnar unsigned long __init e820__end_of_low_ram_pfn(void)
873f361a450SYinghai Lu {
874*06fa48d8SKirill A. Shutemov return e820__end_ram_pfn(1UL << (32 - PAGE_SHIFT));
875f361a450SYinghai Lu }
876ee0c80faSYinghai Lu
early_panic(char * msg)8778c2103f2SDenys Vlasenko static void __init early_panic(char *msg)
878ab4a465eSYinghai Lu {
879ab4a465eSYinghai Lu early_printk(msg);
880ab4a465eSYinghai Lu panic(msg);
881ab4a465eSYinghai Lu }
882ab4a465eSYinghai Lu
88369a7704dSYinghai Lu static int userdef __initdata;
88469a7704dSYinghai Lu
885640e1b38SIngo Molnar /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */
parse_memopt(char * p)886ab4a465eSYinghai Lu static int __init parse_memopt(char *p)
887ab4a465eSYinghai Lu {
888ab4a465eSYinghai Lu u64 mem_size;
889ab4a465eSYinghai Lu
890ab4a465eSYinghai Lu if (!p)
891ab4a465eSYinghai Lu return -EINVAL;
892ab4a465eSYinghai Lu
893ab4a465eSYinghai Lu if (!strcmp(p, "nopentium")) {
8949a6d44b9SKamal Mostafa #ifdef CONFIG_X86_32
895ab4a465eSYinghai Lu setup_clear_cpu_cap(X86_FEATURE_PSE);
896ab4a465eSYinghai Lu return 0;
8979a6d44b9SKamal Mostafa #else
89801259ef1SIngo Molnar pr_warn("mem=nopentium ignored! (only supported on x86_32)\n");
8999a6d44b9SKamal Mostafa return -EINVAL;
900ab4a465eSYinghai Lu #endif
9019a6d44b9SKamal Mostafa }
902ab4a465eSYinghai Lu
90369a7704dSYinghai Lu userdef = 1;
904ab4a465eSYinghai Lu mem_size = memparse(p, &p);
905640e1b38SIngo Molnar
906640e1b38SIngo Molnar /* Don't remove all memory when getting "mem={invalid}" parameter: */
90777eed821SKamal Mostafa if (mem_size == 0)
90877eed821SKamal Mostafa return -EINVAL;
909640e1b38SIngo Molnar
91009821ff1SIngo Molnar e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
911611dfd78SBernhard Walle
912357b4da5SJuergen Gross #ifdef CONFIG_MEMORY_HOTPLUG
913357b4da5SJuergen Gross max_mem_size = mem_size;
914357b4da5SJuergen Gross #endif
915357b4da5SJuergen Gross
916ab4a465eSYinghai Lu return 0;
917ab4a465eSYinghai Lu }
918ab4a465eSYinghai Lu early_param("mem", parse_memopt);
919ab4a465eSYinghai Lu
parse_memmap_one(char * p)9209710f581SYinghai Lu static int __init parse_memmap_one(char *p)
921ab4a465eSYinghai Lu {
922ab4a465eSYinghai Lu char *oldp;
923ab4a465eSYinghai Lu u64 start_at, mem_size;
924ab4a465eSYinghai Lu
925a737abd1SCyrill Gorcunov if (!p)
926a737abd1SCyrill Gorcunov return -EINVAL;
927a737abd1SCyrill Gorcunov
928d6be118aSPrarit Bhargava if (!strncmp(p, "exactmap", 8)) {
929bf495573SIngo Molnar e820_table->nr_entries = 0;
930ab4a465eSYinghai Lu userdef = 1;
931ab4a465eSYinghai Lu return 0;
932ab4a465eSYinghai Lu }
933ab4a465eSYinghai Lu
934ab4a465eSYinghai Lu oldp = p;
935ab4a465eSYinghai Lu mem_size = memparse(p, &p);
936ab4a465eSYinghai Lu if (p == oldp)
937ab4a465eSYinghai Lu return -EINVAL;
938ab4a465eSYinghai Lu
939ab4a465eSYinghai Lu userdef = 1;
940ab4a465eSYinghai Lu if (*p == '@') {
941ab4a465eSYinghai Lu start_at = memparse(p+1, &p);
94209821ff1SIngo Molnar e820__range_add(start_at, mem_size, E820_TYPE_RAM);
943ab4a465eSYinghai Lu } else if (*p == '#') {
944ab4a465eSYinghai Lu start_at = memparse(p+1, &p);
94509821ff1SIngo Molnar e820__range_add(start_at, mem_size, E820_TYPE_ACPI);
946ab4a465eSYinghai Lu } else if (*p == '$') {
947ab4a465eSYinghai Lu start_at = memparse(p+1, &p);
94809821ff1SIngo Molnar e820__range_add(start_at, mem_size, E820_TYPE_RESERVED);
949ec776ef6SChristoph Hellwig } else if (*p == '!') {
950ec776ef6SChristoph Hellwig start_at = memparse(p+1, &p);
95109821ff1SIngo Molnar e820__range_add(start_at, mem_size, E820_TYPE_PRAM);
952ef61f8a3SJan H. Schönherr } else if (*p == '%') {
953ef61f8a3SJan H. Schönherr enum e820_type from = 0, to = 0;
954ef61f8a3SJan H. Schönherr
955ef61f8a3SJan H. Schönherr start_at = memparse(p + 1, &p);
956ef61f8a3SJan H. Schönherr if (*p == '-')
957ef61f8a3SJan H. Schönherr from = simple_strtoull(p + 1, &p, 0);
958ef61f8a3SJan H. Schönherr if (*p == '+')
959ef61f8a3SJan H. Schönherr to = simple_strtoull(p + 1, &p, 0);
960ef61f8a3SJan H. Schönherr if (*p != '\0')
961ef61f8a3SJan H. Schönherr return -EINVAL;
962ef61f8a3SJan H. Schönherr if (from && to)
963ef61f8a3SJan H. Schönherr e820__range_update(start_at, mem_size, from, to);
964ef61f8a3SJan H. Schönherr else if (to)
965ef61f8a3SJan H. Schönherr e820__range_add(start_at, mem_size, to);
966ef61f8a3SJan H. Schönherr else if (from)
967ef61f8a3SJan H. Schönherr e820__range_remove(start_at, mem_size, from, 1);
968ef61f8a3SJan H. Schönherr else
969ef61f8a3SJan H. Schönherr e820__range_remove(start_at, mem_size, 0, 0);
970640e1b38SIngo Molnar } else {
97109821ff1SIngo Molnar e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1);
972640e1b38SIngo Molnar }
9737b479becSYinghai Lu
974ab4a465eSYinghai Lu return *p == '\0' ? 0 : -EINVAL;
975ab4a465eSYinghai Lu }
976640e1b38SIngo Molnar
parse_memmap_opt(char * str)9779710f581SYinghai Lu static int __init parse_memmap_opt(char *str)
9789710f581SYinghai Lu {
9799710f581SYinghai Lu while (str) {
9809710f581SYinghai Lu char *k = strchr(str, ',');
9819710f581SYinghai Lu
9829710f581SYinghai Lu if (k)
9839710f581SYinghai Lu *k++ = 0;
9849710f581SYinghai Lu
9859710f581SYinghai Lu parse_memmap_one(str);
9869710f581SYinghai Lu str = k;
9879710f581SYinghai Lu }
9889710f581SYinghai Lu
9899710f581SYinghai Lu return 0;
9909710f581SYinghai Lu }
991ab4a465eSYinghai Lu early_param("memmap", parse_memmap_opt);
992ab4a465eSYinghai Lu
9931a127034SIngo Molnar /*
9941a127034SIngo Molnar * Reserve all entries from the bootloader's extensible data nodes list,
9951a127034SIngo Molnar * because if present we are going to use it later on to fetch e820
9961a127034SIngo Molnar * entries from it:
9971a127034SIngo Molnar */
e820__reserve_setup_data(void)9981a127034SIngo Molnar void __init e820__reserve_setup_data(void)
999da92139bSIngo Molnar {
10007228918bSRoss Philipson struct setup_indirect *indirect;
1001da92139bSIngo Molnar struct setup_data *data;
10027228918bSRoss Philipson u64 pa_data, pa_next;
10037228918bSRoss Philipson u32 len;
1004da92139bSIngo Molnar
1005da92139bSIngo Molnar pa_data = boot_params.hdr.setup_data;
1006da92139bSIngo Molnar if (!pa_data)
1007da92139bSIngo Molnar return;
1008da92139bSIngo Molnar
1009da92139bSIngo Molnar while (pa_data) {
1010da92139bSIngo Molnar data = early_memremap(pa_data, sizeof(*data));
10117228918bSRoss Philipson if (!data) {
10127228918bSRoss Philipson pr_warn("e820: failed to memremap setup_data entry\n");
10137228918bSRoss Philipson return;
10147228918bSRoss Philipson }
10157228918bSRoss Philipson
10167228918bSRoss Philipson len = sizeof(*data);
10177228918bSRoss Philipson pa_next = data->next;
10187228918bSRoss Philipson
101909821ff1SIngo Molnar e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
10208efbc518SDave Young
10217228918bSRoss Philipson if (data->type == SETUP_INDIRECT) {
10227228918bSRoss Philipson len += data->len;
10237228918bSRoss Philipson early_memunmap(data, sizeof(*data));
10247228918bSRoss Philipson data = early_memremap(pa_data, len);
10257228918bSRoss Philipson if (!data) {
10267228918bSRoss Philipson pr_warn("e820: failed to memremap indirect setup_data\n");
10277228918bSRoss Philipson return;
1028b3c72fc9SDaniel Kiper }
1029b3c72fc9SDaniel Kiper
10307228918bSRoss Philipson indirect = (struct setup_indirect *)data->data;
10317228918bSRoss Philipson
1032fc7f27cdSDave Young if (indirect->type != SETUP_INDIRECT)
10337228918bSRoss Philipson e820__range_update(indirect->addr, indirect->len,
10347228918bSRoss Philipson E820_TYPE_RAM, E820_TYPE_RESERVED_KERN);
10357228918bSRoss Philipson }
10367228918bSRoss Philipson
10377228918bSRoss Philipson pa_data = pa_next;
10387228918bSRoss Philipson early_memunmap(data, len);
1039da92139bSIngo Molnar }
1040da92139bSIngo Molnar
1041f9748fa0SIngo Molnar e820__update_table(e820_table);
10421a127034SIngo Molnar
10431a127034SIngo Molnar pr_info("extended physical RAM map:\n");
1044be0c3f0fSIngo Molnar e820__print_table("reserve setup_data");
1045da92139bSIngo Molnar }
1046da92139bSIngo Molnar
10479641bdafSIngo Molnar /*
10489641bdafSIngo Molnar * Called after parse_early_param(), after early parameters (such as mem=)
10499641bdafSIngo Molnar * have been processed, in which case we already have an E820 table filled in
10509641bdafSIngo Molnar * via the parameter callback function(s), but it's not sorted and printed yet:
10519641bdafSIngo Molnar */
e820__finish_early_params(void)10529641bdafSIngo Molnar void __init e820__finish_early_params(void)
1053ab4a465eSYinghai Lu {
1054ab4a465eSYinghai Lu if (userdef) {
1055f9748fa0SIngo Molnar if (e820__update_table(e820_table) < 0)
1056ab4a465eSYinghai Lu early_panic("Invalid user supplied memory map");
1057ab4a465eSYinghai Lu
10581de392f5SJoe Perches pr_info("user-defined physical RAM map:\n");
1059be0c3f0fSIngo Molnar e820__print_table("user");
1060ab4a465eSYinghai Lu }
1061ab4a465eSYinghai Lu }
106241c094fdSYinghai Lu
e820_type_to_string(struct e820_entry * entry)1063c594761dSIngo Molnar static const char *__init e820_type_to_string(struct e820_entry *entry)
10645dfcf14dSBernhard Walle {
1065c594761dSIngo Molnar switch (entry->type) {
106609821ff1SIngo Molnar case E820_TYPE_RESERVED_KERN: /* Fall-through: */
106709821ff1SIngo Molnar case E820_TYPE_RAM: return "System RAM";
106809821ff1SIngo Molnar case E820_TYPE_ACPI: return "ACPI Tables";
106909821ff1SIngo Molnar case E820_TYPE_NVS: return "ACPI Non-volatile Storage";
107009821ff1SIngo Molnar case E820_TYPE_UNUSABLE: return "Unusable memory";
107109821ff1SIngo Molnar case E820_TYPE_PRAM: return "Persistent Memory (legacy)";
107209821ff1SIngo Molnar case E820_TYPE_PMEM: return "Persistent Memory";
1073c5231a57SIngo Molnar case E820_TYPE_RESERVED: return "Reserved";
1074262b45aeSDan Williams case E820_TYPE_SOFT_RESERVED: return "Soft Reserved";
1075c5231a57SIngo Molnar default: return "Unknown E820 type";
10765dfcf14dSBernhard Walle }
10775dfcf14dSBernhard Walle }
10785dfcf14dSBernhard Walle
e820_type_to_iomem_type(struct e820_entry * entry)1079c594761dSIngo Molnar static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry)
1080f33b14a4SToshi Kani {
1081c594761dSIngo Molnar switch (entry->type) {
108209821ff1SIngo Molnar case E820_TYPE_RESERVED_KERN: /* Fall-through: */
108309821ff1SIngo Molnar case E820_TYPE_RAM: return IORESOURCE_SYSTEM_RAM;
108409821ff1SIngo Molnar case E820_TYPE_ACPI: /* Fall-through: */
108509821ff1SIngo Molnar case E820_TYPE_NVS: /* Fall-through: */
108609821ff1SIngo Molnar case E820_TYPE_UNUSABLE: /* Fall-through: */
108709821ff1SIngo Molnar case E820_TYPE_PRAM: /* Fall-through: */
108809821ff1SIngo Molnar case E820_TYPE_PMEM: /* Fall-through: */
1089c5231a57SIngo Molnar case E820_TYPE_RESERVED: /* Fall-through: */
1090262b45aeSDan Williams case E820_TYPE_SOFT_RESERVED: /* Fall-through: */
1091640e1b38SIngo Molnar default: return IORESOURCE_MEM;
1092f33b14a4SToshi Kani }
1093f33b14a4SToshi Kani }
1094f33b14a4SToshi Kani
e820_type_to_iores_desc(struct e820_entry * entry)1095c594761dSIngo Molnar static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry)
1096f33b14a4SToshi Kani {
1097c594761dSIngo Molnar switch (entry->type) {
109809821ff1SIngo Molnar case E820_TYPE_ACPI: return IORES_DESC_ACPI_TABLES;
109909821ff1SIngo Molnar case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE;
110009821ff1SIngo Molnar case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY;
110109821ff1SIngo Molnar case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY;
1102ae9e13d6SLianbo Jiang case E820_TYPE_RESERVED: return IORES_DESC_RESERVED;
1103262b45aeSDan Williams case E820_TYPE_SOFT_RESERVED: return IORES_DESC_SOFT_RESERVED;
110409821ff1SIngo Molnar case E820_TYPE_RESERVED_KERN: /* Fall-through: */
110509821ff1SIngo Molnar case E820_TYPE_RAM: /* Fall-through: */
110609821ff1SIngo Molnar case E820_TYPE_UNUSABLE: /* Fall-through: */
1107640e1b38SIngo Molnar default: return IORES_DESC_NONE;
1108f33b14a4SToshi Kani }
1109f33b14a4SToshi Kani }
1110f33b14a4SToshi Kani
do_mark_busy(enum e820_type type,struct resource * res)1111c5231a57SIngo Molnar static bool __init do_mark_busy(enum e820_type type, struct resource *res)
1112ad5fb870SDan Williams {
1113ad5fb870SDan Williams /* this is the legacy bios/dos rom-shadow + mmio region */
1114ad5fb870SDan Williams if (res->start < (1ULL<<20))
1115ad5fb870SDan Williams return true;
1116ad5fb870SDan Williams
1117ad5fb870SDan Williams /*
1118262b45aeSDan Williams * Treat persistent memory and other special memory ranges like
1119262b45aeSDan Williams * device memory, i.e. reserve it for exclusive use of a driver
1120ad5fb870SDan Williams */
1121ad5fb870SDan Williams switch (type) {
112209821ff1SIngo Molnar case E820_TYPE_RESERVED:
1123262b45aeSDan Williams case E820_TYPE_SOFT_RESERVED:
112409821ff1SIngo Molnar case E820_TYPE_PRAM:
112509821ff1SIngo Molnar case E820_TYPE_PMEM:
1126ad5fb870SDan Williams return false;
1127c5231a57SIngo Molnar case E820_TYPE_RESERVED_KERN:
1128c5231a57SIngo Molnar case E820_TYPE_RAM:
1129c5231a57SIngo Molnar case E820_TYPE_ACPI:
1130c5231a57SIngo Molnar case E820_TYPE_NVS:
1131c5231a57SIngo Molnar case E820_TYPE_UNUSABLE:
1132ad5fb870SDan Williams default:
1133ad5fb870SDan Williams return true;
1134ad5fb870SDan Williams }
1135ad5fb870SDan Williams }
1136ad5fb870SDan Williams
113741c094fdSYinghai Lu /*
1138640e1b38SIngo Molnar * Mark E820 reserved areas as busy for the resource manager:
113941c094fdSYinghai Lu */
1140640e1b38SIngo Molnar
1141a5444d15SIngo Molnar static struct resource __initdata *e820_res;
1142640e1b38SIngo Molnar
e820__reserve_resources(void)11431506c8dcSIngo Molnar void __init e820__reserve_resources(void)
114441c094fdSYinghai Lu {
114541c094fdSYinghai Lu int i;
114658f7c988SYinghai Lu struct resource *res;
1147a5444d15SIngo Molnar u64 end;
114841c094fdSYinghai Lu
11497e1c4e27SMike Rapoport res = memblock_alloc(sizeof(*res) * e820_table->nr_entries,
11507e1c4e27SMike Rapoport SMP_CACHE_BYTES);
11518a7f97b9SMike Rapoport if (!res)
11528a7f97b9SMike Rapoport panic("%s: Failed to allocate %zu bytes\n", __func__,
11538a7f97b9SMike Rapoport sizeof(*res) * e820_table->nr_entries);
115458f7c988SYinghai Lu e820_res = res;
1155c594761dSIngo Molnar
1156bf495573SIngo Molnar for (i = 0; i < e820_table->nr_entries; i++) {
1157c594761dSIngo Molnar struct e820_entry *entry = e820_table->entries + i;
1158c594761dSIngo Molnar
1159c594761dSIngo Molnar end = entry->addr + entry->size - 1;
11608308c54dSJeremy Fitzhardinge if (end != (resource_size_t)end) {
116141c094fdSYinghai Lu res++;
116241c094fdSYinghai Lu continue;
116341c094fdSYinghai Lu }
1164c594761dSIngo Molnar res->start = entry->addr;
1165b4df32f4SYinghai Lu res->end = end;
1166c594761dSIngo Molnar res->name = e820_type_to_string(entry);
1167c594761dSIngo Molnar res->flags = e820_type_to_iomem_type(entry);
1168c594761dSIngo Molnar res->desc = e820_type_to_iores_desc(entry);
1169a5444d15SIngo Molnar
1170a5444d15SIngo Molnar /*
11711506c8dcSIngo Molnar * Don't register the region that could be conflicted with
11721506c8dcSIngo Molnar * PCI device BAR resources and insert them later in
11731506c8dcSIngo Molnar * pcibios_resource_survey():
1174a5444d15SIngo Molnar */
1175c594761dSIngo Molnar if (do_mark_busy(entry->type, res)) {
11761f987577SLinus Torvalds res->flags |= IORESOURCE_BUSY;
117741c094fdSYinghai Lu insert_resource(&iomem_resource, res);
11781f987577SLinus Torvalds }
117941c094fdSYinghai Lu res++;
118041c094fdSYinghai Lu }
11815dfcf14dSBernhard Walle
118212df216cSChen Yu /* Expose the bootloader-provided memory layout to the sysfs. */
118312df216cSChen Yu for (i = 0; i < e820_table_firmware->nr_entries; i++) {
118412df216cSChen Yu struct e820_entry *entry = e820_table_firmware->entries + i;
1185640e1b38SIngo Molnar
1186c594761dSIngo Molnar firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry));
11875dfcf14dSBernhard Walle }
118841c094fdSYinghai Lu }
118941c094fdSYinghai Lu
11901506c8dcSIngo Molnar /*
11911506c8dcSIngo Molnar * How much should we pad the end of RAM, depending on where it is?
11921506c8dcSIngo Molnar */
ram_alignment(resource_size_t pos)11938c2103f2SDenys Vlasenko static unsigned long __init ram_alignment(resource_size_t pos)
119445fbe3eeSLinus Torvalds {
119545fbe3eeSLinus Torvalds unsigned long mb = pos >> 20;
119645fbe3eeSLinus Torvalds
119745fbe3eeSLinus Torvalds /* To 64kB in the first megabyte */
119845fbe3eeSLinus Torvalds if (!mb)
119945fbe3eeSLinus Torvalds return 64*1024;
120045fbe3eeSLinus Torvalds
120145fbe3eeSLinus Torvalds /* To 1MB in the first 16MB */
120245fbe3eeSLinus Torvalds if (mb < 16)
120345fbe3eeSLinus Torvalds return 1024*1024;
120445fbe3eeSLinus Torvalds
120515b812f1SYinghai Lu /* To 64MB for anything above that */
120615b812f1SYinghai Lu return 64*1024*1024;
120745fbe3eeSLinus Torvalds }
120845fbe3eeSLinus Torvalds
12097c5371c4SYinghai Lu #define MAX_RESOURCE_SIZE ((resource_size_t)-1)
12107c5371c4SYinghai Lu
e820__reserve_resources_late(void)12111506c8dcSIngo Molnar void __init e820__reserve_resources_late(void)
121258f7c988SYinghai Lu {
121358f7c988SYinghai Lu int i;
121458f7c988SYinghai Lu struct resource *res;
121558f7c988SYinghai Lu
121658f7c988SYinghai Lu res = e820_res;
1217bf495573SIngo Molnar for (i = 0; i < e820_table->nr_entries; i++) {
1218a5444d15SIngo Molnar if (!res->parent && res->end)
12191f987577SLinus Torvalds insert_resource_expand_to_fit(&iomem_resource, res);
122058f7c988SYinghai Lu res++;
122158f7c988SYinghai Lu }
122245fbe3eeSLinus Torvalds
122345fbe3eeSLinus Torvalds /*
1224640e1b38SIngo Molnar * Try to bump up RAM regions to reasonable boundaries, to
122545fbe3eeSLinus Torvalds * avoid stolen RAM:
122645fbe3eeSLinus Torvalds */
1227bf495573SIngo Molnar for (i = 0; i < e820_table->nr_entries; i++) {
1228bf495573SIngo Molnar struct e820_entry *entry = &e820_table->entries[i];
12297c5371c4SYinghai Lu u64 start, end;
123045fbe3eeSLinus Torvalds
123109821ff1SIngo Molnar if (entry->type != E820_TYPE_RAM)
123245fbe3eeSLinus Torvalds continue;
1233640e1b38SIngo Molnar
123445fbe3eeSLinus Torvalds start = entry->addr + entry->size;
12357c5371c4SYinghai Lu end = round_up(start, ram_alignment(start)) - 1;
12367c5371c4SYinghai Lu if (end > MAX_RESOURCE_SIZE)
12377c5371c4SYinghai Lu end = MAX_RESOURCE_SIZE;
12387c5371c4SYinghai Lu if (start >= end)
123945fbe3eeSLinus Torvalds continue;
1240640e1b38SIngo Molnar
1241e22af0beSBorislav Petkov printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end);
1242640e1b38SIngo Molnar reserve_region_with_split(&iomem_resource, start, end, "RAM buffer");
124345fbe3eeSLinus Torvalds }
124458f7c988SYinghai Lu }
124558f7c988SYinghai Lu
1246640e1b38SIngo Molnar /*
1247640e1b38SIngo Molnar * Pass the firmware (bootloader) E820 map to the kernel and process it:
1248640e1b38SIngo Molnar */
e820__memory_setup_default(void)1249103e2063SIngo Molnar char *__init e820__memory_setup_default(void)
1250064d25f1SYinghai Lu {
1251064d25f1SYinghai Lu char *who = "BIOS-e820";
1252640e1b38SIngo Molnar
1253064d25f1SYinghai Lu /*
1254064d25f1SYinghai Lu * Try to copy the BIOS-supplied E820-map.
1255064d25f1SYinghai Lu *
1256064d25f1SYinghai Lu * Otherwise fake a memory map; one section from 0k->640k,
1257064d25f1SYinghai Lu * the next section from 1mb->appropriate_mem_k
1258064d25f1SYinghai Lu */
1259640e1b38SIngo Molnar if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) {
126095a71a45SYinghai Lu u64 mem_size;
126141c094fdSYinghai Lu
1262640e1b38SIngo Molnar /* Compare results from other methods and take the one that gives more RAM: */
1263640e1b38SIngo Molnar if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) {
1264064d25f1SYinghai Lu mem_size = boot_params.screen_info.ext_mem_k;
1265064d25f1SYinghai Lu who = "BIOS-88";
1266064d25f1SYinghai Lu } else {
1267064d25f1SYinghai Lu mem_size = boot_params.alt_mem_k;
1268064d25f1SYinghai Lu who = "BIOS-e801";
1269064d25f1SYinghai Lu }
1270064d25f1SYinghai Lu
1271bf495573SIngo Molnar e820_table->nr_entries = 0;
127209821ff1SIngo Molnar e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM);
127309821ff1SIngo Molnar e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM);
1274064d25f1SYinghai Lu }
1275064d25f1SYinghai Lu
12767410aa1cSIngo Molnar /* We just appended a lot of ranges, sanitize the table: */
12777410aa1cSIngo Molnar e820__update_table(e820_table);
12787410aa1cSIngo Molnar
1279064d25f1SYinghai Lu return who;
1280064d25f1SYinghai Lu }
1281064d25f1SYinghai Lu
1282103e2063SIngo Molnar /*
1283103e2063SIngo Molnar * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader
1284103e2063SIngo Molnar * E820 map - with an optional platform quirk available for virtual platforms
1285103e2063SIngo Molnar * to override this method of boot environment processing:
1286103e2063SIngo Molnar */
e820__memory_setup(void)1287103e2063SIngo Molnar void __init e820__memory_setup(void)
1288064d25f1SYinghai Lu {
12890be15526SYinghai Lu char *who;
12900be15526SYinghai Lu
129109c51513SIngo Molnar /* This is a firmware interface ABI - make sure we don't break it: */
12927410aa1cSIngo Molnar BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20);
129309c51513SIngo Molnar
12946b18ae3eSThomas Gleixner who = x86_init.resources.memory_setup();
1295640e1b38SIngo Molnar
1296a09bae0fSChen Yu memcpy(e820_table_kexec, e820_table, sizeof(*e820_table_kexec));
129712df216cSChen Yu memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware));
1298640e1b38SIngo Molnar
12991de392f5SJoe Perches pr_info("BIOS-provided physical RAM map:\n");
1300be0c3f0fSIngo Molnar e820__print_table(who);
1301064d25f1SYinghai Lu }
130272d7c3b3SYinghai Lu
e820__memblock_setup(void)13034918e228SIngo Molnar void __init e820__memblock_setup(void)
130472d7c3b3SYinghai Lu {
130572d7c3b3SYinghai Lu int i;
130672d7c3b3SYinghai Lu u64 end;
130772d7c3b3SYinghai Lu
130872d7c3b3SYinghai Lu /*
13094918e228SIngo Molnar * The bootstrap memblock region count maximum is 128 entries
13104918e228SIngo Molnar * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries
13114918e228SIngo Molnar * than that - so allow memblock resizing.
13124918e228SIngo Molnar *
13134918e228SIngo Molnar * This is safe, because this call happens pretty late during x86 setup,
13144918e228SIngo Molnar * so we know about reserved memory regions already. (This is important
13154918e228SIngo Molnar * so that memblock resizing does no stomp over reserved areas.)
131672d7c3b3SYinghai Lu */
13171aadc056STejun Heo memblock_allow_resize();
131872d7c3b3SYinghai Lu
1319bf495573SIngo Molnar for (i = 0; i < e820_table->nr_entries; i++) {
1320e5540f87SIngo Molnar struct e820_entry *entry = &e820_table->entries[i];
132172d7c3b3SYinghai Lu
1322e5540f87SIngo Molnar end = entry->addr + entry->size;
132372d7c3b3SYinghai Lu if (end != (resource_size_t)end)
132472d7c3b3SYinghai Lu continue;
132572d7c3b3SYinghai Lu
1326262b45aeSDan Williams if (entry->type == E820_TYPE_SOFT_RESERVED)
1327262b45aeSDan Williams memblock_reserve(entry->addr, entry->size);
1328262b45aeSDan Williams
132909821ff1SIngo Molnar if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN)
13309fd61bc9SMasayoshi Mizuma continue;
13319fd61bc9SMasayoshi Mizuma
1332e5540f87SIngo Molnar memblock_add(entry->addr, entry->size);
133372d7c3b3SYinghai Lu }
133472d7c3b3SYinghai Lu
13354918e228SIngo Molnar /* Throw away partial pages: */
13366ede1fd3SYinghai Lu memblock_trim_memory(PAGE_SIZE);
13376ede1fd3SYinghai Lu
133872d7c3b3SYinghai Lu memblock_dump_all();
133972d7c3b3SYinghai Lu }
1340