blob: 004aa1c31e4f42e20ee4b7a45ee47d3a8b02836f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Local APIC handling, local APIC timers
3 *
Ingo Molnar8f47e162009-01-31 02:03:42 +01004 * (c) 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kernel_stat.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010018#include <linux/mc146818rtc.h>
Thomas Gleixner70a20022008-01-30 13:30:18 +010019#include <linux/acpi_pmtmr.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010020#include <linux/clockchips.h>
21#include <linux/interrupt.h>
22#include <linux/bootmem.h>
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +010023#include <linux/ftrace.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010024#include <linux/ioport.h>
25#include <linux/module.h>
26#include <linux/sysdev.h>
27#include <linux/delay.h>
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +053028#include <linux/timex.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010029#include <linux/dmar.h>
30#include <linux/init.h>
31#include <linux/cpu.h>
32#include <linux/dmi.h>
33#include <linux/nmi.h>
34#include <linux/smp.h>
35#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Yinghai Lu773763d2008-08-24 02:01:52 -070037#include <asm/arch_hooks.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/pgalloc.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010039#include <asm/genapic.h>
40#include <asm/atomic.h>
41#include <asm/mpspec.h>
Yinghai Lu773763d2008-08-24 02:01:52 -070042#include <asm/i8253.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010043#include <asm/i8259.h>
Andi Kleen73dea472006-02-03 21:50:50 +010044#include <asm/proto.h>
Andi Kleen2c8c0e62006-09-26 10:52:32 +020045#include <asm/apic.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010046#include <asm/desc.h>
47#include <asm/hpet.h>
48#include <asm/idle.h>
49#include <asm/mtrr.h>
Jaswinder Singh Rajput2bc13792009-01-11 20:34:47 +053050#include <asm/smp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Brian Gerstec70de82009-01-27 12:56:47 +090052unsigned int num_processors;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010053
Brian Gerstec70de82009-01-27 12:56:47 +090054unsigned disabled_cpus __cpuinitdata;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010055
Brian Gerstec70de82009-01-27 12:56:47 +090056/* Processor that is doing the boot up */
57unsigned int boot_cpu_physical_apicid = -1U;
Glauber Costa5af55732008-03-25 13:28:56 -030058
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070059/*
Ingo Molnarfdbecd92009-01-31 03:57:12 +010060 * The highest APIC ID seen during enumeration.
61 *
62 * This determines the messaging protocol we can use: if all APIC IDs
63 * are in the 0 ... 7 range, then we can use logical addressing which
64 * has some performance advantages (better broadcasting).
65 *
66 * If there's an APIC ID above 8, we use physical addressing.
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070067 */
Brian Gerstec70de82009-01-27 12:56:47 +090068unsigned int max_physical_apicid;
69
Ingo Molnarfdbecd92009-01-31 03:57:12 +010070/*
71 * Bitmask of physically existing CPUs:
72 */
Brian Gerstec70de82009-01-27 12:56:47 +090073physid_mask_t phys_cpu_present_map;
74
75/*
76 * Map cpu index to physical APIC ID
77 */
78DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
79DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
80EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
81EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070082
Yinghai Lub3c51172008-08-24 02:01:46 -070083#ifdef CONFIG_X86_32
84/*
85 * Knob to control our willingness to enable the local APIC.
86 *
87 * +1=force-enable
88 */
89static int force_enable_local_apic;
90/*
91 * APIC command line parameters
92 */
93static int __init parse_lapic(char *arg)
94{
95 force_enable_local_apic = 1;
96 return 0;
97}
98early_param("lapic", parse_lapic);
Yinghai Luf28c0ae2008-08-24 02:01:49 -070099/* Local APIC was disabled by the BIOS and enabled by the kernel */
100static int enabled_via_apicbase;
101
Yinghai Lub3c51172008-08-24 02:01:46 -0700102#endif
103
104#ifdef CONFIG_X86_64
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200105static int apic_calibrate_pmtmr __initdata;
Yinghai Lub3c51172008-08-24 02:01:46 -0700106static __init int setup_apicpmtimer(char *s)
107{
108 apic_calibrate_pmtmr = 1;
109 notsc_setup(NULL);
110 return 0;
111}
112__setup("apicpmtimer", setup_apicpmtimer);
113#endif
114
Yinghai Lu06cd9a72009-02-16 17:29:58 -0800115#ifdef CONFIG_X86_X2APIC
Suresh Siddha89027d32008-07-10 11:16:56 -0700116int x2apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -0700117/* x2apic enabled before OS handover */
Jaswinder Singhb6b301a2008-12-23 21:52:33 +0530118static int x2apic_preenabled;
119static int disable_x2apic;
Yinghai Lu49899ea2008-08-24 02:01:47 -0700120static __init int setup_nox2apic(char *str)
121{
122 disable_x2apic = 1;
123 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
124 return 0;
125}
126early_param("nox2apic", setup_nox2apic);
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Yinghai Lub3c51172008-08-24 02:01:46 -0700129unsigned long mp_lapic_addr;
130int disable_apic;
131/* Disable local APIC timer from the kernel commandline or via dmi quirk */
132static int disable_apic_timer __cpuinitdata;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100133/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -0700134int local_apic_timer_c2_ok;
135EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
136
Yinghai Luefa25592008-08-19 20:50:36 -0700137int first_system_vector = 0xfe;
138
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100139/*
140 * Debug level, exported for io_apic.c
141 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +0100142unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100143
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -0700144int pic_mode;
145
Alexey Starikovskiybab4b272008-05-19 19:47:03 +0400146/* Have we found an MP table */
147int smp_found_config;
148
Aaron Durbin39928722006-12-07 02:14:01 +0100149static struct resource lapic_resource = {
150 .name = "Local APIC",
151 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
152};
153
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200154static unsigned int calibration_result;
155
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200156static int lapic_next_event(unsigned long delta,
157 struct clock_event_device *evt);
158static void lapic_timer_setup(enum clock_event_mode mode,
159 struct clock_event_device *evt);
Mike Travis96289372008-12-31 18:08:46 -0800160static void lapic_timer_broadcast(const struct cpumask *mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100161static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200162
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400163/*
164 * The local apic timer can be used for any function which is CPU local.
165 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200166static struct clock_event_device lapic_clockevent = {
167 .name = "lapic",
168 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
169 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
170 .shift = 32,
171 .set_mode = lapic_timer_setup,
172 .set_next_event = lapic_next_event,
173 .broadcast = lapic_timer_broadcast,
174 .rating = 100,
175 .irq = -1,
176};
177static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
178
Andi Kleend3432892008-01-30 13:33:17 +0100179static unsigned long apic_phys;
180
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100181/*
182 * Get the LAPIC version
183 */
184static inline int lapic_get_version(void)
185{
186 return GET_APIC_VERSION(apic_read(APIC_LVR));
187}
188
189/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400190 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100191 */
192static inline int lapic_is_integrated(void)
193{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400194#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100195 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400196#else
197 return APIC_INTEGRATED(lapic_get_version());
198#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100199}
200
201/*
202 * Check, whether this is a modern or a first generation APIC
203 */
204static int modern_apic(void)
205{
206 /* AMD systems use old APIC versions, so check the CPU */
207 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
208 boot_cpu_data.x86 >= 0xf)
209 return 1;
210 return lapic_get_version() >= 0x14;
211}
212
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400213/*
214 * Paravirt kernels also might be using these below ops. So we still
215 * use generic apic_read()/apic_write(), which might be pointing to different
216 * ops in PARAVIRT case.
217 */
Suresh Siddha1b374e42008-07-10 11:16:49 -0700218void xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100219{
220 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
221 cpu_relax();
222}
223
Suresh Siddha1b374e42008-07-10 11:16:49 -0700224u32 safe_xapic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100225{
226 u32 send_status;
227 int timeout;
228
229 timeout = 0;
230 do {
231 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
232 if (!send_status)
233 break;
234 udelay(100);
235 } while (timeout++ < 1000);
236
237 return send_status;
238}
239
Suresh Siddha1b374e42008-07-10 11:16:49 -0700240void xapic_icr_write(u32 low, u32 id)
241{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200242 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700243 apic_write(APIC_ICR, low);
244}
245
Jaswinder Singh Rajputec8c8422008-12-30 22:46:36 +0530246static u64 xapic_icr_read(void)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700247{
248 u32 icr1, icr2;
249
250 icr2 = apic_read(APIC_ICR2);
251 icr1 = apic_read(APIC_ICR);
252
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400253 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700254}
255
256static struct apic_ops xapic_ops = {
257 .read = native_apic_mem_read,
258 .write = native_apic_mem_write,
Suresh Siddha1b374e42008-07-10 11:16:49 -0700259 .icr_read = xapic_icr_read,
260 .icr_write = xapic_icr_write,
261 .wait_icr_idle = xapic_wait_icr_idle,
262 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
263};
264
265struct apic_ops __read_mostly *apic_ops = &xapic_ops;
Suresh Siddha1b374e42008-07-10 11:16:49 -0700266EXPORT_SYMBOL_GPL(apic_ops);
267
Yinghai Lu06cd9a72009-02-16 17:29:58 -0800268#ifdef CONFIG_X86_X2APIC
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700269static void x2apic_wait_icr_idle(void)
270{
271 /* no need to wait for icr idle in x2apic */
272 return;
273}
274
275static u32 safe_x2apic_wait_icr_idle(void)
276{
277 /* no need to wait for icr idle in x2apic */
278 return 0;
279}
280
281void x2apic_icr_write(u32 low, u32 id)
282{
283 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
284}
285
Jaswinder Singh Rajputec8c8422008-12-30 22:46:36 +0530286static u64 x2apic_icr_read(void)
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700287{
288 unsigned long val;
289
290 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
291 return val;
292}
293
294static struct apic_ops x2apic_ops = {
295 .read = native_apic_msr_read,
296 .write = native_apic_msr_write,
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700297 .icr_read = x2apic_icr_read,
298 .icr_write = x2apic_icr_write,
299 .wait_icr_idle = x2apic_wait_icr_idle,
300 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
301};
Yinghai Lu49899ea2008-08-24 02:01:47 -0700302#endif
Suresh Siddha13c88fb52008-07-10 11:16:52 -0700303
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100304/**
305 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
306 */
Jan Beuliche9427102008-01-30 13:31:24 +0100307void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100308{
309 unsigned int v;
310
311 /* unmask and set to NMI */
312 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200313
314 /* Level triggered for 82489DX (32bit mode) */
315 if (!lapic_is_integrated())
316 v |= APIC_LVT_LEVEL_TRIGGER;
317
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100318 apic_write(APIC_LVT0, v);
319}
320
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700321#ifdef CONFIG_X86_32
322/**
323 * get_physical_broadcast - Get number of physical broadcast IDs
324 */
325int get_physical_broadcast(void)
326{
327 return modern_apic() ? 0xff : 0xf;
328}
329#endif
330
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100331/**
332 * lapic_get_maxlvt - get the maximum number of local vector table entries
333 */
334int lapic_get_maxlvt(void)
335{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200336 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100337
338 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200339 /*
340 * - we always have APIC integrated on 64bit mode
341 * - 82489DXs do not report # of LVT entries
342 */
343 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100344}
345
346/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400347 * Local APIC timer
348 */
349
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400350/* Clock divisor */
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400351#define APIC_DIVISOR 16
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200352
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100353/*
354 * This function sets up the local APIC timer, with a timeout of
355 * 'clocks' APIC bus clock. During calibration we actually call
356 * this function twice on the boot CPU, once with a bogus timeout
357 * value, second time for real. The other (noncalibrating) CPUs
358 * call this function only once, with the real, calibrated value.
359 *
360 * We do reads before writes even if unnecessary, to get around the
361 * P5 APIC double write bug.
362 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100363static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
364{
365 unsigned int lvtt_value, tmp_value;
366
367 lvtt_value = LOCAL_TIMER_VECTOR;
368 if (!oneshot)
369 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200370 if (!lapic_is_integrated())
371 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
372
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100373 if (!irqen)
374 lvtt_value |= APIC_LVT_MASKED;
375
376 apic_write(APIC_LVTT, lvtt_value);
377
378 /*
379 * Divide PICLK by 16
380 */
381 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400382 apic_write(APIC_TDCR,
383 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
384 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100385
386 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200387 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100388}
389
390/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100391 * Setup extended LVT, AMD specific (K8, family 10h)
392 *
393 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
394 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Robert Richter286f5712008-07-22 21:08:46 +0200395 *
396 * If mask=1, the LVT entry does not generate interrupts while mask=0
397 * enables the vector. See also the BKDGs.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100398 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100399
400#define APIC_EILVT_LVTOFF_MCE 0
401#define APIC_EILVT_LVTOFF_IBS 1
402
403static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100404{
Robert Richter7b83dae2008-01-30 13:30:40 +0100405 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100406 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
407
408 apic_write(reg, v);
409}
410
Robert Richter7b83dae2008-01-30 13:30:40 +0100411u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
412{
413 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
414 return APIC_EILVT_LVTOFF_MCE;
415}
416
417u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
418{
419 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
420 return APIC_EILVT_LVTOFF_IBS;
421}
Robert Richter6aa360e2008-07-23 15:28:14 +0200422EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100423
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100424/*
425 * Program the next event, relative to now
426 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200427static int lapic_next_event(unsigned long delta,
428 struct clock_event_device *evt)
429{
430 apic_write(APIC_TMICT, delta);
431 return 0;
432}
433
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100434/*
435 * Setup the lapic timer in periodic or oneshot mode
436 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200437static void lapic_timer_setup(enum clock_event_mode mode,
438 struct clock_event_device *evt)
439{
440 unsigned long flags;
441 unsigned int v;
442
443 /* Lapic used as dummy for broadcast ? */
444 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
445 return;
446
447 local_irq_save(flags);
448
449 switch (mode) {
450 case CLOCK_EVT_MODE_PERIODIC:
451 case CLOCK_EVT_MODE_ONESHOT:
452 __setup_APIC_LVTT(calibration_result,
453 mode != CLOCK_EVT_MODE_PERIODIC, 1);
454 break;
455 case CLOCK_EVT_MODE_UNUSED:
456 case CLOCK_EVT_MODE_SHUTDOWN:
457 v = apic_read(APIC_LVTT);
458 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
459 apic_write(APIC_LVTT, v);
Thomas Gleixnera98f8fd2008-11-06 01:13:39 +0100460 apic_write(APIC_TMICT, 0xffffffff);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200461 break;
462 case CLOCK_EVT_MODE_RESUME:
463 /* Nothing to do here */
464 break;
465 }
466
467 local_irq_restore(flags);
468}
469
470/*
471 * Local APIC timer broadcast function
472 */
Mike Travis96289372008-12-31 18:08:46 -0800473static void lapic_timer_broadcast(const struct cpumask *mask)
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200474{
475#ifdef CONFIG_SMP
Ingo Molnardac5f412009-01-28 15:42:24 +0100476 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200477#endif
478}
479
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100480/*
481 * Setup the local APIC timer for this CPU. Copy the initilized values
482 * of the boot CPU and register the clock event in the framework.
483 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700484static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200485{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100486 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
487
488 memcpy(levt, &lapic_clockevent, sizeof(*levt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030489 levt->cpumask = cpumask_of(smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100490
491 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200492}
493
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700494/*
495 * In this functions we calibrate APIC bus clocks to the external timer.
496 *
497 * We want to do the calibration only once since we want to have local timer
498 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
499 * frequency.
500 *
501 * This was previously done by reading the PIT/HPET and waiting for a wrap
502 * around to find out, that a tick has elapsed. I have a box, where the PIT
503 * readout is broken, so it never gets out of the wait loop again. This was
504 * also reported by others.
505 *
506 * Monitoring the jiffies value is inaccurate and the clockevents
507 * infrastructure allows us to do a simple substitution of the interrupt
508 * handler.
509 *
510 * The calibration routine also uses the pm_timer when possible, as the PIT
511 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
512 * back to normal later in the boot process).
513 */
514
515#define LAPIC_CAL_LOOPS (HZ/10)
516
517static __initdata int lapic_cal_loops = -1;
518static __initdata long lapic_cal_t1, lapic_cal_t2;
519static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
520static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
521static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
522
523/*
524 * Temporary interrupt handler.
525 */
526static void __init lapic_cal_handler(struct clock_event_device *dev)
527{
528 unsigned long long tsc = 0;
529 long tapic = apic_read(APIC_TMCCT);
530 unsigned long pm = acpi_pm_read_early();
531
532 if (cpu_has_tsc)
533 rdtscll(tsc);
534
535 switch (lapic_cal_loops++) {
536 case 0:
537 lapic_cal_t1 = tapic;
538 lapic_cal_tsc1 = tsc;
539 lapic_cal_pm1 = pm;
540 lapic_cal_j1 = jiffies;
541 break;
542
543 case LAPIC_CAL_LOOPS:
544 lapic_cal_t2 = tapic;
545 lapic_cal_tsc2 = tsc;
546 if (pm < lapic_cal_pm1)
547 pm += ACPI_PM_OVRRUN;
548 lapic_cal_pm2 = pm;
549 lapic_cal_j2 = jiffies;
550 break;
551 }
552}
553
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900554static int __init
555calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400556{
557 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
558 const long pm_thresh = pm_100ms / 100;
559 unsigned long mult;
560 u64 res;
561
562#ifndef CONFIG_X86_PM_TIMER
563 return -1;
564#endif
565
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900566 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400567
568 /* Check, if the PM timer is available */
569 if (!deltapm)
570 return -1;
571
572 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
573
574 if (deltapm > (pm_100ms - pm_thresh) &&
575 deltapm < (pm_100ms + pm_thresh)) {
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900576 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900577 return 0;
578 }
579
580 res = (((u64)deltapm) * mult) >> 22;
581 do_div(res, 1000000);
582 pr_warning("APIC calibration not consistent "
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900583 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900584
585 /* Correct the lapic counter value */
586 res = (((u64)(*delta)) * pm_100ms);
587 do_div(res, deltapm);
588 pr_info("APIC delta adjusted to PM-Timer: "
589 "%lu (%ld)\n", (unsigned long)res, *delta);
590 *delta = (long)res;
591
592 /* Correct the tsc counter value */
593 if (cpu_has_tsc) {
594 res = (((u64)(*deltatsc)) * pm_100ms);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400595 do_div(res, deltapm);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900596 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
597 "PM-Timer: %lu (%ld) \n",
598 (unsigned long)res, *deltatsc);
599 *deltatsc = (long)res;
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400600 }
601
602 return 0;
603}
604
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700605static int __init calibrate_APIC_clock(void)
606{
607 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700608 void (*real_handler)(struct clock_event_device *dev);
609 unsigned long deltaj;
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900610 long delta, deltatsc;
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700611 int pm_referenced = 0;
612
613 local_irq_disable();
614
615 /* Replace the global interrupt handler */
616 real_handler = global_clock_event->event_handler;
617 global_clock_event->event_handler = lapic_cal_handler;
618
619 /*
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400620 * Setup the APIC counter to maximum. There is no way the lapic
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700621 * can underflow in the 100ms detection time frame
622 */
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400623 __setup_APIC_LVTT(0xffffffff, 0, 0);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700624
625 /* Let the interrupts run */
626 local_irq_enable();
627
628 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
629 cpu_relax();
630
631 local_irq_disable();
632
633 /* Restore the real event handler */
634 global_clock_event->event_handler = real_handler;
635
636 /* Build delta t1-t2 as apic timer counts down */
637 delta = lapic_cal_t1 - lapic_cal_t2;
638 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
639
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900640 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
641
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400642 /* we trust the PM based calibration if possible */
643 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900644 &delta, &deltatsc);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700645
646 /* Calculate the scaled math multiplication factor */
647 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
648 lapic_clockevent.shift);
649 lapic_clockevent.max_delta_ns =
650 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
651 lapic_clockevent.min_delta_ns =
652 clockevent_delta2ns(0xF, &lapic_clockevent);
653
654 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
655
656 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
657 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
658 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
659 calibration_result);
660
661 if (cpu_has_tsc) {
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700662 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
663 "%ld.%04ld MHz.\n",
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900664 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
665 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700666 }
667
668 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
669 "%u.%04u MHz.\n",
670 calibration_result / (1000000 / HZ),
671 calibration_result % (1000000 / HZ));
672
673 /*
674 * Do a sanity check on the APIC calibration result
675 */
676 if (calibration_result < (1000000 / HZ)) {
677 local_irq_enable();
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100678 pr_warning("APIC frequency too slow, disabling apic timer\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700679 return -1;
680 }
681
682 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
683
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400684 /*
685 * PM timer calibration failed or not turned on
686 * so lets try APIC timer based calibration
687 */
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700688 if (!pm_referenced) {
689 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
690
691 /*
692 * Setup the apic timer manually
693 */
694 levt->event_handler = lapic_cal_handler;
695 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
696 lapic_cal_loops = -1;
697
698 /* Let the interrupts run */
699 local_irq_enable();
700
701 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
702 cpu_relax();
703
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700704 /* Stop the lapic timer */
705 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
706
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700707 /* Jiffies delta */
708 deltaj = lapic_cal_j2 - lapic_cal_j1;
709 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
710
711 /* Check, if the jiffies result is consistent */
712 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
713 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
714 else
715 levt->features |= CLOCK_EVT_FEAT_DUMMY;
716 } else
717 local_irq_enable();
718
719 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +0530720 pr_warning("APIC timer disabled due to verification failure\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700721 return -1;
722 }
723
724 return 0;
725}
726
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100727/*
728 * Setup the boot APIC
729 *
730 * Calibrate and verify the result.
731 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100732void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100734 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400735 * The local apic timer can be disabled via the kernel
736 * commandline or from the CPU detection code. Register the lapic
737 * timer as a dummy clock event source on SMP systems, so the
738 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100739 */
740 if (disable_apic_timer) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100741 pr_info("Disabling APIC timer\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100742 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100743 if (num_possible_cpus() > 1) {
744 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100745 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100746 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100747 return;
748 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200749
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400750 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
751 "calibrating APIC timer ...\n");
752
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400753 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100754 /* No broadcast on UP ! */
755 if (num_possible_cpus() > 1)
756 setup_APIC_timer();
757 return;
758 }
759
760 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100761 * If nmi_watchdog is set to IO_APIC, we need the
762 * PIT/HPET going. Otherwise register lapic as a dummy
763 * device.
764 */
765 if (nmi_watchdog != NMI_IO_APIC)
766 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
767 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100768 pr_warning("APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200769 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100770
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400771 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100772 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100775void __cpuinit setup_secondary_APIC_clock(void)
776{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100777 setup_APIC_timer();
778}
779
780/*
781 * The guts of the apic timer interrupt
782 */
783static void local_apic_timer_interrupt(void)
784{
785 int cpu = smp_processor_id();
786 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
787
788 /*
789 * Normally we should not be here till LAPIC has been initialized but
790 * in some cases like kdump, its possible that there is a pending LAPIC
791 * timer interrupt from previous kernel's context and is delivered in
792 * new kernel the moment interrupts are enabled.
793 *
794 * Interrupts are enabled early and LAPIC is setup much later, hence
795 * its possible that when we get here evt->event_handler is NULL.
796 * Check for event_handler being NULL and discard the interrupt as
797 * spurious.
798 */
799 if (!evt->event_handler) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100800 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100801 /* Switch it off */
802 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
803 return;
804 }
805
806 /*
807 * the NMI deadlock-detector uses this.
808 */
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800809 inc_irq_stat(apic_timer_irqs);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100810
811 evt->event_handler(evt);
812}
813
814/*
815 * Local APIC timer interrupt. This is the most natural way for doing
816 * local interrupts, but local timer interrupts can be emulated by
817 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
818 *
819 * [ if a single-CPU system runs an SMP kernel then we call the local
820 * interrupt as well. Thus we cannot inline the local irq ... ]
821 */
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +0100822void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100823{
824 struct pt_regs *old_regs = set_irq_regs(regs);
825
826 /*
827 * NOTE! We'd better ACK the irq immediately,
828 * because timer handling can be slow.
829 */
830 ack_APIC_irq();
831 /*
832 * update_process_times() expects us to have done irq_enter().
833 * Besides, if we don't timer interrupts ignore the global
834 * interrupt lock, which is the WrongThing (tm) to do.
835 */
836 exit_idle();
837 irq_enter();
838 local_apic_timer_interrupt();
839 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400840
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100841 set_irq_regs(old_regs);
842}
843
844int setup_profiling_timer(unsigned int multiplier)
845{
846 return -EINVAL;
847}
848
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100849/*
850 * Local APIC start and shutdown
851 */
852
853/**
854 * clear_local_APIC - shutdown the local APIC
855 *
856 * This is called, when a CPU is disabled and before rebooting, so the state of
857 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
858 * leftovers during boot.
859 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860void clear_local_APIC(void)
861{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400862 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100863 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Andi Kleend3432892008-01-30 13:33:17 +0100865 /* APIC hasn't been mapped yet */
866 if (!apic_phys)
867 return;
868
869 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200871 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 * if the vector is zero. Mask LVTERR first to prevent this.
873 */
874 if (maxlvt >= 3) {
875 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100876 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878 /*
879 * Careful: we have to set masks only first to deassert
880 * any level-triggered sources.
881 */
882 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100883 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100885 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100887 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (maxlvt >= 4) {
889 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100890 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400893 /* lets not touch this if we didn't frob it */
894#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
895 if (maxlvt >= 5) {
896 v = apic_read(APIC_LVTTHMR);
897 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
898 }
899#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /*
901 * Clean APIC state for other OSs:
902 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100903 apic_write(APIC_LVTT, APIC_LVT_MASKED);
904 apic_write(APIC_LVT0, APIC_LVT_MASKED);
905 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100907 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100909 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400910
911 /* Integrated APIC (!82489DX) ? */
912 if (lapic_is_integrated()) {
913 if (maxlvt > 3)
914 /* Clear ESR due to Pentium errata 3AP and 11AP */
915 apic_write(APIC_ESR, 0);
916 apic_read(APIC_ESR);
917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100920/**
921 * disable_local_APIC - clear and disable the local APIC
922 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923void disable_local_APIC(void)
924{
925 unsigned int value;
926
Jan Beulich4a13ad02009-01-14 12:28:51 +0000927 /* APIC hasn't been mapped yet */
928 if (!apic_phys)
929 return;
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 clear_local_APIC();
932
933 /*
934 * Disable APIC (implies clearing of registers
935 * for 82489DX!).
936 */
937 value = apic_read(APIC_SPIV);
938 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100939 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400940
941#ifdef CONFIG_X86_32
942 /*
943 * When LAPIC was disabled by the BIOS and enabled by the kernel,
944 * restore the disabled state.
945 */
946 if (enabled_via_apicbase) {
947 unsigned int l, h;
948
949 rdmsr(MSR_IA32_APICBASE, l, h);
950 l &= ~MSR_IA32_APICBASE_ENABLE;
951 wrmsr(MSR_IA32_APICBASE, l, h);
952 }
953#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954}
955
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400956/*
957 * If Linux enabled the LAPIC against the BIOS default disable it down before
958 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
959 * not power-off. Additionally clear all LVT entries before disable_local_APIC
960 * for the case where Linux didn't enable the LAPIC.
961 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700962void lapic_shutdown(void)
963{
964 unsigned long flags;
965
966 if (!cpu_has_apic)
967 return;
968
969 local_irq_save(flags);
970
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400971#ifdef CONFIG_X86_32
972 if (!enabled_via_apicbase)
973 clear_local_APIC();
974 else
975#endif
976 disable_local_APIC();
977
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700978
979 local_irq_restore(flags);
980}
981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982/*
983 * This is to verify that we're looking at a real local APIC.
984 * Check these against your board if the CPUs aren't getting
985 * started for no apparent reason.
986 */
987int __init verify_local_APIC(void)
988{
989 unsigned int reg0, reg1;
990
991 /*
992 * The version register is read-only in a real APIC.
993 */
994 reg0 = apic_read(APIC_LVR);
995 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
996 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
997 reg1 = apic_read(APIC_LVR);
998 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
999
1000 /*
1001 * The two version reads above should print the same
1002 * numbers. If the second one is different, then we
1003 * poke at a non-APIC.
1004 */
1005 if (reg1 != reg0)
1006 return 0;
1007
1008 /*
1009 * Check if the version looks reasonably.
1010 */
1011 reg1 = GET_APIC_VERSION(reg0);
1012 if (reg1 == 0x00 || reg1 == 0xff)
1013 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001014 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (reg1 < 0x02 || reg1 == 0xff)
1016 return 0;
1017
1018 /*
1019 * The ID register is read/write in a real APIC.
1020 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001021 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001023 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001024 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1026 apic_write(APIC_ID, reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +01001027 if (reg1 != (reg0 ^ apic->apic_id_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 return 0;
1029
1030 /*
1031 * The next two are just to see if we have sane values.
1032 * They're only really relevant if we're in Virtual Wire
1033 * compatibility mode, but most boxes are anymore.
1034 */
1035 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001036 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 reg1 = apic_read(APIC_LVT1);
1038 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1039
1040 return 1;
1041}
1042
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001043/**
1044 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046void __init sync_Arb_IDs(void)
1047{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +02001048 /*
1049 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1050 * needed on AMD.
1051 */
1052 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return;
1054
1055 /*
1056 * Wait for idle.
1057 */
1058 apic_wait_icr_idle();
1059
1060 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +04001061 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1062 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * An initial setup of the virtual wire mode.
1067 */
1068void __init init_bsp_APIC(void)
1069{
Andi Kleen11a8e772006-01-11 22:46:51 +01001070 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
1073 * Don't do the setup now if we have a SMP BIOS as the
1074 * through-I/O-APIC virtual wire mode might be active.
1075 */
1076 if (smp_found_config || !cpu_has_apic)
1077 return;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
1080 * Do not trust the local APIC being empty at bootup.
1081 */
1082 clear_local_APIC();
1083
1084 /*
1085 * Enable APIC.
1086 */
1087 value = apic_read(APIC_SPIV);
1088 value &= ~APIC_VECTOR_MASK;
1089 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001090
1091#ifdef CONFIG_X86_32
1092 /* This bit is reserved on P4/Xeon and should be cleared */
1093 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1094 (boot_cpu_data.x86 == 15))
1095 value &= ~APIC_SPIV_FOCUS_DISABLED;
1096 else
1097#endif
1098 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001100 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 /*
1103 * Set up the virtual wire mode.
1104 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001105 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001107 if (!lapic_is_integrated()) /* 82489DX */
1108 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001109 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110}
1111
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001112static void __cpuinit lapic_setup_esr(void)
1113{
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001114 unsigned int oldvalue, value, maxlvt;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001115
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001116 if (!lapic_is_integrated()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001117 pr_info("No ESR for 82489DX.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001118 return;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001119 }
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001120
Ingo Molnar08125d32009-01-28 05:08:44 +01001121 if (apic->disable_esr) {
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001122 /*
1123 * Something untraceable is creating bad interrupts on
1124 * secondary quads ... for the moment, just leave the
1125 * ESR disabled - we can't do anything useful with the
1126 * errors anyway - mbligh
1127 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001128 pr_info("Leaving ESR disabled.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001129 return;
1130 }
1131
1132 maxlvt = lapic_get_maxlvt();
1133 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1134 apic_write(APIC_ESR, 0);
1135 oldvalue = apic_read(APIC_ESR);
1136
1137 /* enables sending errors */
1138 value = ERROR_APIC_VECTOR;
1139 apic_write(APIC_LVTERR, value);
1140
1141 /*
1142 * spec says clear errors after enabling vector.
1143 */
1144 if (maxlvt > 3)
1145 apic_write(APIC_ESR, 0);
1146 value = apic_read(APIC_ESR);
1147 if (value != oldvalue)
1148 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1149 "vector: 0x%08x after: 0x%08x\n",
1150 oldvalue, value);
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001151}
1152
1153
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001154/**
1155 * setup_local_APIC - setup the local APIC
1156 */
1157void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
Andi Kleen739f33b2008-01-30 13:30:40 +01001159 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001160 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Jan Beulichf1182632009-01-14 12:27:35 +00001162 if (disable_apic) {
Ingo Molnar65a4e572009-01-31 03:36:17 +01001163 arch_disable_smp_support();
Jan Beulichf1182632009-01-14 12:27:35 +00001164 return;
1165 }
1166
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001167#ifdef CONFIG_X86_32
1168 /* Pound the ESR really hard over the head with a big hammer - mbligh */
Ingo Molnar08125d32009-01-28 05:08:44 +01001169 if (lapic_is_integrated() && apic->disable_esr) {
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001170 apic_write(APIC_ESR, 0);
1171 apic_write(APIC_ESR, 0);
1172 apic_write(APIC_ESR, 0);
1173 apic_write(APIC_ESR, 0);
1174 }
1175#endif
1176
Jack Steinerac23d4e2008-03-28 14:12:16 -05001177 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 /*
1180 * Double-check whether this APIC is really registered.
1181 * This is meaningless in clustered apic mode, so we skip it.
1182 */
Ingo Molnar7ed248d2009-01-28 03:43:47 +01001183 if (!apic->apic_id_registered())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 BUG();
1185
1186 /*
1187 * Intel recommends to set DFR, LDR and TPR before enabling
1188 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1189 * document number 292116). So here it goes...
1190 */
Ingo Molnara5c43292009-01-28 06:50:47 +01001191 apic->init_apic_ldr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /*
1194 * Set Task Priority to 'accept all'. We never change this
1195 * later on.
1196 */
1197 value = apic_read(APIC_TASKPRI);
1198 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +01001199 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001202 * After a crash, we no longer service the interrupts and a pending
1203 * interrupt from previous kernel might still have ISR bit set.
1204 *
1205 * Most probably by now CPU has serviced that pending interrupt and
1206 * it might not have done the ack_APIC_irq() because it thought,
1207 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1208 * does not clear the ISR bit and cpu thinks it has already serivced
1209 * the interrupt. Hence a vector might get locked. It was noticed
1210 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1211 */
1212 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1213 value = apic_read(APIC_ISR + i*0x10);
1214 for (j = 31; j >= 0; j--) {
1215 if (value & (1<<j))
1216 ack_APIC_irq();
1217 }
1218 }
1219
1220 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 * Now that we are all set up, enable the APIC
1222 */
1223 value = apic_read(APIC_SPIV);
1224 value &= ~APIC_VECTOR_MASK;
1225 /*
1226 * Enable APIC
1227 */
1228 value |= APIC_SPIV_APIC_ENABLED;
1229
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001230#ifdef CONFIG_X86_32
1231 /*
1232 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1233 * certain networking cards. If high frequency interrupts are
1234 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1235 * entry is masked/unmasked at a high rate as well then sooner or
1236 * later IOAPIC line gets 'stuck', no more interrupts are received
1237 * from the device. If focus CPU is disabled then the hang goes
1238 * away, oh well :-(
1239 *
1240 * [ This bug can be reproduced easily with a level-triggered
1241 * PCI Ne2000 networking cards and PII/PIII processors, dual
1242 * BX chipset. ]
1243 */
1244 /*
1245 * Actually disabling the focus CPU check just makes the hang less
1246 * frequent as it makes the interrupt distributon model be more
1247 * like LRU than MRU (the short-term load is more even across CPUs).
1248 * See also the comment in end_level_ioapic_irq(). --macro
1249 */
1250
1251 /*
1252 * - enable focus processor (bit==0)
1253 * - 64bit mode always use processor focus
1254 * so no need to set it
1255 */
1256 value &= ~APIC_SPIV_FOCUS_DISABLED;
1257#endif
Andi Kleen3f14c742006-09-26 10:52:29 +02001258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 /*
1260 * Set spurious IRQ vector
1261 */
1262 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001263 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 /*
1266 * Set up LVT0, LVT1:
1267 *
1268 * set up through-local-APIC on the BP's LINT0. This is not
1269 * strictly necessary in pure symmetric-IO mode, but sometimes
1270 * we delegate interrupts to the 8259A.
1271 */
1272 /*
1273 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1274 */
1275 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001276 if (!smp_processor_id() && (pic_mode || !value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001278 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001279 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 } else {
1281 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001282 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001283 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001285 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
1287 /*
1288 * only the BP should see the LINT1 NMI signal, obviously.
1289 */
1290 if (!smp_processor_id())
1291 value = APIC_DM_NMI;
1292 else
1293 value = APIC_DM_NMI | APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001294 if (!lapic_is_integrated()) /* 82489DX */
1295 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001296 apic_write(APIC_LVT1, value);
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001297
Jack Steinerac23d4e2008-03-28 14:12:16 -05001298 preempt_enable();
Andi Kleen739f33b2008-01-30 13:30:40 +01001299}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Andi Kleen739f33b2008-01-30 13:30:40 +01001301void __cpuinit end_local_APIC_setup(void)
1302{
1303 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001304
1305#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001306 {
1307 unsigned int value;
1308 /* Disable the local apic timer */
1309 value = apic_read(APIC_LVTT);
1310 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1311 apic_write(APIC_LVTT, value);
1312 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001313#endif
1314
Don Zickusf2802e72006-09-26 10:52:26 +02001315 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 apic_pm_activate();
1317}
1318
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001319#ifdef CONFIG_X86_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001320void check_x2apic(void)
1321{
1322 int msr, msr2;
1323
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001324 if (!cpu_has_x2apic)
1325 return;
1326
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001327 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1328
1329 if (msr & X2APIC_ENABLE) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001330 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001331 x2apic_preenabled = x2apic = 1;
1332 apic_ops = &x2apic_ops;
1333 }
1334}
1335
1336void enable_x2apic(void)
1337{
1338 int msr, msr2;
1339
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001340 if (!x2apic)
1341 return;
1342
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001343 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1344 if (!(msr & X2APIC_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001345 pr_info("Enabling x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001346 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1347 }
1348}
1349
Al Viro2236d252008-11-22 17:37:34 +00001350void __init enable_IR_x2apic(void)
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001351{
1352#ifdef CONFIG_INTR_REMAP
1353 int ret;
1354 unsigned long flags;
1355
1356 if (!cpu_has_x2apic)
1357 return;
1358
1359 if (!x2apic_preenabled && disable_x2apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001360 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1361 "because of nox2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001362 return;
1363 }
1364
1365 if (x2apic_preenabled && disable_x2apic)
1366 panic("Bios already enabled x2apic, can't enforce nox2apic");
1367
1368 if (!x2apic_preenabled && skip_ioapic_setup) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001369 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1370 "because of skipping io-apic setup\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001371 return;
1372 }
1373
1374 ret = dmar_table_init();
1375 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001376 pr_info("dmar_table_init() failed with %d:\n", ret);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001377
1378 if (x2apic_preenabled)
1379 panic("x2apic enabled by bios. But IR enabling failed");
1380 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001381 pr_info("Not enabling x2apic,Intr-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001382 return;
1383 }
1384
1385 local_irq_save(flags);
1386 mask_8259A();
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001387
1388 ret = save_mask_IO_APIC_setup();
1389 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001390 pr_info("Saving IO-APIC state failed: %d\n", ret);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001391 goto end;
1392 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001393
1394 ret = enable_intr_remapping(1);
1395
1396 if (ret && x2apic_preenabled) {
1397 local_irq_restore(flags);
1398 panic("x2apic enabled by bios. But IR enabling failed");
1399 }
1400
1401 if (ret)
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001402 goto end_restore;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001403
1404 if (!x2apic) {
1405 x2apic = 1;
1406 apic_ops = &x2apic_ops;
1407 enable_x2apic();
1408 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001409
1410end_restore:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001411 if (ret)
1412 /*
1413 * IR enabling failed
1414 */
1415 restore_IO_APIC_setup();
1416 else
1417 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1418
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001419end:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001420 unmask_8259A();
1421 local_irq_restore(flags);
1422
1423 if (!ret) {
1424 if (!x2apic_preenabled)
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001425 pr_info("Enabled x2apic and interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001426 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001427 pr_info("Enabled Interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001428 } else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001429 pr_err("Failed to enable Interrupt-remapping and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001430#else
1431 if (!cpu_has_x2apic)
1432 return;
1433
1434 if (x2apic_preenabled)
1435 panic("x2apic enabled prior OS handover,"
1436 " enable CONFIG_INTR_REMAP");
1437
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001438 pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1439 " and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001440#endif
1441
1442 return;
1443}
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001444#endif /* CONFIG_X86_X2APIC */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001445
Yinghai Lube7a6562008-08-24 02:01:51 -07001446#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001447/*
1448 * Detect and enable local APICs on non-SMP boards.
1449 * Original code written by Keir Fraser.
1450 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1451 * not correctly set up (usually the APIC timer won't work etc.)
1452 */
1453static int __init detect_init_APIC(void)
1454{
1455 if (!cpu_has_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001456 pr_info("No local APIC present\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001457 return -1;
1458 }
1459
1460 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001461 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001462 return 0;
1463}
Yinghai Lube7a6562008-08-24 02:01:51 -07001464#else
1465/*
1466 * Detect and initialize APIC
1467 */
1468static int __init detect_init_APIC(void)
1469{
1470 u32 h, l, features;
1471
1472 /* Disabled by kernel option? */
1473 if (disable_apic)
1474 return -1;
1475
1476 switch (boot_cpu_data.x86_vendor) {
1477 case X86_VENDOR_AMD:
1478 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
Borislav Petkov85877062009-02-03 16:24:22 +01001479 (boot_cpu_data.x86 >= 15))
Yinghai Lube7a6562008-08-24 02:01:51 -07001480 break;
1481 goto no_apic;
1482 case X86_VENDOR_INTEL:
1483 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1484 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1485 break;
1486 goto no_apic;
1487 default:
1488 goto no_apic;
1489 }
1490
1491 if (!cpu_has_apic) {
1492 /*
1493 * Over-ride BIOS and try to enable the local APIC only if
1494 * "lapic" specified.
1495 */
1496 if (!force_enable_local_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001497 pr_info("Local APIC disabled by BIOS -- "
1498 "you can enable it with \"lapic\"\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001499 return -1;
1500 }
1501 /*
1502 * Some BIOSes disable the local APIC in the APIC_BASE
1503 * MSR. This can only be done in software for Intel P6 or later
1504 * and AMD K7 (Model > 1) or later.
1505 */
1506 rdmsr(MSR_IA32_APICBASE, l, h);
1507 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001508 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001509 l &= ~MSR_IA32_APICBASE_BASE;
1510 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1511 wrmsr(MSR_IA32_APICBASE, l, h);
1512 enabled_via_apicbase = 1;
1513 }
1514 }
1515 /*
1516 * The APIC feature bit should now be enabled
1517 * in `cpuid'
1518 */
1519 features = cpuid_edx(1);
1520 if (!(features & (1 << X86_FEATURE_APIC))) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001521 pr_warning("Could not enable APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001522 return -1;
1523 }
1524 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1525 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1526
1527 /* The BIOS may have set up the APIC at some other address */
1528 rdmsr(MSR_IA32_APICBASE, l, h);
1529 if (l & MSR_IA32_APICBASE_ENABLE)
1530 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1531
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001532 pr_info("Found and enabled local APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001533
1534 apic_pm_activate();
1535
1536 return 0;
1537
1538no_apic:
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001539 pr_info("No local APIC present or hardware disabled\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001540 return -1;
1541}
1542#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001543
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001544#ifdef CONFIG_X86_64
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001545void __init early_init_lapic_mapping(void)
1546{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001547 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001548
1549 /*
1550 * If no local APIC can be found then go out
1551 * : it means there is no mpatable and MADT
1552 */
1553 if (!smp_found_config)
1554 return;
1555
Thomas Gleixner431ee792008-05-12 15:43:35 +02001556 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001557
Thomas Gleixner431ee792008-05-12 15:43:35 +02001558 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001559 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001560 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001561
1562 /*
1563 * Fetch the APIC ID of the BSP in case we have a
1564 * default configuration (or the MP table is broken).
1565 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001566 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001567}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001568#endif
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001569
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001570/**
1571 * init_apic_mappings - initialize APIC mappings
1572 */
1573void __init init_apic_mappings(void)
1574{
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001575#ifdef CONFIG_X86_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001576 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001577 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001578 return;
1579 }
Yinghai Lu49899ea2008-08-24 02:01:47 -07001580#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001581
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001582 /*
1583 * If no local APIC can be found then set up a fake all
1584 * zeroes page to simulate the local APIC and another
1585 * one for the IO-APIC.
1586 */
1587 if (!smp_found_config && detect_init_APIC()) {
1588 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1589 apic_phys = __pa(apic_phys);
1590 } else
1591 apic_phys = mp_lapic_addr;
1592
1593 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Yinghai Lu79c09692008-09-07 17:58:57 -07001594 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001595 APIC_BASE, apic_phys);
1596
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001597 /*
1598 * Fetch the APIC ID of the BSP in case we have a
1599 * default configuration (or the MP table is broken).
1600 */
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001601 if (boot_cpu_physical_apicid == -1U)
1602 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001603}
1604
1605/*
1606 * This initializes the IO-APIC and APIC hardware if this is
1607 * a UP kernel.
1608 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001609int apic_version[MAX_APICS];
1610
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001611int __init APIC_init_uniprocessor(void)
1612{
1613 if (disable_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001614 pr_info("Apic disabled\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001615 return -1;
1616 }
Jan Beulichf1182632009-01-14 12:27:35 +00001617#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001618 if (!cpu_has_apic) {
1619 disable_apic = 1;
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001620 pr_info("Apic disabled by BIOS\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001621 return -1;
1622 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001623#else
1624 if (!smp_found_config && !cpu_has_apic)
1625 return -1;
1626
1627 /*
1628 * Complain if the BIOS pretends there is one.
1629 */
1630 if (!cpu_has_apic &&
1631 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001632 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1633 boot_cpu_physical_apicid);
Yinghai Lufa2bd352008-08-24 02:01:50 -07001634 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1635 return -1;
1636 }
1637#endif
1638
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001639 enable_IR_x2apic();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001640#ifdef CONFIG_X86_64
Ingo Molnar72ce0162009-01-28 06:50:47 +01001641 default_setup_apic_routing();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001642#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001643
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001644 verify_local_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001645 connect_bsp_APIC();
1646
Yinghai Lufa2bd352008-08-24 02:01:50 -07001647#ifdef CONFIG_X86_64
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001648 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Yinghai Lufa2bd352008-08-24 02:01:50 -07001649#else
1650 /*
1651 * Hack: In case of kdump, after a crash, kernel might be booting
1652 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1653 * might be zero if read from MP tables. Get it from LAPIC.
1654 */
1655# ifdef CONFIG_CRASH_DUMP
1656 boot_cpu_physical_apicid = read_apic_id();
1657# endif
1658#endif
1659 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001660 setup_local_APIC();
1661
Yinghai Lu88d0f552009-02-14 23:57:28 -08001662#ifdef CONFIG_X86_IO_APIC
Andi Kleen739f33b2008-01-30 13:30:40 +01001663 /*
1664 * Now enable IO-APICs, actually call clear_IO_APIC
Yinghai Lu98c061b2009-02-16 00:00:50 -08001665 * We need clear_IO_APIC before enabling error vector
Andi Kleen739f33b2008-01-30 13:30:40 +01001666 */
1667 if (!skip_ioapic_setup && nr_ioapics)
1668 enable_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001669#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001670
1671 end_local_APIC_setup();
1672
Yinghai Lufa2bd352008-08-24 02:01:50 -07001673#ifdef CONFIG_X86_IO_APIC
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001674 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1675 setup_IO_APIC();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001676 else {
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001677 nr_ioapics = 0;
Yinghai Lu98c061b2009-02-16 00:00:50 -08001678 localise_nmi_watchdog();
1679 }
1680#else
1681 localise_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001682#endif
1683
Yinghai Lufa2bd352008-08-24 02:01:50 -07001684 setup_boot_clock();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001685#ifdef CONFIG_X86_64
1686 check_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001687#endif
1688
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001689 return 0;
1690}
1691
1692/*
1693 * Local APIC interrupts
1694 */
1695
1696/*
1697 * This interrupt should _never_ happen with our APIC/SMP architecture
1698 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001699void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001700{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001701 u32 v;
1702
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001703 exit_idle();
1704 irq_enter();
1705 /*
1706 * Check if this really is a spurious interrupt and ACK it
1707 * if it is a vectored one. Just in case...
1708 * Spurious interrupts should not be ACKed.
1709 */
1710 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1711 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1712 ack_APIC_irq();
1713
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -08001714 inc_irq_stat(irq_spurious_count);
1715
Yinghai Ludc1528d2008-08-24 02:01:53 -07001716 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001717 pr_info("spurious APIC interrupt on CPU#%d, "
1718 "should never happen.\n", smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001719 irq_exit();
1720}
1721
1722/*
1723 * This interrupt should never happen with our APIC/SMP architecture
1724 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001725void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001726{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001727 u32 v, v1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001728
1729 exit_idle();
1730 irq_enter();
1731 /* First tickle the hardware, only then report what went on. -- REW */
1732 v = apic_read(APIC_ESR);
1733 apic_write(APIC_ESR, 0);
1734 v1 = apic_read(APIC_ESR);
1735 ack_APIC_irq();
1736 atomic_inc(&irq_err_count);
1737
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001738 /*
1739 * Here is what the APIC error bits mean:
1740 * 0: Send CS error
1741 * 1: Receive CS error
1742 * 2: Send accept error
1743 * 3: Receive accept error
1744 * 4: Reserved
1745 * 5: Send illegal vector
1746 * 6: Received illegal vector
1747 * 7: Illegal register address
1748 */
1749 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001750 smp_processor_id(), v , v1);
1751 irq_exit();
1752}
1753
Glauber Costab5841762008-05-28 13:38:28 -03001754/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001755 * connect_bsp_APIC - attach the APIC to the interrupt system
1756 */
Glauber Costab5841762008-05-28 13:38:28 -03001757void __init connect_bsp_APIC(void)
1758{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001759#ifdef CONFIG_X86_32
1760 if (pic_mode) {
1761 /*
1762 * Do not trust the local APIC being empty at bootup.
1763 */
1764 clear_local_APIC();
1765 /*
1766 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1767 * local APIC to INT and NMI lines.
1768 */
1769 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1770 "enabling APIC mode.\n");
1771 outb(0x70, 0x22);
1772 outb(0x01, 0x23);
1773 }
1774#endif
Ingo Molnar49040332009-01-28 12:43:18 +01001775 if (apic->enable_apic_mode)
1776 apic->enable_apic_mode();
Glauber Costab5841762008-05-28 13:38:28 -03001777}
1778
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001779/**
1780 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1781 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1782 *
1783 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1784 * APIC is disabled.
1785 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001786void disconnect_bsp_APIC(int virt_wire_setup)
1787{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001788 unsigned int value;
1789
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001790#ifdef CONFIG_X86_32
1791 if (pic_mode) {
1792 /*
1793 * Put the board back into PIC mode (has an effect only on
1794 * certain older boards). Note that APIC interrupts, including
1795 * IPIs, won't work beyond this point! The only exception are
1796 * INIT IPIs.
1797 */
1798 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1799 "entering PIC mode.\n");
1800 outb(0x70, 0x22);
1801 outb(0x00, 0x23);
1802 return;
1803 }
1804#endif
1805
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001806 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001807
1808 /* For the spurious interrupt use vector F, and enable it */
1809 value = apic_read(APIC_SPIV);
1810 value &= ~APIC_VECTOR_MASK;
1811 value |= APIC_SPIV_APIC_ENABLED;
1812 value |= 0xf;
1813 apic_write(APIC_SPIV, value);
1814
1815 if (!virt_wire_setup) {
1816 /*
1817 * For LVT0 make it edge triggered, active high,
1818 * external and enabled
1819 */
1820 value = apic_read(APIC_LVT0);
1821 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1822 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1823 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1824 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1825 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1826 apic_write(APIC_LVT0, value);
1827 } else {
1828 /* Disable LVT0 */
1829 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1830 }
1831
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001832 /*
1833 * For LVT1 make it edge triggered, active high,
1834 * nmi and enabled
1835 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001836 value = apic_read(APIC_LVT1);
1837 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1838 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1839 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1840 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1841 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1842 apic_write(APIC_LVT1, value);
1843}
1844
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001845void __cpuinit generic_processor_info(int apicid, int version)
1846{
1847 int cpu;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001848
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001849 /*
1850 * Validate version
1851 */
1852 if (version == 0x0) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001853 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
Mike Travis3b11ce72008-12-17 15:21:39 -08001854 "fixing up to 0x10. (tell your hw vendor)\n",
1855 version);
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001856 version = 0x10;
1857 }
1858 apic_version[apicid] = version;
1859
Mike Travis3b11ce72008-12-17 15:21:39 -08001860 if (num_processors >= nr_cpu_ids) {
1861 int max = nr_cpu_ids;
1862 int thiscpu = max + disabled_cpus;
1863
1864 pr_warning(
1865 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1866 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1867
1868 disabled_cpus++;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001869 return;
1870 }
1871
1872 num_processors++;
Mike Travis3b11ce72008-12-17 15:21:39 -08001873 cpu = cpumask_next_zero(-1, cpu_present_mask);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001874
Mike Travisb2b815d2009-01-16 15:22:16 -08001875 if (version != apic_version[boot_cpu_physical_apicid])
1876 WARN_ONCE(1,
1877 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1878 apic_version[boot_cpu_physical_apicid], cpu, version);
1879
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001880 physid_set(apicid, phys_cpu_present_map);
1881 if (apicid == boot_cpu_physical_apicid) {
1882 /*
1883 * x86_bios_cpu_apicid is required to have processors listed
1884 * in same order as logical cpu numbers. Hence the first
1885 * entry is BSP, and so on.
1886 */
1887 cpu = 0;
1888 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001889 if (apicid > max_physical_apicid)
1890 max_physical_apicid = apicid;
1891
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001892#ifdef CONFIG_X86_32
1893 /*
1894 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1895 * but we need to work other dependencies like SMP_SUSPEND etc
1896 * before this can be done without some confusion.
1897 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1898 * - Ashok Raj <ashok.raj@intel.com>
1899 */
1900 if (max_physical_apicid >= 8) {
1901 switch (boot_cpu_data.x86_vendor) {
1902 case X86_VENDOR_INTEL:
1903 if (!APIC_XAPIC(version)) {
1904 def_to_bigsmp = 0;
1905 break;
1906 }
1907 /* If P4 and above fall through */
1908 case X86_VENDOR_AMD:
1909 def_to_bigsmp = 1;
1910 }
1911 }
1912#endif
1913
Ingo Molnar3e5095d2009-01-27 17:07:08 +01001914#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
Tejun Heof10fcd42009-01-13 20:41:34 +09001915 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1916 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001917#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001918
Mike Travis1de88cd2008-12-16 17:34:02 -08001919 set_cpu_possible(cpu, true);
1920 set_cpu_present(cpu, true);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001921}
1922
Suresh Siddha0c81c742008-07-10 11:16:48 -07001923int hard_smp_processor_id(void)
1924{
1925 return read_apic_id();
1926}
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +01001927
1928void default_init_apic_ldr(void)
1929{
1930 unsigned long val;
1931
1932 apic_write(APIC_DFR, APIC_DFR_VALUE);
1933 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
1934 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1935 apic_write(APIC_LDR, val);
1936}
1937
1938#ifdef CONFIG_X86_32
1939int default_apicid_to_node(int logical_apicid)
1940{
1941#ifdef CONFIG_SMP
1942 return apicid_2_node[hard_smp_processor_id()];
1943#else
1944 return 0;
1945#endif
1946}
Yinghai Lu34919982008-08-24 02:01:48 -07001947#endif
Suresh Siddha0c81c742008-07-10 11:16:48 -07001948
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001949/*
1950 * Power management
1951 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952#ifdef CONFIG_PM
1953
1954static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001955 /*
1956 * 'active' is true if the local APIC was enabled by us and
1957 * not the BIOS; this signifies that we are also responsible
1958 * for disabling it before entering apm/acpi suspend
1959 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int active;
1961 /* r/w apic fields */
1962 unsigned int apic_id;
1963 unsigned int apic_taskpri;
1964 unsigned int apic_ldr;
1965 unsigned int apic_dfr;
1966 unsigned int apic_spiv;
1967 unsigned int apic_lvtt;
1968 unsigned int apic_lvtpc;
1969 unsigned int apic_lvt0;
1970 unsigned int apic_lvt1;
1971 unsigned int apic_lvterr;
1972 unsigned int apic_tmict;
1973 unsigned int apic_tdcr;
1974 unsigned int apic_thmr;
1975} apic_pm_state;
1976
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001977static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978{
1979 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001980 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 if (!apic_pm_state.active)
1983 return 0;
1984
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001985 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001986
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001987 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1989 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1990 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1991 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1992 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001993 if (maxlvt >= 4)
1994 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1996 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1997 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1998 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1999 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04002000#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002001 if (maxlvt >= 5)
2002 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2003#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04002004
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02002005 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 disable_local_APIC();
2007 local_irq_restore(flags);
2008 return 0;
2009}
2010
2011static int lapic_resume(struct sys_device *dev)
2012{
2013 unsigned int l, h;
2014 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002015 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 if (!apic_pm_state.active)
2018 return 0;
2019
Thomas Gleixner37e650c2008-01-30 13:30:14 +01002020 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 local_irq_save(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002023
Yinghai Lu06cd9a72009-02-16 17:29:58 -08002024#ifdef CONFIG_X86_X2APIC
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002025 if (x2apic)
2026 enable_x2apic();
2027 else
2028#endif
Yinghai Lud5e629a2008-08-17 21:12:27 -07002029 {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002030 /*
2031 * Make sure the APICBASE points to the right address
2032 *
2033 * FIXME! This will be wrong if we ever support suspend on
2034 * SMP! We'll need to do this as part of the CPU restore!
2035 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002036 rdmsr(MSR_IA32_APICBASE, l, h);
2037 l &= ~MSR_IA32_APICBASE_BASE;
2038 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2039 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07002040 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2043 apic_write(APIC_ID, apic_pm_state.apic_id);
2044 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2045 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2046 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2047 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2048 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2049 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002050#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002051 if (maxlvt >= 5)
2052 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2053#endif
2054 if (maxlvt >= 4)
2055 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2057 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2058 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2059 apic_write(APIC_ESR, 0);
2060 apic_read(APIC_ESR);
2061 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2062 apic_write(APIC_ESR, 0);
2063 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return 0;
2068}
2069
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002070/*
2071 * This device has no shutdown method - fully functioning local APICs
2072 * are needed on every CPU up until machine_halt/restart/poweroff.
2073 */
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002076 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 .resume = lapic_resume,
2078 .suspend = lapic_suspend,
2079};
2080
2081static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002082 .id = 0,
2083 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084};
2085
Ashok Raje6982c62005-06-25 14:54:58 -07002086static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 apic_pm_state.active = 1;
2089}
2090
2091static int __init init_lapic_sysfs(void)
2092{
2093 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002094
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 if (!cpu_has_apic)
2096 return 0;
2097 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 error = sysdev_class_register(&lapic_sysclass);
2100 if (!error)
2101 error = sysdev_register(&device_lapic);
2102 return error;
2103}
2104device_initcall(init_lapic_sysfs);
2105
2106#else /* CONFIG_PM */
2107
2108static void apic_pm_activate(void) { }
2109
2110#endif /* CONFIG_PM */
2111
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002112#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002114 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 *
2116 * Thus far, the major user of this is IBM's Summit2 series:
2117 *
Linus Torvalds637029c2006-02-27 20:41:56 -08002118 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 * multi-chassis. Use available data to take a good guess.
2120 * If in doubt, go HPET.
2121 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002122__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 int i, clusters, zeros;
2125 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08002126 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2128
Yinghai Lu322850a2008-02-23 21:48:42 -08002129 /*
2130 * there is not this kind of box with AMD CPU yet.
2131 * Some AMD box with quadcore cpu and 8 sockets apicid
2132 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08002133 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08002134 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002135 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08002136 return 0;
2137
Mike Travis23ca4bb2008-05-12 21:21:12 +02002138 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07002139 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Mike Travis168ef542008-12-16 17:34:01 -08002141 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002142 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01002143 if (bios_cpu_apicid) {
2144 id = bios_cpu_apicid[i];
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302145 } else if (i < nr_cpu_ids) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002146 if (cpu_present(i))
2147 id = per_cpu(x86_bios_cpu_apicid, i);
2148 else
2149 continue;
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302150 } else
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002151 break;
2152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 if (id != BAD_APICID)
2154 __set_bit(APIC_CLUSTERID(id), clustermap);
2155 }
2156
2157 /* Problem: Partially populated chassis may not have CPUs in some of
2158 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01002159 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2160 * Since clusters are allocated sequentially, count zeros only if
2161 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 */
2163 clusters = 0;
2164 zeros = 0;
2165 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2166 if (test_bit(i, clustermap)) {
2167 clusters += 1 + zeros;
2168 zeros = 0;
2169 } else
2170 ++zeros;
2171 }
2172
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002173 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2174 * not guaranteed to be synced between boards
2175 */
2176 if (is_vsmp_box() && clusters > 1)
2177 return 1;
2178
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002180 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 * May have to revisit this when multi-core + hyperthreaded CPUs come
2182 * out, but AFAIK this will work even for them.
2183 */
2184 return (clusters > 2);
2185}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
2188/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002189 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002191static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002192{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07002194 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002195 return 0;
2196}
2197early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002199/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002200static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002201{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002202 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002203}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002204early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Linus Torvalds2e7c2832007-03-23 11:32:31 -07002206static int __init parse_lapic_timer_c2_ok(char *arg)
2207{
2208 local_apic_timer_c2_ok = 1;
2209 return 0;
2210}
2211early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2212
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002213static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002214{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002216 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002217}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002218early_param("noapictimer", parse_disable_apic_timer);
2219
2220static int __init parse_nolapic_timer(char *arg)
2221{
2222 disable_apic_timer = 1;
2223 return 0;
2224}
2225early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01002226
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002227static int __init apic_set_verbosity(char *arg)
2228{
2229 if (!arg) {
2230#ifdef CONFIG_X86_64
2231 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002232 return 0;
2233#endif
2234 return -EINVAL;
2235 }
2236
2237 if (strcmp("debug", arg) == 0)
2238 apic_verbosity = APIC_DEBUG;
2239 else if (strcmp("verbose", arg) == 0)
2240 apic_verbosity = APIC_VERBOSE;
2241 else {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01002242 pr_warning("APIC Verbosity level %s not recognised"
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002243 " use apic=verbose or apic=debug\n", arg);
2244 return -EINVAL;
2245 }
2246
2247 return 0;
2248}
2249early_param("apic", apic_set_verbosity);
2250
Yinghai Lu1e934dd2008-02-22 13:37:26 -08002251static int __init lapic_insert_resource(void)
2252{
2253 if (!apic_phys)
2254 return -1;
2255
2256 /* Put local APIC into the resource map. */
2257 lapic_resource.start = apic_phys;
2258 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2259 insert_resource(&iomem_resource, &lapic_resource);
2260
2261 return 0;
2262}
2263
2264/*
2265 * need call insert after e820_reserve_resources()
2266 * that is using request_resource
2267 */
2268late_initcall(lapic_insert_resource);