blob: 68ea75fa6d35640984ff52951399f48ef58810dc [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
Alexey Starikovskiy8f6e2ca2008-03-27 23:54:38 +030053unsigned long mp_lapic_addr;
54
Thomas Gleixnere05d7232007-02-16 01:27:58 -080055/*
Eric W. Biederman9635b472005-06-25 14:57:41 -070056 * Knob to control our willingness to enable the local APIC.
Thomas Gleixnere05d7232007-02-16 01:27:58 -080057 *
58 * -1=force-disable, +1=force-enable
Eric W. Biederman9635b472005-06-25 14:57:41 -070059 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +010060static int enable_local_apic __initdata;
Eric W. Biederman9635b472005-06-25 14:57:41 -070061
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080062/* Local APIC timer verification ok */
63static int local_apic_timer_verify_ok;
Andi Kleend3f7eae2007-08-10 22:31:07 +020064/* Disable local APIC timer from the kernel commandline or via dmi quirk
65 or using CPU MSR check */
66int local_apic_timer_disabled;
Thomas Gleixnere585bef2007-03-23 16:08:01 +010067/* Local APIC timer works in C2 */
68int local_apic_timer_c2_ok;
69EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080070
Eric W. Biederman9635b472005-06-25 14:57:41 -070071/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080072 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74int apic_verbosity;
75
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080076static unsigned int calibration_result;
77
78static int lapic_next_event(unsigned long delta,
79 struct clock_event_device *evt);
80static void lapic_timer_setup(enum clock_event_mode mode,
81 struct clock_event_device *evt);
82static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083static void apic_pm_activate(void);
84
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080085/*
86 * The local apic timer can be used for any function which is CPU local.
87 */
88static struct clock_event_device lapic_clockevent = {
89 .name = "lapic",
90 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080091 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080092 .shift = 32,
93 .set_mode = lapic_timer_setup,
94 .set_next_event = lapic_next_event,
95 .broadcast = lapic_timer_broadcast,
96 .rating = 100,
97 .irq = -1,
98};
99static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800101/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102static int enabled_via_apicbase;
103
Andi Kleend3432892008-01-30 13:33:17 +0100104static unsigned long apic_phys;
105
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800106/*
107 * Get the LAPIC version
108 */
109static inline int lapic_get_version(void)
110{
111 return GET_APIC_VERSION(apic_read(APIC_LVR));
112}
113
114/*
Joe Perchesab4a5742008-01-30 13:31:42 +0100115 * Check, if the APIC is integrated or a separate chip
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800116 */
117static inline int lapic_is_integrated(void)
118{
119 return APIC_INTEGRATED(lapic_get_version());
120}
121
122/*
123 * Check, whether this is a modern or a first generation APIC
124 */
125static int modern_apic(void)
126{
127 /* AMD systems use old APIC versions, so check the CPU */
128 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
129 boot_cpu_data.x86 >= 0xf)
130 return 1;
131 return lapic_get_version() >= 0x14;
132}
133
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200134void apic_wait_icr_idle(void)
135{
136 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
137 cpu_relax();
138}
139
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100140u32 safe_apic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200141{
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100142 u32 send_status;
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200143 int timeout;
144
145 timeout = 0;
146 do {
147 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
148 if (!send_status)
149 break;
150 udelay(100);
151 } while (timeout++ < 1000);
152
153 return send_status;
154}
155
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800156/**
157 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
158 */
Jan Beuliche9427102008-01-30 13:31:24 +0100159void __cpuinit enable_NMI_through_LVT0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800161 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800163 /* Level triggered for 82489DX */
164 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 v |= APIC_LVT_LEVEL_TRIGGER;
166 apic_write_around(APIC_LVT0, v);
167}
168
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800169/**
170 * get_physical_broadcast - Get number of physical broadcast IDs
171 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172int get_physical_broadcast(void)
173{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800174 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800177/**
178 * lapic_get_maxlvt - get the maximum number of local vector table entries
179 */
180int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800182 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800185 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800189 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800192/* Clock divisor is set to 16 */
193#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195/*
196 * This function sets up the local APIC timer, with a timeout of
197 * 'clocks' APIC bus clock. During calibration we actually call
198 * this function twice on the boot CPU, once with a bogus timeout
199 * value, second time for real. The other (noncalibrating) CPUs
200 * call this function only once, with the real, calibrated value.
201 *
202 * We do reads before writes even if unnecessary, to get around the
203 * P5 APIC double write bug.
204 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800205static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800207 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800209 lvtt_value = LOCAL_TIMER_VECTOR;
210 if (!oneshot)
211 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800212 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100214
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800215 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100216 lvtt_value |= APIC_LVT_MASKED;
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 apic_write_around(APIC_LVTT, lvtt_value);
219
220 /*
221 * Divide PICLK by 16
222 */
223 tmp_value = apic_read(APIC_TDCR);
224 apic_write_around(APIC_TDCR, (tmp_value
225 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
226 | APIC_TDR_DIV_16);
227
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800228 if (!oneshot)
229 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800232/*
233 * Program the next event, relative to now
234 */
235static int lapic_next_event(unsigned long delta,
236 struct clock_event_device *evt)
237{
238 apic_write_around(APIC_TMICT, delta);
239 return 0;
240}
241
242/*
243 * Setup the lapic timer in periodic or oneshot mode
244 */
245static void lapic_timer_setup(enum clock_event_mode mode,
246 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800249 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800251 /* Lapic used for broadcast ? */
252 if (!local_apic_timer_verify_ok)
253 return;
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 local_irq_save(flags);
256
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800257 switch (mode) {
258 case CLOCK_EVT_MODE_PERIODIC:
259 case CLOCK_EVT_MODE_ONESHOT:
260 __setup_APIC_LVTT(calibration_result,
261 mode != CLOCK_EVT_MODE_PERIODIC, 1);
262 break;
263 case CLOCK_EVT_MODE_UNUSED:
264 case CLOCK_EVT_MODE_SHUTDOWN:
265 v = apic_read(APIC_LVTT);
266 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
267 apic_write_around(APIC_LVTT, v);
268 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700269 case CLOCK_EVT_MODE_RESUME:
270 /* Nothing to do here */
271 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 local_irq_restore(flags);
275}
276
277/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800278 * Local APIC timer broadcast function
279 */
280static void lapic_timer_broadcast(cpumask_t mask)
281{
282#ifdef CONFIG_SMP
283 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
284#endif
285}
286
287/*
288 * Setup the local APIC timer for this CPU. Copy the initilized values
289 * of the boot CPU and register the clock event in the framework.
290 */
291static void __devinit setup_APIC_timer(void)
292{
293 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
294
295 memcpy(levt, &lapic_clockevent, sizeof(*levt));
296 levt->cpumask = cpumask_of_cpu(smp_processor_id());
297
298 clockevents_register_device(levt);
299}
300
301/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800302 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800304 * We want to do the calibration only once since we want to have local timer
305 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
306 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800307 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800308 * This was previously done by reading the PIT/HPET and waiting for a wrap
309 * around to find out, that a tick has elapsed. I have a box, where the PIT
310 * readout is broken, so it never gets out of the wait loop again. This was
311 * also reported by others.
312 *
313 * Monitoring the jiffies value is inaccurate and the clockevents
314 * infrastructure allows us to do a simple substitution of the interrupt
315 * handler.
316 *
317 * The calibration routine also uses the pm_timer when possible, as the PIT
318 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
319 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 */
321
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800322#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200324static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800325static __initdata long lapic_cal_t1, lapic_cal_t2;
326static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
327static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
328static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
329
330/*
331 * Temporary interrupt handler.
332 */
333static void __init lapic_cal_handler(struct clock_event_device *dev)
334{
335 unsigned long long tsc = 0;
336 long tapic = apic_read(APIC_TMCCT);
337 unsigned long pm = acpi_pm_read_early();
338
339 if (cpu_has_tsc)
340 rdtscll(tsc);
341
342 switch (lapic_cal_loops++) {
343 case 0:
344 lapic_cal_t1 = tapic;
345 lapic_cal_tsc1 = tsc;
346 lapic_cal_pm1 = pm;
347 lapic_cal_j1 = jiffies;
348 break;
349
350 case LAPIC_CAL_LOOPS:
351 lapic_cal_t2 = tapic;
352 lapic_cal_tsc2 = tsc;
353 if (pm < lapic_cal_pm1)
354 pm += ACPI_PM_OVRRUN;
355 lapic_cal_pm2 = pm;
356 lapic_cal_j2 = jiffies;
357 break;
358 }
359}
360
361/*
362 * Setup the boot APIC
363 *
364 * Calibrate and verify the result.
365 */
366void __init setup_boot_APIC_clock(void)
367{
368 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
369 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
370 const long pm_thresh = pm_100ms/100;
371 void (*real_handler)(struct clock_event_device *dev);
372 unsigned long deltaj;
373 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800374 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800375
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800376 /*
377 * The local apic timer can be disabled via the kernel
Andi Kleend3f7eae2007-08-10 22:31:07 +0200378 * commandline or from the CPU detection code. Register the lapic
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800379 * timer as a dummy clock event source on SMP systems, so the
380 * broadcast mechanism is used. On UP systems simply ignore it.
381 */
382 if (local_apic_timer_disabled) {
383 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100384 if (num_possible_cpus() > 1) {
385 lapic_clockevent.mult = 1;
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800386 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100387 }
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800388 return;
389 }
390
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800391 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
392 "calibrating APIC timer ...\n");
393
394 local_irq_disable();
395
396 /* Replace the global interrupt handler */
397 real_handler = global_clock_event->event_handler;
398 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800401 * Setup the APIC counter to 1e9. There is no way the lapic
402 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800404 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800406 /* Let the interrupts run */
407 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800409 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
410 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800412 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800414 /* Restore the real event handler */
415 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800417 /* Build delta t1-t2 as apic timer counts down */
418 delta = lapic_cal_t1 - lapic_cal_t2;
419 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800421 /* Check, if the PM timer is available */
422 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
423 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800425 if (deltapm) {
426 unsigned long mult;
427 u64 res;
428
429 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
430
431 if (deltapm > (pm_100ms - pm_thresh) &&
432 deltapm < (pm_100ms + pm_thresh)) {
433 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
434 } else {
435 res = (((u64) deltapm) * mult) >> 22;
436 do_div(res, 1000000);
437 printk(KERN_WARNING "APIC calibration not consistent "
438 "with PM Timer: %ldms instead of 100ms\n",
439 (long)res);
440 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100441 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800442 do_div(res, deltapm);
443 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
444 "%lu (%ld)\n", (unsigned long) res, delta);
445 delta = (long) res;
446 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800447 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800450 /* Calculate the scaled math multiplication factor */
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800451 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800452 lapic_clockevent.max_delta_ns =
453 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
454 lapic_clockevent.min_delta_ns =
455 clockevent_delta2ns(0xF, &lapic_clockevent);
456
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800457 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800458
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800459 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
460 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
461 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
462 calibration_result);
463
464 if (cpu_has_tsc) {
465 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800467 "%ld.%04ld MHz.\n",
468 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
469 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800473 "%u.%04u MHz.\n",
474 calibration_result / (1000000 / HZ),
475 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800477 local_apic_timer_verify_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100479 /*
480 * Do a sanity check on the APIC calibration result
481 */
482 if (calibration_result < (1000000 / HZ)) {
483 local_irq_enable();
484 printk(KERN_WARNING
485 "APIC frequency too slow, disabling apic timer\n");
486 /* No broadcast on UP ! */
487 if (num_possible_cpus() > 1)
488 setup_APIC_timer();
489 return;
490 }
491
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800492 /* We trust the pm timer based calibration */
493 if (!pm_referenced) {
494 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800495
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800496 /*
497 * Setup the apic timer manually
498 */
499 levt->event_handler = lapic_cal_handler;
500 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
501 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800502
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800503 /* Let the interrupts run */
504 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800505
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200506 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800507 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800508
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800509 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800510
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800511 /* Stop the lapic timer */
512 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800513
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800514 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800515
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800516 /* Jiffies delta */
517 deltaj = lapic_cal_j2 - lapic_cal_j1;
518 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800519
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800520 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800521 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800522 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800523 else
524 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100525 } else
526 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800527
528 if (!local_apic_timer_verify_ok) {
529 printk(KERN_WARNING
530 "APIC timer disabled due to verification failure.\n");
531 /* No broadcast on UP ! */
532 if (num_possible_cpus() == 1)
533 return;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800534 } else {
535 /*
536 * If nmi_watchdog is set to IO_APIC, we need the
537 * PIT/HPET going. Otherwise register lapic as a dummy
538 * device.
539 */
540 if (nmi_watchdog != NMI_IO_APIC)
541 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
Ingo Molnar820de5c2007-07-21 04:37:36 -0700542 else
543 printk(KERN_WARNING "APIC timer registered as dummy,"
544 " due to nmi_watchdog=1!\n");
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800545 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800546
547 /* Setup the lapic or request the broadcast */
548 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Li Shaohua0bb31842005-06-25 14:54:55 -0700551void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800553 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800557 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800559static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800561 int cpu = smp_processor_id();
562 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800565 * Normally we should not be here till LAPIC has been initialized but
566 * in some cases like kdump, its possible that there is a pending LAPIC
567 * timer interrupt from previous kernel's context and is delivered in
568 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800570 * Interrupts are enabled early and LAPIC is setup much later, hence
571 * its possible that when we get here evt->event_handler is NULL.
572 * Check for event_handler being NULL and discard the interrupt as
573 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800575 if (!evt->event_handler) {
576 printk(KERN_WARNING
577 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
578 /* Switch it off */
579 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
580 return;
581 }
582
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100583 /*
584 * the NMI deadlock-detector uses this.
585 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800586 per_cpu(irq_stat, cpu).apic_timer_irqs++;
587
588 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/*
592 * Local APIC timer interrupt. This is the most natural way for doing
593 * local interrupts, but local timer interrupts can be emulated by
594 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
595 *
596 * [ if a single-CPU system runs an SMP kernel then we call the local
597 * interrupt as well. Thus we cannot inline the local irq ... ]
598 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100599void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
David Howells7d12e782006-10-05 14:55:46 +0100601 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /*
604 * NOTE! We'd better ACK the irq immediately,
605 * because timer handling can be slow.
606 */
607 ack_APIC_irq();
608 /*
609 * update_process_times() expects us to have done irq_enter().
610 * Besides, if we don't timer interrupts ignore the global
611 * interrupt lock, which is the WrongThing (tm) to do.
612 */
613 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800614 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800616
David Howells7d12e782006-10-05 14:55:46 +0100617 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100620int setup_profiling_timer(unsigned int multiplier)
621{
622 return -EINVAL;
623}
624
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625/*
Robert Richtere319e762008-02-13 16:19:36 +0100626 * Setup extended LVT, AMD specific (K8, family 10h)
627 *
628 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
629 * MCE interrupts are supported. Thus MCE offset must be set to 0.
630 */
631
632#define APIC_EILVT_LVTOFF_MCE 0
633#define APIC_EILVT_LVTOFF_IBS 1
634
635static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
636{
637 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
638 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
639 apic_write(reg, v);
640}
641
642u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
643{
644 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
645 return APIC_EILVT_LVTOFF_MCE;
646}
647
648u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
649{
650 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
651 return APIC_EILVT_LVTOFF_IBS;
652}
653
654/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800655 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800657
658/**
659 * clear_local_APIC - shutdown the local APIC
660 *
661 * This is called, when a CPU is disabled and before rebooting, so the state of
662 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
663 * leftovers during boot.
664 */
665void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Andi Kleend3432892008-01-30 13:33:17 +0100667 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100668 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Andi Kleend3432892008-01-30 13:33:17 +0100670 /* APIC hasn't been mapped yet */
671 if (!apic_phys)
672 return;
673
674 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800676 * Masking an LVT entry can trigger a local APIC error
677 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800679 if (maxlvt >= 3) {
680 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
681 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
682 }
683 /*
684 * Careful: we have to set masks only first to deassert
685 * any level-triggered sources.
686 */
687 v = apic_read(APIC_LVTT);
688 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
689 v = apic_read(APIC_LVT0);
690 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
691 v = apic_read(APIC_LVT1);
692 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
693 if (maxlvt >= 4) {
694 v = apic_read(APIC_LVTPC);
695 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
696 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800698 /* lets not touch this if we didn't frob it */
699#ifdef CONFIG_X86_MCE_P4THERMAL
700 if (maxlvt >= 5) {
701 v = apic_read(APIC_LVTTHMR);
702 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
703 }
704#endif
705 /*
706 * Clean APIC state for other OSs:
707 */
708 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
709 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
710 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
711 if (maxlvt >= 3)
712 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
713 if (maxlvt >= 4)
714 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
715
716#ifdef CONFIG_X86_MCE_P4THERMAL
717 if (maxlvt >= 5)
718 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
719#endif
720 /* Integrated APIC (!82489DX) ? */
721 if (lapic_is_integrated()) {
722 if (maxlvt > 3)
723 /* Clear ESR due to Pentium errata 3AP and 11AP */
724 apic_write(APIC_ESR, 0);
725 apic_read(APIC_ESR);
726 }
727}
728
729/**
730 * disable_local_APIC - clear and disable the local APIC
731 */
732void disable_local_APIC(void)
733{
734 unsigned long value;
735
736 clear_local_APIC();
737
738 /*
739 * Disable APIC (implies clearing of registers
740 * for 82489DX!).
741 */
742 value = apic_read(APIC_SPIV);
743 value &= ~APIC_SPIV_APIC_ENABLED;
744 apic_write_around(APIC_SPIV, value);
745
746 /*
747 * When LAPIC was disabled by the BIOS and enabled by the kernel,
748 * restore the disabled state.
749 */
750 if (enabled_via_apicbase) {
751 unsigned int l, h;
752
753 rdmsr(MSR_IA32_APICBASE, l, h);
754 l &= ~MSR_IA32_APICBASE_ENABLE;
755 wrmsr(MSR_IA32_APICBASE, l, h);
756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
759/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800760 * If Linux enabled the LAPIC against the BIOS default disable it down before
761 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
762 * not power-off. Additionally clear all LVT entries before disable_local_APIC
763 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800765void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800767 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800769 if (!cpu_has_apic)
770 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800772 local_irq_save(flags);
773 clear_local_APIC();
774
775 if (enabled_via_apicbase)
776 disable_local_APIC();
777
778 local_irq_restore(flags);
779}
780
781/*
782 * This is to verify that we're looking at a real local APIC.
783 * Check these against your board if the CPUs aren't getting
784 * started for no apparent reason.
785 */
786int __init verify_local_APIC(void)
787{
788 unsigned int reg0, reg1;
789
790 /*
791 * The version register is read-only in a real APIC.
792 */
793 reg0 = apic_read(APIC_LVR);
794 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
795 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
796 reg1 = apic_read(APIC_LVR);
797 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
798
799 /*
800 * The two version reads above should print the same
801 * numbers. If the second one is different, then we
802 * poke at a non-APIC.
803 */
804 if (reg1 != reg0)
805 return 0;
806
807 /*
808 * Check if the version looks reasonably.
809 */
810 reg1 = GET_APIC_VERSION(reg0);
811 if (reg1 == 0x00 || reg1 == 0xff)
812 return 0;
813 reg1 = lapic_get_maxlvt();
814 if (reg1 < 0x02 || reg1 == 0xff)
815 return 0;
816
817 /*
818 * The ID register is read/write in a real APIC.
819 */
820 reg0 = apic_read(APIC_ID);
821 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
822
823 /*
824 * The next two are just to see if we have sane values.
825 * They're only really relevant if we're in Virtual Wire
826 * compatibility mode, but most boxes are anymore.
827 */
828 reg0 = apic_read(APIC_LVT0);
829 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
830 reg1 = apic_read(APIC_LVT1);
831 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
832
833 return 1;
834}
835
836/**
837 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
838 */
839void __init sync_Arb_IDs(void)
840{
841 /*
842 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
843 * needed on AMD.
844 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100845 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800846 return;
847 /*
848 * Wait for idle.
849 */
850 apic_wait_icr_idle();
851
852 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
853 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
854 | APIC_DM_INIT);
855}
856
857/*
858 * An initial setup of the virtual wire mode.
859 */
860void __init init_bsp_APIC(void)
861{
862 unsigned long value;
863
864 /*
865 * Don't do the setup now if we have a SMP BIOS as the
866 * through-I/O-APIC virtual wire mode might be active.
867 */
868 if (smp_found_config || !cpu_has_apic)
869 return;
870
871 /*
872 * Do not trust the local APIC being empty at bootup.
873 */
874 clear_local_APIC();
875
876 /*
877 * Enable APIC.
878 */
879 value = apic_read(APIC_SPIV);
880 value &= ~APIC_VECTOR_MASK;
881 value |= APIC_SPIV_APIC_ENABLED;
882
883 /* This bit is reserved on P4/Xeon and should be cleared */
884 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
885 (boot_cpu_data.x86 == 15))
886 value &= ~APIC_SPIV_FOCUS_DISABLED;
887 else
888 value |= APIC_SPIV_FOCUS_DISABLED;
889 value |= SPURIOUS_APIC_VECTOR;
890 apic_write_around(APIC_SPIV, value);
891
892 /*
893 * Set up the virtual wire mode.
894 */
895 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
896 value = APIC_DM_NMI;
897 if (!lapic_is_integrated()) /* 82489DX */
898 value |= APIC_LVT_LEVEL_TRIGGER;
899 apic_write_around(APIC_LVT1, value);
900}
901
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300902void __cpuinit lapic_setup_esr(void)
903{
904 unsigned long oldvalue, value, maxlvt;
905 if (lapic_is_integrated() && !esr_disable) {
906 /* !82489DX */
907 maxlvt = lapic_get_maxlvt();
908 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
909 apic_write(APIC_ESR, 0);
910 oldvalue = apic_read(APIC_ESR);
911
912 /* enables sending errors */
913 value = ERROR_APIC_VECTOR;
914 apic_write_around(APIC_LVTERR, value);
915 /*
916 * spec says clear errors after enabling vector.
917 */
918 if (maxlvt > 3)
919 apic_write(APIC_ESR, 0);
920 value = apic_read(APIC_ESR);
921 if (value != oldvalue)
922 apic_printk(APIC_VERBOSE, "ESR value before enabling "
923 "vector: 0x%08lx after: 0x%08lx\n",
924 oldvalue, value);
925 } else {
926 if (esr_disable)
927 /*
928 * Something untraceable is creating bad interrupts on
929 * secondary quads ... for the moment, just leave the
930 * ESR disabled - we can't do anything useful with the
931 * errors anyway - mbligh
932 */
933 printk(KERN_INFO "Leaving ESR disabled.\n");
934 else
935 printk(KERN_INFO "No ESR for 82489DX.\n");
936 }
937}
938
939
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800940/**
941 * setup_local_APIC - setup the local APIC
942 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100943void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800944{
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300945 unsigned long value, integrated;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800946 int i, j;
947
948 /* Pound the ESR really hard over the head with a big hammer - mbligh */
949 if (esr_disable) {
950 apic_write(APIC_ESR, 0);
951 apic_write(APIC_ESR, 0);
952 apic_write(APIC_ESR, 0);
953 apic_write(APIC_ESR, 0);
954 }
955
956 integrated = lapic_is_integrated();
957
958 /*
959 * Double-check whether this APIC is really registered.
960 */
961 if (!apic_id_registered())
962 BUG();
963
964 /*
965 * Intel recommends to set DFR, LDR and TPR before enabling
966 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
967 * document number 292116). So here it goes...
968 */
969 init_apic_ldr();
970
971 /*
972 * Set Task Priority to 'accept all'. We never change this
973 * later on.
974 */
975 value = apic_read(APIC_TASKPRI);
976 value &= ~APIC_TPRI_MASK;
977 apic_write_around(APIC_TASKPRI, value);
978
979 /*
980 * After a crash, we no longer service the interrupts and a pending
981 * interrupt from previous kernel might still have ISR bit set.
982 *
983 * Most probably by now CPU has serviced that pending interrupt and
984 * it might not have done the ack_APIC_irq() because it thought,
985 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
986 * does not clear the ISR bit and cpu thinks it has already serivced
987 * the interrupt. Hence a vector might get locked. It was noticed
988 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
989 */
990 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
991 value = apic_read(APIC_ISR + i*0x10);
992 for (j = 31; j >= 0; j--) {
993 if (value & (1<<j))
994 ack_APIC_irq();
995 }
996 }
997
998 /*
999 * Now that we are all set up, enable the APIC
1000 */
1001 value = apic_read(APIC_SPIV);
1002 value &= ~APIC_VECTOR_MASK;
1003 /*
1004 * Enable APIC
1005 */
1006 value |= APIC_SPIV_APIC_ENABLED;
1007
1008 /*
1009 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1010 * certain networking cards. If high frequency interrupts are
1011 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1012 * entry is masked/unmasked at a high rate as well then sooner or
1013 * later IOAPIC line gets 'stuck', no more interrupts are received
1014 * from the device. If focus CPU is disabled then the hang goes
1015 * away, oh well :-(
1016 *
1017 * [ This bug can be reproduced easily with a level-triggered
1018 * PCI Ne2000 networking cards and PII/PIII processors, dual
1019 * BX chipset. ]
1020 */
1021 /*
1022 * Actually disabling the focus CPU check just makes the hang less
1023 * frequent as it makes the interrupt distributon model be more
1024 * like LRU than MRU (the short-term load is more even across CPUs).
1025 * See also the comment in end_level_ioapic_irq(). --macro
1026 */
1027
1028 /* Enable focus processor (bit==0) */
1029 value &= ~APIC_SPIV_FOCUS_DISABLED;
1030
1031 /*
1032 * Set spurious IRQ vector
1033 */
1034 value |= SPURIOUS_APIC_VECTOR;
1035 apic_write_around(APIC_SPIV, value);
1036
1037 /*
1038 * Set up LVT0, LVT1:
1039 *
1040 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001041 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001042 * we delegate interrupts to the 8259A.
1043 */
1044 /*
1045 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1046 */
1047 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1048 if (!smp_processor_id() && (pic_mode || !value)) {
1049 value = APIC_DM_EXTINT;
1050 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1051 smp_processor_id());
1052 } else {
1053 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1054 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1055 smp_processor_id());
1056 }
1057 apic_write_around(APIC_LVT0, value);
1058
1059 /*
1060 * only the BP should see the LINT1 NMI signal, obviously.
1061 */
1062 if (!smp_processor_id())
1063 value = APIC_DM_NMI;
1064 else
1065 value = APIC_DM_NMI | APIC_LVT_MASKED;
1066 if (!integrated) /* 82489DX */
1067 value |= APIC_LVT_LEVEL_TRIGGER;
1068 apic_write_around(APIC_LVT1, value);
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001069}
1070
1071void __cpuinit end_local_APIC_setup(void)
1072{
1073 unsigned long value;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001074
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001075 lapic_setup_esr();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001076 /* Disable the local apic timer */
1077 value = apic_read(APIC_LVTT);
1078 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1079 apic_write_around(APIC_LVTT, value);
1080
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001081 setup_apic_nmi_watchdog(NULL);
1082 apic_pm_activate();
1083}
1084
1085/*
1086 * Detect and initialize APIC
1087 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001088static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001089{
1090 u32 h, l, features;
1091
1092 /* Disabled by kernel option? */
1093 if (enable_local_apic < 0)
1094 return -1;
1095
1096 switch (boot_cpu_data.x86_vendor) {
1097 case X86_VENDOR_AMD:
1098 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1099 (boot_cpu_data.x86 == 15))
1100 break;
1101 goto no_apic;
1102 case X86_VENDOR_INTEL:
1103 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1104 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1105 break;
1106 goto no_apic;
1107 default:
1108 goto no_apic;
1109 }
1110
1111 if (!cpu_has_apic) {
1112 /*
1113 * Over-ride BIOS and try to enable the local APIC only if
1114 * "lapic" specified.
1115 */
1116 if (enable_local_apic <= 0) {
1117 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1118 "you can enable it with \"lapic\"\n");
1119 return -1;
1120 }
1121 /*
1122 * Some BIOSes disable the local APIC in the APIC_BASE
1123 * MSR. This can only be done in software for Intel P6 or later
1124 * and AMD K7 (Model > 1) or later.
1125 */
1126 rdmsr(MSR_IA32_APICBASE, l, h);
1127 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1128 printk(KERN_INFO
1129 "Local APIC disabled by BIOS -- reenabling.\n");
1130 l &= ~MSR_IA32_APICBASE_BASE;
1131 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1132 wrmsr(MSR_IA32_APICBASE, l, h);
1133 enabled_via_apicbase = 1;
1134 }
1135 }
1136 /*
1137 * The APIC feature bit should now be enabled
1138 * in `cpuid'
1139 */
1140 features = cpuid_edx(1);
1141 if (!(features & (1 << X86_FEATURE_APIC))) {
1142 printk(KERN_WARNING "Could not enable APIC!\n");
1143 return -1;
1144 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001145 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001146 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1147
1148 /* The BIOS may have set up the APIC at some other address */
1149 rdmsr(MSR_IA32_APICBASE, l, h);
1150 if (l & MSR_IA32_APICBASE_ENABLE)
1151 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1152
Daniel Gollub0328ece2007-08-15 02:40:35 +02001153 if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001154 nmi_watchdog = NMI_LOCAL_APIC;
1155
1156 printk(KERN_INFO "Found and enabled local APIC!\n");
1157
1158 apic_pm_activate();
1159
1160 return 0;
1161
1162no_apic:
1163 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1164 return -1;
1165}
1166
1167/**
1168 * init_apic_mappings - initialize APIC mappings
1169 */
1170void __init init_apic_mappings(void)
1171{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001172 /*
1173 * If no local APIC can be found then set up a fake all
1174 * zeroes page to simulate the local APIC and another
1175 * one for the IO-APIC.
1176 */
1177 if (!smp_found_config && detect_init_APIC()) {
1178 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1179 apic_phys = __pa(apic_phys);
1180 } else
1181 apic_phys = mp_lapic_addr;
1182
1183 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1184 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1185 apic_phys);
1186
1187 /*
1188 * Fetch the APIC ID of the BSP in case we have a
1189 * default configuration (or the MP table is broken).
1190 */
1191 if (boot_cpu_physical_apicid == -1U)
1192 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1193
1194#ifdef CONFIG_X86_IO_APIC
1195 {
1196 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1197 int i;
1198
1199 for (i = 0; i < nr_ioapics; i++) {
1200 if (smp_found_config) {
1201 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1202 if (!ioapic_phys) {
1203 printk(KERN_ERR
1204 "WARNING: bogus zero IO-APIC "
1205 "address found in MPTABLE, "
1206 "disabling IO/APIC support!\n");
1207 smp_found_config = 0;
1208 skip_ioapic_setup = 1;
1209 goto fake_ioapic_page;
1210 }
1211 } else {
1212fake_ioapic_page:
1213 ioapic_phys = (unsigned long)
1214 alloc_bootmem_pages(PAGE_SIZE);
1215 ioapic_phys = __pa(ioapic_phys);
1216 }
1217 set_fixmap_nocache(idx, ioapic_phys);
1218 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1219 __fix_to_virt(idx), ioapic_phys);
1220 idx++;
1221 }
1222 }
1223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001227 * This initializes the IO-APIC and APIC hardware if this is
1228 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 */
Alexey Starikovskiye81b2c62008-03-27 23:54:31 +03001230
1231int apic_version[MAX_APICS];
1232
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001233int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001235 if (enable_local_apic < 0)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001236 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001237
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001238 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001239 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001242 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001244 if (!cpu_has_apic &&
1245 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001247 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001248 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return -1;
1250 }
1251
1252 verify_local_APIC();
1253
1254 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001255
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001256 /*
1257 * Hack: In case of kdump, after a crash, kernel might be booting
1258 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1259 * might be zero if read from MP tables. Get it from LAPIC.
1260 */
1261#ifdef CONFIG_CRASH_DUMP
1262 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1263#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001264 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 setup_local_APIC();
1267
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001268 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001270 if (smp_found_config)
1271 if (!skip_ioapic_setup && nr_ioapics)
1272 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001274 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001275
1276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001278
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001279/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001280 * Local APIC interrupts
1281 */
1282
1283/*
1284 * This interrupt should _never_ happen with our APIC/SMP architecture
1285 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001286void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001287{
1288 unsigned long v;
1289
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001290 irq_enter();
1291 /*
1292 * Check if this really is a spurious interrupt and ACK it
1293 * if it is a vectored one. Just in case...
1294 * Spurious interrupts should not be ACKed.
1295 */
1296 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1297 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1298 ack_APIC_irq();
1299
1300 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1301 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1302 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001303 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001304 irq_exit();
1305}
1306
1307/*
1308 * This interrupt should never happen with our APIC/SMP architecture
1309 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001310void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001311{
1312 unsigned long v, v1;
1313
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001314 irq_enter();
1315 /* First tickle the hardware, only then report what went on. -- REW */
1316 v = apic_read(APIC_ESR);
1317 apic_write(APIC_ESR, 0);
1318 v1 = apic_read(APIC_ESR);
1319 ack_APIC_irq();
1320 atomic_inc(&irq_err_count);
1321
1322 /* Here is what the APIC error bits mean:
1323 0: Send CS error
1324 1: Receive CS error
1325 2: Send accept error
1326 3: Receive accept error
1327 4: Reserved
1328 5: Send illegal vector
1329 6: Received illegal vector
1330 7: Illegal register address
1331 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001332 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001333 smp_processor_id(), v , v1);
1334 irq_exit();
1335}
1336
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001337#ifdef CONFIG_SMP
1338void __init smp_intr_init(void)
1339{
1340 /*
1341 * IRQ0 must be given a fixed assignment and initialized,
1342 * because it's used before the IO-APIC is set up.
1343 */
1344 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1345
1346 /*
1347 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1348 * IPI, driven by wakeup.
1349 */
1350 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1351
1352 /* IPI for invalidation */
1353 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1354
1355 /* IPI for generic function call */
1356 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1357}
1358#endif
1359
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001360/*
1361 * Initialize APIC interrupts
1362 */
1363void __init apic_intr_init(void)
1364{
1365#ifdef CONFIG_SMP
1366 smp_intr_init();
1367#endif
1368 /* self generated IPI for local APIC timer */
1369 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1370
1371 /* IPI vectors for APIC spurious and error interrupts */
1372 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1373 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1374
1375 /* thermal monitor LVT interrupt */
1376#ifdef CONFIG_X86_MCE_P4THERMAL
1377 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1378#endif
1379}
1380
1381/**
1382 * connect_bsp_APIC - attach the APIC to the interrupt system
1383 */
1384void __init connect_bsp_APIC(void)
1385{
1386 if (pic_mode) {
1387 /*
1388 * Do not trust the local APIC being empty at bootup.
1389 */
1390 clear_local_APIC();
1391 /*
1392 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1393 * local APIC to INT and NMI lines.
1394 */
1395 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1396 "enabling APIC mode.\n");
1397 outb(0x70, 0x22);
1398 outb(0x01, 0x23);
1399 }
1400 enable_apic_mode();
1401}
1402
1403/**
1404 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1405 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1406 *
1407 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1408 * APIC is disabled.
1409 */
1410void disconnect_bsp_APIC(int virt_wire_setup)
1411{
1412 if (pic_mode) {
1413 /*
1414 * Put the board back into PIC mode (has an effect only on
1415 * certain older boards). Note that APIC interrupts, including
1416 * IPIs, won't work beyond this point! The only exception are
1417 * INIT IPIs.
1418 */
1419 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1420 "entering PIC mode.\n");
1421 outb(0x70, 0x22);
1422 outb(0x00, 0x23);
1423 } else {
1424 /* Go back to Virtual Wire compatibility mode */
1425 unsigned long value;
1426
1427 /* For the spurious interrupt use vector F, and enable it */
1428 value = apic_read(APIC_SPIV);
1429 value &= ~APIC_VECTOR_MASK;
1430 value |= APIC_SPIV_APIC_ENABLED;
1431 value |= 0xf;
1432 apic_write_around(APIC_SPIV, value);
1433
1434 if (!virt_wire_setup) {
1435 /*
1436 * For LVT0 make it edge triggered, active high,
1437 * external and enabled
1438 */
1439 value = apic_read(APIC_LVT0);
1440 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1441 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001442 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001443 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1444 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1445 apic_write_around(APIC_LVT0, value);
1446 } else {
1447 /* Disable LVT0 */
1448 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1449 }
1450
1451 /*
1452 * For LVT1 make it edge triggered, active high, nmi and
1453 * enabled
1454 */
1455 value = apic_read(APIC_LVT1);
1456 value &= ~(
1457 APIC_MODE_MASK | APIC_SEND_PENDING |
1458 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1459 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1460 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1461 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1462 apic_write_around(APIC_LVT1, value);
1463 }
1464}
1465
1466/*
1467 * Power management
1468 */
1469#ifdef CONFIG_PM
1470
1471static struct {
1472 int active;
1473 /* r/w apic fields */
1474 unsigned int apic_id;
1475 unsigned int apic_taskpri;
1476 unsigned int apic_ldr;
1477 unsigned int apic_dfr;
1478 unsigned int apic_spiv;
1479 unsigned int apic_lvtt;
1480 unsigned int apic_lvtpc;
1481 unsigned int apic_lvt0;
1482 unsigned int apic_lvt1;
1483 unsigned int apic_lvterr;
1484 unsigned int apic_tmict;
1485 unsigned int apic_tdcr;
1486 unsigned int apic_thmr;
1487} apic_pm_state;
1488
1489static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1490{
1491 unsigned long flags;
1492 int maxlvt;
1493
1494 if (!apic_pm_state.active)
1495 return 0;
1496
1497 maxlvt = lapic_get_maxlvt();
1498
1499 apic_pm_state.apic_id = apic_read(APIC_ID);
1500 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1501 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1502 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1503 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1504 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1505 if (maxlvt >= 4)
1506 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1507 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1508 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1509 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1510 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1511 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1512#ifdef CONFIG_X86_MCE_P4THERMAL
1513 if (maxlvt >= 5)
1514 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1515#endif
1516
1517 local_irq_save(flags);
1518 disable_local_APIC();
1519 local_irq_restore(flags);
1520 return 0;
1521}
1522
1523static int lapic_resume(struct sys_device *dev)
1524{
1525 unsigned int l, h;
1526 unsigned long flags;
1527 int maxlvt;
1528
1529 if (!apic_pm_state.active)
1530 return 0;
1531
1532 maxlvt = lapic_get_maxlvt();
1533
1534 local_irq_save(flags);
1535
1536 /*
1537 * Make sure the APICBASE points to the right address
1538 *
1539 * FIXME! This will be wrong if we ever support suspend on
1540 * SMP! We'll need to do this as part of the CPU restore!
1541 */
1542 rdmsr(MSR_IA32_APICBASE, l, h);
1543 l &= ~MSR_IA32_APICBASE_BASE;
1544 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1545 wrmsr(MSR_IA32_APICBASE, l, h);
1546
1547 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1548 apic_write(APIC_ID, apic_pm_state.apic_id);
1549 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1550 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1551 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1552 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1553 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1554 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1555#ifdef CONFIG_X86_MCE_P4THERMAL
1556 if (maxlvt >= 5)
1557 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1558#endif
1559 if (maxlvt >= 4)
1560 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1561 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1562 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1563 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1564 apic_write(APIC_ESR, 0);
1565 apic_read(APIC_ESR);
1566 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1567 apic_write(APIC_ESR, 0);
1568 apic_read(APIC_ESR);
1569 local_irq_restore(flags);
1570 return 0;
1571}
1572
1573/*
1574 * This device has no shutdown method - fully functioning local APICs
1575 * are needed on every CPU up until machine_halt/restart/poweroff.
1576 */
1577
1578static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001579 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001580 .resume = lapic_resume,
1581 .suspend = lapic_suspend,
1582};
1583
1584static struct sys_device device_lapic = {
1585 .id = 0,
1586 .cls = &lapic_sysclass,
1587};
1588
1589static void __devinit apic_pm_activate(void)
1590{
1591 apic_pm_state.active = 1;
1592}
1593
1594static int __init init_lapic_sysfs(void)
1595{
1596 int error;
1597
1598 if (!cpu_has_apic)
1599 return 0;
1600 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1601
1602 error = sysdev_class_register(&lapic_sysclass);
1603 if (!error)
1604 error = sysdev_register(&device_lapic);
1605 return error;
1606}
1607device_initcall(init_lapic_sysfs);
1608
1609#else /* CONFIG_PM */
1610
1611static void apic_pm_activate(void) { }
1612
1613#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001614
1615/*
1616 * APIC command line parameters
1617 */
1618static int __init parse_lapic(char *arg)
1619{
1620 enable_local_apic = 1;
1621 return 0;
1622}
1623early_param("lapic", parse_lapic);
1624
1625static int __init parse_nolapic(char *arg)
1626{
1627 enable_local_apic = -1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001628 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001629 return 0;
1630}
1631early_param("nolapic", parse_nolapic);
1632
1633static int __init parse_disable_lapic_timer(char *arg)
1634{
1635 local_apic_timer_disabled = 1;
1636 return 0;
1637}
1638early_param("nolapic_timer", parse_disable_lapic_timer);
1639
1640static int __init parse_lapic_timer_c2_ok(char *arg)
1641{
1642 local_apic_timer_c2_ok = 1;
1643 return 0;
1644}
1645early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1646
1647static int __init apic_set_verbosity(char *str)
1648{
1649 if (strcmp("debug", str) == 0)
1650 apic_verbosity = APIC_DEBUG;
1651 else if (strcmp("verbose", str) == 0)
1652 apic_verbosity = APIC_VERBOSE;
1653 return 1;
1654}
1655__setup("apic=", apic_set_verbosity);
1656