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