Merge branch 'master' into for_paulus
[linux-drm-fsl-dcu.git] / arch / x86_64 / kernel / 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
17 #include <linux/cpu.h>
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/kobject.h>
22 #include <linux/notifier.h>
23 #include <linux/sched.h>
24 #include <linux/smp.h>
25 #include <linux/sysdev.h>
26 #include <linux/sysfs.h>
27 #include <asm/apic.h>
28 #include <asm/mce.h>
29 #include <asm/msr.h>
30 #include <asm/percpu.h>
31 #include <asm/idle.h>
32
33 #define PFX               "mce_threshold: "
34 #define VERSION           "version 1.1.1"
35 #define NR_BANKS          6
36 #define NR_BLOCKS         9
37 #define THRESHOLD_MAX     0xFFF
38 #define INT_TYPE_APIC     0x00020000
39 #define MASK_VALID_HI     0x80000000
40 #define MASK_LVTOFF_HI    0x00F00000
41 #define MASK_COUNT_EN_HI  0x00080000
42 #define MASK_INT_TYPE_HI  0x00060000
43 #define MASK_OVERFLOW_HI  0x00010000
44 #define MASK_ERR_COUNT_HI 0x00000FFF
45 #define MASK_BLKPTR_LO    0xFF000000
46 #define MCG_XBLK_ADDR     0xC0000400
47
48 struct threshold_block {
49         unsigned int block;
50         unsigned int bank;
51         unsigned int cpu;
52         u32 address;
53         u16 interrupt_enable;
54         u16 threshold_limit;
55         struct kobject kobj;
56         struct list_head miscj;
57 };
58
59 /* defaults used early on boot */
60 static struct threshold_block threshold_defaults = {
61         .interrupt_enable = 0,
62         .threshold_limit = THRESHOLD_MAX,
63 };
64
65 struct threshold_bank {
66         struct kobject kobj;
67         struct threshold_block *blocks;
68         cpumask_t cpus;
69 };
70 static DEFINE_PER_CPU(struct threshold_bank *, threshold_banks[NR_BANKS]);
71
72 #ifdef CONFIG_SMP
73 static unsigned char shared_bank[NR_BANKS] = {
74         0, 0, 0, 0, 1
75 };
76 #endif
77
78 static DEFINE_PER_CPU(unsigned char, bank_map); /* see which banks are on */
79
80 /*
81  * CPU Initialization
82  */
83
84 /* must be called with correct cpu affinity */
85 static void threshold_restart_bank(struct threshold_block *b,
86                                    int reset, u16 old_limit)
87 {
88         u32 mci_misc_hi, mci_misc_lo;
89
90         rdmsr(b->address, mci_misc_lo, mci_misc_hi);
91
92         if (b->threshold_limit < (mci_misc_hi & THRESHOLD_MAX))
93                 reset = 1;      /* limit cannot be lower than err count */
94
95         if (reset) {            /* reset err count and overflow bit */
96                 mci_misc_hi =
97                     (mci_misc_hi & ~(MASK_ERR_COUNT_HI | MASK_OVERFLOW_HI)) |
98                     (THRESHOLD_MAX - b->threshold_limit);
99         } else if (old_limit) { /* change limit w/o reset */
100                 int new_count = (mci_misc_hi & THRESHOLD_MAX) +
101                     (old_limit - b->threshold_limit);
102                 mci_misc_hi = (mci_misc_hi & ~MASK_ERR_COUNT_HI) |
103                     (new_count & THRESHOLD_MAX);
104         }
105
106         b->interrupt_enable ?
107             (mci_misc_hi = (mci_misc_hi & ~MASK_INT_TYPE_HI) | INT_TYPE_APIC) :
108             (mci_misc_hi &= ~MASK_INT_TYPE_HI);
109
110         mci_misc_hi |= MASK_COUNT_EN_HI;
111         wrmsr(b->address, mci_misc_lo, mci_misc_hi);
112 }
113
114 /* cpu init entry point, called from mce.c with preempt off */
115 void __cpuinit mce_amd_feature_init(struct cpuinfo_x86 *c)
116 {
117         unsigned int bank, block;
118         unsigned int cpu = smp_processor_id();
119         u32 low = 0, high = 0, address = 0;
120
121         for (bank = 0; bank < NR_BANKS; ++bank) {
122                 for (block = 0; block < NR_BLOCKS; ++block) {
123                         if (block == 0)
124                                 address = MSR_IA32_MC0_MISC + bank * 4;
125                         else if (block == 1)
126                                 address = MCG_XBLK_ADDR
127                                         + ((low & MASK_BLKPTR_LO) >> 21);
128                         else
129                                 ++address;
130
131                         if (rdmsr_safe(address, &low, &high))
132                                 continue;
133
134                         if (!(high & MASK_VALID_HI)) {
135                                 if (block)
136                                         continue;
137                                 else
138                                         break;
139                         }
140
141                         if (!(high & MASK_VALID_HI >> 1)  ||
142                              (high & MASK_VALID_HI >> 2))
143                                 continue;
144
145                         if (!block)
146                                 per_cpu(bank_map, cpu) |= (1 << bank);
147 #ifdef CONFIG_SMP
148                         if (shared_bank[bank] && c->cpu_core_id)
149                                 break;
150 #endif
151                         high &= ~MASK_LVTOFF_HI;
152                         high |= K8_APIC_EXT_LVT_ENTRY_THRESHOLD << 20;
153                         wrmsr(address, low, high);
154
155                         setup_APIC_extened_lvt(K8_APIC_EXT_LVT_ENTRY_THRESHOLD,
156                                                THRESHOLD_APIC_VECTOR,
157                                                K8_APIC_EXT_INT_MSG_FIX, 0);
158
159                         threshold_defaults.address = address;
160                         threshold_restart_bank(&threshold_defaults, 0, 0);
161                 }
162         }
163 }
164
165 /*
166  * APIC Interrupt Handler
167  */
168
169 /*
170  * threshold interrupt handler will service THRESHOLD_APIC_VECTOR.
171  * the interrupt goes off when error_count reaches threshold_limit.
172  * the handler will simply log mcelog w/ software defined bank number.
173  */
174 asmlinkage void mce_threshold_interrupt(void)
175 {
176         unsigned int bank, block;
177         struct mce m;
178         u32 low = 0, high = 0, address = 0;
179
180         ack_APIC_irq();
181         exit_idle();
182         irq_enter();
183
184         memset(&m, 0, sizeof(m));
185         rdtscll(m.tsc);
186         m.cpu = smp_processor_id();
187
188         /* assume first bank caused it */
189         for (bank = 0; bank < NR_BANKS; ++bank) {
190                 for (block = 0; block < NR_BLOCKS; ++block) {
191                         if (block == 0)
192                                 address = MSR_IA32_MC0_MISC + bank * 4;
193                         else if (block == 1)
194                                 address = MCG_XBLK_ADDR
195                                         + ((low & MASK_BLKPTR_LO) >> 21);
196                         else
197                                 ++address;
198
199                         if (rdmsr_safe(address, &low, &high))
200                                 continue;
201
202                         if (!(high & MASK_VALID_HI)) {
203                                 if (block)
204                                         continue;
205                                 else
206                                         break;
207                         }
208
209                         if (!(high & MASK_VALID_HI >> 1)  ||
210                              (high & MASK_VALID_HI >> 2))
211                                 continue;
212
213                         if (high & MASK_OVERFLOW_HI) {
214                                 rdmsrl(address, m.misc);
215                                 rdmsrl(MSR_IA32_MC0_STATUS + bank * 4,
216                                        m.status);
217                                 m.bank = K8_MCE_THRESHOLD_BASE
218                                        + bank * NR_BLOCKS
219                                        + block;
220                                 mce_log(&m);
221                                 goto out;
222                         }
223                 }
224         }
225 out:
226         irq_exit();
227 }
228
229 /*
230  * Sysfs Interface
231  */
232
233 struct threshold_attr {
234         struct attribute attr;
235         ssize_t(*show) (struct threshold_block *, char *);
236         ssize_t(*store) (struct threshold_block *, const char *, size_t count);
237 };
238
239 static cpumask_t affinity_set(unsigned int cpu)
240 {
241         cpumask_t oldmask = current->cpus_allowed;
242         cpumask_t newmask = CPU_MASK_NONE;
243         cpu_set(cpu, newmask);
244         set_cpus_allowed(current, newmask);
245         return oldmask;
246 }
247
248 static void affinity_restore(cpumask_t oldmask)
249 {
250         set_cpus_allowed(current, oldmask);
251 }
252
253 #define SHOW_FIELDS(name)                                           \
254 static ssize_t show_ ## name(struct threshold_block * b, char *buf) \
255 {                                                                   \
256         return sprintf(buf, "%lx\n", (unsigned long) b->name);      \
257 }
258 SHOW_FIELDS(interrupt_enable)
259 SHOW_FIELDS(threshold_limit)
260
261 static ssize_t store_interrupt_enable(struct threshold_block *b,
262                                       const char *buf, size_t count)
263 {
264         char *end;
265         cpumask_t oldmask;
266         unsigned long new = simple_strtoul(buf, &end, 0);
267         if (end == buf)
268                 return -EINVAL;
269         b->interrupt_enable = !!new;
270
271         oldmask = affinity_set(b->cpu);
272         threshold_restart_bank(b, 0, 0);
273         affinity_restore(oldmask);
274
275         return end - buf;
276 }
277
278 static ssize_t store_threshold_limit(struct threshold_block *b,
279                                      const char *buf, size_t count)
280 {
281         char *end;
282         cpumask_t oldmask;
283         u16 old;
284         unsigned long new = simple_strtoul(buf, &end, 0);
285         if (end == buf)
286                 return -EINVAL;
287         if (new > THRESHOLD_MAX)
288                 new = THRESHOLD_MAX;
289         if (new < 1)
290                 new = 1;
291         old = b->threshold_limit;
292         b->threshold_limit = new;
293
294         oldmask = affinity_set(b->cpu);
295         threshold_restart_bank(b, 0, old);
296         affinity_restore(oldmask);
297
298         return end - buf;
299 }
300
301 static ssize_t show_error_count(struct threshold_block *b, char *buf)
302 {
303         u32 high, low;
304         cpumask_t oldmask;
305         oldmask = affinity_set(b->cpu);
306         rdmsr(b->address, low, high);
307         affinity_restore(oldmask);
308         return sprintf(buf, "%x\n",
309                        (high & 0xFFF) - (THRESHOLD_MAX - b->threshold_limit));
310 }
311
312 static ssize_t store_error_count(struct threshold_block *b,
313                                  const char *buf, size_t count)
314 {
315         cpumask_t oldmask;
316         oldmask = affinity_set(b->cpu);
317         threshold_restart_bank(b, 1, 0);
318         affinity_restore(oldmask);
319         return 1;
320 }
321
322 #define THRESHOLD_ATTR(_name,_mode,_show,_store) {            \
323         .attr = {.name = __stringify(_name), .mode = _mode }, \
324         .show = _show,                                        \
325         .store = _store,                                      \
326 };
327
328 #define RW_ATTR(name)                                           \
329 static struct threshold_attr name =                             \
330         THRESHOLD_ATTR(name, 0644, show_## name, store_## name)
331
332 RW_ATTR(interrupt_enable);
333 RW_ATTR(threshold_limit);
334 RW_ATTR(error_count);
335
336 static struct attribute *default_attrs[] = {
337         &interrupt_enable.attr,
338         &threshold_limit.attr,
339         &error_count.attr,
340         NULL
341 };
342
343 #define to_block(k) container_of(k, struct threshold_block, kobj)
344 #define to_attr(a) container_of(a, struct threshold_attr, attr)
345
346 static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
347 {
348         struct threshold_block *b = to_block(kobj);
349         struct threshold_attr *a = to_attr(attr);
350         ssize_t ret;
351         ret = a->show ? a->show(b, buf) : -EIO;
352         return ret;
353 }
354
355 static ssize_t store(struct kobject *kobj, struct attribute *attr,
356                      const char *buf, size_t count)
357 {
358         struct threshold_block *b = to_block(kobj);
359         struct threshold_attr *a = to_attr(attr);
360         ssize_t ret;
361         ret = a->store ? a->store(b, buf, count) : -EIO;
362         return ret;
363 }
364
365 static struct sysfs_ops threshold_ops = {
366         .show = show,
367         .store = store,
368 };
369
370 static struct kobj_type threshold_ktype = {
371         .sysfs_ops = &threshold_ops,
372         .default_attrs = default_attrs,
373 };
374
375 static __cpuinit int allocate_threshold_blocks(unsigned int cpu,
376                                                unsigned int bank,
377                                                unsigned int block,
378                                                u32 address)
379 {
380         int err;
381         u32 low, high;
382         struct threshold_block *b = NULL;
383
384         if ((bank >= NR_BANKS) || (block >= NR_BLOCKS))
385                 return 0;
386
387         if (rdmsr_safe(address, &low, &high))
388                 goto recurse;
389
390         if (!(high & MASK_VALID_HI)) {
391                 if (block)
392                         goto recurse;
393                 else
394                         return 0;
395         }
396
397         if (!(high & MASK_VALID_HI >> 1)  ||
398              (high & MASK_VALID_HI >> 2))
399                 goto recurse;
400
401         b = kzalloc(sizeof(struct threshold_block), GFP_KERNEL);
402         if (!b)
403                 return -ENOMEM;
404
405         b->block = block;
406         b->bank = bank;
407         b->cpu = cpu;
408         b->address = address;
409         b->interrupt_enable = 0;
410         b->threshold_limit = THRESHOLD_MAX;
411
412         INIT_LIST_HEAD(&b->miscj);
413
414         if (per_cpu(threshold_banks, cpu)[bank]->blocks)
415                 list_add(&b->miscj,
416                          &per_cpu(threshold_banks, cpu)[bank]->blocks->miscj);
417         else
418                 per_cpu(threshold_banks, cpu)[bank]->blocks = b;
419
420         kobject_set_name(&b->kobj, "misc%i", block);
421         b->kobj.parent = &per_cpu(threshold_banks, cpu)[bank]->kobj;
422         b->kobj.ktype = &threshold_ktype;
423         err = kobject_register(&b->kobj);
424         if (err)
425                 goto out_free;
426 recurse:
427         if (!block) {
428                 address = (low & MASK_BLKPTR_LO) >> 21;
429                 if (!address)
430                         return 0;
431                 address += MCG_XBLK_ADDR;
432         } else
433                 ++address;
434
435         err = allocate_threshold_blocks(cpu, bank, ++block, address);
436         if (err)
437                 goto out_free;
438
439         return err;
440
441 out_free:
442         if (b) {
443                 kobject_unregister(&b->kobj);
444                 kfree(b);
445         }
446         return err;
447 }
448
449 /* symlinks sibling shared banks to first core.  first core owns dir/files. */
450 static __cpuinit int threshold_create_bank(unsigned int cpu, unsigned int bank)
451 {
452         int i, err = 0;
453         struct threshold_bank *b = NULL;
454         cpumask_t oldmask = CPU_MASK_NONE;
455         char name[32];
456
457         sprintf(name, "threshold_bank%i", bank);
458
459 #ifdef CONFIG_SMP
460         if (cpu_data[cpu].cpu_core_id && shared_bank[bank]) {   /* symlink */
461                 i = first_cpu(cpu_core_map[cpu]);
462
463                 /* first core not up yet */
464                 if (cpu_data[i].cpu_core_id)
465                         goto out;
466
467                 /* already linked */
468                 if (per_cpu(threshold_banks, cpu)[bank])
469                         goto out;
470
471                 b = per_cpu(threshold_banks, i)[bank];
472
473                 if (!b)
474                         goto out;
475
476                 err = sysfs_create_link(&per_cpu(device_mce, cpu).kobj,
477                                         &b->kobj, name);
478                 if (err)
479                         goto out;
480
481                 b->cpus = cpu_core_map[cpu];
482                 per_cpu(threshold_banks, cpu)[bank] = b;
483                 goto out;
484         }
485 #endif
486
487         b = kzalloc(sizeof(struct threshold_bank), GFP_KERNEL);
488         if (!b) {
489                 err = -ENOMEM;
490                 goto out;
491         }
492
493         kobject_set_name(&b->kobj, "threshold_bank%i", bank);
494         b->kobj.parent = &per_cpu(device_mce, cpu).kobj;
495 #ifndef CONFIG_SMP
496         b->cpus = CPU_MASK_ALL;
497 #else
498         b->cpus = cpu_core_map[cpu];
499 #endif
500         err = kobject_register(&b->kobj);
501         if (err)
502                 goto out_free;
503
504         per_cpu(threshold_banks, cpu)[bank] = b;
505
506         oldmask = affinity_set(cpu);
507         err = allocate_threshold_blocks(cpu, bank, 0,
508                                         MSR_IA32_MC0_MISC + bank * 4);
509         affinity_restore(oldmask);
510
511         if (err)
512                 goto out_free;
513
514         for_each_cpu_mask(i, b->cpus) {
515                 if (i == cpu)
516                         continue;
517
518                 err = sysfs_create_link(&per_cpu(device_mce, i).kobj,
519                                         &b->kobj, name);
520                 if (err)
521                         goto out;
522
523                 per_cpu(threshold_banks, i)[bank] = b;
524         }
525
526         goto out;
527
528 out_free:
529         per_cpu(threshold_banks, cpu)[bank] = NULL;
530         kfree(b);
531 out:
532         return err;
533 }
534
535 /* create dir/files for all valid threshold banks */
536 static __cpuinit int threshold_create_device(unsigned int cpu)
537 {
538         unsigned int bank;
539         int err = 0;
540
541         for (bank = 0; bank < NR_BANKS; ++bank) {
542                 if (!(per_cpu(bank_map, cpu) & 1 << bank))
543                         continue;
544                 err = threshold_create_bank(cpu, bank);
545                 if (err)
546                         goto out;
547         }
548 out:
549         return err;
550 }
551
552 /*
553  * let's be hotplug friendly.
554  * in case of multiple core processors, the first core always takes ownership
555  *   of shared sysfs dir/files, and rest of the cores will be symlinked to it.
556  */
557
558 static void deallocate_threshold_block(unsigned int cpu,
559                                                  unsigned int bank)
560 {
561         struct threshold_block *pos = NULL;
562         struct threshold_block *tmp = NULL;
563         struct threshold_bank *head = per_cpu(threshold_banks, cpu)[bank];
564
565         if (!head)
566                 return;
567
568         list_for_each_entry_safe(pos, tmp, &head->blocks->miscj, miscj) {
569                 kobject_unregister(&pos->kobj);
570                 list_del(&pos->miscj);
571                 kfree(pos);
572         }
573
574         kfree(per_cpu(threshold_banks, cpu)[bank]->blocks);
575         per_cpu(threshold_banks, cpu)[bank]->blocks = NULL;
576 }
577
578 static void threshold_remove_bank(unsigned int cpu, int bank)
579 {
580         int i = 0;
581         struct threshold_bank *b;
582         char name[32];
583
584         b = per_cpu(threshold_banks, cpu)[bank];
585
586         if (!b)
587                 return;
588
589         if (!b->blocks)
590                 goto free_out;
591
592         sprintf(name, "threshold_bank%i", bank);
593
594 #ifdef CONFIG_SMP
595         /* sibling symlink */
596         if (shared_bank[bank] && b->blocks->cpu != cpu) {
597                 sysfs_remove_link(&per_cpu(device_mce, cpu).kobj, name);
598                 per_cpu(threshold_banks, cpu)[bank] = NULL;
599                 return;
600         }
601 #endif
602
603         /* remove all sibling symlinks before unregistering */
604         for_each_cpu_mask(i, b->cpus) {
605                 if (i == cpu)
606                         continue;
607
608                 sysfs_remove_link(&per_cpu(device_mce, i).kobj, name);
609                 per_cpu(threshold_banks, i)[bank] = NULL;
610         }
611
612         deallocate_threshold_block(cpu, bank);
613
614 free_out:
615         kobject_unregister(&b->kobj);
616         kfree(b);
617         per_cpu(threshold_banks, cpu)[bank] = NULL;
618 }
619
620 static void threshold_remove_device(unsigned int cpu)
621 {
622         unsigned int bank;
623
624         for (bank = 0; bank < NR_BANKS; ++bank) {
625                 if (!(per_cpu(bank_map, cpu) & 1 << bank))
626                         continue;
627                 threshold_remove_bank(cpu, bank);
628         }
629 }
630
631 /* get notified when a cpu comes on/off */
632 static int threshold_cpu_callback(struct notifier_block *nfb,
633                                             unsigned long action, void *hcpu)
634 {
635         /* cpu was unsigned int to begin with */
636         unsigned int cpu = (unsigned long)hcpu;
637
638         if (cpu >= NR_CPUS)
639                 goto out;
640
641         switch (action) {
642         case CPU_ONLINE:
643                 threshold_create_device(cpu);
644                 break;
645         case CPU_DEAD:
646                 threshold_remove_device(cpu);
647                 break;
648         default:
649                 break;
650         }
651       out:
652         return NOTIFY_OK;
653 }
654
655 static struct notifier_block threshold_cpu_notifier = {
656         .notifier_call = threshold_cpu_callback,
657 };
658
659 static __init int threshold_init_device(void)
660 {
661         unsigned lcpu = 0;
662
663         /* to hit CPUs online before the notifier is up */
664         for_each_online_cpu(lcpu) {
665                 int err = threshold_create_device(lcpu);
666                 if (err)
667                         return err;
668         }
669         register_hotcpu_notifier(&threshold_cpu_notifier);
670         return 0;
671 }
672
673 device_initcall(threshold_init_device);