Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Intel specific MCE features. |
| 4 | * Copyright 2004 Zwane Mwaikambo <zwane@linuxpower.ca> |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 5 | * Copyright (C) 2008, 2009 Intel Corporation |
| 6 | * Author: Andi Kleen |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 9 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/percpu.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 12 | #include <linux/sched.h> |
Chen, Gong | 27f6c57 | 2014-03-27 21:24:36 -0400 | [diff] [blame] | 13 | #include <linux/cpumask.h> |
H. Peter Anvin | 1bf7b31 | 2009-06-17 08:31:15 -0700 | [diff] [blame] | 14 | #include <asm/apic.h> |
Tony Luck | 3f5a789 | 2016-11-18 09:48:36 -0800 | [diff] [blame] | 15 | #include <asm/cpufeature.h> |
| 16 | #include <asm/intel-family.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/processor.h> |
| 18 | #include <asm/msr.h> |
| 19 | #include <asm/mce.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Borislav Petkov | 21afaf1 | 2018-11-18 15:15:05 +0100 | [diff] [blame] | 21 | #include "internal.h" |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 22 | |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 23 | /* |
| 24 | * Support for Intel Correct Machine Check Interrupts. This allows |
| 25 | * the CPU to raise an interrupt when a corrected machine check happened. |
| 26 | * Normally we pick those up using a regular polling timer. |
| 27 | * Also supports reliable discovery of shared banks. |
| 28 | */ |
| 29 | |
Naveen N. Rao | 0644414 | 2013-06-25 23:58:59 +0530 | [diff] [blame] | 30 | /* |
| 31 | * CMCI can be delivered to multiple cpus that share a machine check bank |
| 32 | * so we need to designate a single cpu to process errors logged in each bank |
| 33 | * in the interrupt handler (otherwise we would have many races and potential |
| 34 | * double reporting of the same error). |
| 35 | * Note that this can change when a cpu is offlined or brought online since |
| 36 | * some MCA banks are shared across cpus. When a cpu is offlined, cmci_clear() |
| 37 | * disables CMCI on all banks owned by the cpu and clears this bitfield. At |
| 38 | * this point, cmci_rediscover() kicks in and a different cpu may end up |
| 39 | * taking ownership of some of the shared MCA banks that were previously |
| 40 | * owned by the offlined cpu. |
| 41 | */ |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 42 | static DEFINE_PER_CPU(mce_banks_t, mce_banks_owned); |
| 43 | |
| 44 | /* |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 45 | * CMCI storm detection backoff counter |
| 46 | * |
| 47 | * During storm, we reset this counter to INITIAL_CHECK_INTERVAL in case we've |
| 48 | * encountered an error. If not, we decrement it by one. We signal the end of |
| 49 | * the CMCI storm when it reaches 0. |
| 50 | */ |
| 51 | static DEFINE_PER_CPU(int, cmci_backoff_cnt); |
| 52 | |
| 53 | /* |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 54 | * cmci_discover_lock protects against parallel discovery attempts |
| 55 | * which could race against each other. |
| 56 | */ |
Thomas Gleixner | ed5c41d3 | 2014-08-05 22:57:19 +0200 | [diff] [blame] | 57 | static DEFINE_RAW_SPINLOCK(cmci_discover_lock); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 58 | |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 59 | #define CMCI_THRESHOLD 1 |
| 60 | #define CMCI_POLL_INTERVAL (30 * HZ) |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 61 | #define CMCI_STORM_INTERVAL (HZ) |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 62 | #define CMCI_STORM_THRESHOLD 15 |
| 63 | |
| 64 | static DEFINE_PER_CPU(unsigned long, cmci_time_stamp); |
| 65 | static DEFINE_PER_CPU(unsigned int, cmci_storm_cnt); |
| 66 | static DEFINE_PER_CPU(unsigned int, cmci_storm_state); |
| 67 | |
| 68 | enum { |
| 69 | CMCI_STORM_NONE, |
| 70 | CMCI_STORM_ACTIVE, |
| 71 | CMCI_STORM_SUBSIDED, |
| 72 | }; |
| 73 | |
| 74 | static atomic_t cmci_storm_on_cpus; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 75 | |
H. Peter Anvin | df20e2e | 2009-02-24 13:19:02 -0800 | [diff] [blame] | 76 | static int cmci_supported(int *banks) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 77 | { |
| 78 | u64 cap; |
| 79 | |
Borislav Petkov | 7af19e4 | 2012-10-15 20:25:17 +0200 | [diff] [blame] | 80 | if (mca_cfg.cmci_disabled || mca_cfg.ignore_ce) |
Hidetoshi Seto | 62fdac5 | 2009-06-11 16:06:07 +0900 | [diff] [blame] | 81 | return 0; |
| 82 | |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 83 | /* |
| 84 | * Vendor check is not strictly needed, but the initial |
| 85 | * initialization is vendor keyed and this |
| 86 | * makes sure none of the backdoors are entered otherwise. |
| 87 | */ |
Tony W Wang-oc | 5a3d56a | 2019-09-18 14:19:32 +0800 | [diff] [blame] | 88 | if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL && |
| 89 | boot_cpu_data.x86_vendor != X86_VENDOR_ZHAOXIN) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 90 | return 0; |
Tony W Wang-oc | 5a3d56a | 2019-09-18 14:19:32 +0800 | [diff] [blame] | 91 | |
Borislav Petkov | 93984fb | 2016-04-04 22:25:00 +0200 | [diff] [blame] | 92 | if (!boot_cpu_has(X86_FEATURE_APIC) || lapic_get_maxlvt() < 6) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 93 | return 0; |
| 94 | rdmsrl(MSR_IA32_MCG_CAP, cap); |
| 95 | *banks = min_t(unsigned, MAX_NR_BANKS, cap & 0xff); |
| 96 | return !!(cap & MCG_CMCI_P); |
| 97 | } |
| 98 | |
Ashok Raj | 88d5386 | 2015-06-04 18:55:23 +0200 | [diff] [blame] | 99 | static bool lmce_supported(void) |
| 100 | { |
| 101 | u64 tmp; |
| 102 | |
| 103 | if (mca_cfg.lmce_disabled) |
| 104 | return false; |
| 105 | |
| 106 | rdmsrl(MSR_IA32_MCG_CAP, tmp); |
| 107 | |
| 108 | /* |
| 109 | * LMCE depends on recovery support in the processor. Hence both |
| 110 | * MCG_SER_P and MCG_LMCE_P should be present in MCG_CAP. |
| 111 | */ |
| 112 | if ((tmp & (MCG_SER_P | MCG_LMCE_P)) != |
| 113 | (MCG_SER_P | MCG_LMCE_P)) |
| 114 | return false; |
| 115 | |
| 116 | /* |
| 117 | * BIOS should indicate support for LMCE by setting bit 20 in |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 118 | * IA32_FEAT_CTL without which touching MCG_EXT_CTL will generate a #GP |
Sean Christopherson | 6d527ce | 2019-12-20 20:44:59 -0800 | [diff] [blame] | 119 | * fault. The MSR must also be locked for LMCE_ENABLED to take effect. |
| 120 | * WARN if the MSR isn't locked as init_ia32_feat_ctl() unconditionally |
| 121 | * locks the MSR in the event that it wasn't already locked by BIOS. |
Ashok Raj | 88d5386 | 2015-06-04 18:55:23 +0200 | [diff] [blame] | 122 | */ |
Sean Christopherson | 32ad73d | 2019-12-20 20:44:55 -0800 | [diff] [blame] | 123 | rdmsrl(MSR_IA32_FEAT_CTL, tmp); |
Sean Christopherson | 6d527ce | 2019-12-20 20:44:59 -0800 | [diff] [blame] | 124 | if (WARN_ON_ONCE(!(tmp & FEAT_CTL_LOCKED))) |
| 125 | return false; |
Ashok Raj | 88d5386 | 2015-06-04 18:55:23 +0200 | [diff] [blame] | 126 | |
Sean Christopherson | 6d527ce | 2019-12-20 20:44:59 -0800 | [diff] [blame] | 127 | return tmp & FEAT_CTL_LMCE_ENABLED; |
Ashok Raj | 88d5386 | 2015-06-04 18:55:23 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 130 | bool mce_intel_cmci_poll(void) |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 131 | { |
| 132 | if (__this_cpu_read(cmci_storm_state) == CMCI_STORM_NONE) |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 133 | return false; |
| 134 | |
| 135 | /* |
| 136 | * Reset the counter if we've logged an error in the last poll |
| 137 | * during the storm. |
| 138 | */ |
Borislav Petkov | 5446735 | 2016-11-10 14:10:53 +0100 | [diff] [blame] | 139 | if (machine_check_poll(0, this_cpu_ptr(&mce_banks_owned))) |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 140 | this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL); |
| 141 | else |
| 142 | this_cpu_dec(cmci_backoff_cnt); |
| 143 | |
| 144 | return true; |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | void mce_intel_hcpu_update(unsigned long cpu) |
| 148 | { |
| 149 | if (per_cpu(cmci_storm_state, cpu) == CMCI_STORM_ACTIVE) |
| 150 | atomic_dec(&cmci_storm_on_cpus); |
| 151 | |
| 152 | per_cpu(cmci_storm_state, cpu) = CMCI_STORM_NONE; |
| 153 | } |
| 154 | |
Xie XiuQi | 1b48465 | 2015-08-12 18:29:41 +0200 | [diff] [blame] | 155 | static void cmci_toggle_interrupt_mode(bool on) |
| 156 | { |
| 157 | unsigned long flags, *owned; |
| 158 | int bank; |
| 159 | u64 val; |
| 160 | |
| 161 | raw_spin_lock_irqsave(&cmci_discover_lock, flags); |
| 162 | owned = this_cpu_ptr(mce_banks_owned); |
| 163 | for_each_set_bit(bank, owned, MAX_NR_BANKS) { |
| 164 | rdmsrl(MSR_IA32_MCx_CTL2(bank), val); |
| 165 | |
| 166 | if (on) |
| 167 | val |= MCI_CTL2_CMCI_EN; |
| 168 | else |
| 169 | val &= ~MCI_CTL2_CMCI_EN; |
| 170 | |
| 171 | wrmsrl(MSR_IA32_MCx_CTL2(bank), val); |
| 172 | } |
| 173 | raw_spin_unlock_irqrestore(&cmci_discover_lock, flags); |
| 174 | } |
| 175 | |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 176 | unsigned long cmci_intel_adjust_timer(unsigned long interval) |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 177 | { |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 178 | if ((this_cpu_read(cmci_backoff_cnt) > 0) && |
| 179 | (__this_cpu_read(cmci_storm_state) == CMCI_STORM_ACTIVE)) { |
| 180 | mce_notify_irq(); |
| 181 | return CMCI_STORM_INTERVAL; |
| 182 | } |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 183 | |
| 184 | switch (__this_cpu_read(cmci_storm_state)) { |
| 185 | case CMCI_STORM_ACTIVE: |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 186 | |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 187 | /* |
| 188 | * We switch back to interrupt mode once the poll timer has |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 189 | * silenced itself. That means no events recorded and the timer |
| 190 | * interval is back to our poll interval. |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 191 | */ |
| 192 | __this_cpu_write(cmci_storm_state, CMCI_STORM_SUBSIDED); |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 193 | if (!atomic_sub_return(1, &cmci_storm_on_cpus)) |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 194 | pr_notice("CMCI storm subsided: switching to interrupt mode\n"); |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 195 | |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 196 | fallthrough; |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 197 | |
| 198 | case CMCI_STORM_SUBSIDED: |
| 199 | /* |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 200 | * We wait for all CPUs to go back to SUBSIDED state. When that |
| 201 | * happens we switch back to interrupt mode. |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 202 | */ |
| 203 | if (!atomic_read(&cmci_storm_on_cpus)) { |
| 204 | __this_cpu_write(cmci_storm_state, CMCI_STORM_NONE); |
Xie XiuQi | 1b48465 | 2015-08-12 18:29:41 +0200 | [diff] [blame] | 205 | cmci_toggle_interrupt_mode(true); |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 206 | cmci_recheck(); |
| 207 | } |
| 208 | return CMCI_POLL_INTERVAL; |
| 209 | default: |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 210 | |
| 211 | /* We have shiny weather. Let the poll do whatever it thinks. */ |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 212 | return interval; |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | static bool cmci_storm_detect(void) |
| 217 | { |
| 218 | unsigned int cnt = __this_cpu_read(cmci_storm_cnt); |
| 219 | unsigned long ts = __this_cpu_read(cmci_time_stamp); |
| 220 | unsigned long now = jiffies; |
| 221 | int r; |
| 222 | |
| 223 | if (__this_cpu_read(cmci_storm_state) != CMCI_STORM_NONE) |
| 224 | return true; |
| 225 | |
| 226 | if (time_before_eq(now, ts + CMCI_STORM_INTERVAL)) { |
| 227 | cnt++; |
| 228 | } else { |
| 229 | cnt = 1; |
| 230 | __this_cpu_write(cmci_time_stamp, now); |
| 231 | } |
| 232 | __this_cpu_write(cmci_storm_cnt, cnt); |
| 233 | |
| 234 | if (cnt <= CMCI_STORM_THRESHOLD) |
| 235 | return false; |
| 236 | |
Xie XiuQi | 1b48465 | 2015-08-12 18:29:41 +0200 | [diff] [blame] | 237 | cmci_toggle_interrupt_mode(false); |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 238 | __this_cpu_write(cmci_storm_state, CMCI_STORM_ACTIVE); |
| 239 | r = atomic_add_return(1, &cmci_storm_on_cpus); |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 240 | mce_timer_kick(CMCI_STORM_INTERVAL); |
| 241 | this_cpu_write(cmci_backoff_cnt, INITIAL_CHECK_INTERVAL); |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 242 | |
| 243 | if (r == 1) |
| 244 | pr_notice("CMCI storm detected: switching to poll mode\n"); |
| 245 | return true; |
| 246 | } |
| 247 | |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 248 | /* |
| 249 | * The interrupt handler. This is called on every event. |
| 250 | * Just call the poller directly to log any events. |
| 251 | * This could in theory increase the threshold under high load, |
| 252 | * but doesn't for now. |
| 253 | */ |
| 254 | static void intel_threshold_interrupt(void) |
| 255 | { |
Chen Gong | 55babd8 | 2012-08-09 11:44:51 -0700 | [diff] [blame] | 256 | if (cmci_storm_detect()) |
| 257 | return; |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 258 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 259 | machine_check_poll(MCP_TIMESTAMP, this_cpu_ptr(&mce_banks_owned)); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 262 | /* |
| 263 | * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks |
| 264 | * on this CPU. Use the algorithm recommended in the SDM to discover shared |
| 265 | * banks. |
| 266 | */ |
Tony Luck | 4670a300 | 2012-08-09 10:59:21 -0700 | [diff] [blame] | 267 | static void cmci_discover(int banks) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 268 | { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 269 | unsigned long *owned = (void *)this_cpu_ptr(&mce_banks_owned); |
Hidetoshi Seto | e529992 | 2009-05-08 17:28:40 +0900 | [diff] [blame] | 270 | unsigned long flags; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 271 | int i; |
Naveen N. Rao | 450cc20 | 2012-09-27 10:08:00 -0700 | [diff] [blame] | 272 | int bios_wrong_thresh = 0; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 273 | |
Thomas Gleixner | ed5c41d3 | 2014-08-05 22:57:19 +0200 | [diff] [blame] | 274 | raw_spin_lock_irqsave(&cmci_discover_lock, flags); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 275 | for (i = 0; i < banks; i++) { |
| 276 | u64 val; |
Naveen N. Rao | 450cc20 | 2012-09-27 10:08:00 -0700 | [diff] [blame] | 277 | int bios_zero_thresh = 0; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 278 | |
| 279 | if (test_bit(i, owned)) |
| 280 | continue; |
| 281 | |
Naveen N. Rao | c3d1fb5 | 2013-07-01 21:08:47 +0530 | [diff] [blame] | 282 | /* Skip banks in firmware first mode */ |
| 283 | if (test_bit(i, mce_banks_ce_disabled)) |
| 284 | continue; |
| 285 | |
Andi Kleen | a2d32bc | 2009-07-09 00:31:44 +0200 | [diff] [blame] | 286 | rdmsrl(MSR_IA32_MCx_CTL2(i), val); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 287 | |
| 288 | /* Already owned by someone else? */ |
Huang Ying | 1f9a0bd | 2010-06-08 14:09:08 +0800 | [diff] [blame] | 289 | if (val & MCI_CTL2_CMCI_EN) { |
Tony Luck | 4670a300 | 2012-08-09 10:59:21 -0700 | [diff] [blame] | 290 | clear_bit(i, owned); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 291 | __clear_bit(i, this_cpu_ptr(mce_poll_banks)); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 292 | continue; |
| 293 | } |
| 294 | |
Borislav Petkov | 1462594 | 2012-10-17 12:05:33 +0200 | [diff] [blame] | 295 | if (!mca_cfg.bios_cmci_threshold) { |
Naveen N. Rao | 450cc20 | 2012-09-27 10:08:00 -0700 | [diff] [blame] | 296 | val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK; |
| 297 | val |= CMCI_THRESHOLD; |
| 298 | } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK)) { |
| 299 | /* |
| 300 | * If bios_cmci_threshold boot option was specified |
| 301 | * but the threshold is zero, we'll try to initialize |
| 302 | * it to 1. |
| 303 | */ |
| 304 | bios_zero_thresh = 1; |
| 305 | val |= CMCI_THRESHOLD; |
| 306 | } |
| 307 | |
| 308 | val |= MCI_CTL2_CMCI_EN; |
Andi Kleen | a2d32bc | 2009-07-09 00:31:44 +0200 | [diff] [blame] | 309 | wrmsrl(MSR_IA32_MCx_CTL2(i), val); |
| 310 | rdmsrl(MSR_IA32_MCx_CTL2(i), val); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 311 | |
| 312 | /* Did the enable bit stick? -- the bank supports CMCI */ |
Huang Ying | 1f9a0bd | 2010-06-08 14:09:08 +0800 | [diff] [blame] | 313 | if (val & MCI_CTL2_CMCI_EN) { |
Tony Luck | 4670a300 | 2012-08-09 10:59:21 -0700 | [diff] [blame] | 314 | set_bit(i, owned); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 315 | __clear_bit(i, this_cpu_ptr(mce_poll_banks)); |
Naveen N. Rao | 450cc20 | 2012-09-27 10:08:00 -0700 | [diff] [blame] | 316 | /* |
| 317 | * We are able to set thresholds for some banks that |
| 318 | * had a threshold of 0. This means the BIOS has not |
| 319 | * set the thresholds properly or does not work with |
| 320 | * this boot option. Note down now and report later. |
| 321 | */ |
Borislav Petkov | 1462594 | 2012-10-17 12:05:33 +0200 | [diff] [blame] | 322 | if (mca_cfg.bios_cmci_threshold && bios_zero_thresh && |
Naveen N. Rao | 450cc20 | 2012-09-27 10:08:00 -0700 | [diff] [blame] | 323 | (val & MCI_CTL2_CMCI_THRESHOLD_MASK)) |
| 324 | bios_wrong_thresh = 1; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 325 | } else { |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 326 | WARN_ON(!test_bit(i, this_cpu_ptr(mce_poll_banks))); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 327 | } |
| 328 | } |
Thomas Gleixner | ed5c41d3 | 2014-08-05 22:57:19 +0200 | [diff] [blame] | 329 | raw_spin_unlock_irqrestore(&cmci_discover_lock, flags); |
Borislav Petkov | 1462594 | 2012-10-17 12:05:33 +0200 | [diff] [blame] | 330 | if (mca_cfg.bios_cmci_threshold && bios_wrong_thresh) { |
Naveen N. Rao | 450cc20 | 2012-09-27 10:08:00 -0700 | [diff] [blame] | 331 | pr_info_once( |
| 332 | "bios_cmci_threshold: Some banks do not have valid thresholds set\n"); |
| 333 | pr_info_once( |
| 334 | "bios_cmci_threshold: Make sure your BIOS supports this boot option\n"); |
| 335 | } |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* |
| 339 | * Just in case we missed an event during initialization check |
| 340 | * all the CMCI owned banks. |
| 341 | */ |
H. Peter Anvin | df20e2e | 2009-02-24 13:19:02 -0800 | [diff] [blame] | 342 | void cmci_recheck(void) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 343 | { |
| 344 | unsigned long flags; |
| 345 | int banks; |
| 346 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 347 | if (!mce_available(raw_cpu_ptr(&cpu_info)) || !cmci_supported(&banks)) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 348 | return; |
Borislav Petkov | 3f2f068 | 2015-01-13 15:08:51 +0100 | [diff] [blame] | 349 | |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 350 | local_irq_save(flags); |
Borislav Petkov | 5446735 | 2016-11-10 14:10:53 +0100 | [diff] [blame] | 351 | machine_check_poll(0, this_cpu_ptr(&mce_banks_owned)); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 352 | local_irq_restore(flags); |
| 353 | } |
| 354 | |
Naveen N. Rao | c3d1fb5 | 2013-07-01 21:08:47 +0530 | [diff] [blame] | 355 | /* Caller must hold the lock on cmci_discover_lock */ |
| 356 | static void __cmci_disable_bank(int bank) |
| 357 | { |
| 358 | u64 val; |
| 359 | |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 360 | if (!test_bit(bank, this_cpu_ptr(mce_banks_owned))) |
Naveen N. Rao | c3d1fb5 | 2013-07-01 21:08:47 +0530 | [diff] [blame] | 361 | return; |
| 362 | rdmsrl(MSR_IA32_MCx_CTL2(bank), val); |
| 363 | val &= ~MCI_CTL2_CMCI_EN; |
| 364 | wrmsrl(MSR_IA32_MCx_CTL2(bank), val); |
Christoph Lameter | 89cbc76 | 2014-08-17 12:30:40 -0500 | [diff] [blame] | 365 | __clear_bit(bank, this_cpu_ptr(mce_banks_owned)); |
Naveen N. Rao | c3d1fb5 | 2013-07-01 21:08:47 +0530 | [diff] [blame] | 366 | } |
| 367 | |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 368 | /* |
| 369 | * Disable CMCI on this CPU for all banks it owns when it goes down. |
| 370 | * This allows other CPUs to claim the banks on rediscovery. |
| 371 | */ |
H. Peter Anvin | df20e2e | 2009-02-24 13:19:02 -0800 | [diff] [blame] | 372 | void cmci_clear(void) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 373 | { |
Hidetoshi Seto | e529992 | 2009-05-08 17:28:40 +0900 | [diff] [blame] | 374 | unsigned long flags; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 375 | int i; |
| 376 | int banks; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 377 | |
| 378 | if (!cmci_supported(&banks)) |
| 379 | return; |
Thomas Gleixner | ed5c41d3 | 2014-08-05 22:57:19 +0200 | [diff] [blame] | 380 | raw_spin_lock_irqsave(&cmci_discover_lock, flags); |
Naveen N. Rao | c3d1fb5 | 2013-07-01 21:08:47 +0530 | [diff] [blame] | 381 | for (i = 0; i < banks; i++) |
| 382 | __cmci_disable_bank(i); |
Thomas Gleixner | ed5c41d3 | 2014-08-05 22:57:19 +0200 | [diff] [blame] | 383 | raw_spin_unlock_irqrestore(&cmci_discover_lock, flags); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 384 | } |
| 385 | |
Srivatsa S. Bhat | 7a0c819 | 2013-03-20 15:31:29 +0530 | [diff] [blame] | 386 | static void cmci_rediscover_work_func(void *arg) |
Tang Chen | 85b9763 | 2012-10-29 11:01:50 +0800 | [diff] [blame] | 387 | { |
| 388 | int banks; |
| 389 | |
| 390 | /* Recheck banks in case CPUs don't all have the same */ |
| 391 | if (cmci_supported(&banks)) |
| 392 | cmci_discover(banks); |
Tang Chen | 85b9763 | 2012-10-29 11:01:50 +0800 | [diff] [blame] | 393 | } |
| 394 | |
Srivatsa S. Bhat | 7a0c819 | 2013-03-20 15:31:29 +0530 | [diff] [blame] | 395 | /* After a CPU went down cycle through all the others and rediscover */ |
| 396 | void cmci_rediscover(void) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 397 | { |
Srivatsa S. Bhat | 7a0c819 | 2013-03-20 15:31:29 +0530 | [diff] [blame] | 398 | int banks; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 399 | |
| 400 | if (!cmci_supported(&banks)) |
| 401 | return; |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 402 | |
Srivatsa S. Bhat | 7a0c819 | 2013-03-20 15:31:29 +0530 | [diff] [blame] | 403 | on_each_cpu(cmci_rediscover_work_func, NULL, 1); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /* |
| 407 | * Reenable CMCI on this CPU in case a CPU down failed. |
| 408 | */ |
| 409 | void cmci_reenable(void) |
| 410 | { |
| 411 | int banks; |
| 412 | if (cmci_supported(&banks)) |
Tony Luck | 4670a300 | 2012-08-09 10:59:21 -0700 | [diff] [blame] | 413 | cmci_discover(banks); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Naveen N. Rao | c3d1fb5 | 2013-07-01 21:08:47 +0530 | [diff] [blame] | 416 | void cmci_disable_bank(int bank) |
| 417 | { |
| 418 | int banks; |
| 419 | unsigned long flags; |
| 420 | |
| 421 | if (!cmci_supported(&banks)) |
| 422 | return; |
| 423 | |
Thomas Gleixner | ed5c41d3 | 2014-08-05 22:57:19 +0200 | [diff] [blame] | 424 | raw_spin_lock_irqsave(&cmci_discover_lock, flags); |
Naveen N. Rao | c3d1fb5 | 2013-07-01 21:08:47 +0530 | [diff] [blame] | 425 | __cmci_disable_bank(bank); |
Thomas Gleixner | ed5c41d3 | 2014-08-05 22:57:19 +0200 | [diff] [blame] | 426 | raw_spin_unlock_irqrestore(&cmci_discover_lock, flags); |
Naveen N. Rao | c3d1fb5 | 2013-07-01 21:08:47 +0530 | [diff] [blame] | 427 | } |
| 428 | |
Tony W Wang-oc | 5a3d56a | 2019-09-18 14:19:32 +0800 | [diff] [blame] | 429 | void intel_init_cmci(void) |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 430 | { |
| 431 | int banks; |
| 432 | |
| 433 | if (!cmci_supported(&banks)) |
| 434 | return; |
| 435 | |
| 436 | mce_threshold_vector = intel_threshold_interrupt; |
Tony Luck | 4670a300 | 2012-08-09 10:59:21 -0700 | [diff] [blame] | 437 | cmci_discover(banks); |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 438 | /* |
| 439 | * For CPU #0 this runs with still disabled APIC, but that's |
| 440 | * ok because only the vector is set up. We still do another |
| 441 | * check for the banks later for CPU #0 just to make sure |
| 442 | * to not miss any events. |
| 443 | */ |
| 444 | apic_write(APIC_LVTCMCI, THRESHOLD_APIC_VECTOR|APIC_DM_FIXED); |
| 445 | cmci_recheck(); |
| 446 | } |
| 447 | |
Tony W Wang-oc | 70f0c23 | 2019-09-18 14:19:33 +0800 | [diff] [blame] | 448 | void intel_init_lmce(void) |
Ashok Raj | 88d5386 | 2015-06-04 18:55:23 +0200 | [diff] [blame] | 449 | { |
| 450 | u64 val; |
| 451 | |
| 452 | if (!lmce_supported()) |
| 453 | return; |
| 454 | |
| 455 | rdmsrl(MSR_IA32_MCG_EXT_CTL, val); |
| 456 | |
| 457 | if (!(val & MCG_EXT_CTL_LMCE_EN)) |
| 458 | wrmsrl(MSR_IA32_MCG_EXT_CTL, val | MCG_EXT_CTL_LMCE_EN); |
| 459 | } |
| 460 | |
Tony W Wang-oc | 70f0c23 | 2019-09-18 14:19:33 +0800 | [diff] [blame] | 461 | void intel_clear_lmce(void) |
Ashok Raj | 8838eb6 | 2015-08-12 18:29:40 +0200 | [diff] [blame] | 462 | { |
| 463 | u64 val; |
| 464 | |
| 465 | if (!lmce_supported()) |
| 466 | return; |
| 467 | |
| 468 | rdmsrl(MSR_IA32_MCG_EXT_CTL, val); |
| 469 | val &= ~MCG_EXT_CTL_LMCE_EN; |
| 470 | wrmsrl(MSR_IA32_MCG_EXT_CTL, val); |
| 471 | } |
| 472 | |
Tony Luck | 3f5a789 | 2016-11-18 09:48:36 -0800 | [diff] [blame] | 473 | static void intel_ppin_init(struct cpuinfo_x86 *c) |
| 474 | { |
| 475 | unsigned long long val; |
| 476 | |
| 477 | /* |
| 478 | * Even if testing the presence of the MSR would be enough, we don't |
| 479 | * want to risk the situation where other models reuse this MSR for |
| 480 | * other purposes. |
| 481 | */ |
| 482 | switch (c->x86_model) { |
| 483 | case INTEL_FAM6_IVYBRIDGE_X: |
| 484 | case INTEL_FAM6_HASWELL_X: |
Peter Zijlstra | 5ebb34e | 2019-08-27 21:48:24 +0200 | [diff] [blame] | 485 | case INTEL_FAM6_BROADWELL_D: |
Tony Luck | 3f5a789 | 2016-11-18 09:48:36 -0800 | [diff] [blame] | 486 | case INTEL_FAM6_BROADWELL_X: |
| 487 | case INTEL_FAM6_SKYLAKE_X: |
Tony Luck | dc6b025 | 2019-10-28 09:37:19 -0700 | [diff] [blame] | 488 | case INTEL_FAM6_ICELAKE_X: |
Tony Luck | e464121 | 2022-01-21 09:47:38 -0800 | [diff] [blame] | 489 | case INTEL_FAM6_ICELAKE_D: |
Tony Luck | a331f5f | 2021-03-19 10:39:19 -0700 | [diff] [blame] | 490 | case INTEL_FAM6_SAPPHIRERAPIDS_X: |
Piotr Luc | 9ea74f7 | 2017-04-13 22:10:56 +0200 | [diff] [blame] | 491 | case INTEL_FAM6_XEON_PHI_KNL: |
| 492 | case INTEL_FAM6_XEON_PHI_KNM: |
| 493 | |
Tony Luck | 3f5a789 | 2016-11-18 09:48:36 -0800 | [diff] [blame] | 494 | if (rdmsrl_safe(MSR_PPIN_CTL, &val)) |
| 495 | return; |
| 496 | |
| 497 | if ((val & 3UL) == 1UL) { |
Tony Luck | 59b5809 | 2020-02-25 17:17:37 -0800 | [diff] [blame] | 498 | /* PPIN locked in disabled mode */ |
Tony Luck | 3f5a789 | 2016-11-18 09:48:36 -0800 | [diff] [blame] | 499 | return; |
| 500 | } |
| 501 | |
Tony Luck | 59b5809 | 2020-02-25 17:17:37 -0800 | [diff] [blame] | 502 | /* If PPIN is disabled, try to enable */ |
| 503 | if (!(val & 2UL)) { |
Tony Luck | 3f5a789 | 2016-11-18 09:48:36 -0800 | [diff] [blame] | 504 | wrmsrl_safe(MSR_PPIN_CTL, val | 2UL); |
| 505 | rdmsrl_safe(MSR_PPIN_CTL, &val); |
| 506 | } |
| 507 | |
Tony Luck | 59b5809 | 2020-02-25 17:17:37 -0800 | [diff] [blame] | 508 | /* Is the enable bit set? */ |
| 509 | if (val & 2UL) |
Tony Luck | 3f5a789 | 2016-11-18 09:48:36 -0800 | [diff] [blame] | 510 | set_cpu_cap(c, X86_FEATURE_INTEL_PPIN); |
| 511 | } |
| 512 | } |
| 513 | |
Tony Luck | 68299a4 | 2020-10-30 12:04:00 -0700 | [diff] [blame] | 514 | /* |
| 515 | * Enable additional error logs from the integrated |
| 516 | * memory controller on processors that support this. |
| 517 | */ |
| 518 | static void intel_imc_init(struct cpuinfo_x86 *c) |
| 519 | { |
| 520 | u64 error_control; |
| 521 | |
| 522 | switch (c->x86_model) { |
| 523 | case INTEL_FAM6_SANDYBRIDGE_X: |
| 524 | case INTEL_FAM6_IVYBRIDGE_X: |
| 525 | case INTEL_FAM6_HASWELL_X: |
Tony Luck | 098416e | 2020-11-10 16:39:54 -0800 | [diff] [blame] | 526 | if (rdmsrl_safe(MSR_ERROR_CONTROL, &error_control)) |
| 527 | return; |
Tony Luck | 68299a4 | 2020-10-30 12:04:00 -0700 | [diff] [blame] | 528 | error_control |= 2; |
Tony Luck | 098416e | 2020-11-10 16:39:54 -0800 | [diff] [blame] | 529 | wrmsrl_safe(MSR_ERROR_CONTROL, error_control); |
Tony Luck | 68299a4 | 2020-10-30 12:04:00 -0700 | [diff] [blame] | 530 | break; |
| 531 | } |
| 532 | } |
| 533 | |
H. Peter Anvin | cc3ca22 | 2009-02-20 23:35:51 -0800 | [diff] [blame] | 534 | void mce_intel_feature_init(struct cpuinfo_x86 *c) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | { |
Andi Kleen | 88ccbed | 2009-02-12 13:49:36 +0100 | [diff] [blame] | 536 | intel_init_cmci(); |
Ashok Raj | 243d657 | 2015-06-04 18:55:24 +0200 | [diff] [blame] | 537 | intel_init_lmce(); |
Tony Luck | 3f5a789 | 2016-11-18 09:48:36 -0800 | [diff] [blame] | 538 | intel_ppin_init(c); |
Tony Luck | 68299a4 | 2020-10-30 12:04:00 -0700 | [diff] [blame] | 539 | intel_imc_init(c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | } |
Ashok Raj | 8838eb6 | 2015-08-12 18:29:40 +0200 | [diff] [blame] | 541 | |
| 542 | void mce_intel_feature_clear(struct cpuinfo_x86 *c) |
| 543 | { |
| 544 | intel_clear_lmce(); |
| 545 | } |
Prarit Bhargava | 2976908 | 2020-02-19 08:16:11 -0500 | [diff] [blame] | 546 | |
| 547 | bool intel_filter_mce(struct mce *m) |
| 548 | { |
| 549 | struct cpuinfo_x86 *c = &boot_cpu_data; |
| 550 | |
Dave Jones | e629fc1 | 2021-10-29 16:57:59 -0400 | [diff] [blame] | 551 | /* MCE errata HSD131, HSM142, HSW131, BDM48, HSM142 and SKX37 */ |
Prarit Bhargava | 2976908 | 2020-02-19 08:16:11 -0500 | [diff] [blame] | 552 | if ((c->x86 == 6) && |
| 553 | ((c->x86_model == INTEL_FAM6_HASWELL) || |
| 554 | (c->x86_model == INTEL_FAM6_HASWELL_L) || |
| 555 | (c->x86_model == INTEL_FAM6_BROADWELL) || |
Dave Jones | e629fc1 | 2021-10-29 16:57:59 -0400 | [diff] [blame] | 556 | (c->x86_model == INTEL_FAM6_HASWELL_G) || |
| 557 | (c->x86_model == INTEL_FAM6_SKYLAKE_X)) && |
Prarit Bhargava | 2976908 | 2020-02-19 08:16:11 -0500 | [diff] [blame] | 558 | (m->bank == 0) && |
| 559 | ((m->status & 0xa0000000ffffffff) == 0x80000000000f0005)) |
| 560 | return true; |
| 561 | |
| 562 | return false; |
| 563 | } |