blob: b8616aaa168d367f3fe30c7b94ea665ba1bf344c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * (c) 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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/kernel_stat.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010018#include <linux/mc146818rtc.h>
Thomas Gleixner70a20022008-01-30 13:30:18 +010019#include <linux/acpi_pmtmr.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010020#include <linux/clockchips.h>
21#include <linux/interrupt.h>
22#include <linux/bootmem.h>
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +010023#include <linux/ftrace.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010024#include <linux/ioport.h>
25#include <linux/module.h>
26#include <linux/sysdev.h>
27#include <linux/delay.h>
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +053028#include <linux/timex.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010029#include <linux/dmar.h>
30#include <linux/init.h>
31#include <linux/cpu.h>
32#include <linux/dmi.h>
33#include <linux/nmi.h>
34#include <linux/smp.h>
35#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Yinghai Lu773763d2008-08-24 02:01:52 -070037#include <asm/arch_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/pgalloc.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010039#include <asm/genapic.h>
40#include <asm/atomic.h>
41#include <asm/mpspec.h>
Yinghai Lu773763d2008-08-24 02:01:52 -070042#include <asm/i8253.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010043#include <asm/i8259.h>
Andi Kleen73dea472006-02-03 21:50:50 +010044#include <asm/proto.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020045#include <asm/apic.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010046#include <asm/desc.h>
47#include <asm/hpet.h>
48#include <asm/idle.h>
49#include <asm/mtrr.h>
Jaswinder Singh Rajput2bc13792009-01-11 20:34:47 +053050#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Brian Gerstec70de82009-01-27 12:56:47 +090052unsigned int num_processors;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010053
Brian Gerstec70de82009-01-27 12:56:47 +090054unsigned disabled_cpus __cpuinitdata;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010055
Brian Gerstec70de82009-01-27 12:56:47 +090056/* Processor that is doing the boot up */
57unsigned int boot_cpu_physical_apicid = -1U;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010058
59/*
60 * The highest APIC ID seen during enumeration.
61 *
62 * This determines the messaging protocol we can use: if all APIC IDs
63 * are in the 0 ... 7 range, then we can use logical addressing which
64 * has some performance advantages (better broadcasting).
65 *
66 * If there's an APIC ID above 8, we use physical addressing.
67 */
Brian Gerstec70de82009-01-27 12:56:47 +090068unsigned int max_physical_apicid;
69
Ingo Molnarfdbecd92009-01-31 03:57:12 +010070/*
71 * Bitmask of physically existing CPUs:
72 */
Brian Gerstec70de82009-01-27 12:56:47 +090073physid_mask_t phys_cpu_present_map;
74
75/*
76 * Map cpu index to physical APIC ID
77 */
78DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
79DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
80EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
81EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
82
Yinghai Lub3c51172008-08-24 02:01:46 -070083#ifdef CONFIG_X86_32
84/*
85 * Knob to control our willingness to enable the local APIC.
86 *
87 * +1=force-enable
88 */
89static int force_enable_local_apic;
90/*
91 * APIC command line parameters
92 */
93static int __init parse_lapic(char *arg)
94{
95 force_enable_local_apic = 1;
96 return 0;
97}
98early_param("lapic", parse_lapic);
Yinghai Luf28c0ae2008-08-24 02:01:49 -070099/* Local APIC was disabled by the BIOS and enabled by the kernel */
100static int enabled_via_apicbase;
101
Yinghai Lub3c51172008-08-24 02:01:46 -0700102#endif
103
104#ifdef CONFIG_X86_64
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200105static int apic_calibrate_pmtmr __initdata;
Yinghai Lub3c51172008-08-24 02:01:46 -0700106static __init int setup_apicpmtimer(char *s)
107{
108 apic_calibrate_pmtmr = 1;
109 notsc_setup(NULL);
110 return 0;
111}
112__setup("apicpmtimer", setup_apicpmtimer);
113#endif
114
Yinghai Lu49899ea2008-08-24 02:01:47 -0700115#ifdef CONFIG_X86_64
116#define HAVE_X2APIC
117#endif
118
119#ifdef HAVE_X2APIC
Suresh Siddha89027d32008-07-10 11:16:56 -0700120int x2apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -0700121/* x2apic enabled before OS handover */
Jaswinder Singhb6b301a2008-12-23 21:52:33 +0530122static int x2apic_preenabled;
123static int disable_x2apic;
Yinghai Lu49899ea2008-08-24 02:01:47 -0700124static __init int setup_nox2apic(char *str)
125{
126 disable_x2apic = 1;
127 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
128 return 0;
129}
130early_param("nox2apic", setup_nox2apic);
131#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Yinghai Lub3c51172008-08-24 02:01:46 -0700133unsigned long mp_lapic_addr;
134int disable_apic;
135/* Disable local APIC timer from the kernel commandline or via dmi quirk */
136static int disable_apic_timer __cpuinitdata;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100137/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -0700138int local_apic_timer_c2_ok;
139EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
140
Yinghai Luefa25592008-08-19 20:50:36 -0700141int first_system_vector = 0xfe;
142
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100143/*
144 * Debug level, exported for io_apic.c
145 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +0100146unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100147
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -0700148int pic_mode;
149
Alexey Starikovskiybab4b272008-05-19 19:47:03 +0400150/* Have we found an MP table */
151int smp_found_config;
152
Aaron Durbin39928722006-12-07 02:14:01 +0100153static struct resource lapic_resource = {
154 .name = "Local APIC",
155 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
156};
157
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200158static unsigned int calibration_result;
159
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200160static int lapic_next_event(unsigned long delta,
161 struct clock_event_device *evt);
162static void lapic_timer_setup(enum clock_event_mode mode,
163 struct clock_event_device *evt);
Mike Travis96289372008-12-31 18:08:46 -0800164static void lapic_timer_broadcast(const struct cpumask *mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100165static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200166
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400167/*
168 * The local apic timer can be used for any function which is CPU local.
169 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200170static struct clock_event_device lapic_clockevent = {
171 .name = "lapic",
172 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
173 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
174 .shift = 32,
175 .set_mode = lapic_timer_setup,
176 .set_next_event = lapic_next_event,
177 .broadcast = lapic_timer_broadcast,
178 .rating = 100,
179 .irq = -1,
180};
181static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
182
Andi Kleend3432892008-01-30 13:33:17 +0100183static unsigned long apic_phys;
184
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100185/*
186 * Get the LAPIC version
187 */
188static inline int lapic_get_version(void)
189{
190 return GET_APIC_VERSION(apic_read(APIC_LVR));
191}
192
193/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400194 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100195 */
196static inline int lapic_is_integrated(void)
197{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400198#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100199 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400200#else
201 return APIC_INTEGRATED(lapic_get_version());
202#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100203}
204
205/*
206 * Check, whether this is a modern or a first generation APIC
207 */
208static int modern_apic(void)
209{
210 /* AMD systems use old APIC versions, so check the CPU */
211 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
212 boot_cpu_data.x86 >= 0xf)
213 return 1;
214 return lapic_get_version() >= 0x14;
215}
216
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400217/*
218 * Paravirt kernels also might be using these below ops. So we still
219 * use generic apic_read()/apic_write(), which might be pointing to different
220 * ops in PARAVIRT case.
221 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700222void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100223{
224 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
225 cpu_relax();
226}
227
Suresh Siddha1b374e42008-07-10 11:16:49 -0700228u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100229{
230 u32 send_status;
231 int timeout;
232
233 timeout = 0;
234 do {
235 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
236 if (!send_status)
237 break;
238 udelay(100);
239 } while (timeout++ < 1000);
240
241 return send_status;
242}
243
Suresh Siddha1b374e42008-07-10 11:16:49 -0700244void xapic_icr_write(u32 low, u32 id)
245{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200246 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700247 apic_write(APIC_ICR, low);
248}
249
Jaswinder Singh Rajputec8c8422008-12-30 22:46:36 +0530250static u64 xapic_icr_read(void)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700251{
252 u32 icr1, icr2;
253
254 icr2 = apic_read(APIC_ICR2);
255 icr1 = apic_read(APIC_ICR);
256
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400257 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700258}
259
260static struct apic_ops xapic_ops = {
261 .read = native_apic_mem_read,
262 .write = native_apic_mem_write,
Suresh Siddha1b374e42008-07-10 11:16:49 -0700263 .icr_read = xapic_icr_read,
264 .icr_write = xapic_icr_write,
265 .wait_icr_idle = xapic_wait_icr_idle,
266 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
267};
268
269struct apic_ops __read_mostly *apic_ops = &xapic_ops;
Suresh Siddha1b374e42008-07-10 11:16:49 -0700270EXPORT_SYMBOL_GPL(apic_ops);
271
Yinghai Lu49899ea2008-08-24 02:01:47 -0700272#ifdef HAVE_X2APIC
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700273static void x2apic_wait_icr_idle(void)
274{
275 /* no need to wait for icr idle in x2apic */
276 return;
277}
278
279static u32 safe_x2apic_wait_icr_idle(void)
280{
281 /* no need to wait for icr idle in x2apic */
282 return 0;
283}
284
285void x2apic_icr_write(u32 low, u32 id)
286{
287 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
288}
289
Jaswinder Singh Rajputec8c8422008-12-30 22:46:36 +0530290static u64 x2apic_icr_read(void)
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700291{
292 unsigned long val;
293
294 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
295 return val;
296}
297
298static struct apic_ops x2apic_ops = {
299 .read = native_apic_msr_read,
300 .write = native_apic_msr_write,
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700301 .icr_read = x2apic_icr_read,
302 .icr_write = x2apic_icr_write,
303 .wait_icr_idle = x2apic_wait_icr_idle,
304 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
305};
Yinghai Lu49899ea2008-08-24 02:01:47 -0700306#endif
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700307
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100308/**
309 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
310 */
Jan Beuliche9427102008-01-30 13:31:24 +0100311void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100312{
313 unsigned int v;
314
315 /* unmask and set to NMI */
316 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200317
318 /* Level triggered for 82489DX (32bit mode) */
319 if (!lapic_is_integrated())
320 v |= APIC_LVT_LEVEL_TRIGGER;
321
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100322 apic_write(APIC_LVT0, v);
323}
324
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700325#ifdef CONFIG_X86_32
326/**
327 * get_physical_broadcast - Get number of physical broadcast IDs
328 */
329int get_physical_broadcast(void)
330{
331 return modern_apic() ? 0xff : 0xf;
332}
333#endif
334
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100335/**
336 * lapic_get_maxlvt - get the maximum number of local vector table entries
337 */
338int lapic_get_maxlvt(void)
339{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200340 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100341
342 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200343 /*
344 * - we always have APIC integrated on 64bit mode
345 * - 82489DXs do not report # of LVT entries
346 */
347 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100348}
349
350/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400351 * Local APIC timer
352 */
353
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400354/* Clock divisor */
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400355#define APIC_DIVISOR 16
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200356
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100357/*
358 * This function sets up the local APIC timer, with a timeout of
359 * 'clocks' APIC bus clock. During calibration we actually call
360 * this function twice on the boot CPU, once with a bogus timeout
361 * value, second time for real. The other (noncalibrating) CPUs
362 * call this function only once, with the real, calibrated value.
363 *
364 * We do reads before writes even if unnecessary, to get around the
365 * P5 APIC double write bug.
366 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100367static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
368{
369 unsigned int lvtt_value, tmp_value;
370
371 lvtt_value = LOCAL_TIMER_VECTOR;
372 if (!oneshot)
373 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200374 if (!lapic_is_integrated())
375 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
376
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100377 if (!irqen)
378 lvtt_value |= APIC_LVT_MASKED;
379
380 apic_write(APIC_LVTT, lvtt_value);
381
382 /*
383 * Divide PICLK by 16
384 */
385 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400386 apic_write(APIC_TDCR,
387 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
388 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100389
390 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200391 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100392}
393
394/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100395 * Setup extended LVT, AMD specific (K8, family 10h)
396 *
397 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
398 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Robert Richter286f5712008-07-22 21:08:46 +0200399 *
400 * If mask=1, the LVT entry does not generate interrupts while mask=0
401 * enables the vector. See also the BKDGs.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100402 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100403
404#define APIC_EILVT_LVTOFF_MCE 0
405#define APIC_EILVT_LVTOFF_IBS 1
406
407static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100408{
Robert Richter7b83dae2008-01-30 13:30:40 +0100409 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100410 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
411
412 apic_write(reg, v);
413}
414
Robert Richter7b83dae2008-01-30 13:30:40 +0100415u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
416{
417 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
418 return APIC_EILVT_LVTOFF_MCE;
419}
420
421u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
422{
423 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
424 return APIC_EILVT_LVTOFF_IBS;
425}
Robert Richter6aa360e2008-07-23 15:28:14 +0200426EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100427
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100428/*
429 * Program the next event, relative to now
430 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200431static int lapic_next_event(unsigned long delta,
432 struct clock_event_device *evt)
433{
434 apic_write(APIC_TMICT, delta);
435 return 0;
436}
437
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100438/*
439 * Setup the lapic timer in periodic or oneshot mode
440 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200441static void lapic_timer_setup(enum clock_event_mode mode,
442 struct clock_event_device *evt)
443{
444 unsigned long flags;
445 unsigned int v;
446
447 /* Lapic used as dummy for broadcast ? */
448 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
449 return;
450
451 local_irq_save(flags);
452
453 switch (mode) {
454 case CLOCK_EVT_MODE_PERIODIC:
455 case CLOCK_EVT_MODE_ONESHOT:
456 __setup_APIC_LVTT(calibration_result,
457 mode != CLOCK_EVT_MODE_PERIODIC, 1);
458 break;
459 case CLOCK_EVT_MODE_UNUSED:
460 case CLOCK_EVT_MODE_SHUTDOWN:
461 v = apic_read(APIC_LVTT);
462 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
463 apic_write(APIC_LVTT, v);
Thomas Gleixnera98f8fd2008-11-06 01:13:39 +0100464 apic_write(APIC_TMICT, 0xffffffff);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200465 break;
466 case CLOCK_EVT_MODE_RESUME:
467 /* Nothing to do here */
468 break;
469 }
470
471 local_irq_restore(flags);
472}
473
474/*
475 * Local APIC timer broadcast function
476 */
Mike Travis96289372008-12-31 18:08:46 -0800477static void lapic_timer_broadcast(const struct cpumask *mask)
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200478{
479#ifdef CONFIG_SMP
Ingo Molnardac5f412009-01-28 15:42:24 +0100480 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200481#endif
482}
483
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100484/*
485 * Setup the local APIC timer for this CPU. Copy the initilized values
486 * of the boot CPU and register the clock event in the framework.
487 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700488static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200489{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100490 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
491
492 memcpy(levt, &lapic_clockevent, sizeof(*levt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030493 levt->cpumask = cpumask_of(smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100494
495 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200496}
497
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700498/*
499 * In this functions we calibrate APIC bus clocks to the external timer.
500 *
501 * We want to do the calibration only once since we want to have local timer
502 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
503 * frequency.
504 *
505 * This was previously done by reading the PIT/HPET and waiting for a wrap
506 * around to find out, that a tick has elapsed. I have a box, where the PIT
507 * readout is broken, so it never gets out of the wait loop again. This was
508 * also reported by others.
509 *
510 * Monitoring the jiffies value is inaccurate and the clockevents
511 * infrastructure allows us to do a simple substitution of the interrupt
512 * handler.
513 *
514 * The calibration routine also uses the pm_timer when possible, as the PIT
515 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
516 * back to normal later in the boot process).
517 */
518
519#define LAPIC_CAL_LOOPS (HZ/10)
520
521static __initdata int lapic_cal_loops = -1;
522static __initdata long lapic_cal_t1, lapic_cal_t2;
523static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
524static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
525static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
526
527/*
528 * Temporary interrupt handler.
529 */
530static void __init lapic_cal_handler(struct clock_event_device *dev)
531{
532 unsigned long long tsc = 0;
533 long tapic = apic_read(APIC_TMCCT);
534 unsigned long pm = acpi_pm_read_early();
535
536 if (cpu_has_tsc)
537 rdtscll(tsc);
538
539 switch (lapic_cal_loops++) {
540 case 0:
541 lapic_cal_t1 = tapic;
542 lapic_cal_tsc1 = tsc;
543 lapic_cal_pm1 = pm;
544 lapic_cal_j1 = jiffies;
545 break;
546
547 case LAPIC_CAL_LOOPS:
548 lapic_cal_t2 = tapic;
549 lapic_cal_tsc2 = tsc;
550 if (pm < lapic_cal_pm1)
551 pm += ACPI_PM_OVRRUN;
552 lapic_cal_pm2 = pm;
553 lapic_cal_j2 = jiffies;
554 break;
555 }
556}
557
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400558static int __init calibrate_by_pmtimer(long deltapm, long *delta)
559{
560 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
561 const long pm_thresh = pm_100ms / 100;
562 unsigned long mult;
563 u64 res;
564
565#ifndef CONFIG_X86_PM_TIMER
566 return -1;
567#endif
568
569 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
570
571 /* Check, if the PM timer is available */
572 if (!deltapm)
573 return -1;
574
575 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
576
577 if (deltapm > (pm_100ms - pm_thresh) &&
578 deltapm < (pm_100ms + pm_thresh)) {
579 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
580 } else {
581 res = (((u64)deltapm) * mult) >> 22;
582 do_div(res, 1000000);
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100583 pr_warning("APIC calibration not consistent "
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400584 "with PM Timer: %ldms instead of 100ms\n",
585 (long)res);
586 /* Correct the lapic counter value */
587 res = (((u64)(*delta)) * pm_100ms);
588 do_div(res, deltapm);
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100589 pr_info("APIC delta adjusted to PM-Timer: "
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400590 "%lu (%ld)\n", (unsigned long)res, *delta);
591 *delta = (long)res;
592 }
593
594 return 0;
595}
596
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700597static int __init calibrate_APIC_clock(void)
598{
599 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700600 void (*real_handler)(struct clock_event_device *dev);
601 unsigned long deltaj;
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400602 long delta;
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700603 int pm_referenced = 0;
604
605 local_irq_disable();
606
607 /* Replace the global interrupt handler */
608 real_handler = global_clock_event->event_handler;
609 global_clock_event->event_handler = lapic_cal_handler;
610
611 /*
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400612 * Setup the APIC counter to maximum. There is no way the lapic
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700613 * can underflow in the 100ms detection time frame
614 */
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400615 __setup_APIC_LVTT(0xffffffff, 0, 0);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700616
617 /* Let the interrupts run */
618 local_irq_enable();
619
620 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
621 cpu_relax();
622
623 local_irq_disable();
624
625 /* Restore the real event handler */
626 global_clock_event->event_handler = real_handler;
627
628 /* Build delta t1-t2 as apic timer counts down */
629 delta = lapic_cal_t1 - lapic_cal_t2;
630 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
631
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400632 /* we trust the PM based calibration if possible */
633 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
634 &delta);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700635
636 /* Calculate the scaled math multiplication factor */
637 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
638 lapic_clockevent.shift);
639 lapic_clockevent.max_delta_ns =
640 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
641 lapic_clockevent.min_delta_ns =
642 clockevent_delta2ns(0xF, &lapic_clockevent);
643
644 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
645
646 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
647 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
648 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
649 calibration_result);
650
651 if (cpu_has_tsc) {
652 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
653 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
654 "%ld.%04ld MHz.\n",
655 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
656 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
657 }
658
659 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
660 "%u.%04u MHz.\n",
661 calibration_result / (1000000 / HZ),
662 calibration_result % (1000000 / HZ));
663
664 /*
665 * Do a sanity check on the APIC calibration result
666 */
667 if (calibration_result < (1000000 / HZ)) {
668 local_irq_enable();
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100669 pr_warning("APIC frequency too slow, disabling apic timer\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700670 return -1;
671 }
672
673 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
674
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400675 /*
676 * PM timer calibration failed or not turned on
677 * so lets try APIC timer based calibration
678 */
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700679 if (!pm_referenced) {
680 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
681
682 /*
683 * Setup the apic timer manually
684 */
685 levt->event_handler = lapic_cal_handler;
686 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
687 lapic_cal_loops = -1;
688
689 /* Let the interrupts run */
690 local_irq_enable();
691
692 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
693 cpu_relax();
694
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700695 /* Stop the lapic timer */
696 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
697
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700698 /* Jiffies delta */
699 deltaj = lapic_cal_j2 - lapic_cal_j1;
700 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
701
702 /* Check, if the jiffies result is consistent */
703 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
704 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
705 else
706 levt->features |= CLOCK_EVT_FEAT_DUMMY;
707 } else
708 local_irq_enable();
709
710 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +0530711 pr_warning("APIC timer disabled due to verification failure\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700712 return -1;
713 }
714
715 return 0;
716}
717
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100718/*
719 * Setup the boot APIC
720 *
721 * Calibrate and verify the result.
722 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100723void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100725 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400726 * The local apic timer can be disabled via the kernel
727 * commandline or from the CPU detection code. Register the lapic
728 * timer as a dummy clock event source on SMP systems, so the
729 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100730 */
731 if (disable_apic_timer) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100732 pr_info("Disabling APIC timer\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100733 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100734 if (num_possible_cpus() > 1) {
735 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100736 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100737 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100738 return;
739 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200740
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400741 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
742 "calibrating APIC timer ...\n");
743
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400744 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100745 /* No broadcast on UP ! */
746 if (num_possible_cpus() > 1)
747 setup_APIC_timer();
748 return;
749 }
750
751 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100752 * If nmi_watchdog is set to IO_APIC, we need the
753 * PIT/HPET going. Otherwise register lapic as a dummy
754 * device.
755 */
756 if (nmi_watchdog != NMI_IO_APIC)
757 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
758 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100759 pr_warning("APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200760 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100761
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400762 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100763 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100766void __cpuinit setup_secondary_APIC_clock(void)
767{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100768 setup_APIC_timer();
769}
770
771/*
772 * The guts of the apic timer interrupt
773 */
774static void local_apic_timer_interrupt(void)
775{
776 int cpu = smp_processor_id();
777 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
778
779 /*
780 * Normally we should not be here till LAPIC has been initialized but
781 * in some cases like kdump, its possible that there is a pending LAPIC
782 * timer interrupt from previous kernel's context and is delivered in
783 * new kernel the moment interrupts are enabled.
784 *
785 * Interrupts are enabled early and LAPIC is setup much later, hence
786 * its possible that when we get here evt->event_handler is NULL.
787 * Check for event_handler being NULL and discard the interrupt as
788 * spurious.
789 */
790 if (!evt->event_handler) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100791 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100792 /* Switch it off */
793 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
794 return;
795 }
796
797 /*
798 * the NMI deadlock-detector uses this.
799 */
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800800 inc_irq_stat(apic_timer_irqs);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100801
802 evt->event_handler(evt);
803}
804
805/*
806 * Local APIC timer interrupt. This is the most natural way for doing
807 * local interrupts, but local timer interrupts can be emulated by
808 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
809 *
810 * [ if a single-CPU system runs an SMP kernel then we call the local
811 * interrupt as well. Thus we cannot inline the local irq ... ]
812 */
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +0100813void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100814{
815 struct pt_regs *old_regs = set_irq_regs(regs);
816
817 /*
818 * NOTE! We'd better ACK the irq immediately,
819 * because timer handling can be slow.
820 */
821 ack_APIC_irq();
822 /*
823 * update_process_times() expects us to have done irq_enter().
824 * Besides, if we don't timer interrupts ignore the global
825 * interrupt lock, which is the WrongThing (tm) to do.
826 */
827 exit_idle();
828 irq_enter();
829 local_apic_timer_interrupt();
830 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400831
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100832 set_irq_regs(old_regs);
833}
834
835int setup_profiling_timer(unsigned int multiplier)
836{
837 return -EINVAL;
838}
839
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100840/*
841 * Local APIC start and shutdown
842 */
843
844/**
845 * clear_local_APIC - shutdown the local APIC
846 *
847 * This is called, when a CPU is disabled and before rebooting, so the state of
848 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
849 * leftovers during boot.
850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851void clear_local_APIC(void)
852{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400853 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100854 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
Andi Kleend3432892008-01-30 13:33:17 +0100856 /* APIC hasn't been mapped yet */
857 if (!apic_phys)
858 return;
859
860 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200862 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 * if the vector is zero. Mask LVTERR first to prevent this.
864 */
865 if (maxlvt >= 3) {
866 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100867 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
869 /*
870 * Careful: we have to set masks only first to deassert
871 * any level-triggered sources.
872 */
873 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100874 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100876 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100878 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (maxlvt >= 4) {
880 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100881 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400884 /* lets not touch this if we didn't frob it */
885#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
886 if (maxlvt >= 5) {
887 v = apic_read(APIC_LVTTHMR);
888 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
889 }
890#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 /*
892 * Clean APIC state for other OSs:
893 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100894 apic_write(APIC_LVTT, APIC_LVT_MASKED);
895 apic_write(APIC_LVT0, APIC_LVT_MASKED);
896 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100898 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100900 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400901
902 /* Integrated APIC (!82489DX) ? */
903 if (lapic_is_integrated()) {
904 if (maxlvt > 3)
905 /* Clear ESR due to Pentium errata 3AP and 11AP */
906 apic_write(APIC_ESR, 0);
907 apic_read(APIC_ESR);
908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100911/**
912 * disable_local_APIC - clear and disable the local APIC
913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914void disable_local_APIC(void)
915{
916 unsigned int value;
917
Jan Beulicha08c4742009-01-14 12:28:51 +0000918 /* APIC hasn't been mapped yet */
919 if (!apic_phys)
920 return;
921
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 clear_local_APIC();
923
924 /*
925 * Disable APIC (implies clearing of registers
926 * for 82489DX!).
927 */
928 value = apic_read(APIC_SPIV);
929 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100930 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400931
932#ifdef CONFIG_X86_32
933 /*
934 * When LAPIC was disabled by the BIOS and enabled by the kernel,
935 * restore the disabled state.
936 */
937 if (enabled_via_apicbase) {
938 unsigned int l, h;
939
940 rdmsr(MSR_IA32_APICBASE, l, h);
941 l &= ~MSR_IA32_APICBASE_ENABLE;
942 wrmsr(MSR_IA32_APICBASE, l, h);
943 }
944#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400947/*
948 * If Linux enabled the LAPIC against the BIOS default disable it down before
949 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
950 * not power-off. Additionally clear all LVT entries before disable_local_APIC
951 * for the case where Linux didn't enable the LAPIC.
952 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700953void lapic_shutdown(void)
954{
955 unsigned long flags;
956
957 if (!cpu_has_apic)
958 return;
959
960 local_irq_save(flags);
961
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400962#ifdef CONFIG_X86_32
963 if (!enabled_via_apicbase)
964 clear_local_APIC();
965 else
966#endif
967 disable_local_APIC();
968
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700969
970 local_irq_restore(flags);
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/*
974 * This is to verify that we're looking at a real local APIC.
975 * Check these against your board if the CPUs aren't getting
976 * started for no apparent reason.
977 */
978int __init verify_local_APIC(void)
979{
980 unsigned int reg0, reg1;
981
982 /*
983 * The version register is read-only in a real APIC.
984 */
985 reg0 = apic_read(APIC_LVR);
986 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
987 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
988 reg1 = apic_read(APIC_LVR);
989 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
990
991 /*
992 * The two version reads above should print the same
993 * numbers. If the second one is different, then we
994 * poke at a non-APIC.
995 */
996 if (reg1 != reg0)
997 return 0;
998
999 /*
1000 * Check if the version looks reasonably.
1001 */
1002 reg1 = GET_APIC_VERSION(reg0);
1003 if (reg1 == 0x00 || reg1 == 0xff)
1004 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001005 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (reg1 < 0x02 || reg1 == 0xff)
1007 return 0;
1008
1009 /*
1010 * The ID register is read/write in a real APIC.
1011 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001012 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001014 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001015 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1017 apic_write(APIC_ID, reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001018 if (reg1 != (reg0 ^ apic->apic_id_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 return 0;
1020
1021 /*
1022 * The next two are just to see if we have sane values.
1023 * They're only really relevant if we're in Virtual Wire
1024 * compatibility mode, but most boxes are anymore.
1025 */
1026 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001027 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 reg1 = apic_read(APIC_LVT1);
1029 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1030
1031 return 1;
1032}
1033
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001034/**
1035 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1036 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037void __init sync_Arb_IDs(void)
1038{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +02001039 /*
1040 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1041 * needed on AMD.
1042 */
1043 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 return;
1045
1046 /*
1047 * Wait for idle.
1048 */
1049 apic_wait_icr_idle();
1050
1051 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +04001052 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1053 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054}
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*
1057 * An initial setup of the virtual wire mode.
1058 */
1059void __init init_bsp_APIC(void)
1060{
Andi Kleen11a8e772006-01-11 22:46:51 +01001061 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /*
1064 * Don't do the setup now if we have a SMP BIOS as the
1065 * through-I/O-APIC virtual wire mode might be active.
1066 */
1067 if (smp_found_config || !cpu_has_apic)
1068 return;
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 /*
1071 * Do not trust the local APIC being empty at bootup.
1072 */
1073 clear_local_APIC();
1074
1075 /*
1076 * Enable APIC.
1077 */
1078 value = apic_read(APIC_SPIV);
1079 value &= ~APIC_VECTOR_MASK;
1080 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001081
1082#ifdef CONFIG_X86_32
1083 /* This bit is reserved on P4/Xeon and should be cleared */
1084 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1085 (boot_cpu_data.x86 == 15))
1086 value &= ~APIC_SPIV_FOCUS_DISABLED;
1087 else
1088#endif
1089 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001091 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 /*
1094 * Set up the virtual wire mode.
1095 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001096 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001098 if (!lapic_is_integrated()) /* 82489DX */
1099 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001100 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001103static void __cpuinit lapic_setup_esr(void)
1104{
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001105 unsigned int oldvalue, value, maxlvt;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001106
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001107 if (!lapic_is_integrated()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001108 pr_info("No ESR for 82489DX.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001109 return;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001110 }
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001111
Ingo Molnar08125d32009-01-28 05:08:44 +01001112 if (apic->disable_esr) {
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001113 /*
1114 * Something untraceable is creating bad interrupts on
1115 * secondary quads ... for the moment, just leave the
1116 * ESR disabled - we can't do anything useful with the
1117 * errors anyway - mbligh
1118 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001119 pr_info("Leaving ESR disabled.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001120 return;
1121 }
1122
1123 maxlvt = lapic_get_maxlvt();
1124 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1125 apic_write(APIC_ESR, 0);
1126 oldvalue = apic_read(APIC_ESR);
1127
1128 /* enables sending errors */
1129 value = ERROR_APIC_VECTOR;
1130 apic_write(APIC_LVTERR, value);
1131
1132 /*
1133 * spec says clear errors after enabling vector.
1134 */
1135 if (maxlvt > 3)
1136 apic_write(APIC_ESR, 0);
1137 value = apic_read(APIC_ESR);
1138 if (value != oldvalue)
1139 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1140 "vector: 0x%08x after: 0x%08x\n",
1141 oldvalue, value);
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001142}
1143
1144
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001145/**
1146 * setup_local_APIC - setup the local APIC
1147 */
1148void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
Andi Kleen739f33b2008-01-30 13:30:40 +01001150 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001151 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Jan Beulichf1182632009-01-14 12:27:35 +00001153 if (disable_apic) {
Ingo Molnar65a4e572009-01-31 03:36:17 +01001154 arch_disable_smp_support();
Jan Beulichf1182632009-01-14 12:27:35 +00001155 return;
1156 }
1157
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001158#ifdef CONFIG_X86_32
1159 /* Pound the ESR really hard over the head with a big hammer - mbligh */
Ingo Molnar08125d32009-01-28 05:08:44 +01001160 if (lapic_is_integrated() && apic->disable_esr) {
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001161 apic_write(APIC_ESR, 0);
1162 apic_write(APIC_ESR, 0);
1163 apic_write(APIC_ESR, 0);
1164 apic_write(APIC_ESR, 0);
1165 }
1166#endif
1167
Jack Steinerac23d4e2008-03-28 14:12:16 -05001168 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 /*
1171 * Double-check whether this APIC is really registered.
1172 * This is meaningless in clustered apic mode, so we skip it.
1173 */
Ingo Molnar7ed248d2009-01-28 03:43:47 +01001174 if (!apic->apic_id_registered())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 BUG();
1176
1177 /*
1178 * Intel recommends to set DFR, LDR and TPR before enabling
1179 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1180 * document number 292116). So here it goes...
1181 */
Ingo Molnara5c43292009-01-28 06:50:47 +01001182 apic->init_apic_ldr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 /*
1185 * Set Task Priority to 'accept all'. We never change this
1186 * later on.
1187 */
1188 value = apic_read(APIC_TASKPRI);
1189 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +01001190 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001193 * After a crash, we no longer service the interrupts and a pending
1194 * interrupt from previous kernel might still have ISR bit set.
1195 *
1196 * Most probably by now CPU has serviced that pending interrupt and
1197 * it might not have done the ack_APIC_irq() because it thought,
1198 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1199 * does not clear the ISR bit and cpu thinks it has already serivced
1200 * the interrupt. Hence a vector might get locked. It was noticed
1201 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1202 */
1203 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1204 value = apic_read(APIC_ISR + i*0x10);
1205 for (j = 31; j >= 0; j--) {
1206 if (value & (1<<j))
1207 ack_APIC_irq();
1208 }
1209 }
1210
1211 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 * Now that we are all set up, enable the APIC
1213 */
1214 value = apic_read(APIC_SPIV);
1215 value &= ~APIC_VECTOR_MASK;
1216 /*
1217 * Enable APIC
1218 */
1219 value |= APIC_SPIV_APIC_ENABLED;
1220
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001221#ifdef CONFIG_X86_32
1222 /*
1223 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1224 * certain networking cards. If high frequency interrupts are
1225 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1226 * entry is masked/unmasked at a high rate as well then sooner or
1227 * later IOAPIC line gets 'stuck', no more interrupts are received
1228 * from the device. If focus CPU is disabled then the hang goes
1229 * away, oh well :-(
1230 *
1231 * [ This bug can be reproduced easily with a level-triggered
1232 * PCI Ne2000 networking cards and PII/PIII processors, dual
1233 * BX chipset. ]
1234 */
1235 /*
1236 * Actually disabling the focus CPU check just makes the hang less
1237 * frequent as it makes the interrupt distributon model be more
1238 * like LRU than MRU (the short-term load is more even across CPUs).
1239 * See also the comment in end_level_ioapic_irq(). --macro
1240 */
1241
1242 /*
1243 * - enable focus processor (bit==0)
1244 * - 64bit mode always use processor focus
1245 * so no need to set it
1246 */
1247 value &= ~APIC_SPIV_FOCUS_DISABLED;
1248#endif
Andi Kleen3f14c742006-09-26 10:52:29 +02001249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * Set spurious IRQ vector
1252 */
1253 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001254 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 /*
1257 * Set up LVT0, LVT1:
1258 *
1259 * set up through-local-APIC on the BP's LINT0. This is not
1260 * strictly necessary in pure symmetric-IO mode, but sometimes
1261 * we delegate interrupts to the 8259A.
1262 */
1263 /*
1264 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1265 */
1266 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001267 if (!smp_processor_id() && (pic_mode || !value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001269 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001270 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 } else {
1272 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001273 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001274 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001276 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
1278 /*
1279 * only the BP should see the LINT1 NMI signal, obviously.
1280 */
1281 if (!smp_processor_id())
1282 value = APIC_DM_NMI;
1283 else
1284 value = APIC_DM_NMI | APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001285 if (!lapic_is_integrated()) /* 82489DX */
1286 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001287 apic_write(APIC_LVT1, value);
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001288
Jack Steinerac23d4e2008-03-28 14:12:16 -05001289 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +01001290}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Andi Kleen739f33b2008-01-30 13:30:40 +01001292void __cpuinit end_local_APIC_setup(void)
1293{
1294 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001295
1296#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001297 {
1298 unsigned int value;
1299 /* Disable the local apic timer */
1300 value = apic_read(APIC_LVTT);
1301 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1302 apic_write(APIC_LVTT, value);
1303 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001304#endif
1305
Don Zickusf2802e72006-09-26 10:52:26 +02001306 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 apic_pm_activate();
1308}
1309
Yinghai Lu49899ea2008-08-24 02:01:47 -07001310#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001311void check_x2apic(void)
1312{
1313 int msr, msr2;
1314
1315 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1316
1317 if (msr & X2APIC_ENABLE) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001318 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001319 x2apic_preenabled = x2apic = 1;
1320 apic_ops = &x2apic_ops;
1321 }
1322}
1323
1324void enable_x2apic(void)
1325{
1326 int msr, msr2;
1327
1328 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1329 if (!(msr & X2APIC_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001330 pr_info("Enabling x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001331 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1332 }
1333}
1334
Al Viro2236d252008-11-22 17:37:34 +00001335void __init enable_IR_x2apic(void)
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001336{
1337#ifdef CONFIG_INTR_REMAP
1338 int ret;
1339 unsigned long flags;
1340
1341 if (!cpu_has_x2apic)
1342 return;
1343
1344 if (!x2apic_preenabled && disable_x2apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001345 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1346 "because of nox2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001347 return;
1348 }
1349
1350 if (x2apic_preenabled && disable_x2apic)
1351 panic("Bios already enabled x2apic, can't enforce nox2apic");
1352
1353 if (!x2apic_preenabled && skip_ioapic_setup) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001354 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1355 "because of skipping io-apic setup\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001356 return;
1357 }
1358
1359 ret = dmar_table_init();
1360 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001361 pr_info("dmar_table_init() failed with %d:\n", ret);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001362
1363 if (x2apic_preenabled)
1364 panic("x2apic enabled by bios. But IR enabling failed");
1365 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001366 pr_info("Not enabling x2apic,Intr-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001367 return;
1368 }
1369
1370 local_irq_save(flags);
1371 mask_8259A();
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001372
1373 ret = save_mask_IO_APIC_setup();
1374 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001375 pr_info("Saving IO-APIC state failed: %d\n", ret);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001376 goto end;
1377 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001378
1379 ret = enable_intr_remapping(1);
1380
1381 if (ret && x2apic_preenabled) {
1382 local_irq_restore(flags);
1383 panic("x2apic enabled by bios. But IR enabling failed");
1384 }
1385
1386 if (ret)
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001387 goto end_restore;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001388
1389 if (!x2apic) {
1390 x2apic = 1;
1391 apic_ops = &x2apic_ops;
1392 enable_x2apic();
1393 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001394
1395end_restore:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001396 if (ret)
1397 /*
1398 * IR enabling failed
1399 */
1400 restore_IO_APIC_setup();
1401 else
1402 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1403
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001404end:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001405 unmask_8259A();
1406 local_irq_restore(flags);
1407
1408 if (!ret) {
1409 if (!x2apic_preenabled)
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001410 pr_info("Enabled x2apic and interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001411 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001412 pr_info("Enabled Interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001413 } else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001414 pr_err("Failed to enable Interrupt-remapping and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001415#else
1416 if (!cpu_has_x2apic)
1417 return;
1418
1419 if (x2apic_preenabled)
1420 panic("x2apic enabled prior OS handover,"
1421 " enable CONFIG_INTR_REMAP");
1422
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001423 pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1424 " and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001425#endif
1426
1427 return;
1428}
Yinghai Lu49899ea2008-08-24 02:01:47 -07001429#endif /* HAVE_X2APIC */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001430
Yinghai Lube7a6562008-08-24 02:01:51 -07001431#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001432/*
1433 * Detect and enable local APICs on non-SMP boards.
1434 * Original code written by Keir Fraser.
1435 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1436 * not correctly set up (usually the APIC timer won't work etc.)
1437 */
1438static int __init detect_init_APIC(void)
1439{
1440 if (!cpu_has_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001441 pr_info("No local APIC present\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001442 return -1;
1443 }
1444
1445 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001446 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001447 return 0;
1448}
Yinghai Lube7a6562008-08-24 02:01:51 -07001449#else
1450/*
1451 * Detect and initialize APIC
1452 */
1453static int __init detect_init_APIC(void)
1454{
1455 u32 h, l, features;
1456
1457 /* Disabled by kernel option? */
1458 if (disable_apic)
1459 return -1;
1460
1461 switch (boot_cpu_data.x86_vendor) {
1462 case X86_VENDOR_AMD:
1463 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
Borislav Petkov85877062009-02-03 16:24:22 +01001464 (boot_cpu_data.x86 >= 15))
Yinghai Lube7a6562008-08-24 02:01:51 -07001465 break;
1466 goto no_apic;
1467 case X86_VENDOR_INTEL:
1468 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1469 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1470 break;
1471 goto no_apic;
1472 default:
1473 goto no_apic;
1474 }
1475
1476 if (!cpu_has_apic) {
1477 /*
1478 * Over-ride BIOS and try to enable the local APIC only if
1479 * "lapic" specified.
1480 */
1481 if (!force_enable_local_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001482 pr_info("Local APIC disabled by BIOS -- "
1483 "you can enable it with \"lapic\"\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001484 return -1;
1485 }
1486 /*
1487 * Some BIOSes disable the local APIC in the APIC_BASE
1488 * MSR. This can only be done in software for Intel P6 or later
1489 * and AMD K7 (Model > 1) or later.
1490 */
1491 rdmsr(MSR_IA32_APICBASE, l, h);
1492 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001493 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001494 l &= ~MSR_IA32_APICBASE_BASE;
1495 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1496 wrmsr(MSR_IA32_APICBASE, l, h);
1497 enabled_via_apicbase = 1;
1498 }
1499 }
1500 /*
1501 * The APIC feature bit should now be enabled
1502 * in `cpuid'
1503 */
1504 features = cpuid_edx(1);
1505 if (!(features & (1 << X86_FEATURE_APIC))) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001506 pr_warning("Could not enable APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001507 return -1;
1508 }
1509 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1510 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1511
1512 /* The BIOS may have set up the APIC at some other address */
1513 rdmsr(MSR_IA32_APICBASE, l, h);
1514 if (l & MSR_IA32_APICBASE_ENABLE)
1515 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1516
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001517 pr_info("Found and enabled local APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001518
1519 apic_pm_activate();
1520
1521 return 0;
1522
1523no_apic:
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001524 pr_info("No local APIC present or hardware disabled\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001525 return -1;
1526}
1527#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001528
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001529#ifdef CONFIG_X86_64
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001530void __init early_init_lapic_mapping(void)
1531{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001532 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001533
1534 /*
1535 * If no local APIC can be found then go out
1536 * : it means there is no mpatable and MADT
1537 */
1538 if (!smp_found_config)
1539 return;
1540
Thomas Gleixner431ee792008-05-12 15:43:35 +02001541 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001542
Thomas Gleixner431ee792008-05-12 15:43:35 +02001543 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001544 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001545 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001546
1547 /*
1548 * Fetch the APIC ID of the BSP in case we have a
1549 * default configuration (or the MP table is broken).
1550 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001551 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001552}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001553#endif
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001554
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001555/**
1556 * init_apic_mappings - initialize APIC mappings
1557 */
1558void __init init_apic_mappings(void)
1559{
Yinghai Lu49899ea2008-08-24 02:01:47 -07001560#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001561 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001562 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001563 return;
1564 }
Yinghai Lu49899ea2008-08-24 02:01:47 -07001565#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001566
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001567 /*
1568 * If no local APIC can be found then set up a fake all
1569 * zeroes page to simulate the local APIC and another
1570 * one for the IO-APIC.
1571 */
1572 if (!smp_found_config && detect_init_APIC()) {
1573 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1574 apic_phys = __pa(apic_phys);
1575 } else
1576 apic_phys = mp_lapic_addr;
1577
1578 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Yinghai Lu79c09692008-09-07 17:58:57 -07001579 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001580 APIC_BASE, apic_phys);
1581
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001582 /*
1583 * Fetch the APIC ID of the BSP in case we have a
1584 * default configuration (or the MP table is broken).
1585 */
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001586 if (boot_cpu_physical_apicid == -1U)
1587 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001588}
1589
1590/*
1591 * This initializes the IO-APIC and APIC hardware if this is
1592 * a UP kernel.
1593 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001594int apic_version[MAX_APICS];
1595
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001596int __init APIC_init_uniprocessor(void)
1597{
1598 if (disable_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001599 pr_info("Apic disabled\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001600 return -1;
1601 }
Jan Beulichf1182632009-01-14 12:27:35 +00001602#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001603 if (!cpu_has_apic) {
1604 disable_apic = 1;
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001605 pr_info("Apic disabled by BIOS\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001606 return -1;
1607 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001608#else
1609 if (!smp_found_config && !cpu_has_apic)
1610 return -1;
1611
1612 /*
1613 * Complain if the BIOS pretends there is one.
1614 */
1615 if (!cpu_has_apic &&
1616 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001617 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1618 boot_cpu_physical_apicid);
Yinghai Lufa2bd352008-08-24 02:01:50 -07001619 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1620 return -1;
1621 }
1622#endif
1623
Yinghai Lu49899ea2008-08-24 02:01:47 -07001624#ifdef HAVE_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001625 enable_IR_x2apic();
Yinghai Lu49899ea2008-08-24 02:01:47 -07001626#endif
Yinghai Lufa2bd352008-08-24 02:01:50 -07001627#ifdef CONFIG_X86_64
Ingo Molnar72ce0162009-01-28 06:50:47 +01001628 default_setup_apic_routing();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001629#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001630
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001631 verify_local_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001632 connect_bsp_APIC();
1633
Yinghai Lufa2bd352008-08-24 02:01:50 -07001634#ifdef CONFIG_X86_64
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001635 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Yinghai Lufa2bd352008-08-24 02:01:50 -07001636#else
1637 /*
1638 * Hack: In case of kdump, after a crash, kernel might be booting
1639 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1640 * might be zero if read from MP tables. Get it from LAPIC.
1641 */
1642# ifdef CONFIG_CRASH_DUMP
1643 boot_cpu_physical_apicid = read_apic_id();
1644# endif
1645#endif
1646 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001647 setup_local_APIC();
1648
Yinghai Lu88d0f552009-02-14 23:57:28 -08001649#ifdef CONFIG_X86_IO_APIC
Andi Kleen739f33b2008-01-30 13:30:40 +01001650 /*
1651 * Now enable IO-APICs, actually call clear_IO_APIC
1652 * We need clear_IO_APIC before enabling vector on BP
1653 */
1654 if (!skip_ioapic_setup && nr_ioapics)
1655 enable_IO_APIC();
1656
Maciej W. Rozyckiacae7d92008-06-06 03:27:49 +01001657 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1658 localise_nmi_watchdog();
Yinghai Lu88d0f552009-02-14 23:57:28 -08001659#else
1660 localise_nmi_watchdog();
1661#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001662 end_local_APIC_setup();
1663
Yinghai Lufa2bd352008-08-24 02:01:50 -07001664#ifdef CONFIG_X86_IO_APIC
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001665 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1666 setup_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001667# ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001668 else
1669 nr_ioapics = 0;
Yinghai Lufa2bd352008-08-24 02:01:50 -07001670# endif
1671#endif
1672
1673#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001674 setup_boot_APIC_clock();
1675 check_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001676#else
1677 setup_boot_clock();
1678#endif
1679
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001680 return 0;
1681}
1682
1683/*
1684 * Local APIC interrupts
1685 */
1686
1687/*
1688 * This interrupt should _never_ happen with our APIC/SMP architecture
1689 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001690void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001691{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001692 u32 v;
1693
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001694 exit_idle();
1695 irq_enter();
1696 /*
1697 * Check if this really is a spurious interrupt and ACK it
1698 * if it is a vectored one. Just in case...
1699 * Spurious interrupts should not be ACKed.
1700 */
1701 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1702 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1703 ack_APIC_irq();
1704
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -08001705 inc_irq_stat(irq_spurious_count);
1706
Yinghai Ludc1528d2008-08-24 02:01:53 -07001707 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001708 pr_info("spurious APIC interrupt on CPU#%d, "
1709 "should never happen.\n", smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001710 irq_exit();
1711}
1712
1713/*
1714 * This interrupt should never happen with our APIC/SMP architecture
1715 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001716void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001717{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001718 u32 v, v1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001719
1720 exit_idle();
1721 irq_enter();
1722 /* First tickle the hardware, only then report what went on. -- REW */
1723 v = apic_read(APIC_ESR);
1724 apic_write(APIC_ESR, 0);
1725 v1 = apic_read(APIC_ESR);
1726 ack_APIC_irq();
1727 atomic_inc(&irq_err_count);
1728
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001729 /*
1730 * Here is what the APIC error bits mean:
1731 * 0: Send CS error
1732 * 1: Receive CS error
1733 * 2: Send accept error
1734 * 3: Receive accept error
1735 * 4: Reserved
1736 * 5: Send illegal vector
1737 * 6: Received illegal vector
1738 * 7: Illegal register address
1739 */
1740 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001741 smp_processor_id(), v , v1);
1742 irq_exit();
1743}
1744
Glauber Costab5841762008-05-28 13:38:28 -03001745/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001746 * connect_bsp_APIC - attach the APIC to the interrupt system
1747 */
Glauber Costab5841762008-05-28 13:38:28 -03001748void __init connect_bsp_APIC(void)
1749{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001750#ifdef CONFIG_X86_32
1751 if (pic_mode) {
1752 /*
1753 * Do not trust the local APIC being empty at bootup.
1754 */
1755 clear_local_APIC();
1756 /*
1757 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1758 * local APIC to INT and NMI lines.
1759 */
1760 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1761 "enabling APIC mode.\n");
1762 outb(0x70, 0x22);
1763 outb(0x01, 0x23);
1764 }
1765#endif
Ingo Molnar49040332009-01-28 12:43:18 +01001766 if (apic->enable_apic_mode)
1767 apic->enable_apic_mode();
Glauber Costab5841762008-05-28 13:38:28 -03001768}
1769
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001770/**
1771 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1772 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1773 *
1774 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1775 * APIC is disabled.
1776 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001777void disconnect_bsp_APIC(int virt_wire_setup)
1778{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001779 unsigned int value;
1780
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001781#ifdef CONFIG_X86_32
1782 if (pic_mode) {
1783 /*
1784 * Put the board back into PIC mode (has an effect only on
1785 * certain older boards). Note that APIC interrupts, including
1786 * IPIs, won't work beyond this point! The only exception are
1787 * INIT IPIs.
1788 */
1789 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1790 "entering PIC mode.\n");
1791 outb(0x70, 0x22);
1792 outb(0x00, 0x23);
1793 return;
1794 }
1795#endif
1796
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001797 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001798
1799 /* For the spurious interrupt use vector F, and enable it */
1800 value = apic_read(APIC_SPIV);
1801 value &= ~APIC_VECTOR_MASK;
1802 value |= APIC_SPIV_APIC_ENABLED;
1803 value |= 0xf;
1804 apic_write(APIC_SPIV, value);
1805
1806 if (!virt_wire_setup) {
1807 /*
1808 * For LVT0 make it edge triggered, active high,
1809 * external and enabled
1810 */
1811 value = apic_read(APIC_LVT0);
1812 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1813 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1814 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1815 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1816 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1817 apic_write(APIC_LVT0, value);
1818 } else {
1819 /* Disable LVT0 */
1820 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1821 }
1822
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001823 /*
1824 * For LVT1 make it edge triggered, active high,
1825 * nmi and enabled
1826 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001827 value = apic_read(APIC_LVT1);
1828 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1829 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1830 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1831 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1832 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1833 apic_write(APIC_LVT1, value);
1834}
1835
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001836void __cpuinit generic_processor_info(int apicid, int version)
1837{
1838 int cpu;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001839
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001840 /*
1841 * Validate version
1842 */
1843 if (version == 0x0) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001844 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
Mike Travis3b11ce72008-12-17 15:21:39 -08001845 "fixing up to 0x10. (tell your hw vendor)\n",
1846 version);
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001847 version = 0x10;
1848 }
1849 apic_version[apicid] = version;
1850
Mike Travis3b11ce72008-12-17 15:21:39 -08001851 if (num_processors >= nr_cpu_ids) {
1852 int max = nr_cpu_ids;
1853 int thiscpu = max + disabled_cpus;
1854
1855 pr_warning(
1856 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1857 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1858
1859 disabled_cpus++;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001860 return;
1861 }
1862
1863 num_processors++;
Mike Travis3b11ce72008-12-17 15:21:39 -08001864 cpu = cpumask_next_zero(-1, cpu_present_mask);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001865
Mike Traviscef30b32009-01-16 15:58:13 -08001866 if (version != apic_version[boot_cpu_physical_apicid])
1867 WARN_ONCE(1,
1868 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1869 apic_version[boot_cpu_physical_apicid], cpu, version);
1870
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001871 physid_set(apicid, phys_cpu_present_map);
1872 if (apicid == boot_cpu_physical_apicid) {
1873 /*
1874 * x86_bios_cpu_apicid is required to have processors listed
1875 * in same order as logical cpu numbers. Hence the first
1876 * entry is BSP, and so on.
1877 */
1878 cpu = 0;
1879 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001880 if (apicid > max_physical_apicid)
1881 max_physical_apicid = apicid;
1882
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001883#ifdef CONFIG_X86_32
1884 /*
1885 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1886 * but we need to work other dependencies like SMP_SUSPEND etc
1887 * before this can be done without some confusion.
1888 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1889 * - Ashok Raj <ashok.raj@intel.com>
1890 */
1891 if (max_physical_apicid >= 8) {
1892 switch (boot_cpu_data.x86_vendor) {
1893 case X86_VENDOR_INTEL:
1894 if (!APIC_XAPIC(version)) {
1895 def_to_bigsmp = 0;
1896 break;
1897 }
1898 /* If P4 and above fall through */
1899 case X86_VENDOR_AMD:
1900 def_to_bigsmp = 1;
1901 }
1902 }
1903#endif
1904
Ingo Molnar3e5095d2009-01-27 17:07:08 +01001905#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
Tejun Heof10fcd42009-01-13 20:41:34 +09001906 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1907 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001908#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001909
Mike Travis1de88cd2008-12-16 17:34:02 -08001910 set_cpu_possible(cpu, true);
1911 set_cpu_present(cpu, true);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001912}
1913
Suresh Siddha0c81c742008-07-10 11:16:48 -07001914int hard_smp_processor_id(void)
1915{
1916 return read_apic_id();
1917}
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +01001918
1919void default_init_apic_ldr(void)
1920{
1921 unsigned long val;
1922
1923 apic_write(APIC_DFR, APIC_DFR_VALUE);
1924 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
1925 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1926 apic_write(APIC_LDR, val);
1927}
1928
1929#ifdef CONFIG_X86_32
1930int default_apicid_to_node(int logical_apicid)
1931{
1932#ifdef CONFIG_SMP
1933 return apicid_2_node[hard_smp_processor_id()];
1934#else
1935 return 0;
1936#endif
1937}
Yinghai Lu34919982008-08-24 02:01:48 -07001938#endif
Suresh Siddha0c81c742008-07-10 11:16:48 -07001939
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001940/*
1941 * Power management
1942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943#ifdef CONFIG_PM
1944
1945static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001946 /*
1947 * 'active' is true if the local APIC was enabled by us and
1948 * not the BIOS; this signifies that we are also responsible
1949 * for disabling it before entering apm/acpi suspend
1950 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 int active;
1952 /* r/w apic fields */
1953 unsigned int apic_id;
1954 unsigned int apic_taskpri;
1955 unsigned int apic_ldr;
1956 unsigned int apic_dfr;
1957 unsigned int apic_spiv;
1958 unsigned int apic_lvtt;
1959 unsigned int apic_lvtpc;
1960 unsigned int apic_lvt0;
1961 unsigned int apic_lvt1;
1962 unsigned int apic_lvterr;
1963 unsigned int apic_tmict;
1964 unsigned int apic_tdcr;
1965 unsigned int apic_thmr;
1966} apic_pm_state;
1967
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001968static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
1970 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001971 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 if (!apic_pm_state.active)
1974 return 0;
1975
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001976 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001977
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001978 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1980 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1981 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1982 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1983 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001984 if (maxlvt >= 4)
1985 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1987 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1988 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1989 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1990 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001991#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001992 if (maxlvt >= 5)
1993 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1994#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001995
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001996 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 disable_local_APIC();
1998 local_irq_restore(flags);
1999 return 0;
2000}
2001
2002static int lapic_resume(struct sys_device *dev)
2003{
2004 unsigned int l, h;
2005 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002006 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 if (!apic_pm_state.active)
2009 return 0;
2010
Thomas Gleixner37e650c2008-01-30 13:30:14 +01002011 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 local_irq_save(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002014
Yinghai Lu49899ea2008-08-24 02:01:47 -07002015#ifdef HAVE_X2APIC
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002016 if (x2apic)
2017 enable_x2apic();
2018 else
2019#endif
Yinghai Lud5e629a2008-08-17 21:12:27 -07002020 {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002021 /*
2022 * Make sure the APICBASE points to the right address
2023 *
2024 * FIXME! This will be wrong if we ever support suspend on
2025 * SMP! We'll need to do this as part of the CPU restore!
2026 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002027 rdmsr(MSR_IA32_APICBASE, l, h);
2028 l &= ~MSR_IA32_APICBASE_BASE;
2029 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2030 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07002031 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002032
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2034 apic_write(APIC_ID, apic_pm_state.apic_id);
2035 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2036 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2037 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2038 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2039 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2040 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002041#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002042 if (maxlvt >= 5)
2043 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2044#endif
2045 if (maxlvt >= 4)
2046 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2048 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2049 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2050 apic_write(APIC_ESR, 0);
2051 apic_read(APIC_ESR);
2052 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2053 apic_write(APIC_ESR, 0);
2054 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return 0;
2059}
2060
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002061/*
2062 * This device has no shutdown method - fully functioning local APICs
2063 * are needed on every CPU up until machine_halt/restart/poweroff.
2064 */
2065
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002067 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 .resume = lapic_resume,
2069 .suspend = lapic_suspend,
2070};
2071
2072static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002073 .id = 0,
2074 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075};
2076
Ashok Raje6982c62005-06-25 14:54:58 -07002077static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 apic_pm_state.active = 1;
2080}
2081
2082static int __init init_lapic_sysfs(void)
2083{
2084 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 if (!cpu_has_apic)
2087 return 0;
2088 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 error = sysdev_class_register(&lapic_sysclass);
2091 if (!error)
2092 error = sysdev_register(&device_lapic);
2093 return error;
2094}
2095device_initcall(init_lapic_sysfs);
2096
2097#else /* CONFIG_PM */
2098
2099static void apic_pm_activate(void) { }
2100
2101#endif /* CONFIG_PM */
2102
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002103#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002105 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 *
2107 * Thus far, the major user of this is IBM's Summit2 series:
2108 *
Linus Torvalds637029c2006-02-27 20:41:56 -08002109 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 * multi-chassis. Use available data to take a good guess.
2111 * If in doubt, go HPET.
2112 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002113__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 int i, clusters, zeros;
2116 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08002117 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2119
Yinghai Lu322850a2008-02-23 21:48:42 -08002120 /*
2121 * there is not this kind of box with AMD CPU yet.
2122 * Some AMD box with quadcore cpu and 8 sockets apicid
2123 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08002124 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08002125 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002126 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08002127 return 0;
2128
Mike Travis23ca4bb2008-05-12 21:21:12 +02002129 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07002130 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Mike Travis168ef542008-12-16 17:34:01 -08002132 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002133 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01002134 if (bios_cpu_apicid) {
2135 id = bios_cpu_apicid[i];
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302136 } else if (i < nr_cpu_ids) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002137 if (cpu_present(i))
2138 id = per_cpu(x86_bios_cpu_apicid, i);
2139 else
2140 continue;
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302141 } else
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002142 break;
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 if (id != BAD_APICID)
2145 __set_bit(APIC_CLUSTERID(id), clustermap);
2146 }
2147
2148 /* Problem: Partially populated chassis may not have CPUs in some of
2149 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01002150 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2151 * Since clusters are allocated sequentially, count zeros only if
2152 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 */
2154 clusters = 0;
2155 zeros = 0;
2156 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2157 if (test_bit(i, clustermap)) {
2158 clusters += 1 + zeros;
2159 zeros = 0;
2160 } else
2161 ++zeros;
2162 }
2163
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002164 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2165 * not guaranteed to be synced between boards
2166 */
2167 if (is_vsmp_box() && clusters > 1)
2168 return 1;
2169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002171 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 * May have to revisit this when multi-core + hyperthreaded CPUs come
2173 * out, but AFAIK this will work even for them.
2174 */
2175 return (clusters > 2);
2176}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002177#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002180 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002182static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07002185 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002186 return 0;
2187}
2188early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002190/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002191static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002192{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002193 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002194}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002195early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Linus Torvalds2e7c2832007-03-23 11:32:31 -07002197static int __init parse_lapic_timer_c2_ok(char *arg)
2198{
2199 local_apic_timer_c2_ok = 1;
2200 return 0;
2201}
2202early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2203
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002204static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002205{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002207 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002208}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002209early_param("noapictimer", parse_disable_apic_timer);
2210
2211static int __init parse_nolapic_timer(char *arg)
2212{
2213 disable_apic_timer = 1;
2214 return 0;
2215}
2216early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01002217
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002218static int __init apic_set_verbosity(char *arg)
2219{
2220 if (!arg) {
2221#ifdef CONFIG_X86_64
2222 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002223 return 0;
2224#endif
2225 return -EINVAL;
2226 }
2227
2228 if (strcmp("debug", arg) == 0)
2229 apic_verbosity = APIC_DEBUG;
2230 else if (strcmp("verbose", arg) == 0)
2231 apic_verbosity = APIC_VERBOSE;
2232 else {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01002233 pr_warning("APIC Verbosity level %s not recognised"
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002234 " use apic=verbose or apic=debug\n", arg);
2235 return -EINVAL;
2236 }
2237
2238 return 0;
2239}
2240early_param("apic", apic_set_verbosity);
2241
Yinghai Lu1e934dd2008-02-22 13:37:26 -08002242static int __init lapic_insert_resource(void)
2243{
2244 if (!apic_phys)
2245 return -1;
2246
2247 /* Put local APIC into the resource map. */
2248 lapic_resource.start = apic_phys;
2249 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2250 insert_resource(&iomem_resource, &lapic_resource);
2251
2252 return 0;
2253}
2254
2255/*
2256 * need call insert after e820_reserve_resources()
2257 * that is using request_resource
2258 */
2259late_initcall(lapic_insert_resource);