@@ -54,8 +54,27 @@ static DEFINE_RAW_SPINLOCK(cmci_discover_lock);
5454 */
5555static DEFINE_SPINLOCK (cmci_poll_lock );
5656
57+ /* Linux non-storm CMCI threshold (may be overridden by BIOS) */
5758#define CMCI_THRESHOLD 1
5859
60+ /*
61+ * MCi_CTL2 threshold for each bank when there is no storm.
62+ * Default value for each bank may have been set by BIOS.
63+ */
64+ static u16 cmci_threshold [MAX_NR_BANKS ];
65+
66+ /*
67+ * High threshold to limit CMCI rate during storms. Max supported is
68+ * 0x7FFF. Use this slightly smaller value so it has a distinctive
69+ * signature when some asks "Why am I not seeing all corrected errors?"
70+ * A high threshold is used instead of just disabling CMCI for a
71+ * bank because both corrected and uncorrected errors may be logged
72+ * in the same bank and signalled with CMCI. The threshold only applies
73+ * to corrected errors, so keeping CMCI enabled means that uncorrected
74+ * errors will still be processed in a timely fashion.
75+ */
76+ #define CMCI_STORM_THRESHOLD 32749
77+
5978static int cmci_supported (int * banks )
6079{
6180 u64 cap ;
@@ -110,6 +129,31 @@ static bool lmce_supported(void)
110129 return tmp & FEAT_CTL_LMCE_ENABLED ;
111130}
112131
132+ /*
133+ * Set a new CMCI threshold value. Preserve the state of the
134+ * MCI_CTL2_CMCI_EN bit in case this happens during a
135+ * cmci_rediscover() operation.
136+ */
137+ static void cmci_set_threshold (int bank , int thresh )
138+ {
139+ unsigned long flags ;
140+ u64 val ;
141+
142+ raw_spin_lock_irqsave (& cmci_discover_lock , flags );
143+ rdmsrl (MSR_IA32_MCx_CTL2 (bank ), val );
144+ val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK ;
145+ wrmsrl (MSR_IA32_MCx_CTL2 (bank ), val | thresh );
146+ raw_spin_unlock_irqrestore (& cmci_discover_lock , flags );
147+ }
148+
149+ void mce_intel_handle_storm (int bank , bool on )
150+ {
151+ if (on )
152+ cmci_set_threshold (bank , CMCI_STORM_THRESHOLD );
153+ else
154+ cmci_set_threshold (bank , cmci_threshold [bank ]);
155+ }
156+
113157/*
114158 * The interrupt handler. This is called on every event.
115159 * Just call the poller directly to log any events.
@@ -121,72 +165,130 @@ static void intel_threshold_interrupt(void)
121165 machine_check_poll (MCP_TIMESTAMP , this_cpu_ptr (& mce_banks_owned ));
122166}
123167
168+ /*
169+ * Check all the reasons why current CPU cannot claim
170+ * ownership of a bank.
171+ * 1: CPU already owns this bank
172+ * 2: BIOS owns this bank
173+ * 3: Some other CPU owns this bank
174+ */
175+ static bool cmci_skip_bank (int bank , u64 * val )
176+ {
177+ unsigned long * owned = (void * )this_cpu_ptr (& mce_banks_owned );
178+
179+ if (test_bit (bank , owned ))
180+ return true;
181+
182+ /* Skip banks in firmware first mode */
183+ if (test_bit (bank , mce_banks_ce_disabled ))
184+ return true;
185+
186+ rdmsrl (MSR_IA32_MCx_CTL2 (bank ), * val );
187+
188+ /* Already owned by someone else? */
189+ if (* val & MCI_CTL2_CMCI_EN ) {
190+ clear_bit (bank , owned );
191+ __clear_bit (bank , this_cpu_ptr (mce_poll_banks ));
192+ return true;
193+ }
194+
195+ return false;
196+ }
197+
198+ /*
199+ * Decide which CMCI interrupt threshold to use:
200+ * 1: If this bank is in storm mode from whichever CPU was
201+ * the previous owner, stay in storm mode.
202+ * 2: If ignoring any threshold set by BIOS, set Linux default
203+ * 3: Try to honor BIOS threshold (unless buggy BIOS set it at zero).
204+ */
205+ static u64 cmci_pick_threshold (u64 val , int * bios_zero_thresh )
206+ {
207+ if ((val & MCI_CTL2_CMCI_THRESHOLD_MASK ) == CMCI_STORM_THRESHOLD )
208+ return val ;
209+
210+ if (!mca_cfg .bios_cmci_threshold ) {
211+ val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK ;
212+ val |= CMCI_THRESHOLD ;
213+ } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK )) {
214+ /*
215+ * If bios_cmci_threshold boot option was specified
216+ * but the threshold is zero, we'll try to initialize
217+ * it to 1.
218+ */
219+ * bios_zero_thresh = 1 ;
220+ val |= CMCI_THRESHOLD ;
221+ }
222+
223+ return val ;
224+ }
225+
226+ /*
227+ * Try to claim ownership of a bank.
228+ */
229+ static void cmci_claim_bank (int bank , u64 val , int bios_zero_thresh , int * bios_wrong_thresh )
230+ {
231+ struct mca_storm_desc * storm = this_cpu_ptr (& storm_desc );
232+
233+ val |= MCI_CTL2_CMCI_EN ;
234+ wrmsrl (MSR_IA32_MCx_CTL2 (bank ), val );
235+ rdmsrl (MSR_IA32_MCx_CTL2 (bank ), val );
236+
237+ /* If the enable bit did not stick, this bank should be polled. */
238+ if (!(val & MCI_CTL2_CMCI_EN )) {
239+ WARN_ON (!test_bit (bank , this_cpu_ptr (mce_poll_banks )));
240+ storm -> banks [bank ].poll_only = true;
241+ return ;
242+ }
243+
244+ /* This CPU successfully set the enable bit. */
245+ set_bit (bank , (void * )this_cpu_ptr (& mce_banks_owned ));
246+
247+ if ((val & MCI_CTL2_CMCI_THRESHOLD_MASK ) == CMCI_STORM_THRESHOLD ) {
248+ pr_notice ("CPU%d BANK%d CMCI inherited storm\n" , smp_processor_id (), bank );
249+ mce_inherit_storm (bank );
250+ cmci_storm_begin (bank );
251+ } else {
252+ __clear_bit (bank , this_cpu_ptr (mce_poll_banks ));
253+ }
254+
255+ /*
256+ * We are able to set thresholds for some banks that
257+ * had a threshold of 0. This means the BIOS has not
258+ * set the thresholds properly or does not work with
259+ * this boot option. Note down now and report later.
260+ */
261+ if (mca_cfg .bios_cmci_threshold && bios_zero_thresh &&
262+ (val & MCI_CTL2_CMCI_THRESHOLD_MASK ))
263+ * bios_wrong_thresh = 1 ;
264+
265+ /* Save default threshold for each bank */
266+ if (cmci_threshold [bank ] == 0 )
267+ cmci_threshold [bank ] = val & MCI_CTL2_CMCI_THRESHOLD_MASK ;
268+ }
269+
124270/*
125271 * Enable CMCI (Corrected Machine Check Interrupt) for available MCE banks
126272 * on this CPU. Use the algorithm recommended in the SDM to discover shared
127- * banks.
273+ * banks. Called during initial bootstrap, and also for hotplug CPU operations
274+ * to rediscover/reassign machine check banks.
128275 */
129276static void cmci_discover (int banks )
130277{
131- unsigned long * owned = ( void * ) this_cpu_ptr ( & mce_banks_owned ) ;
278+ int bios_wrong_thresh = 0 ;
132279 unsigned long flags ;
133280 int i ;
134- int bios_wrong_thresh = 0 ;
135281
136282 raw_spin_lock_irqsave (& cmci_discover_lock , flags );
137283 for (i = 0 ; i < banks ; i ++ ) {
138284 u64 val ;
139285 int bios_zero_thresh = 0 ;
140286
141- if (test_bit (i , owned ))
142- continue ;
143-
144- /* Skip banks in firmware first mode */
145- if (test_bit (i , mce_banks_ce_disabled ))
287+ if (cmci_skip_bank (i , & val ))
146288 continue ;
147289
148- rdmsrl (MSR_IA32_MCx_CTL2 (i ), val );
149-
150- /* Already owned by someone else? */
151- if (val & MCI_CTL2_CMCI_EN ) {
152- clear_bit (i , owned );
153- __clear_bit (i , this_cpu_ptr (mce_poll_banks ));
154- continue ;
155- }
156-
157- if (!mca_cfg .bios_cmci_threshold ) {
158- val &= ~MCI_CTL2_CMCI_THRESHOLD_MASK ;
159- val |= CMCI_THRESHOLD ;
160- } else if (!(val & MCI_CTL2_CMCI_THRESHOLD_MASK )) {
161- /*
162- * If bios_cmci_threshold boot option was specified
163- * but the threshold is zero, we'll try to initialize
164- * it to 1.
165- */
166- bios_zero_thresh = 1 ;
167- val |= CMCI_THRESHOLD ;
168- }
169-
170- val |= MCI_CTL2_CMCI_EN ;
171- wrmsrl (MSR_IA32_MCx_CTL2 (i ), val );
172- rdmsrl (MSR_IA32_MCx_CTL2 (i ), val );
173-
174- /* Did the enable bit stick? -- the bank supports CMCI */
175- if (val & MCI_CTL2_CMCI_EN ) {
176- set_bit (i , owned );
177- __clear_bit (i , this_cpu_ptr (mce_poll_banks ));
178- /*
179- * We are able to set thresholds for some banks that
180- * had a threshold of 0. This means the BIOS has not
181- * set the thresholds properly or does not work with
182- * this boot option. Note down now and report later.
183- */
184- if (mca_cfg .bios_cmci_threshold && bios_zero_thresh &&
185- (val & MCI_CTL2_CMCI_THRESHOLD_MASK ))
186- bios_wrong_thresh = 1 ;
187- } else {
188- WARN_ON (!test_bit (i , this_cpu_ptr (mce_poll_banks )));
189- }
290+ val = cmci_pick_threshold (val , & bios_zero_thresh );
291+ cmci_claim_bank (i , val , bios_zero_thresh , & bios_wrong_thresh );
190292 }
191293 raw_spin_unlock_irqrestore (& cmci_discover_lock , flags );
192294 if (mca_cfg .bios_cmci_threshold && bios_wrong_thresh ) {
@@ -225,6 +327,9 @@ static void __cmci_disable_bank(int bank)
225327 val &= ~MCI_CTL2_CMCI_EN ;
226328 wrmsrl (MSR_IA32_MCx_CTL2 (bank ), val );
227329 __clear_bit (bank , this_cpu_ptr (mce_banks_owned ));
330+
331+ if ((val & MCI_CTL2_CMCI_THRESHOLD_MASK ) == CMCI_STORM_THRESHOLD )
332+ cmci_storm_end (bank );
228333}
229334
230335/*
0 commit comments