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