xref: /linux-6.15/arch/x86/kernel/setup.c (revision b3c72fc9)
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);
441*b3c72fc9SDaniel Kiper 
442*b3c72fc9SDaniel Kiper 		if (data->type == SETUP_INDIRECT &&
443*b3c72fc9SDaniel Kiper 		    ((struct setup_indirect *)data->data)->type != SETUP_INDIRECT)
444*b3c72fc9SDaniel Kiper 			memblock_reserve(((struct setup_indirect *)data->data)->addr,
445*b3c72fc9SDaniel Kiper 					 ((struct setup_indirect *)data->data)->len);
446*b3c72fc9SDaniel Kiper 
447a0a0becdSYinghai Lu 		pa_data = data->next;
4488d4a40bcSJuergen Gross 		early_memunmap(data, sizeof(*data));
449a0a0becdSYinghai Lu 	}
450a0a0becdSYinghai Lu }
451a0a0becdSYinghai Lu 
45255f26239SYinghai Lu /*
453ccb4defaSYinghai Lu  * --------- Crashkernel reservation ------------------------------
454ccb4defaSYinghai Lu  */
455ccb4defaSYinghai Lu 
4562965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
45732105f7fSBernhard Walle 
458606134f7SBorislav Petkov /* 16M alignment for crash kernel regions */
4599ca5c8e6SDave Young #define CRASH_ALIGN		SZ_16M
460606134f7SBorislav Petkov 
4617f8595bfSH. Peter Anvin /*
4628ff80fbeSBaoquan He  * Keep the crash kernel below this limit.
4638ff80fbeSBaoquan He  *
4648ff80fbeSBaoquan He  * On 32 bits earlier kernels would limit the kernel to the low 512 MiB
4658ff80fbeSBaoquan He  * due to mapping restrictions.
4668ff80fbeSBaoquan He  *
4678ff80fbeSBaoquan He  * On 64bit, kdump kernel need be restricted to be under 64TB, which is
4688ff80fbeSBaoquan He  * the upper limit of system RAM in 4-level paing mode. Since the kdump
4698ff80fbeSBaoquan He  * jumping could be from 5-level to 4-level, the jumping will fail if
4708ff80fbeSBaoquan He  * kernel is put above 64TB, and there's no way to detect the paging mode
4718ff80fbeSBaoquan He  * of the kernel which will be loaded for dumping during the 1st kernel
4728ff80fbeSBaoquan He  * bootup.
4737f8595bfSH. Peter Anvin  */
4747f8595bfSH. Peter Anvin #ifdef CONFIG_X86_32
4759ca5c8e6SDave Young # define CRASH_ADDR_LOW_MAX	SZ_512M
4769ca5c8e6SDave Young # define CRASH_ADDR_HIGH_MAX	SZ_512M
4777f8595bfSH. Peter Anvin #else
4789ca5c8e6SDave Young # define CRASH_ADDR_LOW_MAX	SZ_4G
4798ff80fbeSBaoquan He # define CRASH_ADDR_HIGH_MAX	SZ_64T
4807f8595bfSH. Peter Anvin #endif
4817f8595bfSH. Peter Anvin 
482eb6db83dSBaoquan He static int __init reserve_crashkernel_low(void)
4830212f915SYinghai Lu {
4840212f915SYinghai Lu #ifdef CONFIG_X86_64
485f56d5578SBorislav Petkov 	unsigned long long base, low_base = 0, low_size = 0;
4860212f915SYinghai Lu 	unsigned long total_low_mem;
4870212f915SYinghai Lu 	int ret;
4880212f915SYinghai Lu 
4890212f915SYinghai Lu 	total_low_mem = memblock_mem_size(1UL << (32 - PAGE_SHIFT));
49097eac21bSBorislav Petkov 
491adbc742bSYinghai Lu 	/* crashkernel=Y,low */
49297eac21bSBorislav Petkov 	ret = parse_crashkernel_low(boot_command_line, total_low_mem, &low_size, &base);
493f56d5578SBorislav Petkov 	if (ret) {
494c729de8fSYinghai Lu 		/*
495392e879aSAndy Shevchenko 		 * two parts from kernel/dma/swiotlb.c:
49694fb9334SJoerg Roedel 		 * -swiotlb size: user-specified with swiotlb= or default.
49794fb9334SJoerg Roedel 		 *
49894fb9334SJoerg Roedel 		 * -swiotlb overflow buffer: now hardcoded to 32k. We round it
49994fb9334SJoerg Roedel 		 * to 8M for other buffers that may need to stay low too. Also
50094fb9334SJoerg Roedel 		 * make sure we allocate enough extra low memory so that we
50194fb9334SJoerg Roedel 		 * don't run out of DMA buffers for 32-bit devices.
502c729de8fSYinghai Lu 		 */
50394fb9334SJoerg Roedel 		low_size = max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
504c729de8fSYinghai Lu 	} else {
505adbc742bSYinghai Lu 		/* passed with crashkernel=0,low ? */
506c729de8fSYinghai Lu 		if (!low_size)
507eb6db83dSBaoquan He 			return 0;
508c729de8fSYinghai Lu 	}
5090212f915SYinghai Lu 
510fe2d48b8SJiri Bohac 	low_base = memblock_find_in_range(0, 1ULL << 32, low_size, CRASH_ALIGN);
5110212f915SYinghai Lu 	if (!low_base) {
512eb6db83dSBaoquan He 		pr_err("Cannot reserve %ldMB crashkernel low memory, please try smaller size.\n",
513eb6db83dSBaoquan He 		       (unsigned long)(low_size >> 20));
514eb6db83dSBaoquan He 		return -ENOMEM;
5150212f915SYinghai Lu 	}
5160212f915SYinghai Lu 
5176f376057SBorislav Petkov 	ret = memblock_reserve(low_base, low_size);
5186f376057SBorislav Petkov 	if (ret) {
5196f376057SBorislav Petkov 		pr_err("%s: Error reserving crashkernel low memblock.\n", __func__);
5206f376057SBorislav Petkov 		return ret;
5216f376057SBorislav Petkov 	}
52297eac21bSBorislav Petkov 
5230212f915SYinghai Lu 	pr_info("Reserving %ldMB of low memory at %ldMB for crashkernel (System low RAM: %ldMB)\n",
5240212f915SYinghai Lu 		(unsigned long)(low_size >> 20),
5250212f915SYinghai Lu 		(unsigned long)(low_base >> 20),
5260212f915SYinghai Lu 		(unsigned long)(total_low_mem >> 20));
52797eac21bSBorislav Petkov 
5280212f915SYinghai Lu 	crashk_low_res.start = low_base;
5290212f915SYinghai Lu 	crashk_low_res.end   = low_base + low_size - 1;
5300212f915SYinghai Lu 	insert_resource(&iomem_resource, &crashk_low_res);
5310212f915SYinghai Lu #endif
532eb6db83dSBaoquan He 	return 0;
5330212f915SYinghai Lu }
5340212f915SYinghai Lu 
53529f784e3SYinghai Lu static void __init reserve_crashkernel(void)
536ccb4defaSYinghai Lu {
537f56d5578SBorislav Petkov 	unsigned long long crash_size, crash_base, total_mem;
53855a20ee7SYinghai Lu 	bool high = false;
539ccb4defaSYinghai Lu 	int ret;
540ccb4defaSYinghai Lu 
54109c71bfdSDave Young 	total_mem = memblock_phys_mem_size();
542ccb4defaSYinghai Lu 
54355a20ee7SYinghai Lu 	/* crashkernel=XM */
54497eac21bSBorislav Petkov 	ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
54555a20ee7SYinghai Lu 	if (ret != 0 || crash_size <= 0) {
546adbc742bSYinghai Lu 		/* crashkernel=X,high */
54755a20ee7SYinghai Lu 		ret = parse_crashkernel_high(boot_command_line, total_mem,
54855a20ee7SYinghai Lu 					     &crash_size, &crash_base);
54932105f7fSBernhard Walle 		if (ret != 0 || crash_size <= 0)
55032105f7fSBernhard Walle 			return;
55155a20ee7SYinghai Lu 		high = true;
55255a20ee7SYinghai Lu 	}
55332105f7fSBernhard Walle 
5543db3eb28SPetr Tesarik 	if (xen_pv_domain()) {
5553db3eb28SPetr Tesarik 		pr_info("Ignoring crashkernel for a Xen PV domain\n");
5563db3eb28SPetr Tesarik 		return;
5573db3eb28SPetr Tesarik 	}
5583db3eb28SPetr Tesarik 
55932105f7fSBernhard Walle 	/* 0 means: find the address automatically */
560b9ac3849SDave Young 	if (!crash_base) {
5619f4c1396SYinghai Lu 		/*
562a8d4c824SXunlei Pang 		 * Set CRASH_ADDR_LOW_MAX upper bound for crash memory,
563b9ac3849SDave Young 		 * crashkernel=x,high reserves memory over 4G, also allocates
564b9ac3849SDave Young 		 * 256M extra low memory for DMA buffers and swiotlb.
565b9ac3849SDave Young 		 * But the extra memory is not required for all machines.
566b9ac3849SDave Young 		 * So try low memory first and fall back to high memory
567b9ac3849SDave Young 		 * unless "crashkernel=size[KMG],high" is specified.
5689f4c1396SYinghai Lu 		 */
569b9ac3849SDave Young 		if (!high)
570606134f7SBorislav Petkov 			crash_base = memblock_find_in_range(CRASH_ALIGN,
571b9ac3849SDave Young 						CRASH_ADDR_LOW_MAX,
572b9ac3849SDave Young 						crash_size, CRASH_ALIGN);
573b9ac3849SDave Young 		if (!crash_base)
574b9ac3849SDave Young 			crash_base = memblock_find_in_range(CRASH_ALIGN,
575b9ac3849SDave Young 						CRASH_ADDR_HIGH_MAX,
576606134f7SBorislav Petkov 						crash_size, CRASH_ALIGN);
5771f5026a7STejun Heo 		if (!crash_base) {
57844280733SYinghai Lu 			pr_info("crashkernel reservation failed - No suitable area found.\n");
579ccb4defaSYinghai Lu 			return;
580ccb4defaSYinghai Lu 		}
58132105f7fSBernhard Walle 	} else {
58244280733SYinghai Lu 		unsigned long long start;
58344280733SYinghai Lu 
5849f4c1396SYinghai Lu 		start = memblock_find_in_range(crash_base,
58597eac21bSBorislav Petkov 					       crash_base + crash_size,
58697eac21bSBorislav Petkov 					       crash_size, 1 << 20);
58744280733SYinghai Lu 		if (start != crash_base) {
58844280733SYinghai Lu 			pr_info("crashkernel reservation failed - memory is in use.\n");
589ccb4defaSYinghai Lu 			return;
590ccb4defaSYinghai Lu 		}
59132105f7fSBernhard Walle 	}
5926f376057SBorislav Petkov 	ret = memblock_reserve(crash_base, crash_size);
5936f376057SBorislav Petkov 	if (ret) {
5946f376057SBorislav Petkov 		pr_err("%s: Error reserving crashkernel memblock.\n", __func__);
5956f376057SBorislav Petkov 		return;
5966f376057SBorislav Petkov 	}
597ccb4defaSYinghai Lu 
598eb6db83dSBaoquan He 	if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) {
599eb6db83dSBaoquan He 		memblock_free(crash_base, crash_size);
600eb6db83dSBaoquan He 		return;
601eb6db83dSBaoquan He 	}
602eb6db83dSBaoquan He 
603f56d5578SBorislav Petkov 	pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
604ccb4defaSYinghai Lu 		(unsigned long)(crash_size >> 20),
605ccb4defaSYinghai Lu 		(unsigned long)(crash_base >> 20),
606ccb4defaSYinghai Lu 		(unsigned long)(total_mem >> 20));
607ccb4defaSYinghai Lu 
608ccb4defaSYinghai Lu 	crashk_res.start = crash_base;
609ccb4defaSYinghai Lu 	crashk_res.end   = crash_base + crash_size - 1;
610ccb4defaSYinghai Lu 	insert_resource(&iomem_resource, &crashk_res);
611ccb4defaSYinghai Lu }
612ccb4defaSYinghai Lu #else
61329f784e3SYinghai Lu static void __init reserve_crashkernel(void)
614ccb4defaSYinghai Lu {
615ccb4defaSYinghai Lu }
616ccb4defaSYinghai Lu #endif
617ccb4defaSYinghai Lu 
618bdba0e70SYinghai Lu static struct resource standard_io_resources[] = {
619bdba0e70SYinghai Lu 	{ .name = "dma1", .start = 0x00, .end = 0x1f,
620bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
621bdba0e70SYinghai Lu 	{ .name = "pic1", .start = 0x20, .end = 0x21,
622bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
623bdba0e70SYinghai Lu 	{ .name = "timer0", .start = 0x40, .end = 0x43,
624bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
625bdba0e70SYinghai Lu 	{ .name = "timer1", .start = 0x50, .end = 0x53,
626bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
627bdba0e70SYinghai Lu 	{ .name = "keyboard", .start = 0x60, .end = 0x60,
628bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
629bdba0e70SYinghai Lu 	{ .name = "keyboard", .start = 0x64, .end = 0x64,
630bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
631bdba0e70SYinghai Lu 	{ .name = "dma page reg", .start = 0x80, .end = 0x8f,
632bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
633bdba0e70SYinghai Lu 	{ .name = "pic2", .start = 0xa0, .end = 0xa1,
634bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
635bdba0e70SYinghai Lu 	{ .name = "dma2", .start = 0xc0, .end = 0xdf,
636bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
637bdba0e70SYinghai Lu 	{ .name = "fpu", .start = 0xf0, .end = 0xff,
638bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO }
639bdba0e70SYinghai Lu };
640bdba0e70SYinghai Lu 
6418fee697dSThomas Gleixner void __init reserve_standard_io_resources(void)
642bdba0e70SYinghai Lu {
643bdba0e70SYinghai Lu 	int i;
644bdba0e70SYinghai Lu 
645bdba0e70SYinghai Lu 	/* request I/O space for devices used on all i[345]86 PCs */
646bdba0e70SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
647bdba0e70SYinghai Lu 		request_resource(&ioport_resource, &standard_io_resources[i]);
648bdba0e70SYinghai Lu 
649bdba0e70SYinghai Lu }
650bdba0e70SYinghai Lu 
651042be38eSYinghai Lu static __init void reserve_ibft_region(void)
652042be38eSYinghai Lu {
653042be38eSYinghai Lu 	unsigned long addr, size = 0;
654042be38eSYinghai Lu 
655042be38eSYinghai Lu 	addr = find_ibft_region(&size);
656042be38eSYinghai Lu 
657042be38eSYinghai Lu 	if (size)
65824aa0788STejun Heo 		memblock_reserve(addr, size);
659042be38eSYinghai Lu }
660042be38eSYinghai Lu 
661a9acc536SJesse Barnes static bool __init snb_gfx_workaround_needed(void)
662a9acc536SJesse Barnes {
663e43b3cecSH. Peter Anvin #ifdef CONFIG_PCI
664a9acc536SJesse Barnes 	int i;
665a9acc536SJesse Barnes 	u16 vendor, devid;
666ab3cd867SH. Peter Anvin 	static const __initconst u16 snb_ids[] = {
667a9acc536SJesse Barnes 		0x0102,
668a9acc536SJesse Barnes 		0x0112,
669a9acc536SJesse Barnes 		0x0122,
670a9acc536SJesse Barnes 		0x0106,
671a9acc536SJesse Barnes 		0x0116,
672a9acc536SJesse Barnes 		0x0126,
673a9acc536SJesse Barnes 		0x010a,
674a9acc536SJesse Barnes 	};
675a9acc536SJesse Barnes 
676a9acc536SJesse Barnes 	/* Assume no if something weird is going on with PCI */
677a9acc536SJesse Barnes 	if (!early_pci_allowed())
678a9acc536SJesse Barnes 		return false;
679a9acc536SJesse Barnes 
680a9acc536SJesse Barnes 	vendor = read_pci_config_16(0, 2, 0, PCI_VENDOR_ID);
681a9acc536SJesse Barnes 	if (vendor != 0x8086)
682a9acc536SJesse Barnes 		return false;
683a9acc536SJesse Barnes 
684a9acc536SJesse Barnes 	devid = read_pci_config_16(0, 2, 0, PCI_DEVICE_ID);
685a9acc536SJesse Barnes 	for (i = 0; i < ARRAY_SIZE(snb_ids); i++)
686a9acc536SJesse Barnes 		if (devid == snb_ids[i])
687a9acc536SJesse Barnes 			return true;
688e43b3cecSH. Peter Anvin #endif
689a9acc536SJesse Barnes 
690a9acc536SJesse Barnes 	return false;
691a9acc536SJesse Barnes }
692a9acc536SJesse Barnes 
693a9acc536SJesse Barnes /*
694a9acc536SJesse Barnes  * Sandy Bridge graphics has trouble with certain ranges, exclude
695a9acc536SJesse Barnes  * them from allocation.
696a9acc536SJesse Barnes  */
697a9acc536SJesse Barnes static void __init trim_snb_memory(void)
698a9acc536SJesse Barnes {
699ab3cd867SH. Peter Anvin 	static const __initconst unsigned long bad_pages[] = {
700a9acc536SJesse Barnes 		0x20050000,
701a9acc536SJesse Barnes 		0x20110000,
702a9acc536SJesse Barnes 		0x20130000,
703a9acc536SJesse Barnes 		0x20138000,
704a9acc536SJesse Barnes 		0x40004000,
705a9acc536SJesse Barnes 	};
706a9acc536SJesse Barnes 	int i;
707a9acc536SJesse Barnes 
708a9acc536SJesse Barnes 	if (!snb_gfx_workaround_needed())
709a9acc536SJesse Barnes 		return;
710a9acc536SJesse Barnes 
711a9acc536SJesse Barnes 	printk(KERN_DEBUG "reserving inaccessible SNB gfx pages\n");
712a9acc536SJesse Barnes 
713a9acc536SJesse Barnes 	/*
714a9acc536SJesse Barnes 	 * Reserve all memory below the 1 MB mark that has not
715a9acc536SJesse Barnes 	 * already been reserved.
716a9acc536SJesse Barnes 	 */
717a9acc536SJesse Barnes 	memblock_reserve(0, 1<<20);
718a9acc536SJesse Barnes 
719a9acc536SJesse Barnes 	for (i = 0; i < ARRAY_SIZE(bad_pages); i++) {
720a9acc536SJesse Barnes 		if (memblock_reserve(bad_pages[i], PAGE_SIZE))
721a9acc536SJesse Barnes 			printk(KERN_WARNING "failed to reserve 0x%08lx\n",
722a9acc536SJesse Barnes 			       bad_pages[i]);
723a9acc536SJesse Barnes 	}
724a9acc536SJesse Barnes }
725a9acc536SJesse Barnes 
726a9acc536SJesse Barnes /*
727a9acc536SJesse Barnes  * Here we put platform-specific memory range workarounds, i.e.
728a9acc536SJesse Barnes  * memory known to be corrupt or otherwise in need to be reserved on
729a9acc536SJesse Barnes  * specific platforms.
730a9acc536SJesse Barnes  *
731a9acc536SJesse Barnes  * If this gets used more widely it could use a real dispatch mechanism.
732a9acc536SJesse Barnes  */
733a9acc536SJesse Barnes static void __init trim_platform_memory_ranges(void)
734a9acc536SJesse Barnes {
735a9acc536SJesse Barnes 	trim_snb_memory();
736a9acc536SJesse Barnes }
737a9acc536SJesse Barnes 
7381b5576e6SYinghai Lu static void __init trim_bios_range(void)
7391b5576e6SYinghai Lu {
7401b5576e6SYinghai Lu 	/*
7411b5576e6SYinghai Lu 	 * A special case is the first 4Kb of memory;
7421b5576e6SYinghai Lu 	 * This is a BIOS owned area, not kernel ram, but generally
7431b5576e6SYinghai Lu 	 * not listed as such in the E820 table.
744d0cd7425SH. Peter Anvin 	 *
745d0cd7425SH. Peter Anvin 	 * This typically reserves additional memory (64KiB by default)
746d0cd7425SH. Peter Anvin 	 * since some BIOSes are known to corrupt low memory.  See the
7479ea77bdbSH. Peter Anvin 	 * Kconfig help text for X86_RESERVE_LOW.
7481b5576e6SYinghai Lu 	 */
74909821ff1SIngo Molnar 	e820__range_update(0, PAGE_SIZE, E820_TYPE_RAM, E820_TYPE_RESERVED);
750d0cd7425SH. Peter Anvin 
7511b5576e6SYinghai Lu 	/*
7521b5576e6SYinghai Lu 	 * special case: Some BIOSen report the PC BIOS
7531b5576e6SYinghai Lu 	 * area (640->1Mb) as ram even though it is not.
7541b5576e6SYinghai Lu 	 * take them out.
7551b5576e6SYinghai Lu 	 */
75609821ff1SIngo Molnar 	e820__range_remove(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_TYPE_RAM, 1);
757a9acc536SJesse Barnes 
758f9748fa0SIngo Molnar 	e820__update_table(e820_table);
7591b5576e6SYinghai Lu }
7601b5576e6SYinghai Lu 
761b422a309SYinghai Lu /* called before trim_bios_range() to spare extra sanitize */
762b422a309SYinghai Lu static void __init e820_add_kernel_range(void)
763b422a309SYinghai Lu {
764b422a309SYinghai Lu 	u64 start = __pa_symbol(_text);
765b422a309SYinghai Lu 	u64 size = __pa_symbol(_end) - start;
766b422a309SYinghai Lu 
767b422a309SYinghai Lu 	/*
76809821ff1SIngo Molnar 	 * Complain if .text .data and .bss are not marked as E820_TYPE_RAM and
769b422a309SYinghai Lu 	 * attempt to fix it by adding the range. We may have a confused BIOS,
770b422a309SYinghai Lu 	 * or the user may have used memmap=exactmap or memmap=xxM$yyM to
771b422a309SYinghai Lu 	 * exclude kernel range. If we really are running on top non-RAM,
772b422a309SYinghai Lu 	 * we will crash later anyways.
773b422a309SYinghai Lu 	 */
77409821ff1SIngo Molnar 	if (e820__mapped_all(start, start + size, E820_TYPE_RAM))
775b422a309SYinghai Lu 		return;
776b422a309SYinghai Lu 
77709821ff1SIngo Molnar 	pr_warn(".text .data .bss are not marked as E820_TYPE_RAM!\n");
77809821ff1SIngo Molnar 	e820__range_remove(start, size, E820_TYPE_RAM, 0);
77909821ff1SIngo Molnar 	e820__range_add(start, size, E820_TYPE_RAM);
780b422a309SYinghai Lu }
781b422a309SYinghai Lu 
78295c96084SH. Peter Anvin static unsigned reserve_low = CONFIG_X86_RESERVE_LOW << 10;
78395c96084SH. Peter Anvin 
7849ea77bdbSH. Peter Anvin static int __init parse_reservelow(char *p)
7859ea77bdbSH. Peter Anvin {
7869ea77bdbSH. Peter Anvin 	unsigned long long size;
7879ea77bdbSH. Peter Anvin 
7889ea77bdbSH. Peter Anvin 	if (!p)
7899ea77bdbSH. Peter Anvin 		return -EINVAL;
7909ea77bdbSH. Peter Anvin 
7919ea77bdbSH. Peter Anvin 	size = memparse(p, &p);
7929ea77bdbSH. Peter Anvin 
7939ea77bdbSH. Peter Anvin 	if (size < 4096)
7949ea77bdbSH. Peter Anvin 		size = 4096;
7959ea77bdbSH. Peter Anvin 
7969ea77bdbSH. Peter Anvin 	if (size > 640*1024)
7979ea77bdbSH. Peter Anvin 		size = 640*1024;
7989ea77bdbSH. Peter Anvin 
7999ea77bdbSH. Peter Anvin 	reserve_low = size;
8009ea77bdbSH. Peter Anvin 
8019ea77bdbSH. Peter Anvin 	return 0;
8029ea77bdbSH. Peter Anvin }
8039ea77bdbSH. Peter Anvin 
8049ea77bdbSH. Peter Anvin early_param("reservelow", parse_reservelow);
8059ea77bdbSH. Peter Anvin 
80695c96084SH. Peter Anvin static void __init trim_low_memory_range(void)
80795c96084SH. Peter Anvin {
80895c96084SH. Peter Anvin 	memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE));
80995c96084SH. Peter Anvin }
81095c96084SH. Peter Anvin 
8115394f80fSJeremy Fitzhardinge /*
812f32360efSKees Cook  * Dump out kernel offset information on panic.
813f32360efSKees Cook  */
814f32360efSKees Cook static int
815f32360efSKees Cook dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
816f32360efSKees Cook {
81778cac48cSBorislav Petkov 	if (kaslr_enabled()) {
81878cac48cSBorislav Petkov 		pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
8194545c898SJiri Kosina 			 kaslr_offset(),
82078cac48cSBorislav Petkov 			 __START_KERNEL,
82178cac48cSBorislav Petkov 			 __START_KERNEL_map,
82278cac48cSBorislav Petkov 			 MODULES_VADDR-1);
82378cac48cSBorislav Petkov 	} else {
82478cac48cSBorislav Petkov 		pr_emerg("Kernel Offset: disabled\n");
82578cac48cSBorislav Petkov 	}
826f32360efSKees Cook 
827f32360efSKees Cook 	return 0;
828f32360efSKees Cook }
829f32360efSKees Cook 
830f32360efSKees Cook /*
83155f26239SYinghai Lu  * Determine if we were loaded by an EFI loader.  If so, then we have also been
83255f26239SYinghai Lu  * passed the efi memmap, systab, etc., so we should use these data structures
83355f26239SYinghai Lu  * for initialization.  Note, the efi init code path is determined by the
83455f26239SYinghai Lu  * global efi_enabled. This allows the same kernel image to be used on existing
83555f26239SYinghai Lu  * systems (with a traditional BIOS) as well as on EFI systems.
83655f26239SYinghai Lu  */
83755f26239SYinghai Lu /*
83855f26239SYinghai Lu  * setup_arch - architecture-specific boot-time initializations
83955f26239SYinghai Lu  *
84055f26239SYinghai Lu  * Note: On x86_64, fixmaps are ready for use even before this is called.
84155f26239SYinghai Lu  */
84255f26239SYinghai Lu 
84355f26239SYinghai Lu void __init setup_arch(char **cmdline_p)
84455f26239SYinghai Lu {
845c603a309SThomas Lendacky 	/*
846c603a309SThomas Lendacky 	 * Reserve the memory occupied by the kernel between _text and
847c603a309SThomas Lendacky 	 * __end_of_kernel_reserve symbols. Any kernel sections after the
848c603a309SThomas Lendacky 	 * __end_of_kernel_reserve symbol must be explicitly reserved with a
849c603a309SThomas Lendacky 	 * separate memblock_reserve() or they will be discarded.
850c603a309SThomas Lendacky 	 */
8516c902b65SYinghai Lu 	memblock_reserve(__pa_symbol(_text),
852c603a309SThomas Lendacky 			 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
8536c902b65SYinghai Lu 
85410a70416SAndi Kleen 	/*
85510a70416SAndi Kleen 	 * Make sure page 0 is always reserved because on systems with
85610a70416SAndi Kleen 	 * L1TF its contents can be leaked to user processes.
85710a70416SAndi Kleen 	 */
85810a70416SAndi Kleen 	memblock_reserve(0, PAGE_SIZE);
85910a70416SAndi Kleen 
8601b8c78beSYinghai Lu 	early_reserve_initrd();
8611b8c78beSYinghai Lu 
8626c902b65SYinghai Lu 	/*
8636c902b65SYinghai Lu 	 * At this point everything still needed from the boot loader
8646c902b65SYinghai Lu 	 * or BIOS or kernel text should be early reserved or marked not
8656c902b65SYinghai Lu 	 * RAM in e820. All other memory is free game.
8666c902b65SYinghai Lu 	 */
8676c902b65SYinghai Lu 
86855f26239SYinghai Lu #ifdef CONFIG_X86_32
86955f26239SYinghai Lu 	memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
870b40827faSBorislav Petkov 
871b40827faSBorislav Petkov 	/*
872b40827faSBorislav Petkov 	 * copy kernel address range established so far and switch
873b40827faSBorislav Petkov 	 * to the proper swapper page table
874b40827faSBorislav Petkov 	 */
875b40827faSBorislav Petkov 	clone_pgd_range(swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
876b40827faSBorislav Petkov 			initial_page_table + KERNEL_PGD_BOUNDARY,
877b40827faSBorislav Petkov 			KERNEL_PGD_PTRS);
878b40827faSBorislav Petkov 
879b40827faSBorislav Petkov 	load_cr3(swapper_pg_dir);
8802075244fSBryan O'Donoghue 	/*
8812075244fSBryan O'Donoghue 	 * Note: Quark X1000 CPUs advertise PGE incorrectly and require
8822075244fSBryan O'Donoghue 	 * a cr3 based tlb flush, so the following __flush_tlb_all()
8832075244fSBryan O'Donoghue 	 * will not flush anything because the cpu quirk which clears
8842075244fSBryan O'Donoghue 	 * X86_FEATURE_PGE has not been invoked yet. Though due to the
8852075244fSBryan O'Donoghue 	 * load_cr3() above the TLB has been flushed already. The
8862075244fSBryan O'Donoghue 	 * quirk is invoked before subsequent calls to __flush_tlb_all()
8872075244fSBryan O'Donoghue 	 * so proper operation is guaranteed.
8882075244fSBryan O'Donoghue 	 */
889b40827faSBorislav Petkov 	__flush_tlb_all();
89055f26239SYinghai Lu #else
89155f26239SYinghai Lu 	printk(KERN_INFO "Command line: %s\n", boot_command_line);
892162434e7SKirill A. Shutemov 	boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS;
89355f26239SYinghai Lu #endif
89455f26239SYinghai Lu 
8959863c90fSAlok Kataria 	/*
8969863c90fSAlok Kataria 	 * If we have OLPC OFW, we might end up relocating the fixmap due to
8979863c90fSAlok Kataria 	 * reserve_top(), so do this before touching the ioremap area.
8989863c90fSAlok Kataria 	 */
899fd699c76SAndres Salomon 	olpc_ofw_detect();
900fd699c76SAndres Salomon 
901433f8924SThomas Gleixner 	idt_setup_early_traps();
9029e882c92SJeremy Fitzhardinge 	early_cpu_init();
9038990cac6SPavel Tatashin 	arch_init_ideal_nops();
9048990cac6SPavel Tatashin 	jump_label_init();
9051a98fd14SJeremy Fitzhardinge 	early_ioremap_init();
9061a98fd14SJeremy Fitzhardinge 
907fd699c76SAndres Salomon 	setup_olpc_ofw_pgd();
908fd699c76SAndres Salomon 
90955f26239SYinghai Lu 	ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
91055f26239SYinghai Lu 	screen_info = boot_params.screen_info;
91155f26239SYinghai Lu 	edid_info = boot_params.edid_info;
91255f26239SYinghai Lu #ifdef CONFIG_X86_32
91355f26239SYinghai Lu 	apm_info.bios = boot_params.apm_bios_info;
91455f26239SYinghai Lu 	ist_info = boot_params.ist_info;
91555f26239SYinghai Lu #endif
91655f26239SYinghai Lu 	saved_video_mode = boot_params.hdr.vid_mode;
91755f26239SYinghai Lu 	bootloader_type = boot_params.hdr.type_of_loader;
9185031296cSH. Peter Anvin 	if ((bootloader_type >> 4) == 0xe) {
9195031296cSH. Peter Anvin 		bootloader_type &= 0xf;
9205031296cSH. Peter Anvin 		bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4;
9215031296cSH. Peter Anvin 	}
9225031296cSH. Peter Anvin 	bootloader_version  = bootloader_type & 0xf;
9235031296cSH. Peter Anvin 	bootloader_version |= boot_params.hdr.ext_loader_ver << 4;
92455f26239SYinghai Lu 
92555f26239SYinghai Lu #ifdef CONFIG_BLK_DEV_RAM
92655f26239SYinghai Lu 	rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
92755f26239SYinghai Lu 	rd_prompt = ((boot_params.hdr.ram_size & RAMDISK_PROMPT_FLAG) != 0);
92855f26239SYinghai Lu 	rd_doload = ((boot_params.hdr.ram_size & RAMDISK_LOAD_FLAG) != 0);
92955f26239SYinghai Lu #endif
93055f26239SYinghai Lu #ifdef CONFIG_EFI
93155f26239SYinghai Lu 	if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
9329402973dSDaniel Kiper 		     EFI32_LOADER_SIGNATURE, 4)) {
9333e909599SMatt Fleming 		set_bit(EFI_BOOT, &efi.flags);
9341adbfa35SOlof Johansson 	} else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
9359402973dSDaniel Kiper 		     EFI64_LOADER_SIGNATURE, 4)) {
9363e909599SMatt Fleming 		set_bit(EFI_BOOT, &efi.flags);
9373e909599SMatt Fleming 		set_bit(EFI_64BIT, &efi.flags);
93855f26239SYinghai Lu 	}
93955f26239SYinghai Lu #endif
94055f26239SYinghai Lu 
94142bbdb43SThomas Gleixner 	x86_init.oem.arch_setup();
94255f26239SYinghai Lu 
943419afdf5SBjorn Helgaas 	iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
944103e2063SIngo Molnar 	e820__memory_setup();
94528bb2237SYinghai Lu 	parse_setup_data();
94628bb2237SYinghai Lu 
94755f26239SYinghai Lu 	copy_edd();
94855f26239SYinghai Lu 
94955f26239SYinghai Lu 	if (!boot_params.hdr.root_flags)
95055f26239SYinghai Lu 		root_mountflags &= ~MS_RDONLY;
95155f26239SYinghai Lu 	init_mm.start_code = (unsigned long) _text;
95255f26239SYinghai Lu 	init_mm.end_code = (unsigned long) _etext;
95355f26239SYinghai Lu 	init_mm.end_data = (unsigned long) _edata;
95493dbda7cSJeremy Fitzhardinge 	init_mm.brk = _brk_end;
95555f26239SYinghai Lu 
956fe3d197fSDave Hansen 	mpx_mm_init(&init_mm);
957fe3d197fSDave Hansen 
9584046d6e8SLinus Torvalds 	code_resource.start = __pa_symbol(_text);
9594046d6e8SLinus Torvalds 	code_resource.end = __pa_symbol(_etext)-1;
9604046d6e8SLinus Torvalds 	data_resource.start = __pa_symbol(_etext);
9614046d6e8SLinus Torvalds 	data_resource.end = __pa_symbol(_edata)-1;
9624046d6e8SLinus Torvalds 	bss_resource.start = __pa_symbol(__bss_start);
9634046d6e8SLinus Torvalds 	bss_resource.end = __pa_symbol(__bss_stop)-1;
9644046d6e8SLinus Torvalds 
965516cbf37STim Bird #ifdef CONFIG_CMDLINE_BOOL
966516cbf37STim Bird #ifdef CONFIG_CMDLINE_OVERRIDE
967516cbf37STim Bird 	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
968516cbf37STim Bird #else
969516cbf37STim Bird 	if (builtin_cmdline[0]) {
970516cbf37STim Bird 		/* append boot loader cmdline to builtin */
971516cbf37STim Bird 		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
972516cbf37STim Bird 		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
973516cbf37STim Bird 		strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
974516cbf37STim Bird 	}
975516cbf37STim Bird #endif
976516cbf37STim Bird #endif
977516cbf37STim Bird 
978eda6da92SYinghai Lu 	strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
979eda6da92SYinghai Lu 	*cmdline_p = command_line;
980eda6da92SYinghai Lu 
981eda6da92SYinghai Lu 	/*
9824b0f3b81SKees Cook 	 * x86_configure_nx() is called before parse_early_param() to detect
9834b0f3b81SKees Cook 	 * whether hardware doesn't support NX (so that the early EHCI debug
9844b0f3b81SKees Cook 	 * console setup can safely call set_fixmap()). It may then be called
9854b0f3b81SKees Cook 	 * again from within noexec_setup() during parsing early parameters
9864b0f3b81SKees Cook 	 * to honor the respective command line option.
987eda6da92SYinghai Lu 	 */
9884763ed4dSH. Peter Anvin 	x86_configure_nx();
989eda6da92SYinghai Lu 
990eda6da92SYinghai Lu 	parse_early_param();
991eda6da92SYinghai Lu 
992835bcec5SDave Young 	if (efi_enabled(EFI_BOOT))
993835bcec5SDave Young 		efi_memblock_x86_reserve_range();
99439fa104dSReza Arbab #ifdef CONFIG_MEMORY_HOTPLUG
99539fa104dSReza Arbab 	/*
99639fa104dSReza Arbab 	 * Memory used by the kernel cannot be hot-removed because Linux
99739fa104dSReza Arbab 	 * cannot migrate the kernel pages. When memory hotplug is
99839fa104dSReza Arbab 	 * enabled, we should prevent memblock from allocating memory
99939fa104dSReza Arbab 	 * for the kernel.
100039fa104dSReza Arbab 	 *
100139fa104dSReza Arbab 	 * ACPI SRAT records all hotpluggable memory ranges. But before
100239fa104dSReza Arbab 	 * SRAT is parsed, we don't know about it.
100339fa104dSReza Arbab 	 *
100439fa104dSReza Arbab 	 * The kernel image is loaded into memory at very early time. We
100539fa104dSReza Arbab 	 * cannot prevent this anyway. So on NUMA system, we set any
100639fa104dSReza Arbab 	 * node the kernel resides in as un-hotpluggable.
100739fa104dSReza Arbab 	 *
100839fa104dSReza Arbab 	 * Since on modern servers, one node could have double-digit
100939fa104dSReza Arbab 	 * gigabytes memory, we can assume the memory around the kernel
101039fa104dSReza Arbab 	 * image is also un-hotpluggable. So before SRAT is parsed, just
101139fa104dSReza Arbab 	 * allocate memory near the kernel image to try the best to keep
101239fa104dSReza Arbab 	 * the kernel away from hotpluggable memory.
101339fa104dSReza Arbab 	 */
101439fa104dSReza Arbab 	if (movable_node_is_enabled())
101539fa104dSReza Arbab 		memblock_set_bottom_up(true);
101639fa104dSReza Arbab #endif
101739fa104dSReza Arbab 
10184b0f3b81SKees Cook 	x86_report_nx();
10190ad5bce7SJeremy Fitzhardinge 
102028bb2237SYinghai Lu 	/* after early param, so could get panic from serial */
1021a9ce6bc1SYinghai Lu 	memblock_x86_reserve_range_setup_data();
102228bb2237SYinghai Lu 
102355f26239SYinghai Lu 	if (acpi_mps_check()) {
102455f26239SYinghai Lu #ifdef CONFIG_X86_LOCAL_APIC
102555f26239SYinghai Lu 		disable_apic = 1;
102655f26239SYinghai Lu #endif
1027988781dcSYinghai Lu 		setup_clear_cpu_cap(X86_FEATURE_APIC);
102855f26239SYinghai Lu 	}
102955f26239SYinghai Lu 
10301a127034SIngo Molnar 	e820__reserve_setup_data();
10319641bdafSIngo Molnar 	e820__finish_early_params();
103255f26239SYinghai Lu 
103383e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT))
1034ff0c0874SBrian Maly 		efi_init();
1035ff0c0874SBrian Maly 
10360fca0812SRobert Richter 	dmi_setup();
10372216d199SYinghai Lu 
103888b094fbSAlok Kataria 	/*
103988b094fbSAlok Kataria 	 * VMware detection requires dmi to be available, so this
10400fca0812SRobert Richter 	 * needs to be done after dmi_setup(), for the boot CPU.
104188b094fbSAlok Kataria 	 */
10422d826404SThomas Gleixner 	init_hypervisor_platform();
104388b094fbSAlok Kataria 
1044cf7a63efSPavel Tatashin 	tsc_early_init();
1045f7cf5a5bSThomas Gleixner 	x86_init.resources.probe_roms();
104655f26239SYinghai Lu 
10474046d6e8SLinus Torvalds 	/* after parse_early_param, so could debug it */
10484046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &code_resource);
10494046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &data_resource);
10504046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &bss_resource);
10514046d6e8SLinus Torvalds 
1052b422a309SYinghai Lu 	e820_add_kernel_range();
10531b5576e6SYinghai Lu 	trim_bios_range();
105455f26239SYinghai Lu #ifdef CONFIG_X86_32
105555f26239SYinghai Lu 	if (ppro_with_ram_bug()) {
105609821ff1SIngo Molnar 		e820__range_update(0x70000000ULL, 0x40000ULL, E820_TYPE_RAM,
105709821ff1SIngo Molnar 				  E820_TYPE_RESERVED);
1058f9748fa0SIngo Molnar 		e820__update_table(e820_table);
105955f26239SYinghai Lu 		printk(KERN_INFO "fixed physical RAM map:\n");
1060be0c3f0fSIngo Molnar 		e820__print_table("bad_ppro");
106155f26239SYinghai Lu 	}
106255f26239SYinghai Lu #else
106355f26239SYinghai Lu 	early_gart_iommu_check();
106455f26239SYinghai Lu #endif
106555f26239SYinghai Lu 
106655f26239SYinghai Lu 	/*
106755f26239SYinghai Lu 	 * partially used pages are not usable - thus
106855f26239SYinghai Lu 	 * we are rounding upwards:
106955f26239SYinghai Lu 	 */
10700c6fc11aSIngo Molnar 	max_pfn = e820__end_of_ram_pfn();
107155f26239SYinghai Lu 
107255f26239SYinghai Lu 	/* update e820 for memory not covered by WB MTRRs */
107355f26239SYinghai Lu 	mtrr_bp_init();
10742dc807b3SYinghai Lu 	if (mtrr_trim_uncached_memory(max_pfn))
10750c6fc11aSIngo Molnar 		max_pfn = e820__end_of_ram_pfn();
107655f26239SYinghai Lu 
10778dd33030SIgor Mammedov 	max_possible_pfn = max_pfn;
10788dd33030SIgor Mammedov 
1079c7d2361fSThomas Garnier 	/*
108099c13b8cSMikulas Patocka 	 * This call is required when the CPU does not support PAT. If
108199c13b8cSMikulas Patocka 	 * mtrr_bp_init() invoked it already via pat_init() the call has no
108299c13b8cSMikulas Patocka 	 * effect.
108399c13b8cSMikulas Patocka 	 */
108499c13b8cSMikulas Patocka 	init_cache_modes();
108599c13b8cSMikulas Patocka 
108699c13b8cSMikulas Patocka 	/*
1087c7d2361fSThomas Garnier 	 * Define random base addresses for memory sections after max_pfn is
1088c7d2361fSThomas Garnier 	 * defined and before each memory section base is used.
1089c7d2361fSThomas Garnier 	 */
1090c7d2361fSThomas Garnier 	kernel_randomize_memory();
1091c7d2361fSThomas Garnier 
109255f26239SYinghai Lu #ifdef CONFIG_X86_32
109355f26239SYinghai Lu 	/* max_low_pfn get updated here */
109455f26239SYinghai Lu 	find_low_pfn_range();
109555f26239SYinghai Lu #else
10966e1cb38aSSuresh Siddha 	check_x2apic();
109755f26239SYinghai Lu 
109855f26239SYinghai Lu 	/* How many end-of-memory variables you have, grandma! */
109955f26239SYinghai Lu 	/* need this before calling reserve_initrd */
1100f361a450SYinghai Lu 	if (max_pfn > (1UL<<(32 - PAGE_SHIFT)))
11010c6fc11aSIngo Molnar 		max_low_pfn = e820__end_of_low_ram_pfn();
1102f361a450SYinghai Lu 	else
110355f26239SYinghai Lu 		max_low_pfn = max_pfn;
1104f361a450SYinghai Lu 
110555f26239SYinghai Lu 	high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
110655f26239SYinghai Lu #endif
110755f26239SYinghai Lu 
1108893f38d1SYinghai Lu 	/*
1109893f38d1SYinghai Lu 	 * Find and reserve possible boot-time SMP configuration:
1110893f38d1SYinghai Lu 	 */
1111893f38d1SYinghai Lu 	find_smp_config();
1112893f38d1SYinghai Lu 
1113042be38eSYinghai Lu 	reserve_ibft_region();
1114042be38eSYinghai Lu 
11158d57470dSYinghai Lu 	early_alloc_pgt_buf();
11168d57470dSYinghai Lu 
111772d7c3b3SYinghai Lu 	/*
11184918e228SIngo Molnar 	 * Need to conclude brk, before e820__memblock_setup()
111972d7c3b3SYinghai Lu 	 *  it could use memblock_find_in_range, could overlap with
112072d7c3b3SYinghai Lu 	 *  brk area.
112172d7c3b3SYinghai Lu 	 */
112272d7c3b3SYinghai Lu 	reserve_brk();
112372d7c3b3SYinghai Lu 
1124e5f15b45SYinghai Lu 	cleanup_highmap();
1125e5f15b45SYinghai Lu 
11262449f343STang Chen 	memblock_set_current_limit(ISA_END_ADDRESS);
11274918e228SIngo Molnar 	e820__memblock_setup();
112872d7c3b3SYinghai Lu 
1129007b7560SAndy Lutomirski 	reserve_bios_regions();
1130007b7560SAndy Lutomirski 
11314971531aSMatt Fleming 	if (efi_enabled(EFI_MEMMAP)) {
11324971531aSMatt Fleming 		efi_fake_memmap();
11334971531aSMatt Fleming 		efi_find_mirror();
11343dad6f7fSRicardo Neri 		efi_esrt_init();
11354971531aSMatt Fleming 
1136916f676fSMatthew Garrett 		/*
11374971531aSMatt Fleming 		 * The EFI specification says that boot service code won't be
11384971531aSMatt Fleming 		 * called after ExitBootServices(). This is, in fact, a lie.
1139916f676fSMatthew Garrett 		 */
1140916f676fSMatthew Garrett 		efi_reserve_boot_services();
11414971531aSMatt Fleming 	}
1142916f676fSMatthew Garrett 
114372d7c3b3SYinghai Lu 	/* preallocate 4k for mptable mpc */
11445da217caSIngo Molnar 	e820__memblock_alloc_reserved_mpc_new();
114572d7c3b3SYinghai Lu 
114672d7c3b3SYinghai Lu #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
114772d7c3b3SYinghai Lu 	setup_bios_corruption_check();
114872d7c3b3SYinghai Lu #endif
114972d7c3b3SYinghai Lu 
115010054230SYinghai Lu #ifdef CONFIG_X86_32
1151365811d6SBjorn Helgaas 	printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n",
1152365811d6SBjorn Helgaas 			(max_pfn_mapped<<PAGE_SHIFT) - 1);
115310054230SYinghai Lu #endif
115472d7c3b3SYinghai Lu 
11554f7b9226SYinghai Lu 	reserve_real_mode();
1156893f38d1SYinghai Lu 
1157a9acc536SJesse Barnes 	trim_platform_memory_ranges();
115895c96084SH. Peter Anvin 	trim_low_memory_range();
1159a9acc536SJesse Barnes 
116022ddfcaaSYinghai Lu 	init_mem_mapping();
1161854c879fSPekka J Enberg 
1162433f8924SThomas Gleixner 	idt_setup_early_pf();
1163cda846f1SJarkko Sakkinen 
116418bc7bd5SAndy Lutomirski 	/*
116518bc7bd5SAndy Lutomirski 	 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features)
116618bc7bd5SAndy Lutomirski 	 * with the current CR4 value.  This may not be necessary, but
116718bc7bd5SAndy Lutomirski 	 * auditing all the early-boot CR4 manipulation would be needed to
116818bc7bd5SAndy Lutomirski 	 * rule it out.
1169c7ad5ad2SAndy Lutomirski 	 *
1170c7ad5ad2SAndy Lutomirski 	 * Mask off features that don't work outside long mode (just
1171c7ad5ad2SAndy Lutomirski 	 * PCIDE for now).
117218bc7bd5SAndy Lutomirski 	 */
1173c7ad5ad2SAndy Lutomirski 	mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE;
117418bc7bd5SAndy Lutomirski 
11754ce7a869SYinghai Lu 	memblock_set_current_limit(get_max_mapped());
1176e7b37895SYinghai Lu 
1177e7b37895SYinghai Lu 	/*
1178e7b37895SYinghai Lu 	 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
1179e7b37895SYinghai Lu 	 */
1180e7b37895SYinghai Lu 
1181e7b37895SYinghai Lu #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
1182e7b37895SYinghai Lu 	if (init_ohci1394_dma_early)
1183162a7e75SMike Travis 		init_ohci1394_dma_on_all_controllers();
1184162a7e75SMike Travis #endif
1185e7b37895SYinghai Lu 	/* Allocate bigger log buffer */
118655f26239SYinghai Lu 	setup_log_buf(1);
118755f26239SYinghai Lu 
11889661b332SDavid Howells 	if (efi_enabled(EFI_BOOT)) {
11899661b332SDavid Howells 		switch (boot_params.secure_boot) {
11909661b332SDavid Howells 		case efi_secureboot_mode_disabled:
11919661b332SDavid Howells 			pr_info("Secure boot disabled\n");
11929661b332SDavid Howells 			break;
11939661b332SDavid Howells 		case efi_secureboot_mode_enabled:
11949661b332SDavid Howells 			pr_info("Secure boot enabled\n");
11959661b332SDavid Howells 			break;
11969661b332SDavid Howells 		default:
11979661b332SDavid Howells 			pr_info("Secure boot could not be determined\n");
11989661b332SDavid Howells 			break;
11999661b332SDavid Howells 		}
12009661b332SDavid Howells 	}
12019661b332SDavid Howells 
120244280733SYinghai Lu 	reserve_initrd();
120344280733SYinghai Lu 
1204da3d3f98SAleksey Makarov 	acpi_table_upgrade();
120555f26239SYinghai Lu 
120655f26239SYinghai Lu 	vsmp_init();
120755f26239SYinghai Lu 
120855f26239SYinghai Lu 	io_delay_init();
120955f26239SYinghai Lu 
1210630b3affSLukas Wunner 	early_platform_quirks();
1211630b3affSLukas Wunner 
121255f26239SYinghai Lu 	/*
121355f26239SYinghai Lu 	 * Parse the ACPI tables for possible boot-time SMP configuration.
121455f26239SYinghai Lu 	 */
121520e6926dSYinghai Lu 	acpi_boot_table_init();
121620e6926dSYinghai Lu 
121720e6926dSYinghai Lu 	early_acpi_boot_init();
121820e6926dSYinghai Lu 
121991467bdfSBernhard Walle 	initmem_init();
12203c325f82SWeijie Yang 	dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
1221fa591c4aSTang Chen 
1222fa591c4aSTang Chen 	/*
1223fa591c4aSTang Chen 	 * Reserve memory for crash kernel after SRAT is parsed so that it
1224fa591c4aSTang Chen 	 * won't consume hotpluggable memory.
1225fa591c4aSTang Chen 	 */
1226fa591c4aSTang Chen 	reserve_crashkernel();
1227fa591c4aSTang Chen 
122855f26239SYinghai Lu 	memblock_find_dma_reserve();
122955f26239SYinghai Lu 
1230ccb64941SBoris Ostrovsky 	if (!early_xdbc_setup_hardware())
1231ccb64941SBoris Ostrovsky 		early_xdbc_register_console();
1232ccb64941SBoris Ostrovsky 
1233030cb6c0SThomas Gleixner 	x86_init.paging.pagetable_init();
123455f26239SYinghai Lu 
1235ef7f0d6aSAndrey Ryabinin 	kasan_init();
1236ef7f0d6aSAndrey Ryabinin 
1237d2b6dc61SAndy Lutomirski 	/*
1238945fd17aSThomas Gleixner 	 * Sync back kernel address range.
1239945fd17aSThomas Gleixner 	 *
1240945fd17aSThomas Gleixner 	 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
1241945fd17aSThomas Gleixner 	 * this call?
1242d2b6dc61SAndy Lutomirski 	 */
1243945fd17aSThomas Gleixner 	sync_initial_page_table();
1244d2b6dc61SAndy Lutomirski 
124531625340SJoseph Cihula 	tboot_probe();
124631625340SJoseph Cihula 
124755f26239SYinghai Lu 	map_vsyscall();
124855f26239SYinghai Lu 
124955f26239SYinghai Lu 	generic_apic_probe();
125055f26239SYinghai Lu 
125155f26239SYinghai Lu 	early_quirks();
125255f26239SYinghai Lu 
125355f26239SYinghai Lu 	/*
125455f26239SYinghai Lu 	 * Read APIC and some other early information from ACPI tables.
125555f26239SYinghai Lu 	 */
125655f26239SYinghai Lu 	acpi_boot_init();
1257efafc8b2SFeng Tang 	sfi_init();
1258a906fdaaSThomas Gleixner 	x86_dtb_init();
1259efafc8b2SFeng Tang 
126055f26239SYinghai Lu 	/*
126155f26239SYinghai Lu 	 * get boot-time SMP configuration:
126255f26239SYinghai Lu 	 */
126355f26239SYinghai Lu 	get_smp_config();
126455f26239SYinghai Lu 
12651e90a13dSThomas Gleixner 	/*
12661e90a13dSThomas Gleixner 	 * Systems w/o ACPI and mptables might not have it mapped the local
12671e90a13dSThomas Gleixner 	 * APIC yet, but prefill_possible_map() might need to access it.
12681e90a13dSThomas Gleixner 	 */
12691e90a13dSThomas Gleixner 	init_apic_mappings();
12701e90a13dSThomas Gleixner 
1271329513a3SYinghai Lu 	prefill_possible_map();
1272301e6190SYinghai Lu 
12735f4765f9SYinghai Lu 	init_cpu_to_node();
12745f4765f9SYinghai Lu 
1275ca1b8862SThomas Gleixner 	io_apic_init_mappings();
12769d6a4d08SYinghai Lu 
1277f3614646SJuergen Gross 	x86_init.hyper.guest_late_init();
127855f26239SYinghai Lu 
12791506c8dcSIngo Molnar 	e820__reserve_resources();
1280cc55f753SZhimin Gu 	e820__register_nosave_regions(max_pfn);
128155f26239SYinghai Lu 
12828fee697dSThomas Gleixner 	x86_init.resources.reserve_resources();
128355f26239SYinghai Lu 
12842df908baSIngo Molnar 	e820__setup_pci_gap();
128555f26239SYinghai Lu 
128655f26239SYinghai Lu #ifdef CONFIG_VT
128755f26239SYinghai Lu #if defined(CONFIG_VGA_CONSOLE)
128883e68189SMatt Fleming 	if (!efi_enabled(EFI_BOOT) || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
128955f26239SYinghai Lu 		conswitchp = &vga_con;
129055f26239SYinghai Lu #elif defined(CONFIG_DUMMY_CONSOLE)
129155f26239SYinghai Lu 	conswitchp = &dummy_con;
129255f26239SYinghai Lu #endif
129355f26239SYinghai Lu #endif
12946f30c1acSThomas Gleixner 	x86_init.oem.banner();
1295a2202aa2SYong Wang 
12966b617e22SFeng Tang 	x86_init.timers.wallclock_init();
12976b617e22SFeng Tang 
1298a2202aa2SYong Wang 	mcheck_init();
1299f49aa448SJason Baron 
1300b3c869d3SJohn Stultz 	register_refined_jiffies(CLOCK_TICK_RATE);
13015189c2a7SOlof Johansson 
13025189c2a7SOlof Johansson #ifdef CONFIG_EFI
1303a5d90c92SBorislav Petkov 	if (efi_enabled(EFI_BOOT))
1304a5d90c92SBorislav Petkov 		efi_apply_memmap_quirks();
13055189c2a7SOlof Johansson #endif
1306ee9f8fceSJosh Poimboeuf 
1307ee9f8fceSJosh Poimboeuf 	unwind_init();
130855f26239SYinghai Lu }
13095649b7c3SIngo Molnar 
13109be1b56aSIngo Molnar #ifdef CONFIG_X86_32
13115649b7c3SIngo Molnar 
13128fee697dSThomas Gleixner static struct resource video_ram_resource = {
13138fee697dSThomas Gleixner 	.name	= "Video RAM area",
13148fee697dSThomas Gleixner 	.start	= 0xa0000,
13158fee697dSThomas Gleixner 	.end	= 0xbffff,
13168fee697dSThomas Gleixner 	.flags	= IORESOURCE_BUSY | IORESOURCE_MEM
13179be1b56aSIngo Molnar };
13189be1b56aSIngo Molnar 
13198fee697dSThomas Gleixner void __init i386_reserve_resources(void)
13209be1b56aSIngo Molnar {
13218fee697dSThomas Gleixner 	request_resource(&iomem_resource, &video_ram_resource);
13228fee697dSThomas Gleixner 	reserve_standard_io_resources();
13239be1b56aSIngo Molnar }
13249be1b56aSIngo Molnar 
13259be1b56aSIngo Molnar #endif /* CONFIG_X86_32 */
1326f32360efSKees Cook 
1327f32360efSKees Cook static struct notifier_block kernel_offset_notifier = {
1328f32360efSKees Cook 	.notifier_call = dump_kernel_offset
1329f32360efSKees Cook };
1330f32360efSKees Cook 
1331f32360efSKees Cook static int __init register_kernel_offset_dumper(void)
1332f32360efSKees Cook {
1333f32360efSKees Cook 	atomic_notifier_chain_register(&panic_notifier_list,
1334f32360efSKees Cook 					&kernel_offset_notifier);
1335f32360efSKees Cook 	return 0;
1336f32360efSKees Cook }
1337f32360efSKees Cook __initcall(register_kernel_offset_dumper);
1338