ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / i386 / kernel / cpu / mcheck / p4.c
1 /*
2  * P4 specific Machine Check Exception Reporting
3  */
4
5 #include <linux/init.h>
6 #include <linux/types.h>
7 #include <linux/kernel.h>
8 #include <linux/config.h>
9 #include <linux/irq.h>
10 #include <linux/interrupt.h>
11 #include <linux/smp.h>
12
13 #include <asm/processor.h> 
14 #include <asm/system.h>
15 #include <asm/msr.h>
16 #include <asm/apic.h>
17
18 #include "mce.h"
19
20 /* as supported by the P4/Xeon family */
21 struct intel_mce_extended_msrs {
22         u32 eax;
23         u32 ebx;
24         u32 ecx;
25         u32 edx;
26         u32 esi;
27         u32 edi;
28         u32 ebp;
29         u32 esp;
30         u32 eflags;
31         u32 eip;
32         /* u32 *reserved[]; */
33 };
34
35 static int mce_num_extended_msrs = 0;
36
37
38 #ifdef CONFIG_X86_MCE_P4THERMAL
39 static void unexpected_thermal_interrupt(struct pt_regs *regs)
40 {       
41         printk(KERN_ERR "CPU#%d: Unexpected LVT TMR interrupt!\n", smp_processor_id());
42 }
43
44 /* P4/Xeon Thermal transition interrupt handler */
45 static void intel_thermal_interrupt(struct pt_regs *regs)
46 {
47         u32 l, h;
48         unsigned int cpu = smp_processor_id();
49
50         ack_APIC_irq();
51
52         rdmsr (MSR_IA32_THERM_STATUS, l, h);
53         if (l & 1) {
54                 printk(KERN_EMERG "CPU#%d: Temperature above threshold\n", cpu);
55                 printk(KERN_EMERG "CPU#%d: Running in modulated clock mode\n", cpu);
56         } else {
57                 printk(KERN_INFO "CPU#%d: Temperature/speed normal\n", cpu);
58         }
59 }
60
61 /* Thermal interrupt handler for this CPU setup */
62 static void (*vendor_thermal_interrupt)(struct pt_regs *regs) = unexpected_thermal_interrupt;
63
64 asmlinkage void smp_thermal_interrupt(struct pt_regs regs)
65 {
66         irq_enter();
67         vendor_thermal_interrupt(&regs);
68         irq_exit();
69 }
70
71 /* P4/Xeon Thermal regulation detect and init */
72 static void __init intel_init_thermal(struct cpuinfo_x86 *c)
73 {
74         u32 l, h;
75         unsigned int cpu = smp_processor_id();
76
77         /* Thermal monitoring */
78         if (!cpu_has(c, X86_FEATURE_ACPI))
79                 return; /* -ENODEV */
80
81         /* Clock modulation */
82         if (!cpu_has(c, X86_FEATURE_ACC))
83                 return; /* -ENODEV */
84
85         /* first check if its enabled already, in which case there might
86          * be some SMM goo which handles it, so we can't even put a handler
87          * since it might be delivered via SMI already -zwanem.
88          */
89         rdmsr (MSR_IA32_MISC_ENABLE, l, h);
90         h = apic_read(APIC_LVTTHMR);
91         if ((l & (1<<3)) && (h & APIC_DM_SMI)) {
92                 printk(KERN_DEBUG "CPU#%d: Thermal monitoring handled by SMI\n", cpu);
93                 return; /* -EBUSY */
94         }
95
96         /* check whether a vector already exists, temporarily masked? */        
97         if (h & APIC_VECTOR_MASK) {
98                 printk(KERN_DEBUG "CPU#%d: Thermal LVT vector (%#x) already installed\n",
99                         cpu, (h & APIC_VECTOR_MASK));
100                 return; /* -EBUSY */
101         }
102
103         /* The temperature transition interrupt handler setup */
104         h = THERMAL_APIC_VECTOR;                /* our delivery vector */
105         h |= (APIC_DM_FIXED | APIC_LVT_MASKED); /* we'll mask till we're ready */
106         apic_write_around(APIC_LVTTHMR, h);
107
108         rdmsr (MSR_IA32_THERM_INTERRUPT, l, h);
109         wrmsr (MSR_IA32_THERM_INTERRUPT, l | 0x03 , h);
110
111         /* ok we're good to go... */
112         vendor_thermal_interrupt = intel_thermal_interrupt;
113         
114         rdmsr (MSR_IA32_MISC_ENABLE, l, h);
115         wrmsr (MSR_IA32_MISC_ENABLE, l | (1<<3), h);
116         
117         l = apic_read (APIC_LVTTHMR);
118         apic_write_around (APIC_LVTTHMR, l & ~APIC_LVT_MASKED);
119         printk (KERN_INFO "CPU#%d: Thermal monitoring enabled\n", cpu);
120         return;
121 }
122 #endif /* CONFIG_X86_MCE_P4THERMAL */
123
124
125 /* P4/Xeon Extended MCE MSR retrieval, return 0 if unsupported */
126 static inline int intel_get_extended_msrs(struct intel_mce_extended_msrs *r)
127 {
128         u32 h;
129
130         if (mce_num_extended_msrs == 0)
131                 goto done;
132
133         rdmsr (MSR_IA32_MCG_EAX, r->eax, h);
134         rdmsr (MSR_IA32_MCG_EBX, r->ebx, h);
135         rdmsr (MSR_IA32_MCG_ECX, r->ecx, h);
136         rdmsr (MSR_IA32_MCG_EDX, r->edx, h);
137         rdmsr (MSR_IA32_MCG_ESI, r->esi, h);
138         rdmsr (MSR_IA32_MCG_EDI, r->edi, h);
139         rdmsr (MSR_IA32_MCG_EBP, r->ebp, h);
140         rdmsr (MSR_IA32_MCG_ESP, r->esp, h);
141         rdmsr (MSR_IA32_MCG_EFLAGS, r->eflags, h);
142         rdmsr (MSR_IA32_MCG_EIP, r->eip, h);
143
144         /* can we rely on kmalloc to do a dynamic
145          * allocation for the reserved registers?
146          */
147 done:
148         return mce_num_extended_msrs;
149 }
150
151 static asmlinkage void intel_machine_check(struct pt_regs * regs, long error_code)
152 {
153         int recover=1;
154         u32 alow, ahigh, high, low;
155         u32 mcgstl, mcgsth;
156         int i;
157         struct intel_mce_extended_msrs dbg;
158
159         rdmsr (MSR_IA32_MCG_STATUS, mcgstl, mcgsth);
160         if (mcgstl & (1<<0))    /* Recoverable ? */
161                 recover=0;
162
163         printk (KERN_EMERG "CPU %d: Machine Check Exception: %08x%08x\n",
164                 smp_processor_id(), mcgsth, mcgstl);
165
166         if (intel_get_extended_msrs(&dbg)) {
167                 printk (KERN_DEBUG "CPU %d: EIP: %08x EFLAGS: %08x\n",
168                         smp_processor_id(), dbg.eip, dbg.eflags);
169                 printk (KERN_DEBUG "\teax: %08x ebx: %08x ecx: %08x edx: %08x\n",
170                         dbg.eax, dbg.ebx, dbg.ecx, dbg.edx);
171                 printk (KERN_DEBUG "\tesi: %08x edi: %08x ebp: %08x esp: %08x\n",
172                         dbg.esi, dbg.edi, dbg.ebp, dbg.esp);
173         }
174
175         for (i=0; i<nr_mce_banks; i++) {
176                 rdmsr (MSR_IA32_MC0_STATUS+i*4,low, high);
177                 if (high & (1<<31)) {
178                         if (high & (1<<29))
179                                 recover |= 1;
180                         if (high & (1<<25))
181                                 recover |= 2;
182                         printk (KERN_EMERG "Bank %d: %08x%08x", i, high, low);
183                         high &= ~(1<<31);
184                         if (high & (1<<27)) {
185                                 rdmsr (MSR_IA32_MC0_MISC+i*4, alow, ahigh);
186                                 printk ("[%08x%08x]", ahigh, alow);
187                         }
188                         if (high & (1<<26)) {
189                                 rdmsr (MSR_IA32_MC0_ADDR+i*4, alow, ahigh);
190                                 printk (" at %08x%08x", ahigh, alow);
191                         }
192                         printk ("\n");
193                 }
194         }
195
196         if (recover & 2)
197                 panic ("CPU context corrupt");
198         if (recover & 1)
199                 panic ("Unable to continue");
200
201         printk(KERN_EMERG "Attempting to continue.\n");
202         /* 
203          * Do not clear the MSR_IA32_MCi_STATUS if the error is not 
204          * recoverable/continuable.This will allow BIOS to look at the MSRs
205          * for errors if the OS could not log the error.
206          */
207         for (i=0; i<nr_mce_banks; i++) {
208                 u32 msr;
209                 msr = MSR_IA32_MC0_STATUS+i*4;
210                 rdmsr (msr, low, high);
211                 if (high&(1<<31)) {
212                         /* Clear it */
213                         wrmsr(msr, 0UL, 0UL);
214                         /* Serialize */
215                         wmb();
216                 }
217         }
218         mcgstl &= ~(1<<2);
219         wrmsr (MSR_IA32_MCG_STATUS,mcgstl, mcgsth);
220 }
221
222
223 void __init intel_p4_mcheck_init(struct cpuinfo_x86 *c)
224 {
225         u32 l, h;
226         int i;
227         
228         machine_check_vector = intel_machine_check;
229         wmb();
230
231         printk (KERN_INFO "Intel machine check architecture supported.\n");
232         rdmsr (MSR_IA32_MCG_CAP, l, h);
233         if (l & (1<<8)) /* Control register present ? */
234                 wrmsr (MSR_IA32_MCG_CTL, 0xffffffff, 0xffffffff);
235         nr_mce_banks = l & 0xff;
236
237         for (i=0; i<nr_mce_banks; i++) {
238                 wrmsr (MSR_IA32_MC0_CTL+4*i, 0xffffffff, 0xffffffff);
239                 wrmsr (MSR_IA32_MC0_STATUS+4*i, 0x0, 0x0);
240         }
241
242         set_in_cr4 (X86_CR4_MCE);
243         printk (KERN_INFO "Intel machine check reporting enabled on CPU#%d.\n",
244                 smp_processor_id());
245
246         /* Check for P4/Xeon extended MCE MSRs */
247         rdmsr (MSR_IA32_MCG_CAP, l, h);
248         if (l & (1<<9)) {/* MCG_EXT_P */
249                 mce_num_extended_msrs = (l >> 16) & 0xff;
250                 printk (KERN_INFO "CPU#%d: Intel P4/Xeon Extended MCE MSRs (%d) available\n",
251                         smp_processor_id(), mce_num_extended_msrs);
252
253 #ifdef CONFIG_X86_MCE_P4THERMAL
254                 /* Check for P4/Xeon Thermal monitor */
255                 intel_init_thermal(c);
256 #endif
257         }
258 }