mce_intel_64.c 5.7 KB

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