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