]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - arch/x86/kernel/cpu/mcheck/mce_intel.c
Merge tag 'pinctrl-v4.1-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[mirror_ubuntu-zesty-kernel.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 <linux/cpumask.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 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
50 /*
51 * cmci_discover_lock protects against parallel discovery attempts
52 * which could race against each other.
53 */
54 static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
55
56 #define CMCI_THRESHOLD 1
57 #define CMCI_POLL_INTERVAL (30 * HZ)
58 #define CMCI_STORM_INTERVAL (HZ)
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;
72
73 static int cmci_supported(int *banks)
74 {
75 u64 cap;
76
77 if (mca_cfg.cmci_disabled || mca_cfg.ignore_ce)
78 return 0;
79
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
94 bool mce_intel_cmci_poll(void)
95 {
96 if (__this_cpu_read(cmci_storm_state) == CMCI_STORM_NONE)
97 return false;
98
99 /*
100 * Reset the counter if we've logged an error in the last poll
101 * during the storm.
102 */
103 if (machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned)))
104 this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
105 else
106 this_cpu_dec(cmci_backoff_cnt);
107
108 return true;
109 }
110
111 void mce_intel_hcpu_update(unsigned long cpu)
112 {
113 if (per_cpu(cmci_storm_state, cpu) == CMCI_STORM_ACTIVE)
114 atomic_dec(&cmci_storm_on_cpus);
115
116 per_cpu(cmci_storm_state, cpu) = CMCI_STORM_NONE;
117 }
118
119 unsigned long cmci_intel_adjust_timer(unsigned long interval)
120 {
121 if ((this_cpu_read(cmci_backoff_cnt) > 0) &&
122 (__this_cpu_read(cmci_storm_state) == CMCI_STORM_ACTIVE)) {
123 mce_notify_irq();
124 return CMCI_STORM_INTERVAL;
125 }
126
127 switch (__this_cpu_read(cmci_storm_state)) {
128 case CMCI_STORM_ACTIVE:
129
130 /*
131 * We switch back to interrupt mode once the poll timer has
132 * silenced itself. That means no events recorded and the timer
133 * interval is back to our poll interval.
134 */
135 __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED);
136 if (!atomic_sub_return(1, &cmci_storm_on_cpus))
137 pr_notice("CMCI storm subsided: switching to interrupt mode\n");
138
139 /* FALLTHROUGH */
140
141 case CMCI_STORM_SUBSIDED:
142 /*
143 * We wait for all CPUs to go back to SUBSIDED state. When that
144 * happens we switch back to interrupt mode.
145 */
146 if (!atomic_read(&cmci_storm_on_cpus)) {
147 __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE);
148 cmci_reenable();
149 cmci_recheck();
150 }
151 return CMCI_POLL_INTERVAL;
152 default:
153
154 /* We have shiny weather. Let the poll do whatever it thinks. */
155 return interval;
156 }
157 }
158
159 static void cmci_storm_disable_banks(void)
160 {
161 unsigned long flags, *owned;
162 int bank;
163 u64 val;
164
165 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
166 owned = this_cpu_ptr(mce_banks_owned);
167 for_each_set_bit(bank, owned, MAX_NR_BANKS) {
168 rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
169 val &= ~MCI_CTL2_CMCI_EN;
170 wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
171 }
172 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
173 }
174
175 static bool cmci_storm_detect(void)
176 {
177 unsigned int cnt = __this_cpu_read(cmci_storm_cnt);
178 unsigned long ts = __this_cpu_read(cmci_time_stamp);
179 unsigned long now = jiffies;
180 int r;
181
182 if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE)
183 return true;
184
185 if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) {
186 cnt++;
187 } else {
188 cnt = 1;
189 __this_cpu_write(cmci_time_stamp, now);
190 }
191 __this_cpu_write(cmci_storm_cnt, cnt);
192
193 if (cnt <= CMCI_STORM_THRESHOLD)
194 return false;
195
196 cmci_storm_disable_banks();
197 __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE);
198 r = atomic_add_return(1, &cmci_storm_on_cpus);
199 mce_timer_kick(CMCI_STORM_INTERVAL);
200 this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
201
202 if (r == 1)
203 pr_notice("CMCI storm detected: switching to poll mode\n");
204 return true;
205 }
206
207 /*
208 * The interrupt handler. This is called on every event.
209 * Just call the poller directly to log any events.
210 * This could in theory increase the threshold under high load,
211 * but doesn't for now.
212 */
213 static void intel_threshold_interrupt(void)
214 {
215 if (cmci_storm_detect())
216 return;
217
218 machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
219 mce_notify_irq();
220 }
221
222 /*
223 * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
224 * on this CPU. Use the algorithm recommended in the SDM to discover shared
225 * banks.
226 */
227 static void cmci_discover(int banks)
228 {
229 unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned);
230 unsigned long flags;
231 int i;
232 int bios_wrong_thresh = 0;
233
234 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
235 for (i = 0; i < banks; i++) {
236 u64 val;
237 int bios_zero_thresh = 0;
238
239 if (test_bit(i, owned))
240 continue;
241
242 /* Skip banks in firmware first mode */
243 if (test_bit(i, mce_banks_ce_disabled))
244 continue;
245
246 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
247
248 /* Already owned by someone else? */
249 if (val & MCI_CTL2_CMCI_EN) {
250 clear_bit(i, owned);
251 __clear_bit(i, this_cpu_ptr(mce_poll_banks));
252 continue;
253 }
254
255 if (!mca_cfg.bios_cmci_threshold) {
256 val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
257 val |= CMCI_THRESHOLD;
258 } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
259 /*
260 * If bios_cmci_threshold boot option was specified
261 * but the threshold is zero, we'll try to initialize
262 * it to 1.
263 */
264 bios_zero_thresh = 1;
265 val |= CMCI_THRESHOLD;
266 }
267
268 val |= MCI_CTL2_CMCI_EN;
269 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
270 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
271
272 /* Did the enable bit stick? -- the bank supports CMCI */
273 if (val & MCI_CTL2_CMCI_EN) {
274 set_bit(i, owned);
275 __clear_bit(i, this_cpu_ptr(mce_poll_banks));
276 /*
277 * We are able to set thresholds for some banks that
278 * had a threshold of 0. This means the BIOS has not
279 * set the thresholds properly or does not work with
280 * this boot option. Note down now and report later.
281 */
282 if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
283 (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
284 bios_wrong_thresh = 1;
285 } else {
286 WARN_ON(!test_bit(i, this_cpu_ptr(mce_poll_banks)));
287 }
288 }
289 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
290 if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
291 pr_info_once(
292 "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
293 pr_info_once(
294 "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
295 }
296 }
297
298 /*
299 * Just in case we missed an event during initialization check
300 * all the CMCI owned banks.
301 */
302 void cmci_recheck(void)
303 {
304 unsigned long flags;
305 int banks;
306
307 if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
308 return;
309
310 local_irq_save(flags);
311 machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
312 local_irq_restore(flags);
313 }
314
315 /* Caller must hold the lock on cmci_discover_lock */
316 static void __cmci_disable_bank(int bank)
317 {
318 u64 val;
319
320 if (!test_bit(bank, this_cpu_ptr(mce_banks_owned)))
321 return;
322 rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
323 val &= ~MCI_CTL2_CMCI_EN;
324 wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
325 __clear_bit(bank, this_cpu_ptr(mce_banks_owned));
326 }
327
328 /*
329 * Disable CMCI on this CPU for all banks it owns when it goes down.
330 * This allows other CPUs to claim the banks on rediscovery.
331 */
332 void cmci_clear(void)
333 {
334 unsigned long flags;
335 int i;
336 int banks;
337
338 if (!cmci_supported(&banks))
339 return;
340 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
341 for (i = 0; i < banks; i++)
342 __cmci_disable_bank(i);
343 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
344 }
345
346 static void cmci_rediscover_work_func(void *arg)
347 {
348 int banks;
349
350 /* Recheck banks in case CPUs don't all have the same */
351 if (cmci_supported(&banks))
352 cmci_discover(banks);
353 }
354
355 /* After a CPU went down cycle through all the others and rediscover */
356 void cmci_rediscover(void)
357 {
358 int banks;
359
360 if (!cmci_supported(&banks))
361 return;
362
363 on_each_cpu(cmci_rediscover_work_func, NULL, 1);
364 }
365
366 /*
367 * Reenable CMCI on this CPU in case a CPU down failed.
368 */
369 void cmci_reenable(void)
370 {
371 int banks;
372 if (cmci_supported(&banks))
373 cmci_discover(banks);
374 }
375
376 void cmci_disable_bank(int bank)
377 {
378 int banks;
379 unsigned long flags;
380
381 if (!cmci_supported(&banks))
382 return;
383
384 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
385 __cmci_disable_bank(bank);
386 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
387 }
388
389 static void intel_init_cmci(void)
390 {
391 int banks;
392
393 if (!cmci_supported(&banks))
394 return;
395
396 mce_threshold_vector = intel_threshold_interrupt;
397 cmci_discover(banks);
398 /*
399 * For CPU #0 this runs with still disabled APIC, but that's
400 * ok because only the vector is set up. We still do another
401 * check for the banks later for CPU #0 just to make sure
402 * to not miss any events.
403 */
404 apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
405 cmci_recheck();
406 }
407
408 void mce_intel_feature_init(struct cpuinfo_x86 *c)
409 {
410 intel_init_thermal(c);
411 intel_init_cmci();
412 }