blob: 4e34b0f9d6138bc9141fc6410ed59d6dabd14001 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
4 * $Id: e820.c,v 1.4 2002/09/19 19:25:32 ak Exp $
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -07005 *
6 * Getting sanitize_e820_map() in sync with i386 version by applying change:
7 * - Provisions for empty E820 memory regions (reported by certain BIOSes).
8 * Alex Achenbach <xela@slit.de>, December 2002.
9 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
10 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12#include <linux/config.h>
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/init.h>
16#include <linux/bootmem.h>
17#include <linux/ioport.h>
18#include <linux/string.h>
Eric W. Biederman5f5609d2005-06-25 14:58:04 -070019#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/page.h>
21#include <asm/e820.h>
22#include <asm/proto.h>
23#include <asm/bootsetup.h>
24
25extern char _end[];
26
27/*
28 * PFN of last memory page.
29 */
30unsigned long end_pfn;
Andi Kleenf3591ff2005-09-13 11:35:28 +020031EXPORT_SYMBOL(end_pfn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33/*
34 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
35 * The direct mapping extends to end_pfn_map, so that we can directly access
36 * apertures, ACPI and other tables without having to play with fixmaps.
37 */
38unsigned long end_pfn_map;
39
40/*
41 * Last pfn which the user wants to use.
42 */
43unsigned long end_user_pfn = MAXMEM>>PAGE_SHIFT;
44
45extern struct resource code_resource, data_resource;
46
47/* Check for some hardcoded bad areas that early boot is not allowed to touch */
48static inline int bad_addr(unsigned long *addrp, unsigned long size)
49{
50 unsigned long addr = *addrp, last = addr + size;
51
52 /* various gunk below that needed for SMP startup */
53 if (addr < 0x8000) {
54 *addrp = 0x8000;
55 return 1;
56 }
57
58 /* direct mapping tables of the kernel */
59 if (last >= table_start<<PAGE_SHIFT && addr < table_end<<PAGE_SHIFT) {
60 *addrp = table_end << PAGE_SHIFT;
61 return 1;
62 }
63
64 /* initrd */
65#ifdef CONFIG_BLK_DEV_INITRD
66 if (LOADER_TYPE && INITRD_START && last >= INITRD_START &&
67 addr < INITRD_START+INITRD_SIZE) {
68 *addrp = INITRD_START + INITRD_SIZE;
69 return 1;
70 }
71#endif
72 /* kernel code + 640k memory hole (later should not be needed, but
73 be paranoid for now) */
74 if (last >= 640*1024 && addr < __pa_symbol(&_end)) {
75 *addrp = __pa_symbol(&_end);
76 return 1;
77 }
78 /* XXX ramdisk image here? */
79 return 0;
80}
81
82int __init e820_mapped(unsigned long start, unsigned long end, unsigned type)
83{
84 int i;
85 for (i = 0; i < e820.nr_map; i++) {
86 struct e820entry *ei = &e820.map[i];
87 if (type && ei->type != type)
88 continue;
Eric W. Biederman48c8b112005-09-06 15:16:20 -070089 if (ei->addr >= end || ei->addr + ei->size <= start)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 continue;
91 return 1;
92 }
93 return 0;
94}
95
96/*
97 * Find a free area in a specific range.
98 */
99unsigned long __init find_e820_area(unsigned long start, unsigned long end, unsigned size)
100{
101 int i;
102 for (i = 0; i < e820.nr_map; i++) {
103 struct e820entry *ei = &e820.map[i];
104 unsigned long addr = ei->addr, last;
105 if (ei->type != E820_RAM)
106 continue;
107 if (addr < start)
108 addr = start;
109 if (addr > ei->addr + ei->size)
110 continue;
111 while (bad_addr(&addr, size) && addr+size < ei->addr + ei->size)
112 ;
113 last = addr + size;
114 if (last > ei->addr + ei->size)
115 continue;
116 if (last > end)
117 continue;
118 return addr;
119 }
120 return -1UL;
121}
122
123/*
124 * Free bootmem based on the e820 table for a node.
125 */
126void __init e820_bootmem_free(pg_data_t *pgdat, unsigned long start,unsigned long end)
127{
128 int i;
129 for (i = 0; i < e820.nr_map; i++) {
130 struct e820entry *ei = &e820.map[i];
131 unsigned long last, addr;
132
133 if (ei->type != E820_RAM ||
134 ei->addr+ei->size <= start ||
Andi Kleen7c7a3892005-09-12 18:49:24 +0200135 ei->addr >= end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 continue;
137
138 addr = round_up(ei->addr, PAGE_SIZE);
139 if (addr < start)
140 addr = start;
141
142 last = round_down(ei->addr + ei->size, PAGE_SIZE);
143 if (last >= end)
144 last = end;
145
146 if (last > addr && last-addr >= PAGE_SIZE)
147 free_bootmem_node(pgdat, addr, last-addr);
148 }
149}
150
151/*
152 * Find the highest page frame number we have available
153 */
154unsigned long __init e820_end_of_ram(void)
155{
156 int i;
157 unsigned long end_pfn = 0;
158
159 for (i = 0; i < e820.nr_map; i++) {
160 struct e820entry *ei = &e820.map[i];
161 unsigned long start, end;
162
163 start = round_up(ei->addr, PAGE_SIZE);
164 end = round_down(ei->addr + ei->size, PAGE_SIZE);
165 if (start >= end)
166 continue;
167 if (ei->type == E820_RAM) {
168 if (end > end_pfn<<PAGE_SHIFT)
169 end_pfn = end>>PAGE_SHIFT;
170 } else {
171 if (end > end_pfn_map<<PAGE_SHIFT)
172 end_pfn_map = end>>PAGE_SHIFT;
173 }
174 }
175
176 if (end_pfn > end_pfn_map)
177 end_pfn_map = end_pfn;
178 if (end_pfn_map > MAXMEM>>PAGE_SHIFT)
179 end_pfn_map = MAXMEM>>PAGE_SHIFT;
180 if (end_pfn > end_user_pfn)
181 end_pfn = end_user_pfn;
182 if (end_pfn > end_pfn_map)
183 end_pfn = end_pfn_map;
184
185 return end_pfn;
186}
187
188/*
Andi Kleen485761b2005-08-26 18:34:10 -0700189 * Compute how much memory is missing in a range.
190 * Unlike the other functions in this file the arguments are in page numbers.
191 */
192unsigned long __init
193e820_hole_size(unsigned long start_pfn, unsigned long end_pfn)
194{
195 unsigned long ram = 0;
196 unsigned long start = start_pfn << PAGE_SHIFT;
197 unsigned long end = end_pfn << PAGE_SHIFT;
198 int i;
199 for (i = 0; i < e820.nr_map; i++) {
200 struct e820entry *ei = &e820.map[i];
201 unsigned long last, addr;
202
203 if (ei->type != E820_RAM ||
204 ei->addr+ei->size <= start ||
205 ei->addr >= end)
206 continue;
207
208 addr = round_up(ei->addr, PAGE_SIZE);
209 if (addr < start)
210 addr = start;
211
212 last = round_down(ei->addr + ei->size, PAGE_SIZE);
213 if (last >= end)
214 last = end;
215
216 if (last > addr)
217 ram += last - addr;
218 }
219 return ((end - start) - ram) >> PAGE_SHIFT;
220}
221
222/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 * Mark e820 reserved areas as busy for the resource manager.
224 */
225void __init e820_reserve_resources(void)
226{
227 int i;
228 for (i = 0; i < e820.nr_map; i++) {
229 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 res = alloc_bootmem_low(sizeof(struct resource));
231 switch (e820.map[i].type) {
232 case E820_RAM: res->name = "System RAM"; break;
233 case E820_ACPI: res->name = "ACPI Tables"; break;
234 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
235 default: res->name = "reserved";
236 }
237 res->start = e820.map[i].addr;
238 res->end = res->start + e820.map[i].size - 1;
239 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
240 request_resource(&iomem_resource, res);
241 if (e820.map[i].type == E820_RAM) {
242 /*
243 * We don't know which RAM region contains kernel data,
244 * so we try it repeatedly and let the resource manager
245 * test it.
246 */
247 request_resource(res, &code_resource);
248 request_resource(res, &data_resource);
Eric W. Biederman5f5609d2005-06-25 14:58:04 -0700249#ifdef CONFIG_KEXEC
250 request_resource(res, &crashk_res);
251#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253 }
254}
255
256/*
257 * Add a memory region to the kernel e820 map.
258 */
259void __init add_memory_region(unsigned long start, unsigned long size, int type)
260{
261 int x = e820.nr_map;
262
263 if (x == E820MAX) {
264 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
265 return;
266 }
267
268 e820.map[x].addr = start;
269 e820.map[x].size = size;
270 e820.map[x].type = type;
271 e820.nr_map++;
272}
273
274void __init e820_print_map(char *who)
275{
276 int i;
277
278 for (i = 0; i < e820.nr_map; i++) {
279 printk(" %s: %016Lx - %016Lx ", who,
280 (unsigned long long) e820.map[i].addr,
281 (unsigned long long) (e820.map[i].addr + e820.map[i].size));
282 switch (e820.map[i].type) {
283 case E820_RAM: printk("(usable)\n");
284 break;
285 case E820_RESERVED:
286 printk("(reserved)\n");
287 break;
288 case E820_ACPI:
289 printk("(ACPI data)\n");
290 break;
291 case E820_NVS:
292 printk("(ACPI NVS)\n");
293 break;
294 default: printk("type %u\n", e820.map[i].type);
295 break;
296 }
297 }
298}
299
300/*
301 * Sanitize the BIOS e820 map.
302 *
303 * Some e820 responses include overlapping entries. The following
304 * replaces the original e820 map with a new one, removing overlaps.
305 *
306 */
307static int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
308{
309 struct change_member {
310 struct e820entry *pbios; /* pointer to original bios entry */
311 unsigned long long addr; /* address for this change point */
312 };
313 static struct change_member change_point_list[2*E820MAX] __initdata;
314 static struct change_member *change_point[2*E820MAX] __initdata;
315 static struct e820entry *overlap_list[E820MAX] __initdata;
316 static struct e820entry new_bios[E820MAX] __initdata;
317 struct change_member *change_tmp;
318 unsigned long current_type, last_type;
319 unsigned long long last_addr;
320 int chgidx, still_changing;
321 int overlap_entries;
322 int new_bios_entry;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700323 int old_nr, new_nr, chg_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 int i;
325
326 /*
327 Visually we're performing the following (1,2,3,4 = memory types)...
328
329 Sample memory map (w/overlaps):
330 ____22__________________
331 ______________________4_
332 ____1111________________
333 _44_____________________
334 11111111________________
335 ____________________33__
336 ___________44___________
337 __________33333_________
338 ______________22________
339 ___________________2222_
340 _________111111111______
341 _____________________11_
342 _________________4______
343
344 Sanitized equivalent (no overlap):
345 1_______________________
346 _44_____________________
347 ___1____________________
348 ____22__________________
349 ______11________________
350 _________1______________
351 __________3_____________
352 ___________44___________
353 _____________33_________
354 _______________2________
355 ________________1_______
356 _________________4______
357 ___________________2____
358 ____________________33__
359 ______________________4_
360 */
361
362 /* if there's only one memory region, don't bother */
363 if (*pnr_map < 2)
364 return -1;
365
366 old_nr = *pnr_map;
367
368 /* bail out if we find any unreasonable addresses in bios map */
369 for (i=0; i<old_nr; i++)
370 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
371 return -1;
372
373 /* create pointers for initial change-point information (for sorting) */
374 for (i=0; i < 2*old_nr; i++)
375 change_point[i] = &change_point_list[i];
376
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700377 /* record all known change-points (starting and ending addresses),
378 omitting those that are for empty memory regions */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 chgidx = 0;
380 for (i=0; i < old_nr; i++) {
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700381 if (biosmap[i].size != 0) {
382 change_point[chgidx]->addr = biosmap[i].addr;
383 change_point[chgidx++]->pbios = &biosmap[i];
384 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
385 change_point[chgidx++]->pbios = &biosmap[i];
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700388 chg_nr = chgidx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390 /* sort change-point list by memory addresses (low -> high) */
391 still_changing = 1;
392 while (still_changing) {
393 still_changing = 0;
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700394 for (i=1; i < chg_nr; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* if <current_addr> > <last_addr>, swap */
396 /* or, if current=<start_addr> & last=<end_addr>, swap */
397 if ((change_point[i]->addr < change_point[i-1]->addr) ||
398 ((change_point[i]->addr == change_point[i-1]->addr) &&
399 (change_point[i]->addr == change_point[i]->pbios->addr) &&
400 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
401 )
402 {
403 change_tmp = change_point[i];
404 change_point[i] = change_point[i-1];
405 change_point[i-1] = change_tmp;
406 still_changing=1;
407 }
408 }
409 }
410
411 /* create a new bios memory map, removing overlaps */
412 overlap_entries=0; /* number of entries in the overlap table */
413 new_bios_entry=0; /* index for creating new bios map entries */
414 last_type = 0; /* start with undefined memory type */
415 last_addr = 0; /* start with 0 as last starting address */
416 /* loop through change-points, determining affect on the new bios map */
Venkatesh Pallipadi8059b2a2005-05-01 08:58:52 -0700417 for (chgidx=0; chgidx < chg_nr; chgidx++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 {
419 /* keep track of all overlapping bios entries */
420 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
421 {
422 /* add map entry to overlap list (> 1 entry implies an overlap) */
423 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
424 }
425 else
426 {
427 /* remove entry from list (order independent, so swap with last) */
428 for (i=0; i<overlap_entries; i++)
429 {
430 if (overlap_list[i] == change_point[chgidx]->pbios)
431 overlap_list[i] = overlap_list[overlap_entries-1];
432 }
433 overlap_entries--;
434 }
435 /* if there are overlapping entries, decide which "type" to use */
436 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
437 current_type = 0;
438 for (i=0; i<overlap_entries; i++)
439 if (overlap_list[i]->type > current_type)
440 current_type = overlap_list[i]->type;
441 /* continue building up new bios map based on this information */
442 if (current_type != last_type) {
443 if (last_type != 0) {
444 new_bios[new_bios_entry].size =
445 change_point[chgidx]->addr - last_addr;
446 /* move forward only if the new size was non-zero */
447 if (new_bios[new_bios_entry].size != 0)
448 if (++new_bios_entry >= E820MAX)
449 break; /* no more space left for new bios entries */
450 }
451 if (current_type != 0) {
452 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
453 new_bios[new_bios_entry].type = current_type;
454 last_addr=change_point[chgidx]->addr;
455 }
456 last_type = current_type;
457 }
458 }
459 new_nr = new_bios_entry; /* retain count for new bios entries */
460
461 /* copy new bios mapping into original location */
462 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
463 *pnr_map = new_nr;
464
465 return 0;
466}
467
468/*
469 * Copy the BIOS e820 map into a safe place.
470 *
471 * Sanity-check it while we're at it..
472 *
473 * If we're lucky and live on a modern system, the setup code
474 * will have given us a memory map that we can use to properly
475 * set up memory. If we aren't, we'll fake a memory map.
476 *
477 * We check to see that the memory map contains at least 2 elements
478 * before we'll use it, because the detection code in setup.S may
479 * not be perfect and most every PC known to man has two memory
480 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
481 * thinkpad 560x, for example, does not cooperate with the memory
482 * detection code.)
483 */
484static int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
485{
486 /* Only one memory region (or negative)? Ignore it */
487 if (nr_map < 2)
488 return -1;
489
490 do {
491 unsigned long start = biosmap->addr;
492 unsigned long size = biosmap->size;
493 unsigned long end = start + size;
494 unsigned long type = biosmap->type;
495
496 /* Overflow in 64 bits? Ignore the memory map. */
497 if (start > end)
498 return -1;
499
500 /*
501 * Some BIOSes claim RAM in the 640k - 1M region.
502 * Not right. Fix it up.
503 *
504 * This should be removed on Hammer which is supposed to not
505 * have non e820 covered ISA mappings there, but I had some strange
506 * problems so it stays for now. -AK
507 */
508 if (type == E820_RAM) {
509 if (start < 0x100000ULL && end > 0xA0000ULL) {
510 if (start < 0xA0000ULL)
511 add_memory_region(start, 0xA0000ULL-start, type);
512 if (end <= 0x100000ULL)
513 continue;
514 start = 0x100000ULL;
515 size = end - start;
516 }
517 }
518
519 add_memory_region(start, size, type);
520 } while (biosmap++,--nr_map);
521 return 0;
522}
523
524void __init setup_memory_region(void)
525{
526 char *who = "BIOS-e820";
527
528 /*
529 * Try to copy the BIOS-supplied E820-map.
530 *
531 * Otherwise fake a memory map; one section from 0k->640k,
532 * the next section from 1mb->appropriate_mem_k
533 */
534 sanitize_e820_map(E820_MAP, &E820_MAP_NR);
535 if (copy_e820_map(E820_MAP, E820_MAP_NR) < 0) {
536 unsigned long mem_size;
537
538 /* compare results from other methods and take the greater */
539 if (ALT_MEM_K < EXT_MEM_K) {
540 mem_size = EXT_MEM_K;
541 who = "BIOS-88";
542 } else {
543 mem_size = ALT_MEM_K;
544 who = "BIOS-e801";
545 }
546
547 e820.nr_map = 0;
548 add_memory_region(0, LOWMEMSIZE(), E820_RAM);
549 add_memory_region(HIGH_MEMORY, mem_size << 10, E820_RAM);
550 }
551 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
552 e820_print_map(who);
553}
554
555void __init parse_memopt(char *p, char **from)
556{
557 end_user_pfn = memparse(p, from);
558 end_user_pfn >>= PAGE_SHIFT;
559}
560
Andi Kleena1e97782005-04-16 15:25:12 -0700561unsigned long pci_mem_start = 0xaeedbabe;
562
563/*
564 * Search for the biggest gap in the low 32 bits of the e820
565 * memory space. We pass this space to PCI to assign MMIO resources
566 * for hotplug or unconfigured devices in.
567 * Hopefully the BIOS let enough space left.
568 */
569__init void e820_setup_gap(void)
570{
Daniel Ritzf0eca962005-09-09 00:57:14 +0200571 unsigned long gapstart, gapsize, round;
Andi Kleena1e97782005-04-16 15:25:12 -0700572 unsigned long last;
573 int i;
574 int found = 0;
575
576 last = 0x100000000ull;
577 gapstart = 0x10000000;
578 gapsize = 0x400000;
579 i = e820.nr_map;
580 while (--i >= 0) {
581 unsigned long long start = e820.map[i].addr;
582 unsigned long long end = start + e820.map[i].size;
583
584 /*
585 * Since "last" is at most 4GB, we know we'll
586 * fit in 32 bits if this condition is true
587 */
588 if (last > end) {
589 unsigned long gap = last - end;
590
591 if (gap > gapsize) {
592 gapsize = gap;
593 gapstart = end;
594 found = 1;
595 }
596 }
597 if (start < last)
598 last = start;
599 }
600
601 if (!found) {
602 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
603 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit address range\n"
604 KERN_ERR "PCI: Unassigned devices with 32bit resource registers may break!\n");
605 }
606
607 /*
Daniel Ritzf0eca962005-09-09 00:57:14 +0200608 * See how much we want to round up: start off with
609 * rounding to the next 1MB area.
Andi Kleena1e97782005-04-16 15:25:12 -0700610 */
Daniel Ritzf0eca962005-09-09 00:57:14 +0200611 round = 0x100000;
612 while ((gapsize >> 4) > round)
613 round += round;
614 /* Fun with two's complement */
615 pci_mem_start = (gapstart + round) & -round;
Andi Kleena1e97782005-04-16 15:25:12 -0700616
617 printk(KERN_INFO "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
618 pci_mem_start, gapstart, gapsize);
619}