mce_intel.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. /* Skip banks in firmware first mode */
  177. if (test_bit(i, mce_banks_ce_disabled))
  178. continue;
  179. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  180. /* Already owned by someone else? */
  181. if (val & MCI_CTL2_CMCI_EN) {
  182. clear_bit(i, owned);
  183. __clear_bit(i, __get_cpu_var(mce_poll_banks));
  184. continue;
  185. }
  186. if (!mca_cfg.bios_cmci_threshold) {
  187. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  188. val |= CMCI_THRESHOLD;
  189. } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
  190. /*
  191. * If bios_cmci_threshold boot option was specified
  192. * but the threshold is zero, we'll try to initialize
  193. * it to 1.
  194. */
  195. bios_zero_thresh = 1;
  196. val |= CMCI_THRESHOLD;
  197. }
  198. val |= MCI_CTL2_CMCI_EN;
  199. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  200. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  201. /* Did the enable bit stick? -- the bank supports CMCI */
  202. if (val & MCI_CTL2_CMCI_EN) {
  203. set_bit(i, owned);
  204. __clear_bit(i, __get_cpu_var(mce_poll_banks));
  205. /*
  206. * We are able to set thresholds for some banks that
  207. * had a threshold of 0. This means the BIOS has not
  208. * set the thresholds properly or does not work with
  209. * this boot option. Note down now and report later.
  210. */
  211. if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
  212. (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
  213. bios_wrong_thresh = 1;
  214. } else {
  215. WARN_ON(!test_bit(i, __get_cpu_var(mce_poll_banks)));
  216. }
  217. }
  218. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  219. if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
  220. pr_info_once(
  221. "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
  222. pr_info_once(
  223. "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
  224. }
  225. }
  226. /*
  227. * Just in case we missed an event during initialization check
  228. * all the CMCI owned banks.
  229. */
  230. void cmci_recheck(void)
  231. {
  232. unsigned long flags;
  233. int banks;
  234. if (!mce_available(__this_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
  235. return;
  236. local_irq_save(flags);
  237. machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
  238. local_irq_restore(flags);
  239. }
  240. /* Caller must hold the lock on cmci_discover_lock */
  241. static void __cmci_disable_bank(int bank)
  242. {
  243. u64 val;
  244. if (!test_bit(bank, __get_cpu_var(mce_banks_owned)))
  245. return;
  246. rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
  247. val &= ~MCI_CTL2_CMCI_EN;
  248. wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
  249. __clear_bit(bank, __get_cpu_var(mce_banks_owned));
  250. }
  251. /*
  252. * Disable CMCI on this CPU for all banks it owns when it goes down.
  253. * This allows other CPUs to claim the banks on rediscovery.
  254. */
  255. void cmci_clear(void)
  256. {
  257. unsigned long flags;
  258. int i;
  259. int banks;
  260. if (!cmci_supported(&banks))
  261. return;
  262. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  263. for (i = 0; i < banks; i++)
  264. __cmci_disable_bank(i);
  265. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  266. }
  267. static void cmci_rediscover_work_func(void *arg)
  268. {
  269. int banks;
  270. /* Recheck banks in case CPUs don't all have the same */
  271. if (cmci_supported(&banks))
  272. cmci_discover(banks);
  273. }
  274. /* After a CPU went down cycle through all the others and rediscover */
  275. void cmci_rediscover(void)
  276. {
  277. int banks;
  278. if (!cmci_supported(&banks))
  279. return;
  280. on_each_cpu(cmci_rediscover_work_func, NULL, 1);
  281. }
  282. /*
  283. * Reenable CMCI on this CPU in case a CPU down failed.
  284. */
  285. void cmci_reenable(void)
  286. {
  287. int banks;
  288. if (cmci_supported(&banks))
  289. cmci_discover(banks);
  290. }
  291. void cmci_disable_bank(int bank)
  292. {
  293. int banks;
  294. unsigned long flags;
  295. if (!cmci_supported(&banks))
  296. return;
  297. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  298. __cmci_disable_bank(bank);
  299. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  300. }
  301. static void intel_init_cmci(void)
  302. {
  303. int banks;
  304. if (!cmci_supported(&banks))
  305. return;
  306. mce_threshold_vector = intel_threshold_interrupt;
  307. cmci_discover(banks);
  308. /*
  309. * For CPU #0 this runs with still disabled APIC, but that's
  310. * ok because only the vector is set up. We still do another
  311. * check for the banks later for CPU #0 just to make sure
  312. * to not miss any events.
  313. */
  314. apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
  315. cmci_recheck();
  316. }
  317. void mce_intel_feature_init(struct cpuinfo_x86 *c)
  318. {
  319. intel_init_thermal(c);
  320. intel_init_cmci();
  321. }