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