aboutsummaryrefslogtreecommitdiffstats
path: root/arch/i386/kernel/apic.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/i386/kernel/apic.c')
-rw-r--r--arch/i386/kernel/apic.c1631
1 files changed, 870 insertions, 761 deletions
diff --git a/arch/i386/kernel/apic.c b/arch/i386/kernel/apic.c
index 776d9be26af9..9655c233e6f1 100644
--- a/arch/i386/kernel/apic.c
+++ b/arch/i386/kernel/apic.c
@@ -25,6 +25,8 @@
25#include <linux/kernel_stat.h> 25#include <linux/kernel_stat.h>
26#include <linux/sysdev.h> 26#include <linux/sysdev.h>
27#include <linux/cpu.h> 27#include <linux/cpu.h>
28#include <linux/clockchips.h>
29#include <linux/acpi_pmtmr.h>
28#include <linux/module.h> 30#include <linux/module.h>
29 31
30#include <asm/atomic.h> 32#include <asm/atomic.h>
@@ -36,6 +38,7 @@
36#include <asm/hpet.h> 38#include <asm/hpet.h>
37#include <asm/i8253.h> 39#include <asm/i8253.h>
38#include <asm/nmi.h> 40#include <asm/nmi.h>
41#include <asm/idle.h>
39 42
40#include <mach_apic.h> 43#include <mach_apic.h>
41#include <mach_apicdef.h> 44#include <mach_apicdef.h>
@@ -44,128 +47,549 @@
44#include "io_ports.h" 47#include "io_ports.h"
45 48
46/* 49/*
47 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as 50 * Sanity check
48 * IPIs in place of local APIC timers
49 */ 51 */
50static cpumask_t timer_bcast_ipi; 52#if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
53# error SPURIOUS_APIC_VECTOR definition error
54#endif
51 55
52/* 56/*
53 * Knob to control our willingness to enable the local APIC. 57 * Knob to control our willingness to enable the local APIC.
58 *
59 * -1=force-disable, +1=force-enable
54 */ 60 */
55static int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */ 61static int enable_local_apic __initdata = 0;
56
57static inline void lapic_disable(void)
58{
59 enable_local_apic = -1;
60 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
61}
62 62
63static inline void lapic_enable(void) 63/* Local APIC timer verification ok */
64{ 64static int local_apic_timer_verify_ok;
65 enable_local_apic = 1;
66}
67 65
68/* 66/*
69 * Debug level 67 * Debug level, exported for io_apic.c
70 */ 68 */
71int apic_verbosity; 69int apic_verbosity;
72 70
71static unsigned int calibration_result;
73 72
73static int lapic_next_event(unsigned long delta,
74 struct clock_event_device *evt);
75static void lapic_timer_setup(enum clock_event_mode mode,
76 struct clock_event_device *evt);
77static void lapic_timer_broadcast(cpumask_t mask);
74static void apic_pm_activate(void); 78static void apic_pm_activate(void);
75 79
80/*
81 * The local apic timer can be used for any function which is CPU local.
82 */
83static struct clock_event_device lapic_clockevent = {
84 .name = "lapic",
85 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
86 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
87 .shift = 32,
88 .set_mode = lapic_timer_setup,
89 .set_next_event = lapic_next_event,
90 .broadcast = lapic_timer_broadcast,
91 .rating = 100,
92 .irq = -1,
93};
94static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
95
96/* Local APIC was disabled by the BIOS and enabled by the kernel */
97static int enabled_via_apicbase;
98
99/*
100 * Get the LAPIC version
101 */
102static inline int lapic_get_version(void)
103{
104 return GET_APIC_VERSION(apic_read(APIC_LVR));
105}
106
107/*
108 * Check, if the APIC is integrated or a seperate chip
109 */
110static inline int lapic_is_integrated(void)
111{
112 return APIC_INTEGRATED(lapic_get_version());
113}
114
115/*
116 * Check, whether this is a modern or a first generation APIC
117 */
76static int modern_apic(void) 118static int modern_apic(void)
77{ 119{
78 unsigned int lvr, version;
79 /* AMD systems use old APIC versions, so check the CPU */ 120 /* AMD systems use old APIC versions, so check the CPU */
80 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD && 121 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
81 boot_cpu_data.x86 >= 0xf) 122 boot_cpu_data.x86 >= 0xf)
82 return 1; 123 return 1;
83 lvr = apic_read(APIC_LVR); 124 return lapic_get_version() >= 0x14;
84 version = GET_APIC_VERSION(lvr);
85 return version >= 0x14;
86} 125}
87 126
127/**
128 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
129 */
130void enable_NMI_through_LVT0 (void * dummy)
131{
132 unsigned int v = APIC_DM_NMI;
133
134 /* Level triggered for 82489DX */
135 if (!lapic_is_integrated())
136 v |= APIC_LVT_LEVEL_TRIGGER;
137 apic_write_around(APIC_LVT0, v);
138}
139
140/**
141 * get_physical_broadcast - Get number of physical broadcast IDs
142 */
143int get_physical_broadcast(void)
144{
145 return modern_apic() ? 0xff : 0xf;
146}
147
148/**
149 * lapic_get_maxlvt - get the maximum number of local vector table entries
150 */
151int lapic_get_maxlvt(void)
152{
153 unsigned int v = apic_read(APIC_LVR);
154
155 /* 82489DXs do not report # of LVT entries. */
156 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
157}
158
159/*
160 * Local APIC timer
161 */
162
163/* Clock divisor is set to 16 */
164#define APIC_DIVISOR 16
165
88/* 166/*
89 * 'what should we do if we get a hw irq event on an illegal vector'. 167 * This function sets up the local APIC timer, with a timeout of
90 * each architecture has to answer this themselves. 168 * 'clocks' APIC bus clock. During calibration we actually call
169 * this function twice on the boot CPU, once with a bogus timeout
170 * value, second time for real. The other (noncalibrating) CPUs
171 * call this function only once, with the real, calibrated value.
172 *
173 * We do reads before writes even if unnecessary, to get around the
174 * P5 APIC double write bug.
91 */ 175 */
92void ack_bad_irq(unsigned int irq) 176static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
93{ 177{
94 printk("unexpected IRQ trap at vector %02x\n", irq); 178 unsigned int lvtt_value, tmp_value;
179
180 lvtt_value = LOCAL_TIMER_VECTOR;
181 if (!oneshot)
182 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
183 if (!lapic_is_integrated())
184 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
185
186 if (!irqen)
187 lvtt_value |= APIC_LVT_MASKED;
188
189 apic_write_around(APIC_LVTT, lvtt_value);
190
95 /* 191 /*
96 * Currently unexpected vectors happen only on SMP and APIC. 192 * Divide PICLK by 16
97 * We _must_ ack these because every local APIC has only N
98 * irq slots per priority level, and a 'hanging, unacked' IRQ
99 * holds up an irq slot - in excessive cases (when multiple
100 * unexpected vectors occur) that might lock up the APIC
101 * completely.
102 * But only ack when the APIC is enabled -AK
103 */ 193 */
104 if (cpu_has_apic) 194 tmp_value = apic_read(APIC_TDCR);
105 ack_APIC_irq(); 195 apic_write_around(APIC_TDCR, (tmp_value
196 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
197 | APIC_TDR_DIV_16);
198
199 if (!oneshot)
200 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
106} 201}
107 202
108void __init apic_intr_init(void) 203/*
204 * Program the next event, relative to now
205 */
206static int lapic_next_event(unsigned long delta,
207 struct clock_event_device *evt)
208{
209 apic_write_around(APIC_TMICT, delta);
210 return 0;
211}
212
213/*
214 * Setup the lapic timer in periodic or oneshot mode
215 */
216static void lapic_timer_setup(enum clock_event_mode mode,
217 struct clock_event_device *evt)
218{
219 unsigned long flags;
220 unsigned int v;
221
222 /* Lapic used for broadcast ? */
223 if (!local_apic_timer_verify_ok)
224 return;
225
226 local_irq_save(flags);
227
228 switch (mode) {
229 case CLOCK_EVT_MODE_PERIODIC:
230 case CLOCK_EVT_MODE_ONESHOT:
231 __setup_APIC_LVTT(calibration_result,
232 mode != CLOCK_EVT_MODE_PERIODIC, 1);
233 break;
234 case CLOCK_EVT_MODE_UNUSED:
235 case CLOCK_EVT_MODE_SHUTDOWN:
236 v = apic_read(APIC_LVTT);
237 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
238 apic_write_around(APIC_LVTT, v);
239 break;
240 }
241
242 local_irq_restore(flags);
243}
244
245/*
246 * Local APIC timer broadcast function
247 */
248static void lapic_timer_broadcast(cpumask_t mask)
109{ 249{
110#ifdef CONFIG_SMP 250#ifdef CONFIG_SMP
111 smp_intr_init(); 251 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
112#endif 252#endif
113 /* self generated IPI for local APIC timer */ 253}
114 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
115 254
116 /* IPI vectors for APIC spurious and error interrupts */ 255/*
117 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt); 256 * Setup the local APIC timer for this CPU. Copy the initilized values
118 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt); 257 * of the boot CPU and register the clock event in the framework.
258 */
259static void __devinit setup_APIC_timer(void)
260{
261 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
119 262
120 /* thermal monitor LVT interrupt */ 263 memcpy(levt, &lapic_clockevent, sizeof(*levt));
121#ifdef CONFIG_X86_MCE_P4THERMAL 264 levt->cpumask = cpumask_of_cpu(smp_processor_id());
122 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt); 265
123#endif 266 clockevents_register_device(levt);
124} 267}
125 268
126/* Using APIC to generate smp_local_timer_interrupt? */ 269/*
127int using_apic_timer __read_mostly = 0; 270 * In this functions we calibrate APIC bus clocks to the external timer.
271 *
272 * We want to do the calibration only once since we want to have local timer
273 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
274 * frequency.
275 *
276 * This was previously done by reading the PIT/HPET and waiting for a wrap
277 * around to find out, that a tick has elapsed. I have a box, where the PIT
278 * readout is broken, so it never gets out of the wait loop again. This was
279 * also reported by others.
280 *
281 * Monitoring the jiffies value is inaccurate and the clockevents
282 * infrastructure allows us to do a simple substitution of the interrupt
283 * handler.
284 *
285 * The calibration routine also uses the pm_timer when possible, as the PIT
286 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
287 * back to normal later in the boot process).
288 */
289
290#define LAPIC_CAL_LOOPS (HZ/10)
128 291
129static int enabled_via_apicbase; 292static __initdata volatile int lapic_cal_loops = -1;
293static __initdata long lapic_cal_t1, lapic_cal_t2;
294static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
295static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
296static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
130 297
131void enable_NMI_through_LVT0 (void * dummy) 298/*
299 * Temporary interrupt handler.
300 */
301static void __init lapic_cal_handler(struct clock_event_device *dev)
132{ 302{
133 unsigned int v, ver; 303 unsigned long long tsc = 0;
304 long tapic = apic_read(APIC_TMCCT);
305 unsigned long pm = acpi_pm_read_early();
134 306
135 ver = apic_read(APIC_LVR); 307 if (cpu_has_tsc)
136 ver = GET_APIC_VERSION(ver); 308 rdtscll(tsc);
137 v = APIC_DM_NMI; /* unmask and set to NMI */ 309
138 if (!APIC_INTEGRATED(ver)) /* 82489DX */ 310 switch (lapic_cal_loops++) {
139 v |= APIC_LVT_LEVEL_TRIGGER; 311 case 0:
140 apic_write_around(APIC_LVT0, v); 312 lapic_cal_t1 = tapic;
313 lapic_cal_tsc1 = tsc;
314 lapic_cal_pm1 = pm;
315 lapic_cal_j1 = jiffies;
316 break;
317
318 case LAPIC_CAL_LOOPS:
319 lapic_cal_t2 = tapic;
320 lapic_cal_tsc2 = tsc;
321 if (pm < lapic_cal_pm1)
322 pm += ACPI_PM_OVRRUN;
323 lapic_cal_pm2 = pm;
324 lapic_cal_j2 = jiffies;
325 break;
326 }
141} 327}
142 328
143int get_physical_broadcast(void) 329/*
330 * Setup the boot APIC
331 *
332 * Calibrate and verify the result.
333 */
334void __init setup_boot_APIC_clock(void)
144{ 335{
145 if (modern_apic()) 336 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
146 return 0xff; 337 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
147 else 338 const long pm_thresh = pm_100ms/100;
148 return 0xf; 339 void (*real_handler)(struct clock_event_device *dev);
340 unsigned long deltaj;
341 long delta, deltapm;
342
343 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
344 "calibrating APIC timer ...\n");
345
346 local_irq_disable();
347
348 /* Replace the global interrupt handler */
349 real_handler = global_clock_event->event_handler;
350 global_clock_event->event_handler = lapic_cal_handler;
351
352 /*
353 * Setup the APIC counter to 1e9. There is no way the lapic
354 * can underflow in the 100ms detection time frame
355 */
356 __setup_APIC_LVTT(1000000000, 0, 0);
357
358 /* Let the interrupts run */
359 local_irq_enable();
360
361 while(lapic_cal_loops <= LAPIC_CAL_LOOPS);
362
363 local_irq_disable();
364
365 /* Restore the real event handler */
366 global_clock_event->event_handler = real_handler;
367
368 /* Build delta t1-t2 as apic timer counts down */
369 delta = lapic_cal_t1 - lapic_cal_t2;
370 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
371
372 /* Check, if the PM timer is available */
373 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
374 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
375
376 if (deltapm) {
377 unsigned long mult;
378 u64 res;
379
380 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
381
382 if (deltapm > (pm_100ms - pm_thresh) &&
383 deltapm < (pm_100ms + pm_thresh)) {
384 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
385 } else {
386 res = (((u64) deltapm) * mult) >> 22;
387 do_div(res, 1000000);
388 printk(KERN_WARNING "APIC calibration not consistent "
389 "with PM Timer: %ldms instead of 100ms\n",
390 (long)res);
391 /* Correct the lapic counter value */
392 res = (((u64) delta ) * pm_100ms);
393 do_div(res, deltapm);
394 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
395 "%lu (%ld)\n", (unsigned long) res, delta);
396 delta = (long) res;
397 }
398 }
399
400 /* Calculate the scaled math multiplication factor */
401 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
402 lapic_clockevent.max_delta_ns =
403 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
404 lapic_clockevent.min_delta_ns =
405 clockevent_delta2ns(0xF, &lapic_clockevent);
406
407 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
408
409 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
410 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
411 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
412 calibration_result);
413
414 if (cpu_has_tsc) {
415 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
416 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
417 "%ld.%04ld MHz.\n",
418 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
419 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
420 }
421
422 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
423 "%u.%04u MHz.\n",
424 calibration_result / (1000000 / HZ),
425 calibration_result % (1000000 / HZ));
426
427
428 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
429
430 /*
431 * Setup the apic timer manually
432 */
433 local_apic_timer_verify_ok = 1;
434 levt->event_handler = lapic_cal_handler;
435 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
436 lapic_cal_loops = -1;
437
438 /* Let the interrupts run */
439 local_irq_enable();
440
441 while(lapic_cal_loops <= LAPIC_CAL_LOOPS);
442
443 local_irq_disable();
444
445 /* Stop the lapic timer */
446 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
447
448 local_irq_enable();
449
450 /* Jiffies delta */
451 deltaj = lapic_cal_j2 - lapic_cal_j1;
452 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
453
454 /* Check, if the PM timer is available */
455 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
456 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
457
458 local_apic_timer_verify_ok = 0;
459
460 if (deltapm) {
461 if (deltapm > (pm_100ms - pm_thresh) &&
462 deltapm < (pm_100ms + pm_thresh)) {
463 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
464 /* Check, if the jiffies result is consistent */
465 if (deltaj < LAPIC_CAL_LOOPS-2 ||
466 deltaj > LAPIC_CAL_LOOPS+2) {
467 /*
468 * Not sure, what we can do about this one.
469 * When high resultion timers are active
470 * and the lapic timer does not stop in C3
471 * we are fine. Otherwise more trouble might
472 * be waiting. -- tglx
473 */
474 printk(KERN_WARNING "Global event device %s "
475 "has wrong frequency "
476 "(%lu ticks instead of %d)\n",
477 global_clock_event->name, deltaj,
478 LAPIC_CAL_LOOPS);
479 }
480 local_apic_timer_verify_ok = 1;
481 }
482 } else {
483 /* Check, if the jiffies result is consistent */
484 if (deltaj >= LAPIC_CAL_LOOPS-2 &&
485 deltaj <= LAPIC_CAL_LOOPS+2) {
486 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
487 local_apic_timer_verify_ok = 1;
488 }
489 }
490
491 if (!local_apic_timer_verify_ok) {
492 printk(KERN_WARNING
493 "APIC timer disabled due to verification failure.\n");
494 /* No broadcast on UP ! */
495 if (num_possible_cpus() == 1)
496 return;
497 } else
498 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
499
500 /* Setup the lapic or request the broadcast */
501 setup_APIC_timer();
502}
503
504void __devinit setup_secondary_APIC_clock(void)
505{
506 setup_APIC_timer();
149} 507}
150 508
151int get_maxlvt(void) 509/*
510 * The guts of the apic timer interrupt
511 */
512static void local_apic_timer_interrupt(void)
152{ 513{
153 unsigned int v, ver, maxlvt; 514 int cpu = smp_processor_id();
515 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
154 516
155 v = apic_read(APIC_LVR); 517 /*
156 ver = GET_APIC_VERSION(v); 518 * Normally we should not be here till LAPIC has been initialized but
157 /* 82489DXs do not report # of LVT entries. */ 519 * in some cases like kdump, its possible that there is a pending LAPIC
158 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2; 520 * timer interrupt from previous kernel's context and is delivered in
159 return maxlvt; 521 * new kernel the moment interrupts are enabled.
522 *
523 * Interrupts are enabled early and LAPIC is setup much later, hence
524 * its possible that when we get here evt->event_handler is NULL.
525 * Check for event_handler being NULL and discard the interrupt as
526 * spurious.
527 */
528 if (!evt->event_handler) {
529 printk(KERN_WARNING
530 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
531 /* Switch it off */
532 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
533 return;
534 }
535
536 per_cpu(irq_stat, cpu).apic_timer_irqs++;
537
538 evt->event_handler(evt);
160} 539}
161 540
541/*
542 * Local APIC timer interrupt. This is the most natural way for doing
543 * local interrupts, but local timer interrupts can be emulated by
544 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
545 *
546 * [ if a single-CPU system runs an SMP kernel then we call the local
547 * interrupt as well. Thus we cannot inline the local irq ... ]
548 */
549
550void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
551{
552 struct pt_regs *old_regs = set_irq_regs(regs);
553
554 /*
555 * NOTE! We'd better ACK the irq immediately,
556 * because timer handling can be slow.
557 */
558 ack_APIC_irq();
559 /*
560 * update_process_times() expects us to have done irq_enter().
561 * Besides, if we don't timer interrupts ignore the global
562 * interrupt lock, which is the WrongThing (tm) to do.
563 */
564 exit_idle();
565 irq_enter();
566 local_apic_timer_interrupt();
567 irq_exit();
568
569 set_irq_regs(old_regs);
570}
571
572int setup_profiling_timer(unsigned int multiplier)
573{
574 return -EINVAL;
575}
576
577/*
578 * Local APIC start and shutdown
579 */
580
581/**
582 * clear_local_APIC - shutdown the local APIC
583 *
584 * This is called, when a CPU is disabled and before rebooting, so the state of
585 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
586 * leftovers during boot.
587 */
162void clear_local_APIC(void) 588void clear_local_APIC(void)
163{ 589{
164 int maxlvt; 590 int maxlvt = lapic_get_maxlvt();
165 unsigned long v; 591 unsigned long v;
166 592
167 maxlvt = get_maxlvt();
168
169 /* 593 /*
170 * Masking an LVT entry can trigger a local APIC error 594 * Masking an LVT entry can trigger a local APIC error
171 * if the vector is zero. Mask LVTERR first to prevent this. 595 * if the vector is zero. Mask LVTERR first to prevent this.
@@ -189,7 +613,7 @@ void clear_local_APIC(void)
189 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED); 613 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
190 } 614 }
191 615
192/* lets not touch this if we didn't frob it */ 616 /* lets not touch this if we didn't frob it */
193#ifdef CONFIG_X86_MCE_P4THERMAL 617#ifdef CONFIG_X86_MCE_P4THERMAL
194 if (maxlvt >= 5) { 618 if (maxlvt >= 5) {
195 v = apic_read(APIC_LVTTHMR); 619 v = apic_read(APIC_LVTTHMR);
@@ -211,85 +635,18 @@ void clear_local_APIC(void)
211 if (maxlvt >= 5) 635 if (maxlvt >= 5)
212 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED); 636 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
213#endif 637#endif
214 v = GET_APIC_VERSION(apic_read(APIC_LVR)); 638 /* Integrated APIC (!82489DX) ? */
215 if (APIC_INTEGRATED(v)) { /* !82489DX */ 639 if (lapic_is_integrated()) {
216 if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */ 640 if (maxlvt > 3)
641 /* Clear ESR due to Pentium errata 3AP and 11AP */
217 apic_write(APIC_ESR, 0); 642 apic_write(APIC_ESR, 0);
218 apic_read(APIC_ESR); 643 apic_read(APIC_ESR);
219 } 644 }
220} 645}
221 646
222void __init connect_bsp_APIC(void) 647/**
223{ 648 * disable_local_APIC - clear and disable the local APIC
224 if (pic_mode) { 649 */
225 /*
226 * Do not trust the local APIC being empty at bootup.
227 */
228 clear_local_APIC();
229 /*
230 * PIC mode, enable APIC mode in the IMCR, i.e.
231 * connect BSP's local APIC to INT and NMI lines.
232 */
233 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
234 "enabling APIC mode.\n");
235 outb(0x70, 0x22);
236 outb(0x01, 0x23);
237 }
238 enable_apic_mode();
239}
240
241void disconnect_bsp_APIC(int virt_wire_setup)
242{
243 if (pic_mode) {
244 /*
245 * Put the board back into PIC mode (has an effect
246 * only on certain older boards). Note that APIC
247 * interrupts, including IPIs, won't work beyond
248 * this point! The only exception are INIT IPIs.
249 */
250 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
251 "entering PIC mode.\n");
252 outb(0x70, 0x22);
253 outb(0x00, 0x23);
254 }
255 else {
256 /* Go back to Virtual Wire compatibility mode */
257 unsigned long value;
258
259 /* For the spurious interrupt use vector F, and enable it */
260 value = apic_read(APIC_SPIV);
261 value &= ~APIC_VECTOR_MASK;
262 value |= APIC_SPIV_APIC_ENABLED;
263 value |= 0xf;
264 apic_write_around(APIC_SPIV, value);
265
266 if (!virt_wire_setup) {
267 /* For LVT0 make it edge triggered, active high, external and enabled */
268 value = apic_read(APIC_LVT0);
269 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
270 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
271 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
272 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
273 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
274 apic_write_around(APIC_LVT0, value);
275 }
276 else {
277 /* Disable LVT0 */
278 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
279 }
280
281 /* For LVT1 make it edge triggered, active high, nmi and enabled */
282 value = apic_read(APIC_LVT1);
283 value &= ~(
284 APIC_MODE_MASK | APIC_SEND_PENDING |
285 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
286 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
287 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
288 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
289 apic_write_around(APIC_LVT1, value);
290 }
291}
292
293void disable_local_APIC(void) 650void disable_local_APIC(void)
294{ 651{
295 unsigned long value; 652 unsigned long value;
@@ -304,8 +661,13 @@ void disable_local_APIC(void)
304 value &= ~APIC_SPIV_APIC_ENABLED; 661 value &= ~APIC_SPIV_APIC_ENABLED;
305 apic_write_around(APIC_SPIV, value); 662 apic_write_around(APIC_SPIV, value);
306 663
664 /*
665 * When LAPIC was disabled by the BIOS and enabled by the kernel,
666 * restore the disabled state.
667 */
307 if (enabled_via_apicbase) { 668 if (enabled_via_apicbase) {
308 unsigned int l, h; 669 unsigned int l, h;
670
309 rdmsr(MSR_IA32_APICBASE, l, h); 671 rdmsr(MSR_IA32_APICBASE, l, h);
310 l &= ~MSR_IA32_APICBASE_ENABLE; 672 l &= ~MSR_IA32_APICBASE_ENABLE;
311 wrmsr(MSR_IA32_APICBASE, l, h); 673 wrmsr(MSR_IA32_APICBASE, l, h);
@@ -313,6 +675,28 @@ void disable_local_APIC(void)
313} 675}
314 676
315/* 677/*
678 * If Linux enabled the LAPIC against the BIOS default disable it down before
679 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
680 * not power-off. Additionally clear all LVT entries before disable_local_APIC
681 * for the case where Linux didn't enable the LAPIC.
682 */
683void lapic_shutdown(void)
684{
685 unsigned long flags;
686
687 if (!cpu_has_apic)
688 return;
689
690 local_irq_save(flags);
691 clear_local_APIC();
692
693 if (enabled_via_apicbase)
694 disable_local_APIC();
695
696 local_irq_restore(flags);
697}
698
699/*
316 * This is to verify that we're looking at a real local APIC. 700 * This is to verify that we're looking at a real local APIC.
317 * Check these against your board if the CPUs aren't getting 701 * Check these against your board if the CPUs aren't getting
318 * started for no apparent reason. 702 * started for no apparent reason.
@@ -344,7 +728,7 @@ int __init verify_local_APIC(void)
344 reg1 = GET_APIC_VERSION(reg0); 728 reg1 = GET_APIC_VERSION(reg0);
345 if (reg1 == 0x00 || reg1 == 0xff) 729 if (reg1 == 0x00 || reg1 == 0xff)
346 return 0; 730 return 0;
347 reg1 = get_maxlvt(); 731 reg1 = lapic_get_maxlvt();
348 if (reg1 < 0x02 || reg1 == 0xff) 732 if (reg1 < 0x02 || reg1 == 0xff)
349 return 0; 733 return 0;
350 734
@@ -367,10 +751,15 @@ int __init verify_local_APIC(void)
367 return 1; 751 return 1;
368} 752}
369 753
754/**
755 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
756 */
370void __init sync_Arb_IDs(void) 757void __init sync_Arb_IDs(void)
371{ 758{
372 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 759 /*
373 And not needed on AMD */ 760 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
761 * needed on AMD.
762 */
374 if (modern_apic()) 763 if (modern_apic())
375 return; 764 return;
376 /* 765 /*
@@ -383,14 +772,12 @@ void __init sync_Arb_IDs(void)
383 | APIC_DM_INIT); 772 | APIC_DM_INIT);
384} 773}
385 774
386extern void __error_in_apic_c (void);
387
388/* 775/*
389 * An initial setup of the virtual wire mode. 776 * An initial setup of the virtual wire mode.
390 */ 777 */
391void __init init_bsp_APIC(void) 778void __init init_bsp_APIC(void)
392{ 779{
393 unsigned long value, ver; 780 unsigned long value;
394 781
395 /* 782 /*
396 * Don't do the setup now if we have a SMP BIOS as the 783 * Don't do the setup now if we have a SMP BIOS as the
@@ -399,9 +786,6 @@ void __init init_bsp_APIC(void)
399 if (smp_found_config || !cpu_has_apic) 786 if (smp_found_config || !cpu_has_apic)
400 return; 787 return;
401 788
402 value = apic_read(APIC_LVR);
403 ver = GET_APIC_VERSION(value);
404
405 /* 789 /*
406 * Do not trust the local APIC being empty at bootup. 790 * Do not trust the local APIC being empty at bootup.
407 */ 791 */
@@ -413,9 +797,10 @@ void __init init_bsp_APIC(void)
413 value = apic_read(APIC_SPIV); 797 value = apic_read(APIC_SPIV);
414 value &= ~APIC_VECTOR_MASK; 798 value &= ~APIC_VECTOR_MASK;
415 value |= APIC_SPIV_APIC_ENABLED; 799 value |= APIC_SPIV_APIC_ENABLED;
416 800
417 /* This bit is reserved on P4/Xeon and should be cleared */ 801 /* This bit is reserved on P4/Xeon and should be cleared */
418 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15)) 802 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
803 (boot_cpu_data.x86 == 15))
419 value &= ~APIC_SPIV_FOCUS_DISABLED; 804 value &= ~APIC_SPIV_FOCUS_DISABLED;
420 else 805 else
421 value |= APIC_SPIV_FOCUS_DISABLED; 806 value |= APIC_SPIV_FOCUS_DISABLED;
@@ -427,14 +812,17 @@ void __init init_bsp_APIC(void)
427 */ 812 */
428 apic_write_around(APIC_LVT0, APIC_DM_EXTINT); 813 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
429 value = APIC_DM_NMI; 814 value = APIC_DM_NMI;
430 if (!APIC_INTEGRATED(ver)) /* 82489DX */ 815 if (!lapic_is_integrated()) /* 82489DX */
431 value |= APIC_LVT_LEVEL_TRIGGER; 816 value |= APIC_LVT_LEVEL_TRIGGER;
432 apic_write_around(APIC_LVT1, value); 817 apic_write_around(APIC_LVT1, value);
433} 818}
434 819
820/**
821 * setup_local_APIC - setup the local APIC
822 */
435void __devinit setup_local_APIC(void) 823void __devinit setup_local_APIC(void)
436{ 824{
437 unsigned long oldvalue, value, ver, maxlvt; 825 unsigned long oldvalue, value, maxlvt, integrated;
438 int i, j; 826 int i, j;
439 827
440 /* Pound the ESR really hard over the head with a big hammer - mbligh */ 828 /* Pound the ESR really hard over the head with a big hammer - mbligh */
@@ -445,11 +833,7 @@ void __devinit setup_local_APIC(void)
445 apic_write(APIC_ESR, 0); 833 apic_write(APIC_ESR, 0);
446 } 834 }
447 835
448 value = apic_read(APIC_LVR); 836 integrated = lapic_is_integrated();
449 ver = GET_APIC_VERSION(value);
450
451 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
452 __error_in_apic_c();
453 837
454 /* 838 /*
455 * Double-check whether this APIC is really registered. 839 * Double-check whether this APIC is really registered.
@@ -520,13 +904,10 @@ void __devinit setup_local_APIC(void)
520 * like LRU than MRU (the short-term load is more even across CPUs). 904 * like LRU than MRU (the short-term load is more even across CPUs).
521 * See also the comment in end_level_ioapic_irq(). --macro 905 * See also the comment in end_level_ioapic_irq(). --macro
522 */ 906 */
523#if 1 907
524 /* Enable focus processor (bit==0) */ 908 /* Enable focus processor (bit==0) */
525 value &= ~APIC_SPIV_FOCUS_DISABLED; 909 value &= ~APIC_SPIV_FOCUS_DISABLED;
526#else 910
527 /* Disable focus processor (bit==1) */
528 value |= APIC_SPIV_FOCUS_DISABLED;
529#endif
530 /* 911 /*
531 * Set spurious IRQ vector 912 * Set spurious IRQ vector
532 */ 913 */
@@ -562,17 +943,18 @@ void __devinit setup_local_APIC(void)
562 value = APIC_DM_NMI; 943 value = APIC_DM_NMI;
563 else 944 else
564 value = APIC_DM_NMI | APIC_LVT_MASKED; 945 value = APIC_DM_NMI | APIC_LVT_MASKED;
565 if (!APIC_INTEGRATED(ver)) /* 82489DX */ 946 if (!integrated) /* 82489DX */
566 value |= APIC_LVT_LEVEL_TRIGGER; 947 value |= APIC_LVT_LEVEL_TRIGGER;
567 apic_write_around(APIC_LVT1, value); 948 apic_write_around(APIC_LVT1, value);
568 949
569 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */ 950 if (integrated && !esr_disable) { /* !82489DX */
570 maxlvt = get_maxlvt(); 951 maxlvt = lapic_get_maxlvt();
571 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */ 952 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
572 apic_write(APIC_ESR, 0); 953 apic_write(APIC_ESR, 0);
573 oldvalue = apic_read(APIC_ESR); 954 oldvalue = apic_read(APIC_ESR);
574 955
575 value = ERROR_APIC_VECTOR; // enables sending errors 956 /* enables sending errors */
957 value = ERROR_APIC_VECTOR;
576 apic_write_around(APIC_LVTERR, value); 958 apic_write_around(APIC_LVTERR, value);
577 /* 959 /*
578 * spec says clear errors after enabling vector. 960 * spec says clear errors after enabling vector.
@@ -585,207 +967,30 @@ void __devinit setup_local_APIC(void)
585 "vector: 0x%08lx after: 0x%08lx\n", 967 "vector: 0x%08lx after: 0x%08lx\n",
586 oldvalue, value); 968 oldvalue, value);
587 } else { 969 } else {
588 if (esr_disable) 970 if (esr_disable)
589 /* 971 /*
590 * Something untraceble is creating bad interrupts on 972 * Something untraceble is creating bad interrupts on
591 * secondary quads ... for the moment, just leave the 973 * secondary quads ... for the moment, just leave the
592 * ESR disabled - we can't do anything useful with the 974 * ESR disabled - we can't do anything useful with the
593 * errors anyway - mbligh 975 * errors anyway - mbligh
594 */ 976 */
595 printk("Leaving ESR disabled.\n"); 977 printk(KERN_INFO "Leaving ESR disabled.\n");
596 else 978 else
597 printk("No ESR for 82489DX.\n"); 979 printk(KERN_INFO "No ESR for 82489DX.\n");
598 } 980 }
599 981
982 /* Disable the local apic timer */
983 value = apic_read(APIC_LVTT);
984 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
985 apic_write_around(APIC_LVTT, value);
986
600 setup_apic_nmi_watchdog(NULL); 987 setup_apic_nmi_watchdog(NULL);
601 apic_pm_activate(); 988 apic_pm_activate();
602} 989}
603 990
604/* 991/*
605 * If Linux enabled the LAPIC against the BIOS default 992 * Detect and initialize APIC
606 * disable it down before re-entering the BIOS on shutdown.
607 * Otherwise the BIOS may get confused and not power-off.
608 * Additionally clear all LVT entries before disable_local_APIC
609 * for the case where Linux didn't enable the LAPIC.
610 */ 993 */
611void lapic_shutdown(void)
612{
613 unsigned long flags;
614
615 if (!cpu_has_apic)
616 return;
617
618 local_irq_save(flags);
619 clear_local_APIC();
620
621 if (enabled_via_apicbase)
622 disable_local_APIC();
623
624 local_irq_restore(flags);
625}
626
627#ifdef CONFIG_PM
628
629static struct {
630 int active;
631 /* r/w apic fields */
632 unsigned int apic_id;
633 unsigned int apic_taskpri;
634 unsigned int apic_ldr;
635 unsigned int apic_dfr;
636 unsigned int apic_spiv;
637 unsigned int apic_lvtt;
638 unsigned int apic_lvtpc;
639 unsigned int apic_lvt0;
640 unsigned int apic_lvt1;
641 unsigned int apic_lvterr;
642 unsigned int apic_tmict;
643 unsigned int apic_tdcr;
644 unsigned int apic_thmr;
645} apic_pm_state;
646
647static int lapic_suspend(struct sys_device *dev, pm_message_t state)
648{
649 unsigned long flags;
650 int maxlvt;
651
652 if (!apic_pm_state.active)
653 return 0;
654
655 maxlvt = get_maxlvt();
656
657 apic_pm_state.apic_id = apic_read(APIC_ID);
658 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
659 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
660 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
661 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
662 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
663 if (maxlvt >= 4)
664 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
665 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
666 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
667 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
668 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
669 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
670#ifdef CONFIG_X86_MCE_P4THERMAL
671 if (maxlvt >= 5)
672 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
673#endif
674
675 local_irq_save(flags);
676 disable_local_APIC();
677 local_irq_restore(flags);
678 return 0;
679}
680
681static int lapic_resume(struct sys_device *dev)
682{
683 unsigned int l, h;
684 unsigned long flags;
685 int maxlvt;
686
687 if (!apic_pm_state.active)
688 return 0;
689
690 maxlvt = get_maxlvt();
691
692 local_irq_save(flags);
693
694 /*
695 * Make sure the APICBASE points to the right address
696 *
697 * FIXME! This will be wrong if we ever support suspend on
698 * SMP! We'll need to do this as part of the CPU restore!
699 */
700 rdmsr(MSR_IA32_APICBASE, l, h);
701 l &= ~MSR_IA32_APICBASE_BASE;
702 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
703 wrmsr(MSR_IA32_APICBASE, l, h);
704
705 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
706 apic_write(APIC_ID, apic_pm_state.apic_id);
707 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
708 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
709 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
710 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
711 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
712 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
713#ifdef CONFIG_X86_MCE_P4THERMAL
714 if (maxlvt >= 5)
715 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
716#endif
717 if (maxlvt >= 4)
718 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
719 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
720 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
721 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
722 apic_write(APIC_ESR, 0);
723 apic_read(APIC_ESR);
724 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
725 apic_write(APIC_ESR, 0);
726 apic_read(APIC_ESR);
727 local_irq_restore(flags);
728 return 0;
729}
730
731/*
732 * This device has no shutdown method - fully functioning local APICs
733 * are needed on every CPU up until machine_halt/restart/poweroff.
734 */
735
736static struct sysdev_class lapic_sysclass = {
737 set_kset_name("lapic"),
738 .resume = lapic_resume,
739 .suspend = lapic_suspend,
740};
741
742static struct sys_device device_lapic = {
743 .id = 0,
744 .cls = &lapic_sysclass,
745};
746
747static void __devinit apic_pm_activate(void)
748{
749 apic_pm_state.active = 1;
750}
751
752static int __init init_lapic_sysfs(void)
753{
754 int error;
755
756 if (!cpu_has_apic)
757 return 0;
758 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
759
760 error = sysdev_class_register(&lapic_sysclass);
761 if (!error)
762 error = sysdev_register(&device_lapic);
763 return error;
764}
765device_initcall(init_lapic_sysfs);
766
767#else /* CONFIG_PM */
768
769static void apic_pm_activate(void) { }
770
771#endif /* CONFIG_PM */
772
773/*
774 * Detect and enable local APICs on non-SMP boards.
775 * Original code written by Keir Fraser.
776 */
777
778static int __init apic_set_verbosity(char *str)
779{
780 if (strcmp("debug", str) == 0)
781 apic_verbosity = APIC_DEBUG;
782 else if (strcmp("verbose", str) == 0)
783 apic_verbosity = APIC_VERBOSE;
784 return 1;
785}
786
787__setup("apic=", apic_set_verbosity);
788
789static int __init detect_init_APIC (void) 994static int __init detect_init_APIC (void)
790{ 995{
791 u32 h, l, features; 996 u32 h, l, features;
@@ -797,7 +1002,7 @@ static int __init detect_init_APIC (void)
797 switch (boot_cpu_data.x86_vendor) { 1002 switch (boot_cpu_data.x86_vendor) {
798 case X86_VENDOR_AMD: 1003 case X86_VENDOR_AMD:
799 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) || 1004 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
800 (boot_cpu_data.x86 == 15)) 1005 (boot_cpu_data.x86 == 15))
801 break; 1006 break;
802 goto no_apic; 1007 goto no_apic;
803 case X86_VENDOR_INTEL: 1008 case X86_VENDOR_INTEL:
@@ -811,23 +1016,23 @@ static int __init detect_init_APIC (void)
811 1016
812 if (!cpu_has_apic) { 1017 if (!cpu_has_apic) {
813 /* 1018 /*
814 * Over-ride BIOS and try to enable the local 1019 * Over-ride BIOS and try to enable the local APIC only if
815 * APIC only if "lapic" specified. 1020 * "lapic" specified.
816 */ 1021 */
817 if (enable_local_apic <= 0) { 1022 if (enable_local_apic <= 0) {
818 printk("Local APIC disabled by BIOS -- " 1023 printk(KERN_INFO "Local APIC disabled by BIOS -- "
819 "you can enable it with \"lapic\"\n"); 1024 "you can enable it with \"lapic\"\n");
820 return -1; 1025 return -1;
821 } 1026 }
822 /* 1027 /*
823 * Some BIOSes disable the local APIC in the 1028 * Some BIOSes disable the local APIC in the APIC_BASE
824 * APIC_BASE MSR. This can only be done in 1029 * MSR. This can only be done in software for Intel P6 or later
825 * software for Intel P6 or later and AMD K7 1030 * and AMD K7 (Model > 1) or later.
826 * (Model > 1) or later.
827 */ 1031 */
828 rdmsr(MSR_IA32_APICBASE, l, h); 1032 rdmsr(MSR_IA32_APICBASE, l, h);
829 if (!(l & MSR_IA32_APICBASE_ENABLE)) { 1033 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
830 printk("Local APIC disabled by BIOS -- reenabling.\n"); 1034 printk(KERN_INFO
1035 "Local APIC disabled by BIOS -- reenabling.\n");
831 l &= ~MSR_IA32_APICBASE_BASE; 1036 l &= ~MSR_IA32_APICBASE_BASE;
832 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE; 1037 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
833 wrmsr(MSR_IA32_APICBASE, l, h); 1038 wrmsr(MSR_IA32_APICBASE, l, h);
@@ -840,7 +1045,7 @@ static int __init detect_init_APIC (void)
840 */ 1045 */
841 features = cpuid_edx(1); 1046 features = cpuid_edx(1);
842 if (!(features & (1 << X86_FEATURE_APIC))) { 1047 if (!(features & (1 << X86_FEATURE_APIC))) {
843 printk("Could not enable APIC!\n"); 1048 printk(KERN_WARNING "Could not enable APIC!\n");
844 return -1; 1049 return -1;
845 } 1050 }
846 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability); 1051 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
@@ -854,17 +1059,20 @@ static int __init detect_init_APIC (void)
854 if (nmi_watchdog != NMI_NONE) 1059 if (nmi_watchdog != NMI_NONE)
855 nmi_watchdog = NMI_LOCAL_APIC; 1060 nmi_watchdog = NMI_LOCAL_APIC;
856 1061
857 printk("Found and enabled local APIC!\n"); 1062 printk(KERN_INFO "Found and enabled local APIC!\n");
858 1063
859 apic_pm_activate(); 1064 apic_pm_activate();
860 1065
861 return 0; 1066 return 0;
862 1067
863no_apic: 1068no_apic:
864 printk("No local APIC present or hardware disabled\n"); 1069 printk(KERN_INFO "No local APIC present or hardware disabled\n");
865 return -1; 1070 return -1;
866} 1071}
867 1072
1073/**
1074 * init_apic_mappings - initialize APIC mappings
1075 */
868void __init init_apic_mappings(void) 1076void __init init_apic_mappings(void)
869{ 1077{
870 unsigned long apic_phys; 1078 unsigned long apic_phys;
@@ -924,387 +1132,96 @@ fake_ioapic_page:
924} 1132}
925 1133
926/* 1134/*
927 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts 1135 * This initializes the IO-APIC and APIC hardware if this is
928 * per second. We assume that the caller has already set up the local 1136 * a UP kernel.
929 * APIC.
930 *
931 * The APIC timer is not exactly sync with the external timer chip, it
932 * closely follows bus clocks.
933 */
934
935/*
936 * The timer chip is already set up at HZ interrupts per second here,
937 * but we do not accept timer interrupts yet. We only allow the BP
938 * to calibrate.
939 */
940static unsigned int __devinit get_8254_timer_count(void)
941{
942 unsigned long flags;
943
944 unsigned int count;
945
946 spin_lock_irqsave(&i8253_lock, flags);
947
948 outb_p(0x00, PIT_MODE);
949 count = inb_p(PIT_CH0);
950 count |= inb_p(PIT_CH0) << 8;
951
952 spin_unlock_irqrestore(&i8253_lock, flags);
953
954 return count;
955}
956
957/* next tick in 8254 can be caught by catching timer wraparound */
958static void __devinit wait_8254_wraparound(void)
959{
960 unsigned int curr_count, prev_count;
961
962 curr_count = get_8254_timer_count();
963 do {
964 prev_count = curr_count;
965 curr_count = get_8254_timer_count();
966
967 /* workaround for broken Mercury/Neptune */
968 if (prev_count >= curr_count + 0x100)
969 curr_count = get_8254_timer_count();
970
971 } while (prev_count >= curr_count);
972}
973
974/*
975 * Default initialization for 8254 timers. If we use other timers like HPET,
976 * we override this later
977 */
978void (*wait_timer_tick)(void) __devinitdata = wait_8254_wraparound;
979
980/*
981 * This function sets up the local APIC timer, with a timeout of
982 * 'clocks' APIC bus clock. During calibration we actually call
983 * this function twice on the boot CPU, once with a bogus timeout
984 * value, second time for real. The other (noncalibrating) CPUs
985 * call this function only once, with the real, calibrated value.
986 *
987 * We do reads before writes even if unnecessary, to get around the
988 * P5 APIC double write bug.
989 */ 1137 */
990 1138int __init APIC_init_uniprocessor (void)
991#define APIC_DIVISOR 16
992
993static void __setup_APIC_LVTT(unsigned int clocks)
994{ 1139{
995 unsigned int lvtt_value, tmp_value, ver; 1140 if (enable_local_apic < 0)
996 int cpu = smp_processor_id(); 1141 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
997
998 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
999 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
1000 if (!APIC_INTEGRATED(ver))
1001 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
1002
1003 if (cpu_isset(cpu, timer_bcast_ipi))
1004 lvtt_value |= APIC_LVT_MASKED;
1005 1142
1006 apic_write_around(APIC_LVTT, lvtt_value); 1143 if (!smp_found_config && !cpu_has_apic)
1144 return -1;
1007 1145
1008 /* 1146 /*
1009 * Divide PICLK by 16 1147 * Complain if the BIOS pretends there is one.
1010 */ 1148 */
1011 tmp_value = apic_read(APIC_TDCR); 1149 if (!cpu_has_apic &&
1012 apic_write_around(APIC_TDCR, (tmp_value 1150 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1013 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) 1151 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1014 | APIC_TDR_DIV_16); 1152 boot_cpu_physical_apicid);
1015 1153 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1016 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR); 1154 return -1;
1017} 1155 }
1018 1156
1019static void __devinit setup_APIC_timer(unsigned int clocks) 1157 verify_local_APIC();
1020{
1021 unsigned long flags;
1022 1158
1023 local_irq_save(flags); 1159 connect_bsp_APIC();
1024 1160
1025 /* 1161 /*
1026 * Wait for IRQ0's slice: 1162 * Hack: In case of kdump, after a crash, kernel might be booting
1163 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1164 * might be zero if read from MP tables. Get it from LAPIC.
1027 */ 1165 */
1028 wait_timer_tick(); 1166#ifdef CONFIG_CRASH_DUMP
1167 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1168#endif
1169 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1029 1170
1030 __setup_APIC_LVTT(clocks); 1171 setup_local_APIC();
1031 1172
1032 local_irq_restore(flags); 1173#ifdef CONFIG_X86_IO_APIC
1174 if (smp_found_config)
1175 if (!skip_ioapic_setup && nr_ioapics)
1176 setup_IO_APIC();
1177#endif
1178 setup_boot_clock();
1179
1180 return 0;
1033} 1181}
1034 1182
1035/* 1183/*
1036 * In this function we calibrate APIC bus clocks to the external 1184 * APIC command line parameters
1037 * timer. Unfortunately we cannot use jiffies and the timer irq
1038 * to calibrate, since some later bootup code depends on getting
1039 * the first irq? Ugh.
1040 *
1041 * We want to do the calibration only once since we
1042 * want to have local timer irqs syncron. CPUs connected
1043 * by the same APIC bus have the very same bus frequency.
1044 * And we want to have irqs off anyways, no accidental
1045 * APIC irq that way.
1046 */ 1185 */
1047 1186static int __init parse_lapic(char *arg)
1048static int __init calibrate_APIC_clock(void)
1049{
1050 unsigned long long t1 = 0, t2 = 0;
1051 long tt1, tt2;
1052 long result;
1053 int i;
1054 const int LOOPS = HZ/10;
1055
1056 apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
1057
1058 /*
1059 * Put whatever arbitrary (but long enough) timeout
1060 * value into the APIC clock, we just want to get the
1061 * counter running for calibration.
1062 */
1063 __setup_APIC_LVTT(1000000000);
1064
1065 /*
1066 * The timer chip counts down to zero. Let's wait
1067 * for a wraparound to start exact measurement:
1068 * (the current tick might have been already half done)
1069 */
1070
1071 wait_timer_tick();
1072
1073 /*
1074 * We wrapped around just now. Let's start:
1075 */
1076 if (cpu_has_tsc)
1077 rdtscll(t1);
1078 tt1 = apic_read(APIC_TMCCT);
1079
1080 /*
1081 * Let's wait LOOPS wraprounds:
1082 */
1083 for (i = 0; i < LOOPS; i++)
1084 wait_timer_tick();
1085
1086 tt2 = apic_read(APIC_TMCCT);
1087 if (cpu_has_tsc)
1088 rdtscll(t2);
1089
1090 /*
1091 * The APIC bus clock counter is 32 bits only, it
1092 * might have overflown, but note that we use signed
1093 * longs, thus no extra care needed.
1094 *
1095 * underflown to be exact, as the timer counts down ;)
1096 */
1097
1098 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
1099
1100 if (cpu_has_tsc)
1101 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
1102 "%ld.%04ld MHz.\n",
1103 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
1104 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
1105
1106 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
1107 "%ld.%04ld MHz.\n",
1108 result/(1000000/HZ),
1109 result%(1000000/HZ));
1110
1111 return result;
1112}
1113
1114static unsigned int calibration_result;
1115
1116void __init setup_boot_APIC_clock(void)
1117{
1118 unsigned long flags;
1119 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
1120 using_apic_timer = 1;
1121
1122 local_irq_save(flags);
1123
1124 calibration_result = calibrate_APIC_clock();
1125 /*
1126 * Now set up the timer for real.
1127 */
1128 setup_APIC_timer(calibration_result);
1129
1130 local_irq_restore(flags);
1131}
1132
1133void __devinit setup_secondary_APIC_clock(void)
1134{
1135 setup_APIC_timer(calibration_result);
1136}
1137
1138void disable_APIC_timer(void)
1139{
1140 if (using_apic_timer) {
1141 unsigned long v;
1142
1143 v = apic_read(APIC_LVTT);
1144 /*
1145 * When an illegal vector value (0-15) is written to an LVT
1146 * entry and delivery mode is Fixed, the APIC may signal an
1147 * illegal vector error, with out regard to whether the mask
1148 * bit is set or whether an interrupt is actually seen on input.
1149 *
1150 * Boot sequence might call this function when the LVTT has
1151 * '0' vector value. So make sure vector field is set to
1152 * valid value.
1153 */
1154 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1155 apic_write_around(APIC_LVTT, v);
1156 }
1157}
1158
1159void enable_APIC_timer(void)
1160{ 1187{
1161 int cpu = smp_processor_id(); 1188 enable_local_apic = 1;
1162 1189 return 0;
1163 if (using_apic_timer &&
1164 !cpu_isset(cpu, timer_bcast_ipi)) {
1165 unsigned long v;
1166
1167 v = apic_read(APIC_LVTT);
1168 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
1169 }
1170} 1190}
1191early_param("lapic", parse_lapic);
1171 1192
1172void switch_APIC_timer_to_ipi(void *cpumask) 1193static int __init parse_nolapic(char *arg)
1173{ 1194{
1174 cpumask_t mask = *(cpumask_t *)cpumask; 1195 enable_local_apic = -1;
1175 int cpu = smp_processor_id(); 1196 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1176 1197 return 0;
1177 if (cpu_isset(cpu, mask) &&
1178 !cpu_isset(cpu, timer_bcast_ipi)) {
1179 disable_APIC_timer();
1180 cpu_set(cpu, timer_bcast_ipi);
1181 }
1182} 1198}
1183EXPORT_SYMBOL(switch_APIC_timer_to_ipi); 1199early_param("nolapic", parse_nolapic);
1184 1200
1185void switch_ipi_to_APIC_timer(void *cpumask) 1201static int __init apic_set_verbosity(char *str)
1186{ 1202{
1187 cpumask_t mask = *(cpumask_t *)cpumask; 1203 if (strcmp("debug", str) == 0)
1188 int cpu = smp_processor_id(); 1204 apic_verbosity = APIC_DEBUG;
1189 1205 else if (strcmp("verbose", str) == 0)
1190 if (cpu_isset(cpu, mask) && 1206 apic_verbosity = APIC_VERBOSE;
1191 cpu_isset(cpu, timer_bcast_ipi)) { 1207 return 1;
1192 cpu_clear(cpu, timer_bcast_ipi);
1193 enable_APIC_timer();
1194 }
1195} 1208}
1196EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
1197
1198#undef APIC_DIVISOR
1199 1209
1200/* 1210__setup("apic=", apic_set_verbosity);
1201 * Local timer interrupt handler. It does both profiling and
1202 * process statistics/rescheduling.
1203 *
1204 * We do profiling in every local tick, statistics/rescheduling
1205 * happen only every 'profiling multiplier' ticks. The default
1206 * multiplier is 1 and it can be changed by writing the new multiplier
1207 * value into /proc/profile.
1208 */
1209
1210inline void smp_local_timer_interrupt(void)
1211{
1212 profile_tick(CPU_PROFILING);
1213#ifdef CONFIG_SMP
1214 update_process_times(user_mode_vm(get_irq_regs()));
1215#endif
1216 1211
1217 /*
1218 * We take the 'long' return path, and there every subsystem
1219 * grabs the apropriate locks (kernel lock/ irq lock).
1220 *
1221 * we might want to decouple profiling from the 'long path',
1222 * and do the profiling totally in assembly.
1223 *
1224 * Currently this isn't too much of an issue (performance wise),
1225 * we can take more than 100K local irqs per second on a 100 MHz P5.
1226 */
1227}
1228 1212
1229/* 1213/*
1230 * Local APIC timer interrupt. This is the most natural way for doing 1214 * Local APIC interrupts
1231 * local interrupts, but local timer interrupts can be emulated by
1232 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1233 *
1234 * [ if a single-CPU system runs an SMP kernel then we call the local
1235 * interrupt as well. Thus we cannot inline the local irq ... ]
1236 */ 1215 */
1237 1216
1238fastcall void smp_apic_timer_interrupt(struct pt_regs *regs)
1239{
1240 struct pt_regs *old_regs = set_irq_regs(regs);
1241 int cpu = smp_processor_id();
1242
1243 /*
1244 * the NMI deadlock-detector uses this.
1245 */
1246 per_cpu(irq_stat, cpu).apic_timer_irqs++;
1247
1248 /*
1249 * NOTE! We'd better ACK the irq immediately,
1250 * because timer handling can be slow.
1251 */
1252 ack_APIC_irq();
1253 /*
1254 * update_process_times() expects us to have done irq_enter().
1255 * Besides, if we don't timer interrupts ignore the global
1256 * interrupt lock, which is the WrongThing (tm) to do.
1257 */
1258 irq_enter();
1259 smp_local_timer_interrupt();
1260 irq_exit();
1261 set_irq_regs(old_regs);
1262}
1263
1264#ifndef CONFIG_SMP
1265static void up_apic_timer_interrupt_call(void)
1266{
1267 int cpu = smp_processor_id();
1268
1269 /*
1270 * the NMI deadlock-detector uses this.
1271 */
1272 per_cpu(irq_stat, cpu).apic_timer_irqs++;
1273
1274 smp_local_timer_interrupt();
1275}
1276#endif
1277
1278void smp_send_timer_broadcast_ipi(void)
1279{
1280 cpumask_t mask;
1281
1282 cpus_and(mask, cpu_online_map, timer_bcast_ipi);
1283 if (!cpus_empty(mask)) {
1284#ifdef CONFIG_SMP
1285 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
1286#else
1287 /*
1288 * We can directly call the apic timer interrupt handler
1289 * in UP case. Minus all irq related functions
1290 */
1291 up_apic_timer_interrupt_call();
1292#endif
1293 }
1294}
1295
1296int setup_profiling_timer(unsigned int multiplier)
1297{
1298 return -EINVAL;
1299}
1300
1301/* 1217/*
1302 * This interrupt should _never_ happen with our APIC/SMP architecture 1218 * This interrupt should _never_ happen with our APIC/SMP architecture
1303 */ 1219 */
1304fastcall void smp_spurious_interrupt(struct pt_regs *regs) 1220void smp_spurious_interrupt(struct pt_regs *regs)
1305{ 1221{
1306 unsigned long v; 1222 unsigned long v;
1307 1223
1224 exit_idle();
1308 irq_enter(); 1225 irq_enter();
1309 /* 1226 /*
1310 * Check if this really is a spurious interrupt and ACK it 1227 * Check if this really is a spurious interrupt and ACK it
@@ -1316,19 +1233,19 @@ fastcall void smp_spurious_interrupt(struct pt_regs *regs)
1316 ack_APIC_irq(); 1233 ack_APIC_irq();
1317 1234
1318 /* see sw-dev-man vol 3, chapter 7.4.13.5 */ 1235 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1319 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n", 1236 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1320 smp_processor_id()); 1237 "should never happen.\n", smp_processor_id());
1321 irq_exit(); 1238 irq_exit();
1322} 1239}
1323 1240
1324/* 1241/*
1325 * This interrupt should never happen with our APIC/SMP architecture 1242 * This interrupt should never happen with our APIC/SMP architecture
1326 */ 1243 */
1327 1244void smp_error_interrupt(struct pt_regs *regs)
1328fastcall void smp_error_interrupt(struct pt_regs *regs)
1329{ 1245{
1330 unsigned long v, v1; 1246 unsigned long v, v1;
1331 1247
1248 exit_idle();
1332 irq_enter(); 1249 irq_enter();
1333 /* First tickle the hardware, only then report what went on. -- REW */ 1250 /* First tickle the hardware, only then report what went on. -- REW */
1334 v = apic_read(APIC_ESR); 1251 v = apic_read(APIC_ESR);
@@ -1348,69 +1265,261 @@ fastcall void smp_error_interrupt(struct pt_regs *regs)
1348 7: Illegal register address 1265 7: Illegal register address
1349 */ 1266 */
1350 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n", 1267 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1351 smp_processor_id(), v , v1); 1268 smp_processor_id(), v , v1);
1352 irq_exit(); 1269 irq_exit();
1353} 1270}
1354 1271
1355/* 1272/*
1356 * This initializes the IO-APIC and APIC hardware if this is 1273 * Initialize APIC interrupts
1357 * a UP kernel.
1358 */ 1274 */
1359int __init APIC_init_uniprocessor (void) 1275void __init apic_intr_init(void)
1360{ 1276{
1361 if (enable_local_apic < 0) 1277#ifdef CONFIG_SMP
1362 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability); 1278 smp_intr_init();
1279#endif
1280 /* self generated IPI for local APIC timer */
1281 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1363 1282
1364 if (!smp_found_config && !cpu_has_apic) 1283 /* IPI vectors for APIC spurious and error interrupts */
1365 return -1; 1284 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1285 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1366 1286
1367 /* 1287 /* thermal monitor LVT interrupt */
1368 * Complain if the BIOS pretends there is one. 1288#ifdef CONFIG_X86_MCE_P4THERMAL
1369 */ 1289 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1370 if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) { 1290#endif
1371 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n", 1291}
1372 boot_cpu_physical_apicid); 1292
1373 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability); 1293/**
1374 return -1; 1294 * connect_bsp_APIC - attach the APIC to the interrupt system
1295 */
1296void __init connect_bsp_APIC(void)
1297{
1298 if (pic_mode) {
1299 /*
1300 * Do not trust the local APIC being empty at bootup.
1301 */
1302 clear_local_APIC();
1303 /*
1304 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1305 * local APIC to INT and NMI lines.
1306 */
1307 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1308 "enabling APIC mode.\n");
1309 outb(0x70, 0x22);
1310 outb(0x01, 0x23);
1375 } 1311 }
1312 enable_apic_mode();
1313}
1376 1314
1377 verify_local_APIC(); 1315/**
1316 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1317 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1318 *
1319 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1320 * APIC is disabled.
1321 */
1322void disconnect_bsp_APIC(int virt_wire_setup)
1323{
1324 if (pic_mode) {
1325 /*
1326 * Put the board back into PIC mode (has an effect only on
1327 * certain older boards). Note that APIC interrupts, including
1328 * IPIs, won't work beyond this point! The only exception are
1329 * INIT IPIs.
1330 */
1331 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1332 "entering PIC mode.\n");
1333 outb(0x70, 0x22);
1334 outb(0x00, 0x23);
1335 } else {
1336 /* Go back to Virtual Wire compatibility mode */
1337 unsigned long value;
1378 1338
1379 connect_bsp_APIC(); 1339 /* For the spurious interrupt use vector F, and enable it */
1340 value = apic_read(APIC_SPIV);
1341 value &= ~APIC_VECTOR_MASK;
1342 value |= APIC_SPIV_APIC_ENABLED;
1343 value |= 0xf;
1344 apic_write_around(APIC_SPIV, value);
1380 1345
1381 /* 1346 if (!virt_wire_setup) {
1382 * Hack: In case of kdump, after a crash, kernel might be booting 1347 /*
1383 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid 1348 * For LVT0 make it edge triggered, active high,
1384 * might be zero if read from MP tables. Get it from LAPIC. 1349 * external and enabled
1385 */ 1350 */
1386#ifdef CONFIG_CRASH_DUMP 1351 value = apic_read(APIC_LVT0);
1387 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID)); 1352 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1388#endif 1353 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1389 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid); 1354 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1355 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1356 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1357 apic_write_around(APIC_LVT0, value);
1358 } else {
1359 /* Disable LVT0 */
1360 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1361 }
1390 1362
1391 setup_local_APIC(); 1363 /*
1364 * For LVT1 make it edge triggered, active high, nmi and
1365 * enabled
1366 */
1367 value = apic_read(APIC_LVT1);
1368 value &= ~(
1369 APIC_MODE_MASK | APIC_SEND_PENDING |
1370 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1371 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1372 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1373 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1374 apic_write_around(APIC_LVT1, value);
1375 }
1376}
1392 1377
1393#ifdef CONFIG_X86_IO_APIC 1378/*
1394 if (smp_found_config) 1379 * Power management
1395 if (!skip_ioapic_setup && nr_ioapics) 1380 */
1396 setup_IO_APIC(); 1381#ifdef CONFIG_PM
1382
1383static struct {
1384 int active;
1385 /* r/w apic fields */
1386 unsigned int apic_id;
1387 unsigned int apic_taskpri;
1388 unsigned int apic_ldr;
1389 unsigned int apic_dfr;
1390 unsigned int apic_spiv;
1391 unsigned int apic_lvtt;
1392 unsigned int apic_lvtpc;
1393 unsigned int apic_lvt0;
1394 unsigned int apic_lvt1;
1395 unsigned int apic_lvterr;
1396 unsigned int apic_tmict;
1397 unsigned int apic_tdcr;
1398 unsigned int apic_thmr;
1399} apic_pm_state;
1400
1401static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1402{
1403 unsigned long flags;
1404 int maxlvt;
1405
1406 if (!apic_pm_state.active)
1407 return 0;
1408
1409 maxlvt = lapic_get_maxlvt();
1410
1411 apic_pm_state.apic_id = apic_read(APIC_ID);
1412 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1413 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1414 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1415 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1416 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1417 if (maxlvt >= 4)
1418 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1419 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1420 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1421 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1422 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1423 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1424#ifdef CONFIG_X86_MCE_P4THERMAL
1425 if (maxlvt >= 5)
1426 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1397#endif 1427#endif
1398 setup_boot_APIC_clock();
1399 1428
1429 local_irq_save(flags);
1430 disable_local_APIC();
1431 local_irq_restore(flags);
1400 return 0; 1432 return 0;
1401} 1433}
1402 1434
1403static int __init parse_lapic(char *arg) 1435static int lapic_resume(struct sys_device *dev)
1404{ 1436{
1405 lapic_enable(); 1437 unsigned int l, h;
1438 unsigned long flags;
1439 int maxlvt;
1440
1441 if (!apic_pm_state.active)
1442 return 0;
1443
1444 maxlvt = lapic_get_maxlvt();
1445
1446 local_irq_save(flags);
1447
1448 /*
1449 * Make sure the APICBASE points to the right address
1450 *
1451 * FIXME! This will be wrong if we ever support suspend on
1452 * SMP! We'll need to do this as part of the CPU restore!
1453 */
1454 rdmsr(MSR_IA32_APICBASE, l, h);
1455 l &= ~MSR_IA32_APICBASE_BASE;
1456 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1457 wrmsr(MSR_IA32_APICBASE, l, h);
1458
1459 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1460 apic_write(APIC_ID, apic_pm_state.apic_id);
1461 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1462 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1463 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1464 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1465 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1466 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1467#ifdef CONFIG_X86_MCE_P4THERMAL
1468 if (maxlvt >= 5)
1469 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1470#endif
1471 if (maxlvt >= 4)
1472 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1473 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1474 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1475 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1476 apic_write(APIC_ESR, 0);
1477 apic_read(APIC_ESR);
1478 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1479 apic_write(APIC_ESR, 0);
1480 apic_read(APIC_ESR);
1481 local_irq_restore(flags);
1406 return 0; 1482 return 0;
1407} 1483}
1408early_param("lapic", parse_lapic);
1409 1484
1410static int __init parse_nolapic(char *arg) 1485/*
1486 * This device has no shutdown method - fully functioning local APICs
1487 * are needed on every CPU up until machine_halt/restart/poweroff.
1488 */
1489
1490static struct sysdev_class lapic_sysclass = {
1491 set_kset_name("lapic"),
1492 .resume = lapic_resume,
1493 .suspend = lapic_suspend,
1494};
1495
1496static struct sys_device device_lapic = {
1497 .id = 0,
1498 .cls = &lapic_sysclass,
1499};
1500
1501static void __devinit apic_pm_activate(void)
1411{ 1502{
1412 lapic_disable(); 1503 apic_pm_state.active = 1;
1413 return 0;
1414} 1504}
1415early_param("nolapic", parse_nolapic);
1416 1505
1506static int __init init_lapic_sysfs(void)
1507{
1508 int error;
1509
1510 if (!cpu_has_apic)
1511 return 0;
1512 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1513
1514 error = sysdev_class_register(&lapic_sysclass);
1515 if (!error)
1516 error = sysdev_register(&device_lapic);
1517 return error;
1518}
1519device_initcall(init_lapic_sysfs);
1520
1521#else /* CONFIG_PM */
1522
1523static void apic_pm_activate(void) { }
1524
1525#endif /* CONFIG_PM */