blob: cb1304856a5cdf6422350d63451433fc1e651d33 [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
Robert Richtera68c4392010-10-06 12:27:53 +0200393static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100394
Robert Richtera68c4392010-10-06 12:27:53 +0200395static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
396{
397 return (old & APIC_EILVT_MASKED)
398 || (new == APIC_EILVT_MASKED)
399 || ((new & ~APIC_EILVT_MASKED) == old);
400}
401
402static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
403{
404 unsigned int rsvd; /* 0: uninitialized */
405
406 if (offset >= APIC_EILVT_NR_MAX)
407 return ~0;
408
409 rsvd = atomic_read(&eilvt_offsets[offset]) & ~APIC_EILVT_MASKED;
410 do {
411 if (rsvd &&
412 !eilvt_entry_is_changeable(rsvd, new))
413 /* may not change if vectors are different */
414 return rsvd;
415 rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
416 } while (rsvd != new);
417
418 return new;
419}
420
421/*
422 * If mask=1, the LVT entry does not generate interrupts while mask=0
423 * enables the vector. See also the BKDGs.
424 */
425
Robert Richter27afdf22010-10-06 12:27:54 +0200426int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
Robert Richtera68c4392010-10-06 12:27:53 +0200427{
428 unsigned long reg = APIC_EILVTn(offset);
429 unsigned int new, old, reserved;
430
431 new = (mask << 16) | (msg_type << 8) | vector;
432 old = apic_read(reg);
433 reserved = reserve_eilvt_offset(offset, new);
434
435 if (reserved != new) {
Robert Richtereb48c9c2010-10-25 16:03:39 +0200436 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
437 "vector 0x%x, but the register is already in use for "
438 "vector 0x%x on another cpu\n",
439 smp_processor_id(), reg, offset, new, reserved);
Robert Richtera68c4392010-10-06 12:27:53 +0200440 return -EINVAL;
441 }
442
443 if (!eilvt_entry_is_changeable(old, new)) {
Robert Richtereb48c9c2010-10-25 16:03:39 +0200444 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
445 "vector 0x%x, but the register is already in use for "
446 "vector 0x%x on this cpu\n",
447 smp_processor_id(), reg, offset, new, old);
Robert Richtera68c4392010-10-06 12:27:53 +0200448 return -EBUSY;
449 }
450
451 apic_write(reg, new);
452
453 return 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100454}
Robert Richter27afdf22010-10-06 12:27:54 +0200455EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
Robert Richter7b83dae2008-01-30 13:30:40 +0100456
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100457/*
458 * Program the next event, relative to now
459 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200460static int lapic_next_event(unsigned long delta,
461 struct clock_event_device *evt)
462{
463 apic_write(APIC_TMICT, delta);
464 return 0;
465}
466
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100467/*
468 * Setup the lapic timer in periodic or oneshot mode
469 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200470static void lapic_timer_setup(enum clock_event_mode mode,
471 struct clock_event_device *evt)
472{
473 unsigned long flags;
474 unsigned int v;
475
476 /* Lapic used as dummy for broadcast ? */
477 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
478 return;
479
480 local_irq_save(flags);
481
482 switch (mode) {
483 case CLOCK_EVT_MODE_PERIODIC:
484 case CLOCK_EVT_MODE_ONESHOT:
485 __setup_APIC_LVTT(calibration_result,
486 mode != CLOCK_EVT_MODE_PERIODIC, 1);
487 break;
488 case CLOCK_EVT_MODE_UNUSED:
489 case CLOCK_EVT_MODE_SHUTDOWN:
490 v = apic_read(APIC_LVTT);
491 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
492 apic_write(APIC_LVTT, v);
Andreas Herrmann6f9b4102009-10-27 11:01:38 +0100493 apic_write(APIC_TMICT, 0);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200494 break;
495 case CLOCK_EVT_MODE_RESUME:
496 /* Nothing to do here */
497 break;
498 }
499
500 local_irq_restore(flags);
501}
502
503/*
504 * Local APIC timer broadcast function
505 */
Mike Travis96289372008-12-31 18:08:46 -0800506static void lapic_timer_broadcast(const struct cpumask *mask)
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200507{
508#ifdef CONFIG_SMP
Ingo Molnardac5f412009-01-28 15:42:24 +0100509 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200510#endif
511}
512
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100513/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200514 * Setup the local APIC timer for this CPU. Copy the initialized values
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100515 * of the boot CPU and register the clock event in the framework.
516 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700517static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200518{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100519 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
520
Venkatesh Pallipadidb954b52009-04-06 18:51:29 -0700521 if (cpu_has(&current_cpu_data, X86_FEATURE_ARAT)) {
522 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
523 /* Make LAPIC timer preferrable over percpu HPET */
524 lapic_clockevent.rating = 150;
525 }
526
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100527 memcpy(levt, &lapic_clockevent, sizeof(*levt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030528 levt->cpumask = cpumask_of(smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100529
530 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200531}
532
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700533/*
534 * In this functions we calibrate APIC bus clocks to the external timer.
535 *
536 * We want to do the calibration only once since we want to have local timer
537 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
538 * frequency.
539 *
540 * This was previously done by reading the PIT/HPET and waiting for a wrap
541 * around to find out, that a tick has elapsed. I have a box, where the PIT
542 * readout is broken, so it never gets out of the wait loop again. This was
543 * also reported by others.
544 *
545 * Monitoring the jiffies value is inaccurate and the clockevents
546 * infrastructure allows us to do a simple substitution of the interrupt
547 * handler.
548 *
549 * The calibration routine also uses the pm_timer when possible, as the PIT
550 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
551 * back to normal later in the boot process).
552 */
553
554#define LAPIC_CAL_LOOPS (HZ/10)
555
556static __initdata int lapic_cal_loops = -1;
557static __initdata long lapic_cal_t1, lapic_cal_t2;
558static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
559static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
560static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
561
562/*
563 * Temporary interrupt handler.
564 */
565static void __init lapic_cal_handler(struct clock_event_device *dev)
566{
567 unsigned long long tsc = 0;
568 long tapic = apic_read(APIC_TMCCT);
569 unsigned long pm = acpi_pm_read_early();
570
571 if (cpu_has_tsc)
572 rdtscll(tsc);
573
574 switch (lapic_cal_loops++) {
575 case 0:
576 lapic_cal_t1 = tapic;
577 lapic_cal_tsc1 = tsc;
578 lapic_cal_pm1 = pm;
579 lapic_cal_j1 = jiffies;
580 break;
581
582 case LAPIC_CAL_LOOPS:
583 lapic_cal_t2 = tapic;
584 lapic_cal_tsc2 = tsc;
585 if (pm < lapic_cal_pm1)
586 pm += ACPI_PM_OVRRUN;
587 lapic_cal_pm2 = pm;
588 lapic_cal_j2 = jiffies;
589 break;
590 }
591}
592
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900593static int __init
594calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400595{
596 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
597 const long pm_thresh = pm_100ms / 100;
598 unsigned long mult;
599 u64 res;
600
601#ifndef CONFIG_X86_PM_TIMER
602 return -1;
603#endif
604
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900605 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400606
607 /* Check, if the PM timer is available */
608 if (!deltapm)
609 return -1;
610
611 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
612
613 if (deltapm > (pm_100ms - pm_thresh) &&
614 deltapm < (pm_100ms + pm_thresh)) {
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900615 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900616 return 0;
617 }
618
619 res = (((u64)deltapm) * mult) >> 22;
620 do_div(res, 1000000);
621 pr_warning("APIC calibration not consistent "
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900622 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900623
624 /* Correct the lapic counter value */
625 res = (((u64)(*delta)) * pm_100ms);
626 do_div(res, deltapm);
627 pr_info("APIC delta adjusted to PM-Timer: "
628 "%lu (%ld)\n", (unsigned long)res, *delta);
629 *delta = (long)res;
630
631 /* Correct the tsc counter value */
632 if (cpu_has_tsc) {
633 res = (((u64)(*deltatsc)) * pm_100ms);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400634 do_div(res, deltapm);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900635 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
Frans Pop3235dc32010-02-06 18:47:17 +0100636 "PM-Timer: %lu (%ld)\n",
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900637 (unsigned long)res, *deltatsc);
638 *deltatsc = (long)res;
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400639 }
640
641 return 0;
642}
643
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700644static int __init calibrate_APIC_clock(void)
645{
646 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700647 void (*real_handler)(struct clock_event_device *dev);
648 unsigned long deltaj;
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900649 long delta, deltatsc;
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700650 int pm_referenced = 0;
651
652 local_irq_disable();
653
654 /* Replace the global interrupt handler */
655 real_handler = global_clock_event->event_handler;
656 global_clock_event->event_handler = lapic_cal_handler;
657
658 /*
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400659 * Setup the APIC counter to maximum. There is no way the lapic
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700660 * can underflow in the 100ms detection time frame
661 */
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400662 __setup_APIC_LVTT(0xffffffff, 0, 0);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700663
664 /* Let the interrupts run */
665 local_irq_enable();
666
667 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
668 cpu_relax();
669
670 local_irq_disable();
671
672 /* Restore the real event handler */
673 global_clock_event->event_handler = real_handler;
674
675 /* Build delta t1-t2 as apic timer counts down */
676 delta = lapic_cal_t1 - lapic_cal_t2;
677 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
678
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900679 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
680
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400681 /* we trust the PM based calibration if possible */
682 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900683 &delta, &deltatsc);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700684
685 /* Calculate the scaled math multiplication factor */
686 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
687 lapic_clockevent.shift);
688 lapic_clockevent.max_delta_ns =
689 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
690 lapic_clockevent.min_delta_ns =
691 clockevent_delta2ns(0xF, &lapic_clockevent);
692
693 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
694
695 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
Thomas Gleixner411462f2009-11-16 11:52:39 +0100696 apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700697 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
698 calibration_result);
699
700 if (cpu_has_tsc) {
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700701 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
702 "%ld.%04ld MHz.\n",
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900703 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
704 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700705 }
706
707 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
708 "%u.%04u MHz.\n",
709 calibration_result / (1000000 / HZ),
710 calibration_result % (1000000 / HZ));
711
712 /*
713 * Do a sanity check on the APIC calibration result
714 */
715 if (calibration_result < (1000000 / HZ)) {
716 local_irq_enable();
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100717 pr_warning("APIC frequency too slow, disabling apic timer\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700718 return -1;
719 }
720
721 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
722
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400723 /*
724 * PM timer calibration failed or not turned on
725 * so lets try APIC timer based calibration
726 */
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700727 if (!pm_referenced) {
728 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
729
730 /*
731 * Setup the apic timer manually
732 */
733 levt->event_handler = lapic_cal_handler;
734 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
735 lapic_cal_loops = -1;
736
737 /* Let the interrupts run */
738 local_irq_enable();
739
740 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
741 cpu_relax();
742
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700743 /* Stop the lapic timer */
744 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
745
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700746 /* Jiffies delta */
747 deltaj = lapic_cal_j2 - lapic_cal_j1;
748 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
749
750 /* Check, if the jiffies result is consistent */
751 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
752 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
753 else
754 levt->features |= CLOCK_EVT_FEAT_DUMMY;
755 } else
756 local_irq_enable();
757
758 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +0530759 pr_warning("APIC timer disabled due to verification failure\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700760 return -1;
761 }
762
763 return 0;
764}
765
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100766/*
767 * Setup the boot APIC
768 *
769 * Calibrate and verify the result.
770 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100771void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100773 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400774 * The local apic timer can be disabled via the kernel
775 * commandline or from the CPU detection code. Register the lapic
776 * timer as a dummy clock event source on SMP systems, so the
777 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100778 */
779 if (disable_apic_timer) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100780 pr_info("Disabling APIC timer\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100781 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100782 if (num_possible_cpus() > 1) {
783 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100784 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100785 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100786 return;
787 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200788
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400789 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
790 "calibrating APIC timer ...\n");
791
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400792 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100793 /* No broadcast on UP ! */
794 if (num_possible_cpus() > 1)
795 setup_APIC_timer();
796 return;
797 }
798
799 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100800 * If nmi_watchdog is set to IO_APIC, we need the
801 * PIT/HPET going. Otherwise register lapic as a dummy
802 * device.
803 */
804 if (nmi_watchdog != NMI_IO_APIC)
805 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
806 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100807 pr_warning("APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200808 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100809
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400810 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100811 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
813
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100814void __cpuinit setup_secondary_APIC_clock(void)
815{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100816 setup_APIC_timer();
817}
818
819/*
820 * The guts of the apic timer interrupt
821 */
822static void local_apic_timer_interrupt(void)
823{
824 int cpu = smp_processor_id();
825 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
826
827 /*
828 * Normally we should not be here till LAPIC has been initialized but
829 * in some cases like kdump, its possible that there is a pending LAPIC
830 * timer interrupt from previous kernel's context and is delivered in
831 * new kernel the moment interrupts are enabled.
832 *
833 * Interrupts are enabled early and LAPIC is setup much later, hence
834 * its possible that when we get here evt->event_handler is NULL.
835 * Check for event_handler being NULL and discard the interrupt as
836 * spurious.
837 */
838 if (!evt->event_handler) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100839 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100840 /* Switch it off */
841 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
842 return;
843 }
844
845 /*
846 * the NMI deadlock-detector uses this.
847 */
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800848 inc_irq_stat(apic_timer_irqs);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100849
850 evt->event_handler(evt);
851}
852
853/*
854 * Local APIC timer interrupt. This is the most natural way for doing
855 * local interrupts, but local timer interrupts can be emulated by
856 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
857 *
858 * [ if a single-CPU system runs an SMP kernel then we call the local
859 * interrupt as well. Thus we cannot inline the local irq ... ]
860 */
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +0100861void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100862{
863 struct pt_regs *old_regs = set_irq_regs(regs);
864
865 /*
866 * NOTE! We'd better ACK the irq immediately,
867 * because timer handling can be slow.
868 */
869 ack_APIC_irq();
870 /*
871 * update_process_times() expects us to have done irq_enter().
872 * Besides, if we don't timer interrupts ignore the global
873 * interrupt lock, which is the WrongThing (tm) to do.
874 */
875 exit_idle();
876 irq_enter();
877 local_apic_timer_interrupt();
878 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400879
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100880 set_irq_regs(old_regs);
881}
882
883int setup_profiling_timer(unsigned int multiplier)
884{
885 return -EINVAL;
886}
887
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100888/*
889 * Local APIC start and shutdown
890 */
891
892/**
893 * clear_local_APIC - shutdown the local APIC
894 *
895 * This is called, when a CPU is disabled and before rebooting, so the state of
896 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
897 * leftovers during boot.
898 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899void clear_local_APIC(void)
900{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400901 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100902 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Andi Kleend3432892008-01-30 13:33:17 +0100904 /* APIC hasn't been mapped yet */
Suresh Siddhafc1edaf2009-04-20 13:02:27 -0700905 if (!x2apic_mode && !apic_phys)
Andi Kleend3432892008-01-30 13:33:17 +0100906 return;
907
908 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200910 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * if the vector is zero. Mask LVTERR first to prevent this.
912 */
913 if (maxlvt >= 3) {
914 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100915 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 /*
918 * Careful: we have to set masks only first to deassert
919 * any level-triggered sources.
920 */
921 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100922 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100924 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100926 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (maxlvt >= 4) {
928 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100929 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400932 /* lets not touch this if we didn't frob it */
Andi Kleen4efc0672009-04-28 19:07:31 +0200933#ifdef CONFIG_X86_THERMAL_VECTOR
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400934 if (maxlvt >= 5) {
935 v = apic_read(APIC_LVTTHMR);
936 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
937 }
938#endif
Andi Kleen5ca86812009-02-12 13:49:37 +0100939#ifdef CONFIG_X86_MCE_INTEL
940 if (maxlvt >= 6) {
941 v = apic_read(APIC_LVTCMCI);
942 if (!(v & APIC_LVT_MASKED))
943 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
944 }
945#endif
946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 /*
948 * Clean APIC state for other OSs:
949 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100950 apic_write(APIC_LVTT, APIC_LVT_MASKED);
951 apic_write(APIC_LVT0, APIC_LVT_MASKED);
952 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100954 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100956 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400957
958 /* Integrated APIC (!82489DX) ? */
959 if (lapic_is_integrated()) {
960 if (maxlvt > 3)
961 /* Clear ESR due to Pentium errata 3AP and 11AP */
962 apic_write(APIC_ESR, 0);
963 apic_read(APIC_ESR);
964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100967/**
968 * disable_local_APIC - clear and disable the local APIC
969 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970void disable_local_APIC(void)
971{
972 unsigned int value;
973
Jan Beulich4a13ad02009-01-14 12:28:51 +0000974 /* APIC hasn't been mapped yet */
Yinghai Lufd19dce2010-07-15 00:00:59 -0700975 if (!x2apic_mode && !apic_phys)
Jan Beulich4a13ad02009-01-14 12:28:51 +0000976 return;
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 clear_local_APIC();
979
980 /*
981 * Disable APIC (implies clearing of registers
982 * for 82489DX!).
983 */
984 value = apic_read(APIC_SPIV);
985 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100986 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400987
988#ifdef CONFIG_X86_32
989 /*
990 * When LAPIC was disabled by the BIOS and enabled by the kernel,
991 * restore the disabled state.
992 */
993 if (enabled_via_apicbase) {
994 unsigned int l, h;
995
996 rdmsr(MSR_IA32_APICBASE, l, h);
997 l &= ~MSR_IA32_APICBASE_ENABLE;
998 wrmsr(MSR_IA32_APICBASE, l, h);
999 }
1000#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001}
1002
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +04001003/*
1004 * If Linux enabled the LAPIC against the BIOS default disable it down before
1005 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
1006 * not power-off. Additionally clear all LVT entries before disable_local_APIC
1007 * for the case where Linux didn't enable the LAPIC.
1008 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -07001009void lapic_shutdown(void)
1010{
1011 unsigned long flags;
1012
Cyrill Gorcunov83121362009-09-15 11:12:30 +04001013 if (!cpu_has_apic && !apic_from_smp_config())
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -07001014 return;
1015
1016 local_irq_save(flags);
1017
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +04001018#ifdef CONFIG_X86_32
1019 if (!enabled_via_apicbase)
1020 clear_local_APIC();
1021 else
1022#endif
1023 disable_local_APIC();
1024
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -07001025
1026 local_irq_restore(flags);
1027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029/*
1030 * This is to verify that we're looking at a real local APIC.
1031 * Check these against your board if the CPUs aren't getting
1032 * started for no apparent reason.
1033 */
1034int __init verify_local_APIC(void)
1035{
1036 unsigned int reg0, reg1;
1037
1038 /*
1039 * The version register is read-only in a real APIC.
1040 */
1041 reg0 = apic_read(APIC_LVR);
1042 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
1043 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
1044 reg1 = apic_read(APIC_LVR);
1045 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
1046
1047 /*
1048 * The two version reads above should print the same
1049 * numbers. If the second one is different, then we
1050 * poke at a non-APIC.
1051 */
1052 if (reg1 != reg0)
1053 return 0;
1054
1055 /*
1056 * Check if the version looks reasonably.
1057 */
1058 reg1 = GET_APIC_VERSION(reg0);
1059 if (reg1 == 0x00 || reg1 == 0xff)
1060 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001061 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (reg1 < 0x02 || reg1 == 0xff)
1063 return 0;
1064
1065 /*
1066 * The ID register is read/write in a real APIC.
1067 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001068 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001070 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001071 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1073 apic_write(APIC_ID, reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001074 if (reg1 != (reg0 ^ apic->apic_id_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076
1077 /*
1078 * The next two are just to see if we have sane values.
1079 * They're only really relevant if we're in Virtual Wire
1080 * compatibility mode, but most boxes are anymore.
1081 */
1082 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001083 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 reg1 = apic_read(APIC_LVT1);
1085 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1086
1087 return 1;
1088}
1089
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001090/**
1091 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1092 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093void __init sync_Arb_IDs(void)
1094{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +02001095 /*
1096 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1097 * needed on AMD.
1098 */
1099 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 return;
1101
1102 /*
1103 * Wait for idle.
1104 */
1105 apic_wait_icr_idle();
1106
1107 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +04001108 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1109 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112/*
1113 * An initial setup of the virtual wire mode.
1114 */
1115void __init init_bsp_APIC(void)
1116{
Andi Kleen11a8e772006-01-11 22:46:51 +01001117 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 /*
1120 * Don't do the setup now if we have a SMP BIOS as the
1121 * through-I/O-APIC virtual wire mode might be active.
1122 */
1123 if (smp_found_config || !cpu_has_apic)
1124 return;
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 /*
1127 * Do not trust the local APIC being empty at bootup.
1128 */
1129 clear_local_APIC();
1130
1131 /*
1132 * Enable APIC.
1133 */
1134 value = apic_read(APIC_SPIV);
1135 value &= ~APIC_VECTOR_MASK;
1136 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001137
1138#ifdef CONFIG_X86_32
1139 /* This bit is reserved on P4/Xeon and should be cleared */
1140 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1141 (boot_cpu_data.x86 == 15))
1142 value &= ~APIC_SPIV_FOCUS_DISABLED;
1143 else
1144#endif
1145 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001147 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
1149 /*
1150 * Set up the virtual wire mode.
1151 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001152 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001154 if (!lapic_is_integrated()) /* 82489DX */
1155 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001156 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157}
1158
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001159static void __cpuinit lapic_setup_esr(void)
1160{
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001161 unsigned int oldvalue, value, maxlvt;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001162
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001163 if (!lapic_is_integrated()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001164 pr_info("No ESR for 82489DX.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001165 return;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001166 }
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001167
Ingo Molnar08125d32009-01-28 05:08:44 +01001168 if (apic->disable_esr) {
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001169 /*
1170 * Something untraceable is creating bad interrupts on
1171 * secondary quads ... for the moment, just leave the
1172 * ESR disabled - we can't do anything useful with the
1173 * errors anyway - mbligh
1174 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001175 pr_info("Leaving ESR disabled.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001176 return;
1177 }
1178
1179 maxlvt = lapic_get_maxlvt();
1180 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1181 apic_write(APIC_ESR, 0);
1182 oldvalue = apic_read(APIC_ESR);
1183
1184 /* enables sending errors */
1185 value = ERROR_APIC_VECTOR;
1186 apic_write(APIC_LVTERR, value);
1187
1188 /*
1189 * spec says clear errors after enabling vector.
1190 */
1191 if (maxlvt > 3)
1192 apic_write(APIC_ESR, 0);
1193 value = apic_read(APIC_ESR);
1194 if (value != oldvalue)
1195 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1196 "vector: 0x%08x after: 0x%08x\n",
1197 oldvalue, value);
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001198}
1199
1200
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001201/**
1202 * setup_local_APIC - setup the local APIC
1203 */
1204void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001206 unsigned int value, queued;
1207 int i, j, acked = 0;
1208 unsigned long long tsc = 0, ntsc;
1209 long long max_loops = cpu_khz;
1210
1211 if (cpu_has_tsc)
1212 rdtscll(tsc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Jan Beulichf1182632009-01-14 12:27:35 +00001214 if (disable_apic) {
Ingo Molnar65a4e572009-01-31 03:36:17 +01001215 arch_disable_smp_support();
Jan Beulichf1182632009-01-14 12:27:35 +00001216 return;
1217 }
1218
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001219#ifdef CONFIG_X86_32
1220 /* Pound the ESR really hard over the head with a big hammer - mbligh */
Ingo Molnar08125d32009-01-28 05:08:44 +01001221 if (lapic_is_integrated() && apic->disable_esr) {
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001222 apic_write(APIC_ESR, 0);
1223 apic_write(APIC_ESR, 0);
1224 apic_write(APIC_ESR, 0);
1225 apic_write(APIC_ESR, 0);
1226 }
1227#endif
Ingo Molnarcdd6c482009-09-21 12:02:48 +02001228 perf_events_lapic_init();
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001229
Jack Steinerac23d4e2008-03-28 14:12:16 -05001230 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 /*
1233 * Double-check whether this APIC is really registered.
1234 * This is meaningless in clustered apic mode, so we skip it.
1235 */
Daniel Walkerc2777f92009-09-12 10:40:20 -07001236 BUG_ON(!apic->apic_id_registered());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 /*
1239 * Intel recommends to set DFR, LDR and TPR before enabling
1240 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1241 * document number 292116). So here it goes...
1242 */
Ingo Molnara5c43292009-01-28 06:50:47 +01001243 apic->init_apic_ldr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 /*
1246 * Set Task Priority to 'accept all'. We never change this
1247 * later on.
1248 */
1249 value = apic_read(APIC_TASKPRI);
1250 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +01001251 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001254 * After a crash, we no longer service the interrupts and a pending
1255 * interrupt from previous kernel might still have ISR bit set.
1256 *
1257 * Most probably by now CPU has serviced that pending interrupt and
1258 * it might not have done the ack_APIC_irq() because it thought,
1259 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1260 * does not clear the ISR bit and cpu thinks it has already serivced
1261 * the interrupt. Hence a vector might get locked. It was noticed
1262 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1263 */
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001264 do {
1265 queued = 0;
1266 for (i = APIC_ISR_NR - 1; i >= 0; i--)
1267 queued |= apic_read(APIC_IRR + i*0x10);
1268
1269 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1270 value = apic_read(APIC_ISR + i*0x10);
1271 for (j = 31; j >= 0; j--) {
1272 if (value & (1<<j)) {
1273 ack_APIC_irq();
1274 acked++;
1275 }
1276 }
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001277 }
Kerstin Jonsson8c3ba8d2010-05-24 12:13:15 -07001278 if (acked > 256) {
1279 printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
1280 acked);
1281 break;
1282 }
1283 if (cpu_has_tsc) {
1284 rdtscll(ntsc);
1285 max_loops = (cpu_khz << 10) - (ntsc - tsc);
1286 } else
1287 max_loops--;
1288 } while (queued && max_loops > 0);
1289 WARN_ON(max_loops <= 0);
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001290
1291 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 * Now that we are all set up, enable the APIC
1293 */
1294 value = apic_read(APIC_SPIV);
1295 value &= ~APIC_VECTOR_MASK;
1296 /*
1297 * Enable APIC
1298 */
1299 value |= APIC_SPIV_APIC_ENABLED;
1300
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001301#ifdef CONFIG_X86_32
1302 /*
1303 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1304 * certain networking cards. If high frequency interrupts are
1305 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1306 * entry is masked/unmasked at a high rate as well then sooner or
1307 * later IOAPIC line gets 'stuck', no more interrupts are received
1308 * from the device. If focus CPU is disabled then the hang goes
1309 * away, oh well :-(
1310 *
1311 * [ This bug can be reproduced easily with a level-triggered
1312 * PCI Ne2000 networking cards and PII/PIII processors, dual
1313 * BX chipset. ]
1314 */
1315 /*
1316 * Actually disabling the focus CPU check just makes the hang less
1317 * frequent as it makes the interrupt distributon model be more
1318 * like LRU than MRU (the short-term load is more even across CPUs).
1319 * See also the comment in end_level_ioapic_irq(). --macro
1320 */
1321
1322 /*
1323 * - enable focus processor (bit==0)
1324 * - 64bit mode always use processor focus
1325 * so no need to set it
1326 */
1327 value &= ~APIC_SPIV_FOCUS_DISABLED;
1328#endif
Andi Kleen3f14c742006-09-26 10:52:29 +02001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /*
1331 * Set spurious IRQ vector
1332 */
1333 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001334 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 /*
1337 * Set up LVT0, LVT1:
1338 *
1339 * set up through-local-APIC on the BP's LINT0. This is not
1340 * strictly necessary in pure symmetric-IO mode, but sometimes
1341 * we delegate interrupts to the 8259A.
1342 */
1343 /*
1344 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1345 */
1346 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001347 if (!smp_processor_id() && (pic_mode || !value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001349 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001350 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 } else {
1352 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001353 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001354 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001356 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 /*
1359 * only the BP should see the LINT1 NMI signal, obviously.
1360 */
1361 if (!smp_processor_id())
1362 value = APIC_DM_NMI;
1363 else
1364 value = APIC_DM_NMI | APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001365 if (!lapic_is_integrated()) /* 82489DX */
1366 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001367 apic_write(APIC_LVT1, value);
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001368
Jack Steinerac23d4e2008-03-28 14:12:16 -05001369 preempt_enable();
Andi Kleenbe71b852009-02-12 13:49:38 +01001370
1371#ifdef CONFIG_X86_MCE_INTEL
1372 /* Recheck CMCI information after local APIC is up on CPU #0 */
1373 if (smp_processor_id() == 0)
1374 cmci_recheck();
1375#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001376}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Andi Kleen739f33b2008-01-30 13:30:40 +01001378void __cpuinit end_local_APIC_setup(void)
1379{
1380 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001381
1382#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001383 {
1384 unsigned int value;
1385 /* Disable the local apic timer */
1386 value = apic_read(APIC_LVTT);
1387 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1388 apic_write(APIC_LVTT, value);
1389 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001390#endif
1391
Don Zickusf2802e72006-09-26 10:52:26 +02001392 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 apic_pm_activate();
1394}
1395
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001396#ifdef CONFIG_X86_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001397void check_x2apic(void)
1398{
Suresh Siddhaef1f87a2009-02-21 14:23:21 -08001399 if (x2apic_enabled()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001400 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001401 x2apic_preenabled = x2apic_mode = 1;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001402 }
1403}
1404
1405void enable_x2apic(void)
1406{
1407 int msr, msr2;
1408
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001409 if (!x2apic_mode)
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001410 return;
1411
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001412 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1413 if (!(msr & X2APIC_ENABLE)) {
Mike Travis450b1e82009-12-11 08:08:50 -08001414 printk_once(KERN_INFO "Enabling x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001415 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1416 }
1417}
Weidong Han93758232009-04-17 16:42:14 +08001418#endif /* CONFIG_X86_X2APIC */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001419
Gleb Natapovce69a782009-07-20 15:24:17 +03001420int __init enable_IR(void)
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001421{
1422#ifdef CONFIG_INTR_REMAP
Weidong Han93758232009-04-17 16:42:14 +08001423 if (!intr_remapping_supported()) {
1424 pr_debug("intr-remapping not supported\n");
Gleb Natapovce69a782009-07-20 15:24:17 +03001425 return 0;
Weidong Han93758232009-04-17 16:42:14 +08001426 }
1427
Weidong Han93758232009-04-17 16:42:14 +08001428 if (!x2apic_preenabled && skip_ioapic_setup) {
1429 pr_info("Skipped enabling intr-remap because of skipping "
1430 "io-apic setup\n");
Gleb Natapovce69a782009-07-20 15:24:17 +03001431 return 0;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001432 }
1433
Gleb Natapovce69a782009-07-20 15:24:17 +03001434 if (enable_intr_remapping(x2apic_supported()))
1435 return 0;
1436
1437 pr_info("Enabled Interrupt-remapping\n");
1438
1439 return 1;
1440
1441#endif
1442 return 0;
1443}
1444
1445void __init enable_IR_x2apic(void)
1446{
1447 unsigned long flags;
1448 struct IO_APIC_route_entry **ioapic_entries = NULL;
1449 int ret, x2apic_enabled = 0;
Yinghai Lue6707612009-11-21 00:23:37 -08001450 int dmar_table_init_ret;
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001451
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001452 dmar_table_init_ret = dmar_table_init();
Yinghai Lue6707612009-11-21 00:23:37 -08001453 if (dmar_table_init_ret && !x2apic_supported())
1454 return;
Gleb Natapovce69a782009-07-20 15:24:17 +03001455
Fenghua Yub24696b2009-03-27 14:22:44 -07001456 ioapic_entries = alloc_ioapic_entries();
1457 if (!ioapic_entries) {
Gleb Natapovce69a782009-07-20 15:24:17 +03001458 pr_err("Allocate ioapic_entries failed\n");
1459 goto out;
Fenghua Yub24696b2009-03-27 14:22:44 -07001460 }
1461
1462 ret = save_IO_APIC_setup(ioapic_entries);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001463 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001464 pr_info("Saving IO-APIC state failed: %d\n", ret);
Gleb Natapovce69a782009-07-20 15:24:17 +03001465 goto out;
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001466 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001467
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001468 local_irq_save(flags);
Jacob Panb81bb372009-11-09 11:27:04 -08001469 legacy_pic->mask_all();
Gleb Natapovce69a782009-07-20 15:24:17 +03001470 mask_IO_APIC_setup(ioapic_entries);
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001471
Yinghai Lub7f42ab2009-08-17 11:19:40 -07001472 if (dmar_table_init_ret)
1473 ret = 0;
1474 else
1475 ret = enable_IR();
1476
Gleb Natapovce69a782009-07-20 15:24:17 +03001477 if (!ret) {
1478 /* IR is required if there is APIC ID > 255 even when running
1479 * under KVM
1480 */
1481 if (max_physical_apicid > 255 || !kvm_para_available())
1482 goto nox2apic;
1483 /*
1484 * without IR all CPUs can be addressed by IOAPIC/MSI
1485 * only in physical mode
1486 */
1487 x2apic_force_phys();
1488 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001489
Gleb Natapovce69a782009-07-20 15:24:17 +03001490 x2apic_enabled = 1;
Weidong Han93758232009-04-17 16:42:14 +08001491
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001492 if (x2apic_supported() && !x2apic_mode) {
1493 x2apic_mode = 1;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001494 enable_x2apic();
Weidong Han93758232009-04-17 16:42:14 +08001495 pr_info("Enabled x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001496 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001497
Gleb Natapovce69a782009-07-20 15:24:17 +03001498nox2apic:
1499 if (!ret) /* IR enabling failed */
Fenghua Yub24696b2009-03-27 14:22:44 -07001500 restore_IO_APIC_setup(ioapic_entries);
Jacob Panb81bb372009-11-09 11:27:04 -08001501 legacy_pic->restore_mask();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001502 local_irq_restore(flags);
1503
Gleb Natapovce69a782009-07-20 15:24:17 +03001504out:
Fenghua Yub24696b2009-03-27 14:22:44 -07001505 if (ioapic_entries)
1506 free_ioapic_entries(ioapic_entries);
Weidong Han93758232009-04-17 16:42:14 +08001507
Gleb Natapovce69a782009-07-20 15:24:17 +03001508 if (x2apic_enabled)
Weidong Han93758232009-04-17 16:42:14 +08001509 return;
1510
Weidong Han93758232009-04-17 16:42:14 +08001511 if (x2apic_preenabled)
Gleb Natapovce69a782009-07-20 15:24:17 +03001512 panic("x2apic: enabled by BIOS but kernel init failed.");
Weidong Han93758232009-04-17 16:42:14 +08001513 else if (cpu_has_x2apic)
Gleb Natapovce69a782009-07-20 15:24:17 +03001514 pr_info("Not enabling x2apic, Intr-remapping init failed.\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001515}
Weidong Han93758232009-04-17 16:42:14 +08001516
Yinghai Lube7a6562008-08-24 02:01:51 -07001517#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001518/*
1519 * Detect and enable local APICs on non-SMP boards.
1520 * Original code written by Keir Fraser.
1521 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1522 * not correctly set up (usually the APIC timer won't work etc.)
1523 */
1524static int __init detect_init_APIC(void)
1525{
1526 if (!cpu_has_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001527 pr_info("No local APIC present\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001528 return -1;
1529 }
1530
1531 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001532 return 0;
1533}
Yinghai Lube7a6562008-08-24 02:01:51 -07001534#else
1535/*
1536 * Detect and initialize APIC
1537 */
1538static int __init detect_init_APIC(void)
1539{
1540 u32 h, l, features;
1541
1542 /* Disabled by kernel option? */
1543 if (disable_apic)
1544 return -1;
1545
1546 switch (boot_cpu_data.x86_vendor) {
1547 case X86_VENDOR_AMD:
1548 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
Borislav Petkov85877062009-02-03 16:24:22 +01001549 (boot_cpu_data.x86 >= 15))
Yinghai Lube7a6562008-08-24 02:01:51 -07001550 break;
1551 goto no_apic;
1552 case X86_VENDOR_INTEL:
1553 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1554 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1555 break;
1556 goto no_apic;
1557 default:
1558 goto no_apic;
1559 }
1560
1561 if (!cpu_has_apic) {
1562 /*
1563 * Over-ride BIOS and try to enable the local APIC only if
1564 * "lapic" specified.
1565 */
1566 if (!force_enable_local_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001567 pr_info("Local APIC disabled by BIOS -- "
1568 "you can enable it with \"lapic\"\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001569 return -1;
1570 }
1571 /*
1572 * Some BIOSes disable the local APIC in the APIC_BASE
1573 * MSR. This can only be done in software for Intel P6 or later
1574 * and AMD K7 (Model > 1) or later.
1575 */
1576 rdmsr(MSR_IA32_APICBASE, l, h);
1577 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001578 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001579 l &= ~MSR_IA32_APICBASE_BASE;
1580 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1581 wrmsr(MSR_IA32_APICBASE, l, h);
1582 enabled_via_apicbase = 1;
1583 }
1584 }
1585 /*
1586 * The APIC feature bit should now be enabled
1587 * in `cpuid'
1588 */
1589 features = cpuid_edx(1);
1590 if (!(features & (1 << X86_FEATURE_APIC))) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001591 pr_warning("Could not enable APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001592 return -1;
1593 }
1594 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1595 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1596
1597 /* The BIOS may have set up the APIC at some other address */
1598 rdmsr(MSR_IA32_APICBASE, l, h);
1599 if (l & MSR_IA32_APICBASE_ENABLE)
1600 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1601
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001602 pr_info("Found and enabled local APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001603
1604 apic_pm_activate();
1605
1606 return 0;
1607
1608no_apic:
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001609 pr_info("No local APIC present or hardware disabled\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001610 return -1;
1611}
1612#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001613
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001614#ifdef CONFIG_X86_64
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001615void __init early_init_lapic_mapping(void)
1616{
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001617 /*
1618 * If no local APIC can be found then go out
1619 * : it means there is no mpatable and MADT
1620 */
1621 if (!smp_found_config)
1622 return;
1623
Cyrill Gorcunovd3a247b2009-08-26 21:13:24 +04001624 set_fixmap_nocache(FIX_APIC_BASE, mp_lapic_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001625 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Cyrill Gorcunovd3a247b2009-08-26 21:13:24 +04001626 APIC_BASE, mp_lapic_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001627
1628 /*
1629 * Fetch the APIC ID of the BSP in case we have a
1630 * default configuration (or the MP table is broken).
1631 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001632 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001633}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001634#endif
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001635
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001636/**
1637 * init_apic_mappings - initialize APIC mappings
1638 */
1639void __init init_apic_mappings(void)
1640{
Yinghai Lu4401da62009-05-02 10:40:57 -07001641 unsigned int new_apicid;
1642
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07001643 if (x2apic_mode) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001644 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001645 return;
1646 }
1647
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001648 /* If no local APIC can be found return early */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001649 if (!smp_found_config && detect_init_APIC()) {
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001650 /* lets NOP'ify apic operations */
Cyrill Gorcunovcec6be62009-05-11 17:41:40 +04001651 pr_info("APIC: disable apic facility\n");
1652 apic_disable();
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001653 } else {
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001654 apic_phys = mp_lapic_addr;
1655
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001656 /*
1657 * acpi lapic path already maps that address in
1658 * acpi_register_lapic_address()
1659 */
Eric W. Biederman5989cd62010-08-04 13:30:27 -07001660 if (!acpi_lapic && !smp_found_config)
Yinghai Lu4797f6b2009-05-02 10:40:57 -07001661 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1662
1663 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
1664 APIC_BASE, apic_phys);
Cyrill Gorcunovcec6be62009-05-11 17:41:40 +04001665 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001666
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001667 /*
1668 * Fetch the APIC ID of the BSP in case we have a
1669 * default configuration (or the MP table is broken).
1670 */
Yinghai Lu4401da62009-05-02 10:40:57 -07001671 new_apicid = read_apic_id();
1672 if (boot_cpu_physical_apicid != new_apicid) {
1673 boot_cpu_physical_apicid = new_apicid;
Cyrill Gorcunov103428e2009-06-07 16:48:40 +04001674 /*
1675 * yeah -- we lie about apic_version
1676 * in case if apic was disabled via boot option
1677 * but it's not a problem for SMP compiled kernel
1678 * since smp_sanity_check is prepared for such a case
1679 * and disable smp mode
1680 */
Yinghai Lu4401da62009-05-02 10:40:57 -07001681 apic_version[new_apicid] =
1682 GET_APIC_VERSION(apic_read(APIC_LVR));
Cyrill Gorcunov08306ce2009-04-12 20:47:41 +04001683 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001684}
1685
1686/*
1687 * This initializes the IO-APIC and APIC hardware if this is
1688 * a UP kernel.
1689 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001690int apic_version[MAX_APICS];
1691
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001692int __init APIC_init_uniprocessor(void)
1693{
1694 if (disable_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001695 pr_info("Apic disabled\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001696 return -1;
1697 }
Jan Beulichf1182632009-01-14 12:27:35 +00001698#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001699 if (!cpu_has_apic) {
1700 disable_apic = 1;
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001701 pr_info("Apic disabled by BIOS\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001702 return -1;
1703 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001704#else
1705 if (!smp_found_config && !cpu_has_apic)
1706 return -1;
1707
1708 /*
1709 * Complain if the BIOS pretends there is one.
1710 */
1711 if (!cpu_has_apic &&
1712 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001713 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1714 boot_cpu_physical_apicid);
Yinghai Lufa2bd352008-08-24 02:01:50 -07001715 return -1;
1716 }
1717#endif
1718
Ingo Molnar72ce0162009-01-28 06:50:47 +01001719 default_setup_apic_routing();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001720
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001721 verify_local_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001722 connect_bsp_APIC();
1723
Yinghai Lufa2bd352008-08-24 02:01:50 -07001724#ifdef CONFIG_X86_64
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001725 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Yinghai Lufa2bd352008-08-24 02:01:50 -07001726#else
1727 /*
1728 * Hack: In case of kdump, after a crash, kernel might be booting
1729 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1730 * might be zero if read from MP tables. Get it from LAPIC.
1731 */
1732# ifdef CONFIG_CRASH_DUMP
1733 boot_cpu_physical_apicid = read_apic_id();
1734# endif
1735#endif
1736 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001737 setup_local_APIC();
1738
Yinghai Lu88d0f552009-02-14 23:57:28 -08001739#ifdef CONFIG_X86_IO_APIC
Andi Kleen739f33b2008-01-30 13:30:40 +01001740 /*
1741 * Now enable IO-APICs, actually call clear_IO_APIC
Yinghai Lu98c061b2009-02-16 00:00:50 -08001742 * We need clear_IO_APIC before enabling error vector
Andi Kleen739f33b2008-01-30 13:30:40 +01001743 */
1744 if (!skip_ioapic_setup && nr_ioapics)
1745 enable_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001746#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001747
1748 end_local_APIC_setup();
1749
Yinghai Lufa2bd352008-08-24 02:01:50 -07001750#ifdef CONFIG_X86_IO_APIC
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001751 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1752 setup_IO_APIC();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001753 else {
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001754 nr_ioapics = 0;
Yinghai Lu98c061b2009-02-16 00:00:50 -08001755 localise_nmi_watchdog();
1756 }
1757#else
1758 localise_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001759#endif
1760
Thomas Gleixner736deca2009-08-19 12:35:53 +02001761 x86_init.timers.setup_percpu_clockev();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001762#ifdef CONFIG_X86_64
1763 check_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001764#endif
1765
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001766 return 0;
1767}
1768
1769/*
1770 * Local APIC interrupts
1771 */
1772
1773/*
1774 * This interrupt should _never_ happen with our APIC/SMP architecture
1775 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001776void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001777{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001778 u32 v;
1779
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001780 exit_idle();
1781 irq_enter();
1782 /*
1783 * Check if this really is a spurious interrupt and ACK it
1784 * if it is a vectored one. Just in case...
1785 * Spurious interrupts should not be ACKed.
1786 */
1787 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1788 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1789 ack_APIC_irq();
1790
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -08001791 inc_irq_stat(irq_spurious_count);
1792
Yinghai Ludc1528d2008-08-24 02:01:53 -07001793 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001794 pr_info("spurious APIC interrupt on CPU#%d, "
1795 "should never happen.\n", smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001796 irq_exit();
1797}
1798
1799/*
1800 * This interrupt should never happen with our APIC/SMP architecture
1801 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001802void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001803{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001804 u32 v, v1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001805
1806 exit_idle();
1807 irq_enter();
1808 /* First tickle the hardware, only then report what went on. -- REW */
1809 v = apic_read(APIC_ESR);
1810 apic_write(APIC_ESR, 0);
1811 v1 = apic_read(APIC_ESR);
1812 ack_APIC_irq();
1813 atomic_inc(&irq_err_count);
1814
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001815 /*
1816 * Here is what the APIC error bits mean:
1817 * 0: Send CS error
1818 * 1: Receive CS error
1819 * 2: Send accept error
1820 * 3: Receive accept error
1821 * 4: Reserved
1822 * 5: Send illegal vector
1823 * 6: Received illegal vector
1824 * 7: Illegal register address
1825 */
1826 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001827 smp_processor_id(), v , v1);
1828 irq_exit();
1829}
1830
Glauber Costab5841762008-05-28 13:38:28 -03001831/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001832 * connect_bsp_APIC - attach the APIC to the interrupt system
1833 */
Glauber Costab5841762008-05-28 13:38:28 -03001834void __init connect_bsp_APIC(void)
1835{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001836#ifdef CONFIG_X86_32
1837 if (pic_mode) {
1838 /*
1839 * Do not trust the local APIC being empty at bootup.
1840 */
1841 clear_local_APIC();
1842 /*
1843 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1844 * local APIC to INT and NMI lines.
1845 */
1846 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1847 "enabling APIC mode.\n");
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +04001848 imcr_pic_to_apic();
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001849 }
1850#endif
Ingo Molnar49040332009-01-28 12:43:18 +01001851 if (apic->enable_apic_mode)
1852 apic->enable_apic_mode();
Glauber Costab5841762008-05-28 13:38:28 -03001853}
1854
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001855/**
1856 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1857 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1858 *
1859 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1860 * APIC is disabled.
1861 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001862void disconnect_bsp_APIC(int virt_wire_setup)
1863{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001864 unsigned int value;
1865
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001866#ifdef CONFIG_X86_32
1867 if (pic_mode) {
1868 /*
1869 * Put the board back into PIC mode (has an effect only on
1870 * certain older boards). Note that APIC interrupts, including
1871 * IPIs, won't work beyond this point! The only exception are
1872 * INIT IPIs.
1873 */
1874 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1875 "entering PIC mode.\n");
Cyrill Gorcunovc0eaa452009-04-12 20:47:40 +04001876 imcr_apic_to_pic();
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001877 return;
1878 }
1879#endif
1880
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001881 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001882
1883 /* For the spurious interrupt use vector F, and enable it */
1884 value = apic_read(APIC_SPIV);
1885 value &= ~APIC_VECTOR_MASK;
1886 value |= APIC_SPIV_APIC_ENABLED;
1887 value |= 0xf;
1888 apic_write(APIC_SPIV, value);
1889
1890 if (!virt_wire_setup) {
1891 /*
1892 * For LVT0 make it edge triggered, active high,
1893 * external and enabled
1894 */
1895 value = apic_read(APIC_LVT0);
1896 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1897 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1898 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1899 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1900 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1901 apic_write(APIC_LVT0, value);
1902 } else {
1903 /* Disable LVT0 */
1904 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1905 }
1906
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001907 /*
1908 * For LVT1 make it edge triggered, active high,
1909 * nmi and enabled
1910 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001911 value = apic_read(APIC_LVT1);
1912 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1913 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1914 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1915 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1916 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1917 apic_write(APIC_LVT1, value);
1918}
1919
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001920void __cpuinit generic_processor_info(int apicid, int version)
1921{
1922 int cpu;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001923
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001924 /*
1925 * Validate version
1926 */
1927 if (version == 0x0) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001928 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
Mike Travis3b11ce72008-12-17 15:21:39 -08001929 "fixing up to 0x10. (tell your hw vendor)\n",
1930 version);
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001931 version = 0x10;
1932 }
1933 apic_version[apicid] = version;
1934
Mike Travis3b11ce72008-12-17 15:21:39 -08001935 if (num_processors >= nr_cpu_ids) {
1936 int max = nr_cpu_ids;
1937 int thiscpu = max + disabled_cpus;
1938
1939 pr_warning(
1940 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1941 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1942
1943 disabled_cpus++;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001944 return;
1945 }
1946
1947 num_processors++;
Mike Travis3b11ce72008-12-17 15:21:39 -08001948 cpu = cpumask_next_zero(-1, cpu_present_mask);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001949
Mike Travisb2b815d2009-01-16 15:22:16 -08001950 if (version != apic_version[boot_cpu_physical_apicid])
1951 WARN_ONCE(1,
1952 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1953 apic_version[boot_cpu_physical_apicid], cpu, version);
1954
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001955 physid_set(apicid, phys_cpu_present_map);
1956 if (apicid == boot_cpu_physical_apicid) {
1957 /*
1958 * x86_bios_cpu_apicid is required to have processors listed
1959 * in same order as logical cpu numbers. Hence the first
1960 * entry is BSP, and so on.
1961 */
1962 cpu = 0;
1963 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001964 if (apicid > max_physical_apicid)
1965 max_physical_apicid = apicid;
1966
Ingo Molnar3e5095d2009-01-27 17:07:08 +01001967#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
Tejun Heof10fcd42009-01-13 20:41:34 +09001968 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1969 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001970#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001971
Mike Travis1de88cd2008-12-16 17:34:02 -08001972 set_cpu_possible(cpu, true);
1973 set_cpu_present(cpu, true);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001974}
1975
Suresh Siddha0c81c742008-07-10 11:16:48 -07001976int hard_smp_processor_id(void)
1977{
1978 return read_apic_id();
1979}
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +01001980
1981void default_init_apic_ldr(void)
1982{
1983 unsigned long val;
1984
1985 apic_write(APIC_DFR, APIC_DFR_VALUE);
1986 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
1987 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1988 apic_write(APIC_LDR, val);
1989}
1990
1991#ifdef CONFIG_X86_32
1992int default_apicid_to_node(int logical_apicid)
1993{
1994#ifdef CONFIG_SMP
1995 return apicid_2_node[hard_smp_processor_id()];
1996#else
1997 return 0;
1998#endif
1999}
Yinghai Lu34919982008-08-24 02:01:48 -07002000#endif
Suresh Siddha0c81c742008-07-10 11:16:48 -07002001
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002002/*
2003 * Power management
2004 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005#ifdef CONFIG_PM
2006
2007static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002008 /*
2009 * 'active' is true if the local APIC was enabled by us and
2010 * not the BIOS; this signifies that we are also responsible
2011 * for disabling it before entering apm/acpi suspend
2012 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int active;
2014 /* r/w apic fields */
2015 unsigned int apic_id;
2016 unsigned int apic_taskpri;
2017 unsigned int apic_ldr;
2018 unsigned int apic_dfr;
2019 unsigned int apic_spiv;
2020 unsigned int apic_lvtt;
2021 unsigned int apic_lvtpc;
2022 unsigned int apic_lvt0;
2023 unsigned int apic_lvt1;
2024 unsigned int apic_lvterr;
2025 unsigned int apic_tmict;
2026 unsigned int apic_tdcr;
2027 unsigned int apic_thmr;
2028} apic_pm_state;
2029
Pavel Machek0b9c33a2005-04-16 15:25:31 -07002030static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031{
2032 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002033 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 if (!apic_pm_state.active)
2036 return 0;
2037
Thomas Gleixner37e650c2008-01-30 13:30:14 +01002038 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01002039
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07002040 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2042 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2043 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2044 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2045 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01002046 if (maxlvt >= 4)
2047 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2049 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2050 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2051 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2052 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Andi Kleen4efc0672009-04-28 19:07:31 +02002053#ifdef CONFIG_X86_THERMAL_VECTOR
Karsten Wiesef990fff2006-12-07 02:14:11 +01002054 if (maxlvt >= 5)
2055 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2056#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04002057
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02002058 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 disable_local_APIC();
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002060
Fenghua Yub24696b2009-03-27 14:22:44 -07002061 if (intr_remapping_enabled)
2062 disable_intr_remapping();
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 local_irq_restore(flags);
2065 return 0;
2066}
2067
2068static int lapic_resume(struct sys_device *dev)
2069{
2070 unsigned int l, h;
2071 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002072 int maxlvt;
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002073 int ret = 0;
Fenghua Yub24696b2009-03-27 14:22:44 -07002074 struct IO_APIC_route_entry **ioapic_entries = NULL;
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 if (!apic_pm_state.active)
2077 return 0;
2078
Fenghua Yub24696b2009-03-27 14:22:44 -07002079 local_irq_save(flags);
Weidong Han9a2755c2009-04-17 16:42:16 +08002080 if (intr_remapping_enabled) {
Fenghua Yub24696b2009-03-27 14:22:44 -07002081 ioapic_entries = alloc_ioapic_entries();
2082 if (!ioapic_entries) {
2083 WARN(1, "Alloc ioapic_entries in lapic resume failed.");
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002084 ret = -ENOMEM;
2085 goto restore;
Fenghua Yub24696b2009-03-27 14:22:44 -07002086 }
2087
2088 ret = save_IO_APIC_setup(ioapic_entries);
2089 if (ret) {
2090 WARN(1, "Saving IO-APIC state failed: %d\n", ret);
2091 free_ioapic_entries(ioapic_entries);
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002092 goto restore;
Fenghua Yub24696b2009-03-27 14:22:44 -07002093 }
2094
2095 mask_IO_APIC_setup(ioapic_entries);
Jacob Panb81bb372009-11-09 11:27:04 -08002096 legacy_pic->mask_all();
Fenghua Yub24696b2009-03-27 14:22:44 -07002097 }
Karsten Wiesef990fff2006-12-07 02:14:11 +01002098
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002099 if (x2apic_mode)
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002100 enable_x2apic();
Suresh Siddhacf6567f2009-03-16 17:05:00 -07002101 else {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002102 /*
2103 * Make sure the APICBASE points to the right address
2104 *
2105 * FIXME! This will be wrong if we ever support suspend on
2106 * SMP! We'll need to do this as part of the CPU restore!
2107 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002108 rdmsr(MSR_IA32_APICBASE, l, h);
2109 l &= ~MSR_IA32_APICBASE_BASE;
2110 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2111 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07002112 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002113
Fenghua Yub24696b2009-03-27 14:22:44 -07002114 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2116 apic_write(APIC_ID, apic_pm_state.apic_id);
2117 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2118 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2119 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2120 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2121 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2122 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002123#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002124 if (maxlvt >= 5)
2125 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2126#endif
2127 if (maxlvt >= 4)
2128 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2130 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2131 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2132 apic_write(APIC_ESR, 0);
2133 apic_read(APIC_ESR);
2134 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2135 apic_write(APIC_ESR, 0);
2136 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002137
Weidong Han9a2755c2009-04-17 16:42:16 +08002138 if (intr_remapping_enabled) {
Suresh Siddhafc1edaf2009-04-20 13:02:27 -07002139 reenable_intr_remapping(x2apic_mode);
Jacob Panb81bb372009-11-09 11:27:04 -08002140 legacy_pic->restore_mask();
Fenghua Yub24696b2009-03-27 14:22:44 -07002141 restore_IO_APIC_setup(ioapic_entries);
2142 free_ioapic_entries(ioapic_entries);
2143 }
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002144restore:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002146
Jiri Slaby3d58829b2009-05-28 09:54:47 +02002147 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148}
2149
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002150/*
2151 * This device has no shutdown method - fully functioning local APICs
2152 * are needed on every CPU up until machine_halt/restart/poweroff.
2153 */
2154
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002156 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 .resume = lapic_resume,
2158 .suspend = lapic_suspend,
2159};
2160
2161static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002162 .id = 0,
2163 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164};
2165
Ashok Raje6982c62005-06-25 14:54:58 -07002166static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 apic_pm_state.active = 1;
2169}
2170
2171static int __init init_lapic_sysfs(void)
2172{
2173 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 if (!cpu_has_apic)
2176 return 0;
2177 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 error = sysdev_class_register(&lapic_sysclass);
2180 if (!error)
2181 error = sysdev_register(&device_lapic);
2182 return error;
2183}
Fenghua Yub24696b2009-03-27 14:22:44 -07002184
2185/* local apic needs to resume before other devices access its registers. */
2186core_initcall(init_lapic_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188#else /* CONFIG_PM */
2189
2190static void apic_pm_activate(void) { }
2191
2192#endif /* CONFIG_PM */
2193
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002194#ifdef CONFIG_X86_64
Yinghai Lue0e42142009-04-26 23:39:38 -07002195
2196static int __cpuinit apic_cluster_num(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 int i, clusters, zeros;
2199 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08002200 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2202
Mike Travis23ca4bb2008-05-12 21:21:12 +02002203 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07002204 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Mike Travis168ef542008-12-16 17:34:01 -08002206 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002207 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01002208 if (bios_cpu_apicid) {
2209 id = bios_cpu_apicid[i];
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302210 } else if (i < nr_cpu_ids) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002211 if (cpu_present(i))
2212 id = per_cpu(x86_bios_cpu_apicid, i);
2213 else
2214 continue;
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302215 } else
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002216 break;
2217
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (id != BAD_APICID)
2219 __set_bit(APIC_CLUSTERID(id), clustermap);
2220 }
2221
2222 /* Problem: Partially populated chassis may not have CPUs in some of
2223 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01002224 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2225 * Since clusters are allocated sequentially, count zeros only if
2226 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 */
2228 clusters = 0;
2229 zeros = 0;
2230 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2231 if (test_bit(i, clustermap)) {
2232 clusters += 1 + zeros;
2233 zeros = 0;
2234 } else
2235 ++zeros;
2236 }
2237
Yinghai Lue0e42142009-04-26 23:39:38 -07002238 return clusters;
2239}
2240
2241static int __cpuinitdata multi_checked;
2242static int __cpuinitdata multi;
2243
2244static int __cpuinit set_multi(const struct dmi_system_id *d)
2245{
2246 if (multi)
2247 return 0;
Cyrill Gorcunov6f0aced2009-05-01 23:54:25 +04002248 pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
Yinghai Lue0e42142009-04-26 23:39:38 -07002249 multi = 1;
2250 return 0;
2251}
2252
2253static const __cpuinitconst struct dmi_system_id multi_dmi_table[] = {
2254 {
2255 .callback = set_multi,
2256 .ident = "IBM System Summit2",
2257 .matches = {
2258 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2259 DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2260 },
2261 },
2262 {}
2263};
2264
2265static void __cpuinit dmi_check_multi(void)
2266{
2267 if (multi_checked)
2268 return;
2269
2270 dmi_check_system(multi_dmi_table);
2271 multi_checked = 1;
2272}
2273
2274/*
2275 * apic_is_clustered_box() -- Check if we can expect good TSC
2276 *
2277 * Thus far, the major user of this is IBM's Summit2 series:
2278 * Clustered boxes may have unsynced TSC problems if they are
2279 * multi-chassis.
2280 * Use DMI to check them
2281 */
2282__cpuinit int apic_is_clustered_box(void)
2283{
2284 dmi_check_multi();
2285 if (multi)
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002286 return 1;
2287
Yinghai Lue0e42142009-04-26 23:39:38 -07002288 if (!is_vsmp_box())
2289 return 0;
2290
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 /*
Yinghai Lue0e42142009-04-26 23:39:38 -07002292 * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2293 * not guaranteed to be synced between boards
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 */
Yinghai Lue0e42142009-04-26 23:39:38 -07002295 if (apic_cluster_num() > 1)
2296 return 1;
2297
2298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002300#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
2302/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002303 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002305static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002306{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07002308 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002309 return 0;
2310}
2311early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002313/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002314static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002315{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002316 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002317}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002318early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Linus Torvalds2e7c2832007-03-23 11:32:31 -07002320static int __init parse_lapic_timer_c2_ok(char *arg)
2321{
2322 local_apic_timer_c2_ok = 1;
2323 return 0;
2324}
2325early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2326
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002327static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002328{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002330 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002331}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002332early_param("noapictimer", parse_disable_apic_timer);
2333
2334static int __init parse_nolapic_timer(char *arg)
2335{
2336 disable_apic_timer = 1;
2337 return 0;
2338}
2339early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01002340
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002341static int __init apic_set_verbosity(char *arg)
2342{
2343 if (!arg) {
2344#ifdef CONFIG_X86_64
2345 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002346 return 0;
2347#endif
2348 return -EINVAL;
2349 }
2350
2351 if (strcmp("debug", arg) == 0)
2352 apic_verbosity = APIC_DEBUG;
2353 else if (strcmp("verbose", arg) == 0)
2354 apic_verbosity = APIC_VERBOSE;
2355 else {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01002356 pr_warning("APIC Verbosity level %s not recognised"
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002357 " use apic=verbose or apic=debug\n", arg);
2358 return -EINVAL;
2359 }
2360
2361 return 0;
2362}
2363early_param("apic", apic_set_verbosity);
2364
Yinghai Lu1e934dd2008-02-22 13:37:26 -08002365static int __init lapic_insert_resource(void)
2366{
2367 if (!apic_phys)
2368 return -1;
2369
2370 /* Put local APIC into the resource map. */
2371 lapic_resource.start = apic_phys;
2372 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2373 insert_resource(&iomem_resource, &lapic_resource);
2374
2375 return 0;
2376}
2377
2378/*
2379 * need call insert after e820_reserve_resources()
2380 * that is using request_resource
2381 */
2382late_initcall(lapic_insert_resource);