common.c 28 KB

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