mce_intel_64.c 5.3 KB

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