mce_intel.c 9.4 KB

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