intel.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/string.h>
  4. #include <linux/bitops.h>
  5. #include <linux/smp.h>
  6. #include <linux/sched.h>
  7. #include <linux/sched/clock.h>
  8. #include <linux/thread_info.h>
  9. #include <linux/init.h>
  10. #include <linux/uaccess.h>
  11. #include <asm/cpufeature.h>
  12. #include <asm/pgtable.h>
  13. #include <asm/msr.h>
  14. #include <asm/bugs.h>
  15. #include <asm/cpu.h>
  16. #include <asm/intel-family.h>
  17. #include <asm/microcode_intel.h>
  18. #include <asm/hwcap2.h>
  19. #include <asm/elf.h>
  20. #ifdef CONFIG_X86_64
  21. #include <linux/topology.h>
  22. #endif
  23. #include "cpu.h"
  24. #ifdef CONFIG_X86_LOCAL_APIC
  25. #include <asm/mpspec.h>
  26. #include <asm/apic.h>
  27. #endif
  28. /*
  29. * Just in case our CPU detection goes bad, or you have a weird system,
  30. * allow a way to override the automatic disabling of MPX.
  31. */
  32. static int forcempx;
  33. static int __init forcempx_setup(char *__unused)
  34. {
  35. forcempx = 1;
  36. return 1;
  37. }
  38. __setup("intel-skd-046-workaround=disable", forcempx_setup);
  39. void check_mpx_erratum(struct cpuinfo_x86 *c)
  40. {
  41. if (forcempx)
  42. return;
  43. /*
  44. * Turn off the MPX feature on CPUs where SMEP is not
  45. * available or disabled.
  46. *
  47. * Works around Intel Erratum SKD046: "Branch Instructions
  48. * May Initialize MPX Bound Registers Incorrectly".
  49. *
  50. * This might falsely disable MPX on systems without
  51. * SMEP, like Atom processors without SMEP. But there
  52. * is no such hardware known at the moment.
  53. */
  54. if (cpu_has(c, X86_FEATURE_MPX) && !cpu_has(c, X86_FEATURE_SMEP)) {
  55. setup_clear_cpu_cap(X86_FEATURE_MPX);
  56. pr_warn("x86/mpx: Disabling MPX since SMEP not present\n");
  57. }
  58. }
  59. static bool ring3mwait_disabled __read_mostly;
  60. static int __init ring3mwait_disable(char *__unused)
  61. {
  62. ring3mwait_disabled = true;
  63. return 0;
  64. }
  65. __setup("ring3mwait=disable", ring3mwait_disable);
  66. static void probe_xeon_phi_r3mwait(struct cpuinfo_x86 *c)
  67. {
  68. /*
  69. * Ring 3 MONITOR/MWAIT feature cannot be detected without
  70. * cpu model and family comparison.
  71. */
  72. if (c->x86 != 6)
  73. return;
  74. switch (c->x86_model) {
  75. case INTEL_FAM6_XEON_PHI_KNL:
  76. case INTEL_FAM6_XEON_PHI_KNM:
  77. break;
  78. default:
  79. return;
  80. }
  81. if (ring3mwait_disabled)
  82. return;
  83. set_cpu_cap(c, X86_FEATURE_RING3MWAIT);
  84. this_cpu_or(msr_misc_features_shadow,
  85. 1UL << MSR_MISC_FEATURES_ENABLES_RING3MWAIT_BIT);
  86. if (c == &boot_cpu_data)
  87. ELF_HWCAP2 |= HWCAP2_RING3MWAIT;
  88. }
  89. /*
  90. * Early microcode releases for the Spectre v2 mitigation were broken.
  91. * Information taken from;
  92. * - https://newsroom.intel.com/wp-content/uploads/sites/11/2018/01/microcode-update-guidance.pdf
  93. * - https://kb.vmware.com/s/article/52345
  94. * - Microcode revisions observed in the wild
  95. * - Release note from 20180108 microcode release
  96. */
  97. struct sku_microcode {
  98. u8 model;
  99. u8 stepping;
  100. u32 microcode;
  101. };
  102. static const struct sku_microcode spectre_bad_microcodes[] = {
  103. { INTEL_FAM6_KABYLAKE_DESKTOP, 0x0B, 0x80 },
  104. { INTEL_FAM6_KABYLAKE_DESKTOP, 0x0A, 0x80 },
  105. { INTEL_FAM6_KABYLAKE_DESKTOP, 0x09, 0x80 },
  106. { INTEL_FAM6_KABYLAKE_MOBILE, 0x0A, 0x80 },
  107. { INTEL_FAM6_KABYLAKE_MOBILE, 0x09, 0x80 },
  108. { INTEL_FAM6_SKYLAKE_X, 0x03, 0x0100013e },
  109. { INTEL_FAM6_SKYLAKE_X, 0x04, 0x0200003c },
  110. { INTEL_FAM6_SKYLAKE_DESKTOP, 0x03, 0xc2 },
  111. { INTEL_FAM6_BROADWELL_CORE, 0x04, 0x28 },
  112. { INTEL_FAM6_BROADWELL_GT3E, 0x01, 0x1b },
  113. { INTEL_FAM6_BROADWELL_XEON_D, 0x02, 0x14 },
  114. { INTEL_FAM6_BROADWELL_XEON_D, 0x03, 0x07000011 },
  115. { INTEL_FAM6_BROADWELL_X, 0x01, 0x0b000025 },
  116. { INTEL_FAM6_HASWELL_ULT, 0x01, 0x21 },
  117. { INTEL_FAM6_HASWELL_GT3E, 0x01, 0x18 },
  118. { INTEL_FAM6_HASWELL_CORE, 0x03, 0x23 },
  119. { INTEL_FAM6_HASWELL_X, 0x02, 0x3b },
  120. { INTEL_FAM6_HASWELL_X, 0x04, 0x10 },
  121. { INTEL_FAM6_IVYBRIDGE_X, 0x04, 0x42a },
  122. /* Observed in the wild */
  123. { INTEL_FAM6_SANDYBRIDGE_X, 0x06, 0x61b },
  124. { INTEL_FAM6_SANDYBRIDGE_X, 0x07, 0x712 },
  125. };
  126. static bool bad_spectre_microcode(struct cpuinfo_x86 *c)
  127. {
  128. int i;
  129. /*
  130. * We know that the hypervisor lie to us on the microcode version so
  131. * we may as well hope that it is running the correct version.
  132. */
  133. if (cpu_has(c, X86_FEATURE_HYPERVISOR))
  134. return false;
  135. for (i = 0; i < ARRAY_SIZE(spectre_bad_microcodes); i++) {
  136. if (c->x86_model == spectre_bad_microcodes[i].model &&
  137. c->x86_stepping == spectre_bad_microcodes[i].stepping)
  138. return (c->microcode <= spectre_bad_microcodes[i].microcode);
  139. }
  140. return false;
  141. }
  142. static void early_init_intel(struct cpuinfo_x86 *c)
  143. {
  144. u64 misc_enable;
  145. /* Unmask CPUID levels if masked: */
  146. if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) {
  147. if (msr_clear_bit(MSR_IA32_MISC_ENABLE,
  148. MSR_IA32_MISC_ENABLE_LIMIT_CPUID_BIT) > 0) {
  149. c->cpuid_level = cpuid_eax(0);
  150. get_cpu_cap(c);
  151. }
  152. }
  153. if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
  154. (c->x86 == 0x6 && c->x86_model >= 0x0e))
  155. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  156. if (c->x86 >= 6 && !cpu_has(c, X86_FEATURE_IA64))
  157. c->microcode = intel_get_microcode_revision();
  158. /* Now if any of them are set, check the blacklist and clear the lot */
  159. if ((cpu_has(c, X86_FEATURE_SPEC_CTRL) ||
  160. cpu_has(c, X86_FEATURE_INTEL_STIBP) ||
  161. cpu_has(c, X86_FEATURE_IBRS) || cpu_has(c, X86_FEATURE_IBPB) ||
  162. cpu_has(c, X86_FEATURE_STIBP)) && bad_spectre_microcode(c)) {
  163. pr_warn("Intel Spectre v2 broken microcode detected; disabling Speculation Control\n");
  164. setup_clear_cpu_cap(X86_FEATURE_IBRS);
  165. setup_clear_cpu_cap(X86_FEATURE_IBPB);
  166. setup_clear_cpu_cap(X86_FEATURE_STIBP);
  167. setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL);
  168. setup_clear_cpu_cap(X86_FEATURE_INTEL_STIBP);
  169. }
  170. /*
  171. * Atom erratum AAE44/AAF40/AAG38/AAH41:
  172. *
  173. * A race condition between speculative fetches and invalidating
  174. * a large page. This is worked around in microcode, but we
  175. * need the microcode to have already been loaded... so if it is
  176. * not, recommend a BIOS update and disable large pages.
  177. */
  178. if (c->x86 == 6 && c->x86_model == 0x1c && c->x86_stepping <= 2 &&
  179. c->microcode < 0x20e) {
  180. pr_warn("Atom PSE erratum detected, BIOS microcode update recommended\n");
  181. clear_cpu_cap(c, X86_FEATURE_PSE);
  182. }
  183. #ifdef CONFIG_X86_64
  184. set_cpu_cap(c, X86_FEATURE_SYSENTER32);
  185. #else
  186. /* Netburst reports 64 bytes clflush size, but does IO in 128 bytes */
  187. if (c->x86 == 15 && c->x86_cache_alignment == 64)
  188. c->x86_cache_alignment = 128;
  189. #endif
  190. /* CPUID workaround for 0F33/0F34 CPU */
  191. if (c->x86 == 0xF && c->x86_model == 0x3
  192. && (c->x86_stepping == 0x3 || c->x86_stepping == 0x4))
  193. c->x86_phys_bits = 36;
  194. /*
  195. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  196. * with P/T states and does not stop in deep C-states.
  197. *
  198. * It is also reliable across cores and sockets. (but not across
  199. * cabinets - we turn it off in that case explicitly.)
  200. */
  201. if (c->x86_power & (1 << 8)) {
  202. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  203. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  204. }
  205. /* Penwell and Cloverview have the TSC which doesn't sleep on S3 */
  206. if (c->x86 == 6) {
  207. switch (c->x86_model) {
  208. case 0x27: /* Penwell */
  209. case 0x35: /* Cloverview */
  210. case 0x4a: /* Merrifield */
  211. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC_S3);
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. /*
  218. * There is a known erratum on Pentium III and Core Solo
  219. * and Core Duo CPUs.
  220. * " Page with PAT set to WC while associated MTRR is UC
  221. * may consolidate to UC "
  222. * Because of this erratum, it is better to stick with
  223. * setting WC in MTRR rather than using PAT on these CPUs.
  224. *
  225. * Enable PAT WC only on P4, Core 2 or later CPUs.
  226. */
  227. if (c->x86 == 6 && c->x86_model < 15)
  228. clear_cpu_cap(c, X86_FEATURE_PAT);
  229. /*
  230. * If fast string is not enabled in IA32_MISC_ENABLE for any reason,
  231. * clear the fast string and enhanced fast string CPU capabilities.
  232. */
  233. if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) {
  234. rdmsrl(MSR_IA32_MISC_ENABLE, misc_enable);
  235. if (!(misc_enable & MSR_IA32_MISC_ENABLE_FAST_STRING)) {
  236. pr_info("Disabled fast string operations\n");
  237. setup_clear_cpu_cap(X86_FEATURE_REP_GOOD);
  238. setup_clear_cpu_cap(X86_FEATURE_ERMS);
  239. }
  240. }
  241. /*
  242. * Intel Quark Core DevMan_001.pdf section 6.4.11
  243. * "The operating system also is required to invalidate (i.e., flush)
  244. * the TLB when any changes are made to any of the page table entries.
  245. * The operating system must reload CR3 to cause the TLB to be flushed"
  246. *
  247. * As a result, boot_cpu_has(X86_FEATURE_PGE) in arch/x86/include/asm/tlbflush.h
  248. * should be false so that __flush_tlb_all() causes CR3 insted of CR4.PGE
  249. * to be modified.
  250. */
  251. if (c->x86 == 5 && c->x86_model == 9) {
  252. pr_info("Disabling PGE capability bit\n");
  253. setup_clear_cpu_cap(X86_FEATURE_PGE);
  254. }
  255. if (c->cpuid_level >= 0x00000001) {
  256. u32 eax, ebx, ecx, edx;
  257. cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
  258. /*
  259. * If HTT (EDX[28]) is set EBX[16:23] contain the number of
  260. * apicids which are reserved per package. Store the resulting
  261. * shift value for the package management code.
  262. */
  263. if (edx & (1U << 28))
  264. c->x86_coreid_bits = get_count_order((ebx >> 16) & 0xff);
  265. }
  266. check_mpx_erratum(c);
  267. }
  268. #ifdef CONFIG_X86_32
  269. /*
  270. * Early probe support logic for ppro memory erratum #50
  271. *
  272. * This is called before we do cpu ident work
  273. */
  274. int ppro_with_ram_bug(void)
  275. {
  276. /* Uses data from early_cpu_detect now */
  277. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  278. boot_cpu_data.x86 == 6 &&
  279. boot_cpu_data.x86_model == 1 &&
  280. boot_cpu_data.x86_stepping < 8) {
  281. pr_info("Pentium Pro with Errata#50 detected. Taking evasive action.\n");
  282. return 1;
  283. }
  284. return 0;
  285. }
  286. static void intel_smp_check(struct cpuinfo_x86 *c)
  287. {
  288. /* calling is from identify_secondary_cpu() ? */
  289. if (!c->cpu_index)
  290. return;
  291. /*
  292. * Mask B, Pentium, but not Pentium MMX
  293. */
  294. if (c->x86 == 5 &&
  295. c->x86_stepping >= 1 && c->x86_stepping <= 4 &&
  296. c->x86_model <= 3) {
  297. /*
  298. * Remember we have B step Pentia with bugs
  299. */
  300. WARN_ONCE(1, "WARNING: SMP operation may be unreliable"
  301. "with B stepping processors.\n");
  302. }
  303. }
  304. static int forcepae;
  305. static int __init forcepae_setup(char *__unused)
  306. {
  307. forcepae = 1;
  308. return 1;
  309. }
  310. __setup("forcepae", forcepae_setup);
  311. static void intel_workarounds(struct cpuinfo_x86 *c)
  312. {
  313. #ifdef CONFIG_X86_F00F_BUG
  314. /*
  315. * All models of Pentium and Pentium with MMX technology CPUs
  316. * have the F0 0F bug, which lets nonprivileged users lock up the
  317. * system. Announce that the fault handler will be checking for it.
  318. * The Quark is also family 5, but does not have the same bug.
  319. */
  320. clear_cpu_bug(c, X86_BUG_F00F);
  321. if (c->x86 == 5 && c->x86_model < 9) {
  322. static int f00f_workaround_enabled;
  323. set_cpu_bug(c, X86_BUG_F00F);
  324. if (!f00f_workaround_enabled) {
  325. pr_notice("Intel Pentium with F0 0F bug - workaround enabled.\n");
  326. f00f_workaround_enabled = 1;
  327. }
  328. }
  329. #endif
  330. /*
  331. * SEP CPUID bug: Pentium Pro reports SEP but doesn't have it until
  332. * model 3 mask 3
  333. */
  334. if ((c->x86<<8 | c->x86_model<<4 | c->x86_stepping) < 0x633)
  335. clear_cpu_cap(c, X86_FEATURE_SEP);
  336. /*
  337. * PAE CPUID issue: many Pentium M report no PAE but may have a
  338. * functionally usable PAE implementation.
  339. * Forcefully enable PAE if kernel parameter "forcepae" is present.
  340. */
  341. if (forcepae) {
  342. pr_warn("PAE forced!\n");
  343. set_cpu_cap(c, X86_FEATURE_PAE);
  344. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_NOW_UNRELIABLE);
  345. }
  346. /*
  347. * P4 Xeon erratum 037 workaround.
  348. * Hardware prefetcher may cause stale data to be loaded into the cache.
  349. */
  350. if ((c->x86 == 15) && (c->x86_model == 1) && (c->x86_stepping == 1)) {
  351. if (msr_set_bit(MSR_IA32_MISC_ENABLE,
  352. MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE_BIT) > 0) {
  353. pr_info("CPU: C0 stepping P4 Xeon detected.\n");
  354. pr_info("CPU: Disabling hardware prefetching (Erratum 037)\n");
  355. }
  356. }
  357. /*
  358. * See if we have a good local APIC by checking for buggy Pentia,
  359. * i.e. all B steppings and the C2 stepping of P54C when using their
  360. * integrated APIC (see 11AP erratum in "Pentium Processor
  361. * Specification Update").
  362. */
  363. if (boot_cpu_has(X86_FEATURE_APIC) && (c->x86<<8 | c->x86_model<<4) == 0x520 &&
  364. (c->x86_stepping < 0x6 || c->x86_stepping == 0xb))
  365. set_cpu_bug(c, X86_BUG_11AP);
  366. #ifdef CONFIG_X86_INTEL_USERCOPY
  367. /*
  368. * Set up the preferred alignment for movsl bulk memory moves
  369. */
  370. switch (c->x86) {
  371. case 4: /* 486: untested */
  372. break;
  373. case 5: /* Old Pentia: untested */
  374. break;
  375. case 6: /* PII/PIII only like movsl with 8-byte alignment */
  376. movsl_mask.mask = 7;
  377. break;
  378. case 15: /* P4 is OK down to 8-byte alignment */
  379. movsl_mask.mask = 7;
  380. break;
  381. }
  382. #endif
  383. intel_smp_check(c);
  384. }
  385. #else
  386. static void intel_workarounds(struct cpuinfo_x86 *c)
  387. {
  388. }
  389. #endif
  390. static void srat_detect_node(struct cpuinfo_x86 *c)
  391. {
  392. #ifdef CONFIG_NUMA
  393. unsigned node;
  394. int cpu = smp_processor_id();
  395. /* Don't do the funky fallback heuristics the AMD version employs
  396. for now. */
  397. node = numa_cpu_node(cpu);
  398. if (node == NUMA_NO_NODE || !node_online(node)) {
  399. /* reuse the value from init_cpu_to_node() */
  400. node = cpu_to_node(cpu);
  401. }
  402. numa_set_node(cpu, node);
  403. #endif
  404. }
  405. /*
  406. * find out the number of processor cores on the die
  407. */
  408. static int intel_num_cpu_cores(struct cpuinfo_x86 *c)
  409. {
  410. unsigned int eax, ebx, ecx, edx;
  411. if (!IS_ENABLED(CONFIG_SMP) || c->cpuid_level < 4)
  412. return 1;
  413. /* Intel has a non-standard dependency on %ecx for this CPUID level. */
  414. cpuid_count(4, 0, &eax, &ebx, &ecx, &edx);
  415. if (eax & 0x1f)
  416. return (eax >> 26) + 1;
  417. else
  418. return 1;
  419. }
  420. static void detect_vmx_virtcap(struct cpuinfo_x86 *c)
  421. {
  422. /* Intel VMX MSR indicated features */
  423. #define X86_VMX_FEATURE_PROC_CTLS_TPR_SHADOW 0x00200000
  424. #define X86_VMX_FEATURE_PROC_CTLS_VNMI 0x00400000
  425. #define X86_VMX_FEATURE_PROC_CTLS_2ND_CTLS 0x80000000
  426. #define X86_VMX_FEATURE_PROC_CTLS2_VIRT_APIC 0x00000001
  427. #define X86_VMX_FEATURE_PROC_CTLS2_EPT 0x00000002
  428. #define X86_VMX_FEATURE_PROC_CTLS2_VPID 0x00000020
  429. u32 vmx_msr_low, vmx_msr_high, msr_ctl, msr_ctl2;
  430. clear_cpu_cap(c, X86_FEATURE_TPR_SHADOW);
  431. clear_cpu_cap(c, X86_FEATURE_VNMI);
  432. clear_cpu_cap(c, X86_FEATURE_FLEXPRIORITY);
  433. clear_cpu_cap(c, X86_FEATURE_EPT);
  434. clear_cpu_cap(c, X86_FEATURE_VPID);
  435. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS, vmx_msr_low, vmx_msr_high);
  436. msr_ctl = vmx_msr_high | vmx_msr_low;
  437. if (msr_ctl & X86_VMX_FEATURE_PROC_CTLS_TPR_SHADOW)
  438. set_cpu_cap(c, X86_FEATURE_TPR_SHADOW);
  439. if (msr_ctl & X86_VMX_FEATURE_PROC_CTLS_VNMI)
  440. set_cpu_cap(c, X86_FEATURE_VNMI);
  441. if (msr_ctl & X86_VMX_FEATURE_PROC_CTLS_2ND_CTLS) {
  442. rdmsr(MSR_IA32_VMX_PROCBASED_CTLS2,
  443. vmx_msr_low, vmx_msr_high);
  444. msr_ctl2 = vmx_msr_high | vmx_msr_low;
  445. if ((msr_ctl2 & X86_VMX_FEATURE_PROC_CTLS2_VIRT_APIC) &&
  446. (msr_ctl & X86_VMX_FEATURE_PROC_CTLS_TPR_SHADOW))
  447. set_cpu_cap(c, X86_FEATURE_FLEXPRIORITY);
  448. if (msr_ctl2 & X86_VMX_FEATURE_PROC_CTLS2_EPT)
  449. set_cpu_cap(c, X86_FEATURE_EPT);
  450. if (msr_ctl2 & X86_VMX_FEATURE_PROC_CTLS2_VPID)
  451. set_cpu_cap(c, X86_FEATURE_VPID);
  452. }
  453. }
  454. static void init_intel_energy_perf(struct cpuinfo_x86 *c)
  455. {
  456. u64 epb;
  457. /*
  458. * Initialize MSR_IA32_ENERGY_PERF_BIAS if not already initialized.
  459. * (x86_energy_perf_policy(8) is available to change it at run-time.)
  460. */
  461. if (!cpu_has(c, X86_FEATURE_EPB))
  462. return;
  463. rdmsrl(MSR_IA32_ENERGY_PERF_BIAS, epb);
  464. if ((epb & 0xF) != ENERGY_PERF_BIAS_PERFORMANCE)
  465. return;
  466. pr_warn_once("ENERGY_PERF_BIAS: Set to 'normal', was 'performance'\n");
  467. pr_warn_once("ENERGY_PERF_BIAS: View and update with x86_energy_perf_policy(8)\n");
  468. epb = (epb & ~0xF) | ENERGY_PERF_BIAS_NORMAL;
  469. wrmsrl(MSR_IA32_ENERGY_PERF_BIAS, epb);
  470. }
  471. static void intel_bsp_resume(struct cpuinfo_x86 *c)
  472. {
  473. /*
  474. * MSR_IA32_ENERGY_PERF_BIAS is lost across suspend/resume,
  475. * so reinitialize it properly like during bootup:
  476. */
  477. init_intel_energy_perf(c);
  478. }
  479. static void init_cpuid_fault(struct cpuinfo_x86 *c)
  480. {
  481. u64 msr;
  482. if (!rdmsrl_safe(MSR_PLATFORM_INFO, &msr)) {
  483. if (msr & MSR_PLATFORM_INFO_CPUID_FAULT)
  484. set_cpu_cap(c, X86_FEATURE_CPUID_FAULT);
  485. }
  486. }
  487. static void init_intel_misc_features(struct cpuinfo_x86 *c)
  488. {
  489. u64 msr;
  490. if (rdmsrl_safe(MSR_MISC_FEATURES_ENABLES, &msr))
  491. return;
  492. /* Clear all MISC features */
  493. this_cpu_write(msr_misc_features_shadow, 0);
  494. /* Check features and update capabilities and shadow control bits */
  495. init_cpuid_fault(c);
  496. probe_xeon_phi_r3mwait(c);
  497. msr = this_cpu_read(msr_misc_features_shadow);
  498. wrmsrl(MSR_MISC_FEATURES_ENABLES, msr);
  499. }
  500. static void init_intel(struct cpuinfo_x86 *c)
  501. {
  502. unsigned int l2 = 0;
  503. early_init_intel(c);
  504. intel_workarounds(c);
  505. /*
  506. * Detect the extended topology information if available. This
  507. * will reinitialise the initial_apicid which will be used
  508. * in init_intel_cacheinfo()
  509. */
  510. detect_extended_topology(c);
  511. if (!cpu_has(c, X86_FEATURE_XTOPOLOGY)) {
  512. /*
  513. * let's use the legacy cpuid vector 0x1 and 0x4 for topology
  514. * detection.
  515. */
  516. c->x86_max_cores = intel_num_cpu_cores(c);
  517. #ifdef CONFIG_X86_32
  518. detect_ht(c);
  519. #endif
  520. }
  521. l2 = init_intel_cacheinfo(c);
  522. /* Detect legacy cache sizes if init_intel_cacheinfo did not */
  523. if (l2 == 0) {
  524. cpu_detect_cache_sizes(c);
  525. l2 = c->x86_cache_size;
  526. }
  527. if (c->cpuid_level > 9) {
  528. unsigned eax = cpuid_eax(10);
  529. /* Check for version and the number of counters */
  530. if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
  531. set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
  532. }
  533. if (cpu_has(c, X86_FEATURE_XMM2))
  534. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  535. if (boot_cpu_has(X86_FEATURE_DS)) {
  536. unsigned int l1;
  537. rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
  538. if (!(l1 & (1<<11)))
  539. set_cpu_cap(c, X86_FEATURE_BTS);
  540. if (!(l1 & (1<<12)))
  541. set_cpu_cap(c, X86_FEATURE_PEBS);
  542. }
  543. if (c->x86 == 6 && boot_cpu_has(X86_FEATURE_CLFLUSH) &&
  544. (c->x86_model == 29 || c->x86_model == 46 || c->x86_model == 47))
  545. set_cpu_bug(c, X86_BUG_CLFLUSH_MONITOR);
  546. if (c->x86 == 6 && boot_cpu_has(X86_FEATURE_MWAIT) &&
  547. ((c->x86_model == INTEL_FAM6_ATOM_GOLDMONT)))
  548. set_cpu_bug(c, X86_BUG_MONITOR);
  549. #ifdef CONFIG_X86_64
  550. if (c->x86 == 15)
  551. c->x86_cache_alignment = c->x86_clflush_size * 2;
  552. if (c->x86 == 6)
  553. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  554. #else
  555. /*
  556. * Names for the Pentium II/Celeron processors
  557. * detectable only by also checking the cache size.
  558. * Dixon is NOT a Celeron.
  559. */
  560. if (c->x86 == 6) {
  561. char *p = NULL;
  562. switch (c->x86_model) {
  563. case 5:
  564. if (l2 == 0)
  565. p = "Celeron (Covington)";
  566. else if (l2 == 256)
  567. p = "Mobile Pentium II (Dixon)";
  568. break;
  569. case 6:
  570. if (l2 == 128)
  571. p = "Celeron (Mendocino)";
  572. else if (c->x86_stepping == 0 || c->x86_stepping == 5)
  573. p = "Celeron-A";
  574. break;
  575. case 8:
  576. if (l2 == 128)
  577. p = "Celeron (Coppermine)";
  578. break;
  579. }
  580. if (p)
  581. strcpy(c->x86_model_id, p);
  582. }
  583. if (c->x86 == 15)
  584. set_cpu_cap(c, X86_FEATURE_P4);
  585. if (c->x86 == 6)
  586. set_cpu_cap(c, X86_FEATURE_P3);
  587. #endif
  588. /* Work around errata */
  589. srat_detect_node(c);
  590. if (cpu_has(c, X86_FEATURE_VMX))
  591. detect_vmx_virtcap(c);
  592. init_intel_energy_perf(c);
  593. init_intel_misc_features(c);
  594. }
  595. #ifdef CONFIG_X86_32
  596. static unsigned int intel_size_cache(struct cpuinfo_x86 *c, unsigned int size)
  597. {
  598. /*
  599. * Intel PIII Tualatin. This comes in two flavours.
  600. * One has 256kb of cache, the other 512. We have no way
  601. * to determine which, so we use a boottime override
  602. * for the 512kb model, and assume 256 otherwise.
  603. */
  604. if ((c->x86 == 6) && (c->x86_model == 11) && (size == 0))
  605. size = 256;
  606. /*
  607. * Intel Quark SoC X1000 contains a 4-way set associative
  608. * 16K cache with a 16 byte cache line and 256 lines per tag
  609. */
  610. if ((c->x86 == 5) && (c->x86_model == 9))
  611. size = 16;
  612. return size;
  613. }
  614. #endif
  615. #define TLB_INST_4K 0x01
  616. #define TLB_INST_4M 0x02
  617. #define TLB_INST_2M_4M 0x03
  618. #define TLB_INST_ALL 0x05
  619. #define TLB_INST_1G 0x06
  620. #define TLB_DATA_4K 0x11
  621. #define TLB_DATA_4M 0x12
  622. #define TLB_DATA_2M_4M 0x13
  623. #define TLB_DATA_4K_4M 0x14
  624. #define TLB_DATA_1G 0x16
  625. #define TLB_DATA0_4K 0x21
  626. #define TLB_DATA0_4M 0x22
  627. #define TLB_DATA0_2M_4M 0x23
  628. #define STLB_4K 0x41
  629. #define STLB_4K_2M 0x42
  630. static const struct _tlb_table intel_tlb_table[] = {
  631. { 0x01, TLB_INST_4K, 32, " TLB_INST 4 KByte pages, 4-way set associative" },
  632. { 0x02, TLB_INST_4M, 2, " TLB_INST 4 MByte pages, full associative" },
  633. { 0x03, TLB_DATA_4K, 64, " TLB_DATA 4 KByte pages, 4-way set associative" },
  634. { 0x04, TLB_DATA_4M, 8, " TLB_DATA 4 MByte pages, 4-way set associative" },
  635. { 0x05, TLB_DATA_4M, 32, " TLB_DATA 4 MByte pages, 4-way set associative" },
  636. { 0x0b, TLB_INST_4M, 4, " TLB_INST 4 MByte pages, 4-way set associative" },
  637. { 0x4f, TLB_INST_4K, 32, " TLB_INST 4 KByte pages */" },
  638. { 0x50, TLB_INST_ALL, 64, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  639. { 0x51, TLB_INST_ALL, 128, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  640. { 0x52, TLB_INST_ALL, 256, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  641. { 0x55, TLB_INST_2M_4M, 7, " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
  642. { 0x56, TLB_DATA0_4M, 16, " TLB_DATA0 4 MByte pages, 4-way set associative" },
  643. { 0x57, TLB_DATA0_4K, 16, " TLB_DATA0 4 KByte pages, 4-way associative" },
  644. { 0x59, TLB_DATA0_4K, 16, " TLB_DATA0 4 KByte pages, fully associative" },
  645. { 0x5a, TLB_DATA0_2M_4M, 32, " TLB_DATA0 2-MByte or 4 MByte pages, 4-way set associative" },
  646. { 0x5b, TLB_DATA_4K_4M, 64, " TLB_DATA 4 KByte and 4 MByte pages" },
  647. { 0x5c, TLB_DATA_4K_4M, 128, " TLB_DATA 4 KByte and 4 MByte pages" },
  648. { 0x5d, TLB_DATA_4K_4M, 256, " TLB_DATA 4 KByte and 4 MByte pages" },
  649. { 0x61, TLB_INST_4K, 48, " TLB_INST 4 KByte pages, full associative" },
  650. { 0x63, TLB_DATA_1G, 4, " TLB_DATA 1 GByte pages, 4-way set associative" },
  651. { 0x76, TLB_INST_2M_4M, 8, " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
  652. { 0xb0, TLB_INST_4K, 128, " TLB_INST 4 KByte pages, 4-way set associative" },
  653. { 0xb1, TLB_INST_2M_4M, 4, " TLB_INST 2M pages, 4-way, 8 entries or 4M pages, 4-way entries" },
  654. { 0xb2, TLB_INST_4K, 64, " TLB_INST 4KByte pages, 4-way set associative" },
  655. { 0xb3, TLB_DATA_4K, 128, " TLB_DATA 4 KByte pages, 4-way set associative" },
  656. { 0xb4, TLB_DATA_4K, 256, " TLB_DATA 4 KByte pages, 4-way associative" },
  657. { 0xb5, TLB_INST_4K, 64, " TLB_INST 4 KByte pages, 8-way set associative" },
  658. { 0xb6, TLB_INST_4K, 128, " TLB_INST 4 KByte pages, 8-way set associative" },
  659. { 0xba, TLB_DATA_4K, 64, " TLB_DATA 4 KByte pages, 4-way associative" },
  660. { 0xc0, TLB_DATA_4K_4M, 8, " TLB_DATA 4 KByte and 4 MByte pages, 4-way associative" },
  661. { 0xc1, STLB_4K_2M, 1024, " STLB 4 KByte and 2 MByte pages, 8-way associative" },
  662. { 0xc2, TLB_DATA_2M_4M, 16, " DTLB 2 MByte/4MByte pages, 4-way associative" },
  663. { 0xca, STLB_4K, 512, " STLB 4 KByte pages, 4-way associative" },
  664. { 0x00, 0, 0 }
  665. };
  666. static void intel_tlb_lookup(const unsigned char desc)
  667. {
  668. unsigned char k;
  669. if (desc == 0)
  670. return;
  671. /* look up this descriptor in the table */
  672. for (k = 0; intel_tlb_table[k].descriptor != desc && \
  673. intel_tlb_table[k].descriptor != 0; k++)
  674. ;
  675. if (intel_tlb_table[k].tlb_type == 0)
  676. return;
  677. switch (intel_tlb_table[k].tlb_type) {
  678. case STLB_4K:
  679. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  680. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  681. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  682. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  683. break;
  684. case STLB_4K_2M:
  685. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  686. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  687. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  688. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  689. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  690. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  691. if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
  692. tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
  693. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  694. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  695. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  696. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  697. break;
  698. case TLB_INST_ALL:
  699. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  700. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  701. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  702. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  703. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  704. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  705. break;
  706. case TLB_INST_4K:
  707. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  708. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  709. break;
  710. case TLB_INST_4M:
  711. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  712. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  713. break;
  714. case TLB_INST_2M_4M:
  715. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  716. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  717. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  718. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  719. break;
  720. case TLB_DATA_4K:
  721. case TLB_DATA0_4K:
  722. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  723. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  724. break;
  725. case TLB_DATA_4M:
  726. case TLB_DATA0_4M:
  727. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  728. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  729. break;
  730. case TLB_DATA_2M_4M:
  731. case TLB_DATA0_2M_4M:
  732. if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
  733. tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
  734. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  735. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  736. break;
  737. case TLB_DATA_4K_4M:
  738. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  739. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  740. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  741. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  742. break;
  743. case TLB_DATA_1G:
  744. if (tlb_lld_1g[ENTRIES] < intel_tlb_table[k].entries)
  745. tlb_lld_1g[ENTRIES] = intel_tlb_table[k].entries;
  746. break;
  747. }
  748. }
  749. static void intel_detect_tlb(struct cpuinfo_x86 *c)
  750. {
  751. int i, j, n;
  752. unsigned int regs[4];
  753. unsigned char *desc = (unsigned char *)regs;
  754. if (c->cpuid_level < 2)
  755. return;
  756. /* Number of times to iterate */
  757. n = cpuid_eax(2) & 0xFF;
  758. for (i = 0 ; i < n ; i++) {
  759. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  760. /* If bit 31 is set, this is an unknown format */
  761. for (j = 0 ; j < 3 ; j++)
  762. if (regs[j] & (1 << 31))
  763. regs[j] = 0;
  764. /* Byte 0 is level count, not a descriptor */
  765. for (j = 1 ; j < 16 ; j++)
  766. intel_tlb_lookup(desc[j]);
  767. }
  768. }
  769. static const struct cpu_dev intel_cpu_dev = {
  770. .c_vendor = "Intel",
  771. .c_ident = { "GenuineIntel" },
  772. #ifdef CONFIG_X86_32
  773. .legacy_models = {
  774. { .family = 4, .model_names =
  775. {
  776. [0] = "486 DX-25/33",
  777. [1] = "486 DX-50",
  778. [2] = "486 SX",
  779. [3] = "486 DX/2",
  780. [4] = "486 SL",
  781. [5] = "486 SX/2",
  782. [7] = "486 DX/2-WB",
  783. [8] = "486 DX/4",
  784. [9] = "486 DX/4-WB"
  785. }
  786. },
  787. { .family = 5, .model_names =
  788. {
  789. [0] = "Pentium 60/66 A-step",
  790. [1] = "Pentium 60/66",
  791. [2] = "Pentium 75 - 200",
  792. [3] = "OverDrive PODP5V83",
  793. [4] = "Pentium MMX",
  794. [7] = "Mobile Pentium 75 - 200",
  795. [8] = "Mobile Pentium MMX",
  796. [9] = "Quark SoC X1000",
  797. }
  798. },
  799. { .family = 6, .model_names =
  800. {
  801. [0] = "Pentium Pro A-step",
  802. [1] = "Pentium Pro",
  803. [3] = "Pentium II (Klamath)",
  804. [4] = "Pentium II (Deschutes)",
  805. [5] = "Pentium II (Deschutes)",
  806. [6] = "Mobile Pentium II",
  807. [7] = "Pentium III (Katmai)",
  808. [8] = "Pentium III (Coppermine)",
  809. [10] = "Pentium III (Cascades)",
  810. [11] = "Pentium III (Tualatin)",
  811. }
  812. },
  813. { .family = 15, .model_names =
  814. {
  815. [0] = "Pentium 4 (Unknown)",
  816. [1] = "Pentium 4 (Willamette)",
  817. [2] = "Pentium 4 (Northwood)",
  818. [4] = "Pentium 4 (Foster)",
  819. [5] = "Pentium 4 (Foster)",
  820. }
  821. },
  822. },
  823. .legacy_cache_size = intel_size_cache,
  824. #endif
  825. .c_detect_tlb = intel_detect_tlb,
  826. .c_early_init = early_init_intel,
  827. .c_init = init_intel,
  828. .c_bsp_resume = intel_bsp_resume,
  829. .c_x86_vendor = X86_VENDOR_INTEL,
  830. };
  831. cpu_dev_register(intel_cpu_dev);