xref: /linux-6.15/arch/x86/kernel/setup.c (revision 30120d72)
1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
255f26239SYinghai Lu /*
355f26239SYinghai Lu  *  Copyright (C) 1995  Linus Torvalds
455f26239SYinghai Lu  *
512609013SIngo Molnar  * This file contains the setup_arch() code, which handles the architecture-dependent
612609013SIngo Molnar  * parts of early kernel initialization.
755f26239SYinghai Lu  */
855f26239SYinghai Lu #include <linux/console.h>
912609013SIngo Molnar #include <linux/crash_dump.h>
100b1abd1fSChristoph Hellwig #include <linux/dma-map-ops.h>
1155f26239SYinghai Lu #include <linux/dmi.h>
1212609013SIngo Molnar #include <linux/efi.h>
1355f26239SYinghai Lu #include <linux/init_ohci1394_dma.h>
1412609013SIngo Molnar #include <linux/initrd.h>
1512609013SIngo Molnar #include <linux/iscsi_ibft.h>
1612609013SIngo Molnar #include <linux/memblock.h>
17f39650deSAndy Shevchenko #include <linux/panic_notifier.h>
1812609013SIngo Molnar #include <linux/pci.h>
1912609013SIngo Molnar #include <linux/root_dev.h>
20cf11e85fSRoman Gushchin #include <linux/hugetlb.h>
2112609013SIngo Molnar #include <linux/tboot.h>
2212609013SIngo Molnar #include <linux/usb/xhci-dbgp.h>
23a945c834SPeter Zijlstra #include <linux/static_call.h>
240a0f0d8bSChristoph Hellwig #include <linux/swiotlb.h>
2512609013SIngo Molnar 
26e262e32dSDavid Howells #include <uapi/linux/mount.h>
2755f26239SYinghai Lu 
2812609013SIngo Molnar #include <xen/xen.h>
2955f26239SYinghai Lu 
3055f26239SYinghai Lu #include <asm/apic.h>
3113c01139SIngo Molnar #include <asm/numa.h>
3255f26239SYinghai Lu #include <asm/bios_ebda.h>
3355f26239SYinghai Lu #include <asm/bugs.h>
346e5385d4SJaswinder Singh Rajput #include <asm/cpu.h>
3512609013SIngo Molnar #include <asm/efi.h>
361d9b16d1SJoerg Roedel #include <asm/gart.h>
3788b094fbSAlok Kataria #include <asm/hypervisor.h>
3812609013SIngo Molnar #include <asm/io_apic.h>
3912609013SIngo Molnar #include <asm/kasan.h>
400483e1faSThomas Garnier #include <asm/kaslr.h>
4112609013SIngo Molnar #include <asm/mce.h>
4212609013SIngo Molnar #include <asm/mtrr.h>
43ca947b72SSean Christopherson #include <asm/realmode.h>
4412609013SIngo Molnar #include <asm/olpc_ofw.h>
4512609013SIngo Molnar #include <asm/pci-direct.h>
4612609013SIngo Molnar #include <asm/prom.h>
4712609013SIngo Molnar #include <asm/proto.h>
489a90ed06SBorislav Petkov #include <asm/thermal.h>
49ee9f8fceSJosh Poimboeuf #include <asm/unwind.h>
5012609013SIngo Molnar #include <asm/vsyscall.h>
51186525bdSIngo Molnar #include <linux/vmalloc.h>
5255f26239SYinghai Lu 
532b72394eSPekka Enberg /*
54360db4acSIngo Molnar  * max_low_pfn_mapped: highest directly mapped pfn < 4 GB
55360db4acSIngo Molnar  * max_pfn_mapped:     highest directly mapped pfn > 4 GB
5666520ebcSJacob Shin  *
5709821ff1SIngo Molnar  * The direct mapping only covers E820_TYPE_RAM regions, so the ranges and gaps are
58360db4acSIngo Molnar  * represented by pfn_mapped[].
592b72394eSPekka Enberg  */
602b72394eSPekka Enberg unsigned long max_low_pfn_mapped;
612b72394eSPekka Enberg unsigned long max_pfn_mapped;
622b72394eSPekka Enberg 
63e808bae2SThadeu Lima de Souza Cascardo #ifdef CONFIG_DMI
64796216a5SJeremy Fitzhardinge RESERVE_BRK(dmi_alloc, 65536);
65e808bae2SThadeu Lima de Souza Cascardo #endif
66796216a5SJeremy Fitzhardinge 
67c0b5842aSIngo Molnar 
68360db4acSIngo Molnar /*
69360db4acSIngo Molnar  * Range of the BSS area. The size of the BSS area is determined
7081519f77SCao jin  * at link time, with RESERVE_BRK() facility reserving additional
71360db4acSIngo Molnar  * chunks.
72360db4acSIngo Molnar  */
73360db4acSIngo Molnar unsigned long _brk_start = (unsigned long)__brk_base;
7493dbda7cSJeremy Fitzhardinge unsigned long _brk_end   = (unsigned long)__brk_base;
7593dbda7cSJeremy Fitzhardinge 
76217b8ce8SYinghai Lu struct boot_params boot_params;
77217b8ce8SYinghai Lu 
784046d6e8SLinus Torvalds /*
79360db4acSIngo Molnar  * These are the four main kernel memory regions, we put them into
80360db4acSIngo Molnar  * the resource tree so that kdump tools and other debugging tools
81360db4acSIngo Molnar  * recover it:
824046d6e8SLinus Torvalds  */
83360db4acSIngo Molnar 
84a3299754SKees Cook static struct resource rodata_resource = {
85a3299754SKees Cook 	.name	= "Kernel rodata",
86a3299754SKees Cook 	.start	= 0,
87a3299754SKees Cook 	.end	= 0,
88a3299754SKees Cook 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
89a3299754SKees Cook };
90a3299754SKees Cook 
914046d6e8SLinus Torvalds static struct resource data_resource = {
924046d6e8SLinus Torvalds 	.name	= "Kernel data",
934046d6e8SLinus Torvalds 	.start	= 0,
944046d6e8SLinus Torvalds 	.end	= 0,
954046d6e8SLinus Torvalds 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
964046d6e8SLinus Torvalds };
974046d6e8SLinus Torvalds 
984046d6e8SLinus Torvalds static struct resource code_resource = {
994046d6e8SLinus Torvalds 	.name	= "Kernel code",
1004046d6e8SLinus Torvalds 	.start	= 0,
1014046d6e8SLinus Torvalds 	.end	= 0,
1024046d6e8SLinus Torvalds 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
1034046d6e8SLinus Torvalds };
1044046d6e8SLinus Torvalds 
1054046d6e8SLinus Torvalds static struct resource bss_resource = {
1064046d6e8SLinus Torvalds 	.name	= "Kernel bss",
1074046d6e8SLinus Torvalds 	.start	= 0,
1084046d6e8SLinus Torvalds 	.end	= 0,
1094046d6e8SLinus Torvalds 	.flags	= IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM
1104046d6e8SLinus Torvalds };
1114046d6e8SLinus Torvalds 
1124046d6e8SLinus Torvalds 
11355f26239SYinghai Lu #ifdef CONFIG_X86_32
114360db4acSIngo Molnar /* CPU data as detected by the assembly code in head_32.S */
1156415813bSMathias Krause struct cpuinfo_x86 new_cpu_data;
1166415813bSMathias Krause 
117360db4acSIngo Molnar /* Common CPU data for all CPUs */
1186415813bSMathias Krause struct cpuinfo_x86 boot_cpu_data __read_mostly;
11955f26239SYinghai Lu EXPORT_SYMBOL(boot_cpu_data);
12055f26239SYinghai Lu 
12155f26239SYinghai Lu unsigned int def_to_bigsmp;
12255f26239SYinghai Lu 
12355f26239SYinghai Lu struct apm_info apm_info;
12455f26239SYinghai Lu EXPORT_SYMBOL(apm_info);
12555f26239SYinghai Lu 
12655f26239SYinghai Lu #if defined(CONFIG_X86_SPEEDSTEP_SMI) || \
12755f26239SYinghai Lu 	defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE)
12855f26239SYinghai Lu struct ist_info ist_info;
12955f26239SYinghai Lu EXPORT_SYMBOL(ist_info);
13055f26239SYinghai Lu #else
13155f26239SYinghai Lu struct ist_info ist_info;
13255f26239SYinghai Lu #endif
13355f26239SYinghai Lu 
13455f26239SYinghai Lu #else
135162434e7SKirill A. Shutemov struct cpuinfo_x86 boot_cpu_data __read_mostly;
13655f26239SYinghai Lu EXPORT_SYMBOL(boot_cpu_data);
13755f26239SYinghai Lu #endif
13855f26239SYinghai Lu 
13955f26239SYinghai Lu 
14055f26239SYinghai Lu #if !defined(CONFIG_X86_PAE) || defined(CONFIG_X86_64)
141404f6aacSKees Cook __visible unsigned long mmu_cr4_features __ro_after_init;
14255f26239SYinghai Lu #else
143404f6aacSKees Cook __visible unsigned long mmu_cr4_features __ro_after_init = X86_CR4_PAE;
14455f26239SYinghai Lu #endif
14555f26239SYinghai Lu 
1465031296cSH. Peter Anvin /* Boot loader ID and version as integers, for the benefit of proc_dointvec */
1475031296cSH. Peter Anvin int bootloader_type, bootloader_version;
14855f26239SYinghai Lu 
14955f26239SYinghai Lu /*
15055f26239SYinghai Lu  * Setup options
15155f26239SYinghai Lu  */
15255f26239SYinghai Lu struct screen_info screen_info;
15355f26239SYinghai Lu EXPORT_SYMBOL(screen_info);
15455f26239SYinghai Lu struct edid_info edid_info;
15555f26239SYinghai Lu EXPORT_SYMBOL_GPL(edid_info);
15655f26239SYinghai Lu 
15755f26239SYinghai Lu extern int root_mountflags;
15855f26239SYinghai Lu 
15955f26239SYinghai Lu unsigned long saved_video_mode;
16055f26239SYinghai Lu 
16155f26239SYinghai Lu #define RAMDISK_IMAGE_START_MASK	0x07FF
16255f26239SYinghai Lu #define RAMDISK_PROMPT_FLAG		0x8000
16355f26239SYinghai Lu #define RAMDISK_LOAD_FLAG		0x4000
16455f26239SYinghai Lu 
16555f26239SYinghai Lu static char __initdata command_line[COMMAND_LINE_SIZE];
166516cbf37STim Bird #ifdef CONFIG_CMDLINE_BOOL
167516cbf37STim Bird static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
168516cbf37STim Bird #endif
16955f26239SYinghai Lu 
17055f26239SYinghai Lu #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
17155f26239SYinghai Lu struct edd edd;
17255f26239SYinghai Lu #ifdef CONFIG_EDD_MODULE
17355f26239SYinghai Lu EXPORT_SYMBOL(edd);
17455f26239SYinghai Lu #endif
17555f26239SYinghai Lu /**
17655f26239SYinghai Lu  * copy_edd() - Copy the BIOS EDD information
17755f26239SYinghai Lu  *              from boot_params into a safe place.
17855f26239SYinghai Lu  *
17955f26239SYinghai Lu  */
1809eaa192dSHelight.Xu static inline void __init copy_edd(void)
18155f26239SYinghai Lu {
18255f26239SYinghai Lu      memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer,
18355f26239SYinghai Lu 	    sizeof(edd.mbr_signature));
18455f26239SYinghai Lu      memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info));
18555f26239SYinghai Lu      edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries;
18655f26239SYinghai Lu      edd.edd_info_nr = boot_params.eddbuf_entries;
18755f26239SYinghai Lu }
18855f26239SYinghai Lu #else
1899eaa192dSHelight.Xu static inline void __init copy_edd(void)
19055f26239SYinghai Lu {
19155f26239SYinghai Lu }
19255f26239SYinghai Lu #endif
19355f26239SYinghai Lu 
1945368a2beSH. Peter Anvin void * __init extend_brk(size_t size, size_t align)
1955368a2beSH. Peter Anvin {
1965368a2beSH. Peter Anvin 	size_t mask = align - 1;
1975368a2beSH. Peter Anvin 	void *ret;
1985368a2beSH. Peter Anvin 
1995368a2beSH. Peter Anvin 	BUG_ON(_brk_start == 0);
2005368a2beSH. Peter Anvin 	BUG_ON(align & mask);
2015368a2beSH. Peter Anvin 
2025368a2beSH. Peter Anvin 	_brk_end = (_brk_end + mask) & ~mask;
2035368a2beSH. Peter Anvin 	BUG_ON((char *)(_brk_end + size) > __brk_limit);
2045368a2beSH. Peter Anvin 
2055368a2beSH. Peter Anvin 	ret = (void *)_brk_end;
2065368a2beSH. Peter Anvin 	_brk_end += size;
2075368a2beSH. Peter Anvin 
2085368a2beSH. Peter Anvin 	memset(ret, 0, size);
2095368a2beSH. Peter Anvin 
2105368a2beSH. Peter Anvin 	return ret;
2115368a2beSH. Peter Anvin }
2125368a2beSH. Peter Anvin 
213148b2098SYinghai Lu #ifdef CONFIG_X86_32
214e5f15b45SYinghai Lu static void __init cleanup_highmap(void)
215f005fe12SYinghai Lu {
216f005fe12SYinghai Lu }
217854c879fSPekka J Enberg #endif
218854c879fSPekka J Enberg 
2195368a2beSH. Peter Anvin static void __init reserve_brk(void)
2205368a2beSH. Peter Anvin {
2215368a2beSH. Peter Anvin 	if (_brk_end > _brk_start)
222fc8d7826SAlexander Duyck 		memblock_reserve(__pa_symbol(_brk_start),
223fc8d7826SAlexander Duyck 				 _brk_end - _brk_start);
2245368a2beSH. Peter Anvin 
2255368a2beSH. Peter Anvin 	/* Mark brk area as locked down and no longer taking any
2265368a2beSH. Peter Anvin 	   new allocations */
2275368a2beSH. Peter Anvin 	_brk_start = 0;
2285368a2beSH. Peter Anvin }
2295368a2beSH. Peter Anvin 
2305aa3d718SBorislav Petkov u64 relocated_ramdisk;
2315aa3d718SBorislav Petkov 
23255f26239SYinghai Lu #ifdef CONFIG_BLK_DEV_INITRD
23355f26239SYinghai Lu 
234a8a51a88SYinghai Lu static u64 __init get_ramdisk_image(void)
235a8a51a88SYinghai Lu {
236a8a51a88SYinghai Lu 	u64 ramdisk_image = boot_params.hdr.ramdisk_image;
237a8a51a88SYinghai Lu 
238ee92d815SYinghai Lu 	ramdisk_image |= (u64)boot_params.ext_ramdisk_image << 32;
239ee92d815SYinghai Lu 
240694cfd87SRonald G. Minnich 	if (ramdisk_image == 0)
241694cfd87SRonald G. Minnich 		ramdisk_image = phys_initrd_start;
242694cfd87SRonald G. Minnich 
243a8a51a88SYinghai Lu 	return ramdisk_image;
244a8a51a88SYinghai Lu }
245a8a51a88SYinghai Lu static u64 __init get_ramdisk_size(void)
246a8a51a88SYinghai Lu {
247a8a51a88SYinghai Lu 	u64 ramdisk_size = boot_params.hdr.ramdisk_size;
248a8a51a88SYinghai Lu 
249ee92d815SYinghai Lu 	ramdisk_size |= (u64)boot_params.ext_ramdisk_size << 32;
250ee92d815SYinghai Lu 
251694cfd87SRonald G. Minnich 	if (ramdisk_size == 0)
252694cfd87SRonald G. Minnich 		ramdisk_size = phys_initrd_size;
253694cfd87SRonald G. Minnich 
254a8a51a88SYinghai Lu 	return ramdisk_size;
255a8a51a88SYinghai Lu }
256a8a51a88SYinghai Lu 
25755f26239SYinghai Lu static void __init relocate_initrd(void)
25855f26239SYinghai Lu {
259c967da6aSYinghai Lu 	/* Assume only end is not page aligned */
260a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
261a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
262c967da6aSYinghai Lu 	u64 area_size     = PAGE_ALIGN(ramdisk_size);
26355f26239SYinghai Lu 
264e8c57d40SYinghai Lu 	/* We need to move the initrd down into directly mapped mem */
2653c45ee6dSMike Rapoport 	relocated_ramdisk = memblock_phys_alloc_range(area_size, PAGE_SIZE, 0,
2663c45ee6dSMike Rapoport 						      PFN_PHYS(max_pfn_mapped));
2675aa3d718SBorislav Petkov 	if (!relocated_ramdisk)
26855f26239SYinghai Lu 		panic("Cannot find place for new RAMDISK of size %lld\n",
26955f26239SYinghai Lu 		      ramdisk_size);
27055f26239SYinghai Lu 
2715aa3d718SBorislav Petkov 	initrd_start = relocated_ramdisk + PAGE_OFFSET;
27255f26239SYinghai Lu 	initrd_end   = initrd_start + ramdisk_size;
273365811d6SBjorn Helgaas 	printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
2745aa3d718SBorislav Petkov 	       relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
27555f26239SYinghai Lu 
2765dd2c4bdSMark Salter 	copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size);
27755f26239SYinghai Lu 
278365811d6SBjorn Helgaas 	printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to"
279365811d6SBjorn Helgaas 		" [mem %#010llx-%#010llx]\n",
28055f26239SYinghai Lu 		ramdisk_image, ramdisk_image + ramdisk_size - 1,
2815aa3d718SBorislav Petkov 		relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
28255f26239SYinghai Lu }
28355f26239SYinghai Lu 
2841b8c78beSYinghai Lu static void __init early_reserve_initrd(void)
2851b8c78beSYinghai Lu {
2861b8c78beSYinghai Lu 	/* Assume only end is not page aligned */
287a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
288a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
2891b8c78beSYinghai Lu 	u64 ramdisk_end   = PAGE_ALIGN(ramdisk_image + ramdisk_size);
2901b8c78beSYinghai Lu 
2911b8c78beSYinghai Lu 	if (!boot_params.hdr.type_of_loader ||
2921b8c78beSYinghai Lu 	    !ramdisk_image || !ramdisk_size)
2931b8c78beSYinghai Lu 		return;		/* No initrd provided by bootloader */
2941b8c78beSYinghai Lu 
2951b8c78beSYinghai Lu 	memblock_reserve(ramdisk_image, ramdisk_end - ramdisk_image);
2961b8c78beSYinghai Lu }
2973c45ee6dSMike Rapoport 
29855f26239SYinghai Lu static void __init reserve_initrd(void)
29955f26239SYinghai Lu {
300c967da6aSYinghai Lu 	/* Assume only end is not page aligned */
301a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
302a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
303c967da6aSYinghai Lu 	u64 ramdisk_end   = PAGE_ALIGN(ramdisk_image + ramdisk_size);
30455f26239SYinghai Lu 
30555f26239SYinghai Lu 	if (!boot_params.hdr.type_of_loader ||
30655f26239SYinghai Lu 	    !ramdisk_image || !ramdisk_size)
30755f26239SYinghai Lu 		return;		/* No initrd provided by bootloader */
30855f26239SYinghai Lu 
30955f26239SYinghai Lu 	initrd_start = 0;
31055f26239SYinghai Lu 
311365811d6SBjorn Helgaas 	printk(KERN_INFO "RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image,
312365811d6SBjorn Helgaas 			ramdisk_end - 1);
31355f26239SYinghai Lu 
31474f27655SYinghai Lu 	if (pfn_range_is_mapped(PFN_DOWN(ramdisk_image),
315e8c57d40SYinghai Lu 				PFN_DOWN(ramdisk_end))) {
316e8c57d40SYinghai Lu 		/* All are mapped, easy case */
31755f26239SYinghai Lu 		initrd_start = ramdisk_image + PAGE_OFFSET;
31855f26239SYinghai Lu 		initrd_end = initrd_start + ramdisk_size;
31955f26239SYinghai Lu 		return;
32055f26239SYinghai Lu 	}
32155f26239SYinghai Lu 
32255f26239SYinghai Lu 	relocate_initrd();
3238c5dd8f4SYinghai Lu 
32424aa0788STejun Heo 	memblock_free(ramdisk_image, ramdisk_end - ramdisk_image);
32555f26239SYinghai Lu }
326af06f8b7SLv Zheng 
32755f26239SYinghai Lu #else
3281b8c78beSYinghai Lu static void __init early_reserve_initrd(void)
3291b8c78beSYinghai Lu {
3301b8c78beSYinghai Lu }
33155f26239SYinghai Lu static void __init reserve_initrd(void)
33255f26239SYinghai Lu {
33355f26239SYinghai Lu }
33455f26239SYinghai Lu #endif /* CONFIG_BLK_DEV_INITRD */
33555f26239SYinghai Lu 
33629f784e3SYinghai Lu static void __init parse_setup_data(void)
337257b0fdeSYinghai Lu {
338257b0fdeSYinghai Lu 	struct setup_data *data;
33930e46b57SLinn Crosetto 	u64 pa_data, pa_next;
340257b0fdeSYinghai Lu 
341257b0fdeSYinghai Lu 	pa_data = boot_params.hdr.setup_data;
342257b0fdeSYinghai Lu 	while (pa_data) {
3437389882cSWANG Chao 		u32 data_len, data_type;
344f1c2b357SSebastian Andrzej Siewior 
3457389882cSWANG Chao 		data = early_memremap(pa_data, sizeof(*data));
346f1c2b357SSebastian Andrzej Siewior 		data_len = data->len + sizeof(struct setup_data);
34730e46b57SLinn Crosetto 		data_type = data->type;
34830e46b57SLinn Crosetto 		pa_next = data->next;
3498d4a40bcSJuergen Gross 		early_memunmap(data, sizeof(*data));
350f1c2b357SSebastian Andrzej Siewior 
35130e46b57SLinn Crosetto 		switch (data_type) {
352257b0fdeSYinghai Lu 		case SETUP_E820_EXT:
353914053c0SIngo Molnar 			e820__memory_setup_extended(pa_data, data_len);
354257b0fdeSYinghai Lu 			break;
355da6b737bSSebastian Andrzej Siewior 		case SETUP_DTB:
356da6b737bSSebastian Andrzej Siewior 			add_dtb(pa_data);
357257b0fdeSYinghai Lu 			break;
3581fec0533SDave Young 		case SETUP_EFI:
3591fec0533SDave Young 			parse_efi_setup(pa_data, data_len);
3601fec0533SDave Young 			break;
361257b0fdeSYinghai Lu 		default:
362257b0fdeSYinghai Lu 			break;
363257b0fdeSYinghai Lu 		}
36430e46b57SLinn Crosetto 		pa_data = pa_next;
365257b0fdeSYinghai Lu 	}
366257b0fdeSYinghai Lu }
367257b0fdeSYinghai Lu 
368a9ce6bc1SYinghai Lu static void __init memblock_x86_reserve_range_setup_data(void)
369a0a0becdSYinghai Lu {
370a0a0becdSYinghai Lu 	struct setup_data *data;
371a0a0becdSYinghai Lu 	u64 pa_data;
372a0a0becdSYinghai Lu 
373a0a0becdSYinghai Lu 	pa_data = boot_params.hdr.setup_data;
374a0a0becdSYinghai Lu 	while (pa_data) {
37588b4c146SJeremy Fitzhardinge 		data = early_memremap(pa_data, sizeof(*data));
37624aa0788STejun Heo 		memblock_reserve(pa_data, sizeof(*data) + data->len);
377b3c72fc9SDaniel Kiper 
378b3c72fc9SDaniel Kiper 		if (data->type == SETUP_INDIRECT &&
379b3c72fc9SDaniel Kiper 		    ((struct setup_indirect *)data->data)->type != SETUP_INDIRECT)
380b3c72fc9SDaniel Kiper 			memblock_reserve(((struct setup_indirect *)data->data)->addr,
381b3c72fc9SDaniel Kiper 					 ((struct setup_indirect *)data->data)->len);
382b3c72fc9SDaniel Kiper 
383a0a0becdSYinghai Lu 		pa_data = data->next;
3848d4a40bcSJuergen Gross 		early_memunmap(data, sizeof(*data));
385a0a0becdSYinghai Lu 	}
386a0a0becdSYinghai Lu }
387a0a0becdSYinghai Lu 
38855f26239SYinghai Lu /*
389ccb4defaSYinghai Lu  * --------- Crashkernel reservation ------------------------------
390ccb4defaSYinghai Lu  */
391ccb4defaSYinghai Lu 
3922965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
39332105f7fSBernhard Walle 
394606134f7SBorislav Petkov /* 16M alignment for crash kernel regions */
3959ca5c8e6SDave Young #define CRASH_ALIGN		SZ_16M
396606134f7SBorislav Petkov 
3977f8595bfSH. Peter Anvin /*
3988ff80fbeSBaoquan He  * Keep the crash kernel below this limit.
3998ff80fbeSBaoquan He  *
400360db4acSIngo Molnar  * Earlier 32-bits kernels would limit the kernel to the low 512 MB range
4018ff80fbeSBaoquan He  * due to mapping restrictions.
4028ff80fbeSBaoquan He  *
403360db4acSIngo Molnar  * 64-bit kdump kernels need to be restricted to be under 64 TB, which is
40411a98f37SCao jin  * the upper limit of system RAM in 4-level paging mode. Since the kdump
405360db4acSIngo Molnar  * jump could be from 5-level paging to 4-level paging, the jump will fail if
406360db4acSIngo Molnar  * the kernel is put above 64 TB, and during the 1st kernel bootup there's
407360db4acSIngo Molnar  * no good way to detect the paging mode of the target kernel which will be
408360db4acSIngo Molnar  * loaded for dumping.
4097f8595bfSH. Peter Anvin  */
4107f8595bfSH. Peter Anvin #ifdef CONFIG_X86_32
4119ca5c8e6SDave Young # define CRASH_ADDR_LOW_MAX	SZ_512M
4129ca5c8e6SDave Young # define CRASH_ADDR_HIGH_MAX	SZ_512M
4137f8595bfSH. Peter Anvin #else
4149ca5c8e6SDave Young # define CRASH_ADDR_LOW_MAX	SZ_4G
4158ff80fbeSBaoquan He # define CRASH_ADDR_HIGH_MAX	SZ_64T
4167f8595bfSH. Peter Anvin #endif
4177f8595bfSH. Peter Anvin 
418eb6db83dSBaoquan He static int __init reserve_crashkernel_low(void)
4190212f915SYinghai Lu {
4200212f915SYinghai Lu #ifdef CONFIG_X86_64
421f56d5578SBorislav Petkov 	unsigned long long base, low_base = 0, low_size = 0;
4226120cdc0SMike Rapoport 	unsigned long low_mem_limit;
4230212f915SYinghai Lu 	int ret;
4240212f915SYinghai Lu 
4256120cdc0SMike Rapoport 	low_mem_limit = min(memblock_phys_mem_size(), CRASH_ADDR_LOW_MAX);
42697eac21bSBorislav Petkov 
427adbc742bSYinghai Lu 	/* crashkernel=Y,low */
4286120cdc0SMike Rapoport 	ret = parse_crashkernel_low(boot_command_line, low_mem_limit, &low_size, &base);
429f56d5578SBorislav Petkov 	if (ret) {
430c729de8fSYinghai Lu 		/*
431392e879aSAndy Shevchenko 		 * two parts from kernel/dma/swiotlb.c:
43294fb9334SJoerg Roedel 		 * -swiotlb size: user-specified with swiotlb= or default.
43394fb9334SJoerg Roedel 		 *
43494fb9334SJoerg Roedel 		 * -swiotlb overflow buffer: now hardcoded to 32k. We round it
43594fb9334SJoerg Roedel 		 * to 8M for other buffers that may need to stay low too. Also
43694fb9334SJoerg Roedel 		 * make sure we allocate enough extra low memory so that we
43794fb9334SJoerg Roedel 		 * don't run out of DMA buffers for 32-bit devices.
438c729de8fSYinghai Lu 		 */
43994fb9334SJoerg Roedel 		low_size = max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
440c729de8fSYinghai Lu 	} else {
441adbc742bSYinghai Lu 		/* passed with crashkernel=0,low ? */
442c729de8fSYinghai Lu 		if (!low_size)
443eb6db83dSBaoquan He 			return 0;
444c729de8fSYinghai Lu 	}
4450212f915SYinghai Lu 
4466120cdc0SMike Rapoport 	low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
4470212f915SYinghai Lu 	if (!low_base) {
448eb6db83dSBaoquan He 		pr_err("Cannot reserve %ldMB crashkernel low memory, please try smaller size.\n",
449eb6db83dSBaoquan He 		       (unsigned long)(low_size >> 20));
450eb6db83dSBaoquan He 		return -ENOMEM;
4510212f915SYinghai Lu 	}
4520212f915SYinghai Lu 
4536120cdc0SMike Rapoport 	pr_info("Reserving %ldMB of low memory at %ldMB for crashkernel (low RAM limit: %ldMB)\n",
4540212f915SYinghai Lu 		(unsigned long)(low_size >> 20),
4550212f915SYinghai Lu 		(unsigned long)(low_base >> 20),
4566120cdc0SMike Rapoport 		(unsigned long)(low_mem_limit >> 20));
45797eac21bSBorislav Petkov 
4580212f915SYinghai Lu 	crashk_low_res.start = low_base;
4590212f915SYinghai Lu 	crashk_low_res.end   = low_base + low_size - 1;
4600212f915SYinghai Lu 	insert_resource(&iomem_resource, &crashk_low_res);
4610212f915SYinghai Lu #endif
462eb6db83dSBaoquan He 	return 0;
4630212f915SYinghai Lu }
4640212f915SYinghai Lu 
46529f784e3SYinghai Lu static void __init reserve_crashkernel(void)
466ccb4defaSYinghai Lu {
467f56d5578SBorislav Petkov 	unsigned long long crash_size, crash_base, total_mem;
46855a20ee7SYinghai Lu 	bool high = false;
469ccb4defaSYinghai Lu 	int ret;
470ccb4defaSYinghai Lu 
47109c71bfdSDave Young 	total_mem = memblock_phys_mem_size();
472ccb4defaSYinghai Lu 
47355a20ee7SYinghai Lu 	/* crashkernel=XM */
47497eac21bSBorislav Petkov 	ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
47555a20ee7SYinghai Lu 	if (ret != 0 || crash_size <= 0) {
476adbc742bSYinghai Lu 		/* crashkernel=X,high */
47755a20ee7SYinghai Lu 		ret = parse_crashkernel_high(boot_command_line, total_mem,
47855a20ee7SYinghai Lu 					     &crash_size, &crash_base);
47932105f7fSBernhard Walle 		if (ret != 0 || crash_size <= 0)
48032105f7fSBernhard Walle 			return;
48155a20ee7SYinghai Lu 		high = true;
48255a20ee7SYinghai Lu 	}
48332105f7fSBernhard Walle 
4843db3eb28SPetr Tesarik 	if (xen_pv_domain()) {
4853db3eb28SPetr Tesarik 		pr_info("Ignoring crashkernel for a Xen PV domain\n");
4863db3eb28SPetr Tesarik 		return;
4873db3eb28SPetr Tesarik 	}
4883db3eb28SPetr Tesarik 
48932105f7fSBernhard Walle 	/* 0 means: find the address automatically */
490b9ac3849SDave Young 	if (!crash_base) {
4919f4c1396SYinghai Lu 		/*
492a8d4c824SXunlei Pang 		 * Set CRASH_ADDR_LOW_MAX upper bound for crash memory,
493b9ac3849SDave Young 		 * crashkernel=x,high reserves memory over 4G, also allocates
494b9ac3849SDave Young 		 * 256M extra low memory for DMA buffers and swiotlb.
495b9ac3849SDave Young 		 * But the extra memory is not required for all machines.
496b9ac3849SDave Young 		 * So try low memory first and fall back to high memory
497b9ac3849SDave Young 		 * unless "crashkernel=size[KMG],high" is specified.
4989f4c1396SYinghai Lu 		 */
499b9ac3849SDave Young 		if (!high)
5006120cdc0SMike Rapoport 			crash_base = memblock_phys_alloc_range(crash_size,
5016120cdc0SMike Rapoport 						CRASH_ALIGN, CRASH_ALIGN,
5026120cdc0SMike Rapoport 						CRASH_ADDR_LOW_MAX);
503b9ac3849SDave Young 		if (!crash_base)
5046120cdc0SMike Rapoport 			crash_base = memblock_phys_alloc_range(crash_size,
5056120cdc0SMike Rapoport 						CRASH_ALIGN, CRASH_ALIGN,
5066120cdc0SMike Rapoport 						CRASH_ADDR_HIGH_MAX);
5071f5026a7STejun Heo 		if (!crash_base) {
50844280733SYinghai Lu 			pr_info("crashkernel reservation failed - No suitable area found.\n");
509ccb4defaSYinghai Lu 			return;
510ccb4defaSYinghai Lu 		}
51132105f7fSBernhard Walle 	} else {
51244280733SYinghai Lu 		unsigned long long start;
51344280733SYinghai Lu 
5146120cdc0SMike Rapoport 		start = memblock_phys_alloc_range(crash_size, SZ_1M, crash_base,
5156120cdc0SMike Rapoport 						  crash_base + crash_size);
51644280733SYinghai Lu 		if (start != crash_base) {
51744280733SYinghai Lu 			pr_info("crashkernel reservation failed - memory is in use.\n");
518ccb4defaSYinghai Lu 			return;
519ccb4defaSYinghai Lu 		}
52032105f7fSBernhard Walle 	}
521ccb4defaSYinghai Lu 
522eb6db83dSBaoquan He 	if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) {
523eb6db83dSBaoquan He 		memblock_free(crash_base, crash_size);
524eb6db83dSBaoquan He 		return;
525eb6db83dSBaoquan He 	}
526eb6db83dSBaoquan He 
527f56d5578SBorislav Petkov 	pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
528ccb4defaSYinghai Lu 		(unsigned long)(crash_size >> 20),
529ccb4defaSYinghai Lu 		(unsigned long)(crash_base >> 20),
530ccb4defaSYinghai Lu 		(unsigned long)(total_mem >> 20));
531ccb4defaSYinghai Lu 
532ccb4defaSYinghai Lu 	crashk_res.start = crash_base;
533ccb4defaSYinghai Lu 	crashk_res.end   = crash_base + crash_size - 1;
534ccb4defaSYinghai Lu 	insert_resource(&iomem_resource, &crashk_res);
535ccb4defaSYinghai Lu }
536ccb4defaSYinghai Lu #else
53729f784e3SYinghai Lu static void __init reserve_crashkernel(void)
538ccb4defaSYinghai Lu {
539ccb4defaSYinghai Lu }
540ccb4defaSYinghai Lu #endif
541ccb4defaSYinghai Lu 
542bdba0e70SYinghai Lu static struct resource standard_io_resources[] = {
543bdba0e70SYinghai Lu 	{ .name = "dma1", .start = 0x00, .end = 0x1f,
544bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
545bdba0e70SYinghai Lu 	{ .name = "pic1", .start = 0x20, .end = 0x21,
546bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
547bdba0e70SYinghai Lu 	{ .name = "timer0", .start = 0x40, .end = 0x43,
548bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
549bdba0e70SYinghai Lu 	{ .name = "timer1", .start = 0x50, .end = 0x53,
550bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
551bdba0e70SYinghai Lu 	{ .name = "keyboard", .start = 0x60, .end = 0x60,
552bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
553bdba0e70SYinghai Lu 	{ .name = "keyboard", .start = 0x64, .end = 0x64,
554bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
555bdba0e70SYinghai Lu 	{ .name = "dma page reg", .start = 0x80, .end = 0x8f,
556bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
557bdba0e70SYinghai Lu 	{ .name = "pic2", .start = 0xa0, .end = 0xa1,
558bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
559bdba0e70SYinghai Lu 	{ .name = "dma2", .start = 0xc0, .end = 0xdf,
560bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
561bdba0e70SYinghai Lu 	{ .name = "fpu", .start = 0xf0, .end = 0xff,
562bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO }
563bdba0e70SYinghai Lu };
564bdba0e70SYinghai Lu 
5658fee697dSThomas Gleixner void __init reserve_standard_io_resources(void)
566bdba0e70SYinghai Lu {
567bdba0e70SYinghai Lu 	int i;
568bdba0e70SYinghai Lu 
569bdba0e70SYinghai Lu 	/* request I/O space for devices used on all i[345]86 PCs */
570bdba0e70SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
571bdba0e70SYinghai Lu 		request_resource(&ioport_resource, &standard_io_resources[i]);
572bdba0e70SYinghai Lu 
573bdba0e70SYinghai Lu }
574bdba0e70SYinghai Lu 
575042be38eSYinghai Lu static __init void reserve_ibft_region(void)
576042be38eSYinghai Lu {
577042be38eSYinghai Lu 	unsigned long addr, size = 0;
578042be38eSYinghai Lu 
579042be38eSYinghai Lu 	addr = find_ibft_region(&size);
580042be38eSYinghai Lu 
581042be38eSYinghai Lu 	if (size)
58224aa0788STejun Heo 		memblock_reserve(addr, size);
583042be38eSYinghai Lu }
584042be38eSYinghai Lu 
585a9acc536SJesse Barnes static bool __init snb_gfx_workaround_needed(void)
586a9acc536SJesse Barnes {
587e43b3cecSH. Peter Anvin #ifdef CONFIG_PCI
588a9acc536SJesse Barnes 	int i;
589a9acc536SJesse Barnes 	u16 vendor, devid;
590ab3cd867SH. Peter Anvin 	static const __initconst u16 snb_ids[] = {
591a9acc536SJesse Barnes 		0x0102,
592a9acc536SJesse Barnes 		0x0112,
593a9acc536SJesse Barnes 		0x0122,
594a9acc536SJesse Barnes 		0x0106,
595a9acc536SJesse Barnes 		0x0116,
596a9acc536SJesse Barnes 		0x0126,
597a9acc536SJesse Barnes 		0x010a,
598a9acc536SJesse Barnes 	};
599a9acc536SJesse Barnes 
600a9acc536SJesse Barnes 	/* Assume no if something weird is going on with PCI */
601a9acc536SJesse Barnes 	if (!early_pci_allowed())
602a9acc536SJesse Barnes 		return false;
603a9acc536SJesse Barnes 
604a9acc536SJesse Barnes 	vendor = read_pci_config_16(0, 2, 0, PCI_VENDOR_ID);
605a9acc536SJesse Barnes 	if (vendor != 0x8086)
606a9acc536SJesse Barnes 		return false;
607a9acc536SJesse Barnes 
608a9acc536SJesse Barnes 	devid = read_pci_config_16(0, 2, 0, PCI_DEVICE_ID);
609a9acc536SJesse Barnes 	for (i = 0; i < ARRAY_SIZE(snb_ids); i++)
610a9acc536SJesse Barnes 		if (devid == snb_ids[i])
611a9acc536SJesse Barnes 			return true;
612e43b3cecSH. Peter Anvin #endif
613a9acc536SJesse Barnes 
614a9acc536SJesse Barnes 	return false;
615a9acc536SJesse Barnes }
616a9acc536SJesse Barnes 
617a9acc536SJesse Barnes /*
618a9acc536SJesse Barnes  * Sandy Bridge graphics has trouble with certain ranges, exclude
619a9acc536SJesse Barnes  * them from allocation.
620a9acc536SJesse Barnes  */
621a9acc536SJesse Barnes static void __init trim_snb_memory(void)
622a9acc536SJesse Barnes {
623ab3cd867SH. Peter Anvin 	static const __initconst unsigned long bad_pages[] = {
624a9acc536SJesse Barnes 		0x20050000,
625a9acc536SJesse Barnes 		0x20110000,
626a9acc536SJesse Barnes 		0x20130000,
627a9acc536SJesse Barnes 		0x20138000,
628a9acc536SJesse Barnes 		0x40004000,
629a9acc536SJesse Barnes 	};
630a9acc536SJesse Barnes 	int i;
631a9acc536SJesse Barnes 
632a9acc536SJesse Barnes 	if (!snb_gfx_workaround_needed())
633a9acc536SJesse Barnes 		return;
634a9acc536SJesse Barnes 
635a9acc536SJesse Barnes 	printk(KERN_DEBUG "reserving inaccessible SNB gfx pages\n");
636a9acc536SJesse Barnes 
637a9acc536SJesse Barnes 	/*
638c361e5d4SMike Rapoport 	 * SandyBridge integrated graphics devices have a bug that prevents
639c361e5d4SMike Rapoport 	 * them from accessing certain memory ranges, namely anything below
640c361e5d4SMike Rapoport 	 * 1M and in the pages listed in bad_pages[] above.
641c361e5d4SMike Rapoport 	 *
642f1d4d47cSMike Rapoport 	 * To avoid these pages being ever accessed by SNB gfx devices reserve
643f1d4d47cSMike Rapoport 	 * bad_pages that have not already been reserved at boot time.
644f1d4d47cSMike Rapoport 	 * All memory below the 1 MB mark is anyway reserved later during
645f1d4d47cSMike Rapoport 	 * setup_arch(), so there is no need to reserve it here.
646a9acc536SJesse Barnes 	 */
647a9acc536SJesse Barnes 
648a9acc536SJesse Barnes 	for (i = 0; i < ARRAY_SIZE(bad_pages); i++) {
649a9acc536SJesse Barnes 		if (memblock_reserve(bad_pages[i], PAGE_SIZE))
650a9acc536SJesse Barnes 			printk(KERN_WARNING "failed to reserve 0x%08lx\n",
651a9acc536SJesse Barnes 			       bad_pages[i]);
652a9acc536SJesse Barnes 	}
653a9acc536SJesse Barnes }
654a9acc536SJesse Barnes 
6551b5576e6SYinghai Lu static void __init trim_bios_range(void)
6561b5576e6SYinghai Lu {
6571b5576e6SYinghai Lu 	/*
6585c279c4cSMike Rapoport 	 * A special case is the first 4Kb of memory;
6595c279c4cSMike Rapoport 	 * This is a BIOS owned area, not kernel ram, but generally
6605c279c4cSMike Rapoport 	 * not listed as such in the E820 table.
6615c279c4cSMike Rapoport 	 *
6625c279c4cSMike Rapoport 	 * This typically reserves additional memory (64KiB by default)
6635c279c4cSMike Rapoport 	 * since some BIOSes are known to corrupt low memory.  See the
6645c279c4cSMike Rapoport 	 * Kconfig help text for X86_RESERVE_LOW.
6655c279c4cSMike Rapoport 	 */
6665c279c4cSMike Rapoport 	e820__range_update(0, PAGE_SIZE, E820_TYPE_RAM, E820_TYPE_RESERVED);
6675c279c4cSMike Rapoport 
6685c279c4cSMike Rapoport 	/*
66911a98f37SCao jin 	 * special case: Some BIOSes report the PC BIOS
67011a98f37SCao jin 	 * area (640Kb -> 1Mb) as RAM even though it is not.
6711b5576e6SYinghai Lu 	 * take them out.
6721b5576e6SYinghai Lu 	 */
67309821ff1SIngo Molnar 	e820__range_remove(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_TYPE_RAM, 1);
674a9acc536SJesse Barnes 
675f9748fa0SIngo Molnar 	e820__update_table(e820_table);
6761b5576e6SYinghai Lu }
6771b5576e6SYinghai Lu 
678b422a309SYinghai Lu /* called before trim_bios_range() to spare extra sanitize */
679b422a309SYinghai Lu static void __init e820_add_kernel_range(void)
680b422a309SYinghai Lu {
681b422a309SYinghai Lu 	u64 start = __pa_symbol(_text);
682b422a309SYinghai Lu 	u64 size = __pa_symbol(_end) - start;
683b422a309SYinghai Lu 
684b422a309SYinghai Lu 	/*
68509821ff1SIngo Molnar 	 * Complain if .text .data and .bss are not marked as E820_TYPE_RAM and
686b422a309SYinghai Lu 	 * attempt to fix it by adding the range. We may have a confused BIOS,
687b422a309SYinghai Lu 	 * or the user may have used memmap=exactmap or memmap=xxM$yyM to
688b422a309SYinghai Lu 	 * exclude kernel range. If we really are running on top non-RAM,
689b422a309SYinghai Lu 	 * we will crash later anyways.
690b422a309SYinghai Lu 	 */
69109821ff1SIngo Molnar 	if (e820__mapped_all(start, start + size, E820_TYPE_RAM))
692b422a309SYinghai Lu 		return;
693b422a309SYinghai Lu 
69409821ff1SIngo Molnar 	pr_warn(".text .data .bss are not marked as E820_TYPE_RAM!\n");
69509821ff1SIngo Molnar 	e820__range_remove(start, size, E820_TYPE_RAM, 0);
69609821ff1SIngo Molnar 	e820__range_add(start, size, E820_TYPE_RAM);
697b422a309SYinghai Lu }
698b422a309SYinghai Lu 
699a799c2bdSMike Rapoport static void __init early_reserve_memory(void)
70095c96084SH. Peter Anvin {
701a799c2bdSMike Rapoport 	/*
702a799c2bdSMike Rapoport 	 * Reserve the memory occupied by the kernel between _text and
703a799c2bdSMike Rapoport 	 * __end_of_kernel_reserve symbols. Any kernel sections after the
704a799c2bdSMike Rapoport 	 * __end_of_kernel_reserve symbol must be explicitly reserved with a
705a799c2bdSMike Rapoport 	 * separate memblock_reserve() or they will be discarded.
706a799c2bdSMike Rapoport 	 */
707a799c2bdSMike Rapoport 	memblock_reserve(__pa_symbol(_text),
708a799c2bdSMike Rapoport 			 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
709a799c2bdSMike Rapoport 
710a799c2bdSMike Rapoport 	/*
7114c674481SMike Rapoport 	 * The first 4Kb of memory is a BIOS owned area, but generally it is
7124c674481SMike Rapoport 	 * not listed as such in the E820 table.
7134c674481SMike Rapoport 	 *
714f1d4d47cSMike Rapoport 	 * Reserve the first 64K of memory since some BIOSes are known to
715f1d4d47cSMike Rapoport 	 * corrupt low memory. After the real mode trampoline is allocated the
716f1d4d47cSMike Rapoport 	 * rest of the memory below 640k is reserved.
7174c674481SMike Rapoport 	 *
7184c674481SMike Rapoport 	 * In addition, make sure page 0 is always reserved because on
7194c674481SMike Rapoport 	 * systems with L1TF its contents can be leaked to user processes.
720a799c2bdSMike Rapoport 	 */
721f1d4d47cSMike Rapoport 	memblock_reserve(0, SZ_64K);
722a799c2bdSMike Rapoport 
723a799c2bdSMike Rapoport 	early_reserve_initrd();
724a799c2bdSMike Rapoport 
725a799c2bdSMike Rapoport 	if (efi_enabled(EFI_BOOT))
726a799c2bdSMike Rapoport 		efi_memblock_x86_reserve_range();
727a799c2bdSMike Rapoport 
728a799c2bdSMike Rapoport 	memblock_x86_reserve_range_setup_data();
729a799c2bdSMike Rapoport 
730a799c2bdSMike Rapoport 	reserve_ibft_region();
731a799c2bdSMike Rapoport 	reserve_bios_regions();
732f1d4d47cSMike Rapoport 	trim_snb_memory();
73395c96084SH. Peter Anvin }
73495c96084SH. Peter Anvin 
7355394f80fSJeremy Fitzhardinge /*
736f32360efSKees Cook  * Dump out kernel offset information on panic.
737f32360efSKees Cook  */
738f32360efSKees Cook static int
739f32360efSKees Cook dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
740f32360efSKees Cook {
74178cac48cSBorislav Petkov 	if (kaslr_enabled()) {
74278cac48cSBorislav Petkov 		pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
7434545c898SJiri Kosina 			 kaslr_offset(),
74478cac48cSBorislav Petkov 			 __START_KERNEL,
74578cac48cSBorislav Petkov 			 __START_KERNEL_map,
74678cac48cSBorislav Petkov 			 MODULES_VADDR-1);
74778cac48cSBorislav Petkov 	} else {
74878cac48cSBorislav Petkov 		pr_emerg("Kernel Offset: disabled\n");
74978cac48cSBorislav Petkov 	}
750f32360efSKees Cook 
751f32360efSKees Cook 	return 0;
752f32360efSKees Cook }
753f32360efSKees Cook 
754f32360efSKees Cook /*
75555f26239SYinghai Lu  * Determine if we were loaded by an EFI loader.  If so, then we have also been
75655f26239SYinghai Lu  * passed the efi memmap, systab, etc., so we should use these data structures
75755f26239SYinghai Lu  * for initialization.  Note, the efi init code path is determined by the
75855f26239SYinghai Lu  * global efi_enabled. This allows the same kernel image to be used on existing
75955f26239SYinghai Lu  * systems (with a traditional BIOS) as well as on EFI systems.
76055f26239SYinghai Lu  */
76155f26239SYinghai Lu /*
76255f26239SYinghai Lu  * setup_arch - architecture-specific boot-time initializations
76355f26239SYinghai Lu  *
76455f26239SYinghai Lu  * Note: On x86_64, fixmaps are ready for use even before this is called.
76555f26239SYinghai Lu  */
76655f26239SYinghai Lu 
76755f26239SYinghai Lu void __init setup_arch(char **cmdline_p)
76855f26239SYinghai Lu {
76955f26239SYinghai Lu #ifdef CONFIG_X86_32
77055f26239SYinghai Lu 	memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
771b40827faSBorislav Petkov 
772b40827faSBorislav Petkov 	/*
773b40827faSBorislav Petkov 	 * copy kernel address range established so far and switch
774b40827faSBorislav Petkov 	 * to the proper swapper page table
775b40827faSBorislav Petkov 	 */
776b40827faSBorislav Petkov 	clone_pgd_range(swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
777b40827faSBorislav Petkov 			initial_page_table + KERNEL_PGD_BOUNDARY,
778b40827faSBorislav Petkov 			KERNEL_PGD_PTRS);
779b40827faSBorislav Petkov 
780b40827faSBorislav Petkov 	load_cr3(swapper_pg_dir);
7812075244fSBryan O'Donoghue 	/*
7822075244fSBryan O'Donoghue 	 * Note: Quark X1000 CPUs advertise PGE incorrectly and require
7832075244fSBryan O'Donoghue 	 * a cr3 based tlb flush, so the following __flush_tlb_all()
784360db4acSIngo Molnar 	 * will not flush anything because the CPU quirk which clears
7852075244fSBryan O'Donoghue 	 * X86_FEATURE_PGE has not been invoked yet. Though due to the
7862075244fSBryan O'Donoghue 	 * load_cr3() above the TLB has been flushed already. The
7872075244fSBryan O'Donoghue 	 * quirk is invoked before subsequent calls to __flush_tlb_all()
7882075244fSBryan O'Donoghue 	 * so proper operation is guaranteed.
7892075244fSBryan O'Donoghue 	 */
790b40827faSBorislav Petkov 	__flush_tlb_all();
79155f26239SYinghai Lu #else
79255f26239SYinghai Lu 	printk(KERN_INFO "Command line: %s\n", boot_command_line);
793162434e7SKirill A. Shutemov 	boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS;
79455f26239SYinghai Lu #endif
79555f26239SYinghai Lu 
7969863c90fSAlok Kataria 	/*
7979863c90fSAlok Kataria 	 * If we have OLPC OFW, we might end up relocating the fixmap due to
7989863c90fSAlok Kataria 	 * reserve_top(), so do this before touching the ioremap area.
7999863c90fSAlok Kataria 	 */
800fd699c76SAndres Salomon 	olpc_ofw_detect();
801fd699c76SAndres Salomon 
802433f8924SThomas Gleixner 	idt_setup_early_traps();
8039e882c92SJeremy Fitzhardinge 	early_cpu_init();
8048990cac6SPavel Tatashin 	jump_label_init();
805a945c834SPeter Zijlstra 	static_call_init();
8061a98fd14SJeremy Fitzhardinge 	early_ioremap_init();
8071a98fd14SJeremy Fitzhardinge 
808fd699c76SAndres Salomon 	setup_olpc_ofw_pgd();
809fd699c76SAndres Salomon 
81055f26239SYinghai Lu 	ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
81155f26239SYinghai Lu 	screen_info = boot_params.screen_info;
81255f26239SYinghai Lu 	edid_info = boot_params.edid_info;
81355f26239SYinghai Lu #ifdef CONFIG_X86_32
81455f26239SYinghai Lu 	apm_info.bios = boot_params.apm_bios_info;
81555f26239SYinghai Lu 	ist_info = boot_params.ist_info;
81655f26239SYinghai Lu #endif
81755f26239SYinghai Lu 	saved_video_mode = boot_params.hdr.vid_mode;
81855f26239SYinghai Lu 	bootloader_type = boot_params.hdr.type_of_loader;
8195031296cSH. Peter Anvin 	if ((bootloader_type >> 4) == 0xe) {
8205031296cSH. Peter Anvin 		bootloader_type &= 0xf;
8215031296cSH. Peter Anvin 		bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4;
8225031296cSH. Peter Anvin 	}
8235031296cSH. Peter Anvin 	bootloader_version  = bootloader_type & 0xf;
8245031296cSH. Peter Anvin 	bootloader_version |= boot_params.hdr.ext_loader_ver << 4;
82555f26239SYinghai Lu 
82655f26239SYinghai Lu #ifdef CONFIG_BLK_DEV_RAM
82755f26239SYinghai Lu 	rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
82855f26239SYinghai Lu #endif
82955f26239SYinghai Lu #ifdef CONFIG_EFI
83055f26239SYinghai Lu 	if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
8319402973dSDaniel Kiper 		     EFI32_LOADER_SIGNATURE, 4)) {
8323e909599SMatt Fleming 		set_bit(EFI_BOOT, &efi.flags);
8331adbfa35SOlof Johansson 	} else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
8349402973dSDaniel Kiper 		     EFI64_LOADER_SIGNATURE, 4)) {
8353e909599SMatt Fleming 		set_bit(EFI_BOOT, &efi.flags);
8363e909599SMatt Fleming 		set_bit(EFI_64BIT, &efi.flags);
83755f26239SYinghai Lu 	}
83855f26239SYinghai Lu #endif
83955f26239SYinghai Lu 
84042bbdb43SThomas Gleixner 	x86_init.oem.arch_setup();
84155f26239SYinghai Lu 
842419afdf5SBjorn Helgaas 	iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
843103e2063SIngo Molnar 	e820__memory_setup();
84428bb2237SYinghai Lu 	parse_setup_data();
84528bb2237SYinghai Lu 
84655f26239SYinghai Lu 	copy_edd();
84755f26239SYinghai Lu 
84855f26239SYinghai Lu 	if (!boot_params.hdr.root_flags)
84955f26239SYinghai Lu 		root_mountflags &= ~MS_RDONLY;
850*30120d72SKefeng Wang 	setup_initial_init_mm(_text, _etext, _edata, (void *)_brk_end);
85155f26239SYinghai Lu 
8524046d6e8SLinus Torvalds 	code_resource.start = __pa_symbol(_text);
8534046d6e8SLinus Torvalds 	code_resource.end = __pa_symbol(_etext)-1;
854a3299754SKees Cook 	rodata_resource.start = __pa_symbol(__start_rodata);
855a3299754SKees Cook 	rodata_resource.end = __pa_symbol(__end_rodata)-1;
856a3299754SKees Cook 	data_resource.start = __pa_symbol(_sdata);
8574046d6e8SLinus Torvalds 	data_resource.end = __pa_symbol(_edata)-1;
8584046d6e8SLinus Torvalds 	bss_resource.start = __pa_symbol(__bss_start);
8594046d6e8SLinus Torvalds 	bss_resource.end = __pa_symbol(__bss_stop)-1;
8604046d6e8SLinus Torvalds 
861516cbf37STim Bird #ifdef CONFIG_CMDLINE_BOOL
862516cbf37STim Bird #ifdef CONFIG_CMDLINE_OVERRIDE
863516cbf37STim Bird 	strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
864516cbf37STim Bird #else
865516cbf37STim Bird 	if (builtin_cmdline[0]) {
866516cbf37STim Bird 		/* append boot loader cmdline to builtin */
867516cbf37STim Bird 		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
868516cbf37STim Bird 		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
869516cbf37STim Bird 		strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
870516cbf37STim Bird 	}
871516cbf37STim Bird #endif
872516cbf37STim Bird #endif
873516cbf37STim Bird 
874eda6da92SYinghai Lu 	strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
875eda6da92SYinghai Lu 	*cmdline_p = command_line;
876eda6da92SYinghai Lu 
877eda6da92SYinghai Lu 	/*
8784b0f3b81SKees Cook 	 * x86_configure_nx() is called before parse_early_param() to detect
8794b0f3b81SKees Cook 	 * whether hardware doesn't support NX (so that the early EHCI debug
8804b0f3b81SKees Cook 	 * console setup can safely call set_fixmap()). It may then be called
8814b0f3b81SKees Cook 	 * again from within noexec_setup() during parsing early parameters
8824b0f3b81SKees Cook 	 * to honor the respective command line option.
883eda6da92SYinghai Lu 	 */
8844763ed4dSH. Peter Anvin 	x86_configure_nx();
885eda6da92SYinghai Lu 
886eda6da92SYinghai Lu 	parse_early_param();
887eda6da92SYinghai Lu 
888a799c2bdSMike Rapoport 	/*
889a799c2bdSMike Rapoport 	 * Do some memory reservations *before* memory is added to
890a799c2bdSMike Rapoport 	 * memblock, so memblock allocations won't overwrite it.
891a799c2bdSMike Rapoport 	 * Do it after early param, so we could get (unlikely) panic from
892a799c2bdSMike Rapoport 	 * serial.
893a799c2bdSMike Rapoport 	 *
894a799c2bdSMike Rapoport 	 * After this point everything still needed from the boot loader or
895a799c2bdSMike Rapoport 	 * firmware or kernel text should be early reserved or marked not
896a799c2bdSMike Rapoport 	 * RAM in e820. All other memory is free game.
897a799c2bdSMike Rapoport 	 */
898a799c2bdSMike Rapoport 	early_reserve_memory();
899a799c2bdSMike Rapoport 
90039fa104dSReza Arbab #ifdef CONFIG_MEMORY_HOTPLUG
90139fa104dSReza Arbab 	/*
90239fa104dSReza Arbab 	 * Memory used by the kernel cannot be hot-removed because Linux
90339fa104dSReza Arbab 	 * cannot migrate the kernel pages. When memory hotplug is
90439fa104dSReza Arbab 	 * enabled, we should prevent memblock from allocating memory
90539fa104dSReza Arbab 	 * for the kernel.
90639fa104dSReza Arbab 	 *
90739fa104dSReza Arbab 	 * ACPI SRAT records all hotpluggable memory ranges. But before
90839fa104dSReza Arbab 	 * SRAT is parsed, we don't know about it.
90939fa104dSReza Arbab 	 *
91039fa104dSReza Arbab 	 * The kernel image is loaded into memory at very early time. We
91139fa104dSReza Arbab 	 * cannot prevent this anyway. So on NUMA system, we set any
91239fa104dSReza Arbab 	 * node the kernel resides in as un-hotpluggable.
91339fa104dSReza Arbab 	 *
91439fa104dSReza Arbab 	 * Since on modern servers, one node could have double-digit
91539fa104dSReza Arbab 	 * gigabytes memory, we can assume the memory around the kernel
91639fa104dSReza Arbab 	 * image is also un-hotpluggable. So before SRAT is parsed, just
91739fa104dSReza Arbab 	 * allocate memory near the kernel image to try the best to keep
91839fa104dSReza Arbab 	 * the kernel away from hotpluggable memory.
91939fa104dSReza Arbab 	 */
92039fa104dSReza Arbab 	if (movable_node_is_enabled())
92139fa104dSReza Arbab 		memblock_set_bottom_up(true);
92239fa104dSReza Arbab #endif
92339fa104dSReza Arbab 
9244b0f3b81SKees Cook 	x86_report_nx();
9250ad5bce7SJeremy Fitzhardinge 
92655f26239SYinghai Lu 	if (acpi_mps_check()) {
92755f26239SYinghai Lu #ifdef CONFIG_X86_LOCAL_APIC
92855f26239SYinghai Lu 		disable_apic = 1;
92955f26239SYinghai Lu #endif
930988781dcSYinghai Lu 		setup_clear_cpu_cap(X86_FEATURE_APIC);
93155f26239SYinghai Lu 	}
93255f26239SYinghai Lu 
9331a127034SIngo Molnar 	e820__reserve_setup_data();
9349641bdafSIngo Molnar 	e820__finish_early_params();
93555f26239SYinghai Lu 
93683e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT))
937ff0c0874SBrian Maly 		efi_init();
938ff0c0874SBrian Maly 
9390fca0812SRobert Richter 	dmi_setup();
9402216d199SYinghai Lu 
94188b094fbSAlok Kataria 	/*
94288b094fbSAlok Kataria 	 * VMware detection requires dmi to be available, so this
9430fca0812SRobert Richter 	 * needs to be done after dmi_setup(), for the boot CPU.
94488b094fbSAlok Kataria 	 */
9452d826404SThomas Gleixner 	init_hypervisor_platform();
94688b094fbSAlok Kataria 
947cf7a63efSPavel Tatashin 	tsc_early_init();
948f7cf5a5bSThomas Gleixner 	x86_init.resources.probe_roms();
94955f26239SYinghai Lu 
9504046d6e8SLinus Torvalds 	/* after parse_early_param, so could debug it */
9514046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &code_resource);
952a3299754SKees Cook 	insert_resource(&iomem_resource, &rodata_resource);
9534046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &data_resource);
9544046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &bss_resource);
9554046d6e8SLinus Torvalds 
956b422a309SYinghai Lu 	e820_add_kernel_range();
9571b5576e6SYinghai Lu 	trim_bios_range();
95855f26239SYinghai Lu #ifdef CONFIG_X86_32
95955f26239SYinghai Lu 	if (ppro_with_ram_bug()) {
96009821ff1SIngo Molnar 		e820__range_update(0x70000000ULL, 0x40000ULL, E820_TYPE_RAM,
96109821ff1SIngo Molnar 				  E820_TYPE_RESERVED);
962f9748fa0SIngo Molnar 		e820__update_table(e820_table);
96355f26239SYinghai Lu 		printk(KERN_INFO "fixed physical RAM map:\n");
964be0c3f0fSIngo Molnar 		e820__print_table("bad_ppro");
96555f26239SYinghai Lu 	}
96655f26239SYinghai Lu #else
96755f26239SYinghai Lu 	early_gart_iommu_check();
96855f26239SYinghai Lu #endif
96955f26239SYinghai Lu 
97055f26239SYinghai Lu 	/*
97155f26239SYinghai Lu 	 * partially used pages are not usable - thus
97255f26239SYinghai Lu 	 * we are rounding upwards:
97355f26239SYinghai Lu 	 */
9740c6fc11aSIngo Molnar 	max_pfn = e820__end_of_ram_pfn();
97555f26239SYinghai Lu 
97655f26239SYinghai Lu 	/* update e820 for memory not covered by WB MTRRs */
97755f26239SYinghai Lu 	mtrr_bp_init();
9782dc807b3SYinghai Lu 	if (mtrr_trim_uncached_memory(max_pfn))
9790c6fc11aSIngo Molnar 		max_pfn = e820__end_of_ram_pfn();
98055f26239SYinghai Lu 
9818dd33030SIgor Mammedov 	max_possible_pfn = max_pfn;
9828dd33030SIgor Mammedov 
983c7d2361fSThomas Garnier 	/*
98499c13b8cSMikulas Patocka 	 * This call is required when the CPU does not support PAT. If
98599c13b8cSMikulas Patocka 	 * mtrr_bp_init() invoked it already via pat_init() the call has no
98699c13b8cSMikulas Patocka 	 * effect.
98799c13b8cSMikulas Patocka 	 */
98899c13b8cSMikulas Patocka 	init_cache_modes();
98999c13b8cSMikulas Patocka 
99099c13b8cSMikulas Patocka 	/*
991c7d2361fSThomas Garnier 	 * Define random base addresses for memory sections after max_pfn is
992c7d2361fSThomas Garnier 	 * defined and before each memory section base is used.
993c7d2361fSThomas Garnier 	 */
994c7d2361fSThomas Garnier 	kernel_randomize_memory();
995c7d2361fSThomas Garnier 
99655f26239SYinghai Lu #ifdef CONFIG_X86_32
99755f26239SYinghai Lu 	/* max_low_pfn get updated here */
99855f26239SYinghai Lu 	find_low_pfn_range();
99955f26239SYinghai Lu #else
10006e1cb38aSSuresh Siddha 	check_x2apic();
100155f26239SYinghai Lu 
100255f26239SYinghai Lu 	/* How many end-of-memory variables you have, grandma! */
100355f26239SYinghai Lu 	/* need this before calling reserve_initrd */
1004f361a450SYinghai Lu 	if (max_pfn > (1UL<<(32 - PAGE_SHIFT)))
10050c6fc11aSIngo Molnar 		max_low_pfn = e820__end_of_low_ram_pfn();
1006f361a450SYinghai Lu 	else
100755f26239SYinghai Lu 		max_low_pfn = max_pfn;
1008f361a450SYinghai Lu 
100955f26239SYinghai Lu 	high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
101055f26239SYinghai Lu #endif
101155f26239SYinghai Lu 
1012893f38d1SYinghai Lu 	/*
1013893f38d1SYinghai Lu 	 * Find and reserve possible boot-time SMP configuration:
1014893f38d1SYinghai Lu 	 */
1015893f38d1SYinghai Lu 	find_smp_config();
1016893f38d1SYinghai Lu 
10178d57470dSYinghai Lu 	early_alloc_pgt_buf();
10188d57470dSYinghai Lu 
101972d7c3b3SYinghai Lu 	/*
10204918e228SIngo Molnar 	 * Need to conclude brk, before e820__memblock_setup()
102172d7c3b3SYinghai Lu 	 * it could use memblock_find_in_range, could overlap with
102272d7c3b3SYinghai Lu 	 * brk area.
102372d7c3b3SYinghai Lu 	 */
102472d7c3b3SYinghai Lu 	reserve_brk();
102572d7c3b3SYinghai Lu 
1026e5f15b45SYinghai Lu 	cleanup_highmap();
1027e5f15b45SYinghai Lu 
10282449f343STang Chen 	memblock_set_current_limit(ISA_END_ADDRESS);
10294918e228SIngo Molnar 	e820__memblock_setup();
103072d7c3b3SYinghai Lu 
1031e998879dSAshish Kalra 	/*
1032e998879dSAshish Kalra 	 * Needs to run after memblock setup because it needs the physical
1033e998879dSAshish Kalra 	 * memory size.
1034e998879dSAshish Kalra 	 */
1035e998879dSAshish Kalra 	sev_setup_arch();
1036e998879dSAshish Kalra 
10374971531aSMatt Fleming 	efi_fake_memmap();
10384971531aSMatt Fleming 	efi_find_mirror();
10393dad6f7fSRicardo Neri 	efi_esrt_init();
104058c90902SLenny Szubowicz 	efi_mokvar_table_init();
10414971531aSMatt Fleming 
1042916f676fSMatthew Garrett 	/*
10434971531aSMatt Fleming 	 * The EFI specification says that boot service code won't be
10444971531aSMatt Fleming 	 * called after ExitBootServices(). This is, in fact, a lie.
1045916f676fSMatthew Garrett 	 */
1046916f676fSMatthew Garrett 	efi_reserve_boot_services();
1047916f676fSMatthew Garrett 
104872d7c3b3SYinghai Lu 	/* preallocate 4k for mptable mpc */
10495da217caSIngo Molnar 	e820__memblock_alloc_reserved_mpc_new();
105072d7c3b3SYinghai Lu 
105172d7c3b3SYinghai Lu #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
105272d7c3b3SYinghai Lu 	setup_bios_corruption_check();
105372d7c3b3SYinghai Lu #endif
105472d7c3b3SYinghai Lu 
105510054230SYinghai Lu #ifdef CONFIG_X86_32
1056365811d6SBjorn Helgaas 	printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n",
1057365811d6SBjorn Helgaas 			(max_pfn_mapped<<PAGE_SHIFT) - 1);
105810054230SYinghai Lu #endif
105972d7c3b3SYinghai Lu 
1060c361e5d4SMike Rapoport 	/*
1061ec35d1d9SBorislav Petkov 	 * Find free memory for the real mode trampoline and place it there. If
1062ec35d1d9SBorislav Petkov 	 * there is not enough free memory under 1M, on EFI-enabled systems
1063ec35d1d9SBorislav Petkov 	 * there will be additional attempt to reclaim the memory for the real
1064ec35d1d9SBorislav Petkov 	 * mode trampoline at efi_free_boot_services().
1065f1d4d47cSMike Rapoport 	 *
1066ec35d1d9SBorislav Petkov 	 * Unconditionally reserve the entire first 1M of RAM because BIOSes
1067ec35d1d9SBorislav Petkov 	 * are known to corrupt low memory and several hundred kilobytes are not
1068ec35d1d9SBorislav Petkov 	 * worth complex detection what memory gets clobbered. Windows does the
1069ec35d1d9SBorislav Petkov 	 * same thing for very similar reasons.
1070ec35d1d9SBorislav Petkov 	 *
1071ec35d1d9SBorislav Petkov 	 * Moreover, on machines with SandyBridge graphics or in setups that use
1072ec35d1d9SBorislav Petkov 	 * crashkernel the entire 1M is reserved anyway.
1073c361e5d4SMike Rapoport 	 */
1074f1d4d47cSMike Rapoport 	reserve_real_mode();
1075a9acc536SJesse Barnes 
107622ddfcaaSYinghai Lu 	init_mem_mapping();
1077854c879fSPekka J Enberg 
1078433f8924SThomas Gleixner 	idt_setup_early_pf();
1079cda846f1SJarkko Sakkinen 
108018bc7bd5SAndy Lutomirski 	/*
108118bc7bd5SAndy Lutomirski 	 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features)
108218bc7bd5SAndy Lutomirski 	 * with the current CR4 value.  This may not be necessary, but
108318bc7bd5SAndy Lutomirski 	 * auditing all the early-boot CR4 manipulation would be needed to
108418bc7bd5SAndy Lutomirski 	 * rule it out.
1085c7ad5ad2SAndy Lutomirski 	 *
1086c7ad5ad2SAndy Lutomirski 	 * Mask off features that don't work outside long mode (just
1087c7ad5ad2SAndy Lutomirski 	 * PCIDE for now).
108818bc7bd5SAndy Lutomirski 	 */
1089c7ad5ad2SAndy Lutomirski 	mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE;
109018bc7bd5SAndy Lutomirski 
10914ce7a869SYinghai Lu 	memblock_set_current_limit(get_max_mapped());
1092e7b37895SYinghai Lu 
1093e7b37895SYinghai Lu 	/*
1094e7b37895SYinghai Lu 	 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
1095e7b37895SYinghai Lu 	 */
1096e7b37895SYinghai Lu 
1097e7b37895SYinghai Lu #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
1098e7b37895SYinghai Lu 	if (init_ohci1394_dma_early)
1099162a7e75SMike Travis 		init_ohci1394_dma_on_all_controllers();
1100162a7e75SMike Travis #endif
1101e7b37895SYinghai Lu 	/* Allocate bigger log buffer */
110255f26239SYinghai Lu 	setup_log_buf(1);
110355f26239SYinghai Lu 
11049661b332SDavid Howells 	if (efi_enabled(EFI_BOOT)) {
11059661b332SDavid Howells 		switch (boot_params.secure_boot) {
11069661b332SDavid Howells 		case efi_secureboot_mode_disabled:
11079661b332SDavid Howells 			pr_info("Secure boot disabled\n");
11089661b332SDavid Howells 			break;
11099661b332SDavid Howells 		case efi_secureboot_mode_enabled:
11109661b332SDavid Howells 			pr_info("Secure boot enabled\n");
11119661b332SDavid Howells 			break;
11129661b332SDavid Howells 		default:
11139661b332SDavid Howells 			pr_info("Secure boot could not be determined\n");
11149661b332SDavid Howells 			break;
11159661b332SDavid Howells 		}
11169661b332SDavid Howells 	}
11179661b332SDavid Howells 
111844280733SYinghai Lu 	reserve_initrd();
111944280733SYinghai Lu 
1120da3d3f98SAleksey Makarov 	acpi_table_upgrade();
11216998a880SRafael J. Wysocki 	/* Look for ACPI tables and reserve memory occupied by them. */
11226998a880SRafael J. Wysocki 	acpi_boot_table_init();
112355f26239SYinghai Lu 
112455f26239SYinghai Lu 	vsmp_init();
112555f26239SYinghai Lu 
112655f26239SYinghai Lu 	io_delay_init();
112755f26239SYinghai Lu 
1128630b3affSLukas Wunner 	early_platform_quirks();
1129630b3affSLukas Wunner 
113020e6926dSYinghai Lu 	early_acpi_boot_init();
113120e6926dSYinghai Lu 
113291467bdfSBernhard Walle 	initmem_init();
11333c325f82SWeijie Yang 	dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
1134fa591c4aSTang Chen 
1135cf11e85fSRoman Gushchin 	if (boot_cpu_has(X86_FEATURE_GBPAGES))
1136cf11e85fSRoman Gushchin 		hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
1137cf11e85fSRoman Gushchin 
1138fa591c4aSTang Chen 	/*
1139fa591c4aSTang Chen 	 * Reserve memory for crash kernel after SRAT is parsed so that it
1140fa591c4aSTang Chen 	 * won't consume hotpluggable memory.
1141fa591c4aSTang Chen 	 */
1142fa591c4aSTang Chen 	reserve_crashkernel();
1143fa591c4aSTang Chen 
114455f26239SYinghai Lu 	memblock_find_dma_reserve();
114555f26239SYinghai Lu 
1146ccb64941SBoris Ostrovsky 	if (!early_xdbc_setup_hardware())
1147ccb64941SBoris Ostrovsky 		early_xdbc_register_console();
1148ccb64941SBoris Ostrovsky 
1149030cb6c0SThomas Gleixner 	x86_init.paging.pagetable_init();
115055f26239SYinghai Lu 
1151ef7f0d6aSAndrey Ryabinin 	kasan_init();
1152ef7f0d6aSAndrey Ryabinin 
1153d2b6dc61SAndy Lutomirski 	/*
1154945fd17aSThomas Gleixner 	 * Sync back kernel address range.
1155945fd17aSThomas Gleixner 	 *
1156945fd17aSThomas Gleixner 	 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
1157945fd17aSThomas Gleixner 	 * this call?
1158d2b6dc61SAndy Lutomirski 	 */
1159945fd17aSThomas Gleixner 	sync_initial_page_table();
1160d2b6dc61SAndy Lutomirski 
116131625340SJoseph Cihula 	tboot_probe();
116231625340SJoseph Cihula 
116355f26239SYinghai Lu 	map_vsyscall();
116455f26239SYinghai Lu 
116555f26239SYinghai Lu 	generic_apic_probe();
116655f26239SYinghai Lu 
116755f26239SYinghai Lu 	early_quirks();
116855f26239SYinghai Lu 
116955f26239SYinghai Lu 	/*
117055f26239SYinghai Lu 	 * Read APIC and some other early information from ACPI tables.
117155f26239SYinghai Lu 	 */
117255f26239SYinghai Lu 	acpi_boot_init();
1173a906fdaaSThomas Gleixner 	x86_dtb_init();
1174efafc8b2SFeng Tang 
117555f26239SYinghai Lu 	/*
117655f26239SYinghai Lu 	 * get boot-time SMP configuration:
117755f26239SYinghai Lu 	 */
117855f26239SYinghai Lu 	get_smp_config();
117955f26239SYinghai Lu 
11801e90a13dSThomas Gleixner 	/*
11811e90a13dSThomas Gleixner 	 * Systems w/o ACPI and mptables might not have it mapped the local
11821e90a13dSThomas Gleixner 	 * APIC yet, but prefill_possible_map() might need to access it.
11831e90a13dSThomas Gleixner 	 */
11841e90a13dSThomas Gleixner 	init_apic_mappings();
11851e90a13dSThomas Gleixner 
1186329513a3SYinghai Lu 	prefill_possible_map();
1187301e6190SYinghai Lu 
11885f4765f9SYinghai Lu 	init_cpu_to_node();
118973bf7382SJonathan Cameron 	init_gi_nodes();
11905f4765f9SYinghai Lu 
1191ca1b8862SThomas Gleixner 	io_apic_init_mappings();
11929d6a4d08SYinghai Lu 
1193f3614646SJuergen Gross 	x86_init.hyper.guest_late_init();
119455f26239SYinghai Lu 
11951506c8dcSIngo Molnar 	e820__reserve_resources();
1196cc55f753SZhimin Gu 	e820__register_nosave_regions(max_pfn);
119755f26239SYinghai Lu 
11988fee697dSThomas Gleixner 	x86_init.resources.reserve_resources();
119955f26239SYinghai Lu 
12002df908baSIngo Molnar 	e820__setup_pci_gap();
120155f26239SYinghai Lu 
120255f26239SYinghai Lu #ifdef CONFIG_VT
120355f26239SYinghai Lu #if defined(CONFIG_VGA_CONSOLE)
120483e68189SMatt Fleming 	if (!efi_enabled(EFI_BOOT) || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
120555f26239SYinghai Lu 		conswitchp = &vga_con;
120655f26239SYinghai Lu #endif
120755f26239SYinghai Lu #endif
12086f30c1acSThomas Gleixner 	x86_init.oem.banner();
1209a2202aa2SYong Wang 
12106b617e22SFeng Tang 	x86_init.timers.wallclock_init();
12116b617e22SFeng Tang 
12129a90ed06SBorislav Petkov 	/*
12139a90ed06SBorislav Petkov 	 * This needs to run before setup_local_APIC() which soft-disables the
12149a90ed06SBorislav Petkov 	 * local APIC temporarily and that masks the thermal LVT interrupt,
12159a90ed06SBorislav Petkov 	 * leading to softlockups on machines which have configured SMI
12169a90ed06SBorislav Petkov 	 * interrupt delivery.
12179a90ed06SBorislav Petkov 	 */
12189a90ed06SBorislav Petkov 	therm_lvt_init();
12199a90ed06SBorislav Petkov 
1220a2202aa2SYong Wang 	mcheck_init();
1221f49aa448SJason Baron 
1222b3c869d3SJohn Stultz 	register_refined_jiffies(CLOCK_TICK_RATE);
12235189c2a7SOlof Johansson 
12245189c2a7SOlof Johansson #ifdef CONFIG_EFI
1225a5d90c92SBorislav Petkov 	if (efi_enabled(EFI_BOOT))
1226a5d90c92SBorislav Petkov 		efi_apply_memmap_quirks();
12275189c2a7SOlof Johansson #endif
1228ee9f8fceSJosh Poimboeuf 
1229ee9f8fceSJosh Poimboeuf 	unwind_init();
123055f26239SYinghai Lu }
12315649b7c3SIngo Molnar 
12329be1b56aSIngo Molnar #ifdef CONFIG_X86_32
12335649b7c3SIngo Molnar 
12348fee697dSThomas Gleixner static struct resource video_ram_resource = {
12358fee697dSThomas Gleixner 	.name	= "Video RAM area",
12368fee697dSThomas Gleixner 	.start	= 0xa0000,
12378fee697dSThomas Gleixner 	.end	= 0xbffff,
12388fee697dSThomas Gleixner 	.flags	= IORESOURCE_BUSY | IORESOURCE_MEM
12399be1b56aSIngo Molnar };
12409be1b56aSIngo Molnar 
12418fee697dSThomas Gleixner void __init i386_reserve_resources(void)
12429be1b56aSIngo Molnar {
12438fee697dSThomas Gleixner 	request_resource(&iomem_resource, &video_ram_resource);
12448fee697dSThomas Gleixner 	reserve_standard_io_resources();
12459be1b56aSIngo Molnar }
12469be1b56aSIngo Molnar 
12479be1b56aSIngo Molnar #endif /* CONFIG_X86_32 */
1248f32360efSKees Cook 
1249f32360efSKees Cook static struct notifier_block kernel_offset_notifier = {
1250f32360efSKees Cook 	.notifier_call = dump_kernel_offset
1251f32360efSKees Cook };
1252f32360efSKees Cook 
1253f32360efSKees Cook static int __init register_kernel_offset_dumper(void)
1254f32360efSKees Cook {
1255f32360efSKees Cook 	atomic_notifier_chain_register(&panic_notifier_list,
1256f32360efSKees Cook 					&kernel_offset_notifier);
1257f32360efSKees Cook 	return 0;
1258f32360efSKees Cook }
1259f32360efSKees Cook __initcall(register_kernel_offset_dumper);
1260