intel.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Intel specific MCE features.
  4. * Copyright 2004 Zwane Mwaikambo <[email protected]>
  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 "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. boot_cpu_data.x86_vendor != X86_VENDOR_ZHAOXIN)
  77. return 0;
  78. if (!boot_cpu_has(X86_FEATURE_APIC) || lapic_get_maxlvt() < 6)
  79. return 0;
  80. rdmsrl(MSR_IA32_MCG_CAP, cap);
  81. *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
  82. return !!(cap & MCG_CMCI_P);
  83. }
  84. static bool lmce_supported(void)
  85. {
  86. u64 tmp;
  87. if (mca_cfg.lmce_disabled)
  88. return false;
  89. rdmsrl(MSR_IA32_MCG_CAP, tmp);
  90. /*
  91. * LMCE depends on recovery support in the processor. Hence both
  92. * MCG_SER_P and MCG_LMCE_P should be present in MCG_CAP.
  93. */
  94. if ((tmp & (MCG_SER_P | MCG_LMCE_P)) !=
  95. (MCG_SER_P | MCG_LMCE_P))
  96. return false;
  97. /*
  98. * BIOS should indicate support for LMCE by setting bit 20 in
  99. * IA32_FEAT_CTL without which touching MCG_EXT_CTL will generate a #GP
  100. * fault. The MSR must also be locked for LMCE_ENABLED to take effect.
  101. * WARN if the MSR isn't locked as init_ia32_feat_ctl() unconditionally
  102. * locks the MSR in the event that it wasn't already locked by BIOS.
  103. */
  104. rdmsrl(MSR_IA32_FEAT_CTL, tmp);
  105. if (WARN_ON_ONCE(!(tmp & FEAT_CTL_LOCKED)))
  106. return false;
  107. return tmp & FEAT_CTL_LMCE_ENABLED;
  108. }
  109. bool mce_intel_cmci_poll(void)
  110. {
  111. if (__this_cpu_read(cmci_storm_state) == CMCI_STORM_NONE)
  112. return false;
  113. /*
  114. * Reset the counter if we've logged an error in the last poll
  115. * during the storm.
  116. */
  117. if (machine_check_poll(0, this_cpu_ptr(&mce_banks_owned)))
  118. this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
  119. else
  120. this_cpu_dec(cmci_backoff_cnt);
  121. return true;
  122. }
  123. void mce_intel_hcpu_update(unsigned long cpu)
  124. {
  125. if (per_cpu(cmci_storm_state, cpu) == CMCI_STORM_ACTIVE)
  126. atomic_dec(&cmci_storm_on_cpus);
  127. per_cpu(cmci_storm_state, cpu) = CMCI_STORM_NONE;
  128. }
  129. static void cmci_toggle_interrupt_mode(bool on)
  130. {
  131. unsigned long flags, *owned;
  132. int bank;
  133. u64 val;
  134. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  135. owned = this_cpu_ptr(mce_banks_owned);
  136. for_each_set_bit(bank, owned, MAX_NR_BANKS) {
  137. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  138. if (on)
  139. val |= MCI_CTL2_CMCI_EN;
  140. else
  141. val &= ~MCI_CTL2_CMCI_EN;
  142. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  143. }
  144. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  145. }
  146. unsigned long cmci_intel_adjust_timer(unsigned long interval)
  147. {
  148. if ((this_cpu_read(cmci_backoff_cnt) > 0) &&
  149. (__this_cpu_read(cmci_storm_state) == CMCI_STORM_ACTIVE)) {
  150. mce_notify_irq();
  151. return CMCI_STORM_INTERVAL;
  152. }
  153. switch (__this_cpu_read(cmci_storm_state)) {
  154. case CMCI_STORM_ACTIVE:
  155. /*
  156. * We switch back to interrupt mode once the poll timer has
  157. * silenced itself. That means no events recorded and the timer
  158. * interval is back to our poll interval.
  159. */
  160. __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED);
  161. if (!atomic_sub_return(1, &cmci_storm_on_cpus))
  162. pr_notice("CMCI storm subsided: switching to interrupt mode\n");
  163. fallthrough;
  164. case CMCI_STORM_SUBSIDED:
  165. /*
  166. * We wait for all CPUs to go back to SUBSIDED state. When that
  167. * happens we switch back to interrupt mode.
  168. */
  169. if (!atomic_read(&cmci_storm_on_cpus)) {
  170. __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE);
  171. cmci_toggle_interrupt_mode(true);
  172. cmci_recheck();
  173. }
  174. return CMCI_POLL_INTERVAL;
  175. default:
  176. /* We have shiny weather. Let the poll do whatever it thinks. */
  177. return interval;
  178. }
  179. }
  180. static bool cmci_storm_detect(void)
  181. {
  182. unsigned int cnt = __this_cpu_read(cmci_storm_cnt);
  183. unsigned long ts = __this_cpu_read(cmci_time_stamp);
  184. unsigned long now = jiffies;
  185. int r;
  186. if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE)
  187. return true;
  188. if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) {
  189. cnt++;
  190. } else {
  191. cnt = 1;
  192. __this_cpu_write(cmci_time_stamp, now);
  193. }
  194. __this_cpu_write(cmci_storm_cnt, cnt);
  195. if (cnt <= CMCI_STORM_THRESHOLD)
  196. return false;
  197. cmci_toggle_interrupt_mode(false);
  198. __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE);
  199. r = atomic_add_return(1, &cmci_storm_on_cpus);
  200. mce_timer_kick(CMCI_STORM_INTERVAL);
  201. this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
  202. if (r == 1)
  203. pr_notice("CMCI storm detected: switching to poll mode\n");
  204. return true;
  205. }
  206. /*
  207. * The interrupt handler. This is called on every event.
  208. * Just call the poller directly to log any events.
  209. * This could in theory increase the threshold under high load,
  210. * but doesn't for now.
  211. */
  212. static void intel_threshold_interrupt(void)
  213. {
  214. if (cmci_storm_detect())
  215. return;
  216. machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
  217. }
  218. /*
  219. * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
  220. * on this CPU. Use the algorithm recommended in the SDM to discover shared
  221. * banks.
  222. */
  223. static void cmci_discover(int banks)
  224. {
  225. unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned);
  226. unsigned long flags;
  227. int i;
  228. int bios_wrong_thresh = 0;
  229. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  230. for (i = 0; i < banks; i++) {
  231. u64 val;
  232. int bios_zero_thresh = 0;
  233. if (test_bit(i, owned))
  234. continue;
  235. /* Skip banks in firmware first mode */
  236. if (test_bit(i, mce_banks_ce_disabled))
  237. continue;
  238. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  239. /* Already owned by someone else? */
  240. if (val & MCI_CTL2_CMCI_EN) {
  241. clear_bit(i, owned);
  242. __clear_bit(i, this_cpu_ptr(mce_poll_banks));
  243. continue;
  244. }
  245. if (!mca_cfg.bios_cmci_threshold) {
  246. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  247. val |= CMCI_THRESHOLD;
  248. } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
  249. /*
  250. * If bios_cmci_threshold boot option was specified
  251. * but the threshold is zero, we'll try to initialize
  252. * it to 1.
  253. */
  254. bios_zero_thresh = 1;
  255. val |= CMCI_THRESHOLD;
  256. }
  257. val |= MCI_CTL2_CMCI_EN;
  258. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  259. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  260. /* Did the enable bit stick? -- the bank supports CMCI */
  261. if (val & MCI_CTL2_CMCI_EN) {
  262. set_bit(i, owned);
  263. __clear_bit(i, this_cpu_ptr(mce_poll_banks));
  264. /*
  265. * We are able to set thresholds for some banks that
  266. * had a threshold of 0. This means the BIOS has not
  267. * set the thresholds properly or does not work with
  268. * this boot option. Note down now and report later.
  269. */
  270. if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
  271. (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
  272. bios_wrong_thresh = 1;
  273. } else {
  274. WARN_ON(!test_bit(i, this_cpu_ptr(mce_poll_banks)));
  275. }
  276. }
  277. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  278. if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
  279. pr_info_once(
  280. "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
  281. pr_info_once(
  282. "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
  283. }
  284. }
  285. /*
  286. * Just in case we missed an event during initialization check
  287. * all the CMCI owned banks.
  288. */
  289. void cmci_recheck(void)
  290. {
  291. unsigned long flags;
  292. int banks;
  293. if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
  294. return;
  295. local_irq_save(flags);
  296. machine_check_poll(0, this_cpu_ptr(&mce_banks_owned));
  297. local_irq_restore(flags);
  298. }
  299. /* Caller must hold the lock on cmci_discover_lock */
  300. static void __cmci_disable_bank(int bank)
  301. {
  302. u64 val;
  303. if (!test_bit(bank, this_cpu_ptr(mce_banks_owned)))
  304. return;
  305. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  306. val &= ~MCI_CTL2_CMCI_EN;
  307. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  308. __clear_bit(bank, this_cpu_ptr(mce_banks_owned));
  309. }
  310. /*
  311. * Disable CMCI on this CPU for all banks it owns when it goes down.
  312. * This allows other CPUs to claim the banks on rediscovery.
  313. */
  314. void cmci_clear(void)
  315. {
  316. unsigned long flags;
  317. int i;
  318. int banks;
  319. if (!cmci_supported(&banks))
  320. return;
  321. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  322. for (i = 0; i < banks; i++)
  323. __cmci_disable_bank(i);
  324. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  325. }
  326. static void cmci_rediscover_work_func(void *arg)
  327. {
  328. int banks;
  329. /* Recheck banks in case CPUs don't all have the same */
  330. if (cmci_supported(&banks))
  331. cmci_discover(banks);
  332. }
  333. /* After a CPU went down cycle through all the others and rediscover */
  334. void cmci_rediscover(void)
  335. {
  336. int banks;
  337. if (!cmci_supported(&banks))
  338. return;
  339. on_each_cpu(cmci_rediscover_work_func, NULL, 1);
  340. }
  341. /*
  342. * Reenable CMCI on this CPU in case a CPU down failed.
  343. */
  344. void cmci_reenable(void)
  345. {
  346. int banks;
  347. if (cmci_supported(&banks))
  348. cmci_discover(banks);
  349. }
  350. void cmci_disable_bank(int bank)
  351. {
  352. int banks;
  353. unsigned long flags;
  354. if (!cmci_supported(&banks))
  355. return;
  356. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  357. __cmci_disable_bank(bank);
  358. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  359. }
  360. void intel_init_cmci(void)
  361. {
  362. int banks;
  363. if (!cmci_supported(&banks))
  364. return;
  365. mce_threshold_vector = intel_threshold_interrupt;
  366. cmci_discover(banks);
  367. /*
  368. * For CPU #0 this runs with still disabled APIC, but that's
  369. * ok because only the vector is set up. We still do another
  370. * check for the banks later for CPU #0 just to make sure
  371. * to not miss any events.
  372. */
  373. apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
  374. cmci_recheck();
  375. }
  376. void intel_init_lmce(void)
  377. {
  378. u64 val;
  379. if (!lmce_supported())
  380. return;
  381. rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
  382. if (!(val & MCG_EXT_CTL_LMCE_EN))
  383. wrmsrl(MSR_IA32_MCG_EXT_CTL, val | MCG_EXT_CTL_LMCE_EN);
  384. }
  385. void intel_clear_lmce(void)
  386. {
  387. u64 val;
  388. if (!lmce_supported())
  389. return;
  390. rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
  391. val &= ~MCG_EXT_CTL_LMCE_EN;
  392. wrmsrl(MSR_IA32_MCG_EXT_CTL, val);
  393. }
  394. /*
  395. * Enable additional error logs from the integrated
  396. * memory controller on processors that support this.
  397. */
  398. static void intel_imc_init(struct cpuinfo_x86 *c)
  399. {
  400. u64 error_control;
  401. switch (c->x86_model) {
  402. case INTEL_FAM6_SANDYBRIDGE_X:
  403. case INTEL_FAM6_IVYBRIDGE_X:
  404. case INTEL_FAM6_HASWELL_X:
  405. if (rdmsrl_safe(MSR_ERROR_CONTROL, &error_control))
  406. return;
  407. error_control |= 2;
  408. wrmsrl_safe(MSR_ERROR_CONTROL, error_control);
  409. break;
  410. }
  411. }
  412. void mce_intel_feature_init(struct cpuinfo_x86 *c)
  413. {
  414. intel_init_cmci();
  415. intel_init_lmce();
  416. intel_imc_init(c);
  417. }
  418. void mce_intel_feature_clear(struct cpuinfo_x86 *c)
  419. {
  420. intel_clear_lmce();
  421. }
  422. bool intel_filter_mce(struct mce *m)
  423. {
  424. struct cpuinfo_x86 *c = &boot_cpu_data;
  425. /* MCE errata HSD131, HSM142, HSW131, BDM48, HSM142 and SKX37 */
  426. if ((c->x86 == 6) &&
  427. ((c->x86_model == INTEL_FAM6_HASWELL) ||
  428. (c->x86_model == INTEL_FAM6_HASWELL_L) ||
  429. (c->x86_model == INTEL_FAM6_BROADWELL) ||
  430. (c->x86_model == INTEL_FAM6_HASWELL_G) ||
  431. (c->x86_model == INTEL_FAM6_SKYLAKE_X)) &&
  432. (m->bank == 0) &&
  433. ((m->status & 0xa0000000ffffffff) == 0x80000000000f0005))
  434. return true;
  435. return false;
  436. }