x86: cleanup kernel/setup_64.c
[deliverable/linux.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>
d25bf7e5 26#include <linux/module.h>
39928722 27#include <linux/ioport.h>
ba7eda4c 28#include <linux/clockchips.h>
70a20025 29#include <linux/acpi_pmtmr.h>
1da177e4
LT
30
31#include <asm/atomic.h>
32#include <asm/smp.h>
33#include <asm/mtrr.h>
34#include <asm/mpspec.h>
35#include <asm/pgalloc.h>
36#include <asm/mach_apic.h>
75152114 37#include <asm/nmi.h>
95833c83 38#include <asm/idle.h>
73dea47f
AK
39#include <asm/proto.h>
40#include <asm/timex.h>
2d0c87c3 41#include <asm/hpet.h>
2c8c0e6b 42#include <asm/apic.h>
1da177e4
LT
43
44int apic_verbosity;
fb79d22e 45int disable_apic_timer __cpuinitdata;
bc1d99c1 46static int apic_calibrate_pmtmr __initdata;
0e078e2f 47int disable_apic;
1da177e4 48
2e7c2838
LT
49/* Local APIC timer works in C2? */
50int local_apic_timer_c2_ok;
51EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
52
39928722
AD
53static struct resource lapic_resource = {
54 .name = "Local APIC",
55 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
56};
57
d03030e9
TG
58static unsigned int calibration_result;
59
ba7eda4c
TG
60static int lapic_next_event(unsigned long delta,
61 struct clock_event_device *evt);
62static void lapic_timer_setup(enum clock_event_mode mode,
63 struct clock_event_device *evt);
ba7eda4c 64static void lapic_timer_broadcast(cpumask_t mask);
0e078e2f 65static void apic_pm_activate(void);
ba7eda4c
TG
66
67static struct clock_event_device lapic_clockevent = {
68 .name = "lapic",
69 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
70 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
71 .shift = 32,
72 .set_mode = lapic_timer_setup,
73 .set_next_event = lapic_next_event,
74 .broadcast = lapic_timer_broadcast,
75 .rating = 100,
76 .irq = -1,
77};
78static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
79
0e078e2f
TG
80/*
81 * Get the LAPIC version
82 */
83static inline int lapic_get_version(void)
ba7eda4c 84{
0e078e2f 85 return GET_APIC_VERSION(apic_read(APIC_LVR));
ba7eda4c
TG
86}
87
0e078e2f
TG
88/*
89 * Check, if the APIC is integrated or a seperate chip
90 */
91static inline int lapic_is_integrated(void)
ba7eda4c 92{
0e078e2f 93 return 1;
ba7eda4c
TG
94}
95
96/*
0e078e2f 97 * Check, whether this is a modern or a first generation APIC
ba7eda4c 98 */
0e078e2f 99static int modern_apic(void)
ba7eda4c 100{
0e078e2f
TG
101 /* AMD systems use old APIC versions, so check the CPU */
102 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
103 boot_cpu_data.x86 >= 0xf)
104 return 1;
105 return lapic_get_version() >= 0x14;
ba7eda4c
TG
106}
107
8339e9fb
FLV
108void apic_wait_icr_idle(void)
109{
110 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
111 cpu_relax();
112}
113
3c6bb07a 114u32 safe_apic_wait_icr_idle(void)
8339e9fb 115{
3c6bb07a 116 u32 send_status;
8339e9fb
FLV
117 int timeout;
118
119 timeout = 0;
120 do {
121 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
122 if (!send_status)
123 break;
124 udelay(100);
125 } while (timeout++ < 1000);
126
127 return send_status;
128}
129
0e078e2f
TG
130/**
131 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
132 */
133void enable_NMI_through_LVT0(void *dummy)
1da177e4 134{
11a8e778 135 unsigned int v;
6935d1f9
TG
136
137 /* unmask and set to NMI */
138 v = APIC_DM_NMI;
11a8e778 139 apic_write(APIC_LVT0, v);
1da177e4
LT
140}
141
0e078e2f
TG
142/**
143 * lapic_get_maxlvt - get the maximum number of local vector table entries
144 */
37e650c7 145int lapic_get_maxlvt(void)
1da177e4 146{
11a8e778 147 unsigned int v, maxlvt;
1da177e4
LT
148
149 v = apic_read(APIC_LVR);
1da177e4
LT
150 maxlvt = GET_APIC_MAXLVT(v);
151 return maxlvt;
152}
153
0e078e2f
TG
154/*
155 * This function sets up the local APIC timer, with a timeout of
156 * 'clocks' APIC bus clock. During calibration we actually call
157 * this function twice on the boot CPU, once with a bogus timeout
158 * value, second time for real. The other (noncalibrating) CPUs
159 * call this function only once, with the real, calibrated value.
160 *
161 * We do reads before writes even if unnecessary, to get around the
162 * P5 APIC double write bug.
163 */
164
165static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
1da177e4 166{
0e078e2f 167 unsigned int lvtt_value, tmp_value;
1da177e4 168
0e078e2f
TG
169 lvtt_value = LOCAL_TIMER_VECTOR;
170 if (!oneshot)
171 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
172 if (!irqen)
173 lvtt_value |= APIC_LVT_MASKED;
1da177e4 174
0e078e2f 175 apic_write(APIC_LVTT, lvtt_value);
1da177e4
LT
176
177 /*
0e078e2f 178 * Divide PICLK by 16
1da177e4 179 */
0e078e2f
TG
180 tmp_value = apic_read(APIC_TDCR);
181 apic_write(APIC_TDCR, (tmp_value
182 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
183 | APIC_TDR_DIV_16);
184
185 if (!oneshot)
186 apic_write(APIC_TMICT, clocks);
1da177e4
LT
187}
188
0e078e2f
TG
189/*
190 * Setup extended LVT (K8 specific)
191 */
192void setup_APIC_extended_lvt(unsigned char lvt_off, unsigned char vector,
193 unsigned char msg_type, unsigned char mask)
1da177e4 194{
0e078e2f
TG
195 unsigned long reg = (lvt_off << 4) + K8_APIC_EXT_LVT_BASE;
196 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
a8fcf1a2 197
0e078e2f 198 apic_write(reg, v);
1da177e4
LT
199}
200
0e078e2f
TG
201/*
202 * Program the next event, relative to now
203 */
204static int lapic_next_event(unsigned long delta,
205 struct clock_event_device *evt)
1da177e4 206{
0e078e2f
TG
207 apic_write(APIC_TMICT, delta);
208 return 0;
1da177e4
LT
209}
210
0e078e2f
TG
211/*
212 * Setup the lapic timer in periodic or oneshot mode
213 */
214static void lapic_timer_setup(enum clock_event_mode mode,
215 struct clock_event_device *evt)
9b7711f0
HS
216{
217 unsigned long flags;
0e078e2f 218 unsigned int v;
9b7711f0 219
0e078e2f
TG
220 /* Lapic used as dummy for broadcast ? */
221 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
9b7711f0
HS
222 return;
223
224 local_irq_save(flags);
225
0e078e2f
TG
226 switch (mode) {
227 case CLOCK_EVT_MODE_PERIODIC:
228 case CLOCK_EVT_MODE_ONESHOT:
229 __setup_APIC_LVTT(calibration_result,
230 mode != CLOCK_EVT_MODE_PERIODIC, 1);
231 break;
232 case CLOCK_EVT_MODE_UNUSED:
233 case CLOCK_EVT_MODE_SHUTDOWN:
234 v = apic_read(APIC_LVTT);
235 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
236 apic_write(APIC_LVTT, v);
237 break;
238 case CLOCK_EVT_MODE_RESUME:
239 /* Nothing to do here */
240 break;
241 }
9b7711f0
HS
242
243 local_irq_restore(flags);
244}
245
1da177e4 246/*
0e078e2f 247 * Local APIC timer broadcast function
1da177e4 248 */
0e078e2f 249static void lapic_timer_broadcast(cpumask_t mask)
1da177e4 250{
0e078e2f
TG
251#ifdef CONFIG_SMP
252 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
253#endif
254}
1da177e4 255
0e078e2f
TG
256/*
257 * Setup the local APIC timer for this CPU. Copy the initilized values
258 * of the boot CPU and register the clock event in the framework.
259 */
260static void setup_APIC_timer(void)
261{
262 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
1da177e4 263
0e078e2f
TG
264 memcpy(levt, &lapic_clockevent, sizeof(*levt));
265 levt->cpumask = cpumask_of_cpu(smp_processor_id());
1da177e4 266
0e078e2f
TG
267 clockevents_register_device(levt);
268}
1da177e4 269
0e078e2f
TG
270/*
271 * In this function we calibrate APIC bus clocks to the external
272 * timer. Unfortunately we cannot use jiffies and the timer irq
273 * to calibrate, since some later bootup code depends on getting
274 * the first irq? Ugh.
275 *
276 * We want to do the calibration only once since we
277 * want to have local timer irqs syncron. CPUs connected
278 * by the same APIC bus have the very same bus frequency.
279 * And we want to have irqs off anyways, no accidental
280 * APIC irq that way.
281 */
282
283#define TICK_COUNT 100000000
284
285static void __init calibrate_APIC_clock(void)
286{
287 unsigned apic, apic_start;
288 unsigned long tsc, tsc_start;
289 int result;
290
291 local_irq_disable();
292
293 /*
294 * Put whatever arbitrary (but long enough) timeout
295 * value into the APIC clock, we just want to get the
296 * counter running for calibration.
297 *
298 * No interrupt enable !
299 */
300 __setup_APIC_LVTT(250000000, 0, 0);
301
302 apic_start = apic_read(APIC_TMCCT);
303#ifdef CONFIG_X86_PM_TIMER
304 if (apic_calibrate_pmtmr && pmtmr_ioport) {
305 pmtimer_wait(5000); /* 5ms wait */
306 apic = apic_read(APIC_TMCCT);
307 result = (apic_start - apic) * 1000L / 5;
308 } else
309#endif
310 {
311 rdtscll(tsc_start);
312
313 do {
314 apic = apic_read(APIC_TMCCT);
315 rdtscll(tsc);
316 } while ((tsc - tsc_start) < TICK_COUNT &&
317 (apic_start - apic) < TICK_COUNT);
318
319 result = (apic_start - apic) * 1000L * tsc_khz /
320 (tsc - tsc_start);
321 }
322
323 local_irq_enable();
324
325 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
326
327 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
328 result / 1000 / 1000, result / 1000 % 1000);
329
330 /* Calculate the scaled math multiplication factor */
331 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC, 32);
332 lapic_clockevent.max_delta_ns =
333 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
334 lapic_clockevent.min_delta_ns =
335 clockevent_delta2ns(0xF, &lapic_clockevent);
336
337 calibration_result = result / HZ;
338}
339
340void __init setup_boot_APIC_clock(void)
341{
342 /*
343 * The local apic timer can be disabled via the kernel commandline.
344 * Register the lapic timer as a dummy clock event source on SMP
345 * systems, so the broadcast mechanism is used. On UP systems simply
346 * ignore it.
347 */
348 if (disable_apic_timer) {
349 printk(KERN_INFO "Disabling APIC timer\n");
350 /* No broadcast on UP ! */
351 if (num_possible_cpus() > 1)
352 setup_APIC_timer();
353 return;
354 }
355
356 printk(KERN_INFO "Using local APIC timer interrupts.\n");
357 calibrate_APIC_clock();
358
359 /*
360 * If nmi_watchdog is set to IO_APIC, we need the
361 * PIT/HPET going. Otherwise register lapic as a dummy
362 * device.
363 */
364 if (nmi_watchdog != NMI_IO_APIC)
365 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
366 else
367 printk(KERN_WARNING "APIC timer registered as dummy,"
368 " due to nmi_watchdog=1!\n");
369
370 setup_APIC_timer();
371}
372
373/*
374 * AMD C1E enabled CPUs have a real nasty problem: Some BIOSes set the
375 * C1E flag only in the secondary CPU, so when we detect the wreckage
376 * we already have enabled the boot CPU local apic timer. Check, if
377 * disable_apic_timer is set and the DUMMY flag is cleared. If yes,
378 * set the DUMMY flag again and force the broadcast mode in the
379 * clockevents layer.
380 */
381void __cpuinit check_boot_apic_timer_broadcast(void)
382{
383 if (!disable_apic_timer ||
384 (lapic_clockevent.features & CLOCK_EVT_FEAT_DUMMY))
385 return;
386
387 printk(KERN_INFO "AMD C1E detected late. Force timer broadcast.\n");
388 lapic_clockevent.features |= CLOCK_EVT_FEAT_DUMMY;
389
390 local_irq_enable();
391 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE, &boot_cpu_id);
392 local_irq_disable();
393}
394
395void __cpuinit setup_secondary_APIC_clock(void)
396{
397 check_boot_apic_timer_broadcast();
398 setup_APIC_timer();
399}
400
401/*
402 * The guts of the apic timer interrupt
403 */
404static void local_apic_timer_interrupt(void)
405{
406 int cpu = smp_processor_id();
407 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
408
409 /*
410 * Normally we should not be here till LAPIC has been initialized but
411 * in some cases like kdump, its possible that there is a pending LAPIC
412 * timer interrupt from previous kernel's context and is delivered in
413 * new kernel the moment interrupts are enabled.
414 *
415 * Interrupts are enabled early and LAPIC is setup much later, hence
416 * its possible that when we get here evt->event_handler is NULL.
417 * Check for event_handler being NULL and discard the interrupt as
418 * spurious.
419 */
420 if (!evt->event_handler) {
421 printk(KERN_WARNING
422 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
423 /* Switch it off */
424 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
425 return;
426 }
427
428 /*
429 * the NMI deadlock-detector uses this.
430 */
431 add_pda(apic_timer_irqs, 1);
432
433 evt->event_handler(evt);
434}
435
436/*
437 * Local APIC timer interrupt. This is the most natural way for doing
438 * local interrupts, but local timer interrupts can be emulated by
439 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
440 *
441 * [ if a single-CPU system runs an SMP kernel then we call the local
442 * interrupt as well. Thus we cannot inline the local irq ... ]
443 */
444void smp_apic_timer_interrupt(struct pt_regs *regs)
445{
446 struct pt_regs *old_regs = set_irq_regs(regs);
447
448 /*
449 * NOTE! We'd better ACK the irq immediately,
450 * because timer handling can be slow.
451 */
452 ack_APIC_irq();
453 /*
454 * update_process_times() expects us to have done irq_enter().
455 * Besides, if we don't timer interrupts ignore the global
456 * interrupt lock, which is the WrongThing (tm) to do.
457 */
458 exit_idle();
459 irq_enter();
460 local_apic_timer_interrupt();
461 irq_exit();
462 set_irq_regs(old_regs);
463}
464
465int setup_profiling_timer(unsigned int multiplier)
466{
467 return -EINVAL;
468}
469
470
471/*
472 * Local APIC start and shutdown
473 */
474
475/**
476 * clear_local_APIC - shutdown the local APIC
477 *
478 * This is called, when a CPU is disabled and before rebooting, so the state of
479 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
480 * leftovers during boot.
481 */
482void clear_local_APIC(void)
483{
484 int maxlvt = lapic_get_maxlvt();
485 u32 v;
486
487 /*
488 * Masking an LVT entry can trigger a local APIC error
489 * if the vector is zero. Mask LVTERR first to prevent this.
490 */
491 if (maxlvt >= 3) {
492 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
493 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
494 }
495 /*
496 * Careful: we have to set masks only first to deassert
497 * any level-triggered sources.
498 */
499 v = apic_read(APIC_LVTT);
500 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
501 v = apic_read(APIC_LVT0);
502 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
503 v = apic_read(APIC_LVT1);
504 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
505 if (maxlvt >= 4) {
506 v = apic_read(APIC_LVTPC);
507 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
508 }
509
510 /*
511 * Clean APIC state for other OSs:
512 */
513 apic_write(APIC_LVTT, APIC_LVT_MASKED);
514 apic_write(APIC_LVT0, APIC_LVT_MASKED);
515 apic_write(APIC_LVT1, APIC_LVT_MASKED);
516 if (maxlvt >= 3)
517 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
518 if (maxlvt >= 4)
519 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
520 apic_write(APIC_ESR, 0);
521 apic_read(APIC_ESR);
522}
523
524/**
525 * disable_local_APIC - clear and disable the local APIC
526 */
527void disable_local_APIC(void)
528{
529 unsigned int value;
530
531 clear_local_APIC();
532
533 /*
534 * Disable APIC (implies clearing of registers
535 * for 82489DX!).
536 */
537 value = apic_read(APIC_SPIV);
538 value &= ~APIC_SPIV_APIC_ENABLED;
539 apic_write(APIC_SPIV, value);
540}
541
542void lapic_shutdown(void)
543{
544 unsigned long flags;
545
546 if (!cpu_has_apic)
547 return;
548
549 local_irq_save(flags);
550
551 disable_local_APIC();
552
553 local_irq_restore(flags);
554}
555
556/*
557 * This is to verify that we're looking at a real local APIC.
558 * Check these against your board if the CPUs aren't getting
559 * started for no apparent reason.
560 */
561int __init verify_local_APIC(void)
562{
563 unsigned int reg0, reg1;
564
565 /*
566 * The version register is read-only in a real APIC.
567 */
568 reg0 = apic_read(APIC_LVR);
569 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
570 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
571 reg1 = apic_read(APIC_LVR);
572 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
573
574 /*
575 * The two version reads above should print the same
576 * numbers. If the second one is different, then we
577 * poke at a non-APIC.
578 */
579 if (reg1 != reg0)
580 return 0;
581
582 /*
583 * Check if the version looks reasonably.
584 */
585 reg1 = GET_APIC_VERSION(reg0);
586 if (reg1 == 0x00 || reg1 == 0xff)
587 return 0;
588 reg1 = lapic_get_maxlvt();
589 if (reg1 < 0x02 || reg1 == 0xff)
590 return 0;
591
592 /*
593 * The ID register is read/write in a real APIC.
594 */
595 reg0 = apic_read(APIC_ID);
596 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
597 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
598 reg1 = apic_read(APIC_ID);
599 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
600 apic_write(APIC_ID, reg0);
601 if (reg1 != (reg0 ^ APIC_ID_MASK))
602 return 0;
603
604 /*
1da177e4
LT
605 * The next two are just to see if we have sane values.
606 * They're only really relevant if we're in Virtual Wire
607 * compatibility mode, but most boxes are anymore.
608 */
609 reg0 = apic_read(APIC_LVT0);
0e078e2f 610 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
1da177e4
LT
611 reg1 = apic_read(APIC_LVT1);
612 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
613
614 return 1;
615}
616
0e078e2f
TG
617/**
618 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
619 */
1da177e4
LT
620void __init sync_Arb_IDs(void)
621{
622 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
0e078e2f 623 if (modern_apic())
1da177e4
LT
624 return;
625
626 /*
627 * Wait for idle.
628 */
629 apic_wait_icr_idle();
630
631 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
11a8e778 632 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
1da177e4
LT
633 | APIC_DM_INIT);
634}
635
1da177e4
LT
636/*
637 * An initial setup of the virtual wire mode.
638 */
639void __init init_bsp_APIC(void)
640{
11a8e778 641 unsigned int value;
1da177e4
LT
642
643 /*
644 * Don't do the setup now if we have a SMP BIOS as the
645 * through-I/O-APIC virtual wire mode might be active.
646 */
647 if (smp_found_config || !cpu_has_apic)
648 return;
649
650 value = apic_read(APIC_LVR);
1da177e4
LT
651
652 /*
653 * Do not trust the local APIC being empty at bootup.
654 */
655 clear_local_APIC();
656
657 /*
658 * Enable APIC.
659 */
660 value = apic_read(APIC_SPIV);
661 value &= ~APIC_VECTOR_MASK;
662 value |= APIC_SPIV_APIC_ENABLED;
663 value |= APIC_SPIV_FOCUS_DISABLED;
664 value |= SPURIOUS_APIC_VECTOR;
11a8e778 665 apic_write(APIC_SPIV, value);
1da177e4
LT
666
667 /*
668 * Set up the virtual wire mode.
669 */
11a8e778 670 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4 671 value = APIC_DM_NMI;
11a8e778 672 apic_write(APIC_LVT1, value);
1da177e4
LT
673}
674
0e078e2f
TG
675/**
676 * setup_local_APIC - setup the local APIC
677 */
678void __cpuinit setup_local_APIC(void)
1da177e4 679{
11a8e778 680 unsigned int value, maxlvt;
da7ed9f9 681 int i, j;
1da177e4 682
1da177e4 683 value = apic_read(APIC_LVR);
1da177e4 684
fe7414a2 685 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
1da177e4
LT
686
687 /*
688 * Double-check whether this APIC is really registered.
689 * This is meaningless in clustered apic mode, so we skip it.
690 */
691 if (!apic_id_registered())
692 BUG();
693
694 /*
695 * Intel recommends to set DFR, LDR and TPR before enabling
696 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
697 * document number 292116). So here it goes...
698 */
699 init_apic_ldr();
700
701 /*
702 * Set Task Priority to 'accept all'. We never change this
703 * later on.
704 */
705 value = apic_read(APIC_TASKPRI);
706 value &= ~APIC_TPRI_MASK;
11a8e778 707 apic_write(APIC_TASKPRI, value);
1da177e4 708
da7ed9f9
VG
709 /*
710 * After a crash, we no longer service the interrupts and a pending
711 * interrupt from previous kernel might still have ISR bit set.
712 *
713 * Most probably by now CPU has serviced that pending interrupt and
714 * it might not have done the ack_APIC_irq() because it thought,
715 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
716 * does not clear the ISR bit and cpu thinks it has already serivced
717 * the interrupt. Hence a vector might get locked. It was noticed
718 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
719 */
720 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
721 value = apic_read(APIC_ISR + i*0x10);
722 for (j = 31; j >= 0; j--) {
723 if (value & (1<<j))
724 ack_APIC_irq();
725 }
726 }
727
1da177e4
LT
728 /*
729 * Now that we are all set up, enable the APIC
730 */
731 value = apic_read(APIC_SPIV);
732 value &= ~APIC_VECTOR_MASK;
733 /*
734 * Enable APIC
735 */
736 value |= APIC_SPIV_APIC_ENABLED;
737
3f14c746
AK
738 /* We always use processor focus */
739
1da177e4
LT
740 /*
741 * Set spurious IRQ vector
742 */
743 value |= SPURIOUS_APIC_VECTOR;
11a8e778 744 apic_write(APIC_SPIV, value);
1da177e4
LT
745
746 /*
747 * Set up LVT0, LVT1:
748 *
749 * set up through-local-APIC on the BP's LINT0. This is not
750 * strictly necessary in pure symmetric-IO mode, but sometimes
751 * we delegate interrupts to the 8259A.
752 */
753 /*
754 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
755 */
756 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
a8fcf1a2 757 if (!smp_processor_id() && !value) {
1da177e4 758 value = APIC_DM_EXTINT;
bc1d99c1
CW
759 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
760 smp_processor_id());
1da177e4
LT
761 } else {
762 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
bc1d99c1
CW
763 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
764 smp_processor_id());
1da177e4 765 }
11a8e778 766 apic_write(APIC_LVT0, value);
1da177e4
LT
767
768 /*
769 * only the BP should see the LINT1 NMI signal, obviously.
770 */
771 if (!smp_processor_id())
772 value = APIC_DM_NMI;
773 else
774 value = APIC_DM_NMI | APIC_LVT_MASKED;
11a8e778 775 apic_write(APIC_LVT1, value);
1da177e4 776
61c11341 777 {
1da177e4 778 unsigned oldvalue;
37e650c7 779 maxlvt = lapic_get_maxlvt();
1da177e4
LT
780 oldvalue = apic_read(APIC_ESR);
781 value = ERROR_APIC_VECTOR; // enables sending errors
11a8e778 782 apic_write(APIC_LVTERR, value);
1da177e4
LT
783 /*
784 * spec says clear errors after enabling vector.
785 */
786 if (maxlvt > 3)
787 apic_write(APIC_ESR, 0);
788 value = apic_read(APIC_ESR);
789 if (value != oldvalue)
790 apic_printk(APIC_VERBOSE,
791 "ESR value after enabling vector: %08x, after %08x\n",
792 oldvalue, value);
1da177e4
LT
793 }
794
795 nmi_watchdog_default();
f2802e7f 796 setup_apic_nmi_watchdog(NULL);
0e078e2f 797 apic_pm_activate();
1da177e4 798}
1da177e4
LT
799
800/*
801 * Detect and enable local APICs on non-SMP boards.
802 * Original code written by Keir Fraser.
803 * On AMD64 we trust the BIOS - if it says no APIC it is likely
6935d1f9 804 * not correctly set up (usually the APIC timer won't work etc.)
1da177e4 805 */
0e078e2f 806static int __init detect_init_APIC(void)
1da177e4
LT
807{
808 if (!cpu_has_apic) {
809 printk(KERN_INFO "No local APIC present\n");
810 return -1;
811 }
812
813 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
814 boot_cpu_id = 0;
815 return 0;
816}
817
0e078e2f
TG
818/**
819 * init_apic_mappings - initialize APIC mappings
820 */
1da177e4
LT
821void __init init_apic_mappings(void)
822{
823 unsigned long apic_phys;
824
825 /*
826 * If no local APIC can be found then set up a fake all
827 * zeroes page to simulate the local APIC and another
828 * one for the IO-APIC.
829 */
830 if (!smp_found_config && detect_init_APIC()) {
831 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
832 apic_phys = __pa(apic_phys);
833 } else
834 apic_phys = mp_lapic_addr;
835
836 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
7ffeeb1e
YL
837 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
838 APIC_BASE, apic_phys);
1da177e4 839
39928722
AD
840 /* Put local APIC into the resource map. */
841 lapic_resource.start = apic_phys;
842 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
843 insert_resource(&iomem_resource, &lapic_resource);
844
1da177e4
LT
845 /*
846 * Fetch the APIC ID of the BSP in case we have a
847 * default configuration (or the MP table is broken).
848 */
1d3fbbf9 849 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
1da177e4
LT
850}
851
852/*
0e078e2f
TG
853 * This initializes the IO-APIC and APIC hardware if this is
854 * a UP kernel.
1da177e4 855 */
0e078e2f 856int __init APIC_init_uniprocessor(void)
1da177e4 857{
0e078e2f
TG
858 if (disable_apic) {
859 printk(KERN_INFO "Apic disabled\n");
860 return -1;
861 }
862 if (!cpu_has_apic) {
863 disable_apic = 1;
864 printk(KERN_INFO "Apic disabled by BIOS\n");
865 return -1;
866 }
1da177e4 867
0e078e2f 868 verify_local_APIC();
1da177e4 869
0e078e2f
TG
870 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_id);
871 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_id));
1da177e4 872
0e078e2f 873 setup_local_APIC();
1da177e4 874
0e078e2f
TG
875 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
876 setup_IO_APIC();
877 else
878 nr_ioapics = 0;
879 setup_boot_APIC_clock();
880 check_nmi_watchdog();
881 return 0;
1da177e4
LT
882}
883
884/*
0e078e2f 885 * Local APIC interrupts
1da177e4
LT
886 */
887
0e078e2f
TG
888/*
889 * This interrupt should _never_ happen with our APIC/SMP architecture
890 */
891asmlinkage void smp_spurious_interrupt(void)
1da177e4 892{
0e078e2f
TG
893 unsigned int v;
894 exit_idle();
895 irq_enter();
1da177e4 896 /*
0e078e2f
TG
897 * Check if this really is a spurious interrupt and ACK it
898 * if it is a vectored one. Just in case...
899 * Spurious interrupts should not be ACKed.
1da177e4 900 */
0e078e2f
TG
901 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
902 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
903 ack_APIC_irq();
c4d58cbd 904
0e078e2f
TG
905 add_pda(irq_spurious_count, 1);
906 irq_exit();
907}
1da177e4 908
0e078e2f
TG
909/*
910 * This interrupt should never happen with our APIC/SMP architecture
911 */
912asmlinkage void smp_error_interrupt(void)
913{
914 unsigned int v, v1;
1da177e4 915
0e078e2f
TG
916 exit_idle();
917 irq_enter();
918 /* First tickle the hardware, only then report what went on. -- REW */
919 v = apic_read(APIC_ESR);
920 apic_write(APIC_ESR, 0);
921 v1 = apic_read(APIC_ESR);
922 ack_APIC_irq();
923 atomic_inc(&irq_err_count);
ba7eda4c 924
0e078e2f
TG
925 /* Here is what the APIC error bits mean:
926 0: Send CS error
927 1: Receive CS error
928 2: Send accept error
929 3: Receive accept error
930 4: Reserved
931 5: Send illegal vector
932 6: Received illegal vector
933 7: Illegal register address
934 */
935 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
936 smp_processor_id(), v , v1);
937 irq_exit();
1da177e4
LT
938}
939
0e078e2f 940void disconnect_bsp_APIC(int virt_wire_setup)
1da177e4 941{
0e078e2f
TG
942 /* Go back to Virtual Wire compatibility mode */
943 unsigned long value;
1da177e4 944
0e078e2f
TG
945 /* For the spurious interrupt use vector F, and enable it */
946 value = apic_read(APIC_SPIV);
947 value &= ~APIC_VECTOR_MASK;
948 value |= APIC_SPIV_APIC_ENABLED;
949 value |= 0xf;
950 apic_write(APIC_SPIV, value);
b8ce3359 951
0e078e2f
TG
952 if (!virt_wire_setup) {
953 /*
954 * For LVT0 make it edge triggered, active high,
955 * external and enabled
956 */
957 value = apic_read(APIC_LVT0);
958 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
959 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
960 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
961 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
962 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
963 apic_write(APIC_LVT0, value);
964 } else {
965 /* Disable LVT0 */
966 apic_write(APIC_LVT0, APIC_LVT_MASKED);
967 }
b8ce3359 968
0e078e2f
TG
969 /* For LVT1 make it edge triggered, active high, nmi and enabled */
970 value = apic_read(APIC_LVT1);
971 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
972 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
973 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
974 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
975 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
976 apic_write(APIC_LVT1, value);
1da177e4
LT
977}
978
89039b37 979/*
0e078e2f 980 * Power management
89039b37 981 */
0e078e2f
TG
982#ifdef CONFIG_PM
983
984static struct {
985 /* 'active' is true if the local APIC was enabled by us and
986 not the BIOS; this signifies that we are also responsible
987 for disabling it before entering apm/acpi suspend */
988 int active;
989 /* r/w apic fields */
990 unsigned int apic_id;
991 unsigned int apic_taskpri;
992 unsigned int apic_ldr;
993 unsigned int apic_dfr;
994 unsigned int apic_spiv;
995 unsigned int apic_lvtt;
996 unsigned int apic_lvtpc;
997 unsigned int apic_lvt0;
998 unsigned int apic_lvt1;
999 unsigned int apic_lvterr;
1000 unsigned int apic_tmict;
1001 unsigned int apic_tdcr;
1002 unsigned int apic_thmr;
1003} apic_pm_state;
1004
1005static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1006{
1007 unsigned long flags;
1008 int maxlvt;
89039b37 1009
0e078e2f
TG
1010 if (!apic_pm_state.active)
1011 return 0;
89039b37 1012
0e078e2f 1013 maxlvt = lapic_get_maxlvt();
89039b37 1014
0e078e2f
TG
1015 apic_pm_state.apic_id = apic_read(APIC_ID);
1016 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1017 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1018 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1019 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1020 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1021 if (maxlvt >= 4)
1022 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1023 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1024 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1025 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1026 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1027 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1028#ifdef CONFIG_X86_MCE_INTEL
1029 if (maxlvt >= 5)
1030 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1031#endif
1032 local_irq_save(flags);
1033 disable_local_APIC();
1034 local_irq_restore(flags);
1035 return 0;
1da177e4
LT
1036}
1037
0e078e2f 1038static int lapic_resume(struct sys_device *dev)
1da177e4 1039{
0e078e2f
TG
1040 unsigned int l, h;
1041 unsigned long flags;
1042 int maxlvt;
1da177e4 1043
0e078e2f
TG
1044 if (!apic_pm_state.active)
1045 return 0;
89b831ef 1046
0e078e2f 1047 maxlvt = lapic_get_maxlvt();
1da177e4 1048
0e078e2f
TG
1049 local_irq_save(flags);
1050 rdmsr(MSR_IA32_APICBASE, l, h);
1051 l &= ~MSR_IA32_APICBASE_BASE;
1052 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1053 wrmsr(MSR_IA32_APICBASE, l, h);
1054 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1055 apic_write(APIC_ID, apic_pm_state.apic_id);
1056 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1057 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1058 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1059 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1060 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1061 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1062#ifdef CONFIG_X86_MCE_INTEL
1063 if (maxlvt >= 5)
1064 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1065#endif
1066 if (maxlvt >= 4)
1067 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1068 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1069 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1070 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1071 apic_write(APIC_ESR, 0);
1072 apic_read(APIC_ESR);
1073 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1074 apic_write(APIC_ESR, 0);
1075 apic_read(APIC_ESR);
1076 local_irq_restore(flags);
1077 return 0;
1078}
b8ce3359 1079
0e078e2f
TG
1080static struct sysdev_class lapic_sysclass = {
1081 .name = "lapic",
1082 .resume = lapic_resume,
1083 .suspend = lapic_suspend,
1084};
b8ce3359 1085
0e078e2f
TG
1086static struct sys_device device_lapic = {
1087 .id = 0,
1088 .cls = &lapic_sysclass,
1089};
b8ce3359 1090
0e078e2f
TG
1091static void __cpuinit apic_pm_activate(void)
1092{
1093 apic_pm_state.active = 1;
1da177e4
LT
1094}
1095
0e078e2f 1096static int __init init_lapic_sysfs(void)
1da177e4 1097{
0e078e2f
TG
1098 int error;
1099 if (!cpu_has_apic)
1100 return 0;
1101 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1102 error = sysdev_class_register(&lapic_sysclass);
1103 if (!error)
1104 error = sysdev_register(&device_lapic);
1105 return error;
1da177e4 1106}
0e078e2f
TG
1107device_initcall(init_lapic_sysfs);
1108
1109#else /* CONFIG_PM */
1110
1111static void apic_pm_activate(void) { }
1112
1113#endif /* CONFIG_PM */
1da177e4
LT
1114
1115/*
f8bf3c65 1116 * apic_is_clustered_box() -- Check if we can expect good TSC
1da177e4
LT
1117 *
1118 * Thus far, the major user of this is IBM's Summit2 series:
1119 *
637029c6 1120 * Clustered boxes may have unsynced TSC problems if they are
1da177e4
LT
1121 * multi-chassis. Use available data to take a good guess.
1122 * If in doubt, go HPET.
1123 */
f8bf3c65 1124__cpuinit int apic_is_clustered_box(void)
1da177e4
LT
1125{
1126 int i, clusters, zeros;
1127 unsigned id;
1128 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1129
376ec33f 1130 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
1da177e4
LT
1131
1132 for (i = 0; i < NR_CPUS; i++) {
1133 id = bios_cpu_apicid[i];
1134 if (id != BAD_APICID)
1135 __set_bit(APIC_CLUSTERID(id), clustermap);
1136 }
1137
1138 /* Problem: Partially populated chassis may not have CPUs in some of
1139 * the APIC clusters they have been allocated. Only present CPUs have
1140 * bios_cpu_apicid entries, thus causing zeroes in the bitmap. Since
1141 * clusters are allocated sequentially, count zeros only if they are
1142 * bounded by ones.
1143 */
1144 clusters = 0;
1145 zeros = 0;
1146 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1147 if (test_bit(i, clustermap)) {
1148 clusters += 1 + zeros;
1149 zeros = 0;
1150 } else
1151 ++zeros;
1152 }
1153
1154 /*
f8bf3c65 1155 * If clusters > 2, then should be multi-chassis.
1da177e4
LT
1156 * May have to revisit this when multi-core + hyperthreaded CPUs come
1157 * out, but AFAIK this will work even for them.
1158 */
1159 return (clusters > 2);
1160}
1161
1162/*
0e078e2f 1163 * APIC command line parameters
1da177e4 1164 */
0e078e2f 1165static int __init apic_set_verbosity(char *str)
1da177e4 1166{
0e078e2f
TG
1167 if (str == NULL) {
1168 skip_ioapic_setup = 0;
1169 ioapic_force = 1;
1170 return 0;
1da177e4 1171 }
0e078e2f
TG
1172 if (strcmp("debug", str) == 0)
1173 apic_verbosity = APIC_DEBUG;
1174 else if (strcmp("verbose", str) == 0)
1175 apic_verbosity = APIC_VERBOSE;
1176 else {
1177 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1178 " use apic=verbose or apic=debug\n", str);
1179 return -EINVAL;
1da177e4
LT
1180 }
1181
1da177e4
LT
1182 return 0;
1183}
0e078e2f 1184early_param("apic", apic_set_verbosity);
1da177e4 1185
6935d1f9
TG
1186static __init int setup_disableapic(char *str)
1187{
1da177e4 1188 disable_apic = 1;
2c8c0e6b
AK
1189 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1190 return 0;
1191}
1192early_param("disableapic", setup_disableapic);
1da177e4 1193
2c8c0e6b 1194/* same as disableapic, for compatibility */
6935d1f9
TG
1195static __init int setup_nolapic(char *str)
1196{
2c8c0e6b 1197 return setup_disableapic(str);
6935d1f9 1198}
2c8c0e6b 1199early_param("nolapic", setup_nolapic);
1da177e4 1200
2e7c2838
LT
1201static int __init parse_lapic_timer_c2_ok(char *arg)
1202{
1203 local_apic_timer_c2_ok = 1;
1204 return 0;
1205}
1206early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1207
6935d1f9
TG
1208static __init int setup_noapictimer(char *str)
1209{
73dea47f 1210 if (str[0] != ' ' && str[0] != 0)
9b41046c 1211 return 0;
1da177e4 1212 disable_apic_timer = 1;
9b41046c 1213 return 1;
6935d1f9 1214}
9f75e9b7 1215__setup("noapictimer", setup_noapictimer);
73dea47f 1216
0c3749c4
AK
1217static __init int setup_apicpmtimer(char *s)
1218{
1219 apic_calibrate_pmtmr = 1;
7fd67843 1220 notsc_setup(NULL);
b8ce3359 1221 return 0;
0c3749c4
AK
1222}
1223__setup("apicpmtimer", setup_apicpmtimer);
1224
This page took 0.385097 seconds and 5 git commands to generate.