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