aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/kernel
diff options
context:
space:
mode:
authorYinghai Lu <yhlu.kernel@gmail.com>2008-05-11 03:30:15 -0400
committerThomas Gleixner <tglx@linutronix.de>2008-05-25 04:55:10 -0400
commitb79cd8f1268bab57ff85b19d131f7f23deab2dee (patch)
tree9f7c90389329bf76ba5f0ced29a3dc4c6ec7680b /arch/x86/kernel
parent833e78bfeeef628f0201349a0a05a54f48f07884 (diff)
x86: make e820.c to have common functions
remove the duplicated copy of these functions. Signed-off-by: Yinghai Lu <yhlu.kernel@gmail.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
Diffstat (limited to 'arch/x86/kernel')
-rw-r--r--arch/x86/kernel/Makefile2
-rw-r--r--arch/x86/kernel/e820.c475
-rw-r--r--arch/x86/kernel/e820_32.c411
-rw-r--r--arch/x86/kernel/e820_64.c444
-rw-r--r--arch/x86/kernel/setup_32.c2
5 files changed, 482 insertions, 852 deletions
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index 5e618c3b4720..5369a4e36f17 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -22,7 +22,7 @@ obj-y += setup_$(BITS).o i8259_$(BITS).o setup.o
22obj-$(CONFIG_X86_32) += sys_i386_32.o i386_ksyms_32.o 22obj-$(CONFIG_X86_32) += sys_i386_32.o i386_ksyms_32.o
23obj-$(CONFIG_X86_64) += sys_x86_64.o x8664_ksyms_64.o 23obj-$(CONFIG_X86_64) += sys_x86_64.o x8664_ksyms_64.o
24obj-$(CONFIG_X86_64) += syscall_64.o vsyscall_64.o setup64.o 24obj-$(CONFIG_X86_64) += syscall_64.o vsyscall_64.o setup64.o
25obj-y += bootflag.o e820_$(BITS).o 25obj-y += bootflag.o e820_$(BITS).o e820.o
26obj-y += pci-dma.o quirks.o i8237.o topology.o kdebugfs.o 26obj-y += pci-dma.o quirks.o i8237.o topology.o kdebugfs.o
27obj-y += alternative.o i8253.o pci-nommu.o 27obj-y += alternative.o i8253.o pci-nommu.o
28obj-$(CONFIG_X86_64) += bugs_64.o 28obj-$(CONFIG_X86_64) += bugs_64.o
diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
new file mode 100644
index 000000000000..2cb686f60d0d
--- /dev/null
+++ b/arch/x86/kernel/e820.c
@@ -0,0 +1,475 @@
1/*
2 * Handle the memory map.
3 * The functions here do the job until bootmem takes over.
4 *
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>
9 *
10 */
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>
19#include <linux/mm.h>
20#include <linux/pfn.h>
21
22#include <asm/pgtable.h>
23#include <asm/page.h>
24#include <asm/e820.h>
25#include <asm/setup.h>
26
27struct e820map e820;
28
29/* For PCI or other memory-mapped resources */
30unsigned long pci_mem_start = 0xaeedbabe;
31#ifdef CONFIG_PCI
32EXPORT_SYMBOL(pci_mem_start);
33#endif
34
35/*
36 * This function checks if any part of the range <start,end> is mapped
37 * with type.
38 */
39int
40e820_any_mapped(u64 start, u64 end, unsigned type)
41{
42 int i;
43
44 for (i = 0; i < e820.nr_map; i++) {
45 struct e820entry *ei = &e820.map[i];
46
47 if (type && ei->type != type)
48 continue;
49 if (ei->addr >= end || ei->addr + ei->size <= start)
50 continue;
51 return 1;
52 }
53 return 0;
54}
55EXPORT_SYMBOL_GPL(e820_any_mapped);
56
57/*
58 * This function checks if the entire range <start,end> is mapped with type.
59 *
60 * Note: this function only works correct if the e820 table is sorted and
61 * not-overlapping, which is the case
62 */
63int __init e820_all_mapped(u64 start, u64 end, unsigned type)
64{
65 int i;
66
67 for (i = 0; i < e820.nr_map; i++) {
68 struct e820entry *ei = &e820.map[i];
69
70 if (type && ei->type != type)
71 continue;
72 /* is the region (part) in overlap with the current region ?*/
73 if (ei->addr >= end || ei->addr + ei->size <= start)
74 continue;
75
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
78 */
79 if (ei->addr <= start)
80 start = ei->addr + ei->size;
81 /*
82 * if start is now at or beyond end, we're done, full
83 * coverage
84 */
85 if (start >= end)
86 return 1;
87 }
88 return 0;
89}
90
91/*
92 * Add a memory region to the kernel e820 map.
93 */
94void __init add_memory_region(u64 start, u64 size, int type)
95{
96 int x = e820.nr_map;
97
98 if (x == E820MAX) {
99 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
100 return;
101 }
102
103 e820.map[x].addr = start;
104 e820.map[x].size = size;
105 e820.map[x].type = type;
106 e820.nr_map++;
107}
108
109void __init e820_print_map(char *who)
110{
111 int i;
112
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,
116 (unsigned long long)
117 (e820.map[i].addr + e820.map[i].size));
118 switch (e820.map[i].type) {
119 case E820_RAM:
120 printk(KERN_CONT "(usable)\n");
121 break;
122 case E820_RESERVED:
123 printk(KERN_CONT "(reserved)\n");
124 break;
125 case E820_ACPI:
126 printk(KERN_CONT "(ACPI data)\n");
127 break;
128 case E820_NVS:
129 printk(KERN_CONT "(ACPI NVS)\n");
130 break;
131 default:
132 printk(KERN_CONT "type %u\n", e820.map[i].type);
133 break;
134 }
135 }
136}
137
138/*
139 * Sanitize the BIOS e820 map.
140 *
141 * Some e820 responses include overlapping entries. The following
142 * replaces the original e820 map with a new one, removing overlaps.
143 *
144 */
145int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
146{
147 struct change_member {
148 struct e820entry *pbios; /* pointer to original bios entry */
149 unsigned long long addr; /* address for this change point */
150 };
151 static struct change_member change_point_list[2*E820MAX] __initdata;
152 static struct change_member *change_point[2*E820MAX] __initdata;
153 static struct e820entry *overlap_list[E820MAX] __initdata;
154 static struct e820entry new_bios[E820MAX] __initdata;
155 struct change_member *change_tmp;
156 unsigned long current_type, last_type;
157 unsigned long long last_addr;
158 int chgidx, still_changing;
159 int overlap_entries;
160 int new_bios_entry;
161 int old_nr, new_nr, chg_nr;
162 int i;
163
164 /*
165 Visually we're performing the following
166 (1,2,3,4 = memory types)...
167
168 Sample memory map (w/overlaps):
169 ____22__________________
170 ______________________4_
171 ____1111________________
172 _44_____________________
173 11111111________________
174 ____________________33__
175 ___________44___________
176 __________33333_________
177 ______________22________
178 ___________________2222_
179 _________111111111______
180 _____________________11_
181 _________________4______
182
183 Sanitized equivalent (no overlap):
184 1_______________________
185 _44_____________________
186 ___1____________________
187 ____22__________________
188 ______11________________
189 _________1______________
190 __________3_____________
191 ___________44___________
192 _____________33_________
193 _______________2________
194 ________________1_______
195 _________________4______
196 ___________________2____
197 ____________________33__
198 ______________________4_
199 */
200
201 /* if there's only one memory region, don't bother */
202 if (*pnr_map < 2)
203 return -1;
204
205 old_nr = *pnr_map;
206
207 /* bail out if we find any unreasonable addresses in bios map */
208 for (i = 0; i < old_nr; i++)
209 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
210 return -1;
211
212 /* create pointers for initial change-point information (for sorting) */
213 for (i = 0; i < 2 * old_nr; i++)
214 change_point[i] = &change_point_list[i];
215
216 /* record all known change-points (starting and ending addresses),
217 omitting those that are for empty memory regions */
218 chgidx = 0;
219 for (i = 0; i < old_nr; i++) {
220 if (biosmap[i].size != 0) {
221 change_point[chgidx]->addr = biosmap[i].addr;
222 change_point[chgidx++]->pbios = &biosmap[i];
223 change_point[chgidx]->addr = biosmap[i].addr +
224 biosmap[i].size;
225 change_point[chgidx++]->pbios = &biosmap[i];
226 }
227 }
228 chg_nr = chgidx;
229
230 /* sort change-point list by memory addresses (low -> high) */
231 still_changing = 1;
232 while (still_changing) {
233 still_changing = 0;
234 for (i = 1; i < chg_nr; i++) {
235 unsigned long long curaddr, lastaddr;
236 unsigned long long curpbaddr, lastpbaddr;
237
238 curaddr = change_point[i]->addr;
239 lastaddr = change_point[i - 1]->addr;
240 curpbaddr = change_point[i]->pbios->addr;
241 lastpbaddr = change_point[i - 1]->pbios->addr;
242
243 /*
244 * swap entries, when:
245 *
246 * curaddr > lastaddr or
247 * curaddr == lastaddr and curaddr == curpbaddr and
248 * lastaddr != lastpbaddr
249 */
250 if (curaddr < lastaddr ||
251 (curaddr == lastaddr && curaddr == curpbaddr &&
252 lastaddr != lastpbaddr)) {
253 change_tmp = change_point[i];
254 change_point[i] = change_point[i-1];
255 change_point[i-1] = change_tmp;
256 still_changing = 1;
257 }
258 }
259 }
260
261 /* create a new bios memory map, removing overlaps */
262 overlap_entries = 0; /* number of entries in the overlap table */
263 new_bios_entry = 0; /* index for creating new bios map entries */
264 last_type = 0; /* start with undefined memory type */
265 last_addr = 0; /* start with 0 as last starting address */
266
267 /* loop through change-points, determining affect on the new bios map */
268 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
269 /* keep track of all overlapping bios entries */
270 if (change_point[chgidx]->addr ==
271 change_point[chgidx]->pbios->addr) {
272 /*
273 * add map entry to overlap list (> 1 entry
274 * implies an overlap)
275 */
276 overlap_list[overlap_entries++] =
277 change_point[chgidx]->pbios;
278 } else {
279 /*
280 * remove entry from list (order independent,
281 * so swap with last)
282 */
283 for (i = 0; i < overlap_entries; i++) {
284 if (overlap_list[i] ==
285 change_point[chgidx]->pbios)
286 overlap_list[i] =
287 overlap_list[overlap_entries-1];
288 }
289 overlap_entries--;
290 }
291 /*
292 * if there are overlapping entries, decide which
293 * "type" to use (larger value takes precedence --
294 * 1=usable, 2,3,4,4+=unusable)
295 */
296 current_type = 0;
297 for (i = 0; i < overlap_entries; i++)
298 if (overlap_list[i]->type > current_type)
299 current_type = overlap_list[i]->type;
300 /*
301 * continue building up new bios map based on this
302 * information
303 */
304 if (current_type != last_type) {
305 if (last_type != 0) {
306 new_bios[new_bios_entry].size =
307 change_point[chgidx]->addr - last_addr;
308 /*
309 * move forward only if the new size
310 * was non-zero
311 */
312 if (new_bios[new_bios_entry].size != 0)
313 /*
314 * no more space left for new
315 * bios entries ?
316 */
317 if (++new_bios_entry >= E820MAX)
318 break;
319 }
320 if (current_type != 0) {
321 new_bios[new_bios_entry].addr =
322 change_point[chgidx]->addr;
323 new_bios[new_bios_entry].type = current_type;
324 last_addr = change_point[chgidx]->addr;
325 }
326 last_type = current_type;
327 }
328 }
329 /* retain count for new bios entries */
330 new_nr = new_bios_entry;
331
332 /* copy new bios mapping into original location */
333 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
334 *pnr_map = new_nr;
335
336 return 0;
337}
338
339/*
340 * Copy the BIOS e820 map into a safe place.
341 *
342 * Sanity-check it while we're at it..
343 *
344 * If we're lucky and live on a modern system, the setup code
345 * will have given us a memory map that we can use to properly
346 * set up memory. If we aren't, we'll fake a memory map.
347 */
348int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
349{
350 /* Only one memory region (or negative)? Ignore it */
351 if (nr_map < 2)
352 return -1;
353
354 do {
355 u64 start = biosmap->addr;
356 u64 size = biosmap->size;
357 u64 end = start + size;
358 u32 type = biosmap->type;
359
360 /* Overflow in 64 bits? Ignore the memory map. */
361 if (start > end)
362 return -1;
363
364 add_memory_region(start, size, type);
365 } while (biosmap++, --nr_map);
366 return 0;
367}
368
369u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
370 unsigned new_type)
371{
372 int i;
373 u64 real_updated_size = 0;
374
375 BUG_ON(old_type == new_type);
376
377 for (i = 0; i < e820.nr_map; i++) {
378 struct e820entry *ei = &e820.map[i];
379 u64 final_start, final_end;
380 if (ei->type != old_type)
381 continue;
382 /* totally covered? */
383 if (ei->addr >= start &&
384 (ei->addr + ei->size) <= (start + size)) {
385 ei->type = new_type;
386 real_updated_size += ei->size;
387 continue;
388 }
389 /* partially covered */
390 final_start = max(start, ei->addr);
391 final_end = min(start + size, ei->addr + ei->size);
392 if (final_start >= final_end)
393 continue;
394 add_memory_region(final_start, final_end - final_start,
395 new_type);
396 real_updated_size += final_end - final_start;
397 }
398 return real_updated_size;
399}
400
401void __init update_e820(void)
402{
403 u8 nr_map;
404
405 nr_map = e820.nr_map;
406 if (sanitize_e820_map(e820.map, &nr_map))
407 return;
408 e820.nr_map = nr_map;
409 printk(KERN_INFO "modified physical RAM map:\n");
410 e820_print_map("modified");
411}
412
413/*
414 * Search for the biggest gap in the low 32 bits of the e820
415 * memory space. We pass this space to PCI to assign MMIO resources
416 * for hotplug or unconfigured devices in.
417 * Hopefully the BIOS let enough space left.
418 */
419__init void e820_setup_gap(void)
420{
421 unsigned long gapstart, gapsize, round;
422 unsigned long long last;
423 int i;
424 int found = 0;
425
426 last = 0x100000000ull;
427 gapstart = 0x10000000;
428 gapsize = 0x400000;
429 i = e820.nr_map;
430 while (--i >= 0) {
431 unsigned long long start = e820.map[i].addr;
432 unsigned long long end = start + e820.map[i].size;
433
434 /*
435 * Since "last" is at most 4GB, we know we'll
436 * fit in 32 bits if this condition is true
437 */
438 if (last > end) {
439 unsigned long gap = last - end;
440
441 if (gap > gapsize) {
442 gapsize = gap;
443 gapstart = end;
444 found = 1;
445 }
446 }
447 if (start < last)
448 last = start;
449 }
450
451#ifdef CONFIG_X86_64
452 if (!found) {
453 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
454 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
455 "address range\n"
456 KERN_ERR "PCI: Unassigned devices with 32bit resource "
457 "registers may break!\n");
458 }
459#endif
460
461 /*
462 * See how much we want to round up: start off with
463 * rounding to the next 1MB area.
464 */
465 round = 0x100000;
466 while ((gapsize >> 4) > round)
467 round += round;
468 /* Fun with two's complement */
469 pci_mem_start = (gapstart + round) & -round;
470
471 printk(KERN_INFO
472 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
473 pci_mem_start, gapstart, gapsize);
474}
475
diff --git a/arch/x86/kernel/e820_32.c b/arch/x86/kernel/e820_32.c
index 751d517d802c..dfe25751038b 100644
--- a/arch/x86/kernel/e820_32.c
+++ b/arch/x86/kernel/e820_32.c
@@ -16,21 +16,6 @@
16#include <asm/e820.h> 16#include <asm/e820.h>
17#include <asm/setup.h> 17#include <asm/setup.h>
18 18
19struct e820map e820;
20struct change_member {
21 struct e820entry *pbios; /* pointer to original bios entry */
22 unsigned long long addr; /* address for this change point */
23};
24static struct change_member change_point_list[2*E820MAX] __initdata;
25static struct change_member *change_point[2*E820MAX] __initdata;
26static struct e820entry *overlap_list[E820MAX] __initdata;
27static struct e820entry new_bios[E820MAX] __initdata;
28/* For PCI or other memory-mapped resources */
29unsigned long pci_mem_start = 0x10000000;
30#ifdef CONFIG_PCI
31EXPORT_SYMBOL(pci_mem_start);
32#endif
33
34static struct resource system_rom_resource = { 19static struct resource system_rom_resource = {
35 .name = "System ROM", 20 .name = "System ROM",
36 .start = 0xf0000, 21 .start = 0xf0000,
@@ -254,223 +239,6 @@ void __init e820_mark_nosave_regions(void)
254} 239}
255#endif 240#endif
256 241
257void __init add_memory_region(unsigned long long start,
258 unsigned long long size, int type)
259{
260 int x;
261
262 x = e820.nr_map;
263
264 if (x == E820MAX) {
265 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
266 return;
267 }
268
269 e820.map[x].addr = start;
270 e820.map[x].size = size;
271 e820.map[x].type = type;
272 e820.nr_map++;
273} /* add_memory_region */
274
275/*
276 * Sanitize the BIOS e820 map.
277 *
278 * Some e820 responses include overlapping entries. The following
279 * replaces the original e820 map with a new one, removing overlaps.
280 *
281 */
282int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
283{
284 struct change_member *change_tmp;
285 unsigned long current_type, last_type;
286 unsigned long long last_addr;
287 int chgidx, still_changing;
288 int overlap_entries;
289 int new_bios_entry;
290 int old_nr, new_nr, chg_nr;
291 int i;
292
293 /*
294 Visually we're performing the following (1,2,3,4 = memory types)...
295
296 Sample memory map (w/overlaps):
297 ____22__________________
298 ______________________4_
299 ____1111________________
300 _44_____________________
301 11111111________________
302 ____________________33__
303 ___________44___________
304 __________33333_________
305 ______________22________
306 ___________________2222_
307 _________111111111______
308 _____________________11_
309 _________________4______
310
311 Sanitized equivalent (no overlap):
312 1_______________________
313 _44_____________________
314 ___1____________________
315 ____22__________________
316 ______11________________
317 _________1______________
318 __________3_____________
319 ___________44___________
320 _____________33_________
321 _______________2________
322 ________________1_______
323 _________________4______
324 ___________________2____
325 ____________________33__
326 ______________________4_
327 */
328 /* if there's only one memory region, don't bother */
329 if (*pnr_map < 2) {
330 return -1;
331 }
332
333 old_nr = *pnr_map;
334
335 /* bail out if we find any unreasonable addresses in bios map */
336 for (i=0; i<old_nr; i++)
337 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
338 return -1;
339 }
340
341 /* create pointers for initial change-point information (for sorting) */
342 for (i=0; i < 2*old_nr; i++)
343 change_point[i] = &change_point_list[i];
344
345 /* record all known change-points (starting and ending addresses),
346 omitting those that are for empty memory regions */
347 chgidx = 0;
348 for (i=0; i < old_nr; i++) {
349 if (biosmap[i].size != 0) {
350 change_point[chgidx]->addr = biosmap[i].addr;
351 change_point[chgidx++]->pbios = &biosmap[i];
352 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
353 change_point[chgidx++]->pbios = &biosmap[i];
354 }
355 }
356 chg_nr = chgidx; /* true number of change-points */
357
358 /* sort change-point list by memory addresses (low -> high) */
359 still_changing = 1;
360 while (still_changing) {
361 still_changing = 0;
362 for (i=1; i < chg_nr; i++) {
363 /* if <current_addr> > <last_addr>, swap */
364 /* or, if current=<start_addr> & last=<end_addr>, swap */
365 if ((change_point[i]->addr < change_point[i-1]->addr) ||
366 ((change_point[i]->addr == change_point[i-1]->addr) &&
367 (change_point[i]->addr == change_point[i]->pbios->addr) &&
368 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
369 )
370 {
371 change_tmp = change_point[i];
372 change_point[i] = change_point[i-1];
373 change_point[i-1] = change_tmp;
374 still_changing=1;
375 }
376 }
377 }
378
379 /* create a new bios memory map, removing overlaps */
380 overlap_entries=0; /* number of entries in the overlap table */
381 new_bios_entry=0; /* index for creating new bios map entries */
382 last_type = 0; /* start with undefined memory type */
383 last_addr = 0; /* start with 0 as last starting address */
384 /* loop through change-points, determining affect on the new bios map */
385 for (chgidx=0; chgidx < chg_nr; chgidx++)
386 {
387 /* keep track of all overlapping bios entries */
388 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
389 {
390 /* add map entry to overlap list (> 1 entry implies an overlap) */
391 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
392 }
393 else
394 {
395 /* remove entry from list (order independent, so swap with last) */
396 for (i=0; i<overlap_entries; i++)
397 {
398 if (overlap_list[i] == change_point[chgidx]->pbios)
399 overlap_list[i] = overlap_list[overlap_entries-1];
400 }
401 overlap_entries--;
402 }
403 /* if there are overlapping entries, decide which "type" to use */
404 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
405 current_type = 0;
406 for (i=0; i<overlap_entries; i++)
407 if (overlap_list[i]->type > current_type)
408 current_type = overlap_list[i]->type;
409 /* continue building up new bios map based on this information */
410 if (current_type != last_type) {
411 if (last_type != 0) {
412 new_bios[new_bios_entry].size =
413 change_point[chgidx]->addr - last_addr;
414 /* move forward only if the new size was non-zero */
415 if (new_bios[new_bios_entry].size != 0)
416 if (++new_bios_entry >= E820MAX)
417 break; /* no more space left for new bios entries */
418 }
419 if (current_type != 0) {
420 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
421 new_bios[new_bios_entry].type = current_type;
422 last_addr=change_point[chgidx]->addr;
423 }
424 last_type = current_type;
425 }
426 }
427 new_nr = new_bios_entry; /* retain count for new bios entries */
428
429 /* copy new bios mapping into original location */
430 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
431 *pnr_map = new_nr;
432
433 return 0;
434}
435
436/*
437 * Copy the BIOS e820 map into a safe place.
438 *
439 * Sanity-check it while we're at it..
440 *
441 * If we're lucky and live on a modern system, the setup code
442 * will have given us a memory map that we can use to properly
443 * set up memory. If we aren't, we'll fake a memory map.
444 *
445 * We check to see that the memory map contains at least 2 elements
446 * before we'll use it, because the detection code in setup.S may
447 * not be perfect and most every PC known to man has two memory
448 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
449 * thinkpad 560x, for example, does not cooperate with the memory
450 * detection code.)
451 */
452int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
453{
454 /* Only one memory region (or negative)? Ignore it */
455 if (nr_map < 2)
456 return -1;
457
458 do {
459 u64 start = biosmap->addr;
460 u64 size = biosmap->size;
461 u64 end = start + size;
462 u32 type = biosmap->type;
463
464 /* Overflow in 64 bits? Ignore the memory map. */
465 if (start > end)
466 return -1;
467
468 add_memory_region(start, size, type);
469 } while (biosmap++, --nr_map);
470
471 return 0;
472}
473
474/* 242/*
475 * Find the highest page frame number we have available 243 * Find the highest page frame number we have available
476 */ 244 */
@@ -535,86 +303,12 @@ void __init register_bootmem_low_pages(unsigned long max_low_pfn)
535 } 303 }
536} 304}
537 305
538void __init e820_register_memory(void)
539{
540 unsigned long gapstart, gapsize, round;
541 unsigned long long last;
542 int i;
543
544 /*
545 * Search for the biggest gap in the low 32 bits of the e820
546 * memory space.
547 */
548 last = 0x100000000ull;
549 gapstart = 0x10000000;
550 gapsize = 0x400000;
551 i = e820.nr_map;
552 while (--i >= 0) {
553 unsigned long long start = e820.map[i].addr;
554 unsigned long long end = start + e820.map[i].size;
555
556 /*
557 * Since "last" is at most 4GB, we know we'll
558 * fit in 32 bits if this condition is true
559 */
560 if (last > end) {
561 unsigned long gap = last - end;
562
563 if (gap > gapsize) {
564 gapsize = gap;
565 gapstart = end;
566 }
567 }
568 if (start < last)
569 last = start;
570 }
571
572 /*
573 * See how much we want to round up: start off with
574 * rounding to the next 1MB area.
575 */
576 round = 0x100000;
577 while ((gapsize >> 4) > round)
578 round += round;
579 /* Fun with two's complement */
580 pci_mem_start = (gapstart + round) & -round;
581
582 printk("Allocating PCI resources starting at %08lx (gap: %08lx:%08lx)\n",
583 pci_mem_start, gapstart, gapsize);
584}
585
586static void __init print_memory_map(char *who)
587{
588 int i;
589
590 for (i = 0; i < e820.nr_map; i++) {
591 printk(" %s: %016Lx - %016Lx ", who,
592 e820.map[i].addr,
593 e820.map[i].addr + e820.map[i].size);
594 switch (e820.map[i].type) {
595 case E820_RAM: printk("(usable)\n");
596 break;
597 case E820_RESERVED:
598 printk("(reserved)\n");
599 break;
600 case E820_ACPI:
601 printk("(ACPI data)\n");
602 break;
603 case E820_NVS:
604 printk("(ACPI NVS)\n");
605 break;
606 default: printk("type %u\n", e820.map[i].type);
607 break;
608 }
609 }
610}
611
612void __init limit_regions(unsigned long long size) 306void __init limit_regions(unsigned long long size)
613{ 307{
614 unsigned long long current_addr; 308 unsigned long long current_addr;
615 int i; 309 int i;
616 310
617 print_memory_map("limit_regions start"); 311 e820_print_map("limit_regions start");
618 for (i = 0; i < e820.nr_map; i++) { 312 for (i = 0; i < e820.nr_map; i++) {
619 current_addr = e820.map[i].addr + e820.map[i].size; 313 current_addr = e820.map[i].addr + e820.map[i].size;
620 if (current_addr < size) 314 if (current_addr < size)
@@ -633,62 +327,10 @@ void __init limit_regions(unsigned long long size)
633 e820.nr_map = i + 1; 327 e820.nr_map = i + 1;
634 e820.map[i].size -= current_addr - size; 328 e820.map[i].size -= current_addr - size;
635 } 329 }
636 print_memory_map("limit_regions endfor"); 330 e820_print_map("limit_regions endfor");
637 return; 331 return;
638 } 332 }
639 print_memory_map("limit_regions endfunc"); 333 e820_print_map("limit_regions endfunc");
640}
641
642/*
643 * This function checks if any part of the range <start,end> is mapped
644 * with type.
645 */
646int
647e820_any_mapped(u64 start, u64 end, unsigned type)
648{
649 int i;
650 for (i = 0; i < e820.nr_map; i++) {
651 const struct e820entry *ei = &e820.map[i];
652 if (type && ei->type != type)
653 continue;
654 if (ei->addr >= end || ei->addr + ei->size <= start)
655 continue;
656 return 1;
657 }
658 return 0;
659}
660EXPORT_SYMBOL_GPL(e820_any_mapped);
661
662 /*
663 * This function checks if the entire range <start,end> is mapped with type.
664 *
665 * Note: this function only works correct if the e820 table is sorted and
666 * not-overlapping, which is the case
667 */
668int __init
669e820_all_mapped(unsigned long s, unsigned long e, unsigned type)
670{
671 u64 start = s;
672 u64 end = e;
673 int i;
674 for (i = 0; i < e820.nr_map; i++) {
675 struct e820entry *ei = &e820.map[i];
676 if (type && ei->type != type)
677 continue;
678 /* is the region (part) in overlap with the current region ?*/
679 if (ei->addr >= end || ei->addr + ei->size <= start)
680 continue;
681 /* if the region is at the beginning of <start,end> we move
682 * start to the end of the region since it's ok until there
683 */
684 if (ei->addr <= start)
685 start = ei->addr + ei->size;
686 /* if start is now at or beyond end, we're done, full
687 * coverage */
688 if (start >= end)
689 return 1; /* we're done */
690 }
691 return 0;
692} 334}
693 335
694/* Overridden in paravirt.c if CONFIG_PARAVIRT */ 336/* Overridden in paravirt.c if CONFIG_PARAVIRT */
@@ -700,7 +342,7 @@ char * __init __attribute__((weak)) memory_setup(void)
700void __init setup_memory_map(void) 342void __init setup_memory_map(void)
701{ 343{
702 printk(KERN_INFO "BIOS-provided physical RAM map:\n"); 344 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
703 print_memory_map(memory_setup()); 345 e820_print_map(memory_setup());
704} 346}
705 347
706static int __initdata user_defined_memmap; 348static int __initdata user_defined_memmap;
@@ -783,55 +425,12 @@ static int __init parse_memmap(char *arg)
783 return 0; 425 return 0;
784} 426}
785early_param("memmap", parse_memmap); 427early_param("memmap", parse_memmap);
786u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
787 unsigned new_type)
788{
789 int i;
790 u64 real_updated_size = 0;
791
792 BUG_ON(old_type == new_type);
793
794 for (i = 0; i < e820.nr_map; i++) {
795 struct e820entry *ei = &e820.map[i];
796 u64 final_start, final_end;
797 if (ei->type != old_type)
798 continue;
799 /* totally covered? */
800 if (ei->addr >= start &&
801 (ei->addr + ei->size) <= (start + size)) {
802 ei->type = new_type;
803 real_updated_size += ei->size;
804 continue;
805 }
806 /* partially covered */
807 final_start = max(start, ei->addr);
808 final_end = min(start + size, ei->addr + ei->size);
809 if (final_start >= final_end)
810 continue;
811 add_memory_region(final_start, final_end - final_start,
812 new_type);
813 real_updated_size += final_end - final_start;
814 }
815
816 return real_updated_size;
817}
818 428
819void __init finish_e820_parsing(void) 429void __init finish_e820_parsing(void)
820{ 430{
821 if (user_defined_memmap) { 431 if (user_defined_memmap) {
822 printk(KERN_INFO "user-defined physical RAM map:\n"); 432 printk(KERN_INFO "user-defined physical RAM map:\n");
823 print_memory_map("user"); 433 e820_print_map("user");
824 } 434 }
825} 435}
826 436
827void __init update_e820(void)
828{
829 u8 nr_map;
830
831 nr_map = e820.nr_map;
832 if (sanitize_e820_map(e820.map, &nr_map))
833 return;
834 e820.nr_map = nr_map;
835 printk(KERN_INFO "modified physical RAM map:\n");
836 print_memory_map("modified");
837}
diff --git a/arch/x86/kernel/e820_64.c b/arch/x86/kernel/e820_64.c
index c45b4dea4055..28a14eb65d3d 100644
--- a/arch/x86/kernel/e820_64.c
+++ b/arch/x86/kernel/e820_64.c
@@ -29,8 +29,6 @@
29#include <asm/kdebug.h> 29#include <asm/kdebug.h>
30#include <asm/trampoline.h> 30#include <asm/trampoline.h>
31 31
32struct e820map e820;
33
34/* 32/*
35 * PFN of last memory page. 33 * PFN of last memory page.
36 */ 34 */
@@ -176,62 +174,6 @@ again:
176 } 174 }
177 return changed; 175 return changed;
178} 176}
179/*
180 * This function checks if any part of the range <start,end> is mapped
181 * with type.
182 */
183int
184e820_any_mapped(unsigned long start, unsigned long end, unsigned type)
185{
186 int i;
187
188 for (i = 0; i < e820.nr_map; i++) {
189 struct e820entry *ei = &e820.map[i];
190
191 if (type && ei->type != type)
192 continue;
193 if (ei->addr >= end || ei->addr + ei->size <= start)
194 continue;
195 return 1;
196 }
197 return 0;
198}
199EXPORT_SYMBOL_GPL(e820_any_mapped);
200
201/*
202 * This function checks if the entire range <start,end> is mapped with type.
203 *
204 * Note: this function only works correct if the e820 table is sorted and
205 * not-overlapping, which is the case
206 */
207int __init e820_all_mapped(unsigned long start, unsigned long end,
208 unsigned type)
209{
210 int i;
211
212 for (i = 0; i < e820.nr_map; i++) {
213 struct e820entry *ei = &e820.map[i];
214
215 if (type && ei->type != type)
216 continue;
217 /* is the region (part) in overlap with the current region ?*/
218 if (ei->addr >= end || ei->addr + ei->size <= start)
219 continue;
220
221 /* if the region is at the beginning of <start,end> we move
222 * start to the end of the region since it's ok until there
223 */
224 if (ei->addr <= start)
225 start = ei->addr + ei->size;
226 /*
227 * if start is now at or beyond end, we're done, full
228 * coverage
229 */
230 if (start >= end)
231 return 1;
232 }
233 return 0;
234}
235 177
236/* 178/*
237 * Find a free area with specified alignment in a specific range. 179 * Find a free area with specified alignment in a specific range.
@@ -435,24 +377,6 @@ e820_register_active_regions(int nid, unsigned long start_pfn,
435} 377}
436 378
437/* 379/*
438 * Add a memory region to the kernel e820 map.
439 */
440void __init add_memory_region(unsigned long start, unsigned long size, int type)
441{
442 int x = e820.nr_map;
443
444 if (x == E820MAX) {
445 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
446 return;
447 }
448
449 e820.map[x].addr = start;
450 e820.map[x].size = size;
451 e820.map[x].type = type;
452 e820.nr_map++;
453}
454
455/*
456 * Find the hole size (in bytes) in the memory range. 380 * Find the hole size (in bytes) in the memory range.
457 * @start: starting address of the memory range to scan 381 * @start: starting address of the memory range to scan
458 * @end: ending address of the memory range to scan 382 * @end: ending address of the memory range to scan
@@ -473,266 +397,6 @@ unsigned long __init e820_hole_size(unsigned long start, unsigned long end)
473 return end - start - (ram << PAGE_SHIFT); 397 return end - start - (ram << PAGE_SHIFT);
474} 398}
475 399
476static void __init e820_print_map(char *who)
477{
478 int i;
479
480 for (i = 0; i < e820.nr_map; i++) {
481 printk(KERN_INFO " %s: %016Lx - %016Lx ", who,
482 (unsigned long long) e820.map[i].addr,
483 (unsigned long long)
484 (e820.map[i].addr + e820.map[i].size));
485 switch (e820.map[i].type) {
486 case E820_RAM:
487 printk(KERN_CONT "(usable)\n");
488 break;
489 case E820_RESERVED:
490 printk(KERN_CONT "(reserved)\n");
491 break;
492 case E820_ACPI:
493 printk(KERN_CONT "(ACPI data)\n");
494 break;
495 case E820_NVS:
496 printk(KERN_CONT "(ACPI NVS)\n");
497 break;
498 default:
499 printk(KERN_CONT "type %u\n", e820.map[i].type);
500 break;
501 }
502 }
503}
504
505/*
506 * Sanitize the BIOS e820 map.
507 *
508 * Some e820 responses include overlapping entries. The following
509 * replaces the original e820 map with a new one, removing overlaps.
510 *
511 */
512static int __init sanitize_e820_map(struct e820entry *biosmap, char *pnr_map)
513{
514 struct change_member {
515 struct e820entry *pbios; /* pointer to original bios entry */
516 unsigned long long addr; /* address for this change point */
517 };
518 static struct change_member change_point_list[2*E820MAX] __initdata;
519 static struct change_member *change_point[2*E820MAX] __initdata;
520 static struct e820entry *overlap_list[E820MAX] __initdata;
521 static struct e820entry new_bios[E820MAX] __initdata;
522 struct change_member *change_tmp;
523 unsigned long current_type, last_type;
524 unsigned long long last_addr;
525 int chgidx, still_changing;
526 int overlap_entries;
527 int new_bios_entry;
528 int old_nr, new_nr, chg_nr;
529 int i;
530
531 /*
532 Visually we're performing the following
533 (1,2,3,4 = memory types)...
534
535 Sample memory map (w/overlaps):
536 ____22__________________
537 ______________________4_
538 ____1111________________
539 _44_____________________
540 11111111________________
541 ____________________33__
542 ___________44___________
543 __________33333_________
544 ______________22________
545 ___________________2222_
546 _________111111111______
547 _____________________11_
548 _________________4______
549
550 Sanitized equivalent (no overlap):
551 1_______________________
552 _44_____________________
553 ___1____________________
554 ____22__________________
555 ______11________________
556 _________1______________
557 __________3_____________
558 ___________44___________
559 _____________33_________
560 _______________2________
561 ________________1_______
562 _________________4______
563 ___________________2____
564 ____________________33__
565 ______________________4_
566 */
567
568 /* if there's only one memory region, don't bother */
569 if (*pnr_map < 2)
570 return -1;
571
572 old_nr = *pnr_map;
573
574 /* bail out if we find any unreasonable addresses in bios map */
575 for (i = 0; i < old_nr; i++)
576 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr)
577 return -1;
578
579 /* create pointers for initial change-point information (for sorting) */
580 for (i = 0; i < 2 * old_nr; i++)
581 change_point[i] = &change_point_list[i];
582
583 /* record all known change-points (starting and ending addresses),
584 omitting those that are for empty memory regions */
585 chgidx = 0;
586 for (i = 0; i < old_nr; i++) {
587 if (biosmap[i].size != 0) {
588 change_point[chgidx]->addr = biosmap[i].addr;
589 change_point[chgidx++]->pbios = &biosmap[i];
590 change_point[chgidx]->addr = biosmap[i].addr +
591 biosmap[i].size;
592 change_point[chgidx++]->pbios = &biosmap[i];
593 }
594 }
595 chg_nr = chgidx;
596
597 /* sort change-point list by memory addresses (low -> high) */
598 still_changing = 1;
599 while (still_changing) {
600 still_changing = 0;
601 for (i = 1; i < chg_nr; i++) {
602 unsigned long long curaddr, lastaddr;
603 unsigned long long curpbaddr, lastpbaddr;
604
605 curaddr = change_point[i]->addr;
606 lastaddr = change_point[i - 1]->addr;
607 curpbaddr = change_point[i]->pbios->addr;
608 lastpbaddr = change_point[i - 1]->pbios->addr;
609
610 /*
611 * swap entries, when:
612 *
613 * curaddr > lastaddr or
614 * curaddr == lastaddr and curaddr == curpbaddr and
615 * lastaddr != lastpbaddr
616 */
617 if (curaddr < lastaddr ||
618 (curaddr == lastaddr && curaddr == curpbaddr &&
619 lastaddr != lastpbaddr)) {
620 change_tmp = change_point[i];
621 change_point[i] = change_point[i-1];
622 change_point[i-1] = change_tmp;
623 still_changing = 1;
624 }
625 }
626 }
627
628 /* create a new bios memory map, removing overlaps */
629 overlap_entries = 0; /* number of entries in the overlap table */
630 new_bios_entry = 0; /* index for creating new bios map entries */
631 last_type = 0; /* start with undefined memory type */
632 last_addr = 0; /* start with 0 as last starting address */
633
634 /* loop through change-points, determining affect on the new bios map */
635 for (chgidx = 0; chgidx < chg_nr; chgidx++) {
636 /* keep track of all overlapping bios entries */
637 if (change_point[chgidx]->addr ==
638 change_point[chgidx]->pbios->addr) {
639 /*
640 * add map entry to overlap list (> 1 entry
641 * implies an overlap)
642 */
643 overlap_list[overlap_entries++] =
644 change_point[chgidx]->pbios;
645 } else {
646 /*
647 * remove entry from list (order independent,
648 * so swap with last)
649 */
650 for (i = 0; i < overlap_entries; i++) {
651 if (overlap_list[i] ==
652 change_point[chgidx]->pbios)
653 overlap_list[i] =
654 overlap_list[overlap_entries-1];
655 }
656 overlap_entries--;
657 }
658 /*
659 * if there are overlapping entries, decide which
660 * "type" to use (larger value takes precedence --
661 * 1=usable, 2,3,4,4+=unusable)
662 */
663 current_type = 0;
664 for (i = 0; i < overlap_entries; i++)
665 if (overlap_list[i]->type > current_type)
666 current_type = overlap_list[i]->type;
667 /*
668 * continue building up new bios map based on this
669 * information
670 */
671 if (current_type != last_type) {
672 if (last_type != 0) {
673 new_bios[new_bios_entry].size =
674 change_point[chgidx]->addr - last_addr;
675 /*
676 * move forward only if the new size
677 * was non-zero
678 */
679 if (new_bios[new_bios_entry].size != 0)
680 /*
681 * no more space left for new
682 * bios entries ?
683 */
684 if (++new_bios_entry >= E820MAX)
685 break;
686 }
687 if (current_type != 0) {
688 new_bios[new_bios_entry].addr =
689 change_point[chgidx]->addr;
690 new_bios[new_bios_entry].type = current_type;
691 last_addr = change_point[chgidx]->addr;
692 }
693 last_type = current_type;
694 }
695 }
696 /* retain count for new bios entries */
697 new_nr = new_bios_entry;
698
699 /* copy new bios mapping into original location */
700 memcpy(biosmap, new_bios, new_nr * sizeof(struct e820entry));
701 *pnr_map = new_nr;
702
703 return 0;
704}
705
706/*
707 * Copy the BIOS e820 map into a safe place.
708 *
709 * Sanity-check it while we're at it..
710 *
711 * If we're lucky and live on a modern system, the setup code
712 * will have given us a memory map that we can use to properly
713 * set up memory. If we aren't, we'll fake a memory map.
714 */
715static int __init copy_e820_map(struct e820entry *biosmap, int nr_map)
716{
717 /* Only one memory region (or negative)? Ignore it */
718 if (nr_map < 2)
719 return -1;
720
721 do {
722 u64 start = biosmap->addr;
723 u64 size = biosmap->size;
724 u64 end = start + size;
725 u32 type = biosmap->type;
726
727 /* Overflow in 64 bits? Ignore the memory map. */
728 if (start > end)
729 return -1;
730
731 add_memory_region(start, size, type);
732 } while (biosmap++, --nr_map);
733 return 0;
734}
735
736static void early_panic(char *msg) 400static void early_panic(char *msg)
737{ 401{
738 early_printk(msg); 402 early_printk(msg);
@@ -829,114 +493,6 @@ void __init finish_e820_parsing(void)
829 } 493 }
830} 494}
831 495
832u64 __init update_memory_range(u64 start, u64 size, unsigned old_type,
833 unsigned new_type)
834{
835 int i;
836 u64 real_updated_size = 0;
837
838 BUG_ON(old_type == new_type);
839
840 for (i = 0; i < e820.nr_map; i++) {
841 struct e820entry *ei = &e820.map[i];
842 u64 final_start, final_end;
843 if (ei->type != old_type)
844 continue;
845 /* totally covered? */
846 if (ei->addr >= start &&
847 (ei->addr + ei->size) <= (start + size)) {
848 ei->type = new_type;
849 real_updated_size += ei->size;
850 continue;
851 }
852 /* partially covered */
853 final_start = max(start, ei->addr);
854 final_end = min(start + size, ei->addr + ei->size);
855 if (final_start >= final_end)
856 continue;
857 add_memory_region(final_start, final_end - final_start,
858 new_type);
859 real_updated_size += final_end - final_start;
860 }
861 return real_updated_size;
862}
863
864void __init update_e820(void)
865{
866 u8 nr_map;
867
868 nr_map = e820.nr_map;
869 if (sanitize_e820_map(e820.map, &nr_map))
870 return;
871 e820.nr_map = nr_map;
872 printk(KERN_INFO "modified physical RAM map:\n");
873 e820_print_map("modified");
874}
875
876unsigned long pci_mem_start = 0xaeedbabe;
877EXPORT_SYMBOL(pci_mem_start);
878
879/*
880 * Search for the biggest gap in the low 32 bits of the e820
881 * memory space. We pass this space to PCI to assign MMIO resources
882 * for hotplug or unconfigured devices in.
883 * Hopefully the BIOS let enough space left.
884 */
885__init void e820_setup_gap(void)
886{
887 unsigned long gapstart, gapsize, round;
888 unsigned long last;
889 int i;
890 int found = 0;
891
892 last = 0x100000000ull;
893 gapstart = 0x10000000;
894 gapsize = 0x400000;
895 i = e820.nr_map;
896 while (--i >= 0) {
897 unsigned long long start = e820.map[i].addr;
898 unsigned long long end = start + e820.map[i].size;
899
900 /*
901 * Since "last" is at most 4GB, we know we'll
902 * fit in 32 bits if this condition is true
903 */
904 if (last > end) {
905 unsigned long gap = last - end;
906
907 if (gap > gapsize) {
908 gapsize = gap;
909 gapstart = end;
910 found = 1;
911 }
912 }
913 if (start < last)
914 last = start;
915 }
916
917 if (!found) {
918 gapstart = (end_pfn << PAGE_SHIFT) + 1024*1024;
919 printk(KERN_ERR "PCI: Warning: Cannot find a gap in the 32bit "
920 "address range\n"
921 KERN_ERR "PCI: Unassigned devices with 32bit resource "
922 "registers may break!\n");
923 }
924
925 /*
926 * See how much we want to round up: start off with
927 * rounding to the next 1MB area.
928 */
929 round = 0x100000;
930 while ((gapsize >> 4) > round)
931 round += round;
932 /* Fun with two's complement */
933 pci_mem_start = (gapstart + round) & -round;
934
935 printk(KERN_INFO
936 "Allocating PCI resources starting at %lx (gap: %lx:%lx)\n",
937 pci_mem_start, gapstart, gapsize);
938}
939
940int __init arch_get_ram_range(int slot, u64 *addr, u64 *size) 496int __init arch_get_ram_range(int slot, u64 *addr, u64 *size)
941{ 497{
942 int i; 498 int i;
diff --git a/arch/x86/kernel/setup_32.c b/arch/x86/kernel/setup_32.c
index b54c79c91efd..5faeab69edd9 100644
--- a/arch/x86/kernel/setup_32.c
+++ b/arch/x86/kernel/setup_32.c
@@ -875,7 +875,7 @@ void __init setup_arch(char **cmdline_p)
875 get_smp_config(); 875 get_smp_config();
876#endif 876#endif
877 877
878 e820_register_memory(); 878 e820_setup_gap();
879 e820_mark_nosave_regions(); 879 e820_mark_nosave_regions();
880 880
881#ifdef CONFIG_VT 881#ifdef CONFIG_VT