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*E820MAX] __initdata;
153 static struct change_member *change_point[2*E820MAX] __initdata;
154 static struct e820entry *overlap_list[E820MAX] __initdata;
155 static struct e820entry new_bios[E820MAX] __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 */
208 /* bail out if we find any unreasonable addresses in bios map */
209 for (i = 0; i < old_nr; i++)
210 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
213 /* create pointers for initial change-point information (for sorting) */
214 for (i = 0; i < 2 * old_nr; i++)
215 change_point[i] = &change_point_list[i];
217 /* record all known change-points (starting and ending addresses),
218 omitting those that are for empty memory regions */
220 for (i = 0; i < old_nr; i++) {
221 if (biosmap[i].size != 0) {
222 change_point[chgidx]->addr = biosmap[i].addr;
223 change_point[chgidx++]->pbios = &biosmap[i];
224 change_point[chgidx]->addr = biosmap[i].addr +
226 change_point[chgidx++]->pbios = &biosmap[i];
231 /* sort change-point list by memory addresses (low -> high) */
233 while (still_changing) {
235 for (i = 1; i < chg_nr; i++) {
236 unsigned long long curaddr, lastaddr;
237 unsigned long long curpbaddr, lastpbaddr;
239 curaddr = change_point[i]->addr;
240 lastaddr = change_point[i - 1]->addr;
241 curpbaddr = change_point[i]->pbios->addr;
242 lastpbaddr = change_point[i - 1]->pbios->addr;
245 * swap entries, when:
247 * curaddr > lastaddr or
248 * curaddr == lastaddr and curaddr == curpbaddr and
249 * lastaddr != lastpbaddr
251 if (curaddr < lastaddr ||
252 (curaddr == lastaddr && curaddr == curpbaddr &&
253 lastaddr != lastpbaddr)) {
254 change_tmp = change_point[i];
255 change_point[i] = change_point[i-1];
256 change_point[i-1] = change_tmp;
262 /* create a new bios memory map, removing overlaps */
263 overlap_entries = 0; /* number of entries in the overlap table */
264 new_bios_entry = 0; /* index for creating new bios map entries */
265 last_type = 0; /* start with undefined memory type */
266 last_addr = 0; /* start with 0 as last starting address */
268 /* loop through change-points, determining affect on the new bios map */
269 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
270 /* keep track of all overlapping bios entries */
271 if (change_point[chgidx]->addr ==
272 change_point[chgidx]->pbios->addr) {
274 * add map entry to overlap list (> 1 entry
275 * implies an overlap)
277 overlap_list[overlap_entries++] =
278 change_point[chgidx]->pbios;
281 * remove entry from list (order independent,
284 for (i = 0; i < overlap_entries; i++) {
285 if (overlap_list[i] ==
286 change_point[chgidx]->pbios)
288 overlap_list[overlap_entries-1];
293 * if there are overlapping entries, decide which
294 * "type" to use (larger value takes precedence --
295 * 1=usable, 2,3,4,4+=unusable)
298 for (i = 0; i < overlap_entries; i++)
299 if (overlap_list[i]->type > current_type)
300 current_type = overlap_list[i]->type;
302 * continue building up new bios map based on this
305 if (current_type != last_type) {
306 if (last_type != 0) {
307 new_bios[new_bios_entry].size =
308 change_point[chgidx]->addr - last_addr;
310 * move forward only if the new size
313 if (new_bios[new_bios_entry].size != 0)
315 * no more space left for new
318 if (++new_bios_entry >= max_nr_map)
321 if (current_type != 0) {
322 new_bios[new_bios_entry].addr =
323 change_point[chgidx]->addr;
324 new_bios[new_bios_entry].type = current_type;
325 last_addr = change_point[chgidx]->addr;
327 last_type = current_type;
330 /* retain count for new bios entries */
331 new_nr = new_bios_entry;
333 /* copy new bios mapping into original location */
334 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
341 * Copy the BIOS e820 map into a safe place.
343 * Sanity-check it while we're at it..
345 * If we're lucky and live on a modern system, the setup code
346 * will have given us a memory map that we can use to properly
347 * set up memory. If we aren't, we'll fake a memory map.
349 int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
351 /* Only one memory region (or negative)? Ignore it */
356 u64 start = biosmap->addr;
357 u64 size = biosmap->size;
358 u64 end = start + size;
359 u32 type = biosmap->type;
361 /* Overflow in 64 bits? Ignore the memory map. */
365 add_memory_region(start, size, type);
366 } while (biosmap++, --nr_map);
370 u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
374 u64 real_updated_size = 0;
376 BUG_ON(old_type == new_type);
378 for (i = 0; i < e820.nr_map; i++) {
379 struct e820entry *ei = &e820.map[i];
380 u64 final_start, final_end;
381 if (ei->type != old_type)
383 /* totally covered? */
384 if (ei->addr >= start &&
385 (ei->addr + ei->size) <= (start + size)) {
387 real_updated_size += ei->size;
390 /* partially covered */
391 final_start = max(start, ei->addr);
392 final_end = min(start + size, ei->addr + ei->size);
393 if (final_start >= final_end)
395 add_memory_region(final_start, final_end - final_start,
397 real_updated_size += final_end - final_start;
399 return real_updated_size;
402 void __init update_e820(void)
406 nr_map = e820.nr_map;
407 if (sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &nr_map))
409 e820.nr_map = nr_map;
410 printk(KERN_INFO "modified physical RAM map:\n");
411 e820_print_map("modified");
415 * Search for the biggest gap in the low 32 bits of the e820
416 * memory space. We pass this space to PCI to assign MMIO resources
417 * for hotplug or unconfigured devices in.
418 * Hopefully the BIOS let enough space left.
420 __init void e820_setup_gap(void)
422 unsigned long gapstart, gapsize, round;
423 unsigned long long last;
427 last = 0x100000000ull;
428 gapstart = 0x10000000;
432 unsigned long long start = e820.map[i].addr;
433 unsigned long long end = start + e820.map[i].size;
436 * Since "last" is at most 4GB, we know we'll
437 * fit in 32 bits if this condition is true
440 unsigned long gap = last - end;
454 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
455 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
457 KERN_ERR "PCI: Unassigned devices with 32bit resource "
458 "registers may break!\n");
463 * See how much we want to round up: start off with
464 * rounding to the next 1MB area.
467 while ((gapsize >> 4) > round)
469 /* Fun with two's complement */
470 pci_mem_start = (gapstart + round) & -round;
473 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
474 pci_mem_start, gapstart, gapsize);