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