mce_intel_64.c 6.9 KB

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