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