intel.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/pgtable.h>
  4. #include <linux/string.h>
  5. #include <linux/bitops.h>
  6. #include <linux/smp.h>
  7. #include <linux/sched.h>
  8. #include <linux/sched/clock.h>
  9. #include <linux/semaphore.h>
  10. #include <linux/thread_info.h>
  11. #include <linux/init.h>
  12. #include <linux/uaccess.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/delay.h>
  15. #include <linux/cpuhotplug.h>
  16. #include <asm/cpufeature.h>
  17. #include <asm/msr.h>
  18. #include <asm/bugs.h>
  19. #include <asm/cpu.h>
  20. #include <asm/intel-family.h>
  21. #include <asm/microcode_intel.h>
  22. #include <asm/hwcap2.h>
  23. #include <asm/elf.h>
  24. #include <asm/cpu_device_id.h>
  25. #include <asm/cmdline.h>
  26. #include <asm/traps.h>
  27. #include <asm/resctrl.h>
  28. #include <asm/numa.h>
  29. #include <asm/thermal.h>
  30. #ifdef CONFIG_X86_64
  31. #include <linux/topology.h>
  32. #endif
  33. #include "cpu.h"
  34. #ifdef CONFIG_X86_LOCAL_APIC
  35. #include <asm/mpspec.h>
  36. #include <asm/apic.h>
  37. #endif
  38. enum split_lock_detect_state {
  39. sld_off = 0,
  40. sld_warn,
  41. sld_fatal,
  42. sld_ratelimit,
  43. };
  44. /*
  45. * Default to sld_off because most systems do not support split lock detection.
  46. * sld_state_setup() will switch this to sld_warn on systems that support
  47. * split lock/bus lock detect, unless there is a command line override.
  48. */
  49. static enum split_lock_detect_state sld_state __ro_after_init = sld_off;
  50. static u64 msr_test_ctrl_cache __ro_after_init;
  51. /*
  52. * With a name like MSR_TEST_CTL it should go without saying, but don't touch
  53. * MSR_TEST_CTL unless the CPU is one of the whitelisted models. Writing it
  54. * on CPUs that do not support SLD can cause fireworks, even when writing '0'.
  55. */
  56. static bool cpu_model_supports_sld __ro_after_init;
  57. /*
  58. * Processors which have self-snooping capability can handle conflicting
  59. * memory type across CPUs by snooping its own cache. However, there exists
  60. * CPU models in which having conflicting memory types still leads to
  61. * unpredictable behavior, machine check errors, or hangs. Clear this
  62. * feature to prevent its use on machines with known erratas.
  63. */
  64. static void check_memory_type_self_snoop_errata(struct cpuinfo_x86 *c)
  65. {
  66. switch (c->x86_model) {
  67. case INTEL_FAM6_CORE_YONAH:
  68. case INTEL_FAM6_CORE2_MEROM:
  69. case INTEL_FAM6_CORE2_MEROM_L:
  70. case INTEL_FAM6_CORE2_PENRYN:
  71. case INTEL_FAM6_CORE2_DUNNINGTON:
  72. case INTEL_FAM6_NEHALEM:
  73. case INTEL_FAM6_NEHALEM_G:
  74. case INTEL_FAM6_NEHALEM_EP:
  75. case INTEL_FAM6_NEHALEM_EX:
  76. case INTEL_FAM6_WESTMERE:
  77. case INTEL_FAM6_WESTMERE_EP:
  78. case INTEL_FAM6_SANDYBRIDGE:
  79. setup_clear_cpu_cap(X86_FEATURE_SELFSNOOP);
  80. }
  81. }
  82. static bool ring3mwait_disabled __read_mostly;
  83. static int __init ring3mwait_disable(char *__unused)
  84. {
  85. ring3mwait_disabled = true;
  86. return 1;
  87. }
  88. __setup("ring3mwait=disable", ring3mwait_disable);
  89. static void probe_xeon_phi_r3mwait(struct cpuinfo_x86 *c)
  90. {
  91. /*
  92. * Ring 3 MONITOR/MWAIT feature cannot be detected without
  93. * cpu model and family comparison.
  94. */
  95. if (c->x86 != 6)
  96. return;
  97. switch (c->x86_model) {
  98. case INTEL_FAM6_XEON_PHI_KNL:
  99. case INTEL_FAM6_XEON_PHI_KNM:
  100. break;
  101. default:
  102. return;
  103. }
  104. if (ring3mwait_disabled)
  105. return;
  106. set_cpu_cap(c, X86_FEATURE_RING3MWAIT);
  107. this_cpu_or(msr_misc_features_shadow,
  108. 1UL << MSR_MISC_FEATURES_ENABLES_RING3MWAIT_BIT);
  109. if (c == &boot_cpu_data)
  110. ELF_HWCAP2 |= HWCAP2_RING3MWAIT;
  111. }
  112. /*
  113. * Early microcode releases for the Spectre v2 mitigation were broken.
  114. * Information taken from;
  115. * - https://newsroom.intel.com/wp-content/uploads/sites/11/2018/03/microcode-update-guidance.pdf
  116. * - https://kb.vmware.com/s/article/52345
  117. * - Microcode revisions observed in the wild
  118. * - Release note from 20180108 microcode release
  119. */
  120. struct sku_microcode {
  121. u8 model;
  122. u8 stepping;
  123. u32 microcode;
  124. };
  125. static const struct sku_microcode spectre_bad_microcodes[] = {
  126. { INTEL_FAM6_KABYLAKE, 0x0B, 0x80 },
  127. { INTEL_FAM6_KABYLAKE, 0x0A, 0x80 },
  128. { INTEL_FAM6_KABYLAKE, 0x09, 0x80 },
  129. { INTEL_FAM6_KABYLAKE_L, 0x0A, 0x80 },
  130. { INTEL_FAM6_KABYLAKE_L, 0x09, 0x80 },
  131. { INTEL_FAM6_SKYLAKE_X, 0x03, 0x0100013e },
  132. { INTEL_FAM6_SKYLAKE_X, 0x04, 0x0200003c },
  133. { INTEL_FAM6_BROADWELL, 0x04, 0x28 },
  134. { INTEL_FAM6_BROADWELL_G, 0x01, 0x1b },
  135. { INTEL_FAM6_BROADWELL_D, 0x02, 0x14 },
  136. { INTEL_FAM6_BROADWELL_D, 0x03, 0x07000011 },
  137. { INTEL_FAM6_BROADWELL_X, 0x01, 0x0b000025 },
  138. { INTEL_FAM6_HASWELL_L, 0x01, 0x21 },
  139. { INTEL_FAM6_HASWELL_G, 0x01, 0x18 },
  140. { INTEL_FAM6_HASWELL, 0x03, 0x23 },
  141. { INTEL_FAM6_HASWELL_X, 0x02, 0x3b },
  142. { INTEL_FAM6_HASWELL_X, 0x04, 0x10 },
  143. { INTEL_FAM6_IVYBRIDGE_X, 0x04, 0x42a },
  144. /* Observed in the wild */
  145. { INTEL_FAM6_SANDYBRIDGE_X, 0x06, 0x61b },
  146. { INTEL_FAM6_SANDYBRIDGE_X, 0x07, 0x712 },
  147. };
  148. static bool bad_spectre_microcode(struct cpuinfo_x86 *c)
  149. {
  150. int i;
  151. /*
  152. * We know that the hypervisor lie to us on the microcode version so
  153. * we may as well hope that it is running the correct version.
  154. */
  155. if (cpu_has(c, X86_FEATURE_HYPERVISOR))
  156. return false;
  157. if (c->x86 != 6)
  158. return false;
  159. for (i = 0; i < ARRAY_SIZE(spectre_bad_microcodes); i++) {
  160. if (c->x86_model == spectre_bad_microcodes[i].model &&
  161. c->x86_stepping == spectre_bad_microcodes[i].stepping)
  162. return (c->microcode <= spectre_bad_microcodes[i].microcode);
  163. }
  164. return false;
  165. }
  166. int intel_cpu_collect_info(struct ucode_cpu_info *uci)
  167. {
  168. unsigned int val[2];
  169. unsigned int family, model;
  170. struct cpu_signature csig = { 0 };
  171. unsigned int eax, ebx, ecx, edx;
  172. memset(uci, 0, sizeof(*uci));
  173. eax = 0x00000001;
  174. ecx = 0;
  175. native_cpuid(&eax, &ebx, &ecx, &edx);
  176. csig.sig = eax;
  177. family = x86_family(eax);
  178. model = x86_model(eax);
  179. if (model >= 5 || family > 6) {
  180. /* get processor flags from MSR 0x17 */
  181. native_rdmsr(MSR_IA32_PLATFORM_ID, val[0], val[1]);
  182. csig.pf = 1 << ((val[1] >> 18) & 7);
  183. }
  184. csig.rev = intel_get_microcode_revision();
  185. uci->cpu_sig = csig;
  186. uci->valid = 1;
  187. return 0;
  188. }
  189. EXPORT_SYMBOL_GPL(intel_cpu_collect_info);
  190. static void early_init_intel(struct cpuinfo_x86 *c)
  191. {
  192. u64 misc_enable;
  193. /* Unmask CPUID levels if masked: */
  194. if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) {
  195. if (msr_clear_bit(MSR_IA32_MISC_ENABLE,
  196. MSR_IA32_MISC_ENABLE_LIMIT_CPUID_BIT) > 0) {
  197. c->cpuid_level = cpuid_eax(0);
  198. get_cpu_cap(c);
  199. }
  200. }
  201. if ((c->x86 == 0xf && c->x86_model >= 0x03) ||
  202. (c->x86 == 0x6 && c->x86_model >= 0x0e))
  203. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  204. if (c->x86 >= 6 && !cpu_has(c, X86_FEATURE_IA64))
  205. c->microcode = intel_get_microcode_revision();
  206. /* Now if any of them are set, check the blacklist and clear the lot */
  207. if ((cpu_has(c, X86_FEATURE_SPEC_CTRL) ||
  208. cpu_has(c, X86_FEATURE_INTEL_STIBP) ||
  209. cpu_has(c, X86_FEATURE_IBRS) || cpu_has(c, X86_FEATURE_IBPB) ||
  210. cpu_has(c, X86_FEATURE_STIBP)) && bad_spectre_microcode(c)) {
  211. pr_warn("Intel Spectre v2 broken microcode detected; disabling Speculation Control\n");
  212. setup_clear_cpu_cap(X86_FEATURE_IBRS);
  213. setup_clear_cpu_cap(X86_FEATURE_IBPB);
  214. setup_clear_cpu_cap(X86_FEATURE_STIBP);
  215. setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL);
  216. setup_clear_cpu_cap(X86_FEATURE_MSR_SPEC_CTRL);
  217. setup_clear_cpu_cap(X86_FEATURE_INTEL_STIBP);
  218. setup_clear_cpu_cap(X86_FEATURE_SSBD);
  219. setup_clear_cpu_cap(X86_FEATURE_SPEC_CTRL_SSBD);
  220. }
  221. /*
  222. * Atom erratum AAE44/AAF40/AAG38/AAH41:
  223. *
  224. * A race condition between speculative fetches and invalidating
  225. * a large page. This is worked around in microcode, but we
  226. * need the microcode to have already been loaded... so if it is
  227. * not, recommend a BIOS update and disable large pages.
  228. */
  229. if (c->x86 == 6 && c->x86_model == 0x1c && c->x86_stepping <= 2 &&
  230. c->microcode < 0x20e) {
  231. pr_warn("Atom PSE erratum detected, BIOS microcode update recommended\n");
  232. clear_cpu_cap(c, X86_FEATURE_PSE);
  233. }
  234. #ifdef CONFIG_X86_64
  235. set_cpu_cap(c, X86_FEATURE_SYSENTER32);
  236. #else
  237. /* Netburst reports 64 bytes clflush size, but does IO in 128 bytes */
  238. if (c->x86 == 15 && c->x86_cache_alignment == 64)
  239. c->x86_cache_alignment = 128;
  240. #endif
  241. /* CPUID workaround for 0F33/0F34 CPU */
  242. if (c->x86 == 0xF && c->x86_model == 0x3
  243. && (c->x86_stepping == 0x3 || c->x86_stepping == 0x4))
  244. c->x86_phys_bits = 36;
  245. /*
  246. * c->x86_power is 8000_0007 edx. Bit 8 is TSC runs at constant rate
  247. * with P/T states and does not stop in deep C-states.
  248. *
  249. * It is also reliable across cores and sockets. (but not across
  250. * cabinets - we turn it off in that case explicitly.)
  251. */
  252. if (c->x86_power & (1 << 8)) {
  253. set_cpu_cap(c, X86_FEATURE_CONSTANT_TSC);
  254. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC);
  255. }
  256. /* Penwell and Cloverview have the TSC which doesn't sleep on S3 */
  257. if (c->x86 == 6) {
  258. switch (c->x86_model) {
  259. case INTEL_FAM6_ATOM_SALTWELL_MID:
  260. case INTEL_FAM6_ATOM_SALTWELL_TABLET:
  261. case INTEL_FAM6_ATOM_SILVERMONT_MID:
  262. case INTEL_FAM6_ATOM_AIRMONT_NP:
  263. set_cpu_cap(c, X86_FEATURE_NONSTOP_TSC_S3);
  264. break;
  265. default:
  266. break;
  267. }
  268. }
  269. /*
  270. * There is a known erratum on Pentium III and Core Solo
  271. * and Core Duo CPUs.
  272. * " Page with PAT set to WC while associated MTRR is UC
  273. * may consolidate to UC "
  274. * Because of this erratum, it is better to stick with
  275. * setting WC in MTRR rather than using PAT on these CPUs.
  276. *
  277. * Enable PAT WC only on P4, Core 2 or later CPUs.
  278. */
  279. if (c->x86 == 6 && c->x86_model < 15)
  280. clear_cpu_cap(c, X86_FEATURE_PAT);
  281. /*
  282. * If fast string is not enabled in IA32_MISC_ENABLE for any reason,
  283. * clear the fast string and enhanced fast string CPU capabilities.
  284. */
  285. if (c->x86 > 6 || (c->x86 == 6 && c->x86_model >= 0xd)) {
  286. rdmsrl(MSR_IA32_MISC_ENABLE, misc_enable);
  287. if (!(misc_enable & MSR_IA32_MISC_ENABLE_FAST_STRING)) {
  288. pr_info("Disabled fast string operations\n");
  289. setup_clear_cpu_cap(X86_FEATURE_REP_GOOD);
  290. setup_clear_cpu_cap(X86_FEATURE_ERMS);
  291. }
  292. }
  293. /*
  294. * Intel Quark Core DevMan_001.pdf section 6.4.11
  295. * "The operating system also is required to invalidate (i.e., flush)
  296. * the TLB when any changes are made to any of the page table entries.
  297. * The operating system must reload CR3 to cause the TLB to be flushed"
  298. *
  299. * As a result, boot_cpu_has(X86_FEATURE_PGE) in arch/x86/include/asm/tlbflush.h
  300. * should be false so that __flush_tlb_all() causes CR3 instead of CR4.PGE
  301. * to be modified.
  302. */
  303. if (c->x86 == 5 && c->x86_model == 9) {
  304. pr_info("Disabling PGE capability bit\n");
  305. setup_clear_cpu_cap(X86_FEATURE_PGE);
  306. }
  307. if (c->cpuid_level >= 0x00000001) {
  308. u32 eax, ebx, ecx, edx;
  309. cpuid(0x00000001, &eax, &ebx, &ecx, &edx);
  310. /*
  311. * If HTT (EDX[28]) is set EBX[16:23] contain the number of
  312. * apicids which are reserved per package. Store the resulting
  313. * shift value for the package management code.
  314. */
  315. if (edx & (1U << 28))
  316. c->x86_coreid_bits = get_count_order((ebx >> 16) & 0xff);
  317. }
  318. check_memory_type_self_snoop_errata(c);
  319. /*
  320. * Get the number of SMT siblings early from the extended topology
  321. * leaf, if available. Otherwise try the legacy SMT detection.
  322. */
  323. if (detect_extended_topology_early(c) < 0)
  324. detect_ht_early(c);
  325. }
  326. static void bsp_init_intel(struct cpuinfo_x86 *c)
  327. {
  328. resctrl_cpu_detect(c);
  329. }
  330. #ifdef CONFIG_X86_32
  331. /*
  332. * Early probe support logic for ppro memory erratum #50
  333. *
  334. * This is called before we do cpu ident work
  335. */
  336. int ppro_with_ram_bug(void)
  337. {
  338. /* Uses data from early_cpu_detect now */
  339. if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL &&
  340. boot_cpu_data.x86 == 6 &&
  341. boot_cpu_data.x86_model == 1 &&
  342. boot_cpu_data.x86_stepping < 8) {
  343. pr_info("Pentium Pro with Errata#50 detected. Taking evasive action.\n");
  344. return 1;
  345. }
  346. return 0;
  347. }
  348. static void intel_smp_check(struct cpuinfo_x86 *c)
  349. {
  350. /* calling is from identify_secondary_cpu() ? */
  351. if (!c->cpu_index)
  352. return;
  353. /*
  354. * Mask B, Pentium, but not Pentium MMX
  355. */
  356. if (c->x86 == 5 &&
  357. c->x86_stepping >= 1 && c->x86_stepping <= 4 &&
  358. c->x86_model <= 3) {
  359. /*
  360. * Remember we have B step Pentia with bugs
  361. */
  362. WARN_ONCE(1, "WARNING: SMP operation may be unreliable"
  363. "with B stepping processors.\n");
  364. }
  365. }
  366. static int forcepae;
  367. static int __init forcepae_setup(char *__unused)
  368. {
  369. forcepae = 1;
  370. return 1;
  371. }
  372. __setup("forcepae", forcepae_setup);
  373. static void intel_workarounds(struct cpuinfo_x86 *c)
  374. {
  375. #ifdef CONFIG_X86_F00F_BUG
  376. /*
  377. * All models of Pentium and Pentium with MMX technology CPUs
  378. * have the F0 0F bug, which lets nonprivileged users lock up the
  379. * system. Announce that the fault handler will be checking for it.
  380. * The Quark is also family 5, but does not have the same bug.
  381. */
  382. clear_cpu_bug(c, X86_BUG_F00F);
  383. if (c->x86 == 5 && c->x86_model < 9) {
  384. static int f00f_workaround_enabled;
  385. set_cpu_bug(c, X86_BUG_F00F);
  386. if (!f00f_workaround_enabled) {
  387. pr_notice("Intel Pentium with F0 0F bug - workaround enabled.\n");
  388. f00f_workaround_enabled = 1;
  389. }
  390. }
  391. #endif
  392. /*
  393. * SEP CPUID bug: Pentium Pro reports SEP but doesn't have it until
  394. * model 3 mask 3
  395. */
  396. if ((c->x86<<8 | c->x86_model<<4 | c->x86_stepping) < 0x633)
  397. clear_cpu_cap(c, X86_FEATURE_SEP);
  398. /*
  399. * PAE CPUID issue: many Pentium M report no PAE but may have a
  400. * functionally usable PAE implementation.
  401. * Forcefully enable PAE if kernel parameter "forcepae" is present.
  402. */
  403. if (forcepae) {
  404. pr_warn("PAE forced!\n");
  405. set_cpu_cap(c, X86_FEATURE_PAE);
  406. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_NOW_UNRELIABLE);
  407. }
  408. /*
  409. * P4 Xeon erratum 037 workaround.
  410. * Hardware prefetcher may cause stale data to be loaded into the cache.
  411. */
  412. if ((c->x86 == 15) && (c->x86_model == 1) && (c->x86_stepping == 1)) {
  413. if (msr_set_bit(MSR_IA32_MISC_ENABLE,
  414. MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE_BIT) > 0) {
  415. pr_info("CPU: C0 stepping P4 Xeon detected.\n");
  416. pr_info("CPU: Disabling hardware prefetching (Erratum 037)\n");
  417. }
  418. }
  419. /*
  420. * See if we have a good local APIC by checking for buggy Pentia,
  421. * i.e. all B steppings and the C2 stepping of P54C when using their
  422. * integrated APIC (see 11AP erratum in "Pentium Processor
  423. * Specification Update").
  424. */
  425. if (boot_cpu_has(X86_FEATURE_APIC) && (c->x86<<8 | c->x86_model<<4) == 0x520 &&
  426. (c->x86_stepping < 0x6 || c->x86_stepping == 0xb))
  427. set_cpu_bug(c, X86_BUG_11AP);
  428. #ifdef CONFIG_X86_INTEL_USERCOPY
  429. /*
  430. * Set up the preferred alignment for movsl bulk memory moves
  431. */
  432. switch (c->x86) {
  433. case 4: /* 486: untested */
  434. break;
  435. case 5: /* Old Pentia: untested */
  436. break;
  437. case 6: /* PII/PIII only like movsl with 8-byte alignment */
  438. movsl_mask.mask = 7;
  439. break;
  440. case 15: /* P4 is OK down to 8-byte alignment */
  441. movsl_mask.mask = 7;
  442. break;
  443. }
  444. #endif
  445. intel_smp_check(c);
  446. }
  447. #else
  448. static void intel_workarounds(struct cpuinfo_x86 *c)
  449. {
  450. }
  451. #endif
  452. static void srat_detect_node(struct cpuinfo_x86 *c)
  453. {
  454. #ifdef CONFIG_NUMA
  455. unsigned node;
  456. int cpu = smp_processor_id();
  457. /* Don't do the funky fallback heuristics the AMD version employs
  458. for now. */
  459. node = numa_cpu_node(cpu);
  460. if (node == NUMA_NO_NODE || !node_online(node)) {
  461. /* reuse the value from init_cpu_to_node() */
  462. node = cpu_to_node(cpu);
  463. }
  464. numa_set_node(cpu, node);
  465. #endif
  466. }
  467. #define MSR_IA32_TME_ACTIVATE 0x982
  468. /* Helpers to access TME_ACTIVATE MSR */
  469. #define TME_ACTIVATE_LOCKED(x) (x & 0x1)
  470. #define TME_ACTIVATE_ENABLED(x) (x & 0x2)
  471. #define TME_ACTIVATE_POLICY(x) ((x >> 4) & 0xf) /* Bits 7:4 */
  472. #define TME_ACTIVATE_POLICY_AES_XTS_128 0
  473. #define TME_ACTIVATE_KEYID_BITS(x) ((x >> 32) & 0xf) /* Bits 35:32 */
  474. #define TME_ACTIVATE_CRYPTO_ALGS(x) ((x >> 48) & 0xffff) /* Bits 63:48 */
  475. #define TME_ACTIVATE_CRYPTO_AES_XTS_128 1
  476. /* Values for mktme_status (SW only construct) */
  477. #define MKTME_ENABLED 0
  478. #define MKTME_DISABLED 1
  479. #define MKTME_UNINITIALIZED 2
  480. static int mktme_status = MKTME_UNINITIALIZED;
  481. static void detect_tme(struct cpuinfo_x86 *c)
  482. {
  483. u64 tme_activate, tme_policy, tme_crypto_algs;
  484. int keyid_bits = 0, nr_keyids = 0;
  485. static u64 tme_activate_cpu0 = 0;
  486. rdmsrl(MSR_IA32_TME_ACTIVATE, tme_activate);
  487. if (mktme_status != MKTME_UNINITIALIZED) {
  488. if (tme_activate != tme_activate_cpu0) {
  489. /* Broken BIOS? */
  490. pr_err_once("x86/tme: configuration is inconsistent between CPUs\n");
  491. pr_err_once("x86/tme: MKTME is not usable\n");
  492. mktme_status = MKTME_DISABLED;
  493. /* Proceed. We may need to exclude bits from x86_phys_bits. */
  494. }
  495. } else {
  496. tme_activate_cpu0 = tme_activate;
  497. }
  498. if (!TME_ACTIVATE_LOCKED(tme_activate) || !TME_ACTIVATE_ENABLED(tme_activate)) {
  499. pr_info_once("x86/tme: not enabled by BIOS\n");
  500. mktme_status = MKTME_DISABLED;
  501. return;
  502. }
  503. if (mktme_status != MKTME_UNINITIALIZED)
  504. goto detect_keyid_bits;
  505. pr_info("x86/tme: enabled by BIOS\n");
  506. tme_policy = TME_ACTIVATE_POLICY(tme_activate);
  507. if (tme_policy != TME_ACTIVATE_POLICY_AES_XTS_128)
  508. pr_warn("x86/tme: Unknown policy is active: %#llx\n", tme_policy);
  509. tme_crypto_algs = TME_ACTIVATE_CRYPTO_ALGS(tme_activate);
  510. if (!(tme_crypto_algs & TME_ACTIVATE_CRYPTO_AES_XTS_128)) {
  511. pr_err("x86/mktme: No known encryption algorithm is supported: %#llx\n",
  512. tme_crypto_algs);
  513. mktme_status = MKTME_DISABLED;
  514. }
  515. detect_keyid_bits:
  516. keyid_bits = TME_ACTIVATE_KEYID_BITS(tme_activate);
  517. nr_keyids = (1UL << keyid_bits) - 1;
  518. if (nr_keyids) {
  519. pr_info_once("x86/mktme: enabled by BIOS\n");
  520. pr_info_once("x86/mktme: %d KeyIDs available\n", nr_keyids);
  521. } else {
  522. pr_info_once("x86/mktme: disabled by BIOS\n");
  523. }
  524. if (mktme_status == MKTME_UNINITIALIZED) {
  525. /* MKTME is usable */
  526. mktme_status = MKTME_ENABLED;
  527. }
  528. /*
  529. * KeyID bits effectively lower the number of physical address
  530. * bits. Update cpuinfo_x86::x86_phys_bits accordingly.
  531. */
  532. c->x86_phys_bits -= keyid_bits;
  533. }
  534. static void init_cpuid_fault(struct cpuinfo_x86 *c)
  535. {
  536. u64 msr;
  537. if (!rdmsrl_safe(MSR_PLATFORM_INFO, &msr)) {
  538. if (msr & MSR_PLATFORM_INFO_CPUID_FAULT)
  539. set_cpu_cap(c, X86_FEATURE_CPUID_FAULT);
  540. }
  541. }
  542. static void init_intel_misc_features(struct cpuinfo_x86 *c)
  543. {
  544. u64 msr;
  545. if (rdmsrl_safe(MSR_MISC_FEATURES_ENABLES, &msr))
  546. return;
  547. /* Clear all MISC features */
  548. this_cpu_write(msr_misc_features_shadow, 0);
  549. /* Check features and update capabilities and shadow control bits */
  550. init_cpuid_fault(c);
  551. probe_xeon_phi_r3mwait(c);
  552. msr = this_cpu_read(msr_misc_features_shadow);
  553. wrmsrl(MSR_MISC_FEATURES_ENABLES, msr);
  554. }
  555. static void split_lock_init(void);
  556. static void bus_lock_init(void);
  557. static void init_intel(struct cpuinfo_x86 *c)
  558. {
  559. early_init_intel(c);
  560. intel_workarounds(c);
  561. /*
  562. * Detect the extended topology information if available. This
  563. * will reinitialise the initial_apicid which will be used
  564. * in init_intel_cacheinfo()
  565. */
  566. detect_extended_topology(c);
  567. if (!cpu_has(c, X86_FEATURE_XTOPOLOGY)) {
  568. /*
  569. * let's use the legacy cpuid vector 0x1 and 0x4 for topology
  570. * detection.
  571. */
  572. detect_num_cpu_cores(c);
  573. #ifdef CONFIG_X86_32
  574. detect_ht(c);
  575. #endif
  576. }
  577. init_intel_cacheinfo(c);
  578. if (c->cpuid_level > 9) {
  579. unsigned eax = cpuid_eax(10);
  580. /* Check for version and the number of counters */
  581. if ((eax & 0xff) && (((eax>>8) & 0xff) > 1))
  582. set_cpu_cap(c, X86_FEATURE_ARCH_PERFMON);
  583. }
  584. if (cpu_has(c, X86_FEATURE_XMM2))
  585. set_cpu_cap(c, X86_FEATURE_LFENCE_RDTSC);
  586. if (boot_cpu_has(X86_FEATURE_DS)) {
  587. unsigned int l1, l2;
  588. rdmsr(MSR_IA32_MISC_ENABLE, l1, l2);
  589. if (!(l1 & MSR_IA32_MISC_ENABLE_BTS_UNAVAIL))
  590. set_cpu_cap(c, X86_FEATURE_BTS);
  591. if (!(l1 & MSR_IA32_MISC_ENABLE_PEBS_UNAVAIL))
  592. set_cpu_cap(c, X86_FEATURE_PEBS);
  593. }
  594. if (c->x86 == 6 && boot_cpu_has(X86_FEATURE_CLFLUSH) &&
  595. (c->x86_model == 29 || c->x86_model == 46 || c->x86_model == 47))
  596. set_cpu_bug(c, X86_BUG_CLFLUSH_MONITOR);
  597. if (c->x86 == 6 && boot_cpu_has(X86_FEATURE_MWAIT) &&
  598. ((c->x86_model == INTEL_FAM6_ATOM_GOLDMONT)))
  599. set_cpu_bug(c, X86_BUG_MONITOR);
  600. #ifdef CONFIG_X86_64
  601. if (c->x86 == 15)
  602. c->x86_cache_alignment = c->x86_clflush_size * 2;
  603. if (c->x86 == 6)
  604. set_cpu_cap(c, X86_FEATURE_REP_GOOD);
  605. #else
  606. /*
  607. * Names for the Pentium II/Celeron processors
  608. * detectable only by also checking the cache size.
  609. * Dixon is NOT a Celeron.
  610. */
  611. if (c->x86 == 6) {
  612. unsigned int l2 = c->x86_cache_size;
  613. char *p = NULL;
  614. switch (c->x86_model) {
  615. case 5:
  616. if (l2 == 0)
  617. p = "Celeron (Covington)";
  618. else if (l2 == 256)
  619. p = "Mobile Pentium II (Dixon)";
  620. break;
  621. case 6:
  622. if (l2 == 128)
  623. p = "Celeron (Mendocino)";
  624. else if (c->x86_stepping == 0 || c->x86_stepping == 5)
  625. p = "Celeron-A";
  626. break;
  627. case 8:
  628. if (l2 == 128)
  629. p = "Celeron (Coppermine)";
  630. break;
  631. }
  632. if (p)
  633. strcpy(c->x86_model_id, p);
  634. }
  635. if (c->x86 == 15)
  636. set_cpu_cap(c, X86_FEATURE_P4);
  637. if (c->x86 == 6)
  638. set_cpu_cap(c, X86_FEATURE_P3);
  639. #endif
  640. /* Work around errata */
  641. srat_detect_node(c);
  642. init_ia32_feat_ctl(c);
  643. if (cpu_has(c, X86_FEATURE_TME))
  644. detect_tme(c);
  645. init_intel_misc_features(c);
  646. split_lock_init();
  647. bus_lock_init();
  648. intel_init_thermal(c);
  649. }
  650. #ifdef CONFIG_X86_32
  651. static unsigned int intel_size_cache(struct cpuinfo_x86 *c, unsigned int size)
  652. {
  653. /*
  654. * Intel PIII Tualatin. This comes in two flavours.
  655. * One has 256kb of cache, the other 512. We have no way
  656. * to determine which, so we use a boottime override
  657. * for the 512kb model, and assume 256 otherwise.
  658. */
  659. if ((c->x86 == 6) && (c->x86_model == 11) && (size == 0))
  660. size = 256;
  661. /*
  662. * Intel Quark SoC X1000 contains a 4-way set associative
  663. * 16K cache with a 16 byte cache line and 256 lines per tag
  664. */
  665. if ((c->x86 == 5) && (c->x86_model == 9))
  666. size = 16;
  667. return size;
  668. }
  669. #endif
  670. #define TLB_INST_4K 0x01
  671. #define TLB_INST_4M 0x02
  672. #define TLB_INST_2M_4M 0x03
  673. #define TLB_INST_ALL 0x05
  674. #define TLB_INST_1G 0x06
  675. #define TLB_DATA_4K 0x11
  676. #define TLB_DATA_4M 0x12
  677. #define TLB_DATA_2M_4M 0x13
  678. #define TLB_DATA_4K_4M 0x14
  679. #define TLB_DATA_1G 0x16
  680. #define TLB_DATA0_4K 0x21
  681. #define TLB_DATA0_4M 0x22
  682. #define TLB_DATA0_2M_4M 0x23
  683. #define STLB_4K 0x41
  684. #define STLB_4K_2M 0x42
  685. static const struct _tlb_table intel_tlb_table[] = {
  686. { 0x01, TLB_INST_4K, 32, " TLB_INST 4 KByte pages, 4-way set associative" },
  687. { 0x02, TLB_INST_4M, 2, " TLB_INST 4 MByte pages, full associative" },
  688. { 0x03, TLB_DATA_4K, 64, " TLB_DATA 4 KByte pages, 4-way set associative" },
  689. { 0x04, TLB_DATA_4M, 8, " TLB_DATA 4 MByte pages, 4-way set associative" },
  690. { 0x05, TLB_DATA_4M, 32, " TLB_DATA 4 MByte pages, 4-way set associative" },
  691. { 0x0b, TLB_INST_4M, 4, " TLB_INST 4 MByte pages, 4-way set associative" },
  692. { 0x4f, TLB_INST_4K, 32, " TLB_INST 4 KByte pages" },
  693. { 0x50, TLB_INST_ALL, 64, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  694. { 0x51, TLB_INST_ALL, 128, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  695. { 0x52, TLB_INST_ALL, 256, " TLB_INST 4 KByte and 2-MByte or 4-MByte pages" },
  696. { 0x55, TLB_INST_2M_4M, 7, " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
  697. { 0x56, TLB_DATA0_4M, 16, " TLB_DATA0 4 MByte pages, 4-way set associative" },
  698. { 0x57, TLB_DATA0_4K, 16, " TLB_DATA0 4 KByte pages, 4-way associative" },
  699. { 0x59, TLB_DATA0_4K, 16, " TLB_DATA0 4 KByte pages, fully associative" },
  700. { 0x5a, TLB_DATA0_2M_4M, 32, " TLB_DATA0 2-MByte or 4 MByte pages, 4-way set associative" },
  701. { 0x5b, TLB_DATA_4K_4M, 64, " TLB_DATA 4 KByte and 4 MByte pages" },
  702. { 0x5c, TLB_DATA_4K_4M, 128, " TLB_DATA 4 KByte and 4 MByte pages" },
  703. { 0x5d, TLB_DATA_4K_4M, 256, " TLB_DATA 4 KByte and 4 MByte pages" },
  704. { 0x61, TLB_INST_4K, 48, " TLB_INST 4 KByte pages, full associative" },
  705. { 0x63, TLB_DATA_1G, 4, " TLB_DATA 1 GByte pages, 4-way set associative" },
  706. { 0x6b, TLB_DATA_4K, 256, " TLB_DATA 4 KByte pages, 8-way associative" },
  707. { 0x6c, TLB_DATA_2M_4M, 128, " TLB_DATA 2 MByte or 4 MByte pages, 8-way associative" },
  708. { 0x6d, TLB_DATA_1G, 16, " TLB_DATA 1 GByte pages, fully associative" },
  709. { 0x76, TLB_INST_2M_4M, 8, " TLB_INST 2-MByte or 4-MByte pages, fully associative" },
  710. { 0xb0, TLB_INST_4K, 128, " TLB_INST 4 KByte pages, 4-way set associative" },
  711. { 0xb1, TLB_INST_2M_4M, 4, " TLB_INST 2M pages, 4-way, 8 entries or 4M pages, 4-way entries" },
  712. { 0xb2, TLB_INST_4K, 64, " TLB_INST 4KByte pages, 4-way set associative" },
  713. { 0xb3, TLB_DATA_4K, 128, " TLB_DATA 4 KByte pages, 4-way set associative" },
  714. { 0xb4, TLB_DATA_4K, 256, " TLB_DATA 4 KByte pages, 4-way associative" },
  715. { 0xb5, TLB_INST_4K, 64, " TLB_INST 4 KByte pages, 8-way set associative" },
  716. { 0xb6, TLB_INST_4K, 128, " TLB_INST 4 KByte pages, 8-way set associative" },
  717. { 0xba, TLB_DATA_4K, 64, " TLB_DATA 4 KByte pages, 4-way associative" },
  718. { 0xc0, TLB_DATA_4K_4M, 8, " TLB_DATA 4 KByte and 4 MByte pages, 4-way associative" },
  719. { 0xc1, STLB_4K_2M, 1024, " STLB 4 KByte and 2 MByte pages, 8-way associative" },
  720. { 0xc2, TLB_DATA_2M_4M, 16, " TLB_DATA 2 MByte/4MByte pages, 4-way associative" },
  721. { 0xca, STLB_4K, 512, " STLB 4 KByte pages, 4-way associative" },
  722. { 0x00, 0, 0 }
  723. };
  724. static void intel_tlb_lookup(const unsigned char desc)
  725. {
  726. unsigned char k;
  727. if (desc == 0)
  728. return;
  729. /* look up this descriptor in the table */
  730. for (k = 0; intel_tlb_table[k].descriptor != desc &&
  731. intel_tlb_table[k].descriptor != 0; k++)
  732. ;
  733. if (intel_tlb_table[k].tlb_type == 0)
  734. return;
  735. switch (intel_tlb_table[k].tlb_type) {
  736. case STLB_4K:
  737. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  738. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  739. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  740. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  741. break;
  742. case STLB_4K_2M:
  743. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  744. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  745. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  746. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  747. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  748. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  749. if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
  750. tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
  751. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  752. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  753. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  754. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  755. break;
  756. case TLB_INST_ALL:
  757. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  758. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  759. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  760. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  761. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  762. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  763. break;
  764. case TLB_INST_4K:
  765. if (tlb_lli_4k[ENTRIES] < intel_tlb_table[k].entries)
  766. tlb_lli_4k[ENTRIES] = intel_tlb_table[k].entries;
  767. break;
  768. case TLB_INST_4M:
  769. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  770. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  771. break;
  772. case TLB_INST_2M_4M:
  773. if (tlb_lli_2m[ENTRIES] < intel_tlb_table[k].entries)
  774. tlb_lli_2m[ENTRIES] = intel_tlb_table[k].entries;
  775. if (tlb_lli_4m[ENTRIES] < intel_tlb_table[k].entries)
  776. tlb_lli_4m[ENTRIES] = intel_tlb_table[k].entries;
  777. break;
  778. case TLB_DATA_4K:
  779. case TLB_DATA0_4K:
  780. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  781. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  782. break;
  783. case TLB_DATA_4M:
  784. case TLB_DATA0_4M:
  785. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  786. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  787. break;
  788. case TLB_DATA_2M_4M:
  789. case TLB_DATA0_2M_4M:
  790. if (tlb_lld_2m[ENTRIES] < intel_tlb_table[k].entries)
  791. tlb_lld_2m[ENTRIES] = intel_tlb_table[k].entries;
  792. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  793. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  794. break;
  795. case TLB_DATA_4K_4M:
  796. if (tlb_lld_4k[ENTRIES] < intel_tlb_table[k].entries)
  797. tlb_lld_4k[ENTRIES] = intel_tlb_table[k].entries;
  798. if (tlb_lld_4m[ENTRIES] < intel_tlb_table[k].entries)
  799. tlb_lld_4m[ENTRIES] = intel_tlb_table[k].entries;
  800. break;
  801. case TLB_DATA_1G:
  802. if (tlb_lld_1g[ENTRIES] < intel_tlb_table[k].entries)
  803. tlb_lld_1g[ENTRIES] = intel_tlb_table[k].entries;
  804. break;
  805. }
  806. }
  807. static void intel_detect_tlb(struct cpuinfo_x86 *c)
  808. {
  809. int i, j, n;
  810. unsigned int regs[4];
  811. unsigned char *desc = (unsigned char *)regs;
  812. if (c->cpuid_level < 2)
  813. return;
  814. /* Number of times to iterate */
  815. n = cpuid_eax(2) & 0xFF;
  816. for (i = 0 ; i < n ; i++) {
  817. cpuid(2, &regs[0], &regs[1], &regs[2], &regs[3]);
  818. /* If bit 31 is set, this is an unknown format */
  819. for (j = 0 ; j < 3 ; j++)
  820. if (regs[j] & (1 << 31))
  821. regs[j] = 0;
  822. /* Byte 0 is level count, not a descriptor */
  823. for (j = 1 ; j < 16 ; j++)
  824. intel_tlb_lookup(desc[j]);
  825. }
  826. }
  827. static const struct cpu_dev intel_cpu_dev = {
  828. .c_vendor = "Intel",
  829. .c_ident = { "GenuineIntel" },
  830. #ifdef CONFIG_X86_32
  831. .legacy_models = {
  832. { .family = 4, .model_names =
  833. {
  834. [0] = "486 DX-25/33",
  835. [1] = "486 DX-50",
  836. [2] = "486 SX",
  837. [3] = "486 DX/2",
  838. [4] = "486 SL",
  839. [5] = "486 SX/2",
  840. [7] = "486 DX/2-WB",
  841. [8] = "486 DX/4",
  842. [9] = "486 DX/4-WB"
  843. }
  844. },
  845. { .family = 5, .model_names =
  846. {
  847. [0] = "Pentium 60/66 A-step",
  848. [1] = "Pentium 60/66",
  849. [2] = "Pentium 75 - 200",
  850. [3] = "OverDrive PODP5V83",
  851. [4] = "Pentium MMX",
  852. [7] = "Mobile Pentium 75 - 200",
  853. [8] = "Mobile Pentium MMX",
  854. [9] = "Quark SoC X1000",
  855. }
  856. },
  857. { .family = 6, .model_names =
  858. {
  859. [0] = "Pentium Pro A-step",
  860. [1] = "Pentium Pro",
  861. [3] = "Pentium II (Klamath)",
  862. [4] = "Pentium II (Deschutes)",
  863. [5] = "Pentium II (Deschutes)",
  864. [6] = "Mobile Pentium II",
  865. [7] = "Pentium III (Katmai)",
  866. [8] = "Pentium III (Coppermine)",
  867. [10] = "Pentium III (Cascades)",
  868. [11] = "Pentium III (Tualatin)",
  869. }
  870. },
  871. { .family = 15, .model_names =
  872. {
  873. [0] = "Pentium 4 (Unknown)",
  874. [1] = "Pentium 4 (Willamette)",
  875. [2] = "Pentium 4 (Northwood)",
  876. [4] = "Pentium 4 (Foster)",
  877. [5] = "Pentium 4 (Foster)",
  878. }
  879. },
  880. },
  881. .legacy_cache_size = intel_size_cache,
  882. #endif
  883. .c_detect_tlb = intel_detect_tlb,
  884. .c_early_init = early_init_intel,
  885. .c_bsp_init = bsp_init_intel,
  886. .c_init = init_intel,
  887. .c_x86_vendor = X86_VENDOR_INTEL,
  888. };
  889. cpu_dev_register(intel_cpu_dev);
  890. #undef pr_fmt
  891. #define pr_fmt(fmt) "x86/split lock detection: " fmt
  892. static const struct {
  893. const char *option;
  894. enum split_lock_detect_state state;
  895. } sld_options[] __initconst = {
  896. { "off", sld_off },
  897. { "warn", sld_warn },
  898. { "fatal", sld_fatal },
  899. { "ratelimit:", sld_ratelimit },
  900. };
  901. static struct ratelimit_state bld_ratelimit;
  902. static unsigned int sysctl_sld_mitigate = 1;
  903. static DEFINE_SEMAPHORE(buslock_sem);
  904. #ifdef CONFIG_PROC_SYSCTL
  905. static struct ctl_table sld_sysctls[] = {
  906. {
  907. .procname = "split_lock_mitigate",
  908. .data = &sysctl_sld_mitigate,
  909. .maxlen = sizeof(unsigned int),
  910. .mode = 0644,
  911. .proc_handler = proc_douintvec_minmax,
  912. .extra1 = SYSCTL_ZERO,
  913. .extra2 = SYSCTL_ONE,
  914. },
  915. {}
  916. };
  917. static int __init sld_mitigate_sysctl_init(void)
  918. {
  919. register_sysctl_init("kernel", sld_sysctls);
  920. return 0;
  921. }
  922. late_initcall(sld_mitigate_sysctl_init);
  923. #endif
  924. static inline bool match_option(const char *arg, int arglen, const char *opt)
  925. {
  926. int len = strlen(opt), ratelimit;
  927. if (strncmp(arg, opt, len))
  928. return false;
  929. /*
  930. * Min ratelimit is 1 bus lock/sec.
  931. * Max ratelimit is 1000 bus locks/sec.
  932. */
  933. if (sscanf(arg, "ratelimit:%d", &ratelimit) == 1 &&
  934. ratelimit > 0 && ratelimit <= 1000) {
  935. ratelimit_state_init(&bld_ratelimit, HZ, ratelimit);
  936. ratelimit_set_flags(&bld_ratelimit, RATELIMIT_MSG_ON_RELEASE);
  937. return true;
  938. }
  939. return len == arglen;
  940. }
  941. static bool split_lock_verify_msr(bool on)
  942. {
  943. u64 ctrl, tmp;
  944. if (rdmsrl_safe(MSR_TEST_CTRL, &ctrl))
  945. return false;
  946. if (on)
  947. ctrl |= MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
  948. else
  949. ctrl &= ~MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
  950. if (wrmsrl_safe(MSR_TEST_CTRL, ctrl))
  951. return false;
  952. rdmsrl(MSR_TEST_CTRL, tmp);
  953. return ctrl == tmp;
  954. }
  955. static void __init sld_state_setup(void)
  956. {
  957. enum split_lock_detect_state state = sld_warn;
  958. char arg[20];
  959. int i, ret;
  960. if (!boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
  961. !boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
  962. return;
  963. ret = cmdline_find_option(boot_command_line, "split_lock_detect",
  964. arg, sizeof(arg));
  965. if (ret >= 0) {
  966. for (i = 0; i < ARRAY_SIZE(sld_options); i++) {
  967. if (match_option(arg, ret, sld_options[i].option)) {
  968. state = sld_options[i].state;
  969. break;
  970. }
  971. }
  972. }
  973. sld_state = state;
  974. }
  975. static void __init __split_lock_setup(void)
  976. {
  977. if (!split_lock_verify_msr(false)) {
  978. pr_info("MSR access failed: Disabled\n");
  979. return;
  980. }
  981. rdmsrl(MSR_TEST_CTRL, msr_test_ctrl_cache);
  982. if (!split_lock_verify_msr(true)) {
  983. pr_info("MSR access failed: Disabled\n");
  984. return;
  985. }
  986. /* Restore the MSR to its cached value. */
  987. wrmsrl(MSR_TEST_CTRL, msr_test_ctrl_cache);
  988. setup_force_cpu_cap(X86_FEATURE_SPLIT_LOCK_DETECT);
  989. }
  990. /*
  991. * MSR_TEST_CTRL is per core, but we treat it like a per CPU MSR. Locking
  992. * is not implemented as one thread could undo the setting of the other
  993. * thread immediately after dropping the lock anyway.
  994. */
  995. static void sld_update_msr(bool on)
  996. {
  997. u64 test_ctrl_val = msr_test_ctrl_cache;
  998. if (on)
  999. test_ctrl_val |= MSR_TEST_CTRL_SPLIT_LOCK_DETECT;
  1000. wrmsrl(MSR_TEST_CTRL, test_ctrl_val);
  1001. }
  1002. static void split_lock_init(void)
  1003. {
  1004. /*
  1005. * #DB for bus lock handles ratelimit and #AC for split lock is
  1006. * disabled.
  1007. */
  1008. if (sld_state == sld_ratelimit) {
  1009. split_lock_verify_msr(false);
  1010. return;
  1011. }
  1012. if (cpu_model_supports_sld)
  1013. split_lock_verify_msr(sld_state != sld_off);
  1014. }
  1015. static void __split_lock_reenable_unlock(struct work_struct *work)
  1016. {
  1017. sld_update_msr(true);
  1018. up(&buslock_sem);
  1019. }
  1020. static DECLARE_DELAYED_WORK(sl_reenable_unlock, __split_lock_reenable_unlock);
  1021. static void __split_lock_reenable(struct work_struct *work)
  1022. {
  1023. sld_update_msr(true);
  1024. }
  1025. static DECLARE_DELAYED_WORK(sl_reenable, __split_lock_reenable);
  1026. /*
  1027. * If a CPU goes offline with pending delayed work to re-enable split lock
  1028. * detection then the delayed work will be executed on some other CPU. That
  1029. * handles releasing the buslock_sem, but because it executes on a
  1030. * different CPU probably won't re-enable split lock detection. This is a
  1031. * problem on HT systems since the sibling CPU on the same core may then be
  1032. * left running with split lock detection disabled.
  1033. *
  1034. * Unconditionally re-enable detection here.
  1035. */
  1036. static int splitlock_cpu_offline(unsigned int cpu)
  1037. {
  1038. sld_update_msr(true);
  1039. return 0;
  1040. }
  1041. static void split_lock_warn(unsigned long ip)
  1042. {
  1043. struct delayed_work *work;
  1044. int cpu;
  1045. if (!current->reported_split_lock)
  1046. pr_warn_ratelimited("#AC: %s/%d took a split_lock trap at address: 0x%lx\n",
  1047. current->comm, current->pid, ip);
  1048. current->reported_split_lock = 1;
  1049. if (sysctl_sld_mitigate) {
  1050. /*
  1051. * misery factor #1:
  1052. * sleep 10ms before trying to execute split lock.
  1053. */
  1054. if (msleep_interruptible(10) > 0)
  1055. return;
  1056. /*
  1057. * Misery factor #2:
  1058. * only allow one buslocked disabled core at a time.
  1059. */
  1060. if (down_interruptible(&buslock_sem) == -EINTR)
  1061. return;
  1062. work = &sl_reenable_unlock;
  1063. } else {
  1064. work = &sl_reenable;
  1065. }
  1066. cpu = get_cpu();
  1067. schedule_delayed_work_on(cpu, work, 2);
  1068. /* Disable split lock detection on this CPU to make progress */
  1069. sld_update_msr(false);
  1070. put_cpu();
  1071. }
  1072. bool handle_guest_split_lock(unsigned long ip)
  1073. {
  1074. if (sld_state == sld_warn) {
  1075. split_lock_warn(ip);
  1076. return true;
  1077. }
  1078. pr_warn_once("#AC: %s/%d %s split_lock trap at address: 0x%lx\n",
  1079. current->comm, current->pid,
  1080. sld_state == sld_fatal ? "fatal" : "bogus", ip);
  1081. current->thread.error_code = 0;
  1082. current->thread.trap_nr = X86_TRAP_AC;
  1083. force_sig_fault(SIGBUS, BUS_ADRALN, NULL);
  1084. return false;
  1085. }
  1086. EXPORT_SYMBOL_GPL(handle_guest_split_lock);
  1087. static void bus_lock_init(void)
  1088. {
  1089. u64 val;
  1090. if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
  1091. return;
  1092. rdmsrl(MSR_IA32_DEBUGCTLMSR, val);
  1093. if ((boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) &&
  1094. (sld_state == sld_warn || sld_state == sld_fatal)) ||
  1095. sld_state == sld_off) {
  1096. /*
  1097. * Warn and fatal are handled by #AC for split lock if #AC for
  1098. * split lock is supported.
  1099. */
  1100. val &= ~DEBUGCTLMSR_BUS_LOCK_DETECT;
  1101. } else {
  1102. val |= DEBUGCTLMSR_BUS_LOCK_DETECT;
  1103. }
  1104. wrmsrl(MSR_IA32_DEBUGCTLMSR, val);
  1105. }
  1106. bool handle_user_split_lock(struct pt_regs *regs, long error_code)
  1107. {
  1108. if ((regs->flags & X86_EFLAGS_AC) || sld_state == sld_fatal)
  1109. return false;
  1110. split_lock_warn(regs->ip);
  1111. return true;
  1112. }
  1113. void handle_bus_lock(struct pt_regs *regs)
  1114. {
  1115. switch (sld_state) {
  1116. case sld_off:
  1117. break;
  1118. case sld_ratelimit:
  1119. /* Enforce no more than bld_ratelimit bus locks/sec. */
  1120. while (!__ratelimit(&bld_ratelimit))
  1121. msleep(20);
  1122. /* Warn on the bus lock. */
  1123. fallthrough;
  1124. case sld_warn:
  1125. pr_warn_ratelimited("#DB: %s/%d took a bus_lock trap at address: 0x%lx\n",
  1126. current->comm, current->pid, regs->ip);
  1127. break;
  1128. case sld_fatal:
  1129. force_sig_fault(SIGBUS, BUS_ADRALN, NULL);
  1130. break;
  1131. }
  1132. }
  1133. /*
  1134. * Bits in the IA32_CORE_CAPABILITIES are not architectural, so they should
  1135. * only be trusted if it is confirmed that a CPU model implements a
  1136. * specific feature at a particular bit position.
  1137. *
  1138. * The possible driver data field values:
  1139. *
  1140. * - 0: CPU models that are known to have the per-core split-lock detection
  1141. * feature even though they do not enumerate IA32_CORE_CAPABILITIES.
  1142. *
  1143. * - 1: CPU models which may enumerate IA32_CORE_CAPABILITIES and if so use
  1144. * bit 5 to enumerate the per-core split-lock detection feature.
  1145. */
  1146. static const struct x86_cpu_id split_lock_cpu_ids[] __initconst = {
  1147. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_X, 0),
  1148. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_L, 0),
  1149. X86_MATCH_INTEL_FAM6_MODEL(ICELAKE_D, 0),
  1150. X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT, 1),
  1151. X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_D, 1),
  1152. X86_MATCH_INTEL_FAM6_MODEL(ATOM_TREMONT_L, 1),
  1153. X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE_L, 1),
  1154. X86_MATCH_INTEL_FAM6_MODEL(TIGERLAKE, 1),
  1155. X86_MATCH_INTEL_FAM6_MODEL(SAPPHIRERAPIDS_X, 1),
  1156. X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE, 1),
  1157. X86_MATCH_INTEL_FAM6_MODEL(ALDERLAKE_L, 1),
  1158. X86_MATCH_INTEL_FAM6_MODEL(RAPTORLAKE, 1),
  1159. {}
  1160. };
  1161. static void __init split_lock_setup(struct cpuinfo_x86 *c)
  1162. {
  1163. const struct x86_cpu_id *m;
  1164. u64 ia32_core_caps;
  1165. if (boot_cpu_has(X86_FEATURE_HYPERVISOR))
  1166. return;
  1167. m = x86_match_cpu(split_lock_cpu_ids);
  1168. if (!m)
  1169. return;
  1170. switch (m->driver_data) {
  1171. case 0:
  1172. break;
  1173. case 1:
  1174. if (!cpu_has(c, X86_FEATURE_CORE_CAPABILITIES))
  1175. return;
  1176. rdmsrl(MSR_IA32_CORE_CAPS, ia32_core_caps);
  1177. if (!(ia32_core_caps & MSR_IA32_CORE_CAPS_SPLIT_LOCK_DETECT))
  1178. return;
  1179. break;
  1180. default:
  1181. return;
  1182. }
  1183. cpu_model_supports_sld = true;
  1184. __split_lock_setup();
  1185. }
  1186. static void sld_state_show(void)
  1187. {
  1188. if (!boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT) &&
  1189. !boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT))
  1190. return;
  1191. switch (sld_state) {
  1192. case sld_off:
  1193. pr_info("disabled\n");
  1194. break;
  1195. case sld_warn:
  1196. if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
  1197. pr_info("#AC: crashing the kernel on kernel split_locks and warning on user-space split_locks\n");
  1198. if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
  1199. "x86/splitlock", NULL, splitlock_cpu_offline) < 0)
  1200. pr_warn("No splitlock CPU offline handler\n");
  1201. } else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
  1202. pr_info("#DB: warning on user-space bus_locks\n");
  1203. }
  1204. break;
  1205. case sld_fatal:
  1206. if (boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT)) {
  1207. pr_info("#AC: crashing the kernel on kernel split_locks and sending SIGBUS on user-space split_locks\n");
  1208. } else if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT)) {
  1209. pr_info("#DB: sending SIGBUS on user-space bus_locks%s\n",
  1210. boot_cpu_has(X86_FEATURE_SPLIT_LOCK_DETECT) ?
  1211. " from non-WB" : "");
  1212. }
  1213. break;
  1214. case sld_ratelimit:
  1215. if (boot_cpu_has(X86_FEATURE_BUS_LOCK_DETECT))
  1216. pr_info("#DB: setting system wide bus lock rate limit to %u/sec\n", bld_ratelimit.burst);
  1217. break;
  1218. }
  1219. }
  1220. void __init sld_setup(struct cpuinfo_x86 *c)
  1221. {
  1222. split_lock_setup(c);
  1223. sld_state_setup();
  1224. sld_state_show();
  1225. }
  1226. #define X86_HYBRID_CPU_TYPE_ID_SHIFT 24
  1227. /**
  1228. * get_this_hybrid_cpu_type() - Get the type of this hybrid CPU
  1229. *
  1230. * Returns the CPU type [31:24] (i.e., Atom or Core) of a CPU in
  1231. * a hybrid processor. If the processor is not hybrid, returns 0.
  1232. */
  1233. u8 get_this_hybrid_cpu_type(void)
  1234. {
  1235. if (!cpu_feature_enabled(X86_FEATURE_HYBRID_CPU))
  1236. return 0;
  1237. return cpuid_eax(0x0000001a) >> X86_HYBRID_CPU_TYPE_ID_SHIFT;
  1238. }