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