]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Intel specific MCE features. | |
3 | * Copyright 2004 Zwane Mwaikambo <zwane@linuxpower.ca> | |
88ccbedd AK |
4 | * Copyright (C) 2008, 2009 Intel Corporation |
5 | * Author: Andi Kleen | |
1da177e4 LT |
6 | */ |
7 | ||
5a0e3ad6 | 8 | #include <linux/gfp.h> |
1da177e4 LT |
9 | #include <linux/interrupt.h> |
10 | #include <linux/percpu.h> | |
d43c36dc | 11 | #include <linux/sched.h> |
27f6c573 | 12 | #include <linux/cpumask.h> |
1bf7b31e | 13 | #include <asm/apic.h> |
1da177e4 LT |
14 | #include <asm/processor.h> |
15 | #include <asm/msr.h> | |
16 | #include <asm/mce.h> | |
1da177e4 | 17 | |
55babd8f CG |
18 | #include "mce-internal.h" |
19 | ||
88ccbedd AK |
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 | ||
0644414e NR |
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 | */ | |
88ccbedd AK |
39 | static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned); |
40 | ||
3f2f0680 BP |
41 | /* |
42 | * CMCI storm detection backoff counter | |
43 | * | |
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. | |
47 | */ | |
48 | static DEFINE_PER_CPU(int, cmci_backoff_cnt); | |
49 | ||
88ccbedd AK |
50 | /* |
51 | * cmci_discover_lock protects against parallel discovery attempts | |
52 | * which could race against each other. | |
53 | */ | |
ed5c41d3 | 54 | static DEFINE_RAW_SPINLOCK(cmci_discover_lock); |
88ccbedd | 55 | |
55babd8f CG |
56 | #define CMCI_THRESHOLD 1 |
57 | #define CMCI_POLL_INTERVAL (30 * HZ) | |
3f2f0680 | 58 | #define CMCI_STORM_INTERVAL (HZ) |
55babd8f CG |
59 | #define CMCI_STORM_THRESHOLD 15 |
60 | ||
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); | |
64 | ||
65 | enum { | |
66 | CMCI_STORM_NONE, | |
67 | CMCI_STORM_ACTIVE, | |
68 | CMCI_STORM_SUBSIDED, | |
69 | }; | |
70 | ||
71 | static atomic_t cmci_storm_on_cpus; | |
88ccbedd | 72 | |
df20e2eb | 73 | static int cmci_supported(int *banks) |
88ccbedd AK |
74 | { |
75 | u64 cap; | |
76 | ||
7af19e4a | 77 | if (mca_cfg.cmci_disabled || mca_cfg.ignore_ce) |
62fdac59 HS |
78 | return 0; |
79 | ||
88ccbedd AK |
80 | /* |
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. | |
84 | */ | |
85 | if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) | |
86 | return 0; | |
87 | if (!cpu_has_apic || lapic_get_maxlvt() < 6) | |
88 | return 0; | |
89 | rdmsrl(MSR_IA32_MCG_CAP, cap); | |
90 | *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff); | |
91 | return !!(cap & MCG_CMCI_P); | |
92 | } | |
93 | ||
88d53867 AR |
94 | static bool lmce_supported(void) |
95 | { | |
96 | u64 tmp; | |
97 | ||
98 | if (mca_cfg.lmce_disabled) | |
99 | return false; | |
100 | ||
101 | rdmsrl(MSR_IA32_MCG_CAP, tmp); | |
102 | ||
103 | /* | |
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. | |
106 | */ | |
107 | if ((tmp & (MCG_SER_P | MCG_LMCE_P)) != | |
108 | (MCG_SER_P | MCG_LMCE_P)) | |
109 | return false; | |
110 | ||
111 | /* | |
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. | |
115 | */ | |
116 | rdmsrl(MSR_IA32_FEATURE_CONTROL, tmp); | |
117 | if ((tmp & (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_LMCE)) == | |
118 | (FEATURE_CONTROL_LOCKED | FEATURE_CONTROL_LMCE)) | |
119 | return true; | |
120 | ||
121 | return false; | |
122 | } | |
123 | ||
3f2f0680 | 124 | bool mce_intel_cmci_poll(void) |
55babd8f CG |
125 | { |
126 | if (__this_cpu_read(cmci_storm_state) == CMCI_STORM_NONE) | |
3f2f0680 BP |
127 | return false; |
128 | ||
129 | /* | |
130 | * Reset the counter if we've logged an error in the last poll | |
131 | * during the storm. | |
132 | */ | |
133 | if (machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned))) | |
134 | this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL); | |
135 | else | |
136 | this_cpu_dec(cmci_backoff_cnt); | |
137 | ||
138 | return true; | |
55babd8f CG |
139 | } |
140 | ||
141 | void mce_intel_hcpu_update(unsigned long cpu) | |
142 | { | |
143 | if (per_cpu(cmci_storm_state, cpu) == CMCI_STORM_ACTIVE) | |
144 | atomic_dec(&cmci_storm_on_cpus); | |
145 | ||
146 | per_cpu(cmci_storm_state, cpu) = CMCI_STORM_NONE; | |
147 | } | |
148 | ||
3f2f0680 | 149 | unsigned long cmci_intel_adjust_timer(unsigned long interval) |
55babd8f | 150 | { |
3f2f0680 BP |
151 | if ((this_cpu_read(cmci_backoff_cnt) > 0) && |
152 | (__this_cpu_read(cmci_storm_state) == CMCI_STORM_ACTIVE)) { | |
153 | mce_notify_irq(); | |
154 | return CMCI_STORM_INTERVAL; | |
155 | } | |
55babd8f CG |
156 | |
157 | switch (__this_cpu_read(cmci_storm_state)) { | |
158 | case CMCI_STORM_ACTIVE: | |
3f2f0680 | 159 | |
55babd8f CG |
160 | /* |
161 | * We switch back to interrupt mode once the poll timer has | |
3f2f0680 BP |
162 | * silenced itself. That means no events recorded and the timer |
163 | * interval is back to our poll interval. | |
55babd8f CG |
164 | */ |
165 | __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED); | |
3f2f0680 | 166 | if (!atomic_sub_return(1, &cmci_storm_on_cpus)) |
55babd8f | 167 | pr_notice("CMCI storm subsided: switching to interrupt mode\n"); |
3f2f0680 | 168 | |
55babd8f CG |
169 | /* FALLTHROUGH */ |
170 | ||
171 | case CMCI_STORM_SUBSIDED: | |
172 | /* | |
3f2f0680 BP |
173 | * We wait for all CPUs to go back to SUBSIDED state. When that |
174 | * happens we switch back to interrupt mode. | |
55babd8f CG |
175 | */ |
176 | if (!atomic_read(&cmci_storm_on_cpus)) { | |
177 | __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE); | |
178 | cmci_reenable(); | |
179 | cmci_recheck(); | |
180 | } | |
181 | return CMCI_POLL_INTERVAL; | |
182 | default: | |
3f2f0680 BP |
183 | |
184 | /* We have shiny weather. Let the poll do whatever it thinks. */ | |
55babd8f CG |
185 | return interval; |
186 | } | |
187 | } | |
188 | ||
27f6c573 CG |
189 | static void cmci_storm_disable_banks(void) |
190 | { | |
191 | unsigned long flags, *owned; | |
192 | int bank; | |
193 | u64 val; | |
194 | ||
ed5c41d3 | 195 | raw_spin_lock_irqsave(&cmci_discover_lock, flags); |
89cbc767 | 196 | owned = this_cpu_ptr(mce_banks_owned); |
27f6c573 CG |
197 | for_each_set_bit(bank, owned, MAX_NR_BANKS) { |
198 | rdmsrl(MSR_IA32_MCx_CTL2(bank), val); | |
199 | val &= ~MCI_CTL2_CMCI_EN; | |
200 | wrmsrl(MSR_IA32_MCx_CTL2(bank), val); | |
201 | } | |
ed5c41d3 | 202 | raw_spin_unlock_irqrestore(&cmci_discover_lock, flags); |
27f6c573 CG |
203 | } |
204 | ||
55babd8f CG |
205 | static bool cmci_storm_detect(void) |
206 | { | |
207 | unsigned int cnt = __this_cpu_read(cmci_storm_cnt); | |
208 | unsigned long ts = __this_cpu_read(cmci_time_stamp); | |
209 | unsigned long now = jiffies; | |
210 | int r; | |
211 | ||
212 | if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE) | |
213 | return true; | |
214 | ||
215 | if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) { | |
216 | cnt++; | |
217 | } else { | |
218 | cnt = 1; | |
219 | __this_cpu_write(cmci_time_stamp, now); | |
220 | } | |
221 | __this_cpu_write(cmci_storm_cnt, cnt); | |
222 | ||
223 | if (cnt <= CMCI_STORM_THRESHOLD) | |
224 | return false; | |
225 | ||
27f6c573 | 226 | cmci_storm_disable_banks(); |
55babd8f CG |
227 | __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE); |
228 | r = atomic_add_return(1, &cmci_storm_on_cpus); | |
3f2f0680 BP |
229 | mce_timer_kick(CMCI_STORM_INTERVAL); |
230 | this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL); | |
55babd8f CG |
231 | |
232 | if (r == 1) | |
233 | pr_notice("CMCI storm detected: switching to poll mode\n"); | |
234 | return true; | |
235 | } | |
236 | ||
88ccbedd AK |
237 | /* |
238 | * The interrupt handler. This is called on every event. | |
239 | * Just call the poller directly to log any events. | |
240 | * This could in theory increase the threshold under high load, | |
241 | * but doesn't for now. | |
242 | */ | |
243 | static void intel_threshold_interrupt(void) | |
244 | { | |
55babd8f CG |
245 | if (cmci_storm_detect()) |
246 | return; | |
3f2f0680 | 247 | |
89cbc767 | 248 | machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned)); |
9ff36ee9 | 249 | mce_notify_irq(); |
88ccbedd AK |
250 | } |
251 | ||
88ccbedd AK |
252 | /* |
253 | * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks | |
254 | * on this CPU. Use the algorithm recommended in the SDM to discover shared | |
255 | * banks. | |
256 | */ | |
4670a300 | 257 | static void cmci_discover(int banks) |
88ccbedd | 258 | { |
89cbc767 | 259 | unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned); |
e5299926 | 260 | unsigned long flags; |
88ccbedd | 261 | int i; |
450cc201 | 262 | int bios_wrong_thresh = 0; |
88ccbedd | 263 | |
ed5c41d3 | 264 | raw_spin_lock_irqsave(&cmci_discover_lock, flags); |
88ccbedd AK |
265 | for (i = 0; i < banks; i++) { |
266 | u64 val; | |
450cc201 | 267 | int bios_zero_thresh = 0; |
88ccbedd AK |
268 | |
269 | if (test_bit(i, owned)) | |
270 | continue; | |
271 | ||
c3d1fb56 NR |
272 | /* Skip banks in firmware first mode */ |
273 | if (test_bit(i, mce_banks_ce_disabled)) | |
274 | continue; | |
275 | ||
a2d32bcb | 276 | rdmsrl(MSR_IA32_MCx_CTL2(i), val); |
88ccbedd AK |
277 | |
278 | /* Already owned by someone else? */ | |
1f9a0bd4 | 279 | if (val & MCI_CTL2_CMCI_EN) { |
4670a300 | 280 | clear_bit(i, owned); |
89cbc767 | 281 | __clear_bit(i, this_cpu_ptr(mce_poll_banks)); |
88ccbedd AK |
282 | continue; |
283 | } | |
284 | ||
1462594b | 285 | if (!mca_cfg.bios_cmci_threshold) { |
450cc201 NR |
286 | val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK; |
287 | val |= CMCI_THRESHOLD; | |
288 | } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) { | |
289 | /* | |
290 | * If bios_cmci_threshold boot option was specified | |
291 | * but the threshold is zero, we'll try to initialize | |
292 | * it to 1. | |
293 | */ | |
294 | bios_zero_thresh = 1; | |
295 | val |= CMCI_THRESHOLD; | |
296 | } | |
297 | ||
298 | val |= MCI_CTL2_CMCI_EN; | |
a2d32bcb AK |
299 | wrmsrl(MSR_IA32_MCx_CTL2(i), val); |
300 | rdmsrl(MSR_IA32_MCx_CTL2(i), val); | |
88ccbedd AK |
301 | |
302 | /* Did the enable bit stick? -- the bank supports CMCI */ | |
1f9a0bd4 | 303 | if (val & MCI_CTL2_CMCI_EN) { |
4670a300 | 304 | set_bit(i, owned); |
89cbc767 | 305 | __clear_bit(i, this_cpu_ptr(mce_poll_banks)); |
450cc201 NR |
306 | /* |
307 | * We are able to set thresholds for some banks that | |
308 | * had a threshold of 0. This means the BIOS has not | |
309 | * set the thresholds properly or does not work with | |
310 | * this boot option. Note down now and report later. | |
311 | */ | |
1462594b | 312 | if (mca_cfg.bios_cmci_threshold && bios_zero_thresh && |
450cc201 NR |
313 | (val & MCI_CTL2_CMCI_THRESHOLD_MASK)) |
314 | bios_wrong_thresh = 1; | |
88ccbedd | 315 | } else { |
89cbc767 | 316 | WARN_ON(!test_bit(i, this_cpu_ptr(mce_poll_banks))); |
88ccbedd AK |
317 | } |
318 | } | |
ed5c41d3 | 319 | raw_spin_unlock_irqrestore(&cmci_discover_lock, flags); |
1462594b | 320 | if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) { |
450cc201 NR |
321 | pr_info_once( |
322 | "bios_cmci_threshold: Some banks do not have valid thresholds set\n"); | |
323 | pr_info_once( | |
324 | "bios_cmci_threshold: Make sure your BIOS supports this boot option\n"); | |
325 | } | |
88ccbedd AK |
326 | } |
327 | ||
328 | /* | |
329 | * Just in case we missed an event during initialization check | |
330 | * all the CMCI owned banks. | |
331 | */ | |
df20e2eb | 332 | void cmci_recheck(void) |
88ccbedd AK |
333 | { |
334 | unsigned long flags; | |
335 | int banks; | |
336 | ||
89cbc767 | 337 | if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks)) |
88ccbedd | 338 | return; |
3f2f0680 | 339 | |
88ccbedd | 340 | local_irq_save(flags); |
89cbc767 | 341 | machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned)); |
88ccbedd AK |
342 | local_irq_restore(flags); |
343 | } | |
344 | ||
c3d1fb56 NR |
345 | /* Caller must hold the lock on cmci_discover_lock */ |
346 | static void __cmci_disable_bank(int bank) | |
347 | { | |
348 | u64 val; | |
349 | ||
89cbc767 | 350 | if (!test_bit(bank, this_cpu_ptr(mce_banks_owned))) |
c3d1fb56 NR |
351 | return; |
352 | rdmsrl(MSR_IA32_MCx_CTL2(bank), val); | |
353 | val &= ~MCI_CTL2_CMCI_EN; | |
354 | wrmsrl(MSR_IA32_MCx_CTL2(bank), val); | |
89cbc767 | 355 | __clear_bit(bank, this_cpu_ptr(mce_banks_owned)); |
c3d1fb56 NR |
356 | } |
357 | ||
88ccbedd AK |
358 | /* |
359 | * Disable CMCI on this CPU for all banks it owns when it goes down. | |
360 | * This allows other CPUs to claim the banks on rediscovery. | |
361 | */ | |
df20e2eb | 362 | void cmci_clear(void) |
88ccbedd | 363 | { |
e5299926 | 364 | unsigned long flags; |
88ccbedd AK |
365 | int i; |
366 | int banks; | |
88ccbedd AK |
367 | |
368 | if (!cmci_supported(&banks)) | |
369 | return; | |
ed5c41d3 | 370 | raw_spin_lock_irqsave(&cmci_discover_lock, flags); |
c3d1fb56 NR |
371 | for (i = 0; i < banks; i++) |
372 | __cmci_disable_bank(i); | |
ed5c41d3 | 373 | raw_spin_unlock_irqrestore(&cmci_discover_lock, flags); |
88ccbedd AK |
374 | } |
375 | ||
7a0c819d | 376 | static void cmci_rediscover_work_func(void *arg) |
85b97637 TC |
377 | { |
378 | int banks; | |
379 | ||
380 | /* Recheck banks in case CPUs don't all have the same */ | |
381 | if (cmci_supported(&banks)) | |
382 | cmci_discover(banks); | |
85b97637 TC |
383 | } |
384 | ||
7a0c819d SB |
385 | /* After a CPU went down cycle through all the others and rediscover */ |
386 | void cmci_rediscover(void) | |
88ccbedd | 387 | { |
7a0c819d | 388 | int banks; |
88ccbedd AK |
389 | |
390 | if (!cmci_supported(&banks)) | |
391 | return; | |
88ccbedd | 392 | |
7a0c819d | 393 | on_each_cpu(cmci_rediscover_work_func, NULL, 1); |
88ccbedd AK |
394 | } |
395 | ||
396 | /* | |
397 | * Reenable CMCI on this CPU in case a CPU down failed. | |
398 | */ | |
399 | void cmci_reenable(void) | |
400 | { | |
401 | int banks; | |
402 | if (cmci_supported(&banks)) | |
4670a300 | 403 | cmci_discover(banks); |
88ccbedd AK |
404 | } |
405 | ||
c3d1fb56 NR |
406 | void cmci_disable_bank(int bank) |
407 | { | |
408 | int banks; | |
409 | unsigned long flags; | |
410 | ||
411 | if (!cmci_supported(&banks)) | |
412 | return; | |
413 | ||
ed5c41d3 | 414 | raw_spin_lock_irqsave(&cmci_discover_lock, flags); |
c3d1fb56 | 415 | __cmci_disable_bank(bank); |
ed5c41d3 | 416 | raw_spin_unlock_irqrestore(&cmci_discover_lock, flags); |
c3d1fb56 NR |
417 | } |
418 | ||
514ec49a | 419 | static void intel_init_cmci(void) |
88ccbedd AK |
420 | { |
421 | int banks; | |
422 | ||
423 | if (!cmci_supported(&banks)) | |
424 | return; | |
425 | ||
426 | mce_threshold_vector = intel_threshold_interrupt; | |
4670a300 | 427 | cmci_discover(banks); |
88ccbedd AK |
428 | /* |
429 | * For CPU #0 this runs with still disabled APIC, but that's | |
430 | * ok because only the vector is set up. We still do another | |
431 | * check for the banks later for CPU #0 just to make sure | |
432 | * to not miss any events. | |
433 | */ | |
434 | apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED); | |
435 | cmci_recheck(); | |
436 | } | |
437 | ||
88d53867 AR |
438 | void intel_init_lmce(void) |
439 | { | |
440 | u64 val; | |
441 | ||
442 | if (!lmce_supported()) | |
443 | return; | |
444 | ||
445 | rdmsrl(MSR_IA32_MCG_EXT_CTL, val); | |
446 | ||
447 | if (!(val & MCG_EXT_CTL_LMCE_EN)) | |
448 | wrmsrl(MSR_IA32_MCG_EXT_CTL, val | MCG_EXT_CTL_LMCE_EN); | |
449 | } | |
450 | ||
cc3ca220 | 451 | void mce_intel_feature_init(struct cpuinfo_x86 *c) |
1da177e4 LT |
452 | { |
453 | intel_init_thermal(c); | |
88ccbedd | 454 | intel_init_cmci(); |
1da177e4 | 455 | } |