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