blob: 8728f54a93d8dd4f02e7f40aadbe9daf26401018 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/init.h>
18
19#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/delay.h>
21#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/interrupt.h>
23#include <linux/mc146818rtc.h>
24#include <linux/kernel_stat.h>
25#include <linux/sysdev.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070026#include <linux/cpu.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080027#include <linux/clockchips.h>
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080028#include <linux/acpi_pmtmr.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010029#include <linux/module.h>
Thomas Gleixnerad62ca22007-03-22 00:11:21 -080030#include <linux/dmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <asm/atomic.h>
33#include <asm/smp.h>
34#include <asm/mtrr.h>
35#include <asm/mpspec.h>
36#include <asm/desc.h>
37#include <asm/arch_hooks.h>
38#include <asm/hpet.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070039#include <asm/i8253.h>
Don Zickus3e4ff112006-06-26 13:57:01 +020040#include <asm/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <mach_apic.h>
Jesper Juhl382dbd02006-03-23 02:59:49 -080043#include <mach_apicdef.h>
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +010044#include <mach_ipi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080047 * Sanity check
48 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +010049#if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
Thomas Gleixnere05d7232007-02-16 01:27:58 -080050# error SPURIOUS_APIC_VECTOR definition error
51#endif
52
Alexey Starikovskiy8f6e2ca2008-03-27 23:54:38 +030053unsigned long mp_lapic_addr;
54
Thomas Gleixnere05d7232007-02-16 01:27:58 -080055/*
Eric W. Biederman9635b472005-06-25 14:57:41 -070056 * Knob to control our willingness to enable the local APIC.
Thomas Gleixnere05d7232007-02-16 01:27:58 -080057 *
Yinghai Lu914bebf2008-06-29 00:06:37 -070058 * +1=force-enable
Eric W. Biederman9635b472005-06-25 14:57:41 -070059 */
Yinghai Lu914bebf2008-06-29 00:06:37 -070060static int force_enable_local_apic;
61int disable_apic;
Eric W. Biederman9635b472005-06-25 14:57:41 -070062
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -080063/* Local APIC timer verification ok */
64static int local_apic_timer_verify_ok;
Thomas Gleixneraa276e12008-06-09 19:15:00 +020065/* Disable local APIC timer from the kernel commandline or via dmi quirk */
66static int local_apic_timer_disabled;
Thomas Gleixnere585bef2007-03-23 16:08:01 +010067/* Local APIC timer works in C2 */
68int local_apic_timer_c2_ok;
69EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080070
Alan Mayerce178332008-04-16 15:17:20 -050071int first_system_vector = 0xfe;
72
73char system_vectors[NR_VECTORS] = { [0 ... NR_VECTORS-1] = SYS_VECTOR_FREE};
74
Eric W. Biederman9635b472005-06-25 14:57:41 -070075/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -080076 * Debug level, exported for io_apic.c
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 */
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{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800208 unsigned int v = APIC_DM_NMI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800210 /* Level triggered for 82489DX */
211 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 v |= APIC_LVT_LEVEL_TRIGGER;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100213 apic_write(APIC_LVT0, v);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214}
215
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800216/**
217 * get_physical_broadcast - Get number of physical broadcast IDs
218 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219int get_physical_broadcast(void)
220{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800221 return modern_apic() ? 0xff : 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800224/**
225 * lapic_get_maxlvt - get the maximum number of local vector table entries
226 */
227int lapic_get_maxlvt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800229 unsigned int v = apic_read(APIC_LVR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 /* 82489DXs do not report # of LVT entries. */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800232 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
235/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800236 * Local APIC timer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800239/* Clock divisor is set to 16 */
240#define APIC_DIVISOR 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242/*
243 * This function sets up the local APIC timer, with a timeout of
244 * 'clocks' APIC bus clock. During calibration we actually call
245 * this function twice on the boot CPU, once with a bogus timeout
246 * value, second time for real. The other (noncalibrating) CPUs
247 * call this function only once, with the real, calibrated value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800249static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800251 unsigned int lvtt_value, tmp_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800253 lvtt_value = LOCAL_TIMER_VECTOR;
254 if (!oneshot)
255 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800256 if (!lapic_is_integrated())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100258
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800259 if (!irqen)
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100260 lvtt_value |= APIC_LVT_MASKED;
261
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100262 apic_write(APIC_LVTT, lvtt_value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 /*
265 * Divide PICLK by 16
266 */
267 tmp_value = apic_read(APIC_TDCR);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100268 apic_write(APIC_TDCR,
269 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
270 APIC_TDR_DIV_16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800272 if (!oneshot)
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100273 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800276/*
277 * Program the next event, relative to now
278 */
279static int lapic_next_event(unsigned long delta,
280 struct clock_event_device *evt)
281{
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100282 apic_write(APIC_TMICT, delta);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800283 return 0;
284}
285
286/*
287 * Setup the lapic timer in periodic or oneshot mode
288 */
289static void lapic_timer_setup(enum clock_event_mode mode,
290 struct clock_event_device *evt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 unsigned long flags;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800293 unsigned int v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800295 /* Lapic used for broadcast ? */
296 if (!local_apic_timer_verify_ok)
297 return;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 local_irq_save(flags);
300
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800301 switch (mode) {
302 case CLOCK_EVT_MODE_PERIODIC:
303 case CLOCK_EVT_MODE_ONESHOT:
304 __setup_APIC_LVTT(calibration_result,
305 mode != CLOCK_EVT_MODE_PERIODIC, 1);
306 break;
307 case CLOCK_EVT_MODE_UNUSED:
308 case CLOCK_EVT_MODE_SHUTDOWN:
309 v = apic_read(APIC_LVTT);
310 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100311 apic_write(APIC_LVTT, v);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800312 break;
Thomas Gleixner18de5bc2007-07-21 04:37:34 -0700313 case CLOCK_EVT_MODE_RESUME:
314 /* Nothing to do here */
315 break;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318 local_irq_restore(flags);
319}
320
321/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800322 * Local APIC timer broadcast function
323 */
324static void lapic_timer_broadcast(cpumask_t mask)
325{
326#ifdef CONFIG_SMP
327 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
328#endif
329}
330
331/*
332 * Setup the local APIC timer for this CPU. Copy the initilized values
333 * of the boot CPU and register the clock event in the framework.
334 */
335static void __devinit setup_APIC_timer(void)
336{
337 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
338
339 memcpy(levt, &lapic_clockevent, sizeof(*levt));
340 levt->cpumask = cpumask_of_cpu(smp_processor_id());
341
342 clockevents_register_device(levt);
343}
344
345/*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800346 * In this functions we calibrate APIC bus clocks to the external timer.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800348 * We want to do the calibration only once since we want to have local timer
349 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
350 * frequency.
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800351 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800352 * This was previously done by reading the PIT/HPET and waiting for a wrap
353 * around to find out, that a tick has elapsed. I have a box, where the PIT
354 * readout is broken, so it never gets out of the wait loop again. This was
355 * also reported by others.
356 *
357 * Monitoring the jiffies value is inaccurate and the clockevents
358 * infrastructure allows us to do a simple substitution of the interrupt
359 * handler.
360 *
361 * The calibration routine also uses the pm_timer when possible, as the PIT
362 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
363 * back to normal later in the boot process).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
365
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800366#define LAPIC_CAL_LOOPS (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200368static __initdata int lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800369static __initdata long lapic_cal_t1, lapic_cal_t2;
370static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
371static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
372static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
373
374/*
375 * Temporary interrupt handler.
376 */
377static void __init lapic_cal_handler(struct clock_event_device *dev)
378{
379 unsigned long long tsc = 0;
380 long tapic = apic_read(APIC_TMCCT);
381 unsigned long pm = acpi_pm_read_early();
382
383 if (cpu_has_tsc)
384 rdtscll(tsc);
385
386 switch (lapic_cal_loops++) {
387 case 0:
388 lapic_cal_t1 = tapic;
389 lapic_cal_tsc1 = tsc;
390 lapic_cal_pm1 = pm;
391 lapic_cal_j1 = jiffies;
392 break;
393
394 case LAPIC_CAL_LOOPS:
395 lapic_cal_t2 = tapic;
396 lapic_cal_tsc2 = tsc;
397 if (pm < lapic_cal_pm1)
398 pm += ACPI_PM_OVRRUN;
399 lapic_cal_pm2 = pm;
400 lapic_cal_j2 = jiffies;
401 break;
402 }
403}
404
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400405static int __init calibrate_APIC_clock(void)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800406{
407 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
408 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
409 const long pm_thresh = pm_100ms/100;
410 void (*real_handler)(struct clock_event_device *dev);
411 unsigned long deltaj;
412 long delta, deltapm;
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800413 int pm_referenced = 0;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800414
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800415 local_irq_disable();
416
417 /* Replace the global interrupt handler */
418 real_handler = global_clock_event->event_handler;
419 global_clock_event->event_handler = lapic_cal_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800422 * Setup the APIC counter to 1e9. There is no way the lapic
423 * can underflow in the 100ms detection time frame
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800425 __setup_APIC_LVTT(1000000000, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800427 /* Let the interrupts run */
428 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800430 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
431 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800433 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800435 /* Restore the real event handler */
436 global_clock_event->event_handler = real_handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800438 /* Build delta t1-t2 as apic timer counts down */
439 delta = lapic_cal_t1 - lapic_cal_t2;
440 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800442 /* Check, if the PM timer is available */
443 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
444 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800446 if (deltapm) {
447 unsigned long mult;
448 u64 res;
449
450 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
451
452 if (deltapm > (pm_100ms - pm_thresh) &&
453 deltapm < (pm_100ms + pm_thresh)) {
454 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
455 } else {
456 res = (((u64) deltapm) * mult) >> 22;
457 do_div(res, 1000000);
458 printk(KERN_WARNING "APIC calibration not consistent "
459 "with PM Timer: %ldms instead of 100ms\n",
460 (long)res);
461 /* Correct the lapic counter value */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +0100462 res = (((u64) delta) * pm_100ms);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800463 do_div(res, deltapm);
464 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
465 "%lu (%ld)\n", (unsigned long) res, delta);
466 delta = (long) res;
467 }
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800468 pm_referenced = 1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800469 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800471 /* Calculate the scaled math multiplication factor */
Akinobu Mita877084f2008-04-19 23:55:16 +0900472 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
473 lapic_clockevent.shift);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800474 lapic_clockevent.max_delta_ns =
475 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
476 lapic_clockevent.min_delta_ns =
477 clockevent_delta2ns(0xF, &lapic_clockevent);
478
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800479 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800480
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800481 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
482 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
483 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
484 calibration_result);
485
486 if (cpu_has_tsc) {
487 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800489 "%ld.%04ld MHz.\n",
490 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
491 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800495 "%u.%04u MHz.\n",
496 calibration_result / (1000000 / HZ),
497 calibration_result % (1000000 / HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100499 /*
500 * Do a sanity check on the APIC calibration result
501 */
502 if (calibration_result < (1000000 / HZ)) {
503 local_irq_enable();
504 printk(KERN_WARNING
505 "APIC frequency too slow, disabling apic timer\n");
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400506 return -1;
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100507 }
508
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400509 local_apic_timer_verify_ok = 1;
510
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800511 /* We trust the pm timer based calibration */
512 if (!pm_referenced) {
513 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800514
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800515 /*
516 * Setup the apic timer manually
517 */
518 levt->event_handler = lapic_cal_handler;
519 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
520 lapic_cal_loops = -1;
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800521
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800522 /* Let the interrupts run */
523 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800524
Thomas Gleixnerf5352fd2007-07-21 17:11:32 +0200525 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800526 cpu_relax();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800527
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800528 local_irq_disable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800529
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800530 /* Stop the lapic timer */
531 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800532
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800533 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800534
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800535 /* Jiffies delta */
536 deltaj = lapic_cal_j2 - lapic_cal_j1;
537 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800538
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800539 /* Check, if the jiffies result is consistent */
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800540 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800541 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
Thomas Gleixnerca1b9402007-03-18 01:26:13 -0800542 else
543 local_apic_timer_verify_ok = 0;
Ingo Molnar4edc5db2007-03-22 10:31:19 +0100544 } else
545 local_irq_enable();
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800546
547 if (!local_apic_timer_verify_ok) {
548 printk(KERN_WARNING
549 "APIC timer disabled due to verification failure.\n");
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400550 return -1;
Thomas Gleixnera5f5e432007-03-05 00:30:45 -0800551 }
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800552
Cyrill Gorcunov836c1292008-07-15 21:02:55 +0400553 return 0;
554}
555
556/*
557 * Setup the boot APIC
558 *
559 * Calibrate and verify the result.
560 */
561void __init setup_boot_APIC_clock(void)
562{
563 /*
564 * The local apic timer can be disabled via the kernel
565 * commandline or from the CPU detection code. Register the lapic
566 * timer as a dummy clock event source on SMP systems, so the
567 * broadcast mechanism is used. On UP systems simply ignore it.
568 */
569 if (local_apic_timer_disabled) {
570 /* No broadcast on UP ! */
571 if (num_possible_cpus() > 1) {
572 lapic_clockevent.mult = 1;
573 setup_APIC_timer();
574 }
575 return;
576 }
577
578 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
579 "calibrating APIC timer ...\n");
580
581 if (calibrate_APIC_clock()) {
582 /* No broadcast on UP ! */
583 if (num_possible_cpus() > 1)
584 setup_APIC_timer();
585 return;
586 }
587
588 /*
589 * If nmi_watchdog is set to IO_APIC, we need the
590 * PIT/HPET going. Otherwise register lapic as a dummy
591 * device.
592 */
593 if (nmi_watchdog != NMI_IO_APIC)
594 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
595 else
596 printk(KERN_WARNING "APIC timer registered as dummy,"
597 " due to nmi_watchdog=%d!\n", nmi_watchdog);
598
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800599 /* Setup the lapic or request the broadcast */
600 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Li Shaohua0bb31842005-06-25 14:54:55 -0700603void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800605 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800609 * The guts of the apic timer interrupt
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800611static void local_apic_timer_interrupt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800613 int cpu = smp_processor_id();
614 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 /*
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800617 * Normally we should not be here till LAPIC has been initialized but
618 * in some cases like kdump, its possible that there is a pending LAPIC
619 * timer interrupt from previous kernel's context and is delivered in
620 * new kernel the moment interrupts are enabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 *
Thomas Gleixnerd36b49b2007-02-16 01:28:06 -0800622 * Interrupts are enabled early and LAPIC is setup much later, hence
623 * its possible that when we get here evt->event_handler is NULL.
624 * Check for event_handler being NULL and discard the interrupt as
625 * spurious.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800627 if (!evt->event_handler) {
628 printk(KERN_WARNING
629 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
630 /* Switch it off */
631 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
632 return;
633 }
634
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100635 /*
636 * the NMI deadlock-detector uses this.
637 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800638 per_cpu(irq_stat, cpu).apic_timer_irqs++;
639
640 evt->event_handler(evt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643/*
644 * Local APIC timer interrupt. This is the most natural way for doing
645 * local interrupts, but local timer interrupts can be emulated by
646 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
647 *
648 * [ if a single-CPU system runs an SMP kernel then we call the local
649 * interrupt as well. Thus we cannot inline the local irq ... ]
650 */
Harvey Harrison75604d72008-01-30 13:31:17 +0100651void smp_apic_timer_interrupt(struct pt_regs *regs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
David Howells7d12e782006-10-05 14:55:46 +0100653 struct pt_regs *old_regs = set_irq_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655 /*
656 * NOTE! We'd better ACK the irq immediately,
657 * because timer handling can be slow.
658 */
659 ack_APIC_irq();
660 /*
661 * update_process_times() expects us to have done irq_enter().
662 * Besides, if we don't timer interrupts ignore the global
663 * interrupt lock, which is the WrongThing (tm) to do.
664 */
665 irq_enter();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800666 local_apic_timer_interrupt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 irq_exit();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800668
David Howells7d12e782006-10-05 14:55:46 +0100669 set_irq_regs(old_regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +0100672int setup_profiling_timer(unsigned int multiplier)
673{
674 return -EINVAL;
675}
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677/*
Robert Richtere319e762008-02-13 16:19:36 +0100678 * Setup extended LVT, AMD specific (K8, family 10h)
679 *
680 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
681 * MCE interrupts are supported. Thus MCE offset must be set to 0.
682 */
683
684#define APIC_EILVT_LVTOFF_MCE 0
685#define APIC_EILVT_LVTOFF_IBS 1
686
687static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
688{
689 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
690 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
691 apic_write(reg, v);
692}
693
694u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
695{
696 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
697 return APIC_EILVT_LVTOFF_MCE;
698}
699
700u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
701{
702 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
703 return APIC_EILVT_LVTOFF_IBS;
704}
705
706/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800707 * Local APIC start and shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800709
710/**
711 * clear_local_APIC - shutdown the local APIC
712 *
713 * This is called, when a CPU is disabled and before rebooting, so the state of
714 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
715 * leftovers during boot.
716 */
717void clear_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718{
Andi Kleend3432892008-01-30 13:33:17 +0100719 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100720 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Andi Kleend3432892008-01-30 13:33:17 +0100722 /* APIC hasn't been mapped yet */
723 if (!apic_phys)
724 return;
725
726 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 /*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800728 * Masking an LVT entry can trigger a local APIC error
729 * if the vector is zero. Mask LVTERR first to prevent this.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800731 if (maxlvt >= 3) {
732 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100733 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800734 }
735 /*
736 * Careful: we have to set masks only first to deassert
737 * any level-triggered sources.
738 */
739 v = apic_read(APIC_LVTT);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100740 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800741 v = apic_read(APIC_LVT0);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100742 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800743 v = apic_read(APIC_LVT1);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100744 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800745 if (maxlvt >= 4) {
746 v = apic_read(APIC_LVTPC);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100747 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800750 /* lets not touch this if we didn't frob it */
751#ifdef CONFIG_X86_MCE_P4THERMAL
752 if (maxlvt >= 5) {
753 v = apic_read(APIC_LVTTHMR);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100754 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800755 }
756#endif
757 /*
758 * Clean APIC state for other OSs:
759 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100760 apic_write(APIC_LVTT, APIC_LVT_MASKED);
761 apic_write(APIC_LVT0, APIC_LVT_MASKED);
762 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800763 if (maxlvt >= 3)
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100764 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800765 if (maxlvt >= 4)
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100766 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800767
768#ifdef CONFIG_X86_MCE_P4THERMAL
769 if (maxlvt >= 5)
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100770 apic_write(APIC_LVTTHMR, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800771#endif
772 /* Integrated APIC (!82489DX) ? */
773 if (lapic_is_integrated()) {
774 if (maxlvt > 3)
775 /* Clear ESR due to Pentium errata 3AP and 11AP */
776 apic_write(APIC_ESR, 0);
777 apic_read(APIC_ESR);
778 }
779}
780
781/**
782 * disable_local_APIC - clear and disable the local APIC
783 */
784void disable_local_APIC(void)
785{
786 unsigned long value;
787
788 clear_local_APIC();
789
790 /*
791 * Disable APIC (implies clearing of registers
792 * for 82489DX!).
793 */
794 value = apic_read(APIC_SPIV);
795 value &= ~APIC_SPIV_APIC_ENABLED;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100796 apic_write(APIC_SPIV, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800797
798 /*
799 * When LAPIC was disabled by the BIOS and enabled by the kernel,
800 * restore the disabled state.
801 */
802 if (enabled_via_apicbase) {
803 unsigned int l, h;
804
805 rdmsr(MSR_IA32_APICBASE, l, h);
806 l &= ~MSR_IA32_APICBASE_ENABLE;
807 wrmsr(MSR_IA32_APICBASE, l, h);
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
810
811/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800812 * If Linux enabled the LAPIC against the BIOS default disable it down before
813 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
814 * not power-off. Additionally clear all LVT entries before disable_local_APIC
815 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800817void lapic_shutdown(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800819 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800821 if (!cpu_has_apic)
822 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800824 local_irq_save(flags);
825 clear_local_APIC();
826
827 if (enabled_via_apicbase)
828 disable_local_APIC();
829
830 local_irq_restore(flags);
831}
832
833/*
834 * This is to verify that we're looking at a real local APIC.
835 * Check these against your board if the CPUs aren't getting
836 * started for no apparent reason.
837 */
838int __init verify_local_APIC(void)
839{
840 unsigned int reg0, reg1;
841
842 /*
843 * The version register is read-only in a real APIC.
844 */
845 reg0 = apic_read(APIC_LVR);
846 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
847 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
848 reg1 = apic_read(APIC_LVR);
849 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
850
851 /*
852 * The two version reads above should print the same
853 * numbers. If the second one is different, then we
854 * poke at a non-APIC.
855 */
856 if (reg1 != reg0)
857 return 0;
858
859 /*
860 * Check if the version looks reasonably.
861 */
862 reg1 = GET_APIC_VERSION(reg0);
863 if (reg1 == 0x00 || reg1 == 0xff)
864 return 0;
865 reg1 = lapic_get_maxlvt();
866 if (reg1 < 0x02 || reg1 == 0xff)
867 return 0;
868
869 /*
870 * The ID register is read/write in a real APIC.
871 */
872 reg0 = apic_read(APIC_ID);
873 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
874
875 /*
876 * The next two are just to see if we have sane values.
877 * They're only really relevant if we're in Virtual Wire
878 * compatibility mode, but most boxes are anymore.
879 */
880 reg0 = apic_read(APIC_LVT0);
881 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
882 reg1 = apic_read(APIC_LVT1);
883 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
884
885 return 1;
886}
887
888/**
889 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
890 */
891void __init sync_Arb_IDs(void)
892{
893 /*
894 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
895 * needed on AMD.
896 */
Ingo Molnarf44d9ef2007-11-26 20:42:20 +0100897 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800898 return;
899 /*
900 * Wait for idle.
901 */
902 apic_wait_icr_idle();
903
904 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100905 apic_write(APIC_ICR,
906 APIC_DEST_ALLINC | APIC_INT_LEVELTRIG | APIC_DM_INIT);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800907}
908
909/*
910 * An initial setup of the virtual wire mode.
911 */
912void __init init_bsp_APIC(void)
913{
914 unsigned long value;
915
916 /*
917 * Don't do the setup now if we have a SMP BIOS as the
918 * through-I/O-APIC virtual wire mode might be active.
919 */
920 if (smp_found_config || !cpu_has_apic)
921 return;
922
923 /*
924 * Do not trust the local APIC being empty at bootup.
925 */
926 clear_local_APIC();
927
928 /*
929 * Enable APIC.
930 */
931 value = apic_read(APIC_SPIV);
932 value &= ~APIC_VECTOR_MASK;
933 value |= APIC_SPIV_APIC_ENABLED;
934
935 /* This bit is reserved on P4/Xeon and should be cleared */
936 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
937 (boot_cpu_data.x86 == 15))
938 value &= ~APIC_SPIV_FOCUS_DISABLED;
939 else
940 value |= APIC_SPIV_FOCUS_DISABLED;
941 value |= SPURIOUS_APIC_VECTOR;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100942 apic_write(APIC_SPIV, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800943
944 /*
945 * Set up the virtual wire mode.
946 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100947 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800948 value = APIC_DM_NMI;
949 if (!lapic_is_integrated()) /* 82489DX */
950 value |= APIC_LVT_LEVEL_TRIGGER;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100951 apic_write(APIC_LVT1, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800952}
953
Ingo Molnara4928cf2008-04-23 13:20:56 +0200954static void __cpuinit lapic_setup_esr(void)
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300955{
956 unsigned long oldvalue, value, maxlvt;
957 if (lapic_is_integrated() && !esr_disable) {
958 /* !82489DX */
959 maxlvt = lapic_get_maxlvt();
960 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
961 apic_write(APIC_ESR, 0);
962 oldvalue = apic_read(APIC_ESR);
963
964 /* enables sending errors */
965 value = ERROR_APIC_VECTOR;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +0100966 apic_write(APIC_LVTERR, value);
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300967 /*
968 * spec says clear errors after enabling vector.
969 */
970 if (maxlvt > 3)
971 apic_write(APIC_ESR, 0);
972 value = apic_read(APIC_ESR);
973 if (value != oldvalue)
974 apic_printk(APIC_VERBOSE, "ESR value before enabling "
975 "vector: 0x%08lx after: 0x%08lx\n",
976 oldvalue, value);
977 } else {
978 if (esr_disable)
979 /*
980 * Something untraceable is creating bad interrupts on
981 * secondary quads ... for the moment, just leave the
982 * ESR disabled - we can't do anything useful with the
983 * errors anyway - mbligh
984 */
985 printk(KERN_INFO "Leaving ESR disabled.\n");
986 else
987 printk(KERN_INFO "No ESR for 82489DX.\n");
988 }
989}
990
991
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800992/**
993 * setup_local_APIC - setup the local APIC
994 */
Adrian Bunkd5337982007-12-19 23:20:18 +0100995void __cpuinit setup_local_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800996{
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -0300997 unsigned long value, integrated;
Thomas Gleixnere05d7232007-02-16 01:27:58 -0800998 int i, j;
999
1000 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1001 if (esr_disable) {
1002 apic_write(APIC_ESR, 0);
1003 apic_write(APIC_ESR, 0);
1004 apic_write(APIC_ESR, 0);
1005 apic_write(APIC_ESR, 0);
1006 }
1007
1008 integrated = lapic_is_integrated();
1009
1010 /*
1011 * Double-check whether this APIC is really registered.
1012 */
1013 if (!apic_id_registered())
Ingo Molnar22d5c672008-07-10 16:29:28 +02001014 WARN_ON_ONCE(1);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001015
1016 /*
1017 * Intel recommends to set DFR, LDR and TPR before enabling
1018 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1019 * document number 292116). So here it goes...
1020 */
1021 init_apic_ldr();
1022
1023 /*
1024 * Set Task Priority to 'accept all'. We never change this
1025 * later on.
1026 */
1027 value = apic_read(APIC_TASKPRI);
1028 value &= ~APIC_TPRI_MASK;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001029 apic_write(APIC_TASKPRI, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001030
1031 /*
1032 * After a crash, we no longer service the interrupts and a pending
1033 * interrupt from previous kernel might still have ISR bit set.
1034 *
1035 * Most probably by now CPU has serviced that pending interrupt and
1036 * it might not have done the ack_APIC_irq() because it thought,
1037 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1038 * does not clear the ISR bit and cpu thinks it has already serivced
1039 * the interrupt. Hence a vector might get locked. It was noticed
1040 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1041 */
1042 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1043 value = apic_read(APIC_ISR + i*0x10);
1044 for (j = 31; j >= 0; j--) {
1045 if (value & (1<<j))
1046 ack_APIC_irq();
1047 }
1048 }
1049
1050 /*
1051 * Now that we are all set up, enable the APIC
1052 */
1053 value = apic_read(APIC_SPIV);
1054 value &= ~APIC_VECTOR_MASK;
1055 /*
1056 * Enable APIC
1057 */
1058 value |= APIC_SPIV_APIC_ENABLED;
1059
1060 /*
1061 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1062 * certain networking cards. If high frequency interrupts are
1063 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1064 * entry is masked/unmasked at a high rate as well then sooner or
1065 * later IOAPIC line gets 'stuck', no more interrupts are received
1066 * from the device. If focus CPU is disabled then the hang goes
1067 * away, oh well :-(
1068 *
1069 * [ This bug can be reproduced easily with a level-triggered
1070 * PCI Ne2000 networking cards and PII/PIII processors, dual
1071 * BX chipset. ]
1072 */
1073 /*
1074 * Actually disabling the focus CPU check just makes the hang less
1075 * frequent as it makes the interrupt distributon model be more
1076 * like LRU than MRU (the short-term load is more even across CPUs).
1077 * See also the comment in end_level_ioapic_irq(). --macro
1078 */
1079
1080 /* Enable focus processor (bit==0) */
1081 value &= ~APIC_SPIV_FOCUS_DISABLED;
1082
1083 /*
1084 * Set spurious IRQ vector
1085 */
1086 value |= SPURIOUS_APIC_VECTOR;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001087 apic_write(APIC_SPIV, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001088
1089 /*
1090 * Set up LVT0, LVT1:
1091 *
1092 * set up through-local-APIC on the BP's LINT0. This is not
Simon Arlott27b46d72007-10-20 01:13:56 +02001093 * strictly necessary in pure symmetric-IO mode, but sometimes
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001094 * we delegate interrupts to the 8259A.
1095 */
1096 /*
1097 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1098 */
1099 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1100 if (!smp_processor_id() && (pic_mode || !value)) {
1101 value = APIC_DM_EXTINT;
1102 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1103 smp_processor_id());
1104 } else {
1105 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1106 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1107 smp_processor_id());
1108 }
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001109 apic_write(APIC_LVT0, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001110
1111 /*
1112 * only the BP should see the LINT1 NMI signal, obviously.
1113 */
1114 if (!smp_processor_id())
1115 value = APIC_DM_NMI;
1116 else
1117 value = APIC_DM_NMI | APIC_LVT_MASKED;
1118 if (!integrated) /* 82489DX */
1119 value |= APIC_LVT_LEVEL_TRIGGER;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001120 apic_write(APIC_LVT1, value);
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001121}
1122
1123void __cpuinit end_local_APIC_setup(void)
1124{
1125 unsigned long value;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001126
Glauber de Oliveira Costadf7939a2008-03-19 14:25:48 -03001127 lapic_setup_esr();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001128 /* Disable the local apic timer */
1129 value = apic_read(APIC_LVTT);
1130 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001131 apic_write(APIC_LVTT, value);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001132
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001133 setup_apic_nmi_watchdog(NULL);
1134 apic_pm_activate();
1135}
1136
1137/*
1138 * Detect and initialize APIC
1139 */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001140static int __init detect_init_APIC(void)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001141{
1142 u32 h, l, features;
1143
1144 /* Disabled by kernel option? */
Yinghai Lu914bebf2008-06-29 00:06:37 -07001145 if (disable_apic)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001146 return -1;
1147
1148 switch (boot_cpu_data.x86_vendor) {
1149 case X86_VENDOR_AMD:
1150 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1151 (boot_cpu_data.x86 == 15))
1152 break;
1153 goto no_apic;
1154 case X86_VENDOR_INTEL:
1155 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1156 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1157 break;
1158 goto no_apic;
1159 default:
1160 goto no_apic;
1161 }
1162
1163 if (!cpu_has_apic) {
1164 /*
1165 * Over-ride BIOS and try to enable the local APIC only if
1166 * "lapic" specified.
1167 */
Yinghai Lu914bebf2008-06-29 00:06:37 -07001168 if (!force_enable_local_apic) {
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001169 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1170 "you can enable it with \"lapic\"\n");
1171 return -1;
1172 }
1173 /*
1174 * Some BIOSes disable the local APIC in the APIC_BASE
1175 * MSR. This can only be done in software for Intel P6 or later
1176 * and AMD K7 (Model > 1) or later.
1177 */
1178 rdmsr(MSR_IA32_APICBASE, l, h);
1179 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1180 printk(KERN_INFO
1181 "Local APIC disabled by BIOS -- reenabling.\n");
1182 l &= ~MSR_IA32_APICBASE_BASE;
1183 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1184 wrmsr(MSR_IA32_APICBASE, l, h);
1185 enabled_via_apicbase = 1;
1186 }
1187 }
1188 /*
1189 * The APIC feature bit should now be enabled
1190 * in `cpuid'
1191 */
1192 features = cpuid_edx(1);
1193 if (!(features & (1 << X86_FEATURE_APIC))) {
1194 printk(KERN_WARNING "Could not enable APIC!\n");
1195 return -1;
1196 }
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001197 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001198 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1199
1200 /* The BIOS may have set up the APIC at some other address */
1201 rdmsr(MSR_IA32_APICBASE, l, h);
1202 if (l & MSR_IA32_APICBASE_ENABLE)
1203 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1204
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001205 printk(KERN_INFO "Found and enabled local APIC!\n");
1206
1207 apic_pm_activate();
1208
1209 return 0;
1210
1211no_apic:
1212 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1213 return -1;
1214}
1215
1216/**
1217 * init_apic_mappings - initialize APIC mappings
1218 */
1219void __init init_apic_mappings(void)
1220{
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001221 /*
1222 * If no local APIC can be found then set up a fake all
1223 * zeroes page to simulate the local APIC and another
1224 * one for the IO-APIC.
1225 */
1226 if (!smp_found_config && detect_init_APIC()) {
1227 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1228 apic_phys = __pa(apic_phys);
1229 } else
1230 apic_phys = mp_lapic_addr;
1231
1232 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1233 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1234 apic_phys);
1235
1236 /*
1237 * Fetch the APIC ID of the BSP in case we have a
1238 * default configuration (or the MP table is broken).
1239 */
1240 if (boot_cpu_physical_apicid == -1U)
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001241 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243}
1244
1245/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001246 * This initializes the IO-APIC and APIC hardware if this is
1247 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 */
Alexey Starikovskiye81b2c62008-03-27 23:54:31 +03001249
1250int apic_version[MAX_APICS];
1251
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01001252int __init APIC_init_uniprocessor(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Yinghai Lu914bebf2008-06-29 00:06:37 -07001254 if (disable_apic)
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001255 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001256
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001257 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001258 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001261 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001263 if (!cpu_has_apic &&
1264 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001266 boot_cpu_physical_apicid);
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001267 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return -1;
1269 }
1270
1271 verify_local_APIC();
1272
1273 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001274
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001275 /*
1276 * Hack: In case of kdump, after a crash, kernel might be booting
1277 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1278 * might be zero if read from MP tables. Get it from LAPIC.
1279 */
1280#ifdef CONFIG_CRASH_DUMP
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001281 boot_cpu_physical_apicid = read_apic_id();
Vivek Goyalbe0d03f2006-05-20 15:00:21 -07001282#endif
Jack Steinerb6df1b82008-06-19 21:51:05 -05001283 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 setup_local_APIC();
1286
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001287#ifdef CONFIG_X86_IO_APIC
1288 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1289#endif
1290 localise_nmi_watchdog();
Glauber de Oliveira Costaac60aae2008-03-19 14:25:49 -03001291 end_local_APIC_setup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001293 if (smp_found_config)
1294 if (!skip_ioapic_setup && nr_ioapics)
1295 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296#endif
Zachary Amsdenbbab4f32007-02-13 13:26:21 +01001297 setup_boot_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001298
1299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
Rusty Russell1a3f2392006-09-26 10:52:32 +02001301
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001302/*
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001303 * Local APIC interrupts
1304 */
1305
1306/*
1307 * This interrupt should _never_ happen with our APIC/SMP architecture
1308 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001309void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001310{
1311 unsigned long v;
1312
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001313 irq_enter();
1314 /*
1315 * Check if this really is a spurious interrupt and ACK it
1316 * if it is a vectored one. Just in case...
1317 * Spurious interrupts should not be ACKed.
1318 */
1319 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1320 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1321 ack_APIC_irq();
1322
1323 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1324 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1325 "should never happen.\n", smp_processor_id());
Joe Korty38e760a2007-10-17 18:04:40 +02001326 __get_cpu_var(irq_stat).irq_spurious_count++;
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001327 irq_exit();
1328}
1329
1330/*
1331 * This interrupt should never happen with our APIC/SMP architecture
1332 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -08001333void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001334{
1335 unsigned long v, v1;
1336
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001337 irq_enter();
1338 /* First tickle the hardware, only then report what went on. -- REW */
1339 v = apic_read(APIC_ESR);
1340 apic_write(APIC_ESR, 0);
1341 v1 = apic_read(APIC_ESR);
1342 ack_APIC_irq();
1343 atomic_inc(&irq_err_count);
1344
1345 /* Here is what the APIC error bits mean:
1346 0: Send CS error
1347 1: Receive CS error
1348 2: Send accept error
1349 3: Receive accept error
1350 4: Reserved
1351 5: Send illegal vector
1352 6: Received illegal vector
1353 7: Illegal register address
1354 */
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001355 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001356 smp_processor_id(), v , v1);
1357 irq_exit();
1358}
1359
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001360#ifdef CONFIG_SMP
1361void __init smp_intr_init(void)
1362{
1363 /*
1364 * IRQ0 must be given a fixed assignment and initialized,
1365 * because it's used before the IO-APIC is set up.
1366 */
1367 set_intr_gate(FIRST_DEVICE_VECTOR, interrupt[0]);
1368
1369 /*
1370 * The reschedule interrupt is a CPU-to-CPU reschedule-helper
1371 * IPI, driven by wakeup.
1372 */
Alan Mayer305b92a2008-04-15 15:36:56 -05001373 alloc_intr_gate(RESCHEDULE_VECTOR, reschedule_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001374
1375 /* IPI for invalidation */
Alan Mayer305b92a2008-04-15 15:36:56 -05001376 alloc_intr_gate(INVALIDATE_TLB_VECTOR, invalidate_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001377
1378 /* IPI for generic function call */
Alan Mayer305b92a2008-04-15 15:36:56 -05001379 alloc_intr_gate(CALL_FUNCTION_VECTOR, call_function_interrupt);
Jens Axboe3b16cf82008-06-26 11:21:54 +02001380
1381 /* IPI for single call function */
1382 set_intr_gate(CALL_FUNCTION_SINGLE_VECTOR,
1383 call_function_single_interrupt);
Glauber de Oliveira Costa17c9ab12008-03-19 14:25:33 -03001384}
1385#endif
1386
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001387/*
1388 * Initialize APIC interrupts
1389 */
1390void __init apic_intr_init(void)
1391{
1392#ifdef CONFIG_SMP
1393 smp_intr_init();
1394#endif
1395 /* self generated IPI for local APIC timer */
Alan Mayer305b92a2008-04-15 15:36:56 -05001396 alloc_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001397
1398 /* IPI vectors for APIC spurious and error interrupts */
Alan Mayer305b92a2008-04-15 15:36:56 -05001399 alloc_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1400 alloc_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001401
1402 /* thermal monitor LVT interrupt */
1403#ifdef CONFIG_X86_MCE_P4THERMAL
Alan Mayer305b92a2008-04-15 15:36:56 -05001404 alloc_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001405#endif
1406}
1407
1408/**
1409 * connect_bsp_APIC - attach the APIC to the interrupt system
1410 */
1411void __init connect_bsp_APIC(void)
1412{
1413 if (pic_mode) {
1414 /*
1415 * Do not trust the local APIC being empty at bootup.
1416 */
1417 clear_local_APIC();
1418 /*
1419 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1420 * local APIC to INT and NMI lines.
1421 */
1422 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1423 "enabling APIC mode.\n");
1424 outb(0x70, 0x22);
1425 outb(0x01, 0x23);
1426 }
1427 enable_apic_mode();
1428}
1429
1430/**
1431 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1432 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1433 *
1434 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1435 * APIC is disabled.
1436 */
1437void disconnect_bsp_APIC(int virt_wire_setup)
1438{
1439 if (pic_mode) {
1440 /*
1441 * Put the board back into PIC mode (has an effect only on
1442 * certain older boards). Note that APIC interrupts, including
1443 * IPIs, won't work beyond this point! The only exception are
1444 * INIT IPIs.
1445 */
1446 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1447 "entering PIC mode.\n");
1448 outb(0x70, 0x22);
1449 outb(0x00, 0x23);
1450 } else {
1451 /* Go back to Virtual Wire compatibility mode */
1452 unsigned long value;
1453
1454 /* For the spurious interrupt use vector F, and enable it */
1455 value = apic_read(APIC_SPIV);
1456 value &= ~APIC_VECTOR_MASK;
1457 value |= APIC_SPIV_APIC_ENABLED;
1458 value |= 0xf;
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001459 apic_write(APIC_SPIV, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001460
1461 if (!virt_wire_setup) {
1462 /*
1463 * For LVT0 make it edge triggered, active high,
1464 * external and enabled
1465 */
1466 value = apic_read(APIC_LVT0);
1467 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1468 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
Hiroshi Shimamotoff8a03a2008-01-30 13:32:36 +01001469 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001470 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1471 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001472 apic_write(APIC_LVT0, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001473 } else {
1474 /* Disable LVT0 */
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001475 apic_write(APIC_LVT0, APIC_LVT_MASKED);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001476 }
1477
1478 /*
1479 * For LVT1 make it edge triggered, active high, nmi and
1480 * enabled
1481 */
1482 value = apic_read(APIC_LVT1);
1483 value &= ~(
1484 APIC_MODE_MASK | APIC_SEND_PENDING |
1485 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1486 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1487 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1488 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
Maciej W. Rozycki593f4a72008-07-16 19:15:30 +01001489 apic_write(APIC_LVT1, value);
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001490 }
1491}
1492
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001493unsigned int __cpuinitdata maxcpus = NR_CPUS;
1494
1495void __cpuinit generic_processor_info(int apicid, int version)
1496{
1497 int cpu;
1498 cpumask_t tmp_map;
1499 physid_mask_t phys_cpu;
1500
1501 /*
1502 * Validate version
1503 */
1504 if (version == 0x0) {
1505 printk(KERN_WARNING "BIOS bug, APIC version is 0 for CPU#%d! "
1506 "fixing up to 0x10. (tell your hw vendor)\n",
1507 version);
1508 version = 0x10;
1509 }
1510 apic_version[apicid] = version;
1511
1512 phys_cpu = apicid_to_cpu_present(apicid);
1513 physids_or(phys_cpu_present_map, phys_cpu_present_map, phys_cpu);
1514
1515 if (num_processors >= NR_CPUS) {
1516 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1517 " Processor ignored.\n", NR_CPUS);
1518 return;
1519 }
1520
1521 if (num_processors >= maxcpus) {
1522 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1523 " Processor ignored.\n", maxcpus);
1524 return;
1525 }
1526
1527 num_processors++;
1528 cpus_complement(tmp_map, cpu_present_map);
1529 cpu = first_cpu(tmp_map);
1530
1531 if (apicid == boot_cpu_physical_apicid)
1532 /*
1533 * x86_bios_cpu_apicid is required to have processors listed
1534 * in same order as logical cpu numbers. Hence the first
1535 * entry is BSP, and so on.
1536 */
1537 cpu = 0;
1538
Yinghai Lue0da3362008-06-08 18:29:22 -07001539 if (apicid > max_physical_apicid)
1540 max_physical_apicid = apicid;
1541
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001542 /*
1543 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1544 * but we need to work other dependencies like SMP_SUSPEND etc
1545 * before this can be done without some confusion.
1546 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1547 * - Ashok Raj <ashok.raj@intel.com>
1548 */
Yinghai Lue0da3362008-06-08 18:29:22 -07001549 if (max_physical_apicid >= 8) {
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001550 switch (boot_cpu_data.x86_vendor) {
1551 case X86_VENDOR_INTEL:
1552 if (!APIC_XAPIC(version)) {
1553 def_to_bigsmp = 0;
1554 break;
1555 }
1556 /* If P4 and above fall through */
1557 case X86_VENDOR_AMD:
1558 def_to_bigsmp = 1;
1559 }
1560 }
1561#ifdef CONFIG_SMP
1562 /* are we being called early in kernel startup? */
Mike Travis23ca4bb2008-05-12 21:21:12 +02001563 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1564 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1565 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Alexey Starikovskiy903dcb52008-03-27 23:55:22 +03001566
1567 cpu_to_apicid[cpu] = apicid;
1568 bios_cpu_apicid[cpu] = apicid;
1569 } else {
1570 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1571 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1572 }
1573#endif
1574 cpu_set(cpu, cpu_possible_map);
1575 cpu_set(cpu, cpu_present_map);
1576}
1577
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001578/*
1579 * Power management
1580 */
1581#ifdef CONFIG_PM
1582
1583static struct {
1584 int active;
1585 /* r/w apic fields */
1586 unsigned int apic_id;
1587 unsigned int apic_taskpri;
1588 unsigned int apic_ldr;
1589 unsigned int apic_dfr;
1590 unsigned int apic_spiv;
1591 unsigned int apic_lvtt;
1592 unsigned int apic_lvtpc;
1593 unsigned int apic_lvt0;
1594 unsigned int apic_lvt1;
1595 unsigned int apic_lvterr;
1596 unsigned int apic_tmict;
1597 unsigned int apic_tdcr;
1598 unsigned int apic_thmr;
1599} apic_pm_state;
1600
1601static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1602{
1603 unsigned long flags;
1604 int maxlvt;
1605
1606 if (!apic_pm_state.active)
1607 return 0;
1608
1609 maxlvt = lapic_get_maxlvt();
1610
1611 apic_pm_state.apic_id = apic_read(APIC_ID);
1612 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1613 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1614 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1615 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1616 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1617 if (maxlvt >= 4)
1618 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1619 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1620 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1621 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1622 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1623 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1624#ifdef CONFIG_X86_MCE_P4THERMAL
1625 if (maxlvt >= 5)
1626 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1627#endif
1628
1629 local_irq_save(flags);
1630 disable_local_APIC();
1631 local_irq_restore(flags);
1632 return 0;
1633}
1634
1635static int lapic_resume(struct sys_device *dev)
1636{
1637 unsigned int l, h;
1638 unsigned long flags;
1639 int maxlvt;
1640
1641 if (!apic_pm_state.active)
1642 return 0;
1643
1644 maxlvt = lapic_get_maxlvt();
1645
1646 local_irq_save(flags);
1647
1648 /*
1649 * Make sure the APICBASE points to the right address
1650 *
1651 * FIXME! This will be wrong if we ever support suspend on
1652 * SMP! We'll need to do this as part of the CPU restore!
1653 */
1654 rdmsr(MSR_IA32_APICBASE, l, h);
1655 l &= ~MSR_IA32_APICBASE_BASE;
1656 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1657 wrmsr(MSR_IA32_APICBASE, l, h);
1658
1659 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1660 apic_write(APIC_ID, apic_pm_state.apic_id);
1661 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1662 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1663 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1664 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1665 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1666 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1667#ifdef CONFIG_X86_MCE_P4THERMAL
1668 if (maxlvt >= 5)
1669 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1670#endif
1671 if (maxlvt >= 4)
1672 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1673 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1674 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1675 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1676 apic_write(APIC_ESR, 0);
1677 apic_read(APIC_ESR);
1678 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1679 apic_write(APIC_ESR, 0);
1680 apic_read(APIC_ESR);
1681 local_irq_restore(flags);
1682 return 0;
1683}
1684
1685/*
1686 * This device has no shutdown method - fully functioning local APICs
1687 * are needed on every CPU up until machine_halt/restart/poweroff.
1688 */
1689
1690static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01001691 .name = "lapic",
Thomas Gleixnere05d7232007-02-16 01:27:58 -08001692 .resume = lapic_resume,
1693 .suspend = lapic_suspend,
1694};
1695
1696static struct sys_device device_lapic = {
1697 .id = 0,
1698 .cls = &lapic_sysclass,
1699};
1700
1701static void __devinit apic_pm_activate(void)
1702{
1703 apic_pm_state.active = 1;
1704}
1705
1706static int __init init_lapic_sysfs(void)
1707{
1708 int error;
1709
1710 if (!cpu_has_apic)
1711 return 0;
1712 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1713
1714 error = sysdev_class_register(&lapic_sysclass);
1715 if (!error)
1716 error = sysdev_register(&device_lapic);
1717 return error;
1718}
1719device_initcall(init_lapic_sysfs);
1720
1721#else /* CONFIG_PM */
1722
1723static void apic_pm_activate(void) { }
1724
1725#endif /* CONFIG_PM */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001726
1727/*
1728 * APIC command line parameters
1729 */
1730static int __init parse_lapic(char *arg)
1731{
Yinghai Lu914bebf2008-06-29 00:06:37 -07001732 force_enable_local_apic = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001733 return 0;
1734}
1735early_param("lapic", parse_lapic);
1736
1737static int __init parse_nolapic(char *arg)
1738{
Yinghai Lu914bebf2008-06-29 00:06:37 -07001739 disable_apic = 1;
Jeremy Fitzhardinge53756d32008-01-30 13:30:55 +01001740 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001741 return 0;
1742}
1743early_param("nolapic", parse_nolapic);
1744
1745static int __init parse_disable_lapic_timer(char *arg)
1746{
1747 local_apic_timer_disabled = 1;
1748 return 0;
1749}
1750early_param("nolapic_timer", parse_disable_lapic_timer);
1751
1752static int __init parse_lapic_timer_c2_ok(char *arg)
1753{
1754 local_apic_timer_c2_ok = 1;
1755 return 0;
1756}
1757early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1758
1759static int __init apic_set_verbosity(char *str)
1760{
1761 if (strcmp("debug", str) == 0)
1762 apic_verbosity = APIC_DEBUG;
1763 else if (strcmp("verbose", str) == 0)
1764 apic_verbosity = APIC_VERBOSE;
1765 return 1;
1766}
1767__setup("apic=", apic_set_verbosity);
1768
Cyrill Gorcunov746f2eb2008-07-01 21:43:52 +04001769static int __init lapic_insert_resource(void)
1770{
1771 if (!apic_phys)
1772 return -1;
1773
1774 /* Put local APIC into the resource map. */
1775 lapic_resource.start = apic_phys;
1776 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1777 insert_resource(&iomem_resource, &lapic_resource);
1778
1779 return 0;
1780}
1781
1782/*
1783 * need call insert after e820_reserve_resources()
1784 * that is using request_resource
1785 */
1786late_initcall(lapic_insert_resource);