mce_intel.c 5.3 KB

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