blob: 6aa93db7faa3751a5c9ac9e4cf48cc29689d35ce [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/mc146818rtc.h>
24#include <linux/kernel_stat.h>
25#include <linux/sysdev.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070026#include <linux/cpu.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080027#include <linux/clockchips.h>
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080028#include <linux/acpi_pmtmr.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010029#include <linux/module.h>
Thomas Gleixnerad62ca22007-03-22 00:11:21 -080030#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/atomic.h>
33#include <asm/smp.h>
34#include <asm/mtrr.h>
35#include <asm/mpspec.h>
36#include <asm/desc.h>
37#include <asm/arch_hooks.h>
38#include <asm/hpet.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070039#include <asm/i8253.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020040#include <asm/nmi.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080047 * Sanity check
48 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +010049#if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
Thomas Gleixnere05d7232007-02-16 01:27:58 -080050# error SPURIOUS_APIC_VECTOR definition error
51#endif
52
53/*
Eric W. Biederman9635b472005-06-25 14:57:41 -070054 * Knob to control our willingness to enable the local APIC.
Thomas Gleixnere05d7232007-02-16 01:27:58 -080055 *
56 * -1=force-disable, +1=force-enable
Eric W. Biederman9635b472005-06-25 14:57:41 -070057 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +010058static int enable_local_apic __initdata;
Eric W. Biederman9635b472005-06-25 14:57:41 -070059
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080060/* Local APIC timer verification ok */
61static int local_apic_timer_verify_ok;
Andi Kleend3f7eae2007-08-10 22:31:07 +020062/* Disable local APIC timer from the kernel commandline or via dmi quirk
63 or using CPU MSR check */
64int local_apic_timer_disabled;
Thomas Gleixnere585bef2007-03-23 16:08:01 +010065/* Local APIC timer works in C2 */
66int local_apic_timer_c2_ok;
67EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080068
Eric W. Biederman9635b472005-06-25 14:57:41 -070069/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080070 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72int apic_verbosity;
73
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080074static unsigned int calibration_result;
75
76static int lapic_next_event(unsigned long delta,
77 struct clock_event_device *evt);
78static void lapic_timer_setup(enum clock_event_mode mode,
79 struct clock_event_device *evt);
80static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static void apic_pm_activate(void);
82
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080083/*
84 * The local apic timer can be used for any function which is CPU local.
85 */
86static struct clock_event_device lapic_clockevent = {
87 .name = "lapic",
88 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080089 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080090 .shift = 32,
91 .set_mode = lapic_timer_setup,
92 .set_next_event = lapic_next_event,
93 .broadcast = lapic_timer_broadcast,
94 .rating = 100,
95 .irq = -1,
96};
97static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Thomas Gleixnere05d7232007-02-16 01:27:58 -080099/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int enabled_via_apicbase;
101
Andi Kleend3432892008-01-30 13:33:17 +0100102static unsigned long apic_phys;
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/*
Joe Perchesab4a5742008-01-30 13:31:42 +0100113 * Check, if the APIC is integrated or a separate chip
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800114 */
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
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200132void apic_wait_icr_idle(void)
133{
134 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
135 cpu_relax();
136}
137
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100138u32 safe_apic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200139{
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100140 u32 send_status;
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200141 int timeout;
142
143 timeout = 0;
144 do {
145 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
146 if (!send_status)
147 break;
148 udelay(100);
149 } while (timeout++ < 1000);
150
151 return send_status;
152}
153
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800154/**
155 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
156 */
Jan Beuliche9427102008-01-30 13:31:24 +0100157void __cpuinit enable_NMI_through_LVT0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800159 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800161 /* Level triggered for 82489DX */
162 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 v |= APIC_LVT_LEVEL_TRIGGER;
164 apic_write_around(APIC_LVT0, v);
165}
166
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800167/**
168 * get_physical_broadcast - Get number of physical broadcast IDs
169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170int get_physical_broadcast(void)
171{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800172 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800175/**
176 * lapic_get_maxlvt - get the maximum number of local vector table entries
177 */
178int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800180 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800183 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800187 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800190/* Clock divisor is set to 16 */
191#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/*
194 * This function sets up the local APIC timer, with a timeout of
195 * 'clocks' APIC bus clock. During calibration we actually call
196 * this function twice on the boot CPU, once with a bogus timeout
197 * value, second time for real. The other (noncalibrating) CPUs
198 * call this function only once, with the real, calibrated value.
199 *
200 * We do reads before writes even if unnecessary, to get around the
201 * P5 APIC double write bug.
202 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800203static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800205 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800207 lvtt_value = LOCAL_TIMER_VECTOR;
208 if (!oneshot)
209 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800210 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100212
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800213 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100214 lvtt_value |= APIC_LVT_MASKED;
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 apic_write_around(APIC_LVTT, lvtt_value);
217
218 /*
219 * Divide PICLK by 16
220 */
221 tmp_value = apic_read(APIC_TDCR);
222 apic_write_around(APIC_TDCR, (tmp_value
223 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
224 | APIC_TDR_DIV_16);
225
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800226 if (!oneshot)
227 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800230/*
231 * Program the next event, relative to now
232 */
233static int lapic_next_event(unsigned long delta,
234 struct clock_event_device *evt)
235{
236 apic_write_around(APIC_TMICT, delta);
237 return 0;
238}
239
240/*
241 * Setup the lapic timer in periodic or oneshot mode
242 */
243static void lapic_timer_setup(enum clock_event_mode mode,
244 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800247 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800249 /* Lapic used for broadcast ? */
250 if (!local_apic_timer_verify_ok)
251 return;
252
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 local_irq_save(flags);
254
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800255 switch (mode) {
256 case CLOCK_EVT_MODE_PERIODIC:
257 case CLOCK_EVT_MODE_ONESHOT:
258 __setup_APIC_LVTT(calibration_result,
259 mode != CLOCK_EVT_MODE_PERIODIC, 1);
260 break;
261 case CLOCK_EVT_MODE_UNUSED:
262 case CLOCK_EVT_MODE_SHUTDOWN:
263 v = apic_read(APIC_LVTT);
264 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
265 apic_write_around(APIC_LVTT, v);
266 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700267 case CLOCK_EVT_MODE_RESUME:
268 /* Nothing to do here */
269 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 local_irq_restore(flags);
273}
274
275/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800276 * Local APIC timer broadcast function
277 */
278static void lapic_timer_broadcast(cpumask_t mask)
279{
280#ifdef CONFIG_SMP
281 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
282#endif
283}
284
285/*
286 * Setup the local APIC timer for this CPU. Copy the initilized values
287 * of the boot CPU and register the clock event in the framework.
288 */
289static void __devinit setup_APIC_timer(void)
290{
291 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
292
293 memcpy(levt, &lapic_clockevent, sizeof(*levt));
294 levt->cpumask = cpumask_of_cpu(smp_processor_id());
295
296 clockevents_register_device(levt);
297}
298
299/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800300 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800302 * We want to do the calibration only once since we want to have local timer
303 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
304 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800305 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800306 * This was previously done by reading the PIT/HPET and waiting for a wrap
307 * around to find out, that a tick has elapsed. I have a box, where the PIT
308 * readout is broken, so it never gets out of the wait loop again. This was
309 * also reported by others.
310 *
311 * Monitoring the jiffies value is inaccurate and the clockevents
312 * infrastructure allows us to do a simple substitution of the interrupt
313 * handler.
314 *
315 * The calibration routine also uses the pm_timer when possible, as the PIT
316 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
317 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 */
319
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800320#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200322static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800323static __initdata long lapic_cal_t1, lapic_cal_t2;
324static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
325static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
326static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
327
328/*
329 * Temporary interrupt handler.
330 */
331static void __init lapic_cal_handler(struct clock_event_device *dev)
332{
333 unsigned long long tsc = 0;
334 long tapic = apic_read(APIC_TMCCT);
335 unsigned long pm = acpi_pm_read_early();
336
337 if (cpu_has_tsc)
338 rdtscll(tsc);
339
340 switch (lapic_cal_loops++) {
341 case 0:
342 lapic_cal_t1 = tapic;
343 lapic_cal_tsc1 = tsc;
344 lapic_cal_pm1 = pm;
345 lapic_cal_j1 = jiffies;
346 break;
347
348 case LAPIC_CAL_LOOPS:
349 lapic_cal_t2 = tapic;
350 lapic_cal_tsc2 = tsc;
351 if (pm < lapic_cal_pm1)
352 pm += ACPI_PM_OVRRUN;
353 lapic_cal_pm2 = pm;
354 lapic_cal_j2 = jiffies;
355 break;
356 }
357}
358
359/*
360 * Setup the boot APIC
361 *
362 * Calibrate and verify the result.
363 */
364void __init setup_boot_APIC_clock(void)
365{
366 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
367 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
368 const long pm_thresh = pm_100ms/100;
369 void (*real_handler)(struct clock_event_device *dev);
370 unsigned long deltaj;
371 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800372 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800373
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800374 /*
375 * The local apic timer can be disabled via the kernel
Andi Kleend3f7eae2007-08-10 22:31:07 +0200376 * commandline or from the CPU detection code. Register the lapic
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800377 * timer as a dummy clock event source on SMP systems, so the
378 * broadcast mechanism is used. On UP systems simply ignore it.
379 */
380 if (local_apic_timer_disabled) {
381 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100382 if (num_possible_cpus() > 1) {
383 lapic_clockevent.mult = 1;
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800384 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100385 }
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800386 return;
387 }
388
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800389 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
390 "calibrating APIC timer ...\n");
391
392 local_irq_disable();
393
394 /* Replace the global interrupt handler */
395 real_handler = global_clock_event->event_handler;
396 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800399 * Setup the APIC counter to 1e9. There is no way the lapic
400 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800402 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800404 /* Let the interrupts run */
405 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800407 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
408 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800410 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800412 /* Restore the real event handler */
413 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800415 /* Build delta t1-t2 as apic timer counts down */
416 delta = lapic_cal_t1 - lapic_cal_t2;
417 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800419 /* Check, if the PM timer is available */
420 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
421 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800423 if (deltapm) {
424 unsigned long mult;
425 u64 res;
426
427 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
428
429 if (deltapm > (pm_100ms - pm_thresh) &&
430 deltapm < (pm_100ms + pm_thresh)) {
431 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
432 } else {
433 res = (((u64) deltapm) * mult) >> 22;
434 do_div(res, 1000000);
435 printk(KERN_WARNING "APIC calibration not consistent "
436 "with PM Timer: %ldms instead of 100ms\n",
437 (long)res);
438 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100439 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800440 do_div(res, deltapm);
441 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
442 "%lu (%ld)\n", (unsigned long) res, delta);
443 delta = (long) res;
444 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800445 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800448 /* Calculate the scaled math multiplication factor */
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800449 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800450 lapic_clockevent.max_delta_ns =
451 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
452 lapic_clockevent.min_delta_ns =
453 clockevent_delta2ns(0xF, &lapic_clockevent);
454
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800455 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800456
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800457 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
458 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
459 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
460 calibration_result);
461
462 if (cpu_has_tsc) {
463 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800465 "%ld.%04ld MHz.\n",
466 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
467 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800471 "%u.%04u MHz.\n",
472 calibration_result / (1000000 / HZ),
473 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800475 local_apic_timer_verify_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100477 /*
478 * Do a sanity check on the APIC calibration result
479 */
480 if (calibration_result < (1000000 / HZ)) {
481 local_irq_enable();
482 printk(KERN_WARNING
483 "APIC frequency too slow, disabling apic timer\n");
484 /* No broadcast on UP ! */
485 if (num_possible_cpus() > 1)
486 setup_APIC_timer();
487 return;
488 }
489
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800490 /* We trust the pm timer based calibration */
491 if (!pm_referenced) {
492 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800493
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800494 /*
495 * Setup the apic timer manually
496 */
497 levt->event_handler = lapic_cal_handler;
498 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
499 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800500
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800501 /* Let the interrupts run */
502 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800503
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200504 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800505 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800506
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800507 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800508
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800509 /* Stop the lapic timer */
510 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800511
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800512 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800513
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800514 /* Jiffies delta */
515 deltaj = lapic_cal_j2 - lapic_cal_j1;
516 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800517
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800518 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800519 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800520 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800521 else
522 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100523 } else
524 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800525
526 if (!local_apic_timer_verify_ok) {
527 printk(KERN_WARNING
528 "APIC timer disabled due to verification failure.\n");
529 /* No broadcast on UP ! */
530 if (num_possible_cpus() == 1)
531 return;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800532 } else {
533 /*
534 * If nmi_watchdog is set to IO_APIC, we need the
535 * PIT/HPET going. Otherwise register lapic as a dummy
536 * device.
537 */
538 if (nmi_watchdog != NMI_IO_APIC)
539 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
Ingo Molnar820de5c2007-07-21 04:37:36 -0700540 else
541 printk(KERN_WARNING "APIC timer registered as dummy,"
542 " due to nmi_watchdog=1!\n");
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800543 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800544
545 /* Setup the lapic or request the broadcast */
546 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
Li Shaohua0bb31842005-06-25 14:54:55 -0700549void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800551 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800555 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800557static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800559 int cpu = smp_processor_id();
560 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800563 * Normally we should not be here till LAPIC has been initialized but
564 * in some cases like kdump, its possible that there is a pending LAPIC
565 * timer interrupt from previous kernel's context and is delivered in
566 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800568 * Interrupts are enabled early and LAPIC is setup much later, hence
569 * its possible that when we get here evt->event_handler is NULL.
570 * Check for event_handler being NULL and discard the interrupt as
571 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800573 if (!evt->event_handler) {
574 printk(KERN_WARNING
575 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
576 /* Switch it off */
577 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
578 return;
579 }
580
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100581 /*
582 * the NMI deadlock-detector uses this.
583 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800584 per_cpu(irq_stat, cpu).apic_timer_irqs++;
585
586 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
589/*
590 * Local APIC timer interrupt. This is the most natural way for doing
591 * local interrupts, but local timer interrupts can be emulated by
592 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
593 *
594 * [ if a single-CPU system runs an SMP kernel then we call the local
595 * interrupt as well. Thus we cannot inline the local irq ... ]
596 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100597void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
David Howells7d12e782006-10-05 14:55:46 +0100599 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 /*
602 * NOTE! We'd better ACK the irq immediately,
603 * because timer handling can be slow.
604 */
605 ack_APIC_irq();
606 /*
607 * update_process_times() expects us to have done irq_enter().
608 * Besides, if we don't timer interrupts ignore the global
609 * interrupt lock, which is the WrongThing (tm) to do.
610 */
611 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800612 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800614
David Howells7d12e782006-10-05 14:55:46 +0100615 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100618int setup_profiling_timer(unsigned int multiplier)
619{
620 return -EINVAL;
621}
622
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623/*
Robert Richtere319e762008-02-13 16:19:36 +0100624 * Setup extended LVT, AMD specific (K8, family 10h)
625 *
626 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
627 * MCE interrupts are supported. Thus MCE offset must be set to 0.
628 */
629
630#define APIC_EILVT_LVTOFF_MCE 0
631#define APIC_EILVT_LVTOFF_IBS 1
632
633static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
634{
635 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
636 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
637 apic_write(reg, v);
638}
639
640u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
641{
642 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
643 return APIC_EILVT_LVTOFF_MCE;
644}
645
646u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
647{
648 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
649 return APIC_EILVT_LVTOFF_IBS;
650}
651
652/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800653 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800655
656/**
657 * clear_local_APIC - shutdown the local APIC
658 *
659 * This is called, when a CPU is disabled and before rebooting, so the state of
660 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
661 * leftovers during boot.
662 */
663void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
Andi Kleend3432892008-01-30 13:33:17 +0100665 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100666 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Andi Kleend3432892008-01-30 13:33:17 +0100668 /* APIC hasn't been mapped yet */
669 if (!apic_phys)
670 return;
671
672 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800674 * Masking an LVT entry can trigger a local APIC error
675 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800677 if (maxlvt >= 3) {
678 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
679 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
680 }
681 /*
682 * Careful: we have to set masks only first to deassert
683 * any level-triggered sources.
684 */
685 v = apic_read(APIC_LVTT);
686 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
687 v = apic_read(APIC_LVT0);
688 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
689 v = apic_read(APIC_LVT1);
690 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
691 if (maxlvt >= 4) {
692 v = apic_read(APIC_LVTPC);
693 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800696 /* lets not touch this if we didn't frob it */
697#ifdef CONFIG_X86_MCE_P4THERMAL
698 if (maxlvt >= 5) {
699 v = apic_read(APIC_LVTTHMR);
700 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
701 }
702#endif
703 /*
704 * Clean APIC state for other OSs:
705 */
706 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
707 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
708 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
709 if (maxlvt >= 3)
710 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
711 if (maxlvt >= 4)
712 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
713
714#ifdef CONFIG_X86_MCE_P4THERMAL
715 if (maxlvt >= 5)
716 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
717#endif
718 /* Integrated APIC (!82489DX) ? */
719 if (lapic_is_integrated()) {
720 if (maxlvt > 3)
721 /* Clear ESR due to Pentium errata 3AP and 11AP */
722 apic_write(APIC_ESR, 0);
723 apic_read(APIC_ESR);
724 }
725}
726
727/**
728 * disable_local_APIC - clear and disable the local APIC
729 */
730void disable_local_APIC(void)
731{
732 unsigned long value;
733
734 clear_local_APIC();
735
736 /*
737 * Disable APIC (implies clearing of registers
738 * for 82489DX!).
739 */
740 value = apic_read(APIC_SPIV);
741 value &= ~APIC_SPIV_APIC_ENABLED;
742 apic_write_around(APIC_SPIV, value);
743
744 /*
745 * When LAPIC was disabled by the BIOS and enabled by the kernel,
746 * restore the disabled state.
747 */
748 if (enabled_via_apicbase) {
749 unsigned int l, h;
750
751 rdmsr(MSR_IA32_APICBASE, l, h);
752 l &= ~MSR_IA32_APICBASE_ENABLE;
753 wrmsr(MSR_IA32_APICBASE, l, h);
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
757/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800758 * If Linux enabled the LAPIC against the BIOS default disable it down before
759 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
760 * not power-off. Additionally clear all LVT entries before disable_local_APIC
761 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800763void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800765 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800767 if (!cpu_has_apic)
768 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800770 local_irq_save(flags);
771 clear_local_APIC();
772
773 if (enabled_via_apicbase)
774 disable_local_APIC();
775
776 local_irq_restore(flags);
777}
778
779/*
780 * This is to verify that we're looking at a real local APIC.
781 * Check these against your board if the CPUs aren't getting
782 * started for no apparent reason.
783 */
784int __init verify_local_APIC(void)
785{
786 unsigned int reg0, reg1;
787
788 /*
789 * The version register is read-only in a real APIC.
790 */
791 reg0 = apic_read(APIC_LVR);
792 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
793 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
794 reg1 = apic_read(APIC_LVR);
795 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
796
797 /*
798 * The two version reads above should print the same
799 * numbers. If the second one is different, then we
800 * poke at a non-APIC.
801 */
802 if (reg1 != reg0)
803 return 0;
804
805 /*
806 * Check if the version looks reasonably.
807 */
808 reg1 = GET_APIC_VERSION(reg0);
809 if (reg1 == 0x00 || reg1 == 0xff)
810 return 0;
811 reg1 = lapic_get_maxlvt();
812 if (reg1 < 0x02 || reg1 == 0xff)
813 return 0;
814
815 /*
816 * The ID register is read/write in a real APIC.
817 */
818 reg0 = apic_read(APIC_ID);
819 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
820
821 /*
822 * The next two are just to see if we have sane values.
823 * They're only really relevant if we're in Virtual Wire
824 * compatibility mode, but most boxes are anymore.
825 */
826 reg0 = apic_read(APIC_LVT0);
827 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
828 reg1 = apic_read(APIC_LVT1);
829 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
830
831 return 1;
832}
833
834/**
835 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
836 */
837void __init sync_Arb_IDs(void)
838{
839 /*
840 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
841 * needed on AMD.
842 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100843 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800844 return;
845 /*
846 * Wait for idle.
847 */
848 apic_wait_icr_idle();
849
850 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
851 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
852 | APIC_DM_INIT);
853}
854
855/*
856 * An initial setup of the virtual wire mode.
857 */
858void __init init_bsp_APIC(void)
859{
860 unsigned long value;
861
862 /*
863 * Don't do the setup now if we have a SMP BIOS as the
864 * through-I/O-APIC virtual wire mode might be active.
865 */
866 if (smp_found_config || !cpu_has_apic)
867 return;
868
869 /*
870 * Do not trust the local APIC being empty at bootup.
871 */
872 clear_local_APIC();
873
874 /*
875 * Enable APIC.
876 */
877 value = apic_read(APIC_SPIV);
878 value &= ~APIC_VECTOR_MASK;
879 value |= APIC_SPIV_APIC_ENABLED;
880
881 /* This bit is reserved on P4/Xeon and should be cleared */
882 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
883 (boot_cpu_data.x86 == 15))
884 value &= ~APIC_SPIV_FOCUS_DISABLED;
885 else
886 value |= APIC_SPIV_FOCUS_DISABLED;
887 value |= SPURIOUS_APIC_VECTOR;
888 apic_write_around(APIC_SPIV, value);
889
890 /*
891 * Set up the virtual wire mode.
892 */
893 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
894 value = APIC_DM_NMI;
895 if (!lapic_is_integrated()) /* 82489DX */
896 value |= APIC_LVT_LEVEL_TRIGGER;
897 apic_write_around(APIC_LVT1, value);
898}
899
900/**
901 * setup_local_APIC - setup the local APIC
902 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100903void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800904{
905 unsigned long oldvalue, value, maxlvt, integrated;
906 int i, j;
907
908 /* Pound the ESR really hard over the head with a big hammer - mbligh */
909 if (esr_disable) {
910 apic_write(APIC_ESR, 0);
911 apic_write(APIC_ESR, 0);
912 apic_write(APIC_ESR, 0);
913 apic_write(APIC_ESR, 0);
914 }
915
916 integrated = lapic_is_integrated();
917
918 /*
919 * Double-check whether this APIC is really registered.
920 */
921 if (!apic_id_registered())
922 BUG();
923
924 /*
925 * Intel recommends to set DFR, LDR and TPR before enabling
926 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
927 * document number 292116). So here it goes...
928 */
929 init_apic_ldr();
930
931 /*
932 * Set Task Priority to 'accept all'. We never change this
933 * later on.
934 */
935 value = apic_read(APIC_TASKPRI);
936 value &= ~APIC_TPRI_MASK;
937 apic_write_around(APIC_TASKPRI, value);
938
939 /*
940 * After a crash, we no longer service the interrupts and a pending
941 * interrupt from previous kernel might still have ISR bit set.
942 *
943 * Most probably by now CPU has serviced that pending interrupt and
944 * it might not have done the ack_APIC_irq() because it thought,
945 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
946 * does not clear the ISR bit and cpu thinks it has already serivced
947 * the interrupt. Hence a vector might get locked. It was noticed
948 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
949 */
950 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
951 value = apic_read(APIC_ISR + i*0x10);
952 for (j = 31; j >= 0; j--) {
953 if (value & (1<<j))
954 ack_APIC_irq();
955 }
956 }
957
958 /*
959 * Now that we are all set up, enable the APIC
960 */
961 value = apic_read(APIC_SPIV);
962 value &= ~APIC_VECTOR_MASK;
963 /*
964 * Enable APIC
965 */
966 value |= APIC_SPIV_APIC_ENABLED;
967
968 /*
969 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
970 * certain networking cards. If high frequency interrupts are
971 * happening on a particular IOAPIC pin, plus the IOAPIC routing
972 * entry is masked/unmasked at a high rate as well then sooner or
973 * later IOAPIC line gets 'stuck', no more interrupts are received
974 * from the device. If focus CPU is disabled then the hang goes
975 * away, oh well :-(
976 *
977 * [ This bug can be reproduced easily with a level-triggered
978 * PCI Ne2000 networking cards and PII/PIII processors, dual
979 * BX chipset. ]
980 */
981 /*
982 * Actually disabling the focus CPU check just makes the hang less
983 * frequent as it makes the interrupt distributon model be more
984 * like LRU than MRU (the short-term load is more even across CPUs).
985 * See also the comment in end_level_ioapic_irq(). --macro
986 */
987
988 /* Enable focus processor (bit==0) */
989 value &= ~APIC_SPIV_FOCUS_DISABLED;
990
991 /*
992 * Set spurious IRQ vector
993 */
994 value |= SPURIOUS_APIC_VECTOR;
995 apic_write_around(APIC_SPIV, value);
996
997 /*
998 * Set up LVT0, LVT1:
999 *
1000 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001001 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001002 * we delegate interrupts to the 8259A.
1003 */
1004 /*
1005 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1006 */
1007 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1008 if (!smp_processor_id() && (pic_mode || !value)) {
1009 value = APIC_DM_EXTINT;
1010 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1011 smp_processor_id());
1012 } else {
1013 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1014 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1015 smp_processor_id());
1016 }
1017 apic_write_around(APIC_LVT0, value);
1018
1019 /*
1020 * only the BP should see the LINT1 NMI signal, obviously.
1021 */
1022 if (!smp_processor_id())
1023 value = APIC_DM_NMI;
1024 else
1025 value = APIC_DM_NMI | APIC_LVT_MASKED;
1026 if (!integrated) /* 82489DX */
1027 value |= APIC_LVT_LEVEL_TRIGGER;
1028 apic_write_around(APIC_LVT1, value);
1029
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001030 if (integrated && !esr_disable) {
1031 /* !82489DX */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001032 maxlvt = lapic_get_maxlvt();
1033 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1034 apic_write(APIC_ESR, 0);
1035 oldvalue = apic_read(APIC_ESR);
1036
1037 /* enables sending errors */
1038 value = ERROR_APIC_VECTOR;
1039 apic_write_around(APIC_LVTERR, value);
1040 /*
1041 * spec says clear errors after enabling vector.
1042 */
1043 if (maxlvt > 3)
1044 apic_write(APIC_ESR, 0);
1045 value = apic_read(APIC_ESR);
1046 if (value != oldvalue)
1047 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1048 "vector: 0x%08lx after: 0x%08lx\n",
1049 oldvalue, value);
1050 } else {
1051 if (esr_disable)
1052 /*
Simon Arlott27b46d72007-10-20 01:13:56 +02001053 * Something untraceable is creating bad interrupts on
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001054 * secondary quads ... for the moment, just leave the
1055 * ESR disabled - we can't do anything useful with the
1056 * errors anyway - mbligh
1057 */
1058 printk(KERN_INFO "Leaving ESR disabled.\n");
1059 else
1060 printk(KERN_INFO "No ESR for 82489DX.\n");
1061 }
1062
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001063 /* Disable the local apic timer */
1064 value = apic_read(APIC_LVTT);
1065 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1066 apic_write_around(APIC_LVTT, value);
1067
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001068 setup_apic_nmi_watchdog(NULL);
1069 apic_pm_activate();
1070}
1071
1072/*
1073 * Detect and initialize APIC
1074 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001075static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001076{
1077 u32 h, l, features;
1078
1079 /* Disabled by kernel option? */
1080 if (enable_local_apic < 0)
1081 return -1;
1082
1083 switch (boot_cpu_data.x86_vendor) {
1084 case X86_VENDOR_AMD:
1085 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1086 (boot_cpu_data.x86 == 15))
1087 break;
1088 goto no_apic;
1089 case X86_VENDOR_INTEL:
1090 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1091 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1092 break;
1093 goto no_apic;
1094 default:
1095 goto no_apic;
1096 }
1097
1098 if (!cpu_has_apic) {
1099 /*
1100 * Over-ride BIOS and try to enable the local APIC only if
1101 * "lapic" specified.
1102 */
1103 if (enable_local_apic <= 0) {
1104 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1105 "you can enable it with \"lapic\"\n");
1106 return -1;
1107 }
1108 /*
1109 * Some BIOSes disable the local APIC in the APIC_BASE
1110 * MSR. This can only be done in software for Intel P6 or later
1111 * and AMD K7 (Model > 1) or later.
1112 */
1113 rdmsr(MSR_IA32_APICBASE, l, h);
1114 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1115 printk(KERN_INFO
1116 "Local APIC disabled by BIOS -- reenabling.\n");
1117 l &= ~MSR_IA32_APICBASE_BASE;
1118 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1119 wrmsr(MSR_IA32_APICBASE, l, h);
1120 enabled_via_apicbase = 1;
1121 }
1122 }
1123 /*
1124 * The APIC feature bit should now be enabled
1125 * in `cpuid'
1126 */
1127 features = cpuid_edx(1);
1128 if (!(features & (1 << X86_FEATURE_APIC))) {
1129 printk(KERN_WARNING "Could not enable APIC!\n");
1130 return -1;
1131 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001132 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001133 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1134
1135 /* The BIOS may have set up the APIC at some other address */
1136 rdmsr(MSR_IA32_APICBASE, l, h);
1137 if (l & MSR_IA32_APICBASE_ENABLE)
1138 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1139
Daniel Gollub0328ece2007-08-15 02:40:35 +02001140 if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001141 nmi_watchdog = NMI_LOCAL_APIC;
1142
1143 printk(KERN_INFO "Found and enabled local APIC!\n");
1144
1145 apic_pm_activate();
1146
1147 return 0;
1148
1149no_apic:
1150 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1151 return -1;
1152}
1153
1154/**
1155 * init_apic_mappings - initialize APIC mappings
1156 */
1157void __init init_apic_mappings(void)
1158{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001159 /*
1160 * If no local APIC can be found then set up a fake all
1161 * zeroes page to simulate the local APIC and another
1162 * one for the IO-APIC.
1163 */
1164 if (!smp_found_config && detect_init_APIC()) {
1165 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1166 apic_phys = __pa(apic_phys);
1167 } else
1168 apic_phys = mp_lapic_addr;
1169
1170 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1171 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1172 apic_phys);
1173
1174 /*
1175 * Fetch the APIC ID of the BSP in case we have a
1176 * default configuration (or the MP table is broken).
1177 */
1178 if (boot_cpu_physical_apicid == -1U)
1179 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1180
1181#ifdef CONFIG_X86_IO_APIC
1182 {
1183 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1184 int i;
1185
1186 for (i = 0; i < nr_ioapics; i++) {
1187 if (smp_found_config) {
1188 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1189 if (!ioapic_phys) {
1190 printk(KERN_ERR
1191 "WARNING: bogus zero IO-APIC "
1192 "address found in MPTABLE, "
1193 "disabling IO/APIC support!\n");
1194 smp_found_config = 0;
1195 skip_ioapic_setup = 1;
1196 goto fake_ioapic_page;
1197 }
1198 } else {
1199fake_ioapic_page:
1200 ioapic_phys = (unsigned long)
1201 alloc_bootmem_pages(PAGE_SIZE);
1202 ioapic_phys = __pa(ioapic_phys);
1203 }
1204 set_fixmap_nocache(idx, ioapic_phys);
1205 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1206 __fix_to_virt(idx), ioapic_phys);
1207 idx++;
1208 }
1209 }
1210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
1213/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001214 * This initializes the IO-APIC and APIC hardware if this is
1215 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001217int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001219 if (enable_local_apic < 0)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001220 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001221
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001222 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001223 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
1225 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001226 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001228 if (!cpu_has_apic &&
1229 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001231 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001232 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 return -1;
1234 }
1235
1236 verify_local_APIC();
1237
1238 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001239
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001240 /*
1241 * Hack: In case of kdump, after a crash, kernel might be booting
1242 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1243 * might be zero if read from MP tables. Get it from LAPIC.
1244 */
1245#ifdef CONFIG_CRASH_DUMP
1246 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1247#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001248 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 setup_local_APIC();
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001253 if (smp_found_config)
1254 if (!skip_ioapic_setup && nr_ioapics)
1255 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001257 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001258
1259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001261
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001262/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001263 * Local APIC interrupts
1264 */
1265
1266/*
1267 * This interrupt should _never_ happen with our APIC/SMP architecture
1268 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001269void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001270{
1271 unsigned long v;
1272
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001273 irq_enter();
1274 /*
1275 * Check if this really is a spurious interrupt and ACK it
1276 * if it is a vectored one. Just in case...
1277 * Spurious interrupts should not be ACKed.
1278 */
1279 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1280 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1281 ack_APIC_irq();
1282
1283 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1284 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1285 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001286 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001287 irq_exit();
1288}
1289
1290/*
1291 * This interrupt should never happen with our APIC/SMP architecture
1292 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001293void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001294{
1295 unsigned long v, v1;
1296
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001297 irq_enter();
1298 /* First tickle the hardware, only then report what went on. -- REW */
1299 v = apic_read(APIC_ESR);
1300 apic_write(APIC_ESR, 0);
1301 v1 = apic_read(APIC_ESR);
1302 ack_APIC_irq();
1303 atomic_inc(&irq_err_count);
1304
1305 /* Here is what the APIC error bits mean:
1306 0: Send CS error
1307 1: Receive CS error
1308 2: Send accept error
1309 3: Receive accept error
1310 4: Reserved
1311 5: Send illegal vector
1312 6: Received illegal vector
1313 7: Illegal register address
1314 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001315 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001316 smp_processor_id(), v , v1);
1317 irq_exit();
1318}
1319
1320/*
1321 * Initialize APIC interrupts
1322 */
1323void __init apic_intr_init(void)
1324{
1325#ifdef CONFIG_SMP
1326 smp_intr_init();
1327#endif
1328 /* self generated IPI for local APIC timer */
1329 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1330
1331 /* IPI vectors for APIC spurious and error interrupts */
1332 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1333 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1334
1335 /* thermal monitor LVT interrupt */
1336#ifdef CONFIG_X86_MCE_P4THERMAL
1337 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1338#endif
1339}
1340
1341/**
1342 * connect_bsp_APIC - attach the APIC to the interrupt system
1343 */
1344void __init connect_bsp_APIC(void)
1345{
1346 if (pic_mode) {
1347 /*
1348 * Do not trust the local APIC being empty at bootup.
1349 */
1350 clear_local_APIC();
1351 /*
1352 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1353 * local APIC to INT and NMI lines.
1354 */
1355 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1356 "enabling APIC mode.\n");
1357 outb(0x70, 0x22);
1358 outb(0x01, 0x23);
1359 }
1360 enable_apic_mode();
1361}
1362
1363/**
1364 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1365 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1366 *
1367 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1368 * APIC is disabled.
1369 */
1370void disconnect_bsp_APIC(int virt_wire_setup)
1371{
1372 if (pic_mode) {
1373 /*
1374 * Put the board back into PIC mode (has an effect only on
1375 * certain older boards). Note that APIC interrupts, including
1376 * IPIs, won't work beyond this point! The only exception are
1377 * INIT IPIs.
1378 */
1379 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1380 "entering PIC mode.\n");
1381 outb(0x70, 0x22);
1382 outb(0x00, 0x23);
1383 } else {
1384 /* Go back to Virtual Wire compatibility mode */
1385 unsigned long value;
1386
1387 /* For the spurious interrupt use vector F, and enable it */
1388 value = apic_read(APIC_SPIV);
1389 value &= ~APIC_VECTOR_MASK;
1390 value |= APIC_SPIV_APIC_ENABLED;
1391 value |= 0xf;
1392 apic_write_around(APIC_SPIV, value);
1393
1394 if (!virt_wire_setup) {
1395 /*
1396 * For LVT0 make it edge triggered, active high,
1397 * external and enabled
1398 */
1399 value = apic_read(APIC_LVT0);
1400 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1401 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001402 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001403 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1404 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1405 apic_write_around(APIC_LVT0, value);
1406 } else {
1407 /* Disable LVT0 */
1408 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1409 }
1410
1411 /*
1412 * For LVT1 make it edge triggered, active high, nmi and
1413 * enabled
1414 */
1415 value = apic_read(APIC_LVT1);
1416 value &= ~(
1417 APIC_MODE_MASK | APIC_SEND_PENDING |
1418 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1419 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1420 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1421 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1422 apic_write_around(APIC_LVT1, value);
1423 }
1424}
1425
1426/*
1427 * Power management
1428 */
1429#ifdef CONFIG_PM
1430
1431static struct {
1432 int active;
1433 /* r/w apic fields */
1434 unsigned int apic_id;
1435 unsigned int apic_taskpri;
1436 unsigned int apic_ldr;
1437 unsigned int apic_dfr;
1438 unsigned int apic_spiv;
1439 unsigned int apic_lvtt;
1440 unsigned int apic_lvtpc;
1441 unsigned int apic_lvt0;
1442 unsigned int apic_lvt1;
1443 unsigned int apic_lvterr;
1444 unsigned int apic_tmict;
1445 unsigned int apic_tdcr;
1446 unsigned int apic_thmr;
1447} apic_pm_state;
1448
1449static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1450{
1451 unsigned long flags;
1452 int maxlvt;
1453
1454 if (!apic_pm_state.active)
1455 return 0;
1456
1457 maxlvt = lapic_get_maxlvt();
1458
1459 apic_pm_state.apic_id = apic_read(APIC_ID);
1460 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1461 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1462 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1463 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1464 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1465 if (maxlvt >= 4)
1466 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1467 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1468 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1469 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1470 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1471 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1472#ifdef CONFIG_X86_MCE_P4THERMAL
1473 if (maxlvt >= 5)
1474 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1475#endif
1476
1477 local_irq_save(flags);
1478 disable_local_APIC();
1479 local_irq_restore(flags);
1480 return 0;
1481}
1482
1483static int lapic_resume(struct sys_device *dev)
1484{
1485 unsigned int l, h;
1486 unsigned long flags;
1487 int maxlvt;
1488
1489 if (!apic_pm_state.active)
1490 return 0;
1491
1492 maxlvt = lapic_get_maxlvt();
1493
1494 local_irq_save(flags);
1495
1496 /*
1497 * Make sure the APICBASE points to the right address
1498 *
1499 * FIXME! This will be wrong if we ever support suspend on
1500 * SMP! We'll need to do this as part of the CPU restore!
1501 */
1502 rdmsr(MSR_IA32_APICBASE, l, h);
1503 l &= ~MSR_IA32_APICBASE_BASE;
1504 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1505 wrmsr(MSR_IA32_APICBASE, l, h);
1506
1507 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1508 apic_write(APIC_ID, apic_pm_state.apic_id);
1509 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1510 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1511 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1512 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1513 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1514 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1515#ifdef CONFIG_X86_MCE_P4THERMAL
1516 if (maxlvt >= 5)
1517 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1518#endif
1519 if (maxlvt >= 4)
1520 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1521 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1522 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1523 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1524 apic_write(APIC_ESR, 0);
1525 apic_read(APIC_ESR);
1526 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1527 apic_write(APIC_ESR, 0);
1528 apic_read(APIC_ESR);
1529 local_irq_restore(flags);
1530 return 0;
1531}
1532
1533/*
1534 * This device has no shutdown method - fully functioning local APICs
1535 * are needed on every CPU up until machine_halt/restart/poweroff.
1536 */
1537
1538static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001539 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001540 .resume = lapic_resume,
1541 .suspend = lapic_suspend,
1542};
1543
1544static struct sys_device device_lapic = {
1545 .id = 0,
1546 .cls = &lapic_sysclass,
1547};
1548
1549static void __devinit apic_pm_activate(void)
1550{
1551 apic_pm_state.active = 1;
1552}
1553
1554static int __init init_lapic_sysfs(void)
1555{
1556 int error;
1557
1558 if (!cpu_has_apic)
1559 return 0;
1560 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1561
1562 error = sysdev_class_register(&lapic_sysclass);
1563 if (!error)
1564 error = sysdev_register(&device_lapic);
1565 return error;
1566}
1567device_initcall(init_lapic_sysfs);
1568
1569#else /* CONFIG_PM */
1570
1571static void apic_pm_activate(void) { }
1572
1573#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001574
1575/*
1576 * APIC command line parameters
1577 */
1578static int __init parse_lapic(char *arg)
1579{
1580 enable_local_apic = 1;
1581 return 0;
1582}
1583early_param("lapic", parse_lapic);
1584
1585static int __init parse_nolapic(char *arg)
1586{
1587 enable_local_apic = -1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001588 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001589 return 0;
1590}
1591early_param("nolapic", parse_nolapic);
1592
1593static int __init parse_disable_lapic_timer(char *arg)
1594{
1595 local_apic_timer_disabled = 1;
1596 return 0;
1597}
1598early_param("nolapic_timer", parse_disable_lapic_timer);
1599
1600static int __init parse_lapic_timer_c2_ok(char *arg)
1601{
1602 local_apic_timer_c2_ok = 1;
1603 return 0;
1604}
1605early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1606
1607static int __init apic_set_verbosity(char *str)
1608{
1609 if (strcmp("debug", str) == 0)
1610 apic_verbosity = APIC_DEBUG;
1611 else if (strcmp("verbose", str) == 0)
1612 apic_verbosity = APIC_VERBOSE;
1613 return 1;
1614}
1615__setup("apic=", apic_set_verbosity);
1616