setup_64.c 29 KB

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