mce_intel.c 9.8 KB

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