x86, mm: Break down init_all_memory_mapping
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / arch / x86 / kernel / setup.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2 * Copyright (C) 1995 Linus Torvalds
3 *
4 * Support of BIGMEM added by Gerhard Wichert, Siemens AG, July 1999
5 *
6 * Memory region support
7 * David Parsons <orc@pell.chi.il.us>, July-August 1999
8 *
9 * Added E820 sanitization routine (removes overlapping memory regions);
10 * Brian Moyle <bmoyle@mvista.com>, February 2001
11 *
12 * Moved CPU detection code to cpu/${cpu}.c
13 * Patrick Mochel <mochel@osdl.org>, March 2002
14 *
15 * Provisions for empty E820 memory regions (reported by certain BIOSes).
16 * Alex Achenbach <xela@slit.de>, December 2002.
17 *
18 */
19
20/*
21 * This file handles the architecture-dependent parts of initialization
22 */
23
24#include <linux/sched.h>
25#include <linux/mm.h>
05b79bdc 26#include <linux/mmzone.h>
894673ee 27#include <linux/screen_info.h>
1da177e4
LT
28#include <linux/ioport.h>
29#include <linux/acpi.h>
efafc8b2 30#include <linux/sfi.h>
1da177e4
LT
31#include <linux/apm_bios.h>
32#include <linux/initrd.h>
33#include <linux/bootmem.h>
72d7c3b3 34#include <linux/memblock.h>
1da177e4
LT
35#include <linux/seq_file.h>
36#include <linux/console.h>
1da177e4
LT
37#include <linux/root_dev.h>
38#include <linux/highmem.h>
39#include <linux/module.h>
40#include <linux/efi.h>
41#include <linux/init.h>
42#include <linux/edd.h>
138fe4e0 43#include <linux/iscsi_ibft.h>
1da177e4 44#include <linux/nodemask.h>
1bc3b91a 45#include <linux/kexec.h>
e9928674 46#include <linux/dmi.h>
22a9835c 47#include <linux/pfn.h>
376ff035 48#include <linux/pci.h>
46d671b5 49#include <asm/pci-direct.h>
f212ec4b 50#include <linux/init_ohci1394_dma.h>
790c73f6 51#include <linux/kvm_para.h>
0a2b9a6e 52#include <linux/dma-contiguous.h>
1bc3b91a 53
46d671b5
YL
54#include <linux/errno.h>
55#include <linux/kernel.h>
56#include <linux/stddef.h>
57#include <linux/unistd.h>
58#include <linux/ptrace.h>
46d671b5
YL
59#include <linux/user.h>
60#include <linux/delay.h>
46d671b5
YL
61
62#include <linux/kallsyms.h>
46d671b5
YL
63#include <linux/cpufreq.h>
64#include <linux/dma-mapping.h>
65#include <linux/ctype.h>
66#include <linux/uaccess.h>
67
68#include <linux/percpu.h>
69#include <linux/crash_dump.h>
69575d38 70#include <linux/tboot.h>
b3c869d3 71#include <linux/jiffies.h>
46d671b5 72
1da177e4 73#include <video/edid.h>
1bc3b91a 74
093af8d7 75#include <asm/mtrr.h>
9635b47d 76#include <asm/apic.h>
084ee1c6 77#include <asm/realmode.h>
1da177e4
LT
78#include <asm/e820.h>
79#include <asm/mpspec.h>
80#include <asm/setup.h>
55f26239 81#include <asm/efi.h>
8e6dafd6
IM
82#include <asm/timer.h>
83#include <asm/i8259.h>
1da177e4 84#include <asm/sections.h>
1c6e5503 85#include <asm/dmi.h>
1da177e4
LT
86#include <asm/io_apic.h>
87#include <asm/ist.h>
1164dd00 88#include <asm/setup_arch.h>
ce3fe6b2 89#include <asm/bios_ebda.h>
00bf4098 90#include <asm/cacheflush.h>
2fde61fd 91#include <asm/processor.h>
cc9f7a0c 92#include <asm/bugs.h>
1da177e4 93
46d671b5 94#include <asm/vsyscall.h>
6e5385d4 95#include <asm/cpu.h>
46d671b5
YL
96#include <asm/desc.h>
97#include <asm/dma.h>
46a7fa27 98#include <asm/iommu.h>
1d9b16d1 99#include <asm/gart.h>
46d671b5
YL
100#include <asm/mmu_context.h>
101#include <asm/proto.h>
102
46d671b5 103#include <asm/paravirt.h>
88b094fb 104#include <asm/hypervisor.h>
fd699c76 105#include <asm/olpc_ofw.h>
46d671b5
YL
106
107#include <asm/percpu.h>
46d671b5
YL
108#include <asm/topology.h>
109#include <asm/apicdef.h>
23ac4ae8 110#include <asm/amd_nb.h>
55f26239
YL
111#ifdef CONFIG_X86_64
112#include <asm/numa_64.h>
113#endif
a2202aa2 114#include <asm/mce.h>
f49aa448 115#include <asm/alternative.h>
da6b737b 116#include <asm/prom.h>
46d671b5 117
2b72394e 118/*
66520ebc
JS
119 * max_low_pfn_mapped: highest direct mapped pfn under 4GB
120 * max_pfn_mapped: highest direct mapped pfn over 4GB
121 *
122 * The direct mapping only covers E820_RAM regions, so the ranges and gaps are
123 * represented by pfn_mapped
2b72394e
PE
124 */
125unsigned long max_low_pfn_mapped;
126unsigned long max_pfn_mapped;
127
e808bae2 128#ifdef CONFIG_DMI
796216a5 129RESERVE_BRK(dmi_alloc, 65536);
e808bae2 130#endif
796216a5 131
c0b5842a 132
93dbda7c
JF
133static __initdata unsigned long _brk_start = (unsigned long)__brk_base;
134unsigned long _brk_end = (unsigned long)__brk_base;
135
c0b5842a
IM
136#ifdef CONFIG_X86_64
137int default_cpu_present_to_apicid(int mps_cpu)
138{
139 return __default_cpu_present_to_apicid(mps_cpu);
140}
141
e11dadab 142int default_check_phys_apicid_present(int phys_apicid)
c0b5842a 143{
e11dadab 144 return __default_check_phys_apicid_present(phys_apicid);
c0b5842a
IM
145}
146#endif
147
217b8ce8
YL
148#ifndef CONFIG_DEBUG_BOOT_PARAMS
149struct boot_params __initdata boot_params;
150#else
151struct boot_params boot_params;
152#endif
153
1da177e4
LT
154/*
155 * Machine setup..
156 */
c9cce83d
BW
157static struct resource data_resource = {
158 .name = "Kernel data",
159 .start = 0,
160 .end = 0,
161 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
162};
163
164static struct resource code_resource = {
165 .name = "Kernel code",
166 .start = 0,
167 .end = 0,
168 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
169};
170
171static struct resource bss_resource = {
172 .name = "Kernel bss",
173 .start = 0,
174 .end = 0,
175 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
176};
177
7dea23ec
YL
178
179#ifdef CONFIG_X86_32
1da177e4 180/* cpu data as detected by the assembly code in head.S */
7dea23ec 181struct cpuinfo_x86 new_cpu_data __cpuinitdata = {0, 0, 0, 0, -1, 1, 0, 0, -1};
1da177e4 182/* common cpu data for all cpus */
7dea23ec 183struct cpuinfo_x86 boot_cpu_data __read_mostly = {0, 0, 0, 0, -1, 1, 0, 0, -1};
129f6946 184EXPORT_SYMBOL(boot_cpu_data);
1da177e4 185
0c254e38
AS
186unsigned int def_to_bigsmp;
187
1da177e4
LT
188/* for MCA, but anyone else can use it if they want */
189unsigned int machine_id;
190unsigned int machine_submodel_id;
191unsigned int BIOS_revision;
1da177e4 192
7dea23ec
YL
193struct apm_info apm_info;
194EXPORT_SYMBOL(apm_info);
195
196#if defined(CONFIG_X86_SPEEDSTEP_SMI) || \
197 defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE)
198struct ist_info ist_info;
199EXPORT_SYMBOL(ist_info);
200#else
201struct ist_info ist_info;
202#endif
203
204#else
ed26dbe5
JF
205struct cpuinfo_x86 boot_cpu_data __read_mostly = {
206 .x86_phys_bits = MAX_PHYSMEM_BITS,
207};
7dea23ec
YL
208EXPORT_SYMBOL(boot_cpu_data);
209#endif
210
211
212#if !defined(CONFIG_X86_PAE) || defined(CONFIG_X86_64)
213unsigned long mmu_cr4_features;
214#else
215unsigned long mmu_cr4_features = X86_CR4_PAE;
216#endif
217
5031296c
PA
218/* Boot loader ID and version as integers, for the benefit of proc_dointvec */
219int bootloader_type, bootloader_version;
1da177e4 220
1da177e4
LT
221/*
222 * Setup options
223 */
1da177e4 224struct screen_info screen_info;
129f6946 225EXPORT_SYMBOL(screen_info);
1da177e4 226struct edid_info edid_info;
5e518d76 227EXPORT_SYMBOL_GPL(edid_info);
1da177e4 228
1da177e4
LT
229extern int root_mountflags;
230
e44b7b75 231unsigned long saved_video_mode;
1da177e4 232
cf8fa920 233#define RAMDISK_IMAGE_START_MASK 0x07FF
1da177e4 234#define RAMDISK_PROMPT_FLAG 0x8000
cf8fa920 235#define RAMDISK_LOAD_FLAG 0x4000
1da177e4 236
4e498b66 237static char __initdata command_line[COMMAND_LINE_SIZE];
516cbf37
TB
238#ifdef CONFIG_CMDLINE_BOOL
239static char __initdata builtin_cmdline[COMMAND_LINE_SIZE] = CONFIG_CMDLINE;
240#endif
1da177e4 241
1da177e4
LT
242#if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
243struct edd edd;
244#ifdef CONFIG_EDD_MODULE
245EXPORT_SYMBOL(edd);
246#endif
247/**
248 * copy_edd() - Copy the BIOS EDD information
249 * from boot_params into a safe place.
250 *
251 */
9eaa192d 252static inline void __init copy_edd(void)
1da177e4 253{
30c82645
PA
254 memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer,
255 sizeof(edd.mbr_signature));
256 memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info));
257 edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries;
258 edd.edd_info_nr = boot_params.eddbuf_entries;
1da177e4
LT
259}
260#else
9eaa192d 261static inline void __init copy_edd(void)
1da177e4
LT
262{
263}
264#endif
265
5368a2be
PA
266void * __init extend_brk(size_t size, size_t align)
267{
268 size_t mask = align - 1;
269 void *ret;
270
271 BUG_ON(_brk_start == 0);
272 BUG_ON(align & mask);
273
274 _brk_end = (_brk_end + mask) & ~mask;
275 BUG_ON((char *)(_brk_end + size) > __brk_limit);
276
277 ret = (void *)_brk_end;
278 _brk_end += size;
279
280 memset(ret, 0, size);
281
282 return ret;
283}
284
854c879f
PE
285#ifdef CONFIG_X86_64
286static void __init init_gbpages(void)
287{
288 if (direct_gbpages && cpu_has_gbpages)
289 printk(KERN_INFO "Using GB pages for direct mapping\n");
290 else
291 direct_gbpages = 0;
292}
293#else
294static inline void init_gbpages(void)
295{
296}
e5f15b45 297static void __init cleanup_highmap(void)
f005fe12
YL
298{
299}
854c879f
PE
300#endif
301
5368a2be
PA
302static void __init reserve_brk(void)
303{
304 if (_brk_end > _brk_start)
24aa0788
TH
305 memblock_reserve(__pa(_brk_start),
306 __pa(_brk_end) - __pa(_brk_start));
5368a2be
PA
307
308 /* Mark brk area as locked down and no longer taking any
309 new allocations */
310 _brk_start = 0;
311}
312
cf8fa920
PA
313#ifdef CONFIG_BLK_DEV_INITRD
314
eb1379cb
YL
315#define MAX_MAP_CHUNK (NR_FIX_BTMAPS << PAGE_SHIFT)
316static void __init relocate_initrd(void)
cf8fa920 317{
c967da6a 318 /* Assume only end is not page aligned */
ba5b14cc
YL
319 u64 ramdisk_image = boot_params.hdr.ramdisk_image;
320 u64 ramdisk_size = boot_params.hdr.ramdisk_size;
c967da6a 321 u64 area_size = PAGE_ALIGN(ramdisk_size);
ba5b14cc 322 u64 ramdisk_here;
eb1379cb
YL
323 unsigned long slop, clen, mapaddr;
324 char *p, *q;
cf8fa920 325
e8c57d40 326 /* We need to move the initrd down into directly mapped mem */
74f27655 327 ramdisk_here = memblock_find_in_range(0, PFN_PHYS(max_pfn_mapped),
e8c57d40 328 area_size, PAGE_SIZE);
cf8fa920 329
1f5026a7 330 if (!ramdisk_here)
3945e2c9
YL
331 panic("Cannot find place for new RAMDISK of size %lld\n",
332 ramdisk_size);
333
e8c57d40 334 /* Note: this includes all the mem currently occupied by
cf8fa920 335 the initrd, we rely on that fact to keep the data intact. */
24aa0788 336 memblock_reserve(ramdisk_here, area_size);
cf8fa920
PA
337 initrd_start = ramdisk_here + PAGE_OFFSET;
338 initrd_end = initrd_start + ramdisk_size;
365811d6
BH
339 printk(KERN_INFO "Allocated new RAMDISK: [mem %#010llx-%#010llx]\n",
340 ramdisk_here, ramdisk_here + ramdisk_size - 1);
cf8fa920 341
cf8fa920
PA
342 q = (char *)initrd_start;
343
e8c57d40 344 /* Copy the initrd */
cf8fa920
PA
345 while (ramdisk_size) {
346 slop = ramdisk_image & ~PAGE_MASK;
347 clen = ramdisk_size;
348 if (clen > MAX_MAP_CHUNK-slop)
349 clen = MAX_MAP_CHUNK-slop;
350 mapaddr = ramdisk_image & PAGE_MASK;
88b4c146 351 p = early_memremap(mapaddr, clen+slop);
cf8fa920 352 memcpy(q, p+slop, clen);
beacfaac 353 early_iounmap(p, clen+slop);
cf8fa920
PA
354 q += clen;
355 ramdisk_image += clen;
356 ramdisk_size -= clen;
357 }
e8c57d40 358
ba5b14cc
YL
359 ramdisk_image = boot_params.hdr.ramdisk_image;
360 ramdisk_size = boot_params.hdr.ramdisk_size;
365811d6
BH
361 printk(KERN_INFO "Move RAMDISK from [mem %#010llx-%#010llx] to"
362 " [mem %#010llx-%#010llx]\n",
ba5b14cc
YL
363 ramdisk_image, ramdisk_image + ramdisk_size - 1,
364 ramdisk_here, ramdisk_here + ramdisk_size - 1);
eb1379cb 365}
9a27f5c5 366
e8c57d40
YL
367static u64 __init get_mem_size(unsigned long limit_pfn)
368{
369 int i;
370 u64 mapped_pages = 0;
371 unsigned long start_pfn, end_pfn;
372
373 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, NULL) {
374 start_pfn = min_t(unsigned long, start_pfn, limit_pfn);
375 end_pfn = min_t(unsigned long, end_pfn, limit_pfn);
376 mapped_pages += end_pfn - start_pfn;
377 }
378
379 return mapped_pages << PAGE_SHIFT;
380}
eb1379cb
YL
381static void __init reserve_initrd(void)
382{
c967da6a 383 /* Assume only end is not page aligned */
eb1379cb
YL
384 u64 ramdisk_image = boot_params.hdr.ramdisk_image;
385 u64 ramdisk_size = boot_params.hdr.ramdisk_size;
c967da6a 386 u64 ramdisk_end = PAGE_ALIGN(ramdisk_image + ramdisk_size);
e8c57d40 387 u64 mapped_size;
eb1379cb
YL
388
389 if (!boot_params.hdr.type_of_loader ||
390 !ramdisk_image || !ramdisk_size)
391 return; /* No initrd provided by bootloader */
392
393 initrd_start = 0;
394
74f27655 395 mapped_size = get_mem_size(max_pfn_mapped);
e8c57d40 396 if (ramdisk_size >= (mapped_size>>1))
ab7b64e9
PJ
397 panic("initrd too large to handle, "
398 "disabling initrd (%lld needed, %lld available)\n",
e8c57d40 399 ramdisk_size, mapped_size>>1);
eb1379cb 400
365811d6
BH
401 printk(KERN_INFO "RAMDISK: [mem %#010llx-%#010llx]\n", ramdisk_image,
402 ramdisk_end - 1);
eb1379cb 403
74f27655 404 if (pfn_range_is_mapped(PFN_DOWN(ramdisk_image),
e8c57d40
YL
405 PFN_DOWN(ramdisk_end))) {
406 /* All are mapped, easy case */
eb1379cb
YL
407 /*
408 * don't need to reserve again, already reserved early
409 * in i386_start_kernel
410 */
411 initrd_start = ramdisk_image + PAGE_OFFSET;
412 initrd_end = initrd_start + ramdisk_size;
413 return;
414 }
415
eb1379cb 416 relocate_initrd();
8c5dd8f4 417
24aa0788 418 memblock_free(ramdisk_image, ramdisk_end - ramdisk_image);
cf8fa920 419}
225c37d7 420#else
eb1379cb 421static void __init reserve_initrd(void)
225c37d7
YL
422{
423}
cf8fa920
PA
424#endif /* CONFIG_BLK_DEV_INITRD */
425
29f784e3 426static void __init parse_setup_data(void)
257b0fde
YL
427{
428 struct setup_data *data;
429 u64 pa_data;
430
431 if (boot_params.hdr.version < 0x0209)
432 return;
433 pa_data = boot_params.hdr.setup_data;
434 while (pa_data) {
f1c2b357
SAS
435 u32 data_len, map_len;
436
437 map_len = max(PAGE_SIZE - (pa_data & ~PAGE_MASK),
438 (u64)sizeof(struct setup_data));
439 data = early_memremap(pa_data, map_len);
440 data_len = data->len + sizeof(struct setup_data);
441 if (data_len > map_len) {
442 early_iounmap(data, map_len);
443 data = early_memremap(pa_data, data_len);
444 map_len = data_len;
445 }
446
257b0fde
YL
447 switch (data->type) {
448 case SETUP_E820_EXT:
f1c2b357 449 parse_e820_ext(data);
257b0fde 450 break;
da6b737b
SAS
451 case SETUP_DTB:
452 add_dtb(pa_data);
257b0fde
YL
453 break;
454 default:
455 break;
456 }
257b0fde 457 pa_data = data->next;
f1c2b357 458 early_iounmap(data, map_len);
257b0fde
YL
459 }
460}
461
a0a0becd 462static void __init e820_reserve_setup_data(void)
28bb2237
YL
463{
464 struct setup_data *data;
465 u64 pa_data;
d9a81b44 466 int found = 0;
28bb2237
YL
467
468 if (boot_params.hdr.version < 0x0209)
469 return;
470 pa_data = boot_params.hdr.setup_data;
471 while (pa_data) {
88b4c146 472 data = early_memremap(pa_data, sizeof(*data));
28bb2237
YL
473 e820_update_range(pa_data, sizeof(*data)+data->len,
474 E820_RAM, E820_RESERVED_KERN);
d9a81b44 475 found = 1;
28bb2237
YL
476 pa_data = data->next;
477 early_iounmap(data, sizeof(*data));
478 }
d9a81b44
YL
479 if (!found)
480 return;
481
28bb2237 482 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
a0a0becd 483 memcpy(&e820_saved, &e820, sizeof(struct e820map));
28bb2237
YL
484 printk(KERN_INFO "extended physical RAM map:\n");
485 e820_print_map("reserve setup_data");
486}
487
a9ce6bc1 488static void __init memblock_x86_reserve_range_setup_data(void)
a0a0becd
YL
489{
490 struct setup_data *data;
491 u64 pa_data;
a0a0becd
YL
492
493 if (boot_params.hdr.version < 0x0209)
494 return;
495 pa_data = boot_params.hdr.setup_data;
496 while (pa_data) {
88b4c146 497 data = early_memremap(pa_data, sizeof(*data));
24aa0788 498 memblock_reserve(pa_data, sizeof(*data) + data->len);
a0a0becd
YL
499 pa_data = data->next;
500 early_iounmap(data, sizeof(*data));
501 }
502}
503
ccb4defa
YL
504/*
505 * --------- Crashkernel reservation ------------------------------
506 */
507
508#ifdef CONFIG_KEXEC
32105f7f 509
7f8595bf
PA
510/*
511 * Keep the crash kernel below this limit. On 32 bits earlier kernels
512 * would limit the kernel to the low 512 MiB due to mapping restrictions.
513 * On 64 bits, kexec-tools currently limits us to 896 MiB; increase this
514 * limit once kexec-tools are fixed.
515 */
516#ifdef CONFIG_X86_32
517# define CRASH_KERNEL_ADDR_MAX (512 << 20)
518#else
519# define CRASH_KERNEL_ADDR_MAX (896 << 20)
520#endif
521
29f784e3 522static void __init reserve_crashkernel(void)
ccb4defa
YL
523{
524 unsigned long long total_mem;
525 unsigned long long crash_size, crash_base;
526 int ret;
527
09c71bfd 528 total_mem = memblock_phys_mem_size();
ccb4defa
YL
529
530 ret = parse_crashkernel(boot_command_line, total_mem,
531 &crash_size, &crash_base);
32105f7f
BW
532 if (ret != 0 || crash_size <= 0)
533 return;
534
535 /* 0 means: find the address automatically */
536 if (crash_base <= 0) {
44280733
YL
537 const unsigned long long alignment = 16<<20; /* 16M */
538
9f4c1396 539 /*
7f8595bf 540 * kexec want bzImage is below CRASH_KERNEL_ADDR_MAX
9f4c1396
YL
541 */
542 crash_base = memblock_find_in_range(alignment,
7f8595bf 543 CRASH_KERNEL_ADDR_MAX, crash_size, alignment);
9f4c1396 544
1f5026a7 545 if (!crash_base) {
44280733 546 pr_info("crashkernel reservation failed - No suitable area found.\n");
ccb4defa
YL
547 return;
548 }
32105f7f 549 } else {
44280733
YL
550 unsigned long long start;
551
9f4c1396
YL
552 start = memblock_find_in_range(crash_base,
553 crash_base + crash_size, crash_size, 1<<20);
44280733
YL
554 if (start != crash_base) {
555 pr_info("crashkernel reservation failed - memory is in use.\n");
ccb4defa
YL
556 return;
557 }
32105f7f 558 }
24aa0788 559 memblock_reserve(crash_base, crash_size);
ccb4defa 560
32105f7f
BW
561 printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
562 "for crashkernel (System RAM: %ldMB)\n",
563 (unsigned long)(crash_size >> 20),
564 (unsigned long)(crash_base >> 20),
565 (unsigned long)(total_mem >> 20));
ccb4defa 566
32105f7f
BW
567 crashk_res.start = crash_base;
568 crashk_res.end = crash_base + crash_size - 1;
569 insert_resource(&iomem_resource, &crashk_res);
ccb4defa
YL
570}
571#else
29f784e3 572static void __init reserve_crashkernel(void)
ccb4defa
YL
573{
574}
575#endif
576
bdba0e70
YL
577static struct resource standard_io_resources[] = {
578 { .name = "dma1", .start = 0x00, .end = 0x1f,
579 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
580 { .name = "pic1", .start = 0x20, .end = 0x21,
581 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
582 { .name = "timer0", .start = 0x40, .end = 0x43,
583 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
584 { .name = "timer1", .start = 0x50, .end = 0x53,
585 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
586 { .name = "keyboard", .start = 0x60, .end = 0x60,
587 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
588 { .name = "keyboard", .start = 0x64, .end = 0x64,
589 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
590 { .name = "dma page reg", .start = 0x80, .end = 0x8f,
591 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
592 { .name = "pic2", .start = 0xa0, .end = 0xa1,
593 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
594 { .name = "dma2", .start = 0xc0, .end = 0xdf,
595 .flags = IORESOURCE_BUSY | IORESOURCE_IO },
596 { .name = "fpu", .start = 0xf0, .end = 0xff,
597 .flags = IORESOURCE_BUSY | IORESOURCE_IO }
598};
599
8fee697d 600void __init reserve_standard_io_resources(void)
bdba0e70
YL
601{
602 int i;
603
604 /* request I/O space for devices used on all i[345]86 PCs */
605 for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
606 request_resource(&ioport_resource, &standard_io_resources[i]);
607
608}
609
042be38e
YL
610static __init void reserve_ibft_region(void)
611{
612 unsigned long addr, size = 0;
613
614 addr = find_ibft_region(&size);
615
616 if (size)
24aa0788 617 memblock_reserve(addr, size);
042be38e
YL
618}
619
9ea77bdb 620static unsigned reserve_low = CONFIG_X86_RESERVE_LOW << 10;
5649b7c3 621
1b5576e6
YL
622static void __init trim_bios_range(void)
623{
624 /*
625 * A special case is the first 4Kb of memory;
626 * This is a BIOS owned area, not kernel ram, but generally
627 * not listed as such in the E820 table.
d0cd7425
PA
628 *
629 * This typically reserves additional memory (64KiB by default)
630 * since some BIOSes are known to corrupt low memory. See the
9ea77bdb 631 * Kconfig help text for X86_RESERVE_LOW.
1b5576e6 632 */
9ea77bdb 633 e820_update_range(0, ALIGN(reserve_low, PAGE_SIZE),
d0cd7425
PA
634 E820_RAM, E820_RESERVED);
635
1b5576e6
YL
636 /*
637 * special case: Some BIOSen report the PC BIOS
638 * area (640->1Mb) as ram even though it is not.
639 * take them out.
640 */
641 e820_remove_range(BIOS_BEGIN, BIOS_END - BIOS_BEGIN, E820_RAM, 1);
642 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
643}
644
9ea77bdb
PA
645static int __init parse_reservelow(char *p)
646{
647 unsigned long long size;
648
649 if (!p)
650 return -EINVAL;
651
652 size = memparse(p, &p);
653
654 if (size < 4096)
655 size = 4096;
656
657 if (size > 640*1024)
658 size = 640*1024;
659
660 reserve_low = size;
661
662 return 0;
663}
664
665early_param("reservelow", parse_reservelow);
666
1da177e4
LT
667/*
668 * Determine if we were loaded by an EFI loader. If so, then we have also been
669 * passed the efi memmap, systab, etc., so we should use these data structures
670 * for initialization. Note, the efi init code path is determined by the
671 * global efi_enabled. This allows the same kernel image to be used on existing
672 * systems (with a traditional BIOS) as well as on EFI systems.
673 */
76934ed4
YL
674/*
675 * setup_arch - architecture-specific boot-time initializations
676 *
677 * Note: On x86_64, fixmaps are ready for use even before this is called.
678 */
679
1da177e4
LT
680void __init setup_arch(char **cmdline_p)
681{
76934ed4 682#ifdef CONFIG_X86_32
1da177e4 683 memcpy(&boot_cpu_data, &new_cpu_data, sizeof(new_cpu_data));
3b33553b 684 visws_early_detect();
b40827fa
BP
685
686 /*
687 * copy kernel address range established so far and switch
688 * to the proper swapper page table
689 */
690 clone_pgd_range(swapper_pg_dir + KERNEL_PGD_BOUNDARY,
691 initial_page_table + KERNEL_PGD_BOUNDARY,
692 KERNEL_PGD_PTRS);
693
694 load_cr3(swapper_pg_dir);
695 __flush_tlb_all();
76934ed4
YL
696#else
697 printk(KERN_INFO "Command line: %s\n", boot_command_line);
698#endif
1da177e4 699
9863c90f
AK
700 /*
701 * If we have OLPC OFW, we might end up relocating the fixmap due to
702 * reserve_top(), so do this before touching the ioremap area.
703 */
fd699c76
AS
704 olpc_ofw_detect();
705
29c84391 706 early_trap_init();
9e882c92 707 early_cpu_init();
1a98fd14
JF
708 early_ioremap_init();
709
fd699c76
AS
710 setup_olpc_ofw_pgd();
711
30c82645
PA
712 ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
713 screen_info = boot_params.screen_info;
714 edid_info = boot_params.edid_info;
76934ed4 715#ifdef CONFIG_X86_32
30c82645
PA
716 apm_info.bios = boot_params.apm_bios_info;
717 ist_info = boot_params.ist_info;
76934ed4 718 if (boot_params.sys_desc_table.length != 0) {
30c82645
PA
719 machine_id = boot_params.sys_desc_table.table[0];
720 machine_submodel_id = boot_params.sys_desc_table.table[1];
721 BIOS_revision = boot_params.sys_desc_table.table[2];
1da177e4 722 }
76934ed4
YL
723#endif
724 saved_video_mode = boot_params.hdr.vid_mode;
30c82645 725 bootloader_type = boot_params.hdr.type_of_loader;
5031296c
PA
726 if ((bootloader_type >> 4) == 0xe) {
727 bootloader_type &= 0xf;
728 bootloader_type |= (boot_params.hdr.ext_loader_type+0x10) << 4;
729 }
730 bootloader_version = bootloader_type & 0xf;
731 bootloader_version |= boot_params.hdr.ext_loader_ver << 4;
1da177e4
LT
732
733#ifdef CONFIG_BLK_DEV_RAM
30c82645
PA
734 rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
735 rd_prompt = ((boot_params.hdr.ram_size & RAMDISK_PROMPT_FLAG) != 0);
736 rd_doload = ((boot_params.hdr.ram_size & RAMDISK_LOAD_FLAG) != 0);
1da177e4 737#endif
7465252e
YL
738#ifdef CONFIG_EFI
739 if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
1adbfa35 740 "EL32", 4)) {
7465252e 741 efi_enabled = 1;
1adbfa35
OJ
742 efi_64bit = false;
743 } else if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
744 "EL64", 4)) {
745 efi_enabled = 1;
746 efi_64bit = true;
7465252e 747 }
1adbfa35
OJ
748 if (efi_enabled && efi_memblock_x86_reserve_range())
749 efi_enabled = 0;
7465252e
YL
750#endif
751
42bbdb43 752 x86_init.oem.arch_setup();
2215e69d 753
419afdf5 754 iomem_resource.end = (1ULL << boot_cpu_data.x86_phys_bits) - 1;
0dbfafa5 755 setup_memory_map();
28bb2237 756 parse_setup_data();
a0a0becd
YL
757 /* update the e820_saved too */
758 e820_reserve_setup_data();
28bb2237 759
1da177e4
LT
760 copy_edd();
761
30c82645 762 if (!boot_params.hdr.root_flags)
1da177e4
LT
763 root_mountflags &= ~MS_RDONLY;
764 init_mm.start_code = (unsigned long) _text;
765 init_mm.end_code = (unsigned long) _etext;
766 init_mm.end_data = (unsigned long) _edata;
93dbda7c 767 init_mm.brk = _brk_end;
1da177e4
LT
768
769 code_resource.start = virt_to_phys(_text);
770 code_resource.end = virt_to_phys(_etext)-1;
771 data_resource.start = virt_to_phys(_etext);
772 data_resource.end = virt_to_phys(_edata)-1;
00bf4098
BW
773 bss_resource.start = virt_to_phys(&__bss_start);
774 bss_resource.end = virt_to_phys(&__bss_stop)-1;
1da177e4 775
516cbf37
TB
776#ifdef CONFIG_CMDLINE_BOOL
777#ifdef CONFIG_CMDLINE_OVERRIDE
778 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
779#else
780 if (builtin_cmdline[0]) {
781 /* append boot loader cmdline to builtin */
782 strlcat(builtin_cmdline, " ", COMMAND_LINE_SIZE);
783 strlcat(builtin_cmdline, boot_command_line, COMMAND_LINE_SIZE);
784 strlcpy(boot_command_line, builtin_cmdline, COMMAND_LINE_SIZE);
785 }
786#endif
787#endif
788
eda6da92
YL
789 strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
790 *cmdline_p = command_line;
791
eda6da92 792 /*
4b0f3b81
KC
793 * x86_configure_nx() is called before parse_early_param() to detect
794 * whether hardware doesn't support NX (so that the early EHCI debug
795 * console setup can safely call set_fixmap()). It may then be called
796 * again from within noexec_setup() during parsing early parameters
797 * to honor the respective command line option.
eda6da92 798 */
4763ed4d 799 x86_configure_nx();
eda6da92
YL
800
801 parse_early_param();
802
4b0f3b81 803 x86_report_nx();
0ad5bce7 804
28bb2237 805 /* after early param, so could get panic from serial */
a9ce6bc1 806 memblock_x86_reserve_range_setup_data();
28bb2237 807
76934ed4 808 if (acpi_mps_check()) {
3eb11edc 809#ifdef CONFIG_X86_LOCAL_APIC
76934ed4 810 disable_apic = 1;
3eb11edc 811#endif
988781dc 812 setup_clear_cpu_cap(X86_FEATURE_APIC);
3c999f14
YL
813 }
814
dc7c65db
LT
815#ifdef CONFIG_PCI
816 if (pci_early_dump_regs)
817 early_dump_pci_devices();
818#endif
819
0dbfafa5 820 finish_e820_parsing();
1a3f239d 821
ff0c0874
BM
822 if (efi_enabled)
823 efi_init();
824
2216d199
YL
825 dmi_scan_machine();
826
88b094fb
AK
827 /*
828 * VMware detection requires dmi to be available, so this
829 * needs to be done after dmi_scan_machine, for the BP.
830 */
2d826404 831 init_hypervisor_platform();
88b094fb 832
f7cf5a5b 833 x86_init.resources.probe_roms();
41c094fd
YL
834
835 /* after parse_early_param, so could debug it */
836 insert_resource(&iomem_resource, &code_resource);
837 insert_resource(&iomem_resource, &data_resource);
838 insert_resource(&iomem_resource, &bss_resource);
839
4eea6aa5
JS
840 /*
841 * Complain if .text .data and .bss are not marked as E820_RAM and
842 * attempt to fix it by adding the range. We may have a confused BIOS,
843 * or the user may have incorrectly supplied it via memmap=exactmap. If
844 * we really are running on top non-RAM, we will crash later anyways.
845 */
846 if (!e820_all_mapped(code_resource.start, __pa(__brk_limit), E820_RAM)) {
847 pr_warn(".text .data .bss are not marked as E820_RAM!\n");
848
849 e820_add_region(code_resource.start,
850 __pa(__brk_limit) - code_resource.start + 1,
851 E820_RAM);
852 }
853
1b5576e6 854 trim_bios_range();
76934ed4 855#ifdef CONFIG_X86_32
cc9f7a0c
YL
856 if (ppro_with_ram_bug()) {
857 e820_update_range(0x70000000ULL, 0x40000ULL, E820_RAM,
858 E820_RESERVED);
859 sanitize_e820_map(e820.map, ARRAY_SIZE(e820.map), &e820.nr_map);
860 printk(KERN_INFO "fixed physical RAM map:\n");
861 e820_print_map("bad_ppro");
862 }
76934ed4
YL
863#else
864 early_gart_iommu_check();
865#endif
cc9f7a0c 866
7b2a0a6c
YL
867 /*
868 * partially used pages are not usable - thus
869 * we are rounding upwards:
870 */
f361a450 871 max_pfn = e820_end_of_ram_pfn();
7b2a0a6c 872
093af8d7
YL
873 /* update e820 for memory not covered by WB MTRRs */
874 mtrr_bp_init();
2dc807b3 875 if (mtrr_trim_uncached_memory(max_pfn))
f361a450 876 max_pfn = e820_end_of_ram_pfn();
76c32418 877
76934ed4 878#ifdef CONFIG_X86_32
4e29684c 879 /* max_low_pfn get updated here */
2ec65f8b 880 find_low_pfn_range();
76934ed4
YL
881#else
882 num_physpages = max_pfn;
883
06cd9a7d 884 check_x2apic();
76934ed4
YL
885
886 /* How many end-of-memory variables you have, grandma! */
887 /* need this before calling reserve_initrd */
f361a450
YL
888 if (max_pfn > (1UL<<(32 - PAGE_SHIFT)))
889 max_low_pfn = e820_end_of_low_ram_pfn();
890 else
891 max_low_pfn = max_pfn;
892
76934ed4 893 high_memory = (void *)__va(max_pfn * PAGE_SIZE - 1) + 1;
5394f80f
JF
894#endif
895
893f38d1
YL
896 /*
897 * Find and reserve possible boot-time SMP configuration:
898 */
899 find_smp_config();
900
042be38e
YL
901 reserve_ibft_region();
902
72d7c3b3
YL
903 /*
904 * Need to conclude brk, before memblock_x86_fill()
905 * it could use memblock_find_in_range, could overlap with
906 * brk area.
907 */
908 reserve_brk();
909
e5f15b45
YL
910 cleanup_highmap();
911
dd7dfad7 912 memblock.current_limit = ISA_END_ADDRESS;
72d7c3b3
YL
913 memblock_x86_fill();
914
916f676f
MG
915 /*
916 * The EFI specification says that boot service code won't be called
917 * after ExitBootServices(). This is, in fact, a lie.
918 */
919 if (efi_enabled)
920 efi_reserve_boot_services();
921
72d7c3b3
YL
922 /* preallocate 4k for mptable mpc */
923 early_reserve_e820_mpc_new();
924
925#ifdef CONFIG_X86_CHECK_BIOS_CORRUPTION
926 setup_bios_corruption_check();
927#endif
928
365811d6
BH
929 printk(KERN_DEBUG "initial memory mapped: [mem 0x00000000-%#010lx]\n",
930 (max_pfn_mapped<<PAGE_SHIFT) - 1);
72d7c3b3 931
084ee1c6 932 setup_real_mode();
893f38d1 933
854c879f
PE
934 init_gbpages();
935
22ddfcaa 936 init_mem_mapping();
f361a450 937
72d7c3b3 938 memblock.current_limit = get_max_mapped();
0a2b9a6e 939 dma_contiguous_reserve(0);
4e29684c 940
e7b37895
YL
941 /*
942 * NOTE: On x86-32, only from this point on, fixmaps are ready for use.
943 */
944
945#ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
946 if (init_ohci1394_dma_early)
947 init_ohci1394_dma_on_all_controllers();
948#endif
162a7e75
MT
949 /* Allocate bigger log buffer */
950 setup_log_buf(1);
e7b37895 951
2ec65f8b
YL
952 reserve_initrd();
953
44280733
YL
954 reserve_crashkernel();
955
76934ed4 956 vsmp_init();
76934ed4 957
1c6e5503
YL
958 io_delay_init();
959
1c6e5503
YL
960 /*
961 * Parse the ACPI tables for possible boot-time SMP configuration.
962 */
963 acpi_boot_table_init();
1c6e5503 964
2e42060c
JS
965 early_acpi_boot_init();
966
d8fc3afc 967 initmem_init();
6f2a7536 968 memblock_find_dma_reserve();
91467bdf 969
90993cdd 970#ifdef CONFIG_KVM_GUEST
790c73f6
GOC
971 kvmclock_init();
972#endif
973
7737b215 974 x86_init.paging.pagetable_init();
f212ec4b 975
4da9484b
PA
976 if (boot_cpu_data.cpuid_level >= 0) {
977 /* A CPU has %cr4 if and only if it has CPUID */
978 mmu_cr4_features = read_cr4();
cda846f1
JS
979 if (trampoline_cr4_features)
980 *trampoline_cr4_features = mmu_cr4_features;
4da9484b
PA
981 }
982
b40827fa
BP
983#ifdef CONFIG_X86_32
984 /* sync back kernel address range */
985 clone_pgd_range(initial_page_table + KERNEL_PGD_BOUNDARY,
986 swapper_pg_dir + KERNEL_PGD_BOUNDARY,
987 KERNEL_PGD_PTRS);
988#endif
fd89a137 989
31625340
JC
990 tboot_probe();
991
76934ed4
YL
992#ifdef CONFIG_X86_64
993 map_vsyscall();
994#endif
995
1a3f239d 996 generic_apic_probe();
1da177e4 997
54ef3400 998 early_quirks();
d44647b0 999
295deae4
YL
1000 /*
1001 * Read APIC and some other early information from ACPI tables.
1002 */
1da177e4 1003 acpi_boot_init();
efafc8b2 1004 sfi_init();
a906fdaa 1005 x86_dtb_init();
04606618 1006
295deae4
YL
1007 /*
1008 * get boot-time SMP configuration:
1009 */
e0da3364
YL
1010 if (smp_found_config)
1011 get_smp_config();
76934ed4 1012
329513a3 1013 prefill_possible_map();
301e6190 1014
5f4765f9 1015 init_cpu_to_node();
5f4765f9 1016
76934ed4 1017 init_apic_mappings();
9438ef7f
IM
1018 if (x86_io_apic_ops.init)
1019 x86_io_apic_ops.init();
9d6a4d08 1020
295deae4 1021 kvm_guest_init();
1da177e4 1022
41c094fd 1023 e820_reserve_resources();
bf62f398 1024 e820_mark_nosave_regions(max_low_pfn);
1da177e4 1025
8fee697d 1026 x86_init.resources.reserve_resources();
41c094fd
YL
1027
1028 e820_setup_gap();
1029
1da177e4
LT
1030#ifdef CONFIG_VT
1031#if defined(CONFIG_VGA_CONSOLE)
1032 if (!efi_enabled || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
1033 conswitchp = &vga_con;
1034#elif defined(CONFIG_DUMMY_CONSOLE)
1035 conswitchp = &dummy_con;
1036#endif
1037#endif
6f30c1ac 1038 x86_init.oem.banner();
a2202aa2 1039
6b617e22
FT
1040 x86_init.timers.wallclock_init();
1041
a2202aa2 1042 mcheck_init();
f49aa448 1043
dc326fca 1044 arch_init_ideal_nops();
b3c869d3
JS
1045
1046 register_refined_jiffies(CLOCK_TICK_RATE);
5189c2a7
OJ
1047
1048#ifdef CONFIG_EFI
1049 /* Once setup is done above, disable efi_enabled on mismatched
1050 * firmware/kernel archtectures since there is no support for
1051 * runtime services.
1052 */
1053 if (efi_enabled && IS_ENABLED(CONFIG_X86_64) != efi_64bit) {
1054 pr_info("efi: Setup done, disabling due to 32/64-bit mismatch\n");
1055 efi_unmap_memmap();
1056 efi_enabled = 0;
1057 }
1058#endif
1da177e4 1059}
5649b7c3 1060
9be1b56a
IM
1061#ifdef CONFIG_X86_32
1062
8fee697d
TG
1063static struct resource video_ram_resource = {
1064 .name = "Video RAM area",
1065 .start = 0xa0000,
1066 .end = 0xbffff,
1067 .flags = IORESOURCE_BUSY | IORESOURCE_MEM
9be1b56a
IM
1068};
1069
8fee697d 1070void __init i386_reserve_resources(void)
9be1b56a 1071{
8fee697d
TG
1072 request_resource(&iomem_resource, &video_ram_resource);
1073 reserve_standard_io_resources();
9be1b56a
IM
1074}
1075
9be1b56a 1076#endif /* CONFIG_X86_32 */