blob: d15df6e49bf02936be5f51854040efbf7a7e89cf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Intel specific MCE features.
3 * Copyright 2004 Zwane Mwaikambo <zwane@linuxpower.ca>
Andi Kleen88ccbed2009-02-12 13:49:36 +01004 * Copyright (C) 2008, 2009 Intel Corporation
5 * Author: Andi Kleen
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
8#include <linux/init.h>
9#include <linux/interrupt.h>
10#include <linux/percpu.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040011#include <linux/sched.h>
H. Peter Anvin1bf7b312009-06-17 08:31:15 -070012#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/processor.h>
14#include <asm/msr.h>
15#include <asm/mce.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Andi Kleen88ccbed2009-02-12 13:49:36 +010017/*
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.
22 */
23
24static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned);
25
26/*
27 * cmci_discover_lock protects against parallel discovery attempts
28 * which could race against each other.
29 */
30static DEFINE_SPINLOCK(cmci_discover_lock);
31
32#define CMCI_THRESHOLD 1
33
H. Peter Anvindf20e2e2009-02-24 13:19:02 -080034static int cmci_supported(int *banks)
Andi Kleen88ccbed2009-02-12 13:49:36 +010035{
36 u64 cap;
37
Hidetoshi Seto62fdac52009-06-11 16:06:07 +090038 if (mce_cmci_disabled || mce_ignore_ce)
39 return 0;
40
Andi Kleen88ccbed2009-02-12 13:49:36 +010041 /*
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.
45 */
46 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
47 return 0;
48 if (!cpu_has_apic || lapic_get_maxlvt() < 6)
49 return 0;
50 rdmsrl(MSR_IA32_MCG_CAP, cap);
51 *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
52 return !!(cap & MCG_CMCI_P);
53}
54
55/*
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.
60 */
61static void intel_threshold_interrupt(void)
62{
63 machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
Andi Kleen9ff36ee2009-05-27 21:56:58 +020064 mce_notify_irq();
Andi Kleen88ccbed2009-02-12 13:49:36 +010065}
66
67static void print_update(char *type, int *hdr, int num)
68{
69 if (*hdr == 0)
70 printk(KERN_INFO "CPU %d MCA banks", smp_processor_id());
71 *hdr = 1;
72 printk(KERN_CONT " %s:%d", type, num);
73}
74
75/*
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
78 * banks.
79 */
H. Peter Anvindf20e2e2009-02-24 13:19:02 -080080static void cmci_discover(int banks, int boot)
Andi Kleen88ccbed2009-02-12 13:49:36 +010081{
82 unsigned long *owned = (void *)&__get_cpu_var(mce_banks_owned);
Hidetoshi Setoe5299922009-05-08 17:28:40 +090083 unsigned long flags;
Andi Kleen88ccbed2009-02-12 13:49:36 +010084 int hdr = 0;
85 int i;
86
Hidetoshi Setoe5299922009-05-08 17:28:40 +090087 spin_lock_irqsave(&cmci_discover_lock, flags);
Andi Kleen88ccbed2009-02-12 13:49:36 +010088 for (i = 0; i < banks; i++) {
89 u64 val;
90
91 if (test_bit(i, owned))
92 continue;
93
Andi Kleena2d32bc2009-07-09 00:31:44 +020094 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
Andi Kleen88ccbed2009-02-12 13:49:36 +010095
96 /* Already owned by someone else? */
97 if (val & CMCI_EN) {
Mike Travis10fb7f12010-03-05 13:10:36 -060098 if (test_and_clear_bit(i, owned) && !boot)
Andi Kleen88ccbed2009-02-12 13:49:36 +010099 print_update("SHD", &hdr, i);
100 __clear_bit(i, __get_cpu_var(mce_poll_banks));
101 continue;
102 }
103
104 val |= CMCI_EN | CMCI_THRESHOLD;
Andi Kleena2d32bc2009-07-09 00:31:44 +0200105 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
106 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100107
108 /* Did the enable bit stick? -- the bank supports CMCI */
109 if (val & CMCI_EN) {
Mike Travis10fb7f12010-03-05 13:10:36 -0600110 if (!test_and_set_bit(i, owned) && !boot)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100111 print_update("CMCI", &hdr, i);
112 __clear_bit(i, __get_cpu_var(mce_poll_banks));
113 } else {
114 WARN_ON(!test_bit(i, __get_cpu_var(mce_poll_banks)));
115 }
116 }
Hidetoshi Setoe5299922009-05-08 17:28:40 +0900117 spin_unlock_irqrestore(&cmci_discover_lock, flags);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100118 if (hdr)
119 printk(KERN_CONT "\n");
120}
121
122/*
123 * Just in case we missed an event during initialization check
124 * all the CMCI owned banks.
125 */
H. Peter Anvindf20e2e2009-02-24 13:19:02 -0800126void cmci_recheck(void)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100127{
128 unsigned long flags;
129 int banks;
130
131 if (!mce_available(&current_cpu_data) || !cmci_supported(&banks))
132 return;
133 local_irq_save(flags);
134 machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
135 local_irq_restore(flags);
136}
137
138/*
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.
141 */
H. Peter Anvindf20e2e2009-02-24 13:19:02 -0800142void cmci_clear(void)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100143{
Hidetoshi Setoe5299922009-05-08 17:28:40 +0900144 unsigned long flags;
Andi Kleen88ccbed2009-02-12 13:49:36 +0100145 int i;
146 int banks;
147 u64 val;
148
149 if (!cmci_supported(&banks))
150 return;
Hidetoshi Setoe5299922009-05-08 17:28:40 +0900151 spin_lock_irqsave(&cmci_discover_lock, flags);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100152 for (i = 0; i < banks; i++) {
153 if (!test_bit(i, __get_cpu_var(mce_banks_owned)))
154 continue;
155 /* Disable CMCI */
Andi Kleena2d32bc2009-07-09 00:31:44 +0200156 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100157 val &= ~(CMCI_EN|CMCI_THRESHOLD_MASK);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200158 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100159 __clear_bit(i, __get_cpu_var(mce_banks_owned));
160 }
Hidetoshi Setoe5299922009-05-08 17:28:40 +0900161 spin_unlock_irqrestore(&cmci_discover_lock, flags);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100162}
163
164/*
165 * After a CPU went down cycle through all the others and rediscover
166 * Must run in process context.
167 */
H. Peter Anvindf20e2e2009-02-24 13:19:02 -0800168void cmci_rediscover(int dying)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100169{
170 int banks;
171 int cpu;
172 cpumask_var_t old;
173
174 if (!cmci_supported(&banks))
175 return;
176 if (!alloc_cpumask_var(&old, GFP_KERNEL))
177 return;
178 cpumask_copy(old, &current->cpus_allowed);
179
Hidetoshi Seto61a021a2009-04-14 17:09:04 +0900180 for_each_online_cpu(cpu) {
Andi Kleen88ccbed2009-02-12 13:49:36 +0100181 if (cpu == dying)
182 continue;
Rusty Russell4f062892009-03-13 14:49:54 +1030183 if (set_cpus_allowed_ptr(current, cpumask_of(cpu)))
Andi Kleen88ccbed2009-02-12 13:49:36 +0100184 continue;
185 /* Recheck banks in case CPUs don't all have the same */
186 if (cmci_supported(&banks))
187 cmci_discover(banks, 0);
188 }
189
190 set_cpus_allowed_ptr(current, old);
191 free_cpumask_var(old);
192}
193
194/*
195 * Reenable CMCI on this CPU in case a CPU down failed.
196 */
197void cmci_reenable(void)
198{
199 int banks;
200 if (cmci_supported(&banks))
201 cmci_discover(banks, 0);
202}
203
Hidetoshi Seto514ec492009-03-16 17:07:33 +0900204static void intel_init_cmci(void)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100205{
206 int banks;
207
208 if (!cmci_supported(&banks))
209 return;
210
211 mce_threshold_vector = intel_threshold_interrupt;
212 cmci_discover(banks, 1);
213 /*
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.
218 */
219 apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
220 cmci_recheck();
221}
222
H. Peter Anvincc3ca222009-02-20 23:35:51 -0800223void mce_intel_feature_init(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 intel_init_thermal(c);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100226 intel_init_cmci();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}