intel-iommu: Unify hardware and software passthrough support
[deliverable/linux.git] / arch / x86 / kernel / e820.c
CommitLineData
b79cd8f1
YL
1/*
2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
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 *
10 */
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>
17#include <linux/kexec.h>
18#include <linux/module.h>
19#include <linux/mm.h>
20#include <linux/pfn.h>
bf62f398 21#include <linux/suspend.h>
5dfcf14d 22#include <linux/firmware-map.h>
b79cd8f1
YL
23
24#include <asm/pgtable.h>
25#include <asm/page.h>
26#include <asm/e820.h>
a4c81cf6 27#include <asm/proto.h>
b79cd8f1 28#include <asm/setup.h>
a4c81cf6 29#include <asm/trampoline.h>
b79cd8f1 30
5dfcf14d
BW
31/*
32 * The e820 map is the map that gets modified e.g. with command line parameters
33 * and that is also registered with modifications in the kernel resource tree
34 * with the iomem_resource as parent.
35 *
36 * The e820_saved is directly saved after the BIOS-provided memory map is
37 * copied. It doesn't get modified afterwards. It's registered for the
38 * /sys/firmware/memmap interface.
39 *
40 * That memory map is not modified and is used as base for kexec. The kexec'd
41 * kernel should get the same memory map as the firmware provides. Then the
42 * user can e.g. boot the original kernel with mem=1G while still booting the
43 * next kernel with full memory.
44 */
b79cd8f1 45struct e820map e820;
5dfcf14d 46struct e820map e820_saved;
b79cd8f1
YL
47
48/* For PCI or other memory-mapped resources */
49unsigned long pci_mem_start = 0xaeedbabe;
50#ifdef CONFIG_PCI
51EXPORT_SYMBOL(pci_mem_start);
52#endif
53
54/*
55 * This function checks if any part of the range <start,end> is mapped
56 * with type.
57 */
58int
59e820_any_mapped(u64 start, u64 end, unsigned type)
60{
61 int i;
62
63 for (i = 0; i < e820.nr_map; i++) {
64 struct e820entry *ei = &e820.map[i];
65
66 if (type && ei->type != type)
67 continue;
68 if (ei->addr >= end || ei->addr + ei->size <= start)
69 continue;
70 return 1;
71 }
72 return 0;
73}
74EXPORT_SYMBOL_GPL(e820_any_mapped);
75
76/*
77 * This function checks if the entire range <start,end> is mapped with type.
78 *
79 * Note: this function only works correct if the e820 table is sorted and
80 * not-overlapping, which is the case
81 */
82int __init e820_all_mapped(u64 start, u64 end, unsigned type)
83{
84 int i;
85
86 for (i = 0; i < e820.nr_map; i++) {
87 struct e820entry *ei = &e820.map[i];
88
89 if (type && ei->type != type)
90 continue;
91 /* is the region (part) in overlap with the current region ?*/
92 if (ei->addr >= end || ei->addr + ei->size <= start)
93 continue;
94
95 /* if the region is at the beginning of <start,end> we move
96 * start to the end of the region since it's ok until there
97 */
98 if (ei->addr <= start)
99 start = ei->addr + ei->size;
100 /*
101 * if start is now at or beyond end, we're done, full
102 * coverage
103 */
104 if (start >= end)
105 return 1;
106 }
107 return 0;
108}
109
110/*
111 * Add a memory region to the kernel e820 map.
112 */
773e673d
YL
113static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size,
114 int type)
b79cd8f1 115{
773e673d 116 int x = e820x->nr_map;
b79cd8f1 117
773e673d 118 if (x == ARRAY_SIZE(e820x->map)) {
b79cd8f1
YL
119 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
120 return;
121 }
122
773e673d
YL
123 e820x->map[x].addr = start;
124 e820x->map[x].size = size;
125 e820x->map[x].type = type;
126 e820x->nr_map++;
127}
128
129void __init e820_add_region(u64 start, u64 size, int type)
130{
131 __e820_add_region(&e820, start, size, type);
b79cd8f1
YL
132}
133
c61cf4cf
YL
134static void __init e820_print_type(u32 type)
135{
136 switch (type) {
137 case E820_RAM:
138 case E820_RESERVED_KERN:
139 printk(KERN_CONT "(usable)");
140 break;
141 case E820_RESERVED:
142 printk(KERN_CONT "(reserved)");
143 break;
144 case E820_ACPI:
145 printk(KERN_CONT "(ACPI data)");
146 break;
147 case E820_NVS:
148 printk(KERN_CONT "(ACPI NVS)");
149 break;
150 case E820_UNUSABLE:
151 printk(KERN_CONT "(unusable)");
152 break;
153 default:
154 printk(KERN_CONT "type %u", type);
155 break;
156 }
157}
158
b79cd8f1
YL
159void __init e820_print_map(char *who)
160{
161 int i;
162
163 for (i = 0; i < e820.nr_map; i++) {
164 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
165 (unsigned long long) e820.map[i].addr,
166 (unsigned long long)
167 (e820.map[i].addr + e820.map[i].size));
c61cf4cf
YL
168 e820_print_type(e820.map[i].type);
169 printk(KERN_CONT "\n");
b79cd8f1
YL
170 }
171}
172
173/*
174 * Sanitize the BIOS e820 map.
175 *
176 * Some e820 responses include overlapping entries. The following
5b7eb2e9
PJ
177 * replaces the original e820 map with a new one, removing overlaps,
178 * and resolving conflicting memory types in favor of highest
179 * numbered type.
b79cd8f1 180 *
5b7eb2e9
PJ
181 * The input parameter biosmap points to an array of 'struct
182 * e820entry' which on entry has elements in the range [0, *pnr_map)
183 * valid, and which has space for up to max_nr_map entries.
184 * On return, the resulting sanitized e820 map entries will be in
185 * overwritten in the same location, starting at biosmap.
186 *
187 * The integer pointed to by pnr_map must be valid on entry (the
188 * current number of valid entries located at biosmap) and will
189 * be updated on return, with the new number of valid entries
190 * (something no more than max_nr_map.)
191 *
192 * The return value from sanitize_e820_map() is zero if it
193 * successfully 'sanitized' the map entries passed in, and is -1
194 * if it did nothing, which can happen if either of (1) it was
195 * only passed one map entry, or (2) any of the input map entries
196 * were invalid (start + size < start, meaning that the size was
197 * so big the described memory range wrapped around through zero.)
198 *
199 * Visually we're performing the following
200 * (1,2,3,4 = memory types)...
201 *
202 * Sample memory map (w/overlaps):
203 * ____22__________________
204 * ______________________4_
205 * ____1111________________
206 * _44_____________________
207 * 11111111________________
208 * ____________________33__
209 * ___________44___________
210 * __________33333_________
211 * ______________22________
212 * ___________________2222_
213 * _________111111111______
214 * _____________________11_
215 * _________________4______
216 *
217 * Sanitized equivalent (no overlap):
218 * 1_______________________
219 * _44_____________________
220 * ___1____________________
221 * ____22__________________
222 * ______11________________
223 * _________1______________
224 * __________3_____________
225 * ___________44___________
226 * _____________33_________
227 * _______________2________
228 * ________________1_______
229 * _________________4______
230 * ___________________2____
231 * ____________________33__
232 * ______________________4_
b79cd8f1 233 */
5b7eb2e9 234
c3965bd1 235int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
ba639039 236 u32 *pnr_map)
b79cd8f1
YL
237{
238 struct change_member {
239 struct e820entry *pbios; /* pointer to original bios entry */
240 unsigned long long addr; /* address for this change point */
241 };
157fabf0
PJ
242 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
243 static struct change_member *change_point[2*E820_X_MAX] __initdata;
244 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
245 static struct e820entry new_bios[E820_X_MAX] __initdata;
b79cd8f1
YL
246 struct change_member *change_tmp;
247 unsigned long current_type, last_type;
248 unsigned long long last_addr;
249 int chgidx, still_changing;
250 int overlap_entries;
251 int new_bios_entry;
252 int old_nr, new_nr, chg_nr;
253 int i;
254
b79cd8f1
YL
255 /* if there's only one memory region, don't bother */
256 if (*pnr_map < 2)
257 return -1;
258
259 old_nr = *pnr_map;
6e9bcc79 260 BUG_ON(old_nr > max_nr_map);
b79cd8f1
YL
261
262 /* bail out if we find any unreasonable addresses in bios map */
263 for (i = 0; i < old_nr; i++)
264 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
265 return -1;
266
267 /* create pointers for initial change-point information (for sorting) */
268 for (i = 0; i < 2 * old_nr; i++)
269 change_point[i] = &change_point_list[i];
270
271 /* record all known change-points (starting and ending addresses),
272 omitting those that are for empty memory regions */
273 chgidx = 0;
274 for (i = 0; i < old_nr; i++) {
275 if (biosmap[i].size != 0) {
276 change_point[chgidx]->addr = biosmap[i].addr;
277 change_point[chgidx++]->pbios = &biosmap[i];
278 change_point[chgidx]->addr = biosmap[i].addr +
279 biosmap[i].size;
280 change_point[chgidx++]->pbios = &biosmap[i];
281 }
282 }
283 chg_nr = chgidx;
284
285 /* sort change-point list by memory addresses (low -> high) */
286 still_changing = 1;
287 while (still_changing) {
288 still_changing = 0;
289 for (i = 1; i < chg_nr; i++) {
290 unsigned long long curaddr, lastaddr;
291 unsigned long long curpbaddr, lastpbaddr;
292
293 curaddr = change_point[i]->addr;
294 lastaddr = change_point[i - 1]->addr;
295 curpbaddr = change_point[i]->pbios->addr;
296 lastpbaddr = change_point[i - 1]->pbios->addr;
297
298 /*
299 * swap entries, when:
300 *
301 * curaddr > lastaddr or
302 * curaddr == lastaddr and curaddr == curpbaddr and
303 * lastaddr != lastpbaddr
304 */
305 if (curaddr < lastaddr ||
306 (curaddr == lastaddr && curaddr == curpbaddr &&
307 lastaddr != lastpbaddr)) {
308 change_tmp = change_point[i];
309 change_point[i] = change_point[i-1];
310 change_point[i-1] = change_tmp;
311 still_changing = 1;
312 }
313 }
314 }
315
316 /* create a new bios memory map, removing overlaps */
317 overlap_entries = 0; /* number of entries in the overlap table */
318 new_bios_entry = 0; /* index for creating new bios map entries */
319 last_type = 0; /* start with undefined memory type */
320 last_addr = 0; /* start with 0 as last starting address */
321
322 /* loop through change-points, determining affect on the new bios map */
323 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
324 /* keep track of all overlapping bios entries */
325 if (change_point[chgidx]->addr ==
326 change_point[chgidx]->pbios->addr) {
327 /*
328 * add map entry to overlap list (> 1 entry
329 * implies an overlap)
330 */
331 overlap_list[overlap_entries++] =
332 change_point[chgidx]->pbios;
333 } else {
334 /*
335 * remove entry from list (order independent,
336 * so swap with last)
337 */
338 for (i = 0; i < overlap_entries; i++) {
339 if (overlap_list[i] ==
340 change_point[chgidx]->pbios)
341 overlap_list[i] =
342 overlap_list[overlap_entries-1];
343 }
344 overlap_entries--;
345 }
346 /*
347 * if there are overlapping entries, decide which
348 * "type" to use (larger value takes precedence --
349 * 1=usable, 2,3,4,4+=unusable)
350 */
351 current_type = 0;
352 for (i = 0; i < overlap_entries; i++)
353 if (overlap_list[i]->type > current_type)
354 current_type = overlap_list[i]->type;
355 /*
356 * continue building up new bios map based on this
357 * information
358 */
359 if (current_type != last_type) {
360 if (last_type != 0) {
361 new_bios[new_bios_entry].size =
362 change_point[chgidx]->addr - last_addr;
363 /*
364 * move forward only if the new size
365 * was non-zero
366 */
367 if (new_bios[new_bios_entry].size != 0)
368 /*
369 * no more space left for new
370 * bios entries ?
371 */
c3965bd1 372 if (++new_bios_entry >= max_nr_map)
b79cd8f1
YL
373 break;
374 }
375 if (current_type != 0) {
376 new_bios[new_bios_entry].addr =
377 change_point[chgidx]->addr;
378 new_bios[new_bios_entry].type = current_type;
379 last_addr = change_point[chgidx]->addr;
380 }
381 last_type = current_type;
382 }
383 }
384 /* retain count for new bios entries */
385 new_nr = new_bios_entry;
386
387 /* copy new bios mapping into original location */
388 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
389 *pnr_map = new_nr;
390
391 return 0;
392}
393
dc8e8120 394static int __init __append_e820_map(struct e820entry *biosmap, int nr_map)
8c5beb50
HY
395{
396 while (nr_map) {
397 u64 start = biosmap->addr;
398 u64 size = biosmap->size;
399 u64 end = start + size;
400 u32 type = biosmap->type;
401
402 /* Overflow in 64 bits? Ignore the memory map. */
403 if (start > end)
404 return -1;
405
406 e820_add_region(start, size, type);
407
408 biosmap++;
409 nr_map--;
410 }
411 return 0;
412}
413
b79cd8f1
YL
414/*
415 * Copy the BIOS e820 map into a safe place.
416 *
417 * Sanity-check it while we're at it..
418 *
419 * If we're lucky and live on a modern system, the setup code
420 * will have given us a memory map that we can use to properly
421 * set up memory. If we aren't, we'll fake a memory map.
422 */
dc8e8120 423static int __init append_e820_map(struct e820entry *biosmap, int nr_map)
b79cd8f1
YL
424{
425 /* Only one memory region (or negative)? Ignore it */
426 if (nr_map < 2)
427 return -1;
428
dc8e8120 429 return __append_e820_map(biosmap, nr_map);
b79cd8f1
YL
430}
431
773e673d 432static u64 __init __e820_update_range(struct e820map *e820x, u64 start,
fc9036ea
YL
433 u64 size, unsigned old_type,
434 unsigned new_type)
b79cd8f1 435{
78a8b35b 436 u64 end;
773e673d 437 unsigned int i;
b79cd8f1
YL
438 u64 real_updated_size = 0;
439
440 BUG_ON(old_type == new_type);
441
232b957a
YL
442 if (size > (ULLONG_MAX - start))
443 size = ULLONG_MAX - start;
444
78a8b35b 445 end = start + size;
c61cf4cf
YL
446 printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ",
447 (unsigned long long) start,
448 (unsigned long long) end);
449 e820_print_type(old_type);
450 printk(KERN_CONT " ==> ");
451 e820_print_type(new_type);
452 printk(KERN_CONT "\n");
453
5c0e6f03 454 for (i = 0; i < e820x->nr_map; i++) {
fc9036ea 455 struct e820entry *ei = &e820x->map[i];
b79cd8f1 456 u64 final_start, final_end;
78a8b35b
YL
457 u64 ei_end;
458
b79cd8f1
YL
459 if (ei->type != old_type)
460 continue;
78a8b35b
YL
461
462 ei_end = ei->addr + ei->size;
463 /* totally covered by new range? */
464 if (ei->addr >= start && ei_end <= end) {
b79cd8f1
YL
465 ei->type = new_type;
466 real_updated_size += ei->size;
467 continue;
468 }
78a8b35b
YL
469
470 /* new range is totally covered? */
471 if (ei->addr < start && ei_end > end) {
472 __e820_add_region(e820x, start, size, new_type);
473 __e820_add_region(e820x, end, ei_end - end, ei->type);
474 ei->size = start - ei->addr;
475 real_updated_size += size;
476 continue;
477 }
478
b79cd8f1
YL
479 /* partially covered */
480 final_start = max(start, ei->addr);
78a8b35b 481 final_end = min(end, ei_end);
b79cd8f1
YL
482 if (final_start >= final_end)
483 continue;
5c0e6f03 484
773e673d
YL
485 __e820_add_region(e820x, final_start, final_end - final_start,
486 new_type);
5c0e6f03 487
b79cd8f1 488 real_updated_size += final_end - final_start;
976dd4dc 489
773e673d
YL
490 /*
491 * left range could be head or tail, so need to update
492 * size at first.
493 */
494 ei->size -= final_end - final_start;
976dd4dc
YL
495 if (ei->addr < final_start)
496 continue;
497 ei->addr = final_end;
b79cd8f1
YL
498 }
499 return real_updated_size;
500}
501
fc9036ea
YL
502u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
503 unsigned new_type)
504{
773e673d 505 return __e820_update_range(&e820, start, size, old_type, new_type);
fc9036ea
YL
506}
507
508static u64 __init e820_update_range_saved(u64 start, u64 size,
509 unsigned old_type, unsigned new_type)
510{
773e673d 511 return __e820_update_range(&e820_saved, start, size, old_type,
fc9036ea
YL
512 new_type);
513}
514
7a1fd986
YL
515/* make e820 not cover the range */
516u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
517 int checktype)
518{
519 int i;
520 u64 real_removed_size = 0;
521
232b957a
YL
522 if (size > (ULLONG_MAX - start))
523 size = ULLONG_MAX - start;
524
7a1fd986
YL
525 for (i = 0; i < e820.nr_map; i++) {
526 struct e820entry *ei = &e820.map[i];
527 u64 final_start, final_end;
528
529 if (checktype && ei->type != old_type)
530 continue;
531 /* totally covered? */
532 if (ei->addr >= start &&
533 (ei->addr + ei->size) <= (start + size)) {
534 real_removed_size += ei->size;
535 memset(ei, 0, sizeof(struct e820entry));
536 continue;
537 }
538 /* partially covered */
539 final_start = max(start, ei->addr);
540 final_end = min(start + size, ei->addr + ei->size);
541 if (final_start >= final_end)
542 continue;
543 real_removed_size += final_end - final_start;
544
545 ei->size -= final_end - final_start;
546 if (ei->addr < final_start)
547 continue;
548 ei->addr = final_end;
549 }
550 return real_removed_size;
551}
552
b79cd8f1
YL
553void __init update_e820(void)
554{
ba639039 555 u32 nr_map;
b79cd8f1
YL
556
557 nr_map = e820.nr_map;
c3965bd1 558 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
b79cd8f1
YL
559 return;
560 e820.nr_map = nr_map;
561 printk(KERN_INFO "modified physical RAM map:\n");
562 e820_print_map("modified");
563}
fc9036ea
YL
564static void __init update_e820_saved(void)
565{
ba639039 566 u32 nr_map;
fc9036ea
YL
567
568 nr_map = e820_saved.nr_map;
569 if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map))
570 return;
571 e820_saved.nr_map = nr_map;
572}
fd6493e1 573#define MAX_GAP_END 0x100000000ull
b79cd8f1 574/*
fd6493e1 575 * Search for a gap in the e820 memory space from start_addr to end_addr.
b79cd8f1 576 */
3381959d 577__init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize,
fd6493e1 578 unsigned long start_addr, unsigned long long end_addr)
b79cd8f1 579{
fd6493e1 580 unsigned long long last;
3381959d 581 int i = e820.nr_map;
b79cd8f1
YL
582 int found = 0;
583
fd6493e1
AK
584 last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END;
585
b79cd8f1
YL
586 while (--i >= 0) {
587 unsigned long long start = e820.map[i].addr;
588 unsigned long long end = start + e820.map[i].size;
589
3381959d
AK
590 if (end < start_addr)
591 continue;
592
b79cd8f1
YL
593 /*
594 * Since "last" is at most 4GB, we know we'll
595 * fit in 32 bits if this condition is true
596 */
597 if (last > end) {
598 unsigned long gap = last - end;
599
3381959d
AK
600 if (gap >= *gapsize) {
601 *gapsize = gap;
602 *gapstart = end;
b79cd8f1
YL
603 found = 1;
604 }
605 }
606 if (start < last)
607 last = start;
608 }
3381959d
AK
609 return found;
610}
611
612/*
613 * Search for the biggest gap in the low 32 bits of the e820
614 * memory space. We pass this space to PCI to assign MMIO resources
615 * for hotplug or unconfigured devices in.
616 * Hopefully the BIOS let enough space left.
617 */
618__init void e820_setup_gap(void)
619{
5d423ccd 620 unsigned long gapstart, gapsize;
3381959d
AK
621 int found;
622
623 gapstart = 0x10000000;
624 gapsize = 0x400000;
fd6493e1 625 found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END);
b79cd8f1
YL
626
627#ifdef CONFIG_X86_64
628 if (!found) {
c987d12f 629 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024;
b79cd8f1
YL
630 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
631 "address range\n"
632 KERN_ERR "PCI: Unassigned devices with 32bit resource "
633 "registers may break!\n");
634 }
635#endif
636
637 /*
5d423ccd 638 * e820_reserve_resources_late protect stolen RAM already
b79cd8f1 639 */
5d423ccd 640 pci_mem_start = gapstart;
b79cd8f1
YL
641
642 printk(KERN_INFO
643 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
644 pci_mem_start, gapstart, gapsize);
645}
646
8c5beb50
HY
647/**
648 * Because of the size limitation of struct boot_params, only first
649 * 128 E820 memory entries are passed to kernel via
650 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
651 * linked list of struct setup_data, which is parsed here.
652 */
653void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
654{
655 u32 map_len;
656 int entries;
657 struct e820entry *extmap;
658
659 entries = sdata->len / sizeof(struct e820entry);
660 map_len = sdata->len + sizeof(struct setup_data);
661 if (map_len > PAGE_SIZE)
662 sdata = early_ioremap(pa_data, map_len);
663 extmap = (struct e820entry *)(sdata->data);
dc8e8120 664 __append_e820_map(extmap, entries);
8c5beb50
HY
665 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
666 if (map_len > PAGE_SIZE)
667 early_iounmap(sdata, map_len);
668 printk(KERN_INFO "extended physical RAM map:\n");
669 e820_print_map("extended");
670}
671
bf62f398
YL
672#if defined(CONFIG_X86_64) || \
673 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
674/**
675 * Find the ranges of physical addresses that do not correspond to
676 * e820 RAM areas and mark the corresponding pages as nosave for
677 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
678 *
679 * This function requires the e820 map to be sorted and without any
680 * overlapping entries and assumes the first e820 area to be RAM.
681 */
682void __init e820_mark_nosave_regions(unsigned long limit_pfn)
683{
684 int i;
685 unsigned long pfn;
686
687 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
688 for (i = 1; i < e820.nr_map; i++) {
689 struct e820entry *ei = &e820.map[i];
690
691 if (pfn < PFN_UP(ei->addr))
692 register_nosave_region(pfn, PFN_UP(ei->addr));
693
694 pfn = PFN_DOWN(ei->addr + ei->size);
28bb2237 695 if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN)
bf62f398
YL
696 register_nosave_region(PFN_UP(ei->addr), pfn);
697
698 if (pfn >= limit_pfn)
699 break;
700 }
701}
702#endif
a4c81cf6 703
b69edc76
RW
704#ifdef CONFIG_HIBERNATION
705/**
706 * Mark ACPI NVS memory region, so that we can save/restore it during
707 * hibernation and the subsequent resume.
708 */
709static int __init e820_mark_nvs_memory(void)
710{
711 int i;
712
713 for (i = 0; i < e820.nr_map; i++) {
714 struct e820entry *ei = &e820.map[i];
715
716 if (ei->type == E820_NVS)
717 hibernate_nvs_register(ei->addr, ei->size);
718 }
719
720 return 0;
721}
722core_initcall(e820_mark_nvs_memory);
723#endif
724
a4c81cf6
YL
725/*
726 * Early reserved memory areas.
727 */
728#define MAX_EARLY_RES 20
729
730struct early_res {
731 u64 start, end;
732 char name[16];
c4ba1320 733 char overlap_ok;
a4c81cf6
YL
734};
735static struct early_res early_res[MAX_EARLY_RES] __initdata = {
736 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
a4c81cf6
YL
737 {}
738};
739
d3fbe5ea 740static int __init find_overlapped_early(u64 start, u64 end)
a4c81cf6
YL
741{
742 int i;
743 struct early_res *r;
d3fbe5ea 744
a4c81cf6
YL
745 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
746 r = &early_res[i];
747 if (end > r->start && start < r->end)
d3fbe5ea 748 break;
a4c81cf6 749 }
d3fbe5ea
HY
750
751 return i;
752}
753
c4ba1320
PJ
754/*
755 * Drop the i-th range from the early reservation map,
756 * by copying any higher ranges down one over it, and
757 * clearing what had been the last slot.
758 */
759static void __init drop_range(int i)
760{
761 int j;
762
763 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
764 ;
765
766 memmove(&early_res[i], &early_res[i + 1],
767 (j - 1 - i) * sizeof(struct early_res));
768
769 early_res[j - 1].end = 0;
770}
771
772/*
773 * Split any existing ranges that:
774 * 1) are marked 'overlap_ok', and
775 * 2) overlap with the stated range [start, end)
776 * into whatever portion (if any) of the existing range is entirely
777 * below or entirely above the stated range. Drop the portion
778 * of the existing range that overlaps with the stated range,
779 * which will allow the caller of this routine to then add that
780 * stated range without conflicting with any existing range.
781 */
782static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
783{
784 int i;
785 struct early_res *r;
786 u64 lower_start, lower_end;
787 u64 upper_start, upper_end;
788 char name[16];
789
790 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
791 r = &early_res[i];
792
793 /* Continue past non-overlapping ranges */
794 if (end <= r->start || start >= r->end)
795 continue;
796
797 /*
798 * Leave non-ok overlaps as is; let caller
799 * panic "Overlapping early reservations"
800 * when it hits this overlap.
801 */
802 if (!r->overlap_ok)
803 return;
804
805 /*
806 * We have an ok overlap. We will drop it from the early
807 * reservation map, and add back in any non-overlapping
808 * portions (lower or upper) as separate, overlap_ok,
809 * non-overlapping ranges.
810 */
811
812 /* 1. Note any non-overlapping (lower or upper) ranges. */
813 strncpy(name, r->name, sizeof(name) - 1);
814
815 lower_start = lower_end = 0;
816 upper_start = upper_end = 0;
817 if (r->start < start) {
818 lower_start = r->start;
819 lower_end = start;
820 }
821 if (r->end > end) {
822 upper_start = end;
823 upper_end = r->end;
824 }
825
826 /* 2. Drop the original ok overlapping range */
827 drop_range(i);
828
829 i--; /* resume for-loop on copied down entry */
830
831 /* 3. Add back in any non-overlapping ranges. */
832 if (lower_end)
833 reserve_early_overlap_ok(lower_start, lower_end, name);
834 if (upper_end)
835 reserve_early_overlap_ok(upper_start, upper_end, name);
836 }
837}
838
839static void __init __reserve_early(u64 start, u64 end, char *name,
840 int overlap_ok)
d3fbe5ea
HY
841{
842 int i;
843 struct early_res *r;
844
845 i = find_overlapped_early(start, end);
a4c81cf6
YL
846 if (i >= MAX_EARLY_RES)
847 panic("Too many early reservations");
848 r = &early_res[i];
d3fbe5ea
HY
849 if (r->end)
850 panic("Overlapping early reservations "
851 "%llx-%llx %s to %llx-%llx %s\n",
852 start, end - 1, name?name:"", r->start,
853 r->end - 1, r->name);
a4c81cf6
YL
854 r->start = start;
855 r->end = end;
c4ba1320 856 r->overlap_ok = overlap_ok;
a4c81cf6
YL
857 if (name)
858 strncpy(r->name, name, sizeof(r->name) - 1);
859}
860
c4ba1320
PJ
861/*
862 * A few early reservtations come here.
863 *
864 * The 'overlap_ok' in the name of this routine does -not- mean it
865 * is ok for these reservations to overlap an earlier reservation.
866 * Rather it means that it is ok for subsequent reservations to
867 * overlap this one.
868 *
869 * Use this entry point to reserve early ranges when you are doing
870 * so out of "Paranoia", reserving perhaps more memory than you need,
871 * just in case, and don't mind a subsequent overlapping reservation
872 * that is known to be needed.
873 *
874 * The drop_overlaps_that_are_ok() call here isn't really needed.
875 * It would be needed if we had two colliding 'overlap_ok'
876 * reservations, so that the second such would not panic on the
877 * overlap with the first. We don't have any such as of this
878 * writing, but might as well tolerate such if it happens in
879 * the future.
880 */
881void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
882{
883 drop_overlaps_that_are_ok(start, end);
884 __reserve_early(start, end, name, 1);
885}
886
887/*
888 * Most early reservations come here.
889 *
890 * We first have drop_overlaps_that_are_ok() drop any pre-existing
891 * 'overlap_ok' ranges, so that we can then reserve this memory
892 * range without risk of panic'ing on an overlapping overlap_ok
893 * early reservation.
894 */
895void __init reserve_early(u64 start, u64 end, char *name)
896{
46cb27f5
YL
897 if (start >= end)
898 return;
899
c4ba1320
PJ
900 drop_overlaps_that_are_ok(start, end);
901 __reserve_early(start, end, name, 0);
902}
903
a4c81cf6
YL
904void __init free_early(u64 start, u64 end)
905{
906 struct early_res *r;
c4ba1320 907 int i;
a4c81cf6 908
d3fbe5ea
HY
909 i = find_overlapped_early(start, end);
910 r = &early_res[i];
911 if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
a4c81cf6 912 panic("free_early on not reserved area: %llx-%llx!",
d3fbe5ea 913 start, end - 1);
a4c81cf6 914
c4ba1320 915 drop_range(i);
a4c81cf6
YL
916}
917
918void __init early_res_to_bootmem(u64 start, u64 end)
919{
ab67715c 920 int i, count;
a4c81cf6 921 u64 final_start, final_end;
ab67715c
YL
922
923 count = 0;
924 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++)
925 count++;
926
5f1f2b3d
YL
927 printk(KERN_INFO "(%d early reservations) ==> bootmem [%010llx - %010llx]\n",
928 count, start, end);
ab67715c 929 for (i = 0; i < count; i++) {
a4c81cf6 930 struct early_res *r = &early_res[i];
4fcc545a 931 printk(KERN_INFO " #%d [%010llx - %010llx] %16s", i,
ab67715c 932 r->start, r->end, r->name);
a4c81cf6
YL
933 final_start = max(start, r->start);
934 final_end = min(end, r->end);
ab67715c
YL
935 if (final_start >= final_end) {
936 printk(KERN_CONT "\n");
a4c81cf6 937 continue;
ab67715c 938 }
4fcc545a 939 printk(KERN_CONT " ==> [%010llx - %010llx]\n",
ab67715c 940 final_start, final_end);
d2dbf343 941 reserve_bootmem_generic(final_start, final_end - final_start,
a4c81cf6 942 BOOTMEM_DEFAULT);
a4c81cf6
YL
943 }
944}
945
946/* Check for already reserved areas */
947static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
948{
949 int i;
d3fbe5ea 950 u64 addr = *addrp;
a4c81cf6 951 int changed = 0;
d3fbe5ea 952 struct early_res *r;
a4c81cf6 953again:
d3fbe5ea
HY
954 i = find_overlapped_early(addr, addr + size);
955 r = &early_res[i];
956 if (i < MAX_EARLY_RES && r->end) {
957 *addrp = addr = round_up(r->end, align);
958 changed = 1;
959 goto again;
a4c81cf6
YL
960 }
961 return changed;
962}
963
964/* Check for already reserved areas */
965static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
966{
967 int i;
968 u64 addr = *addrp, last;
969 u64 size = *sizep;
970 int changed = 0;
971again:
972 last = addr + size;
973 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
974 struct early_res *r = &early_res[i];
975 if (last > r->start && addr < r->start) {
976 size = r->start - addr;
977 changed = 1;
978 goto again;
979 }
980 if (last > r->end && addr < r->end) {
981 addr = round_up(r->end, align);
982 size = last - addr;
983 changed = 1;
984 goto again;
985 }
986 if (last <= r->end && addr >= r->start) {
987 (*sizep)++;
988 return 0;
989 }
990 }
991 if (changed) {
992 *addrp = addr;
993 *sizep = size;
994 }
995 return changed;
996}
997
998/*
999 * Find a free area with specified alignment in a specific range.
1000 */
1001u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
1002{
1003 int i;
1004
1005 for (i = 0; i < e820.nr_map; i++) {
1006 struct e820entry *ei = &e820.map[i];
1007 u64 addr, last;
1008 u64 ei_last;
1009
1010 if (ei->type != E820_RAM)
1011 continue;
1012 addr = round_up(ei->addr, align);
1013 ei_last = ei->addr + ei->size;
1014 if (addr < start)
1015 addr = round_up(start, align);
1016 if (addr >= ei_last)
1017 continue;
1018 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
1019 ;
1020 last = addr + size;
1021 if (last > ei_last)
1022 continue;
1023 if (last > end)
1024 continue;
1025 return addr;
1026 }
1027 return -1ULL;
1028}
1029
1030/*
1031 * Find next free range after *start
1032 */
1033u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
1034{
1035 int i;
1036
1037 for (i = 0; i < e820.nr_map; i++) {
1038 struct e820entry *ei = &e820.map[i];
1039 u64 addr, last;
1040 u64 ei_last;
1041
1042 if (ei->type != E820_RAM)
1043 continue;
1044 addr = round_up(ei->addr, align);
1045 ei_last = ei->addr + ei->size;
1046 if (addr < start)
1047 addr = round_up(start, align);
1048 if (addr >= ei_last)
1049 continue;
1050 *sizep = ei_last - addr;
1051 while (bad_addr_size(&addr, sizep, align) &&
1052 addr + *sizep <= ei_last)
1053 ;
1054 last = addr + *sizep;
1055 if (last > ei_last)
1056 continue;
1057 return addr;
1058 }
a4c81cf6 1059
5c0e6f03 1060 return -1ULL;
a4c81cf6 1061}
2944e16b
YL
1062
1063/*
1064 * pre allocated 4k and reserved it in e820
1065 */
1066u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
1067{
1068 u64 size = 0;
1069 u64 addr;
1070 u64 start;
1071
61438766 1072 for (start = startt; ; start += size) {
2944e16b 1073 start = find_e820_area_size(start, &size, align);
61438766
JB
1074 if (!(start + 1))
1075 return 0;
1076 if (size >= sizet)
1077 break;
1078 }
2944e16b 1079
5c0e6f03
JB
1080#ifdef CONFIG_X86_32
1081 if (start >= MAXMEM)
1082 return 0;
1083 if (start + size > MAXMEM)
1084 size = MAXMEM - start;
1085#endif
1086
2944e16b 1087 addr = round_down(start + size - sizet, align);
5c0e6f03
JB
1088 if (addr < start)
1089 return 0;
d0be6bde 1090 e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
fc9036ea 1091 e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED);
2944e16b
YL
1092 printk(KERN_INFO "update e820 for early_reserve_e820\n");
1093 update_e820();
fc9036ea 1094 update_e820_saved();
2944e16b
YL
1095
1096 return addr;
1097}
1098
ee0c80fa
YL
1099#ifdef CONFIG_X86_32
1100# ifdef CONFIG_X86_PAE
1101# define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
1102# else
1103# define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
1104# endif
1105#else /* CONFIG_X86_32 */
bd70e522 1106# define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
ee0c80fa
YL
1107#endif
1108
ee0c80fa
YL
1109/*
1110 * Find the highest page frame number we have available
1111 */
f361a450 1112static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type)
ee0c80fa 1113{
2dc807b3
YL
1114 int i;
1115 unsigned long last_pfn = 0;
ee0c80fa
YL
1116 unsigned long max_arch_pfn = MAX_ARCH_PFN;
1117
2dc807b3
YL
1118 for (i = 0; i < e820.nr_map; i++) {
1119 struct e820entry *ei = &e820.map[i];
f361a450 1120 unsigned long start_pfn;
2dc807b3
YL
1121 unsigned long end_pfn;
1122
f361a450 1123 if (ei->type != type)
c22d4c18 1124 continue;
c22d4c18 1125
f361a450 1126 start_pfn = ei->addr >> PAGE_SHIFT;
2dc807b3 1127 end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT;
f361a450
YL
1128
1129 if (start_pfn >= limit_pfn)
1130 continue;
1131 if (end_pfn > limit_pfn) {
1132 last_pfn = limit_pfn;
1133 break;
1134 }
2dc807b3
YL
1135 if (end_pfn > last_pfn)
1136 last_pfn = end_pfn;
1137 }
ee0c80fa
YL
1138
1139 if (last_pfn > max_arch_pfn)
1140 last_pfn = max_arch_pfn;
ee0c80fa 1141
5dab8ec1 1142 printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n",
ee0c80fa
YL
1143 last_pfn, max_arch_pfn);
1144 return last_pfn;
1145}
f361a450
YL
1146unsigned long __init e820_end_of_ram_pfn(void)
1147{
1148 return e820_end_pfn(MAX_ARCH_PFN, E820_RAM);
1149}
ee0c80fa 1150
f361a450
YL
1151unsigned long __init e820_end_of_low_ram_pfn(void)
1152{
1153 return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM);
1154}
ee0c80fa
YL
1155/*
1156 * Finds an active region in the address range from start_pfn to last_pfn and
1157 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
1158 */
1159int __init e820_find_active_region(const struct e820entry *ei,
1160 unsigned long start_pfn,
1161 unsigned long last_pfn,
1162 unsigned long *ei_startpfn,
1163 unsigned long *ei_endpfn)
1164{
1165 u64 align = PAGE_SIZE;
1166
1167 *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
1168 *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
1169
1170 /* Skip map entries smaller than a page */
1171 if (*ei_startpfn >= *ei_endpfn)
1172 return 0;
1173
1174 /* Skip if map is outside the node */
1175 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
1176 *ei_startpfn >= last_pfn)
1177 return 0;
1178
1179 /* Check for overlaps */
1180 if (*ei_startpfn < start_pfn)
1181 *ei_startpfn = start_pfn;
1182 if (*ei_endpfn > last_pfn)
1183 *ei_endpfn = last_pfn;
1184
ee0c80fa
YL
1185 return 1;
1186}
1187
1188/* Walk the e820 map and register active regions within a node */
1189void __init e820_register_active_regions(int nid, unsigned long start_pfn,
1190 unsigned long last_pfn)
1191{
1192 unsigned long ei_startpfn;
1193 unsigned long ei_endpfn;
1194 int i;
1195
1196 for (i = 0; i < e820.nr_map; i++)
1197 if (e820_find_active_region(&e820.map[i],
1198 start_pfn, last_pfn,
1199 &ei_startpfn, &ei_endpfn))
1200 add_active_range(nid, ei_startpfn, ei_endpfn);
1201}
1202
1203/*
1204 * Find the hole size (in bytes) in the memory range.
1205 * @start: starting address of the memory range to scan
1206 * @end: ending address of the memory range to scan
1207 */
1208u64 __init e820_hole_size(u64 start, u64 end)
1209{
1210 unsigned long start_pfn = start >> PAGE_SHIFT;
1211 unsigned long last_pfn = end >> PAGE_SHIFT;
1212 unsigned long ei_startpfn, ei_endpfn, ram = 0;
1213 int i;
1214
1215 for (i = 0; i < e820.nr_map; i++) {
1216 if (e820_find_active_region(&e820.map[i],
1217 start_pfn, last_pfn,
1218 &ei_startpfn, &ei_endpfn))
1219 ram += ei_endpfn - ei_startpfn;
1220 }
1221 return end - start - ((u64)ram << PAGE_SHIFT);
1222}
ab4a465e
YL
1223
1224static void early_panic(char *msg)
1225{
1226 early_printk(msg);
1227 panic(msg);
1228}
1229
69a7704d
YL
1230static int userdef __initdata;
1231
ab4a465e
YL
1232/* "mem=nopentium" disables the 4MB page tables. */
1233static int __init parse_memopt(char *p)
1234{
1235 u64 mem_size;
1236
1237 if (!p)
1238 return -EINVAL;
1239
1240#ifdef CONFIG_X86_32
1241 if (!strcmp(p, "nopentium")) {
1242 setup_clear_cpu_cap(X86_FEATURE_PSE);
1243 return 0;
1244 }
1245#endif
1246
69a7704d 1247 userdef = 1;
ab4a465e 1248 mem_size = memparse(p, &p);
69a7704d 1249 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
611dfd78 1250
ab4a465e
YL
1251 return 0;
1252}
1253early_param("mem", parse_memopt);
1254
ab4a465e
YL
1255static int __init parse_memmap_opt(char *p)
1256{
1257 char *oldp;
1258 u64 start_at, mem_size;
1259
a737abd1
CG
1260 if (!p)
1261 return -EINVAL;
1262
d6be118a 1263 if (!strncmp(p, "exactmap", 8)) {
ab4a465e
YL
1264#ifdef CONFIG_CRASH_DUMP
1265 /*
1266 * If we are doing a crash dump, we still need to know
1267 * the real mem size before original memory map is
1268 * reset.
1269 */
f361a450 1270 saved_max_pfn = e820_end_of_ram_pfn();
ab4a465e
YL
1271#endif
1272 e820.nr_map = 0;
1273 userdef = 1;
1274 return 0;
1275 }
1276
1277 oldp = p;
1278 mem_size = memparse(p, &p);
1279 if (p == oldp)
1280 return -EINVAL;
1281
1282 userdef = 1;
1283 if (*p == '@') {
1284 start_at = memparse(p+1, &p);
d0be6bde 1285 e820_add_region(start_at, mem_size, E820_RAM);
ab4a465e
YL
1286 } else if (*p == '#') {
1287 start_at = memparse(p+1, &p);
d0be6bde 1288 e820_add_region(start_at, mem_size, E820_ACPI);
ab4a465e
YL
1289 } else if (*p == '$') {
1290 start_at = memparse(p+1, &p);
d0be6bde 1291 e820_add_region(start_at, mem_size, E820_RESERVED);
7b479bec 1292 } else
69a7704d 1293 e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1);
7b479bec 1294
ab4a465e
YL
1295 return *p == '\0' ? 0 : -EINVAL;
1296}
1297early_param("memmap", parse_memmap_opt);
1298
1299void __init finish_e820_parsing(void)
1300{
1301 if (userdef) {
ba639039 1302 u32 nr = e820.nr_map;
ab4a465e
YL
1303
1304 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1305 early_panic("Invalid user supplied memory map");
1306 e820.nr_map = nr;
1307
1308 printk(KERN_INFO "user-defined physical RAM map:\n");
1309 e820_print_map("user");
1310 }
1311}
41c094fd 1312
5dfcf14d
BW
1313static inline const char *e820_type_to_string(int e820_type)
1314{
1315 switch (e820_type) {
1316 case E820_RESERVED_KERN:
1317 case E820_RAM: return "System RAM";
1318 case E820_ACPI: return "ACPI Tables";
1319 case E820_NVS: return "ACPI Non-volatile Storage";
671eef85 1320 case E820_UNUSABLE: return "Unusable memory";
5dfcf14d
BW
1321 default: return "reserved";
1322 }
1323}
1324
41c094fd
YL
1325/*
1326 * Mark e820 reserved areas as busy for the resource manager.
1327 */
a5444d15 1328static struct resource __initdata *e820_res;
41c094fd
YL
1329void __init e820_reserve_resources(void)
1330{
1331 int i;
58f7c988 1332 struct resource *res;
a5444d15 1333 u64 end;
41c094fd
YL
1334
1335 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
58f7c988 1336 e820_res = res;
41c094fd 1337 for (i = 0; i < e820.nr_map; i++) {
b4df32f4 1338 end = e820.map[i].addr + e820.map[i].size - 1;
8308c54d 1339 if (end != (resource_size_t)end) {
41c094fd
YL
1340 res++;
1341 continue;
1342 }
5dfcf14d 1343 res->name = e820_type_to_string(e820.map[i].type);
b4df32f4
YL
1344 res->start = e820.map[i].addr;
1345 res->end = end;
1346
1f987577 1347 res->flags = IORESOURCE_MEM;
a5444d15
IM
1348
1349 /*
1350 * don't register the region that could be conflicted with
1351 * pci device BAR resource and insert them later in
1352 * pcibios_resource_survey()
1353 */
1f987577
LT
1354 if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) {
1355 res->flags |= IORESOURCE_BUSY;
58f7c988 1356 insert_resource(&iomem_resource, res);
1f987577 1357 }
41c094fd
YL
1358 res++;
1359 }
5dfcf14d
BW
1360
1361 for (i = 0; i < e820_saved.nr_map; i++) {
1362 struct e820entry *entry = &e820_saved.map[i];
1363 firmware_map_add_early(entry->addr,
1364 entry->addr + entry->size - 1,
1365 e820_type_to_string(entry->type));
1366 }
41c094fd
YL
1367}
1368
45fbe3ee
LT
1369/* How much should we pad RAM ending depending on where it is? */
1370static unsigned long ram_alignment(resource_size_t pos)
1371{
1372 unsigned long mb = pos >> 20;
1373
1374 /* To 64kB in the first megabyte */
1375 if (!mb)
1376 return 64*1024;
1377
1378 /* To 1MB in the first 16MB */
1379 if (mb < 16)
1380 return 1024*1024;
1381
1382 /* To 32MB for anything above that */
1383 return 32*1024*1024;
1384}
1385
7c5371c4
YL
1386#define MAX_RESOURCE_SIZE ((resource_size_t)-1)
1387
58f7c988
YL
1388void __init e820_reserve_resources_late(void)
1389{
1390 int i;
1391 struct resource *res;
1392
1393 res = e820_res;
1394 for (i = 0; i < e820.nr_map; i++) {
a5444d15 1395 if (!res->parent && res->end)
1f987577 1396 insert_resource_expand_to_fit(&iomem_resource, res);
58f7c988
YL
1397 res++;
1398 }
45fbe3ee
LT
1399
1400 /*
1401 * Try to bump up RAM regions to reasonable boundaries to
1402 * avoid stolen RAM:
1403 */
1404 for (i = 0; i < e820.nr_map; i++) {
7c5371c4
YL
1405 struct e820entry *entry = &e820.map[i];
1406 u64 start, end;
45fbe3ee
LT
1407
1408 if (entry->type != E820_RAM)
1409 continue;
1410 start = entry->addr + entry->size;
7c5371c4
YL
1411 end = round_up(start, ram_alignment(start)) - 1;
1412 if (end > MAX_RESOURCE_SIZE)
1413 end = MAX_RESOURCE_SIZE;
1414 if (start >= end)
45fbe3ee 1415 continue;
7c5371c4
YL
1416 reserve_region_with_split(&iomem_resource, start, end,
1417 "RAM buffer");
45fbe3ee 1418 }
58f7c988
YL
1419}
1420
95a71a45 1421char *__init default_machine_specific_memory_setup(void)
064d25f1
YL
1422{
1423 char *who = "BIOS-e820";
ba639039 1424 u32 new_nr;
064d25f1
YL
1425 /*
1426 * Try to copy the BIOS-supplied E820-map.
1427 *
1428 * Otherwise fake a memory map; one section from 0k->640k,
1429 * the next section from 1mb->appropriate_mem_k
1430 */
1431 new_nr = boot_params.e820_entries;
1432 sanitize_e820_map(boot_params.e820_map,
1433 ARRAY_SIZE(boot_params.e820_map),
1434 &new_nr);
1435 boot_params.e820_entries = new_nr;
dc8e8120
YL
1436 if (append_e820_map(boot_params.e820_map, boot_params.e820_entries)
1437 < 0) {
95a71a45 1438 u64 mem_size;
064d25f1
YL
1439
1440 /* compare results from other methods and take the greater */
1441 if (boot_params.alt_mem_k
1442 < boot_params.screen_info.ext_mem_k) {
1443 mem_size = boot_params.screen_info.ext_mem_k;
1444 who = "BIOS-88";
1445 } else {
1446 mem_size = boot_params.alt_mem_k;
1447 who = "BIOS-e801";
1448 }
1449
1450 e820.nr_map = 0;
1451 e820_add_region(0, LOWMEMSIZE(), E820_RAM);
1452 e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
064d25f1
YL
1453 }
1454
1455 /* In case someone cares... */
1456 return who;
1457}
1458
95a71a45
YL
1459char *__init __attribute__((weak)) machine_specific_memory_setup(void)
1460{
3c9cb6de
YL
1461 if (x86_quirks->arch_memory_setup) {
1462 char *who = x86_quirks->arch_memory_setup();
3b33553b
IM
1463
1464 if (who)
1465 return who;
1466 }
95a71a45
YL
1467 return default_machine_specific_memory_setup();
1468}
1469
064d25f1
YL
1470/* Overridden in paravirt.c if CONFIG_PARAVIRT */
1471char * __init __attribute__((weak)) memory_setup(void)
1472{
1473 return machine_specific_memory_setup();
1474}
1475
1476void __init setup_memory_map(void)
1477{
0be15526
YL
1478 char *who;
1479
1480 who = memory_setup();
1481 memcpy(&e820_saved, &e820, sizeof(struct e820map));
064d25f1 1482 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
0be15526 1483 e820_print_map(who);
064d25f1 1484}
This page took 0.176421 seconds and 5 git commands to generate.