mce_intel.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 (!cpu_has_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. unsigned long cmci_intel_adjust_timer(unsigned long interval)
  125. {
  126. if ((this_cpu_read(cmci_backoff_cnt) > 0) &&
  127. (__this_cpu_read(cmci_storm_state) == CMCI_STORM_ACTIVE)) {
  128. mce_notify_irq();
  129. return CMCI_STORM_INTERVAL;
  130. }
  131. switch (__this_cpu_read(cmci_storm_state)) {
  132. case CMCI_STORM_ACTIVE:
  133. /*
  134. * We switch back to interrupt mode once the poll timer has
  135. * silenced itself. That means no events recorded and the timer
  136. * interval is back to our poll interval.
  137. */
  138. __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED);
  139. if (!atomic_sub_return(1, &cmci_storm_on_cpus))
  140. pr_notice("CMCI storm subsided: switching to interrupt mode\n");
  141. /* FALLTHROUGH */
  142. case CMCI_STORM_SUBSIDED:
  143. /*
  144. * We wait for all CPUs to go back to SUBSIDED state. When that
  145. * happens we switch back to interrupt mode.
  146. */
  147. if (!atomic_read(&cmci_storm_on_cpus)) {
  148. __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE);
  149. cmci_reenable();
  150. cmci_recheck();
  151. }
  152. return CMCI_POLL_INTERVAL;
  153. default:
  154. /* We have shiny weather. Let the poll do whatever it thinks. */
  155. return interval;
  156. }
  157. }
  158. static void cmci_storm_disable_banks(void)
  159. {
  160. unsigned long flags, *owned;
  161. int bank;
  162. u64 val;
  163. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  164. owned = this_cpu_ptr(mce_banks_owned);
  165. for_each_set_bit(bank, owned, MAX_NR_BANKS) {
  166. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  167. val &= ~MCI_CTL2_CMCI_EN;
  168. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  169. }
  170. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  171. }
  172. static bool cmci_storm_detect(void)
  173. {
  174. unsigned int cnt = __this_cpu_read(cmci_storm_cnt);
  175. unsigned long ts = __this_cpu_read(cmci_time_stamp);
  176. unsigned long now = jiffies;
  177. int r;
  178. if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE)
  179. return true;
  180. if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) {
  181. cnt++;
  182. } else {
  183. cnt = 1;
  184. __this_cpu_write(cmci_time_stamp, now);
  185. }
  186. __this_cpu_write(cmci_storm_cnt, cnt);
  187. if (cnt <= CMCI_STORM_THRESHOLD)
  188. return false;
  189. cmci_storm_disable_banks();
  190. __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE);
  191. r = atomic_add_return(1, &cmci_storm_on_cpus);
  192. mce_timer_kick(CMCI_STORM_INTERVAL);
  193. this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
  194. if (r == 1)
  195. pr_notice("CMCI storm detected: switching to poll mode\n");
  196. return true;
  197. }
  198. /*
  199. * The interrupt handler. This is called on every event.
  200. * Just call the poller directly to log any events.
  201. * This could in theory increase the threshold under high load,
  202. * but doesn't for now.
  203. */
  204. static void intel_threshold_interrupt(void)
  205. {
  206. if (cmci_storm_detect())
  207. return;
  208. machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
  209. mce_notify_irq();
  210. }
  211. /*
  212. * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
  213. * on this CPU. Use the algorithm recommended in the SDM to discover shared
  214. * banks.
  215. */
  216. static void cmci_discover(int banks)
  217. {
  218. unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned);
  219. unsigned long flags;
  220. int i;
  221. int bios_wrong_thresh = 0;
  222. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  223. for (i = 0; i < banks; i++) {
  224. u64 val;
  225. int bios_zero_thresh = 0;
  226. if (test_bit(i, owned))
  227. continue;
  228. /* Skip banks in firmware first mode */
  229. if (test_bit(i, mce_banks_ce_disabled))
  230. continue;
  231. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  232. /* Already owned by someone else? */
  233. if (val & MCI_CTL2_CMCI_EN) {
  234. clear_bit(i, owned);
  235. __clear_bit(i, this_cpu_ptr(mce_poll_banks));
  236. continue;
  237. }
  238. if (!mca_cfg.bios_cmci_threshold) {
  239. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  240. val |= CMCI_THRESHOLD;
  241. } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
  242. /*
  243. * If bios_cmci_threshold boot option was specified
  244. * but the threshold is zero, we'll try to initialize
  245. * it to 1.
  246. */
  247. bios_zero_thresh = 1;
  248. val |= CMCI_THRESHOLD;
  249. }
  250. val |= MCI_CTL2_CMCI_EN;
  251. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  252. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  253. /* Did the enable bit stick? -- the bank supports CMCI */
  254. if (val & MCI_CTL2_CMCI_EN) {
  255. set_bit(i, owned);
  256. __clear_bit(i, this_cpu_ptr(mce_poll_banks));
  257. /*
  258. * We are able to set thresholds for some banks that
  259. * had a threshold of 0. This means the BIOS has not
  260. * set the thresholds properly or does not work with
  261. * this boot option. Note down now and report later.
  262. */
  263. if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
  264. (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
  265. bios_wrong_thresh = 1;
  266. } else {
  267. WARN_ON(!test_bit(i, this_cpu_ptr(mce_poll_banks)));
  268. }
  269. }
  270. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  271. if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
  272. pr_info_once(
  273. "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
  274. pr_info_once(
  275. "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
  276. }
  277. }
  278. /*
  279. * Just in case we missed an event during initialization check
  280. * all the CMCI owned banks.
  281. */
  282. void cmci_recheck(void)
  283. {
  284. unsigned long flags;
  285. int banks;
  286. if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
  287. return;
  288. local_irq_save(flags);
  289. machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
  290. local_irq_restore(flags);
  291. }
  292. /* Caller must hold the lock on cmci_discover_lock */
  293. static void __cmci_disable_bank(int bank)
  294. {
  295. u64 val;
  296. if (!test_bit(bank, this_cpu_ptr(mce_banks_owned)))
  297. return;
  298. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  299. val &= ~MCI_CTL2_CMCI_EN;
  300. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  301. __clear_bit(bank, this_cpu_ptr(mce_banks_owned));
  302. }
  303. /*
  304. * Disable CMCI on this CPU for all banks it owns when it goes down.
  305. * This allows other CPUs to claim the banks on rediscovery.
  306. */
  307. void cmci_clear(void)
  308. {
  309. unsigned long flags;
  310. int i;
  311. int banks;
  312. if (!cmci_supported(&banks))
  313. return;
  314. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  315. for (i = 0; i < banks; i++)
  316. __cmci_disable_bank(i);
  317. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  318. }
  319. static void cmci_rediscover_work_func(void *arg)
  320. {
  321. int banks;
  322. /* Recheck banks in case CPUs don't all have the same */
  323. if (cmci_supported(&banks))
  324. cmci_discover(banks);
  325. }
  326. /* After a CPU went down cycle through all the others and rediscover */
  327. void cmci_rediscover(void)
  328. {
  329. int banks;
  330. if (!cmci_supported(&banks))
  331. return;
  332. on_each_cpu(cmci_rediscover_work_func, NULL, 1);
  333. }
  334. /*
  335. * Reenable CMCI on this CPU in case a CPU down failed.
  336. */
  337. void cmci_reenable(void)
  338. {
  339. int banks;
  340. if (cmci_supported(&banks))
  341. cmci_discover(banks);
  342. }
  343. void cmci_disable_bank(int bank)
  344. {
  345. int banks;
  346. unsigned long flags;
  347. if (!cmci_supported(&banks))
  348. return;
  349. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  350. __cmci_disable_bank(bank);
  351. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  352. }
  353. static void intel_init_cmci(void)
  354. {
  355. int banks;
  356. if (!cmci_supported(&banks))
  357. return;
  358. mce_threshold_vector = intel_threshold_interrupt;
  359. cmci_discover(banks);
  360. /*
  361. * For CPU #0 this runs with still disabled APIC, but that's
  362. * ok because only the vector is set up. We still do another
  363. * check for the banks later for CPU #0 just to make sure
  364. * to not miss any events.
  365. */
  366. apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
  367. cmci_recheck();
  368. }
  369. void intel_init_lmce(void)
  370. {
  371. u64 val;
  372. if (!lmce_supported())
  373. return;
  374. rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
  375. if (!(val & MCG_EXT_CTL_LMCE_EN))
  376. wrmsrl(MSR_IA32_MCG_EXT_CTL, val | MCG_EXT_CTL_LMCE_EN);
  377. }
  378. void mce_intel_feature_init(struct cpuinfo_x86 *c)
  379. {
  380. intel_init_thermal(c);
  381. intel_init_cmci();
  382. intel_init_lmce();
  383. }