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