blob: 0db95760b0732bd1c29b5ecf19dc4ce6d9fb03ac [file] [log] [blame]
bibo,mao269c2d82006-12-07 02:14:06 +01001#include <linux/kernel.h>
2#include <linux/types.h>
3#include <linux/init.h>
4#include <linux/bootmem.h>
5#include <linux/ioport.h>
6#include <linux/string.h>
7#include <linux/kexec.h>
8#include <linux/module.h>
9#include <linux/mm.h>
10#include <linux/efi.h>
11
12#include <asm/pgtable.h>
13#include <asm/page.h>
14#include <asm/e820.h>
15
16#ifdef CONFIG_EFI
17int efi_enabled = 0;
18EXPORT_SYMBOL(efi_enabled);
19#endif
20
21struct e820map e820;
bibo,mao8e3342f2006-12-07 02:14:06 +010022struct change_member {
23 struct e820entry *pbios; /* pointer to original bios entry */
24 unsigned long long addr; /* address for this change point */
25};
26static struct change_member change_point_list[2*E820MAX] __initdata;
27static struct change_member *change_point[2*E820MAX] __initdata;
28static struct e820entry *overlap_list[E820MAX] __initdata;
29static struct e820entry new_bios[E820MAX] __initdata;
bibo,mao269c2d82006-12-07 02:14:06 +010030struct resource data_resource = {
31 .name = "Kernel data",
32 .start = 0,
33 .end = 0,
34 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
35};
36
37struct resource code_resource = {
38 .name = "Kernel code",
39 .start = 0,
40 .end = 0,
41 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
42};
43
44static struct resource system_rom_resource = {
45 .name = "System ROM",
46 .start = 0xf0000,
47 .end = 0xfffff,
48 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
49};
50
51static struct resource extension_rom_resource = {
52 .name = "Extension ROM",
53 .start = 0xe0000,
54 .end = 0xeffff,
55 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
56};
57
58static struct resource adapter_rom_resources[] = { {
59 .name = "Adapter ROM",
60 .start = 0xc8000,
61 .end = 0,
62 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
63}, {
64 .name = "Adapter ROM",
65 .start = 0,
66 .end = 0,
67 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
68}, {
69 .name = "Adapter ROM",
70 .start = 0,
71 .end = 0,
72 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
73}, {
74 .name = "Adapter ROM",
75 .start = 0,
76 .end = 0,
77 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
78}, {
79 .name = "Adapter ROM",
80 .start = 0,
81 .end = 0,
82 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
83}, {
84 .name = "Adapter ROM",
85 .start = 0,
86 .end = 0,
87 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
88} };
89
90static struct resource video_rom_resource = {
91 .name = "Video ROM",
92 .start = 0xc0000,
93 .end = 0xc7fff,
94 .flags = IORESOURCE_BUSY | IORESOURCE_READONLY | IORESOURCE_MEM
95};
96
97static struct resource video_ram_resource = {
98 .name = "Video RAM area",
99 .start = 0xa0000,
100 .end = 0xbffff,
101 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
102};
103
104static struct resource standard_io_resources[] = { {
105 .name = "dma1",
106 .start = 0x0000,
107 .end = 0x001f,
108 .flags = IORESOURCE_BUSY | IORESOURCE_IO
109}, {
110 .name = "pic1",
111 .start = 0x0020,
112 .end = 0x0021,
113 .flags = IORESOURCE_BUSY | IORESOURCE_IO
114}, {
115 .name = "timer0",
116 .start = 0x0040,
117 .end = 0x0043,
118 .flags = IORESOURCE_BUSY | IORESOURCE_IO
119}, {
120 .name = "timer1",
121 .start = 0x0050,
122 .end = 0x0053,
123 .flags = IORESOURCE_BUSY | IORESOURCE_IO
124}, {
125 .name = "keyboard",
126 .start = 0x0060,
127 .end = 0x006f,
128 .flags = IORESOURCE_BUSY | IORESOURCE_IO
129}, {
130 .name = "dma page reg",
131 .start = 0x0080,
132 .end = 0x008f,
133 .flags = IORESOURCE_BUSY | IORESOURCE_IO
134}, {
135 .name = "pic2",
136 .start = 0x00a0,
137 .end = 0x00a1,
138 .flags = IORESOURCE_BUSY | IORESOURCE_IO
139}, {
140 .name = "dma2",
141 .start = 0x00c0,
142 .end = 0x00df,
143 .flags = IORESOURCE_BUSY | IORESOURCE_IO
144}, {
145 .name = "fpu",
146 .start = 0x00f0,
147 .end = 0x00ff,
148 .flags = IORESOURCE_BUSY | IORESOURCE_IO
149} };
150
151#define romsignature(x) (*(unsigned short *)(x) == 0xaa55)
152
153static int __init romchecksum(unsigned char *rom, unsigned long length)
154{
155 unsigned char *p, sum = 0;
156
157 for (p = rom; p < rom + length; p++)
158 sum += *p;
159 return sum == 0;
160}
161
162static void __init probe_roms(void)
163{
164 unsigned long start, length, upper;
165 unsigned char *rom;
166 int i;
167
168 /* video rom */
169 upper = adapter_rom_resources[0].start;
170 for (start = video_rom_resource.start; start < upper; start += 2048) {
171 rom = isa_bus_to_virt(start);
172 if (!romsignature(rom))
173 continue;
174
175 video_rom_resource.start = start;
176
177 /* 0 < length <= 0x7f * 512, historically */
178 length = rom[2] * 512;
179
180 /* if checksum okay, trust length byte */
181 if (length && romchecksum(rom, length))
182 video_rom_resource.end = start + length - 1;
183
184 request_resource(&iomem_resource, &video_rom_resource);
185 break;
186 }
187
188 start = (video_rom_resource.end + 1 + 2047) & ~2047UL;
189 if (start < upper)
190 start = upper;
191
192 /* system rom */
193 request_resource(&iomem_resource, &system_rom_resource);
194 upper = system_rom_resource.start;
195
196 /* check for extension rom (ignore length byte!) */
197 rom = isa_bus_to_virt(extension_rom_resource.start);
198 if (romsignature(rom)) {
199 length = extension_rom_resource.end - extension_rom_resource.start + 1;
200 if (romchecksum(rom, length)) {
201 request_resource(&iomem_resource, &extension_rom_resource);
202 upper = extension_rom_resource.start;
203 }
204 }
205
206 /* check for adapter roms on 2k boundaries */
207 for (i = 0; i < ARRAY_SIZE(adapter_rom_resources) && start < upper; start += 2048) {
208 rom = isa_bus_to_virt(start);
209 if (!romsignature(rom))
210 continue;
211
212 /* 0 < length <= 0x7f * 512, historically */
213 length = rom[2] * 512;
214
215 /* but accept any length that fits if checksum okay */
216 if (!length || start + length > upper || !romchecksum(rom, length))
217 continue;
218
219 adapter_rom_resources[i].start = start;
220 adapter_rom_resources[i].end = start + length - 1;
221 request_resource(&iomem_resource, &adapter_rom_resources[i]);
222
223 start = adapter_rom_resources[i++].end & ~2047UL;
224 }
225}
226
227/*
228 * Request address space for all standard RAM and ROM resources
229 * and also for regions reported as reserved by the e820.
230 */
231static void __init
232legacy_init_iomem_resources(struct resource *code_resource, struct resource *data_resource)
233{
234 int i;
235
236 probe_roms();
237 for (i = 0; i < e820.nr_map; i++) {
238 struct resource *res;
239#ifndef CONFIG_RESOURCES_64BIT
240 if (e820.map[i].addr + e820.map[i].size > 0x100000000ULL)
241 continue;
242#endif
243 res = kzalloc(sizeof(struct resource), GFP_ATOMIC);
244 switch (e820.map[i].type) {
245 case E820_RAM: res->name = "System RAM"; break;
246 case E820_ACPI: res->name = "ACPI Tables"; break;
247 case E820_NVS: res->name = "ACPI Non-volatile Storage"; break;
248 default: res->name = "reserved";
249 }
250 res->start = e820.map[i].addr;
251 res->end = res->start + e820.map[i].size - 1;
252 res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
253 if (request_resource(&iomem_resource, res)) {
254 kfree(res);
255 continue;
256 }
257 if (e820.map[i].type == E820_RAM) {
258 /*
259 * We don't know which RAM region contains kernel data,
260 * so we try it repeatedly and let the resource manager
261 * test it.
262 */
263 request_resource(res, code_resource);
264 request_resource(res, data_resource);
265#ifdef CONFIG_KEXEC
266 request_resource(res, &crashk_res);
267#endif
268 }
269 }
270}
271
272/*
273 * Request address space for all standard resources
274 *
275 * This is called just before pcibios_init(), which is also a
276 * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
277 */
278static int __init request_standard_resources(void)
279{
280 int i;
281
282 printk("Setting up standard PCI resources\n");
283 if (efi_enabled)
284 efi_initialize_iomem_resources(&code_resource, &data_resource);
285 else
286 legacy_init_iomem_resources(&code_resource, &data_resource);
287
288 /* EFI systems may still have VGA */
289 request_resource(&iomem_resource, &video_ram_resource);
290
291 /* request I/O space for devices used on all i[345]86 PCs */
292 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
293 request_resource(&ioport_resource, &standard_io_resources[i]);
294 return 0;
295}
296
297subsys_initcall(request_standard_resources);
bibo,mao8e3342f2006-12-07 02:14:06 +0100298
299void __init add_memory_region(unsigned long long start,
300 unsigned long long size, int type)
301{
302 int x;
303
304 if (!efi_enabled) {
305 x = e820.nr_map;
306
307 if (x == E820MAX) {
308 printk(KERN_ERR "Ooops! Too many entries in the memory map!\n");
309 return;
310 }
311
312 e820.map[x].addr = start;
313 e820.map[x].size = size;
314 e820.map[x].type = type;
315 e820.nr_map++;
316 }
317} /* add_memory_region */
318
319/*
320 * Sanitize the BIOS e820 map.
321 *
322 * Some e820 responses include overlapping entries. The following
323 * replaces the original e820 map with a new one, removing overlaps.
324 *
325 */
326int __init sanitize_e820_map(struct e820entry * biosmap, char * pnr_map)
327{
328 struct change_member *change_tmp;
329 unsigned long current_type, last_type;
330 unsigned long long last_addr;
331 int chgidx, still_changing;
332 int overlap_entries;
333 int new_bios_entry;
334 int old_nr, new_nr, chg_nr;
335 int i;
336
337 /*
338 Visually we're performing the following (1,2,3,4 = memory types)...
339
340 Sample memory map (w/overlaps):
341 ____22__________________
342 ______________________4_
343 ____1111________________
344 _44_____________________
345 11111111________________
346 ____________________33__
347 ___________44___________
348 __________33333_________
349 ______________22________
350 ___________________2222_
351 _________111111111______
352 _____________________11_
353 _________________4______
354
355 Sanitized equivalent (no overlap):
356 1_______________________
357 _44_____________________
358 ___1____________________
359 ____22__________________
360 ______11________________
361 _________1______________
362 __________3_____________
363 ___________44___________
364 _____________33_________
365 _______________2________
366 ________________1_______
367 _________________4______
368 ___________________2____
369 ____________________33__
370 ______________________4_
371 */
372 printk("sanitize start\n");
373 /* if there's only one memory region, don't bother */
374 if (*pnr_map < 2) {
375 printk("sanitize bail 0\n");
376 return -1;
377 }
378
379 old_nr = *pnr_map;
380
381 /* bail out if we find any unreasonable addresses in bios map */
382 for (i=0; i<old_nr; i++)
383 if (biosmap[i].addr + biosmap[i].size < biosmap[i].addr) {
384 printk("sanitize bail 1\n");
385 return -1;
386 }
387
388 /* create pointers for initial change-point information (for sorting) */
389 for (i=0; i < 2*old_nr; i++)
390 change_point[i] = &change_point_list[i];
391
392 /* record all known change-points (starting and ending addresses),
393 omitting those that are for empty memory regions */
394 chgidx = 0;
395 for (i=0; i < old_nr; i++) {
396 if (biosmap[i].size != 0) {
397 change_point[chgidx]->addr = biosmap[i].addr;
398 change_point[chgidx++]->pbios = &biosmap[i];
399 change_point[chgidx]->addr = biosmap[i].addr + biosmap[i].size;
400 change_point[chgidx++]->pbios = &biosmap[i];
401 }
402 }
403 chg_nr = chgidx; /* true number of change-points */
404
405 /* sort change-point list by memory addresses (low -> high) */
406 still_changing = 1;
407 while (still_changing) {
408 still_changing = 0;
409 for (i=1; i < chg_nr; i++) {
410 /* if <current_addr> > <last_addr>, swap */
411 /* or, if current=<start_addr> & last=<end_addr>, swap */
412 if ((change_point[i]->addr < change_point[i-1]->addr) ||
413 ((change_point[i]->addr == change_point[i-1]->addr) &&
414 (change_point[i]->addr == change_point[i]->pbios->addr) &&
415 (change_point[i-1]->addr != change_point[i-1]->pbios->addr))
416 )
417 {
418 change_tmp = change_point[i];
419 change_point[i] = change_point[i-1];
420 change_point[i-1] = change_tmp;
421 still_changing=1;
422 }
423 }
424 }
425
426 /* create a new bios memory map, removing overlaps */
427 overlap_entries=0; /* number of entries in the overlap table */
428 new_bios_entry=0; /* index for creating new bios map entries */
429 last_type = 0; /* start with undefined memory type */
430 last_addr = 0; /* start with 0 as last starting address */
431 /* loop through change-points, determining affect on the new bios map */
432 for (chgidx=0; chgidx < chg_nr; chgidx++)
433 {
434 /* keep track of all overlapping bios entries */
435 if (change_point[chgidx]->addr == change_point[chgidx]->pbios->addr)
436 {
437 /* add map entry to overlap list (> 1 entry implies an overlap) */
438 overlap_list[overlap_entries++]=change_point[chgidx]->pbios;
439 }
440 else
441 {
442 /* remove entry from list (order independent, so swap with last) */
443 for (i=0; i<overlap_entries; i++)
444 {
445 if (overlap_list[i] == change_point[chgidx]->pbios)
446 overlap_list[i] = overlap_list[overlap_entries-1];
447 }
448 overlap_entries--;
449 }
450 /* if there are overlapping entries, decide which "type" to use */
451 /* (larger value takes precedence -- 1=usable, 2,3,4,4+=unusable) */
452 current_type = 0;
453 for (i=0; i<overlap_entries; i++)
454 if (overlap_list[i]->type > current_type)
455 current_type = overlap_list[i]->type;
456 /* continue building up new bios map based on this information */
457 if (current_type != last_type) {
458 if (last_type != 0) {
459 new_bios[new_bios_entry].size =
460 change_point[chgidx]->addr - last_addr;
461 /* move forward only if the new size was non-zero */
462 if (new_bios[new_bios_entry].size != 0)
463 if (++new_bios_entry >= E820MAX)
464 break; /* no more space left for new bios entries */
465 }
466 if (current_type != 0) {
467 new_bios[new_bios_entry].addr = change_point[chgidx]->addr;
468 new_bios[new_bios_entry].type = current_type;
469 last_addr=change_point[chgidx]->addr;
470 }
471 last_type = current_type;
472 }
473 }
474 new_nr = new_bios_entry; /* retain count for new bios entries */
475
476 /* copy new bios mapping into original location */
477 memcpy(biosmap, new_bios, new_nr*sizeof(struct e820entry));
478 *pnr_map = new_nr;
479
480 printk("sanitize end\n");
481 return 0;
482}
483
484/*
485 * Copy the BIOS e820 map into a safe place.
486 *
487 * Sanity-check it while we're at it..
488 *
489 * If we're lucky and live on a modern system, the setup code
490 * will have given us a memory map that we can use to properly
491 * set up memory. If we aren't, we'll fake a memory map.
492 *
493 * We check to see that the memory map contains at least 2 elements
494 * before we'll use it, because the detection code in setup.S may
495 * not be perfect and most every PC known to man has two memory
496 * regions: one from 0 to 640k, and one from 1mb up. (The IBM
497 * thinkpad 560x, for example, does not cooperate with the memory
498 * detection code.)
499 */
500int __init copy_e820_map(struct e820entry * biosmap, int nr_map)
501{
502 /* Only one memory region (or negative)? Ignore it */
503 if (nr_map < 2)
504 return -1;
505
506 do {
507 unsigned long long start = biosmap->addr;
508 unsigned long long size = biosmap->size;
509 unsigned long long end = start + size;
510 unsigned long type = biosmap->type;
511 printk("copy_e820_map() start: %016Lx size: %016Lx end: %016Lx type: %ld\n", start, size, end, type);
512
513 /* Overflow in 64 bits? Ignore the memory map. */
514 if (start > end)
515 return -1;
516
517 /*
518 * Some BIOSes claim RAM in the 640k - 1M region.
519 * Not right. Fix it up.
520 */
521 if (type == E820_RAM) {
522 printk("copy_e820_map() type is E820_RAM\n");
523 if (start < 0x100000ULL && end > 0xA0000ULL) {
524 printk("copy_e820_map() lies in range...\n");
525 if (start < 0xA0000ULL) {
526 printk("copy_e820_map() start < 0xA0000ULL\n");
527 add_memory_region(start, 0xA0000ULL-start, type);
528 }
529 if (end <= 0x100000ULL) {
530 printk("copy_e820_map() end <= 0x100000ULL\n");
531 continue;
532 }
533 start = 0x100000ULL;
534 size = end - start;
535 }
536 }
537 add_memory_region(start, size, type);
538 } while (biosmap++,--nr_map);
539 return 0;
540}
541