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