Merge branch 'tunnels'
[linux.git] / arch / x86 / kernel / cpu / mcheck / mce_intel.c
1 /*
2  * Intel specific MCE features.
3  * Copyright 2004 Zwane Mwaikambo <zwane@linuxpower.ca>
4  * Copyright (C) 2008, 2009 Intel Corporation
5  * Author: Andi Kleen
6  */
7
8 #include <linux/gfp.h>
9 #include <linux/interrupt.h>
10 #include <linux/percpu.h>
11 #include <linux/sched.h>
12 #include <asm/apic.h>
13 #include <asm/processor.h>
14 #include <asm/msr.h>
15 #include <asm/mce.h>
16
17 #include "mce-internal.h"
18
19 /*
20  * Support for Intel Correct Machine Check Interrupts. This allows
21  * the CPU to raise an interrupt when a corrected machine check happened.
22  * Normally we pick those up using a regular polling timer.
23  * Also supports reliable discovery of shared banks.
24  */
25
26 /*
27  * CMCI can be delivered to multiple cpus that share a machine check bank
28  * so we need to designate a single cpu to process errors logged in each bank
29  * in the interrupt handler (otherwise we would have many races and potential
30  * double reporting of the same error).
31  * Note that this can change when a cpu is offlined or brought online since
32  * some MCA banks are shared across cpus. When a cpu is offlined, cmci_clear()
33  * disables CMCI on all banks owned by the cpu and clears this bitfield. At
34  * this point, cmci_rediscover() kicks in and a different cpu may end up
35  * taking ownership of some of the shared MCA banks that were previously
36  * owned by the offlined cpu.
37  */
38 static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned);
39
40 /*
41  * cmci_discover_lock protects against parallel discovery attempts
42  * which could race against each other.
43  */
44 static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
45
46 #define CMCI_THRESHOLD          1
47 #define CMCI_POLL_INTERVAL      (30 * HZ)
48 #define CMCI_STORM_INTERVAL     (1 * HZ)
49 #define CMCI_STORM_THRESHOLD    15
50
51 static DEFINE_PER_CPU(unsigned long, cmci_time_stamp);
52 static DEFINE_PER_CPU(unsigned int, cmci_storm_cnt);
53 static DEFINE_PER_CPU(unsigned int, cmci_storm_state);
54
55 enum {
56         CMCI_STORM_NONE,
57         CMCI_STORM_ACTIVE,
58         CMCI_STORM_SUBSIDED,
59 };
60
61 static atomic_t cmci_storm_on_cpus;
62
63 static int cmci_supported(int *banks)
64 {
65         u64 cap;
66
67         if (mca_cfg.cmci_disabled || mca_cfg.ignore_ce)
68                 return 0;
69
70         /*
71          * Vendor check is not strictly needed, but the initial
72          * initialization is vendor keyed and this
73          * makes sure none of the backdoors are entered otherwise.
74          */
75         if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL)
76                 return 0;
77         if (!cpu_has_apic || lapic_get_maxlvt() < 6)
78                 return 0;
79         rdmsrl(MSR_IA32_MCG_CAP, cap);
80         *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
81         return !!(cap & MCG_CMCI_P);
82 }
83
84 void mce_intel_cmci_poll(void)
85 {
86         if (__this_cpu_read(cmci_storm_state) == CMCI_STORM_NONE)
87                 return;
88         machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
89 }
90
91 void mce_intel_hcpu_update(unsigned long cpu)
92 {
93         if (per_cpu(cmci_storm_state, cpu) == CMCI_STORM_ACTIVE)
94                 atomic_dec(&cmci_storm_on_cpus);
95
96         per_cpu(cmci_storm_state, cpu) = CMCI_STORM_NONE;
97 }
98
99 unsigned long mce_intel_adjust_timer(unsigned long interval)
100 {
101         int r;
102
103         if (interval < CMCI_POLL_INTERVAL)
104                 return interval;
105
106         switch (__this_cpu_read(cmci_storm_state)) {
107         case CMCI_STORM_ACTIVE:
108                 /*
109                  * We switch back to interrupt mode once the poll timer has
110                  * silenced itself. That means no events recorded and the
111                  * timer interval is back to our poll interval.
112                  */
113                 __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED);
114                 r = atomic_sub_return(1, &cmci_storm_on_cpus);
115                 if (r == 0)
116                         pr_notice("CMCI storm subsided: switching to interrupt mode\n");
117                 /* FALLTHROUGH */
118
119         case CMCI_STORM_SUBSIDED:
120                 /*
121                  * We wait for all cpus to go back to SUBSIDED
122                  * state. When that happens we switch back to
123                  * interrupt mode.
124                  */
125                 if (!atomic_read(&cmci_storm_on_cpus)) {
126                         __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE);
127                         cmci_reenable();
128                         cmci_recheck();
129                 }
130                 return CMCI_POLL_INTERVAL;
131         default:
132                 /*
133                  * We have shiny weather. Let the poll do whatever it
134                  * thinks.
135                  */
136                 return interval;
137         }
138 }
139
140 static bool cmci_storm_detect(void)
141 {
142         unsigned int cnt = __this_cpu_read(cmci_storm_cnt);
143         unsigned long ts = __this_cpu_read(cmci_time_stamp);
144         unsigned long now = jiffies;
145         int r;
146
147         if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE)
148                 return true;
149
150         if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) {
151                 cnt++;
152         } else {
153                 cnt = 1;
154                 __this_cpu_write(cmci_time_stamp, now);
155         }
156         __this_cpu_write(cmci_storm_cnt, cnt);
157
158         if (cnt <= CMCI_STORM_THRESHOLD)
159                 return false;
160
161         cmci_clear();
162         __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE);
163         r = atomic_add_return(1, &cmci_storm_on_cpus);
164         mce_timer_kick(CMCI_POLL_INTERVAL);
165
166         if (r == 1)
167                 pr_notice("CMCI storm detected: switching to poll mode\n");
168         return true;
169 }
170
171 /*
172  * The interrupt handler. This is called on every event.
173  * Just call the poller directly to log any events.
174  * This could in theory increase the threshold under high load,
175  * but doesn't for now.
176  */
177 static void intel_threshold_interrupt(void)
178 {
179         if (cmci_storm_detect())
180                 return;
181         machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
182         mce_notify_irq();
183 }
184
185 /*
186  * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
187  * on this CPU. Use the algorithm recommended in the SDM to discover shared
188  * banks.
189  */
190 static void cmci_discover(int banks)
191 {
192         unsigned long *owned = (void *)&__get_cpu_var(mce_banks_owned);
193         unsigned long flags;
194         int i;
195         int bios_wrong_thresh = 0;
196
197         raw_spin_lock_irqsave(&cmci_discover_lock, flags);
198         for (i = 0; i < banks; i++) {
199                 u64 val;
200                 int bios_zero_thresh = 0;
201
202                 if (test_bit(i, owned))
203                         continue;
204
205                 /* Skip banks in firmware first mode */
206                 if (test_bit(i, mce_banks_ce_disabled))
207                         continue;
208
209                 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
210
211                 /* Already owned by someone else? */
212                 if (val & MCI_CTL2_CMCI_EN) {
213                         clear_bit(i, owned);
214                         __clear_bit(i, __get_cpu_var(mce_poll_banks));
215                         continue;
216                 }
217
218                 if (!mca_cfg.bios_cmci_threshold) {
219                         val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
220                         val |= CMCI_THRESHOLD;
221                 } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
222                         /*
223                          * If bios_cmci_threshold boot option was specified
224                          * but the threshold is zero, we'll try to initialize
225                          * it to 1.
226                          */
227                         bios_zero_thresh = 1;
228                         val |= CMCI_THRESHOLD;
229                 }
230
231                 val |= MCI_CTL2_CMCI_EN;
232                 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
233                 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
234
235                 /* Did the enable bit stick? -- the bank supports CMCI */
236                 if (val & MCI_CTL2_CMCI_EN) {
237                         set_bit(i, owned);
238                         __clear_bit(i, __get_cpu_var(mce_poll_banks));
239                         /*
240                          * We are able to set thresholds for some banks that
241                          * had a threshold of 0. This means the BIOS has not
242                          * set the thresholds properly or does not work with
243                          * this boot option. Note down now and report later.
244                          */
245                         if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
246                                         (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
247                                 bios_wrong_thresh = 1;
248                 } else {
249                         WARN_ON(!test_bit(i, __get_cpu_var(mce_poll_banks)));
250                 }
251         }
252         raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
253         if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
254                 pr_info_once(
255                         "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
256                 pr_info_once(
257                         "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
258         }
259 }
260
261 /*
262  * Just in case we missed an event during initialization check
263  * all the CMCI owned banks.
264  */
265 void cmci_recheck(void)
266 {
267         unsigned long flags;
268         int banks;
269
270         if (!mce_available(__this_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
271                 return;
272         local_irq_save(flags);
273         machine_check_poll(MCP_TIMESTAMP, &__get_cpu_var(mce_banks_owned));
274         local_irq_restore(flags);
275 }
276
277 /* Caller must hold the lock on cmci_discover_lock */
278 static void __cmci_disable_bank(int bank)
279 {
280         u64 val;
281
282         if (!test_bit(bank, __get_cpu_var(mce_banks_owned)))
283                 return;
284         rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
285         val &= ~MCI_CTL2_CMCI_EN;
286         wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
287         __clear_bit(bank, __get_cpu_var(mce_banks_owned));
288 }
289
290 /*
291  * Disable CMCI on this CPU for all banks it owns when it goes down.
292  * This allows other CPUs to claim the banks on rediscovery.
293  */
294 void cmci_clear(void)
295 {
296         unsigned long flags;
297         int i;
298         int banks;
299
300         if (!cmci_supported(&banks))
301                 return;
302         raw_spin_lock_irqsave(&cmci_discover_lock, flags);
303         for (i = 0; i < banks; i++)
304                 __cmci_disable_bank(i);
305         raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
306 }
307
308 static void cmci_rediscover_work_func(void *arg)
309 {
310         int banks;
311
312         /* Recheck banks in case CPUs don't all have the same */
313         if (cmci_supported(&banks))
314                 cmci_discover(banks);
315 }
316
317 /* After a CPU went down cycle through all the others and rediscover */
318 void cmci_rediscover(void)
319 {
320         int banks;
321
322         if (!cmci_supported(&banks))
323                 return;
324
325         on_each_cpu(cmci_rediscover_work_func, NULL, 1);
326 }
327
328 /*
329  * Reenable CMCI on this CPU in case a CPU down failed.
330  */
331 void cmci_reenable(void)
332 {
333         int banks;
334         if (cmci_supported(&banks))
335                 cmci_discover(banks);
336 }
337
338 void cmci_disable_bank(int bank)
339 {
340         int banks;
341         unsigned long flags;
342
343         if (!cmci_supported(&banks))
344                 return;
345
346         raw_spin_lock_irqsave(&cmci_discover_lock, flags);
347         __cmci_disable_bank(bank);
348         raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
349 }
350
351 static void intel_init_cmci(void)
352 {
353         int banks;
354
355         if (!cmci_supported(&banks))
356                 return;
357
358         mce_threshold_vector = intel_threshold_interrupt;
359         cmci_discover(banks);
360         /*
361          * For CPU #0 this runs with still disabled APIC, but that's
362          * ok because only the vector is set up. We still do another
363          * check for the banks later for CPU #0 just to make sure
364          * to not miss any events.
365          */
366         apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
367         cmci_recheck();
368 }
369
370 void mce_intel_feature_init(struct cpuinfo_x86 *c)
371 {
372         intel_init_thermal(c);
373         intel_init_cmci();
374 }