blob: e98b5c750bdf6b89fd196657c5e8ecb75ed6bdab [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/bootmem.h>
22#include <linux/smp_lock.h>
23#include <linux/interrupt.h>
24#include <linux/mc146818rtc.h>
25#include <linux/kernel_stat.h>
26#include <linux/sysdev.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070027#include <linux/cpu.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080028#include <linux/clockchips.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010029#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <asm/atomic.h>
32#include <asm/smp.h>
33#include <asm/mtrr.h>
34#include <asm/mpspec.h>
35#include <asm/desc.h>
36#include <asm/arch_hooks.h>
37#include <asm/hpet.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070038#include <asm/i8253.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020039#include <asm/nmi.h>
Stephane Eranian2ff2d3d2007-02-13 13:26:22 +010040#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <mach_apic.h>
Jesper Juhl382dbd02006-03-23 02:59:49 -080043#include <mach_apicdef.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010044#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "io_ports.h"
47
48/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080049 * Sanity check
50 */
51#if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
52# error SPURIOUS_APIC_VECTOR definition error
53#endif
54
55/*
Eric W. Biederman9635b472005-06-25 14:57:41 -070056 * Knob to control our willingness to enable the local APIC.
Thomas Gleixnere05d7232007-02-16 01:27:58 -080057 *
58 * -1=force-disable, +1=force-enable
Eric W. Biederman9635b472005-06-25 14:57:41 -070059 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -080060static int enable_local_apic __initdata = 0;
Eric W. Biederman9635b472005-06-25 14:57:41 -070061
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080062/* Enable local APIC timer for highres/dyntick on UP */
63static int enable_local_apic_timer __initdata = 0;
64
Eric W. Biederman9635b472005-06-25 14:57:41 -070065/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080066 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68int apic_verbosity;
69
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080070static unsigned int calibration_result;
71
72static int lapic_next_event(unsigned long delta,
73 struct clock_event_device *evt);
74static void lapic_timer_setup(enum clock_event_mode mode,
75 struct clock_event_device *evt);
76static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void apic_pm_activate(void);
78
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080079/*
80 * The local apic timer can be used for any function which is CPU local.
81 */
82static struct clock_event_device lapic_clockevent = {
83 .name = "lapic",
84 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
85 | CLOCK_EVT_FEAT_C3STOP,
86 .shift = 32,
87 .set_mode = lapic_timer_setup,
88 .set_next_event = lapic_next_event,
89 .broadcast = lapic_timer_broadcast,
90 .rating = 100,
91 .irq = -1,
92};
93static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Gleixnere05d7232007-02-16 01:27:58 -080095/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int enabled_via_apicbase;
97
Thomas Gleixnere05d7232007-02-16 01:27:58 -080098/*
99 * Get the LAPIC version
100 */
101static inline int lapic_get_version(void)
102{
103 return GET_APIC_VERSION(apic_read(APIC_LVR));
104}
105
106/*
107 * Check, if the APIC is integrated or a seperate chip
108 */
109static inline int lapic_is_integrated(void)
110{
111 return APIC_INTEGRATED(lapic_get_version());
112}
113
114/*
115 * Check, whether this is a modern or a first generation APIC
116 */
117static int modern_apic(void)
118{
119 /* AMD systems use old APIC versions, so check the CPU */
120 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
121 boot_cpu_data.x86 >= 0xf)
122 return 1;
123 return lapic_get_version() >= 0x14;
124}
125
126/**
127 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129void enable_NMI_through_LVT0 (void * dummy)
130{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800131 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800133 /* Level triggered for 82489DX */
134 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 v |= APIC_LVT_LEVEL_TRIGGER;
136 apic_write_around(APIC_LVT0, v);
137}
138
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800139/**
140 * get_physical_broadcast - Get number of physical broadcast IDs
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142int get_physical_broadcast(void)
143{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800144 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800147/**
148 * lapic_get_maxlvt - get the maximum number of local vector table entries
149 */
150int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800152 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800155 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800159 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
162/*
163 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
164 * per second. We assume that the caller has already set up the local
165 * APIC.
166 *
167 * The APIC timer is not exactly sync with the external timer chip, it
168 * closely follows bus clocks.
169 */
170
171/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800172 * FIXME: Move this to i8253.h. There is no need to keep the access to
173 * the PIT scattered all around the place -tglx
174 */
175
176/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 * The timer chip is already set up at HZ interrupts per second here,
178 * but we do not accept timer interrupts yet. We only allow the BP
179 * to calibrate.
180 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700181static unsigned int __devinit get_8254_timer_count(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 unsigned long flags;
184
185 unsigned int count;
186
187 spin_lock_irqsave(&i8253_lock, flags);
188
189 outb_p(0x00, PIT_MODE);
190 count = inb_p(PIT_CH0);
191 count |= inb_p(PIT_CH0) << 8;
192
193 spin_unlock_irqrestore(&i8253_lock, flags);
194
195 return count;
196}
197
198/* next tick in 8254 can be caught by catching timer wraparound */
Li Shaohua0bb31842005-06-25 14:54:55 -0700199static void __devinit wait_8254_wraparound(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 unsigned int curr_count, prev_count;
202
203 curr_count = get_8254_timer_count();
204 do {
205 prev_count = curr_count;
206 curr_count = get_8254_timer_count();
207
208 /* workaround for broken Mercury/Neptune */
209 if (prev_count >= curr_count + 0x100)
210 curr_count = get_8254_timer_count();
211
212 } while (prev_count >= curr_count);
213}
214
215/*
216 * Default initialization for 8254 timers. If we use other timers like HPET,
217 * we override this later
218 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700219void (*wait_timer_tick)(void) __devinitdata = wait_8254_wraparound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221/*
222 * This function sets up the local APIC timer, with a timeout of
223 * 'clocks' APIC bus clock. During calibration we actually call
224 * this function twice on the boot CPU, once with a bogus timeout
225 * value, second time for real. The other (noncalibrating) CPUs
226 * call this function only once, with the real, calibrated value.
227 *
228 * We do reads before writes even if unnecessary, to get around the
229 * P5 APIC double write bug.
230 */
231
232#define APIC_DIVISOR 16
233
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800234static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800236 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800238 lvtt_value = LOCAL_TIMER_VECTOR;
239 if (!oneshot)
240 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800241 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100243
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800244 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100245 lvtt_value |= APIC_LVT_MASKED;
246
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 apic_write_around(APIC_LVTT, lvtt_value);
248
249 /*
250 * Divide PICLK by 16
251 */
252 tmp_value = apic_read(APIC_TDCR);
253 apic_write_around(APIC_TDCR, (tmp_value
254 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
255 | APIC_TDR_DIV_16);
256
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800257 if (!oneshot)
258 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800261/*
262 * Program the next event, relative to now
263 */
264static int lapic_next_event(unsigned long delta,
265 struct clock_event_device *evt)
266{
267 apic_write_around(APIC_TMICT, delta);
268 return 0;
269}
270
271/*
272 * Setup the lapic timer in periodic or oneshot mode
273 */
274static void lapic_timer_setup(enum clock_event_mode mode,
275 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800278 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 local_irq_save(flags);
281
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800282 switch (mode) {
283 case CLOCK_EVT_MODE_PERIODIC:
284 case CLOCK_EVT_MODE_ONESHOT:
285 __setup_APIC_LVTT(calibration_result,
286 mode != CLOCK_EVT_MODE_PERIODIC, 1);
287 break;
288 case CLOCK_EVT_MODE_UNUSED:
289 case CLOCK_EVT_MODE_SHUTDOWN:
290 v = apic_read(APIC_LVTT);
291 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
292 apic_write_around(APIC_LVTT, v);
293 break;
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296 local_irq_restore(flags);
297}
298
299/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800300 * Local APIC timer broadcast function
301 */
302static void lapic_timer_broadcast(cpumask_t mask)
303{
304#ifdef CONFIG_SMP
305 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
306#endif
307}
308
309/*
310 * Setup the local APIC timer for this CPU. Copy the initilized values
311 * of the boot CPU and register the clock event in the framework.
312 */
313static void __devinit setup_APIC_timer(void)
314{
315 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
316
317 memcpy(levt, &lapic_clockevent, sizeof(*levt));
318 levt->cpumask = cpumask_of_cpu(smp_processor_id());
319
320 clockevents_register_device(levt);
321}
322
323/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * In this function we calibrate APIC bus clocks to the external
325 * timer. Unfortunately we cannot use jiffies and the timer irq
326 * to calibrate, since some later bootup code depends on getting
327 * the first irq? Ugh.
328 *
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800329 * TODO: Fix this rather than saying "Ugh" -tglx
330 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * We want to do the calibration only once since we
332 * want to have local timer irqs syncron. CPUs connected
333 * by the same APIC bus have the very same bus frequency.
334 * And we want to have irqs off anyways, no accidental
335 * APIC irq that way.
336 */
337
338static int __init calibrate_APIC_clock(void)
339{
340 unsigned long long t1 = 0, t2 = 0;
341 long tt1, tt2;
342 long result;
343 int i;
344 const int LOOPS = HZ/10;
345
346 apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
347
348 /*
349 * Put whatever arbitrary (but long enough) timeout
350 * value into the APIC clock, we just want to get the
351 * counter running for calibration.
352 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800353 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /*
356 * The timer chip counts down to zero. Let's wait
357 * for a wraparound to start exact measurement:
358 * (the current tick might have been already half done)
359 */
360
361 wait_timer_tick();
362
363 /*
364 * We wrapped around just now. Let's start:
365 */
366 if (cpu_has_tsc)
367 rdtscll(t1);
368 tt1 = apic_read(APIC_TMCCT);
369
370 /*
371 * Let's wait LOOPS wraprounds:
372 */
373 for (i = 0; i < LOOPS; i++)
374 wait_timer_tick();
375
376 tt2 = apic_read(APIC_TMCCT);
377 if (cpu_has_tsc)
378 rdtscll(t2);
379
380 /*
381 * The APIC bus clock counter is 32 bits only, it
382 * might have overflown, but note that we use signed
383 * longs, thus no extra care needed.
384 *
385 * underflown to be exact, as the timer counts down ;)
386 */
387
388 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
389
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800390 /* Calculate the scaled math multiplication factor */
391 lapic_clockevent.mult = div_sc(tt1-tt2, TICK_NSEC * LOOPS, 32);
392 lapic_clockevent.max_delta_ns =
393 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
394 lapic_clockevent.min_delta_ns =
395 clockevent_delta2ns(0xF, &lapic_clockevent);
396
397 apic_printk(APIC_VERBOSE, "..... tt1-tt2 %ld\n", tt1 - tt2);
398 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
399 apic_printk(APIC_VERBOSE, "..... calibration result: %ld\n", result);
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (cpu_has_tsc)
402 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
403 "%ld.%04ld MHz.\n",
404 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
405 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
406
407 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
408 "%ld.%04ld MHz.\n",
409 result/(1000000/HZ),
410 result%(1000000/HZ));
411
412 return result;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415void __init setup_boot_APIC_clock(void)
416{
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -0800417 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -0800420 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
422 calibration_result = calibrate_APIC_clock();
423 /*
424 * Now set up the timer for real.
425 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800426 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -0800428 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Li Shaohua0bb31842005-06-25 14:54:55 -0700431void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800433 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800437 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800439static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800441 int cpu = smp_processor_id();
442 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800445 * Normally we should not be here till LAPIC has been
446 * initialized but in some cases like kdump, its possible that
447 * there is a pending LAPIC timer interrupt from previous
448 * kernel's context and is delivered in new kernel the moment
449 * interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800451 * Interrupts are enabled early and LAPIC is setup much later,
452 * hence its possible that when we get here evt->event_handler
453 * is NULL. Check for event_handler being NULL and discard
454 * the interrupt as spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800456 if (!evt->event_handler) {
457 printk(KERN_WARNING
458 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
459 /* Switch it off */
460 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
461 return;
462 }
463
464 per_cpu(irq_stat, cpu).apic_timer_irqs++;
465
466 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
469/*
470 * Local APIC timer interrupt. This is the most natural way for doing
471 * local interrupts, but local timer interrupts can be emulated by
472 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
473 *
474 * [ if a single-CPU system runs an SMP kernel then we call the local
475 * interrupt as well. Thus we cannot inline the local irq ... ]
476 */
477
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800478void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
David Howells7d12e782006-10-05 14:55:46 +0100480 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 /*
483 * NOTE! We'd better ACK the irq immediately,
484 * because timer handling can be slow.
485 */
486 ack_APIC_irq();
487 /*
488 * update_process_times() expects us to have done irq_enter().
489 * Besides, if we don't timer interrupts ignore the global
490 * interrupt lock, which is the WrongThing (tm) to do.
491 */
Stephane Eranian2ff2d3d2007-02-13 13:26:22 +0100492 exit_idle();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800494 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800496
David Howells7d12e782006-10-05 14:55:46 +0100497 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498}
499
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100500int setup_profiling_timer(unsigned int multiplier)
501{
502 return -EINVAL;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800506 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800508
509/**
510 * clear_local_APIC - shutdown the local APIC
511 *
512 * This is called, when a CPU is disabled and before rebooting, so the state of
513 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
514 * leftovers during boot.
515 */
516void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800518 int maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 unsigned long v;
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800522 * Masking an LVT entry can trigger a local APIC error
523 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800525 if (maxlvt >= 3) {
526 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
527 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
528 }
529 /*
530 * Careful: we have to set masks only first to deassert
531 * any level-triggered sources.
532 */
533 v = apic_read(APIC_LVTT);
534 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
535 v = apic_read(APIC_LVT0);
536 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
537 v = apic_read(APIC_LVT1);
538 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
539 if (maxlvt >= 4) {
540 v = apic_read(APIC_LVTPC);
541 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
542 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800544 /* lets not touch this if we didn't frob it */
545#ifdef CONFIG_X86_MCE_P4THERMAL
546 if (maxlvt >= 5) {
547 v = apic_read(APIC_LVTTHMR);
548 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
549 }
550#endif
551 /*
552 * Clean APIC state for other OSs:
553 */
554 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
555 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
556 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
557 if (maxlvt >= 3)
558 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
559 if (maxlvt >= 4)
560 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
561
562#ifdef CONFIG_X86_MCE_P4THERMAL
563 if (maxlvt >= 5)
564 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
565#endif
566 /* Integrated APIC (!82489DX) ? */
567 if (lapic_is_integrated()) {
568 if (maxlvt > 3)
569 /* Clear ESR due to Pentium errata 3AP and 11AP */
570 apic_write(APIC_ESR, 0);
571 apic_read(APIC_ESR);
572 }
573}
574
575/**
576 * disable_local_APIC - clear and disable the local APIC
577 */
578void disable_local_APIC(void)
579{
580 unsigned long value;
581
582 clear_local_APIC();
583
584 /*
585 * Disable APIC (implies clearing of registers
586 * for 82489DX!).
587 */
588 value = apic_read(APIC_SPIV);
589 value &= ~APIC_SPIV_APIC_ENABLED;
590 apic_write_around(APIC_SPIV, value);
591
592 /*
593 * When LAPIC was disabled by the BIOS and enabled by the kernel,
594 * restore the disabled state.
595 */
596 if (enabled_via_apicbase) {
597 unsigned int l, h;
598
599 rdmsr(MSR_IA32_APICBASE, l, h);
600 l &= ~MSR_IA32_APICBASE_ENABLE;
601 wrmsr(MSR_IA32_APICBASE, l, h);
602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
605/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800606 * If Linux enabled the LAPIC against the BIOS default disable it down before
607 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
608 * not power-off. Additionally clear all LVT entries before disable_local_APIC
609 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800611void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800613 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800615 if (!cpu_has_apic)
616 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800618 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/*
628 * This is to verify that we're looking at a real local APIC.
629 * Check these against your board if the CPUs aren't getting
630 * started for no apparent reason.
631 */
632int __init verify_local_APIC(void)
633{
634 unsigned int reg0, reg1;
635
636 /*
637 * The version register is read-only in a real APIC.
638 */
639 reg0 = apic_read(APIC_LVR);
640 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
641 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
642 reg1 = apic_read(APIC_LVR);
643 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
644
645 /*
646 * The two version reads above should print the same
647 * numbers. If the second one is different, then we
648 * poke at a non-APIC.
649 */
650 if (reg1 != reg0)
651 return 0;
652
653 /*
654 * Check if the version looks reasonably.
655 */
656 reg1 = GET_APIC_VERSION(reg0);
657 if (reg1 == 0x00 || reg1 == 0xff)
658 return 0;
659 reg1 = lapic_get_maxlvt();
660 if (reg1 < 0x02 || reg1 == 0xff)
661 return 0;
662
663 /*
664 * The ID register is read/write in a real APIC.
665 */
666 reg0 = apic_read(APIC_ID);
667 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
668
669 /*
670 * The next two are just to see if we have sane values.
671 * They're only really relevant if we're in Virtual Wire
672 * compatibility mode, but most boxes are anymore.
673 */
674 reg0 = apic_read(APIC_LVT0);
675 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
676 reg1 = apic_read(APIC_LVT1);
677 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
678
679 return 1;
680}
681
682/**
683 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
684 */
685void __init sync_Arb_IDs(void)
686{
687 /*
688 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
689 * needed on AMD.
690 */
691 if (modern_apic())
692 return;
693 /*
694 * Wait for idle.
695 */
696 apic_wait_icr_idle();
697
698 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
699 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
700 | APIC_DM_INIT);
701}
702
703/*
704 * An initial setup of the virtual wire mode.
705 */
706void __init init_bsp_APIC(void)
707{
708 unsigned long value;
709
710 /*
711 * Don't do the setup now if we have a SMP BIOS as the
712 * through-I/O-APIC virtual wire mode might be active.
713 */
714 if (smp_found_config || !cpu_has_apic)
715 return;
716
717 /*
718 * Do not trust the local APIC being empty at bootup.
719 */
720 clear_local_APIC();
721
722 /*
723 * Enable APIC.
724 */
725 value = apic_read(APIC_SPIV);
726 value &= ~APIC_VECTOR_MASK;
727 value |= APIC_SPIV_APIC_ENABLED;
728
729 /* This bit is reserved on P4/Xeon and should be cleared */
730 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
731 (boot_cpu_data.x86 == 15))
732 value &= ~APIC_SPIV_FOCUS_DISABLED;
733 else
734 value |= APIC_SPIV_FOCUS_DISABLED;
735 value |= SPURIOUS_APIC_VECTOR;
736 apic_write_around(APIC_SPIV, value);
737
738 /*
739 * Set up the virtual wire mode.
740 */
741 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
742 value = APIC_DM_NMI;
743 if (!lapic_is_integrated()) /* 82489DX */
744 value |= APIC_LVT_LEVEL_TRIGGER;
745 apic_write_around(APIC_LVT1, value);
746}
747
748/**
749 * setup_local_APIC - setup the local APIC
750 */
751void __devinit setup_local_APIC(void)
752{
753 unsigned long oldvalue, value, maxlvt, integrated;
754 int i, j;
755
756 /* Pound the ESR really hard over the head with a big hammer - mbligh */
757 if (esr_disable) {
758 apic_write(APIC_ESR, 0);
759 apic_write(APIC_ESR, 0);
760 apic_write(APIC_ESR, 0);
761 apic_write(APIC_ESR, 0);
762 }
763
764 integrated = lapic_is_integrated();
765
766 /*
767 * Double-check whether this APIC is really registered.
768 */
769 if (!apic_id_registered())
770 BUG();
771
772 /*
773 * Intel recommends to set DFR, LDR and TPR before enabling
774 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
775 * document number 292116). So here it goes...
776 */
777 init_apic_ldr();
778
779 /*
780 * Set Task Priority to 'accept all'. We never change this
781 * later on.
782 */
783 value = apic_read(APIC_TASKPRI);
784 value &= ~APIC_TPRI_MASK;
785 apic_write_around(APIC_TASKPRI, value);
786
787 /*
788 * After a crash, we no longer service the interrupts and a pending
789 * interrupt from previous kernel might still have ISR bit set.
790 *
791 * Most probably by now CPU has serviced that pending interrupt and
792 * it might not have done the ack_APIC_irq() because it thought,
793 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
794 * does not clear the ISR bit and cpu thinks it has already serivced
795 * the interrupt. Hence a vector might get locked. It was noticed
796 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
797 */
798 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
799 value = apic_read(APIC_ISR + i*0x10);
800 for (j = 31; j >= 0; j--) {
801 if (value & (1<<j))
802 ack_APIC_irq();
803 }
804 }
805
806 /*
807 * Now that we are all set up, enable the APIC
808 */
809 value = apic_read(APIC_SPIV);
810 value &= ~APIC_VECTOR_MASK;
811 /*
812 * Enable APIC
813 */
814 value |= APIC_SPIV_APIC_ENABLED;
815
816 /*
817 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
818 * certain networking cards. If high frequency interrupts are
819 * happening on a particular IOAPIC pin, plus the IOAPIC routing
820 * entry is masked/unmasked at a high rate as well then sooner or
821 * later IOAPIC line gets 'stuck', no more interrupts are received
822 * from the device. If focus CPU is disabled then the hang goes
823 * away, oh well :-(
824 *
825 * [ This bug can be reproduced easily with a level-triggered
826 * PCI Ne2000 networking cards and PII/PIII processors, dual
827 * BX chipset. ]
828 */
829 /*
830 * Actually disabling the focus CPU check just makes the hang less
831 * frequent as it makes the interrupt distributon model be more
832 * like LRU than MRU (the short-term load is more even across CPUs).
833 * See also the comment in end_level_ioapic_irq(). --macro
834 */
835
836 /* Enable focus processor (bit==0) */
837 value &= ~APIC_SPIV_FOCUS_DISABLED;
838
839 /*
840 * Set spurious IRQ vector
841 */
842 value |= SPURIOUS_APIC_VECTOR;
843 apic_write_around(APIC_SPIV, value);
844
845 /*
846 * Set up LVT0, LVT1:
847 *
848 * set up through-local-APIC on the BP's LINT0. This is not
849 * strictly necessery in pure symmetric-IO mode, but sometimes
850 * we delegate interrupts to the 8259A.
851 */
852 /*
853 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
854 */
855 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
856 if (!smp_processor_id() && (pic_mode || !value)) {
857 value = APIC_DM_EXTINT;
858 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
859 smp_processor_id());
860 } else {
861 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
862 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
863 smp_processor_id());
864 }
865 apic_write_around(APIC_LVT0, value);
866
867 /*
868 * only the BP should see the LINT1 NMI signal, obviously.
869 */
870 if (!smp_processor_id())
871 value = APIC_DM_NMI;
872 else
873 value = APIC_DM_NMI | APIC_LVT_MASKED;
874 if (!integrated) /* 82489DX */
875 value |= APIC_LVT_LEVEL_TRIGGER;
876 apic_write_around(APIC_LVT1, value);
877
878 if (integrated && !esr_disable) { /* !82489DX */
879 maxlvt = lapic_get_maxlvt();
880 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
881 apic_write(APIC_ESR, 0);
882 oldvalue = apic_read(APIC_ESR);
883
884 /* enables sending errors */
885 value = ERROR_APIC_VECTOR;
886 apic_write_around(APIC_LVTERR, value);
887 /*
888 * spec says clear errors after enabling vector.
889 */
890 if (maxlvt > 3)
891 apic_write(APIC_ESR, 0);
892 value = apic_read(APIC_ESR);
893 if (value != oldvalue)
894 apic_printk(APIC_VERBOSE, "ESR value before enabling "
895 "vector: 0x%08lx after: 0x%08lx\n",
896 oldvalue, value);
897 } else {
898 if (esr_disable)
899 /*
900 * Something untraceble is creating bad interrupts on
901 * secondary quads ... for the moment, just leave the
902 * ESR disabled - we can't do anything useful with the
903 * errors anyway - mbligh
904 */
905 printk(KERN_INFO "Leaving ESR disabled.\n");
906 else
907 printk(KERN_INFO "No ESR for 82489DX.\n");
908 }
909
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800910 /* Disable the local apic timer */
911 value = apic_read(APIC_LVTT);
912 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
913 apic_write_around(APIC_LVTT, value);
914
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800915 setup_apic_nmi_watchdog(NULL);
916 apic_pm_activate();
917}
918
919/*
920 * Detect and initialize APIC
921 */
922static int __init detect_init_APIC (void)
923{
924 u32 h, l, features;
925
926 /* Disabled by kernel option? */
927 if (enable_local_apic < 0)
928 return -1;
929
930 switch (boot_cpu_data.x86_vendor) {
931 case X86_VENDOR_AMD:
932 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
933 (boot_cpu_data.x86 == 15))
934 break;
935 goto no_apic;
936 case X86_VENDOR_INTEL:
937 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
938 (boot_cpu_data.x86 == 5 && cpu_has_apic))
939 break;
940 goto no_apic;
941 default:
942 goto no_apic;
943 }
944
945 if (!cpu_has_apic) {
946 /*
947 * Over-ride BIOS and try to enable the local APIC only if
948 * "lapic" specified.
949 */
950 if (enable_local_apic <= 0) {
951 printk(KERN_INFO "Local APIC disabled by BIOS -- "
952 "you can enable it with \"lapic\"\n");
953 return -1;
954 }
955 /*
956 * Some BIOSes disable the local APIC in the APIC_BASE
957 * MSR. This can only be done in software for Intel P6 or later
958 * and AMD K7 (Model > 1) or later.
959 */
960 rdmsr(MSR_IA32_APICBASE, l, h);
961 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
962 printk(KERN_INFO
963 "Local APIC disabled by BIOS -- reenabling.\n");
964 l &= ~MSR_IA32_APICBASE_BASE;
965 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
966 wrmsr(MSR_IA32_APICBASE, l, h);
967 enabled_via_apicbase = 1;
968 }
969 }
970 /*
971 * The APIC feature bit should now be enabled
972 * in `cpuid'
973 */
974 features = cpuid_edx(1);
975 if (!(features & (1 << X86_FEATURE_APIC))) {
976 printk(KERN_WARNING "Could not enable APIC!\n");
977 return -1;
978 }
979 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
980 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
981
982 /* The BIOS may have set up the APIC at some other address */
983 rdmsr(MSR_IA32_APICBASE, l, h);
984 if (l & MSR_IA32_APICBASE_ENABLE)
985 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
986
987 if (nmi_watchdog != NMI_NONE)
988 nmi_watchdog = NMI_LOCAL_APIC;
989
990 printk(KERN_INFO "Found and enabled local APIC!\n");
991
992 apic_pm_activate();
993
994 return 0;
995
996no_apic:
997 printk(KERN_INFO "No local APIC present or hardware disabled\n");
998 return -1;
999}
1000
1001/**
1002 * init_apic_mappings - initialize APIC mappings
1003 */
1004void __init init_apic_mappings(void)
1005{
1006 unsigned long apic_phys;
1007
1008 /*
1009 * If no local APIC can be found then set up a fake all
1010 * zeroes page to simulate the local APIC and another
1011 * one for the IO-APIC.
1012 */
1013 if (!smp_found_config && detect_init_APIC()) {
1014 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1015 apic_phys = __pa(apic_phys);
1016 } else
1017 apic_phys = mp_lapic_addr;
1018
1019 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1020 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1021 apic_phys);
1022
1023 /*
1024 * Fetch the APIC ID of the BSP in case we have a
1025 * default configuration (or the MP table is broken).
1026 */
1027 if (boot_cpu_physical_apicid == -1U)
1028 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1029
1030#ifdef CONFIG_X86_IO_APIC
1031 {
1032 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1033 int i;
1034
1035 for (i = 0; i < nr_ioapics; i++) {
1036 if (smp_found_config) {
1037 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1038 if (!ioapic_phys) {
1039 printk(KERN_ERR
1040 "WARNING: bogus zero IO-APIC "
1041 "address found in MPTABLE, "
1042 "disabling IO/APIC support!\n");
1043 smp_found_config = 0;
1044 skip_ioapic_setup = 1;
1045 goto fake_ioapic_page;
1046 }
1047 } else {
1048fake_ioapic_page:
1049 ioapic_phys = (unsigned long)
1050 alloc_bootmem_pages(PAGE_SIZE);
1051 ioapic_phys = __pa(ioapic_phys);
1052 }
1053 set_fixmap_nocache(idx, ioapic_phys);
1054 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1055 __fix_to_virt(idx), ioapic_phys);
1056 idx++;
1057 }
1058 }
1059#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
1062/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001063 * This initializes the IO-APIC and APIC hardware if this is
1064 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 */
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001066int __init APIC_init_uniprocessor (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001068 if (enable_local_apic < 0)
1069 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001070
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001071 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001072 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001075 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001077 if (!cpu_has_apic &&
1078 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001080 boot_cpu_physical_apicid);
Andi Kleen3777a952006-02-03 21:51:53 +01001081 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return -1;
1083 }
1084
1085 verify_local_APIC();
1086
1087 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001088
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001089 /*
1090 * Hack: In case of kdump, after a crash, kernel might be booting
1091 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1092 * might be zero if read from MP tables. Get it from LAPIC.
1093 */
1094#ifdef CONFIG_CRASH_DUMP
1095 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1096#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001097 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 setup_local_APIC();
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001102 if (smp_found_config)
1103 if (!skip_ioapic_setup && nr_ioapics)
1104 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001106 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001107
1108 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001110
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001111/*
1112 * APIC command line parameters
1113 */
Rusty Russell1a3f2392006-09-26 10:52:32 +02001114static int __init parse_lapic(char *arg)
1115{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001116 enable_local_apic = 1;
Rusty Russell1a3f2392006-09-26 10:52:32 +02001117 return 0;
1118}
1119early_param("lapic", parse_lapic);
1120
1121static int __init parse_nolapic(char *arg)
1122{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001123 enable_local_apic = -1;
1124 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Rusty Russell1a3f2392006-09-26 10:52:32 +02001125 return 0;
1126}
1127early_param("nolapic", parse_nolapic);
1128
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001129static int __init apic_enable_lapic_timer(char *str)
1130{
1131 enable_local_apic_timer = 1;
1132 return 0;
1133}
1134early_param("lapictimer", apic_enable_lapic_timer);
1135
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001136static int __init apic_set_verbosity(char *str)
1137{
1138 if (strcmp("debug", str) == 0)
1139 apic_verbosity = APIC_DEBUG;
1140 else if (strcmp("verbose", str) == 0)
1141 apic_verbosity = APIC_VERBOSE;
1142 return 1;
1143}
1144
1145__setup("apic=", apic_set_verbosity);
1146
1147
1148/*
1149 * Local APIC interrupts
1150 */
1151
1152/*
1153 * This interrupt should _never_ happen with our APIC/SMP architecture
1154 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001155void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001156{
1157 unsigned long v;
1158
1159 exit_idle();
1160 irq_enter();
1161 /*
1162 * Check if this really is a spurious interrupt and ACK it
1163 * if it is a vectored one. Just in case...
1164 * Spurious interrupts should not be ACKed.
1165 */
1166 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1167 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1168 ack_APIC_irq();
1169
1170 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1171 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1172 "should never happen.\n", smp_processor_id());
1173 irq_exit();
1174}
1175
1176/*
1177 * This interrupt should never happen with our APIC/SMP architecture
1178 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001179void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001180{
1181 unsigned long v, v1;
1182
1183 exit_idle();
1184 irq_enter();
1185 /* First tickle the hardware, only then report what went on. -- REW */
1186 v = apic_read(APIC_ESR);
1187 apic_write(APIC_ESR, 0);
1188 v1 = apic_read(APIC_ESR);
1189 ack_APIC_irq();
1190 atomic_inc(&irq_err_count);
1191
1192 /* Here is what the APIC error bits mean:
1193 0: Send CS error
1194 1: Receive CS error
1195 2: Send accept error
1196 3: Receive accept error
1197 4: Reserved
1198 5: Send illegal vector
1199 6: Received illegal vector
1200 7: Illegal register address
1201 */
1202 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1203 smp_processor_id(), v , v1);
1204 irq_exit();
1205}
1206
1207/*
1208 * Initialize APIC interrupts
1209 */
1210void __init apic_intr_init(void)
1211{
1212#ifdef CONFIG_SMP
1213 smp_intr_init();
1214#endif
1215 /* self generated IPI for local APIC timer */
1216 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1217
1218 /* IPI vectors for APIC spurious and error interrupts */
1219 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1220 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1221
1222 /* thermal monitor LVT interrupt */
1223#ifdef CONFIG_X86_MCE_P4THERMAL
1224 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1225#endif
1226}
1227
1228/**
1229 * connect_bsp_APIC - attach the APIC to the interrupt system
1230 */
1231void __init connect_bsp_APIC(void)
1232{
1233 if (pic_mode) {
1234 /*
1235 * Do not trust the local APIC being empty at bootup.
1236 */
1237 clear_local_APIC();
1238 /*
1239 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1240 * local APIC to INT and NMI lines.
1241 */
1242 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1243 "enabling APIC mode.\n");
1244 outb(0x70, 0x22);
1245 outb(0x01, 0x23);
1246 }
1247 enable_apic_mode();
1248}
1249
1250/**
1251 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1252 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1253 *
1254 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1255 * APIC is disabled.
1256 */
1257void disconnect_bsp_APIC(int virt_wire_setup)
1258{
1259 if (pic_mode) {
1260 /*
1261 * Put the board back into PIC mode (has an effect only on
1262 * certain older boards). Note that APIC interrupts, including
1263 * IPIs, won't work beyond this point! The only exception are
1264 * INIT IPIs.
1265 */
1266 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1267 "entering PIC mode.\n");
1268 outb(0x70, 0x22);
1269 outb(0x00, 0x23);
1270 } else {
1271 /* Go back to Virtual Wire compatibility mode */
1272 unsigned long value;
1273
1274 /* For the spurious interrupt use vector F, and enable it */
1275 value = apic_read(APIC_SPIV);
1276 value &= ~APIC_VECTOR_MASK;
1277 value |= APIC_SPIV_APIC_ENABLED;
1278 value |= 0xf;
1279 apic_write_around(APIC_SPIV, value);
1280
1281 if (!virt_wire_setup) {
1282 /*
1283 * For LVT0 make it edge triggered, active high,
1284 * external and enabled
1285 */
1286 value = apic_read(APIC_LVT0);
1287 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1288 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1289 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1290 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1291 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1292 apic_write_around(APIC_LVT0, value);
1293 } else {
1294 /* Disable LVT0 */
1295 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1296 }
1297
1298 /*
1299 * For LVT1 make it edge triggered, active high, nmi and
1300 * enabled
1301 */
1302 value = apic_read(APIC_LVT1);
1303 value &= ~(
1304 APIC_MODE_MASK | APIC_SEND_PENDING |
1305 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1306 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1307 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1308 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1309 apic_write_around(APIC_LVT1, value);
1310 }
1311}
1312
1313/*
1314 * Power management
1315 */
1316#ifdef CONFIG_PM
1317
1318static struct {
1319 int active;
1320 /* r/w apic fields */
1321 unsigned int apic_id;
1322 unsigned int apic_taskpri;
1323 unsigned int apic_ldr;
1324 unsigned int apic_dfr;
1325 unsigned int apic_spiv;
1326 unsigned int apic_lvtt;
1327 unsigned int apic_lvtpc;
1328 unsigned int apic_lvt0;
1329 unsigned int apic_lvt1;
1330 unsigned int apic_lvterr;
1331 unsigned int apic_tmict;
1332 unsigned int apic_tdcr;
1333 unsigned int apic_thmr;
1334} apic_pm_state;
1335
1336static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1337{
1338 unsigned long flags;
1339 int maxlvt;
1340
1341 if (!apic_pm_state.active)
1342 return 0;
1343
1344 maxlvt = lapic_get_maxlvt();
1345
1346 apic_pm_state.apic_id = apic_read(APIC_ID);
1347 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1348 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1349 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1350 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1351 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1352 if (maxlvt >= 4)
1353 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1354 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1355 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1356 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1357 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1358 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1359#ifdef CONFIG_X86_MCE_P4THERMAL
1360 if (maxlvt >= 5)
1361 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1362#endif
1363
1364 local_irq_save(flags);
1365 disable_local_APIC();
1366 local_irq_restore(flags);
1367 return 0;
1368}
1369
1370static int lapic_resume(struct sys_device *dev)
1371{
1372 unsigned int l, h;
1373 unsigned long flags;
1374 int maxlvt;
1375
1376 if (!apic_pm_state.active)
1377 return 0;
1378
1379 maxlvt = lapic_get_maxlvt();
1380
1381 local_irq_save(flags);
1382
1383 /*
1384 * Make sure the APICBASE points to the right address
1385 *
1386 * FIXME! This will be wrong if we ever support suspend on
1387 * SMP! We'll need to do this as part of the CPU restore!
1388 */
1389 rdmsr(MSR_IA32_APICBASE, l, h);
1390 l &= ~MSR_IA32_APICBASE_BASE;
1391 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1392 wrmsr(MSR_IA32_APICBASE, l, h);
1393
1394 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1395 apic_write(APIC_ID, apic_pm_state.apic_id);
1396 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1397 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1398 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1399 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1400 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1401 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1402#ifdef CONFIG_X86_MCE_P4THERMAL
1403 if (maxlvt >= 5)
1404 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1405#endif
1406 if (maxlvt >= 4)
1407 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1408 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1409 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1410 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1411 apic_write(APIC_ESR, 0);
1412 apic_read(APIC_ESR);
1413 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1414 apic_write(APIC_ESR, 0);
1415 apic_read(APIC_ESR);
1416 local_irq_restore(flags);
1417 return 0;
1418}
1419
1420/*
1421 * This device has no shutdown method - fully functioning local APICs
1422 * are needed on every CPU up until machine_halt/restart/poweroff.
1423 */
1424
1425static struct sysdev_class lapic_sysclass = {
1426 set_kset_name("lapic"),
1427 .resume = lapic_resume,
1428 .suspend = lapic_suspend,
1429};
1430
1431static struct sys_device device_lapic = {
1432 .id = 0,
1433 .cls = &lapic_sysclass,
1434};
1435
1436static void __devinit apic_pm_activate(void)
1437{
1438 apic_pm_state.active = 1;
1439}
1440
1441static int __init init_lapic_sysfs(void)
1442{
1443 int error;
1444
1445 if (!cpu_has_apic)
1446 return 0;
1447 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1448
1449 error = sysdev_class_register(&lapic_sysclass);
1450 if (!error)
1451 error = sysdev_register(&device_lapic);
1452 return error;
1453}
1454device_initcall(init_lapic_sysfs);
1455
1456#else /* CONFIG_PM */
1457
1458static void apic_pm_activate(void) { }
1459
1460#endif /* CONFIG_PM */