mce_intel.c 5.4 KB

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