blob: 419e0ee3b12f4cedf0edf18420bb01c3049f8daa [file] [log] [blame]
Jacob Shin89b831e2005-11-05 17:25:53 +01001/*
Aravind Gopalakrishnanea2ca362016-03-07 14:02:21 +01002 * (c) 2005-2016 Advanced Micro Devices, Inc.
Jacob Shin89b831e2005-11-05 17:25:53 +01003 * Your use of this code is subject to the terms and conditions of the
4 * GNU general public license version 2. See "COPYING" or
5 * http://www.gnu.org/licenses/gpl.html
6 *
7 * Written by Jacob Shin - AMD, Inc.
Borislav Petkove6d41e82012-10-29 18:40:08 +01008 * Maintained by: Borislav Petkov <bp@alien8.de>
Jacob Shin89b831e2005-11-05 17:25:53 +01009 *
Borislav Petkov3490c0e2015-05-07 12:06:43 +020010 * All MC4_MISCi registers are shared between cores on a node.
Jacob Shin89b831e2005-11-05 17:25:53 +010011 */
Jacob Shin89b831e2005-11-05 17:25:53 +010012#include <linux/interrupt.h>
Jacob Shin89b831e2005-11-05 17:25:53 +010013#include <linux/notifier.h>
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +020014#include <linux/kobject.h>
Hidetoshi Seto34fa1962009-04-08 12:31:18 +020015#include <linux/percpu.h>
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +020016#include <linux/errno.h>
17#include <linux/sched.h>
Jacob Shin89b831e2005-11-05 17:25:53 +010018#include <linux/sysfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +020020#include <linux/init.h>
21#include <linux/cpu.h>
22#include <linux/smp.h>
23
Borislav Petkov019f34f2012-05-02 17:16:59 +020024#include <asm/amd_nb.h>
Jacob Shin89b831e2005-11-05 17:25:53 +010025#include <asm/apic.h>
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +020026#include <asm/idle.h>
Jacob Shin89b831e2005-11-05 17:25:53 +010027#include <asm/mce.h>
28#include <asm/msr.h>
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -050029#include <asm/trace/irq_vectors.h>
Jacob Shin89b831e2005-11-05 17:25:53 +010030
Aravind Gopalakrishnan60f116f2016-01-25 20:41:50 +010031#define NR_BLOCKS 5
Jacob Shin2903ee82006-06-26 13:58:56 +020032#define THRESHOLD_MAX 0xFFF
33#define INT_TYPE_APIC 0x00020000
34#define MASK_VALID_HI 0x80000000
Jan Beulich24ce0e92007-02-13 13:26:23 +010035#define MASK_CNTP_HI 0x40000000
36#define MASK_LOCKED_HI 0x20000000
Jacob Shin2903ee82006-06-26 13:58:56 +020037#define MASK_LVTOFF_HI 0x00F00000
38#define MASK_COUNT_EN_HI 0x00080000
39#define MASK_INT_TYPE_HI 0x00060000
40#define MASK_OVERFLOW_HI 0x00010000
Jacob Shin89b831e2005-11-05 17:25:53 +010041#define MASK_ERR_COUNT_HI 0x00000FFF
Jacob Shin95268662006-06-26 13:58:53 +020042#define MASK_BLKPTR_LO 0xFF000000
43#define MCG_XBLK_ADDR 0xC0000400
Jacob Shin89b831e2005-11-05 17:25:53 +010044
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -050045/* Deferred error settings */
46#define MSR_CU_DEF_ERR 0xC0000410
47#define MASK_DEF_LVTOFF 0x000000F0
48#define MASK_DEF_INT_TYPE 0x00000006
49#define DEF_LVT_OFF 0x2
50#define DEF_INT_TYPE_APIC 0x2
51
Aravind Gopalakrishnanf57a1f32016-01-25 20:41:51 +010052/* Scalable MCA: */
53
54/* Threshold LVT offset is at MSR0xC0000410[15:12] */
55#define SMCA_THR_LVT_OFF 0xF000
56
Borislav Petkov336d3352012-05-04 17:05:27 +020057static const char * const th_names[] = {
58 "load_store",
59 "insn_fetch",
60 "combined_unit",
61 "",
62 "northbridge",
63 "execution_unit",
64};
65
Aravind Gopalakrishnanbe0aec22016-03-07 14:02:18 +010066/* Define HWID to IP type mappings for Scalable MCA */
67struct amd_hwid amd_hwids[] = {
68 [SMCA_F17H_CORE] = { "f17h_core", 0xB0 },
69 [SMCA_DF] = { "data_fabric", 0x2E },
70 [SMCA_UMC] = { "umc", 0x96 },
71 [SMCA_PB] = { "param_block", 0x5 },
72 [SMCA_PSP] = { "psp", 0xFF },
73 [SMCA_SMU] = { "smu", 0x1 },
74};
75EXPORT_SYMBOL_GPL(amd_hwids);
76
77const char * const amd_core_mcablock_names[] = {
78 [SMCA_LS] = "load_store",
79 [SMCA_IF] = "insn_fetch",
80 [SMCA_L2_CACHE] = "l2_cache",
81 [SMCA_DE] = "decode_unit",
82 [RES] = "",
83 [SMCA_EX] = "execution_unit",
84 [SMCA_FP] = "floating_point",
85 [SMCA_L3_CACHE] = "l3_cache",
86};
87EXPORT_SYMBOL_GPL(amd_core_mcablock_names);
88
89const char * const amd_df_mcablock_names[] = {
90 [SMCA_CS] = "coherent_slave",
91 [SMCA_PIE] = "pie",
92};
93EXPORT_SYMBOL_GPL(amd_df_mcablock_names);
94
Boris Ostrovskybafcdd32013-03-14 17:10:41 -040095static DEFINE_PER_CPU(struct threshold_bank **, threshold_banks);
Aravind Gopalakrishnan955d1422016-07-08 11:09:38 +020096static DEFINE_PER_CPU(unsigned int, bank_map); /* see which banks are on */
Jacob Shin89b831e2005-11-05 17:25:53 +010097
Andi Kleenb2762682009-02-12 13:49:31 +010098static void amd_threshold_interrupt(void);
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -050099static void amd_deferred_error_interrupt(void);
100
101static void default_deferred_error_interrupt(void)
102{
103 pr_err("Unexpected deferred interrupt at vector %x\n", DEFERRED_ERROR_VECTOR);
104}
105void (*deferred_error_int_vector)(void) = default_deferred_error_interrupt;
Andi Kleenb2762682009-02-12 13:49:31 +0100106
Jacob Shin89b831e2005-11-05 17:25:53 +0100107/*
108 * CPU Initialization
109 */
110
Mike Travis4cd46012008-12-16 17:34:04 -0800111struct thresh_restart {
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200112 struct threshold_block *b;
113 int reset;
Robert Richter9c37c9d2010-10-25 16:03:35 +0200114 int set_lvt_off;
115 int lvt_off;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200116 u16 old_limit;
Mike Travis4cd46012008-12-16 17:34:04 -0800117};
118
Boris Ostrovskyc76e8162013-03-14 17:10:40 -0400119static inline bool is_shared_bank(int bank)
120{
Aravind Gopalakrishnan284b9652016-01-25 20:41:49 +0100121 /*
122 * Scalable MCA provides for only one core to have access to the MSRs of
123 * a shared bank.
124 */
125 if (mce_flags.smca)
126 return false;
127
Boris Ostrovskyc76e8162013-03-14 17:10:40 -0400128 /* Bank 4 is for northbridge reporting and is thus shared */
129 return (bank == 4);
130}
131
Jan Beulich2cd4c302015-01-23 08:32:01 +0000132static const char *bank4_names(const struct threshold_block *b)
Borislav Petkov336d3352012-05-04 17:05:27 +0200133{
134 switch (b->address) {
135 /* MSR4_MISC0 */
136 case 0x00000413:
137 return "dram";
138
139 case 0xc0000408:
140 return "ht_links";
141
142 case 0xc0000409:
143 return "l3_cache";
144
145 default:
146 WARN(1, "Funny MSR: 0x%08x\n", b->address);
147 return "";
148 }
149};
150
151
Borislav Petkovf227d432012-04-16 18:01:53 +0200152static bool lvt_interrupt_supported(unsigned int bank, u32 msr_high_bits)
153{
154 /*
155 * bank 4 supports APIC LVT interrupts implicitly since forever.
156 */
157 if (bank == 4)
158 return true;
159
160 /*
161 * IntP: interrupt present; if this bit is set, the thresholding
162 * bank can generate APIC LVT interrupts
163 */
164 return msr_high_bits & BIT(28);
165}
166
Robert Richterbbaff082010-10-25 16:03:37 +0200167static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
168{
169 int msr = (hi & MASK_LVTOFF_HI) >> 20;
170
171 if (apic < 0) {
172 pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
173 "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
174 b->bank, b->block, b->address, hi, lo);
175 return 0;
176 }
177
178 if (apic != msr) {
Aravind Gopalakrishnanf57a1f32016-01-25 20:41:51 +0100179 /*
180 * On SMCA CPUs, LVT offset is programmed at a different MSR, and
181 * the BIOS provides the value. The original field where LVT offset
182 * was set is reserved. Return early here:
183 */
184 if (mce_flags.smca)
185 return 0;
186
Robert Richterbbaff082010-10-25 16:03:37 +0200187 pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
188 "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
189 b->cpu, apic, b->bank, b->block, b->address, hi, lo);
190 return 0;
191 }
192
193 return 1;
194};
195
Aravind Gopalakrishnanea2ca362016-03-07 14:02:21 +0100196/* Reprogram MCx_MISC MSR behind this threshold bank. */
Andrew Mortona6b6a142009-03-18 10:40:25 +1030197static void threshold_restart_bank(void *_tr)
Jacob Shin89b831e2005-11-05 17:25:53 +0100198{
Mike Travis4cd46012008-12-16 17:34:04 -0800199 struct thresh_restart *tr = _tr;
Robert Richter7203a042010-10-25 16:03:36 +0200200 u32 hi, lo;
Jacob Shin89b831e2005-11-05 17:25:53 +0100201
Robert Richter7203a042010-10-25 16:03:36 +0200202 rdmsr(tr->b->address, lo, hi);
Jacob Shin89b831e2005-11-05 17:25:53 +0100203
Robert Richter7203a042010-10-25 16:03:36 +0200204 if (tr->b->threshold_limit < (hi & THRESHOLD_MAX))
Mike Travis4cd46012008-12-16 17:34:04 -0800205 tr->reset = 1; /* limit cannot be lower than err count */
Jacob Shin89b831e2005-11-05 17:25:53 +0100206
Mike Travis4cd46012008-12-16 17:34:04 -0800207 if (tr->reset) { /* reset err count and overflow bit */
Robert Richter7203a042010-10-25 16:03:36 +0200208 hi =
209 (hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
Mike Travis4cd46012008-12-16 17:34:04 -0800210 (THRESHOLD_MAX - tr->b->threshold_limit);
211 } else if (tr->old_limit) { /* change limit w/o reset */
Robert Richter7203a042010-10-25 16:03:36 +0200212 int new_count = (hi & THRESHOLD_MAX) +
Mike Travis4cd46012008-12-16 17:34:04 -0800213 (tr->old_limit - tr->b->threshold_limit);
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200214
Robert Richter7203a042010-10-25 16:03:36 +0200215 hi = (hi & ~MASK_ERR_COUNT_HI) |
Jacob Shin89b831e2005-11-05 17:25:53 +0100216 (new_count & THRESHOLD_MAX);
217 }
218
Borislav Petkovf227d432012-04-16 18:01:53 +0200219 /* clear IntType */
220 hi &= ~MASK_INT_TYPE_HI;
221
222 if (!tr->b->interrupt_capable)
223 goto done;
224
Robert Richter9c37c9d2010-10-25 16:03:35 +0200225 if (tr->set_lvt_off) {
Robert Richterbbaff082010-10-25 16:03:37 +0200226 if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
227 /* set new lvt offset */
228 hi &= ~MASK_LVTOFF_HI;
229 hi |= tr->lvt_off << 20;
230 }
Robert Richter9c37c9d2010-10-25 16:03:35 +0200231 }
232
Borislav Petkovf227d432012-04-16 18:01:53 +0200233 if (tr->b->interrupt_enable)
234 hi |= INT_TYPE_APIC;
235
236 done:
Jacob Shin89b831e2005-11-05 17:25:53 +0100237
Robert Richter7203a042010-10-25 16:03:36 +0200238 hi |= MASK_COUNT_EN_HI;
239 wrmsr(tr->b->address, lo, hi);
Jacob Shin89b831e2005-11-05 17:25:53 +0100240}
241
Robert Richter9c37c9d2010-10-25 16:03:35 +0200242static void mce_threshold_block_init(struct threshold_block *b, int offset)
243{
244 struct thresh_restart tr = {
245 .b = b,
246 .set_lvt_off = 1,
247 .lvt_off = offset,
248 };
249
250 b->threshold_limit = THRESHOLD_MAX;
251 threshold_restart_bank(&tr);
252};
253
Aravind Gopalakrishnan868c00b2015-05-06 06:58:58 -0500254static int setup_APIC_mce_threshold(int reserved, int new)
Robert Richterbbaff082010-10-25 16:03:37 +0200255{
256 if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
257 APIC_EILVT_MSG_FIX, 0))
258 return new;
259
260 return reserved;
261}
262
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500263static int setup_APIC_deferred_error(int reserved, int new)
264{
265 if (reserved < 0 && !setup_APIC_eilvt(new, DEFERRED_ERROR_VECTOR,
266 APIC_EILVT_MSG_FIX, 0))
267 return new;
268
269 return reserved;
270}
271
272static void deferred_error_interrupt_enable(struct cpuinfo_x86 *c)
273{
274 u32 low = 0, high = 0;
275 int def_offset = -1, def_new;
276
277 if (rdmsr_safe(MSR_CU_DEF_ERR, &low, &high))
278 return;
279
280 def_new = (low & MASK_DEF_LVTOFF) >> 4;
281 if (!(low & MASK_DEF_LVTOFF)) {
282 pr_err(FW_BUG "Your BIOS is not setting up LVT offset 0x2 for deferred error IRQs correctly.\n");
283 def_new = DEF_LVT_OFF;
284 low = (low & ~MASK_DEF_LVTOFF) | (DEF_LVT_OFF << 4);
285 }
286
287 def_offset = setup_APIC_deferred_error(def_offset, def_new);
288 if ((def_offset == def_new) &&
289 (deferred_error_int_vector != amd_deferred_error_interrupt))
290 deferred_error_int_vector = amd_deferred_error_interrupt;
291
292 low = (low & ~MASK_DEF_INT_TYPE) | DEF_INT_TYPE_APIC;
293 wrmsr(MSR_CU_DEF_ERR, low, high);
294}
295
Aravind Gopalakrishnan8dd1e172016-03-07 14:02:19 +0100296static u32 get_block_address(u32 current_addr, u32 low, u32 high,
297 unsigned int bank, unsigned int block)
298{
299 u32 addr = 0, offset = 0;
300
301 if (mce_flags.smca) {
302 if (!block) {
303 addr = MSR_AMD64_SMCA_MCx_MISC(bank);
304 } else {
305 /*
306 * For SMCA enabled processors, BLKPTR field of the
307 * first MISC register (MCx_MISC0) indicates presence of
308 * additional MISC register set (MISC1-4).
309 */
310 u32 low, high;
311
312 if (rdmsr_safe(MSR_AMD64_SMCA_MCx_CONFIG(bank), &low, &high))
313 return addr;
314
315 if (!(low & MCI_CONFIG_MCAX))
316 return addr;
317
318 if (!rdmsr_safe(MSR_AMD64_SMCA_MCx_MISC(bank), &low, &high) &&
319 (low & MASK_BLKPTR_LO))
320 addr = MSR_AMD64_SMCA_MCx_MISCy(bank, block - 1);
321 }
322 return addr;
323 }
324
325 /* Fall back to method we used for older processors: */
326 switch (block) {
327 case 0:
Yazen Ghannamd9d73fc2016-04-30 14:33:55 +0200328 addr = msr_ops.misc(bank);
Aravind Gopalakrishnan8dd1e172016-03-07 14:02:19 +0100329 break;
330 case 1:
331 offset = ((low & MASK_BLKPTR_LO) >> 21);
332 if (offset)
333 addr = MCG_XBLK_ADDR + offset;
334 break;
335 default:
336 addr = ++current_addr;
337 }
338 return addr;
339}
340
Borislav Petkov429893b2016-01-25 20:41:52 +0100341static int
342prepare_threshold_block(unsigned int bank, unsigned int block, u32 addr,
343 int offset, u32 misc_high)
344{
345 unsigned int cpu = smp_processor_id();
Borislav Petkove128b4f2016-05-11 14:58:25 +0200346 u32 smca_low, smca_high, smca_addr;
Borislav Petkov429893b2016-01-25 20:41:52 +0100347 struct threshold_block b;
348 int new;
349
350 if (!block)
351 per_cpu(bank_map, cpu) |= (1 << bank);
352
353 memset(&b, 0, sizeof(b));
354 b.cpu = cpu;
355 b.bank = bank;
356 b.block = block;
357 b.address = addr;
358 b.interrupt_capable = lvt_interrupt_supported(bank, misc_high);
359
360 if (!b.interrupt_capable)
361 goto done;
362
363 b.interrupt_enable = 1;
364
Borislav Petkove128b4f2016-05-11 14:58:25 +0200365 if (!mce_flags.smca) {
Borislav Petkov429893b2016-01-25 20:41:52 +0100366 new = (misc_high & MASK_LVTOFF_HI) >> 20;
Borislav Petkove128b4f2016-05-11 14:58:25 +0200367 goto set_offset;
Borislav Petkov429893b2016-01-25 20:41:52 +0100368 }
369
Borislav Petkove128b4f2016-05-11 14:58:25 +0200370 smca_addr = MSR_AMD64_SMCA_MCx_CONFIG(bank);
371
372 if (!rdmsr_safe(smca_addr, &smca_low, &smca_high)) {
373 /*
374 * OS is required to set the MCAX bit to acknowledge that it is
375 * now using the new MSR ranges and new registers under each
376 * bank. It also means that the OS will configure deferred
377 * errors in the new MCx_CONFIG register. If the bit is not set,
378 * uncorrectable errors will cause a system panic.
379 *
380 * MCA_CONFIG[MCAX] is bit 32 (0 in the high portion of the MSR.)
381 */
382 smca_high |= BIT(0);
383
384 /*
385 * SMCA logs Deferred Error information in MCA_DE{STAT,ADDR}
386 * registers with the option of additionally logging to
387 * MCA_{STATUS,ADDR} if MCA_CONFIG[LogDeferredInMcaStat] is set.
388 *
389 * This bit is usually set by BIOS to retain the old behavior
390 * for OSes that don't use the new registers. Linux supports the
391 * new registers so let's disable that additional logging here.
392 *
393 * MCA_CONFIG[LogDeferredInMcaStat] is bit 34 (bit 2 in the high
394 * portion of the MSR).
395 */
396 smca_high &= ~BIT(2);
397
398 wrmsr(smca_addr, smca_low, smca_high);
399 }
400
401 /* Gather LVT offset for thresholding: */
402 if (rdmsr_safe(MSR_CU_DEF_ERR, &smca_low, &smca_high))
403 goto out;
404
405 new = (smca_low & SMCA_THR_LVT_OFF) >> 12;
406
407set_offset:
Borislav Petkov429893b2016-01-25 20:41:52 +0100408 offset = setup_APIC_mce_threshold(offset, new);
409
410 if ((offset == new) && (mce_threshold_vector != amd_threshold_interrupt))
411 mce_threshold_vector = amd_threshold_interrupt;
412
413done:
414 mce_threshold_block_init(&b, offset);
415
416out:
417 return offset;
418}
419
Jacob Shin95268662006-06-26 13:58:53 +0200420/* cpu init entry point, called from mce.c with preempt off */
H. Peter Anvincc3ca222009-02-20 23:35:51 -0800421void mce_amd_feature_init(struct cpuinfo_x86 *c)
Jacob Shin89b831e2005-11-05 17:25:53 +0100422{
Jacob Shin95268662006-06-26 13:58:53 +0200423 u32 low = 0, high = 0, address = 0;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200424 unsigned int bank, block;
Borislav Petkov429893b2016-01-25 20:41:52 +0100425 int offset = -1;
Jacob Shin89b831e2005-11-05 17:25:53 +0100426
Boris Ostrovskybafcdd32013-03-14 17:10:41 -0400427 for (bank = 0; bank < mca_cfg.banks; ++bank) {
Jacob Shin95268662006-06-26 13:58:53 +0200428 for (block = 0; block < NR_BLOCKS; ++block) {
Aravind Gopalakrishnan8dd1e172016-03-07 14:02:19 +0100429 address = get_block_address(address, low, high, bank, block);
430 if (!address)
431 break;
Jacob Shin89b831e2005-11-05 17:25:53 +0100432
Jacob Shin95268662006-06-26 13:58:53 +0200433 if (rdmsr_safe(address, &low, &high))
Jan Beulich24ce0e92007-02-13 13:26:23 +0100434 break;
Jacob Shin89b831e2005-11-05 17:25:53 +0100435
Borislav Petkov6dcbfe42010-10-08 12:08:34 +0200436 if (!(high & MASK_VALID_HI))
437 continue;
Jacob Shin89b831e2005-11-05 17:25:53 +0100438
Jan Beulich24ce0e92007-02-13 13:26:23 +0100439 if (!(high & MASK_CNTP_HI) ||
440 (high & MASK_LOCKED_HI))
Jacob Shin95268662006-06-26 13:58:53 +0200441 continue;
442
Borislav Petkov429893b2016-01-25 20:41:52 +0100443 offset = prepare_threshold_block(bank, block, address, offset, high);
Jacob Shin95268662006-06-26 13:58:53 +0200444 }
Jacob Shin89b831e2005-11-05 17:25:53 +0100445 }
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500446
447 if (mce_flags.succor)
448 deferred_error_interrupt_enable(c);
Jacob Shin89b831e2005-11-05 17:25:53 +0100449}
450
Yazen Ghannam34102002016-05-11 14:58:23 +0200451static void
452__log_error(unsigned int bank, bool deferred_err, bool threshold_err, u64 misc)
Aravind Gopalakrishnanafdf3442015-05-06 06:58:53 -0500453{
Yazen Ghannam34102002016-05-11 14:58:23 +0200454 u32 msr_status = msr_ops.status(bank);
455 u32 msr_addr = msr_ops.addr(bank);
Aravind Gopalakrishnanafdf3442015-05-06 06:58:53 -0500456 struct mce m;
457 u64 status;
458
Yazen Ghannam34102002016-05-11 14:58:23 +0200459 WARN_ON_ONCE(deferred_err && threshold_err);
460
461 if (deferred_err && mce_flags.smca) {
462 msr_status = MSR_AMD64_SMCA_MCx_DESTAT(bank);
463 msr_addr = MSR_AMD64_SMCA_MCx_DEADDR(bank);
464 }
465
466 rdmsrl(msr_status, status);
467
Aravind Gopalakrishnanafdf3442015-05-06 06:58:53 -0500468 if (!(status & MCI_STATUS_VAL))
469 return;
470
471 mce_setup(&m);
472
473 m.status = status;
474 m.bank = bank;
Aravind Gopalakrishnan6e6e7462015-05-06 06:58:54 -0500475
Aravind Gopalakrishnanafdf3442015-05-06 06:58:53 -0500476 if (threshold_err)
477 m.misc = misc;
478
Aravind Gopalakrishnan6e6e7462015-05-06 06:58:54 -0500479 if (m.status & MCI_STATUS_ADDRV)
Yazen Ghannam34102002016-05-11 14:58:23 +0200480 rdmsrl(msr_addr, m.addr);
Aravind Gopalakrishnanafdf3442015-05-06 06:58:53 -0500481
Yazen Ghannamdb819d62016-09-12 09:59:28 +0200482 if (mce_flags.smca && (m.status & MCI_STATUS_SYNDV))
483 rdmsrl(MSR_AMD64_SMCA_MCx_SYND(bank), m.synd);
484
Aravind Gopalakrishnan6e6e7462015-05-06 06:58:54 -0500485 mce_log(&m);
Yazen Ghannam34102002016-05-11 14:58:23 +0200486
487 wrmsrl(msr_status, 0);
Aravind Gopalakrishnanafdf3442015-05-06 06:58:53 -0500488}
489
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500490static inline void __smp_deferred_error_interrupt(void)
491{
492 inc_irq_stat(irq_deferred_error_count);
493 deferred_error_int_vector();
494}
495
496asmlinkage __visible void smp_deferred_error_interrupt(void)
497{
498 entering_irq();
499 __smp_deferred_error_interrupt();
500 exiting_ack_irq();
501}
502
503asmlinkage __visible void smp_trace_deferred_error_interrupt(void)
504{
505 entering_irq();
506 trace_deferred_error_apic_entry(DEFERRED_ERROR_VECTOR);
507 __smp_deferred_error_interrupt();
508 trace_deferred_error_apic_exit(DEFERRED_ERROR_VECTOR);
509 exiting_ack_irq();
510}
511
512/* APIC interrupt handler for deferred errors */
513static void amd_deferred_error_interrupt(void)
514{
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500515 unsigned int bank;
Yazen Ghannam34102002016-05-11 14:58:23 +0200516 u32 msr_status;
517 u64 status;
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500518
519 for (bank = 0; bank < mca_cfg.banks; ++bank) {
Yazen Ghannam34102002016-05-11 14:58:23 +0200520 msr_status = (mce_flags.smca) ? MSR_AMD64_SMCA_MCx_DESTAT(bank)
521 : msr_ops.status(bank);
522
523 rdmsrl(msr_status, status);
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500524
525 if (!(status & MCI_STATUS_VAL) ||
526 !(status & MCI_STATUS_DEFERRED))
527 continue;
528
Yazen Ghannam34102002016-05-11 14:58:23 +0200529 __log_error(bank, true, false, 0);
Aravind Gopalakrishnan24fd78a2015-05-06 06:58:56 -0500530 break;
531 }
532}
533
Jacob Shin89b831e2005-11-05 17:25:53 +0100534/*
535 * APIC Interrupt Handler
536 */
537
538/*
539 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
540 * the interrupt goes off when error_count reaches threshold_limit.
541 * the handler will simply log mcelog w/ software defined bank number.
542 */
Aravind Gopalakrishnanafdf3442015-05-06 06:58:53 -0500543
Andi Kleenb2762682009-02-12 13:49:31 +0100544static void amd_threshold_interrupt(void)
Jacob Shin89b831e2005-11-05 17:25:53 +0100545{
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200546 u32 low = 0, high = 0, address = 0;
Chen Yucong44612a32014-10-02 14:48:19 +0200547 int cpu = smp_processor_id();
Jacob Shin95268662006-06-26 13:58:53 +0200548 unsigned int bank, block;
Jacob Shin89b831e2005-11-05 17:25:53 +0100549
Jacob Shin89b831e2005-11-05 17:25:53 +0100550 /* assume first bank caused it */
Boris Ostrovskybafcdd32013-03-14 17:10:41 -0400551 for (bank = 0; bank < mca_cfg.banks; ++bank) {
Chen Yucong44612a32014-10-02 14:48:19 +0200552 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
Jan Beulich24ce0e92007-02-13 13:26:23 +0100553 continue;
Jacob Shin95268662006-06-26 13:58:53 +0200554 for (block = 0; block < NR_BLOCKS; ++block) {
Aravind Gopalakrishnan8dd1e172016-03-07 14:02:19 +0100555 address = get_block_address(address, low, high, bank, block);
556 if (!address)
557 break;
Jacob Shin89b831e2005-11-05 17:25:53 +0100558
Jacob Shin95268662006-06-26 13:58:53 +0200559 if (rdmsr_safe(address, &low, &high))
Jan Beulich24ce0e92007-02-13 13:26:23 +0100560 break;
Jacob Shin95268662006-06-26 13:58:53 +0200561
562 if (!(high & MASK_VALID_HI)) {
563 if (block)
564 continue;
565 else
566 break;
567 }
568
Jan Beulich24ce0e92007-02-13 13:26:23 +0100569 if (!(high & MASK_CNTP_HI) ||
570 (high & MASK_LOCKED_HI))
Jacob Shin95268662006-06-26 13:58:53 +0200571 continue;
572
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200573 /*
574 * Log the machine check that caused the threshold
575 * event.
576 */
Chen Yucong44612a32014-10-02 14:48:19 +0200577 if (high & MASK_OVERFLOW_HI)
578 goto log;
Jacob Shin89b831e2005-11-05 17:25:53 +0100579 }
580 }
Chen Yucong44612a32014-10-02 14:48:19 +0200581 return;
582
583log:
Yazen Ghannam34102002016-05-11 14:58:23 +0200584 __log_error(bank, false, true, ((u64)high << 32) | low);
Jacob Shin89b831e2005-11-05 17:25:53 +0100585}
586
587/*
588 * Sysfs Interface
589 */
590
Jacob Shin89b831e2005-11-05 17:25:53 +0100591struct threshold_attr {
Jacob Shin2903ee82006-06-26 13:58:56 +0200592 struct attribute attr;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200593 ssize_t (*show) (struct threshold_block *, char *);
594 ssize_t (*store) (struct threshold_block *, const char *, size_t count);
Jacob Shin89b831e2005-11-05 17:25:53 +0100595};
596
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200597#define SHOW_FIELDS(name) \
598static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
599{ \
Borislav Petkov18c20f32012-04-27 12:31:34 +0200600 return sprintf(buf, "%lu\n", (unsigned long) b->name); \
Jacob Shin2903ee82006-06-26 13:58:56 +0200601}
Jacob Shin89b831e2005-11-05 17:25:53 +0100602SHOW_FIELDS(interrupt_enable)
603SHOW_FIELDS(threshold_limit)
604
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200605static ssize_t
Hidetoshi Seto9319cec2009-04-14 17:26:30 +0900606store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
Jacob Shin89b831e2005-11-05 17:25:53 +0100607{
Mike Travis4cd46012008-12-16 17:34:04 -0800608 struct thresh_restart tr;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200609 unsigned long new;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200610
Borislav Petkovf227d432012-04-16 18:01:53 +0200611 if (!b->interrupt_capable)
612 return -EINVAL;
613
Daniel Walter164109e2014-08-08 14:24:03 -0700614 if (kstrtoul(buf, 0, &new) < 0)
Jacob Shin89b831e2005-11-05 17:25:53 +0100615 return -EINVAL;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200616
Jacob Shin89b831e2005-11-05 17:25:53 +0100617 b->interrupt_enable = !!new;
618
Robert Richter9c37c9d2010-10-25 16:03:35 +0200619 memset(&tr, 0, sizeof(tr));
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200620 tr.b = b;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200621
Andrew Mortona6b6a142009-03-18 10:40:25 +1030622 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
Jacob Shin89b831e2005-11-05 17:25:53 +0100623
Hidetoshi Seto9319cec2009-04-14 17:26:30 +0900624 return size;
Jacob Shin89b831e2005-11-05 17:25:53 +0100625}
626
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200627static ssize_t
Hidetoshi Seto9319cec2009-04-14 17:26:30 +0900628store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
Jacob Shin89b831e2005-11-05 17:25:53 +0100629{
Mike Travis4cd46012008-12-16 17:34:04 -0800630 struct thresh_restart tr;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200631 unsigned long new;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200632
Daniel Walter164109e2014-08-08 14:24:03 -0700633 if (kstrtoul(buf, 0, &new) < 0)
Jacob Shin89b831e2005-11-05 17:25:53 +0100634 return -EINVAL;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200635
Jacob Shin89b831e2005-11-05 17:25:53 +0100636 if (new > THRESHOLD_MAX)
637 new = THRESHOLD_MAX;
638 if (new < 1)
639 new = 1;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200640
Robert Richter9c37c9d2010-10-25 16:03:35 +0200641 memset(&tr, 0, sizeof(tr));
Mike Travis4cd46012008-12-16 17:34:04 -0800642 tr.old_limit = b->threshold_limit;
Jacob Shin89b831e2005-11-05 17:25:53 +0100643 b->threshold_limit = new;
Mike Travis4cd46012008-12-16 17:34:04 -0800644 tr.b = b;
Jacob Shin89b831e2005-11-05 17:25:53 +0100645
Andrew Mortona6b6a142009-03-18 10:40:25 +1030646 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
Jacob Shin89b831e2005-11-05 17:25:53 +0100647
Hidetoshi Seto9319cec2009-04-14 17:26:30 +0900648 return size;
Jacob Shin89b831e2005-11-05 17:25:53 +0100649}
650
Jacob Shin95268662006-06-26 13:58:53 +0200651static ssize_t show_error_count(struct threshold_block *b, char *buf)
Jacob Shin89b831e2005-11-05 17:25:53 +0100652{
Borislav Petkov2c9c42f2012-04-27 12:53:59 +0200653 u32 lo, hi;
Andrew Mortona6b6a142009-03-18 10:40:25 +1030654
Borislav Petkov2c9c42f2012-04-27 12:53:59 +0200655 rdmsr_on_cpu(b->cpu, b->address, &lo, &hi);
656
657 return sprintf(buf, "%u\n", ((hi & THRESHOLD_MAX) -
658 (THRESHOLD_MAX - b->threshold_limit)));
Jacob Shin89b831e2005-11-05 17:25:53 +0100659}
660
Borislav Petkov6e927362012-04-27 15:37:25 +0200661static struct threshold_attr error_count = {
662 .attr = {.name = __stringify(error_count), .mode = 0444 },
663 .show = show_error_count,
664};
Jacob Shin89b831e2005-11-05 17:25:53 +0100665
Hidetoshi Seto34fa1962009-04-08 12:31:18 +0200666#define RW_ATTR(val) \
667static struct threshold_attr val = { \
668 .attr = {.name = __stringify(val), .mode = 0644 }, \
669 .show = show_## val, \
670 .store = store_## val, \
Jacob Shin89b831e2005-11-05 17:25:53 +0100671};
672
Jacob Shin2903ee82006-06-26 13:58:56 +0200673RW_ATTR(interrupt_enable);
674RW_ATTR(threshold_limit);
Jacob Shin89b831e2005-11-05 17:25:53 +0100675
676static struct attribute *default_attrs[] = {
Jacob Shin89b831e2005-11-05 17:25:53 +0100677 &threshold_limit.attr,
678 &error_count.attr,
Borislav Petkovd26ecc42012-04-16 18:20:36 +0200679 NULL, /* possibly interrupt_enable if supported, see below */
680 NULL,
Jacob Shin89b831e2005-11-05 17:25:53 +0100681};
682
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200683#define to_block(k) container_of(k, struct threshold_block, kobj)
684#define to_attr(a) container_of(a, struct threshold_attr, attr)
Jacob Shin89b831e2005-11-05 17:25:53 +0100685
686static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
687{
Jacob Shin95268662006-06-26 13:58:53 +0200688 struct threshold_block *b = to_block(kobj);
Jacob Shin89b831e2005-11-05 17:25:53 +0100689 struct threshold_attr *a = to_attr(attr);
690 ssize_t ret;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200691
Jacob Shin89b831e2005-11-05 17:25:53 +0100692 ret = a->show ? a->show(b, buf) : -EIO;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200693
Jacob Shin89b831e2005-11-05 17:25:53 +0100694 return ret;
695}
696
697static ssize_t store(struct kobject *kobj, struct attribute *attr,
698 const char *buf, size_t count)
699{
Jacob Shin95268662006-06-26 13:58:53 +0200700 struct threshold_block *b = to_block(kobj);
Jacob Shin89b831e2005-11-05 17:25:53 +0100701 struct threshold_attr *a = to_attr(attr);
702 ssize_t ret;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200703
Jacob Shin89b831e2005-11-05 17:25:53 +0100704 ret = a->store ? a->store(b, buf, count) : -EIO;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200705
Jacob Shin89b831e2005-11-05 17:25:53 +0100706 return ret;
707}
708
Emese Revfy52cf25d2010-01-19 02:58:23 +0100709static const struct sysfs_ops threshold_ops = {
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200710 .show = show,
711 .store = store,
Jacob Shin89b831e2005-11-05 17:25:53 +0100712};
713
714static struct kobj_type threshold_ktype = {
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200715 .sysfs_ops = &threshold_ops,
716 .default_attrs = default_attrs,
Jacob Shin89b831e2005-11-05 17:25:53 +0100717};
718
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400719static int allocate_threshold_blocks(unsigned int cpu, unsigned int bank,
720 unsigned int block, u32 address)
Jacob Shin89b831e2005-11-05 17:25:53 +0100721{
Jacob Shin95268662006-06-26 13:58:53 +0200722 struct threshold_block *b = NULL;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200723 u32 low, high;
724 int err;
Jacob Shin95268662006-06-26 13:58:53 +0200725
Boris Ostrovskybafcdd32013-03-14 17:10:41 -0400726 if ((bank >= mca_cfg.banks) || (block >= NR_BLOCKS))
Jacob Shin95268662006-06-26 13:58:53 +0200727 return 0;
728
Andrew Mortona6b6a142009-03-18 10:40:25 +1030729 if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
Jan Beulich24ce0e92007-02-13 13:26:23 +0100730 return 0;
Jacob Shin95268662006-06-26 13:58:53 +0200731
732 if (!(high & MASK_VALID_HI)) {
733 if (block)
734 goto recurse;
735 else
736 return 0;
737 }
738
Jan Beulich24ce0e92007-02-13 13:26:23 +0100739 if (!(high & MASK_CNTP_HI) ||
740 (high & MASK_LOCKED_HI))
Jacob Shin95268662006-06-26 13:58:53 +0200741 goto recurse;
742
743 b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
744 if (!b)
745 return -ENOMEM;
Jacob Shin95268662006-06-26 13:58:53 +0200746
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200747 b->block = block;
748 b->bank = bank;
749 b->cpu = cpu;
750 b->address = address;
751 b->interrupt_enable = 0;
Borislav Petkovf227d432012-04-16 18:01:53 +0200752 b->interrupt_capable = lvt_interrupt_supported(bank, high);
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200753 b->threshold_limit = THRESHOLD_MAX;
Jacob Shin95268662006-06-26 13:58:53 +0200754
Aravind Gopalakrishnand79f9312015-02-02 11:02:41 -0600755 if (b->interrupt_capable) {
Borislav Petkovd26ecc42012-04-16 18:20:36 +0200756 threshold_ktype.default_attrs[2] = &interrupt_enable.attr;
Aravind Gopalakrishnand79f9312015-02-02 11:02:41 -0600757 b->interrupt_enable = 1;
758 } else {
Borislav Petkovd26ecc42012-04-16 18:20:36 +0200759 threshold_ktype.default_attrs[2] = NULL;
Aravind Gopalakrishnand79f9312015-02-02 11:02:41 -0600760 }
Borislav Petkovd26ecc42012-04-16 18:20:36 +0200761
Jacob Shin95268662006-06-26 13:58:53 +0200762 INIT_LIST_HEAD(&b->miscj);
763
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200764 if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
Jacob Shin95268662006-06-26 13:58:53 +0200765 list_add(&b->miscj,
766 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200767 } else {
Jacob Shin95268662006-06-26 13:58:53 +0200768 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200769 }
Jacob Shin95268662006-06-26 13:58:53 +0200770
Greg Kroah-Hartman542eb752007-12-19 09:23:20 -0800771 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
772 per_cpu(threshold_banks, cpu)[bank]->kobj,
Borislav Petkov336d3352012-05-04 17:05:27 +0200773 (bank == 4 ? bank4_names(b) : th_names[bank]));
Jacob Shin95268662006-06-26 13:58:53 +0200774 if (err)
775 goto out_free;
776recurse:
Aravind Gopalakrishnan8dd1e172016-03-07 14:02:19 +0100777 address = get_block_address(address, low, high, bank, ++block);
778 if (!address)
779 return 0;
Jacob Shin95268662006-06-26 13:58:53 +0200780
Aravind Gopalakrishnan8dd1e172016-03-07 14:02:19 +0100781 err = allocate_threshold_blocks(cpu, bank, block, address);
Jacob Shin95268662006-06-26 13:58:53 +0200782 if (err)
783 goto out_free;
784
Greg KH213eca7f2008-01-30 13:29:58 +0100785 if (b)
786 kobject_uevent(&b->kobj, KOBJ_ADD);
Greg Kroah-Hartman542eb752007-12-19 09:23:20 -0800787
Jacob Shin95268662006-06-26 13:58:53 +0200788 return err;
789
790out_free:
791 if (b) {
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800792 kobject_put(&b->kobj);
Julia Lawalld9a5ac92011-05-13 15:52:09 +0200793 list_del(&b->miscj);
Jacob Shin95268662006-06-26 13:58:53 +0200794 kfree(b);
795 }
796 return err;
797}
798
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400799static int __threshold_add_blocks(struct threshold_bank *b)
Borislav Petkov019f34f2012-05-02 17:16:59 +0200800{
801 struct list_head *head = &b->blocks->miscj;
802 struct threshold_block *pos = NULL;
803 struct threshold_block *tmp = NULL;
804 int err = 0;
805
806 err = kobject_add(&b->blocks->kobj, b->kobj, b->blocks->kobj.name);
807 if (err)
808 return err;
809
810 list_for_each_entry_safe(pos, tmp, head, miscj) {
811
812 err = kobject_add(&pos->kobj, b->kobj, pos->kobj.name);
813 if (err) {
814 list_for_each_entry_safe_reverse(pos, tmp, head, miscj)
815 kobject_del(&pos->kobj);
816
817 return err;
818 }
819 }
820 return err;
821}
822
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400823static int threshold_create_bank(unsigned int cpu, unsigned int bank)
Jacob Shin95268662006-06-26 13:58:53 +0200824{
Greg Kroah-Hartmand6126ef2012-01-26 15:49:14 -0800825 struct device *dev = per_cpu(mce_device, cpu);
Borislav Petkov019f34f2012-05-02 17:16:59 +0200826 struct amd_northbridge *nb = NULL;
Borislav Petkov92e26e22012-05-02 16:20:49 +0200827 struct threshold_bank *b = NULL;
Borislav Petkov336d3352012-05-04 17:05:27 +0200828 const char *name = th_names[bank];
Borislav Petkov92e26e22012-05-02 16:20:49 +0200829 int err = 0;
Jacob Shin95268662006-06-26 13:58:53 +0200830
Boris Ostrovskyc76e8162013-03-14 17:10:40 -0400831 if (is_shared_bank(bank)) {
Borislav Petkov019f34f2012-05-02 17:16:59 +0200832 nb = node_to_amd_nb(amd_get_nb_id(cpu));
Borislav Petkov019f34f2012-05-02 17:16:59 +0200833
834 /* threshold descriptor already initialized on this node? */
Daniel J Blueman21c5e502012-10-01 14:42:05 +0800835 if (nb && nb->bank4) {
Borislav Petkov019f34f2012-05-02 17:16:59 +0200836 /* yes, use it */
837 b = nb->bank4;
838 err = kobject_add(b->kobj, &dev->kobj, name);
839 if (err)
840 goto out;
841
842 per_cpu(threshold_banks, cpu)[bank] = b;
843 atomic_inc(&b->cpus);
844
845 err = __threshold_add_blocks(b);
846
847 goto out;
848 }
849 }
850
Jacob Shin95268662006-06-26 13:58:53 +0200851 b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
Jacob Shin89b831e2005-11-05 17:25:53 +0100852 if (!b) {
853 err = -ENOMEM;
854 goto out;
855 }
Jacob Shin89b831e2005-11-05 17:25:53 +0100856
Greg Kroah-Hartmane032d8072012-01-16 14:40:28 -0800857 b->kobj = kobject_create_and_add(name, &dev->kobj);
Borislav Petkov92e26e22012-05-02 16:20:49 +0200858 if (!b->kobj) {
859 err = -EINVAL;
Greg Kroah-Hartmana521cf22007-12-19 09:23:20 -0800860 goto out_free;
Borislav Petkov92e26e22012-05-02 16:20:49 +0200861 }
Jacob Shin95268662006-06-26 13:58:53 +0200862
Jacob Shin89b831e2005-11-05 17:25:53 +0100863 per_cpu(threshold_banks, cpu)[bank] = b;
Jacob Shin95268662006-06-26 13:58:53 +0200864
Boris Ostrovskyc76e8162013-03-14 17:10:40 -0400865 if (is_shared_bank(bank)) {
Borislav Petkov019f34f2012-05-02 17:16:59 +0200866 atomic_set(&b->cpus, 1);
867
868 /* nb is already initialized, see above */
Daniel J Blueman21c5e502012-10-01 14:42:05 +0800869 if (nb) {
870 WARN_ON(nb->bank4);
871 nb->bank4 = b;
872 }
Borislav Petkov019f34f2012-05-02 17:16:59 +0200873 }
874
Yazen Ghannam74ab0e72016-09-12 09:59:27 +0200875 err = allocate_threshold_blocks(cpu, bank, 0, msr_ops.misc(bank));
Borislav Petkov92e26e22012-05-02 16:20:49 +0200876 if (!err)
877 goto out;
Jacob Shin95268662006-06-26 13:58:53 +0200878
Borislav Petkov019f34f2012-05-02 17:16:59 +0200879 out_free:
Jacob Shin95268662006-06-26 13:58:53 +0200880 kfree(b);
Borislav Petkov019f34f2012-05-02 17:16:59 +0200881
882 out:
Jacob Shin89b831e2005-11-05 17:25:53 +0100883 return err;
884}
885
886/* create dir/files for all valid threshold banks */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400887static int threshold_create_device(unsigned int cpu)
Jacob Shin89b831e2005-11-05 17:25:53 +0100888{
Jacob Shin2903ee82006-06-26 13:58:56 +0200889 unsigned int bank;
Boris Ostrovskybafcdd32013-03-14 17:10:41 -0400890 struct threshold_bank **bp;
Jacob Shin89b831e2005-11-05 17:25:53 +0100891 int err = 0;
892
Boris Ostrovskybafcdd32013-03-14 17:10:41 -0400893 bp = kzalloc(sizeof(struct threshold_bank *) * mca_cfg.banks,
894 GFP_KERNEL);
895 if (!bp)
896 return -ENOMEM;
897
898 per_cpu(threshold_banks, cpu) = bp;
899
900 for (bank = 0; bank < mca_cfg.banks; ++bank) {
Yinghai Lu5a96f4a2008-01-30 13:33:40 +0100901 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
Jacob Shin89b831e2005-11-05 17:25:53 +0100902 continue;
903 err = threshold_create_bank(cpu, bank);
904 if (err)
Robert Richter0a179412010-10-25 16:03:38 +0200905 return err;
Jacob Shin89b831e2005-11-05 17:25:53 +0100906 }
Robert Richter0a179412010-10-25 16:03:38 +0200907
Jacob Shin89b831e2005-11-05 17:25:53 +0100908 return err;
909}
910
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700911static void deallocate_threshold_block(unsigned int cpu,
Jacob Shin95268662006-06-26 13:58:53 +0200912 unsigned int bank)
913{
914 struct threshold_block *pos = NULL;
915 struct threshold_block *tmp = NULL;
916 struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
917
918 if (!head)
919 return;
920
921 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800922 kobject_put(&pos->kobj);
Jacob Shin95268662006-06-26 13:58:53 +0200923 list_del(&pos->miscj);
924 kfree(pos);
925 }
926
927 kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
928 per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
929}
930
Borislav Petkov019f34f2012-05-02 17:16:59 +0200931static void __threshold_remove_blocks(struct threshold_bank *b)
932{
933 struct threshold_block *pos = NULL;
934 struct threshold_block *tmp = NULL;
935
936 kobject_del(b->kobj);
937
938 list_for_each_entry_safe(pos, tmp, &b->blocks->miscj, miscj)
939 kobject_del(&pos->kobj);
940}
941
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700942static void threshold_remove_bank(unsigned int cpu, int bank)
Jacob Shin89b831e2005-11-05 17:25:53 +0100943{
Borislav Petkov019f34f2012-05-02 17:16:59 +0200944 struct amd_northbridge *nb;
Jacob Shin89b831e2005-11-05 17:25:53 +0100945 struct threshold_bank *b;
Jacob Shin89b831e2005-11-05 17:25:53 +0100946
947 b = per_cpu(threshold_banks, cpu)[bank];
948 if (!b)
949 return;
Borislav Petkov019f34f2012-05-02 17:16:59 +0200950
Jacob Shin95268662006-06-26 13:58:53 +0200951 if (!b->blocks)
952 goto free_out;
953
Boris Ostrovskyc76e8162013-03-14 17:10:40 -0400954 if (is_shared_bank(bank)) {
Borislav Petkov019f34f2012-05-02 17:16:59 +0200955 if (!atomic_dec_and_test(&b->cpus)) {
956 __threshold_remove_blocks(b);
957 per_cpu(threshold_banks, cpu)[bank] = NULL;
958 return;
959 } else {
960 /*
961 * the last CPU on this node using the shared bank is
962 * going away, remove that bank now.
963 */
964 nb = node_to_amd_nb(amd_get_nb_id(cpu));
965 nb->bank4 = NULL;
966 }
967 }
968
Jacob Shin95268662006-06-26 13:58:53 +0200969 deallocate_threshold_block(cpu, bank);
970
971free_out:
Rafael J. Wysocki87357282008-08-22 22:23:09 +0200972 kobject_del(b->kobj);
Greg Kroah-Hartman38a382a2007-12-20 08:13:05 -0800973 kobject_put(b->kobj);
Jacob Shin95268662006-06-26 13:58:53 +0200974 kfree(b);
975 per_cpu(threshold_banks, cpu)[bank] = NULL;
Jacob Shin89b831e2005-11-05 17:25:53 +0100976}
977
Chandra Seetharamanbe6b5a32006-07-30 03:03:37 -0700978static void threshold_remove_device(unsigned int cpu)
Jacob Shin89b831e2005-11-05 17:25:53 +0100979{
Jacob Shin2903ee82006-06-26 13:58:56 +0200980 unsigned int bank;
Jacob Shin89b831e2005-11-05 17:25:53 +0100981
Boris Ostrovskybafcdd32013-03-14 17:10:41 -0400982 for (bank = 0; bank < mca_cfg.banks; ++bank) {
Yinghai Lu5a96f4a2008-01-30 13:33:40 +0100983 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
Jacob Shin89b831e2005-11-05 17:25:53 +0100984 continue;
985 threshold_remove_bank(cpu, bank);
986 }
Boris Ostrovskybafcdd32013-03-14 17:10:41 -0400987 kfree(per_cpu(threshold_banks, cpu));
Jacob Shin89b831e2005-11-05 17:25:53 +0100988}
989
Jacob Shin89b831e2005-11-05 17:25:53 +0100990/* get notified when a cpu comes on/off */
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400991static void
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +0200992amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
Jacob Shin89b831e2005-11-05 17:25:53 +0100993{
Jacob Shin89b831e2005-11-05 17:25:53 +0100994 switch (action) {
995 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -0700996 case CPU_ONLINE_FROZEN:
Jacob Shin89b831e2005-11-05 17:25:53 +0100997 threshold_create_device(cpu);
Jacob Shin89b831e2005-11-05 17:25:53 +0100998 break;
999 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001000 case CPU_DEAD_FROZEN:
Jacob Shin89b831e2005-11-05 17:25:53 +01001001 threshold_remove_device(cpu);
1002 break;
1003 default:
1004 break;
1005 }
Jacob Shin89b831e2005-11-05 17:25:53 +01001006}
1007
Jacob Shin89b831e2005-11-05 17:25:53 +01001008static __init int threshold_init_device(void)
1009{
Jacob Shin2903ee82006-06-26 13:58:56 +02001010 unsigned lcpu = 0;
Jacob Shin89b831e2005-11-05 17:25:53 +01001011
Jacob Shin89b831e2005-11-05 17:25:53 +01001012 /* to hit CPUs online before the notifier is up */
1013 for_each_online_cpu(lcpu) {
Jacob Shinfff2e892006-06-26 13:58:50 +02001014 int err = threshold_create_device(lcpu);
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +02001015
Jacob Shin89b831e2005-11-05 17:25:53 +01001016 if (err)
Jacob Shinfff2e892006-06-26 13:58:50 +02001017 return err;
Jacob Shin89b831e2005-11-05 17:25:53 +01001018 }
Rafael J. Wysocki87357282008-08-22 22:23:09 +02001019 threshold_cpu_callback = amd_64_threshold_cpu_callback;
Ingo Molnar1cb2a8e2009-04-08 12:31:18 +02001020
Jacob Shinfff2e892006-06-26 13:58:50 +02001021 return 0;
Jacob Shin89b831e2005-11-05 17:25:53 +01001022}
Liu, Jinsonga8fccdb2012-06-07 19:58:50 +08001023/*
1024 * there are 3 funcs which need to be _initcalled in a logic sequence:
1025 * 1. xen_late_init_mcelog
1026 * 2. mcheck_init_device
1027 * 3. threshold_init_device
1028 *
1029 * xen_late_init_mcelog must register xen_mce_chrdev_device before
1030 * native mce_chrdev_device registration if running under xen platform;
1031 *
1032 * mcheck_init_device should be inited before threshold_init_device to
1033 * initialize mce_device, otherwise a NULL ptr dereference will cause panic.
1034 *
1035 * so we use following _initcalls
1036 * 1. device_initcall(xen_late_init_mcelog);
1037 * 2. device_initcall_sync(mcheck_init_device);
1038 * 3. late_initcall(threshold_init_device);
1039 *
1040 * when running under xen, the initcall order is 1,2,3;
1041 * on baremetal, we skip 1 and we do only 2 and 3.
1042 */
1043late_initcall(threshold_init_device);