2 * Intel specific MCE features.
3 * Copyright 2004 Zwane Mwaikambo <zwane@linuxpower.ca>
4 * Copyright (C) 2008, 2009 Intel Corporation
9 #include <linux/interrupt.h>
10 #include <linux/percpu.h>
11 #include <linux/sched.h>
12 #include <linux/cpumask.h>
14 #include <asm/processor.h>
18 #include "mce-internal.h"
21 * Support for Intel Correct Machine Check Interrupts. This allows
22 * the CPU to raise an interrupt when a corrected machine check happened.
23 * Normally we pick those up using a regular polling timer.
24 * Also supports reliable discovery of shared banks.
28 * CMCI can be delivered to multiple cpus that share a machine check bank
29 * so we need to designate a single cpu to process errors logged in each bank
30 * in the interrupt handler (otherwise we would have many races and potential
31 * double reporting of the same error).
32 * Note that this can change when a cpu is offlined or brought online since
33 * some MCA banks are shared across cpus. When a cpu is offlined, cmci_clear()
34 * disables CMCI on all banks owned by the cpu and clears this bitfield. At
35 * this point, cmci_rediscover() kicks in and a different cpu may end up
36 * taking ownership of some of the shared MCA banks that were previously
37 * owned by the offlined cpu.
39 static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned);
42 * CMCI storm detection backoff counter
44 * During storm, we reset this counter to INITIAL_CHECK_INTERVAL in case we've
45 * encountered an error. If not, we decrement it by one. We signal the end of
46 * the CMCI storm when it reaches 0.
48 static DEFINE_PER_CPU(int, cmci_backoff_cnt);
51 * cmci_discover_lock protects against parallel discovery attempts
52 * which could race against each other.
54 static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
56 #define CMCI_THRESHOLD 1
57 #define CMCI_POLL_INTERVAL (30 * HZ)
58 #define CMCI_STORM_INTERVAL (HZ)
59 #define CMCI_STORM_THRESHOLD 15
61 static DEFINE_PER_CPU(unsigned long, cmci_time_stamp);
62 static DEFINE_PER_CPU(unsigned int, cmci_storm_cnt);
63 static DEFINE_PER_CPU(unsigned int, cmci_storm_state);
71 static atomic_t cmci_storm_on_cpus;
73 static int cmci_supported(int *banks)
77 if (mca_cfg.cmci_disabled || mca_cfg.ignore_ce)
81 * Vendor check is not strictly needed, but the initial
82 * initialization is vendor keyed and this
83 * makes sure none of the backdoors are entered otherwise.
85 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
87 if (!cpu_has_apic || lapic_get_maxlvt() < 6)
89 rdmsrl(MSR_IA32_MCG_CAP, cap);
90 *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
91 return !!(cap & MCG_CMCI_P);
94 static bool lmce_supported(void)
98 if (mca_cfg.lmce_disabled)
101 rdmsrl(MSR_IA32_MCG_CAP, tmp);
104 * LMCE depends on recovery support in the processor. Hence both
105 * MCG_SER_P and MCG_LMCE_P should be present in MCG_CAP.
107 if ((tmp & (MCG_SER_P | MCG_LMCE_P)) !=
108 (MCG_SER_P | MCG_LMCE_P))
112 * BIOS should indicate support for LMCE by setting bit 20 in
113 * IA32_FEATURE_CONTROL without which touching MCG_EXT_CTL will
114 * generate a #GP fault.
116 rdmsrl(MSR_IA32_FEATURE_CONTROL, tmp);
117 if ((tmp & (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_LMCE)) ==
118 (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_LMCE))
124 bool mce_intel_cmci_poll(void)
126 if (__this_cpu_read(cmci_storm_state) == CMCI_STORM_NONE)
130 * Reset the counter if we've logged an error in the last poll
133 if (machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned)))
134 this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
136 this_cpu_dec(cmci_backoff_cnt);
141 void mce_intel_hcpu_update(unsigned long cpu)
143 if (per_cpu(cmci_storm_state, cpu) == CMCI_STORM_ACTIVE)
144 atomic_dec(&cmci_storm_on_cpus);
146 per_cpu(cmci_storm_state, cpu) = CMCI_STORM_NONE;
149 static void cmci_toggle_interrupt_mode(bool on)
151 unsigned long flags, *owned;
155 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
156 owned = this_cpu_ptr(mce_banks_owned);
157 for_each_set_bit(bank, owned, MAX_NR_BANKS) {
158 rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
161 val |= MCI_CTL2_CMCI_EN;
163 val &= ~MCI_CTL2_CMCI_EN;
165 wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
167 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
170 unsigned long cmci_intel_adjust_timer(unsigned long interval)
172 if ((this_cpu_read(cmci_backoff_cnt) > 0) &&
173 (__this_cpu_read(cmci_storm_state) == CMCI_STORM_ACTIVE)) {
175 return CMCI_STORM_INTERVAL;
178 switch (__this_cpu_read(cmci_storm_state)) {
179 case CMCI_STORM_ACTIVE:
182 * We switch back to interrupt mode once the poll timer has
183 * silenced itself. That means no events recorded and the timer
184 * interval is back to our poll interval.
186 __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED);
187 if (!atomic_sub_return(1, &cmci_storm_on_cpus))
188 pr_notice("CMCI storm subsided: switching to interrupt mode\n");
192 case CMCI_STORM_SUBSIDED:
194 * We wait for all CPUs to go back to SUBSIDED state. When that
195 * happens we switch back to interrupt mode.
197 if (!atomic_read(&cmci_storm_on_cpus)) {
198 __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE);
199 cmci_toggle_interrupt_mode(true);
202 return CMCI_POLL_INTERVAL;
205 /* We have shiny weather. Let the poll do whatever it thinks. */
210 static bool cmci_storm_detect(void)
212 unsigned int cnt = __this_cpu_read(cmci_storm_cnt);
213 unsigned long ts = __this_cpu_read(cmci_time_stamp);
214 unsigned long now = jiffies;
217 if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE)
220 if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) {
224 __this_cpu_write(cmci_time_stamp, now);
226 __this_cpu_write(cmci_storm_cnt, cnt);
228 if (cnt <= CMCI_STORM_THRESHOLD)
231 cmci_toggle_interrupt_mode(false);
232 __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE);
233 r = atomic_add_return(1, &cmci_storm_on_cpus);
234 mce_timer_kick(CMCI_STORM_INTERVAL);
235 this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
238 pr_notice("CMCI storm detected: switching to poll mode\n");
243 * The interrupt handler. This is called on every event.
244 * Just call the poller directly to log any events.
245 * This could in theory increase the threshold under high load,
246 * but doesn't for now.
248 static void intel_threshold_interrupt(void)
250 if (cmci_storm_detect())
253 machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
257 * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
258 * on this CPU. Use the algorithm recommended in the SDM to discover shared
261 static void cmci_discover(int banks)
263 unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned);
266 int bios_wrong_thresh = 0;
268 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
269 for (i = 0; i < banks; i++) {
271 int bios_zero_thresh = 0;
273 if (test_bit(i, owned))
276 /* Skip banks in firmware first mode */
277 if (test_bit(i, mce_banks_ce_disabled))
280 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
282 /* Already owned by someone else? */
283 if (val & MCI_CTL2_CMCI_EN) {
285 __clear_bit(i, this_cpu_ptr(mce_poll_banks));
289 if (!mca_cfg.bios_cmci_threshold) {
290 val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
291 val |= CMCI_THRESHOLD;
292 } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
294 * If bios_cmci_threshold boot option was specified
295 * but the threshold is zero, we'll try to initialize
298 bios_zero_thresh = 1;
299 val |= CMCI_THRESHOLD;
302 val |= MCI_CTL2_CMCI_EN;
303 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
304 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
306 /* Did the enable bit stick? -- the bank supports CMCI */
307 if (val & MCI_CTL2_CMCI_EN) {
309 __clear_bit(i, this_cpu_ptr(mce_poll_banks));
311 * We are able to set thresholds for some banks that
312 * had a threshold of 0. This means the BIOS has not
313 * set the thresholds properly or does not work with
314 * this boot option. Note down now and report later.
316 if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
317 (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
318 bios_wrong_thresh = 1;
320 WARN_ON(!test_bit(i, this_cpu_ptr(mce_poll_banks)));
323 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
324 if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
326 "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
328 "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
333 * Just in case we missed an event during initialization check
334 * all the CMCI owned banks.
336 void cmci_recheck(void)
341 if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
344 local_irq_save(flags);
345 machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
346 local_irq_restore(flags);
349 /* Caller must hold the lock on cmci_discover_lock */
350 static void __cmci_disable_bank(int bank)
354 if (!test_bit(bank, this_cpu_ptr(mce_banks_owned)))
356 rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
357 val &= ~MCI_CTL2_CMCI_EN;
358 wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
359 __clear_bit(bank, this_cpu_ptr(mce_banks_owned));
363 * Disable CMCI on this CPU for all banks it owns when it goes down.
364 * This allows other CPUs to claim the banks on rediscovery.
366 void cmci_clear(void)
372 if (!cmci_supported(&banks))
374 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
375 for (i = 0; i < banks; i++)
376 __cmci_disable_bank(i);
377 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
380 static void cmci_rediscover_work_func(void *arg)
384 /* Recheck banks in case CPUs don't all have the same */
385 if (cmci_supported(&banks))
386 cmci_discover(banks);
389 /* After a CPU went down cycle through all the others and rediscover */
390 void cmci_rediscover(void)
394 if (!cmci_supported(&banks))
397 on_each_cpu(cmci_rediscover_work_func, NULL, 1);
401 * Reenable CMCI on this CPU in case a CPU down failed.
403 void cmci_reenable(void)
406 if (cmci_supported(&banks))
407 cmci_discover(banks);
410 void cmci_disable_bank(int bank)
415 if (!cmci_supported(&banks))
418 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
419 __cmci_disable_bank(bank);
420 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
423 static void intel_init_cmci(void)
427 if (!cmci_supported(&banks))
430 mce_threshold_vector = intel_threshold_interrupt;
431 cmci_discover(banks);
433 * For CPU #0 this runs with still disabled APIC, but that's
434 * ok because only the vector is set up. We still do another
435 * check for the banks later for CPU #0 just to make sure
436 * to not miss any events.
438 apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
442 static void intel_init_lmce(void)
446 if (!lmce_supported())
449 rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
451 if (!(val & MCG_EXT_CTL_LMCE_EN))
452 wrmsrl(MSR_IA32_MCG_EXT_CTL, val | MCG_EXT_CTL_LMCE_EN);
455 static void intel_clear_lmce(void)
459 if (!lmce_supported())
462 rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
463 val &= ~MCG_EXT_CTL_LMCE_EN;
464 wrmsrl(MSR_IA32_MCG_EXT_CTL, val);
467 void mce_intel_feature_init(struct cpuinfo_x86 *c)
469 intel_init_thermal(c);
474 void mce_intel_feature_clear(struct cpuinfo_x86 *c)