x86/cpufeature: Remove cpu_has_osxsave
[linux-2.6-block.git] / arch / x86 / kernel / apic / apic.c
CommitLineData
1da177e4
LT
1/*
2 * Local APIC handling, local APIC timers
3 *
8f47e163 4 * (c) 1999, 2000, 2009 Ingo Molnar <mingo@redhat.com>
1da177e4
LT
5 *
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
cdd6c482 17#include <linux/perf_event.h>
1da177e4 18#include <linux/kernel_stat.h>
d1de36f5 19#include <linux/mc146818rtc.h>
70a20025 20#include <linux/acpi_pmtmr.h>
d1de36f5
IM
21#include <linux/clockchips.h>
22#include <linux/interrupt.h>
23#include <linux/bootmem.h>
24#include <linux/ftrace.h>
25#include <linux/ioport.h>
e83a5fdc 26#include <linux/module.h>
f3c6ea1b 27#include <linux/syscore_ops.h>
d1de36f5
IM
28#include <linux/delay.h>
29#include <linux/timex.h>
334955ef 30#include <linux/i8253.h>
6e1cb38a 31#include <linux/dmar.h>
d1de36f5
IM
32#include <linux/init.h>
33#include <linux/cpu.h>
34#include <linux/dmi.h>
d1de36f5
IM
35#include <linux/smp.h>
36#include <linux/mm.h>
1da177e4 37
83ab8514 38#include <asm/trace/irq_vectors.h>
8a8f422d 39#include <asm/irq_remapping.h>
cdd6c482 40#include <asm/perf_event.h>
736decac 41#include <asm/x86_init.h>
1da177e4 42#include <asm/pgalloc.h>
60063497 43#include <linux/atomic.h>
1da177e4 44#include <asm/mpspec.h>
d1de36f5 45#include <asm/i8259.h>
73dea47f 46#include <asm/proto.h>
2c8c0e6b 47#include <asm/apic.h>
7167d08e 48#include <asm/io_apic.h>
d1de36f5
IM
49#include <asm/desc.h>
50#include <asm/hpet.h>
51#include <asm/idle.h>
52#include <asm/mtrr.h>
16f871bc 53#include <asm/time.h>
2bc13797 54#include <asm/smp.h>
be71b855 55#include <asm/mce.h>
8c3ba8d0 56#include <asm/tsc.h>
2904ed8d 57#include <asm/hypervisor.h>
1da177e4 58
ec70de8b 59unsigned int num_processors;
fdbecd9f 60
148f9bb8 61unsigned disabled_cpus;
fdbecd9f 62
ec70de8b
BG
63/* Processor that is doing the boot up */
64unsigned int boot_cpu_physical_apicid = -1U;
cc08e04c 65EXPORT_SYMBOL_GPL(boot_cpu_physical_apicid);
5af5573e 66
80e5609c 67/*
fdbecd9f 68 * The highest APIC ID seen during enumeration.
80e5609c 69 */
a491cc90 70static unsigned int max_physical_apicid;
5af5573e 71
80e5609c 72/*
fdbecd9f 73 * Bitmask of physically existing CPUs:
80e5609c 74 */
ec70de8b
BG
75physid_mask_t phys_cpu_present_map;
76
151e0c7d
HD
77/*
78 * Processor to be disabled specified by kernel parameter
79 * disable_cpu_apicid=<int>, mostly used for the kdump 2nd kernel to
80 * avoid undefined behaviour caused by sending INIT from AP to BSP.
81 */
5b4d1dbc 82static unsigned int disabled_cpu_apicid __read_mostly = BAD_APICID;
151e0c7d 83
b7c4948e
HK
84/*
85 * This variable controls which CPUs receive external NMIs. By default,
86 * external NMIs are delivered only to the BSP.
87 */
88static int apic_extnmi = APIC_EXTNMI_BSP;
89
ec70de8b
BG
90/*
91 * Map cpu index to physical APIC ID
92 */
0816b0f0
VZ
93DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_cpu_to_apicid, BAD_APICID);
94DEFINE_EARLY_PER_CPU_READ_MOSTLY(u16, x86_bios_cpu_apicid, BAD_APICID);
ec70de8b
BG
95EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
96EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
80e5609c 97
b3c51170 98#ifdef CONFIG_X86_32
4c321ff8 99
4c321ff8
TH
100/*
101 * On x86_32, the mapping between cpu and logical apicid may vary
102 * depending on apic in use. The following early percpu variable is
103 * used for the mapping. This is where the behaviors of x86_64 and 32
104 * actually diverge. Let's keep it ugly for now.
105 */
0816b0f0 106DEFINE_EARLY_PER_CPU_READ_MOSTLY(int, x86_cpu_to_logical_apicid, BAD_APICID);
4c321ff8 107
f28c0ae2
YL
108/* Local APIC was disabled by the BIOS and enabled by the kernel */
109static int enabled_via_apicbase;
110
c0eaa453
CG
111/*
112 * Handle interrupt mode configuration register (IMCR).
113 * This register controls whether the interrupt signals
114 * that reach the BSP come from the master PIC or from the
115 * local APIC. Before entering Symmetric I/O Mode, either
116 * the BIOS or the operating system must switch out of
117 * PIC Mode by changing the IMCR.
118 */
5cda395f 119static inline void imcr_pic_to_apic(void)
c0eaa453
CG
120{
121 /* select IMCR register */
122 outb(0x70, 0x22);
123 /* NMI and 8259 INTR go through APIC */
124 outb(0x01, 0x23);
125}
126
5cda395f 127static inline void imcr_apic_to_pic(void)
c0eaa453
CG
128{
129 /* select IMCR register */
130 outb(0x70, 0x22);
131 /* NMI and 8259 INTR go directly to BSP */
132 outb(0x00, 0x23);
133}
b3c51170
YL
134#endif
135
279f1461
SS
136/*
137 * Knob to control our willingness to enable the local APIC.
138 *
139 * +1=force-enable
140 */
141static int force_enable_local_apic __initdata;
dc9788f4 142
279f1461
SS
143/*
144 * APIC command line parameters
145 */
146static int __init parse_lapic(char *arg)
147{
148 if (config_enabled(CONFIG_X86_32) && !arg)
149 force_enable_local_apic = 1;
27cf9298 150 else if (arg && !strncmp(arg, "notscdeadline", 13))
279f1461
SS
151 setup_clear_cpu_cap(X86_FEATURE_TSC_DEADLINE_TIMER);
152 return 0;
153}
154early_param("lapic", parse_lapic);
155
b3c51170 156#ifdef CONFIG_X86_64
bc1d99c1 157static int apic_calibrate_pmtmr __initdata;
b3c51170
YL
158static __init int setup_apicpmtimer(char *s)
159{
160 apic_calibrate_pmtmr = 1;
161 notsc_setup(NULL);
162 return 0;
163}
164__setup("apicpmtimer", setup_apicpmtimer);
165#endif
166
b3c51170
YL
167unsigned long mp_lapic_addr;
168int disable_apic;
169/* Disable local APIC timer from the kernel commandline or via dmi quirk */
25874a29 170static int disable_apic_timer __initdata;
e83a5fdc 171/* Local APIC timer works in C2 */
2e7c2838
LT
172int local_apic_timer_c2_ok;
173EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
174
2414e021 175int first_system_vector = FIRST_SYSTEM_VECTOR;
efa2559f 176
e83a5fdc
HS
177/*
178 * Debug level, exported for io_apic.c
179 */
baa13188 180unsigned int apic_verbosity;
e83a5fdc 181
89c38c28
CG
182int pic_mode;
183
bab4b27c
AS
184/* Have we found an MP table */
185int smp_found_config;
186
39928722
AD
187static struct resource lapic_resource = {
188 .name = "Local APIC",
189 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
190};
191
1ade93ef 192unsigned int lapic_timer_frequency = 0;
d03030e9 193
0e078e2f 194static void apic_pm_activate(void);
ba7eda4c 195
d3432896
AK
196static unsigned long apic_phys;
197
0e078e2f
TG
198/*
199 * Get the LAPIC version
200 */
201static inline int lapic_get_version(void)
ba7eda4c 202{
0e078e2f 203 return GET_APIC_VERSION(apic_read(APIC_LVR));
ba7eda4c
TG
204}
205
0e078e2f 206/*
9c803869 207 * Check, if the APIC is integrated or a separate chip
0e078e2f
TG
208 */
209static inline int lapic_is_integrated(void)
ba7eda4c 210{
9c803869 211#ifdef CONFIG_X86_64
0e078e2f 212 return 1;
9c803869
CG
213#else
214 return APIC_INTEGRATED(lapic_get_version());
215#endif
ba7eda4c
TG
216}
217
218/*
0e078e2f 219 * Check, whether this is a modern or a first generation APIC
ba7eda4c 220 */
0e078e2f 221static int modern_apic(void)
ba7eda4c 222{
0e078e2f
TG
223 /* AMD systems use old APIC versions, so check the CPU */
224 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
225 boot_cpu_data.x86 >= 0xf)
226 return 1;
227 return lapic_get_version() >= 0x14;
ba7eda4c
TG
228}
229
08306ce6 230/*
a933c618
CG
231 * right after this call apic become NOOP driven
232 * so apic->write/read doesn't do anything
08306ce6 233 */
25874a29 234static void __init apic_disable(void)
08306ce6 235{
f88f2b4f 236 pr_info("APIC: switched to apic NOOP\n");
a933c618 237 apic = &apic_noop;
08306ce6
CG
238}
239
c1eeb2de 240void native_apic_wait_icr_idle(void)
8339e9fb
FLV
241{
242 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
243 cpu_relax();
244}
245
c1eeb2de 246u32 native_safe_apic_wait_icr_idle(void)
8339e9fb 247{
3c6bb07a 248 u32 send_status;
8339e9fb
FLV
249 int timeout;
250
251 timeout = 0;
252 do {
253 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
254 if (!send_status)
255 break;
b49d7d87 256 inc_irq_stat(icr_read_retry_count);
8339e9fb
FLV
257 udelay(100);
258 } while (timeout++ < 1000);
259
260 return send_status;
261}
262
c1eeb2de 263void native_apic_icr_write(u32 low, u32 id)
1b374e4d 264{
ea7bdc65
JK
265 unsigned long flags;
266
267 local_irq_save(flags);
ed4e5ec1 268 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
1b374e4d 269 apic_write(APIC_ICR, low);
ea7bdc65 270 local_irq_restore(flags);
1b374e4d
SS
271}
272
c1eeb2de 273u64 native_apic_icr_read(void)
1b374e4d
SS
274{
275 u32 icr1, icr2;
276
277 icr2 = apic_read(APIC_ICR2);
278 icr1 = apic_read(APIC_ICR);
279
cf9768d7 280 return icr1 | ((u64)icr2 << 32);
1b374e4d
SS
281}
282
7c37e48b
CG
283#ifdef CONFIG_X86_32
284/**
285 * get_physical_broadcast - Get number of physical broadcast IDs
286 */
287int get_physical_broadcast(void)
288{
289 return modern_apic() ? 0xff : 0xf;
290}
291#endif
292
0e078e2f
TG
293/**
294 * lapic_get_maxlvt - get the maximum number of local vector table entries
295 */
37e650c7 296int lapic_get_maxlvt(void)
1da177e4 297{
36a028de 298 unsigned int v;
1da177e4
LT
299
300 v = apic_read(APIC_LVR);
36a028de
CG
301 /*
302 * - we always have APIC integrated on 64bit mode
303 * - 82489DXs do not report # of LVT entries
304 */
305 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
1da177e4
LT
306}
307
274cfe59
CG
308/*
309 * Local APIC timer
310 */
311
c40aaec6 312/* Clock divisor */
c40aaec6 313#define APIC_DIVISOR 16
279f1461 314#define TSC_DIVISOR 32
f07f4f90 315
0e078e2f
TG
316/*
317 * This function sets up the local APIC timer, with a timeout of
318 * 'clocks' APIC bus clock. During calibration we actually call
319 * this function twice on the boot CPU, once with a bogus timeout
320 * value, second time for real. The other (noncalibrating) CPUs
321 * call this function only once, with the real, calibrated value.
322 *
323 * We do reads before writes even if unnecessary, to get around the
324 * P5 APIC double write bug.
325 */
0e078e2f 326static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
1da177e4 327{
0e078e2f 328 unsigned int lvtt_value, tmp_value;
1da177e4 329
0e078e2f
TG
330 lvtt_value = LOCAL_TIMER_VECTOR;
331 if (!oneshot)
332 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
279f1461
SS
333 else if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER))
334 lvtt_value |= APIC_LVT_TIMER_TSCDEADLINE;
335
f07f4f90
CG
336 if (!lapic_is_integrated())
337 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
338
0e078e2f
TG
339 if (!irqen)
340 lvtt_value |= APIC_LVT_MASKED;
1da177e4 341
0e078e2f 342 apic_write(APIC_LVTT, lvtt_value);
1da177e4 343
279f1461 344 if (lvtt_value & APIC_LVT_TIMER_TSCDEADLINE) {
5d7c631d
SL
345 /*
346 * See Intel SDM: TSC-Deadline Mode chapter. In xAPIC mode,
347 * writing to the APIC LVTT and TSC_DEADLINE MSR isn't serialized.
348 * According to Intel, MFENCE can do the serialization here.
349 */
350 asm volatile("mfence" : : : "memory");
351
279f1461
SS
352 printk_once(KERN_DEBUG "TSC deadline timer enabled\n");
353 return;
354 }
355
1da177e4 356 /*
0e078e2f 357 * Divide PICLK by 16
1da177e4 358 */
0e078e2f 359 tmp_value = apic_read(APIC_TDCR);
c40aaec6
CG
360 apic_write(APIC_TDCR,
361 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
362 APIC_TDR_DIV_16);
0e078e2f
TG
363
364 if (!oneshot)
f07f4f90 365 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
1da177e4
LT
366}
367
0e078e2f 368/*
a68c439b 369 * Setup extended LVT, AMD specific
7b83dae7 370 *
a68c439b
RR
371 * Software should use the LVT offsets the BIOS provides. The offsets
372 * are determined by the subsystems using it like those for MCE
373 * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
374 * are supported. Beginning with family 10h at least 4 offsets are
375 * available.
286f5718 376 *
a68c439b
RR
377 * Since the offsets must be consistent for all cores, we keep track
378 * of the LVT offsets in software and reserve the offset for the same
379 * vector also to be used on other cores. An offset is freed by
380 * setting the entry to APIC_EILVT_MASKED.
381 *
382 * If the BIOS is right, there should be no conflicts. Otherwise a
383 * "[Firmware Bug]: ..." error message is generated. However, if
384 * software does not properly determines the offsets, it is not
385 * necessarily a BIOS bug.
0e078e2f 386 */
7b83dae7 387
a68c439b
RR
388static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
389
390static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
391{
392 return (old & APIC_EILVT_MASKED)
393 || (new == APIC_EILVT_MASKED)
394 || ((new & ~APIC_EILVT_MASKED) == old);
395}
396
397static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
398{
8abc3122 399 unsigned int rsvd, vector;
a68c439b
RR
400
401 if (offset >= APIC_EILVT_NR_MAX)
402 return ~0;
403
8abc3122 404 rsvd = atomic_read(&eilvt_offsets[offset]);
a68c439b 405 do {
8abc3122
RR
406 vector = rsvd & ~APIC_EILVT_MASKED; /* 0: unassigned */
407 if (vector && !eilvt_entry_is_changeable(vector, new))
a68c439b
RR
408 /* may not change if vectors are different */
409 return rsvd;
410 rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
411 } while (rsvd != new);
412
8abc3122
RR
413 rsvd &= ~APIC_EILVT_MASKED;
414 if (rsvd && rsvd != vector)
415 pr_info("LVT offset %d assigned for vector 0x%02x\n",
416 offset, rsvd);
417
a68c439b
RR
418 return new;
419}
420
421/*
422 * If mask=1, the LVT entry does not generate interrupts while mask=0
cbf74cea
RR
423 * enables the vector. See also the BKDGs. Must be called with
424 * preemption disabled.
a68c439b
RR
425 */
426
27afdf20 427int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
1da177e4 428{
a68c439b
RR
429 unsigned long reg = APIC_EILVTn(offset);
430 unsigned int new, old, reserved;
431
432 new = (mask << 16) | (msg_type << 8) | vector;
433 old = apic_read(reg);
434 reserved = reserve_eilvt_offset(offset, new);
435
436 if (reserved != new) {
eb48c9cb
RR
437 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
438 "vector 0x%x, but the register is already in use for "
439 "vector 0x%x on another cpu\n",
440 smp_processor_id(), reg, offset, new, reserved);
a68c439b
RR
441 return -EINVAL;
442 }
443
444 if (!eilvt_entry_is_changeable(old, new)) {
eb48c9cb
RR
445 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
446 "vector 0x%x, but the register is already in use for "
447 "vector 0x%x on this cpu\n",
448 smp_processor_id(), reg, offset, new, old);
a68c439b
RR
449 return -EBUSY;
450 }
451
452 apic_write(reg, new);
a8fcf1a2 453
a68c439b 454 return 0;
1da177e4 455}
27afdf20 456EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
7b83dae7 457
0e078e2f
TG
458/*
459 * Program the next event, relative to now
460 */
461static int lapic_next_event(unsigned long delta,
462 struct clock_event_device *evt)
1da177e4 463{
0e078e2f
TG
464 apic_write(APIC_TMICT, delta);
465 return 0;
1da177e4
LT
466}
467
279f1461
SS
468static int lapic_next_deadline(unsigned long delta,
469 struct clock_event_device *evt)
470{
471 u64 tsc;
472
4ea1636b 473 tsc = rdtsc();
279f1461
SS
474 wrmsrl(MSR_IA32_TSC_DEADLINE, tsc + (((u64) delta) * TSC_DIVISOR));
475 return 0;
476}
477
b23d8e52 478static int lapic_timer_shutdown(struct clock_event_device *evt)
9b7711f0 479{
0e078e2f 480 unsigned int v;
9b7711f0 481
0e078e2f
TG
482 /* Lapic used as dummy for broadcast ? */
483 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
b23d8e52 484 return 0;
9b7711f0 485
b23d8e52
VK
486 v = apic_read(APIC_LVTT);
487 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
488 apic_write(APIC_LVTT, v);
489 apic_write(APIC_TMICT, 0);
b23d8e52
VK
490 return 0;
491}
9b7711f0 492
b23d8e52
VK
493static inline int
494lapic_timer_set_periodic_oneshot(struct clock_event_device *evt, bool oneshot)
495{
b23d8e52
VK
496 /* Lapic used as dummy for broadcast ? */
497 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
498 return 0;
9b7711f0 499
b23d8e52 500 __setup_APIC_LVTT(lapic_timer_frequency, oneshot, 1);
b23d8e52
VK
501 return 0;
502}
503
504static int lapic_timer_set_periodic(struct clock_event_device *evt)
505{
506 return lapic_timer_set_periodic_oneshot(evt, false);
507}
508
509static int lapic_timer_set_oneshot(struct clock_event_device *evt)
510{
511 return lapic_timer_set_periodic_oneshot(evt, true);
9b7711f0
HS
512}
513
1da177e4 514/*
0e078e2f 515 * Local APIC timer broadcast function
1da177e4 516 */
9628937d 517static void lapic_timer_broadcast(const struct cpumask *mask)
1da177e4 518{
0e078e2f 519#ifdef CONFIG_SMP
dac5f412 520 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
0e078e2f
TG
521#endif
522}
1da177e4 523
25874a29
HK
524
525/*
526 * The local apic timer can be used for any function which is CPU local.
527 */
528static struct clock_event_device lapic_clockevent = {
b23d8e52
VK
529 .name = "lapic",
530 .features = CLOCK_EVT_FEAT_PERIODIC |
531 CLOCK_EVT_FEAT_ONESHOT | CLOCK_EVT_FEAT_C3STOP
532 | CLOCK_EVT_FEAT_DUMMY,
533 .shift = 32,
534 .set_state_shutdown = lapic_timer_shutdown,
535 .set_state_periodic = lapic_timer_set_periodic,
536 .set_state_oneshot = lapic_timer_set_oneshot,
537 .set_next_event = lapic_next_event,
538 .broadcast = lapic_timer_broadcast,
539 .rating = 100,
540 .irq = -1,
25874a29
HK
541};
542static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
543
0e078e2f 544/*
421f91d2 545 * Setup the local APIC timer for this CPU. Copy the initialized values
0e078e2f
TG
546 * of the boot CPU and register the clock event in the framework.
547 */
148f9bb8 548static void setup_APIC_timer(void)
0e078e2f 549{
89cbc767 550 struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
1da177e4 551
349c004e 552 if (this_cpu_has(X86_FEATURE_ARAT)) {
db954b58
VP
553 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
554 /* Make LAPIC timer preferrable over percpu HPET */
555 lapic_clockevent.rating = 150;
556 }
557
0e078e2f 558 memcpy(levt, &lapic_clockevent, sizeof(*levt));
320ab2b0 559 levt->cpumask = cpumask_of(smp_processor_id());
1da177e4 560
279f1461
SS
561 if (this_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
562 levt->features &= ~(CLOCK_EVT_FEAT_PERIODIC |
563 CLOCK_EVT_FEAT_DUMMY);
564 levt->set_next_event = lapic_next_deadline;
565 clockevents_config_and_register(levt,
566 (tsc_khz / TSC_DIVISOR) * 1000,
567 0xF, ~0UL);
568 } else
569 clockevents_register_device(levt);
0e078e2f 570}
1da177e4 571
2f04fa88
YL
572/*
573 * In this functions we calibrate APIC bus clocks to the external timer.
574 *
575 * We want to do the calibration only once since we want to have local timer
576 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
577 * frequency.
578 *
579 * This was previously done by reading the PIT/HPET and waiting for a wrap
580 * around to find out, that a tick has elapsed. I have a box, where the PIT
581 * readout is broken, so it never gets out of the wait loop again. This was
582 * also reported by others.
583 *
584 * Monitoring the jiffies value is inaccurate and the clockevents
585 * infrastructure allows us to do a simple substitution of the interrupt
586 * handler.
587 *
588 * The calibration routine also uses the pm_timer when possible, as the PIT
589 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
590 * back to normal later in the boot process).
591 */
592
593#define LAPIC_CAL_LOOPS (HZ/10)
594
595static __initdata int lapic_cal_loops = -1;
596static __initdata long lapic_cal_t1, lapic_cal_t2;
597static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
598static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
599static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
600
601/*
602 * Temporary interrupt handler.
603 */
604static void __init lapic_cal_handler(struct clock_event_device *dev)
605{
606 unsigned long long tsc = 0;
607 long tapic = apic_read(APIC_TMCCT);
608 unsigned long pm = acpi_pm_read_early();
609
610 if (cpu_has_tsc)
4ea1636b 611 tsc = rdtsc();
2f04fa88
YL
612
613 switch (lapic_cal_loops++) {
614 case 0:
615 lapic_cal_t1 = tapic;
616 lapic_cal_tsc1 = tsc;
617 lapic_cal_pm1 = pm;
618 lapic_cal_j1 = jiffies;
619 break;
620
621 case LAPIC_CAL_LOOPS:
622 lapic_cal_t2 = tapic;
623 lapic_cal_tsc2 = tsc;
624 if (pm < lapic_cal_pm1)
625 pm += ACPI_PM_OVRRUN;
626 lapic_cal_pm2 = pm;
627 lapic_cal_j2 = jiffies;
628 break;
629 }
630}
631
754ef0cd
YI
632static int __init
633calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
b189892d
CG
634{
635 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
636 const long pm_thresh = pm_100ms / 100;
637 unsigned long mult;
638 u64 res;
639
640#ifndef CONFIG_X86_PM_TIMER
641 return -1;
642#endif
643
39ba5d43 644 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
b189892d
CG
645
646 /* Check, if the PM timer is available */
647 if (!deltapm)
648 return -1;
649
650 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
651
652 if (deltapm > (pm_100ms - pm_thresh) &&
653 deltapm < (pm_100ms + pm_thresh)) {
39ba5d43 654 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
754ef0cd
YI
655 return 0;
656 }
657
658 res = (((u64)deltapm) * mult) >> 22;
659 do_div(res, 1000000);
660 pr_warning("APIC calibration not consistent "
39ba5d43 661 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
754ef0cd
YI
662
663 /* Correct the lapic counter value */
664 res = (((u64)(*delta)) * pm_100ms);
665 do_div(res, deltapm);
666 pr_info("APIC delta adjusted to PM-Timer: "
667 "%lu (%ld)\n", (unsigned long)res, *delta);
668 *delta = (long)res;
669
670 /* Correct the tsc counter value */
671 if (cpu_has_tsc) {
672 res = (((u64)(*deltatsc)) * pm_100ms);
b189892d 673 do_div(res, deltapm);
754ef0cd 674 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
3235dc3f 675 "PM-Timer: %lu (%ld)\n",
754ef0cd
YI
676 (unsigned long)res, *deltatsc);
677 *deltatsc = (long)res;
b189892d
CG
678 }
679
680 return 0;
681}
682
2f04fa88
YL
683static int __init calibrate_APIC_clock(void)
684{
89cbc767 685 struct clock_event_device *levt = this_cpu_ptr(&lapic_events);
2f04fa88
YL
686 void (*real_handler)(struct clock_event_device *dev);
687 unsigned long deltaj;
754ef0cd 688 long delta, deltatsc;
2f04fa88
YL
689 int pm_referenced = 0;
690
1ade93ef
JP
691 /**
692 * check if lapic timer has already been calibrated by platform
693 * specific routine, such as tsc calibration code. if so, we just fill
694 * in the clockevent structure and return.
695 */
696
279f1461
SS
697 if (boot_cpu_has(X86_FEATURE_TSC_DEADLINE_TIMER)) {
698 return 0;
699 } else if (lapic_timer_frequency) {
1ade93ef
JP
700 apic_printk(APIC_VERBOSE, "lapic timer already calibrated %d\n",
701 lapic_timer_frequency);
702 lapic_clockevent.mult = div_sc(lapic_timer_frequency/APIC_DIVISOR,
703 TICK_NSEC, lapic_clockevent.shift);
704 lapic_clockevent.max_delta_ns =
705 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
706 lapic_clockevent.min_delta_ns =
707 clockevent_delta2ns(0xF, &lapic_clockevent);
708 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
709 return 0;
710 }
711
279f1461
SS
712 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
713 "calibrating APIC timer ...\n");
714
2f04fa88
YL
715 local_irq_disable();
716
717 /* Replace the global interrupt handler */
718 real_handler = global_clock_event->event_handler;
719 global_clock_event->event_handler = lapic_cal_handler;
720
721 /*
81608f3c 722 * Setup the APIC counter to maximum. There is no way the lapic
2f04fa88
YL
723 * can underflow in the 100ms detection time frame
724 */
81608f3c 725 __setup_APIC_LVTT(0xffffffff, 0, 0);
2f04fa88
YL
726
727 /* Let the interrupts run */
728 local_irq_enable();
729
730 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
731 cpu_relax();
732
733 local_irq_disable();
734
735 /* Restore the real event handler */
736 global_clock_event->event_handler = real_handler;
737
738 /* Build delta t1-t2 as apic timer counts down */
739 delta = lapic_cal_t1 - lapic_cal_t2;
740 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
741
754ef0cd
YI
742 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
743
b189892d
CG
744 /* we trust the PM based calibration if possible */
745 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
754ef0cd 746 &delta, &deltatsc);
2f04fa88
YL
747
748 /* Calculate the scaled math multiplication factor */
749 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
750 lapic_clockevent.shift);
751 lapic_clockevent.max_delta_ns =
4aed89d6 752 clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
2f04fa88
YL
753 lapic_clockevent.min_delta_ns =
754 clockevent_delta2ns(0xF, &lapic_clockevent);
755
1ade93ef 756 lapic_timer_frequency = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
2f04fa88
YL
757
758 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
411462f6 759 apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
2f04fa88 760 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
1ade93ef 761 lapic_timer_frequency);
2f04fa88
YL
762
763 if (cpu_has_tsc) {
2f04fa88
YL
764 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
765 "%ld.%04ld MHz.\n",
754ef0cd
YI
766 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
767 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
2f04fa88
YL
768 }
769
770 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
771 "%u.%04u MHz.\n",
1ade93ef
JP
772 lapic_timer_frequency / (1000000 / HZ),
773 lapic_timer_frequency % (1000000 / HZ));
2f04fa88
YL
774
775 /*
776 * Do a sanity check on the APIC calibration result
777 */
1ade93ef 778 if (lapic_timer_frequency < (1000000 / HZ)) {
2f04fa88 779 local_irq_enable();
ba21ebb6 780 pr_warning("APIC frequency too slow, disabling apic timer\n");
2f04fa88
YL
781 return -1;
782 }
783
784 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
785
b189892d
CG
786 /*
787 * PM timer calibration failed or not turned on
788 * so lets try APIC timer based calibration
789 */
2f04fa88
YL
790 if (!pm_referenced) {
791 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
792
793 /*
794 * Setup the apic timer manually
795 */
796 levt->event_handler = lapic_cal_handler;
b23d8e52 797 lapic_timer_set_periodic(levt);
2f04fa88
YL
798 lapic_cal_loops = -1;
799
800 /* Let the interrupts run */
801 local_irq_enable();
802
803 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
804 cpu_relax();
805
2f04fa88 806 /* Stop the lapic timer */
c948c260 807 local_irq_disable();
b23d8e52 808 lapic_timer_shutdown(levt);
2f04fa88 809
2f04fa88
YL
810 /* Jiffies delta */
811 deltaj = lapic_cal_j2 - lapic_cal_j1;
812 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
813
814 /* Check, if the jiffies result is consistent */
815 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
816 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
817 else
818 levt->features |= CLOCK_EVT_FEAT_DUMMY;
c948c260
TG
819 }
820 local_irq_enable();
2f04fa88
YL
821
822 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
e423e33e 823 pr_warning("APIC timer disabled due to verification failure\n");
2f04fa88
YL
824 return -1;
825 }
826
827 return 0;
828}
829
e83a5fdc
HS
830/*
831 * Setup the boot APIC
832 *
833 * Calibrate and verify the result.
834 */
0e078e2f
TG
835void __init setup_boot_APIC_clock(void)
836{
837 /*
274cfe59
CG
838 * The local apic timer can be disabled via the kernel
839 * commandline or from the CPU detection code. Register the lapic
840 * timer as a dummy clock event source on SMP systems, so the
841 * broadcast mechanism is used. On UP systems simply ignore it.
0e078e2f
TG
842 */
843 if (disable_apic_timer) {
ba21ebb6 844 pr_info("Disabling APIC timer\n");
0e078e2f 845 /* No broadcast on UP ! */
9d09951d
TG
846 if (num_possible_cpus() > 1) {
847 lapic_clockevent.mult = 1;
0e078e2f 848 setup_APIC_timer();
9d09951d 849 }
0e078e2f
TG
850 return;
851 }
852
89b3b1f4 853 if (calibrate_APIC_clock()) {
c2b84b30
TG
854 /* No broadcast on UP ! */
855 if (num_possible_cpus() > 1)
856 setup_APIC_timer();
857 return;
858 }
859
0e078e2f
TG
860 /*
861 * If nmi_watchdog is set to IO_APIC, we need the
862 * PIT/HPET going. Otherwise register lapic as a dummy
863 * device.
864 */
072b198a 865 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
0e078e2f 866
274cfe59 867 /* Setup the lapic or request the broadcast */
0e078e2f
TG
868 setup_APIC_timer();
869}
870
148f9bb8 871void setup_secondary_APIC_clock(void)
0e078e2f 872{
0e078e2f
TG
873 setup_APIC_timer();
874}
875
876/*
877 * The guts of the apic timer interrupt
878 */
879static void local_apic_timer_interrupt(void)
880{
881 int cpu = smp_processor_id();
882 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
883
884 /*
885 * Normally we should not be here till LAPIC has been initialized but
886 * in some cases like kdump, its possible that there is a pending LAPIC
887 * timer interrupt from previous kernel's context and is delivered in
888 * new kernel the moment interrupts are enabled.
889 *
890 * Interrupts are enabled early and LAPIC is setup much later, hence
891 * its possible that when we get here evt->event_handler is NULL.
892 * Check for event_handler being NULL and discard the interrupt as
893 * spurious.
894 */
895 if (!evt->event_handler) {
ba21ebb6 896 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
0e078e2f 897 /* Switch it off */
b23d8e52 898 lapic_timer_shutdown(evt);
0e078e2f
TG
899 return;
900 }
901
902 /*
903 * the NMI deadlock-detector uses this.
904 */
915b0d01 905 inc_irq_stat(apic_timer_irqs);
0e078e2f
TG
906
907 evt->event_handler(evt);
908}
909
910/*
911 * Local APIC timer interrupt. This is the most natural way for doing
912 * local interrupts, but local timer interrupts can be emulated by
913 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
914 *
915 * [ if a single-CPU system runs an SMP kernel then we call the local
916 * interrupt as well. Thus we cannot inline the local irq ... ]
917 */
1d9090e2 918__visible void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
0e078e2f
TG
919{
920 struct pt_regs *old_regs = set_irq_regs(regs);
921
922 /*
923 * NOTE! We'd better ACK the irq immediately,
924 * because timer handling can be slow.
eddc0e92 925 *
0e078e2f
TG
926 * update_process_times() expects us to have done irq_enter().
927 * Besides, if we don't timer interrupts ignore the global
928 * interrupt lock, which is the WrongThing (tm) to do.
0e078e2f 929 */
eddc0e92 930 entering_ack_irq();
0e078e2f 931 local_apic_timer_interrupt();
eddc0e92 932 exiting_irq();
274cfe59 933
0e078e2f
TG
934 set_irq_regs(old_regs);
935}
936
1d9090e2 937__visible void __irq_entry smp_trace_apic_timer_interrupt(struct pt_regs *regs)
cf910e83
SA
938{
939 struct pt_regs *old_regs = set_irq_regs(regs);
940
0e078e2f 941 /*
cf910e83
SA
942 * NOTE! We'd better ACK the irq immediately,
943 * because timer handling can be slow.
944 *
0e078e2f
TG
945 * update_process_times() expects us to have done irq_enter().
946 * Besides, if we don't timer interrupts ignore the global
947 * interrupt lock, which is the WrongThing (tm) to do.
948 */
cf910e83
SA
949 entering_ack_irq();
950 trace_local_timer_entry(LOCAL_TIMER_VECTOR);
0e078e2f 951 local_apic_timer_interrupt();
cf910e83
SA
952 trace_local_timer_exit(LOCAL_TIMER_VECTOR);
953 exiting_irq();
274cfe59 954
0e078e2f
TG
955 set_irq_regs(old_regs);
956}
957
958int setup_profiling_timer(unsigned int multiplier)
959{
960 return -EINVAL;
961}
962
0e078e2f
TG
963/*
964 * Local APIC start and shutdown
965 */
966
967/**
968 * clear_local_APIC - shutdown the local APIC
969 *
970 * This is called, when a CPU is disabled and before rebooting, so the state of
971 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
972 * leftovers during boot.
973 */
974void clear_local_APIC(void)
975{
2584a82d 976 int maxlvt;
0e078e2f
TG
977 u32 v;
978
d3432896 979 /* APIC hasn't been mapped yet */
fc1edaf9 980 if (!x2apic_mode && !apic_phys)
d3432896
AK
981 return;
982
983 maxlvt = lapic_get_maxlvt();
0e078e2f
TG
984 /*
985 * Masking an LVT entry can trigger a local APIC error
986 * if the vector is zero. Mask LVTERR first to prevent this.
987 */
988 if (maxlvt >= 3) {
989 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
990 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
991 }
992 /*
993 * Careful: we have to set masks only first to deassert
994 * any level-triggered sources.
995 */
996 v = apic_read(APIC_LVTT);
997 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
998 v = apic_read(APIC_LVT0);
999 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1000 v = apic_read(APIC_LVT1);
1001 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
1002 if (maxlvt >= 4) {
1003 v = apic_read(APIC_LVTPC);
1004 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
1005 }
1006
6764014b 1007 /* lets not touch this if we didn't frob it */
4efc0670 1008#ifdef CONFIG_X86_THERMAL_VECTOR
6764014b
CG
1009 if (maxlvt >= 5) {
1010 v = apic_read(APIC_LVTTHMR);
1011 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
1012 }
1013#endif
5ca8681c
AK
1014#ifdef CONFIG_X86_MCE_INTEL
1015 if (maxlvt >= 6) {
1016 v = apic_read(APIC_LVTCMCI);
1017 if (!(v & APIC_LVT_MASKED))
1018 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
1019 }
1020#endif
1021
0e078e2f
TG
1022 /*
1023 * Clean APIC state for other OSs:
1024 */
1025 apic_write(APIC_LVTT, APIC_LVT_MASKED);
1026 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1027 apic_write(APIC_LVT1, APIC_LVT_MASKED);
1028 if (maxlvt >= 3)
1029 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
1030 if (maxlvt >= 4)
1031 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
6764014b
CG
1032
1033 /* Integrated APIC (!82489DX) ? */
1034 if (lapic_is_integrated()) {
1035 if (maxlvt > 3)
1036 /* Clear ESR due to Pentium errata 3AP and 11AP */
1037 apic_write(APIC_ESR, 0);
1038 apic_read(APIC_ESR);
1039 }
0e078e2f
TG
1040}
1041
1042/**
1043 * disable_local_APIC - clear and disable the local APIC
1044 */
1045void disable_local_APIC(void)
1046{
1047 unsigned int value;
1048
4a13ad0b 1049 /* APIC hasn't been mapped yet */
fd19dce7 1050 if (!x2apic_mode && !apic_phys)
4a13ad0b
JB
1051 return;
1052
0e078e2f
TG
1053 clear_local_APIC();
1054
1055 /*
1056 * Disable APIC (implies clearing of registers
1057 * for 82489DX!).
1058 */
1059 value = apic_read(APIC_SPIV);
1060 value &= ~APIC_SPIV_APIC_ENABLED;
1061 apic_write(APIC_SPIV, value);
990b183e
CG
1062
1063#ifdef CONFIG_X86_32
1064 /*
1065 * When LAPIC was disabled by the BIOS and enabled by the kernel,
1066 * restore the disabled state.
1067 */
1068 if (enabled_via_apicbase) {
1069 unsigned int l, h;
1070
1071 rdmsr(MSR_IA32_APICBASE, l, h);
1072 l &= ~MSR_IA32_APICBASE_ENABLE;
1073 wrmsr(MSR_IA32_APICBASE, l, h);
1074 }
1075#endif
0e078e2f
TG
1076}
1077
fe4024dc
CG
1078/*
1079 * If Linux enabled the LAPIC against the BIOS default disable it down before
1080 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
1081 * not power-off. Additionally clear all LVT entries before disable_local_APIC
1082 * for the case where Linux didn't enable the LAPIC.
1083 */
0e078e2f
TG
1084void lapic_shutdown(void)
1085{
1086 unsigned long flags;
1087
8312136f 1088 if (!cpu_has_apic && !apic_from_smp_config())
0e078e2f
TG
1089 return;
1090
1091 local_irq_save(flags);
1092
fe4024dc
CG
1093#ifdef CONFIG_X86_32
1094 if (!enabled_via_apicbase)
1095 clear_local_APIC();
1096 else
1097#endif
1098 disable_local_APIC();
1099
0e078e2f
TG
1100
1101 local_irq_restore(flags);
1102}
1103
0e078e2f
TG
1104/**
1105 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1106 */
1da177e4
LT
1107void __init sync_Arb_IDs(void)
1108{
296cb951
CG
1109 /*
1110 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1111 * needed on AMD.
1112 */
1113 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1da177e4
LT
1114 return;
1115
1116 /*
1117 * Wait for idle.
1118 */
1119 apic_wait_icr_idle();
1120
1121 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
6f6da97f
CG
1122 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1123 APIC_INT_LEVELTRIG | APIC_DM_INIT);
1da177e4
LT
1124}
1125
1da177e4
LT
1126/*
1127 * An initial setup of the virtual wire mode.
1128 */
1129void __init init_bsp_APIC(void)
1130{
11a8e778 1131 unsigned int value;
1da177e4
LT
1132
1133 /*
1134 * Don't do the setup now if we have a SMP BIOS as the
1135 * through-I/O-APIC virtual wire mode might be active.
1136 */
1137 if (smp_found_config || !cpu_has_apic)
1138 return;
1139
1da177e4
LT
1140 /*
1141 * Do not trust the local APIC being empty at bootup.
1142 */
1143 clear_local_APIC();
1144
1145 /*
1146 * Enable APIC.
1147 */
1148 value = apic_read(APIC_SPIV);
1149 value &= ~APIC_VECTOR_MASK;
1150 value |= APIC_SPIV_APIC_ENABLED;
638c0411
CG
1151
1152#ifdef CONFIG_X86_32
1153 /* This bit is reserved on P4/Xeon and should be cleared */
1154 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1155 (boot_cpu_data.x86 == 15))
1156 value &= ~APIC_SPIV_FOCUS_DISABLED;
1157 else
1158#endif
1159 value |= APIC_SPIV_FOCUS_DISABLED;
1da177e4 1160 value |= SPURIOUS_APIC_VECTOR;
11a8e778 1161 apic_write(APIC_SPIV, value);
1da177e4
LT
1162
1163 /*
1164 * Set up the virtual wire mode.
1165 */
11a8e778 1166 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4 1167 value = APIC_DM_NMI;
638c0411
CG
1168 if (!lapic_is_integrated()) /* 82489DX */
1169 value |= APIC_LVT_LEVEL_TRIGGER;
b7c4948e
HK
1170 if (apic_extnmi == APIC_EXTNMI_NONE)
1171 value |= APIC_LVT_MASKED;
11a8e778 1172 apic_write(APIC_LVT1, value);
1da177e4
LT
1173}
1174
148f9bb8 1175static void lapic_setup_esr(void)
c43da2f5 1176{
9df08f10
CG
1177 unsigned int oldvalue, value, maxlvt;
1178
1179 if (!lapic_is_integrated()) {
ba21ebb6 1180 pr_info("No ESR for 82489DX.\n");
9df08f10
CG
1181 return;
1182 }
c43da2f5 1183
08125d3e 1184 if (apic->disable_esr) {
c43da2f5 1185 /*
9df08f10
CG
1186 * Something untraceable is creating bad interrupts on
1187 * secondary quads ... for the moment, just leave the
1188 * ESR disabled - we can't do anything useful with the
1189 * errors anyway - mbligh
c43da2f5 1190 */
ba21ebb6 1191 pr_info("Leaving ESR disabled.\n");
9df08f10 1192 return;
c43da2f5 1193 }
9df08f10
CG
1194
1195 maxlvt = lapic_get_maxlvt();
1196 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1197 apic_write(APIC_ESR, 0);
1198 oldvalue = apic_read(APIC_ESR);
1199
1200 /* enables sending errors */
1201 value = ERROR_APIC_VECTOR;
1202 apic_write(APIC_LVTERR, value);
1203
1204 /*
1205 * spec says clear errors after enabling vector.
1206 */
1207 if (maxlvt > 3)
1208 apic_write(APIC_ESR, 0);
1209 value = apic_read(APIC_ESR);
1210 if (value != oldvalue)
1211 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1212 "vector: 0x%08x after: 0x%08x\n",
1213 oldvalue, value);
c43da2f5
CG
1214}
1215
0e078e2f
TG
1216/**
1217 * setup_local_APIC - setup the local APIC
0aa002fe
TH
1218 *
1219 * Used to setup local APIC while initializing BSP or bringin up APs.
1220 * Always called with preemption disabled.
0e078e2f 1221 */
148f9bb8 1222void setup_local_APIC(void)
1da177e4 1223{
0aa002fe 1224 int cpu = smp_processor_id();
8c3ba8d0
KJ
1225 unsigned int value, queued;
1226 int i, j, acked = 0;
1227 unsigned long long tsc = 0, ntsc;
b47dcbdc 1228 long long max_loops = cpu_khz ? cpu_khz : 1000000;
8c3ba8d0
KJ
1229
1230 if (cpu_has_tsc)
4ea1636b 1231 tsc = rdtsc();
1da177e4 1232
f1182638 1233 if (disable_apic) {
7167d08e 1234 disable_ioapic_support();
f1182638
JB
1235 return;
1236 }
1237
89c38c28
CG
1238#ifdef CONFIG_X86_32
1239 /* Pound the ESR really hard over the head with a big hammer - mbligh */
08125d3e 1240 if (lapic_is_integrated() && apic->disable_esr) {
89c38c28
CG
1241 apic_write(APIC_ESR, 0);
1242 apic_write(APIC_ESR, 0);
1243 apic_write(APIC_ESR, 0);
1244 apic_write(APIC_ESR, 0);
1245 }
1246#endif
cdd6c482 1247 perf_events_lapic_init();
89c38c28 1248
1da177e4
LT
1249 /*
1250 * Double-check whether this APIC is really registered.
1251 * This is meaningless in clustered apic mode, so we skip it.
1252 */
c2777f98 1253 BUG_ON(!apic->apic_id_registered());
1da177e4
LT
1254
1255 /*
1256 * Intel recommends to set DFR, LDR and TPR before enabling
1257 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1258 * document number 292116). So here it goes...
1259 */
a5c43296 1260 apic->init_apic_ldr();
1da177e4 1261
6f802c4b
TH
1262#ifdef CONFIG_X86_32
1263 /*
acb8bc09
TH
1264 * APIC LDR is initialized. If logical_apicid mapping was
1265 * initialized during get_smp_config(), make sure it matches the
1266 * actual value.
6f802c4b 1267 */
acb8bc09
TH
1268 i = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
1269 WARN_ON(i != BAD_APICID && i != logical_smp_processor_id());
1270 /* always use the value from LDR */
6f802c4b
TH
1271 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
1272 logical_smp_processor_id();
1273#endif
1274
1da177e4
LT
1275 /*
1276 * Set Task Priority to 'accept all'. We never change this
1277 * later on.
1278 */
1279 value = apic_read(APIC_TASKPRI);
1280 value &= ~APIC_TPRI_MASK;
11a8e778 1281 apic_write(APIC_TASKPRI, value);
1da177e4 1282
da7ed9f9
VG
1283 /*
1284 * After a crash, we no longer service the interrupts and a pending
1285 * interrupt from previous kernel might still have ISR bit set.
1286 *
1287 * Most probably by now CPU has serviced that pending interrupt and
1288 * it might not have done the ack_APIC_irq() because it thought,
1289 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1290 * does not clear the ISR bit and cpu thinks it has already serivced
1291 * the interrupt. Hence a vector might get locked. It was noticed
1292 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1293 */
8c3ba8d0
KJ
1294 do {
1295 queued = 0;
1296 for (i = APIC_ISR_NR - 1; i >= 0; i--)
1297 queued |= apic_read(APIC_IRR + i*0x10);
1298
1299 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1300 value = apic_read(APIC_ISR + i*0x10);
1301 for (j = 31; j >= 0; j--) {
1302 if (value & (1<<j)) {
1303 ack_APIC_irq();
1304 acked++;
1305 }
1306 }
da7ed9f9 1307 }
8c3ba8d0
KJ
1308 if (acked > 256) {
1309 printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
1310 acked);
1311 break;
1312 }
42fa4250 1313 if (queued) {
b47dcbdc 1314 if (cpu_has_tsc && cpu_khz) {
4ea1636b 1315 ntsc = rdtsc();
42fa4250
SF
1316 max_loops = (cpu_khz << 10) - (ntsc - tsc);
1317 } else
1318 max_loops--;
1319 }
8c3ba8d0
KJ
1320 } while (queued && max_loops > 0);
1321 WARN_ON(max_loops <= 0);
da7ed9f9 1322
1da177e4
LT
1323 /*
1324 * Now that we are all set up, enable the APIC
1325 */
1326 value = apic_read(APIC_SPIV);
1327 value &= ~APIC_VECTOR_MASK;
1328 /*
1329 * Enable APIC
1330 */
1331 value |= APIC_SPIV_APIC_ENABLED;
1332
89c38c28
CG
1333#ifdef CONFIG_X86_32
1334 /*
1335 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1336 * certain networking cards. If high frequency interrupts are
1337 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1338 * entry is masked/unmasked at a high rate as well then sooner or
1339 * later IOAPIC line gets 'stuck', no more interrupts are received
1340 * from the device. If focus CPU is disabled then the hang goes
1341 * away, oh well :-(
1342 *
1343 * [ This bug can be reproduced easily with a level-triggered
1344 * PCI Ne2000 networking cards and PII/PIII processors, dual
1345 * BX chipset. ]
1346 */
1347 /*
1348 * Actually disabling the focus CPU check just makes the hang less
1349 * frequent as it makes the interrupt distributon model be more
1350 * like LRU than MRU (the short-term load is more even across CPUs).
1351 * See also the comment in end_level_ioapic_irq(). --macro
1352 */
1353
1354 /*
1355 * - enable focus processor (bit==0)
1356 * - 64bit mode always use processor focus
1357 * so no need to set it
1358 */
1359 value &= ~APIC_SPIV_FOCUS_DISABLED;
1360#endif
3f14c746 1361
1da177e4
LT
1362 /*
1363 * Set spurious IRQ vector
1364 */
1365 value |= SPURIOUS_APIC_VECTOR;
11a8e778 1366 apic_write(APIC_SPIV, value);
1da177e4
LT
1367
1368 /*
1369 * Set up LVT0, LVT1:
1370 *
1371 * set up through-local-APIC on the BP's LINT0. This is not
1372 * strictly necessary in pure symmetric-IO mode, but sometimes
1373 * we delegate interrupts to the 8259A.
1374 */
1375 /*
1376 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1377 */
1378 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
0aa002fe 1379 if (!cpu && (pic_mode || !value)) {
1da177e4 1380 value = APIC_DM_EXTINT;
0aa002fe 1381 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
1da177e4
LT
1382 } else {
1383 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
0aa002fe 1384 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
1da177e4 1385 }
11a8e778 1386 apic_write(APIC_LVT0, value);
1da177e4
LT
1387
1388 /*
b7c4948e
HK
1389 * Only the BSP sees the LINT1 NMI signal by default. This can be
1390 * modified by apic_extnmi= boot option.
1da177e4 1391 */
b7c4948e
HK
1392 if ((!cpu && apic_extnmi != APIC_EXTNMI_NONE) ||
1393 apic_extnmi == APIC_EXTNMI_ALL)
1da177e4
LT
1394 value = APIC_DM_NMI;
1395 else
1396 value = APIC_DM_NMI | APIC_LVT_MASKED;
89c38c28
CG
1397 if (!lapic_is_integrated()) /* 82489DX */
1398 value |= APIC_LVT_LEVEL_TRIGGER;
11a8e778 1399 apic_write(APIC_LVT1, value);
89c38c28 1400
be71b855
AK
1401#ifdef CONFIG_X86_MCE_INTEL
1402 /* Recheck CMCI information after local APIC is up on CPU #0 */
0aa002fe 1403 if (!cpu)
be71b855
AK
1404 cmci_recheck();
1405#endif
739f33b3 1406}
1da177e4 1407
05f7e46d 1408static void end_local_APIC_setup(void)
739f33b3
AK
1409{
1410 lapic_setup_esr();
fa6b95fc
CG
1411
1412#ifdef CONFIG_X86_32
1b4ee4e4
CG
1413 {
1414 unsigned int value;
1415 /* Disable the local apic timer */
1416 value = apic_read(APIC_LVTT);
1417 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1418 apic_write(APIC_LVTT, value);
1419 }
fa6b95fc
CG
1420#endif
1421
0e078e2f 1422 apic_pm_activate();
2fb270f3
JB
1423}
1424
05f7e46d
TG
1425/*
1426 * APIC setup function for application processors. Called from smpboot.c
1427 */
1428void apic_ap_setup(void)
2fb270f3 1429{
05f7e46d 1430 setup_local_APIC();
2fb270f3 1431 end_local_APIC_setup();
1da177e4 1432}
1da177e4 1433
06cd9a7d 1434#ifdef CONFIG_X86_X2APIC
bfb05070 1435int x2apic_mode;
12e189d3
TG
1436
1437enum {
1438 X2APIC_OFF,
1439 X2APIC_ON,
1440 X2APIC_DISABLED,
1441};
1442static int x2apic_state;
1443
d786ad32 1444static void __x2apic_disable(void)
44e25ff9
TG
1445{
1446 u64 msr;
1447
a57e456a 1448 if (!cpu_has_apic)
659006bf
TG
1449 return;
1450
44e25ff9
TG
1451 rdmsrl(MSR_IA32_APICBASE, msr);
1452 if (!(msr & X2APIC_ENABLE))
1453 return;
1454 /* Disable xapic and x2apic first and then reenable xapic mode */
1455 wrmsrl(MSR_IA32_APICBASE, msr & ~(X2APIC_ENABLE | XAPIC_ENABLE));
1456 wrmsrl(MSR_IA32_APICBASE, msr & ~X2APIC_ENABLE);
1457 printk_once(KERN_INFO "x2apic disabled\n");
1458}
1459
d786ad32 1460static void __x2apic_enable(void)
659006bf
TG
1461{
1462 u64 msr;
1463
1464 rdmsrl(MSR_IA32_APICBASE, msr);
1465 if (msr & X2APIC_ENABLE)
1466 return;
1467 wrmsrl(MSR_IA32_APICBASE, msr | X2APIC_ENABLE);
1468 printk_once(KERN_INFO "x2apic enabled\n");
1469}
1470
bfb05070
TG
1471static int __init setup_nox2apic(char *str)
1472{
1473 if (x2apic_enabled()) {
1474 int apicid = native_apic_msr_read(APIC_ID);
1475
1476 if (apicid >= 255) {
1477 pr_warning("Apicid: %08x, cannot enforce nox2apic\n",
1478 apicid);
1479 return 0;
1480 }
44e25ff9
TG
1481 pr_warning("x2apic already enabled.\n");
1482 __x2apic_disable();
1483 }
1484 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
12e189d3 1485 x2apic_state = X2APIC_DISABLED;
44e25ff9 1486 x2apic_mode = 0;
bfb05070
TG
1487 return 0;
1488}
1489early_param("nox2apic", setup_nox2apic);
1490
659006bf
TG
1491/* Called from cpu_init() to enable x2apic on (secondary) cpus */
1492void x2apic_setup(void)
1493{
1494 /*
1495 * If x2apic is not in ON state, disable it if already enabled
1496 * from BIOS.
1497 */
1498 if (x2apic_state != X2APIC_ON) {
1499 __x2apic_disable();
1500 return;
1501 }
1502 __x2apic_enable();
1503}
1504
44e25ff9 1505static __init void x2apic_disable(void)
fb209bd8 1506{
a57e456a 1507 u32 x2apic_id, state = x2apic_state;
fb209bd8 1508
a57e456a
TG
1509 x2apic_mode = 0;
1510 x2apic_state = X2APIC_DISABLED;
1511
1512 if (state != X2APIC_ON)
1513 return;
fb209bd8 1514
6d2d49d2
TG
1515 x2apic_id = read_apic_id();
1516 if (x2apic_id >= 255)
1517 panic("Cannot disable x2apic, id: %08x\n", x2apic_id);
9aa16365 1518
6d2d49d2
TG
1519 __x2apic_disable();
1520 register_lapic_address(mp_lapic_addr);
fb209bd8
YL
1521}
1522
659006bf 1523static __init void x2apic_enable(void)
6e1cb38a 1524{
659006bf 1525 if (x2apic_state != X2APIC_OFF)
06cd9a7d
YL
1526 return;
1527
659006bf 1528 x2apic_mode = 1;
12e189d3 1529 x2apic_state = X2APIC_ON;
659006bf 1530 __x2apic_enable();
6e1cb38a 1531}
d524165c 1532
62e61633 1533static __init void try_to_enable_x2apic(int remap_mode)
07806c50 1534{
659006bf 1535 if (x2apic_state == X2APIC_DISABLED)
07806c50
JL
1536 return;
1537
62e61633 1538 if (remap_mode != IRQ_REMAP_X2APIC_MODE) {
07806c50
JL
1539 /* IR is required if there is APIC ID > 255 even when running
1540 * under KVM
1541 */
1542 if (max_physical_apicid > 255 ||
8329aa9f 1543 !hypervisor_x2apic_available()) {
62e61633 1544 pr_info("x2apic: IRQ remapping doesn't support X2APIC mode\n");
44e25ff9 1545 x2apic_disable();
07806c50
JL
1546 return;
1547 }
1548
1549 /*
1550 * without IR all CPUs can be addressed by IOAPIC/MSI
1551 * only in physical mode
1552 */
55eae7de 1553 x2apic_phys = 1;
07806c50 1554 }
659006bf 1555 x2apic_enable();
55eae7de
TG
1556}
1557
1558void __init check_x2apic(void)
1559{
1560 if (x2apic_enabled()) {
1561 pr_info("x2apic: enabled by BIOS, switching to x2apic ops\n");
1562 x2apic_mode = 1;
12e189d3
TG
1563 x2apic_state = X2APIC_ON;
1564 } else if (!cpu_has_x2apic) {
1565 x2apic_state = X2APIC_DISABLED;
55eae7de
TG
1566 }
1567}
1568#else /* CONFIG_X86_X2APIC */
1569static int __init validate_x2apic(void)
1570{
1571 if (!apic_is_x2apic_enabled())
1572 return 0;
1573 /*
1574 * Checkme: Can we simply turn off x2apic here instead of panic?
1575 */
1576 panic("BIOS has enabled x2apic but kernel doesn't support x2apic, please disable x2apic in BIOS.\n");
1577}
1578early_initcall(validate_x2apic);
1579
62e61633 1580static inline void try_to_enable_x2apic(int remap_mode) { }
659006bf 1581static inline void __x2apic_enable(void) { }
55eae7de
TG
1582#endif /* !CONFIG_X86_X2APIC */
1583
1584static int __init try_to_enable_IR(void)
1585{
1586#ifdef CONFIG_X86_IO_APIC
1587 if (!x2apic_enabled() && skip_ioapic_setup) {
1588 pr_info("Not enabling interrupt remapping due to skipped IO-APIC setup\n");
1589 return -1;
1590 }
ce69a784 1591#endif
55eae7de 1592 return irq_remapping_enable();
ce69a784
GN
1593}
1594
1595void __init enable_IR_x2apic(void)
1596{
1597 unsigned long flags;
07806c50 1598 int ret, ir_stat;
b7f42ab2 1599
07806c50
JL
1600 ir_stat = irq_remapping_prepare();
1601 if (ir_stat < 0 && !x2apic_supported())
e670761f 1602 return;
ce69a784 1603
31dce14a 1604 ret = save_ioapic_entries();
5ffa4eb2 1605 if (ret) {
ba21ebb6 1606 pr_info("Saving IO-APIC state failed: %d\n", ret);
fb209bd8 1607 return;
5ffa4eb2 1608 }
6e1cb38a 1609
05c3dc2c 1610 local_irq_save(flags);
b81bb373 1611 legacy_pic->mask_all();
31dce14a 1612 mask_ioapic_entries();
05c3dc2c 1613
6a6256f9 1614 /* If irq_remapping_prepare() succeeded, try to enable it */
07806c50
JL
1615 if (ir_stat >= 0)
1616 ir_stat = try_to_enable_IR();
1617 /* ir_stat contains the remap mode or an error code */
1618 try_to_enable_x2apic(ir_stat);
a31bc327 1619
07806c50 1620 if (ir_stat < 0)
31dce14a 1621 restore_ioapic_entries();
b81bb373 1622 legacy_pic->restore_mask();
6e1cb38a 1623 local_irq_restore(flags);
6e1cb38a 1624}
93758238 1625
be7a656f 1626#ifdef CONFIG_X86_64
1da177e4
LT
1627/*
1628 * Detect and enable local APICs on non-SMP boards.
1629 * Original code written by Keir Fraser.
1630 * On AMD64 we trust the BIOS - if it says no APIC it is likely
6935d1f9 1631 * not correctly set up (usually the APIC timer won't work etc.)
1da177e4 1632 */
0e078e2f 1633static int __init detect_init_APIC(void)
1da177e4
LT
1634{
1635 if (!cpu_has_apic) {
ba21ebb6 1636 pr_info("No local APIC present\n");
1da177e4
LT
1637 return -1;
1638 }
1639
1640 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1da177e4
LT
1641 return 0;
1642}
be7a656f 1643#else
5a7ae78f 1644
25874a29 1645static int __init apic_verify(void)
5a7ae78f
TG
1646{
1647 u32 features, h, l;
1648
1649 /*
1650 * The APIC feature bit should now be enabled
1651 * in `cpuid'
1652 */
1653 features = cpuid_edx(1);
1654 if (!(features & (1 << X86_FEATURE_APIC))) {
1655 pr_warning("Could not enable APIC!\n");
1656 return -1;
1657 }
1658 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1659 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1660
1661 /* The BIOS may have set up the APIC at some other address */
cbf2829b
BD
1662 if (boot_cpu_data.x86 >= 6) {
1663 rdmsr(MSR_IA32_APICBASE, l, h);
1664 if (l & MSR_IA32_APICBASE_ENABLE)
1665 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1666 }
5a7ae78f
TG
1667
1668 pr_info("Found and enabled local APIC!\n");
1669 return 0;
1670}
1671
25874a29 1672int __init apic_force_enable(unsigned long addr)
5a7ae78f
TG
1673{
1674 u32 h, l;
1675
1676 if (disable_apic)
1677 return -1;
1678
1679 /*
1680 * Some BIOSes disable the local APIC in the APIC_BASE
1681 * MSR. This can only be done in software for Intel P6 or later
1682 * and AMD K7 (Model > 1) or later.
1683 */
cbf2829b
BD
1684 if (boot_cpu_data.x86 >= 6) {
1685 rdmsr(MSR_IA32_APICBASE, l, h);
1686 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1687 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1688 l &= ~MSR_IA32_APICBASE_BASE;
1689 l |= MSR_IA32_APICBASE_ENABLE | addr;
1690 wrmsr(MSR_IA32_APICBASE, l, h);
1691 enabled_via_apicbase = 1;
1692 }
5a7ae78f
TG
1693 }
1694 return apic_verify();
1695}
1696
be7a656f
YL
1697/*
1698 * Detect and initialize APIC
1699 */
1700static int __init detect_init_APIC(void)
1701{
be7a656f
YL
1702 /* Disabled by kernel option? */
1703 if (disable_apic)
1704 return -1;
1705
1706 switch (boot_cpu_data.x86_vendor) {
1707 case X86_VENDOR_AMD:
1708 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
85877061 1709 (boot_cpu_data.x86 >= 15))
be7a656f
YL
1710 break;
1711 goto no_apic;
1712 case X86_VENDOR_INTEL:
1713 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1714 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1715 break;
1716 goto no_apic;
1717 default:
1718 goto no_apic;
1719 }
1720
1721 if (!cpu_has_apic) {
1722 /*
1723 * Over-ride BIOS and try to enable the local APIC only if
1724 * "lapic" specified.
1725 */
1726 if (!force_enable_local_apic) {
ba21ebb6
CG
1727 pr_info("Local APIC disabled by BIOS -- "
1728 "you can enable it with \"lapic\"\n");
be7a656f
YL
1729 return -1;
1730 }
a906fdaa 1731 if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
5a7ae78f
TG
1732 return -1;
1733 } else {
1734 if (apic_verify())
1735 return -1;
be7a656f 1736 }
be7a656f
YL
1737
1738 apic_pm_activate();
1739
1740 return 0;
1741
1742no_apic:
ba21ebb6 1743 pr_info("No local APIC present or hardware disabled\n");
be7a656f
YL
1744 return -1;
1745}
1746#endif
1da177e4 1747
0e078e2f
TG
1748/**
1749 * init_apic_mappings - initialize APIC mappings
1750 */
1da177e4
LT
1751void __init init_apic_mappings(void)
1752{
4401da61
YL
1753 unsigned int new_apicid;
1754
fc1edaf9 1755 if (x2apic_mode) {
4c9961d5 1756 boot_cpu_physical_apicid = read_apic_id();
6e1cb38a
SS
1757 return;
1758 }
1759
4797f6b0 1760 /* If no local APIC can be found return early */
1da177e4 1761 if (!smp_found_config && detect_init_APIC()) {
4797f6b0
YL
1762 /* lets NOP'ify apic operations */
1763 pr_info("APIC: disable apic facility\n");
1764 apic_disable();
1765 } else {
1da177e4
LT
1766 apic_phys = mp_lapic_addr;
1767
4797f6b0
YL
1768 /*
1769 * acpi lapic path already maps that address in
1770 * acpi_register_lapic_address()
1771 */
5989cd6a 1772 if (!acpi_lapic && !smp_found_config)
326a2e6b 1773 register_lapic_address(apic_phys);
cec6be6d 1774 }
1da177e4
LT
1775
1776 /*
1777 * Fetch the APIC ID of the BSP in case we have a
1778 * default configuration (or the MP table is broken).
1779 */
4401da61
YL
1780 new_apicid = read_apic_id();
1781 if (boot_cpu_physical_apicid != new_apicid) {
1782 boot_cpu_physical_apicid = new_apicid;
103428e5
CG
1783 /*
1784 * yeah -- we lie about apic_version
1785 * in case if apic was disabled via boot option
1786 * but it's not a problem for SMP compiled kernel
1787 * since smp_sanity_check is prepared for such a case
1788 * and disable smp mode
1789 */
4401da61
YL
1790 apic_version[new_apicid] =
1791 GET_APIC_VERSION(apic_read(APIC_LVR));
08306ce6 1792 }
1da177e4
LT
1793}
1794
c0104d38
YL
1795void __init register_lapic_address(unsigned long address)
1796{
1797 mp_lapic_addr = address;
1798
0450193b
YL
1799 if (!x2apic_mode) {
1800 set_fixmap_nocache(FIX_APIC_BASE, address);
1801 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1802 APIC_BASE, mp_lapic_addr);
1803 }
c0104d38
YL
1804 if (boot_cpu_physical_apicid == -1U) {
1805 boot_cpu_physical_apicid = read_apic_id();
1806 apic_version[boot_cpu_physical_apicid] =
1807 GET_APIC_VERSION(apic_read(APIC_LVR));
1808 }
1809}
1810
56d91f13 1811int apic_version[MAX_LOCAL_APIC];
1b313f4a 1812
1da177e4 1813/*
0e078e2f 1814 * Local APIC interrupts
1da177e4
LT
1815 */
1816
0e078e2f
TG
1817/*
1818 * This interrupt should _never_ happen with our APIC/SMP architecture
1819 */
d786ad32 1820static void __smp_spurious_interrupt(u8 vector)
1da177e4 1821{
dc1528dd
YL
1822 u32 v;
1823
1da177e4 1824 /*
0e078e2f
TG
1825 * Check if this really is a spurious interrupt and ACK it
1826 * if it is a vectored one. Just in case...
1827 * Spurious interrupts should not be ACKed.
1da177e4 1828 */
2414e021
JB
1829 v = apic_read(APIC_ISR + ((vector & ~0x1f) >> 1));
1830 if (v & (1 << (vector & 0x1f)))
0e078e2f 1831 ack_APIC_irq();
c4d58cbd 1832
915b0d01
HS
1833 inc_irq_stat(irq_spurious_count);
1834
dc1528dd 1835 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
2414e021
JB
1836 pr_info("spurious APIC interrupt through vector %02x on CPU#%d, "
1837 "should never happen.\n", vector, smp_processor_id());
eddc0e92
SA
1838}
1839
1d9090e2 1840__visible void smp_spurious_interrupt(struct pt_regs *regs)
eddc0e92
SA
1841{
1842 entering_irq();
2414e021 1843 __smp_spurious_interrupt(~regs->orig_ax);
eddc0e92 1844 exiting_irq();
0e078e2f 1845}
1da177e4 1846
1d9090e2 1847__visible void smp_trace_spurious_interrupt(struct pt_regs *regs)
cf910e83 1848{
2414e021
JB
1849 u8 vector = ~regs->orig_ax;
1850
cf910e83 1851 entering_irq();
2414e021
JB
1852 trace_spurious_apic_entry(vector);
1853 __smp_spurious_interrupt(vector);
1854 trace_spurious_apic_exit(vector);
cf910e83 1855 exiting_irq();
0e078e2f 1856}
1da177e4 1857
0e078e2f
TG
1858/*
1859 * This interrupt should never happen with our APIC/SMP architecture
1860 */
d786ad32 1861static void __smp_error_interrupt(struct pt_regs *regs)
0e078e2f 1862{
60283df7 1863 u32 v;
2b398bd9
YS
1864 u32 i = 0;
1865 static const char * const error_interrupt_reason[] = {
1866 "Send CS error", /* APIC Error Bit 0 */
1867 "Receive CS error", /* APIC Error Bit 1 */
1868 "Send accept error", /* APIC Error Bit 2 */
1869 "Receive accept error", /* APIC Error Bit 3 */
1870 "Redirectable IPI", /* APIC Error Bit 4 */
1871 "Send illegal vector", /* APIC Error Bit 5 */
1872 "Received illegal vector", /* APIC Error Bit 6 */
1873 "Illegal register address", /* APIC Error Bit 7 */
1874 };
1da177e4 1875
0e078e2f 1876 /* First tickle the hardware, only then report what went on. -- REW */
023de4a0
MR
1877 if (lapic_get_maxlvt() > 3) /* Due to the Pentium erratum 3AP. */
1878 apic_write(APIC_ESR, 0);
60283df7 1879 v = apic_read(APIC_ESR);
0e078e2f
TG
1880 ack_APIC_irq();
1881 atomic_inc(&irq_err_count);
ba7eda4c 1882
60283df7
RW
1883 apic_printk(APIC_DEBUG, KERN_DEBUG "APIC error on CPU%d: %02x",
1884 smp_processor_id(), v);
2b398bd9 1885
60283df7
RW
1886 v &= 0xff;
1887 while (v) {
1888 if (v & 0x1)
2b398bd9
YS
1889 apic_printk(APIC_DEBUG, KERN_CONT " : %s", error_interrupt_reason[i]);
1890 i++;
60283df7 1891 v >>= 1;
4b8073e4 1892 }
2b398bd9
YS
1893
1894 apic_printk(APIC_DEBUG, KERN_CONT "\n");
1895
eddc0e92
SA
1896}
1897
1d9090e2 1898__visible void smp_error_interrupt(struct pt_regs *regs)
eddc0e92
SA
1899{
1900 entering_irq();
1901 __smp_error_interrupt(regs);
1902 exiting_irq();
1da177e4
LT
1903}
1904
1d9090e2 1905__visible void smp_trace_error_interrupt(struct pt_regs *regs)
cf910e83
SA
1906{
1907 entering_irq();
1908 trace_error_apic_entry(ERROR_APIC_VECTOR);
1909 __smp_error_interrupt(regs);
1910 trace_error_apic_exit(ERROR_APIC_VECTOR);
1911 exiting_irq();
1da177e4
LT
1912}
1913
b5841765 1914/**
36c9d674
CG
1915 * connect_bsp_APIC - attach the APIC to the interrupt system
1916 */
05f7e46d 1917static void __init connect_bsp_APIC(void)
b5841765 1918{
36c9d674
CG
1919#ifdef CONFIG_X86_32
1920 if (pic_mode) {
1921 /*
1922 * Do not trust the local APIC being empty at bootup.
1923 */
1924 clear_local_APIC();
1925 /*
1926 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1927 * local APIC to INT and NMI lines.
1928 */
1929 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1930 "enabling APIC mode.\n");
c0eaa453 1931 imcr_pic_to_apic();
36c9d674
CG
1932 }
1933#endif
b5841765
GC
1934}
1935
274cfe59
CG
1936/**
1937 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1938 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1939 *
1940 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1941 * APIC is disabled.
1942 */
0e078e2f 1943void disconnect_bsp_APIC(int virt_wire_setup)
1da177e4 1944{
1b4ee4e4
CG
1945 unsigned int value;
1946
c177b0bc
CG
1947#ifdef CONFIG_X86_32
1948 if (pic_mode) {
1949 /*
1950 * Put the board back into PIC mode (has an effect only on
1951 * certain older boards). Note that APIC interrupts, including
1952 * IPIs, won't work beyond this point! The only exception are
1953 * INIT IPIs.
1954 */
1955 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1956 "entering PIC mode.\n");
c0eaa453 1957 imcr_apic_to_pic();
c177b0bc
CG
1958 return;
1959 }
1960#endif
1961
0e078e2f 1962 /* Go back to Virtual Wire compatibility mode */
1da177e4 1963
0e078e2f
TG
1964 /* For the spurious interrupt use vector F, and enable it */
1965 value = apic_read(APIC_SPIV);
1966 value &= ~APIC_VECTOR_MASK;
1967 value |= APIC_SPIV_APIC_ENABLED;
1968 value |= 0xf;
1969 apic_write(APIC_SPIV, value);
b8ce3359 1970
0e078e2f
TG
1971 if (!virt_wire_setup) {
1972 /*
1973 * For LVT0 make it edge triggered, active high,
1974 * external and enabled
1975 */
1976 value = apic_read(APIC_LVT0);
1977 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1978 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1979 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1980 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1981 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1982 apic_write(APIC_LVT0, value);
1983 } else {
1984 /* Disable LVT0 */
1985 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1986 }
b8ce3359 1987
c177b0bc
CG
1988 /*
1989 * For LVT1 make it edge triggered, active high,
1990 * nmi and enabled
1991 */
0e078e2f
TG
1992 value = apic_read(APIC_LVT1);
1993 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1994 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1995 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1996 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1997 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1998 apic_write(APIC_LVT1, value);
1da177e4
LT
1999}
2000
7e1f85f9 2001int generic_processor_info(int apicid, int version)
be8a5685 2002{
14cb6dcf
VG
2003 int cpu, max = nr_cpu_ids;
2004 bool boot_cpu_detected = physid_isset(boot_cpu_physical_apicid,
2005 phys_cpu_present_map);
2006
151e0c7d
HD
2007 /*
2008 * boot_cpu_physical_apicid is designed to have the apicid
2009 * returned by read_apic_id(), i.e, the apicid of the
2010 * currently booting-up processor. However, on some platforms,
5b4d1dbc 2011 * it is temporarily modified by the apicid reported as BSP
151e0c7d
HD
2012 * through MP table. Concretely:
2013 *
2014 * - arch/x86/kernel/mpparse.c: MP_processor_info()
2015 * - arch/x86/mm/amdtopology.c: amd_numa_init()
151e0c7d
HD
2016 *
2017 * This function is executed with the modified
2018 * boot_cpu_physical_apicid. So, disabled_cpu_apicid kernel
2019 * parameter doesn't work to disable APs on kdump 2nd kernel.
2020 *
2021 * Since fixing handling of boot_cpu_physical_apicid requires
2022 * another discussion and tests on each platform, we leave it
2023 * for now and here we use read_apic_id() directly in this
2024 * function, generic_processor_info().
2025 */
2026 if (disabled_cpu_apicid != BAD_APICID &&
2027 disabled_cpu_apicid != read_apic_id() &&
2028 disabled_cpu_apicid == apicid) {
2029 int thiscpu = num_processors + disabled_cpus;
2030
5b4d1dbc 2031 pr_warning("APIC: Disabling requested cpu."
151e0c7d
HD
2032 " Processor %d/0x%x ignored.\n",
2033 thiscpu, apicid);
2034
2035 disabled_cpus++;
2036 return -ENODEV;
2037 }
2038
14cb6dcf
VG
2039 /*
2040 * If boot cpu has not been detected yet, then only allow upto
2041 * nr_cpu_ids - 1 processors and keep one slot free for boot cpu
2042 */
2043 if (!boot_cpu_detected && num_processors >= nr_cpu_ids - 1 &&
2044 apicid != boot_cpu_physical_apicid) {
2045 int thiscpu = max + disabled_cpus - 1;
2046
2047 pr_warning(
2048 "ACPI: NR_CPUS/possible_cpus limit of %i almost"
2049 " reached. Keeping one slot for boot cpu."
2050 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2051
2052 disabled_cpus++;
7e1f85f9 2053 return -ENODEV;
14cb6dcf 2054 }
be8a5685 2055
3b11ce7f 2056 if (num_processors >= nr_cpu_ids) {
3b11ce7f
MT
2057 int thiscpu = max + disabled_cpus;
2058
2059 pr_warning(
2060 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
2061 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2062
2063 disabled_cpus++;
7e1f85f9 2064 return -EINVAL;
be8a5685
AS
2065 }
2066
2067 num_processors++;
be8a5685
AS
2068 if (apicid == boot_cpu_physical_apicid) {
2069 /*
2070 * x86_bios_cpu_apicid is required to have processors listed
2071 * in same order as logical cpu numbers. Hence the first
2072 * entry is BSP, and so on.
e5fea868
YL
2073 * boot_cpu_init() already hold bit 0 in cpu_present_mask
2074 * for BSP.
be8a5685
AS
2075 */
2076 cpu = 0;
e5fea868
YL
2077 } else
2078 cpu = cpumask_next_zero(-1, cpu_present_mask);
2079
1f12e32f
TG
2080 /*
2081 * This can happen on physical hotplug. The sanity check at boot time
2082 * is done from native_smp_prepare_cpus() after num_possible_cpus() is
2083 * established.
2084 */
2085 if (topology_update_package_map(apicid, cpu) < 0) {
2086 int thiscpu = max + disabled_cpus;
2087
2088 pr_warning("ACPI: Package limit reached. Processor %d/0x%x ignored.\n",
2089 thiscpu, apicid);
2090 disabled_cpus++;
2091 return -ENOSPC;
2092 }
2093
e5fea868
YL
2094 /*
2095 * Validate version
2096 */
2097 if (version == 0x0) {
2098 pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2099 cpu, apicid);
2100 version = 0x10;
be8a5685 2101 }
e5fea868
YL
2102 apic_version[apicid] = version;
2103
2104 if (version != apic_version[boot_cpu_physical_apicid]) {
2105 pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2106 apic_version[boot_cpu_physical_apicid], cpu, version);
2107 }
2108
2109 physid_set(apicid, phys_cpu_present_map);
e0da3364
YL
2110 if (apicid > max_physical_apicid)
2111 max_physical_apicid = apicid;
2112
3e5095d1 2113#if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
f10fcd47
TH
2114 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
2115 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1b313f4a 2116#endif
acb8bc09
TH
2117#ifdef CONFIG_X86_32
2118 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
2119 apic->x86_32_early_logical_apicid(cpu);
2120#endif
1de88cd4
MT
2121 set_cpu_possible(cpu, true);
2122 set_cpu_present(cpu, true);
7e1f85f9
JL
2123
2124 return cpu;
be8a5685
AS
2125}
2126
0c81c746
SS
2127int hard_smp_processor_id(void)
2128{
2129 return read_apic_id();
2130}
1dcdd3d1
IM
2131
2132void default_init_apic_ldr(void)
2133{
2134 unsigned long val;
2135
2136 apic_write(APIC_DFR, APIC_DFR_VALUE);
2137 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
2138 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
2139 apic_write(APIC_LDR, val);
2140}
2141
ff164324
AG
2142int default_cpu_mask_to_apicid_and(const struct cpumask *cpumask,
2143 const struct cpumask *andmask,
2144 unsigned int *apicid)
6398268d 2145{
ea3807ea 2146 unsigned int cpu;
6398268d
AG
2147
2148 for_each_cpu_and(cpu, cpumask, andmask) {
2149 if (cpumask_test_cpu(cpu, cpu_online_mask))
2150 break;
2151 }
ff164324 2152
ea3807ea 2153 if (likely(cpu < nr_cpu_ids)) {
a5a39156
AG
2154 *apicid = per_cpu(x86_cpu_to_apicid, cpu);
2155 return 0;
a5a39156 2156 }
ea3807ea
AG
2157
2158 return -EINVAL;
6398268d
AG
2159}
2160
1551df64
MT
2161/*
2162 * Override the generic EOI implementation with an optimized version.
2163 * Only called during early boot when only one CPU is active and with
2164 * interrupts disabled, so we know this does not race with actual APIC driver
2165 * use.
2166 */
2167void __init apic_set_eoi_write(void (*eoi_write)(u32 reg, u32 v))
2168{
2169 struct apic **drv;
2170
2171 for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
2172 /* Should happen once for each apic */
2173 WARN_ON((*drv)->eoi_write == eoi_write);
2174 (*drv)->eoi_write = eoi_write;
2175 }
2176}
2177
374aab33 2178static void __init apic_bsp_up_setup(void)
05f7e46d 2179{
374aab33
TG
2180#ifdef CONFIG_X86_64
2181 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
2182#else
05f7e46d 2183 /*
374aab33
TG
2184 * Hack: In case of kdump, after a crash, kernel might be booting
2185 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
2186 * might be zero if read from MP tables. Get it from LAPIC.
05f7e46d 2187 */
374aab33
TG
2188# ifdef CONFIG_CRASH_DUMP
2189 boot_cpu_physical_apicid = read_apic_id();
2190# endif
2191#endif
2192 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
05f7e46d
TG
2193}
2194
2195/**
2196 * apic_bsp_setup - Setup function for local apic and io-apic
374aab33 2197 * @upmode: Force UP mode (for APIC_init_uniprocessor)
05f7e46d
TG
2198 *
2199 * Returns:
2200 * apic_id of BSP APIC
2201 */
374aab33 2202int __init apic_bsp_setup(bool upmode)
05f7e46d
TG
2203{
2204 int id;
2205
2206 connect_bsp_APIC();
374aab33
TG
2207 if (upmode)
2208 apic_bsp_up_setup();
05f7e46d
TG
2209 setup_local_APIC();
2210
2211 if (x2apic_mode)
2212 id = apic_read(APIC_LDR);
2213 else
2214 id = GET_APIC_LOGICAL_ID(apic_read(APIC_LDR));
2215
2216 enable_IO_APIC();
374aab33
TG
2217 end_local_APIC_setup();
2218 irq_remap_enable_fault_handling();
05f7e46d 2219 setup_IO_APIC();
9c4d9c73
TG
2220 /* Setup local timer */
2221 x86_init.timers.setup_percpu_clockev();
05f7e46d
TG
2222 return id;
2223}
2224
e714a91f
TG
2225/*
2226 * This initializes the IO-APIC and APIC hardware if this is
2227 * a UP kernel.
2228 */
2229int __init APIC_init_uniprocessor(void)
2230{
2231 if (disable_apic) {
2232 pr_info("Apic disabled\n");
2233 return -1;
2234 }
2235#ifdef CONFIG_X86_64
2236 if (!cpu_has_apic) {
2237 disable_apic = 1;
2238 pr_info("Apic disabled by BIOS\n");
2239 return -1;
2240 }
2241#else
2242 if (!smp_found_config && !cpu_has_apic)
2243 return -1;
2244
2245 /*
2246 * Complain if the BIOS pretends there is one.
2247 */
2248 if (!cpu_has_apic &&
2249 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
2250 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
2251 boot_cpu_physical_apicid);
2252 return -1;
2253 }
2254#endif
2255
374aab33
TG
2256 if (!smp_found_config)
2257 disable_ioapic_support();
e714a91f 2258
374aab33 2259 default_setup_apic_routing();
374aab33 2260 apic_bsp_setup(true);
e714a91f
TG
2261 return 0;
2262}
2263
30b8b006
TG
2264#ifdef CONFIG_UP_LATE_INIT
2265void __init up_late_init(void)
2266{
2267 APIC_init_uniprocessor();
2268}
2269#endif
2270
89039b37 2271/*
0e078e2f 2272 * Power management
89039b37 2273 */
0e078e2f
TG
2274#ifdef CONFIG_PM
2275
2276static struct {
274cfe59
CG
2277 /*
2278 * 'active' is true if the local APIC was enabled by us and
2279 * not the BIOS; this signifies that we are also responsible
2280 * for disabling it before entering apm/acpi suspend
2281 */
0e078e2f
TG
2282 int active;
2283 /* r/w apic fields */
2284 unsigned int apic_id;
2285 unsigned int apic_taskpri;
2286 unsigned int apic_ldr;
2287 unsigned int apic_dfr;
2288 unsigned int apic_spiv;
2289 unsigned int apic_lvtt;
2290 unsigned int apic_lvtpc;
2291 unsigned int apic_lvt0;
2292 unsigned int apic_lvt1;
2293 unsigned int apic_lvterr;
2294 unsigned int apic_tmict;
2295 unsigned int apic_tdcr;
2296 unsigned int apic_thmr;
42baa258 2297 unsigned int apic_cmci;
0e078e2f
TG
2298} apic_pm_state;
2299
f3c6ea1b 2300static int lapic_suspend(void)
0e078e2f
TG
2301{
2302 unsigned long flags;
2303 int maxlvt;
89039b37 2304
0e078e2f
TG
2305 if (!apic_pm_state.active)
2306 return 0;
89039b37 2307
0e078e2f 2308 maxlvt = lapic_get_maxlvt();
89039b37 2309
2d7a66d0 2310 apic_pm_state.apic_id = apic_read(APIC_ID);
0e078e2f
TG
2311 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2312 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2313 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2314 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2315 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
2316 if (maxlvt >= 4)
2317 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
2318 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2319 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2320 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2321 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2322 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
4efc0670 2323#ifdef CONFIG_X86_THERMAL_VECTOR
0e078e2f
TG
2324 if (maxlvt >= 5)
2325 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2326#endif
42baa258
JG
2327#ifdef CONFIG_X86_MCE_INTEL
2328 if (maxlvt >= 6)
2329 apic_pm_state.apic_cmci = apic_read(APIC_LVTCMCI);
2330#endif
24968cfd 2331
0e078e2f
TG
2332 local_irq_save(flags);
2333 disable_local_APIC();
fc1edaf9 2334
70733e0c 2335 irq_remapping_disable();
fc1edaf9 2336
0e078e2f
TG
2337 local_irq_restore(flags);
2338 return 0;
1da177e4
LT
2339}
2340
f3c6ea1b 2341static void lapic_resume(void)
1da177e4 2342{
0e078e2f
TG
2343 unsigned int l, h;
2344 unsigned long flags;
31dce14a 2345 int maxlvt;
b24696bc 2346
0e078e2f 2347 if (!apic_pm_state.active)
f3c6ea1b 2348 return;
89b831ef 2349
0e078e2f 2350 local_irq_save(flags);
336224ba
JR
2351
2352 /*
2353 * IO-APIC and PIC have their own resume routines.
2354 * We just mask them here to make sure the interrupt
2355 * subsystem is completely quiet while we enable x2apic
2356 * and interrupt-remapping.
2357 */
2358 mask_ioapic_entries();
2359 legacy_pic->mask_all();
92206c90 2360
659006bf
TG
2361 if (x2apic_mode) {
2362 __x2apic_enable();
2363 } else {
92206c90
CG
2364 /*
2365 * Make sure the APICBASE points to the right address
2366 *
2367 * FIXME! This will be wrong if we ever support suspend on
2368 * SMP! We'll need to do this as part of the CPU restore!
2369 */
cbf2829b
BD
2370 if (boot_cpu_data.x86 >= 6) {
2371 rdmsr(MSR_IA32_APICBASE, l, h);
2372 l &= ~MSR_IA32_APICBASE_BASE;
2373 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2374 wrmsr(MSR_IA32_APICBASE, l, h);
2375 }
d5e629a6 2376 }
6e1cb38a 2377
b24696bc 2378 maxlvt = lapic_get_maxlvt();
0e078e2f
TG
2379 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2380 apic_write(APIC_ID, apic_pm_state.apic_id);
2381 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2382 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2383 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2384 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2385 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2386 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
42baa258 2387#ifdef CONFIG_X86_THERMAL_VECTOR
0e078e2f
TG
2388 if (maxlvt >= 5)
2389 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
42baa258
JG
2390#endif
2391#ifdef CONFIG_X86_MCE_INTEL
2392 if (maxlvt >= 6)
2393 apic_write(APIC_LVTCMCI, apic_pm_state.apic_cmci);
0e078e2f
TG
2394#endif
2395 if (maxlvt >= 4)
2396 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2397 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2398 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2399 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2400 apic_write(APIC_ESR, 0);
2401 apic_read(APIC_ESR);
2402 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2403 apic_write(APIC_ESR, 0);
2404 apic_read(APIC_ESR);
92206c90 2405
70733e0c 2406 irq_remapping_reenable(x2apic_mode);
31dce14a 2407
0e078e2f 2408 local_irq_restore(flags);
0e078e2f 2409}
b8ce3359 2410
274cfe59
CG
2411/*
2412 * This device has no shutdown method - fully functioning local APICs
2413 * are needed on every CPU up until machine_halt/restart/poweroff.
2414 */
2415
f3c6ea1b 2416static struct syscore_ops lapic_syscore_ops = {
0e078e2f
TG
2417 .resume = lapic_resume,
2418 .suspend = lapic_suspend,
2419};
b8ce3359 2420
148f9bb8 2421static void apic_pm_activate(void)
0e078e2f
TG
2422{
2423 apic_pm_state.active = 1;
1da177e4
LT
2424}
2425
0e078e2f 2426static int __init init_lapic_sysfs(void)
1da177e4 2427{
0e078e2f 2428 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
f3c6ea1b
RW
2429 if (cpu_has_apic)
2430 register_syscore_ops(&lapic_syscore_ops);
e83a5fdc 2431
f3c6ea1b 2432 return 0;
1da177e4 2433}
b24696bc
FY
2434
2435/* local apic needs to resume before other devices access its registers. */
2436core_initcall(init_lapic_sysfs);
0e078e2f
TG
2437
2438#else /* CONFIG_PM */
2439
2440static void apic_pm_activate(void) { }
2441
2442#endif /* CONFIG_PM */
1da177e4 2443
f28c0ae2 2444#ifdef CONFIG_X86_64
e0e42142 2445
148f9bb8
PG
2446static int multi_checked;
2447static int multi;
e0e42142 2448
148f9bb8 2449static int set_multi(const struct dmi_system_id *d)
e0e42142
YL
2450{
2451 if (multi)
2452 return 0;
6f0aced6 2453 pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
e0e42142
YL
2454 multi = 1;
2455 return 0;
2456}
2457
148f9bb8 2458static const struct dmi_system_id multi_dmi_table[] = {
e0e42142
YL
2459 {
2460 .callback = set_multi,
2461 .ident = "IBM System Summit2",
2462 .matches = {
2463 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2464 DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2465 },
2466 },
2467 {}
2468};
2469
148f9bb8 2470static void dmi_check_multi(void)
e0e42142
YL
2471{
2472 if (multi_checked)
2473 return;
2474
2475 dmi_check_system(multi_dmi_table);
2476 multi_checked = 1;
2477}
2478
2479/*
2480 * apic_is_clustered_box() -- Check if we can expect good TSC
2481 *
2482 * Thus far, the major user of this is IBM's Summit2 series:
2483 * Clustered boxes may have unsynced TSC problems if they are
2484 * multi-chassis.
2485 * Use DMI to check them
2486 */
148f9bb8 2487int apic_is_clustered_box(void)
e0e42142
YL
2488{
2489 dmi_check_multi();
411cf9ee 2490 return multi;
1da177e4 2491}
f28c0ae2 2492#endif
1da177e4
LT
2493
2494/*
0e078e2f 2495 * APIC command line parameters
1da177e4 2496 */
789fa735 2497static int __init setup_disableapic(char *arg)
6935d1f9 2498{
1da177e4 2499 disable_apic = 1;
9175fc06 2500 setup_clear_cpu_cap(X86_FEATURE_APIC);
2c8c0e6b
AK
2501 return 0;
2502}
2503early_param("disableapic", setup_disableapic);
1da177e4 2504
2c8c0e6b 2505/* same as disableapic, for compatibility */
789fa735 2506static int __init setup_nolapic(char *arg)
6935d1f9 2507{
789fa735 2508 return setup_disableapic(arg);
6935d1f9 2509}
2c8c0e6b 2510early_param("nolapic", setup_nolapic);
1da177e4 2511
2e7c2838
LT
2512static int __init parse_lapic_timer_c2_ok(char *arg)
2513{
2514 local_apic_timer_c2_ok = 1;
2515 return 0;
2516}
2517early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2518
36fef094 2519static int __init parse_disable_apic_timer(char *arg)
6935d1f9 2520{
1da177e4 2521 disable_apic_timer = 1;
36fef094 2522 return 0;
6935d1f9 2523}
36fef094
CG
2524early_param("noapictimer", parse_disable_apic_timer);
2525
2526static int __init parse_nolapic_timer(char *arg)
2527{
2528 disable_apic_timer = 1;
2529 return 0;
6935d1f9 2530}
36fef094 2531early_param("nolapic_timer", parse_nolapic_timer);
73dea47f 2532
79af9bec
CG
2533static int __init apic_set_verbosity(char *arg)
2534{
2535 if (!arg) {
2536#ifdef CONFIG_X86_64
2537 skip_ioapic_setup = 0;
79af9bec
CG
2538 return 0;
2539#endif
2540 return -EINVAL;
2541 }
2542
2543 if (strcmp("debug", arg) == 0)
2544 apic_verbosity = APIC_DEBUG;
2545 else if (strcmp("verbose", arg) == 0)
2546 apic_verbosity = APIC_VERBOSE;
2547 else {
ba21ebb6 2548 pr_warning("APIC Verbosity level %s not recognised"
79af9bec
CG
2549 " use apic=verbose or apic=debug\n", arg);
2550 return -EINVAL;
2551 }
2552
2553 return 0;
2554}
2555early_param("apic", apic_set_verbosity);
2556
1e934dda
YL
2557static int __init lapic_insert_resource(void)
2558{
2559 if (!apic_phys)
2560 return -1;
2561
2562 /* Put local APIC into the resource map. */
2563 lapic_resource.start = apic_phys;
2564 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2565 insert_resource(&iomem_resource, &lapic_resource);
2566
2567 return 0;
2568}
2569
2570/*
2571 * need call insert after e820_reserve_resources()
2572 * that is using request_resource
2573 */
2574late_initcall(lapic_insert_resource);
151e0c7d
HD
2575
2576static int __init apic_set_disabled_cpu_apicid(char *arg)
2577{
2578 if (!arg || !get_option(&arg, &disabled_cpu_apicid))
2579 return -EINVAL;
2580
2581 return 0;
2582}
2583early_param("disable_cpu_apicid", apic_set_disabled_cpu_apicid);
b7c4948e
HK
2584
2585static int __init apic_set_extnmi(char *arg)
2586{
2587 if (!arg)
2588 return -EINVAL;
2589
2590 if (!strncmp("all", arg, 3))
2591 apic_extnmi = APIC_EXTNMI_ALL;
2592 else if (!strncmp("none", arg, 4))
2593 apic_extnmi = APIC_EXTNMI_NONE;
2594 else if (!strncmp("bsp", arg, 3))
2595 apic_extnmi = APIC_EXTNMI_BSP;
2596 else {
2597 pr_warn("Unknown external NMI delivery mode `%s' ignored\n", arg);
2598 return -EINVAL;
2599 }
2600
2601 return 0;
2602}
2603early_param("apic_extnmi", apic_set_extnmi);