common.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  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 __cpuinitdata = &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. void __init cpu_detect(struct cpuinfo_x86 *c)
  194. {
  195. /* Get vendor name */
  196. cpuid(0x00000000, &c->cpuid_level,
  197. (int *)&c->x86_vendor_id[0],
  198. (int *)&c->x86_vendor_id[8],
  199. (int *)&c->x86_vendor_id[4]);
  200. c->x86 = 4;
  201. if (c->cpuid_level >= 0x00000001) {
  202. u32 junk, tfms, cap0, misc;
  203. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  204. c->x86 = (tfms >> 8) & 15;
  205. c->x86_model = (tfms >> 4) & 15;
  206. if (c->x86 == 0xf)
  207. c->x86 += (tfms >> 20) & 0xff;
  208. if (c->x86 >= 0x6)
  209. c->x86_model += ((tfms >> 16) & 0xF) << 4;
  210. c->x86_mask = tfms & 15;
  211. if (cap0 & (1<<19))
  212. c->x86_cache_alignment = ((misc >> 8) & 0xff) * 8;
  213. }
  214. }
  215. /* Do minimum CPU detection early.
  216. Fields really needed: vendor, cpuid_level, family, model, mask, cache alignment.
  217. The others are not touched to avoid unwanted side effects.
  218. WARNING: this function is only called on the BP. Don't add code here
  219. that is supposed to run on all CPUs. */
  220. static void __init early_cpu_detect(void)
  221. {
  222. struct cpuinfo_x86 *c = &boot_cpu_data;
  223. c->x86_cache_alignment = 32;
  224. if (!have_cpuid_p())
  225. return;
  226. cpu_detect(c);
  227. get_cpu_vendor(c, 1);
  228. }
  229. static void __cpuinit generic_identify(struct cpuinfo_x86 * c)
  230. {
  231. u32 tfms, xlvl;
  232. int ebx;
  233. if (have_cpuid_p()) {
  234. /* Get vendor name */
  235. cpuid(0x00000000, &c->cpuid_level,
  236. (int *)&c->x86_vendor_id[0],
  237. (int *)&c->x86_vendor_id[8],
  238. (int *)&c->x86_vendor_id[4]);
  239. get_cpu_vendor(c, 0);
  240. /* Initialize the standard set of capabilities */
  241. /* Note that the vendor-specific code below might override */
  242. /* Intel-defined flags: level 0x00000001 */
  243. if ( c->cpuid_level >= 0x00000001 ) {
  244. u32 capability, excap;
  245. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  246. c->x86_capability[0] = capability;
  247. c->x86_capability[4] = excap;
  248. c->x86 = (tfms >> 8) & 15;
  249. c->x86_model = (tfms >> 4) & 15;
  250. if (c->x86 == 0xf)
  251. c->x86 += (tfms >> 20) & 0xff;
  252. if (c->x86 >= 0x6)
  253. c->x86_model += ((tfms >> 16) & 0xF) << 4;
  254. c->x86_mask = tfms & 15;
  255. #ifdef CONFIG_X86_HT
  256. c->apicid = phys_pkg_id((ebx >> 24) & 0xFF, 0);
  257. #else
  258. c->apicid = (ebx >> 24) & 0xFF;
  259. #endif
  260. if (c->x86_capability[0] & (1<<19))
  261. c->x86_clflush_size = ((ebx >> 8) & 0xff) * 8;
  262. } else {
  263. /* Have CPUID level 0 only - unheard of */
  264. c->x86 = 4;
  265. }
  266. /* AMD-defined flags: level 0x80000001 */
  267. xlvl = cpuid_eax(0x80000000);
  268. if ( (xlvl & 0xffff0000) == 0x80000000 ) {
  269. if ( xlvl >= 0x80000001 ) {
  270. c->x86_capability[1] = cpuid_edx(0x80000001);
  271. c->x86_capability[6] = cpuid_ecx(0x80000001);
  272. }
  273. if ( xlvl >= 0x80000004 )
  274. get_model_name(c); /* Default name */
  275. }
  276. }
  277. early_intel_workaround(c);
  278. #ifdef CONFIG_X86_HT
  279. c->phys_proc_id = (cpuid_ebx(1) >> 24) & 0xff;
  280. #endif
  281. }
  282. static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  283. {
  284. if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr ) {
  285. /* Disable processor serial number */
  286. unsigned long lo,hi;
  287. rdmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
  288. lo |= 0x200000;
  289. wrmsr(MSR_IA32_BBL_CR_CTL,lo,hi);
  290. printk(KERN_NOTICE "CPU serial number disabled.\n");
  291. clear_bit(X86_FEATURE_PN, c->x86_capability);
  292. /* Disabling the serial number may affect the cpuid level */
  293. c->cpuid_level = cpuid_eax(0);
  294. }
  295. }
  296. static int __init x86_serial_nr_setup(char *s)
  297. {
  298. disable_x86_serial_nr = 0;
  299. return 1;
  300. }
  301. __setup("serialnumber", x86_serial_nr_setup);
  302. /*
  303. * This does the hard work of actually picking apart the CPU stuff...
  304. */
  305. void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  306. {
  307. int i;
  308. c->loops_per_jiffy = loops_per_jiffy;
  309. c->x86_cache_size = -1;
  310. c->x86_vendor = X86_VENDOR_UNKNOWN;
  311. c->cpuid_level = -1; /* CPUID not detected */
  312. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  313. c->x86_vendor_id[0] = '\0'; /* Unset */
  314. c->x86_model_id[0] = '\0'; /* Unset */
  315. c->x86_max_cores = 1;
  316. c->x86_clflush_size = 32;
  317. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  318. if (!have_cpuid_p()) {
  319. /* First of all, decide if this is a 486 or higher */
  320. /* It's a 486 if we can modify the AC flag */
  321. if ( flag_is_changeable_p(X86_EFLAGS_AC) )
  322. c->x86 = 4;
  323. else
  324. c->x86 = 3;
  325. }
  326. generic_identify(c);
  327. printk(KERN_DEBUG "CPU: After generic identify, caps:");
  328. for (i = 0; i < NCAPINTS; i++)
  329. printk(" %08lx", c->x86_capability[i]);
  330. printk("\n");
  331. if (this_cpu->c_identify) {
  332. this_cpu->c_identify(c);
  333. printk(KERN_DEBUG "CPU: After vendor identify, caps:");
  334. for (i = 0; i < NCAPINTS; i++)
  335. printk(" %08lx", c->x86_capability[i]);
  336. printk("\n");
  337. }
  338. /*
  339. * Vendor-specific initialization. In this section we
  340. * canonicalize the feature flags, meaning if there are
  341. * features a certain CPU supports which CPUID doesn't
  342. * tell us, CPUID claiming incorrect flags, or other bugs,
  343. * we handle them here.
  344. *
  345. * At the end of this section, c->x86_capability better
  346. * indicate the features this CPU genuinely supports!
  347. */
  348. if (this_cpu->c_init)
  349. this_cpu->c_init(c);
  350. /* Disable the PN if appropriate */
  351. squash_the_stupid_serial_number(c);
  352. /*
  353. * The vendor-specific functions might have changed features. Now
  354. * we do "generic changes."
  355. */
  356. /* TSC disabled? */
  357. if ( tsc_disable )
  358. clear_bit(X86_FEATURE_TSC, c->x86_capability);
  359. /* FXSR disabled? */
  360. if (disable_x86_fxsr) {
  361. clear_bit(X86_FEATURE_FXSR, c->x86_capability);
  362. clear_bit(X86_FEATURE_XMM, c->x86_capability);
  363. }
  364. /* SEP disabled? */
  365. if (disable_x86_sep)
  366. clear_bit(X86_FEATURE_SEP, c->x86_capability);
  367. if (disable_pse)
  368. clear_bit(X86_FEATURE_PSE, c->x86_capability);
  369. /* If the model name is still unset, do table lookup. */
  370. if ( !c->x86_model_id[0] ) {
  371. char *p;
  372. p = table_lookup_model(c);
  373. if ( p )
  374. strcpy(c->x86_model_id, p);
  375. else
  376. /* Last resort... */
  377. sprintf(c->x86_model_id, "%02x/%02x",
  378. c->x86, c->x86_model);
  379. }
  380. /* Now the feature flags better reflect actual CPU features! */
  381. printk(KERN_DEBUG "CPU: After all inits, caps:");
  382. for (i = 0; i < NCAPINTS; i++)
  383. printk(" %08lx", c->x86_capability[i]);
  384. printk("\n");
  385. /*
  386. * On SMP, boot_cpu_data holds the common feature set between
  387. * all CPUs; so make sure that we indicate which features are
  388. * common between the CPUs. The first time this routine gets
  389. * executed, c == &boot_cpu_data.
  390. */
  391. if ( c != &boot_cpu_data ) {
  392. /* AND the already accumulated flags with these */
  393. for ( i = 0 ; i < NCAPINTS ; i++ )
  394. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  395. }
  396. /* Init Machine Check Exception if available. */
  397. mcheck_init(c);
  398. if (c == &boot_cpu_data)
  399. sysenter_setup();
  400. enable_sep_cpu();
  401. if (c == &boot_cpu_data)
  402. mtrr_bp_init();
  403. else
  404. mtrr_ap_init();
  405. }
  406. #ifdef CONFIG_X86_HT
  407. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  408. {
  409. u32 eax, ebx, ecx, edx;
  410. int index_msb, core_bits;
  411. cpuid(1, &eax, &ebx, &ecx, &edx);
  412. if (!cpu_has(c, X86_FEATURE_HT) || cpu_has(c, X86_FEATURE_CMP_LEGACY))
  413. return;
  414. smp_num_siblings = (ebx & 0xff0000) >> 16;
  415. if (smp_num_siblings == 1) {
  416. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  417. } else if (smp_num_siblings > 1 ) {
  418. if (smp_num_siblings > NR_CPUS) {
  419. printk(KERN_WARNING "CPU: Unsupported number of the "
  420. "siblings %d", smp_num_siblings);
  421. smp_num_siblings = 1;
  422. return;
  423. }
  424. index_msb = get_count_order(smp_num_siblings);
  425. c->phys_proc_id = phys_pkg_id((ebx >> 24) & 0xFF, index_msb);
  426. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  427. c->phys_proc_id);
  428. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  429. index_msb = get_count_order(smp_num_siblings) ;
  430. core_bits = get_count_order(c->x86_max_cores);
  431. c->cpu_core_id = phys_pkg_id((ebx >> 24) & 0xFF, index_msb) &
  432. ((1 << core_bits) - 1);
  433. if (c->x86_max_cores > 1)
  434. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  435. c->cpu_core_id);
  436. }
  437. }
  438. #endif
  439. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  440. {
  441. char *vendor = NULL;
  442. if (c->x86_vendor < X86_VENDOR_NUM)
  443. vendor = this_cpu->c_vendor;
  444. else if (c->cpuid_level >= 0)
  445. vendor = c->x86_vendor_id;
  446. if (vendor && strncmp(c->x86_model_id, vendor, strlen(vendor)))
  447. printk("%s ", vendor);
  448. if (!c->x86_model_id[0])
  449. printk("%d86", c->x86);
  450. else
  451. printk("%s", c->x86_model_id);
  452. if (c->x86_mask || c->cpuid_level >= 0)
  453. printk(" stepping %02x\n", c->x86_mask);
  454. else
  455. printk("\n");
  456. }
  457. cpumask_t cpu_initialized __cpuinitdata = CPU_MASK_NONE;
  458. /* This is hacky. :)
  459. * We're emulating future behavior.
  460. * In the future, the cpu-specific init functions will be called implicitly
  461. * via the magic of initcalls.
  462. * They will insert themselves into the cpu_devs structure.
  463. * Then, when cpu_init() is called, we can just iterate over that array.
  464. */
  465. extern int intel_cpu_init(void);
  466. extern int cyrix_init_cpu(void);
  467. extern int nsc_init_cpu(void);
  468. extern int amd_init_cpu(void);
  469. extern int centaur_init_cpu(void);
  470. extern int transmeta_init_cpu(void);
  471. extern int rise_init_cpu(void);
  472. extern int nexgen_init_cpu(void);
  473. extern int umc_init_cpu(void);
  474. void __init early_cpu_init(void)
  475. {
  476. intel_cpu_init();
  477. cyrix_init_cpu();
  478. nsc_init_cpu();
  479. amd_init_cpu();
  480. centaur_init_cpu();
  481. transmeta_init_cpu();
  482. rise_init_cpu();
  483. nexgen_init_cpu();
  484. umc_init_cpu();
  485. early_cpu_detect();
  486. #ifdef CONFIG_DEBUG_PAGEALLOC
  487. /* pse is not compatible with on-the-fly unmapping,
  488. * disable it even if the cpus claim to support it.
  489. */
  490. clear_bit(X86_FEATURE_PSE, boot_cpu_data.x86_capability);
  491. disable_pse = 1;
  492. #endif
  493. }
  494. /* Make sure %gs is initialized properly in idle threads */
  495. struct pt_regs * __devinit idle_regs(struct pt_regs *regs)
  496. {
  497. memset(regs, 0, sizeof(struct pt_regs));
  498. regs->xgs = __KERNEL_PDA;
  499. return regs;
  500. }
  501. static __cpuinit int alloc_gdt(int cpu)
  502. {
  503. struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
  504. struct desc_struct *gdt;
  505. struct i386_pda *pda;
  506. gdt = (struct desc_struct *)cpu_gdt_descr->address;
  507. pda = cpu_pda(cpu);
  508. /*
  509. * This is a horrible hack to allocate the GDT. The problem
  510. * is that cpu_init() is called really early for the boot CPU
  511. * (and hence needs bootmem) but much later for the secondary
  512. * CPUs, when bootmem will have gone away
  513. */
  514. if (NODE_DATA(0)->bdata->node_bootmem_map) {
  515. BUG_ON(gdt != NULL || pda != NULL);
  516. gdt = alloc_bootmem_pages(PAGE_SIZE);
  517. pda = alloc_bootmem(sizeof(*pda));
  518. /* alloc_bootmem(_pages) panics on failure, so no check */
  519. memset(gdt, 0, PAGE_SIZE);
  520. memset(pda, 0, sizeof(*pda));
  521. } else {
  522. /* GDT and PDA might already have been allocated if
  523. this is a CPU hotplug re-insertion. */
  524. if (gdt == NULL)
  525. gdt = (struct desc_struct *)get_zeroed_page(GFP_KERNEL);
  526. if (pda == NULL)
  527. pda = kmalloc_node(sizeof(*pda), GFP_KERNEL, cpu_to_node(cpu));
  528. if (unlikely(!gdt || !pda)) {
  529. free_pages((unsigned long)gdt, 0);
  530. kfree(pda);
  531. return 0;
  532. }
  533. }
  534. cpu_gdt_descr->address = (unsigned long)gdt;
  535. cpu_pda(cpu) = pda;
  536. return 1;
  537. }
  538. /* Initial PDA used by boot CPU */
  539. struct i386_pda boot_pda = {
  540. ._pda = &boot_pda,
  541. .cpu_number = 0,
  542. .pcurrent = &init_task,
  543. };
  544. static inline void set_kernel_gs(void)
  545. {
  546. /* Set %gs for this CPU's PDA. Memory clobber is to create a
  547. barrier with respect to any PDA operations, so the compiler
  548. doesn't move any before here. */
  549. asm volatile ("mov %0, %%gs" : : "r" (__KERNEL_PDA) : "memory");
  550. }
  551. /* Initialize the CPU's GDT and PDA. The boot CPU does this for
  552. itself, but secondaries find this done for them. */
  553. __cpuinit int init_gdt(int cpu, struct task_struct *idle)
  554. {
  555. struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
  556. struct desc_struct *gdt;
  557. struct i386_pda *pda;
  558. /* For non-boot CPUs, the GDT and PDA should already have been
  559. allocated. */
  560. if (!alloc_gdt(cpu)) {
  561. printk(KERN_CRIT "CPU%d failed to allocate GDT or PDA\n", cpu);
  562. return 0;
  563. }
  564. gdt = (struct desc_struct *)cpu_gdt_descr->address;
  565. pda = cpu_pda(cpu);
  566. BUG_ON(gdt == NULL || pda == NULL);
  567. /*
  568. * Initialize the per-CPU GDT with the boot GDT,
  569. * and set up the GDT descriptor:
  570. */
  571. memcpy(gdt, cpu_gdt_table, GDT_SIZE);
  572. cpu_gdt_descr->size = GDT_SIZE - 1;
  573. pack_descriptor((u32 *)&gdt[GDT_ENTRY_PDA].a,
  574. (u32 *)&gdt[GDT_ENTRY_PDA].b,
  575. (unsigned long)pda, sizeof(*pda) - 1,
  576. 0x80 | DESCTYPE_S | 0x2, 0); /* present read-write data segment */
  577. memset(pda, 0, sizeof(*pda));
  578. pda->_pda = pda;
  579. pda->cpu_number = cpu;
  580. pda->pcurrent = idle;
  581. return 1;
  582. }
  583. void __cpuinit cpu_set_gdt(int cpu)
  584. {
  585. struct Xgt_desc_struct *cpu_gdt_descr = &per_cpu(cpu_gdt_descr, cpu);
  586. /* Reinit these anyway, even if they've already been done (on
  587. the boot CPU, this will transition from the boot gdt+pda to
  588. the real ones). */
  589. load_gdt(cpu_gdt_descr);
  590. set_kernel_gs();
  591. }
  592. /* Common CPU init for both boot and secondary CPUs */
  593. static void __cpuinit _cpu_init(int cpu, struct task_struct *curr)
  594. {
  595. struct tss_struct * t = &per_cpu(init_tss, cpu);
  596. struct thread_struct *thread = &curr->thread;
  597. if (cpu_test_and_set(cpu, cpu_initialized)) {
  598. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
  599. for (;;) local_irq_enable();
  600. }
  601. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  602. if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  603. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  604. if (tsc_disable && cpu_has_tsc) {
  605. printk(KERN_NOTICE "Disabling TSC...\n");
  606. /**** FIX-HPA: DOES THIS REALLY BELONG HERE? ****/
  607. clear_bit(X86_FEATURE_TSC, boot_cpu_data.x86_capability);
  608. set_in_cr4(X86_CR4_TSD);
  609. }
  610. load_idt(&idt_descr);
  611. /*
  612. * Set up and load the per-CPU TSS and LDT
  613. */
  614. atomic_inc(&init_mm.mm_count);
  615. curr->active_mm = &init_mm;
  616. if (curr->mm)
  617. BUG();
  618. enter_lazy_tlb(&init_mm, curr);
  619. load_esp0(t, thread);
  620. set_tss_desc(cpu,t);
  621. load_TR_desc();
  622. load_LDT(&init_mm.context);
  623. #ifdef CONFIG_DOUBLEFAULT
  624. /* Set up doublefault TSS pointer in the GDT */
  625. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  626. #endif
  627. /* Clear %fs. */
  628. asm volatile ("mov %0, %%fs" : : "r" (0));
  629. /* Clear all 6 debug registers: */
  630. set_debugreg(0, 0);
  631. set_debugreg(0, 1);
  632. set_debugreg(0, 2);
  633. set_debugreg(0, 3);
  634. set_debugreg(0, 6);
  635. set_debugreg(0, 7);
  636. /*
  637. * Force FPU initialization:
  638. */
  639. current_thread_info()->status = 0;
  640. clear_used_math();
  641. mxcsr_feature_mask_init();
  642. }
  643. /* Entrypoint to initialize secondary CPU */
  644. void __cpuinit secondary_cpu_init(void)
  645. {
  646. int cpu = smp_processor_id();
  647. struct task_struct *curr = current;
  648. _cpu_init(cpu, curr);
  649. }
  650. /*
  651. * cpu_init() initializes state that is per-CPU. Some data is already
  652. * initialized (naturally) in the bootstrap process, such as the GDT
  653. * and IDT. We reload them nevertheless, this function acts as a
  654. * 'CPU state barrier', nothing should get across.
  655. */
  656. void __cpuinit cpu_init(void)
  657. {
  658. int cpu = smp_processor_id();
  659. struct task_struct *curr = current;
  660. /* Set up the real GDT and PDA, so we can transition from the
  661. boot versions. */
  662. if (!init_gdt(cpu, curr)) {
  663. /* failed to allocate something; not much we can do... */
  664. for (;;)
  665. local_irq_enable();
  666. }
  667. cpu_set_gdt(cpu);
  668. _cpu_init(cpu, curr);
  669. }
  670. #ifdef CONFIG_HOTPLUG_CPU
  671. void __cpuinit cpu_uninit(void)
  672. {
  673. int cpu = raw_smp_processor_id();
  674. cpu_clear(cpu, cpu_initialized);
  675. /* lazy TLB state */
  676. per_cpu(cpu_tlbstate, cpu).state = 0;
  677. per_cpu(cpu_tlbstate, cpu).active_mm = &init_mm;
  678. }
  679. #endif