blob: 62b48e40920a2fe2445bc68ba562fcc2e4d8c987 [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09008#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/interrupt.h>
11#include <linux/percpu.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040012#include <linux/sched.h>
H. Peter Anvin1bf7b312009-06-17 08:31:15 -070013#include <asm/apic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <asm/processor.h>
15#include <asm/msr.h>
16#include <asm/mce.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Andi Kleen88ccbed2009-02-12 13:49:36 +010018/*
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.
23 */
24
25static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned);
26
27/*
28 * cmci_discover_lock protects against parallel discovery attempts
29 * which could race against each other.
30 */
31static DEFINE_SPINLOCK(cmci_discover_lock);
32
33#define CMCI_THRESHOLD 1
34
H. Peter Anvindf20e2e2009-02-24 13:19:02 -080035static int cmci_supported(int *banks)
Andi Kleen88ccbed2009-02-12 13:49:36 +010036{
37 u64 cap;
38
Hidetoshi Seto62fdac52009-06-11 16:06:07 +090039 if (mce_cmci_disabled || mce_ignore_ce)
40 return 0;
41
Andi Kleen88ccbed2009-02-12 13:49:36 +010042 /*
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.
46 */
47 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
48 return 0;
49 if (!cpu_has_apic || lapic_get_maxlvt() < 6)
50 return 0;
51 rdmsrl(MSR_IA32_MCG_CAP, cap);
52 *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
53 return !!(cap & MCG_CMCI_P);
54}
55
56/*
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.
61 */
62static void intel_threshold_interrupt(void)
63{
64 machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
Andi Kleen9ff36ee2009-05-27 21:56:58 +020065 mce_notify_irq();
Andi Kleen88ccbed2009-02-12 13:49:36 +010066}
67
68static void print_update(char *type, int *hdr, int num)
69{
70 if (*hdr == 0)
71 printk(KERN_INFO "CPU %d MCA banks", smp_processor_id());
72 *hdr = 1;
73 printk(KERN_CONT " %s:%d", type, num);
74}
75
76/*
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
79 * banks.
80 */
H. Peter Anvindf20e2e2009-02-24 13:19:02 -080081static void cmci_discover(int banks, int boot)
Andi Kleen88ccbed2009-02-12 13:49:36 +010082{
83 unsigned long *owned = (void *)&__get_cpu_var(mce_banks_owned);
Hidetoshi Setoe5299922009-05-08 17:28:40 +090084 unsigned long flags;
Andi Kleen88ccbed2009-02-12 13:49:36 +010085 int hdr = 0;
86 int i;
87
Hidetoshi Setoe5299922009-05-08 17:28:40 +090088 spin_lock_irqsave(&cmci_discover_lock, flags);
Andi Kleen88ccbed2009-02-12 13:49:36 +010089 for (i = 0; i < banks; i++) {
90 u64 val;
91
92 if (test_bit(i, owned))
93 continue;
94
Andi Kleena2d32bc2009-07-09 00:31:44 +020095 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
Andi Kleen88ccbed2009-02-12 13:49:36 +010096
97 /* Already owned by someone else? */
98 if (val & CMCI_EN) {
Mike Travis10fb7f12010-03-05 13:10:36 -060099 if (test_and_clear_bit(i, owned) && !boot)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100100 print_update("SHD", &hdr, i);
101 __clear_bit(i, __get_cpu_var(mce_poll_banks));
102 continue;
103 }
104
105 val |= CMCI_EN | CMCI_THRESHOLD;
Andi Kleena2d32bc2009-07-09 00:31:44 +0200106 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
107 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100108
109 /* Did the enable bit stick? -- the bank supports CMCI */
110 if (val & CMCI_EN) {
Mike Travis10fb7f12010-03-05 13:10:36 -0600111 if (!test_and_set_bit(i, owned) && !boot)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100112 print_update("CMCI", &hdr, i);
113 __clear_bit(i, __get_cpu_var(mce_poll_banks));
114 } else {
115 WARN_ON(!test_bit(i, __get_cpu_var(mce_poll_banks)));
116 }
117 }
Hidetoshi Setoe5299922009-05-08 17:28:40 +0900118 spin_unlock_irqrestore(&cmci_discover_lock, flags);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100119 if (hdr)
120 printk(KERN_CONT "\n");
121}
122
123/*
124 * Just in case we missed an event during initialization check
125 * all the CMCI owned banks.
126 */
H. Peter Anvindf20e2e2009-02-24 13:19:02 -0800127void cmci_recheck(void)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100128{
129 unsigned long flags;
130 int banks;
131
132 if (!mce_available(&current_cpu_data) || !cmci_supported(&banks))
133 return;
134 local_irq_save(flags);
135 machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
136 local_irq_restore(flags);
137}
138
139/*
140 * Disable CMCI on this CPU for all banks it owns when it goes down.
141 * This allows other CPUs to claim the banks on rediscovery.
142 */
H. Peter Anvindf20e2e2009-02-24 13:19:02 -0800143void cmci_clear(void)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100144{
Hidetoshi Setoe5299922009-05-08 17:28:40 +0900145 unsigned long flags;
Andi Kleen88ccbed2009-02-12 13:49:36 +0100146 int i;
147 int banks;
148 u64 val;
149
150 if (!cmci_supported(&banks))
151 return;
Hidetoshi Setoe5299922009-05-08 17:28:40 +0900152 spin_lock_irqsave(&cmci_discover_lock, flags);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100153 for (i = 0; i < banks; i++) {
154 if (!test_bit(i, __get_cpu_var(mce_banks_owned)))
155 continue;
156 /* Disable CMCI */
Andi Kleena2d32bc2009-07-09 00:31:44 +0200157 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100158 val &= ~(CMCI_EN|CMCI_THRESHOLD_MASK);
Andi Kleena2d32bc2009-07-09 00:31:44 +0200159 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100160 __clear_bit(i, __get_cpu_var(mce_banks_owned));
161 }
Hidetoshi Setoe5299922009-05-08 17:28:40 +0900162 spin_unlock_irqrestore(&cmci_discover_lock, flags);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100163}
164
165/*
166 * After a CPU went down cycle through all the others and rediscover
167 * Must run in process context.
168 */
H. Peter Anvindf20e2e2009-02-24 13:19:02 -0800169void cmci_rediscover(int dying)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100170{
171 int banks;
172 int cpu;
173 cpumask_var_t old;
174
175 if (!cmci_supported(&banks))
176 return;
177 if (!alloc_cpumask_var(&old, GFP_KERNEL))
178 return;
179 cpumask_copy(old, &current->cpus_allowed);
180
Hidetoshi Seto61a021a2009-04-14 17:09:04 +0900181 for_each_online_cpu(cpu) {
Andi Kleen88ccbed2009-02-12 13:49:36 +0100182 if (cpu == dying)
183 continue;
Rusty Russell4f062892009-03-13 14:49:54 +1030184 if (set_cpus_allowed_ptr(current, cpumask_of(cpu)))
Andi Kleen88ccbed2009-02-12 13:49:36 +0100185 continue;
186 /* Recheck banks in case CPUs don't all have the same */
187 if (cmci_supported(&banks))
188 cmci_discover(banks, 0);
189 }
190
191 set_cpus_allowed_ptr(current, old);
192 free_cpumask_var(old);
193}
194
195/*
196 * Reenable CMCI on this CPU in case a CPU down failed.
197 */
198void cmci_reenable(void)
199{
200 int banks;
201 if (cmci_supported(&banks))
202 cmci_discover(banks, 0);
203}
204
Hidetoshi Seto514ec492009-03-16 17:07:33 +0900205static void intel_init_cmci(void)
Andi Kleen88ccbed2009-02-12 13:49:36 +0100206{
207 int banks;
208
209 if (!cmci_supported(&banks))
210 return;
211
212 mce_threshold_vector = intel_threshold_interrupt;
213 cmci_discover(banks, 1);
214 /*
215 * For CPU #0 this runs with still disabled APIC, but that's
216 * ok because only the vector is set up. We still do another
217 * check for the banks later for CPU #0 just to make sure
218 * to not miss any events.
219 */
220 apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
221 cmci_recheck();
222}
223
H. Peter Anvincc3ca222009-02-20 23:35:51 -0800224void mce_intel_feature_init(struct cpuinfo_x86 *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 intel_init_thermal(c);
Andi Kleen88ccbed2009-02-12 13:49:36 +0100227 intel_init_cmci();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}