setup_64.c 29 KB

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