blob: 6dea8306d8c05c0303aac0e1c268facda83e74c1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/mc146818rtc.h>
24#include <linux/kernel_stat.h>
25#include <linux/sysdev.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070026#include <linux/cpu.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080027#include <linux/clockchips.h>
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080028#include <linux/acpi_pmtmr.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010029#include <linux/module.h>
Thomas Gleixnerad62ca22007-03-22 00:11:21 -080030#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/atomic.h>
33#include <asm/smp.h>
34#include <asm/mtrr.h>
35#include <asm/mpspec.h>
36#include <asm/desc.h>
37#include <asm/arch_hooks.h>
38#include <asm/hpet.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070039#include <asm/i8253.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020040#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <mach_apic.h>
Jesper Juhl382dbd02006-03-23 02:59:49 -080043#include <mach_apicdef.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010044#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080047 * Sanity check
48 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +010049#if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
Thomas Gleixnere05d7232007-02-16 01:27:58 -080050# error SPURIOUS_APIC_VECTOR definition error
51#endif
52
Alexey Starikovskiy8f6e2ca2008-03-27 23:54:38 +030053unsigned long mp_lapic_addr;
54
Thomas Gleixnere05d7232007-02-16 01:27:58 -080055/*
Eric W. Biederman9635b472005-06-25 14:57:41 -070056 * Knob to control our willingness to enable the local APIC.
Thomas Gleixnere05d7232007-02-16 01:27:58 -080057 *
Yinghai Lu914bebf2008-06-29 00:06:37 -070058 * +1=force-enable
Eric W. Biederman9635b472005-06-25 14:57:41 -070059 */
Yinghai Lu914bebf2008-06-29 00:06:37 -070060static int force_enable_local_apic;
61int disable_apic;
Eric W. Biederman9635b472005-06-25 14:57:41 -070062
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080063/* Local APIC timer verification ok */
64static int local_apic_timer_verify_ok;
Thomas Gleixneraa276e12008-06-09 19:15:00 +020065/* Disable local APIC timer from the kernel commandline or via dmi quirk */
66static int local_apic_timer_disabled;
Thomas Gleixnere585bef2007-03-23 16:08:01 +010067/* Local APIC timer works in C2 */
68int local_apic_timer_c2_ok;
69EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080070
Alan Mayerce178332008-04-16 15:17:20 -050071int first_system_vector = 0xfe;
72
73char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
74
Eric W. Biederman9635b472005-06-25 14:57:41 -070075/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080076 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
78int apic_verbosity;
79
Alexey Starikovskiyf3918352008-05-23 01:54:51 +040080int pic_mode;
81
Alexey Starikovskiybab4b272008-05-19 19:47:03 +040082/* Have we found an MP table */
83int smp_found_config;
84
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080085static unsigned int calibration_result;
86
87static int lapic_next_event(unsigned long delta,
88 struct clock_event_device *evt);
89static void lapic_timer_setup(enum clock_event_mode mode,
90 struct clock_event_device *evt);
91static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static void apic_pm_activate(void);
93
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080094/*
95 * The local apic timer can be used for any function which is CPU local.
96 */
97static struct clock_event_device lapic_clockevent = {
98 .name = "lapic",
99 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800100 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800101 .shift = 32,
102 .set_mode = lapic_timer_setup,
103 .set_next_event = lapic_next_event,
104 .broadcast = lapic_timer_broadcast,
105 .rating = 100,
106 .irq = -1,
107};
108static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800110/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static int enabled_via_apicbase;
112
Andi Kleend3432892008-01-30 13:33:17 +0100113static unsigned long apic_phys;
114
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800115/*
116 * Get the LAPIC version
117 */
118static inline int lapic_get_version(void)
119{
120 return GET_APIC_VERSION(apic_read(APIC_LVR));
121}
122
123/*
Joe Perchesab4a5742008-01-30 13:31:42 +0100124 * Check, if the APIC is integrated or a separate chip
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800125 */
126static inline int lapic_is_integrated(void)
127{
128 return APIC_INTEGRATED(lapic_get_version());
129}
130
131/*
132 * Check, whether this is a modern or a first generation APIC
133 */
134static int modern_apic(void)
135{
136 /* AMD systems use old APIC versions, so check the CPU */
137 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
138 boot_cpu_data.x86 >= 0xf)
139 return 1;
140 return lapic_get_version() >= 0x14;
141}
142
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200143void apic_wait_icr_idle(void)
144{
145 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
146 cpu_relax();
147}
148
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100149u32 safe_apic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200150{
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100151 u32 send_status;
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200152 int timeout;
153
154 timeout = 0;
155 do {
156 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
157 if (!send_status)
158 break;
159 udelay(100);
160 } while (timeout++ < 1000);
161
162 return send_status;
163}
164
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800165/**
166 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
167 */
Jan Beuliche9427102008-01-30 13:31:24 +0100168void __cpuinit enable_NMI_through_LVT0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800170 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800172 /* Level triggered for 82489DX */
173 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 v |= APIC_LVT_LEVEL_TRIGGER;
175 apic_write_around(APIC_LVT0, v);
176}
177
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800178/**
179 * get_physical_broadcast - Get number of physical broadcast IDs
180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181int get_physical_broadcast(void)
182{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800183 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800186/**
187 * lapic_get_maxlvt - get the maximum number of local vector table entries
188 */
189int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800191 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800194 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
197/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800198 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800201/* Clock divisor is set to 16 */
202#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204/*
205 * This function sets up the local APIC timer, with a timeout of
206 * 'clocks' APIC bus clock. During calibration we actually call
207 * this function twice on the boot CPU, once with a bogus timeout
208 * value, second time for real. The other (noncalibrating) CPUs
209 * call this function only once, with the real, calibrated value.
210 *
211 * We do reads before writes even if unnecessary, to get around the
212 * P5 APIC double write bug.
213 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800214static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800216 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800218 lvtt_value = LOCAL_TIMER_VECTOR;
219 if (!oneshot)
220 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800221 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100223
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800224 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100225 lvtt_value |= APIC_LVT_MASKED;
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 apic_write_around(APIC_LVTT, lvtt_value);
228
229 /*
230 * Divide PICLK by 16
231 */
232 tmp_value = apic_read(APIC_TDCR);
233 apic_write_around(APIC_TDCR, (tmp_value
234 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
235 | APIC_TDR_DIV_16);
236
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800237 if (!oneshot)
238 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800241/*
242 * Program the next event, relative to now
243 */
244static int lapic_next_event(unsigned long delta,
245 struct clock_event_device *evt)
246{
247 apic_write_around(APIC_TMICT, delta);
248 return 0;
249}
250
251/*
252 * Setup the lapic timer in periodic or oneshot mode
253 */
254static void lapic_timer_setup(enum clock_event_mode mode,
255 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800258 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800260 /* Lapic used for broadcast ? */
261 if (!local_apic_timer_verify_ok)
262 return;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 local_irq_save(flags);
265
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800266 switch (mode) {
267 case CLOCK_EVT_MODE_PERIODIC:
268 case CLOCK_EVT_MODE_ONESHOT:
269 __setup_APIC_LVTT(calibration_result,
270 mode != CLOCK_EVT_MODE_PERIODIC, 1);
271 break;
272 case CLOCK_EVT_MODE_UNUSED:
273 case CLOCK_EVT_MODE_SHUTDOWN:
274 v = apic_read(APIC_LVTT);
275 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
276 apic_write_around(APIC_LVTT, v);
277 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700278 case CLOCK_EVT_MODE_RESUME:
279 /* Nothing to do here */
280 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 local_irq_restore(flags);
284}
285
286/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800287 * Local APIC timer broadcast function
288 */
289static void lapic_timer_broadcast(cpumask_t mask)
290{
291#ifdef CONFIG_SMP
292 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
293#endif
294}
295
296/*
297 * Setup the local APIC timer for this CPU. Copy the initilized values
298 * of the boot CPU and register the clock event in the framework.
299 */
300static void __devinit setup_APIC_timer(void)
301{
302 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
303
304 memcpy(levt, &lapic_clockevent, sizeof(*levt));
305 levt->cpumask = cpumask_of_cpu(smp_processor_id());
306
307 clockevents_register_device(levt);
308}
309
310/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800311 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800313 * We want to do the calibration only once since we want to have local timer
314 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
315 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800316 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800317 * This was previously done by reading the PIT/HPET and waiting for a wrap
318 * around to find out, that a tick has elapsed. I have a box, where the PIT
319 * readout is broken, so it never gets out of the wait loop again. This was
320 * also reported by others.
321 *
322 * Monitoring the jiffies value is inaccurate and the clockevents
323 * infrastructure allows us to do a simple substitution of the interrupt
324 * handler.
325 *
326 * The calibration routine also uses the pm_timer when possible, as the PIT
327 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
328 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
330
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800331#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200333static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800334static __initdata long lapic_cal_t1, lapic_cal_t2;
335static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
336static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
337static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
338
339/*
340 * Temporary interrupt handler.
341 */
342static void __init lapic_cal_handler(struct clock_event_device *dev)
343{
344 unsigned long long tsc = 0;
345 long tapic = apic_read(APIC_TMCCT);
346 unsigned long pm = acpi_pm_read_early();
347
348 if (cpu_has_tsc)
349 rdtscll(tsc);
350
351 switch (lapic_cal_loops++) {
352 case 0:
353 lapic_cal_t1 = tapic;
354 lapic_cal_tsc1 = tsc;
355 lapic_cal_pm1 = pm;
356 lapic_cal_j1 = jiffies;
357 break;
358
359 case LAPIC_CAL_LOOPS:
360 lapic_cal_t2 = tapic;
361 lapic_cal_tsc2 = tsc;
362 if (pm < lapic_cal_pm1)
363 pm += ACPI_PM_OVRRUN;
364 lapic_cal_pm2 = pm;
365 lapic_cal_j2 = jiffies;
366 break;
367 }
368}
369
370/*
371 * Setup the boot APIC
372 *
373 * Calibrate and verify the result.
374 */
375void __init setup_boot_APIC_clock(void)
376{
377 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
378 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
379 const long pm_thresh = pm_100ms/100;
380 void (*real_handler)(struct clock_event_device *dev);
381 unsigned long deltaj;
382 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800383 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800384
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800385 /*
386 * The local apic timer can be disabled via the kernel
Andi Kleend3f7eae2007-08-10 22:31:07 +0200387 * commandline or from the CPU detection code. Register the lapic
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800388 * timer as a dummy clock event source on SMP systems, so the
389 * broadcast mechanism is used. On UP systems simply ignore it.
390 */
391 if (local_apic_timer_disabled) {
392 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100393 if (num_possible_cpus() > 1) {
394 lapic_clockevent.mult = 1;
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800395 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100396 }
Thomas Gleixnerad62ca22007-03-22 00:11:21 -0800397 return;
398 }
399
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800400 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
401 "calibrating APIC timer ...\n");
402
403 local_irq_disable();
404
405 /* Replace the global interrupt handler */
406 real_handler = global_clock_event->event_handler;
407 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800410 * Setup the APIC counter to 1e9. There is no way the lapic
411 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800413 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800415 /* Let the interrupts run */
416 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800418 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
419 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800421 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800423 /* Restore the real event handler */
424 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800426 /* Build delta t1-t2 as apic timer counts down */
427 delta = lapic_cal_t1 - lapic_cal_t2;
428 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800430 /* Check, if the PM timer is available */
431 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
432 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800434 if (deltapm) {
435 unsigned long mult;
436 u64 res;
437
438 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
439
440 if (deltapm > (pm_100ms - pm_thresh) &&
441 deltapm < (pm_100ms + pm_thresh)) {
442 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
443 } else {
444 res = (((u64) deltapm) * mult) >> 22;
445 do_div(res, 1000000);
446 printk(KERN_WARNING "APIC calibration not consistent "
447 "with PM Timer: %ldms instead of 100ms\n",
448 (long)res);
449 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100450 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800451 do_div(res, deltapm);
452 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
453 "%lu (%ld)\n", (unsigned long) res, delta);
454 delta = (long) res;
455 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800456 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800459 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900460 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
461 lapic_clockevent.shift);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800462 lapic_clockevent.max_delta_ns =
463 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
464 lapic_clockevent.min_delta_ns =
465 clockevent_delta2ns(0xF, &lapic_clockevent);
466
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800467 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800468
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800469 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
470 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
471 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
472 calibration_result);
473
474 if (cpu_has_tsc) {
475 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800477 "%ld.%04ld MHz.\n",
478 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
479 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800483 "%u.%04u MHz.\n",
484 calibration_result / (1000000 / HZ),
485 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800487 local_apic_timer_verify_ok = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100489 /*
490 * Do a sanity check on the APIC calibration result
491 */
492 if (calibration_result < (1000000 / HZ)) {
493 local_irq_enable();
494 printk(KERN_WARNING
495 "APIC frequency too slow, disabling apic timer\n");
496 /* No broadcast on UP ! */
497 if (num_possible_cpus() > 1)
498 setup_APIC_timer();
499 return;
500 }
501
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800502 /* We trust the pm timer based calibration */
503 if (!pm_referenced) {
504 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800505
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800506 /*
507 * Setup the apic timer manually
508 */
509 levt->event_handler = lapic_cal_handler;
510 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
511 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800512
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800513 /* Let the interrupts run */
514 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800515
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200516 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800517 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800518
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800519 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800520
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800521 /* Stop the lapic timer */
522 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800523
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800524 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800525
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800526 /* Jiffies delta */
527 deltaj = lapic_cal_j2 - lapic_cal_j1;
528 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800529
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800530 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800531 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800532 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800533 else
534 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100535 } else
536 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800537
538 if (!local_apic_timer_verify_ok) {
539 printk(KERN_WARNING
540 "APIC timer disabled due to verification failure.\n");
541 /* No broadcast on UP ! */
542 if (num_possible_cpus() == 1)
543 return;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800544 } else {
545 /*
546 * If nmi_watchdog is set to IO_APIC, we need the
547 * PIT/HPET going. Otherwise register lapic as a dummy
548 * device.
549 */
550 if (nmi_watchdog != NMI_IO_APIC)
551 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
Ingo Molnar820de5c2007-07-21 04:37:36 -0700552 else
553 printk(KERN_WARNING "APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200554 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800555 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800556
557 /* Setup the lapic or request the broadcast */
558 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Li Shaohua0bb31842005-06-25 14:54:55 -0700561void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800563 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800567 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800569static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800571 int cpu = smp_processor_id();
572 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800575 * Normally we should not be here till LAPIC has been initialized but
576 * in some cases like kdump, its possible that there is a pending LAPIC
577 * timer interrupt from previous kernel's context and is delivered in
578 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800580 * Interrupts are enabled early and LAPIC is setup much later, hence
581 * its possible that when we get here evt->event_handler is NULL.
582 * Check for event_handler being NULL and discard the interrupt as
583 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800585 if (!evt->event_handler) {
586 printk(KERN_WARNING
587 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
588 /* Switch it off */
589 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
590 return;
591 }
592
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100593 /*
594 * the NMI deadlock-detector uses this.
595 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800596 per_cpu(irq_stat, cpu).apic_timer_irqs++;
597
598 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601/*
602 * Local APIC timer interrupt. This is the most natural way for doing
603 * local interrupts, but local timer interrupts can be emulated by
604 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
605 *
606 * [ if a single-CPU system runs an SMP kernel then we call the local
607 * interrupt as well. Thus we cannot inline the local irq ... ]
608 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100609void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
David Howells7d12e782006-10-05 14:55:46 +0100611 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
613 /*
614 * NOTE! We'd better ACK the irq immediately,
615 * because timer handling can be slow.
616 */
617 ack_APIC_irq();
618 /*
619 * update_process_times() expects us to have done irq_enter().
620 * Besides, if we don't timer interrupts ignore the global
621 * interrupt lock, which is the WrongThing (tm) to do.
622 */
623 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800624 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800626
David Howells7d12e782006-10-05 14:55:46 +0100627 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100630int setup_profiling_timer(unsigned int multiplier)
631{
632 return -EINVAL;
633}
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635/*
Robert Richtere319e762008-02-13 16:19:36 +0100636 * Setup extended LVT, AMD specific (K8, family 10h)
637 *
638 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
639 * MCE interrupts are supported. Thus MCE offset must be set to 0.
640 */
641
642#define APIC_EILVT_LVTOFF_MCE 0
643#define APIC_EILVT_LVTOFF_IBS 1
644
645static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
646{
647 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
648 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
649 apic_write(reg, v);
650}
651
652u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
653{
654 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
655 return APIC_EILVT_LVTOFF_MCE;
656}
657
658u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
659{
660 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
661 return APIC_EILVT_LVTOFF_IBS;
662}
663
664/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800665 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800667
668/**
669 * clear_local_APIC - shutdown the local APIC
670 *
671 * This is called, when a CPU is disabled and before rebooting, so the state of
672 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
673 * leftovers during boot.
674 */
675void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Andi Kleend3432892008-01-30 13:33:17 +0100677 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100678 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Andi Kleend3432892008-01-30 13:33:17 +0100680 /* APIC hasn't been mapped yet */
681 if (!apic_phys)
682 return;
683
684 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800686 * Masking an LVT entry can trigger a local APIC error
687 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800689 if (maxlvt >= 3) {
690 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
691 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
692 }
693 /*
694 * Careful: we have to set masks only first to deassert
695 * any level-triggered sources.
696 */
697 v = apic_read(APIC_LVTT);
698 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
699 v = apic_read(APIC_LVT0);
700 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
701 v = apic_read(APIC_LVT1);
702 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
703 if (maxlvt >= 4) {
704 v = apic_read(APIC_LVTPC);
705 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800708 /* lets not touch this if we didn't frob it */
709#ifdef CONFIG_X86_MCE_P4THERMAL
710 if (maxlvt >= 5) {
711 v = apic_read(APIC_LVTTHMR);
712 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
713 }
714#endif
715 /*
716 * Clean APIC state for other OSs:
717 */
718 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
719 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
720 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
721 if (maxlvt >= 3)
722 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
723 if (maxlvt >= 4)
724 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
725
726#ifdef CONFIG_X86_MCE_P4THERMAL
727 if (maxlvt >= 5)
728 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
729#endif
730 /* Integrated APIC (!82489DX) ? */
731 if (lapic_is_integrated()) {
732 if (maxlvt > 3)
733 /* Clear ESR due to Pentium errata 3AP and 11AP */
734 apic_write(APIC_ESR, 0);
735 apic_read(APIC_ESR);
736 }
737}
738
739/**
740 * disable_local_APIC - clear and disable the local APIC
741 */
742void disable_local_APIC(void)
743{
744 unsigned long value;
745
746 clear_local_APIC();
747
748 /*
749 * Disable APIC (implies clearing of registers
750 * for 82489DX!).
751 */
752 value = apic_read(APIC_SPIV);
753 value &= ~APIC_SPIV_APIC_ENABLED;
754 apic_write_around(APIC_SPIV, value);
755
756 /*
757 * When LAPIC was disabled by the BIOS and enabled by the kernel,
758 * restore the disabled state.
759 */
760 if (enabled_via_apicbase) {
761 unsigned int l, h;
762
763 rdmsr(MSR_IA32_APICBASE, l, h);
764 l &= ~MSR_IA32_APICBASE_ENABLE;
765 wrmsr(MSR_IA32_APICBASE, l, h);
766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800770 * If Linux enabled the LAPIC against the BIOS default disable it down before
771 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
772 * not power-off. Additionally clear all LVT entries before disable_local_APIC
773 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800775void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800777 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800779 if (!cpu_has_apic)
780 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800782 local_irq_save(flags);
783 clear_local_APIC();
784
785 if (enabled_via_apicbase)
786 disable_local_APIC();
787
788 local_irq_restore(flags);
789}
790
791/*
792 * This is to verify that we're looking at a real local APIC.
793 * Check these against your board if the CPUs aren't getting
794 * started for no apparent reason.
795 */
796int __init verify_local_APIC(void)
797{
798 unsigned int reg0, reg1;
799
800 /*
801 * The version register is read-only in a real APIC.
802 */
803 reg0 = apic_read(APIC_LVR);
804 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
805 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
806 reg1 = apic_read(APIC_LVR);
807 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
808
809 /*
810 * The two version reads above should print the same
811 * numbers. If the second one is different, then we
812 * poke at a non-APIC.
813 */
814 if (reg1 != reg0)
815 return 0;
816
817 /*
818 * Check if the version looks reasonably.
819 */
820 reg1 = GET_APIC_VERSION(reg0);
821 if (reg1 == 0x00 || reg1 == 0xff)
822 return 0;
823 reg1 = lapic_get_maxlvt();
824 if (reg1 < 0x02 || reg1 == 0xff)
825 return 0;
826
827 /*
828 * The ID register is read/write in a real APIC.
829 */
830 reg0 = apic_read(APIC_ID);
831 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
832
833 /*
834 * The next two are just to see if we have sane values.
835 * They're only really relevant if we're in Virtual Wire
836 * compatibility mode, but most boxes are anymore.
837 */
838 reg0 = apic_read(APIC_LVT0);
839 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
840 reg1 = apic_read(APIC_LVT1);
841 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
842
843 return 1;
844}
845
846/**
847 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
848 */
849void __init sync_Arb_IDs(void)
850{
851 /*
852 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
853 * needed on AMD.
854 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100855 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800856 return;
857 /*
858 * Wait for idle.
859 */
860 apic_wait_icr_idle();
861
862 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
863 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
864 | APIC_DM_INIT);
865}
866
867/*
868 * An initial setup of the virtual wire mode.
869 */
870void __init init_bsp_APIC(void)
871{
872 unsigned long value;
873
874 /*
875 * Don't do the setup now if we have a SMP BIOS as the
876 * through-I/O-APIC virtual wire mode might be active.
877 */
878 if (smp_found_config || !cpu_has_apic)
879 return;
880
881 /*
882 * Do not trust the local APIC being empty at bootup.
883 */
884 clear_local_APIC();
885
886 /*
887 * Enable APIC.
888 */
889 value = apic_read(APIC_SPIV);
890 value &= ~APIC_VECTOR_MASK;
891 value |= APIC_SPIV_APIC_ENABLED;
892
893 /* This bit is reserved on P4/Xeon and should be cleared */
894 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
895 (boot_cpu_data.x86 == 15))
896 value &= ~APIC_SPIV_FOCUS_DISABLED;
897 else
898 value |= APIC_SPIV_FOCUS_DISABLED;
899 value |= SPURIOUS_APIC_VECTOR;
900 apic_write_around(APIC_SPIV, value);
901
902 /*
903 * Set up the virtual wire mode.
904 */
905 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
906 value = APIC_DM_NMI;
907 if (!lapic_is_integrated()) /* 82489DX */
908 value |= APIC_LVT_LEVEL_TRIGGER;
909 apic_write_around(APIC_LVT1, value);
910}
911
Ingo Molnara4928cf2008-04-23 13:20:56 +0200912static void __cpuinit lapic_setup_esr(void)
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300913{
914 unsigned long oldvalue, value, maxlvt;
915 if (lapic_is_integrated() && !esr_disable) {
916 /* !82489DX */
917 maxlvt = lapic_get_maxlvt();
918 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
919 apic_write(APIC_ESR, 0);
920 oldvalue = apic_read(APIC_ESR);
921
922 /* enables sending errors */
923 value = ERROR_APIC_VECTOR;
924 apic_write_around(APIC_LVTERR, value);
925 /*
926 * spec says clear errors after enabling vector.
927 */
928 if (maxlvt > 3)
929 apic_write(APIC_ESR, 0);
930 value = apic_read(APIC_ESR);
931 if (value != oldvalue)
932 apic_printk(APIC_VERBOSE, "ESR value before enabling "
933 "vector: 0x%08lx after: 0x%08lx\n",
934 oldvalue, value);
935 } else {
936 if (esr_disable)
937 /*
938 * Something untraceable is creating bad interrupts on
939 * secondary quads ... for the moment, just leave the
940 * ESR disabled - we can't do anything useful with the
941 * errors anyway - mbligh
942 */
943 printk(KERN_INFO "Leaving ESR disabled.\n");
944 else
945 printk(KERN_INFO "No ESR for 82489DX.\n");
946 }
947}
948
949
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800950/**
951 * setup_local_APIC - setup the local APIC
952 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100953void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800954{
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300955 unsigned long value, integrated;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800956 int i, j;
957
958 /* Pound the ESR really hard over the head with a big hammer - mbligh */
959 if (esr_disable) {
960 apic_write(APIC_ESR, 0);
961 apic_write(APIC_ESR, 0);
962 apic_write(APIC_ESR, 0);
963 apic_write(APIC_ESR, 0);
964 }
965
966 integrated = lapic_is_integrated();
967
968 /*
969 * Double-check whether this APIC is really registered.
970 */
971 if (!apic_id_registered())
972 BUG();
973
974 /*
975 * Intel recommends to set DFR, LDR and TPR before enabling
976 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
977 * document number 292116). So here it goes...
978 */
979 init_apic_ldr();
980
981 /*
982 * Set Task Priority to 'accept all'. We never change this
983 * later on.
984 */
985 value = apic_read(APIC_TASKPRI);
986 value &= ~APIC_TPRI_MASK;
987 apic_write_around(APIC_TASKPRI, value);
988
989 /*
990 * After a crash, we no longer service the interrupts and a pending
991 * interrupt from previous kernel might still have ISR bit set.
992 *
993 * Most probably by now CPU has serviced that pending interrupt and
994 * it might not have done the ack_APIC_irq() because it thought,
995 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
996 * does not clear the ISR bit and cpu thinks it has already serivced
997 * the interrupt. Hence a vector might get locked. It was noticed
998 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
999 */
1000 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1001 value = apic_read(APIC_ISR + i*0x10);
1002 for (j = 31; j >= 0; j--) {
1003 if (value & (1<<j))
1004 ack_APIC_irq();
1005 }
1006 }
1007
1008 /*
1009 * Now that we are all set up, enable the APIC
1010 */
1011 value = apic_read(APIC_SPIV);
1012 value &= ~APIC_VECTOR_MASK;
1013 /*
1014 * Enable APIC
1015 */
1016 value |= APIC_SPIV_APIC_ENABLED;
1017
1018 /*
1019 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1020 * certain networking cards. If high frequency interrupts are
1021 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1022 * entry is masked/unmasked at a high rate as well then sooner or
1023 * later IOAPIC line gets 'stuck', no more interrupts are received
1024 * from the device. If focus CPU is disabled then the hang goes
1025 * away, oh well :-(
1026 *
1027 * [ This bug can be reproduced easily with a level-triggered
1028 * PCI Ne2000 networking cards and PII/PIII processors, dual
1029 * BX chipset. ]
1030 */
1031 /*
1032 * Actually disabling the focus CPU check just makes the hang less
1033 * frequent as it makes the interrupt distributon model be more
1034 * like LRU than MRU (the short-term load is more even across CPUs).
1035 * See also the comment in end_level_ioapic_irq(). --macro
1036 */
1037
1038 /* Enable focus processor (bit==0) */
1039 value &= ~APIC_SPIV_FOCUS_DISABLED;
1040
1041 /*
1042 * Set spurious IRQ vector
1043 */
1044 value |= SPURIOUS_APIC_VECTOR;
1045 apic_write_around(APIC_SPIV, value);
1046
1047 /*
1048 * Set up LVT0, LVT1:
1049 *
1050 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001051 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001052 * we delegate interrupts to the 8259A.
1053 */
1054 /*
1055 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1056 */
1057 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1058 if (!smp_processor_id() && (pic_mode || !value)) {
1059 value = APIC_DM_EXTINT;
1060 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1061 smp_processor_id());
1062 } else {
1063 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1064 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1065 smp_processor_id());
1066 }
1067 apic_write_around(APIC_LVT0, value);
1068
1069 /*
1070 * only the BP should see the LINT1 NMI signal, obviously.
1071 */
1072 if (!smp_processor_id())
1073 value = APIC_DM_NMI;
1074 else
1075 value = APIC_DM_NMI | APIC_LVT_MASKED;
1076 if (!integrated) /* 82489DX */
1077 value |= APIC_LVT_LEVEL_TRIGGER;
1078 apic_write_around(APIC_LVT1, value);
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001079}
1080
1081void __cpuinit end_local_APIC_setup(void)
1082{
1083 unsigned long value;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001084
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001085 lapic_setup_esr();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001086 /* Disable the local apic timer */
1087 value = apic_read(APIC_LVTT);
1088 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1089 apic_write_around(APIC_LVTT, value);
1090
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001091 setup_apic_nmi_watchdog(NULL);
1092 apic_pm_activate();
1093}
1094
1095/*
1096 * Detect and initialize APIC
1097 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001098static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001099{
1100 u32 h, l, features;
1101
1102 /* Disabled by kernel option? */
Yinghai Lu914bebf2008-06-29 00:06:37 -07001103 if (disable_apic)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001104 return -1;
1105
1106 switch (boot_cpu_data.x86_vendor) {
1107 case X86_VENDOR_AMD:
1108 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1109 (boot_cpu_data.x86 == 15))
1110 break;
1111 goto no_apic;
1112 case X86_VENDOR_INTEL:
1113 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1114 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1115 break;
1116 goto no_apic;
1117 default:
1118 goto no_apic;
1119 }
1120
1121 if (!cpu_has_apic) {
1122 /*
1123 * Over-ride BIOS and try to enable the local APIC only if
1124 * "lapic" specified.
1125 */
Yinghai Lu914bebf2008-06-29 00:06:37 -07001126 if (!force_enable_local_apic) {
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001127 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1128 "you can enable it with \"lapic\"\n");
1129 return -1;
1130 }
1131 /*
1132 * Some BIOSes disable the local APIC in the APIC_BASE
1133 * MSR. This can only be done in software for Intel P6 or later
1134 * and AMD K7 (Model > 1) or later.
1135 */
1136 rdmsr(MSR_IA32_APICBASE, l, h);
1137 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1138 printk(KERN_INFO
1139 "Local APIC disabled by BIOS -- reenabling.\n");
1140 l &= ~MSR_IA32_APICBASE_BASE;
1141 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1142 wrmsr(MSR_IA32_APICBASE, l, h);
1143 enabled_via_apicbase = 1;
1144 }
1145 }
1146 /*
1147 * The APIC feature bit should now be enabled
1148 * in `cpuid'
1149 */
1150 features = cpuid_edx(1);
1151 if (!(features & (1 << X86_FEATURE_APIC))) {
1152 printk(KERN_WARNING "Could not enable APIC!\n");
1153 return -1;
1154 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001155 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001156 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1157
1158 /* The BIOS may have set up the APIC at some other address */
1159 rdmsr(MSR_IA32_APICBASE, l, h);
1160 if (l & MSR_IA32_APICBASE_ENABLE)
1161 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1162
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001163 printk(KERN_INFO "Found and enabled local APIC!\n");
1164
1165 apic_pm_activate();
1166
1167 return 0;
1168
1169no_apic:
1170 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1171 return -1;
1172}
1173
1174/**
1175 * init_apic_mappings - initialize APIC mappings
1176 */
1177void __init init_apic_mappings(void)
1178{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001179 /*
1180 * If no local APIC can be found then set up a fake all
1181 * zeroes page to simulate the local APIC and another
1182 * one for the IO-APIC.
1183 */
1184 if (!smp_found_config && detect_init_APIC()) {
1185 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1186 apic_phys = __pa(apic_phys);
1187 } else
1188 apic_phys = mp_lapic_addr;
1189
1190 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1191 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1192 apic_phys);
1193
1194 /*
1195 * Fetch the APIC ID of the BSP in case we have a
1196 * default configuration (or the MP table is broken).
1197 */
1198 if (boot_cpu_physical_apicid == -1U)
Jack Steiner05f2d122008-03-28 14:12:02 -05001199 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
1203/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001204 * This initializes the IO-APIC and APIC hardware if this is
1205 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 */
Alexey Starikovskiye81b2c62008-03-27 23:54:31 +03001207
1208int apic_version[MAX_APICS];
1209
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001210int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Yinghai Lu914bebf2008-06-29 00:06:37 -07001212 if (disable_apic)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001213 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001214
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001215 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001216 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001219 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001221 if (!cpu_has_apic &&
1222 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001224 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001225 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 return -1;
1227 }
1228
1229 verify_local_APIC();
1230
1231 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001232
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001233 /*
1234 * Hack: In case of kdump, after a crash, kernel might be booting
1235 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1236 * might be zero if read from MP tables. Get it from LAPIC.
1237 */
1238#ifdef CONFIG_CRASH_DUMP
Jack Steiner05f2d122008-03-28 14:12:02 -05001239 boot_cpu_physical_apicid = GET_APIC_ID(read_apic_id());
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001240#endif
Jack Steinerb6df1b82008-06-19 21:51:05 -05001241 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 setup_local_APIC();
1244
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001245#ifdef CONFIG_X86_IO_APIC
1246 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1247#endif
1248 localise_nmi_watchdog();
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001249 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001251 if (smp_found_config)
1252 if (!skip_ioapic_setup && nr_ioapics)
1253 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001255 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001256
1257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001259
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001260/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001261 * Local APIC interrupts
1262 */
1263
1264/*
1265 * This interrupt should _never_ happen with our APIC/SMP architecture
1266 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001267void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001268{
1269 unsigned long v;
1270
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001271 irq_enter();
1272 /*
1273 * Check if this really is a spurious interrupt and ACK it
1274 * if it is a vectored one. Just in case...
1275 * Spurious interrupts should not be ACKed.
1276 */
1277 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1278 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1279 ack_APIC_irq();
1280
1281 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1282 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1283 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001284 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001285 irq_exit();
1286}
1287
1288/*
1289 * This interrupt should never happen with our APIC/SMP architecture
1290 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001291void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001292{
1293 unsigned long v, v1;
1294
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001295 irq_enter();
1296 /* First tickle the hardware, only then report what went on. -- REW */
1297 v = apic_read(APIC_ESR);
1298 apic_write(APIC_ESR, 0);
1299 v1 = apic_read(APIC_ESR);
1300 ack_APIC_irq();
1301 atomic_inc(&irq_err_count);
1302
1303 /* Here is what the APIC error bits mean:
1304 0: Send CS error
1305 1: Receive CS error
1306 2: Send accept error
1307 3: Receive accept error
1308 4: Reserved
1309 5: Send illegal vector
1310 6: Received illegal vector
1311 7: Illegal register address
1312 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001313 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001314 smp_processor_id(), v , v1);
1315 irq_exit();
1316}
1317
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001318#ifdef CONFIG_SMP
1319void __init smp_intr_init(void)
1320{
1321 /*
1322 * IRQ0 must be given a fixed assignment and initialized,
1323 * because it's used before the IO-APIC is set up.
1324 */
1325 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1326
1327 /*
1328 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1329 * IPI, driven by wakeup.
1330 */
Alan Mayer305b92a2008-04-15 15:36:56 -05001331 alloc_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001332
1333 /* IPI for invalidation */
Alan Mayer305b92a2008-04-15 15:36:56 -05001334 alloc_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001335
1336 /* IPI for generic function call */
Alan Mayer305b92a2008-04-15 15:36:56 -05001337 alloc_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001338}
1339#endif
1340
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001341/*
1342 * Initialize APIC interrupts
1343 */
1344void __init apic_intr_init(void)
1345{
1346#ifdef CONFIG_SMP
1347 smp_intr_init();
1348#endif
1349 /* self generated IPI for local APIC timer */
Alan Mayer305b92a2008-04-15 15:36:56 -05001350 alloc_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001351
1352 /* IPI vectors for APIC spurious and error interrupts */
Alan Mayer305b92a2008-04-15 15:36:56 -05001353 alloc_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1354 alloc_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001355
1356 /* thermal monitor LVT interrupt */
1357#ifdef CONFIG_X86_MCE_P4THERMAL
Alan Mayer305b92a2008-04-15 15:36:56 -05001358 alloc_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001359#endif
1360}
1361
1362/**
1363 * connect_bsp_APIC - attach the APIC to the interrupt system
1364 */
1365void __init connect_bsp_APIC(void)
1366{
1367 if (pic_mode) {
1368 /*
1369 * Do not trust the local APIC being empty at bootup.
1370 */
1371 clear_local_APIC();
1372 /*
1373 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1374 * local APIC to INT and NMI lines.
1375 */
1376 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1377 "enabling APIC mode.\n");
1378 outb(0x70, 0x22);
1379 outb(0x01, 0x23);
1380 }
1381 enable_apic_mode();
1382}
1383
1384/**
1385 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1386 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1387 *
1388 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1389 * APIC is disabled.
1390 */
1391void disconnect_bsp_APIC(int virt_wire_setup)
1392{
1393 if (pic_mode) {
1394 /*
1395 * Put the board back into PIC mode (has an effect only on
1396 * certain older boards). Note that APIC interrupts, including
1397 * IPIs, won't work beyond this point! The only exception are
1398 * INIT IPIs.
1399 */
1400 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1401 "entering PIC mode.\n");
1402 outb(0x70, 0x22);
1403 outb(0x00, 0x23);
1404 } else {
1405 /* Go back to Virtual Wire compatibility mode */
1406 unsigned long value;
1407
1408 /* For the spurious interrupt use vector F, and enable it */
1409 value = apic_read(APIC_SPIV);
1410 value &= ~APIC_VECTOR_MASK;
1411 value |= APIC_SPIV_APIC_ENABLED;
1412 value |= 0xf;
1413 apic_write_around(APIC_SPIV, value);
1414
1415 if (!virt_wire_setup) {
1416 /*
1417 * For LVT0 make it edge triggered, active high,
1418 * external and enabled
1419 */
1420 value = apic_read(APIC_LVT0);
1421 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1422 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001423 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001424 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1425 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1426 apic_write_around(APIC_LVT0, value);
1427 } else {
1428 /* Disable LVT0 */
1429 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1430 }
1431
1432 /*
1433 * For LVT1 make it edge triggered, active high, nmi and
1434 * enabled
1435 */
1436 value = apic_read(APIC_LVT1);
1437 value &= ~(
1438 APIC_MODE_MASK | APIC_SEND_PENDING |
1439 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1440 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1441 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1442 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1443 apic_write_around(APIC_LVT1, value);
1444 }
1445}
1446
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001447unsigned int __cpuinitdata maxcpus = NR_CPUS;
1448
1449void __cpuinit generic_processor_info(int apicid, int version)
1450{
1451 int cpu;
1452 cpumask_t tmp_map;
1453 physid_mask_t phys_cpu;
1454
1455 /*
1456 * Validate version
1457 */
1458 if (version == 0x0) {
1459 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1460 "fixing up to 0x10. (tell your hw vendor)\n",
1461 version);
1462 version = 0x10;
1463 }
1464 apic_version[apicid] = version;
1465
1466 phys_cpu = apicid_to_cpu_present(apicid);
1467 physids_or(phys_cpu_present_map, phys_cpu_present_map, phys_cpu);
1468
1469 if (num_processors >= NR_CPUS) {
1470 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1471 " Processor ignored.\n", NR_CPUS);
1472 return;
1473 }
1474
1475 if (num_processors >= maxcpus) {
1476 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1477 " Processor ignored.\n", maxcpus);
1478 return;
1479 }
1480
1481 num_processors++;
1482 cpus_complement(tmp_map, cpu_present_map);
1483 cpu = first_cpu(tmp_map);
1484
1485 if (apicid == boot_cpu_physical_apicid)
1486 /*
1487 * x86_bios_cpu_apicid is required to have processors listed
1488 * in same order as logical cpu numbers. Hence the first
1489 * entry is BSP, and so on.
1490 */
1491 cpu = 0;
1492
Yinghai Lue0da3362008-06-08 18:29:22 -07001493 if (apicid > max_physical_apicid)
1494 max_physical_apicid = apicid;
1495
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001496 /*
1497 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1498 * but we need to work other dependencies like SMP_SUSPEND etc
1499 * before this can be done without some confusion.
1500 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1501 * - Ashok Raj <ashok.raj@intel.com>
1502 */
Yinghai Lue0da3362008-06-08 18:29:22 -07001503 if (max_physical_apicid >= 8) {
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001504 switch (boot_cpu_data.x86_vendor) {
1505 case X86_VENDOR_INTEL:
1506 if (!APIC_XAPIC(version)) {
1507 def_to_bigsmp = 0;
1508 break;
1509 }
1510 /* If P4 and above fall through */
1511 case X86_VENDOR_AMD:
1512 def_to_bigsmp = 1;
1513 }
1514 }
1515#ifdef CONFIG_SMP
1516 /* are we being called early in kernel startup? */
Mike Travis23ca4bb2008-05-12 21:21:12 +02001517 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1518 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1519 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001520
1521 cpu_to_apicid[cpu] = apicid;
1522 bios_cpu_apicid[cpu] = apicid;
1523 } else {
1524 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1525 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1526 }
1527#endif
1528 cpu_set(cpu, cpu_possible_map);
1529 cpu_set(cpu, cpu_present_map);
1530}
1531
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001532/*
1533 * Power management
1534 */
1535#ifdef CONFIG_PM
1536
1537static struct {
1538 int active;
1539 /* r/w apic fields */
1540 unsigned int apic_id;
1541 unsigned int apic_taskpri;
1542 unsigned int apic_ldr;
1543 unsigned int apic_dfr;
1544 unsigned int apic_spiv;
1545 unsigned int apic_lvtt;
1546 unsigned int apic_lvtpc;
1547 unsigned int apic_lvt0;
1548 unsigned int apic_lvt1;
1549 unsigned int apic_lvterr;
1550 unsigned int apic_tmict;
1551 unsigned int apic_tdcr;
1552 unsigned int apic_thmr;
1553} apic_pm_state;
1554
1555static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1556{
1557 unsigned long flags;
1558 int maxlvt;
1559
1560 if (!apic_pm_state.active)
1561 return 0;
1562
1563 maxlvt = lapic_get_maxlvt();
1564
1565 apic_pm_state.apic_id = apic_read(APIC_ID);
1566 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1567 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1568 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1569 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1570 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1571 if (maxlvt >= 4)
1572 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1573 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1574 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1575 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1576 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1577 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1578#ifdef CONFIG_X86_MCE_P4THERMAL
1579 if (maxlvt >= 5)
1580 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1581#endif
1582
1583 local_irq_save(flags);
1584 disable_local_APIC();
1585 local_irq_restore(flags);
1586 return 0;
1587}
1588
1589static int lapic_resume(struct sys_device *dev)
1590{
1591 unsigned int l, h;
1592 unsigned long flags;
1593 int maxlvt;
1594
1595 if (!apic_pm_state.active)
1596 return 0;
1597
1598 maxlvt = lapic_get_maxlvt();
1599
1600 local_irq_save(flags);
1601
1602 /*
1603 * Make sure the APICBASE points to the right address
1604 *
1605 * FIXME! This will be wrong if we ever support suspend on
1606 * SMP! We'll need to do this as part of the CPU restore!
1607 */
1608 rdmsr(MSR_IA32_APICBASE, l, h);
1609 l &= ~MSR_IA32_APICBASE_BASE;
1610 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1611 wrmsr(MSR_IA32_APICBASE, l, h);
1612
1613 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1614 apic_write(APIC_ID, apic_pm_state.apic_id);
1615 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1616 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1617 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1618 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1619 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1620 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1621#ifdef CONFIG_X86_MCE_P4THERMAL
1622 if (maxlvt >= 5)
1623 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1624#endif
1625 if (maxlvt >= 4)
1626 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1627 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1628 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1629 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1630 apic_write(APIC_ESR, 0);
1631 apic_read(APIC_ESR);
1632 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1633 apic_write(APIC_ESR, 0);
1634 apic_read(APIC_ESR);
1635 local_irq_restore(flags);
1636 return 0;
1637}
1638
1639/*
1640 * This device has no shutdown method - fully functioning local APICs
1641 * are needed on every CPU up until machine_halt/restart/poweroff.
1642 */
1643
1644static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001645 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001646 .resume = lapic_resume,
1647 .suspend = lapic_suspend,
1648};
1649
1650static struct sys_device device_lapic = {
1651 .id = 0,
1652 .cls = &lapic_sysclass,
1653};
1654
1655static void __devinit apic_pm_activate(void)
1656{
1657 apic_pm_state.active = 1;
1658}
1659
1660static int __init init_lapic_sysfs(void)
1661{
1662 int error;
1663
1664 if (!cpu_has_apic)
1665 return 0;
1666 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1667
1668 error = sysdev_class_register(&lapic_sysclass);
1669 if (!error)
1670 error = sysdev_register(&device_lapic);
1671 return error;
1672}
1673device_initcall(init_lapic_sysfs);
1674
1675#else /* CONFIG_PM */
1676
1677static void apic_pm_activate(void) { }
1678
1679#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001680
1681/*
1682 * APIC command line parameters
1683 */
1684static int __init parse_lapic(char *arg)
1685{
Yinghai Lu914bebf2008-06-29 00:06:37 -07001686 force_enable_local_apic = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001687 return 0;
1688}
1689early_param("lapic", parse_lapic);
1690
1691static int __init parse_nolapic(char *arg)
1692{
Yinghai Lu914bebf2008-06-29 00:06:37 -07001693 disable_apic = 1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001694 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001695 return 0;
1696}
1697early_param("nolapic", parse_nolapic);
1698
1699static int __init parse_disable_lapic_timer(char *arg)
1700{
1701 local_apic_timer_disabled = 1;
1702 return 0;
1703}
1704early_param("nolapic_timer", parse_disable_lapic_timer);
1705
1706static int __init parse_lapic_timer_c2_ok(char *arg)
1707{
1708 local_apic_timer_c2_ok = 1;
1709 return 0;
1710}
1711early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1712
1713static int __init apic_set_verbosity(char *str)
1714{
1715 if (strcmp("debug", str) == 0)
1716 apic_verbosity = APIC_DEBUG;
1717 else if (strcmp("verbose", str) == 0)
1718 apic_verbosity = APIC_VERBOSE;
1719 return 1;
1720}
1721__setup("apic=", apic_set_verbosity);
1722