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 |
Paul Jackson | 5b7eb2e | 2008-05-14 08:15:52 -0700 | [diff] [blame^] | 142 | * replaces the original e820 map with a new one, removing overlaps, |
| 143 | * and resolving conflicting memory types in favor of highest |
| 144 | * numbered type. |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 145 | * |
Paul Jackson | 5b7eb2e | 2008-05-14 08:15:52 -0700 | [diff] [blame^] | 146 | * The input parameter biosmap points to an array of 'struct |
| 147 | * e820entry' which on entry has elements in the range [0, *pnr_map) |
| 148 | * valid, and which has space for up to max_nr_map entries. |
| 149 | * On return, the resulting sanitized e820 map entries will be in |
| 150 | * overwritten in the same location, starting at biosmap. |
| 151 | * |
| 152 | * The integer pointed to by pnr_map must be valid on entry (the |
| 153 | * current number of valid entries located at biosmap) and will |
| 154 | * be updated on return, with the new number of valid entries |
| 155 | * (something no more than max_nr_map.) |
| 156 | * |
| 157 | * The return value from sanitize_e820_map() is zero if it |
| 158 | * successfully 'sanitized' the map entries passed in, and is -1 |
| 159 | * if it did nothing, which can happen if either of (1) it was |
| 160 | * only passed one map entry, or (2) any of the input map entries |
| 161 | * were invalid (start + size < start, meaning that the size was |
| 162 | * so big the described memory range wrapped around through zero.) |
| 163 | * |
| 164 | * Visually we're performing the following |
| 165 | * (1,2,3,4 = memory types)... |
| 166 | * |
| 167 | * Sample memory map (w/overlaps): |
| 168 | * ____22__________________ |
| 169 | * ______________________4_ |
| 170 | * ____1111________________ |
| 171 | * _44_____________________ |
| 172 | * 11111111________________ |
| 173 | * ____________________33__ |
| 174 | * ___________44___________ |
| 175 | * __________33333_________ |
| 176 | * ______________22________ |
| 177 | * ___________________2222_ |
| 178 | * _________111111111______ |
| 179 | * _____________________11_ |
| 180 | * _________________4______ |
| 181 | * |
| 182 | * Sanitized equivalent (no overlap): |
| 183 | * 1_______________________ |
| 184 | * _44_____________________ |
| 185 | * ___1____________________ |
| 186 | * ____22__________________ |
| 187 | * ______11________________ |
| 188 | * _________1______________ |
| 189 | * __________3_____________ |
| 190 | * ___________44___________ |
| 191 | * _____________33_________ |
| 192 | * _______________2________ |
| 193 | * ________________1_______ |
| 194 | * _________________4______ |
| 195 | * ___________________2____ |
| 196 | * ____________________33__ |
| 197 | * ______________________4_ |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 198 | */ |
Paul Jackson | 5b7eb2e | 2008-05-14 08:15:52 -0700 | [diff] [blame^] | 199 | |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame] | 200 | int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map, |
Paul Jackson | 6e9bcc7 | 2008-05-14 08:15:46 -0700 | [diff] [blame] | 201 | int *pnr_map) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 202 | { |
| 203 | struct change_member { |
| 204 | struct e820entry *pbios; /* pointer to original bios entry */ |
| 205 | unsigned long long addr; /* address for this change point */ |
| 206 | }; |
Paul Jackson | 028b785 | 2008-05-14 08:15:40 -0700 | [diff] [blame] | 207 | static struct change_member change_point_list[2*E820_X_MAX] __initdata; |
| 208 | static struct change_member *change_point[2*E820_X_MAX] __initdata; |
| 209 | static struct e820entry *overlap_list[E820_X_MAX] __initdata; |
| 210 | static struct e820entry new_bios[E820_X_MAX] __initdata; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 211 | struct change_member *change_tmp; |
| 212 | unsigned long current_type, last_type; |
| 213 | unsigned long long last_addr; |
| 214 | int chgidx, still_changing; |
| 215 | int overlap_entries; |
| 216 | int new_bios_entry; |
| 217 | int old_nr, new_nr, chg_nr; |
| 218 | int i; |
| 219 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 220 | /* if there's only one memory region, don't bother */ |
| 221 | if (*pnr_map < 2) |
| 222 | return -1; |
| 223 | |
| 224 | old_nr = *pnr_map; |
Paul Jackson | 6e9bcc7 | 2008-05-14 08:15:46 -0700 | [diff] [blame] | 225 | BUG_ON(old_nr > max_nr_map); |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 226 | |
| 227 | /* bail out if we find any unreasonable addresses in bios map */ |
| 228 | for (i = 0; i < old_nr; i++) |
| 229 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) |
| 230 | return -1; |
| 231 | |
| 232 | /* create pointers for initial change-point information (for sorting) */ |
| 233 | for (i = 0; i < 2 * old_nr; i++) |
| 234 | change_point[i] = &change_point_list[i]; |
| 235 | |
| 236 | /* record all known change-points (starting and ending addresses), |
| 237 | omitting those that are for empty memory regions */ |
| 238 | chgidx = 0; |
| 239 | for (i = 0; i < old_nr; i++) { |
| 240 | if (biosmap[i].size != 0) { |
| 241 | change_point[chgidx]->addr = biosmap[i].addr; |
| 242 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 243 | change_point[chgidx]->addr = biosmap[i].addr + |
| 244 | biosmap[i].size; |
| 245 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 246 | } |
| 247 | } |
| 248 | chg_nr = chgidx; |
| 249 | |
| 250 | /* sort change-point list by memory addresses (low -> high) */ |
| 251 | still_changing = 1; |
| 252 | while (still_changing) { |
| 253 | still_changing = 0; |
| 254 | for (i = 1; i < chg_nr; i++) { |
| 255 | unsigned long long curaddr, lastaddr; |
| 256 | unsigned long long curpbaddr, lastpbaddr; |
| 257 | |
| 258 | curaddr = change_point[i]->addr; |
| 259 | lastaddr = change_point[i - 1]->addr; |
| 260 | curpbaddr = change_point[i]->pbios->addr; |
| 261 | lastpbaddr = change_point[i - 1]->pbios->addr; |
| 262 | |
| 263 | /* |
| 264 | * swap entries, when: |
| 265 | * |
| 266 | * curaddr > lastaddr or |
| 267 | * curaddr == lastaddr and curaddr == curpbaddr and |
| 268 | * lastaddr != lastpbaddr |
| 269 | */ |
| 270 | if (curaddr < lastaddr || |
| 271 | (curaddr == lastaddr && curaddr == curpbaddr && |
| 272 | lastaddr != lastpbaddr)) { |
| 273 | change_tmp = change_point[i]; |
| 274 | change_point[i] = change_point[i-1]; |
| 275 | change_point[i-1] = change_tmp; |
| 276 | still_changing = 1; |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /* create a new bios memory map, removing overlaps */ |
| 282 | overlap_entries = 0; /* number of entries in the overlap table */ |
| 283 | new_bios_entry = 0; /* index for creating new bios map entries */ |
| 284 | last_type = 0; /* start with undefined memory type */ |
| 285 | last_addr = 0; /* start with 0 as last starting address */ |
| 286 | |
| 287 | /* loop through change-points, determining affect on the new bios map */ |
| 288 | for (chgidx = 0; chgidx < chg_nr; chgidx++) { |
| 289 | /* keep track of all overlapping bios entries */ |
| 290 | if (change_point[chgidx]->addr == |
| 291 | change_point[chgidx]->pbios->addr) { |
| 292 | /* |
| 293 | * add map entry to overlap list (> 1 entry |
| 294 | * implies an overlap) |
| 295 | */ |
| 296 | overlap_list[overlap_entries++] = |
| 297 | change_point[chgidx]->pbios; |
| 298 | } else { |
| 299 | /* |
| 300 | * remove entry from list (order independent, |
| 301 | * so swap with last) |
| 302 | */ |
| 303 | for (i = 0; i < overlap_entries; i++) { |
| 304 | if (overlap_list[i] == |
| 305 | change_point[chgidx]->pbios) |
| 306 | overlap_list[i] = |
| 307 | overlap_list[overlap_entries-1]; |
| 308 | } |
| 309 | overlap_entries--; |
| 310 | } |
| 311 | /* |
| 312 | * if there are overlapping entries, decide which |
| 313 | * "type" to use (larger value takes precedence -- |
| 314 | * 1=usable, 2,3,4,4+=unusable) |
| 315 | */ |
| 316 | current_type = 0; |
| 317 | for (i = 0; i < overlap_entries; i++) |
| 318 | if (overlap_list[i]->type > current_type) |
| 319 | current_type = overlap_list[i]->type; |
| 320 | /* |
| 321 | * continue building up new bios map based on this |
| 322 | * information |
| 323 | */ |
| 324 | if (current_type != last_type) { |
| 325 | if (last_type != 0) { |
| 326 | new_bios[new_bios_entry].size = |
| 327 | change_point[chgidx]->addr - last_addr; |
| 328 | /* |
| 329 | * move forward only if the new size |
| 330 | * was non-zero |
| 331 | */ |
| 332 | if (new_bios[new_bios_entry].size != 0) |
| 333 | /* |
| 334 | * no more space left for new |
| 335 | * bios entries ? |
| 336 | */ |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame] | 337 | if (++new_bios_entry >= max_nr_map) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | if (current_type != 0) { |
| 341 | new_bios[new_bios_entry].addr = |
| 342 | change_point[chgidx]->addr; |
| 343 | new_bios[new_bios_entry].type = current_type; |
| 344 | last_addr = change_point[chgidx]->addr; |
| 345 | } |
| 346 | last_type = current_type; |
| 347 | } |
| 348 | } |
| 349 | /* retain count for new bios entries */ |
| 350 | new_nr = new_bios_entry; |
| 351 | |
| 352 | /* copy new bios mapping into original location */ |
| 353 | memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry)); |
| 354 | *pnr_map = new_nr; |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Copy the BIOS e820 map into a safe place. |
| 361 | * |
| 362 | * Sanity-check it while we're at it.. |
| 363 | * |
| 364 | * If we're lucky and live on a modern system, the setup code |
| 365 | * will have given us a memory map that we can use to properly |
| 366 | * set up memory. If we aren't, we'll fake a memory map. |
| 367 | */ |
| 368 | int __init copy_e820_map(struct e820entry *biosmap, int nr_map) |
| 369 | { |
| 370 | /* Only one memory region (or negative)? Ignore it */ |
| 371 | if (nr_map < 2) |
| 372 | return -1; |
| 373 | |
| 374 | do { |
| 375 | u64 start = biosmap->addr; |
| 376 | u64 size = biosmap->size; |
| 377 | u64 end = start + size; |
| 378 | u32 type = biosmap->type; |
| 379 | |
| 380 | /* Overflow in 64 bits? Ignore the memory map. */ |
| 381 | if (start > end) |
| 382 | return -1; |
| 383 | |
| 384 | add_memory_region(start, size, type); |
| 385 | } while (biosmap++, --nr_map); |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | u64 __init update_memory_range(u64 start, u64 size, unsigned old_type, |
| 390 | unsigned new_type) |
| 391 | { |
| 392 | int i; |
| 393 | u64 real_updated_size = 0; |
| 394 | |
| 395 | BUG_ON(old_type == new_type); |
| 396 | |
| 397 | for (i = 0; i < e820.nr_map; i++) { |
| 398 | struct e820entry *ei = &e820.map[i]; |
| 399 | u64 final_start, final_end; |
| 400 | if (ei->type != old_type) |
| 401 | continue; |
| 402 | /* totally covered? */ |
| 403 | if (ei->addr >= start && |
| 404 | (ei->addr + ei->size) <= (start + size)) { |
| 405 | ei->type = new_type; |
| 406 | real_updated_size += ei->size; |
| 407 | continue; |
| 408 | } |
| 409 | /* partially covered */ |
| 410 | final_start = max(start, ei->addr); |
| 411 | final_end = min(start + size, ei->addr + ei->size); |
| 412 | if (final_start >= final_end) |
| 413 | continue; |
| 414 | add_memory_region(final_start, final_end - final_start, |
| 415 | new_type); |
| 416 | real_updated_size += final_end - final_start; |
| 417 | } |
| 418 | return real_updated_size; |
| 419 | } |
| 420 | |
| 421 | void __init update_e820(void) |
| 422 | { |
Paul Jackson | 6e9bcc7 | 2008-05-14 08:15:46 -0700 | [diff] [blame] | 423 | int nr_map; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 424 | |
| 425 | nr_map = e820.nr_map; |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame] | 426 | if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map)) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 427 | return; |
| 428 | e820.nr_map = nr_map; |
| 429 | printk(KERN_INFO "modified physical RAM map:\n"); |
| 430 | e820_print_map("modified"); |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Search for the biggest gap in the low 32 bits of the e820 |
| 435 | * memory space. We pass this space to PCI to assign MMIO resources |
| 436 | * for hotplug or unconfigured devices in. |
| 437 | * Hopefully the BIOS let enough space left. |
| 438 | */ |
| 439 | __init void e820_setup_gap(void) |
| 440 | { |
| 441 | unsigned long gapstart, gapsize, round; |
| 442 | unsigned long long last; |
| 443 | int i; |
| 444 | int found = 0; |
| 445 | |
| 446 | last = 0x100000000ull; |
| 447 | gapstart = 0x10000000; |
| 448 | gapsize = 0x400000; |
| 449 | i = e820.nr_map; |
| 450 | while (--i >= 0) { |
| 451 | unsigned long long start = e820.map[i].addr; |
| 452 | unsigned long long end = start + e820.map[i].size; |
| 453 | |
| 454 | /* |
| 455 | * Since "last" is at most 4GB, we know we'll |
| 456 | * fit in 32 bits if this condition is true |
| 457 | */ |
| 458 | if (last > end) { |
| 459 | unsigned long gap = last - end; |
| 460 | |
| 461 | if (gap > gapsize) { |
| 462 | gapsize = gap; |
| 463 | gapstart = end; |
| 464 | found = 1; |
| 465 | } |
| 466 | } |
| 467 | if (start < last) |
| 468 | last = start; |
| 469 | } |
| 470 | |
| 471 | #ifdef CONFIG_X86_64 |
| 472 | if (!found) { |
| 473 | gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024; |
| 474 | printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit " |
| 475 | "address range\n" |
| 476 | KERN_ERR "PCI: Unassigned devices with 32bit resource " |
| 477 | "registers may break!\n"); |
| 478 | } |
| 479 | #endif |
| 480 | |
| 481 | /* |
| 482 | * See how much we want to round up: start off with |
| 483 | * rounding to the next 1MB area. |
| 484 | */ |
| 485 | round = 0x100000; |
| 486 | while ((gapsize >> 4) > round) |
| 487 | round += round; |
| 488 | /* Fun with two's complement */ |
| 489 | pci_mem_start = (gapstart + round) & -round; |
| 490 | |
| 491 | printk(KERN_INFO |
| 492 | "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n", |
| 493 | pci_mem_start, gapstart, gapsize); |
| 494 | } |
| 495 | |