mce_intel.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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_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. static void print_update(char *type, int *hdr, int num)
  59. {
  60. if (*hdr == 0)
  61. printk(KERN_INFO "CPU %d MCA banks", smp_processor_id());
  62. *hdr = 1;
  63. printk(KERN_CONT " %s:%d", type, num);
  64. }
  65. /*
  66. * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
  67. * on this CPU. Use the algorithm recommended in the SDM to discover shared
  68. * banks.
  69. */
  70. static void cmci_discover(int banks, int boot)
  71. {
  72. unsigned long *owned = (void *)&__get_cpu_var(mce_banks_owned);
  73. unsigned long flags;
  74. int hdr = 0;
  75. int i;
  76. spin_lock_irqsave(&cmci_discover_lock, flags);
  77. for (i = 0; i < banks; i++) {
  78. u64 val;
  79. if (test_bit(i, owned))
  80. continue;
  81. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  82. /* Already owned by someone else? */
  83. if (val & MCI_CTL2_CMCI_EN) {
  84. if (test_and_clear_bit(i, owned) && !boot)
  85. print_update("SHD", &hdr, i);
  86. __clear_bit(i, __get_cpu_var(mce_poll_banks));
  87. continue;
  88. }
  89. val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
  90. val |= MCI_CTL2_CMCI_EN | CMCI_THRESHOLD;
  91. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  92. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  93. /* Did the enable bit stick? -- the bank supports CMCI */
  94. if (val & MCI_CTL2_CMCI_EN) {
  95. if (!test_and_set_bit(i, owned) && !boot)
  96. print_update("CMCI", &hdr, i);
  97. __clear_bit(i, __get_cpu_var(mce_poll_banks));
  98. } else {
  99. WARN_ON(!test_bit(i, __get_cpu_var(mce_poll_banks)));
  100. }
  101. }
  102. spin_unlock_irqrestore(&cmci_discover_lock, flags);
  103. if (hdr)
  104. printk(KERN_CONT "\n");
  105. }
  106. /*
  107. * Just in case we missed an event during initialization check
  108. * all the CMCI owned banks.
  109. */
  110. void cmci_recheck(void)
  111. {
  112. unsigned long flags;
  113. int banks;
  114. if (!mce_available(__this_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
  115. return;
  116. local_irq_save(flags);
  117. machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
  118. local_irq_restore(flags);
  119. }
  120. /*
  121. * Disable CMCI on this CPU for all banks it owns when it goes down.
  122. * This allows other CPUs to claim the banks on rediscovery.
  123. */
  124. void cmci_clear(void)
  125. {
  126. unsigned long flags;
  127. int i;
  128. int banks;
  129. u64 val;
  130. if (!cmci_supported(&banks))
  131. return;
  132. spin_lock_irqsave(&cmci_discover_lock, flags);
  133. for (i = 0; i < banks; i++) {
  134. if (!test_bit(i, __get_cpu_var(mce_banks_owned)))
  135. continue;
  136. /* Disable CMCI */
  137. rdmsrl(MSR_IA32_MCx_CTL2(i), val);
  138. val &= ~(MCI_CTL2_CMCI_EN|MCI_CTL2_CMCI_THRESHOLD_MASK);
  139. wrmsrl(MSR_IA32_MCx_CTL2(i), val);
  140. __clear_bit(i, __get_cpu_var(mce_banks_owned));
  141. }
  142. spin_unlock_irqrestore(&cmci_discover_lock, flags);
  143. }
  144. /*
  145. * After a CPU went down cycle through all the others and rediscover
  146. * Must run in process context.
  147. */
  148. void cmci_rediscover(int dying)
  149. {
  150. int banks;
  151. int cpu;
  152. cpumask_var_t old;
  153. if (!cmci_supported(&banks))
  154. return;
  155. if (!alloc_cpumask_var(&old, GFP_KERNEL))
  156. return;
  157. cpumask_copy(old, &current->cpus_allowed);
  158. for_each_online_cpu(cpu) {
  159. if (cpu == dying)
  160. continue;
  161. if (set_cpus_allowed_ptr(current, cpumask_of(cpu)))
  162. continue;
  163. /* Recheck banks in case CPUs don't all have the same */
  164. if (cmci_supported(&banks))
  165. cmci_discover(banks, 0);
  166. }
  167. set_cpus_allowed_ptr(current, old);
  168. free_cpumask_var(old);
  169. }
  170. /*
  171. * Reenable CMCI on this CPU in case a CPU down failed.
  172. */
  173. void cmci_reenable(void)
  174. {
  175. int banks;
  176. if (cmci_supported(&banks))
  177. cmci_discover(banks, 0);
  178. }
  179. static void intel_init_cmci(void)
  180. {
  181. int banks;
  182. if (!cmci_supported(&banks))
  183. return;
  184. mce_threshold_vector = intel_threshold_interrupt;
  185. cmci_discover(banks, 1);
  186. /*
  187. * For CPU #0 this runs with still disabled APIC, but that's
  188. * ok because only the vector is set up. We still do another
  189. * check for the banks later for CPU #0 just to make sure
  190. * to not miss any events.
  191. */
  192. apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
  193. cmci_recheck();
  194. }
  195. void mce_intel_feature_init(struct cpuinfo_x86 *c)
  196. {
  197. intel_init_thermal(c);
  198. intel_init_cmci();
  199. }