mce_intel_64.c 6.2 KB

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