mce_intel.c 12 KB

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