blob: 60df2efd7c80c5a899613a4e4ea928db757624b0 [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
Paul Mackerrasa63eaf32009-05-22 14:17:31 +100017#include <linux/perf_counter.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 Molnar5c167b82008-12-17 09:02:19 +010038#include <asm/perf_counter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <asm/pgalloc.h>
Ingo Molnard1de36f2009-01-31 01:59:14 +010040#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>
Andi Kleenbe71b852009-02-12 13:49:38 +010051#include <asm/mce.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Brian Gerstec70de82009-01-27 12:56:47 +090053unsigned int num_processors;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010054
Brian Gerstec70de82009-01-27 12:56:47 +090055unsigned disabled_cpus __cpuinitdata;
Ingo Molnarfdbecd92009-01-31 03:57:12 +010056
Brian Gerstec70de82009-01-27 12:56:47 +090057/* Processor that is doing the boot up */
58unsigned int boot_cpu_physical_apicid = -1U;
Glauber Costa5af55732008-03-25 13:28:56 -030059
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070060/*
Ingo Molnarfdbecd92009-01-31 03:57:12 +010061 * The highest APIC ID seen during enumeration.
62 *
63 * This determines the messaging protocol we can use: if all APIC IDs
64 * are in the 0 ... 7 range, then we can use logical addressing which
65 * has some performance advantages (better broadcasting).
66 *
67 * If there's an APIC ID above 8, we use physical addressing.
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070068 */
Brian Gerstec70de82009-01-27 12:56:47 +090069unsigned int max_physical_apicid;
70
Ingo Molnarfdbecd92009-01-31 03:57:12 +010071/*
72 * Bitmask of physically existing CPUs:
73 */
Brian Gerstec70de82009-01-27 12:56:47 +090074physid_mask_t phys_cpu_present_map;
75
76/*
77 * Map cpu index to physical APIC ID
78 */
79DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
80DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
81EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
82EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
Cyrill Gorcunov80e56092008-08-24 02:01:42 -070083
Yinghai Lub3c51172008-08-24 02:01:46 -070084#ifdef CONFIG_X86_32
85/*
86 * Knob to control our willingness to enable the local APIC.
87 *
88 * +1=force-enable
89 */
90static int force_enable_local_apic;
91/*
92 * APIC command line parameters
93 */
94static int __init parse_lapic(char *arg)
95{
96 force_enable_local_apic = 1;
97 return 0;
98}
99early_param("lapic", parse_lapic);
Yinghai Luf28c0ae2008-08-24 02:01:49 -0700100/* Local APIC was disabled by the BIOS and enabled by the kernel */
101static int enabled_via_apicbase;
102
Yinghai Lub3c51172008-08-24 02:01:46 -0700103#endif
104
105#ifdef CONFIG_X86_64
Chris Wrightbc1d99c2007-10-12 23:04:23 +0200106static int apic_calibrate_pmtmr __initdata;
Yinghai Lub3c51172008-08-24 02:01:46 -0700107static __init int setup_apicpmtimer(char *s)
108{
109 apic_calibrate_pmtmr = 1;
110 notsc_setup(NULL);
111 return 0;
112}
113__setup("apicpmtimer", setup_apicpmtimer);
114#endif
115
Yinghai Lu06cd9a72009-02-16 17:29:58 -0800116#ifdef CONFIG_X86_X2APIC
Suresh Siddha89027d32008-07-10 11:16:56 -0700117int x2apic;
Suresh Siddha6e1cb382008-07-10 11:16:58 -0700118/* x2apic enabled before OS handover */
Jaswinder Singhb6b301a2008-12-23 21:52:33 +0530119static int x2apic_preenabled;
120static int disable_x2apic;
Yinghai Lu49899ea2008-08-24 02:01:47 -0700121static __init int setup_nox2apic(char *str)
122{
123 disable_x2apic = 1;
124 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
125 return 0;
126}
127early_param("nox2apic", setup_nox2apic);
128#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Yinghai Lub3c51172008-08-24 02:01:46 -0700130unsigned long mp_lapic_addr;
131int disable_apic;
132/* Disable local APIC timer from the kernel commandline or via dmi quirk */
133static int disable_apic_timer __cpuinitdata;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100134/* Local APIC timer works in C2 */
Linus Torvalds2e7c2832007-03-23 11:32:31 -0700135int local_apic_timer_c2_ok;
136EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
137
Yinghai Luefa25592008-08-19 20:50:36 -0700138int first_system_vector = 0xfe;
139
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100140/*
141 * Debug level, exported for io_apic.c
142 */
Maciej W. Rozyckibaa13182008-07-14 18:44:51 +0100143unsigned int apic_verbosity;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100144
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -0700145int pic_mode;
146
Alexey Starikovskiybab4b272008-05-19 19:47:03 +0400147/* Have we found an MP table */
148int smp_found_config;
149
Aaron Durbin39928722006-12-07 02:14:01 +0100150static struct resource lapic_resource = {
151 .name = "Local APIC",
152 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
153};
154
Thomas Gleixnerd03030e2007-10-12 23:04:06 +0200155static unsigned int calibration_result;
156
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200157static int lapic_next_event(unsigned long delta,
158 struct clock_event_device *evt);
159static void lapic_timer_setup(enum clock_event_mode mode,
160 struct clock_event_device *evt);
Mike Travis96289372008-12-31 18:08:46 -0800161static void lapic_timer_broadcast(const struct cpumask *mask);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100162static void apic_pm_activate(void);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200163
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400164/*
165 * The local apic timer can be used for any function which is CPU local.
166 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200167static struct clock_event_device lapic_clockevent = {
168 .name = "lapic",
169 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
170 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
171 .shift = 32,
172 .set_mode = lapic_timer_setup,
173 .set_next_event = lapic_next_event,
174 .broadcast = lapic_timer_broadcast,
175 .rating = 100,
176 .irq = -1,
177};
178static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
179
Andi Kleend3432892008-01-30 13:33:17 +0100180static unsigned long apic_phys;
181
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100182/*
183 * Get the LAPIC version
184 */
185static inline int lapic_get_version(void)
186{
187 return GET_APIC_VERSION(apic_read(APIC_LVR));
188}
189
190/*
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400191 * Check, if the APIC is integrated or a separate chip
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100192 */
193static inline int lapic_is_integrated(void)
194{
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400195#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100196 return 1;
Cyrill Gorcunov9c803862008-08-16 23:21:54 +0400197#else
198 return APIC_INTEGRATED(lapic_get_version());
199#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100200}
201
202/*
203 * Check, whether this is a modern or a first generation APIC
204 */
205static int modern_apic(void)
206{
207 /* AMD systems use old APIC versions, so check the CPU */
208 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
209 boot_cpu_data.x86 >= 0xf)
210 return 1;
211 return lapic_get_version() >= 0x14;
212}
213
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800214void native_apic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100215{
216 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
217 cpu_relax();
218}
219
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800220u32 native_safe_apic_wait_icr_idle(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100221{
222 u32 send_status;
223 int timeout;
224
225 timeout = 0;
226 do {
227 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
228 if (!send_status)
229 break;
230 udelay(100);
231 } while (timeout++ < 1000);
232
233 return send_status;
234}
235
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800236void native_apic_icr_write(u32 low, u32 id)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700237{
Cyrill Gorcunoved4e5ec2008-08-15 13:51:20 +0200238 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
Suresh Siddha1b374e42008-07-10 11:16:49 -0700239 apic_write(APIC_ICR, low);
240}
241
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800242u64 native_apic_icr_read(void)
Suresh Siddha1b374e42008-07-10 11:16:49 -0700243{
244 u32 icr1, icr2;
245
246 icr2 = apic_read(APIC_ICR2);
247 icr1 = apic_read(APIC_ICR);
248
Cyrill Gorcunovcf9768d72008-08-16 23:21:55 +0400249 return icr1 | ((u64)icr2 << 32);
Suresh Siddha1b374e42008-07-10 11:16:49 -0700250}
251
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100252/**
253 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
254 */
Jan Beuliche9427102008-01-30 13:31:24 +0100255void __cpuinit enable_NMI_through_LVT0(void)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100256{
257 unsigned int v;
258
259 /* unmask and set to NMI */
260 v = APIC_DM_NMI;
Cyrill Gorcunovd4c63ec2008-07-24 13:52:29 +0200261
262 /* Level triggered for 82489DX (32bit mode) */
263 if (!lapic_is_integrated())
264 v |= APIC_LVT_LEVEL_TRIGGER;
265
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100266 apic_write(APIC_LVT0, v);
267}
268
Cyrill Gorcunov7c37e482008-08-24 02:01:40 -0700269#ifdef CONFIG_X86_32
270/**
271 * get_physical_broadcast - Get number of physical broadcast IDs
272 */
273int get_physical_broadcast(void)
274{
275 return modern_apic() ? 0xff : 0xf;
276}
277#endif
278
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100279/**
280 * lapic_get_maxlvt - get the maximum number of local vector table entries
281 */
282int lapic_get_maxlvt(void)
283{
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200284 unsigned int v;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100285
286 v = apic_read(APIC_LVR);
Cyrill Gorcunov36a028d2008-07-24 13:52:28 +0200287 /*
288 * - we always have APIC integrated on 64bit mode
289 * - 82489DXs do not report # of LVT entries
290 */
291 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100292}
293
294/*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400295 * Local APIC timer
296 */
297
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400298/* Clock divisor */
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400299#define APIC_DIVISOR 16
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200300
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100301/*
302 * This function sets up the local APIC timer, with a timeout of
303 * 'clocks' APIC bus clock. During calibration we actually call
304 * this function twice on the boot CPU, once with a bogus timeout
305 * value, second time for real. The other (noncalibrating) CPUs
306 * call this function only once, with the real, calibrated value.
307 *
308 * We do reads before writes even if unnecessary, to get around the
309 * P5 APIC double write bug.
310 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100311static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
312{
313 unsigned int lvtt_value, tmp_value;
314
315 lvtt_value = LOCAL_TIMER_VECTOR;
316 if (!oneshot)
317 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200318 if (!lapic_is_integrated())
319 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
320
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100321 if (!irqen)
322 lvtt_value |= APIC_LVT_MASKED;
323
324 apic_write(APIC_LVTT, lvtt_value);
325
326 /*
327 * Divide PICLK by 16
328 */
329 tmp_value = apic_read(APIC_TDCR);
Cyrill Gorcunovc40aaec2008-08-18 20:45:55 +0400330 apic_write(APIC_TDCR,
331 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
332 APIC_TDR_DIV_16);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100333
334 if (!oneshot)
Cyrill Gorcunovf07f4f92008-08-15 13:51:21 +0200335 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100336}
337
338/*
Robert Richter7b83dae2008-01-30 13:30:40 +0100339 * Setup extended LVT, AMD specific (K8, family 10h)
340 *
341 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
342 * MCE interrupts are supported. Thus MCE offset must be set to 0.
Robert Richter286f5712008-07-22 21:08:46 +0200343 *
344 * If mask=1, the LVT entry does not generate interrupts while mask=0
345 * enables the vector. See also the BKDGs.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100346 */
Robert Richter7b83dae2008-01-30 13:30:40 +0100347
348#define APIC_EILVT_LVTOFF_MCE 0
349#define APIC_EILVT_LVTOFF_IBS 1
350
351static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100352{
Robert Richter7b83dae2008-01-30 13:30:40 +0100353 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100354 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
355
356 apic_write(reg, v);
357}
358
Robert Richter7b83dae2008-01-30 13:30:40 +0100359u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
360{
361 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
362 return APIC_EILVT_LVTOFF_MCE;
363}
364
365u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
366{
367 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
368 return APIC_EILVT_LVTOFF_IBS;
369}
Robert Richter6aa360e2008-07-23 15:28:14 +0200370EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
Robert Richter7b83dae2008-01-30 13:30:40 +0100371
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100372/*
373 * Program the next event, relative to now
374 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200375static int lapic_next_event(unsigned long delta,
376 struct clock_event_device *evt)
377{
378 apic_write(APIC_TMICT, delta);
379 return 0;
380}
381
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100382/*
383 * Setup the lapic timer in periodic or oneshot mode
384 */
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200385static void lapic_timer_setup(enum clock_event_mode mode,
386 struct clock_event_device *evt)
387{
388 unsigned long flags;
389 unsigned int v;
390
391 /* Lapic used as dummy for broadcast ? */
392 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
393 return;
394
395 local_irq_save(flags);
396
397 switch (mode) {
398 case CLOCK_EVT_MODE_PERIODIC:
399 case CLOCK_EVT_MODE_ONESHOT:
400 __setup_APIC_LVTT(calibration_result,
401 mode != CLOCK_EVT_MODE_PERIODIC, 1);
402 break;
403 case CLOCK_EVT_MODE_UNUSED:
404 case CLOCK_EVT_MODE_SHUTDOWN:
405 v = apic_read(APIC_LVTT);
406 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
407 apic_write(APIC_LVTT, v);
Thomas Gleixnera98f8fd2008-11-06 01:13:39 +0100408 apic_write(APIC_TMICT, 0xffffffff);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200409 break;
410 case CLOCK_EVT_MODE_RESUME:
411 /* Nothing to do here */
412 break;
413 }
414
415 local_irq_restore(flags);
416}
417
418/*
419 * Local APIC timer broadcast function
420 */
Mike Travis96289372008-12-31 18:08:46 -0800421static void lapic_timer_broadcast(const struct cpumask *mask)
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200422{
423#ifdef CONFIG_SMP
Ingo Molnardac5f412009-01-28 15:42:24 +0100424 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
Thomas Gleixnerba7eda42007-10-12 23:04:07 +0200425#endif
426}
427
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100428/*
429 * Setup the local APIC timer for this CPU. Copy the initilized values
430 * of the boot CPU and register the clock event in the framework.
431 */
Cyrill Gorcunovdb4b5522008-08-24 02:01:39 -0700432static void __cpuinit setup_APIC_timer(void)
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200433{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100434 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
435
Venkatesh Pallipadidb954b52009-04-06 18:51:29 -0700436 if (cpu_has(&current_cpu_data, X86_FEATURE_ARAT)) {
437 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
438 /* Make LAPIC timer preferrable over percpu HPET */
439 lapic_clockevent.rating = 150;
440 }
441
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100442 memcpy(levt, &lapic_clockevent, sizeof(*levt));
Rusty Russell320ab2b2008-12-13 21:20:26 +1030443 levt->cpumask = cpumask_of(smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100444
445 clockevents_register_device(levt);
Fernando Luis VazquezCao8339e9f2007-05-02 19:27:17 +0200446}
447
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700448/*
449 * In this functions we calibrate APIC bus clocks to the external timer.
450 *
451 * We want to do the calibration only once since we want to have local timer
452 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
453 * frequency.
454 *
455 * This was previously done by reading the PIT/HPET and waiting for a wrap
456 * around to find out, that a tick has elapsed. I have a box, where the PIT
457 * readout is broken, so it never gets out of the wait loop again. This was
458 * also reported by others.
459 *
460 * Monitoring the jiffies value is inaccurate and the clockevents
461 * infrastructure allows us to do a simple substitution of the interrupt
462 * handler.
463 *
464 * The calibration routine also uses the pm_timer when possible, as the PIT
465 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
466 * back to normal later in the boot process).
467 */
468
469#define LAPIC_CAL_LOOPS (HZ/10)
470
471static __initdata int lapic_cal_loops = -1;
472static __initdata long lapic_cal_t1, lapic_cal_t2;
473static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
474static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
475static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
476
477/*
478 * Temporary interrupt handler.
479 */
480static void __init lapic_cal_handler(struct clock_event_device *dev)
481{
482 unsigned long long tsc = 0;
483 long tapic = apic_read(APIC_TMCCT);
484 unsigned long pm = acpi_pm_read_early();
485
486 if (cpu_has_tsc)
487 rdtscll(tsc);
488
489 switch (lapic_cal_loops++) {
490 case 0:
491 lapic_cal_t1 = tapic;
492 lapic_cal_tsc1 = tsc;
493 lapic_cal_pm1 = pm;
494 lapic_cal_j1 = jiffies;
495 break;
496
497 case LAPIC_CAL_LOOPS:
498 lapic_cal_t2 = tapic;
499 lapic_cal_tsc2 = tsc;
500 if (pm < lapic_cal_pm1)
501 pm += ACPI_PM_OVRRUN;
502 lapic_cal_pm2 = pm;
503 lapic_cal_j2 = jiffies;
504 break;
505 }
506}
507
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900508static int __init
509calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400510{
511 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
512 const long pm_thresh = pm_100ms / 100;
513 unsigned long mult;
514 u64 res;
515
516#ifndef CONFIG_X86_PM_TIMER
517 return -1;
518#endif
519
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900520 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400521
522 /* Check, if the PM timer is available */
523 if (!deltapm)
524 return -1;
525
526 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
527
528 if (deltapm > (pm_100ms - pm_thresh) &&
529 deltapm < (pm_100ms + pm_thresh)) {
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900530 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900531 return 0;
532 }
533
534 res = (((u64)deltapm) * mult) >> 22;
535 do_div(res, 1000000);
536 pr_warning("APIC calibration not consistent "
Yasuaki Ishimatsu39ba5d42009-01-28 12:52:24 +0900537 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900538
539 /* Correct the lapic counter value */
540 res = (((u64)(*delta)) * pm_100ms);
541 do_div(res, deltapm);
542 pr_info("APIC delta adjusted to PM-Timer: "
543 "%lu (%ld)\n", (unsigned long)res, *delta);
544 *delta = (long)res;
545
546 /* Correct the tsc counter value */
547 if (cpu_has_tsc) {
548 res = (((u64)(*deltatsc)) * pm_100ms);
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400549 do_div(res, deltapm);
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900550 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
551 "PM-Timer: %lu (%ld) \n",
552 (unsigned long)res, *deltatsc);
553 *deltatsc = (long)res;
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400554 }
555
556 return 0;
557}
558
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700559static int __init calibrate_APIC_clock(void)
560{
561 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700562 void (*real_handler)(struct clock_event_device *dev);
563 unsigned long deltaj;
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900564 long delta, deltatsc;
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700565 int pm_referenced = 0;
566
567 local_irq_disable();
568
569 /* Replace the global interrupt handler */
570 real_handler = global_clock_event->event_handler;
571 global_clock_event->event_handler = lapic_cal_handler;
572
573 /*
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400574 * Setup the APIC counter to maximum. There is no way the lapic
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700575 * can underflow in the 100ms detection time frame
576 */
Cyrill Gorcunov81608f32008-10-10 19:00:17 +0400577 __setup_APIC_LVTT(0xffffffff, 0, 0);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700578
579 /* Let the interrupts run */
580 local_irq_enable();
581
582 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
583 cpu_relax();
584
585 local_irq_disable();
586
587 /* Restore the real event handler */
588 global_clock_event->event_handler = real_handler;
589
590 /* Build delta t1-t2 as apic timer counts down */
591 delta = lapic_cal_t1 - lapic_cal_t2;
592 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
593
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900594 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
595
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400596 /* we trust the PM based calibration if possible */
597 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900598 &delta, &deltatsc);
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700599
600 /* Calculate the scaled math multiplication factor */
601 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
602 lapic_clockevent.shift);
603 lapic_clockevent.max_delta_ns =
604 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
605 lapic_clockevent.min_delta_ns =
606 clockevent_delta2ns(0xF, &lapic_clockevent);
607
608 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
609
610 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
611 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
612 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
613 calibration_result);
614
615 if (cpu_has_tsc) {
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700616 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
617 "%ld.%04ld MHz.\n",
Yasuaki Ishimatsu754ef0c2009-01-28 12:51:09 +0900618 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
619 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700620 }
621
622 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
623 "%u.%04u MHz.\n",
624 calibration_result / (1000000 / HZ),
625 calibration_result % (1000000 / HZ));
626
627 /*
628 * Do a sanity check on the APIC calibration result
629 */
630 if (calibration_result < (1000000 / HZ)) {
631 local_irq_enable();
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100632 pr_warning("APIC frequency too slow, disabling apic timer\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700633 return -1;
634 }
635
636 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
637
Cyrill Gorcunovb1898922008-09-12 23:58:24 +0400638 /*
639 * PM timer calibration failed or not turned on
640 * so lets try APIC timer based calibration
641 */
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700642 if (!pm_referenced) {
643 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
644
645 /*
646 * Setup the apic timer manually
647 */
648 levt->event_handler = lapic_cal_handler;
649 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
650 lapic_cal_loops = -1;
651
652 /* Let the interrupts run */
653 local_irq_enable();
654
655 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
656 cpu_relax();
657
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700658 /* Stop the lapic timer */
659 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
660
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700661 /* Jiffies delta */
662 deltaj = lapic_cal_j2 - lapic_cal_j1;
663 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
664
665 /* Check, if the jiffies result is consistent */
666 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
667 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
668 else
669 levt->features |= CLOCK_EVT_FEAT_DUMMY;
670 } else
671 local_irq_enable();
672
673 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +0530674 pr_warning("APIC timer disabled due to verification failure\n");
Yinghai Lu2f04fa82008-08-24 02:01:54 -0700675 return -1;
676 }
677
678 return 0;
679}
680
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +0100681/*
682 * Setup the boot APIC
683 *
684 * Calibrate and verify the result.
685 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100686void __init setup_boot_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100688 /*
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400689 * The local apic timer can be disabled via the kernel
690 * commandline or from the CPU detection code. Register the lapic
691 * timer as a dummy clock event source on SMP systems, so the
692 * broadcast mechanism is used. On UP systems simply ignore it.
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100693 */
694 if (disable_apic_timer) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100695 pr_info("Disabling APIC timer\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100696 /* No broadcast on UP ! */
Thomas Gleixner9d099512008-01-30 13:33:04 +0100697 if (num_possible_cpus() > 1) {
698 lapic_clockevent.mult = 1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100699 setup_APIC_timer();
Thomas Gleixner9d099512008-01-30 13:33:04 +0100700 }
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100701 return;
702 }
Thomas Gleixner6935d1f2007-07-21 17:10:17 +0200703
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400704 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
705 "calibrating APIC timer ...\n");
706
Cyrill Gorcunov89b3b1f2008-07-15 21:02:54 +0400707 if (calibrate_APIC_clock()) {
Thomas Gleixnerc2b84b32008-01-30 13:33:04 +0100708 /* No broadcast on UP ! */
709 if (num_possible_cpus() > 1)
710 setup_APIC_timer();
711 return;
712 }
713
714 /*
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100715 * If nmi_watchdog is set to IO_APIC, we need the
716 * PIT/HPET going. Otherwise register lapic as a dummy
717 * device.
718 */
719 if (nmi_watchdog != NMI_IO_APIC)
720 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
721 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100722 pr_warning("APIC timer registered as dummy,"
Cyrill Gorcunov116f5702008-06-24 22:52:04 +0200723 " due to nmi_watchdog=%d!\n", nmi_watchdog);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100724
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400725 /* Setup the lapic or request the broadcast */
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100726 setup_APIC_timer();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100729void __cpuinit setup_secondary_APIC_clock(void)
730{
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100731 setup_APIC_timer();
732}
733
734/*
735 * The guts of the apic timer interrupt
736 */
737static void local_apic_timer_interrupt(void)
738{
739 int cpu = smp_processor_id();
740 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
741
742 /*
743 * Normally we should not be here till LAPIC has been initialized but
744 * in some cases like kdump, its possible that there is a pending LAPIC
745 * timer interrupt from previous kernel's context and is delivered in
746 * new kernel the moment interrupts are enabled.
747 *
748 * Interrupts are enabled early and LAPIC is setup much later, hence
749 * its possible that when we get here evt->event_handler is NULL.
750 * Check for event_handler being NULL and discard the interrupt as
751 * spurious.
752 */
753 if (!evt->event_handler) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +0100754 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100755 /* Switch it off */
756 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
757 return;
758 }
759
760 /*
761 * the NMI deadlock-detector uses this.
762 */
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -0800763 inc_irq_stat(apic_timer_irqs);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100764
765 evt->event_handler(evt);
766}
767
768/*
769 * Local APIC timer interrupt. This is the most natural way for doing
770 * local interrupts, but local timer interrupts can be emulated by
771 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
772 *
773 * [ if a single-CPU system runs an SMP kernel then we call the local
774 * interrupt as well. Thus we cannot inline the local irq ... ]
775 */
Frederic Weisbeckerbcbc4f22008-12-09 23:54:20 +0100776void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100777{
778 struct pt_regs *old_regs = set_irq_regs(regs);
779
780 /*
781 * NOTE! We'd better ACK the irq immediately,
782 * because timer handling can be slow.
783 */
784 ack_APIC_irq();
785 /*
786 * update_process_times() expects us to have done irq_enter().
787 * Besides, if we don't timer interrupts ignore the global
788 * interrupt lock, which is the WrongThing (tm) to do.
789 */
790 exit_idle();
791 irq_enter();
792 local_apic_timer_interrupt();
793 irq_exit();
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +0400794
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100795 set_irq_regs(old_regs);
796}
797
798int setup_profiling_timer(unsigned int multiplier)
799{
800 return -EINVAL;
801}
802
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100803/*
804 * Local APIC start and shutdown
805 */
806
807/**
808 * clear_local_APIC - shutdown the local APIC
809 *
810 * This is called, when a CPU is disabled and before rebooting, so the state of
811 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
812 * leftovers during boot.
813 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814void clear_local_APIC(void)
815{
Chuck Ebbert2584a822008-05-20 18:18:12 -0400816 int maxlvt;
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100817 u32 v;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
Andi Kleend3432892008-01-30 13:33:17 +0100819 /* APIC hasn't been mapped yet */
Suresh Siddhacf6567f2009-03-16 17:05:00 -0700820 if (!x2apic && !apic_phys)
Andi Kleend3432892008-01-30 13:33:17 +0100821 return;
822
823 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 /*
Siddha, Suresh B704fc592006-06-26 13:59:53 +0200825 * Masking an LVT entry can trigger a local APIC error
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 * if the vector is zero. Mask LVTERR first to prevent this.
827 */
828 if (maxlvt >= 3) {
829 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
Andi Kleen11a8e772006-01-11 22:46:51 +0100830 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832 /*
833 * Careful: we have to set masks only first to deassert
834 * any level-triggered sources.
835 */
836 v = apic_read(APIC_LVTT);
Andi Kleen11a8e772006-01-11 22:46:51 +0100837 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 v = apic_read(APIC_LVT0);
Andi Kleen11a8e772006-01-11 22:46:51 +0100839 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 v = apic_read(APIC_LVT1);
Andi Kleen11a8e772006-01-11 22:46:51 +0100841 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 if (maxlvt >= 4) {
843 v = apic_read(APIC_LVTPC);
Andi Kleen11a8e772006-01-11 22:46:51 +0100844 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400847 /* lets not touch this if we didn't frob it */
Andi Kleen07db1c12009-02-12 13:39:35 +0100848#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400849 if (maxlvt >= 5) {
850 v = apic_read(APIC_LVTTHMR);
851 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
852 }
853#endif
Andi Kleen5ca86812009-02-12 13:49:37 +0100854#ifdef CONFIG_X86_MCE_INTEL
855 if (maxlvt >= 6) {
856 v = apic_read(APIC_LVTCMCI);
857 if (!(v & APIC_LVT_MASKED))
858 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
859 }
860#endif
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
863 * Clean APIC state for other OSs:
864 */
Andi Kleen11a8e772006-01-11 22:46:51 +0100865 apic_write(APIC_LVTT, APIC_LVT_MASKED);
866 apic_write(APIC_LVT0, APIC_LVT_MASKED);
867 apic_write(APIC_LVT1, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 if (maxlvt >= 3)
Andi Kleen11a8e772006-01-11 22:46:51 +0100869 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (maxlvt >= 4)
Andi Kleen11a8e772006-01-11 22:46:51 +0100871 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
Cyrill Gorcunov67640142008-08-16 23:21:50 +0400872
873 /* Integrated APIC (!82489DX) ? */
874 if (lapic_is_integrated()) {
875 if (maxlvt > 3)
876 /* Clear ESR due to Pentium errata 3AP and 11AP */
877 apic_write(APIC_ESR, 0);
878 apic_read(APIC_ESR);
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100882/**
883 * disable_local_APIC - clear and disable the local APIC
884 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885void disable_local_APIC(void)
886{
887 unsigned int value;
888
Jan Beulicha08c4742009-01-14 12:28:51 +0000889 /* APIC hasn't been mapped yet */
890 if (!apic_phys)
891 return;
892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 clear_local_APIC();
894
895 /*
896 * Disable APIC (implies clearing of registers
897 * for 82489DX!).
898 */
899 value = apic_read(APIC_SPIV);
900 value &= ~APIC_SPIV_APIC_ENABLED;
Andi Kleen11a8e772006-01-11 22:46:51 +0100901 apic_write(APIC_SPIV, value);
Cyrill Gorcunov990b1832008-08-18 20:45:51 +0400902
903#ifdef CONFIG_X86_32
904 /*
905 * When LAPIC was disabled by the BIOS and enabled by the kernel,
906 * restore the disabled state.
907 */
908 if (enabled_via_apicbase) {
909 unsigned int l, h;
910
911 rdmsr(MSR_IA32_APICBASE, l, h);
912 l &= ~MSR_IA32_APICBASE_ENABLE;
913 wrmsr(MSR_IA32_APICBASE, l, h);
914 }
915#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916}
917
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400918/*
919 * If Linux enabled the LAPIC against the BIOS default disable it down before
920 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
921 * not power-off. Additionally clear all LVT entries before disable_local_APIC
922 * for the case where Linux didn't enable the LAPIC.
923 */
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700924void lapic_shutdown(void)
925{
926 unsigned long flags;
927
928 if (!cpu_has_apic)
929 return;
930
931 local_irq_save(flags);
932
Cyrill Gorcunovfe4024d2008-08-18 20:45:52 +0400933#ifdef CONFIG_X86_32
934 if (!enabled_via_apicbase)
935 clear_local_APIC();
936 else
937#endif
938 disable_local_APIC();
939
Hiroshi Shimamoto9b7711f2007-10-19 18:21:11 -0700940
941 local_irq_restore(flags);
942}
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
945 * This is to verify that we're looking at a real local APIC.
946 * Check these against your board if the CPUs aren't getting
947 * started for no apparent reason.
948 */
949int __init verify_local_APIC(void)
950{
951 unsigned int reg0, reg1;
952
953 /*
954 * The version register is read-only in a real APIC.
955 */
956 reg0 = apic_read(APIC_LVR);
957 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
958 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
959 reg1 = apic_read(APIC_LVR);
960 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
961
962 /*
963 * The two version reads above should print the same
964 * numbers. If the second one is different, then we
965 * poke at a non-APIC.
966 */
967 if (reg1 != reg0)
968 return 0;
969
970 /*
971 * Check if the version looks reasonably.
972 */
973 reg1 = GET_APIC_VERSION(reg0);
974 if (reg1 == 0x00 || reg1 == 0xff)
975 return 0;
Thomas Gleixner37e650c2008-01-30 13:30:14 +0100976 reg1 = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 if (reg1 < 0x02 || reg1 == 0xff)
978 return 0;
979
980 /*
981 * The ID register is read/write in a real APIC.
982 */
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700983 reg0 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +0100985 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
Suresh Siddha2d7a66d2008-07-11 14:24:19 -0700986 reg1 = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
988 apic_write(APIC_ID, reg0);
Ingo Molnar5b812722009-01-28 14:59:17 +0100989 if (reg1 != (reg0 ^ apic->apic_id_mask))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 return 0;
991
992 /*
993 * The next two are just to see if we have sane values.
994 * They're only really relevant if we're in Virtual Wire
995 * compatibility mode, but most boxes are anymore.
996 */
997 reg0 = apic_read(APIC_LVT0);
Thomas Gleixner0e078e22008-01-30 13:30:20 +0100998 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 reg1 = apic_read(APIC_LVT1);
1000 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1001
1002 return 1;
1003}
1004
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001005/**
1006 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1007 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008void __init sync_Arb_IDs(void)
1009{
Cyrill Gorcunov296cb952008-08-15 13:51:23 +02001010 /*
1011 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1012 * needed on AMD.
1013 */
1014 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return;
1016
1017 /*
1018 * Wait for idle.
1019 */
1020 apic_wait_icr_idle();
1021
1022 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
Cyrill Gorcunov6f6da972008-08-15 23:05:19 +04001023 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1024 APIC_INT_LEVELTRIG | APIC_DM_INIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025}
1026
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027/*
1028 * An initial setup of the virtual wire mode.
1029 */
1030void __init init_bsp_APIC(void)
1031{
Andi Kleen11a8e772006-01-11 22:46:51 +01001032 unsigned int value;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 /*
1035 * Don't do the setup now if we have a SMP BIOS as the
1036 * through-I/O-APIC virtual wire mode might be active.
1037 */
1038 if (smp_found_config || !cpu_has_apic)
1039 return;
1040
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 /*
1042 * Do not trust the local APIC being empty at bootup.
1043 */
1044 clear_local_APIC();
1045
1046 /*
1047 * Enable APIC.
1048 */
1049 value = apic_read(APIC_SPIV);
1050 value &= ~APIC_VECTOR_MASK;
1051 value |= APIC_SPIV_APIC_ENABLED;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001052
1053#ifdef CONFIG_X86_32
1054 /* This bit is reserved on P4/Xeon and should be cleared */
1055 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1056 (boot_cpu_data.x86 == 15))
1057 value &= ~APIC_SPIV_FOCUS_DISABLED;
1058 else
1059#endif
1060 value |= APIC_SPIV_FOCUS_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001062 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /*
1065 * Set up the virtual wire mode.
1066 */
Andi Kleen11a8e772006-01-11 22:46:51 +01001067 apic_write(APIC_LVT0, APIC_DM_EXTINT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 value = APIC_DM_NMI;
Cyrill Gorcunov638c0412008-08-15 23:05:18 +04001069 if (!lapic_is_integrated()) /* 82489DX */
1070 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001071 apic_write(APIC_LVT1, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001074static void __cpuinit lapic_setup_esr(void)
1075{
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001076 unsigned int oldvalue, value, maxlvt;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001077
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001078 if (!lapic_is_integrated()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001079 pr_info("No ESR for 82489DX.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001080 return;
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001081 }
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001082
Ingo Molnar08125d32009-01-28 05:08:44 +01001083 if (apic->disable_esr) {
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001084 /*
1085 * Something untraceable is creating bad interrupts on
1086 * secondary quads ... for the moment, just leave the
1087 * ESR disabled - we can't do anything useful with the
1088 * errors anyway - mbligh
1089 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001090 pr_info("Leaving ESR disabled.\n");
Cyrill Gorcunov9df08f12008-09-14 11:55:37 +04001091 return;
1092 }
1093
1094 maxlvt = lapic_get_maxlvt();
1095 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1096 apic_write(APIC_ESR, 0);
1097 oldvalue = apic_read(APIC_ESR);
1098
1099 /* enables sending errors */
1100 value = ERROR_APIC_VECTOR;
1101 apic_write(APIC_LVTERR, value);
1102
1103 /*
1104 * spec says clear errors after enabling vector.
1105 */
1106 if (maxlvt > 3)
1107 apic_write(APIC_ESR, 0);
1108 value = apic_read(APIC_ESR);
1109 if (value != oldvalue)
1110 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1111 "vector: 0x%08x after: 0x%08x\n",
1112 oldvalue, value);
Cyrill Gorcunovc43da2f2008-08-18 20:45:54 +04001113}
1114
1115
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001116/**
1117 * setup_local_APIC - setup the local APIC
1118 */
1119void __cpuinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Andi Kleen739f33b2008-01-30 13:30:40 +01001121 unsigned int value;
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001122 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Jan Beulichf1182632009-01-14 12:27:35 +00001124 if (disable_apic) {
Ingo Molnar65a4e572009-01-31 03:36:17 +01001125 arch_disable_smp_support();
Jan Beulichf1182632009-01-14 12:27:35 +00001126 return;
1127 }
1128
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001129#ifdef CONFIG_X86_32
1130 /* Pound the ESR really hard over the head with a big hammer - mbligh */
Ingo Molnar08125d32009-01-28 05:08:44 +01001131 if (lapic_is_integrated() && apic->disable_esr) {
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001132 apic_write(APIC_ESR, 0);
1133 apic_write(APIC_ESR, 0);
1134 apic_write(APIC_ESR, 0);
1135 apic_write(APIC_ESR, 0);
1136 }
1137#endif
Yong Wangc323d952009-05-29 13:28:35 +08001138 perf_counters_lapic_init();
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001139
Jack Steinerac23d4e2008-03-28 14:12:16 -05001140 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 /*
1143 * Double-check whether this APIC is really registered.
1144 * This is meaningless in clustered apic mode, so we skip it.
1145 */
Ingo Molnar7ed248d2009-01-28 03:43:47 +01001146 if (!apic->apic_id_registered())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 BUG();
1148
1149 /*
1150 * Intel recommends to set DFR, LDR and TPR before enabling
1151 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1152 * document number 292116). So here it goes...
1153 */
Ingo Molnara5c43292009-01-28 06:50:47 +01001154 apic->init_apic_ldr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 /*
1157 * Set Task Priority to 'accept all'. We never change this
1158 * later on.
1159 */
1160 value = apic_read(APIC_TASKPRI);
1161 value &= ~APIC_TPRI_MASK;
Andi Kleen11a8e772006-01-11 22:46:51 +01001162 apic_write(APIC_TASKPRI, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 /*
Vivek Goyalda7ed9f2006-03-25 16:31:16 +01001165 * After a crash, we no longer service the interrupts and a pending
1166 * interrupt from previous kernel might still have ISR bit set.
1167 *
1168 * Most probably by now CPU has serviced that pending interrupt and
1169 * it might not have done the ack_APIC_irq() because it thought,
1170 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1171 * does not clear the ISR bit and cpu thinks it has already serivced
1172 * the interrupt. Hence a vector might get locked. It was noticed
1173 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1174 */
1175 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1176 value = apic_read(APIC_ISR + i*0x10);
1177 for (j = 31; j >= 0; j--) {
1178 if (value & (1<<j))
1179 ack_APIC_irq();
1180 }
1181 }
1182
1183 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 * Now that we are all set up, enable the APIC
1185 */
1186 value = apic_read(APIC_SPIV);
1187 value &= ~APIC_VECTOR_MASK;
1188 /*
1189 * Enable APIC
1190 */
1191 value |= APIC_SPIV_APIC_ENABLED;
1192
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001193#ifdef CONFIG_X86_32
1194 /*
1195 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1196 * certain networking cards. If high frequency interrupts are
1197 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1198 * entry is masked/unmasked at a high rate as well then sooner or
1199 * later IOAPIC line gets 'stuck', no more interrupts are received
1200 * from the device. If focus CPU is disabled then the hang goes
1201 * away, oh well :-(
1202 *
1203 * [ This bug can be reproduced easily with a level-triggered
1204 * PCI Ne2000 networking cards and PII/PIII processors, dual
1205 * BX chipset. ]
1206 */
1207 /*
1208 * Actually disabling the focus CPU check just makes the hang less
1209 * frequent as it makes the interrupt distributon model be more
1210 * like LRU than MRU (the short-term load is more even across CPUs).
1211 * See also the comment in end_level_ioapic_irq(). --macro
1212 */
1213
1214 /*
1215 * - enable focus processor (bit==0)
1216 * - 64bit mode always use processor focus
1217 * so no need to set it
1218 */
1219 value &= ~APIC_SPIV_FOCUS_DISABLED;
1220#endif
Andi Kleen3f14c742006-09-26 10:52:29 +02001221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 /*
1223 * Set spurious IRQ vector
1224 */
1225 value |= SPURIOUS_APIC_VECTOR;
Andi Kleen11a8e772006-01-11 22:46:51 +01001226 apic_write(APIC_SPIV, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
1228 /*
1229 * Set up LVT0, LVT1:
1230 *
1231 * set up through-local-APIC on the BP's LINT0. This is not
1232 * strictly necessary in pure symmetric-IO mode, but sometimes
1233 * we delegate interrupts to the 8259A.
1234 */
1235 /*
1236 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1237 */
1238 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001239 if (!smp_processor_id() && (pic_mode || !value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 value = APIC_DM_EXTINT;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001241 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001242 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 } else {
1244 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
Chris Wrightbc1d99c2007-10-12 23:04:23 +02001245 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001246 smp_processor_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
Andi Kleen11a8e772006-01-11 22:46:51 +01001248 apic_write(APIC_LVT0, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 /*
1251 * only the BP should see the LINT1 NMI signal, obviously.
1252 */
1253 if (!smp_processor_id())
1254 value = APIC_DM_NMI;
1255 else
1256 value = APIC_DM_NMI | APIC_LVT_MASKED;
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001257 if (!lapic_is_integrated()) /* 82489DX */
1258 value |= APIC_LVT_LEVEL_TRIGGER;
Andi Kleen11a8e772006-01-11 22:46:51 +01001259 apic_write(APIC_LVT1, value);
Cyrill Gorcunov89c38c22008-08-24 02:01:43 -07001260
Jack Steinerac23d4e2008-03-28 14:12:16 -05001261 preempt_enable();
Andi Kleenbe71b852009-02-12 13:49:38 +01001262
1263#ifdef CONFIG_X86_MCE_INTEL
1264 /* Recheck CMCI information after local APIC is up on CPU #0 */
1265 if (smp_processor_id() == 0)
1266 cmci_recheck();
1267#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001268}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Andi Kleen739f33b2008-01-30 13:30:40 +01001270void __cpuinit end_local_APIC_setup(void)
1271{
1272 lapic_setup_esr();
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001273
1274#ifdef CONFIG_X86_32
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001275 {
1276 unsigned int value;
1277 /* Disable the local apic timer */
1278 value = apic_read(APIC_LVTT);
1279 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1280 apic_write(APIC_LVTT, value);
1281 }
Cyrill Gorcunovfa6b95f2008-08-18 20:45:58 +04001282#endif
1283
Don Zickusf2802e72006-09-26 10:52:26 +02001284 setup_apic_nmi_watchdog(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 apic_pm_activate();
1286}
1287
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001288#ifdef CONFIG_X86_X2APIC
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001289void check_x2apic(void)
1290{
Suresh Siddhaef1f87a2009-02-21 14:23:21 -08001291 if (x2apic_enabled()) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001292 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001293 x2apic_preenabled = x2apic = 1;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001294 }
1295}
1296
1297void enable_x2apic(void)
1298{
1299 int msr, msr2;
1300
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001301 if (!x2apic)
1302 return;
1303
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001304 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1305 if (!(msr & X2APIC_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001306 pr_info("Enabling x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001307 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1308 }
1309}
1310
Al Viro2236d252008-11-22 17:37:34 +00001311void __init enable_IR_x2apic(void)
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001312{
1313#ifdef CONFIG_INTR_REMAP
1314 int ret;
1315 unsigned long flags;
Fenghua Yub24696b2009-03-27 14:22:44 -07001316 struct IO_APIC_route_entry **ioapic_entries = NULL;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001317
1318 if (!cpu_has_x2apic)
1319 return;
1320
1321 if (!x2apic_preenabled && disable_x2apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001322 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1323 "because of nox2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001324 return;
1325 }
1326
1327 if (x2apic_preenabled && disable_x2apic)
1328 panic("Bios already enabled x2apic, can't enforce nox2apic");
1329
1330 if (!x2apic_preenabled && skip_ioapic_setup) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001331 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1332 "because of skipping io-apic setup\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001333 return;
1334 }
1335
1336 ret = dmar_table_init();
1337 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001338 pr_info("dmar_table_init() failed with %d:\n", ret);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001339
1340 if (x2apic_preenabled)
1341 panic("x2apic enabled by bios. But IR enabling failed");
1342 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001343 pr_info("Not enabling x2apic,Intr-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001344 return;
1345 }
1346
Fenghua Yub24696b2009-03-27 14:22:44 -07001347 ioapic_entries = alloc_ioapic_entries();
1348 if (!ioapic_entries) {
1349 pr_info("Allocate ioapic_entries failed: %d\n", ret);
1350 goto end;
1351 }
1352
1353 ret = save_IO_APIC_setup(ioapic_entries);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001354 if (ret) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001355 pr_info("Saving IO-APIC state failed: %d\n", ret);
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001356 goto end;
1357 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001358
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001359 local_irq_save(flags);
Fenghua Yub24696b2009-03-27 14:22:44 -07001360 mask_IO_APIC_setup(ioapic_entries);
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001361 mask_8259A();
1362
Fenghua Yub24696b2009-03-27 14:22:44 -07001363 ret = enable_intr_remapping(EIM_32BIT_APIC_ID);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001364
1365 if (ret && x2apic_preenabled) {
1366 local_irq_restore(flags);
1367 panic("x2apic enabled by bios. But IR enabling failed");
1368 }
1369
1370 if (ret)
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001371 goto end_restore;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001372
1373 if (!x2apic) {
1374 x2apic = 1;
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001375 enable_x2apic();
1376 }
Cyrill Gorcunov5ffa4eb2008-09-18 23:37:57 +04001377
1378end_restore:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001379 if (ret)
1380 /*
1381 * IR enabling failed
1382 */
Fenghua Yub24696b2009-03-27 14:22:44 -07001383 restore_IO_APIC_setup(ioapic_entries);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001384 else
Fenghua Yub24696b2009-03-27 14:22:44 -07001385 reinit_intr_remapped_IO_APIC(x2apic_preenabled, ioapic_entries);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001386
1387 unmask_8259A();
1388 local_irq_restore(flags);
1389
Suresh Siddha05c3dc22009-03-16 17:05:03 -07001390end:
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001391 if (!ret) {
1392 if (!x2apic_preenabled)
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001393 pr_info("Enabled x2apic and interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001394 else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001395 pr_info("Enabled Interrupt-remapping\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001396 } else
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001397 pr_err("Failed to enable Interrupt-remapping and x2apic\n");
Fenghua Yub24696b2009-03-27 14:22:44 -07001398 if (ioapic_entries)
1399 free_ioapic_entries(ioapic_entries);
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001400#else
1401 if (!cpu_has_x2apic)
1402 return;
1403
1404 if (x2apic_preenabled)
1405 panic("x2apic enabled prior OS handover,"
1406 " enable CONFIG_INTR_REMAP");
1407
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001408 pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1409 " and x2apic\n");
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001410#endif
1411
1412 return;
1413}
Yinghai Lu06cd9a72009-02-16 17:29:58 -08001414#endif /* CONFIG_X86_X2APIC */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001415
Yinghai Lube7a6562008-08-24 02:01:51 -07001416#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001417/*
1418 * Detect and enable local APICs on non-SMP boards.
1419 * Original code written by Keir Fraser.
1420 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1421 * not correctly set up (usually the APIC timer won't work etc.)
1422 */
1423static int __init detect_init_APIC(void)
1424{
1425 if (!cpu_has_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001426 pr_info("No local APIC present\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001427 return -1;
1428 }
1429
1430 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001431 boot_cpu_physical_apicid = 0;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001432 return 0;
1433}
Yinghai Lube7a6562008-08-24 02:01:51 -07001434#else
1435/*
1436 * Detect and initialize APIC
1437 */
1438static int __init detect_init_APIC(void)
1439{
1440 u32 h, l, features;
1441
1442 /* Disabled by kernel option? */
1443 if (disable_apic)
1444 return -1;
1445
1446 switch (boot_cpu_data.x86_vendor) {
1447 case X86_VENDOR_AMD:
1448 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
Borislav Petkov85877062009-02-03 16:24:22 +01001449 (boot_cpu_data.x86 >= 15))
Yinghai Lube7a6562008-08-24 02:01:51 -07001450 break;
1451 goto no_apic;
1452 case X86_VENDOR_INTEL:
1453 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1454 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1455 break;
1456 goto no_apic;
1457 default:
1458 goto no_apic;
1459 }
1460
1461 if (!cpu_has_apic) {
1462 /*
1463 * Over-ride BIOS and try to enable the local APIC only if
1464 * "lapic" specified.
1465 */
1466 if (!force_enable_local_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001467 pr_info("Local APIC disabled by BIOS -- "
1468 "you can enable it with \"lapic\"\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001469 return -1;
1470 }
1471 /*
1472 * Some BIOSes disable the local APIC in the APIC_BASE
1473 * MSR. This can only be done in software for Intel P6 or later
1474 * and AMD K7 (Model > 1) or later.
1475 */
1476 rdmsr(MSR_IA32_APICBASE, l, h);
1477 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001478 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001479 l &= ~MSR_IA32_APICBASE_BASE;
1480 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1481 wrmsr(MSR_IA32_APICBASE, l, h);
1482 enabled_via_apicbase = 1;
1483 }
1484 }
1485 /*
1486 * The APIC feature bit should now be enabled
1487 * in `cpuid'
1488 */
1489 features = cpuid_edx(1);
1490 if (!(features & (1 << X86_FEATURE_APIC))) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001491 pr_warning("Could not enable APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001492 return -1;
1493 }
1494 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1495 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1496
1497 /* The BIOS may have set up the APIC at some other address */
1498 rdmsr(MSR_IA32_APICBASE, l, h);
1499 if (l & MSR_IA32_APICBASE_ENABLE)
1500 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1501
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001502 pr_info("Found and enabled local APIC!\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001503
1504 apic_pm_activate();
1505
1506 return 0;
1507
1508no_apic:
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001509 pr_info("No local APIC present or hardware disabled\n");
Yinghai Lube7a6562008-08-24 02:01:51 -07001510 return -1;
1511}
1512#endif
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001513
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001514#ifdef CONFIG_X86_64
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001515void __init early_init_lapic_mapping(void)
1516{
Thomas Gleixner431ee792008-05-12 15:43:35 +02001517 unsigned long phys_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001518
1519 /*
1520 * If no local APIC can be found then go out
1521 * : it means there is no mpatable and MADT
1522 */
1523 if (!smp_found_config)
1524 return;
1525
Thomas Gleixner431ee792008-05-12 15:43:35 +02001526 phys_addr = mp_lapic_addr;
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001527
Thomas Gleixner431ee792008-05-12 15:43:35 +02001528 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001529 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
Thomas Gleixner431ee792008-05-12 15:43:35 +02001530 APIC_BASE, phys_addr);
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001531
1532 /*
1533 * Fetch the APIC ID of the BSP in case we have a
1534 * default configuration (or the MP table is broken).
1535 */
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001536 boot_cpu_physical_apicid = read_apic_id();
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001537}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001538#endif
Yinghai Lu8643f9d2008-02-19 03:21:06 -08001539
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001540/**
1541 * init_apic_mappings - initialize APIC mappings
1542 */
1543void __init init_apic_mappings(void)
1544{
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001545 if (x2apic) {
Yinghai Lu4c9961d2008-07-11 18:44:16 -07001546 boot_cpu_physical_apicid = read_apic_id();
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001547 return;
1548 }
1549
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001550 /*
1551 * If no local APIC can be found then set up a fake all
1552 * zeroes page to simulate the local APIC and another
1553 * one for the IO-APIC.
1554 */
1555 if (!smp_found_config && detect_init_APIC()) {
1556 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1557 apic_phys = __pa(apic_phys);
1558 } else
1559 apic_phys = mp_lapic_addr;
1560
1561 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
Yinghai Lu79c09692008-09-07 17:58:57 -07001562 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001563 APIC_BASE, apic_phys);
1564
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001565 /*
1566 * Fetch the APIC ID of the BSP in case we have a
1567 * default configuration (or the MP table is broken).
1568 */
Yinghai Luf28c0ae2008-08-24 02:01:49 -07001569 if (boot_cpu_physical_apicid == -1U)
1570 boot_cpu_physical_apicid = read_apic_id();
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001571}
1572
1573/*
1574 * This initializes the IO-APIC and APIC hardware if this is
1575 * a UP kernel.
1576 */
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001577int apic_version[MAX_APICS];
1578
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001579int __init APIC_init_uniprocessor(void)
1580{
1581 if (disable_apic) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001582 pr_info("Apic disabled\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001583 return -1;
1584 }
Jan Beulichf1182632009-01-14 12:27:35 +00001585#ifdef CONFIG_X86_64
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001586 if (!cpu_has_apic) {
1587 disable_apic = 1;
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001588 pr_info("Apic disabled by BIOS\n");
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001589 return -1;
1590 }
Yinghai Lufa2bd352008-08-24 02:01:50 -07001591#else
1592 if (!smp_found_config && !cpu_has_apic)
1593 return -1;
1594
1595 /*
1596 * Complain if the BIOS pretends there is one.
1597 */
1598 if (!cpu_has_apic &&
1599 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001600 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1601 boot_cpu_physical_apicid);
Yinghai Lufa2bd352008-08-24 02:01:50 -07001602 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1603 return -1;
1604 }
1605#endif
1606
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001607 enable_IR_x2apic();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001608#ifdef CONFIG_X86_64
Ingo Molnar72ce0162009-01-28 06:50:47 +01001609 default_setup_apic_routing();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001610#endif
Suresh Siddha6e1cb382008-07-10 11:16:58 -07001611
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001612 verify_local_APIC();
Glauber Costab5841762008-05-28 13:38:28 -03001613 connect_bsp_APIC();
1614
Yinghai Lufa2bd352008-08-24 02:01:50 -07001615#ifdef CONFIG_X86_64
Glauber de Oliveira Costac70dcb72008-03-19 14:25:58 -03001616 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
Yinghai Lufa2bd352008-08-24 02:01:50 -07001617#else
1618 /*
1619 * Hack: In case of kdump, after a crash, kernel might be booting
1620 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1621 * might be zero if read from MP tables. Get it from LAPIC.
1622 */
1623# ifdef CONFIG_CRASH_DUMP
1624 boot_cpu_physical_apicid = read_apic_id();
1625# endif
1626#endif
1627 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001628 setup_local_APIC();
1629
Yinghai Lu88d0f552009-02-14 23:57:28 -08001630#ifdef CONFIG_X86_IO_APIC
Andi Kleen739f33b2008-01-30 13:30:40 +01001631 /*
1632 * Now enable IO-APICs, actually call clear_IO_APIC
Yinghai Lu98c061b2009-02-16 00:00:50 -08001633 * We need clear_IO_APIC before enabling error vector
Andi Kleen739f33b2008-01-30 13:30:40 +01001634 */
1635 if (!skip_ioapic_setup && nr_ioapics)
1636 enable_IO_APIC();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001637#endif
Andi Kleen739f33b2008-01-30 13:30:40 +01001638
1639 end_local_APIC_setup();
1640
Yinghai Lufa2bd352008-08-24 02:01:50 -07001641#ifdef CONFIG_X86_IO_APIC
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001642 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1643 setup_IO_APIC();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001644 else {
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001645 nr_ioapics = 0;
Yinghai Lu98c061b2009-02-16 00:00:50 -08001646 localise_nmi_watchdog();
1647 }
1648#else
1649 localise_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001650#endif
1651
Yinghai Lufa2bd352008-08-24 02:01:50 -07001652 setup_boot_clock();
Yinghai Lu98c061b2009-02-16 00:00:50 -08001653#ifdef CONFIG_X86_64
1654 check_nmi_watchdog();
Yinghai Lufa2bd352008-08-24 02:01:50 -07001655#endif
1656
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001657 return 0;
1658}
1659
1660/*
1661 * Local APIC interrupts
1662 */
1663
1664/*
1665 * This interrupt should _never_ happen with our APIC/SMP architecture
1666 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001667void smp_spurious_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001668{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001669 u32 v;
1670
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001671 exit_idle();
1672 irq_enter();
1673 /*
1674 * Check if this really is a spurious interrupt and ACK it
1675 * if it is a vectored one. Just in case...
1676 * Spurious interrupts should not be ACKed.
1677 */
1678 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1679 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1680 ack_APIC_irq();
1681
Hiroshi Shimamoto915b0d02008-12-08 19:19:26 -08001682 inc_irq_stat(irq_spurious_count);
1683
Yinghai Ludc1528d2008-08-24 02:01:53 -07001684 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001685 pr_info("spurious APIC interrupt on CPU#%d, "
1686 "should never happen.\n", smp_processor_id());
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001687 irq_exit();
1688}
1689
1690/*
1691 * This interrupt should never happen with our APIC/SMP architecture
1692 */
Yinghai Ludc1528d2008-08-24 02:01:53 -07001693void smp_error_interrupt(struct pt_regs *regs)
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001694{
Yinghai Ludc1528d2008-08-24 02:01:53 -07001695 u32 v, v1;
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001696
1697 exit_idle();
1698 irq_enter();
1699 /* First tickle the hardware, only then report what went on. -- REW */
1700 v = apic_read(APIC_ESR);
1701 apic_write(APIC_ESR, 0);
1702 v1 = apic_read(APIC_ESR);
1703 ack_APIC_irq();
1704 atomic_inc(&irq_err_count);
1705
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001706 /*
1707 * Here is what the APIC error bits mean:
1708 * 0: Send CS error
1709 * 1: Receive CS error
1710 * 2: Send accept error
1711 * 3: Receive accept error
1712 * 4: Reserved
1713 * 5: Send illegal vector
1714 * 6: Received illegal vector
1715 * 7: Illegal register address
1716 */
1717 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001718 smp_processor_id(), v , v1);
1719 irq_exit();
1720}
1721
Glauber Costab5841762008-05-28 13:38:28 -03001722/**
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001723 * connect_bsp_APIC - attach the APIC to the interrupt system
1724 */
Glauber Costab5841762008-05-28 13:38:28 -03001725void __init connect_bsp_APIC(void)
1726{
Cyrill Gorcunov36c9d672008-08-18 20:45:53 +04001727#ifdef CONFIG_X86_32
1728 if (pic_mode) {
1729 /*
1730 * Do not trust the local APIC being empty at bootup.
1731 */
1732 clear_local_APIC();
1733 /*
1734 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1735 * local APIC to INT and NMI lines.
1736 */
1737 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1738 "enabling APIC mode.\n");
1739 outb(0x70, 0x22);
1740 outb(0x01, 0x23);
1741 }
1742#endif
Ingo Molnar49040332009-01-28 12:43:18 +01001743 if (apic->enable_apic_mode)
1744 apic->enable_apic_mode();
Glauber Costab5841762008-05-28 13:38:28 -03001745}
1746
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001747/**
1748 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1749 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1750 *
1751 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1752 * APIC is disabled.
1753 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001754void disconnect_bsp_APIC(int virt_wire_setup)
1755{
Cyrill Gorcunov1b4ee4e2008-08-18 23:12:33 +04001756 unsigned int value;
1757
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001758#ifdef CONFIG_X86_32
1759 if (pic_mode) {
1760 /*
1761 * Put the board back into PIC mode (has an effect only on
1762 * certain older boards). Note that APIC interrupts, including
1763 * IPIs, won't work beyond this point! The only exception are
1764 * INIT IPIs.
1765 */
1766 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1767 "entering PIC mode.\n");
1768 outb(0x70, 0x22);
1769 outb(0x00, 0x23);
1770 return;
1771 }
1772#endif
1773
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001774 /* Go back to Virtual Wire compatibility mode */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001775
1776 /* For the spurious interrupt use vector F, and enable it */
1777 value = apic_read(APIC_SPIV);
1778 value &= ~APIC_VECTOR_MASK;
1779 value |= APIC_SPIV_APIC_ENABLED;
1780 value |= 0xf;
1781 apic_write(APIC_SPIV, value);
1782
1783 if (!virt_wire_setup) {
1784 /*
1785 * For LVT0 make it edge triggered, active high,
1786 * external and enabled
1787 */
1788 value = apic_read(APIC_LVT0);
1789 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1790 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1791 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1792 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1793 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1794 apic_write(APIC_LVT0, value);
1795 } else {
1796 /* Disable LVT0 */
1797 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1798 }
1799
Cyrill Gorcunovc177b0b2008-08-18 20:45:56 +04001800 /*
1801 * For LVT1 make it edge triggered, active high,
1802 * nmi and enabled
1803 */
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001804 value = apic_read(APIC_LVT1);
1805 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1806 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1807 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1808 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1809 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1810 apic_write(APIC_LVT1, value);
1811}
1812
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001813void __cpuinit generic_processor_info(int apicid, int version)
1814{
1815 int cpu;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001816
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001817 /*
1818 * Validate version
1819 */
1820 if (version == 0x0) {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01001821 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
Mike Travis3b11ce72008-12-17 15:21:39 -08001822 "fixing up to 0x10. (tell your hw vendor)\n",
1823 version);
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001824 version = 0x10;
1825 }
1826 apic_version[apicid] = version;
1827
Mike Travis3b11ce72008-12-17 15:21:39 -08001828 if (num_processors >= nr_cpu_ids) {
1829 int max = nr_cpu_ids;
1830 int thiscpu = max + disabled_cpus;
1831
1832 pr_warning(
1833 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1834 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1835
1836 disabled_cpus++;
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001837 return;
1838 }
1839
1840 num_processors++;
Mike Travis3b11ce72008-12-17 15:21:39 -08001841 cpu = cpumask_next_zero(-1, cpu_present_mask);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001842
Mike Traviscef30b32009-01-16 15:58:13 -08001843 if (version != apic_version[boot_cpu_physical_apicid])
1844 WARN_ONCE(1,
1845 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1846 apic_version[boot_cpu_physical_apicid], cpu, version);
1847
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001848 physid_set(apicid, phys_cpu_present_map);
1849 if (apicid == boot_cpu_physical_apicid) {
1850 /*
1851 * x86_bios_cpu_apicid is required to have processors listed
1852 * in same order as logical cpu numbers. Hence the first
1853 * entry is BSP, and so on.
1854 */
1855 cpu = 0;
1856 }
Yinghai Lue0da3362008-06-08 18:29:22 -07001857 if (apicid > max_physical_apicid)
1858 max_physical_apicid = apicid;
1859
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001860#ifdef CONFIG_X86_32
1861 /*
1862 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1863 * but we need to work other dependencies like SMP_SUSPEND etc
1864 * before this can be done without some confusion.
1865 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1866 * - Ashok Raj <ashok.raj@intel.com>
1867 */
1868 if (max_physical_apicid >= 8) {
1869 switch (boot_cpu_data.x86_vendor) {
1870 case X86_VENDOR_INTEL:
1871 if (!APIC_XAPIC(version)) {
1872 def_to_bigsmp = 0;
1873 break;
1874 }
1875 /* If P4 and above fall through */
1876 case X86_VENDOR_AMD:
1877 def_to_bigsmp = 1;
1878 }
1879 }
1880#endif
1881
Ingo Molnar3e5095d2009-01-27 17:07:08 +01001882#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
Tejun Heof10fcd42009-01-13 20:41:34 +09001883 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1884 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
Cyrill Gorcunov1b313f42008-08-18 20:45:57 +04001885#endif
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001886
Mike Travis1de88cd2008-12-16 17:34:02 -08001887 set_cpu_possible(cpu, true);
1888 set_cpu_present(cpu, true);
Alexey Starikovskiybe8a5682008-03-27 23:56:19 +03001889}
1890
Suresh Siddha0c81c742008-07-10 11:16:48 -07001891int hard_smp_processor_id(void)
1892{
1893 return read_apic_id();
1894}
Ingo Molnar1dcdd3d2009-01-28 17:55:37 +01001895
1896void default_init_apic_ldr(void)
1897{
1898 unsigned long val;
1899
1900 apic_write(APIC_DFR, APIC_DFR_VALUE);
1901 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
1902 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1903 apic_write(APIC_LDR, val);
1904}
1905
1906#ifdef CONFIG_X86_32
1907int default_apicid_to_node(int logical_apicid)
1908{
1909#ifdef CONFIG_SMP
1910 return apicid_2_node[hard_smp_processor_id()];
1911#else
1912 return 0;
1913#endif
1914}
Yinghai Lu34919982008-08-24 02:01:48 -07001915#endif
Suresh Siddha0c81c742008-07-10 11:16:48 -07001916
Thomas Gleixner0e078e22008-01-30 13:30:20 +01001917/*
1918 * Power management
1919 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920#ifdef CONFIG_PM
1921
1922static struct {
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04001923 /*
1924 * 'active' is true if the local APIC was enabled by us and
1925 * not the BIOS; this signifies that we are also responsible
1926 * for disabling it before entering apm/acpi suspend
1927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 int active;
1929 /* r/w apic fields */
1930 unsigned int apic_id;
1931 unsigned int apic_taskpri;
1932 unsigned int apic_ldr;
1933 unsigned int apic_dfr;
1934 unsigned int apic_spiv;
1935 unsigned int apic_lvtt;
1936 unsigned int apic_lvtpc;
1937 unsigned int apic_lvt0;
1938 unsigned int apic_lvt1;
1939 unsigned int apic_lvterr;
1940 unsigned int apic_tmict;
1941 unsigned int apic_tdcr;
1942 unsigned int apic_thmr;
1943} apic_pm_state;
1944
Pavel Machek0b9c33a2005-04-16 15:25:31 -07001945static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946{
1947 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001948 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 if (!apic_pm_state.active)
1951 return 0;
1952
Thomas Gleixner37e650c2008-01-30 13:30:14 +01001953 maxlvt = lapic_get_maxlvt();
Karsten Wiesef990fff2006-12-07 02:14:11 +01001954
Suresh Siddha2d7a66d2008-07-11 14:24:19 -07001955 apic_pm_state.apic_id = apic_read(APIC_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1957 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1958 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1959 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1960 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
Karsten Wiesef990fff2006-12-07 02:14:11 +01001961 if (maxlvt >= 4)
1962 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1964 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1965 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1966 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1967 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001968#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01001969 if (maxlvt >= 5)
1970 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1971#endif
Cyrill Gorcunov24968cf2008-08-16 23:21:52 +04001972
Fernando Luis Vázquez Cao2b94ab22006-09-26 10:52:33 +02001973 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 disable_local_APIC();
Fenghua Yub24696b2009-03-27 14:22:44 -07001975#ifdef CONFIG_INTR_REMAP
1976 if (intr_remapping_enabled)
1977 disable_intr_remapping();
1978#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 local_irq_restore(flags);
1980 return 0;
1981}
1982
1983static int lapic_resume(struct sys_device *dev)
1984{
1985 unsigned int l, h;
1986 unsigned long flags;
Karsten Wiesef990fff2006-12-07 02:14:11 +01001987 int maxlvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
Fenghua Yub24696b2009-03-27 14:22:44 -07001989#ifdef CONFIG_INTR_REMAP
1990 int ret;
1991 struct IO_APIC_route_entry **ioapic_entries = NULL;
1992
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 if (!apic_pm_state.active)
1994 return 0;
1995
Fenghua Yub24696b2009-03-27 14:22:44 -07001996 local_irq_save(flags);
1997 if (x2apic) {
1998 ioapic_entries = alloc_ioapic_entries();
1999 if (!ioapic_entries) {
2000 WARN(1, "Alloc ioapic_entries in lapic resume failed.");
2001 return -ENOMEM;
2002 }
2003
2004 ret = save_IO_APIC_setup(ioapic_entries);
2005 if (ret) {
2006 WARN(1, "Saving IO-APIC state failed: %d\n", ret);
2007 free_ioapic_entries(ioapic_entries);
2008 return ret;
2009 }
2010
2011 mask_IO_APIC_setup(ioapic_entries);
2012 mask_8259A();
2013 enable_x2apic();
2014 }
2015#else
2016 if (!apic_pm_state.active)
2017 return 0;
Karsten Wiesef990fff2006-12-07 02:14:11 +01002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 local_irq_save(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002020 if (x2apic)
2021 enable_x2apic();
Fenghua Yub24696b2009-03-27 14:22:44 -07002022#endif
2023
Suresh Siddhacf6567f2009-03-16 17:05:00 -07002024 else {
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002025 /*
2026 * Make sure the APICBASE points to the right address
2027 *
2028 * FIXME! This will be wrong if we ever support suspend on
2029 * SMP! We'll need to do this as part of the CPU restore!
2030 */
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002031 rdmsr(MSR_IA32_APICBASE, l, h);
2032 l &= ~MSR_IA32_APICBASE_BASE;
2033 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2034 wrmsr(MSR_IA32_APICBASE, l, h);
Yinghai Lud5e629a2008-08-17 21:12:27 -07002035 }
Suresh Siddha6e1cb382008-07-10 11:16:58 -07002036
Fenghua Yub24696b2009-03-27 14:22:44 -07002037 maxlvt = lapic_get_maxlvt();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2039 apic_write(APIC_ID, apic_pm_state.apic_id);
2040 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2041 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2042 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2043 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2044 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2045 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002046#if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
Karsten Wiesef990fff2006-12-07 02:14:11 +01002047 if (maxlvt >= 5)
2048 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2049#endif
2050 if (maxlvt >= 4)
2051 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2053 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2054 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2055 apic_write(APIC_ESR, 0);
2056 apic_read(APIC_ESR);
2057 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2058 apic_write(APIC_ESR, 0);
2059 apic_read(APIC_ESR);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002060
Fenghua Yub24696b2009-03-27 14:22:44 -07002061#ifdef CONFIG_INTR_REMAP
2062 if (intr_remapping_enabled)
2063 reenable_intr_remapping(EIM_32BIT_APIC_ID);
2064
2065 if (x2apic) {
2066 unmask_8259A();
2067 restore_IO_APIC_setup(ioapic_entries);
2068 free_ioapic_entries(ioapic_entries);
2069 }
2070#endif
2071
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 local_irq_restore(flags);
Cyrill Gorcunov92206c92008-08-16 23:21:51 +04002073
Fenghua Yub24696b2009-03-27 14:22:44 -07002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 return 0;
2076}
2077
Cyrill Gorcunov274cfe52008-08-16 23:21:53 +04002078/*
2079 * This device has no shutdown method - fully functioning local APICs
2080 * are needed on every CPU up until machine_halt/restart/poweroff.
2081 */
2082
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083static struct sysdev_class lapic_sysclass = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +01002084 .name = "lapic",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 .resume = lapic_resume,
2086 .suspend = lapic_suspend,
2087};
2088
2089static struct sys_device device_lapic = {
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002090 .id = 0,
2091 .cls = &lapic_sysclass,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092};
2093
Ashok Raje6982c62005-06-25 14:54:58 -07002094static void __cpuinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095{
2096 apic_pm_state.active = 1;
2097}
2098
2099static int __init init_lapic_sysfs(void)
2100{
2101 int error;
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002102
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 if (!cpu_has_apic)
2104 return 0;
2105 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
Hiroshi Shimamotoe83a5fd2008-01-30 13:32:35 +01002106
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 error = sysdev_class_register(&lapic_sysclass);
2108 if (!error)
2109 error = sysdev_register(&device_lapic);
2110 return error;
2111}
Fenghua Yub24696b2009-03-27 14:22:44 -07002112
2113/* local apic needs to resume before other devices access its registers. */
2114core_initcall(init_lapic_sysfs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115
2116#else /* CONFIG_PM */
2117
2118static void apic_pm_activate(void) { }
2119
2120#endif /* CONFIG_PM */
2121
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002122#ifdef CONFIG_X86_64
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123/*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002124 * apic_is_clustered_box() -- Check if we can expect good TSC
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 *
2126 * Thus far, the major user of this is IBM's Summit2 series:
2127 *
Linus Torvalds637029c2006-02-27 20:41:56 -08002128 * Clustered boxes may have unsynced TSC problems if they are
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 * multi-chassis. Use available data to take a good guess.
2130 * If in doubt, go HPET.
2131 */
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002132__cpuinit int apic_is_clustered_box(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 int i, clusters, zeros;
2135 unsigned id;
Yinghai Lu322850a2008-02-23 21:48:42 -08002136 u16 *bios_cpu_apicid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2138
Yinghai Lu322850a2008-02-23 21:48:42 -08002139 /*
2140 * there is not this kind of box with AMD CPU yet.
2141 * Some AMD box with quadcore cpu and 8 sockets apicid
2142 * will be [4, 0x23] or [8, 0x27] could be thought to
Yinghai Luf8fffa42008-02-24 21:36:28 -08002143 * vsmp box still need checking...
Yinghai Lu322850a2008-02-23 21:48:42 -08002144 */
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002145 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
Yinghai Lu322850a2008-02-23 21:48:42 -08002146 return 0;
2147
Mike Travis23ca4bb2008-05-12 21:21:12 +02002148 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
Suresh Siddha376ec332005-05-16 21:53:32 -07002149 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Mike Travis168ef542008-12-16 17:34:01 -08002151 for (i = 0; i < nr_cpu_ids; i++) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002152 /* are we being called early in kernel startup? */
Mike Travis693e3c52008-01-30 13:33:14 +01002153 if (bios_cpu_apicid) {
2154 id = bios_cpu_apicid[i];
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302155 } else if (i < nr_cpu_ids) {
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002156 if (cpu_present(i))
2157 id = per_cpu(x86_bios_cpu_apicid, i);
2158 else
2159 continue;
Jaswinder Singh Rajpute423e332009-01-04 16:16:25 +05302160 } else
travis@sgi.come8c10ef2008-01-30 13:33:12 +01002161 break;
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (id != BAD_APICID)
2164 __set_bit(APIC_CLUSTERID(id), clustermap);
2165 }
2166
2167 /* Problem: Partially populated chassis may not have CPUs in some of
2168 * the APIC clusters they have been allocated. Only present CPUs have
travis@sgi.com602a54a2008-01-30 13:33:21 +01002169 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2170 * Since clusters are allocated sequentially, count zeros only if
2171 * they are bounded by ones.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 */
2173 clusters = 0;
2174 zeros = 0;
2175 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2176 if (test_bit(i, clustermap)) {
2177 clusters += 1 + zeros;
2178 zeros = 0;
2179 } else
2180 ++zeros;
2181 }
2182
Ravikiran G Thirumalai1cb68482008-03-20 00:45:08 -07002183 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2184 * not guaranteed to be synced between boards
2185 */
2186 if (is_vsmp_box() && clusters > 1)
2187 return 1;
2188
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /*
Vojtech Pavlikf8bf3c62006-06-26 13:58:23 +02002190 * If clusters > 2, then should be multi-chassis.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 * May have to revisit this when multi-core + hyperthreaded CPUs come
2192 * out, but AFAIK this will work even for them.
2193 */
2194 return (clusters > 2);
2195}
Yinghai Luf28c0ae2008-08-24 02:01:49 -07002196#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198/*
Thomas Gleixner0e078e22008-01-30 13:30:20 +01002199 * APIC command line parameters
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002201static int __init setup_disableapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002202{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 disable_apic = 1;
Yinghai Lu9175fc02008-07-21 01:38:14 -07002204 setup_clear_cpu_cap(X86_FEATURE_APIC);
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002205 return 0;
2206}
2207early_param("disableapic", setup_disableapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002209/* same as disableapic, for compatibility */
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002210static int __init setup_nolapic(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002211{
Cyrill Gorcunov789fa732008-08-18 20:46:01 +04002212 return setup_disableapic(arg);
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002213}
Andi Kleen2c8c0e62006-09-26 10:52:32 +02002214early_param("nolapic", setup_nolapic);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Linus Torvalds2e7c2832007-03-23 11:32:31 -07002216static int __init parse_lapic_timer_c2_ok(char *arg)
2217{
2218 local_apic_timer_c2_ok = 1;
2219 return 0;
2220}
2221early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2222
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002223static int __init parse_disable_apic_timer(char *arg)
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 disable_apic_timer = 1;
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002226 return 0;
Thomas Gleixner6935d1f2007-07-21 17:10:17 +02002227}
Cyrill Gorcunov36fef092008-08-15 13:51:20 +02002228early_param("noapictimer", parse_disable_apic_timer);
2229
2230static int __init parse_nolapic_timer(char *arg)
2231{
2232 disable_apic_timer = 1;
2233 return 0;
2234}
2235early_param("nolapic_timer", parse_nolapic_timer);
Andi Kleen73dea472006-02-03 21:50:50 +01002236
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002237static int __init apic_set_verbosity(char *arg)
2238{
2239 if (!arg) {
2240#ifdef CONFIG_X86_64
2241 skip_ioapic_setup = 0;
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002242 return 0;
2243#endif
2244 return -EINVAL;
2245 }
2246
2247 if (strcmp("debug", arg) == 0)
2248 apic_verbosity = APIC_DEBUG;
2249 else if (strcmp("verbose", arg) == 0)
2250 apic_verbosity = APIC_VERBOSE;
2251 else {
Cyrill Gorcunovba21ebb2008-11-10 09:16:41 +01002252 pr_warning("APIC Verbosity level %s not recognised"
Cyrill Gorcunov79af9be2008-08-18 20:46:00 +04002253 " use apic=verbose or apic=debug\n", arg);
2254 return -EINVAL;
2255 }
2256
2257 return 0;
2258}
2259early_param("apic", apic_set_verbosity);
2260
Yinghai Lu1e934dd2008-02-22 13:37:26 -08002261static int __init lapic_insert_resource(void)
2262{
2263 if (!apic_phys)
2264 return -1;
2265
2266 /* Put local APIC into the resource map. */
2267 lapic_resource.start = apic_phys;
2268 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2269 insert_resource(&iomem_resource, &lapic_resource);
2270
2271 return 0;
2272}
2273
2274/*
2275 * need call insert after e820_reserve_resources()
2276 * that is using request_resource
2277 */
2278late_initcall(lapic_insert_resource);