bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/types.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/bootmem.h> |
| 5 | #include <linux/ioport.h> |
| 6 | #include <linux/string.h> |
| 7 | #include <linux/kexec.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/efi.h> |
bibo,mao | b2dff6a | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 11 | #include <linux/pfn.h> |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 12 | |
| 13 | #include <asm/pgtable.h> |
| 14 | #include <asm/page.h> |
| 15 | #include <asm/e820.h> |
| 16 | |
| 17 | #ifdef CONFIG_EFI |
| 18 | int efi_enabled = 0; |
| 19 | EXPORT_SYMBOL(efi_enabled); |
| 20 | #endif |
| 21 | |
| 22 | struct e820map e820; |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 23 | struct change_member { |
| 24 | struct e820entry *pbios; /* pointer to original bios entry */ |
| 25 | unsigned long long addr; /* address for this change point */ |
| 26 | }; |
| 27 | static struct change_member change_point_list[2*E820MAX] __initdata; |
| 28 | static struct change_member *change_point[2*E820MAX] __initdata; |
| 29 | static struct e820entry *overlap_list[E820MAX] __initdata; |
| 30 | static struct e820entry new_bios[E820MAX] __initdata; |
bibo,mao | b5b2405 | 2006-12-07 02:14:06 +0100 | [diff] [blame^] | 31 | /* For PCI or other memory-mapped resources */ |
| 32 | unsigned long pci_mem_start = 0x10000000; |
| 33 | #ifdef CONFIG_PCI |
| 34 | EXPORT_SYMBOL(pci_mem_start); |
| 35 | #endif |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 36 | struct resource data_resource = { |
| 37 | .name = "Kernel data", |
| 38 | .start = 0, |
| 39 | .end = 0, |
| 40 | .flags = IORESOURCE_BUSY | IORESOURCE_MEM |
| 41 | }; |
| 42 | |
| 43 | struct resource code_resource = { |
| 44 | .name = "Kernel code", |
| 45 | .start = 0, |
| 46 | .end = 0, |
| 47 | .flags = IORESOURCE_BUSY | IORESOURCE_MEM |
| 48 | }; |
| 49 | |
| 50 | static struct resource system_rom_resource = { |
| 51 | .name = "System ROM", |
| 52 | .start = 0xf0000, |
| 53 | .end = 0xfffff, |
| 54 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 55 | }; |
| 56 | |
| 57 | static struct resource extension_rom_resource = { |
| 58 | .name = "Extension ROM", |
| 59 | .start = 0xe0000, |
| 60 | .end = 0xeffff, |
| 61 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 62 | }; |
| 63 | |
| 64 | static struct resource adapter_rom_resources[] = { { |
| 65 | .name = "Adapter ROM", |
| 66 | .start = 0xc8000, |
| 67 | .end = 0, |
| 68 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 69 | }, { |
| 70 | .name = "Adapter ROM", |
| 71 | .start = 0, |
| 72 | .end = 0, |
| 73 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 74 | }, { |
| 75 | .name = "Adapter ROM", |
| 76 | .start = 0, |
| 77 | .end = 0, |
| 78 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 79 | }, { |
| 80 | .name = "Adapter ROM", |
| 81 | .start = 0, |
| 82 | .end = 0, |
| 83 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 84 | }, { |
| 85 | .name = "Adapter ROM", |
| 86 | .start = 0, |
| 87 | .end = 0, |
| 88 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 89 | }, { |
| 90 | .name = "Adapter ROM", |
| 91 | .start = 0, |
| 92 | .end = 0, |
| 93 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 94 | } }; |
| 95 | |
| 96 | static struct resource video_rom_resource = { |
| 97 | .name = "Video ROM", |
| 98 | .start = 0xc0000, |
| 99 | .end = 0xc7fff, |
| 100 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 101 | }; |
| 102 | |
| 103 | static struct resource video_ram_resource = { |
| 104 | .name = "Video RAM area", |
| 105 | .start = 0xa0000, |
| 106 | .end = 0xbffff, |
| 107 | .flags = IORESOURCE_BUSY | IORESOURCE_MEM |
| 108 | }; |
| 109 | |
| 110 | static struct resource standard_io_resources[] = { { |
| 111 | .name = "dma1", |
| 112 | .start = 0x0000, |
| 113 | .end = 0x001f, |
| 114 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 115 | }, { |
| 116 | .name = "pic1", |
| 117 | .start = 0x0020, |
| 118 | .end = 0x0021, |
| 119 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 120 | }, { |
| 121 | .name = "timer0", |
| 122 | .start = 0x0040, |
| 123 | .end = 0x0043, |
| 124 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 125 | }, { |
| 126 | .name = "timer1", |
| 127 | .start = 0x0050, |
| 128 | .end = 0x0053, |
| 129 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 130 | }, { |
| 131 | .name = "keyboard", |
| 132 | .start = 0x0060, |
| 133 | .end = 0x006f, |
| 134 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 135 | }, { |
| 136 | .name = "dma page reg", |
| 137 | .start = 0x0080, |
| 138 | .end = 0x008f, |
| 139 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 140 | }, { |
| 141 | .name = "pic2", |
| 142 | .start = 0x00a0, |
| 143 | .end = 0x00a1, |
| 144 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 145 | }, { |
| 146 | .name = "dma2", |
| 147 | .start = 0x00c0, |
| 148 | .end = 0x00df, |
| 149 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 150 | }, { |
| 151 | .name = "fpu", |
| 152 | .start = 0x00f0, |
| 153 | .end = 0x00ff, |
| 154 | .flags = IORESOURCE_BUSY | IORESOURCE_IO |
| 155 | } }; |
| 156 | |
| 157 | #define romsignature(x) (*(unsigned short *)(x) == 0xaa55) |
| 158 | |
| 159 | static int __init romchecksum(unsigned char *rom, unsigned long length) |
| 160 | { |
| 161 | unsigned char *p, sum = 0; |
| 162 | |
| 163 | for (p = rom; p < rom + length; p++) |
| 164 | sum += *p; |
| 165 | return sum == 0; |
| 166 | } |
| 167 | |
| 168 | static void __init probe_roms(void) |
| 169 | { |
| 170 | unsigned long start, length, upper; |
| 171 | unsigned char *rom; |
| 172 | int i; |
| 173 | |
| 174 | /* video rom */ |
| 175 | upper = adapter_rom_resources[0].start; |
| 176 | for (start = video_rom_resource.start; start < upper; start += 2048) { |
| 177 | rom = isa_bus_to_virt(start); |
| 178 | if (!romsignature(rom)) |
| 179 | continue; |
| 180 | |
| 181 | video_rom_resource.start = start; |
| 182 | |
| 183 | /* 0 < length <= 0x7f * 512, historically */ |
| 184 | length = rom[2] * 512; |
| 185 | |
| 186 | /* if checksum okay, trust length byte */ |
| 187 | if (length && romchecksum(rom, length)) |
| 188 | video_rom_resource.end = start + length - 1; |
| 189 | |
| 190 | request_resource(&iomem_resource, &video_rom_resource); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | start = (video_rom_resource.end + 1 + 2047) & ~2047UL; |
| 195 | if (start < upper) |
| 196 | start = upper; |
| 197 | |
| 198 | /* system rom */ |
| 199 | request_resource(&iomem_resource, &system_rom_resource); |
| 200 | upper = system_rom_resource.start; |
| 201 | |
| 202 | /* check for extension rom (ignore length byte!) */ |
| 203 | rom = isa_bus_to_virt(extension_rom_resource.start); |
| 204 | if (romsignature(rom)) { |
| 205 | length = extension_rom_resource.end - extension_rom_resource.start + 1; |
| 206 | if (romchecksum(rom, length)) { |
| 207 | request_resource(&iomem_resource, &extension_rom_resource); |
| 208 | upper = extension_rom_resource.start; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* check for adapter roms on 2k boundaries */ |
| 213 | for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) { |
| 214 | rom = isa_bus_to_virt(start); |
| 215 | if (!romsignature(rom)) |
| 216 | continue; |
| 217 | |
| 218 | /* 0 < length <= 0x7f * 512, historically */ |
| 219 | length = rom[2] * 512; |
| 220 | |
| 221 | /* but accept any length that fits if checksum okay */ |
| 222 | if (!length || start + length > upper || !romchecksum(rom, length)) |
| 223 | continue; |
| 224 | |
| 225 | adapter_rom_resources[i].start = start; |
| 226 | adapter_rom_resources[i].end = start + length - 1; |
| 227 | request_resource(&iomem_resource, &adapter_rom_resources[i]); |
| 228 | |
| 229 | start = adapter_rom_resources[i++].end & ~2047UL; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Request address space for all standard RAM and ROM resources |
| 235 | * and also for regions reported as reserved by the e820. |
| 236 | */ |
| 237 | static void __init |
| 238 | legacy_init_iomem_resources(struct resource *code_resource, struct resource *data_resource) |
| 239 | { |
| 240 | int i; |
| 241 | |
| 242 | probe_roms(); |
| 243 | for (i = 0; i < e820.nr_map; i++) { |
| 244 | struct resource *res; |
| 245 | #ifndef CONFIG_RESOURCES_64BIT |
| 246 | if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL) |
| 247 | continue; |
| 248 | #endif |
| 249 | res = kzalloc(sizeof(struct resource), GFP_ATOMIC); |
| 250 | switch (e820.map[i].type) { |
| 251 | case E820_RAM: res->name = "System RAM"; break; |
| 252 | case E820_ACPI: res->name = "ACPI Tables"; break; |
| 253 | case E820_NVS: res->name = "ACPI Non-volatile Storage"; break; |
| 254 | default: res->name = "reserved"; |
| 255 | } |
| 256 | res->start = e820.map[i].addr; |
| 257 | res->end = res->start + e820.map[i].size - 1; |
| 258 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
| 259 | if (request_resource(&iomem_resource, res)) { |
| 260 | kfree(res); |
| 261 | continue; |
| 262 | } |
| 263 | if (e820.map[i].type == E820_RAM) { |
| 264 | /* |
| 265 | * We don't know which RAM region contains kernel data, |
| 266 | * so we try it repeatedly and let the resource manager |
| 267 | * test it. |
| 268 | */ |
| 269 | request_resource(res, code_resource); |
| 270 | request_resource(res, data_resource); |
| 271 | #ifdef CONFIG_KEXEC |
| 272 | request_resource(res, &crashk_res); |
| 273 | #endif |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Request address space for all standard resources |
| 280 | * |
| 281 | * This is called just before pcibios_init(), which is also a |
| 282 | * subsys_initcall, but is linked in later (in arch/i386/pci/common.c). |
| 283 | */ |
| 284 | static int __init request_standard_resources(void) |
| 285 | { |
| 286 | int i; |
| 287 | |
| 288 | printk("Setting up standard PCI resources\n"); |
| 289 | if (efi_enabled) |
| 290 | efi_initialize_iomem_resources(&code_resource, &data_resource); |
| 291 | else |
| 292 | legacy_init_iomem_resources(&code_resource, &data_resource); |
| 293 | |
| 294 | /* EFI systems may still have VGA */ |
| 295 | request_resource(&iomem_resource, &video_ram_resource); |
| 296 | |
| 297 | /* request I/O space for devices used on all i[345]86 PCs */ |
| 298 | for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++) |
| 299 | request_resource(&ioport_resource, &standard_io_resources[i]); |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | subsys_initcall(request_standard_resources); |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 304 | |
| 305 | void __init add_memory_region(unsigned long long start, |
| 306 | unsigned long long size, int type) |
| 307 | { |
| 308 | int x; |
| 309 | |
| 310 | if (!efi_enabled) { |
| 311 | x = e820.nr_map; |
| 312 | |
| 313 | if (x == E820MAX) { |
| 314 | printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | e820.map[x].addr = start; |
| 319 | e820.map[x].size = size; |
| 320 | e820.map[x].type = type; |
| 321 | e820.nr_map++; |
| 322 | } |
| 323 | } /* add_memory_region */ |
| 324 | |
| 325 | /* |
| 326 | * Sanitize the BIOS e820 map. |
| 327 | * |
| 328 | * Some e820 responses include overlapping entries. The following |
| 329 | * replaces the original e820 map with a new one, removing overlaps. |
| 330 | * |
| 331 | */ |
| 332 | int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map) |
| 333 | { |
| 334 | struct change_member *change_tmp; |
| 335 | unsigned long current_type, last_type; |
| 336 | unsigned long long last_addr; |
| 337 | int chgidx, still_changing; |
| 338 | int overlap_entries; |
| 339 | int new_bios_entry; |
| 340 | int old_nr, new_nr, chg_nr; |
| 341 | int i; |
| 342 | |
| 343 | /* |
| 344 | Visually we're performing the following (1,2,3,4 = memory types)... |
| 345 | |
| 346 | Sample memory map (w/overlaps): |
| 347 | ____22__________________ |
| 348 | ______________________4_ |
| 349 | ____1111________________ |
| 350 | _44_____________________ |
| 351 | 11111111________________ |
| 352 | ____________________33__ |
| 353 | ___________44___________ |
| 354 | __________33333_________ |
| 355 | ______________22________ |
| 356 | ___________________2222_ |
| 357 | _________111111111______ |
| 358 | _____________________11_ |
| 359 | _________________4______ |
| 360 | |
| 361 | Sanitized equivalent (no overlap): |
| 362 | 1_______________________ |
| 363 | _44_____________________ |
| 364 | ___1____________________ |
| 365 | ____22__________________ |
| 366 | ______11________________ |
| 367 | _________1______________ |
| 368 | __________3_____________ |
| 369 | ___________44___________ |
| 370 | _____________33_________ |
| 371 | _______________2________ |
| 372 | ________________1_______ |
| 373 | _________________4______ |
| 374 | ___________________2____ |
| 375 | ____________________33__ |
| 376 | ______________________4_ |
| 377 | */ |
| 378 | printk("sanitize start\n"); |
| 379 | /* if there's only one memory region, don't bother */ |
| 380 | if (*pnr_map < 2) { |
| 381 | printk("sanitize bail 0\n"); |
| 382 | return -1; |
| 383 | } |
| 384 | |
| 385 | old_nr = *pnr_map; |
| 386 | |
| 387 | /* bail out if we find any unreasonable addresses in bios map */ |
| 388 | for (i=0; i<old_nr; i++) |
| 389 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) { |
| 390 | printk("sanitize bail 1\n"); |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | /* create pointers for initial change-point information (for sorting) */ |
| 395 | for (i=0; i < 2*old_nr; i++) |
| 396 | change_point[i] = &change_point_list[i]; |
| 397 | |
| 398 | /* record all known change-points (starting and ending addresses), |
| 399 | omitting those that are for empty memory regions */ |
| 400 | chgidx = 0; |
| 401 | for (i=0; i < old_nr; i++) { |
| 402 | if (biosmap[i].size != 0) { |
| 403 | change_point[chgidx]->addr = biosmap[i].addr; |
| 404 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 405 | change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size; |
| 406 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 407 | } |
| 408 | } |
| 409 | chg_nr = chgidx; /* true number of change-points */ |
| 410 | |
| 411 | /* sort change-point list by memory addresses (low -> high) */ |
| 412 | still_changing = 1; |
| 413 | while (still_changing) { |
| 414 | still_changing = 0; |
| 415 | for (i=1; i < chg_nr; i++) { |
| 416 | /* if <current_addr> > <last_addr>, swap */ |
| 417 | /* or, if current=<start_addr> & last=<end_addr>, swap */ |
| 418 | if ((change_point[i]->addr < change_point[i-1]->addr) || |
| 419 | ((change_point[i]->addr == change_point[i-1]->addr) && |
| 420 | (change_point[i]->addr == change_point[i]->pbios->addr) && |
| 421 | (change_point[i-1]->addr != change_point[i-1]->pbios->addr)) |
| 422 | ) |
| 423 | { |
| 424 | change_tmp = change_point[i]; |
| 425 | change_point[i] = change_point[i-1]; |
| 426 | change_point[i-1] = change_tmp; |
| 427 | still_changing=1; |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* create a new bios memory map, removing overlaps */ |
| 433 | overlap_entries=0; /* number of entries in the overlap table */ |
| 434 | new_bios_entry=0; /* index for creating new bios map entries */ |
| 435 | last_type = 0; /* start with undefined memory type */ |
| 436 | last_addr = 0; /* start with 0 as last starting address */ |
| 437 | /* loop through change-points, determining affect on the new bios map */ |
| 438 | for (chgidx=0; chgidx < chg_nr; chgidx++) |
| 439 | { |
| 440 | /* keep track of all overlapping bios entries */ |
| 441 | if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr) |
| 442 | { |
| 443 | /* add map entry to overlap list (> 1 entry implies an overlap) */ |
| 444 | overlap_list[overlap_entries++]=change_point[chgidx]->pbios; |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | /* remove entry from list (order independent, so swap with last) */ |
| 449 | for (i=0; i<overlap_entries; i++) |
| 450 | { |
| 451 | if (overlap_list[i] == change_point[chgidx]->pbios) |
| 452 | overlap_list[i] = overlap_list[overlap_entries-1]; |
| 453 | } |
| 454 | overlap_entries--; |
| 455 | } |
| 456 | /* if there are overlapping entries, decide which "type" to use */ |
| 457 | /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */ |
| 458 | current_type = 0; |
| 459 | for (i=0; i<overlap_entries; i++) |
| 460 | if (overlap_list[i]->type > current_type) |
| 461 | current_type = overlap_list[i]->type; |
| 462 | /* continue building up new bios map based on this information */ |
| 463 | if (current_type != last_type) { |
| 464 | if (last_type != 0) { |
| 465 | new_bios[new_bios_entry].size = |
| 466 | change_point[chgidx]->addr - last_addr; |
| 467 | /* move forward only if the new size was non-zero */ |
| 468 | if (new_bios[new_bios_entry].size != 0) |
| 469 | if (++new_bios_entry >= E820MAX) |
| 470 | break; /* no more space left for new bios entries */ |
| 471 | } |
| 472 | if (current_type != 0) { |
| 473 | new_bios[new_bios_entry].addr = change_point[chgidx]->addr; |
| 474 | new_bios[new_bios_entry].type = current_type; |
| 475 | last_addr=change_point[chgidx]->addr; |
| 476 | } |
| 477 | last_type = current_type; |
| 478 | } |
| 479 | } |
| 480 | new_nr = new_bios_entry; /* retain count for new bios entries */ |
| 481 | |
| 482 | /* copy new bios mapping into original location */ |
| 483 | memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry)); |
| 484 | *pnr_map = new_nr; |
| 485 | |
| 486 | printk("sanitize end\n"); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | /* |
| 491 | * Copy the BIOS e820 map into a safe place. |
| 492 | * |
| 493 | * Sanity-check it while we're at it.. |
| 494 | * |
| 495 | * If we're lucky and live on a modern system, the setup code |
| 496 | * will have given us a memory map that we can use to properly |
| 497 | * set up memory. If we aren't, we'll fake a memory map. |
| 498 | * |
| 499 | * We check to see that the memory map contains at least 2 elements |
| 500 | * before we'll use it, because the detection code in setup.S may |
| 501 | * not be perfect and most every PC known to man has two memory |
| 502 | * regions: one from 0 to 640k, and one from 1mb up. (The IBM |
| 503 | * thinkpad 560x, for example, does not cooperate with the memory |
| 504 | * detection code.) |
| 505 | */ |
| 506 | int __init copy_e820_map(struct e820entry * biosmap, int nr_map) |
| 507 | { |
| 508 | /* Only one memory region (or negative)? Ignore it */ |
| 509 | if (nr_map < 2) |
| 510 | return -1; |
| 511 | |
| 512 | do { |
| 513 | unsigned long long start = biosmap->addr; |
| 514 | unsigned long long size = biosmap->size; |
| 515 | unsigned long long end = start + size; |
| 516 | unsigned long type = biosmap->type; |
| 517 | printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, type); |
| 518 | |
| 519 | /* Overflow in 64 bits? Ignore the memory map. */ |
| 520 | if (start > end) |
| 521 | return -1; |
| 522 | |
| 523 | /* |
| 524 | * Some BIOSes claim RAM in the 640k - 1M region. |
| 525 | * Not right. Fix it up. |
| 526 | */ |
| 527 | if (type == E820_RAM) { |
| 528 | printk("copy_e820_map() type is E820_RAM\n"); |
| 529 | if (start < 0x100000ULL && end > 0xA0000ULL) { |
| 530 | printk("copy_e820_map() lies in range...\n"); |
| 531 | if (start < 0xA0000ULL) { |
| 532 | printk("copy_e820_map() start < 0xA0000ULL\n"); |
| 533 | add_memory_region(start, 0xA0000ULL-start, type); |
| 534 | } |
| 535 | if (end <= 0x100000ULL) { |
| 536 | printk("copy_e820_map() end <= 0x100000ULL\n"); |
| 537 | continue; |
| 538 | } |
| 539 | start = 0x100000ULL; |
| 540 | size = end - start; |
| 541 | } |
| 542 | } |
| 543 | add_memory_region(start, size, type); |
| 544 | } while (biosmap++,--nr_map); |
| 545 | return 0; |
| 546 | } |
| 547 | |
bibo,mao | b2dff6a | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 548 | /* |
| 549 | * Callback for efi_memory_walk. |
| 550 | */ |
| 551 | static int __init |
| 552 | efi_find_max_pfn(unsigned long start, unsigned long end, void *arg) |
| 553 | { |
| 554 | unsigned long *max_pfn = arg, pfn; |
| 555 | |
| 556 | if (start < end) { |
| 557 | pfn = PFN_UP(end -1); |
| 558 | if (pfn > *max_pfn) |
| 559 | *max_pfn = pfn; |
| 560 | } |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | static int __init |
| 565 | efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg) |
| 566 | { |
| 567 | memory_present(0, PFN_UP(start), PFN_DOWN(end)); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* |
| 572 | * Find the highest page frame number we have available |
| 573 | */ |
| 574 | void __init find_max_pfn(void) |
| 575 | { |
| 576 | int i; |
| 577 | |
| 578 | max_pfn = 0; |
| 579 | if (efi_enabled) { |
| 580 | efi_memmap_walk(efi_find_max_pfn, &max_pfn); |
| 581 | efi_memmap_walk(efi_memory_present_wrapper, NULL); |
| 582 | return; |
| 583 | } |
| 584 | |
| 585 | for (i = 0; i < e820.nr_map; i++) { |
| 586 | unsigned long start, end; |
| 587 | /* RAM? */ |
| 588 | if (e820.map[i].type != E820_RAM) |
| 589 | continue; |
| 590 | start = PFN_UP(e820.map[i].addr); |
| 591 | end = PFN_DOWN(e820.map[i].addr + e820.map[i].size); |
| 592 | if (start >= end) |
| 593 | continue; |
| 594 | if (end > max_pfn) |
| 595 | max_pfn = end; |
| 596 | memory_present(0, start, end); |
| 597 | } |
| 598 | } |
bibo,mao | b5b2405 | 2006-12-07 02:14:06 +0100 | [diff] [blame^] | 599 | |
| 600 | /* |
| 601 | * Free all available memory for boot time allocation. Used |
| 602 | * as a callback function by efi_memory_walk() |
| 603 | */ |
| 604 | |
| 605 | static int __init |
| 606 | free_available_memory(unsigned long start, unsigned long end, void *arg) |
| 607 | { |
| 608 | /* check max_low_pfn */ |
| 609 | if (start >= (max_low_pfn << PAGE_SHIFT)) |
| 610 | return 0; |
| 611 | if (end >= (max_low_pfn << PAGE_SHIFT)) |
| 612 | end = max_low_pfn << PAGE_SHIFT; |
| 613 | if (start < end) |
| 614 | free_bootmem(start, end - start); |
| 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | /* |
| 619 | * Register fully available low RAM pages with the bootmem allocator. |
| 620 | */ |
| 621 | void __init register_bootmem_low_pages(unsigned long max_low_pfn) |
| 622 | { |
| 623 | int i; |
| 624 | |
| 625 | if (efi_enabled) { |
| 626 | efi_memmap_walk(free_available_memory, NULL); |
| 627 | return; |
| 628 | } |
| 629 | for (i = 0; i < e820.nr_map; i++) { |
| 630 | unsigned long curr_pfn, last_pfn, size; |
| 631 | /* |
| 632 | * Reserve usable low memory |
| 633 | */ |
| 634 | if (e820.map[i].type != E820_RAM) |
| 635 | continue; |
| 636 | /* |
| 637 | * We are rounding up the start address of usable memory: |
| 638 | */ |
| 639 | curr_pfn = PFN_UP(e820.map[i].addr); |
| 640 | if (curr_pfn >= max_low_pfn) |
| 641 | continue; |
| 642 | /* |
| 643 | * ... and at the end of the usable range downwards: |
| 644 | */ |
| 645 | last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size); |
| 646 | |
| 647 | if (last_pfn > max_low_pfn) |
| 648 | last_pfn = max_low_pfn; |
| 649 | |
| 650 | /* |
| 651 | * .. finally, did all the rounding and playing |
| 652 | * around just make the area go away? |
| 653 | */ |
| 654 | if (last_pfn <= curr_pfn) |
| 655 | continue; |
| 656 | |
| 657 | size = last_pfn - curr_pfn; |
| 658 | free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size)); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | void __init register_memory(void) |
| 663 | { |
| 664 | unsigned long gapstart, gapsize, round; |
| 665 | unsigned long long last; |
| 666 | int i; |
| 667 | |
| 668 | /* |
| 669 | * Search for the bigest gap in the low 32 bits of the e820 |
| 670 | * memory space. |
| 671 | */ |
| 672 | last = 0x100000000ull; |
| 673 | gapstart = 0x10000000; |
| 674 | gapsize = 0x400000; |
| 675 | i = e820.nr_map; |
| 676 | while (--i >= 0) { |
| 677 | unsigned long long start = e820.map[i].addr; |
| 678 | unsigned long long end = start + e820.map[i].size; |
| 679 | |
| 680 | /* |
| 681 | * Since "last" is at most 4GB, we know we'll |
| 682 | * fit in 32 bits if this condition is true |
| 683 | */ |
| 684 | if (last > end) { |
| 685 | unsigned long gap = last - end; |
| 686 | |
| 687 | if (gap > gapsize) { |
| 688 | gapsize = gap; |
| 689 | gapstart = end; |
| 690 | } |
| 691 | } |
| 692 | if (start < last) |
| 693 | last = start; |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * See how much we want to round up: start off with |
| 698 | * rounding to the next 1MB area. |
| 699 | */ |
| 700 | round = 0x100000; |
| 701 | while ((gapsize >> 4) > round) |
| 702 | round += round; |
| 703 | /* Fun with two's complement */ |
| 704 | pci_mem_start = (gapstart + round) & -round; |
| 705 | |
| 706 | printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n", |
| 707 | pci_mem_start, gapstart, gapsize); |
| 708 | } |