common.c 27 KB

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