blob: 20d4dbc42e6ef2ce54fbdf8d668bf3f0bc9f4460 [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
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800102/*
103 * Get the LAPIC version
104 */
105static inline int lapic_get_version(void)
106{
107 return GET_APIC_VERSION(apic_read(APIC_LVR));
108}
109
110/*
Joe Perchesab4a5742008-01-30 13:31:42 +0100111 * Check, if the APIC is integrated or a separate chip
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800112 */
113static inline int lapic_is_integrated(void)
114{
115 return APIC_INTEGRATED(lapic_get_version());
116}
117
118/*
119 * Check, whether this is a modern or a first generation APIC
120 */
121static int modern_apic(void)
122{
123 /* AMD systems use old APIC versions, so check the CPU */
124 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
125 boot_cpu_data.x86 >= 0xf)
126 return 1;
127 return lapic_get_version() >= 0x14;
128}
129
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200130void apic_wait_icr_idle(void)
131{
132 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
133 cpu_relax();
134}
135
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100136u32 safe_apic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200137{
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100138 u32 send_status;
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200139 int timeout;
140
141 timeout = 0;
142 do {
143 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
144 if (!send_status)
145 break;
146 udelay(100);
147 } while (timeout++ < 1000);
148
149 return send_status;
150}
151
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800152/**
153 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
154 */
Jan Beuliche9427102008-01-30 13:31:24 +0100155void __cpuinit enable_NMI_through_LVT0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800157 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800159 /* Level triggered for 82489DX */
160 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 v |= APIC_LVT_LEVEL_TRIGGER;
162 apic_write_around(APIC_LVT0, v);
163}
164
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800165/**
166 * get_physical_broadcast - Get number of physical broadcast IDs
167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168int get_physical_broadcast(void)
169{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800170 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800173/**
174 * lapic_get_maxlvt - get the maximum number of local vector table entries
175 */
176int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800178 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800181 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800185 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800188/* Clock divisor is set to 16 */
189#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/*
192 * This function sets up the local APIC timer, with a timeout of
193 * 'clocks' APIC bus clock. During calibration we actually call
194 * this function twice on the boot CPU, once with a bogus timeout
195 * value, second time for real. The other (noncalibrating) CPUs
196 * call this function only once, with the real, calibrated value.
197 *
198 * We do reads before writes even if unnecessary, to get around the
199 * P5 APIC double write bug.
200 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800201static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800203 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800205 lvtt_value = LOCAL_TIMER_VECTOR;
206 if (!oneshot)
207 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800208 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100210
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800211 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100212 lvtt_value |= APIC_LVT_MASKED;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 apic_write_around(APIC_LVTT, lvtt_value);
215
216 /*
217 * Divide PICLK by 16
218 */
219 tmp_value = apic_read(APIC_TDCR);
220 apic_write_around(APIC_TDCR, (tmp_value
221 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
222 | APIC_TDR_DIV_16);
223
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800224 if (!oneshot)
225 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800228/*
229 * Program the next event, relative to now
230 */
231static int lapic_next_event(unsigned long delta,
232 struct clock_event_device *evt)
233{
234 apic_write_around(APIC_TMICT, delta);
235 return 0;
236}
237
238/*
239 * Setup the lapic timer in periodic or oneshot mode
240 */
241static void lapic_timer_setup(enum clock_event_mode mode,
242 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800245 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800247 /* Lapic used for broadcast ? */
248 if (!local_apic_timer_verify_ok)
249 return;
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 local_irq_save(flags);
252
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800253 switch (mode) {
254 case CLOCK_EVT_MODE_PERIODIC:
255 case CLOCK_EVT_MODE_ONESHOT:
256 __setup_APIC_LVTT(calibration_result,
257 mode != CLOCK_EVT_MODE_PERIODIC, 1);
258 break;
259 case CLOCK_EVT_MODE_UNUSED:
260 case CLOCK_EVT_MODE_SHUTDOWN:
261 v = apic_read(APIC_LVTT);
262 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
263 apic_write_around(APIC_LVTT, v);
264 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700265 case CLOCK_EVT_MODE_RESUME:
266 /* Nothing to do here */
267 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800268 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 local_irq_restore(flags);
271}
272
273/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800274 * Local APIC timer broadcast function
275 */
276static void lapic_timer_broadcast(cpumask_t mask)
277{
278#ifdef CONFIG_SMP
279 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
280#endif
281}
282
283/*
284 * Setup the local APIC timer for this CPU. Copy the initilized values
285 * of the boot CPU and register the clock event in the framework.
286 */
287static void __devinit setup_APIC_timer(void)
288{
289 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
290
291 memcpy(levt, &lapic_clockevent, sizeof(*levt));
292 levt->cpumask = cpumask_of_cpu(smp_processor_id());
293
294 clockevents_register_device(levt);
295}
296
297/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800298 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800300 * We want to do the calibration only once since we want to have local timer
301 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
302 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800303 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800304 * This was previously done by reading the PIT/HPET and waiting for a wrap
305 * around to find out, that a tick has elapsed. I have a box, where the PIT
306 * readout is broken, so it never gets out of the wait loop again. This was
307 * also reported by others.
308 *
309 * Monitoring the jiffies value is inaccurate and the clockevents
310 * infrastructure allows us to do a simple substitution of the interrupt
311 * handler.
312 *
313 * The calibration routine also uses the pm_timer when possible, as the PIT
314 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
315 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
317
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800318#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200320static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800321static __initdata long lapic_cal_t1, lapic_cal_t2;
322static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
323static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
324static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
325
326/*
327 * Temporary interrupt handler.
328 */
329static void __init lapic_cal_handler(struct clock_event_device *dev)
330{
331 unsigned long long tsc = 0;
332 long tapic = apic_read(APIC_TMCCT);
333 unsigned long pm = acpi_pm_read_early();
334
335 if (cpu_has_tsc)
336 rdtscll(tsc);
337
338 switch (lapic_cal_loops++) {
339 case 0:
340 lapic_cal_t1 = tapic;
341 lapic_cal_tsc1 = tsc;
342 lapic_cal_pm1 = pm;
343 lapic_cal_j1 = jiffies;
344 break;
345
346 case LAPIC_CAL_LOOPS:
347 lapic_cal_t2 = tapic;
348 lapic_cal_tsc2 = tsc;
349 if (pm < lapic_cal_pm1)
350 pm += ACPI_PM_OVRRUN;
351 lapic_cal_pm2 = pm;
352 lapic_cal_j2 = jiffies;
353 break;
354 }
355}
356
357/*
358 * Setup the boot APIC
359 *
360 * Calibrate and verify the result.
361 */
362void __init setup_boot_APIC_clock(void)
363{
364 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
365 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
366 const long pm_thresh = pm_100ms/100;
367 void (*real_handler)(struct clock_event_device *dev);
368 unsigned long deltaj;
369 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800370 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800371
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800372 /*
373 * The local apic timer can be disabled via the kernel
Andi Kleend3f7eae2007-08-10 22:31:07 +0200374 * commandline or from the CPU detection code. Register the lapic
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800375 * timer as a dummy clock event source on SMP systems, so the
376 * broadcast mechanism is used. On UP systems simply ignore it.
377 */
378 if (local_apic_timer_disabled) {
379 /* No broadcast on UP ! */
380 if (num_possible_cpus() > 1)
381 setup_APIC_timer();
382 return;
383 }
384
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800385 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
386 "calibrating APIC timer ...\n");
387
388 local_irq_disable();
389
390 /* Replace the global interrupt handler */
391 real_handler = global_clock_event->event_handler;
392 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800395 * Setup the APIC counter to 1e9. There is no way the lapic
396 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800398 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800400 /* Let the interrupts run */
401 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800403 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
404 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800406 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800408 /* Restore the real event handler */
409 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800411 /* Build delta t1-t2 as apic timer counts down */
412 delta = lapic_cal_t1 - lapic_cal_t2;
413 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800415 /* Check, if the PM timer is available */
416 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
417 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800419 if (deltapm) {
420 unsigned long mult;
421 u64 res;
422
423 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
424
425 if (deltapm > (pm_100ms - pm_thresh) &&
426 deltapm < (pm_100ms + pm_thresh)) {
427 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
428 } else {
429 res = (((u64) deltapm) * mult) >> 22;
430 do_div(res, 1000000);
431 printk(KERN_WARNING "APIC calibration not consistent "
432 "with PM Timer: %ldms instead of 100ms\n",
433 (long)res);
434 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100435 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800436 do_div(res, deltapm);
437 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
438 "%lu (%ld)\n", (unsigned long) res, delta);
439 delta = (long) res;
440 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800441 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800444 /* Calculate the scaled math multiplication factor */
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800445 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800446 lapic_clockevent.max_delta_ns =
447 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
448 lapic_clockevent.min_delta_ns =
449 clockevent_delta2ns(0xF, &lapic_clockevent);
450
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800451 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800452
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800453 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
454 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
455 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
456 calibration_result);
457
458 if (cpu_has_tsc) {
459 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800461 "%ld.%04ld MHz.\n",
462 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
463 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800467 "%u.%04u MHz.\n",
468 calibration_result / (1000000 / HZ),
469 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800471 local_apic_timer_verify_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100473 /*
474 * Do a sanity check on the APIC calibration result
475 */
476 if (calibration_result < (1000000 / HZ)) {
477 local_irq_enable();
478 printk(KERN_WARNING
479 "APIC frequency too slow, disabling apic timer\n");
480 /* No broadcast on UP ! */
481 if (num_possible_cpus() > 1)
482 setup_APIC_timer();
483 return;
484 }
485
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800486 /* We trust the pm timer based calibration */
487 if (!pm_referenced) {
488 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800489
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800490 /*
491 * Setup the apic timer manually
492 */
493 levt->event_handler = lapic_cal_handler;
494 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
495 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800496
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800497 /* Let the interrupts run */
498 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800499
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200500 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800501 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800502
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800503 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800504
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800505 /* Stop the lapic timer */
506 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800507
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800508 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800509
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800510 /* Jiffies delta */
511 deltaj = lapic_cal_j2 - lapic_cal_j1;
512 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800513
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800514 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800515 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800516 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800517 else
518 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100519 } else
520 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800521
522 if (!local_apic_timer_verify_ok) {
523 printk(KERN_WARNING
524 "APIC timer disabled due to verification failure.\n");
525 /* No broadcast on UP ! */
526 if (num_possible_cpus() == 1)
527 return;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800528 } else {
529 /*
530 * If nmi_watchdog is set to IO_APIC, we need the
531 * PIT/HPET going. Otherwise register lapic as a dummy
532 * device.
533 */
534 if (nmi_watchdog != NMI_IO_APIC)
535 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
Ingo Molnar820de5c2007-07-21 04:37:36 -0700536 else
537 printk(KERN_WARNING "APIC timer registered as dummy,"
538 " due to nmi_watchdog=1!\n");
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800539 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800540
541 /* Setup the lapic or request the broadcast */
542 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Li Shaohua0bb31842005-06-25 14:54:55 -0700545void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800547 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800551 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800553static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800555 int cpu = smp_processor_id();
556 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800559 * Normally we should not be here till LAPIC has been initialized but
560 * in some cases like kdump, its possible that there is a pending LAPIC
561 * timer interrupt from previous kernel's context and is delivered in
562 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800564 * Interrupts are enabled early and LAPIC is setup much later, hence
565 * its possible that when we get here evt->event_handler is NULL.
566 * Check for event_handler being NULL and discard the interrupt as
567 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800569 if (!evt->event_handler) {
570 printk(KERN_WARNING
571 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
572 /* Switch it off */
573 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
574 return;
575 }
576
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100577 /*
578 * the NMI deadlock-detector uses this.
579 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800580 per_cpu(irq_stat, cpu).apic_timer_irqs++;
581
582 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
585/*
586 * Local APIC timer interrupt. This is the most natural way for doing
587 * local interrupts, but local timer interrupts can be emulated by
588 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
589 *
590 * [ if a single-CPU system runs an SMP kernel then we call the local
591 * interrupt as well. Thus we cannot inline the local irq ... ]
592 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100593void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
David Howells7d12e782006-10-05 14:55:46 +0100595 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /*
598 * NOTE! We'd better ACK the irq immediately,
599 * because timer handling can be slow.
600 */
601 ack_APIC_irq();
602 /*
603 * update_process_times() expects us to have done irq_enter().
604 * Besides, if we don't timer interrupts ignore the global
605 * interrupt lock, which is the WrongThing (tm) to do.
606 */
607 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800608 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800610
David Howells7d12e782006-10-05 14:55:46 +0100611 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100614int setup_profiling_timer(unsigned int multiplier)
615{
616 return -EINVAL;
617}
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800620 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800622
623/**
624 * clear_local_APIC - shutdown the local APIC
625 *
626 * This is called, when a CPU is disabled and before rebooting, so the state of
627 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
628 * leftovers during boot.
629 */
630void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800632 int maxlvt = lapic_get_maxlvt();
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100633 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800636 * Masking an LVT entry can trigger a local APIC error
637 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800639 if (maxlvt >= 3) {
640 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
641 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
642 }
643 /*
644 * Careful: we have to set masks only first to deassert
645 * any level-triggered sources.
646 */
647 v = apic_read(APIC_LVTT);
648 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
649 v = apic_read(APIC_LVT0);
650 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
651 v = apic_read(APIC_LVT1);
652 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
653 if (maxlvt >= 4) {
654 v = apic_read(APIC_LVTPC);
655 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800658 /* lets not touch this if we didn't frob it */
659#ifdef CONFIG_X86_MCE_P4THERMAL
660 if (maxlvt >= 5) {
661 v = apic_read(APIC_LVTTHMR);
662 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
663 }
664#endif
665 /*
666 * Clean APIC state for other OSs:
667 */
668 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
669 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
670 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
671 if (maxlvt >= 3)
672 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
673 if (maxlvt >= 4)
674 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
675
676#ifdef CONFIG_X86_MCE_P4THERMAL
677 if (maxlvt >= 5)
678 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
679#endif
680 /* Integrated APIC (!82489DX) ? */
681 if (lapic_is_integrated()) {
682 if (maxlvt > 3)
683 /* Clear ESR due to Pentium errata 3AP and 11AP */
684 apic_write(APIC_ESR, 0);
685 apic_read(APIC_ESR);
686 }
687}
688
689/**
690 * disable_local_APIC - clear and disable the local APIC
691 */
692void disable_local_APIC(void)
693{
694 unsigned long value;
695
696 clear_local_APIC();
697
698 /*
699 * Disable APIC (implies clearing of registers
700 * for 82489DX!).
701 */
702 value = apic_read(APIC_SPIV);
703 value &= ~APIC_SPIV_APIC_ENABLED;
704 apic_write_around(APIC_SPIV, value);
705
706 /*
707 * When LAPIC was disabled by the BIOS and enabled by the kernel,
708 * restore the disabled state.
709 */
710 if (enabled_via_apicbase) {
711 unsigned int l, h;
712
713 rdmsr(MSR_IA32_APICBASE, l, h);
714 l &= ~MSR_IA32_APICBASE_ENABLE;
715 wrmsr(MSR_IA32_APICBASE, l, h);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
719/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800720 * If Linux enabled the LAPIC against the BIOS default disable it down before
721 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
722 * not power-off. Additionally clear all LVT entries before disable_local_APIC
723 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800725void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800727 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800729 if (!cpu_has_apic)
730 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800732 local_irq_save(flags);
733 clear_local_APIC();
734
735 if (enabled_via_apicbase)
736 disable_local_APIC();
737
738 local_irq_restore(flags);
739}
740
741/*
742 * This is to verify that we're looking at a real local APIC.
743 * Check these against your board if the CPUs aren't getting
744 * started for no apparent reason.
745 */
746int __init verify_local_APIC(void)
747{
748 unsigned int reg0, reg1;
749
750 /*
751 * The version register is read-only in a real APIC.
752 */
753 reg0 = apic_read(APIC_LVR);
754 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
755 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
756 reg1 = apic_read(APIC_LVR);
757 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
758
759 /*
760 * The two version reads above should print the same
761 * numbers. If the second one is different, then we
762 * poke at a non-APIC.
763 */
764 if (reg1 != reg0)
765 return 0;
766
767 /*
768 * Check if the version looks reasonably.
769 */
770 reg1 = GET_APIC_VERSION(reg0);
771 if (reg1 == 0x00 || reg1 == 0xff)
772 return 0;
773 reg1 = lapic_get_maxlvt();
774 if (reg1 < 0x02 || reg1 == 0xff)
775 return 0;
776
777 /*
778 * The ID register is read/write in a real APIC.
779 */
780 reg0 = apic_read(APIC_ID);
781 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
782
783 /*
784 * The next two are just to see if we have sane values.
785 * They're only really relevant if we're in Virtual Wire
786 * compatibility mode, but most boxes are anymore.
787 */
788 reg0 = apic_read(APIC_LVT0);
789 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
790 reg1 = apic_read(APIC_LVT1);
791 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
792
793 return 1;
794}
795
796/**
797 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
798 */
799void __init sync_Arb_IDs(void)
800{
801 /*
802 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
803 * needed on AMD.
804 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100805 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800806 return;
807 /*
808 * Wait for idle.
809 */
810 apic_wait_icr_idle();
811
812 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
813 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
814 | APIC_DM_INIT);
815}
816
817/*
818 * An initial setup of the virtual wire mode.
819 */
820void __init init_bsp_APIC(void)
821{
822 unsigned long value;
823
824 /*
825 * Don't do the setup now if we have a SMP BIOS as the
826 * through-I/O-APIC virtual wire mode might be active.
827 */
828 if (smp_found_config || !cpu_has_apic)
829 return;
830
831 /*
832 * Do not trust the local APIC being empty at bootup.
833 */
834 clear_local_APIC();
835
836 /*
837 * Enable APIC.
838 */
839 value = apic_read(APIC_SPIV);
840 value &= ~APIC_VECTOR_MASK;
841 value |= APIC_SPIV_APIC_ENABLED;
842
843 /* This bit is reserved on P4/Xeon and should be cleared */
844 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
845 (boot_cpu_data.x86 == 15))
846 value &= ~APIC_SPIV_FOCUS_DISABLED;
847 else
848 value |= APIC_SPIV_FOCUS_DISABLED;
849 value |= SPURIOUS_APIC_VECTOR;
850 apic_write_around(APIC_SPIV, value);
851
852 /*
853 * Set up the virtual wire mode.
854 */
855 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
856 value = APIC_DM_NMI;
857 if (!lapic_is_integrated()) /* 82489DX */
858 value |= APIC_LVT_LEVEL_TRIGGER;
859 apic_write_around(APIC_LVT1, value);
860}
861
862/**
863 * setup_local_APIC - setup the local APIC
864 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100865void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800866{
867 unsigned long oldvalue, value, maxlvt, integrated;
868 int i, j;
869
870 /* Pound the ESR really hard over the head with a big hammer - mbligh */
871 if (esr_disable) {
872 apic_write(APIC_ESR, 0);
873 apic_write(APIC_ESR, 0);
874 apic_write(APIC_ESR, 0);
875 apic_write(APIC_ESR, 0);
876 }
877
878 integrated = lapic_is_integrated();
879
880 /*
881 * Double-check whether this APIC is really registered.
882 */
883 if (!apic_id_registered())
884 BUG();
885
886 /*
887 * Intel recommends to set DFR, LDR and TPR before enabling
888 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
889 * document number 292116). So here it goes...
890 */
891 init_apic_ldr();
892
893 /*
894 * Set Task Priority to 'accept all'. We never change this
895 * later on.
896 */
897 value = apic_read(APIC_TASKPRI);
898 value &= ~APIC_TPRI_MASK;
899 apic_write_around(APIC_TASKPRI, value);
900
901 /*
902 * After a crash, we no longer service the interrupts and a pending
903 * interrupt from previous kernel might still have ISR bit set.
904 *
905 * Most probably by now CPU has serviced that pending interrupt and
906 * it might not have done the ack_APIC_irq() because it thought,
907 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
908 * does not clear the ISR bit and cpu thinks it has already serivced
909 * the interrupt. Hence a vector might get locked. It was noticed
910 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
911 */
912 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
913 value = apic_read(APIC_ISR + i*0x10);
914 for (j = 31; j >= 0; j--) {
915 if (value & (1<<j))
916 ack_APIC_irq();
917 }
918 }
919
920 /*
921 * Now that we are all set up, enable the APIC
922 */
923 value = apic_read(APIC_SPIV);
924 value &= ~APIC_VECTOR_MASK;
925 /*
926 * Enable APIC
927 */
928 value |= APIC_SPIV_APIC_ENABLED;
929
930 /*
931 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
932 * certain networking cards. If high frequency interrupts are
933 * happening on a particular IOAPIC pin, plus the IOAPIC routing
934 * entry is masked/unmasked at a high rate as well then sooner or
935 * later IOAPIC line gets 'stuck', no more interrupts are received
936 * from the device. If focus CPU is disabled then the hang goes
937 * away, oh well :-(
938 *
939 * [ This bug can be reproduced easily with a level-triggered
940 * PCI Ne2000 networking cards and PII/PIII processors, dual
941 * BX chipset. ]
942 */
943 /*
944 * Actually disabling the focus CPU check just makes the hang less
945 * frequent as it makes the interrupt distributon model be more
946 * like LRU than MRU (the short-term load is more even across CPUs).
947 * See also the comment in end_level_ioapic_irq(). --macro
948 */
949
950 /* Enable focus processor (bit==0) */
951 value &= ~APIC_SPIV_FOCUS_DISABLED;
952
953 /*
954 * Set spurious IRQ vector
955 */
956 value |= SPURIOUS_APIC_VECTOR;
957 apic_write_around(APIC_SPIV, value);
958
959 /*
960 * Set up LVT0, LVT1:
961 *
962 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +0200963 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800964 * we delegate interrupts to the 8259A.
965 */
966 /*
967 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
968 */
969 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
970 if (!smp_processor_id() && (pic_mode || !value)) {
971 value = APIC_DM_EXTINT;
972 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
973 smp_processor_id());
974 } else {
975 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
976 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
977 smp_processor_id());
978 }
979 apic_write_around(APIC_LVT0, value);
980
981 /*
982 * only the BP should see the LINT1 NMI signal, obviously.
983 */
984 if (!smp_processor_id())
985 value = APIC_DM_NMI;
986 else
987 value = APIC_DM_NMI | APIC_LVT_MASKED;
988 if (!integrated) /* 82489DX */
989 value |= APIC_LVT_LEVEL_TRIGGER;
990 apic_write_around(APIC_LVT1, value);
991
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100992 if (integrated && !esr_disable) {
993 /* !82489DX */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800994 maxlvt = lapic_get_maxlvt();
995 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
996 apic_write(APIC_ESR, 0);
997 oldvalue = apic_read(APIC_ESR);
998
999 /* enables sending errors */
1000 value = ERROR_APIC_VECTOR;
1001 apic_write_around(APIC_LVTERR, value);
1002 /*
1003 * spec says clear errors after enabling vector.
1004 */
1005 if (maxlvt > 3)
1006 apic_write(APIC_ESR, 0);
1007 value = apic_read(APIC_ESR);
1008 if (value != oldvalue)
1009 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1010 "vector: 0x%08lx after: 0x%08lx\n",
1011 oldvalue, value);
1012 } else {
1013 if (esr_disable)
1014 /*
Simon Arlott27b46d72007-10-20 01:13:56 +02001015 * Something untraceable is creating bad interrupts on
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001016 * secondary quads ... for the moment, just leave the
1017 * ESR disabled - we can't do anything useful with the
1018 * errors anyway - mbligh
1019 */
1020 printk(KERN_INFO "Leaving ESR disabled.\n");
1021 else
1022 printk(KERN_INFO "No ESR for 82489DX.\n");
1023 }
1024
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001025 /* Disable the local apic timer */
1026 value = apic_read(APIC_LVTT);
1027 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1028 apic_write_around(APIC_LVTT, value);
1029
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001030 setup_apic_nmi_watchdog(NULL);
1031 apic_pm_activate();
1032}
1033
1034/*
1035 * Detect and initialize APIC
1036 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001037static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001038{
1039 u32 h, l, features;
1040
1041 /* Disabled by kernel option? */
1042 if (enable_local_apic < 0)
1043 return -1;
1044
1045 switch (boot_cpu_data.x86_vendor) {
1046 case X86_VENDOR_AMD:
1047 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1048 (boot_cpu_data.x86 == 15))
1049 break;
1050 goto no_apic;
1051 case X86_VENDOR_INTEL:
1052 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1053 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1054 break;
1055 goto no_apic;
1056 default:
1057 goto no_apic;
1058 }
1059
1060 if (!cpu_has_apic) {
1061 /*
1062 * Over-ride BIOS and try to enable the local APIC only if
1063 * "lapic" specified.
1064 */
1065 if (enable_local_apic <= 0) {
1066 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1067 "you can enable it with \"lapic\"\n");
1068 return -1;
1069 }
1070 /*
1071 * Some BIOSes disable the local APIC in the APIC_BASE
1072 * MSR. This can only be done in software for Intel P6 or later
1073 * and AMD K7 (Model > 1) or later.
1074 */
1075 rdmsr(MSR_IA32_APICBASE, l, h);
1076 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1077 printk(KERN_INFO
1078 "Local APIC disabled by BIOS -- reenabling.\n");
1079 l &= ~MSR_IA32_APICBASE_BASE;
1080 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1081 wrmsr(MSR_IA32_APICBASE, l, h);
1082 enabled_via_apicbase = 1;
1083 }
1084 }
1085 /*
1086 * The APIC feature bit should now be enabled
1087 * in `cpuid'
1088 */
1089 features = cpuid_edx(1);
1090 if (!(features & (1 << X86_FEATURE_APIC))) {
1091 printk(KERN_WARNING "Could not enable APIC!\n");
1092 return -1;
1093 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001094 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001095 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1096
1097 /* The BIOS may have set up the APIC at some other address */
1098 rdmsr(MSR_IA32_APICBASE, l, h);
1099 if (l & MSR_IA32_APICBASE_ENABLE)
1100 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1101
Daniel Gollub0328ece2007-08-15 02:40:35 +02001102 if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001103 nmi_watchdog = NMI_LOCAL_APIC;
1104
1105 printk(KERN_INFO "Found and enabled local APIC!\n");
1106
1107 apic_pm_activate();
1108
1109 return 0;
1110
1111no_apic:
1112 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1113 return -1;
1114}
1115
1116/**
1117 * init_apic_mappings - initialize APIC mappings
1118 */
1119void __init init_apic_mappings(void)
1120{
1121 unsigned long apic_phys;
1122
1123 /*
1124 * If no local APIC can be found then set up a fake all
1125 * zeroes page to simulate the local APIC and another
1126 * one for the IO-APIC.
1127 */
1128 if (!smp_found_config && detect_init_APIC()) {
1129 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1130 apic_phys = __pa(apic_phys);
1131 } else
1132 apic_phys = mp_lapic_addr;
1133
1134 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1135 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1136 apic_phys);
1137
1138 /*
1139 * Fetch the APIC ID of the BSP in case we have a
1140 * default configuration (or the MP table is broken).
1141 */
1142 if (boot_cpu_physical_apicid == -1U)
1143 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1144
1145#ifdef CONFIG_X86_IO_APIC
1146 {
1147 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1148 int i;
1149
1150 for (i = 0; i < nr_ioapics; i++) {
1151 if (smp_found_config) {
1152 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1153 if (!ioapic_phys) {
1154 printk(KERN_ERR
1155 "WARNING: bogus zero IO-APIC "
1156 "address found in MPTABLE, "
1157 "disabling IO/APIC support!\n");
1158 smp_found_config = 0;
1159 skip_ioapic_setup = 1;
1160 goto fake_ioapic_page;
1161 }
1162 } else {
1163fake_ioapic_page:
1164 ioapic_phys = (unsigned long)
1165 alloc_bootmem_pages(PAGE_SIZE);
1166 ioapic_phys = __pa(ioapic_phys);
1167 }
1168 set_fixmap_nocache(idx, ioapic_phys);
1169 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1170 __fix_to_virt(idx), ioapic_phys);
1171 idx++;
1172 }
1173 }
1174#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001178 * This initializes the IO-APIC and APIC hardware if this is
1179 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001181int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001183 if (enable_local_apic < 0)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001184 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001185
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001186 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001187 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001190 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001192 if (!cpu_has_apic &&
1193 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001195 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001196 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 return -1;
1198 }
1199
1200 verify_local_APIC();
1201
1202 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001203
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001204 /*
1205 * Hack: In case of kdump, after a crash, kernel might be booting
1206 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1207 * might be zero if read from MP tables. Get it from LAPIC.
1208 */
1209#ifdef CONFIG_CRASH_DUMP
1210 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1211#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001212 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1213
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 setup_local_APIC();
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001217 if (smp_found_config)
1218 if (!skip_ioapic_setup && nr_ioapics)
1219 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001221 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001222
1223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001225
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001226/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001227 * Local APIC interrupts
1228 */
1229
1230/*
1231 * This interrupt should _never_ happen with our APIC/SMP architecture
1232 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001233void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001234{
1235 unsigned long v;
1236
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001237 irq_enter();
1238 /*
1239 * Check if this really is a spurious interrupt and ACK it
1240 * if it is a vectored one. Just in case...
1241 * Spurious interrupts should not be ACKed.
1242 */
1243 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1244 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1245 ack_APIC_irq();
1246
1247 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1248 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1249 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001250 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001251 irq_exit();
1252}
1253
1254/*
1255 * This interrupt should never happen with our APIC/SMP architecture
1256 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001257void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001258{
1259 unsigned long v, v1;
1260
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001261 irq_enter();
1262 /* First tickle the hardware, only then report what went on. -- REW */
1263 v = apic_read(APIC_ESR);
1264 apic_write(APIC_ESR, 0);
1265 v1 = apic_read(APIC_ESR);
1266 ack_APIC_irq();
1267 atomic_inc(&irq_err_count);
1268
1269 /* Here is what the APIC error bits mean:
1270 0: Send CS error
1271 1: Receive CS error
1272 2: Send accept error
1273 3: Receive accept error
1274 4: Reserved
1275 5: Send illegal vector
1276 6: Received illegal vector
1277 7: Illegal register address
1278 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001279 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001280 smp_processor_id(), v , v1);
1281 irq_exit();
1282}
1283
1284/*
1285 * Initialize APIC interrupts
1286 */
1287void __init apic_intr_init(void)
1288{
1289#ifdef CONFIG_SMP
1290 smp_intr_init();
1291#endif
1292 /* self generated IPI for local APIC timer */
1293 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1294
1295 /* IPI vectors for APIC spurious and error interrupts */
1296 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1297 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1298
1299 /* thermal monitor LVT interrupt */
1300#ifdef CONFIG_X86_MCE_P4THERMAL
1301 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1302#endif
1303}
1304
1305/**
1306 * connect_bsp_APIC - attach the APIC to the interrupt system
1307 */
1308void __init connect_bsp_APIC(void)
1309{
1310 if (pic_mode) {
1311 /*
1312 * Do not trust the local APIC being empty at bootup.
1313 */
1314 clear_local_APIC();
1315 /*
1316 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1317 * local APIC to INT and NMI lines.
1318 */
1319 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1320 "enabling APIC mode.\n");
1321 outb(0x70, 0x22);
1322 outb(0x01, 0x23);
1323 }
1324 enable_apic_mode();
1325}
1326
1327/**
1328 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1329 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1330 *
1331 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1332 * APIC is disabled.
1333 */
1334void disconnect_bsp_APIC(int virt_wire_setup)
1335{
1336 if (pic_mode) {
1337 /*
1338 * Put the board back into PIC mode (has an effect only on
1339 * certain older boards). Note that APIC interrupts, including
1340 * IPIs, won't work beyond this point! The only exception are
1341 * INIT IPIs.
1342 */
1343 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1344 "entering PIC mode.\n");
1345 outb(0x70, 0x22);
1346 outb(0x00, 0x23);
1347 } else {
1348 /* Go back to Virtual Wire compatibility mode */
1349 unsigned long value;
1350
1351 /* For the spurious interrupt use vector F, and enable it */
1352 value = apic_read(APIC_SPIV);
1353 value &= ~APIC_VECTOR_MASK;
1354 value |= APIC_SPIV_APIC_ENABLED;
1355 value |= 0xf;
1356 apic_write_around(APIC_SPIV, value);
1357
1358 if (!virt_wire_setup) {
1359 /*
1360 * For LVT0 make it edge triggered, active high,
1361 * external and enabled
1362 */
1363 value = apic_read(APIC_LVT0);
1364 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1365 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001366 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001367 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1368 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1369 apic_write_around(APIC_LVT0, value);
1370 } else {
1371 /* Disable LVT0 */
1372 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1373 }
1374
1375 /*
1376 * For LVT1 make it edge triggered, active high, nmi and
1377 * enabled
1378 */
1379 value = apic_read(APIC_LVT1);
1380 value &= ~(
1381 APIC_MODE_MASK | APIC_SEND_PENDING |
1382 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1383 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1384 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1385 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1386 apic_write_around(APIC_LVT1, value);
1387 }
1388}
1389
1390/*
1391 * Power management
1392 */
1393#ifdef CONFIG_PM
1394
1395static struct {
1396 int active;
1397 /* r/w apic fields */
1398 unsigned int apic_id;
1399 unsigned int apic_taskpri;
1400 unsigned int apic_ldr;
1401 unsigned int apic_dfr;
1402 unsigned int apic_spiv;
1403 unsigned int apic_lvtt;
1404 unsigned int apic_lvtpc;
1405 unsigned int apic_lvt0;
1406 unsigned int apic_lvt1;
1407 unsigned int apic_lvterr;
1408 unsigned int apic_tmict;
1409 unsigned int apic_tdcr;
1410 unsigned int apic_thmr;
1411} apic_pm_state;
1412
1413static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1414{
1415 unsigned long flags;
1416 int maxlvt;
1417
1418 if (!apic_pm_state.active)
1419 return 0;
1420
1421 maxlvt = lapic_get_maxlvt();
1422
1423 apic_pm_state.apic_id = apic_read(APIC_ID);
1424 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1425 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1426 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1427 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1428 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1429 if (maxlvt >= 4)
1430 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1431 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1432 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1433 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1434 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1435 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1436#ifdef CONFIG_X86_MCE_P4THERMAL
1437 if (maxlvt >= 5)
1438 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1439#endif
1440
1441 local_irq_save(flags);
1442 disable_local_APIC();
1443 local_irq_restore(flags);
1444 return 0;
1445}
1446
1447static int lapic_resume(struct sys_device *dev)
1448{
1449 unsigned int l, h;
1450 unsigned long flags;
1451 int maxlvt;
1452
1453 if (!apic_pm_state.active)
1454 return 0;
1455
1456 maxlvt = lapic_get_maxlvt();
1457
1458 local_irq_save(flags);
1459
1460 /*
1461 * Make sure the APICBASE points to the right address
1462 *
1463 * FIXME! This will be wrong if we ever support suspend on
1464 * SMP! We'll need to do this as part of the CPU restore!
1465 */
1466 rdmsr(MSR_IA32_APICBASE, l, h);
1467 l &= ~MSR_IA32_APICBASE_BASE;
1468 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1469 wrmsr(MSR_IA32_APICBASE, l, h);
1470
1471 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1472 apic_write(APIC_ID, apic_pm_state.apic_id);
1473 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1474 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1475 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1476 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1477 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1478 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1479#ifdef CONFIG_X86_MCE_P4THERMAL
1480 if (maxlvt >= 5)
1481 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1482#endif
1483 if (maxlvt >= 4)
1484 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1485 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1486 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1487 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1488 apic_write(APIC_ESR, 0);
1489 apic_read(APIC_ESR);
1490 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1491 apic_write(APIC_ESR, 0);
1492 apic_read(APIC_ESR);
1493 local_irq_restore(flags);
1494 return 0;
1495}
1496
1497/*
1498 * This device has no shutdown method - fully functioning local APICs
1499 * are needed on every CPU up until machine_halt/restart/poweroff.
1500 */
1501
1502static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001503 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001504 .resume = lapic_resume,
1505 .suspend = lapic_suspend,
1506};
1507
1508static struct sys_device device_lapic = {
1509 .id = 0,
1510 .cls = &lapic_sysclass,
1511};
1512
1513static void __devinit apic_pm_activate(void)
1514{
1515 apic_pm_state.active = 1;
1516}
1517
1518static int __init init_lapic_sysfs(void)
1519{
1520 int error;
1521
1522 if (!cpu_has_apic)
1523 return 0;
1524 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1525
1526 error = sysdev_class_register(&lapic_sysclass);
1527 if (!error)
1528 error = sysdev_register(&device_lapic);
1529 return error;
1530}
1531device_initcall(init_lapic_sysfs);
1532
1533#else /* CONFIG_PM */
1534
1535static void apic_pm_activate(void) { }
1536
1537#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001538
1539/*
1540 * APIC command line parameters
1541 */
1542static int __init parse_lapic(char *arg)
1543{
1544 enable_local_apic = 1;
1545 return 0;
1546}
1547early_param("lapic", parse_lapic);
1548
1549static int __init parse_nolapic(char *arg)
1550{
1551 enable_local_apic = -1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001552 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001553 return 0;
1554}
1555early_param("nolapic", parse_nolapic);
1556
1557static int __init parse_disable_lapic_timer(char *arg)
1558{
1559 local_apic_timer_disabled = 1;
1560 return 0;
1561}
1562early_param("nolapic_timer", parse_disable_lapic_timer);
1563
1564static int __init parse_lapic_timer_c2_ok(char *arg)
1565{
1566 local_apic_timer_c2_ok = 1;
1567 return 0;
1568}
1569early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1570
1571static int __init apic_set_verbosity(char *str)
1572{
1573 if (strcmp("debug", str) == 0)
1574 apic_verbosity = APIC_DEBUG;
1575 else if (strcmp("verbose", str) == 0)
1576 apic_verbosity = APIC_VERBOSE;
1577 return 1;
1578}
1579__setup("apic=", apic_set_verbosity);
1580