1 /* 2 * Low level x86 E820 memory map handling functions. 3 * 4 * The firmware and bootloader passes us the "E820 table", which is the primary 5 * physical memory layout description available about x86 systems. 6 * 7 * The kernel takes the E820 memory layout and optionally modifies it with 8 * quirks and other tweaks, and feeds that into the generic Linux memory 9 * allocation code routines via a platform independent interface (memblock, etc.). 10 */ 11 #include <linux/crash_dump.h> 12 #include <linux/bootmem.h> 13 #include <linux/suspend.h> 14 #include <linux/acpi.h> 15 #include <linux/firmware-map.h> 16 #include <linux/memblock.h> 17 #include <linux/sort.h> 18 19 #include <asm/e820/api.h> 20 #include <asm/setup.h> 21 22 /* 23 * We organize the E820 table into two main data structures: 24 * 25 * - 'e820_table_firmware': the original firmware version passed to us by the 26 * bootloader - not modified by the kernel. We use this to: 27 * 28 * - inform the user about the firmware's notion of memory layout 29 * via /sys/firmware/memmap 30 * 31 * - the hibernation code uses it to generate a kernel-independent MD5 32 * fingerprint of the physical memory layout of a system. 33 * 34 * - kexec, which is a bootloader in disguise, uses the original E820 35 * layout to pass to the kexec-ed kernel. This way the original kernel 36 * can have a restricted E820 map while the kexec()-ed kexec-kernel 37 * can have access to full memory - etc. 38 * 39 * - 'e820_table': this is the main E820 table that is massaged by the 40 * low level x86 platform code, or modified by boot parameters, before 41 * passed on to higher level MM layers. 42 * 43 * Once the E820 map has been converted to the standard Linux memory layout 44 * information its role stops - modifying it has no effect and does not get 45 * re-propagated. So itsmain role is a temporary bootstrap storage of firmware 46 * specific memory layout data during early bootup. 47 */ 48 static struct e820_table e820_table_init __initdata; 49 static struct e820_table e820_table_firmware_init __initdata; 50 51 struct e820_table *e820_table __refdata = &e820_table_init; 52 struct e820_table *e820_table_firmware __refdata = &e820_table_firmware_init; 53 54 /* For PCI or other memory-mapped resources */ 55 unsigned long pci_mem_start = 0xaeedbabe; 56 #ifdef CONFIG_PCI 57 EXPORT_SYMBOL(pci_mem_start); 58 #endif 59 60 /* 61 * This function checks if any part of the range <start,end> is mapped 62 * with type. 63 */ 64 bool e820__mapped_any(u64 start, u64 end, enum e820_type type) 65 { 66 int i; 67 68 for (i = 0; i < e820_table->nr_entries; i++) { 69 struct e820_entry *entry = &e820_table->entries[i]; 70 71 if (type && entry->type != type) 72 continue; 73 if (entry->addr >= end || entry->addr + entry->size <= start) 74 continue; 75 return 1; 76 } 77 return 0; 78 } 79 EXPORT_SYMBOL_GPL(e820__mapped_any); 80 81 /* 82 * This function checks if the entire <start,end> range is mapped with 'type'. 83 * 84 * Note: this function only works correctly once the E820 table is sorted and 85 * not-overlapping (at least for the range specified), which is the case normally. 86 */ 87 bool __init e820__mapped_all(u64 start, u64 end, enum e820_type type) 88 { 89 int i; 90 91 for (i = 0; i < e820_table->nr_entries; i++) { 92 struct e820_entry *entry = &e820_table->entries[i]; 93 94 if (type && entry->type != type) 95 continue; 96 97 /* Is the region (part) in overlap with the current region? */ 98 if (entry->addr >= end || entry->addr + entry->size <= start) 99 continue; 100 101 /* 102 * If the region is at the beginning of <start,end> we move 103 * 'start' to the end of the region since it's ok until there 104 */ 105 if (entry->addr <= start) 106 start = entry->addr + entry->size; 107 108 /* 109 * If 'start' is now at or beyond 'end', we're done, full 110 * coverage of the desired range exists: 111 */ 112 if (start >= end) 113 return 1; 114 } 115 return 0; 116 } 117 118 /* 119 * Add a memory region to the kernel E820 map. 120 */ 121 static void __init __e820__range_add(struct e820_table *table, u64 start, u64 size, enum e820_type type) 122 { 123 int x = table->nr_entries; 124 125 if (x >= ARRAY_SIZE(table->entries)) { 126 pr_err("e820: too many entries; ignoring [mem %#010llx-%#010llx]\n", start, start + size - 1); 127 return; 128 } 129 130 table->entries[x].addr = start; 131 table->entries[x].size = size; 132 table->entries[x].type = type; 133 table->nr_entries++; 134 } 135 136 void __init e820__range_add(u64 start, u64 size, enum e820_type type) 137 { 138 __e820__range_add(e820_table, start, size, type); 139 } 140 141 static void __init e820_print_type(enum e820_type type) 142 { 143 switch (type) { 144 case E820_TYPE_RAM: /* Fall through: */ 145 case E820_TYPE_RESERVED_KERN: pr_cont("usable"); break; 146 case E820_TYPE_RESERVED: pr_cont("reserved"); break; 147 case E820_TYPE_ACPI: pr_cont("ACPI data"); break; 148 case E820_TYPE_NVS: pr_cont("ACPI NVS"); break; 149 case E820_TYPE_UNUSABLE: pr_cont("unusable"); break; 150 case E820_TYPE_PMEM: /* Fall through: */ 151 case E820_TYPE_PRAM: pr_cont("persistent (type %u)", type); break; 152 default: pr_cont("type %u", type); break; 153 } 154 } 155 156 void __init e820__print_table(char *who) 157 { 158 int i; 159 160 for (i = 0; i < e820_table->nr_entries; i++) { 161 pr_info("%s: [mem %#018Lx-%#018Lx] ", who, 162 e820_table->entries[i].addr, 163 e820_table->entries[i].addr + e820_table->entries[i].size - 1); 164 165 e820_print_type(e820_table->entries[i].type); 166 pr_cont("\n"); 167 } 168 } 169 170 /* 171 * Sanitize an E820 map. 172 * 173 * Some E820 layouts include overlapping entries. The following 174 * replaces the original E820 map with a new one, removing overlaps, 175 * and resolving conflicting memory types in favor of highest 176 * numbered type. 177 * 178 * The input parameter 'entries' points to an array of 'struct 179 * e820_entry' which on entry has elements in the range [0, *nr_entries) 180 * valid, and which has space for up to max_nr_entries entries. 181 * On return, the resulting sanitized E820 map entries will be in 182 * overwritten in the same location, starting at 'entries'. 183 * 184 * The integer pointed to by nr_entries must be valid on entry (the 185 * current number of valid entries located at 'entries'). If the 186 * sanitizing succeeds the *nr_entries will be updated with the new 187 * number of valid entries (something no more than max_nr_entries). 188 * 189 * The return value from e820__update_table() is zero if it 190 * successfully 'sanitized' the map entries passed in, and is -1 191 * if it did nothing, which can happen if either of (1) it was 192 * only passed one map entry, or (2) any of the input map entries 193 * were invalid (start + size < start, meaning that the size was 194 * so big the described memory range wrapped around through zero.) 195 * 196 * Visually we're performing the following 197 * (1,2,3,4 = memory types)... 198 * 199 * Sample memory map (w/overlaps): 200 * ____22__________________ 201 * ______________________4_ 202 * ____1111________________ 203 * _44_____________________ 204 * 11111111________________ 205 * ____________________33__ 206 * ___________44___________ 207 * __________33333_________ 208 * ______________22________ 209 * ___________________2222_ 210 * _________111111111______ 211 * _____________________11_ 212 * _________________4______ 213 * 214 * Sanitized equivalent (no overlap): 215 * 1_______________________ 216 * _44_____________________ 217 * ___1____________________ 218 * ____22__________________ 219 * ______11________________ 220 * _________1______________ 221 * __________3_____________ 222 * ___________44___________ 223 * _____________33_________ 224 * _______________2________ 225 * ________________1_______ 226 * _________________4______ 227 * ___________________2____ 228 * ____________________33__ 229 * ______________________4_ 230 */ 231 struct change_member { 232 /* Pointer to the original entry: */ 233 struct e820_entry *entry; 234 /* Address for this change point: */ 235 unsigned long long addr; 236 }; 237 238 static struct change_member change_point_list[2*E820_MAX_ENTRIES] __initdata; 239 static struct change_member *change_point[2*E820_MAX_ENTRIES] __initdata; 240 static struct e820_entry *overlap_list[E820_MAX_ENTRIES] __initdata; 241 static struct e820_entry new_entries[E820_MAX_ENTRIES] __initdata; 242 243 static int __init cpcompare(const void *a, const void *b) 244 { 245 struct change_member * const *app = a, * const *bpp = b; 246 const struct change_member *ap = *app, *bp = *bpp; 247 248 /* 249 * Inputs are pointers to two elements of change_point[]. If their 250 * addresses are not equal, their difference dominates. If the addresses 251 * are equal, then consider one that represents the end of its region 252 * to be greater than one that does not. 253 */ 254 if (ap->addr != bp->addr) 255 return ap->addr > bp->addr ? 1 : -1; 256 257 return (ap->addr != ap->entry->addr) - (bp->addr != bp->entry->addr); 258 } 259 260 int __init e820__update_table(struct e820_table *table) 261 { 262 struct e820_entry *entries = table->entries; 263 u32 max_nr_entries = ARRAY_SIZE(table->entries); 264 enum e820_type current_type, last_type; 265 unsigned long long last_addr; 266 u32 new_nr_entries, overlap_entries; 267 u32 i, chg_idx, chg_nr; 268 269 /* If there's only one memory region, don't bother: */ 270 if (table->nr_entries < 2) 271 return -1; 272 273 BUG_ON(table->nr_entries > max_nr_entries); 274 275 /* Bail out if we find any unreasonable addresses in the map: */ 276 for (i = 0; i < table->nr_entries; i++) { 277 if (entries[i].addr + entries[i].size < entries[i].addr) 278 return -1; 279 } 280 281 /* Create pointers for initial change-point information (for sorting): */ 282 for (i = 0; i < 2 * table->nr_entries; i++) 283 change_point[i] = &change_point_list[i]; 284 285 /* 286 * Record all known change-points (starting and ending addresses), 287 * omitting empty memory regions: 288 */ 289 chg_idx = 0; 290 for (i = 0; i < table->nr_entries; i++) { 291 if (entries[i].size != 0) { 292 change_point[chg_idx]->addr = entries[i].addr; 293 change_point[chg_idx++]->entry = &entries[i]; 294 change_point[chg_idx]->addr = entries[i].addr + entries[i].size; 295 change_point[chg_idx++]->entry = &entries[i]; 296 } 297 } 298 chg_nr = chg_idx; 299 300 /* Sort change-point list by memory addresses (low -> high): */ 301 sort(change_point, chg_nr, sizeof(*change_point), cpcompare, NULL); 302 303 /* Create a new memory map, removing overlaps: */ 304 overlap_entries = 0; /* Number of entries in the overlap table */ 305 new_nr_entries = 0; /* Index for creating new map entries */ 306 last_type = 0; /* Start with undefined memory type */ 307 last_addr = 0; /* Start with 0 as last starting address */ 308 309 /* Loop through change-points, determining effect on the new map: */ 310 for (chg_idx = 0; chg_idx < chg_nr; chg_idx++) { 311 /* Keep track of all overlapping entries */ 312 if (change_point[chg_idx]->addr == change_point[chg_idx]->entry->addr) { 313 /* Add map entry to overlap list (> 1 entry implies an overlap) */ 314 overlap_list[overlap_entries++] = change_point[chg_idx]->entry; 315 } else { 316 /* Remove entry from list (order independent, so swap with last): */ 317 for (i = 0; i < overlap_entries; i++) { 318 if (overlap_list[i] == change_point[chg_idx]->entry) 319 overlap_list[i] = overlap_list[overlap_entries-1]; 320 } 321 overlap_entries--; 322 } 323 /* 324 * If there are overlapping entries, decide which 325 * "type" to use (larger value takes precedence -- 326 * 1=usable, 2,3,4,4+=unusable) 327 */ 328 current_type = 0; 329 for (i = 0; i < overlap_entries; i++) { 330 if (overlap_list[i]->type > current_type) 331 current_type = overlap_list[i]->type; 332 } 333 334 /* Continue building up new map based on this information: */ 335 if (current_type != last_type || current_type == E820_TYPE_PRAM) { 336 if (last_type != 0) { 337 new_entries[new_nr_entries].size = change_point[chg_idx]->addr - last_addr; 338 /* Move forward only if the new size was non-zero: */ 339 if (new_entries[new_nr_entries].size != 0) 340 /* No more space left for new entries? */ 341 if (++new_nr_entries >= max_nr_entries) 342 break; 343 } 344 if (current_type != 0) { 345 new_entries[new_nr_entries].addr = change_point[chg_idx]->addr; 346 new_entries[new_nr_entries].type = current_type; 347 last_addr = change_point[chg_idx]->addr; 348 } 349 last_type = current_type; 350 } 351 } 352 353 /* Copy the new entries into the original location: */ 354 memcpy(entries, new_entries, new_nr_entries*sizeof(*entries)); 355 table->nr_entries = new_nr_entries; 356 357 return 0; 358 } 359 360 static int __init __append_e820_table(struct boot_e820_entry *entries, u32 nr_entries) 361 { 362 struct boot_e820_entry *entry = entries; 363 364 while (nr_entries) { 365 u64 start = entry->addr; 366 u64 size = entry->size; 367 u64 end = start + size - 1; 368 u32 type = entry->type; 369 370 /* Ignore the entry on 64-bit overflow: */ 371 if (start > end && likely(size)) 372 return -1; 373 374 e820__range_add(start, size, type); 375 376 entry++; 377 nr_entries--; 378 } 379 return 0; 380 } 381 382 /* 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 */ 391 static int __init append_e820_table(struct boot_e820_entry *entries, u32 nr_entries) 392 { 393 /* Only one memory region (or negative)? Ignore it */ 394 if (nr_entries < 2) 395 return -1; 396 397 return __append_e820_table(entries, nr_entries); 398 } 399 400 static u64 __init 401 __e820__range_update(struct e820_table *table, u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) 402 { 403 u64 end; 404 unsigned int i; 405 u64 real_updated_size = 0; 406 407 BUG_ON(old_type == new_type); 408 409 if (size > (ULLONG_MAX - start)) 410 size = ULLONG_MAX - start; 411 412 end = start + size; 413 printk(KERN_DEBUG "e820: update [mem %#010Lx-%#010Lx] ", start, end - 1); 414 e820_print_type(old_type); 415 pr_cont(" ==> "); 416 e820_print_type(new_type); 417 pr_cont("\n"); 418 419 for (i = 0; i < table->nr_entries; i++) { 420 struct e820_entry *entry = &table->entries[i]; 421 u64 final_start, final_end; 422 u64 entry_end; 423 424 if (entry->type != old_type) 425 continue; 426 427 entry_end = entry->addr + entry->size; 428 429 /* Completely covered by new range? */ 430 if (entry->addr >= start && entry_end <= end) { 431 entry->type = new_type; 432 real_updated_size += entry->size; 433 continue; 434 } 435 436 /* New range is completely covered? */ 437 if (entry->addr < start && entry_end > end) { 438 __e820__range_add(table, start, size, new_type); 439 __e820__range_add(table, end, entry_end - end, entry->type); 440 entry->size = start - entry->addr; 441 real_updated_size += size; 442 continue; 443 } 444 445 /* Partially covered: */ 446 final_start = max(start, entry->addr); 447 final_end = min(end, entry_end); 448 if (final_start >= final_end) 449 continue; 450 451 __e820__range_add(table, final_start, final_end - final_start, new_type); 452 453 real_updated_size += final_end - final_start; 454 455 /* 456 * Left range could be head or tail, so need to update 457 * its size first: 458 */ 459 entry->size -= final_end - final_start; 460 if (entry->addr < final_start) 461 continue; 462 463 entry->addr = final_end; 464 } 465 return real_updated_size; 466 } 467 468 u64 __init e820__range_update(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) 469 { 470 return __e820__range_update(e820_table, start, size, old_type, new_type); 471 } 472 473 static u64 __init e820__range_update_firmware(u64 start, u64 size, enum e820_type old_type, enum e820_type new_type) 474 { 475 return __e820__range_update(e820_table_firmware, start, size, old_type, new_type); 476 } 477 478 /* Remove a range of memory from the E820 table: */ 479 u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type) 480 { 481 int i; 482 u64 end; 483 u64 real_removed_size = 0; 484 485 if (size > (ULLONG_MAX - start)) 486 size = ULLONG_MAX - start; 487 488 end = start + size; 489 printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1); 490 if (check_type) 491 e820_print_type(old_type); 492 pr_cont("\n"); 493 494 for (i = 0; i < e820_table->nr_entries; i++) { 495 struct e820_entry *entry = &e820_table->entries[i]; 496 u64 final_start, final_end; 497 u64 entry_end; 498 499 if (check_type && entry->type != old_type) 500 continue; 501 502 entry_end = entry->addr + entry->size; 503 504 /* Completely covered? */ 505 if (entry->addr >= start && entry_end <= end) { 506 real_removed_size += entry->size; 507 memset(entry, 0, sizeof(*entry)); 508 continue; 509 } 510 511 /* Is the new range completely covered? */ 512 if (entry->addr < start && entry_end > end) { 513 e820__range_add(end, entry_end - end, entry->type); 514 entry->size = start - entry->addr; 515 real_removed_size += size; 516 continue; 517 } 518 519 /* Partially covered: */ 520 final_start = max(start, entry->addr); 521 final_end = min(end, entry_end); 522 if (final_start >= final_end) 523 continue; 524 525 real_removed_size += final_end - final_start; 526 527 /* 528 * Left range could be head or tail, so need to update 529 * the size first: 530 */ 531 entry->size -= final_end - final_start; 532 if (entry->addr < final_start) 533 continue; 534 535 entry->addr = final_end; 536 } 537 return real_removed_size; 538 } 539 540 void __init e820__update_table_print(void) 541 { 542 if (e820__update_table(e820_table)) 543 return; 544 545 pr_info("e820: modified physical RAM map:\n"); 546 e820__print_table("modified"); 547 } 548 549 static void __init e820__update_table_firmware(void) 550 { 551 e820__update_table(e820_table_firmware); 552 } 553 554 #define MAX_GAP_END 0x100000000ull 555 556 /* 557 * Search for a gap in the E820 memory space from 0 to MAX_GAP_END (4GB). 558 */ 559 static int __init e820_search_gap(unsigned long *gapstart, unsigned long *gapsize) 560 { 561 unsigned long long last = MAX_GAP_END; 562 int i = e820_table->nr_entries; 563 int found = 0; 564 565 while (--i >= 0) { 566 unsigned long long start = e820_table->entries[i].addr; 567 unsigned long long end = start + e820_table->entries[i].size; 568 569 /* 570 * Since "last" is at most 4GB, we know we'll 571 * fit in 32 bits if this condition is true: 572 */ 573 if (last > end) { 574 unsigned long gap = last - end; 575 576 if (gap >= *gapsize) { 577 *gapsize = gap; 578 *gapstart = end; 579 found = 1; 580 } 581 } 582 if (start < last) 583 last = start; 584 } 585 return found; 586 } 587 588 /* 589 * Search for the biggest gap in the low 32 bits of the E820 590 * memory space. We pass this space to the PCI subsystem, so 591 * that it can assign MMIO resources for hotplug or 592 * unconfigured devices in. 593 * 594 * Hopefully the BIOS let enough space left. 595 */ 596 __init void e820__setup_pci_gap(void) 597 { 598 unsigned long gapstart, gapsize; 599 int found; 600 601 gapsize = 0x400000; 602 found = e820_search_gap(&gapstart, &gapsize); 603 604 if (!found) { 605 #ifdef CONFIG_X86_64 606 gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024; 607 pr_err( 608 "e820: Cannot find an available gap in the 32-bit address range\n" 609 "e820: PCI devices with unassigned 32-bit BARs may not work!\n"); 610 #else 611 gapstart = 0x10000000; 612 #endif 613 } 614 615 /* 616 * e820__reserve_resources_late() protects stolen RAM already: 617 */ 618 pci_mem_start = gapstart; 619 620 pr_info("e820: [mem %#010lx-%#010lx] available for PCI devices\n", gapstart, gapstart + gapsize - 1); 621 } 622 623 /* 624 * Called late during init, in free_initmem(). 625 * 626 * Initial e820_table and e820_table_firmware are largish __initdata arrays. 627 * 628 * Copy them to a (usually much smaller) dynamically allocated area that is 629 * sized precisely after the number of e820 entries. 630 * 631 * This is done after we've performed all the fixes and tweaks to the tables. 632 * All functions which modify them are __init functions, which won't exist 633 * after free_initmem(). 634 */ 635 __init void e820__reallocate_tables(void) 636 { 637 struct e820_table *n; 638 int size; 639 640 size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table->nr_entries; 641 n = kmalloc(size, GFP_KERNEL); 642 BUG_ON(!n); 643 memcpy(n, e820_table, size); 644 e820_table = n; 645 646 size = offsetof(struct e820_table, entries) + sizeof(struct e820_entry)*e820_table_firmware->nr_entries; 647 n = kmalloc(size, GFP_KERNEL); 648 BUG_ON(!n); 649 memcpy(n, e820_table_firmware, size); 650 e820_table_firmware = n; 651 } 652 653 /* 654 * Because of the small fixed size of struct boot_params, only the first 655 * 128 E820 memory entries are passed to the kernel via boot_params.e820_table, 656 * the remaining (if any) entries are passed via the SETUP_E820_EXT node of 657 * struct setup_data, which is parsed here. 658 */ 659 void __init e820__memory_setup_extended(u64 phys_addr, u32 data_len) 660 { 661 int entries; 662 struct boot_e820_entry *extmap; 663 struct setup_data *sdata; 664 665 sdata = early_memremap(phys_addr, data_len); 666 entries = sdata->len / sizeof(*extmap); 667 extmap = (struct boot_e820_entry *)(sdata->data); 668 669 __append_e820_table(extmap, entries); 670 e820__update_table(e820_table); 671 672 early_memunmap(sdata, data_len); 673 pr_info("e820: extended physical RAM map:\n"); 674 e820__print_table("extended"); 675 } 676 677 /* 678 * Find the ranges of physical addresses that do not correspond to 679 * E820 RAM areas and register the corresponding pages as 'nosave' for 680 * hibernation (32-bit) or software suspend and suspend to RAM (64-bit). 681 * 682 * This function requires the E820 map to be sorted and without any 683 * overlapping entries. 684 */ 685 void __init e820__register_nosave_regions(unsigned long limit_pfn) 686 { 687 int i; 688 unsigned long pfn = 0; 689 690 for (i = 0; i < e820_table->nr_entries; i++) { 691 struct e820_entry *entry = &e820_table->entries[i]; 692 693 if (pfn < PFN_UP(entry->addr)) 694 register_nosave_region(pfn, PFN_UP(entry->addr)); 695 696 pfn = PFN_DOWN(entry->addr + entry->size); 697 698 if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN) 699 register_nosave_region(PFN_UP(entry->addr), pfn); 700 701 if (pfn >= limit_pfn) 702 break; 703 } 704 } 705 706 #ifdef CONFIG_ACPI 707 /* 708 * Register ACPI NVS memory regions, so that we can save/restore them during 709 * hibernation and the subsequent resume: 710 */ 711 static int __init e820__register_nvs_regions(void) 712 { 713 int i; 714 715 for (i = 0; i < e820_table->nr_entries; i++) { 716 struct e820_entry *entry = &e820_table->entries[i]; 717 718 if (entry->type == E820_TYPE_NVS) 719 acpi_nvs_register(entry->addr, entry->size); 720 } 721 722 return 0; 723 } 724 core_initcall(e820__register_nvs_regions); 725 #endif 726 727 /* 728 * Allocate the requested number of bytes with the requsted alignment 729 * and return (the physical address) to the caller. Also register this 730 * range in the 'firmware' E820 table as a reserved range. 731 * 732 * This allows kexec to fake a new mptable, as if it came from the real 733 * system. 734 */ 735 u64 __init e820__memblock_alloc_reserved(u64 size, u64 align) 736 { 737 u64 addr; 738 739 addr = __memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); 740 if (addr) { 741 e820__range_update_firmware(addr, size, E820_TYPE_RAM, E820_TYPE_RESERVED); 742 pr_info("e820: update e820_table_firmware for e820__memblock_alloc_reserved()\n"); 743 e820__update_table_firmware(); 744 } 745 746 return addr; 747 } 748 749 #ifdef CONFIG_X86_32 750 # ifdef CONFIG_X86_PAE 751 # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT)) 752 # else 753 # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT)) 754 # endif 755 #else /* CONFIG_X86_32 */ 756 # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT 757 #endif 758 759 /* 760 * Find the highest page frame number we have available 761 */ 762 static unsigned long __init e820_end_pfn(unsigned long limit_pfn, enum e820_type type) 763 { 764 int i; 765 unsigned long last_pfn = 0; 766 unsigned long max_arch_pfn = MAX_ARCH_PFN; 767 768 for (i = 0; i < e820_table->nr_entries; i++) { 769 struct e820_entry *entry = &e820_table->entries[i]; 770 unsigned long start_pfn; 771 unsigned long end_pfn; 772 773 if (entry->type != type) 774 continue; 775 776 start_pfn = entry->addr >> PAGE_SHIFT; 777 end_pfn = (entry->addr + entry->size) >> PAGE_SHIFT; 778 779 if (start_pfn >= limit_pfn) 780 continue; 781 if (end_pfn > limit_pfn) { 782 last_pfn = limit_pfn; 783 break; 784 } 785 if (end_pfn > last_pfn) 786 last_pfn = end_pfn; 787 } 788 789 if (last_pfn > max_arch_pfn) 790 last_pfn = max_arch_pfn; 791 792 pr_info("e820: last_pfn = %#lx max_arch_pfn = %#lx\n", 793 last_pfn, max_arch_pfn); 794 return last_pfn; 795 } 796 797 unsigned long __init e820__end_of_ram_pfn(void) 798 { 799 return e820_end_pfn(MAX_ARCH_PFN, E820_TYPE_RAM); 800 } 801 802 unsigned long __init e820__end_of_low_ram_pfn(void) 803 { 804 return e820_end_pfn(1UL << (32 - PAGE_SHIFT), E820_TYPE_RAM); 805 } 806 807 static void __init early_panic(char *msg) 808 { 809 early_printk(msg); 810 panic(msg); 811 } 812 813 static int userdef __initdata; 814 815 /* The "mem=nopentium" boot option disables 4MB page tables on 32-bit kernels: */ 816 static int __init parse_memopt(char *p) 817 { 818 u64 mem_size; 819 820 if (!p) 821 return -EINVAL; 822 823 if (!strcmp(p, "nopentium")) { 824 #ifdef CONFIG_X86_32 825 setup_clear_cpu_cap(X86_FEATURE_PSE); 826 return 0; 827 #else 828 pr_warn("mem=nopentium ignored! (only supported on x86_32)\n"); 829 return -EINVAL; 830 #endif 831 } 832 833 userdef = 1; 834 mem_size = memparse(p, &p); 835 836 /* Don't remove all memory when getting "mem={invalid}" parameter: */ 837 if (mem_size == 0) 838 return -EINVAL; 839 840 e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1); 841 842 return 0; 843 } 844 early_param("mem", parse_memopt); 845 846 static int __init parse_memmap_one(char *p) 847 { 848 char *oldp; 849 u64 start_at, mem_size; 850 851 if (!p) 852 return -EINVAL; 853 854 if (!strncmp(p, "exactmap", 8)) { 855 #ifdef CONFIG_CRASH_DUMP 856 /* 857 * If we are doing a crash dump, we still need to know 858 * the real memory size before the original memory map is 859 * reset. 860 */ 861 saved_max_pfn = e820__end_of_ram_pfn(); 862 #endif 863 e820_table->nr_entries = 0; 864 userdef = 1; 865 return 0; 866 } 867 868 oldp = p; 869 mem_size = memparse(p, &p); 870 if (p == oldp) 871 return -EINVAL; 872 873 userdef = 1; 874 if (*p == '@') { 875 start_at = memparse(p+1, &p); 876 e820__range_add(start_at, mem_size, E820_TYPE_RAM); 877 } else if (*p == '#') { 878 start_at = memparse(p+1, &p); 879 e820__range_add(start_at, mem_size, E820_TYPE_ACPI); 880 } else if (*p == '$') { 881 start_at = memparse(p+1, &p); 882 e820__range_add(start_at, mem_size, E820_TYPE_RESERVED); 883 } else if (*p == '!') { 884 start_at = memparse(p+1, &p); 885 e820__range_add(start_at, mem_size, E820_TYPE_PRAM); 886 } else { 887 e820__range_remove(mem_size, ULLONG_MAX - mem_size, E820_TYPE_RAM, 1); 888 } 889 890 return *p == '\0' ? 0 : -EINVAL; 891 } 892 893 static int __init parse_memmap_opt(char *str) 894 { 895 while (str) { 896 char *k = strchr(str, ','); 897 898 if (k) 899 *k++ = 0; 900 901 parse_memmap_one(str); 902 str = k; 903 } 904 905 return 0; 906 } 907 early_param("memmap", parse_memmap_opt); 908 909 /* 910 * Reserve all entries from the bootloader's extensible data nodes list, 911 * because if present we are going to use it later on to fetch e820 912 * entries from it: 913 */ 914 void __init e820__reserve_setup_data(void) 915 { 916 struct setup_data *data; 917 u64 pa_data; 918 919 pa_data = boot_params.hdr.setup_data; 920 if (!pa_data) 921 return; 922 923 while (pa_data) { 924 data = early_memremap(pa_data, sizeof(*data)); 925 e820__range_update(pa_data, sizeof(*data)+data->len, E820_TYPE_RAM, E820_TYPE_RESERVED_KERN); 926 pa_data = data->next; 927 early_memunmap(data, sizeof(*data)); 928 } 929 930 e820__update_table(e820_table); 931 932 memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware)); 933 934 pr_info("extended physical RAM map:\n"); 935 e820__print_table("reserve setup_data"); 936 } 937 938 /* 939 * Called after parse_early_param(), after early parameters (such as mem=) 940 * have been processed, in which case we already have an E820 table filled in 941 * via the parameter callback function(s), but it's not sorted and printed yet: 942 */ 943 void __init e820__finish_early_params(void) 944 { 945 if (userdef) { 946 if (e820__update_table(e820_table) < 0) 947 early_panic("Invalid user supplied memory map"); 948 949 pr_info("e820: user-defined physical RAM map:\n"); 950 e820__print_table("user"); 951 } 952 } 953 954 static const char *__init e820_type_to_string(struct e820_entry *entry) 955 { 956 switch (entry->type) { 957 case E820_TYPE_RESERVED_KERN: /* Fall-through: */ 958 case E820_TYPE_RAM: return "System RAM"; 959 case E820_TYPE_ACPI: return "ACPI Tables"; 960 case E820_TYPE_NVS: return "ACPI Non-volatile Storage"; 961 case E820_TYPE_UNUSABLE: return "Unusable memory"; 962 case E820_TYPE_PRAM: return "Persistent Memory (legacy)"; 963 case E820_TYPE_PMEM: return "Persistent Memory"; 964 case E820_TYPE_RESERVED: return "Reserved"; 965 default: return "Unknown E820 type"; 966 } 967 } 968 969 static unsigned long __init e820_type_to_iomem_type(struct e820_entry *entry) 970 { 971 switch (entry->type) { 972 case E820_TYPE_RESERVED_KERN: /* Fall-through: */ 973 case E820_TYPE_RAM: return IORESOURCE_SYSTEM_RAM; 974 case E820_TYPE_ACPI: /* Fall-through: */ 975 case E820_TYPE_NVS: /* Fall-through: */ 976 case E820_TYPE_UNUSABLE: /* Fall-through: */ 977 case E820_TYPE_PRAM: /* Fall-through: */ 978 case E820_TYPE_PMEM: /* Fall-through: */ 979 case E820_TYPE_RESERVED: /* Fall-through: */ 980 default: return IORESOURCE_MEM; 981 } 982 } 983 984 static unsigned long __init e820_type_to_iores_desc(struct e820_entry *entry) 985 { 986 switch (entry->type) { 987 case E820_TYPE_ACPI: return IORES_DESC_ACPI_TABLES; 988 case E820_TYPE_NVS: return IORES_DESC_ACPI_NV_STORAGE; 989 case E820_TYPE_PMEM: return IORES_DESC_PERSISTENT_MEMORY; 990 case E820_TYPE_PRAM: return IORES_DESC_PERSISTENT_MEMORY_LEGACY; 991 case E820_TYPE_RESERVED_KERN: /* Fall-through: */ 992 case E820_TYPE_RAM: /* Fall-through: */ 993 case E820_TYPE_UNUSABLE: /* Fall-through: */ 994 case E820_TYPE_RESERVED: /* Fall-through: */ 995 default: return IORES_DESC_NONE; 996 } 997 } 998 999 static bool __init do_mark_busy(enum e820_type type, struct resource *res) 1000 { 1001 /* this is the legacy bios/dos rom-shadow + mmio region */ 1002 if (res->start < (1ULL<<20)) 1003 return true; 1004 1005 /* 1006 * Treat persistent memory like device memory, i.e. reserve it 1007 * for exclusive use of a driver 1008 */ 1009 switch (type) { 1010 case E820_TYPE_RESERVED: 1011 case E820_TYPE_PRAM: 1012 case E820_TYPE_PMEM: 1013 return false; 1014 case E820_TYPE_RESERVED_KERN: 1015 case E820_TYPE_RAM: 1016 case E820_TYPE_ACPI: 1017 case E820_TYPE_NVS: 1018 case E820_TYPE_UNUSABLE: 1019 default: 1020 return true; 1021 } 1022 } 1023 1024 /* 1025 * Mark E820 reserved areas as busy for the resource manager: 1026 */ 1027 1028 static struct resource __initdata *e820_res; 1029 1030 void __init e820__reserve_resources(void) 1031 { 1032 int i; 1033 struct resource *res; 1034 u64 end; 1035 1036 res = alloc_bootmem(sizeof(*res) * e820_table->nr_entries); 1037 e820_res = res; 1038 1039 for (i = 0; i < e820_table->nr_entries; i++) { 1040 struct e820_entry *entry = e820_table->entries + i; 1041 1042 end = entry->addr + entry->size - 1; 1043 if (end != (resource_size_t)end) { 1044 res++; 1045 continue; 1046 } 1047 res->start = entry->addr; 1048 res->end = end; 1049 res->name = e820_type_to_string(entry); 1050 res->flags = e820_type_to_iomem_type(entry); 1051 res->desc = e820_type_to_iores_desc(entry); 1052 1053 /* 1054 * Don't register the region that could be conflicted with 1055 * PCI device BAR resources and insert them later in 1056 * pcibios_resource_survey(): 1057 */ 1058 if (do_mark_busy(entry->type, res)) { 1059 res->flags |= IORESOURCE_BUSY; 1060 insert_resource(&iomem_resource, res); 1061 } 1062 res++; 1063 } 1064 1065 for (i = 0; i < e820_table_firmware->nr_entries; i++) { 1066 struct e820_entry *entry = e820_table_firmware->entries + i; 1067 1068 firmware_map_add_early(entry->addr, entry->addr + entry->size, e820_type_to_string(entry)); 1069 } 1070 } 1071 1072 /* 1073 * How much should we pad the end of RAM, depending on where it is? 1074 */ 1075 static unsigned long __init ram_alignment(resource_size_t pos) 1076 { 1077 unsigned long mb = pos >> 20; 1078 1079 /* To 64kB in the first megabyte */ 1080 if (!mb) 1081 return 64*1024; 1082 1083 /* To 1MB in the first 16MB */ 1084 if (mb < 16) 1085 return 1024*1024; 1086 1087 /* To 64MB for anything above that */ 1088 return 64*1024*1024; 1089 } 1090 1091 #define MAX_RESOURCE_SIZE ((resource_size_t)-1) 1092 1093 void __init e820__reserve_resources_late(void) 1094 { 1095 int i; 1096 struct resource *res; 1097 1098 res = e820_res; 1099 for (i = 0; i < e820_table->nr_entries; i++) { 1100 if (!res->parent && res->end) 1101 insert_resource_expand_to_fit(&iomem_resource, res); 1102 res++; 1103 } 1104 1105 /* 1106 * Try to bump up RAM regions to reasonable boundaries, to 1107 * avoid stolen RAM: 1108 */ 1109 for (i = 0; i < e820_table->nr_entries; i++) { 1110 struct e820_entry *entry = &e820_table->entries[i]; 1111 u64 start, end; 1112 1113 if (entry->type != E820_TYPE_RAM) 1114 continue; 1115 1116 start = entry->addr + entry->size; 1117 end = round_up(start, ram_alignment(start)) - 1; 1118 if (end > MAX_RESOURCE_SIZE) 1119 end = MAX_RESOURCE_SIZE; 1120 if (start >= end) 1121 continue; 1122 1123 printk(KERN_DEBUG "e820: reserve RAM buffer [mem %#010llx-%#010llx]\n", start, end); 1124 reserve_region_with_split(&iomem_resource, start, end, "RAM buffer"); 1125 } 1126 } 1127 1128 /* 1129 * Pass the firmware (bootloader) E820 map to the kernel and process it: 1130 */ 1131 char *__init e820__memory_setup_default(void) 1132 { 1133 char *who = "BIOS-e820"; 1134 1135 /* 1136 * Try to copy the BIOS-supplied E820-map. 1137 * 1138 * Otherwise fake a memory map; one section from 0k->640k, 1139 * the next section from 1mb->appropriate_mem_k 1140 */ 1141 if (append_e820_table(boot_params.e820_table, boot_params.e820_entries) < 0) { 1142 u64 mem_size; 1143 1144 /* Compare results from other methods and take the one that gives more RAM: */ 1145 if (boot_params.alt_mem_k < boot_params.screen_info.ext_mem_k) { 1146 mem_size = boot_params.screen_info.ext_mem_k; 1147 who = "BIOS-88"; 1148 } else { 1149 mem_size = boot_params.alt_mem_k; 1150 who = "BIOS-e801"; 1151 } 1152 1153 e820_table->nr_entries = 0; 1154 e820__range_add(0, LOWMEMSIZE(), E820_TYPE_RAM); 1155 e820__range_add(HIGH_MEMORY, mem_size << 10, E820_TYPE_RAM); 1156 } 1157 1158 /* We just appended a lot of ranges, sanitize the table: */ 1159 e820__update_table(e820_table); 1160 1161 return who; 1162 } 1163 1164 /* 1165 * Calls e820__memory_setup_default() in essence to pick up the firmware/bootloader 1166 * E820 map - with an optional platform quirk available for virtual platforms 1167 * to override this method of boot environment processing: 1168 */ 1169 void __init e820__memory_setup(void) 1170 { 1171 char *who; 1172 1173 /* This is a firmware interface ABI - make sure we don't break it: */ 1174 BUILD_BUG_ON(sizeof(struct boot_e820_entry) != 20); 1175 1176 who = x86_init.resources.memory_setup(); 1177 1178 memcpy(e820_table_firmware, e820_table, sizeof(*e820_table_firmware)); 1179 1180 pr_info("e820: BIOS-provided physical RAM map:\n"); 1181 e820__print_table(who); 1182 } 1183 1184 void __init e820__memblock_setup(void) 1185 { 1186 int i; 1187 u64 end; 1188 1189 /* 1190 * The bootstrap memblock region count maximum is 128 entries 1191 * (INIT_MEMBLOCK_REGIONS), but EFI might pass us more E820 entries 1192 * than that - so allow memblock resizing. 1193 * 1194 * This is safe, because this call happens pretty late during x86 setup, 1195 * so we know about reserved memory regions already. (This is important 1196 * so that memblock resizing does no stomp over reserved areas.) 1197 */ 1198 memblock_allow_resize(); 1199 1200 for (i = 0; i < e820_table->nr_entries; i++) { 1201 struct e820_entry *entry = &e820_table->entries[i]; 1202 1203 end = entry->addr + entry->size; 1204 if (end != (resource_size_t)end) 1205 continue; 1206 1207 if (entry->type != E820_TYPE_RAM && entry->type != E820_TYPE_RESERVED_KERN) 1208 continue; 1209 1210 memblock_add(entry->addr, entry->size); 1211 } 1212 1213 /* Throw away partial pages: */ 1214 memblock_trim_memory(PAGE_SIZE); 1215 1216 memblock_dump_all(); 1217 } 1218