common.c 20 KB

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