Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/agpgart
[linux-drm-fsl-dcu.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/init.h>
18
19 #include <linux/mm.h>
20 #include <linux/delay.h>
21 #include <linux/bootmem.h>
22 #include <linux/smp_lock.h>
23 #include <linux/interrupt.h>
24 #include <linux/mc146818rtc.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/sysdev.h>
27 #include <linux/cpu.h>
28 #include <linux/clockchips.h>
29 #include <linux/acpi_pmtmr.h>
30 #include <linux/module.h>
31
32 #include <asm/atomic.h>
33 #include <asm/smp.h>
34 #include <asm/mtrr.h>
35 #include <asm/mpspec.h>
36 #include <asm/desc.h>
37 #include <asm/arch_hooks.h>
38 #include <asm/hpet.h>
39 #include <asm/i8253.h>
40 #include <asm/nmi.h>
41 #include <asm/idle.h>
42
43 #include <mach_apic.h>
44 #include <mach_apicdef.h>
45 #include <mach_ipi.h>
46
47 #include "io_ports.h"
48
49 /*
50  * Sanity check
51  */
52 #if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
53 # error SPURIOUS_APIC_VECTOR definition error
54 #endif
55
56 /*
57  * Knob to control our willingness to enable the local APIC.
58  *
59  * -1=force-disable, +1=force-enable
60  */
61 static int enable_local_apic __initdata = 0;
62
63 /* Local APIC timer verification ok */
64 static int local_apic_timer_verify_ok;
65
66 /*
67  * Debug level, exported for io_apic.c
68  */
69 int apic_verbosity;
70
71 static unsigned int calibration_result;
72
73 static int lapic_next_event(unsigned long delta,
74                             struct clock_event_device *evt);
75 static void lapic_timer_setup(enum clock_event_mode mode,
76                               struct clock_event_device *evt);
77 static void lapic_timer_broadcast(cpumask_t mask);
78 static void apic_pm_activate(void);
79
80 /*
81  * The local apic timer can be used for any function which is CPU local.
82  */
83 static struct clock_event_device lapic_clockevent = {
84         .name           = "lapic",
85         .features       = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
86                         | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
87         .shift          = 32,
88         .set_mode       = lapic_timer_setup,
89         .set_next_event = lapic_next_event,
90         .broadcast      = lapic_timer_broadcast,
91         .rating         = 100,
92         .irq            = -1,
93 };
94 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
95
96 /* Local APIC was disabled by the BIOS and enabled by the kernel */
97 static int enabled_via_apicbase;
98
99 /*
100  * Get the LAPIC version
101  */
102 static inline int lapic_get_version(void)
103 {
104         return GET_APIC_VERSION(apic_read(APIC_LVR));
105 }
106
107 /*
108  * Check, if the APIC is integrated or a seperate chip
109  */
110 static inline int lapic_is_integrated(void)
111 {
112         return APIC_INTEGRATED(lapic_get_version());
113 }
114
115 /*
116  * Check, whether this is a modern or a first generation APIC
117  */
118 static int modern_apic(void)
119 {
120         /* AMD systems use old APIC versions, so check the CPU */
121         if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
122             boot_cpu_data.x86 >= 0xf)
123                 return 1;
124         return lapic_get_version() >= 0x14;
125 }
126
127 /**
128  * enable_NMI_through_LVT0 - enable NMI through local vector table 0
129  */
130 void enable_NMI_through_LVT0 (void * dummy)
131 {
132         unsigned int v = APIC_DM_NMI;
133
134         /* Level triggered for 82489DX */
135         if (!lapic_is_integrated())
136                 v |= APIC_LVT_LEVEL_TRIGGER;
137         apic_write_around(APIC_LVT0, v);
138 }
139
140 /**
141  * get_physical_broadcast - Get number of physical broadcast IDs
142  */
143 int get_physical_broadcast(void)
144 {
145         return modern_apic() ? 0xff : 0xf;
146 }
147
148 /**
149  * lapic_get_maxlvt - get the maximum number of local vector table entries
150  */
151 int lapic_get_maxlvt(void)
152 {
153         unsigned int v = apic_read(APIC_LVR);
154
155         /* 82489DXs do not report # of LVT entries. */
156         return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
157 }
158
159 /*
160  * Local APIC timer
161  */
162
163 /* Clock divisor is set to 16 */
164 #define APIC_DIVISOR 16
165
166 /*
167  * This function sets up the local APIC timer, with a timeout of
168  * 'clocks' APIC bus clock. During calibration we actually call
169  * this function twice on the boot CPU, once with a bogus timeout
170  * value, second time for real. The other (noncalibrating) CPUs
171  * call this function only once, with the real, calibrated value.
172  *
173  * We do reads before writes even if unnecessary, to get around the
174  * P5 APIC double write bug.
175  */
176 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
177 {
178         unsigned int lvtt_value, tmp_value;
179
180         lvtt_value = LOCAL_TIMER_VECTOR;
181         if (!oneshot)
182                 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
183         if (!lapic_is_integrated())
184                 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
185
186         if (!irqen)
187                 lvtt_value |= APIC_LVT_MASKED;
188
189         apic_write_around(APIC_LVTT, lvtt_value);
190
191         /*
192          * Divide PICLK by 16
193          */
194         tmp_value = apic_read(APIC_TDCR);
195         apic_write_around(APIC_TDCR, (tmp_value
196                                 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
197                                 | APIC_TDR_DIV_16);
198
199         if (!oneshot)
200                 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
201 }
202
203 /*
204  * Program the next event, relative to now
205  */
206 static int lapic_next_event(unsigned long delta,
207                             struct clock_event_device *evt)
208 {
209         apic_write_around(APIC_TMICT, delta);
210         return 0;
211 }
212
213 /*
214  * Setup the lapic timer in periodic or oneshot mode
215  */
216 static void lapic_timer_setup(enum clock_event_mode mode,
217                               struct clock_event_device *evt)
218 {
219         unsigned long flags;
220         unsigned int v;
221
222         /* Lapic used for broadcast ? */
223         if (!local_apic_timer_verify_ok)
224                 return;
225
226         local_irq_save(flags);
227
228         switch (mode) {
229         case CLOCK_EVT_MODE_PERIODIC:
230         case CLOCK_EVT_MODE_ONESHOT:
231                 __setup_APIC_LVTT(calibration_result,
232                                   mode != CLOCK_EVT_MODE_PERIODIC, 1);
233                 break;
234         case CLOCK_EVT_MODE_UNUSED:
235         case CLOCK_EVT_MODE_SHUTDOWN:
236                 v = apic_read(APIC_LVTT);
237                 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
238                 apic_write_around(APIC_LVTT, v);
239                 break;
240         }
241
242         local_irq_restore(flags);
243 }
244
245 /*
246  * Local APIC timer broadcast function
247  */
248 static void lapic_timer_broadcast(cpumask_t mask)
249 {
250 #ifdef CONFIG_SMP
251         send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
252 #endif
253 }
254
255 /*
256  * Setup the local APIC timer for this CPU. Copy the initilized values
257  * of the boot CPU and register the clock event in the framework.
258  */
259 static void __devinit setup_APIC_timer(void)
260 {
261         struct clock_event_device *levt = &__get_cpu_var(lapic_events);
262
263         memcpy(levt, &lapic_clockevent, sizeof(*levt));
264         levt->cpumask = cpumask_of_cpu(smp_processor_id());
265
266         clockevents_register_device(levt);
267 }
268
269 /*
270  * In this functions we calibrate APIC bus clocks to the external timer.
271  *
272  * We want to do the calibration only once since we want to have local timer
273  * irqs syncron. CPUs connected by the same APIC bus have the very same bus
274  * frequency.
275  *
276  * This was previously done by reading the PIT/HPET and waiting for a wrap
277  * around to find out, that a tick has elapsed. I have a box, where the PIT
278  * readout is broken, so it never gets out of the wait loop again. This was
279  * also reported by others.
280  *
281  * Monitoring the jiffies value is inaccurate and the clockevents
282  * infrastructure allows us to do a simple substitution of the interrupt
283  * handler.
284  *
285  * The calibration routine also uses the pm_timer when possible, as the PIT
286  * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
287  * back to normal later in the boot process).
288  */
289
290 #define LAPIC_CAL_LOOPS         (HZ/10)
291
292 static __initdata volatile int lapic_cal_loops = -1;
293 static __initdata long lapic_cal_t1, lapic_cal_t2;
294 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
295 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
296 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
297
298 /*
299  * Temporary interrupt handler.
300  */
301 static void __init lapic_cal_handler(struct clock_event_device *dev)
302 {
303         unsigned long long tsc = 0;
304         long tapic = apic_read(APIC_TMCCT);
305         unsigned long pm = acpi_pm_read_early();
306
307         if (cpu_has_tsc)
308                 rdtscll(tsc);
309
310         switch (lapic_cal_loops++) {
311         case 0:
312                 lapic_cal_t1 = tapic;
313                 lapic_cal_tsc1 = tsc;
314                 lapic_cal_pm1 = pm;
315                 lapic_cal_j1 = jiffies;
316                 break;
317
318         case LAPIC_CAL_LOOPS:
319                 lapic_cal_t2 = tapic;
320                 lapic_cal_tsc2 = tsc;
321                 if (pm < lapic_cal_pm1)
322                         pm += ACPI_PM_OVRRUN;
323                 lapic_cal_pm2 = pm;
324                 lapic_cal_j2 = jiffies;
325                 break;
326         }
327 }
328
329 /*
330  * Setup the boot APIC
331  *
332  * Calibrate and verify the result.
333  */
334 void __init setup_boot_APIC_clock(void)
335 {
336         struct clock_event_device *levt = &__get_cpu_var(lapic_events);
337         const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
338         const long pm_thresh = pm_100ms/100;
339         void (*real_handler)(struct clock_event_device *dev);
340         unsigned long deltaj;
341         long delta, deltapm;
342
343         apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
344                     "calibrating APIC timer ...\n");
345
346         local_irq_disable();
347
348         /* Replace the global interrupt handler */
349         real_handler = global_clock_event->event_handler;
350         global_clock_event->event_handler = lapic_cal_handler;
351
352         /*
353          * Setup the APIC counter to 1e9. There is no way the lapic
354          * can underflow in the 100ms detection time frame
355          */
356         __setup_APIC_LVTT(1000000000, 0, 0);
357
358         /* Let the interrupts run */
359         local_irq_enable();
360
361         while(lapic_cal_loops <= LAPIC_CAL_LOOPS);
362
363         local_irq_disable();
364
365         /* Restore the real event handler */
366         global_clock_event->event_handler = real_handler;
367
368         /* Build delta t1-t2 as apic timer counts down */
369         delta = lapic_cal_t1 - lapic_cal_t2;
370         apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
371
372         /* Check, if the PM timer is available */
373         deltapm = lapic_cal_pm2 - lapic_cal_pm1;
374         apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
375
376         if (deltapm) {
377                 unsigned long mult;
378                 u64 res;
379
380                 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
381
382                 if (deltapm > (pm_100ms - pm_thresh) &&
383                     deltapm < (pm_100ms + pm_thresh)) {
384                         apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
385                 } else {
386                         res = (((u64) deltapm) *  mult) >> 22;
387                         do_div(res, 1000000);
388                         printk(KERN_WARNING "APIC calibration not consistent "
389                                "with PM Timer: %ldms instead of 100ms\n",
390                                (long)res);
391                         /* Correct the lapic counter value */
392                         res = (((u64) delta ) * pm_100ms);
393                         do_div(res, deltapm);
394                         printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
395                                "%lu (%ld)\n", (unsigned long) res, delta);
396                         delta = (long) res;
397                 }
398         }
399
400         /* Calculate the scaled math multiplication factor */
401         lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
402         lapic_clockevent.max_delta_ns =
403                 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
404         lapic_clockevent.min_delta_ns =
405                 clockevent_delta2ns(0xF, &lapic_clockevent);
406
407         calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
408
409         apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
410         apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
411         apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
412                     calibration_result);
413
414         if (cpu_has_tsc) {
415                 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
416                 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
417                             "%ld.%04ld MHz.\n",
418                             (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
419                             (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
420         }
421
422         apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
423                     "%u.%04u MHz.\n",
424                     calibration_result / (1000000 / HZ),
425                     calibration_result % (1000000 / HZ));
426
427
428         apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
429
430         /*
431          * Setup the apic timer manually
432          */
433         local_apic_timer_verify_ok = 1;
434         levt->event_handler = lapic_cal_handler;
435         lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
436         lapic_cal_loops = -1;
437
438         /* Let the interrupts run */
439         local_irq_enable();
440
441         while(lapic_cal_loops <= LAPIC_CAL_LOOPS);
442
443         local_irq_disable();
444
445         /* Stop the lapic timer */
446         lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
447
448         local_irq_enable();
449
450         /* Jiffies delta */
451         deltaj = lapic_cal_j2 - lapic_cal_j1;
452         apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
453
454         /* Check, if the PM timer is available */
455         deltapm = lapic_cal_pm2 - lapic_cal_pm1;
456         apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
457
458         local_apic_timer_verify_ok = 0;
459
460         if (deltapm) {
461                 if (deltapm > (pm_100ms - pm_thresh) &&
462                     deltapm < (pm_100ms + pm_thresh)) {
463                         apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
464                         /* Check, if the jiffies result is consistent */
465                         if (deltaj < LAPIC_CAL_LOOPS-2 ||
466                             deltaj > LAPIC_CAL_LOOPS+2) {
467                                 /*
468                                  * Not sure, what we can do about this one.
469                                  * When high resultion timers are active
470                                  * and the lapic timer does not stop in C3
471                                  * we are fine. Otherwise more trouble might
472                                  * be waiting. -- tglx
473                                  */
474                                 printk(KERN_WARNING "Global event device %s "
475                                        "has wrong frequency "
476                                        "(%lu ticks instead of %d)\n",
477                                        global_clock_event->name, deltaj,
478                                        LAPIC_CAL_LOOPS);
479                         }
480                         local_apic_timer_verify_ok = 1;
481                 }
482         } else {
483                 /* Check, if the jiffies result is consistent */
484                 if (deltaj >= LAPIC_CAL_LOOPS-2 &&
485                     deltaj <= LAPIC_CAL_LOOPS+2) {
486                         apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
487                         local_apic_timer_verify_ok = 1;
488                 }
489         }
490
491         if (!local_apic_timer_verify_ok) {
492                 printk(KERN_WARNING
493                        "APIC timer disabled due to verification failure.\n");
494                 /* No broadcast on UP ! */
495                 if (num_possible_cpus() == 1)
496                         return;
497         } else
498                 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
499
500         /* Setup the lapic or request the broadcast */
501         setup_APIC_timer();
502 }
503
504 void __devinit setup_secondary_APIC_clock(void)
505 {
506         setup_APIC_timer();
507 }
508
509 /*
510  * The guts of the apic timer interrupt
511  */
512 static void local_apic_timer_interrupt(void)
513 {
514         int cpu = smp_processor_id();
515         struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
516
517         /*
518          * Normally we should not be here till LAPIC has been initialized but
519          * in some cases like kdump, its possible that there is a pending LAPIC
520          * timer interrupt from previous kernel's context and is delivered in
521          * new kernel the moment interrupts are enabled.
522          *
523          * Interrupts are enabled early and LAPIC is setup much later, hence
524          * its possible that when we get here evt->event_handler is NULL.
525          * Check for event_handler being NULL and discard the interrupt as
526          * spurious.
527          */
528         if (!evt->event_handler) {
529                 printk(KERN_WARNING
530                        "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
531                 /* Switch it off */
532                 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
533                 return;
534         }
535
536         per_cpu(irq_stat, cpu).apic_timer_irqs++;
537
538         evt->event_handler(evt);
539 }
540
541 /*
542  * Local APIC timer interrupt. This is the most natural way for doing
543  * local interrupts, but local timer interrupts can be emulated by
544  * broadcast interrupts too. [in case the hw doesn't support APIC timers]
545  *
546  * [ if a single-CPU system runs an SMP kernel then we call the local
547  *   interrupt as well. Thus we cannot inline the local irq ... ]
548  */
549
550 void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
551 {
552         struct pt_regs *old_regs = set_irq_regs(regs);
553
554         /*
555          * NOTE! We'd better ACK the irq immediately,
556          * because timer handling can be slow.
557          */
558         ack_APIC_irq();
559         /*
560          * update_process_times() expects us to have done irq_enter().
561          * Besides, if we don't timer interrupts ignore the global
562          * interrupt lock, which is the WrongThing (tm) to do.
563          */
564         exit_idle();
565         irq_enter();
566         local_apic_timer_interrupt();
567         irq_exit();
568
569         set_irq_regs(old_regs);
570 }
571
572 int setup_profiling_timer(unsigned int multiplier)
573 {
574         return -EINVAL;
575 }
576
577 /*
578  * Local APIC start and shutdown
579  */
580
581 /**
582  * clear_local_APIC - shutdown the local APIC
583  *
584  * This is called, when a CPU is disabled and before rebooting, so the state of
585  * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
586  * leftovers during boot.
587  */
588 void clear_local_APIC(void)
589 {
590         int maxlvt = lapic_get_maxlvt();
591         unsigned long v;
592
593         /*
594          * Masking an LVT entry can trigger a local APIC error
595          * if the vector is zero. Mask LVTERR first to prevent this.
596          */
597         if (maxlvt >= 3) {
598                 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
599                 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
600         }
601         /*
602          * Careful: we have to set masks only first to deassert
603          * any level-triggered sources.
604          */
605         v = apic_read(APIC_LVTT);
606         apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
607         v = apic_read(APIC_LVT0);
608         apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
609         v = apic_read(APIC_LVT1);
610         apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
611         if (maxlvt >= 4) {
612                 v = apic_read(APIC_LVTPC);
613                 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
614         }
615
616         /* lets not touch this if we didn't frob it */
617 #ifdef CONFIG_X86_MCE_P4THERMAL
618         if (maxlvt >= 5) {
619                 v = apic_read(APIC_LVTTHMR);
620                 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
621         }
622 #endif
623         /*
624          * Clean APIC state for other OSs:
625          */
626         apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
627         apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
628         apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
629         if (maxlvt >= 3)
630                 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
631         if (maxlvt >= 4)
632                 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
633
634 #ifdef CONFIG_X86_MCE_P4THERMAL
635         if (maxlvt >= 5)
636                 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
637 #endif
638         /* Integrated APIC (!82489DX) ? */
639         if (lapic_is_integrated()) {
640                 if (maxlvt > 3)
641                         /* Clear ESR due to Pentium errata 3AP and 11AP */
642                         apic_write(APIC_ESR, 0);
643                 apic_read(APIC_ESR);
644         }
645 }
646
647 /**
648  * disable_local_APIC - clear and disable the local APIC
649  */
650 void disable_local_APIC(void)
651 {
652         unsigned long value;
653
654         clear_local_APIC();
655
656         /*
657          * Disable APIC (implies clearing of registers
658          * for 82489DX!).
659          */
660         value = apic_read(APIC_SPIV);
661         value &= ~APIC_SPIV_APIC_ENABLED;
662         apic_write_around(APIC_SPIV, value);
663
664         /*
665          * When LAPIC was disabled by the BIOS and enabled by the kernel,
666          * restore the disabled state.
667          */
668         if (enabled_via_apicbase) {
669                 unsigned int l, h;
670
671                 rdmsr(MSR_IA32_APICBASE, l, h);
672                 l &= ~MSR_IA32_APICBASE_ENABLE;
673                 wrmsr(MSR_IA32_APICBASE, l, h);
674         }
675 }
676
677 /*
678  * If Linux enabled the LAPIC against the BIOS default disable it down before
679  * re-entering the BIOS on shutdown.  Otherwise the BIOS may get confused and
680  * not power-off.  Additionally clear all LVT entries before disable_local_APIC
681  * for the case where Linux didn't enable the LAPIC.
682  */
683 void lapic_shutdown(void)
684 {
685         unsigned long flags;
686
687         if (!cpu_has_apic)
688                 return;
689
690         local_irq_save(flags);
691         clear_local_APIC();
692
693         if (enabled_via_apicbase)
694                 disable_local_APIC();
695
696         local_irq_restore(flags);
697 }
698
699 /*
700  * This is to verify that we're looking at a real local APIC.
701  * Check these against your board if the CPUs aren't getting
702  * started for no apparent reason.
703  */
704 int __init verify_local_APIC(void)
705 {
706         unsigned int reg0, reg1;
707
708         /*
709          * The version register is read-only in a real APIC.
710          */
711         reg0 = apic_read(APIC_LVR);
712         apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
713         apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
714         reg1 = apic_read(APIC_LVR);
715         apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
716
717         /*
718          * The two version reads above should print the same
719          * numbers.  If the second one is different, then we
720          * poke at a non-APIC.
721          */
722         if (reg1 != reg0)
723                 return 0;
724
725         /*
726          * Check if the version looks reasonably.
727          */
728         reg1 = GET_APIC_VERSION(reg0);
729         if (reg1 == 0x00 || reg1 == 0xff)
730                 return 0;
731         reg1 = lapic_get_maxlvt();
732         if (reg1 < 0x02 || reg1 == 0xff)
733                 return 0;
734
735         /*
736          * The ID register is read/write in a real APIC.
737          */
738         reg0 = apic_read(APIC_ID);
739         apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
740
741         /*
742          * The next two are just to see if we have sane values.
743          * They're only really relevant if we're in Virtual Wire
744          * compatibility mode, but most boxes are anymore.
745          */
746         reg0 = apic_read(APIC_LVT0);
747         apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
748         reg1 = apic_read(APIC_LVT1);
749         apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
750
751         return 1;
752 }
753
754 /**
755  * sync_Arb_IDs - synchronize APIC bus arbitration IDs
756  */
757 void __init sync_Arb_IDs(void)
758 {
759         /*
760          * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
761          * needed on AMD.
762          */
763         if (modern_apic())
764                 return;
765         /*
766          * Wait for idle.
767          */
768         apic_wait_icr_idle();
769
770         apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
771         apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
772                                 | APIC_DM_INIT);
773 }
774
775 /*
776  * An initial setup of the virtual wire mode.
777  */
778 void __init init_bsp_APIC(void)
779 {
780         unsigned long value;
781
782         /*
783          * Don't do the setup now if we have a SMP BIOS as the
784          * through-I/O-APIC virtual wire mode might be active.
785          */
786         if (smp_found_config || !cpu_has_apic)
787                 return;
788
789         /*
790          * Do not trust the local APIC being empty at bootup.
791          */
792         clear_local_APIC();
793
794         /*
795          * Enable APIC.
796          */
797         value = apic_read(APIC_SPIV);
798         value &= ~APIC_VECTOR_MASK;
799         value |= APIC_SPIV_APIC_ENABLED;
800
801         /* This bit is reserved on P4/Xeon and should be cleared */
802         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
803             (boot_cpu_data.x86 == 15))
804                 value &= ~APIC_SPIV_FOCUS_DISABLED;
805         else
806                 value |= APIC_SPIV_FOCUS_DISABLED;
807         value |= SPURIOUS_APIC_VECTOR;
808         apic_write_around(APIC_SPIV, value);
809
810         /*
811          * Set up the virtual wire mode.
812          */
813         apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
814         value = APIC_DM_NMI;
815         if (!lapic_is_integrated())             /* 82489DX */
816                 value |= APIC_LVT_LEVEL_TRIGGER;
817         apic_write_around(APIC_LVT1, value);
818 }
819
820 /**
821  * setup_local_APIC - setup the local APIC
822  */
823 void __devinit setup_local_APIC(void)
824 {
825         unsigned long oldvalue, value, maxlvt, integrated;
826         int i, j;
827
828         /* Pound the ESR really hard over the head with a big hammer - mbligh */
829         if (esr_disable) {
830                 apic_write(APIC_ESR, 0);
831                 apic_write(APIC_ESR, 0);
832                 apic_write(APIC_ESR, 0);
833                 apic_write(APIC_ESR, 0);
834         }
835
836         integrated = lapic_is_integrated();
837
838         /*
839          * Double-check whether this APIC is really registered.
840          */
841         if (!apic_id_registered())
842                 BUG();
843
844         /*
845          * Intel recommends to set DFR, LDR and TPR before enabling
846          * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
847          * document number 292116).  So here it goes...
848          */
849         init_apic_ldr();
850
851         /*
852          * Set Task Priority to 'accept all'. We never change this
853          * later on.
854          */
855         value = apic_read(APIC_TASKPRI);
856         value &= ~APIC_TPRI_MASK;
857         apic_write_around(APIC_TASKPRI, value);
858
859         /*
860          * After a crash, we no longer service the interrupts and a pending
861          * interrupt from previous kernel might still have ISR bit set.
862          *
863          * Most probably by now CPU has serviced that pending interrupt and
864          * it might not have done the ack_APIC_irq() because it thought,
865          * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
866          * does not clear the ISR bit and cpu thinks it has already serivced
867          * the interrupt. Hence a vector might get locked. It was noticed
868          * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
869          */
870         for (i = APIC_ISR_NR - 1; i >= 0; i--) {
871                 value = apic_read(APIC_ISR + i*0x10);
872                 for (j = 31; j >= 0; j--) {
873                         if (value & (1<<j))
874                                 ack_APIC_irq();
875                 }
876         }
877
878         /*
879          * Now that we are all set up, enable the APIC
880          */
881         value = apic_read(APIC_SPIV);
882         value &= ~APIC_VECTOR_MASK;
883         /*
884          * Enable APIC
885          */
886         value |= APIC_SPIV_APIC_ENABLED;
887
888         /*
889          * Some unknown Intel IO/APIC (or APIC) errata is biting us with
890          * certain networking cards. If high frequency interrupts are
891          * happening on a particular IOAPIC pin, plus the IOAPIC routing
892          * entry is masked/unmasked at a high rate as well then sooner or
893          * later IOAPIC line gets 'stuck', no more interrupts are received
894          * from the device. If focus CPU is disabled then the hang goes
895          * away, oh well :-(
896          *
897          * [ This bug can be reproduced easily with a level-triggered
898          *   PCI Ne2000 networking cards and PII/PIII processors, dual
899          *   BX chipset. ]
900          */
901         /*
902          * Actually disabling the focus CPU check just makes the hang less
903          * frequent as it makes the interrupt distributon model be more
904          * like LRU than MRU (the short-term load is more even across CPUs).
905          * See also the comment in end_level_ioapic_irq().  --macro
906          */
907
908         /* Enable focus processor (bit==0) */
909         value &= ~APIC_SPIV_FOCUS_DISABLED;
910
911         /*
912          * Set spurious IRQ vector
913          */
914         value |= SPURIOUS_APIC_VECTOR;
915         apic_write_around(APIC_SPIV, value);
916
917         /*
918          * Set up LVT0, LVT1:
919          *
920          * set up through-local-APIC on the BP's LINT0. This is not
921          * strictly necessery in pure symmetric-IO mode, but sometimes
922          * we delegate interrupts to the 8259A.
923          */
924         /*
925          * TODO: set up through-local-APIC from through-I/O-APIC? --macro
926          */
927         value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
928         if (!smp_processor_id() && (pic_mode || !value)) {
929                 value = APIC_DM_EXTINT;
930                 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
931                                 smp_processor_id());
932         } else {
933                 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
934                 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
935                                 smp_processor_id());
936         }
937         apic_write_around(APIC_LVT0, value);
938
939         /*
940          * only the BP should see the LINT1 NMI signal, obviously.
941          */
942         if (!smp_processor_id())
943                 value = APIC_DM_NMI;
944         else
945                 value = APIC_DM_NMI | APIC_LVT_MASKED;
946         if (!integrated)                /* 82489DX */
947                 value |= APIC_LVT_LEVEL_TRIGGER;
948         apic_write_around(APIC_LVT1, value);
949
950         if (integrated && !esr_disable) {               /* !82489DX */
951                 maxlvt = lapic_get_maxlvt();
952                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
953                         apic_write(APIC_ESR, 0);
954                 oldvalue = apic_read(APIC_ESR);
955
956                 /* enables sending errors */
957                 value = ERROR_APIC_VECTOR;
958                 apic_write_around(APIC_LVTERR, value);
959                 /*
960                  * spec says clear errors after enabling vector.
961                  */
962                 if (maxlvt > 3)
963                         apic_write(APIC_ESR, 0);
964                 value = apic_read(APIC_ESR);
965                 if (value != oldvalue)
966                         apic_printk(APIC_VERBOSE, "ESR value before enabling "
967                                 "vector: 0x%08lx  after: 0x%08lx\n",
968                                 oldvalue, value);
969         } else {
970                 if (esr_disable)
971                         /*
972                          * Something untraceble is creating bad interrupts on
973                          * secondary quads ... for the moment, just leave the
974                          * ESR disabled - we can't do anything useful with the
975                          * errors anyway - mbligh
976                          */
977                         printk(KERN_INFO "Leaving ESR disabled.\n");
978                 else
979                         printk(KERN_INFO "No ESR for 82489DX.\n");
980         }
981
982         /* Disable the local apic timer */
983         value = apic_read(APIC_LVTT);
984         value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
985         apic_write_around(APIC_LVTT, value);
986
987         setup_apic_nmi_watchdog(NULL);
988         apic_pm_activate();
989 }
990
991 /*
992  * Detect and initialize APIC
993  */
994 static int __init detect_init_APIC (void)
995 {
996         u32 h, l, features;
997
998         /* Disabled by kernel option? */
999         if (enable_local_apic < 0)
1000                 return -1;
1001
1002         switch (boot_cpu_data.x86_vendor) {
1003         case X86_VENDOR_AMD:
1004                 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1005                     (boot_cpu_data.x86 == 15))
1006                         break;
1007                 goto no_apic;
1008         case X86_VENDOR_INTEL:
1009                 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1010                     (boot_cpu_data.x86 == 5 && cpu_has_apic))
1011                         break;
1012                 goto no_apic;
1013         default:
1014                 goto no_apic;
1015         }
1016
1017         if (!cpu_has_apic) {
1018                 /*
1019                  * Over-ride BIOS and try to enable the local APIC only if
1020                  * "lapic" specified.
1021                  */
1022                 if (enable_local_apic <= 0) {
1023                         printk(KERN_INFO "Local APIC disabled by BIOS -- "
1024                                "you can enable it with \"lapic\"\n");
1025                         return -1;
1026                 }
1027                 /*
1028                  * Some BIOSes disable the local APIC in the APIC_BASE
1029                  * MSR. This can only be done in software for Intel P6 or later
1030                  * and AMD K7 (Model > 1) or later.
1031                  */
1032                 rdmsr(MSR_IA32_APICBASE, l, h);
1033                 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1034                         printk(KERN_INFO
1035                                "Local APIC disabled by BIOS -- reenabling.\n");
1036                         l &= ~MSR_IA32_APICBASE_BASE;
1037                         l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1038                         wrmsr(MSR_IA32_APICBASE, l, h);
1039                         enabled_via_apicbase = 1;
1040                 }
1041         }
1042         /*
1043          * The APIC feature bit should now be enabled
1044          * in `cpuid'
1045          */
1046         features = cpuid_edx(1);
1047         if (!(features & (1 << X86_FEATURE_APIC))) {
1048                 printk(KERN_WARNING "Could not enable APIC!\n");
1049                 return -1;
1050         }
1051         set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1052         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1053
1054         /* The BIOS may have set up the APIC at some other address */
1055         rdmsr(MSR_IA32_APICBASE, l, h);
1056         if (l & MSR_IA32_APICBASE_ENABLE)
1057                 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1058
1059         if (nmi_watchdog != NMI_NONE)
1060                 nmi_watchdog = NMI_LOCAL_APIC;
1061
1062         printk(KERN_INFO "Found and enabled local APIC!\n");
1063
1064         apic_pm_activate();
1065
1066         return 0;
1067
1068 no_apic:
1069         printk(KERN_INFO "No local APIC present or hardware disabled\n");
1070         return -1;
1071 }
1072
1073 /**
1074  * init_apic_mappings - initialize APIC mappings
1075  */
1076 void __init init_apic_mappings(void)
1077 {
1078         unsigned long apic_phys;
1079
1080         /*
1081          * If no local APIC can be found then set up a fake all
1082          * zeroes page to simulate the local APIC and another
1083          * one for the IO-APIC.
1084          */
1085         if (!smp_found_config && detect_init_APIC()) {
1086                 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1087                 apic_phys = __pa(apic_phys);
1088         } else
1089                 apic_phys = mp_lapic_addr;
1090
1091         set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1092         printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1093                apic_phys);
1094
1095         /*
1096          * Fetch the APIC ID of the BSP in case we have a
1097          * default configuration (or the MP table is broken).
1098          */
1099         if (boot_cpu_physical_apicid == -1U)
1100                 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1101
1102 #ifdef CONFIG_X86_IO_APIC
1103         {
1104                 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1105                 int i;
1106
1107                 for (i = 0; i < nr_ioapics; i++) {
1108                         if (smp_found_config) {
1109                                 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1110                                 if (!ioapic_phys) {
1111                                         printk(KERN_ERR
1112                                                "WARNING: bogus zero IO-APIC "
1113                                                "address found in MPTABLE, "
1114                                                "disabling IO/APIC support!\n");
1115                                         smp_found_config = 0;
1116                                         skip_ioapic_setup = 1;
1117                                         goto fake_ioapic_page;
1118                                 }
1119                         } else {
1120 fake_ioapic_page:
1121                                 ioapic_phys = (unsigned long)
1122                                               alloc_bootmem_pages(PAGE_SIZE);
1123                                 ioapic_phys = __pa(ioapic_phys);
1124                         }
1125                         set_fixmap_nocache(idx, ioapic_phys);
1126                         printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1127                                __fix_to_virt(idx), ioapic_phys);
1128                         idx++;
1129                 }
1130         }
1131 #endif
1132 }
1133
1134 /*
1135  * This initializes the IO-APIC and APIC hardware if this is
1136  * a UP kernel.
1137  */
1138 int __init APIC_init_uniprocessor (void)
1139 {
1140         if (enable_local_apic < 0)
1141                 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1142
1143         if (!smp_found_config && !cpu_has_apic)
1144                 return -1;
1145
1146         /*
1147          * Complain if the BIOS pretends there is one.
1148          */
1149         if (!cpu_has_apic &&
1150             APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1151                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1152                        boot_cpu_physical_apicid);
1153                 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1154                 return -1;
1155         }
1156
1157         verify_local_APIC();
1158
1159         connect_bsp_APIC();
1160
1161         /*
1162          * Hack: In case of kdump, after a crash, kernel might be booting
1163          * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1164          * might be zero if read from MP tables. Get it from LAPIC.
1165          */
1166 #ifdef CONFIG_CRASH_DUMP
1167         boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1168 #endif
1169         phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1170
1171         setup_local_APIC();
1172
1173 #ifdef CONFIG_X86_IO_APIC
1174         if (smp_found_config)
1175                 if (!skip_ioapic_setup && nr_ioapics)
1176                         setup_IO_APIC();
1177 #endif
1178         setup_boot_clock();
1179
1180         return 0;
1181 }
1182
1183 /*
1184  * APIC command line parameters
1185  */
1186 static int __init parse_lapic(char *arg)
1187 {
1188         enable_local_apic = 1;
1189         return 0;
1190 }
1191 early_param("lapic", parse_lapic);
1192
1193 static int __init parse_nolapic(char *arg)
1194 {
1195         enable_local_apic = -1;
1196         clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1197         return 0;
1198 }
1199 early_param("nolapic", parse_nolapic);
1200
1201 static int __init apic_set_verbosity(char *str)
1202 {
1203         if (strcmp("debug", str) == 0)
1204                 apic_verbosity = APIC_DEBUG;
1205         else if (strcmp("verbose", str) == 0)
1206                 apic_verbosity = APIC_VERBOSE;
1207         return 1;
1208 }
1209
1210 __setup("apic=", apic_set_verbosity);
1211
1212
1213 /*
1214  * Local APIC interrupts
1215  */
1216
1217 /*
1218  * This interrupt should _never_ happen with our APIC/SMP architecture
1219  */
1220 void smp_spurious_interrupt(struct pt_regs *regs)
1221 {
1222         unsigned long v;
1223
1224         exit_idle();
1225         irq_enter();
1226         /*
1227          * Check if this really is a spurious interrupt and ACK it
1228          * if it is a vectored one.  Just in case...
1229          * Spurious interrupts should not be ACKed.
1230          */
1231         v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1232         if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1233                 ack_APIC_irq();
1234
1235         /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1236         printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1237                "should never happen.\n", smp_processor_id());
1238         irq_exit();
1239 }
1240
1241 /*
1242  * This interrupt should never happen with our APIC/SMP architecture
1243  */
1244 void smp_error_interrupt(struct pt_regs *regs)
1245 {
1246         unsigned long v, v1;
1247
1248         exit_idle();
1249         irq_enter();
1250         /* First tickle the hardware, only then report what went on. -- REW */
1251         v = apic_read(APIC_ESR);
1252         apic_write(APIC_ESR, 0);
1253         v1 = apic_read(APIC_ESR);
1254         ack_APIC_irq();
1255         atomic_inc(&irq_err_count);
1256
1257         /* Here is what the APIC error bits mean:
1258            0: Send CS error
1259            1: Receive CS error
1260            2: Send accept error
1261            3: Receive accept error
1262            4: Reserved
1263            5: Send illegal vector
1264            6: Received illegal vector
1265            7: Illegal register address
1266         */
1267         printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1268                 smp_processor_id(), v , v1);
1269         irq_exit();
1270 }
1271
1272 /*
1273  * Initialize APIC interrupts
1274  */
1275 void __init apic_intr_init(void)
1276 {
1277 #ifdef CONFIG_SMP
1278         smp_intr_init();
1279 #endif
1280         /* self generated IPI for local APIC timer */
1281         set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1282
1283         /* IPI vectors for APIC spurious and error interrupts */
1284         set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1285         set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1286
1287         /* thermal monitor LVT interrupt */
1288 #ifdef CONFIG_X86_MCE_P4THERMAL
1289         set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1290 #endif
1291 }
1292
1293 /**
1294  * connect_bsp_APIC - attach the APIC to the interrupt system
1295  */
1296 void __init connect_bsp_APIC(void)
1297 {
1298         if (pic_mode) {
1299                 /*
1300                  * Do not trust the local APIC being empty at bootup.
1301                  */
1302                 clear_local_APIC();
1303                 /*
1304                  * PIC mode, enable APIC mode in the IMCR, i.e.  connect BSP's
1305                  * local APIC to INT and NMI lines.
1306                  */
1307                 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1308                                 "enabling APIC mode.\n");
1309                 outb(0x70, 0x22);
1310                 outb(0x01, 0x23);
1311         }
1312         enable_apic_mode();
1313 }
1314
1315 /**
1316  * disconnect_bsp_APIC - detach the APIC from the interrupt system
1317  * @virt_wire_setup:    indicates, whether virtual wire mode is selected
1318  *
1319  * Virtual wire mode is necessary to deliver legacy interrupts even when the
1320  * APIC is disabled.
1321  */
1322 void disconnect_bsp_APIC(int virt_wire_setup)
1323 {
1324         if (pic_mode) {
1325                 /*
1326                  * Put the board back into PIC mode (has an effect only on
1327                  * certain older boards).  Note that APIC interrupts, including
1328                  * IPIs, won't work beyond this point!  The only exception are
1329                  * INIT IPIs.
1330                  */
1331                 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1332                                 "entering PIC mode.\n");
1333                 outb(0x70, 0x22);
1334                 outb(0x00, 0x23);
1335         } else {
1336                 /* Go back to Virtual Wire compatibility mode */
1337                 unsigned long value;
1338
1339                 /* For the spurious interrupt use vector F, and enable it */
1340                 value = apic_read(APIC_SPIV);
1341                 value &= ~APIC_VECTOR_MASK;
1342                 value |= APIC_SPIV_APIC_ENABLED;
1343                 value |= 0xf;
1344                 apic_write_around(APIC_SPIV, value);
1345
1346                 if (!virt_wire_setup) {
1347                         /*
1348                          * For LVT0 make it edge triggered, active high,
1349                          * external and enabled
1350                          */
1351                         value = apic_read(APIC_LVT0);
1352                         value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1353                                 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1354                                 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1355                         value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1356                         value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1357                         apic_write_around(APIC_LVT0, value);
1358                 } else {
1359                         /* Disable LVT0 */
1360                         apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1361                 }
1362
1363                 /*
1364                  * For LVT1 make it edge triggered, active high, nmi and
1365                  * enabled
1366                  */
1367                 value = apic_read(APIC_LVT1);
1368                 value &= ~(
1369                         APIC_MODE_MASK | APIC_SEND_PENDING |
1370                         APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1371                         APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1372                 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1373                 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1374                 apic_write_around(APIC_LVT1, value);
1375         }
1376 }
1377
1378 /*
1379  * Power management
1380  */
1381 #ifdef CONFIG_PM
1382
1383 static struct {
1384         int active;
1385         /* r/w apic fields */
1386         unsigned int apic_id;
1387         unsigned int apic_taskpri;
1388         unsigned int apic_ldr;
1389         unsigned int apic_dfr;
1390         unsigned int apic_spiv;
1391         unsigned int apic_lvtt;
1392         unsigned int apic_lvtpc;
1393         unsigned int apic_lvt0;
1394         unsigned int apic_lvt1;
1395         unsigned int apic_lvterr;
1396         unsigned int apic_tmict;
1397         unsigned int apic_tdcr;
1398         unsigned int apic_thmr;
1399 } apic_pm_state;
1400
1401 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1402 {
1403         unsigned long flags;
1404         int maxlvt;
1405
1406         if (!apic_pm_state.active)
1407                 return 0;
1408
1409         maxlvt = lapic_get_maxlvt();
1410
1411         apic_pm_state.apic_id = apic_read(APIC_ID);
1412         apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1413         apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1414         apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1415         apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1416         apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1417         if (maxlvt >= 4)
1418                 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1419         apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1420         apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1421         apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1422         apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1423         apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1424 #ifdef CONFIG_X86_MCE_P4THERMAL
1425         if (maxlvt >= 5)
1426                 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1427 #endif
1428
1429         local_irq_save(flags);
1430         disable_local_APIC();
1431         local_irq_restore(flags);
1432         return 0;
1433 }
1434
1435 static int lapic_resume(struct sys_device *dev)
1436 {
1437         unsigned int l, h;
1438         unsigned long flags;
1439         int maxlvt;
1440
1441         if (!apic_pm_state.active)
1442                 return 0;
1443
1444         maxlvt = lapic_get_maxlvt();
1445
1446         local_irq_save(flags);
1447
1448         /*
1449          * Make sure the APICBASE points to the right address
1450          *
1451          * FIXME! This will be wrong if we ever support suspend on
1452          * SMP! We'll need to do this as part of the CPU restore!
1453          */
1454         rdmsr(MSR_IA32_APICBASE, l, h);
1455         l &= ~MSR_IA32_APICBASE_BASE;
1456         l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1457         wrmsr(MSR_IA32_APICBASE, l, h);
1458
1459         apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1460         apic_write(APIC_ID, apic_pm_state.apic_id);
1461         apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1462         apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1463         apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1464         apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1465         apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1466         apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1467 #ifdef CONFIG_X86_MCE_P4THERMAL
1468         if (maxlvt >= 5)
1469                 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1470 #endif
1471         if (maxlvt >= 4)
1472                 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1473         apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1474         apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1475         apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1476         apic_write(APIC_ESR, 0);
1477         apic_read(APIC_ESR);
1478         apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1479         apic_write(APIC_ESR, 0);
1480         apic_read(APIC_ESR);
1481         local_irq_restore(flags);
1482         return 0;
1483 }
1484
1485 /*
1486  * This device has no shutdown method - fully functioning local APICs
1487  * are needed on every CPU up until machine_halt/restart/poweroff.
1488  */
1489
1490 static struct sysdev_class lapic_sysclass = {
1491         set_kset_name("lapic"),
1492         .resume         = lapic_resume,
1493         .suspend        = lapic_suspend,
1494 };
1495
1496 static struct sys_device device_lapic = {
1497         .id     = 0,
1498         .cls    = &lapic_sysclass,
1499 };
1500
1501 static void __devinit apic_pm_activate(void)
1502 {
1503         apic_pm_state.active = 1;
1504 }
1505
1506 static int __init init_lapic_sysfs(void)
1507 {
1508         int error;
1509
1510         if (!cpu_has_apic)
1511                 return 0;
1512         /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1513
1514         error = sysdev_class_register(&lapic_sysclass);
1515         if (!error)
1516                 error = sysdev_register(&device_lapic);
1517         return error;
1518 }
1519 device_initcall(init_lapic_sysfs);
1520
1521 #else   /* CONFIG_PM */
1522
1523 static void apic_pm_activate(void) { }
1524
1525 #endif  /* CONFIG_PM */