blob: e88415282a6f0ebf3024c2a5b1165e95502686e2 [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>
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080029#include <linux/acpi_pmtmr.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010030#include <linux/module.h>
Thomas Gleixnerad62ca22007-03-22 00:11:21 -080031#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/atomic.h>
34#include <asm/smp.h>
35#include <asm/mtrr.h>
36#include <asm/mpspec.h>
37#include <asm/desc.h>
38#include <asm/arch_hooks.h>
39#include <asm/hpet.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070040#include <asm/i8253.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020041#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <mach_apic.h>
Jesper Juhl382dbd02006-03-23 02:59:49 -080044#include <mach_apicdef.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010045#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include "io_ports.h"
48
49/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080050 * Sanity check
51 */
52#if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
53# error SPURIOUS_APIC_VECTOR definition error
54#endif
55
56/*
Eric W. Biederman9635b472005-06-25 14:57:41 -070057 * Knob to control our willingness to enable the local APIC.
Thomas Gleixnere05d7232007-02-16 01:27:58 -080058 *
59 * -1=force-disable, +1=force-enable
Eric W. Biederman9635b472005-06-25 14:57:41 -070060 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -080061static int enable_local_apic __initdata = 0;
Eric W. Biederman9635b472005-06-25 14:57:41 -070062
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080063/* Local APIC timer verification ok */
64static int local_apic_timer_verify_ok;
Thomas Gleixnerad62ca22007-03-22 00:11:21 -080065/* Disable local APIC timer from the kernel commandline or via dmi quirk */
66static int local_apic_timer_disabled;
Thomas Gleixnere585bef2007-03-23 16:08:01 +010067/* Local APIC timer works in C2 */
68int local_apic_timer_c2_ok;
69EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080070
Eric W. Biederman9635b472005-06-25 14:57:41 -070071/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080072 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74int apic_verbosity;
75
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080076static unsigned int calibration_result;
77
78static int lapic_next_event(unsigned long delta,
79 struct clock_event_device *evt);
80static void lapic_timer_setup(enum clock_event_mode mode,
81 struct clock_event_device *evt);
82static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static void apic_pm_activate(void);
84
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080085/*
86 * The local apic timer can be used for any function which is CPU local.
87 */
88static struct clock_event_device lapic_clockevent = {
89 .name = "lapic",
90 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080091 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080092 .shift = 32,
93 .set_mode = lapic_timer_setup,
94 .set_next_event = lapic_next_event,
95 .broadcast = lapic_timer_broadcast,
96 .rating = 100,
97 .irq = -1,
98};
99static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800101/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int enabled_via_apicbase;
103
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800104/*
105 * Get the LAPIC version
106 */
107static inline int lapic_get_version(void)
108{
109 return GET_APIC_VERSION(apic_read(APIC_LVR));
110}
111
112/*
113 * Check, if the APIC is integrated or a seperate chip
114 */
115static inline int lapic_is_integrated(void)
116{
117 return APIC_INTEGRATED(lapic_get_version());
118}
119
120/*
121 * Check, whether this is a modern or a first generation APIC
122 */
123static int modern_apic(void)
124{
125 /* AMD systems use old APIC versions, so check the CPU */
126 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
127 boot_cpu_data.x86 >= 0xf)
128 return 1;
129 return lapic_get_version() >= 0x14;
130}
131
132/**
133 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135void enable_NMI_through_LVT0 (void * dummy)
136{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800137 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800139 /* Level triggered for 82489DX */
140 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 v |= APIC_LVT_LEVEL_TRIGGER;
142 apic_write_around(APIC_LVT0, v);
143}
144
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800145/**
146 * get_physical_broadcast - Get number of physical broadcast IDs
147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148int get_physical_broadcast(void)
149{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800150 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800153/**
154 * lapic_get_maxlvt - get the maximum number of local vector table entries
155 */
156int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800158 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800161 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162}
163
164/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800165 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800168/* Clock divisor is set to 16 */
169#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/*
172 * This function sets up the local APIC timer, with a timeout of
173 * 'clocks' APIC bus clock. During calibration we actually call
174 * this function twice on the boot CPU, once with a bogus timeout
175 * value, second time for real. The other (noncalibrating) CPUs
176 * call this function only once, with the real, calibrated value.
177 *
178 * We do reads before writes even if unnecessary, to get around the
179 * P5 APIC double write bug.
180 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800181static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800183 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800185 lvtt_value = LOCAL_TIMER_VECTOR;
186 if (!oneshot)
187 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800188 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100190
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800191 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100192 lvtt_value |= APIC_LVT_MASKED;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 apic_write_around(APIC_LVTT, lvtt_value);
195
196 /*
197 * Divide PICLK by 16
198 */
199 tmp_value = apic_read(APIC_TDCR);
200 apic_write_around(APIC_TDCR, (tmp_value
201 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
202 | APIC_TDR_DIV_16);
203
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800204 if (!oneshot)
205 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800208/*
209 * Program the next event, relative to now
210 */
211static int lapic_next_event(unsigned long delta,
212 struct clock_event_device *evt)
213{
214 apic_write_around(APIC_TMICT, delta);
215 return 0;
216}
217
218/*
219 * Setup the lapic timer in periodic or oneshot mode
220 */
221static void lapic_timer_setup(enum clock_event_mode mode,
222 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
224 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800225 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800227 /* Lapic used for broadcast ? */
228 if (!local_apic_timer_verify_ok)
229 return;
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 local_irq_save(flags);
232
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800233 switch (mode) {
234 case CLOCK_EVT_MODE_PERIODIC:
235 case CLOCK_EVT_MODE_ONESHOT:
236 __setup_APIC_LVTT(calibration_result,
237 mode != CLOCK_EVT_MODE_PERIODIC, 1);
238 break;
239 case CLOCK_EVT_MODE_UNUSED:
240 case CLOCK_EVT_MODE_SHUTDOWN:
241 v = apic_read(APIC_LVTT);
242 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
243 apic_write_around(APIC_LVTT, v);
244 break;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 local_irq_restore(flags);
248}
249
250/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800251 * Local APIC timer broadcast function
252 */
253static void lapic_timer_broadcast(cpumask_t mask)
254{
255#ifdef CONFIG_SMP
256 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
257#endif
258}
259
260/*
261 * Setup the local APIC timer for this CPU. Copy the initilized values
262 * of the boot CPU and register the clock event in the framework.
263 */
264static void __devinit setup_APIC_timer(void)
265{
266 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
267
268 memcpy(levt, &lapic_clockevent, sizeof(*levt));
269 levt->cpumask = cpumask_of_cpu(smp_processor_id());
270
271 clockevents_register_device(levt);
272}
273
274/*
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800275 * Detect systems with known broken BIOS implementations
276 */
277static int __init lapic_check_broken_bios(struct dmi_system_id *d)
278{
279 printk(KERN_NOTICE "%s detected: disabling lapic timer.\n",
280 d->ident);
281 local_apic_timer_disabled = 1;
282 return 0;
283}
284
285static struct dmi_system_id __initdata broken_bios_dmi_table[] = {
286 {
287 /*
288 * BIOS exports only C1 state, but uses deeper power
289 * modes behind the kernels back.
290 */
291 .callback = lapic_check_broken_bios,
292 .ident = "HP nx6325",
293 .matches = {
294 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6325"),
295 },
296 },
297 {}
298};
299
300/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800301 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800303 * We want to do the calibration only once since we want to have local timer
304 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
305 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800306 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800307 * This was previously done by reading the PIT/HPET and waiting for a wrap
308 * around to find out, that a tick has elapsed. I have a box, where the PIT
309 * readout is broken, so it never gets out of the wait loop again. This was
310 * also reported by others.
311 *
312 * Monitoring the jiffies value is inaccurate and the clockevents
313 * infrastructure allows us to do a simple substitution of the interrupt
314 * handler.
315 *
316 * The calibration routine also uses the pm_timer when possible, as the PIT
317 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
318 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
320
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800321#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800323static __initdata volatile int lapic_cal_loops = -1;
324static __initdata long lapic_cal_t1, lapic_cal_t2;
325static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
326static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
327static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
328
329/*
330 * Temporary interrupt handler.
331 */
332static void __init lapic_cal_handler(struct clock_event_device *dev)
333{
334 unsigned long long tsc = 0;
335 long tapic = apic_read(APIC_TMCCT);
336 unsigned long pm = acpi_pm_read_early();
337
338 if (cpu_has_tsc)
339 rdtscll(tsc);
340
341 switch (lapic_cal_loops++) {
342 case 0:
343 lapic_cal_t1 = tapic;
344 lapic_cal_tsc1 = tsc;
345 lapic_cal_pm1 = pm;
346 lapic_cal_j1 = jiffies;
347 break;
348
349 case LAPIC_CAL_LOOPS:
350 lapic_cal_t2 = tapic;
351 lapic_cal_tsc2 = tsc;
352 if (pm < lapic_cal_pm1)
353 pm += ACPI_PM_OVRRUN;
354 lapic_cal_pm2 = pm;
355 lapic_cal_j2 = jiffies;
356 break;
357 }
358}
359
360/*
361 * Setup the boot APIC
362 *
363 * Calibrate and verify the result.
364 */
365void __init setup_boot_APIC_clock(void)
366{
367 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
368 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
369 const long pm_thresh = pm_100ms/100;
370 void (*real_handler)(struct clock_event_device *dev);
371 unsigned long deltaj;
372 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800373 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800374
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800375 /* Detect know broken systems */
376 dmi_check_system(broken_bios_dmi_table);
377
378 /*
379 * The local apic timer can be disabled via the kernel
380 * commandline or from the dmi quirk above. Register the lapic
381 * timer as a dummy clock event source on SMP systems, so the
382 * broadcast mechanism is used. On UP systems simply ignore it.
383 */
384 if (local_apic_timer_disabled) {
385 /* No broadcast on UP ! */
386 if (num_possible_cpus() > 1)
387 setup_APIC_timer();
388 return;
389 }
390
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800391 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
392 "calibrating APIC timer ...\n");
393
394 local_irq_disable();
395
396 /* Replace the global interrupt handler */
397 real_handler = global_clock_event->event_handler;
398 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800401 * Setup the APIC counter to 1e9. There is no way the lapic
402 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800404 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800406 /* Let the interrupts run */
407 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800409 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
410 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800412 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800414 /* Restore the real event handler */
415 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800417 /* Build delta t1-t2 as apic timer counts down */
418 delta = lapic_cal_t1 - lapic_cal_t2;
419 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800421 /* Check, if the PM timer is available */
422 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
423 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800425 if (deltapm) {
426 unsigned long mult;
427 u64 res;
428
429 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
430
431 if (deltapm > (pm_100ms - pm_thresh) &&
432 deltapm < (pm_100ms + pm_thresh)) {
433 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
434 } else {
435 res = (((u64) deltapm) * mult) >> 22;
436 do_div(res, 1000000);
437 printk(KERN_WARNING "APIC calibration not consistent "
438 "with PM Timer: %ldms instead of 100ms\n",
439 (long)res);
440 /* Correct the lapic counter value */
441 res = (((u64) delta ) * pm_100ms);
442 do_div(res, deltapm);
443 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
444 "%lu (%ld)\n", (unsigned long) res, delta);
445 delta = (long) res;
446 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800447 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800450 /* Calculate the scaled math multiplication factor */
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800451 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800452 lapic_clockevent.max_delta_ns =
453 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
454 lapic_clockevent.min_delta_ns =
455 clockevent_delta2ns(0xF, &lapic_clockevent);
456
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800457 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800458
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800459 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
460 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
461 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
462 calibration_result);
463
464 if (cpu_has_tsc) {
465 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800467 "%ld.%04ld MHz.\n",
468 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
469 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800473 "%u.%04u MHz.\n",
474 calibration_result / (1000000 / HZ),
475 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800477 local_apic_timer_verify_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800479 /* We trust the pm timer based calibration */
480 if (!pm_referenced) {
481 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800482
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800483 /*
484 * Setup the apic timer manually
485 */
486 levt->event_handler = lapic_cal_handler;
487 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
488 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800489
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800490 /* Let the interrupts run */
491 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800492
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800493 while(lapic_cal_loops <= LAPIC_CAL_LOOPS)
494 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800495
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800496 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800497
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800498 /* Stop the lapic timer */
499 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800500
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800501 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800502
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800503 /* Jiffies delta */
504 deltaj = lapic_cal_j2 - lapic_cal_j1;
505 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800506
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800507 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800508 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800509 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800510 else
511 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100512 } else
513 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800514
515 if (!local_apic_timer_verify_ok) {
516 printk(KERN_WARNING
517 "APIC timer disabled due to verification failure.\n");
518 /* No broadcast on UP ! */
519 if (num_possible_cpus() == 1)
520 return;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800521 } else {
522 /*
523 * If nmi_watchdog is set to IO_APIC, we need the
524 * PIT/HPET going. Otherwise register lapic as a dummy
525 * device.
526 */
527 if (nmi_watchdog != NMI_IO_APIC)
528 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
529 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800530
531 /* Setup the lapic or request the broadcast */
532 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Li Shaohua0bb31842005-06-25 14:54:55 -0700535void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800537 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800541 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800543static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800545 int cpu = smp_processor_id();
546 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800549 * Normally we should not be here till LAPIC has been initialized but
550 * in some cases like kdump, its possible that there is a pending LAPIC
551 * timer interrupt from previous kernel's context and is delivered in
552 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800554 * Interrupts are enabled early and LAPIC is setup much later, hence
555 * its possible that when we get here evt->event_handler is NULL.
556 * Check for event_handler being NULL and discard the interrupt as
557 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800559 if (!evt->event_handler) {
560 printk(KERN_WARNING
561 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
562 /* Switch it off */
563 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
564 return;
565 }
566
567 per_cpu(irq_stat, cpu).apic_timer_irqs++;
568
569 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570}
571
572/*
573 * Local APIC timer interrupt. This is the most natural way for doing
574 * local interrupts, but local timer interrupts can be emulated by
575 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
576 *
577 * [ if a single-CPU system runs an SMP kernel then we call the local
578 * interrupt as well. Thus we cannot inline the local irq ... ]
579 */
580
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800581void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
David Howells7d12e782006-10-05 14:55:46 +0100583 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /*
586 * NOTE! We'd better ACK the irq immediately,
587 * because timer handling can be slow.
588 */
589 ack_APIC_irq();
590 /*
591 * update_process_times() expects us to have done irq_enter().
592 * Besides, if we don't timer interrupts ignore the global
593 * interrupt lock, which is the WrongThing (tm) to do.
594 */
595 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800596 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800598
David Howells7d12e782006-10-05 14:55:46 +0100599 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100602int setup_profiling_timer(unsigned int multiplier)
603{
604 return -EINVAL;
605}
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800608 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800610
611/**
612 * clear_local_APIC - shutdown the local APIC
613 *
614 * This is called, when a CPU is disabled and before rebooting, so the state of
615 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
616 * leftovers during boot.
617 */
618void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800620 int maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 unsigned long v;
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800624 * Masking an LVT entry can trigger a local APIC error
625 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800627 if (maxlvt >= 3) {
628 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
629 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
630 }
631 /*
632 * Careful: we have to set masks only first to deassert
633 * any level-triggered sources.
634 */
635 v = apic_read(APIC_LVTT);
636 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
637 v = apic_read(APIC_LVT0);
638 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
639 v = apic_read(APIC_LVT1);
640 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
641 if (maxlvt >= 4) {
642 v = apic_read(APIC_LVTPC);
643 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800646 /* lets not touch this if we didn't frob it */
647#ifdef CONFIG_X86_MCE_P4THERMAL
648 if (maxlvt >= 5) {
649 v = apic_read(APIC_LVTTHMR);
650 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
651 }
652#endif
653 /*
654 * Clean APIC state for other OSs:
655 */
656 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
657 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
658 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
659 if (maxlvt >= 3)
660 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
661 if (maxlvt >= 4)
662 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
663
664#ifdef CONFIG_X86_MCE_P4THERMAL
665 if (maxlvt >= 5)
666 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
667#endif
668 /* Integrated APIC (!82489DX) ? */
669 if (lapic_is_integrated()) {
670 if (maxlvt > 3)
671 /* Clear ESR due to Pentium errata 3AP and 11AP */
672 apic_write(APIC_ESR, 0);
673 apic_read(APIC_ESR);
674 }
675}
676
677/**
678 * disable_local_APIC - clear and disable the local APIC
679 */
680void disable_local_APIC(void)
681{
682 unsigned long value;
683
684 clear_local_APIC();
685
686 /*
687 * Disable APIC (implies clearing of registers
688 * for 82489DX!).
689 */
690 value = apic_read(APIC_SPIV);
691 value &= ~APIC_SPIV_APIC_ENABLED;
692 apic_write_around(APIC_SPIV, value);
693
694 /*
695 * When LAPIC was disabled by the BIOS and enabled by the kernel,
696 * restore the disabled state.
697 */
698 if (enabled_via_apicbase) {
699 unsigned int l, h;
700
701 rdmsr(MSR_IA32_APICBASE, l, h);
702 l &= ~MSR_IA32_APICBASE_ENABLE;
703 wrmsr(MSR_IA32_APICBASE, l, h);
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800708 * If Linux enabled the LAPIC against the BIOS default disable it down before
709 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
710 * not power-off. Additionally clear all LVT entries before disable_local_APIC
711 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800713void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800715 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800717 if (!cpu_has_apic)
718 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800720 local_irq_save(flags);
721 clear_local_APIC();
722
723 if (enabled_via_apicbase)
724 disable_local_APIC();
725
726 local_irq_restore(flags);
727}
728
729/*
730 * This is to verify that we're looking at a real local APIC.
731 * Check these against your board if the CPUs aren't getting
732 * started for no apparent reason.
733 */
734int __init verify_local_APIC(void)
735{
736 unsigned int reg0, reg1;
737
738 /*
739 * The version register is read-only in a real APIC.
740 */
741 reg0 = apic_read(APIC_LVR);
742 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
743 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
744 reg1 = apic_read(APIC_LVR);
745 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
746
747 /*
748 * The two version reads above should print the same
749 * numbers. If the second one is different, then we
750 * poke at a non-APIC.
751 */
752 if (reg1 != reg0)
753 return 0;
754
755 /*
756 * Check if the version looks reasonably.
757 */
758 reg1 = GET_APIC_VERSION(reg0);
759 if (reg1 == 0x00 || reg1 == 0xff)
760 return 0;
761 reg1 = lapic_get_maxlvt();
762 if (reg1 < 0x02 || reg1 == 0xff)
763 return 0;
764
765 /*
766 * The ID register is read/write in a real APIC.
767 */
768 reg0 = apic_read(APIC_ID);
769 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
770
771 /*
772 * The next two are just to see if we have sane values.
773 * They're only really relevant if we're in Virtual Wire
774 * compatibility mode, but most boxes are anymore.
775 */
776 reg0 = apic_read(APIC_LVT0);
777 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
778 reg1 = apic_read(APIC_LVT1);
779 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
780
781 return 1;
782}
783
784/**
785 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
786 */
787void __init sync_Arb_IDs(void)
788{
789 /*
790 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
791 * needed on AMD.
792 */
793 if (modern_apic())
794 return;
795 /*
796 * Wait for idle.
797 */
798 apic_wait_icr_idle();
799
800 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
801 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
802 | APIC_DM_INIT);
803}
804
805/*
806 * An initial setup of the virtual wire mode.
807 */
808void __init init_bsp_APIC(void)
809{
810 unsigned long value;
811
812 /*
813 * Don't do the setup now if we have a SMP BIOS as the
814 * through-I/O-APIC virtual wire mode might be active.
815 */
816 if (smp_found_config || !cpu_has_apic)
817 return;
818
819 /*
820 * Do not trust the local APIC being empty at bootup.
821 */
822 clear_local_APIC();
823
824 /*
825 * Enable APIC.
826 */
827 value = apic_read(APIC_SPIV);
828 value &= ~APIC_VECTOR_MASK;
829 value |= APIC_SPIV_APIC_ENABLED;
830
831 /* This bit is reserved on P4/Xeon and should be cleared */
832 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
833 (boot_cpu_data.x86 == 15))
834 value &= ~APIC_SPIV_FOCUS_DISABLED;
835 else
836 value |= APIC_SPIV_FOCUS_DISABLED;
837 value |= SPURIOUS_APIC_VECTOR;
838 apic_write_around(APIC_SPIV, value);
839
840 /*
841 * Set up the virtual wire mode.
842 */
843 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
844 value = APIC_DM_NMI;
845 if (!lapic_is_integrated()) /* 82489DX */
846 value |= APIC_LVT_LEVEL_TRIGGER;
847 apic_write_around(APIC_LVT1, value);
848}
849
850/**
851 * setup_local_APIC - setup the local APIC
852 */
853void __devinit setup_local_APIC(void)
854{
855 unsigned long oldvalue, value, maxlvt, integrated;
856 int i, j;
857
858 /* Pound the ESR really hard over the head with a big hammer - mbligh */
859 if (esr_disable) {
860 apic_write(APIC_ESR, 0);
861 apic_write(APIC_ESR, 0);
862 apic_write(APIC_ESR, 0);
863 apic_write(APIC_ESR, 0);
864 }
865
866 integrated = lapic_is_integrated();
867
868 /*
869 * Double-check whether this APIC is really registered.
870 */
871 if (!apic_id_registered())
872 BUG();
873
874 /*
875 * Intel recommends to set DFR, LDR and TPR before enabling
876 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
877 * document number 292116). So here it goes...
878 */
879 init_apic_ldr();
880
881 /*
882 * Set Task Priority to 'accept all'. We never change this
883 * later on.
884 */
885 value = apic_read(APIC_TASKPRI);
886 value &= ~APIC_TPRI_MASK;
887 apic_write_around(APIC_TASKPRI, value);
888
889 /*
890 * After a crash, we no longer service the interrupts and a pending
891 * interrupt from previous kernel might still have ISR bit set.
892 *
893 * Most probably by now CPU has serviced that pending interrupt and
894 * it might not have done the ack_APIC_irq() because it thought,
895 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
896 * does not clear the ISR bit and cpu thinks it has already serivced
897 * the interrupt. Hence a vector might get locked. It was noticed
898 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
899 */
900 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
901 value = apic_read(APIC_ISR + i*0x10);
902 for (j = 31; j >= 0; j--) {
903 if (value & (1<<j))
904 ack_APIC_irq();
905 }
906 }
907
908 /*
909 * Now that we are all set up, enable the APIC
910 */
911 value = apic_read(APIC_SPIV);
912 value &= ~APIC_VECTOR_MASK;
913 /*
914 * Enable APIC
915 */
916 value |= APIC_SPIV_APIC_ENABLED;
917
918 /*
919 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
920 * certain networking cards. If high frequency interrupts are
921 * happening on a particular IOAPIC pin, plus the IOAPIC routing
922 * entry is masked/unmasked at a high rate as well then sooner or
923 * later IOAPIC line gets 'stuck', no more interrupts are received
924 * from the device. If focus CPU is disabled then the hang goes
925 * away, oh well :-(
926 *
927 * [ This bug can be reproduced easily with a level-triggered
928 * PCI Ne2000 networking cards and PII/PIII processors, dual
929 * BX chipset. ]
930 */
931 /*
932 * Actually disabling the focus CPU check just makes the hang less
933 * frequent as it makes the interrupt distributon model be more
934 * like LRU than MRU (the short-term load is more even across CPUs).
935 * See also the comment in end_level_ioapic_irq(). --macro
936 */
937
938 /* Enable focus processor (bit==0) */
939 value &= ~APIC_SPIV_FOCUS_DISABLED;
940
941 /*
942 * Set spurious IRQ vector
943 */
944 value |= SPURIOUS_APIC_VECTOR;
945 apic_write_around(APIC_SPIV, value);
946
947 /*
948 * Set up LVT0, LVT1:
949 *
950 * set up through-local-APIC on the BP's LINT0. This is not
951 * strictly necessery in pure symmetric-IO mode, but sometimes
952 * we delegate interrupts to the 8259A.
953 */
954 /*
955 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
956 */
957 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
958 if (!smp_processor_id() && (pic_mode || !value)) {
959 value = APIC_DM_EXTINT;
960 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
961 smp_processor_id());
962 } else {
963 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
964 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
965 smp_processor_id());
966 }
967 apic_write_around(APIC_LVT0, value);
968
969 /*
970 * only the BP should see the LINT1 NMI signal, obviously.
971 */
972 if (!smp_processor_id())
973 value = APIC_DM_NMI;
974 else
975 value = APIC_DM_NMI | APIC_LVT_MASKED;
976 if (!integrated) /* 82489DX */
977 value |= APIC_LVT_LEVEL_TRIGGER;
978 apic_write_around(APIC_LVT1, value);
979
980 if (integrated && !esr_disable) { /* !82489DX */
981 maxlvt = lapic_get_maxlvt();
982 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
983 apic_write(APIC_ESR, 0);
984 oldvalue = apic_read(APIC_ESR);
985
986 /* enables sending errors */
987 value = ERROR_APIC_VECTOR;
988 apic_write_around(APIC_LVTERR, value);
989 /*
990 * spec says clear errors after enabling vector.
991 */
992 if (maxlvt > 3)
993 apic_write(APIC_ESR, 0);
994 value = apic_read(APIC_ESR);
995 if (value != oldvalue)
996 apic_printk(APIC_VERBOSE, "ESR value before enabling "
997 "vector: 0x%08lx after: 0x%08lx\n",
998 oldvalue, value);
999 } else {
1000 if (esr_disable)
1001 /*
1002 * Something untraceble is creating bad interrupts on
1003 * secondary quads ... for the moment, just leave the
1004 * ESR disabled - we can't do anything useful with the
1005 * errors anyway - mbligh
1006 */
1007 printk(KERN_INFO "Leaving ESR disabled.\n");
1008 else
1009 printk(KERN_INFO "No ESR for 82489DX.\n");
1010 }
1011
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001012 /* Disable the local apic timer */
1013 value = apic_read(APIC_LVTT);
1014 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1015 apic_write_around(APIC_LVTT, value);
1016
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001017 setup_apic_nmi_watchdog(NULL);
1018 apic_pm_activate();
1019}
1020
1021/*
1022 * Detect and initialize APIC
1023 */
1024static int __init detect_init_APIC (void)
1025{
1026 u32 h, l, features;
1027
1028 /* Disabled by kernel option? */
1029 if (enable_local_apic < 0)
1030 return -1;
1031
1032 switch (boot_cpu_data.x86_vendor) {
1033 case X86_VENDOR_AMD:
1034 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1035 (boot_cpu_data.x86 == 15))
1036 break;
1037 goto no_apic;
1038 case X86_VENDOR_INTEL:
1039 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1040 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1041 break;
1042 goto no_apic;
1043 default:
1044 goto no_apic;
1045 }
1046
1047 if (!cpu_has_apic) {
1048 /*
1049 * Over-ride BIOS and try to enable the local APIC only if
1050 * "lapic" specified.
1051 */
1052 if (enable_local_apic <= 0) {
1053 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1054 "you can enable it with \"lapic\"\n");
1055 return -1;
1056 }
1057 /*
1058 * Some BIOSes disable the local APIC in the APIC_BASE
1059 * MSR. This can only be done in software for Intel P6 or later
1060 * and AMD K7 (Model > 1) or later.
1061 */
1062 rdmsr(MSR_IA32_APICBASE, l, h);
1063 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1064 printk(KERN_INFO
1065 "Local APIC disabled by BIOS -- reenabling.\n");
1066 l &= ~MSR_IA32_APICBASE_BASE;
1067 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1068 wrmsr(MSR_IA32_APICBASE, l, h);
1069 enabled_via_apicbase = 1;
1070 }
1071 }
1072 /*
1073 * The APIC feature bit should now be enabled
1074 * in `cpuid'
1075 */
1076 features = cpuid_edx(1);
1077 if (!(features & (1 << X86_FEATURE_APIC))) {
1078 printk(KERN_WARNING "Could not enable APIC!\n");
1079 return -1;
1080 }
1081 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1082 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1083
1084 /* The BIOS may have set up the APIC at some other address */
1085 rdmsr(MSR_IA32_APICBASE, l, h);
1086 if (l & MSR_IA32_APICBASE_ENABLE)
1087 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1088
1089 if (nmi_watchdog != NMI_NONE)
1090 nmi_watchdog = NMI_LOCAL_APIC;
1091
1092 printk(KERN_INFO "Found and enabled local APIC!\n");
1093
1094 apic_pm_activate();
1095
1096 return 0;
1097
1098no_apic:
1099 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1100 return -1;
1101}
1102
1103/**
1104 * init_apic_mappings - initialize APIC mappings
1105 */
1106void __init init_apic_mappings(void)
1107{
1108 unsigned long apic_phys;
1109
1110 /*
1111 * If no local APIC can be found then set up a fake all
1112 * zeroes page to simulate the local APIC and another
1113 * one for the IO-APIC.
1114 */
1115 if (!smp_found_config && detect_init_APIC()) {
1116 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1117 apic_phys = __pa(apic_phys);
1118 } else
1119 apic_phys = mp_lapic_addr;
1120
1121 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1122 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1123 apic_phys);
1124
1125 /*
1126 * Fetch the APIC ID of the BSP in case we have a
1127 * default configuration (or the MP table is broken).
1128 */
1129 if (boot_cpu_physical_apicid == -1U)
1130 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1131
1132#ifdef CONFIG_X86_IO_APIC
1133 {
1134 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1135 int i;
1136
1137 for (i = 0; i < nr_ioapics; i++) {
1138 if (smp_found_config) {
1139 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1140 if (!ioapic_phys) {
1141 printk(KERN_ERR
1142 "WARNING: bogus zero IO-APIC "
1143 "address found in MPTABLE, "
1144 "disabling IO/APIC support!\n");
1145 smp_found_config = 0;
1146 skip_ioapic_setup = 1;
1147 goto fake_ioapic_page;
1148 }
1149 } else {
1150fake_ioapic_page:
1151 ioapic_phys = (unsigned long)
1152 alloc_bootmem_pages(PAGE_SIZE);
1153 ioapic_phys = __pa(ioapic_phys);
1154 }
1155 set_fixmap_nocache(idx, ioapic_phys);
1156 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1157 __fix_to_virt(idx), ioapic_phys);
1158 idx++;
1159 }
1160 }
1161#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001165 * This initializes the IO-APIC and APIC hardware if this is
1166 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001168int __init APIC_init_uniprocessor (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001170 if (enable_local_apic < 0)
1171 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001172
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001173 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001174 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
1176 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001177 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001179 if (!cpu_has_apic &&
1180 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001182 boot_cpu_physical_apicid);
Andi Kleen3777a952006-02-03 21:51:53 +01001183 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return -1;
1185 }
1186
1187 verify_local_APIC();
1188
1189 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001190
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001191 /*
1192 * Hack: In case of kdump, after a crash, kernel might be booting
1193 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1194 * might be zero if read from MP tables. Get it from LAPIC.
1195 */
1196#ifdef CONFIG_CRASH_DUMP
1197 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1198#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001199 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 setup_local_APIC();
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001204 if (smp_found_config)
1205 if (!skip_ioapic_setup && nr_ioapics)
1206 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001208 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001209
1210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001212
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001213/*
1214 * APIC command line parameters
1215 */
Rusty Russell1a3f2392006-09-26 10:52:32 +02001216static int __init parse_lapic(char *arg)
1217{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001218 enable_local_apic = 1;
Rusty Russell1a3f2392006-09-26 10:52:32 +02001219 return 0;
1220}
1221early_param("lapic", parse_lapic);
1222
1223static int __init parse_nolapic(char *arg)
1224{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001225 enable_local_apic = -1;
1226 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Rusty Russell1a3f2392006-09-26 10:52:32 +02001227 return 0;
1228}
1229early_param("nolapic", parse_nolapic);
1230
Thomas Gleixnerad62ca22007-03-22 00:11:21 -08001231static int __init parse_disable_lapic_timer(char *arg)
1232{
1233 local_apic_timer_disabled = 1;
1234 return 0;
1235}
1236early_param("nolapic_timer", parse_disable_lapic_timer);
1237
Thomas Gleixnere585bef2007-03-23 16:08:01 +01001238static int __init parse_lapic_timer_c2_ok(char *arg)
1239{
1240 local_apic_timer_c2_ok = 1;
1241 return 0;
1242}
1243early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1244
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001245static int __init apic_set_verbosity(char *str)
1246{
1247 if (strcmp("debug", str) == 0)
1248 apic_verbosity = APIC_DEBUG;
1249 else if (strcmp("verbose", str) == 0)
1250 apic_verbosity = APIC_VERBOSE;
1251 return 1;
1252}
1253
1254__setup("apic=", apic_set_verbosity);
1255
1256
1257/*
1258 * Local APIC interrupts
1259 */
1260
1261/*
1262 * This interrupt should _never_ happen with our APIC/SMP architecture
1263 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001264void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001265{
1266 unsigned long v;
1267
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001268 irq_enter();
1269 /*
1270 * Check if this really is a spurious interrupt and ACK it
1271 * if it is a vectored one. Just in case...
1272 * Spurious interrupts should not be ACKed.
1273 */
1274 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1275 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1276 ack_APIC_irq();
1277
1278 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1279 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1280 "should never happen.\n", smp_processor_id());
1281 irq_exit();
1282}
1283
1284/*
1285 * This interrupt should never happen with our APIC/SMP architecture
1286 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001287void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001288{
1289 unsigned long v, v1;
1290
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001291 irq_enter();
1292 /* First tickle the hardware, only then report what went on. -- REW */
1293 v = apic_read(APIC_ESR);
1294 apic_write(APIC_ESR, 0);
1295 v1 = apic_read(APIC_ESR);
1296 ack_APIC_irq();
1297 atomic_inc(&irq_err_count);
1298
1299 /* Here is what the APIC error bits mean:
1300 0: Send CS error
1301 1: Receive CS error
1302 2: Send accept error
1303 3: Receive accept error
1304 4: Reserved
1305 5: Send illegal vector
1306 6: Received illegal vector
1307 7: Illegal register address
1308 */
1309 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1310 smp_processor_id(), v , v1);
1311 irq_exit();
1312}
1313
1314/*
1315 * Initialize APIC interrupts
1316 */
1317void __init apic_intr_init(void)
1318{
1319#ifdef CONFIG_SMP
1320 smp_intr_init();
1321#endif
1322 /* self generated IPI for local APIC timer */
1323 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1324
1325 /* IPI vectors for APIC spurious and error interrupts */
1326 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1327 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1328
1329 /* thermal monitor LVT interrupt */
1330#ifdef CONFIG_X86_MCE_P4THERMAL
1331 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1332#endif
1333}
1334
1335/**
1336 * connect_bsp_APIC - attach the APIC to the interrupt system
1337 */
1338void __init connect_bsp_APIC(void)
1339{
1340 if (pic_mode) {
1341 /*
1342 * Do not trust the local APIC being empty at bootup.
1343 */
1344 clear_local_APIC();
1345 /*
1346 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1347 * local APIC to INT and NMI lines.
1348 */
1349 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1350 "enabling APIC mode.\n");
1351 outb(0x70, 0x22);
1352 outb(0x01, 0x23);
1353 }
1354 enable_apic_mode();
1355}
1356
1357/**
1358 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1359 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1360 *
1361 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1362 * APIC is disabled.
1363 */
1364void disconnect_bsp_APIC(int virt_wire_setup)
1365{
1366 if (pic_mode) {
1367 /*
1368 * Put the board back into PIC mode (has an effect only on
1369 * certain older boards). Note that APIC interrupts, including
1370 * IPIs, won't work beyond this point! The only exception are
1371 * INIT IPIs.
1372 */
1373 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1374 "entering PIC mode.\n");
1375 outb(0x70, 0x22);
1376 outb(0x00, 0x23);
1377 } else {
1378 /* Go back to Virtual Wire compatibility mode */
1379 unsigned long value;
1380
1381 /* For the spurious interrupt use vector F, and enable it */
1382 value = apic_read(APIC_SPIV);
1383 value &= ~APIC_VECTOR_MASK;
1384 value |= APIC_SPIV_APIC_ENABLED;
1385 value |= 0xf;
1386 apic_write_around(APIC_SPIV, value);
1387
1388 if (!virt_wire_setup) {
1389 /*
1390 * For LVT0 make it edge triggered, active high,
1391 * external and enabled
1392 */
1393 value = apic_read(APIC_LVT0);
1394 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1395 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1396 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1397 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1398 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1399 apic_write_around(APIC_LVT0, value);
1400 } else {
1401 /* Disable LVT0 */
1402 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1403 }
1404
1405 /*
1406 * For LVT1 make it edge triggered, active high, nmi and
1407 * enabled
1408 */
1409 value = apic_read(APIC_LVT1);
1410 value &= ~(
1411 APIC_MODE_MASK | APIC_SEND_PENDING |
1412 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1413 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1414 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1415 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1416 apic_write_around(APIC_LVT1, value);
1417 }
1418}
1419
1420/*
1421 * Power management
1422 */
1423#ifdef CONFIG_PM
1424
1425static struct {
1426 int active;
1427 /* r/w apic fields */
1428 unsigned int apic_id;
1429 unsigned int apic_taskpri;
1430 unsigned int apic_ldr;
1431 unsigned int apic_dfr;
1432 unsigned int apic_spiv;
1433 unsigned int apic_lvtt;
1434 unsigned int apic_lvtpc;
1435 unsigned int apic_lvt0;
1436 unsigned int apic_lvt1;
1437 unsigned int apic_lvterr;
1438 unsigned int apic_tmict;
1439 unsigned int apic_tdcr;
1440 unsigned int apic_thmr;
1441} apic_pm_state;
1442
1443static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1444{
1445 unsigned long flags;
1446 int maxlvt;
1447
1448 if (!apic_pm_state.active)
1449 return 0;
1450
1451 maxlvt = lapic_get_maxlvt();
1452
1453 apic_pm_state.apic_id = apic_read(APIC_ID);
1454 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1455 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1456 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1457 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1458 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1459 if (maxlvt >= 4)
1460 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1461 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1462 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1463 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1464 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1465 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1466#ifdef CONFIG_X86_MCE_P4THERMAL
1467 if (maxlvt >= 5)
1468 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1469#endif
1470
1471 local_irq_save(flags);
1472 disable_local_APIC();
1473 local_irq_restore(flags);
1474 return 0;
1475}
1476
1477static int lapic_resume(struct sys_device *dev)
1478{
1479 unsigned int l, h;
1480 unsigned long flags;
1481 int maxlvt;
1482
1483 if (!apic_pm_state.active)
1484 return 0;
1485
1486 maxlvt = lapic_get_maxlvt();
1487
1488 local_irq_save(flags);
1489
1490 /*
1491 * Make sure the APICBASE points to the right address
1492 *
1493 * FIXME! This will be wrong if we ever support suspend on
1494 * SMP! We'll need to do this as part of the CPU restore!
1495 */
1496 rdmsr(MSR_IA32_APICBASE, l, h);
1497 l &= ~MSR_IA32_APICBASE_BASE;
1498 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1499 wrmsr(MSR_IA32_APICBASE, l, h);
1500
1501 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1502 apic_write(APIC_ID, apic_pm_state.apic_id);
1503 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1504 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1505 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1506 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1507 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1508 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1509#ifdef CONFIG_X86_MCE_P4THERMAL
1510 if (maxlvt >= 5)
1511 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1512#endif
1513 if (maxlvt >= 4)
1514 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1515 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1516 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1517 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1518 apic_write(APIC_ESR, 0);
1519 apic_read(APIC_ESR);
1520 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1521 apic_write(APIC_ESR, 0);
1522 apic_read(APIC_ESR);
1523 local_irq_restore(flags);
1524 return 0;
1525}
1526
1527/*
1528 * This device has no shutdown method - fully functioning local APICs
1529 * are needed on every CPU up until machine_halt/restart/poweroff.
1530 */
1531
1532static struct sysdev_class lapic_sysclass = {
1533 set_kset_name("lapic"),
1534 .resume = lapic_resume,
1535 .suspend = lapic_suspend,
1536};
1537
1538static struct sys_device device_lapic = {
1539 .id = 0,
1540 .cls = &lapic_sysclass,
1541};
1542
1543static void __devinit apic_pm_activate(void)
1544{
1545 apic_pm_state.active = 1;
1546}
1547
1548static int __init init_lapic_sysfs(void)
1549{
1550 int error;
1551
1552 if (!cpu_has_apic)
1553 return 0;
1554 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1555
1556 error = sysdev_class_register(&lapic_sysclass);
1557 if (!error)
1558 error = sysdev_register(&device_lapic);
1559 return error;
1560}
1561device_initcall(init_lapic_sysfs);
1562
1563#else /* CONFIG_PM */
1564
1565static void apic_pm_activate(void) { }
1566
1567#endif /* CONFIG_PM */