mce_intel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel specific MCE features.
  4. * Copyright 2004 Zwane Mwaikambo <zwane@linuxpower.ca>
  5. * Copyright (C) 2008, 2009 Intel Corporation
  6. * Author: Andi Kleen
  7. */
  8. #include <linux/gfp.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/percpu.h>
  11. #include <linux/sched.h>
  12. #include <linux/cpumask.h>
  13. #include <asm/apic.h>
  14. #include <asm/cpufeature.h>
  15. #include <asm/intel-family.h>
  16. #include <asm/processor.h>
  17. #include <asm/msr.h>
  18. #include <asm/mce.h>
  19. #include "mce-internal.h"
  20. /*
  21. * Support for Intel Correct Machine Check Interrupts. This allows
  22. * the CPU to raise an interrupt when a corrected machine check happened.
  23. * Normally we pick those up using a regular polling timer.
  24. * Also supports reliable discovery of shared banks.
  25. */
  26. /*
  27. * CMCI can be delivered to multiple cpus that share a machine check bank
  28. * so we need to designate a single cpu to process errors logged in each bank
  29. * in the interrupt handler (otherwise we would have many races and potential
  30. * double reporting of the same error).
  31. * Note that this can change when a cpu is offlined or brought online since
  32. * some MCA banks are shared across cpus. When a cpu is offlined, cmci_clear()
  33. * disables CMCI on all banks owned by the cpu and clears this bitfield. At
  34. * this point, cmci_rediscover() kicks in and a different cpu may end up
  35. * taking ownership of some of the shared MCA banks that were previously
  36. * owned by the offlined cpu.
  37. */
  38. static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned);
  39. /*
  40. * CMCI storm detection backoff counter
  41. *
  42. * During storm, we reset this counter to INITIAL_CHECK_INTERVAL in case we've
  43. * encountered an error. If not, we decrement it by one. We signal the end of
  44. * the CMCI storm when it reaches 0.
  45. */
  46. static DEFINE_PER_CPU(int, cmci_backoff_cnt);
  47. /*
  48. * cmci_discover_lock protects against parallel discovery attempts
  49. * which could race against each other.
  50. */
  51. static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
  52. #define CMCI_THRESHOLD 1
  53. #define CMCI_POLL_INTERVAL (30 * HZ)
  54. #define CMCI_STORM_INTERVAL (HZ)
  55. #define CMCI_STORM_THRESHOLD 15
  56. static DEFINE_PER_CPU(unsigned long, cmci_time_stamp);
  57. static DEFINE_PER_CPU(unsigned int, cmci_storm_cnt);
  58. static DEFINE_PER_CPU(unsigned int, cmci_storm_state);
  59. enum {
  60. CMCI_STORM_NONE,
  61. CMCI_STORM_ACTIVE,
  62. CMCI_STORM_SUBSIDED,
  63. };
  64. static atomic_t cmci_storm_on_cpus;
  65. static int cmci_supported(int *banks)
  66. {
  67. u64 cap;
  68. if (mca_cfg.cmci_disabled || mca_cfg.ignore_ce)
  69. return 0;
  70. /*
  71. * Vendor check is not strictly needed, but the initial
  72. * initialization is vendor keyed and this
  73. * makes sure none of the backdoors are entered otherwise.
  74. */
  75. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  76. return 0;
  77. if (!boot_cpu_has(X86_FEATURE_APIC) || lapic_get_maxlvt() < 6)
  78. return 0;
  79. rdmsrl(MSR_IA32_MCG_CAP, cap);
  80. *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
  81. return !!(cap & MCG_CMCI_P);
  82. }
  83. static bool lmce_supported(void)
  84. {
  85. u64 tmp;
  86. if (mca_cfg.lmce_disabled)
  87. return false;
  88. rdmsrl(MSR_IA32_MCG_CAP, tmp);
  89. /*
  90. * LMCE depends on recovery support in the processor. Hence both
  91. * MCG_SER_P and MCG_LMCE_P should be present in MCG_CAP.
  92. */
  93. if ((tmp & (MCG_SER_P | MCG_LMCE_P)) !=
  94. (MCG_SER_P | MCG_LMCE_P))
  95. return false;
  96. /*
  97. * BIOS should indicate support for LMCE by setting bit 20 in
  98. * IA32_FEATURE_CONTROL without which touching MCG_EXT_CTL will
  99. * generate a #GP fault.
  100. */
  101. rdmsrl(MSR_IA32_FEATURE_CONTROL, tmp);
  102. if ((tmp & (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_LMCE)) ==
  103. (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_LMCE))
  104. return true;
  105. return false;
  106. }
  107. bool mce_intel_cmci_poll(void)
  108. {
  109. if (__this_cpu_read(cmci_storm_state) == CMCI_STORM_NONE)
  110. return false;
  111. /*
  112. * Reset the counter if we've logged an error in the last poll
  113. * during the storm.
  114. */
  115. if (machine_check_poll(0, this_cpu_ptr(&mce_banks_owned)))
  116. this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
  117. else
  118. this_cpu_dec(cmci_backoff_cnt);
  119. return true;
  120. }
  121. void mce_intel_hcpu_update(unsigned long cpu)
  122. {
  123. if (per_cpu(cmci_storm_state, cpu) == CMCI_STORM_ACTIVE)
  124. atomic_dec(&cmci_storm_on_cpus);
  125. per_cpu(cmci_storm_state, cpu) = CMCI_STORM_NONE;
  126. }
  127. static void cmci_toggle_interrupt_mode(bool on)
  128. {
  129. unsigned long flags, *owned;
  130. int bank;
  131. u64 val;
  132. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  133. owned = this_cpu_ptr(mce_banks_owned);
  134. for_each_set_bit(bank, owned, MAX_NR_BANKS) {
  135. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  136. if (on)
  137. val |= MCI_CTL2_CMCI_EN;
  138. else
  139. val &= ~MCI_CTL2_CMCI_EN;
  140. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  141. }
  142. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  143. }
  144. unsigned long cmci_intel_adjust_timer(unsigned long interval)
  145. {
  146. if ((this_cpu_read(cmci_backoff_cnt) > 0) &&
  147. (__this_cpu_read(cmci_storm_state) == CMCI_STORM_ACTIVE)) {
  148. mce_notify_irq();
  149. return CMCI_STORM_INTERVAL;
  150. }
  151. switch (__this_cpu_read(cmci_storm_state)) {
  152. case CMCI_STORM_ACTIVE:
  153. /*
  154. * We switch back to interrupt mode once the poll timer has
  155. * silenced itself. That means no events recorded and the timer
  156. * interval is back to our poll interval.
  157. */
  158. __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED);
  159. if (!atomic_sub_return(1, &cmci_storm_on_cpus))
  160. pr_notice("CMCI storm subsided: switching to interrupt mode\n");
  161. /* FALLTHROUGH */
  162. case CMCI_STORM_SUBSIDED:
  163. /*
  164. * We wait for all CPUs to go back to SUBSIDED state. When that
  165. * happens we switch back to interrupt mode.
  166. */
  167. if (!atomic_read(&cmci_storm_on_cpus)) {
  168. __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE);
  169. cmci_toggle_interrupt_mode(true);
  170. cmci_recheck();
  171. }
  172. return CMCI_POLL_INTERVAL;
  173. default:
  174. /* We have shiny weather. Let the poll do whatever it thinks. */
  175. return interval;
  176. }
  177. }
  178. static bool cmci_storm_detect(void)
  179. {
  180. unsigned int cnt = __this_cpu_read(cmci_storm_cnt);
  181. unsigned long ts = __this_cpu_read(cmci_time_stamp);
  182. unsigned long now = jiffies;
  183. int r;
  184. if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE)
  185. return true;
  186. if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) {
  187. cnt++;
  188. } else {
  189. cnt = 1;
  190. __this_cpu_write(cmci_time_stamp, now);
  191. }
  192. __this_cpu_write(cmci_storm_cnt, cnt);
  193. if (cnt <= CMCI_STORM_THRESHOLD)
  194. return false;
  195. cmci_toggle_interrupt_mode(false);
  196. __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE);
  197. r = atomic_add_return(1, &cmci_storm_on_cpus);
  198. mce_timer_kick(CMCI_STORM_INTERVAL);
  199. this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
  200. if (r == 1)
  201. pr_notice("CMCI storm detected: switching to poll mode\n");
  202. return true;
  203. }
  204. /*
  205. * The interrupt handler. This is called on every event.
  206. * Just call the poller directly to log any events.
  207. * This could in theory increase the threshold under high load,
  208. * but doesn't for now.
  209. */
  210. static void intel_threshold_interrupt(void)
  211. {
  212. if (cmci_storm_detect())
  213. return;
  214. machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
  215. }
  216. /*
  217. * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
  218. * on this CPU. Use the algorithm recommended in the SDM to discover shared
  219. * banks.
  220. */
  221. static void cmci_discover(int banks)
  222. {
  223. unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned);
  224. unsigned long flags;
  225. int i;
  226. int bios_wrong_thresh = 0;
  227. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  228. for (i = 0; i < banks; i++) {
  229. u64 val;
  230. int bios_zero_thresh = 0;
  231. if (test_bit(i, owned))
  232. continue;
  233. /* Skip banks in firmware first mode */
  234. if (test_bit(i, mce_banks_ce_disabled))
  235. continue;
  236. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  237. /* Already owned by someone else? */
  238. if (val & MCI_CTL2_CMCI_EN) {
  239. clear_bit(i, owned);
  240. __clear_bit(i, this_cpu_ptr(mce_poll_banks));
  241. continue;
  242. }
  243. if (!mca_cfg.bios_cmci_threshold) {
  244. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  245. val |= CMCI_THRESHOLD;
  246. } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
  247. /*
  248. * If bios_cmci_threshold boot option was specified
  249. * but the threshold is zero, we'll try to initialize
  250. * it to 1.
  251. */
  252. bios_zero_thresh = 1;
  253. val |= CMCI_THRESHOLD;
  254. }
  255. val |= MCI_CTL2_CMCI_EN;
  256. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  257. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  258. /* Did the enable bit stick? -- the bank supports CMCI */
  259. if (val & MCI_CTL2_CMCI_EN) {
  260. set_bit(i, owned);
  261. __clear_bit(i, this_cpu_ptr(mce_poll_banks));
  262. /*
  263. * We are able to set thresholds for some banks that
  264. * had a threshold of 0. This means the BIOS has not
  265. * set the thresholds properly or does not work with
  266. * this boot option. Note down now and report later.
  267. */
  268. if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
  269. (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
  270. bios_wrong_thresh = 1;
  271. } else {
  272. WARN_ON(!test_bit(i, this_cpu_ptr(mce_poll_banks)));
  273. }
  274. }
  275. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  276. if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
  277. pr_info_once(
  278. "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
  279. pr_info_once(
  280. "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
  281. }
  282. }
  283. /*
  284. * Just in case we missed an event during initialization check
  285. * all the CMCI owned banks.
  286. */
  287. void cmci_recheck(void)
  288. {
  289. unsigned long flags;
  290. int banks;
  291. if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
  292. return;
  293. local_irq_save(flags);
  294. machine_check_poll(0, this_cpu_ptr(&mce_banks_owned));
  295. local_irq_restore(flags);
  296. }
  297. /* Caller must hold the lock on cmci_discover_lock */
  298. static void __cmci_disable_bank(int bank)
  299. {
  300. u64 val;
  301. if (!test_bit(bank, this_cpu_ptr(mce_banks_owned)))
  302. return;
  303. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  304. val &= ~MCI_CTL2_CMCI_EN;
  305. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  306. __clear_bit(bank, this_cpu_ptr(mce_banks_owned));
  307. }
  308. /*
  309. * Disable CMCI on this CPU for all banks it owns when it goes down.
  310. * This allows other CPUs to claim the banks on rediscovery.
  311. */
  312. void cmci_clear(void)
  313. {
  314. unsigned long flags;
  315. int i;
  316. int banks;
  317. if (!cmci_supported(&banks))
  318. return;
  319. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  320. for (i = 0; i < banks; i++)
  321. __cmci_disable_bank(i);
  322. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  323. }
  324. static void cmci_rediscover_work_func(void *arg)
  325. {
  326. int banks;
  327. /* Recheck banks in case CPUs don't all have the same */
  328. if (cmci_supported(&banks))
  329. cmci_discover(banks);
  330. }
  331. /* After a CPU went down cycle through all the others and rediscover */
  332. void cmci_rediscover(void)
  333. {
  334. int banks;
  335. if (!cmci_supported(&banks))
  336. return;
  337. on_each_cpu(cmci_rediscover_work_func, NULL, 1);
  338. }
  339. /*
  340. * Reenable CMCI on this CPU in case a CPU down failed.
  341. */
  342. void cmci_reenable(void)
  343. {
  344. int banks;
  345. if (cmci_supported(&banks))
  346. cmci_discover(banks);
  347. }
  348. void cmci_disable_bank(int bank)
  349. {
  350. int banks;
  351. unsigned long flags;
  352. if (!cmci_supported(&banks))
  353. return;
  354. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  355. __cmci_disable_bank(bank);
  356. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  357. }
  358. static void intel_init_cmci(void)
  359. {
  360. int banks;
  361. if (!cmci_supported(&banks))
  362. return;
  363. mce_threshold_vector = intel_threshold_interrupt;
  364. cmci_discover(banks);
  365. /*
  366. * For CPU #0 this runs with still disabled APIC, but that's
  367. * ok because only the vector is set up. We still do another
  368. * check for the banks later for CPU #0 just to make sure
  369. * to not miss any events.
  370. */
  371. apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
  372. cmci_recheck();
  373. }
  374. static void intel_init_lmce(void)
  375. {
  376. u64 val;
  377. if (!lmce_supported())
  378. return;
  379. rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
  380. if (!(val & MCG_EXT_CTL_LMCE_EN))
  381. wrmsrl(MSR_IA32_MCG_EXT_CTL, val | MCG_EXT_CTL_LMCE_EN);
  382. }
  383. static void intel_clear_lmce(void)
  384. {
  385. u64 val;
  386. if (!lmce_supported())
  387. return;
  388. rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
  389. val &= ~MCG_EXT_CTL_LMCE_EN;
  390. wrmsrl(MSR_IA32_MCG_EXT_CTL, val);
  391. }
  392. static void intel_ppin_init(struct cpuinfo_x86 *c)
  393. {
  394. unsigned long long val;
  395. /*
  396. * Even if testing the presence of the MSR would be enough, we don't
  397. * want to risk the situation where other models reuse this MSR for
  398. * other purposes.
  399. */
  400. switch (c->x86_model) {
  401. case INTEL_FAM6_IVYBRIDGE_X:
  402. case INTEL_FAM6_HASWELL_X:
  403. case INTEL_FAM6_BROADWELL_XEON_D:
  404. case INTEL_FAM6_BROADWELL_X:
  405. case INTEL_FAM6_SKYLAKE_X:
  406. case INTEL_FAM6_XEON_PHI_KNL:
  407. case INTEL_FAM6_XEON_PHI_KNM:
  408. if (rdmsrl_safe(MSR_PPIN_CTL, &val))
  409. return;
  410. if ((val & 3UL) == 1UL) {
  411. /* PPIN available but disabled: */
  412. return;
  413. }
  414. /* If PPIN is disabled, but not locked, try to enable: */
  415. if (!(val & 3UL)) {
  416. wrmsrl_safe(MSR_PPIN_CTL, val | 2UL);
  417. rdmsrl_safe(MSR_PPIN_CTL, &val);
  418. }
  419. if ((val & 3UL) == 2UL)
  420. set_cpu_cap(c, X86_FEATURE_INTEL_PPIN);
  421. }
  422. }
  423. void mce_intel_feature_init(struct cpuinfo_x86 *c)
  424. {
  425. intel_init_thermal(c);
  426. intel_init_cmci();
  427. intel_init_lmce();
  428. intel_ppin_init(c);
  429. }
  430. void mce_intel_feature_clear(struct cpuinfo_x86 *c)
  431. {
  432. intel_clear_lmce();
  433. }