mce_intel.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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/init.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/percpu.h>
  11. #include <linux/sched.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_RAW_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 bool cmci_storm_detect(void)
  122. {
  123. unsigned int cnt = __this_cpu_read(cmci_storm_cnt);
  124. unsigned long ts = __this_cpu_read(cmci_time_stamp);
  125. unsigned long now = jiffies;
  126. int r;
  127. if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE)
  128. return true;
  129. if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) {
  130. cnt++;
  131. } else {
  132. cnt = 1;
  133. __this_cpu_write(cmci_time_stamp, now);
  134. }
  135. __this_cpu_write(cmci_storm_cnt, cnt);
  136. if (cnt <= CMCI_STORM_THRESHOLD)
  137. return false;
  138. cmci_clear();
  139. __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE);
  140. r = atomic_add_return(1, &cmci_storm_on_cpus);
  141. mce_timer_kick(CMCI_POLL_INTERVAL);
  142. if (r == 1)
  143. pr_notice("CMCI storm detected: switching to poll mode\n");
  144. return true;
  145. }
  146. /*
  147. * The interrupt handler. This is called on every event.
  148. * Just call the poller directly to log any events.
  149. * This could in theory increase the threshold under high load,
  150. * but doesn't for now.
  151. */
  152. static void intel_threshold_interrupt(void)
  153. {
  154. if (cmci_storm_detect())
  155. return;
  156. machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
  157. mce_notify_irq();
  158. }
  159. /*
  160. * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
  161. * on this CPU. Use the algorithm recommended in the SDM to discover shared
  162. * banks.
  163. */
  164. static void cmci_discover(int banks)
  165. {
  166. unsigned long *owned = (void *)&__get_cpu_var(mce_banks_owned);
  167. unsigned long flags;
  168. int i;
  169. int bios_wrong_thresh = 0;
  170. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  171. for (i = 0; i < banks; i++) {
  172. u64 val;
  173. int bios_zero_thresh = 0;
  174. if (test_bit(i, owned))
  175. continue;
  176. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  177. /* Already owned by someone else? */
  178. if (val & MCI_CTL2_CMCI_EN) {
  179. clear_bit(i, owned);
  180. __clear_bit(i, __get_cpu_var(mce_poll_banks));
  181. continue;
  182. }
  183. if (!mca_cfg.bios_cmci_threshold) {
  184. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  185. val |= CMCI_THRESHOLD;
  186. } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
  187. /*
  188. * If bios_cmci_threshold boot option was specified
  189. * but the threshold is zero, we'll try to initialize
  190. * it to 1.
  191. */
  192. bios_zero_thresh = 1;
  193. val |= CMCI_THRESHOLD;
  194. }
  195. val |= MCI_CTL2_CMCI_EN;
  196. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  197. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  198. /* Did the enable bit stick? -- the bank supports CMCI */
  199. if (val & MCI_CTL2_CMCI_EN) {
  200. set_bit(i, owned);
  201. __clear_bit(i, __get_cpu_var(mce_poll_banks));
  202. /*
  203. * We are able to set thresholds for some banks that
  204. * had a threshold of 0. This means the BIOS has not
  205. * set the thresholds properly or does not work with
  206. * this boot option. Note down now and report later.
  207. */
  208. if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
  209. (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
  210. bios_wrong_thresh = 1;
  211. } else {
  212. WARN_ON(!test_bit(i, __get_cpu_var(mce_poll_banks)));
  213. }
  214. }
  215. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  216. if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
  217. pr_info_once(
  218. "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
  219. pr_info_once(
  220. "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
  221. }
  222. }
  223. /*
  224. * Just in case we missed an event during initialization check
  225. * all the CMCI owned banks.
  226. */
  227. void cmci_recheck(void)
  228. {
  229. unsigned long flags;
  230. int banks;
  231. if (!mce_available(__this_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
  232. return;
  233. local_irq_save(flags);
  234. machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
  235. local_irq_restore(flags);
  236. }
  237. /*
  238. * Disable CMCI on this CPU for all banks it owns when it goes down.
  239. * This allows other CPUs to claim the banks on rediscovery.
  240. */
  241. void cmci_clear(void)
  242. {
  243. unsigned long flags;
  244. int i;
  245. int banks;
  246. u64 val;
  247. if (!cmci_supported(&banks))
  248. return;
  249. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  250. for (i = 0; i < banks; i++) {
  251. if (!test_bit(i, __get_cpu_var(mce_banks_owned)))
  252. continue;
  253. /* Disable CMCI */
  254. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  255. val &= ~MCI_CTL2_CMCI_EN;
  256. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  257. __clear_bit(i, __get_cpu_var(mce_banks_owned));
  258. }
  259. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  260. }
  261. static void cmci_rediscover_work_func(void *arg)
  262. {
  263. int banks;
  264. /* Recheck banks in case CPUs don't all have the same */
  265. if (cmci_supported(&banks))
  266. cmci_discover(banks);
  267. }
  268. /* After a CPU went down cycle through all the others and rediscover */
  269. void cmci_rediscover(void)
  270. {
  271. int banks;
  272. if (!cmci_supported(&banks))
  273. return;
  274. on_each_cpu(cmci_rediscover_work_func, NULL, 1);
  275. }
  276. /*
  277. * Reenable CMCI on this CPU in case a CPU down failed.
  278. */
  279. void cmci_reenable(void)
  280. {
  281. int banks;
  282. if (cmci_supported(&banks))
  283. cmci_discover(banks);
  284. }
  285. static void intel_init_cmci(void)
  286. {
  287. int banks;
  288. if (!cmci_supported(&banks))
  289. return;
  290. mce_threshold_vector = intel_threshold_interrupt;
  291. cmci_discover(banks);
  292. /*
  293. * For CPU #0 this runs with still disabled APIC, but that's
  294. * ok because only the vector is set up. We still do another
  295. * check for the banks later for CPU #0 just to make sure
  296. * to not miss any events.
  297. */
  298. apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
  299. cmci_recheck();
  300. }
  301. void mce_intel_feature_init(struct cpuinfo_x86 *c)
  302. {
  303. intel_init_thermal(c);
  304. intel_init_cmci();
  305. }