mce_intel.c 8.5 KB

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