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> |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 15 | #include <linux/pfn.h> |
Yinghai Lu | bf62f39 | 2008-05-20 20:10:58 -0700 | [diff] [blame] | 16 | #include <linux/suspend.h> |
Bernhard Walle | 5dfcf14 | 2008-06-27 13:12:55 +0200 | [diff] [blame] | 17 | #include <linux/firmware-map.h> |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 18 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 19 | #include <asm/e820.h> |
Yinghai Lu | a4c81cf | 2008-05-18 01:18:57 -0700 | [diff] [blame] | 20 | #include <asm/proto.h> |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 21 | #include <asm/setup.h> |
| 22 | |
Bernhard Walle | 5dfcf14 | 2008-06-27 13:12:55 +0200 | [diff] [blame] | 23 | /* |
| 24 | * The e820 map is the map that gets modified e.g. with command line parameters |
| 25 | * and that is also registered with modifications in the kernel resource tree |
| 26 | * with the iomem_resource as parent. |
| 27 | * |
| 28 | * The e820_saved is directly saved after the BIOS-provided memory map is |
| 29 | * copied. It doesn't get modified afterwards. It's registered for the |
| 30 | * /sys/firmware/memmap interface. |
| 31 | * |
| 32 | * That memory map is not modified and is used as base for kexec. The kexec'd |
| 33 | * kernel should get the same memory map as the firmware provides. Then the |
| 34 | * user can e.g. boot the original kernel with mem=1G while still booting the |
| 35 | * next kernel with full memory. |
| 36 | */ |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 37 | struct e820map e820; |
Bernhard Walle | 5dfcf14 | 2008-06-27 13:12:55 +0200 | [diff] [blame] | 38 | struct e820map e820_saved; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 39 | |
| 40 | /* For PCI or other memory-mapped resources */ |
| 41 | unsigned long pci_mem_start = 0xaeedbabe; |
| 42 | #ifdef CONFIG_PCI |
| 43 | EXPORT_SYMBOL(pci_mem_start); |
| 44 | #endif |
| 45 | |
| 46 | /* |
| 47 | * This function checks if any part of the range <start,end> is mapped |
| 48 | * with type. |
| 49 | */ |
| 50 | int |
| 51 | e820_any_mapped(u64 start, u64 end, unsigned type) |
| 52 | { |
| 53 | int i; |
| 54 | |
| 55 | for (i = 0; i < e820.nr_map; i++) { |
| 56 | struct e820entry *ei = &e820.map[i]; |
| 57 | |
| 58 | if (type && ei->type != type) |
| 59 | continue; |
| 60 | if (ei->addr >= end || ei->addr + ei->size <= start) |
| 61 | continue; |
| 62 | return 1; |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(e820_any_mapped); |
| 67 | |
| 68 | /* |
| 69 | * This function checks if the entire range <start,end> is mapped with type. |
| 70 | * |
| 71 | * Note: this function only works correct if the e820 table is sorted and |
| 72 | * not-overlapping, which is the case |
| 73 | */ |
| 74 | int __init e820_all_mapped(u64 start, u64 end, unsigned type) |
| 75 | { |
| 76 | int i; |
| 77 | |
| 78 | for (i = 0; i < e820.nr_map; i++) { |
| 79 | struct e820entry *ei = &e820.map[i]; |
| 80 | |
| 81 | if (type && ei->type != type) |
| 82 | continue; |
| 83 | /* is the region (part) in overlap with the current region ?*/ |
| 84 | if (ei->addr >= end || ei->addr + ei->size <= start) |
| 85 | continue; |
| 86 | |
| 87 | /* if the region is at the beginning of <start,end> we move |
| 88 | * start to the end of the region since it's ok until there |
| 89 | */ |
| 90 | if (ei->addr <= start) |
| 91 | start = ei->addr + ei->size; |
| 92 | /* |
| 93 | * if start is now at or beyond end, we're done, full |
| 94 | * coverage |
| 95 | */ |
| 96 | if (start >= end) |
| 97 | return 1; |
| 98 | } |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | /* |
| 103 | * Add a memory region to the kernel e820 map. |
| 104 | */ |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 105 | static void __init __e820_add_region(struct e820map *e820x, u64 start, u64 size, |
| 106 | int type) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 107 | { |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 108 | int x = e820x->nr_map; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 109 | |
Cyrill Gorcunov | 5051fd6 | 2009-08-24 21:53:37 +0400 | [diff] [blame] | 110 | if (x >= ARRAY_SIZE(e820x->map)) { |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 111 | printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); |
| 112 | return; |
| 113 | } |
| 114 | |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 115 | e820x->map[x].addr = start; |
| 116 | e820x->map[x].size = size; |
| 117 | e820x->map[x].type = type; |
| 118 | e820x->nr_map++; |
| 119 | } |
| 120 | |
| 121 | void __init e820_add_region(u64 start, u64 size, int type) |
| 122 | { |
| 123 | __e820_add_region(&e820, start, size, type); |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Yinghai Lu | c61cf4c | 2009-03-15 00:59:19 -0700 | [diff] [blame] | 126 | static void __init e820_print_type(u32 type) |
| 127 | { |
| 128 | switch (type) { |
| 129 | case E820_RAM: |
| 130 | case E820_RESERVED_KERN: |
| 131 | printk(KERN_CONT "(usable)"); |
| 132 | break; |
| 133 | case E820_RESERVED: |
| 134 | printk(KERN_CONT "(reserved)"); |
| 135 | break; |
| 136 | case E820_ACPI: |
| 137 | printk(KERN_CONT "(ACPI data)"); |
| 138 | break; |
| 139 | case E820_NVS: |
| 140 | printk(KERN_CONT "(ACPI NVS)"); |
| 141 | break; |
| 142 | case E820_UNUSABLE: |
| 143 | printk(KERN_CONT "(unusable)"); |
| 144 | break; |
| 145 | default: |
| 146 | printk(KERN_CONT "type %u", type); |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 151 | void __init e820_print_map(char *who) |
| 152 | { |
| 153 | int i; |
| 154 | |
| 155 | for (i = 0; i < e820.nr_map; i++) { |
| 156 | printk(KERN_INFO " %s: %016Lx - %016Lx ", who, |
| 157 | (unsigned long long) e820.map[i].addr, |
| 158 | (unsigned long long) |
| 159 | (e820.map[i].addr + e820.map[i].size)); |
Yinghai Lu | c61cf4c | 2009-03-15 00:59:19 -0700 | [diff] [blame] | 160 | e820_print_type(e820.map[i].type); |
| 161 | printk(KERN_CONT "\n"); |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Sanitize the BIOS e820 map. |
| 167 | * |
| 168 | * Some e820 responses include overlapping entries. The following |
Paul Jackson | 5b7eb2e | 2008-05-14 08:15:52 -0700 | [diff] [blame] | 169 | * replaces the original e820 map with a new one, removing overlaps, |
| 170 | * and resolving conflicting memory types in favor of highest |
| 171 | * numbered type. |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 172 | * |
Paul Jackson | 5b7eb2e | 2008-05-14 08:15:52 -0700 | [diff] [blame] | 173 | * The input parameter biosmap points to an array of 'struct |
| 174 | * e820entry' which on entry has elements in the range [0, *pnr_map) |
| 175 | * valid, and which has space for up to max_nr_map entries. |
| 176 | * On return, the resulting sanitized e820 map entries will be in |
| 177 | * overwritten in the same location, starting at biosmap. |
| 178 | * |
| 179 | * The integer pointed to by pnr_map must be valid on entry (the |
| 180 | * current number of valid entries located at biosmap) and will |
| 181 | * be updated on return, with the new number of valid entries |
| 182 | * (something no more than max_nr_map.) |
| 183 | * |
| 184 | * The return value from sanitize_e820_map() is zero if it |
| 185 | * successfully 'sanitized' the map entries passed in, and is -1 |
| 186 | * if it did nothing, which can happen if either of (1) it was |
| 187 | * only passed one map entry, or (2) any of the input map entries |
| 188 | * were invalid (start + size < start, meaning that the size was |
| 189 | * so big the described memory range wrapped around through zero.) |
| 190 | * |
| 191 | * Visually we're performing the following |
| 192 | * (1,2,3,4 = memory types)... |
| 193 | * |
| 194 | * Sample memory map (w/overlaps): |
| 195 | * ____22__________________ |
| 196 | * ______________________4_ |
| 197 | * ____1111________________ |
| 198 | * _44_____________________ |
| 199 | * 11111111________________ |
| 200 | * ____________________33__ |
| 201 | * ___________44___________ |
| 202 | * __________33333_________ |
| 203 | * ______________22________ |
| 204 | * ___________________2222_ |
| 205 | * _________111111111______ |
| 206 | * _____________________11_ |
| 207 | * _________________4______ |
| 208 | * |
| 209 | * Sanitized equivalent (no overlap): |
| 210 | * 1_______________________ |
| 211 | * _44_____________________ |
| 212 | * ___1____________________ |
| 213 | * ____22__________________ |
| 214 | * ______11________________ |
| 215 | * _________1______________ |
| 216 | * __________3_____________ |
| 217 | * ___________44___________ |
| 218 | * _____________33_________ |
| 219 | * _______________2________ |
| 220 | * ________________1_______ |
| 221 | * _________________4______ |
| 222 | * ___________________2____ |
| 223 | * ____________________33__ |
| 224 | * ______________________4_ |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 225 | */ |
Paul Jackson | 5b7eb2e | 2008-05-14 08:15:52 -0700 | [diff] [blame] | 226 | |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame] | 227 | int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map, |
Jaswinder Singh Rajput | ba63903 | 2009-03-23 02:13:01 +0530 | [diff] [blame] | 228 | u32 *pnr_map) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 229 | { |
| 230 | struct change_member { |
| 231 | struct e820entry *pbios; /* pointer to original bios entry */ |
| 232 | unsigned long long addr; /* address for this change point */ |
| 233 | }; |
Paul Jackson | 157fabf | 2008-06-22 07:21:57 -0700 | [diff] [blame] | 234 | static struct change_member change_point_list[2*E820_X_MAX] __initdata; |
| 235 | static struct change_member *change_point[2*E820_X_MAX] __initdata; |
| 236 | static struct e820entry *overlap_list[E820_X_MAX] __initdata; |
| 237 | static struct e820entry new_bios[E820_X_MAX] __initdata; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 238 | struct change_member *change_tmp; |
| 239 | unsigned long current_type, last_type; |
| 240 | unsigned long long last_addr; |
| 241 | int chgidx, still_changing; |
| 242 | int overlap_entries; |
| 243 | int new_bios_entry; |
| 244 | int old_nr, new_nr, chg_nr; |
| 245 | int i; |
| 246 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 247 | /* if there's only one memory region, don't bother */ |
| 248 | if (*pnr_map < 2) |
| 249 | return -1; |
| 250 | |
| 251 | old_nr = *pnr_map; |
Paul Jackson | 6e9bcc7 | 2008-05-14 08:15:46 -0700 | [diff] [blame] | 252 | BUG_ON(old_nr > max_nr_map); |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 253 | |
| 254 | /* bail out if we find any unreasonable addresses in bios map */ |
| 255 | for (i = 0; i < old_nr; i++) |
| 256 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) |
| 257 | return -1; |
| 258 | |
| 259 | /* create pointers for initial change-point information (for sorting) */ |
| 260 | for (i = 0; i < 2 * old_nr; i++) |
| 261 | change_point[i] = &change_point_list[i]; |
| 262 | |
| 263 | /* record all known change-points (starting and ending addresses), |
| 264 | omitting those that are for empty memory regions */ |
| 265 | chgidx = 0; |
| 266 | for (i = 0; i < old_nr; i++) { |
| 267 | if (biosmap[i].size != 0) { |
| 268 | change_point[chgidx]->addr = biosmap[i].addr; |
| 269 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 270 | change_point[chgidx]->addr = biosmap[i].addr + |
| 271 | biosmap[i].size; |
| 272 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 273 | } |
| 274 | } |
| 275 | chg_nr = chgidx; |
| 276 | |
| 277 | /* sort change-point list by memory addresses (low -> high) */ |
| 278 | still_changing = 1; |
| 279 | while (still_changing) { |
| 280 | still_changing = 0; |
| 281 | for (i = 1; i < chg_nr; i++) { |
| 282 | unsigned long long curaddr, lastaddr; |
| 283 | unsigned long long curpbaddr, lastpbaddr; |
| 284 | |
| 285 | curaddr = change_point[i]->addr; |
| 286 | lastaddr = change_point[i - 1]->addr; |
| 287 | curpbaddr = change_point[i]->pbios->addr; |
| 288 | lastpbaddr = change_point[i - 1]->pbios->addr; |
| 289 | |
| 290 | /* |
| 291 | * swap entries, when: |
| 292 | * |
| 293 | * curaddr > lastaddr or |
| 294 | * curaddr == lastaddr and curaddr == curpbaddr and |
| 295 | * lastaddr != lastpbaddr |
| 296 | */ |
| 297 | if (curaddr < lastaddr || |
| 298 | (curaddr == lastaddr && curaddr == curpbaddr && |
| 299 | lastaddr != lastpbaddr)) { |
| 300 | change_tmp = change_point[i]; |
| 301 | change_point[i] = change_point[i-1]; |
| 302 | change_point[i-1] = change_tmp; |
| 303 | still_changing = 1; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /* create a new bios memory map, removing overlaps */ |
| 309 | overlap_entries = 0; /* number of entries in the overlap table */ |
| 310 | new_bios_entry = 0; /* index for creating new bios map entries */ |
| 311 | last_type = 0; /* start with undefined memory type */ |
| 312 | last_addr = 0; /* start with 0 as last starting address */ |
| 313 | |
| 314 | /* loop through change-points, determining affect on the new bios map */ |
| 315 | for (chgidx = 0; chgidx < chg_nr; chgidx++) { |
| 316 | /* keep track of all overlapping bios entries */ |
| 317 | if (change_point[chgidx]->addr == |
| 318 | change_point[chgidx]->pbios->addr) { |
| 319 | /* |
| 320 | * add map entry to overlap list (> 1 entry |
| 321 | * implies an overlap) |
| 322 | */ |
| 323 | overlap_list[overlap_entries++] = |
| 324 | change_point[chgidx]->pbios; |
| 325 | } else { |
| 326 | /* |
| 327 | * remove entry from list (order independent, |
| 328 | * so swap with last) |
| 329 | */ |
| 330 | for (i = 0; i < overlap_entries; i++) { |
| 331 | if (overlap_list[i] == |
| 332 | change_point[chgidx]->pbios) |
| 333 | overlap_list[i] = |
| 334 | overlap_list[overlap_entries-1]; |
| 335 | } |
| 336 | overlap_entries--; |
| 337 | } |
| 338 | /* |
| 339 | * if there are overlapping entries, decide which |
| 340 | * "type" to use (larger value takes precedence -- |
| 341 | * 1=usable, 2,3,4,4+=unusable) |
| 342 | */ |
| 343 | current_type = 0; |
| 344 | for (i = 0; i < overlap_entries; i++) |
| 345 | if (overlap_list[i]->type > current_type) |
| 346 | current_type = overlap_list[i]->type; |
| 347 | /* |
| 348 | * continue building up new bios map based on this |
| 349 | * information |
| 350 | */ |
| 351 | if (current_type != last_type) { |
| 352 | if (last_type != 0) { |
| 353 | new_bios[new_bios_entry].size = |
| 354 | change_point[chgidx]->addr - last_addr; |
| 355 | /* |
| 356 | * move forward only if the new size |
| 357 | * was non-zero |
| 358 | */ |
| 359 | if (new_bios[new_bios_entry].size != 0) |
| 360 | /* |
| 361 | * no more space left for new |
| 362 | * bios entries ? |
| 363 | */ |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame] | 364 | if (++new_bios_entry >= max_nr_map) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 365 | break; |
| 366 | } |
| 367 | if (current_type != 0) { |
| 368 | new_bios[new_bios_entry].addr = |
| 369 | change_point[chgidx]->addr; |
| 370 | new_bios[new_bios_entry].type = current_type; |
| 371 | last_addr = change_point[chgidx]->addr; |
| 372 | } |
| 373 | last_type = current_type; |
| 374 | } |
| 375 | } |
| 376 | /* retain count for new bios entries */ |
| 377 | new_nr = new_bios_entry; |
| 378 | |
| 379 | /* copy new bios mapping into original location */ |
| 380 | memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry)); |
| 381 | *pnr_map = new_nr; |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
Yinghai Lu | dc8e812 | 2008-07-01 20:02:16 -0700 | [diff] [blame] | 386 | static int __init __append_e820_map(struct e820entry *biosmap, int nr_map) |
Huang, Ying | 8c5beb5 | 2008-06-11 11:33:39 +0800 | [diff] [blame] | 387 | { |
| 388 | while (nr_map) { |
| 389 | u64 start = biosmap->addr; |
| 390 | u64 size = biosmap->size; |
| 391 | u64 end = start + size; |
| 392 | u32 type = biosmap->type; |
| 393 | |
| 394 | /* Overflow in 64 bits? Ignore the memory map. */ |
| 395 | if (start > end) |
| 396 | return -1; |
| 397 | |
| 398 | e820_add_region(start, size, type); |
| 399 | |
| 400 | biosmap++; |
| 401 | nr_map--; |
| 402 | } |
| 403 | return 0; |
| 404 | } |
| 405 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 406 | /* |
| 407 | * Copy the BIOS e820 map into a safe place. |
| 408 | * |
| 409 | * Sanity-check it while we're at it.. |
| 410 | * |
| 411 | * If we're lucky and live on a modern system, the setup code |
| 412 | * will have given us a memory map that we can use to properly |
| 413 | * set up memory. If we aren't, we'll fake a memory map. |
| 414 | */ |
Yinghai Lu | dc8e812 | 2008-07-01 20:02:16 -0700 | [diff] [blame] | 415 | static int __init append_e820_map(struct e820entry *biosmap, int nr_map) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 416 | { |
| 417 | /* Only one memory region (or negative)? Ignore it */ |
| 418 | if (nr_map < 2) |
| 419 | return -1; |
| 420 | |
Yinghai Lu | dc8e812 | 2008-07-01 20:02:16 -0700 | [diff] [blame] | 421 | return __append_e820_map(biosmap, nr_map); |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 422 | } |
| 423 | |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 424 | static u64 __init __e820_update_range(struct e820map *e820x, u64 start, |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 425 | u64 size, unsigned old_type, |
| 426 | unsigned new_type) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 427 | { |
Yinghai Lu | 78a8b35b | 2009-03-12 22:36:01 -0700 | [diff] [blame] | 428 | u64 end; |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 429 | unsigned int i; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 430 | u64 real_updated_size = 0; |
| 431 | |
| 432 | BUG_ON(old_type == new_type); |
| 433 | |
Yinghai Lu | 232b957 | 2008-06-24 14:58:38 -0700 | [diff] [blame] | 434 | if (size > (ULLONG_MAX - start)) |
| 435 | size = ULLONG_MAX - start; |
| 436 | |
Yinghai Lu | 78a8b35b | 2009-03-12 22:36:01 -0700 | [diff] [blame] | 437 | end = start + size; |
Yinghai Lu | c61cf4c | 2009-03-15 00:59:19 -0700 | [diff] [blame] | 438 | printk(KERN_DEBUG "e820 update range: %016Lx - %016Lx ", |
| 439 | (unsigned long long) start, |
| 440 | (unsigned long long) end); |
| 441 | e820_print_type(old_type); |
| 442 | printk(KERN_CONT " ==> "); |
| 443 | e820_print_type(new_type); |
| 444 | printk(KERN_CONT "\n"); |
| 445 | |
Jan Beulich | 5c0e6f0 | 2009-03-12 13:07:23 +0000 | [diff] [blame] | 446 | for (i = 0; i < e820x->nr_map; i++) { |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 447 | struct e820entry *ei = &e820x->map[i]; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 448 | u64 final_start, final_end; |
Yinghai Lu | 78a8b35b | 2009-03-12 22:36:01 -0700 | [diff] [blame] | 449 | u64 ei_end; |
| 450 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 451 | if (ei->type != old_type) |
| 452 | continue; |
Yinghai Lu | 78a8b35b | 2009-03-12 22:36:01 -0700 | [diff] [blame] | 453 | |
| 454 | ei_end = ei->addr + ei->size; |
| 455 | /* totally covered by new range? */ |
| 456 | if (ei->addr >= start && ei_end <= end) { |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 457 | ei->type = new_type; |
| 458 | real_updated_size += ei->size; |
| 459 | continue; |
| 460 | } |
Yinghai Lu | 78a8b35b | 2009-03-12 22:36:01 -0700 | [diff] [blame] | 461 | |
| 462 | /* new range is totally covered? */ |
| 463 | if (ei->addr < start && ei_end > end) { |
| 464 | __e820_add_region(e820x, start, size, new_type); |
| 465 | __e820_add_region(e820x, end, ei_end - end, ei->type); |
| 466 | ei->size = start - ei->addr; |
| 467 | real_updated_size += size; |
| 468 | continue; |
| 469 | } |
| 470 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 471 | /* partially covered */ |
| 472 | final_start = max(start, ei->addr); |
Yinghai Lu | 78a8b35b | 2009-03-12 22:36:01 -0700 | [diff] [blame] | 473 | final_end = min(end, ei_end); |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 474 | if (final_start >= final_end) |
| 475 | continue; |
Jan Beulich | 5c0e6f0 | 2009-03-12 13:07:23 +0000 | [diff] [blame] | 476 | |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 477 | __e820_add_region(e820x, final_start, final_end - final_start, |
| 478 | new_type); |
Jan Beulich | 5c0e6f0 | 2009-03-12 13:07:23 +0000 | [diff] [blame] | 479 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 480 | real_updated_size += final_end - final_start; |
Yinghai Lu | 976dd4d | 2008-06-24 14:55:32 -0700 | [diff] [blame] | 481 | |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 482 | /* |
| 483 | * left range could be head or tail, so need to update |
| 484 | * size at first. |
| 485 | */ |
| 486 | ei->size -= final_end - final_start; |
Yinghai Lu | 976dd4d | 2008-06-24 14:55:32 -0700 | [diff] [blame] | 487 | if (ei->addr < final_start) |
| 488 | continue; |
| 489 | ei->addr = final_end; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 490 | } |
| 491 | return real_updated_size; |
| 492 | } |
| 493 | |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 494 | u64 __init e820_update_range(u64 start, u64 size, unsigned old_type, |
| 495 | unsigned new_type) |
| 496 | { |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 497 | return __e820_update_range(&e820, start, size, old_type, new_type); |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static u64 __init e820_update_range_saved(u64 start, u64 size, |
| 501 | unsigned old_type, unsigned new_type) |
| 502 | { |
Yinghai Lu | 773e673 | 2009-03-12 21:35:18 -0700 | [diff] [blame] | 503 | return __e820_update_range(&e820_saved, start, size, old_type, |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 504 | new_type); |
| 505 | } |
| 506 | |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 507 | /* make e820 not cover the range */ |
| 508 | u64 __init e820_remove_range(u64 start, u64 size, unsigned old_type, |
| 509 | int checktype) |
| 510 | { |
| 511 | int i; |
Yinghai Lu | 1b5576e | 2010-01-22 11:21:04 +0800 | [diff] [blame] | 512 | u64 end; |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 513 | u64 real_removed_size = 0; |
| 514 | |
Yinghai Lu | 232b957 | 2008-06-24 14:58:38 -0700 | [diff] [blame] | 515 | if (size > (ULLONG_MAX - start)) |
| 516 | size = ULLONG_MAX - start; |
| 517 | |
Yinghai Lu | 1b5576e | 2010-01-22 11:21:04 +0800 | [diff] [blame] | 518 | end = start + size; |
| 519 | printk(KERN_DEBUG "e820 remove range: %016Lx - %016Lx ", |
| 520 | (unsigned long long) start, |
| 521 | (unsigned long long) end); |
Yinghai Lu | 9f3a5f5 | 2010-03-29 22:38:29 -0700 | [diff] [blame] | 522 | if (checktype) |
| 523 | e820_print_type(old_type); |
Yinghai Lu | 1b5576e | 2010-01-22 11:21:04 +0800 | [diff] [blame] | 524 | printk(KERN_CONT "\n"); |
| 525 | |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 526 | for (i = 0; i < e820.nr_map; i++) { |
| 527 | struct e820entry *ei = &e820.map[i]; |
| 528 | u64 final_start, final_end; |
Yinghai Lu | 9f3a5f5 | 2010-03-29 22:38:29 -0700 | [diff] [blame] | 529 | u64 ei_end; |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 530 | |
| 531 | if (checktype && ei->type != old_type) |
| 532 | continue; |
Yinghai Lu | 9f3a5f5 | 2010-03-29 22:38:29 -0700 | [diff] [blame] | 533 | |
| 534 | ei_end = ei->addr + ei->size; |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 535 | /* totally covered? */ |
Yinghai Lu | 9f3a5f5 | 2010-03-29 22:38:29 -0700 | [diff] [blame] | 536 | if (ei->addr >= start && ei_end <= end) { |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 537 | real_removed_size += ei->size; |
| 538 | memset(ei, 0, sizeof(struct e820entry)); |
| 539 | continue; |
| 540 | } |
Yinghai Lu | 9f3a5f5 | 2010-03-29 22:38:29 -0700 | [diff] [blame] | 541 | |
| 542 | /* new range is totally covered? */ |
| 543 | if (ei->addr < start && ei_end > end) { |
| 544 | e820_add_region(end, ei_end - end, ei->type); |
| 545 | ei->size = start - ei->addr; |
| 546 | real_removed_size += size; |
| 547 | continue; |
| 548 | } |
| 549 | |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 550 | /* partially covered */ |
| 551 | final_start = max(start, ei->addr); |
Yinghai Lu | 9f3a5f5 | 2010-03-29 22:38:29 -0700 | [diff] [blame] | 552 | final_end = min(end, ei_end); |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 553 | if (final_start >= final_end) |
| 554 | continue; |
| 555 | real_removed_size += final_end - final_start; |
| 556 | |
Yinghai Lu | 9f3a5f5 | 2010-03-29 22:38:29 -0700 | [diff] [blame] | 557 | /* |
| 558 | * left range could be head or tail, so need to update |
| 559 | * size at first. |
| 560 | */ |
Yinghai Lu | 7a1fd98 | 2008-06-21 14:48:05 -0700 | [diff] [blame] | 561 | ei->size -= final_end - final_start; |
| 562 | if (ei->addr < final_start) |
| 563 | continue; |
| 564 | ei->addr = final_end; |
| 565 | } |
| 566 | return real_removed_size; |
| 567 | } |
| 568 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 569 | void __init update_e820(void) |
| 570 | { |
Jaswinder Singh Rajput | ba63903 | 2009-03-23 02:13:01 +0530 | [diff] [blame] | 571 | u32 nr_map; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 572 | |
| 573 | nr_map = e820.nr_map; |
Paul Jackson | c3965bd | 2008-05-14 08:15:34 -0700 | [diff] [blame] | 574 | if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map)) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 575 | return; |
| 576 | e820.nr_map = nr_map; |
| 577 | printk(KERN_INFO "modified physical RAM map:\n"); |
| 578 | e820_print_map("modified"); |
| 579 | } |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 580 | static void __init update_e820_saved(void) |
| 581 | { |
Jaswinder Singh Rajput | ba63903 | 2009-03-23 02:13:01 +0530 | [diff] [blame] | 582 | u32 nr_map; |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 583 | |
| 584 | nr_map = e820_saved.nr_map; |
| 585 | if (sanitize_e820_map(e820_saved.map, ARRAY_SIZE(e820_saved.map), &nr_map)) |
| 586 | return; |
| 587 | e820_saved.nr_map = nr_map; |
| 588 | } |
Alok Kataria | fd6493e | 2008-06-25 11:02:42 -0700 | [diff] [blame] | 589 | #define MAX_GAP_END 0x100000000ull |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 590 | /* |
Alok Kataria | fd6493e | 2008-06-25 11:02:42 -0700 | [diff] [blame] | 591 | * Search for a gap in the e820 memory space from start_addr to end_addr. |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 592 | */ |
Alok Kataria | 3381959 | 2008-06-24 11:48:30 -0700 | [diff] [blame] | 593 | __init int e820_search_gap(unsigned long *gapstart, unsigned long *gapsize, |
Alok Kataria | fd6493e | 2008-06-25 11:02:42 -0700 | [diff] [blame] | 594 | unsigned long start_addr, unsigned long long end_addr) |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 595 | { |
Alok Kataria | fd6493e | 2008-06-25 11:02:42 -0700 | [diff] [blame] | 596 | unsigned long long last; |
Alok Kataria | 3381959 | 2008-06-24 11:48:30 -0700 | [diff] [blame] | 597 | int i = e820.nr_map; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 598 | int found = 0; |
| 599 | |
Alok Kataria | fd6493e | 2008-06-25 11:02:42 -0700 | [diff] [blame] | 600 | last = (end_addr && end_addr < MAX_GAP_END) ? end_addr : MAX_GAP_END; |
| 601 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 602 | while (--i >= 0) { |
| 603 | unsigned long long start = e820.map[i].addr; |
| 604 | unsigned long long end = start + e820.map[i].size; |
| 605 | |
Alok Kataria | 3381959 | 2008-06-24 11:48:30 -0700 | [diff] [blame] | 606 | if (end < start_addr) |
| 607 | continue; |
| 608 | |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 609 | /* |
| 610 | * Since "last" is at most 4GB, we know we'll |
| 611 | * fit in 32 bits if this condition is true |
| 612 | */ |
| 613 | if (last > end) { |
| 614 | unsigned long gap = last - end; |
| 615 | |
Alok Kataria | 3381959 | 2008-06-24 11:48:30 -0700 | [diff] [blame] | 616 | if (gap >= *gapsize) { |
| 617 | *gapsize = gap; |
| 618 | *gapstart = end; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 619 | found = 1; |
| 620 | } |
| 621 | } |
| 622 | if (start < last) |
| 623 | last = start; |
| 624 | } |
Alok Kataria | 3381959 | 2008-06-24 11:48:30 -0700 | [diff] [blame] | 625 | return found; |
| 626 | } |
| 627 | |
| 628 | /* |
| 629 | * Search for the biggest gap in the low 32 bits of the e820 |
| 630 | * memory space. We pass this space to PCI to assign MMIO resources |
| 631 | * for hotplug or unconfigured devices in. |
| 632 | * Hopefully the BIOS let enough space left. |
| 633 | */ |
| 634 | __init void e820_setup_gap(void) |
| 635 | { |
Yinghai Lu | 5d423cc | 2009-05-06 08:07:52 -0700 | [diff] [blame] | 636 | unsigned long gapstart, gapsize; |
Alok Kataria | 3381959 | 2008-06-24 11:48:30 -0700 | [diff] [blame] | 637 | int found; |
| 638 | |
| 639 | gapstart = 0x10000000; |
| 640 | gapsize = 0x400000; |
Alok Kataria | fd6493e | 2008-06-25 11:02:42 -0700 | [diff] [blame] | 641 | found = e820_search_gap(&gapstart, &gapsize, 0, MAX_GAP_END); |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 642 | |
| 643 | #ifdef CONFIG_X86_64 |
| 644 | if (!found) { |
Yinghai Lu | c987d12 | 2008-06-24 22:14:09 -0700 | [diff] [blame] | 645 | gapstart = (max_pfn << PAGE_SHIFT) + 1024*1024; |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 646 | printk(KERN_ERR |
| 647 | "PCI: Warning: Cannot find a gap in the 32bit address range\n" |
| 648 | "PCI: Unassigned devices with 32bit resource registers may break!\n"); |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 649 | } |
| 650 | #endif |
| 651 | |
| 652 | /* |
Yinghai Lu | 5d423cc | 2009-05-06 08:07:52 -0700 | [diff] [blame] | 653 | * e820_reserve_resources_late protect stolen RAM already |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 654 | */ |
Yinghai Lu | 5d423cc | 2009-05-06 08:07:52 -0700 | [diff] [blame] | 655 | pci_mem_start = gapstart; |
Yinghai Lu | b79cd8f | 2008-05-11 00:30:15 -0700 | [diff] [blame] | 656 | |
| 657 | printk(KERN_INFO |
| 658 | "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n", |
| 659 | pci_mem_start, gapstart, gapsize); |
| 660 | } |
| 661 | |
Huang, Ying | 8c5beb5 | 2008-06-11 11:33:39 +0800 | [diff] [blame] | 662 | /** |
| 663 | * Because of the size limitation of struct boot_params, only first |
| 664 | * 128 E820 memory entries are passed to kernel via |
| 665 | * boot_params.e820_map, others are passed via SETUP_E820_EXT node of |
| 666 | * linked list of struct setup_data, which is parsed here. |
| 667 | */ |
| 668 | void __init parse_e820_ext(struct setup_data *sdata, unsigned long pa_data) |
| 669 | { |
| 670 | u32 map_len; |
| 671 | int entries; |
| 672 | struct e820entry *extmap; |
| 673 | |
| 674 | entries = sdata->len / sizeof(struct e820entry); |
| 675 | map_len = sdata->len + sizeof(struct setup_data); |
| 676 | if (map_len > PAGE_SIZE) |
| 677 | sdata = early_ioremap(pa_data, map_len); |
| 678 | extmap = (struct e820entry *)(sdata->data); |
Yinghai Lu | dc8e812 | 2008-07-01 20:02:16 -0700 | [diff] [blame] | 679 | __append_e820_map(extmap, entries); |
Huang, Ying | 8c5beb5 | 2008-06-11 11:33:39 +0800 | [diff] [blame] | 680 | sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map); |
| 681 | if (map_len > PAGE_SIZE) |
| 682 | early_iounmap(sdata, map_len); |
| 683 | printk(KERN_INFO "extended physical RAM map:\n"); |
| 684 | e820_print_map("extended"); |
| 685 | } |
| 686 | |
Yinghai Lu | bf62f39 | 2008-05-20 20:10:58 -0700 | [diff] [blame] | 687 | #if defined(CONFIG_X86_64) || \ |
| 688 | (defined(CONFIG_X86_32) && defined(CONFIG_HIBERNATION)) |
| 689 | /** |
| 690 | * Find the ranges of physical addresses that do not correspond to |
| 691 | * e820 RAM areas and mark the corresponding pages as nosave for |
| 692 | * hibernation (32 bit) or software suspend and suspend to RAM (64 bit). |
| 693 | * |
| 694 | * This function requires the e820 map to be sorted and without any |
| 695 | * overlapping entries and assumes the first e820 area to be RAM. |
| 696 | */ |
| 697 | void __init e820_mark_nosave_regions(unsigned long limit_pfn) |
| 698 | { |
| 699 | int i; |
| 700 | unsigned long pfn; |
| 701 | |
| 702 | pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size); |
| 703 | for (i = 1; i < e820.nr_map; i++) { |
| 704 | struct e820entry *ei = &e820.map[i]; |
| 705 | |
| 706 | if (pfn < PFN_UP(ei->addr)) |
| 707 | register_nosave_region(pfn, PFN_UP(ei->addr)); |
| 708 | |
| 709 | pfn = PFN_DOWN(ei->addr + ei->size); |
Yinghai Lu | 28bb223 | 2008-06-30 16:20:54 -0700 | [diff] [blame] | 710 | if (ei->type != E820_RAM && ei->type != E820_RESERVED_KERN) |
Yinghai Lu | bf62f39 | 2008-05-20 20:10:58 -0700 | [diff] [blame] | 711 | register_nosave_region(PFN_UP(ei->addr), pfn); |
| 712 | |
| 713 | if (pfn >= limit_pfn) |
| 714 | break; |
| 715 | } |
| 716 | } |
| 717 | #endif |
Yinghai Lu | a4c81cf | 2008-05-18 01:18:57 -0700 | [diff] [blame] | 718 | |
Rafael J. Wysocki | b69edc7 | 2008-10-31 01:02:41 +0100 | [diff] [blame] | 719 | #ifdef CONFIG_HIBERNATION |
| 720 | /** |
| 721 | * Mark ACPI NVS memory region, so that we can save/restore it during |
| 722 | * hibernation and the subsequent resume. |
| 723 | */ |
| 724 | static int __init e820_mark_nvs_memory(void) |
| 725 | { |
| 726 | int i; |
| 727 | |
| 728 | for (i = 0; i < e820.nr_map; i++) { |
| 729 | struct e820entry *ei = &e820.map[i]; |
| 730 | |
| 731 | if (ei->type == E820_NVS) |
Matthew Garrett | dd4c4f1 | 2010-05-28 16:32:14 -0400 | [diff] [blame] | 732 | suspend_nvs_register(ei->addr, ei->size); |
Rafael J. Wysocki | b69edc7 | 2008-10-31 01:02:41 +0100 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | core_initcall(e820_mark_nvs_memory); |
| 738 | #endif |
| 739 | |
Yinghai Lu | a4c81cf | 2008-05-18 01:18:57 -0700 | [diff] [blame] | 740 | /* |
Yinghai Lu | efdd0e8 | 2010-02-10 01:20:26 -0800 | [diff] [blame] | 741 | * Find a free area with specified alignment in a specific range. |
| 742 | */ |
| 743 | u64 __init find_e820_area(u64 start, u64 end, u64 size, u64 align) |
| 744 | { |
| 745 | int i; |
| 746 | |
| 747 | for (i = 0; i < e820.nr_map; i++) { |
| 748 | struct e820entry *ei = &e820.map[i]; |
| 749 | u64 addr; |
| 750 | u64 ei_start, ei_last; |
| 751 | |
| 752 | if (ei->type != E820_RAM) |
| 753 | continue; |
| 754 | |
| 755 | ei_last = ei->addr + ei->size; |
| 756 | ei_start = ei->addr; |
| 757 | addr = find_early_area(ei_start, ei_last, start, end, |
| 758 | size, align); |
| 759 | |
| 760 | if (addr != -1ULL) |
| 761 | return addr; |
| 762 | } |
| 763 | return -1ULL; |
| 764 | } |
| 765 | |
Yinghai Lu | dd645ce | 2010-02-10 01:20:30 -0800 | [diff] [blame] | 766 | u64 __init find_fw_memmap_area(u64 start, u64 end, u64 size, u64 align) |
| 767 | { |
| 768 | return find_e820_area(start, end, size, align); |
| 769 | } |
Yinghai Lu | 580e0ad | 2010-02-16 18:40:35 -0800 | [diff] [blame] | 770 | |
| 771 | u64 __init get_max_mapped(void) |
| 772 | { |
| 773 | u64 end = max_pfn_mapped; |
| 774 | |
| 775 | end <<= PAGE_SHIFT; |
| 776 | |
| 777 | return end; |
| 778 | } |
Yinghai Lu | efdd0e8 | 2010-02-10 01:20:26 -0800 | [diff] [blame] | 779 | /* |
| 780 | * Find next free range after *start |
| 781 | */ |
| 782 | u64 __init find_e820_area_size(u64 start, u64 *sizep, u64 align) |
| 783 | { |
| 784 | int i; |
| 785 | |
| 786 | for (i = 0; i < e820.nr_map; i++) { |
| 787 | struct e820entry *ei = &e820.map[i]; |
| 788 | u64 addr; |
| 789 | u64 ei_start, ei_last; |
| 790 | |
| 791 | if (ei->type != E820_RAM) |
| 792 | continue; |
| 793 | |
| 794 | ei_last = ei->addr + ei->size; |
| 795 | ei_start = ei->addr; |
| 796 | addr = find_early_area_size(ei_start, ei_last, start, |
| 797 | sizep, align); |
| 798 | |
| 799 | if (addr != -1ULL) |
| 800 | return addr; |
| 801 | } |
| 802 | |
| 803 | return -1ULL; |
| 804 | } |
| 805 | |
| 806 | /* |
Yinghai Lu | 2944e16 | 2008-06-01 13:17:38 -0700 | [diff] [blame] | 807 | * pre allocated 4k and reserved it in e820 |
| 808 | */ |
| 809 | u64 __init early_reserve_e820(u64 startt, u64 sizet, u64 align) |
| 810 | { |
| 811 | u64 size = 0; |
| 812 | u64 addr; |
| 813 | u64 start; |
| 814 | |
Jan Beulich | 6143876 | 2009-05-06 13:02:19 +0100 | [diff] [blame] | 815 | for (start = startt; ; start += size) { |
Yinghai Lu | 2944e16 | 2008-06-01 13:17:38 -0700 | [diff] [blame] | 816 | start = find_e820_area_size(start, &size, align); |
Jan Beulich | 6143876 | 2009-05-06 13:02:19 +0100 | [diff] [blame] | 817 | if (!(start + 1)) |
| 818 | return 0; |
| 819 | if (size >= sizet) |
| 820 | break; |
| 821 | } |
Yinghai Lu | 2944e16 | 2008-06-01 13:17:38 -0700 | [diff] [blame] | 822 | |
Jan Beulich | 5c0e6f0 | 2009-03-12 13:07:23 +0000 | [diff] [blame] | 823 | #ifdef CONFIG_X86_32 |
| 824 | if (start >= MAXMEM) |
| 825 | return 0; |
| 826 | if (start + size > MAXMEM) |
| 827 | size = MAXMEM - start; |
| 828 | #endif |
| 829 | |
Yinghai Lu | 2944e16 | 2008-06-01 13:17:38 -0700 | [diff] [blame] | 830 | addr = round_down(start + size - sizet, align); |
Jan Beulich | 5c0e6f0 | 2009-03-12 13:07:23 +0000 | [diff] [blame] | 831 | if (addr < start) |
| 832 | return 0; |
Yinghai Lu | d0be6bd | 2008-06-15 18:58:51 -0700 | [diff] [blame] | 833 | e820_update_range(addr, sizet, E820_RAM, E820_RESERVED); |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 834 | e820_update_range_saved(addr, sizet, E820_RAM, E820_RESERVED); |
Yinghai Lu | 2944e16 | 2008-06-01 13:17:38 -0700 | [diff] [blame] | 835 | printk(KERN_INFO "update e820 for early_reserve_e820\n"); |
| 836 | update_e820(); |
Yinghai Lu | fc9036e | 2008-07-03 11:39:00 -0700 | [diff] [blame] | 837 | update_e820_saved(); |
Yinghai Lu | 2944e16 | 2008-06-01 13:17:38 -0700 | [diff] [blame] | 838 | |
| 839 | return addr; |
| 840 | } |
| 841 | |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 842 | #ifdef CONFIG_X86_32 |
| 843 | # ifdef CONFIG_X86_PAE |
| 844 | # define MAX_ARCH_PFN (1ULL<<(36-PAGE_SHIFT)) |
| 845 | # else |
| 846 | # define MAX_ARCH_PFN (1ULL<<(32-PAGE_SHIFT)) |
| 847 | # endif |
| 848 | #else /* CONFIG_X86_32 */ |
Yinghai Lu | bd70e52 | 2008-06-04 13:21:29 -0700 | [diff] [blame] | 849 | # define MAX_ARCH_PFN MAXMEM>>PAGE_SHIFT |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 850 | #endif |
| 851 | |
| 852 | /* |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 853 | * Find the highest page frame number we have available |
| 854 | */ |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 855 | static unsigned long __init e820_end_pfn(unsigned long limit_pfn, unsigned type) |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 856 | { |
Yinghai Lu | 2dc807b | 2008-07-08 18:56:38 -0700 | [diff] [blame] | 857 | int i; |
| 858 | unsigned long last_pfn = 0; |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 859 | unsigned long max_arch_pfn = MAX_ARCH_PFN; |
| 860 | |
Yinghai Lu | 2dc807b | 2008-07-08 18:56:38 -0700 | [diff] [blame] | 861 | for (i = 0; i < e820.nr_map; i++) { |
| 862 | struct e820entry *ei = &e820.map[i]; |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 863 | unsigned long start_pfn; |
Yinghai Lu | 2dc807b | 2008-07-08 18:56:38 -0700 | [diff] [blame] | 864 | unsigned long end_pfn; |
| 865 | |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 866 | if (ei->type != type) |
Yinghai Lu | c22d4c1 | 2008-07-09 03:01:14 -0700 | [diff] [blame] | 867 | continue; |
Yinghai Lu | c22d4c1 | 2008-07-09 03:01:14 -0700 | [diff] [blame] | 868 | |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 869 | start_pfn = ei->addr >> PAGE_SHIFT; |
Yinghai Lu | 2dc807b | 2008-07-08 18:56:38 -0700 | [diff] [blame] | 870 | end_pfn = (ei->addr + ei->size) >> PAGE_SHIFT; |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 871 | |
| 872 | if (start_pfn >= limit_pfn) |
| 873 | continue; |
| 874 | if (end_pfn > limit_pfn) { |
| 875 | last_pfn = limit_pfn; |
| 876 | break; |
| 877 | } |
Yinghai Lu | 2dc807b | 2008-07-08 18:56:38 -0700 | [diff] [blame] | 878 | if (end_pfn > last_pfn) |
| 879 | last_pfn = end_pfn; |
| 880 | } |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 881 | |
| 882 | if (last_pfn > max_arch_pfn) |
| 883 | last_pfn = max_arch_pfn; |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 884 | |
Paul Jackson | 5dab8ec | 2008-06-25 05:44:40 -0700 | [diff] [blame] | 885 | printk(KERN_INFO "last_pfn = %#lx max_arch_pfn = %#lx\n", |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 886 | last_pfn, max_arch_pfn); |
| 887 | return last_pfn; |
| 888 | } |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 889 | unsigned long __init e820_end_of_ram_pfn(void) |
| 890 | { |
| 891 | return e820_end_pfn(MAX_ARCH_PFN, E820_RAM); |
| 892 | } |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 893 | |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 894 | unsigned long __init e820_end_of_low_ram_pfn(void) |
| 895 | { |
| 896 | return e820_end_pfn(1UL<<(32 - PAGE_SHIFT), E820_RAM); |
| 897 | } |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 898 | /* |
| 899 | * Finds an active region in the address range from start_pfn to last_pfn and |
| 900 | * returns its range in ei_startpfn and ei_endpfn for the e820 entry. |
| 901 | */ |
| 902 | int __init e820_find_active_region(const struct e820entry *ei, |
| 903 | unsigned long start_pfn, |
| 904 | unsigned long last_pfn, |
| 905 | unsigned long *ei_startpfn, |
| 906 | unsigned long *ei_endpfn) |
| 907 | { |
| 908 | u64 align = PAGE_SIZE; |
| 909 | |
| 910 | *ei_startpfn = round_up(ei->addr, align) >> PAGE_SHIFT; |
| 911 | *ei_endpfn = round_down(ei->addr + ei->size, align) >> PAGE_SHIFT; |
| 912 | |
| 913 | /* Skip map entries smaller than a page */ |
| 914 | if (*ei_startpfn >= *ei_endpfn) |
| 915 | return 0; |
| 916 | |
| 917 | /* Skip if map is outside the node */ |
| 918 | if (ei->type != E820_RAM || *ei_endpfn <= start_pfn || |
| 919 | *ei_startpfn >= last_pfn) |
| 920 | return 0; |
| 921 | |
| 922 | /* Check for overlaps */ |
| 923 | if (*ei_startpfn < start_pfn) |
| 924 | *ei_startpfn = start_pfn; |
| 925 | if (*ei_endpfn > last_pfn) |
| 926 | *ei_endpfn = last_pfn; |
| 927 | |
Yinghai Lu | ee0c80f | 2008-06-03 19:34:00 -0700 | [diff] [blame] | 928 | return 1; |
| 929 | } |
| 930 | |
| 931 | /* Walk the e820 map and register active regions within a node */ |
| 932 | void __init e820_register_active_regions(int nid, unsigned long start_pfn, |
| 933 | unsigned long last_pfn) |
| 934 | { |
| 935 | unsigned long ei_startpfn; |
| 936 | unsigned long ei_endpfn; |
| 937 | int i; |
| 938 | |
| 939 | for (i = 0; i < e820.nr_map; i++) |
| 940 | if (e820_find_active_region(&e820.map[i], |
| 941 | start_pfn, last_pfn, |
| 942 | &ei_startpfn, &ei_endpfn)) |
| 943 | add_active_range(nid, ei_startpfn, ei_endpfn); |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * Find the hole size (in bytes) in the memory range. |
| 948 | * @start: starting address of the memory range to scan |
| 949 | * @end: ending address of the memory range to scan |
| 950 | */ |
| 951 | u64 __init e820_hole_size(u64 start, u64 end) |
| 952 | { |
| 953 | unsigned long start_pfn = start >> PAGE_SHIFT; |
| 954 | unsigned long last_pfn = end >> PAGE_SHIFT; |
| 955 | unsigned long ei_startpfn, ei_endpfn, ram = 0; |
| 956 | int i; |
| 957 | |
| 958 | for (i = 0; i < e820.nr_map; i++) { |
| 959 | if (e820_find_active_region(&e820.map[i], |
| 960 | start_pfn, last_pfn, |
| 961 | &ei_startpfn, &ei_endpfn)) |
| 962 | ram += ei_endpfn - ei_startpfn; |
| 963 | } |
| 964 | return end - start - ((u64)ram << PAGE_SHIFT); |
| 965 | } |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 966 | |
| 967 | static void early_panic(char *msg) |
| 968 | { |
| 969 | early_printk(msg); |
| 970 | panic(msg); |
| 971 | } |
| 972 | |
Yinghai Lu | 69a7704 | 2008-07-10 04:17:00 -0700 | [diff] [blame] | 973 | static int userdef __initdata; |
| 974 | |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 975 | /* "mem=nopentium" disables the 4MB page tables. */ |
| 976 | static int __init parse_memopt(char *p) |
| 977 | { |
| 978 | u64 mem_size; |
| 979 | |
| 980 | if (!p) |
| 981 | return -EINVAL; |
| 982 | |
| 983 | #ifdef CONFIG_X86_32 |
| 984 | if (!strcmp(p, "nopentium")) { |
| 985 | setup_clear_cpu_cap(X86_FEATURE_PSE); |
| 986 | return 0; |
| 987 | } |
| 988 | #endif |
| 989 | |
Yinghai Lu | 69a7704 | 2008-07-10 04:17:00 -0700 | [diff] [blame] | 990 | userdef = 1; |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 991 | mem_size = memparse(p, &p); |
Yinghai Lu | 69a7704 | 2008-07-10 04:17:00 -0700 | [diff] [blame] | 992 | e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1); |
Bernhard Walle | 611dfd7 | 2008-06-25 21:39:16 +0200 | [diff] [blame] | 993 | |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 994 | return 0; |
| 995 | } |
| 996 | early_param("mem", parse_memopt); |
| 997 | |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 998 | static int __init parse_memmap_opt(char *p) |
| 999 | { |
| 1000 | char *oldp; |
| 1001 | u64 start_at, mem_size; |
| 1002 | |
Cyrill Gorcunov | a737abd | 2008-07-05 15:53:39 +0400 | [diff] [blame] | 1003 | if (!p) |
| 1004 | return -EINVAL; |
| 1005 | |
Prarit Bhargava | d6be118a | 2008-09-09 09:56:08 -0400 | [diff] [blame] | 1006 | if (!strncmp(p, "exactmap", 8)) { |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 1007 | #ifdef CONFIG_CRASH_DUMP |
| 1008 | /* |
| 1009 | * If we are doing a crash dump, we still need to know |
| 1010 | * the real mem size before original memory map is |
| 1011 | * reset. |
| 1012 | */ |
Yinghai Lu | f361a45 | 2008-07-10 20:38:26 -0700 | [diff] [blame] | 1013 | saved_max_pfn = e820_end_of_ram_pfn(); |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 1014 | #endif |
| 1015 | e820.nr_map = 0; |
| 1016 | userdef = 1; |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | oldp = p; |
| 1021 | mem_size = memparse(p, &p); |
| 1022 | if (p == oldp) |
| 1023 | return -EINVAL; |
| 1024 | |
| 1025 | userdef = 1; |
| 1026 | if (*p == '@') { |
| 1027 | start_at = memparse(p+1, &p); |
Yinghai Lu | d0be6bd | 2008-06-15 18:58:51 -0700 | [diff] [blame] | 1028 | e820_add_region(start_at, mem_size, E820_RAM); |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 1029 | } else if (*p == '#') { |
| 1030 | start_at = memparse(p+1, &p); |
Yinghai Lu | d0be6bd | 2008-06-15 18:58:51 -0700 | [diff] [blame] | 1031 | e820_add_region(start_at, mem_size, E820_ACPI); |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 1032 | } else if (*p == '$') { |
| 1033 | start_at = memparse(p+1, &p); |
Yinghai Lu | d0be6bd | 2008-06-15 18:58:51 -0700 | [diff] [blame] | 1034 | e820_add_region(start_at, mem_size, E820_RESERVED); |
Yinghai Lu | 7b479be | 2008-07-12 22:57:07 -0700 | [diff] [blame] | 1035 | } else |
Yinghai Lu | 69a7704 | 2008-07-10 04:17:00 -0700 | [diff] [blame] | 1036 | e820_remove_range(mem_size, ULLONG_MAX - mem_size, E820_RAM, 1); |
Yinghai Lu | 7b479be | 2008-07-12 22:57:07 -0700 | [diff] [blame] | 1037 | |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 1038 | return *p == '\0' ? 0 : -EINVAL; |
| 1039 | } |
| 1040 | early_param("memmap", parse_memmap_opt); |
| 1041 | |
| 1042 | void __init finish_e820_parsing(void) |
| 1043 | { |
| 1044 | if (userdef) { |
Jaswinder Singh Rajput | ba63903 | 2009-03-23 02:13:01 +0530 | [diff] [blame] | 1045 | u32 nr = e820.nr_map; |
Yinghai Lu | ab4a465 | 2008-06-10 12:55:54 -0700 | [diff] [blame] | 1046 | |
| 1047 | if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr) < 0) |
| 1048 | early_panic("Invalid user supplied memory map"); |
| 1049 | e820.nr_map = nr; |
| 1050 | |
| 1051 | printk(KERN_INFO "user-defined physical RAM map:\n"); |
| 1052 | e820_print_map("user"); |
| 1053 | } |
| 1054 | } |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1055 | |
Bernhard Walle | 5dfcf14 | 2008-06-27 13:12:55 +0200 | [diff] [blame] | 1056 | static inline const char *e820_type_to_string(int e820_type) |
| 1057 | { |
| 1058 | switch (e820_type) { |
| 1059 | case E820_RESERVED_KERN: |
| 1060 | case E820_RAM: return "System RAM"; |
| 1061 | case E820_ACPI: return "ACPI Tables"; |
| 1062 | case E820_NVS: return "ACPI Non-volatile Storage"; |
Cihula, Joseph | 671eef8 | 2008-08-20 16:43:07 -0700 | [diff] [blame] | 1063 | case E820_UNUSABLE: return "Unusable memory"; |
Bernhard Walle | 5dfcf14 | 2008-06-27 13:12:55 +0200 | [diff] [blame] | 1064 | default: return "reserved"; |
| 1065 | } |
| 1066 | } |
| 1067 | |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1068 | /* |
| 1069 | * Mark e820 reserved areas as busy for the resource manager. |
| 1070 | */ |
Ingo Molnar | a5444d1 | 2008-08-29 08:09:23 +0200 | [diff] [blame] | 1071 | static struct resource __initdata *e820_res; |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1072 | void __init e820_reserve_resources(void) |
| 1073 | { |
| 1074 | int i; |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame] | 1075 | struct resource *res; |
Ingo Molnar | a5444d1 | 2008-08-29 08:09:23 +0200 | [diff] [blame] | 1076 | u64 end; |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1077 | |
Jan Beulich | 3c1596e | 2009-09-21 17:03:06 -0700 | [diff] [blame] | 1078 | res = alloc_bootmem(sizeof(struct resource) * e820.nr_map); |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame] | 1079 | e820_res = res; |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1080 | for (i = 0; i < e820.nr_map; i++) { |
Yinghai Lu | b4df32f | 2008-06-28 17:49:59 -0700 | [diff] [blame] | 1081 | end = e820.map[i].addr + e820.map[i].size - 1; |
Jeremy Fitzhardinge | 8308c54 | 2008-09-11 01:31:50 -0700 | [diff] [blame] | 1082 | if (end != (resource_size_t)end) { |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1083 | res++; |
| 1084 | continue; |
| 1085 | } |
Bernhard Walle | 5dfcf14 | 2008-06-27 13:12:55 +0200 | [diff] [blame] | 1086 | res->name = e820_type_to_string(e820.map[i].type); |
Yinghai Lu | b4df32f | 2008-06-28 17:49:59 -0700 | [diff] [blame] | 1087 | res->start = e820.map[i].addr; |
| 1088 | res->end = end; |
| 1089 | |
Linus Torvalds | 1f987577 | 2008-11-01 10:17:22 -0700 | [diff] [blame] | 1090 | res->flags = IORESOURCE_MEM; |
Ingo Molnar | a5444d1 | 2008-08-29 08:09:23 +0200 | [diff] [blame] | 1091 | |
| 1092 | /* |
| 1093 | * don't register the region that could be conflicted with |
| 1094 | * pci device BAR resource and insert them later in |
| 1095 | * pcibios_resource_survey() |
| 1096 | */ |
Linus Torvalds | 1f987577 | 2008-11-01 10:17:22 -0700 | [diff] [blame] | 1097 | if (e820.map[i].type != E820_RESERVED || res->start < (1ULL<<20)) { |
| 1098 | res->flags |= IORESOURCE_BUSY; |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame] | 1099 | insert_resource(&iomem_resource, res); |
Linus Torvalds | 1f987577 | 2008-11-01 10:17:22 -0700 | [diff] [blame] | 1100 | } |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1101 | res++; |
| 1102 | } |
Bernhard Walle | 5dfcf14 | 2008-06-27 13:12:55 +0200 | [diff] [blame] | 1103 | |
| 1104 | for (i = 0; i < e820_saved.nr_map; i++) { |
| 1105 | struct e820entry *entry = &e820_saved.map[i]; |
| 1106 | firmware_map_add_early(entry->addr, |
| 1107 | entry->addr + entry->size - 1, |
| 1108 | e820_type_to_string(entry->type)); |
| 1109 | } |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1110 | } |
| 1111 | |
Linus Torvalds | 45fbe3e | 2009-05-06 08:06:44 -0700 | [diff] [blame] | 1112 | /* How much should we pad RAM ending depending on where it is? */ |
| 1113 | static unsigned long ram_alignment(resource_size_t pos) |
| 1114 | { |
| 1115 | unsigned long mb = pos >> 20; |
| 1116 | |
| 1117 | /* To 64kB in the first megabyte */ |
| 1118 | if (!mb) |
| 1119 | return 64*1024; |
| 1120 | |
| 1121 | /* To 1MB in the first 16MB */ |
| 1122 | if (mb < 16) |
| 1123 | return 1024*1024; |
| 1124 | |
Yinghai Lu | 15b812f | 2009-10-11 14:17:16 -0700 | [diff] [blame] | 1125 | /* To 64MB for anything above that */ |
| 1126 | return 64*1024*1024; |
Linus Torvalds | 45fbe3e | 2009-05-06 08:06:44 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
Yinghai Lu | 7c5371c | 2009-07-01 12:32:18 -0700 | [diff] [blame] | 1129 | #define MAX_RESOURCE_SIZE ((resource_size_t)-1) |
| 1130 | |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame] | 1131 | void __init e820_reserve_resources_late(void) |
| 1132 | { |
| 1133 | int i; |
| 1134 | struct resource *res; |
| 1135 | |
| 1136 | res = e820_res; |
| 1137 | for (i = 0; i < e820.nr_map; i++) { |
Ingo Molnar | a5444d1 | 2008-08-29 08:09:23 +0200 | [diff] [blame] | 1138 | if (!res->parent && res->end) |
Linus Torvalds | 1f987577 | 2008-11-01 10:17:22 -0700 | [diff] [blame] | 1139 | insert_resource_expand_to_fit(&iomem_resource, res); |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame] | 1140 | res++; |
| 1141 | } |
Linus Torvalds | 45fbe3e | 2009-05-06 08:06:44 -0700 | [diff] [blame] | 1142 | |
| 1143 | /* |
| 1144 | * Try to bump up RAM regions to reasonable boundaries to |
| 1145 | * avoid stolen RAM: |
| 1146 | */ |
| 1147 | for (i = 0; i < e820.nr_map; i++) { |
Yinghai Lu | 7c5371c | 2009-07-01 12:32:18 -0700 | [diff] [blame] | 1148 | struct e820entry *entry = &e820.map[i]; |
| 1149 | u64 start, end; |
Linus Torvalds | 45fbe3e | 2009-05-06 08:06:44 -0700 | [diff] [blame] | 1150 | |
| 1151 | if (entry->type != E820_RAM) |
| 1152 | continue; |
| 1153 | start = entry->addr + entry->size; |
Yinghai Lu | 7c5371c | 2009-07-01 12:32:18 -0700 | [diff] [blame] | 1154 | end = round_up(start, ram_alignment(start)) - 1; |
| 1155 | if (end > MAX_RESOURCE_SIZE) |
| 1156 | end = MAX_RESOURCE_SIZE; |
| 1157 | if (start >= end) |
Linus Torvalds | 45fbe3e | 2009-05-06 08:06:44 -0700 | [diff] [blame] | 1158 | continue; |
Yinghai Lu | 79c6016 | 2010-02-10 01:20:14 -0800 | [diff] [blame] | 1159 | printk(KERN_DEBUG "reserve RAM buffer: %016llx - %016llx ", |
| 1160 | start, end); |
Yinghai Lu | 7c5371c | 2009-07-01 12:32:18 -0700 | [diff] [blame] | 1161 | reserve_region_with_split(&iomem_resource, start, end, |
| 1162 | "RAM buffer"); |
Linus Torvalds | 45fbe3e | 2009-05-06 08:06:44 -0700 | [diff] [blame] | 1163 | } |
Yinghai Lu | 58f7c98 | 2008-08-28 13:52:25 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
Yinghai Lu | 95a71a4 | 2008-06-18 17:27:08 -0700 | [diff] [blame] | 1166 | char *__init default_machine_specific_memory_setup(void) |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 1167 | { |
| 1168 | char *who = "BIOS-e820"; |
Jaswinder Singh Rajput | ba63903 | 2009-03-23 02:13:01 +0530 | [diff] [blame] | 1169 | u32 new_nr; |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 1170 | /* |
| 1171 | * Try to copy the BIOS-supplied E820-map. |
| 1172 | * |
| 1173 | * Otherwise fake a memory map; one section from 0k->640k, |
| 1174 | * the next section from 1mb->appropriate_mem_k |
| 1175 | */ |
| 1176 | new_nr = boot_params.e820_entries; |
| 1177 | sanitize_e820_map(boot_params.e820_map, |
| 1178 | ARRAY_SIZE(boot_params.e820_map), |
| 1179 | &new_nr); |
| 1180 | boot_params.e820_entries = new_nr; |
Yinghai Lu | dc8e812 | 2008-07-01 20:02:16 -0700 | [diff] [blame] | 1181 | if (append_e820_map(boot_params.e820_map, boot_params.e820_entries) |
| 1182 | < 0) { |
Yinghai Lu | 95a71a4 | 2008-06-18 17:27:08 -0700 | [diff] [blame] | 1183 | u64 mem_size; |
Yinghai Lu | 41c094f | 2008-06-16 13:03:31 -0700 | [diff] [blame] | 1184 | |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 1185 | /* compare results from other methods and take the greater */ |
| 1186 | if (boot_params.alt_mem_k |
| 1187 | < boot_params.screen_info.ext_mem_k) { |
| 1188 | mem_size = boot_params.screen_info.ext_mem_k; |
| 1189 | who = "BIOS-88"; |
| 1190 | } else { |
| 1191 | mem_size = boot_params.alt_mem_k; |
| 1192 | who = "BIOS-e801"; |
| 1193 | } |
| 1194 | |
| 1195 | e820.nr_map = 0; |
| 1196 | e820_add_region(0, LOWMEMSIZE(), E820_RAM); |
| 1197 | e820_add_region(HIGH_MEMORY, mem_size << 10, E820_RAM); |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | /* In case someone cares... */ |
| 1201 | return who; |
| 1202 | } |
| 1203 | |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 1204 | void __init setup_memory_map(void) |
| 1205 | { |
Yinghai Lu | 0be1552 | 2008-07-03 11:35:37 -0700 | [diff] [blame] | 1206 | char *who; |
| 1207 | |
Thomas Gleixner | 6b18ae3 | 2009-08-20 10:19:54 +0200 | [diff] [blame] | 1208 | who = x86_init.resources.memory_setup(); |
Yinghai Lu | 0be1552 | 2008-07-03 11:35:37 -0700 | [diff] [blame] | 1209 | memcpy(&e820_saved, &e820, sizeof(struct e820map)); |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 1210 | printk(KERN_INFO "BIOS-provided physical RAM map:\n"); |
Yinghai Lu | 0be1552 | 2008-07-03 11:35:37 -0700 | [diff] [blame] | 1211 | e820_print_map(who); |
Yinghai Lu | 064d25f | 2008-06-16 19:58:28 -0700 | [diff] [blame] | 1212 | } |