]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/x86/kernel/cpu/mcheck/mce_amd.c
x86: Simplify code by removing a !SMP #ifdefs from 'struct cpuinfo_x86'
[mirror_ubuntu-bionic-kernel.git] / arch / x86 / kernel / cpu / mcheck / mce_amd.c
1 /*
2 * (c) 2005, 2006 Advanced Micro Devices, Inc.
3 * 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.
8 *
9 * Support : jacob.shin@amd.com
10 *
11 * April 2006
12 * - added support for AMD Family 0x10 processors
13 *
14 * All MC4_MISCi registers are shared between multi-cores
15 */
16 #include <linux/interrupt.h>
17 #include <linux/notifier.h>
18 #include <linux/kobject.h>
19 #include <linux/percpu.h>
20 #include <linux/sysdev.h>
21 #include <linux/errno.h>
22 #include <linux/sched.h>
23 #include <linux/sysfs.h>
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/cpu.h>
27 #include <linux/smp.h>
28
29 #include <asm/apic.h>
30 #include <asm/idle.h>
31 #include <asm/mce.h>
32 #include <asm/msr.h>
33
34 #define NR_BANKS 6
35 #define NR_BLOCKS 9
36 #define THRESHOLD_MAX 0xFFF
37 #define INT_TYPE_APIC 0x00020000
38 #define MASK_VALID_HI 0x80000000
39 #define MASK_CNTP_HI 0x40000000
40 #define MASK_LOCKED_HI 0x20000000
41 #define MASK_LVTOFF_HI 0x00F00000
42 #define MASK_COUNT_EN_HI 0x00080000
43 #define MASK_INT_TYPE_HI 0x00060000
44 #define MASK_OVERFLOW_HI 0x00010000
45 #define MASK_ERR_COUNT_HI 0x00000FFF
46 #define MASK_BLKPTR_LO 0xFF000000
47 #define MCG_XBLK_ADDR 0xC0000400
48
49 struct threshold_block {
50 unsigned int block;
51 unsigned int bank;
52 unsigned int cpu;
53 u32 address;
54 u16 interrupt_enable;
55 u16 threshold_limit;
56 struct kobject kobj;
57 struct list_head miscj;
58 };
59
60 struct threshold_bank {
61 struct kobject *kobj;
62 struct threshold_block *blocks;
63 cpumask_var_t cpus;
64 };
65 static DEFINE_PER_CPU(struct threshold_bank * [NR_BANKS], threshold_banks);
66
67 static unsigned char shared_bank[NR_BANKS] = {
68 0, 0, 0, 0, 1
69 };
70
71 static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */
72
73 static void amd_threshold_interrupt(void);
74
75 /*
76 * CPU Initialization
77 */
78
79 struct thresh_restart {
80 struct threshold_block *b;
81 int reset;
82 int set_lvt_off;
83 int lvt_off;
84 u16 old_limit;
85 };
86
87 static int lvt_off_valid(struct threshold_block *b, int apic, u32 lo, u32 hi)
88 {
89 int msr = (hi & MASK_LVTOFF_HI) >> 20;
90
91 if (apic < 0) {
92 pr_err(FW_BUG "cpu %d, failed to setup threshold interrupt "
93 "for bank %d, block %d (MSR%08X=0x%x%08x)\n", b->cpu,
94 b->bank, b->block, b->address, hi, lo);
95 return 0;
96 }
97
98 if (apic != msr) {
99 pr_err(FW_BUG "cpu %d, invalid threshold interrupt offset %d "
100 "for bank %d, block %d (MSR%08X=0x%x%08x)\n",
101 b->cpu, apic, b->bank, b->block, b->address, hi, lo);
102 return 0;
103 }
104
105 return 1;
106 };
107
108 /* must be called with correct cpu affinity */
109 /* Called via smp_call_function_single() */
110 static void threshold_restart_bank(void *_tr)
111 {
112 struct thresh_restart *tr = _tr;
113 u32 hi, lo;
114
115 rdmsr(tr->b->address, lo, hi);
116
117 if (tr->b->threshold_limit < (hi & THRESHOLD_MAX))
118 tr->reset = 1; /* limit cannot be lower than err count */
119
120 if (tr->reset) { /* reset err count and overflow bit */
121 hi =
122 (hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
123 (THRESHOLD_MAX - tr->b->threshold_limit);
124 } else if (tr->old_limit) { /* change limit w/o reset */
125 int new_count = (hi & THRESHOLD_MAX) +
126 (tr->old_limit - tr->b->threshold_limit);
127
128 hi = (hi & ~MASK_ERR_COUNT_HI) |
129 (new_count & THRESHOLD_MAX);
130 }
131
132 if (tr->set_lvt_off) {
133 if (lvt_off_valid(tr->b, tr->lvt_off, lo, hi)) {
134 /* set new lvt offset */
135 hi &= ~MASK_LVTOFF_HI;
136 hi |= tr->lvt_off << 20;
137 }
138 }
139
140 tr->b->interrupt_enable ?
141 (hi = (hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) :
142 (hi &= ~MASK_INT_TYPE_HI);
143
144 hi |= MASK_COUNT_EN_HI;
145 wrmsr(tr->b->address, lo, hi);
146 }
147
148 static void mce_threshold_block_init(struct threshold_block *b, int offset)
149 {
150 struct thresh_restart tr = {
151 .b = b,
152 .set_lvt_off = 1,
153 .lvt_off = offset,
154 };
155
156 b->threshold_limit = THRESHOLD_MAX;
157 threshold_restart_bank(&tr);
158 };
159
160 static int setup_APIC_mce(int reserved, int new)
161 {
162 if (reserved < 0 && !setup_APIC_eilvt(new, THRESHOLD_APIC_VECTOR,
163 APIC_EILVT_MSG_FIX, 0))
164 return new;
165
166 return reserved;
167 }
168
169 /* cpu init entry point, called from mce.c with preempt off */
170 void mce_amd_feature_init(struct cpuinfo_x86 *c)
171 {
172 struct threshold_block b;
173 unsigned int cpu = smp_processor_id();
174 u32 low = 0, high = 0, address = 0;
175 unsigned int bank, block;
176 int offset = -1;
177
178 for (bank = 0; bank < NR_BANKS; ++bank) {
179 for (block = 0; block < NR_BLOCKS; ++block) {
180 if (block == 0)
181 address = MSR_IA32_MC0_MISC + bank * 4;
182 else if (block == 1) {
183 address = (low & MASK_BLKPTR_LO) >> 21;
184 if (!address)
185 break;
186
187 address += MCG_XBLK_ADDR;
188 } else
189 ++address;
190
191 if (rdmsr_safe(address, &low, &high))
192 break;
193
194 if (!(high & MASK_VALID_HI))
195 continue;
196
197 if (!(high & MASK_CNTP_HI) ||
198 (high & MASK_LOCKED_HI))
199 continue;
200
201 if (!block)
202 per_cpu(bank_map, cpu) |= (1 << bank);
203 if (shared_bank[bank] && c->cpu_core_id)
204 break;
205
206 offset = setup_APIC_mce(offset,
207 (high & MASK_LVTOFF_HI) >> 20);
208
209 memset(&b, 0, sizeof(b));
210 b.cpu = cpu;
211 b.bank = bank;
212 b.block = block;
213 b.address = address;
214
215 mce_threshold_block_init(&b, offset);
216 mce_threshold_vector = amd_threshold_interrupt;
217 }
218 }
219 }
220
221 /*
222 * APIC Interrupt Handler
223 */
224
225 /*
226 * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
227 * the interrupt goes off when error_count reaches threshold_limit.
228 * the handler will simply log mcelog w/ software defined bank number.
229 */
230 static void amd_threshold_interrupt(void)
231 {
232 u32 low = 0, high = 0, address = 0;
233 unsigned int bank, block;
234 struct mce m;
235
236 mce_setup(&m);
237
238 /* assume first bank caused it */
239 for (bank = 0; bank < NR_BANKS; ++bank) {
240 if (!(per_cpu(bank_map, m.cpu) & (1 << bank)))
241 continue;
242 for (block = 0; block < NR_BLOCKS; ++block) {
243 if (block == 0) {
244 address = MSR_IA32_MC0_MISC + bank * 4;
245 } else if (block == 1) {
246 address = (low & MASK_BLKPTR_LO) >> 21;
247 if (!address)
248 break;
249 address += MCG_XBLK_ADDR;
250 } else {
251 ++address;
252 }
253
254 if (rdmsr_safe(address, &low, &high))
255 break;
256
257 if (!(high & MASK_VALID_HI)) {
258 if (block)
259 continue;
260 else
261 break;
262 }
263
264 if (!(high & MASK_CNTP_HI) ||
265 (high & MASK_LOCKED_HI))
266 continue;
267
268 /*
269 * Log the machine check that caused the threshold
270 * event.
271 */
272 machine_check_poll(MCP_TIMESTAMP,
273 &__get_cpu_var(mce_poll_banks));
274
275 if (high & MASK_OVERFLOW_HI) {
276 rdmsrl(address, m.misc);
277 rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
278 m.status);
279 m.bank = K8_MCE_THRESHOLD_BASE
280 + bank * NR_BLOCKS
281 + block;
282 mce_log(&m);
283 return;
284 }
285 }
286 }
287 }
288
289 /*
290 * Sysfs Interface
291 */
292
293 struct threshold_attr {
294 struct attribute attr;
295 ssize_t (*show) (struct threshold_block *, char *);
296 ssize_t (*store) (struct threshold_block *, const char *, size_t count);
297 };
298
299 #define SHOW_FIELDS(name) \
300 static ssize_t show_ ## name(struct threshold_block *b, char *buf) \
301 { \
302 return sprintf(buf, "%lx\n", (unsigned long) b->name); \
303 }
304 SHOW_FIELDS(interrupt_enable)
305 SHOW_FIELDS(threshold_limit)
306
307 static ssize_t
308 store_interrupt_enable(struct threshold_block *b, const char *buf, size_t size)
309 {
310 struct thresh_restart tr;
311 unsigned long new;
312
313 if (strict_strtoul(buf, 0, &new) < 0)
314 return -EINVAL;
315
316 b->interrupt_enable = !!new;
317
318 memset(&tr, 0, sizeof(tr));
319 tr.b = b;
320
321 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
322
323 return size;
324 }
325
326 static ssize_t
327 store_threshold_limit(struct threshold_block *b, const char *buf, size_t size)
328 {
329 struct thresh_restart tr;
330 unsigned long new;
331
332 if (strict_strtoul(buf, 0, &new) < 0)
333 return -EINVAL;
334
335 if (new > THRESHOLD_MAX)
336 new = THRESHOLD_MAX;
337 if (new < 1)
338 new = 1;
339
340 memset(&tr, 0, sizeof(tr));
341 tr.old_limit = b->threshold_limit;
342 b->threshold_limit = new;
343 tr.b = b;
344
345 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
346
347 return size;
348 }
349
350 struct threshold_block_cross_cpu {
351 struct threshold_block *tb;
352 long retval;
353 };
354
355 static void local_error_count_handler(void *_tbcc)
356 {
357 struct threshold_block_cross_cpu *tbcc = _tbcc;
358 struct threshold_block *b = tbcc->tb;
359 u32 low, high;
360
361 rdmsr(b->address, low, high);
362 tbcc->retval = (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit);
363 }
364
365 static ssize_t show_error_count(struct threshold_block *b, char *buf)
366 {
367 struct threshold_block_cross_cpu tbcc = { .tb = b, };
368
369 smp_call_function_single(b->cpu, local_error_count_handler, &tbcc, 1);
370 return sprintf(buf, "%lx\n", tbcc.retval);
371 }
372
373 static ssize_t store_error_count(struct threshold_block *b,
374 const char *buf, size_t count)
375 {
376 struct thresh_restart tr = { .b = b, .reset = 1, .old_limit = 0 };
377
378 smp_call_function_single(b->cpu, threshold_restart_bank, &tr, 1);
379 return 1;
380 }
381
382 #define RW_ATTR(val) \
383 static struct threshold_attr val = { \
384 .attr = {.name = __stringify(val), .mode = 0644 }, \
385 .show = show_## val, \
386 .store = store_## val, \
387 };
388
389 RW_ATTR(interrupt_enable);
390 RW_ATTR(threshold_limit);
391 RW_ATTR(error_count);
392
393 static struct attribute *default_attrs[] = {
394 &interrupt_enable.attr,
395 &threshold_limit.attr,
396 &error_count.attr,
397 NULL
398 };
399
400 #define to_block(k) container_of(k, struct threshold_block, kobj)
401 #define to_attr(a) container_of(a, struct threshold_attr, attr)
402
403 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
404 {
405 struct threshold_block *b = to_block(kobj);
406 struct threshold_attr *a = to_attr(attr);
407 ssize_t ret;
408
409 ret = a->show ? a->show(b, buf) : -EIO;
410
411 return ret;
412 }
413
414 static ssize_t store(struct kobject *kobj, struct attribute *attr,
415 const char *buf, size_t count)
416 {
417 struct threshold_block *b = to_block(kobj);
418 struct threshold_attr *a = to_attr(attr);
419 ssize_t ret;
420
421 ret = a->store ? a->store(b, buf, count) : -EIO;
422
423 return ret;
424 }
425
426 static const struct sysfs_ops threshold_ops = {
427 .show = show,
428 .store = store,
429 };
430
431 static struct kobj_type threshold_ktype = {
432 .sysfs_ops = &threshold_ops,
433 .default_attrs = default_attrs,
434 };
435
436 static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
437 unsigned int bank,
438 unsigned int block,
439 u32 address)
440 {
441 struct threshold_block *b = NULL;
442 u32 low, high;
443 int err;
444
445 if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
446 return 0;
447
448 if (rdmsr_safe_on_cpu(cpu, address, &low, &high))
449 return 0;
450
451 if (!(high & MASK_VALID_HI)) {
452 if (block)
453 goto recurse;
454 else
455 return 0;
456 }
457
458 if (!(high & MASK_CNTP_HI) ||
459 (high & MASK_LOCKED_HI))
460 goto recurse;
461
462 b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
463 if (!b)
464 return -ENOMEM;
465
466 b->block = block;
467 b->bank = bank;
468 b->cpu = cpu;
469 b->address = address;
470 b->interrupt_enable = 0;
471 b->threshold_limit = THRESHOLD_MAX;
472
473 INIT_LIST_HEAD(&b->miscj);
474
475 if (per_cpu(threshold_banks, cpu)[bank]->blocks) {
476 list_add(&b->miscj,
477 &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
478 } else {
479 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
480 }
481
482 err = kobject_init_and_add(&b->kobj, &threshold_ktype,
483 per_cpu(threshold_banks, cpu)[bank]->kobj,
484 "misc%i", block);
485 if (err)
486 goto out_free;
487 recurse:
488 if (!block) {
489 address = (low & MASK_BLKPTR_LO) >> 21;
490 if (!address)
491 return 0;
492 address += MCG_XBLK_ADDR;
493 } else {
494 ++address;
495 }
496
497 err = allocate_threshold_blocks(cpu, bank, ++block, address);
498 if (err)
499 goto out_free;
500
501 if (b)
502 kobject_uevent(&b->kobj, KOBJ_ADD);
503
504 return err;
505
506 out_free:
507 if (b) {
508 kobject_put(&b->kobj);
509 list_del(&b->miscj);
510 kfree(b);
511 }
512 return err;
513 }
514
515 static __cpuinit long
516 local_allocate_threshold_blocks(int cpu, unsigned int bank)
517 {
518 return allocate_threshold_blocks(cpu, bank, 0,
519 MSR_IA32_MC0_MISC + bank * 4);
520 }
521
522 /* symlinks sibling shared banks to first core. first core owns dir/files. */
523 static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
524 {
525 int i, err = 0;
526 struct threshold_bank *b = NULL;
527 char name[32];
528
529 sprintf(name, "threshold_bank%i", bank);
530
531 if (cpu_data(cpu).cpu_core_id && shared_bank[bank]) { /* symlink */
532 i = cpumask_first(cpu_llc_shared_mask(cpu));
533
534 /* first core not up yet */
535 if (cpu_data(i).cpu_core_id)
536 goto out;
537
538 /* already linked */
539 if (per_cpu(threshold_banks, cpu)[bank])
540 goto out;
541
542 b = per_cpu(threshold_banks, i)[bank];
543
544 if (!b)
545 goto out;
546
547 err = sysfs_create_link(&per_cpu(mce_sysdev, cpu).kobj,
548 b->kobj, name);
549 if (err)
550 goto out;
551
552 cpumask_copy(b->cpus, cpu_llc_shared_mask(cpu));
553 per_cpu(threshold_banks, cpu)[bank] = b;
554
555 goto out;
556 }
557
558 b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
559 if (!b) {
560 err = -ENOMEM;
561 goto out;
562 }
563 if (!zalloc_cpumask_var(&b->cpus, GFP_KERNEL)) {
564 kfree(b);
565 err = -ENOMEM;
566 goto out;
567 }
568
569 b->kobj = kobject_create_and_add(name, &per_cpu(mce_sysdev, cpu).kobj);
570 if (!b->kobj)
571 goto out_free;
572
573 #ifndef CONFIG_SMP
574 cpumask_setall(b->cpus);
575 #else
576 cpumask_set_cpu(cpu, b->cpus);
577 #endif
578
579 per_cpu(threshold_banks, cpu)[bank] = b;
580
581 err = local_allocate_threshold_blocks(cpu, bank);
582 if (err)
583 goto out_free;
584
585 for_each_cpu(i, b->cpus) {
586 if (i == cpu)
587 continue;
588
589 err = sysfs_create_link(&per_cpu(mce_sysdev, i).kobj,
590 b->kobj, name);
591 if (err)
592 goto out;
593
594 per_cpu(threshold_banks, i)[bank] = b;
595 }
596
597 goto out;
598
599 out_free:
600 per_cpu(threshold_banks, cpu)[bank] = NULL;
601 free_cpumask_var(b->cpus);
602 kfree(b);
603 out:
604 return err;
605 }
606
607 /* create dir/files for all valid threshold banks */
608 static __cpuinit int threshold_create_device(unsigned int cpu)
609 {
610 unsigned int bank;
611 int err = 0;
612
613 for (bank = 0; bank < NR_BANKS; ++bank) {
614 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
615 continue;
616 err = threshold_create_bank(cpu, bank);
617 if (err)
618 return err;
619 }
620
621 return err;
622 }
623
624 /*
625 * let's be hotplug friendly.
626 * in case of multiple core processors, the first core always takes ownership
627 * of shared sysfs dir/files, and rest of the cores will be symlinked to it.
628 */
629
630 static void deallocate_threshold_block(unsigned int cpu,
631 unsigned int bank)
632 {
633 struct threshold_block *pos = NULL;
634 struct threshold_block *tmp = NULL;
635 struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
636
637 if (!head)
638 return;
639
640 list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
641 kobject_put(&pos->kobj);
642 list_del(&pos->miscj);
643 kfree(pos);
644 }
645
646 kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
647 per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
648 }
649
650 static void threshold_remove_bank(unsigned int cpu, int bank)
651 {
652 struct threshold_bank *b;
653 char name[32];
654 int i = 0;
655
656 b = per_cpu(threshold_banks, cpu)[bank];
657 if (!b)
658 return;
659 if (!b->blocks)
660 goto free_out;
661
662 sprintf(name, "threshold_bank%i", bank);
663
664 #ifdef CONFIG_SMP
665 /* sibling symlink */
666 if (shared_bank[bank] && b->blocks->cpu != cpu) {
667 sysfs_remove_link(&per_cpu(mce_sysdev, cpu).kobj, name);
668 per_cpu(threshold_banks, cpu)[bank] = NULL;
669
670 return;
671 }
672 #endif
673
674 /* remove all sibling symlinks before unregistering */
675 for_each_cpu(i, b->cpus) {
676 if (i == cpu)
677 continue;
678
679 sysfs_remove_link(&per_cpu(mce_sysdev, i).kobj, name);
680 per_cpu(threshold_banks, i)[bank] = NULL;
681 }
682
683 deallocate_threshold_block(cpu, bank);
684
685 free_out:
686 kobject_del(b->kobj);
687 kobject_put(b->kobj);
688 free_cpumask_var(b->cpus);
689 kfree(b);
690 per_cpu(threshold_banks, cpu)[bank] = NULL;
691 }
692
693 static void threshold_remove_device(unsigned int cpu)
694 {
695 unsigned int bank;
696
697 for (bank = 0; bank < NR_BANKS; ++bank) {
698 if (!(per_cpu(bank_map, cpu) & (1 << bank)))
699 continue;
700 threshold_remove_bank(cpu, bank);
701 }
702 }
703
704 /* get notified when a cpu comes on/off */
705 static void __cpuinit
706 amd_64_threshold_cpu_callback(unsigned long action, unsigned int cpu)
707 {
708 switch (action) {
709 case CPU_ONLINE:
710 case CPU_ONLINE_FROZEN:
711 threshold_create_device(cpu);
712 break;
713 case CPU_DEAD:
714 case CPU_DEAD_FROZEN:
715 threshold_remove_device(cpu);
716 break;
717 default:
718 break;
719 }
720 }
721
722 static __init int threshold_init_device(void)
723 {
724 unsigned lcpu = 0;
725
726 /* to hit CPUs online before the notifier is up */
727 for_each_online_cpu(lcpu) {
728 int err = threshold_create_device(lcpu);
729
730 if (err)
731 return err;
732 }
733 threshold_cpu_callback = amd_64_threshold_cpu_callback;
734
735 return 0;
736 }
737 device_initcall(threshold_init_device);