2 * Intel specific MCE features.
3 * Copyright 2004 Zwane Mwaikambo <zwane@linuxpower.ca>
4 * Copyright (C) 2008, 2009 Intel Corporation
9 #include <linux/init.h>
10 #include <linux/interrupt.h>
11 #include <linux/percpu.h>
12 #include <linux/sched.h>
14 #include <asm/processor.h>
19 * Support for Intel Correct Machine Check Interrupts. This allows
20 * the CPU to raise an interrupt when a corrected machine check happened.
21 * Normally we pick those up using a regular polling timer.
22 * Also supports reliable discovery of shared banks.
25 static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned);
28 * cmci_discover_lock protects against parallel discovery attempts
29 * which could race against each other.
31 static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
33 #define CMCI_THRESHOLD 1
35 static int cmci_supported(int *banks)
39 if (mce_cmci_disabled || mce_ignore_ce)
43 * Vendor check is not strictly needed, but the initial
44 * initialization is vendor keyed and this
45 * makes sure none of the backdoors are entered otherwise.
47 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
49 if (!cpu_has_apic || lapic_get_maxlvt() < 6)
51 rdmsrl(MSR_IA32_MCG_CAP, cap);
52 *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
53 return !!(cap & MCG_CMCI_P);
57 * The interrupt handler. This is called on every event.
58 * Just call the poller directly to log any events.
59 * This could in theory increase the threshold under high load,
60 * but doesn't for now.
62 static void intel_threshold_interrupt(void)
64 machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
68 static void print_update(char *type, int *hdr, int num)
71 printk(KERN_INFO "CPU %d MCA banks", smp_processor_id());
73 printk(KERN_CONT " %s:%d", type, num);
77 * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
78 * on this CPU. Use the algorithm recommended in the SDM to discover shared
81 static void cmci_discover(int banks, int boot)
83 unsigned long *owned = (void *)&__get_cpu_var(mce_banks_owned);
88 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
89 for (i = 0; i < banks; i++) {
92 if (test_bit(i, owned))
95 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
97 /* Already owned by someone else? */
98 if (val & MCI_CTL2_CMCI_EN) {
99 if (test_and_clear_bit(i, owned) && !boot)
100 print_update("SHD", &hdr, i);
101 __clear_bit(i, __get_cpu_var(mce_poll_banks));
105 val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
106 val |= MCI_CTL2_CMCI_EN | CMCI_THRESHOLD;
107 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
108 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
110 /* Did the enable bit stick? -- the bank supports CMCI */
111 if (val & MCI_CTL2_CMCI_EN) {
112 if (!test_and_set_bit(i, owned) && !boot)
113 print_update("CMCI", &hdr, i);
114 __clear_bit(i, __get_cpu_var(mce_poll_banks));
116 WARN_ON(!test_bit(i, __get_cpu_var(mce_poll_banks)));
119 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
121 printk(KERN_CONT "\n");
125 * Just in case we missed an event during initialization check
126 * all the CMCI owned banks.
128 void cmci_recheck(void)
133 if (!mce_available(__this_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
135 local_irq_save(flags);
136 machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
137 local_irq_restore(flags);
141 * Disable CMCI on this CPU for all banks it owns when it goes down.
142 * This allows other CPUs to claim the banks on rediscovery.
144 void cmci_clear(void)
151 if (!cmci_supported(&banks))
153 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
154 for (i = 0; i < banks; i++) {
155 if (!test_bit(i, __get_cpu_var(mce_banks_owned)))
158 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
159 val &= ~(MCI_CTL2_CMCI_EN|MCI_CTL2_CMCI_THRESHOLD_MASK);
160 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
161 __clear_bit(i, __get_cpu_var(mce_banks_owned));
163 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
167 * After a CPU went down cycle through all the others and rediscover
168 * Must run in process context.
170 void cmci_rediscover(int dying)
176 if (!cmci_supported(&banks))
178 if (!alloc_cpumask_var(&old, GFP_KERNEL))
180 cpumask_copy(old, ¤t->cpus_allowed);
182 for_each_online_cpu(cpu) {
185 if (set_cpus_allowed_ptr(current, cpumask_of(cpu)))
187 /* Recheck banks in case CPUs don't all have the same */
188 if (cmci_supported(&banks))
189 cmci_discover(banks, 0);
192 set_cpus_allowed_ptr(current, old);
193 free_cpumask_var(old);
197 * Reenable CMCI on this CPU in case a CPU down failed.
199 void cmci_reenable(void)
202 if (cmci_supported(&banks))
203 cmci_discover(banks, 0);
206 static void intel_init_cmci(void)
210 if (!cmci_supported(&banks))
213 mce_threshold_vector = intel_threshold_interrupt;
214 cmci_discover(banks, 1);
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.
221 apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
225 void mce_intel_feature_init(struct cpuinfo_x86 *c)
227 intel_init_thermal(c);