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