setup_64.c 29 KB

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