blob: 2bfeafd24f5cca0a2d70458304c1c43287453c66 [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
Ingo Molnarcdd6c482009-09-21 12:02:48 +020017#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/kernel_stat.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010019#include <linux/mc146818rtc.h>
Thomas Gleixner70a20022008-01-30 13:30:18 +010020#include <linux/acpi_pmtmr.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010021#include <linux/clockchips.h>
22#include <linux/interrupt.h>
23#include <linux/bootmem.h>
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +010024#include <linux/ftrace.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010025#include <linux/ioport.h>
26#include <linux/module.h>
27#include <linux/sysdev.h>
28#include <linux/delay.h>
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +053029#include <linux/timex.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010030#include <linux/dmar.h>
31#include <linux/init.h>
32#include <linux/cpu.h>
33#include <linux/dmi.h>
34#include <linux/nmi.h>
35#include <linux/smp.h>
36#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Ingo Molnarcdd6c482009-09-21 12:02:48 +020038#include <asm/perf_event.h>
Thomas Gleixner736deca2009-08-19 12:35:53 +020039#include <asm/x86_init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/pgalloc.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010041#include <asm/atomic.h>
42#include <asm/mpspec.h>
Yinghai Lu773763d2008-08-24 02:01:52 -070043#include <asm/i8253.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010044#include <asm/i8259.h>
Andi Kleen73dea472006-02-03 21:50:50 +010045#include <asm/proto.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020046#include <asm/apic.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010047#include <asm/desc.h>
48#include <asm/hpet.h>
49#include <asm/idle.h>
50#include <asm/mtrr.h>
Jaswinder Singh Rajput2bc13792009-01-11 20:34:47 +053051#include <asm/smp.h>
Andi Kleenbe71b852009-02-12 13:49:38 +010052#include <asm/mce.h>
Gleb Natapovce69a782009-07-20 15:24:17 +030053#include <asm/kvm_para.h>
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -070054#include <asm/tsc.h>
Robert Richtera68c4392010-10-06 12:27:53 +020055#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Brian Gerstec70de82009-01-27 12:56:47 +090057unsigned int num_processors;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010058
Brian Gerstec70de82009-01-27 12:56:47 +090059unsigned disabled_cpus __cpuinitdata;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010060
Brian Gerstec70de82009-01-27 12:56:47 +090061/* Processor that is doing the boot up */
62unsigned int boot_cpu_physical_apicid = -1U;
Glauber Costa5af55732008-03-25 13:28:56 -030063
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070064/*
Ingo Molnarfdbecd92009-01-31 03:57:12 +010065 * The highest APIC ID seen during enumeration.
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070066 */
Brian Gerstec70de82009-01-27 12:56:47 +090067unsigned int max_physical_apicid;
68
Ingo Molnarfdbecd92009-01-31 03:57:12 +010069/*
70 * Bitmask of physically existing CPUs:
71 */
Brian Gerstec70de82009-01-27 12:56:47 +090072physid_mask_t phys_cpu_present_map;
73
74/*
75 * Map cpu index to physical APIC ID
76 */
77DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
78DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
79EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
80EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070081
Yinghai Lub3c51172008-08-24 02:01:46 -070082#ifdef CONFIG_X86_32
83/*
84 * Knob to control our willingness to enable the local APIC.
85 *
86 * +1=force-enable
87 */
88static int force_enable_local_apic;
89/*
90 * APIC command line parameters
91 */
92static int __init parse_lapic(char *arg)
93{
94 force_enable_local_apic = 1;
95 return 0;
96}
97early_param("lapic", parse_lapic);
Yinghai Luf28c0ae2008-08-24 02:01:49 -070098/* Local APIC was disabled by the BIOS and enabled by the kernel */
99static int enabled_via_apicbase;
100
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +0400101/*
102 * Handle interrupt mode configuration register (IMCR).
103 * This register controls whether the interrupt signals
104 * that reach the BSP come from the master PIC or from the
105 * local APIC. Before entering Symmetric I/O Mode, either
106 * the BIOS or the operating system must switch out of
107 * PIC Mode by changing the IMCR.
108 */
Alexander van Heukelum5cda3952009-04-13 17:39:24 +0200109static inline void imcr_pic_to_apic(void)
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +0400110{
111 /* select IMCR register */
112 outb(0x70, 0x22);
113 /* NMI and 8259 INTR go through APIC */
114 outb(0x01, 0x23);
115}
116
Alexander van Heukelum5cda3952009-04-13 17:39:24 +0200117static inline void imcr_apic_to_pic(void)
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +0400118{
119 /* select IMCR register */
120 outb(0x70, 0x22);
121 /* NMI and 8259 INTR go directly to BSP */
122 outb(0x00, 0x23);
123}
Yinghai Lub3c51172008-08-24 02:01:46 -0700124#endif
125
126#ifdef CONFIG_X86_64
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200127static int apic_calibrate_pmtmr __initdata;
Yinghai Lub3c51172008-08-24 02:01:46 -0700128static __init int setup_apicpmtimer(char *s)
129{
130 apic_calibrate_pmtmr = 1;
131 notsc_setup(NULL);
132 return 0;
133}
134__setup("apicpmtimer", setup_apicpmtimer);
135#endif
136
Suresh Siddhafc1edaf2009-04-20 13:02:27 -0700137int x2apic_mode;
Yinghai Lu06cd9a72009-02-16 17:29:58 -0800138#ifdef CONFIG_X86_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -0700139/* x2apic enabled before OS handover */
Jaswinder Singhb6b301a2008-12-23 21:52:33 +0530140static int x2apic_preenabled;
Yinghai Lu49899ea2008-08-24 02:01:47 -0700141static __init int setup_nox2apic(char *str)
142{
Suresh Siddha39d83a52009-04-20 13:02:29 -0700143 if (x2apic_enabled()) {
144 pr_warning("Bios already enabled x2apic, "
145 "can't enforce nox2apic");
146 return 0;
147 }
148
Yinghai Lu49899ea2008-08-24 02:01:47 -0700149 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
150 return 0;
151}
152early_param("nox2apic", setup_nox2apic);
153#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Yinghai Lub3c51172008-08-24 02:01:46 -0700155unsigned long mp_lapic_addr;
156int disable_apic;
157/* Disable local APIC timer from the kernel commandline or via dmi quirk */
158static int disable_apic_timer __cpuinitdata;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100159/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -0700160int local_apic_timer_c2_ok;
161EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
162
Yinghai Luefa25592008-08-19 20:50:36 -0700163int first_system_vector = 0xfe;
164
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100165/*
166 * Debug level, exported for io_apic.c
167 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +0100168unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100169
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -0700170int pic_mode;
171
Alexey Starikovskiybab4b272008-05-19 19:47:03 +0400172/* Have we found an MP table */
173int smp_found_config;
174
Aaron Durbin39928722006-12-07 02:14:01 +0100175static struct resource lapic_resource = {
176 .name = "Local APIC",
177 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
178};
179
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200180static unsigned int calibration_result;
181
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200182static int lapic_next_event(unsigned long delta,
183 struct clock_event_device *evt);
184static void lapic_timer_setup(enum clock_event_mode mode,
185 struct clock_event_device *evt);
Mike Travis96289372008-12-31 18:08:46 -0800186static void lapic_timer_broadcast(const struct cpumask *mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100187static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200188
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400189/*
190 * The local apic timer can be used for any function which is CPU local.
191 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200192static struct clock_event_device lapic_clockevent = {
193 .name = "lapic",
194 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
195 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
196 .shift = 32,
197 .set_mode = lapic_timer_setup,
198 .set_next_event = lapic_next_event,
199 .broadcast = lapic_timer_broadcast,
200 .rating = 100,
201 .irq = -1,
202};
203static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
204
Andi Kleend3432892008-01-30 13:33:17 +0100205static unsigned long apic_phys;
206
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100207/*
208 * Get the LAPIC version
209 */
210static inline int lapic_get_version(void)
211{
212 return GET_APIC_VERSION(apic_read(APIC_LVR));
213}
214
215/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400216 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100217 */
218static inline int lapic_is_integrated(void)
219{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400220#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100221 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400222#else
223 return APIC_INTEGRATED(lapic_get_version());
224#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100225}
226
227/*
228 * Check, whether this is a modern or a first generation APIC
229 */
230static int modern_apic(void)
231{
232 /* AMD systems use old APIC versions, so check the CPU */
233 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
234 boot_cpu_data.x86 >= 0xf)
235 return 1;
236 return lapic_get_version() >= 0x14;
237}
238
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +0400239/*
Cyrill Gorcunova933c612009-10-14 00:07:04 +0400240 * right after this call apic become NOOP driven
241 * so apic->write/read doesn't do anything
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +0400242 */
243void apic_disable(void)
244{
Cyrill Gorcunovf88f2b42009-10-15 19:04:16 +0400245 pr_info("APIC: switched to apic NOOP\n");
Cyrill Gorcunova933c612009-10-14 00:07:04 +0400246 apic = &apic_noop;
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +0400247}
248
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800249void native_apic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100250{
251 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
252 cpu_relax();
253}
254
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800255u32 native_safe_apic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100256{
257 u32 send_status;
258 int timeout;
259
260 timeout = 0;
261 do {
262 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
263 if (!send_status)
264 break;
265 udelay(100);
266 } while (timeout++ < 1000);
267
268 return send_status;
269}
270
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800271void native_apic_icr_write(u32 low, u32 id)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700272{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200273 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700274 apic_write(APIC_ICR, low);
275}
276
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800277u64 native_apic_icr_read(void)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700278{
279 u32 icr1, icr2;
280
281 icr2 = apic_read(APIC_ICR2);
282 icr1 = apic_read(APIC_ICR);
283
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400284 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700285}
286
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100287/**
288 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
289 */
Jan Beuliche9427102008-01-30 13:31:24 +0100290void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100291{
292 unsigned int v;
293
294 /* unmask and set to NMI */
295 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200296
297 /* Level triggered for 82489DX (32bit mode) */
298 if (!lapic_is_integrated())
299 v |= APIC_LVT_LEVEL_TRIGGER;
300
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100301 apic_write(APIC_LVT0, v);
302}
303
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700304#ifdef CONFIG_X86_32
305/**
306 * get_physical_broadcast - Get number of physical broadcast IDs
307 */
308int get_physical_broadcast(void)
309{
310 return modern_apic() ? 0xff : 0xf;
311}
312#endif
313
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100314/**
315 * lapic_get_maxlvt - get the maximum number of local vector table entries
316 */
317int lapic_get_maxlvt(void)
318{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200319 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100320
321 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200322 /*
323 * - we always have APIC integrated on 64bit mode
324 * - 82489DXs do not report # of LVT entries
325 */
326 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100327}
328
329/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400330 * Local APIC timer
331 */
332
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400333/* Clock divisor */
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400334#define APIC_DIVISOR 16
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200335
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100336/*
337 * This function sets up the local APIC timer, with a timeout of
338 * 'clocks' APIC bus clock. During calibration we actually call
339 * this function twice on the boot CPU, once with a bogus timeout
340 * value, second time for real. The other (noncalibrating) CPUs
341 * call this function only once, with the real, calibrated value.
342 *
343 * We do reads before writes even if unnecessary, to get around the
344 * P5 APIC double write bug.
345 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100346static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
347{
348 unsigned int lvtt_value, tmp_value;
349
350 lvtt_value = LOCAL_TIMER_VECTOR;
351 if (!oneshot)
352 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200353 if (!lapic_is_integrated())
354 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
355
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100356 if (!irqen)
357 lvtt_value |= APIC_LVT_MASKED;
358
359 apic_write(APIC_LVTT, lvtt_value);
360
361 /*
362 * Divide PICLK by 16
363 */
364 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400365 apic_write(APIC_TDCR,
366 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
367 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100368
369 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200370 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100371}
372
373/*
Robert Richtera68c4392010-10-06 12:27:53 +0200374 * Setup extended LVT, AMD specific
Robert Richter7b83dae2008-01-30 13:30:40 +0100375 *
Robert Richtera68c4392010-10-06 12:27:53 +0200376 * Software should use the LVT offsets the BIOS provides. The offsets
377 * are determined by the subsystems using it like those for MCE
378 * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
379 * are supported. Beginning with family 10h at least 4 offsets are
380 * available.
Robert Richter286f5712008-07-22 21:08:46 +0200381 *
Robert Richtera68c4392010-10-06 12:27:53 +0200382 * Since the offsets must be consistent for all cores, we keep track
383 * of the LVT offsets in software and reserve the offset for the same
384 * vector also to be used on other cores. An offset is freed by
385 * setting the entry to APIC_EILVT_MASKED.
386 *
387 * If the BIOS is right, there should be no conflicts. Otherwise a
388 * "[Firmware Bug]: ..." error message is generated. However, if
389 * software does not properly determines the offsets, it is not
390 * necessarily a BIOS bug.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100391 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100392
393#define APIC_EILVT_LVTOFF_MCE 0
394#define APIC_EILVT_LVTOFF_IBS 1
395
Robert Richtera68c4392010-10-06 12:27:53 +0200396static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100397
Robert Richtera68c4392010-10-06 12:27:53 +0200398static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
399{
400 return (old & APIC_EILVT_MASKED)
401 || (new == APIC_EILVT_MASKED)
402 || ((new & ~APIC_EILVT_MASKED) == old);
403}
404
405static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
406{
407 unsigned int rsvd; /* 0: uninitialized */
408
409 if (offset >= APIC_EILVT_NR_MAX)
410 return ~0;
411
412 rsvd = atomic_read(&eilvt_offsets[offset]) & ~APIC_EILVT_MASKED;
413 do {
414 if (rsvd &&
415 !eilvt_entry_is_changeable(rsvd, new))
416 /* may not change if vectors are different */
417 return rsvd;
418 rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
419 } while (rsvd != new);
420
421 return new;
422}
423
424/*
425 * If mask=1, the LVT entry does not generate interrupts while mask=0
426 * enables the vector. See also the BKDGs.
427 */
428
429static int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
430{
431 unsigned long reg = APIC_EILVTn(offset);
432 unsigned int new, old, reserved;
433
434 new = (mask << 16) | (msg_type << 8) | vector;
435 old = apic_read(reg);
436 reserved = reserve_eilvt_offset(offset, new);
437
438 if (reserved != new) {
439 pr_err(FW_BUG "cpu %d, try to setup vector 0x%x, but "
440 "vector 0x%x was already reserved by another core, "
441 "APIC%lX=0x%x\n",
442 smp_processor_id(), new, reserved, reg, old);
443 return -EINVAL;
444 }
445
446 if (!eilvt_entry_is_changeable(old, new)) {
447 pr_err(FW_BUG "cpu %d, try to setup vector 0x%x but "
448 "register already in use, APIC%lX=0x%x\n",
449 smp_processor_id(), new, reg, old);
450 return -EBUSY;
451 }
452
453 apic_write(reg, new);
454
455 return 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100456}
457
Robert Richter7b83dae2008-01-30 13:30:40 +0100458u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
459{
460 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
461 return APIC_EILVT_LVTOFF_MCE;
462}
463
464u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
465{
466 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
467 return APIC_EILVT_LVTOFF_IBS;
468}
Robert Richter6aa360e2008-07-23 15:28:14 +0200469EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100470
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100471/*
472 * Program the next event, relative to now
473 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200474static int lapic_next_event(unsigned long delta,
475 struct clock_event_device *evt)
476{
477 apic_write(APIC_TMICT, delta);
478 return 0;
479}
480
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100481/*
482 * Setup the lapic timer in periodic or oneshot mode
483 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200484static void lapic_timer_setup(enum clock_event_mode mode,
485 struct clock_event_device *evt)
486{
487 unsigned long flags;
488 unsigned int v;
489
490 /* Lapic used as dummy for broadcast ? */
491 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
492 return;
493
494 local_irq_save(flags);
495
496 switch (mode) {
497 case CLOCK_EVT_MODE_PERIODIC:
498 case CLOCK_EVT_MODE_ONESHOT:
499 __setup_APIC_LVTT(calibration_result,
500 mode != CLOCK_EVT_MODE_PERIODIC, 1);
501 break;
502 case CLOCK_EVT_MODE_UNUSED:
503 case CLOCK_EVT_MODE_SHUTDOWN:
504 v = apic_read(APIC_LVTT);
505 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
506 apic_write(APIC_LVTT, v);
Andreas Herrmann6f9b4102009-10-27 11:01:38 +0100507 apic_write(APIC_TMICT, 0);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200508 break;
509 case CLOCK_EVT_MODE_RESUME:
510 /* Nothing to do here */
511 break;
512 }
513
514 local_irq_restore(flags);
515}
516
517/*
518 * Local APIC timer broadcast function
519 */
Mike Travis96289372008-12-31 18:08:46 -0800520static void lapic_timer_broadcast(const struct cpumask *mask)
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200521{
522#ifdef CONFIG_SMP
Ingo Molnardac5f412009-01-28 15:42:24 +0100523 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200524#endif
525}
526
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100527/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200528 * Setup the local APIC timer for this CPU. Copy the initialized values
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100529 * of the boot CPU and register the clock event in the framework.
530 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700531static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200532{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100533 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
534
Venkatesh Pallipadidb954b52009-04-06 18:51:29 -0700535 if (cpu_has(&current_cpu_data, X86_FEATURE_ARAT)) {
536 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
537 /* Make LAPIC timer preferrable over percpu HPET */
538 lapic_clockevent.rating = 150;
539 }
540
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100541 memcpy(levt, &lapic_clockevent, sizeof(*levt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030542 levt->cpumask = cpumask_of(smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100543
544 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200545}
546
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700547/*
548 * In this functions we calibrate APIC bus clocks to the external timer.
549 *
550 * We want to do the calibration only once since we want to have local timer
551 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
552 * frequency.
553 *
554 * This was previously done by reading the PIT/HPET and waiting for a wrap
555 * around to find out, that a tick has elapsed. I have a box, where the PIT
556 * readout is broken, so it never gets out of the wait loop again. This was
557 * also reported by others.
558 *
559 * Monitoring the jiffies value is inaccurate and the clockevents
560 * infrastructure allows us to do a simple substitution of the interrupt
561 * handler.
562 *
563 * The calibration routine also uses the pm_timer when possible, as the PIT
564 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
565 * back to normal later in the boot process).
566 */
567
568#define LAPIC_CAL_LOOPS (HZ/10)
569
570static __initdata int lapic_cal_loops = -1;
571static __initdata long lapic_cal_t1, lapic_cal_t2;
572static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
573static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
574static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
575
576/*
577 * Temporary interrupt handler.
578 */
579static void __init lapic_cal_handler(struct clock_event_device *dev)
580{
581 unsigned long long tsc = 0;
582 long tapic = apic_read(APIC_TMCCT);
583 unsigned long pm = acpi_pm_read_early();
584
585 if (cpu_has_tsc)
586 rdtscll(tsc);
587
588 switch (lapic_cal_loops++) {
589 case 0:
590 lapic_cal_t1 = tapic;
591 lapic_cal_tsc1 = tsc;
592 lapic_cal_pm1 = pm;
593 lapic_cal_j1 = jiffies;
594 break;
595
596 case LAPIC_CAL_LOOPS:
597 lapic_cal_t2 = tapic;
598 lapic_cal_tsc2 = tsc;
599 if (pm < lapic_cal_pm1)
600 pm += ACPI_PM_OVRRUN;
601 lapic_cal_pm2 = pm;
602 lapic_cal_j2 = jiffies;
603 break;
604 }
605}
606
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900607static int __init
608calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400609{
610 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
611 const long pm_thresh = pm_100ms / 100;
612 unsigned long mult;
613 u64 res;
614
615#ifndef CONFIG_X86_PM_TIMER
616 return -1;
617#endif
618
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900619 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400620
621 /* Check, if the PM timer is available */
622 if (!deltapm)
623 return -1;
624
625 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
626
627 if (deltapm > (pm_100ms - pm_thresh) &&
628 deltapm < (pm_100ms + pm_thresh)) {
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900629 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900630 return 0;
631 }
632
633 res = (((u64)deltapm) * mult) >> 22;
634 do_div(res, 1000000);
635 pr_warning("APIC calibration not consistent "
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900636 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900637
638 /* Correct the lapic counter value */
639 res = (((u64)(*delta)) * pm_100ms);
640 do_div(res, deltapm);
641 pr_info("APIC delta adjusted to PM-Timer: "
642 "%lu (%ld)\n", (unsigned long)res, *delta);
643 *delta = (long)res;
644
645 /* Correct the tsc counter value */
646 if (cpu_has_tsc) {
647 res = (((u64)(*deltatsc)) * pm_100ms);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400648 do_div(res, deltapm);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900649 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
Frans Pop3235dc32010-02-06 18:47:17 +0100650 "PM-Timer: %lu (%ld)\n",
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900651 (unsigned long)res, *deltatsc);
652 *deltatsc = (long)res;
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400653 }
654
655 return 0;
656}
657
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700658static int __init calibrate_APIC_clock(void)
659{
660 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700661 void (*real_handler)(struct clock_event_device *dev);
662 unsigned long deltaj;
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900663 long delta, deltatsc;
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700664 int pm_referenced = 0;
665
666 local_irq_disable();
667
668 /* Replace the global interrupt handler */
669 real_handler = global_clock_event->event_handler;
670 global_clock_event->event_handler = lapic_cal_handler;
671
672 /*
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400673 * Setup the APIC counter to maximum. There is no way the lapic
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700674 * can underflow in the 100ms detection time frame
675 */
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400676 __setup_APIC_LVTT(0xffffffff, 0, 0);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700677
678 /* Let the interrupts run */
679 local_irq_enable();
680
681 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
682 cpu_relax();
683
684 local_irq_disable();
685
686 /* Restore the real event handler */
687 global_clock_event->event_handler = real_handler;
688
689 /* Build delta t1-t2 as apic timer counts down */
690 delta = lapic_cal_t1 - lapic_cal_t2;
691 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
692
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900693 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
694
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400695 /* we trust the PM based calibration if possible */
696 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900697 &delta, &deltatsc);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700698
699 /* Calculate the scaled math multiplication factor */
700 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
701 lapic_clockevent.shift);
702 lapic_clockevent.max_delta_ns =
703 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
704 lapic_clockevent.min_delta_ns =
705 clockevent_delta2ns(0xF, &lapic_clockevent);
706
707 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
708
709 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
Thomas Gleixner411462f2009-11-16 11:52:39 +0100710 apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700711 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
712 calibration_result);
713
714 if (cpu_has_tsc) {
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700715 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
716 "%ld.%04ld MHz.\n",
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900717 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
718 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700719 }
720
721 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
722 "%u.%04u MHz.\n",
723 calibration_result / (1000000 / HZ),
724 calibration_result % (1000000 / HZ));
725
726 /*
727 * Do a sanity check on the APIC calibration result
728 */
729 if (calibration_result < (1000000 / HZ)) {
730 local_irq_enable();
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100731 pr_warning("APIC frequency too slow, disabling apic timer\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700732 return -1;
733 }
734
735 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
736
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400737 /*
738 * PM timer calibration failed or not turned on
739 * so lets try APIC timer based calibration
740 */
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700741 if (!pm_referenced) {
742 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
743
744 /*
745 * Setup the apic timer manually
746 */
747 levt->event_handler = lapic_cal_handler;
748 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
749 lapic_cal_loops = -1;
750
751 /* Let the interrupts run */
752 local_irq_enable();
753
754 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
755 cpu_relax();
756
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700757 /* Stop the lapic timer */
758 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
759
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700760 /* Jiffies delta */
761 deltaj = lapic_cal_j2 - lapic_cal_j1;
762 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
763
764 /* Check, if the jiffies result is consistent */
765 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
766 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
767 else
768 levt->features |= CLOCK_EVT_FEAT_DUMMY;
769 } else
770 local_irq_enable();
771
772 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +0530773 pr_warning("APIC timer disabled due to verification failure\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700774 return -1;
775 }
776
777 return 0;
778}
779
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100780/*
781 * Setup the boot APIC
782 *
783 * Calibrate and verify the result.
784 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100785void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100787 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400788 * The local apic timer can be disabled via the kernel
789 * commandline or from the CPU detection code. Register the lapic
790 * timer as a dummy clock event source on SMP systems, so the
791 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100792 */
793 if (disable_apic_timer) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100794 pr_info("Disabling APIC timer\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100795 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100796 if (num_possible_cpus() > 1) {
797 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100798 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100799 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100800 return;
801 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200802
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400803 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
804 "calibrating APIC timer ...\n");
805
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400806 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100807 /* No broadcast on UP ! */
808 if (num_possible_cpus() > 1)
809 setup_APIC_timer();
810 return;
811 }
812
813 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100814 * If nmi_watchdog is set to IO_APIC, we need the
815 * PIT/HPET going. Otherwise register lapic as a dummy
816 * device.
817 */
818 if (nmi_watchdog != NMI_IO_APIC)
819 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
820 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100821 pr_warning("APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200822 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100823
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400824 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100825 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826}
827
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100828void __cpuinit setup_secondary_APIC_clock(void)
829{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100830 setup_APIC_timer();
831}
832
833/*
834 * The guts of the apic timer interrupt
835 */
836static void local_apic_timer_interrupt(void)
837{
838 int cpu = smp_processor_id();
839 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
840
841 /*
842 * Normally we should not be here till LAPIC has been initialized but
843 * in some cases like kdump, its possible that there is a pending LAPIC
844 * timer interrupt from previous kernel's context and is delivered in
845 * new kernel the moment interrupts are enabled.
846 *
847 * Interrupts are enabled early and LAPIC is setup much later, hence
848 * its possible that when we get here evt->event_handler is NULL.
849 * Check for event_handler being NULL and discard the interrupt as
850 * spurious.
851 */
852 if (!evt->event_handler) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100853 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100854 /* Switch it off */
855 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
856 return;
857 }
858
859 /*
860 * the NMI deadlock-detector uses this.
861 */
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800862 inc_irq_stat(apic_timer_irqs);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100863
864 evt->event_handler(evt);
865}
866
867/*
868 * Local APIC timer interrupt. This is the most natural way for doing
869 * local interrupts, but local timer interrupts can be emulated by
870 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
871 *
872 * [ if a single-CPU system runs an SMP kernel then we call the local
873 * interrupt as well. Thus we cannot inline the local irq ... ]
874 */
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +0100875void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100876{
877 struct pt_regs *old_regs = set_irq_regs(regs);
878
879 /*
880 * NOTE! We'd better ACK the irq immediately,
881 * because timer handling can be slow.
882 */
883 ack_APIC_irq();
884 /*
885 * update_process_times() expects us to have done irq_enter().
886 * Besides, if we don't timer interrupts ignore the global
887 * interrupt lock, which is the WrongThing (tm) to do.
888 */
889 exit_idle();
890 irq_enter();
891 local_apic_timer_interrupt();
892 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400893
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100894 set_irq_regs(old_regs);
895}
896
897int setup_profiling_timer(unsigned int multiplier)
898{
899 return -EINVAL;
900}
901
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100902/*
903 * Local APIC start and shutdown
904 */
905
906/**
907 * clear_local_APIC - shutdown the local APIC
908 *
909 * This is called, when a CPU is disabled and before rebooting, so the state of
910 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
911 * leftovers during boot.
912 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913void clear_local_APIC(void)
914{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400915 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100916 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Andi Kleend3432892008-01-30 13:33:17 +0100918 /* APIC hasn't been mapped yet */
Suresh Siddhafc1edaf2009-04-20 13:02:27 -0700919 if (!x2apic_mode && !apic_phys)
Andi Kleend3432892008-01-30 13:33:17 +0100920 return;
921
922 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200924 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 * if the vector is zero. Mask LVTERR first to prevent this.
926 */
927 if (maxlvt >= 3) {
928 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100929 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931 /*
932 * Careful: we have to set masks only first to deassert
933 * any level-triggered sources.
934 */
935 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100936 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100938 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100940 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (maxlvt >= 4) {
942 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100943 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400946 /* lets not touch this if we didn't frob it */
Andi Kleen4efc0672009-04-28 19:07:31 +0200947#ifdef CONFIG_X86_THERMAL_VECTOR
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400948 if (maxlvt >= 5) {
949 v = apic_read(APIC_LVTTHMR);
950 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
951 }
952#endif
Andi Kleen5ca86812009-02-12 13:49:37 +0100953#ifdef CONFIG_X86_MCE_INTEL
954 if (maxlvt >= 6) {
955 v = apic_read(APIC_LVTCMCI);
956 if (!(v & APIC_LVT_MASKED))
957 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
958 }
959#endif
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 /*
962 * Clean APIC state for other OSs:
963 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100964 apic_write(APIC_LVTT, APIC_LVT_MASKED);
965 apic_write(APIC_LVT0, APIC_LVT_MASKED);
966 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100968 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100970 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400971
972 /* Integrated APIC (!82489DX) ? */
973 if (lapic_is_integrated()) {
974 if (maxlvt > 3)
975 /* Clear ESR due to Pentium errata 3AP and 11AP */
976 apic_write(APIC_ESR, 0);
977 apic_read(APIC_ESR);
978 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100981/**
982 * disable_local_APIC - clear and disable the local APIC
983 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984void disable_local_APIC(void)
985{
986 unsigned int value;
987
Jan Beulich4a13ad02009-01-14 12:28:51 +0000988 /* APIC hasn't been mapped yet */
Yinghai Lufd19dce2010-07-15 00:00:59 -0700989 if (!x2apic_mode && !apic_phys)
Jan Beulich4a13ad02009-01-14 12:28:51 +0000990 return;
991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 clear_local_APIC();
993
994 /*
995 * Disable APIC (implies clearing of registers
996 * for 82489DX!).
997 */
998 value = apic_read(APIC_SPIV);
999 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +01001000 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +04001001
1002#ifdef CONFIG_X86_32
1003 /*
1004 * When LAPIC was disabled by the BIOS and enabled by the kernel,
1005 * restore the disabled state.
1006 */
1007 if (enabled_via_apicbase) {
1008 unsigned int l, h;
1009
1010 rdmsr(MSR_IA32_APICBASE, l, h);
1011 l &= ~MSR_IA32_APICBASE_ENABLE;
1012 wrmsr(MSR_IA32_APICBASE, l, h);
1013 }
1014#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015}
1016
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +04001017/*
1018 * If Linux enabled the LAPIC against the BIOS default disable it down before
1019 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
1020 * not power-off. Additionally clear all LVT entries before disable_local_APIC
1021 * for the case where Linux didn't enable the LAPIC.
1022 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -07001023void lapic_shutdown(void)
1024{
1025 unsigned long flags;
1026
Cyrill Gorcunov83121362009-09-15 11:12:30 +04001027 if (!cpu_has_apic && !apic_from_smp_config())
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -07001028 return;
1029
1030 local_irq_save(flags);
1031
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +04001032#ifdef CONFIG_X86_32
1033 if (!enabled_via_apicbase)
1034 clear_local_APIC();
1035 else
1036#endif
1037 disable_local_APIC();
1038
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -07001039
1040 local_irq_restore(flags);
1041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/*
1044 * This is to verify that we're looking at a real local APIC.
1045 * Check these against your board if the CPUs aren't getting
1046 * started for no apparent reason.
1047 */
1048int __init verify_local_APIC(void)
1049{
1050 unsigned int reg0, reg1;
1051
1052 /*
1053 * The version register is read-only in a real APIC.
1054 */
1055 reg0 = apic_read(APIC_LVR);
1056 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
1057 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
1058 reg1 = apic_read(APIC_LVR);
1059 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
1060
1061 /*
1062 * The two version reads above should print the same
1063 * numbers. If the second one is different, then we
1064 * poke at a non-APIC.
1065 */
1066 if (reg1 != reg0)
1067 return 0;
1068
1069 /*
1070 * Check if the version looks reasonably.
1071 */
1072 reg1 = GET_APIC_VERSION(reg0);
1073 if (reg1 == 0x00 || reg1 == 0xff)
1074 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001075 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 if (reg1 < 0x02 || reg1 == 0xff)
1077 return 0;
1078
1079 /*
1080 * The ID register is read/write in a real APIC.
1081 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001082 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001084 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001085 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1087 apic_write(APIC_ID, reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001088 if (reg1 != (reg0 ^ apic->apic_id_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return 0;
1090
1091 /*
1092 * The next two are just to see if we have sane values.
1093 * They're only really relevant if we're in Virtual Wire
1094 * compatibility mode, but most boxes are anymore.
1095 */
1096 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001097 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 reg1 = apic_read(APIC_LVT1);
1099 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1100
1101 return 1;
1102}
1103
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001104/**
1105 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107void __init sync_Arb_IDs(void)
1108{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +02001109 /*
1110 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1111 * needed on AMD.
1112 */
1113 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 return;
1115
1116 /*
1117 * Wait for idle.
1118 */
1119 apic_wait_icr_idle();
1120
1121 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +04001122 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1123 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126/*
1127 * An initial setup of the virtual wire mode.
1128 */
1129void __init init_bsp_APIC(void)
1130{
Andi Kleen11a8e772006-01-11 22:46:51 +01001131 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 /*
1134 * Don't do the setup now if we have a SMP BIOS as the
1135 * through-I/O-APIC virtual wire mode might be active.
1136 */
1137 if (smp_found_config || !cpu_has_apic)
1138 return;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /*
1141 * Do not trust the local APIC being empty at bootup.
1142 */
1143 clear_local_APIC();
1144
1145 /*
1146 * Enable APIC.
1147 */
1148 value = apic_read(APIC_SPIV);
1149 value &= ~APIC_VECTOR_MASK;
1150 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001151
1152#ifdef CONFIG_X86_32
1153 /* This bit is reserved on P4/Xeon and should be cleared */
1154 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1155 (boot_cpu_data.x86 == 15))
1156 value &= ~APIC_SPIV_FOCUS_DISABLED;
1157 else
1158#endif
1159 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001161 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 /*
1164 * Set up the virtual wire mode.
1165 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001166 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001168 if (!lapic_is_integrated()) /* 82489DX */
1169 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001170 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171}
1172
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001173static void __cpuinit lapic_setup_esr(void)
1174{
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001175 unsigned int oldvalue, value, maxlvt;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001176
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001177 if (!lapic_is_integrated()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001178 pr_info("No ESR for 82489DX.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001179 return;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001180 }
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001181
Ingo Molnar08125d32009-01-28 05:08:44 +01001182 if (apic->disable_esr) {
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001183 /*
1184 * Something untraceable is creating bad interrupts on
1185 * secondary quads ... for the moment, just leave the
1186 * ESR disabled - we can't do anything useful with the
1187 * errors anyway - mbligh
1188 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001189 pr_info("Leaving ESR disabled.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001190 return;
1191 }
1192
1193 maxlvt = lapic_get_maxlvt();
1194 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1195 apic_write(APIC_ESR, 0);
1196 oldvalue = apic_read(APIC_ESR);
1197
1198 /* enables sending errors */
1199 value = ERROR_APIC_VECTOR;
1200 apic_write(APIC_LVTERR, value);
1201
1202 /*
1203 * spec says clear errors after enabling vector.
1204 */
1205 if (maxlvt > 3)
1206 apic_write(APIC_ESR, 0);
1207 value = apic_read(APIC_ESR);
1208 if (value != oldvalue)
1209 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1210 "vector: 0x%08x after: 0x%08x\n",
1211 oldvalue, value);
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001212}
1213
1214
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001215/**
1216 * setup_local_APIC - setup the local APIC
1217 */
1218void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001220 unsigned int value, queued;
1221 int i, j, acked = 0;
1222 unsigned long long tsc = 0, ntsc;
1223 long long max_loops = cpu_khz;
1224
1225 if (cpu_has_tsc)
1226 rdtscll(tsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Jan Beulichf1182632009-01-14 12:27:35 +00001228 if (disable_apic) {
Ingo Molnar65a4e572009-01-31 03:36:17 +01001229 arch_disable_smp_support();
Jan Beulichf1182632009-01-14 12:27:35 +00001230 return;
1231 }
1232
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001233#ifdef CONFIG_X86_32
1234 /* Pound the ESR really hard over the head with a big hammer - mbligh */
Ingo Molnar08125d32009-01-28 05:08:44 +01001235 if (lapic_is_integrated() && apic->disable_esr) {
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001236 apic_write(APIC_ESR, 0);
1237 apic_write(APIC_ESR, 0);
1238 apic_write(APIC_ESR, 0);
1239 apic_write(APIC_ESR, 0);
1240 }
1241#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001242 perf_events_lapic_init();
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001243
Jack Steinerac23d4e2008-03-28 14:12:16 -05001244 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 /*
1247 * Double-check whether this APIC is really registered.
1248 * This is meaningless in clustered apic mode, so we skip it.
1249 */
Daniel Walkerc2777f92009-09-12 10:40:20 -07001250 BUG_ON(!apic->apic_id_registered());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /*
1253 * Intel recommends to set DFR, LDR and TPR before enabling
1254 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1255 * document number 292116). So here it goes...
1256 */
Ingo Molnara5c43292009-01-28 06:50:47 +01001257 apic->init_apic_ldr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /*
1260 * Set Task Priority to 'accept all'. We never change this
1261 * later on.
1262 */
1263 value = apic_read(APIC_TASKPRI);
1264 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +01001265 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
1267 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001268 * After a crash, we no longer service the interrupts and a pending
1269 * interrupt from previous kernel might still have ISR bit set.
1270 *
1271 * Most probably by now CPU has serviced that pending interrupt and
1272 * it might not have done the ack_APIC_irq() because it thought,
1273 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1274 * does not clear the ISR bit and cpu thinks it has already serivced
1275 * the interrupt. Hence a vector might get locked. It was noticed
1276 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1277 */
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001278 do {
1279 queued = 0;
1280 for (i = APIC_ISR_NR - 1; i >= 0; i--)
1281 queued |= apic_read(APIC_IRR + i*0x10);
1282
1283 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1284 value = apic_read(APIC_ISR + i*0x10);
1285 for (j = 31; j >= 0; j--) {
1286 if (value & (1<<j)) {
1287 ack_APIC_irq();
1288 acked++;
1289 }
1290 }
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001291 }
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001292 if (acked > 256) {
1293 printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
1294 acked);
1295 break;
1296 }
1297 if (cpu_has_tsc) {
1298 rdtscll(ntsc);
1299 max_loops = (cpu_khz << 10) - (ntsc - tsc);
1300 } else
1301 max_loops--;
1302 } while (queued && max_loops > 0);
1303 WARN_ON(max_loops <= 0);
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001304
1305 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 * Now that we are all set up, enable the APIC
1307 */
1308 value = apic_read(APIC_SPIV);
1309 value &= ~APIC_VECTOR_MASK;
1310 /*
1311 * Enable APIC
1312 */
1313 value |= APIC_SPIV_APIC_ENABLED;
1314
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001315#ifdef CONFIG_X86_32
1316 /*
1317 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1318 * certain networking cards. If high frequency interrupts are
1319 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1320 * entry is masked/unmasked at a high rate as well then sooner or
1321 * later IOAPIC line gets 'stuck', no more interrupts are received
1322 * from the device. If focus CPU is disabled then the hang goes
1323 * away, oh well :-(
1324 *
1325 * [ This bug can be reproduced easily with a level-triggered
1326 * PCI Ne2000 networking cards and PII/PIII processors, dual
1327 * BX chipset. ]
1328 */
1329 /*
1330 * Actually disabling the focus CPU check just makes the hang less
1331 * frequent as it makes the interrupt distributon model be more
1332 * like LRU than MRU (the short-term load is more even across CPUs).
1333 * See also the comment in end_level_ioapic_irq(). --macro
1334 */
1335
1336 /*
1337 * - enable focus processor (bit==0)
1338 * - 64bit mode always use processor focus
1339 * so no need to set it
1340 */
1341 value &= ~APIC_SPIV_FOCUS_DISABLED;
1342#endif
Andi Kleen3f14c742006-09-26 10:52:29 +02001343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 /*
1345 * Set spurious IRQ vector
1346 */
1347 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001348 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 /*
1351 * Set up LVT0, LVT1:
1352 *
1353 * set up through-local-APIC on the BP's LINT0. This is not
1354 * strictly necessary in pure symmetric-IO mode, but sometimes
1355 * we delegate interrupts to the 8259A.
1356 */
1357 /*
1358 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1359 */
1360 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001361 if (!smp_processor_id() && (pic_mode || !value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001363 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001364 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 } else {
1366 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001367 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001368 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001370 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 /*
1373 * only the BP should see the LINT1 NMI signal, obviously.
1374 */
1375 if (!smp_processor_id())
1376 value = APIC_DM_NMI;
1377 else
1378 value = APIC_DM_NMI | APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001379 if (!lapic_is_integrated()) /* 82489DX */
1380 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001381 apic_write(APIC_LVT1, value);
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001382
Jack Steinerac23d4e2008-03-28 14:12:16 -05001383 preempt_enable();
Andi Kleenbe71b852009-02-12 13:49:38 +01001384
1385#ifdef CONFIG_X86_MCE_INTEL
1386 /* Recheck CMCI information after local APIC is up on CPU #0 */
1387 if (smp_processor_id() == 0)
1388 cmci_recheck();
1389#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001390}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Andi Kleen739f33b2008-01-30 13:30:40 +01001392void __cpuinit end_local_APIC_setup(void)
1393{
1394 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001395
1396#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001397 {
1398 unsigned int value;
1399 /* Disable the local apic timer */
1400 value = apic_read(APIC_LVTT);
1401 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1402 apic_write(APIC_LVTT, value);
1403 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001404#endif
1405
Don Zickusf2802e72006-09-26 10:52:26 +02001406 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 apic_pm_activate();
1408}
1409
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001410#ifdef CONFIG_X86_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001411void check_x2apic(void)
1412{
Suresh Siddhaef1f87a2009-02-21 14:23:21 -08001413 if (x2apic_enabled()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001414 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001415 x2apic_preenabled = x2apic_mode = 1;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001416 }
1417}
1418
1419void enable_x2apic(void)
1420{
1421 int msr, msr2;
1422
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001423 if (!x2apic_mode)
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001424 return;
1425
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001426 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1427 if (!(msr & X2APIC_ENABLE)) {
Mike Travis450b1e82009-12-11 08:08:50 -08001428 printk_once(KERN_INFO "Enabling x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001429 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1430 }
1431}
Weidong Han93758232009-04-17 16:42:14 +08001432#endif /* CONFIG_X86_X2APIC */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001433
Gleb Natapovce69a782009-07-20 15:24:17 +03001434int __init enable_IR(void)
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001435{
1436#ifdef CONFIG_INTR_REMAP
Weidong Han93758232009-04-17 16:42:14 +08001437 if (!intr_remapping_supported()) {
1438 pr_debug("intr-remapping not supported\n");
Gleb Natapovce69a782009-07-20 15:24:17 +03001439 return 0;
Weidong Han93758232009-04-17 16:42:14 +08001440 }
1441
Weidong Han93758232009-04-17 16:42:14 +08001442 if (!x2apic_preenabled && skip_ioapic_setup) {
1443 pr_info("Skipped enabling intr-remap because of skipping "
1444 "io-apic setup\n");
Gleb Natapovce69a782009-07-20 15:24:17 +03001445 return 0;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001446 }
1447
Gleb Natapovce69a782009-07-20 15:24:17 +03001448 if (enable_intr_remapping(x2apic_supported()))
1449 return 0;
1450
1451 pr_info("Enabled Interrupt-remapping\n");
1452
1453 return 1;
1454
1455#endif
1456 return 0;
1457}
1458
1459void __init enable_IR_x2apic(void)
1460{
1461 unsigned long flags;
1462 struct IO_APIC_route_entry **ioapic_entries = NULL;
1463 int ret, x2apic_enabled = 0;
Yinghai Lue6707612009-11-21 00:23:37 -08001464 int dmar_table_init_ret;
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001465
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001466 dmar_table_init_ret = dmar_table_init();
Yinghai Lue6707612009-11-21 00:23:37 -08001467 if (dmar_table_init_ret && !x2apic_supported())
1468 return;
Gleb Natapovce69a782009-07-20 15:24:17 +03001469
Fenghua Yub24696b2009-03-27 14:22:44 -07001470 ioapic_entries = alloc_ioapic_entries();
1471 if (!ioapic_entries) {
Gleb Natapovce69a782009-07-20 15:24:17 +03001472 pr_err("Allocate ioapic_entries failed\n");
1473 goto out;
Fenghua Yub24696b2009-03-27 14:22:44 -07001474 }
1475
1476 ret = save_IO_APIC_setup(ioapic_entries);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001477 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001478 pr_info("Saving IO-APIC state failed: %d\n", ret);
Gleb Natapovce69a782009-07-20 15:24:17 +03001479 goto out;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001480 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001481
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001482 local_irq_save(flags);
Jacob Panb81bb372009-11-09 11:27:04 -08001483 legacy_pic->mask_all();
Gleb Natapovce69a782009-07-20 15:24:17 +03001484 mask_IO_APIC_setup(ioapic_entries);
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001485
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001486 if (dmar_table_init_ret)
1487 ret = 0;
1488 else
1489 ret = enable_IR();
1490
Gleb Natapovce69a782009-07-20 15:24:17 +03001491 if (!ret) {
1492 /* IR is required if there is APIC ID > 255 even when running
1493 * under KVM
1494 */
1495 if (max_physical_apicid > 255 || !kvm_para_available())
1496 goto nox2apic;
1497 /*
1498 * without IR all CPUs can be addressed by IOAPIC/MSI
1499 * only in physical mode
1500 */
1501 x2apic_force_phys();
1502 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001503
Gleb Natapovce69a782009-07-20 15:24:17 +03001504 x2apic_enabled = 1;
Weidong Han93758232009-04-17 16:42:14 +08001505
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001506 if (x2apic_supported() && !x2apic_mode) {
1507 x2apic_mode = 1;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001508 enable_x2apic();
Weidong Han93758232009-04-17 16:42:14 +08001509 pr_info("Enabled x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001510 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001511
Gleb Natapovce69a782009-07-20 15:24:17 +03001512nox2apic:
1513 if (!ret) /* IR enabling failed */
Fenghua Yub24696b2009-03-27 14:22:44 -07001514 restore_IO_APIC_setup(ioapic_entries);
Jacob Panb81bb372009-11-09 11:27:04 -08001515 legacy_pic->restore_mask();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001516 local_irq_restore(flags);
1517
Gleb Natapovce69a782009-07-20 15:24:17 +03001518out:
Fenghua Yub24696b2009-03-27 14:22:44 -07001519 if (ioapic_entries)
1520 free_ioapic_entries(ioapic_entries);
Weidong Han93758232009-04-17 16:42:14 +08001521
Gleb Natapovce69a782009-07-20 15:24:17 +03001522 if (x2apic_enabled)
Weidong Han93758232009-04-17 16:42:14 +08001523 return;
1524
Weidong Han93758232009-04-17 16:42:14 +08001525 if (x2apic_preenabled)
Gleb Natapovce69a782009-07-20 15:24:17 +03001526 panic("x2apic: enabled by BIOS but kernel init failed.");
Weidong Han93758232009-04-17 16:42:14 +08001527 else if (cpu_has_x2apic)
Gleb Natapovce69a782009-07-20 15:24:17 +03001528 pr_info("Not enabling x2apic, Intr-remapping init failed.\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001529}
Weidong Han93758232009-04-17 16:42:14 +08001530
Yinghai Lube7a6562008-08-24 02:01:51 -07001531#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001532/*
1533 * Detect and enable local APICs on non-SMP boards.
1534 * Original code written by Keir Fraser.
1535 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1536 * not correctly set up (usually the APIC timer won't work etc.)
1537 */
1538static int __init detect_init_APIC(void)
1539{
1540 if (!cpu_has_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001541 pr_info("No local APIC present\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001542 return -1;
1543 }
1544
1545 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001546 return 0;
1547}
Yinghai Lube7a6562008-08-24 02:01:51 -07001548#else
1549/*
1550 * Detect and initialize APIC
1551 */
1552static int __init detect_init_APIC(void)
1553{
1554 u32 h, l, features;
1555
1556 /* Disabled by kernel option? */
1557 if (disable_apic)
1558 return -1;
1559
1560 switch (boot_cpu_data.x86_vendor) {
1561 case X86_VENDOR_AMD:
1562 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
Borislav Petkov85877062009-02-03 16:24:22 +01001563 (boot_cpu_data.x86 >= 15))
Yinghai Lube7a6562008-08-24 02:01:51 -07001564 break;
1565 goto no_apic;
1566 case X86_VENDOR_INTEL:
1567 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1568 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1569 break;
1570 goto no_apic;
1571 default:
1572 goto no_apic;
1573 }
1574
1575 if (!cpu_has_apic) {
1576 /*
1577 * Over-ride BIOS and try to enable the local APIC only if
1578 * "lapic" specified.
1579 */
1580 if (!force_enable_local_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001581 pr_info("Local APIC disabled by BIOS -- "
1582 "you can enable it with \"lapic\"\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001583 return -1;
1584 }
1585 /*
1586 * Some BIOSes disable the local APIC in the APIC_BASE
1587 * MSR. This can only be done in software for Intel P6 or later
1588 * and AMD K7 (Model > 1) or later.
1589 */
1590 rdmsr(MSR_IA32_APICBASE, l, h);
1591 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001592 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001593 l &= ~MSR_IA32_APICBASE_BASE;
1594 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1595 wrmsr(MSR_IA32_APICBASE, l, h);
1596 enabled_via_apicbase = 1;
1597 }
1598 }
1599 /*
1600 * The APIC feature bit should now be enabled
1601 * in `cpuid'
1602 */
1603 features = cpuid_edx(1);
1604 if (!(features & (1 << X86_FEATURE_APIC))) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001605 pr_warning("Could not enable APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001606 return -1;
1607 }
1608 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1609 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1610
1611 /* The BIOS may have set up the APIC at some other address */
1612 rdmsr(MSR_IA32_APICBASE, l, h);
1613 if (l & MSR_IA32_APICBASE_ENABLE)
1614 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1615
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001616 pr_info("Found and enabled local APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001617
1618 apic_pm_activate();
1619
1620 return 0;
1621
1622no_apic:
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001623 pr_info("No local APIC present or hardware disabled\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001624 return -1;
1625}
1626#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001627
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001628#ifdef CONFIG_X86_64
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001629void __init early_init_lapic_mapping(void)
1630{
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001631 /*
1632 * If no local APIC can be found then go out
1633 * : it means there is no mpatable and MADT
1634 */
1635 if (!smp_found_config)
1636 return;
1637
Cyrill Gorcunovd3a247b2009-08-26 21:13:24 +04001638 set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001639 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Cyrill Gorcunovd3a247b2009-08-26 21:13:24 +04001640 APIC_BASE, mp_lapic_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001641
1642 /*
1643 * Fetch the APIC ID of the BSP in case we have a
1644 * default configuration (or the MP table is broken).
1645 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001646 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001647}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001648#endif
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001649
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001650/**
1651 * init_apic_mappings - initialize APIC mappings
1652 */
1653void __init init_apic_mappings(void)
1654{
Yinghai Lu4401da62009-05-02 10:40:57 -07001655 unsigned int new_apicid;
1656
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001657 if (x2apic_mode) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001658 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001659 return;
1660 }
1661
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001662 /* If no local APIC can be found return early */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001663 if (!smp_found_config && detect_init_APIC()) {
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001664 /* lets NOP'ify apic operations */
Cyrill Gorcunovcec6be62009-05-11 17:41:40 +04001665 pr_info("APIC: disable apic facility\n");
1666 apic_disable();
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001667 } else {
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001668 apic_phys = mp_lapic_addr;
1669
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001670 /*
1671 * acpi lapic path already maps that address in
1672 * acpi_register_lapic_address()
1673 */
Eric W. Biederman5989cd62010-08-04 13:30:27 -07001674 if (!acpi_lapic && !smp_found_config)
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001675 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1676
1677 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
1678 APIC_BASE, apic_phys);
Cyrill Gorcunovcec6be62009-05-11 17:41:40 +04001679 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001680
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001681 /*
1682 * Fetch the APIC ID of the BSP in case we have a
1683 * default configuration (or the MP table is broken).
1684 */
Yinghai Lu4401da62009-05-02 10:40:57 -07001685 new_apicid = read_apic_id();
1686 if (boot_cpu_physical_apicid != new_apicid) {
1687 boot_cpu_physical_apicid = new_apicid;
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001688 /*
1689 * yeah -- we lie about apic_version
1690 * in case if apic was disabled via boot option
1691 * but it's not a problem for SMP compiled kernel
1692 * since smp_sanity_check is prepared for such a case
1693 * and disable smp mode
1694 */
Yinghai Lu4401da62009-05-02 10:40:57 -07001695 apic_version[new_apicid] =
1696 GET_APIC_VERSION(apic_read(APIC_LVR));
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +04001697 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001698}
1699
1700/*
1701 * This initializes the IO-APIC and APIC hardware if this is
1702 * a UP kernel.
1703 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001704int apic_version[MAX_APICS];
1705
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001706int __init APIC_init_uniprocessor(void)
1707{
1708 if (disable_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001709 pr_info("Apic disabled\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001710 return -1;
1711 }
Jan Beulichf1182632009-01-14 12:27:35 +00001712#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001713 if (!cpu_has_apic) {
1714 disable_apic = 1;
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001715 pr_info("Apic disabled by BIOS\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001716 return -1;
1717 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001718#else
1719 if (!smp_found_config && !cpu_has_apic)
1720 return -1;
1721
1722 /*
1723 * Complain if the BIOS pretends there is one.
1724 */
1725 if (!cpu_has_apic &&
1726 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001727 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1728 boot_cpu_physical_apicid);
Yinghai Lufa2bd352008-08-24 02:01:50 -07001729 return -1;
1730 }
1731#endif
1732
Ingo Molnar72ce0162009-01-28 06:50:47 +01001733 default_setup_apic_routing();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001734
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001735 verify_local_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001736 connect_bsp_APIC();
1737
Yinghai Lufa2bd352008-08-24 02:01:50 -07001738#ifdef CONFIG_X86_64
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001739 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Yinghai Lufa2bd352008-08-24 02:01:50 -07001740#else
1741 /*
1742 * Hack: In case of kdump, after a crash, kernel might be booting
1743 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1744 * might be zero if read from MP tables. Get it from LAPIC.
1745 */
1746# ifdef CONFIG_CRASH_DUMP
1747 boot_cpu_physical_apicid = read_apic_id();
1748# endif
1749#endif
1750 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001751 setup_local_APIC();
1752
Yinghai Lu88d0f552009-02-14 23:57:28 -08001753#ifdef CONFIG_X86_IO_APIC
Andi Kleen739f33b2008-01-30 13:30:40 +01001754 /*
1755 * Now enable IO-APICs, actually call clear_IO_APIC
Yinghai Lu98c061b2009-02-16 00:00:50 -08001756 * We need clear_IO_APIC before enabling error vector
Andi Kleen739f33b2008-01-30 13:30:40 +01001757 */
1758 if (!skip_ioapic_setup && nr_ioapics)
1759 enable_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001760#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001761
1762 end_local_APIC_setup();
1763
Yinghai Lufa2bd352008-08-24 02:01:50 -07001764#ifdef CONFIG_X86_IO_APIC
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001765 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1766 setup_IO_APIC();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001767 else {
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001768 nr_ioapics = 0;
Yinghai Lu98c061b2009-02-16 00:00:50 -08001769 localise_nmi_watchdog();
1770 }
1771#else
1772 localise_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001773#endif
1774
Thomas Gleixner736deca2009-08-19 12:35:53 +02001775 x86_init.timers.setup_percpu_clockev();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001776#ifdef CONFIG_X86_64
1777 check_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001778#endif
1779
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001780 return 0;
1781}
1782
1783/*
1784 * Local APIC interrupts
1785 */
1786
1787/*
1788 * This interrupt should _never_ happen with our APIC/SMP architecture
1789 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001790void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001791{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001792 u32 v;
1793
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001794 exit_idle();
1795 irq_enter();
1796 /*
1797 * Check if this really is a spurious interrupt and ACK it
1798 * if it is a vectored one. Just in case...
1799 * Spurious interrupts should not be ACKed.
1800 */
1801 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1802 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1803 ack_APIC_irq();
1804
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -08001805 inc_irq_stat(irq_spurious_count);
1806
Yinghai Ludc1528d2008-08-24 02:01:53 -07001807 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001808 pr_info("spurious APIC interrupt on CPU#%d, "
1809 "should never happen.\n", smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001810 irq_exit();
1811}
1812
1813/*
1814 * This interrupt should never happen with our APIC/SMP architecture
1815 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001816void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001817{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001818 u32 v, v1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001819
1820 exit_idle();
1821 irq_enter();
1822 /* First tickle the hardware, only then report what went on. -- REW */
1823 v = apic_read(APIC_ESR);
1824 apic_write(APIC_ESR, 0);
1825 v1 = apic_read(APIC_ESR);
1826 ack_APIC_irq();
1827 atomic_inc(&irq_err_count);
1828
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001829 /*
1830 * Here is what the APIC error bits mean:
1831 * 0: Send CS error
1832 * 1: Receive CS error
1833 * 2: Send accept error
1834 * 3: Receive accept error
1835 * 4: Reserved
1836 * 5: Send illegal vector
1837 * 6: Received illegal vector
1838 * 7: Illegal register address
1839 */
1840 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001841 smp_processor_id(), v , v1);
1842 irq_exit();
1843}
1844
Glauber Costab5841762008-05-28 13:38:28 -03001845/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001846 * connect_bsp_APIC - attach the APIC to the interrupt system
1847 */
Glauber Costab5841762008-05-28 13:38:28 -03001848void __init connect_bsp_APIC(void)
1849{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001850#ifdef CONFIG_X86_32
1851 if (pic_mode) {
1852 /*
1853 * Do not trust the local APIC being empty at bootup.
1854 */
1855 clear_local_APIC();
1856 /*
1857 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1858 * local APIC to INT and NMI lines.
1859 */
1860 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1861 "enabling APIC mode.\n");
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +04001862 imcr_pic_to_apic();
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001863 }
1864#endif
Ingo Molnar49040332009-01-28 12:43:18 +01001865 if (apic->enable_apic_mode)
1866 apic->enable_apic_mode();
Glauber Costab5841762008-05-28 13:38:28 -03001867}
1868
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001869/**
1870 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1871 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1872 *
1873 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1874 * APIC is disabled.
1875 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001876void disconnect_bsp_APIC(int virt_wire_setup)
1877{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001878 unsigned int value;
1879
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001880#ifdef CONFIG_X86_32
1881 if (pic_mode) {
1882 /*
1883 * Put the board back into PIC mode (has an effect only on
1884 * certain older boards). Note that APIC interrupts, including
1885 * IPIs, won't work beyond this point! The only exception are
1886 * INIT IPIs.
1887 */
1888 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1889 "entering PIC mode.\n");
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +04001890 imcr_apic_to_pic();
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001891 return;
1892 }
1893#endif
1894
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001895 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001896
1897 /* For the spurious interrupt use vector F, and enable it */
1898 value = apic_read(APIC_SPIV);
1899 value &= ~APIC_VECTOR_MASK;
1900 value |= APIC_SPIV_APIC_ENABLED;
1901 value |= 0xf;
1902 apic_write(APIC_SPIV, value);
1903
1904 if (!virt_wire_setup) {
1905 /*
1906 * For LVT0 make it edge triggered, active high,
1907 * external and enabled
1908 */
1909 value = apic_read(APIC_LVT0);
1910 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1911 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1912 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1913 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1914 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1915 apic_write(APIC_LVT0, value);
1916 } else {
1917 /* Disable LVT0 */
1918 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1919 }
1920
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001921 /*
1922 * For LVT1 make it edge triggered, active high,
1923 * nmi and enabled
1924 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001925 value = apic_read(APIC_LVT1);
1926 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1927 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1928 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1929 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1930 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1931 apic_write(APIC_LVT1, value);
1932}
1933
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001934void __cpuinit generic_processor_info(int apicid, int version)
1935{
1936 int cpu;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001937
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001938 /*
1939 * Validate version
1940 */
1941 if (version == 0x0) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001942 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
Mike Travis3b11ce72008-12-17 15:21:39 -08001943 "fixing up to 0x10. (tell your hw vendor)\n",
1944 version);
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001945 version = 0x10;
1946 }
1947 apic_version[apicid] = version;
1948
Mike Travis3b11ce72008-12-17 15:21:39 -08001949 if (num_processors >= nr_cpu_ids) {
1950 int max = nr_cpu_ids;
1951 int thiscpu = max + disabled_cpus;
1952
1953 pr_warning(
1954 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1955 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1956
1957 disabled_cpus++;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001958 return;
1959 }
1960
1961 num_processors++;
Mike Travis3b11ce72008-12-17 15:21:39 -08001962 cpu = cpumask_next_zero(-1, cpu_present_mask);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001963
Mike Travisb2b815d2009-01-16 15:22:16 -08001964 if (version != apic_version[boot_cpu_physical_apicid])
1965 WARN_ONCE(1,
1966 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1967 apic_version[boot_cpu_physical_apicid], cpu, version);
1968
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001969 physid_set(apicid, phys_cpu_present_map);
1970 if (apicid == boot_cpu_physical_apicid) {
1971 /*
1972 * x86_bios_cpu_apicid is required to have processors listed
1973 * in same order as logical cpu numbers. Hence the first
1974 * entry is BSP, and so on.
1975 */
1976 cpu = 0;
1977 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001978 if (apicid > max_physical_apicid)
1979 max_physical_apicid = apicid;
1980
Ingo Molnar3e5095d2009-01-27 17:07:08 +01001981#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
Tejun Heof10fcd42009-01-13 20:41:34 +09001982 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1983 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001984#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001985
Mike Travis1de88cd2008-12-16 17:34:02 -08001986 set_cpu_possible(cpu, true);
1987 set_cpu_present(cpu, true);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001988}
1989
Suresh Siddha0c81c742008-07-10 11:16:48 -07001990int hard_smp_processor_id(void)
1991{
1992 return read_apic_id();
1993}
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +01001994
1995void default_init_apic_ldr(void)
1996{
1997 unsigned long val;
1998
1999 apic_write(APIC_DFR, APIC_DFR_VALUE);
2000 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
2001 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
2002 apic_write(APIC_LDR, val);
2003}
2004
2005#ifdef CONFIG_X86_32
2006int default_apicid_to_node(int logical_apicid)
2007{
2008#ifdef CONFIG_SMP
2009 return apicid_2_node[hard_smp_processor_id()];
2010#else
2011 return 0;
2012#endif
2013}
Yinghai Lu34919982008-08-24 02:01:48 -07002014#endif
Suresh Siddha0c81c742008-07-10 11:16:48 -07002015
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002016/*
2017 * Power management
2018 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019#ifdef CONFIG_PM
2020
2021static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002022 /*
2023 * 'active' is true if the local APIC was enabled by us and
2024 * not the BIOS; this signifies that we are also responsible
2025 * for disabling it before entering apm/acpi suspend
2026 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 int active;
2028 /* r/w apic fields */
2029 unsigned int apic_id;
2030 unsigned int apic_taskpri;
2031 unsigned int apic_ldr;
2032 unsigned int apic_dfr;
2033 unsigned int apic_spiv;
2034 unsigned int apic_lvtt;
2035 unsigned int apic_lvtpc;
2036 unsigned int apic_lvt0;
2037 unsigned int apic_lvt1;
2038 unsigned int apic_lvterr;
2039 unsigned int apic_tmict;
2040 unsigned int apic_tdcr;
2041 unsigned int apic_thmr;
2042} apic_pm_state;
2043
Pavel Machek0b9c33a2005-04-16 15:25:31 -07002044static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
2046 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002047 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048
2049 if (!apic_pm_state.active)
2050 return 0;
2051
Thomas Gleixner37e650c2008-01-30 13:30:14 +01002052 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01002053
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07002054 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2056 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2057 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2058 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2059 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01002060 if (maxlvt >= 4)
2061 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2063 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2064 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2065 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2066 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Andi Kleen4efc0672009-04-28 19:07:31 +02002067#ifdef CONFIG_X86_THERMAL_VECTOR
Karsten Wiesef990fff2006-12-07 02:14:11 +01002068 if (maxlvt >= 5)
2069 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2070#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04002071
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02002072 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 disable_local_APIC();
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002074
Fenghua Yub24696b2009-03-27 14:22:44 -07002075 if (intr_remapping_enabled)
2076 disable_intr_remapping();
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 local_irq_restore(flags);
2079 return 0;
2080}
2081
2082static int lapic_resume(struct sys_device *dev)
2083{
2084 unsigned int l, h;
2085 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002086 int maxlvt;
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002087 int ret = 0;
Fenghua Yub24696b2009-03-27 14:22:44 -07002088 struct IO_APIC_route_entry **ioapic_entries = NULL;
2089
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (!apic_pm_state.active)
2091 return 0;
2092
Fenghua Yub24696b2009-03-27 14:22:44 -07002093 local_irq_save(flags);
Weidong Han9a2755c2009-04-17 16:42:16 +08002094 if (intr_remapping_enabled) {
Fenghua Yub24696b2009-03-27 14:22:44 -07002095 ioapic_entries = alloc_ioapic_entries();
2096 if (!ioapic_entries) {
2097 WARN(1, "Alloc ioapic_entries in lapic resume failed.");
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002098 ret = -ENOMEM;
2099 goto restore;
Fenghua Yub24696b2009-03-27 14:22:44 -07002100 }
2101
2102 ret = save_IO_APIC_setup(ioapic_entries);
2103 if (ret) {
2104 WARN(1, "Saving IO-APIC state failed: %d\n", ret);
2105 free_ioapic_entries(ioapic_entries);
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002106 goto restore;
Fenghua Yub24696b2009-03-27 14:22:44 -07002107 }
2108
2109 mask_IO_APIC_setup(ioapic_entries);
Jacob Panb81bb372009-11-09 11:27:04 -08002110 legacy_pic->mask_all();
Fenghua Yub24696b2009-03-27 14:22:44 -07002111 }
Karsten Wiesef990fff2006-12-07 02:14:11 +01002112
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002113 if (x2apic_mode)
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002114 enable_x2apic();
Suresh Siddhacf6567f2009-03-16 17:05:00 -07002115 else {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002116 /*
2117 * Make sure the APICBASE points to the right address
2118 *
2119 * FIXME! This will be wrong if we ever support suspend on
2120 * SMP! We'll need to do this as part of the CPU restore!
2121 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002122 rdmsr(MSR_IA32_APICBASE, l, h);
2123 l &= ~MSR_IA32_APICBASE_BASE;
2124 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2125 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07002126 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002127
Fenghua Yub24696b2009-03-27 14:22:44 -07002128 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2130 apic_write(APIC_ID, apic_pm_state.apic_id);
2131 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2132 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2133 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2134 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2135 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2136 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002137#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002138 if (maxlvt >= 5)
2139 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2140#endif
2141 if (maxlvt >= 4)
2142 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2144 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2145 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2146 apic_write(APIC_ESR, 0);
2147 apic_read(APIC_ESR);
2148 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2149 apic_write(APIC_ESR, 0);
2150 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002151
Weidong Han9a2755c2009-04-17 16:42:16 +08002152 if (intr_remapping_enabled) {
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002153 reenable_intr_remapping(x2apic_mode);
Jacob Panb81bb372009-11-09 11:27:04 -08002154 legacy_pic->restore_mask();
Fenghua Yub24696b2009-03-27 14:22:44 -07002155 restore_IO_APIC_setup(ioapic_entries);
2156 free_ioapic_entries(ioapic_entries);
2157 }
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002158restore:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002160
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002161 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162}
2163
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002164/*
2165 * This device has no shutdown method - fully functioning local APICs
2166 * are needed on every CPU up until machine_halt/restart/poweroff.
2167 */
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002170 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 .resume = lapic_resume,
2172 .suspend = lapic_suspend,
2173};
2174
2175static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002176 .id = 0,
2177 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178};
2179
Ashok Raje6982c62005-06-25 14:54:58 -07002180static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181{
2182 apic_pm_state.active = 1;
2183}
2184
2185static int __init init_lapic_sysfs(void)
2186{
2187 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 if (!cpu_has_apic)
2190 return 0;
2191 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 error = sysdev_class_register(&lapic_sysclass);
2194 if (!error)
2195 error = sysdev_register(&device_lapic);
2196 return error;
2197}
Fenghua Yub24696b2009-03-27 14:22:44 -07002198
2199/* local apic needs to resume before other devices access its registers. */
2200core_initcall(init_lapic_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
2202#else /* CONFIG_PM */
2203
2204static void apic_pm_activate(void) { }
2205
2206#endif /* CONFIG_PM */
2207
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002208#ifdef CONFIG_X86_64
Yinghai Lue0e42142009-04-26 23:39:38 -07002209
2210static int __cpuinit apic_cluster_num(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 int i, clusters, zeros;
2213 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08002214 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2216
Mike Travis23ca4bb2008-05-12 21:21:12 +02002217 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07002218 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
Mike Travis168ef542008-12-16 17:34:01 -08002220 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002221 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01002222 if (bios_cpu_apicid) {
2223 id = bios_cpu_apicid[i];
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302224 } else if (i < nr_cpu_ids) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002225 if (cpu_present(i))
2226 id = per_cpu(x86_bios_cpu_apicid, i);
2227 else
2228 continue;
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302229 } else
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002230 break;
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 if (id != BAD_APICID)
2233 __set_bit(APIC_CLUSTERID(id), clustermap);
2234 }
2235
2236 /* Problem: Partially populated chassis may not have CPUs in some of
2237 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01002238 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2239 * Since clusters are allocated sequentially, count zeros only if
2240 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 */
2242 clusters = 0;
2243 zeros = 0;
2244 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2245 if (test_bit(i, clustermap)) {
2246 clusters += 1 + zeros;
2247 zeros = 0;
2248 } else
2249 ++zeros;
2250 }
2251
Yinghai Lue0e42142009-04-26 23:39:38 -07002252 return clusters;
2253}
2254
2255static int __cpuinitdata multi_checked;
2256static int __cpuinitdata multi;
2257
2258static int __cpuinit set_multi(const struct dmi_system_id *d)
2259{
2260 if (multi)
2261 return 0;
Cyrill Gorcunov6f0aced2009-05-01 23:54:25 +04002262 pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
Yinghai Lue0e42142009-04-26 23:39:38 -07002263 multi = 1;
2264 return 0;
2265}
2266
2267static const __cpuinitconst struct dmi_system_id multi_dmi_table[] = {
2268 {
2269 .callback = set_multi,
2270 .ident = "IBM System Summit2",
2271 .matches = {
2272 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2273 DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2274 },
2275 },
2276 {}
2277};
2278
2279static void __cpuinit dmi_check_multi(void)
2280{
2281 if (multi_checked)
2282 return;
2283
2284 dmi_check_system(multi_dmi_table);
2285 multi_checked = 1;
2286}
2287
2288/*
2289 * apic_is_clustered_box() -- Check if we can expect good TSC
2290 *
2291 * Thus far, the major user of this is IBM's Summit2 series:
2292 * Clustered boxes may have unsynced TSC problems if they are
2293 * multi-chassis.
2294 * Use DMI to check them
2295 */
2296__cpuinit int apic_is_clustered_box(void)
2297{
2298 dmi_check_multi();
2299 if (multi)
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002300 return 1;
2301
Yinghai Lue0e42142009-04-26 23:39:38 -07002302 if (!is_vsmp_box())
2303 return 0;
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 /*
Yinghai Lue0e42142009-04-26 23:39:38 -07002306 * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2307 * not guaranteed to be synced between boards
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 */
Yinghai Lue0e42142009-04-26 23:39:38 -07002309 if (apic_cluster_num() > 1)
2310 return 1;
2311
2312 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002314#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002317 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002319static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002320{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07002322 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002323 return 0;
2324}
2325early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002327/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002328static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002329{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002330 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002331}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002332early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Linus Torvalds2e7c2832007-03-23 11:32:31 -07002334static int __init parse_lapic_timer_c2_ok(char *arg)
2335{
2336 local_apic_timer_c2_ok = 1;
2337 return 0;
2338}
2339early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2340
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002341static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002344 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002345}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002346early_param("noapictimer", parse_disable_apic_timer);
2347
2348static int __init parse_nolapic_timer(char *arg)
2349{
2350 disable_apic_timer = 1;
2351 return 0;
2352}
2353early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01002354
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002355static int __init apic_set_verbosity(char *arg)
2356{
2357 if (!arg) {
2358#ifdef CONFIG_X86_64
2359 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002360 return 0;
2361#endif
2362 return -EINVAL;
2363 }
2364
2365 if (strcmp("debug", arg) == 0)
2366 apic_verbosity = APIC_DEBUG;
2367 else if (strcmp("verbose", arg) == 0)
2368 apic_verbosity = APIC_VERBOSE;
2369 else {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01002370 pr_warning("APIC Verbosity level %s not recognised"
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002371 " use apic=verbose or apic=debug\n", arg);
2372 return -EINVAL;
2373 }
2374
2375 return 0;
2376}
2377early_param("apic", apic_set_verbosity);
2378
Yinghai Lu1e934dd2008-02-22 13:37:26 -08002379static int __init lapic_insert_resource(void)
2380{
2381 if (!apic_phys)
2382 return -1;
2383
2384 /* Put local APIC into the resource map. */
2385 lapic_resource.start = apic_phys;
2386 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2387 insert_resource(&iomem_resource, &lapic_resource);
2388
2389 return 0;
2390}
2391
2392/*
2393 * need call insert after e820_reserve_resources()
2394 * that is using request_resource
2395 */
2396late_initcall(lapic_insert_resource);