bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/types.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/bootmem.h> |
| 5 | #include <linux/ioport.h> |
| 6 | #include <linux/string.h> |
| 7 | #include <linux/kexec.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/mm.h> |
bibo,mao | b2dff6a | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 10 | #include <linux/pfn.h> |
Rusty Russell | bd472c7 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 11 | #include <linux/uaccess.h> |
Rafael J. Wysocki | 1c10070 | 2007-07-21 17:11:09 +0200 | [diff] [blame] | 12 | #include <linux/suspend.h> |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 13 | |
| 14 | #include <asm/pgtable.h> |
| 15 | #include <asm/page.h> |
| 16 | #include <asm/e820.h> |
Adrian Bunk | 90611fe | 2007-02-13 13:26:21 +0100 | [diff] [blame] | 17 | #include <asm/setup.h> |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 18 | |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 19 | struct e820map e820; |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 20 | struct change_member { |
| 21 | struct e820entry *pbios; /* pointer to original bios entry */ |
| 22 | unsigned long long addr; /* address for this change point */ |
| 23 | }; |
| 24 | static struct change_member change_point_list[2*E820MAX] __initdata; |
| 25 | static struct change_member *change_point[2*E820MAX] __initdata; |
| 26 | static struct e820entry *overlap_list[E820MAX] __initdata; |
| 27 | static struct e820entry new_bios[E820MAX] __initdata; |
bibo,mao | b5b2405 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 28 | /* For PCI or other memory-mapped resources */ |
| 29 | unsigned long pci_mem_start = 0x10000000; |
| 30 | #ifdef CONFIG_PCI |
| 31 | EXPORT_SYMBOL(pci_mem_start); |
| 32 | #endif |
bibo,mao | cef518e | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 33 | extern int user_defined_memmap; |
Bernhard Walle | 00bf409 | 2007-10-21 16:42:01 -0700 | [diff] [blame] | 34 | |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 35 | static struct resource system_rom_resource = { |
| 36 | .name = "System ROM", |
| 37 | .start = 0xf0000, |
| 38 | .end = 0xfffff, |
| 39 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 40 | }; |
| 41 | |
| 42 | static struct resource extension_rom_resource = { |
| 43 | .name = "Extension ROM", |
| 44 | .start = 0xe0000, |
| 45 | .end = 0xeffff, |
| 46 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 47 | }; |
| 48 | |
| 49 | static struct resource adapter_rom_resources[] = { { |
| 50 | .name = "Adapter ROM", |
| 51 | .start = 0xc8000, |
| 52 | .end = 0, |
| 53 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 54 | }, { |
| 55 | .name = "Adapter ROM", |
| 56 | .start = 0, |
| 57 | .end = 0, |
| 58 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 59 | }, { |
| 60 | .name = "Adapter ROM", |
| 61 | .start = 0, |
| 62 | .end = 0, |
| 63 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 64 | }, { |
| 65 | .name = "Adapter ROM", |
| 66 | .start = 0, |
| 67 | .end = 0, |
| 68 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 69 | }, { |
| 70 | .name = "Adapter ROM", |
| 71 | .start = 0, |
| 72 | .end = 0, |
| 73 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 74 | }, { |
| 75 | .name = "Adapter ROM", |
| 76 | .start = 0, |
| 77 | .end = 0, |
| 78 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 79 | } }; |
| 80 | |
| 81 | static struct resource video_rom_resource = { |
| 82 | .name = "Video ROM", |
| 83 | .start = 0xc0000, |
| 84 | .end = 0xc7fff, |
| 85 | .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM |
| 86 | }; |
| 87 | |
Rene Herman | 3b3d5e1 | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 88 | #define ROMSIGNATURE 0xaa55 |
| 89 | |
| 90 | static int __init romsignature(const unsigned char *rom) |
Rusty Russell | bd472c7 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 91 | { |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 92 | const unsigned short * const ptr = (const unsigned short *)rom; |
Rusty Russell | bd472c7 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 93 | unsigned short sig; |
Rene Herman | 3b3d5e1 | 2007-02-13 13:26:22 +0100 | [diff] [blame] | 94 | |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 95 | return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE; |
Rusty Russell | bd472c7 | 2006-12-07 02:14:08 +0100 | [diff] [blame] | 96 | } |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 97 | |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 98 | static int __init romchecksum(const unsigned char *rom, unsigned long length) |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 99 | { |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 100 | unsigned char sum, c; |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 101 | |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 102 | for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--) |
| 103 | sum += c; |
| 104 | return !length && !sum; |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void __init probe_roms(void) |
| 108 | { |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 109 | const unsigned char *rom; |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 110 | unsigned long start, length, upper; |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 111 | unsigned char c; |
| 112 | int i; |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 113 | |
| 114 | /* video rom */ |
| 115 | upper = adapter_rom_resources[0].start; |
| 116 | for (start = video_rom_resource.start; start < upper; start += 2048) { |
| 117 | rom = isa_bus_to_virt(start); |
| 118 | if (!romsignature(rom)) |
| 119 | continue; |
| 120 | |
| 121 | video_rom_resource.start = start; |
| 122 | |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 123 | if (probe_kernel_address(rom + 2, c) != 0) |
| 124 | continue; |
| 125 | |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 126 | /* 0 < length <= 0x7f * 512, historically */ |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 127 | length = c * 512; |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 128 | |
| 129 | /* if checksum okay, trust length byte */ |
| 130 | if (length && romchecksum(rom, length)) |
| 131 | video_rom_resource.end = start + length - 1; |
| 132 | |
| 133 | request_resource(&iomem_resource, &video_rom_resource); |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | start = (video_rom_resource.end + 1 + 2047) & ~2047UL; |
| 138 | if (start < upper) |
| 139 | start = upper; |
| 140 | |
| 141 | /* system rom */ |
| 142 | request_resource(&iomem_resource, &system_rom_resource); |
| 143 | upper = system_rom_resource.start; |
| 144 | |
| 145 | /* check for extension rom (ignore length byte!) */ |
| 146 | rom = isa_bus_to_virt(extension_rom_resource.start); |
| 147 | if (romsignature(rom)) { |
| 148 | length = extension_rom_resource.end - extension_rom_resource.start + 1; |
| 149 | if (romchecksum(rom, length)) { |
| 150 | request_resource(&iomem_resource, &extension_rom_resource); |
| 151 | upper = extension_rom_resource.start; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* check for adapter roms on 2k boundaries */ |
| 156 | for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) { |
| 157 | rom = isa_bus_to_virt(start); |
| 158 | if (!romsignature(rom)) |
| 159 | continue; |
| 160 | |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 161 | if (probe_kernel_address(rom + 2, c) != 0) |
| 162 | continue; |
| 163 | |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 164 | /* 0 < length <= 0x7f * 512, historically */ |
Rene Herman | 0adad17 | 2007-05-02 19:27:05 +0200 | [diff] [blame] | 165 | length = c * 512; |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 166 | |
| 167 | /* but accept any length that fits if checksum okay */ |
| 168 | if (!length || start + length > upper || !romchecksum(rom, length)) |
| 169 | continue; |
| 170 | |
| 171 | adapter_rom_resources[i].start = start; |
| 172 | adapter_rom_resources[i].end = start + length - 1; |
| 173 | request_resource(&iomem_resource, &adapter_rom_resources[i]); |
| 174 | |
| 175 | start = adapter_rom_resources[i++].end & ~2047UL; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Request address space for all standard RAM and ROM resources |
| 181 | * and also for regions reported as reserved by the e820. |
| 182 | */ |
Huang, Ying | 2215e69 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 183 | void __init init_iomem_resources(struct resource *code_resource, |
Bernhard Walle | c9cce83 | 2008-01-30 13:30:32 +0100 | [diff] [blame] | 184 | struct resource *data_resource, |
| 185 | struct resource *bss_resource) |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 186 | { |
| 187 | int i; |
| 188 | |
| 189 | probe_roms(); |
| 190 | for (i = 0; i < e820.nr_map; i++) { |
| 191 | struct resource *res; |
| 192 | #ifndef CONFIG_RESOURCES_64BIT |
| 193 | if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL) |
| 194 | continue; |
| 195 | #endif |
| 196 | res = kzalloc(sizeof(struct resource), GFP_ATOMIC); |
| 197 | switch (e820.map[i].type) { |
| 198 | case E820_RAM: res->name = "System RAM"; break; |
| 199 | case E820_ACPI: res->name = "ACPI Tables"; break; |
| 200 | case E820_NVS: res->name = "ACPI Non-volatile Storage"; break; |
| 201 | default: res->name = "reserved"; |
| 202 | } |
| 203 | res->start = e820.map[i].addr; |
| 204 | res->end = res->start + e820.map[i].size - 1; |
| 205 | res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; |
| 206 | if (request_resource(&iomem_resource, res)) { |
| 207 | kfree(res); |
| 208 | continue; |
| 209 | } |
| 210 | if (e820.map[i].type == E820_RAM) { |
| 211 | /* |
| 212 | * We don't know which RAM region contains kernel data, |
| 213 | * so we try it repeatedly and let the resource manager |
| 214 | * test it. |
| 215 | */ |
| 216 | request_resource(res, code_resource); |
| 217 | request_resource(res, data_resource); |
Bernhard Walle | 00bf409 | 2007-10-21 16:42:01 -0700 | [diff] [blame] | 218 | request_resource(res, bss_resource); |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 219 | #ifdef CONFIG_KEXEC |
Bernhard Walle | d62cc47 | 2007-10-18 23:40:59 -0700 | [diff] [blame] | 220 | if (crashk_res.start != crashk_res.end) |
| 221 | request_resource(res, &crashk_res); |
bibo,mao | 269c2d8 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 222 | #endif |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 227 | #if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION) |
Rafael J. Wysocki | 1c10070 | 2007-07-21 17:11:09 +0200 | [diff] [blame] | 228 | /** |
| 229 | * e820_mark_nosave_regions - Find the ranges of physical addresses that do not |
| 230 | * correspond to e820 RAM areas and mark the corresponding pages as nosave for |
| 231 | * hibernation. |
| 232 | * |
| 233 | * This function requires the e820 map to be sorted and without any |
| 234 | * overlapping entries and assumes the first e820 area to be RAM. |
| 235 | */ |
| 236 | void __init e820_mark_nosave_regions(void) |
| 237 | { |
| 238 | int i; |
| 239 | unsigned long pfn; |
| 240 | |
| 241 | pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size); |
| 242 | for (i = 1; i < e820.nr_map; i++) { |
| 243 | struct e820entry *ei = &e820.map[i]; |
| 244 | |
| 245 | if (pfn < PFN_UP(ei->addr)) |
| 246 | register_nosave_region(pfn, PFN_UP(ei->addr)); |
| 247 | |
| 248 | pfn = PFN_DOWN(ei->addr + ei->size); |
| 249 | if (ei->type != E820_RAM) |
| 250 | register_nosave_region(PFN_UP(ei->addr), pfn); |
| 251 | |
| 252 | if (pfn >= max_low_pfn) |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | #endif |
| 257 | |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 258 | void __init add_memory_region(unsigned long long start, |
| 259 | unsigned long long size, int type) |
| 260 | { |
| 261 | int x; |
| 262 | |
Huang, Ying | 2215e69 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 263 | x = e820.nr_map; |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 264 | |
Huang, Ying | 2215e69 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 265 | if (x == E820MAX) { |
| 266 | printk(KERN_ERR "Ooops! Too many entries in the memory map!\n"); |
| 267 | return; |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 268 | } |
Huang, Ying | 2215e69 | 2008-01-30 13:31:19 +0100 | [diff] [blame] | 269 | |
| 270 | e820.map[x].addr = start; |
| 271 | e820.map[x].size = size; |
| 272 | e820.map[x].type = type; |
| 273 | e820.nr_map++; |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 274 | } /* add_memory_region */ |
| 275 | |
| 276 | /* |
| 277 | * Sanitize the BIOS e820 map. |
| 278 | * |
| 279 | * Some e820 responses include overlapping entries. The following |
| 280 | * replaces the original e820 map with a new one, removing overlaps. |
| 281 | * |
| 282 | */ |
| 283 | int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map) |
| 284 | { |
| 285 | struct change_member *change_tmp; |
| 286 | unsigned long current_type, last_type; |
| 287 | unsigned long long last_addr; |
| 288 | int chgidx, still_changing; |
| 289 | int overlap_entries; |
| 290 | int new_bios_entry; |
| 291 | int old_nr, new_nr, chg_nr; |
| 292 | int i; |
| 293 | |
| 294 | /* |
| 295 | Visually we're performing the following (1,2,3,4 = memory types)... |
| 296 | |
| 297 | Sample memory map (w/overlaps): |
| 298 | ____22__________________ |
| 299 | ______________________4_ |
| 300 | ____1111________________ |
| 301 | _44_____________________ |
| 302 | 11111111________________ |
| 303 | ____________________33__ |
| 304 | ___________44___________ |
| 305 | __________33333_________ |
| 306 | ______________22________ |
| 307 | ___________________2222_ |
| 308 | _________111111111______ |
| 309 | _____________________11_ |
| 310 | _________________4______ |
| 311 | |
| 312 | Sanitized equivalent (no overlap): |
| 313 | 1_______________________ |
| 314 | _44_____________________ |
| 315 | ___1____________________ |
| 316 | ____22__________________ |
| 317 | ______11________________ |
| 318 | _________1______________ |
| 319 | __________3_____________ |
| 320 | ___________44___________ |
| 321 | _____________33_________ |
| 322 | _______________2________ |
| 323 | ________________1_______ |
| 324 | _________________4______ |
| 325 | ___________________2____ |
| 326 | ____________________33__ |
| 327 | ______________________4_ |
| 328 | */ |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 329 | /* if there's only one memory region, don't bother */ |
| 330 | if (*pnr_map < 2) { |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 331 | return -1; |
| 332 | } |
| 333 | |
| 334 | old_nr = *pnr_map; |
| 335 | |
| 336 | /* bail out if we find any unreasonable addresses in bios map */ |
| 337 | for (i=0; i<old_nr; i++) |
| 338 | if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) { |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | /* create pointers for initial change-point information (for sorting) */ |
| 343 | for (i=0; i < 2*old_nr; i++) |
| 344 | change_point[i] = &change_point_list[i]; |
| 345 | |
| 346 | /* record all known change-points (starting and ending addresses), |
| 347 | omitting those that are for empty memory regions */ |
| 348 | chgidx = 0; |
| 349 | for (i=0; i < old_nr; i++) { |
| 350 | if (biosmap[i].size != 0) { |
| 351 | change_point[chgidx]->addr = biosmap[i].addr; |
| 352 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 353 | change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size; |
| 354 | change_point[chgidx++]->pbios = &biosmap[i]; |
| 355 | } |
| 356 | } |
| 357 | chg_nr = chgidx; /* true number of change-points */ |
| 358 | |
| 359 | /* sort change-point list by memory addresses (low -> high) */ |
| 360 | still_changing = 1; |
| 361 | while (still_changing) { |
| 362 | still_changing = 0; |
| 363 | for (i=1; i < chg_nr; i++) { |
| 364 | /* if <current_addr> > <last_addr>, swap */ |
| 365 | /* or, if current=<start_addr> & last=<end_addr>, swap */ |
| 366 | if ((change_point[i]->addr < change_point[i-1]->addr) || |
| 367 | ((change_point[i]->addr == change_point[i-1]->addr) && |
| 368 | (change_point[i]->addr == change_point[i]->pbios->addr) && |
| 369 | (change_point[i-1]->addr != change_point[i-1]->pbios->addr)) |
| 370 | ) |
| 371 | { |
| 372 | change_tmp = change_point[i]; |
| 373 | change_point[i] = change_point[i-1]; |
| 374 | change_point[i-1] = change_tmp; |
| 375 | still_changing=1; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | /* create a new bios memory map, removing overlaps */ |
| 381 | overlap_entries=0; /* number of entries in the overlap table */ |
| 382 | new_bios_entry=0; /* index for creating new bios map entries */ |
| 383 | last_type = 0; /* start with undefined memory type */ |
| 384 | last_addr = 0; /* start with 0 as last starting address */ |
| 385 | /* loop through change-points, determining affect on the new bios map */ |
| 386 | for (chgidx=0; chgidx < chg_nr; chgidx++) |
| 387 | { |
| 388 | /* keep track of all overlapping bios entries */ |
| 389 | if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr) |
| 390 | { |
| 391 | /* add map entry to overlap list (> 1 entry implies an overlap) */ |
| 392 | overlap_list[overlap_entries++]=change_point[chgidx]->pbios; |
| 393 | } |
| 394 | else |
| 395 | { |
| 396 | /* remove entry from list (order independent, so swap with last) */ |
| 397 | for (i=0; i<overlap_entries; i++) |
| 398 | { |
| 399 | if (overlap_list[i] == change_point[chgidx]->pbios) |
| 400 | overlap_list[i] = overlap_list[overlap_entries-1]; |
| 401 | } |
| 402 | overlap_entries--; |
| 403 | } |
| 404 | /* if there are overlapping entries, decide which "type" to use */ |
| 405 | /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */ |
| 406 | current_type = 0; |
| 407 | for (i=0; i<overlap_entries; i++) |
| 408 | if (overlap_list[i]->type > current_type) |
| 409 | current_type = overlap_list[i]->type; |
| 410 | /* continue building up new bios map based on this information */ |
| 411 | if (current_type != last_type) { |
| 412 | if (last_type != 0) { |
| 413 | new_bios[new_bios_entry].size = |
| 414 | change_point[chgidx]->addr - last_addr; |
| 415 | /* move forward only if the new size was non-zero */ |
| 416 | if (new_bios[new_bios_entry].size != 0) |
| 417 | if (++new_bios_entry >= E820MAX) |
| 418 | break; /* no more space left for new bios entries */ |
| 419 | } |
| 420 | if (current_type != 0) { |
| 421 | new_bios[new_bios_entry].addr = change_point[chgidx]->addr; |
| 422 | new_bios[new_bios_entry].type = current_type; |
| 423 | last_addr=change_point[chgidx]->addr; |
| 424 | } |
| 425 | last_type = current_type; |
| 426 | } |
| 427 | } |
| 428 | new_nr = new_bios_entry; /* retain count for new bios entries */ |
| 429 | |
| 430 | /* copy new bios mapping into original location */ |
| 431 | memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry)); |
| 432 | *pnr_map = new_nr; |
| 433 | |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | /* |
| 438 | * Copy the BIOS e820 map into a safe place. |
| 439 | * |
| 440 | * Sanity-check it while we're at it.. |
| 441 | * |
| 442 | * If we're lucky and live on a modern system, the setup code |
| 443 | * will have given us a memory map that we can use to properly |
| 444 | * set up memory. If we aren't, we'll fake a memory map. |
| 445 | * |
| 446 | * We check to see that the memory map contains at least 2 elements |
| 447 | * before we'll use it, because the detection code in setup.S may |
| 448 | * not be perfect and most every PC known to man has two memory |
| 449 | * regions: one from 0 to 640k, and one from 1mb up. (The IBM |
| 450 | * thinkpad 560x, for example, does not cooperate with the memory |
| 451 | * detection code.) |
| 452 | */ |
Alexander van Heukelum | dedd04b | 2008-03-01 17:09:12 +0100 | [diff] [blame] | 453 | int __init copy_e820_map(struct e820entry *biosmap, int nr_map) |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 454 | { |
| 455 | /* Only one memory region (or negative)? Ignore it */ |
| 456 | if (nr_map < 2) |
| 457 | return -1; |
| 458 | |
| 459 | do { |
Alexander van Heukelum | dedd04b | 2008-03-01 17:09:12 +0100 | [diff] [blame] | 460 | u64 start = biosmap->addr; |
| 461 | u64 size = biosmap->size; |
| 462 | u64 end = start + size; |
| 463 | u32 type = biosmap->type; |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 464 | |
| 465 | /* Overflow in 64 bits? Ignore the memory map. */ |
| 466 | if (start > end) |
| 467 | return -1; |
| 468 | |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 469 | add_memory_region(start, size, type); |
Alexander van Heukelum | dedd04b | 2008-03-01 17:09:12 +0100 | [diff] [blame] | 470 | } while (biosmap++, --nr_map); |
| 471 | |
bibo,mao | 8e3342f | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 472 | return 0; |
| 473 | } |
| 474 | |
bibo,mao | b2dff6a | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 475 | /* |
bibo,mao | b2dff6a | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 476 | * Find the highest page frame number we have available |
| 477 | */ |
Ingo Molnar | fa5c463 | 2008-04-16 02:29:42 +0200 | [diff] [blame] | 478 | void __init propagate_e820_map(void) |
bibo,mao | b2dff6a | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 479 | { |
| 480 | int i; |
| 481 | |
| 482 | max_pfn = 0; |
bibo,mao | b2dff6a | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 483 | |
| 484 | for (i = 0; i < e820.nr_map; i++) { |
| 485 | unsigned long start, end; |
| 486 | /* RAM? */ |
| 487 | if (e820.map[i].type != E820_RAM) |
| 488 | continue; |
| 489 | start = PFN_UP(e820.map[i].addr); |
| 490 | end = PFN_DOWN(e820.map[i].addr + e820.map[i].size); |
| 491 | if (start >= end) |
| 492 | continue; |
| 493 | if (end > max_pfn) |
| 494 | max_pfn = end; |
| 495 | memory_present(0, start, end); |
| 496 | } |
| 497 | } |
bibo,mao | b5b2405 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 498 | |
| 499 | /* |
bibo,mao | b5b2405 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 500 | * Register fully available low RAM pages with the bootmem allocator. |
| 501 | */ |
| 502 | void __init register_bootmem_low_pages(unsigned long max_low_pfn) |
| 503 | { |
| 504 | int i; |
| 505 | |
bibo,mao | b5b2405 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 506 | for (i = 0; i < e820.nr_map; i++) { |
| 507 | unsigned long curr_pfn, last_pfn, size; |
| 508 | /* |
| 509 | * Reserve usable low memory |
| 510 | */ |
| 511 | if (e820.map[i].type != E820_RAM) |
| 512 | continue; |
| 513 | /* |
| 514 | * We are rounding up the start address of usable memory: |
| 515 | */ |
| 516 | curr_pfn = PFN_UP(e820.map[i].addr); |
| 517 | if (curr_pfn >= max_low_pfn) |
| 518 | continue; |
| 519 | /* |
| 520 | * ... and at the end of the usable range downwards: |
| 521 | */ |
| 522 | last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size); |
| 523 | |
| 524 | if (last_pfn > max_low_pfn) |
| 525 | last_pfn = max_low_pfn; |
| 526 | |
| 527 | /* |
| 528 | * .. finally, did all the rounding and playing |
| 529 | * around just make the area go away? |
| 530 | */ |
| 531 | if (last_pfn <= curr_pfn) |
| 532 | continue; |
| 533 | |
| 534 | size = last_pfn - curr_pfn; |
| 535 | free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size)); |
| 536 | } |
| 537 | } |
| 538 | |
Yasunori Goto | 5c95da9 | 2006-12-22 01:09:54 -0800 | [diff] [blame] | 539 | void __init e820_register_memory(void) |
bibo,mao | b5b2405 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 540 | { |
| 541 | unsigned long gapstart, gapsize, round; |
| 542 | unsigned long long last; |
| 543 | int i; |
| 544 | |
| 545 | /* |
Simon Arlott | 27b46d7 | 2007-10-20 01:13:56 +0200 | [diff] [blame] | 546 | * Search for the biggest gap in the low 32 bits of the e820 |
bibo,mao | b5b2405 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 547 | * memory space. |
| 548 | */ |
| 549 | last = 0x100000000ull; |
| 550 | gapstart = 0x10000000; |
| 551 | gapsize = 0x400000; |
| 552 | i = e820.nr_map; |
| 553 | while (--i >= 0) { |
| 554 | unsigned long long start = e820.map[i].addr; |
| 555 | unsigned long long end = start + e820.map[i].size; |
| 556 | |
| 557 | /* |
| 558 | * Since "last" is at most 4GB, we know we'll |
| 559 | * fit in 32 bits if this condition is true |
| 560 | */ |
| 561 | if (last > end) { |
| 562 | unsigned long gap = last - end; |
| 563 | |
| 564 | if (gap > gapsize) { |
| 565 | gapsize = gap; |
| 566 | gapstart = end; |
| 567 | } |
| 568 | } |
| 569 | if (start < last) |
| 570 | last = start; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * See how much we want to round up: start off with |
| 575 | * rounding to the next 1MB area. |
| 576 | */ |
| 577 | round = 0x100000; |
| 578 | while ((gapsize >> 4) > round) |
| 579 | round += round; |
| 580 | /* Fun with two's complement */ |
| 581 | pci_mem_start = (gapstart + round) & -round; |
| 582 | |
| 583 | printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n", |
| 584 | pci_mem_start, gapstart, gapsize); |
| 585 | } |
bibo,mao | cef518e | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 586 | |
| 587 | void __init print_memory_map(char *who) |
| 588 | { |
| 589 | int i; |
| 590 | |
| 591 | for (i = 0; i < e820.nr_map; i++) { |
| 592 | printk(" %s: %016Lx - %016Lx ", who, |
| 593 | e820.map[i].addr, |
| 594 | e820.map[i].addr + e820.map[i].size); |
| 595 | switch (e820.map[i].type) { |
| 596 | case E820_RAM: printk("(usable)\n"); |
| 597 | break; |
| 598 | case E820_RESERVED: |
| 599 | printk("(reserved)\n"); |
| 600 | break; |
| 601 | case E820_ACPI: |
| 602 | printk("(ACPI data)\n"); |
| 603 | break; |
| 604 | case E820_NVS: |
| 605 | printk("(ACPI NVS)\n"); |
| 606 | break; |
H. Peter Anvin | 9c25d13 | 2007-07-11 12:18:34 -0700 | [diff] [blame] | 607 | default: printk("type %u\n", e820.map[i].type); |
bibo,mao | cef518e | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 608 | break; |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
Artiom Myaskouvskey | bf7e6a1 | 2006-12-07 02:14:11 +0100 | [diff] [blame] | 613 | void __init limit_regions(unsigned long long size) |
| 614 | { |
| 615 | unsigned long long current_addr; |
bibo,mao | cef518e | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 616 | int i; |
| 617 | |
| 618 | print_memory_map("limit_regions start"); |
bibo,mao | cef518e | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 619 | for (i = 0; i < e820.nr_map; i++) { |
| 620 | current_addr = e820.map[i].addr + e820.map[i].size; |
| 621 | if (current_addr < size) |
| 622 | continue; |
| 623 | |
| 624 | if (e820.map[i].type != E820_RAM) |
| 625 | continue; |
| 626 | |
| 627 | if (e820.map[i].addr >= size) { |
| 628 | /* |
| 629 | * This region starts past the end of the |
| 630 | * requested size, skip it completely. |
| 631 | */ |
| 632 | e820.nr_map = i; |
| 633 | } else { |
| 634 | e820.nr_map = i + 1; |
| 635 | e820.map[i].size -= current_addr - size; |
| 636 | } |
| 637 | print_memory_map("limit_regions endfor"); |
| 638 | return; |
| 639 | } |
| 640 | print_memory_map("limit_regions endfunc"); |
| 641 | } |
| 642 | |
Jan Beulich | b92e9fa | 2007-05-02 19:27:11 +0200 | [diff] [blame] | 643 | /* |
| 644 | * This function checks if any part of the range <start,end> is mapped |
| 645 | * with type. |
| 646 | */ |
| 647 | int |
| 648 | e820_any_mapped(u64 start, u64 end, unsigned type) |
| 649 | { |
| 650 | int i; |
| 651 | for (i = 0; i < e820.nr_map; i++) { |
| 652 | const struct e820entry *ei = &e820.map[i]; |
| 653 | if (type && ei->type != type) |
| 654 | continue; |
| 655 | if (ei->addr >= end || ei->addr + ei->size <= start) |
| 656 | continue; |
| 657 | return 1; |
| 658 | } |
| 659 | return 0; |
| 660 | } |
| 661 | EXPORT_SYMBOL_GPL(e820_any_mapped); |
| 662 | |
bibo,mao | cef518e | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 663 | /* |
| 664 | * This function checks if the entire range <start,end> is mapped with type. |
| 665 | * |
| 666 | * Note: this function only works correct if the e820 table is sorted and |
| 667 | * not-overlapping, which is the case |
| 668 | */ |
| 669 | int __init |
| 670 | e820_all_mapped(unsigned long s, unsigned long e, unsigned type) |
| 671 | { |
| 672 | u64 start = s; |
| 673 | u64 end = e; |
| 674 | int i; |
| 675 | for (i = 0; i < e820.nr_map; i++) { |
| 676 | struct e820entry *ei = &e820.map[i]; |
| 677 | if (type && ei->type != type) |
| 678 | continue; |
| 679 | /* is the region (part) in overlap with the current region ?*/ |
| 680 | if (ei->addr >= end || ei->addr + ei->size <= start) |
| 681 | continue; |
| 682 | /* if the region is at the beginning of <start,end> we move |
| 683 | * start to the end of the region since it's ok until there |
| 684 | */ |
| 685 | if (ei->addr <= start) |
| 686 | start = ei->addr + ei->size; |
| 687 | /* if start is now at or beyond end, we're done, full |
| 688 | * coverage */ |
| 689 | if (start >= end) |
| 690 | return 1; /* we're done */ |
| 691 | } |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | static int __init parse_memmap(char *arg) |
| 696 | { |
| 697 | if (!arg) |
| 698 | return -EINVAL; |
| 699 | |
| 700 | if (strcmp(arg, "exactmap") == 0) { |
| 701 | #ifdef CONFIG_CRASH_DUMP |
| 702 | /* If we are doing a crash dump, we |
| 703 | * still need to know the real mem |
| 704 | * size before original memory map is |
| 705 | * reset. |
| 706 | */ |
Ingo Molnar | fa5c463 | 2008-04-16 02:29:42 +0200 | [diff] [blame] | 707 | propagate_e820_map(); |
bibo,mao | cef518e | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 708 | saved_max_pfn = max_pfn; |
| 709 | #endif |
| 710 | e820.nr_map = 0; |
| 711 | user_defined_memmap = 1; |
| 712 | } else { |
| 713 | /* If the user specifies memory size, we |
| 714 | * limit the BIOS-provided memory map to |
| 715 | * that size. exactmap can be used to specify |
| 716 | * the exact map. mem=number can be used to |
| 717 | * trim the existing memory map. |
| 718 | */ |
| 719 | unsigned long long start_at, mem_size; |
| 720 | |
| 721 | mem_size = memparse(arg, &arg); |
| 722 | if (*arg == '@') { |
| 723 | start_at = memparse(arg+1, &arg); |
| 724 | add_memory_region(start_at, mem_size, E820_RAM); |
| 725 | } else if (*arg == '#') { |
| 726 | start_at = memparse(arg+1, &arg); |
| 727 | add_memory_region(start_at, mem_size, E820_ACPI); |
| 728 | } else if (*arg == '$') { |
| 729 | start_at = memparse(arg+1, &arg); |
| 730 | add_memory_region(start_at, mem_size, E820_RESERVED); |
| 731 | } else { |
| 732 | limit_regions(mem_size); |
| 733 | user_defined_memmap = 1; |
| 734 | } |
| 735 | } |
| 736 | return 0; |
| 737 | } |
| 738 | early_param("memmap", parse_memmap); |
Yinghai Lu | 5dca6a1 | 2008-03-18 16:44:19 -0700 | [diff] [blame] | 739 | void __init update_memory_range(u64 start, u64 size, unsigned old_type, |
| 740 | unsigned new_type) |
| 741 | { |
| 742 | int i; |
| 743 | |
| 744 | BUG_ON(old_type == new_type); |
| 745 | |
| 746 | for (i = 0; i < e820.nr_map; i++) { |
| 747 | struct e820entry *ei = &e820.map[i]; |
| 748 | u64 final_start, final_end; |
| 749 | if (ei->type != old_type) |
| 750 | continue; |
| 751 | /* totally covered? */ |
| 752 | if (ei->addr >= start && ei->size <= size) { |
| 753 | ei->type = new_type; |
| 754 | continue; |
| 755 | } |
| 756 | /* partially covered */ |
| 757 | final_start = max(start, ei->addr); |
| 758 | final_end = min(start + size, ei->addr + ei->size); |
| 759 | if (final_start >= final_end) |
| 760 | continue; |
| 761 | add_memory_region(final_start, final_end - final_start, |
| 762 | new_type); |
| 763 | } |
| 764 | } |
Yinghai Lu | 093af8d | 2008-01-30 13:33:32 +0100 | [diff] [blame] | 765 | void __init update_e820(void) |
| 766 | { |
| 767 | u8 nr_map; |
| 768 | |
| 769 | nr_map = e820.nr_map; |
| 770 | if (sanitize_e820_map(e820.map, &nr_map)) |
| 771 | return; |
| 772 | e820.nr_map = nr_map; |
| 773 | printk(KERN_INFO "modified physical RAM map:\n"); |
| 774 | print_memory_map("modified"); |
| 775 | } |