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