mce_intel.c 5.3 KB

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