blob: 848c603457feb4649d5bd31166c45d0b10bef43f [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
Alexey Starikovskiyacff5a72008-03-27 23:55:16 +030055DEFINE_PER_CPU(u16, x86_bios_cpu_apicid) = BAD_APICID;
56EXPORT_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
57
Thomas Gleixnere05d7232007-02-16 01:27:58 -080058/*
Eric W. Biederman9635b472005-06-25 14:57:41 -070059 * Knob to control our willingness to enable the local APIC.
Thomas Gleixnere05d7232007-02-16 01:27:58 -080060 *
61 * -1=force-disable, +1=force-enable
Eric W. Biederman9635b472005-06-25 14:57:41 -070062 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +010063static int enable_local_apic __initdata;
Eric W. Biederman9635b472005-06-25 14:57:41 -070064
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080065/* Local APIC timer verification ok */
66static int local_apic_timer_verify_ok;
Andi Kleend3f7eae2007-08-10 22:31:07 +020067/* Disable local APIC timer from the kernel commandline or via dmi quirk
68 or using CPU MSR check */
69int local_apic_timer_disabled;
Thomas Gleixnere585bef2007-03-23 16:08:01 +010070/* Local APIC timer works in C2 */
71int local_apic_timer_c2_ok;
72EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080073
Eric W. Biederman9635b472005-06-25 14:57:41 -070074/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080075 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 */
77int apic_verbosity;
78
Alexey Starikovskiyf3918352008-05-23 01:54:51 +040079int pic_mode;
80
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080081static unsigned int calibration_result;
82
83static int lapic_next_event(unsigned long delta,
84 struct clock_event_device *evt);
85static void lapic_timer_setup(enum clock_event_mode mode,
86 struct clock_event_device *evt);
87static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static void apic_pm_activate(void);
89
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080090/*
91 * The local apic timer can be used for any function which is CPU local.
92 */
93static struct clock_event_device lapic_clockevent = {
94 .name = "lapic",
95 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080096 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080097 .shift = 32,
98 .set_mode = lapic_timer_setup,
99 .set_next_event = lapic_next_event,
100 .broadcast = lapic_timer_broadcast,
101 .rating = 100,
102 .irq = -1,
103};
104static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800106/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static int enabled_via_apicbase;
108
Andi Kleend3432892008-01-30 13:33:17 +0100109static unsigned long apic_phys;
110
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800111/*
112 * Get the LAPIC version
113 */
114static inline int lapic_get_version(void)
115{
116 return GET_APIC_VERSION(apic_read(APIC_LVR));
117}
118
119/*
Joe Perchesab4a5742008-01-30 13:31:42 +0100120 * Check, if the APIC is integrated or a separate chip
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800121 */
122static inline int lapic_is_integrated(void)
123{
124 return APIC_INTEGRATED(lapic_get_version());
125}
126
127/*
128 * Check, whether this is a modern or a first generation APIC
129 */
130static int modern_apic(void)
131{
132 /* AMD systems use old APIC versions, so check the CPU */
133 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
134 boot_cpu_data.x86 >= 0xf)
135 return 1;
136 return lapic_get_version() >= 0x14;
137}
138
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200139void apic_wait_icr_idle(void)
140{
141 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
142 cpu_relax();
143}
144
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100145u32 safe_apic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200146{
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100147 u32 send_status;
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200148 int timeout;
149
150 timeout = 0;
151 do {
152 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
153 if (!send_status)
154 break;
155 udelay(100);
156 } while (timeout++ < 1000);
157
158 return send_status;
159}
160
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800161/**
162 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
163 */
Jan Beuliche9427102008-01-30 13:31:24 +0100164void __cpuinit enable_NMI_through_LVT0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800166 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800168 /* Level triggered for 82489DX */
169 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 v |= APIC_LVT_LEVEL_TRIGGER;
171 apic_write_around(APIC_LVT0, v);
172}
173
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800174/**
175 * get_physical_broadcast - Get number of physical broadcast IDs
176 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177int get_physical_broadcast(void)
178{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800179 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800182/**
183 * lapic_get_maxlvt - get the maximum number of local vector table entries
184 */
185int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800187 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800190 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800194 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800197/* Clock divisor is set to 16 */
198#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/*
201 * This function sets up the local APIC timer, with a timeout of
202 * 'clocks' APIC bus clock. During calibration we actually call
203 * this function twice on the boot CPU, once with a bogus timeout
204 * value, second time for real. The other (noncalibrating) CPUs
205 * call this function only once, with the real, calibrated value.
206 *
207 * We do reads before writes even if unnecessary, to get around the
208 * P5 APIC double write bug.
209 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800210static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800212 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800214 lvtt_value = LOCAL_TIMER_VECTOR;
215 if (!oneshot)
216 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800217 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100219
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800220 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100221 lvtt_value |= APIC_LVT_MASKED;
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 apic_write_around(APIC_LVTT, lvtt_value);
224
225 /*
226 * Divide PICLK by 16
227 */
228 tmp_value = apic_read(APIC_TDCR);
229 apic_write_around(APIC_TDCR, (tmp_value
230 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
231 | APIC_TDR_DIV_16);
232
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800233 if (!oneshot)
234 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800237/*
238 * Program the next event, relative to now
239 */
240static int lapic_next_event(unsigned long delta,
241 struct clock_event_device *evt)
242{
243 apic_write_around(APIC_TMICT, delta);
244 return 0;
245}
246
247/*
248 * Setup the lapic timer in periodic or oneshot mode
249 */
250static void lapic_timer_setup(enum clock_event_mode mode,
251 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800254 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800256 /* Lapic used for broadcast ? */
257 if (!local_apic_timer_verify_ok)
258 return;
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 local_irq_save(flags);
261
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800262 switch (mode) {
263 case CLOCK_EVT_MODE_PERIODIC:
264 case CLOCK_EVT_MODE_ONESHOT:
265 __setup_APIC_LVTT(calibration_result,
266 mode != CLOCK_EVT_MODE_PERIODIC, 1);
267 break;
268 case CLOCK_EVT_MODE_UNUSED:
269 case CLOCK_EVT_MODE_SHUTDOWN:
270 v = apic_read(APIC_LVTT);
271 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
272 apic_write_around(APIC_LVTT, v);
273 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700274 case CLOCK_EVT_MODE_RESUME:
275 /* Nothing to do here */
276 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 local_irq_restore(flags);
280}
281
282/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800283 * Local APIC timer broadcast function
284 */
285static void lapic_timer_broadcast(cpumask_t mask)
286{
287#ifdef CONFIG_SMP
288 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
289#endif
290}
291
292/*
293 * Setup the local APIC timer for this CPU. Copy the initilized values
294 * of the boot CPU and register the clock event in the framework.
295 */
296static void __devinit setup_APIC_timer(void)
297{
298 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
299
300 memcpy(levt, &lapic_clockevent, sizeof(*levt));
301 levt->cpumask = cpumask_of_cpu(smp_processor_id());
302
303 clockevents_register_device(levt);
304}
305
306/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800307 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800309 * We want to do the calibration only once since we want to have local timer
310 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
311 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800312 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800313 * This was previously done by reading the PIT/HPET and waiting for a wrap
314 * around to find out, that a tick has elapsed. I have a box, where the PIT
315 * readout is broken, so it never gets out of the wait loop again. This was
316 * also reported by others.
317 *
318 * Monitoring the jiffies value is inaccurate and the clockevents
319 * infrastructure allows us to do a simple substitution of the interrupt
320 * handler.
321 *
322 * The calibration routine also uses the pm_timer when possible, as the PIT
323 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
324 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
326
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800327#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200329static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800330static __initdata long lapic_cal_t1, lapic_cal_t2;
331static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
332static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
333static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
334
335/*
336 * Temporary interrupt handler.
337 */
338static void __init lapic_cal_handler(struct clock_event_device *dev)
339{
340 unsigned long long tsc = 0;
341 long tapic = apic_read(APIC_TMCCT);
342 unsigned long pm = acpi_pm_read_early();
343
344 if (cpu_has_tsc)
345 rdtscll(tsc);
346
347 switch (lapic_cal_loops++) {
348 case 0:
349 lapic_cal_t1 = tapic;
350 lapic_cal_tsc1 = tsc;
351 lapic_cal_pm1 = pm;
352 lapic_cal_j1 = jiffies;
353 break;
354
355 case LAPIC_CAL_LOOPS:
356 lapic_cal_t2 = tapic;
357 lapic_cal_tsc2 = tsc;
358 if (pm < lapic_cal_pm1)
359 pm += ACPI_PM_OVRRUN;
360 lapic_cal_pm2 = pm;
361 lapic_cal_j2 = jiffies;
362 break;
363 }
364}
365
366/*
367 * Setup the boot APIC
368 *
369 * Calibrate and verify the result.
370 */
371void __init setup_boot_APIC_clock(void)
372{
373 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
374 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
375 const long pm_thresh = pm_100ms/100;
376 void (*real_handler)(struct clock_event_device *dev);
377 unsigned long deltaj;
378 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800379 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800380
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800381 /*
382 * The local apic timer can be disabled via the kernel
Andi Kleend3f7eae2007-08-10 22:31:07 +0200383 * commandline or from the CPU detection code. Register the lapic
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800384 * timer as a dummy clock event source on SMP systems, so the
385 * broadcast mechanism is used. On UP systems simply ignore it.
386 */
387 if (local_apic_timer_disabled) {
388 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100389 if (num_possible_cpus() > 1) {
390 lapic_clockevent.mult = 1;
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800391 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100392 }
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800393 return;
394 }
395
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800396 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
397 "calibrating APIC timer ...\n");
398
399 local_irq_disable();
400
401 /* Replace the global interrupt handler */
402 real_handler = global_clock_event->event_handler;
403 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800406 * Setup the APIC counter to 1e9. There is no way the lapic
407 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800409 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800411 /* Let the interrupts run */
412 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800414 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
415 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800417 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800419 /* Restore the real event handler */
420 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800422 /* Build delta t1-t2 as apic timer counts down */
423 delta = lapic_cal_t1 - lapic_cal_t2;
424 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800426 /* Check, if the PM timer is available */
427 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
428 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800430 if (deltapm) {
431 unsigned long mult;
432 u64 res;
433
434 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
435
436 if (deltapm > (pm_100ms - pm_thresh) &&
437 deltapm < (pm_100ms + pm_thresh)) {
438 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
439 } else {
440 res = (((u64) deltapm) * mult) >> 22;
441 do_div(res, 1000000);
442 printk(KERN_WARNING "APIC calibration not consistent "
443 "with PM Timer: %ldms instead of 100ms\n",
444 (long)res);
445 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100446 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800447 do_div(res, deltapm);
448 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
449 "%lu (%ld)\n", (unsigned long) res, delta);
450 delta = (long) res;
451 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800452 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800455 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900456 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
457 lapic_clockevent.shift);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800458 lapic_clockevent.max_delta_ns =
459 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
460 lapic_clockevent.min_delta_ns =
461 clockevent_delta2ns(0xF, &lapic_clockevent);
462
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800463 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800464
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800465 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
466 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
467 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
468 calibration_result);
469
470 if (cpu_has_tsc) {
471 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800473 "%ld.%04ld MHz.\n",
474 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
475 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800479 "%u.%04u MHz.\n",
480 calibration_result / (1000000 / HZ),
481 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800483 local_apic_timer_verify_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100485 /*
486 * Do a sanity check on the APIC calibration result
487 */
488 if (calibration_result < (1000000 / HZ)) {
489 local_irq_enable();
490 printk(KERN_WARNING
491 "APIC frequency too slow, disabling apic timer\n");
492 /* No broadcast on UP ! */
493 if (num_possible_cpus() > 1)
494 setup_APIC_timer();
495 return;
496 }
497
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800498 /* We trust the pm timer based calibration */
499 if (!pm_referenced) {
500 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800501
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800502 /*
503 * Setup the apic timer manually
504 */
505 levt->event_handler = lapic_cal_handler;
506 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
507 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800508
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800509 /* Let the interrupts run */
510 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800511
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200512 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800513 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800514
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800515 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800516
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800517 /* Stop the lapic timer */
518 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800519
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800520 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800521
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800522 /* Jiffies delta */
523 deltaj = lapic_cal_j2 - lapic_cal_j1;
524 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800525
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800526 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800527 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800528 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800529 else
530 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100531 } else
532 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800533
534 if (!local_apic_timer_verify_ok) {
535 printk(KERN_WARNING
536 "APIC timer disabled due to verification failure.\n");
537 /* No broadcast on UP ! */
538 if (num_possible_cpus() == 1)
539 return;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800540 } else {
541 /*
542 * If nmi_watchdog is set to IO_APIC, we need the
543 * PIT/HPET going. Otherwise register lapic as a dummy
544 * device.
545 */
546 if (nmi_watchdog != NMI_IO_APIC)
547 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
Ingo Molnar820de5c2007-07-21 04:37:36 -0700548 else
549 printk(KERN_WARNING "APIC timer registered as dummy,"
550 " due to nmi_watchdog=1!\n");
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800551 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800552
553 /* Setup the lapic or request the broadcast */
554 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
Li Shaohua0bb31842005-06-25 14:54:55 -0700557void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800559 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800563 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800565static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800567 int cpu = smp_processor_id();
568 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
570 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800571 * Normally we should not be here till LAPIC has been initialized but
572 * in some cases like kdump, its possible that there is a pending LAPIC
573 * timer interrupt from previous kernel's context and is delivered in
574 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800576 * Interrupts are enabled early and LAPIC is setup much later, hence
577 * its possible that when we get here evt->event_handler is NULL.
578 * Check for event_handler being NULL and discard the interrupt as
579 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800581 if (!evt->event_handler) {
582 printk(KERN_WARNING
583 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
584 /* Switch it off */
585 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
586 return;
587 }
588
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100589 /*
590 * the NMI deadlock-detector uses this.
591 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800592 per_cpu(irq_stat, cpu).apic_timer_irqs++;
593
594 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
597/*
598 * Local APIC timer interrupt. This is the most natural way for doing
599 * local interrupts, but local timer interrupts can be emulated by
600 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
601 *
602 * [ if a single-CPU system runs an SMP kernel then we call the local
603 * interrupt as well. Thus we cannot inline the local irq ... ]
604 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100605void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606{
David Howells7d12e782006-10-05 14:55:46 +0100607 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 /*
610 * NOTE! We'd better ACK the irq immediately,
611 * because timer handling can be slow.
612 */
613 ack_APIC_irq();
614 /*
615 * update_process_times() expects us to have done irq_enter().
616 * Besides, if we don't timer interrupts ignore the global
617 * interrupt lock, which is the WrongThing (tm) to do.
618 */
619 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800620 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800622
David Howells7d12e782006-10-05 14:55:46 +0100623 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100626int setup_profiling_timer(unsigned int multiplier)
627{
628 return -EINVAL;
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/*
Robert Richtere319e762008-02-13 16:19:36 +0100632 * Setup extended LVT, AMD specific (K8, family 10h)
633 *
634 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
635 * MCE interrupts are supported. Thus MCE offset must be set to 0.
636 */
637
638#define APIC_EILVT_LVTOFF_MCE 0
639#define APIC_EILVT_LVTOFF_IBS 1
640
641static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
642{
643 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
644 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
645 apic_write(reg, v);
646}
647
648u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
649{
650 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
651 return APIC_EILVT_LVTOFF_MCE;
652}
653
654u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
655{
656 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
657 return APIC_EILVT_LVTOFF_IBS;
658}
659
660/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800661 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800663
664/**
665 * clear_local_APIC - shutdown the local APIC
666 *
667 * This is called, when a CPU is disabled and before rebooting, so the state of
668 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
669 * leftovers during boot.
670 */
671void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Andi Kleend3432892008-01-30 13:33:17 +0100673 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100674 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Andi Kleend3432892008-01-30 13:33:17 +0100676 /* APIC hasn't been mapped yet */
677 if (!apic_phys)
678 return;
679
680 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800682 * Masking an LVT entry can trigger a local APIC error
683 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800685 if (maxlvt >= 3) {
686 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
687 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
688 }
689 /*
690 * Careful: we have to set masks only first to deassert
691 * any level-triggered sources.
692 */
693 v = apic_read(APIC_LVTT);
694 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
695 v = apic_read(APIC_LVT0);
696 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
697 v = apic_read(APIC_LVT1);
698 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
699 if (maxlvt >= 4) {
700 v = apic_read(APIC_LVTPC);
701 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800704 /* lets not touch this if we didn't frob it */
705#ifdef CONFIG_X86_MCE_P4THERMAL
706 if (maxlvt >= 5) {
707 v = apic_read(APIC_LVTTHMR);
708 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
709 }
710#endif
711 /*
712 * Clean APIC state for other OSs:
713 */
714 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
715 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
716 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
717 if (maxlvt >= 3)
718 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
719 if (maxlvt >= 4)
720 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
721
722#ifdef CONFIG_X86_MCE_P4THERMAL
723 if (maxlvt >= 5)
724 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
725#endif
726 /* Integrated APIC (!82489DX) ? */
727 if (lapic_is_integrated()) {
728 if (maxlvt > 3)
729 /* Clear ESR due to Pentium errata 3AP and 11AP */
730 apic_write(APIC_ESR, 0);
731 apic_read(APIC_ESR);
732 }
733}
734
735/**
736 * disable_local_APIC - clear and disable the local APIC
737 */
738void disable_local_APIC(void)
739{
740 unsigned long value;
741
742 clear_local_APIC();
743
744 /*
745 * Disable APIC (implies clearing of registers
746 * for 82489DX!).
747 */
748 value = apic_read(APIC_SPIV);
749 value &= ~APIC_SPIV_APIC_ENABLED;
750 apic_write_around(APIC_SPIV, value);
751
752 /*
753 * When LAPIC was disabled by the BIOS and enabled by the kernel,
754 * restore the disabled state.
755 */
756 if (enabled_via_apicbase) {
757 unsigned int l, h;
758
759 rdmsr(MSR_IA32_APICBASE, l, h);
760 l &= ~MSR_IA32_APICBASE_ENABLE;
761 wrmsr(MSR_IA32_APICBASE, l, h);
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763}
764
765/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800766 * If Linux enabled the LAPIC against the BIOS default disable it down before
767 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
768 * not power-off. Additionally clear all LVT entries before disable_local_APIC
769 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800771void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800773 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800775 if (!cpu_has_apic)
776 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800778 local_irq_save(flags);
779 clear_local_APIC();
780
781 if (enabled_via_apicbase)
782 disable_local_APIC();
783
784 local_irq_restore(flags);
785}
786
787/*
788 * This is to verify that we're looking at a real local APIC.
789 * Check these against your board if the CPUs aren't getting
790 * started for no apparent reason.
791 */
792int __init verify_local_APIC(void)
793{
794 unsigned int reg0, reg1;
795
796 /*
797 * The version register is read-only in a real APIC.
798 */
799 reg0 = apic_read(APIC_LVR);
800 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
801 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
802 reg1 = apic_read(APIC_LVR);
803 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
804
805 /*
806 * The two version reads above should print the same
807 * numbers. If the second one is different, then we
808 * poke at a non-APIC.
809 */
810 if (reg1 != reg0)
811 return 0;
812
813 /*
814 * Check if the version looks reasonably.
815 */
816 reg1 = GET_APIC_VERSION(reg0);
817 if (reg1 == 0x00 || reg1 == 0xff)
818 return 0;
819 reg1 = lapic_get_maxlvt();
820 if (reg1 < 0x02 || reg1 == 0xff)
821 return 0;
822
823 /*
824 * The ID register is read/write in a real APIC.
825 */
826 reg0 = apic_read(APIC_ID);
827 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
828
829 /*
830 * The next two are just to see if we have sane values.
831 * They're only really relevant if we're in Virtual Wire
832 * compatibility mode, but most boxes are anymore.
833 */
834 reg0 = apic_read(APIC_LVT0);
835 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
836 reg1 = apic_read(APIC_LVT1);
837 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
838
839 return 1;
840}
841
842/**
843 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
844 */
845void __init sync_Arb_IDs(void)
846{
847 /*
848 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
849 * needed on AMD.
850 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100851 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800852 return;
853 /*
854 * Wait for idle.
855 */
856 apic_wait_icr_idle();
857
858 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
859 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
860 | APIC_DM_INIT);
861}
862
863/*
864 * An initial setup of the virtual wire mode.
865 */
866void __init init_bsp_APIC(void)
867{
868 unsigned long value;
869
870 /*
871 * Don't do the setup now if we have a SMP BIOS as the
872 * through-I/O-APIC virtual wire mode might be active.
873 */
874 if (smp_found_config || !cpu_has_apic)
875 return;
876
877 /*
878 * Do not trust the local APIC being empty at bootup.
879 */
880 clear_local_APIC();
881
882 /*
883 * Enable APIC.
884 */
885 value = apic_read(APIC_SPIV);
886 value &= ~APIC_VECTOR_MASK;
887 value |= APIC_SPIV_APIC_ENABLED;
888
889 /* This bit is reserved on P4/Xeon and should be cleared */
890 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
891 (boot_cpu_data.x86 == 15))
892 value &= ~APIC_SPIV_FOCUS_DISABLED;
893 else
894 value |= APIC_SPIV_FOCUS_DISABLED;
895 value |= SPURIOUS_APIC_VECTOR;
896 apic_write_around(APIC_SPIV, value);
897
898 /*
899 * Set up the virtual wire mode.
900 */
901 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
902 value = APIC_DM_NMI;
903 if (!lapic_is_integrated()) /* 82489DX */
904 value |= APIC_LVT_LEVEL_TRIGGER;
905 apic_write_around(APIC_LVT1, value);
906}
907
Ingo Molnara4928cf2008-04-23 13:20:56 +0200908static void __cpuinit lapic_setup_esr(void)
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300909{
910 unsigned long oldvalue, value, maxlvt;
911 if (lapic_is_integrated() && !esr_disable) {
912 /* !82489DX */
913 maxlvt = lapic_get_maxlvt();
914 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
915 apic_write(APIC_ESR, 0);
916 oldvalue = apic_read(APIC_ESR);
917
918 /* enables sending errors */
919 value = ERROR_APIC_VECTOR;
920 apic_write_around(APIC_LVTERR, value);
921 /*
922 * spec says clear errors after enabling vector.
923 */
924 if (maxlvt > 3)
925 apic_write(APIC_ESR, 0);
926 value = apic_read(APIC_ESR);
927 if (value != oldvalue)
928 apic_printk(APIC_VERBOSE, "ESR value before enabling "
929 "vector: 0x%08lx after: 0x%08lx\n",
930 oldvalue, value);
931 } else {
932 if (esr_disable)
933 /*
934 * Something untraceable is creating bad interrupts on
935 * secondary quads ... for the moment, just leave the
936 * ESR disabled - we can't do anything useful with the
937 * errors anyway - mbligh
938 */
939 printk(KERN_INFO "Leaving ESR disabled.\n");
940 else
941 printk(KERN_INFO "No ESR for 82489DX.\n");
942 }
943}
944
945
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800946/**
947 * setup_local_APIC - setup the local APIC
948 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100949void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800950{
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300951 unsigned long value, integrated;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800952 int i, j;
953
954 /* Pound the ESR really hard over the head with a big hammer - mbligh */
955 if (esr_disable) {
956 apic_write(APIC_ESR, 0);
957 apic_write(APIC_ESR, 0);
958 apic_write(APIC_ESR, 0);
959 apic_write(APIC_ESR, 0);
960 }
961
962 integrated = lapic_is_integrated();
963
964 /*
965 * Double-check whether this APIC is really registered.
966 */
967 if (!apic_id_registered())
968 BUG();
969
970 /*
971 * Intel recommends to set DFR, LDR and TPR before enabling
972 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
973 * document number 292116). So here it goes...
974 */
975 init_apic_ldr();
976
977 /*
978 * Set Task Priority to 'accept all'. We never change this
979 * later on.
980 */
981 value = apic_read(APIC_TASKPRI);
982 value &= ~APIC_TPRI_MASK;
983 apic_write_around(APIC_TASKPRI, value);
984
985 /*
986 * After a crash, we no longer service the interrupts and a pending
987 * interrupt from previous kernel might still have ISR bit set.
988 *
989 * Most probably by now CPU has serviced that pending interrupt and
990 * it might not have done the ack_APIC_irq() because it thought,
991 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
992 * does not clear the ISR bit and cpu thinks it has already serivced
993 * the interrupt. Hence a vector might get locked. It was noticed
994 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
995 */
996 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
997 value = apic_read(APIC_ISR + i*0x10);
998 for (j = 31; j >= 0; j--) {
999 if (value & (1<<j))
1000 ack_APIC_irq();
1001 }
1002 }
1003
1004 /*
1005 * Now that we are all set up, enable the APIC
1006 */
1007 value = apic_read(APIC_SPIV);
1008 value &= ~APIC_VECTOR_MASK;
1009 /*
1010 * Enable APIC
1011 */
1012 value |= APIC_SPIV_APIC_ENABLED;
1013
1014 /*
1015 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1016 * certain networking cards. If high frequency interrupts are
1017 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1018 * entry is masked/unmasked at a high rate as well then sooner or
1019 * later IOAPIC line gets 'stuck', no more interrupts are received
1020 * from the device. If focus CPU is disabled then the hang goes
1021 * away, oh well :-(
1022 *
1023 * [ This bug can be reproduced easily with a level-triggered
1024 * PCI Ne2000 networking cards and PII/PIII processors, dual
1025 * BX chipset. ]
1026 */
1027 /*
1028 * Actually disabling the focus CPU check just makes the hang less
1029 * frequent as it makes the interrupt distributon model be more
1030 * like LRU than MRU (the short-term load is more even across CPUs).
1031 * See also the comment in end_level_ioapic_irq(). --macro
1032 */
1033
1034 /* Enable focus processor (bit==0) */
1035 value &= ~APIC_SPIV_FOCUS_DISABLED;
1036
1037 /*
1038 * Set spurious IRQ vector
1039 */
1040 value |= SPURIOUS_APIC_VECTOR;
1041 apic_write_around(APIC_SPIV, value);
1042
1043 /*
1044 * Set up LVT0, LVT1:
1045 *
1046 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001047 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001048 * we delegate interrupts to the 8259A.
1049 */
1050 /*
1051 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1052 */
1053 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1054 if (!smp_processor_id() && (pic_mode || !value)) {
1055 value = APIC_DM_EXTINT;
1056 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1057 smp_processor_id());
1058 } else {
1059 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1060 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1061 smp_processor_id());
1062 }
1063 apic_write_around(APIC_LVT0, value);
1064
1065 /*
1066 * only the BP should see the LINT1 NMI signal, obviously.
1067 */
1068 if (!smp_processor_id())
1069 value = APIC_DM_NMI;
1070 else
1071 value = APIC_DM_NMI | APIC_LVT_MASKED;
1072 if (!integrated) /* 82489DX */
1073 value |= APIC_LVT_LEVEL_TRIGGER;
1074 apic_write_around(APIC_LVT1, value);
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001075}
1076
1077void __cpuinit end_local_APIC_setup(void)
1078{
1079 unsigned long value;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001080
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001081 lapic_setup_esr();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001082 /* Disable the local apic timer */
1083 value = apic_read(APIC_LVTT);
1084 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1085 apic_write_around(APIC_LVTT, value);
1086
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001087 setup_apic_nmi_watchdog(NULL);
1088 apic_pm_activate();
1089}
1090
1091/*
1092 * Detect and initialize APIC
1093 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001094static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001095{
1096 u32 h, l, features;
1097
1098 /* Disabled by kernel option? */
1099 if (enable_local_apic < 0)
1100 return -1;
1101
1102 switch (boot_cpu_data.x86_vendor) {
1103 case X86_VENDOR_AMD:
1104 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1105 (boot_cpu_data.x86 == 15))
1106 break;
1107 goto no_apic;
1108 case X86_VENDOR_INTEL:
1109 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1110 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1111 break;
1112 goto no_apic;
1113 default:
1114 goto no_apic;
1115 }
1116
1117 if (!cpu_has_apic) {
1118 /*
1119 * Over-ride BIOS and try to enable the local APIC only if
1120 * "lapic" specified.
1121 */
1122 if (enable_local_apic <= 0) {
1123 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1124 "you can enable it with \"lapic\"\n");
1125 return -1;
1126 }
1127 /*
1128 * Some BIOSes disable the local APIC in the APIC_BASE
1129 * MSR. This can only be done in software for Intel P6 or later
1130 * and AMD K7 (Model > 1) or later.
1131 */
1132 rdmsr(MSR_IA32_APICBASE, l, h);
1133 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1134 printk(KERN_INFO
1135 "Local APIC disabled by BIOS -- reenabling.\n");
1136 l &= ~MSR_IA32_APICBASE_BASE;
1137 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1138 wrmsr(MSR_IA32_APICBASE, l, h);
1139 enabled_via_apicbase = 1;
1140 }
1141 }
1142 /*
1143 * The APIC feature bit should now be enabled
1144 * in `cpuid'
1145 */
1146 features = cpuid_edx(1);
1147 if (!(features & (1 << X86_FEATURE_APIC))) {
1148 printk(KERN_WARNING "Could not enable APIC!\n");
1149 return -1;
1150 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001151 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001152 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1153
1154 /* The BIOS may have set up the APIC at some other address */
1155 rdmsr(MSR_IA32_APICBASE, l, h);
1156 if (l & MSR_IA32_APICBASE_ENABLE)
1157 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1158
Daniel Gollub0328ece2007-08-15 02:40:35 +02001159 if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001160 nmi_watchdog = NMI_LOCAL_APIC;
1161
1162 printk(KERN_INFO "Found and enabled local APIC!\n");
1163
1164 apic_pm_activate();
1165
1166 return 0;
1167
1168no_apic:
1169 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1170 return -1;
1171}
1172
1173/**
1174 * init_apic_mappings - initialize APIC mappings
1175 */
1176void __init init_apic_mappings(void)
1177{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001178 /*
1179 * If no local APIC can be found then set up a fake all
1180 * zeroes page to simulate the local APIC and another
1181 * one for the IO-APIC.
1182 */
1183 if (!smp_found_config && detect_init_APIC()) {
1184 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1185 apic_phys = __pa(apic_phys);
1186 } else
1187 apic_phys = mp_lapic_addr;
1188
1189 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1190 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1191 apic_phys);
1192
1193 /*
1194 * Fetch the APIC ID of the BSP in case we have a
1195 * default configuration (or the MP table is broken).
1196 */
1197 if (boot_cpu_physical_apicid == -1U)
Jack Steiner05f2d122008-03-28 14:12:02 -05001198 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001199
1200#ifdef CONFIG_X86_IO_APIC
1201 {
1202 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1203 int i;
1204
1205 for (i = 0; i < nr_ioapics; i++) {
1206 if (smp_found_config) {
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001207 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001208 if (!ioapic_phys) {
1209 printk(KERN_ERR
1210 "WARNING: bogus zero IO-APIC "
1211 "address found in MPTABLE, "
1212 "disabling IO/APIC support!\n");
1213 smp_found_config = 0;
1214 skip_ioapic_setup = 1;
1215 goto fake_ioapic_page;
1216 }
1217 } else {
1218fake_ioapic_page:
1219 ioapic_phys = (unsigned long)
1220 alloc_bootmem_pages(PAGE_SIZE);
1221 ioapic_phys = __pa(ioapic_phys);
1222 }
1223 set_fixmap_nocache(idx, ioapic_phys);
1224 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1225 __fix_to_virt(idx), ioapic_phys);
1226 idx++;
1227 }
1228 }
1229#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230}
1231
1232/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001233 * This initializes the IO-APIC and APIC hardware if this is
1234 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 */
Alexey Starikovskiye81b2c62008-03-27 23:54:31 +03001236
1237int apic_version[MAX_APICS];
1238
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001239int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001241 if (enable_local_apic < 0)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001242 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001243
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001244 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001245 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
1247 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001248 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001250 if (!cpu_has_apic &&
1251 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001253 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001254 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 return -1;
1256 }
1257
1258 verify_local_APIC();
1259
1260 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001261
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001262 /*
1263 * Hack: In case of kdump, after a crash, kernel might be booting
1264 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1265 * might be zero if read from MP tables. Get it from LAPIC.
1266 */
1267#ifdef CONFIG_CRASH_DUMP
Jack Steiner05f2d122008-03-28 14:12:02 -05001268 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001269#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001270 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 setup_local_APIC();
1273
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001274 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001276 if (smp_found_config)
1277 if (!skip_ioapic_setup && nr_ioapics)
1278 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001280 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001281
1282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001284
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001285/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001286 * Local APIC interrupts
1287 */
1288
1289/*
1290 * This interrupt should _never_ happen with our APIC/SMP architecture
1291 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001292void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001293{
1294 unsigned long v;
1295
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001296 irq_enter();
1297 /*
1298 * Check if this really is a spurious interrupt and ACK it
1299 * if it is a vectored one. Just in case...
1300 * Spurious interrupts should not be ACKed.
1301 */
1302 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1303 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1304 ack_APIC_irq();
1305
1306 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1307 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1308 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001309 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001310 irq_exit();
1311}
1312
1313/*
1314 * This interrupt should never happen with our APIC/SMP architecture
1315 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001316void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001317{
1318 unsigned long v, v1;
1319
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001320 irq_enter();
1321 /* First tickle the hardware, only then report what went on. -- REW */
1322 v = apic_read(APIC_ESR);
1323 apic_write(APIC_ESR, 0);
1324 v1 = apic_read(APIC_ESR);
1325 ack_APIC_irq();
1326 atomic_inc(&irq_err_count);
1327
1328 /* Here is what the APIC error bits mean:
1329 0: Send CS error
1330 1: Receive CS error
1331 2: Send accept error
1332 3: Receive accept error
1333 4: Reserved
1334 5: Send illegal vector
1335 6: Received illegal vector
1336 7: Illegal register address
1337 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001338 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001339 smp_processor_id(), v , v1);
1340 irq_exit();
1341}
1342
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001343#ifdef CONFIG_SMP
1344void __init smp_intr_init(void)
1345{
1346 /*
1347 * IRQ0 must be given a fixed assignment and initialized,
1348 * because it's used before the IO-APIC is set up.
1349 */
1350 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1351
1352 /*
1353 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1354 * IPI, driven by wakeup.
1355 */
1356 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1357
1358 /* IPI for invalidation */
1359 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1360
1361 /* IPI for generic function call */
1362 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1363}
1364#endif
1365
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001366/*
1367 * Initialize APIC interrupts
1368 */
1369void __init apic_intr_init(void)
1370{
1371#ifdef CONFIG_SMP
1372 smp_intr_init();
1373#endif
1374 /* self generated IPI for local APIC timer */
1375 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1376
1377 /* IPI vectors for APIC spurious and error interrupts */
1378 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1379 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1380
1381 /* thermal monitor LVT interrupt */
1382#ifdef CONFIG_X86_MCE_P4THERMAL
1383 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1384#endif
1385}
1386
1387/**
1388 * connect_bsp_APIC - attach the APIC to the interrupt system
1389 */
1390void __init connect_bsp_APIC(void)
1391{
1392 if (pic_mode) {
1393 /*
1394 * Do not trust the local APIC being empty at bootup.
1395 */
1396 clear_local_APIC();
1397 /*
1398 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1399 * local APIC to INT and NMI lines.
1400 */
1401 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1402 "enabling APIC mode.\n");
1403 outb(0x70, 0x22);
1404 outb(0x01, 0x23);
1405 }
1406 enable_apic_mode();
1407}
1408
1409/**
1410 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1411 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1412 *
1413 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1414 * APIC is disabled.
1415 */
1416void disconnect_bsp_APIC(int virt_wire_setup)
1417{
1418 if (pic_mode) {
1419 /*
1420 * Put the board back into PIC mode (has an effect only on
1421 * certain older boards). Note that APIC interrupts, including
1422 * IPIs, won't work beyond this point! The only exception are
1423 * INIT IPIs.
1424 */
1425 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1426 "entering PIC mode.\n");
1427 outb(0x70, 0x22);
1428 outb(0x00, 0x23);
1429 } else {
1430 /* Go back to Virtual Wire compatibility mode */
1431 unsigned long value;
1432
1433 /* For the spurious interrupt use vector F, and enable it */
1434 value = apic_read(APIC_SPIV);
1435 value &= ~APIC_VECTOR_MASK;
1436 value |= APIC_SPIV_APIC_ENABLED;
1437 value |= 0xf;
1438 apic_write_around(APIC_SPIV, value);
1439
1440 if (!virt_wire_setup) {
1441 /*
1442 * For LVT0 make it edge triggered, active high,
1443 * external and enabled
1444 */
1445 value = apic_read(APIC_LVT0);
1446 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1447 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001448 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001449 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1450 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1451 apic_write_around(APIC_LVT0, value);
1452 } else {
1453 /* Disable LVT0 */
1454 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1455 }
1456
1457 /*
1458 * For LVT1 make it edge triggered, active high, nmi and
1459 * enabled
1460 */
1461 value = apic_read(APIC_LVT1);
1462 value &= ~(
1463 APIC_MODE_MASK | APIC_SEND_PENDING |
1464 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1465 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1466 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1467 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1468 apic_write_around(APIC_LVT1, value);
1469 }
1470}
1471
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001472unsigned int __cpuinitdata maxcpus = NR_CPUS;
1473
1474void __cpuinit generic_processor_info(int apicid, int version)
1475{
1476 int cpu;
1477 cpumask_t tmp_map;
1478 physid_mask_t phys_cpu;
1479
1480 /*
1481 * Validate version
1482 */
1483 if (version == 0x0) {
1484 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1485 "fixing up to 0x10. (tell your hw vendor)\n",
1486 version);
1487 version = 0x10;
1488 }
1489 apic_version[apicid] = version;
1490
1491 phys_cpu = apicid_to_cpu_present(apicid);
1492 physids_or(phys_cpu_present_map, phys_cpu_present_map, phys_cpu);
1493
1494 if (num_processors >= NR_CPUS) {
1495 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1496 " Processor ignored.\n", NR_CPUS);
1497 return;
1498 }
1499
1500 if (num_processors >= maxcpus) {
1501 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1502 " Processor ignored.\n", maxcpus);
1503 return;
1504 }
1505
1506 num_processors++;
1507 cpus_complement(tmp_map, cpu_present_map);
1508 cpu = first_cpu(tmp_map);
1509
1510 if (apicid == boot_cpu_physical_apicid)
1511 /*
1512 * x86_bios_cpu_apicid is required to have processors listed
1513 * in same order as logical cpu numbers. Hence the first
1514 * entry is BSP, and so on.
1515 */
1516 cpu = 0;
1517
1518 /*
1519 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1520 * but we need to work other dependencies like SMP_SUSPEND etc
1521 * before this can be done without some confusion.
1522 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1523 * - Ashok Raj <ashok.raj@intel.com>
1524 */
1525 if (num_processors > 8) {
1526 switch (boot_cpu_data.x86_vendor) {
1527 case X86_VENDOR_INTEL:
1528 if (!APIC_XAPIC(version)) {
1529 def_to_bigsmp = 0;
1530 break;
1531 }
1532 /* If P4 and above fall through */
1533 case X86_VENDOR_AMD:
1534 def_to_bigsmp = 1;
1535 }
1536 }
1537#ifdef CONFIG_SMP
1538 /* are we being called early in kernel startup? */
1539 if (x86_cpu_to_apicid_early_ptr) {
1540 u16 *cpu_to_apicid = x86_cpu_to_apicid_early_ptr;
1541 u16 *bios_cpu_apicid = x86_bios_cpu_apicid_early_ptr;
1542
1543 cpu_to_apicid[cpu] = apicid;
1544 bios_cpu_apicid[cpu] = apicid;
1545 } else {
1546 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1547 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1548 }
1549#endif
1550 cpu_set(cpu, cpu_possible_map);
1551 cpu_set(cpu, cpu_present_map);
1552}
1553
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001554/*
1555 * Power management
1556 */
1557#ifdef CONFIG_PM
1558
1559static struct {
1560 int active;
1561 /* r/w apic fields */
1562 unsigned int apic_id;
1563 unsigned int apic_taskpri;
1564 unsigned int apic_ldr;
1565 unsigned int apic_dfr;
1566 unsigned int apic_spiv;
1567 unsigned int apic_lvtt;
1568 unsigned int apic_lvtpc;
1569 unsigned int apic_lvt0;
1570 unsigned int apic_lvt1;
1571 unsigned int apic_lvterr;
1572 unsigned int apic_tmict;
1573 unsigned int apic_tdcr;
1574 unsigned int apic_thmr;
1575} apic_pm_state;
1576
1577static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1578{
1579 unsigned long flags;
1580 int maxlvt;
1581
1582 if (!apic_pm_state.active)
1583 return 0;
1584
1585 maxlvt = lapic_get_maxlvt();
1586
1587 apic_pm_state.apic_id = apic_read(APIC_ID);
1588 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1589 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1590 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1591 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1592 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1593 if (maxlvt >= 4)
1594 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1595 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1596 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1597 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1598 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1599 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1600#ifdef CONFIG_X86_MCE_P4THERMAL
1601 if (maxlvt >= 5)
1602 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1603#endif
1604
1605 local_irq_save(flags);
1606 disable_local_APIC();
1607 local_irq_restore(flags);
1608 return 0;
1609}
1610
1611static int lapic_resume(struct sys_device *dev)
1612{
1613 unsigned int l, h;
1614 unsigned long flags;
1615 int maxlvt;
1616
1617 if (!apic_pm_state.active)
1618 return 0;
1619
1620 maxlvt = lapic_get_maxlvt();
1621
1622 local_irq_save(flags);
1623
1624 /*
1625 * Make sure the APICBASE points to the right address
1626 *
1627 * FIXME! This will be wrong if we ever support suspend on
1628 * SMP! We'll need to do this as part of the CPU restore!
1629 */
1630 rdmsr(MSR_IA32_APICBASE, l, h);
1631 l &= ~MSR_IA32_APICBASE_BASE;
1632 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1633 wrmsr(MSR_IA32_APICBASE, l, h);
1634
1635 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1636 apic_write(APIC_ID, apic_pm_state.apic_id);
1637 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1638 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1639 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1640 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1641 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1642 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1643#ifdef CONFIG_X86_MCE_P4THERMAL
1644 if (maxlvt >= 5)
1645 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1646#endif
1647 if (maxlvt >= 4)
1648 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1649 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1650 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1651 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1652 apic_write(APIC_ESR, 0);
1653 apic_read(APIC_ESR);
1654 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1655 apic_write(APIC_ESR, 0);
1656 apic_read(APIC_ESR);
1657 local_irq_restore(flags);
1658 return 0;
1659}
1660
1661/*
1662 * This device has no shutdown method - fully functioning local APICs
1663 * are needed on every CPU up until machine_halt/restart/poweroff.
1664 */
1665
1666static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001667 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001668 .resume = lapic_resume,
1669 .suspend = lapic_suspend,
1670};
1671
1672static struct sys_device device_lapic = {
1673 .id = 0,
1674 .cls = &lapic_sysclass,
1675};
1676
1677static void __devinit apic_pm_activate(void)
1678{
1679 apic_pm_state.active = 1;
1680}
1681
1682static int __init init_lapic_sysfs(void)
1683{
1684 int error;
1685
1686 if (!cpu_has_apic)
1687 return 0;
1688 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1689
1690 error = sysdev_class_register(&lapic_sysclass);
1691 if (!error)
1692 error = sysdev_register(&device_lapic);
1693 return error;
1694}
1695device_initcall(init_lapic_sysfs);
1696
1697#else /* CONFIG_PM */
1698
1699static void apic_pm_activate(void) { }
1700
1701#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001702
1703/*
1704 * APIC command line parameters
1705 */
1706static int __init parse_lapic(char *arg)
1707{
1708 enable_local_apic = 1;
1709 return 0;
1710}
1711early_param("lapic", parse_lapic);
1712
1713static int __init parse_nolapic(char *arg)
1714{
1715 enable_local_apic = -1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001716 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001717 return 0;
1718}
1719early_param("nolapic", parse_nolapic);
1720
1721static int __init parse_disable_lapic_timer(char *arg)
1722{
1723 local_apic_timer_disabled = 1;
1724 return 0;
1725}
1726early_param("nolapic_timer", parse_disable_lapic_timer);
1727
1728static int __init parse_lapic_timer_c2_ok(char *arg)
1729{
1730 local_apic_timer_c2_ok = 1;
1731 return 0;
1732}
1733early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1734
1735static int __init apic_set_verbosity(char *str)
1736{
1737 if (strcmp("debug", str) == 0)
1738 apic_verbosity = APIC_DEBUG;
1739 else if (strcmp("verbose", str) == 0)
1740 apic_verbosity = APIC_VERBOSE;
1741 return 1;
1742}
1743__setup("apic=", apic_set_verbosity);
1744