blob: 15b4393ff9bf8964efefab1e7de4068c0db96ceb [file] [log] [blame]
Yinghai Lub79cd8f2008-05-11 00:30:15 -07001/*
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>
Yinghai Lubf62f392008-05-20 20:10:58 -070021#include <linux/suspend.h>
Yinghai Lub79cd8f2008-05-11 00:30:15 -070022
23#include <asm/pgtable.h>
24#include <asm/page.h>
25#include <asm/e820.h>
Yinghai Lua4c81cf2008-05-18 01:18:57 -070026#include <asm/proto.h>
Yinghai Lub79cd8f2008-05-11 00:30:15 -070027#include <asm/setup.h>
Yinghai Lua4c81cf2008-05-18 01:18:57 -070028#include <asm/trampoline.h>
Yinghai Lub79cd8f2008-05-11 00:30:15 -070029
30struct e820map e820;
31
32/* For PCI or other memory-mapped resources */
33unsigned long pci_mem_start = 0xaeedbabe;
34#ifdef CONFIG_PCI
35EXPORT_SYMBOL(pci_mem_start);
36#endif
37
38/*
39 * This function checks if any part of the range <start,end> is mapped
40 * with type.
41 */
42int
43e820_any_mapped(u64 start, u64 end, unsigned type)
44{
45 int i;
46
47 for (i = 0; i < e820.nr_map; i++) {
48 struct e820entry *ei = &e820.map[i];
49
50 if (type && ei->type != type)
51 continue;
52 if (ei->addr >= end || ei->addr + ei->size <= start)
53 continue;
54 return 1;
55 }
56 return 0;
57}
58EXPORT_SYMBOL_GPL(e820_any_mapped);
59
60/*
61 * This function checks if the entire range <start,end> is mapped with type.
62 *
63 * Note: this function only works correct if the e820 table is sorted and
64 * not-overlapping, which is the case
65 */
66int __init e820_all_mapped(u64 start, u64 end, unsigned type)
67{
68 int i;
69
70 for (i = 0; i < e820.nr_map; i++) {
71 struct e820entry *ei = &e820.map[i];
72
73 if (type && ei->type != type)
74 continue;
75 /* is the region (part) in overlap with the current region ?*/
76 if (ei->addr >= end || ei->addr + ei->size <= start)
77 continue;
78
79 /* if the region is at the beginning of <start,end> we move
80 * start to the end of the region since it's ok until there
81 */
82 if (ei->addr <= start)
83 start = ei->addr + ei->size;
84 /*
85 * if start is now at or beyond end, we're done, full
86 * coverage
87 */
88 if (start >= end)
89 return 1;
90 }
91 return 0;
92}
93
94/*
95 * Add a memory region to the kernel e820 map.
96 */
Yinghai Lud0be6bd2008-06-15 18:58:51 -070097void __init e820_add_region(u64 start, u64 size, int type)
Yinghai Lub79cd8f2008-05-11 00:30:15 -070098{
99 int x = e820.nr_map;
100
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700101 if (x == ARRAY_SIZE(e820.map)) {
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700102 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
103 return;
104 }
105
106 e820.map[x].addr = start;
107 e820.map[x].size = size;
108 e820.map[x].type = type;
109 e820.nr_map++;
110}
111
112void __init e820_print_map(char *who)
113{
114 int i;
115
116 for (i = 0; i < e820.nr_map; i++) {
117 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
118 (unsigned long long) e820.map[i].addr,
119 (unsigned long long)
120 (e820.map[i].addr + e820.map[i].size));
121 switch (e820.map[i].type) {
122 case E820_RAM:
123 printk(KERN_CONT "(usable)\n");
124 break;
125 case E820_RESERVED:
126 printk(KERN_CONT "(reserved)\n");
127 break;
128 case E820_ACPI:
129 printk(KERN_CONT "(ACPI data)\n");
130 break;
131 case E820_NVS:
132 printk(KERN_CONT "(ACPI NVS)\n");
133 break;
134 default:
135 printk(KERN_CONT "type %u\n", e820.map[i].type);
136 break;
137 }
138 }
139}
140
141/*
142 * Sanitize the BIOS e820 map.
143 *
144 * Some e820 responses include overlapping entries. The following
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700145 * replaces the original e820 map with a new one, removing overlaps,
146 * and resolving conflicting memory types in favor of highest
147 * numbered type.
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700148 *
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700149 * The input parameter biosmap points to an array of 'struct
150 * e820entry' which on entry has elements in the range [0, *pnr_map)
151 * valid, and which has space for up to max_nr_map entries.
152 * On return, the resulting sanitized e820 map entries will be in
153 * overwritten in the same location, starting at biosmap.
154 *
155 * The integer pointed to by pnr_map must be valid on entry (the
156 * current number of valid entries located at biosmap) and will
157 * be updated on return, with the new number of valid entries
158 * (something no more than max_nr_map.)
159 *
160 * The return value from sanitize_e820_map() is zero if it
161 * successfully 'sanitized' the map entries passed in, and is -1
162 * if it did nothing, which can happen if either of (1) it was
163 * only passed one map entry, or (2) any of the input map entries
164 * were invalid (start + size < start, meaning that the size was
165 * so big the described memory range wrapped around through zero.)
166 *
167 * Visually we're performing the following
168 * (1,2,3,4 = memory types)...
169 *
170 * Sample memory map (w/overlaps):
171 * ____22__________________
172 * ______________________4_
173 * ____1111________________
174 * _44_____________________
175 * 11111111________________
176 * ____________________33__
177 * ___________44___________
178 * __________33333_________
179 * ______________22________
180 * ___________________2222_
181 * _________111111111______
182 * _____________________11_
183 * _________________4______
184 *
185 * Sanitized equivalent (no overlap):
186 * 1_______________________
187 * _44_____________________
188 * ___1____________________
189 * ____22__________________
190 * ______11________________
191 * _________1______________
192 * __________3_____________
193 * ___________44___________
194 * _____________33_________
195 * _______________2________
196 * ________________1_______
197 * _________________4______
198 * ___________________2____
199 * ____________________33__
200 * ______________________4_
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700201 */
Paul Jackson5b7eb2e2008-05-14 08:15:52 -0700202
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700203int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700204 int *pnr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700205{
206 struct change_member {
207 struct e820entry *pbios; /* pointer to original bios entry */
208 unsigned long long addr; /* address for this change point */
209 };
Paul Jackson157fabf2008-06-22 07:21:57 -0700210 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
211 static struct change_member *change_point[2*E820_X_MAX] __initdata;
212 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
213 static struct e820entry new_bios[E820_X_MAX] __initdata;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700214 struct change_member *change_tmp;
215 unsigned long current_type, last_type;
216 unsigned long long last_addr;
217 int chgidx, still_changing;
218 int overlap_entries;
219 int new_bios_entry;
220 int old_nr, new_nr, chg_nr;
221 int i;
222
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700223 /* if there's only one memory region, don't bother */
224 if (*pnr_map < 2)
225 return -1;
226
227 old_nr = *pnr_map;
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700228 BUG_ON(old_nr > max_nr_map);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700229
230 /* bail out if we find any unreasonable addresses in bios map */
231 for (i = 0; i < old_nr; i++)
232 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
233 return -1;
234
235 /* create pointers for initial change-point information (for sorting) */
236 for (i = 0; i < 2 * old_nr; i++)
237 change_point[i] = &change_point_list[i];
238
239 /* record all known change-points (starting and ending addresses),
240 omitting those that are for empty memory regions */
241 chgidx = 0;
242 for (i = 0; i < old_nr; i++) {
243 if (biosmap[i].size != 0) {
244 change_point[chgidx]->addr = biosmap[i].addr;
245 change_point[chgidx++]->pbios = &biosmap[i];
246 change_point[chgidx]->addr = biosmap[i].addr +
247 biosmap[i].size;
248 change_point[chgidx++]->pbios = &biosmap[i];
249 }
250 }
251 chg_nr = chgidx;
252
253 /* sort change-point list by memory addresses (low -> high) */
254 still_changing = 1;
255 while (still_changing) {
256 still_changing = 0;
257 for (i = 1; i < chg_nr; i++) {
258 unsigned long long curaddr, lastaddr;
259 unsigned long long curpbaddr, lastpbaddr;
260
261 curaddr = change_point[i]->addr;
262 lastaddr = change_point[i - 1]->addr;
263 curpbaddr = change_point[i]->pbios->addr;
264 lastpbaddr = change_point[i - 1]->pbios->addr;
265
266 /*
267 * swap entries, when:
268 *
269 * curaddr > lastaddr or
270 * curaddr == lastaddr and curaddr == curpbaddr and
271 * lastaddr != lastpbaddr
272 */
273 if (curaddr < lastaddr ||
274 (curaddr == lastaddr && curaddr == curpbaddr &&
275 lastaddr != lastpbaddr)) {
276 change_tmp = change_point[i];
277 change_point[i] = change_point[i-1];
278 change_point[i-1] = change_tmp;
279 still_changing = 1;
280 }
281 }
282 }
283
284 /* create a new bios memory map, removing overlaps */
285 overlap_entries = 0; /* number of entries in the overlap table */
286 new_bios_entry = 0; /* index for creating new bios map entries */
287 last_type = 0; /* start with undefined memory type */
288 last_addr = 0; /* start with 0 as last starting address */
289
290 /* loop through change-points, determining affect on the new bios map */
291 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
292 /* keep track of all overlapping bios entries */
293 if (change_point[chgidx]->addr ==
294 change_point[chgidx]->pbios->addr) {
295 /*
296 * add map entry to overlap list (> 1 entry
297 * implies an overlap)
298 */
299 overlap_list[overlap_entries++] =
300 change_point[chgidx]->pbios;
301 } else {
302 /*
303 * remove entry from list (order independent,
304 * so swap with last)
305 */
306 for (i = 0; i < overlap_entries; i++) {
307 if (overlap_list[i] ==
308 change_point[chgidx]->pbios)
309 overlap_list[i] =
310 overlap_list[overlap_entries-1];
311 }
312 overlap_entries--;
313 }
314 /*
315 * if there are overlapping entries, decide which
316 * "type" to use (larger value takes precedence --
317 * 1=usable, 2,3,4,4+=unusable)
318 */
319 current_type = 0;
320 for (i = 0; i < overlap_entries; i++)
321 if (overlap_list[i]->type > current_type)
322 current_type = overlap_list[i]->type;
323 /*
324 * continue building up new bios map based on this
325 * information
326 */
327 if (current_type != last_type) {
328 if (last_type != 0) {
329 new_bios[new_bios_entry].size =
330 change_point[chgidx]->addr - last_addr;
331 /*
332 * move forward only if the new size
333 * was non-zero
334 */
335 if (new_bios[new_bios_entry].size != 0)
336 /*
337 * no more space left for new
338 * bios entries ?
339 */
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700340 if (++new_bios_entry >= max_nr_map)
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700341 break;
342 }
343 if (current_type != 0) {
344 new_bios[new_bios_entry].addr =
345 change_point[chgidx]->addr;
346 new_bios[new_bios_entry].type = current_type;
347 last_addr = change_point[chgidx]->addr;
348 }
349 last_type = current_type;
350 }
351 }
352 /* retain count for new bios entries */
353 new_nr = new_bios_entry;
354
355 /* copy new bios mapping into original location */
356 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
357 *pnr_map = new_nr;
358
359 return 0;
360}
361
Huang, Ying8c5beb52008-06-11 11:33:39 +0800362static int __init __copy_e820_map(struct e820entry *biosmap, int nr_map)
363{
364 while (nr_map) {
365 u64 start = biosmap->addr;
366 u64 size = biosmap->size;
367 u64 end = start + size;
368 u32 type = biosmap->type;
369
370 /* Overflow in 64 bits? Ignore the memory map. */
371 if (start > end)
372 return -1;
373
374 e820_add_region(start, size, type);
375
376 biosmap++;
377 nr_map--;
378 }
379 return 0;
380}
381
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700382/*
383 * Copy the BIOS e820 map into a safe place.
384 *
385 * Sanity-check it while we're at it..
386 *
387 * If we're lucky and live on a modern system, the setup code
388 * will have given us a memory map that we can use to properly
389 * set up memory. If we aren't, we'll fake a memory map.
390 */
391int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
392{
393 /* Only one memory region (or negative)? Ignore it */
394 if (nr_map < 2)
395 return -1;
396
Huang, Ying8c5beb52008-06-11 11:33:39 +0800397 return __copy_e820_map(biosmap, nr_map);
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700398}
399
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700400u64 __init e820_update_range(u64 start, u64 size, unsigned old_type,
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700401 unsigned new_type)
402{
403 int i;
404 u64 real_updated_size = 0;
405
406 BUG_ON(old_type == new_type);
407
408 for (i = 0; i < e820.nr_map; i++) {
409 struct e820entry *ei = &e820.map[i];
410 u64 final_start, final_end;
411 if (ei->type != old_type)
412 continue;
413 /* totally covered? */
414 if (ei->addr >= start &&
415 (ei->addr + ei->size) <= (start + size)) {
416 ei->type = new_type;
417 real_updated_size += ei->size;
418 continue;
419 }
420 /* partially covered */
421 final_start = max(start, ei->addr);
422 final_end = min(start + size, ei->addr + ei->size);
423 if (final_start >= final_end)
424 continue;
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700425 e820_add_region(final_start, final_end - final_start,
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700426 new_type);
427 real_updated_size += final_end - final_start;
Yinghai Lu976dd4d2008-06-24 14:55:32 -0700428
429 ei->size -= final_end - final_start;
430 if (ei->addr < final_start)
431 continue;
432 ei->addr = final_end;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700433 }
434 return real_updated_size;
435}
436
Yinghai Lu7a1fd982008-06-21 14:48:05 -0700437/* make e820 not cover the range */
438u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type,
439 int checktype)
440{
441 int i;
442 u64 real_removed_size = 0;
443
444 for (i = 0; i < e820.nr_map; i++) {
445 struct e820entry *ei = &e820.map[i];
446 u64 final_start, final_end;
447
448 if (checktype && ei->type != old_type)
449 continue;
450 /* totally covered? */
451 if (ei->addr >= start &&
452 (ei->addr + ei->size) <= (start + size)) {
453 real_removed_size += ei->size;
454 memset(ei, 0, sizeof(struct e820entry));
455 continue;
456 }
457 /* partially covered */
458 final_start = max(start, ei->addr);
459 final_end = min(start + size, ei->addr + ei->size);
460 if (final_start >= final_end)
461 continue;
462 real_removed_size += final_end - final_start;
463
464 ei->size -= final_end - final_start;
465 if (ei->addr < final_start)
466 continue;
467 ei->addr = final_end;
468 }
469 return real_removed_size;
470}
471
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700472void __init update_e820(void)
473{
Paul Jackson6e9bcc72008-05-14 08:15:46 -0700474 int nr_map;
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700475
476 nr_map = e820.nr_map;
Paul Jacksonc3965bd2008-05-14 08:15:34 -0700477 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
Yinghai Lub79cd8f2008-05-11 00:30:15 -0700478 return;
479 e820.nr_map = nr_map;
480 printk(KERN_INFO "modified physical RAM map:\n");
481 e820_print_map("modified");
482}
483
484/*
485 * Search for the biggest gap in the low 32 bits of the e820
486 * memory space. We pass this space to PCI to assign MMIO resources
487 * for hotplug or unconfigured devices in.
488 * Hopefully the BIOS let enough space left.
489 */
490__init void e820_setup_gap(void)
491{
492 unsigned long gapstart, gapsize, round;
493 unsigned long long last;
494 int i;
495 int found = 0;
496
497 last = 0x100000000ull;
498 gapstart = 0x10000000;
499 gapsize = 0x400000;
500 i = e820.nr_map;
501 while (--i >= 0) {
502 unsigned long long start = e820.map[i].addr;
503 unsigned long long end = start + e820.map[i].size;
504
505 /*
506 * Since "last" is at most 4GB, we know we'll
507 * fit in 32 bits if this condition is true
508 */
509 if (last > end) {
510 unsigned long gap = last - end;
511
512 if (gap > gapsize) {
513 gapsize = gap;
514 gapstart = end;
515 found = 1;
516 }
517 }
518 if (start < last)
519 last = start;
520 }
521
522#ifdef CONFIG_X86_64
523 if (!found) {
524 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
525 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
526 "address range\n"
527 KERN_ERR "PCI: Unassigned devices with 32bit resource "
528 "registers may break!\n");
529 }
530#endif
531
532 /*
533 * See how much we want to round up: start off with
534 * rounding to the next 1MB area.
535 */
536 round = 0x100000;
537 while ((gapsize >> 4) > round)
538 round += round;
539 /* Fun with two's complement */
540 pci_mem_start = (gapstart + round) & -round;
541
542 printk(KERN_INFO
543 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
544 pci_mem_start, gapstart, gapsize);
545}
546
Huang, Ying8c5beb52008-06-11 11:33:39 +0800547/**
548 * Because of the size limitation of struct boot_params, only first
549 * 128 E820 memory entries are passed to kernel via
550 * boot_params.e820_map, others are passed via SETUP_E820_EXT node of
551 * linked list of struct setup_data, which is parsed here.
552 */
553void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data)
554{
555 u32 map_len;
556 int entries;
557 struct e820entry *extmap;
558
559 entries = sdata->len / sizeof(struct e820entry);
560 map_len = sdata->len + sizeof(struct setup_data);
561 if (map_len > PAGE_SIZE)
562 sdata = early_ioremap(pa_data, map_len);
563 extmap = (struct e820entry *)(sdata->data);
564 __copy_e820_map(extmap, entries);
565 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
566 if (map_len > PAGE_SIZE)
567 early_iounmap(sdata, map_len);
568 printk(KERN_INFO "extended physical RAM map:\n");
569 e820_print_map("extended");
570}
571
Yinghai Lubf62f392008-05-20 20:10:58 -0700572#if defined(CONFIG_X86_64) || \
573 (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION))
574/**
575 * Find the ranges of physical addresses that do not correspond to
576 * e820 RAM areas and mark the corresponding pages as nosave for
577 * hibernation (32 bit) or software suspend and suspend to RAM (64 bit).
578 *
579 * This function requires the e820 map to be sorted and without any
580 * overlapping entries and assumes the first e820 area to be RAM.
581 */
582void __init e820_mark_nosave_regions(unsigned long limit_pfn)
583{
584 int i;
585 unsigned long pfn;
586
587 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
588 for (i = 1; i < e820.nr_map; i++) {
589 struct e820entry *ei = &e820.map[i];
590
591 if (pfn < PFN_UP(ei->addr))
592 register_nosave_region(pfn, PFN_UP(ei->addr));
593
594 pfn = PFN_DOWN(ei->addr + ei->size);
595 if (ei->type != E820_RAM)
596 register_nosave_region(PFN_UP(ei->addr), pfn);
597
598 if (pfn >= limit_pfn)
599 break;
600 }
601}
602#endif
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700603
604/*
605 * Early reserved memory areas.
606 */
607#define MAX_EARLY_RES 20
608
609struct early_res {
610 u64 start, end;
611 char name[16];
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700612 char overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700613};
614static struct early_res early_res[MAX_EARLY_RES] __initdata = {
615 { 0, PAGE_SIZE, "BIOS data page" }, /* BIOS data page */
616#if defined(CONFIG_X86_64) && defined(CONFIG_X86_TRAMPOLINE)
617 { TRAMPOLINE_BASE, TRAMPOLINE_BASE + 2 * PAGE_SIZE, "TRAMPOLINE" },
618#endif
619#if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
620 /*
621 * But first pinch a few for the stack/trampoline stuff
622 * FIXME: Don't need the extra page at 4K, but need to fix
623 * trampoline before removing it. (see the GDT stuff)
624 */
625 { PAGE_SIZE, PAGE_SIZE + PAGE_SIZE, "EX TRAMPOLINE" },
626 /*
627 * Has to be in very low memory so we can execute
628 * real-mode AP code.
629 */
630 { TRAMPOLINE_BASE, TRAMPOLINE_BASE + PAGE_SIZE, "TRAMPOLINE" },
631#endif
632 {}
633};
634
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800635static int __init find_overlapped_early(u64 start, u64 end)
636{
637 int i;
638 struct early_res *r;
639
640 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
641 r = &early_res[i];
642 if (end > r->start && start < r->end)
643 break;
644 }
645
646 return i;
647}
648
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700649/*
650 * Drop the i-th range from the early reservation map,
651 * by copying any higher ranges down one over it, and
652 * clearing what had been the last slot.
653 */
654static void __init drop_range(int i)
655{
656 int j;
657
658 for (j = i + 1; j < MAX_EARLY_RES && early_res[j].end; j++)
659 ;
660
661 memmove(&early_res[i], &early_res[i + 1],
662 (j - 1 - i) * sizeof(struct early_res));
663
664 early_res[j - 1].end = 0;
665}
666
667/*
668 * Split any existing ranges that:
669 * 1) are marked 'overlap_ok', and
670 * 2) overlap with the stated range [start, end)
671 * into whatever portion (if any) of the existing range is entirely
672 * below or entirely above the stated range. Drop the portion
673 * of the existing range that overlaps with the stated range,
674 * which will allow the caller of this routine to then add that
675 * stated range without conflicting with any existing range.
676 */
677static void __init drop_overlaps_that_are_ok(u64 start, u64 end)
678{
679 int i;
680 struct early_res *r;
681 u64 lower_start, lower_end;
682 u64 upper_start, upper_end;
683 char name[16];
684
685 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
686 r = &early_res[i];
687
688 /* Continue past non-overlapping ranges */
689 if (end <= r->start || start >= r->end)
690 continue;
691
692 /*
693 * Leave non-ok overlaps as is; let caller
694 * panic "Overlapping early reservations"
695 * when it hits this overlap.
696 */
697 if (!r->overlap_ok)
698 return;
699
700 /*
701 * We have an ok overlap. We will drop it from the early
702 * reservation map, and add back in any non-overlapping
703 * portions (lower or upper) as separate, overlap_ok,
704 * non-overlapping ranges.
705 */
706
707 /* 1. Note any non-overlapping (lower or upper) ranges. */
708 strncpy(name, r->name, sizeof(name) - 1);
709
710 lower_start = lower_end = 0;
711 upper_start = upper_end = 0;
712 if (r->start < start) {
713 lower_start = r->start;
714 lower_end = start;
715 }
716 if (r->end > end) {
717 upper_start = end;
718 upper_end = r->end;
719 }
720
721 /* 2. Drop the original ok overlapping range */
722 drop_range(i);
723
724 i--; /* resume for-loop on copied down entry */
725
726 /* 3. Add back in any non-overlapping ranges. */
727 if (lower_end)
728 reserve_early_overlap_ok(lower_start, lower_end, name);
729 if (upper_end)
730 reserve_early_overlap_ok(upper_start, upper_end, name);
731 }
732}
733
734static void __init __reserve_early(u64 start, u64 end, char *name,
735 int overlap_ok)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700736{
737 int i;
738 struct early_res *r;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800739
740 i = find_overlapped_early(start, end);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700741 if (i >= MAX_EARLY_RES)
742 panic("Too many early reservations");
743 r = &early_res[i];
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800744 if (r->end)
745 panic("Overlapping early reservations "
746 "%llx-%llx %s to %llx-%llx %s\n",
747 start, end - 1, name?name:"", r->start,
748 r->end - 1, r->name);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700749 r->start = start;
750 r->end = end;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700751 r->overlap_ok = overlap_ok;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700752 if (name)
753 strncpy(r->name, name, sizeof(r->name) - 1);
754}
755
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700756/*
757 * A few early reservtations come here.
758 *
759 * The 'overlap_ok' in the name of this routine does -not- mean it
760 * is ok for these reservations to overlap an earlier reservation.
761 * Rather it means that it is ok for subsequent reservations to
762 * overlap this one.
763 *
764 * Use this entry point to reserve early ranges when you are doing
765 * so out of "Paranoia", reserving perhaps more memory than you need,
766 * just in case, and don't mind a subsequent overlapping reservation
767 * that is known to be needed.
768 *
769 * The drop_overlaps_that_are_ok() call here isn't really needed.
770 * It would be needed if we had two colliding 'overlap_ok'
771 * reservations, so that the second such would not panic on the
772 * overlap with the first. We don't have any such as of this
773 * writing, but might as well tolerate such if it happens in
774 * the future.
775 */
776void __init reserve_early_overlap_ok(u64 start, u64 end, char *name)
777{
778 drop_overlaps_that_are_ok(start, end);
779 __reserve_early(start, end, name, 1);
780}
781
782/*
783 * Most early reservations come here.
784 *
785 * We first have drop_overlaps_that_are_ok() drop any pre-existing
786 * 'overlap_ok' ranges, so that we can then reserve this memory
787 * range without risk of panic'ing on an overlapping overlap_ok
788 * early reservation.
789 */
790void __init reserve_early(u64 start, u64 end, char *name)
791{
792 drop_overlaps_that_are_ok(start, end);
793 __reserve_early(start, end, name, 0);
794}
795
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700796void __init free_early(u64 start, u64 end)
797{
798 struct early_res *r;
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700799 int i;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700800
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800801 i = find_overlapped_early(start, end);
802 r = &early_res[i];
803 if (i >= MAX_EARLY_RES || r->end != end || r->start != start)
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700804 panic("free_early on not reserved area: %llx-%llx!",
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800805 start, end - 1);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700806
Paul Jacksonc4ba1322008-06-22 07:22:07 -0700807 drop_range(i);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700808}
809
810void __init early_res_to_bootmem(u64 start, u64 end)
811{
812 int i;
813 u64 final_start, final_end;
814 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
815 struct early_res *r = &early_res[i];
816 final_start = max(start, r->start);
817 final_end = min(end, r->end);
818 if (final_start >= final_end)
819 continue;
820 printk(KERN_INFO " early res: %d [%llx-%llx] %s\n", i,
821 final_start, final_end - 1, r->name);
Yinghai Lud2dbf342008-06-13 02:00:56 -0700822 reserve_bootmem_generic(final_start, final_end - final_start,
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700823 BOOTMEM_DEFAULT);
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700824 }
825}
826
827/* Check for already reserved areas */
828static inline int __init bad_addr(u64 *addrp, u64 size, u64 align)
829{
830 int i;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800831 u64 addr = *addrp;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700832 int changed = 0;
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800833 struct early_res *r;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700834again:
Huang, Yingd3fbe5e2008-06-02 14:26:14 +0800835 i = find_overlapped_early(addr, addr + size);
836 r = &early_res[i];
837 if (i < MAX_EARLY_RES && r->end) {
838 *addrp = addr = round_up(r->end, align);
839 changed = 1;
840 goto again;
Yinghai Lua4c81cf2008-05-18 01:18:57 -0700841 }
842 return changed;
843}
844
845/* Check for already reserved areas */
846static inline int __init bad_addr_size(u64 *addrp, u64 *sizep, u64 align)
847{
848 int i;
849 u64 addr = *addrp, last;
850 u64 size = *sizep;
851 int changed = 0;
852again:
853 last = addr + size;
854 for (i = 0; i < MAX_EARLY_RES && early_res[i].end; i++) {
855 struct early_res *r = &early_res[i];
856 if (last > r->start && addr < r->start) {
857 size = r->start - addr;
858 changed = 1;
859 goto again;
860 }
861 if (last > r->end && addr < r->end) {
862 addr = round_up(r->end, align);
863 size = last - addr;
864 changed = 1;
865 goto again;
866 }
867 if (last <= r->end && addr >= r->start) {
868 (*sizep)++;
869 return 0;
870 }
871 }
872 if (changed) {
873 *addrp = addr;
874 *sizep = size;
875 }
876 return changed;
877}
878
879/*
880 * Find a free area with specified alignment in a specific range.
881 */
882u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align)
883{
884 int i;
885
886 for (i = 0; i < e820.nr_map; i++) {
887 struct e820entry *ei = &e820.map[i];
888 u64 addr, last;
889 u64 ei_last;
890
891 if (ei->type != E820_RAM)
892 continue;
893 addr = round_up(ei->addr, align);
894 ei_last = ei->addr + ei->size;
895 if (addr < start)
896 addr = round_up(start, align);
897 if (addr >= ei_last)
898 continue;
899 while (bad_addr(&addr, size, align) && addr+size <= ei_last)
900 ;
901 last = addr + size;
902 if (last > ei_last)
903 continue;
904 if (last > end)
905 continue;
906 return addr;
907 }
908 return -1ULL;
909}
910
911/*
912 * Find next free range after *start
913 */
914u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align)
915{
916 int i;
917
918 for (i = 0; i < e820.nr_map; i++) {
919 struct e820entry *ei = &e820.map[i];
920 u64 addr, last;
921 u64 ei_last;
922
923 if (ei->type != E820_RAM)
924 continue;
925 addr = round_up(ei->addr, align);
926 ei_last = ei->addr + ei->size;
927 if (addr < start)
928 addr = round_up(start, align);
929 if (addr >= ei_last)
930 continue;
931 *sizep = ei_last - addr;
932 while (bad_addr_size(&addr, sizep, align) &&
933 addr + *sizep <= ei_last)
934 ;
935 last = addr + *sizep;
936 if (last > ei_last)
937 continue;
938 return addr;
939 }
940 return -1UL;
941
942}
Yinghai Lu2944e162008-06-01 13:17:38 -0700943
944/*
945 * pre allocated 4k and reserved it in e820
946 */
947u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align)
948{
949 u64 size = 0;
950 u64 addr;
951 u64 start;
952
953 start = startt;
954 while (size < sizet)
955 start = find_e820_area_size(start, &size, align);
956
957 if (size < sizet)
958 return 0;
959
960 addr = round_down(start + size - sizet, align);
Yinghai Lud0be6bd2008-06-15 18:58:51 -0700961 e820_update_range(addr, sizet, E820_RAM, E820_RESERVED);
Yinghai Lu2944e162008-06-01 13:17:38 -0700962 printk(KERN_INFO "update e820 for early_reserve_e820\n");
963 update_e820();
964
965 return addr;
966}
967
Yinghai Luee0c80f2008-06-03 19:34:00 -0700968#ifdef CONFIG_X86_32
969# ifdef CONFIG_X86_PAE
970# define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT))
971# else
972# define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT))
973# endif
974#else /* CONFIG_X86_32 */
Yinghai Lubd70e522008-06-04 13:21:29 -0700975# define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT
Yinghai Luee0c80f2008-06-03 19:34:00 -0700976#endif
977
978/*
979 * Last pfn which the user wants to use.
980 */
981unsigned long __initdata end_user_pfn = MAX_ARCH_PFN;
982
983/*
984 * Find the highest page frame number we have available
985 */
986unsigned long __init e820_end_of_ram(void)
987{
988 unsigned long last_pfn;
989 unsigned long max_arch_pfn = MAX_ARCH_PFN;
990
991 last_pfn = find_max_pfn_with_active_regions();
992
993 if (last_pfn > max_arch_pfn)
994 last_pfn = max_arch_pfn;
995 if (last_pfn > end_user_pfn)
996 last_pfn = end_user_pfn;
997
Paul Jacksone2fc2522008-06-22 07:22:12 -0700998 printk(KERN_INFO "last_pfn = 0x%lx max_arch_pfn = 0x%lx\n",
Yinghai Luee0c80f2008-06-03 19:34:00 -0700999 last_pfn, max_arch_pfn);
1000 return last_pfn;
1001}
1002
1003/*
1004 * Finds an active region in the address range from start_pfn to last_pfn and
1005 * returns its range in ei_startpfn and ei_endpfn for the e820 entry.
1006 */
1007int __init e820_find_active_region(const struct e820entry *ei,
1008 unsigned long start_pfn,
1009 unsigned long last_pfn,
1010 unsigned long *ei_startpfn,
1011 unsigned long *ei_endpfn)
1012{
1013 u64 align = PAGE_SIZE;
1014
1015 *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT;
1016 *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT;
1017
1018 /* Skip map entries smaller than a page */
1019 if (*ei_startpfn >= *ei_endpfn)
1020 return 0;
1021
1022 /* Skip if map is outside the node */
1023 if (ei->type != E820_RAM || *ei_endpfn <= start_pfn ||
1024 *ei_startpfn >= last_pfn)
1025 return 0;
1026
1027 /* Check for overlaps */
1028 if (*ei_startpfn < start_pfn)
1029 *ei_startpfn = start_pfn;
1030 if (*ei_endpfn > last_pfn)
1031 *ei_endpfn = last_pfn;
1032
1033 /* Obey end_user_pfn to save on memmap */
1034 if (*ei_startpfn >= end_user_pfn)
1035 return 0;
1036 if (*ei_endpfn > end_user_pfn)
1037 *ei_endpfn = end_user_pfn;
1038
1039 return 1;
1040}
1041
1042/* Walk the e820 map and register active regions within a node */
1043void __init e820_register_active_regions(int nid, unsigned long start_pfn,
1044 unsigned long last_pfn)
1045{
1046 unsigned long ei_startpfn;
1047 unsigned long ei_endpfn;
1048 int i;
1049
1050 for (i = 0; i < e820.nr_map; i++)
1051 if (e820_find_active_region(&e820.map[i],
1052 start_pfn, last_pfn,
1053 &ei_startpfn, &ei_endpfn))
1054 add_active_range(nid, ei_startpfn, ei_endpfn);
1055}
1056
1057/*
1058 * Find the hole size (in bytes) in the memory range.
1059 * @start: starting address of the memory range to scan
1060 * @end: ending address of the memory range to scan
1061 */
1062u64 __init e820_hole_size(u64 start, u64 end)
1063{
1064 unsigned long start_pfn = start >> PAGE_SHIFT;
1065 unsigned long last_pfn = end >> PAGE_SHIFT;
1066 unsigned long ei_startpfn, ei_endpfn, ram = 0;
1067 int i;
1068
1069 for (i = 0; i < e820.nr_map; i++) {
1070 if (e820_find_active_region(&e820.map[i],
1071 start_pfn, last_pfn,
1072 &ei_startpfn, &ei_endpfn))
1073 ram += ei_endpfn - ei_startpfn;
1074 }
1075 return end - start - ((u64)ram << PAGE_SHIFT);
1076}
Yinghai Luab4a4652008-06-10 12:55:54 -07001077
1078static void early_panic(char *msg)
1079{
1080 early_printk(msg);
1081 panic(msg);
1082}
1083
1084/* "mem=nopentium" disables the 4MB page tables. */
1085static int __init parse_memopt(char *p)
1086{
1087 u64 mem_size;
1088
1089 if (!p)
1090 return -EINVAL;
1091
1092#ifdef CONFIG_X86_32
1093 if (!strcmp(p, "nopentium")) {
1094 setup_clear_cpu_cap(X86_FEATURE_PSE);
1095 return 0;
1096 }
1097#endif
1098
1099 mem_size = memparse(p, &p);
1100 end_user_pfn = mem_size>>PAGE_SHIFT;
1101 return 0;
1102}
1103early_param("mem", parse_memopt);
1104
1105static int userdef __initdata;
1106
1107static int __init parse_memmap_opt(char *p)
1108{
1109 char *oldp;
1110 u64 start_at, mem_size;
1111
1112 if (!strcmp(p, "exactmap")) {
1113#ifdef CONFIG_CRASH_DUMP
1114 /*
1115 * If we are doing a crash dump, we still need to know
1116 * the real mem size before original memory map is
1117 * reset.
1118 */
1119 e820_register_active_regions(0, 0, -1UL);
1120 saved_max_pfn = e820_end_of_ram();
1121 remove_all_active_ranges();
1122#endif
1123 e820.nr_map = 0;
1124 userdef = 1;
1125 return 0;
1126 }
1127
1128 oldp = p;
1129 mem_size = memparse(p, &p);
1130 if (p == oldp)
1131 return -EINVAL;
1132
1133 userdef = 1;
1134 if (*p == '@') {
1135 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001136 e820_add_region(start_at, mem_size, E820_RAM);
Yinghai Luab4a4652008-06-10 12:55:54 -07001137 } else if (*p == '#') {
1138 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001139 e820_add_region(start_at, mem_size, E820_ACPI);
Yinghai Luab4a4652008-06-10 12:55:54 -07001140 } else if (*p == '$') {
1141 start_at = memparse(p+1, &p);
Yinghai Lud0be6bd2008-06-15 18:58:51 -07001142 e820_add_region(start_at, mem_size, E820_RESERVED);
Yinghai Luab4a4652008-06-10 12:55:54 -07001143 } else {
1144 end_user_pfn = (mem_size >> PAGE_SHIFT);
1145 }
1146 return *p == '\0' ? 0 : -EINVAL;
1147}
1148early_param("memmap", parse_memmap_opt);
1149
1150void __init finish_e820_parsing(void)
1151{
1152 if (userdef) {
1153 int nr = e820.nr_map;
1154
1155 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0)
1156 early_panic("Invalid user supplied memory map");
1157 e820.nr_map = nr;
1158
1159 printk(KERN_INFO "user-defined physical RAM map:\n");
1160 e820_print_map("user");
1161 }
1162}
Yinghai Lu41c094f2008-06-16 13:03:31 -07001163
1164/*
1165 * Mark e820 reserved areas as busy for the resource manager.
1166 */
1167void __init e820_reserve_resources(void)
1168{
1169 int i;
1170 struct resource *res;
1171
1172 res = alloc_bootmem_low(sizeof(struct resource) * e820.nr_map);
1173 for (i = 0; i < e820.nr_map; i++) {
1174 switch (e820.map[i].type) {
1175 case E820_RAM: res->name = "System RAM"; break;
1176 case E820_ACPI: res->name = "ACPI Tables"; break;
1177 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
1178 default: res->name = "reserved";
1179 }
1180 res->start = e820.map[i].addr;
1181 res->end = res->start + e820.map[i].size - 1;
1182#ifndef CONFIG_RESOURCES_64BIT
1183 if (res->end > 0x100000000ULL) {
1184 res++;
1185 continue;
1186 }
1187#endif
1188 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
1189 insert_resource(&iomem_resource, res);
1190 res++;
1191 }
1192}
1193
Yinghai Lu95a71a42008-06-18 17:27:08 -07001194char *__init default_machine_specific_memory_setup(void)
Yinghai Lu064d25f2008-06-16 19:58:28 -07001195{
1196 char *who = "BIOS-e820";
1197 int new_nr;
1198 /*
1199 * Try to copy the BIOS-supplied E820-map.
1200 *
1201 * Otherwise fake a memory map; one section from 0k->640k,
1202 * the next section from 1mb->appropriate_mem_k
1203 */
1204 new_nr = boot_params.e820_entries;
1205 sanitize_e820_map(boot_params.e820_map,
1206 ARRAY_SIZE(boot_params.e820_map),
1207 &new_nr);
1208 boot_params.e820_entries = new_nr;
1209 if (copy_e820_map(boot_params.e820_map, boot_params.e820_entries) < 0) {
Yinghai Lu95a71a42008-06-18 17:27:08 -07001210 u64 mem_size;
Yinghai Lu41c094f2008-06-16 13:03:31 -07001211
Yinghai Lu064d25f2008-06-16 19:58:28 -07001212 /* compare results from other methods and take the greater */
1213 if (boot_params.alt_mem_k
1214 < boot_params.screen_info.ext_mem_k) {
1215 mem_size = boot_params.screen_info.ext_mem_k;
1216 who = "BIOS-88";
1217 } else {
1218 mem_size = boot_params.alt_mem_k;
1219 who = "BIOS-e801";
1220 }
1221
1222 e820.nr_map = 0;
1223 e820_add_region(0, LOWMEMSIZE(), E820_RAM);
1224 e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
Yinghai Lu064d25f2008-06-16 19:58:28 -07001225 }
1226
1227 /* In case someone cares... */
1228 return who;
1229}
1230
Yinghai Lu95a71a42008-06-18 17:27:08 -07001231char *__init __attribute__((weak)) machine_specific_memory_setup(void)
1232{
1233 return default_machine_specific_memory_setup();
1234}
1235
Yinghai Lu064d25f2008-06-16 19:58:28 -07001236/* Overridden in paravirt.c if CONFIG_PARAVIRT */
1237char * __init __attribute__((weak)) memory_setup(void)
1238{
1239 return machine_specific_memory_setup();
1240}
1241
1242void __init setup_memory_map(void)
1243{
1244 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
1245 e820_print_map(memory_setup());
1246}
1247
1248#ifdef CONFIG_X86_64
1249int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
1250{
1251 int i;
1252
1253 if (slot < 0 || slot >= e820.nr_map)
1254 return -1;
1255 for (i = slot; i < e820.nr_map; i++) {
1256 if (e820.map[i].type != E820_RAM)
1257 continue;
1258 break;
1259 }
1260 if (i == e820.nr_map || e820.map[i].addr > (max_pfn << PAGE_SHIFT))
1261 return -1;
1262 *addr = e820.map[i].addr;
1263 *size = min_t(u64, e820.map[i].size + e820.map[i].addr,
1264 max_pfn << PAGE_SHIFT) - *addr;
1265 return i + 1;
1266}
1267#endif