blob: 6f506020bd7dd6eafd92902041196e112a12572d [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
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300900void __cpuinit lapic_setup_esr(void)
901{
902 unsigned long oldvalue, value, maxlvt;
903 if (lapic_is_integrated() && !esr_disable) {
904 /* !82489DX */
905 maxlvt = lapic_get_maxlvt();
906 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
907 apic_write(APIC_ESR, 0);
908 oldvalue = apic_read(APIC_ESR);
909
910 /* enables sending errors */
911 value = ERROR_APIC_VECTOR;
912 apic_write_around(APIC_LVTERR, value);
913 /*
914 * spec says clear errors after enabling vector.
915 */
916 if (maxlvt > 3)
917 apic_write(APIC_ESR, 0);
918 value = apic_read(APIC_ESR);
919 if (value != oldvalue)
920 apic_printk(APIC_VERBOSE, "ESR value before enabling "
921 "vector: 0x%08lx after: 0x%08lx\n",
922 oldvalue, value);
923 } else {
924 if (esr_disable)
925 /*
926 * Something untraceable is creating bad interrupts on
927 * secondary quads ... for the moment, just leave the
928 * ESR disabled - we can't do anything useful with the
929 * errors anyway - mbligh
930 */
931 printk(KERN_INFO "Leaving ESR disabled.\n");
932 else
933 printk(KERN_INFO "No ESR for 82489DX.\n");
934 }
935}
936
937
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800938/**
939 * setup_local_APIC - setup the local APIC
940 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100941void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800942{
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300943 unsigned long value, integrated;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800944 int i, j;
945
946 /* Pound the ESR really hard over the head with a big hammer - mbligh */
947 if (esr_disable) {
948 apic_write(APIC_ESR, 0);
949 apic_write(APIC_ESR, 0);
950 apic_write(APIC_ESR, 0);
951 apic_write(APIC_ESR, 0);
952 }
953
954 integrated = lapic_is_integrated();
955
956 /*
957 * Double-check whether this APIC is really registered.
958 */
959 if (!apic_id_registered())
960 BUG();
961
962 /*
963 * Intel recommends to set DFR, LDR and TPR before enabling
964 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
965 * document number 292116). So here it goes...
966 */
967 init_apic_ldr();
968
969 /*
970 * Set Task Priority to 'accept all'. We never change this
971 * later on.
972 */
973 value = apic_read(APIC_TASKPRI);
974 value &= ~APIC_TPRI_MASK;
975 apic_write_around(APIC_TASKPRI, value);
976
977 /*
978 * After a crash, we no longer service the interrupts and a pending
979 * interrupt from previous kernel might still have ISR bit set.
980 *
981 * Most probably by now CPU has serviced that pending interrupt and
982 * it might not have done the ack_APIC_irq() because it thought,
983 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
984 * does not clear the ISR bit and cpu thinks it has already serivced
985 * the interrupt. Hence a vector might get locked. It was noticed
986 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
987 */
988 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
989 value = apic_read(APIC_ISR + i*0x10);
990 for (j = 31; j >= 0; j--) {
991 if (value & (1<<j))
992 ack_APIC_irq();
993 }
994 }
995
996 /*
997 * Now that we are all set up, enable the APIC
998 */
999 value = apic_read(APIC_SPIV);
1000 value &= ~APIC_VECTOR_MASK;
1001 /*
1002 * Enable APIC
1003 */
1004 value |= APIC_SPIV_APIC_ENABLED;
1005
1006 /*
1007 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1008 * certain networking cards. If high frequency interrupts are
1009 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1010 * entry is masked/unmasked at a high rate as well then sooner or
1011 * later IOAPIC line gets 'stuck', no more interrupts are received
1012 * from the device. If focus CPU is disabled then the hang goes
1013 * away, oh well :-(
1014 *
1015 * [ This bug can be reproduced easily with a level-triggered
1016 * PCI Ne2000 networking cards and PII/PIII processors, dual
1017 * BX chipset. ]
1018 */
1019 /*
1020 * Actually disabling the focus CPU check just makes the hang less
1021 * frequent as it makes the interrupt distributon model be more
1022 * like LRU than MRU (the short-term load is more even across CPUs).
1023 * See also the comment in end_level_ioapic_irq(). --macro
1024 */
1025
1026 /* Enable focus processor (bit==0) */
1027 value &= ~APIC_SPIV_FOCUS_DISABLED;
1028
1029 /*
1030 * Set spurious IRQ vector
1031 */
1032 value |= SPURIOUS_APIC_VECTOR;
1033 apic_write_around(APIC_SPIV, value);
1034
1035 /*
1036 * Set up LVT0, LVT1:
1037 *
1038 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001039 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001040 * we delegate interrupts to the 8259A.
1041 */
1042 /*
1043 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1044 */
1045 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1046 if (!smp_processor_id() && (pic_mode || !value)) {
1047 value = APIC_DM_EXTINT;
1048 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1049 smp_processor_id());
1050 } else {
1051 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1052 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1053 smp_processor_id());
1054 }
1055 apic_write_around(APIC_LVT0, value);
1056
1057 /*
1058 * only the BP should see the LINT1 NMI signal, obviously.
1059 */
1060 if (!smp_processor_id())
1061 value = APIC_DM_NMI;
1062 else
1063 value = APIC_DM_NMI | APIC_LVT_MASKED;
1064 if (!integrated) /* 82489DX */
1065 value |= APIC_LVT_LEVEL_TRIGGER;
1066 apic_write_around(APIC_LVT1, value);
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001067}
1068
1069void __cpuinit end_local_APIC_setup(void)
1070{
1071 unsigned long value;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001072
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001073 lapic_setup_esr();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001074 /* Disable the local apic timer */
1075 value = apic_read(APIC_LVTT);
1076 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1077 apic_write_around(APIC_LVTT, value);
1078
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001079 setup_apic_nmi_watchdog(NULL);
1080 apic_pm_activate();
1081}
1082
1083/*
1084 * Detect and initialize APIC
1085 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001086static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001087{
1088 u32 h, l, features;
1089
1090 /* Disabled by kernel option? */
1091 if (enable_local_apic < 0)
1092 return -1;
1093
1094 switch (boot_cpu_data.x86_vendor) {
1095 case X86_VENDOR_AMD:
1096 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1097 (boot_cpu_data.x86 == 15))
1098 break;
1099 goto no_apic;
1100 case X86_VENDOR_INTEL:
1101 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1102 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1103 break;
1104 goto no_apic;
1105 default:
1106 goto no_apic;
1107 }
1108
1109 if (!cpu_has_apic) {
1110 /*
1111 * Over-ride BIOS and try to enable the local APIC only if
1112 * "lapic" specified.
1113 */
1114 if (enable_local_apic <= 0) {
1115 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1116 "you can enable it with \"lapic\"\n");
1117 return -1;
1118 }
1119 /*
1120 * Some BIOSes disable the local APIC in the APIC_BASE
1121 * MSR. This can only be done in software for Intel P6 or later
1122 * and AMD K7 (Model > 1) or later.
1123 */
1124 rdmsr(MSR_IA32_APICBASE, l, h);
1125 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1126 printk(KERN_INFO
1127 "Local APIC disabled by BIOS -- reenabling.\n");
1128 l &= ~MSR_IA32_APICBASE_BASE;
1129 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1130 wrmsr(MSR_IA32_APICBASE, l, h);
1131 enabled_via_apicbase = 1;
1132 }
1133 }
1134 /*
1135 * The APIC feature bit should now be enabled
1136 * in `cpuid'
1137 */
1138 features = cpuid_edx(1);
1139 if (!(features & (1 << X86_FEATURE_APIC))) {
1140 printk(KERN_WARNING "Could not enable APIC!\n");
1141 return -1;
1142 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001143 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001144 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1145
1146 /* The BIOS may have set up the APIC at some other address */
1147 rdmsr(MSR_IA32_APICBASE, l, h);
1148 if (l & MSR_IA32_APICBASE_ENABLE)
1149 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1150
Daniel Gollub0328ece2007-08-15 02:40:35 +02001151 if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001152 nmi_watchdog = NMI_LOCAL_APIC;
1153
1154 printk(KERN_INFO "Found and enabled local APIC!\n");
1155
1156 apic_pm_activate();
1157
1158 return 0;
1159
1160no_apic:
1161 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1162 return -1;
1163}
1164
1165/**
1166 * init_apic_mappings - initialize APIC mappings
1167 */
1168void __init init_apic_mappings(void)
1169{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001170 /*
1171 * If no local APIC can be found then set up a fake all
1172 * zeroes page to simulate the local APIC and another
1173 * one for the IO-APIC.
1174 */
1175 if (!smp_found_config && detect_init_APIC()) {
1176 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1177 apic_phys = __pa(apic_phys);
1178 } else
1179 apic_phys = mp_lapic_addr;
1180
1181 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1182 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1183 apic_phys);
1184
1185 /*
1186 * Fetch the APIC ID of the BSP in case we have a
1187 * default configuration (or the MP table is broken).
1188 */
1189 if (boot_cpu_physical_apicid == -1U)
1190 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1191
1192#ifdef CONFIG_X86_IO_APIC
1193 {
1194 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1195 int i;
1196
1197 for (i = 0; i < nr_ioapics; i++) {
1198 if (smp_found_config) {
1199 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1200 if (!ioapic_phys) {
1201 printk(KERN_ERR
1202 "WARNING: bogus zero IO-APIC "
1203 "address found in MPTABLE, "
1204 "disabling IO/APIC support!\n");
1205 smp_found_config = 0;
1206 skip_ioapic_setup = 1;
1207 goto fake_ioapic_page;
1208 }
1209 } else {
1210fake_ioapic_page:
1211 ioapic_phys = (unsigned long)
1212 alloc_bootmem_pages(PAGE_SIZE);
1213 ioapic_phys = __pa(ioapic_phys);
1214 }
1215 set_fixmap_nocache(idx, ioapic_phys);
1216 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1217 __fix_to_virt(idx), ioapic_phys);
1218 idx++;
1219 }
1220 }
1221#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
1224/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001225 * This initializes the IO-APIC and APIC hardware if this is
1226 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001228int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001230 if (enable_local_apic < 0)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001231 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001232
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001233 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001234 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001237 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001239 if (!cpu_has_apic &&
1240 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001242 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001243 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -1;
1245 }
1246
1247 verify_local_APIC();
1248
1249 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001250
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001251 /*
1252 * Hack: In case of kdump, after a crash, kernel might be booting
1253 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1254 * might be zero if read from MP tables. Get it from LAPIC.
1255 */
1256#ifdef CONFIG_CRASH_DUMP
1257 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1258#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001259 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 setup_local_APIC();
1262
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001263 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001265 if (smp_found_config)
1266 if (!skip_ioapic_setup && nr_ioapics)
1267 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001269 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001270
1271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001273
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001274/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001275 * Local APIC interrupts
1276 */
1277
1278/*
1279 * This interrupt should _never_ happen with our APIC/SMP architecture
1280 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001281void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001282{
1283 unsigned long v;
1284
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001285 irq_enter();
1286 /*
1287 * Check if this really is a spurious interrupt and ACK it
1288 * if it is a vectored one. Just in case...
1289 * Spurious interrupts should not be ACKed.
1290 */
1291 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1292 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1293 ack_APIC_irq();
1294
1295 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1296 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1297 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001298 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001299 irq_exit();
1300}
1301
1302/*
1303 * This interrupt should never happen with our APIC/SMP architecture
1304 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001305void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001306{
1307 unsigned long v, v1;
1308
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001309 irq_enter();
1310 /* First tickle the hardware, only then report what went on. -- REW */
1311 v = apic_read(APIC_ESR);
1312 apic_write(APIC_ESR, 0);
1313 v1 = apic_read(APIC_ESR);
1314 ack_APIC_irq();
1315 atomic_inc(&irq_err_count);
1316
1317 /* Here is what the APIC error bits mean:
1318 0: Send CS error
1319 1: Receive CS error
1320 2: Send accept error
1321 3: Receive accept error
1322 4: Reserved
1323 5: Send illegal vector
1324 6: Received illegal vector
1325 7: Illegal register address
1326 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001327 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001328 smp_processor_id(), v , v1);
1329 irq_exit();
1330}
1331
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001332#ifdef CONFIG_SMP
1333void __init smp_intr_init(void)
1334{
1335 /*
1336 * IRQ0 must be given a fixed assignment and initialized,
1337 * because it's used before the IO-APIC is set up.
1338 */
1339 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1340
1341 /*
1342 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1343 * IPI, driven by wakeup.
1344 */
1345 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1346
1347 /* IPI for invalidation */
1348 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1349
1350 /* IPI for generic function call */
1351 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1352}
1353#endif
1354
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001355/*
1356 * Initialize APIC interrupts
1357 */
1358void __init apic_intr_init(void)
1359{
1360#ifdef CONFIG_SMP
1361 smp_intr_init();
1362#endif
1363 /* self generated IPI for local APIC timer */
1364 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1365
1366 /* IPI vectors for APIC spurious and error interrupts */
1367 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1368 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1369
1370 /* thermal monitor LVT interrupt */
1371#ifdef CONFIG_X86_MCE_P4THERMAL
1372 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1373#endif
1374}
1375
1376/**
1377 * connect_bsp_APIC - attach the APIC to the interrupt system
1378 */
1379void __init connect_bsp_APIC(void)
1380{
1381 if (pic_mode) {
1382 /*
1383 * Do not trust the local APIC being empty at bootup.
1384 */
1385 clear_local_APIC();
1386 /*
1387 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1388 * local APIC to INT and NMI lines.
1389 */
1390 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1391 "enabling APIC mode.\n");
1392 outb(0x70, 0x22);
1393 outb(0x01, 0x23);
1394 }
1395 enable_apic_mode();
1396}
1397
1398/**
1399 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1400 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1401 *
1402 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1403 * APIC is disabled.
1404 */
1405void disconnect_bsp_APIC(int virt_wire_setup)
1406{
1407 if (pic_mode) {
1408 /*
1409 * Put the board back into PIC mode (has an effect only on
1410 * certain older boards). Note that APIC interrupts, including
1411 * IPIs, won't work beyond this point! The only exception are
1412 * INIT IPIs.
1413 */
1414 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1415 "entering PIC mode.\n");
1416 outb(0x70, 0x22);
1417 outb(0x00, 0x23);
1418 } else {
1419 /* Go back to Virtual Wire compatibility mode */
1420 unsigned long value;
1421
1422 /* For the spurious interrupt use vector F, and enable it */
1423 value = apic_read(APIC_SPIV);
1424 value &= ~APIC_VECTOR_MASK;
1425 value |= APIC_SPIV_APIC_ENABLED;
1426 value |= 0xf;
1427 apic_write_around(APIC_SPIV, value);
1428
1429 if (!virt_wire_setup) {
1430 /*
1431 * For LVT0 make it edge triggered, active high,
1432 * external and enabled
1433 */
1434 value = apic_read(APIC_LVT0);
1435 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1436 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001437 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001438 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1439 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1440 apic_write_around(APIC_LVT0, value);
1441 } else {
1442 /* Disable LVT0 */
1443 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1444 }
1445
1446 /*
1447 * For LVT1 make it edge triggered, active high, nmi and
1448 * enabled
1449 */
1450 value = apic_read(APIC_LVT1);
1451 value &= ~(
1452 APIC_MODE_MASK | APIC_SEND_PENDING |
1453 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1454 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1455 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1456 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1457 apic_write_around(APIC_LVT1, value);
1458 }
1459}
1460
1461/*
1462 * Power management
1463 */
1464#ifdef CONFIG_PM
1465
1466static struct {
1467 int active;
1468 /* r/w apic fields */
1469 unsigned int apic_id;
1470 unsigned int apic_taskpri;
1471 unsigned int apic_ldr;
1472 unsigned int apic_dfr;
1473 unsigned int apic_spiv;
1474 unsigned int apic_lvtt;
1475 unsigned int apic_lvtpc;
1476 unsigned int apic_lvt0;
1477 unsigned int apic_lvt1;
1478 unsigned int apic_lvterr;
1479 unsigned int apic_tmict;
1480 unsigned int apic_tdcr;
1481 unsigned int apic_thmr;
1482} apic_pm_state;
1483
1484static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1485{
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 apic_pm_state.apic_id = apic_read(APIC_ID);
1495 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1496 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1497 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1498 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1499 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1500 if (maxlvt >= 4)
1501 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1502 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1503 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1504 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1505 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1506 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1507#ifdef CONFIG_X86_MCE_P4THERMAL
1508 if (maxlvt >= 5)
1509 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1510#endif
1511
1512 local_irq_save(flags);
1513 disable_local_APIC();
1514 local_irq_restore(flags);
1515 return 0;
1516}
1517
1518static int lapic_resume(struct sys_device *dev)
1519{
1520 unsigned int l, h;
1521 unsigned long flags;
1522 int maxlvt;
1523
1524 if (!apic_pm_state.active)
1525 return 0;
1526
1527 maxlvt = lapic_get_maxlvt();
1528
1529 local_irq_save(flags);
1530
1531 /*
1532 * Make sure the APICBASE points to the right address
1533 *
1534 * FIXME! This will be wrong if we ever support suspend on
1535 * SMP! We'll need to do this as part of the CPU restore!
1536 */
1537 rdmsr(MSR_IA32_APICBASE, l, h);
1538 l &= ~MSR_IA32_APICBASE_BASE;
1539 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1540 wrmsr(MSR_IA32_APICBASE, l, h);
1541
1542 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1543 apic_write(APIC_ID, apic_pm_state.apic_id);
1544 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1545 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1546 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1547 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1548 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1549 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1550#ifdef CONFIG_X86_MCE_P4THERMAL
1551 if (maxlvt >= 5)
1552 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1553#endif
1554 if (maxlvt >= 4)
1555 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1556 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1557 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1558 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1559 apic_write(APIC_ESR, 0);
1560 apic_read(APIC_ESR);
1561 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1562 apic_write(APIC_ESR, 0);
1563 apic_read(APIC_ESR);
1564 local_irq_restore(flags);
1565 return 0;
1566}
1567
1568/*
1569 * This device has no shutdown method - fully functioning local APICs
1570 * are needed on every CPU up until machine_halt/restart/poweroff.
1571 */
1572
1573static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001574 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001575 .resume = lapic_resume,
1576 .suspend = lapic_suspend,
1577};
1578
1579static struct sys_device device_lapic = {
1580 .id = 0,
1581 .cls = &lapic_sysclass,
1582};
1583
1584static void __devinit apic_pm_activate(void)
1585{
1586 apic_pm_state.active = 1;
1587}
1588
1589static int __init init_lapic_sysfs(void)
1590{
1591 int error;
1592
1593 if (!cpu_has_apic)
1594 return 0;
1595 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1596
1597 error = sysdev_class_register(&lapic_sysclass);
1598 if (!error)
1599 error = sysdev_register(&device_lapic);
1600 return error;
1601}
1602device_initcall(init_lapic_sysfs);
1603
1604#else /* CONFIG_PM */
1605
1606static void apic_pm_activate(void) { }
1607
1608#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001609
1610/*
1611 * APIC command line parameters
1612 */
1613static int __init parse_lapic(char *arg)
1614{
1615 enable_local_apic = 1;
1616 return 0;
1617}
1618early_param("lapic", parse_lapic);
1619
1620static int __init parse_nolapic(char *arg)
1621{
1622 enable_local_apic = -1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001623 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001624 return 0;
1625}
1626early_param("nolapic", parse_nolapic);
1627
1628static int __init parse_disable_lapic_timer(char *arg)
1629{
1630 local_apic_timer_disabled = 1;
1631 return 0;
1632}
1633early_param("nolapic_timer", parse_disable_lapic_timer);
1634
1635static int __init parse_lapic_timer_c2_ok(char *arg)
1636{
1637 local_apic_timer_c2_ok = 1;
1638 return 0;
1639}
1640early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1641
1642static int __init apic_set_verbosity(char *str)
1643{
1644 if (strcmp("debug", str) == 0)
1645 apic_verbosity = APIC_DEBUG;
1646 else if (strcmp("verbose", str) == 0)
1647 apic_verbosity = APIC_VERBOSE;
1648 return 1;
1649}
1650__setup("apic=", apic_set_verbosity);
1651