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