common.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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/perf_counter.h>
  21. #include <asm/pat.h>
  22. #include <asm/asm.h>
  23. #include <asm/numa.h>
  24. #include <asm/smp.h>
  25. #include <asm/cpu.h>
  26. #include <asm/cpumask.h>
  27. #ifdef CONFIG_X86_LOCAL_APIC
  28. #include <asm/mpspec.h>
  29. #include <asm/apic.h>
  30. #include <mach_apic.h>
  31. #include <asm/genapic.h>
  32. #include <asm/uv/uv.h>
  33. #endif
  34. #include <asm/pgtable.h>
  35. #include <asm/processor.h>
  36. #include <asm/desc.h>
  37. #include <asm/atomic.h>
  38. #include <asm/proto.h>
  39. #include <asm/sections.h>
  40. #include <asm/setup.h>
  41. #include <asm/hypervisor.h>
  42. #include "cpu.h"
  43. #ifdef CONFIG_X86_64
  44. /* all of these masks are initialized in setup_cpu_local_masks() */
  45. cpumask_var_t cpu_callin_mask;
  46. cpumask_var_t cpu_callout_mask;
  47. cpumask_var_t cpu_initialized_mask;
  48. /* representing cpus for which sibling maps can be computed */
  49. cpumask_var_t cpu_sibling_setup_mask;
  50. #else /* CONFIG_X86_32 */
  51. cpumask_t cpu_callin_map;
  52. cpumask_t cpu_callout_map;
  53. cpumask_t cpu_initialized;
  54. cpumask_t cpu_sibling_setup_map;
  55. #endif /* CONFIG_X86_32 */
  56. static struct cpu_dev *this_cpu __cpuinitdata;
  57. DEFINE_PER_CPU_PAGE_ALIGNED(struct gdt_page, gdt_page) = { .gdt = {
  58. #ifdef CONFIG_X86_64
  59. /*
  60. * We need valid kernel segments for data and code in long mode too
  61. * IRET will check the segment types kkeil 2000/10/28
  62. * Also sysret mandates a special GDT layout
  63. *
  64. * The TLS descriptors are currently at a different place compared to i386.
  65. * Hopefully nobody expects them at a fixed place (Wine?)
  66. */
  67. [GDT_ENTRY_KERNEL32_CS] = { { { 0x0000ffff, 0x00cf9b00 } } },
  68. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00af9b00 } } },
  69. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9300 } } },
  70. [GDT_ENTRY_DEFAULT_USER32_CS] = { { { 0x0000ffff, 0x00cffb00 } } },
  71. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff300 } } },
  72. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00affb00 } } },
  73. #else
  74. [GDT_ENTRY_KERNEL_CS] = { { { 0x0000ffff, 0x00cf9a00 } } },
  75. [GDT_ENTRY_KERNEL_DS] = { { { 0x0000ffff, 0x00cf9200 } } },
  76. [GDT_ENTRY_DEFAULT_USER_CS] = { { { 0x0000ffff, 0x00cffa00 } } },
  77. [GDT_ENTRY_DEFAULT_USER_DS] = { { { 0x0000ffff, 0x00cff200 } } },
  78. /*
  79. * Segments used for calling PnP BIOS have byte granularity.
  80. * They code segments and data segments have fixed 64k limits,
  81. * the transfer segment sizes are set at run time.
  82. */
  83. /* 32-bit code */
  84. [GDT_ENTRY_PNPBIOS_CS32] = { { { 0x0000ffff, 0x00409a00 } } },
  85. /* 16-bit code */
  86. [GDT_ENTRY_PNPBIOS_CS16] = { { { 0x0000ffff, 0x00009a00 } } },
  87. /* 16-bit data */
  88. [GDT_ENTRY_PNPBIOS_DS] = { { { 0x0000ffff, 0x00009200 } } },
  89. /* 16-bit data */
  90. [GDT_ENTRY_PNPBIOS_TS1] = { { { 0x00000000, 0x00009200 } } },
  91. /* 16-bit data */
  92. [GDT_ENTRY_PNPBIOS_TS2] = { { { 0x00000000, 0x00009200 } } },
  93. /*
  94. * The APM segments have byte granularity and their bases
  95. * are set at run time. All have 64k limits.
  96. */
  97. /* 32-bit code */
  98. [GDT_ENTRY_APMBIOS_BASE] = { { { 0x0000ffff, 0x00409a00 } } },
  99. /* 16-bit code */
  100. [GDT_ENTRY_APMBIOS_BASE+1] = { { { 0x0000ffff, 0x00009a00 } } },
  101. /* data */
  102. [GDT_ENTRY_APMBIOS_BASE+2] = { { { 0x0000ffff, 0x00409200 } } },
  103. [GDT_ENTRY_ESPFIX_SS] = { { { 0x00000000, 0x00c09200 } } },
  104. [GDT_ENTRY_PERCPU] = { { { 0x0000ffff, 0x00cf9200 } } },
  105. #endif
  106. } };
  107. EXPORT_PER_CPU_SYMBOL_GPL(gdt_page);
  108. #ifdef CONFIG_X86_32
  109. static int cachesize_override __cpuinitdata = -1;
  110. static int disable_x86_serial_nr __cpuinitdata = 1;
  111. static int __init cachesize_setup(char *str)
  112. {
  113. get_option(&str, &cachesize_override);
  114. return 1;
  115. }
  116. __setup("cachesize=", cachesize_setup);
  117. static int __init x86_fxsr_setup(char *s)
  118. {
  119. setup_clear_cpu_cap(X86_FEATURE_FXSR);
  120. setup_clear_cpu_cap(X86_FEATURE_XMM);
  121. return 1;
  122. }
  123. __setup("nofxsr", x86_fxsr_setup);
  124. static int __init x86_sep_setup(char *s)
  125. {
  126. setup_clear_cpu_cap(X86_FEATURE_SEP);
  127. return 1;
  128. }
  129. __setup("nosep", x86_sep_setup);
  130. /* Standard macro to see if a specific flag is changeable */
  131. static inline int flag_is_changeable_p(u32 flag)
  132. {
  133. u32 f1, f2;
  134. /*
  135. * Cyrix and IDT cpus allow disabling of CPUID
  136. * so the code below may return different results
  137. * when it is executed before and after enabling
  138. * the CPUID. Add "volatile" to not allow gcc to
  139. * optimize the subsequent calls to this function.
  140. */
  141. asm volatile ("pushfl\n\t"
  142. "pushfl\n\t"
  143. "popl %0\n\t"
  144. "movl %0,%1\n\t"
  145. "xorl %2,%0\n\t"
  146. "pushl %0\n\t"
  147. "popfl\n\t"
  148. "pushfl\n\t"
  149. "popl %0\n\t"
  150. "popfl\n\t"
  151. : "=&r" (f1), "=&r" (f2)
  152. : "ir" (flag));
  153. return ((f1^f2) & flag) != 0;
  154. }
  155. /* Probe for the CPUID instruction */
  156. static int __cpuinit have_cpuid_p(void)
  157. {
  158. return flag_is_changeable_p(X86_EFLAGS_ID);
  159. }
  160. static void __cpuinit squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  161. {
  162. if (cpu_has(c, X86_FEATURE_PN) && disable_x86_serial_nr) {
  163. /* Disable processor serial number */
  164. unsigned long lo, hi;
  165. rdmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  166. lo |= 0x200000;
  167. wrmsr(MSR_IA32_BBL_CR_CTL, lo, hi);
  168. printk(KERN_NOTICE "CPU serial number disabled.\n");
  169. clear_cpu_cap(c, X86_FEATURE_PN);
  170. /* Disabling the serial number may affect the cpuid level */
  171. c->cpuid_level = cpuid_eax(0);
  172. }
  173. }
  174. static int __init x86_serial_nr_setup(char *s)
  175. {
  176. disable_x86_serial_nr = 0;
  177. return 1;
  178. }
  179. __setup("serialnumber", x86_serial_nr_setup);
  180. #else
  181. static inline int flag_is_changeable_p(u32 flag)
  182. {
  183. return 1;
  184. }
  185. /* Probe for the CPUID instruction */
  186. static inline int have_cpuid_p(void)
  187. {
  188. return 1;
  189. }
  190. static inline void squash_the_stupid_serial_number(struct cpuinfo_x86 *c)
  191. {
  192. }
  193. #endif
  194. /*
  195. * Naming convention should be: <Name> [(<Codename>)]
  196. * This table only is used unless init_<vendor>() below doesn't set it;
  197. * in particular, if CPUID levels 0x80000002..4 are supported, this isn't used
  198. *
  199. */
  200. /* Look up CPU names by table lookup. */
  201. static char __cpuinit *table_lookup_model(struct cpuinfo_x86 *c)
  202. {
  203. struct cpu_model_info *info;
  204. if (c->x86_model >= 16)
  205. return NULL; /* Range check */
  206. if (!this_cpu)
  207. return NULL;
  208. info = this_cpu->c_models;
  209. while (info && info->family) {
  210. if (info->family == c->x86)
  211. return info->model_names[c->x86_model];
  212. info++;
  213. }
  214. return NULL; /* Not found */
  215. }
  216. __u32 cleared_cpu_caps[NCAPINTS] __cpuinitdata;
  217. /* Current gdt points %fs at the "master" per-cpu area: after this,
  218. * it's on the real one. */
  219. void switch_to_new_gdt(void)
  220. {
  221. struct desc_ptr gdt_descr;
  222. gdt_descr.address = (long)get_cpu_gdt_table(smp_processor_id());
  223. gdt_descr.size = GDT_SIZE - 1;
  224. load_gdt(&gdt_descr);
  225. #ifdef CONFIG_X86_32
  226. asm("mov %0, %%fs" : : "r" (__KERNEL_PERCPU) : "memory");
  227. #endif
  228. }
  229. static struct cpu_dev *cpu_devs[X86_VENDOR_NUM] = {};
  230. static void __cpuinit default_init(struct cpuinfo_x86 *c)
  231. {
  232. #ifdef CONFIG_X86_64
  233. display_cacheinfo(c);
  234. #else
  235. /* Not much we can do here... */
  236. /* Check if at least it has cpuid */
  237. if (c->cpuid_level == -1) {
  238. /* No cpuid. It must be an ancient CPU */
  239. if (c->x86 == 4)
  240. strcpy(c->x86_model_id, "486");
  241. else if (c->x86 == 3)
  242. strcpy(c->x86_model_id, "386");
  243. }
  244. #endif
  245. }
  246. static struct cpu_dev __cpuinitdata default_cpu = {
  247. .c_init = default_init,
  248. .c_vendor = "Unknown",
  249. .c_x86_vendor = X86_VENDOR_UNKNOWN,
  250. };
  251. static void __cpuinit get_model_name(struct cpuinfo_x86 *c)
  252. {
  253. unsigned int *v;
  254. char *p, *q;
  255. if (c->extended_cpuid_level < 0x80000004)
  256. return;
  257. v = (unsigned int *) c->x86_model_id;
  258. cpuid(0x80000002, &v[0], &v[1], &v[2], &v[3]);
  259. cpuid(0x80000003, &v[4], &v[5], &v[6], &v[7]);
  260. cpuid(0x80000004, &v[8], &v[9], &v[10], &v[11]);
  261. c->x86_model_id[48] = 0;
  262. /* Intel chips right-justify this string for some dumb reason;
  263. undo that brain damage */
  264. p = q = &c->x86_model_id[0];
  265. while (*p == ' ')
  266. p++;
  267. if (p != q) {
  268. while (*p)
  269. *q++ = *p++;
  270. while (q <= &c->x86_model_id[48])
  271. *q++ = '\0'; /* Zero-pad the rest */
  272. }
  273. }
  274. void __cpuinit display_cacheinfo(struct cpuinfo_x86 *c)
  275. {
  276. unsigned int n, dummy, ebx, ecx, edx, l2size;
  277. n = c->extended_cpuid_level;
  278. if (n >= 0x80000005) {
  279. cpuid(0x80000005, &dummy, &ebx, &ecx, &edx);
  280. printk(KERN_INFO "CPU: L1 I Cache: %dK (%d bytes/line), D cache %dK (%d bytes/line)\n",
  281. edx>>24, edx&0xFF, ecx>>24, ecx&0xFF);
  282. c->x86_cache_size = (ecx>>24) + (edx>>24);
  283. #ifdef CONFIG_X86_64
  284. /* On K8 L1 TLB is inclusive, so don't count it */
  285. c->x86_tlbsize = 0;
  286. #endif
  287. }
  288. if (n < 0x80000006) /* Some chips just has a large L1. */
  289. return;
  290. cpuid(0x80000006, &dummy, &ebx, &ecx, &edx);
  291. l2size = ecx >> 16;
  292. #ifdef CONFIG_X86_64
  293. c->x86_tlbsize += ((ebx >> 16) & 0xfff) + (ebx & 0xfff);
  294. #else
  295. /* do processor-specific cache resizing */
  296. if (this_cpu->c_size_cache)
  297. l2size = this_cpu->c_size_cache(c, l2size);
  298. /* Allow user to override all this if necessary. */
  299. if (cachesize_override != -1)
  300. l2size = cachesize_override;
  301. if (l2size == 0)
  302. return; /* Again, no L2 cache is possible */
  303. #endif
  304. c->x86_cache_size = l2size;
  305. printk(KERN_INFO "CPU: L2 Cache: %dK (%d bytes/line)\n",
  306. l2size, ecx & 0xFF);
  307. }
  308. void __cpuinit detect_ht(struct cpuinfo_x86 *c)
  309. {
  310. #ifdef CONFIG_X86_HT
  311. u32 eax, ebx, ecx, edx;
  312. int index_msb, core_bits;
  313. if (!cpu_has(c, X86_FEATURE_HT))
  314. return;
  315. if (cpu_has(c, X86_FEATURE_CMP_LEGACY))
  316. goto out;
  317. if (cpu_has(c, X86_FEATURE_XTOPOLOGY))
  318. return;
  319. cpuid(1, &eax, &ebx, &ecx, &edx);
  320. smp_num_siblings = (ebx & 0xff0000) >> 16;
  321. if (smp_num_siblings == 1) {
  322. printk(KERN_INFO "CPU: Hyper-Threading is disabled\n");
  323. } else if (smp_num_siblings > 1) {
  324. if (smp_num_siblings > nr_cpu_ids) {
  325. printk(KERN_WARNING "CPU: Unsupported number of siblings %d",
  326. smp_num_siblings);
  327. smp_num_siblings = 1;
  328. return;
  329. }
  330. index_msb = get_count_order(smp_num_siblings);
  331. #ifdef CONFIG_X86_64
  332. c->phys_proc_id = phys_pkg_id(index_msb);
  333. #else
  334. c->phys_proc_id = phys_pkg_id(c->initial_apicid, index_msb);
  335. #endif
  336. smp_num_siblings = smp_num_siblings / c->x86_max_cores;
  337. index_msb = get_count_order(smp_num_siblings);
  338. core_bits = get_count_order(c->x86_max_cores);
  339. #ifdef CONFIG_X86_64
  340. c->cpu_core_id = phys_pkg_id(index_msb) &
  341. ((1 << core_bits) - 1);
  342. #else
  343. c->cpu_core_id = phys_pkg_id(c->initial_apicid, index_msb) &
  344. ((1 << core_bits) - 1);
  345. #endif
  346. }
  347. out:
  348. if ((c->x86_max_cores * smp_num_siblings) > 1) {
  349. printk(KERN_INFO "CPU: Physical Processor ID: %d\n",
  350. c->phys_proc_id);
  351. printk(KERN_INFO "CPU: Processor Core ID: %d\n",
  352. c->cpu_core_id);
  353. }
  354. #endif
  355. }
  356. static void __cpuinit get_cpu_vendor(struct cpuinfo_x86 *c)
  357. {
  358. char *v = c->x86_vendor_id;
  359. int i;
  360. static int printed;
  361. for (i = 0; i < X86_VENDOR_NUM; i++) {
  362. if (!cpu_devs[i])
  363. break;
  364. if (!strcmp(v, cpu_devs[i]->c_ident[0]) ||
  365. (cpu_devs[i]->c_ident[1] &&
  366. !strcmp(v, cpu_devs[i]->c_ident[1]))) {
  367. this_cpu = cpu_devs[i];
  368. c->x86_vendor = this_cpu->c_x86_vendor;
  369. return;
  370. }
  371. }
  372. if (!printed) {
  373. printed++;
  374. printk(KERN_ERR "CPU: vendor_id '%s' unknown, using generic init.\n", v);
  375. printk(KERN_ERR "CPU: Your system may be unstable.\n");
  376. }
  377. c->x86_vendor = X86_VENDOR_UNKNOWN;
  378. this_cpu = &default_cpu;
  379. }
  380. void __cpuinit cpu_detect(struct cpuinfo_x86 *c)
  381. {
  382. /* Get vendor name */
  383. cpuid(0x00000000, (unsigned int *)&c->cpuid_level,
  384. (unsigned int *)&c->x86_vendor_id[0],
  385. (unsigned int *)&c->x86_vendor_id[8],
  386. (unsigned int *)&c->x86_vendor_id[4]);
  387. c->x86 = 4;
  388. /* Intel-defined flags: level 0x00000001 */
  389. if (c->cpuid_level >= 0x00000001) {
  390. u32 junk, tfms, cap0, misc;
  391. cpuid(0x00000001, &tfms, &misc, &junk, &cap0);
  392. c->x86 = (tfms >> 8) & 0xf;
  393. c->x86_model = (tfms >> 4) & 0xf;
  394. c->x86_mask = tfms & 0xf;
  395. if (c->x86 == 0xf)
  396. c->x86 += (tfms >> 20) & 0xff;
  397. if (c->x86 >= 0x6)
  398. c->x86_model += ((tfms >> 16) & 0xf) << 4;
  399. if (cap0 & (1<<19)) {
  400. c->x86_clflush_size = ((misc >> 8) & 0xff) * 8;
  401. c->x86_cache_alignment = c->x86_clflush_size;
  402. }
  403. }
  404. }
  405. static void __cpuinit get_cpu_cap(struct cpuinfo_x86 *c)
  406. {
  407. u32 tfms, xlvl;
  408. u32 ebx;
  409. /* Intel-defined flags: level 0x00000001 */
  410. if (c->cpuid_level >= 0x00000001) {
  411. u32 capability, excap;
  412. cpuid(0x00000001, &tfms, &ebx, &excap, &capability);
  413. c->x86_capability[0] = capability;
  414. c->x86_capability[4] = excap;
  415. }
  416. /* AMD-defined flags: level 0x80000001 */
  417. xlvl = cpuid_eax(0x80000000);
  418. c->extended_cpuid_level = xlvl;
  419. if ((xlvl & 0xffff0000) == 0x80000000) {
  420. if (xlvl >= 0x80000001) {
  421. c->x86_capability[1] = cpuid_edx(0x80000001);
  422. c->x86_capability[6] = cpuid_ecx(0x80000001);
  423. }
  424. }
  425. #ifdef CONFIG_X86_64
  426. if (c->extended_cpuid_level >= 0x80000008) {
  427. u32 eax = cpuid_eax(0x80000008);
  428. c->x86_virt_bits = (eax >> 8) & 0xff;
  429. c->x86_phys_bits = eax & 0xff;
  430. }
  431. #endif
  432. if (c->extended_cpuid_level >= 0x80000007)
  433. c->x86_power = cpuid_edx(0x80000007);
  434. }
  435. static void __cpuinit identify_cpu_without_cpuid(struct cpuinfo_x86 *c)
  436. {
  437. #ifdef CONFIG_X86_32
  438. int i;
  439. /*
  440. * First of all, decide if this is a 486 or higher
  441. * It's a 486 if we can modify the AC flag
  442. */
  443. if (flag_is_changeable_p(X86_EFLAGS_AC))
  444. c->x86 = 4;
  445. else
  446. c->x86 = 3;
  447. for (i = 0; i < X86_VENDOR_NUM; i++)
  448. if (cpu_devs[i] && cpu_devs[i]->c_identify) {
  449. c->x86_vendor_id[0] = 0;
  450. cpu_devs[i]->c_identify(c);
  451. if (c->x86_vendor_id[0]) {
  452. get_cpu_vendor(c);
  453. break;
  454. }
  455. }
  456. #endif
  457. }
  458. /*
  459. * Do minimum CPU detection early.
  460. * Fields really needed: vendor, cpuid_level, family, model, mask,
  461. * cache alignment.
  462. * The others are not touched to avoid unwanted side effects.
  463. *
  464. * WARNING: this function is only called on the BP. Don't add code here
  465. * that is supposed to run on all CPUs.
  466. */
  467. static void __init early_identify_cpu(struct cpuinfo_x86 *c)
  468. {
  469. #ifdef CONFIG_X86_64
  470. c->x86_clflush_size = 64;
  471. #else
  472. c->x86_clflush_size = 32;
  473. #endif
  474. c->x86_cache_alignment = c->x86_clflush_size;
  475. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  476. c->extended_cpuid_level = 0;
  477. if (!have_cpuid_p())
  478. identify_cpu_without_cpuid(c);
  479. /* cyrix could have cpuid enabled via c_identify()*/
  480. if (!have_cpuid_p())
  481. return;
  482. cpu_detect(c);
  483. get_cpu_vendor(c);
  484. get_cpu_cap(c);
  485. if (this_cpu->c_early_init)
  486. this_cpu->c_early_init(c);
  487. validate_pat_support(c);
  488. #ifdef CONFIG_SMP
  489. c->cpu_index = boot_cpu_id;
  490. #endif
  491. }
  492. void __init early_cpu_init(void)
  493. {
  494. struct cpu_dev **cdev;
  495. int count = 0;
  496. printk("KERNEL supported cpus:\n");
  497. for (cdev = __x86_cpu_dev_start; cdev < __x86_cpu_dev_end; cdev++) {
  498. struct cpu_dev *cpudev = *cdev;
  499. unsigned int j;
  500. if (count >= X86_VENDOR_NUM)
  501. break;
  502. cpu_devs[count] = cpudev;
  503. count++;
  504. for (j = 0; j < 2; j++) {
  505. if (!cpudev->c_ident[j])
  506. continue;
  507. printk(" %s %s\n", cpudev->c_vendor,
  508. cpudev->c_ident[j]);
  509. }
  510. }
  511. early_identify_cpu(&boot_cpu_data);
  512. }
  513. /*
  514. * The NOPL instruction is supposed to exist on all CPUs with
  515. * family >= 6; unfortunately, that's not true in practice because
  516. * of early VIA chips and (more importantly) broken virtualizers that
  517. * are not easy to detect. In the latter case it doesn't even *fail*
  518. * reliably, so probing for it doesn't even work. Disable it completely
  519. * unless we can find a reliable way to detect all the broken cases.
  520. */
  521. static void __cpuinit detect_nopl(struct cpuinfo_x86 *c)
  522. {
  523. clear_cpu_cap(c, X86_FEATURE_NOPL);
  524. }
  525. static void __cpuinit generic_identify(struct cpuinfo_x86 *c)
  526. {
  527. c->extended_cpuid_level = 0;
  528. if (!have_cpuid_p())
  529. identify_cpu_without_cpuid(c);
  530. /* cyrix could have cpuid enabled via c_identify()*/
  531. if (!have_cpuid_p())
  532. return;
  533. cpu_detect(c);
  534. get_cpu_vendor(c);
  535. get_cpu_cap(c);
  536. if (c->cpuid_level >= 0x00000001) {
  537. c->initial_apicid = (cpuid_ebx(1) >> 24) & 0xFF;
  538. #ifdef CONFIG_X86_32
  539. # ifdef CONFIG_X86_HT
  540. c->apicid = phys_pkg_id(c->initial_apicid, 0);
  541. # else
  542. c->apicid = c->initial_apicid;
  543. # endif
  544. #endif
  545. #ifdef CONFIG_X86_HT
  546. c->phys_proc_id = c->initial_apicid;
  547. #endif
  548. }
  549. get_model_name(c); /* Default name */
  550. init_scattered_cpuid_features(c);
  551. detect_nopl(c);
  552. }
  553. /*
  554. * This does the hard work of actually picking apart the CPU stuff...
  555. */
  556. static void __cpuinit identify_cpu(struct cpuinfo_x86 *c)
  557. {
  558. int i;
  559. c->loops_per_jiffy = loops_per_jiffy;
  560. c->x86_cache_size = -1;
  561. c->x86_vendor = X86_VENDOR_UNKNOWN;
  562. c->x86_model = c->x86_mask = 0; /* So far unknown... */
  563. c->x86_vendor_id[0] = '\0'; /* Unset */
  564. c->x86_model_id[0] = '\0'; /* Unset */
  565. c->x86_max_cores = 1;
  566. c->x86_coreid_bits = 0;
  567. #ifdef CONFIG_X86_64
  568. c->x86_clflush_size = 64;
  569. #else
  570. c->cpuid_level = -1; /* CPUID not detected */
  571. c->x86_clflush_size = 32;
  572. #endif
  573. c->x86_cache_alignment = c->x86_clflush_size;
  574. memset(&c->x86_capability, 0, sizeof c->x86_capability);
  575. generic_identify(c);
  576. if (this_cpu->c_identify)
  577. this_cpu->c_identify(c);
  578. #ifdef CONFIG_X86_64
  579. c->apicid = phys_pkg_id(0);
  580. #endif
  581. /*
  582. * Vendor-specific initialization. In this section we
  583. * canonicalize the feature flags, meaning if there are
  584. * features a certain CPU supports which CPUID doesn't
  585. * tell us, CPUID claiming incorrect flags, or other bugs,
  586. * we handle them here.
  587. *
  588. * At the end of this section, c->x86_capability better
  589. * indicate the features this CPU genuinely supports!
  590. */
  591. if (this_cpu->c_init)
  592. this_cpu->c_init(c);
  593. /* Disable the PN if appropriate */
  594. squash_the_stupid_serial_number(c);
  595. /*
  596. * The vendor-specific functions might have changed features. Now
  597. * we do "generic changes."
  598. */
  599. /* If the model name is still unset, do table lookup. */
  600. if (!c->x86_model_id[0]) {
  601. char *p;
  602. p = table_lookup_model(c);
  603. if (p)
  604. strcpy(c->x86_model_id, p);
  605. else
  606. /* Last resort... */
  607. sprintf(c->x86_model_id, "%02x/%02x",
  608. c->x86, c->x86_model);
  609. }
  610. #ifdef CONFIG_X86_64
  611. detect_ht(c);
  612. #endif
  613. init_hypervisor(c);
  614. /*
  615. * On SMP, boot_cpu_data holds the common feature set between
  616. * all CPUs; so make sure that we indicate which features are
  617. * common between the CPUs. The first time this routine gets
  618. * executed, c == &boot_cpu_data.
  619. */
  620. if (c != &boot_cpu_data) {
  621. /* AND the already accumulated flags with these */
  622. for (i = 0; i < NCAPINTS; i++)
  623. boot_cpu_data.x86_capability[i] &= c->x86_capability[i];
  624. }
  625. /* Clear all flags overriden by options */
  626. for (i = 0; i < NCAPINTS; i++)
  627. c->x86_capability[i] &= ~cleared_cpu_caps[i];
  628. #ifdef CONFIG_X86_MCE
  629. /* Init Machine Check Exception if available. */
  630. mcheck_init(c);
  631. #endif
  632. select_idle_routine(c);
  633. #if defined(CONFIG_NUMA) && defined(CONFIG_X86_64)
  634. numa_add_cpu(smp_processor_id());
  635. #endif
  636. }
  637. #ifdef CONFIG_X86_64
  638. static void vgetcpu_set_mode(void)
  639. {
  640. if (cpu_has(&boot_cpu_data, X86_FEATURE_RDTSCP))
  641. vgetcpu_mode = VGETCPU_RDTSCP;
  642. else
  643. vgetcpu_mode = VGETCPU_LSL;
  644. }
  645. #endif
  646. void __init identify_boot_cpu(void)
  647. {
  648. identify_cpu(&boot_cpu_data);
  649. #ifdef CONFIG_X86_32
  650. sysenter_setup();
  651. enable_sep_cpu();
  652. #else
  653. vgetcpu_set_mode();
  654. #endif
  655. init_hw_perf_counters();
  656. }
  657. void __cpuinit identify_secondary_cpu(struct cpuinfo_x86 *c)
  658. {
  659. BUG_ON(c == &boot_cpu_data);
  660. identify_cpu(c);
  661. #ifdef CONFIG_X86_32
  662. enable_sep_cpu();
  663. #endif
  664. mtrr_ap_init();
  665. }
  666. struct msr_range {
  667. unsigned min;
  668. unsigned max;
  669. };
  670. static struct msr_range msr_range_array[] __cpuinitdata = {
  671. { 0x00000000, 0x00000418},
  672. { 0xc0000000, 0xc000040b},
  673. { 0xc0010000, 0xc0010142},
  674. { 0xc0011000, 0xc001103b},
  675. };
  676. static void __cpuinit print_cpu_msr(void)
  677. {
  678. unsigned index;
  679. u64 val;
  680. int i;
  681. unsigned index_min, index_max;
  682. for (i = 0; i < ARRAY_SIZE(msr_range_array); i++) {
  683. index_min = msr_range_array[i].min;
  684. index_max = msr_range_array[i].max;
  685. for (index = index_min; index < index_max; index++) {
  686. if (rdmsrl_amd_safe(index, &val))
  687. continue;
  688. printk(KERN_INFO " MSR%08x: %016llx\n", index, val);
  689. }
  690. }
  691. }
  692. static int show_msr __cpuinitdata;
  693. static __init int setup_show_msr(char *arg)
  694. {
  695. int num;
  696. get_option(&arg, &num);
  697. if (num > 0)
  698. show_msr = num;
  699. return 1;
  700. }
  701. __setup("show_msr=", setup_show_msr);
  702. static __init int setup_noclflush(char *arg)
  703. {
  704. setup_clear_cpu_cap(X86_FEATURE_CLFLSH);
  705. return 1;
  706. }
  707. __setup("noclflush", setup_noclflush);
  708. void __cpuinit print_cpu_info(struct cpuinfo_x86 *c)
  709. {
  710. char *vendor = NULL;
  711. if (c->x86_vendor < X86_VENDOR_NUM)
  712. vendor = this_cpu->c_vendor;
  713. else if (c->cpuid_level >= 0)
  714. vendor = c->x86_vendor_id;
  715. if (vendor && !strstr(c->x86_model_id, vendor))
  716. printk(KERN_CONT "%s ", vendor);
  717. if (c->x86_model_id[0])
  718. printk(KERN_CONT "%s", c->x86_model_id);
  719. else
  720. printk(KERN_CONT "%d86", c->x86);
  721. if (c->x86_mask || c->cpuid_level >= 0)
  722. printk(KERN_CONT " stepping %02x\n", c->x86_mask);
  723. else
  724. printk(KERN_CONT "\n");
  725. #ifdef CONFIG_SMP
  726. if (c->cpu_index < show_msr)
  727. print_cpu_msr();
  728. #else
  729. if (show_msr)
  730. print_cpu_msr();
  731. #endif
  732. }
  733. static __init int setup_disablecpuid(char *arg)
  734. {
  735. int bit;
  736. if (get_option(&arg, &bit) && bit < NCAPINTS*32)
  737. setup_clear_cpu_cap(bit);
  738. else
  739. return 0;
  740. return 1;
  741. }
  742. __setup("clearcpuid=", setup_disablecpuid);
  743. #ifdef CONFIG_X86_64
  744. struct desc_ptr idt_descr = { 256 * 16 - 1, (unsigned long) idt_table };
  745. DEFINE_PER_CPU_FIRST(union irq_stack_union,
  746. irq_stack_union) __aligned(PAGE_SIZE);
  747. #ifdef CONFIG_SMP
  748. DEFINE_PER_CPU(char *, irq_stack_ptr); /* will be set during per cpu init */
  749. #else
  750. DEFINE_PER_CPU(char *, irq_stack_ptr) =
  751. per_cpu_var(irq_stack_union.irq_stack) + IRQ_STACK_SIZE - 64;
  752. #endif
  753. DEFINE_PER_CPU(unsigned long, kernel_stack) =
  754. (unsigned long)&init_thread_union - KERNEL_STACK_OFFSET + THREAD_SIZE;
  755. EXPORT_PER_CPU_SYMBOL(kernel_stack);
  756. DEFINE_PER_CPU(unsigned int, irq_count) = -1;
  757. static DEFINE_PER_CPU_PAGE_ALIGNED(char, exception_stacks
  758. [(N_EXCEPTION_STACKS - 1) * EXCEPTION_STKSZ + DEBUG_STKSZ])
  759. __aligned(PAGE_SIZE);
  760. extern asmlinkage void ignore_sysret(void);
  761. /* May not be marked __init: used by software suspend */
  762. void syscall_init(void)
  763. {
  764. /*
  765. * LSTAR and STAR live in a bit strange symbiosis.
  766. * They both write to the same internal register. STAR allows to
  767. * set CS/DS but only a 32bit target. LSTAR sets the 64bit rip.
  768. */
  769. wrmsrl(MSR_STAR, ((u64)__USER32_CS)<<48 | ((u64)__KERNEL_CS)<<32);
  770. wrmsrl(MSR_LSTAR, system_call);
  771. wrmsrl(MSR_CSTAR, ignore_sysret);
  772. #ifdef CONFIG_IA32_EMULATION
  773. syscall32_cpu_init();
  774. #endif
  775. /* Flags to clear on syscall */
  776. wrmsrl(MSR_SYSCALL_MASK,
  777. X86_EFLAGS_TF|X86_EFLAGS_DF|X86_EFLAGS_IF|X86_EFLAGS_IOPL);
  778. }
  779. unsigned long kernel_eflags;
  780. /*
  781. * Copies of the original ist values from the tss are only accessed during
  782. * debugging, no special alignment required.
  783. */
  784. DEFINE_PER_CPU(struct orig_ist, orig_ist);
  785. #else
  786. /* Make sure %fs is initialized properly in idle threads */
  787. struct pt_regs * __cpuinit idle_regs(struct pt_regs *regs)
  788. {
  789. memset(regs, 0, sizeof(struct pt_regs));
  790. regs->fs = __KERNEL_PERCPU;
  791. return regs;
  792. }
  793. #endif
  794. /*
  795. * cpu_init() initializes state that is per-CPU. Some data is already
  796. * initialized (naturally) in the bootstrap process, such as the GDT
  797. * and IDT. We reload them nevertheless, this function acts as a
  798. * 'CPU state barrier', nothing should get across.
  799. * A lot of state is already set up in PDA init for 64 bit
  800. */
  801. #ifdef CONFIG_X86_64
  802. void __cpuinit cpu_init(void)
  803. {
  804. int cpu = stack_smp_processor_id();
  805. struct tss_struct *t = &per_cpu(init_tss, cpu);
  806. struct orig_ist *orig_ist = &per_cpu(orig_ist, cpu);
  807. unsigned long v;
  808. struct task_struct *me;
  809. int i;
  810. loadsegment(fs, 0);
  811. loadsegment(gs, 0);
  812. load_gs_base(cpu);
  813. #ifdef CONFIG_NUMA
  814. if (cpu != 0 && percpu_read(node_number) == 0 &&
  815. cpu_to_node(cpu) != NUMA_NO_NODE)
  816. percpu_write(node_number, cpu_to_node(cpu));
  817. #endif
  818. me = current;
  819. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask))
  820. panic("CPU#%d already initialized!\n", cpu);
  821. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  822. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  823. /*
  824. * Initialize the per-CPU GDT with the boot GDT,
  825. * and set up the GDT descriptor:
  826. */
  827. switch_to_new_gdt();
  828. load_idt((const struct desc_ptr *)&idt_descr);
  829. memset(me->thread.tls_array, 0, GDT_ENTRY_TLS_ENTRIES * 8);
  830. syscall_init();
  831. wrmsrl(MSR_FS_BASE, 0);
  832. wrmsrl(MSR_KERNEL_GS_BASE, 0);
  833. barrier();
  834. check_efer();
  835. if (cpu != 0 && x2apic)
  836. enable_x2apic();
  837. /*
  838. * set up and load the per-CPU TSS
  839. */
  840. if (!orig_ist->ist[0]) {
  841. static const unsigned int sizes[N_EXCEPTION_STACKS] = {
  842. [0 ... N_EXCEPTION_STACKS - 1] = EXCEPTION_STKSZ,
  843. [DEBUG_STACK - 1] = DEBUG_STKSZ
  844. };
  845. char *estacks = per_cpu(exception_stacks, cpu);
  846. for (v = 0; v < N_EXCEPTION_STACKS; v++) {
  847. estacks += sizes[v];
  848. orig_ist->ist[v] = t->x86_tss.ist[v] =
  849. (unsigned long)estacks;
  850. }
  851. }
  852. t->x86_tss.io_bitmap_base = offsetof(struct tss_struct, io_bitmap);
  853. /*
  854. * <= is required because the CPU will access up to
  855. * 8 bits beyond the end of the IO permission bitmap.
  856. */
  857. for (i = 0; i <= IO_BITMAP_LONGS; i++)
  858. t->io_bitmap[i] = ~0UL;
  859. atomic_inc(&init_mm.mm_count);
  860. me->active_mm = &init_mm;
  861. if (me->mm)
  862. BUG();
  863. enter_lazy_tlb(&init_mm, me);
  864. load_sp0(t, &current->thread);
  865. set_tss_desc(cpu, t);
  866. load_TR_desc();
  867. load_LDT(&init_mm.context);
  868. #ifdef CONFIG_KGDB
  869. /*
  870. * If the kgdb is connected no debug regs should be altered. This
  871. * is only applicable when KGDB and a KGDB I/O module are built
  872. * into the kernel and you are using early debugging with
  873. * kgdbwait. KGDB will control the kernel HW breakpoint registers.
  874. */
  875. if (kgdb_connected && arch_kgdb_ops.correct_hw_break)
  876. arch_kgdb_ops.correct_hw_break();
  877. else {
  878. #endif
  879. /*
  880. * Clear all 6 debug registers:
  881. */
  882. set_debugreg(0UL, 0);
  883. set_debugreg(0UL, 1);
  884. set_debugreg(0UL, 2);
  885. set_debugreg(0UL, 3);
  886. set_debugreg(0UL, 6);
  887. set_debugreg(0UL, 7);
  888. #ifdef CONFIG_KGDB
  889. /* If the kgdb is connected no debug regs should be altered. */
  890. }
  891. #endif
  892. fpu_init();
  893. raw_local_save_flags(kernel_eflags);
  894. if (is_uv_system())
  895. uv_cpu_init();
  896. }
  897. #else
  898. void __cpuinit cpu_init(void)
  899. {
  900. int cpu = smp_processor_id();
  901. struct task_struct *curr = current;
  902. struct tss_struct *t = &per_cpu(init_tss, cpu);
  903. struct thread_struct *thread = &curr->thread;
  904. if (cpumask_test_and_set_cpu(cpu, cpu_initialized_mask)) {
  905. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu);
  906. for (;;) local_irq_enable();
  907. }
  908. printk(KERN_INFO "Initializing CPU#%d\n", cpu);
  909. if (cpu_has_vme || cpu_has_tsc || cpu_has_de)
  910. clear_in_cr4(X86_CR4_VME|X86_CR4_PVI|X86_CR4_TSD|X86_CR4_DE);
  911. load_idt(&idt_descr);
  912. switch_to_new_gdt();
  913. /*
  914. * Set up and load the per-CPU TSS and LDT
  915. */
  916. atomic_inc(&init_mm.mm_count);
  917. curr->active_mm = &init_mm;
  918. if (curr->mm)
  919. BUG();
  920. enter_lazy_tlb(&init_mm, curr);
  921. load_sp0(t, thread);
  922. set_tss_desc(cpu, t);
  923. load_TR_desc();
  924. load_LDT(&init_mm.context);
  925. #ifdef CONFIG_DOUBLEFAULT
  926. /* Set up doublefault TSS pointer in the GDT */
  927. __set_tss_desc(cpu, GDT_ENTRY_DOUBLEFAULT_TSS, &doublefault_tss);
  928. #endif
  929. /* Clear %gs. */
  930. asm volatile ("mov %0, %%gs" : : "r" (0));
  931. /* Clear all 6 debug registers: */
  932. set_debugreg(0, 0);
  933. set_debugreg(0, 1);
  934. set_debugreg(0, 2);
  935. set_debugreg(0, 3);
  936. set_debugreg(0, 6);
  937. set_debugreg(0, 7);
  938. /*
  939. * Force FPU initialization:
  940. */
  941. if (cpu_has_xsave)
  942. current_thread_info()->status = TS_XSAVE;
  943. else
  944. current_thread_info()->status = 0;
  945. clear_used_math();
  946. mxcsr_feature_mask_init();
  947. /*
  948. * Boot processor to setup the FP and extended state context info.
  949. */
  950. if (smp_processor_id() == boot_cpu_id)
  951. init_thread_xstate();
  952. xsave_init();
  953. }
  954. #endif