blob: 79f0d52fa99a3282c0ba1a04c57cf282d3b40a23 [file] [log] [blame]
Thomas Gleixner2f36fa12008-01-30 13:30:12 +01001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -07004 *
5 * Getting sanitize_e820_map() in sync with i386 version by applying change:
6 * - Provisions for empty E820 memory regions (reported by certain BIOSes).
7 * Alex Achenbach <xela@slit.de>, December 2002.
8 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/init.h>
14#include <linux/bootmem.h>
15#include <linux/ioport.h>
16#include <linux/string.h>
Eric W. Biederman5f5609d2005-06-25 14:58:04 -070017#include <linux/kexec.h>
Andrew Mortonb9491ac2005-09-16 19:27:54 -070018#include <linux/module.h>
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -070019#include <linux/mm.h>
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -070020#include <linux/suspend.h>
21#include <linux/pfn.h>
Andrew Mortonb9491ac2005-09-16 19:27:54 -070022
Andrew Morton1a91023a2006-07-10 04:43:49 -070023#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <asm/page.h>
25#include <asm/e820.h>
26#include <asm/proto.h>
H. Peter Anvin30c82642007-10-15 17:13:22 -070027#include <asm/setup.h>
Andi Kleen2bc04142005-11-05 17:25:53 +010028#include <asm/sections.h>
Thomas Gleixner718fc132008-01-30 13:30:17 +010029#include <asm/kdebug.h>
Pavel Macheke44b7b72008-04-10 23:28:10 +020030#include <asm/trampoline.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Jan Beulichb92e9fa2007-05-02 19:27:11 +020032struct e820map e820;
Andi Kleen3bd4d182006-09-26 10:52:33 +020033
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010034/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * PFN of last memory page.
36 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010037unsigned long end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010039/*
Thomas Gleixner67794292008-03-21 21:27:10 +010040 * end_pfn only includes RAM, while max_pfn_mapped includes all e820 entries.
41 * The direct mapping extends to max_pfn_mapped, so that we can directly access
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 * apertures, ACPI and other tables without having to play with fixmaps.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010043 */
Thomas Gleixner67794292008-03-21 21:27:10 +010044unsigned long max_pfn_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Thomas Gleixner2f36fa12008-01-30 13:30:12 +010046/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * Last pfn which the user wants to use.
48 */
Jan Beulichcaff0712006-09-26 10:52:31 +020049static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Andi Kleen75175272008-01-30 13:33:17 +010051/*
52 * Early reserved memory areas.
53 */
54#define MAX_EARLY_RES 20
55
56struct early_res {
57 unsigned long start, end;
Yinghai Lu25eff8d2008-02-01 17:49:41 +010058 char name[16];
Andi Kleen75175272008-01-30 13:33:17 +010059};
60static struct early_res early_res[MAX_EARLY_RES] __initdata = {
Yinghai Lu25eff8d2008-02-01 17:49:41 +010061 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
Pavel Macheke44b7b72008-04-10 23:28:10 +020062#ifdef CONFIG_X86_TRAMPOLINE
63 { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
Andi Kleen75175272008-01-30 13:33:17 +010064#endif
65 {}
66};
67
Yinghai Lu25eff8d2008-02-01 17:49:41 +010068void __init reserve_early(unsigned long start, unsigned long end, char *name)
Andi Kleen75175272008-01-30 13:33:17 +010069{
70 int i;
71 struct early_res *r;
72 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
73 r = &early_res[i];
74 if (end > r->start && start < r->end)
Yinghai Lu25eff8d2008-02-01 17:49:41 +010075 panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
76 start, end - 1, name?name:"", r->start, r->end - 1, r->name);
Andi Kleen75175272008-01-30 13:33:17 +010077 }
78 if (i >= MAX_EARLY_RES)
79 panic("Too many early reservations");
80 r = &early_res[i];
81 r->start = start;
82 r->end = end;
Yinghai Lu25eff8d2008-02-01 17:49:41 +010083 if (name)
84 strncpy(r->name, name, sizeof(r->name) - 1);
Andi Kleen75175272008-01-30 13:33:17 +010085}
86
Huang, Ying50eae2a2008-03-28 10:49:42 +080087void __init free_early(unsigned long start, unsigned long end)
88{
89 struct early_res *r;
90 int i, j;
91
92 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
93 r = &early_res[i];
94 if (start == r->start && end == r->end)
95 break;
96 }
97 if (i >= MAX_EARLY_RES || !early_res[i].end)
98 panic("free_early on not reserved area: %lx-%lx!", start, end);
99
100 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
101 ;
102
103 memcpy(&early_res[i], &early_res[i + 1],
104 (j - 1 - i) * sizeof(struct early_res));
105
106 early_res[j - 1].end = 0;
107}
108
Andi Kleen75175272008-01-30 13:33:17 +0100109void __init early_res_to_bootmem(void)
110{
111 int i;
112 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
113 struct early_res *r = &early_res[i];
Yinghai Lu25eff8d2008-02-01 17:49:41 +0100114 printk(KERN_INFO "early res: %d [%lx-%lx] %s\n", i,
115 r->start, r->end - 1, r->name);
Andi Kleen75175272008-01-30 13:33:17 +0100116 reserve_bootmem_generic(r->start, r->end - r->start);
117 }
118}
119
120/* Check for already reserved areas */
Jacek Luczak1a7a34a2008-04-10 13:40:57 +0200121static inline int __init
Yinghai Lu48c508b2008-04-17 17:40:45 +0200122bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100123{
Andi Kleen75175272008-01-30 13:33:17 +0100124 int i;
125 unsigned long addr = *addrp, last;
126 int changed = 0;
127again:
128 last = addr + size;
129 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
130 struct early_res *r = &early_res[i];
131 if (last >= r->start && addr < r->end) {
Yinghai Lu48c508b2008-04-17 17:40:45 +0200132 *addrp = addr = round_up(r->end, align);
Andi Kleen75175272008-01-30 13:33:17 +0100133 changed = 1;
134 goto again;
H. Peter Anvin30c82642007-10-15 17:13:22 -0700135 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100136 }
Andi Kleen75175272008-01-30 13:33:17 +0100137 return changed;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700140/* Check for already reserved areas */
Jacek Luczak1a7a34a2008-04-10 13:40:57 +0200141static inline int __init
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700142bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
143{
144 int i;
145 unsigned long addr = *addrp, last;
146 unsigned long size = *sizep;
147 int changed = 0;
148again:
149 last = addr + size;
150 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
151 struct early_res *r = &early_res[i];
152 if (last > r->start && addr < r->start) {
153 size = r->start - addr;
154 changed = 1;
155 goto again;
156 }
157 if (last > r->end && addr < r->end) {
158 addr = round_up(r->end, align);
159 size = last - addr;
160 changed = 1;
161 goto again;
162 }
163 if (last <= r->end && addr >= r->start) {
164 (*sizep)++;
165 return 0;
166 }
167 }
168 if (changed) {
169 *addrp = addr;
170 *sizep = size;
171 }
172 return changed;
173}
Arjan van de Ven95222362006-04-07 19:49:27 +0200174/*
175 * This function checks if any part of the range <start,end> is mapped
176 * with type.
177 */
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200178int
Arjan van de Veneee5a9f2006-04-07 19:49:24 +0200179e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100180{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100182
183 for (i = 0; i < e820.nr_map; i++) {
184 struct e820entry *ei = &e820.map[i];
185
186 if (type && ei->type != type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 continue;
Eric W. Biederman48c8b112005-09-06 15:16:20 -0700188 if (ei->addr >= end || ei->addr + ei->size <= start)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100189 continue;
190 return 1;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return 0;
193}
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200194EXPORT_SYMBOL_GPL(e820_any_mapped);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds79e453d2006-09-19 08:15:22 -0700196/*
197 * This function checks if the entire range <start,end> is mapped with type.
198 *
199 * Note: this function only works correct if the e820 table is sorted and
200 * not-overlapping, which is the case
201 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100202int __init e820_all_mapped(unsigned long start, unsigned long end,
203 unsigned type)
Linus Torvalds79e453d2006-09-19 08:15:22 -0700204{
205 int i;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100206
Linus Torvalds79e453d2006-09-19 08:15:22 -0700207 for (i = 0; i < e820.nr_map; i++) {
208 struct e820entry *ei = &e820.map[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100209
Linus Torvalds79e453d2006-09-19 08:15:22 -0700210 if (type && ei->type != type)
211 continue;
212 /* is the region (part) in overlap with the current region ?*/
213 if (ei->addr >= end || ei->addr + ei->size <= start)
214 continue;
215
216 /* if the region is at the beginning of <start,end> we move
217 * start to the end of the region since it's ok until there
218 */
219 if (ei->addr <= start)
220 start = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100221 /*
222 * if start is now at or beyond end, we're done, full
223 * coverage
224 */
Linus Torvalds79e453d2006-09-19 08:15:22 -0700225 if (start >= end)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100226 return 1;
Linus Torvalds79e453d2006-09-19 08:15:22 -0700227 }
228 return 0;
229}
230
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100231/*
Yinghai Lu24a5da72008-02-01 17:49:41 +0100232 * Find a free area with specified alignment in a specific range.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100233 */
234unsigned long __init find_e820_area(unsigned long start, unsigned long end,
Yinghai Lu48c508b2008-04-17 17:40:45 +0200235 unsigned long size, unsigned long align)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100236{
237 int i;
238
239 for (i = 0; i < e820.nr_map; i++) {
240 struct e820entry *ei = &e820.map[i];
Yinghai Lu48c508b2008-04-17 17:40:45 +0200241 unsigned long addr, last;
242 unsigned long ei_last;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100243
244 if (ei->type != E820_RAM)
245 continue;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200246 addr = round_up(ei->addr, align);
247 ei_last = ei->addr + ei->size;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100248 if (addr < start)
Yinghai Lu48c508b2008-04-17 17:40:45 +0200249 addr = round_up(start, align);
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700250 if (addr >= ei_last)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100251 continue;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200252 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 ;
Yinghai Lu24a5da72008-02-01 17:49:41 +0100254 last = addr + size;
Yinghai Lu48c508b2008-04-17 17:40:45 +0200255 if (last > ei_last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100257 if (last > end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 continue;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100259 return addr;
260 }
261 return -1UL;
262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/*
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700265 * Find next free range after *start
266 */
Yinghai Luc64df702008-03-21 18:56:19 -0700267unsigned long __init find_e820_area_size(unsigned long start,
268 unsigned long *sizep,
269 unsigned long align)
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700270{
271 int i;
272
273 for (i = 0; i < e820.nr_map; i++) {
274 struct e820entry *ei = &e820.map[i];
275 unsigned long addr, last;
276 unsigned long ei_last;
277
278 if (ei->type != E820_RAM)
279 continue;
280 addr = round_up(ei->addr, align);
281 ei_last = ei->addr + ei->size;
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700282 if (addr < start)
283 addr = round_up(start, align);
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700284 if (addr >= ei_last)
285 continue;
286 *sizep = ei_last - addr;
Yinghai Luc64df702008-03-21 18:56:19 -0700287 while (bad_addr_size(&addr, sizep, align) &&
288 addr + *sizep <= ei_last)
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700289 ;
290 last = addr + *sizep;
Yinghai Lu272b9ca2008-03-20 23:58:33 -0700291 if (last > ei_last)
292 continue;
293 return addr;
294 }
295 return -1UL;
296
297}
298/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * Find the highest page frame number we have available
300 */
301unsigned long __init e820_end_of_ram(void)
302{
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100303 unsigned long end_pfn;
304
Mel Gorman5cb248a2006-09-27 01:49:52 -0700305 end_pfn = find_max_pfn_with_active_regions();
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100306
Thomas Gleixner67794292008-03-21 21:27:10 +0100307 if (end_pfn > max_pfn_mapped)
308 max_pfn_mapped = end_pfn;
309 if (max_pfn_mapped > MAXMEM>>PAGE_SHIFT)
310 max_pfn_mapped = MAXMEM>>PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (end_pfn > end_user_pfn)
312 end_pfn = end_user_pfn;
Thomas Gleixner67794292008-03-21 21:27:10 +0100313 if (end_pfn > max_pfn_mapped)
314 end_pfn = max_pfn_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
Thomas Gleixner67794292008-03-21 21:27:10 +0100316 printk(KERN_INFO "max_pfn_mapped = %lu\n", max_pfn_mapped);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100317 return end_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Andi Kleen485761b2005-08-26 18:34:10 -0700320/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * Mark e820 reserved areas as busy for the resource manager.
322 */
Yinghai Lu3def3d62008-02-22 17:07:16 -0800323void __init e820_reserve_resources(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 int i;
Yinghai Lu01561262008-03-20 23:57:21 -0700326 struct resource *res;
327
328 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 for (i = 0; i < e820.nr_map; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 switch (e820.map[i].type) {
331 case E820_RAM: res->name = "System RAM"; break;
332 case E820_ACPI: res->name = "ACPI Tables"; break;
333 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
334 default: res->name = "reserved";
335 }
336 res->start = e820.map[i].addr;
337 res->end = res->start + e820.map[i].size - 1;
338 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
Yinghai Lu3def3d62008-02-22 17:07:16 -0800339 insert_resource(&iomem_resource, res);
Yinghai Lu01561262008-03-20 23:57:21 -0700340 res++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342}
343
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700344/*
345 * Find the ranges of physical addresses that do not correspond to
346 * e820 RAM areas and mark the corresponding pages as nosave for software
347 * suspend and suspend to RAM.
348 *
349 * This function requires the e820 map to be sorted and without any
350 * overlapping entries and assumes the first e820 area to be RAM.
351 */
352void __init e820_mark_nosave_regions(void)
353{
354 int i;
355 unsigned long paddr;
356
357 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
358 for (i = 1; i < e820.nr_map; i++) {
359 struct e820entry *ei = &e820.map[i];
360
361 if (paddr < ei->addr)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700362 register_nosave_region(PFN_DOWN(paddr),
363 PFN_UP(ei->addr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700364
365 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
366 if (ei->type != E820_RAM)
Rafael J. Wysocki74dfd662007-05-06 14:50:43 -0700367 register_nosave_region(PFN_UP(ei->addr),
368 PFN_DOWN(paddr));
Rafael J. Wysockie8eff5a2006-09-25 23:32:46 -0700369
370 if (paddr >= (end_pfn << PAGE_SHIFT))
371 break;
372 }
373}
374
David Rientjes3af044e2007-07-21 17:10:31 +0200375/*
376 * Finds an active region in the address range from start_pfn to end_pfn and
377 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
378 */
379static int __init e820_find_active_region(const struct e820entry *ei,
380 unsigned long start_pfn,
381 unsigned long end_pfn,
382 unsigned long *ei_startpfn,
383 unsigned long *ei_endpfn)
384{
385 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
386 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
387
388 /* Skip map entries smaller than a page */
389 if (*ei_startpfn >= *ei_endpfn)
390 return 0;
391
Thomas Gleixner67794292008-03-21 21:27:10 +0100392 /* Check if max_pfn_mapped should be updated */
393 if (ei->type != E820_RAM && *ei_endpfn > max_pfn_mapped)
394 max_pfn_mapped = *ei_endpfn;
David Rientjes3af044e2007-07-21 17:10:31 +0200395
396 /* Skip if map is outside the node */
397 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
398 *ei_startpfn >= end_pfn)
399 return 0;
400
401 /* Check for overlaps */
402 if (*ei_startpfn < start_pfn)
403 *ei_startpfn = start_pfn;
404 if (*ei_endpfn > end_pfn)
405 *ei_endpfn = end_pfn;
406
407 /* Obey end_user_pfn to save on memmap */
408 if (*ei_startpfn >= end_user_pfn)
409 return 0;
410 if (*ei_endpfn > end_user_pfn)
411 *ei_endpfn = end_user_pfn;
412
413 return 1;
414}
415
Mel Gorman5cb248a2006-09-27 01:49:52 -0700416/* Walk the e820 map and register active regions within a node */
417void __init
418e820_register_active_regions(int nid, unsigned long start_pfn,
419 unsigned long end_pfn)
420{
David Rientjes3af044e2007-07-21 17:10:31 +0200421 unsigned long ei_startpfn;
422 unsigned long ei_endpfn;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700423 int i;
Mel Gorman5cb248a2006-09-27 01:49:52 -0700424
David Rientjes3af044e2007-07-21 17:10:31 +0200425 for (i = 0; i < e820.nr_map; i++)
426 if (e820_find_active_region(&e820.map[i],
427 start_pfn, end_pfn,
428 &ei_startpfn, &ei_endpfn))
429 add_active_range(nid, ei_startpfn, ei_endpfn);
Mel Gorman5cb248a2006-09-27 01:49:52 -0700430}
431
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100432/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * Add a memory region to the kernel e820 map.
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435void __init add_memory_region(unsigned long start, unsigned long size, int type)
436{
437 int x = e820.nr_map;
438
439 if (x == E820MAX) {
440 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
441 return;
442 }
443
444 e820.map[x].addr = start;
445 e820.map[x].size = size;
446 e820.map[x].type = type;
447 e820.nr_map++;
448}
449
David Rientjesa7e96622007-07-21 17:11:29 +0200450/*
451 * Find the hole size (in bytes) in the memory range.
452 * @start: starting address of the memory range to scan
453 * @end: ending address of the memory range to scan
454 */
455unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
456{
457 unsigned long start_pfn = start >> PAGE_SHIFT;
458 unsigned long end_pfn = end >> PAGE_SHIFT;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100459 unsigned long ei_startpfn, ei_endpfn, ram = 0;
David Rientjesa7e96622007-07-21 17:11:29 +0200460 int i;
461
462 for (i = 0; i < e820.nr_map; i++) {
463 if (e820_find_active_region(&e820.map[i],
464 start_pfn, end_pfn,
465 &ei_startpfn, &ei_endpfn))
466 ram += ei_endpfn - ei_startpfn;
467 }
468 return end - start - (ram << PAGE_SHIFT);
469}
470
Adrian Bunk013d23e2008-01-30 13:30:30 +0100471static void __init e820_print_map(char *who)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 int i;
474
475 for (i = 0; i < e820.nr_map; i++) {
Dan Aloni5a3ece72007-07-21 17:11:37 +0200476 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100477 (unsigned long long) e820.map[i].addr,
478 (unsigned long long)
479 (e820.map[i].addr + e820.map[i].size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 switch (e820.map[i].type) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100481 case E820_RAM:
482 printk(KERN_CONT "(usable)\n");
483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 case E820_RESERVED:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100485 printk(KERN_CONT "(reserved)\n");
486 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 case E820_ACPI:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100488 printk(KERN_CONT "(ACPI data)\n");
489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 case E820_NVS:
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100491 printk(KERN_CONT "(ACPI NVS)\n");
492 break;
493 default:
494 printk(KERN_CONT "type %u\n", e820.map[i].type);
495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
497 }
498}
499
500/*
501 * Sanitize the BIOS e820 map.
502 *
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100503 * Some e820 responses include overlapping entries. The following
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * replaces the original e820 map with a new one, removing overlaps.
505 *
506 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100507static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
509 struct change_member {
510 struct e820entry *pbios; /* pointer to original bios entry */
511 unsigned long long addr; /* address for this change point */
512 };
513 static struct change_member change_point_list[2*E820MAX] __initdata;
514 static struct change_member *change_point[2*E820MAX] __initdata;
515 static struct e820entry *overlap_list[E820MAX] __initdata;
516 static struct e820entry new_bios[E820MAX] __initdata;
517 struct change_member *change_tmp;
518 unsigned long current_type, last_type;
519 unsigned long long last_addr;
520 int chgidx, still_changing;
521 int overlap_entries;
522 int new_bios_entry;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700523 int old_nr, new_nr, chg_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 int i;
525
526 /*
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100527 Visually we're performing the following
528 (1,2,3,4 = memory types)...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
530 Sample memory map (w/overlaps):
531 ____22__________________
532 ______________________4_
533 ____1111________________
534 _44_____________________
535 11111111________________
536 ____________________33__
537 ___________44___________
538 __________33333_________
539 ______________22________
540 ___________________2222_
541 _________111111111______
542 _____________________11_
543 _________________4______
544
545 Sanitized equivalent (no overlap):
546 1_______________________
547 _44_____________________
548 ___1____________________
549 ____22__________________
550 ______11________________
551 _________1______________
552 __________3_____________
553 ___________44___________
554 _____________33_________
555 _______________2________
556 ________________1_______
557 _________________4______
558 ___________________2____
559 ____________________33__
560 ______________________4_
561 */
562
563 /* if there's only one memory region, don't bother */
564 if (*pnr_map < 2)
565 return -1;
566
567 old_nr = *pnr_map;
568
569 /* bail out if we find any unreasonable addresses in bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100570 for (i = 0; i < old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
572 return -1;
573
574 /* create pointers for initial change-point information (for sorting) */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100575 for (i = 0; i < 2 * old_nr; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 change_point[i] = &change_point_list[i];
577
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700578 /* record all known change-points (starting and ending addresses),
579 omitting those that are for empty memory regions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 chgidx = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100581 for (i = 0; i < old_nr; i++) {
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700582 if (biosmap[i].size != 0) {
583 change_point[chgidx]->addr = biosmap[i].addr;
584 change_point[chgidx++]->pbios = &biosmap[i];
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100585 change_point[chgidx]->addr = biosmap[i].addr +
586 biosmap[i].size;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700587 change_point[chgidx++]->pbios = &biosmap[i];
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 }
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700590 chg_nr = chgidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 /* sort change-point list by memory addresses (low -> high) */
593 still_changing = 1;
594 while (still_changing) {
595 still_changing = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100596 for (i = 1; i < chg_nr; i++) {
597 unsigned long long curaddr, lastaddr;
598 unsigned long long curpbaddr, lastpbaddr;
599
600 curaddr = change_point[i]->addr;
601 lastaddr = change_point[i - 1]->addr;
602 curpbaddr = change_point[i]->pbios->addr;
603 lastpbaddr = change_point[i - 1]->pbios->addr;
604
605 /*
606 * swap entries, when:
607 *
608 * curaddr > lastaddr or
609 * curaddr == lastaddr and curaddr == curpbaddr and
610 * lastaddr != lastpbaddr
611 */
612 if (curaddr < lastaddr ||
613 (curaddr == lastaddr && curaddr == curpbaddr &&
614 lastaddr != lastpbaddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 change_tmp = change_point[i];
616 change_point[i] = change_point[i-1];
617 change_point[i-1] = change_tmp;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100618 still_changing = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620 }
621 }
622
623 /* create a new bios memory map, removing overlaps */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100624 overlap_entries = 0; /* number of entries in the overlap table */
625 new_bios_entry = 0; /* index for creating new bios map entries */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 last_type = 0; /* start with undefined memory type */
627 last_addr = 0; /* start with 0 as last starting address */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* loop through change-points, determining affect on the new bios map */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100630 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* keep track of all overlapping bios entries */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100632 if (change_point[chgidx]->addr ==
633 change_point[chgidx]->pbios->addr) {
634 /*
635 * add map entry to overlap list (> 1 entry
636 * implies an overlap)
637 */
638 overlap_list[overlap_entries++] =
639 change_point[chgidx]->pbios;
640 } else {
641 /*
642 * remove entry from list (order independent,
643 * so swap with last)
644 */
645 for (i = 0; i < overlap_entries; i++) {
646 if (overlap_list[i] ==
647 change_point[chgidx]->pbios)
648 overlap_list[i] =
649 overlap_list[overlap_entries-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 }
651 overlap_entries--;
652 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100653 /*
654 * if there are overlapping entries, decide which
655 * "type" to use (larger value takes precedence --
656 * 1=usable, 2,3,4,4+=unusable)
657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 current_type = 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100659 for (i = 0; i < overlap_entries; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (overlap_list[i]->type > current_type)
661 current_type = overlap_list[i]->type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100662 /*
663 * continue building up new bios map based on this
664 * information
665 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (current_type != last_type) {
667 if (last_type != 0) {
668 new_bios[new_bios_entry].size =
669 change_point[chgidx]->addr - last_addr;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100670 /*
671 * move forward only if the new size
672 * was non-zero
673 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (new_bios[new_bios_entry].size != 0)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100675 /*
676 * no more space left for new
677 * bios entries ?
678 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (++new_bios_entry >= E820MAX)
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100680 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682 if (current_type != 0) {
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100683 new_bios[new_bios_entry].addr =
684 change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 new_bios[new_bios_entry].type = current_type;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100686 last_addr = change_point[chgidx]->addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
688 last_type = current_type;
689 }
690 }
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100691 /* retain count for new bios entries */
692 new_nr = new_bios_entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /* copy new bios mapping into original location */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100695 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 *pnr_map = new_nr;
697
698 return 0;
699}
700
701/*
702 * Copy the BIOS e820 map into a safe place.
703 *
704 * Sanity-check it while we're at it..
705 *
706 * If we're lucky and live on a modern system, the setup code
707 * will have given us a memory map that we can use to properly
708 * set up memory. If we aren't, we'll fake a memory map.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 */
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100710static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 /* Only one memory region (or negative)? Ignore it */
713 if (nr_map < 2)
714 return -1;
715
716 do {
Alexander van Heukelum320a6b22008-03-01 17:12:43 +0100717 u64 start = biosmap->addr;
718 u64 size = biosmap->size;
719 u64 end = start + size;
720 u32 type = biosmap->type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /* Overflow in 64 bits? Ignore the memory map. */
723 if (start > end)
724 return -1;
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 add_memory_region(start, size, type);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100727 } while (biosmap++, --nr_map);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
729}
730
Adrian Bunk013d23e2008-01-30 13:30:30 +0100731static void early_panic(char *msg)
Andi Kleen8380aab2006-09-26 10:52:37 +0200732{
733 early_printk(msg);
734 panic(msg);
735}
736
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100737/* We're not void only for x86 32-bit compat */
738char * __init machine_specific_memory_setup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100740 char *who = "BIOS-e820";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /*
742 * Try to copy the BIOS-supplied E820-map.
743 *
744 * Otherwise fake a memory map; one section from 0k->640k,
745 * the next section from 1mb->appropriate_mem_k
746 */
H. Peter Anvin30c82642007-10-15 17:13:22 -0700747 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
748 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
Andi Kleen8380aab2006-09-26 10:52:37 +0200749 early_panic("Cannot find a valid memory map");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
Glauber de Oliveira Costa746ef0c2008-01-30 13:31:11 +0100751 e820_print_map(who);
752
753 /* In case someone cares... */
754 return who;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200757static int __init parse_memopt(char *p)
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800758{
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200759 if (!p)
760 return -EINVAL;
761 end_user_pfn = memparse(p, &p);
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100762 end_user_pfn >>= PAGE_SHIFT;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200763 return 0;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100764}
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200765early_param("mem", parse_memopt);
766
767static int userdef __initdata;
768
769static int __init parse_memmap_opt(char *p)
770{
771 char *oldp;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800772 unsigned long long start_at, mem_size;
773
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200774 if (!strcmp(p, "exactmap")) {
775#ifdef CONFIG_CRASH_DUMP
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100776 /*
777 * If we are doing a crash dump, we still need to know
778 * the real mem size before original memory map is
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200779 * reset.
780 */
Magnus Damm15803a42006-11-14 16:57:46 +0100781 e820_register_active_regions(0, 0, -1UL);
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200782 saved_max_pfn = e820_end_of_ram();
Magnus Damm15803a42006-11-14 16:57:46 +0100783 remove_all_active_ranges();
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200784#endif
Thomas Gleixner67794292008-03-21 21:27:10 +0100785 max_pfn_mapped = 0;
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200786 e820.nr_map = 0;
787 userdef = 1;
788 return 0;
789 }
790
791 oldp = p;
792 mem_size = memparse(p, &p);
793 if (p == oldp)
794 return -EINVAL;
Vladimir Bereznikerb3ca74a2008-01-30 13:30:46 +0100795
796 userdef = 1;
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800797 if (*p == '@') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200798 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800799 add_memory_region(start_at, mem_size, E820_RAM);
800 } else if (*p == '#') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200801 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800802 add_memory_region(start_at, mem_size, E820_ACPI);
803 } else if (*p == '$') {
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200804 start_at = memparse(p+1, &p);
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800805 add_memory_region(start_at, mem_size, E820_RESERVED);
806 } else {
807 end_user_pfn = (mem_size >> PAGE_SHIFT);
808 }
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200809 return *p == '\0' ? 0 : -EINVAL;
810}
811early_param("memmap", parse_memmap_opt);
812
Sam Ravnborg43999d92007-03-16 21:07:36 +0100813void __init finish_e820_parsing(void)
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200814{
815 if (userdef) {
Vladimir Bereznikerb3ca74a2008-01-30 13:30:46 +0100816 char nr = e820.nr_map;
817
818 if (sanitize_e820_map(e820.map, &nr) < 0)
819 early_panic("Invalid user supplied memory map");
820 e820.nr_map = nr;
821
Andi Kleen2c8c0e62006-09-26 10:52:32 +0200822 printk(KERN_INFO "user-defined physical RAM map:\n");
823 e820_print_map("user");
824 }
akpm@osdl.org69cda7b2006-01-09 20:51:46 -0800825}
826
Yinghai Lu5dca6a12008-03-18 16:44:19 -0700827void __init update_memory_range(u64 start, u64 size, unsigned old_type,
828 unsigned new_type)
829{
830 int i;
831
832 BUG_ON(old_type == new_type);
833
834 for (i = 0; i < e820.nr_map; i++) {
835 struct e820entry *ei = &e820.map[i];
836 u64 final_start, final_end;
837 if (ei->type != old_type)
838 continue;
839 /* totally covered? */
840 if (ei->addr >= start && ei->size <= size) {
841 ei->type = new_type;
842 continue;
843 }
844 /* partially covered */
845 final_start = max(start, ei->addr);
846 final_end = min(start + size, ei->addr + ei->size);
847 if (final_start >= final_end)
848 continue;
849 add_memory_region(final_start, final_end - final_start,
850 new_type);
851 }
852}
853
Yinghai Luaaf23042008-01-30 13:33:09 +0100854void __init update_e820(void)
855{
856 u8 nr_map;
857
858 nr_map = e820.nr_map;
859 if (sanitize_e820_map(e820.map, &nr_map))
860 return;
861 e820.nr_map = nr_map;
862 printk(KERN_INFO "modified physical RAM map:\n");
863 e820_print_map("modified");
864}
865
Andi Kleena1e97782005-04-16 15:25:12 -0700866unsigned long pci_mem_start = 0xaeedbabe;
Andi Kleen2ee60e172006-06-26 13:59:44 +0200867EXPORT_SYMBOL(pci_mem_start);
Andi Kleena1e97782005-04-16 15:25:12 -0700868
869/*
870 * Search for the biggest gap in the low 32 bits of the e820
871 * memory space. We pass this space to PCI to assign MMIO resources
872 * for hotplug or unconfigured devices in.
873 * Hopefully the BIOS let enough space left.
874 */
875__init void e820_setup_gap(void)
876{
Daniel Ritzf0eca962005-09-09 00:57:14 +0200877 unsigned long gapstart, gapsize, round;
Andi Kleena1e97782005-04-16 15:25:12 -0700878 unsigned long last;
879 int i;
880 int found = 0;
881
882 last = 0x100000000ull;
883 gapstart = 0x10000000;
884 gapsize = 0x400000;
885 i = e820.nr_map;
886 while (--i >= 0) {
887 unsigned long long start = e820.map[i].addr;
888 unsigned long long end = start + e820.map[i].size;
889
890 /*
891 * Since "last" is at most 4GB, we know we'll
892 * fit in 32 bits if this condition is true
893 */
894 if (last > end) {
895 unsigned long gap = last - end;
896
897 if (gap > gapsize) {
898 gapsize = gap;
899 gapstart = end;
900 found = 1;
901 }
902 }
903 if (start < last)
904 last = start;
905 }
906
907 if (!found) {
908 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100909 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
910 "address range\n"
911 KERN_ERR "PCI: Unassigned devices with 32bit resource "
912 "registers may break!\n");
Andi Kleena1e97782005-04-16 15:25:12 -0700913 }
914
915 /*
Daniel Ritzf0eca962005-09-09 00:57:14 +0200916 * See how much we want to round up: start off with
917 * rounding to the next 1MB area.
Andi Kleena1e97782005-04-16 15:25:12 -0700918 */
Daniel Ritzf0eca962005-09-09 00:57:14 +0200919 round = 0x100000;
920 while ((gapsize >> 4) > round)
921 round += round;
922 /* Fun with two's complement */
923 pci_mem_start = (gapstart + round) & -round;
Andi Kleena1e97782005-04-16 15:25:12 -0700924
Thomas Gleixner2f36fa12008-01-30 13:30:12 +0100925 printk(KERN_INFO
926 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
927 pci_mem_start, gapstart, gapsize);
Andi Kleena1e97782005-04-16 15:25:12 -0700928}
Keshavamurthy, Anil Se8204822007-10-21 16:41:55 -0700929
930int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
931{
932 int i;
933
934 if (slot < 0 || slot >= e820.nr_map)
935 return -1;
936 for (i = slot; i < e820.nr_map; i++) {
937 if (e820.map[i].type != E820_RAM)
938 continue;
939 break;
940 }
941 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
942 return -1;
943 *addr = e820.map[i].addr;
944 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
945 max_pfn << PAGE_SHIFT) - *addr;
946 return i + 1;
947}