common.c 27 KB

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