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