2 * Intel specific MCE features.
3 * Copyright 2004 Zwane Mwaikambo <zwane@linuxpower.ca>
4 * Copyright (C) 2008, 2009 Intel Corporation
8 #include <linux/init.h>
9 #include <linux/interrupt.h>
10 #include <linux/percpu.h>
11 #include <linux/sched.h>
13 #include <asm/processor.h>
18 * Support for Intel Correct Machine Check Interrupts. This allows
19 * the CPU to raise an interrupt when a corrected machine check happened.
20 * Normally we pick those up using a regular polling timer.
21 * Also supports reliable discovery of shared banks.
24 static DEFINE_PER_CPU(mce_banks_t
, mce_banks_owned
);
27 * cmci_discover_lock protects against parallel discovery attempts
28 * which could race against each other.
30 static DEFINE_SPINLOCK(cmci_discover_lock
);
32 #define CMCI_THRESHOLD 1
34 static int cmci_supported(int *banks
)
38 if (mce_cmci_disabled
|| mce_ignore_ce
)
42 * Vendor check is not strictly needed, but the initial
43 * initialization is vendor keyed and this
44 * makes sure none of the backdoors are entered otherwise.
46 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
)
48 if (!cpu_has_apic
|| lapic_get_maxlvt() < 6)
50 rdmsrl(MSR_IA32_MCG_CAP
, cap
);
51 *banks
= min_t(unsigned, MAX_NR_BANKS
, cap
& 0xff);
52 return !!(cap
& MCG_CMCI_P
);
56 * The interrupt handler. This is called on every event.
57 * Just call the poller directly to log any events.
58 * This could in theory increase the threshold under high load,
59 * but doesn't for now.
61 static void intel_threshold_interrupt(void)
63 machine_check_poll(MCP_TIMESTAMP
, &__get_cpu_var(mce_banks_owned
));
67 static void print_update(char *type
, int *hdr
, int num
)
70 printk(KERN_INFO
"CPU %d MCA banks", smp_processor_id());
72 printk(KERN_CONT
" %s:%d", type
, num
);
76 * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
77 * on this CPU. Use the algorithm recommended in the SDM to discover shared
80 static void cmci_discover(int banks
, int boot
)
82 unsigned long *owned
= (void *)&__get_cpu_var(mce_banks_owned
);
87 spin_lock_irqsave(&cmci_discover_lock
, flags
);
88 for (i
= 0; i
< banks
; i
++) {
91 if (test_bit(i
, owned
))
94 rdmsrl(MSR_IA32_MCx_CTL2(i
), val
);
96 /* Already owned by someone else? */
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
));
104 val
|= CMCI_EN
| CMCI_THRESHOLD
;
105 wrmsrl(MSR_IA32_MCx_CTL2(i
), val
);
106 rdmsrl(MSR_IA32_MCx_CTL2(i
), val
);
108 /* Did the enable bit stick? -- the bank supports CMCI */
110 if (!test_and_set_bit(i
, owned
) || boot
)
111 print_update("CMCI", &hdr
, i
);
112 __clear_bit(i
, __get_cpu_var(mce_poll_banks
));
114 WARN_ON(!test_bit(i
, __get_cpu_var(mce_poll_banks
)));
117 spin_unlock_irqrestore(&cmci_discover_lock
, flags
);
119 printk(KERN_CONT
"\n");
123 * Just in case we missed an event during initialization check
124 * all the CMCI owned banks.
126 void cmci_recheck(void)
131 if (!mce_available(¤t_cpu_data
) || !cmci_supported(&banks
))
133 local_irq_save(flags
);
134 machine_check_poll(MCP_TIMESTAMP
, &__get_cpu_var(mce_banks_owned
));
135 local_irq_restore(flags
);
139 * Disable CMCI on this CPU for all banks it owns when it goes down.
140 * This allows other CPUs to claim the banks on rediscovery.
142 void cmci_clear(void)
149 if (!cmci_supported(&banks
))
151 spin_lock_irqsave(&cmci_discover_lock
, flags
);
152 for (i
= 0; i
< banks
; i
++) {
153 if (!test_bit(i
, __get_cpu_var(mce_banks_owned
)))
156 rdmsrl(MSR_IA32_MCx_CTL2(i
), val
);
157 val
&= ~(CMCI_EN
|CMCI_THRESHOLD_MASK
);
158 wrmsrl(MSR_IA32_MCx_CTL2(i
), val
);
159 __clear_bit(i
, __get_cpu_var(mce_banks_owned
));
161 spin_unlock_irqrestore(&cmci_discover_lock
, flags
);
165 * After a CPU went down cycle through all the others and rediscover
166 * Must run in process context.
168 void cmci_rediscover(int dying
)
174 if (!cmci_supported(&banks
))
176 if (!alloc_cpumask_var(&old
, GFP_KERNEL
))
178 cpumask_copy(old
, ¤t
->cpus_allowed
);
180 for_each_online_cpu(cpu
) {
183 if (set_cpus_allowed_ptr(current
, cpumask_of(cpu
)))
185 /* Recheck banks in case CPUs don't all have the same */
186 if (cmci_supported(&banks
))
187 cmci_discover(banks
, 0);
190 set_cpus_allowed_ptr(current
, old
);
191 free_cpumask_var(old
);
195 * Reenable CMCI on this CPU in case a CPU down failed.
197 void cmci_reenable(void)
200 if (cmci_supported(&banks
))
201 cmci_discover(banks
, 0);
204 static void intel_init_cmci(void)
208 if (!cmci_supported(&banks
))
211 mce_threshold_vector
= intel_threshold_interrupt
;
212 cmci_discover(banks
, 1);
214 * For CPU #0 this runs with still disabled APIC, but that's
215 * ok because only the vector is set up. We still do another
216 * check for the banks later for CPU #0 just to make sure
217 * to not miss any events.
219 apic_write(APIC_LVTCMCI
, THRESHOLD_APIC_VECTOR
|APIC_DM_FIXED
);
223 void mce_intel_feature_init(struct cpuinfo_x86
*c
)
225 intel_init_thermal(c
);