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
#define NR_BANKS          6
#define NR_BLOCKS         9
#define THRESHOLD_MAX     0xFFF
#define INT_TYPE_APIC     0x00020000
#define MASK_VALID_HI     0x80000000
39 40
#define MASK_CNTP_HI      0x40000000
#define MASK_LOCKED_HI    0x20000000
J
Jacob Shin 已提交
41 42 43 44
#define MASK_LVTOFF_HI    0x00F00000
#define MASK_COUNT_EN_HI  0x00080000
#define MASK_INT_TYPE_HI  0x00060000
#define MASK_OVERFLOW_HI  0x00010000
45
#define MASK_ERR_COUNT_HI 0x00000FFF
46 47
#define MASK_BLKPTR_LO    0xFF000000
#define MCG_XBLK_ADDR     0xC0000400
48

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

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

67 68 69 70 71 72 73 74
#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 */

75 76
static void amd_threshold_interrupt(void);

77 78 79 80
/*
 * CPU Initialization
 */

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

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
{
	int msr = (hi & MASK_LVTOFF_HI) >> 20;

	if (apic < 0) {
		pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
		       "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
		       b->bank, b->block, b->address, hi, lo);
		return 0;
	}

	if (apic != msr) {
		pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
		       "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
		       b->cpu, apic, b->bank, b->block, b->address, hi, lo);
		return 0;
	}

	return 1;
};

110
/* must be called with correct cpu affinity */
111 112
/* Called via smp_call_function_single() */
static void threshold_restart_bank(void *_tr)
113
{
114
	struct thresh_restart *tr = _tr;
115
	u32 hi, lo;
116

117
	rdmsr(tr->b->address, lo, hi);
118

119
	if (tr->b->threshold_limit < (hi & THRESHOLD_MAX))
120
		tr->reset = 1;	/* limit cannot be lower than err count */
121

122
	if (tr->reset) {		/* reset err count and overflow bit */
123 124
		hi =
		    (hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
125 126
		    (THRESHOLD_MAX - tr->b->threshold_limit);
	} else if (tr->old_limit) {	/* change limit w/o reset */
127
		int new_count = (hi & THRESHOLD_MAX) +
128
		    (tr->old_limit - tr->b->threshold_limit);
I
Ingo Molnar 已提交
129

130
		hi = (hi & ~MASK_ERR_COUNT_HI) |
131 132 133
		    (new_count & THRESHOLD_MAX);
	}

134
	if (tr->set_lvt_off) {
135 136 137 138 139
		if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
			/* set new lvt offset */
			hi &= ~MASK_LVTOFF_HI;
			hi |= tr->lvt_off << 20;
		}
140 141
	}

142
	tr->b->interrupt_enable ?
143 144
	    (hi = (hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) :
	    (hi &= ~MASK_INT_TYPE_HI);
145

146 147
	hi |= MASK_COUNT_EN_HI;
	wrmsr(tr->b->address, lo, hi);
148 149
}

150 151 152 153 154 155 156 157 158 159 160 161
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);
};

162 163 164 165 166 167 168 169 170
static int setup_APIC_mce(int reserved, int new)
{
	if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
					      APIC_EILVT_MSG_FIX, 0))
		return new;

	return reserved;
}

171
/* cpu init entry point, called from mce.c with preempt off */
172
void mce_amd_feature_init(struct cpuinfo_x86 *c)
173
{
174
	struct threshold_block b;
175
	unsigned int cpu = smp_processor_id();
176
	u32 low = 0, high = 0, address = 0;
I
Ingo Molnar 已提交
177
	unsigned int bank, block;
178
	int offset = -1;
179 180

	for (bank = 0; bank < NR_BANKS; ++bank) {
181 182 183
		for (block = 0; block < NR_BLOCKS; ++block) {
			if (block == 0)
				address = MSR_IA32_MC0_MISC + bank * 4;
184 185 186 187
			else if (block == 1) {
				address = (low & MASK_BLKPTR_LO) >> 21;
				if (!address)
					break;
188

189
				address += MCG_XBLK_ADDR;
I
Ingo Molnar 已提交
190
			} else
191 192 193
				++address;

			if (rdmsr_safe(address, &low, &high))
194
				break;
195

196 197
			if (!(high & MASK_VALID_HI))
				continue;
198

199 200
			if (!(high & MASK_CNTP_HI)  ||
			     (high & MASK_LOCKED_HI))
201 202 203 204
				continue;

			if (!block)
				per_cpu(bank_map, cpu) |= (1 << bank);
205
#ifdef CONFIG_SMP
206 207
			if (shared_bank[bank] && c->cpu_core_id)
				break;
208
#endif
209 210
			offset = setup_APIC_mce(offset,
						(high & MASK_LVTOFF_HI) >> 20);
211

212 213 214 215 216
			memset(&b, 0, sizeof(b));
			b.cpu		= cpu;
			b.bank		= bank;
			b.block		= block;
			b.address	= address;
217

218
			mce_threshold_block_init(&b, offset);
219
			mce_threshold_vector = amd_threshold_interrupt;
220
		}
221 222 223 224 225 226 227 228 229 230 231 232
	}
}

/*
 * 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.
 */
233
static void amd_threshold_interrupt(void)
234
{
I
Ingo Molnar 已提交
235
	u32 low = 0, high = 0, address = 0;
236
	unsigned int bank, block;
237 238
	struct mce m;

239
	mce_setup(&m);
240 241 242

	/* assume first bank caused it */
	for (bank = 0; bank < NR_BANKS; ++bank) {
243 244
		if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
			continue;
245
		for (block = 0; block < NR_BLOCKS; ++block) {
I
Ingo Molnar 已提交
246
			if (block == 0) {
247
				address = MSR_IA32_MC0_MISC + bank * 4;
I
Ingo Molnar 已提交
248
			} else if (block == 1) {
249 250 251 252
				address = (low & MASK_BLKPTR_LO) >> 21;
				if (!address)
					break;
				address += MCG_XBLK_ADDR;
I
Ingo Molnar 已提交
253
			} else {
254
				++address;
I
Ingo Molnar 已提交
255
			}
256 257

			if (rdmsr_safe(address, &low, &high))
258
				break;
259 260 261 262 263 264 265 266

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

267 268
			if (!(high & MASK_CNTP_HI)  ||
			     (high & MASK_LOCKED_HI))
269 270
				continue;

I
Ingo Molnar 已提交
271 272 273 274
			/*
			 * Log the machine check that caused the threshold
			 * event.
			 */
275 276
			machine_check_poll(MCP_TIMESTAMP,
					&__get_cpu_var(mce_poll_banks));
277

278 279 280 281 282 283 284 285
			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);
286
				return;
287
			}
288 289 290 291 292 293 294 295 296
		}
	}
}

/*
 * Sysfs Interface
 */

struct threshold_attr {
J
Jacob Shin 已提交
297
	struct attribute attr;
I
Ingo Molnar 已提交
298 299
	ssize_t (*show) (struct threshold_block *, char *);
	ssize_t (*store) (struct threshold_block *, const char *, size_t count);
300 301
};

I
Ingo Molnar 已提交
302 303 304 305
#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 已提交
306
}
307 308 309
SHOW_FIELDS(interrupt_enable)
SHOW_FIELDS(threshold_limit)

I
Ingo Molnar 已提交
310
static ssize_t
H
Hidetoshi Seto 已提交
311
store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
312
{
313
	struct thresh_restart tr;
I
Ingo Molnar 已提交
314 315
	unsigned long new;

H
Hidetoshi Seto 已提交
316
	if (strict_strtoul(buf, 0, &new) < 0)
317
		return -EINVAL;
I
Ingo Molnar 已提交
318

319 320
	b->interrupt_enable = !!new;

321
	memset(&tr, 0, sizeof(tr));
I
Ingo Molnar 已提交
322 323
	tr.b		= b;

324
	smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
325

H
Hidetoshi Seto 已提交
326
	return size;
327 328
}

I
Ingo Molnar 已提交
329
static ssize_t
H
Hidetoshi Seto 已提交
330
store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
331
{
332
	struct thresh_restart tr;
I
Ingo Molnar 已提交
333 334
	unsigned long new;

H
Hidetoshi Seto 已提交
335
	if (strict_strtoul(buf, 0, &new) < 0)
336
		return -EINVAL;
I
Ingo Molnar 已提交
337

338 339 340 341
	if (new > THRESHOLD_MAX)
		new = THRESHOLD_MAX;
	if (new < 1)
		new = 1;
I
Ingo Molnar 已提交
342

343
	memset(&tr, 0, sizeof(tr));
344
	tr.old_limit = b->threshold_limit;
345
	b->threshold_limit = new;
346
	tr.b = b;
347

348
	smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
349

H
Hidetoshi Seto 已提交
350
	return size;
351 352
}

353
struct threshold_block_cross_cpu {
I
Ingo Molnar 已提交
354 355
	struct threshold_block	*tb;
	long			retval;
356 357 358
};

static void local_error_count_handler(void *_tbcc)
359
{
360 361
	struct threshold_block_cross_cpu *tbcc = _tbcc;
	struct threshold_block *b = tbcc->tb;
362 363
	u32 low, high;

364
	rdmsr(b->address, low, high);
365
	tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
366 367 368 369
}

static ssize_t show_error_count(struct threshold_block *b, char *buf)
{
370 371 372 373
	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);
374 375
}

376
static ssize_t store_error_count(struct threshold_block *b,
377 378
				 const char *buf, size_t count)
{
379 380
	struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };

381
	smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
382 383 384
	return 1;
}

385 386 387 388 389
#define RW_ATTR(val)							\
static struct threshold_attr val = {					\
	.attr	= {.name = __stringify(val), .mode = 0644 },		\
	.show	= show_## val,						\
	.store	= store_## val,						\
390 391
};

J
Jacob Shin 已提交
392 393 394
RW_ATTR(interrupt_enable);
RW_ATTR(threshold_limit);
RW_ATTR(error_count);
395 396 397 398 399 400 401 402

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

I
Ingo Molnar 已提交
403 404
#define to_block(k)	container_of(k, struct threshold_block, kobj)
#define to_attr(a)	container_of(a, struct threshold_attr, attr)
405 406 407

static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
{
408
	struct threshold_block *b = to_block(kobj);
409 410
	struct threshold_attr *a = to_attr(attr);
	ssize_t ret;
I
Ingo Molnar 已提交
411

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

414 415 416 417 418 419
	return ret;
}

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

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

426 427 428
	return ret;
}

429
static const struct sysfs_ops threshold_ops = {
I
Ingo Molnar 已提交
430 431
	.show			= show,
	.store			= store,
432 433 434
};

static struct kobj_type threshold_ktype = {
I
Ingo Molnar 已提交
435 436
	.sysfs_ops		= &threshold_ops,
	.default_attrs		= default_attrs,
437 438
};

439 440 441 442 443 444
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 已提交
445 446
	u32 low, high;
	int err;
447 448 449 450

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

451
	if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
452
		return 0;
453 454 455 456 457 458 459 460

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

461 462
	if (!(high & MASK_CNTP_HI)  ||
	     (high & MASK_LOCKED_HI))
463 464 465 466 467 468
		goto recurse;

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

I
Ingo Molnar 已提交
469 470 471 472 473 474
	b->block		= block;
	b->bank			= bank;
	b->cpu			= cpu;
	b->address		= address;
	b->interrupt_enable	= 0;
	b->threshold_limit	= THRESHOLD_MAX;
475 476 477

	INIT_LIST_HEAD(&b->miscj);

I
Ingo Molnar 已提交
478
	if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
479 480
		list_add(&b->miscj,
			 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
I
Ingo Molnar 已提交
481
	} else {
482
		per_cpu(threshold_banks, cpu)[bank]->blocks = b;
I
Ingo Molnar 已提交
483
	}
484

485 486 487
	err = kobject_init_and_add(&b->kobj, &threshold_ktype,
				   per_cpu(threshold_banks, cpu)[bank]->kobj,
				   "misc%i", block);
488 489 490 491 492 493 494 495
	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 已提交
496
	} else {
497
		++address;
I
Ingo Molnar 已提交
498
	}
499 500 501 502 503

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

504 505
	if (b)
		kobject_uevent(&b->kobj, KOBJ_ADD);
506

507 508 509 510
	return err;

out_free:
	if (b) {
511
		kobject_put(&b->kobj);
512 513 514 515 516
		kfree(b);
	}
	return err;
}

517 518
static __cpuinit long
local_allocate_threshold_blocks(int cpu, unsigned int bank)
519
{
520 521
	return allocate_threshold_blocks(cpu, bank, 0,
					 MSR_IA32_MC0_MISC + bank * 4);
522 523
}

524
/* symlinks sibling shared banks to first core.  first core owns dir/files. */
525
static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
526
{
527
	int i, err = 0;
528
	struct threshold_bank *b = NULL;
529
	char name[32];
530
#ifdef CONFIG_SMP
531
	struct cpuinfo_x86 *c = &cpu_data(cpu);
532
#endif
533 534

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

#ifdef CONFIG_SMP
537
	if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) {	/* symlink */
538
		i = cpumask_first(c->llc_shared_map);
539 540

		/* first core not up yet */
541
		if (cpu_data(i).cpu_core_id)
542 543 544 545 546 547 548
			goto out;

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

		b = per_cpu(threshold_banks, i)[bank];
549 550 551

		if (!b)
			goto out;
552

I
Ingo Molnar 已提交
553
		err = sysfs_create_link(&per_cpu(mce_dev, cpu).kobj,
554
					b->kobj, name);
555 556
		if (err)
			goto out;
557

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

561 562 563 564
		goto out;
	}
#endif

565
	b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
566 567 568 569
	if (!b) {
		err = -ENOMEM;
		goto out;
	}
570
	if (!zalloc_cpumask_var(&b->cpus, GFP_KERNEL)) {
571 572 573 574
		kfree(b);
		err = -ENOMEM;
		goto out;
	}
575

I
Ingo Molnar 已提交
576
	b->kobj = kobject_create_and_add(name, &per_cpu(mce_dev, cpu).kobj);
577 578 579
	if (!b->kobj)
		goto out_free;

580
#ifndef CONFIG_SMP
581
	cpumask_setall(b->cpus);
582
#else
583
	cpumask_set_cpu(cpu, b->cpus);
584 585
#endif

586
	per_cpu(threshold_banks, cpu)[bank] = b;
587

588
	err = local_allocate_threshold_blocks(cpu, bank);
589 590 591
	if (err)
		goto out_free;

592
	for_each_cpu(i, b->cpus) {
593 594 595
		if (i == cpu)
			continue;

I
Ingo Molnar 已提交
596
		err = sysfs_create_link(&per_cpu(mce_dev, i).kobj,
597
					b->kobj, name);
598 599 600 601 602 603 604 605 606 607
		if (err)
			goto out;

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

	goto out;

out_free:
	per_cpu(threshold_banks, cpu)[bank] = NULL;
608
	free_cpumask_var(b->cpus);
609
	kfree(b);
J
Jacob Shin 已提交
610
out:
611 612 613 614 615 616
	return err;
}

/* create dir/files for all valid threshold banks */
static __cpuinit int threshold_create_device(unsigned int cpu)
{
J
Jacob Shin 已提交
617
	unsigned int bank;
618 619 620
	int err = 0;

	for (bank = 0; bank < NR_BANKS; ++bank) {
621
		if (!(per_cpu(bank_map, cpu) & (1 << bank)))
622 623 624 625 626
			continue;
		err = threshold_create_bank(cpu, bank);
		if (err)
			goto out;
	}
J
Jacob Shin 已提交
627
out:
628 629 630 631 632 633 634 635 636
	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.
 */

637
static void deallocate_threshold_block(unsigned int cpu,
638 639 640 641 642 643 644 645 646 647
						 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) {
648
		kobject_put(&pos->kobj);
649 650 651 652 653 654 655 656
		list_del(&pos->miscj);
		kfree(pos);
	}

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

657
static void threshold_remove_bank(unsigned int cpu, int bank)
658 659
{
	struct threshold_bank *b;
660
	char name[32];
I
Ingo Molnar 已提交
661
	int i = 0;
662 663 664 665

	b = per_cpu(threshold_banks, cpu)[bank];
	if (!b)
		return;
666 667 668 669 670
	if (!b->blocks)
		goto free_out;

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

671
#ifdef CONFIG_SMP
672 673
	/* sibling symlink */
	if (shared_bank[bank] && b->blocks->cpu != cpu) {
I
Ingo Molnar 已提交
674
		sysfs_remove_link(&per_cpu(mce_dev, cpu).kobj, name);
675
		per_cpu(threshold_banks, cpu)[bank] = NULL;
I
Ingo Molnar 已提交
676

677
		return;
678
	}
679
#endif
680 681

	/* remove all sibling symlinks before unregistering */
682
	for_each_cpu(i, b->cpus) {
683 684 685
		if (i == cpu)
			continue;

I
Ingo Molnar 已提交
686
		sysfs_remove_link(&per_cpu(mce_dev, i).kobj, name);
687 688 689 690 691 692
		per_cpu(threshold_banks, i)[bank] = NULL;
	}

	deallocate_threshold_block(cpu, bank);

free_out:
693
	kobject_del(b->kobj);
694
	kobject_put(b->kobj);
695
	free_cpumask_var(b->cpus);
696 697
	kfree(b);
	per_cpu(threshold_banks, cpu)[bank] = NULL;
698 699
}

700
static void threshold_remove_device(unsigned int cpu)
701
{
J
Jacob Shin 已提交
702
	unsigned int bank;
703 704

	for (bank = 0; bank < NR_BANKS; ++bank) {
705
		if (!(per_cpu(bank_map, cpu) & (1 << bank)))
706 707 708 709 710 711
			continue;
		threshold_remove_bank(cpu, bank);
	}
}

/* get notified when a cpu comes on/off */
I
Ingo Molnar 已提交
712 713
static void __cpuinit
amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
714 715 716
{
	switch (action) {
	case CPU_ONLINE:
717
	case CPU_ONLINE_FROZEN:
718 719 720
		threshold_create_device(cpu);
		break;
	case CPU_DEAD:
721
	case CPU_DEAD_FROZEN:
722 723 724 725 726 727 728 729 730
		threshold_remove_device(cpu);
		break;
	default:
		break;
	}
}

static __init int threshold_init_device(void)
{
J
Jacob Shin 已提交
731
	unsigned lcpu = 0;
732 733 734

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

737
		if (err)
738
			return err;
739
	}
740
	threshold_cpu_callback = amd_64_threshold_cpu_callback;
I
Ingo Molnar 已提交
741

742
	return 0;
743 744
}
device_initcall(threshold_init_device);