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