common.c 18 KB

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