common.c 20 KB

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