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 <asm/processor.h>
15 #include <asm/hw_irq.h>
17 #include <asm/therm_throt.h>
22 asmlinkage
void smp_thermal_interrupt(void)
31 rdmsrl(MSR_IA32_THERM_STATUS
, msr_val
);
32 if (therm_throt_process(msr_val
& 1))
33 mce_log_therm_throt_event(msr_val
);
35 inc_irq_stat(irq_thermal_count
);
40 * Support for Intel Correct Machine Check Interrupts. This allows
41 * the CPU to raise an interrupt when a corrected machine check happened.
42 * Normally we pick those up using a regular polling timer.
43 * Also supports reliable discovery of shared banks.
46 static DEFINE_PER_CPU(mce_banks_t
, mce_banks_owned
);
49 * cmci_discover_lock protects against parallel discovery attempts
50 * which could race against each other.
52 static DEFINE_SPINLOCK(cmci_discover_lock
);
54 #define CMCI_THRESHOLD 1
56 static int cmci_supported(int *banks
)
61 * Vendor check is not strictly needed, but the initial
62 * initialization is vendor keyed and this
63 * makes sure none of the backdoors are entered otherwise.
65 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
)
67 if (!cpu_has_apic
|| lapic_get_maxlvt() < 6)
69 rdmsrl(MSR_IA32_MCG_CAP
, cap
);
70 *banks
= min_t(unsigned, MAX_NR_BANKS
, cap
& 0xff);
71 return !!(cap
& MCG_CMCI_P
);
75 * The interrupt handler. This is called on every event.
76 * Just call the poller directly to log any events.
77 * This could in theory increase the threshold under high load,
78 * but doesn't for now.
80 static void intel_threshold_interrupt(void)
82 machine_check_poll(MCP_TIMESTAMP
, &__get_cpu_var(mce_banks_owned
));
86 static void print_update(char *type
, int *hdr
, int num
)
89 printk(KERN_INFO
"CPU %d MCA banks", smp_processor_id());
91 printk(KERN_CONT
" %s:%d", type
, num
);
95 * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
96 * on this CPU. Use the algorithm recommended in the SDM to discover shared
99 static void cmci_discover(int banks
, int boot
)
101 unsigned long *owned
= (void *)&__get_cpu_var(mce_banks_owned
);
106 spin_lock_irqsave(&cmci_discover_lock
, flags
);
107 for (i
= 0; i
< banks
; i
++) {
110 if (test_bit(i
, owned
))
113 rdmsrl(MSR_IA32_MC0_CTL2
+ i
, val
);
115 /* Already owned by someone else? */
117 if (test_and_clear_bit(i
, owned
) || boot
)
118 print_update("SHD", &hdr
, i
);
119 __clear_bit(i
, __get_cpu_var(mce_poll_banks
));
123 val
|= CMCI_EN
| CMCI_THRESHOLD
;
124 wrmsrl(MSR_IA32_MC0_CTL2
+ i
, val
);
125 rdmsrl(MSR_IA32_MC0_CTL2
+ i
, val
);
127 /* Did the enable bit stick? -- the bank supports CMCI */
129 if (!test_and_set_bit(i
, owned
) || boot
)
130 print_update("CMCI", &hdr
, i
);
131 __clear_bit(i
, __get_cpu_var(mce_poll_banks
));
133 WARN_ON(!test_bit(i
, __get_cpu_var(mce_poll_banks
)));
136 spin_unlock_irqrestore(&cmci_discover_lock
, flags
);
138 printk(KERN_CONT
"\n");
142 * Just in case we missed an event during initialization check
143 * all the CMCI owned banks.
145 void cmci_recheck(void)
150 if (!mce_available(¤t_cpu_data
) || !cmci_supported(&banks
))
152 local_irq_save(flags
);
153 machine_check_poll(MCP_TIMESTAMP
, &__get_cpu_var(mce_banks_owned
));
154 local_irq_restore(flags
);
158 * Disable CMCI on this CPU for all banks it owns when it goes down.
159 * This allows other CPUs to claim the banks on rediscovery.
161 void cmci_clear(void)
168 if (!cmci_supported(&banks
))
170 spin_lock_irqsave(&cmci_discover_lock
, flags
);
171 for (i
= 0; i
< banks
; i
++) {
172 if (!test_bit(i
, __get_cpu_var(mce_banks_owned
)))
175 rdmsrl(MSR_IA32_MC0_CTL2
+ i
, val
);
176 val
&= ~(CMCI_EN
|CMCI_THRESHOLD_MASK
);
177 wrmsrl(MSR_IA32_MC0_CTL2
+ i
, val
);
178 __clear_bit(i
, __get_cpu_var(mce_banks_owned
));
180 spin_unlock_irqrestore(&cmci_discover_lock
, flags
);
184 * After a CPU went down cycle through all the others and rediscover
185 * Must run in process context.
187 void cmci_rediscover(int dying
)
193 if (!cmci_supported(&banks
))
195 if (!alloc_cpumask_var(&old
, GFP_KERNEL
))
197 cpumask_copy(old
, ¤t
->cpus_allowed
);
199 for_each_online_cpu (cpu
) {
202 if (set_cpus_allowed_ptr(current
, cpumask_of(cpu
)))
204 /* Recheck banks in case CPUs don't all have the same */
205 if (cmci_supported(&banks
))
206 cmci_discover(banks
, 0);
209 set_cpus_allowed_ptr(current
, old
);
210 free_cpumask_var(old
);
214 * Reenable CMCI on this CPU in case a CPU down failed.
216 void cmci_reenable(void)
219 if (cmci_supported(&banks
))
220 cmci_discover(banks
, 0);
223 static void intel_init_cmci(void)
227 if (!cmci_supported(&banks
))
230 mce_threshold_vector
= intel_threshold_interrupt
;
231 cmci_discover(banks
, 1);
233 * For CPU #0 this runs with still disabled APIC, but that's
234 * ok because only the vector is set up. We still do another
235 * check for the banks later for CPU #0 just to make sure
236 * to not miss any events.
238 apic_write(APIC_LVTCMCI
, THRESHOLD_APIC_VECTOR
|APIC_DM_FIXED
);
242 void mce_intel_feature_init(struct cpuinfo_x86
*c
)
244 intel_init_thermal(c
);