xref: /linux-6.15/arch/x86/kernel/setup.c (revision 4059ba65)
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  */
8ea7b4244SNathan Chancellor #include <linux/acpi.h>
955f26239SYinghai Lu #include <linux/console.h>
1012609013SIngo Molnar #include <linux/crash_dump.h>
110b1abd1fSChristoph Hellwig #include <linux/dma-map-ops.h>
1255f26239SYinghai Lu #include <linux/dmi.h>
1312609013SIngo Molnar #include <linux/efi.h>
14b69a2afdSJonathan McDowell #include <linux/ima.h>
1555f26239SYinghai Lu #include <linux/init_ohci1394_dma.h>
1612609013SIngo Molnar #include <linux/initrd.h>
1712609013SIngo Molnar #include <linux/iscsi_ibft.h>
1812609013SIngo Molnar #include <linux/memblock.h>
19f39650deSAndy Shevchenko #include <linux/panic_notifier.h>
2012609013SIngo Molnar #include <linux/pci.h>
2112609013SIngo Molnar #include <linux/root_dev.h>
22cf11e85fSRoman Gushchin #include <linux/hugetlb.h>
2312609013SIngo Molnar #include <linux/tboot.h>
2412609013SIngo Molnar #include <linux/usb/xhci-dbgp.h>
25a945c834SPeter Zijlstra #include <linux/static_call.h>
260a0f0d8bSChristoph Hellwig #include <linux/swiotlb.h>
2768b8e971SJason A. Donenfeld #include <linux/random.h>
2812609013SIngo Molnar 
29e262e32dSDavid Howells #include <uapi/linux/mount.h>
3055f26239SYinghai Lu 
3112609013SIngo Molnar #include <xen/xen.h>
3255f26239SYinghai Lu 
3355f26239SYinghai Lu #include <asm/apic.h>
34*4059ba65SArd Biesheuvel #include <asm/efi.h>
3513c01139SIngo Molnar #include <asm/numa.h>
3655f26239SYinghai Lu #include <asm/bios_ebda.h>
3755f26239SYinghai Lu #include <asm/bugs.h>
386e5385d4SJaswinder Singh Rajput #include <asm/cpu.h>
3912609013SIngo Molnar #include <asm/efi.h>
401d9b16d1SJoerg Roedel #include <asm/gart.h>
4188b094fbSAlok Kataria #include <asm/hypervisor.h>
4212609013SIngo Molnar #include <asm/io_apic.h>
4312609013SIngo Molnar #include <asm/kasan.h>
440483e1faSThomas Garnier #include <asm/kaslr.h>
4512609013SIngo Molnar #include <asm/mce.h>
464d5cff69SChristoph Hellwig #include <asm/memtype.h>
4712609013SIngo Molnar #include <asm/mtrr.h>
48ca947b72SSean Christopherson #include <asm/realmode.h>
4912609013SIngo Molnar #include <asm/olpc_ofw.h>
5012609013SIngo Molnar #include <asm/pci-direct.h>
5112609013SIngo Molnar #include <asm/prom.h>
5212609013SIngo Molnar #include <asm/proto.h>
539a90ed06SBorislav Petkov #include <asm/thermal.h>
54ee9f8fceSJosh Poimboeuf #include <asm/unwind.h>
5512609013SIngo Molnar #include <asm/vsyscall.h>
56186525bdSIngo Molnar #include <linux/vmalloc.h>
5755f26239SYinghai Lu 
582b72394eSPekka Enberg /*
59360db4acSIngo Molnar  * max_low_pfn_mapped: highest directly mapped pfn < 4 GB
60360db4acSIngo Molnar  * max_pfn_mapped:     highest directly mapped pfn > 4 GB
6166520ebcSJacob Shin  *
6209821ff1SIngo Molnar  * The direct mapping only covers E820_TYPE_RAM regions, so the ranges and gaps are
63360db4acSIngo Molnar  * represented by pfn_mapped[].
642b72394eSPekka Enberg  */
652b72394eSPekka Enberg unsigned long max_low_pfn_mapped;
662b72394eSPekka Enberg unsigned long max_pfn_mapped;
672b72394eSPekka Enberg 
68e808bae2SThadeu Lima de Souza Cascardo #ifdef CONFIG_DMI
69796216a5SJeremy Fitzhardinge RESERVE_BRK(dmi_alloc, 65536);
70e808bae2SThadeu Lima de Souza Cascardo #endif
71796216a5SJeremy Fitzhardinge 
72c0b5842aSIngo 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 
146b69a2afdSJonathan McDowell #ifdef CONFIG_IMA
147b69a2afdSJonathan McDowell static phys_addr_t ima_kexec_buffer_phys;
148b69a2afdSJonathan McDowell static size_t ima_kexec_buffer_size;
149b69a2afdSJonathan McDowell #endif
150b69a2afdSJonathan McDowell 
1515031296cSH. Peter Anvin /* Boot loader ID and version as integers, for the benefit of proc_dointvec */
1525031296cSH. Peter Anvin int bootloader_type, bootloader_version;
15355f26239SYinghai Lu 
15455f26239SYinghai Lu /*
15555f26239SYinghai Lu  * Setup options
15655f26239SYinghai Lu  */
15755f26239SYinghai Lu struct screen_info screen_info;
15855f26239SYinghai Lu EXPORT_SYMBOL(screen_info);
15955f26239SYinghai Lu struct edid_info edid_info;
16055f26239SYinghai Lu EXPORT_SYMBOL_GPL(edid_info);
16155f26239SYinghai Lu 
16255f26239SYinghai Lu extern int root_mountflags;
16355f26239SYinghai Lu 
16455f26239SYinghai Lu unsigned long saved_video_mode;
16555f26239SYinghai Lu 
16655f26239SYinghai Lu #define RAMDISK_IMAGE_START_MASK	0x07FF
16755f26239SYinghai Lu #define RAMDISK_PROMPT_FLAG		0x8000
16855f26239SYinghai Lu #define RAMDISK_LOAD_FLAG		0x4000
16955f26239SYinghai Lu 
17055f26239SYinghai Lu static char __initdata command_line[COMMAND_LINE_SIZE];
171516cbf37STim Bird #ifdef CONFIG_CMDLINE_BOOL
172516cbf37STim Bird static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
173516cbf37STim Bird #endif
17455f26239SYinghai Lu 
17555f26239SYinghai Lu #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
17655f26239SYinghai Lu struct edd edd;
17755f26239SYinghai Lu #ifdef CONFIG_EDD_MODULE
17855f26239SYinghai Lu EXPORT_SYMBOL(edd);
17955f26239SYinghai Lu #endif
18055f26239SYinghai Lu /**
18155f26239SYinghai Lu  * copy_edd() - Copy the BIOS EDD information
18255f26239SYinghai Lu  *              from boot_params into a safe place.
18355f26239SYinghai Lu  *
18455f26239SYinghai Lu  */
1859eaa192dSHelight.Xu static inline void __init copy_edd(void)
18655f26239SYinghai Lu {
18755f26239SYinghai Lu      memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer,
18855f26239SYinghai Lu 	    sizeof(edd.mbr_signature));
18955f26239SYinghai Lu      memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info));
19055f26239SYinghai Lu      edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries;
19155f26239SYinghai Lu      edd.edd_info_nr = boot_params.eddbuf_entries;
19255f26239SYinghai Lu }
19355f26239SYinghai Lu #else
1949eaa192dSHelight.Xu static inline void __init copy_edd(void)
19555f26239SYinghai Lu {
19655f26239SYinghai Lu }
19755f26239SYinghai Lu #endif
19855f26239SYinghai Lu 
1995368a2beSH. Peter Anvin void * __init extend_brk(size_t size, size_t align)
2005368a2beSH. Peter Anvin {
2015368a2beSH. Peter Anvin 	size_t mask = align - 1;
2025368a2beSH. Peter Anvin 	void *ret;
2035368a2beSH. Peter Anvin 
2045368a2beSH. Peter Anvin 	BUG_ON(_brk_start == 0);
2055368a2beSH. Peter Anvin 	BUG_ON(align & mask);
2065368a2beSH. Peter Anvin 
2075368a2beSH. Peter Anvin 	_brk_end = (_brk_end + mask) & ~mask;
2085368a2beSH. Peter Anvin 	BUG_ON((char *)(_brk_end + size) > __brk_limit);
2095368a2beSH. Peter Anvin 
2105368a2beSH. Peter Anvin 	ret = (void *)_brk_end;
2115368a2beSH. Peter Anvin 	_brk_end += size;
2125368a2beSH. Peter Anvin 
2135368a2beSH. Peter Anvin 	memset(ret, 0, size);
2145368a2beSH. Peter Anvin 
2155368a2beSH. Peter Anvin 	return ret;
2165368a2beSH. Peter Anvin }
2175368a2beSH. Peter Anvin 
218148b2098SYinghai Lu #ifdef CONFIG_X86_32
219e5f15b45SYinghai Lu static void __init cleanup_highmap(void)
220f005fe12SYinghai Lu {
221f005fe12SYinghai Lu }
222854c879fSPekka J Enberg #endif
223854c879fSPekka J Enberg 
2245368a2beSH. Peter Anvin static void __init reserve_brk(void)
2255368a2beSH. Peter Anvin {
2265368a2beSH. Peter Anvin 	if (_brk_end > _brk_start)
227fc8d7826SAlexander Duyck 		memblock_reserve(__pa_symbol(_brk_start),
228fc8d7826SAlexander Duyck 				 _brk_end - _brk_start);
2295368a2beSH. Peter Anvin 
2305368a2beSH. Peter Anvin 	/* Mark brk area as locked down and no longer taking any
2315368a2beSH. Peter Anvin 	   new allocations */
2325368a2beSH. Peter Anvin 	_brk_start = 0;
2335368a2beSH. Peter Anvin }
2345368a2beSH. Peter Anvin 
2355aa3d718SBorislav Petkov u64 relocated_ramdisk;
2365aa3d718SBorislav Petkov 
23755f26239SYinghai Lu #ifdef CONFIG_BLK_DEV_INITRD
23855f26239SYinghai Lu 
239a8a51a88SYinghai Lu static u64 __init get_ramdisk_image(void)
240a8a51a88SYinghai Lu {
241a8a51a88SYinghai Lu 	u64 ramdisk_image = boot_params.hdr.ramdisk_image;
242a8a51a88SYinghai Lu 
243ee92d815SYinghai Lu 	ramdisk_image |= (u64)boot_params.ext_ramdisk_image << 32;
244ee92d815SYinghai Lu 
245694cfd87SRonald G. Minnich 	if (ramdisk_image == 0)
246694cfd87SRonald G. Minnich 		ramdisk_image = phys_initrd_start;
247694cfd87SRonald G. Minnich 
248a8a51a88SYinghai Lu 	return ramdisk_image;
249a8a51a88SYinghai Lu }
250a8a51a88SYinghai Lu static u64 __init get_ramdisk_size(void)
251a8a51a88SYinghai Lu {
252a8a51a88SYinghai Lu 	u64 ramdisk_size = boot_params.hdr.ramdisk_size;
253a8a51a88SYinghai Lu 
254ee92d815SYinghai Lu 	ramdisk_size |= (u64)boot_params.ext_ramdisk_size << 32;
255ee92d815SYinghai Lu 
256694cfd87SRonald G. Minnich 	if (ramdisk_size == 0)
257694cfd87SRonald G. Minnich 		ramdisk_size = phys_initrd_size;
258694cfd87SRonald G. Minnich 
259a8a51a88SYinghai Lu 	return ramdisk_size;
260a8a51a88SYinghai Lu }
261a8a51a88SYinghai Lu 
26255f26239SYinghai Lu static void __init relocate_initrd(void)
26355f26239SYinghai Lu {
264c967da6aSYinghai Lu 	/* Assume only end is not page aligned */
265a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
266a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
267c967da6aSYinghai Lu 	u64 area_size     = PAGE_ALIGN(ramdisk_size);
26855f26239SYinghai Lu 
269e8c57d40SYinghai Lu 	/* We need to move the initrd down into directly mapped mem */
2703c45ee6dSMike Rapoport 	relocated_ramdisk = memblock_phys_alloc_range(area_size, PAGE_SIZE, 0,
2713c45ee6dSMike Rapoport 						      PFN_PHYS(max_pfn_mapped));
2725aa3d718SBorislav Petkov 	if (!relocated_ramdisk)
27355f26239SYinghai Lu 		panic("Cannot find place for new RAMDISK of size %lld\n",
27455f26239SYinghai Lu 		      ramdisk_size);
27555f26239SYinghai Lu 
2765aa3d718SBorislav Petkov 	initrd_start = relocated_ramdisk + PAGE_OFFSET;
27755f26239SYinghai Lu 	initrd_end   = initrd_start + ramdisk_size;
278365811d6SBjorn Helgaas 	printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
2795aa3d718SBorislav Petkov 	       relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
28055f26239SYinghai Lu 
2815dd2c4bdSMark Salter 	copy_from_early_mem((void *)initrd_start, ramdisk_image, ramdisk_size);
28255f26239SYinghai Lu 
283365811d6SBjorn Helgaas 	printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to"
284365811d6SBjorn Helgaas 		" [mem %#010llx-%#010llx]\n",
28555f26239SYinghai Lu 		ramdisk_image, ramdisk_image + ramdisk_size - 1,
2865aa3d718SBorislav Petkov 		relocated_ramdisk, relocated_ramdisk + ramdisk_size - 1);
28755f26239SYinghai Lu }
28855f26239SYinghai Lu 
2891b8c78beSYinghai Lu static void __init early_reserve_initrd(void)
2901b8c78beSYinghai Lu {
2911b8c78beSYinghai Lu 	/* Assume only end is not page aligned */
292a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
293a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
2941b8c78beSYinghai Lu 	u64 ramdisk_end   = PAGE_ALIGN(ramdisk_image + ramdisk_size);
2951b8c78beSYinghai Lu 
2961b8c78beSYinghai Lu 	if (!boot_params.hdr.type_of_loader ||
2971b8c78beSYinghai Lu 	    !ramdisk_image || !ramdisk_size)
2981b8c78beSYinghai Lu 		return;		/* No initrd provided by bootloader */
2991b8c78beSYinghai Lu 
3001b8c78beSYinghai Lu 	memblock_reserve(ramdisk_image, ramdisk_end - ramdisk_image);
3011b8c78beSYinghai Lu }
3023c45ee6dSMike Rapoport 
30355f26239SYinghai Lu static void __init reserve_initrd(void)
30455f26239SYinghai Lu {
305c967da6aSYinghai Lu 	/* Assume only end is not page aligned */
306a8a51a88SYinghai Lu 	u64 ramdisk_image = get_ramdisk_image();
307a8a51a88SYinghai Lu 	u64 ramdisk_size  = get_ramdisk_size();
308c967da6aSYinghai Lu 	u64 ramdisk_end   = PAGE_ALIGN(ramdisk_image + ramdisk_size);
30955f26239SYinghai Lu 
31055f26239SYinghai Lu 	if (!boot_params.hdr.type_of_loader ||
31155f26239SYinghai Lu 	    !ramdisk_image || !ramdisk_size)
31255f26239SYinghai Lu 		return;		/* No initrd provided by bootloader */
31355f26239SYinghai Lu 
31455f26239SYinghai Lu 	initrd_start = 0;
31555f26239SYinghai Lu 
316365811d6SBjorn Helgaas 	printk(KERN_INFO "RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image,
317365811d6SBjorn Helgaas 			ramdisk_end - 1);
31855f26239SYinghai Lu 
31974f27655SYinghai Lu 	if (pfn_range_is_mapped(PFN_DOWN(ramdisk_image),
320e8c57d40SYinghai Lu 				PFN_DOWN(ramdisk_end))) {
321e8c57d40SYinghai Lu 		/* All are mapped, easy case */
32255f26239SYinghai Lu 		initrd_start = ramdisk_image + PAGE_OFFSET;
32355f26239SYinghai Lu 		initrd_end = initrd_start + ramdisk_size;
32455f26239SYinghai Lu 		return;
32555f26239SYinghai Lu 	}
32655f26239SYinghai Lu 
32755f26239SYinghai Lu 	relocate_initrd();
3288c5dd8f4SYinghai Lu 
3293ecc6834SMike Rapoport 	memblock_phys_free(ramdisk_image, ramdisk_end - ramdisk_image);
33055f26239SYinghai Lu }
331af06f8b7SLv Zheng 
33255f26239SYinghai Lu #else
3331b8c78beSYinghai Lu static void __init early_reserve_initrd(void)
3341b8c78beSYinghai Lu {
3351b8c78beSYinghai Lu }
33655f26239SYinghai Lu static void __init reserve_initrd(void)
33755f26239SYinghai Lu {
33855f26239SYinghai Lu }
33955f26239SYinghai Lu #endif /* CONFIG_BLK_DEV_INITRD */
34055f26239SYinghai Lu 
341b69a2afdSJonathan McDowell static void __init add_early_ima_buffer(u64 phys_addr)
342b69a2afdSJonathan McDowell {
343b69a2afdSJonathan McDowell #ifdef CONFIG_IMA
344b69a2afdSJonathan McDowell 	struct ima_setup_data *data;
345b69a2afdSJonathan McDowell 
346b69a2afdSJonathan McDowell 	data = early_memremap(phys_addr + sizeof(struct setup_data), sizeof(*data));
347b69a2afdSJonathan McDowell 	if (!data) {
348b69a2afdSJonathan McDowell 		pr_warn("setup: failed to memremap ima_setup_data entry\n");
349b69a2afdSJonathan McDowell 		return;
350b69a2afdSJonathan McDowell 	}
351b69a2afdSJonathan McDowell 
352b69a2afdSJonathan McDowell 	if (data->size) {
353b69a2afdSJonathan McDowell 		memblock_reserve(data->addr, data->size);
354b69a2afdSJonathan McDowell 		ima_kexec_buffer_phys = data->addr;
355b69a2afdSJonathan McDowell 		ima_kexec_buffer_size = data->size;
356b69a2afdSJonathan McDowell 	}
357b69a2afdSJonathan McDowell 
358b69a2afdSJonathan McDowell 	early_memunmap(data, sizeof(*data));
359b69a2afdSJonathan McDowell #else
360b69a2afdSJonathan McDowell 	pr_warn("Passed IMA kexec data, but CONFIG_IMA not set. Ignoring.\n");
361b69a2afdSJonathan McDowell #endif
362b69a2afdSJonathan McDowell }
363b69a2afdSJonathan McDowell 
364b69a2afdSJonathan McDowell #if defined(CONFIG_HAVE_IMA_KEXEC) && !defined(CONFIG_OF_FLATTREE)
365b69a2afdSJonathan McDowell int __init ima_free_kexec_buffer(void)
366b69a2afdSJonathan McDowell {
367b69a2afdSJonathan McDowell 	int rc;
368b69a2afdSJonathan McDowell 
369b69a2afdSJonathan McDowell 	if (!ima_kexec_buffer_size)
370b69a2afdSJonathan McDowell 		return -ENOENT;
371b69a2afdSJonathan McDowell 
372b69a2afdSJonathan McDowell 	rc = memblock_phys_free(ima_kexec_buffer_phys,
373b69a2afdSJonathan McDowell 				ima_kexec_buffer_size);
374b69a2afdSJonathan McDowell 	if (rc)
375b69a2afdSJonathan McDowell 		return rc;
376b69a2afdSJonathan McDowell 
377b69a2afdSJonathan McDowell 	ima_kexec_buffer_phys = 0;
378b69a2afdSJonathan McDowell 	ima_kexec_buffer_size = 0;
379b69a2afdSJonathan McDowell 
380b69a2afdSJonathan McDowell 	return 0;
381b69a2afdSJonathan McDowell }
382b69a2afdSJonathan McDowell 
383b69a2afdSJonathan McDowell int __init ima_get_kexec_buffer(void **addr, size_t *size)
384b69a2afdSJonathan McDowell {
385b69a2afdSJonathan McDowell 	if (!ima_kexec_buffer_size)
386b69a2afdSJonathan McDowell 		return -ENOENT;
387b69a2afdSJonathan McDowell 
388b69a2afdSJonathan McDowell 	*addr = __va(ima_kexec_buffer_phys);
389b69a2afdSJonathan McDowell 	*size = ima_kexec_buffer_size;
390b69a2afdSJonathan McDowell 
391b69a2afdSJonathan McDowell 	return 0;
392b69a2afdSJonathan McDowell }
393b69a2afdSJonathan McDowell #endif
394b69a2afdSJonathan McDowell 
39529f784e3SYinghai Lu static void __init parse_setup_data(void)
396257b0fdeSYinghai Lu {
397257b0fdeSYinghai Lu 	struct setup_data *data;
39830e46b57SLinn Crosetto 	u64 pa_data, pa_next;
399257b0fdeSYinghai Lu 
400257b0fdeSYinghai Lu 	pa_data = boot_params.hdr.setup_data;
401257b0fdeSYinghai Lu 	while (pa_data) {
4027389882cSWANG Chao 		u32 data_len, data_type;
403f1c2b357SSebastian Andrzej Siewior 
4047389882cSWANG Chao 		data = early_memremap(pa_data, sizeof(*data));
405f1c2b357SSebastian Andrzej Siewior 		data_len = data->len + sizeof(struct setup_data);
40630e46b57SLinn Crosetto 		data_type = data->type;
40730e46b57SLinn Crosetto 		pa_next = data->next;
4088d4a40bcSJuergen Gross 		early_memunmap(data, sizeof(*data));
409f1c2b357SSebastian Andrzej Siewior 
41030e46b57SLinn Crosetto 		switch (data_type) {
411257b0fdeSYinghai Lu 		case SETUP_E820_EXT:
412914053c0SIngo Molnar 			e820__memory_setup_extended(pa_data, data_len);
413257b0fdeSYinghai Lu 			break;
414da6b737bSSebastian Andrzej Siewior 		case SETUP_DTB:
415da6b737bSSebastian Andrzej Siewior 			add_dtb(pa_data);
416257b0fdeSYinghai Lu 			break;
4171fec0533SDave Young 		case SETUP_EFI:
4181fec0533SDave Young 			parse_efi_setup(pa_data, data_len);
4191fec0533SDave Young 			break;
420b69a2afdSJonathan McDowell 		case SETUP_IMA:
421b69a2afdSJonathan McDowell 			add_early_ima_buffer(pa_data);
422b69a2afdSJonathan McDowell 			break;
42368b8e971SJason A. Donenfeld 		case SETUP_RNG_SEED:
42468b8e971SJason A. Donenfeld 			data = early_memremap(pa_data, data_len);
42568b8e971SJason A. Donenfeld 			add_bootloader_randomness(data->data, data->len);
42668b8e971SJason A. Donenfeld 			/* Zero seed for forward secrecy. */
42768b8e971SJason A. Donenfeld 			memzero_explicit(data->data, data->len);
42868b8e971SJason A. Donenfeld 			/* Zero length in case we find ourselves back here by accident. */
42968b8e971SJason A. Donenfeld 			memzero_explicit(&data->len, sizeof(data->len));
43068b8e971SJason A. Donenfeld 			early_memunmap(data, data_len);
43168b8e971SJason A. Donenfeld 			break;
432257b0fdeSYinghai Lu 		default:
433257b0fdeSYinghai Lu 			break;
434257b0fdeSYinghai Lu 		}
43530e46b57SLinn Crosetto 		pa_data = pa_next;
436257b0fdeSYinghai Lu 	}
437257b0fdeSYinghai Lu }
438257b0fdeSYinghai Lu 
439a9ce6bc1SYinghai Lu static void __init memblock_x86_reserve_range_setup_data(void)
440a0a0becdSYinghai Lu {
4417228918bSRoss Philipson 	struct setup_indirect *indirect;
442a0a0becdSYinghai Lu 	struct setup_data *data;
4437228918bSRoss Philipson 	u64 pa_data, pa_next;
4447228918bSRoss Philipson 	u32 len;
445a0a0becdSYinghai Lu 
446a0a0becdSYinghai Lu 	pa_data = boot_params.hdr.setup_data;
447a0a0becdSYinghai Lu 	while (pa_data) {
44888b4c146SJeremy Fitzhardinge 		data = early_memremap(pa_data, sizeof(*data));
4497228918bSRoss Philipson 		if (!data) {
4507228918bSRoss Philipson 			pr_warn("setup: failed to memremap setup_data entry\n");
4517228918bSRoss Philipson 			return;
4527228918bSRoss Philipson 		}
4537228918bSRoss Philipson 
4547228918bSRoss Philipson 		len = sizeof(*data);
4557228918bSRoss Philipson 		pa_next = data->next;
4567228918bSRoss Philipson 
45724aa0788STejun Heo 		memblock_reserve(pa_data, sizeof(*data) + data->len);
458b3c72fc9SDaniel Kiper 
4597228918bSRoss Philipson 		if (data->type == SETUP_INDIRECT) {
4607228918bSRoss Philipson 			len += data->len;
4618d4a40bcSJuergen Gross 			early_memunmap(data, sizeof(*data));
4627228918bSRoss Philipson 			data = early_memremap(pa_data, len);
4637228918bSRoss Philipson 			if (!data) {
4647228918bSRoss Philipson 				pr_warn("setup: failed to memremap indirect setup_data\n");
4657228918bSRoss Philipson 				return;
4667228918bSRoss Philipson 			}
4677228918bSRoss Philipson 
4687228918bSRoss Philipson 			indirect = (struct setup_indirect *)data->data;
4697228918bSRoss Philipson 
4707228918bSRoss Philipson 			if (indirect->type != SETUP_INDIRECT)
4717228918bSRoss Philipson 				memblock_reserve(indirect->addr, indirect->len);
4727228918bSRoss Philipson 		}
4737228918bSRoss Philipson 
4747228918bSRoss Philipson 		pa_data = pa_next;
4757228918bSRoss Philipson 		early_memunmap(data, len);
476a0a0becdSYinghai Lu 	}
477a0a0becdSYinghai Lu }
478a0a0becdSYinghai Lu 
47955f26239SYinghai Lu /*
480ccb4defaSYinghai Lu  * --------- Crashkernel reservation ------------------------------
481ccb4defaSYinghai Lu  */
482ccb4defaSYinghai Lu 
483606134f7SBorislav Petkov /* 16M alignment for crash kernel regions */
4849ca5c8e6SDave Young #define CRASH_ALIGN		SZ_16M
485606134f7SBorislav Petkov 
4867f8595bfSH. Peter Anvin /*
4878ff80fbeSBaoquan He  * Keep the crash kernel below this limit.
4888ff80fbeSBaoquan He  *
489360db4acSIngo Molnar  * Earlier 32-bits kernels would limit the kernel to the low 512 MB range
4908ff80fbeSBaoquan He  * due to mapping restrictions.
4918ff80fbeSBaoquan He  *
492360db4acSIngo Molnar  * 64-bit kdump kernels need to be restricted to be under 64 TB, which is
49311a98f37SCao jin  * the upper limit of system RAM in 4-level paging mode. Since the kdump
494360db4acSIngo Molnar  * jump could be from 5-level paging to 4-level paging, the jump will fail if
495360db4acSIngo Molnar  * the kernel is put above 64 TB, and during the 1st kernel bootup there's
496360db4acSIngo Molnar  * no good way to detect the paging mode of the target kernel which will be
497360db4acSIngo Molnar  * loaded for dumping.
4987f8595bfSH. Peter Anvin  */
4997f8595bfSH. Peter Anvin #ifdef CONFIG_X86_32
5009ca5c8e6SDave Young # define CRASH_ADDR_LOW_MAX	SZ_512M
5019ca5c8e6SDave Young # define CRASH_ADDR_HIGH_MAX	SZ_512M
5027f8595bfSH. Peter Anvin #else
5039ca5c8e6SDave Young # define CRASH_ADDR_LOW_MAX	SZ_4G
5048ff80fbeSBaoquan He # define CRASH_ADDR_HIGH_MAX	SZ_64T
5057f8595bfSH. Peter Anvin #endif
5067f8595bfSH. Peter Anvin 
507eb6db83dSBaoquan He static int __init reserve_crashkernel_low(void)
5080212f915SYinghai Lu {
5090212f915SYinghai Lu #ifdef CONFIG_X86_64
510f56d5578SBorislav Petkov 	unsigned long long base, low_base = 0, low_size = 0;
5116120cdc0SMike Rapoport 	unsigned long low_mem_limit;
5120212f915SYinghai Lu 	int ret;
5130212f915SYinghai Lu 
5146120cdc0SMike Rapoport 	low_mem_limit = min(memblock_phys_mem_size(), CRASH_ADDR_LOW_MAX);
51597eac21bSBorislav Petkov 
516adbc742bSYinghai Lu 	/* crashkernel=Y,low */
5176120cdc0SMike Rapoport 	ret = parse_crashkernel_low(boot_command_line, low_mem_limit, &low_size, &base);
518f56d5578SBorislav Petkov 	if (ret) {
519c729de8fSYinghai Lu 		/*
520392e879aSAndy Shevchenko 		 * two parts from kernel/dma/swiotlb.c:
52194fb9334SJoerg Roedel 		 * -swiotlb size: user-specified with swiotlb= or default.
52294fb9334SJoerg Roedel 		 *
52394fb9334SJoerg Roedel 		 * -swiotlb overflow buffer: now hardcoded to 32k. We round it
52494fb9334SJoerg Roedel 		 * to 8M for other buffers that may need to stay low too. Also
52594fb9334SJoerg Roedel 		 * make sure we allocate enough extra low memory so that we
52694fb9334SJoerg Roedel 		 * don't run out of DMA buffers for 32-bit devices.
527c729de8fSYinghai Lu 		 */
52894fb9334SJoerg Roedel 		low_size = max(swiotlb_size_or_default() + (8UL << 20), 256UL << 20);
529c729de8fSYinghai Lu 	} else {
530adbc742bSYinghai Lu 		/* passed with crashkernel=0,low ? */
531c729de8fSYinghai Lu 		if (!low_size)
532eb6db83dSBaoquan He 			return 0;
533c729de8fSYinghai Lu 	}
5340212f915SYinghai Lu 
5356120cdc0SMike Rapoport 	low_base = memblock_phys_alloc_range(low_size, CRASH_ALIGN, 0, CRASH_ADDR_LOW_MAX);
5360212f915SYinghai Lu 	if (!low_base) {
537eb6db83dSBaoquan He 		pr_err("Cannot reserve %ldMB crashkernel low memory, please try smaller size.\n",
538eb6db83dSBaoquan He 		       (unsigned long)(low_size >> 20));
539eb6db83dSBaoquan He 		return -ENOMEM;
5400212f915SYinghai Lu 	}
5410212f915SYinghai Lu 
5426120cdc0SMike Rapoport 	pr_info("Reserving %ldMB of low memory at %ldMB for crashkernel (low RAM limit: %ldMB)\n",
5430212f915SYinghai Lu 		(unsigned long)(low_size >> 20),
5440212f915SYinghai Lu 		(unsigned long)(low_base >> 20),
5456120cdc0SMike Rapoport 		(unsigned long)(low_mem_limit >> 20));
54697eac21bSBorislav Petkov 
5470212f915SYinghai Lu 	crashk_low_res.start = low_base;
5480212f915SYinghai Lu 	crashk_low_res.end   = low_base + low_size - 1;
5490212f915SYinghai Lu 	insert_resource(&iomem_resource, &crashk_low_res);
5500212f915SYinghai Lu #endif
551eb6db83dSBaoquan He 	return 0;
5520212f915SYinghai Lu }
5530212f915SYinghai Lu 
55429f784e3SYinghai Lu static void __init reserve_crashkernel(void)
555ccb4defaSYinghai Lu {
556f56d5578SBorislav Petkov 	unsigned long long crash_size, crash_base, total_mem;
55755a20ee7SYinghai Lu 	bool high = false;
558ccb4defaSYinghai Lu 	int ret;
559ccb4defaSYinghai Lu 
5604ece09beSJisheng Zhang 	if (!IS_ENABLED(CONFIG_KEXEC_CORE))
5614ece09beSJisheng Zhang 		return;
5624ece09beSJisheng Zhang 
56309c71bfdSDave Young 	total_mem = memblock_phys_mem_size();
564ccb4defaSYinghai Lu 
56555a20ee7SYinghai Lu 	/* crashkernel=XM */
56697eac21bSBorislav Petkov 	ret = parse_crashkernel(boot_command_line, total_mem, &crash_size, &crash_base);
56755a20ee7SYinghai Lu 	if (ret != 0 || crash_size <= 0) {
568adbc742bSYinghai Lu 		/* crashkernel=X,high */
56955a20ee7SYinghai Lu 		ret = parse_crashkernel_high(boot_command_line, total_mem,
57055a20ee7SYinghai Lu 					     &crash_size, &crash_base);
57132105f7fSBernhard Walle 		if (ret != 0 || crash_size <= 0)
57232105f7fSBernhard Walle 			return;
57355a20ee7SYinghai Lu 		high = true;
57455a20ee7SYinghai Lu 	}
57532105f7fSBernhard Walle 
5763db3eb28SPetr Tesarik 	if (xen_pv_domain()) {
5773db3eb28SPetr Tesarik 		pr_info("Ignoring crashkernel for a Xen PV domain\n");
5783db3eb28SPetr Tesarik 		return;
5793db3eb28SPetr Tesarik 	}
5803db3eb28SPetr Tesarik 
58132105f7fSBernhard Walle 	/* 0 means: find the address automatically */
582b9ac3849SDave Young 	if (!crash_base) {
5839f4c1396SYinghai Lu 		/*
584a8d4c824SXunlei Pang 		 * Set CRASH_ADDR_LOW_MAX upper bound for crash memory,
585b9ac3849SDave Young 		 * crashkernel=x,high reserves memory over 4G, also allocates
586b9ac3849SDave Young 		 * 256M extra low memory for DMA buffers and swiotlb.
587b9ac3849SDave Young 		 * But the extra memory is not required for all machines.
588b9ac3849SDave Young 		 * So try low memory first and fall back to high memory
589b9ac3849SDave Young 		 * unless "crashkernel=size[KMG],high" is specified.
5909f4c1396SYinghai Lu 		 */
591b9ac3849SDave Young 		if (!high)
5926120cdc0SMike Rapoport 			crash_base = memblock_phys_alloc_range(crash_size,
5936120cdc0SMike Rapoport 						CRASH_ALIGN, CRASH_ALIGN,
5946120cdc0SMike Rapoport 						CRASH_ADDR_LOW_MAX);
595b9ac3849SDave Young 		if (!crash_base)
5966120cdc0SMike Rapoport 			crash_base = memblock_phys_alloc_range(crash_size,
5976120cdc0SMike Rapoport 						CRASH_ALIGN, CRASH_ALIGN,
5986120cdc0SMike Rapoport 						CRASH_ADDR_HIGH_MAX);
5991f5026a7STejun Heo 		if (!crash_base) {
60044280733SYinghai Lu 			pr_info("crashkernel reservation failed - No suitable area found.\n");
601ccb4defaSYinghai Lu 			return;
602ccb4defaSYinghai Lu 		}
60332105f7fSBernhard Walle 	} else {
60444280733SYinghai Lu 		unsigned long long start;
60544280733SYinghai Lu 
6066120cdc0SMike Rapoport 		start = memblock_phys_alloc_range(crash_size, SZ_1M, crash_base,
6076120cdc0SMike Rapoport 						  crash_base + crash_size);
60844280733SYinghai Lu 		if (start != crash_base) {
60944280733SYinghai Lu 			pr_info("crashkernel reservation failed - memory is in use.\n");
610ccb4defaSYinghai Lu 			return;
611ccb4defaSYinghai Lu 		}
61232105f7fSBernhard Walle 	}
613ccb4defaSYinghai Lu 
614eb6db83dSBaoquan He 	if (crash_base >= (1ULL << 32) && reserve_crashkernel_low()) {
6153ecc6834SMike Rapoport 		memblock_phys_free(crash_base, crash_size);
616eb6db83dSBaoquan He 		return;
617eb6db83dSBaoquan He 	}
618eb6db83dSBaoquan He 
619f56d5578SBorislav Petkov 	pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
620ccb4defaSYinghai Lu 		(unsigned long)(crash_size >> 20),
621ccb4defaSYinghai Lu 		(unsigned long)(crash_base >> 20),
622ccb4defaSYinghai Lu 		(unsigned long)(total_mem >> 20));
623ccb4defaSYinghai Lu 
624ccb4defaSYinghai Lu 	crashk_res.start = crash_base;
625ccb4defaSYinghai Lu 	crashk_res.end   = crash_base + crash_size - 1;
626ccb4defaSYinghai Lu 	insert_resource(&iomem_resource, &crashk_res);
627ccb4defaSYinghai Lu }
628ccb4defaSYinghai Lu 
629bdba0e70SYinghai Lu static struct resource standard_io_resources[] = {
630bdba0e70SYinghai Lu 	{ .name = "dma1", .start = 0x00, .end = 0x1f,
631bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
632bdba0e70SYinghai Lu 	{ .name = "pic1", .start = 0x20, .end = 0x21,
633bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
634bdba0e70SYinghai Lu 	{ .name = "timer0", .start = 0x40, .end = 0x43,
635bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
636bdba0e70SYinghai Lu 	{ .name = "timer1", .start = 0x50, .end = 0x53,
637bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
638bdba0e70SYinghai Lu 	{ .name = "keyboard", .start = 0x60, .end = 0x60,
639bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
640bdba0e70SYinghai Lu 	{ .name = "keyboard", .start = 0x64, .end = 0x64,
641bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
642bdba0e70SYinghai Lu 	{ .name = "dma page reg", .start = 0x80, .end = 0x8f,
643bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
644bdba0e70SYinghai Lu 	{ .name = "pic2", .start = 0xa0, .end = 0xa1,
645bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
646bdba0e70SYinghai Lu 	{ .name = "dma2", .start = 0xc0, .end = 0xdf,
647bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO },
648bdba0e70SYinghai Lu 	{ .name = "fpu", .start = 0xf0, .end = 0xff,
649bdba0e70SYinghai Lu 		.flags = IORESOURCE_BUSY | IORESOURCE_IO }
650bdba0e70SYinghai Lu };
651bdba0e70SYinghai Lu 
6528fee697dSThomas Gleixner void __init reserve_standard_io_resources(void)
653bdba0e70SYinghai Lu {
654bdba0e70SYinghai Lu 	int i;
655bdba0e70SYinghai Lu 
656bdba0e70SYinghai Lu 	/* request I/O space for devices used on all i[345]86 PCs */
657bdba0e70SYinghai Lu 	for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
658bdba0e70SYinghai Lu 		request_resource(&ioport_resource, &standard_io_resources[i]);
659bdba0e70SYinghai Lu 
660bdba0e70SYinghai Lu }
661bdba0e70SYinghai Lu 
662a9acc536SJesse Barnes static bool __init snb_gfx_workaround_needed(void)
663a9acc536SJesse Barnes {
664e43b3cecSH. Peter Anvin #ifdef CONFIG_PCI
665a9acc536SJesse Barnes 	int i;
666a9acc536SJesse Barnes 	u16 vendor, devid;
667ab3cd867SH. Peter Anvin 	static const __initconst u16 snb_ids[] = {
668a9acc536SJesse Barnes 		0x0102,
669a9acc536SJesse Barnes 		0x0112,
670a9acc536SJesse Barnes 		0x0122,
671a9acc536SJesse Barnes 		0x0106,
672a9acc536SJesse Barnes 		0x0116,
673a9acc536SJesse Barnes 		0x0126,
674a9acc536SJesse Barnes 		0x010a,
675a9acc536SJesse Barnes 	};
676a9acc536SJesse Barnes 
677a9acc536SJesse Barnes 	/* Assume no if something weird is going on with PCI */
678a9acc536SJesse Barnes 	if (!early_pci_allowed())
679a9acc536SJesse Barnes 		return false;
680a9acc536SJesse Barnes 
681a9acc536SJesse Barnes 	vendor = read_pci_config_16(0, 2, 0, PCI_VENDOR_ID);
682a9acc536SJesse Barnes 	if (vendor != 0x8086)
683a9acc536SJesse Barnes 		return false;
684a9acc536SJesse Barnes 
685a9acc536SJesse Barnes 	devid = read_pci_config_16(0, 2, 0, PCI_DEVICE_ID);
686a9acc536SJesse Barnes 	for (i = 0; i < ARRAY_SIZE(snb_ids); i++)
687a9acc536SJesse Barnes 		if (devid == snb_ids[i])
688a9acc536SJesse Barnes 			return true;
689e43b3cecSH. Peter Anvin #endif
690a9acc536SJesse Barnes 
691a9acc536SJesse Barnes 	return false;
692a9acc536SJesse Barnes }
693a9acc536SJesse Barnes 
694a9acc536SJesse Barnes /*
695a9acc536SJesse Barnes  * Sandy Bridge graphics has trouble with certain ranges, exclude
696a9acc536SJesse Barnes  * them from allocation.
697a9acc536SJesse Barnes  */
698a9acc536SJesse Barnes static void __init trim_snb_memory(void)
699a9acc536SJesse Barnes {
700ab3cd867SH. Peter Anvin 	static const __initconst unsigned long bad_pages[] = {
701a9acc536SJesse Barnes 		0x20050000,
702a9acc536SJesse Barnes 		0x20110000,
703a9acc536SJesse Barnes 		0x20130000,
704a9acc536SJesse Barnes 		0x20138000,
705a9acc536SJesse Barnes 		0x40004000,
706a9acc536SJesse Barnes 	};
707a9acc536SJesse Barnes 	int i;
708a9acc536SJesse Barnes 
709a9acc536SJesse Barnes 	if (!snb_gfx_workaround_needed())
710a9acc536SJesse Barnes 		return;
711a9acc536SJesse Barnes 
712a9acc536SJesse Barnes 	printk(KERN_DEBUG "reserving inaccessible SNB gfx pages\n");
713a9acc536SJesse Barnes 
714a9acc536SJesse Barnes 	/*
715c361e5d4SMike Rapoport 	 * SandyBridge integrated graphics devices have a bug that prevents
716c361e5d4SMike Rapoport 	 * them from accessing certain memory ranges, namely anything below
717c361e5d4SMike Rapoport 	 * 1M and in the pages listed in bad_pages[] above.
718c361e5d4SMike Rapoport 	 *
719f1d4d47cSMike Rapoport 	 * To avoid these pages being ever accessed by SNB gfx devices reserve
720f1d4d47cSMike Rapoport 	 * bad_pages that have not already been reserved at boot time.
721f1d4d47cSMike Rapoport 	 * All memory below the 1 MB mark is anyway reserved later during
722f1d4d47cSMike Rapoport 	 * setup_arch(), so there is no need to reserve it here.
723a9acc536SJesse Barnes 	 */
724a9acc536SJesse Barnes 
725a9acc536SJesse Barnes 	for (i = 0; i < ARRAY_SIZE(bad_pages); i++) {
726a9acc536SJesse Barnes 		if (memblock_reserve(bad_pages[i], PAGE_SIZE))
727a9acc536SJesse Barnes 			printk(KERN_WARNING "failed to reserve 0x%08lx\n",
728a9acc536SJesse Barnes 			       bad_pages[i]);
729a9acc536SJesse Barnes 	}
730a9acc536SJesse Barnes }
731a9acc536SJesse Barnes 
7321b5576e6SYinghai Lu static void __init trim_bios_range(void)
7331b5576e6SYinghai Lu {
7341b5576e6SYinghai Lu 	/*
7355c279c4cSMike Rapoport 	 * A special case is the first 4Kb of memory;
7365c279c4cSMike Rapoport 	 * This is a BIOS owned area, not kernel ram, but generally
7375c279c4cSMike Rapoport 	 * not listed as such in the E820 table.
7385c279c4cSMike Rapoport 	 *
7395c279c4cSMike Rapoport 	 * This typically reserves additional memory (64KiB by default)
7405c279c4cSMike Rapoport 	 * since some BIOSes are known to corrupt low memory.  See the
7415c279c4cSMike Rapoport 	 * Kconfig help text for X86_RESERVE_LOW.
7425c279c4cSMike Rapoport 	 */
7435c279c4cSMike Rapoport 	e820__range_update(0, PAGE_SIZE, E820_TYPE_RAM, E820_TYPE_RESERVED);
7445c279c4cSMike Rapoport 
7455c279c4cSMike Rapoport 	/*
74611a98f37SCao jin 	 * special case: Some BIOSes report the PC BIOS
74711a98f37SCao jin 	 * area (640Kb -> 1Mb) as RAM even though it is not.
7481b5576e6SYinghai Lu 	 * take them out.
7491b5576e6SYinghai Lu 	 */
75009821ff1SIngo Molnar 	e820__range_remove(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_TYPE_RAM, 1);
751a9acc536SJesse Barnes 
752f9748fa0SIngo Molnar 	e820__update_table(e820_table);
7531b5576e6SYinghai Lu }
7541b5576e6SYinghai Lu 
755b422a309SYinghai Lu /* called before trim_bios_range() to spare extra sanitize */
756b422a309SYinghai Lu static void __init e820_add_kernel_range(void)
757b422a309SYinghai Lu {
758b422a309SYinghai Lu 	u64 start = __pa_symbol(_text);
759b422a309SYinghai Lu 	u64 size = __pa_symbol(_end) - start;
760b422a309SYinghai Lu 
761b422a309SYinghai Lu 	/*
76209821ff1SIngo Molnar 	 * Complain if .text .data and .bss are not marked as E820_TYPE_RAM and
763b422a309SYinghai Lu 	 * attempt to fix it by adding the range. We may have a confused BIOS,
764b422a309SYinghai Lu 	 * or the user may have used memmap=exactmap or memmap=xxM$yyM to
765b422a309SYinghai Lu 	 * exclude kernel range. If we really are running on top non-RAM,
766b422a309SYinghai Lu 	 * we will crash later anyways.
767b422a309SYinghai Lu 	 */
76809821ff1SIngo Molnar 	if (e820__mapped_all(start, start + size, E820_TYPE_RAM))
769b422a309SYinghai Lu 		return;
770b422a309SYinghai Lu 
77109821ff1SIngo Molnar 	pr_warn(".text .data .bss are not marked as E820_TYPE_RAM!\n");
77209821ff1SIngo Molnar 	e820__range_remove(start, size, E820_TYPE_RAM, 0);
77309821ff1SIngo Molnar 	e820__range_add(start, size, E820_TYPE_RAM);
774b422a309SYinghai Lu }
775b422a309SYinghai Lu 
776a799c2bdSMike Rapoport static void __init early_reserve_memory(void)
77795c96084SH. Peter Anvin {
778a799c2bdSMike Rapoport 	/*
779a799c2bdSMike Rapoport 	 * Reserve the memory occupied by the kernel between _text and
780a799c2bdSMike Rapoport 	 * __end_of_kernel_reserve symbols. Any kernel sections after the
781a799c2bdSMike Rapoport 	 * __end_of_kernel_reserve symbol must be explicitly reserved with a
782a799c2bdSMike Rapoport 	 * separate memblock_reserve() or they will be discarded.
783a799c2bdSMike Rapoport 	 */
784a799c2bdSMike Rapoport 	memblock_reserve(__pa_symbol(_text),
785a799c2bdSMike Rapoport 			 (unsigned long)__end_of_kernel_reserve - (unsigned long)_text);
786a799c2bdSMike Rapoport 
787a799c2bdSMike Rapoport 	/*
7884c674481SMike Rapoport 	 * The first 4Kb of memory is a BIOS owned area, but generally it is
7894c674481SMike Rapoport 	 * not listed as such in the E820 table.
7904c674481SMike Rapoport 	 *
791f1d4d47cSMike Rapoport 	 * Reserve the first 64K of memory since some BIOSes are known to
792f1d4d47cSMike Rapoport 	 * corrupt low memory. After the real mode trampoline is allocated the
793f1d4d47cSMike Rapoport 	 * rest of the memory below 640k is reserved.
7944c674481SMike Rapoport 	 *
7954c674481SMike Rapoport 	 * In addition, make sure page 0 is always reserved because on
7964c674481SMike Rapoport 	 * systems with L1TF its contents can be leaked to user processes.
797a799c2bdSMike Rapoport 	 */
798f1d4d47cSMike Rapoport 	memblock_reserve(0, SZ_64K);
799a799c2bdSMike Rapoport 
800a799c2bdSMike Rapoport 	early_reserve_initrd();
801a799c2bdSMike Rapoport 
802a799c2bdSMike Rapoport 	memblock_x86_reserve_range_setup_data();
803a799c2bdSMike Rapoport 
804a799c2bdSMike Rapoport 	reserve_ibft_region();
805a799c2bdSMike Rapoport 	reserve_bios_regions();
806f1d4d47cSMike Rapoport 	trim_snb_memory();
80795c96084SH. Peter Anvin }
80895c96084SH. Peter Anvin 
8095394f80fSJeremy Fitzhardinge /*
810f32360efSKees Cook  * Dump out kernel offset information on panic.
811f32360efSKees Cook  */
812f32360efSKees Cook static int
813f32360efSKees Cook dump_kernel_offset(struct notifier_block *self, unsigned long v, void *p)
814f32360efSKees Cook {
81578cac48cSBorislav Petkov 	if (kaslr_enabled()) {
81678cac48cSBorislav Petkov 		pr_emerg("Kernel Offset: 0x%lx from 0x%lx (relocation range: 0x%lx-0x%lx)\n",
8174545c898SJiri Kosina 			 kaslr_offset(),
81878cac48cSBorislav Petkov 			 __START_KERNEL,
81978cac48cSBorislav Petkov 			 __START_KERNEL_map,
82078cac48cSBorislav Petkov 			 MODULES_VADDR-1);
82178cac48cSBorislav Petkov 	} else {
82278cac48cSBorislav Petkov 		pr_emerg("Kernel Offset: disabled\n");
82378cac48cSBorislav Petkov 	}
824f32360efSKees Cook 
825f32360efSKees Cook 	return 0;
826f32360efSKees Cook }
827f32360efSKees Cook 
82876ea0025SBorislav Petkov void x86_configure_nx(void)
82976ea0025SBorislav Petkov {
83076ea0025SBorislav Petkov 	if (boot_cpu_has(X86_FEATURE_NX))
83176ea0025SBorislav Petkov 		__supported_pte_mask |= _PAGE_NX;
83276ea0025SBorislav Petkov 	else
83376ea0025SBorislav Petkov 		__supported_pte_mask &= ~_PAGE_NX;
83476ea0025SBorislav Petkov }
83576ea0025SBorislav Petkov 
83676ea0025SBorislav Petkov static void __init x86_report_nx(void)
83776ea0025SBorislav Petkov {
83876ea0025SBorislav Petkov 	if (!boot_cpu_has(X86_FEATURE_NX)) {
83976ea0025SBorislav Petkov 		printk(KERN_NOTICE "Notice: NX (Execute Disable) protection "
84076ea0025SBorislav Petkov 		       "missing in CPU!\n");
84176ea0025SBorislav Petkov 	} else {
84276ea0025SBorislav Petkov #if defined(CONFIG_X86_64) || defined(CONFIG_X86_PAE)
84376ea0025SBorislav Petkov 		printk(KERN_INFO "NX (Execute Disable) protection: active\n");
84476ea0025SBorislav Petkov #else
84576ea0025SBorislav Petkov 		/* 32bit non-PAE kernel, NX cannot be used */
84676ea0025SBorislav Petkov 		printk(KERN_NOTICE "Notice: NX (Execute Disable) protection "
84776ea0025SBorislav Petkov 		       "cannot be enabled: non-PAE kernel!\n");
84876ea0025SBorislav Petkov #endif
84976ea0025SBorislav Petkov 	}
85076ea0025SBorislav Petkov }
85176ea0025SBorislav Petkov 
852f32360efSKees Cook /*
85355f26239SYinghai Lu  * Determine if we were loaded by an EFI loader.  If so, then we have also been
85455f26239SYinghai Lu  * passed the efi memmap, systab, etc., so we should use these data structures
85555f26239SYinghai Lu  * for initialization.  Note, the efi init code path is determined by the
85655f26239SYinghai Lu  * global efi_enabled. This allows the same kernel image to be used on existing
85755f26239SYinghai Lu  * systems (with a traditional BIOS) as well as on EFI systems.
85855f26239SYinghai Lu  */
85955f26239SYinghai Lu /*
86055f26239SYinghai Lu  * setup_arch - architecture-specific boot-time initializations
86155f26239SYinghai Lu  *
86255f26239SYinghai Lu  * Note: On x86_64, fixmaps are ready for use even before this is called.
86355f26239SYinghai Lu  */
86455f26239SYinghai Lu 
86555f26239SYinghai Lu void __init setup_arch(char **cmdline_p)
86655f26239SYinghai Lu {
86755f26239SYinghai Lu #ifdef CONFIG_X86_32
86855f26239SYinghai Lu 	memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
869b40827faSBorislav Petkov 
870b40827faSBorislav Petkov 	/*
871b40827faSBorislav Petkov 	 * copy kernel address range established so far and switch
872b40827faSBorislav Petkov 	 * to the proper swapper page table
873b40827faSBorislav Petkov 	 */
874b40827faSBorislav Petkov 	clone_pgd_range(swapper_pg_dir     + KERNEL_PGD_BOUNDARY,
875b40827faSBorislav Petkov 			initial_page_table + KERNEL_PGD_BOUNDARY,
876b40827faSBorislav Petkov 			KERNEL_PGD_PTRS);
877b40827faSBorislav Petkov 
878b40827faSBorislav Petkov 	load_cr3(swapper_pg_dir);
8792075244fSBryan O'Donoghue 	/*
8802075244fSBryan O'Donoghue 	 * Note: Quark X1000 CPUs advertise PGE incorrectly and require
8812075244fSBryan O'Donoghue 	 * a cr3 based tlb flush, so the following __flush_tlb_all()
882360db4acSIngo Molnar 	 * will not flush anything because the CPU quirk which clears
8832075244fSBryan O'Donoghue 	 * X86_FEATURE_PGE has not been invoked yet. Though due to the
8842075244fSBryan O'Donoghue 	 * load_cr3() above the TLB has been flushed already. The
8852075244fSBryan O'Donoghue 	 * quirk is invoked before subsequent calls to __flush_tlb_all()
8862075244fSBryan O'Donoghue 	 * so proper operation is guaranteed.
8872075244fSBryan O'Donoghue 	 */
888b40827faSBorislav Petkov 	__flush_tlb_all();
88955f26239SYinghai Lu #else
89055f26239SYinghai Lu 	printk(KERN_INFO "Command line: %s\n", boot_command_line);
891162434e7SKirill A. Shutemov 	boot_cpu_data.x86_phys_bits = MAX_PHYSMEM_BITS;
89255f26239SYinghai Lu #endif
89355f26239SYinghai Lu 
8949863c90fSAlok Kataria 	/*
8959863c90fSAlok Kataria 	 * If we have OLPC OFW, we might end up relocating the fixmap due to
8969863c90fSAlok Kataria 	 * reserve_top(), so do this before touching the ioremap area.
8979863c90fSAlok Kataria 	 */
898fd699c76SAndres Salomon 	olpc_ofw_detect();
899fd699c76SAndres Salomon 
900433f8924SThomas Gleixner 	idt_setup_early_traps();
9019e882c92SJeremy Fitzhardinge 	early_cpu_init();
9028990cac6SPavel Tatashin 	jump_label_init();
903a945c834SPeter Zijlstra 	static_call_init();
9041a98fd14SJeremy Fitzhardinge 	early_ioremap_init();
9051a98fd14SJeremy Fitzhardinge 
906fd699c76SAndres Salomon 	setup_olpc_ofw_pgd();
907fd699c76SAndres Salomon 
90855f26239SYinghai Lu 	ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
90955f26239SYinghai Lu 	screen_info = boot_params.screen_info;
91055f26239SYinghai Lu 	edid_info = boot_params.edid_info;
91155f26239SYinghai Lu #ifdef CONFIG_X86_32
91255f26239SYinghai Lu 	apm_info.bios = boot_params.apm_bios_info;
91355f26239SYinghai Lu 	ist_info = boot_params.ist_info;
91455f26239SYinghai Lu #endif
91555f26239SYinghai Lu 	saved_video_mode = boot_params.hdr.vid_mode;
91655f26239SYinghai Lu 	bootloader_type = boot_params.hdr.type_of_loader;
9175031296cSH. Peter Anvin 	if ((bootloader_type >> 4) == 0xe) {
9185031296cSH. Peter Anvin 		bootloader_type &= 0xf;
9195031296cSH. Peter Anvin 		bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4;
9205031296cSH. Peter Anvin 	}
9215031296cSH. Peter Anvin 	bootloader_version  = bootloader_type & 0xf;
9225031296cSH. Peter Anvin 	bootloader_version |= boot_params.hdr.ext_loader_ver << 4;
92355f26239SYinghai Lu 
92455f26239SYinghai Lu #ifdef CONFIG_BLK_DEV_RAM
92555f26239SYinghai Lu 	rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
92655f26239SYinghai Lu #endif
92755f26239SYinghai Lu #ifdef CONFIG_EFI
92855f26239SYinghai Lu 	if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
9299402973dSDaniel Kiper 		     EFI32_LOADER_SIGNATURE, 4)) {
9303e909599SMatt Fleming 		set_bit(EFI_BOOT, &efi.flags);
9311adbfa35SOlof Johansson 	} else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
9329402973dSDaniel Kiper 		     EFI64_LOADER_SIGNATURE, 4)) {
9333e909599SMatt Fleming 		set_bit(EFI_BOOT, &efi.flags);
9343e909599SMatt Fleming 		set_bit(EFI_64BIT, &efi.flags);
93555f26239SYinghai Lu 	}
93655f26239SYinghai Lu #endif
93755f26239SYinghai Lu 
93842bbdb43SThomas Gleixner 	x86_init.oem.arch_setup();
93955f26239SYinghai Lu 
9408aa83e63SJuergen Gross 	/*
9418aa83e63SJuergen Gross 	 * Do some memory reservations *before* memory is added to memblock, so
9428aa83e63SJuergen Gross 	 * memblock allocations won't overwrite it.
9438aa83e63SJuergen Gross 	 *
9448aa83e63SJuergen Gross 	 * After this point, everything still needed from the boot loader or
9458aa83e63SJuergen Gross 	 * firmware or kernel text should be early reserved or marked not RAM in
9468aa83e63SJuergen Gross 	 * e820. All other memory is free game.
9478aa83e63SJuergen Gross 	 *
9488aa83e63SJuergen Gross 	 * This call needs to happen before e820__memory_setup() which calls the
9498aa83e63SJuergen Gross 	 * xen_memory_setup() on Xen dom0 which relies on the fact that those
9508aa83e63SJuergen Gross 	 * early reservations have happened already.
9518aa83e63SJuergen Gross 	 */
9528aa83e63SJuergen Gross 	early_reserve_memory();
9538aa83e63SJuergen Gross 
954419afdf5SBjorn Helgaas 	iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
955103e2063SIngo Molnar 	e820__memory_setup();
95628bb2237SYinghai Lu 	parse_setup_data();
95728bb2237SYinghai Lu 
95855f26239SYinghai Lu 	copy_edd();
95955f26239SYinghai Lu 
96055f26239SYinghai Lu 	if (!boot_params.hdr.root_flags)
96155f26239SYinghai Lu 		root_mountflags &= ~MS_RDONLY;
96230120d72SKefeng Wang 	setup_initial_init_mm(_text, _etext, _edata, (void *)_brk_end);
96355f26239SYinghai Lu 
9644046d6e8SLinus Torvalds 	code_resource.start = __pa_symbol(_text);
9654046d6e8SLinus Torvalds 	code_resource.end = __pa_symbol(_etext)-1;
966a3299754SKees Cook 	rodata_resource.start = __pa_symbol(__start_rodata);
967a3299754SKees Cook 	rodata_resource.end = __pa_symbol(__end_rodata)-1;
968a3299754SKees Cook 	data_resource.start = __pa_symbol(_sdata);
9694046d6e8SLinus Torvalds 	data_resource.end = __pa_symbol(_edata)-1;
9704046d6e8SLinus Torvalds 	bss_resource.start = __pa_symbol(__bss_start);
9714046d6e8SLinus Torvalds 	bss_resource.end = __pa_symbol(__bss_stop)-1;
9724046d6e8SLinus Torvalds 
973fbe61839SBorislav Petkov #ifdef CONFIG_CMDLINE_BOOL
974fbe61839SBorislav Petkov #ifdef CONFIG_CMDLINE_OVERRIDE
9758a33d96bSXueBing Chen 	strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
976fbe61839SBorislav Petkov #else
977fbe61839SBorislav Petkov 	if (builtin_cmdline[0]) {
978fbe61839SBorislav Petkov 		/* append boot loader cmdline to builtin */
979fbe61839SBorislav Petkov 		strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
980fbe61839SBorislav Petkov 		strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
9818a33d96bSXueBing Chen 		strscpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
982fbe61839SBorislav Petkov 	}
983fbe61839SBorislav Petkov #endif
984fbe61839SBorislav Petkov #endif
985fbe61839SBorislav Petkov 
9868a33d96bSXueBing Chen 	strscpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
987fbe61839SBorislav Petkov 	*cmdline_p = command_line;
988fbe61839SBorislav Petkov 
989fbe61839SBorislav Petkov 	/*
990fbe61839SBorislav Petkov 	 * x86_configure_nx() is called before parse_early_param() to detect
991fbe61839SBorislav Petkov 	 * whether hardware doesn't support NX (so that the early EHCI debug
99276ea0025SBorislav Petkov 	 * console setup can safely call set_fixmap()).
993fbe61839SBorislav Petkov 	 */
994fbe61839SBorislav Petkov 	x86_configure_nx();
995fbe61839SBorislav Petkov 
996fbe61839SBorislav Petkov 	parse_early_param();
997fbe61839SBorislav Petkov 
9982f5b3514SMike Rapoport 	if (efi_enabled(EFI_BOOT))
9992f5b3514SMike Rapoport 		efi_memblock_x86_reserve_range();
10002f5b3514SMike Rapoport 
100139fa104dSReza Arbab #ifdef CONFIG_MEMORY_HOTPLUG
100239fa104dSReza Arbab 	/*
100339fa104dSReza Arbab 	 * Memory used by the kernel cannot be hot-removed because Linux
100439fa104dSReza Arbab 	 * cannot migrate the kernel pages. When memory hotplug is
100539fa104dSReza Arbab 	 * enabled, we should prevent memblock from allocating memory
100639fa104dSReza Arbab 	 * for the kernel.
100739fa104dSReza Arbab 	 *
100839fa104dSReza Arbab 	 * ACPI SRAT records all hotpluggable memory ranges. But before
100939fa104dSReza Arbab 	 * SRAT is parsed, we don't know about it.
101039fa104dSReza Arbab 	 *
101139fa104dSReza Arbab 	 * The kernel image is loaded into memory at very early time. We
101239fa104dSReza Arbab 	 * cannot prevent this anyway. So on NUMA system, we set any
101339fa104dSReza Arbab 	 * node the kernel resides in as un-hotpluggable.
101439fa104dSReza Arbab 	 *
101539fa104dSReza Arbab 	 * Since on modern servers, one node could have double-digit
101639fa104dSReza Arbab 	 * gigabytes memory, we can assume the memory around the kernel
101739fa104dSReza Arbab 	 * image is also un-hotpluggable. So before SRAT is parsed, just
101839fa104dSReza Arbab 	 * allocate memory near the kernel image to try the best to keep
101939fa104dSReza Arbab 	 * the kernel away from hotpluggable memory.
102039fa104dSReza Arbab 	 */
102139fa104dSReza Arbab 	if (movable_node_is_enabled())
102239fa104dSReza Arbab 		memblock_set_bottom_up(true);
102339fa104dSReza Arbab #endif
102439fa104dSReza Arbab 
10254b0f3b81SKees Cook 	x86_report_nx();
10260ad5bce7SJeremy Fitzhardinge 
102755f26239SYinghai Lu 	if (acpi_mps_check()) {
102855f26239SYinghai Lu #ifdef CONFIG_X86_LOCAL_APIC
102955f26239SYinghai Lu 		disable_apic = 1;
103055f26239SYinghai Lu #endif
1031988781dcSYinghai Lu 		setup_clear_cpu_cap(X86_FEATURE_APIC);
103255f26239SYinghai Lu 	}
103355f26239SYinghai Lu 
10341a127034SIngo Molnar 	e820__reserve_setup_data();
10359641bdafSIngo Molnar 	e820__finish_early_params();
103655f26239SYinghai Lu 
103783e68189SMatt Fleming 	if (efi_enabled(EFI_BOOT))
1038ff0c0874SBrian Maly 		efi_init();
1039ff0c0874SBrian Maly 
10400fca0812SRobert Richter 	dmi_setup();
10412216d199SYinghai Lu 
104288b094fbSAlok Kataria 	/*
104388b094fbSAlok Kataria 	 * VMware detection requires dmi to be available, so this
10440fca0812SRobert Richter 	 * needs to be done after dmi_setup(), for the boot CPU.
104588b094fbSAlok Kataria 	 */
10462d826404SThomas Gleixner 	init_hypervisor_platform();
104788b094fbSAlok Kataria 
1048cf7a63efSPavel Tatashin 	tsc_early_init();
1049f7cf5a5bSThomas Gleixner 	x86_init.resources.probe_roms();
105055f26239SYinghai Lu 
10514046d6e8SLinus Torvalds 	/* after parse_early_param, so could debug it */
10524046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &code_resource);
1053a3299754SKees Cook 	insert_resource(&iomem_resource, &rodata_resource);
10544046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &data_resource);
10554046d6e8SLinus Torvalds 	insert_resource(&iomem_resource, &bss_resource);
10564046d6e8SLinus Torvalds 
1057b422a309SYinghai Lu 	e820_add_kernel_range();
10581b5576e6SYinghai Lu 	trim_bios_range();
105955f26239SYinghai Lu #ifdef CONFIG_X86_32
106055f26239SYinghai Lu 	if (ppro_with_ram_bug()) {
106109821ff1SIngo Molnar 		e820__range_update(0x70000000ULL, 0x40000ULL, E820_TYPE_RAM,
106209821ff1SIngo Molnar 				  E820_TYPE_RESERVED);
1063f9748fa0SIngo Molnar 		e820__update_table(e820_table);
106455f26239SYinghai Lu 		printk(KERN_INFO "fixed physical RAM map:\n");
1065be0c3f0fSIngo Molnar 		e820__print_table("bad_ppro");
106655f26239SYinghai Lu 	}
106755f26239SYinghai Lu #else
106855f26239SYinghai Lu 	early_gart_iommu_check();
106955f26239SYinghai Lu #endif
107055f26239SYinghai Lu 
107155f26239SYinghai Lu 	/*
107255f26239SYinghai Lu 	 * partially used pages are not usable - thus
107355f26239SYinghai Lu 	 * we are rounding upwards:
107455f26239SYinghai Lu 	 */
10750c6fc11aSIngo Molnar 	max_pfn = e820__end_of_ram_pfn();
107655f26239SYinghai Lu 
107755f26239SYinghai Lu 	/* update e820 for memory not covered by WB MTRRs */
10784d5cff69SChristoph Hellwig 	if (IS_ENABLED(CONFIG_MTRR))
107955f26239SYinghai Lu 		mtrr_bp_init();
10804d5cff69SChristoph Hellwig 	else
10814d5cff69SChristoph Hellwig 		pat_disable("PAT support disabled because CONFIG_MTRR is disabled in the kernel.");
10824d5cff69SChristoph Hellwig 
10832dc807b3SYinghai Lu 	if (mtrr_trim_uncached_memory(max_pfn))
10840c6fc11aSIngo Molnar 		max_pfn = e820__end_of_ram_pfn();
108555f26239SYinghai Lu 
10868dd33030SIgor Mammedov 	max_possible_pfn = max_pfn;
10878dd33030SIgor Mammedov 
1088c7d2361fSThomas Garnier 	/*
108999c13b8cSMikulas Patocka 	 * This call is required when the CPU does not support PAT. If
109099c13b8cSMikulas Patocka 	 * mtrr_bp_init() invoked it already via pat_init() the call has no
109199c13b8cSMikulas Patocka 	 * effect.
109299c13b8cSMikulas Patocka 	 */
109399c13b8cSMikulas Patocka 	init_cache_modes();
109499c13b8cSMikulas Patocka 
109599c13b8cSMikulas Patocka 	/*
1096c7d2361fSThomas Garnier 	 * Define random base addresses for memory sections after max_pfn is
1097c7d2361fSThomas Garnier 	 * defined and before each memory section base is used.
1098c7d2361fSThomas Garnier 	 */
1099c7d2361fSThomas Garnier 	kernel_randomize_memory();
1100c7d2361fSThomas Garnier 
110155f26239SYinghai Lu #ifdef CONFIG_X86_32
110255f26239SYinghai Lu 	/* max_low_pfn get updated here */
110355f26239SYinghai Lu 	find_low_pfn_range();
110455f26239SYinghai Lu #else
11056e1cb38aSSuresh Siddha 	check_x2apic();
110655f26239SYinghai Lu 
110755f26239SYinghai Lu 	/* How many end-of-memory variables you have, grandma! */
110855f26239SYinghai Lu 	/* need this before calling reserve_initrd */
1109f361a450SYinghai Lu 	if (max_pfn > (1UL<<(32 - PAGE_SHIFT)))
11100c6fc11aSIngo Molnar 		max_low_pfn = e820__end_of_low_ram_pfn();
1111f361a450SYinghai Lu 	else
111255f26239SYinghai Lu 		max_low_pfn = max_pfn;
1113f361a450SYinghai Lu 
111455f26239SYinghai Lu 	high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
111555f26239SYinghai Lu #endif
111655f26239SYinghai Lu 
1117893f38d1SYinghai Lu 	/*
1118893f38d1SYinghai Lu 	 * Find and reserve possible boot-time SMP configuration:
1119893f38d1SYinghai Lu 	 */
1120893f38d1SYinghai Lu 	find_smp_config();
1121893f38d1SYinghai Lu 
11228d57470dSYinghai Lu 	early_alloc_pgt_buf();
11238d57470dSYinghai Lu 
112472d7c3b3SYinghai Lu 	/*
11254918e228SIngo Molnar 	 * Need to conclude brk, before e820__memblock_setup()
112672d7c3b3SYinghai Lu 	 * it could use memblock_find_in_range, could overlap with
112772d7c3b3SYinghai Lu 	 * brk area.
112872d7c3b3SYinghai Lu 	 */
112972d7c3b3SYinghai Lu 	reserve_brk();
113072d7c3b3SYinghai Lu 
1131e5f15b45SYinghai Lu 	cleanup_highmap();
1132e5f15b45SYinghai Lu 
11332449f343STang Chen 	memblock_set_current_limit(ISA_END_ADDRESS);
11344918e228SIngo Molnar 	e820__memblock_setup();
113572d7c3b3SYinghai Lu 
1136e998879dSAshish Kalra 	/*
1137e998879dSAshish Kalra 	 * Needs to run after memblock setup because it needs the physical
1138e998879dSAshish Kalra 	 * memory size.
1139e998879dSAshish Kalra 	 */
1140e998879dSAshish Kalra 	sev_setup_arch();
1141e998879dSAshish Kalra 
11424971531aSMatt Fleming 	efi_fake_memmap();
11434971531aSMatt Fleming 	efi_find_mirror();
11443dad6f7fSRicardo Neri 	efi_esrt_init();
114558c90902SLenny Szubowicz 	efi_mokvar_table_init();
11464971531aSMatt Fleming 
1147916f676fSMatthew Garrett 	/*
11484971531aSMatt Fleming 	 * The EFI specification says that boot service code won't be
11494971531aSMatt Fleming 	 * called after ExitBootServices(). This is, in fact, a lie.
1150916f676fSMatthew Garrett 	 */
1151916f676fSMatthew Garrett 	efi_reserve_boot_services();
1152916f676fSMatthew Garrett 
115372d7c3b3SYinghai Lu 	/* preallocate 4k for mptable mpc */
11545da217caSIngo Molnar 	e820__memblock_alloc_reserved_mpc_new();
115572d7c3b3SYinghai Lu 
115672d7c3b3SYinghai Lu #ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
115772d7c3b3SYinghai Lu 	setup_bios_corruption_check();
115872d7c3b3SYinghai Lu #endif
115972d7c3b3SYinghai Lu 
116010054230SYinghai Lu #ifdef CONFIG_X86_32
1161365811d6SBjorn Helgaas 	printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n",
1162365811d6SBjorn Helgaas 			(max_pfn_mapped<<PAGE_SHIFT) - 1);
116310054230SYinghai Lu #endif
116472d7c3b3SYinghai Lu 
1165c361e5d4SMike Rapoport 	/*
1166ec35d1d9SBorislav Petkov 	 * Find free memory for the real mode trampoline and place it there. If
1167ec35d1d9SBorislav Petkov 	 * there is not enough free memory under 1M, on EFI-enabled systems
1168ec35d1d9SBorislav Petkov 	 * there will be additional attempt to reclaim the memory for the real
1169ec35d1d9SBorislav Petkov 	 * mode trampoline at efi_free_boot_services().
1170f1d4d47cSMike Rapoport 	 *
1171ec35d1d9SBorislav Petkov 	 * Unconditionally reserve the entire first 1M of RAM because BIOSes
1172ec35d1d9SBorislav Petkov 	 * are known to corrupt low memory and several hundred kilobytes are not
1173ec35d1d9SBorislav Petkov 	 * worth complex detection what memory gets clobbered. Windows does the
1174ec35d1d9SBorislav Petkov 	 * same thing for very similar reasons.
1175ec35d1d9SBorislav Petkov 	 *
1176ec35d1d9SBorislav Petkov 	 * Moreover, on machines with SandyBridge graphics or in setups that use
1177ec35d1d9SBorislav Petkov 	 * crashkernel the entire 1M is reserved anyway.
1178c361e5d4SMike Rapoport 	 */
1179f1d4d47cSMike Rapoport 	reserve_real_mode();
1180a9acc536SJesse Barnes 
118122ddfcaaSYinghai Lu 	init_mem_mapping();
1182854c879fSPekka J Enberg 
1183433f8924SThomas Gleixner 	idt_setup_early_pf();
1184cda846f1SJarkko Sakkinen 
118518bc7bd5SAndy Lutomirski 	/*
118618bc7bd5SAndy Lutomirski 	 * Update mmu_cr4_features (and, indirectly, trampoline_cr4_features)
118718bc7bd5SAndy Lutomirski 	 * with the current CR4 value.  This may not be necessary, but
118818bc7bd5SAndy Lutomirski 	 * auditing all the early-boot CR4 manipulation would be needed to
118918bc7bd5SAndy Lutomirski 	 * rule it out.
1190c7ad5ad2SAndy Lutomirski 	 *
1191c7ad5ad2SAndy Lutomirski 	 * Mask off features that don't work outside long mode (just
1192c7ad5ad2SAndy Lutomirski 	 * PCIDE for now).
119318bc7bd5SAndy Lutomirski 	 */
1194c7ad5ad2SAndy Lutomirski 	mmu_cr4_features = __read_cr4() & ~X86_CR4_PCIDE;
119518bc7bd5SAndy Lutomirski 
11964ce7a869SYinghai Lu 	memblock_set_current_limit(get_max_mapped());
1197e7b37895SYinghai Lu 
1198e7b37895SYinghai Lu 	/*
1199e7b37895SYinghai Lu 	 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
1200e7b37895SYinghai Lu 	 */
1201e7b37895SYinghai Lu 
1202e7b37895SYinghai Lu #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
1203e7b37895SYinghai Lu 	if (init_ohci1394_dma_early)
1204162a7e75SMike Travis 		init_ohci1394_dma_on_all_controllers();
1205162a7e75SMike Travis #endif
1206e7b37895SYinghai Lu 	/* Allocate bigger log buffer */
120755f26239SYinghai Lu 	setup_log_buf(1);
120855f26239SYinghai Lu 
12099661b332SDavid Howells 	if (efi_enabled(EFI_BOOT)) {
12109661b332SDavid Howells 		switch (boot_params.secure_boot) {
12119661b332SDavid Howells 		case efi_secureboot_mode_disabled:
12129661b332SDavid Howells 			pr_info("Secure boot disabled\n");
12139661b332SDavid Howells 			break;
12149661b332SDavid Howells 		case efi_secureboot_mode_enabled:
12159661b332SDavid Howells 			pr_info("Secure boot enabled\n");
12169661b332SDavid Howells 			break;
12179661b332SDavid Howells 		default:
12189661b332SDavid Howells 			pr_info("Secure boot could not be determined\n");
12199661b332SDavid Howells 			break;
12209661b332SDavid Howells 		}
12219661b332SDavid Howells 	}
12229661b332SDavid Howells 
122344280733SYinghai Lu 	reserve_initrd();
122444280733SYinghai Lu 
1225da3d3f98SAleksey Makarov 	acpi_table_upgrade();
12266998a880SRafael J. Wysocki 	/* Look for ACPI tables and reserve memory occupied by them. */
12276998a880SRafael J. Wysocki 	acpi_boot_table_init();
122855f26239SYinghai Lu 
122955f26239SYinghai Lu 	vsmp_init();
123055f26239SYinghai Lu 
123155f26239SYinghai Lu 	io_delay_init();
123255f26239SYinghai Lu 
1233630b3affSLukas Wunner 	early_platform_quirks();
1234630b3affSLukas Wunner 
123520e6926dSYinghai Lu 	early_acpi_boot_init();
123620e6926dSYinghai Lu 
123791467bdfSBernhard Walle 	initmem_init();
12383c325f82SWeijie Yang 	dma_contiguous_reserve(max_pfn_mapped << PAGE_SHIFT);
1239fa591c4aSTang Chen 
1240cf11e85fSRoman Gushchin 	if (boot_cpu_has(X86_FEATURE_GBPAGES))
1241cf11e85fSRoman Gushchin 		hugetlb_cma_reserve(PUD_SHIFT - PAGE_SHIFT);
1242cf11e85fSRoman Gushchin 
1243fa591c4aSTang Chen 	/*
1244fa591c4aSTang Chen 	 * Reserve memory for crash kernel after SRAT is parsed so that it
1245fa591c4aSTang Chen 	 * won't consume hotpluggable memory.
1246fa591c4aSTang Chen 	 */
1247fa591c4aSTang Chen 	reserve_crashkernel();
1248fa591c4aSTang Chen 
124955f26239SYinghai Lu 	memblock_find_dma_reserve();
125055f26239SYinghai Lu 
1251ccb64941SBoris Ostrovsky 	if (!early_xdbc_setup_hardware())
1252ccb64941SBoris Ostrovsky 		early_xdbc_register_console();
1253ccb64941SBoris Ostrovsky 
1254030cb6c0SThomas Gleixner 	x86_init.paging.pagetable_init();
125555f26239SYinghai Lu 
1256ef7f0d6aSAndrey Ryabinin 	kasan_init();
1257ef7f0d6aSAndrey Ryabinin 
1258d2b6dc61SAndy Lutomirski 	/*
1259945fd17aSThomas Gleixner 	 * Sync back kernel address range.
1260945fd17aSThomas Gleixner 	 *
1261945fd17aSThomas Gleixner 	 * FIXME: Can the later sync in setup_cpu_entry_areas() replace
1262945fd17aSThomas Gleixner 	 * this call?
1263d2b6dc61SAndy Lutomirski 	 */
1264945fd17aSThomas Gleixner 	sync_initial_page_table();
1265d2b6dc61SAndy Lutomirski 
126631625340SJoseph Cihula 	tboot_probe();
126731625340SJoseph Cihula 
126855f26239SYinghai Lu 	map_vsyscall();
126955f26239SYinghai Lu 
127055f26239SYinghai Lu 	generic_apic_probe();
127155f26239SYinghai Lu 
127255f26239SYinghai Lu 	early_quirks();
127355f26239SYinghai Lu 
127455f26239SYinghai Lu 	/*
127555f26239SYinghai Lu 	 * Read APIC and some other early information from ACPI tables.
127655f26239SYinghai Lu 	 */
127755f26239SYinghai Lu 	acpi_boot_init();
1278a906fdaaSThomas Gleixner 	x86_dtb_init();
1279efafc8b2SFeng Tang 
128055f26239SYinghai Lu 	/*
128155f26239SYinghai Lu 	 * get boot-time SMP configuration:
128255f26239SYinghai Lu 	 */
128355f26239SYinghai Lu 	get_smp_config();
128455f26239SYinghai Lu 
12851e90a13dSThomas Gleixner 	/*
12861e90a13dSThomas Gleixner 	 * Systems w/o ACPI and mptables might not have it mapped the local
12871e90a13dSThomas Gleixner 	 * APIC yet, but prefill_possible_map() might need to access it.
12881e90a13dSThomas Gleixner 	 */
12891e90a13dSThomas Gleixner 	init_apic_mappings();
12901e90a13dSThomas Gleixner 
1291329513a3SYinghai Lu 	prefill_possible_map();
1292301e6190SYinghai Lu 
12935f4765f9SYinghai Lu 	init_cpu_to_node();
129473bf7382SJonathan Cameron 	init_gi_nodes();
12955f4765f9SYinghai Lu 
1296ca1b8862SThomas Gleixner 	io_apic_init_mappings();
12979d6a4d08SYinghai Lu 
1298f3614646SJuergen Gross 	x86_init.hyper.guest_late_init();
129955f26239SYinghai Lu 
13001506c8dcSIngo Molnar 	e820__reserve_resources();
1301cc55f753SZhimin Gu 	e820__register_nosave_regions(max_pfn);
130255f26239SYinghai Lu 
13038fee697dSThomas Gleixner 	x86_init.resources.reserve_resources();
130455f26239SYinghai Lu 
13052df908baSIngo Molnar 	e820__setup_pci_gap();
130655f26239SYinghai Lu 
130755f26239SYinghai Lu #ifdef CONFIG_VT
130855f26239SYinghai Lu #if defined(CONFIG_VGA_CONSOLE)
130983e68189SMatt Fleming 	if (!efi_enabled(EFI_BOOT) || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
131055f26239SYinghai Lu 		conswitchp = &vga_con;
131155f26239SYinghai Lu #endif
131255f26239SYinghai Lu #endif
13136f30c1acSThomas Gleixner 	x86_init.oem.banner();
1314a2202aa2SYong Wang 
13156b617e22SFeng Tang 	x86_init.timers.wallclock_init();
13166b617e22SFeng Tang 
13179a90ed06SBorislav Petkov 	/*
13189a90ed06SBorislav Petkov 	 * This needs to run before setup_local_APIC() which soft-disables the
13199a90ed06SBorislav Petkov 	 * local APIC temporarily and that masks the thermal LVT interrupt,
13209a90ed06SBorislav Petkov 	 * leading to softlockups on machines which have configured SMI
13219a90ed06SBorislav Petkov 	 * interrupt delivery.
13229a90ed06SBorislav Petkov 	 */
13239a90ed06SBorislav Petkov 	therm_lvt_init();
13249a90ed06SBorislav Petkov 
1325a2202aa2SYong Wang 	mcheck_init();
1326f49aa448SJason Baron 
1327b3c869d3SJohn Stultz 	register_refined_jiffies(CLOCK_TICK_RATE);
13285189c2a7SOlof Johansson 
13295189c2a7SOlof Johansson #ifdef CONFIG_EFI
1330a5d90c92SBorislav Petkov 	if (efi_enabled(EFI_BOOT))
1331a5d90c92SBorislav Petkov 		efi_apply_memmap_quirks();
13325189c2a7SOlof Johansson #endif
1333ee9f8fceSJosh Poimboeuf 
1334ee9f8fceSJosh Poimboeuf 	unwind_init();
133555f26239SYinghai Lu }
13365649b7c3SIngo Molnar 
13379be1b56aSIngo Molnar #ifdef CONFIG_X86_32
13385649b7c3SIngo Molnar 
13398fee697dSThomas Gleixner static struct resource video_ram_resource = {
13408fee697dSThomas Gleixner 	.name	= "Video RAM area",
13418fee697dSThomas Gleixner 	.start	= 0xa0000,
13428fee697dSThomas Gleixner 	.end	= 0xbffff,
13438fee697dSThomas Gleixner 	.flags	= IORESOURCE_BUSY | IORESOURCE_MEM
13449be1b56aSIngo Molnar };
13459be1b56aSIngo Molnar 
13468fee697dSThomas Gleixner void __init i386_reserve_resources(void)
13479be1b56aSIngo Molnar {
13488fee697dSThomas Gleixner 	request_resource(&iomem_resource, &video_ram_resource);
13498fee697dSThomas Gleixner 	reserve_standard_io_resources();
13509be1b56aSIngo Molnar }
13519be1b56aSIngo Molnar 
13529be1b56aSIngo Molnar #endif /* CONFIG_X86_32 */
1353f32360efSKees Cook 
1354f32360efSKees Cook static struct notifier_block kernel_offset_notifier = {
1355f32360efSKees Cook 	.notifier_call = dump_kernel_offset
1356f32360efSKees Cook };
1357f32360efSKees Cook 
1358f32360efSKees Cook static int __init register_kernel_offset_dumper(void)
1359f32360efSKees Cook {
1360f32360efSKees Cook 	atomic_notifier_chain_register(&panic_notifier_list,
1361f32360efSKees Cook 					&kernel_offset_notifier);
1362f32360efSKees Cook 	return 0;
1363f32360efSKees Cook }
1364f32360efSKees Cook __initcall(register_kernel_offset_dumper);
1365