0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/gfp.h>
0010 #include <linux/interrupt.h>
0011 #include <linux/percpu.h>
0012 #include <linux/sched.h>
0013 #include <linux/cpumask.h>
0014 #include <asm/apic.h>
0015 #include <asm/cpufeature.h>
0016 #include <asm/intel-family.h>
0017 #include <asm/processor.h>
0018 #include <asm/msr.h>
0019 #include <asm/mce.h>
0020
0021 #include "internal.h"
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned);
0043
0044
0045
0046
0047
0048
0049
0050
0051 static DEFINE_PER_CPU(int, cmci_backoff_cnt);
0052
0053
0054
0055
0056
0057 static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
0058
0059 #define CMCI_THRESHOLD 1
0060 #define CMCI_POLL_INTERVAL (30 * HZ)
0061 #define CMCI_STORM_INTERVAL (HZ)
0062 #define CMCI_STORM_THRESHOLD 15
0063
0064 static DEFINE_PER_CPU(unsigned long, cmci_time_stamp);
0065 static DEFINE_PER_CPU(unsigned int, cmci_storm_cnt);
0066 static DEFINE_PER_CPU(unsigned int, cmci_storm_state);
0067
0068 enum {
0069 CMCI_STORM_NONE,
0070 CMCI_STORM_ACTIVE,
0071 CMCI_STORM_SUBSIDED,
0072 };
0073
0074 static atomic_t cmci_storm_on_cpus;
0075
0076 static int cmci_supported(int *banks)
0077 {
0078 u64 cap;
0079
0080 if (mca_cfg.cmci_disabled || mca_cfg.ignore_ce)
0081 return 0;
0082
0083
0084
0085
0086
0087
0088 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL &&
0089 boot_cpu_data.x86_vendor != X86_VENDOR_ZHAOXIN)
0090 return 0;
0091
0092 if (!boot_cpu_has(X86_FEATURE_APIC) || lapic_get_maxlvt() < 6)
0093 return 0;
0094 rdmsrl(MSR_IA32_MCG_CAP, cap);
0095 *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff);
0096 return !!(cap & MCG_CMCI_P);
0097 }
0098
0099 static bool lmce_supported(void)
0100 {
0101 u64 tmp;
0102
0103 if (mca_cfg.lmce_disabled)
0104 return false;
0105
0106 rdmsrl(MSR_IA32_MCG_CAP, tmp);
0107
0108
0109
0110
0111
0112 if ((tmp & (MCG_SER_P | MCG_LMCE_P)) !=
0113 (MCG_SER_P | MCG_LMCE_P))
0114 return false;
0115
0116
0117
0118
0119
0120
0121
0122
0123 rdmsrl(MSR_IA32_FEAT_CTL, tmp);
0124 if (WARN_ON_ONCE(!(tmp & FEAT_CTL_LOCKED)))
0125 return false;
0126
0127 return tmp & FEAT_CTL_LMCE_ENABLED;
0128 }
0129
0130 bool mce_intel_cmci_poll(void)
0131 {
0132 if (__this_cpu_read(cmci_storm_state) == CMCI_STORM_NONE)
0133 return false;
0134
0135
0136
0137
0138
0139 if (machine_check_poll(0, this_cpu_ptr(&mce_banks_owned)))
0140 this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
0141 else
0142 this_cpu_dec(cmci_backoff_cnt);
0143
0144 return true;
0145 }
0146
0147 void mce_intel_hcpu_update(unsigned long cpu)
0148 {
0149 if (per_cpu(cmci_storm_state, cpu) == CMCI_STORM_ACTIVE)
0150 atomic_dec(&cmci_storm_on_cpus);
0151
0152 per_cpu(cmci_storm_state, cpu) = CMCI_STORM_NONE;
0153 }
0154
0155 static void cmci_toggle_interrupt_mode(bool on)
0156 {
0157 unsigned long flags, *owned;
0158 int bank;
0159 u64 val;
0160
0161 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
0162 owned = this_cpu_ptr(mce_banks_owned);
0163 for_each_set_bit(bank, owned, MAX_NR_BANKS) {
0164 rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
0165
0166 if (on)
0167 val |= MCI_CTL2_CMCI_EN;
0168 else
0169 val &= ~MCI_CTL2_CMCI_EN;
0170
0171 wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
0172 }
0173 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
0174 }
0175
0176 unsigned long cmci_intel_adjust_timer(unsigned long interval)
0177 {
0178 if ((this_cpu_read(cmci_backoff_cnt) > 0) &&
0179 (__this_cpu_read(cmci_storm_state) == CMCI_STORM_ACTIVE)) {
0180 mce_notify_irq();
0181 return CMCI_STORM_INTERVAL;
0182 }
0183
0184 switch (__this_cpu_read(cmci_storm_state)) {
0185 case CMCI_STORM_ACTIVE:
0186
0187
0188
0189
0190
0191
0192 __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED);
0193 if (!atomic_sub_return(1, &cmci_storm_on_cpus))
0194 pr_notice("CMCI storm subsided: switching to interrupt mode\n");
0195
0196 fallthrough;
0197
0198 case CMCI_STORM_SUBSIDED:
0199
0200
0201
0202
0203 if (!atomic_read(&cmci_storm_on_cpus)) {
0204 __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE);
0205 cmci_toggle_interrupt_mode(true);
0206 cmci_recheck();
0207 }
0208 return CMCI_POLL_INTERVAL;
0209 default:
0210
0211
0212 return interval;
0213 }
0214 }
0215
0216 static bool cmci_storm_detect(void)
0217 {
0218 unsigned int cnt = __this_cpu_read(cmci_storm_cnt);
0219 unsigned long ts = __this_cpu_read(cmci_time_stamp);
0220 unsigned long now = jiffies;
0221 int r;
0222
0223 if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE)
0224 return true;
0225
0226 if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) {
0227 cnt++;
0228 } else {
0229 cnt = 1;
0230 __this_cpu_write(cmci_time_stamp, now);
0231 }
0232 __this_cpu_write(cmci_storm_cnt, cnt);
0233
0234 if (cnt <= CMCI_STORM_THRESHOLD)
0235 return false;
0236
0237 cmci_toggle_interrupt_mode(false);
0238 __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE);
0239 r = atomic_add_return(1, &cmci_storm_on_cpus);
0240 mce_timer_kick(CMCI_STORM_INTERVAL);
0241 this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL);
0242
0243 if (r == 1)
0244 pr_notice("CMCI storm detected: switching to poll mode\n");
0245 return true;
0246 }
0247
0248
0249
0250
0251
0252
0253
0254 static void intel_threshold_interrupt(void)
0255 {
0256 if (cmci_storm_detect())
0257 return;
0258
0259 machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned));
0260 }
0261
0262
0263
0264
0265
0266
0267 static void cmci_discover(int banks)
0268 {
0269 unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned);
0270 unsigned long flags;
0271 int i;
0272 int bios_wrong_thresh = 0;
0273
0274 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
0275 for (i = 0; i < banks; i++) {
0276 u64 val;
0277 int bios_zero_thresh = 0;
0278
0279 if (test_bit(i, owned))
0280 continue;
0281
0282
0283 if (test_bit(i, mce_banks_ce_disabled))
0284 continue;
0285
0286 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
0287
0288
0289 if (val & MCI_CTL2_CMCI_EN) {
0290 clear_bit(i, owned);
0291 __clear_bit(i, this_cpu_ptr(mce_poll_banks));
0292 continue;
0293 }
0294
0295 if (!mca_cfg.bios_cmci_threshold) {
0296 val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK;
0297 val |= CMCI_THRESHOLD;
0298 } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) {
0299
0300
0301
0302
0303
0304 bios_zero_thresh = 1;
0305 val |= CMCI_THRESHOLD;
0306 }
0307
0308 val |= MCI_CTL2_CMCI_EN;
0309 wrmsrl(MSR_IA32_MCx_CTL2(i), val);
0310 rdmsrl(MSR_IA32_MCx_CTL2(i), val);
0311
0312
0313 if (val & MCI_CTL2_CMCI_EN) {
0314 set_bit(i, owned);
0315 __clear_bit(i, this_cpu_ptr(mce_poll_banks));
0316
0317
0318
0319
0320
0321
0322 if (mca_cfg.bios_cmci_threshold && bios_zero_thresh &&
0323 (val & MCI_CTL2_CMCI_THRESHOLD_MASK))
0324 bios_wrong_thresh = 1;
0325 } else {
0326 WARN_ON(!test_bit(i, this_cpu_ptr(mce_poll_banks)));
0327 }
0328 }
0329 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
0330 if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) {
0331 pr_info_once(
0332 "bios_cmci_threshold: Some banks do not have valid thresholds set\n");
0333 pr_info_once(
0334 "bios_cmci_threshold: Make sure your BIOS supports this boot option\n");
0335 }
0336 }
0337
0338
0339
0340
0341
0342 void cmci_recheck(void)
0343 {
0344 unsigned long flags;
0345 int banks;
0346
0347 if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks))
0348 return;
0349
0350 local_irq_save(flags);
0351 machine_check_poll(0, this_cpu_ptr(&mce_banks_owned));
0352 local_irq_restore(flags);
0353 }
0354
0355
0356 static void __cmci_disable_bank(int bank)
0357 {
0358 u64 val;
0359
0360 if (!test_bit(bank, this_cpu_ptr(mce_banks_owned)))
0361 return;
0362 rdmsrl(MSR_IA32_MCx_CTL2(bank), val);
0363 val &= ~MCI_CTL2_CMCI_EN;
0364 wrmsrl(MSR_IA32_MCx_CTL2(bank), val);
0365 __clear_bit(bank, this_cpu_ptr(mce_banks_owned));
0366 }
0367
0368
0369
0370
0371
0372 void cmci_clear(void)
0373 {
0374 unsigned long flags;
0375 int i;
0376 int banks;
0377
0378 if (!cmci_supported(&banks))
0379 return;
0380 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
0381 for (i = 0; i < banks; i++)
0382 __cmci_disable_bank(i);
0383 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
0384 }
0385
0386 static void cmci_rediscover_work_func(void *arg)
0387 {
0388 int banks;
0389
0390
0391 if (cmci_supported(&banks))
0392 cmci_discover(banks);
0393 }
0394
0395
0396 void cmci_rediscover(void)
0397 {
0398 int banks;
0399
0400 if (!cmci_supported(&banks))
0401 return;
0402
0403 on_each_cpu(cmci_rediscover_work_func, NULL, 1);
0404 }
0405
0406
0407
0408
0409 void cmci_reenable(void)
0410 {
0411 int banks;
0412 if (cmci_supported(&banks))
0413 cmci_discover(banks);
0414 }
0415
0416 void cmci_disable_bank(int bank)
0417 {
0418 int banks;
0419 unsigned long flags;
0420
0421 if (!cmci_supported(&banks))
0422 return;
0423
0424 raw_spin_lock_irqsave(&cmci_discover_lock, flags);
0425 __cmci_disable_bank(bank);
0426 raw_spin_unlock_irqrestore(&cmci_discover_lock, flags);
0427 }
0428
0429 void intel_init_cmci(void)
0430 {
0431 int banks;
0432
0433 if (!cmci_supported(&banks))
0434 return;
0435
0436 mce_threshold_vector = intel_threshold_interrupt;
0437 cmci_discover(banks);
0438
0439
0440
0441
0442
0443
0444 apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED);
0445 cmci_recheck();
0446 }
0447
0448 void intel_init_lmce(void)
0449 {
0450 u64 val;
0451
0452 if (!lmce_supported())
0453 return;
0454
0455 rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
0456
0457 if (!(val & MCG_EXT_CTL_LMCE_EN))
0458 wrmsrl(MSR_IA32_MCG_EXT_CTL, val | MCG_EXT_CTL_LMCE_EN);
0459 }
0460
0461 void intel_clear_lmce(void)
0462 {
0463 u64 val;
0464
0465 if (!lmce_supported())
0466 return;
0467
0468 rdmsrl(MSR_IA32_MCG_EXT_CTL, val);
0469 val &= ~MCG_EXT_CTL_LMCE_EN;
0470 wrmsrl(MSR_IA32_MCG_EXT_CTL, val);
0471 }
0472
0473
0474
0475
0476
0477 static void intel_imc_init(struct cpuinfo_x86 *c)
0478 {
0479 u64 error_control;
0480
0481 switch (c->x86_model) {
0482 case INTEL_FAM6_SANDYBRIDGE_X:
0483 case INTEL_FAM6_IVYBRIDGE_X:
0484 case INTEL_FAM6_HASWELL_X:
0485 if (rdmsrl_safe(MSR_ERROR_CONTROL, &error_control))
0486 return;
0487 error_control |= 2;
0488 wrmsrl_safe(MSR_ERROR_CONTROL, error_control);
0489 break;
0490 }
0491 }
0492
0493 void mce_intel_feature_init(struct cpuinfo_x86 *c)
0494 {
0495 intel_init_cmci();
0496 intel_init_lmce();
0497 intel_imc_init(c);
0498 }
0499
0500 void mce_intel_feature_clear(struct cpuinfo_x86 *c)
0501 {
0502 intel_clear_lmce();
0503 }
0504
0505 bool intel_filter_mce(struct mce *m)
0506 {
0507 struct cpuinfo_x86 *c = &boot_cpu_data;
0508
0509
0510 if ((c->x86 == 6) &&
0511 ((c->x86_model == INTEL_FAM6_HASWELL) ||
0512 (c->x86_model == INTEL_FAM6_HASWELL_L) ||
0513 (c->x86_model == INTEL_FAM6_BROADWELL) ||
0514 (c->x86_model == INTEL_FAM6_HASWELL_G) ||
0515 (c->x86_model == INTEL_FAM6_SKYLAKE_X)) &&
0516 (m->bank == 0) &&
0517 ((m->status & 0xa0000000ffffffff) == 0x80000000000f0005))
0518 return true;
0519
0520 return false;
0521 }