common.c 29 KB

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