common.c 18 KB

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