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 $
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>
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>
21 #include <asm/proto.h>
22 #include <asm/bootsetup.h>
27 * PFN of last memory page.
29 unsigned long end_pfn
;
32 * end_pfn only includes RAM, while end_pfn_map includes all e820 entries.
33 * The direct mapping extends to end_pfn_map, so that we can directly access
34 * apertures, ACPI and other tables without having to play with fixmaps.
36 unsigned long end_pfn_map
;
39 * Last pfn which the user wants to use.
41 unsigned long end_user_pfn
= MAXMEM
>>PAGE_SHIFT
;
43 extern struct resource code_resource
, data_resource
;
45 /* Check for some hardcoded bad areas that early boot is not allowed to touch */
46 static inline int bad_addr(unsigned long *addrp
, unsigned long size
)
48 unsigned long addr
= *addrp
, last
= addr
+ size
;
50 /* various gunk below that needed for SMP startup */
56 /* direct mapping tables of the kernel */
57 if (last
>= table_start
<<PAGE_SHIFT
&& addr
< table_end
<<PAGE_SHIFT
) {
58 *addrp
= table_end
<< PAGE_SHIFT
;
63 #ifdef CONFIG_BLK_DEV_INITRD
64 if (LOADER_TYPE
&& INITRD_START
&& last
>= INITRD_START
&&
65 addr
< INITRD_START
+INITRD_SIZE
) {
66 *addrp
= INITRD_START
+ INITRD_SIZE
;
70 /* kernel code + 640k memory hole (later should not be needed, but
71 be paranoid for now) */
72 if (last
>= 640*1024 && addr
< __pa_symbol(&_end
)) {
73 *addrp
= __pa_symbol(&_end
);
76 /* XXX ramdisk image here? */
80 int __init
e820_mapped(unsigned long start
, unsigned long end
, unsigned type
)
83 for (i
= 0; i
< e820
.nr_map
; i
++) {
84 struct e820entry
*ei
= &e820
.map
[i
];
85 if (type
&& ei
->type
!= type
)
87 if (ei
->addr
>= end
|| ei
->addr
+ ei
->size
< start
)
95 * Find a free area in a specific range.
97 unsigned long __init
find_e820_area(unsigned long start
, unsigned long end
, unsigned size
)
100 for (i
= 0; i
< e820
.nr_map
; i
++) {
101 struct e820entry
*ei
= &e820
.map
[i
];
102 unsigned long addr
= ei
->addr
, last
;
103 if (ei
->type
!= E820_RAM
)
107 if (addr
> ei
->addr
+ ei
->size
)
109 while (bad_addr(&addr
, size
) && addr
+size
< ei
->addr
+ ei
->size
)
112 if (last
> ei
->addr
+ ei
->size
)
122 * Free bootmem based on the e820 table for a node.
124 void __init
e820_bootmem_free(pg_data_t
*pgdat
, unsigned long start
,unsigned long end
)
127 for (i
= 0; i
< e820
.nr_map
; i
++) {
128 struct e820entry
*ei
= &e820
.map
[i
];
129 unsigned long last
, addr
;
131 if (ei
->type
!= E820_RAM
||
132 ei
->addr
+ei
->size
<= start
||
136 addr
= round_up(ei
->addr
, PAGE_SIZE
);
140 last
= round_down(ei
->addr
+ ei
->size
, PAGE_SIZE
);
144 if (last
> addr
&& last
-addr
>= PAGE_SIZE
)
145 free_bootmem_node(pgdat
, addr
, last
-addr
);
150 * Find the highest page frame number we have available
152 unsigned long __init
e820_end_of_ram(void)
155 unsigned long end_pfn
= 0;
157 for (i
= 0; i
< e820
.nr_map
; i
++) {
158 struct e820entry
*ei
= &e820
.map
[i
];
159 unsigned long start
, end
;
161 start
= round_up(ei
->addr
, PAGE_SIZE
);
162 end
= round_down(ei
->addr
+ ei
->size
, PAGE_SIZE
);
165 if (ei
->type
== E820_RAM
) {
166 if (end
> end_pfn
<<PAGE_SHIFT
)
167 end_pfn
= end
>>PAGE_SHIFT
;
169 if (end
> end_pfn_map
<<PAGE_SHIFT
)
170 end_pfn_map
= end
>>PAGE_SHIFT
;
174 if (end_pfn
> end_pfn_map
)
175 end_pfn_map
= end_pfn
;
176 if (end_pfn_map
> MAXMEM
>>PAGE_SHIFT
)
177 end_pfn_map
= MAXMEM
>>PAGE_SHIFT
;
178 if (end_pfn
> end_user_pfn
)
179 end_pfn
= end_user_pfn
;
180 if (end_pfn
> end_pfn_map
)
181 end_pfn
= end_pfn_map
;
187 * Mark e820 reserved areas as busy for the resource manager.
189 void __init
e820_reserve_resources(void)
192 for (i
= 0; i
< e820
.nr_map
; i
++) {
193 struct resource
*res
;
194 if (e820
.map
[i
].addr
+ e820
.map
[i
].size
> 0x100000000ULL
)
196 res
= alloc_bootmem_low(sizeof(struct resource
));
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";
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 request_resource(&iomem_resource
, res
);
207 if (e820
.map
[i
].type
== E820_RAM
) {
209 * We don't know which RAM region contains kernel data,
210 * so we try it repeatedly and let the resource manager
213 request_resource(res
, &code_resource
);
214 request_resource(res
, &data_resource
);
220 * Add a memory region to the kernel e820 map.
222 void __init
add_memory_region(unsigned long start
, unsigned long size
, int type
)
227 printk(KERN_ERR
"Ooops! Too many entries in the memory map!\n");
231 e820
.map
[x
].addr
= start
;
232 e820
.map
[x
].size
= size
;
233 e820
.map
[x
].type
= type
;
237 void __init
e820_print_map(char *who
)
241 for (i
= 0; i
< e820
.nr_map
; i
++) {
242 printk(" %s: %016Lx - %016Lx ", who
,
243 (unsigned long long) e820
.map
[i
].addr
,
244 (unsigned long long) (e820
.map
[i
].addr
+ e820
.map
[i
].size
));
245 switch (e820
.map
[i
].type
) {
246 case E820_RAM
: printk("(usable)\n");
249 printk("(reserved)\n");
252 printk("(ACPI data)\n");
255 printk("(ACPI NVS)\n");
257 default: printk("type %u\n", e820
.map
[i
].type
);
264 * Sanitize the BIOS e820 map.
266 * Some e820 responses include overlapping entries. The following
267 * replaces the original e820 map with a new one, removing overlaps.
270 static int __init
sanitize_e820_map(struct e820entry
* biosmap
, char * pnr_map
)
272 struct change_member
{
273 struct e820entry
*pbios
; /* pointer to original bios entry */
274 unsigned long long addr
; /* address for this change point */
276 static struct change_member change_point_list
[2*E820MAX
] __initdata
;
277 static struct change_member
*change_point
[2*E820MAX
] __initdata
;
278 static struct e820entry
*overlap_list
[E820MAX
] __initdata
;
279 static struct e820entry new_bios
[E820MAX
] __initdata
;
280 struct change_member
*change_tmp
;
281 unsigned long current_type
, last_type
;
282 unsigned long long last_addr
;
283 int chgidx
, still_changing
;
286 int old_nr
, new_nr
, chg_nr
;
290 Visually we're performing the following (1,2,3,4 = memory types)...
292 Sample memory map (w/overlaps):
293 ____22__________________
294 ______________________4_
295 ____1111________________
296 _44_____________________
297 11111111________________
298 ____________________33__
299 ___________44___________
300 __________33333_________
301 ______________22________
302 ___________________2222_
303 _________111111111______
304 _____________________11_
305 _________________4______
307 Sanitized equivalent (no overlap):
308 1_______________________
309 _44_____________________
310 ___1____________________
311 ____22__________________
312 ______11________________
313 _________1______________
314 __________3_____________
315 ___________44___________
316 _____________33_________
317 _______________2________
318 ________________1_______
319 _________________4______
320 ___________________2____
321 ____________________33__
322 ______________________4_
325 /* if there's only one memory region, don't bother */
331 /* bail out if we find any unreasonable addresses in bios map */
332 for (i
=0; i
<old_nr
; i
++)
333 if (biosmap
[i
].addr
+ biosmap
[i
].size
< biosmap
[i
].addr
)
336 /* create pointers for initial change-point information (for sorting) */
337 for (i
=0; i
< 2*old_nr
; i
++)
338 change_point
[i
] = &change_point_list
[i
];
340 /* record all known change-points (starting and ending addresses),
341 omitting those that are for empty memory regions */
343 for (i
=0; i
< old_nr
; i
++) {
344 if (biosmap
[i
].size
!= 0) {
345 change_point
[chgidx
]->addr
= biosmap
[i
].addr
;
346 change_point
[chgidx
++]->pbios
= &biosmap
[i
];
347 change_point
[chgidx
]->addr
= biosmap
[i
].addr
+ biosmap
[i
].size
;
348 change_point
[chgidx
++]->pbios
= &biosmap
[i
];
353 /* sort change-point list by memory addresses (low -> high) */
355 while (still_changing
) {
357 for (i
=1; i
< chg_nr
; i
++) {
358 /* if <current_addr> > <last_addr>, swap */
359 /* or, if current=<start_addr> & last=<end_addr>, swap */
360 if ((change_point
[i
]->addr
< change_point
[i
-1]->addr
) ||
361 ((change_point
[i
]->addr
== change_point
[i
-1]->addr
) &&
362 (change_point
[i
]->addr
== change_point
[i
]->pbios
->addr
) &&
363 (change_point
[i
-1]->addr
!= change_point
[i
-1]->pbios
->addr
))
366 change_tmp
= change_point
[i
];
367 change_point
[i
] = change_point
[i
-1];
368 change_point
[i
-1] = change_tmp
;
374 /* create a new bios memory map, removing overlaps */
375 overlap_entries
=0; /* number of entries in the overlap table */
376 new_bios_entry
=0; /* index for creating new bios map entries */
377 last_type
= 0; /* start with undefined memory type */
378 last_addr
= 0; /* start with 0 as last starting address */
379 /* loop through change-points, determining affect on the new bios map */
380 for (chgidx
=0; chgidx
< chg_nr
; chgidx
++)
382 /* keep track of all overlapping bios entries */
383 if (change_point
[chgidx
]->addr
== change_point
[chgidx
]->pbios
->addr
)
385 /* add map entry to overlap list (> 1 entry implies an overlap) */
386 overlap_list
[overlap_entries
++]=change_point
[chgidx
]->pbios
;
390 /* remove entry from list (order independent, so swap with last) */
391 for (i
=0; i
<overlap_entries
; i
++)
393 if (overlap_list
[i
] == change_point
[chgidx
]->pbios
)
394 overlap_list
[i
] = overlap_list
[overlap_entries
-1];
398 /* if there are overlapping entries, decide which "type" to use */
399 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
401 for (i
=0; i
<overlap_entries
; i
++)
402 if (overlap_list
[i
]->type
> current_type
)
403 current_type
= overlap_list
[i
]->type
;
404 /* continue building up new bios map based on this information */
405 if (current_type
!= last_type
) {
406 if (last_type
!= 0) {
407 new_bios
[new_bios_entry
].size
=
408 change_point
[chgidx
]->addr
- last_addr
;
409 /* move forward only if the new size was non-zero */
410 if (new_bios
[new_bios_entry
].size
!= 0)
411 if (++new_bios_entry
>= E820MAX
)
412 break; /* no more space left for new bios entries */
414 if (current_type
!= 0) {
415 new_bios
[new_bios_entry
].addr
= change_point
[chgidx
]->addr
;
416 new_bios
[new_bios_entry
].type
= current_type
;
417 last_addr
=change_point
[chgidx
]->addr
;
419 last_type
= current_type
;
422 new_nr
= new_bios_entry
; /* retain count for new bios entries */
424 /* copy new bios mapping into original location */
425 memcpy(biosmap
, new_bios
, new_nr
*sizeof(struct e820entry
));
432 * Copy the BIOS e820 map into a safe place.
434 * Sanity-check it while we're at it..
436 * If we're lucky and live on a modern system, the setup code
437 * will have given us a memory map that we can use to properly
438 * set up memory. If we aren't, we'll fake a memory map.
440 * We check to see that the memory map contains at least 2 elements
441 * before we'll use it, because the detection code in setup.S may
442 * not be perfect and most every PC known to man has two memory
443 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
444 * thinkpad 560x, for example, does not cooperate with the memory
447 static int __init
copy_e820_map(struct e820entry
* biosmap
, int nr_map
)
449 /* Only one memory region (or negative)? Ignore it */
454 unsigned long start
= biosmap
->addr
;
455 unsigned long size
= biosmap
->size
;
456 unsigned long end
= start
+ size
;
457 unsigned long type
= biosmap
->type
;
459 /* Overflow in 64 bits? Ignore the memory map. */
464 * Some BIOSes claim RAM in the 640k - 1M region.
465 * Not right. Fix it up.
467 * This should be removed on Hammer which is supposed to not
468 * have non e820 covered ISA mappings there, but I had some strange
469 * problems so it stays for now. -AK
471 if (type
== E820_RAM
) {
472 if (start
< 0x100000ULL
&& end
> 0xA0000ULL
) {
473 if (start
< 0xA0000ULL
)
474 add_memory_region(start
, 0xA0000ULL
-start
, type
);
475 if (end
<= 0x100000ULL
)
482 add_memory_region(start
, size
, type
);
483 } while (biosmap
++,--nr_map
);
487 void __init
setup_memory_region(void)
489 char *who
= "BIOS-e820";
492 * Try to copy the BIOS-supplied E820-map.
494 * Otherwise fake a memory map; one section from 0k->640k,
495 * the next section from 1mb->appropriate_mem_k
497 sanitize_e820_map(E820_MAP
, &E820_MAP_NR
);
498 if (copy_e820_map(E820_MAP
, E820_MAP_NR
) < 0) {
499 unsigned long mem_size
;
501 /* compare results from other methods and take the greater */
502 if (ALT_MEM_K
< EXT_MEM_K
) {
503 mem_size
= EXT_MEM_K
;
506 mem_size
= ALT_MEM_K
;
511 add_memory_region(0, LOWMEMSIZE(), E820_RAM
);
512 add_memory_region(HIGH_MEMORY
, mem_size
<< 10, E820_RAM
);
514 printk(KERN_INFO
"BIOS-provided physical RAM map:\n");
518 void __init
parse_memopt(char *p
, char **from
)
520 end_user_pfn
= memparse(p
, from
);
521 end_user_pfn
>>= PAGE_SHIFT
;
524 unsigned long pci_mem_start
= 0xaeedbabe;
527 * Search for the biggest gap in the low 32 bits of the e820
528 * memory space. We pass this space to PCI to assign MMIO resources
529 * for hotplug or unconfigured devices in.
530 * Hopefully the BIOS let enough space left.
532 __init
void e820_setup_gap(void)
534 unsigned long gapstart
, gapsize
;
539 last
= 0x100000000ull
;
540 gapstart
= 0x10000000;
544 unsigned long long start
= e820
.map
[i
].addr
;
545 unsigned long long end
= start
+ e820
.map
[i
].size
;
548 * Since "last" is at most 4GB, we know we'll
549 * fit in 32 bits if this condition is true
552 unsigned long gap
= last
- end
;
565 gapstart
= (end_pfn
<< PAGE_SHIFT
) + 1024*1024;
566 printk(KERN_ERR
"PCI: Warning: Cannot find a gap in the 32bit address range\n"
567 KERN_ERR
"PCI: Unassigned devices with 32bit resource registers may break!\n");
571 * Start allocating dynamic PCI memory a bit into the gap,
572 * aligned up to the nearest megabyte.
574 * Question: should we try to pad it up a bit (do something
575 * like " + (gapsize >> 3)" in there too?). We now have the
578 pci_mem_start
= (gapstart
+ 0xfffff) & ~0xfffff;
580 printk(KERN_INFO
"Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
581 pci_mem_start
, gapstart
, gapsize
);