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