]> bbs.cooldavid.org Git - net-next-2.6.git/blame - arch/x86/kernel/e820_64.c
x86: memtest bootparam
[net-next-2.6.git] / arch / x86 / kernel / e820_64.c
CommitLineData
2f36fa13 1/*
1da177e4
LT
2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
8059b2a2
VP
4 *
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 *
1da177e4 10 */
1da177e4
LT
11#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>
5f5609df 17#include <linux/kexec.h>
b9491ac8 18#include <linux/module.h>
e8eff5ac 19#include <linux/mm.h>
74dfd666
RW
20#include <linux/suspend.h>
21#include <linux/pfn.h>
b9491ac8 22
1a91023a 23#include <asm/pgtable.h>
1da177e4
LT
24#include <asm/page.h>
25#include <asm/e820.h>
26#include <asm/proto.h>
30c82645 27#include <asm/setup.h>
2bc0414e 28#include <asm/sections.h>
718fc13b 29#include <asm/kdebug.h>
1da177e4 30
b92e9fac 31struct e820map e820;
3bd4d18c 32
2f36fa13 33/*
1da177e4
LT
34 * PFN of last memory page.
35 */
2f36fa13 36unsigned long end_pfn;
1da177e4 37
2f36fa13 38/*
1da177e4
LT
39 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
40 * The direct mapping extends to end_pfn_map, so that we can directly access
41 * apertures, ACPI and other tables without having to play with fixmaps.
2f36fa13
TG
42 */
43unsigned long end_pfn_map;
1da177e4 44
2f36fa13 45/*
1da177e4
LT
46 * Last pfn which the user wants to use.
47 */
caff0710 48static unsigned long __initdata end_user_pfn = MAXMEM>>PAGE_SHIFT;
1da177e4 49
75175278
AK
50/*
51 * Early reserved memory areas.
52 */
53#define MAX_EARLY_RES 20
30c82645 54
75175278
AK
55struct early_res {
56 unsigned long start, end;
25eff8d4 57 char name[16];
75175278
AK
58};
59static struct early_res early_res[MAX_EARLY_RES] __initdata = {
25eff8d4 60 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
75175278 61#ifdef CONFIG_SMP
25eff8d4 62 { SMP_TRAMPOLINE_BASE, SMP_TRAMPOLINE_BASE + 2*PAGE_SIZE, "SMP_TRAMPOLINE" },
1da177e4 63#endif
75175278
AK
64 {}
65};
66
25eff8d4 67void __init reserve_early(unsigned long start, unsigned long end, char *name)
75175278
AK
68{
69 int i;
70 struct early_res *r;
71 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
72 r = &early_res[i];
73 if (end > r->start && start < r->end)
25eff8d4
YL
74 panic("Overlapping early reservations %lx-%lx %s to %lx-%lx %s\n",
75 start, end - 1, name?name:"", r->start, r->end - 1, r->name);
1da177e4 76 }
75175278
AK
77 if (i >= MAX_EARLY_RES)
78 panic("Too many early reservations");
79 r = &early_res[i];
80 r->start = start;
81 r->end = end;
25eff8d4
YL
82 if (name)
83 strncpy(r->name, name, sizeof(r->name) - 1);
75175278 84}
ac71d12c 85
75175278
AK
86void __init early_res_to_bootmem(void)
87{
88 int i;
89 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
90 struct early_res *r = &early_res[i];
25eff8d4
YL
91 printk(KERN_INFO "early res: %d [%lx-%lx] %s\n", i,
92 r->start, r->end - 1, r->name);
75175278 93 reserve_bootmem_generic(r->start, r->end - r->start);
ac71d12c 94 }
75175278 95}
ac71d12c 96
75175278 97/* Check for already reserved areas */
48c508b3
YL
98static inline int
99bad_addr(unsigned long *addrp, unsigned long size, unsigned long align)
75175278
AK
100{
101 int i;
102 unsigned long addr = *addrp, last;
103 int changed = 0;
104again:
105 last = addr + size;
106 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
107 struct early_res *r = &early_res[i];
108 if (last >= r->start && addr < r->end) {
48c508b3 109 *addrp = addr = round_up(r->end, align);
75175278
AK
110 changed = 1;
111 goto again;
112 }
076422d2 113 }
75175278 114 return changed;
2f36fa13 115}
1da177e4 116
272b9cad
YL
117/* Check for already reserved areas */
118static inline int
119bad_addr_size(unsigned long *addrp, unsigned long *sizep, unsigned long align)
120{
121 int i;
122 unsigned long addr = *addrp, last;
123 unsigned long size = *sizep;
124 int changed = 0;
125again:
126 last = addr + size;
127 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
128 struct early_res *r = &early_res[i];
129 if (last > r->start && addr < r->start) {
130 size = r->start - addr;
131 changed = 1;
132 goto again;
133 }
134 if (last > r->end && addr < r->end) {
135 addr = round_up(r->end, align);
136 size = last - addr;
137 changed = 1;
138 goto again;
139 }
140 if (last <= r->end && addr >= r->start) {
141 (*sizep)++;
142 return 0;
143 }
144 }
145 if (changed) {
146 *addrp = addr;
147 *sizep = size;
148 }
149 return changed;
150}
95222368
AV
151/*
152 * This function checks if any part of the range <start,end> is mapped
153 * with type.
154 */
b92e9fac 155int
eee5a9fa 156e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
2f36fa13 157{
1da177e4 158 int i;
2f36fa13
TG
159
160 for (i = 0; i < e820.nr_map; i++) {
161 struct e820entry *ei = &e820.map[i];
162
163 if (type && ei->type != type)
1da177e4 164 continue;
48c8b113 165 if (ei->addr >= end || ei->addr + ei->size <= start)
2f36fa13
TG
166 continue;
167 return 1;
168 }
1da177e4
LT
169 return 0;
170}
b92e9fac 171EXPORT_SYMBOL_GPL(e820_any_mapped);
1da177e4 172
79e453d4
LT
173/*
174 * This function checks if the entire range <start,end> is mapped with type.
175 *
176 * Note: this function only works correct if the e820 table is sorted and
177 * not-overlapping, which is the case
178 */
2f36fa13
TG
179int __init e820_all_mapped(unsigned long start, unsigned long end,
180 unsigned type)
79e453d4
LT
181{
182 int i;
2f36fa13 183
79e453d4
LT
184 for (i = 0; i < e820.nr_map; i++) {
185 struct e820entry *ei = &e820.map[i];
2f36fa13 186
79e453d4
LT
187 if (type && ei->type != type)
188 continue;
189 /* is the region (part) in overlap with the current region ?*/
190 if (ei->addr >= end || ei->addr + ei->size <= start)
191 continue;
192
193 /* if the region is at the beginning of <start,end> we move
194 * start to the end of the region since it's ok until there
195 */
196 if (ei->addr <= start)
197 start = ei->addr + ei->size;
2f36fa13
TG
198 /*
199 * if start is now at or beyond end, we're done, full
200 * coverage
201 */
79e453d4 202 if (start >= end)
2f36fa13 203 return 1;
79e453d4
LT
204 }
205 return 0;
206}
207
2f36fa13 208/*
24a5da73 209 * Find a free area with specified alignment in a specific range.
2f36fa13
TG
210 */
211unsigned long __init find_e820_area(unsigned long start, unsigned long end,
48c508b3 212 unsigned long size, unsigned long align)
2f36fa13
TG
213{
214 int i;
215
216 for (i = 0; i < e820.nr_map; i++) {
217 struct e820entry *ei = &e820.map[i];
48c508b3
YL
218 unsigned long addr, last;
219 unsigned long ei_last;
2f36fa13
TG
220
221 if (ei->type != E820_RAM)
222 continue;
48c508b3
YL
223 addr = round_up(ei->addr, align);
224 ei_last = ei->addr + ei->size;
2f36fa13 225 if (addr < start)
48c508b3 226 addr = round_up(start, align);
272b9cad 227 if (addr >= ei_last)
2f36fa13 228 continue;
48c508b3 229 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
1da177e4 230 ;
24a5da73 231 last = addr + size;
48c508b3 232 if (last > ei_last)
1da177e4 233 continue;
2f36fa13 234 if (last > end)
1da177e4 235 continue;
2f36fa13
TG
236 return addr;
237 }
238 return -1UL;
239}
1da177e4 240
272b9cad
YL
241/*
242 * Find next free range after *start
243 */
c64df707
YL
244unsigned long __init find_e820_area_size(unsigned long start,
245 unsigned long *sizep,
246 unsigned long align)
272b9cad
YL
247{
248 int i;
249
250 for (i = 0; i < e820.nr_map; i++) {
251 struct e820entry *ei = &e820.map[i];
252 unsigned long addr, last;
253 unsigned long ei_last;
254
255 if (ei->type != E820_RAM)
256 continue;
257 addr = round_up(ei->addr, align);
258 ei_last = ei->addr + ei->size;
272b9cad
YL
259 if (addr < start)
260 addr = round_up(start, align);
272b9cad
YL
261 if (addr >= ei_last)
262 continue;
263 *sizep = ei_last - addr;
c64df707
YL
264 while (bad_addr_size(&addr, sizep, align) &&
265 addr + *sizep <= ei_last)
272b9cad
YL
266 ;
267 last = addr + *sizep;
272b9cad
YL
268 if (last > ei_last)
269 continue;
270 return addr;
271 }
272 return -1UL;
273
274}
1da177e4
LT
275/*
276 * Find the highest page frame number we have available
277 */
278unsigned long __init e820_end_of_ram(void)
279{
2f36fa13
TG
280 unsigned long end_pfn;
281
5cb248ab 282 end_pfn = find_max_pfn_with_active_regions();
2f36fa13
TG
283
284 if (end_pfn > end_pfn_map)
1da177e4
LT
285 end_pfn_map = end_pfn;
286 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
287 end_pfn_map = MAXMEM>>PAGE_SHIFT;
288 if (end_pfn > end_user_pfn)
289 end_pfn = end_user_pfn;
2f36fa13
TG
290 if (end_pfn > end_pfn_map)
291 end_pfn = end_pfn_map;
1da177e4 292
2f36fa13
TG
293 printk(KERN_INFO "end_pfn_map = %lu\n", end_pfn_map);
294 return end_pfn;
1da177e4
LT
295}
296
485761bd 297/*
1da177e4
LT
298 * Mark e820 reserved areas as busy for the resource manager.
299 */
3def3d6d 300void __init e820_reserve_resources(void)
1da177e4
LT
301{
302 int i;
01561264
YL
303 struct resource *res;
304
305 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
1da177e4 306 for (i = 0; i < e820.nr_map; i++) {
1da177e4
LT
307 switch (e820.map[i].type) {
308 case E820_RAM: res->name = "System RAM"; break;
309 case E820_ACPI: res->name = "ACPI Tables"; break;
310 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
311 default: res->name = "reserved";
312 }
313 res->start = e820.map[i].addr;
314 res->end = res->start + e820.map[i].size - 1;
315 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
3def3d6d 316 insert_resource(&iomem_resource, res);
01561264 317 res++;
1da177e4
LT
318 }
319}
320
e8eff5ac
RW
321/*
322 * Find the ranges of physical addresses that do not correspond to
323 * e820 RAM areas and mark the corresponding pages as nosave for software
324 * suspend and suspend to RAM.
325 *
326 * This function requires the e820 map to be sorted and without any
327 * overlapping entries and assumes the first e820 area to be RAM.
328 */
329void __init e820_mark_nosave_regions(void)
330{
331 int i;
332 unsigned long paddr;
333
334 paddr = round_down(e820.map[0].addr + e820.map[0].size, PAGE_SIZE);
335 for (i = 1; i < e820.nr_map; i++) {
336 struct e820entry *ei = &e820.map[i];
337
338 if (paddr < ei->addr)
74dfd666
RW
339 register_nosave_region(PFN_DOWN(paddr),
340 PFN_UP(ei->addr));
e8eff5ac
RW
341
342 paddr = round_down(ei->addr + ei->size, PAGE_SIZE);
343 if (ei->type != E820_RAM)
74dfd666
RW
344 register_nosave_region(PFN_UP(ei->addr),
345 PFN_DOWN(paddr));
e8eff5ac
RW
346
347 if (paddr >= (end_pfn << PAGE_SHIFT))
348 break;
349 }
350}
351
3af044e0
DR
352/*
353 * Finds an active region in the address range from start_pfn to end_pfn and
354 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
355 */
356static int __init e820_find_active_region(const struct e820entry *ei,
357 unsigned long start_pfn,
358 unsigned long end_pfn,
359 unsigned long *ei_startpfn,
360 unsigned long *ei_endpfn)
361{
362 *ei_startpfn = round_up(ei->addr, PAGE_SIZE) >> PAGE_SHIFT;
363 *ei_endpfn = round_down(ei->addr + ei->size, PAGE_SIZE) >> PAGE_SHIFT;
364
365 /* Skip map entries smaller than a page */
366 if (*ei_startpfn >= *ei_endpfn)
367 return 0;
368
369 /* Check if end_pfn_map should be updated */
370 if (ei->type != E820_RAM && *ei_endpfn > end_pfn_map)
371 end_pfn_map = *ei_endpfn;
372
373 /* Skip if map is outside the node */
374 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
375 *ei_startpfn >= end_pfn)
376 return 0;
377
378 /* Check for overlaps */
379 if (*ei_startpfn < start_pfn)
380 *ei_startpfn = start_pfn;
381 if (*ei_endpfn > end_pfn)
382 *ei_endpfn = end_pfn;
383
384 /* Obey end_user_pfn to save on memmap */
385 if (*ei_startpfn >= end_user_pfn)
386 return 0;
387 if (*ei_endpfn > end_user_pfn)
388 *ei_endpfn = end_user_pfn;
389
390 return 1;
391}
392
5cb248ab
MG
393/* Walk the e820 map and register active regions within a node */
394void __init
395e820_register_active_regions(int nid, unsigned long start_pfn,
396 unsigned long end_pfn)
397{
3af044e0
DR
398 unsigned long ei_startpfn;
399 unsigned long ei_endpfn;
5cb248ab 400 int i;
5cb248ab 401
3af044e0
DR
402 for (i = 0; i < e820.nr_map; i++)
403 if (e820_find_active_region(&e820.map[i],
404 start_pfn, end_pfn,
405 &ei_startpfn, &ei_endpfn))
406 add_active_range(nid, ei_startpfn, ei_endpfn);
5cb248ab
MG
407}
408
2f36fa13 409/*
1da177e4 410 * Add a memory region to the kernel e820 map.
2f36fa13 411 */
1da177e4
LT
412void __init add_memory_region(unsigned long start, unsigned long size, int type)
413{
414 int x = e820.nr_map;
415
416 if (x == E820MAX) {
417 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
418 return;
419 }
420
421 e820.map[x].addr = start;
422 e820.map[x].size = size;
423 e820.map[x].type = type;
424 e820.nr_map++;
425}
426
a7e96629
DR
427/*
428 * Find the hole size (in bytes) in the memory range.
429 * @start: starting address of the memory range to scan
430 * @end: ending address of the memory range to scan
431 */
432unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
433{
434 unsigned long start_pfn = start >> PAGE_SHIFT;
435 unsigned long end_pfn = end >> PAGE_SHIFT;
2f36fa13 436 unsigned long ei_startpfn, ei_endpfn, ram = 0;
a7e96629
DR
437 int i;
438
439 for (i = 0; i < e820.nr_map; i++) {
440 if (e820_find_active_region(&e820.map[i],
441 start_pfn, end_pfn,
442 &ei_startpfn, &ei_endpfn))
443 ram += ei_endpfn - ei_startpfn;
444 }
445 return end - start - (ram << PAGE_SHIFT);
446}
447
013d23e1 448static void __init e820_print_map(char *who)
1da177e4
LT
449{
450 int i;
451
452 for (i = 0; i < e820.nr_map; i++) {
5a3ece79 453 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
2f36fa13
TG
454 (unsigned long long) e820.map[i].addr,
455 (unsigned long long)
456 (e820.map[i].addr + e820.map[i].size));
1da177e4 457 switch (e820.map[i].type) {
2f36fa13
TG
458 case E820_RAM:
459 printk(KERN_CONT "(usable)\n");
460 break;
1da177e4 461 case E820_RESERVED:
2f36fa13
TG
462 printk(KERN_CONT "(reserved)\n");
463 break;
1da177e4 464 case E820_ACPI:
2f36fa13
TG
465 printk(KERN_CONT "(ACPI data)\n");
466 break;
1da177e4 467 case E820_NVS:
2f36fa13
TG
468 printk(KERN_CONT "(ACPI NVS)\n");
469 break;
470 default:
471 printk(KERN_CONT "type %u\n", e820.map[i].type);
472 break;
1da177e4
LT
473 }
474 }
475}
476
477/*
478 * Sanitize the BIOS e820 map.
479 *
2f36fa13 480 * Some e820 responses include overlapping entries. The following
1da177e4
LT
481 * replaces the original e820 map with a new one, removing overlaps.
482 *
483 */
2f36fa13 484static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
1da177e4
LT
485{
486 struct change_member {
487 struct e820entry *pbios; /* pointer to original bios entry */
488 unsigned long long addr; /* address for this change point */
489 };
490 static struct change_member change_point_list[2*E820MAX] __initdata;
491 static struct change_member *change_point[2*E820MAX] __initdata;
492 static struct e820entry *overlap_list[E820MAX] __initdata;
493 static struct e820entry new_bios[E820MAX] __initdata;
494 struct change_member *change_tmp;
495 unsigned long current_type, last_type;
496 unsigned long long last_addr;
497 int chgidx, still_changing;
498 int overlap_entries;
499 int new_bios_entry;
8059b2a2 500 int old_nr, new_nr, chg_nr;
1da177e4
LT
501 int i;
502
503 /*
2f36fa13
TG
504 Visually we're performing the following
505 (1,2,3,4 = memory types)...
1da177e4
LT
506
507 Sample memory map (w/overlaps):
508 ____22__________________
509 ______________________4_
510 ____1111________________
511 _44_____________________
512 11111111________________
513 ____________________33__
514 ___________44___________
515 __________33333_________
516 ______________22________
517 ___________________2222_
518 _________111111111______
519 _____________________11_
520 _________________4______
521
522 Sanitized equivalent (no overlap):
523 1_______________________
524 _44_____________________
525 ___1____________________
526 ____22__________________
527 ______11________________
528 _________1______________
529 __________3_____________
530 ___________44___________
531 _____________33_________
532 _______________2________
533 ________________1_______
534 _________________4______
535 ___________________2____
536 ____________________33__
537 ______________________4_
538 */
539
540 /* if there's only one memory region, don't bother */
541 if (*pnr_map < 2)
542 return -1;
543
544 old_nr = *pnr_map;
545
546 /* bail out if we find any unreasonable addresses in bios map */
2f36fa13 547 for (i = 0; i < old_nr; i++)
1da177e4
LT
548 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
549 return -1;
550
551 /* create pointers for initial change-point information (for sorting) */
2f36fa13 552 for (i = 0; i < 2 * old_nr; i++)
1da177e4
LT
553 change_point[i] = &change_point_list[i];
554
8059b2a2
VP
555 /* record all known change-points (starting and ending addresses),
556 omitting those that are for empty memory regions */
1da177e4 557 chgidx = 0;
2f36fa13 558 for (i = 0; i < old_nr; i++) {
8059b2a2
VP
559 if (biosmap[i].size != 0) {
560 change_point[chgidx]->addr = biosmap[i].addr;
561 change_point[chgidx++]->pbios = &biosmap[i];
2f36fa13
TG
562 change_point[chgidx]->addr = biosmap[i].addr +
563 biosmap[i].size;
8059b2a2
VP
564 change_point[chgidx++]->pbios = &biosmap[i];
565 }
1da177e4 566 }
8059b2a2 567 chg_nr = chgidx;
1da177e4
LT
568
569 /* sort change-point list by memory addresses (low -> high) */
570 still_changing = 1;
571 while (still_changing) {
572 still_changing = 0;
2f36fa13
TG
573 for (i = 1; i < chg_nr; i++) {
574 unsigned long long curaddr, lastaddr;
575 unsigned long long curpbaddr, lastpbaddr;
576
577 curaddr = change_point[i]->addr;
578 lastaddr = change_point[i - 1]->addr;
579 curpbaddr = change_point[i]->pbios->addr;
580 lastpbaddr = change_point[i - 1]->pbios->addr;
581
582 /*
583 * swap entries, when:
584 *
585 * curaddr > lastaddr or
586 * curaddr == lastaddr and curaddr == curpbaddr and
587 * lastaddr != lastpbaddr
588 */
589 if (curaddr < lastaddr ||
590 (curaddr == lastaddr && curaddr == curpbaddr &&
591 lastaddr != lastpbaddr)) {
1da177e4
LT
592 change_tmp = change_point[i];
593 change_point[i] = change_point[i-1];
594 change_point[i-1] = change_tmp;
2f36fa13 595 still_changing = 1;
1da177e4
LT
596 }
597 }
598 }
599
600 /* create a new bios memory map, removing overlaps */
2f36fa13
TG
601 overlap_entries = 0; /* number of entries in the overlap table */
602 new_bios_entry = 0; /* index for creating new bios map entries */
1da177e4
LT
603 last_type = 0; /* start with undefined memory type */
604 last_addr = 0; /* start with 0 as last starting address */
2f36fa13 605
1da177e4 606 /* loop through change-points, determining affect on the new bios map */
2f36fa13 607 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
1da177e4 608 /* keep track of all overlapping bios entries */
2f36fa13
TG
609 if (change_point[chgidx]->addr ==
610 change_point[chgidx]->pbios->addr) {
611 /*
612 * add map entry to overlap list (> 1 entry
613 * implies an overlap)
614 */
615 overlap_list[overlap_entries++] =
616 change_point[chgidx]->pbios;
617 } else {
618 /*
619 * remove entry from list (order independent,
620 * so swap with last)
621 */
622 for (i = 0; i < overlap_entries; i++) {
623 if (overlap_list[i] ==
624 change_point[chgidx]->pbios)
625 overlap_list[i] =
626 overlap_list[overlap_entries-1];
1da177e4
LT
627 }
628 overlap_entries--;
629 }
2f36fa13
TG
630 /*
631 * if there are overlapping entries, decide which
632 * "type" to use (larger value takes precedence --
633 * 1=usable, 2,3,4,4+=unusable)
634 */
1da177e4 635 current_type = 0;
2f36fa13 636 for (i = 0; i < overlap_entries; i++)
1da177e4
LT
637 if (overlap_list[i]->type > current_type)
638 current_type = overlap_list[i]->type;
2f36fa13
TG
639 /*
640 * continue building up new bios map based on this
641 * information
642 */
1da177e4
LT
643 if (current_type != last_type) {
644 if (last_type != 0) {
645 new_bios[new_bios_entry].size =
646 change_point[chgidx]->addr - last_addr;
2f36fa13
TG
647 /*
648 * move forward only if the new size
649 * was non-zero
650 */
1da177e4 651 if (new_bios[new_bios_entry].size != 0)
2f36fa13
TG
652 /*
653 * no more space left for new
654 * bios entries ?
655 */
1da177e4 656 if (++new_bios_entry >= E820MAX)
2f36fa13 657 break;
1da177e4
LT
658 }
659 if (current_type != 0) {
2f36fa13
TG
660 new_bios[new_bios_entry].addr =
661 change_point[chgidx]->addr;
1da177e4 662 new_bios[new_bios_entry].type = current_type;
2f36fa13 663 last_addr = change_point[chgidx]->addr;
1da177e4
LT
664 }
665 last_type = current_type;
666 }
667 }
2f36fa13
TG
668 /* retain count for new bios entries */
669 new_nr = new_bios_entry;
1da177e4
LT
670
671 /* copy new bios mapping into original location */
2f36fa13 672 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
1da177e4
LT
673 *pnr_map = new_nr;
674
675 return 0;
676}
677
678/*
679 * Copy the BIOS e820 map into a safe place.
680 *
681 * Sanity-check it while we're at it..
682 *
683 * If we're lucky and live on a modern system, the setup code
684 * will have given us a memory map that we can use to properly
685 * set up memory. If we aren't, we'll fake a memory map.
1da177e4 686 */
2f36fa13 687static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
1da177e4
LT
688{
689 /* Only one memory region (or negative)? Ignore it */
690 if (nr_map < 2)
691 return -1;
692
693 do {
320a6b2e
AH
694 u64 start = biosmap->addr;
695 u64 size = biosmap->size;
696 u64 end = start + size;
697 u32 type = biosmap->type;
1da177e4
LT
698
699 /* Overflow in 64 bits? Ignore the memory map. */
700 if (start > end)
701 return -1;
702
1da177e4 703 add_memory_region(start, size, type);
2f36fa13 704 } while (biosmap++, --nr_map);
1da177e4
LT
705 return 0;
706}
707
013d23e1 708static void early_panic(char *msg)
1da177e4 709{
8380aabb
AK
710 early_printk(msg);
711 panic(msg);
712}
1da177e4 713
746ef0cd
GOC
714/* We're not void only for x86 32-bit compat */
715char * __init machine_specific_memory_setup(void)
8380aabb 716{
746ef0cd 717 char *who = "BIOS-e820";
1da177e4
LT
718 /*
719 * Try to copy the BIOS-supplied E820-map.
720 *
721 * Otherwise fake a memory map; one section from 0k->640k,
722 * the next section from 1mb->appropriate_mem_k
723 */
30c82645
PA
724 sanitize_e820_map(boot_params.e820_map, &boot_params.e820_entries);
725 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0)
8380aabb 726 early_panic("Cannot find a valid memory map");
1da177e4 727 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
746ef0cd
GOC
728 e820_print_map(who);
729
730 /* In case someone cares... */
731 return who;
1da177e4
LT
732}
733
2c8c0e6b
AK
734static int __init parse_memopt(char *p)
735{
736 if (!p)
737 return -EINVAL;
738 end_user_pfn = memparse(p, &p);
2f36fa13 739 end_user_pfn >>= PAGE_SHIFT;
2c8c0e6b 740 return 0;
2f36fa13 741}
2c8c0e6b 742early_param("mem", parse_memopt);
1da177e4 743
2c8c0e6b 744static int userdef __initdata;
1da177e4 745
2c8c0e6b 746static int __init parse_memmap_opt(char *p)
69cda7b1 747{
2c8c0e6b 748 char *oldp;
69cda7b1
AM
749 unsigned long long start_at, mem_size;
750
2c8c0e6b
AK
751 if (!strcmp(p, "exactmap")) {
752#ifdef CONFIG_CRASH_DUMP
2f36fa13
TG
753 /*
754 * If we are doing a crash dump, we still need to know
755 * the real mem size before original memory map is
2c8c0e6b
AK
756 * reset.
757 */
15803a43 758 e820_register_active_regions(0, 0, -1UL);
2c8c0e6b 759 saved_max_pfn = e820_end_of_ram();
15803a43 760 remove_all_active_ranges();
2c8c0e6b
AK
761#endif
762 end_pfn_map = 0;
763 e820.nr_map = 0;
764 userdef = 1;
765 return 0;
766 }
767
768 oldp = p;
769 mem_size = memparse(p, &p);
770 if (p == oldp)
771 return -EINVAL;
b3ca74a2
VB
772
773 userdef = 1;
69cda7b1 774 if (*p == '@') {
2c8c0e6b 775 start_at = memparse(p+1, &p);
69cda7b1
AM
776 add_memory_region(start_at, mem_size, E820_RAM);
777 } else if (*p == '#') {
2c8c0e6b 778 start_at = memparse(p+1, &p);
69cda7b1
AM
779 add_memory_region(start_at, mem_size, E820_ACPI);
780 } else if (*p == '$') {
2c8c0e6b 781 start_at = memparse(p+1, &p);
69cda7b1
AM
782 add_memory_region(start_at, mem_size, E820_RESERVED);
783 } else {
784 end_user_pfn = (mem_size >> PAGE_SHIFT);
785 }
2c8c0e6b
AK
786 return *p == '\0' ? 0 : -EINVAL;
787}
788early_param("memmap", parse_memmap_opt);
789
43999d9e 790void __init finish_e820_parsing(void)
2c8c0e6b
AK
791{
792 if (userdef) {
b3ca74a2
VB
793 char nr = e820.nr_map;
794
795 if (sanitize_e820_map(e820.map, &nr) < 0)
796 early_panic("Invalid user supplied memory map");
797 e820.nr_map = nr;
798
2c8c0e6b
AK
799 printk(KERN_INFO "user-defined physical RAM map:\n");
800 e820_print_map("user");
801 }
69cda7b1
AM
802}
803
5dca6a1b
YL
804void __init update_memory_range(u64 start, u64 size, unsigned old_type,
805 unsigned new_type)
806{
807 int i;
808
809 BUG_ON(old_type == new_type);
810
811 for (i = 0; i < e820.nr_map; i++) {
812 struct e820entry *ei = &e820.map[i];
813 u64 final_start, final_end;
814 if (ei->type != old_type)
815 continue;
816 /* totally covered? */
817 if (ei->addr >= start && ei->size <= size) {
818 ei->type = new_type;
819 continue;
820 }
821 /* partially covered */
822 final_start = max(start, ei->addr);
823 final_end = min(start + size, ei->addr + ei->size);
824 if (final_start >= final_end)
825 continue;
826 add_memory_region(final_start, final_end - final_start,
827 new_type);
828 }
829}
830
aaf23042
YL
831void __init update_e820(void)
832{
833 u8 nr_map;
834
835 nr_map = e820.nr_map;
836 if (sanitize_e820_map(e820.map, &nr_map))
837 return;
838 e820.nr_map = nr_map;
839 printk(KERN_INFO "modified physical RAM map:\n");
840 e820_print_map("modified");
841}
842
a1e97782 843unsigned long pci_mem_start = 0xaeedbabe;
2ee60e17 844EXPORT_SYMBOL(pci_mem_start);
a1e97782
AK
845
846/*
847 * Search for the biggest gap in the low 32 bits of the e820
848 * memory space. We pass this space to PCI to assign MMIO resources
849 * for hotplug or unconfigured devices in.
850 * Hopefully the BIOS let enough space left.
851 */
852__init void e820_setup_gap(void)
853{
f0eca962 854 unsigned long gapstart, gapsize, round;
a1e97782
AK
855 unsigned long last;
856 int i;
857 int found = 0;
858
859 last = 0x100000000ull;
860 gapstart = 0x10000000;
861 gapsize = 0x400000;
862 i = e820.nr_map;
863 while (--i >= 0) {
864 unsigned long long start = e820.map[i].addr;
865 unsigned long long end = start + e820.map[i].size;
866
867 /*
868 * Since "last" is at most 4GB, we know we'll
869 * fit in 32 bits if this condition is true
870 */
871 if (last > end) {
872 unsigned long gap = last - end;
873
874 if (gap > gapsize) {
875 gapsize = gap;
876 gapstart = end;
877 found = 1;
878 }
879 }
880 if (start < last)
881 last = start;
882 }
883
884 if (!found) {
885 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
2f36fa13
TG
886 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
887 "address range\n"
888 KERN_ERR "PCI: Unassigned devices with 32bit resource "
889 "registers may break!\n");
a1e97782
AK
890 }
891
892 /*
f0eca962
DR
893 * See how much we want to round up: start off with
894 * rounding to the next 1MB area.
a1e97782 895 */
f0eca962
DR
896 round = 0x100000;
897 while ((gapsize >> 4) > round)
898 round += round;
899 /* Fun with two's complement */
900 pci_mem_start = (gapstart + round) & -round;
a1e97782 901
2f36fa13
TG
902 printk(KERN_INFO
903 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
904 pci_mem_start, gapstart, gapsize);
a1e97782 905}
e820482c
KA
906
907int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
908{
909 int i;
910
911 if (slot < 0 || slot >= e820.nr_map)
912 return -1;
913 for (i = slot; i < e820.nr_map; i++) {
914 if (e820.map[i].type != E820_RAM)
915 continue;
916 break;
917 }
918 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
919 return -1;
920 *addr = e820.map[i].addr;
921 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
922 max_pfn << PAGE_SHIFT) - *addr;
923 return i + 1;
924}