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