mce_intel.c 13 KB

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