setup_64.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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_generic(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. #ifdef CONFIG_PCI_MMCONFIG
  240. extern void __cpuinit fam10h_check_enable_mmcfg(void);
  241. extern void __init check_enable_amd_mmconf_dmi(void);
  242. #else
  243. void __cpuinit fam10h_check_enable_mmcfg(void)
  244. {
  245. }
  246. void __init check_enable_amd_mmconf_dmi(void)
  247. {
  248. }
  249. #endif
  250. /*
  251. * setup_arch - architecture-specific boot-time initializations
  252. *
  253. * Note: On x86_64, fixmaps are ready for use even before this is called.
  254. */
  255. void __init setup_arch(char **cmdline_p)
  256. {
  257. unsigned i;
  258. printk(KERN_INFO "Command line: %s\n", boot_command_line);
  259. ROOT_DEV = old_decode_dev(boot_params.hdr.root_dev);
  260. screen_info = boot_params.screen_info;
  261. edid_info = boot_params.edid_info;
  262. saved_video_mode = boot_params.hdr.vid_mode;
  263. bootloader_type = boot_params.hdr.type_of_loader;
  264. #ifdef CONFIG_BLK_DEV_RAM
  265. rd_image_start = boot_params.hdr.ram_size & RAMDISK_IMAGE_START_MASK;
  266. rd_prompt = ((boot_params.hdr.ram_size & RAMDISK_PROMPT_FLAG) != 0);
  267. rd_doload = ((boot_params.hdr.ram_size & RAMDISK_LOAD_FLAG) != 0);
  268. #endif
  269. #ifdef CONFIG_EFI
  270. if (!strncmp((char *)&boot_params.efi_info.efi_loader_signature,
  271. "EL64", 4)) {
  272. efi_enabled = 1;
  273. efi_reserve_early();
  274. }
  275. #endif
  276. ARCH_SETUP
  277. setup_memory_map();
  278. copy_edd();
  279. if (!boot_params.hdr.root_flags)
  280. root_mountflags &= ~MS_RDONLY;
  281. init_mm.start_code = (unsigned long) &_text;
  282. init_mm.end_code = (unsigned long) &_etext;
  283. init_mm.end_data = (unsigned long) &_edata;
  284. init_mm.brk = (unsigned long) &_end;
  285. code_resource.start = virt_to_phys(&_text);
  286. code_resource.end = virt_to_phys(&_etext)-1;
  287. data_resource.start = virt_to_phys(&_etext);
  288. data_resource.end = virt_to_phys(&_edata)-1;
  289. bss_resource.start = virt_to_phys(&__bss_start);
  290. bss_resource.end = virt_to_phys(&__bss_stop)-1;
  291. early_identify_cpu(&boot_cpu_data);
  292. strlcpy(command_line, boot_command_line, COMMAND_LINE_SIZE);
  293. *cmdline_p = command_line;
  294. parse_setup_data();
  295. parse_early_param();
  296. #ifdef CONFIG_PROVIDE_OHCI1394_DMA_INIT
  297. if (init_ohci1394_dma_early)
  298. init_ohci1394_dma_on_all_controllers();
  299. #endif
  300. finish_e820_parsing();
  301. /* after parse_early_param, so could debug it */
  302. insert_resource(&iomem_resource, &code_resource);
  303. insert_resource(&iomem_resource, &data_resource);
  304. insert_resource(&iomem_resource, &bss_resource);
  305. early_gart_iommu_check();
  306. e820_register_active_regions(0, 0, -1UL);
  307. /*
  308. * partially used pages are not usable - thus
  309. * we are rounding upwards:
  310. */
  311. end_pfn = e820_end_of_ram();
  312. /* pre allocte 4k for mptable mpc */
  313. early_reserve_e820_mpc_new();
  314. /* update e820 for memory not covered by WB MTRRs */
  315. mtrr_bp_init();
  316. if (mtrr_trim_uncached_memory(end_pfn)) {
  317. remove_all_active_ranges();
  318. e820_register_active_regions(0, 0, -1UL);
  319. end_pfn = e820_end_of_ram();
  320. }
  321. num_physpages = end_pfn;
  322. check_efer();
  323. max_pfn_mapped = init_memory_mapping(0, (end_pfn << PAGE_SHIFT));
  324. if (efi_enabled)
  325. efi_init();
  326. vsmp_init();
  327. dmi_scan_machine();
  328. io_delay_init();
  329. #ifdef CONFIG_KVM_CLOCK
  330. kvmclock_init();
  331. #endif
  332. #ifdef CONFIG_SMP
  333. /* setup to use the early static init tables during kernel startup */
  334. x86_cpu_to_apicid_early_ptr = (void *)x86_cpu_to_apicid_init;
  335. x86_bios_cpu_apicid_early_ptr = (void *)x86_bios_cpu_apicid_init;
  336. #ifdef CONFIG_NUMA
  337. x86_cpu_to_node_map_early_ptr = (void *)x86_cpu_to_node_map_init;
  338. #endif
  339. #endif
  340. #ifdef CONFIG_ACPI
  341. /*
  342. * Initialize the ACPI boot-time table parser (gets the RSDP and SDT).
  343. * Call this early for SRAT node setup.
  344. */
  345. acpi_boot_table_init();
  346. #endif
  347. /* How many end-of-memory variables you have, grandma! */
  348. max_low_pfn = end_pfn;
  349. max_pfn = end_pfn;
  350. high_memory = (void *)__va(end_pfn * PAGE_SIZE - 1) + 1;
  351. /* Remove active ranges so rediscovery with NUMA-awareness happens */
  352. remove_all_active_ranges();
  353. #ifdef CONFIG_ACPI_NUMA
  354. /*
  355. * Parse SRAT to discover nodes.
  356. */
  357. acpi_numa_init();
  358. #endif
  359. #ifdef CONFIG_NUMA
  360. numa_initmem_init(0, end_pfn);
  361. #else
  362. contig_initmem_init(0, end_pfn);
  363. #endif
  364. dma32_reserve_bootmem();
  365. #ifdef CONFIG_ACPI_SLEEP
  366. /*
  367. * Reserve low memory region for sleep support.
  368. */
  369. acpi_reserve_bootmem();
  370. #endif
  371. #ifdef CONFIG_X86_MPPARSE
  372. /*
  373. * Find and reserve possible boot-time SMP configuration:
  374. */
  375. find_smp_config();
  376. #endif
  377. #ifdef CONFIG_BLK_DEV_INITRD
  378. if (boot_params.hdr.type_of_loader && boot_params.hdr.ramdisk_image) {
  379. unsigned long ramdisk_image = boot_params.hdr.ramdisk_image;
  380. unsigned long ramdisk_size = boot_params.hdr.ramdisk_size;
  381. unsigned long ramdisk_end = ramdisk_image + ramdisk_size;
  382. unsigned long end_of_mem = end_pfn << PAGE_SHIFT;
  383. if (ramdisk_end <= end_of_mem) {
  384. /*
  385. * don't need to reserve again, already reserved early
  386. * in x86_64_start_kernel, and early_res_to_bootmem
  387. * convert that to reserved in bootmem
  388. */
  389. initrd_start = ramdisk_image + PAGE_OFFSET;
  390. initrd_end = initrd_start+ramdisk_size;
  391. } else {
  392. free_bootmem(ramdisk_image, ramdisk_size);
  393. printk(KERN_ERR "initrd extends beyond end of memory "
  394. "(0x%08lx > 0x%08lx)\ndisabling initrd\n",
  395. ramdisk_end, end_of_mem);
  396. initrd_start = 0;
  397. }
  398. }
  399. #endif
  400. reserve_crashkernel();
  401. reserve_ibft_region();
  402. paging_init();
  403. map_vsyscall();
  404. early_quirks();
  405. #ifdef CONFIG_ACPI
  406. /*
  407. * Read APIC and some other early information from ACPI tables.
  408. */
  409. acpi_boot_init();
  410. #endif
  411. init_cpu_to_node();
  412. #ifdef CONFIG_X86_MPPARSE
  413. /*
  414. * get boot-time SMP configuration:
  415. */
  416. if (smp_found_config)
  417. get_smp_config();
  418. #endif
  419. init_apic_mappings();
  420. ioapic_init_mappings();
  421. kvm_guest_init();
  422. /*
  423. * We trust e820 completely. No explicit ROM probing in memory.
  424. */
  425. e820_reserve_resources();
  426. e820_mark_nosave_regions(end_pfn);
  427. /* request I/O space for devices used on all i[345]86 PCs */
  428. for (i = 0; i < ARRAY_SIZE(standard_io_resources); i++)
  429. request_resource(&ioport_resource, &standard_io_resources[i]);
  430. e820_setup_gap();
  431. #ifdef CONFIG_VT
  432. #if defined(CONFIG_VGA_CONSOLE)
  433. if (!efi_enabled || (efi_mem_type(0xa0000) != EFI_CONVENTIONAL_MEMORY))
  434. conswitchp = &vga_con;
  435. #elif defined(CONFIG_DUMMY_CONSOLE)
  436. conswitchp = &dummy_con;
  437. #endif
  438. #endif
  439. /* do this before identify_cpu for boot cpu */
  440. check_enable_amd_mmconf_dmi();
  441. }
  442. static int __cpuinit get_model_name(struct cpuinfo_x86 *c)
  443. {
  444. unsigned int *v;
  445. if (c->extended_cpuid_level < 0x80000004)
  446. return 0;
  447. v = (unsigned int *) c->x86_model_id;
  448. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  449. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  450. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  451. c->x86_model_id[48] = 0;
  452. return 1;
  453. }
  454. static void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  455. {
  456. unsigned int n, dummy, eax, ebx, ecx, edx;
  457. n = c->extended_cpuid_level;
  458. if (n >= 0x80000005) {
  459. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  460. printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), "
  461. "D cache %dK (%d bytes/line)\n",
  462. edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
  463. c->x86_cache_size = (ecx>>24) + (edx>>24);
  464. /* On K8 L1 TLB is inclusive, so don't count it */
  465. c->x86_tlbsize = 0;
  466. }
  467. if (n >= 0x80000006) {
  468. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  469. ecx = cpuid_ecx(0x80000006);
  470. c->x86_cache_size = ecx >> 16;
  471. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  472. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  473. c->x86_cache_size, ecx & 0xFF);
  474. }
  475. if (n >= 0x80000008) {
  476. cpuid(0x80000008, &eax, &dummy, &dummy, &dummy);
  477. c->x86_virt_bits = (eax >> 8) & 0xff;
  478. c->x86_phys_bits = eax & 0xff;
  479. }
  480. }
  481. #ifdef CONFIG_NUMA
  482. static int __cpuinit nearby_node(int apicid)
  483. {
  484. int i, node;
  485. for (i = apicid - 1; i >= 0; i--) {
  486. node = apicid_to_node[i];
  487. if (node != NUMA_NO_NODE && node_online(node))
  488. return node;
  489. }
  490. for (i = apicid + 1; i < MAX_LOCAL_APIC; i++) {
  491. node = apicid_to_node[i];
  492. if (node != NUMA_NO_NODE && node_online(node))
  493. return node;
  494. }
  495. return first_node(node_online_map); /* Shouldn't happen */
  496. }
  497. #endif
  498. /*
  499. * On a AMD dual core setup the lower bits of the APIC id distingush the cores.
  500. * Assumes number of cores is a power of two.
  501. */
  502. static void __cpuinit amd_detect_cmp(struct cpuinfo_x86 *c)
  503. {
  504. #ifdef CONFIG_SMP
  505. unsigned bits;
  506. #ifdef CONFIG_NUMA
  507. int cpu = smp_processor_id();
  508. int node = 0;
  509. unsigned apicid = hard_smp_processor_id();
  510. #endif
  511. bits = c->x86_coreid_bits;
  512. /* Low order bits define the core id (index of core in socket) */
  513. c->cpu_core_id = c->initial_apicid & ((1 << bits)-1);
  514. /* Convert the initial APIC ID into the socket ID */
  515. c->phys_proc_id = c->initial_apicid >> bits;
  516. #ifdef CONFIG_NUMA
  517. node = c->phys_proc_id;
  518. if (apicid_to_node[apicid] != NUMA_NO_NODE)
  519. node = apicid_to_node[apicid];
  520. if (!node_online(node)) {
  521. /* Two possibilities here:
  522. - The CPU is missing memory and no node was created.
  523. In that case try picking one from a nearby CPU
  524. - The APIC IDs differ from the HyperTransport node IDs
  525. which the K8 northbridge parsing fills in.
  526. Assume they are all increased by a constant offset,
  527. but in the same order as the HT nodeids.
  528. If that doesn't result in a usable node fall back to the
  529. path for the previous case. */
  530. int ht_nodeid = c->initial_apicid;
  531. if (ht_nodeid >= 0 &&
  532. apicid_to_node[ht_nodeid] != NUMA_NO_NODE)
  533. node = apicid_to_node[ht_nodeid];
  534. /* Pick a nearby node */
  535. if (!node_online(node))
  536. node = nearby_node(apicid);
  537. }
  538. numa_set_node(cpu, node);
  539. printk(KERN_INFO "CPU %d/%x -> Node %d\n", cpu, apicid, node);
  540. #endif
  541. #endif
  542. }
  543. static void __cpuinit early_init_amd_mc(struct cpuinfo_x86 *c)
  544. {
  545. #ifdef CONFIG_SMP
  546. unsigned bits, ecx;
  547. /* Multi core CPU? */
  548. if (c->extended_cpuid_level < 0x80000008)
  549. return;
  550. ecx = cpuid_ecx(0x80000008);
  551. c->x86_max_cores = (ecx & 0xff) + 1;
  552. /* CPU telling us the core id bits shift? */
  553. bits = (ecx >> 12) & 0xF;
  554. /* Otherwise recompute */
  555. if (bits == 0) {
  556. while ((1 << bits) < c->x86_max_cores)
  557. bits++;
  558. }
  559. c->x86_coreid_bits = bits;
  560. #endif
  561. }
  562. #define ENABLE_C1E_MASK 0x18000000
  563. #define CPUID_PROCESSOR_SIGNATURE 1
  564. #define CPUID_XFAM 0x0ff00000
  565. #define CPUID_XFAM_K8 0x00000000
  566. #define CPUID_XFAM_10H 0x00100000
  567. #define CPUID_XFAM_11H 0x00200000
  568. #define CPUID_XMOD 0x000f0000
  569. #define CPUID_XMOD_REV_F 0x00040000
  570. /* AMD systems with C1E don't have a working lAPIC timer. Check for that. */
  571. static __cpuinit int amd_apic_timer_broken(void)
  572. {
  573. u32 lo, hi, eax = cpuid_eax(CPUID_PROCESSOR_SIGNATURE);
  574. switch (eax & CPUID_XFAM) {
  575. case CPUID_XFAM_K8:
  576. if ((eax & CPUID_XMOD) < CPUID_XMOD_REV_F)
  577. break;
  578. case CPUID_XFAM_10H:
  579. case CPUID_XFAM_11H:
  580. rdmsr(MSR_K8_ENABLE_C1E, lo, hi);
  581. if (lo & ENABLE_C1E_MASK)
  582. return 1;
  583. break;
  584. default:
  585. /* err on the side of caution */
  586. return 1;
  587. }
  588. return 0;
  589. }
  590. static void __cpuinit early_init_amd(struct cpuinfo_x86 *c)
  591. {
  592. early_init_amd_mc(c);
  593. /* c->x86_power is 8000_0007 edx. Bit 8 is constant TSC */
  594. if (c->x86_power & (1<<8))
  595. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  596. }
  597. static void __cpuinit init_amd(struct cpuinfo_x86 *c)
  598. {
  599. unsigned level;
  600. #ifdef CONFIG_SMP
  601. unsigned long value;
  602. /*
  603. * Disable TLB flush filter by setting HWCR.FFDIS on K8
  604. * bit 6 of msr C001_0015
  605. *
  606. * Errata 63 for SH-B3 steppings
  607. * Errata 122 for all steppings (F+ have it disabled by default)
  608. */
  609. if (c->x86 == 15) {
  610. rdmsrl(MSR_K8_HWCR, value);
  611. value |= 1 << 6;
  612. wrmsrl(MSR_K8_HWCR, value);
  613. }
  614. #endif
  615. /* Bit 31 in normal CPUID used for nonstandard 3DNow ID;
  616. 3DNow is IDd by bit 31 in extended CPUID (1*32+31) anyway */
  617. clear_cpu_cap(c, 0*32+31);
  618. /* On C+ stepping K8 rep microcode works well for copy/memset */
  619. level = cpuid_eax(1);
  620. if (c->x86 == 15 && ((level >= 0x0f48 && level < 0x0f50) ||
  621. level >= 0x0f58))
  622. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  623. if (c->x86 == 0x10 || c->x86 == 0x11)
  624. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  625. /* Enable workaround for FXSAVE leak */
  626. if (c->x86 >= 6)
  627. set_cpu_cap(c, X86_FEATURE_FXSAVE_LEAK);
  628. level = get_model_name(c);
  629. if (!level) {
  630. switch (c->x86) {
  631. case 15:
  632. /* Should distinguish Models here, but this is only
  633. a fallback anyways. */
  634. strcpy(c->x86_model_id, "Hammer");
  635. break;
  636. }
  637. }
  638. display_cacheinfo(c);
  639. /* Multi core CPU? */
  640. if (c->extended_cpuid_level >= 0x80000008)
  641. amd_detect_cmp(c);
  642. if (c->extended_cpuid_level >= 0x80000006 &&
  643. (cpuid_edx(0x80000006) & 0xf000))
  644. num_cache_leaves = 4;
  645. else
  646. num_cache_leaves = 3;
  647. if (c->x86 == 0xf || c->x86 == 0x10 || c->x86 == 0x11)
  648. set_cpu_cap(c, X86_FEATURE_K8);
  649. /* MFENCE stops RDTSC speculation */
  650. set_cpu_cap(c, X86_FEATURE_MFENCE_RDTSC);
  651. if (c->x86 == 0x10)
  652. fam10h_check_enable_mmcfg();
  653. if (amd_apic_timer_broken())
  654. disable_apic_timer = 1;
  655. if (c == &boot_cpu_data && c->x86 >= 0xf && c->x86 <= 0x11) {
  656. unsigned long long tseg;
  657. /*
  658. * Split up direct mapping around the TSEG SMM area.
  659. * Don't do it for gbpages because there seems very little
  660. * benefit in doing so.
  661. */
  662. if (!rdmsrl_safe(MSR_K8_TSEG_ADDR, &tseg) &&
  663. (tseg >> PMD_SHIFT) < (max_pfn_mapped >> (PMD_SHIFT-PAGE_SHIFT)))
  664. set_memory_4k((unsigned long)__va(tseg), 1);
  665. }
  666. }
  667. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  668. {
  669. #ifdef CONFIG_SMP
  670. u32 eax, ebx, ecx, edx;
  671. int index_msb, core_bits;
  672. cpuid(1, &eax, &ebx, &ecx, &edx);
  673. if (!cpu_has(c, X86_FEATURE_HT))
  674. return;
  675. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  676. goto out;
  677. smp_num_siblings = (ebx & 0xff0000) >> 16;
  678. if (smp_num_siblings == 1) {
  679. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  680. } else if (smp_num_siblings > 1) {
  681. if (smp_num_siblings > NR_CPUS) {
  682. printk(KERN_WARNING "CPU: Unsupported number of "
  683. "siblings %d", smp_num_siblings);
  684. smp_num_siblings = 1;
  685. return;
  686. }
  687. index_msb = get_count_order(smp_num_siblings);
  688. c->phys_proc_id = phys_pkg_id(index_msb);
  689. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  690. index_msb = get_count_order(smp_num_siblings);
  691. core_bits = get_count_order(c->x86_max_cores);
  692. c->cpu_core_id = phys_pkg_id(index_msb) &
  693. ((1 << core_bits) - 1);
  694. }
  695. out:
  696. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  697. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  698. c->phys_proc_id);
  699. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  700. c->cpu_core_id);
  701. }
  702. #endif
  703. }
  704. /*
  705. * find out the number of processor cores on the die
  706. */
  707. static int __cpuinit intel_num_cpu_cores(struct cpuinfo_x86 *c)
  708. {
  709. unsigned int eax, t;
  710. if (c->cpuid_level < 4)
  711. return 1;
  712. cpuid_count(4, 0, &eax, &t, &t, &t);
  713. if (eax & 0x1f)
  714. return ((eax >> 26) + 1);
  715. else
  716. return 1;
  717. }
  718. static void __cpuinit srat_detect_node(void)
  719. {
  720. #ifdef CONFIG_NUMA
  721. unsigned node;
  722. int cpu = smp_processor_id();
  723. int apicid = hard_smp_processor_id();
  724. /* Don't do the funky fallback heuristics the AMD version employs
  725. for now. */
  726. node = apicid_to_node[apicid];
  727. if (node == NUMA_NO_NODE || !node_online(node))
  728. node = first_node(node_online_map);
  729. numa_set_node(cpu, node);
  730. printk(KERN_INFO "CPU %d/%x -> Node %d\n", cpu, apicid, node);
  731. #endif
  732. }
  733. static void __cpuinit early_init_intel(struct cpuinfo_x86 *c)
  734. {
  735. if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
  736. (c->x86 == 0x6 && c->x86_model >= 0x0e))
  737. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  738. }
  739. static void __cpuinit init_intel(struct cpuinfo_x86 *c)
  740. {
  741. /* Cache sizes */
  742. unsigned n;
  743. init_intel_cacheinfo(c);
  744. if (c->cpuid_level > 9) {
  745. unsigned eax = cpuid_eax(10);
  746. /* Check for version and the number of counters */
  747. if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
  748. set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
  749. }
  750. if (cpu_has_ds) {
  751. unsigned int l1, l2;
  752. rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
  753. if (!(l1 & (1<<11)))
  754. set_cpu_cap(c, X86_FEATURE_BTS);
  755. if (!(l1 & (1<<12)))
  756. set_cpu_cap(c, X86_FEATURE_PEBS);
  757. }
  758. if (cpu_has_bts)
  759. ds_init_intel(c);
  760. n = c->extended_cpuid_level;
  761. if (n >= 0x80000008) {
  762. unsigned eax = cpuid_eax(0x80000008);
  763. c->x86_virt_bits = (eax >> 8) & 0xff;
  764. c->x86_phys_bits = eax & 0xff;
  765. /* CPUID workaround for Intel 0F34 CPU */
  766. if (c->x86_vendor == X86_VENDOR_INTEL &&
  767. c->x86 == 0xF && c->x86_model == 0x3 &&
  768. c->x86_mask == 0x4)
  769. c->x86_phys_bits = 36;
  770. }
  771. if (c->x86 == 15)
  772. c->x86_cache_alignment = c->x86_clflush_size * 2;
  773. if (c->x86 == 6)
  774. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  775. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  776. c->x86_max_cores = intel_num_cpu_cores(c);
  777. srat_detect_node();
  778. }
  779. static void __cpuinit early_init_centaur(struct cpuinfo_x86 *c)
  780. {
  781. if (c->x86 == 0x6 && c->x86_model >= 0xf)
  782. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  783. }
  784. static void __cpuinit init_centaur(struct cpuinfo_x86 *c)
  785. {
  786. /* Cache sizes */
  787. unsigned n;
  788. n = c->extended_cpuid_level;
  789. if (n >= 0x80000008) {
  790. unsigned eax = cpuid_eax(0x80000008);
  791. c->x86_virt_bits = (eax >> 8) & 0xff;
  792. c->x86_phys_bits = eax & 0xff;
  793. }
  794. if (c->x86 == 0x6 && c->x86_model >= 0xf) {
  795. c->x86_cache_alignment = c->x86_clflush_size * 2;
  796. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  797. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  798. }
  799. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  800. }
  801. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  802. {
  803. char *v = c->x86_vendor_id;
  804. if (!strcmp(v, "AuthenticAMD"))
  805. c->x86_vendor = X86_VENDOR_AMD;
  806. else if (!strcmp(v, "GenuineIntel"))
  807. c->x86_vendor = X86_VENDOR_INTEL;
  808. else if (!strcmp(v, "CentaurHauls"))
  809. c->x86_vendor = X86_VENDOR_CENTAUR;
  810. else
  811. c->x86_vendor = X86_VENDOR_UNKNOWN;
  812. }
  813. /* Do some early cpuid on the boot CPU to get some parameter that are
  814. needed before check_bugs. Everything advanced is in identify_cpu
  815. below. */
  816. static void __cpuinit early_identify_cpu(struct cpuinfo_x86 *c)
  817. {
  818. u32 tfms, xlvl;
  819. c->loops_per_jiffy = loops_per_jiffy;
  820. c->x86_cache_size = -1;
  821. c->x86_vendor = X86_VENDOR_UNKNOWN;
  822. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  823. c->x86_vendor_id[0] = '\0'; /* Unset */
  824. c->x86_model_id[0] = '\0'; /* Unset */
  825. c->x86_clflush_size = 64;
  826. c->x86_cache_alignment = c->x86_clflush_size;
  827. c->x86_max_cores = 1;
  828. c->x86_coreid_bits = 0;
  829. c->extended_cpuid_level = 0;
  830. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  831. /* Get vendor name */
  832. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  833. (unsigned int *)&c->x86_vendor_id[0],
  834. (unsigned int *)&c->x86_vendor_id[8],
  835. (unsigned int *)&c->x86_vendor_id[4]);
  836. get_cpu_vendor(c);
  837. /* Initialize the standard set of capabilities */
  838. /* Note that the vendor-specific code below might override */
  839. /* Intel-defined flags: level 0x00000001 */
  840. if (c->cpuid_level >= 0x00000001) {
  841. __u32 misc;
  842. cpuid(0x00000001, &tfms, &misc, &c->x86_capability[4],
  843. &c->x86_capability[0]);
  844. c->x86 = (tfms >> 8) & 0xf;
  845. c->x86_model = (tfms >> 4) & 0xf;
  846. c->x86_mask = tfms & 0xf;
  847. if (c->x86 == 0xf)
  848. c->x86 += (tfms >> 20) & 0xff;
  849. if (c->x86 >= 0x6)
  850. c->x86_model += ((tfms >> 16) & 0xF) << 4;
  851. if (test_cpu_cap(c, X86_FEATURE_CLFLSH))
  852. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  853. } else {
  854. /* Have CPUID level 0 only - unheard of */
  855. c->x86 = 4;
  856. }
  857. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xff;
  858. #ifdef CONFIG_SMP
  859. c->phys_proc_id = c->initial_apicid;
  860. #endif
  861. /* AMD-defined flags: level 0x80000001 */
  862. xlvl = cpuid_eax(0x80000000);
  863. c->extended_cpuid_level = xlvl;
  864. if ((xlvl & 0xffff0000) == 0x80000000) {
  865. if (xlvl >= 0x80000001) {
  866. c->x86_capability[1] = cpuid_edx(0x80000001);
  867. c->x86_capability[6] = cpuid_ecx(0x80000001);
  868. }
  869. if (xlvl >= 0x80000004)
  870. get_model_name(c); /* Default name */
  871. }
  872. /* Transmeta-defined flags: level 0x80860001 */
  873. xlvl = cpuid_eax(0x80860000);
  874. if ((xlvl & 0xffff0000) == 0x80860000) {
  875. /* Don't set x86_cpuid_level here for now to not confuse. */
  876. if (xlvl >= 0x80860001)
  877. c->x86_capability[2] = cpuid_edx(0x80860001);
  878. }
  879. c->extended_cpuid_level = cpuid_eax(0x80000000);
  880. if (c->extended_cpuid_level >= 0x80000007)
  881. c->x86_power = cpuid_edx(0x80000007);
  882. switch (c->x86_vendor) {
  883. case X86_VENDOR_AMD:
  884. early_init_amd(c);
  885. break;
  886. case X86_VENDOR_INTEL:
  887. early_init_intel(c);
  888. break;
  889. case X86_VENDOR_CENTAUR:
  890. early_init_centaur(c);
  891. break;
  892. }
  893. validate_pat_support(c);
  894. }
  895. /*
  896. * This does the hard work of actually picking apart the CPU stuff...
  897. */
  898. void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  899. {
  900. int i;
  901. early_identify_cpu(c);
  902. init_scattered_cpuid_features(c);
  903. c->apicid = phys_pkg_id(0);
  904. /*
  905. * Vendor-specific initialization. In this section we
  906. * canonicalize the feature flags, meaning if there are
  907. * features a certain CPU supports which CPUID doesn't
  908. * tell us, CPUID claiming incorrect flags, or other bugs,
  909. * we handle them here.
  910. *
  911. * At the end of this section, c->x86_capability better
  912. * indicate the features this CPU genuinely supports!
  913. */
  914. switch (c->x86_vendor) {
  915. case X86_VENDOR_AMD:
  916. init_amd(c);
  917. break;
  918. case X86_VENDOR_INTEL:
  919. init_intel(c);
  920. break;
  921. case X86_VENDOR_CENTAUR:
  922. init_centaur(c);
  923. break;
  924. case X86_VENDOR_UNKNOWN:
  925. default:
  926. display_cacheinfo(c);
  927. break;
  928. }
  929. detect_ht(c);
  930. /*
  931. * On SMP, boot_cpu_data holds the common feature set between
  932. * all CPUs; so make sure that we indicate which features are
  933. * common between the CPUs. The first time this routine gets
  934. * executed, c == &boot_cpu_data.
  935. */
  936. if (c != &boot_cpu_data) {
  937. /* AND the already accumulated flags with these */
  938. for (i = 0; i < NCAPINTS; i++)
  939. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  940. }
  941. /* Clear all flags overriden by options */
  942. for (i = 0; i < NCAPINTS; i++)
  943. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  944. #ifdef CONFIG_X86_MCE
  945. mcheck_init(c);
  946. #endif
  947. select_idle_routine(c);
  948. #ifdef CONFIG_NUMA
  949. numa_add_cpu(smp_processor_id());
  950. #endif
  951. }
  952. void __cpuinit identify_boot_cpu(void)
  953. {
  954. identify_cpu(&boot_cpu_data);
  955. }
  956. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  957. {
  958. BUG_ON(c == &boot_cpu_data);
  959. identify_cpu(c);
  960. mtrr_ap_init();
  961. }
  962. static __init int setup_noclflush(char *arg)
  963. {
  964. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  965. return 1;
  966. }
  967. __setup("noclflush", setup_noclflush);
  968. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  969. {
  970. if (c->x86_model_id[0])
  971. printk(KERN_CONT "%s", c->x86_model_id);
  972. if (c->x86_mask || c->cpuid_level >= 0)
  973. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  974. else
  975. printk(KERN_CONT "\n");
  976. }
  977. static __init int setup_disablecpuid(char *arg)
  978. {
  979. int bit;
  980. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  981. setup_clear_cpu_cap(bit);
  982. else
  983. return 0;
  984. return 1;
  985. }
  986. __setup("clearcpuid=", setup_disablecpuid);