mce_intel_64.c 6.9 KB

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