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