Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Handle the memory map. |
| 3 | * The functions here do the job until bootmem takes over. |
| 4 | * |
| 5 | * Getting sanitize_e820_map() in sync with i386 version by applying change: |
| 6 | * - Provisions for empty E820 memory regions (reported by certain BIOSes). |
| 7 | * Alex Achenbach <xela@slit.de>, December 2002. |
| 8 | * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> |
| 9 | * |
| 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/bootmem.h> |
| 15 | #include <linux/ioport.h> |
| 16 | #include <linux/string.h> |
| 17 | #include <linux/kexec.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/mm.h> |
| 20 | #include <linux/pfn.h> |
| 21 | |
| 22 | #include <asm/pgtable.h> |
| 23 | #include <asm/page.h> |
| 24 | #include <asm/e820.h> |
| 25 | #include <asm/setup.h> |
| 26 | |
| 27 | struct e820map e820; |
| 28 | |
| 29 | /* For PCI or other memory-mapped resources */ |
| 30 | unsigned long pci_mem_start = 0xaeedbabe; |
| 31 | #ifdef CONFIG_PCI |
| 32 | EXPORT_SYMBOL(pci_mem_start); |
| 33 | #endif |
| 34 | |
| 35 | /* |
| 36 | * This function checks if any part of the range <start,end> is mapped |
| 37 | * with type. |
| 38 | */ |
| 39 | int |
| 40 | e820_any_mapped(u64 start, u64 end, unsigned type) |
| 41 | { |
| 42 | int i; |
| 43 | |
| 44 | for (i = 0; i < e820.nr_map; i++) { |
| 45 | struct e820entry *ei = &e820.map[i]; |
| 46 | |
| 47 | if (type && ei->type != type) |
| 48 | continue; |
| 49 | if (ei->addr >= end || ei->addr + ei->size <= start) |
| 50 | continue; |
| 51 | return 1; |
| 52 | } |
| 53 | return 0; |
| 54 | } |
| 55 | EXPORT_SYMBOL_GPL(e820_any_mapped); |
| 56 | |
| 57 | /* |
| 58 | * This function checks if the entire range <start,end> is mapped with type. |
| 59 | * |
| 60 | * Note: this function only works correct if the e820 table is sorted and |
| 61 | * not-overlapping, which is the case |
| 62 | */ |
| 63 | int __init e820_all_mapped(u64 start, u64 end, unsigned type) |
| 64 | { |
| 65 | int i; |
| 66 | |
| 67 | for (i = 0; i < e820.nr_map; i++) { |
| 68 | struct e820entry *ei = &e820.map[i]; |
| 69 | |
| 70 | if (type && ei->type != type) |
| 71 | continue; |
| 72 | /* is the region (part) in overlap with the current region ?*/ |
| 73 | if (ei->addr >= end || ei->addr + ei->size <= start) |
| 74 | continue; |
| 75 | |
| 76 | /* if the region is at the beginning of <start,end> we move |
| 77 | * start to the end of the region since it's ok until there |
| 78 | */ |
| 79 | if (ei->addr <= start) |
| 80 | start = ei->addr + ei->size; |
| 81 | /* |
| 82 | * if start is now at or beyond end, we're done, full |
| 83 | * coverage |
| 84 | */ |
| 85 | if (start >= end) |
| 86 | return 1; |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Add a memory region to the kernel e820 map. |
| 93 | */ |
| 94 | void __init add_memory_region(u64 start, u64 size, int type) |
| 95 | { |
| 96 | int x = e820.nr_map; |
| 97 | |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame^] | 98 | if (x == ARRAY_SIZE(e820.map)) { |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 99 | printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | e820.map[x].addr = start; |
| 104 | e820.map[x].size = size; |
| 105 | e820.map[x].type = type; |
| 106 | e820.nr_map++; |
| 107 | } |
| 108 | |
| 109 | void __init e820_print_map(char *who) |
| 110 | { |
| 111 | int i; |
| 112 | |
| 113 | for (i = 0; i < e820.nr_map; i++) { |
| 114 | printk(KERN_INFO " %s: %016Lx - %016Lx ", who, |
| 115 | (unsigned long long) e820.map[i].addr, |
| 116 | (unsigned long long) |
| 117 | (e820.map[i].addr + e820.map[i].size)); |
| 118 | switch (e820.map[i].type) { |
| 119 | case E820_RAM: |
| 120 | printk(KERN_CONT "(usable)\n"); |
| 121 | break; |
| 122 | case E820_RESERVED: |
| 123 | printk(KERN_CONT "(reserved)\n"); |
| 124 | break; |
| 125 | case E820_ACPI: |
| 126 | printk(KERN_CONT "(ACPI data)\n"); |
| 127 | break; |
| 128 | case E820_NVS: |
| 129 | printk(KERN_CONT "(ACPI NVS)\n"); |
| 130 | break; |
| 131 | default: |
| 132 | printk(KERN_CONT "type %u\n", e820.map[i].type); |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * Sanitize the BIOS e820 map. |
| 140 | * |
| 141 | * Some e820 responses include overlapping entries. The following |
| 142 | * replaces the original e820 map with a new one, removing overlaps. |
| 143 | * |
| 144 | */ |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame^] | 145 | int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map, |
| 146 | char *pnr_map) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 147 | { |
| 148 | struct change_member { |
| 149 | struct e820entry *pbios; /* pointer to original bios entry */ |
| 150 | unsigned long long addr; /* address for this change point */ |
| 151 | }; |
| 152 | static struct change_member change_point_list[2*E820MAX] __initdata; |
| 153 | static struct change_member *change_point[2*E820MAX] __initdata; |
| 154 | static struct e820entry *overlap_list[E820MAX] __initdata; |
| 155 | static struct e820entry new_bios[E820MAX] __initdata; |
| 156 | struct change_member *change_tmp; |
| 157 | unsigned long current_type, last_type; |
| 158 | unsigned long long last_addr; |
| 159 | int chgidx, still_changing; |
| 160 | int overlap_entries; |
| 161 | int new_bios_entry; |
| 162 | int old_nr, new_nr, chg_nr; |
| 163 | int i; |
| 164 | |
| 165 | /* |
| 166 | Visually we're performing the following |
| 167 | (1,2,3,4 = memory types)... |
| 168 | |
| 169 | Sample memory map (w/overlaps): |
| 170 | ____22__________________ |
| 171 | ______________________4_ |
| 172 | ____1111________________ |
| 173 | _44_____________________ |
| 174 | 11111111________________ |
| 175 | ____________________33__ |
| 176 | ___________44___________ |
| 177 | __________33333_________ |
| 178 | ______________22________ |
| 179 | ___________________2222_ |
| 180 | _________111111111______ |
| 181 | _____________________11_ |
| 182 | _________________4______ |
| 183 | |
| 184 | Sanitized equivalent (no overlap): |
| 185 | 1_______________________ |
| 186 | _44_____________________ |
| 187 | ___1____________________ |
| 188 | ____22__________________ |
| 189 | ______11________________ |
| 190 | _________1______________ |
| 191 | __________3_____________ |
| 192 | ___________44___________ |
| 193 | _____________33_________ |
| 194 | _______________2________ |
| 195 | ________________1_______ |
| 196 | _________________4______ |
| 197 | ___________________2____ |
| 198 | ____________________33__ |
| 199 | ______________________4_ |
| 200 | */ |
| 201 | |
| 202 | /* if there's only one memory region, don't bother */ |
| 203 | if (*pnr_map < 2) |
| 204 | return -1; |
| 205 | |
| 206 | old_nr = *pnr_map; |
| 207 | |
| 208 | /* bail out if we find any unreasonable addresses in bios map */ |
| 209 | for (i = 0; i < old_nr; i++) |
| 210 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) |
| 211 | return -1; |
| 212 | |
| 213 | /* create pointers for initial change-point information (for sorting) */ |
| 214 | for (i = 0; i < 2 * old_nr; i++) |
| 215 | change_point[i] = &change_point_list[i]; |
| 216 | |
| 217 | /* record all known change-points (starting and ending addresses), |
| 218 | omitting those that are for empty memory regions */ |
| 219 | chgidx = 0; |
| 220 | for (i = 0; i < old_nr; i++) { |
| 221 | if (biosmap[i].size != 0) { |
| 222 | change_point[chgidx]->addr = biosmap[i].addr; |
| 223 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 224 | change_point[chgidx]->addr = biosmap[i].addr + |
| 225 | biosmap[i].size; |
| 226 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 227 | } |
| 228 | } |
| 229 | chg_nr = chgidx; |
| 230 | |
| 231 | /* sort change-point list by memory addresses (low -> high) */ |
| 232 | still_changing = 1; |
| 233 | while (still_changing) { |
| 234 | still_changing = 0; |
| 235 | for (i = 1; i < chg_nr; i++) { |
| 236 | unsigned long long curaddr, lastaddr; |
| 237 | unsigned long long curpbaddr, lastpbaddr; |
| 238 | |
| 239 | curaddr = change_point[i]->addr; |
| 240 | lastaddr = change_point[i - 1]->addr; |
| 241 | curpbaddr = change_point[i]->pbios->addr; |
| 242 | lastpbaddr = change_point[i - 1]->pbios->addr; |
| 243 | |
| 244 | /* |
| 245 | * swap entries, when: |
| 246 | * |
| 247 | * curaddr > lastaddr or |
| 248 | * curaddr == lastaddr and curaddr == curpbaddr and |
| 249 | * lastaddr != lastpbaddr |
| 250 | */ |
| 251 | if (curaddr < lastaddr || |
| 252 | (curaddr == lastaddr && curaddr == curpbaddr && |
| 253 | lastaddr != lastpbaddr)) { |
| 254 | change_tmp = change_point[i]; |
| 255 | change_point[i] = change_point[i-1]; |
| 256 | change_point[i-1] = change_tmp; |
| 257 | still_changing = 1; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* create a new bios memory map, removing overlaps */ |
| 263 | overlap_entries = 0; /* number of entries in the overlap table */ |
| 264 | new_bios_entry = 0; /* index for creating new bios map entries */ |
| 265 | last_type = 0; /* start with undefined memory type */ |
| 266 | last_addr = 0; /* start with 0 as last starting address */ |
| 267 | |
| 268 | /* loop through change-points, determining affect on the new bios map */ |
| 269 | for (chgidx = 0; chgidx < chg_nr; chgidx++) { |
| 270 | /* keep track of all overlapping bios entries */ |
| 271 | if (change_point[chgidx]->addr == |
| 272 | change_point[chgidx]->pbios->addr) { |
| 273 | /* |
| 274 | * add map entry to overlap list (> 1 entry |
| 275 | * implies an overlap) |
| 276 | */ |
| 277 | overlap_list[overlap_entries++] = |
| 278 | change_point[chgidx]->pbios; |
| 279 | } else { |
| 280 | /* |
| 281 | * remove entry from list (order independent, |
| 282 | * so swap with last) |
| 283 | */ |
| 284 | for (i = 0; i < overlap_entries; i++) { |
| 285 | if (overlap_list[i] == |
| 286 | change_point[chgidx]->pbios) |
| 287 | overlap_list[i] = |
| 288 | overlap_list[overlap_entries-1]; |
| 289 | } |
| 290 | overlap_entries--; |
| 291 | } |
| 292 | /* |
| 293 | * if there are overlapping entries, decide which |
| 294 | * "type" to use (larger value takes precedence -- |
| 295 | * 1=usable, 2,3,4,4+=unusable) |
| 296 | */ |
| 297 | current_type = 0; |
| 298 | for (i = 0; i < overlap_entries; i++) |
| 299 | if (overlap_list[i]->type > current_type) |
| 300 | current_type = overlap_list[i]->type; |
| 301 | /* |
| 302 | * continue building up new bios map based on this |
| 303 | * information |
| 304 | */ |
| 305 | if (current_type != last_type) { |
| 306 | if (last_type != 0) { |
| 307 | new_bios[new_bios_entry].size = |
| 308 | change_point[chgidx]->addr - last_addr; |
| 309 | /* |
| 310 | * move forward only if the new size |
| 311 | * was non-zero |
| 312 | */ |
| 313 | if (new_bios[new_bios_entry].size != 0) |
| 314 | /* |
| 315 | * no more space left for new |
| 316 | * bios entries ? |
| 317 | */ |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame^] | 318 | if (++new_bios_entry >= max_nr_map) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | if (current_type != 0) { |
| 322 | new_bios[new_bios_entry].addr = |
| 323 | change_point[chgidx]->addr; |
| 324 | new_bios[new_bios_entry].type = current_type; |
| 325 | last_addr = change_point[chgidx]->addr; |
| 326 | } |
| 327 | last_type = current_type; |
| 328 | } |
| 329 | } |
| 330 | /* retain count for new bios entries */ |
| 331 | new_nr = new_bios_entry; |
| 332 | |
| 333 | /* copy new bios mapping into original location */ |
| 334 | memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry)); |
| 335 | *pnr_map = new_nr; |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Copy the BIOS e820 map into a safe place. |
| 342 | * |
| 343 | * Sanity-check it while we're at it.. |
| 344 | * |
| 345 | * If we're lucky and live on a modern system, the setup code |
| 346 | * will have given us a memory map that we can use to properly |
| 347 | * set up memory. If we aren't, we'll fake a memory map. |
| 348 | */ |
| 349 | int __init copy_e820_map(struct e820entry *biosmap, int nr_map) |
| 350 | { |
| 351 | /* Only one memory region (or negative)? Ignore it */ |
| 352 | if (nr_map < 2) |
| 353 | return -1; |
| 354 | |
| 355 | do { |
| 356 | u64 start = biosmap->addr; |
| 357 | u64 size = biosmap->size; |
| 358 | u64 end = start + size; |
| 359 | u32 type = biosmap->type; |
| 360 | |
| 361 | /* Overflow in 64 bits? Ignore the memory map. */ |
| 362 | if (start > end) |
| 363 | return -1; |
| 364 | |
| 365 | add_memory_region(start, size, type); |
| 366 | } while (biosmap++, --nr_map); |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | u64 __init update_memory_range(u64 start, u64 size, unsigned old_type, |
| 371 | unsigned new_type) |
| 372 | { |
| 373 | int i; |
| 374 | u64 real_updated_size = 0; |
| 375 | |
| 376 | BUG_ON(old_type == new_type); |
| 377 | |
| 378 | for (i = 0; i < e820.nr_map; i++) { |
| 379 | struct e820entry *ei = &e820.map[i]; |
| 380 | u64 final_start, final_end; |
| 381 | if (ei->type != old_type) |
| 382 | continue; |
| 383 | /* totally covered? */ |
| 384 | if (ei->addr >= start && |
| 385 | (ei->addr + ei->size) <= (start + size)) { |
| 386 | ei->type = new_type; |
| 387 | real_updated_size += ei->size; |
| 388 | continue; |
| 389 | } |
| 390 | /* partially covered */ |
| 391 | final_start = max(start, ei->addr); |
| 392 | final_end = min(start + size, ei->addr + ei->size); |
| 393 | if (final_start >= final_end) |
| 394 | continue; |
| 395 | add_memory_region(final_start, final_end - final_start, |
| 396 | new_type); |
| 397 | real_updated_size += final_end - final_start; |
| 398 | } |
| 399 | return real_updated_size; |
| 400 | } |
| 401 | |
| 402 | void __init update_e820(void) |
| 403 | { |
| 404 | u8 nr_map; |
| 405 | |
| 406 | nr_map = e820.nr_map; |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame^] | 407 | if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map)) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 408 | return; |
| 409 | e820.nr_map = nr_map; |
| 410 | printk(KERN_INFO "modified physical RAM map:\n"); |
| 411 | e820_print_map("modified"); |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Search for the biggest gap in the low 32 bits of the e820 |
| 416 | * memory space. We pass this space to PCI to assign MMIO resources |
| 417 | * for hotplug or unconfigured devices in. |
| 418 | * Hopefully the BIOS let enough space left. |
| 419 | */ |
| 420 | __init void e820_setup_gap(void) |
| 421 | { |
| 422 | unsigned long gapstart, gapsize, round; |
| 423 | unsigned long long last; |
| 424 | int i; |
| 425 | int found = 0; |
| 426 | |
| 427 | last = 0x100000000ull; |
| 428 | gapstart = 0x10000000; |
| 429 | gapsize = 0x400000; |
| 430 | i = e820.nr_map; |
| 431 | while (--i >= 0) { |
| 432 | unsigned long long start = e820.map[i].addr; |
| 433 | unsigned long long end = start + e820.map[i].size; |
| 434 | |
| 435 | /* |
| 436 | * Since "last" is at most 4GB, we know we'll |
| 437 | * fit in 32 bits if this condition is true |
| 438 | */ |
| 439 | if (last > end) { |
| 440 | unsigned long gap = last - end; |
| 441 | |
| 442 | if (gap > gapsize) { |
| 443 | gapsize = gap; |
| 444 | gapstart = end; |
| 445 | found = 1; |
| 446 | } |
| 447 | } |
| 448 | if (start < last) |
| 449 | last = start; |
| 450 | } |
| 451 | |
| 452 | #ifdef CONFIG_X86_64 |
| 453 | if (!found) { |
| 454 | gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024; |
| 455 | printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit " |
| 456 | "address range\n" |
| 457 | KERN_ERR "PCI: Unassigned devices with 32bit resource " |
| 458 | "registers may break!\n"); |
| 459 | } |
| 460 | #endif |
| 461 | |
| 462 | /* |
| 463 | * See how much we want to round up: start off with |
| 464 | * rounding to the next 1MB area. |
| 465 | */ |
| 466 | round = 0x100000; |
| 467 | while ((gapsize >> 4) > round) |
| 468 | round += round; |
| 469 | /* Fun with two's complement */ |
| 470 | pci_mem_start = (gapstart + round) & -round; |
| 471 | |
| 472 | printk(KERN_INFO |
| 473 | "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n", |
| 474 | pci_mem_start, gapstart, gapsize); |
| 475 | } |
| 476 | |