mce_intel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /*
  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. static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned);
  23. /*
  24. * cmci_discover_lock protects against parallel discovery attempts
  25. * which could race against each other.
  26. */
  27. static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
  28. #define CMCI_THRESHOLD 1
  29. static int cmci_supported(int *banks)
  30. {
  31. u64 cap;
  32. if (mce_cmci_disabled || mce_ignore_ce)
  33. return 0;
  34. /*
  35. * Vendor check is not strictly needed, but the initial
  36. * initialization is vendor keyed and this
  37. * makes sure none of the backdoors are entered otherwise.
  38. */
  39. if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
  40. return 0;
  41. if (!cpu_has_apic || lapic_get_maxlvt() < 6)
  42. return 0;
  43. rdmsrl(MSR_IA32_MCG_CAP, cap);
  44. *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
  45. return !!(cap & MCG_CMCI_P);
  46. }
  47. /*
  48. * The interrupt handler. This is called on every event.
  49. * Just call the poller directly to log any events.
  50. * This could in theory increase the threshold under high load,
  51. * but doesn't for now.
  52. */
  53. static void intel_threshold_interrupt(void)
  54. {
  55. machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
  56. mce_notify_irq();
  57. }
  58. /*
  59. * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
  60. * on this CPU. Use the algorithm recommended in the SDM to discover shared
  61. * banks.
  62. */
  63. static void cmci_discover(int banks)
  64. {
  65. unsigned long *owned = (void *)&__get_cpu_var(mce_banks_owned);
  66. unsigned long flags;
  67. int i;
  68. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  69. for (i = 0; i < banks; i++) {
  70. u64 val;
  71. if (test_bit(i, owned))
  72. continue;
  73. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  74. /* Already owned by someone else? */
  75. if (val & MCI_CTL2_CMCI_EN) {
  76. clear_bit(i, owned);
  77. __clear_bit(i, __get_cpu_var(mce_poll_banks));
  78. continue;
  79. }
  80. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  81. val |= MCI_CTL2_CMCI_EN | CMCI_THRESHOLD;
  82. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  83. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  84. /* Did the enable bit stick? -- the bank supports CMCI */
  85. if (val & MCI_CTL2_CMCI_EN) {
  86. set_bit(i, owned);
  87. __clear_bit(i, __get_cpu_var(mce_poll_banks));
  88. } else {
  89. WARN_ON(!test_bit(i, __get_cpu_var(mce_poll_banks)));
  90. }
  91. }
  92. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  93. }
  94. /*
  95. * Just in case we missed an event during initialization check
  96. * all the CMCI owned banks.
  97. */
  98. void cmci_recheck(void)
  99. {
  100. unsigned long flags;
  101. int banks;
  102. if (!mce_available(__this_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
  103. return;
  104. local_irq_save(flags);
  105. machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
  106. local_irq_restore(flags);
  107. }
  108. /*
  109. * Disable CMCI on this CPU for all banks it owns when it goes down.
  110. * This allows other CPUs to claim the banks on rediscovery.
  111. */
  112. void cmci_clear(void)
  113. {
  114. unsigned long flags;
  115. int i;
  116. int banks;
  117. u64 val;
  118. if (!cmci_supported(&banks))
  119. return;
  120. raw_spin_lock_irqsave(&cmci_discover_lock, flags);
  121. for (i = 0; i < banks; i++) {
  122. if (!test_bit(i, __get_cpu_var(mce_banks_owned)))
  123. continue;
  124. /* Disable CMCI */
  125. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  126. val &= ~(MCI_CTL2_CMCI_EN|MCI_CTL2_CMCI_THRESHOLD_MASK);
  127. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  128. __clear_bit(i, __get_cpu_var(mce_banks_owned));
  129. }
  130. raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
  131. }
  132. /*
  133. * After a CPU went down cycle through all the others and rediscover
  134. * Must run in process context.
  135. */
  136. void cmci_rediscover(int dying)
  137. {
  138. int banks;
  139. int cpu;
  140. cpumask_var_t old;
  141. if (!cmci_supported(&banks))
  142. return;
  143. if (!alloc_cpumask_var(&old, GFP_KERNEL))
  144. return;
  145. cpumask_copy(old, &current->cpus_allowed);
  146. for_each_online_cpu(cpu) {
  147. if (cpu == dying)
  148. continue;
  149. if (set_cpus_allowed_ptr(current, cpumask_of(cpu)))
  150. continue;
  151. /* Recheck banks in case CPUs don't all have the same */
  152. if (cmci_supported(&banks))
  153. cmci_discover(banks);
  154. }
  155. set_cpus_allowed_ptr(current, old);
  156. free_cpumask_var(old);
  157. }
  158. /*
  159. * Reenable CMCI on this CPU in case a CPU down failed.
  160. */
  161. void cmci_reenable(void)
  162. {
  163. int banks;
  164. if (cmci_supported(&banks))
  165. cmci_discover(banks);
  166. }
  167. static void intel_init_cmci(void)
  168. {
  169. int banks;
  170. if (!cmci_supported(&banks))
  171. return;
  172. mce_threshold_vector = intel_threshold_interrupt;
  173. cmci_discover(banks);
  174. /*
  175. * For CPU #0 this runs with still disabled APIC, but that's
  176. * ok because only the vector is set up. We still do another
  177. * check for the banks later for CPU #0 just to make sure
  178. * to not miss any events.
  179. */
  180. apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
  181. cmci_recheck();
  182. }
  183. void mce_intel_feature_init(struct cpuinfo_x86 *c)
  184. {
  185. intel_init_thermal(c);
  186. intel_init_cmci();
  187. }