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