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