blob: a151d66f948c8cbf5abb7c5d7b7c82c3524cd4a4 [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 */
Cyrill Gorcunov36fef092008-08-15 13:51:20 +020066static int disable_apic_timer __cpuinitdata;
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 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +010078unsigned int apic_verbosity;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
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
Cyrill Gorcunov746f2eb2008-07-01 21:43:52 +040085static struct resource lapic_resource = {
86 .name = "Local APIC",
87 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
88};
89
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080090static unsigned int calibration_result;
91
92static int lapic_next_event(unsigned long delta,
93 struct clock_event_device *evt);
94static void lapic_timer_setup(enum clock_event_mode mode,
95 struct clock_event_device *evt);
96static void lapic_timer_broadcast(cpumask_t mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void apic_pm_activate(void);
98
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080099/*
100 * The local apic timer can be used for any function which is CPU local.
101 */
102static struct clock_event_device lapic_clockevent = {
103 .name = "lapic",
104 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800105 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800106 .shift = 32,
107 .set_mode = lapic_timer_setup,
108 .set_next_event = lapic_next_event,
109 .broadcast = lapic_timer_broadcast,
110 .rating = 100,
111 .irq = -1,
112};
113static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800115/* Local APIC was disabled by the BIOS and enabled by the kernel */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static int enabled_via_apicbase;
117
Andi Kleend3432892008-01-30 13:33:17 +0100118static unsigned long apic_phys;
119
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800120/*
121 * Get the LAPIC version
122 */
123static inline int lapic_get_version(void)
124{
125 return GET_APIC_VERSION(apic_read(APIC_LVR));
126}
127
128/*
Joe Perchesab4a5742008-01-30 13:31:42 +0100129 * Check, if the APIC is integrated or a separate chip
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800130 */
131static inline int lapic_is_integrated(void)
132{
133 return APIC_INTEGRATED(lapic_get_version());
134}
135
136/*
137 * Check, whether this is a modern or a first generation APIC
138 */
139static int modern_apic(void)
140{
141 /* AMD systems use old APIC versions, so check the CPU */
142 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
143 boot_cpu_data.x86 >= 0xf)
144 return 1;
145 return lapic_get_version() >= 0x14;
146}
147
Suresh Siddha9a8f0e62008-07-18 09:59:40 -0700148/*
149 * Paravirt kernels also might be using these below ops. So we still
150 * use generic apic_read()/apic_write(), which might be pointing to different
151 * ops in PARAVIRT case.
152 */
Yinghai Luc535b6a2008-07-11 18:41:54 -0700153void xapic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200154{
155 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
156 cpu_relax();
157}
158
Yinghai Luc535b6a2008-07-11 18:41:54 -0700159u32 safe_xapic_wait_icr_idle(void)
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200160{
Thomas Gleixner42e0a9a2008-01-30 13:30:15 +0100161 u32 send_status;
Fernando Luis VazquezCaof2b218d2007-05-02 19:27:17 +0200162 int timeout;
163
164 timeout = 0;
165 do {
166 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
167 if (!send_status)
168 break;
169 udelay(100);
170 } while (timeout++ < 1000);
171
172 return send_status;
173}
174
Yinghai Luc535b6a2008-07-11 18:41:54 -0700175void xapic_icr_write(u32 low, u32 id)
176{
Suresh Siddhaf586bf72008-07-18 15:58:35 -0700177 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
178 apic_write(APIC_ICR, low);
Yinghai Luc535b6a2008-07-11 18:41:54 -0700179}
180
181u64 xapic_icr_read(void)
182{
183 u32 icr1, icr2;
184
185 icr2 = apic_read(APIC_ICR2);
186 icr1 = apic_read(APIC_ICR);
187
188 return icr1 | ((u64)icr2 << 32);
189}
190
191static struct apic_ops xapic_ops = {
192 .read = native_apic_mem_read,
193 .write = native_apic_mem_write,
Yinghai Luc535b6a2008-07-11 18:41:54 -0700194 .icr_read = xapic_icr_read,
195 .icr_write = xapic_icr_write,
196 .wait_icr_idle = xapic_wait_icr_idle,
197 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
198};
199
200struct apic_ops __read_mostly *apic_ops = &xapic_ops;
201EXPORT_SYMBOL_GPL(apic_ops);
202
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800203/**
204 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
205 */
Jan Beuliche9427102008-01-30 13:31:24 +0100206void __cpuinit enable_NMI_through_LVT0(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200208 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200210 /* unmask and set to NMI */
211 v = APIC_DM_NMI;
212
213 /* Level triggered for 82489DX (32bit mode) */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800214 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 v |= APIC_LVT_LEVEL_TRIGGER;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200216
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100217 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800220/**
221 * get_physical_broadcast - Get number of physical broadcast IDs
222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223int get_physical_broadcast(void)
224{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800225 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800228/**
229 * lapic_get_maxlvt - get the maximum number of local vector table entries
230 */
231int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200233 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200235 v = apic_read(APIC_LVR);
236 /*
237 * - we always have APIC integrated on 64bit mode
238 * - 82489DXs do not report # of LVT entries
239 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800240 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
243/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800244 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800247/* Clock divisor is set to 16 */
248#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250/*
251 * This function sets up the local APIC timer, with a timeout of
252 * 'clocks' APIC bus clock. During calibration we actually call
253 * this function twice on the boot CPU, once with a bogus timeout
254 * value, second time for real. The other (noncalibrating) CPUs
255 * call this function only once, with the real, calibrated value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800257static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800259 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800261 lvtt_value = LOCAL_TIMER_VECTOR;
262 if (!oneshot)
263 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800264 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100266
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800267 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100268 lvtt_value |= APIC_LVT_MASKED;
269
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100270 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 /*
273 * Divide PICLK by 16
274 */
275 tmp_value = apic_read(APIC_TDCR);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100276 apic_write(APIC_TDCR,
277 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
278 APIC_TDR_DIV_16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800280 if (!oneshot)
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100281 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282}
283
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800284/*
285 * Program the next event, relative to now
286 */
287static int lapic_next_event(unsigned long delta,
288 struct clock_event_device *evt)
289{
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100290 apic_write(APIC_TMICT, delta);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800291 return 0;
292}
293
294/*
295 * Setup the lapic timer in periodic or oneshot mode
296 */
297static void lapic_timer_setup(enum clock_event_mode mode,
298 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800301 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800303 /* Lapic used for broadcast ? */
304 if (!local_apic_timer_verify_ok)
305 return;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 local_irq_save(flags);
308
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800309 switch (mode) {
310 case CLOCK_EVT_MODE_PERIODIC:
311 case CLOCK_EVT_MODE_ONESHOT:
312 __setup_APIC_LVTT(calibration_result,
313 mode != CLOCK_EVT_MODE_PERIODIC, 1);
314 break;
315 case CLOCK_EVT_MODE_UNUSED:
316 case CLOCK_EVT_MODE_SHUTDOWN:
317 v = apic_read(APIC_LVTT);
318 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100319 apic_write(APIC_LVTT, v);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800320 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700321 case CLOCK_EVT_MODE_RESUME:
322 /* Nothing to do here */
323 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 local_irq_restore(flags);
327}
328
329/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800330 * Local APIC timer broadcast function
331 */
332static void lapic_timer_broadcast(cpumask_t mask)
333{
334#ifdef CONFIG_SMP
335 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
336#endif
337}
338
339/*
340 * Setup the local APIC timer for this CPU. Copy the initilized values
341 * of the boot CPU and register the clock event in the framework.
342 */
343static void __devinit setup_APIC_timer(void)
344{
345 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
346
347 memcpy(levt, &lapic_clockevent, sizeof(*levt));
348 levt->cpumask = cpumask_of_cpu(smp_processor_id());
349
350 clockevents_register_device(levt);
351}
352
353/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800354 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800356 * We want to do the calibration only once since we want to have local timer
357 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
358 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800359 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800360 * This was previously done by reading the PIT/HPET and waiting for a wrap
361 * around to find out, that a tick has elapsed. I have a box, where the PIT
362 * readout is broken, so it never gets out of the wait loop again. This was
363 * also reported by others.
364 *
365 * Monitoring the jiffies value is inaccurate and the clockevents
366 * infrastructure allows us to do a simple substitution of the interrupt
367 * handler.
368 *
369 * The calibration routine also uses the pm_timer when possible, as the PIT
370 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
371 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
373
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800374#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200376static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800377static __initdata long lapic_cal_t1, lapic_cal_t2;
378static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
379static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
380static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
381
382/*
383 * Temporary interrupt handler.
384 */
385static void __init lapic_cal_handler(struct clock_event_device *dev)
386{
387 unsigned long long tsc = 0;
388 long tapic = apic_read(APIC_TMCCT);
389 unsigned long pm = acpi_pm_read_early();
390
391 if (cpu_has_tsc)
392 rdtscll(tsc);
393
394 switch (lapic_cal_loops++) {
395 case 0:
396 lapic_cal_t1 = tapic;
397 lapic_cal_tsc1 = tsc;
398 lapic_cal_pm1 = pm;
399 lapic_cal_j1 = jiffies;
400 break;
401
402 case LAPIC_CAL_LOOPS:
403 lapic_cal_t2 = tapic;
404 lapic_cal_tsc2 = tsc;
405 if (pm < lapic_cal_pm1)
406 pm += ACPI_PM_OVRRUN;
407 lapic_cal_pm2 = pm;
408 lapic_cal_j2 = jiffies;
409 break;
410 }
411}
412
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400413static int __init calibrate_APIC_clock(void)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800414{
415 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
416 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
417 const long pm_thresh = pm_100ms/100;
418 void (*real_handler)(struct clock_event_device *dev);
419 unsigned long deltaj;
420 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800421 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800422
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800423 local_irq_disable();
424
425 /* Replace the global interrupt handler */
426 real_handler = global_clock_event->event_handler;
427 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800430 * Setup the APIC counter to 1e9. There is no way the lapic
431 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800433 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800435 /* Let the interrupts run */
436 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800438 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
439 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800441 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800443 /* Restore the real event handler */
444 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800446 /* Build delta t1-t2 as apic timer counts down */
447 delta = lapic_cal_t1 - lapic_cal_t2;
448 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800450 /* Check, if the PM timer is available */
451 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
452 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800454 if (deltapm) {
455 unsigned long mult;
456 u64 res;
457
458 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
459
460 if (deltapm > (pm_100ms - pm_thresh) &&
461 deltapm < (pm_100ms + pm_thresh)) {
462 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
463 } else {
464 res = (((u64) deltapm) * mult) >> 22;
465 do_div(res, 1000000);
466 printk(KERN_WARNING "APIC calibration not consistent "
467 "with PM Timer: %ldms instead of 100ms\n",
468 (long)res);
469 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100470 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800471 do_div(res, deltapm);
472 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
473 "%lu (%ld)\n", (unsigned long) res, delta);
474 delta = (long) res;
475 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800476 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800479 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900480 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
481 lapic_clockevent.shift);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800482 lapic_clockevent.max_delta_ns =
483 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
484 lapic_clockevent.min_delta_ns =
485 clockevent_delta2ns(0xF, &lapic_clockevent);
486
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800487 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800488
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800489 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
490 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
491 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
492 calibration_result);
493
494 if (cpu_has_tsc) {
495 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800497 "%ld.%04ld MHz.\n",
498 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
499 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800503 "%u.%04u MHz.\n",
504 calibration_result / (1000000 / HZ),
505 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100507 /*
508 * Do a sanity check on the APIC calibration result
509 */
510 if (calibration_result < (1000000 / HZ)) {
511 local_irq_enable();
512 printk(KERN_WARNING
513 "APIC frequency too slow, disabling apic timer\n");
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400514 return -1;
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100515 }
516
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400517 local_apic_timer_verify_ok = 1;
518
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800519 /* We trust the pm timer based calibration */
520 if (!pm_referenced) {
521 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800522
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800523 /*
524 * Setup the apic timer manually
525 */
526 levt->event_handler = lapic_cal_handler;
527 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
528 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800529
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800530 /* Let the interrupts run */
531 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800532
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200533 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800534 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800535
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800536 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800537
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800538 /* Stop the lapic timer */
539 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800540
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800541 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800542
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800543 /* Jiffies delta */
544 deltaj = lapic_cal_j2 - lapic_cal_j1;
545 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800546
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800547 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800548 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800549 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800550 else
551 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100552 } else
553 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800554
555 if (!local_apic_timer_verify_ok) {
556 printk(KERN_WARNING
557 "APIC timer disabled due to verification failure.\n");
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400558 return -1;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800559 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800560
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400561 return 0;
562}
563
564/*
565 * Setup the boot APIC
566 *
567 * Calibrate and verify the result.
568 */
569void __init setup_boot_APIC_clock(void)
570{
571 /*
572 * The local apic timer can be disabled via the kernel
573 * commandline or from the CPU detection code. Register the lapic
574 * timer as a dummy clock event source on SMP systems, so the
575 * broadcast mechanism is used. On UP systems simply ignore it.
576 */
Cyrill Gorcunov36fef092008-08-15 13:51:20 +0200577 if (disable_apic_timer) {
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400578 /* No broadcast on UP ! */
579 if (num_possible_cpus() > 1) {
580 lapic_clockevent.mult = 1;
581 setup_APIC_timer();
582 }
583 return;
584 }
585
586 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
587 "calibrating APIC timer ...\n");
588
589 if (calibrate_APIC_clock()) {
590 /* No broadcast on UP ! */
591 if (num_possible_cpus() > 1)
592 setup_APIC_timer();
593 return;
594 }
595
596 /*
597 * If nmi_watchdog is set to IO_APIC, we need the
598 * PIT/HPET going. Otherwise register lapic as a dummy
599 * device.
600 */
601 if (nmi_watchdog != NMI_IO_APIC)
602 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
603 else
604 printk(KERN_WARNING "APIC timer registered as dummy,"
605 " due to nmi_watchdog=%d!\n", nmi_watchdog);
606
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800607 /* Setup the lapic or request the broadcast */
608 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
Li Shaohua0bb31842005-06-25 14:54:55 -0700611void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800613 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800617 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800619static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800621 int cpu = smp_processor_id();
622 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800625 * Normally we should not be here till LAPIC has been initialized but
626 * in some cases like kdump, its possible that there is a pending LAPIC
627 * timer interrupt from previous kernel's context and is delivered in
628 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800630 * Interrupts are enabled early and LAPIC is setup much later, hence
631 * its possible that when we get here evt->event_handler is NULL.
632 * Check for event_handler being NULL and discard the interrupt as
633 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800635 if (!evt->event_handler) {
636 printk(KERN_WARNING
637 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
638 /* Switch it off */
639 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
640 return;
641 }
642
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100643 /*
644 * the NMI deadlock-detector uses this.
645 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800646 per_cpu(irq_stat, cpu).apic_timer_irqs++;
647
648 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651/*
652 * Local APIC timer interrupt. This is the most natural way for doing
653 * local interrupts, but local timer interrupts can be emulated by
654 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
655 *
656 * [ if a single-CPU system runs an SMP kernel then we call the local
657 * interrupt as well. Thus we cannot inline the local irq ... ]
658 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100659void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
David Howells7d12e782006-10-05 14:55:46 +0100661 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 /*
664 * NOTE! We'd better ACK the irq immediately,
665 * because timer handling can be slow.
666 */
667 ack_APIC_irq();
668 /*
669 * update_process_times() expects us to have done irq_enter().
670 * Besides, if we don't timer interrupts ignore the global
671 * interrupt lock, which is the WrongThing (tm) to do.
672 */
673 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800674 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800676
David Howells7d12e782006-10-05 14:55:46 +0100677 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100680int setup_profiling_timer(unsigned int multiplier)
681{
682 return -EINVAL;
683}
684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685/*
Robert Richtere319e762008-02-13 16:19:36 +0100686 * Setup extended LVT, AMD specific (K8, family 10h)
687 *
688 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
689 * MCE interrupts are supported. Thus MCE offset must be set to 0.
690 */
691
692#define APIC_EILVT_LVTOFF_MCE 0
693#define APIC_EILVT_LVTOFF_IBS 1
694
695static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
696{
697 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
698 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
699 apic_write(reg, v);
700}
701
702u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
703{
704 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
705 return APIC_EILVT_LVTOFF_MCE;
706}
707
708u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
709{
710 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
711 return APIC_EILVT_LVTOFF_IBS;
712}
713
714/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800715 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800717
718/**
719 * clear_local_APIC - shutdown the local APIC
720 *
721 * This is called, when a CPU is disabled and before rebooting, so the state of
722 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
723 * leftovers during boot.
724 */
725void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Andi Kleend3432892008-01-30 13:33:17 +0100727 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100728 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
Andi Kleend3432892008-01-30 13:33:17 +0100730 /* APIC hasn't been mapped yet */
731 if (!apic_phys)
732 return;
733
734 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800736 * Masking an LVT entry can trigger a local APIC error
737 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800739 if (maxlvt >= 3) {
740 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100741 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800742 }
743 /*
744 * Careful: we have to set masks only first to deassert
745 * any level-triggered sources.
746 */
747 v = apic_read(APIC_LVTT);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100748 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800749 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100750 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800751 v = apic_read(APIC_LVT1);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100752 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800753 if (maxlvt >= 4) {
754 v = apic_read(APIC_LVTPC);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100755 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800758 /* lets not touch this if we didn't frob it */
759#ifdef CONFIG_X86_MCE_P4THERMAL
760 if (maxlvt >= 5) {
761 v = apic_read(APIC_LVTTHMR);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100762 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800763 }
764#endif
765 /*
766 * Clean APIC state for other OSs:
767 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100768 apic_write(APIC_LVTT, APIC_LVT_MASKED);
769 apic_write(APIC_LVT0, APIC_LVT_MASKED);
770 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800771 if (maxlvt >= 3)
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100772 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800773 if (maxlvt >= 4)
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100774 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800775
776#ifdef CONFIG_X86_MCE_P4THERMAL
777 if (maxlvt >= 5)
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100778 apic_write(APIC_LVTTHMR, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800779#endif
780 /* Integrated APIC (!82489DX) ? */
781 if (lapic_is_integrated()) {
782 if (maxlvt > 3)
783 /* Clear ESR due to Pentium errata 3AP and 11AP */
784 apic_write(APIC_ESR, 0);
785 apic_read(APIC_ESR);
786 }
787}
788
789/**
790 * disable_local_APIC - clear and disable the local APIC
791 */
792void disable_local_APIC(void)
793{
794 unsigned long value;
795
796 clear_local_APIC();
797
798 /*
799 * Disable APIC (implies clearing of registers
800 * for 82489DX!).
801 */
802 value = apic_read(APIC_SPIV);
803 value &= ~APIC_SPIV_APIC_ENABLED;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100804 apic_write(APIC_SPIV, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800805
806 /*
807 * When LAPIC was disabled by the BIOS and enabled by the kernel,
808 * restore the disabled state.
809 */
810 if (enabled_via_apicbase) {
811 unsigned int l, h;
812
813 rdmsr(MSR_IA32_APICBASE, l, h);
814 l &= ~MSR_IA32_APICBASE_ENABLE;
815 wrmsr(MSR_IA32_APICBASE, l, h);
816 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818
819/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800820 * If Linux enabled the LAPIC against the BIOS default disable it down before
821 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
822 * not power-off. Additionally clear all LVT entries before disable_local_APIC
823 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800825void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800827 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800829 if (!cpu_has_apic)
830 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800832 local_irq_save(flags);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800833
834 if (enabled_via_apicbase)
835 disable_local_APIC();
Cyrill Gorcunov9ce122c2008-08-15 13:51:21 +0200836 else
837 clear_local_APIC();
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800838
839 local_irq_restore(flags);
840}
841
842/*
843 * This is to verify that we're looking at a real local APIC.
844 * Check these against your board if the CPUs aren't getting
845 * started for no apparent reason.
846 */
847int __init verify_local_APIC(void)
848{
849 unsigned int reg0, reg1;
850
851 /*
852 * The version register is read-only in a real APIC.
853 */
854 reg0 = apic_read(APIC_LVR);
855 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
856 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
857 reg1 = apic_read(APIC_LVR);
858 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
859
860 /*
861 * The two version reads above should print the same
862 * numbers. If the second one is different, then we
863 * poke at a non-APIC.
864 */
865 if (reg1 != reg0)
866 return 0;
867
868 /*
869 * Check if the version looks reasonably.
870 */
871 reg1 = GET_APIC_VERSION(reg0);
872 if (reg1 == 0x00 || reg1 == 0xff)
873 return 0;
874 reg1 = lapic_get_maxlvt();
875 if (reg1 < 0x02 || reg1 == 0xff)
876 return 0;
877
878 /*
879 * The ID register is read/write in a real APIC.
880 */
881 reg0 = apic_read(APIC_ID);
882 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
883
884 /*
885 * The next two are just to see if we have sane values.
886 * They're only really relevant if we're in Virtual Wire
887 * compatibility mode, but most boxes are anymore.
888 */
889 reg0 = apic_read(APIC_LVT0);
890 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
891 reg1 = apic_read(APIC_LVT1);
892 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
893
894 return 1;
895}
896
897/**
898 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
899 */
900void __init sync_Arb_IDs(void)
901{
902 /*
903 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
904 * needed on AMD.
905 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100906 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800907 return;
908 /*
909 * Wait for idle.
910 */
911 apic_wait_icr_idle();
912
913 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100914 apic_write(APIC_ICR,
915 APIC_DEST_ALLINC | APIC_INT_LEVELTRIG | APIC_DM_INIT);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800916}
917
918/*
919 * An initial setup of the virtual wire mode.
920 */
921void __init init_bsp_APIC(void)
922{
923 unsigned long value;
924
925 /*
926 * Don't do the setup now if we have a SMP BIOS as the
927 * through-I/O-APIC virtual wire mode might be active.
928 */
929 if (smp_found_config || !cpu_has_apic)
930 return;
931
932 /*
933 * Do not trust the local APIC being empty at bootup.
934 */
935 clear_local_APIC();
936
937 /*
938 * Enable APIC.
939 */
940 value = apic_read(APIC_SPIV);
941 value &= ~APIC_VECTOR_MASK;
942 value |= APIC_SPIV_APIC_ENABLED;
943
944 /* This bit is reserved on P4/Xeon and should be cleared */
945 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
946 (boot_cpu_data.x86 == 15))
947 value &= ~APIC_SPIV_FOCUS_DISABLED;
948 else
949 value |= APIC_SPIV_FOCUS_DISABLED;
950 value |= SPURIOUS_APIC_VECTOR;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100951 apic_write(APIC_SPIV, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800952
953 /*
954 * Set up the virtual wire mode.
955 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100956 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800957 value = APIC_DM_NMI;
958 if (!lapic_is_integrated()) /* 82489DX */
959 value |= APIC_LVT_LEVEL_TRIGGER;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100960 apic_write(APIC_LVT1, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800961}
962
Ingo Molnara4928cf2008-04-23 13:20:56 +0200963static void __cpuinit lapic_setup_esr(void)
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300964{
965 unsigned long oldvalue, value, maxlvt;
966 if (lapic_is_integrated() && !esr_disable) {
967 /* !82489DX */
968 maxlvt = lapic_get_maxlvt();
969 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
970 apic_write(APIC_ESR, 0);
971 oldvalue = apic_read(APIC_ESR);
972
973 /* enables sending errors */
974 value = ERROR_APIC_VECTOR;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100975 apic_write(APIC_LVTERR, value);
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300976 /*
977 * spec says clear errors after enabling vector.
978 */
979 if (maxlvt > 3)
980 apic_write(APIC_ESR, 0);
981 value = apic_read(APIC_ESR);
982 if (value != oldvalue)
983 apic_printk(APIC_VERBOSE, "ESR value before enabling "
984 "vector: 0x%08lx after: 0x%08lx\n",
985 oldvalue, value);
986 } else {
987 if (esr_disable)
988 /*
989 * Something untraceable is creating bad interrupts on
990 * secondary quads ... for the moment, just leave the
991 * ESR disabled - we can't do anything useful with the
992 * errors anyway - mbligh
993 */
994 printk(KERN_INFO "Leaving ESR disabled.\n");
995 else
996 printk(KERN_INFO "No ESR for 82489DX.\n");
997 }
998}
999
1000
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001001/**
1002 * setup_local_APIC - setup the local APIC
1003 */
Adrian Bunkd5337982007-12-19 23:20:18 +01001004void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001005{
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001006 unsigned long value, integrated;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001007 int i, j;
1008
1009 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1010 if (esr_disable) {
1011 apic_write(APIC_ESR, 0);
1012 apic_write(APIC_ESR, 0);
1013 apic_write(APIC_ESR, 0);
1014 apic_write(APIC_ESR, 0);
1015 }
1016
1017 integrated = lapic_is_integrated();
1018
1019 /*
1020 * Double-check whether this APIC is really registered.
1021 */
1022 if (!apic_id_registered())
Ingo Molnar22d5c672008-07-10 16:29:28 +02001023 WARN_ON_ONCE(1);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001024
1025 /*
1026 * Intel recommends to set DFR, LDR and TPR before enabling
1027 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1028 * document number 292116). So here it goes...
1029 */
1030 init_apic_ldr();
1031
1032 /*
1033 * Set Task Priority to 'accept all'. We never change this
1034 * later on.
1035 */
1036 value = apic_read(APIC_TASKPRI);
1037 value &= ~APIC_TPRI_MASK;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001038 apic_write(APIC_TASKPRI, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001039
1040 /*
1041 * After a crash, we no longer service the interrupts and a pending
1042 * interrupt from previous kernel might still have ISR bit set.
1043 *
1044 * Most probably by now CPU has serviced that pending interrupt and
1045 * it might not have done the ack_APIC_irq() because it thought,
1046 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1047 * does not clear the ISR bit and cpu thinks it has already serivced
1048 * the interrupt. Hence a vector might get locked. It was noticed
1049 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1050 */
1051 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1052 value = apic_read(APIC_ISR + i*0x10);
1053 for (j = 31; j >= 0; j--) {
1054 if (value & (1<<j))
1055 ack_APIC_irq();
1056 }
1057 }
1058
1059 /*
1060 * Now that we are all set up, enable the APIC
1061 */
1062 value = apic_read(APIC_SPIV);
1063 value &= ~APIC_VECTOR_MASK;
1064 /*
1065 * Enable APIC
1066 */
1067 value |= APIC_SPIV_APIC_ENABLED;
1068
1069 /*
1070 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1071 * certain networking cards. If high frequency interrupts are
1072 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1073 * entry is masked/unmasked at a high rate as well then sooner or
1074 * later IOAPIC line gets 'stuck', no more interrupts are received
1075 * from the device. If focus CPU is disabled then the hang goes
1076 * away, oh well :-(
1077 *
1078 * [ This bug can be reproduced easily with a level-triggered
1079 * PCI Ne2000 networking cards and PII/PIII processors, dual
1080 * BX chipset. ]
1081 */
1082 /*
1083 * Actually disabling the focus CPU check just makes the hang less
1084 * frequent as it makes the interrupt distributon model be more
1085 * like LRU than MRU (the short-term load is more even across CPUs).
1086 * See also the comment in end_level_ioapic_irq(). --macro
1087 */
1088
1089 /* Enable focus processor (bit==0) */
1090 value &= ~APIC_SPIV_FOCUS_DISABLED;
1091
1092 /*
1093 * Set spurious IRQ vector
1094 */
1095 value |= SPURIOUS_APIC_VECTOR;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001096 apic_write(APIC_SPIV, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001097
1098 /*
1099 * Set up LVT0, LVT1:
1100 *
1101 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001102 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001103 * we delegate interrupts to the 8259A.
1104 */
1105 /*
1106 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1107 */
1108 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1109 if (!smp_processor_id() && (pic_mode || !value)) {
1110 value = APIC_DM_EXTINT;
1111 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1112 smp_processor_id());
1113 } else {
1114 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1115 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1116 smp_processor_id());
1117 }
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001118 apic_write(APIC_LVT0, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001119
1120 /*
1121 * only the BP should see the LINT1 NMI signal, obviously.
1122 */
1123 if (!smp_processor_id())
1124 value = APIC_DM_NMI;
1125 else
1126 value = APIC_DM_NMI | APIC_LVT_MASKED;
1127 if (!integrated) /* 82489DX */
1128 value |= APIC_LVT_LEVEL_TRIGGER;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001129 apic_write(APIC_LVT1, value);
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001130}
1131
1132void __cpuinit end_local_APIC_setup(void)
1133{
1134 unsigned long value;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001135
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001136 lapic_setup_esr();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001137 /* Disable the local apic timer */
1138 value = apic_read(APIC_LVTT);
1139 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001140 apic_write(APIC_LVTT, value);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001141
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001142 setup_apic_nmi_watchdog(NULL);
1143 apic_pm_activate();
1144}
1145
1146/*
1147 * Detect and initialize APIC
1148 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001149static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001150{
1151 u32 h, l, features;
1152
1153 /* Disabled by kernel option? */
Yinghai Lu914bebf2008-06-29 00:06:37 -07001154 if (disable_apic)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001155 return -1;
1156
1157 switch (boot_cpu_data.x86_vendor) {
1158 case X86_VENDOR_AMD:
1159 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1160 (boot_cpu_data.x86 == 15))
1161 break;
1162 goto no_apic;
1163 case X86_VENDOR_INTEL:
1164 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1165 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1166 break;
1167 goto no_apic;
1168 default:
1169 goto no_apic;
1170 }
1171
1172 if (!cpu_has_apic) {
1173 /*
1174 * Over-ride BIOS and try to enable the local APIC only if
1175 * "lapic" specified.
1176 */
Yinghai Lu914bebf2008-06-29 00:06:37 -07001177 if (!force_enable_local_apic) {
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001178 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1179 "you can enable it with \"lapic\"\n");
1180 return -1;
1181 }
1182 /*
1183 * Some BIOSes disable the local APIC in the APIC_BASE
1184 * MSR. This can only be done in software for Intel P6 or later
1185 * and AMD K7 (Model > 1) or later.
1186 */
1187 rdmsr(MSR_IA32_APICBASE, l, h);
1188 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1189 printk(KERN_INFO
1190 "Local APIC disabled by BIOS -- reenabling.\n");
1191 l &= ~MSR_IA32_APICBASE_BASE;
1192 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1193 wrmsr(MSR_IA32_APICBASE, l, h);
1194 enabled_via_apicbase = 1;
1195 }
1196 }
1197 /*
1198 * The APIC feature bit should now be enabled
1199 * in `cpuid'
1200 */
1201 features = cpuid_edx(1);
1202 if (!(features & (1 << X86_FEATURE_APIC))) {
1203 printk(KERN_WARNING "Could not enable APIC!\n");
1204 return -1;
1205 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001206 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001207 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1208
1209 /* The BIOS may have set up the APIC at some other address */
1210 rdmsr(MSR_IA32_APICBASE, l, h);
1211 if (l & MSR_IA32_APICBASE_ENABLE)
1212 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1213
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001214 printk(KERN_INFO "Found and enabled local APIC!\n");
1215
1216 apic_pm_activate();
1217
1218 return 0;
1219
1220no_apic:
1221 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1222 return -1;
1223}
1224
1225/**
1226 * init_apic_mappings - initialize APIC mappings
1227 */
1228void __init init_apic_mappings(void)
1229{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001230 /*
1231 * If no local APIC can be found then set up a fake all
1232 * zeroes page to simulate the local APIC and another
1233 * one for the IO-APIC.
1234 */
1235 if (!smp_found_config && detect_init_APIC()) {
1236 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1237 apic_phys = __pa(apic_phys);
1238 } else
1239 apic_phys = mp_lapic_addr;
1240
1241 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1242 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1243 apic_phys);
1244
1245 /*
1246 * Fetch the APIC ID of the BSP in case we have a
1247 * default configuration (or the MP table is broken).
1248 */
1249 if (boot_cpu_physical_apicid == -1U)
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001250 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252}
1253
1254/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001255 * This initializes the IO-APIC and APIC hardware if this is
1256 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 */
Alexey Starikovskiye81b2c62008-03-27 23:54:31 +03001258
1259int apic_version[MAX_APICS];
1260
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001261int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001263 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001264 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001267 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001269 if (!cpu_has_apic &&
1270 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001272 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001273 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 return -1;
1275 }
1276
1277 verify_local_APIC();
1278
1279 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001280
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001281 /*
1282 * Hack: In case of kdump, after a crash, kernel might be booting
1283 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1284 * might be zero if read from MP tables. Get it from LAPIC.
1285 */
1286#ifdef CONFIG_CRASH_DUMP
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001287 boot_cpu_physical_apicid = read_apic_id();
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001288#endif
Jack Steinerb6df1b82008-06-19 21:51:05 -05001289 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 setup_local_APIC();
1292
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001293#ifdef CONFIG_X86_IO_APIC
1294 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1295#endif
1296 localise_nmi_watchdog();
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001297 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001299 if (smp_found_config)
1300 if (!skip_ioapic_setup && nr_ioapics)
1301 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001303 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001304
1305 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001307
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001308/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001309 * Local APIC interrupts
1310 */
1311
1312/*
1313 * This interrupt should _never_ happen with our APIC/SMP architecture
1314 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001315void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001316{
1317 unsigned long v;
1318
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001319 irq_enter();
1320 /*
1321 * Check if this really is a spurious interrupt and ACK it
1322 * if it is a vectored one. Just in case...
1323 * Spurious interrupts should not be ACKed.
1324 */
1325 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1326 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1327 ack_APIC_irq();
1328
1329 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1330 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1331 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001332 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001333 irq_exit();
1334}
1335
1336/*
1337 * This interrupt should never happen with our APIC/SMP architecture
1338 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001339void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001340{
1341 unsigned long v, v1;
1342
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001343 irq_enter();
1344 /* First tickle the hardware, only then report what went on. -- REW */
1345 v = apic_read(APIC_ESR);
1346 apic_write(APIC_ESR, 0);
1347 v1 = apic_read(APIC_ESR);
1348 ack_APIC_irq();
1349 atomic_inc(&irq_err_count);
1350
1351 /* Here is what the APIC error bits mean:
1352 0: Send CS error
1353 1: Receive CS error
1354 2: Send accept error
1355 3: Receive accept error
1356 4: Reserved
1357 5: Send illegal vector
1358 6: Received illegal vector
1359 7: Illegal register address
1360 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001361 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001362 smp_processor_id(), v , v1);
1363 irq_exit();
1364}
1365
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001366/**
1367 * connect_bsp_APIC - attach the APIC to the interrupt system
1368 */
1369void __init connect_bsp_APIC(void)
1370{
1371 if (pic_mode) {
1372 /*
1373 * Do not trust the local APIC being empty at bootup.
1374 */
1375 clear_local_APIC();
1376 /*
1377 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1378 * local APIC to INT and NMI lines.
1379 */
1380 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1381 "enabling APIC mode.\n");
1382 outb(0x70, 0x22);
1383 outb(0x01, 0x23);
1384 }
1385 enable_apic_mode();
1386}
1387
1388/**
1389 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1390 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1391 *
1392 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1393 * APIC is disabled.
1394 */
1395void disconnect_bsp_APIC(int virt_wire_setup)
1396{
1397 if (pic_mode) {
1398 /*
1399 * Put the board back into PIC mode (has an effect only on
1400 * certain older boards). Note that APIC interrupts, including
1401 * IPIs, won't work beyond this point! The only exception are
1402 * INIT IPIs.
1403 */
1404 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1405 "entering PIC mode.\n");
1406 outb(0x70, 0x22);
1407 outb(0x00, 0x23);
1408 } else {
1409 /* Go back to Virtual Wire compatibility mode */
1410 unsigned long value;
1411
1412 /* For the spurious interrupt use vector F, and enable it */
1413 value = apic_read(APIC_SPIV);
1414 value &= ~APIC_VECTOR_MASK;
1415 value |= APIC_SPIV_APIC_ENABLED;
1416 value |= 0xf;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001417 apic_write(APIC_SPIV, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001418
1419 if (!virt_wire_setup) {
1420 /*
1421 * For LVT0 make it edge triggered, active high,
1422 * external and enabled
1423 */
1424 value = apic_read(APIC_LVT0);
1425 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1426 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001427 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001428 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1429 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001430 apic_write(APIC_LVT0, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001431 } else {
1432 /* Disable LVT0 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001433 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001434 }
1435
1436 /*
1437 * For LVT1 make it edge triggered, active high, nmi and
1438 * enabled
1439 */
1440 value = apic_read(APIC_LVT1);
1441 value &= ~(
1442 APIC_MODE_MASK | APIC_SEND_PENDING |
1443 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1444 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1445 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1446 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001447 apic_write(APIC_LVT1, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001448 }
1449}
1450
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001451unsigned int __cpuinitdata maxcpus = NR_CPUS;
1452
1453void __cpuinit generic_processor_info(int apicid, int version)
1454{
1455 int cpu;
1456 cpumask_t tmp_map;
1457 physid_mask_t phys_cpu;
1458
1459 /*
1460 * Validate version
1461 */
1462 if (version == 0x0) {
1463 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1464 "fixing up to 0x10. (tell your hw vendor)\n",
1465 version);
1466 version = 0x10;
1467 }
1468 apic_version[apicid] = version;
1469
1470 phys_cpu = apicid_to_cpu_present(apicid);
1471 physids_or(phys_cpu_present_map, phys_cpu_present_map, phys_cpu);
1472
1473 if (num_processors >= NR_CPUS) {
1474 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1475 " Processor ignored.\n", NR_CPUS);
1476 return;
1477 }
1478
1479 if (num_processors >= maxcpus) {
1480 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1481 " Processor ignored.\n", maxcpus);
1482 return;
1483 }
1484
1485 num_processors++;
1486 cpus_complement(tmp_map, cpu_present_map);
1487 cpu = first_cpu(tmp_map);
1488
1489 if (apicid == boot_cpu_physical_apicid)
1490 /*
1491 * x86_bios_cpu_apicid is required to have processors listed
1492 * in same order as logical cpu numbers. Hence the first
1493 * entry is BSP, and so on.
1494 */
1495 cpu = 0;
1496
Yinghai Lue0da3362008-06-08 18:29:22 -07001497 if (apicid > max_physical_apicid)
1498 max_physical_apicid = apicid;
1499
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001500 /*
1501 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1502 * but we need to work other dependencies like SMP_SUSPEND etc
1503 * before this can be done without some confusion.
1504 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1505 * - Ashok Raj <ashok.raj@intel.com>
1506 */
Yinghai Lue0da3362008-06-08 18:29:22 -07001507 if (max_physical_apicid >= 8) {
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001508 switch (boot_cpu_data.x86_vendor) {
1509 case X86_VENDOR_INTEL:
1510 if (!APIC_XAPIC(version)) {
1511 def_to_bigsmp = 0;
1512 break;
1513 }
1514 /* If P4 and above fall through */
1515 case X86_VENDOR_AMD:
1516 def_to_bigsmp = 1;
1517 }
1518 }
1519#ifdef CONFIG_SMP
1520 /* are we being called early in kernel startup? */
Mike Travis23ca4bb2008-05-12 21:21:12 +02001521 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1522 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1523 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001524
1525 cpu_to_apicid[cpu] = apicid;
1526 bios_cpu_apicid[cpu] = apicid;
1527 } else {
1528 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1529 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1530 }
1531#endif
1532 cpu_set(cpu, cpu_possible_map);
1533 cpu_set(cpu, cpu_present_map);
1534}
1535
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001536/*
1537 * Power management
1538 */
1539#ifdef CONFIG_PM
1540
1541static struct {
1542 int active;
1543 /* r/w apic fields */
1544 unsigned int apic_id;
1545 unsigned int apic_taskpri;
1546 unsigned int apic_ldr;
1547 unsigned int apic_dfr;
1548 unsigned int apic_spiv;
1549 unsigned int apic_lvtt;
1550 unsigned int apic_lvtpc;
1551 unsigned int apic_lvt0;
1552 unsigned int apic_lvt1;
1553 unsigned int apic_lvterr;
1554 unsigned int apic_tmict;
1555 unsigned int apic_tdcr;
1556 unsigned int apic_thmr;
1557} apic_pm_state;
1558
1559static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1560{
1561 unsigned long flags;
1562 int maxlvt;
1563
1564 if (!apic_pm_state.active)
1565 return 0;
1566
1567 maxlvt = lapic_get_maxlvt();
1568
1569 apic_pm_state.apic_id = apic_read(APIC_ID);
1570 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1571 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1572 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1573 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1574 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1575 if (maxlvt >= 4)
1576 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1577 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1578 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1579 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1580 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1581 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1582#ifdef CONFIG_X86_MCE_P4THERMAL
1583 if (maxlvt >= 5)
1584 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1585#endif
1586
1587 local_irq_save(flags);
1588 disable_local_APIC();
1589 local_irq_restore(flags);
1590 return 0;
1591}
1592
1593static int lapic_resume(struct sys_device *dev)
1594{
1595 unsigned int l, h;
1596 unsigned long flags;
1597 int maxlvt;
1598
1599 if (!apic_pm_state.active)
1600 return 0;
1601
1602 maxlvt = lapic_get_maxlvt();
1603
1604 local_irq_save(flags);
1605
1606 /*
1607 * Make sure the APICBASE points to the right address
1608 *
1609 * FIXME! This will be wrong if we ever support suspend on
1610 * SMP! We'll need to do this as part of the CPU restore!
1611 */
1612 rdmsr(MSR_IA32_APICBASE, l, h);
1613 l &= ~MSR_IA32_APICBASE_BASE;
1614 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1615 wrmsr(MSR_IA32_APICBASE, l, h);
1616
1617 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1618 apic_write(APIC_ID, apic_pm_state.apic_id);
1619 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1620 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1621 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1622 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1623 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1624 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1625#ifdef CONFIG_X86_MCE_P4THERMAL
1626 if (maxlvt >= 5)
1627 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1628#endif
1629 if (maxlvt >= 4)
1630 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1631 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1632 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1633 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1634 apic_write(APIC_ESR, 0);
1635 apic_read(APIC_ESR);
1636 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1637 apic_write(APIC_ESR, 0);
1638 apic_read(APIC_ESR);
1639 local_irq_restore(flags);
1640 return 0;
1641}
1642
1643/*
1644 * This device has no shutdown method - fully functioning local APICs
1645 * are needed on every CPU up until machine_halt/restart/poweroff.
1646 */
1647
1648static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001649 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001650 .resume = lapic_resume,
1651 .suspend = lapic_suspend,
1652};
1653
1654static struct sys_device device_lapic = {
1655 .id = 0,
1656 .cls = &lapic_sysclass,
1657};
1658
1659static void __devinit apic_pm_activate(void)
1660{
1661 apic_pm_state.active = 1;
1662}
1663
1664static int __init init_lapic_sysfs(void)
1665{
1666 int error;
1667
1668 if (!cpu_has_apic)
1669 return 0;
1670 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1671
1672 error = sysdev_class_register(&lapic_sysclass);
1673 if (!error)
1674 error = sysdev_register(&device_lapic);
1675 return error;
1676}
1677device_initcall(init_lapic_sysfs);
1678
1679#else /* CONFIG_PM */
1680
1681static void apic_pm_activate(void) { }
1682
1683#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001684
1685/*
1686 * APIC command line parameters
1687 */
1688static int __init parse_lapic(char *arg)
1689{
Yinghai Lu914bebf2008-06-29 00:06:37 -07001690 force_enable_local_apic = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001691 return 0;
1692}
1693early_param("lapic", parse_lapic);
1694
1695static int __init parse_nolapic(char *arg)
1696{
Yinghai Lu914bebf2008-06-29 00:06:37 -07001697 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07001698 setup_clear_cpu_cap(X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001699 return 0;
1700}
1701early_param("nolapic", parse_nolapic);
1702
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001703static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001704{
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001705 disable_apic_timer = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001706 return 0;
1707}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02001708early_param("noapictimer", parse_disable_apic_timer);
1709
1710static int __init parse_nolapic_timer(char *arg)
1711{
1712 disable_apic_timer = 1;
1713 return 0;
1714}
1715early_param("nolapic_timer", parse_nolapic_timer);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001716
1717static int __init parse_lapic_timer_c2_ok(char *arg)
1718{
1719 local_apic_timer_c2_ok = 1;
1720 return 0;
1721}
1722early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1723
Rene Herman48d97cb2008-08-11 19:20:17 +02001724static int __init apic_set_verbosity(char *arg)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001725{
Rene Herman48d97cb2008-08-11 19:20:17 +02001726 if (!arg)
1727 return -EINVAL;
1728
1729 if (strcmp(arg, "debug") == 0)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001730 apic_verbosity = APIC_DEBUG;
Rene Herman48d97cb2008-08-11 19:20:17 +02001731 else if (strcmp(arg, "verbose") == 0)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001732 apic_verbosity = APIC_VERBOSE;
Rene Herman48d97cb2008-08-11 19:20:17 +02001733
Rene Hermanfb6bef82008-08-11 17:45:53 +02001734 return 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001735}
Rene Hermanfb6bef82008-08-11 17:45:53 +02001736early_param("apic", apic_set_verbosity);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001737
Cyrill Gorcunov746f2eb2008-07-01 21:43:52 +04001738static int __init lapic_insert_resource(void)
1739{
1740 if (!apic_phys)
1741 return -1;
1742
1743 /* Put local APIC into the resource map. */
1744 lapic_resource.start = apic_phys;
1745 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1746 insert_resource(&iomem_resource, &lapic_resource);
1747
1748 return 0;
1749}
1750
1751/*
1752 * need call insert after e820_reserve_resources()
1753 * that is using request_resource
1754 */
1755late_initcall(lapic_insert_resource);