x86: coding style fixes to arch/x86/kernel/cpu/mcheck/winchip.c
[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  * Setup extended LVT, AMD specific (K8, family 10h)
625  *
626  * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
627  * MCE interrupts are supported. Thus MCE offset must be set to 0.
628  */
629
630 #define APIC_EILVT_LVTOFF_MCE 0
631 #define APIC_EILVT_LVTOFF_IBS 1
632
633 static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
634 {
635         unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
636         unsigned int  v   = (mask << 16) | (msg_type << 8) | vector;
637         apic_write(reg, v);
638 }
639
640 u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
641 {
642         setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
643         return APIC_EILVT_LVTOFF_MCE;
644 }
645
646 u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
647 {
648         setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
649         return APIC_EILVT_LVTOFF_IBS;
650 }
651
652 /*
653  * Local APIC start and shutdown
654  */
655
656 /**
657  * clear_local_APIC - shutdown the local APIC
658  *
659  * This is called, when a CPU is disabled and before rebooting, so the state of
660  * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
661  * leftovers during boot.
662  */
663 void clear_local_APIC(void)
664 {
665         int maxlvt;
666         u32 v;
667
668         /* APIC hasn't been mapped yet */
669         if (!apic_phys)
670                 return;
671
672         maxlvt = lapic_get_maxlvt();
673         /*
674          * Masking an LVT entry can trigger a local APIC error
675          * if the vector is zero. Mask LVTERR first to prevent this.
676          */
677         if (maxlvt >= 3) {
678                 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
679                 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
680         }
681         /*
682          * Careful: we have to set masks only first to deassert
683          * any level-triggered sources.
684          */
685         v = apic_read(APIC_LVTT);
686         apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
687         v = apic_read(APIC_LVT0);
688         apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
689         v = apic_read(APIC_LVT1);
690         apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
691         if (maxlvt >= 4) {
692                 v = apic_read(APIC_LVTPC);
693                 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
694         }
695
696         /* lets not touch this if we didn't frob it */
697 #ifdef CONFIG_X86_MCE_P4THERMAL
698         if (maxlvt >= 5) {
699                 v = apic_read(APIC_LVTTHMR);
700                 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
701         }
702 #endif
703         /*
704          * Clean APIC state for other OSs:
705          */
706         apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
707         apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
708         apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
709         if (maxlvt >= 3)
710                 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
711         if (maxlvt >= 4)
712                 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
713
714 #ifdef CONFIG_X86_MCE_P4THERMAL
715         if (maxlvt >= 5)
716                 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
717 #endif
718         /* Integrated APIC (!82489DX) ? */
719         if (lapic_is_integrated()) {
720                 if (maxlvt > 3)
721                         /* Clear ESR due to Pentium errata 3AP and 11AP */
722                         apic_write(APIC_ESR, 0);
723                 apic_read(APIC_ESR);
724         }
725 }
726
727 /**
728  * disable_local_APIC - clear and disable the local APIC
729  */
730 void disable_local_APIC(void)
731 {
732         unsigned long value;
733
734         clear_local_APIC();
735
736         /*
737          * Disable APIC (implies clearing of registers
738          * for 82489DX!).
739          */
740         value = apic_read(APIC_SPIV);
741         value &= ~APIC_SPIV_APIC_ENABLED;
742         apic_write_around(APIC_SPIV, value);
743
744         /*
745          * When LAPIC was disabled by the BIOS and enabled by the kernel,
746          * restore the disabled state.
747          */
748         if (enabled_via_apicbase) {
749                 unsigned int l, h;
750
751                 rdmsr(MSR_IA32_APICBASE, l, h);
752                 l &= ~MSR_IA32_APICBASE_ENABLE;
753                 wrmsr(MSR_IA32_APICBASE, l, h);
754         }
755 }
756
757 /*
758  * If Linux enabled the LAPIC against the BIOS default disable it down before
759  * re-entering the BIOS on shutdown.  Otherwise the BIOS may get confused and
760  * not power-off.  Additionally clear all LVT entries before disable_local_APIC
761  * for the case where Linux didn't enable the LAPIC.
762  */
763 void lapic_shutdown(void)
764 {
765         unsigned long flags;
766
767         if (!cpu_has_apic)
768                 return;
769
770         local_irq_save(flags);
771         clear_local_APIC();
772
773         if (enabled_via_apicbase)
774                 disable_local_APIC();
775
776         local_irq_restore(flags);
777 }
778
779 /*
780  * This is to verify that we're looking at a real local APIC.
781  * Check these against your board if the CPUs aren't getting
782  * started for no apparent reason.
783  */
784 int __init verify_local_APIC(void)
785 {
786         unsigned int reg0, reg1;
787
788         /*
789          * The version register is read-only in a real APIC.
790          */
791         reg0 = apic_read(APIC_LVR);
792         apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
793         apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
794         reg1 = apic_read(APIC_LVR);
795         apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
796
797         /*
798          * The two version reads above should print the same
799          * numbers.  If the second one is different, then we
800          * poke at a non-APIC.
801          */
802         if (reg1 != reg0)
803                 return 0;
804
805         /*
806          * Check if the version looks reasonably.
807          */
808         reg1 = GET_APIC_VERSION(reg0);
809         if (reg1 == 0x00 || reg1 == 0xff)
810                 return 0;
811         reg1 = lapic_get_maxlvt();
812         if (reg1 < 0x02 || reg1 == 0xff)
813                 return 0;
814
815         /*
816          * The ID register is read/write in a real APIC.
817          */
818         reg0 = apic_read(APIC_ID);
819         apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
820
821         /*
822          * The next two are just to see if we have sane values.
823          * They're only really relevant if we're in Virtual Wire
824          * compatibility mode, but most boxes are anymore.
825          */
826         reg0 = apic_read(APIC_LVT0);
827         apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
828         reg1 = apic_read(APIC_LVT1);
829         apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
830
831         return 1;
832 }
833
834 /**
835  * sync_Arb_IDs - synchronize APIC bus arbitration IDs
836  */
837 void __init sync_Arb_IDs(void)
838 {
839         /*
840          * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
841          * needed on AMD.
842          */
843         if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
844                 return;
845         /*
846          * Wait for idle.
847          */
848         apic_wait_icr_idle();
849
850         apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
851         apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
852                                 | APIC_DM_INIT);
853 }
854
855 /*
856  * An initial setup of the virtual wire mode.
857  */
858 void __init init_bsp_APIC(void)
859 {
860         unsigned long value;
861
862         /*
863          * Don't do the setup now if we have a SMP BIOS as the
864          * through-I/O-APIC virtual wire mode might be active.
865          */
866         if (smp_found_config || !cpu_has_apic)
867                 return;
868
869         /*
870          * Do not trust the local APIC being empty at bootup.
871          */
872         clear_local_APIC();
873
874         /*
875          * Enable APIC.
876          */
877         value = apic_read(APIC_SPIV);
878         value &= ~APIC_VECTOR_MASK;
879         value |= APIC_SPIV_APIC_ENABLED;
880
881         /* This bit is reserved on P4/Xeon and should be cleared */
882         if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
883             (boot_cpu_data.x86 == 15))
884                 value &= ~APIC_SPIV_FOCUS_DISABLED;
885         else
886                 value |= APIC_SPIV_FOCUS_DISABLED;
887         value |= SPURIOUS_APIC_VECTOR;
888         apic_write_around(APIC_SPIV, value);
889
890         /*
891          * Set up the virtual wire mode.
892          */
893         apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
894         value = APIC_DM_NMI;
895         if (!lapic_is_integrated())             /* 82489DX */
896                 value |= APIC_LVT_LEVEL_TRIGGER;
897         apic_write_around(APIC_LVT1, value);
898 }
899
900 /**
901  * setup_local_APIC - setup the local APIC
902  */
903 void __cpuinit setup_local_APIC(void)
904 {
905         unsigned long oldvalue, value, maxlvt, integrated;
906         int i, j;
907
908         /* Pound the ESR really hard over the head with a big hammer - mbligh */
909         if (esr_disable) {
910                 apic_write(APIC_ESR, 0);
911                 apic_write(APIC_ESR, 0);
912                 apic_write(APIC_ESR, 0);
913                 apic_write(APIC_ESR, 0);
914         }
915
916         integrated = lapic_is_integrated();
917
918         /*
919          * Double-check whether this APIC is really registered.
920          */
921         if (!apic_id_registered())
922                 BUG();
923
924         /*
925          * Intel recommends to set DFR, LDR and TPR before enabling
926          * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
927          * document number 292116).  So here it goes...
928          */
929         init_apic_ldr();
930
931         /*
932          * Set Task Priority to 'accept all'. We never change this
933          * later on.
934          */
935         value = apic_read(APIC_TASKPRI);
936         value &= ~APIC_TPRI_MASK;
937         apic_write_around(APIC_TASKPRI, value);
938
939         /*
940          * After a crash, we no longer service the interrupts and a pending
941          * interrupt from previous kernel might still have ISR bit set.
942          *
943          * Most probably by now CPU has serviced that pending interrupt and
944          * it might not have done the ack_APIC_irq() because it thought,
945          * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
946          * does not clear the ISR bit and cpu thinks it has already serivced
947          * the interrupt. Hence a vector might get locked. It was noticed
948          * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
949          */
950         for (i = APIC_ISR_NR - 1; i >= 0; i--) {
951                 value = apic_read(APIC_ISR + i*0x10);
952                 for (j = 31; j >= 0; j--) {
953                         if (value & (1<<j))
954                                 ack_APIC_irq();
955                 }
956         }
957
958         /*
959          * Now that we are all set up, enable the APIC
960          */
961         value = apic_read(APIC_SPIV);
962         value &= ~APIC_VECTOR_MASK;
963         /*
964          * Enable APIC
965          */
966         value |= APIC_SPIV_APIC_ENABLED;
967
968         /*
969          * Some unknown Intel IO/APIC (or APIC) errata is biting us with
970          * certain networking cards. If high frequency interrupts are
971          * happening on a particular IOAPIC pin, plus the IOAPIC routing
972          * entry is masked/unmasked at a high rate as well then sooner or
973          * later IOAPIC line gets 'stuck', no more interrupts are received
974          * from the device. If focus CPU is disabled then the hang goes
975          * away, oh well :-(
976          *
977          * [ This bug can be reproduced easily with a level-triggered
978          *   PCI Ne2000 networking cards and PII/PIII processors, dual
979          *   BX chipset. ]
980          */
981         /*
982          * Actually disabling the focus CPU check just makes the hang less
983          * frequent as it makes the interrupt distributon model be more
984          * like LRU than MRU (the short-term load is more even across CPUs).
985          * See also the comment in end_level_ioapic_irq().  --macro
986          */
987
988         /* Enable focus processor (bit==0) */
989         value &= ~APIC_SPIV_FOCUS_DISABLED;
990
991         /*
992          * Set spurious IRQ vector
993          */
994         value |= SPURIOUS_APIC_VECTOR;
995         apic_write_around(APIC_SPIV, value);
996
997         /*
998          * Set up LVT0, LVT1:
999          *
1000          * set up through-local-APIC on the BP's LINT0. This is not
1001          * strictly necessary in pure symmetric-IO mode, but sometimes
1002          * we delegate interrupts to the 8259A.
1003          */
1004         /*
1005          * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1006          */
1007         value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1008         if (!smp_processor_id() && (pic_mode || !value)) {
1009                 value = APIC_DM_EXTINT;
1010                 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1011                                 smp_processor_id());
1012         } else {
1013                 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1014                 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1015                                 smp_processor_id());
1016         }
1017         apic_write_around(APIC_LVT0, value);
1018
1019         /*
1020          * only the BP should see the LINT1 NMI signal, obviously.
1021          */
1022         if (!smp_processor_id())
1023                 value = APIC_DM_NMI;
1024         else
1025                 value = APIC_DM_NMI | APIC_LVT_MASKED;
1026         if (!integrated)                /* 82489DX */
1027                 value |= APIC_LVT_LEVEL_TRIGGER;
1028         apic_write_around(APIC_LVT1, value);
1029
1030         if (integrated && !esr_disable) {
1031                 /* !82489DX */
1032                 maxlvt = lapic_get_maxlvt();
1033                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
1034                         apic_write(APIC_ESR, 0);
1035                 oldvalue = apic_read(APIC_ESR);
1036
1037                 /* enables sending errors */
1038                 value = ERROR_APIC_VECTOR;
1039                 apic_write_around(APIC_LVTERR, value);
1040                 /*
1041                  * spec says clear errors after enabling vector.
1042                  */
1043                 if (maxlvt > 3)
1044                         apic_write(APIC_ESR, 0);
1045                 value = apic_read(APIC_ESR);
1046                 if (value != oldvalue)
1047                         apic_printk(APIC_VERBOSE, "ESR value before enabling "
1048                                 "vector: 0x%08lx  after: 0x%08lx\n",
1049                                 oldvalue, value);
1050         } else {
1051                 if (esr_disable)
1052                         /*
1053                          * Something untraceable is creating bad interrupts on
1054                          * secondary quads ... for the moment, just leave the
1055                          * ESR disabled - we can't do anything useful with the
1056                          * errors anyway - mbligh
1057                          */
1058                         printk(KERN_INFO "Leaving ESR disabled.\n");
1059                 else
1060                         printk(KERN_INFO "No ESR for 82489DX.\n");
1061         }
1062
1063         /* Disable the local apic timer */
1064         value = apic_read(APIC_LVTT);
1065         value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1066         apic_write_around(APIC_LVTT, value);
1067
1068         setup_apic_nmi_watchdog(NULL);
1069         apic_pm_activate();
1070 }
1071
1072 /*
1073  * Detect and initialize APIC
1074  */
1075 static int __init detect_init_APIC(void)
1076 {
1077         u32 h, l, features;
1078
1079         /* Disabled by kernel option? */
1080         if (enable_local_apic < 0)
1081                 return -1;
1082
1083         switch (boot_cpu_data.x86_vendor) {
1084         case X86_VENDOR_AMD:
1085                 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1086                     (boot_cpu_data.x86 == 15))
1087                         break;
1088                 goto no_apic;
1089         case X86_VENDOR_INTEL:
1090                 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1091                     (boot_cpu_data.x86 == 5 && cpu_has_apic))
1092                         break;
1093                 goto no_apic;
1094         default:
1095                 goto no_apic;
1096         }
1097
1098         if (!cpu_has_apic) {
1099                 /*
1100                  * Over-ride BIOS and try to enable the local APIC only if
1101                  * "lapic" specified.
1102                  */
1103                 if (enable_local_apic <= 0) {
1104                         printk(KERN_INFO "Local APIC disabled by BIOS -- "
1105                                "you can enable it with \"lapic\"\n");
1106                         return -1;
1107                 }
1108                 /*
1109                  * Some BIOSes disable the local APIC in the APIC_BASE
1110                  * MSR. This can only be done in software for Intel P6 or later
1111                  * and AMD K7 (Model > 1) or later.
1112                  */
1113                 rdmsr(MSR_IA32_APICBASE, l, h);
1114                 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1115                         printk(KERN_INFO
1116                                "Local APIC disabled by BIOS -- reenabling.\n");
1117                         l &= ~MSR_IA32_APICBASE_BASE;
1118                         l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1119                         wrmsr(MSR_IA32_APICBASE, l, h);
1120                         enabled_via_apicbase = 1;
1121                 }
1122         }
1123         /*
1124          * The APIC feature bit should now be enabled
1125          * in `cpuid'
1126          */
1127         features = cpuid_edx(1);
1128         if (!(features & (1 << X86_FEATURE_APIC))) {
1129                 printk(KERN_WARNING "Could not enable APIC!\n");
1130                 return -1;
1131         }
1132         set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1133         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1134
1135         /* The BIOS may have set up the APIC at some other address */
1136         rdmsr(MSR_IA32_APICBASE, l, h);
1137         if (l & MSR_IA32_APICBASE_ENABLE)
1138                 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1139
1140         if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
1141                 nmi_watchdog = NMI_LOCAL_APIC;
1142
1143         printk(KERN_INFO "Found and enabled local APIC!\n");
1144
1145         apic_pm_activate();
1146
1147         return 0;
1148
1149 no_apic:
1150         printk(KERN_INFO "No local APIC present or hardware disabled\n");
1151         return -1;
1152 }
1153
1154 /**
1155  * init_apic_mappings - initialize APIC mappings
1156  */
1157 void __init init_apic_mappings(void)
1158 {
1159         /*
1160          * If no local APIC can be found then set up a fake all
1161          * zeroes page to simulate the local APIC and another
1162          * one for the IO-APIC.
1163          */
1164         if (!smp_found_config && detect_init_APIC()) {
1165                 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1166                 apic_phys = __pa(apic_phys);
1167         } else
1168                 apic_phys = mp_lapic_addr;
1169
1170         set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1171         printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1172                apic_phys);
1173
1174         /*
1175          * Fetch the APIC ID of the BSP in case we have a
1176          * default configuration (or the MP table is broken).
1177          */
1178         if (boot_cpu_physical_apicid == -1U)
1179                 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1180
1181 #ifdef CONFIG_X86_IO_APIC
1182         {
1183                 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1184                 int i;
1185
1186                 for (i = 0; i < nr_ioapics; i++) {
1187                         if (smp_found_config) {
1188                                 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1189                                 if (!ioapic_phys) {
1190                                         printk(KERN_ERR
1191                                                "WARNING: bogus zero IO-APIC "
1192                                                "address found in MPTABLE, "
1193                                                "disabling IO/APIC support!\n");
1194                                         smp_found_config = 0;
1195                                         skip_ioapic_setup = 1;
1196                                         goto fake_ioapic_page;
1197                                 }
1198                         } else {
1199 fake_ioapic_page:
1200                                 ioapic_phys = (unsigned long)
1201                                               alloc_bootmem_pages(PAGE_SIZE);
1202                                 ioapic_phys = __pa(ioapic_phys);
1203                         }
1204                         set_fixmap_nocache(idx, ioapic_phys);
1205                         printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1206                                __fix_to_virt(idx), ioapic_phys);
1207                         idx++;
1208                 }
1209         }
1210 #endif
1211 }
1212
1213 /*
1214  * This initializes the IO-APIC and APIC hardware if this is
1215  * a UP kernel.
1216  */
1217 int __init APIC_init_uniprocessor(void)
1218 {
1219         if (enable_local_apic < 0)
1220                 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1221
1222         if (!smp_found_config && !cpu_has_apic)
1223                 return -1;
1224
1225         /*
1226          * Complain if the BIOS pretends there is one.
1227          */
1228         if (!cpu_has_apic &&
1229             APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1230                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1231                        boot_cpu_physical_apicid);
1232                 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1233                 return -1;
1234         }
1235
1236         verify_local_APIC();
1237
1238         connect_bsp_APIC();
1239
1240         /*
1241          * Hack: In case of kdump, after a crash, kernel might be booting
1242          * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1243          * might be zero if read from MP tables. Get it from LAPIC.
1244          */
1245 #ifdef CONFIG_CRASH_DUMP
1246         boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1247 #endif
1248         phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1249
1250         setup_local_APIC();
1251
1252 #ifdef CONFIG_X86_IO_APIC
1253         if (smp_found_config)
1254                 if (!skip_ioapic_setup && nr_ioapics)
1255                         setup_IO_APIC();
1256 #endif
1257         setup_boot_clock();
1258
1259         return 0;
1260 }
1261
1262 /*
1263  * Local APIC interrupts
1264  */
1265
1266 /*
1267  * This interrupt should _never_ happen with our APIC/SMP architecture
1268  */
1269 void smp_spurious_interrupt(struct pt_regs *regs)
1270 {
1271         unsigned long v;
1272
1273         irq_enter();
1274         /*
1275          * Check if this really is a spurious interrupt and ACK it
1276          * if it is a vectored one.  Just in case...
1277          * Spurious interrupts should not be ACKed.
1278          */
1279         v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1280         if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1281                 ack_APIC_irq();
1282
1283         /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1284         printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1285                "should never happen.\n", smp_processor_id());
1286         __get_cpu_var(irq_stat).irq_spurious_count++;
1287         irq_exit();
1288 }
1289
1290 /*
1291  * This interrupt should never happen with our APIC/SMP architecture
1292  */
1293 void smp_error_interrupt(struct pt_regs *regs)
1294 {
1295         unsigned long v, v1;
1296
1297         irq_enter();
1298         /* First tickle the hardware, only then report what went on. -- REW */
1299         v = apic_read(APIC_ESR);
1300         apic_write(APIC_ESR, 0);
1301         v1 = apic_read(APIC_ESR);
1302         ack_APIC_irq();
1303         atomic_inc(&irq_err_count);
1304
1305         /* Here is what the APIC error bits mean:
1306            0: Send CS error
1307            1: Receive CS error
1308            2: Send accept error
1309            3: Receive accept error
1310            4: Reserved
1311            5: Send illegal vector
1312            6: Received illegal vector
1313            7: Illegal register address
1314         */
1315         printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1316                 smp_processor_id(), v , v1);
1317         irq_exit();
1318 }
1319
1320 /*
1321  * Initialize APIC interrupts
1322  */
1323 void __init apic_intr_init(void)
1324 {
1325 #ifdef CONFIG_SMP
1326         smp_intr_init();
1327 #endif
1328         /* self generated IPI for local APIC timer */
1329         set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1330
1331         /* IPI vectors for APIC spurious and error interrupts */
1332         set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1333         set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1334
1335         /* thermal monitor LVT interrupt */
1336 #ifdef CONFIG_X86_MCE_P4THERMAL
1337         set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1338 #endif
1339 }
1340
1341 /**
1342  * connect_bsp_APIC - attach the APIC to the interrupt system
1343  */
1344 void __init connect_bsp_APIC(void)
1345 {
1346         if (pic_mode) {
1347                 /*
1348                  * Do not trust the local APIC being empty at bootup.
1349                  */
1350                 clear_local_APIC();
1351                 /*
1352                  * PIC mode, enable APIC mode in the IMCR, i.e.  connect BSP's
1353                  * local APIC to INT and NMI lines.
1354                  */
1355                 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1356                                 "enabling APIC mode.\n");
1357                 outb(0x70, 0x22);
1358                 outb(0x01, 0x23);
1359         }
1360         enable_apic_mode();
1361 }
1362
1363 /**
1364  * disconnect_bsp_APIC - detach the APIC from the interrupt system
1365  * @virt_wire_setup:    indicates, whether virtual wire mode is selected
1366  *
1367  * Virtual wire mode is necessary to deliver legacy interrupts even when the
1368  * APIC is disabled.
1369  */
1370 void disconnect_bsp_APIC(int virt_wire_setup)
1371 {
1372         if (pic_mode) {
1373                 /*
1374                  * Put the board back into PIC mode (has an effect only on
1375                  * certain older boards).  Note that APIC interrupts, including
1376                  * IPIs, won't work beyond this point!  The only exception are
1377                  * INIT IPIs.
1378                  */
1379                 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1380                                 "entering PIC mode.\n");
1381                 outb(0x70, 0x22);
1382                 outb(0x00, 0x23);
1383         } else {
1384                 /* Go back to Virtual Wire compatibility mode */
1385                 unsigned long value;
1386
1387                 /* For the spurious interrupt use vector F, and enable it */
1388                 value = apic_read(APIC_SPIV);
1389                 value &= ~APIC_VECTOR_MASK;
1390                 value |= APIC_SPIV_APIC_ENABLED;
1391                 value |= 0xf;
1392                 apic_write_around(APIC_SPIV, value);
1393
1394                 if (!virt_wire_setup) {
1395                         /*
1396                          * For LVT0 make it edge triggered, active high,
1397                          * external and enabled
1398                          */
1399                         value = apic_read(APIC_LVT0);
1400                         value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1401                                 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1402                                 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1403                         value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1404                         value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1405                         apic_write_around(APIC_LVT0, value);
1406                 } else {
1407                         /* Disable LVT0 */
1408                         apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1409                 }
1410
1411                 /*
1412                  * For LVT1 make it edge triggered, active high, nmi and
1413                  * enabled
1414                  */
1415                 value = apic_read(APIC_LVT1);
1416                 value &= ~(
1417                         APIC_MODE_MASK | APIC_SEND_PENDING |
1418                         APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1419                         APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1420                 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1421                 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1422                 apic_write_around(APIC_LVT1, value);
1423         }
1424 }
1425
1426 /*
1427  * Power management
1428  */
1429 #ifdef CONFIG_PM
1430
1431 static struct {
1432         int active;
1433         /* r/w apic fields */
1434         unsigned int apic_id;
1435         unsigned int apic_taskpri;
1436         unsigned int apic_ldr;
1437         unsigned int apic_dfr;
1438         unsigned int apic_spiv;
1439         unsigned int apic_lvtt;
1440         unsigned int apic_lvtpc;
1441         unsigned int apic_lvt0;
1442         unsigned int apic_lvt1;
1443         unsigned int apic_lvterr;
1444         unsigned int apic_tmict;
1445         unsigned int apic_tdcr;
1446         unsigned int apic_thmr;
1447 } apic_pm_state;
1448
1449 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1450 {
1451         unsigned long flags;
1452         int maxlvt;
1453
1454         if (!apic_pm_state.active)
1455                 return 0;
1456
1457         maxlvt = lapic_get_maxlvt();
1458
1459         apic_pm_state.apic_id = apic_read(APIC_ID);
1460         apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1461         apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1462         apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1463         apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1464         apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1465         if (maxlvt >= 4)
1466                 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1467         apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1468         apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1469         apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1470         apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1471         apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1472 #ifdef CONFIG_X86_MCE_P4THERMAL
1473         if (maxlvt >= 5)
1474                 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1475 #endif
1476
1477         local_irq_save(flags);
1478         disable_local_APIC();
1479         local_irq_restore(flags);
1480         return 0;
1481 }
1482
1483 static int lapic_resume(struct sys_device *dev)
1484 {
1485         unsigned int l, h;
1486         unsigned long flags;
1487         int maxlvt;
1488
1489         if (!apic_pm_state.active)
1490                 return 0;
1491
1492         maxlvt = lapic_get_maxlvt();
1493
1494         local_irq_save(flags);
1495
1496         /*
1497          * Make sure the APICBASE points to the right address
1498          *
1499          * FIXME! This will be wrong if we ever support suspend on
1500          * SMP! We'll need to do this as part of the CPU restore!
1501          */
1502         rdmsr(MSR_IA32_APICBASE, l, h);
1503         l &= ~MSR_IA32_APICBASE_BASE;
1504         l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1505         wrmsr(MSR_IA32_APICBASE, l, h);
1506
1507         apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1508         apic_write(APIC_ID, apic_pm_state.apic_id);
1509         apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1510         apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1511         apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1512         apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1513         apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1514         apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1515 #ifdef CONFIG_X86_MCE_P4THERMAL
1516         if (maxlvt >= 5)
1517                 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1518 #endif
1519         if (maxlvt >= 4)
1520                 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1521         apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1522         apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1523         apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1524         apic_write(APIC_ESR, 0);
1525         apic_read(APIC_ESR);
1526         apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1527         apic_write(APIC_ESR, 0);
1528         apic_read(APIC_ESR);
1529         local_irq_restore(flags);
1530         return 0;
1531 }
1532
1533 /*
1534  * This device has no shutdown method - fully functioning local APICs
1535  * are needed on every CPU up until machine_halt/restart/poweroff.
1536  */
1537
1538 static struct sysdev_class lapic_sysclass = {
1539         .name           = "lapic",
1540         .resume         = lapic_resume,
1541         .suspend        = lapic_suspend,
1542 };
1543
1544 static struct sys_device device_lapic = {
1545         .id     = 0,
1546         .cls    = &lapic_sysclass,
1547 };
1548
1549 static void __devinit apic_pm_activate(void)
1550 {
1551         apic_pm_state.active = 1;
1552 }
1553
1554 static int __init init_lapic_sysfs(void)
1555 {
1556         int error;
1557
1558         if (!cpu_has_apic)
1559                 return 0;
1560         /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1561
1562         error = sysdev_class_register(&lapic_sysclass);
1563         if (!error)
1564                 error = sysdev_register(&device_lapic);
1565         return error;
1566 }
1567 device_initcall(init_lapic_sysfs);
1568
1569 #else   /* CONFIG_PM */
1570
1571 static void apic_pm_activate(void) { }
1572
1573 #endif  /* CONFIG_PM */
1574
1575 /*
1576  * APIC command line parameters
1577  */
1578 static int __init parse_lapic(char *arg)
1579 {
1580         enable_local_apic = 1;
1581         return 0;
1582 }
1583 early_param("lapic", parse_lapic);
1584
1585 static int __init parse_nolapic(char *arg)
1586 {
1587         enable_local_apic = -1;
1588         clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1589         return 0;
1590 }
1591 early_param("nolapic", parse_nolapic);
1592
1593 static int __init parse_disable_lapic_timer(char *arg)
1594 {
1595         local_apic_timer_disabled = 1;
1596         return 0;
1597 }
1598 early_param("nolapic_timer", parse_disable_lapic_timer);
1599
1600 static int __init parse_lapic_timer_c2_ok(char *arg)
1601 {
1602         local_apic_timer_c2_ok = 1;
1603         return 0;
1604 }
1605 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1606
1607 static int __init apic_set_verbosity(char *str)
1608 {
1609         if (strcmp("debug", str) == 0)
1610                 apic_verbosity = APIC_DEBUG;
1611         else if (strcmp("verbose", str) == 0)
1612                 apic_verbosity = APIC_VERBOSE;
1613         return 1;
1614 }
1615 __setup("apic=", apic_set_verbosity);
1616