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