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/cpu_device_id.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_discover_lock protects against parallel discovery attempts
  41. * which could race against each other.
  42. */
  43. static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
  44. /*
  45. * On systems that do support CMCI but it's disabled, polling for MCEs can
  46. * cause the same event to be reported multiple times because IA32_MCi_STATUS
  47. * is shared by the same package.
  48. */
  49. static DEFINE_SPINLOCK(cmci_poll_lock);
  50. /* Linux non-storm CMCI threshold (may be overridden by BIOS) */
  51. #define CMCI_THRESHOLD 1
  52. /*
  53. * MCi_CTL2 threshold for each bank when there is no storm.
  54. * Default value for each bank may have been set by BIOS.
  55. */
  56. static u16 cmci_threshold[MAX_NR_BANKS];
  57. /*
  58. * High threshold to limit CMCI rate during storms. Max supported is
  59. * 0x7FFF. Use this slightly smaller value so it has a distinctive
  60. * signature when some asks "Why am I not seeing all corrected errors?"
  61. * A high threshold is used instead of just disabling CMCI for a
  62. * bank because both corrected and uncorrected errors may be logged
  63. * in the same bank and signalled with CMCI. The threshold only applies
  64. * to corrected errors, so keeping CMCI enabled means that uncorrected
  65. * errors will still be processed in a timely fashion.
  66. */
  67. #define CMCI_STORM_THRESHOLD 32749
  68. static int cmci_supported(int *banks)
  69. {
  70. u64 cap;
  71. if (mca_cfg.cmci_disabled || mca_cfg.ignore_ce)
  72. return 0;
  73. /*
  74. * Vendor check is not strictly needed, but the initial
  75. * initialization is vendor keyed and this
  76. * makes sure none of the backdoors are entered otherwise.
  77. */
  78. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
  79. boot_cpu_data.x86_vendor != X86_VENDOR_ZHAOXIN)
  80. return 0;
  81. if (!boot_cpu_has(X86_FEATURE_APIC) || lapic_get_maxlvt() < 6)
  82. return 0;
  83. rdmsrl(MSR_IA32_MCG_CAP, cap);
  84. *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
  85. return !!(cap & MCG_CMCI_P);
  86. }
  87. static bool lmce_supported(void)
  88. {
  89. u64 tmp;
  90. if (mca_cfg.lmce_disabled)
  91. return false;
  92. rdmsrl(MSR_IA32_MCG_CAP, tmp);
  93. /*
  94. * LMCE depends on recovery support in the processor. Hence both
  95. * MCG_SER_P and MCG_LMCE_P should be present in MCG_CAP.
  96. */
  97. if ((tmp & (MCG_SER_P | MCG_LMCE_P)) !=
  98. (MCG_SER_P | MCG_LMCE_P))
  99. return false;
  100. /*
  101. * BIOS should indicate support for LMCE by setting bit 20 in
  102. * IA32_FEAT_CTL without which touching MCG_EXT_CTL will generate a #GP
  103. * fault. The MSR must also be locked for LMCE_ENABLED to take effect.
  104. * WARN if the MSR isn't locked as init_ia32_feat_ctl() unconditionally
  105. * locks the MSR in the event that it wasn't already locked by BIOS.
  106. */
  107. rdmsrl(MSR_IA32_FEAT_CTL, tmp);
  108. if (WARN_ON_ONCE(!(tmp & FEAT_CTL_LOCKED)))
  109. return false;
  110. return tmp & FEAT_CTL_LMCE_ENABLED;
  111. }
  112. /*
  113. * Set a new CMCI threshold value. Preserve the state of the
  114. * MCI_CTL2_CMCI_EN bit in case this happens during a
  115. * cmci_rediscover() operation.
  116. */
  117. static void cmci_set_threshold(int bank, int thresh)
  118. {
  119. unsigned long flags;
  120. u64 val;
  121. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  122. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  123. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  124. wrmsrl(MSR_IA32_MCx_CTL2(bank), val | thresh);
  125. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  126. }
  127. void mce_intel_handle_storm(int bank, bool on)
  128. {
  129. if (on)
  130. cmci_set_threshold(bank, CMCI_STORM_THRESHOLD);
  131. else
  132. cmci_set_threshold(bank, cmci_threshold[bank]);
  133. }
  134. /*
  135. * The interrupt handler. This is called on every event.
  136. * Just call the poller directly to log any events.
  137. * This could in theory increase the threshold under high load,
  138. * but doesn't for now.
  139. */
  140. static void intel_threshold_interrupt(void)
  141. {
  142. machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
  143. }
  144. /*
  145. * Check all the reasons why current CPU cannot claim
  146. * ownership of a bank.
  147. * 1: CPU already owns this bank
  148. * 2: BIOS owns this bank
  149. * 3: Some other CPU owns this bank
  150. */
  151. static bool cmci_skip_bank(int bank, u64 *val)
  152. {
  153. unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned);
  154. if (test_bit(bank, owned))
  155. return true;
  156. /* Skip banks in firmware first mode */
  157. if (test_bit(bank, mce_banks_ce_disabled))
  158. return true;
  159. rdmsrl(MSR_IA32_MCx_CTL2(bank), *val);
  160. /* Already owned by someone else? */
  161. if (*val & MCI_CTL2_CMCI_EN) {
  162. clear_bit(bank, owned);
  163. __clear_bit(bank, this_cpu_ptr(mce_poll_banks));
  164. return true;
  165. }
  166. return false;
  167. }
  168. /*
  169. * Decide which CMCI interrupt threshold to use:
  170. * 1: If this bank is in storm mode from whichever CPU was
  171. * the previous owner, stay in storm mode.
  172. * 2: If ignoring any threshold set by BIOS, set Linux default
  173. * 3: Try to honor BIOS threshold (unless buggy BIOS set it at zero).
  174. */
  175. static u64 cmci_pick_threshold(u64 val, int *bios_zero_thresh)
  176. {
  177. if ((val & MCI_CTL2_CMCI_THRESHOLD_MASK) == CMCI_STORM_THRESHOLD)
  178. return val;
  179. if (!mca_cfg.bios_cmci_threshold) {
  180. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  181. val |= CMCI_THRESHOLD;
  182. } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
  183. /*
  184. * If bios_cmci_threshold boot option was specified
  185. * but the threshold is zero, we'll try to initialize
  186. * it to 1.
  187. */
  188. *bios_zero_thresh = 1;
  189. val |= CMCI_THRESHOLD;
  190. }
  191. return val;
  192. }
  193. /*
  194. * Try to claim ownership of a bank.
  195. */
  196. static void cmci_claim_bank(int bank, u64 val, int bios_zero_thresh, int *bios_wrong_thresh)
  197. {
  198. struct mca_storm_desc *storm = this_cpu_ptr(&storm_desc);
  199. val |= MCI_CTL2_CMCI_EN;
  200. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  201. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  202. /* If the enable bit did not stick, this bank should be polled. */
  203. if (!(val & MCI_CTL2_CMCI_EN)) {
  204. WARN_ON(!test_bit(bank, this_cpu_ptr(mce_poll_banks)));
  205. storm->banks[bank].poll_only = true;
  206. return;
  207. }
  208. /* This CPU successfully set the enable bit. */
  209. set_bit(bank, (void *)this_cpu_ptr(&mce_banks_owned));
  210. if ((val & MCI_CTL2_CMCI_THRESHOLD_MASK) == CMCI_STORM_THRESHOLD) {
  211. pr_notice("CPU%d BANK%d CMCI inherited storm\n", smp_processor_id(), bank);
  212. mce_inherit_storm(bank);
  213. cmci_storm_begin(bank);
  214. } else {
  215. __clear_bit(bank, this_cpu_ptr(mce_poll_banks));
  216. }
  217. /*
  218. * We are able to set thresholds for some banks that
  219. * had a threshold of 0. This means the BIOS has not
  220. * set the thresholds properly or does not work with
  221. * this boot option. Note down now and report later.
  222. */
  223. if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
  224. (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
  225. *bios_wrong_thresh = 1;
  226. /* Save default threshold for each bank */
  227. if (cmci_threshold[bank] == 0)
  228. cmci_threshold[bank] = val & MCI_CTL2_CMCI_THRESHOLD_MASK;
  229. }
  230. /*
  231. * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
  232. * on this CPU. Use the algorithm recommended in the SDM to discover shared
  233. * banks. Called during initial bootstrap, and also for hotplug CPU operations
  234. * to rediscover/reassign machine check banks.
  235. */
  236. static void cmci_discover(int banks)
  237. {
  238. int bios_wrong_thresh = 0;
  239. unsigned long flags;
  240. int i;
  241. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  242. for (i = 0; i < banks; i++) {
  243. u64 val;
  244. int bios_zero_thresh = 0;
  245. if (cmci_skip_bank(i, &val))
  246. continue;
  247. val = cmci_pick_threshold(val, &bios_zero_thresh);
  248. cmci_claim_bank(i, val, bios_zero_thresh, &bios_wrong_thresh);
  249. }
  250. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  251. if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
  252. pr_info_once(
  253. "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
  254. pr_info_once(
  255. "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
  256. }
  257. }
  258. /*
  259. * Just in case we missed an event during initialization check
  260. * all the CMCI owned banks.
  261. */
  262. void cmci_recheck(void)
  263. {
  264. unsigned long flags;
  265. int banks;
  266. if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
  267. return;
  268. local_irq_save(flags);
  269. machine_check_poll(0, this_cpu_ptr(&mce_banks_owned));
  270. local_irq_restore(flags);
  271. }
  272. /* Caller must hold the lock on cmci_discover_lock */
  273. static void __cmci_disable_bank(int bank)
  274. {
  275. u64 val;
  276. if (!test_bit(bank, this_cpu_ptr(mce_banks_owned)))
  277. return;
  278. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  279. val &= ~MCI_CTL2_CMCI_EN;
  280. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  281. __clear_bit(bank, this_cpu_ptr(mce_banks_owned));
  282. if ((val & MCI_CTL2_CMCI_THRESHOLD_MASK) == CMCI_STORM_THRESHOLD)
  283. cmci_storm_end(bank);
  284. }
  285. /*
  286. * Disable CMCI on this CPU for all banks it owns when it goes down.
  287. * This allows other CPUs to claim the banks on rediscovery.
  288. */
  289. void cmci_clear(void)
  290. {
  291. unsigned long flags;
  292. int i;
  293. int banks;
  294. if (!cmci_supported(&banks))
  295. return;
  296. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  297. for (i = 0; i < banks; i++)
  298. __cmci_disable_bank(i);
  299. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  300. }
  301. static void cmci_rediscover_work_func(void *arg)
  302. {
  303. int banks;
  304. /* Recheck banks in case CPUs don't all have the same */
  305. if (cmci_supported(&banks))
  306. cmci_discover(banks);
  307. }
  308. /* After a CPU went down cycle through all the others and rediscover */
  309. void cmci_rediscover(void)
  310. {
  311. int banks;
  312. if (!cmci_supported(&banks))
  313. return;
  314. on_each_cpu(cmci_rediscover_work_func, NULL, 1);
  315. }
  316. /*
  317. * Reenable CMCI on this CPU in case a CPU down failed.
  318. */
  319. void cmci_reenable(void)
  320. {
  321. int banks;
  322. if (cmci_supported(&banks))
  323. cmci_discover(banks);
  324. }
  325. void cmci_disable_bank(int bank)
  326. {
  327. int banks;
  328. unsigned long flags;
  329. if (!cmci_supported(&banks))
  330. return;
  331. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  332. __cmci_disable_bank(bank);
  333. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  334. }
  335. /* Bank polling function when CMCI is disabled. */
  336. static void cmci_mc_poll_banks(void)
  337. {
  338. spin_lock(&cmci_poll_lock);
  339. machine_check_poll(0, this_cpu_ptr(&mce_poll_banks));
  340. spin_unlock(&cmci_poll_lock);
  341. }
  342. void intel_init_cmci(void)
  343. {
  344. int banks;
  345. if (!cmci_supported(&banks)) {
  346. mc_poll_banks = cmci_mc_poll_banks;
  347. return;
  348. }
  349. mce_threshold_vector = intel_threshold_interrupt;
  350. cmci_discover(banks);
  351. /*
  352. * For CPU #0 this runs with still disabled APIC, but that's
  353. * ok because only the vector is set up. We still do another
  354. * check for the banks later for CPU #0 just to make sure
  355. * to not miss any events.
  356. */
  357. apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
  358. cmci_recheck();
  359. }
  360. void intel_init_lmce(void)
  361. {
  362. u64 val;
  363. if (!lmce_supported())
  364. return;
  365. rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
  366. if (!(val & MCG_EXT_CTL_LMCE_EN))
  367. wrmsrl(MSR_IA32_MCG_EXT_CTL, val | MCG_EXT_CTL_LMCE_EN);
  368. }
  369. void intel_clear_lmce(void)
  370. {
  371. u64 val;
  372. if (!lmce_supported())
  373. return;
  374. rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
  375. val &= ~MCG_EXT_CTL_LMCE_EN;
  376. wrmsrl(MSR_IA32_MCG_EXT_CTL, val);
  377. }
  378. /*
  379. * Enable additional error logs from the integrated
  380. * memory controller on processors that support this.
  381. */
  382. static void intel_imc_init(struct cpuinfo_x86 *c)
  383. {
  384. u64 error_control;
  385. switch (c->x86_vfm) {
  386. case INTEL_SANDYBRIDGE_X:
  387. case INTEL_IVYBRIDGE_X:
  388. case INTEL_HASWELL_X:
  389. if (rdmsrl_safe(MSR_ERROR_CONTROL, &error_control))
  390. return;
  391. error_control |= 2;
  392. wrmsrl_safe(MSR_ERROR_CONTROL, error_control);
  393. break;
  394. }
  395. }
  396. void mce_intel_feature_init(struct cpuinfo_x86 *c)
  397. {
  398. intel_init_cmci();
  399. intel_init_lmce();
  400. intel_imc_init(c);
  401. }
  402. void mce_intel_feature_clear(struct cpuinfo_x86 *c)
  403. {
  404. intel_clear_lmce();
  405. cmci_clear();
  406. }
  407. bool intel_filter_mce(struct mce *m)
  408. {
  409. struct cpuinfo_x86 *c = &boot_cpu_data;
  410. /* MCE errata HSD131, HSM142, HSW131, BDM48, HSM142 and SKX37 */
  411. if ((c->x86_vfm == INTEL_HASWELL ||
  412. c->x86_vfm == INTEL_HASWELL_L ||
  413. c->x86_vfm == INTEL_BROADWELL ||
  414. c->x86_vfm == INTEL_HASWELL_G ||
  415. c->x86_vfm == INTEL_SKYLAKE_X) &&
  416. (m->bank == 0) &&
  417. ((m->status & 0xa0000000ffffffff) == 0x80000000000f0005))
  418. return true;
  419. return false;
  420. }
  421. /*
  422. * Check if the address reported by the CPU is in a format we can parse.
  423. * It would be possible to add code for most other cases, but all would
  424. * be somewhat complicated (e.g. segment offset would require an instruction
  425. * parser). So only support physical addresses up to page granularity for now.
  426. */
  427. bool intel_mce_usable_address(struct mce *m)
  428. {
  429. if (!(m->status & MCI_STATUS_MISCV))
  430. return false;
  431. if (MCI_MISC_ADDR_LSB(m->misc) > PAGE_SHIFT)
  432. return false;
  433. if (MCI_MISC_ADDR_MODE(m->misc) != MCI_MISC_ADDR_PHYS)
  434. return false;
  435. return true;
  436. }