blob: 7a2c9cbdb511ebf2150a2bf172aff45621ad5f95 [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>
22#include <linux/smp_lock.h>
23#include <linux/interrupt.h>
24#include <linux/mc146818rtc.h>
25#include <linux/kernel_stat.h>
26#include <linux/sysdev.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070027#include <linux/cpu.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080028#include <linux/clockchips.h>
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080029#include <linux/acpi_pmtmr.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010030#include <linux/module.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
46#include "io_ports.h"
47
48/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080049 * Sanity check
50 */
51#if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
52# error SPURIOUS_APIC_VECTOR definition error
53#endif
54
55/*
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 *
58 * -1=force-disable, +1=force-enable
Eric W. Biederman9635b472005-06-25 14:57:41 -070059 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -080060static int enable_local_apic __initdata = 0;
Eric W. Biederman9635b472005-06-25 14:57:41 -070061
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080062/* Local APIC timer verification ok */
63static int local_apic_timer_verify_ok;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080064
Eric W. Biederman9635b472005-06-25 14:57:41 -070065/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080066 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68int apic_verbosity;
69
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080070static unsigned int calibration_result;
71
72static int lapic_next_event(unsigned long delta,
73 struct clock_event_device *evt);
74static void lapic_timer_setup(enum clock_event_mode mode,
75 struct clock_event_device *evt);
76static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077static void apic_pm_activate(void);
78
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080079/*
80 * The local apic timer can be used for any function which is CPU local.
81 */
82static struct clock_event_device lapic_clockevent = {
83 .name = "lapic",
84 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080085 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080086 .shift = 32,
87 .set_mode = lapic_timer_setup,
88 .set_next_event = lapic_next_event,
89 .broadcast = lapic_timer_broadcast,
90 .rating = 100,
91 .irq = -1,
92};
93static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Thomas Gleixnere05d7232007-02-16 01:27:58 -080095/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096static int enabled_via_apicbase;
97
Thomas Gleixnere05d7232007-02-16 01:27:58 -080098/*
99 * Get the LAPIC version
100 */
101static inline int lapic_get_version(void)
102{
103 return GET_APIC_VERSION(apic_read(APIC_LVR));
104}
105
106/*
107 * Check, if the APIC is integrated or a seperate chip
108 */
109static inline int lapic_is_integrated(void)
110{
111 return APIC_INTEGRATED(lapic_get_version());
112}
113
114/*
115 * Check, whether this is a modern or a first generation APIC
116 */
117static int modern_apic(void)
118{
119 /* AMD systems use old APIC versions, so check the CPU */
120 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
121 boot_cpu_data.x86 >= 0xf)
122 return 1;
123 return lapic_get_version() >= 0x14;
124}
125
126/**
127 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129void enable_NMI_through_LVT0 (void * dummy)
130{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800131 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800133 /* Level triggered for 82489DX */
134 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 v |= APIC_LVT_LEVEL_TRIGGER;
136 apic_write_around(APIC_LVT0, v);
137}
138
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800139/**
140 * get_physical_broadcast - Get number of physical broadcast IDs
141 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142int get_physical_broadcast(void)
143{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800144 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800147/**
148 * lapic_get_maxlvt - get the maximum number of local vector table entries
149 */
150int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800152 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800155 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800159 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800162/* Clock divisor is set to 16 */
163#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165/*
166 * This function sets up the local APIC timer, with a timeout of
167 * 'clocks' APIC bus clock. During calibration we actually call
168 * this function twice on the boot CPU, once with a bogus timeout
169 * value, second time for real. The other (noncalibrating) CPUs
170 * call this function only once, with the real, calibrated value.
171 *
172 * We do reads before writes even if unnecessary, to get around the
173 * P5 APIC double write bug.
174 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800175static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800177 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800179 lvtt_value = LOCAL_TIMER_VECTOR;
180 if (!oneshot)
181 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800182 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100184
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800185 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100186 lvtt_value |= APIC_LVT_MASKED;
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 apic_write_around(APIC_LVTT, lvtt_value);
189
190 /*
191 * Divide PICLK by 16
192 */
193 tmp_value = apic_read(APIC_TDCR);
194 apic_write_around(APIC_TDCR, (tmp_value
195 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
196 | APIC_TDR_DIV_16);
197
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800198 if (!oneshot)
199 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800202/*
203 * Program the next event, relative to now
204 */
205static int lapic_next_event(unsigned long delta,
206 struct clock_event_device *evt)
207{
208 apic_write_around(APIC_TMICT, delta);
209 return 0;
210}
211
212/*
213 * Setup the lapic timer in periodic or oneshot mode
214 */
215static void lapic_timer_setup(enum clock_event_mode mode,
216 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800219 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800221 /* Lapic used for broadcast ? */
222 if (!local_apic_timer_verify_ok)
223 return;
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 local_irq_save(flags);
226
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800227 switch (mode) {
228 case CLOCK_EVT_MODE_PERIODIC:
229 case CLOCK_EVT_MODE_ONESHOT:
230 __setup_APIC_LVTT(calibration_result,
231 mode != CLOCK_EVT_MODE_PERIODIC, 1);
232 break;
233 case CLOCK_EVT_MODE_UNUSED:
234 case CLOCK_EVT_MODE_SHUTDOWN:
235 v = apic_read(APIC_LVTT);
236 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
237 apic_write_around(APIC_LVTT, v);
238 break;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 local_irq_restore(flags);
242}
243
244/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800245 * Local APIC timer broadcast function
246 */
247static void lapic_timer_broadcast(cpumask_t mask)
248{
249#ifdef CONFIG_SMP
250 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
251#endif
252}
253
254/*
255 * Setup the local APIC timer for this CPU. Copy the initilized values
256 * of the boot CPU and register the clock event in the framework.
257 */
258static void __devinit setup_APIC_timer(void)
259{
260 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
261
262 memcpy(levt, &lapic_clockevent, sizeof(*levt));
263 levt->cpumask = cpumask_of_cpu(smp_processor_id());
264
265 clockevents_register_device(levt);
266}
267
268/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800269 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800271 * We want to do the calibration only once since we want to have local timer
272 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
273 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800274 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800275 * This was previously done by reading the PIT/HPET and waiting for a wrap
276 * around to find out, that a tick has elapsed. I have a box, where the PIT
277 * readout is broken, so it never gets out of the wait loop again. This was
278 * also reported by others.
279 *
280 * Monitoring the jiffies value is inaccurate and the clockevents
281 * infrastructure allows us to do a simple substitution of the interrupt
282 * handler.
283 *
284 * The calibration routine also uses the pm_timer when possible, as the PIT
285 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
286 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800289#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800291static __initdata volatile int lapic_cal_loops = -1;
292static __initdata long lapic_cal_t1, lapic_cal_t2;
293static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
294static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
295static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
296
297/*
298 * Temporary interrupt handler.
299 */
300static void __init lapic_cal_handler(struct clock_event_device *dev)
301{
302 unsigned long long tsc = 0;
303 long tapic = apic_read(APIC_TMCCT);
304 unsigned long pm = acpi_pm_read_early();
305
306 if (cpu_has_tsc)
307 rdtscll(tsc);
308
309 switch (lapic_cal_loops++) {
310 case 0:
311 lapic_cal_t1 = tapic;
312 lapic_cal_tsc1 = tsc;
313 lapic_cal_pm1 = pm;
314 lapic_cal_j1 = jiffies;
315 break;
316
317 case LAPIC_CAL_LOOPS:
318 lapic_cal_t2 = tapic;
319 lapic_cal_tsc2 = tsc;
320 if (pm < lapic_cal_pm1)
321 pm += ACPI_PM_OVRRUN;
322 lapic_cal_pm2 = pm;
323 lapic_cal_j2 = jiffies;
324 break;
325 }
326}
327
328/*
329 * Setup the boot APIC
330 *
331 * Calibrate and verify the result.
332 */
333void __init setup_boot_APIC_clock(void)
334{
335 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
336 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
337 const long pm_thresh = pm_100ms/100;
338 void (*real_handler)(struct clock_event_device *dev);
339 unsigned long deltaj;
340 long delta, deltapm;
341
342 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
343 "calibrating APIC timer ...\n");
344
345 local_irq_disable();
346
347 /* Replace the global interrupt handler */
348 real_handler = global_clock_event->event_handler;
349 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800352 * Setup the APIC counter to 1e9. There is no way the lapic
353 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800355 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800357 /* Let the interrupts run */
358 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800360 while(lapic_cal_loops <= LAPIC_CAL_LOOPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800362 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800364 /* Restore the real event handler */
365 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800367 /* Build delta t1-t2 as apic timer counts down */
368 delta = lapic_cal_t1 - lapic_cal_t2;
369 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800371 /* Check, if the PM timer is available */
372 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
373 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800375 if (deltapm) {
376 unsigned long mult;
377 u64 res;
378
379 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
380
381 if (deltapm > (pm_100ms - pm_thresh) &&
382 deltapm < (pm_100ms + pm_thresh)) {
383 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
384 } else {
385 res = (((u64) deltapm) * mult) >> 22;
386 do_div(res, 1000000);
387 printk(KERN_WARNING "APIC calibration not consistent "
388 "with PM Timer: %ldms instead of 100ms\n",
389 (long)res);
390 /* Correct the lapic counter value */
391 res = (((u64) delta ) * pm_100ms);
392 do_div(res, deltapm);
393 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
394 "%lu (%ld)\n", (unsigned long) res, delta);
395 delta = (long) res;
396 }
397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800399 /* Calculate the scaled math multiplication factor */
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800400 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800401 lapic_clockevent.max_delta_ns =
402 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
403 lapic_clockevent.min_delta_ns =
404 clockevent_delta2ns(0xF, &lapic_clockevent);
405
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800406 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800407
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800408 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
409 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
410 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
411 calibration_result);
412
413 if (cpu_has_tsc) {
414 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800416 "%ld.%04ld MHz.\n",
417 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
418 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800422 "%u.%04u MHz.\n",
423 calibration_result / (1000000 / HZ),
424 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800427 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800430 * Setup the apic timer manually
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800432 local_apic_timer_verify_ok = 1;
433 levt->event_handler = lapic_cal_handler;
434 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
435 lapic_cal_loops = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800437 /* Let the interrupts run */
438 local_irq_enable();
439
440 while(lapic_cal_loops <= LAPIC_CAL_LOOPS);
441
442 local_irq_disable();
443
444 /* Stop the lapic timer */
445 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
446
447 local_irq_enable();
448
449 /* Jiffies delta */
450 deltaj = lapic_cal_j2 - lapic_cal_j1;
451 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
452
453 /* Check, if the PM timer is available */
454 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
455 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
456
457 local_apic_timer_verify_ok = 0;
458
459 if (deltapm) {
460 if (deltapm > (pm_100ms - pm_thresh) &&
461 deltapm < (pm_100ms + pm_thresh)) {
462 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
463 /* Check, if the jiffies result is consistent */
464 if (deltaj < LAPIC_CAL_LOOPS-2 ||
465 deltaj > LAPIC_CAL_LOOPS+2) {
466 /*
467 * Not sure, what we can do about this one.
468 * When high resultion timers are active
469 * and the lapic timer does not stop in C3
470 * we are fine. Otherwise more trouble might
471 * be waiting. -- tglx
472 */
473 printk(KERN_WARNING "Global event device %s "
474 "has wrong frequency "
475 "(%lu ticks instead of %d)\n",
476 global_clock_event->name, deltaj,
477 LAPIC_CAL_LOOPS);
478 }
479 local_apic_timer_verify_ok = 1;
480 }
481 } else {
482 /* Check, if the jiffies result is consistent */
483 if (deltaj >= LAPIC_CAL_LOOPS-2 &&
484 deltaj <= LAPIC_CAL_LOOPS+2) {
485 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
486 local_apic_timer_verify_ok = 1;
487 }
488 }
489
490 if (!local_apic_timer_verify_ok) {
491 printk(KERN_WARNING
492 "APIC timer disabled due to verification failure.\n");
493 /* No broadcast on UP ! */
494 if (num_possible_cpus() == 1)
495 return;
496 } else
497 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
498
499 /* Setup the lapic or request the broadcast */
500 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Li Shaohua0bb31842005-06-25 14:54:55 -0700503void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800505 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506}
507
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800509 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800511static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800513 int cpu = smp_processor_id();
514 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800517 * Normally we should not be here till LAPIC has been initialized but
518 * in some cases like kdump, its possible that there is a pending LAPIC
519 * timer interrupt from previous kernel's context and is delivered in
520 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800522 * Interrupts are enabled early and LAPIC is setup much later, hence
523 * its possible that when we get here evt->event_handler is NULL.
524 * Check for event_handler being NULL and discard the interrupt as
525 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800527 if (!evt->event_handler) {
528 printk(KERN_WARNING
529 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
530 /* Switch it off */
531 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
532 return;
533 }
534
535 per_cpu(irq_stat, cpu).apic_timer_irqs++;
536
537 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
540/*
541 * Local APIC timer interrupt. This is the most natural way for doing
542 * local interrupts, but local timer interrupts can be emulated by
543 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
544 *
545 * [ if a single-CPU system runs an SMP kernel then we call the local
546 * interrupt as well. Thus we cannot inline the local irq ... ]
547 */
548
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800549void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550{
David Howells7d12e782006-10-05 14:55:46 +0100551 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 /*
554 * NOTE! We'd better ACK the irq immediately,
555 * because timer handling can be slow.
556 */
557 ack_APIC_irq();
558 /*
559 * update_process_times() expects us to have done irq_enter().
560 * Besides, if we don't timer interrupts ignore the global
561 * interrupt lock, which is the WrongThing (tm) to do.
562 */
563 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800564 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800566
David Howells7d12e782006-10-05 14:55:46 +0100567 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100570int setup_profiling_timer(unsigned int multiplier)
571{
572 return -EINVAL;
573}
574
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800576 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800578
579/**
580 * clear_local_APIC - shutdown the local APIC
581 *
582 * This is called, when a CPU is disabled and before rebooting, so the state of
583 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
584 * leftovers during boot.
585 */
586void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800588 int maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 unsigned long v;
590
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800592 * Masking an LVT entry can trigger a local APIC error
593 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800595 if (maxlvt >= 3) {
596 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
597 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
598 }
599 /*
600 * Careful: we have to set masks only first to deassert
601 * any level-triggered sources.
602 */
603 v = apic_read(APIC_LVTT);
604 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
605 v = apic_read(APIC_LVT0);
606 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
607 v = apic_read(APIC_LVT1);
608 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
609 if (maxlvt >= 4) {
610 v = apic_read(APIC_LVTPC);
611 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800614 /* lets not touch this if we didn't frob it */
615#ifdef CONFIG_X86_MCE_P4THERMAL
616 if (maxlvt >= 5) {
617 v = apic_read(APIC_LVTTHMR);
618 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
619 }
620#endif
621 /*
622 * Clean APIC state for other OSs:
623 */
624 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
625 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
626 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
627 if (maxlvt >= 3)
628 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
629 if (maxlvt >= 4)
630 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
631
632#ifdef CONFIG_X86_MCE_P4THERMAL
633 if (maxlvt >= 5)
634 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
635#endif
636 /* Integrated APIC (!82489DX) ? */
637 if (lapic_is_integrated()) {
638 if (maxlvt > 3)
639 /* Clear ESR due to Pentium errata 3AP and 11AP */
640 apic_write(APIC_ESR, 0);
641 apic_read(APIC_ESR);
642 }
643}
644
645/**
646 * disable_local_APIC - clear and disable the local APIC
647 */
648void disable_local_APIC(void)
649{
650 unsigned long value;
651
652 clear_local_APIC();
653
654 /*
655 * Disable APIC (implies clearing of registers
656 * for 82489DX!).
657 */
658 value = apic_read(APIC_SPIV);
659 value &= ~APIC_SPIV_APIC_ENABLED;
660 apic_write_around(APIC_SPIV, value);
661
662 /*
663 * When LAPIC was disabled by the BIOS and enabled by the kernel,
664 * restore the disabled state.
665 */
666 if (enabled_via_apicbase) {
667 unsigned int l, h;
668
669 rdmsr(MSR_IA32_APICBASE, l, h);
670 l &= ~MSR_IA32_APICBASE_ENABLE;
671 wrmsr(MSR_IA32_APICBASE, l, h);
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673}
674
675/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800676 * If Linux enabled the LAPIC against the BIOS default disable it down before
677 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
678 * not power-off. Additionally clear all LVT entries before disable_local_APIC
679 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800681void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800683 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800685 if (!cpu_has_apic)
686 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800688 local_irq_save(flags);
689 clear_local_APIC();
690
691 if (enabled_via_apicbase)
692 disable_local_APIC();
693
694 local_irq_restore(flags);
695}
696
697/*
698 * This is to verify that we're looking at a real local APIC.
699 * Check these against your board if the CPUs aren't getting
700 * started for no apparent reason.
701 */
702int __init verify_local_APIC(void)
703{
704 unsigned int reg0, reg1;
705
706 /*
707 * The version register is read-only in a real APIC.
708 */
709 reg0 = apic_read(APIC_LVR);
710 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
711 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
712 reg1 = apic_read(APIC_LVR);
713 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
714
715 /*
716 * The two version reads above should print the same
717 * numbers. If the second one is different, then we
718 * poke at a non-APIC.
719 */
720 if (reg1 != reg0)
721 return 0;
722
723 /*
724 * Check if the version looks reasonably.
725 */
726 reg1 = GET_APIC_VERSION(reg0);
727 if (reg1 == 0x00 || reg1 == 0xff)
728 return 0;
729 reg1 = lapic_get_maxlvt();
730 if (reg1 < 0x02 || reg1 == 0xff)
731 return 0;
732
733 /*
734 * The ID register is read/write in a real APIC.
735 */
736 reg0 = apic_read(APIC_ID);
737 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
738
739 /*
740 * The next two are just to see if we have sane values.
741 * They're only really relevant if we're in Virtual Wire
742 * compatibility mode, but most boxes are anymore.
743 */
744 reg0 = apic_read(APIC_LVT0);
745 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
746 reg1 = apic_read(APIC_LVT1);
747 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
748
749 return 1;
750}
751
752/**
753 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
754 */
755void __init sync_Arb_IDs(void)
756{
757 /*
758 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
759 * needed on AMD.
760 */
761 if (modern_apic())
762 return;
763 /*
764 * Wait for idle.
765 */
766 apic_wait_icr_idle();
767
768 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
769 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
770 | APIC_DM_INIT);
771}
772
773/*
774 * An initial setup of the virtual wire mode.
775 */
776void __init init_bsp_APIC(void)
777{
778 unsigned long value;
779
780 /*
781 * Don't do the setup now if we have a SMP BIOS as the
782 * through-I/O-APIC virtual wire mode might be active.
783 */
784 if (smp_found_config || !cpu_has_apic)
785 return;
786
787 /*
788 * Do not trust the local APIC being empty at bootup.
789 */
790 clear_local_APIC();
791
792 /*
793 * Enable APIC.
794 */
795 value = apic_read(APIC_SPIV);
796 value &= ~APIC_VECTOR_MASK;
797 value |= APIC_SPIV_APIC_ENABLED;
798
799 /* This bit is reserved on P4/Xeon and should be cleared */
800 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
801 (boot_cpu_data.x86 == 15))
802 value &= ~APIC_SPIV_FOCUS_DISABLED;
803 else
804 value |= APIC_SPIV_FOCUS_DISABLED;
805 value |= SPURIOUS_APIC_VECTOR;
806 apic_write_around(APIC_SPIV, value);
807
808 /*
809 * Set up the virtual wire mode.
810 */
811 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
812 value = APIC_DM_NMI;
813 if (!lapic_is_integrated()) /* 82489DX */
814 value |= APIC_LVT_LEVEL_TRIGGER;
815 apic_write_around(APIC_LVT1, value);
816}
817
818/**
819 * setup_local_APIC - setup the local APIC
820 */
821void __devinit setup_local_APIC(void)
822{
823 unsigned long oldvalue, value, maxlvt, integrated;
824 int i, j;
825
826 /* Pound the ESR really hard over the head with a big hammer - mbligh */
827 if (esr_disable) {
828 apic_write(APIC_ESR, 0);
829 apic_write(APIC_ESR, 0);
830 apic_write(APIC_ESR, 0);
831 apic_write(APIC_ESR, 0);
832 }
833
834 integrated = lapic_is_integrated();
835
836 /*
837 * Double-check whether this APIC is really registered.
838 */
839 if (!apic_id_registered())
840 BUG();
841
842 /*
843 * Intel recommends to set DFR, LDR and TPR before enabling
844 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
845 * document number 292116). So here it goes...
846 */
847 init_apic_ldr();
848
849 /*
850 * Set Task Priority to 'accept all'. We never change this
851 * later on.
852 */
853 value = apic_read(APIC_TASKPRI);
854 value &= ~APIC_TPRI_MASK;
855 apic_write_around(APIC_TASKPRI, value);
856
857 /*
858 * After a crash, we no longer service the interrupts and a pending
859 * interrupt from previous kernel might still have ISR bit set.
860 *
861 * Most probably by now CPU has serviced that pending interrupt and
862 * it might not have done the ack_APIC_irq() because it thought,
863 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
864 * does not clear the ISR bit and cpu thinks it has already serivced
865 * the interrupt. Hence a vector might get locked. It was noticed
866 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
867 */
868 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
869 value = apic_read(APIC_ISR + i*0x10);
870 for (j = 31; j >= 0; j--) {
871 if (value & (1<<j))
872 ack_APIC_irq();
873 }
874 }
875
876 /*
877 * Now that we are all set up, enable the APIC
878 */
879 value = apic_read(APIC_SPIV);
880 value &= ~APIC_VECTOR_MASK;
881 /*
882 * Enable APIC
883 */
884 value |= APIC_SPIV_APIC_ENABLED;
885
886 /*
887 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
888 * certain networking cards. If high frequency interrupts are
889 * happening on a particular IOAPIC pin, plus the IOAPIC routing
890 * entry is masked/unmasked at a high rate as well then sooner or
891 * later IOAPIC line gets 'stuck', no more interrupts are received
892 * from the device. If focus CPU is disabled then the hang goes
893 * away, oh well :-(
894 *
895 * [ This bug can be reproduced easily with a level-triggered
896 * PCI Ne2000 networking cards and PII/PIII processors, dual
897 * BX chipset. ]
898 */
899 /*
900 * Actually disabling the focus CPU check just makes the hang less
901 * frequent as it makes the interrupt distributon model be more
902 * like LRU than MRU (the short-term load is more even across CPUs).
903 * See also the comment in end_level_ioapic_irq(). --macro
904 */
905
906 /* Enable focus processor (bit==0) */
907 value &= ~APIC_SPIV_FOCUS_DISABLED;
908
909 /*
910 * Set spurious IRQ vector
911 */
912 value |= SPURIOUS_APIC_VECTOR;
913 apic_write_around(APIC_SPIV, value);
914
915 /*
916 * Set up LVT0, LVT1:
917 *
918 * set up through-local-APIC on the BP's LINT0. This is not
919 * strictly necessery in pure symmetric-IO mode, but sometimes
920 * we delegate interrupts to the 8259A.
921 */
922 /*
923 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
924 */
925 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
926 if (!smp_processor_id() && (pic_mode || !value)) {
927 value = APIC_DM_EXTINT;
928 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
929 smp_processor_id());
930 } else {
931 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
932 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
933 smp_processor_id());
934 }
935 apic_write_around(APIC_LVT0, value);
936
937 /*
938 * only the BP should see the LINT1 NMI signal, obviously.
939 */
940 if (!smp_processor_id())
941 value = APIC_DM_NMI;
942 else
943 value = APIC_DM_NMI | APIC_LVT_MASKED;
944 if (!integrated) /* 82489DX */
945 value |= APIC_LVT_LEVEL_TRIGGER;
946 apic_write_around(APIC_LVT1, value);
947
948 if (integrated && !esr_disable) { /* !82489DX */
949 maxlvt = lapic_get_maxlvt();
950 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
951 apic_write(APIC_ESR, 0);
952 oldvalue = apic_read(APIC_ESR);
953
954 /* enables sending errors */
955 value = ERROR_APIC_VECTOR;
956 apic_write_around(APIC_LVTERR, value);
957 /*
958 * spec says clear errors after enabling vector.
959 */
960 if (maxlvt > 3)
961 apic_write(APIC_ESR, 0);
962 value = apic_read(APIC_ESR);
963 if (value != oldvalue)
964 apic_printk(APIC_VERBOSE, "ESR value before enabling "
965 "vector: 0x%08lx after: 0x%08lx\n",
966 oldvalue, value);
967 } else {
968 if (esr_disable)
969 /*
970 * Something untraceble is creating bad interrupts on
971 * secondary quads ... for the moment, just leave the
972 * ESR disabled - we can't do anything useful with the
973 * errors anyway - mbligh
974 */
975 printk(KERN_INFO "Leaving ESR disabled.\n");
976 else
977 printk(KERN_INFO "No ESR for 82489DX.\n");
978 }
979
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800980 /* Disable the local apic timer */
981 value = apic_read(APIC_LVTT);
982 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
983 apic_write_around(APIC_LVTT, value);
984
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800985 setup_apic_nmi_watchdog(NULL);
986 apic_pm_activate();
987}
988
989/*
990 * Detect and initialize APIC
991 */
992static int __init detect_init_APIC (void)
993{
994 u32 h, l, features;
995
996 /* Disabled by kernel option? */
997 if (enable_local_apic < 0)
998 return -1;
999
1000 switch (boot_cpu_data.x86_vendor) {
1001 case X86_VENDOR_AMD:
1002 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1003 (boot_cpu_data.x86 == 15))
1004 break;
1005 goto no_apic;
1006 case X86_VENDOR_INTEL:
1007 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1008 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1009 break;
1010 goto no_apic;
1011 default:
1012 goto no_apic;
1013 }
1014
1015 if (!cpu_has_apic) {
1016 /*
1017 * Over-ride BIOS and try to enable the local APIC only if
1018 * "lapic" specified.
1019 */
1020 if (enable_local_apic <= 0) {
1021 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1022 "you can enable it with \"lapic\"\n");
1023 return -1;
1024 }
1025 /*
1026 * Some BIOSes disable the local APIC in the APIC_BASE
1027 * MSR. This can only be done in software for Intel P6 or later
1028 * and AMD K7 (Model > 1) or later.
1029 */
1030 rdmsr(MSR_IA32_APICBASE, l, h);
1031 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1032 printk(KERN_INFO
1033 "Local APIC disabled by BIOS -- reenabling.\n");
1034 l &= ~MSR_IA32_APICBASE_BASE;
1035 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1036 wrmsr(MSR_IA32_APICBASE, l, h);
1037 enabled_via_apicbase = 1;
1038 }
1039 }
1040 /*
1041 * The APIC feature bit should now be enabled
1042 * in `cpuid'
1043 */
1044 features = cpuid_edx(1);
1045 if (!(features & (1 << X86_FEATURE_APIC))) {
1046 printk(KERN_WARNING "Could not enable APIC!\n");
1047 return -1;
1048 }
1049 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1050 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1051
1052 /* The BIOS may have set up the APIC at some other address */
1053 rdmsr(MSR_IA32_APICBASE, l, h);
1054 if (l & MSR_IA32_APICBASE_ENABLE)
1055 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1056
1057 if (nmi_watchdog != NMI_NONE)
1058 nmi_watchdog = NMI_LOCAL_APIC;
1059
1060 printk(KERN_INFO "Found and enabled local APIC!\n");
1061
1062 apic_pm_activate();
1063
1064 return 0;
1065
1066no_apic:
1067 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1068 return -1;
1069}
1070
1071/**
1072 * init_apic_mappings - initialize APIC mappings
1073 */
1074void __init init_apic_mappings(void)
1075{
1076 unsigned long apic_phys;
1077
1078 /*
1079 * If no local APIC can be found then set up a fake all
1080 * zeroes page to simulate the local APIC and another
1081 * one for the IO-APIC.
1082 */
1083 if (!smp_found_config && detect_init_APIC()) {
1084 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1085 apic_phys = __pa(apic_phys);
1086 } else
1087 apic_phys = mp_lapic_addr;
1088
1089 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1090 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1091 apic_phys);
1092
1093 /*
1094 * Fetch the APIC ID of the BSP in case we have a
1095 * default configuration (or the MP table is broken).
1096 */
1097 if (boot_cpu_physical_apicid == -1U)
1098 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1099
1100#ifdef CONFIG_X86_IO_APIC
1101 {
1102 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1103 int i;
1104
1105 for (i = 0; i < nr_ioapics; i++) {
1106 if (smp_found_config) {
1107 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1108 if (!ioapic_phys) {
1109 printk(KERN_ERR
1110 "WARNING: bogus zero IO-APIC "
1111 "address found in MPTABLE, "
1112 "disabling IO/APIC support!\n");
1113 smp_found_config = 0;
1114 skip_ioapic_setup = 1;
1115 goto fake_ioapic_page;
1116 }
1117 } else {
1118fake_ioapic_page:
1119 ioapic_phys = (unsigned long)
1120 alloc_bootmem_pages(PAGE_SIZE);
1121 ioapic_phys = __pa(ioapic_phys);
1122 }
1123 set_fixmap_nocache(idx, ioapic_phys);
1124 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1125 __fix_to_virt(idx), ioapic_phys);
1126 idx++;
1127 }
1128 }
1129#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001133 * This initializes the IO-APIC and APIC hardware if this is
1134 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001136int __init APIC_init_uniprocessor (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001138 if (enable_local_apic < 0)
1139 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001140
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001141 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001142 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001145 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001147 if (!cpu_has_apic &&
1148 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001150 boot_cpu_physical_apicid);
Andi Kleen3777a952006-02-03 21:51:53 +01001151 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return -1;
1153 }
1154
1155 verify_local_APIC();
1156
1157 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001158
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001159 /*
1160 * Hack: In case of kdump, after a crash, kernel might be booting
1161 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1162 * might be zero if read from MP tables. Get it from LAPIC.
1163 */
1164#ifdef CONFIG_CRASH_DUMP
1165 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1166#endif
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001167 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 setup_local_APIC();
1170
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001172 if (smp_found_config)
1173 if (!skip_ioapic_setup && nr_ioapics)
1174 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001176 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001177
1178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001180
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001181/*
1182 * APIC command line parameters
1183 */
Rusty Russell1a3f2392006-09-26 10:52:32 +02001184static int __init parse_lapic(char *arg)
1185{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001186 enable_local_apic = 1;
Rusty Russell1a3f2392006-09-26 10:52:32 +02001187 return 0;
1188}
1189early_param("lapic", parse_lapic);
1190
1191static int __init parse_nolapic(char *arg)
1192{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001193 enable_local_apic = -1;
1194 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Rusty Russell1a3f2392006-09-26 10:52:32 +02001195 return 0;
1196}
1197early_param("nolapic", parse_nolapic);
1198
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001199static int __init apic_set_verbosity(char *str)
1200{
1201 if (strcmp("debug", str) == 0)
1202 apic_verbosity = APIC_DEBUG;
1203 else if (strcmp("verbose", str) == 0)
1204 apic_verbosity = APIC_VERBOSE;
1205 return 1;
1206}
1207
1208__setup("apic=", apic_set_verbosity);
1209
1210
1211/*
1212 * Local APIC interrupts
1213 */
1214
1215/*
1216 * This interrupt should _never_ happen with our APIC/SMP architecture
1217 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001218void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001219{
1220 unsigned long v;
1221
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001222 irq_enter();
1223 /*
1224 * Check if this really is a spurious interrupt and ACK it
1225 * if it is a vectored one. Just in case...
1226 * Spurious interrupts should not be ACKed.
1227 */
1228 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1229 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1230 ack_APIC_irq();
1231
1232 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1233 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1234 "should never happen.\n", smp_processor_id());
1235 irq_exit();
1236}
1237
1238/*
1239 * This interrupt should never happen with our APIC/SMP architecture
1240 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001241void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001242{
1243 unsigned long v, v1;
1244
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001245 irq_enter();
1246 /* First tickle the hardware, only then report what went on. -- REW */
1247 v = apic_read(APIC_ESR);
1248 apic_write(APIC_ESR, 0);
1249 v1 = apic_read(APIC_ESR);
1250 ack_APIC_irq();
1251 atomic_inc(&irq_err_count);
1252
1253 /* Here is what the APIC error bits mean:
1254 0: Send CS error
1255 1: Receive CS error
1256 2: Send accept error
1257 3: Receive accept error
1258 4: Reserved
1259 5: Send illegal vector
1260 6: Received illegal vector
1261 7: Illegal register address
1262 */
1263 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1264 smp_processor_id(), v , v1);
1265 irq_exit();
1266}
1267
1268/*
1269 * Initialize APIC interrupts
1270 */
1271void __init apic_intr_init(void)
1272{
1273#ifdef CONFIG_SMP
1274 smp_intr_init();
1275#endif
1276 /* self generated IPI for local APIC timer */
1277 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1278
1279 /* IPI vectors for APIC spurious and error interrupts */
1280 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1281 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1282
1283 /* thermal monitor LVT interrupt */
1284#ifdef CONFIG_X86_MCE_P4THERMAL
1285 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1286#endif
1287}
1288
1289/**
1290 * connect_bsp_APIC - attach the APIC to the interrupt system
1291 */
1292void __init connect_bsp_APIC(void)
1293{
1294 if (pic_mode) {
1295 /*
1296 * Do not trust the local APIC being empty at bootup.
1297 */
1298 clear_local_APIC();
1299 /*
1300 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1301 * local APIC to INT and NMI lines.
1302 */
1303 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1304 "enabling APIC mode.\n");
1305 outb(0x70, 0x22);
1306 outb(0x01, 0x23);
1307 }
1308 enable_apic_mode();
1309}
1310
1311/**
1312 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1313 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1314 *
1315 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1316 * APIC is disabled.
1317 */
1318void disconnect_bsp_APIC(int virt_wire_setup)
1319{
1320 if (pic_mode) {
1321 /*
1322 * Put the board back into PIC mode (has an effect only on
1323 * certain older boards). Note that APIC interrupts, including
1324 * IPIs, won't work beyond this point! The only exception are
1325 * INIT IPIs.
1326 */
1327 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1328 "entering PIC mode.\n");
1329 outb(0x70, 0x22);
1330 outb(0x00, 0x23);
1331 } else {
1332 /* Go back to Virtual Wire compatibility mode */
1333 unsigned long value;
1334
1335 /* For the spurious interrupt use vector F, and enable it */
1336 value = apic_read(APIC_SPIV);
1337 value &= ~APIC_VECTOR_MASK;
1338 value |= APIC_SPIV_APIC_ENABLED;
1339 value |= 0xf;
1340 apic_write_around(APIC_SPIV, value);
1341
1342 if (!virt_wire_setup) {
1343 /*
1344 * For LVT0 make it edge triggered, active high,
1345 * external and enabled
1346 */
1347 value = apic_read(APIC_LVT0);
1348 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1349 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1350 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1351 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1352 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1353 apic_write_around(APIC_LVT0, value);
1354 } else {
1355 /* Disable LVT0 */
1356 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1357 }
1358
1359 /*
1360 * For LVT1 make it edge triggered, active high, nmi and
1361 * enabled
1362 */
1363 value = apic_read(APIC_LVT1);
1364 value &= ~(
1365 APIC_MODE_MASK | APIC_SEND_PENDING |
1366 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1367 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1368 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1369 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1370 apic_write_around(APIC_LVT1, value);
1371 }
1372}
1373
1374/*
1375 * Power management
1376 */
1377#ifdef CONFIG_PM
1378
1379static struct {
1380 int active;
1381 /* r/w apic fields */
1382 unsigned int apic_id;
1383 unsigned int apic_taskpri;
1384 unsigned int apic_ldr;
1385 unsigned int apic_dfr;
1386 unsigned int apic_spiv;
1387 unsigned int apic_lvtt;
1388 unsigned int apic_lvtpc;
1389 unsigned int apic_lvt0;
1390 unsigned int apic_lvt1;
1391 unsigned int apic_lvterr;
1392 unsigned int apic_tmict;
1393 unsigned int apic_tdcr;
1394 unsigned int apic_thmr;
1395} apic_pm_state;
1396
1397static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1398{
1399 unsigned long flags;
1400 int maxlvt;
1401
1402 if (!apic_pm_state.active)
1403 return 0;
1404
1405 maxlvt = lapic_get_maxlvt();
1406
1407 apic_pm_state.apic_id = apic_read(APIC_ID);
1408 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1409 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1410 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1411 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1412 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1413 if (maxlvt >= 4)
1414 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1415 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1416 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1417 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1418 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1419 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1420#ifdef CONFIG_X86_MCE_P4THERMAL
1421 if (maxlvt >= 5)
1422 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1423#endif
1424
1425 local_irq_save(flags);
1426 disable_local_APIC();
1427 local_irq_restore(flags);
1428 return 0;
1429}
1430
1431static int lapic_resume(struct sys_device *dev)
1432{
1433 unsigned int l, h;
1434 unsigned long flags;
1435 int maxlvt;
1436
1437 if (!apic_pm_state.active)
1438 return 0;
1439
1440 maxlvt = lapic_get_maxlvt();
1441
1442 local_irq_save(flags);
1443
1444 /*
1445 * Make sure the APICBASE points to the right address
1446 *
1447 * FIXME! This will be wrong if we ever support suspend on
1448 * SMP! We'll need to do this as part of the CPU restore!
1449 */
1450 rdmsr(MSR_IA32_APICBASE, l, h);
1451 l &= ~MSR_IA32_APICBASE_BASE;
1452 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1453 wrmsr(MSR_IA32_APICBASE, l, h);
1454
1455 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1456 apic_write(APIC_ID, apic_pm_state.apic_id);
1457 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1458 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1459 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1460 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1461 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1462 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1463#ifdef CONFIG_X86_MCE_P4THERMAL
1464 if (maxlvt >= 5)
1465 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1466#endif
1467 if (maxlvt >= 4)
1468 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1469 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1470 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1471 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1472 apic_write(APIC_ESR, 0);
1473 apic_read(APIC_ESR);
1474 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1475 apic_write(APIC_ESR, 0);
1476 apic_read(APIC_ESR);
1477 local_irq_restore(flags);
1478 return 0;
1479}
1480
1481/*
1482 * This device has no shutdown method - fully functioning local APICs
1483 * are needed on every CPU up until machine_halt/restart/poweroff.
1484 */
1485
1486static struct sysdev_class lapic_sysclass = {
1487 set_kset_name("lapic"),
1488 .resume = lapic_resume,
1489 .suspend = lapic_suspend,
1490};
1491
1492static struct sys_device device_lapic = {
1493 .id = 0,
1494 .cls = &lapic_sysclass,
1495};
1496
1497static void __devinit apic_pm_activate(void)
1498{
1499 apic_pm_state.active = 1;
1500}
1501
1502static int __init init_lapic_sysfs(void)
1503{
1504 int error;
1505
1506 if (!cpu_has_apic)
1507 return 0;
1508 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1509
1510 error = sysdev_class_register(&lapic_sysclass);
1511 if (!error)
1512 error = sysdev_register(&device_lapic);
1513 return error;
1514}
1515device_initcall(init_lapic_sysfs);
1516
1517#else /* CONFIG_PM */
1518
1519static void apic_pm_activate(void) { }
1520
1521#endif /* CONFIG_PM */