setup_64.c 29 KB

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