mce_amd.c 16.0 KB
Newer Older
1
/*
2
 *  (c) 2005, 2006 Advanced Micro Devices, Inc.
3 4 5 6 7 8 9 10
 *  Your use of this code is subject to the terms and conditions of the
 *  GNU general public license version 2. See "COPYING" or
 *  http://www.gnu.org/licenses/gpl.html
 *
 *  Written by Jacob Shin - AMD, Inc.
 *
 *  Support : jacob.shin@amd.com
 *
11 12
 *  April 2006
 *     - added support for AMD Family 0x10 processors
13
 *
14
 *  All MC4_MISCi registers are shared between multi-cores
15 16 17
 */
#include <linux/interrupt.h>
#include <linux/notifier.h>
I
Ingo Molnar 已提交
18
#include <linux/kobject.h>
19
#include <linux/percpu.h>
20
#include <linux/sysdev.h>
I
Ingo Molnar 已提交
21 22
#include <linux/errno.h>
#include <linux/sched.h>
23
#include <linux/sysfs.h>
24
#include <linux/slab.h>
I
Ingo Molnar 已提交
25 26 27 28
#include <linux/init.h>
#include <linux/cpu.h>
#include <linux/smp.h>

29
#include <asm/apic.h>
I
Ingo Molnar 已提交
30
#include <asm/idle.h>
31 32 33
#include <asm/mce.h>
#include <asm/msr.h>

J
Jacob Shin 已提交
34 35 36 37 38 39 40
#define PFX               "mce_threshold: "
#define VERSION           "version 1.1.1"
#define NR_BANKS          6
#define NR_BLOCKS         9
#define THRESHOLD_MAX     0xFFF
#define INT_TYPE_APIC     0x00020000
#define MASK_VALID_HI     0x80000000
41 42
#define MASK_CNTP_HI      0x40000000
#define MASK_LOCKED_HI    0x20000000
J
Jacob Shin 已提交
43 44 45 46
#define MASK_LVTOFF_HI    0x00F00000
#define MASK_COUNT_EN_HI  0x00080000
#define MASK_INT_TYPE_HI  0x00060000
#define MASK_OVERFLOW_HI  0x00010000
47
#define MASK_ERR_COUNT_HI 0x00000FFF
48 49
#define MASK_BLKPTR_LO    0xFF000000
#define MCG_XBLK_ADDR     0xC0000400
50

51
struct threshold_block {
I
Ingo Molnar 已提交
52 53 54 55 56 57 58 59
	unsigned int		block;
	unsigned int		bank;
	unsigned int		cpu;
	u32			address;
	u16			interrupt_enable;
	u16			threshold_limit;
	struct kobject		kobj;
	struct list_head	miscj;
60 61
};

62
struct threshold_bank {
I
Ingo Molnar 已提交
63 64 65
	struct kobject		*kobj;
	struct threshold_block	*blocks;
	cpumask_var_t		cpus;
66
};
67
static DEFINE_PER_CPU(struct threshold_bank * [NR_BANKS], threshold_banks);
68

69 70 71 72 73 74 75 76
#ifdef CONFIG_SMP
static unsigned char shared_bank[NR_BANKS] = {
	0, 0, 0, 0, 1
};
#endif

static DEFINE_PER_CPU(unsigned char, bank_map);	/* see which banks are on */

77 78
static void amd_threshold_interrupt(void);

79 80 81 82
/*
 * CPU Initialization
 */

83
struct thresh_restart {
I
Ingo Molnar 已提交
84 85
	struct threshold_block	*b;
	int			reset;
86 87
	int			set_lvt_off;
	int			lvt_off;
I
Ingo Molnar 已提交
88
	u16			old_limit;
89 90
};

91
/* must be called with correct cpu affinity */
92 93
/* Called via smp_call_function_single() */
static void threshold_restart_bank(void *_tr)
94
{
95
	struct thresh_restart *tr = _tr;
96 97
	u32 mci_misc_hi, mci_misc_lo;

98
	rdmsr(tr->b->address, mci_misc_lo, mci_misc_hi);
99

100 101
	if (tr->b->threshold_limit < (mci_misc_hi & THRESHOLD_MAX))
		tr->reset = 1;	/* limit cannot be lower than err count */
102

103
	if (tr->reset) {		/* reset err count and overflow bit */
104 105
		mci_misc_hi =
		    (mci_misc_hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
106 107
		    (THRESHOLD_MAX - tr->b->threshold_limit);
	} else if (tr->old_limit) {	/* change limit w/o reset */
108
		int new_count = (mci_misc_hi & THRESHOLD_MAX) +
109
		    (tr->old_limit - tr->b->threshold_limit);
I
Ingo Molnar 已提交
110

111 112 113 114
		mci_misc_hi = (mci_misc_hi & ~MASK_ERR_COUNT_HI) |
		    (new_count & THRESHOLD_MAX);
	}

115 116 117 118 119 120
	if (tr->set_lvt_off) {
		/* set new lvt offset */
		mci_misc_hi &= ~MASK_LVTOFF_HI;
		mci_misc_hi |= tr->lvt_off << 20;
	}

121
	tr->b->interrupt_enable ?
122 123 124 125
	    (mci_misc_hi = (mci_misc_hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) :
	    (mci_misc_hi &= ~MASK_INT_TYPE_HI);

	mci_misc_hi |= MASK_COUNT_EN_HI;
126
	wrmsr(tr->b->address, mci_misc_lo, mci_misc_hi);
127 128
}

129 130 131 132 133 134 135 136 137 138 139 140
static void mce_threshold_block_init(struct threshold_block *b, int offset)
{
	struct thresh_restart tr = {
		.b			= b,
		.set_lvt_off		= 1,
		.lvt_off		= offset,
	};

	b->threshold_limit		= THRESHOLD_MAX;
	threshold_restart_bank(&tr);
};

141
/* cpu init entry point, called from mce.c with preempt off */
142
void mce_amd_feature_init(struct cpuinfo_x86 *c)
143
{
144
	struct threshold_block b;
145
	unsigned int cpu = smp_processor_id();
146
	u32 low = 0, high = 0, address = 0;
I
Ingo Molnar 已提交
147
	unsigned int bank, block;
148 149
	int lvt_off = -1;
	u8 offset;
150 151

	for (bank = 0; bank < NR_BANKS; ++bank) {
152 153 154
		for (block = 0; block < NR_BLOCKS; ++block) {
			if (block == 0)
				address = MSR_IA32_MC0_MISC + bank * 4;
155 156 157 158
			else if (block == 1) {
				address = (low & MASK_BLKPTR_LO) >> 21;
				if (!address)
					break;
159

160
				address += MCG_XBLK_ADDR;
I
Ingo Molnar 已提交
161
			} else
162 163 164
				++address;

			if (rdmsr_safe(address, &low, &high))
165
				break;
166

167 168
			if (!(high & MASK_VALID_HI))
				continue;
169

170 171
			if (!(high & MASK_CNTP_HI)  ||
			     (high & MASK_LOCKED_HI))
172 173 174 175
				continue;

			if (!block)
				per_cpu(bank_map, cpu) |= (1 << bank);
176
#ifdef CONFIG_SMP
177 178
			if (shared_bank[bank] && c->cpu_core_id)
				break;
179
#endif
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
			offset = (high & MASK_LVTOFF_HI) >> 20;
			if (lvt_off < 0) {
				if (setup_APIC_eilvt(offset,
						     THRESHOLD_APIC_VECTOR,
						     APIC_EILVT_MSG_FIX, 0)) {
					pr_err(FW_BUG "cpu %d, failed to "
					       "setup threshold interrupt "
					       "for bank %d, block %d "
					       "(MSR%08X=0x%x%08x)",
					       smp_processor_id(), bank, block,
					       address, high, low);
					continue;
				}
				lvt_off = offset;
			} else if (lvt_off != offset) {
				pr_err(FW_BUG "cpu %d, invalid threshold "
				       "interrupt offset %d for bank %d,"
				       "block %d (MSR%08X=0x%x%08x)",
				       smp_processor_id(), lvt_off, bank,
				       block, address, high, low);
				continue;
			}
202

203 204 205 206 207
			memset(&b, 0, sizeof(b));
			b.cpu		= cpu;
			b.bank		= bank;
			b.block		= block;
			b.address	= address;
208

209
			mce_threshold_block_init(&b, offset);
210
			mce_threshold_vector = amd_threshold_interrupt;
211
		}
212 213 214 215 216 217 218 219 220 221 222 223
	}
}

/*
 * APIC Interrupt Handler
 */

/*
 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
 * the interrupt goes off when error_count reaches threshold_limit.
 * the handler will simply log mcelog w/ software defined bank number.
 */
224
static void amd_threshold_interrupt(void)
225
{
I
Ingo Molnar 已提交
226
	u32 low = 0, high = 0, address = 0;
227
	unsigned int bank, block;
228 229
	struct mce m;

230
	mce_setup(&m);
231 232 233

	/* assume first bank caused it */
	for (bank = 0; bank < NR_BANKS; ++bank) {
234 235
		if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
			continue;
236
		for (block = 0; block < NR_BLOCKS; ++block) {
I
Ingo Molnar 已提交
237
			if (block == 0) {
238
				address = MSR_IA32_MC0_MISC + bank * 4;
I
Ingo Molnar 已提交
239
			} else if (block == 1) {
240 241 242 243
				address = (low & MASK_BLKPTR_LO) >> 21;
				if (!address)
					break;
				address += MCG_XBLK_ADDR;
I
Ingo Molnar 已提交
244
			} else {
245
				++address;
I
Ingo Molnar 已提交
246
			}
247 248

			if (rdmsr_safe(address, &low, &high))
249
				break;
250 251 252 253 254 255 256 257

			if (!(high & MASK_VALID_HI)) {
				if (block)
					continue;
				else
					break;
			}

258 259
			if (!(high & MASK_CNTP_HI)  ||
			     (high & MASK_LOCKED_HI))
260 261
				continue;

I
Ingo Molnar 已提交
262 263 264 265
			/*
			 * Log the machine check that caused the threshold
			 * event.
			 */
266 267
			machine_check_poll(MCP_TIMESTAMP,
					&__get_cpu_var(mce_poll_banks));
268

269 270 271 272 273 274 275 276
			if (high & MASK_OVERFLOW_HI) {
				rdmsrl(address, m.misc);
				rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
				       m.status);
				m.bank = K8_MCE_THRESHOLD_BASE
				       + bank * NR_BLOCKS
				       + block;
				mce_log(&m);
277
				return;
278
			}
279 280 281 282 283 284 285 286 287
		}
	}
}

/*
 * Sysfs Interface
 */

struct threshold_attr {
J
Jacob Shin 已提交
288
	struct attribute attr;
I
Ingo Molnar 已提交
289 290
	ssize_t (*show) (struct threshold_block *, char *);
	ssize_t (*store) (struct threshold_block *, const char *, size_t count);
291 292
};

I
Ingo Molnar 已提交
293 294 295 296
#define SHOW_FIELDS(name)						\
static ssize_t show_ ## name(struct threshold_block *b, char *buf)	\
{									\
	return sprintf(buf, "%lx\n", (unsigned long) b->name);		\
J
Jacob Shin 已提交
297
}
298 299 300
SHOW_FIELDS(interrupt_enable)
SHOW_FIELDS(threshold_limit)

I
Ingo Molnar 已提交
301
static ssize_t
H
Hidetoshi Seto 已提交
302
store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
303
{
304
	struct thresh_restart tr;
I
Ingo Molnar 已提交
305 306
	unsigned long new;

H
Hidetoshi Seto 已提交
307
	if (strict_strtoul(buf, 0, &new) < 0)
308
		return -EINVAL;
I
Ingo Molnar 已提交
309

310 311
	b->interrupt_enable = !!new;

312
	memset(&tr, 0, sizeof(tr));
I
Ingo Molnar 已提交
313 314
	tr.b		= b;

315
	smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
316

H
Hidetoshi Seto 已提交
317
	return size;
318 319
}

I
Ingo Molnar 已提交
320
static ssize_t
H
Hidetoshi Seto 已提交
321
store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
322
{
323
	struct thresh_restart tr;
I
Ingo Molnar 已提交
324 325
	unsigned long new;

H
Hidetoshi Seto 已提交
326
	if (strict_strtoul(buf, 0, &new) < 0)
327
		return -EINVAL;
I
Ingo Molnar 已提交
328

329 330 331 332
	if (new > THRESHOLD_MAX)
		new = THRESHOLD_MAX;
	if (new < 1)
		new = 1;
I
Ingo Molnar 已提交
333

334
	memset(&tr, 0, sizeof(tr));
335
	tr.old_limit = b->threshold_limit;
336
	b->threshold_limit = new;
337
	tr.b = b;
338

339
	smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
340

H
Hidetoshi Seto 已提交
341
	return size;
342 343
}

344
struct threshold_block_cross_cpu {
I
Ingo Molnar 已提交
345 346
	struct threshold_block	*tb;
	long			retval;
347 348 349
};

static void local_error_count_handler(void *_tbcc)
350
{
351 352
	struct threshold_block_cross_cpu *tbcc = _tbcc;
	struct threshold_block *b = tbcc->tb;
353 354
	u32 low, high;

355
	rdmsr(b->address, low, high);
356
	tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
357 358 359 360
}

static ssize_t show_error_count(struct threshold_block *b, char *buf)
{
361 362 363 364
	struct threshold_block_cross_cpu tbcc = { .tb = b, };

	smp_call_function_single(b->cpu, local_error_count_handler, &tbcc, 1);
	return sprintf(buf, "%lx\n", tbcc.retval);
365 366
}

367
static ssize_t store_error_count(struct threshold_block *b,
368 369
				 const char *buf, size_t count)
{
370 371
	struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };

372
	smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
373 374 375
	return 1;
}

376 377 378 379 380
#define RW_ATTR(val)							\
static struct threshold_attr val = {					\
	.attr	= {.name = __stringify(val), .mode = 0644 },		\
	.show	= show_## val,						\
	.store	= store_## val,						\
381 382
};

J
Jacob Shin 已提交
383 384 385
RW_ATTR(interrupt_enable);
RW_ATTR(threshold_limit);
RW_ATTR(error_count);
386 387 388 389 390 391 392 393

static struct attribute *default_attrs[] = {
	&interrupt_enable.attr,
	&threshold_limit.attr,
	&error_count.attr,
	NULL
};

I
Ingo Molnar 已提交
394 395
#define to_block(k)	container_of(k, struct threshold_block, kobj)
#define to_attr(a)	container_of(a, struct threshold_attr, attr)
396 397 398

static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
{
399
	struct threshold_block *b = to_block(kobj);
400 401
	struct threshold_attr *a = to_attr(attr);
	ssize_t ret;
I
Ingo Molnar 已提交
402

403
	ret = a->show ? a->show(b, buf) : -EIO;
I
Ingo Molnar 已提交
404

405 406 407 408 409 410
	return ret;
}

static ssize_t store(struct kobject *kobj, struct attribute *attr,
		     const char *buf, size_t count)
{
411
	struct threshold_block *b = to_block(kobj);
412 413
	struct threshold_attr *a = to_attr(attr);
	ssize_t ret;
I
Ingo Molnar 已提交
414

415
	ret = a->store ? a->store(b, buf, count) : -EIO;
I
Ingo Molnar 已提交
416

417 418 419
	return ret;
}

420
static const struct sysfs_ops threshold_ops = {
I
Ingo Molnar 已提交
421 422
	.show			= show,
	.store			= store,
423 424 425
};

static struct kobj_type threshold_ktype = {
I
Ingo Molnar 已提交
426 427
	.sysfs_ops		= &threshold_ops,
	.default_attrs		= default_attrs,
428 429
};

430 431 432 433 434 435
static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
					       unsigned int bank,
					       unsigned int block,
					       u32 address)
{
	struct threshold_block *b = NULL;
I
Ingo Molnar 已提交
436 437
	u32 low, high;
	int err;
438 439 440 441

	if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
		return 0;

442
	if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
443
		return 0;
444 445 446 447 448 449 450 451

	if (!(high & MASK_VALID_HI)) {
		if (block)
			goto recurse;
		else
			return 0;
	}

452 453
	if (!(high & MASK_CNTP_HI)  ||
	     (high & MASK_LOCKED_HI))
454 455 456 457 458 459
		goto recurse;

	b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
	if (!b)
		return -ENOMEM;

I
Ingo Molnar 已提交
460 461 462 463 464 465
	b->block		= block;
	b->bank			= bank;
	b->cpu			= cpu;
	b->address		= address;
	b->interrupt_enable	= 0;
	b->threshold_limit	= THRESHOLD_MAX;
466 467 468

	INIT_LIST_HEAD(&b->miscj);

I
Ingo Molnar 已提交
469
	if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
470 471
		list_add(&b->miscj,
			 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
I
Ingo Molnar 已提交
472
	} else {
473
		per_cpu(threshold_banks, cpu)[bank]->blocks = b;
I
Ingo Molnar 已提交
474
	}
475

476 477 478
	err = kobject_init_and_add(&b->kobj, &threshold_ktype,
				   per_cpu(threshold_banks, cpu)[bank]->kobj,
				   "misc%i", block);
479 480 481 482 483 484 485 486
	if (err)
		goto out_free;
recurse:
	if (!block) {
		address = (low & MASK_BLKPTR_LO) >> 21;
		if (!address)
			return 0;
		address += MCG_XBLK_ADDR;
I
Ingo Molnar 已提交
487
	} else {
488
		++address;
I
Ingo Molnar 已提交
489
	}
490 491 492 493 494

	err = allocate_threshold_blocks(cpu, bank, ++block, address);
	if (err)
		goto out_free;

495 496
	if (b)
		kobject_uevent(&b->kobj, KOBJ_ADD);
497

498 499 500 501
	return err;

out_free:
	if (b) {
502
		kobject_put(&b->kobj);
503 504 505 506 507
		kfree(b);
	}
	return err;
}

508 509
static __cpuinit long
local_allocate_threshold_blocks(int cpu, unsigned int bank)
510
{
511 512
	return allocate_threshold_blocks(cpu, bank, 0,
					 MSR_IA32_MC0_MISC + bank * 4);
513 514
}

515
/* symlinks sibling shared banks to first core.  first core owns dir/files. */
516
static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
517
{
518
	int i, err = 0;
519
	struct threshold_bank *b = NULL;
520
	char name[32];
521
#ifdef CONFIG_SMP
522
	struct cpuinfo_x86 *c = &cpu_data(cpu);
523
#endif
524 525

	sprintf(name, "threshold_bank%i", bank);
526 527

#ifdef CONFIG_SMP
528
	if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) {	/* symlink */
529
		i = cpumask_first(c->llc_shared_map);
530 531

		/* first core not up yet */
532
		if (cpu_data(i).cpu_core_id)
533 534 535 536 537 538 539
			goto out;

		/* already linked */
		if (per_cpu(threshold_banks, cpu)[bank])
			goto out;

		b = per_cpu(threshold_banks, i)[bank];
540 541 542

		if (!b)
			goto out;
543

I
Ingo Molnar 已提交
544
		err = sysfs_create_link(&per_cpu(mce_dev, cpu).kobj,
545
					b->kobj, name);
546 547
		if (err)
			goto out;
548

549
		cpumask_copy(b->cpus, c->llc_shared_map);
550
		per_cpu(threshold_banks, cpu)[bank] = b;
I
Ingo Molnar 已提交
551

552 553 554 555
		goto out;
	}
#endif

556
	b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
557 558 559 560
	if (!b) {
		err = -ENOMEM;
		goto out;
	}
561
	if (!zalloc_cpumask_var(&b->cpus, GFP_KERNEL)) {
562 563 564 565
		kfree(b);
		err = -ENOMEM;
		goto out;
	}
566

I
Ingo Molnar 已提交
567
	b->kobj = kobject_create_and_add(name, &per_cpu(mce_dev, cpu).kobj);
568 569 570
	if (!b->kobj)
		goto out_free;

571
#ifndef CONFIG_SMP
572
	cpumask_setall(b->cpus);
573
#else
574
	cpumask_set_cpu(cpu, b->cpus);
575 576
#endif

577
	per_cpu(threshold_banks, cpu)[bank] = b;
578

579
	err = local_allocate_threshold_blocks(cpu, bank);
580 581 582
	if (err)
		goto out_free;

583
	for_each_cpu(i, b->cpus) {
584 585 586
		if (i == cpu)
			continue;

I
Ingo Molnar 已提交
587
		err = sysfs_create_link(&per_cpu(mce_dev, i).kobj,
588
					b->kobj, name);
589 590 591 592 593 594 595 596 597 598
		if (err)
			goto out;

		per_cpu(threshold_banks, i)[bank] = b;
	}

	goto out;

out_free:
	per_cpu(threshold_banks, cpu)[bank] = NULL;
599
	free_cpumask_var(b->cpus);
600
	kfree(b);
J
Jacob Shin 已提交
601
out:
602 603 604 605 606 607
	return err;
}

/* create dir/files for all valid threshold banks */
static __cpuinit int threshold_create_device(unsigned int cpu)
{
J
Jacob Shin 已提交
608
	unsigned int bank;
609 610 611
	int err = 0;

	for (bank = 0; bank < NR_BANKS; ++bank) {
612
		if (!(per_cpu(bank_map, cpu) & (1 << bank)))
613 614 615 616 617
			continue;
		err = threshold_create_bank(cpu, bank);
		if (err)
			goto out;
	}
J
Jacob Shin 已提交
618
out:
619 620 621 622 623 624 625 626 627
	return err;
}

/*
 * let's be hotplug friendly.
 * in case of multiple core processors, the first core always takes ownership
 *   of shared sysfs dir/files, and rest of the cores will be symlinked to it.
 */

628
static void deallocate_threshold_block(unsigned int cpu,
629 630 631 632 633 634 635 636 637 638
						 unsigned int bank)
{
	struct threshold_block *pos = NULL;
	struct threshold_block *tmp = NULL;
	struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];

	if (!head)
		return;

	list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
639
		kobject_put(&pos->kobj);
640 641 642 643 644 645 646 647
		list_del(&pos->miscj);
		kfree(pos);
	}

	kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
	per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
}

648
static void threshold_remove_bank(unsigned int cpu, int bank)
649 650
{
	struct threshold_bank *b;
651
	char name[32];
I
Ingo Molnar 已提交
652
	int i = 0;
653 654 655 656

	b = per_cpu(threshold_banks, cpu)[bank];
	if (!b)
		return;
657 658 659 660 661
	if (!b->blocks)
		goto free_out;

	sprintf(name, "threshold_bank%i", bank);

662
#ifdef CONFIG_SMP
663 664
	/* sibling symlink */
	if (shared_bank[bank] && b->blocks->cpu != cpu) {
I
Ingo Molnar 已提交
665
		sysfs_remove_link(&per_cpu(mce_dev, cpu).kobj, name);
666
		per_cpu(threshold_banks, cpu)[bank] = NULL;
I
Ingo Molnar 已提交
667

668
		return;
669
	}
670
#endif
671 672

	/* remove all sibling symlinks before unregistering */
673
	for_each_cpu(i, b->cpus) {
674 675 676
		if (i == cpu)
			continue;

I
Ingo Molnar 已提交
677
		sysfs_remove_link(&per_cpu(mce_dev, i).kobj, name);
678 679 680 681 682 683
		per_cpu(threshold_banks, i)[bank] = NULL;
	}

	deallocate_threshold_block(cpu, bank);

free_out:
684
	kobject_del(b->kobj);
685
	kobject_put(b->kobj);
686
	free_cpumask_var(b->cpus);
687 688
	kfree(b);
	per_cpu(threshold_banks, cpu)[bank] = NULL;
689 690
}

691
static void threshold_remove_device(unsigned int cpu)
692
{
J
Jacob Shin 已提交
693
	unsigned int bank;
694 695

	for (bank = 0; bank < NR_BANKS; ++bank) {
696
		if (!(per_cpu(bank_map, cpu) & (1 << bank)))
697 698 699 700 701 702
			continue;
		threshold_remove_bank(cpu, bank);
	}
}

/* get notified when a cpu comes on/off */
I
Ingo Molnar 已提交
703 704
static void __cpuinit
amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
705 706 707
{
	switch (action) {
	case CPU_ONLINE:
708
	case CPU_ONLINE_FROZEN:
709 710 711
		threshold_create_device(cpu);
		break;
	case CPU_DEAD:
712
	case CPU_DEAD_FROZEN:
713 714 715 716 717 718 719 720 721
		threshold_remove_device(cpu);
		break;
	default:
		break;
	}
}

static __init int threshold_init_device(void)
{
J
Jacob Shin 已提交
722
	unsigned lcpu = 0;
723 724 725

	/* to hit CPUs online before the notifier is up */
	for_each_online_cpu(lcpu) {
726
		int err = threshold_create_device(lcpu);
I
Ingo Molnar 已提交
727

728
		if (err)
729
			return err;
730
	}
731
	threshold_cpu_callback = amd_64_threshold_cpu_callback;
I
Ingo Molnar 已提交
732

733
	return 0;
734 735
}
device_initcall(threshold_init_device);