2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
5 * Getting sanitize_e820_map() in sync with i386 version by applying change:
6 * - Provisions for empty E820 memory regions (reported by certain BIOSes).
7 * Alex Achenbach <xela@slit.de>, December 2002.
8 * Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
11 #include <linux/kernel.h>
12 #include <linux/types.h>
13 #include <linux/init.h>
14 #include <linux/bootmem.h>
15 #include <linux/ioport.h>
16 #include <linux/string.h>
17 #include <linux/kexec.h>
18 #include <linux/module.h>
20 #include <linux/pfn.h>
22 #include <asm/pgtable.h>
25 #include <asm/setup.h>
29 /* For PCI or other memory-mapped resources */
30 unsigned long pci_mem_start = 0xaeedbabe;
32 EXPORT_SYMBOL(pci_mem_start);
36 * This function checks if any part of the range <start,end> is mapped
40 e820_any_mapped(u64 start, u64 end, unsigned type)
44 for (i = 0; i < e820.nr_map; i++) {
45 struct e820entry *ei = &e820.map[i];
47 if (type && ei->type != type)
49 if (ei->addr >= end || ei->addr + ei->size <= start)
55 EXPORT_SYMBOL_GPL(e820_any_mapped);
58 * This function checks if the entire range <start,end> is mapped with type.
60 * Note: this function only works correct if the e820 table is sorted and
61 * not-overlapping, which is the case
63 int __init e820_all_mapped(u64 start, u64 end, unsigned type)
67 for (i = 0; i < e820.nr_map; i++) {
68 struct e820entry *ei = &e820.map[i];
70 if (type && ei->type != type)
72 /* is the region (part) in overlap with the current region ?*/
73 if (ei->addr >= end || ei->addr + ei->size <= start)
76 /* if the region is at the beginning of <start,end> we move
77 * start to the end of the region since it's ok until there
79 if (ei->addr <= start)
80 start = ei->addr + ei->size;
82 * if start is now at or beyond end, we're done, full
92 * Add a memory region to the kernel e820 map.
94 void __init add_memory_region(u64 start, u64 size, int type)
98 if (x == ARRAY_SIZE(e820.map)) {
99 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
103 e820.map[x].addr = start;
104 e820.map[x].size = size;
105 e820.map[x].type = type;
109 void __init e820_print_map(char *who)
113 for (i = 0; i < e820.nr_map; i++) {
114 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
115 (unsigned long long) e820.map[i].addr,
117 (e820.map[i].addr + e820.map[i].size));
118 switch (e820.map[i].type) {
120 printk(KERN_CONT "(usable)\n");
123 printk(KERN_CONT "(reserved)\n");
126 printk(KERN_CONT "(ACPI data)\n");
129 printk(KERN_CONT "(ACPI NVS)\n");
132 printk(KERN_CONT "type %u\n", e820.map[i].type);
139 * Sanitize the BIOS e820 map.
141 * Some e820 responses include overlapping entries. The following
142 * replaces the original e820 map with a new one, removing overlaps.
145 int __init sanitize_e820_map(struct e820entry *biosmap, int max_nr_map,
148 struct change_member {
149 struct e820entry *pbios; /* pointer to original bios entry */
150 unsigned long long addr; /* address for this change point */
152 static struct change_member change_point_list[2*E820_X_MAX] __initdata;
153 static struct change_member *change_point[2*E820_X_MAX] __initdata;
154 static struct e820entry *overlap_list[E820_X_MAX] __initdata;
155 static struct e820entry new_bios[E820_X_MAX] __initdata;
156 struct change_member *change_tmp;
157 unsigned long current_type, last_type;
158 unsigned long long last_addr;
159 int chgidx, still_changing;
162 int old_nr, new_nr, chg_nr;
166 Visually we're performing the following
167 (1,2,3,4 = memory types)...
169 Sample memory map (w/overlaps):
170 ____22__________________
171 ______________________4_
172 ____1111________________
173 _44_____________________
174 11111111________________
175 ____________________33__
176 ___________44___________
177 __________33333_________
178 ______________22________
179 ___________________2222_
180 _________111111111______
181 _____________________11_
182 _________________4______
184 Sanitized equivalent (no overlap):
185 1_______________________
186 _44_____________________
187 ___1____________________
188 ____22__________________
189 ______11________________
190 _________1______________
191 __________3_____________
192 ___________44___________
193 _____________33_________
194 _______________2________
195 ________________1_______
196 _________________4______
197 ___________________2____
198 ____________________33__
199 ______________________4_
202 /* if there's only one memory region, don't bother */
207 BUG_ON(old_nr > max_nr_map);
209 /* bail out if we find any unreasonable addresses in bios map */
210 for (i = 0; i < old_nr; i++)
211 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
214 /* create pointers for initial change-point information (for sorting) */
215 for (i = 0; i < 2 * old_nr; i++)
216 change_point[i] = &change_point_list[i];
218 /* record all known change-points (starting and ending addresses),
219 omitting those that are for empty memory regions */
221 for (i = 0; i < old_nr; i++) {
222 if (biosmap[i].size != 0) {
223 change_point[chgidx]->addr = biosmap[i].addr;
224 change_point[chgidx++]->pbios = &biosmap[i];
225 change_point[chgidx]->addr = biosmap[i].addr +
227 change_point[chgidx++]->pbios = &biosmap[i];
232 /* sort change-point list by memory addresses (low -> high) */
234 while (still_changing) {
236 for (i = 1; i < chg_nr; i++) {
237 unsigned long long curaddr, lastaddr;
238 unsigned long long curpbaddr, lastpbaddr;
240 curaddr = change_point[i]->addr;
241 lastaddr = change_point[i - 1]->addr;
242 curpbaddr = change_point[i]->pbios->addr;
243 lastpbaddr = change_point[i - 1]->pbios->addr;
246 * swap entries, when:
248 * curaddr > lastaddr or
249 * curaddr == lastaddr and curaddr == curpbaddr and
250 * lastaddr != lastpbaddr
252 if (curaddr < lastaddr ||
253 (curaddr == lastaddr && curaddr == curpbaddr &&
254 lastaddr != lastpbaddr)) {
255 change_tmp = change_point[i];
256 change_point[i] = change_point[i-1];
257 change_point[i-1] = change_tmp;
263 /* create a new bios memory map, removing overlaps */
264 overlap_entries = 0; /* number of entries in the overlap table */
265 new_bios_entry = 0; /* index for creating new bios map entries */
266 last_type = 0; /* start with undefined memory type */
267 last_addr = 0; /* start with 0 as last starting address */
269 /* loop through change-points, determining affect on the new bios map */
270 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
271 /* keep track of all overlapping bios entries */
272 if (change_point[chgidx]->addr ==
273 change_point[chgidx]->pbios->addr) {
275 * add map entry to overlap list (> 1 entry
276 * implies an overlap)
278 overlap_list[overlap_entries++] =
279 change_point[chgidx]->pbios;
282 * remove entry from list (order independent,
285 for (i = 0; i < overlap_entries; i++) {
286 if (overlap_list[i] ==
287 change_point[chgidx]->pbios)
289 overlap_list[overlap_entries-1];
294 * if there are overlapping entries, decide which
295 * "type" to use (larger value takes precedence --
296 * 1=usable, 2,3,4,4+=unusable)
299 for (i = 0; i < overlap_entries; i++)
300 if (overlap_list[i]->type > current_type)
301 current_type = overlap_list[i]->type;
303 * continue building up new bios map based on this
306 if (current_type != last_type) {
307 if (last_type != 0) {
308 new_bios[new_bios_entry].size =
309 change_point[chgidx]->addr - last_addr;
311 * move forward only if the new size
314 if (new_bios[new_bios_entry].size != 0)
316 * no more space left for new
319 if (++new_bios_entry >= max_nr_map)
322 if (current_type != 0) {
323 new_bios[new_bios_entry].addr =
324 change_point[chgidx]->addr;
325 new_bios[new_bios_entry].type = current_type;
326 last_addr = change_point[chgidx]->addr;
328 last_type = current_type;
331 /* retain count for new bios entries */
332 new_nr = new_bios_entry;
334 /* copy new bios mapping into original location */
335 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
342 * Copy the BIOS e820 map into a safe place.
344 * Sanity-check it while we're at it..
346 * If we're lucky and live on a modern system, the setup code
347 * will have given us a memory map that we can use to properly
348 * set up memory. If we aren't, we'll fake a memory map.
350 int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
352 /* Only one memory region (or negative)? Ignore it */
357 u64 start = biosmap->addr;
358 u64 size = biosmap->size;
359 u64 end = start + size;
360 u32 type = biosmap->type;
362 /* Overflow in 64 bits? Ignore the memory map. */
366 add_memory_region(start, size, type);
367 } while (biosmap++, --nr_map);
371 u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
375 u64 real_updated_size = 0;
377 BUG_ON(old_type == new_type);
379 for (i = 0; i < e820.nr_map; i++) {
380 struct e820entry *ei = &e820.map[i];
381 u64 final_start, final_end;
382 if (ei->type != old_type)
384 /* totally covered? */
385 if (ei->addr >= start &&
386 (ei->addr + ei->size) <= (start + size)) {
388 real_updated_size += ei->size;
391 /* partially covered */
392 final_start = max(start, ei->addr);
393 final_end = min(start + size, ei->addr + ei->size);
394 if (final_start >= final_end)
396 add_memory_region(final_start, final_end - final_start,
398 real_updated_size += final_end - final_start;
400 return real_updated_size;
403 void __init update_e820(void)
407 nr_map = e820.nr_map;
408 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
410 e820.nr_map = nr_map;
411 printk(KERN_INFO "modified physical RAM map:\n");
412 e820_print_map("modified");
416 * Search for the biggest gap in the low 32 bits of the e820
417 * memory space. We pass this space to PCI to assign MMIO resources
418 * for hotplug or unconfigured devices in.
419 * Hopefully the BIOS let enough space left.
421 __init void e820_setup_gap(void)
423 unsigned long gapstart, gapsize, round;
424 unsigned long long last;
428 last = 0x100000000ull;
429 gapstart = 0x10000000;
433 unsigned long long start = e820.map[i].addr;
434 unsigned long long end = start + e820.map[i].size;
437 * Since "last" is at most 4GB, we know we'll
438 * fit in 32 bits if this condition is true
441 unsigned long gap = last - end;
455 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
456 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
458 KERN_ERR "PCI: Unassigned devices with 32bit resource "
459 "registers may break!\n");
464 * See how much we want to round up: start off with
465 * rounding to the next 1MB area.
468 while ((gapsize >> 4) > round)
470 /* Fun with two's complement */
471 pci_mem_start = (gapstart + round) & -round;
474 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
475 pci_mem_start, gapstart, gapsize);