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