2 * Copyright (C) 1995 Linus Torvalds
4 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
6 * Memory region support
7 * David Parsons <orc@pell.chi.il.us>, July-August 1999
9 * Added E820 sanitization routine (removes overlapping memory regions);
10 * Brian Moyle <bmoyle@mvista.com>, February 2001
12 * Moved CPU detection code to cpu/${cpu}.c
13 * Patrick Mochel <mochel@osdl.org>, March 2002
15 * Provisions for empty E820 memory regions (reported by certain BIOSes).
16 * Alex Achenbach <xela@slit.de>, December 2002.
21 * This file handles the architecture-dependent parts of initialization
24 #include <linux/sched.h>
26 #include <linux/mmzone.h>
27 #include <linux/screen_info.h>
28 #include <linux/ioport.h>
29 #include <linux/acpi.h>
30 #include <linux/apm_bios.h>
31 #include <linux/initrd.h>
32 #include <linux/bootmem.h>
33 #include <linux/seq_file.h>
34 #include <linux/console.h>
35 #include <linux/mca.h>
36 #include <linux/root_dev.h>
37 #include <linux/highmem.h>
38 #include <linux/module.h>
39 #include <linux/efi.h>
40 #include <linux/init.h>
41 #include <linux/edd.h>
42 #include <linux/nodemask.h>
43 #include <linux/kexec.h>
44 #include <linux/crash_dump.h>
45 #include <linux/dmi.h>
46 #include <linux/pfn.h>
47 #include <linux/pci.h>
49 #include <video/edid.h>
53 #include <asm/mpspec.h>
54 #include <asm/mmzone.h>
55 #include <asm/setup.h>
56 #include <asm/arch_hooks.h>
57 #include <asm/sections.h>
58 #include <asm/io_apic.h>
62 #include <setup_arch.h>
63 #include <bios_ebda.h>
64 #include <asm/cacheflush.h>
66 /* This value is set up by the early boot code to point to the value
67 immediately after the boot time page tables. It contains a *physical*
68 address, and must not be in the .bss segment! */
69 unsigned long init_pg_tables_end __initdata = ~0UL;
71 int disable_pse __cpuinitdata = 0;
76 static struct resource data_resource = {
77 .name = "Kernel data",
80 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
83 static struct resource code_resource = {
84 .name = "Kernel code",
87 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
90 static struct resource bss_resource = {
94 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
97 static struct resource video_ram_resource = {
98 .name = "Video RAM area",
101 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
104 static struct resource standard_io_resources[] = { {
108 .flags = IORESOURCE_BUSY | IORESOURCE_IO
113 .flags = IORESOURCE_BUSY | IORESOURCE_IO
118 .flags = IORESOURCE_BUSY | IORESOURCE_IO
123 .flags = IORESOURCE_BUSY | IORESOURCE_IO
128 .flags = IORESOURCE_BUSY | IORESOURCE_IO
130 .name = "dma page reg",
133 .flags = IORESOURCE_BUSY | IORESOURCE_IO
138 .flags = IORESOURCE_BUSY | IORESOURCE_IO
143 .flags = IORESOURCE_BUSY | IORESOURCE_IO
148 .flags = IORESOURCE_BUSY | IORESOURCE_IO
151 /* cpu data as detected by the assembly code in head.S */
152 struct cpuinfo_x86 new_cpu_data __cpuinitdata = { 0, 0, 0, 0, -1, 1, 0, 0, -1 };
153 /* common cpu data for all cpus */
154 struct cpuinfo_x86 boot_cpu_data __read_mostly = { 0, 0, 0, 0, -1, 1, 0, 0, -1 };
155 EXPORT_SYMBOL(boot_cpu_data);
157 unsigned long mmu_cr4_features;
159 /* for MCA, but anyone else can use it if they want */
160 unsigned int machine_id;
161 unsigned int machine_submodel_id;
162 unsigned int BIOS_revision;
163 unsigned int mca_pentium_flag;
165 /* Boot loader ID as an integer, for the benefit of proc_dointvec */
168 /* user-defined highmem size */
169 static unsigned int highmem_pages = -1;
174 struct screen_info screen_info;
175 EXPORT_SYMBOL(screen_info);
176 struct apm_info apm_info;
177 EXPORT_SYMBOL(apm_info);
178 struct edid_info edid_info;
179 EXPORT_SYMBOL_GPL(edid_info);
180 struct ist_info ist_info;
181 #if defined(CONFIG_X86_SPEEDSTEP_SMI) || \
182 defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE)
183 EXPORT_SYMBOL(ist_info);
186 extern void early_cpu_init(void);
187 extern int root_mountflags;
189 unsigned long saved_videomode;
191 #define RAMDISK_IMAGE_START_MASK 0x07FF
192 #define RAMDISK_PROMPT_FLAG 0x8000
193 #define RAMDISK_LOAD_FLAG 0x4000
195 static char __initdata command_line[COMMAND_LINE_SIZE];
197 struct boot_params __initdata boot_params;
199 #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
201 #ifdef CONFIG_EDD_MODULE
205 * copy_edd() - Copy the BIOS EDD information
206 * from boot_params into a safe place.
209 static inline void copy_edd(void)
211 memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer,
212 sizeof(edd.mbr_signature));
213 memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info));
214 edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries;
215 edd.edd_info_nr = boot_params.eddbuf_entries;
218 static inline void copy_edd(void)
223 int __initdata user_defined_memmap = 0;
226 * "mem=nopentium" disables the 4MB page tables.
227 * "mem=XXX[kKmM]" defines a memory region from HIGH_MEM
228 * to <mem>, overriding the bios size.
229 * "memmap=XXX[KkmM]@XXX[KkmM]" defines a memory region from
230 * <start> to <start>+<mem>, overriding the bios size.
232 * HPA tells me bootloaders need to parse mem=, so no new
233 * option should be mem= [also see Documentation/i386/boot.txt]
235 static int __init parse_mem(char *arg)
240 if (strcmp(arg, "nopentium") == 0) {
241 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_PSE);
244 /* If the user specifies memory size, we
245 * limit the BIOS-provided memory map to
246 * that size. exactmap can be used to specify
247 * the exact map. mem=number can be used to
248 * trim the existing memory map.
250 unsigned long long mem_size;
252 mem_size = memparse(arg, &arg);
253 limit_regions(mem_size);
254 user_defined_memmap = 1;
258 early_param("mem", parse_mem);
260 #ifdef CONFIG_PROC_VMCORE
261 /* elfcorehdr= specifies the location of elf core header
262 * stored by the crashed kernel.
264 static int __init parse_elfcorehdr(char *arg)
269 elfcorehdr_addr = memparse(arg, &arg);
272 early_param("elfcorehdr", parse_elfcorehdr);
273 #endif /* CONFIG_PROC_VMCORE */
276 * highmem=size forces highmem to be exactly 'size' bytes.
277 * This works even on boxes that have no highmem otherwise.
278 * This also works to reduce highmem size on bigger boxes.
280 static int __init parse_highmem(char *arg)
285 highmem_pages = memparse(arg, &arg) >> PAGE_SHIFT;
288 early_param("highmem", parse_highmem);
291 * vmalloc=size forces the vmalloc area to be exactly 'size'
292 * bytes. This can be used to increase (or decrease) the
293 * vmalloc area - the default is 128m.
295 static int __init parse_vmalloc(char *arg)
300 __VMALLOC_RESERVE = memparse(arg, &arg);
303 early_param("vmalloc", parse_vmalloc);
306 * reservetop=size reserves a hole at the top of the kernel address space which
307 * a hypervisor can load into later. Needed for dynamically loaded hypervisors,
308 * so relocating the fixmap can be done before paging initialization.
310 static int __init parse_reservetop(char *arg)
312 unsigned long address;
317 address = memparse(arg, &arg);
318 reserve_top_address(address);
321 early_param("reservetop", parse_reservetop);
324 * Determine low and high memory ranges:
326 unsigned long __init find_max_low_pfn(void)
328 unsigned long max_low_pfn;
330 max_low_pfn = max_pfn;
331 if (max_low_pfn > MAXMEM_PFN) {
332 if (highmem_pages == -1)
333 highmem_pages = max_pfn - MAXMEM_PFN;
334 if (highmem_pages + MAXMEM_PFN < max_pfn)
335 max_pfn = MAXMEM_PFN + highmem_pages;
336 if (highmem_pages + MAXMEM_PFN > max_pfn) {
337 printk("only %luMB highmem pages available, ignoring highmem size of %uMB.\n", pages_to_mb(max_pfn - MAXMEM_PFN), pages_to_mb(highmem_pages));
340 max_low_pfn = MAXMEM_PFN;
341 #ifndef CONFIG_HIGHMEM
342 /* Maximum memory usable is what is directly addressable */
343 printk(KERN_WARNING "Warning only %ldMB will be used.\n",
345 if (max_pfn > MAX_NONPAE_PFN)
346 printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
348 printk(KERN_WARNING "Use a HIGHMEM enabled kernel.\n");
349 max_pfn = MAXMEM_PFN;
350 #else /* !CONFIG_HIGHMEM */
351 #ifndef CONFIG_HIGHMEM64G
352 if (max_pfn > MAX_NONPAE_PFN) {
353 max_pfn = MAX_NONPAE_PFN;
354 printk(KERN_WARNING "Warning only 4GB will be used.\n");
355 printk(KERN_WARNING "Use a HIGHMEM64G enabled kernel.\n");
357 #endif /* !CONFIG_HIGHMEM64G */
358 #endif /* !CONFIG_HIGHMEM */
360 if (highmem_pages == -1)
362 #ifdef CONFIG_HIGHMEM
363 if (highmem_pages >= max_pfn) {
364 printk(KERN_ERR "highmem size specified (%uMB) is bigger than pages available (%luMB)!.\n", pages_to_mb(highmem_pages), pages_to_mb(max_pfn));
368 if (max_low_pfn-highmem_pages < 64*1024*1024/PAGE_SIZE){
369 printk(KERN_ERR "highmem size %uMB results in smaller than 64MB lowmem, ignoring it.\n", pages_to_mb(highmem_pages));
372 max_low_pfn -= highmem_pages;
376 printk(KERN_ERR "ignoring highmem size on non-highmem kernel!\n");
383 * workaround for Dell systems that neglect to reserve EBDA
385 static void __init reserve_ebda_region(void)
388 addr = get_bios_ebda();
390 reserve_bootmem(addr, PAGE_SIZE);
393 #ifndef CONFIG_NEED_MULTIPLE_NODES
394 void __init setup_bootmem_allocator(void);
395 static unsigned long __init setup_memory(void)
398 * partially used pages are not usable - thus
399 * we are rounding upwards:
401 min_low_pfn = PFN_UP(init_pg_tables_end);
405 max_low_pfn = find_max_low_pfn();
407 #ifdef CONFIG_HIGHMEM
408 highstart_pfn = highend_pfn = max_pfn;
409 if (max_pfn > max_low_pfn) {
410 highstart_pfn = max_low_pfn;
412 printk(KERN_NOTICE "%ldMB HIGHMEM available.\n",
413 pages_to_mb(highend_pfn - highstart_pfn));
414 num_physpages = highend_pfn;
415 high_memory = (void *) __va(highstart_pfn * PAGE_SIZE - 1) + 1;
417 num_physpages = max_low_pfn;
418 high_memory = (void *) __va(max_low_pfn * PAGE_SIZE - 1) + 1;
420 #ifdef CONFIG_FLATMEM
421 max_mapnr = num_physpages;
423 printk(KERN_NOTICE "%ldMB LOWMEM available.\n",
424 pages_to_mb(max_low_pfn));
426 setup_bootmem_allocator();
431 void __init zone_sizes_init(void)
433 unsigned long max_zone_pfns[MAX_NR_ZONES];
434 memset(max_zone_pfns, 0, sizeof(max_zone_pfns));
435 max_zone_pfns[ZONE_DMA] =
436 virt_to_phys((char *)MAX_DMA_ADDRESS) >> PAGE_SHIFT;
437 max_zone_pfns[ZONE_NORMAL] = max_low_pfn;
438 #ifdef CONFIG_HIGHMEM
439 max_zone_pfns[ZONE_HIGHMEM] = highend_pfn;
440 add_active_range(0, 0, highend_pfn);
442 add_active_range(0, 0, max_low_pfn);
445 free_area_init_nodes(max_zone_pfns);
448 extern unsigned long __init setup_memory(void);
449 extern void zone_sizes_init(void);
450 #endif /* !CONFIG_NEED_MULTIPLE_NODES */
452 static inline unsigned long long get_total_mem(void)
454 unsigned long long total;
456 total = max_low_pfn - min_low_pfn;
457 #ifdef CONFIG_HIGHMEM
458 total += highend_pfn - highstart_pfn;
461 return total << PAGE_SHIFT;
465 static void __init reserve_crashkernel(void)
467 unsigned long long total_mem;
468 unsigned long long crash_size, crash_base;
471 total_mem = get_total_mem();
473 ret = parse_crashkernel(boot_command_line, total_mem,
474 &crash_size, &crash_base);
475 if (ret == 0 && crash_size > 0) {
476 if (crash_base > 0) {
477 printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
478 "for crashkernel (System RAM: %ldMB)\n",
479 (unsigned long)(crash_size >> 20),
480 (unsigned long)(crash_base >> 20),
481 (unsigned long)(total_mem >> 20));
482 crashk_res.start = crash_base;
483 crashk_res.end = crash_base + crash_size - 1;
484 reserve_bootmem(crash_base, crash_size);
486 printk(KERN_INFO "crashkernel reservation failed - "
487 "you have to specify a base address\n");
491 static inline void __init reserve_crashkernel(void)
495 void __init setup_bootmem_allocator(void)
497 unsigned long bootmap_size;
499 * Initialize the boot-time allocator (with low memory only):
501 bootmap_size = init_bootmem(min_low_pfn, max_low_pfn);
503 register_bootmem_low_pages(max_low_pfn);
506 * Reserve the bootmem bitmap itself as well. We do this in two
507 * steps (first step was init_bootmem()) because this catches
508 * the (very unlikely) case of us accidentally initializing the
509 * bootmem allocator with an invalid RAM area.
511 reserve_bootmem(__pa_symbol(_text), (PFN_PHYS(min_low_pfn) +
512 bootmap_size + PAGE_SIZE-1) - __pa_symbol(_text));
515 * reserve physical page 0 - it's a special BIOS page on many boxes,
516 * enabling clean reboots, SMP operation, laptop functions.
518 reserve_bootmem(0, PAGE_SIZE);
520 /* reserve EBDA region, it's a 4K region */
521 reserve_ebda_region();
523 /* could be an AMD 768MPX chipset. Reserve a page before VGA to prevent
524 PCI prefetch into it (errata #56). Usually the page is reserved anyways,
525 unless you have no PS/2 mouse plugged in. */
526 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
527 boot_cpu_data.x86 == 6)
528 reserve_bootmem(0xa0000 - 4096, 4096);
532 * But first pinch a few for the stack/trampoline stuff
533 * FIXME: Don't need the extra page at 4K, but need to fix
534 * trampoline before removing it. (see the GDT stuff)
536 reserve_bootmem(PAGE_SIZE, PAGE_SIZE);
538 #ifdef CONFIG_ACPI_SLEEP
540 * Reserve low memory region for sleep support.
542 acpi_reserve_bootmem();
544 #ifdef CONFIG_X86_FIND_SMP_CONFIG
546 * Find and reserve possible boot-time SMP configuration:
551 #ifdef CONFIG_BLK_DEV_INITRD
552 if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
553 unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
554 unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
555 unsigned long ramdisk_end = ramdisk_image + ramdisk_size;
556 unsigned long end_of_lowmem = max_low_pfn << PAGE_SHIFT;
558 if (ramdisk_end <= end_of_lowmem) {
559 reserve_bootmem(ramdisk_image, ramdisk_size);
560 initrd_start = ramdisk_image + PAGE_OFFSET;
561 initrd_end = initrd_start+ramdisk_size;
563 printk(KERN_ERR "initrd extends beyond end of memory "
564 "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
565 ramdisk_end, end_of_lowmem);
570 reserve_crashkernel();
574 * The node 0 pgdat is initialized before all of these because
575 * it's needed for bootmem. node>0 pgdats have their virtual
576 * space allocated before the pagetables are in place to access
577 * them, so they can't be cleared then.
579 * This should all compile down to nothing when NUMA is off.
581 static void __init remapped_pgdat_init(void)
585 for_each_online_node(nid) {
587 memset(NODE_DATA(nid), 0, sizeof(struct pglist_data));
592 static void set_mca_bus(int x)
597 static void set_mca_bus(int x) { }
600 /* Overridden in paravirt.c if CONFIG_PARAVIRT */
601 char * __init __attribute__((weak)) memory_setup(void)
603 return machine_specific_memory_setup();
607 * Determine if we were loaded by an EFI loader. If so, then we have also been
608 * passed the efi memmap, systab, etc., so we should use these data structures
609 * for initialization. Note, the efi init code path is determined by the
610 * global efi_enabled. This allows the same kernel image to be used on existing
611 * systems (with a traditional BIOS) as well as on EFI systems.
613 void __init setup_arch(char **cmdline_p)
615 unsigned long max_low_pfn;
617 memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
618 pre_setup_arch_hook();
622 * FIXME: This isn't an official loader_type right
623 * now but does currently work with elilo.
624 * If we were configured as an EFI kernel, check to make
625 * sure that we were loaded correctly from elilo and that
626 * the system table is valid. If not, then initialize normally.
629 if ((boot_params.hdr.type_of_loader == 0x50) &&
630 boot_params.efi_info.efi_systab)
634 ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
635 screen_info = boot_params.screen_info;
636 edid_info = boot_params.edid_info;
637 apm_info.bios = boot_params.apm_bios_info;
638 ist_info = boot_params.ist_info;
639 saved_videomode = boot_params.hdr.vid_mode;
640 if( boot_params.sys_desc_table.length != 0 ) {
641 set_mca_bus(boot_params.sys_desc_table.table[3] & 0x2);
642 machine_id = boot_params.sys_desc_table.table[0];
643 machine_submodel_id = boot_params.sys_desc_table.table[1];
644 BIOS_revision = boot_params.sys_desc_table.table[2];
646 bootloader_type = boot_params.hdr.type_of_loader;
648 #ifdef CONFIG_BLK_DEV_RAM
649 rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
650 rd_prompt = ((boot_params.hdr.ram_size & RAMDISK_PROMPT_FLAG) != 0);
651 rd_doload = ((boot_params.hdr.ram_size & RAMDISK_LOAD_FLAG) != 0);
657 printk(KERN_INFO "BIOS-provided physical RAM map:\n");
658 print_memory_map(memory_setup());
663 if (!boot_params.hdr.root_flags)
664 root_mountflags &= ~MS_RDONLY;
665 init_mm.start_code = (unsigned long) _text;
666 init_mm.end_code = (unsigned long) _etext;
667 init_mm.end_data = (unsigned long) _edata;
668 init_mm.brk = init_pg_tables_end + PAGE_OFFSET;
670 code_resource.start = virt_to_phys(_text);
671 code_resource.end = virt_to_phys(_etext)-1;
672 data_resource.start = virt_to_phys(_etext);
673 data_resource.end = virt_to_phys(_edata)-1;
674 bss_resource.start = virt_to_phys(&__bss_start);
675 bss_resource.end = virt_to_phys(&__bss_stop)-1;
679 if (user_defined_memmap) {
680 printk(KERN_INFO "user-defined physical RAM map:\n");
681 print_memory_map("user");
684 strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
685 *cmdline_p = command_line;
687 max_low_pfn = setup_memory();
691 * Must be after max_low_pfn is determined, and before kernel
692 * pagetables are setup.
698 * NOTE: before this point _nobody_ is allowed to allocate
699 * any memory using the bootmem allocator. Although the
700 * allocator is now initialised only the first 8Mb of the kernel
701 * virtual address space has been mapped. All allocations before
702 * paging_init() has completed must use the alloc_bootmem_low_pages()
703 * variant (which allocates DMA'able memory) and care must be taken
704 * not to exceed the 8Mb limit.
708 smp_alloc_memory(); /* AP processor realmode stacks in low memory*/
711 remapped_pgdat_init();
716 * NOTE: at this point the bootmem allocator is fully available.
719 paravirt_post_allocator_init();
725 #ifdef CONFIG_X86_GENERICARCH
726 generic_apic_probe();
733 * Parse the ACPI tables for possible boot-time SMP configuration.
735 acpi_boot_table_init();
743 #if defined(CONFIG_SMP) && defined(CONFIG_X86_PC)
745 printk(KERN_WARNING "More than 8 CPUs detected and "
746 "CONFIG_X86_PC cannot handle it.\nUse "
747 "CONFIG_X86_GENERICARCH or CONFIG_X86_BIGSMP.\n");
750 #ifdef CONFIG_X86_LOCAL_APIC
751 if (smp_found_config)
755 e820_register_memory();
756 e820_mark_nosave_regions();
759 #if defined(CONFIG_VGA_CONSOLE)
760 if (!efi_enabled || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
761 conswitchp = &vga_con;
762 #elif defined(CONFIG_DUMMY_CONSOLE)
763 conswitchp = &dummy_con;
769 * Request address space for all standard resources
771 * This is called just before pcibios_init(), which is also a
772 * subsys_initcall, but is linked in later (in arch/i386/pci/common.c).
774 static int __init request_standard_resources(void)
778 printk(KERN_INFO "Setting up standard PCI resources\n");
780 efi_initialize_iomem_resources(&code_resource,
781 &data_resource, &bss_resource);
783 legacy_init_iomem_resources(&code_resource,
784 &data_resource, &bss_resource);
786 /* EFI systems may still have VGA */
787 request_resource(&iomem_resource, &video_ram_resource);
789 /* request I/O space for devices used on all i[345]86 PCs */
790 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
791 request_resource(&ioport_resource, &standard_io_resources[i]);
795 subsys_initcall(request_standard_resources);