blob: 3f13a1aa07cdd433fe15260521fecfd6248915a6 [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
Alan Mayerce178332008-04-16 15:17:20 -050074int first_system_vector = 0xfe;
75
76char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
77
Eric W. Biederman9635b472005-06-25 14:57:41 -070078/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080079 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
81int apic_verbosity;
82
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080083static unsigned int calibration_result;
84
85static int lapic_next_event(unsigned long delta,
86 struct clock_event_device *evt);
87static void lapic_timer_setup(enum clock_event_mode mode,
88 struct clock_event_device *evt);
89static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090static void apic_pm_activate(void);
91
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080092/*
93 * The local apic timer can be used for any function which is CPU local.
94 */
95static struct clock_event_device lapic_clockevent = {
96 .name = "lapic",
97 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080098 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080099 .shift = 32,
100 .set_mode = lapic_timer_setup,
101 .set_next_event = lapic_next_event,
102 .broadcast = lapic_timer_broadcast,
103 .rating = 100,
104 .irq = -1,
105};
106static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800108/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static int enabled_via_apicbase;
110
Andi Kleend3432892008-01-30 13:33:17 +0100111static unsigned long apic_phys;
112
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800113/*
114 * Get the LAPIC version
115 */
116static inline int lapic_get_version(void)
117{
118 return GET_APIC_VERSION(apic_read(APIC_LVR));
119}
120
121/*
Joe Perchesab4a5742008-01-30 13:31:42 +0100122 * Check, if the APIC is integrated or a separate chip
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800123 */
124static inline int lapic_is_integrated(void)
125{
126 return APIC_INTEGRATED(lapic_get_version());
127}
128
129/*
130 * Check, whether this is a modern or a first generation APIC
131 */
132static int modern_apic(void)
133{
134 /* AMD systems use old APIC versions, so check the CPU */
135 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
136 boot_cpu_data.x86 >= 0xf)
137 return 1;
138 return lapic_get_version() >= 0x14;
139}
140
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200141void apic_wait_icr_idle(void)
142{
143 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
144 cpu_relax();
145}
146
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100147u32 safe_apic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200148{
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100149 u32 send_status;
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200150 int timeout;
151
152 timeout = 0;
153 do {
154 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
155 if (!send_status)
156 break;
157 udelay(100);
158 } while (timeout++ < 1000);
159
160 return send_status;
161}
162
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800163/**
164 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
165 */
Jan Beuliche9427102008-01-30 13:31:24 +0100166void __cpuinit enable_NMI_through_LVT0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800168 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800170 /* Level triggered for 82489DX */
171 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 v |= APIC_LVT_LEVEL_TRIGGER;
173 apic_write_around(APIC_LVT0, v);
174}
175
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800176/**
177 * get_physical_broadcast - Get number of physical broadcast IDs
178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179int get_physical_broadcast(void)
180{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800181 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800184/**
185 * lapic_get_maxlvt - get the maximum number of local vector table entries
186 */
187int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800189 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800192 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
195/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800196 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800199/* Clock divisor is set to 16 */
200#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/*
203 * This function sets up the local APIC timer, with a timeout of
204 * 'clocks' APIC bus clock. During calibration we actually call
205 * this function twice on the boot CPU, once with a bogus timeout
206 * value, second time for real. The other (noncalibrating) CPUs
207 * call this function only once, with the real, calibrated value.
208 *
209 * We do reads before writes even if unnecessary, to get around the
210 * P5 APIC double write bug.
211 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800212static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800214 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800216 lvtt_value = LOCAL_TIMER_VECTOR;
217 if (!oneshot)
218 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800219 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100221
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800222 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100223 lvtt_value |= APIC_LVT_MASKED;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 apic_write_around(APIC_LVTT, lvtt_value);
226
227 /*
228 * Divide PICLK by 16
229 */
230 tmp_value = apic_read(APIC_TDCR);
231 apic_write_around(APIC_TDCR, (tmp_value
232 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
233 | APIC_TDR_DIV_16);
234
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800235 if (!oneshot)
236 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800239/*
240 * Program the next event, relative to now
241 */
242static int lapic_next_event(unsigned long delta,
243 struct clock_event_device *evt)
244{
245 apic_write_around(APIC_TMICT, delta);
246 return 0;
247}
248
249/*
250 * Setup the lapic timer in periodic or oneshot mode
251 */
252static void lapic_timer_setup(enum clock_event_mode mode,
253 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800256 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800258 /* Lapic used for broadcast ? */
259 if (!local_apic_timer_verify_ok)
260 return;
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 local_irq_save(flags);
263
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800264 switch (mode) {
265 case CLOCK_EVT_MODE_PERIODIC:
266 case CLOCK_EVT_MODE_ONESHOT:
267 __setup_APIC_LVTT(calibration_result,
268 mode != CLOCK_EVT_MODE_PERIODIC, 1);
269 break;
270 case CLOCK_EVT_MODE_UNUSED:
271 case CLOCK_EVT_MODE_SHUTDOWN:
272 v = apic_read(APIC_LVTT);
273 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
274 apic_write_around(APIC_LVTT, v);
275 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700276 case CLOCK_EVT_MODE_RESUME:
277 /* Nothing to do here */
278 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 local_irq_restore(flags);
282}
283
284/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800285 * Local APIC timer broadcast function
286 */
287static void lapic_timer_broadcast(cpumask_t mask)
288{
289#ifdef CONFIG_SMP
290 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
291#endif
292}
293
294/*
295 * Setup the local APIC timer for this CPU. Copy the initilized values
296 * of the boot CPU and register the clock event in the framework.
297 */
298static void __devinit setup_APIC_timer(void)
299{
300 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
301
302 memcpy(levt, &lapic_clockevent, sizeof(*levt));
303 levt->cpumask = cpumask_of_cpu(smp_processor_id());
304
305 clockevents_register_device(levt);
306}
307
308/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800309 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800311 * We want to do the calibration only once since we want to have local timer
312 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
313 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800314 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800315 * This was previously done by reading the PIT/HPET and waiting for a wrap
316 * around to find out, that a tick has elapsed. I have a box, where the PIT
317 * readout is broken, so it never gets out of the wait loop again. This was
318 * also reported by others.
319 *
320 * Monitoring the jiffies value is inaccurate and the clockevents
321 * infrastructure allows us to do a simple substitution of the interrupt
322 * handler.
323 *
324 * The calibration routine also uses the pm_timer when possible, as the PIT
325 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
326 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 */
328
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800329#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200331static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800332static __initdata long lapic_cal_t1, lapic_cal_t2;
333static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
334static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
335static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
336
337/*
338 * Temporary interrupt handler.
339 */
340static void __init lapic_cal_handler(struct clock_event_device *dev)
341{
342 unsigned long long tsc = 0;
343 long tapic = apic_read(APIC_TMCCT);
344 unsigned long pm = acpi_pm_read_early();
345
346 if (cpu_has_tsc)
347 rdtscll(tsc);
348
349 switch (lapic_cal_loops++) {
350 case 0:
351 lapic_cal_t1 = tapic;
352 lapic_cal_tsc1 = tsc;
353 lapic_cal_pm1 = pm;
354 lapic_cal_j1 = jiffies;
355 break;
356
357 case LAPIC_CAL_LOOPS:
358 lapic_cal_t2 = tapic;
359 lapic_cal_tsc2 = tsc;
360 if (pm < lapic_cal_pm1)
361 pm += ACPI_PM_OVRRUN;
362 lapic_cal_pm2 = pm;
363 lapic_cal_j2 = jiffies;
364 break;
365 }
366}
367
368/*
369 * Setup the boot APIC
370 *
371 * Calibrate and verify the result.
372 */
373void __init setup_boot_APIC_clock(void)
374{
375 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
376 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
377 const long pm_thresh = pm_100ms/100;
378 void (*real_handler)(struct clock_event_device *dev);
379 unsigned long deltaj;
380 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800381 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800382
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800383 /*
384 * The local apic timer can be disabled via the kernel
Andi Kleend3f7eae2007-08-10 22:31:07 +0200385 * commandline or from the CPU detection code. Register the lapic
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800386 * timer as a dummy clock event source on SMP systems, so the
387 * broadcast mechanism is used. On UP systems simply ignore it.
388 */
389 if (local_apic_timer_disabled) {
390 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100391 if (num_possible_cpus() > 1) {
392 lapic_clockevent.mult = 1;
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800393 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100394 }
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800395 return;
396 }
397
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800398 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
399 "calibrating APIC timer ...\n");
400
401 local_irq_disable();
402
403 /* Replace the global interrupt handler */
404 real_handler = global_clock_event->event_handler;
405 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800408 * Setup the APIC counter to 1e9. There is no way the lapic
409 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800411 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800413 /* Let the interrupts run */
414 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800416 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
417 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800419 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800421 /* Restore the real event handler */
422 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800424 /* Build delta t1-t2 as apic timer counts down */
425 delta = lapic_cal_t1 - lapic_cal_t2;
426 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800428 /* Check, if the PM timer is available */
429 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
430 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800432 if (deltapm) {
433 unsigned long mult;
434 u64 res;
435
436 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
437
438 if (deltapm > (pm_100ms - pm_thresh) &&
439 deltapm < (pm_100ms + pm_thresh)) {
440 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
441 } else {
442 res = (((u64) deltapm) * mult) >> 22;
443 do_div(res, 1000000);
444 printk(KERN_WARNING "APIC calibration not consistent "
445 "with PM Timer: %ldms instead of 100ms\n",
446 (long)res);
447 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100448 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800449 do_div(res, deltapm);
450 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
451 "%lu (%ld)\n", (unsigned long) res, delta);
452 delta = (long) res;
453 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800454 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800457 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900458 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
459 lapic_clockevent.shift);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800460 lapic_clockevent.max_delta_ns =
461 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
462 lapic_clockevent.min_delta_ns =
463 clockevent_delta2ns(0xF, &lapic_clockevent);
464
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800465 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800466
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800467 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
468 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
469 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
470 calibration_result);
471
472 if (cpu_has_tsc) {
473 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800475 "%ld.%04ld MHz.\n",
476 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
477 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800481 "%u.%04u MHz.\n",
482 calibration_result / (1000000 / HZ),
483 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800485 local_apic_timer_verify_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100487 /*
488 * Do a sanity check on the APIC calibration result
489 */
490 if (calibration_result < (1000000 / HZ)) {
491 local_irq_enable();
492 printk(KERN_WARNING
493 "APIC frequency too slow, disabling apic timer\n");
494 /* No broadcast on UP ! */
495 if (num_possible_cpus() > 1)
496 setup_APIC_timer();
497 return;
498 }
499
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800500 /* We trust the pm timer based calibration */
501 if (!pm_referenced) {
502 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800503
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800504 /*
505 * Setup the apic timer manually
506 */
507 levt->event_handler = lapic_cal_handler;
508 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
509 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800510
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800511 /* Let the interrupts run */
512 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800513
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200514 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800515 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800516
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800517 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800518
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800519 /* Stop the lapic timer */
520 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800521
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800522 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800523
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800524 /* Jiffies delta */
525 deltaj = lapic_cal_j2 - lapic_cal_j1;
526 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800527
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800528 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800529 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800530 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800531 else
532 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100533 } else
534 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800535
536 if (!local_apic_timer_verify_ok) {
537 printk(KERN_WARNING
538 "APIC timer disabled due to verification failure.\n");
539 /* No broadcast on UP ! */
540 if (num_possible_cpus() == 1)
541 return;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800542 } else {
543 /*
544 * If nmi_watchdog is set to IO_APIC, we need the
545 * PIT/HPET going. Otherwise register lapic as a dummy
546 * device.
547 */
548 if (nmi_watchdog != NMI_IO_APIC)
549 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
Ingo Molnar820de5c2007-07-21 04:37:36 -0700550 else
551 printk(KERN_WARNING "APIC timer registered as dummy,"
552 " due to nmi_watchdog=1!\n");
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800553 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800554
555 /* Setup the lapic or request the broadcast */
556 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
Li Shaohua0bb31842005-06-25 14:54:55 -0700559void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800561 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800565 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800567static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800569 int cpu = smp_processor_id();
570 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800573 * Normally we should not be here till LAPIC has been initialized but
574 * in some cases like kdump, its possible that there is a pending LAPIC
575 * timer interrupt from previous kernel's context and is delivered in
576 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800578 * Interrupts are enabled early and LAPIC is setup much later, hence
579 * its possible that when we get here evt->event_handler is NULL.
580 * Check for event_handler being NULL and discard the interrupt as
581 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800583 if (!evt->event_handler) {
584 printk(KERN_WARNING
585 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
586 /* Switch it off */
587 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
588 return;
589 }
590
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100591 /*
592 * the NMI deadlock-detector uses this.
593 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800594 per_cpu(irq_stat, cpu).apic_timer_irqs++;
595
596 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599/*
600 * Local APIC timer interrupt. This is the most natural way for doing
601 * local interrupts, but local timer interrupts can be emulated by
602 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
603 *
604 * [ if a single-CPU system runs an SMP kernel then we call the local
605 * interrupt as well. Thus we cannot inline the local irq ... ]
606 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100607void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
David Howells7d12e782006-10-05 14:55:46 +0100609 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
611 /*
612 * NOTE! We'd better ACK the irq immediately,
613 * because timer handling can be slow.
614 */
615 ack_APIC_irq();
616 /*
617 * update_process_times() expects us to have done irq_enter().
618 * Besides, if we don't timer interrupts ignore the global
619 * interrupt lock, which is the WrongThing (tm) to do.
620 */
621 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800622 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800624
David Howells7d12e782006-10-05 14:55:46 +0100625 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626}
627
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100628int setup_profiling_timer(unsigned int multiplier)
629{
630 return -EINVAL;
631}
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633/*
Robert Richtere319e762008-02-13 16:19:36 +0100634 * Setup extended LVT, AMD specific (K8, family 10h)
635 *
636 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
637 * MCE interrupts are supported. Thus MCE offset must be set to 0.
638 */
639
640#define APIC_EILVT_LVTOFF_MCE 0
641#define APIC_EILVT_LVTOFF_IBS 1
642
643static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
644{
645 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
646 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
647 apic_write(reg, v);
648}
649
650u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
651{
652 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
653 return APIC_EILVT_LVTOFF_MCE;
654}
655
656u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
657{
658 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
659 return APIC_EILVT_LVTOFF_IBS;
660}
661
662/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800663 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800665
666/**
667 * clear_local_APIC - shutdown the local APIC
668 *
669 * This is called, when a CPU is disabled and before rebooting, so the state of
670 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
671 * leftovers during boot.
672 */
673void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Andi Kleend3432892008-01-30 13:33:17 +0100675 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100676 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Andi Kleend3432892008-01-30 13:33:17 +0100678 /* APIC hasn't been mapped yet */
679 if (!apic_phys)
680 return;
681
682 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800684 * Masking an LVT entry can trigger a local APIC error
685 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800687 if (maxlvt >= 3) {
688 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
689 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
690 }
691 /*
692 * Careful: we have to set masks only first to deassert
693 * any level-triggered sources.
694 */
695 v = apic_read(APIC_LVTT);
696 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
697 v = apic_read(APIC_LVT0);
698 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
699 v = apic_read(APIC_LVT1);
700 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
701 if (maxlvt >= 4) {
702 v = apic_read(APIC_LVTPC);
703 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800706 /* lets not touch this if we didn't frob it */
707#ifdef CONFIG_X86_MCE_P4THERMAL
708 if (maxlvt >= 5) {
709 v = apic_read(APIC_LVTTHMR);
710 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
711 }
712#endif
713 /*
714 * Clean APIC state for other OSs:
715 */
716 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
717 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
718 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
719 if (maxlvt >= 3)
720 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
721 if (maxlvt >= 4)
722 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
723
724#ifdef CONFIG_X86_MCE_P4THERMAL
725 if (maxlvt >= 5)
726 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
727#endif
728 /* Integrated APIC (!82489DX) ? */
729 if (lapic_is_integrated()) {
730 if (maxlvt > 3)
731 /* Clear ESR due to Pentium errata 3AP and 11AP */
732 apic_write(APIC_ESR, 0);
733 apic_read(APIC_ESR);
734 }
735}
736
737/**
738 * disable_local_APIC - clear and disable the local APIC
739 */
740void disable_local_APIC(void)
741{
742 unsigned long value;
743
744 clear_local_APIC();
745
746 /*
747 * Disable APIC (implies clearing of registers
748 * for 82489DX!).
749 */
750 value = apic_read(APIC_SPIV);
751 value &= ~APIC_SPIV_APIC_ENABLED;
752 apic_write_around(APIC_SPIV, value);
753
754 /*
755 * When LAPIC was disabled by the BIOS and enabled by the kernel,
756 * restore the disabled state.
757 */
758 if (enabled_via_apicbase) {
759 unsigned int l, h;
760
761 rdmsr(MSR_IA32_APICBASE, l, h);
762 l &= ~MSR_IA32_APICBASE_ENABLE;
763 wrmsr(MSR_IA32_APICBASE, l, h);
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
767/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800768 * If Linux enabled the LAPIC against the BIOS default disable it down before
769 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
770 * not power-off. Additionally clear all LVT entries before disable_local_APIC
771 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800773void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800775 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800777 if (!cpu_has_apic)
778 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800780 local_irq_save(flags);
781 clear_local_APIC();
782
783 if (enabled_via_apicbase)
784 disable_local_APIC();
785
786 local_irq_restore(flags);
787}
788
789/*
790 * This is to verify that we're looking at a real local APIC.
791 * Check these against your board if the CPUs aren't getting
792 * started for no apparent reason.
793 */
794int __init verify_local_APIC(void)
795{
796 unsigned int reg0, reg1;
797
798 /*
799 * The version register is read-only in a real APIC.
800 */
801 reg0 = apic_read(APIC_LVR);
802 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
803 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
804 reg1 = apic_read(APIC_LVR);
805 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
806
807 /*
808 * The two version reads above should print the same
809 * numbers. If the second one is different, then we
810 * poke at a non-APIC.
811 */
812 if (reg1 != reg0)
813 return 0;
814
815 /*
816 * Check if the version looks reasonably.
817 */
818 reg1 = GET_APIC_VERSION(reg0);
819 if (reg1 == 0x00 || reg1 == 0xff)
820 return 0;
821 reg1 = lapic_get_maxlvt();
822 if (reg1 < 0x02 || reg1 == 0xff)
823 return 0;
824
825 /*
826 * The ID register is read/write in a real APIC.
827 */
828 reg0 = apic_read(APIC_ID);
829 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
830
831 /*
832 * The next two are just to see if we have sane values.
833 * They're only really relevant if we're in Virtual Wire
834 * compatibility mode, but most boxes are anymore.
835 */
836 reg0 = apic_read(APIC_LVT0);
837 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
838 reg1 = apic_read(APIC_LVT1);
839 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
840
841 return 1;
842}
843
844/**
845 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
846 */
847void __init sync_Arb_IDs(void)
848{
849 /*
850 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
851 * needed on AMD.
852 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100853 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800854 return;
855 /*
856 * Wait for idle.
857 */
858 apic_wait_icr_idle();
859
860 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
861 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
862 | APIC_DM_INIT);
863}
864
865/*
866 * An initial setup of the virtual wire mode.
867 */
868void __init init_bsp_APIC(void)
869{
870 unsigned long value;
871
872 /*
873 * Don't do the setup now if we have a SMP BIOS as the
874 * through-I/O-APIC virtual wire mode might be active.
875 */
876 if (smp_found_config || !cpu_has_apic)
877 return;
878
879 /*
880 * Do not trust the local APIC being empty at bootup.
881 */
882 clear_local_APIC();
883
884 /*
885 * Enable APIC.
886 */
887 value = apic_read(APIC_SPIV);
888 value &= ~APIC_VECTOR_MASK;
889 value |= APIC_SPIV_APIC_ENABLED;
890
891 /* This bit is reserved on P4/Xeon and should be cleared */
892 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
893 (boot_cpu_data.x86 == 15))
894 value &= ~APIC_SPIV_FOCUS_DISABLED;
895 else
896 value |= APIC_SPIV_FOCUS_DISABLED;
897 value |= SPURIOUS_APIC_VECTOR;
898 apic_write_around(APIC_SPIV, value);
899
900 /*
901 * Set up the virtual wire mode.
902 */
903 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
904 value = APIC_DM_NMI;
905 if (!lapic_is_integrated()) /* 82489DX */
906 value |= APIC_LVT_LEVEL_TRIGGER;
907 apic_write_around(APIC_LVT1, value);
908}
909
Ingo Molnara4928cf2008-04-23 13:20:56 +0200910static void __cpuinit lapic_setup_esr(void)
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300911{
912 unsigned long oldvalue, value, maxlvt;
913 if (lapic_is_integrated() && !esr_disable) {
914 /* !82489DX */
915 maxlvt = lapic_get_maxlvt();
916 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
917 apic_write(APIC_ESR, 0);
918 oldvalue = apic_read(APIC_ESR);
919
920 /* enables sending errors */
921 value = ERROR_APIC_VECTOR;
922 apic_write_around(APIC_LVTERR, value);
923 /*
924 * spec says clear errors after enabling vector.
925 */
926 if (maxlvt > 3)
927 apic_write(APIC_ESR, 0);
928 value = apic_read(APIC_ESR);
929 if (value != oldvalue)
930 apic_printk(APIC_VERBOSE, "ESR value before enabling "
931 "vector: 0x%08lx after: 0x%08lx\n",
932 oldvalue, value);
933 } else {
934 if (esr_disable)
935 /*
936 * Something untraceable is creating bad interrupts on
937 * secondary quads ... for the moment, just leave the
938 * ESR disabled - we can't do anything useful with the
939 * errors anyway - mbligh
940 */
941 printk(KERN_INFO "Leaving ESR disabled.\n");
942 else
943 printk(KERN_INFO "No ESR for 82489DX.\n");
944 }
945}
946
947
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800948/**
949 * setup_local_APIC - setup the local APIC
950 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100951void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800952{
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300953 unsigned long value, integrated;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800954 int i, j;
955
956 /* Pound the ESR really hard over the head with a big hammer - mbligh */
957 if (esr_disable) {
958 apic_write(APIC_ESR, 0);
959 apic_write(APIC_ESR, 0);
960 apic_write(APIC_ESR, 0);
961 apic_write(APIC_ESR, 0);
962 }
963
964 integrated = lapic_is_integrated();
965
966 /*
967 * Double-check whether this APIC is really registered.
968 */
969 if (!apic_id_registered())
970 BUG();
971
972 /*
973 * Intel recommends to set DFR, LDR and TPR before enabling
974 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
975 * document number 292116). So here it goes...
976 */
977 init_apic_ldr();
978
979 /*
980 * Set Task Priority to 'accept all'. We never change this
981 * later on.
982 */
983 value = apic_read(APIC_TASKPRI);
984 value &= ~APIC_TPRI_MASK;
985 apic_write_around(APIC_TASKPRI, value);
986
987 /*
988 * After a crash, we no longer service the interrupts and a pending
989 * interrupt from previous kernel might still have ISR bit set.
990 *
991 * Most probably by now CPU has serviced that pending interrupt and
992 * it might not have done the ack_APIC_irq() because it thought,
993 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
994 * does not clear the ISR bit and cpu thinks it has already serivced
995 * the interrupt. Hence a vector might get locked. It was noticed
996 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
997 */
998 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
999 value = apic_read(APIC_ISR + i*0x10);
1000 for (j = 31; j >= 0; j--) {
1001 if (value & (1<<j))
1002 ack_APIC_irq();
1003 }
1004 }
1005
1006 /*
1007 * Now that we are all set up, enable the APIC
1008 */
1009 value = apic_read(APIC_SPIV);
1010 value &= ~APIC_VECTOR_MASK;
1011 /*
1012 * Enable APIC
1013 */
1014 value |= APIC_SPIV_APIC_ENABLED;
1015
1016 /*
1017 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1018 * certain networking cards. If high frequency interrupts are
1019 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1020 * entry is masked/unmasked at a high rate as well then sooner or
1021 * later IOAPIC line gets 'stuck', no more interrupts are received
1022 * from the device. If focus CPU is disabled then the hang goes
1023 * away, oh well :-(
1024 *
1025 * [ This bug can be reproduced easily with a level-triggered
1026 * PCI Ne2000 networking cards and PII/PIII processors, dual
1027 * BX chipset. ]
1028 */
1029 /*
1030 * Actually disabling the focus CPU check just makes the hang less
1031 * frequent as it makes the interrupt distributon model be more
1032 * like LRU than MRU (the short-term load is more even across CPUs).
1033 * See also the comment in end_level_ioapic_irq(). --macro
1034 */
1035
1036 /* Enable focus processor (bit==0) */
1037 value &= ~APIC_SPIV_FOCUS_DISABLED;
1038
1039 /*
1040 * Set spurious IRQ vector
1041 */
1042 value |= SPURIOUS_APIC_VECTOR;
1043 apic_write_around(APIC_SPIV, value);
1044
1045 /*
1046 * Set up LVT0, LVT1:
1047 *
1048 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001049 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001050 * we delegate interrupts to the 8259A.
1051 */
1052 /*
1053 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1054 */
1055 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1056 if (!smp_processor_id() && (pic_mode || !value)) {
1057 value = APIC_DM_EXTINT;
1058 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1059 smp_processor_id());
1060 } else {
1061 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1062 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1063 smp_processor_id());
1064 }
1065 apic_write_around(APIC_LVT0, value);
1066
1067 /*
1068 * only the BP should see the LINT1 NMI signal, obviously.
1069 */
1070 if (!smp_processor_id())
1071 value = APIC_DM_NMI;
1072 else
1073 value = APIC_DM_NMI | APIC_LVT_MASKED;
1074 if (!integrated) /* 82489DX */
1075 value |= APIC_LVT_LEVEL_TRIGGER;
1076 apic_write_around(APIC_LVT1, value);
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001077}
1078
1079void __cpuinit end_local_APIC_setup(void)
1080{
1081 unsigned long value;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001082
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001083 lapic_setup_esr();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001084 /* Disable the local apic timer */
1085 value = apic_read(APIC_LVTT);
1086 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1087 apic_write_around(APIC_LVTT, value);
1088
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001089 setup_apic_nmi_watchdog(NULL);
1090 apic_pm_activate();
1091}
1092
1093/*
1094 * Detect and initialize APIC
1095 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001096static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001097{
1098 u32 h, l, features;
1099
1100 /* Disabled by kernel option? */
1101 if (enable_local_apic < 0)
1102 return -1;
1103
1104 switch (boot_cpu_data.x86_vendor) {
1105 case X86_VENDOR_AMD:
1106 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1107 (boot_cpu_data.x86 == 15))
1108 break;
1109 goto no_apic;
1110 case X86_VENDOR_INTEL:
1111 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1112 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1113 break;
1114 goto no_apic;
1115 default:
1116 goto no_apic;
1117 }
1118
1119 if (!cpu_has_apic) {
1120 /*
1121 * Over-ride BIOS and try to enable the local APIC only if
1122 * "lapic" specified.
1123 */
1124 if (enable_local_apic <= 0) {
1125 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1126 "you can enable it with \"lapic\"\n");
1127 return -1;
1128 }
1129 /*
1130 * Some BIOSes disable the local APIC in the APIC_BASE
1131 * MSR. This can only be done in software for Intel P6 or later
1132 * and AMD K7 (Model > 1) or later.
1133 */
1134 rdmsr(MSR_IA32_APICBASE, l, h);
1135 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1136 printk(KERN_INFO
1137 "Local APIC disabled by BIOS -- reenabling.\n");
1138 l &= ~MSR_IA32_APICBASE_BASE;
1139 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1140 wrmsr(MSR_IA32_APICBASE, l, h);
1141 enabled_via_apicbase = 1;
1142 }
1143 }
1144 /*
1145 * The APIC feature bit should now be enabled
1146 * in `cpuid'
1147 */
1148 features = cpuid_edx(1);
1149 if (!(features & (1 << X86_FEATURE_APIC))) {
1150 printk(KERN_WARNING "Could not enable APIC!\n");
1151 return -1;
1152 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001153 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001154 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1155
1156 /* The BIOS may have set up the APIC at some other address */
1157 rdmsr(MSR_IA32_APICBASE, l, h);
1158 if (l & MSR_IA32_APICBASE_ENABLE)
1159 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1160
Daniel Gollub0328ece2007-08-15 02:40:35 +02001161 if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001162 nmi_watchdog = NMI_LOCAL_APIC;
1163
1164 printk(KERN_INFO "Found and enabled local APIC!\n");
1165
1166 apic_pm_activate();
1167
1168 return 0;
1169
1170no_apic:
1171 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1172 return -1;
1173}
1174
1175/**
1176 * init_apic_mappings - initialize APIC mappings
1177 */
1178void __init init_apic_mappings(void)
1179{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001180 /*
1181 * If no local APIC can be found then set up a fake all
1182 * zeroes page to simulate the local APIC and another
1183 * one for the IO-APIC.
1184 */
1185 if (!smp_found_config && detect_init_APIC()) {
1186 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1187 apic_phys = __pa(apic_phys);
1188 } else
1189 apic_phys = mp_lapic_addr;
1190
1191 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1192 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1193 apic_phys);
1194
1195 /*
1196 * Fetch the APIC ID of the BSP in case we have a
1197 * default configuration (or the MP table is broken).
1198 */
1199 if (boot_cpu_physical_apicid == -1U)
Jack Steiner05f2d122008-03-28 14:12:02 -05001200 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001201
1202#ifdef CONFIG_X86_IO_APIC
1203 {
1204 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1205 int i;
1206
1207 for (i = 0; i < nr_ioapics; i++) {
1208 if (smp_found_config) {
1209 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1210 if (!ioapic_phys) {
1211 printk(KERN_ERR
1212 "WARNING: bogus zero IO-APIC "
1213 "address found in MPTABLE, "
1214 "disabling IO/APIC support!\n");
1215 smp_found_config = 0;
1216 skip_ioapic_setup = 1;
1217 goto fake_ioapic_page;
1218 }
1219 } else {
1220fake_ioapic_page:
1221 ioapic_phys = (unsigned long)
1222 alloc_bootmem_pages(PAGE_SIZE);
1223 ioapic_phys = __pa(ioapic_phys);
1224 }
1225 set_fixmap_nocache(idx, ioapic_phys);
1226 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1227 __fix_to_virt(idx), ioapic_phys);
1228 idx++;
1229 }
1230 }
1231#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
1234/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001235 * This initializes the IO-APIC and APIC hardware if this is
1236 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
Alexey Starikovskiye81b2c62008-03-27 23:54:31 +03001238
1239int apic_version[MAX_APICS];
1240
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001241int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001243 if (enable_local_apic < 0)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001244 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001245
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001246 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001247 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001250 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001252 if (!cpu_has_apic &&
1253 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001255 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001256 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 return -1;
1258 }
1259
1260 verify_local_APIC();
1261
1262 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001263
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001264 /*
1265 * Hack: In case of kdump, after a crash, kernel might be booting
1266 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1267 * might be zero if read from MP tables. Get it from LAPIC.
1268 */
1269#ifdef CONFIG_CRASH_DUMP
Jack Steiner05f2d122008-03-28 14:12:02 -05001270 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001271#endif
Jack Steinerb6df1b82008-06-19 21:51:05 -05001272 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001273
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 setup_local_APIC();
1275
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001276 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001278 if (smp_found_config)
1279 if (!skip_ioapic_setup && nr_ioapics)
1280 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001282 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001283
1284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001286
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001287/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001288 * Local APIC interrupts
1289 */
1290
1291/*
1292 * This interrupt should _never_ happen with our APIC/SMP architecture
1293 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001294void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001295{
1296 unsigned long v;
1297
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001298 irq_enter();
1299 /*
1300 * Check if this really is a spurious interrupt and ACK it
1301 * if it is a vectored one. Just in case...
1302 * Spurious interrupts should not be ACKed.
1303 */
1304 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1305 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1306 ack_APIC_irq();
1307
1308 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1309 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1310 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001311 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001312 irq_exit();
1313}
1314
1315/*
1316 * This interrupt should never happen with our APIC/SMP architecture
1317 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001318void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001319{
1320 unsigned long v, v1;
1321
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001322 irq_enter();
1323 /* First tickle the hardware, only then report what went on. -- REW */
1324 v = apic_read(APIC_ESR);
1325 apic_write(APIC_ESR, 0);
1326 v1 = apic_read(APIC_ESR);
1327 ack_APIC_irq();
1328 atomic_inc(&irq_err_count);
1329
1330 /* Here is what the APIC error bits mean:
1331 0: Send CS error
1332 1: Receive CS error
1333 2: Send accept error
1334 3: Receive accept error
1335 4: Reserved
1336 5: Send illegal vector
1337 6: Received illegal vector
1338 7: Illegal register address
1339 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001340 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001341 smp_processor_id(), v , v1);
1342 irq_exit();
1343}
1344
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001345#ifdef CONFIG_SMP
1346void __init smp_intr_init(void)
1347{
1348 /*
1349 * IRQ0 must be given a fixed assignment and initialized,
1350 * because it's used before the IO-APIC is set up.
1351 */
1352 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1353
1354 /*
1355 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1356 * IPI, driven by wakeup.
1357 */
Alan Mayer305b92a2008-04-15 15:36:56 -05001358 alloc_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001359
1360 /* IPI for invalidation */
Alan Mayer305b92a2008-04-15 15:36:56 -05001361 alloc_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001362
1363 /* IPI for generic function call */
Alan Mayer305b92a2008-04-15 15:36:56 -05001364 alloc_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001365}
1366#endif
1367
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001368/*
1369 * Initialize APIC interrupts
1370 */
1371void __init apic_intr_init(void)
1372{
1373#ifdef CONFIG_SMP
1374 smp_intr_init();
1375#endif
1376 /* self generated IPI for local APIC timer */
Alan Mayer305b92a2008-04-15 15:36:56 -05001377 alloc_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001378
1379 /* IPI vectors for APIC spurious and error interrupts */
Alan Mayer305b92a2008-04-15 15:36:56 -05001380 alloc_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1381 alloc_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001382
1383 /* thermal monitor LVT interrupt */
1384#ifdef CONFIG_X86_MCE_P4THERMAL
Alan Mayer305b92a2008-04-15 15:36:56 -05001385 alloc_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001386#endif
1387}
1388
1389/**
1390 * connect_bsp_APIC - attach the APIC to the interrupt system
1391 */
1392void __init connect_bsp_APIC(void)
1393{
1394 if (pic_mode) {
1395 /*
1396 * Do not trust the local APIC being empty at bootup.
1397 */
1398 clear_local_APIC();
1399 /*
1400 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1401 * local APIC to INT and NMI lines.
1402 */
1403 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1404 "enabling APIC mode.\n");
1405 outb(0x70, 0x22);
1406 outb(0x01, 0x23);
1407 }
1408 enable_apic_mode();
1409}
1410
1411/**
1412 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1413 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1414 *
1415 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1416 * APIC is disabled.
1417 */
1418void disconnect_bsp_APIC(int virt_wire_setup)
1419{
1420 if (pic_mode) {
1421 /*
1422 * Put the board back into PIC mode (has an effect only on
1423 * certain older boards). Note that APIC interrupts, including
1424 * IPIs, won't work beyond this point! The only exception are
1425 * INIT IPIs.
1426 */
1427 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1428 "entering PIC mode.\n");
1429 outb(0x70, 0x22);
1430 outb(0x00, 0x23);
1431 } else {
1432 /* Go back to Virtual Wire compatibility mode */
1433 unsigned long value;
1434
1435 /* For the spurious interrupt use vector F, and enable it */
1436 value = apic_read(APIC_SPIV);
1437 value &= ~APIC_VECTOR_MASK;
1438 value |= APIC_SPIV_APIC_ENABLED;
1439 value |= 0xf;
1440 apic_write_around(APIC_SPIV, value);
1441
1442 if (!virt_wire_setup) {
1443 /*
1444 * For LVT0 make it edge triggered, active high,
1445 * external and enabled
1446 */
1447 value = apic_read(APIC_LVT0);
1448 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1449 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001450 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001451 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1452 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1453 apic_write_around(APIC_LVT0, value);
1454 } else {
1455 /* Disable LVT0 */
1456 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1457 }
1458
1459 /*
1460 * For LVT1 make it edge triggered, active high, nmi and
1461 * enabled
1462 */
1463 value = apic_read(APIC_LVT1);
1464 value &= ~(
1465 APIC_MODE_MASK | APIC_SEND_PENDING |
1466 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1467 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1468 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1469 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1470 apic_write_around(APIC_LVT1, value);
1471 }
1472}
1473
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001474unsigned int __cpuinitdata maxcpus = NR_CPUS;
1475
1476void __cpuinit generic_processor_info(int apicid, int version)
1477{
1478 int cpu;
1479 cpumask_t tmp_map;
1480 physid_mask_t phys_cpu;
1481
1482 /*
1483 * Validate version
1484 */
1485 if (version == 0x0) {
1486 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1487 "fixing up to 0x10. (tell your hw vendor)\n",
1488 version);
1489 version = 0x10;
1490 }
1491 apic_version[apicid] = version;
1492
1493 phys_cpu = apicid_to_cpu_present(apicid);
1494 physids_or(phys_cpu_present_map, phys_cpu_present_map, phys_cpu);
1495
1496 if (num_processors >= NR_CPUS) {
1497 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1498 " Processor ignored.\n", NR_CPUS);
1499 return;
1500 }
1501
1502 if (num_processors >= maxcpus) {
1503 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1504 " Processor ignored.\n", maxcpus);
1505 return;
1506 }
1507
1508 num_processors++;
1509 cpus_complement(tmp_map, cpu_present_map);
1510 cpu = first_cpu(tmp_map);
1511
1512 if (apicid == boot_cpu_physical_apicid)
1513 /*
1514 * x86_bios_cpu_apicid is required to have processors listed
1515 * in same order as logical cpu numbers. Hence the first
1516 * entry is BSP, and so on.
1517 */
1518 cpu = 0;
1519
1520 /*
1521 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1522 * but we need to work other dependencies like SMP_SUSPEND etc
1523 * before this can be done without some confusion.
1524 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1525 * - Ashok Raj <ashok.raj@intel.com>
1526 */
1527 if (num_processors > 8) {
1528 switch (boot_cpu_data.x86_vendor) {
1529 case X86_VENDOR_INTEL:
1530 if (!APIC_XAPIC(version)) {
1531 def_to_bigsmp = 0;
1532 break;
1533 }
1534 /* If P4 and above fall through */
1535 case X86_VENDOR_AMD:
1536 def_to_bigsmp = 1;
1537 }
1538 }
1539#ifdef CONFIG_SMP
1540 /* are we being called early in kernel startup? */
1541 if (x86_cpu_to_apicid_early_ptr) {
1542 u16 *cpu_to_apicid = x86_cpu_to_apicid_early_ptr;
1543 u16 *bios_cpu_apicid = x86_bios_cpu_apicid_early_ptr;
1544
1545 cpu_to_apicid[cpu] = apicid;
1546 bios_cpu_apicid[cpu] = apicid;
1547 } else {
1548 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1549 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1550 }
1551#endif
1552 cpu_set(cpu, cpu_possible_map);
1553 cpu_set(cpu, cpu_present_map);
1554}
1555
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001556/*
1557 * Power management
1558 */
1559#ifdef CONFIG_PM
1560
1561static struct {
1562 int active;
1563 /* r/w apic fields */
1564 unsigned int apic_id;
1565 unsigned int apic_taskpri;
1566 unsigned int apic_ldr;
1567 unsigned int apic_dfr;
1568 unsigned int apic_spiv;
1569 unsigned int apic_lvtt;
1570 unsigned int apic_lvtpc;
1571 unsigned int apic_lvt0;
1572 unsigned int apic_lvt1;
1573 unsigned int apic_lvterr;
1574 unsigned int apic_tmict;
1575 unsigned int apic_tdcr;
1576 unsigned int apic_thmr;
1577} apic_pm_state;
1578
1579static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1580{
1581 unsigned long flags;
1582 int maxlvt;
1583
1584 if (!apic_pm_state.active)
1585 return 0;
1586
1587 maxlvt = lapic_get_maxlvt();
1588
1589 apic_pm_state.apic_id = apic_read(APIC_ID);
1590 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1591 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1592 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1593 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1594 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1595 if (maxlvt >= 4)
1596 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1597 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1598 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1599 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1600 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1601 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1602#ifdef CONFIG_X86_MCE_P4THERMAL
1603 if (maxlvt >= 5)
1604 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1605#endif
1606
1607 local_irq_save(flags);
1608 disable_local_APIC();
1609 local_irq_restore(flags);
1610 return 0;
1611}
1612
1613static int lapic_resume(struct sys_device *dev)
1614{
1615 unsigned int l, h;
1616 unsigned long flags;
1617 int maxlvt;
1618
1619 if (!apic_pm_state.active)
1620 return 0;
1621
1622 maxlvt = lapic_get_maxlvt();
1623
1624 local_irq_save(flags);
1625
1626 /*
1627 * Make sure the APICBASE points to the right address
1628 *
1629 * FIXME! This will be wrong if we ever support suspend on
1630 * SMP! We'll need to do this as part of the CPU restore!
1631 */
1632 rdmsr(MSR_IA32_APICBASE, l, h);
1633 l &= ~MSR_IA32_APICBASE_BASE;
1634 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1635 wrmsr(MSR_IA32_APICBASE, l, h);
1636
1637 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1638 apic_write(APIC_ID, apic_pm_state.apic_id);
1639 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1640 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1641 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1642 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1643 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1644 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1645#ifdef CONFIG_X86_MCE_P4THERMAL
1646 if (maxlvt >= 5)
1647 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1648#endif
1649 if (maxlvt >= 4)
1650 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1651 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1652 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1653 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1654 apic_write(APIC_ESR, 0);
1655 apic_read(APIC_ESR);
1656 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1657 apic_write(APIC_ESR, 0);
1658 apic_read(APIC_ESR);
1659 local_irq_restore(flags);
1660 return 0;
1661}
1662
1663/*
1664 * This device has no shutdown method - fully functioning local APICs
1665 * are needed on every CPU up until machine_halt/restart/poweroff.
1666 */
1667
1668static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001669 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001670 .resume = lapic_resume,
1671 .suspend = lapic_suspend,
1672};
1673
1674static struct sys_device device_lapic = {
1675 .id = 0,
1676 .cls = &lapic_sysclass,
1677};
1678
1679static void __devinit apic_pm_activate(void)
1680{
1681 apic_pm_state.active = 1;
1682}
1683
1684static int __init init_lapic_sysfs(void)
1685{
1686 int error;
1687
1688 if (!cpu_has_apic)
1689 return 0;
1690 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1691
1692 error = sysdev_class_register(&lapic_sysclass);
1693 if (!error)
1694 error = sysdev_register(&device_lapic);
1695 return error;
1696}
1697device_initcall(init_lapic_sysfs);
1698
1699#else /* CONFIG_PM */
1700
1701static void apic_pm_activate(void) { }
1702
1703#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001704
1705/*
1706 * APIC command line parameters
1707 */
1708static int __init parse_lapic(char *arg)
1709{
1710 enable_local_apic = 1;
1711 return 0;
1712}
1713early_param("lapic", parse_lapic);
1714
1715static int __init parse_nolapic(char *arg)
1716{
1717 enable_local_apic = -1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001718 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001719 return 0;
1720}
1721early_param("nolapic", parse_nolapic);
1722
1723static int __init parse_disable_lapic_timer(char *arg)
1724{
1725 local_apic_timer_disabled = 1;
1726 return 0;
1727}
1728early_param("nolapic_timer", parse_disable_lapic_timer);
1729
1730static int __init parse_lapic_timer_c2_ok(char *arg)
1731{
1732 local_apic_timer_c2_ok = 1;
1733 return 0;
1734}
1735early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1736
1737static int __init apic_set_verbosity(char *str)
1738{
1739 if (strcmp("debug", str) == 0)
1740 apic_verbosity = APIC_DEBUG;
1741 else if (strcmp("verbose", str) == 0)
1742 apic_verbosity = APIC_VERBOSE;
1743 return 1;
1744}
1745__setup("apic=", apic_set_verbosity);
1746