xref: /linux-6.15/arch/x86/kernel/setup.c (revision 11a98f37)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
255f26239SYinghai Lu /*
355f26239SYinghai Lu  *  Copyright (C) 1995  Linus Torvalds
455f26239SYinghai Lu  *
555f26239SYinghai Lu  *  Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
655f26239SYinghai Lu  *
755f26239SYinghai Lu  *  Memory region support
855f26239SYinghai Lu  *	David Parsons <[email protected]>, July-August 1999
955f26239SYinghai Lu  *
1055f26239SYinghai Lu  *  Added E820 sanitization routine (removes overlapping memory regions);
1155f26239SYinghai Lu  *  Brian Moyle <[email protected]>, February 2001
1255f26239SYinghai Lu  *
1355f26239SYinghai Lu  * Moved CPU detection code to cpu/${cpu}.c
1455f26239SYinghai Lu  *    Patrick Mochel <[email protected]>, March 2002
1555f26239SYinghai Lu  *
1655f26239SYinghai Lu  *  Provisions for empty E820 memory regions (reported by certain BIOSes).
1755f26239SYinghai Lu  *  Alex Achenbach <[email protected]>, December 2002.
1855f26239SYinghai Lu  *
1955f26239SYinghai Lu  */
2055f26239SYinghai Lu 
2155f26239SYinghai Lu /*
2255f26239SYinghai Lu  * This file handles the architecture-dependent parts of initialization
2355f26239SYinghai Lu  */
2455f26239SYinghai Lu 
2555f26239SYinghai Lu #include <linux/sched.h>
2655f26239SYinghai Lu #include <linux/mm.h>
2755f26239SYinghai Lu #include <linux/mmzone.h>
2855f26239SYinghai Lu #include <linux/screen_info.h>
2955f26239SYinghai Lu #include <linux/ioport.h>
3055f26239SYinghai Lu #include <linux/acpi.h>
31efafc8b2SFeng Tang #include <linux/sfi.h>
3255f26239SYinghai Lu #include <linux/apm_bios.h>
3355f26239SYinghai Lu #include <linux/initrd.h>
3472d7c3b3SYinghai Lu #include <linux/memblock.h>
3555f26239SYinghai Lu #include <linux/seq_file.h>
3655f26239SYinghai Lu #include <linux/console.h>
3755f26239SYinghai Lu #include <linux/root_dev.h>
3855f26239SYinghai Lu #include <linux/highmem.h>
39186f4360SPaul Gortmaker #include <linux/export.h>
4055f26239SYinghai Lu #include <linux/efi.h>
4155f26239SYinghai Lu #include <linux/init.h>
4255f26239SYinghai Lu #include <linux/edd.h>
4355f26239SYinghai Lu #include <linux/iscsi_ibft.h>
4455f26239SYinghai Lu #include <linux/nodemask.h>
4555f26239SYinghai Lu #include <linux/kexec.h>
4655f26239SYinghai Lu #include <linux/dmi.h>
4755f26239SYinghai Lu #include <linux/pfn.h>
4855f26239SYinghai Lu #include <linux/pci.h>
4955f26239SYinghai Lu #include <asm/pci-direct.h>
5055f26239SYinghai Lu #include <linux/init_ohci1394_dma.h>
5155f26239SYinghai Lu #include <linux/kvm_para.h>
520a2b9a6eSMarek Szyprowski #include <linux/dma-contiguous.h>
533db3eb28SPetr Tesarik #include <xen/xen.h>
54e262e32dSDavid Howells #include <uapi/linux/mount.h>
5555f26239SYinghai Lu 
5655f26239SYinghai Lu #include <linux/errno.h>
5755f26239SYinghai Lu #include <linux/kernel.h>
5855f26239SYinghai Lu #include <linux/stddef.h>
5955f26239SYinghai Lu #include <linux/unistd.h>
6055f26239SYinghai Lu #include <linux/ptrace.h>
6155f26239SYinghai Lu #include <linux/user.h>
6255f26239SYinghai Lu #include <linux/delay.h>
6355f26239SYinghai Lu 
6455f26239SYinghai Lu #include <linux/kallsyms.h>
6555f26239SYinghai Lu #include <linux/cpufreq.h>
6655f26239SYinghai Lu #include <linux/dma-mapping.h>
6755f26239SYinghai Lu #include <linux/ctype.h>
6855f26239SYinghai Lu #include <linux/uaccess.h>
6955f26239SYinghai Lu 
7055f26239SYinghai Lu #include <linux/percpu.h>
7155f26239SYinghai Lu #include <linux/crash_dump.h>
7269575d38SShane Wang #include <linux/tboot.h>
73b3c869d3SJohn Stultz #include <linux/jiffies.h>
74b9d05200STom Lendacky #include <linux/mem_encrypt.h>
759ca5c8e6SDave Young #include <linux/sizes.h>
7655f26239SYinghai Lu 
771b5aeebfSLu Baolu #include <linux/usb/xhci-dbgp.h>
7855f26239SYinghai Lu #include <video/edid.h>
7955f26239SYinghai Lu 
8055f26239SYinghai Lu #include <asm/mtrr.h>
8155f26239SYinghai Lu #include <asm/apic.h>
82084ee1c6SJarkko Sakkinen #include <asm/realmode.h>
8366441bd3SIngo Molnar #include <asm/e820/api.h>
8455f26239SYinghai Lu #include <asm/mpspec.h>
8555f26239SYinghai Lu #include <asm/setup.h>
8655f26239SYinghai Lu #include <asm/efi.h>
878e6dafd6SIngo Molnar #include <asm/timer.h>
888e6dafd6SIngo Molnar #include <asm/i8259.h>
8955f26239SYinghai Lu #include <asm/sections.h>
9055f26239SYinghai Lu #include <asm/io_apic.h>
9155f26239SYinghai Lu #include <asm/ist.h>
921164dd00SIngo Molnar #include <asm/setup_arch.h>
9355f26239SYinghai Lu #include <asm/bios_ebda.h>
9455f26239SYinghai Lu #include <asm/cacheflush.h>
9555f26239SYinghai Lu #include <asm/processor.h>
9655f26239SYinghai Lu #include <asm/bugs.h>
97ef7f0d6aSAndrey Ryabinin #include <asm/kasan.h>
9855f26239SYinghai Lu 
9955f26239SYinghai Lu #include <asm/vsyscall.h>
1006e5385d4SJaswinder Singh Rajput #include <asm/cpu.h>
10155f26239SYinghai Lu #include <asm/desc.h>
10255f26239SYinghai Lu #include <asm/dma.h>
10346a7fa27SFUJITA Tomonori #include <asm/iommu.h>
1041d9b16d1SJoerg Roedel #include <asm/gart.h>
10555f26239SYinghai Lu #include <asm/mmu_context.h>
10655f26239SYinghai Lu #include <asm/proto.h>
10755f26239SYinghai Lu 
10855f26239SYinghai Lu #include <asm/paravirt.h>
10988b094fbSAlok Kataria #include <asm/hypervisor.h>
110fd699c76SAndres Salomon #include <asm/olpc_ofw.h>
11155f26239SYinghai Lu 
11255f26239SYinghai Lu #include <asm/percpu.h>
11355f26239SYinghai Lu #include <asm/topology.h>
11455f26239SYinghai Lu #include <asm/apicdef.h>
11523ac4ae8SAndreas Herrmann #include <asm/amd_nb.h>
116a2202aa2SYong Wang #include <asm/mce.h>
117f49aa448SJason Baron #include <asm/alternative.h>
118da6b737bSSebastian Andrzej Siewior #include <asm/prom.h>
1199a2bc335SBorislav Petkov #include <asm/microcode.h>
1200483e1faSThomas Garnier #include <asm/kaslr.h>
121ee9f8fceSJosh Poimboeuf #include <asm/unwind.h>
12255f26239SYinghai Lu 
1232b72394eSPekka Enberg /*
12466520ebcSJacob Shin  * max_low_pfn_mapped: highest direct mapped pfn under 4GB
12566520ebcSJacob Shin  * max_pfn_mapped:     highest direct mapped pfn over 4GB
12666520ebcSJacob Shin  *
12709821ff1SIngo Molnar  * The direct mapping only covers E820_TYPE_RAM regions, so the ranges and gaps are
12866520ebcSJacob Shin  * represented by pfn_mapped
1292b72394eSPekka Enberg  */
1302b72394eSPekka Enberg unsigned long max_low_pfn_mapped;
1312b72394eSPekka Enberg unsigned long max_pfn_mapped;
1322b72394eSPekka Enberg 
133e808bae2SThadeu Lima de Souza Cascardo #ifdef CONFIG_DMI
134796216a5SJeremy Fitzhardinge RESERVE_BRK(dmi_alloc, 65536);
135e808bae2SThadeu Lima de Souza Cascardo #endif
136796216a5SJeremy Fitzhardinge 
137c0b5842aSIngo Molnar 
13893dbda7cSJeremy Fitzhardinge static __initdata unsigned long _brk_start = (unsigned long)__brk_base;
13993dbda7cSJeremy Fitzhardinge unsigned long _brk_end = (unsigned long)__brk_base;
14093dbda7cSJeremy Fitzhardinge 
141217b8ce8SYinghai Lu struct boot_params boot_params;
142217b8ce8SYinghai Lu 
1434046d6e8SLinus Torvalds /*
1444046d6e8SLinus Torvalds  * Machine setup..
1454046d6e8SLinus Torvalds  */
1464046d6e8SLinus Torvalds static struct resource data_resource = {
1474046d6e8SLinus Torvalds 	.name	= "Kernel data",
1484046d6e8SLinus Torvalds 	.start	= 0,
1494046d6e8SLinus Torvalds 	.end	= 0,
1504046d6e8SLinus Torvalds 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
1514046d6e8SLinus Torvalds };
1524046d6e8SLinus Torvalds 
1534046d6e8SLinus Torvalds static struct resource code_resource = {
1544046d6e8SLinus Torvalds 	.name	= "Kernel code",
1554046d6e8SLinus Torvalds 	.start	= 0,
1564046d6e8SLinus Torvalds 	.end	= 0,
1574046d6e8SLinus Torvalds 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
1584046d6e8SLinus Torvalds };
1594046d6e8SLinus Torvalds 
1604046d6e8SLinus Torvalds static struct resource bss_resource = {
1614046d6e8SLinus Torvalds 	.name	= "Kernel bss",
1624046d6e8SLinus Torvalds 	.start	= 0,
1634046d6e8SLinus Torvalds 	.end	= 0,
1644046d6e8SLinus Torvalds 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
1654046d6e8SLinus Torvalds };
1664046d6e8SLinus Torvalds 
1674046d6e8SLinus Torvalds 
16855f26239SYinghai Lu #ifdef CONFIG_X86_32
1696415813bSMathias Krause /* cpu data as detected by the assembly code in head_32.S */
1706415813bSMathias Krause struct cpuinfo_x86 new_cpu_data;
1716415813bSMathias Krause 
17255f26239SYinghai Lu /* common cpu data for all cpus */
1736415813bSMathias Krause struct cpuinfo_x86 boot_cpu_data __read_mostly;
17455f26239SYinghai Lu EXPORT_SYMBOL(boot_cpu_data);
17555f26239SYinghai Lu 
17655f26239SYinghai Lu unsigned int def_to_bigsmp;
17755f26239SYinghai Lu 
17855f26239SYinghai Lu /* for MCA, but anyone else can use it if they want */
17955f26239SYinghai Lu unsigned int machine_id;
18055f26239SYinghai Lu unsigned int machine_submodel_id;
18155f26239SYinghai Lu unsigned int BIOS_revision;
18255f26239SYinghai Lu 
18355f26239SYinghai Lu struct apm_info apm_info;
18455f26239SYinghai Lu EXPORT_SYMBOL(apm_info);
18555f26239SYinghai Lu 
18655f26239SYinghai Lu #if defined(CONFIG_X86_SPEEDSTEP_SMI) || \
18755f26239SYinghai Lu 	defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE)
18855f26239SYinghai Lu struct ist_info ist_info;
18955f26239SYinghai Lu EXPORT_SYMBOL(ist_info);
19055f26239SYinghai Lu #else
19155f26239SYinghai Lu struct ist_info ist_info;
19255f26239SYinghai Lu #endif
19355f26239SYinghai Lu 
19455f26239SYinghai Lu #else
195162434e7SKirill A. Shutemov struct cpuinfo_x86 boot_cpu_data __read_mostly;
19655f26239SYinghai Lu EXPORT_SYMBOL(boot_cpu_data);
19755f26239SYinghai Lu #endif
19855f26239SYinghai Lu 
19955f26239SYinghai Lu 
20055f26239SYinghai Lu #if !defined(CONFIG_X86_PAE) || defined(CONFIG_X86_64)
201404f6aacSKees Cook __visible unsigned long mmu_cr4_features __ro_after_init;
20255f26239SYinghai Lu #else
203404f6aacSKees Cook __visible unsigned long mmu_cr4_features __ro_after_init = X86_CR4_PAE;
20455f26239SYinghai Lu #endif
20555f26239SYinghai Lu 
2065031296cSH. Peter Anvin /* Boot loader ID and version as integers, for the benefit of proc_dointvec */
2075031296cSH. Peter Anvin int bootloader_type, bootloader_version;
20855f26239SYinghai Lu 
20955f26239SYinghai Lu /*
21055f26239SYinghai Lu  * Setup options
21155f26239SYinghai Lu  */
21255f26239SYinghai Lu struct screen_info screen_info;
21355f26239SYinghai Lu EXPORT_SYMBOL(screen_info);
21455f26239SYinghai Lu struct edid_info edid_info;
21555f26239SYinghai Lu EXPORT_SYMBOL_GPL(edid_info);
21655f26239SYinghai Lu 
21755f26239SYinghai Lu extern int root_mountflags;
21855f26239SYinghai Lu 
21955f26239SYinghai Lu unsigned long saved_video_mode;
22055f26239SYinghai Lu 
22155f26239SYinghai Lu #define RAMDISK_IMAGE_START_MASK	0x07FF
22255f26239SYinghai Lu #define RAMDISK_PROMPT_FLAG		0x8000
22355f26239SYinghai Lu #define RAMDISK_LOAD_FLAG		0x4000
22455f26239SYinghai Lu 
22555f26239SYinghai Lu static char __initdata command_line[COMMAND_LINE_SIZE];
226516cbf37STim Bird #ifdef CONFIG_CMDLINE_BOOL
227516cbf37STim Bird static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
228516cbf37STim Bird #endif
22955f26239SYinghai Lu 
23055f26239SYinghai Lu #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
23155f26239SYinghai Lu struct edd edd;
23255f26239SYinghai Lu #ifdef CONFIG_EDD_MODULE
23355f26239SYinghai Lu EXPORT_SYMBOL(edd);
23455f26239SYinghai Lu #endif
23555f26239SYinghai Lu /**
23655f26239SYinghai Lu  * copy_edd() - Copy the BIOS EDD information
23755f26239SYinghai Lu  *              from boot_params into a safe place.
23855f26239SYinghai Lu  *
23955f26239SYinghai Lu  */
2409eaa192dSHelight.Xu static inline void __init copy_edd(void)
24155f26239SYinghai Lu {
24255f26239SYinghai Lu      memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer,
24355f26239SYinghai Lu 	    sizeof(edd.mbr_signature));
24455f26239SYinghai Lu      memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info));
24555f26239SYinghai Lu      edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries;
24655f26239SYinghai Lu      edd.edd_info_nr = boot_params.eddbuf_entries;
24755f26239SYinghai Lu }
24855f26239SYinghai Lu #else
2499eaa192dSHelight.Xu static inline void __init copy_edd(void)
25055f26239SYinghai Lu {
25155f26239SYinghai Lu }
25255f26239SYinghai Lu #endif
25355f26239SYinghai Lu 
2545368a2beSH. Peter Anvin void * __init extend_brk(size_t size, size_t align)
2555368a2beSH. Peter Anvin {
2565368a2beSH. Peter Anvin 	size_t mask = align - 1;
2575368a2beSH. Peter Anvin 	void *ret;
2585368a2beSH. Peter Anvin 
2595368a2beSH. Peter Anvin 	BUG_ON(_brk_start == 0);
2605368a2beSH. Peter Anvin 	BUG_ON(align & mask);
2615368a2beSH. Peter Anvin 
2625368a2beSH. Peter Anvin 	_brk_end = (_brk_end + mask) & ~mask;
2635368a2beSH. Peter Anvin 	BUG_ON((char *)(_brk_end + size) > __brk_limit);
2645368a2beSH. Peter Anvin 
2655368a2beSH. Peter Anvin 	ret = (void *)_brk_end;
2665368a2beSH. Peter Anvin 	_brk_end += size;
2675368a2beSH. Peter Anvin 
2685368a2beSH. Peter Anvin 	memset(ret, 0, size);
2695368a2beSH. Peter Anvin 
2705368a2beSH. Peter Anvin 	return ret;
2715368a2beSH. Peter Anvin }
2725368a2beSH. Peter Anvin 
273148b2098SYinghai Lu #ifdef CONFIG_X86_32
274e5f15b45SYinghai Lu static void __init cleanup_highmap(void)
275f005fe12SYinghai Lu {
276f005fe12SYinghai Lu }
277854c879fSPekka J Enberg #endif
278854c879fSPekka J Enberg 
2795368a2beSH. Peter Anvin static void __init reserve_brk(void)
2805368a2beSH. Peter Anvin {
2815368a2beSH. Peter Anvin 	if (_brk_end > _brk_start)
282fc8d7826SAlexander Duyck 		memblock_reserve(__pa_symbol(_brk_start),
283fc8d7826SAlexander Duyck 				 _brk_end - _brk_start);
2845368a2beSH. Peter Anvin 
2855368a2beSH. Peter Anvin 	/* Mark brk area as locked down and no longer taking any
2865368a2beSH. Peter Anvin 	   new allocations */
2875368a2beSH. Peter Anvin 	_brk_start = 0;
2885368a2beSH. Peter Anvin }
2895368a2beSH. Peter Anvin 
2905aa3d718SBorislav Petkov u64 relocated_ramdisk;
2915aa3d718SBorislav Petkov 
29255f26239SYinghai Lu #ifdef CONFIG_BLK_DEV_INITRD
29355f26239SYinghai Lu 
294a8a51a88SYinghai Lu static u64 __init get_ramdisk_image(void)
295a8a51a88SYinghai Lu {
296a8a51a88SYinghai Lu 	u64 ramdisk_image = boot_params.hdr.ramdisk_image;
297a8a51a88SYinghai Lu 
298ee92d815SYinghai Lu 	ramdisk_image |= (u64)boot_params.ext_ramdisk_image << 32;
299ee92d815SYinghai Lu 
300a8a51a88SYinghai Lu 	return ramdisk_image;
301a8a51a88SYinghai Lu }
302a8a51a88SYinghai Lu static u64 __init get_ramdisk_size(void)
303a8a51a88SYinghai Lu {
304a8a51a88SYinghai Lu 	u64 ramdisk_size = boot_params.hdr.ramdisk_size;
305a8a51a88SYinghai Lu 
306ee92d815SYinghai Lu 	ramdisk_size |= (u64)boot_params.ext_ramdisk_size << 32;
307ee92d815SYinghai Lu 
308a8a51a88SYinghai Lu 	return ramdisk_size;
309a8a51a88SYinghai Lu }
310a8a51a88SYinghai Lu 
31155f26239SYinghai Lu static void __init relocate_initrd(void)
31255f26239SYinghai Lu {
313c967da6aSYinghai Lu 	/* Assume only end is not page aligned */
314a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
315a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
316c967da6aSYinghai Lu 	u64 area_size     = PAGE_ALIGN(ramdisk_size);
31755f26239SYinghai Lu 
318e8c57d40SYinghai Lu 	/* We need to move the initrd down into directly mapped mem */
3195aa3d718SBorislav Petkov 	relocated_ramdisk = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
320e8c57d40SYinghai Lu 						   area_size, PAGE_SIZE);
32155f26239SYinghai Lu 
3225aa3d718SBorislav Petkov 	if (!relocated_ramdisk)
32355f26239SYinghai Lu 		panic("Cannot find place for new RAMDISK of size %lld\n",
32455f26239SYinghai Lu 		      ramdisk_size);
32555f26239SYinghai Lu 
326e8c57d40SYinghai Lu 	/* Note: this includes all the mem currently occupied by
32755f26239SYinghai Lu 	   the initrd, we rely on that fact to keep the data intact. */
3285aa3d718SBorislav Petkov 	memblock_reserve(relocated_ramdisk, area_size);
3295aa3d718SBorislav Petkov 	initrd_start = relocated_ramdisk + PAGE_OFFSET;
33055f26239SYinghai Lu 	initrd_end   = initrd_start + ramdisk_size;
331365811d6SBjorn Helgaas 	printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
3325aa3d718SBorislav Petkov 	       relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
33355f26239SYinghai Lu 
3345dd2c4bdSMark Salter 	copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size);
33555f26239SYinghai Lu 
336365811d6SBjorn Helgaas 	printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to"
337365811d6SBjorn Helgaas 		" [mem %#010llx-%#010llx]\n",
33855f26239SYinghai Lu 		ramdisk_image, ramdisk_image + ramdisk_size - 1,
3395aa3d718SBorislav Petkov 		relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
34055f26239SYinghai Lu }
34155f26239SYinghai Lu 
3421b8c78beSYinghai Lu static void __init early_reserve_initrd(void)
3431b8c78beSYinghai Lu {
3441b8c78beSYinghai Lu 	/* Assume only end is not page aligned */
345a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
346a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
3471b8c78beSYinghai Lu 	u64 ramdisk_end   = PAGE_ALIGN(ramdisk_image + ramdisk_size);
3481b8c78beSYinghai Lu 
3491b8c78beSYinghai Lu 	if (!boot_params.hdr.type_of_loader ||
3501b8c78beSYinghai Lu 	    !ramdisk_image || !ramdisk_size)
3511b8c78beSYinghai Lu 		return;		/* No initrd provided by bootloader */
3521b8c78beSYinghai Lu 
3531b8c78beSYinghai Lu 	memblock_reserve(ramdisk_image, ramdisk_end - ramdisk_image);
3541b8c78beSYinghai Lu }
35555f26239SYinghai Lu static void __init reserve_initrd(void)
35655f26239SYinghai Lu {
357c967da6aSYinghai Lu 	/* Assume only end is not page aligned */
358a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
359a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
360c967da6aSYinghai Lu 	u64 ramdisk_end   = PAGE_ALIGN(ramdisk_image + ramdisk_size);
361e8c57d40SYinghai Lu 	u64 mapped_size;
36255f26239SYinghai Lu 
36355f26239SYinghai Lu 	if (!boot_params.hdr.type_of_loader ||
36455f26239SYinghai Lu 	    !ramdisk_image || !ramdisk_size)
36555f26239SYinghai Lu 		return;		/* No initrd provided by bootloader */
36655f26239SYinghai Lu 
36755f26239SYinghai Lu 	initrd_start = 0;
36855f26239SYinghai Lu 
369595ad9afSYinghai Lu 	mapped_size = memblock_mem_size(max_pfn_mapped);
370e8c57d40SYinghai Lu 	if (ramdisk_size >= (mapped_size>>1))
371ab7b64e9SPeter Jones 		panic("initrd too large to handle, "
372ab7b64e9SPeter Jones 		       "disabling initrd (%lld needed, %lld available)\n",
373e8c57d40SYinghai Lu 		       ramdisk_size, mapped_size>>1);
37455f26239SYinghai Lu 
375365811d6SBjorn Helgaas 	printk(KERN_INFO "RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image,
376365811d6SBjorn Helgaas 			ramdisk_end - 1);
37755f26239SYinghai Lu 
37874f27655SYinghai Lu 	if (pfn_range_is_mapped(PFN_DOWN(ramdisk_image),
379e8c57d40SYinghai Lu 				PFN_DOWN(ramdisk_end))) {
380e8c57d40SYinghai Lu 		/* All are mapped, easy case */
38155f26239SYinghai Lu 		initrd_start = ramdisk_image + PAGE_OFFSET;
38255f26239SYinghai Lu 		initrd_end = initrd_start + ramdisk_size;
38355f26239SYinghai Lu 		return;
38455f26239SYinghai Lu 	}
38555f26239SYinghai Lu 
38655f26239SYinghai Lu 	relocate_initrd();
3878c5dd8f4SYinghai Lu 
38824aa0788STejun Heo 	memblock_free(ramdisk_image, ramdisk_end - ramdisk_image);
38955f26239SYinghai Lu }
390af06f8b7SLv Zheng 
39155f26239SYinghai Lu #else
3921b8c78beSYinghai Lu static void __init early_reserve_initrd(void)
3931b8c78beSYinghai Lu {
3941b8c78beSYinghai Lu }
39555f26239SYinghai Lu static void __init reserve_initrd(void)
39655f26239SYinghai Lu {
39755f26239SYinghai Lu }
39855f26239SYinghai Lu #endif /* CONFIG_BLK_DEV_INITRD */
39955f26239SYinghai Lu 
40029f784e3SYinghai Lu static void __init parse_setup_data(void)
401257b0fdeSYinghai Lu {
402257b0fdeSYinghai Lu 	struct setup_data *data;
40330e46b57SLinn Crosetto 	u64 pa_data, pa_next;
404257b0fdeSYinghai Lu 
405257b0fdeSYinghai Lu 	pa_data = boot_params.hdr.setup_data;
406257b0fdeSYinghai Lu 	while (pa_data) {
4077389882cSWANG Chao 		u32 data_len, data_type;
408f1c2b357SSebastian Andrzej Siewior 
4097389882cSWANG Chao 		data = early_memremap(pa_data, sizeof(*data));
410f1c2b357SSebastian Andrzej Siewior 		data_len = data->len + sizeof(struct setup_data);
41130e46b57SLinn Crosetto 		data_type = data->type;
41230e46b57SLinn Crosetto 		pa_next = data->next;
4138d4a40bcSJuergen Gross 		early_memunmap(data, sizeof(*data));
414f1c2b357SSebastian Andrzej Siewior 
41530e46b57SLinn Crosetto 		switch (data_type) {
416257b0fdeSYinghai Lu 		case SETUP_E820_EXT:
417914053c0SIngo Molnar 			e820__memory_setup_extended(pa_data, data_len);
418257b0fdeSYinghai Lu 			break;
419da6b737bSSebastian Andrzej Siewior 		case SETUP_DTB:
420da6b737bSSebastian Andrzej Siewior 			add_dtb(pa_data);
421257b0fdeSYinghai Lu 			break;
4221fec0533SDave Young 		case SETUP_EFI:
4231fec0533SDave Young 			parse_efi_setup(pa_data, data_len);
4241fec0533SDave Young 			break;
425257b0fdeSYinghai Lu 		default:
426257b0fdeSYinghai Lu 			break;
427257b0fdeSYinghai Lu 		}
42830e46b57SLinn Crosetto 		pa_data = pa_next;
429257b0fdeSYinghai Lu 	}
430257b0fdeSYinghai Lu }
431257b0fdeSYinghai Lu 
432a9ce6bc1SYinghai Lu static void __init memblock_x86_reserve_range_setup_data(void)
433a0a0becdSYinghai Lu {
434a0a0becdSYinghai Lu 	struct setup_data *data;
435a0a0becdSYinghai Lu 	u64 pa_data;
436a0a0becdSYinghai Lu 
437a0a0becdSYinghai Lu 	pa_data = boot_params.hdr.setup_data;
438a0a0becdSYinghai Lu 	while (pa_data) {
43988b4c146SJeremy Fitzhardinge 		data = early_memremap(pa_data, sizeof(*data));
44024aa0788STejun Heo 		memblock_reserve(pa_data, sizeof(*data) + data->len);
441a0a0becdSYinghai Lu 		pa_data = data->next;
4428d4a40bcSJuergen Gross 		early_memunmap(data, sizeof(*data));
443a0a0becdSYinghai Lu 	}
444a0a0becdSYinghai Lu }
445a0a0becdSYinghai Lu 
44655f26239SYinghai Lu /*
447ccb4defaSYinghai Lu  * --------- Crashkernel reservation ------------------------------
448ccb4defaSYinghai Lu  */
449ccb4defaSYinghai Lu 
4502965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
45132105f7fSBernhard Walle 
452606134f7SBorislav Petkov /* 16M alignment for crash kernel regions */
4539ca5c8e6SDave Young #define CRASH_ALIGN		SZ_16M
454606134f7SBorislav Petkov 
4557f8595bfSH. Peter Anvin /*
4568ff80fbeSBaoquan He  * Keep the crash kernel below this limit.
4578ff80fbeSBaoquan He  *
4588ff80fbeSBaoquan He  * On 32 bits earlier kernels would limit the kernel to the low 512 MiB
4598ff80fbeSBaoquan He  * due to mapping restrictions.
4608ff80fbeSBaoquan He  *
4618ff80fbeSBaoquan He  * On 64bit, kdump kernel need be restricted to be under 64TB, which is
462*11a98f37SCao jin  * the upper limit of system RAM in 4-level paging mode. Since the kdump
4638ff80fbeSBaoquan He  * jumping could be from 5-level to 4-level, the jumping will fail if
4648ff80fbeSBaoquan He  * kernel is put above 64TB, and there's no way to detect the paging mode
4658ff80fbeSBaoquan He  * of the kernel which will be loaded for dumping during the 1st kernel
4668ff80fbeSBaoquan He  * bootup.
4677f8595bfSH. Peter Anvin  */
4687f8595bfSH. Peter Anvin #ifdef CONFIG_X86_32
4699ca5c8e6SDave Young # define CRASH_ADDR_LOW_MAX	SZ_512M
4709ca5c8e6SDave Young # define CRASH_ADDR_HIGH_MAX	SZ_512M
4717f8595bfSH. Peter Anvin #else
4729ca5c8e6SDave Young # define CRASH_ADDR_LOW_MAX	SZ_4G
4738ff80fbeSBaoquan He # define CRASH_ADDR_HIGH_MAX	SZ_64T
4747f8595bfSH. Peter Anvin #endif
4757f8595bfSH. Peter Anvin 
476eb6db83dSBaoquan He static int __init reserve_crashkernel_low(void)
4770212f915SYinghai Lu {
4780212f915SYinghai Lu #ifdef CONFIG_X86_64
479f56d5578SBorislav Petkov 	unsigned long long base, low_base = 0, low_size = 0;
4800212f915SYinghai Lu 	unsigned long total_low_mem;
4810212f915SYinghai Lu 	int ret;
4820212f915SYinghai Lu 
4830212f915SYinghai Lu 	total_low_mem = memblock_mem_size(1UL << (32 - PAGE_SHIFT));
48497eac21bSBorislav Petkov 
485adbc742bSYinghai Lu 	/* crashkernel=Y,low */
48697eac21bSBorislav Petkov 	ret = parse_crashkernel_low(boot_command_line, total_low_mem, &low_size, &base);
487f56d5578SBorislav Petkov 	if (ret) {
488c729de8fSYinghai Lu 		/*
489392e879aSAndy Shevchenko 		 * two parts from kernel/dma/swiotlb.c:
49094fb9334SJoerg Roedel 		 * -swiotlb size: user-specified with swiotlb= or default.
49194fb9334SJoerg Roedel 		 *
49294fb9334SJoerg Roedel 		 * -swiotlb overflow buffer: now hardcoded to 32k. We round it
49394fb9334SJoerg Roedel 		 * to 8M for other buffers that may need to stay low too. Also
49494fb9334SJoerg Roedel 		 * make sure we allocate enough extra low memory so that we
49594fb9334SJoerg Roedel 		 * don't run out of DMA buffers for 32-bit devices.
496c729de8fSYinghai Lu 		 */
49794fb9334SJoerg Roedel 		low_size = max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
498c729de8fSYinghai Lu 	} else {
499adbc742bSYinghai Lu 		/* passed with crashkernel=0,low ? */
500c729de8fSYinghai Lu 		if (!low_size)
501eb6db83dSBaoquan He 			return 0;
502c729de8fSYinghai Lu 	}
5030212f915SYinghai Lu 
504fe2d48b8SJiri Bohac 	low_base = memblock_find_in_range(0, 1ULL << 32, low_size, CRASH_ALIGN);
5050212f915SYinghai Lu 	if (!low_base) {
506eb6db83dSBaoquan He 		pr_err("Cannot reserve %ldMB crashkernel low memory, please try smaller size.\n",
507eb6db83dSBaoquan He 		       (unsigned long)(low_size >> 20));
508eb6db83dSBaoquan He 		return -ENOMEM;
5090212f915SYinghai Lu 	}
5100212f915SYinghai Lu 
5116f376057SBorislav Petkov 	ret = memblock_reserve(low_base, low_size);
5126f376057SBorislav Petkov 	if (ret) {
5136f376057SBorislav Petkov 		pr_err("%s: Error reserving crashkernel low memblock.\n", __func__);
5146f376057SBorislav Petkov 		return ret;
5156f376057SBorislav Petkov 	}
51697eac21bSBorislav Petkov 
5170212f915SYinghai Lu 	pr_info("Reserving %ldMB of low memory at %ldMB for crashkernel (System low RAM: %ldMB)\n",
5180212f915SYinghai Lu 		(unsigned long)(low_size >> 20),
5190212f915SYinghai Lu 		(unsigned long)(low_base >> 20),
5200212f915SYinghai Lu 		(unsigned long)(total_low_mem >> 20));
52197eac21bSBorislav Petkov 
5220212f915SYinghai Lu 	crashk_low_res.start = low_base;
5230212f915SYinghai Lu 	crashk_low_res.end   = low_base + low_size - 1;
5240212f915SYinghai Lu 	insert_resource(&iomem_resource, &crashk_low_res);
5250212f915SYinghai Lu #endif
526eb6db83dSBaoquan He 	return 0;
5270212f915SYinghai Lu }
5280212f915SYinghai Lu 
52929f784e3SYinghai Lu static void __init reserve_crashkernel(void)
530ccb4defaSYinghai Lu {
531f56d5578SBorislav Petkov 	unsigned long long crash_size, crash_base, total_mem;
53255a20ee7SYinghai Lu 	bool high = false;
533ccb4defaSYinghai Lu 	int ret;
534ccb4defaSYinghai Lu 
53509c71bfdSDave Young 	total_mem = memblock_phys_mem_size();
536ccb4defaSYinghai Lu 
53755a20ee7SYinghai Lu 	/* crashkernel=XM */
53897eac21bSBorislav Petkov 	ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
53955a20ee7SYinghai Lu 	if (ret != 0 || crash_size <= 0) {
540adbc742bSYinghai Lu 		/* crashkernel=X,high */
54155a20ee7SYinghai Lu 		ret = parse_crashkernel_high(boot_command_line, total_mem,
54255a20ee7SYinghai Lu 					     &crash_size, &crash_base);
54332105f7fSBernhard Walle 		if (ret != 0 || crash_size <= 0)
54432105f7fSBernhard Walle 			return;
54555a20ee7SYinghai Lu 		high = true;
54655a20ee7SYinghai Lu 	}
54732105f7fSBernhard Walle 
5483db3eb28SPetr Tesarik 	if (xen_pv_domain()) {
5493db3eb28SPetr Tesarik 		pr_info("Ignoring crashkernel for a Xen PV domain\n");
5503db3eb28SPetr Tesarik 		return;
5513db3eb28SPetr Tesarik 	}
5523db3eb28SPetr Tesarik 
55332105f7fSBernhard Walle 	/* 0 means: find the address automatically */
554b9ac3849SDave Young 	if (!crash_base) {
5559f4c1396SYinghai Lu 		/*
556a8d4c824SXunlei Pang 		 * Set CRASH_ADDR_LOW_MAX upper bound for crash memory,
557b9ac3849SDave Young 		 * crashkernel=x,high reserves memory over 4G, also allocates
558b9ac3849SDave Young 		 * 256M extra low memory for DMA buffers and swiotlb.
559b9ac3849SDave Young 		 * But the extra memory is not required for all machines.
560b9ac3849SDave Young 		 * So try low memory first and fall back to high memory
561b9ac3849SDave Young 		 * unless "crashkernel=size[KMG],high" is specified.
5629f4c1396SYinghai Lu 		 */
563b9ac3849SDave Young 		if (!high)
564606134f7SBorislav Petkov 			crash_base = memblock_find_in_range(CRASH_ALIGN,
565b9ac3849SDave Young 						CRASH_ADDR_LOW_MAX,
566b9ac3849SDave Young 						crash_size, CRASH_ALIGN);
567b9ac3849SDave Young 		if (!crash_base)
568b9ac3849SDave Young 			crash_base = memblock_find_in_range(CRASH_ALIGN,
569b9ac3849SDave Young 						CRASH_ADDR_HIGH_MAX,
570606134f7SBorislav Petkov 						crash_size, CRASH_ALIGN);
5711f5026a7STejun Heo 		if (!crash_base) {
57244280733SYinghai Lu 			pr_info("crashkernel reservation failed - No suitable area found.\n");
573ccb4defaSYinghai Lu 			return;
574ccb4defaSYinghai Lu 		}
57532105f7fSBernhard Walle 	} else {
57644280733SYinghai Lu 		unsigned long long start;
57744280733SYinghai Lu 
5789f4c1396SYinghai Lu 		start = memblock_find_in_range(crash_base,
57997eac21bSBorislav Petkov 					       crash_base + crash_size,
58097eac21bSBorislav Petkov 					       crash_size, 1 << 20);
58144280733SYinghai Lu 		if (start != crash_base) {
58244280733SYinghai Lu 			pr_info("crashkernel reservation failed - memory is in use.\n");
583ccb4defaSYinghai Lu 			return;
584ccb4defaSYinghai Lu 		}
58532105f7fSBernhard Walle 	}
5866f376057SBorislav Petkov 	ret = memblock_reserve(crash_base, crash_size);
5876f376057SBorislav Petkov 	if (ret) {
5886f376057SBorislav Petkov 		pr_err("%s: Error reserving crashkernel memblock.\n", __func__);
5896f376057SBorislav Petkov 		return;
5906f376057SBorislav Petkov 	}
591ccb4defaSYinghai Lu 
592eb6db83dSBaoquan He 	if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) {
593eb6db83dSBaoquan He 		memblock_free(crash_base, crash_size);
594eb6db83dSBaoquan He 		return;
595eb6db83dSBaoquan He 	}
596eb6db83dSBaoquan He 
597f56d5578SBorislav Petkov 	pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
598ccb4defaSYinghai Lu 		(unsigned long)(crash_size >> 20),
599ccb4defaSYinghai Lu 		(unsigned long)(crash_base >> 20),
600ccb4defaSYinghai Lu 		(unsigned long)(total_mem >> 20));
601ccb4defaSYinghai Lu 
602ccb4defaSYinghai Lu 	crashk_res.start = crash_base;
603ccb4defaSYinghai Lu 	crashk_res.end   = crash_base + crash_size - 1;
604ccb4defaSYinghai Lu 	insert_resource(&iomem_resource, &crashk_res);
605ccb4defaSYinghai Lu }
606ccb4defaSYinghai Lu #else
60729f784e3SYinghai Lu static void __init reserve_crashkernel(void)
608ccb4defaSYinghai Lu {
609ccb4defaSYinghai Lu }
610ccb4defaSYinghai Lu #endif
611ccb4defaSYinghai Lu 
612bdba0e70SYinghai Lu static struct resource standard_io_resources[] = {
613bdba0e70SYinghai Lu 	{ .name = "dma1", .start = 0x00, .end = 0x1f,
614bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
615bdba0e70SYinghai Lu 	{ .name = "pic1", .start = 0x20, .end = 0x21,
616bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
617bdba0e70SYinghai Lu 	{ .name = "timer0", .start = 0x40, .end = 0x43,
618bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
619bdba0e70SYinghai Lu 	{ .name = "timer1", .start = 0x50, .end = 0x53,
620bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
621bdba0e70SYinghai Lu 	{ .name = "keyboard", .start = 0x60, .end = 0x60,
622bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
623bdba0e70SYinghai Lu 	{ .name = "keyboard", .start = 0x64, .end = 0x64,
624bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
625bdba0e70SYinghai Lu 	{ .name = "dma page reg", .start = 0x80, .end = 0x8f,
626bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
627bdba0e70SYinghai Lu 	{ .name = "pic2", .start = 0xa0, .end = 0xa1,
628bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
629bdba0e70SYinghai Lu 	{ .name = "dma2", .start = 0xc0, .end = 0xdf,
630bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
631bdba0e70SYinghai Lu 	{ .name = "fpu", .start = 0xf0, .end = 0xff,
632bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO }
633bdba0e70SYinghai Lu };
634bdba0e70SYinghai Lu 
6358fee697dSThomas Gleixner void __init reserve_standard_io_resources(void)
636bdba0e70SYinghai Lu {
637bdba0e70SYinghai Lu 	int i;
638bdba0e70SYinghai Lu 
639bdba0e70SYinghai Lu 	/* request I/O space for devices used on all i[345]86 PCs */
640bdba0e70SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
641bdba0e70SYinghai Lu 		request_resource(&ioport_resource, &standard_io_resources[i]);
642bdba0e70SYinghai Lu 
643bdba0e70SYinghai Lu }
644bdba0e70SYinghai Lu 
645042be38eSYinghai Lu static __init void reserve_ibft_region(void)
646042be38eSYinghai Lu {
647042be38eSYinghai Lu 	unsigned long addr, size = 0;
648042be38eSYinghai Lu 
649042be38eSYinghai Lu 	addr = find_ibft_region(&size);
650042be38eSYinghai Lu 
651042be38eSYinghai Lu 	if (size)
65224aa0788STejun Heo 		memblock_reserve(addr, size);
653042be38eSYinghai Lu }
654042be38eSYinghai Lu 
655a9acc536SJesse Barnes static bool __init snb_gfx_workaround_needed(void)
656a9acc536SJesse Barnes {
657e43b3cecSH. Peter Anvin #ifdef CONFIG_PCI
658a9acc536SJesse Barnes 	int i;
659a9acc536SJesse Barnes 	u16 vendor, devid;
660ab3cd867SH. Peter Anvin 	static const __initconst u16 snb_ids[] = {
661a9acc536SJesse Barnes 		0x0102,
662a9acc536SJesse Barnes 		0x0112,
663a9acc536SJesse Barnes 		0x0122,
664a9acc536SJesse Barnes 		0x0106,
665a9acc536SJesse Barnes 		0x0116,
666a9acc536SJesse Barnes 		0x0126,
667a9acc536SJesse Barnes 		0x010a,
668a9acc536SJesse Barnes 	};
669a9acc536SJesse Barnes 
670a9acc536SJesse Barnes 	/* Assume no if something weird is going on with PCI */
671a9acc536SJesse Barnes 	if (!early_pci_allowed())
672a9acc536SJesse Barnes 		return false;
673a9acc536SJesse Barnes 
674a9acc536SJesse Barnes 	vendor = read_pci_config_16(0, 2, 0, PCI_VENDOR_ID);
675a9acc536SJesse Barnes 	if (vendor != 0x8086)
676a9acc536SJesse Barnes 		return false;
677a9acc536SJesse Barnes 
678a9acc536SJesse Barnes 	devid = read_pci_config_16(0, 2, 0, PCI_DEVICE_ID);
679a9acc536SJesse Barnes 	for (i = 0; i < ARRAY_SIZE(snb_ids); i++)
680a9acc536SJesse Barnes 		if (devid == snb_ids[i])
681a9acc536SJesse Barnes 			return true;
682e43b3cecSH. Peter Anvin #endif
683a9acc536SJesse Barnes 
684a9acc536SJesse Barnes 	return false;
685a9acc536SJesse Barnes }
686a9acc536SJesse Barnes 
687a9acc536SJesse Barnes /*
688a9acc536SJesse Barnes  * Sandy Bridge graphics has trouble with certain ranges, exclude
689a9acc536SJesse Barnes  * them from allocation.
690a9acc536SJesse Barnes  */
691a9acc536SJesse Barnes static void __init trim_snb_memory(void)
692a9acc536SJesse Barnes {
693ab3cd867SH. Peter Anvin 	static const __initconst unsigned long bad_pages[] = {
694a9acc536SJesse Barnes 		0x20050000,
695a9acc536SJesse Barnes 		0x20110000,
696a9acc536SJesse Barnes 		0x20130000,
697a9acc536SJesse Barnes 		0x20138000,
698a9acc536SJesse Barnes 		0x40004000,
699a9acc536SJesse Barnes 	};
700a9acc536SJesse Barnes 	int i;
701a9acc536SJesse Barnes 
702a9acc536SJesse Barnes 	if (!snb_gfx_workaround_needed())
703a9acc536SJesse Barnes 		return;
704a9acc536SJesse Barnes 
705a9acc536SJesse Barnes 	printk(KERN_DEBUG "reserving inaccessible SNB gfx pages\n");
706a9acc536SJesse Barnes 
707a9acc536SJesse Barnes 	/*
708a9acc536SJesse Barnes 	 * Reserve all memory below the 1 MB mark that has not
709a9acc536SJesse Barnes 	 * already been reserved.
710a9acc536SJesse Barnes 	 */
711a9acc536SJesse Barnes 	memblock_reserve(0, 1<<20);
712a9acc536SJesse Barnes 
713a9acc536SJesse Barnes 	for (i = 0; i < ARRAY_SIZE(bad_pages); i++) {
714a9acc536SJesse Barnes 		if (memblock_reserve(bad_pages[i], PAGE_SIZE))
715a9acc536SJesse Barnes 			printk(KERN_WARNING "failed to reserve 0x%08lx\n",
716a9acc536SJesse Barnes 			       bad_pages[i]);
717a9acc536SJesse Barnes 	}
718a9acc536SJesse Barnes }
719a9acc536SJesse Barnes 
720a9acc536SJesse Barnes /*
721a9acc536SJesse Barnes  * Here we put platform-specific memory range workarounds, i.e.
722a9acc536SJesse Barnes  * memory known to be corrupt or otherwise in need to be reserved on
723a9acc536SJesse Barnes  * specific platforms.
724a9acc536SJesse Barnes  *
725a9acc536SJesse Barnes  * If this gets used more widely it could use a real dispatch mechanism.
726a9acc536SJesse Barnes  */
727a9acc536SJesse Barnes static void __init trim_platform_memory_ranges(void)
728a9acc536SJesse Barnes {
729a9acc536SJesse Barnes 	trim_snb_memory();
730a9acc536SJesse Barnes }
731a9acc536SJesse Barnes 
7321b5576e6SYinghai Lu static void __init trim_bios_range(void)
7331b5576e6SYinghai Lu {
7341b5576e6SYinghai Lu 	/*
7351b5576e6SYinghai Lu 	 * A special case is the first 4Kb of memory;
7361b5576e6SYinghai Lu 	 * This is a BIOS owned area, not kernel ram, but generally
7371b5576e6SYinghai Lu 	 * not listed as such in the E820 table.
738d0cd7425SH. Peter Anvin 	 *
739d0cd7425SH. Peter Anvin 	 * This typically reserves additional memory (64KiB by default)
740d0cd7425SH. Peter Anvin 	 * since some BIOSes are known to corrupt low memory.  See the
7419ea77bdbSH. Peter Anvin 	 * Kconfig help text for X86_RESERVE_LOW.
7421b5576e6SYinghai Lu 	 */
74309821ff1SIngo Molnar 	e820__range_update(0, PAGE_SIZE, E820_TYPE_RAM, E820_TYPE_RESERVED);
744d0cd7425SH. Peter Anvin 
7451b5576e6SYinghai Lu 	/*
746*11a98f37SCao jin 	 * special case: Some BIOSes report the PC BIOS
747*11a98f37SCao jin 	 * area (640Kb -> 1Mb) as RAM even though it is not.
7481b5576e6SYinghai Lu 	 * take them out.
7491b5576e6SYinghai Lu 	 */
75009821ff1SIngo Molnar 	e820__range_remove(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_TYPE_RAM, 1);
751a9acc536SJesse Barnes 
752f9748fa0SIngo Molnar 	e820__update_table(e820_table);
7531b5576e6SYinghai Lu }
7541b5576e6SYinghai Lu 
755b422a309SYinghai Lu /* called before trim_bios_range() to spare extra sanitize */
756b422a309SYinghai Lu static void __init e820_add_kernel_range(void)
757b422a309SYinghai Lu {
758b422a309SYinghai Lu 	u64 start = __pa_symbol(_text);
759b422a309SYinghai Lu 	u64 size = __pa_symbol(_end) - start;
760b422a309SYinghai Lu 
761b422a309SYinghai Lu 	/*
76209821ff1SIngo Molnar 	 * Complain if .text .data and .bss are not marked as E820_TYPE_RAM and
763b422a309SYinghai Lu 	 * attempt to fix it by adding the range. We may have a confused BIOS,
764b422a309SYinghai Lu 	 * or the user may have used memmap=exactmap or memmap=xxM$yyM to
765b422a309SYinghai Lu 	 * exclude kernel range. If we really are running on top non-RAM,
766b422a309SYinghai Lu 	 * we will crash later anyways.
767b422a309SYinghai Lu 	 */
76809821ff1SIngo Molnar 	if (e820__mapped_all(start, start + size, E820_TYPE_RAM))
769b422a309SYinghai Lu 		return;
770b422a309SYinghai Lu 
77109821ff1SIngo Molnar 	pr_warn(".text .data .bss are not marked as E820_TYPE_RAM!\n");
77209821ff1SIngo Molnar 	e820__range_remove(start, size, E820_TYPE_RAM, 0);
77309821ff1SIngo Molnar 	e820__range_add(start, size, E820_TYPE_RAM);
774b422a309SYinghai Lu }
775b422a309SYinghai Lu 
77695c96084SH. Peter Anvin static unsigned reserve_low = CONFIG_X86_RESERVE_LOW << 10;
77795c96084SH. Peter Anvin 
7789ea77bdbSH. Peter Anvin static int __init parse_reservelow(char *p)
7799ea77bdbSH. Peter Anvin {
7809ea77bdbSH. Peter Anvin 	unsigned long long size;
7819ea77bdbSH. Peter Anvin 
7829ea77bdbSH. Peter Anvin 	if (!p)
7839ea77bdbSH. Peter Anvin 		return -EINVAL;
7849ea77bdbSH. Peter Anvin 
7859ea77bdbSH. Peter Anvin 	size = memparse(p, &p);
7869ea77bdbSH. Peter Anvin 
7879ea77bdbSH. Peter Anvin 	if (size < 4096)
7889ea77bdbSH. Peter Anvin 		size = 4096;
7899ea77bdbSH. Peter Anvin 
7909ea77bdbSH. Peter Anvin 	if (size > 640*1024)
7919ea77bdbSH. Peter Anvin 		size = 640*1024;
7929ea77bdbSH. Peter Anvin 
7939ea77bdbSH. Peter Anvin 	reserve_low = size;
7949ea77bdbSH. Peter Anvin 
7959ea77bdbSH. Peter Anvin 	return 0;
7969ea77bdbSH. Peter Anvin }
7979ea77bdbSH. Peter Anvin 
7989ea77bdbSH. Peter Anvin early_param("reservelow", parse_reservelow);
7999ea77bdbSH. Peter Anvin 
80095c96084SH. Peter Anvin static void __init trim_low_memory_range(void)
80195c96084SH. Peter Anvin {
80295c96084SH. Peter Anvin 	memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE));
80395c96084SH. Peter Anvin }
80495c96084SH. Peter Anvin 
8055394f80fSJeremy Fitzhardinge /*
806f32360efSKees Cook  * Dump out kernel offset information on panic.
807f32360efSKees Cook  */
808f32360efSKees Cook static int
809f32360efSKees Cook dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
810f32360efSKees Cook {
81178cac48cSBorislav Petkov 	if (kaslr_enabled()) {
81278cac48cSBorislav Petkov 		pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
8134545c898SJiri Kosina 			 kaslr_offset(),
81478cac48cSBorislav Petkov 			 __START_KERNEL,
81578cac48cSBorislav Petkov 			 __START_KERNEL_map,
81678cac48cSBorislav Petkov 			 MODULES_VADDR-1);
81778cac48cSBorislav Petkov 	} else {
81878cac48cSBorislav Petkov 		pr_emerg("Kernel Offset: disabled\n");
81978cac48cSBorislav Petkov 	}
820f32360efSKees Cook 
821f32360efSKees Cook 	return 0;
822f32360efSKees Cook }
823f32360efSKees Cook 
824f32360efSKees Cook /*
82555f26239SYinghai Lu  * Determine if we were loaded by an EFI loader.  If so, then we have also been
82655f26239SYinghai Lu  * passed the efi memmap, systab, etc., so we should use these data structures
82755f26239SYinghai Lu  * for initialization.  Note, the efi init code path is determined by the
82855f26239SYinghai Lu  * global efi_enabled. This allows the same kernel image to be used on existing
82955f26239SYinghai Lu  * systems (with a traditional BIOS) as well as on EFI systems.
83055f26239SYinghai Lu  */
83155f26239SYinghai Lu /*
83255f26239SYinghai Lu  * setup_arch - architecture-specific boot-time initializations
83355f26239SYinghai Lu  *
83455f26239SYinghai Lu  * Note: On x86_64, fixmaps are ready for use even before this is called.
83555f26239SYinghai Lu  */
83655f26239SYinghai Lu 
83755f26239SYinghai Lu void __init setup_arch(char **cmdline_p)
83855f26239SYinghai Lu {
839c603a309SThomas Lendacky 	/*
840c603a309SThomas Lendacky 	 * Reserve the memory occupied by the kernel between _text and
841c603a309SThomas Lendacky 	 * __end_of_kernel_reserve symbols. Any kernel sections after the
842c603a309SThomas Lendacky 	 * __end_of_kernel_reserve symbol must be explicitly reserved with a
843c603a309SThomas Lendacky 	 * separate memblock_reserve() or they will be discarded.
844c603a309SThomas Lendacky 	 */
8456c902b65SYinghai Lu 	memblock_reserve(__pa_symbol(_text),
846c603a309SThomas Lendacky 			 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
8476c902b65SYinghai Lu 
84810a70416SAndi Kleen 	/*
84910a70416SAndi Kleen 	 * Make sure page 0 is always reserved because on systems with
85010a70416SAndi Kleen 	 * L1TF its contents can be leaked to user processes.
85110a70416SAndi Kleen 	 */
85210a70416SAndi Kleen 	memblock_reserve(0, PAGE_SIZE);
85310a70416SAndi Kleen 
8541b8c78beSYinghai Lu 	early_reserve_initrd();
8551b8c78beSYinghai Lu 
8566c902b65SYinghai Lu 	/*
8576c902b65SYinghai Lu 	 * At this point everything still needed from the boot loader
8586c902b65SYinghai Lu 	 * or BIOS or kernel text should be early reserved or marked not
8596c902b65SYinghai Lu 	 * RAM in e820. All other memory is free game.
8606c902b65SYinghai Lu 	 */
8616c902b65SYinghai Lu 
86255f26239SYinghai Lu #ifdef CONFIG_X86_32
86355f26239SYinghai Lu 	memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
864b40827faSBorislav Petkov 
865b40827faSBorislav Petkov 	/*
866b40827faSBorislav Petkov 	 * copy kernel address range established so far and switch
867b40827faSBorislav Petkov 	 * to the proper swapper page table
868b40827faSBorislav Petkov 	 */
869b40827faSBorislav Petkov 	clone_pgd_range(swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
870b40827faSBorislav Petkov 			initial_page_table + KERNEL_PGD_BOUNDARY,
871b40827faSBorislav Petkov 			KERNEL_PGD_PTRS);
872b40827faSBorislav Petkov 
873b40827faSBorislav Petkov 	load_cr3(swapper_pg_dir);
8742075244fSBryan O'Donoghue 	/*
8752075244fSBryan O'Donoghue 	 * Note: Quark X1000 CPUs advertise PGE incorrectly and require
8762075244fSBryan O'Donoghue 	 * a cr3 based tlb flush, so the following __flush_tlb_all()
8772075244fSBryan O'Donoghue 	 * will not flush anything because the cpu quirk which clears
8782075244fSBryan O'Donoghue 	 * X86_FEATURE_PGE has not been invoked yet. Though due to the
8792075244fSBryan O'Donoghue 	 * load_cr3() above the TLB has been flushed already. The
8802075244fSBryan O'Donoghue 	 * quirk is invoked before subsequent calls to __flush_tlb_all()
8812075244fSBryan O'Donoghue 	 * so proper operation is guaranteed.
8822075244fSBryan O'Donoghue 	 */
883b40827faSBorislav Petkov 	__flush_tlb_all();
88455f26239SYinghai Lu #else
88555f26239SYinghai Lu 	printk(KERN_INFO "Command line: %s\n", boot_command_line);
886162434e7SKirill A. Shutemov 	boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS;
88755f26239SYinghai Lu #endif
88855f26239SYinghai Lu 
8899863c90fSAlok Kataria 	/*
8909863c90fSAlok Kataria 	 * If we have OLPC OFW, we might end up relocating the fixmap due to
8919863c90fSAlok Kataria 	 * reserve_top(), so do this before touching the ioremap area.
8929863c90fSAlok Kataria 	 */
893fd699c76SAndres Salomon 	olpc_ofw_detect();
894fd699c76SAndres Salomon 
895433f8924SThomas Gleixner 	idt_setup_early_traps();
8969e882c92SJeremy Fitzhardinge 	early_cpu_init();
8978990cac6SPavel Tatashin 	arch_init_ideal_nops();
8988990cac6SPavel Tatashin 	jump_label_init();
8991a98fd14SJeremy Fitzhardinge 	early_ioremap_init();
9001a98fd14SJeremy Fitzhardinge 
901fd699c76SAndres Salomon 	setup_olpc_ofw_pgd();
902fd699c76SAndres Salomon 
90355f26239SYinghai Lu 	ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
90455f26239SYinghai Lu 	screen_info = boot_params.screen_info;
90555f26239SYinghai Lu 	edid_info = boot_params.edid_info;
90655f26239SYinghai Lu #ifdef CONFIG_X86_32
90755f26239SYinghai Lu 	apm_info.bios = boot_params.apm_bios_info;
90855f26239SYinghai Lu 	ist_info = boot_params.ist_info;
90955f26239SYinghai Lu #endif
91055f26239SYinghai Lu 	saved_video_mode = boot_params.hdr.vid_mode;
91155f26239SYinghai Lu 	bootloader_type = boot_params.hdr.type_of_loader;
9125031296cSH. Peter Anvin 	if ((bootloader_type >> 4) == 0xe) {
9135031296cSH. Peter Anvin 		bootloader_type &= 0xf;
9145031296cSH. Peter Anvin 		bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4;
9155031296cSH. Peter Anvin 	}
9165031296cSH. Peter Anvin 	bootloader_version  = bootloader_type & 0xf;
9175031296cSH. Peter Anvin 	bootloader_version |= boot_params.hdr.ext_loader_ver << 4;
91855f26239SYinghai Lu 
91955f26239SYinghai Lu #ifdef CONFIG_BLK_DEV_RAM
92055f26239SYinghai Lu 	rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
92155f26239SYinghai Lu 	rd_prompt = ((boot_params.hdr.ram_size & RAMDISK_PROMPT_FLAG) != 0);
92255f26239SYinghai Lu 	rd_doload = ((boot_params.hdr.ram_size & RAMDISK_LOAD_FLAG) != 0);
92355f26239SYinghai Lu #endif
92455f26239SYinghai Lu #ifdef CONFIG_EFI
92555f26239SYinghai Lu 	if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
9269402973dSDaniel Kiper 		     EFI32_LOADER_SIGNATURE, 4)) {
9273e909599SMatt Fleming 		set_bit(EFI_BOOT, &efi.flags);
9281adbfa35SOlof Johansson 	} else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
9299402973dSDaniel Kiper 		     EFI64_LOADER_SIGNATURE, 4)) {
9303e909599SMatt Fleming 		set_bit(EFI_BOOT, &efi.flags);
9313e909599SMatt Fleming 		set_bit(EFI_64BIT, &efi.flags);
93255f26239SYinghai Lu 	}
93355f26239SYinghai Lu #endif
93455f26239SYinghai Lu 
93542bbdb43SThomas Gleixner 	x86_init.oem.arch_setup();
93655f26239SYinghai Lu 
937419afdf5SBjorn Helgaas 	iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
938103e2063SIngo Molnar 	e820__memory_setup();
93928bb2237SYinghai Lu 	parse_setup_data();
94028bb2237SYinghai Lu 
94155f26239SYinghai Lu 	copy_edd();
94255f26239SYinghai Lu 
94355f26239SYinghai Lu 	if (!boot_params.hdr.root_flags)
94455f26239SYinghai Lu 		root_mountflags &= ~MS_RDONLY;
94555f26239SYinghai Lu 	init_mm.start_code = (unsigned long) _text;
94655f26239SYinghai Lu 	init_mm.end_code = (unsigned long) _etext;
94755f26239SYinghai Lu 	init_mm.end_data = (unsigned long) _edata;
94893dbda7cSJeremy Fitzhardinge 	init_mm.brk = _brk_end;
94955f26239SYinghai Lu 
950fe3d197fSDave Hansen 	mpx_mm_init(&init_mm);
951fe3d197fSDave Hansen 
9524046d6e8SLinus Torvalds 	code_resource.start = __pa_symbol(_text);
9534046d6e8SLinus Torvalds 	code_resource.end = __pa_symbol(_etext)-1;
9544046d6e8SLinus Torvalds 	data_resource.start = __pa_symbol(_etext);
9554046d6e8SLinus Torvalds 	data_resource.end = __pa_symbol(_edata)-1;
9564046d6e8SLinus Torvalds 	bss_resource.start = __pa_symbol(__bss_start);
9574046d6e8SLinus Torvalds 	bss_resource.end = __pa_symbol(__bss_stop)-1;
9584046d6e8SLinus Torvalds 
959516cbf37STim Bird #ifdef CONFIG_CMDLINE_BOOL
960516cbf37STim Bird #ifdef CONFIG_CMDLINE_OVERRIDE
961516cbf37STim Bird 	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
962516cbf37STim Bird #else
963516cbf37STim Bird 	if (builtin_cmdline[0]) {
964516cbf37STim Bird 		/* append boot loader cmdline to builtin */
965516cbf37STim Bird 		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
966516cbf37STim Bird 		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
967516cbf37STim Bird 		strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
968516cbf37STim Bird 	}
969516cbf37STim Bird #endif
970516cbf37STim Bird #endif
971516cbf37STim Bird 
972eda6da92SYinghai Lu 	strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
973eda6da92SYinghai Lu 	*cmdline_p = command_line;
974eda6da92SYinghai Lu 
975eda6da92SYinghai Lu 	/*
9764b0f3b81SKees Cook 	 * x86_configure_nx() is called before parse_early_param() to detect
9774b0f3b81SKees Cook 	 * whether hardware doesn't support NX (so that the early EHCI debug
9784b0f3b81SKees Cook 	 * console setup can safely call set_fixmap()). It may then be called
9794b0f3b81SKees Cook 	 * again from within noexec_setup() during parsing early parameters
9804b0f3b81SKees Cook 	 * to honor the respective command line option.
981eda6da92SYinghai Lu 	 */
9824763ed4dSH. Peter Anvin 	x86_configure_nx();
983eda6da92SYinghai Lu 
984eda6da92SYinghai Lu 	parse_early_param();
985eda6da92SYinghai Lu 
986835bcec5SDave Young 	if (efi_enabled(EFI_BOOT))
987835bcec5SDave Young 		efi_memblock_x86_reserve_range();
98839fa104dSReza Arbab #ifdef CONFIG_MEMORY_HOTPLUG
98939fa104dSReza Arbab 	/*
99039fa104dSReza Arbab 	 * Memory used by the kernel cannot be hot-removed because Linux
99139fa104dSReza Arbab 	 * cannot migrate the kernel pages. When memory hotplug is
99239fa104dSReza Arbab 	 * enabled, we should prevent memblock from allocating memory
99339fa104dSReza Arbab 	 * for the kernel.
99439fa104dSReza Arbab 	 *
99539fa104dSReza Arbab 	 * ACPI SRAT records all hotpluggable memory ranges. But before
99639fa104dSReza Arbab 	 * SRAT is parsed, we don't know about it.
99739fa104dSReza Arbab 	 *
99839fa104dSReza Arbab 	 * The kernel image is loaded into memory at very early time. We
99939fa104dSReza Arbab 	 * cannot prevent this anyway. So on NUMA system, we set any
100039fa104dSReza Arbab 	 * node the kernel resides in as un-hotpluggable.
100139fa104dSReza Arbab 	 *
100239fa104dSReza Arbab 	 * Since on modern servers, one node could have double-digit
100339fa104dSReza Arbab 	 * gigabytes memory, we can assume the memory around the kernel
100439fa104dSReza Arbab 	 * image is also un-hotpluggable. So before SRAT is parsed, just
100539fa104dSReza Arbab 	 * allocate memory near the kernel image to try the best to keep
100639fa104dSReza Arbab 	 * the kernel away from hotpluggable memory.
100739fa104dSReza Arbab 	 */
100839fa104dSReza Arbab 	if (movable_node_is_enabled())
100939fa104dSReza Arbab 		memblock_set_bottom_up(true);
101039fa104dSReza Arbab #endif
101139fa104dSReza Arbab 
10124b0f3b81SKees Cook 	x86_report_nx();
10130ad5bce7SJeremy Fitzhardinge 
101428bb2237SYinghai Lu 	/* after early param, so could get panic from serial */
1015a9ce6bc1SYinghai Lu 	memblock_x86_reserve_range_setup_data();
101628bb2237SYinghai Lu 
101755f26239SYinghai Lu 	if (acpi_mps_check()) {
101855f26239SYinghai Lu #ifdef CONFIG_X86_LOCAL_APIC
101955f26239SYinghai Lu 		disable_apic = 1;
102055f26239SYinghai Lu #endif
1021988781dcSYinghai Lu 		setup_clear_cpu_cap(X86_FEATURE_APIC);
102255f26239SYinghai Lu 	}
102355f26239SYinghai Lu 
10241a127034SIngo Molnar 	e820__reserve_setup_data();
10259641bdafSIngo Molnar 	e820__finish_early_params();
102655f26239SYinghai Lu 
102783e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT))
1028ff0c0874SBrian Maly 		efi_init();
1029ff0c0874SBrian Maly 
10300fca0812SRobert Richter 	dmi_setup();
10312216d199SYinghai Lu 
103288b094fbSAlok Kataria 	/*
103388b094fbSAlok Kataria 	 * VMware detection requires dmi to be available, so this
10340fca0812SRobert Richter 	 * needs to be done after dmi_setup(), for the boot CPU.
103588b094fbSAlok Kataria 	 */
10362d826404SThomas Gleixner 	init_hypervisor_platform();
103788b094fbSAlok Kataria 
1038cf7a63efSPavel Tatashin 	tsc_early_init();
1039f7cf5a5bSThomas Gleixner 	x86_init.resources.probe_roms();
104055f26239SYinghai Lu 
10414046d6e8SLinus Torvalds 	/* after parse_early_param, so could debug it */
10424046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &code_resource);
10434046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &data_resource);
10444046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &bss_resource);
10454046d6e8SLinus Torvalds 
1046b422a309SYinghai Lu 	e820_add_kernel_range();
10471b5576e6SYinghai Lu 	trim_bios_range();
104855f26239SYinghai Lu #ifdef CONFIG_X86_32
104955f26239SYinghai Lu 	if (ppro_with_ram_bug()) {
105009821ff1SIngo Molnar 		e820__range_update(0x70000000ULL, 0x40000ULL, E820_TYPE_RAM,
105109821ff1SIngo Molnar 				  E820_TYPE_RESERVED);
1052f9748fa0SIngo Molnar 		e820__update_table(e820_table);
105355f26239SYinghai Lu 		printk(KERN_INFO "fixed physical RAM map:\n");
1054be0c3f0fSIngo Molnar 		e820__print_table("bad_ppro");
105555f26239SYinghai Lu 	}
105655f26239SYinghai Lu #else
105755f26239SYinghai Lu 	early_gart_iommu_check();
105855f26239SYinghai Lu #endif
105955f26239SYinghai Lu 
106055f26239SYinghai Lu 	/*
106155f26239SYinghai Lu 	 * partially used pages are not usable - thus
106255f26239SYinghai Lu 	 * we are rounding upwards:
106355f26239SYinghai Lu 	 */
10640c6fc11aSIngo Molnar 	max_pfn = e820__end_of_ram_pfn();
106555f26239SYinghai Lu 
106655f26239SYinghai Lu 	/* update e820 for memory not covered by WB MTRRs */
106755f26239SYinghai Lu 	mtrr_bp_init();
10682dc807b3SYinghai Lu 	if (mtrr_trim_uncached_memory(max_pfn))
10690c6fc11aSIngo Molnar 		max_pfn = e820__end_of_ram_pfn();
107055f26239SYinghai Lu 
10718dd33030SIgor Mammedov 	max_possible_pfn = max_pfn;
10728dd33030SIgor Mammedov 
1073c7d2361fSThomas Garnier 	/*
107499c13b8cSMikulas Patocka 	 * This call is required when the CPU does not support PAT. If
107599c13b8cSMikulas Patocka 	 * mtrr_bp_init() invoked it already via pat_init() the call has no
107699c13b8cSMikulas Patocka 	 * effect.
107799c13b8cSMikulas Patocka 	 */
107899c13b8cSMikulas Patocka 	init_cache_modes();
107999c13b8cSMikulas Patocka 
108099c13b8cSMikulas Patocka 	/*
1081c7d2361fSThomas Garnier 	 * Define random base addresses for memory sections after max_pfn is
1082c7d2361fSThomas Garnier 	 * defined and before each memory section base is used.
1083c7d2361fSThomas Garnier 	 */
1084c7d2361fSThomas Garnier 	kernel_randomize_memory();
1085c7d2361fSThomas Garnier 
108655f26239SYinghai Lu #ifdef CONFIG_X86_32
108755f26239SYinghai Lu 	/* max_low_pfn get updated here */
108855f26239SYinghai Lu 	find_low_pfn_range();
108955f26239SYinghai Lu #else
10906e1cb38aSSuresh Siddha 	check_x2apic();
109155f26239SYinghai Lu 
109255f26239SYinghai Lu 	/* How many end-of-memory variables you have, grandma! */
109355f26239SYinghai Lu 	/* need this before calling reserve_initrd */
1094f361a450SYinghai Lu 	if (max_pfn > (1UL<<(32 - PAGE_SHIFT)))
10950c6fc11aSIngo Molnar 		max_low_pfn = e820__end_of_low_ram_pfn();
1096f361a450SYinghai Lu 	else
109755f26239SYinghai Lu 		max_low_pfn = max_pfn;
1098f361a450SYinghai Lu 
109955f26239SYinghai Lu 	high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
110055f26239SYinghai Lu #endif
110155f26239SYinghai Lu 
1102893f38d1SYinghai Lu 	/*
1103893f38d1SYinghai Lu 	 * Find and reserve possible boot-time SMP configuration:
1104893f38d1SYinghai Lu 	 */
1105893f38d1SYinghai Lu 	find_smp_config();
1106893f38d1SYinghai Lu 
1107042be38eSYinghai Lu 	reserve_ibft_region();
1108042be38eSYinghai Lu 
11098d57470dSYinghai Lu 	early_alloc_pgt_buf();
11108d57470dSYinghai Lu 
111172d7c3b3SYinghai Lu 	/*
11124918e228SIngo Molnar 	 * Need to conclude brk, before e820__memblock_setup()
111372d7c3b3SYinghai Lu 	 *  it could use memblock_find_in_range, could overlap with
111472d7c3b3SYinghai Lu 	 *  brk area.
111572d7c3b3SYinghai Lu 	 */
111672d7c3b3SYinghai Lu 	reserve_brk();
111772d7c3b3SYinghai Lu 
1118e5f15b45SYinghai Lu 	cleanup_highmap();
1119e5f15b45SYinghai Lu 
11202449f343STang Chen 	memblock_set_current_limit(ISA_END_ADDRESS);
11214918e228SIngo Molnar 	e820__memblock_setup();
112272d7c3b3SYinghai Lu 
1123007b7560SAndy Lutomirski 	reserve_bios_regions();
1124007b7560SAndy Lutomirski 
11254971531aSMatt Fleming 	if (efi_enabled(EFI_MEMMAP)) {
11264971531aSMatt Fleming 		efi_fake_memmap();
11274971531aSMatt Fleming 		efi_find_mirror();
11283dad6f7fSRicardo Neri 		efi_esrt_init();
11294971531aSMatt Fleming 
1130916f676fSMatthew Garrett 		/*
11314971531aSMatt Fleming 		 * The EFI specification says that boot service code won't be
11324971531aSMatt Fleming 		 * called after ExitBootServices(). This is, in fact, a lie.
1133916f676fSMatthew Garrett 		 */
1134916f676fSMatthew Garrett 		efi_reserve_boot_services();
11354971531aSMatt Fleming 	}
1136916f676fSMatthew Garrett 
113772d7c3b3SYinghai Lu 	/* preallocate 4k for mptable mpc */
11385da217caSIngo Molnar 	e820__memblock_alloc_reserved_mpc_new();
113972d7c3b3SYinghai Lu 
114072d7c3b3SYinghai Lu #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
114172d7c3b3SYinghai Lu 	setup_bios_corruption_check();
114272d7c3b3SYinghai Lu #endif
114372d7c3b3SYinghai Lu 
114410054230SYinghai Lu #ifdef CONFIG_X86_32
1145365811d6SBjorn Helgaas 	printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n",
1146365811d6SBjorn Helgaas 			(max_pfn_mapped<<PAGE_SHIFT) - 1);
114710054230SYinghai Lu #endif
114872d7c3b3SYinghai Lu 
11494f7b9226SYinghai Lu 	reserve_real_mode();
1150893f38d1SYinghai Lu 
1151a9acc536SJesse Barnes 	trim_platform_memory_ranges();
115295c96084SH. Peter Anvin 	trim_low_memory_range();
1153a9acc536SJesse Barnes 
115422ddfcaaSYinghai Lu 	init_mem_mapping();
1155854c879fSPekka J Enberg 
1156433f8924SThomas Gleixner 	idt_setup_early_pf();
1157cda846f1SJarkko Sakkinen 
115818bc7bd5SAndy Lutomirski 	/*
115918bc7bd5SAndy Lutomirski 	 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features)
116018bc7bd5SAndy Lutomirski 	 * with the current CR4 value.  This may not be necessary, but
116118bc7bd5SAndy Lutomirski 	 * auditing all the early-boot CR4 manipulation would be needed to
116218bc7bd5SAndy Lutomirski 	 * rule it out.
1163c7ad5ad2SAndy Lutomirski 	 *
1164c7ad5ad2SAndy Lutomirski 	 * Mask off features that don't work outside long mode (just
1165c7ad5ad2SAndy Lutomirski 	 * PCIDE for now).
116618bc7bd5SAndy Lutomirski 	 */
1167c7ad5ad2SAndy Lutomirski 	mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE;
116818bc7bd5SAndy Lutomirski 
11694ce7a869SYinghai Lu 	memblock_set_current_limit(get_max_mapped());
1170e7b37895SYinghai Lu 
1171e7b37895SYinghai Lu 	/*
1172e7b37895SYinghai Lu 	 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
1173e7b37895SYinghai Lu 	 */
1174e7b37895SYinghai Lu 
1175e7b37895SYinghai Lu #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
1176e7b37895SYinghai Lu 	if (init_ohci1394_dma_early)
1177162a7e75SMike Travis 		init_ohci1394_dma_on_all_controllers();
1178162a7e75SMike Travis #endif
1179e7b37895SYinghai Lu 	/* Allocate bigger log buffer */
118055f26239SYinghai Lu 	setup_log_buf(1);
118155f26239SYinghai Lu 
11829661b332SDavid Howells 	if (efi_enabled(EFI_BOOT)) {
11839661b332SDavid Howells 		switch (boot_params.secure_boot) {
11849661b332SDavid Howells 		case efi_secureboot_mode_disabled:
11859661b332SDavid Howells 			pr_info("Secure boot disabled\n");
11869661b332SDavid Howells 			break;
11879661b332SDavid Howells 		case efi_secureboot_mode_enabled:
11889661b332SDavid Howells 			pr_info("Secure boot enabled\n");
11899661b332SDavid Howells 			break;
11909661b332SDavid Howells 		default:
11919661b332SDavid Howells 			pr_info("Secure boot could not be determined\n");
11929661b332SDavid Howells 			break;
11939661b332SDavid Howells 		}
11949661b332SDavid Howells 	}
11959661b332SDavid Howells 
119644280733SYinghai Lu 	reserve_initrd();
119744280733SYinghai Lu 
1198da3d3f98SAleksey Makarov 	acpi_table_upgrade();
119955f26239SYinghai Lu 
120055f26239SYinghai Lu 	vsmp_init();
120155f26239SYinghai Lu 
120255f26239SYinghai Lu 	io_delay_init();
120355f26239SYinghai Lu 
1204630b3affSLukas Wunner 	early_platform_quirks();
1205630b3affSLukas Wunner 
120655f26239SYinghai Lu 	/*
120755f26239SYinghai Lu 	 * Parse the ACPI tables for possible boot-time SMP configuration.
120855f26239SYinghai Lu 	 */
120920e6926dSYinghai Lu 	acpi_boot_table_init();
121020e6926dSYinghai Lu 
121120e6926dSYinghai Lu 	early_acpi_boot_init();
121220e6926dSYinghai Lu 
121391467bdfSBernhard Walle 	initmem_init();
12143c325f82SWeijie Yang 	dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
1215fa591c4aSTang Chen 
1216fa591c4aSTang Chen 	/*
1217fa591c4aSTang Chen 	 * Reserve memory for crash kernel after SRAT is parsed so that it
1218fa591c4aSTang Chen 	 * won't consume hotpluggable memory.
1219fa591c4aSTang Chen 	 */
1220fa591c4aSTang Chen 	reserve_crashkernel();
1221fa591c4aSTang Chen 
122255f26239SYinghai Lu 	memblock_find_dma_reserve();
122355f26239SYinghai Lu 
1224ccb64941SBoris Ostrovsky 	if (!early_xdbc_setup_hardware())
1225ccb64941SBoris Ostrovsky 		early_xdbc_register_console();
1226ccb64941SBoris Ostrovsky 
1227030cb6c0SThomas Gleixner 	x86_init.paging.pagetable_init();
122855f26239SYinghai Lu 
1229ef7f0d6aSAndrey Ryabinin 	kasan_init();
1230ef7f0d6aSAndrey Ryabinin 
1231d2b6dc61SAndy Lutomirski 	/*
1232945fd17aSThomas Gleixner 	 * Sync back kernel address range.
1233945fd17aSThomas Gleixner 	 *
1234945fd17aSThomas Gleixner 	 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
1235945fd17aSThomas Gleixner 	 * this call?
1236d2b6dc61SAndy Lutomirski 	 */
1237945fd17aSThomas Gleixner 	sync_initial_page_table();
1238d2b6dc61SAndy Lutomirski 
123931625340SJoseph Cihula 	tboot_probe();
124031625340SJoseph Cihula 
124155f26239SYinghai Lu 	map_vsyscall();
124255f26239SYinghai Lu 
124355f26239SYinghai Lu 	generic_apic_probe();
124455f26239SYinghai Lu 
124555f26239SYinghai Lu 	early_quirks();
124655f26239SYinghai Lu 
124755f26239SYinghai Lu 	/*
124855f26239SYinghai Lu 	 * Read APIC and some other early information from ACPI tables.
124955f26239SYinghai Lu 	 */
125055f26239SYinghai Lu 	acpi_boot_init();
1251efafc8b2SFeng Tang 	sfi_init();
1252a906fdaaSThomas Gleixner 	x86_dtb_init();
1253efafc8b2SFeng Tang 
125455f26239SYinghai Lu 	/*
125555f26239SYinghai Lu 	 * get boot-time SMP configuration:
125655f26239SYinghai Lu 	 */
125755f26239SYinghai Lu 	get_smp_config();
125855f26239SYinghai Lu 
12591e90a13dSThomas Gleixner 	/*
12601e90a13dSThomas Gleixner 	 * Systems w/o ACPI and mptables might not have it mapped the local
12611e90a13dSThomas Gleixner 	 * APIC yet, but prefill_possible_map() might need to access it.
12621e90a13dSThomas Gleixner 	 */
12631e90a13dSThomas Gleixner 	init_apic_mappings();
12641e90a13dSThomas Gleixner 
1265329513a3SYinghai Lu 	prefill_possible_map();
1266301e6190SYinghai Lu 
12675f4765f9SYinghai Lu 	init_cpu_to_node();
12685f4765f9SYinghai Lu 
1269ca1b8862SThomas Gleixner 	io_apic_init_mappings();
12709d6a4d08SYinghai Lu 
1271f3614646SJuergen Gross 	x86_init.hyper.guest_late_init();
127255f26239SYinghai Lu 
12731506c8dcSIngo Molnar 	e820__reserve_resources();
1274cc55f753SZhimin Gu 	e820__register_nosave_regions(max_pfn);
127555f26239SYinghai Lu 
12768fee697dSThomas Gleixner 	x86_init.resources.reserve_resources();
127755f26239SYinghai Lu 
12782df908baSIngo Molnar 	e820__setup_pci_gap();
127955f26239SYinghai Lu 
128055f26239SYinghai Lu #ifdef CONFIG_VT
128155f26239SYinghai Lu #if defined(CONFIG_VGA_CONSOLE)
128283e68189SMatt Fleming 	if (!efi_enabled(EFI_BOOT) || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
128355f26239SYinghai Lu 		conswitchp = &vga_con;
128455f26239SYinghai Lu #elif defined(CONFIG_DUMMY_CONSOLE)
128555f26239SYinghai Lu 	conswitchp = &dummy_con;
128655f26239SYinghai Lu #endif
128755f26239SYinghai Lu #endif
12886f30c1acSThomas Gleixner 	x86_init.oem.banner();
1289a2202aa2SYong Wang 
12906b617e22SFeng Tang 	x86_init.timers.wallclock_init();
12916b617e22SFeng Tang 
1292a2202aa2SYong Wang 	mcheck_init();
1293f49aa448SJason Baron 
1294b3c869d3SJohn Stultz 	register_refined_jiffies(CLOCK_TICK_RATE);
12955189c2a7SOlof Johansson 
12965189c2a7SOlof Johansson #ifdef CONFIG_EFI
1297a5d90c92SBorislav Petkov 	if (efi_enabled(EFI_BOOT))
1298a5d90c92SBorislav Petkov 		efi_apply_memmap_quirks();
12995189c2a7SOlof Johansson #endif
1300ee9f8fceSJosh Poimboeuf 
1301ee9f8fceSJosh Poimboeuf 	unwind_init();
130255f26239SYinghai Lu }
13035649b7c3SIngo Molnar 
13049be1b56aSIngo Molnar #ifdef CONFIG_X86_32
13055649b7c3SIngo Molnar 
13068fee697dSThomas Gleixner static struct resource video_ram_resource = {
13078fee697dSThomas Gleixner 	.name	= "Video RAM area",
13088fee697dSThomas Gleixner 	.start	= 0xa0000,
13098fee697dSThomas Gleixner 	.end	= 0xbffff,
13108fee697dSThomas Gleixner 	.flags	= IORESOURCE_BUSY | IORESOURCE_MEM
13119be1b56aSIngo Molnar };
13129be1b56aSIngo Molnar 
13138fee697dSThomas Gleixner void __init i386_reserve_resources(void)
13149be1b56aSIngo Molnar {
13158fee697dSThomas Gleixner 	request_resource(&iomem_resource, &video_ram_resource);
13168fee697dSThomas Gleixner 	reserve_standard_io_resources();
13179be1b56aSIngo Molnar }
13189be1b56aSIngo Molnar 
13199be1b56aSIngo Molnar #endif /* CONFIG_X86_32 */
1320f32360efSKees Cook 
1321f32360efSKees Cook static struct notifier_block kernel_offset_notifier = {
1322f32360efSKees Cook 	.notifier_call = dump_kernel_offset
1323f32360efSKees Cook };
1324f32360efSKees Cook 
1325f32360efSKees Cook static int __init register_kernel_offset_dumper(void)
1326f32360efSKees Cook {
1327f32360efSKees Cook 	atomic_notifier_chain_register(&panic_notifier_list,
1328f32360efSKees Cook 					&kernel_offset_notifier);
1329f32360efSKees Cook 	return 0;
1330f32360efSKees Cook }
1331f32360efSKees Cook __initcall(register_kernel_offset_dumper);
1332