blob: fa1be1d62916a833beb8de04f91a7bc54602e775 [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
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080079static unsigned int calibration_result;
80
81static int lapic_next_event(unsigned long delta,
82 struct clock_event_device *evt);
83static void lapic_timer_setup(enum clock_event_mode mode,
84 struct clock_event_device *evt);
85static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static void apic_pm_activate(void);
87
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080088/*
89 * The local apic timer can be used for any function which is CPU local.
90 */
91static struct clock_event_device lapic_clockevent = {
92 .name = "lapic",
93 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080094 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080095 .shift = 32,
96 .set_mode = lapic_timer_setup,
97 .set_next_event = lapic_next_event,
98 .broadcast = lapic_timer_broadcast,
99 .rating = 100,
100 .irq = -1,
101};
102static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800104/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static int enabled_via_apicbase;
106
Andi Kleend3432892008-01-30 13:33:17 +0100107static unsigned long apic_phys;
108
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800109/*
110 * Get the LAPIC version
111 */
112static inline int lapic_get_version(void)
113{
114 return GET_APIC_VERSION(apic_read(APIC_LVR));
115}
116
117/*
Joe Perchesab4a5742008-01-30 13:31:42 +0100118 * Check, if the APIC is integrated or a separate chip
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800119 */
120static inline int lapic_is_integrated(void)
121{
122 return APIC_INTEGRATED(lapic_get_version());
123}
124
125/*
126 * Check, whether this is a modern or a first generation APIC
127 */
128static int modern_apic(void)
129{
130 /* AMD systems use old APIC versions, so check the CPU */
131 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
132 boot_cpu_data.x86 >= 0xf)
133 return 1;
134 return lapic_get_version() >= 0x14;
135}
136
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200137void apic_wait_icr_idle(void)
138{
139 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
140 cpu_relax();
141}
142
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100143u32 safe_apic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200144{
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100145 u32 send_status;
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200146 int timeout;
147
148 timeout = 0;
149 do {
150 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
151 if (!send_status)
152 break;
153 udelay(100);
154 } while (timeout++ < 1000);
155
156 return send_status;
157}
158
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800159/**
160 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
161 */
Jan Beuliche9427102008-01-30 13:31:24 +0100162void __cpuinit enable_NMI_through_LVT0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800164 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800166 /* Level triggered for 82489DX */
167 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 v |= APIC_LVT_LEVEL_TRIGGER;
169 apic_write_around(APIC_LVT0, v);
170}
171
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800172/**
173 * get_physical_broadcast - Get number of physical broadcast IDs
174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175int get_physical_broadcast(void)
176{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800177 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178}
179
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800180/**
181 * lapic_get_maxlvt - get the maximum number of local vector table entries
182 */
183int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800185 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800188 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
191/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800192 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800195/* Clock divisor is set to 16 */
196#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198/*
199 * This function sets up the local APIC timer, with a timeout of
200 * 'clocks' APIC bus clock. During calibration we actually call
201 * this function twice on the boot CPU, once with a bogus timeout
202 * value, second time for real. The other (noncalibrating) CPUs
203 * call this function only once, with the real, calibrated value.
204 *
205 * We do reads before writes even if unnecessary, to get around the
206 * P5 APIC double write bug.
207 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800208static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800210 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800212 lvtt_value = LOCAL_TIMER_VECTOR;
213 if (!oneshot)
214 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800215 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100217
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800218 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100219 lvtt_value |= APIC_LVT_MASKED;
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 apic_write_around(APIC_LVTT, lvtt_value);
222
223 /*
224 * Divide PICLK by 16
225 */
226 tmp_value = apic_read(APIC_TDCR);
227 apic_write_around(APIC_TDCR, (tmp_value
228 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
229 | APIC_TDR_DIV_16);
230
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800231 if (!oneshot)
232 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800235/*
236 * Program the next event, relative to now
237 */
238static int lapic_next_event(unsigned long delta,
239 struct clock_event_device *evt)
240{
241 apic_write_around(APIC_TMICT, delta);
242 return 0;
243}
244
245/*
246 * Setup the lapic timer in periodic or oneshot mode
247 */
248static void lapic_timer_setup(enum clock_event_mode mode,
249 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800252 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800254 /* Lapic used for broadcast ? */
255 if (!local_apic_timer_verify_ok)
256 return;
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 local_irq_save(flags);
259
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800260 switch (mode) {
261 case CLOCK_EVT_MODE_PERIODIC:
262 case CLOCK_EVT_MODE_ONESHOT:
263 __setup_APIC_LVTT(calibration_result,
264 mode != CLOCK_EVT_MODE_PERIODIC, 1);
265 break;
266 case CLOCK_EVT_MODE_UNUSED:
267 case CLOCK_EVT_MODE_SHUTDOWN:
268 v = apic_read(APIC_LVTT);
269 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
270 apic_write_around(APIC_LVTT, v);
271 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700272 case CLOCK_EVT_MODE_RESUME:
273 /* Nothing to do here */
274 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800275 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 local_irq_restore(flags);
278}
279
280/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800281 * Local APIC timer broadcast function
282 */
283static void lapic_timer_broadcast(cpumask_t mask)
284{
285#ifdef CONFIG_SMP
286 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
287#endif
288}
289
290/*
291 * Setup the local APIC timer for this CPU. Copy the initilized values
292 * of the boot CPU and register the clock event in the framework.
293 */
294static void __devinit setup_APIC_timer(void)
295{
296 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
297
298 memcpy(levt, &lapic_clockevent, sizeof(*levt));
299 levt->cpumask = cpumask_of_cpu(smp_processor_id());
300
301 clockevents_register_device(levt);
302}
303
304/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800305 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800307 * We want to do the calibration only once since we want to have local timer
308 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
309 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800310 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800311 * This was previously done by reading the PIT/HPET and waiting for a wrap
312 * around to find out, that a tick has elapsed. I have a box, where the PIT
313 * readout is broken, so it never gets out of the wait loop again. This was
314 * also reported by others.
315 *
316 * Monitoring the jiffies value is inaccurate and the clockevents
317 * infrastructure allows us to do a simple substitution of the interrupt
318 * handler.
319 *
320 * The calibration routine also uses the pm_timer when possible, as the PIT
321 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
322 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
324
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800325#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200327static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800328static __initdata long lapic_cal_t1, lapic_cal_t2;
329static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
330static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
331static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
332
333/*
334 * Temporary interrupt handler.
335 */
336static void __init lapic_cal_handler(struct clock_event_device *dev)
337{
338 unsigned long long tsc = 0;
339 long tapic = apic_read(APIC_TMCCT);
340 unsigned long pm = acpi_pm_read_early();
341
342 if (cpu_has_tsc)
343 rdtscll(tsc);
344
345 switch (lapic_cal_loops++) {
346 case 0:
347 lapic_cal_t1 = tapic;
348 lapic_cal_tsc1 = tsc;
349 lapic_cal_pm1 = pm;
350 lapic_cal_j1 = jiffies;
351 break;
352
353 case LAPIC_CAL_LOOPS:
354 lapic_cal_t2 = tapic;
355 lapic_cal_tsc2 = tsc;
356 if (pm < lapic_cal_pm1)
357 pm += ACPI_PM_OVRRUN;
358 lapic_cal_pm2 = pm;
359 lapic_cal_j2 = jiffies;
360 break;
361 }
362}
363
364/*
365 * Setup the boot APIC
366 *
367 * Calibrate and verify the result.
368 */
369void __init setup_boot_APIC_clock(void)
370{
371 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
372 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
373 const long pm_thresh = pm_100ms/100;
374 void (*real_handler)(struct clock_event_device *dev);
375 unsigned long deltaj;
376 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800377 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800378
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800379 /*
380 * The local apic timer can be disabled via the kernel
Andi Kleend3f7eae2007-08-10 22:31:07 +0200381 * commandline or from the CPU detection code. Register the lapic
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800382 * timer as a dummy clock event source on SMP systems, so the
383 * broadcast mechanism is used. On UP systems simply ignore it.
384 */
385 if (local_apic_timer_disabled) {
386 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100387 if (num_possible_cpus() > 1) {
388 lapic_clockevent.mult = 1;
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800389 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100390 }
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800391 return;
392 }
393
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800394 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
395 "calibrating APIC timer ...\n");
396
397 local_irq_disable();
398
399 /* Replace the global interrupt handler */
400 real_handler = global_clock_event->event_handler;
401 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800404 * Setup the APIC counter to 1e9. There is no way the lapic
405 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800407 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800409 /* Let the interrupts run */
410 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800412 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
413 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800415 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800417 /* Restore the real event handler */
418 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800420 /* Build delta t1-t2 as apic timer counts down */
421 delta = lapic_cal_t1 - lapic_cal_t2;
422 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800424 /* Check, if the PM timer is available */
425 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
426 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800428 if (deltapm) {
429 unsigned long mult;
430 u64 res;
431
432 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
433
434 if (deltapm > (pm_100ms - pm_thresh) &&
435 deltapm < (pm_100ms + pm_thresh)) {
436 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
437 } else {
438 res = (((u64) deltapm) * mult) >> 22;
439 do_div(res, 1000000);
440 printk(KERN_WARNING "APIC calibration not consistent "
441 "with PM Timer: %ldms instead of 100ms\n",
442 (long)res);
443 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100444 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800445 do_div(res, deltapm);
446 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
447 "%lu (%ld)\n", (unsigned long) res, delta);
448 delta = (long) res;
449 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800450 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800453 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900454 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
455 lapic_clockevent.shift);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800456 lapic_clockevent.max_delta_ns =
457 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
458 lapic_clockevent.min_delta_ns =
459 clockevent_delta2ns(0xF, &lapic_clockevent);
460
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800461 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800462
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800463 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
464 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
465 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
466 calibration_result);
467
468 if (cpu_has_tsc) {
469 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800471 "%ld.%04ld MHz.\n",
472 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
473 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800477 "%u.%04u MHz.\n",
478 calibration_result / (1000000 / HZ),
479 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800481 local_apic_timer_verify_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100483 /*
484 * Do a sanity check on the APIC calibration result
485 */
486 if (calibration_result < (1000000 / HZ)) {
487 local_irq_enable();
488 printk(KERN_WARNING
489 "APIC frequency too slow, disabling apic timer\n");
490 /* No broadcast on UP ! */
491 if (num_possible_cpus() > 1)
492 setup_APIC_timer();
493 return;
494 }
495
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800496 /* We trust the pm timer based calibration */
497 if (!pm_referenced) {
498 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800499
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800500 /*
501 * Setup the apic timer manually
502 */
503 levt->event_handler = lapic_cal_handler;
504 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
505 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800506
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800507 /* Let the interrupts run */
508 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800509
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200510 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800511 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800512
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800513 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800514
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800515 /* Stop the lapic timer */
516 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800517
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800518 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800519
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800520 /* Jiffies delta */
521 deltaj = lapic_cal_j2 - lapic_cal_j1;
522 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800523
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800524 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800525 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800526 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800527 else
528 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100529 } else
530 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800531
532 if (!local_apic_timer_verify_ok) {
533 printk(KERN_WARNING
534 "APIC timer disabled due to verification failure.\n");
535 /* No broadcast on UP ! */
536 if (num_possible_cpus() == 1)
537 return;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800538 } else {
539 /*
540 * If nmi_watchdog is set to IO_APIC, we need the
541 * PIT/HPET going. Otherwise register lapic as a dummy
542 * device.
543 */
544 if (nmi_watchdog != NMI_IO_APIC)
545 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
Ingo Molnar820de5c2007-07-21 04:37:36 -0700546 else
547 printk(KERN_WARNING "APIC timer registered as dummy,"
548 " due to nmi_watchdog=1!\n");
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800549 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800550
551 /* Setup the lapic or request the broadcast */
552 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Li Shaohua0bb31842005-06-25 14:54:55 -0700555void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800557 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800561 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800563static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800565 int cpu = smp_processor_id();
566 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800569 * Normally we should not be here till LAPIC has been initialized but
570 * in some cases like kdump, its possible that there is a pending LAPIC
571 * timer interrupt from previous kernel's context and is delivered in
572 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800574 * Interrupts are enabled early and LAPIC is setup much later, hence
575 * its possible that when we get here evt->event_handler is NULL.
576 * Check for event_handler being NULL and discard the interrupt as
577 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800579 if (!evt->event_handler) {
580 printk(KERN_WARNING
581 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
582 /* Switch it off */
583 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
584 return;
585 }
586
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100587 /*
588 * the NMI deadlock-detector uses this.
589 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800590 per_cpu(irq_stat, cpu).apic_timer_irqs++;
591
592 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
595/*
596 * Local APIC timer interrupt. This is the most natural way for doing
597 * local interrupts, but local timer interrupts can be emulated by
598 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
599 *
600 * [ if a single-CPU system runs an SMP kernel then we call the local
601 * interrupt as well. Thus we cannot inline the local irq ... ]
602 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100603void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
David Howells7d12e782006-10-05 14:55:46 +0100605 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 /*
608 * NOTE! We'd better ACK the irq immediately,
609 * because timer handling can be slow.
610 */
611 ack_APIC_irq();
612 /*
613 * update_process_times() expects us to have done irq_enter().
614 * Besides, if we don't timer interrupts ignore the global
615 * interrupt lock, which is the WrongThing (tm) to do.
616 */
617 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800618 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800620
David Howells7d12e782006-10-05 14:55:46 +0100621 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622}
623
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100624int setup_profiling_timer(unsigned int multiplier)
625{
626 return -EINVAL;
627}
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629/*
Robert Richtere319e762008-02-13 16:19:36 +0100630 * Setup extended LVT, AMD specific (K8, family 10h)
631 *
632 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
633 * MCE interrupts are supported. Thus MCE offset must be set to 0.
634 */
635
636#define APIC_EILVT_LVTOFF_MCE 0
637#define APIC_EILVT_LVTOFF_IBS 1
638
639static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
640{
641 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
642 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
643 apic_write(reg, v);
644}
645
646u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
647{
648 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
649 return APIC_EILVT_LVTOFF_MCE;
650}
651
652u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
653{
654 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
655 return APIC_EILVT_LVTOFF_IBS;
656}
657
658/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800659 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800661
662/**
663 * clear_local_APIC - shutdown the local APIC
664 *
665 * This is called, when a CPU is disabled and before rebooting, so the state of
666 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
667 * leftovers during boot.
668 */
669void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Andi Kleend3432892008-01-30 13:33:17 +0100671 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100672 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Andi Kleend3432892008-01-30 13:33:17 +0100674 /* APIC hasn't been mapped yet */
675 if (!apic_phys)
676 return;
677
678 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800680 * Masking an LVT entry can trigger a local APIC error
681 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800683 if (maxlvt >= 3) {
684 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
685 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
686 }
687 /*
688 * Careful: we have to set masks only first to deassert
689 * any level-triggered sources.
690 */
691 v = apic_read(APIC_LVTT);
692 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
693 v = apic_read(APIC_LVT0);
694 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
695 v = apic_read(APIC_LVT1);
696 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
697 if (maxlvt >= 4) {
698 v = apic_read(APIC_LVTPC);
699 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800702 /* lets not touch this if we didn't frob it */
703#ifdef CONFIG_X86_MCE_P4THERMAL
704 if (maxlvt >= 5) {
705 v = apic_read(APIC_LVTTHMR);
706 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
707 }
708#endif
709 /*
710 * Clean APIC state for other OSs:
711 */
712 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
713 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
714 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
715 if (maxlvt >= 3)
716 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
717 if (maxlvt >= 4)
718 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
719
720#ifdef CONFIG_X86_MCE_P4THERMAL
721 if (maxlvt >= 5)
722 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
723#endif
724 /* Integrated APIC (!82489DX) ? */
725 if (lapic_is_integrated()) {
726 if (maxlvt > 3)
727 /* Clear ESR due to Pentium errata 3AP and 11AP */
728 apic_write(APIC_ESR, 0);
729 apic_read(APIC_ESR);
730 }
731}
732
733/**
734 * disable_local_APIC - clear and disable the local APIC
735 */
736void disable_local_APIC(void)
737{
738 unsigned long value;
739
740 clear_local_APIC();
741
742 /*
743 * Disable APIC (implies clearing of registers
744 * for 82489DX!).
745 */
746 value = apic_read(APIC_SPIV);
747 value &= ~APIC_SPIV_APIC_ENABLED;
748 apic_write_around(APIC_SPIV, value);
749
750 /*
751 * When LAPIC was disabled by the BIOS and enabled by the kernel,
752 * restore the disabled state.
753 */
754 if (enabled_via_apicbase) {
755 unsigned int l, h;
756
757 rdmsr(MSR_IA32_APICBASE, l, h);
758 l &= ~MSR_IA32_APICBASE_ENABLE;
759 wrmsr(MSR_IA32_APICBASE, l, h);
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
762
763/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800764 * If Linux enabled the LAPIC against the BIOS default disable it down before
765 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
766 * not power-off. Additionally clear all LVT entries before disable_local_APIC
767 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800769void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800771 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800773 if (!cpu_has_apic)
774 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800776 local_irq_save(flags);
777 clear_local_APIC();
778
779 if (enabled_via_apicbase)
780 disable_local_APIC();
781
782 local_irq_restore(flags);
783}
784
785/*
786 * This is to verify that we're looking at a real local APIC.
787 * Check these against your board if the CPUs aren't getting
788 * started for no apparent reason.
789 */
790int __init verify_local_APIC(void)
791{
792 unsigned int reg0, reg1;
793
794 /*
795 * The version register is read-only in a real APIC.
796 */
797 reg0 = apic_read(APIC_LVR);
798 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
799 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
800 reg1 = apic_read(APIC_LVR);
801 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
802
803 /*
804 * The two version reads above should print the same
805 * numbers. If the second one is different, then we
806 * poke at a non-APIC.
807 */
808 if (reg1 != reg0)
809 return 0;
810
811 /*
812 * Check if the version looks reasonably.
813 */
814 reg1 = GET_APIC_VERSION(reg0);
815 if (reg1 == 0x00 || reg1 == 0xff)
816 return 0;
817 reg1 = lapic_get_maxlvt();
818 if (reg1 < 0x02 || reg1 == 0xff)
819 return 0;
820
821 /*
822 * The ID register is read/write in a real APIC.
823 */
824 reg0 = apic_read(APIC_ID);
825 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
826
827 /*
828 * The next two are just to see if we have sane values.
829 * They're only really relevant if we're in Virtual Wire
830 * compatibility mode, but most boxes are anymore.
831 */
832 reg0 = apic_read(APIC_LVT0);
833 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
834 reg1 = apic_read(APIC_LVT1);
835 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
836
837 return 1;
838}
839
840/**
841 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
842 */
843void __init sync_Arb_IDs(void)
844{
845 /*
846 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
847 * needed on AMD.
848 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100849 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800850 return;
851 /*
852 * Wait for idle.
853 */
854 apic_wait_icr_idle();
855
856 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
857 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
858 | APIC_DM_INIT);
859}
860
861/*
862 * An initial setup of the virtual wire mode.
863 */
864void __init init_bsp_APIC(void)
865{
866 unsigned long value;
867
868 /*
869 * Don't do the setup now if we have a SMP BIOS as the
870 * through-I/O-APIC virtual wire mode might be active.
871 */
872 if (smp_found_config || !cpu_has_apic)
873 return;
874
875 /*
876 * Do not trust the local APIC being empty at bootup.
877 */
878 clear_local_APIC();
879
880 /*
881 * Enable APIC.
882 */
883 value = apic_read(APIC_SPIV);
884 value &= ~APIC_VECTOR_MASK;
885 value |= APIC_SPIV_APIC_ENABLED;
886
887 /* This bit is reserved on P4/Xeon and should be cleared */
888 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
889 (boot_cpu_data.x86 == 15))
890 value &= ~APIC_SPIV_FOCUS_DISABLED;
891 else
892 value |= APIC_SPIV_FOCUS_DISABLED;
893 value |= SPURIOUS_APIC_VECTOR;
894 apic_write_around(APIC_SPIV, value);
895
896 /*
897 * Set up the virtual wire mode.
898 */
899 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
900 value = APIC_DM_NMI;
901 if (!lapic_is_integrated()) /* 82489DX */
902 value |= APIC_LVT_LEVEL_TRIGGER;
903 apic_write_around(APIC_LVT1, value);
904}
905
Ingo Molnara4928cf2008-04-23 13:20:56 +0200906static void __cpuinit lapic_setup_esr(void)
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300907{
908 unsigned long oldvalue, value, maxlvt;
909 if (lapic_is_integrated() && !esr_disable) {
910 /* !82489DX */
911 maxlvt = lapic_get_maxlvt();
912 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
913 apic_write(APIC_ESR, 0);
914 oldvalue = apic_read(APIC_ESR);
915
916 /* enables sending errors */
917 value = ERROR_APIC_VECTOR;
918 apic_write_around(APIC_LVTERR, value);
919 /*
920 * spec says clear errors after enabling vector.
921 */
922 if (maxlvt > 3)
923 apic_write(APIC_ESR, 0);
924 value = apic_read(APIC_ESR);
925 if (value != oldvalue)
926 apic_printk(APIC_VERBOSE, "ESR value before enabling "
927 "vector: 0x%08lx after: 0x%08lx\n",
928 oldvalue, value);
929 } else {
930 if (esr_disable)
931 /*
932 * Something untraceable is creating bad interrupts on
933 * secondary quads ... for the moment, just leave the
934 * ESR disabled - we can't do anything useful with the
935 * errors anyway - mbligh
936 */
937 printk(KERN_INFO "Leaving ESR disabled.\n");
938 else
939 printk(KERN_INFO "No ESR for 82489DX.\n");
940 }
941}
942
943
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800944/**
945 * setup_local_APIC - setup the local APIC
946 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100947void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800948{
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300949 unsigned long value, integrated;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800950 int i, j;
951
952 /* Pound the ESR really hard over the head with a big hammer - mbligh */
953 if (esr_disable) {
954 apic_write(APIC_ESR, 0);
955 apic_write(APIC_ESR, 0);
956 apic_write(APIC_ESR, 0);
957 apic_write(APIC_ESR, 0);
958 }
959
960 integrated = lapic_is_integrated();
961
962 /*
963 * Double-check whether this APIC is really registered.
964 */
965 if (!apic_id_registered())
966 BUG();
967
968 /*
969 * Intel recommends to set DFR, LDR and TPR before enabling
970 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
971 * document number 292116). So here it goes...
972 */
973 init_apic_ldr();
974
975 /*
976 * Set Task Priority to 'accept all'. We never change this
977 * later on.
978 */
979 value = apic_read(APIC_TASKPRI);
980 value &= ~APIC_TPRI_MASK;
981 apic_write_around(APIC_TASKPRI, value);
982
983 /*
984 * After a crash, we no longer service the interrupts and a pending
985 * interrupt from previous kernel might still have ISR bit set.
986 *
987 * Most probably by now CPU has serviced that pending interrupt and
988 * it might not have done the ack_APIC_irq() because it thought,
989 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
990 * does not clear the ISR bit and cpu thinks it has already serivced
991 * the interrupt. Hence a vector might get locked. It was noticed
992 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
993 */
994 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
995 value = apic_read(APIC_ISR + i*0x10);
996 for (j = 31; j >= 0; j--) {
997 if (value & (1<<j))
998 ack_APIC_irq();
999 }
1000 }
1001
1002 /*
1003 * Now that we are all set up, enable the APIC
1004 */
1005 value = apic_read(APIC_SPIV);
1006 value &= ~APIC_VECTOR_MASK;
1007 /*
1008 * Enable APIC
1009 */
1010 value |= APIC_SPIV_APIC_ENABLED;
1011
1012 /*
1013 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1014 * certain networking cards. If high frequency interrupts are
1015 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1016 * entry is masked/unmasked at a high rate as well then sooner or
1017 * later IOAPIC line gets 'stuck', no more interrupts are received
1018 * from the device. If focus CPU is disabled then the hang goes
1019 * away, oh well :-(
1020 *
1021 * [ This bug can be reproduced easily with a level-triggered
1022 * PCI Ne2000 networking cards and PII/PIII processors, dual
1023 * BX chipset. ]
1024 */
1025 /*
1026 * Actually disabling the focus CPU check just makes the hang less
1027 * frequent as it makes the interrupt distributon model be more
1028 * like LRU than MRU (the short-term load is more even across CPUs).
1029 * See also the comment in end_level_ioapic_irq(). --macro
1030 */
1031
1032 /* Enable focus processor (bit==0) */
1033 value &= ~APIC_SPIV_FOCUS_DISABLED;
1034
1035 /*
1036 * Set spurious IRQ vector
1037 */
1038 value |= SPURIOUS_APIC_VECTOR;
1039 apic_write_around(APIC_SPIV, value);
1040
1041 /*
1042 * Set up LVT0, LVT1:
1043 *
1044 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001045 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001046 * we delegate interrupts to the 8259A.
1047 */
1048 /*
1049 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1050 */
1051 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1052 if (!smp_processor_id() && (pic_mode || !value)) {
1053 value = APIC_DM_EXTINT;
1054 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1055 smp_processor_id());
1056 } else {
1057 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1058 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1059 smp_processor_id());
1060 }
1061 apic_write_around(APIC_LVT0, value);
1062
1063 /*
1064 * only the BP should see the LINT1 NMI signal, obviously.
1065 */
1066 if (!smp_processor_id())
1067 value = APIC_DM_NMI;
1068 else
1069 value = APIC_DM_NMI | APIC_LVT_MASKED;
1070 if (!integrated) /* 82489DX */
1071 value |= APIC_LVT_LEVEL_TRIGGER;
1072 apic_write_around(APIC_LVT1, value);
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001073}
1074
1075void __cpuinit end_local_APIC_setup(void)
1076{
1077 unsigned long value;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001078
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001079 lapic_setup_esr();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001080 /* Disable the local apic timer */
1081 value = apic_read(APIC_LVTT);
1082 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1083 apic_write_around(APIC_LVTT, value);
1084
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001085 setup_apic_nmi_watchdog(NULL);
1086 apic_pm_activate();
1087}
1088
1089/*
1090 * Detect and initialize APIC
1091 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001092static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001093{
1094 u32 h, l, features;
1095
1096 /* Disabled by kernel option? */
1097 if (enable_local_apic < 0)
1098 return -1;
1099
1100 switch (boot_cpu_data.x86_vendor) {
1101 case X86_VENDOR_AMD:
1102 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1103 (boot_cpu_data.x86 == 15))
1104 break;
1105 goto no_apic;
1106 case X86_VENDOR_INTEL:
1107 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1108 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1109 break;
1110 goto no_apic;
1111 default:
1112 goto no_apic;
1113 }
1114
1115 if (!cpu_has_apic) {
1116 /*
1117 * Over-ride BIOS and try to enable the local APIC only if
1118 * "lapic" specified.
1119 */
1120 if (enable_local_apic <= 0) {
1121 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1122 "you can enable it with \"lapic\"\n");
1123 return -1;
1124 }
1125 /*
1126 * Some BIOSes disable the local APIC in the APIC_BASE
1127 * MSR. This can only be done in software for Intel P6 or later
1128 * and AMD K7 (Model > 1) or later.
1129 */
1130 rdmsr(MSR_IA32_APICBASE, l, h);
1131 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1132 printk(KERN_INFO
1133 "Local APIC disabled by BIOS -- reenabling.\n");
1134 l &= ~MSR_IA32_APICBASE_BASE;
1135 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1136 wrmsr(MSR_IA32_APICBASE, l, h);
1137 enabled_via_apicbase = 1;
1138 }
1139 }
1140 /*
1141 * The APIC feature bit should now be enabled
1142 * in `cpuid'
1143 */
1144 features = cpuid_edx(1);
1145 if (!(features & (1 << X86_FEATURE_APIC))) {
1146 printk(KERN_WARNING "Could not enable APIC!\n");
1147 return -1;
1148 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001149 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001150 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1151
1152 /* The BIOS may have set up the APIC at some other address */
1153 rdmsr(MSR_IA32_APICBASE, l, h);
1154 if (l & MSR_IA32_APICBASE_ENABLE)
1155 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1156
Daniel Gollub0328ece2007-08-15 02:40:35 +02001157 if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001158 nmi_watchdog = NMI_LOCAL_APIC;
1159
1160 printk(KERN_INFO "Found and enabled local APIC!\n");
1161
1162 apic_pm_activate();
1163
1164 return 0;
1165
1166no_apic:
1167 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1168 return -1;
1169}
1170
1171/**
1172 * init_apic_mappings - initialize APIC mappings
1173 */
1174void __init init_apic_mappings(void)
1175{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001176 /*
1177 * If no local APIC can be found then set up a fake all
1178 * zeroes page to simulate the local APIC and another
1179 * one for the IO-APIC.
1180 */
1181 if (!smp_found_config && detect_init_APIC()) {
1182 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1183 apic_phys = __pa(apic_phys);
1184 } else
1185 apic_phys = mp_lapic_addr;
1186
1187 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1188 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1189 apic_phys);
1190
1191 /*
1192 * Fetch the APIC ID of the BSP in case we have a
1193 * default configuration (or the MP table is broken).
1194 */
1195 if (boot_cpu_physical_apicid == -1U)
Jack Steiner05f2d122008-03-28 14:12:02 -05001196 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001197
1198#ifdef CONFIG_X86_IO_APIC
1199 {
1200 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1201 int i;
1202
1203 for (i = 0; i < nr_ioapics; i++) {
1204 if (smp_found_config) {
Alexey Starikovskiyec2cd0a2008-05-14 19:03:10 +04001205 ioapic_phys = mp_ioapics[i].mp_apicaddr;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001206 if (!ioapic_phys) {
1207 printk(KERN_ERR
1208 "WARNING: bogus zero IO-APIC "
1209 "address found in MPTABLE, "
1210 "disabling IO/APIC support!\n");
1211 smp_found_config = 0;
1212 skip_ioapic_setup = 1;
1213 goto fake_ioapic_page;
1214 }
1215 } else {
1216fake_ioapic_page:
1217 ioapic_phys = (unsigned long)
1218 alloc_bootmem_pages(PAGE_SIZE);
1219 ioapic_phys = __pa(ioapic_phys);
1220 }
1221 set_fixmap_nocache(idx, ioapic_phys);
1222 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1223 __fix_to_virt(idx), ioapic_phys);
1224 idx++;
1225 }
1226 }
1227#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001231 * This initializes the IO-APIC and APIC hardware if this is
1232 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 */
Alexey Starikovskiye81b2c62008-03-27 23:54:31 +03001234
1235int apic_version[MAX_APICS];
1236
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001237int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001239 if (enable_local_apic < 0)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001240 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001241
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001242 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001243 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001246 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001248 if (!cpu_has_apic &&
1249 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001251 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001252 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return -1;
1254 }
1255
1256 verify_local_APIC();
1257
1258 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001259
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001260 /*
1261 * Hack: In case of kdump, after a crash, kernel might be booting
1262 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1263 * might be zero if read from MP tables. Get it from LAPIC.
1264 */
1265#ifdef CONFIG_CRASH_DUMP
Jack Steiner05f2d122008-03-28 14:12:02 -05001266 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001267#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001268 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 setup_local_APIC();
1271
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001272 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001274 if (smp_found_config)
1275 if (!skip_ioapic_setup && nr_ioapics)
1276 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001278 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001279
1280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001282
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001283/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001284 * Local APIC interrupts
1285 */
1286
1287/*
1288 * This interrupt should _never_ happen with our APIC/SMP architecture
1289 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001290void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001291{
1292 unsigned long v;
1293
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001294 irq_enter();
1295 /*
1296 * Check if this really is a spurious interrupt and ACK it
1297 * if it is a vectored one. Just in case...
1298 * Spurious interrupts should not be ACKed.
1299 */
1300 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1301 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1302 ack_APIC_irq();
1303
1304 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1305 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1306 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001307 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001308 irq_exit();
1309}
1310
1311/*
1312 * This interrupt should never happen with our APIC/SMP architecture
1313 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001314void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001315{
1316 unsigned long v, v1;
1317
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001318 irq_enter();
1319 /* First tickle the hardware, only then report what went on. -- REW */
1320 v = apic_read(APIC_ESR);
1321 apic_write(APIC_ESR, 0);
1322 v1 = apic_read(APIC_ESR);
1323 ack_APIC_irq();
1324 atomic_inc(&irq_err_count);
1325
1326 /* Here is what the APIC error bits mean:
1327 0: Send CS error
1328 1: Receive CS error
1329 2: Send accept error
1330 3: Receive accept error
1331 4: Reserved
1332 5: Send illegal vector
1333 6: Received illegal vector
1334 7: Illegal register address
1335 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001336 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001337 smp_processor_id(), v , v1);
1338 irq_exit();
1339}
1340
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001341#ifdef CONFIG_SMP
1342void __init smp_intr_init(void)
1343{
1344 /*
1345 * IRQ0 must be given a fixed assignment and initialized,
1346 * because it's used before the IO-APIC is set up.
1347 */
1348 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1349
1350 /*
1351 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1352 * IPI, driven by wakeup.
1353 */
1354 set_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
1355
1356 /* IPI for invalidation */
1357 set_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
1358
1359 /* IPI for generic function call */
1360 set_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
1361}
1362#endif
1363
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001364/*
1365 * Initialize APIC interrupts
1366 */
1367void __init apic_intr_init(void)
1368{
1369#ifdef CONFIG_SMP
1370 smp_intr_init();
1371#endif
1372 /* self generated IPI for local APIC timer */
1373 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1374
1375 /* IPI vectors for APIC spurious and error interrupts */
1376 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1377 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1378
1379 /* thermal monitor LVT interrupt */
1380#ifdef CONFIG_X86_MCE_P4THERMAL
1381 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1382#endif
1383}
1384
1385/**
1386 * connect_bsp_APIC - attach the APIC to the interrupt system
1387 */
1388void __init connect_bsp_APIC(void)
1389{
1390 if (pic_mode) {
1391 /*
1392 * Do not trust the local APIC being empty at bootup.
1393 */
1394 clear_local_APIC();
1395 /*
1396 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1397 * local APIC to INT and NMI lines.
1398 */
1399 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1400 "enabling APIC mode.\n");
1401 outb(0x70, 0x22);
1402 outb(0x01, 0x23);
1403 }
1404 enable_apic_mode();
1405}
1406
1407/**
1408 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1409 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1410 *
1411 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1412 * APIC is disabled.
1413 */
1414void disconnect_bsp_APIC(int virt_wire_setup)
1415{
1416 if (pic_mode) {
1417 /*
1418 * Put the board back into PIC mode (has an effect only on
1419 * certain older boards). Note that APIC interrupts, including
1420 * IPIs, won't work beyond this point! The only exception are
1421 * INIT IPIs.
1422 */
1423 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1424 "entering PIC mode.\n");
1425 outb(0x70, 0x22);
1426 outb(0x00, 0x23);
1427 } else {
1428 /* Go back to Virtual Wire compatibility mode */
1429 unsigned long value;
1430
1431 /* For the spurious interrupt use vector F, and enable it */
1432 value = apic_read(APIC_SPIV);
1433 value &= ~APIC_VECTOR_MASK;
1434 value |= APIC_SPIV_APIC_ENABLED;
1435 value |= 0xf;
1436 apic_write_around(APIC_SPIV, value);
1437
1438 if (!virt_wire_setup) {
1439 /*
1440 * For LVT0 make it edge triggered, active high,
1441 * external and enabled
1442 */
1443 value = apic_read(APIC_LVT0);
1444 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1445 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001446 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001447 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1448 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1449 apic_write_around(APIC_LVT0, value);
1450 } else {
1451 /* Disable LVT0 */
1452 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1453 }
1454
1455 /*
1456 * For LVT1 make it edge triggered, active high, nmi and
1457 * enabled
1458 */
1459 value = apic_read(APIC_LVT1);
1460 value &= ~(
1461 APIC_MODE_MASK | APIC_SEND_PENDING |
1462 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1463 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1464 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1465 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1466 apic_write_around(APIC_LVT1, value);
1467 }
1468}
1469
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001470unsigned int __cpuinitdata maxcpus = NR_CPUS;
1471
1472void __cpuinit generic_processor_info(int apicid, int version)
1473{
1474 int cpu;
1475 cpumask_t tmp_map;
1476 physid_mask_t phys_cpu;
1477
1478 /*
1479 * Validate version
1480 */
1481 if (version == 0x0) {
1482 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1483 "fixing up to 0x10. (tell your hw vendor)\n",
1484 version);
1485 version = 0x10;
1486 }
1487 apic_version[apicid] = version;
1488
1489 phys_cpu = apicid_to_cpu_present(apicid);
1490 physids_or(phys_cpu_present_map, phys_cpu_present_map, phys_cpu);
1491
1492 if (num_processors >= NR_CPUS) {
1493 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1494 " Processor ignored.\n", NR_CPUS);
1495 return;
1496 }
1497
1498 if (num_processors >= maxcpus) {
1499 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1500 " Processor ignored.\n", maxcpus);
1501 return;
1502 }
1503
1504 num_processors++;
1505 cpus_complement(tmp_map, cpu_present_map);
1506 cpu = first_cpu(tmp_map);
1507
1508 if (apicid == boot_cpu_physical_apicid)
1509 /*
1510 * x86_bios_cpu_apicid is required to have processors listed
1511 * in same order as logical cpu numbers. Hence the first
1512 * entry is BSP, and so on.
1513 */
1514 cpu = 0;
1515
1516 /*
1517 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1518 * but we need to work other dependencies like SMP_SUSPEND etc
1519 * before this can be done without some confusion.
1520 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1521 * - Ashok Raj <ashok.raj@intel.com>
1522 */
1523 if (num_processors > 8) {
1524 switch (boot_cpu_data.x86_vendor) {
1525 case X86_VENDOR_INTEL:
1526 if (!APIC_XAPIC(version)) {
1527 def_to_bigsmp = 0;
1528 break;
1529 }
1530 /* If P4 and above fall through */
1531 case X86_VENDOR_AMD:
1532 def_to_bigsmp = 1;
1533 }
1534 }
1535#ifdef CONFIG_SMP
1536 /* are we being called early in kernel startup? */
1537 if (x86_cpu_to_apicid_early_ptr) {
1538 u16 *cpu_to_apicid = x86_cpu_to_apicid_early_ptr;
1539 u16 *bios_cpu_apicid = x86_bios_cpu_apicid_early_ptr;
1540
1541 cpu_to_apicid[cpu] = apicid;
1542 bios_cpu_apicid[cpu] = apicid;
1543 } else {
1544 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1545 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1546 }
1547#endif
1548 cpu_set(cpu, cpu_possible_map);
1549 cpu_set(cpu, cpu_present_map);
1550}
1551
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001552/*
1553 * Power management
1554 */
1555#ifdef CONFIG_PM
1556
1557static struct {
1558 int active;
1559 /* r/w apic fields */
1560 unsigned int apic_id;
1561 unsigned int apic_taskpri;
1562 unsigned int apic_ldr;
1563 unsigned int apic_dfr;
1564 unsigned int apic_spiv;
1565 unsigned int apic_lvtt;
1566 unsigned int apic_lvtpc;
1567 unsigned int apic_lvt0;
1568 unsigned int apic_lvt1;
1569 unsigned int apic_lvterr;
1570 unsigned int apic_tmict;
1571 unsigned int apic_tdcr;
1572 unsigned int apic_thmr;
1573} apic_pm_state;
1574
1575static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1576{
1577 unsigned long flags;
1578 int maxlvt;
1579
1580 if (!apic_pm_state.active)
1581 return 0;
1582
1583 maxlvt = lapic_get_maxlvt();
1584
1585 apic_pm_state.apic_id = apic_read(APIC_ID);
1586 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1587 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1588 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1589 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1590 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1591 if (maxlvt >= 4)
1592 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1593 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1594 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1595 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1596 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1597 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1598#ifdef CONFIG_X86_MCE_P4THERMAL
1599 if (maxlvt >= 5)
1600 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1601#endif
1602
1603 local_irq_save(flags);
1604 disable_local_APIC();
1605 local_irq_restore(flags);
1606 return 0;
1607}
1608
1609static int lapic_resume(struct sys_device *dev)
1610{
1611 unsigned int l, h;
1612 unsigned long flags;
1613 int maxlvt;
1614
1615 if (!apic_pm_state.active)
1616 return 0;
1617
1618 maxlvt = lapic_get_maxlvt();
1619
1620 local_irq_save(flags);
1621
1622 /*
1623 * Make sure the APICBASE points to the right address
1624 *
1625 * FIXME! This will be wrong if we ever support suspend on
1626 * SMP! We'll need to do this as part of the CPU restore!
1627 */
1628 rdmsr(MSR_IA32_APICBASE, l, h);
1629 l &= ~MSR_IA32_APICBASE_BASE;
1630 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1631 wrmsr(MSR_IA32_APICBASE, l, h);
1632
1633 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1634 apic_write(APIC_ID, apic_pm_state.apic_id);
1635 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1636 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1637 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1638 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1639 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1640 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1641#ifdef CONFIG_X86_MCE_P4THERMAL
1642 if (maxlvt >= 5)
1643 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1644#endif
1645 if (maxlvt >= 4)
1646 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1647 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1648 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1649 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1650 apic_write(APIC_ESR, 0);
1651 apic_read(APIC_ESR);
1652 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1653 apic_write(APIC_ESR, 0);
1654 apic_read(APIC_ESR);
1655 local_irq_restore(flags);
1656 return 0;
1657}
1658
1659/*
1660 * This device has no shutdown method - fully functioning local APICs
1661 * are needed on every CPU up until machine_halt/restart/poweroff.
1662 */
1663
1664static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001665 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001666 .resume = lapic_resume,
1667 .suspend = lapic_suspend,
1668};
1669
1670static struct sys_device device_lapic = {
1671 .id = 0,
1672 .cls = &lapic_sysclass,
1673};
1674
1675static void __devinit apic_pm_activate(void)
1676{
1677 apic_pm_state.active = 1;
1678}
1679
1680static int __init init_lapic_sysfs(void)
1681{
1682 int error;
1683
1684 if (!cpu_has_apic)
1685 return 0;
1686 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1687
1688 error = sysdev_class_register(&lapic_sysclass);
1689 if (!error)
1690 error = sysdev_register(&device_lapic);
1691 return error;
1692}
1693device_initcall(init_lapic_sysfs);
1694
1695#else /* CONFIG_PM */
1696
1697static void apic_pm_activate(void) { }
1698
1699#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001700
1701/*
1702 * APIC command line parameters
1703 */
1704static int __init parse_lapic(char *arg)
1705{
1706 enable_local_apic = 1;
1707 return 0;
1708}
1709early_param("lapic", parse_lapic);
1710
1711static int __init parse_nolapic(char *arg)
1712{
1713 enable_local_apic = -1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001714 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001715 return 0;
1716}
1717early_param("nolapic", parse_nolapic);
1718
1719static int __init parse_disable_lapic_timer(char *arg)
1720{
1721 local_apic_timer_disabled = 1;
1722 return 0;
1723}
1724early_param("nolapic_timer", parse_disable_lapic_timer);
1725
1726static int __init parse_lapic_timer_c2_ok(char *arg)
1727{
1728 local_apic_timer_c2_ok = 1;
1729 return 0;
1730}
1731early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1732
1733static int __init apic_set_verbosity(char *str)
1734{
1735 if (strcmp("debug", str) == 0)
1736 apic_verbosity = APIC_DEBUG;
1737 else if (strcmp("verbose", str) == 0)
1738 apic_verbosity = APIC_VERBOSE;
1739 return 1;
1740}
1741__setup("apic=", apic_set_verbosity);
1742