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