common.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. #include <linux/init.h>
  2. #include <linux/kernel.h>
  3. #include <linux/sched.h>
  4. #include <linux/string.h>
  5. #include <linux/bootmem.h>
  6. #include <linux/bitops.h>
  7. #include <linux/module.h>
  8. #include <linux/kgdb.h>
  9. #include <linux/topology.h>
  10. #include <linux/delay.h>
  11. #include <linux/smp.h>
  12. #include <linux/percpu.h>
  13. #include <asm/i387.h>
  14. #include <asm/msr.h>
  15. #include <asm/io.h>
  16. #include <asm/linkage.h>
  17. #include <asm/mmu_context.h>
  18. #include <asm/mtrr.h>
  19. #include <asm/mce.h>
  20. #include <asm/pat.h>
  21. #include <asm/asm.h>
  22. #include <asm/numa.h>
  23. #include <asm/smp.h>
  24. #include <asm/cpu.h>
  25. #include <asm/cpumask.h>
  26. #include <asm/apic.h>
  27. #ifdef CONFIG_X86_LOCAL_APIC
  28. #include <asm/uv/uv.h>
  29. #endif
  30. #include <asm/pgtable.h>
  31. #include <asm/processor.h>
  32. #include <asm/desc.h>
  33. #include <asm/atomic.h>
  34. #include <asm/proto.h>
  35. #include <asm/sections.h>
  36. #include <asm/setup.h>
  37. #include <asm/hypervisor.h>
  38. #include <asm/stackprotector.h>
  39. #include "cpu.h"
  40. #ifdef CONFIG_X86_64
  41. /* all of these masks are initialized in setup_cpu_local_masks() */
  42. cpumask_var_t cpu_callin_mask;
  43. cpumask_var_t cpu_callout_mask;
  44. cpumask_var_t cpu_initialized_mask;
  45. /* representing cpus for which sibling maps can be computed */
  46. cpumask_var_t cpu_sibling_setup_mask;
  47. /* correctly size the local cpu masks */
  48. void __init setup_cpu_local_masks(void)
  49. {
  50. alloc_bootmem_cpumask_var(&cpu_initialized_mask);
  51. alloc_bootmem_cpumask_var(&cpu_callin_mask);
  52. alloc_bootmem_cpumask_var(&cpu_callout_mask);
  53. alloc_bootmem_cpumask_var(&cpu_sibling_setup_mask);
  54. }
  55. #else /* CONFIG_X86_32 */
  56. cpumask_t cpu_callin_map;
  57. cpumask_t cpu_callout_map;
  58. cpumask_t cpu_initialized;
  59. cpumask_t cpu_sibling_setup_map;
  60. #endif /* CONFIG_X86_32 */
  61. static struct cpu_dev *this_cpu __cpuinitdata;
  62. DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
  63. #ifdef CONFIG_X86_64
  64. /*
  65. * We need valid kernel segments for data and code in long mode too
  66. * IRET will check the segment types kkeil 2000/10/28
  67. * Also sysret mandates a special GDT layout
  68. *
  69. * The TLS descriptors are currently at a different place compared to i386.
  70. * Hopefully nobody expects them at a fixed place (Wine?)
  71. */
  72. [GDT_ENTRY_KERNEL32_CS] = { { { 0x0000ffff, 0x00cf9b00 } } },
  73. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00af9b00 } } },
  74. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9300 } } },
  75. [GDT_ENTRY_DEFAULT_USER32_CS] = { { { 0x0000ffff, 0x00cffb00 } } },
  76. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff300 } } },
  77. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00affb00 } } },
  78. #else
  79. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00cf9a00 } } },
  80. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9200 } } },
  81. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00cffa00 } } },
  82. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff200 } } },
  83. /*
  84. * Segments used for calling PnP BIOS have byte granularity.
  85. * They code segments and data segments have fixed 64k limits,
  86. * the transfer segment sizes are set at run time.
  87. */
  88. /* 32-bit code */
  89. [GDT_ENTRY_PNPBIOS_CS32] = { { { 0x0000ffff, 0x00409a00 } } },
  90. /* 16-bit code */
  91. [GDT_ENTRY_PNPBIOS_CS16] = { { { 0x0000ffff, 0x00009a00 } } },
  92. /* 16-bit data */
  93. [GDT_ENTRY_PNPBIOS_DS] = { { { 0x0000ffff, 0x00009200 } } },
  94. /* 16-bit data */
  95. [GDT_ENTRY_PNPBIOS_TS1] = { { { 0x00000000, 0x00009200 } } },
  96. /* 16-bit data */
  97. [GDT_ENTRY_PNPBIOS_TS2] = { { { 0x00000000, 0x00009200 } } },
  98. /*
  99. * The APM segments have byte granularity and their bases
  100. * are set at run time. All have 64k limits.
  101. */
  102. /* 32-bit code */
  103. [GDT_ENTRY_APMBIOS_BASE] = { { { 0x0000ffff, 0x00409a00 } } },
  104. /* 16-bit code */
  105. [GDT_ENTRY_APMBIOS_BASE+1] = { { { 0x0000ffff, 0x00009a00 } } },
  106. /* data */
  107. [GDT_ENTRY_APMBIOS_BASE+2] = { { { 0x0000ffff, 0x00409200 } } },
  108. [GDT_ENTRY_ESPFIX_SS] = { { { 0x00000000, 0x00c09200 } } },
  109. [GDT_ENTRY_PERCPU] = { { { 0x0000ffff, 0x00cf9200 } } },
  110. GDT_STACK_CANARY_INIT
  111. #endif
  112. } };
  113. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  114. #ifdef CONFIG_X86_32
  115. static int cachesize_override __cpuinitdata = -1;
  116. static int disable_x86_serial_nr __cpuinitdata = 1;
  117. static int __init cachesize_setup(char *str)
  118. {
  119. get_option(&str, &cachesize_override);
  120. return 1;
  121. }
  122. __setup("cachesize=", cachesize_setup);
  123. static int __init x86_fxsr_setup(char *s)
  124. {
  125. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  126. setup_clear_cpu_cap(X86_FEATURE_XMM);
  127. return 1;
  128. }
  129. __setup("nofxsr", x86_fxsr_setup);
  130. static int __init x86_sep_setup(char *s)
  131. {
  132. setup_clear_cpu_cap(X86_FEATURE_SEP);
  133. return 1;
  134. }
  135. __setup("nosep", x86_sep_setup);
  136. /* Standard macro to see if a specific flag is changeable */
  137. static inline int flag_is_changeable_p(u32 flag)
  138. {
  139. u32 f1, f2;
  140. /*
  141. * Cyrix and IDT cpus allow disabling of CPUID
  142. * so the code below may return different results
  143. * when it is executed before and after enabling
  144. * the CPUID. Add "volatile" to not allow gcc to
  145. * optimize the subsequent calls to this function.
  146. */
  147. asm volatile ("pushfl\n\t"
  148. "pushfl\n\t"
  149. "popl %0\n\t"
  150. "movl %0,%1\n\t"
  151. "xorl %2,%0\n\t"
  152. "pushl %0\n\t"
  153. "popfl\n\t"
  154. "pushfl\n\t"
  155. "popl %0\n\t"
  156. "popfl\n\t"
  157. : "=&r" (f1), "=&r" (f2)
  158. : "ir" (flag));
  159. return ((f1^f2) & flag) != 0;
  160. }
  161. /* Probe for the CPUID instruction */
  162. static int __cpuinit have_cpuid_p(void)
  163. {
  164. return flag_is_changeable_p(X86_EFLAGS_ID);
  165. }
  166. static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  167. {
  168. if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr) {
  169. /* Disable processor serial number */
  170. unsigned long lo, hi;
  171. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  172. lo |= 0x200000;
  173. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  174. printk(KERN_NOTICE "CPU serial number disabled.\n");
  175. clear_cpu_cap(c, X86_FEATURE_PN);
  176. /* Disabling the serial number may affect the cpuid level */
  177. c->cpuid_level = cpuid_eax(0);
  178. }
  179. }
  180. static int __init x86_serial_nr_setup(char *s)
  181. {
  182. disable_x86_serial_nr = 0;
  183. return 1;
  184. }
  185. __setup("serialnumber", x86_serial_nr_setup);
  186. #else
  187. static inline int flag_is_changeable_p(u32 flag)
  188. {
  189. return 1;
  190. }
  191. /* Probe for the CPUID instruction */
  192. static inline int have_cpuid_p(void)
  193. {
  194. return 1;
  195. }
  196. static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  197. {
  198. }
  199. #endif
  200. /*
  201. * Some CPU features depend on higher CPUID levels, which may not always
  202. * be available due to CPUID level capping or broken virtualization
  203. * software. Add those features to this table to auto-disable them.
  204. */
  205. struct cpuid_dependent_feature {
  206. u32 feature;
  207. u32 level;
  208. };
  209. static const struct cpuid_dependent_feature __cpuinitconst
  210. cpuid_dependent_features[] = {
  211. { X86_FEATURE_MWAIT, 0x00000005 },
  212. { X86_FEATURE_DCA, 0x00000009 },
  213. { X86_FEATURE_XSAVE, 0x0000000d },
  214. { 0, 0 }
  215. };
  216. static void __cpuinit filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
  217. {
  218. const struct cpuid_dependent_feature *df;
  219. for (df = cpuid_dependent_features; df->feature; df++) {
  220. /*
  221. * Note: cpuid_level is set to -1 if unavailable, but
  222. * extended_extended_level is set to 0 if unavailable
  223. * and the legitimate extended levels are all negative
  224. * when signed; hence the weird messing around with
  225. * signs here...
  226. */
  227. if (cpu_has(c, df->feature) &&
  228. ((s32)df->level < 0 ?
  229. (u32)df->level > (u32)c->extended_cpuid_level :
  230. (s32)df->level > (s32)c->cpuid_level)) {
  231. clear_cpu_cap(c, df->feature);
  232. if (warn)
  233. printk(KERN_WARNING
  234. "CPU: CPU feature %s disabled "
  235. "due to lack of CPUID level 0x%x\n",
  236. x86_cap_flags[df->feature],
  237. df->level);
  238. }
  239. }
  240. }
  241. /*
  242. * Naming convention should be: <Name> [(<Codename>)]
  243. * This table only is used unless init_<vendor>() below doesn't set it;
  244. * in particular, if CPUID levels 0x80000002..4 are supported, this isn't used
  245. *
  246. */
  247. /* Look up CPU names by table lookup. */
  248. static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
  249. {
  250. struct cpu_model_info *info;
  251. if (c->x86_model >= 16)
  252. return NULL; /* Range check */
  253. if (!this_cpu)
  254. return NULL;
  255. info = this_cpu->c_models;
  256. while (info && info->family) {
  257. if (info->family == c->x86)
  258. return info->model_names[c->x86_model];
  259. info++;
  260. }
  261. return NULL; /* Not found */
  262. }
  263. __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
  264. void load_percpu_segment(int cpu)
  265. {
  266. #ifdef CONFIG_X86_32
  267. loadsegment(fs, __KERNEL_PERCPU);
  268. #else
  269. loadsegment(gs, 0);
  270. wrmsrl(MSR_GS_BASE, (unsigned long)per_cpu(irq_stack_union.gs_base, cpu));
  271. #endif
  272. load_stack_canary_segment();
  273. }
  274. /* Current gdt points %fs at the "master" per-cpu area: after this,
  275. * it's on the real one. */
  276. void switch_to_new_gdt(int cpu)
  277. {
  278. struct desc_ptr gdt_descr;
  279. gdt_descr.address = (long)get_cpu_gdt_table(cpu);
  280. gdt_descr.size = GDT_SIZE - 1;
  281. load_gdt(&gdt_descr);
  282. /* Reload the per-cpu base */
  283. load_percpu_segment(cpu);
  284. }
  285. static struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  286. static void __cpuinit default_init(struct cpuinfo_x86 *c)
  287. {
  288. #ifdef CONFIG_X86_64
  289. display_cacheinfo(c);
  290. #else
  291. /* Not much we can do here... */
  292. /* Check if at least it has cpuid */
  293. if (c->cpuid_level == -1) {
  294. /* No cpuid. It must be an ancient CPU */
  295. if (c->x86 == 4)
  296. strcpy(c->x86_model_id, "486");
  297. else if (c->x86 == 3)
  298. strcpy(c->x86_model_id, "386");
  299. }
  300. #endif
  301. }
  302. static struct cpu_dev __cpuinitdata default_cpu = {
  303. .c_init = default_init,
  304. .c_vendor = "Unknown",
  305. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  306. };
  307. static void __cpuinit get_model_name(struct cpuinfo_x86 *c)
  308. {
  309. unsigned int *v;
  310. char *p, *q;
  311. if (c->extended_cpuid_level < 0x80000004)
  312. return;
  313. v = (unsigned int *) c->x86_model_id;
  314. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  315. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  316. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  317. c->x86_model_id[48] = 0;
  318. /* Intel chips right-justify this string for some dumb reason;
  319. undo that brain damage */
  320. p = q = &c->x86_model_id[0];
  321. while (*p == ' ')
  322. p++;
  323. if (p != q) {
  324. while (*p)
  325. *q++ = *p++;
  326. while (q <= &c->x86_model_id[48])
  327. *q++ = '\0'; /* Zero-pad the rest */
  328. }
  329. }
  330. void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  331. {
  332. unsigned int n, dummy, ebx, ecx, edx, l2size;
  333. n = c->extended_cpuid_level;
  334. if (n >= 0x80000005) {
  335. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  336. printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
  337. edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
  338. c->x86_cache_size = (ecx>>24) + (edx>>24);
  339. #ifdef CONFIG_X86_64
  340. /* On K8 L1 TLB is inclusive, so don't count it */
  341. c->x86_tlbsize = 0;
  342. #endif
  343. }
  344. if (n < 0x80000006) /* Some chips just has a large L1. */
  345. return;
  346. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  347. l2size = ecx >> 16;
  348. #ifdef CONFIG_X86_64
  349. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  350. #else
  351. /* do processor-specific cache resizing */
  352. if (this_cpu->c_size_cache)
  353. l2size = this_cpu->c_size_cache(c, l2size);
  354. /* Allow user to override all this if necessary. */
  355. if (cachesize_override != -1)
  356. l2size = cachesize_override;
  357. if (l2size == 0)
  358. return; /* Again, no L2 cache is possible */
  359. #endif
  360. c->x86_cache_size = l2size;
  361. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  362. l2size, ecx & 0xFF);
  363. }
  364. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  365. {
  366. #ifdef CONFIG_X86_HT
  367. u32 eax, ebx, ecx, edx;
  368. int index_msb, core_bits;
  369. if (!cpu_has(c, X86_FEATURE_HT))
  370. return;
  371. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  372. goto out;
  373. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  374. return;
  375. cpuid(1, &eax, &ebx, &ecx, &edx);
  376. smp_num_siblings = (ebx & 0xff0000) >> 16;
  377. if (smp_num_siblings == 1) {
  378. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  379. } else if (smp_num_siblings > 1) {
  380. if (smp_num_siblings > nr_cpu_ids) {
  381. printk(KERN_WARNING "CPU: Unsupported number of siblings %d",
  382. smp_num_siblings);
  383. smp_num_siblings = 1;
  384. return;
  385. }
  386. index_msb = get_count_order(smp_num_siblings);
  387. c->phys_proc_id = apic->phys_pkg_id(c->initial_apicid, index_msb);
  388. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  389. index_msb = get_count_order(smp_num_siblings);
  390. core_bits = get_count_order(c->x86_max_cores);
  391. c->cpu_core_id = apic->phys_pkg_id(c->initial_apicid, index_msb) &
  392. ((1 << core_bits) - 1);
  393. }
  394. out:
  395. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  396. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  397. c->phys_proc_id);
  398. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  399. c->cpu_core_id);
  400. }
  401. #endif
  402. }
  403. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  404. {
  405. char *v = c->x86_vendor_id;
  406. int i;
  407. static int printed;
  408. for (i = 0; i < X86_VENDOR_NUM; i++) {
  409. if (!cpu_devs[i])
  410. break;
  411. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  412. (cpu_devs[i]->c_ident[1] &&
  413. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  414. this_cpu = cpu_devs[i];
  415. c->x86_vendor = this_cpu->c_x86_vendor;
  416. return;
  417. }
  418. }
  419. if (!printed) {
  420. printed++;
  421. printk(KERN_ERR "CPU: vendor_id '%s' unknown, using generic init.\n", v);
  422. printk(KERN_ERR "CPU: Your system may be unstable.\n");
  423. }
  424. c->x86_vendor = X86_VENDOR_UNKNOWN;
  425. this_cpu = &default_cpu;
  426. }
  427. void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
  428. {
  429. /* Get vendor name */
  430. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  431. (unsigned int *)&c->x86_vendor_id[0],
  432. (unsigned int *)&c->x86_vendor_id[8],
  433. (unsigned int *)&c->x86_vendor_id[4]);
  434. c->x86 = 4;
  435. /* Intel-defined flags: level 0x00000001 */
  436. if (c->cpuid_level >= 0x00000001) {
  437. u32 junk, tfms, cap0, misc;
  438. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  439. c->x86 = (tfms >> 8) & 0xf;
  440. c->x86_model = (tfms >> 4) & 0xf;
  441. c->x86_mask = tfms & 0xf;
  442. if (c->x86 == 0xf)
  443. c->x86 += (tfms >> 20) & 0xff;
  444. if (c->x86 >= 0x6)
  445. c->x86_model += ((tfms >> 16) & 0xf) << 4;
  446. if (cap0 & (1<<19)) {
  447. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  448. c->x86_cache_alignment = c->x86_clflush_size;
  449. }
  450. }
  451. }
  452. static void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
  453. {
  454. u32 tfms, xlvl;
  455. u32 ebx;
  456. /* Intel-defined flags: level 0x00000001 */
  457. if (c->cpuid_level >= 0x00000001) {
  458. u32 capability, excap;
  459. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  460. c->x86_capability[0] = capability;
  461. c->x86_capability[4] = excap;
  462. }
  463. /* AMD-defined flags: level 0x80000001 */
  464. xlvl = cpuid_eax(0x80000000);
  465. c->extended_cpuid_level = xlvl;
  466. if ((xlvl & 0xffff0000) == 0x80000000) {
  467. if (xlvl >= 0x80000001) {
  468. c->x86_capability[1] = cpuid_edx(0x80000001);
  469. c->x86_capability[6] = cpuid_ecx(0x80000001);
  470. }
  471. }
  472. #ifdef CONFIG_X86_64
  473. if (c->extended_cpuid_level >= 0x80000008) {
  474. u32 eax = cpuid_eax(0x80000008);
  475. c->x86_virt_bits = (eax >> 8) & 0xff;
  476. c->x86_phys_bits = eax & 0xff;
  477. }
  478. #endif
  479. if (c->extended_cpuid_level >= 0x80000007)
  480. c->x86_power = cpuid_edx(0x80000007);
  481. }
  482. static void __cpuinit identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  483. {
  484. #ifdef CONFIG_X86_32
  485. int i;
  486. /*
  487. * First of all, decide if this is a 486 or higher
  488. * It's a 486 if we can modify the AC flag
  489. */
  490. if (flag_is_changeable_p(X86_EFLAGS_AC))
  491. c->x86 = 4;
  492. else
  493. c->x86 = 3;
  494. for (i = 0; i < X86_VENDOR_NUM; i++)
  495. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  496. c->x86_vendor_id[0] = 0;
  497. cpu_devs[i]->c_identify(c);
  498. if (c->x86_vendor_id[0]) {
  499. get_cpu_vendor(c);
  500. break;
  501. }
  502. }
  503. #endif
  504. }
  505. /*
  506. * Do minimum CPU detection early.
  507. * Fields really needed: vendor, cpuid_level, family, model, mask,
  508. * cache alignment.
  509. * The others are not touched to avoid unwanted side effects.
  510. *
  511. * WARNING: this function is only called on the BP. Don't add code here
  512. * that is supposed to run on all CPUs.
  513. */
  514. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  515. {
  516. #ifdef CONFIG_X86_64
  517. c->x86_clflush_size = 64;
  518. #else
  519. c->x86_clflush_size = 32;
  520. #endif
  521. c->x86_cache_alignment = c->x86_clflush_size;
  522. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  523. c->extended_cpuid_level = 0;
  524. if (!have_cpuid_p())
  525. identify_cpu_without_cpuid(c);
  526. /* cyrix could have cpuid enabled via c_identify()*/
  527. if (!have_cpuid_p())
  528. return;
  529. cpu_detect(c);
  530. get_cpu_vendor(c);
  531. get_cpu_cap(c);
  532. if (this_cpu->c_early_init)
  533. this_cpu->c_early_init(c);
  534. #ifdef CONFIG_SMP
  535. c->cpu_index = boot_cpu_id;
  536. #endif
  537. filter_cpuid_features(c, false);
  538. }
  539. void __init early_cpu_init(void)
  540. {
  541. struct cpu_dev **cdev;
  542. int count = 0;
  543. printk("KERNEL supported cpus:\n");
  544. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  545. struct cpu_dev *cpudev = *cdev;
  546. unsigned int j;
  547. if (count >= X86_VENDOR_NUM)
  548. break;
  549. cpu_devs[count] = cpudev;
  550. count++;
  551. for (j = 0; j < 2; j++) {
  552. if (!cpudev->c_ident[j])
  553. continue;
  554. printk(" %s %s\n", cpudev->c_vendor,
  555. cpudev->c_ident[j]);
  556. }
  557. }
  558. early_identify_cpu(&boot_cpu_data);
  559. }
  560. /*
  561. * The NOPL instruction is supposed to exist on all CPUs with
  562. * family >= 6; unfortunately, that's not true in practice because
  563. * of early VIA chips and (more importantly) broken virtualizers that
  564. * are not easy to detect. In the latter case it doesn't even *fail*
  565. * reliably, so probing for it doesn't even work. Disable it completely
  566. * unless we can find a reliable way to detect all the broken cases.
  567. */
  568. static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
  569. {
  570. clear_cpu_cap(c, X86_FEATURE_NOPL);
  571. }
  572. static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
  573. {
  574. c->extended_cpuid_level = 0;
  575. if (!have_cpuid_p())
  576. identify_cpu_without_cpuid(c);
  577. /* cyrix could have cpuid enabled via c_identify()*/
  578. if (!have_cpuid_p())
  579. return;
  580. cpu_detect(c);
  581. get_cpu_vendor(c);
  582. get_cpu_cap(c);
  583. if (c->cpuid_level >= 0x00000001) {
  584. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  585. #ifdef CONFIG_X86_32
  586. # ifdef CONFIG_X86_HT
  587. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  588. # else
  589. c->apicid = c->initial_apicid;
  590. # endif
  591. #endif
  592. #ifdef CONFIG_X86_HT
  593. c->phys_proc_id = c->initial_apicid;
  594. #endif
  595. }
  596. get_model_name(c); /* Default name */
  597. init_scattered_cpuid_features(c);
  598. detect_nopl(c);
  599. }
  600. /*
  601. * This does the hard work of actually picking apart the CPU stuff...
  602. */
  603. static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  604. {
  605. int i;
  606. c->loops_per_jiffy = loops_per_jiffy;
  607. c->x86_cache_size = -1;
  608. c->x86_vendor = X86_VENDOR_UNKNOWN;
  609. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  610. c->x86_vendor_id[0] = '\0'; /* Unset */
  611. c->x86_model_id[0] = '\0'; /* Unset */
  612. c->x86_max_cores = 1;
  613. c->x86_coreid_bits = 0;
  614. #ifdef CONFIG_X86_64
  615. c->x86_clflush_size = 64;
  616. #else
  617. c->cpuid_level = -1; /* CPUID not detected */
  618. c->x86_clflush_size = 32;
  619. #endif
  620. c->x86_cache_alignment = c->x86_clflush_size;
  621. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  622. generic_identify(c);
  623. if (this_cpu->c_identify)
  624. this_cpu->c_identify(c);
  625. #ifdef CONFIG_X86_64
  626. c->apicid = apic->phys_pkg_id(c->initial_apicid, 0);
  627. #endif
  628. /*
  629. * Vendor-specific initialization. In this section we
  630. * canonicalize the feature flags, meaning if there are
  631. * features a certain CPU supports which CPUID doesn't
  632. * tell us, CPUID claiming incorrect flags, or other bugs,
  633. * we handle them here.
  634. *
  635. * At the end of this section, c->x86_capability better
  636. * indicate the features this CPU genuinely supports!
  637. */
  638. if (this_cpu->c_init)
  639. this_cpu->c_init(c);
  640. /* Disable the PN if appropriate */
  641. squash_the_stupid_serial_number(c);
  642. /*
  643. * The vendor-specific functions might have changed features. Now
  644. * we do "generic changes."
  645. */
  646. /* Filter out anything that depends on CPUID levels we don't have */
  647. filter_cpuid_features(c, true);
  648. /* If the model name is still unset, do table lookup. */
  649. if (!c->x86_model_id[0]) {
  650. char *p;
  651. p = table_lookup_model(c);
  652. if (p)
  653. strcpy(c->x86_model_id, p);
  654. else
  655. /* Last resort... */
  656. sprintf(c->x86_model_id, "%02x/%02x",
  657. c->x86, c->x86_model);
  658. }
  659. #ifdef CONFIG_X86_64
  660. detect_ht(c);
  661. #endif
  662. init_hypervisor(c);
  663. /*
  664. * On SMP, boot_cpu_data holds the common feature set between
  665. * all CPUs; so make sure that we indicate which features are
  666. * common between the CPUs. The first time this routine gets
  667. * executed, c == &boot_cpu_data.
  668. */
  669. if (c != &boot_cpu_data) {
  670. /* AND the already accumulated flags with these */
  671. for (i = 0; i < NCAPINTS; i++)
  672. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  673. }
  674. /* Clear all flags overriden by options */
  675. for (i = 0; i < NCAPINTS; i++)
  676. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  677. #ifdef CONFIG_X86_MCE
  678. /* Init Machine Check Exception if available. */
  679. mcheck_init(c);
  680. #endif
  681. select_idle_routine(c);
  682. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
  683. numa_add_cpu(smp_processor_id());
  684. #endif
  685. }
  686. #ifdef CONFIG_X86_64
  687. static void vgetcpu_set_mode(void)
  688. {
  689. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  690. vgetcpu_mode = VGETCPU_RDTSCP;
  691. else
  692. vgetcpu_mode = VGETCPU_LSL;
  693. }
  694. #endif
  695. void __init identify_boot_cpu(void)
  696. {
  697. identify_cpu(&boot_cpu_data);
  698. #ifdef CONFIG_X86_32
  699. sysenter_setup();
  700. enable_sep_cpu();
  701. #else
  702. vgetcpu_set_mode();
  703. #endif
  704. }
  705. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  706. {
  707. BUG_ON(c == &boot_cpu_data);
  708. identify_cpu(c);
  709. #ifdef CONFIG_X86_32
  710. enable_sep_cpu();
  711. #endif
  712. mtrr_ap_init();
  713. }
  714. struct msr_range {
  715. unsigned min;
  716. unsigned max;
  717. };
  718. static struct msr_range msr_range_array[] __cpuinitdata = {
  719. { 0x00000000, 0x00000418},
  720. { 0xc0000000, 0xc000040b},
  721. { 0xc0010000, 0xc0010142},
  722. { 0xc0011000, 0xc001103b},
  723. };
  724. static void __cpuinit print_cpu_msr(void)
  725. {
  726. unsigned index;
  727. u64 val;
  728. int i;
  729. unsigned index_min, index_max;
  730. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  731. index_min = msr_range_array[i].min;
  732. index_max = msr_range_array[i].max;
  733. for (index = index_min; index < index_max; index++) {
  734. if (rdmsrl_amd_safe(index, &val))
  735. continue;
  736. printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
  737. }
  738. }
  739. }
  740. static int show_msr __cpuinitdata;
  741. static __init int setup_show_msr(char *arg)
  742. {
  743. int num;
  744. get_option(&arg, &num);
  745. if (num > 0)
  746. show_msr = num;
  747. return 1;
  748. }
  749. __setup("show_msr=", setup_show_msr);
  750. static __init int setup_noclflush(char *arg)
  751. {
  752. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  753. return 1;
  754. }
  755. __setup("noclflush", setup_noclflush);
  756. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  757. {
  758. char *vendor = NULL;
  759. if (c->x86_vendor < X86_VENDOR_NUM)
  760. vendor = this_cpu->c_vendor;
  761. else if (c->cpuid_level >= 0)
  762. vendor = c->x86_vendor_id;
  763. if (vendor && !strstr(c->x86_model_id, vendor))
  764. printk(KERN_CONT "%s ", vendor);
  765. if (c->x86_model_id[0])
  766. printk(KERN_CONT "%s", c->x86_model_id);
  767. else
  768. printk(KERN_CONT "%d86", c->x86);
  769. if (c->x86_mask || c->cpuid_level >= 0)
  770. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  771. else
  772. printk(KERN_CONT "\n");
  773. #ifdef CONFIG_SMP
  774. if (c->cpu_index < show_msr)
  775. print_cpu_msr();
  776. #else
  777. if (show_msr)
  778. print_cpu_msr();
  779. #endif
  780. }
  781. static __init int setup_disablecpuid(char *arg)
  782. {
  783. int bit;
  784. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  785. setup_clear_cpu_cap(bit);
  786. else
  787. return 0;
  788. return 1;
  789. }
  790. __setup("clearcpuid=", setup_disablecpuid);
  791. #ifdef CONFIG_X86_64
  792. struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
  793. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  794. irq_stack_union) __aligned(PAGE_SIZE);
  795. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  796. init_per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
  797. DEFINE_PER_CPU(unsigned long, kernel_stack) =
  798. (unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
  799. EXPORT_PER_CPU_SYMBOL(kernel_stack);
  800. DEFINE_PER_CPU(unsigned int, irq_count) = -1;
  801. static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
  802. [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ])
  803. __aligned(PAGE_SIZE);
  804. extern asmlinkage void ignore_sysret(void);
  805. /* May not be marked __init: used by software suspend */
  806. void syscall_init(void)
  807. {
  808. /*
  809. * LSTAR and STAR live in a bit strange symbiosis.
  810. * They both write to the same internal register. STAR allows to
  811. * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
  812. */
  813. wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
  814. wrmsrl(MSR_LSTAR, system_call);
  815. wrmsrl(MSR_CSTAR, ignore_sysret);
  816. #ifdef CONFIG_IA32_EMULATION
  817. syscall32_cpu_init();
  818. #endif
  819. /* Flags to clear on syscall */
  820. wrmsrl(MSR_SYSCALL_MASK,
  821. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
  822. }
  823. unsigned long kernel_eflags;
  824. /*
  825. * Copies of the original ist values from the tss are only accessed during
  826. * debugging, no special alignment required.
  827. */
  828. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  829. #else /* x86_64 */
  830. #ifdef CONFIG_CC_STACKPROTECTOR
  831. DEFINE_PER_CPU(unsigned long, stack_canary);
  832. #endif
  833. /* Make sure %fs and %gs are initialized properly in idle threads */
  834. struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
  835. {
  836. memset(regs, 0, sizeof(struct pt_regs));
  837. regs->fs = __KERNEL_PERCPU;
  838. regs->gs = __KERNEL_STACK_CANARY;
  839. return regs;
  840. }
  841. #endif /* x86_64 */
  842. /*
  843. * cpu_init() initializes state that is per-CPU. Some data is already
  844. * initialized (naturally) in the bootstrap process, such as the GDT
  845. * and IDT. We reload them nevertheless, this function acts as a
  846. * 'CPU state barrier', nothing should get across.
  847. * A lot of state is already set up in PDA init for 64 bit
  848. */
  849. #ifdef CONFIG_X86_64
  850. void __cpuinit cpu_init(void)
  851. {
  852. int cpu = stack_smp_processor_id();
  853. struct tss_struct *t = &per_cpu(init_tss, cpu);
  854. struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
  855. unsigned long v;
  856. struct task_struct *me;
  857. int i;
  858. #ifdef CONFIG_NUMA
  859. if (cpu != 0 && percpu_read(node_number) == 0 &&
  860. cpu_to_node(cpu) != NUMA_NO_NODE)
  861. percpu_write(node_number, cpu_to_node(cpu));
  862. #endif
  863. me = current;
  864. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
  865. panic("CPU#%d already initialized!\n", cpu);
  866. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  867. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  868. /*
  869. * Initialize the per-CPU GDT with the boot GDT,
  870. * and set up the GDT descriptor:
  871. */
  872. switch_to_new_gdt(cpu);
  873. loadsegment(fs, 0);
  874. load_idt((const struct desc_ptr *)&idt_descr);
  875. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  876. syscall_init();
  877. wrmsrl(MSR_FS_BASE, 0);
  878. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  879. barrier();
  880. check_efer();
  881. if (cpu != 0)
  882. enable_x2apic();
  883. /*
  884. * set up and load the per-CPU TSS
  885. */
  886. if (!orig_ist->ist[0]) {
  887. static const unsigned int sizes[N_EXCEPTION_STACKS] = {
  888. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  889. [DEBUG_STACK - 1] = DEBUG_STKSZ
  890. };
  891. char *estacks = per_cpu(exception_stacks, cpu);
  892. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  893. estacks += sizes[v];
  894. orig_ist->ist[v] = t->x86_tss.ist[v] =
  895. (unsigned long)estacks;
  896. }
  897. }
  898. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  899. /*
  900. * <= is required because the CPU will access up to
  901. * 8 bits beyond the end of the IO permission bitmap.
  902. */
  903. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  904. t->io_bitmap[i] = ~0UL;
  905. atomic_inc(&init_mm.mm_count);
  906. me->active_mm = &init_mm;
  907. BUG_ON(me->mm);
  908. enter_lazy_tlb(&init_mm, me);
  909. load_sp0(t, &current->thread);
  910. set_tss_desc(cpu, t);
  911. load_TR_desc();
  912. load_LDT(&init_mm.context);
  913. #ifdef CONFIG_KGDB
  914. /*
  915. * If the kgdb is connected no debug regs should be altered. This
  916. * is only applicable when KGDB and a KGDB I/O module are built
  917. * into the kernel and you are using early debugging with
  918. * kgdbwait. KGDB will control the kernel HW breakpoint registers.
  919. */
  920. if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
  921. arch_kgdb_ops.correct_hw_break();
  922. else
  923. #endif
  924. {
  925. /*
  926. * Clear all 6 debug registers:
  927. */
  928. set_debugreg(0UL, 0);
  929. set_debugreg(0UL, 1);
  930. set_debugreg(0UL, 2);
  931. set_debugreg(0UL, 3);
  932. set_debugreg(0UL, 6);
  933. set_debugreg(0UL, 7);
  934. }
  935. fpu_init();
  936. raw_local_save_flags(kernel_eflags);
  937. if (is_uv_system())
  938. uv_cpu_init();
  939. }
  940. #else
  941. void __cpuinit cpu_init(void)
  942. {
  943. int cpu = smp_processor_id();
  944. struct task_struct *curr = current;
  945. struct tss_struct *t = &per_cpu(init_tss, cpu);
  946. struct thread_struct *thread = &curr->thread;
  947. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
  948. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
  949. for (;;) local_irq_enable();
  950. }
  951. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  952. if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  953. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  954. load_idt(&idt_descr);
  955. switch_to_new_gdt(cpu);
  956. /*
  957. * Set up and load the per-CPU TSS and LDT
  958. */
  959. atomic_inc(&init_mm.mm_count);
  960. curr->active_mm = &init_mm;
  961. BUG_ON(curr->mm);
  962. enter_lazy_tlb(&init_mm, curr);
  963. load_sp0(t, thread);
  964. set_tss_desc(cpu, t);
  965. load_TR_desc();
  966. load_LDT(&init_mm.context);
  967. #ifdef CONFIG_DOUBLEFAULT
  968. /* Set up doublefault TSS pointer in the GDT */
  969. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  970. #endif
  971. /* Clear all 6 debug registers: */
  972. set_debugreg(0, 0);
  973. set_debugreg(0, 1);
  974. set_debugreg(0, 2);
  975. set_debugreg(0, 3);
  976. set_debugreg(0, 6);
  977. set_debugreg(0, 7);
  978. /*
  979. * Force FPU initialization:
  980. */
  981. if (cpu_has_xsave)
  982. current_thread_info()->status = TS_XSAVE;
  983. else
  984. current_thread_info()->status = 0;
  985. clear_used_math();
  986. mxcsr_feature_mask_init();
  987. /*
  988. * Boot processor to setup the FP and extended state context info.
  989. */
  990. if (smp_processor_id() == boot_cpu_id)
  991. init_thread_xstate();
  992. xsave_init();
  993. }
  994. #endif