Merge branch 'linus' into x86/x2apic
[cascardo/linux.git] / arch / x86 / kernel / apic_64.c
1 /*
2  *      Local APIC handling, local APIC timers
3  *
4  *      (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5  *
6  *      Fixes
7  *      Maciej W. Rozycki       :       Bits for genuine 82489DX APICs;
8  *                                      thanks to Eric Gilmore
9  *                                      and Rolf G. Tews
10  *                                      for testing these extensively.
11  *      Maciej W. Rozycki       :       Various updates and fixes.
12  *      Mikael Pettersson       :       Power Management for UP-APIC.
13  *      Pavel Machek and
14  *      Mikael Pettersson       :       PM converted to driver model.
15  */
16
17 #include <linux/init.h>
18
19 #include <linux/mm.h>
20 #include <linux/delay.h>
21 #include <linux/bootmem.h>
22 #include <linux/interrupt.h>
23 #include <linux/mc146818rtc.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/sysdev.h>
26 #include <linux/ioport.h>
27 #include <linux/clockchips.h>
28 #include <linux/acpi_pmtmr.h>
29 #include <linux/module.h>
30 #include <linux/dmar.h>
31
32 #include <asm/atomic.h>
33 #include <asm/smp.h>
34 #include <asm/mtrr.h>
35 #include <asm/mpspec.h>
36 #include <asm/hpet.h>
37 #include <asm/pgalloc.h>
38 #include <asm/nmi.h>
39 #include <asm/idle.h>
40 #include <asm/proto.h>
41 #include <asm/timex.h>
42 #include <asm/apic.h>
43 #include <asm/i8259.h>
44
45 #include <mach_ipi.h>
46 #include <mach_apic.h>
47
48 static int disable_apic_timer __cpuinitdata;
49 static int apic_calibrate_pmtmr __initdata;
50 int disable_apic;
51 int disable_x2apic;
52 int x2apic;
53
54 /* x2apic enabled before OS handover */
55 int x2apic_preenabled;
56
57 /* Local APIC timer works in C2 */
58 int local_apic_timer_c2_ok;
59 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
60
61 /*
62  * Debug level, exported for io_apic.c
63  */
64 unsigned int apic_verbosity;
65
66 /* Have we found an MP table */
67 int smp_found_config;
68
69 static struct resource lapic_resource = {
70         .name = "Local APIC",
71         .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
72 };
73
74 static unsigned int calibration_result;
75
76 static int lapic_next_event(unsigned long delta,
77                             struct clock_event_device *evt);
78 static void lapic_timer_setup(enum clock_event_mode mode,
79                               struct clock_event_device *evt);
80 static void lapic_timer_broadcast(cpumask_t mask);
81 static void apic_pm_activate(void);
82
83 static struct clock_event_device lapic_clockevent = {
84         .name           = "lapic",
85         .features       = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
86                         | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
87         .shift          = 32,
88         .set_mode       = lapic_timer_setup,
89         .set_next_event = lapic_next_event,
90         .broadcast      = lapic_timer_broadcast,
91         .rating         = 100,
92         .irq            = -1,
93 };
94 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
95
96 static unsigned long apic_phys;
97
98 unsigned long mp_lapic_addr;
99
100 unsigned int __cpuinitdata maxcpus = NR_CPUS;
101 /*
102  * Get the LAPIC version
103  */
104 static inline int lapic_get_version(void)
105 {
106         return GET_APIC_VERSION(apic_read(APIC_LVR));
107 }
108
109 /*
110  * Check, if the APIC is integrated or a seperate chip
111  */
112 static inline int lapic_is_integrated(void)
113 {
114         return 1;
115 }
116
117 /*
118  * Check, whether this is a modern or a first generation APIC
119  */
120 static int modern_apic(void)
121 {
122         /* AMD systems use old APIC versions, so check the CPU */
123         if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
124             boot_cpu_data.x86 >= 0xf)
125                 return 1;
126         return lapic_get_version() >= 0x14;
127 }
128
129 void xapic_wait_icr_idle(void)
130 {
131         while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
132                 cpu_relax();
133 }
134
135 u32 safe_xapic_wait_icr_idle(void)
136 {
137         u32 send_status;
138         int timeout;
139
140         timeout = 0;
141         do {
142                 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
143                 if (!send_status)
144                         break;
145                 udelay(100);
146         } while (timeout++ < 1000);
147
148         return send_status;
149 }
150
151 void xapic_icr_write(u32 low, u32 id)
152 {
153         apic_write(APIC_ICR2, id << 24);
154         apic_write(APIC_ICR, low);
155 }
156
157 u64 xapic_icr_read(void)
158 {
159         u32 icr1, icr2;
160
161         icr2 = apic_read(APIC_ICR2);
162         icr1 = apic_read(APIC_ICR);
163
164         return (icr1 | ((u64)icr2 << 32));
165 }
166
167 static struct apic_ops xapic_ops = {
168         .read = native_apic_mem_read,
169         .write = native_apic_mem_write,
170         .icr_read = xapic_icr_read,
171         .icr_write = xapic_icr_write,
172         .wait_icr_idle = xapic_wait_icr_idle,
173         .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
174 };
175
176 struct apic_ops __read_mostly *apic_ops = &xapic_ops;
177
178 EXPORT_SYMBOL_GPL(apic_ops);
179
180 static void x2apic_wait_icr_idle(void)
181 {
182         /* no need to wait for icr idle in x2apic */
183         return;
184 }
185
186 static u32 safe_x2apic_wait_icr_idle(void)
187 {
188         /* no need to wait for icr idle in x2apic */
189         return 0;
190 }
191
192 void x2apic_icr_write(u32 low, u32 id)
193 {
194         wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
195 }
196
197 u64 x2apic_icr_read(void)
198 {
199         unsigned long val;
200
201         rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
202         return val;
203 }
204
205 static struct apic_ops x2apic_ops = {
206         .read = native_apic_msr_read,
207         .write = native_apic_msr_write,
208         .icr_read = x2apic_icr_read,
209         .icr_write = x2apic_icr_write,
210         .wait_icr_idle = x2apic_wait_icr_idle,
211         .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
212 };
213
214 /**
215  * enable_NMI_through_LVT0 - enable NMI through local vector table 0
216  */
217 void __cpuinit enable_NMI_through_LVT0(void)
218 {
219         unsigned int v;
220
221         /* unmask and set to NMI */
222         v = APIC_DM_NMI;
223         apic_write(APIC_LVT0, v);
224 }
225
226 /**
227  * lapic_get_maxlvt - get the maximum number of local vector table entries
228  */
229 int lapic_get_maxlvt(void)
230 {
231         unsigned int v, maxlvt;
232
233         v = apic_read(APIC_LVR);
234         maxlvt = GET_APIC_MAXLVT(v);
235         return maxlvt;
236 }
237
238 /*
239  * This function sets up the local APIC timer, with a timeout of
240  * 'clocks' APIC bus clock. During calibration we actually call
241  * this function twice on the boot CPU, once with a bogus timeout
242  * value, second time for real. The other (noncalibrating) CPUs
243  * call this function only once, with the real, calibrated value.
244  *
245  * We do reads before writes even if unnecessary, to get around the
246  * P5 APIC double write bug.
247  */
248
249 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
250 {
251         unsigned int lvtt_value, tmp_value;
252
253         lvtt_value = LOCAL_TIMER_VECTOR;
254         if (!oneshot)
255                 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
256         if (!irqen)
257                 lvtt_value |= APIC_LVT_MASKED;
258
259         apic_write(APIC_LVTT, lvtt_value);
260
261         /*
262          * Divide PICLK by 16
263          */
264         tmp_value = apic_read(APIC_TDCR);
265         apic_write(APIC_TDCR, (tmp_value
266                                 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
267                                 | APIC_TDR_DIV_16);
268
269         if (!oneshot)
270                 apic_write(APIC_TMICT, clocks);
271 }
272
273 /*
274  * Setup extended LVT, AMD specific (K8, family 10h)
275  *
276  * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
277  * MCE interrupts are supported. Thus MCE offset must be set to 0.
278  */
279
280 #define APIC_EILVT_LVTOFF_MCE 0
281 #define APIC_EILVT_LVTOFF_IBS 1
282
283 static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
284 {
285         unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
286         unsigned int  v   = (mask << 16) | (msg_type << 8) | vector;
287
288         apic_write(reg, v);
289 }
290
291 u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
292 {
293         setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
294         return APIC_EILVT_LVTOFF_MCE;
295 }
296
297 u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
298 {
299         setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
300         return APIC_EILVT_LVTOFF_IBS;
301 }
302
303 /*
304  * Program the next event, relative to now
305  */
306 static int lapic_next_event(unsigned long delta,
307                             struct clock_event_device *evt)
308 {
309         apic_write(APIC_TMICT, delta);
310         return 0;
311 }
312
313 /*
314  * Setup the lapic timer in periodic or oneshot mode
315  */
316 static void lapic_timer_setup(enum clock_event_mode mode,
317                               struct clock_event_device *evt)
318 {
319         unsigned long flags;
320         unsigned int v;
321
322         /* Lapic used as dummy for broadcast ? */
323         if (evt->features & CLOCK_EVT_FEAT_DUMMY)
324                 return;
325
326         local_irq_save(flags);
327
328         switch (mode) {
329         case CLOCK_EVT_MODE_PERIODIC:
330         case CLOCK_EVT_MODE_ONESHOT:
331                 __setup_APIC_LVTT(calibration_result,
332                                   mode != CLOCK_EVT_MODE_PERIODIC, 1);
333                 break;
334         case CLOCK_EVT_MODE_UNUSED:
335         case CLOCK_EVT_MODE_SHUTDOWN:
336                 v = apic_read(APIC_LVTT);
337                 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
338                 apic_write(APIC_LVTT, v);
339                 break;
340         case CLOCK_EVT_MODE_RESUME:
341                 /* Nothing to do here */
342                 break;
343         }
344
345         local_irq_restore(flags);
346 }
347
348 /*
349  * Local APIC timer broadcast function
350  */
351 static void lapic_timer_broadcast(cpumask_t mask)
352 {
353 #ifdef CONFIG_SMP
354         send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
355 #endif
356 }
357
358 /*
359  * Setup the local APIC timer for this CPU. Copy the initilized values
360  * of the boot CPU and register the clock event in the framework.
361  */
362 static void setup_APIC_timer(void)
363 {
364         struct clock_event_device *levt = &__get_cpu_var(lapic_events);
365
366         memcpy(levt, &lapic_clockevent, sizeof(*levt));
367         levt->cpumask = cpumask_of_cpu(smp_processor_id());
368
369         clockevents_register_device(levt);
370 }
371
372 /*
373  * In this function we calibrate APIC bus clocks to the external
374  * timer. Unfortunately we cannot use jiffies and the timer irq
375  * to calibrate, since some later bootup code depends on getting
376  * the first irq? Ugh.
377  *
378  * We want to do the calibration only once since we
379  * want to have local timer irqs syncron. CPUs connected
380  * by the same APIC bus have the very same bus frequency.
381  * And we want to have irqs off anyways, no accidental
382  * APIC irq that way.
383  */
384
385 #define TICK_COUNT 100000000
386
387 static int __init calibrate_APIC_clock(void)
388 {
389         unsigned apic, apic_start;
390         unsigned long tsc, tsc_start;
391         int result;
392
393         local_irq_disable();
394
395         /*
396          * Put whatever arbitrary (but long enough) timeout
397          * value into the APIC clock, we just want to get the
398          * counter running for calibration.
399          *
400          * No interrupt enable !
401          */
402         __setup_APIC_LVTT(250000000, 0, 0);
403
404         apic_start = apic_read(APIC_TMCCT);
405 #ifdef CONFIG_X86_PM_TIMER
406         if (apic_calibrate_pmtmr && pmtmr_ioport) {
407                 pmtimer_wait(5000);  /* 5ms wait */
408                 apic = apic_read(APIC_TMCCT);
409                 result = (apic_start - apic) * 1000L / 5;
410         } else
411 #endif
412         {
413                 rdtscll(tsc_start);
414
415                 do {
416                         apic = apic_read(APIC_TMCCT);
417                         rdtscll(tsc);
418                 } while ((tsc - tsc_start) < TICK_COUNT &&
419                                 (apic_start - apic) < TICK_COUNT);
420
421                 result = (apic_start - apic) * 1000L * tsc_khz /
422                                         (tsc - tsc_start);
423         }
424
425         local_irq_enable();
426
427         printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
428
429         printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
430                 result / 1000 / 1000, result / 1000 % 1000);
431
432         /* Calculate the scaled math multiplication factor */
433         lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
434                                        lapic_clockevent.shift);
435         lapic_clockevent.max_delta_ns =
436                 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
437         lapic_clockevent.min_delta_ns =
438                 clockevent_delta2ns(0xF, &lapic_clockevent);
439
440         calibration_result = result / HZ;
441
442         /*
443          * Do a sanity check on the APIC calibration result
444          */
445         if (calibration_result < (1000000 / HZ)) {
446                 printk(KERN_WARNING
447                         "APIC frequency too slow, disabling apic timer\n");
448                 return -1;
449         }
450
451         return 0;
452 }
453
454 /*
455  * Setup the boot APIC
456  *
457  * Calibrate and verify the result.
458  */
459 void __init setup_boot_APIC_clock(void)
460 {
461         /*
462          * The local apic timer can be disabled via the kernel commandline.
463          * Register the lapic timer as a dummy clock event source on SMP
464          * systems, so the broadcast mechanism is used. On UP systems simply
465          * ignore it.
466          */
467         if (disable_apic_timer) {
468                 printk(KERN_INFO "Disabling APIC timer\n");
469                 /* No broadcast on UP ! */
470                 if (num_possible_cpus() > 1) {
471                         lapic_clockevent.mult = 1;
472                         setup_APIC_timer();
473                 }
474                 return;
475         }
476
477         printk(KERN_INFO "Using local APIC timer interrupts.\n");
478         if (calibrate_APIC_clock()) {
479                 /* No broadcast on UP ! */
480                 if (num_possible_cpus() > 1)
481                         setup_APIC_timer();
482                 return;
483         }
484
485         /*
486          * If nmi_watchdog is set to IO_APIC, we need the
487          * PIT/HPET going.  Otherwise register lapic as a dummy
488          * device.
489          */
490         if (nmi_watchdog != NMI_IO_APIC)
491                 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
492         else
493                 printk(KERN_WARNING "APIC timer registered as dummy,"
494                         " due to nmi_watchdog=%d!\n", nmi_watchdog);
495
496         setup_APIC_timer();
497 }
498
499 void __cpuinit setup_secondary_APIC_clock(void)
500 {
501         setup_APIC_timer();
502 }
503
504 /*
505  * The guts of the apic timer interrupt
506  */
507 static void local_apic_timer_interrupt(void)
508 {
509         int cpu = smp_processor_id();
510         struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
511
512         /*
513          * Normally we should not be here till LAPIC has been initialized but
514          * in some cases like kdump, its possible that there is a pending LAPIC
515          * timer interrupt from previous kernel's context and is delivered in
516          * new kernel the moment interrupts are enabled.
517          *
518          * Interrupts are enabled early and LAPIC is setup much later, hence
519          * its possible that when we get here evt->event_handler is NULL.
520          * Check for event_handler being NULL and discard the interrupt as
521          * spurious.
522          */
523         if (!evt->event_handler) {
524                 printk(KERN_WARNING
525                        "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
526                 /* Switch it off */
527                 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
528                 return;
529         }
530
531         /*
532          * the NMI deadlock-detector uses this.
533          */
534         add_pda(apic_timer_irqs, 1);
535
536         evt->event_handler(evt);
537 }
538
539 /*
540  * Local APIC timer interrupt. This is the most natural way for doing
541  * local interrupts, but local timer interrupts can be emulated by
542  * broadcast interrupts too. [in case the hw doesn't support APIC timers]
543  *
544  * [ if a single-CPU system runs an SMP kernel then we call the local
545  *   interrupt as well. Thus we cannot inline the local irq ... ]
546  */
547 void smp_apic_timer_interrupt(struct pt_regs *regs)
548 {
549         struct pt_regs *old_regs = set_irq_regs(regs);
550
551         /*
552          * NOTE! We'd better ACK the irq immediately,
553          * because timer handling can be slow.
554          */
555         ack_APIC_irq();
556         /*
557          * update_process_times() expects us to have done irq_enter().
558          * Besides, if we don't timer interrupts ignore the global
559          * interrupt lock, which is the WrongThing (tm) to do.
560          */
561         exit_idle();
562         irq_enter();
563         local_apic_timer_interrupt();
564         irq_exit();
565         set_irq_regs(old_regs);
566 }
567
568 int setup_profiling_timer(unsigned int multiplier)
569 {
570         return -EINVAL;
571 }
572
573
574 /*
575  * Local APIC start and shutdown
576  */
577
578 /**
579  * clear_local_APIC - shutdown the local APIC
580  *
581  * This is called, when a CPU is disabled and before rebooting, so the state of
582  * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
583  * leftovers during boot.
584  */
585 void clear_local_APIC(void)
586 {
587         int maxlvt;
588         u32 v;
589
590         /* APIC hasn't been mapped yet */
591         if (!apic_phys)
592                 return;
593
594         maxlvt = lapic_get_maxlvt();
595         /*
596          * Masking an LVT entry can trigger a local APIC error
597          * if the vector is zero. Mask LVTERR first to prevent this.
598          */
599         if (maxlvt >= 3) {
600                 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
601                 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
602         }
603         /*
604          * Careful: we have to set masks only first to deassert
605          * any level-triggered sources.
606          */
607         v = apic_read(APIC_LVTT);
608         apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
609         v = apic_read(APIC_LVT0);
610         apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
611         v = apic_read(APIC_LVT1);
612         apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
613         if (maxlvt >= 4) {
614                 v = apic_read(APIC_LVTPC);
615                 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
616         }
617
618         /*
619          * Clean APIC state for other OSs:
620          */
621         apic_write(APIC_LVTT, APIC_LVT_MASKED);
622         apic_write(APIC_LVT0, APIC_LVT_MASKED);
623         apic_write(APIC_LVT1, APIC_LVT_MASKED);
624         if (maxlvt >= 3)
625                 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
626         if (maxlvt >= 4)
627                 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
628         apic_write(APIC_ESR, 0);
629         apic_read(APIC_ESR);
630 }
631
632 /**
633  * disable_local_APIC - clear and disable the local APIC
634  */
635 void disable_local_APIC(void)
636 {
637         unsigned int value;
638
639         clear_local_APIC();
640
641         /*
642          * Disable APIC (implies clearing of registers
643          * for 82489DX!).
644          */
645         value = apic_read(APIC_SPIV);
646         value &= ~APIC_SPIV_APIC_ENABLED;
647         apic_write(APIC_SPIV, value);
648 }
649
650 void lapic_shutdown(void)
651 {
652         unsigned long flags;
653
654         if (!cpu_has_apic)
655                 return;
656
657         local_irq_save(flags);
658
659         disable_local_APIC();
660
661         local_irq_restore(flags);
662 }
663
664 /*
665  * This is to verify that we're looking at a real local APIC.
666  * Check these against your board if the CPUs aren't getting
667  * started for no apparent reason.
668  */
669 int __init verify_local_APIC(void)
670 {
671         unsigned int reg0, reg1;
672
673         /*
674          * The version register is read-only in a real APIC.
675          */
676         reg0 = apic_read(APIC_LVR);
677         apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
678         apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
679         reg1 = apic_read(APIC_LVR);
680         apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
681
682         /*
683          * The two version reads above should print the same
684          * numbers.  If the second one is different, then we
685          * poke at a non-APIC.
686          */
687         if (reg1 != reg0)
688                 return 0;
689
690         /*
691          * Check if the version looks reasonably.
692          */
693         reg1 = GET_APIC_VERSION(reg0);
694         if (reg1 == 0x00 || reg1 == 0xff)
695                 return 0;
696         reg1 = lapic_get_maxlvt();
697         if (reg1 < 0x02 || reg1 == 0xff)
698                 return 0;
699
700         /*
701          * The ID register is read/write in a real APIC.
702          */
703         reg0 = apic_read(APIC_ID);
704         apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
705         apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
706         reg1 = apic_read(APIC_ID);
707         apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
708         apic_write(APIC_ID, reg0);
709         if (reg1 != (reg0 ^ APIC_ID_MASK))
710                 return 0;
711
712         /*
713          * The next two are just to see if we have sane values.
714          * They're only really relevant if we're in Virtual Wire
715          * compatibility mode, but most boxes are anymore.
716          */
717         reg0 = apic_read(APIC_LVT0);
718         apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
719         reg1 = apic_read(APIC_LVT1);
720         apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
721
722         return 1;
723 }
724
725 /**
726  * sync_Arb_IDs - synchronize APIC bus arbitration IDs
727  */
728 void __init sync_Arb_IDs(void)
729 {
730         /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
731         if (modern_apic())
732                 return;
733
734         /*
735          * Wait for idle.
736          */
737         apic_wait_icr_idle();
738
739         apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
740         apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
741                                 | APIC_DM_INIT);
742 }
743
744 /*
745  * An initial setup of the virtual wire mode.
746  */
747 void __init init_bsp_APIC(void)
748 {
749         unsigned int value;
750
751         /*
752          * Don't do the setup now if we have a SMP BIOS as the
753          * through-I/O-APIC virtual wire mode might be active.
754          */
755         if (smp_found_config || !cpu_has_apic)
756                 return;
757
758         value = apic_read(APIC_LVR);
759
760         /*
761          * Do not trust the local APIC being empty at bootup.
762          */
763         clear_local_APIC();
764
765         /*
766          * Enable APIC.
767          */
768         value = apic_read(APIC_SPIV);
769         value &= ~APIC_VECTOR_MASK;
770         value |= APIC_SPIV_APIC_ENABLED;
771         value |= APIC_SPIV_FOCUS_DISABLED;
772         value |= SPURIOUS_APIC_VECTOR;
773         apic_write(APIC_SPIV, value);
774
775         /*
776          * Set up the virtual wire mode.
777          */
778         apic_write(APIC_LVT0, APIC_DM_EXTINT);
779         value = APIC_DM_NMI;
780         apic_write(APIC_LVT1, value);
781 }
782
783 /**
784  * setup_local_APIC - setup the local APIC
785  */
786 void __cpuinit setup_local_APIC(void)
787 {
788         unsigned int value;
789         int i, j;
790
791         preempt_disable();
792         value = apic_read(APIC_LVR);
793
794         BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
795
796         /*
797          * Double-check whether this APIC is really registered.
798          * This is meaningless in clustered apic mode, so we skip it.
799          */
800         if (!apic_id_registered())
801                 BUG();
802
803         /*
804          * Intel recommends to set DFR, LDR and TPR before enabling
805          * an APIC.  See e.g. "AP-388 82489DX User's Manual" (Intel
806          * document number 292116).  So here it goes...
807          */
808         init_apic_ldr();
809
810         /*
811          * Set Task Priority to 'accept all'. We never change this
812          * later on.
813          */
814         value = apic_read(APIC_TASKPRI);
815         value &= ~APIC_TPRI_MASK;
816         apic_write(APIC_TASKPRI, value);
817
818         /*
819          * After a crash, we no longer service the interrupts and a pending
820          * interrupt from previous kernel might still have ISR bit set.
821          *
822          * Most probably by now CPU has serviced that pending interrupt and
823          * it might not have done the ack_APIC_irq() because it thought,
824          * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
825          * does not clear the ISR bit and cpu thinks it has already serivced
826          * the interrupt. Hence a vector might get locked. It was noticed
827          * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
828          */
829         for (i = APIC_ISR_NR - 1; i >= 0; i--) {
830                 value = apic_read(APIC_ISR + i*0x10);
831                 for (j = 31; j >= 0; j--) {
832                         if (value & (1<<j))
833                                 ack_APIC_irq();
834                 }
835         }
836
837         /*
838          * Now that we are all set up, enable the APIC
839          */
840         value = apic_read(APIC_SPIV);
841         value &= ~APIC_VECTOR_MASK;
842         /*
843          * Enable APIC
844          */
845         value |= APIC_SPIV_APIC_ENABLED;
846
847         /* We always use processor focus */
848
849         /*
850          * Set spurious IRQ vector
851          */
852         value |= SPURIOUS_APIC_VECTOR;
853         apic_write(APIC_SPIV, value);
854
855         /*
856          * Set up LVT0, LVT1:
857          *
858          * set up through-local-APIC on the BP's LINT0. This is not
859          * strictly necessary in pure symmetric-IO mode, but sometimes
860          * we delegate interrupts to the 8259A.
861          */
862         /*
863          * TODO: set up through-local-APIC from through-I/O-APIC? --macro
864          */
865         value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
866         if (!smp_processor_id() && !value) {
867                 value = APIC_DM_EXTINT;
868                 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
869                             smp_processor_id());
870         } else {
871                 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
872                 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
873                             smp_processor_id());
874         }
875         apic_write(APIC_LVT0, value);
876
877         /*
878          * only the BP should see the LINT1 NMI signal, obviously.
879          */
880         if (!smp_processor_id())
881                 value = APIC_DM_NMI;
882         else
883                 value = APIC_DM_NMI | APIC_LVT_MASKED;
884         apic_write(APIC_LVT1, value);
885         preempt_enable();
886 }
887
888 static void __cpuinit lapic_setup_esr(void)
889 {
890         unsigned maxlvt = lapic_get_maxlvt();
891
892         apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
893         /*
894          * spec says clear errors after enabling vector.
895          */
896         if (maxlvt > 3)
897                 apic_write(APIC_ESR, 0);
898 }
899
900 void __cpuinit end_local_APIC_setup(void)
901 {
902         lapic_setup_esr();
903         setup_apic_nmi_watchdog(NULL);
904         apic_pm_activate();
905 }
906
907 void check_x2apic(void)
908 {
909         int msr, msr2;
910
911         rdmsr(MSR_IA32_APICBASE, msr, msr2);
912
913         if (msr & X2APIC_ENABLE) {
914                 printk("x2apic enabled by BIOS, switching to x2apic ops\n");
915                 x2apic_preenabled = x2apic = 1;
916                 apic_ops = &x2apic_ops;
917         }
918 }
919
920 void enable_x2apic(void)
921 {
922         int msr, msr2;
923
924         rdmsr(MSR_IA32_APICBASE, msr, msr2);
925         if (!(msr & X2APIC_ENABLE)) {
926                 printk("Enabling x2apic\n");
927                 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
928         }
929 }
930
931 void enable_IR_x2apic(void)
932 {
933 #ifdef CONFIG_INTR_REMAP
934         int ret;
935         unsigned long flags;
936
937         if (!cpu_has_x2apic)
938                 return;
939
940         if (!x2apic_preenabled && disable_x2apic) {
941                 printk(KERN_INFO
942                        "Skipped enabling x2apic and Interrupt-remapping "
943                        "because of nox2apic\n");
944                 return;
945         }
946
947         if (x2apic_preenabled && disable_x2apic)
948                 panic("Bios already enabled x2apic, can't enforce nox2apic");
949
950         if (!x2apic_preenabled && skip_ioapic_setup) {
951                 printk(KERN_INFO
952                        "Skipped enabling x2apic and Interrupt-remapping "
953                        "because of skipping io-apic setup\n");
954                 return;
955         }
956
957         ret = dmar_table_init();
958         if (ret) {
959                 printk(KERN_INFO
960                        "dmar_table_init() failed with %d:\n", ret);
961
962                 if (x2apic_preenabled)
963                         panic("x2apic enabled by bios. But IR enabling failed");
964                 else
965                         printk(KERN_INFO
966                                "Not enabling x2apic,Intr-remapping\n");
967                 return;
968         }
969
970         local_irq_save(flags);
971         mask_8259A();
972         save_mask_IO_APIC_setup();
973
974         ret = enable_intr_remapping(1);
975
976         if (ret && x2apic_preenabled) {
977                 local_irq_restore(flags);
978                 panic("x2apic enabled by bios. But IR enabling failed");
979         }
980
981         if (ret)
982                 goto end;
983
984         if (!x2apic) {
985                 x2apic = 1;
986                 apic_ops = &x2apic_ops;
987                 enable_x2apic();
988         }
989 end:
990         if (ret)
991                 /*
992                  * IR enabling failed
993                  */
994                 restore_IO_APIC_setup();
995         else
996                 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
997
998         unmask_8259A();
999         local_irq_restore(flags);
1000
1001         if (!ret) {
1002                 if (!x2apic_preenabled)
1003                         printk(KERN_INFO
1004                                "Enabled x2apic and interrupt-remapping\n");
1005                 else
1006                         printk(KERN_INFO
1007                                "Enabled Interrupt-remapping\n");
1008         } else
1009                 printk(KERN_ERR
1010                        "Failed to enable Interrupt-remapping and x2apic\n");
1011 #else
1012         if (!cpu_has_x2apic)
1013                 return;
1014
1015         if (x2apic_preenabled)
1016                 panic("x2apic enabled prior OS handover,"
1017                       " enable CONFIG_INTR_REMAP");
1018
1019         printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1020                " and x2apic\n");
1021 #endif
1022
1023         return;
1024 }
1025
1026 /*
1027  * Detect and enable local APICs on non-SMP boards.
1028  * Original code written by Keir Fraser.
1029  * On AMD64 we trust the BIOS - if it says no APIC it is likely
1030  * not correctly set up (usually the APIC timer won't work etc.)
1031  */
1032 static int __init detect_init_APIC(void)
1033 {
1034         if (!cpu_has_apic) {
1035                 printk(KERN_INFO "No local APIC present\n");
1036                 return -1;
1037         }
1038
1039         mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1040         boot_cpu_physical_apicid = 0;
1041         return 0;
1042 }
1043
1044 void __init early_init_lapic_mapping(void)
1045 {
1046         unsigned long phys_addr;
1047
1048         /*
1049          * If no local APIC can be found then go out
1050          * : it means there is no mpatable and MADT
1051          */
1052         if (!smp_found_config)
1053                 return;
1054
1055         phys_addr = mp_lapic_addr;
1056
1057         set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
1058         apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1059                     APIC_BASE, phys_addr);
1060
1061         /*
1062          * Fetch the APIC ID of the BSP in case we have a
1063          * default configuration (or the MP table is broken).
1064          */
1065         boot_cpu_physical_apicid = read_apic_id();
1066 }
1067
1068 /**
1069  * init_apic_mappings - initialize APIC mappings
1070  */
1071 void __init init_apic_mappings(void)
1072 {
1073         if (x2apic) {
1074                 boot_cpu_physical_apicid = read_apic_id();
1075                 return;
1076         }
1077
1078         /*
1079          * If no local APIC can be found then set up a fake all
1080          * zeroes page to simulate the local APIC and another
1081          * one for the IO-APIC.
1082          */
1083         if (!smp_found_config && detect_init_APIC()) {
1084                 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1085                 apic_phys = __pa(apic_phys);
1086         } else
1087                 apic_phys = mp_lapic_addr;
1088
1089         set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1090         apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1091                                 APIC_BASE, apic_phys);
1092
1093         /*
1094          * Fetch the APIC ID of the BSP in case we have a
1095          * default configuration (or the MP table is broken).
1096          */
1097         boot_cpu_physical_apicid = read_apic_id();
1098 }
1099
1100 /*
1101  * This initializes the IO-APIC and APIC hardware if this is
1102  * a UP kernel.
1103  */
1104 int __init APIC_init_uniprocessor(void)
1105 {
1106         if (disable_apic) {
1107                 printk(KERN_INFO "Apic disabled\n");
1108                 return -1;
1109         }
1110         if (!cpu_has_apic) {
1111                 disable_apic = 1;
1112                 printk(KERN_INFO "Apic disabled by BIOS\n");
1113                 return -1;
1114         }
1115
1116         enable_IR_x2apic();
1117         setup_apic_routing();
1118
1119         verify_local_APIC();
1120
1121         connect_bsp_APIC();
1122
1123         physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1124         apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1125
1126         setup_local_APIC();
1127
1128         /*
1129          * Now enable IO-APICs, actually call clear_IO_APIC
1130          * We need clear_IO_APIC before enabling vector on BP
1131          */
1132         if (!skip_ioapic_setup && nr_ioapics)
1133                 enable_IO_APIC();
1134
1135         if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1136                 localise_nmi_watchdog();
1137         end_local_APIC_setup();
1138
1139         if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1140                 setup_IO_APIC();
1141         else
1142                 nr_ioapics = 0;
1143         setup_boot_APIC_clock();
1144         check_nmi_watchdog();
1145         return 0;
1146 }
1147
1148 /*
1149  * Local APIC interrupts
1150  */
1151
1152 /*
1153  * This interrupt should _never_ happen with our APIC/SMP architecture
1154  */
1155 asmlinkage void smp_spurious_interrupt(void)
1156 {
1157         unsigned int v;
1158         exit_idle();
1159         irq_enter();
1160         /*
1161          * Check if this really is a spurious interrupt and ACK it
1162          * if it is a vectored one.  Just in case...
1163          * Spurious interrupts should not be ACKed.
1164          */
1165         v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1166         if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1167                 ack_APIC_irq();
1168
1169         add_pda(irq_spurious_count, 1);
1170         irq_exit();
1171 }
1172
1173 /*
1174  * This interrupt should never happen with our APIC/SMP architecture
1175  */
1176 asmlinkage void smp_error_interrupt(void)
1177 {
1178         unsigned int v, v1;
1179
1180         exit_idle();
1181         irq_enter();
1182         /* First tickle the hardware, only then report what went on. -- REW */
1183         v = apic_read(APIC_ESR);
1184         apic_write(APIC_ESR, 0);
1185         v1 = apic_read(APIC_ESR);
1186         ack_APIC_irq();
1187         atomic_inc(&irq_err_count);
1188
1189         /* Here is what the APIC error bits mean:
1190            0: Send CS error
1191            1: Receive CS error
1192            2: Send accept error
1193            3: Receive accept error
1194            4: Reserved
1195            5: Send illegal vector
1196            6: Received illegal vector
1197            7: Illegal register address
1198         */
1199         printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1200                 smp_processor_id(), v , v1);
1201         irq_exit();
1202 }
1203
1204 /**
1205  *  * connect_bsp_APIC - attach the APIC to the interrupt system
1206  *   */
1207 void __init connect_bsp_APIC(void)
1208 {
1209         enable_apic_mode();
1210 }
1211
1212 void disconnect_bsp_APIC(int virt_wire_setup)
1213 {
1214         /* Go back to Virtual Wire compatibility mode */
1215         unsigned long value;
1216
1217         /* For the spurious interrupt use vector F, and enable it */
1218         value = apic_read(APIC_SPIV);
1219         value &= ~APIC_VECTOR_MASK;
1220         value |= APIC_SPIV_APIC_ENABLED;
1221         value |= 0xf;
1222         apic_write(APIC_SPIV, value);
1223
1224         if (!virt_wire_setup) {
1225                 /*
1226                  * For LVT0 make it edge triggered, active high,
1227                  * external and enabled
1228                  */
1229                 value = apic_read(APIC_LVT0);
1230                 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1231                         APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1232                         APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1233                 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1234                 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1235                 apic_write(APIC_LVT0, value);
1236         } else {
1237                 /* Disable LVT0 */
1238                 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1239         }
1240
1241         /* For LVT1 make it edge triggered, active high, nmi and enabled */
1242         value = apic_read(APIC_LVT1);
1243         value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1244                         APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1245                         APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1246         value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1247         value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1248         apic_write(APIC_LVT1, value);
1249 }
1250
1251 void __cpuinit generic_processor_info(int apicid, int version)
1252 {
1253         int cpu;
1254         cpumask_t tmp_map;
1255
1256         if (num_processors >= NR_CPUS) {
1257                 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1258                        " Processor ignored.\n", NR_CPUS);
1259                 return;
1260         }
1261
1262         if (num_processors >= maxcpus) {
1263                 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1264                        " Processor ignored.\n", maxcpus);
1265                 return;
1266         }
1267
1268         num_processors++;
1269         cpus_complement(tmp_map, cpu_present_map);
1270         cpu = first_cpu(tmp_map);
1271
1272         physid_set(apicid, phys_cpu_present_map);
1273         if (apicid == boot_cpu_physical_apicid) {
1274                 /*
1275                  * x86_bios_cpu_apicid is required to have processors listed
1276                  * in same order as logical cpu numbers. Hence the first
1277                  * entry is BSP, and so on.
1278                  */
1279                 cpu = 0;
1280         }
1281         if (apicid > max_physical_apicid)
1282                 max_physical_apicid = apicid;
1283
1284         /* are we being called early in kernel startup? */
1285         if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1286                 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1287                 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
1288
1289                 cpu_to_apicid[cpu] = apicid;
1290                 bios_cpu_apicid[cpu] = apicid;
1291         } else {
1292                 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1293                 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1294         }
1295
1296         cpu_set(cpu, cpu_possible_map);
1297         cpu_set(cpu, cpu_present_map);
1298 }
1299
1300 int hard_smp_processor_id(void)
1301 {
1302         return read_apic_id();
1303 }
1304
1305 /*
1306  * Power management
1307  */
1308 #ifdef CONFIG_PM
1309
1310 static struct {
1311         /* 'active' is true if the local APIC was enabled by us and
1312            not the BIOS; this signifies that we are also responsible
1313            for disabling it before entering apm/acpi suspend */
1314         int active;
1315         /* r/w apic fields */
1316         unsigned int apic_id;
1317         unsigned int apic_taskpri;
1318         unsigned int apic_ldr;
1319         unsigned int apic_dfr;
1320         unsigned int apic_spiv;
1321         unsigned int apic_lvtt;
1322         unsigned int apic_lvtpc;
1323         unsigned int apic_lvt0;
1324         unsigned int apic_lvt1;
1325         unsigned int apic_lvterr;
1326         unsigned int apic_tmict;
1327         unsigned int apic_tdcr;
1328         unsigned int apic_thmr;
1329 } apic_pm_state;
1330
1331 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1332 {
1333         unsigned long flags;
1334         int maxlvt;
1335
1336         if (!apic_pm_state.active)
1337                 return 0;
1338
1339         maxlvt = lapic_get_maxlvt();
1340
1341         apic_pm_state.apic_id = apic_read(APIC_ID);
1342         apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1343         apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1344         apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1345         apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1346         apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1347         if (maxlvt >= 4)
1348                 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1349         apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1350         apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1351         apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1352         apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1353         apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1354 #ifdef CONFIG_X86_MCE_INTEL
1355         if (maxlvt >= 5)
1356                 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1357 #endif
1358         local_irq_save(flags);
1359         disable_local_APIC();
1360         local_irq_restore(flags);
1361         return 0;
1362 }
1363
1364 static int lapic_resume(struct sys_device *dev)
1365 {
1366         unsigned int l, h;
1367         unsigned long flags;
1368         int maxlvt;
1369
1370         if (!apic_pm_state.active)
1371                 return 0;
1372
1373         maxlvt = lapic_get_maxlvt();
1374
1375         local_irq_save(flags);
1376         if (!x2apic) {
1377                 rdmsr(MSR_IA32_APICBASE, l, h);
1378                 l &= ~MSR_IA32_APICBASE_BASE;
1379                 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1380                 wrmsr(MSR_IA32_APICBASE, l, h);
1381         } else
1382                 enable_x2apic();
1383
1384         apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1385         apic_write(APIC_ID, apic_pm_state.apic_id);
1386         apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1387         apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1388         apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1389         apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1390         apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1391         apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1392 #ifdef CONFIG_X86_MCE_INTEL
1393         if (maxlvt >= 5)
1394                 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1395 #endif
1396         if (maxlvt >= 4)
1397                 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1398         apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1399         apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1400         apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1401         apic_write(APIC_ESR, 0);
1402         apic_read(APIC_ESR);
1403         apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1404         apic_write(APIC_ESR, 0);
1405         apic_read(APIC_ESR);
1406         local_irq_restore(flags);
1407         return 0;
1408 }
1409
1410 static struct sysdev_class lapic_sysclass = {
1411         .name           = "lapic",
1412         .resume         = lapic_resume,
1413         .suspend        = lapic_suspend,
1414 };
1415
1416 static struct sys_device device_lapic = {
1417         .id     = 0,
1418         .cls    = &lapic_sysclass,
1419 };
1420
1421 static void __cpuinit apic_pm_activate(void)
1422 {
1423         apic_pm_state.active = 1;
1424 }
1425
1426 static int __init init_lapic_sysfs(void)
1427 {
1428         int error;
1429
1430         if (!cpu_has_apic)
1431                 return 0;
1432         /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1433
1434         error = sysdev_class_register(&lapic_sysclass);
1435         if (!error)
1436                 error = sysdev_register(&device_lapic);
1437         return error;
1438 }
1439 device_initcall(init_lapic_sysfs);
1440
1441 #else   /* CONFIG_PM */
1442
1443 static void apic_pm_activate(void) { }
1444
1445 #endif  /* CONFIG_PM */
1446
1447 /*
1448  * apic_is_clustered_box() -- Check if we can expect good TSC
1449  *
1450  * Thus far, the major user of this is IBM's Summit2 series:
1451  *
1452  * Clustered boxes may have unsynced TSC problems if they are
1453  * multi-chassis. Use available data to take a good guess.
1454  * If in doubt, go HPET.
1455  */
1456 __cpuinit int apic_is_clustered_box(void)
1457 {
1458         int i, clusters, zeros;
1459         unsigned id;
1460         u16 *bios_cpu_apicid;
1461         DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1462
1463         /*
1464          * there is not this kind of box with AMD CPU yet.
1465          * Some AMD box with quadcore cpu and 8 sockets apicid
1466          * will be [4, 0x23] or [8, 0x27] could be thought to
1467          * vsmp box still need checking...
1468          */
1469         if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
1470                 return 0;
1471
1472         bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
1473         bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
1474
1475         for (i = 0; i < NR_CPUS; i++) {
1476                 /* are we being called early in kernel startup? */
1477                 if (bios_cpu_apicid) {
1478                         id = bios_cpu_apicid[i];
1479                 }
1480                 else if (i < nr_cpu_ids) {
1481                         if (cpu_present(i))
1482                                 id = per_cpu(x86_bios_cpu_apicid, i);
1483                         else
1484                                 continue;
1485                 }
1486                 else
1487                         break;
1488
1489                 if (id != BAD_APICID)
1490                         __set_bit(APIC_CLUSTERID(id), clustermap);
1491         }
1492
1493         /* Problem:  Partially populated chassis may not have CPUs in some of
1494          * the APIC clusters they have been allocated.  Only present CPUs have
1495          * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
1496          * Since clusters are allocated sequentially, count zeros only if
1497          * they are bounded by ones.
1498          */
1499         clusters = 0;
1500         zeros = 0;
1501         for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1502                 if (test_bit(i, clustermap)) {
1503                         clusters += 1 + zeros;
1504                         zeros = 0;
1505                 } else
1506                         ++zeros;
1507         }
1508
1509         /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
1510          * not guaranteed to be synced between boards
1511          */
1512         if (is_vsmp_box() && clusters > 1)
1513                 return 1;
1514
1515         /*
1516          * If clusters > 2, then should be multi-chassis.
1517          * May have to revisit this when multi-core + hyperthreaded CPUs come
1518          * out, but AFAIK this will work even for them.
1519          */
1520         return (clusters > 2);
1521 }
1522
1523 static __init int setup_nox2apic(char *str)
1524 {
1525         disable_x2apic = 1;
1526         clear_cpu_cap(&boot_cpu_data, X86_FEATURE_X2APIC);
1527         return 0;
1528 }
1529 early_param("nox2apic", setup_nox2apic);
1530
1531
1532 /*
1533  * APIC command line parameters
1534  */
1535 static int __init apic_set_verbosity(char *str)
1536 {
1537         if (str == NULL)  {
1538                 skip_ioapic_setup = 0;
1539                 ioapic_force = 1;
1540                 return 0;
1541         }
1542         if (strcmp("debug", str) == 0)
1543                 apic_verbosity = APIC_DEBUG;
1544         else if (strcmp("verbose", str) == 0)
1545                 apic_verbosity = APIC_VERBOSE;
1546         else {
1547                 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1548                                 " use apic=verbose or apic=debug\n", str);
1549                 return -EINVAL;
1550         }
1551
1552         return 0;
1553 }
1554 early_param("apic", apic_set_verbosity);
1555
1556 static __init int setup_disableapic(char *str)
1557 {
1558         disable_apic = 1;
1559         setup_clear_cpu_cap(X86_FEATURE_APIC);
1560         return 0;
1561 }
1562 early_param("disableapic", setup_disableapic);
1563
1564 /* same as disableapic, for compatibility */
1565 static __init int setup_nolapic(char *str)
1566 {
1567         return setup_disableapic(str);
1568 }
1569 early_param("nolapic", setup_nolapic);
1570
1571 static int __init parse_lapic_timer_c2_ok(char *arg)
1572 {
1573         local_apic_timer_c2_ok = 1;
1574         return 0;
1575 }
1576 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1577
1578 static __init int setup_noapictimer(char *str)
1579 {
1580         if (str[0] != ' ' && str[0] != 0)
1581                 return 0;
1582         disable_apic_timer = 1;
1583         return 1;
1584 }
1585 __setup("noapictimer", setup_noapictimer);
1586
1587 static __init int setup_apicpmtimer(char *s)
1588 {
1589         apic_calibrate_pmtmr = 1;
1590         notsc_setup(NULL);
1591         return 0;
1592 }
1593 __setup("apicpmtimer", setup_apicpmtimer);
1594
1595 static int __init lapic_insert_resource(void)
1596 {
1597         if (!apic_phys)
1598                 return -1;
1599
1600         /* Put local APIC into the resource map. */
1601         lapic_resource.start = apic_phys;
1602         lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1603         insert_resource(&iomem_resource, &lapic_resource);
1604
1605         return 0;
1606 }
1607
1608 /*
1609  * need call insert after e820_reserve_resources()
1610  * that is using request_resource
1611  */
1612 late_initcall(lapic_insert_resource);