| 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 | |
|---|
| 20 | #include <asm/pgtable.h> |
|---|
| 21 | #include <asm/page.h> |
|---|
| 22 | #include <asm/e820.h> |
|---|
| 23 | #include <asm/proto.h> |
|---|
| 24 | #include <asm/bootsetup.h> |
|---|
| 25 | #include <asm/sections.h> |
|---|
| 26 | #include <xen/interface/memory.h> |
|---|
| 27 | |
|---|
| 28 | /* |
|---|
| 29 | * PFN of last memory page. |
|---|
| 30 | */ |
|---|
| 31 | unsigned long end_pfn; |
|---|
| 32 | EXPORT_SYMBOL(end_pfn); |
|---|
| 33 | |
|---|
| 34 | /* |
|---|
| 35 | * end_pfn only includes RAM, while end_pfn_map includes all e820 entries. |
|---|
| 36 | * The direct mapping extends to end_pfn_map, so that we can directly access |
|---|
| 37 | * apertures, ACPI and other tables without having to play with fixmaps. |
|---|
| 38 | */ |
|---|
| 39 | unsigned long end_pfn_map; |
|---|
| 40 | |
|---|
| 41 | /* |
|---|
| 42 | * Last pfn which the user wants to use. |
|---|
| 43 | */ |
|---|
| 44 | unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT; |
|---|
| 45 | |
|---|
| 46 | extern struct resource code_resource, data_resource; |
|---|
| 47 | |
|---|
| 48 | /* Check for some hardcoded bad areas that early boot is not allowed to touch */ |
|---|
| 49 | static inline int bad_addr(unsigned long *addrp, unsigned long size) |
|---|
| 50 | { |
|---|
| 51 | unsigned long addr = *addrp, last = addr + size; |
|---|
| 52 | |
|---|
| 53 | #ifndef CONFIG_XEN |
|---|
| 54 | /* various gunk below that needed for SMP startup */ |
|---|
| 55 | if (addr < 0x8000) { |
|---|
| 56 | *addrp = 0x8000; |
|---|
| 57 | return 1; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | /* direct mapping tables of the kernel */ |
|---|
| 61 | if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) { |
|---|
| 62 | *addrp = table_end << PAGE_SHIFT; |
|---|
| 63 | return 1; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /* initrd */ |
|---|
| 67 | #ifdef CONFIG_BLK_DEV_INITRD |
|---|
| 68 | if (LOADER_TYPE && INITRD_START && last >= INITRD_START && |
|---|
| 69 | addr < INITRD_START+INITRD_SIZE) { |
|---|
| 70 | *addrp = INITRD_START + INITRD_SIZE; |
|---|
| 71 | return 1; |
|---|
| 72 | } |
|---|
| 73 | #endif |
|---|
| 74 | /* kernel code + 640k memory hole (later should not be needed, but |
|---|
| 75 | be paranoid for now) */ |
|---|
| 76 | if (last >= 640*1024 && addr < 1024*1024) { |
|---|
| 77 | *addrp = 1024*1024; |
|---|
| 78 | return 1; |
|---|
| 79 | } |
|---|
| 80 | if (last >= __pa_symbol(&_text) && last < __pa_symbol(&_end)) { |
|---|
| 81 | *addrp = __pa_symbol(&_end); |
|---|
| 82 | return 1; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | if (last >= ebda_addr && addr < ebda_addr + ebda_size) { |
|---|
| 86 | *addrp = ebda_addr + ebda_size; |
|---|
| 87 | return 1; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | /* XXX ramdisk image here? */ |
|---|
| 91 | #else |
|---|
| 92 | if (last < (table_end<<PAGE_SHIFT)) { |
|---|
| 93 | *addrp = table_end << PAGE_SHIFT; |
|---|
| 94 | return 1; |
|---|
| 95 | } |
|---|
| 96 | #endif |
|---|
| 97 | return 0; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | #ifndef CONFIG_XEN |
|---|
| 101 | /* |
|---|
| 102 | * This function checks if any part of the range <start,end> is mapped |
|---|
| 103 | * with type. |
|---|
| 104 | */ |
|---|
| 105 | int __meminit |
|---|
| 106 | e820_any_mapped(unsigned long start, unsigned long end, unsigned type) |
|---|
| 107 | { |
|---|
| 108 | int i; |
|---|
| 109 | for (i = 0; i < e820.nr_map; i++) { |
|---|
| 110 | struct e820entry *ei = &e820.map[i]; |
|---|
| 111 | if (type && ei->type != type) |
|---|
| 112 | continue; |
|---|
| 113 | if (ei->addr >= end || ei->addr + ei->size <= start) |
|---|
| 114 | continue; |
|---|
| 115 | return 1; |
|---|
| 116 | } |
|---|
| 117 | return 0; |
|---|
| 118 | } |
|---|
| 119 | #endif |
|---|
| 120 | |
|---|
| 121 | /* |
|---|
| 122 | * This function checks if the entire range <start,end> is mapped with type. |
|---|
| 123 | * |
|---|
| 124 | * Note: this function only works correct if the e820 table is sorted and |
|---|
| 125 | * not-overlapping, which is the case |
|---|
| 126 | */ |
|---|
| 127 | int __init e820_all_mapped(unsigned long start, unsigned long end, unsigned type) |
|---|
| 128 | { |
|---|
| 129 | int i; |
|---|
| 130 | |
|---|
| 131 | #ifndef CONFIG_XEN |
|---|
| 132 | for (i = 0; i < e820.nr_map; i++) { |
|---|
| 133 | struct e820entry *ei = &e820.map[i]; |
|---|
| 134 | #else |
|---|
| 135 | extern struct e820map machine_e820; |
|---|
| 136 | |
|---|
| 137 | if (!is_initial_xendomain()) |
|---|
| 138 | return 0; |
|---|
| 139 | for (i = 0; i < machine_e820.nr_map; i++) { |
|---|
| 140 | const struct e820entry *ei = &machine_e820.map[i]; |
|---|
| 141 | #endif |
|---|
| 142 | |
|---|
| 143 | if (type && ei->type != type) |
|---|
| 144 | continue; |
|---|
| 145 | /* is the region (part) in overlap with the current region ?*/ |
|---|
| 146 | if (ei->addr >= end || ei->addr + ei->size <= start) |
|---|
| 147 | continue; |
|---|
| 148 | |
|---|
| 149 | /* if the region is at the beginning of <start,end> we move |
|---|
| 150 | * start to the end of the region since it's ok until there |
|---|
| 151 | */ |
|---|
| 152 | if (ei->addr <= start) |
|---|
| 153 | start = ei->addr + ei->size; |
|---|
| 154 | /* if start is now at or beyond end, we're done, full coverage */ |
|---|
| 155 | if (start >= end) |
|---|
| 156 | return 1; /* we're done */ |
|---|
| 157 | } |
|---|
| 158 | return 0; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /* |
|---|
| 162 | * Find a free area in a specific range. |
|---|
| 163 | */ |
|---|
| 164 | unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size) |
|---|
| 165 | { |
|---|
| 166 | int i; |
|---|
| 167 | for (i = 0; i < e820.nr_map; i++) { |
|---|
| 168 | struct e820entry *ei = &e820.map[i]; |
|---|
| 169 | unsigned long addr = ei->addr, last; |
|---|
| 170 | if (ei->type != E820_RAM) |
|---|
| 171 | continue; |
|---|
| 172 | if (addr < start) |
|---|
| 173 | addr = start; |
|---|
| 174 | if (addr > ei->addr + ei->size) |
|---|
| 175 | continue; |
|---|
| 176 | while (bad_addr(&addr, size) && addr+size <= ei->addr+ei->size) |
|---|
| 177 | ; |
|---|
| 178 | last = addr + size; |
|---|
| 179 | if (last > ei->addr + ei->size) |
|---|
| 180 | continue; |
|---|
| 181 | if (last > end) |
|---|
| 182 | continue; |
|---|
| 183 | return addr; |
|---|
| 184 | } |
|---|
| 185 | return -1UL; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /* |
|---|
| 189 | * Free bootmem based on the e820 table for a node. |
|---|
| 190 | */ |
|---|
| 191 | void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end) |
|---|
| 192 | { |
|---|
| 193 | int i; |
|---|
| 194 | for (i = 0; i < e820.nr_map; i++) { |
|---|
| 195 | struct e820entry *ei = &e820.map[i]; |
|---|
| 196 | unsigned long last, addr; |
|---|
| 197 | |
|---|
| 198 | if (ei->type != E820_RAM || |
|---|
| 199 | ei->addr+ei->size <= start || |
|---|
| 200 | ei->addr >= end) |
|---|
| 201 | continue; |
|---|
| 202 | |
|---|
| 203 | addr = round_up(ei->addr, PAGE_SIZE); |
|---|
| 204 | if (addr < start) |
|---|
| 205 | addr = start; |
|---|
| 206 | |
|---|
| 207 | last = round_down(ei->addr + ei->size, PAGE_SIZE); |
|---|
| 208 | if (last >= end) |
|---|
| 209 | last = end; |
|---|
| 210 | |
|---|
| 211 | if (last > addr && last-addr >= PAGE_SIZE) |
|---|
| 212 | free_bootmem_node(pgdat, addr, last-addr); |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | /* |
|---|
| 217 | * Find the highest page frame number we have available |
|---|
| 218 | */ |
|---|
| 219 | unsigned long __init e820_end_of_ram(void) |
|---|
| 220 | { |
|---|
| 221 | int i; |
|---|
| 222 | unsigned long end_pfn = 0; |
|---|
| 223 | |
|---|
| 224 | for (i = 0; i < e820.nr_map; i++) { |
|---|
| 225 | struct e820entry *ei = &e820.map[i]; |
|---|
| 226 | unsigned long start, end; |
|---|
| 227 | |
|---|
| 228 | start = round_up(ei->addr, PAGE_SIZE); |
|---|
| 229 | end = round_down(ei->addr + ei->size, PAGE_SIZE); |
|---|
| 230 | if (start >= end) |
|---|
| 231 | continue; |
|---|
| 232 | if (ei->type == E820_RAM) { |
|---|
| 233 | if (end > end_pfn<<PAGE_SHIFT) |
|---|
| 234 | end_pfn = end>>PAGE_SHIFT; |
|---|
| 235 | } else { |
|---|
| 236 | if (end > end_pfn_map<<PAGE_SHIFT) |
|---|
| 237 | end_pfn_map = end>>PAGE_SHIFT; |
|---|
| 238 | } |
|---|
| 239 | } |
|---|
| 240 | |
|---|
| 241 | if (end_pfn > end_pfn_map) |
|---|
| 242 | end_pfn_map = end_pfn; |
|---|
| 243 | if (end_pfn_map > MAXMEM>>PAGE_SHIFT) |
|---|
| 244 | end_pfn_map = MAXMEM>>PAGE_SHIFT; |
|---|
| 245 | if (end_pfn > end_user_pfn) |
|---|
| 246 | end_pfn = end_user_pfn; |
|---|
| 247 | if (end_pfn > end_pfn_map) |
|---|
| 248 | end_pfn = end_pfn_map; |
|---|
| 249 | |
|---|
| 250 | return end_pfn; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | /* |
|---|
| 254 | * Compute how much memory is missing in a range. |
|---|
| 255 | * Unlike the other functions in this file the arguments are in page numbers. |
|---|
| 256 | */ |
|---|
| 257 | unsigned long __init |
|---|
| 258 | e820_hole_size(unsigned long start_pfn, unsigned long end_pfn) |
|---|
| 259 | { |
|---|
| 260 | unsigned long ram = 0; |
|---|
| 261 | unsigned long start = start_pfn << PAGE_SHIFT; |
|---|
| 262 | unsigned long end = end_pfn << PAGE_SHIFT; |
|---|
| 263 | int i; |
|---|
| 264 | for (i = 0; i < e820.nr_map; i++) { |
|---|
| 265 | struct e820entry *ei = &e820.map[i]; |
|---|
| 266 | unsigned long last, addr; |
|---|
| 267 | |
|---|
| 268 | if (ei->type != E820_RAM || |
|---|
| 269 | ei->addr+ei->size <= start || |
|---|
| 270 | ei->addr >= end) |
|---|
| 271 | continue; |
|---|
| 272 | |
|---|
| 273 | addr = round_up(ei->addr, PAGE_SIZE); |
|---|
| 274 | if (addr < start) |
|---|
| 275 | addr = start; |
|---|
| 276 | |
|---|
| 277 | last = round_down(ei->addr + ei->size, PAGE_SIZE); |
|---|
| 278 | if (last >= end) |
|---|
| 279 | last = end; |
|---|
| 280 | |
|---|
| 281 | if (last > addr) |
|---|
| 282 | ram += last - addr; |
|---|
| 283 | } |
|---|
| 284 | return ((end - start) - ram) >> PAGE_SHIFT; |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | /* |
|---|
| 288 | * Mark e820 reserved areas as busy for the resource manager. |
|---|
| 289 | */ |
|---|
| 290 | void __init e820_reserve_resources(struct e820entry *e820, int nr_map) |
|---|
| 291 | { |
|---|
| 292 | int i; |
|---|
| 293 | for (i = 0; i < nr_map; i++) { |
|---|
| 294 | struct resource *res; |
|---|
| 295 | res = alloc_bootmem_low(sizeof(struct resource)); |
|---|
| 296 | switch (e820[i].type) { |
|---|
| 297 | case E820_RAM: res->name = "System RAM"; break; |
|---|
| 298 | case E820_ACPI: res->name = "ACPI Tables"; break; |
|---|
| 299 | case E820_NVS: res->name = "ACPI Non-volatile Storage"; break; |
|---|
| 300 | default: res->name = "reserved"; |
|---|
| 301 | } |
|---|
| 302 | res->start = e820[i].addr; |
|---|
| 303 | res->end = res->start + e820[i].size - 1; |
|---|
| 304 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
|---|
| 305 | request_resource(&iomem_resource, res); |
|---|
| 306 | if (e820[i].type == E820_RAM) { |
|---|
| 307 | /* |
|---|
| 308 | * We don't know which RAM region contains kernel data, |
|---|
| 309 | * so we try it repeatedly and let the resource manager |
|---|
| 310 | * test it. |
|---|
| 311 | */ |
|---|
| 312 | #ifndef CONFIG_XEN |
|---|
| 313 | request_resource(res, &code_resource); |
|---|
| 314 | request_resource(res, &data_resource); |
|---|
| 315 | #endif |
|---|
| 316 | #ifdef CONFIG_KEXEC |
|---|
| 317 | if (crashk_res.start != crashk_res.end) |
|---|
| 318 | request_resource(res, &crashk_res); |
|---|
| 319 | #ifdef CONFIG_XEN |
|---|
| 320 | xen_machine_kexec_register_resources(res); |
|---|
| 321 | #endif |
|---|
| 322 | #endif |
|---|
| 323 | } |
|---|
| 324 | } |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | /* |
|---|
| 328 | * Add a memory region to the kernel e820 map. |
|---|
| 329 | */ |
|---|
| 330 | void __init add_memory_region(unsigned long start, unsigned long size, int type) |
|---|
| 331 | { |
|---|
| 332 | int x = e820.nr_map; |
|---|
| 333 | |
|---|
| 334 | if (x == E820MAX) { |
|---|
| 335 | printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); |
|---|
| 336 | return; |
|---|
| 337 | } |
|---|
| 338 | |
|---|
| 339 | e820.map[x].addr = start; |
|---|
| 340 | e820.map[x].size = size; |
|---|
| 341 | e820.map[x].type = type; |
|---|
| 342 | e820.nr_map++; |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | void __init e820_print_map(char *who) |
|---|
| 346 | { |
|---|
| 347 | int i; |
|---|
| 348 | |
|---|
| 349 | for (i = 0; i < e820.nr_map; i++) { |
|---|
| 350 | printk(" %s: %016Lx - %016Lx ", who, |
|---|
| 351 | (unsigned long long) e820.map[i].addr, |
|---|
| 352 | (unsigned long long) (e820.map[i].addr + e820.map[i].size)); |
|---|
| 353 | switch (e820.map[i].type) { |
|---|
| 354 | case E820_RAM: printk("(usable)\n"); |
|---|
| 355 | break; |
|---|
| 356 | case E820_RESERVED: |
|---|
| 357 | printk("(reserved)\n"); |
|---|
| 358 | break; |
|---|
| 359 | case E820_ACPI: |
|---|
| 360 | printk("(ACPI data)\n"); |
|---|
| 361 | break; |
|---|
| 362 | case E820_NVS: |
|---|
| 363 | printk("(ACPI NVS)\n"); |
|---|
| 364 | break; |
|---|
| 365 | default: printk("type %u\n", e820.map[i].type); |
|---|
| 366 | break; |
|---|
| 367 | } |
|---|
| 368 | } |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | /* |
|---|
| 372 | * Sanitize the BIOS e820 map. |
|---|
| 373 | * |
|---|
| 374 | * Some e820 responses include overlapping entries. The following |
|---|
| 375 | * replaces the original e820 map with a new one, removing overlaps. |
|---|
| 376 | * |
|---|
| 377 | */ |
|---|
| 378 | static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map) |
|---|
| 379 | { |
|---|
| 380 | struct change_member { |
|---|
| 381 | struct e820entry *pbios; /* pointer to original bios entry */ |
|---|
| 382 | unsigned long long addr; /* address for this change point */ |
|---|
| 383 | }; |
|---|
| 384 | static struct change_member change_point_list[2*E820MAX] __initdata; |
|---|
| 385 | static struct change_member *change_point[2*E820MAX] __initdata; |
|---|
| 386 | static struct e820entry *overlap_list[E820MAX] __initdata; |
|---|
| 387 | static struct e820entry new_bios[E820MAX] __initdata; |
|---|
| 388 | struct change_member *change_tmp; |
|---|
| 389 | unsigned long current_type, last_type; |
|---|
| 390 | unsigned long long last_addr; |
|---|
| 391 | int chgidx, still_changing; |
|---|
| 392 | int overlap_entries; |
|---|
| 393 | int new_bios_entry; |
|---|
| 394 | int old_nr, new_nr, chg_nr; |
|---|
| 395 | int i; |
|---|
| 396 | |
|---|
| 397 | /* |
|---|
| 398 | Visually we're performing the following (1,2,3,4 = memory types)... |
|---|
| 399 | |
|---|
| 400 | Sample memory map (w/overlaps): |
|---|
| 401 | ____22__________________ |
|---|
| 402 | ______________________4_ |
|---|
| 403 | ____1111________________ |
|---|
| 404 | _44_____________________ |
|---|
| 405 | 11111111________________ |
|---|
| 406 | ____________________33__ |
|---|
| 407 | ___________44___________ |
|---|
| 408 | __________33333_________ |
|---|
| 409 | ______________22________ |
|---|
| 410 | ___________________2222_ |
|---|
| 411 | _________111111111______ |
|---|
| 412 | _____________________11_ |
|---|
| 413 | _________________4______ |
|---|
| 414 | |
|---|
| 415 | Sanitized equivalent (no overlap): |
|---|
| 416 | 1_______________________ |
|---|
| 417 | _44_____________________ |
|---|
| 418 | ___1____________________ |
|---|
| 419 | ____22__________________ |
|---|
| 420 | ______11________________ |
|---|
| 421 | _________1______________ |
|---|
| 422 | __________3_____________ |
|---|
| 423 | ___________44___________ |
|---|
| 424 | _____________33_________ |
|---|
| 425 | _______________2________ |
|---|
| 426 | ________________1_______ |
|---|
| 427 | _________________4______ |
|---|
| 428 | ___________________2____ |
|---|
| 429 | ____________________33__ |
|---|
| 430 | ______________________4_ |
|---|
| 431 | */ |
|---|
| 432 | |
|---|
| 433 | /* if there's only one memory region, don't bother */ |
|---|
| 434 | if (*pnr_map < 2) |
|---|
| 435 | return -1; |
|---|
| 436 | |
|---|
| 437 | old_nr = *pnr_map; |
|---|
| 438 | |
|---|
| 439 | /* bail out if we find any unreasonable addresses in bios map */ |
|---|
| 440 | for (i=0; i<old_nr; i++) |
|---|
| 441 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) |
|---|
| 442 | return -1; |
|---|
| 443 | |
|---|
| 444 | /* create pointers for initial change-point information (for sorting) */ |
|---|
| 445 | for (i=0; i < 2*old_nr; i++) |
|---|
| 446 | change_point[i] = &change_point_list[i]; |
|---|
| 447 | |
|---|
| 448 | /* record all known change-points (starting and ending addresses), |
|---|
| 449 | omitting those that are for empty memory regions */ |
|---|
| 450 | chgidx = 0; |
|---|
| 451 | for (i=0; i < old_nr; i++) { |
|---|
| 452 | if (biosmap[i].size != 0) { |
|---|
| 453 | change_point[chgidx]->addr = biosmap[i].addr; |
|---|
| 454 | change_point[chgidx++]->pbios = &biosmap[i]; |
|---|
| 455 | change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size; |
|---|
| 456 | change_point[chgidx++]->pbios = &biosmap[i]; |
|---|
| 457 | } |
|---|
| 458 | } |
|---|
| 459 | chg_nr = chgidx; |
|---|
| 460 | |
|---|
| 461 | /* sort change-point list by memory addresses (low -> high) */ |
|---|
| 462 | still_changing = 1; |
|---|
| 463 | while (still_changing) { |
|---|
| 464 | still_changing = 0; |
|---|
| 465 | for (i=1; i < chg_nr; i++) { |
|---|
| 466 | /* if <current_addr> > <last_addr>, swap */ |
|---|
| 467 | /* or, if current=<start_addr> & last=<end_addr>, swap */ |
|---|
| 468 | if ((change_point[i]->addr < change_point[i-1]->addr) || |
|---|
| 469 | ((change_point[i]->addr == change_point[i-1]->addr) && |
|---|
| 470 | (change_point[i]->addr == change_point[i]->pbios->addr) && |
|---|
| 471 | (change_point[i-1]->addr != change_point[i-1]->pbios->addr)) |
|---|
| 472 | ) |
|---|
| 473 | { |
|---|
| 474 | change_tmp = change_point[i]; |
|---|
| 475 | change_point[i] = change_point[i-1]; |
|---|
| 476 | change_point[i-1] = change_tmp; |
|---|
| 477 | still_changing=1; |
|---|
| 478 | } |
|---|
| 479 | } |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | /* create a new bios memory map, removing overlaps */ |
|---|
| 483 | overlap_entries=0; /* number of entries in the overlap table */ |
|---|
| 484 | new_bios_entry=0; /* index for creating new bios map entries */ |
|---|
| 485 | last_type = 0; /* start with undefined memory type */ |
|---|
| 486 | last_addr = 0; /* start with 0 as last starting address */ |
|---|
| 487 | /* loop through change-points, determining affect on the new bios map */ |
|---|
| 488 | for (chgidx=0; chgidx < chg_nr; chgidx++) |
|---|
| 489 | { |
|---|
| 490 | /* keep track of all overlapping bios entries */ |
|---|
| 491 | if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr) |
|---|
| 492 | { |
|---|
| 493 | /* add map entry to overlap list (> 1 entry implies an overlap) */ |
|---|
| 494 | overlap_list[overlap_entries++]=change_point[chgidx]->pbios; |
|---|
| 495 | } |
|---|
| 496 | else |
|---|
| 497 | { |
|---|
| 498 | /* remove entry from list (order independent, so swap with last) */ |
|---|
| 499 | for (i=0; i<overlap_entries; i++) |
|---|
| 500 | { |
|---|
| 501 | if (overlap_list[i] == change_point[chgidx]->pbios) |
|---|
| 502 | overlap_list[i] = overlap_list[overlap_entries-1]; |
|---|
| 503 | } |
|---|
| 504 | overlap_entries--; |
|---|
| 505 | } |
|---|
| 506 | /* if there are overlapping entries, decide which "type" to use */ |
|---|
| 507 | /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */ |
|---|
| 508 | current_type = 0; |
|---|
| 509 | for (i=0; i<overlap_entries; i++) |
|---|
| 510 | if (overlap_list[i]->type > current_type) |
|---|
| 511 | current_type = overlap_list[i]->type; |
|---|
| 512 | /* continue building up new bios map based on this information */ |
|---|
| 513 | if (current_type != last_type) { |
|---|
| 514 | if (last_type != 0) { |
|---|
| 515 | new_bios[new_bios_entry].size = |
|---|
| 516 | change_point[chgidx]->addr - last_addr; |
|---|
| 517 | /* move forward only if the new size was non-zero */ |
|---|
| 518 | if (new_bios[new_bios_entry].size != 0) |
|---|
| 519 | if (++new_bios_entry >= E820MAX) |
|---|
| 520 | break; /* no more space left for new bios entries */ |
|---|
| 521 | } |
|---|
| 522 | if (current_type != 0) { |
|---|
| 523 | new_bios[new_bios_entry].addr = change_point[chgidx]->addr; |
|---|
| 524 | new_bios[new_bios_entry].type = current_type; |
|---|
| 525 | last_addr=change_point[chgidx]->addr; |
|---|
| 526 | } |
|---|
| 527 | last_type = current_type; |
|---|
| 528 | } |
|---|
| 529 | } |
|---|
| 530 | new_nr = new_bios_entry; /* retain count for new bios entries */ |
|---|
| 531 | |
|---|
| 532 | /* copy new bios mapping into original location */ |
|---|
| 533 | memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry)); |
|---|
| 534 | *pnr_map = new_nr; |
|---|
| 535 | |
|---|
| 536 | return 0; |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | /* |
|---|
| 540 | * Copy the BIOS e820 map into a safe place. |
|---|
| 541 | * |
|---|
| 542 | * Sanity-check it while we're at it.. |
|---|
| 543 | * |
|---|
| 544 | * If we're lucky and live on a modern system, the setup code |
|---|
| 545 | * will have given us a memory map that we can use to properly |
|---|
| 546 | * set up memory. If we aren't, we'll fake a memory map. |
|---|
| 547 | * |
|---|
| 548 | * We check to see that the memory map contains at least 2 elements |
|---|
| 549 | * before we'll use it, because the detection code in setup.S may |
|---|
| 550 | * not be perfect and most every PC known to man has two memory |
|---|
| 551 | * regions: one from 0 to 640k, and one from 1mb up. (The IBM |
|---|
| 552 | * thinkpad 560x, for example, does not cooperate with the memory |
|---|
| 553 | * detection code.) |
|---|
| 554 | */ |
|---|
| 555 | static int __init copy_e820_map(struct e820entry * biosmap, int nr_map) |
|---|
| 556 | { |
|---|
| 557 | #ifndef CONFIG_XEN |
|---|
| 558 | /* Only one memory region (or negative)? Ignore it */ |
|---|
| 559 | if (nr_map < 2) |
|---|
| 560 | return -1; |
|---|
| 561 | #else |
|---|
| 562 | BUG_ON(nr_map < 1); |
|---|
| 563 | #endif |
|---|
| 564 | |
|---|
| 565 | do { |
|---|
| 566 | unsigned long start = biosmap->addr; |
|---|
| 567 | unsigned long size = biosmap->size; |
|---|
| 568 | unsigned long end = start + size; |
|---|
| 569 | unsigned long type = biosmap->type; |
|---|
| 570 | |
|---|
| 571 | /* Overflow in 64 bits? Ignore the memory map. */ |
|---|
| 572 | if (start > end) |
|---|
| 573 | return -1; |
|---|
| 574 | |
|---|
| 575 | #ifndef CONFIG_XEN |
|---|
| 576 | /* |
|---|
| 577 | * Some BIOSes claim RAM in the 640k - 1M region. |
|---|
| 578 | * Not right. Fix it up. |
|---|
| 579 | * |
|---|
| 580 | * This should be removed on Hammer which is supposed to not |
|---|
| 581 | * have non e820 covered ISA mappings there, but I had some strange |
|---|
| 582 | * problems so it stays for now. -AK |
|---|
| 583 | */ |
|---|
| 584 | if (type == E820_RAM) { |
|---|
| 585 | if (start < 0x100000ULL && end > 0xA0000ULL) { |
|---|
| 586 | if (start < 0xA0000ULL) |
|---|
| 587 | add_memory_region(start, 0xA0000ULL-start, type); |
|---|
| 588 | if (end <= 0x100000ULL) |
|---|
| 589 | continue; |
|---|
| 590 | start = 0x100000ULL; |
|---|
| 591 | size = end - start; |
|---|
| 592 | } |
|---|
| 593 | } |
|---|
| 594 | #endif |
|---|
| 595 | |
|---|
| 596 | add_memory_region(start, size, type); |
|---|
| 597 | } while (biosmap++,--nr_map); |
|---|
| 598 | return 0; |
|---|
| 599 | } |
|---|
| 600 | |
|---|
| 601 | #ifndef CONFIG_XEN |
|---|
| 602 | void __init setup_memory_region(void) |
|---|
| 603 | { |
|---|
| 604 | char *who = "BIOS-e820"; |
|---|
| 605 | |
|---|
| 606 | /* |
|---|
| 607 | * Try to copy the BIOS-supplied E820-map. |
|---|
| 608 | * |
|---|
| 609 | * Otherwise fake a memory map; one section from 0k->640k, |
|---|
| 610 | * the next section from 1mb->appropriate_mem_k |
|---|
| 611 | */ |
|---|
| 612 | sanitize_e820_map(E820_MAP, &E820_MAP_NR); |
|---|
| 613 | if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) { |
|---|
| 614 | unsigned long mem_size; |
|---|
| 615 | |
|---|
| 616 | /* compare results from other methods and take the greater */ |
|---|
| 617 | if (ALT_MEM_K < EXT_MEM_K) { |
|---|
| 618 | mem_size = EXT_MEM_K; |
|---|
| 619 | who = "BIOS-88"; |
|---|
| 620 | } else { |
|---|
| 621 | mem_size = ALT_MEM_K; |
|---|
| 622 | who = "BIOS-e801"; |
|---|
| 623 | } |
|---|
| 624 | |
|---|
| 625 | e820.nr_map = 0; |
|---|
| 626 | add_memory_region(0, LOWMEMSIZE(), E820_RAM); |
|---|
| 627 | add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM); |
|---|
| 628 | } |
|---|
| 629 | printk(KERN_INFO "BIOS-provided physical RAM map:\n"); |
|---|
| 630 | e820_print_map(who); |
|---|
| 631 | } |
|---|
| 632 | |
|---|
| 633 | #else /* CONFIG_XEN */ |
|---|
| 634 | |
|---|
| 635 | void __init setup_memory_region(void) |
|---|
| 636 | { |
|---|
| 637 | int rc; |
|---|
| 638 | struct xen_memory_map memmap; |
|---|
| 639 | /* |
|---|
| 640 | * This is rather large for a stack variable but this early in |
|---|
| 641 | * the boot process we know we have plenty slack space. |
|---|
| 642 | */ |
|---|
| 643 | struct e820entry map[E820MAX]; |
|---|
| 644 | |
|---|
| 645 | memmap.nr_entries = E820MAX; |
|---|
| 646 | set_xen_guest_handle(memmap.buffer, map); |
|---|
| 647 | |
|---|
| 648 | rc = HYPERVISOR_memory_op(XENMEM_memory_map, &memmap); |
|---|
| 649 | if ( rc == -ENOSYS ) { |
|---|
| 650 | memmap.nr_entries = 1; |
|---|
| 651 | map[0].addr = 0ULL; |
|---|
| 652 | map[0].size = xen_start_info->nr_pages << PAGE_SHIFT; |
|---|
| 653 | /* 8MB slack (to balance backend allocations). */ |
|---|
| 654 | map[0].size += 8 << 20; |
|---|
| 655 | map[0].type = E820_RAM; |
|---|
| 656 | rc = 0; |
|---|
| 657 | } |
|---|
| 658 | BUG_ON(rc); |
|---|
| 659 | |
|---|
| 660 | sanitize_e820_map(map, (char *)&memmap.nr_entries); |
|---|
| 661 | |
|---|
| 662 | BUG_ON(copy_e820_map(map, (char)memmap.nr_entries) < 0); |
|---|
| 663 | |
|---|
| 664 | printk(KERN_INFO "BIOS-provided physical RAM map:\n"); |
|---|
| 665 | e820_print_map("Xen"); |
|---|
| 666 | } |
|---|
| 667 | #endif |
|---|
| 668 | |
|---|
| 669 | void __init parse_memopt(char *p, char **from) |
|---|
| 670 | { |
|---|
| 671 | int i; |
|---|
| 672 | unsigned long current_end; |
|---|
| 673 | unsigned long end; |
|---|
| 674 | |
|---|
| 675 | end_user_pfn = memparse(p, from); |
|---|
| 676 | end_user_pfn >>= PAGE_SHIFT; |
|---|
| 677 | |
|---|
| 678 | end = end_user_pfn<<PAGE_SHIFT; |
|---|
| 679 | i = e820.nr_map-1; |
|---|
| 680 | current_end = e820.map[i].addr + e820.map[i].size; |
|---|
| 681 | |
|---|
| 682 | if (current_end < end) { |
|---|
| 683 | /* |
|---|
| 684 | * The e820 map ends before our requested size so |
|---|
| 685 | * extend the final entry to the requested address. |
|---|
| 686 | */ |
|---|
| 687 | if (e820.map[i].type == E820_RAM) |
|---|
| 688 | e820.map[i].size = end - e820.map[i].addr; |
|---|
| 689 | else |
|---|
| 690 | add_memory_region(current_end, end - current_end, E820_RAM); |
|---|
| 691 | } |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | void __init parse_memmapopt(char *p, char **from) |
|---|
| 695 | { |
|---|
| 696 | unsigned long long start_at, mem_size; |
|---|
| 697 | |
|---|
| 698 | mem_size = memparse(p, from); |
|---|
| 699 | p = *from; |
|---|
| 700 | if (*p == '@') { |
|---|
| 701 | start_at = memparse(p+1, from); |
|---|
| 702 | add_memory_region(start_at, mem_size, E820_RAM); |
|---|
| 703 | } else if (*p == '#') { |
|---|
| 704 | start_at = memparse(p+1, from); |
|---|
| 705 | add_memory_region(start_at, mem_size, E820_ACPI); |
|---|
| 706 | } else if (*p == '$') { |
|---|
| 707 | start_at = memparse(p+1, from); |
|---|
| 708 | add_memory_region(start_at, mem_size, E820_RESERVED); |
|---|
| 709 | } else { |
|---|
| 710 | end_user_pfn = (mem_size >> PAGE_SHIFT); |
|---|
| 711 | } |
|---|
| 712 | p = *from; |
|---|
| 713 | } |
|---|
| 714 | |
|---|
| 715 | unsigned long pci_mem_start = 0xaeedbabe; |
|---|
| 716 | EXPORT_SYMBOL(pci_mem_start); |
|---|
| 717 | |
|---|
| 718 | /* |
|---|
| 719 | * Search for the biggest gap in the low 32 bits of the e820 |
|---|
| 720 | * memory space. We pass this space to PCI to assign MMIO resources |
|---|
| 721 | * for hotplug or unconfigured devices in. |
|---|
| 722 | * Hopefully the BIOS let enough space left. |
|---|
| 723 | */ |
|---|
| 724 | __init void e820_setup_gap(struct e820entry *e820, int nr_map) |
|---|
| 725 | { |
|---|
| 726 | unsigned long gapstart, gapsize, round; |
|---|
| 727 | unsigned long last; |
|---|
| 728 | int i; |
|---|
| 729 | int found = 0; |
|---|
| 730 | |
|---|
| 731 | last = 0x100000000ull; |
|---|
| 732 | gapstart = 0x10000000; |
|---|
| 733 | gapsize = 0x400000; |
|---|
| 734 | i = nr_map; |
|---|
| 735 | while (--i >= 0) { |
|---|
| 736 | unsigned long long start = e820[i].addr; |
|---|
| 737 | unsigned long long end = start + e820[i].size; |
|---|
| 738 | |
|---|
| 739 | /* |
|---|
| 740 | * Since "last" is at most 4GB, we know we'll |
|---|
| 741 | * fit in 32 bits if this condition is true |
|---|
| 742 | */ |
|---|
| 743 | if (last > end) { |
|---|
| 744 | unsigned long gap = last - end; |
|---|
| 745 | |
|---|
| 746 | if (gap > gapsize) { |
|---|
| 747 | gapsize = gap; |
|---|
| 748 | gapstart = end; |
|---|
| 749 | found = 1; |
|---|
| 750 | } |
|---|
| 751 | } |
|---|
| 752 | if (start < last) |
|---|
| 753 | last = start; |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | if (!found) { |
|---|
| 757 | gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024; |
|---|
| 758 | printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n" |
|---|
| 759 | KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n"); |
|---|
| 760 | } |
|---|
| 761 | |
|---|
| 762 | /* |
|---|
| 763 | * See how much we want to round up: start off with |
|---|
| 764 | * rounding to the next 1MB area. |
|---|
| 765 | */ |
|---|
| 766 | round = 0x100000; |
|---|
| 767 | while ((gapsize >> 4) > round) |
|---|
| 768 | round += round; |
|---|
| 769 | /* Fun with two's complement */ |
|---|
| 770 | pci_mem_start = (gapstart + round) & -round; |
|---|
| 771 | |
|---|
| 772 | printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n", |
|---|
| 773 | pci_mem_start, gapstart, gapsize); |
|---|
| 774 | } |
|---|