mce_intel_64.c 5.7 KB

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