mce_intel.c 10 KB

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