common.c 18 KB

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