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