common.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. #include <linux/init.h>
  2. #include <linux/string.h>
  3. #include <linux/delay.h>
  4. #include <linux/smp.h>
  5. #include <linux/module.h>
  6. #include <linux/percpu.h>
  7. #include <linux/bootmem.h>
  8. #include <asm/processor.h>
  9. #include <asm/i387.h>
  10. #include <asm/msr.h>
  11. #include <asm/io.h>
  12. #include <asm/mmu_context.h>
  13. #include <asm/mtrr.h>
  14. #include <asm/mce.h>
  15. #include <asm/pat.h>
  16. #include <asm/asm.h>
  17. #ifdef CONFIG_X86_LOCAL_APIC
  18. #include <asm/mpspec.h>
  19. #include <asm/apic.h>
  20. #include <mach_apic.h>
  21. #endif
  22. #include "cpu.h"
  23. static struct cpu_dev *this_cpu __cpuinitdata;
  24. DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
  25. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00cf9a00 } } },
  26. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9200 } } },
  27. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00cffa00 } } },
  28. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff200 } } },
  29. /*
  30. * Segments used for calling PnP BIOS have byte granularity.
  31. * They code segments and data segments have fixed 64k limits,
  32. * the transfer segment sizes are set at run time.
  33. */
  34. /* 32-bit code */
  35. [GDT_ENTRY_PNPBIOS_CS32] = { { { 0x0000ffff, 0x00409a00 } } },
  36. /* 16-bit code */
  37. [GDT_ENTRY_PNPBIOS_CS16] = { { { 0x0000ffff, 0x00009a00 } } },
  38. /* 16-bit data */
  39. [GDT_ENTRY_PNPBIOS_DS] = { { { 0x0000ffff, 0x00009200 } } },
  40. /* 16-bit data */
  41. [GDT_ENTRY_PNPBIOS_TS1] = { { { 0x00000000, 0x00009200 } } },
  42. /* 16-bit data */
  43. [GDT_ENTRY_PNPBIOS_TS2] = { { { 0x00000000, 0x00009200 } } },
  44. /*
  45. * The APM segments have byte granularity and their bases
  46. * are set at run time. All have 64k limits.
  47. */
  48. /* 32-bit code */
  49. [GDT_ENTRY_APMBIOS_BASE] = { { { 0x0000ffff, 0x00409a00 } } },
  50. /* 16-bit code */
  51. [GDT_ENTRY_APMBIOS_BASE+1] = { { { 0x0000ffff, 0x00009a00 } } },
  52. /* data */
  53. [GDT_ENTRY_APMBIOS_BASE+2] = { { { 0x0000ffff, 0x00409200 } } },
  54. [GDT_ENTRY_ESPFIX_SS] = { { { 0x00000000, 0x00c09200 } } },
  55. [GDT_ENTRY_PERCPU] = { { { 0x00000000, 0x00000000 } } },
  56. } };
  57. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  58. static int cachesize_override __cpuinitdata = -1;
  59. static int disable_x86_serial_nr __cpuinitdata = 1;
  60. static int __init cachesize_setup(char *str)
  61. {
  62. get_option(&str, &cachesize_override);
  63. return 1;
  64. }
  65. __setup("cachesize=", cachesize_setup);
  66. /*
  67. * Naming convention should be: <Name> [(<Codename>)]
  68. * This table only is used unless init_<vendor>() below doesn't set it;
  69. * in particular, if CPUID levels 0x80000002..4 are supported, this isn't used
  70. *
  71. */
  72. /* Look up CPU names by table lookup. */
  73. static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
  74. {
  75. struct cpu_model_info *info;
  76. if (c->x86_model >= 16)
  77. return NULL; /* Range check */
  78. if (!this_cpu)
  79. return NULL;
  80. info = this_cpu->c_models;
  81. while (info && info->family) {
  82. if (info->family == c->x86)
  83. return info->model_names[c->x86_model];
  84. info++;
  85. }
  86. return NULL; /* Not found */
  87. }
  88. static int __init x86_fxsr_setup(char *s)
  89. {
  90. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  91. setup_clear_cpu_cap(X86_FEATURE_XMM);
  92. return 1;
  93. }
  94. __setup("nofxsr", x86_fxsr_setup);
  95. static int __init x86_sep_setup(char *s)
  96. {
  97. setup_clear_cpu_cap(X86_FEATURE_SEP);
  98. return 1;
  99. }
  100. __setup("nosep", x86_sep_setup);
  101. /* Standard macro to see if a specific flag is changeable */
  102. static inline int flag_is_changeable_p(u32 flag)
  103. {
  104. u32 f1, f2;
  105. asm("pushfl\n\t"
  106. "pushfl\n\t"
  107. "popl %0\n\t"
  108. "movl %0,%1\n\t"
  109. "xorl %2,%0\n\t"
  110. "pushl %0\n\t"
  111. "popfl\n\t"
  112. "pushfl\n\t"
  113. "popl %0\n\t"
  114. "popfl\n\t"
  115. : "=&r" (f1), "=&r" (f2)
  116. : "ir" (flag));
  117. return ((f1^f2) & flag) != 0;
  118. }
  119. /* Probe for the CPUID instruction */
  120. static int __cpuinit have_cpuid_p(void)
  121. {
  122. return flag_is_changeable_p(X86_EFLAGS_ID);
  123. }
  124. static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  125. {
  126. if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr) {
  127. /* Disable processor serial number */
  128. unsigned long lo, hi;
  129. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  130. lo |= 0x200000;
  131. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  132. printk(KERN_NOTICE "CPU serial number disabled.\n");
  133. clear_cpu_cap(c, X86_FEATURE_PN);
  134. /* Disabling the serial number may affect the cpuid level */
  135. c->cpuid_level = cpuid_eax(0);
  136. }
  137. }
  138. static int __init x86_serial_nr_setup(char *s)
  139. {
  140. disable_x86_serial_nr = 0;
  141. return 1;
  142. }
  143. __setup("serialnumber", x86_serial_nr_setup);
  144. __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
  145. /* Current gdt points %fs at the "master" per-cpu area: after this,
  146. * it's on the real one. */
  147. void switch_to_new_gdt(void)
  148. {
  149. struct desc_ptr gdt_descr;
  150. gdt_descr.address = (long)get_cpu_gdt_table(smp_processor_id());
  151. gdt_descr.size = GDT_SIZE - 1;
  152. load_gdt(&gdt_descr);
  153. asm("mov %0, %%fs" : : "r" (__KERNEL_PERCPU) : "memory");
  154. }
  155. static struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  156. static void __cpuinit default_init(struct cpuinfo_x86 *c)
  157. {
  158. /* Not much we can do here... */
  159. /* Check if at least it has cpuid */
  160. if (c->cpuid_level == -1) {
  161. /* No cpuid. It must be an ancient CPU */
  162. if (c->x86 == 4)
  163. strcpy(c->x86_model_id, "486");
  164. else if (c->x86 == 3)
  165. strcpy(c->x86_model_id, "386");
  166. }
  167. }
  168. static struct cpu_dev __cpuinitdata default_cpu = {
  169. .c_init = default_init,
  170. .c_vendor = "Unknown",
  171. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  172. };
  173. int __cpuinit get_model_name(struct cpuinfo_x86 *c)
  174. {
  175. unsigned int *v;
  176. char *p, *q;
  177. if (c->extended_cpuid_level < 0x80000004)
  178. return 0;
  179. v = (unsigned int *) c->x86_model_id;
  180. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  181. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  182. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  183. c->x86_model_id[48] = 0;
  184. /* Intel chips right-justify this string for some dumb reason;
  185. undo that brain damage */
  186. p = q = &c->x86_model_id[0];
  187. while (*p == ' ')
  188. p++;
  189. if (p != q) {
  190. while (*p)
  191. *q++ = *p++;
  192. while (q <= &c->x86_model_id[48])
  193. *q++ = '\0'; /* Zero-pad the rest */
  194. }
  195. return 1;
  196. }
  197. void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  198. {
  199. unsigned int n, dummy, ebx, ecx, edx, l2size;
  200. n = c->extended_cpuid_level;
  201. if (n >= 0x80000005) {
  202. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  203. printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
  204. edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
  205. c->x86_cache_size = (ecx>>24) + (edx>>24);
  206. }
  207. if (n < 0x80000006) /* Some chips just has a large L1. */
  208. return;
  209. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  210. l2size = ecx >> 16;
  211. /* do processor-specific cache resizing */
  212. if (this_cpu->c_size_cache)
  213. l2size = this_cpu->c_size_cache(c, l2size);
  214. /* Allow user to override all this if necessary. */
  215. if (cachesize_override != -1)
  216. l2size = cachesize_override;
  217. if (l2size == 0)
  218. return; /* Again, no L2 cache is possible */
  219. c->x86_cache_size = l2size;
  220. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  221. l2size, ecx & 0xFF);
  222. }
  223. #ifdef CONFIG_X86_HT
  224. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  225. {
  226. u32 eax, ebx, ecx, edx;
  227. int index_msb, core_bits;
  228. if (!cpu_has(c, X86_FEATURE_HT))
  229. return;
  230. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  231. goto out;
  232. cpuid(1, &eax, &ebx, &ecx, &edx);
  233. smp_num_siblings = (ebx & 0xff0000) >> 16;
  234. if (smp_num_siblings == 1) {
  235. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  236. } else if (smp_num_siblings > 1) {
  237. if (smp_num_siblings > NR_CPUS) {
  238. printk(KERN_WARNING "CPU: Unsupported number of siblings %d",
  239. smp_num_siblings);
  240. smp_num_siblings = 1;
  241. return;
  242. }
  243. index_msb = get_count_order(smp_num_siblings);
  244. c->phys_proc_id = phys_pkg_id(c->initial_apicid, index_msb);
  245. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  246. index_msb = get_count_order(smp_num_siblings);
  247. core_bits = get_count_order(c->x86_max_cores);
  248. c->cpu_core_id = phys_pkg_id(c->initial_apicid, index_msb) &
  249. ((1 << core_bits) - 1);
  250. }
  251. out:
  252. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  253. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  254. c->phys_proc_id);
  255. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  256. c->cpu_core_id);
  257. }
  258. }
  259. #endif
  260. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  261. {
  262. char *v = c->x86_vendor_id;
  263. int i;
  264. static int printed;
  265. for (i = 0; i < X86_VENDOR_NUM; i++) {
  266. if (!cpu_devs[i])
  267. break;
  268. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  269. (cpu_devs[i]->c_ident[1] &&
  270. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  271. this_cpu = cpu_devs[i];
  272. c->x86_vendor = this_cpu->c_x86_vendor;
  273. return;
  274. }
  275. }
  276. if (!printed) {
  277. printed++;
  278. printk(KERN_ERR "CPU: Vendor unknown, using generic init.\n");
  279. printk(KERN_ERR "CPU: Your system may be unstable.\n");
  280. }
  281. c->x86_vendor = X86_VENDOR_UNKNOWN;
  282. this_cpu = &default_cpu;
  283. }
  284. void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
  285. {
  286. /* Get vendor name */
  287. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  288. (unsigned int *)&c->x86_vendor_id[0],
  289. (unsigned int *)&c->x86_vendor_id[8],
  290. (unsigned int *)&c->x86_vendor_id[4]);
  291. c->x86 = 4;
  292. /* Intel-defined flags: level 0x00000001 */
  293. if (c->cpuid_level >= 0x00000001) {
  294. u32 junk, tfms, cap0, misc;
  295. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  296. c->x86 = (tfms >> 8) & 0xf;
  297. c->x86_model = (tfms >> 4) & 0xf;
  298. c->x86_mask = tfms & 0xf;
  299. if (c->x86 == 0xf)
  300. c->x86 += (tfms >> 20) & 0xff;
  301. if (c->x86 >= 0x6)
  302. c->x86_model += ((tfms >> 16) & 0xf) << 4;
  303. if (cap0 & (1<<19)) {
  304. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  305. c->x86_cache_alignment = c->x86_clflush_size;
  306. }
  307. }
  308. }
  309. static void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
  310. {
  311. u32 tfms, xlvl;
  312. u32 ebx;
  313. /* Intel-defined flags: level 0x00000001 */
  314. if (c->cpuid_level >= 0x00000001) {
  315. u32 capability, excap;
  316. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  317. c->x86_capability[0] = capability;
  318. c->x86_capability[4] = excap;
  319. }
  320. /* AMD-defined flags: level 0x80000001 */
  321. xlvl = cpuid_eax(0x80000000);
  322. c->extended_cpuid_level = xlvl;
  323. if ((xlvl & 0xffff0000) == 0x80000000) {
  324. if (xlvl >= 0x80000001) {
  325. c->x86_capability[1] = cpuid_edx(0x80000001);
  326. c->x86_capability[6] = cpuid_ecx(0x80000001);
  327. }
  328. }
  329. }
  330. /*
  331. * Do minimum CPU detection early.
  332. * Fields really needed: vendor, cpuid_level, family, model, mask,
  333. * cache alignment.
  334. * The others are not touched to avoid unwanted side effects.
  335. *
  336. * WARNING: this function is only called on the BP. Don't add code here
  337. * that is supposed to run on all CPUs.
  338. */
  339. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  340. {
  341. c->x86_clflush_size = 32;
  342. c->x86_cache_alignment = c->x86_clflush_size;
  343. if (!have_cpuid_p())
  344. return;
  345. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  346. c->extended_cpuid_level = 0;
  347. cpu_detect(c);
  348. get_cpu_vendor(c);
  349. get_cpu_cap(c);
  350. if (this_cpu->c_early_init)
  351. this_cpu->c_early_init(c);
  352. validate_pat_support(c);
  353. }
  354. void __init early_cpu_init(void)
  355. {
  356. struct cpu_dev **cdev;
  357. int count = 0;
  358. printk("KERNEL supported cpus:\n");
  359. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  360. struct cpu_dev *cpudev = *cdev;
  361. unsigned int j;
  362. if (count >= X86_VENDOR_NUM)
  363. break;
  364. cpu_devs[count] = cpudev;
  365. count++;
  366. for (j = 0; j < 2; j++) {
  367. if (!cpudev->c_ident[j])
  368. continue;
  369. printk(" %s %s\n", cpudev->c_vendor,
  370. cpudev->c_ident[j]);
  371. }
  372. }
  373. early_identify_cpu(&boot_cpu_data);
  374. }
  375. /*
  376. * The NOPL instruction is supposed to exist on all CPUs with
  377. * family >= 6, unfortunately, that's not true in practice because
  378. * of early VIA chips and (more importantly) broken virtualizers that
  379. * are not easy to detect. Hence, probe for it based on first
  380. * principles.
  381. */
  382. static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
  383. {
  384. const u32 nopl_signature = 0x888c53b1; /* Random number */
  385. u32 has_nopl = nopl_signature;
  386. clear_cpu_cap(c, X86_FEATURE_NOPL);
  387. if (c->x86 >= 6) {
  388. asm volatile("\n"
  389. "1: .byte 0x0f,0x1f,0xc0\n" /* nopl %eax */
  390. "2:\n"
  391. " .section .fixup,\"ax\"\n"
  392. "3: xor %0,%0\n"
  393. " jmp 2b\n"
  394. " .previous\n"
  395. _ASM_EXTABLE(1b,3b)
  396. : "+a" (has_nopl));
  397. if (has_nopl == nopl_signature)
  398. set_cpu_cap(c, X86_FEATURE_NOPL);
  399. }
  400. }
  401. static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
  402. {
  403. if (!have_cpuid_p())
  404. return;
  405. c->extended_cpuid_level = 0;
  406. cpu_detect(c);
  407. get_cpu_vendor(c);
  408. get_cpu_cap(c);
  409. if (c->cpuid_level >= 0x00000001) {
  410. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  411. #ifdef CONFIG_X86_HT
  412. c->apicid = phys_pkg_id(c->initial_apicid, 0);
  413. c->phys_proc_id = c->initial_apicid;
  414. #else
  415. c->apicid = c->initial_apicid;
  416. #endif
  417. }
  418. if (c->extended_cpuid_level >= 0x80000004)
  419. get_model_name(c); /* Default name */
  420. init_scattered_cpuid_features(c);
  421. detect_nopl(c);
  422. }
  423. /*
  424. * This does the hard work of actually picking apart the CPU stuff...
  425. */
  426. static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  427. {
  428. int i;
  429. c->loops_per_jiffy = loops_per_jiffy;
  430. c->x86_cache_size = -1;
  431. c->x86_vendor = X86_VENDOR_UNKNOWN;
  432. c->cpuid_level = -1; /* CPUID not detected */
  433. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  434. c->x86_vendor_id[0] = '\0'; /* Unset */
  435. c->x86_model_id[0] = '\0'; /* Unset */
  436. c->x86_max_cores = 1;
  437. c->x86_clflush_size = 32;
  438. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  439. if (!have_cpuid_p()) {
  440. /*
  441. * First of all, decide if this is a 486 or higher
  442. * It's a 486 if we can modify the AC flag
  443. */
  444. if (flag_is_changeable_p(X86_EFLAGS_AC))
  445. c->x86 = 4;
  446. else
  447. c->x86 = 3;
  448. }
  449. generic_identify(c);
  450. if (this_cpu->c_identify)
  451. this_cpu->c_identify(c);
  452. /*
  453. * Vendor-specific initialization. In this section we
  454. * canonicalize the feature flags, meaning if there are
  455. * features a certain CPU supports which CPUID doesn't
  456. * tell us, CPUID claiming incorrect flags, or other bugs,
  457. * we handle them here.
  458. *
  459. * At the end of this section, c->x86_capability better
  460. * indicate the features this CPU genuinely supports!
  461. */
  462. if (this_cpu->c_init)
  463. this_cpu->c_init(c);
  464. /* Disable the PN if appropriate */
  465. squash_the_stupid_serial_number(c);
  466. /*
  467. * The vendor-specific functions might have changed features. Now
  468. * we do "generic changes."
  469. */
  470. /* If the model name is still unset, do table lookup. */
  471. if (!c->x86_model_id[0]) {
  472. char *p;
  473. p = table_lookup_model(c);
  474. if (p)
  475. strcpy(c->x86_model_id, p);
  476. else
  477. /* Last resort... */
  478. sprintf(c->x86_model_id, "%02x/%02x",
  479. c->x86, c->x86_model);
  480. }
  481. /*
  482. * On SMP, boot_cpu_data holds the common feature set between
  483. * all CPUs; so make sure that we indicate which features are
  484. * common between the CPUs. The first time this routine gets
  485. * executed, c == &boot_cpu_data.
  486. */
  487. if (c != &boot_cpu_data) {
  488. /* AND the already accumulated flags with these */
  489. for (i = 0; i < NCAPINTS; i++)
  490. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  491. }
  492. /* Clear all flags overriden by options */
  493. for (i = 0; i < NCAPINTS; i++)
  494. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  495. /* Init Machine Check Exception if available. */
  496. mcheck_init(c);
  497. select_idle_routine(c);
  498. }
  499. void __init identify_boot_cpu(void)
  500. {
  501. identify_cpu(&boot_cpu_data);
  502. sysenter_setup();
  503. enable_sep_cpu();
  504. }
  505. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  506. {
  507. BUG_ON(c == &boot_cpu_data);
  508. identify_cpu(c);
  509. enable_sep_cpu();
  510. mtrr_ap_init();
  511. }
  512. struct msr_range {
  513. unsigned min;
  514. unsigned max;
  515. };
  516. static struct msr_range msr_range_array[] __cpuinitdata = {
  517. { 0x00000000, 0x00000418},
  518. { 0xc0000000, 0xc000040b},
  519. { 0xc0010000, 0xc0010142},
  520. { 0xc0011000, 0xc001103b},
  521. };
  522. static void __cpuinit print_cpu_msr(void)
  523. {
  524. unsigned index;
  525. u64 val;
  526. int i;
  527. unsigned index_min, index_max;
  528. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  529. index_min = msr_range_array[i].min;
  530. index_max = msr_range_array[i].max;
  531. for (index = index_min; index < index_max; index++) {
  532. if (rdmsrl_amd_safe(index, &val))
  533. continue;
  534. printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
  535. }
  536. }
  537. }
  538. static int show_msr __cpuinitdata;
  539. static __init int setup_show_msr(char *arg)
  540. {
  541. int num;
  542. get_option(&arg, &num);
  543. if (num > 0)
  544. show_msr = num;
  545. return 1;
  546. }
  547. __setup("show_msr=", setup_show_msr);
  548. static __init int setup_noclflush(char *arg)
  549. {
  550. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  551. return 1;
  552. }
  553. __setup("noclflush", setup_noclflush);
  554. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  555. {
  556. char *vendor = NULL;
  557. if (c->x86_vendor < X86_VENDOR_NUM)
  558. vendor = this_cpu->c_vendor;
  559. else if (c->cpuid_level >= 0)
  560. vendor = c->x86_vendor_id;
  561. if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
  562. printk(KERN_CONT "%s ", vendor);
  563. if (c->x86_model_id[0])
  564. printk(KERN_CONT "%s", c->x86_model_id);
  565. else
  566. printk(KERN_CONT "%d86", c->x86);
  567. if (c->x86_mask || c->cpuid_level >= 0)
  568. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  569. else
  570. printk(KERN_CONT "\n");
  571. #ifdef CONFIG_SMP
  572. if (c->cpu_index < show_msr)
  573. print_cpu_msr();
  574. #else
  575. if (show_msr)
  576. print_cpu_msr();
  577. #endif
  578. }
  579. static __init int setup_disablecpuid(char *arg)
  580. {
  581. int bit;
  582. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  583. setup_clear_cpu_cap(bit);
  584. else
  585. return 0;
  586. return 1;
  587. }
  588. __setup("clearcpuid=", setup_disablecpuid);
  589. cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
  590. /* Make sure %fs is initialized properly in idle threads */
  591. struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
  592. {
  593. memset(regs, 0, sizeof(struct pt_regs));
  594. regs->fs = __KERNEL_PERCPU;
  595. return regs;
  596. }
  597. /*
  598. * cpu_init() initializes state that is per-CPU. Some data is already
  599. * initialized (naturally) in the bootstrap process, such as the GDT
  600. * and IDT. We reload them nevertheless, this function acts as a
  601. * 'CPU state barrier', nothing should get across.
  602. */
  603. void __cpuinit cpu_init(void)
  604. {
  605. int cpu = smp_processor_id();
  606. struct task_struct *curr = current;
  607. struct tss_struct *t = &per_cpu(init_tss, cpu);
  608. struct thread_struct *thread = &curr->thread;
  609. if (cpu_test_and_set(cpu, cpu_initialized)) {
  610. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
  611. for (;;) local_irq_enable();
  612. }
  613. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  614. if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  615. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  616. load_idt(&idt_descr);
  617. switch_to_new_gdt();
  618. /*
  619. * Set up and load the per-CPU TSS and LDT
  620. */
  621. atomic_inc(&init_mm.mm_count);
  622. curr->active_mm = &init_mm;
  623. if (curr->mm)
  624. BUG();
  625. enter_lazy_tlb(&init_mm, curr);
  626. load_sp0(t, thread);
  627. set_tss_desc(cpu, t);
  628. load_TR_desc();
  629. load_LDT(&init_mm.context);
  630. #ifdef CONFIG_DOUBLEFAULT
  631. /* Set up doublefault TSS pointer in the GDT */
  632. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  633. #endif
  634. /* Clear %gs. */
  635. asm volatile ("mov %0, %%gs" : : "r" (0));
  636. /* Clear all 6 debug registers: */
  637. set_debugreg(0, 0);
  638. set_debugreg(0, 1);
  639. set_debugreg(0, 2);
  640. set_debugreg(0, 3);
  641. set_debugreg(0, 6);
  642. set_debugreg(0, 7);
  643. /*
  644. * Force FPU initialization:
  645. */
  646. if (cpu_has_xsave)
  647. current_thread_info()->status = TS_XSAVE;
  648. else
  649. current_thread_info()->status = 0;
  650. clear_used_math();
  651. mxcsr_feature_mask_init();
  652. /*
  653. * Boot processor to setup the FP and extended state context info.
  654. */
  655. if (!smp_processor_id())
  656. init_thread_xstate();
  657. xsave_init();
  658. }
  659. #ifdef CONFIG_HOTPLUG_CPU
  660. void __cpuinit cpu_uninit(void)
  661. {
  662. int cpu = raw_smp_processor_id();
  663. cpu_clear(cpu, cpu_initialized);
  664. /* lazy TLB state */
  665. per_cpu(cpu_tlbstate, cpu).state = 0;
  666. per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
  667. }
  668. #endif