]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/apic_32.c
6f506020bd7dd6eafd92902041196e112a12572d
[linux-2.6-omap-h63xx.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 void __cpuinit lapic_setup_esr(void)
901 {
902         unsigned long oldvalue, value, maxlvt;
903         if (lapic_is_integrated() && !esr_disable) {
904                 /* !82489DX */
905                 maxlvt = lapic_get_maxlvt();
906                 if (maxlvt > 3)         /* Due to the Pentium erratum 3AP. */
907                         apic_write(APIC_ESR, 0);
908                 oldvalue = apic_read(APIC_ESR);
909
910                 /* enables sending errors */
911                 value = ERROR_APIC_VECTOR;
912                 apic_write_around(APIC_LVTERR, value);
913                 /*
914                  * spec says clear errors after enabling vector.
915                  */
916                 if (maxlvt > 3)
917                         apic_write(APIC_ESR, 0);
918                 value = apic_read(APIC_ESR);
919                 if (value != oldvalue)
920                         apic_printk(APIC_VERBOSE, "ESR value before enabling "
921                                 "vector: 0x%08lx  after: 0x%08lx\n",
922                                 oldvalue, value);
923         } else {
924                 if (esr_disable)
925                         /*
926                          * Something untraceable is creating bad interrupts on
927                          * secondary quads ... for the moment, just leave the
928                          * ESR disabled - we can't do anything useful with the
929                          * errors anyway - mbligh
930                          */
931                         printk(KERN_INFO "Leaving ESR disabled.\n");
932                 else
933                         printk(KERN_INFO "No ESR for 82489DX.\n");
934         }
935 }
936
937
938 /**
939  * setup_local_APIC - setup the local APIC
940  */
941 void __cpuinit setup_local_APIC(void)
942 {
943         unsigned long value, integrated;
944         int i, j;
945
946         /* Pound the ESR really hard over the head with a big hammer - mbligh */
947         if (esr_disable) {
948                 apic_write(APIC_ESR, 0);
949                 apic_write(APIC_ESR, 0);
950                 apic_write(APIC_ESR, 0);
951                 apic_write(APIC_ESR, 0);
952         }
953
954         integrated = lapic_is_integrated();
955
956         /*
957          * Double-check whether this APIC is really registered.
958          */
959         if (!apic_id_registered())
960                 BUG();
961
962         /*
963          * Intel recommends to set DFR, LDR and TPR before enabling
964          * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
965          * document number 292116).  So here it goes...
966          */
967         init_apic_ldr();
968
969         /*
970          * Set Task Priority to 'accept all'. We never change this
971          * later on.
972          */
973         value = apic_read(APIC_TASKPRI);
974         value &= ~APIC_TPRI_MASK;
975         apic_write_around(APIC_TASKPRI, value);
976
977         /*
978          * After a crash, we no longer service the interrupts and a pending
979          * interrupt from previous kernel might still have ISR bit set.
980          *
981          * Most probably by now CPU has serviced that pending interrupt and
982          * it might not have done the ack_APIC_irq() because it thought,
983          * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
984          * does not clear the ISR bit and cpu thinks it has already serivced
985          * the interrupt. Hence a vector might get locked. It was noticed
986          * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
987          */
988         for (i = APIC_ISR_NR - 1; i >= 0; i--) {
989                 value = apic_read(APIC_ISR + i*0x10);
990                 for (j = 31; j >= 0; j--) {
991                         if (value & (1<<j))
992                                 ack_APIC_irq();
993                 }
994         }
995
996         /*
997          * Now that we are all set up, enable the APIC
998          */
999         value = apic_read(APIC_SPIV);
1000         value &= ~APIC_VECTOR_MASK;
1001         /*
1002          * Enable APIC
1003          */
1004         value |= APIC_SPIV_APIC_ENABLED;
1005
1006         /*
1007          * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1008          * certain networking cards. If high frequency interrupts are
1009          * happening on a particular IOAPIC pin, plus the IOAPIC routing
1010          * entry is masked/unmasked at a high rate as well then sooner or
1011          * later IOAPIC line gets 'stuck', no more interrupts are received
1012          * from the device. If focus CPU is disabled then the hang goes
1013          * away, oh well :-(
1014          *
1015          * [ This bug can be reproduced easily with a level-triggered
1016          *   PCI Ne2000 networking cards and PII/PIII processors, dual
1017          *   BX chipset. ]
1018          */
1019         /*
1020          * Actually disabling the focus CPU check just makes the hang less
1021          * frequent as it makes the interrupt distributon model be more
1022          * like LRU than MRU (the short-term load is more even across CPUs).
1023          * See also the comment in end_level_ioapic_irq().  --macro
1024          */
1025
1026         /* Enable focus processor (bit==0) */
1027         value &= ~APIC_SPIV_FOCUS_DISABLED;
1028
1029         /*
1030          * Set spurious IRQ vector
1031          */
1032         value |= SPURIOUS_APIC_VECTOR;
1033         apic_write_around(APIC_SPIV, value);
1034
1035         /*
1036          * Set up LVT0, LVT1:
1037          *
1038          * set up through-local-APIC on the BP's LINT0. This is not
1039          * strictly necessary in pure symmetric-IO mode, but sometimes
1040          * we delegate interrupts to the 8259A.
1041          */
1042         /*
1043          * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1044          */
1045         value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1046         if (!smp_processor_id() && (pic_mode || !value)) {
1047                 value = APIC_DM_EXTINT;
1048                 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1049                                 smp_processor_id());
1050         } else {
1051                 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1052                 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1053                                 smp_processor_id());
1054         }
1055         apic_write_around(APIC_LVT0, value);
1056
1057         /*
1058          * only the BP should see the LINT1 NMI signal, obviously.
1059          */
1060         if (!smp_processor_id())
1061                 value = APIC_DM_NMI;
1062         else
1063                 value = APIC_DM_NMI | APIC_LVT_MASKED;
1064         if (!integrated)                /* 82489DX */
1065                 value |= APIC_LVT_LEVEL_TRIGGER;
1066         apic_write_around(APIC_LVT1, value);
1067 }
1068
1069 void __cpuinit end_local_APIC_setup(void)
1070 {
1071         unsigned long value;
1072
1073         lapic_setup_esr();
1074         /* Disable the local apic timer */
1075         value = apic_read(APIC_LVTT);
1076         value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1077         apic_write_around(APIC_LVTT, value);
1078
1079         setup_apic_nmi_watchdog(NULL);
1080         apic_pm_activate();
1081 }
1082
1083 /*
1084  * Detect and initialize APIC
1085  */
1086 static int __init detect_init_APIC(void)
1087 {
1088         u32 h, l, features;
1089
1090         /* Disabled by kernel option? */
1091         if (enable_local_apic < 0)
1092                 return -1;
1093
1094         switch (boot_cpu_data.x86_vendor) {
1095         case X86_VENDOR_AMD:
1096                 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1097                     (boot_cpu_data.x86 == 15))
1098                         break;
1099                 goto no_apic;
1100         case X86_VENDOR_INTEL:
1101                 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1102                     (boot_cpu_data.x86 == 5 && cpu_has_apic))
1103                         break;
1104                 goto no_apic;
1105         default:
1106                 goto no_apic;
1107         }
1108
1109         if (!cpu_has_apic) {
1110                 /*
1111                  * Over-ride BIOS and try to enable the local APIC only if
1112                  * "lapic" specified.
1113                  */
1114                 if (enable_local_apic <= 0) {
1115                         printk(KERN_INFO "Local APIC disabled by BIOS -- "
1116                                "you can enable it with \"lapic\"\n");
1117                         return -1;
1118                 }
1119                 /*
1120                  * Some BIOSes disable the local APIC in the APIC_BASE
1121                  * MSR. This can only be done in software for Intel P6 or later
1122                  * and AMD K7 (Model > 1) or later.
1123                  */
1124                 rdmsr(MSR_IA32_APICBASE, l, h);
1125                 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1126                         printk(KERN_INFO
1127                                "Local APIC disabled by BIOS -- reenabling.\n");
1128                         l &= ~MSR_IA32_APICBASE_BASE;
1129                         l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1130                         wrmsr(MSR_IA32_APICBASE, l, h);
1131                         enabled_via_apicbase = 1;
1132                 }
1133         }
1134         /*
1135          * The APIC feature bit should now be enabled
1136          * in `cpuid'
1137          */
1138         features = cpuid_edx(1);
1139         if (!(features & (1 << X86_FEATURE_APIC))) {
1140                 printk(KERN_WARNING "Could not enable APIC!\n");
1141                 return -1;
1142         }
1143         set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1144         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1145
1146         /* The BIOS may have set up the APIC at some other address */
1147         rdmsr(MSR_IA32_APICBASE, l, h);
1148         if (l & MSR_IA32_APICBASE_ENABLE)
1149                 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1150
1151         if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
1152                 nmi_watchdog = NMI_LOCAL_APIC;
1153
1154         printk(KERN_INFO "Found and enabled local APIC!\n");
1155
1156         apic_pm_activate();
1157
1158         return 0;
1159
1160 no_apic:
1161         printk(KERN_INFO "No local APIC present or hardware disabled\n");
1162         return -1;
1163 }
1164
1165 /**
1166  * init_apic_mappings - initialize APIC mappings
1167  */
1168 void __init init_apic_mappings(void)
1169 {
1170         /*
1171          * If no local APIC can be found then set up a fake all
1172          * zeroes page to simulate the local APIC and another
1173          * one for the IO-APIC.
1174          */
1175         if (!smp_found_config && detect_init_APIC()) {
1176                 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1177                 apic_phys = __pa(apic_phys);
1178         } else
1179                 apic_phys = mp_lapic_addr;
1180
1181         set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1182         printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1183                apic_phys);
1184
1185         /*
1186          * Fetch the APIC ID of the BSP in case we have a
1187          * default configuration (or the MP table is broken).
1188          */
1189         if (boot_cpu_physical_apicid == -1U)
1190                 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1191
1192 #ifdef CONFIG_X86_IO_APIC
1193         {
1194                 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1195                 int i;
1196
1197                 for (i = 0; i < nr_ioapics; i++) {
1198                         if (smp_found_config) {
1199                                 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1200                                 if (!ioapic_phys) {
1201                                         printk(KERN_ERR
1202                                                "WARNING: bogus zero IO-APIC "
1203                                                "address found in MPTABLE, "
1204                                                "disabling IO/APIC support!\n");
1205                                         smp_found_config = 0;
1206                                         skip_ioapic_setup = 1;
1207                                         goto fake_ioapic_page;
1208                                 }
1209                         } else {
1210 fake_ioapic_page:
1211                                 ioapic_phys = (unsigned long)
1212                                               alloc_bootmem_pages(PAGE_SIZE);
1213                                 ioapic_phys = __pa(ioapic_phys);
1214                         }
1215                         set_fixmap_nocache(idx, ioapic_phys);
1216                         printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1217                                __fix_to_virt(idx), ioapic_phys);
1218                         idx++;
1219                 }
1220         }
1221 #endif
1222 }
1223
1224 /*
1225  * This initializes the IO-APIC and APIC hardware if this is
1226  * a UP kernel.
1227  */
1228 int __init APIC_init_uniprocessor(void)
1229 {
1230         if (enable_local_apic < 0)
1231                 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1232
1233         if (!smp_found_config && !cpu_has_apic)
1234                 return -1;
1235
1236         /*
1237          * Complain if the BIOS pretends there is one.
1238          */
1239         if (!cpu_has_apic &&
1240             APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1241                 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1242                        boot_cpu_physical_apicid);
1243                 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1244                 return -1;
1245         }
1246
1247         verify_local_APIC();
1248
1249         connect_bsp_APIC();
1250
1251         /*
1252          * Hack: In case of kdump, after a crash, kernel might be booting
1253          * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1254          * might be zero if read from MP tables. Get it from LAPIC.
1255          */
1256 #ifdef CONFIG_CRASH_DUMP
1257         boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1258 #endif
1259         phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1260
1261         setup_local_APIC();
1262
1263         end_local_APIC_setup();
1264 #ifdef CONFIG_X86_IO_APIC
1265         if (smp_found_config)
1266                 if (!skip_ioapic_setup && nr_ioapics)
1267                         setup_IO_APIC();
1268 #endif
1269         setup_boot_clock();
1270
1271         return 0;
1272 }
1273
1274 /*
1275  * Local APIC interrupts
1276  */
1277
1278 /*
1279  * This interrupt should _never_ happen with our APIC/SMP architecture
1280  */
1281 void smp_spurious_interrupt(struct pt_regs *regs)
1282 {
1283         unsigned long v;
1284
1285         irq_enter();
1286         /*
1287          * Check if this really is a spurious interrupt and ACK it
1288          * if it is a vectored one.  Just in case...
1289          * Spurious interrupts should not be ACKed.
1290          */
1291         v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1292         if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1293                 ack_APIC_irq();
1294
1295         /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1296         printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1297                "should never happen.\n", smp_processor_id());
1298         __get_cpu_var(irq_stat).irq_spurious_count++;
1299         irq_exit();
1300 }
1301
1302 /*
1303  * This interrupt should never happen with our APIC/SMP architecture
1304  */
1305 void smp_error_interrupt(struct pt_regs *regs)
1306 {
1307         unsigned long v, v1;
1308
1309         irq_enter();
1310         /* First tickle the hardware, only then report what went on. -- REW */
1311         v = apic_read(APIC_ESR);
1312         apic_write(APIC_ESR, 0);
1313         v1 = apic_read(APIC_ESR);
1314         ack_APIC_irq();
1315         atomic_inc(&irq_err_count);
1316
1317         /* Here is what the APIC error bits mean:
1318            0: Send CS error
1319            1: Receive CS error
1320            2: Send accept error
1321            3: Receive accept error
1322            4: Reserved
1323            5: Send illegal vector
1324            6: Received illegal vector
1325            7: Illegal register address
1326         */
1327         printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1328                 smp_processor_id(), v , v1);
1329         irq_exit();
1330 }
1331
1332 #ifdef CONFIG_SMP
1333 void __init smp_intr_init(void)
1334 {
1335         /*
1336          * IRQ0 must be given a fixed assignment and initialized,
1337          * because it's used before the IO-APIC is set up.
1338          */
1339         set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1340
1341         /*
1342          * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1343          * IPI, driven by wakeup.
1344          */
1345         set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1346
1347         /* IPI for invalidation */
1348         set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1349
1350         /* IPI for generic function call */
1351         set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1352 }
1353 #endif
1354
1355 /*
1356  * Initialize APIC interrupts
1357  */
1358 void __init apic_intr_init(void)
1359 {
1360 #ifdef CONFIG_SMP
1361         smp_intr_init();
1362 #endif
1363         /* self generated IPI for local APIC timer */
1364         set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1365
1366         /* IPI vectors for APIC spurious and error interrupts */
1367         set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1368         set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1369
1370         /* thermal monitor LVT interrupt */
1371 #ifdef CONFIG_X86_MCE_P4THERMAL
1372         set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1373 #endif
1374 }
1375
1376 /**
1377  * connect_bsp_APIC - attach the APIC to the interrupt system
1378  */
1379 void __init connect_bsp_APIC(void)
1380 {
1381         if (pic_mode) {
1382                 /*
1383                  * Do not trust the local APIC being empty at bootup.
1384                  */
1385                 clear_local_APIC();
1386                 /*
1387                  * PIC mode, enable APIC mode in the IMCR, i.e.  connect BSP's
1388                  * local APIC to INT and NMI lines.
1389                  */
1390                 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1391                                 "enabling APIC mode.\n");
1392                 outb(0x70, 0x22);
1393                 outb(0x01, 0x23);
1394         }
1395         enable_apic_mode();
1396 }
1397
1398 /**
1399  * disconnect_bsp_APIC - detach the APIC from the interrupt system
1400  * @virt_wire_setup:    indicates, whether virtual wire mode is selected
1401  *
1402  * Virtual wire mode is necessary to deliver legacy interrupts even when the
1403  * APIC is disabled.
1404  */
1405 void disconnect_bsp_APIC(int virt_wire_setup)
1406 {
1407         if (pic_mode) {
1408                 /*
1409                  * Put the board back into PIC mode (has an effect only on
1410                  * certain older boards).  Note that APIC interrupts, including
1411                  * IPIs, won't work beyond this point!  The only exception are
1412                  * INIT IPIs.
1413                  */
1414                 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1415                                 "entering PIC mode.\n");
1416                 outb(0x70, 0x22);
1417                 outb(0x00, 0x23);
1418         } else {
1419                 /* Go back to Virtual Wire compatibility mode */
1420                 unsigned long value;
1421
1422                 /* For the spurious interrupt use vector F, and enable it */
1423                 value = apic_read(APIC_SPIV);
1424                 value &= ~APIC_VECTOR_MASK;
1425                 value |= APIC_SPIV_APIC_ENABLED;
1426                 value |= 0xf;
1427                 apic_write_around(APIC_SPIV, value);
1428
1429                 if (!virt_wire_setup) {
1430                         /*
1431                          * For LVT0 make it edge triggered, active high,
1432                          * external and enabled
1433                          */
1434                         value = apic_read(APIC_LVT0);
1435                         value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1436                                 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1437                                 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1438                         value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1439                         value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1440                         apic_write_around(APIC_LVT0, value);
1441                 } else {
1442                         /* Disable LVT0 */
1443                         apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1444                 }
1445
1446                 /*
1447                  * For LVT1 make it edge triggered, active high, nmi and
1448                  * enabled
1449                  */
1450                 value = apic_read(APIC_LVT1);
1451                 value &= ~(
1452                         APIC_MODE_MASK | APIC_SEND_PENDING |
1453                         APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1454                         APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1455                 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1456                 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1457                 apic_write_around(APIC_LVT1, value);
1458         }
1459 }
1460
1461 /*
1462  * Power management
1463  */
1464 #ifdef CONFIG_PM
1465
1466 static struct {
1467         int active;
1468         /* r/w apic fields */
1469         unsigned int apic_id;
1470         unsigned int apic_taskpri;
1471         unsigned int apic_ldr;
1472         unsigned int apic_dfr;
1473         unsigned int apic_spiv;
1474         unsigned int apic_lvtt;
1475         unsigned int apic_lvtpc;
1476         unsigned int apic_lvt0;
1477         unsigned int apic_lvt1;
1478         unsigned int apic_lvterr;
1479         unsigned int apic_tmict;
1480         unsigned int apic_tdcr;
1481         unsigned int apic_thmr;
1482 } apic_pm_state;
1483
1484 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1485 {
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         apic_pm_state.apic_id = apic_read(APIC_ID);
1495         apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1496         apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1497         apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1498         apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1499         apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1500         if (maxlvt >= 4)
1501                 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1502         apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1503         apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1504         apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1505         apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1506         apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1507 #ifdef CONFIG_X86_MCE_P4THERMAL
1508         if (maxlvt >= 5)
1509                 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1510 #endif
1511
1512         local_irq_save(flags);
1513         disable_local_APIC();
1514         local_irq_restore(flags);
1515         return 0;
1516 }
1517
1518 static int lapic_resume(struct sys_device *dev)
1519 {
1520         unsigned int l, h;
1521         unsigned long flags;
1522         int maxlvt;
1523
1524         if (!apic_pm_state.active)
1525                 return 0;
1526
1527         maxlvt = lapic_get_maxlvt();
1528
1529         local_irq_save(flags);
1530
1531         /*
1532          * Make sure the APICBASE points to the right address
1533          *
1534          * FIXME! This will be wrong if we ever support suspend on
1535          * SMP! We'll need to do this as part of the CPU restore!
1536          */
1537         rdmsr(MSR_IA32_APICBASE, l, h);
1538         l &= ~MSR_IA32_APICBASE_BASE;
1539         l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1540         wrmsr(MSR_IA32_APICBASE, l, h);
1541
1542         apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1543         apic_write(APIC_ID, apic_pm_state.apic_id);
1544         apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1545         apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1546         apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1547         apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1548         apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1549         apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1550 #ifdef CONFIG_X86_MCE_P4THERMAL
1551         if (maxlvt >= 5)
1552                 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1553 #endif
1554         if (maxlvt >= 4)
1555                 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1556         apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1557         apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1558         apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1559         apic_write(APIC_ESR, 0);
1560         apic_read(APIC_ESR);
1561         apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1562         apic_write(APIC_ESR, 0);
1563         apic_read(APIC_ESR);
1564         local_irq_restore(flags);
1565         return 0;
1566 }
1567
1568 /*
1569  * This device has no shutdown method - fully functioning local APICs
1570  * are needed on every CPU up until machine_halt/restart/poweroff.
1571  */
1572
1573 static struct sysdev_class lapic_sysclass = {
1574         .name           = "lapic",
1575         .resume         = lapic_resume,
1576         .suspend        = lapic_suspend,
1577 };
1578
1579 static struct sys_device device_lapic = {
1580         .id     = 0,
1581         .cls    = &lapic_sysclass,
1582 };
1583
1584 static void __devinit apic_pm_activate(void)
1585 {
1586         apic_pm_state.active = 1;
1587 }
1588
1589 static int __init init_lapic_sysfs(void)
1590 {
1591         int error;
1592
1593         if (!cpu_has_apic)
1594                 return 0;
1595         /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1596
1597         error = sysdev_class_register(&lapic_sysclass);
1598         if (!error)
1599                 error = sysdev_register(&device_lapic);
1600         return error;
1601 }
1602 device_initcall(init_lapic_sysfs);
1603
1604 #else   /* CONFIG_PM */
1605
1606 static void apic_pm_activate(void) { }
1607
1608 #endif  /* CONFIG_PM */
1609
1610 /*
1611  * APIC command line parameters
1612  */
1613 static int __init parse_lapic(char *arg)
1614 {
1615         enable_local_apic = 1;
1616         return 0;
1617 }
1618 early_param("lapic", parse_lapic);
1619
1620 static int __init parse_nolapic(char *arg)
1621 {
1622         enable_local_apic = -1;
1623         clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1624         return 0;
1625 }
1626 early_param("nolapic", parse_nolapic);
1627
1628 static int __init parse_disable_lapic_timer(char *arg)
1629 {
1630         local_apic_timer_disabled = 1;
1631         return 0;
1632 }
1633 early_param("nolapic_timer", parse_disable_lapic_timer);
1634
1635 static int __init parse_lapic_timer_c2_ok(char *arg)
1636 {
1637         local_apic_timer_c2_ok = 1;
1638         return 0;
1639 }
1640 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1641
1642 static int __init apic_set_verbosity(char *str)
1643 {
1644         if (strcmp("debug", str) == 0)
1645                 apic_verbosity = APIC_DEBUG;
1646         else if (strcmp("verbose", str) == 0)
1647                 apic_verbosity = APIC_VERBOSE;
1648         return 1;
1649 }
1650 __setup("apic=", apic_set_verbosity);
1651