mce_intel.c 8.7 KB

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