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