blob: 18f500d185a2ae182b38852d51729a32995faee4 [file] [log] [blame]
bibo,mao269c2d82006-12-07 02:14:06 +01001#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>
10#include <linux/efi.h>
bibo,maob2dff6a2006-12-07 02:14:06 +010011#include <linux/pfn.h>
Rusty Russellbd472c72006-12-07 02:14:08 +010012#include <linux/uaccess.h>
Rafael J. Wysocki1c100702007-07-21 17:11:09 +020013#include <linux/suspend.h>
bibo,mao269c2d82006-12-07 02:14:06 +010014
15#include <asm/pgtable.h>
16#include <asm/page.h>
17#include <asm/e820.h>
Adrian Bunk90611fe2007-02-13 13:26:21 +010018#include <asm/setup.h>
bibo,mao269c2d82006-12-07 02:14:06 +010019
20#ifdef CONFIG_EFI
21int efi_enabled = 0;
22EXPORT_SYMBOL(efi_enabled);
23#endif
24
25struct e820map e820;
bibo,mao8e3342f2006-12-07 02:14:06 +010026struct change_member {
27 struct e820entry *pbios; /* pointer to original bios entry */
28 unsigned long long addr; /* address for this change point */
29};
30static struct change_member change_point_list[2*E820MAX] __initdata;
31static struct change_member *change_point[2*E820MAX] __initdata;
32static struct e820entry *overlap_list[E820MAX] __initdata;
33static struct e820entry new_bios[E820MAX] __initdata;
bibo,maob5b24052006-12-07 02:14:06 +010034/* For PCI or other memory-mapped resources */
35unsigned long pci_mem_start = 0x10000000;
36#ifdef CONFIG_PCI
37EXPORT_SYMBOL(pci_mem_start);
38#endif
bibo,maocef518e2006-12-07 02:14:06 +010039extern int user_defined_memmap;
bibo,mao269c2d82006-12-07 02:14:06 +010040struct resource data_resource = {
41 .name = "Kernel data",
42 .start = 0,
43 .end = 0,
44 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
45};
46
47struct resource code_resource = {
48 .name = "Kernel code",
49 .start = 0,
50 .end = 0,
51 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
52};
53
Bernhard Walle00bf4092007-10-21 16:42:01 -070054struct resource bss_resource = {
55 .name = "Kernel bss",
56 .start = 0,
57 .end = 0,
58 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
59};
60
bibo,mao269c2d82006-12-07 02:14:06 +010061static struct resource system_rom_resource = {
62 .name = "System ROM",
63 .start = 0xf0000,
64 .end = 0xfffff,
65 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
66};
67
68static struct resource extension_rom_resource = {
69 .name = "Extension ROM",
70 .start = 0xe0000,
71 .end = 0xeffff,
72 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
73};
74
75static struct resource adapter_rom_resources[] = { {
76 .name = "Adapter ROM",
77 .start = 0xc8000,
78 .end = 0,
79 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
80}, {
81 .name = "Adapter ROM",
82 .start = 0,
83 .end = 0,
84 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
85}, {
86 .name = "Adapter ROM",
87 .start = 0,
88 .end = 0,
89 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
90}, {
91 .name = "Adapter ROM",
92 .start = 0,
93 .end = 0,
94 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
95}, {
96 .name = "Adapter ROM",
97 .start = 0,
98 .end = 0,
99 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
100}, {
101 .name = "Adapter ROM",
102 .start = 0,
103 .end = 0,
104 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
105} };
106
107static struct resource video_rom_resource = {
108 .name = "Video ROM",
109 .start = 0xc0000,
110 .end = 0xc7fff,
111 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
112};
113
114static struct resource video_ram_resource = {
115 .name = "Video RAM area",
116 .start = 0xa0000,
117 .end = 0xbffff,
118 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
119};
120
121static struct resource standard_io_resources[] = { {
122 .name = "dma1",
123 .start = 0x0000,
124 .end = 0x001f,
125 .flags = IORESOURCE_BUSY | IORESOURCE_IO
126}, {
127 .name = "pic1",
128 .start = 0x0020,
129 .end = 0x0021,
130 .flags = IORESOURCE_BUSY | IORESOURCE_IO
131}, {
132 .name = "timer0",
133 .start = 0x0040,
134 .end = 0x0043,
135 .flags = IORESOURCE_BUSY | IORESOURCE_IO
136}, {
137 .name = "timer1",
138 .start = 0x0050,
139 .end = 0x0053,
140 .flags = IORESOURCE_BUSY | IORESOURCE_IO
141}, {
142 .name = "keyboard",
143 .start = 0x0060,
144 .end = 0x006f,
145 .flags = IORESOURCE_BUSY | IORESOURCE_IO
146}, {
147 .name = "dma page reg",
148 .start = 0x0080,
149 .end = 0x008f,
150 .flags = IORESOURCE_BUSY | IORESOURCE_IO
151}, {
152 .name = "pic2",
153 .start = 0x00a0,
154 .end = 0x00a1,
155 .flags = IORESOURCE_BUSY | IORESOURCE_IO
156}, {
157 .name = "dma2",
158 .start = 0x00c0,
159 .end = 0x00df,
160 .flags = IORESOURCE_BUSY | IORESOURCE_IO
161}, {
162 .name = "fpu",
163 .start = 0x00f0,
164 .end = 0x00ff,
165 .flags = IORESOURCE_BUSY | IORESOURCE_IO
166} };
167
Rene Herman3b3d5e12007-02-13 13:26:22 +0100168#define ROMSIGNATURE 0xaa55
169
170static int __init romsignature(const unsigned char *rom)
Rusty Russellbd472c72006-12-07 02:14:08 +0100171{
Rene Herman0adad172007-05-02 19:27:05 +0200172 const unsigned short * const ptr = (const unsigned short *)rom;
Rusty Russellbd472c72006-12-07 02:14:08 +0100173 unsigned short sig;
Rene Herman3b3d5e12007-02-13 13:26:22 +0100174
Rene Herman0adad172007-05-02 19:27:05 +0200175 return probe_kernel_address(ptr, sig) == 0 && sig == ROMSIGNATURE;
Rusty Russellbd472c72006-12-07 02:14:08 +0100176}
bibo,mao269c2d82006-12-07 02:14:06 +0100177
Rene Herman0adad172007-05-02 19:27:05 +0200178static int __init romchecksum(const unsigned char *rom, unsigned long length)
bibo,mao269c2d82006-12-07 02:14:06 +0100179{
Rene Herman0adad172007-05-02 19:27:05 +0200180 unsigned char sum, c;
bibo,mao269c2d82006-12-07 02:14:06 +0100181
Rene Herman0adad172007-05-02 19:27:05 +0200182 for (sum = 0; length && probe_kernel_address(rom++, c) == 0; length--)
183 sum += c;
184 return !length && !sum;
bibo,mao269c2d82006-12-07 02:14:06 +0100185}
186
187static void __init probe_roms(void)
188{
Rene Herman0adad172007-05-02 19:27:05 +0200189 const unsigned char *rom;
bibo,mao269c2d82006-12-07 02:14:06 +0100190 unsigned long start, length, upper;
Rene Herman0adad172007-05-02 19:27:05 +0200191 unsigned char c;
192 int i;
bibo,mao269c2d82006-12-07 02:14:06 +0100193
194 /* video rom */
195 upper = adapter_rom_resources[0].start;
196 for (start = video_rom_resource.start; start < upper; start += 2048) {
197 rom = isa_bus_to_virt(start);
198 if (!romsignature(rom))
199 continue;
200
201 video_rom_resource.start = start;
202
Rene Herman0adad172007-05-02 19:27:05 +0200203 if (probe_kernel_address(rom + 2, c) != 0)
204 continue;
205
bibo,mao269c2d82006-12-07 02:14:06 +0100206 /* 0 < length <= 0x7f * 512, historically */
Rene Herman0adad172007-05-02 19:27:05 +0200207 length = c * 512;
bibo,mao269c2d82006-12-07 02:14:06 +0100208
209 /* if checksum okay, trust length byte */
210 if (length && romchecksum(rom, length))
211 video_rom_resource.end = start + length - 1;
212
213 request_resource(&iomem_resource, &video_rom_resource);
214 break;
215 }
216
217 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
218 if (start < upper)
219 start = upper;
220
221 /* system rom */
222 request_resource(&iomem_resource, &system_rom_resource);
223 upper = system_rom_resource.start;
224
225 /* check for extension rom (ignore length byte!) */
226 rom = isa_bus_to_virt(extension_rom_resource.start);
227 if (romsignature(rom)) {
228 length = extension_rom_resource.end - extension_rom_resource.start + 1;
229 if (romchecksum(rom, length)) {
230 request_resource(&iomem_resource, &extension_rom_resource);
231 upper = extension_rom_resource.start;
232 }
233 }
234
235 /* check for adapter roms on 2k boundaries */
236 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
237 rom = isa_bus_to_virt(start);
238 if (!romsignature(rom))
239 continue;
240
Rene Herman0adad172007-05-02 19:27:05 +0200241 if (probe_kernel_address(rom + 2, c) != 0)
242 continue;
243
bibo,mao269c2d82006-12-07 02:14:06 +0100244 /* 0 < length <= 0x7f * 512, historically */
Rene Herman0adad172007-05-02 19:27:05 +0200245 length = c * 512;
bibo,mao269c2d82006-12-07 02:14:06 +0100246
247 /* but accept any length that fits if checksum okay */
248 if (!length || start + length > upper || !romchecksum(rom, length))
249 continue;
250
251 adapter_rom_resources[i].start = start;
252 adapter_rom_resources[i].end = start + length - 1;
253 request_resource(&iomem_resource, &adapter_rom_resources[i]);
254
255 start = adapter_rom_resources[i++].end & ~2047UL;
256 }
257}
258
259/*
260 * Request address space for all standard RAM and ROM resources
261 * and also for regions reported as reserved by the e820.
262 */
263static void __init
Bernhard Walle00bf4092007-10-21 16:42:01 -0700264legacy_init_iomem_resources(struct resource *code_resource,
265 struct resource *data_resource,
266 struct resource *bss_resource)
bibo,mao269c2d82006-12-07 02:14:06 +0100267{
268 int i;
269
270 probe_roms();
271 for (i = 0; i < e820.nr_map; i++) {
272 struct resource *res;
273#ifndef CONFIG_RESOURCES_64BIT
274 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
275 continue;
276#endif
277 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
278 switch (e820.map[i].type) {
279 case E820_RAM: res->name = "System RAM"; break;
280 case E820_ACPI: res->name = "ACPI Tables"; break;
281 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
282 default: res->name = "reserved";
283 }
284 res->start = e820.map[i].addr;
285 res->end = res->start + e820.map[i].size - 1;
286 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
287 if (request_resource(&iomem_resource, res)) {
288 kfree(res);
289 continue;
290 }
291 if (e820.map[i].type == E820_RAM) {
292 /*
293 * We don't know which RAM region contains kernel data,
294 * so we try it repeatedly and let the resource manager
295 * test it.
296 */
297 request_resource(res, code_resource);
298 request_resource(res, data_resource);
Bernhard Walle00bf4092007-10-21 16:42:01 -0700299 request_resource(res, bss_resource);
bibo,mao269c2d82006-12-07 02:14:06 +0100300#ifdef CONFIG_KEXEC
Bernhard Walled62cc472007-10-18 23:40:59 -0700301 if (crashk_res.start != crashk_res.end)
302 request_resource(res, &crashk_res);
bibo,mao269c2d82006-12-07 02:14:06 +0100303#endif
304 }
305 }
306}
307
308/*
309 * Request address space for all standard resources
310 *
311 * This is called just before pcibios_init(), which is also a
312 * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
313 */
314static int __init request_standard_resources(void)
315{
316 int i;
317
318 printk("Setting up standard PCI resources\n");
319 if (efi_enabled)
Bernhard Walle00bf4092007-10-21 16:42:01 -0700320 efi_initialize_iomem_resources(&code_resource,
321 &data_resource, &bss_resource);
bibo,mao269c2d82006-12-07 02:14:06 +0100322 else
Bernhard Walle00bf4092007-10-21 16:42:01 -0700323 legacy_init_iomem_resources(&code_resource,
324 &data_resource, &bss_resource);
bibo,mao269c2d82006-12-07 02:14:06 +0100325
326 /* EFI systems may still have VGA */
327 request_resource(&iomem_resource, &video_ram_resource);
328
329 /* request I/O space for devices used on all i[345]86 PCs */
330 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
331 request_resource(&ioport_resource, &standard_io_resources[i]);
332 return 0;
333}
334
335subsys_initcall(request_standard_resources);
bibo,mao8e3342f2006-12-07 02:14:06 +0100336
Rafael J. Wysockib0cb1a12007-07-29 23:24:36 +0200337#if defined(CONFIG_PM) && defined(CONFIG_HIBERNATION)
Rafael J. Wysocki1c100702007-07-21 17:11:09 +0200338/**
339 * e820_mark_nosave_regions - Find the ranges of physical addresses that do not
340 * correspond to e820 RAM areas and mark the corresponding pages as nosave for
341 * hibernation.
342 *
343 * This function requires the e820 map to be sorted and without any
344 * overlapping entries and assumes the first e820 area to be RAM.
345 */
346void __init e820_mark_nosave_regions(void)
347{
348 int i;
349 unsigned long pfn;
350
351 pfn = PFN_DOWN(e820.map[0].addr + e820.map[0].size);
352 for (i = 1; i < e820.nr_map; i++) {
353 struct e820entry *ei = &e820.map[i];
354
355 if (pfn < PFN_UP(ei->addr))
356 register_nosave_region(pfn, PFN_UP(ei->addr));
357
358 pfn = PFN_DOWN(ei->addr + ei->size);
359 if (ei->type != E820_RAM)
360 register_nosave_region(PFN_UP(ei->addr), pfn);
361
362 if (pfn >= max_low_pfn)
363 break;
364 }
365}
366#endif
367
bibo,mao8e3342f2006-12-07 02:14:06 +0100368void __init add_memory_region(unsigned long long start,
369 unsigned long long size, int type)
370{
371 int x;
372
373 if (!efi_enabled) {
374 x = e820.nr_map;
375
376 if (x == E820MAX) {
377 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
378 return;
379 }
380
381 e820.map[x].addr = start;
382 e820.map[x].size = size;
383 e820.map[x].type = type;
384 e820.nr_map++;
385 }
386} /* add_memory_region */
387
388/*
389 * Sanitize the BIOS e820 map.
390 *
391 * Some e820 responses include overlapping entries. The following
392 * replaces the original e820 map with a new one, removing overlaps.
393 *
394 */
395int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
396{
397 struct change_member *change_tmp;
398 unsigned long current_type, last_type;
399 unsigned long long last_addr;
400 int chgidx, still_changing;
401 int overlap_entries;
402 int new_bios_entry;
403 int old_nr, new_nr, chg_nr;
404 int i;
405
406 /*
407 Visually we're performing the following (1,2,3,4 = memory types)...
408
409 Sample memory map (w/overlaps):
410 ____22__________________
411 ______________________4_
412 ____1111________________
413 _44_____________________
414 11111111________________
415 ____________________33__
416 ___________44___________
417 __________33333_________
418 ______________22________
419 ___________________2222_
420 _________111111111______
421 _____________________11_
422 _________________4______
423
424 Sanitized equivalent (no overlap):
425 1_______________________
426 _44_____________________
427 ___1____________________
428 ____22__________________
429 ______11________________
430 _________1______________
431 __________3_____________
432 ___________44___________
433 _____________33_________
434 _______________2________
435 ________________1_______
436 _________________4______
437 ___________________2____
438 ____________________33__
439 ______________________4_
440 */
bibo,mao8e3342f2006-12-07 02:14:06 +0100441 /* if there's only one memory region, don't bother */
442 if (*pnr_map < 2) {
bibo,mao8e3342f2006-12-07 02:14:06 +0100443 return -1;
444 }
445
446 old_nr = *pnr_map;
447
448 /* bail out if we find any unreasonable addresses in bios map */
449 for (i=0; i<old_nr; i++)
450 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
bibo,mao8e3342f2006-12-07 02:14:06 +0100451 return -1;
452 }
453
454 /* create pointers for initial change-point information (for sorting) */
455 for (i=0; i < 2*old_nr; i++)
456 change_point[i] = &change_point_list[i];
457
458 /* record all known change-points (starting and ending addresses),
459 omitting those that are for empty memory regions */
460 chgidx = 0;
461 for (i=0; i < old_nr; i++) {
462 if (biosmap[i].size != 0) {
463 change_point[chgidx]->addr = biosmap[i].addr;
464 change_point[chgidx++]->pbios = &biosmap[i];
465 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
466 change_point[chgidx++]->pbios = &biosmap[i];
467 }
468 }
469 chg_nr = chgidx; /* true number of change-points */
470
471 /* sort change-point list by memory addresses (low -> high) */
472 still_changing = 1;
473 while (still_changing) {
474 still_changing = 0;
475 for (i=1; i < chg_nr; i++) {
476 /* if <current_addr> > <last_addr>, swap */
477 /* or, if current=<start_addr> & last=<end_addr>, swap */
478 if ((change_point[i]->addr < change_point[i-1]->addr) ||
479 ((change_point[i]->addr == change_point[i-1]->addr) &&
480 (change_point[i]->addr == change_point[i]->pbios->addr) &&
481 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
482 )
483 {
484 change_tmp = change_point[i];
485 change_point[i] = change_point[i-1];
486 change_point[i-1] = change_tmp;
487 still_changing=1;
488 }
489 }
490 }
491
492 /* create a new bios memory map, removing overlaps */
493 overlap_entries=0; /* number of entries in the overlap table */
494 new_bios_entry=0; /* index for creating new bios map entries */
495 last_type = 0; /* start with undefined memory type */
496 last_addr = 0; /* start with 0 as last starting address */
497 /* loop through change-points, determining affect on the new bios map */
498 for (chgidx=0; chgidx < chg_nr; chgidx++)
499 {
500 /* keep track of all overlapping bios entries */
501 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
502 {
503 /* add map entry to overlap list (> 1 entry implies an overlap) */
504 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
505 }
506 else
507 {
508 /* remove entry from list (order independent, so swap with last) */
509 for (i=0; i<overlap_entries; i++)
510 {
511 if (overlap_list[i] == change_point[chgidx]->pbios)
512 overlap_list[i] = overlap_list[overlap_entries-1];
513 }
514 overlap_entries--;
515 }
516 /* if there are overlapping entries, decide which "type" to use */
517 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
518 current_type = 0;
519 for (i=0; i<overlap_entries; i++)
520 if (overlap_list[i]->type > current_type)
521 current_type = overlap_list[i]->type;
522 /* continue building up new bios map based on this information */
523 if (current_type != last_type) {
524 if (last_type != 0) {
525 new_bios[new_bios_entry].size =
526 change_point[chgidx]->addr - last_addr;
527 /* move forward only if the new size was non-zero */
528 if (new_bios[new_bios_entry].size != 0)
529 if (++new_bios_entry >= E820MAX)
530 break; /* no more space left for new bios entries */
531 }
532 if (current_type != 0) {
533 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
534 new_bios[new_bios_entry].type = current_type;
535 last_addr=change_point[chgidx]->addr;
536 }
537 last_type = current_type;
538 }
539 }
540 new_nr = new_bios_entry; /* retain count for new bios entries */
541
542 /* copy new bios mapping into original location */
543 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
544 *pnr_map = new_nr;
545
bibo,mao8e3342f2006-12-07 02:14:06 +0100546 return 0;
547}
548
549/*
550 * Copy the BIOS e820 map into a safe place.
551 *
552 * Sanity-check it while we're at it..
553 *
554 * If we're lucky and live on a modern system, the setup code
555 * will have given us a memory map that we can use to properly
556 * set up memory. If we aren't, we'll fake a memory map.
557 *
558 * We check to see that the memory map contains at least 2 elements
559 * before we'll use it, because the detection code in setup.S may
560 * not be perfect and most every PC known to man has two memory
561 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
562 * thinkpad 560x, for example, does not cooperate with the memory
563 * detection code.)
564 */
565int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
566{
567 /* Only one memory region (or negative)? Ignore it */
568 if (nr_map < 2)
569 return -1;
570
571 do {
572 unsigned long long start = biosmap->addr;
573 unsigned long long size = biosmap->size;
574 unsigned long long end = start + size;
575 unsigned long type = biosmap->type;
bibo,mao8e3342f2006-12-07 02:14:06 +0100576
577 /* Overflow in 64 bits? Ignore the memory map. */
578 if (start > end)
579 return -1;
580
581 /*
582 * Some BIOSes claim RAM in the 640k - 1M region.
583 * Not right. Fix it up.
584 */
585 if (type == E820_RAM) {
bibo,mao8e3342f2006-12-07 02:14:06 +0100586 if (start < 0x100000ULL && end > 0xA0000ULL) {
Andi Kleen13063832007-05-02 19:27:21 +0200587 if (start < 0xA0000ULL)
bibo,mao8e3342f2006-12-07 02:14:06 +0100588 add_memory_region(start, 0xA0000ULL-start, type);
Andi Kleen13063832007-05-02 19:27:21 +0200589 if (end <= 0x100000ULL)
bibo,mao8e3342f2006-12-07 02:14:06 +0100590 continue;
bibo,mao8e3342f2006-12-07 02:14:06 +0100591 start = 0x100000ULL;
592 size = end - start;
593 }
594 }
595 add_memory_region(start, size, type);
596 } while (biosmap++,--nr_map);
597 return 0;
598}
599
bibo,maob2dff6a2006-12-07 02:14:06 +0100600/*
601 * Callback for efi_memory_walk.
602 */
603static int __init
604efi_find_max_pfn(unsigned long start, unsigned long end, void *arg)
605{
606 unsigned long *max_pfn = arg, pfn;
607
608 if (start < end) {
609 pfn = PFN_UP(end -1);
610 if (pfn > *max_pfn)
611 *max_pfn = pfn;
612 }
613 return 0;
614}
615
616static int __init
617efi_memory_present_wrapper(unsigned long start, unsigned long end, void *arg)
618{
619 memory_present(0, PFN_UP(start), PFN_DOWN(end));
620 return 0;
621}
622
623/*
624 * Find the highest page frame number we have available
625 */
626void __init find_max_pfn(void)
627{
628 int i;
629
630 max_pfn = 0;
631 if (efi_enabled) {
632 efi_memmap_walk(efi_find_max_pfn, &max_pfn);
633 efi_memmap_walk(efi_memory_present_wrapper, NULL);
634 return;
635 }
636
637 for (i = 0; i < e820.nr_map; i++) {
638 unsigned long start, end;
639 /* RAM? */
640 if (e820.map[i].type != E820_RAM)
641 continue;
642 start = PFN_UP(e820.map[i].addr);
643 end = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
644 if (start >= end)
645 continue;
646 if (end > max_pfn)
647 max_pfn = end;
648 memory_present(0, start, end);
649 }
650}
bibo,maob5b24052006-12-07 02:14:06 +0100651
652/*
653 * Free all available memory for boot time allocation. Used
654 * as a callback function by efi_memory_walk()
655 */
656
657static int __init
658free_available_memory(unsigned long start, unsigned long end, void *arg)
659{
660 /* check max_low_pfn */
661 if (start >= (max_low_pfn << PAGE_SHIFT))
662 return 0;
663 if (end >= (max_low_pfn << PAGE_SHIFT))
664 end = max_low_pfn << PAGE_SHIFT;
665 if (start < end)
666 free_bootmem(start, end - start);
667
668 return 0;
669}
670/*
671 * Register fully available low RAM pages with the bootmem allocator.
672 */
673void __init register_bootmem_low_pages(unsigned long max_low_pfn)
674{
675 int i;
676
677 if (efi_enabled) {
678 efi_memmap_walk(free_available_memory, NULL);
679 return;
680 }
681 for (i = 0; i < e820.nr_map; i++) {
682 unsigned long curr_pfn, last_pfn, size;
683 /*
684 * Reserve usable low memory
685 */
686 if (e820.map[i].type != E820_RAM)
687 continue;
688 /*
689 * We are rounding up the start address of usable memory:
690 */
691 curr_pfn = PFN_UP(e820.map[i].addr);
692 if (curr_pfn >= max_low_pfn)
693 continue;
694 /*
695 * ... and at the end of the usable range downwards:
696 */
697 last_pfn = PFN_DOWN(e820.map[i].addr + e820.map[i].size);
698
699 if (last_pfn > max_low_pfn)
700 last_pfn = max_low_pfn;
701
702 /*
703 * .. finally, did all the rounding and playing
704 * around just make the area go away?
705 */
706 if (last_pfn <= curr_pfn)
707 continue;
708
709 size = last_pfn - curr_pfn;
710 free_bootmem(PFN_PHYS(curr_pfn), PFN_PHYS(size));
711 }
712}
713
Yasunori Goto5c95da92006-12-22 01:09:54 -0800714void __init e820_register_memory(void)
bibo,maob5b24052006-12-07 02:14:06 +0100715{
716 unsigned long gapstart, gapsize, round;
717 unsigned long long last;
718 int i;
719
720 /*
Simon Arlott27b46d72007-10-20 01:13:56 +0200721 * Search for the biggest gap in the low 32 bits of the e820
bibo,maob5b24052006-12-07 02:14:06 +0100722 * memory space.
723 */
724 last = 0x100000000ull;
725 gapstart = 0x10000000;
726 gapsize = 0x400000;
727 i = e820.nr_map;
728 while (--i >= 0) {
729 unsigned long long start = e820.map[i].addr;
730 unsigned long long end = start + e820.map[i].size;
731
732 /*
733 * Since "last" is at most 4GB, we know we'll
734 * fit in 32 bits if this condition is true
735 */
736 if (last > end) {
737 unsigned long gap = last - end;
738
739 if (gap > gapsize) {
740 gapsize = gap;
741 gapstart = end;
742 }
743 }
744 if (start < last)
745 last = start;
746 }
747
748 /*
749 * See how much we want to round up: start off with
750 * rounding to the next 1MB area.
751 */
752 round = 0x100000;
753 while ((gapsize >> 4) > round)
754 round += round;
755 /* Fun with two's complement */
756 pci_mem_start = (gapstart + round) & -round;
757
758 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
759 pci_mem_start, gapstart, gapsize);
760}
bibo,maocef518e2006-12-07 02:14:06 +0100761
762void __init print_memory_map(char *who)
763{
764 int i;
765
766 for (i = 0; i < e820.nr_map; i++) {
767 printk(" %s: %016Lx - %016Lx ", who,
768 e820.map[i].addr,
769 e820.map[i].addr + e820.map[i].size);
770 switch (e820.map[i].type) {
771 case E820_RAM: printk("(usable)\n");
772 break;
773 case E820_RESERVED:
774 printk("(reserved)\n");
775 break;
776 case E820_ACPI:
777 printk("(ACPI data)\n");
778 break;
779 case E820_NVS:
780 printk("(ACPI NVS)\n");
781 break;
H. Peter Anvin9c25d132007-07-11 12:18:34 -0700782 default: printk("type %u\n", e820.map[i].type);
bibo,maocef518e2006-12-07 02:14:06 +0100783 break;
784 }
785 }
786}
787
Artiom Myaskouvskeybf7e6a12006-12-07 02:14:11 +0100788static __init __always_inline void efi_limit_regions(unsigned long long size)
bibo,maocef518e2006-12-07 02:14:06 +0100789{
790 unsigned long long current_addr = 0;
Artiom Myaskouvskeybf7e6a12006-12-07 02:14:11 +0100791 efi_memory_desc_t *md, *next_md;
792 void *p, *p1;
793 int i, j;
794
795 j = 0;
796 p1 = memmap.map;
797 for (p = p1, i = 0; p < memmap.map_end; p += memmap.desc_size, i++) {
798 md = p;
799 next_md = p1;
800 current_addr = md->phys_addr +
801 PFN_PHYS(md->num_pages);
802 if (is_available_memory(md)) {
803 if (md->phys_addr >= size) continue;
804 memcpy(next_md, md, memmap.desc_size);
805 if (current_addr >= size) {
806 next_md->num_pages -=
807 PFN_UP(current_addr-size);
808 }
809 p1 += memmap.desc_size;
810 next_md = p1;
811 j++;
812 } else if ((md->attribute & EFI_MEMORY_RUNTIME) ==
813 EFI_MEMORY_RUNTIME) {
814 /* In order to make runtime services
815 * available we have to include runtime
816 * memory regions in memory map */
817 memcpy(next_md, md, memmap.desc_size);
818 p1 += memmap.desc_size;
819 next_md = p1;
820 j++;
821 }
822 }
823 memmap.nr_map = j;
824 memmap.map_end = memmap.map +
825 (memmap.nr_map * memmap.desc_size);
826}
827
828void __init limit_regions(unsigned long long size)
829{
830 unsigned long long current_addr;
bibo,maocef518e2006-12-07 02:14:06 +0100831 int i;
832
833 print_memory_map("limit_regions start");
834 if (efi_enabled) {
Artiom Myaskouvskeybf7e6a12006-12-07 02:14:11 +0100835 efi_limit_regions(size);
836 return;
bibo,maocef518e2006-12-07 02:14:06 +0100837 }
838 for (i = 0; i < e820.nr_map; i++) {
839 current_addr = e820.map[i].addr + e820.map[i].size;
840 if (current_addr < size)
841 continue;
842
843 if (e820.map[i].type != E820_RAM)
844 continue;
845
846 if (e820.map[i].addr >= size) {
847 /*
848 * This region starts past the end of the
849 * requested size, skip it completely.
850 */
851 e820.nr_map = i;
852 } else {
853 e820.nr_map = i + 1;
854 e820.map[i].size -= current_addr - size;
855 }
856 print_memory_map("limit_regions endfor");
857 return;
858 }
859 print_memory_map("limit_regions endfunc");
860}
861
Jan Beulichb92e9fa2007-05-02 19:27:11 +0200862/*
863 * This function checks if any part of the range <start,end> is mapped
864 * with type.
865 */
866int
867e820_any_mapped(u64 start, u64 end, unsigned type)
868{
869 int i;
870 for (i = 0; i < e820.nr_map; i++) {
871 const struct e820entry *ei = &e820.map[i];
872 if (type && ei->type != type)
873 continue;
874 if (ei->addr >= end || ei->addr + ei->size <= start)
875 continue;
876 return 1;
877 }
878 return 0;
879}
880EXPORT_SYMBOL_GPL(e820_any_mapped);
881
bibo,maocef518e2006-12-07 02:14:06 +0100882 /*
883 * This function checks if the entire range <start,end> is mapped with type.
884 *
885 * Note: this function only works correct if the e820 table is sorted and
886 * not-overlapping, which is the case
887 */
888int __init
889e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
890{
891 u64 start = s;
892 u64 end = e;
893 int i;
894 for (i = 0; i < e820.nr_map; i++) {
895 struct e820entry *ei = &e820.map[i];
896 if (type && ei->type != type)
897 continue;
898 /* is the region (part) in overlap with the current region ?*/
899 if (ei->addr >= end || ei->addr + ei->size <= start)
900 continue;
901 /* if the region is at the beginning of <start,end> we move
902 * start to the end of the region since it's ok until there
903 */
904 if (ei->addr <= start)
905 start = ei->addr + ei->size;
906 /* if start is now at or beyond end, we're done, full
907 * coverage */
908 if (start >= end)
909 return 1; /* we're done */
910 }
911 return 0;
912}
913
914static int __init parse_memmap(char *arg)
915{
916 if (!arg)
917 return -EINVAL;
918
919 if (strcmp(arg, "exactmap") == 0) {
920#ifdef CONFIG_CRASH_DUMP
921 /* If we are doing a crash dump, we
922 * still need to know the real mem
923 * size before original memory map is
924 * reset.
925 */
926 find_max_pfn();
927 saved_max_pfn = max_pfn;
928#endif
929 e820.nr_map = 0;
930 user_defined_memmap = 1;
931 } else {
932 /* If the user specifies memory size, we
933 * limit the BIOS-provided memory map to
934 * that size. exactmap can be used to specify
935 * the exact map. mem=number can be used to
936 * trim the existing memory map.
937 */
938 unsigned long long start_at, mem_size;
939
940 mem_size = memparse(arg, &arg);
941 if (*arg == '@') {
942 start_at = memparse(arg+1, &arg);
943 add_memory_region(start_at, mem_size, E820_RAM);
944 } else if (*arg == '#') {
945 start_at = memparse(arg+1, &arg);
946 add_memory_region(start_at, mem_size, E820_ACPI);
947 } else if (*arg == '$') {
948 start_at = memparse(arg+1, &arg);
949 add_memory_region(start_at, mem_size, E820_RESERVED);
950 } else {
951 limit_regions(mem_size);
952 user_defined_memmap = 1;
953 }
954 }
955 return 0;
956}
957early_param("memmap", parse_memmap);