xref: /linux/arch/x86/kernel/cpu/mce/amd.c (revision d734667216e3fe13f0d81231f0e5691816ec42f4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  (c) 2005-2016 Advanced Micro Devices, Inc.
4  *
5  *  Written by Jacob Shin - AMD, Inc.
6  *  Maintained by: Borislav Petkov <bp@alien8.de>
7  */
8 #include <linux/interrupt.h>
9 #include <linux/notifier.h>
10 #include <linux/kobject.h>
11 #include <linux/percpu.h>
12 #include <linux/errno.h>
13 #include <linux/sched.h>
14 #include <linux/sysfs.h>
15 #include <linux/slab.h>
16 #include <linux/init.h>
17 #include <linux/cpu.h>
18 #include <linux/smp.h>
19 #include <linux/string.h>
20 
21 #include <asm/traps.h>
22 #include <asm/apic.h>
23 #include <asm/mce.h>
24 #include <asm/msr.h>
25 #include <asm/trace/irq_vectors.h>
26 
27 #include "internal.h"
28 
29 #define NR_BLOCKS         5
30 #define THRESHOLD_MAX     0xFFF
31 #define INT_TYPE_APIC     0x00020000
32 #define MASK_VALID_HI     0x80000000
33 #define MASK_CNTP_HI      0x40000000
34 #define MASK_LOCKED_HI    0x20000000
35 #define MASK_LVTOFF_HI    0x00F00000
36 #define MASK_COUNT_EN_HI  0x00080000
37 #define MASK_INT_TYPE_HI  0x00060000
38 #define MASK_OVERFLOW_HI  0x00010000
39 #define MASK_ERR_COUNT_HI 0x00000FFF
40 #define MASK_BLKPTR_LO    0xFF000000
41 #define MCG_XBLK_ADDR     0xC0000400
42 
43 /* Deferred error settings */
44 #define MSR_CU_DEF_ERR		0xC0000410
45 #define MASK_DEF_LVTOFF		0x000000F0
46 
47 /* Scalable MCA: */
48 
49 /* Threshold LVT offset is at MSR0xC0000410[15:12] */
50 #define SMCA_THR_LVT_OFF	0xF000
51 
52 static bool thresholding_irq_en;
53 
54 struct mce_amd_cpu_data {
55 	mce_banks_t     thr_intr_banks;
56 	mce_banks_t     dfr_intr_banks;
57 
58 	u32		thr_intr_en: 1,
59 			dfr_intr_en: 1,
60 			__resv: 30;
61 };
62 
63 static DEFINE_PER_CPU_READ_MOSTLY(struct mce_amd_cpu_data, mce_amd_data);
64 
65 static const char * const th_names[] = {
66 	"load_store",
67 	"insn_fetch",
68 	"combined_unit",
69 	"decode_unit",
70 	"northbridge",
71 	"execution_unit",
72 };
73 
74 static const char * const smca_umc_block_names[] = {
75 	"dram_ecc",
76 	"misc_umc"
77 };
78 
79 #define HWID_MCATYPE(hwid, mcatype) (((hwid) << 16) | (mcatype))
80 
81 struct smca_hwid {
82 	unsigned int bank_type;	/* Use with smca_bank_types for easy indexing. */
83 	u32 hwid_mcatype;	/* (hwid,mcatype) tuple */
84 };
85 
86 struct smca_bank {
87 	const struct smca_hwid *hwid;
88 	u32 id;			/* Value of MCA_IPID[InstanceId]. */
89 	u8 sysfs_id;		/* Value used for sysfs name. */
90 	u64 paddrv	:1,	/* Physical Address Valid bit in MCA_CONFIG */
91 	    __reserved	:63;
92 };
93 
94 static DEFINE_PER_CPU_READ_MOSTLY(struct smca_bank[MAX_NR_BANKS], smca_banks);
95 static DEFINE_PER_CPU_READ_MOSTLY(u8[N_SMCA_BANK_TYPES], smca_bank_counts);
96 
97 static const char * const smca_names[] = {
98 	[SMCA_CS ... SMCA_CS_V2]	= "coherent_station",
99 	[SMCA_DACC_BE]			= "dacc_be",
100 	[SMCA_DACC_FE]			= "dacc_fe",
101 	[SMCA_DE]			= "decode_unit",
102 	[SMCA_EDDR5CMN]			= "eddr5_cmn",
103 	[SMCA_EX]			= "execution_unit",
104 	[SMCA_FP]			= "floating_point",
105 	[SMCA_GMI_PCS]			= "gmi_pcs",
106 	[SMCA_GMI_PHY]			= "gmi_phy",
107 	[SMCA_IF]			= "insn_fetch",
108 	[SMCA_L2_CACHE]			= "l2_cache",
109 	[SMCA_L3_CACHE]			= "l3_cache",
110 	[SMCA_LS ... SMCA_LS_V2]	= "load_store",
111 	[SMCA_MA_LLC]			= "ma_llc",
112 	[SMCA_MP5]			= "mp5",
113 	[SMCA_MPART]			= "mpart",
114 	[SMCA_MPASP ... SMCA_MPASP_V2]	= "mpasp",
115 	[SMCA_MPDACC]			= "mpdacc",
116 	[SMCA_MPDMA]			= "mpdma",
117 	[SMCA_MPM]			= "mpm",
118 	[SMCA_MPRAS]			= "mpras",
119 	[SMCA_NBIF]			= "nbif",
120 	[SMCA_NBIO]			= "nbio",
121 	[SMCA_PB]			= "param_block",
122 	[SMCA_PCIE ... SMCA_PCIE_V2]	= "pcie",
123 	[SMCA_PCIE_PL]			= "pcie_pl",
124 	[SMCA_PIE]			= "pie",
125 	[SMCA_PSP ... SMCA_PSP_V2]	= "psp",
126 	[SMCA_RESERVED]			= "reserved",
127 	[SMCA_SATA]			= "sata",
128 	[SMCA_SHUB]			= "shub",
129 	[SMCA_SMU ... SMCA_SMU_V2]	= "smu",
130 	[SMCA_SSBDCI]			= "ssbdci",
131 
132 	/* UMC v2 is separate because both of them can exist in a single system. */
133 	[SMCA_UMC]			= "umc",
134 	[SMCA_UMC_V2]			= "umc_v2",
135 	[SMCA_USB]			= "usb",
136 	[SMCA_USR_CP]			= "usr_cp",
137 	[SMCA_USR_DP]			= "usr_dp",
138 	[SMCA_WAFL_PHY]			= "wafl_phy",
139 	[SMCA_XGMI_PCS]			= "xgmi_pcs",
140 	[SMCA_XGMI_PHY]			= "xgmi_phy",
141 };
142 
143 static const char *smca_get_name(enum smca_bank_types t)
144 {
145 	if (t >= N_SMCA_BANK_TYPES)
146 		return NULL;
147 
148 	return smca_names[t];
149 }
150 
151 enum smca_bank_types smca_get_bank_type(unsigned int cpu, unsigned int bank)
152 {
153 	struct smca_bank *b;
154 
155 	if (bank >= MAX_NR_BANKS)
156 		return N_SMCA_BANK_TYPES;
157 
158 	b = &per_cpu(smca_banks, cpu)[bank];
159 	if (!b->hwid)
160 		return N_SMCA_BANK_TYPES;
161 
162 	return b->hwid->bank_type;
163 }
164 EXPORT_SYMBOL_GPL(smca_get_bank_type);
165 
166 /*
167  * Format:
168  * { bank_type, hwid_mcatype }
169  *
170  * alphanumerically sorted by bank type.
171  */
172 static const struct smca_hwid smca_hwid_mcatypes[] = {
173 	{ SMCA_CS,	 HWID_MCATYPE(0x2E, 0x0)	},
174 	{ SMCA_CS_V2,	 HWID_MCATYPE(0x2E, 0x2)	},
175 	{ SMCA_DACC_BE,	 HWID_MCATYPE(0x164, 0x0)	},
176 	{ SMCA_DACC_FE,	 HWID_MCATYPE(0x157, 0x0)	},
177 	{ SMCA_DE,	 HWID_MCATYPE(0xB0, 0x3)	},
178 	{ SMCA_EDDR5CMN, HWID_MCATYPE(0x1E0, 0x0)	},
179 	{ SMCA_EX,	 HWID_MCATYPE(0xB0, 0x5)	},
180 	{ SMCA_FP,	 HWID_MCATYPE(0xB0, 0x6)	},
181 	{ SMCA_GMI_PCS,  HWID_MCATYPE(0x241, 0x0)	},
182 	{ SMCA_GMI_PHY,	 HWID_MCATYPE(0x269, 0x0)	},
183 	{ SMCA_IF,	 HWID_MCATYPE(0xB0, 0x1)	},
184 	{ SMCA_L2_CACHE, HWID_MCATYPE(0xB0, 0x2)	},
185 	{ SMCA_L3_CACHE, HWID_MCATYPE(0xB0, 0x7)	},
186 	{ SMCA_LS,	 HWID_MCATYPE(0xB0, 0x0)	},
187 	{ SMCA_LS_V2,	 HWID_MCATYPE(0xB0, 0x10)	},
188 	{ SMCA_MA_LLC,	 HWID_MCATYPE(0x2E, 0x4)	},
189 	{ SMCA_MP5,	 HWID_MCATYPE(0x01, 0x2)	},
190 	{ SMCA_MPART,	 HWID_MCATYPE(0xFF, 0x2)	},
191 	{ SMCA_MPASP,	 HWID_MCATYPE(0xFD, 0x0)	},
192 	{ SMCA_MPASP_V2, HWID_MCATYPE(0xFD, 0x1)	},
193 	{ SMCA_MPDACC,	 HWID_MCATYPE(0xBE, 0x0)	},
194 	{ SMCA_MPDMA,	 HWID_MCATYPE(0x01, 0x3)	},
195 	{ SMCA_MPM,	 HWID_MCATYPE(0xF9, 0x0)	},
196 	{ SMCA_MPRAS,	 HWID_MCATYPE(0x12, 0x0)	},
197 	{ SMCA_NBIF,	 HWID_MCATYPE(0x6C, 0x0)	},
198 	{ SMCA_NBIO,	 HWID_MCATYPE(0x18, 0x0)	},
199 	{ SMCA_PB,	 HWID_MCATYPE(0x05, 0x0)	},
200 	{ SMCA_PCIE,	 HWID_MCATYPE(0x46, 0x0)	},
201 	{ SMCA_PCIE_V2,	 HWID_MCATYPE(0x46, 0x1)	},
202 	{ SMCA_PCIE_PL,	 HWID_MCATYPE(0x1E1, 0x0)	},
203 	{ SMCA_PIE,	 HWID_MCATYPE(0x2E, 0x1)	},
204 	{ SMCA_PSP,	 HWID_MCATYPE(0xFF, 0x0)	},
205 	{ SMCA_PSP_V2,	 HWID_MCATYPE(0xFF, 0x1)	},
206 	{ SMCA_RESERVED, HWID_MCATYPE(0x00, 0x0)	},
207 	{ SMCA_SATA,	 HWID_MCATYPE(0xA8, 0x0)	},
208 	{ SMCA_SHUB,	 HWID_MCATYPE(0x80, 0x0)	},
209 	{ SMCA_SMU,	 HWID_MCATYPE(0x01, 0x0)	},
210 	{ SMCA_SMU_V2,	 HWID_MCATYPE(0x01, 0x1)	},
211 	{ SMCA_SSBDCI,	 HWID_MCATYPE(0x5C, 0x0)	},
212 	{ SMCA_UMC,	 HWID_MCATYPE(0x96, 0x0)	},
213 	{ SMCA_UMC_V2,	 HWID_MCATYPE(0x96, 0x1)	},
214 	{ SMCA_USB,	 HWID_MCATYPE(0xAA, 0x0)	},
215 	{ SMCA_USR_CP,	 HWID_MCATYPE(0x180, 0x0)	},
216 	{ SMCA_USR_DP,	 HWID_MCATYPE(0x170, 0x0)	},
217 	{ SMCA_WAFL_PHY, HWID_MCATYPE(0x267, 0x0)	},
218 	{ SMCA_XGMI_PCS, HWID_MCATYPE(0x50, 0x0)	},
219 	{ SMCA_XGMI_PHY, HWID_MCATYPE(0x259, 0x0)	},
220 };
221 
222 /*
223  * In SMCA enabled processors, we can have multiple banks for a given IP type.
224  * So to define a unique name for each bank, we use a temp c-string to append
225  * the MCA_IPID[InstanceId] to type's name in get_name().
226  *
227  * InstanceId is 32 bits which is 8 characters. Make sure MAX_MCATYPE_NAME_LEN
228  * is greater than 8 plus 1 (for underscore) plus length of longest type name.
229  */
230 #define MAX_MCATYPE_NAME_LEN	30
231 static char buf_mcatype[MAX_MCATYPE_NAME_LEN];
232 
233 struct threshold_block {
234 	/* This block's number within its bank. */
235 	unsigned int		block;
236 	/* MCA bank number that contains this block. */
237 	unsigned int		bank;
238 	/* CPU which controls this block's MCA bank. */
239 	unsigned int		cpu;
240 	/* MCA_MISC MSR address for this block. */
241 	u32			address;
242 	/* Enable/Disable APIC interrupt. */
243 	bool			interrupt_enable;
244 	/* Bank can generate an interrupt. */
245 	bool			interrupt_capable;
246 	/* Value upon which threshold interrupt is generated. */
247 	u16			threshold_limit;
248 	/* sysfs object */
249 	struct kobject		kobj;
250 	/* List of threshold blocks within this block's MCA bank. */
251 	struct list_head	miscj;
252 };
253 
254 struct threshold_bank {
255 	struct kobject		*kobj;
256 	/* List of threshold blocks within this MCA bank. */
257 	struct list_head	miscj;
258 };
259 
260 static DEFINE_PER_CPU(struct threshold_bank **, threshold_banks);
261 
262 /*
263  * A list of the banks enabled on each logical CPU. Controls which respective
264  * descriptors to initialize later in mce_threshold_create_device().
265  */
266 static DEFINE_PER_CPU(u64, bank_map);
267 
268 static void amd_threshold_interrupt(void);
269 static void amd_deferred_error_interrupt(void);
270 
271 static void default_deferred_error_interrupt(void)
272 {
273 	pr_err("Unexpected deferred interrupt at vector %x\n", DEFERRED_ERROR_VECTOR);
274 }
275 void (*deferred_error_int_vector)(void) = default_deferred_error_interrupt;
276 
277 static void smca_configure(unsigned int bank, unsigned int cpu)
278 {
279 	struct mce_amd_cpu_data *data = this_cpu_ptr(&mce_amd_data);
280 	u8 *bank_counts = this_cpu_ptr(smca_bank_counts);
281 	const struct smca_hwid *s_hwid;
282 	unsigned int i, hwid_mcatype;
283 	struct msr val;
284 	u32 smca_config = MSR_AMD64_SMCA_MCx_CONFIG(bank);
285 
286 	/* Set appropriate bits in MCA_CONFIG */
287 	if (!rdmsrq_safe(smca_config, &val.q)) {
288 		/*
289 		 * OS is required to set the MCAX bit to acknowledge that it is
290 		 * now using the new MSR ranges and new registers under each
291 		 * bank. It also means that the OS will configure deferred
292 		 * errors in the new MCx_CONFIG register. If the bit is not set,
293 		 * uncorrectable errors will cause a system panic.
294 		 *
295 		 * MCA_CONFIG[MCAX] is bit 32 (0 in the high portion of the MSR.)
296 		 */
297 		val.h |= BIT(0);
298 
299 		/*
300 		 * SMCA sets the Deferred Error Interrupt type per bank.
301 		 *
302 		 * MCA_CONFIG[DeferredIntTypeSupported] is bit 5, and tells us
303 		 * if the DeferredIntType bit field is available.
304 		 *
305 		 * MCA_CONFIG[DeferredIntType] is bits [38:37] ([6:5] in the
306 		 * high portion of the MSR). OS should set this to 0x1 to enable
307 		 * APIC based interrupt. First, check that no interrupt has been
308 		 * set.
309 		 */
310 		if ((val.l & BIT(5)) && !((val.h >> 5) & 0x3) && data->dfr_intr_en) {
311 			__set_bit(bank, data->dfr_intr_banks);
312 			val.h |= BIT(5);
313 		}
314 
315 		/*
316 		 * SMCA Corrected Error Interrupt
317 		 *
318 		 * MCA_CONFIG[IntPresent] is bit 10, and tells us if the bank can
319 		 * send an MCA Thresholding interrupt without the OS initializing
320 		 * this feature. This can be used if the threshold limit is managed
321 		 * by the platform.
322 		 *
323 		 * MCA_CONFIG[IntEn] is bit 40 (8 in the high portion of the MSR).
324 		 * The OS should set this to inform the platform that the OS is ready
325 		 * to handle the MCA Thresholding interrupt.
326 		 */
327 		if ((val.l & BIT(10)) && data->thr_intr_en) {
328 			__set_bit(bank, data->thr_intr_banks);
329 			val.h |= BIT(8);
330 		}
331 
332 		this_cpu_ptr(mce_banks_array)[bank].lsb_in_status = !!(val.l & BIT(8));
333 
334 		if (val.l & MCI_CONFIG_PADDRV)
335 			this_cpu_ptr(smca_banks)[bank].paddrv = 1;
336 
337 		wrmsrq(smca_config, val.q);
338 	}
339 
340 	if (rdmsrq_safe(MSR_AMD64_SMCA_MCx_IPID(bank), &val.q)) {
341 		pr_warn("Failed to read MCA_IPID for bank %d\n", bank);
342 		return;
343 	}
344 
345 	hwid_mcatype = HWID_MCATYPE(val.h & MCI_IPID_HWID,
346 				    (val.h & MCI_IPID_MCATYPE) >> 16);
347 
348 	for (i = 0; i < ARRAY_SIZE(smca_hwid_mcatypes); i++) {
349 		s_hwid = &smca_hwid_mcatypes[i];
350 
351 		if (hwid_mcatype == s_hwid->hwid_mcatype) {
352 			this_cpu_ptr(smca_banks)[bank].hwid = s_hwid;
353 			this_cpu_ptr(smca_banks)[bank].id = val.l;
354 			this_cpu_ptr(smca_banks)[bank].sysfs_id = bank_counts[s_hwid->bank_type]++;
355 			break;
356 		}
357 	}
358 }
359 
360 struct thresh_restart {
361 	struct threshold_block	*b;
362 	int			set_lvt_off;
363 	int			lvt_off;
364 	u16			old_limit;
365 };
366 
367 static const char *bank4_names(const struct threshold_block *b)
368 {
369 	switch (b->address) {
370 	/* MSR4_MISC0 */
371 	case 0x00000413:
372 		return "dram";
373 
374 	case 0xc0000408:
375 		return "ht_links";
376 
377 	case 0xc0000409:
378 		return "l3_cache";
379 
380 	default:
381 		WARN(1, "Funny MSR: 0x%08x\n", b->address);
382 		return "";
383 	}
384 };
385 
386 
387 static bool lvt_interrupt_supported(unsigned int bank, u32 msr_high_bits)
388 {
389 	/*
390 	 * bank 4 supports APIC LVT interrupts implicitly since forever.
391 	 */
392 	if (bank == 4)
393 		return true;
394 
395 	/*
396 	 * IntP: interrupt present; if this bit is set, the thresholding
397 	 * bank can generate APIC LVT interrupts
398 	 */
399 	return msr_high_bits & BIT(28);
400 }
401 
402 static bool lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
403 {
404 	int msr = (hi & MASK_LVTOFF_HI) >> 20;
405 
406 	/*
407 	 * On SMCA CPUs, LVT offset is programmed at a different MSR, and
408 	 * the BIOS provides the value. The original field where LVT offset
409 	 * was set is reserved. Return early here:
410 	 */
411 	if (mce_flags.smca)
412 		return false;
413 
414 	if (apic < 0) {
415 		pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
416 		       "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
417 		       b->bank, b->block, b->address, hi, lo);
418 		return false;
419 	}
420 
421 	if (apic != msr) {
422 		pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
423 		       "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
424 		       b->cpu, apic, b->bank, b->block, b->address, hi, lo);
425 		return false;
426 	}
427 
428 	return true;
429 };
430 
431 /* Reprogram MCx_MISC MSR behind this threshold block. */
432 static void threshold_restart_block(void *_tr)
433 {
434 	struct thresh_restart *tr = _tr;
435 	struct msr val;
436 
437 	/* sysfs write might race against an offline operation */
438 	if (!this_cpu_read(threshold_banks) && !tr->set_lvt_off)
439 		return;
440 
441 	rdmsrq(tr->b->address, val.q);
442 
443 	/*
444 	 * Reset error count and overflow bit.
445 	 * This is done during init or after handling an interrupt.
446 	 */
447 	if (val.h & MASK_OVERFLOW_HI || tr->set_lvt_off) {
448 		val.h &= ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI);
449 		val.h |= THRESHOLD_MAX - tr->b->threshold_limit;
450 	} else if (tr->old_limit) {	/* change limit w/o reset */
451 		int new_count = (val.h & THRESHOLD_MAX) +
452 		    (tr->old_limit - tr->b->threshold_limit);
453 
454 		val.h = (val.h & ~MASK_ERR_COUNT_HI) |
455 		    (new_count & THRESHOLD_MAX);
456 	}
457 
458 	/* clear IntType */
459 	val.h &= ~MASK_INT_TYPE_HI;
460 
461 	if (!tr->b->interrupt_capable)
462 		goto done;
463 
464 	if (tr->set_lvt_off) {
465 		if (lvt_off_valid(tr->b, tr->lvt_off, val.l, val.h)) {
466 			/* set new lvt offset */
467 			val.h &= ~MASK_LVTOFF_HI;
468 			val.h |= tr->lvt_off << 20;
469 		}
470 	}
471 
472 	if (tr->b->interrupt_enable)
473 		val.h |= INT_TYPE_APIC;
474 
475  done:
476 
477 	val.h |= MASK_COUNT_EN_HI;
478 	wrmsrq(tr->b->address, val.q);
479 }
480 
481 static void threshold_restart_bank(unsigned int bank, bool intr_en)
482 {
483 	struct threshold_bank **thr_banks = this_cpu_read(threshold_banks);
484 	struct threshold_block *block, *tmp;
485 	struct thresh_restart tr;
486 
487 	if (!thr_banks || !thr_banks[bank])
488 		return;
489 
490 	memset(&tr, 0, sizeof(tr));
491 
492 	list_for_each_entry_safe(block, tmp, &thr_banks[bank]->miscj, miscj) {
493 		tr.b = block;
494 		tr.b->interrupt_enable = intr_en;
495 		threshold_restart_block(&tr);
496 	}
497 }
498 
499 /* Try to use the threshold limit reported through APEI. */
500 static u16 get_thr_limit(void)
501 {
502 	u32 thr_limit = mce_get_apei_thr_limit();
503 
504 	/* Fallback to old default if APEI limit is not available. */
505 	if (!thr_limit)
506 		return THRESHOLD_MAX;
507 
508 	return min(thr_limit, THRESHOLD_MAX);
509 }
510 
511 static void mce_threshold_block_init(struct threshold_block *b, int offset)
512 {
513 	struct thresh_restart tr = {
514 		.b			= b,
515 		.set_lvt_off		= 1,
516 		.lvt_off		= offset,
517 	};
518 
519 	b->threshold_limit		= get_thr_limit();
520 	threshold_restart_block(&tr);
521 };
522 
523 static int setup_APIC_mce_threshold(int reserved, int new)
524 {
525 	if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
526 					      APIC_DELIVERY_MODE_FIXED, 0))
527 		return new;
528 
529 	return reserved;
530 }
531 
532 static u32 get_block_address(u32 current_addr, u32 low, u32 high,
533 			     unsigned int bank, unsigned int block,
534 			     unsigned int cpu)
535 {
536 	u32 addr = 0, offset = 0;
537 
538 	if ((bank >= per_cpu(mce_num_banks, cpu)) || (block >= NR_BLOCKS))
539 		return addr;
540 
541 	if (mce_flags.smca) {
542 		if (!block)
543 			return MSR_AMD64_SMCA_MCx_MISC(bank);
544 
545 		if (!(low & MASK_BLKPTR_LO))
546 			return 0;
547 
548 		return MSR_AMD64_SMCA_MCx_MISCy(bank, block - 1);
549 	}
550 
551 	/* Fall back to method we used for older processors: */
552 	switch (block) {
553 	case 0:
554 		addr = mca_msr_reg(bank, MCA_MISC);
555 		break;
556 	case 1:
557 		offset = ((low & MASK_BLKPTR_LO) >> 21);
558 		if (offset)
559 			addr = MCG_XBLK_ADDR + offset;
560 		break;
561 	default:
562 		addr = ++current_addr;
563 	}
564 	return addr;
565 }
566 
567 static int prepare_threshold_block(unsigned int bank, unsigned int block, u32 addr,
568 				   int offset, u32 misc_high)
569 {
570 	unsigned int cpu = smp_processor_id();
571 	struct threshold_block b;
572 	int new;
573 
574 	if (!block)
575 		per_cpu(bank_map, cpu) |= BIT_ULL(bank);
576 
577 	memset(&b, 0, sizeof(b));
578 	b.cpu			= cpu;
579 	b.bank			= bank;
580 	b.block			= block;
581 	b.address		= addr;
582 	b.interrupt_capable	= lvt_interrupt_supported(bank, misc_high);
583 
584 	if (!b.interrupt_capable)
585 		goto done;
586 
587 	__set_bit(bank, this_cpu_ptr(&mce_amd_data)->thr_intr_banks);
588 	b.interrupt_enable = 1;
589 
590 	if (mce_flags.smca)
591 		goto done;
592 
593 	new = (misc_high & MASK_LVTOFF_HI) >> 20;
594 	offset = setup_APIC_mce_threshold(offset, new);
595 	if (offset == new)
596 		thresholding_irq_en = true;
597 
598 done:
599 	mce_threshold_block_init(&b, offset);
600 
601 	return offset;
602 }
603 
604 bool amd_filter_mce(struct mce *m)
605 {
606 	enum smca_bank_types bank_type = smca_get_bank_type(m->extcpu, m->bank);
607 	struct cpuinfo_x86 *c = &boot_cpu_data;
608 
609 	/* Bogus hw errors on Cezanne A0. */
610 	if (c->x86 == 0x19 &&
611 	    c->x86_model == 0x50 &&
612 	    c->x86_stepping == 0x0) {
613 		if (!(m->status & MCI_STATUS_EN))
614 			return true;
615 	}
616 
617 	/* See Family 17h Models 10h-2Fh Erratum #1114. */
618 	if (c->x86 == 0x17 &&
619 	    c->x86_model >= 0x10 && c->x86_model <= 0x2F &&
620 	    bank_type == SMCA_IF && XEC(m->status, 0x3f) == 10)
621 		return true;
622 
623 	/* NB GART TLB error reporting is disabled by default. */
624 	if (c->x86 < 0x17) {
625 		if (m->bank == 4 && XEC(m->status, 0x1f) == 0x5)
626 			return true;
627 	}
628 
629 	return false;
630 }
631 
632 /*
633  * Turn off thresholding banks for the following conditions:
634  * - MC4_MISC thresholding is not supported on Family 0x15.
635  * - Prevent possible spurious interrupts from the IF bank on Family 0x17
636  *   Models 0x10-0x2F due to Erratum #1114.
637  */
638 static void disable_err_thresholding(struct cpuinfo_x86 *c, unsigned int bank)
639 {
640 	int i, num_msrs;
641 	u64 hwcr;
642 	bool need_toggle;
643 	u32 msrs[NR_BLOCKS];
644 
645 	if (c->x86 == 0x15 && bank == 4) {
646 		msrs[0] = 0x00000413; /* MC4_MISC0 */
647 		msrs[1] = 0xc0000408; /* MC4_MISC1 */
648 		num_msrs = 2;
649 	} else if (c->x86 == 0x17 &&
650 		   (c->x86_model >= 0x10 && c->x86_model <= 0x2F)) {
651 
652 		if (smca_get_bank_type(smp_processor_id(), bank) != SMCA_IF)
653 			return;
654 
655 		msrs[0] = MSR_AMD64_SMCA_MCx_MISC(bank);
656 		num_msrs = 1;
657 	} else {
658 		return;
659 	}
660 
661 	rdmsrq(MSR_K7_HWCR, hwcr);
662 
663 	/* McStatusWrEn has to be set */
664 	need_toggle = !(hwcr & BIT(18));
665 	if (need_toggle)
666 		wrmsrq(MSR_K7_HWCR, hwcr | BIT(18));
667 
668 	/* Clear CntP bit safely */
669 	for (i = 0; i < num_msrs; i++)
670 		msr_clear_bit(msrs[i], 62);
671 
672 	/* restore old settings */
673 	if (need_toggle)
674 		wrmsrq(MSR_K7_HWCR, hwcr);
675 }
676 
677 static void amd_apply_cpu_quirks(struct cpuinfo_x86 *c)
678 {
679 	struct mce_bank *mce_banks = this_cpu_ptr(mce_banks_array);
680 
681 	/* This should be disabled by the BIOS, but isn't always */
682 	if (c->x86 == 15 && this_cpu_read(mce_num_banks) > 4) {
683 		/*
684 		 * disable GART TBL walk error reporting, which
685 		 * trips off incorrectly with the IOMMU & 3ware
686 		 * & Cerberus:
687 		 */
688 		clear_bit(10, (unsigned long *)&mce_banks[4].ctl);
689 	}
690 
691 	/*
692 	 * Various K7s with broken bank 0 around. Always disable
693 	 * by default.
694 	 */
695 	if (c->x86 == 6 && this_cpu_read(mce_num_banks))
696 		mce_banks[0].ctl = 0;
697 }
698 
699 /*
700  * Enable the APIC LVT interrupt vectors once per-CPU. This should be done before hardware is
701  * ready to send interrupts.
702  *
703  * Individual error sources are enabled later during per-bank init.
704  */
705 static void smca_enable_interrupt_vectors(void)
706 {
707 	struct mce_amd_cpu_data *data = this_cpu_ptr(&mce_amd_data);
708 	u64 mca_intr_cfg, offset;
709 
710 	if (!mce_flags.smca || !mce_flags.succor)
711 		return;
712 
713 	if (rdmsrq_safe(MSR_CU_DEF_ERR, &mca_intr_cfg))
714 		return;
715 
716 	offset = (mca_intr_cfg & SMCA_THR_LVT_OFF) >> 12;
717 	if (!setup_APIC_eilvt(offset, THRESHOLD_APIC_VECTOR, APIC_DELIVERY_MODE_FIXED, 0))
718 		data->thr_intr_en = 1;
719 
720 	offset = (mca_intr_cfg & MASK_DEF_LVTOFF) >> 4;
721 	if (!setup_APIC_eilvt(offset, DEFERRED_ERROR_VECTOR, APIC_DELIVERY_MODE_FIXED, 0))
722 		data->dfr_intr_en = 1;
723 }
724 
725 /* cpu init entry point, called from mce.c with preempt off */
726 void mce_amd_feature_init(struct cpuinfo_x86 *c)
727 {
728 	unsigned int bank, block, cpu = smp_processor_id();
729 	struct msr val = { .q = 0 };
730 	u32 address = 0;
731 	int offset = -1;
732 
733 	amd_apply_cpu_quirks(c);
734 
735 	mce_flags.amd_threshold	 = 1;
736 
737 	smca_enable_interrupt_vectors();
738 
739 	for (bank = 0; bank < this_cpu_read(mce_num_banks); ++bank) {
740 		if (mce_flags.smca) {
741 			smca_configure(bank, cpu);
742 
743 			if (!this_cpu_ptr(&mce_amd_data)->thr_intr_en)
744 				continue;
745 		}
746 
747 		disable_err_thresholding(c, bank);
748 
749 		for (block = 0; block < NR_BLOCKS; ++block) {
750 			address = get_block_address(address, val.l, val.h, bank, block, cpu);
751 			if (!address)
752 				break;
753 
754 			if (rdmsrq_safe(address, &val.q))
755 				break;
756 
757 			if (!(val.h & MASK_VALID_HI))
758 				continue;
759 
760 			if (!(val.h & MASK_CNTP_HI)  ||
761 			     (val.h & MASK_LOCKED_HI))
762 				continue;
763 
764 			offset = prepare_threshold_block(bank, block, address, offset, val.h);
765 		}
766 	}
767 }
768 
769 void smca_bsp_init(void)
770 {
771 	mce_threshold_vector	  = amd_threshold_interrupt;
772 	deferred_error_int_vector = amd_deferred_error_interrupt;
773 }
774 
775 /*
776  * DRAM ECC errors are reported in the Northbridge (bank 4) with
777  * Extended Error Code 8.
778  */
779 static bool legacy_mce_is_memory_error(struct mce *m)
780 {
781 	return m->bank == 4 && XEC(m->status, 0x1f) == 8;
782 }
783 
784 /*
785  * DRAM ECC errors are reported in Unified Memory Controllers with
786  * Extended Error Code 0.
787  */
788 static bool smca_mce_is_memory_error(struct mce *m)
789 {
790 	enum smca_bank_types bank_type;
791 
792 	if (XEC(m->status, 0x3f))
793 		return false;
794 
795 	bank_type = smca_get_bank_type(m->extcpu, m->bank);
796 
797 	return bank_type == SMCA_UMC || bank_type == SMCA_UMC_V2;
798 }
799 
800 bool amd_mce_is_memory_error(struct mce *m)
801 {
802 	if (mce_flags.smca)
803 		return smca_mce_is_memory_error(m);
804 	else
805 		return legacy_mce_is_memory_error(m);
806 }
807 
808 /*
809  * Some AMD systems have an explicit indicator that the value in MCA_ADDR is a
810  * system physical address. Individual cases though, need to be detected for
811  * other systems. Future cases will be added as needed.
812  *
813  * 1) General case
814  *	a) Assume address is not usable.
815  * 2) Poison errors
816  *	a) Indicated by MCA_STATUS[43]: poison. Defined for all banks except legacy
817  *	   northbridge (bank 4).
818  *	b) Refers to poison consumption in the core. Does not include "no action",
819  *	   "action optional", or "deferred" error severities.
820  *	c) Will include a usable address so that immediate action can be taken.
821  * 3) Northbridge DRAM ECC errors
822  *	a) Reported in legacy bank 4 with extended error code (XEC) 8.
823  *	b) MCA_STATUS[43] is *not* defined as poison in legacy bank 4. Therefore,
824  *	   this bit should not be checked.
825  * 4) MCI_STATUS_PADDRVAL is set
826  *	a) Will provide a valid system physical address.
827  *
828  * NOTE: SMCA UMC memory errors fall into case #1.
829  */
830 bool amd_mce_usable_address(struct mce *m)
831 {
832 	/* Check special northbridge case 3) first. */
833 	if (!mce_flags.smca) {
834 		if (legacy_mce_is_memory_error(m))
835 			return true;
836 		else if (m->bank == 4)
837 			return false;
838 	}
839 
840 	if (m->bank >= per_cpu(mce_num_banks, m->extcpu))
841 		return false;
842 
843 	if (per_cpu(smca_banks, m->extcpu)[m->bank].paddrv)
844 		return m->status & MCI_STATUS_PADDRV;
845 
846 	/* Check poison bit for all other bank types. */
847 	if (m->status & MCI_STATUS_POISON)
848 		return true;
849 
850 	/* Assume address is not usable for all others. */
851 	return false;
852 }
853 
854 DEFINE_IDTENTRY_SYSVEC(sysvec_deferred_error)
855 {
856 	trace_deferred_error_apic_entry(DEFERRED_ERROR_VECTOR);
857 	inc_irq_stat(DEFERRED_ERROR);
858 	deferred_error_int_vector();
859 	trace_deferred_error_apic_exit(DEFERRED_ERROR_VECTOR);
860 	apic_eoi();
861 }
862 
863 /* APIC interrupt handler for deferred errors */
864 static void amd_deferred_error_interrupt(void)
865 {
866 	machine_check_poll(MCP_TIMESTAMP, &this_cpu_ptr(&mce_amd_data)->dfr_intr_banks);
867 }
868 
869 void mce_amd_handle_storm(unsigned int bank, bool on)
870 {
871 	threshold_restart_bank(bank, !on);
872 }
873 
874 static void amd_reset_thr_limit(unsigned int bank)
875 {
876 	threshold_restart_bank(bank, true);
877 }
878 
879 /*
880  * Threshold interrupt handler will service THRESHOLD_APIC_VECTOR. The interrupt
881  * goes off when error_count reaches threshold_limit.
882  */
883 static void amd_threshold_interrupt(void)
884 {
885 	machine_check_poll(MCP_TIMESTAMP, &this_cpu_ptr(&mce_amd_data)->thr_intr_banks);
886 }
887 
888 void amd_clear_bank(struct mce *m)
889 {
890 	amd_reset_thr_limit(m->bank);
891 
892 	if (mce_flags.smca) {
893 		/*
894 		 * Clear MCA_DESTAT for all deferred errors even those
895 		 * logged in MCA_STATUS.
896 		 */
897 		if (m->status & MCI_STATUS_DEFERRED)
898 			mce_wrmsrq(MSR_AMD64_SMCA_MCx_DESTAT(m->bank), 0);
899 
900 		/* Don't clear MCA_STATUS if MCA_DESTAT was used exclusively. */
901 		if (m->kflags & MCE_CHECK_DFR_REGS)
902 			return;
903 	}
904 
905 	mce_wrmsrq(mca_msr_reg(m->bank, MCA_STATUS), 0);
906 }
907 
908 /*
909  * Sysfs Interface
910  */
911 
912 struct threshold_attr {
913 	struct attribute attr;
914 	ssize_t (*show) (struct threshold_block *, char *);
915 	ssize_t (*store) (struct threshold_block *, const char *, size_t count);
916 };
917 
918 #define SHOW_FIELDS(name)						\
919 static ssize_t show_ ## name(struct threshold_block *b, char *buf)	\
920 {									\
921 	return sprintf(buf, "%lu\n", (unsigned long) b->name);		\
922 }
923 SHOW_FIELDS(interrupt_enable)
924 SHOW_FIELDS(threshold_limit)
925 
926 static ssize_t
927 store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
928 {
929 	struct thresh_restart tr;
930 	unsigned long new;
931 
932 	if (!b->interrupt_capable)
933 		return -EINVAL;
934 
935 	if (kstrtoul(buf, 0, &new) < 0)
936 		return -EINVAL;
937 
938 	b->interrupt_enable = !!new;
939 
940 	memset(&tr, 0, sizeof(tr));
941 	tr.b		= b;
942 
943 	if (smp_call_function_single(b->cpu, threshold_restart_block, &tr, 1))
944 		return -ENODEV;
945 
946 	return size;
947 }
948 
949 static ssize_t
950 store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
951 {
952 	struct thresh_restart tr;
953 	unsigned long new;
954 
955 	if (kstrtoul(buf, 0, &new) < 0)
956 		return -EINVAL;
957 
958 	if (new > THRESHOLD_MAX)
959 		new = THRESHOLD_MAX;
960 	if (new < 1)
961 		new = 1;
962 
963 	memset(&tr, 0, sizeof(tr));
964 	tr.old_limit = b->threshold_limit;
965 	b->threshold_limit = new;
966 	tr.b = b;
967 
968 	if (smp_call_function_single(b->cpu, threshold_restart_block, &tr, 1))
969 		return -ENODEV;
970 
971 	return size;
972 }
973 
974 static ssize_t show_error_count(struct threshold_block *b, char *buf)
975 {
976 	struct msr val;
977 
978 	/* CPU might be offline by now */
979 	if (rdmsrq_on_cpu(b->cpu, b->address, &val.q))
980 		return -ENODEV;
981 
982 	return sprintf(buf, "%u\n", ((val.h & THRESHOLD_MAX) -
983 				     (THRESHOLD_MAX - b->threshold_limit)));
984 }
985 
986 static struct threshold_attr error_count = {
987 	.attr = {.name = __stringify(error_count), .mode = 0444 },
988 	.show = show_error_count,
989 };
990 
991 #define RW_ATTR(val)							\
992 static struct threshold_attr val = {					\
993 	.attr	= {.name = __stringify(val), .mode = 0644 },		\
994 	.show	= show_## val,						\
995 	.store	= store_## val,						\
996 };
997 
998 RW_ATTR(interrupt_enable);
999 RW_ATTR(threshold_limit);
1000 
1001 static struct attribute *default_attrs[] = {
1002 	&threshold_limit.attr,
1003 	&error_count.attr,
1004 	NULL,	/* possibly interrupt_enable if supported, see below */
1005 	NULL,
1006 };
1007 ATTRIBUTE_GROUPS(default);
1008 
1009 #define to_block(k)	container_of(k, struct threshold_block, kobj)
1010 #define to_attr(a)	container_of(a, struct threshold_attr, attr)
1011 
1012 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
1013 {
1014 	struct threshold_block *b = to_block(kobj);
1015 	struct threshold_attr *a = to_attr(attr);
1016 	ssize_t ret;
1017 
1018 	ret = a->show ? a->show(b, buf) : -EIO;
1019 
1020 	return ret;
1021 }
1022 
1023 static ssize_t store(struct kobject *kobj, struct attribute *attr,
1024 		     const char *buf, size_t count)
1025 {
1026 	struct threshold_block *b = to_block(kobj);
1027 	struct threshold_attr *a = to_attr(attr);
1028 	ssize_t ret;
1029 
1030 	ret = a->store ? a->store(b, buf, count) : -EIO;
1031 
1032 	return ret;
1033 }
1034 
1035 static const struct sysfs_ops threshold_ops = {
1036 	.show			= show,
1037 	.store			= store,
1038 };
1039 
1040 static void threshold_block_release(struct kobject *kobj);
1041 
1042 static const struct kobj_type threshold_ktype = {
1043 	.sysfs_ops		= &threshold_ops,
1044 	.default_groups		= default_groups,
1045 	.release		= threshold_block_release,
1046 };
1047 
1048 static const char *get_name(unsigned int cpu, unsigned int bank, struct threshold_block *b)
1049 {
1050 	enum smca_bank_types bank_type;
1051 
1052 	if (!mce_flags.smca) {
1053 		if (b && bank == 4)
1054 			return bank4_names(b);
1055 
1056 		return th_names[bank];
1057 	}
1058 
1059 	bank_type = smca_get_bank_type(cpu, bank);
1060 
1061 	if (b && (bank_type == SMCA_UMC || bank_type == SMCA_UMC_V2)) {
1062 		if (b->block < ARRAY_SIZE(smca_umc_block_names))
1063 			return smca_umc_block_names[b->block];
1064 	}
1065 
1066 	if (b && b->block) {
1067 		snprintf(buf_mcatype, MAX_MCATYPE_NAME_LEN, "th_block_%u", b->block);
1068 		return buf_mcatype;
1069 	}
1070 
1071 	if (bank_type >= N_SMCA_BANK_TYPES) {
1072 		snprintf(buf_mcatype, MAX_MCATYPE_NAME_LEN, "th_bank_%u", bank);
1073 		return buf_mcatype;
1074 	}
1075 
1076 	if (per_cpu(smca_bank_counts, cpu)[bank_type] == 1)
1077 		return smca_get_name(bank_type);
1078 
1079 	snprintf(buf_mcatype, MAX_MCATYPE_NAME_LEN,
1080 		 "%s_%u", smca_get_name(bank_type),
1081 			  per_cpu(smca_banks, cpu)[bank].sysfs_id);
1082 	return buf_mcatype;
1083 }
1084 
1085 static int allocate_threshold_blocks(unsigned int cpu, struct threshold_bank *tb,
1086 				     unsigned int bank, unsigned int block,
1087 				     u32 address)
1088 {
1089 	struct threshold_block *b = NULL;
1090 	struct msr val;
1091 	int err;
1092 
1093 	if ((bank >= this_cpu_read(mce_num_banks)) || (block >= NR_BLOCKS))
1094 		return 0;
1095 
1096 	if (rdmsrq_safe(address, &val.q))
1097 		return 0;
1098 
1099 	if (!(val.h & MASK_VALID_HI)) {
1100 		if (block)
1101 			goto recurse;
1102 		else
1103 			return 0;
1104 	}
1105 
1106 	if (!(val.h & MASK_CNTP_HI)  ||
1107 	     (val.h & MASK_LOCKED_HI))
1108 		goto recurse;
1109 
1110 	b = kzalloc_obj(struct threshold_block);
1111 	if (!b)
1112 		return -ENOMEM;
1113 
1114 	b->block		= block;
1115 	b->bank			= bank;
1116 	b->cpu			= cpu;
1117 	b->address		= address;
1118 	b->interrupt_enable	= 0;
1119 	b->interrupt_capable	= lvt_interrupt_supported(bank, val.h);
1120 	b->threshold_limit	= get_thr_limit();
1121 
1122 	if (b->interrupt_capable) {
1123 		default_attrs[2] = &interrupt_enable.attr;
1124 		b->interrupt_enable = 1;
1125 	} else {
1126 		default_attrs[2] = NULL;
1127 	}
1128 
1129 	list_add(&b->miscj, &tb->miscj);
1130 
1131 	mce_threshold_block_init(b, (val.h & MASK_LVTOFF_HI) >> 20);
1132 
1133 	err = kobject_init_and_add(&b->kobj, &threshold_ktype, tb->kobj, get_name(cpu, bank, b));
1134 	if (err)
1135 		goto out_free;
1136 recurse:
1137 	address = get_block_address(address, val.l, val.h, bank, ++block, cpu);
1138 	if (!address)
1139 		return 0;
1140 
1141 	err = allocate_threshold_blocks(cpu, tb, bank, block, address);
1142 	if (err)
1143 		goto out_free;
1144 
1145 	if (b)
1146 		kobject_uevent(&b->kobj, KOBJ_ADD);
1147 
1148 	return 0;
1149 
1150 out_free:
1151 	if (b) {
1152 		list_del(&b->miscj);
1153 		kobject_put(&b->kobj);
1154 	}
1155 	return err;
1156 }
1157 
1158 static int threshold_create_bank(struct threshold_bank **bp, unsigned int cpu,
1159 				 unsigned int bank)
1160 {
1161 	struct device *dev = this_cpu_read(mce_device);
1162 	struct threshold_bank *b = NULL;
1163 	const char *name = get_name(cpu, bank, NULL);
1164 	int err = 0;
1165 
1166 	if (!dev)
1167 		return -ENODEV;
1168 
1169 	b = kzalloc_obj(struct threshold_bank);
1170 	if (!b) {
1171 		err = -ENOMEM;
1172 		goto out;
1173 	}
1174 
1175 	/* Associate the bank with the per-CPU MCE device */
1176 	b->kobj = kobject_create_and_add(name, &dev->kobj);
1177 	if (!b->kobj) {
1178 		err = -EINVAL;
1179 		goto out_free;
1180 	}
1181 
1182 	INIT_LIST_HEAD(&b->miscj);
1183 
1184 	err = allocate_threshold_blocks(cpu, b, bank, 0, mca_msr_reg(bank, MCA_MISC));
1185 	if (err)
1186 		goto out_kobj;
1187 
1188 	bp[bank] = b;
1189 	return 0;
1190 
1191 out_kobj:
1192 	kobject_put(b->kobj);
1193 out_free:
1194 	kfree(b);
1195 out:
1196 	return err;
1197 }
1198 
1199 static void threshold_block_release(struct kobject *kobj)
1200 {
1201 	kfree(to_block(kobj));
1202 }
1203 
1204 static void threshold_remove_bank(struct threshold_bank *bank)
1205 {
1206 	struct threshold_block *pos, *tmp;
1207 
1208 	list_for_each_entry_safe(pos, tmp, &bank->miscj, miscj) {
1209 		list_del(&pos->miscj);
1210 		kobject_put(&pos->kobj);
1211 	}
1212 
1213 	kobject_put(bank->kobj);
1214 	kfree(bank);
1215 }
1216 
1217 static void __threshold_remove_device(struct threshold_bank **bp)
1218 {
1219 	unsigned int bank, numbanks = this_cpu_read(mce_num_banks);
1220 
1221 	for (bank = 0; bank < numbanks; bank++) {
1222 		if (!bp[bank])
1223 			continue;
1224 
1225 		threshold_remove_bank(bp[bank]);
1226 		bp[bank] = NULL;
1227 	}
1228 	kfree(bp);
1229 }
1230 
1231 void mce_threshold_remove_device(unsigned int cpu)
1232 {
1233 	struct threshold_bank **bp = this_cpu_read(threshold_banks);
1234 
1235 	if (!bp)
1236 		return;
1237 
1238 	/*
1239 	 * Clear the pointer before cleaning up, so that the interrupt won't
1240 	 * touch anything of this.
1241 	 */
1242 	this_cpu_write(threshold_banks, NULL);
1243 
1244 	__threshold_remove_device(bp);
1245 	return;
1246 }
1247 
1248 /**
1249  * mce_threshold_create_device - Create the per-CPU MCE threshold device
1250  * @cpu:	The plugged in CPU
1251  *
1252  * Create directories and files for all valid threshold banks.
1253  *
1254  * This is invoked from the CPU hotplug callback which was installed in
1255  * mcheck_init_device(). The invocation happens in context of the hotplug
1256  * thread running on @cpu.  The callback is invoked on all CPUs which are
1257  * online when the callback is installed or during a real hotplug event.
1258  */
1259 void mce_threshold_create_device(unsigned int cpu)
1260 {
1261 	unsigned int numbanks, bank;
1262 	struct threshold_bank **bp;
1263 
1264 	if (!mce_flags.amd_threshold)
1265 		return;
1266 
1267 	bp = this_cpu_read(threshold_banks);
1268 	if (bp)
1269 		return;
1270 
1271 	numbanks = this_cpu_read(mce_num_banks);
1272 	bp = kzalloc_objs(*bp, numbanks);
1273 	if (!bp)
1274 		return;
1275 
1276 	for (bank = 0; bank < numbanks; ++bank) {
1277 		if (!(this_cpu_read(bank_map) & BIT_ULL(bank)))
1278 			continue;
1279 		if (threshold_create_bank(bp, cpu, bank)) {
1280 			__threshold_remove_device(bp);
1281 			return;
1282 		}
1283 	}
1284 	this_cpu_write(threshold_banks, bp);
1285 
1286 	if (thresholding_irq_en)
1287 		mce_threshold_vector = amd_threshold_interrupt;
1288 	return;
1289 }
1290