setup_64.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179
  1. /*
  2. * Copyright (C) 1995 Linus Torvalds
  3. */
  4. /*
  5. * This file handles the architecture-dependent parts of initialization
  6. */
  7. #include <linux/errno.h>
  8. #include <linux/sched.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/stddef.h>
  12. #include <linux/unistd.h>
  13. #include <linux/ptrace.h>
  14. #include <linux/slab.h>
  15. #include <linux/user.h>
  16. #include <linux/screen_info.h>
  17. #include <linux/ioport.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/initrd.h>
  21. #include <linux/highmem.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/module.h>
  24. #include <asm/processor.h>
  25. #include <linux/console.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/crash_dump.h>
  28. #include <linux/root_dev.h>
  29. #include <linux/pci.h>
  30. #include <asm/pci-direct.h>
  31. #include <linux/efi.h>
  32. #include <linux/acpi.h>
  33. #include <linux/kallsyms.h>
  34. #include <linux/edd.h>
  35. #include <linux/iscsi_ibft.h>
  36. #include <linux/mmzone.h>
  37. #include <linux/kexec.h>
  38. #include <linux/cpufreq.h>
  39. #include <linux/dmi.h>
  40. #include <linux/dma-mapping.h>
  41. #include <linux/ctype.h>
  42. #include <linux/sort.h>
  43. #include <linux/uaccess.h>
  44. #include <linux/init_ohci1394_dma.h>
  45. #include <linux/kvm_para.h>
  46. #include <asm/mtrr.h>
  47. #include <asm/uaccess.h>
  48. #include <asm/system.h>
  49. #include <asm/vsyscall.h>
  50. #include <asm/io.h>
  51. #include <asm/smp.h>
  52. #include <asm/msr.h>
  53. #include <asm/desc.h>
  54. #include <video/edid.h>
  55. #include <asm/e820.h>
  56. #include <asm/mpspec.h>
  57. #include <asm/dma.h>
  58. #include <asm/gart.h>
  59. #include <asm/mpspec.h>
  60. #include <asm/mmu_context.h>
  61. #include <asm/proto.h>
  62. #include <asm/setup.h>
  63. #include <asm/numa.h>
  64. #include <asm/sections.h>
  65. #include <asm/dmi.h>
  66. #include <asm/cacheflush.h>
  67. #include <asm/mce.h>
  68. #include <asm/ds.h>
  69. #include <asm/topology.h>
  70. #include <asm/trampoline.h>
  71. #include <asm/pat.h>
  72. #include <mach_apic.h>
  73. #ifdef CONFIG_PARAVIRT
  74. #include <asm/paravirt.h>
  75. #else
  76. #define ARCH_SETUP
  77. #endif
  78. /*
  79. * Machine setup..
  80. */
  81. struct cpuinfo_x86 boot_cpu_data __read_mostly;
  82. EXPORT_SYMBOL(boot_cpu_data);
  83. __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
  84. unsigned long mmu_cr4_features;
  85. /* Boot loader ID as an integer, for the benefit of proc_dointvec */
  86. int bootloader_type;
  87. unsigned long saved_video_mode;
  88. int force_mwait __cpuinitdata;
  89. /*
  90. * Early DMI memory
  91. */
  92. int dmi_alloc_index;
  93. char dmi_alloc_data[DMI_MAX_DATA];
  94. /*
  95. * Setup options
  96. */
  97. struct screen_info screen_info;
  98. EXPORT_SYMBOL(screen_info);
  99. struct sys_desc_table_struct {
  100. unsigned short length;
  101. unsigned char table[0];
  102. };
  103. struct edid_info edid_info;
  104. EXPORT_SYMBOL_GPL(edid_info);
  105. extern int root_mountflags;
  106. char __initdata command_line[COMMAND_LINE_SIZE];
  107. static struct resource standard_io_resources[] = {
  108. { .name = "dma1", .start = 0x00, .end = 0x1f,
  109. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  110. { .name = "pic1", .start = 0x20, .end = 0x21,
  111. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  112. { .name = "timer0", .start = 0x40, .end = 0x43,
  113. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  114. { .name = "timer1", .start = 0x50, .end = 0x53,
  115. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  116. { .name = "keyboard", .start = 0x60, .end = 0x60,
  117. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  118. { .name = "keyboard", .start = 0x64, .end = 0x64,
  119. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  120. { .name = "dma page reg", .start = 0x80, .end = 0x8f,
  121. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  122. { .name = "pic2", .start = 0xa0, .end = 0xa1,
  123. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  124. { .name = "dma2", .start = 0xc0, .end = 0xdf,
  125. .flags = IORESOURCE_BUSY | IORESOURCE_IO },
  126. { .name = "fpu", .start = 0xf0, .end = 0xff,
  127. .flags = IORESOURCE_BUSY | IORESOURCE_IO }
  128. };
  129. #define IORESOURCE_RAM (IORESOURCE_BUSY | IORESOURCE_MEM)
  130. static struct resource data_resource = {
  131. .name = "Kernel data",
  132. .start = 0,
  133. .end = 0,
  134. .flags = IORESOURCE_RAM,
  135. };
  136. static struct resource code_resource = {
  137. .name = "Kernel code",
  138. .start = 0,
  139. .end = 0,
  140. .flags = IORESOURCE_RAM,
  141. };
  142. static struct resource bss_resource = {
  143. .name = "Kernel bss",
  144. .start = 0,
  145. .end = 0,
  146. .flags = IORESOURCE_RAM,
  147. };
  148. static void __cpuinit early_identify_cpu(struct cpuinfo_x86 *c);
  149. #ifdef CONFIG_PROC_VMCORE
  150. /* elfcorehdr= specifies the location of elf core header
  151. * stored by the crashed kernel. This option will be passed
  152. * by kexec loader to the capture kernel.
  153. */
  154. static int __init setup_elfcorehdr(char *arg)
  155. {
  156. char *end;
  157. if (!arg)
  158. return -EINVAL;
  159. elfcorehdr_addr = memparse(arg, &end);
  160. return end > arg ? 0 : -EINVAL;
  161. }
  162. early_param("elfcorehdr", setup_elfcorehdr);
  163. #endif
  164. #ifndef CONFIG_NUMA
  165. static void __init
  166. contig_initmem_init(unsigned long start_pfn, unsigned long end_pfn)
  167. {
  168. unsigned long bootmap_size, bootmap;
  169. bootmap_size = bootmem_bootmap_pages(end_pfn)<<PAGE_SHIFT;
  170. bootmap = find_e820_area(0, end_pfn<<PAGE_SHIFT, bootmap_size,
  171. PAGE_SIZE);
  172. if (bootmap == -1L)
  173. panic("Cannot find bootmem map of size %ld\n", bootmap_size);
  174. bootmap_size = init_bootmem(bootmap >> PAGE_SHIFT, end_pfn);
  175. e820_register_active_regions(0, start_pfn, end_pfn);
  176. free_bootmem_with_active_regions(0, end_pfn);
  177. early_res_to_bootmem(0, end_pfn<<PAGE_SHIFT);
  178. reserve_bootmem(bootmap, bootmap_size, BOOTMEM_DEFAULT);
  179. }
  180. #endif
  181. #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
  182. struct edd edd;
  183. #ifdef CONFIG_EDD_MODULE
  184. EXPORT_SYMBOL(edd);
  185. #endif
  186. /**
  187. * copy_edd() - Copy the BIOS EDD information
  188. * from boot_params into a safe place.
  189. *
  190. */
  191. static inline void copy_edd(void)
  192. {
  193. memcpy(edd.mbr_signature, boot_params.edd_mbr_sig_buffer,
  194. sizeof(edd.mbr_signature));
  195. memcpy(edd.edd_info, boot_params.eddbuf, sizeof(edd.edd_info));
  196. edd.mbr_signature_nr = boot_params.edd_mbr_sig_buf_entries;
  197. edd.edd_info_nr = boot_params.eddbuf_entries;
  198. }
  199. #else
  200. static inline void copy_edd(void)
  201. {
  202. }
  203. #endif
  204. #ifdef CONFIG_KEXEC
  205. static void __init reserve_crashkernel(void)
  206. {
  207. unsigned long long total_mem;
  208. unsigned long long crash_size, crash_base;
  209. int ret;
  210. total_mem = ((unsigned long long)max_low_pfn - min_low_pfn) << PAGE_SHIFT;
  211. ret = parse_crashkernel(boot_command_line, total_mem,
  212. &crash_size, &crash_base);
  213. if (ret == 0 && crash_size) {
  214. if (crash_base <= 0) {
  215. printk(KERN_INFO "crashkernel reservation failed - "
  216. "you have to specify a base address\n");
  217. return;
  218. }
  219. if (reserve_bootmem(crash_base, crash_size,
  220. BOOTMEM_EXCLUSIVE) < 0) {
  221. printk(KERN_INFO "crashkernel reservation failed - "
  222. "memory is in use\n");
  223. return;
  224. }
  225. printk(KERN_INFO "Reserving %ldMB of memory at %ldMB "
  226. "for crashkernel (System RAM: %ldMB)\n",
  227. (unsigned long)(crash_size >> 20),
  228. (unsigned long)(crash_base >> 20),
  229. (unsigned long)(total_mem >> 20));
  230. crashk_res.start = crash_base;
  231. crashk_res.end = crash_base + crash_size - 1;
  232. insert_resource(&iomem_resource, &crashk_res);
  233. }
  234. }
  235. #else
  236. static inline void __init reserve_crashkernel(void)
  237. {}
  238. #endif
  239. /* Overridden in paravirt.c if CONFIG_PARAVIRT */
  240. void __attribute__((weak)) __init memory_setup(void)
  241. {
  242. machine_specific_memory_setup();
  243. }
  244. #ifdef CONFIG_PCI_MMCONFIG
  245. extern void __cpuinit fam10h_check_enable_mmcfg(void);
  246. extern void __init check_enable_amd_mmconf_dmi(void);
  247. #else
  248. void __cpuinit fam10h_check_enable_mmcfg(void)
  249. {
  250. }
  251. void __init check_enable_amd_mmconf_dmi(void)
  252. {
  253. }
  254. #endif
  255. /*
  256. * setup_arch - architecture-specific boot-time initializations
  257. *
  258. * Note: On x86_64, fixmaps are ready for use even before this is called.
  259. */
  260. void __init setup_arch(char **cmdline_p)
  261. {
  262. unsigned i;
  263. printk(KERN_INFO "Command line: %s\n", boot_command_line);
  264. ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
  265. screen_info = boot_params.screen_info;
  266. edid_info = boot_params.edid_info;
  267. saved_video_mode = boot_params.hdr.vid_mode;
  268. bootloader_type = boot_params.hdr.type_of_loader;
  269. #ifdef CONFIG_BLK_DEV_RAM
  270. rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
  271. rd_prompt = ((boot_params.hdr.ram_size & RAMDISK_PROMPT_FLAG) != 0);
  272. rd_doload = ((boot_params.hdr.ram_size & RAMDISK_LOAD_FLAG) != 0);
  273. #endif
  274. #ifdef CONFIG_EFI
  275. if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
  276. "EL64", 4)) {
  277. efi_enabled = 1;
  278. efi_reserve_early();
  279. }
  280. #endif
  281. ARCH_SETUP
  282. memory_setup();
  283. copy_edd();
  284. if (!boot_params.hdr.root_flags)
  285. root_mountflags &= ~MS_RDONLY;
  286. init_mm.start_code = (unsigned long) &_text;
  287. init_mm.end_code = (unsigned long) &_etext;
  288. init_mm.end_data = (unsigned long) &_edata;
  289. init_mm.brk = (unsigned long) &_end;
  290. code_resource.start = virt_to_phys(&_text);
  291. code_resource.end = virt_to_phys(&_etext)-1;
  292. data_resource.start = virt_to_phys(&_etext);
  293. data_resource.end = virt_to_phys(&_edata)-1;
  294. bss_resource.start = virt_to_phys(&__bss_start);
  295. bss_resource.end = virt_to_phys(&__bss_stop)-1;
  296. early_identify_cpu(&boot_cpu_data);
  297. strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
  298. *cmdline_p = command_line;
  299. parse_setup_data();
  300. parse_early_param();
  301. #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
  302. if (init_ohci1394_dma_early)
  303. init_ohci1394_dma_on_all_controllers();
  304. #endif
  305. finish_e820_parsing();
  306. /* after parse_early_param, so could debug it */
  307. insert_resource(&iomem_resource, &code_resource);
  308. insert_resource(&iomem_resource, &data_resource);
  309. insert_resource(&iomem_resource, &bss_resource);
  310. early_gart_iommu_check();
  311. e820_register_active_regions(0, 0, -1UL);
  312. /*
  313. * partially used pages are not usable - thus
  314. * we are rounding upwards:
  315. */
  316. end_pfn = e820_end_of_ram();
  317. /* pre allocte 4k for mptable mpc */
  318. early_reserve_e820_mpc_new();
  319. /* update e820 for memory not covered by WB MTRRs */
  320. mtrr_bp_init();
  321. if (mtrr_trim_uncached_memory(end_pfn)) {
  322. e820_register_active_regions(0, 0, -1UL);
  323. end_pfn = e820_end_of_ram();
  324. }
  325. num_physpages = end_pfn;
  326. check_efer();
  327. max_pfn_mapped = init_memory_mapping(0, (end_pfn << PAGE_SHIFT));
  328. if (efi_enabled)
  329. efi_init();
  330. vsmp_init();
  331. dmi_scan_machine();
  332. io_delay_init();
  333. #ifdef CONFIG_KVM_CLOCK
  334. kvmclock_init();
  335. #endif
  336. #ifdef CONFIG_SMP
  337. /* setup to use the early static init tables during kernel startup */
  338. x86_cpu_to_apicid_early_ptr = (void *)x86_cpu_to_apicid_init;
  339. x86_bios_cpu_apicid_early_ptr = (void *)x86_bios_cpu_apicid_init;
  340. #ifdef CONFIG_NUMA
  341. x86_cpu_to_node_map_early_ptr = (void *)x86_cpu_to_node_map_init;
  342. #endif
  343. #endif
  344. #ifdef CONFIG_ACPI
  345. /*
  346. * Initialize the ACPI boot-time table parser (gets the RSDP and SDT).
  347. * Call this early for SRAT node setup.
  348. */
  349. acpi_boot_table_init();
  350. #endif
  351. /* How many end-of-memory variables you have, grandma! */
  352. max_low_pfn = end_pfn;
  353. max_pfn = end_pfn;
  354. high_memory = (void *)__va(end_pfn * PAGE_SIZE - 1) + 1;
  355. /* Remove active ranges so rediscovery with NUMA-awareness happens */
  356. remove_all_active_ranges();
  357. #ifdef CONFIG_ACPI_NUMA
  358. /*
  359. * Parse SRAT to discover nodes.
  360. */
  361. acpi_numa_init();
  362. #endif
  363. #ifdef CONFIG_NUMA
  364. numa_initmem_init(0, end_pfn);
  365. #else
  366. contig_initmem_init(0, end_pfn);
  367. #endif
  368. dma32_reserve_bootmem();
  369. #ifdef CONFIG_ACPI_SLEEP
  370. /*
  371. * Reserve low memory region for sleep support.
  372. */
  373. acpi_reserve_bootmem();
  374. #endif
  375. #ifdef CONFIG_X86_MPPARSE
  376. /*
  377. * Find and reserve possible boot-time SMP configuration:
  378. */
  379. find_smp_config();
  380. #endif
  381. #ifdef CONFIG_BLK_DEV_INITRD
  382. if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
  383. unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
  384. unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
  385. unsigned long ramdisk_end = ramdisk_image + ramdisk_size;
  386. unsigned long end_of_mem = end_pfn << PAGE_SHIFT;
  387. if (ramdisk_end <= end_of_mem) {
  388. /*
  389. * don't need to reserve again, already reserved early
  390. * in x86_64_start_kernel, and early_res_to_bootmem
  391. * convert that to reserved in bootmem
  392. */
  393. initrd_start = ramdisk_image + PAGE_OFFSET;
  394. initrd_end = initrd_start+ramdisk_size;
  395. } else {
  396. free_bootmem(ramdisk_image, ramdisk_size);
  397. printk(KERN_ERR "initrd extends beyond end of memory "
  398. "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
  399. ramdisk_end, end_of_mem);
  400. initrd_start = 0;
  401. }
  402. }
  403. #endif
  404. reserve_crashkernel();
  405. reserve_ibft_region();
  406. paging_init();
  407. map_vsyscall();
  408. early_quirks();
  409. #ifdef CONFIG_ACPI
  410. /*
  411. * Read APIC and some other early information from ACPI tables.
  412. */
  413. acpi_boot_init();
  414. #endif
  415. init_cpu_to_node();
  416. #ifdef CONFIG_X86_MPPARSE
  417. /*
  418. * get boot-time SMP configuration:
  419. */
  420. if (smp_found_config)
  421. get_smp_config();
  422. #endif
  423. init_apic_mappings();
  424. ioapic_init_mappings();
  425. kvm_guest_init();
  426. /*
  427. * We trust e820 completely. No explicit ROM probing in memory.
  428. */
  429. e820_reserve_resources();
  430. e820_mark_nosave_regions(end_pfn);
  431. /* request I/O space for devices used on all i[345]86 PCs */
  432. for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
  433. request_resource(&ioport_resource, &standard_io_resources[i]);
  434. e820_setup_gap();
  435. #ifdef CONFIG_VT
  436. #if defined(CONFIG_VGA_CONSOLE)
  437. if (!efi_enabled || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
  438. conswitchp = &vga_con;
  439. #elif defined(CONFIG_DUMMY_CONSOLE)
  440. conswitchp = &dummy_con;
  441. #endif
  442. #endif
  443. /* do this before identify_cpu for boot cpu */
  444. check_enable_amd_mmconf_dmi();
  445. }
  446. static int __cpuinit get_model_name(struct cpuinfo_x86 *c)
  447. {
  448. unsigned int *v;
  449. if (c->extended_cpuid_level < 0x80000004)
  450. return 0;
  451. v = (unsigned int *) c->x86_model_id;
  452. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  453. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  454. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  455. c->x86_model_id[48] = 0;
  456. return 1;
  457. }
  458. static void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  459. {
  460. unsigned int n, dummy, eax, ebx, ecx, edx;
  461. n = c->extended_cpuid_level;
  462. if (n >= 0x80000005) {
  463. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  464. printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), "
  465. "D cache %dK (%d bytes/line)\n",
  466. edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
  467. c->x86_cache_size = (ecx>>24) + (edx>>24);
  468. /* On K8 L1 TLB is inclusive, so don't count it */
  469. c->x86_tlbsize = 0;
  470. }
  471. if (n >= 0x80000006) {
  472. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  473. ecx = cpuid_ecx(0x80000006);
  474. c->x86_cache_size = ecx >> 16;
  475. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  476. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  477. c->x86_cache_size, ecx & 0xFF);
  478. }
  479. if (n >= 0x80000008) {
  480. cpuid(0x80000008, &eax, &dummy, &dummy, &dummy);
  481. c->x86_virt_bits = (eax >> 8) & 0xff;
  482. c->x86_phys_bits = eax & 0xff;
  483. }
  484. }
  485. #ifdef CONFIG_NUMA
  486. static int __cpuinit nearby_node(int apicid)
  487. {
  488. int i, node;
  489. for (i = apicid - 1; i >= 0; i--) {
  490. node = apicid_to_node[i];
  491. if (node != NUMA_NO_NODE && node_online(node))
  492. return node;
  493. }
  494. for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
  495. node = apicid_to_node[i];
  496. if (node != NUMA_NO_NODE && node_online(node))
  497. return node;
  498. }
  499. return first_node(node_online_map); /* Shouldn't happen */
  500. }
  501. #endif
  502. /*
  503. * On a AMD dual core setup the lower bits of the APIC id distingush the cores.
  504. * Assumes number of cores is a power of two.
  505. */
  506. static void __cpuinit amd_detect_cmp(struct cpuinfo_x86 *c)
  507. {
  508. #ifdef CONFIG_SMP
  509. unsigned bits;
  510. #ifdef CONFIG_NUMA
  511. int cpu = smp_processor_id();
  512. int node = 0;
  513. unsigned apicid = hard_smp_processor_id();
  514. #endif
  515. bits = c->x86_coreid_bits;
  516. /* Low order bits define the core id (index of core in socket) */
  517. c->cpu_core_id = c->initial_apicid & ((1 << bits)-1);
  518. /* Convert the initial APIC ID into the socket ID */
  519. c->phys_proc_id = c->initial_apicid >> bits;
  520. #ifdef CONFIG_NUMA
  521. node = c->phys_proc_id;
  522. if (apicid_to_node[apicid] != NUMA_NO_NODE)
  523. node = apicid_to_node[apicid];
  524. if (!node_online(node)) {
  525. /* Two possibilities here:
  526. - The CPU is missing memory and no node was created.
  527. In that case try picking one from a nearby CPU
  528. - The APIC IDs differ from the HyperTransport node IDs
  529. which the K8 northbridge parsing fills in.
  530. Assume they are all increased by a constant offset,
  531. but in the same order as the HT nodeids.
  532. If that doesn't result in a usable node fall back to the
  533. path for the previous case. */
  534. int ht_nodeid = c->initial_apicid;
  535. if (ht_nodeid >= 0 &&
  536. apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
  537. node = apicid_to_node[ht_nodeid];
  538. /* Pick a nearby node */
  539. if (!node_online(node))
  540. node = nearby_node(apicid);
  541. }
  542. numa_set_node(cpu, node);
  543. printk(KERN_INFO "CPU %d/%x -> Node %d\n", cpu, apicid, node);
  544. #endif
  545. #endif
  546. }
  547. static void __cpuinit early_init_amd_mc(struct cpuinfo_x86 *c)
  548. {
  549. #ifdef CONFIG_SMP
  550. unsigned bits, ecx;
  551. /* Multi core CPU? */
  552. if (c->extended_cpuid_level < 0x80000008)
  553. return;
  554. ecx = cpuid_ecx(0x80000008);
  555. c->x86_max_cores = (ecx & 0xff) + 1;
  556. /* CPU telling us the core id bits shift? */
  557. bits = (ecx >> 12) & 0xF;
  558. /* Otherwise recompute */
  559. if (bits == 0) {
  560. while ((1 << bits) < c->x86_max_cores)
  561. bits++;
  562. }
  563. c->x86_coreid_bits = bits;
  564. #endif
  565. }
  566. #define ENABLE_C1E_MASK 0x18000000
  567. #define CPUID_PROCESSOR_SIGNATURE 1
  568. #define CPUID_XFAM 0x0ff00000
  569. #define CPUID_XFAM_K8 0x00000000
  570. #define CPUID_XFAM_10H 0x00100000
  571. #define CPUID_XFAM_11H 0x00200000
  572. #define CPUID_XMOD 0x000f0000
  573. #define CPUID_XMOD_REV_F 0x00040000
  574. /* AMD systems with C1E don't have a working lAPIC timer. Check for that. */
  575. static __cpuinit int amd_apic_timer_broken(void)
  576. {
  577. u32 lo, hi, eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  578. switch (eax & CPUID_XFAM) {
  579. case CPUID_XFAM_K8:
  580. if ((eax & CPUID_XMOD) < CPUID_XMOD_REV_F)
  581. break;
  582. case CPUID_XFAM_10H:
  583. case CPUID_XFAM_11H:
  584. rdmsr(MSR_K8_ENABLE_C1E, lo, hi);
  585. if (lo & ENABLE_C1E_MASK)
  586. return 1;
  587. break;
  588. default:
  589. /* err on the side of caution */
  590. return 1;
  591. }
  592. return 0;
  593. }
  594. static void __cpuinit early_init_amd(struct cpuinfo_x86 *c)
  595. {
  596. early_init_amd_mc(c);
  597. /* c->x86_power is 8000_0007 edx. Bit 8 is constant TSC */
  598. if (c->x86_power & (1<<8))
  599. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  600. }
  601. static void __cpuinit init_amd(struct cpuinfo_x86 *c)
  602. {
  603. unsigned level;
  604. #ifdef CONFIG_SMP
  605. unsigned long value;
  606. /*
  607. * Disable TLB flush filter by setting HWCR.FFDIS on K8
  608. * bit 6 of msr C001_0015
  609. *
  610. * Errata 63 for SH-B3 steppings
  611. * Errata 122 for all steppings (F+ have it disabled by default)
  612. */
  613. if (c->x86 == 15) {
  614. rdmsrl(MSR_K8_HWCR, value);
  615. value |= 1 << 6;
  616. wrmsrl(MSR_K8_HWCR, value);
  617. }
  618. #endif
  619. /* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  620. 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway */
  621. clear_cpu_cap(c, 0*32+31);
  622. /* On C+ stepping K8 rep microcode works well for copy/memset */
  623. level = cpuid_eax(1);
  624. if (c->x86 == 15 && ((level >= 0x0f48 && level < 0x0f50) ||
  625. level >= 0x0f58))
  626. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  627. if (c->x86 == 0x10 || c->x86 == 0x11)
  628. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  629. /* Enable workaround for FXSAVE leak */
  630. if (c->x86 >= 6)
  631. set_cpu_cap(c, X86_FEATURE_FXSAVE_LEAK);
  632. level = get_model_name(c);
  633. if (!level) {
  634. switch (c->x86) {
  635. case 15:
  636. /* Should distinguish Models here, but this is only
  637. a fallback anyways. */
  638. strcpy(c->x86_model_id, "Hammer");
  639. break;
  640. }
  641. }
  642. display_cacheinfo(c);
  643. /* Multi core CPU? */
  644. if (c->extended_cpuid_level >= 0x80000008)
  645. amd_detect_cmp(c);
  646. if (c->extended_cpuid_level >= 0x80000006 &&
  647. (cpuid_edx(0x80000006) & 0xf000))
  648. num_cache_leaves = 4;
  649. else
  650. num_cache_leaves = 3;
  651. if (c->x86 == 0xf || c->x86 == 0x10 || c->x86 == 0x11)
  652. set_cpu_cap(c, X86_FEATURE_K8);
  653. /* MFENCE stops RDTSC speculation */
  654. set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
  655. if (c->x86 == 0x10)
  656. fam10h_check_enable_mmcfg();
  657. if (amd_apic_timer_broken())
  658. disable_apic_timer = 1;
  659. if (c == &boot_cpu_data && c->x86 >= 0xf && c->x86 <= 0x11) {
  660. unsigned long long tseg;
  661. /*
  662. * Split up direct mapping around the TSEG SMM area.
  663. * Don't do it for gbpages because there seems very little
  664. * benefit in doing so.
  665. */
  666. if (!rdmsrl_safe(MSR_K8_TSEG_ADDR, &tseg) &&
  667. (tseg >> PMD_SHIFT) < (max_pfn_mapped >> (PMD_SHIFT-PAGE_SHIFT)))
  668. set_memory_4k((unsigned long)__va(tseg), 1);
  669. }
  670. }
  671. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  672. {
  673. #ifdef CONFIG_SMP
  674. u32 eax, ebx, ecx, edx;
  675. int index_msb, core_bits;
  676. cpuid(1, &eax, &ebx, &ecx, &edx);
  677. if (!cpu_has(c, X86_FEATURE_HT))
  678. return;
  679. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  680. goto out;
  681. smp_num_siblings = (ebx & 0xff0000) >> 16;
  682. if (smp_num_siblings == 1) {
  683. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  684. } else if (smp_num_siblings > 1) {
  685. if (smp_num_siblings > NR_CPUS) {
  686. printk(KERN_WARNING "CPU: Unsupported number of "
  687. "siblings %d", smp_num_siblings);
  688. smp_num_siblings = 1;
  689. return;
  690. }
  691. index_msb = get_count_order(smp_num_siblings);
  692. c->phys_proc_id = phys_pkg_id(index_msb);
  693. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  694. index_msb = get_count_order(smp_num_siblings);
  695. core_bits = get_count_order(c->x86_max_cores);
  696. c->cpu_core_id = phys_pkg_id(index_msb) &
  697. ((1 << core_bits) - 1);
  698. }
  699. out:
  700. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  701. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  702. c->phys_proc_id);
  703. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  704. c->cpu_core_id);
  705. }
  706. #endif
  707. }
  708. /*
  709. * find out the number of processor cores on the die
  710. */
  711. static int __cpuinit intel_num_cpu_cores(struct cpuinfo_x86 *c)
  712. {
  713. unsigned int eax, t;
  714. if (c->cpuid_level < 4)
  715. return 1;
  716. cpuid_count(4, 0, &eax, &t, &t, &t);
  717. if (eax & 0x1f)
  718. return ((eax >> 26) + 1);
  719. else
  720. return 1;
  721. }
  722. static void __cpuinit srat_detect_node(void)
  723. {
  724. #ifdef CONFIG_NUMA
  725. unsigned node;
  726. int cpu = smp_processor_id();
  727. int apicid = hard_smp_processor_id();
  728. /* Don't do the funky fallback heuristics the AMD version employs
  729. for now. */
  730. node = apicid_to_node[apicid];
  731. if (node == NUMA_NO_NODE || !node_online(node))
  732. node = first_node(node_online_map);
  733. numa_set_node(cpu, node);
  734. printk(KERN_INFO "CPU %d/%x -> Node %d\n", cpu, apicid, node);
  735. #endif
  736. }
  737. static void __cpuinit early_init_intel(struct cpuinfo_x86 *c)
  738. {
  739. if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
  740. (c->x86 == 0x6 && c->x86_model >= 0x0e))
  741. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  742. }
  743. static void __cpuinit init_intel(struct cpuinfo_x86 *c)
  744. {
  745. /* Cache sizes */
  746. unsigned n;
  747. init_intel_cacheinfo(c);
  748. if (c->cpuid_level > 9) {
  749. unsigned eax = cpuid_eax(10);
  750. /* Check for version and the number of counters */
  751. if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
  752. set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
  753. }
  754. if (cpu_has_ds) {
  755. unsigned int l1, l2;
  756. rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
  757. if (!(l1 & (1<<11)))
  758. set_cpu_cap(c, X86_FEATURE_BTS);
  759. if (!(l1 & (1<<12)))
  760. set_cpu_cap(c, X86_FEATURE_PEBS);
  761. }
  762. if (cpu_has_bts)
  763. ds_init_intel(c);
  764. n = c->extended_cpuid_level;
  765. if (n >= 0x80000008) {
  766. unsigned eax = cpuid_eax(0x80000008);
  767. c->x86_virt_bits = (eax >> 8) & 0xff;
  768. c->x86_phys_bits = eax & 0xff;
  769. /* CPUID workaround for Intel 0F34 CPU */
  770. if (c->x86_vendor == X86_VENDOR_INTEL &&
  771. c->x86 == 0xF && c->x86_model == 0x3 &&
  772. c->x86_mask == 0x4)
  773. c->x86_phys_bits = 36;
  774. }
  775. if (c->x86 == 15)
  776. c->x86_cache_alignment = c->x86_clflush_size * 2;
  777. if (c->x86 == 6)
  778. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  779. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  780. c->x86_max_cores = intel_num_cpu_cores(c);
  781. srat_detect_node();
  782. }
  783. static void __cpuinit early_init_centaur(struct cpuinfo_x86 *c)
  784. {
  785. if (c->x86 == 0x6 && c->x86_model >= 0xf)
  786. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  787. }
  788. static void __cpuinit init_centaur(struct cpuinfo_x86 *c)
  789. {
  790. /* Cache sizes */
  791. unsigned n;
  792. n = c->extended_cpuid_level;
  793. if (n >= 0x80000008) {
  794. unsigned eax = cpuid_eax(0x80000008);
  795. c->x86_virt_bits = (eax >> 8) & 0xff;
  796. c->x86_phys_bits = eax & 0xff;
  797. }
  798. if (c->x86 == 0x6 && c->x86_model >= 0xf) {
  799. c->x86_cache_alignment = c->x86_clflush_size * 2;
  800. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  801. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  802. }
  803. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  804. }
  805. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  806. {
  807. char *v = c->x86_vendor_id;
  808. if (!strcmp(v, "AuthenticAMD"))
  809. c->x86_vendor = X86_VENDOR_AMD;
  810. else if (!strcmp(v, "GenuineIntel"))
  811. c->x86_vendor = X86_VENDOR_INTEL;
  812. else if (!strcmp(v, "CentaurHauls"))
  813. c->x86_vendor = X86_VENDOR_CENTAUR;
  814. else
  815. c->x86_vendor = X86_VENDOR_UNKNOWN;
  816. }
  817. /* Do some early cpuid on the boot CPU to get some parameter that are
  818. needed before check_bugs. Everything advanced is in identify_cpu
  819. below. */
  820. static void __cpuinit early_identify_cpu(struct cpuinfo_x86 *c)
  821. {
  822. u32 tfms, xlvl;
  823. c->loops_per_jiffy = loops_per_jiffy;
  824. c->x86_cache_size = -1;
  825. c->x86_vendor = X86_VENDOR_UNKNOWN;
  826. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  827. c->x86_vendor_id[0] = '\0'; /* Unset */
  828. c->x86_model_id[0] = '\0'; /* Unset */
  829. c->x86_clflush_size = 64;
  830. c->x86_cache_alignment = c->x86_clflush_size;
  831. c->x86_max_cores = 1;
  832. c->x86_coreid_bits = 0;
  833. c->extended_cpuid_level = 0;
  834. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  835. /* Get vendor name */
  836. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  837. (unsigned int *)&c->x86_vendor_id[0],
  838. (unsigned int *)&c->x86_vendor_id[8],
  839. (unsigned int *)&c->x86_vendor_id[4]);
  840. get_cpu_vendor(c);
  841. /* Initialize the standard set of capabilities */
  842. /* Note that the vendor-specific code below might override */
  843. /* Intel-defined flags: level 0x00000001 */
  844. if (c->cpuid_level >= 0x00000001) {
  845. __u32 misc;
  846. cpuid(0x00000001, &tfms, &misc, &c->x86_capability[4],
  847. &c->x86_capability[0]);
  848. c->x86 = (tfms >> 8) & 0xf;
  849. c->x86_model = (tfms >> 4) & 0xf;
  850. c->x86_mask = tfms & 0xf;
  851. if (c->x86 == 0xf)
  852. c->x86 += (tfms >> 20) & 0xff;
  853. if (c->x86 >= 0x6)
  854. c->x86_model += ((tfms >> 16) & 0xF) << 4;
  855. if (test_cpu_cap(c, X86_FEATURE_CLFLSH))
  856. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  857. } else {
  858. /* Have CPUID level 0 only - unheard of */
  859. c->x86 = 4;
  860. }
  861. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xff;
  862. #ifdef CONFIG_SMP
  863. c->phys_proc_id = c->initial_apicid;
  864. #endif
  865. /* AMD-defined flags: level 0x80000001 */
  866. xlvl = cpuid_eax(0x80000000);
  867. c->extended_cpuid_level = xlvl;
  868. if ((xlvl & 0xffff0000) == 0x80000000) {
  869. if (xlvl >= 0x80000001) {
  870. c->x86_capability[1] = cpuid_edx(0x80000001);
  871. c->x86_capability[6] = cpuid_ecx(0x80000001);
  872. }
  873. if (xlvl >= 0x80000004)
  874. get_model_name(c); /* Default name */
  875. }
  876. /* Transmeta-defined flags: level 0x80860001 */
  877. xlvl = cpuid_eax(0x80860000);
  878. if ((xlvl & 0xffff0000) == 0x80860000) {
  879. /* Don't set x86_cpuid_level here for now to not confuse. */
  880. if (xlvl >= 0x80860001)
  881. c->x86_capability[2] = cpuid_edx(0x80860001);
  882. }
  883. c->extended_cpuid_level = cpuid_eax(0x80000000);
  884. if (c->extended_cpuid_level >= 0x80000007)
  885. c->x86_power = cpuid_edx(0x80000007);
  886. switch (c->x86_vendor) {
  887. case X86_VENDOR_AMD:
  888. early_init_amd(c);
  889. break;
  890. case X86_VENDOR_INTEL:
  891. early_init_intel(c);
  892. break;
  893. case X86_VENDOR_CENTAUR:
  894. early_init_centaur(c);
  895. break;
  896. }
  897. validate_pat_support(c);
  898. }
  899. /*
  900. * This does the hard work of actually picking apart the CPU stuff...
  901. */
  902. void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  903. {
  904. int i;
  905. early_identify_cpu(c);
  906. init_scattered_cpuid_features(c);
  907. c->apicid = phys_pkg_id(0);
  908. /*
  909. * Vendor-specific initialization. In this section we
  910. * canonicalize the feature flags, meaning if there are
  911. * features a certain CPU supports which CPUID doesn't
  912. * tell us, CPUID claiming incorrect flags, or other bugs,
  913. * we handle them here.
  914. *
  915. * At the end of this section, c->x86_capability better
  916. * indicate the features this CPU genuinely supports!
  917. */
  918. switch (c->x86_vendor) {
  919. case X86_VENDOR_AMD:
  920. init_amd(c);
  921. break;
  922. case X86_VENDOR_INTEL:
  923. init_intel(c);
  924. break;
  925. case X86_VENDOR_CENTAUR:
  926. init_centaur(c);
  927. break;
  928. case X86_VENDOR_UNKNOWN:
  929. default:
  930. display_cacheinfo(c);
  931. break;
  932. }
  933. detect_ht(c);
  934. /*
  935. * On SMP, boot_cpu_data holds the common feature set between
  936. * all CPUs; so make sure that we indicate which features are
  937. * common between the CPUs. The first time this routine gets
  938. * executed, c == &boot_cpu_data.
  939. */
  940. if (c != &boot_cpu_data) {
  941. /* AND the already accumulated flags with these */
  942. for (i = 0; i < NCAPINTS; i++)
  943. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  944. }
  945. /* Clear all flags overriden by options */
  946. for (i = 0; i < NCAPINTS; i++)
  947. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  948. #ifdef CONFIG_X86_MCE
  949. mcheck_init(c);
  950. #endif
  951. select_idle_routine(c);
  952. #ifdef CONFIG_NUMA
  953. numa_add_cpu(smp_processor_id());
  954. #endif
  955. }
  956. void __cpuinit identify_boot_cpu(void)
  957. {
  958. identify_cpu(&boot_cpu_data);
  959. }
  960. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  961. {
  962. BUG_ON(c == &boot_cpu_data);
  963. identify_cpu(c);
  964. mtrr_ap_init();
  965. }
  966. static __init int setup_noclflush(char *arg)
  967. {
  968. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  969. return 1;
  970. }
  971. __setup("noclflush", setup_noclflush);
  972. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  973. {
  974. if (c->x86_model_id[0])
  975. printk(KERN_CONT "%s", c->x86_model_id);
  976. if (c->x86_mask || c->cpuid_level >= 0)
  977. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  978. else
  979. printk(KERN_CONT "\n");
  980. }
  981. static __init int setup_disablecpuid(char *arg)
  982. {
  983. int bit;
  984. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  985. setup_clear_cpu_cap(bit);
  986. else
  987. return 0;
  988. return 1;
  989. }
  990. __setup("clearcpuid=", setup_disablecpuid);