setup_64.c 28 KB

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