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