Merge branch 'mv-merge'
[sfrench/cifs-2.6.git] / arch / i386 / kernel / apic.c
1 /*
2  *      Local APIC handling, local APIC timers
3  *
4  *      (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5  *
6  *      Fixes
7  *      Maciej W. Rozycki       :       Bits for genuine 82489DX APICs;
8  *                                      thanks to Eric Gilmore
9  *                                      and Rolf G. Tews
10  *                                      for testing these extensively.
11  *      Maciej W. Rozycki       :       Various updates and fixes.
12  *      Mikael Pettersson       :       Power Management for UP-APIC.
13  *      Pavel Machek and
14  *      Mikael Pettersson       :       PM converted to driver model.
15  */
16
17 #include <linux/config.h>
18 #include <linux/init.h>
19
20 #include <linux/mm.h>
21 #include <linux/delay.h>
22 #include <linux/bootmem.h>
23 #include <linux/smp_lock.h>
24 #include <linux/interrupt.h>
25 #include <linux/mc146818rtc.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/sysdev.h>
28 #include <linux/cpu.h>
29 #include <linux/module.h>
30
31 #include <asm/atomic.h>
32 #include <asm/smp.h>
33 #include <asm/mtrr.h>
34 #include <asm/mpspec.h>
35 #include <asm/desc.h>
36 #include <asm/arch_hooks.h>
37 #include <asm/hpet.h>
38 #include <asm/i8253.h>
39
40 #include <mach_apic.h>
41 #include <mach_apicdef.h>
42 #include <mach_ipi.h>
43
44 #include "io_ports.h"
45
46 /*
47  * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
48  * IPIs in place of local APIC timers
49  */
50 static cpumask_t timer_bcast_ipi;
51
52 /*
53  * Knob to control our willingness to enable the local APIC.
54  */
55 int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
56
57 /*
58  * Debug level
59  */
60 int apic_verbosity;
61
62
63 static void apic_pm_activate(void);
64
65 /*
66  * 'what should we do if we get a hw irq event on an illegal vector'.
67  * each architecture has to answer this themselves.
68  */
69 void ack_bad_irq(unsigned int irq)
70 {
71         printk("unexpected IRQ trap at vector %02x\n", irq);
72         /*
73          * Currently unexpected vectors happen only on SMP and APIC.
74          * We _must_ ack these because every local APIC has only N
75          * irq slots per priority level, and a 'hanging, unacked' IRQ
76          * holds up an irq slot - in excessive cases (when multiple
77          * unexpected vectors occur) that might lock up the APIC
78          * completely.
79          * But only ack when the APIC is enabled -AK
80          */
81         if (cpu_has_apic)
82                 ack_APIC_irq();
83 }
84
85 void __init apic_intr_init(void)
86 {
87 #ifdef CONFIG_SMP
88         smp_intr_init();
89 #endif
90         /* self generated IPI for local APIC timer */
91         set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
92
93         /* IPI vectors for APIC spurious and error interrupts */
94         set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
95         set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
96
97         /* thermal monitor LVT interrupt */
98 #ifdef CONFIG_X86_MCE_P4THERMAL
99         set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
100 #endif
101 }
102
103 /* Using APIC to generate smp_local_timer_interrupt? */
104 int using_apic_timer = 0;
105
106 static int enabled_via_apicbase;
107
108 void enable_NMI_through_LVT0 (void * dummy)
109 {
110         unsigned int v, ver;
111
112         ver = apic_read(APIC_LVR);
113         ver = GET_APIC_VERSION(ver);
114         v = APIC_DM_NMI;                        /* unmask and set to NMI */
115         if (!APIC_INTEGRATED(ver))              /* 82489DX */
116                 v |= APIC_LVT_LEVEL_TRIGGER;
117         apic_write_around(APIC_LVT0, v);
118 }
119
120 int get_physical_broadcast(void)
121 {
122         unsigned int lvr, version;
123         lvr = apic_read(APIC_LVR);
124         version = GET_APIC_VERSION(lvr);
125         if (!APIC_INTEGRATED(version) || version >= 0x14)
126                 return 0xff;
127         else
128                 return 0xf;
129 }
130
131 int get_maxlvt(void)
132 {
133         unsigned int v, ver, maxlvt;
134
135         v = apic_read(APIC_LVR);
136         ver = GET_APIC_VERSION(v);
137         /* 82489DXs do not report # of LVT entries. */
138         maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
139         return maxlvt;
140 }
141
142 void clear_local_APIC(void)
143 {
144         int maxlvt;
145         unsigned long v;
146
147         maxlvt = get_maxlvt();
148
149         /*
150          * Masking an LVT entry on a P6 can trigger a local APIC error
151          * if the vector is zero. Mask LVTERR first to prevent this.
152          */
153         if (maxlvt >= 3) {
154                 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
155                 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
156         }
157         /*
158          * Careful: we have to set masks only first to deassert
159          * any level-triggered sources.
160          */
161         v = apic_read(APIC_LVTT);
162         apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
163         v = apic_read(APIC_LVT0);
164         apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
165         v = apic_read(APIC_LVT1);
166         apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
167         if (maxlvt >= 4) {
168                 v = apic_read(APIC_LVTPC);
169                 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
170         }
171
172 /* lets not touch this if we didn't frob it */
173 #ifdef CONFIG_X86_MCE_P4THERMAL
174         if (maxlvt >= 5) {
175                 v = apic_read(APIC_LVTTHMR);
176                 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
177         }
178 #endif
179         /*
180          * Clean APIC state for other OSs:
181          */
182         apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
183         apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
184         apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
185         if (maxlvt >= 3)
186                 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
187         if (maxlvt >= 4)
188                 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
189
190 #ifdef CONFIG_X86_MCE_P4THERMAL
191         if (maxlvt >= 5)
192                 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
193 #endif
194         v = GET_APIC_VERSION(apic_read(APIC_LVR));
195         if (APIC_INTEGRATED(v)) {       /* !82489DX */
196                 if (maxlvt > 3)         /* Due to Pentium errata 3AP and 11AP. */
197                         apic_write(APIC_ESR, 0);
198                 apic_read(APIC_ESR);
199         }
200 }
201
202 void __init connect_bsp_APIC(void)
203 {
204         if (pic_mode) {
205                 /*
206                  * Do not trust the local APIC being empty at bootup.
207                  */
208                 clear_local_APIC();
209                 /*
210                  * PIC mode, enable APIC mode in the IMCR, i.e.
211                  * connect BSP's local APIC to INT and NMI lines.
212                  */
213                 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
214                                 "enabling APIC mode.\n");
215                 outb(0x70, 0x22);
216                 outb(0x01, 0x23);
217         }
218         enable_apic_mode();
219 }
220
221 void disconnect_bsp_APIC(int virt_wire_setup)
222 {
223         if (pic_mode) {
224                 /*
225                  * Put the board back into PIC mode (has an effect
226                  * only on certain older boards).  Note that APIC
227                  * interrupts, including IPIs, won't work beyond
228                  * this point!  The only exception are INIT IPIs.
229                  */
230                 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
231                                 "entering PIC mode.\n");
232                 outb(0x70, 0x22);
233                 outb(0x00, 0x23);
234         }
235         else {
236                 /* Go back to Virtual Wire compatibility mode */
237                 unsigned long value;
238
239                 /* For the spurious interrupt use vector F, and enable it */
240                 value = apic_read(APIC_SPIV);
241                 value &= ~APIC_VECTOR_MASK;
242                 value |= APIC_SPIV_APIC_ENABLED;
243                 value |= 0xf;
244                 apic_write_around(APIC_SPIV, value);
245
246                 if (!virt_wire_setup) {
247                         /* For LVT0 make it edge triggered, active high, external and enabled */
248                         value = apic_read(APIC_LVT0);
249                         value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
250                                 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
251                                 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
252                         value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
253                         value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
254                         apic_write_around(APIC_LVT0, value);
255                 }
256                 else {
257                         /* Disable LVT0 */
258                         apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
259                 }
260
261                 /* For LVT1 make it edge triggered, active high, nmi and enabled */
262                 value = apic_read(APIC_LVT1);
263                 value &= ~(
264                         APIC_MODE_MASK | APIC_SEND_PENDING |
265                         APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
266                         APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
267                 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
268                 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
269                 apic_write_around(APIC_LVT1, value);
270         }
271 }
272
273 void disable_local_APIC(void)
274 {
275         unsigned long value;
276
277         clear_local_APIC();
278
279         /*
280          * Disable APIC (implies clearing of registers
281          * for 82489DX!).
282          */
283         value = apic_read(APIC_SPIV);
284         value &= ~APIC_SPIV_APIC_ENABLED;
285         apic_write_around(APIC_SPIV, value);
286
287         if (enabled_via_apicbase) {
288                 unsigned int l, h;
289                 rdmsr(MSR_IA32_APICBASE, l, h);
290                 l &= ~MSR_IA32_APICBASE_ENABLE;
291                 wrmsr(MSR_IA32_APICBASE, l, h);
292         }
293 }
294
295 /*
296  * This is to verify that we're looking at a real local APIC.
297  * Check these against your board if the CPUs aren't getting
298  * started for no apparent reason.
299  */
300 int __init verify_local_APIC(void)
301 {
302         unsigned int reg0, reg1;
303
304         /*
305          * The version register is read-only in a real APIC.
306          */
307         reg0 = apic_read(APIC_LVR);
308         apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
309         apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
310         reg1 = apic_read(APIC_LVR);
311         apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
312
313         /*
314          * The two version reads above should print the same
315          * numbers.  If the second one is different, then we
316          * poke at a non-APIC.
317          */
318         if (reg1 != reg0)
319                 return 0;
320
321         /*
322          * Check if the version looks reasonably.
323          */
324         reg1 = GET_APIC_VERSION(reg0);
325         if (reg1 == 0x00 || reg1 == 0xff)
326                 return 0;
327         reg1 = get_maxlvt();
328         if (reg1 < 0x02 || reg1 == 0xff)
329                 return 0;
330
331         /*
332          * The ID register is read/write in a real APIC.
333          */
334         reg0 = apic_read(APIC_ID);
335         apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
336
337         /*
338          * The next two are just to see if we have sane values.
339          * They're only really relevant if we're in Virtual Wire
340          * compatibility mode, but most boxes are anymore.
341          */
342         reg0 = apic_read(APIC_LVT0);
343         apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
344         reg1 = apic_read(APIC_LVT1);
345         apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
346
347         return 1;
348 }
349
350 void __init sync_Arb_IDs(void)
351 {
352         /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
353         unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
354         if (ver >= 0x14)        /* P4 or higher */
355                 return;
356         /*
357          * Wait for idle.
358          */
359         apic_wait_icr_idle();
360
361         apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
362         apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
363                                 | APIC_DM_INIT);
364 }
365
366 extern void __error_in_apic_c (void);
367
368 /*
369  * An initial setup of the virtual wire mode.
370  */
371 void __init init_bsp_APIC(void)
372 {
373         unsigned long value, ver;
374
375         /*
376          * Don't do the setup now if we have a SMP BIOS as the
377          * through-I/O-APIC virtual wire mode might be active.
378          */
379         if (smp_found_config || !cpu_has_apic)
380                 return;
381
382         value = apic_read(APIC_LVR);
383         ver = GET_APIC_VERSION(value);
384
385         /*
386          * Do not trust the local APIC being empty at bootup.
387          */
388         clear_local_APIC();
389
390         /*
391          * Enable APIC.
392          */
393         value = apic_read(APIC_SPIV);
394         value &= ~APIC_VECTOR_MASK;
395         value |= APIC_SPIV_APIC_ENABLED;
396         
397         /* This bit is reserved on P4/Xeon and should be cleared */
398         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
399                 value &= ~APIC_SPIV_FOCUS_DISABLED;
400         else
401                 value |= APIC_SPIV_FOCUS_DISABLED;
402         value |= SPURIOUS_APIC_VECTOR;
403         apic_write_around(APIC_SPIV, value);
404
405         /*
406          * Set up the virtual wire mode.
407          */
408         apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
409         value = APIC_DM_NMI;
410         if (!APIC_INTEGRATED(ver))              /* 82489DX */
411                 value |= APIC_LVT_LEVEL_TRIGGER;
412         apic_write_around(APIC_LVT1, value);
413 }
414
415 void __devinit setup_local_APIC(void)
416 {
417         unsigned long oldvalue, value, ver, maxlvt;
418
419         /* Pound the ESR really hard over the head with a big hammer - mbligh */
420         if (esr_disable) {
421                 apic_write(APIC_ESR, 0);
422                 apic_write(APIC_ESR, 0);
423                 apic_write(APIC_ESR, 0);
424                 apic_write(APIC_ESR, 0);
425         }
426
427         value = apic_read(APIC_LVR);
428         ver = GET_APIC_VERSION(value);
429
430         if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
431                 __error_in_apic_c();
432
433         /*
434          * Double-check whether this APIC is really registered.
435          */
436         if (!apic_id_registered())
437                 BUG();
438
439         /*
440          * Intel recommends to set DFR, LDR and TPR before enabling
441          * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
442          * document number 292116).  So here it goes...
443          */
444         init_apic_ldr();
445
446         /*
447          * Set Task Priority to 'accept all'. We never change this
448          * later on.
449          */
450         value = apic_read(APIC_TASKPRI);
451         value &= ~APIC_TPRI_MASK;
452         apic_write_around(APIC_TASKPRI, value);
453
454         /*
455          * Now that we are all set up, enable the APIC
456          */
457         value = apic_read(APIC_SPIV);
458         value &= ~APIC_VECTOR_MASK;
459         /*
460          * Enable APIC
461          */
462         value |= APIC_SPIV_APIC_ENABLED;
463
464         /*
465          * Some unknown Intel IO/APIC (or APIC) errata is biting us with
466          * certain networking cards. If high frequency interrupts are
467          * happening on a particular IOAPIC pin, plus the IOAPIC routing
468          * entry is masked/unmasked at a high rate as well then sooner or
469          * later IOAPIC line gets 'stuck', no more interrupts are received
470          * from the device. If focus CPU is disabled then the hang goes
471          * away, oh well :-(
472          *
473          * [ This bug can be reproduced easily with a level-triggered
474          *   PCI Ne2000 networking cards and PII/PIII processors, dual
475          *   BX chipset. ]
476          */
477         /*
478          * Actually disabling the focus CPU check just makes the hang less
479          * frequent as it makes the interrupt distributon model be more
480          * like LRU than MRU (the short-term load is more even across CPUs).
481          * See also the comment in end_level_ioapic_irq().  --macro
482          */
483 #if 1
484         /* Enable focus processor (bit==0) */
485         value &= ~APIC_SPIV_FOCUS_DISABLED;
486 #else
487         /* Disable focus processor (bit==1) */
488         value |= APIC_SPIV_FOCUS_DISABLED;
489 #endif
490         /*
491          * Set spurious IRQ vector
492          */
493         value |= SPURIOUS_APIC_VECTOR;
494         apic_write_around(APIC_SPIV, value);
495
496         /*
497          * Set up LVT0, LVT1:
498          *
499          * set up through-local-APIC on the BP's LINT0. This is not
500          * strictly necessery in pure symmetric-IO mode, but sometimes
501          * we delegate interrupts to the 8259A.
502          */
503         /*
504          * TODO: set up through-local-APIC from through-I/O-APIC? --macro
505          */
506         value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
507         if (!smp_processor_id() && (pic_mode || !value)) {
508                 value = APIC_DM_EXTINT;
509                 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
510                                 smp_processor_id());
511         } else {
512                 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
513                 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
514                                 smp_processor_id());
515         }
516         apic_write_around(APIC_LVT0, value);
517
518         /*
519          * only the BP should see the LINT1 NMI signal, obviously.
520          */
521         if (!smp_processor_id())
522                 value = APIC_DM_NMI;
523         else
524                 value = APIC_DM_NMI | APIC_LVT_MASKED;
525         if (!APIC_INTEGRATED(ver))              /* 82489DX */
526                 value |= APIC_LVT_LEVEL_TRIGGER;
527         apic_write_around(APIC_LVT1, value);
528
529         if (APIC_INTEGRATED(ver) && !esr_disable) {             /* !82489DX */
530                 maxlvt = get_maxlvt();
531                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
532                         apic_write(APIC_ESR, 0);
533                 oldvalue = apic_read(APIC_ESR);
534
535                 value = ERROR_APIC_VECTOR;      // enables sending errors
536                 apic_write_around(APIC_LVTERR, value);
537                 /*
538                  * spec says clear errors after enabling vector.
539                  */
540                 if (maxlvt > 3)
541                         apic_write(APIC_ESR, 0);
542                 value = apic_read(APIC_ESR);
543                 if (value != oldvalue)
544                         apic_printk(APIC_VERBOSE, "ESR value before enabling "
545                                 "vector: 0x%08lx  after: 0x%08lx\n",
546                                 oldvalue, value);
547         } else {
548                 if (esr_disable)        
549                         /* 
550                          * Something untraceble is creating bad interrupts on 
551                          * secondary quads ... for the moment, just leave the
552                          * ESR disabled - we can't do anything useful with the
553                          * errors anyway - mbligh
554                          */
555                         printk("Leaving ESR disabled.\n");
556                 else 
557                         printk("No ESR for 82489DX.\n");
558         }
559
560         if (nmi_watchdog == NMI_LOCAL_APIC)
561                 setup_apic_nmi_watchdog();
562         apic_pm_activate();
563 }
564
565 /*
566  * If Linux enabled the LAPIC against the BIOS default
567  * disable it down before re-entering the BIOS on shutdown.
568  * Otherwise the BIOS may get confused and not power-off.
569  * Additionally clear all LVT entries before disable_local_APIC
570  * for the case where Linux didn't enable the LAPIC.
571  */
572 void lapic_shutdown(void)
573 {
574         unsigned long flags;
575
576         if (!cpu_has_apic)
577                 return;
578
579         local_irq_save(flags);
580         clear_local_APIC();
581
582         if (enabled_via_apicbase)
583                 disable_local_APIC();
584
585         local_irq_restore(flags);
586 }
587
588 #ifdef CONFIG_PM
589
590 static struct {
591         int active;
592         /* r/w apic fields */
593         unsigned int apic_id;
594         unsigned int apic_taskpri;
595         unsigned int apic_ldr;
596         unsigned int apic_dfr;
597         unsigned int apic_spiv;
598         unsigned int apic_lvtt;
599         unsigned int apic_lvtpc;
600         unsigned int apic_lvt0;
601         unsigned int apic_lvt1;
602         unsigned int apic_lvterr;
603         unsigned int apic_tmict;
604         unsigned int apic_tdcr;
605         unsigned int apic_thmr;
606 } apic_pm_state;
607
608 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
609 {
610         unsigned long flags;
611
612         if (!apic_pm_state.active)
613                 return 0;
614
615         apic_pm_state.apic_id = apic_read(APIC_ID);
616         apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
617         apic_pm_state.apic_ldr = apic_read(APIC_LDR);
618         apic_pm_state.apic_dfr = apic_read(APIC_DFR);
619         apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
620         apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
621         apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
622         apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
623         apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
624         apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
625         apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
626         apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
627         apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
628         
629         local_irq_save(flags);
630         disable_local_APIC();
631         local_irq_restore(flags);
632         return 0;
633 }
634
635 static int lapic_resume(struct sys_device *dev)
636 {
637         unsigned int l, h;
638         unsigned long flags;
639
640         if (!apic_pm_state.active)
641                 return 0;
642
643         local_irq_save(flags);
644
645         /*
646          * Make sure the APICBASE points to the right address
647          *
648          * FIXME! This will be wrong if we ever support suspend on
649          * SMP! We'll need to do this as part of the CPU restore!
650          */
651         rdmsr(MSR_IA32_APICBASE, l, h);
652         l &= ~MSR_IA32_APICBASE_BASE;
653         l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
654         wrmsr(MSR_IA32_APICBASE, l, h);
655
656         apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
657         apic_write(APIC_ID, apic_pm_state.apic_id);
658         apic_write(APIC_DFR, apic_pm_state.apic_dfr);
659         apic_write(APIC_LDR, apic_pm_state.apic_ldr);
660         apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
661         apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
662         apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
663         apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
664         apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
665         apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
666         apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
667         apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
668         apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
669         apic_write(APIC_ESR, 0);
670         apic_read(APIC_ESR);
671         apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
672         apic_write(APIC_ESR, 0);
673         apic_read(APIC_ESR);
674         local_irq_restore(flags);
675         return 0;
676 }
677
678 /*
679  * This device has no shutdown method - fully functioning local APICs
680  * are needed on every CPU up until machine_halt/restart/poweroff.
681  */
682
683 static struct sysdev_class lapic_sysclass = {
684         set_kset_name("lapic"),
685         .resume         = lapic_resume,
686         .suspend        = lapic_suspend,
687 };
688
689 static struct sys_device device_lapic = {
690         .id     = 0,
691         .cls    = &lapic_sysclass,
692 };
693
694 static void __devinit apic_pm_activate(void)
695 {
696         apic_pm_state.active = 1;
697 }
698
699 static int __init init_lapic_sysfs(void)
700 {
701         int error;
702
703         if (!cpu_has_apic)
704                 return 0;
705         /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
706
707         error = sysdev_class_register(&lapic_sysclass);
708         if (!error)
709                 error = sysdev_register(&device_lapic);
710         return error;
711 }
712 device_initcall(init_lapic_sysfs);
713
714 #else   /* CONFIG_PM */
715
716 static void apic_pm_activate(void) { }
717
718 #endif  /* CONFIG_PM */
719
720 /*
721  * Detect and enable local APICs on non-SMP boards.
722  * Original code written by Keir Fraser.
723  */
724
725 static int __init apic_set_verbosity(char *str)
726 {
727         if (strcmp("debug", str) == 0)
728                 apic_verbosity = APIC_DEBUG;
729         else if (strcmp("verbose", str) == 0)
730                 apic_verbosity = APIC_VERBOSE;
731         else
732                 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
733                                 " use apic=verbose or apic=debug\n", str);
734
735         return 0;
736 }
737
738 __setup("apic=", apic_set_verbosity);
739
740 static int __init detect_init_APIC (void)
741 {
742         u32 h, l, features;
743
744         /* Disabled by kernel option? */
745         if (enable_local_apic < 0)
746                 return -1;
747
748         switch (boot_cpu_data.x86_vendor) {
749         case X86_VENDOR_AMD:
750                 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
751                     (boot_cpu_data.x86 == 15))      
752                         break;
753                 goto no_apic;
754         case X86_VENDOR_INTEL:
755                 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
756                     (boot_cpu_data.x86 == 5 && cpu_has_apic))
757                         break;
758                 goto no_apic;
759         default:
760                 goto no_apic;
761         }
762
763         if (!cpu_has_apic) {
764                 /*
765                  * Over-ride BIOS and try to enable the local
766                  * APIC only if "lapic" specified.
767                  */
768                 if (enable_local_apic <= 0) {
769                         printk("Local APIC disabled by BIOS -- "
770                                "you can enable it with \"lapic\"\n");
771                         return -1;
772                 }
773                 /*
774                  * Some BIOSes disable the local APIC in the
775                  * APIC_BASE MSR. This can only be done in
776                  * software for Intel P6 or later and AMD K7
777                  * (Model > 1) or later.
778                  */
779                 rdmsr(MSR_IA32_APICBASE, l, h);
780                 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
781                         printk("Local APIC disabled by BIOS -- reenabling.\n");
782                         l &= ~MSR_IA32_APICBASE_BASE;
783                         l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
784                         wrmsr(MSR_IA32_APICBASE, l, h);
785                         enabled_via_apicbase = 1;
786                 }
787         }
788         /*
789          * The APIC feature bit should now be enabled
790          * in `cpuid'
791          */
792         features = cpuid_edx(1);
793         if (!(features & (1 << X86_FEATURE_APIC))) {
794                 printk("Could not enable APIC!\n");
795                 return -1;
796         }
797         set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
798         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
799
800         /* The BIOS may have set up the APIC at some other address */
801         rdmsr(MSR_IA32_APICBASE, l, h);
802         if (l & MSR_IA32_APICBASE_ENABLE)
803                 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
804
805         if (nmi_watchdog != NMI_NONE)
806                 nmi_watchdog = NMI_LOCAL_APIC;
807
808         printk("Found and enabled local APIC!\n");
809
810         apic_pm_activate();
811
812         return 0;
813
814 no_apic:
815         printk("No local APIC present or hardware disabled\n");
816         return -1;
817 }
818
819 void __init init_apic_mappings(void)
820 {
821         unsigned long apic_phys;
822
823         /*
824          * If no local APIC can be found then set up a fake all
825          * zeroes page to simulate the local APIC and another
826          * one for the IO-APIC.
827          */
828         if (!smp_found_config && detect_init_APIC()) {
829                 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
830                 apic_phys = __pa(apic_phys);
831         } else
832                 apic_phys = mp_lapic_addr;
833
834         set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
835         printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
836                apic_phys);
837
838         /*
839          * Fetch the APIC ID of the BSP in case we have a
840          * default configuration (or the MP table is broken).
841          */
842         if (boot_cpu_physical_apicid == -1U)
843                 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
844
845 #ifdef CONFIG_X86_IO_APIC
846         {
847                 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
848                 int i;
849
850                 for (i = 0; i < nr_ioapics; i++) {
851                         if (smp_found_config) {
852                                 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
853                                 if (!ioapic_phys) {
854                                         printk(KERN_ERR
855                                                "WARNING: bogus zero IO-APIC "
856                                                "address found in MPTABLE, "
857                                                "disabling IO/APIC support!\n");
858                                         smp_found_config = 0;
859                                         skip_ioapic_setup = 1;
860                                         goto fake_ioapic_page;
861                                 }
862                         } else {
863 fake_ioapic_page:
864                                 ioapic_phys = (unsigned long)
865                                               alloc_bootmem_pages(PAGE_SIZE);
866                                 ioapic_phys = __pa(ioapic_phys);
867                         }
868                         set_fixmap_nocache(idx, ioapic_phys);
869                         printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
870                                __fix_to_virt(idx), ioapic_phys);
871                         idx++;
872                 }
873         }
874 #endif
875 }
876
877 /*
878  * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
879  * per second. We assume that the caller has already set up the local
880  * APIC.
881  *
882  * The APIC timer is not exactly sync with the external timer chip, it
883  * closely follows bus clocks.
884  */
885
886 /*
887  * The timer chip is already set up at HZ interrupts per second here,
888  * but we do not accept timer interrupts yet. We only allow the BP
889  * to calibrate.
890  */
891 static unsigned int __devinit get_8254_timer_count(void)
892 {
893         unsigned long flags;
894
895         unsigned int count;
896
897         spin_lock_irqsave(&i8253_lock, flags);
898
899         outb_p(0x00, PIT_MODE);
900         count = inb_p(PIT_CH0);
901         count |= inb_p(PIT_CH0) << 8;
902
903         spin_unlock_irqrestore(&i8253_lock, flags);
904
905         return count;
906 }
907
908 /* next tick in 8254 can be caught by catching timer wraparound */
909 static void __devinit wait_8254_wraparound(void)
910 {
911         unsigned int curr_count, prev_count;
912
913         curr_count = get_8254_timer_count();
914         do {
915                 prev_count = curr_count;
916                 curr_count = get_8254_timer_count();
917
918                 /* workaround for broken Mercury/Neptune */
919                 if (prev_count >= curr_count + 0x100)
920                         curr_count = get_8254_timer_count();
921
922         } while (prev_count >= curr_count);
923 }
924
925 /*
926  * Default initialization for 8254 timers. If we use other timers like HPET,
927  * we override this later
928  */
929 void (*wait_timer_tick)(void) __devinitdata = wait_8254_wraparound;
930
931 /*
932  * This function sets up the local APIC timer, with a timeout of
933  * 'clocks' APIC bus clock. During calibration we actually call
934  * this function twice on the boot CPU, once with a bogus timeout
935  * value, second time for real. The other (noncalibrating) CPUs
936  * call this function only once, with the real, calibrated value.
937  *
938  * We do reads before writes even if unnecessary, to get around the
939  * P5 APIC double write bug.
940  */
941
942 #define APIC_DIVISOR 16
943
944 static void __setup_APIC_LVTT(unsigned int clocks)
945 {
946         unsigned int lvtt_value, tmp_value, ver;
947         int cpu = smp_processor_id();
948
949         ver = GET_APIC_VERSION(apic_read(APIC_LVR));
950         lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
951         if (!APIC_INTEGRATED(ver))
952                 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
953
954         if (cpu_isset(cpu, timer_bcast_ipi))
955                 lvtt_value |= APIC_LVT_MASKED;
956
957         apic_write_around(APIC_LVTT, lvtt_value);
958
959         /*
960          * Divide PICLK by 16
961          */
962         tmp_value = apic_read(APIC_TDCR);
963         apic_write_around(APIC_TDCR, (tmp_value
964                                 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
965                                 | APIC_TDR_DIV_16);
966
967         apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
968 }
969
970 static void __devinit setup_APIC_timer(unsigned int clocks)
971 {
972         unsigned long flags;
973
974         local_irq_save(flags);
975
976         /*
977          * Wait for IRQ0's slice:
978          */
979         wait_timer_tick();
980
981         __setup_APIC_LVTT(clocks);
982
983         local_irq_restore(flags);
984 }
985
986 /*
987  * In this function we calibrate APIC bus clocks to the external
988  * timer. Unfortunately we cannot use jiffies and the timer irq
989  * to calibrate, since some later bootup code depends on getting
990  * the first irq? Ugh.
991  *
992  * We want to do the calibration only once since we
993  * want to have local timer irqs syncron. CPUs connected
994  * by the same APIC bus have the very same bus frequency.
995  * And we want to have irqs off anyways, no accidental
996  * APIC irq that way.
997  */
998
999 static int __init calibrate_APIC_clock(void)
1000 {
1001         unsigned long long t1 = 0, t2 = 0;
1002         long tt1, tt2;
1003         long result;
1004         int i;
1005         const int LOOPS = HZ/10;
1006
1007         apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
1008
1009         /*
1010          * Put whatever arbitrary (but long enough) timeout
1011          * value into the APIC clock, we just want to get the
1012          * counter running for calibration.
1013          */
1014         __setup_APIC_LVTT(1000000000);
1015
1016         /*
1017          * The timer chip counts down to zero. Let's wait
1018          * for a wraparound to start exact measurement:
1019          * (the current tick might have been already half done)
1020          */
1021
1022         wait_timer_tick();
1023
1024         /*
1025          * We wrapped around just now. Let's start:
1026          */
1027         if (cpu_has_tsc)
1028                 rdtscll(t1);
1029         tt1 = apic_read(APIC_TMCCT);
1030
1031         /*
1032          * Let's wait LOOPS wraprounds:
1033          */
1034         for (i = 0; i < LOOPS; i++)
1035                 wait_timer_tick();
1036
1037         tt2 = apic_read(APIC_TMCCT);
1038         if (cpu_has_tsc)
1039                 rdtscll(t2);
1040
1041         /*
1042          * The APIC bus clock counter is 32 bits only, it
1043          * might have overflown, but note that we use signed
1044          * longs, thus no extra care needed.
1045          *
1046          * underflown to be exact, as the timer counts down ;)
1047          */
1048
1049         result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
1050
1051         if (cpu_has_tsc)
1052                 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
1053                         "%ld.%04ld MHz.\n",
1054                         ((long)(t2-t1)/LOOPS)/(1000000/HZ),
1055                         ((long)(t2-t1)/LOOPS)%(1000000/HZ));
1056
1057         apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
1058                 "%ld.%04ld MHz.\n",
1059                 result/(1000000/HZ),
1060                 result%(1000000/HZ));
1061
1062         return result;
1063 }
1064
1065 static unsigned int calibration_result;
1066
1067 void __init setup_boot_APIC_clock(void)
1068 {
1069         unsigned long flags;
1070         apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
1071         using_apic_timer = 1;
1072
1073         local_irq_save(flags);
1074
1075         calibration_result = calibrate_APIC_clock();
1076         /*
1077          * Now set up the timer for real.
1078          */
1079         setup_APIC_timer(calibration_result);
1080
1081         local_irq_restore(flags);
1082 }
1083
1084 void __devinit setup_secondary_APIC_clock(void)
1085 {
1086         setup_APIC_timer(calibration_result);
1087 }
1088
1089 void disable_APIC_timer(void)
1090 {
1091         if (using_apic_timer) {
1092                 unsigned long v;
1093
1094                 v = apic_read(APIC_LVTT);
1095                 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
1096         }
1097 }
1098
1099 void enable_APIC_timer(void)
1100 {
1101         int cpu = smp_processor_id();
1102
1103         if (using_apic_timer &&
1104             !cpu_isset(cpu, timer_bcast_ipi)) {
1105                 unsigned long v;
1106
1107                 v = apic_read(APIC_LVTT);
1108                 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
1109         }
1110 }
1111
1112 void switch_APIC_timer_to_ipi(void *cpumask)
1113 {
1114         cpumask_t mask = *(cpumask_t *)cpumask;
1115         int cpu = smp_processor_id();
1116
1117         if (cpu_isset(cpu, mask) &&
1118             !cpu_isset(cpu, timer_bcast_ipi)) {
1119                 disable_APIC_timer();
1120                 cpu_set(cpu, timer_bcast_ipi);
1121         }
1122 }
1123 EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
1124
1125 void switch_ipi_to_APIC_timer(void *cpumask)
1126 {
1127         cpumask_t mask = *(cpumask_t *)cpumask;
1128         int cpu = smp_processor_id();
1129
1130         if (cpu_isset(cpu, mask) &&
1131             cpu_isset(cpu, timer_bcast_ipi)) {
1132                 cpu_clear(cpu, timer_bcast_ipi);
1133                 enable_APIC_timer();
1134         }
1135 }
1136 EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
1137
1138 #undef APIC_DIVISOR
1139
1140 /*
1141  * Local timer interrupt handler. It does both profiling and
1142  * process statistics/rescheduling.
1143  *
1144  * We do profiling in every local tick, statistics/rescheduling
1145  * happen only every 'profiling multiplier' ticks. The default
1146  * multiplier is 1 and it can be changed by writing the new multiplier
1147  * value into /proc/profile.
1148  */
1149
1150 inline void smp_local_timer_interrupt(struct pt_regs * regs)
1151 {
1152         profile_tick(CPU_PROFILING, regs);
1153 #ifdef CONFIG_SMP
1154         update_process_times(user_mode_vm(regs));
1155 #endif
1156
1157         /*
1158          * We take the 'long' return path, and there every subsystem
1159          * grabs the apropriate locks (kernel lock/ irq lock).
1160          *
1161          * we might want to decouple profiling from the 'long path',
1162          * and do the profiling totally in assembly.
1163          *
1164          * Currently this isn't too much of an issue (performance wise),
1165          * we can take more than 100K local irqs per second on a 100 MHz P5.
1166          */
1167 }
1168
1169 /*
1170  * Local APIC timer interrupt. This is the most natural way for doing
1171  * local interrupts, but local timer interrupts can be emulated by
1172  * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1173  *
1174  * [ if a single-CPU system runs an SMP kernel then we call the local
1175  *   interrupt as well. Thus we cannot inline the local irq ... ]
1176  */
1177
1178 fastcall void smp_apic_timer_interrupt(struct pt_regs *regs)
1179 {
1180         int cpu = smp_processor_id();
1181
1182         /*
1183          * the NMI deadlock-detector uses this.
1184          */
1185         per_cpu(irq_stat, cpu).apic_timer_irqs++;
1186
1187         /*
1188          * NOTE! We'd better ACK the irq immediately,
1189          * because timer handling can be slow.
1190          */
1191         ack_APIC_irq();
1192         /*
1193          * update_process_times() expects us to have done irq_enter().
1194          * Besides, if we don't timer interrupts ignore the global
1195          * interrupt lock, which is the WrongThing (tm) to do.
1196          */
1197         irq_enter();
1198         smp_local_timer_interrupt(regs);
1199         irq_exit();
1200 }
1201
1202 #ifndef CONFIG_SMP
1203 static void up_apic_timer_interrupt_call(struct pt_regs *regs)
1204 {
1205         int cpu = smp_processor_id();
1206
1207         /*
1208          * the NMI deadlock-detector uses this.
1209          */
1210         per_cpu(irq_stat, cpu).apic_timer_irqs++;
1211
1212         smp_local_timer_interrupt(regs);
1213 }
1214 #endif
1215
1216 void smp_send_timer_broadcast_ipi(struct pt_regs *regs)
1217 {
1218         cpumask_t mask;
1219
1220         cpus_and(mask, cpu_online_map, timer_bcast_ipi);
1221         if (!cpus_empty(mask)) {
1222 #ifdef CONFIG_SMP
1223                 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
1224 #else
1225                 /*
1226                  * We can directly call the apic timer interrupt handler
1227                  * in UP case. Minus all irq related functions
1228                  */
1229                 up_apic_timer_interrupt_call(regs);
1230 #endif
1231         }
1232 }
1233
1234 int setup_profiling_timer(unsigned int multiplier)
1235 {
1236         return -EINVAL;
1237 }
1238
1239 /*
1240  * This interrupt should _never_ happen with our APIC/SMP architecture
1241  */
1242 fastcall void smp_spurious_interrupt(struct pt_regs *regs)
1243 {
1244         unsigned long v;
1245
1246         irq_enter();
1247         /*
1248          * Check if this really is a spurious interrupt and ACK it
1249          * if it is a vectored one.  Just in case...
1250          * Spurious interrupts should not be ACKed.
1251          */
1252         v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1253         if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1254                 ack_APIC_irq();
1255
1256         /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1257         printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1258                         smp_processor_id());
1259         irq_exit();
1260 }
1261
1262 /*
1263  * This interrupt should never happen with our APIC/SMP architecture
1264  */
1265
1266 fastcall void smp_error_interrupt(struct pt_regs *regs)
1267 {
1268         unsigned long v, v1;
1269
1270         irq_enter();
1271         /* First tickle the hardware, only then report what went on. -- REW */
1272         v = apic_read(APIC_ESR);
1273         apic_write(APIC_ESR, 0);
1274         v1 = apic_read(APIC_ESR);
1275         ack_APIC_irq();
1276         atomic_inc(&irq_err_count);
1277
1278         /* Here is what the APIC error bits mean:
1279            0: Send CS error
1280            1: Receive CS error
1281            2: Send accept error
1282            3: Receive accept error
1283            4: Reserved
1284            5: Send illegal vector
1285            6: Received illegal vector
1286            7: Illegal register address
1287         */
1288         printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1289                 smp_processor_id(), v , v1);
1290         irq_exit();
1291 }
1292
1293 /*
1294  * This initializes the IO-APIC and APIC hardware if this is
1295  * a UP kernel.
1296  */
1297 int __init APIC_init_uniprocessor (void)
1298 {
1299         if (enable_local_apic < 0)
1300                 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1301
1302         if (!smp_found_config && !cpu_has_apic)
1303                 return -1;
1304
1305         /*
1306          * Complain if the BIOS pretends there is one.
1307          */
1308         if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1309                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1310                         boot_cpu_physical_apicid);
1311                 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1312                 return -1;
1313         }
1314
1315         verify_local_APIC();
1316
1317         connect_bsp_APIC();
1318
1319         phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1320
1321         setup_local_APIC();
1322
1323 #ifdef CONFIG_X86_IO_APIC
1324         if (smp_found_config)
1325                 if (!skip_ioapic_setup && nr_ioapics)
1326                         setup_IO_APIC();
1327 #endif
1328         setup_boot_APIC_clock();
1329
1330         return 0;
1331 }